Skip to content

Commit b26fc31

Browse files
dtorJiri Kosina
authored andcommitted
HID: i2c-hid: refactor reset command
"Reset" is the only command that needs to wait for interrupt from the device before continuing, so let's factor our waiting logic from __i2c_hid_command() to make it simpler. 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 d34c610 commit b26fc31

1 file changed

Lines changed: 38 additions & 25 deletions

File tree

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

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ struct i2c_hid_cmd {
8888
unsigned int registerIndex;
8989
__u8 opcode;
9090
unsigned int length;
91-
bool wait;
9291
};
9392

9493
union command {
@@ -114,8 +113,7 @@ static const struct i2c_hid_cmd hid_report_descr_cmd = {
114113
.opcode = 0x00,
115114
.length = 2 };
116115
/* commands */
117-
static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01),
118-
.wait = true };
116+
static const struct i2c_hid_cmd hid_reset_cmd = { I2C_HID_CMD(0x01) };
119117
static const struct i2c_hid_cmd hid_get_report_cmd = { I2C_HID_CMD(0x02) };
120118
static const struct i2c_hid_cmd hid_set_report_cmd = { I2C_HID_CMD(0x03) };
121119
static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) };
@@ -220,7 +218,6 @@ static int __i2c_hid_command(struct i2c_hid *ihid,
220218
int msg_num = 1;
221219

222220
int length = command->length;
223-
bool wait = command->wait;
224221
unsigned int registerIndex = command->registerIndex;
225222

226223
/* special case for hid_descr_cmd */
@@ -261,9 +258,6 @@ static int __i2c_hid_command(struct i2c_hid *ihid,
261258
set_bit(I2C_HID_READ_PENDING, &ihid->flags);
262259
}
263260

264-
if (wait)
265-
set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
266-
267261
ret = i2c_transfer(client->adapter, msg, msg_num);
268262

269263
if (data_len > 0)
@@ -272,20 +266,7 @@ static int __i2c_hid_command(struct i2c_hid *ihid,
272266
if (ret != msg_num)
273267
return ret < 0 ? ret : -EIO;
274268

275-
ret = 0;
276-
277-
if (wait && (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET)) {
278-
msleep(100);
279-
} else if (wait) {
280-
i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
281-
if (!wait_event_timeout(ihid->wait,
282-
!test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
283-
msecs_to_jiffies(5000)))
284-
ret = -ENODATA;
285-
i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
286-
}
287-
288-
return ret;
269+
return 0;
289270
}
290271

291272
static int i2c_hid_command(struct i2c_hid *ihid,
@@ -432,6 +413,39 @@ static int i2c_hid_set_power(struct i2c_hid *ihid, int power_state)
432413
return ret;
433414
}
434415

416+
static int i2c_hid_execute_reset(struct i2c_hid *ihid)
417+
{
418+
int ret;
419+
420+
i2c_hid_dbg(ihid, "resetting...\n");
421+
422+
set_bit(I2C_HID_RESET_PENDING, &ihid->flags);
423+
424+
ret = i2c_hid_command(ihid, &hid_reset_cmd, NULL, 0);
425+
if (ret) {
426+
dev_err(&ihid->client->dev, "failed to reset device.\n");
427+
goto out;
428+
}
429+
430+
if (ihid->quirks & I2C_HID_QUIRK_NO_IRQ_AFTER_RESET) {
431+
msleep(100);
432+
goto out;
433+
}
434+
435+
i2c_hid_dbg(ihid, "%s: waiting...\n", __func__);
436+
if (!wait_event_timeout(ihid->wait,
437+
!test_bit(I2C_HID_RESET_PENDING, &ihid->flags),
438+
msecs_to_jiffies(5000))) {
439+
ret = -ENODATA;
440+
goto out;
441+
}
442+
i2c_hid_dbg(ihid, "%s: finished.\n", __func__);
443+
444+
out:
445+
clear_bit(I2C_HID_RESET_PENDING, &ihid->flags);
446+
return ret;
447+
}
448+
435449
static int i2c_hid_hwreset(struct i2c_hid *ihid)
436450
{
437451
int ret;
@@ -449,11 +463,10 @@ static int i2c_hid_hwreset(struct i2c_hid *ihid)
449463
if (ret)
450464
goto out_unlock;
451465

452-
i2c_hid_dbg(ihid, "resetting...\n");
453-
454-
ret = i2c_hid_command(ihid, &hid_reset_cmd, NULL, 0);
466+
ret = i2c_hid_execute_reset(ihid);
455467
if (ret) {
456-
dev_err(&ihid->client->dev, "failed to reset device.\n");
468+
dev_err(&ihid->client->dev,
469+
"failed to reset device: %d\n", ret);
457470
i2c_hid_set_power(ihid, I2C_HID_PWR_SLEEP);
458471
goto out_unlock;
459472
}

0 commit comments

Comments
 (0)