hid-sensor-hub.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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. if (report->id)
  441. ptr++; /* Skip report id */
  442. spin_lock_irqsave(&pdata->lock, flags);
  443. for (i = 0; i < report->maxfield; ++i) {
  444. hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
  445. i, report->field[i]->usage->collection_index,
  446. report->field[i]->usage->hid,
  447. (report->field[i]->report_size *
  448. report->field[i]->report_count)/8);
  449. sz = (report->field[i]->report_size *
  450. report->field[i]->report_count)/8;
  451. collection = &hdev->collection[
  452. report->field[i]->usage->collection_index];
  453. hid_dbg(hdev, "collection->usage %x\n",
  454. collection->usage);
  455. callback = sensor_hub_get_callback(hdev,
  456. report->field[i]->physical,
  457. report->field[i]->usage[0].collection_index,
  458. &hsdev, &priv);
  459. if (!callback) {
  460. ptr += sz;
  461. continue;
  462. }
  463. if (hsdev->pending.status && (hsdev->pending.attr_usage_id ==
  464. report->field[i]->usage->hid ||
  465. hsdev->pending.attr_usage_id ==
  466. report->field[i]->logical)) {
  467. hid_dbg(hdev, "data was pending ...\n");
  468. hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC);
  469. if (hsdev->pending.raw_data)
  470. hsdev->pending.raw_size = sz;
  471. else
  472. hsdev->pending.raw_size = 0;
  473. complete(&hsdev->pending.ready);
  474. }
  475. if (callback->capture_sample) {
  476. if (report->field[i]->logical)
  477. callback->capture_sample(hsdev,
  478. report->field[i]->logical, sz, ptr,
  479. callback->pdev);
  480. else
  481. callback->capture_sample(hsdev,
  482. report->field[i]->usage->hid, sz, ptr,
  483. callback->pdev);
  484. }
  485. ptr += sz;
  486. }
  487. if (callback && collection && callback->send_event)
  488. callback->send_event(hsdev, collection->usage,
  489. callback->pdev);
  490. spin_unlock_irqrestore(&pdata->lock, flags);
  491. return 1;
  492. }
  493. int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev)
  494. {
  495. int ret = 0;
  496. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  497. mutex_lock(&data->mutex);
  498. if (!data->ref_cnt) {
  499. ret = hid_hw_open(hsdev->hdev);
  500. if (ret) {
  501. hid_err(hsdev->hdev, "failed to open hid device\n");
  502. mutex_unlock(&data->mutex);
  503. return ret;
  504. }
  505. }
  506. data->ref_cnt++;
  507. mutex_unlock(&data->mutex);
  508. return ret;
  509. }
  510. EXPORT_SYMBOL_GPL(sensor_hub_device_open);
  511. void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev)
  512. {
  513. struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev);
  514. mutex_lock(&data->mutex);
  515. data->ref_cnt--;
  516. if (!data->ref_cnt)
  517. hid_hw_close(hsdev->hdev);
  518. mutex_unlock(&data->mutex);
  519. }
  520. EXPORT_SYMBOL_GPL(sensor_hub_device_close);
  521. static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  522. unsigned int *rsize)
  523. {
  524. /*
  525. * Checks if the report descriptor of Thinkpad Helix 2 has a logical
  526. * minimum for magnetic flux axis greater than the maximum.
  527. */
  528. if (hdev->product == USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA &&
  529. *rsize == 2558 && rdesc[913] == 0x17 && rdesc[914] == 0x40 &&
  530. rdesc[915] == 0x81 && rdesc[916] == 0x08 &&
  531. rdesc[917] == 0x00 && rdesc[918] == 0x27 &&
  532. rdesc[921] == 0x07 && rdesc[922] == 0x00) {
  533. /* Sets negative logical minimum for mag x, y and z */
  534. rdesc[914] = rdesc[935] = rdesc[956] = 0xc0;
  535. rdesc[915] = rdesc[936] = rdesc[957] = 0x7e;
  536. rdesc[916] = rdesc[937] = rdesc[958] = 0xf7;
  537. rdesc[917] = rdesc[938] = rdesc[959] = 0xff;
  538. }
  539. return rdesc;
  540. }
  541. static int sensor_hub_probe(struct hid_device *hdev,
  542. const struct hid_device_id *id)
  543. {
  544. int ret;
  545. struct sensor_hub_data *sd;
  546. int i;
  547. char *name;
  548. int dev_cnt;
  549. struct hid_sensor_hub_device *hsdev;
  550. struct hid_sensor_hub_device *last_hsdev = NULL;
  551. struct hid_sensor_hub_device *collection_hsdev = NULL;
  552. sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL);
  553. if (!sd) {
  554. hid_err(hdev, "cannot allocate Sensor data\n");
  555. return -ENOMEM;
  556. }
  557. hid_set_drvdata(hdev, sd);
  558. sd->quirks = id->driver_data;
  559. spin_lock_init(&sd->lock);
  560. spin_lock_init(&sd->dyn_callback_lock);
  561. mutex_init(&sd->mutex);
  562. ret = hid_parse(hdev);
  563. if (ret) {
  564. hid_err(hdev, "parse failed\n");
  565. return ret;
  566. }
  567. INIT_LIST_HEAD(&hdev->inputs);
  568. ret = hid_hw_start(hdev, 0);
  569. if (ret) {
  570. hid_err(hdev, "hw start failed\n");
  571. return ret;
  572. }
  573. INIT_LIST_HEAD(&sd->dyn_callback_list);
  574. sd->hid_sensor_client_cnt = 0;
  575. dev_cnt = sensor_hub_get_physical_device_count(hdev);
  576. if (dev_cnt > HID_MAX_PHY_DEVICES) {
  577. hid_err(hdev, "Invalid Physical device count\n");
  578. ret = -EINVAL;
  579. goto err_stop_hw;
  580. }
  581. sd->hid_sensor_hub_client_devs = devm_kcalloc(&hdev->dev,
  582. dev_cnt,
  583. sizeof(struct mfd_cell),
  584. GFP_KERNEL);
  585. if (sd->hid_sensor_hub_client_devs == NULL) {
  586. hid_err(hdev, "Failed to allocate memory for mfd cells\n");
  587. ret = -ENOMEM;
  588. goto err_stop_hw;
  589. }
  590. for (i = 0; i < hdev->maxcollection; ++i) {
  591. struct hid_collection *collection = &hdev->collection[i];
  592. if (collection->type == HID_COLLECTION_PHYSICAL ||
  593. collection->type == HID_COLLECTION_APPLICATION) {
  594. hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev),
  595. GFP_KERNEL);
  596. if (!hsdev) {
  597. hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
  598. ret = -ENOMEM;
  599. goto err_stop_hw;
  600. }
  601. hsdev->hdev = hdev;
  602. hsdev->vendor_id = hdev->vendor;
  603. hsdev->product_id = hdev->product;
  604. hsdev->usage = collection->usage;
  605. hsdev->mutex_ptr = devm_kzalloc(&hdev->dev,
  606. sizeof(struct mutex),
  607. GFP_KERNEL);
  608. if (!hsdev->mutex_ptr) {
  609. ret = -ENOMEM;
  610. goto err_stop_hw;
  611. }
  612. mutex_init(hsdev->mutex_ptr);
  613. hsdev->start_collection_index = i;
  614. if (last_hsdev)
  615. last_hsdev->end_collection_index = i;
  616. last_hsdev = hsdev;
  617. name = devm_kasprintf(&hdev->dev, GFP_KERNEL,
  618. "HID-SENSOR-%x",
  619. collection->usage);
  620. if (name == NULL) {
  621. hid_err(hdev, "Failed MFD device name\n");
  622. ret = -ENOMEM;
  623. goto err_stop_hw;
  624. }
  625. sd->hid_sensor_hub_client_devs[
  626. sd->hid_sensor_client_cnt].name = name;
  627. sd->hid_sensor_hub_client_devs[
  628. sd->hid_sensor_client_cnt].platform_data =
  629. hsdev;
  630. sd->hid_sensor_hub_client_devs[
  631. sd->hid_sensor_client_cnt].pdata_size =
  632. sizeof(*hsdev);
  633. hid_dbg(hdev, "Adding %s:%d\n", name,
  634. hsdev->start_collection_index);
  635. sd->hid_sensor_client_cnt++;
  636. if (collection_hsdev)
  637. collection_hsdev->end_collection_index = i;
  638. if (collection->type == HID_COLLECTION_APPLICATION &&
  639. collection->usage == HID_USAGE_SENSOR_COLLECTION)
  640. collection_hsdev = hsdev;
  641. }
  642. }
  643. if (last_hsdev)
  644. last_hsdev->end_collection_index = i;
  645. if (collection_hsdev)
  646. collection_hsdev->end_collection_index = i;
  647. ret = mfd_add_hotplug_devices(&hdev->dev,
  648. sd->hid_sensor_hub_client_devs,
  649. sd->hid_sensor_client_cnt);
  650. if (ret < 0)
  651. goto err_stop_hw;
  652. return ret;
  653. err_stop_hw:
  654. hid_hw_stop(hdev);
  655. return ret;
  656. }
  657. static void sensor_hub_remove(struct hid_device *hdev)
  658. {
  659. struct sensor_hub_data *data = hid_get_drvdata(hdev);
  660. unsigned long flags;
  661. int i;
  662. hid_dbg(hdev, " hardware removed\n");
  663. hid_hw_close(hdev);
  664. hid_hw_stop(hdev);
  665. spin_lock_irqsave(&data->lock, flags);
  666. for (i = 0; i < data->hid_sensor_client_cnt; ++i) {
  667. struct hid_sensor_hub_device *hsdev =
  668. data->hid_sensor_hub_client_devs[i].platform_data;
  669. if (hsdev->pending.status)
  670. complete(&hsdev->pending.ready);
  671. }
  672. spin_unlock_irqrestore(&data->lock, flags);
  673. mfd_remove_devices(&hdev->dev);
  674. hid_set_drvdata(hdev, NULL);
  675. mutex_destroy(&data->mutex);
  676. }
  677. static const struct hid_device_id sensor_hub_devices[] = {
  678. { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
  679. HID_ANY_ID) },
  680. { }
  681. };
  682. MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
  683. static struct hid_driver sensor_hub_driver = {
  684. .name = "hid-sensor-hub",
  685. .id_table = sensor_hub_devices,
  686. .probe = sensor_hub_probe,
  687. .remove = sensor_hub_remove,
  688. .raw_event = sensor_hub_raw_event,
  689. .report_fixup = sensor_hub_report_fixup,
  690. #ifdef CONFIG_PM
  691. .suspend = sensor_hub_suspend,
  692. .resume = sensor_hub_resume,
  693. .reset_resume = sensor_hub_reset_resume,
  694. #endif
  695. };
  696. module_hid_driver(sensor_hub_driver);
  697. MODULE_DESCRIPTION("HID Sensor Hub driver");
  698. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
  699. MODULE_LICENSE("GPL");