Skip to content

Commit 4fa5a7f

Browse files
bentissJiri Kosina
authored andcommitted
HID: core: implement generic .request()
.request() can be emulated through .raw_request() we can implement this emulation in hid-core, and make .request not mandatory for transport layer drivers. Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Reviewed-by: David Herrmann <dh.herrmann@gmail.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 706daef commit 4fa5a7f

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

drivers/hid/hid-core.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,11 @@ void hid_output_report(struct hid_report *report, __u8 *data)
12481248
}
12491249
EXPORT_SYMBOL_GPL(hid_output_report);
12501250

1251+
static int hid_report_len(struct hid_report *report)
1252+
{
1253+
return ((report->size - 1) >> 3) + 1 + (report->id > 0) + 7;
1254+
}
1255+
12511256
/*
12521257
* Allocator for buffer that is going to be passed to hid_output_report()
12531258
*/
@@ -1258,7 +1263,7 @@ u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)
12581263
* of implement() working on 8 byte chunks
12591264
*/
12601265

1261-
int len = ((report->size - 1) >> 3) + 1 + (report->id > 0) + 7;
1266+
int len = hid_report_len(report);
12621267

12631268
return kmalloc(len, flags);
12641269
}
@@ -1314,6 +1319,44 @@ static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,
13141319
return report;
13151320
}
13161321

1322+
/*
1323+
* Implement a generic .request() callback, using .raw_request()
1324+
* DO NOT USE in hid drivers directly, but through hid_hw_request instead.
1325+
*/
1326+
void __hid_request(struct hid_device *hid, struct hid_report *report,
1327+
int reqtype)
1328+
{
1329+
char *buf;
1330+
int ret;
1331+
int len;
1332+
1333+
if (!hid->ll_driver->raw_request)
1334+
return;
1335+
1336+
buf = hid_alloc_report_buf(report, GFP_KERNEL);
1337+
if (!buf)
1338+
return;
1339+
1340+
len = hid_report_len(report);
1341+
1342+
if (reqtype == HID_REQ_SET_REPORT)
1343+
hid_output_report(report, buf);
1344+
1345+
ret = hid->ll_driver->raw_request(hid, report->id, buf, len,
1346+
report->type, reqtype);
1347+
if (ret < 0) {
1348+
dbg_hid("unable to complete request: %d\n", ret);
1349+
goto out;
1350+
}
1351+
1352+
if (reqtype == HID_REQ_GET_REPORT)
1353+
hid_input_report(hid, report->type, buf, ret, 0);
1354+
1355+
out:
1356+
kfree(buf);
1357+
}
1358+
EXPORT_SYMBOL_GPL(__hid_request);
1359+
13171360
int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, int size,
13181361
int interrupt)
13191362
{

include/linux/hid.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ struct hid_field *hidinput_get_led_field(struct hid_device *hid);
753753
unsigned int hidinput_count_leds(struct hid_device *hid);
754754
__s32 hidinput_calc_abs_res(const struct hid_field *field, __u16 code);
755755
void hid_output_report(struct hid_report *report, __u8 *data);
756+
void __hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype);
756757
u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags);
757758
struct hid_device *hid_allocate_device(void);
758759
struct hid_report *hid_register_report(struct hid_device *device, unsigned type, unsigned id);
@@ -965,7 +966,9 @@ static inline void hid_hw_request(struct hid_device *hdev,
965966
struct hid_report *report, int reqtype)
966967
{
967968
if (hdev->ll_driver->request)
968-
hdev->ll_driver->request(hdev, report, reqtype);
969+
return hdev->ll_driver->request(hdev, report, reqtype);
970+
971+
__hid_request(hdev, report, reqtype);
969972
}
970973

971974
/**

0 commit comments

Comments
 (0)