hid-sensor-hub.c 21 KB

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