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