rtc-hid-sensor-time.c 9.0 KB

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