hid-sensor-rotation.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * HID Sensors Driver
  3. * Copyright (c) 2014, 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. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/module.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <linux/slab.h>
  20. #include <linux/hid-sensor-hub.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/buffer.h>
  24. #include <linux/iio/trigger_consumer.h>
  25. #include <linux/iio/triggered_buffer.h>
  26. #include "../common/hid-sensors/hid-sensor-trigger.h"
  27. struct dev_rot_state {
  28. struct hid_sensor_hub_callbacks callbacks;
  29. struct hid_sensor_common common_attributes;
  30. struct hid_sensor_hub_attribute_info quaternion;
  31. u32 sampled_vals[4];
  32. };
  33. /* Channel definitions */
  34. static const struct iio_chan_spec dev_rot_channels[] = {
  35. {
  36. .type = IIO_ROT,
  37. .modified = 1,
  38. .channel2 = IIO_MOD_QUATERNION,
  39. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  40. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
  41. BIT(IIO_CHAN_INFO_HYSTERESIS)
  42. }
  43. };
  44. /* Adjust channel real bits based on report descriptor */
  45. static void dev_rot_adjust_channel_bit_mask(struct iio_chan_spec *chan,
  46. int size)
  47. {
  48. chan->scan_type.sign = 's';
  49. /* Real storage bits will change based on the report desc. */
  50. chan->scan_type.realbits = size * 8;
  51. /* Maximum size of a sample to capture is u32 */
  52. chan->scan_type.storagebits = sizeof(u32) * 8;
  53. chan->scan_type.repeat = 4;
  54. }
  55. /* Channel read_raw handler */
  56. static int dev_rot_read_raw(struct iio_dev *indio_dev,
  57. struct iio_chan_spec const *chan,
  58. int size, int *vals, int *val_len,
  59. long mask)
  60. {
  61. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  62. int ret_type;
  63. int i;
  64. vals[0] = 0;
  65. vals[1] = 0;
  66. switch (mask) {
  67. case IIO_CHAN_INFO_RAW:
  68. if (size >= 4) {
  69. for (i = 0; i < 4; ++i)
  70. vals[i] = rot_state->sampled_vals[i];
  71. ret_type = IIO_VAL_INT_MULTIPLE;
  72. *val_len = 4;
  73. } else
  74. ret_type = -EINVAL;
  75. break;
  76. case IIO_CHAN_INFO_SAMP_FREQ:
  77. ret_type = hid_sensor_read_samp_freq_value(
  78. &rot_state->common_attributes, &vals[0], &vals[1]);
  79. break;
  80. case IIO_CHAN_INFO_HYSTERESIS:
  81. ret_type = hid_sensor_read_raw_hyst_value(
  82. &rot_state->common_attributes, &vals[0], &vals[1]);
  83. break;
  84. default:
  85. ret_type = -EINVAL;
  86. break;
  87. }
  88. return ret_type;
  89. }
  90. /* Channel write_raw handler */
  91. static int dev_rot_write_raw(struct iio_dev *indio_dev,
  92. struct iio_chan_spec const *chan,
  93. int val,
  94. int val2,
  95. long mask)
  96. {
  97. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  98. int ret;
  99. switch (mask) {
  100. case IIO_CHAN_INFO_SAMP_FREQ:
  101. ret = hid_sensor_write_samp_freq_value(
  102. &rot_state->common_attributes, val, val2);
  103. break;
  104. case IIO_CHAN_INFO_HYSTERESIS:
  105. ret = hid_sensor_write_raw_hyst_value(
  106. &rot_state->common_attributes, val, val2);
  107. break;
  108. default:
  109. ret = -EINVAL;
  110. }
  111. return ret;
  112. }
  113. static const struct iio_info dev_rot_info = {
  114. .driver_module = THIS_MODULE,
  115. .read_raw_multi = &dev_rot_read_raw,
  116. .write_raw = &dev_rot_write_raw,
  117. };
  118. /* Function to push data to buffer */
  119. static void hid_sensor_push_data(struct iio_dev *indio_dev, u8 *data, int len)
  120. {
  121. dev_dbg(&indio_dev->dev, "hid_sensor_push_data >>\n");
  122. iio_push_to_buffers(indio_dev, (u8 *)data);
  123. dev_dbg(&indio_dev->dev, "hid_sensor_push_data <<\n");
  124. }
  125. /* Callback handler to send event after all samples are received and captured */
  126. static int dev_rot_proc_event(struct hid_sensor_hub_device *hsdev,
  127. unsigned usage_id,
  128. void *priv)
  129. {
  130. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  131. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  132. dev_dbg(&indio_dev->dev, "dev_rot_proc_event\n");
  133. if (atomic_read(&rot_state->common_attributes.data_ready))
  134. hid_sensor_push_data(indio_dev,
  135. (u8 *)rot_state->sampled_vals,
  136. sizeof(rot_state->sampled_vals));
  137. return 0;
  138. }
  139. /* Capture samples in local storage */
  140. static int dev_rot_capture_sample(struct hid_sensor_hub_device *hsdev,
  141. unsigned usage_id,
  142. size_t raw_len, char *raw_data,
  143. void *priv)
  144. {
  145. struct iio_dev *indio_dev = platform_get_drvdata(priv);
  146. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  147. if (usage_id == HID_USAGE_SENSOR_ORIENT_QUATERNION) {
  148. memcpy(rot_state->sampled_vals, raw_data,
  149. sizeof(rot_state->sampled_vals));
  150. dev_dbg(&indio_dev->dev, "Recd Quat len:%zu::%zu\n", raw_len,
  151. sizeof(rot_state->sampled_vals));
  152. }
  153. return 0;
  154. }
  155. /* Parse report which is specific to an usage id*/
  156. static int dev_rot_parse_report(struct platform_device *pdev,
  157. struct hid_sensor_hub_device *hsdev,
  158. struct iio_chan_spec *channels,
  159. unsigned usage_id,
  160. struct dev_rot_state *st)
  161. {
  162. int ret;
  163. ret = sensor_hub_input_get_attribute_info(hsdev,
  164. HID_INPUT_REPORT,
  165. usage_id,
  166. HID_USAGE_SENSOR_ORIENT_QUATERNION,
  167. &st->quaternion);
  168. if (ret)
  169. return ret;
  170. dev_rot_adjust_channel_bit_mask(&channels[0],
  171. st->quaternion.size / 4);
  172. dev_dbg(&pdev->dev, "dev_rot %x:%x\n", st->quaternion.index,
  173. st->quaternion.report_id);
  174. dev_dbg(&pdev->dev, "dev_rot: attrib size %d\n",
  175. st->quaternion.size);
  176. /* Set Sensitivity field ids, when there is no individual modifier */
  177. if (st->common_attributes.sensitivity.index < 0) {
  178. sensor_hub_input_get_attribute_info(hsdev,
  179. HID_FEATURE_REPORT, usage_id,
  180. HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
  181. HID_USAGE_SENSOR_DATA_ORIENTATION,
  182. &st->common_attributes.sensitivity);
  183. dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
  184. st->common_attributes.sensitivity.index,
  185. st->common_attributes.sensitivity.report_id);
  186. }
  187. return 0;
  188. }
  189. /* Function to initialize the processing for usage id */
  190. static int hid_dev_rot_probe(struct platform_device *pdev)
  191. {
  192. int ret;
  193. static char *name = "dev_rotation";
  194. struct iio_dev *indio_dev;
  195. struct dev_rot_state *rot_state;
  196. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  197. indio_dev = devm_iio_device_alloc(&pdev->dev,
  198. sizeof(struct dev_rot_state));
  199. if (indio_dev == NULL)
  200. return -ENOMEM;
  201. platform_set_drvdata(pdev, indio_dev);
  202. rot_state = iio_priv(indio_dev);
  203. rot_state->common_attributes.hsdev = hsdev;
  204. rot_state->common_attributes.pdev = pdev;
  205. ret = hid_sensor_parse_common_attributes(hsdev,
  206. HID_USAGE_SENSOR_DEVICE_ORIENTATION,
  207. &rot_state->common_attributes);
  208. if (ret) {
  209. dev_err(&pdev->dev, "failed to setup common attributes\n");
  210. return ret;
  211. }
  212. indio_dev->channels = devm_kmemdup(&pdev->dev, dev_rot_channels,
  213. sizeof(dev_rot_channels),
  214. GFP_KERNEL);
  215. if (!indio_dev->channels) {
  216. dev_err(&pdev->dev, "failed to duplicate channels\n");
  217. return -ENOMEM;
  218. }
  219. ret = dev_rot_parse_report(pdev, hsdev,
  220. (struct iio_chan_spec *)indio_dev->channels,
  221. HID_USAGE_SENSOR_DEVICE_ORIENTATION,
  222. rot_state);
  223. if (ret) {
  224. dev_err(&pdev->dev, "failed to setup attributes\n");
  225. return ret;
  226. }
  227. indio_dev->num_channels = ARRAY_SIZE(dev_rot_channels);
  228. indio_dev->dev.parent = &pdev->dev;
  229. indio_dev->info = &dev_rot_info;
  230. indio_dev->name = name;
  231. indio_dev->modes = INDIO_DIRECT_MODE;
  232. ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
  233. NULL, NULL);
  234. if (ret) {
  235. dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
  236. return ret;
  237. }
  238. atomic_set(&rot_state->common_attributes.data_ready, 0);
  239. ret = hid_sensor_setup_trigger(indio_dev, name,
  240. &rot_state->common_attributes);
  241. if (ret) {
  242. dev_err(&pdev->dev, "trigger setup failed\n");
  243. goto error_unreg_buffer_funcs;
  244. }
  245. ret = iio_device_register(indio_dev);
  246. if (ret) {
  247. dev_err(&pdev->dev, "device register failed\n");
  248. goto error_remove_trigger;
  249. }
  250. rot_state->callbacks.send_event = dev_rot_proc_event;
  251. rot_state->callbacks.capture_sample = dev_rot_capture_sample;
  252. rot_state->callbacks.pdev = pdev;
  253. ret = sensor_hub_register_callback(hsdev,
  254. HID_USAGE_SENSOR_DEVICE_ORIENTATION,
  255. &rot_state->callbacks);
  256. if (ret) {
  257. dev_err(&pdev->dev, "callback reg failed\n");
  258. goto error_iio_unreg;
  259. }
  260. return 0;
  261. error_iio_unreg:
  262. iio_device_unregister(indio_dev);
  263. error_remove_trigger:
  264. hid_sensor_remove_trigger(&rot_state->common_attributes);
  265. error_unreg_buffer_funcs:
  266. iio_triggered_buffer_cleanup(indio_dev);
  267. return ret;
  268. }
  269. /* Function to deinitialize the processing for usage id */
  270. static int hid_dev_rot_remove(struct platform_device *pdev)
  271. {
  272. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  273. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  274. struct dev_rot_state *rot_state = iio_priv(indio_dev);
  275. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_DEVICE_ORIENTATION);
  276. iio_device_unregister(indio_dev);
  277. hid_sensor_remove_trigger(&rot_state->common_attributes);
  278. iio_triggered_buffer_cleanup(indio_dev);
  279. return 0;
  280. }
  281. static const struct platform_device_id hid_dev_rot_ids[] = {
  282. {
  283. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  284. .name = "HID-SENSOR-20008a",
  285. },
  286. { /* sentinel */ }
  287. };
  288. MODULE_DEVICE_TABLE(platform, hid_dev_rot_ids);
  289. static struct platform_driver hid_dev_rot_platform_driver = {
  290. .id_table = hid_dev_rot_ids,
  291. .driver = {
  292. .name = KBUILD_MODNAME,
  293. .pm = &hid_sensor_pm_ops,
  294. },
  295. .probe = hid_dev_rot_probe,
  296. .remove = hid_dev_rot_remove,
  297. };
  298. module_platform_driver(hid_dev_rot_platform_driver);
  299. MODULE_DESCRIPTION("HID Sensor Device Rotation");
  300. MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
  301. MODULE_LICENSE("GPL");