hid-sensor-hub.c 21 KB

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