hid-sensor-hub.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2012, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/hid.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/mfd/core.h>
  24. #include <linux/list.h>
  25. #include <linux/hid-sensor-ids.h>
  26. #include <linux/hid-sensor-hub.h>
  27. #include "hid-ids.h"
  28. #define HID_SENSOR_HUB_ENUM_QUIRK 0x01
  29. /**
  30. * struct sensor_hub_data - Hold a instance data for a HID hub device
  31. * @hsdev: Stored hid instance for current hub device.
  32. * @mutex: Mutex to serialize synchronous request.
  33. * @lock: Spin lock to protect pending request structure.
  34. * @dyn_callback_list: Holds callback function
  35. * @dyn_callback_lock: spin lock to protect callback list
  36. * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
  37. * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
  38. * @ref_cnt: Number of MFD clients have opened this device
  39. */
  40. struct sensor_hub_data {
  41. struct mutex mutex;
  42. spinlock_t lock;
  43. struct list_head dyn_callback_list;
  44. spinlock_t dyn_callback_lock;
  45. struct mfd_cell *hid_sensor_hub_client_devs;
  46. int hid_sensor_client_cnt;
  47. unsigned long quirks;
  48. int ref_cnt;
  49. };
  50. /**
  51. * struct hid_sensor_hub_callbacks_list - Stores callback list
  52. * @list: list head.
  53. * @usage_id: usage id for a physical device.
  54. * @usage_callback: Stores registered callback functions.
  55. * @priv: Private data for a physical device.
  56. */
  57. struct hid_sensor_hub_callbacks_list {
  58. struct list_head list;
  59. u32 usage_id;
  60. struct hid_sensor_hub_device *hsdev;
  61. struct hid_sensor_hub_callbacks *usage_callback;
  62. void *priv;
  63. };
  64. static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
  65. int dir)
  66. {
  67. struct hid_report *report;
  68. list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
  69. if (report->id == id)
  70. return report;
  71. }
  72. hid_warn(hdev, "No report with id 0x%x found\n", id);
  73. return NULL;
  74. }
  75. static int sensor_hub_get_physical_device_count(struct hid_device *hdev)
  76. {
  77. int i;
  78. int count = 0;
  79. for (i = 0; i < hdev->maxcollection; ++i) {
  80. struct hid_collection *collection = &hdev->collection[i];
  81. if (collection->type == HID_COLLECTION_PHYSICAL ||
  82. collection->type == HID_COLLECTION_APPLICATION)
  83. ++count;
  84. }
  85. return count;
  86. }
  87. static void sensor_hub_fill_attr_info(
  88. struct hid_sensor_hub_attribute_info *info,
  89. s32 index, s32 report_id, struct hid_field *field)
  90. {
  91. info->index = index;
  92. info->report_id = report_id;
  93. info->units = field->unit;
  94. info->unit_expo = field->unit_exponent;
  95. info->size = (field->report_size * field->report_count)/8;
  96. info->logical_minimum = field->logical_minimum;
  97. info->logical_maximum = field->logical_maximum;
  98. }
  99. static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
  100. struct hid_device *hdev,
  101. u32 usage_id,
  102. int collection_index,
  103. struct hid_sensor_hub_device **hsdev,
  104. void **priv)
  105. {
  106. struct hid_sensor_hub_callbacks_list *callback;
  107. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  108. unsigned long flags;
  109. spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
  110. list_for_each_entry(callback, &pdata->dyn_callback_list, list)
  111. if ((callback->usage_id == usage_id ||
  112. callback->usage_id == HID_USAGE_SENSOR_COLLECTION) &&
  113. (collection_index >=
  114. callback->hsdev->start_collection_index) &&
  115. (collection_index <
  116. callback->hsdev->end_collection_index)) {
  117. *priv = callback->priv;
  118. *hsdev = callback->hsdev;
  119. spin_unlock_irqrestore(&pdata->dyn_callback_lock,
  120. flags);
  121. return callback->usage_callback;
  122. }
  123. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  124. return NULL;
  125. }
  126. int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
  127. u32 usage_id,
  128. struct hid_sensor_hub_callbacks *usage_callback)
  129. {
  130. struct hid_sensor_hub_callbacks_list *callback;
  131. struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
  132. unsigned long flags;
  133. spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
  134. list_for_each_entry(callback, &pdata->dyn_callback_list, list)
  135. if (callback->usage_id == usage_id &&
  136. callback->hsdev == hsdev) {
  137. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  138. return -EINVAL;
  139. }
  140. callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
  141. if (!callback) {
  142. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  143. return -ENOMEM;
  144. }
  145. callback->hsdev = hsdev;
  146. callback->usage_callback = usage_callback;
  147. callback->usage_id = usage_id;
  148. callback->priv = NULL;
  149. /*
  150. * If there is a handler registered for the collection type, then
  151. * it will handle all reports for sensors in this collection. If
  152. * there is also an individual sensor handler registration, then
  153. * we want to make sure that the reports are directed to collection
  154. * handler, as this may be a fusion sensor. So add collection handlers
  155. * to the beginning of the list, so that they are matched first.
  156. */
  157. if (usage_id == HID_USAGE_SENSOR_COLLECTION)
  158. list_add(&callback->list, &pdata->dyn_callback_list);
  159. else
  160. list_add_tail(&callback->list, &pdata->dyn_callback_list);
  161. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  162. return 0;
  163. }
  164. EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
  165. int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
  166. u32 usage_id)
  167. {
  168. struct hid_sensor_hub_callbacks_list *callback;
  169. struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
  170. unsigned long flags;
  171. spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
  172. list_for_each_entry(callback, &pdata->dyn_callback_list, list)
  173. if (callback->usage_id == usage_id &&
  174. callback->hsdev == hsdev) {
  175. list_del(&callback->list);
  176. kfree(callback);
  177. break;
  178. }
  179. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  180. return 0;
  181. }
  182. EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
  183. int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  184. u32 field_index, int buffer_size, void *buffer)
  185. {
  186. struct hid_report *report;
  187. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  188. __s32 *buf32 = buffer;
  189. int i = 0;
  190. int remaining_bytes;
  191. __s32 value;
  192. int ret = 0;
  193. mutex_lock(&data->mutex);
  194. report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
  195. if (!report || (field_index >= report->maxfield)) {
  196. ret = -EINVAL;
  197. goto done_proc;
  198. }
  199. remaining_bytes = do_div(buffer_size, sizeof(__s32));
  200. if (buffer_size) {
  201. for (i = 0; i < buffer_size; ++i) {
  202. hid_set_field(report->field[field_index], i,
  203. (__force __s32)cpu_to_le32(*buf32));
  204. ++buf32;
  205. }
  206. }
  207. if (remaining_bytes) {
  208. value = 0;
  209. memcpy(&value, (u8 *)buf32, remaining_bytes);
  210. hid_set_field(report->field[field_index], i,
  211. (__force __s32)cpu_to_le32(value));
  212. }
  213. hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
  214. hid_hw_wait(hsdev->hdev);
  215. done_proc:
  216. mutex_unlock(&data->mutex);
  217. return ret;
  218. }
  219. EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
  220. int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  221. u32 field_index, int buffer_size, void *buffer)
  222. {
  223. struct hid_report *report;
  224. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  225. int report_size;
  226. int ret = 0;
  227. mutex_lock(&data->mutex);
  228. report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
  229. if (!report || (field_index >= report->maxfield) ||
  230. report->field[field_index]->report_count < 1) {
  231. ret = -EINVAL;
  232. goto done_proc;
  233. }
  234. hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
  235. hid_hw_wait(hsdev->hdev);
  236. /* calculate number of bytes required to read this field */
  237. report_size = DIV_ROUND_UP(report->field[field_index]->report_size,
  238. 8) *
  239. report->field[field_index]->report_count;
  240. if (!report_size) {
  241. ret = -EINVAL;
  242. goto done_proc;
  243. }
  244. ret = min(report_size, buffer_size);
  245. memcpy(buffer, report->field[field_index]->value, ret);
  246. done_proc:
  247. mutex_unlock(&data->mutex);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
  251. int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
  252. u32 usage_id,
  253. u32 attr_usage_id, u32 report_id,
  254. enum sensor_hub_read_flags flag)
  255. {
  256. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  257. unsigned long flags;
  258. struct hid_report *report;
  259. int ret_val = 0;
  260. report = sensor_hub_report(report_id, hsdev->hdev,
  261. HID_INPUT_REPORT);
  262. if (!report)
  263. return -EINVAL;
  264. mutex_lock(hsdev->mutex_ptr);
  265. if (flag == SENSOR_HUB_SYNC) {
  266. memset(&hsdev->pending, 0, sizeof(hsdev->pending));
  267. init_completion(&hsdev->pending.ready);
  268. hsdev->pending.usage_id = usage_id;
  269. hsdev->pending.attr_usage_id = attr_usage_id;
  270. hsdev->pending.raw_size = 0;
  271. spin_lock_irqsave(&data->lock, flags);
  272. hsdev->pending.status = true;
  273. spin_unlock_irqrestore(&data->lock, flags);
  274. }
  275. mutex_lock(&data->mutex);
  276. hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
  277. mutex_unlock(&data->mutex);
  278. if (flag == SENSOR_HUB_SYNC) {
  279. wait_for_completion_interruptible_timeout(
  280. &hsdev->pending.ready, HZ*5);
  281. switch (hsdev->pending.raw_size) {
  282. case 1:
  283. ret_val = *(u8 *)hsdev->pending.raw_data;
  284. break;
  285. case 2:
  286. ret_val = *(u16 *)hsdev->pending.raw_data;
  287. break;
  288. case 4:
  289. ret_val = *(u32 *)hsdev->pending.raw_data;
  290. break;
  291. default:
  292. ret_val = 0;
  293. }
  294. kfree(hsdev->pending.raw_data);
  295. hsdev->pending.status = false;
  296. }
  297. mutex_unlock(hsdev->mutex_ptr);
  298. return ret_val;
  299. }
  300. EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
  301. int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
  302. u32 report_id, int field_index, u32 usage_id)
  303. {
  304. struct hid_report *report;
  305. struct hid_field *field;
  306. int i;
  307. report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
  308. if (!report || (field_index >= report->maxfield))
  309. goto done_proc;
  310. field = report->field[field_index];
  311. for (i = 0; i < field->maxusage; ++i) {
  312. if (field->usage[i].hid == usage_id)
  313. return field->usage[i].usage_index;
  314. }
  315. done_proc:
  316. return -EINVAL;
  317. }
  318. EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
  319. int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
  320. u8 type,
  321. u32 usage_id,
  322. u32 attr_usage_id,
  323. struct hid_sensor_hub_attribute_info *info)
  324. {
  325. int ret = -1;
  326. int i;
  327. struct hid_report *report;
  328. struct hid_field *field;
  329. struct hid_report_enum *report_enum;
  330. struct hid_device *hdev = hsdev->hdev;
  331. /* Initialize with defaults */
  332. info->usage_id = usage_id;
  333. info->attrib_id = attr_usage_id;
  334. info->report_id = -1;
  335. info->index = -1;
  336. info->units = -1;
  337. info->unit_expo = -1;
  338. report_enum = &hdev->report_enum[type];
  339. list_for_each_entry(report, &report_enum->report_list, list) {
  340. for (i = 0; i < report->maxfield; ++i) {
  341. field = report->field[i];
  342. if (field->maxusage) {
  343. if (field->physical == usage_id &&
  344. (field->logical == attr_usage_id ||
  345. field->usage[0].hid ==
  346. attr_usage_id) &&
  347. (field->usage[0].collection_index >=
  348. hsdev->start_collection_index) &&
  349. (field->usage[0].collection_index <
  350. hsdev->end_collection_index)) {
  351. sensor_hub_fill_attr_info(info, i,
  352. report->id,
  353. field);
  354. ret = 0;
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. return ret;
  361. }
  362. EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
  363. #ifdef CONFIG_PM
  364. static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
  365. {
  366. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  367. struct hid_sensor_hub_callbacks_list *callback;
  368. unsigned long flags;
  369. hid_dbg(hdev, " sensor_hub_suspend\n");
  370. spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
  371. list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
  372. if (callback->usage_callback->suspend)
  373. callback->usage_callback->suspend(
  374. callback->hsdev, callback->priv);
  375. }
  376. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  377. return 0;
  378. }
  379. static int sensor_hub_resume(struct hid_device *hdev)
  380. {
  381. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  382. struct hid_sensor_hub_callbacks_list *callback;
  383. unsigned long flags;
  384. hid_dbg(hdev, " sensor_hub_resume\n");
  385. spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
  386. list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
  387. if (callback->usage_callback->resume)
  388. callback->usage_callback->resume(
  389. callback->hsdev, callback->priv);
  390. }
  391. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  392. return 0;
  393. }
  394. static int sensor_hub_reset_resume(struct hid_device *hdev)
  395. {
  396. return 0;
  397. }
  398. #endif
  399. /*
  400. * Handle raw report as sent by device
  401. */
  402. static int sensor_hub_raw_event(struct hid_device *hdev,
  403. struct hid_report *report, u8 *raw_data, int size)
  404. {
  405. int i;
  406. u8 *ptr;
  407. int sz;
  408. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  409. unsigned long flags;
  410. struct hid_sensor_hub_callbacks *callback = NULL;
  411. struct hid_collection *collection = NULL;
  412. void *priv = NULL;
  413. struct hid_sensor_hub_device *hsdev = NULL;
  414. hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
  415. report->id, size, report->type);
  416. hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
  417. if (report->type != HID_INPUT_REPORT)
  418. return 1;
  419. ptr = raw_data;
  420. ptr++; /* Skip report id */
  421. spin_lock_irqsave(&pdata->lock, flags);
  422. for (i = 0; i < report->maxfield; ++i) {
  423. hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
  424. i, report->field[i]->usage->collection_index,
  425. report->field[i]->usage->hid,
  426. (report->field[i]->report_size *
  427. report->field[i]->report_count)/8);
  428. sz = (report->field[i]->report_size *
  429. report->field[i]->report_count)/8;
  430. collection = &hdev->collection[
  431. report->field[i]->usage->collection_index];
  432. hid_dbg(hdev, "collection->usage %x\n",
  433. collection->usage);
  434. callback = sensor_hub_get_callback(hdev,
  435. report->field[i]->physical,
  436. report->field[i]->usage[0].collection_index,
  437. &hsdev, &priv);
  438. if (!callback) {
  439. ptr += sz;
  440. continue;
  441. }
  442. if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
  443. report->field[i]->usage->hid ||
  444. hsdev->pending.attr_usage_id ==
  445. report->field[i]->logical)) {
  446. hid_dbg(hdev, "data was pending ...\n");
  447. hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
  448. if (hsdev->pending.raw_data)
  449. hsdev->pending.raw_size = sz;
  450. else
  451. hsdev->pending.raw_size = 0;
  452. complete(&hsdev->pending.ready);
  453. }
  454. if (callback->capture_sample) {
  455. if (report->field[i]->logical)
  456. callback->capture_sample(hsdev,
  457. report->field[i]->logical, sz, ptr,
  458. callback->pdev);
  459. else
  460. callback->capture_sample(hsdev,
  461. report->field[i]->usage->hid, sz, ptr,
  462. callback->pdev);
  463. }
  464. ptr += sz;
  465. }
  466. if (callback && collection && callback->send_event)
  467. callback->send_event(hsdev, collection->usage,
  468. callback->pdev);
  469. spin_unlock_irqrestore(&pdata->lock, flags);
  470. return 1;
  471. }
  472. int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
  473. {
  474. int ret = 0;
  475. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  476. mutex_lock(&data->mutex);
  477. if (!data->ref_cnt) {
  478. ret = hid_hw_open(hsdev->hdev);
  479. if (ret) {
  480. hid_err(hsdev->hdev, "failed to open hid device\n");
  481. mutex_unlock(&data->mutex);
  482. return ret;
  483. }
  484. }
  485. data->ref_cnt++;
  486. mutex_unlock(&data->mutex);
  487. return ret;
  488. }
  489. EXPORT_SYMBOL_GPL(sensor_hub_device_open);
  490. void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
  491. {
  492. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  493. mutex_lock(&data->mutex);
  494. data->ref_cnt--;
  495. if (!data->ref_cnt)
  496. hid_hw_close(hsdev->hdev);
  497. mutex_unlock(&data->mutex);
  498. }
  499. EXPORT_SYMBOL_GPL(sensor_hub_device_close);
  500. static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  501. unsigned int *rsize)
  502. {
  503. int index;
  504. struct sensor_hub_data *sd = hid_get_drvdata(hdev);
  505. unsigned char report_block[] = {
  506. 0x0a, 0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
  507. unsigned char power_block[] = {
  508. 0x0a, 0x19, 0x03, 0x15, 0x00, 0x25, 0x05};
  509. if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
  510. hid_dbg(hdev, "No Enum quirks\n");
  511. return rdesc;
  512. }
  513. /* Looks for power and report state usage id and force to 1 */
  514. for (index = 0; index < *rsize; ++index) {
  515. if (((*rsize - index) > sizeof(report_block)) &&
  516. !memcmp(&rdesc[index], report_block,
  517. sizeof(report_block))) {
  518. rdesc[index + 4] = 0x01;
  519. index += sizeof(report_block);
  520. }
  521. if (((*rsize - index) > sizeof(power_block)) &&
  522. !memcmp(&rdesc[index], power_block,
  523. sizeof(power_block))) {
  524. rdesc[index + 4] = 0x01;
  525. index += sizeof(power_block);
  526. }
  527. }
  528. return rdesc;
  529. }
  530. static int sensor_hub_probe(struct hid_device *hdev,
  531. const struct hid_device_id *id)
  532. {
  533. int ret;
  534. struct sensor_hub_data *sd;
  535. int i;
  536. char *name;
  537. int dev_cnt;
  538. struct hid_sensor_hub_device *hsdev;
  539. struct hid_sensor_hub_device *last_hsdev = NULL;
  540. struct hid_sensor_hub_device *collection_hsdev = NULL;
  541. sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
  542. if (!sd) {
  543. hid_err(hdev, "cannot allocate Sensor data\n");
  544. return -ENOMEM;
  545. }
  546. hid_set_drvdata(hdev, sd);
  547. sd->quirks = id->driver_data;
  548. spin_lock_init(&sd->lock);
  549. spin_lock_init(&sd->dyn_callback_lock);
  550. mutex_init(&sd->mutex);
  551. ret = hid_parse(hdev);
  552. if (ret) {
  553. hid_err(hdev, "parse failed\n");
  554. return ret;
  555. }
  556. INIT_LIST_HEAD(&hdev->inputs);
  557. ret = hid_hw_start(hdev, 0);
  558. if (ret) {
  559. hid_err(hdev, "hw start failed\n");
  560. return ret;
  561. }
  562. INIT_LIST_HEAD(&sd->dyn_callback_list);
  563. sd->hid_sensor_client_cnt = 0;
  564. dev_cnt = sensor_hub_get_physical_device_count(hdev);
  565. if (dev_cnt > HID_MAX_PHY_DEVICES) {
  566. hid_err(hdev, "Invalid Physical device count\n");
  567. ret = -EINVAL;
  568. goto err_stop_hw;
  569. }
  570. sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt *
  571. sizeof(struct mfd_cell),
  572. GFP_KERNEL);
  573. if (sd->hid_sensor_hub_client_devs == NULL) {
  574. hid_err(hdev, "Failed to allocate memory for mfd cells\n");
  575. ret = -ENOMEM;
  576. goto err_stop_hw;
  577. }
  578. for (i = 0; i < hdev->maxcollection; ++i) {
  579. struct hid_collection *collection = &hdev->collection[i];
  580. if (collection->type == HID_COLLECTION_PHYSICAL ||
  581. collection->type == HID_COLLECTION_APPLICATION) {
  582. hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
  583. GFP_KERNEL);
  584. if (!hsdev) {
  585. hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
  586. ret = -ENOMEM;
  587. goto err_stop_hw;
  588. }
  589. hsdev->hdev = hdev;
  590. hsdev->vendor_id = hdev->vendor;
  591. hsdev->product_id = hdev->product;
  592. hsdev->usage = collection->usage;
  593. hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
  594. sizeof(struct mutex),
  595. GFP_KERNEL);
  596. if (!hsdev->mutex_ptr) {
  597. ret = -ENOMEM;
  598. goto err_stop_hw;
  599. }
  600. mutex_init(hsdev->mutex_ptr);
  601. hsdev->start_collection_index = i;
  602. if (last_hsdev)
  603. last_hsdev->end_collection_index = i;
  604. last_hsdev = hsdev;
  605. name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  606. "HID-SENSOR-%x",
  607. collection->usage);
  608. if (name == NULL) {
  609. hid_err(hdev, "Failed MFD device name\n");
  610. ret = -ENOMEM;
  611. goto err_stop_hw;
  612. }
  613. sd->hid_sensor_hub_client_devs[
  614. sd->hid_sensor_client_cnt].name = name;
  615. sd->hid_sensor_hub_client_devs[
  616. sd->hid_sensor_client_cnt].platform_data =
  617. hsdev;
  618. sd->hid_sensor_hub_client_devs[
  619. sd->hid_sensor_client_cnt].pdata_size =
  620. sizeof(*hsdev);
  621. hid_dbg(hdev, "Adding %s:%d\n", name,
  622. hsdev->start_collection_index);
  623. sd->hid_sensor_client_cnt++;
  624. if (collection_hsdev)
  625. collection_hsdev->end_collection_index = i;
  626. if (collection->type == HID_COLLECTION_APPLICATION &&
  627. collection->usage == HID_USAGE_SENSOR_COLLECTION)
  628. collection_hsdev = hsdev;
  629. }
  630. }
  631. if (last_hsdev)
  632. last_hsdev->end_collection_index = i;
  633. if (collection_hsdev)
  634. collection_hsdev->end_collection_index = i;
  635. ret = mfd_add_hotplug_devices(&hdev->dev,
  636. sd->hid_sensor_hub_client_devs,
  637. sd->hid_sensor_client_cnt);
  638. if (ret < 0)
  639. goto err_stop_hw;
  640. return ret;
  641. err_stop_hw:
  642. hid_hw_stop(hdev);
  643. return ret;
  644. }
  645. static void sensor_hub_remove(struct hid_device *hdev)
  646. {
  647. struct sensor_hub_data *data = hid_get_drvdata(hdev);
  648. unsigned long flags;
  649. int i;
  650. hid_dbg(hdev, " hardware removed\n");
  651. hid_hw_close(hdev);
  652. hid_hw_stop(hdev);
  653. spin_lock_irqsave(&data->lock, flags);
  654. for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
  655. struct hid_sensor_hub_device *hsdev =
  656. data->hid_sensor_hub_client_devs[i].platform_data;
  657. if (hsdev->pending.status)
  658. complete(&hsdev->pending.ready);
  659. }
  660. spin_unlock_irqrestore(&data->lock, flags);
  661. mfd_remove_devices(&hdev->dev);
  662. hid_set_drvdata(hdev, NULL);
  663. mutex_destroy(&data->mutex);
  664. }
  665. static const struct hid_device_id sensor_hub_devices[] = {
  666. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
  667. USB_DEVICE_ID_INTEL_HID_SENSOR_0),
  668. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  669. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
  670. USB_DEVICE_ID_INTEL_HID_SENSOR_0),
  671. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  672. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
  673. USB_DEVICE_ID_INTEL_HID_SENSOR_1),
  674. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  675. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
  676. USB_DEVICE_ID_MS_SURFACE_PRO_2),
  677. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  678. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
  679. USB_DEVICE_ID_MS_TOUCH_COVER_2),
  680. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  681. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
  682. USB_DEVICE_ID_MS_TYPE_COVER_2),
  683. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  684. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
  685. USB_DEVICE_ID_STM_HID_SENSOR),
  686. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  687. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
  688. USB_DEVICE_ID_STM_HID_SENSOR_1),
  689. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  690. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS,
  691. USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA),
  692. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  693. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
  694. HID_ANY_ID) },
  695. { }
  696. };
  697. MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
  698. static struct hid_driver sensor_hub_driver = {
  699. .name = "hid-sensor-hub",
  700. .id_table = sensor_hub_devices,
  701. .probe = sensor_hub_probe,
  702. .remove = sensor_hub_remove,
  703. .raw_event = sensor_hub_raw_event,
  704. .report_fixup = sensor_hub_report_fixup,
  705. #ifdef CONFIG_PM
  706. .suspend = sensor_hub_suspend,
  707. .resume = sensor_hub_resume,
  708. .reset_resume = sensor_hub_reset_resume,
  709. #endif
  710. };
  711. module_hid_driver(sensor_hub_driver);
  712. MODULE_DESCRIPTION("HID Sensor Hub driver");
  713. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  714. MODULE_LICENSE("GPL");