ishtp-hid-client.c 26 KB

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