ishtp-hid-client.c 26 KB

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