rtc-hid-sensor-time.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 int hid_time_capture_sample(struct hid_sensor_hub_device *hsdev,
  72. unsigned usage_id, size_t raw_len,
  73. char *raw_data, void *priv)
  74. {
  75. struct hid_time_state *time_state = platform_get_drvdata(priv);
  76. struct rtc_time *time_buf = &time_state->time_buf;
  77. switch (usage_id) {
  78. case HID_USAGE_SENSOR_TIME_YEAR:
  79. time_buf->tm_year = *(u8 *)raw_data;
  80. if (time_buf->tm_year < 70)
  81. /* assume we are in 1970...2069 */
  82. time_buf->tm_year += 100;
  83. break;
  84. case HID_USAGE_SENSOR_TIME_MONTH:
  85. /* sensor sending the month as 1-12, we need 0-11 */
  86. time_buf->tm_mon = *(u8 *)raw_data-1;
  87. break;
  88. case HID_USAGE_SENSOR_TIME_DAY:
  89. time_buf->tm_mday = *(u8 *)raw_data;
  90. break;
  91. case HID_USAGE_SENSOR_TIME_HOUR:
  92. time_buf->tm_hour = *(u8 *)raw_data;
  93. break;
  94. case HID_USAGE_SENSOR_TIME_MINUTE:
  95. time_buf->tm_min = *(u8 *)raw_data;
  96. break;
  97. case HID_USAGE_SENSOR_TIME_SECOND:
  98. time_buf->tm_sec = *(u8 *)raw_data;
  99. break;
  100. default:
  101. return -EINVAL;
  102. }
  103. return 0;
  104. }
  105. /* small helper, haven't found any other way */
  106. static const char *hid_time_attrib_name(u32 attrib_id)
  107. {
  108. static const char unknown[] = "unknown";
  109. unsigned i;
  110. for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i) {
  111. if (hid_time_addresses[i] == attrib_id)
  112. return hid_time_channel_names[i];
  113. }
  114. return unknown; /* should never happen */
  115. }
  116. static int hid_time_parse_report(struct platform_device *pdev,
  117. struct hid_sensor_hub_device *hsdev,
  118. unsigned usage_id,
  119. struct hid_time_state *time_state)
  120. {
  121. int report_id, i;
  122. for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i)
  123. if (sensor_hub_input_get_attribute_info(hsdev,
  124. HID_INPUT_REPORT, usage_id,
  125. hid_time_addresses[i],
  126. &time_state->info[i]) < 0)
  127. return -EINVAL;
  128. /* Check the (needed) attributes for sanity */
  129. report_id = time_state->info[0].report_id;
  130. if (report_id < 0) {
  131. dev_err(&pdev->dev, "bad report ID!\n");
  132. return -EINVAL;
  133. }
  134. for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i) {
  135. if (time_state->info[i].report_id != report_id) {
  136. dev_err(&pdev->dev,
  137. "not all needed attributes inside the same report!\n");
  138. return -EINVAL;
  139. }
  140. if (time_state->info[i].size != 1) {
  141. dev_err(&pdev->dev,
  142. "attribute '%s' not 8 bits wide!\n",
  143. hid_time_attrib_name(
  144. time_state->info[i].attrib_id));
  145. return -EINVAL;
  146. }
  147. if (time_state->info[i].units !=
  148. HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED &&
  149. /* allow attribute seconds with unit seconds */
  150. !(time_state->info[i].attrib_id ==
  151. HID_USAGE_SENSOR_TIME_SECOND &&
  152. time_state->info[i].units ==
  153. HID_USAGE_SENSOR_UNITS_SECOND)) {
  154. dev_err(&pdev->dev,
  155. "attribute '%s' hasn't a unit of type 'none'!\n",
  156. hid_time_attrib_name(
  157. time_state->info[i].attrib_id));
  158. return -EINVAL;
  159. }
  160. if (time_state->info[i].unit_expo) {
  161. dev_err(&pdev->dev,
  162. "attribute '%s' hasn't a unit exponent of 1!\n",
  163. hid_time_attrib_name(
  164. time_state->info[i].attrib_id));
  165. return -EINVAL;
  166. }
  167. }
  168. return 0;
  169. }
  170. static int hid_rtc_read_time(struct device *dev, struct rtc_time *tm)
  171. {
  172. unsigned long flags;
  173. struct hid_time_state *time_state =
  174. platform_get_drvdata(to_platform_device(dev));
  175. int ret;
  176. INIT_COMPLETION(time_state->comp_last_time);
  177. /* get a report with all values through requesting one value */
  178. sensor_hub_input_attr_get_raw_value(time_state->common_attributes.hsdev,
  179. HID_USAGE_SENSOR_TIME, hid_time_addresses[0],
  180. time_state->info[0].report_id);
  181. /* wait for all values (event) */
  182. ret = wait_for_completion_killable_timeout(
  183. &time_state->comp_last_time, HZ*6);
  184. if (ret > 0) {
  185. /* no error */
  186. spin_lock_irqsave(&time_state->lock_last_time, flags);
  187. *tm = time_state->last_time;
  188. spin_unlock_irqrestore(&time_state->lock_last_time, flags);
  189. return 0;
  190. }
  191. if (!ret)
  192. return -EIO; /* timeouted */
  193. return ret; /* killed (-ERESTARTSYS) */
  194. }
  195. static const struct rtc_class_ops hid_time_rtc_ops = {
  196. .read_time = hid_rtc_read_time,
  197. };
  198. static int hid_time_probe(struct platform_device *pdev)
  199. {
  200. int ret = 0;
  201. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  202. struct hid_time_state *time_state = devm_kzalloc(&pdev->dev,
  203. sizeof(struct hid_time_state), GFP_KERNEL);
  204. if (time_state == NULL)
  205. return -ENOMEM;
  206. platform_set_drvdata(pdev, time_state);
  207. init_completion(&time_state->comp_last_time);
  208. time_state->common_attributes.hsdev = hsdev;
  209. time_state->common_attributes.pdev = pdev;
  210. ret = hid_sensor_parse_common_attributes(hsdev,
  211. HID_USAGE_SENSOR_TIME,
  212. &time_state->common_attributes);
  213. if (ret) {
  214. dev_err(&pdev->dev, "failed to setup common attributes!\n");
  215. return ret;
  216. }
  217. ret = hid_time_parse_report(pdev, hsdev, HID_USAGE_SENSOR_TIME,
  218. time_state);
  219. if (ret) {
  220. dev_err(&pdev->dev, "failed to setup attributes!\n");
  221. return ret;
  222. }
  223. time_state->callbacks.send_event = hid_time_proc_event;
  224. time_state->callbacks.capture_sample = hid_time_capture_sample;
  225. time_state->callbacks.pdev = pdev;
  226. ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TIME,
  227. &time_state->callbacks);
  228. if (ret < 0) {
  229. dev_err(&pdev->dev, "register callback failed!\n");
  230. return ret;
  231. }
  232. time_state->rtc = rtc_device_register("hid-sensor-time",
  233. &pdev->dev, &hid_time_rtc_ops, THIS_MODULE);
  234. if (IS_ERR(time_state->rtc)) {
  235. dev_err(&pdev->dev, "rtc device register failed!\n");
  236. return PTR_ERR(time_state->rtc);
  237. }
  238. return ret;
  239. }
  240. static int hid_time_remove(struct platform_device *pdev)
  241. {
  242. struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
  243. struct hid_time_state *time_state = platform_get_drvdata(pdev);
  244. rtc_device_unregister(time_state->rtc);
  245. sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TIME);
  246. return 0;
  247. }
  248. static struct platform_driver hid_time_platform_driver = {
  249. .driver = {
  250. .name = DRIVER_NAME,
  251. .owner = THIS_MODULE,
  252. },
  253. .probe = hid_time_probe,
  254. .remove = hid_time_remove,
  255. };
  256. module_platform_driver(hid_time_platform_driver);
  257. MODULE_DESCRIPTION("HID Sensor Time");
  258. MODULE_AUTHOR("Alexander Holler <holler@ahsoftware.de>");
  259. MODULE_LICENSE("GPL");