hid-sensor-hub.c 25 KB

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