hid-sensor-hub.c 22 KB

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