1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ISHTP client driver for HID (ISH)
4 *
5 * Copyright (c) 2014-2016, Intel Corporation.
6 */
7
8 #include <linux/module.h>
9 #include <linux/hid.h>
10 #include <linux/intel-ish-client-if.h>
11 #include <linux/sched.h>
12 #include "ishtp-hid.h"
13
14 /* ISH Transport protocol (ISHTP in short) GUID */
15 static const struct ishtp_device_id hid_ishtp_id_table[] = {
16 { .guid = GUID_INIT(0x33AECD58, 0xB679, 0x4E54,
17 0x9B, 0xD9, 0xA0, 0x4D, 0x34, 0xF0, 0xC2, 0x26), },
18 { }
19 };
20 MODULE_DEVICE_TABLE(ishtp, hid_ishtp_id_table);
21
22 /* Rx ring buffer pool size */
23 #define HID_CL_RX_RING_SIZE 32
24 #define HID_CL_TX_RING_SIZE 16
25
26 #define cl_data_to_dev(client_data) ishtp_device(client_data->cl_device)
27
28 /**
29 * report_bad_packet() - Report bad packets
30 * @hid_ishtp_cl: Client instance to get stats
31 * @recv_buf: Raw received host interface message
32 * @cur_pos: Current position index in payload
33 * @payload_len: Length of payload expected
34 *
35 * Dumps error in case bad packet is received
36 */
report_bad_packet(struct ishtp_cl * hid_ishtp_cl,void * recv_buf,size_t cur_pos,size_t payload_len)37 static void report_bad_packet(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
38 size_t cur_pos, size_t payload_len)
39 {
40 struct hostif_msg *recv_msg = recv_buf;
41 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
42
43 dev_err(cl_data_to_dev(client_data), "[hid-ish]: BAD packet %02X\n"
44 "total_bad=%u cur_pos=%u\n"
45 "[%02X %02X %02X %02X]\n"
46 "payload_len=%u\n"
47 "multi_packet_cnt=%u\n"
48 "is_response=%02X\n",
49 recv_msg->hdr.command, client_data->bad_recv_cnt,
50 (unsigned int)cur_pos,
51 ((unsigned char *)recv_msg)[0], ((unsigned char *)recv_msg)[1],
52 ((unsigned char *)recv_msg)[2], ((unsigned char *)recv_msg)[3],
53 (unsigned int)payload_len, client_data->multi_packet_cnt,
54 recv_msg->hdr.command & ~CMD_MASK);
55 }
56
57 /**
58 * process_recv() - Received and parse incoming packet
59 * @hid_ishtp_cl: Client instance to get stats
60 * @recv_buf: Raw received host interface message
61 * @data_len: length of the message
62 *
63 * Parse the incoming packet. If it is a response packet then it will update
64 * per instance flags and wake up the caller waiting to for the response.
65 */
process_recv(struct ishtp_cl * hid_ishtp_cl,void * recv_buf,size_t data_len)66 static void process_recv(struct ishtp_cl *hid_ishtp_cl, void *recv_buf,
67 size_t data_len)
68 {
69 struct hostif_msg *recv_msg;
70 unsigned char *payload;
71 struct device_info *dev_info;
72 int i, j;
73 size_t payload_len, total_len, cur_pos, raw_len, msg_len;
74 int report_type;
75 struct report_list *reports_list;
76 struct report *report;
77 size_t report_len;
78 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
79 int curr_hid_dev = client_data->cur_hid_dev;
80 struct ishtp_hid_data *hid_data = NULL;
81 struct hid_device *hid = NULL;
82
83 payload = recv_buf + sizeof(struct hostif_msg_hdr);
84 total_len = data_len;
85 cur_pos = 0;
86
87 do {
88 if (cur_pos + sizeof(struct hostif_msg) > total_len) {
89 dev_err(cl_data_to_dev(client_data),
90 "[hid-ish]: error, received %u which is less than data header %u\n",
91 (unsigned int)data_len,
92 (unsigned int)sizeof(struct hostif_msg_hdr));
93 ++client_data->bad_recv_cnt;
94 ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
95 break;
96 }
97
98 recv_msg = (struct hostif_msg *)(recv_buf + cur_pos);
99 payload_len = recv_msg->hdr.size;
100
101 /* Sanity checks */
102 if (cur_pos + payload_len + sizeof(struct hostif_msg) >
103 total_len) {
104 ++client_data->bad_recv_cnt;
105 report_bad_packet(hid_ishtp_cl, recv_msg, cur_pos,
106 payload_len);
107 ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
108 break;
109 }
110
111 hid_ishtp_trace(client_data, "%s %d\n",
112 __func__, recv_msg->hdr.command & CMD_MASK);
113
114 switch (recv_msg->hdr.command & CMD_MASK) {
115 case HOSTIF_DM_ENUM_DEVICES:
116 if ((!(recv_msg->hdr.command & ~CMD_MASK) ||
117 client_data->init_done)) {
118 ++client_data->bad_recv_cnt;
119 report_bad_packet(hid_ishtp_cl, recv_msg,
120 cur_pos,
121 payload_len);
122 ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
123 break;
124 }
125 client_data->hid_dev_count = (unsigned int)*payload;
126 if (!client_data->hid_devices)
127 client_data->hid_devices = devm_kcalloc(
128 cl_data_to_dev(client_data),
129 client_data->hid_dev_count,
130 sizeof(struct device_info),
131 GFP_KERNEL);
132 if (!client_data->hid_devices) {
133 dev_err(cl_data_to_dev(client_data),
134 "Mem alloc failed for hid device info\n");
135 wake_up_interruptible(&client_data->init_wait);
136 break;
137 }
138 for (i = 0; i < client_data->hid_dev_count; ++i) {
139 if (1 + sizeof(struct device_info) * i >=
140 payload_len) {
141 dev_err(cl_data_to_dev(client_data),
142 "[hid-ish]: [ENUM_DEVICES]: content size %zu is bigger than payload_len %zu\n",
143 1 + sizeof(struct device_info)
144 * i, payload_len);
145 }
146
147 if (1 + sizeof(struct device_info) * i >=
148 data_len)
149 break;
150
151 dev_info = (struct device_info *)(payload + 1 +
152 sizeof(struct device_info) * i);
153 if (client_data->hid_devices)
154 memcpy(client_data->hid_devices + i,
155 dev_info,
156 sizeof(struct device_info));
157 }
158
159 client_data->enum_devices_done = true;
160 wake_up_interruptible(&client_data->init_wait);
161
162 break;
163
164 case HOSTIF_GET_HID_DESCRIPTOR:
165 if ((!(recv_msg->hdr.command & ~CMD_MASK) ||
166 client_data->init_done)) {
167 ++client_data->bad_recv_cnt;
168 report_bad_packet(hid_ishtp_cl, recv_msg,
169 cur_pos,
170 payload_len);
171 ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
172 break;
173 }
174 if (!client_data->hid_descr[curr_hid_dev])
175 client_data->hid_descr[curr_hid_dev] =
176 devm_kmalloc(cl_data_to_dev(client_data),
177 payload_len, GFP_KERNEL);
178 if (client_data->hid_descr[curr_hid_dev]) {
179 memcpy(client_data->hid_descr[curr_hid_dev],
180 payload, payload_len);
181 client_data->hid_descr_size[curr_hid_dev] =
182 payload_len;
183 client_data->hid_descr_done = true;
184 }
185 wake_up_interruptible(&client_data->init_wait);
186
187 break;
188
189 case HOSTIF_GET_REPORT_DESCRIPTOR:
190 if ((!(recv_msg->hdr.command & ~CMD_MASK) ||
191 client_data->init_done)) {
192 ++client_data->bad_recv_cnt;
193 report_bad_packet(hid_ishtp_cl, recv_msg,
194 cur_pos,
195 payload_len);
196 ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
197 break;
198 }
199 if (!client_data->report_descr[curr_hid_dev])
200 client_data->report_descr[curr_hid_dev] =
201 devm_kmalloc(cl_data_to_dev(client_data),
202 payload_len, GFP_KERNEL);
203 if (client_data->report_descr[curr_hid_dev]) {
204 memcpy(client_data->report_descr[curr_hid_dev],
205 payload,
206 payload_len);
207 client_data->report_descr_size[curr_hid_dev] =
208 payload_len;
209 client_data->report_descr_done = true;
210 }
211 wake_up_interruptible(&client_data->init_wait);
212
213 break;
214
215 case HOSTIF_GET_FEATURE_REPORT:
216 report_type = HID_FEATURE_REPORT;
217 goto do_get_report;
218
219 case HOSTIF_GET_INPUT_REPORT:
220 report_type = HID_INPUT_REPORT;
221 do_get_report:
222 /* Get index of device that matches this id */
223 for (i = 0; i < client_data->num_hid_devices; ++i) {
224 if (recv_msg->hdr.device_id ==
225 client_data->hid_devices[i].dev_id) {
226 hid = client_data->hid_sensor_hubs[i];
227 if (!hid)
228 break;
229
230 hid_data = hid->driver_data;
231 if (hid_data->raw_get_req) {
232 raw_len =
233 (hid_data->raw_buf_size <
234 payload_len) ?
235 hid_data->raw_buf_size :
236 payload_len;
237
238 memcpy(hid_data->raw_buf,
239 payload, raw_len);
240 } else {
241 hid_input_report
242 (hid, report_type,
243 payload, payload_len,
244 0);
245 }
246
247 ishtp_hid_wakeup(hid);
248 break;
249 }
250 }
251 break;
252
253 case HOSTIF_SET_FEATURE_REPORT:
254 /* Get index of device that matches this id */
255 for (i = 0; i < client_data->num_hid_devices; ++i) {
256 if (recv_msg->hdr.device_id ==
257 client_data->hid_devices[i].dev_id)
258 if (client_data->hid_sensor_hubs[i]) {
259 ishtp_hid_wakeup(
260 client_data->hid_sensor_hubs[
261 i]);
262 break;
263 }
264 }
265 break;
266
267 case HOSTIF_PUBLISH_INPUT_REPORT:
268 report_type = HID_INPUT_REPORT;
269 for (i = 0; i < client_data->num_hid_devices; ++i)
270 if (recv_msg->hdr.device_id ==
271 client_data->hid_devices[i].dev_id)
272 if (client_data->hid_sensor_hubs[i])
273 hid_input_report(
274 client_data->hid_sensor_hubs[
275 i],
276 report_type, payload,
277 payload_len, 0);
278 break;
279
280 case HOSTIF_PUBLISH_INPUT_REPORT_LIST:
281 report_type = HID_INPUT_REPORT;
282 reports_list = (struct report_list *)payload;
283 report = reports_list->reports;
284
285 for (j = 0; j < reports_list->num_of_reports; j++) {
286 recv_msg = container_of(&report->msg,
287 struct hostif_msg, hdr);
288 report_len = report->size;
289 payload = recv_msg->payload;
290 payload_len = report_len -
291 sizeof(struct hostif_msg_hdr);
292
293 for (i = 0; i < client_data->num_hid_devices;
294 ++i)
295 if (recv_msg->hdr.device_id ==
296 client_data->hid_devices[i].dev_id &&
297 client_data->hid_sensor_hubs[i]) {
298 hid_input_report(
299 client_data->hid_sensor_hubs[
300 i],
301 report_type,
302 payload, payload_len,
303 0);
304 }
305
306 report += sizeof(*report) + payload_len;
307 }
308 break;
309 default:
310 ++client_data->bad_recv_cnt;
311 report_bad_packet(hid_ishtp_cl, recv_msg, cur_pos,
312 payload_len);
313 ish_hw_reset(ishtp_get_ishtp_device(hid_ishtp_cl));
314 break;
315
316 }
317
318 msg_len = payload_len + sizeof(struct hostif_msg);
319 if (!cur_pos && cur_pos + msg_len < total_len)
320 ++client_data->multi_packet_cnt;
321
322 cur_pos += msg_len;
323 payload += msg_len;
324
325 } while (cur_pos < total_len);
326 }
327
328 /**
329 * ish_cl_event_cb() - bus driver callback for incoming message/packet
330 * @device: Pointer to the ishtp client device for which this message
331 * is targeted
332 *
333 * Remove the packet from the list and process the message by calling
334 * process_recv
335 */
ish_cl_event_cb(struct ishtp_cl_device * device)336 static void ish_cl_event_cb(struct ishtp_cl_device *device)
337 {
338 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(device);
339 struct ishtp_cl_rb *rb_in_proc;
340 size_t r_length;
341
342 if (!hid_ishtp_cl)
343 return;
344
345 while ((rb_in_proc = ishtp_cl_rx_get_rb(hid_ishtp_cl)) != NULL) {
346 if (!rb_in_proc->buffer.data)
347 return;
348
349 r_length = rb_in_proc->buf_idx;
350
351 /* decide what to do with received data */
352 process_recv(hid_ishtp_cl, rb_in_proc->buffer.data, r_length);
353
354 ishtp_cl_io_rb_recycle(rb_in_proc);
355 }
356 }
357
358 /**
359 * hid_ishtp_set_feature() - send request to ISH FW to set a feature request
360 * @hid: hid device instance for this request
361 * @buf: feature buffer
362 * @len: Length of feature buffer
363 * @report_id: Report id for the feature set request
364 *
365 * This is called from hid core .request() callback. This function doesn't wait
366 * for response.
367 */
hid_ishtp_set_feature(struct hid_device * hid,char * buf,unsigned int len,int report_id)368 void hid_ishtp_set_feature(struct hid_device *hid, char *buf, unsigned int len,
369 int report_id)
370 {
371 struct ishtp_hid_data *hid_data = hid->driver_data;
372 struct ishtp_cl_data *client_data = hid_data->client_data;
373 struct hostif_msg *msg = (struct hostif_msg *)buf;
374 int rv;
375 int i;
376
377 hid_ishtp_trace(client_data, "%s hid %p\n", __func__, hid);
378
379 rv = ishtp_hid_link_ready_wait(client_data);
380 if (rv) {
381 hid_ishtp_trace(client_data, "%s hid %p link not ready\n",
382 __func__, hid);
383 return;
384 }
385
386 memset(msg, 0, sizeof(struct hostif_msg));
387 msg->hdr.command = HOSTIF_SET_FEATURE_REPORT;
388 for (i = 0; i < client_data->num_hid_devices; ++i) {
389 if (hid == client_data->hid_sensor_hubs[i]) {
390 msg->hdr.device_id =
391 client_data->hid_devices[i].dev_id;
392 break;
393 }
394 }
395
396 if (i == client_data->num_hid_devices)
397 return;
398
399 rv = ishtp_cl_send(client_data->hid_ishtp_cl, buf, len);
400 if (rv)
401 hid_ishtp_trace(client_data, "%s hid %p send failed\n",
402 __func__, hid);
403 }
404
405 /**
406 * hid_ishtp_get_report() - request to get feature/input report
407 * @hid: hid device instance for this request
408 * @report_id: Report id for the get request
409 * @report_type: Report type for the this request
410 *
411 * This is called from hid core .request() callback. This function will send
412 * request to FW and return without waiting for response.
413 */
hid_ishtp_get_report(struct hid_device * hid,int report_id,int report_type)414 void hid_ishtp_get_report(struct hid_device *hid, int report_id,
415 int report_type)
416 {
417 struct ishtp_hid_data *hid_data = hid->driver_data;
418 struct ishtp_cl_data *client_data = hid_data->client_data;
419 struct hostif_msg_to_sensor msg = {};
420 int rv;
421 int i;
422
423 hid_ishtp_trace(client_data, "%s hid %p\n", __func__, hid);
424 rv = ishtp_hid_link_ready_wait(client_data);
425 if (rv) {
426 hid_ishtp_trace(client_data, "%s hid %p link not ready\n",
427 __func__, hid);
428 return;
429 }
430
431 msg.hdr.command = (report_type == HID_FEATURE_REPORT) ?
432 HOSTIF_GET_FEATURE_REPORT : HOSTIF_GET_INPUT_REPORT;
433 for (i = 0; i < client_data->num_hid_devices; ++i) {
434 if (hid == client_data->hid_sensor_hubs[i]) {
435 msg.hdr.device_id =
436 client_data->hid_devices[i].dev_id;
437 break;
438 }
439 }
440
441 if (i == client_data->num_hid_devices)
442 return;
443
444 msg.report_id = report_id;
445 rv = ishtp_cl_send(client_data->hid_ishtp_cl, (uint8_t *)&msg,
446 sizeof(msg));
447 if (rv)
448 hid_ishtp_trace(client_data, "%s hid %p send failed\n",
449 __func__, hid);
450 }
451
452 /**
453 * ishtp_hid_link_ready_wait() - Wait for link ready
454 * @client_data: client data instance
455 *
456 * If the transport link started suspend process, then wait, till either
457 * resumed or timeout
458 *
459 * Return: 0 on success, non zero on error
460 */
ishtp_hid_link_ready_wait(struct ishtp_cl_data * client_data)461 int ishtp_hid_link_ready_wait(struct ishtp_cl_data *client_data)
462 {
463 int rc;
464
465 if (client_data->suspended) {
466 hid_ishtp_trace(client_data, "wait for link ready\n");
467 rc = wait_event_interruptible_timeout(
468 client_data->ishtp_resume_wait,
469 !client_data->suspended,
470 5 * HZ);
471
472 if (rc == 0) {
473 hid_ishtp_trace(client_data, "link not ready\n");
474 return -EIO;
475 }
476 hid_ishtp_trace(client_data, "link ready\n");
477 }
478
479 return 0;
480 }
481
482 /**
483 * ishtp_enum_enum_devices() - Enumerate hid devices
484 * @hid_ishtp_cl: client instance
485 *
486 * Helper function to send request to firmware to enumerate HID devices
487 *
488 * Return: 0 on success, non zero on error
489 */
ishtp_enum_enum_devices(struct ishtp_cl * hid_ishtp_cl)490 static int ishtp_enum_enum_devices(struct ishtp_cl *hid_ishtp_cl)
491 {
492 struct hostif_msg msg;
493 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
494 int retry_count;
495 int rv;
496
497 /* Send HOSTIF_DM_ENUM_DEVICES */
498 memset(&msg, 0, sizeof(struct hostif_msg));
499 msg.hdr.command = HOSTIF_DM_ENUM_DEVICES;
500 rv = ishtp_cl_send(hid_ishtp_cl, (unsigned char *)&msg,
501 sizeof(struct hostif_msg));
502 if (rv)
503 return rv;
504
505 retry_count = 0;
506 while (!client_data->enum_devices_done &&
507 retry_count < 10) {
508 wait_event_interruptible_timeout(client_data->init_wait,
509 client_data->enum_devices_done,
510 3 * HZ);
511 ++retry_count;
512 if (!client_data->enum_devices_done)
513 /* Send HOSTIF_DM_ENUM_DEVICES */
514 rv = ishtp_cl_send(hid_ishtp_cl,
515 (unsigned char *) &msg,
516 sizeof(struct hostif_msg));
517 }
518 if (!client_data->enum_devices_done) {
519 dev_err(cl_data_to_dev(client_data),
520 "[hid-ish]: timed out waiting for enum_devices\n");
521 return -ETIMEDOUT;
522 }
523 if (!client_data->hid_devices) {
524 dev_err(cl_data_to_dev(client_data),
525 "[hid-ish]: failed to allocate HID dev structures\n");
526 return -ENOMEM;
527 }
528
529 client_data->num_hid_devices = client_data->hid_dev_count;
530 dev_info(ishtp_device(client_data->cl_device),
531 "[hid-ish]: enum_devices_done OK, num_hid_devices=%d\n",
532 client_data->num_hid_devices);
533
534 return 0;
535 }
536
537 /**
538 * ishtp_get_hid_descriptor() - Get hid descriptor
539 * @hid_ishtp_cl: client instance
540 * @index: Index into the hid_descr array
541 *
542 * Helper function to send request to firmware get HID descriptor of a device
543 *
544 * Return: 0 on success, non zero on error
545 */
ishtp_get_hid_descriptor(struct ishtp_cl * hid_ishtp_cl,int index)546 static int ishtp_get_hid_descriptor(struct ishtp_cl *hid_ishtp_cl, int index)
547 {
548 struct hostif_msg msg;
549 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
550 int rv;
551
552 /* Get HID descriptor */
553 client_data->hid_descr_done = false;
554 memset(&msg, 0, sizeof(struct hostif_msg));
555 msg.hdr.command = HOSTIF_GET_HID_DESCRIPTOR;
556 msg.hdr.device_id = client_data->hid_devices[index].dev_id;
557 rv = ishtp_cl_send(hid_ishtp_cl, (unsigned char *) &msg,
558 sizeof(struct hostif_msg));
559 if (rv)
560 return rv;
561
562 if (!client_data->hid_descr_done) {
563 wait_event_interruptible_timeout(client_data->init_wait,
564 client_data->hid_descr_done,
565 3 * HZ);
566 if (!client_data->hid_descr_done) {
567 dev_err(cl_data_to_dev(client_data),
568 "[hid-ish]: timed out for hid_descr_done\n");
569 return -EIO;
570 }
571
572 if (!client_data->hid_descr[index]) {
573 dev_err(cl_data_to_dev(client_data),
574 "[hid-ish]: allocation HID desc fail\n");
575 return -ENOMEM;
576 }
577 }
578
579 return 0;
580 }
581
582 /**
583 * ishtp_get_report_descriptor() - Get report descriptor
584 * @hid_ishtp_cl: client instance
585 * @index: Index into the hid_descr array
586 *
587 * Helper function to send request to firmware get HID report descriptor of
588 * a device
589 *
590 * Return: 0 on success, non zero on error
591 */
ishtp_get_report_descriptor(struct ishtp_cl * hid_ishtp_cl,int index)592 static int ishtp_get_report_descriptor(struct ishtp_cl *hid_ishtp_cl,
593 int index)
594 {
595 struct hostif_msg msg;
596 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
597 int rv;
598
599 /* Get report descriptor */
600 client_data->report_descr_done = false;
601 memset(&msg, 0, sizeof(struct hostif_msg));
602 msg.hdr.command = HOSTIF_GET_REPORT_DESCRIPTOR;
603 msg.hdr.device_id = client_data->hid_devices[index].dev_id;
604 rv = ishtp_cl_send(hid_ishtp_cl, (unsigned char *) &msg,
605 sizeof(struct hostif_msg));
606 if (rv)
607 return rv;
608
609 if (!client_data->report_descr_done)
610 wait_event_interruptible_timeout(client_data->init_wait,
611 client_data->report_descr_done,
612 3 * HZ);
613 if (!client_data->report_descr_done) {
614 dev_err(cl_data_to_dev(client_data),
615 "[hid-ish]: timed out for report descr\n");
616 return -EIO;
617 }
618 if (!client_data->report_descr[index]) {
619 dev_err(cl_data_to_dev(client_data),
620 "[hid-ish]: failed to alloc report descr\n");
621 return -ENOMEM;
622 }
623
624 return 0;
625 }
626
627 /**
628 * hid_ishtp_cl_init() - Init function for ISHTP client
629 * @hid_ishtp_cl: ISHTP client instance
630 * @reset: true if called for init after reset
631 *
632 * This function complete the initializtion of the client. The summary of
633 * processing:
634 * - Send request to enumerate the hid clients
635 * Get the HID descriptor for each enumearated device
636 * Get report description of each device
637 * Register each device wik hid core by calling ishtp_hid_probe
638 *
639 * Return: 0 on success, non zero on error
640 */
hid_ishtp_cl_init(struct ishtp_cl * hid_ishtp_cl,bool reset)641 static int hid_ishtp_cl_init(struct ishtp_cl *hid_ishtp_cl, bool reset)
642 {
643 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
644 int i;
645 int rv;
646
647 dev_dbg(cl_data_to_dev(client_data), "%s\n", __func__);
648 hid_ishtp_trace(client_data, "%s reset flag: %d\n", __func__, reset);
649
650 client_data->init_done = 0;
651
652 rv = ishtp_cl_establish_connection(hid_ishtp_cl,
653 &hid_ishtp_id_table[0].guid,
654 HID_CL_TX_RING_SIZE,
655 HID_CL_RX_RING_SIZE,
656 reset);
657 if (rv) {
658 dev_err(cl_data_to_dev(client_data),
659 "client connect fail\n");
660 goto err_cl_disconnect;
661 }
662
663 hid_ishtp_trace(client_data, "%s client connected\n", __func__);
664
665 /* Register read callback */
666 ishtp_register_event_cb(client_data->cl_device, ish_cl_event_cb);
667
668 rv = ishtp_enum_enum_devices(hid_ishtp_cl);
669 if (rv)
670 goto err_cl_disconnect;
671
672 hid_ishtp_trace(client_data, "%s enumerated device count %d\n",
673 __func__, client_data->num_hid_devices);
674
675 for (i = 0; i < client_data->num_hid_devices; ++i) {
676 client_data->cur_hid_dev = i;
677
678 rv = ishtp_get_hid_descriptor(hid_ishtp_cl, i);
679 if (rv)
680 goto err_cl_disconnect;
681
682 rv = ishtp_get_report_descriptor(hid_ishtp_cl, i);
683 if (rv)
684 goto err_cl_disconnect;
685
686 if (!reset) {
687 rv = ishtp_hid_probe(i, client_data);
688 if (rv) {
689 dev_err(cl_data_to_dev(client_data),
690 "[hid-ish]: HID probe for #%u failed: %d\n",
691 i, rv);
692 goto err_cl_disconnect;
693 }
694 }
695 } /* for() on all hid devices */
696
697 client_data->init_done = 1;
698 client_data->suspended = false;
699 wake_up_interruptible(&client_data->ishtp_resume_wait);
700 hid_ishtp_trace(client_data, "%s successful init\n", __func__);
701 return 0;
702
703 err_cl_disconnect:
704 ishtp_cl_destroy_connection(hid_ishtp_cl, reset);
705 return rv;
706 }
707
708 /**
709 * hid_ishtp_cl_deinit() - Deinit function for ISHTP client
710 * @hid_ishtp_cl: ISHTP client instance
711 *
712 * Unlink and free hid client
713 */
hid_ishtp_cl_deinit(struct ishtp_cl * hid_ishtp_cl)714 static void hid_ishtp_cl_deinit(struct ishtp_cl *hid_ishtp_cl)
715 {
716 ishtp_cl_destroy_connection(hid_ishtp_cl, false);
717
718 /* disband and free all Tx and Rx client-level rings */
719 ishtp_cl_free(hid_ishtp_cl);
720 }
721
hid_ishtp_cl_reset_handler(struct work_struct * work)722 static void hid_ishtp_cl_reset_handler(struct work_struct *work)
723 {
724 struct ishtp_cl_data *client_data;
725 struct ishtp_cl *hid_ishtp_cl;
726 int retry;
727 int rv;
728
729 client_data = container_of(work, struct ishtp_cl_data, work);
730
731 hid_ishtp_cl = client_data->hid_ishtp_cl;
732
733 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
734 hid_ishtp_cl);
735 dev_dbg(ishtp_device(client_data->cl_device), "%s\n", __func__);
736
737 ishtp_cl_destroy_connection(hid_ishtp_cl, true);
738
739 client_data->num_hid_devices = 0;
740
741 for (retry = 0; retry < 3; ++retry) {
742 rv = hid_ishtp_cl_init(hid_ishtp_cl, true);
743 if (!rv)
744 break;
745 dev_err(cl_data_to_dev(client_data), "Retry reset init\n");
746 }
747 if (rv) {
748 dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
749 hid_ishtp_trace(client_data, "%s Failed hid_ishtp_cl %p\n",
750 __func__, hid_ishtp_cl);
751 }
752 }
753
hid_ishtp_cl_resume_handler(struct work_struct * work)754 static void hid_ishtp_cl_resume_handler(struct work_struct *work)
755 {
756 struct ishtp_cl_data *client_data = container_of(work, struct ishtp_cl_data, resume_work);
757 struct ishtp_cl *hid_ishtp_cl = client_data->hid_ishtp_cl;
758
759 if (ishtp_wait_resume(ishtp_get_ishtp_device(hid_ishtp_cl))) {
760 client_data->suspended = false;
761 wake_up_interruptible(&client_data->ishtp_resume_wait);
762 }
763 }
764
765 ishtp_print_log ishtp_hid_print_trace;
766
767 /**
768 * hid_ishtp_cl_probe() - ISHTP client driver probe
769 * @cl_device: ISHTP client device instance
770 *
771 * This function gets called on device create on ISHTP bus
772 *
773 * Return: 0 on success, non zero on error
774 */
hid_ishtp_cl_probe(struct ishtp_cl_device * cl_device)775 static int hid_ishtp_cl_probe(struct ishtp_cl_device *cl_device)
776 {
777 struct ishtp_cl *hid_ishtp_cl;
778 struct ishtp_cl_data *client_data;
779 int rv;
780
781 if (!cl_device)
782 return -ENODEV;
783
784 client_data = devm_kzalloc(ishtp_device(cl_device),
785 sizeof(*client_data),
786 GFP_KERNEL);
787 if (!client_data)
788 return -ENOMEM;
789
790 hid_ishtp_cl = ishtp_cl_allocate(cl_device);
791 if (!hid_ishtp_cl)
792 return -ENOMEM;
793
794 ishtp_set_drvdata(cl_device, hid_ishtp_cl);
795 ishtp_set_client_data(hid_ishtp_cl, client_data);
796 client_data->hid_ishtp_cl = hid_ishtp_cl;
797 client_data->cl_device = cl_device;
798
799 init_waitqueue_head(&client_data->init_wait);
800 init_waitqueue_head(&client_data->ishtp_resume_wait);
801
802 INIT_WORK(&client_data->work, hid_ishtp_cl_reset_handler);
803 INIT_WORK(&client_data->resume_work, hid_ishtp_cl_resume_handler);
804
805
806 ishtp_hid_print_trace = ishtp_trace_callback(cl_device);
807
808 rv = hid_ishtp_cl_init(hid_ishtp_cl, false);
809 if (rv) {
810 ishtp_cl_free(hid_ishtp_cl);
811 return rv;
812 }
813 ishtp_get_device(cl_device);
814
815 return 0;
816 }
817
818 /**
819 * hid_ishtp_cl_remove() - ISHTP client driver remove
820 * @cl_device: ISHTP client device instance
821 *
822 * This function gets called on device remove on ISHTP bus
823 *
824 * Return: 0
825 */
hid_ishtp_cl_remove(struct ishtp_cl_device * cl_device)826 static void hid_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
827 {
828 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
829 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
830
831 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
832 hid_ishtp_cl);
833
834 dev_dbg(ishtp_device(cl_device), "%s\n", __func__);
835 ishtp_put_device(cl_device);
836 ishtp_hid_remove(client_data);
837 hid_ishtp_cl_deinit(hid_ishtp_cl);
838
839 hid_ishtp_cl = NULL;
840
841 client_data->num_hid_devices = 0;
842 }
843
844 /**
845 * hid_ishtp_cl_reset() - ISHTP client driver reset
846 * @cl_device: ISHTP client device instance
847 *
848 * This function gets called on device reset on ISHTP bus
849 *
850 * Return: 0
851 */
hid_ishtp_cl_reset(struct ishtp_cl_device * cl_device)852 static int hid_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
853 {
854 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
855 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
856
857 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
858 hid_ishtp_cl);
859
860 schedule_work(&client_data->work);
861
862 return 0;
863 }
864
865 /**
866 * hid_ishtp_cl_suspend() - ISHTP client driver suspend
867 * @device: device instance
868 *
869 * This function gets called on system suspend
870 *
871 * Return: 0
872 */
hid_ishtp_cl_suspend(struct device * device)873 static int hid_ishtp_cl_suspend(struct device *device)
874 {
875 struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
876 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
877 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
878
879 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
880 hid_ishtp_cl);
881 client_data->suspended = true;
882
883 return 0;
884 }
885
886 /**
887 * hid_ishtp_cl_resume() - ISHTP client driver resume
888 * @device: device instance
889 *
890 * This function gets called on system resume
891 *
892 * Return: 0
893 */
hid_ishtp_cl_resume(struct device * device)894 static int hid_ishtp_cl_resume(struct device *device)
895 {
896 struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
897 struct ishtp_cl *hid_ishtp_cl = ishtp_get_drvdata(cl_device);
898 struct ishtp_cl_data *client_data = ishtp_get_client_data(hid_ishtp_cl);
899
900 hid_ishtp_trace(client_data, "%s hid_ishtp_cl %p\n", __func__,
901 hid_ishtp_cl);
902 schedule_work(&client_data->resume_work);
903 return 0;
904 }
905
906 static const struct dev_pm_ops hid_ishtp_pm_ops = {
907 .suspend = hid_ishtp_cl_suspend,
908 .resume = hid_ishtp_cl_resume,
909 };
910
911 static struct ishtp_cl_driver hid_ishtp_cl_driver = {
912 .name = "ish-hid",
913 .id = hid_ishtp_id_table,
914 .probe = hid_ishtp_cl_probe,
915 .remove = hid_ishtp_cl_remove,
916 .reset = hid_ishtp_cl_reset,
917 .driver.pm = &hid_ishtp_pm_ops,
918 };
919
ish_hid_init(void)920 static int __init ish_hid_init(void)
921 {
922 int rv;
923
924 /* Register ISHTP client device driver with ISHTP Bus */
925 rv = ishtp_cl_driver_register(&hid_ishtp_cl_driver, THIS_MODULE);
926
927 return rv;
928
929 }
930
ish_hid_exit(void)931 static void __exit ish_hid_exit(void)
932 {
933 ishtp_cl_driver_unregister(&hid_ishtp_cl_driver);
934 }
935
936 late_initcall(ish_hid_init);
937 module_exit(ish_hid_exit);
938
939 MODULE_DESCRIPTION("ISH ISHTP HID client driver");
940 /* Primary author */
941 MODULE_AUTHOR("Daniel Drubin <daniel.drubin@intel.com>");
942 /*
943 * Several modification for multi instance support
944 * suspend/resume and clean up
945 */
946 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
947
948 MODULE_LICENSE("GPL");
949