Skip to content

Commit 86fc3fd

Browse files
dtorJiri Kosina
authored andcommitted
HID: i2c-hid: use helpers to do endian conversion in i2c_hid_get_input()
It is better to use helpers to do endian conversion as it documents and draws attention to it, and might be a bit more performant as well. Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Tested-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 85df713 commit 86fc3fd

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

drivers/hid/i2c-hid/i2c-hid-core.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ static int i2c_hid_hwreset(struct i2c_hid *ihid)
508508

509509
static void i2c_hid_get_input(struct i2c_hid *ihid)
510510
{
511+
u16 size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
512+
u16 ret_size;
511513
int ret;
512-
u32 ret_size;
513-
int size = le16_to_cpu(ihid->hdesc.wMaxInputLength);
514514

515515
if (size > ihid->bufsize)
516516
size = ihid->bufsize;
@@ -525,28 +525,29 @@ static void i2c_hid_get_input(struct i2c_hid *ihid)
525525
return;
526526
}
527527

528-
ret_size = ihid->inbuf[0] | ihid->inbuf[1] << 8;
529-
528+
/* Receiving buffer is properly aligned */
529+
ret_size = le16_to_cpup((__le16 *)ihid->inbuf);
530530
if (!ret_size) {
531531
/* host or device initiated RESET completed */
532532
if (test_and_clear_bit(I2C_HID_RESET_PENDING, &ihid->flags))
533533
wake_up(&ihid->wait);
534534
return;
535535
}
536536

537-
if (ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ && ret_size == 0xffff) {
538-
dev_warn_once(&ihid->client->dev, "%s: IRQ triggered but "
539-
"there's no data\n", __func__);
537+
if ((ihid->quirks & I2C_HID_QUIRK_BOGUS_IRQ) && ret_size == 0xffff) {
538+
dev_warn_once(&ihid->client->dev,
539+
"%s: IRQ triggered but there's no data\n",
540+
__func__);
540541
return;
541542
}
542543

543-
if ((ret_size > size) || (ret_size < 2)) {
544+
if (ret_size > size || ret_size < sizeof(__le16)) {
544545
if (ihid->quirks & I2C_HID_QUIRK_BAD_INPUT_SIZE) {
545-
ihid->inbuf[0] = size & 0xff;
546-
ihid->inbuf[1] = size >> 8;
546+
*(__le16 *)ihid->inbuf = cpu_to_le16(size);
547547
ret_size = size;
548548
} else {
549-
dev_err(&ihid->client->dev, "%s: incomplete report (%d/%d)\n",
549+
dev_err(&ihid->client->dev,
550+
"%s: incomplete report (%d/%d)\n",
550551
__func__, size, ret_size);
551552
return;
552553
}
@@ -557,8 +558,9 @@ static void i2c_hid_get_input(struct i2c_hid *ihid)
557558
if (test_bit(I2C_HID_STARTED, &ihid->flags)) {
558559
pm_wakeup_event(&ihid->client->dev, 0);
559560

560-
hid_input_report(ihid->hid, HID_INPUT_REPORT, ihid->inbuf + 2,
561-
ret_size - 2, 1);
561+
hid_input_report(ihid->hid, HID_INPUT_REPORT,
562+
ihid->inbuf + sizeof(__le16),
563+
ret_size - sizeof(__le16), 1);
562564
}
563565

564566
return;

0 commit comments

Comments
 (0)