hid-sensor-hub.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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/usb.h>
  22. #include "usbhid/usbhid.h"
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/list.h>
  27. #include <linux/hid-sensor-ids.h>
  28. #include <linux/hid-sensor-hub.h>
  29. #include "hid-ids.h"
  30. /**
  31. * struct sensor_hub_pending - Synchronous read pending information
  32. * @status: Pending status true/false.
  33. * @ready: Completion synchronization data.
  34. * @usage_id: Usage id for physical device, E.g. Gyro usage id.
  35. * @attr_usage_id: Usage Id of a field, E.g. X-AXIS for a gyro.
  36. * @raw_size: Response size for a read request.
  37. * @raw_data: Place holder for received response.
  38. */
  39. struct sensor_hub_pending {
  40. bool status;
  41. struct completion ready;
  42. u32 usage_id;
  43. u32 attr_usage_id;
  44. int raw_size;
  45. u8 *raw_data;
  46. };
  47. /**
  48. * struct sensor_hub_data - Hold a instance data for a HID hub device
  49. * @hsdev: Stored hid instance for current hub device.
  50. * @mutex: Mutex to serialize synchronous request.
  51. * @lock: Spin lock to protect pending request structure.
  52. * @pending: Holds information of pending sync read request.
  53. * @dyn_callback_list: Holds callback function
  54. * @dyn_callback_lock: spin lock to protect callback list
  55. * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
  56. * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
  57. */
  58. struct sensor_hub_data {
  59. struct hid_sensor_hub_device *hsdev;
  60. struct mutex mutex;
  61. spinlock_t lock;
  62. struct sensor_hub_pending pending;
  63. struct list_head dyn_callback_list;
  64. spinlock_t dyn_callback_lock;
  65. struct mfd_cell *hid_sensor_hub_client_devs;
  66. int hid_sensor_client_cnt;
  67. };
  68. /**
  69. * struct hid_sensor_hub_callbacks_list - Stores callback list
  70. * @list: list head.
  71. * @usage_id: usage id for a physical device.
  72. * @usage_callback: Stores registered callback functions.
  73. * @priv: Private data for a physical device.
  74. */
  75. struct hid_sensor_hub_callbacks_list {
  76. struct list_head list;
  77. u32 usage_id;
  78. struct hid_sensor_hub_callbacks *usage_callback;
  79. void *priv;
  80. };
  81. static int sensor_hub_check_for_sensor_page(struct hid_device *hdev)
  82. {
  83. int i;
  84. int ret = -EINVAL;
  85. for (i = 0; i < hdev->maxcollection; i++) {
  86. struct hid_collection *col = &hdev->collection[i];
  87. if (col->type == HID_COLLECTION_PHYSICAL &&
  88. (col->usage & HID_USAGE_PAGE) == HID_UP_SENSOR) {
  89. ret = 0;
  90. break;
  91. }
  92. }
  93. return ret;
  94. }
  95. static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
  96. int dir)
  97. {
  98. struct hid_report *report;
  99. list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
  100. if (report->id == id)
  101. return report;
  102. }
  103. hid_warn(hdev, "No report with id 0x%x found\n", id);
  104. return NULL;
  105. }
  106. static int sensor_hub_get_physical_device_count(
  107. struct hid_report_enum *report_enum)
  108. {
  109. struct hid_report *report;
  110. struct hid_field *field;
  111. int cnt = 0;
  112. list_for_each_entry(report, &report_enum->report_list, list) {
  113. field = report->field[0];
  114. if (report->maxfield && field &&
  115. field->physical)
  116. cnt++;
  117. }
  118. return cnt;
  119. }
  120. static void sensor_hub_fill_attr_info(
  121. struct hid_sensor_hub_attribute_info *info,
  122. s32 index, s32 report_id, s32 units, s32 unit_expo, s32 size)
  123. {
  124. info->index = index;
  125. info->report_id = report_id;
  126. info->units = units;
  127. info->unit_expo = unit_expo;
  128. info->size = size/8;
  129. }
  130. static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
  131. struct hid_device *hdev,
  132. u32 usage_id, void **priv)
  133. {
  134. struct hid_sensor_hub_callbacks_list *callback;
  135. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  136. spin_lock(&pdata->dyn_callback_lock);
  137. list_for_each_entry(callback, &pdata->dyn_callback_list, list)
  138. if (callback->usage_id == usage_id) {
  139. *priv = callback->priv;
  140. spin_unlock(&pdata->dyn_callback_lock);
  141. return callback->usage_callback;
  142. }
  143. spin_unlock(&pdata->dyn_callback_lock);
  144. return NULL;
  145. }
  146. int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
  147. u32 usage_id,
  148. struct hid_sensor_hub_callbacks *usage_callback)
  149. {
  150. struct hid_sensor_hub_callbacks_list *callback;
  151. struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
  152. spin_lock(&pdata->dyn_callback_lock);
  153. list_for_each_entry(callback, &pdata->dyn_callback_list, list)
  154. if (callback->usage_id == usage_id) {
  155. spin_unlock(&pdata->dyn_callback_lock);
  156. return -EINVAL;
  157. }
  158. callback = kzalloc(sizeof(*callback), GFP_KERNEL);
  159. if (!callback) {
  160. spin_unlock(&pdata->dyn_callback_lock);
  161. return -ENOMEM;
  162. }
  163. callback->usage_callback = usage_callback;
  164. callback->usage_id = usage_id;
  165. callback->priv = NULL;
  166. list_add_tail(&callback->list, &pdata->dyn_callback_list);
  167. spin_unlock(&pdata->dyn_callback_lock);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
  171. int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
  172. u32 usage_id)
  173. {
  174. struct hid_sensor_hub_callbacks_list *callback;
  175. struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
  176. spin_lock(&pdata->dyn_callback_lock);
  177. list_for_each_entry(callback, &pdata->dyn_callback_list, list)
  178. if (callback->usage_id == usage_id) {
  179. list_del(&callback->list);
  180. kfree(callback);
  181. break;
  182. }
  183. spin_unlock(&pdata->dyn_callback_lock);
  184. return 0;
  185. }
  186. EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
  187. int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  188. u32 field_index, s32 value)
  189. {
  190. struct hid_report *report;
  191. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  192. int ret = 0;
  193. if (report_id < 0)
  194. return -EINVAL;
  195. mutex_lock(&data->mutex);
  196. report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
  197. if (!report || (field_index >= report->maxfield)) {
  198. ret = -EINVAL;
  199. goto done_proc;
  200. }
  201. hid_set_field(report->field[field_index], 0, value);
  202. usbhid_submit_report(hsdev->hdev, report, USB_DIR_OUT);
  203. usbhid_wait_io(hsdev->hdev);
  204. done_proc:
  205. mutex_unlock(&data->mutex);
  206. return ret;
  207. }
  208. EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
  209. int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
  210. u32 field_index, s32 *value)
  211. {
  212. struct hid_report *report;
  213. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  214. int ret = 0;
  215. if (report_id < 0)
  216. return -EINVAL;
  217. mutex_lock(&data->mutex);
  218. report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
  219. if (!report || (field_index >= report->maxfield)) {
  220. ret = -EINVAL;
  221. goto done_proc;
  222. }
  223. usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
  224. usbhid_wait_io(hsdev->hdev);
  225. *value = report->field[field_index]->value[0];
  226. done_proc:
  227. mutex_unlock(&data->mutex);
  228. return ret;
  229. }
  230. EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
  231. int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
  232. u32 usage_id,
  233. u32 attr_usage_id, u32 report_id)
  234. {
  235. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  236. unsigned long flags;
  237. struct hid_report *report;
  238. int ret_val = 0;
  239. if (report_id < 0)
  240. return -EINVAL;
  241. mutex_lock(&data->mutex);
  242. memset(&data->pending, 0, sizeof(data->pending));
  243. init_completion(&data->pending.ready);
  244. data->pending.usage_id = usage_id;
  245. data->pending.attr_usage_id = attr_usage_id;
  246. data->pending.raw_size = 0;
  247. spin_lock_irqsave(&data->lock, flags);
  248. data->pending.status = true;
  249. report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
  250. if (!report) {
  251. spin_unlock_irqrestore(&data->lock, flags);
  252. goto err_free;
  253. }
  254. usbhid_submit_report(hsdev->hdev, report, USB_DIR_IN);
  255. spin_unlock_irqrestore(&data->lock, flags);
  256. wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
  257. switch (data->pending.raw_size) {
  258. case 1:
  259. ret_val = *(u8 *)data->pending.raw_data;
  260. break;
  261. case 2:
  262. ret_val = *(u16 *)data->pending.raw_data;
  263. break;
  264. case 4:
  265. ret_val = *(u32 *)data->pending.raw_data;
  266. break;
  267. default:
  268. ret_val = 0;
  269. }
  270. kfree(data->pending.raw_data);
  271. err_free:
  272. data->pending.status = false;
  273. mutex_unlock(&data->mutex);
  274. return ret_val;
  275. }
  276. EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
  277. int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
  278. u8 type,
  279. u32 usage_id,
  280. u32 attr_usage_id,
  281. struct hid_sensor_hub_attribute_info *info)
  282. {
  283. int ret = -1;
  284. int i, j;
  285. int collection_index = -1;
  286. struct hid_report *report;
  287. struct hid_field *field;
  288. struct hid_report_enum *report_enum;
  289. struct hid_device *hdev = hsdev->hdev;
  290. /* Initialize with defaults */
  291. info->usage_id = usage_id;
  292. info->attrib_id = attr_usage_id;
  293. info->report_id = -1;
  294. info->index = -1;
  295. info->units = -1;
  296. info->unit_expo = -1;
  297. for (i = 0; i < hdev->maxcollection; ++i) {
  298. struct hid_collection *collection = &hdev->collection[i];
  299. if (usage_id == collection->usage) {
  300. collection_index = i;
  301. break;
  302. }
  303. }
  304. if (collection_index == -1)
  305. goto err_ret;
  306. report_enum = &hdev->report_enum[type];
  307. list_for_each_entry(report, &report_enum->report_list, list) {
  308. for (i = 0; i < report->maxfield; ++i) {
  309. field = report->field[i];
  310. if (field->physical == usage_id &&
  311. field->logical == attr_usage_id) {
  312. sensor_hub_fill_attr_info(info, i, report->id,
  313. field->unit, field->unit_exponent,
  314. field->report_size);
  315. ret = 0;
  316. } else {
  317. for (j = 0; j < field->maxusage; ++j) {
  318. if (field->usage[j].hid ==
  319. attr_usage_id &&
  320. field->usage[j].collection_index ==
  321. collection_index) {
  322. sensor_hub_fill_attr_info(info,
  323. i, report->id,
  324. field->unit,
  325. field->unit_exponent,
  326. field->report_size);
  327. ret = 0;
  328. break;
  329. }
  330. }
  331. }
  332. if (ret == 0)
  333. break;
  334. }
  335. }
  336. err_ret:
  337. return ret;
  338. }
  339. EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
  340. #ifdef CONFIG_PM
  341. static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
  342. {
  343. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  344. struct hid_sensor_hub_callbacks_list *callback;
  345. hid_dbg(hdev, " sensor_hub_suspend\n");
  346. spin_lock(&pdata->dyn_callback_lock);
  347. list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
  348. if (callback->usage_callback->suspend)
  349. callback->usage_callback->suspend(
  350. pdata->hsdev, callback->priv);
  351. }
  352. spin_unlock(&pdata->dyn_callback_lock);
  353. return 0;
  354. }
  355. static int sensor_hub_resume(struct hid_device *hdev)
  356. {
  357. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  358. struct hid_sensor_hub_callbacks_list *callback;
  359. hid_dbg(hdev, " sensor_hub_resume\n");
  360. spin_lock(&pdata->dyn_callback_lock);
  361. list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
  362. if (callback->usage_callback->resume)
  363. callback->usage_callback->resume(
  364. pdata->hsdev, callback->priv);
  365. }
  366. spin_unlock(&pdata->dyn_callback_lock);
  367. return 0;
  368. }
  369. static int sensor_hub_reset_resume(struct hid_device *hdev)
  370. {
  371. return 0;
  372. }
  373. #endif
  374. /*
  375. * Handle raw report as sent by device
  376. */
  377. static int sensor_hub_raw_event(struct hid_device *hdev,
  378. struct hid_report *report, u8 *raw_data, int size)
  379. {
  380. int i;
  381. u8 *ptr;
  382. int sz;
  383. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  384. unsigned long flags;
  385. struct hid_sensor_hub_callbacks *callback = NULL;
  386. struct hid_collection *collection = NULL;
  387. void *priv = NULL;
  388. hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
  389. report->id, size, report->type);
  390. hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
  391. if (report->type != HID_INPUT_REPORT)
  392. return 1;
  393. ptr = raw_data;
  394. ptr++; /*Skip report id*/
  395. if (!report)
  396. goto err_report;
  397. spin_lock_irqsave(&pdata->lock, flags);
  398. for (i = 0; i < report->maxfield; ++i) {
  399. hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
  400. i, report->field[i]->usage->collection_index,
  401. report->field[i]->usage->hid,
  402. report->field[i]->report_size/8);
  403. sz = report->field[i]->report_size/8;
  404. if (pdata->pending.status && pdata->pending.attr_usage_id ==
  405. report->field[i]->usage->hid) {
  406. hid_dbg(hdev, "data was pending ...\n");
  407. pdata->pending.raw_data = kmalloc(sz, GFP_KERNEL);
  408. if (pdata->pending.raw_data) {
  409. memcpy(pdata->pending.raw_data, ptr, sz);
  410. pdata->pending.raw_size = sz;
  411. } else
  412. pdata->pending.raw_size = 0;
  413. complete(&pdata->pending.ready);
  414. }
  415. collection = &hdev->collection[
  416. report->field[i]->usage->collection_index];
  417. hid_dbg(hdev, "collection->usage %x\n",
  418. collection->usage);
  419. callback = sensor_hub_get_callback(pdata->hsdev->hdev,
  420. report->field[i]->physical,
  421. &priv);
  422. if (callback && callback->capture_sample) {
  423. if (report->field[i]->logical)
  424. callback->capture_sample(pdata->hsdev,
  425. report->field[i]->logical, sz, ptr,
  426. callback->pdev);
  427. else
  428. callback->capture_sample(pdata->hsdev,
  429. report->field[i]->usage->hid, sz, ptr,
  430. callback->pdev);
  431. }
  432. ptr += sz;
  433. }
  434. if (callback && collection && callback->send_event)
  435. callback->send_event(pdata->hsdev, collection->usage,
  436. callback->pdev);
  437. spin_unlock_irqrestore(&pdata->lock, flags);
  438. err_report:
  439. return 1;
  440. }
  441. static int sensor_hub_probe(struct hid_device *hdev,
  442. const struct hid_device_id *id)
  443. {
  444. int ret;
  445. struct sensor_hub_data *sd;
  446. int i;
  447. char *name;
  448. struct hid_report *report;
  449. struct hid_report_enum *report_enum;
  450. struct hid_field *field;
  451. int dev_cnt;
  452. sd = kzalloc(sizeof(struct sensor_hub_data), GFP_KERNEL);
  453. if (!sd) {
  454. hid_err(hdev, "cannot allocate Sensor data\n");
  455. return -ENOMEM;
  456. }
  457. sd->hsdev = kzalloc(sizeof(struct hid_sensor_hub_device), GFP_KERNEL);
  458. if (!sd->hsdev) {
  459. hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
  460. ret = -ENOMEM;
  461. goto err_free_hub;
  462. }
  463. hid_set_drvdata(hdev, sd);
  464. sd->hsdev->hdev = hdev;
  465. sd->hsdev->vendor_id = hdev->vendor;
  466. sd->hsdev->product_id = hdev->product;
  467. spin_lock_init(&sd->lock);
  468. spin_lock_init(&sd->dyn_callback_lock);
  469. mutex_init(&sd->mutex);
  470. ret = hid_parse(hdev);
  471. if (ret) {
  472. hid_err(hdev, "parse failed\n");
  473. goto err_free;
  474. }
  475. if (sensor_hub_check_for_sensor_page(hdev) < 0) {
  476. hid_err(hdev, "sensor page not found\n");
  477. goto err_free;
  478. }
  479. INIT_LIST_HEAD(&hdev->inputs);
  480. hdev->claimed = HID_CLAIMED_INPUT;
  481. ret = hid_hw_start(hdev, 0);
  482. if (ret) {
  483. hid_err(hdev, "hw start failed\n");
  484. goto err_free;
  485. }
  486. ret = hid_hw_open(hdev);
  487. if (ret) {
  488. hid_err(hdev, "failed to open input interrupt pipe\n");
  489. goto err_stop_hw;
  490. }
  491. INIT_LIST_HEAD(&sd->dyn_callback_list);
  492. sd->hid_sensor_client_cnt = 0;
  493. report_enum = &hdev->report_enum[HID_INPUT_REPORT];
  494. dev_cnt = sensor_hub_get_physical_device_count(report_enum);
  495. if (dev_cnt > HID_MAX_PHY_DEVICES) {
  496. hid_err(hdev, "Invalid Physical device count\n");
  497. ret = -EINVAL;
  498. goto err_close;
  499. }
  500. sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
  501. sizeof(struct mfd_cell),
  502. GFP_KERNEL);
  503. if (sd->hid_sensor_hub_client_devs == NULL) {
  504. hid_err(hdev,
  505. "Failed to allocate memory for mfd cells\n");
  506. ret = -ENOMEM;
  507. goto err_close;
  508. }
  509. list_for_each_entry(report, &report_enum->report_list, list) {
  510. hid_dbg(hdev, "Report id:%x\n", report->id);
  511. field = report->field[0];
  512. if (report->maxfield && field &&
  513. field->physical) {
  514. name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
  515. field->physical);
  516. if (name == NULL) {
  517. hid_err(hdev,
  518. "Failed MFD device name\n");
  519. ret = -ENOMEM;
  520. goto err_free_cells;
  521. }
  522. sd->hid_sensor_hub_client_devs[
  523. sd->hid_sensor_client_cnt].name = name;
  524. sd->hid_sensor_hub_client_devs[
  525. sd->hid_sensor_client_cnt].platform_data =
  526. sd->hsdev;
  527. sd->hid_sensor_hub_client_devs[
  528. sd->hid_sensor_client_cnt].pdata_size =
  529. sizeof(*sd->hsdev);
  530. hid_dbg(hdev, "Adding %s:%p\n", name, sd);
  531. sd->hid_sensor_client_cnt++;
  532. }
  533. }
  534. ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
  535. sd->hid_sensor_client_cnt, NULL, 0, NULL);
  536. if (ret < 0)
  537. goto err_free_names;
  538. return ret;
  539. err_free_names:
  540. for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
  541. kfree(sd->hid_sensor_hub_client_devs[i].name);
  542. err_free_cells:
  543. kfree(sd->hid_sensor_hub_client_devs);
  544. err_close:
  545. hid_hw_stop(hdev);
  546. hid_hw_close(hdev);
  547. err_stop_hw:
  548. hid_hw_stop(hdev);
  549. err_free:
  550. kfree(sd->hsdev);
  551. err_free_hub:
  552. kfree(sd);
  553. return ret;
  554. }
  555. static void sensor_hub_remove(struct hid_device *hdev)
  556. {
  557. struct sensor_hub_data *data = hid_get_drvdata(hdev);
  558. unsigned long flags;
  559. int i;
  560. hid_dbg(hdev, " hardware removed\n");
  561. hdev->claimed &= ~HID_CLAIMED_INPUT;
  562. hid_hw_stop(hdev);
  563. hid_hw_close(hdev);
  564. spin_lock_irqsave(&data->lock, flags);
  565. if (data->pending.status)
  566. complete(&data->pending.ready);
  567. spin_unlock_irqrestore(&data->lock, flags);
  568. mfd_remove_devices(&hdev->dev);
  569. for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
  570. kfree(data->hid_sensor_hub_client_devs[i].name);
  571. kfree(data->hid_sensor_hub_client_devs);
  572. hid_set_drvdata(hdev, NULL);
  573. mutex_destroy(&data->mutex);
  574. kfree(data->hsdev);
  575. kfree(data);
  576. }
  577. static const struct hid_device_id sensor_hub_devices[] = {
  578. { HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
  579. USB_DEVICE_ID_SENSOR_HUB_1020) },
  580. { HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
  581. USB_DEVICE_ID_SENSOR_HUB_1020) },
  582. { HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8086,
  583. USB_DEVICE_ID_SENSOR_HUB_09FA) },
  584. { HID_USB_DEVICE(USB_VENDOR_ID_INTEL_8087,
  585. USB_DEVICE_ID_SENSOR_HUB_09FA) },
  586. { HID_USB_DEVICE(USB_VENDOR_ID_STANTUM_STM,
  587. USB_DEVICE_ID_SENSOR_HUB_7014) },
  588. { }
  589. };
  590. MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
  591. static const struct hid_usage_id sensor_hub_grabbed_usages[] = {
  592. { HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
  593. { HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1 }
  594. };
  595. static struct hid_driver sensor_hub_driver = {
  596. .name = "hid-sensor-hub",
  597. .id_table = sensor_hub_devices,
  598. .probe = sensor_hub_probe,
  599. .remove = sensor_hub_remove,
  600. .raw_event = sensor_hub_raw_event,
  601. #ifdef CONFIG_PM
  602. .suspend = sensor_hub_suspend,
  603. .resume = sensor_hub_resume,
  604. .reset_resume = sensor_hub_reset_resume,
  605. #endif
  606. };
  607. static int __init sensor_hub_init(void)
  608. {
  609. return hid_register_driver(&sensor_hub_driver);
  610. }
  611. static void __exit sensor_hub_exit(void)
  612. {
  613. hid_unregister_driver(&sensor_hub_driver);
  614. }
  615. module_init(sensor_hub_init);
  616. module_exit(sensor_hub_exit);
  617. MODULE_DESCRIPTION("HID Sensor Hub driver");
  618. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  619. MODULE_LICENSE("GPL");