rtc-hid-sensor-time.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * HID Sensor Time Driver
  3. * Copyright (c) 2012, Alexander Holler.
  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/platform_device.h>
  21. #include <linux/module.h>
  22. #include <linux/hid-sensor-hub.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/rtc.h>
  25. enum hid_time_channel {
  26. CHANNEL_SCAN_INDEX_YEAR,
  27. CHANNEL_SCAN_INDEX_MONTH,
  28. CHANNEL_SCAN_INDEX_DAY,
  29. CHANNEL_SCAN_INDEX_HOUR,
  30. CHANNEL_SCAN_INDEX_MINUTE,
  31. CHANNEL_SCAN_INDEX_SECOND,
  32. TIME_RTC_CHANNEL_MAX,
  33. };
  34. struct hid_time_state {
  35. struct hid_sensor_hub_callbacks callbacks;
  36. struct hid_sensor_common common_attributes;
  37. struct hid_sensor_hub_attribute_info info[TIME_RTC_CHANNEL_MAX];
  38. struct rtc_time last_time;
  39. spinlock_t lock_last_time;
  40. struct completion comp_last_time;
  41. struct rtc_time time_buf;
  42. struct rtc_device *rtc;
  43. };
  44. static const u32 hid_time_addresses[TIME_RTC_CHANNEL_MAX] = {
  45. HID_USAGE_SENSOR_TIME_YEAR,
  46. HID_USAGE_SENSOR_TIME_MONTH,
  47. HID_USAGE_SENSOR_TIME_DAY,
  48. HID_USAGE_SENSOR_TIME_HOUR,
  49. HID_USAGE_SENSOR_TIME_MINUTE,
  50. HID_USAGE_SENSOR_TIME_SECOND,
  51. };
  52. /* Channel names for verbose error messages */
  53. static const char * const hid_time_channel_names[TIME_RTC_CHANNEL_MAX] = {
  54. "year", "month", "day", "hour", "minute", "second",
  55. };
  56. /* Callback handler to send event after all samples are received and captured */
  57. static int hid_time_proc_event(struct hid_sensor_hub_device *hsdev,
  58. unsigned usage_id, void *priv)
  59. {
  60. unsigned long flags;
  61. struct hid_time_state *time_state = platform_get_drvdata(priv);
  62. spin_lock_irqsave(&time_state->lock_last_time, flags);
  63. time_state->last_time = time_state->time_buf;
  64. spin_unlock_irqrestore(&time_state->lock_last_time, flags);
  65. complete(&time_state->comp_last_time);
  66. return 0;
  67. }
  68. static u32 hid_time_value(size_t raw_len, char *raw_data)
  69. {
  70. switch (raw_len) {
  71. case 1:
  72. return *(u8 *)raw_data;
  73. case 2:
  74. return *(u16 *)raw_data;
  75. case 4:
  76. return *(u32 *)raw_data;
  77. default:
  78. return (u32)(~0U); /* 0xff... or -1 to denote an error */
  79. }
  80. }
  81. static int hid_time_capture_sample(struct hid_sensor_hub_device *hsdev,
  82. unsigned usage_id, size_t raw_len,
  83. char *raw_data, void *priv)
  84. {
  85. struct hid_time_state *time_state = platform_get_drvdata(priv);
  86. struct rtc_time *time_buf = &time_state->time_buf;
  87. switch (usage_id) {
  88. case HID_USAGE_SENSOR_TIME_YEAR:
  89. /*
  90. * The draft for HID-sensors (HUTRR39) currently doesn't define
  91. * the range for the year attribute. Therefor we support
  92. * 8 bit (0-99) and 16 or 32 bits (full) as size for the year.
  93. */
  94. if (raw_len == 1) {
  95. time_buf->tm_year = *(u8 *)raw_data;
  96. if (time_buf->tm_year < 70)
  97. /* assume we are in 1970...2069 */
  98. time_buf->tm_year += 100;
  99. } else
  100. time_buf->tm_year =
  101. (int)hid_time_value(raw_len, raw_data)-1900;
  102. break;
  103. case HID_USAGE_SENSOR_TIME_MONTH:
  104. /* sensors are sending the month as 1-12, we need 0-11 */
  105. time_buf->tm_mon = (int)hid_time_value(raw_len, raw_data)-1;
  106. break;
  107. case HID_USAGE_SENSOR_TIME_DAY:
  108. time_buf->tm_mday = (int)hid_time_value(raw_len, raw_data);
  109. break;
  110. case HID_USAGE_SENSOR_TIME_HOUR:
  111. time_buf->tm_hour = (int)hid_time_value(raw_len, raw_data);
  112. break;
  113. case HID_USAGE_SENSOR_TIME_MINUTE:
  114. time_buf->tm_min = (int)hid_time_value(raw_len, raw_data);
  115. break;
  116. case HID_USAGE_SENSOR_TIME_SECOND:
  117. time_buf->tm_sec = (int)hid_time_value(raw_len, raw_data);
  118. break;
  119. default:
  120. return -EINVAL;
  121. }
  122. return 0;
  123. }
  124. /* small helper, haven't found any other way */
  125. static const char *hid_time_attrib_name(u32 attrib_id)
  126. {
  127. static const char unknown[] = "unknown";
  128. unsigned i;
  129. for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i) {
  130. if (hid_time_addresses[i] == attrib_id)
  131. return hid_time_channel_names[i];
  132. }
  133. return unknown; /* should never happen */
  134. }
  135. static int hid_time_parse_report(struct platform_device *pdev,
  136. struct hid_sensor_hub_device *hsdev,
  137. unsigned usage_id,
  138. struct hid_time_state *time_state)
  139. {
  140. int report_id, i;
  141. for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i)
  142. if (sensor_hub_input_get_attribute_info(hsdev,
  143. HID_INPUT_REPORT, usage_id,
  144. hid_time_addresses[i],
  145. &time_state->info[i]) < 0)
  146. return -EINVAL;
  147. /* Check the (needed) attributes for sanity */
  148. report_id = time_state->info[0].report_id;
  149. if (report_id < 0) {
  150. dev_err(&pdev->dev, "bad report ID!\n");
  151. return -EINVAL;
  152. }
  153. for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i) {
  154. if (time_state->info[i].report_id != report_id) {
  155. dev_err(&pdev->dev,
  156. "not all needed attributes inside the same report!\n");
  157. return -EINVAL;
  158. }
  159. if (time_state->info[i].size == 3 ||
  160. time_state->info[i].size > 4) {
  161. dev_err(&pdev->dev,
  162. "attribute '%s' not 8, 16 or 32 bits wide!\n",
  163. hid_time_attrib_name(
  164. time_state->info[i].attrib_id));
  165. return -EINVAL;
  166. }
  167. if (time_state->info[i].units !=
  168. HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED &&
  169. /* allow attribute seconds with unit seconds */
  170. !(time_state->info[i].attrib_id ==
  171. HID_USAGE_SENSOR_TIME_SECOND &&
  172. time_state->info[i].units ==
  173. HID_USAGE_SENSOR_UNITS_SECOND)) {
  174. dev_err(&pdev->dev,
  175. "attribute '%s' hasn't a unit of type 'none'!\n",
  176. hid_time_attrib_name(
  177. time_state->info[i].attrib_id));
  178. return -EINVAL;
  179. }
  180. if (time_state->info[i].unit_expo) {
  181. dev_err(&pdev->dev,
  182. "attribute '%s' hasn't a unit exponent of 1!\n",
  183. hid_time_attrib_name(
  184. time_state->info[i].attrib_id));
  185. return -EINVAL;
  186. }
  187. }
  188. return 0;
  189. }
  190. static int hid_rtc_read_time(struct device *dev, struct rtc_time *tm)
  191. {
  192. unsigned long flags;
  193. struct hid_time_state *time_state =
  194. platform_get_drvdata(to_platform_device(dev));
  195. int ret;
  196. reinit_completion(&time_state->comp_last_time);
  197. /* get a report with all values through requesting one value */
  198. sensor_hub_input_attr_get_raw_value(time_state->common_attributes.hsdev,
  199. HID_USAGE_SENSOR_TIME, hid_time_addresses[0],
  200. time_state->info[0].report_id, SENSOR_HUB_SYNC);
  201. /* wait for all values (event) */
  202. ret = wait_for_completion_killable_timeout(
  203. &time_state->comp_last_time, HZ*6);
  204. if (ret > 0) {
  205. /* no error */
  206. spin_lock_irqsave(&time_state->lock_last_time, flags);
  207. *tm = time_state->last_time;
  208. spin_unlock_irqrestore(&time_state->lock_last_time, flags);
  209. return 0;
  210. }
  211. if (!ret)
  212. return -EIO; /* timeouted */
  213. return ret; /* killed (-ERESTARTSYS) */
  214. }
  215. static const struct rtc_class_ops hid_time_rtc_ops = {
  216. .read_time = hid_rtc_read_time,
  217. };
  218. static int hid_time_probe(struct platform_device *pdev)
  219. {
  220. int ret = 0;
  221. struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
  222. struct hid_time_state *time_state = devm_kzalloc(&pdev->dev,
  223. sizeof(struct hid_time_state), GFP_KERNEL);
  224. if (time_state == NULL)
  225. return -ENOMEM;
  226. platform_set_drvdata(pdev, time_state);
  227. spin_lock_init(&time_state->lock_last_time);
  228. init_completion(&time_state->comp_last_time);
  229. time_state->common_attributes.hsdev = hsdev;
  230. time_state->common_attributes.pdev = pdev;
  231. ret = hid_sensor_parse_common_attributes(hsdev,
  232. HID_USAGE_SENSOR_TIME,
  233. &time_state->common_attributes);
  234. if (ret) {
  235. dev_err(&pdev->dev, "failed to setup common attributes!\n");
  236. return ret;
  237. }
  238. ret = hid_time_parse_report(pdev, hsdev, HID_USAGE_SENSOR_TIME,
  239. time_state);
  240. if (ret) {
  241. dev_err(&pdev->dev, "failed to setup attributes!\n");
  242. return ret;
  243. }
  244. time_state->callbacks.send_event = hid_time_proc_event;
  245. time_state->callbacks.capture_sample = hid_time_capture_sample;
  246. time_state->callbacks.pdev = pdev;
  247. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TIME,
  248. &time_state->callbacks);
  249. if (ret < 0) {
  250. dev_err(&pdev->dev, "register callback failed!\n");
  251. return ret;
  252. }
  253. ret = sensor_hub_device_open(hsdev);
  254. if (ret) {
  255. dev_err(&pdev->dev, "failed to open sensor hub device!\n");
  256. goto err_open;
  257. }
  258. /*
  259. * Enable HID input processing early in order to be able to read the
  260. * clock already in devm_rtc_device_register().
  261. */
  262. hid_device_io_start(hsdev->hdev);
  263. time_state->rtc = devm_rtc_device_register(&pdev->dev,
  264. "hid-sensor-time", &hid_time_rtc_ops,
  265. THIS_MODULE);
  266. if (IS_ERR_OR_NULL(time_state->rtc)) {
  267. hid_device_io_stop(hsdev->hdev);
  268. ret = time_state->rtc ? PTR_ERR(time_state->rtc) : -ENODEV;
  269. time_state->rtc = NULL;
  270. dev_err(&pdev->dev, "rtc device register failed!\n");
  271. goto err_rtc;
  272. }
  273. return ret;
  274. err_rtc:
  275. sensor_hub_device_close(hsdev);
  276. err_open:
  277. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TIME);
  278. return ret;
  279. }
  280. static int hid_time_remove(struct platform_device *pdev)
  281. {
  282. struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev);
  283. sensor_hub_device_close(hsdev);
  284. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TIME);
  285. return 0;
  286. }
  287. static const struct platform_device_id hid_time_ids[] = {
  288. {
  289. /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
  290. .name = "HID-SENSOR-2000a0",
  291. },
  292. { /* sentinel */ }
  293. };
  294. MODULE_DEVICE_TABLE(platform, hid_time_ids);
  295. static struct platform_driver hid_time_platform_driver = {
  296. .id_table = hid_time_ids,
  297. .driver = {
  298. .name = KBUILD_MODNAME,
  299. },
  300. .probe = hid_time_probe,
  301. .remove = hid_time_remove,
  302. };
  303. module_platform_driver(hid_time_platform_driver);
  304. MODULE_DESCRIPTION("HID Sensor Time");
  305. MODULE_AUTHOR("Alexander Holler <holler@ahsoftware.de>");
  306. MODULE_LICENSE("GPL");