hid-sensor-hub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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->application == 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 ? report->field[i]->physical :
  448. report->field[i]->application,
  449. report->field[i]->usage[0].collection_index,
  450. &hsdev, &priv);
  451. if (!callback) {
  452. ptr += sz;
  453. continue;
  454. }
  455. if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
  456. report->field[i]->usage->hid ||
  457. hsdev->pending.attr_usage_id ==
  458. report->field[i]->logical)) {
  459. hid_dbg(hdev, "data was pending ...\n");
  460. hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
  461. if (hsdev->pending.raw_data)
  462. hsdev->pending.raw_size = sz;
  463. else
  464. hsdev->pending.raw_size = 0;
  465. complete(&hsdev->pending.ready);
  466. }
  467. if (callback->capture_sample) {
  468. if (report->field[i]->logical)
  469. callback->capture_sample(hsdev,
  470. report->field[i]->logical, sz, ptr,
  471. callback->pdev);
  472. else
  473. callback->capture_sample(hsdev,
  474. report->field[i]->usage->hid, sz, ptr,
  475. callback->pdev);
  476. }
  477. ptr += sz;
  478. }
  479. if (callback && collection && callback->send_event)
  480. callback->send_event(hsdev, collection->usage,
  481. callback->pdev);
  482. spin_unlock_irqrestore(&pdata->lock, flags);
  483. return 1;
  484. }
  485. int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
  486. {
  487. int ret = 0;
  488. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  489. mutex_lock(&data->mutex);
  490. if (!data->ref_cnt) {
  491. ret = hid_hw_open(hsdev->hdev);
  492. if (ret) {
  493. hid_err(hsdev->hdev, "failed to open hid device\n");
  494. mutex_unlock(&data->mutex);
  495. return ret;
  496. }
  497. }
  498. data->ref_cnt++;
  499. mutex_unlock(&data->mutex);
  500. return ret;
  501. }
  502. EXPORT_SYMBOL_GPL(sensor_hub_device_open);
  503. void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
  504. {
  505. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  506. mutex_lock(&data->mutex);
  507. data->ref_cnt--;
  508. if (!data->ref_cnt)
  509. hid_hw_close(hsdev->hdev);
  510. mutex_unlock(&data->mutex);
  511. }
  512. EXPORT_SYMBOL_GPL(sensor_hub_device_close);
  513. static const __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  514. unsigned int *rsize)
  515. {
  516. /*
  517. * Checks if the report descriptor of Thinkpad Helix 2 has a logical
  518. * minimum for magnetic flux axis greater than the maximum.
  519. */
  520. if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
  521. *rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
  522. rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
  523. rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
  524. rdesc[921] == 0x07 && rdesc[922] == 0x00) {
  525. /* Sets negative logical minimum for mag x, y and z */
  526. rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
  527. rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
  528. rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
  529. rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
  530. }
  531. return rdesc;
  532. }
  533. static int sensor_hub_probe(struct hid_device *hdev,
  534. const struct hid_device_id *id)
  535. {
  536. int ret;
  537. struct sensor_hub_data *sd;
  538. int i;
  539. char *name;
  540. int dev_cnt;
  541. struct hid_sensor_hub_device *hsdev;
  542. struct hid_sensor_hub_device *last_hsdev = NULL;
  543. struct hid_sensor_hub_device *collection_hsdev = NULL;
  544. sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
  545. if (!sd) {
  546. hid_err(hdev, "cannot allocate Sensor data\n");
  547. return -ENOMEM;
  548. }
  549. hid_set_drvdata(hdev, sd);
  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, HID_CONNECT_DEFAULT | HID_CONNECT_DRIVER);
  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 int sensor_hub_finalize_pending_fn(struct device *dev, void *data)
  649. {
  650. struct hid_sensor_hub_device *hsdev = dev->platform_data;
  651. if (hsdev->pending.status)
  652. complete(&hsdev->pending.ready);
  653. return 0;
  654. }
  655. static void sensor_hub_remove(struct hid_device *hdev)
  656. {
  657. struct sensor_hub_data *data = hid_get_drvdata(hdev);
  658. unsigned long flags;
  659. hid_dbg(hdev, " hardware removed\n");
  660. hid_hw_close(hdev);
  661. hid_hw_stop(hdev);
  662. spin_lock_irqsave(&data->lock, flags);
  663. device_for_each_child(&hdev->dev, NULL,
  664. sensor_hub_finalize_pending_fn);
  665. spin_unlock_irqrestore(&data->lock, flags);
  666. mfd_remove_devices(&hdev->dev);
  667. mutex_destroy(&data->mutex);
  668. }
  669. static const struct hid_device_id sensor_hub_devices[] = {
  670. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
  671. HID_ANY_ID) },
  672. { }
  673. };
  674. MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
  675. static struct hid_driver sensor_hub_driver = {
  676. .name = "hid-sensor-hub",
  677. .id_table = sensor_hub_devices,
  678. .probe = sensor_hub_probe,
  679. .remove = sensor_hub_remove,
  680. .raw_event = sensor_hub_raw_event,
  681. .report_fixup = sensor_hub_report_fixup,
  682. #ifdef CONFIG_PM
  683. .suspend = sensor_hub_suspend,
  684. .resume = sensor_hub_resume,
  685. .reset_resume = sensor_hub_reset_resume,
  686. #endif
  687. };
  688. module_hid_driver(sensor_hub_driver);
  689. MODULE_DESCRIPTION("HID Sensor Hub driver");
  690. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  691. MODULE_LICENSE("GPL");