rtc-hid-sensor-time.c 8.8 KB

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