Skip to content

Commit 221399b

Browse files
Frank PraznikJiri Kosina
authored andcommitted
HID: sony: Simplify LED initialization and eliminate redundant copies when updating LED states
Directly set the initial LED states in the device state struct instead of copying them from a temporary array. This allows for the removal of a redundant "x = x" copy loop in sony_set_leds() that was taking place any time an LED was updated. It also allows for the simplifying of the parameters in functions dealing with LED initialization and updates since only a pointer to the sony_sc struct is needed now. Signed-off-by: Frank Praznik <[email protected]> Acked-by: Pavel Machek <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent 8f069fd commit 221399b

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

drivers/hid/hid-sony.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,7 @@ static int dualshock4_set_operational_bt(struct hid_device *hdev)
13061306
return ret;
13071307
}
13081308

1309-
static void sixaxis_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1309+
static void sixaxis_set_leds_from_id(struct sony_sc *sc)
13101310
{
13111311
static const __u8 sixaxis_leds[10][4] = {
13121312
{ 0x01, 0x00, 0x00, 0x00 },
@@ -1321,16 +1321,18 @@ static void sixaxis_set_leds_from_id(int id, __u8 values[MAX_LEDS])
13211321
{ 0x01, 0x01, 0x01, 0x01 }
13221322
};
13231323

1324-
BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
1324+
int id = sc->device_id;
1325+
1326+
BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(sixaxis_leds[0]));
13251327

13261328
if (id < 0)
13271329
return;
13281330

13291331
id %= 10;
1330-
memcpy(values, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
1332+
memcpy(sc->led_state, sixaxis_leds[id], sizeof(sixaxis_leds[id]));
13311333
}
13321334

1333-
static void dualshock4_set_leds_from_id(int id, __u8 values[MAX_LEDS])
1335+
static void dualshock4_set_leds_from_id(struct sony_sc *sc)
13341336
{
13351337
/* The first 4 color/index entries match what the PS4 assigns */
13361338
static const __u8 color_code[7][3] = {
@@ -1343,46 +1345,44 @@ static void dualshock4_set_leds_from_id(int id, __u8 values[MAX_LEDS])
13431345
/* White */ { 0x01, 0x01, 0x01 }
13441346
};
13451347

1346-
BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
1348+
int id = sc->device_id;
1349+
1350+
BUILD_BUG_ON(MAX_LEDS < ARRAY_SIZE(color_code[0]));
13471351

13481352
if (id < 0)
13491353
return;
13501354

13511355
id %= 7;
1352-
memcpy(values, color_code[id], sizeof(color_code[id]));
1356+
memcpy(sc->led_state, color_code[id], sizeof(color_code[id]));
13531357
}
13541358

1355-
static void buzz_set_leds(struct hid_device *hdev, const __u8 *leds)
1359+
static void buzz_set_leds(struct sony_sc *sc)
13561360
{
1361+
struct hid_device *hdev = sc->hdev;
13571362
struct list_head *report_list =
13581363
&hdev->report_enum[HID_OUTPUT_REPORT].report_list;
13591364
struct hid_report *report = list_entry(report_list->next,
13601365
struct hid_report, list);
13611366
__s32 *value = report->field[0]->value;
13621367

1368+
BUILD_BUG_ON(MAX_LEDS < 4);
1369+
13631370
value[0] = 0x00;
1364-
value[1] = leds[0] ? 0xff : 0x00;
1365-
value[2] = leds[1] ? 0xff : 0x00;
1366-
value[3] = leds[2] ? 0xff : 0x00;
1367-
value[4] = leds[3] ? 0xff : 0x00;
1371+
value[1] = sc->led_state[0] ? 0xff : 0x00;
1372+
value[2] = sc->led_state[1] ? 0xff : 0x00;
1373+
value[3] = sc->led_state[2] ? 0xff : 0x00;
1374+
value[4] = sc->led_state[3] ? 0xff : 0x00;
13681375
value[5] = 0x00;
13691376
value[6] = 0x00;
13701377
hid_hw_request(hdev, report, HID_REQ_SET_REPORT);
13711378
}
13721379

1373-
static void sony_set_leds(struct sony_sc *sc, const __u8 *leds, int count)
1380+
static void sony_set_leds(struct sony_sc *sc)
13741381
{
1375-
int n;
1376-
1377-
BUG_ON(count > MAX_LEDS);
1378-
1379-
if (sc->quirks & BUZZ_CONTROLLER && count == 4) {
1380-
buzz_set_leds(sc->hdev, leds);
1381-
} else {
1382-
for (n = 0; n < count; n++)
1383-
sc->led_state[n] = leds[n];
1382+
if (!(sc->quirks & BUZZ_CONTROLLER))
13841383
schedule_work(&sc->state_worker);
1385-
}
1384+
else
1385+
buzz_set_leds(sc);
13861386
}
13871387

13881388
static void sony_led_set_brightness(struct led_classdev *led,
@@ -1422,8 +1422,7 @@ static void sony_led_set_brightness(struct led_classdev *led,
14221422
drv_data->led_delay_on[n] = 0;
14231423
drv_data->led_delay_off[n] = 0;
14241424

1425-
sony_set_leds(drv_data, drv_data->led_state,
1426-
drv_data->led_count);
1425+
sony_set_leds(drv_data);
14271426
break;
14281427
}
14291428
}
@@ -1529,7 +1528,6 @@ static int sony_leds_init(struct sony_sc *sc)
15291528
const char *name_fmt;
15301529
static const char * const ds4_name_str[] = { "red", "green", "blue",
15311530
"global" };
1532-
__u8 initial_values[MAX_LEDS] = { 0 };
15331531
__u8 max_brightness[MAX_LEDS] = { [0 ... (MAX_LEDS - 1)] = 1 };
15341532
__u8 use_hw_blink[MAX_LEDS] = { 0 };
15351533

@@ -1544,8 +1542,8 @@ static int sony_leds_init(struct sony_sc *sc)
15441542
if (!hid_validate_values(hdev, HID_OUTPUT_REPORT, 0, 0, 7))
15451543
return -ENODEV;
15461544
} else if (sc->quirks & DUALSHOCK4_CONTROLLER) {
1547-
dualshock4_set_leds_from_id(sc->device_id, initial_values);
1548-
initial_values[3] = 1;
1545+
dualshock4_set_leds_from_id(sc);
1546+
sc->led_state[3] = 1;
15491547
sc->led_count = 4;
15501548
memset(max_brightness, 255, 3);
15511549
use_hw_blink[3] = 1;
@@ -1559,7 +1557,7 @@ static int sony_leds_init(struct sony_sc *sc)
15591557
name_len = 0;
15601558
name_fmt = "%s:%s";
15611559
} else {
1562-
sixaxis_set_leds_from_id(sc->device_id, initial_values);
1560+
sixaxis_set_leds_from_id(sc);
15631561
sc->led_count = 4;
15641562
memset(use_hw_blink, 1, 4);
15651563
use_ds4_names = 0;
@@ -1572,7 +1570,7 @@ static int sony_leds_init(struct sony_sc *sc)
15721570
* only relevant if the driver is loaded after somebody actively set the
15731571
* LEDs to on
15741572
*/
1575-
sony_set_leds(sc, initial_values, sc->led_count);
1573+
sony_set_leds(sc);
15761574

15771575
name_sz = strlen(dev_name(&hdev->dev)) + name_len + 1;
15781576

@@ -1595,7 +1593,7 @@ static int sony_leds_init(struct sony_sc *sc)
15951593
else
15961594
snprintf(name, name_sz, name_fmt, dev_name(&hdev->dev), n + 1);
15971595
led->name = name;
1598-
led->brightness = initial_values[n];
1596+
led->brightness = sc->led_state[n];
15991597
led->max_brightness = max_brightness[n];
16001598
led->brightness_get = sony_led_get_brightness;
16011599
led->brightness_set = sony_led_set_brightness;

0 commit comments

Comments
 (0)