hid-sensor-hub.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. mutex_lock(&data->mutex);
  229. report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
  230. if (!report || (field_index >= report->maxfield) ||
  231. report->field[field_index]->report_count < 1) {
  232. ret = -EINVAL;
  233. goto done_proc;
  234. }
  235. hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
  236. hid_hw_wait(hsdev->hdev);
  237. /* calculate number of bytes required to read this field */
  238. report_size = DIV_ROUND_UP(report->field[field_index]->report_size,
  239. 8) *
  240. report->field[field_index]->report_count;
  241. if (!report_size) {
  242. ret = -EINVAL;
  243. goto done_proc;
  244. }
  245. ret = min(report_size, buffer_size);
  246. memcpy(buffer, report->field[field_index]->value, ret);
  247. done_proc:
  248. mutex_unlock(&data->mutex);
  249. return ret;
  250. }
  251. EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
  252. int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
  253. u32 usage_id,
  254. u32 attr_usage_id, u32 report_id,
  255. enum sensor_hub_read_flags flag)
  256. {
  257. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  258. unsigned long flags;
  259. struct hid_report *report;
  260. int ret_val = 0;
  261. report = sensor_hub_report(report_id, hsdev->hdev,
  262. HID_INPUT_REPORT);
  263. if (!report)
  264. return -EINVAL;
  265. mutex_lock(hsdev->mutex_ptr);
  266. if (flag == SENSOR_HUB_SYNC) {
  267. memset(&hsdev->pending, 0, sizeof(hsdev->pending));
  268. init_completion(&hsdev->pending.ready);
  269. hsdev->pending.usage_id = usage_id;
  270. hsdev->pending.attr_usage_id = attr_usage_id;
  271. hsdev->pending.raw_size = 0;
  272. spin_lock_irqsave(&data->lock, flags);
  273. hsdev->pending.status = true;
  274. spin_unlock_irqrestore(&data->lock, flags);
  275. }
  276. mutex_lock(&data->mutex);
  277. hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
  278. mutex_unlock(&data->mutex);
  279. if (flag == SENSOR_HUB_SYNC) {
  280. wait_for_completion_interruptible_timeout(
  281. &hsdev->pending.ready, HZ*5);
  282. switch (hsdev->pending.raw_size) {
  283. case 1:
  284. ret_val = *(u8 *)hsdev->pending.raw_data;
  285. break;
  286. case 2:
  287. ret_val = *(u16 *)hsdev->pending.raw_data;
  288. break;
  289. case 4:
  290. ret_val = *(u32 *)hsdev->pending.raw_data;
  291. break;
  292. default:
  293. ret_val = 0;
  294. }
  295. kfree(hsdev->pending.raw_data);
  296. hsdev->pending.status = false;
  297. }
  298. mutex_unlock(hsdev->mutex_ptr);
  299. return ret_val;
  300. }
  301. EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
  302. int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev,
  303. u32 report_id, int field_index, u32 usage_id)
  304. {
  305. struct hid_report *report;
  306. struct hid_field *field;
  307. int i;
  308. report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
  309. if (!report || (field_index >= report->maxfield))
  310. goto done_proc;
  311. field = report->field[field_index];
  312. for (i = 0; i < field->maxusage; ++i) {
  313. if (field->usage[i].hid == usage_id)
  314. return field->usage[i].usage_index;
  315. }
  316. done_proc:
  317. return -EINVAL;
  318. }
  319. EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index);
  320. int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
  321. u8 type,
  322. u32 usage_id,
  323. u32 attr_usage_id,
  324. struct hid_sensor_hub_attribute_info *info)
  325. {
  326. int ret = -1;
  327. int i;
  328. struct hid_report *report;
  329. struct hid_field *field;
  330. struct hid_report_enum *report_enum;
  331. struct hid_device *hdev = hsdev->hdev;
  332. /* Initialize with defaults */
  333. info->usage_id = usage_id;
  334. info->attrib_id = attr_usage_id;
  335. info->report_id = -1;
  336. info->index = -1;
  337. info->units = -1;
  338. info->unit_expo = -1;
  339. report_enum = &hdev->report_enum[type];
  340. list_for_each_entry(report, &report_enum->report_list, list) {
  341. for (i = 0; i < report->maxfield; ++i) {
  342. field = report->field[i];
  343. if (field->maxusage) {
  344. if (field->physical == usage_id &&
  345. (field->logical == attr_usage_id ||
  346. field->usage[0].hid ==
  347. attr_usage_id) &&
  348. (field->usage[0].collection_index >=
  349. hsdev->start_collection_index) &&
  350. (field->usage[0].collection_index <
  351. hsdev->end_collection_index)) {
  352. sensor_hub_fill_attr_info(info, i,
  353. report->id,
  354. field);
  355. ret = 0;
  356. break;
  357. }
  358. }
  359. }
  360. }
  361. return ret;
  362. }
  363. EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
  364. #ifdef CONFIG_PM
  365. static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
  366. {
  367. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  368. struct hid_sensor_hub_callbacks_list *callback;
  369. unsigned long flags;
  370. hid_dbg(hdev, " sensor_hub_suspend\n");
  371. spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
  372. list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
  373. if (callback->usage_callback->suspend)
  374. callback->usage_callback->suspend(
  375. callback->hsdev, callback->priv);
  376. }
  377. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  378. return 0;
  379. }
  380. static int sensor_hub_resume(struct hid_device *hdev)
  381. {
  382. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  383. struct hid_sensor_hub_callbacks_list *callback;
  384. unsigned long flags;
  385. hid_dbg(hdev, " sensor_hub_resume\n");
  386. spin_lock_irqsave(&pdata->dyn_callback_lock, flags);
  387. list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
  388. if (callback->usage_callback->resume)
  389. callback->usage_callback->resume(
  390. callback->hsdev, callback->priv);
  391. }
  392. spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags);
  393. return 0;
  394. }
  395. static int sensor_hub_reset_resume(struct hid_device *hdev)
  396. {
  397. return 0;
  398. }
  399. #endif
  400. /*
  401. * Handle raw report as sent by device
  402. */
  403. static int sensor_hub_raw_event(struct hid_device *hdev,
  404. struct hid_report *report, u8 *raw_data, int size)
  405. {
  406. int i;
  407. u8 *ptr;
  408. int sz;
  409. struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
  410. unsigned long flags;
  411. struct hid_sensor_hub_callbacks *callback = NULL;
  412. struct hid_collection *collection = NULL;
  413. void *priv = NULL;
  414. struct hid_sensor_hub_device *hsdev = NULL;
  415. hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
  416. report->id, size, report->type);
  417. hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
  418. if (report->type != HID_INPUT_REPORT)
  419. return 1;
  420. ptr = raw_data;
  421. ptr++; /* Skip report id */
  422. spin_lock_irqsave(&pdata->lock, flags);
  423. for (i = 0; i < report->maxfield; ++i) {
  424. hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
  425. i, report->field[i]->usage->collection_index,
  426. report->field[i]->usage->hid,
  427. (report->field[i]->report_size *
  428. report->field[i]->report_count)/8);
  429. sz = (report->field[i]->report_size *
  430. report->field[i]->report_count)/8;
  431. collection = &hdev->collection[
  432. report->field[i]->usage->collection_index];
  433. hid_dbg(hdev, "collection->usage %x\n",
  434. collection->usage);
  435. callback = sensor_hub_get_callback(hdev,
  436. report->field[i]->physical,
  437. report->field[i]->usage[0].collection_index,
  438. &hsdev, &priv);
  439. if (!callback) {
  440. ptr += sz;
  441. continue;
  442. }
  443. if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
  444. report->field[i]->usage->hid ||
  445. hsdev->pending.attr_usage_id ==
  446. report->field[i]->logical)) {
  447. hid_dbg(hdev, "data was pending ...\n");
  448. hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
  449. if (hsdev->pending.raw_data)
  450. hsdev->pending.raw_size = sz;
  451. else
  452. hsdev->pending.raw_size = 0;
  453. complete(&hsdev->pending.ready);
  454. }
  455. if (callback->capture_sample) {
  456. if (report->field[i]->logical)
  457. callback->capture_sample(hsdev,
  458. report->field[i]->logical, sz, ptr,
  459. callback->pdev);
  460. else
  461. callback->capture_sample(hsdev,
  462. report->field[i]->usage->hid, sz, ptr,
  463. callback->pdev);
  464. }
  465. ptr += sz;
  466. }
  467. if (callback && collection && callback->send_event)
  468. callback->send_event(hsdev, collection->usage,
  469. callback->pdev);
  470. spin_unlock_irqrestore(&pdata->lock, flags);
  471. return 1;
  472. }
  473. int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
  474. {
  475. int ret = 0;
  476. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  477. mutex_lock(&data->mutex);
  478. if (!data->ref_cnt) {
  479. ret = hid_hw_open(hsdev->hdev);
  480. if (ret) {
  481. hid_err(hsdev->hdev, "failed to open hid device\n");
  482. mutex_unlock(&data->mutex);
  483. return ret;
  484. }
  485. }
  486. data->ref_cnt++;
  487. mutex_unlock(&data->mutex);
  488. return ret;
  489. }
  490. EXPORT_SYMBOL_GPL(sensor_hub_device_open);
  491. void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
  492. {
  493. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  494. mutex_lock(&data->mutex);
  495. data->ref_cnt--;
  496. if (!data->ref_cnt)
  497. hid_hw_close(hsdev->hdev);
  498. mutex_unlock(&data->mutex);
  499. }
  500. EXPORT_SYMBOL_GPL(sensor_hub_device_close);
  501. static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  502. unsigned int *rsize)
  503. {
  504. int index;
  505. struct sensor_hub_data *sd = hid_get_drvdata(hdev);
  506. unsigned char report_block[] = {
  507. 0x0a, 0x16, 0x03, 0x15, 0x00, 0x25, 0x05};
  508. unsigned char power_block[] = {
  509. 0x0a, 0x19, 0x03, 0x15, 0x00, 0x25, 0x05};
  510. if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) {
  511. hid_dbg(hdev, "No Enum quirks\n");
  512. return rdesc;
  513. }
  514. /* Looks for power and report state usage id and force to 1 */
  515. for (index = 0; index < *rsize; ++index) {
  516. if (((*rsize - index) > sizeof(report_block)) &&
  517. !memcmp(&rdesc[index], report_block,
  518. sizeof(report_block))) {
  519. rdesc[index + 4] = 0x01;
  520. index += sizeof(report_block);
  521. }
  522. if (((*rsize - index) > sizeof(power_block)) &&
  523. !memcmp(&rdesc[index], power_block,
  524. sizeof(power_block))) {
  525. rdesc[index + 4] = 0x01;
  526. index += sizeof(power_block);
  527. }
  528. }
  529. /* Checks if the report descriptor of Thinkpad Helix 2 has a logical
  530. * minimum for magnetic flux axis greater than the maximum */
  531. if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
  532. *rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
  533. rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
  534. rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
  535. rdesc[921] == 0x07 && rdesc[922] == 0x00) {
  536. /* Sets negative logical minimum for mag x, y and z */
  537. rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
  538. rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
  539. rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
  540. rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
  541. }
  542. return rdesc;
  543. }
  544. static int sensor_hub_probe(struct hid_device *hdev,
  545. const struct hid_device_id *id)
  546. {
  547. int ret;
  548. struct sensor_hub_data *sd;
  549. int i;
  550. char *name;
  551. int dev_cnt;
  552. struct hid_sensor_hub_device *hsdev;
  553. struct hid_sensor_hub_device *last_hsdev = NULL;
  554. struct hid_sensor_hub_device *collection_hsdev = NULL;
  555. sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
  556. if (!sd) {
  557. hid_err(hdev, "cannot allocate Sensor data\n");
  558. return -ENOMEM;
  559. }
  560. hid_set_drvdata(hdev, sd);
  561. sd->quirks = id->driver_data;
  562. spin_lock_init(&sd->lock);
  563. spin_lock_init(&sd->dyn_callback_lock);
  564. mutex_init(&sd->mutex);
  565. ret = hid_parse(hdev);
  566. if (ret) {
  567. hid_err(hdev, "parse failed\n");
  568. return ret;
  569. }
  570. INIT_LIST_HEAD(&hdev->inputs);
  571. ret = hid_hw_start(hdev, 0);
  572. if (ret) {
  573. hid_err(hdev, "hw start failed\n");
  574. return ret;
  575. }
  576. INIT_LIST_HEAD(&sd->dyn_callback_list);
  577. sd->hid_sensor_client_cnt = 0;
  578. dev_cnt = sensor_hub_get_physical_device_count(hdev);
  579. if (dev_cnt > HID_MAX_PHY_DEVICES) {
  580. hid_err(hdev, "Invalid Physical device count\n");
  581. ret = -EINVAL;
  582. goto err_stop_hw;
  583. }
  584. sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt *
  585. sizeof(struct mfd_cell),
  586. GFP_KERNEL);
  587. if (sd->hid_sensor_hub_client_devs == NULL) {
  588. hid_err(hdev, "Failed to allocate memory for mfd cells\n");
  589. ret = -ENOMEM;
  590. goto err_stop_hw;
  591. }
  592. for (i = 0; i < hdev->maxcollection; ++i) {
  593. struct hid_collection *collection = &hdev->collection[i];
  594. if (collection->type == HID_COLLECTION_PHYSICAL ||
  595. collection->type == HID_COLLECTION_APPLICATION) {
  596. hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
  597. GFP_KERNEL);
  598. if (!hsdev) {
  599. hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
  600. ret = -ENOMEM;
  601. goto err_stop_hw;
  602. }
  603. hsdev->hdev = hdev;
  604. hsdev->vendor_id = hdev->vendor;
  605. hsdev->product_id = hdev->product;
  606. hsdev->usage = collection->usage;
  607. hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
  608. sizeof(struct mutex),
  609. GFP_KERNEL);
  610. if (!hsdev->mutex_ptr) {
  611. ret = -ENOMEM;
  612. goto err_stop_hw;
  613. }
  614. mutex_init(hsdev->mutex_ptr);
  615. hsdev->start_collection_index = i;
  616. if (last_hsdev)
  617. last_hsdev->end_collection_index = i;
  618. last_hsdev = hsdev;
  619. name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  620. "HID-SENSOR-%x",
  621. collection->usage);
  622. if (name == NULL) {
  623. hid_err(hdev, "Failed MFD device name\n");
  624. ret = -ENOMEM;
  625. goto err_stop_hw;
  626. }
  627. sd->hid_sensor_hub_client_devs[
  628. sd->hid_sensor_client_cnt].name = name;
  629. sd->hid_sensor_hub_client_devs[
  630. sd->hid_sensor_client_cnt].platform_data =
  631. hsdev;
  632. sd->hid_sensor_hub_client_devs[
  633. sd->hid_sensor_client_cnt].pdata_size =
  634. sizeof(*hsdev);
  635. hid_dbg(hdev, "Adding %s:%d\n", name,
  636. hsdev->start_collection_index);
  637. sd->hid_sensor_client_cnt++;
  638. if (collection_hsdev)
  639. collection_hsdev->end_collection_index = i;
  640. if (collection->type == HID_COLLECTION_APPLICATION &&
  641. collection->usage == HID_USAGE_SENSOR_COLLECTION)
  642. collection_hsdev = hsdev;
  643. }
  644. }
  645. if (last_hsdev)
  646. last_hsdev->end_collection_index = i;
  647. if (collection_hsdev)
  648. collection_hsdev->end_collection_index = i;
  649. ret = mfd_add_hotplug_devices(&hdev->dev,
  650. sd->hid_sensor_hub_client_devs,
  651. sd->hid_sensor_client_cnt);
  652. if (ret < 0)
  653. goto err_stop_hw;
  654. return ret;
  655. err_stop_hw:
  656. hid_hw_stop(hdev);
  657. return ret;
  658. }
  659. static void sensor_hub_remove(struct hid_device *hdev)
  660. {
  661. struct sensor_hub_data *data = hid_get_drvdata(hdev);
  662. unsigned long flags;
  663. int i;
  664. hid_dbg(hdev, " hardware removed\n");
  665. hid_hw_close(hdev);
  666. hid_hw_stop(hdev);
  667. spin_lock_irqsave(&data->lock, flags);
  668. for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
  669. struct hid_sensor_hub_device *hsdev =
  670. data->hid_sensor_hub_client_devs[i].platform_data;
  671. if (hsdev->pending.status)
  672. complete(&hsdev->pending.ready);
  673. }
  674. spin_unlock_irqrestore(&data->lock, flags);
  675. mfd_remove_devices(&hdev->dev);
  676. hid_set_drvdata(hdev, NULL);
  677. mutex_destroy(&data->mutex);
  678. }
  679. static const struct hid_device_id sensor_hub_devices[] = {
  680. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0,
  681. USB_DEVICE_ID_INTEL_HID_SENSOR_0),
  682. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  683. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
  684. USB_DEVICE_ID_INTEL_HID_SENSOR_0),
  685. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  686. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1,
  687. USB_DEVICE_ID_INTEL_HID_SENSOR_1),
  688. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  689. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
  690. USB_DEVICE_ID_MS_SURFACE_PRO_2),
  691. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  692. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
  693. USB_DEVICE_ID_MS_TOUCH_COVER_2),
  694. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  695. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT,
  696. USB_DEVICE_ID_MS_TYPE_COVER_2),
  697. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  698. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
  699. USB_DEVICE_ID_STM_HID_SENSOR),
  700. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  701. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0,
  702. USB_DEVICE_ID_STM_HID_SENSOR_1),
  703. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  704. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS,
  705. USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA),
  706. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  707. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_ITE,
  708. USB_DEVICE_ID_ITE_LENOVO_YOGA),
  709. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  710. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_ITE,
  711. USB_DEVICE_ID_ITE_LENOVO_YOGA2),
  712. .driver_data = HID_SENSOR_HUB_ENUM_QUIRK},
  713. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
  714. HID_ANY_ID) },
  715. { }
  716. };
  717. MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
  718. static struct hid_driver sensor_hub_driver = {
  719. .name = "hid-sensor-hub",
  720. .id_table = sensor_hub_devices,
  721. .probe = sensor_hub_probe,
  722. .remove = sensor_hub_remove,
  723. .raw_event = sensor_hub_raw_event,
  724. .report_fixup = sensor_hub_report_fixup,
  725. #ifdef CONFIG_PM
  726. .suspend = sensor_hub_suspend,
  727. .resume = sensor_hub_resume,
  728. .reset_resume = sensor_hub_reset_resume,
  729. #endif
  730. };
  731. module_hid_driver(sensor_hub_driver);
  732. MODULE_DESCRIPTION("HID Sensor Hub driver");
  733. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  734. MODULE_LICENSE("GPL");