Thomas Gleixner | a61127c | 2019-05-29 16:57:49 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * HID Sensor Time Driver |
| 4 | * Copyright (c) 2012, Alexander Holler. |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 5 | */ |
| 6 | #include <linux/device.h> |
| 7 | #include <linux/platform_device.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/hid-sensor-hub.h> |
| 10 | #include <linux/iio/iio.h> |
| 11 | #include <linux/rtc.h> |
| 12 | |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 13 | enum hid_time_channel { |
| 14 | CHANNEL_SCAN_INDEX_YEAR, |
| 15 | CHANNEL_SCAN_INDEX_MONTH, |
| 16 | CHANNEL_SCAN_INDEX_DAY, |
| 17 | CHANNEL_SCAN_INDEX_HOUR, |
| 18 | CHANNEL_SCAN_INDEX_MINUTE, |
| 19 | CHANNEL_SCAN_INDEX_SECOND, |
| 20 | TIME_RTC_CHANNEL_MAX, |
| 21 | }; |
| 22 | |
| 23 | struct hid_time_state { |
| 24 | struct hid_sensor_hub_callbacks callbacks; |
Alexander Holler | e07c6d1 | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 25 | struct hid_sensor_common common_attributes; |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 26 | struct hid_sensor_hub_attribute_info info[TIME_RTC_CHANNEL_MAX]; |
| 27 | struct rtc_time last_time; |
| 28 | spinlock_t lock_last_time; |
| 29 | struct completion comp_last_time; |
| 30 | struct rtc_time time_buf; |
| 31 | struct rtc_device *rtc; |
| 32 | }; |
| 33 | |
| 34 | static const u32 hid_time_addresses[TIME_RTC_CHANNEL_MAX] = { |
| 35 | HID_USAGE_SENSOR_TIME_YEAR, |
| 36 | HID_USAGE_SENSOR_TIME_MONTH, |
| 37 | HID_USAGE_SENSOR_TIME_DAY, |
| 38 | HID_USAGE_SENSOR_TIME_HOUR, |
| 39 | HID_USAGE_SENSOR_TIME_MINUTE, |
| 40 | HID_USAGE_SENSOR_TIME_SECOND, |
| 41 | }; |
| 42 | |
| 43 | /* Channel names for verbose error messages */ |
| 44 | static const char * const hid_time_channel_names[TIME_RTC_CHANNEL_MAX] = { |
| 45 | "year", "month", "day", "hour", "minute", "second", |
| 46 | }; |
| 47 | |
| 48 | /* Callback handler to send event after all samples are received and captured */ |
| 49 | static int hid_time_proc_event(struct hid_sensor_hub_device *hsdev, |
| 50 | unsigned usage_id, void *priv) |
| 51 | { |
| 52 | unsigned long flags; |
| 53 | struct hid_time_state *time_state = platform_get_drvdata(priv); |
| 54 | |
| 55 | spin_lock_irqsave(&time_state->lock_last_time, flags); |
| 56 | time_state->last_time = time_state->time_buf; |
| 57 | spin_unlock_irqrestore(&time_state->lock_last_time, flags); |
| 58 | complete(&time_state->comp_last_time); |
| 59 | return 0; |
| 60 | } |
| 61 | |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 62 | static u32 hid_time_value(size_t raw_len, char *raw_data) |
| 63 | { |
| 64 | switch (raw_len) { |
| 65 | case 1: |
| 66 | return *(u8 *)raw_data; |
| 67 | case 2: |
| 68 | return *(u16 *)raw_data; |
| 69 | case 4: |
| 70 | return *(u32 *)raw_data; |
| 71 | default: |
| 72 | return (u32)(~0U); /* 0xff... or -1 to denote an error */ |
| 73 | } |
| 74 | } |
| 75 | |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 76 | static int hid_time_capture_sample(struct hid_sensor_hub_device *hsdev, |
| 77 | unsigned usage_id, size_t raw_len, |
| 78 | char *raw_data, void *priv) |
| 79 | { |
| 80 | struct hid_time_state *time_state = platform_get_drvdata(priv); |
| 81 | struct rtc_time *time_buf = &time_state->time_buf; |
| 82 | |
| 83 | switch (usage_id) { |
| 84 | case HID_USAGE_SENSOR_TIME_YEAR: |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 85 | /* |
| 86 | * The draft for HID-sensors (HUTRR39) currently doesn't define |
| 87 | * the range for the year attribute. Therefor we support |
| 88 | * 8 bit (0-99) and 16 or 32 bits (full) as size for the year. |
| 89 | */ |
Alexander Holler | 7e3c741 | 2013-07-03 15:07:00 -0700 | [diff] [blame] | 90 | if (raw_len == 1) { |
| 91 | time_buf->tm_year = *(u8 *)raw_data; |
| 92 | if (time_buf->tm_year < 70) |
| 93 | /* assume we are in 1970...2069 */ |
| 94 | time_buf->tm_year += 100; |
| 95 | } else |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 96 | time_buf->tm_year = |
| 97 | (int)hid_time_value(raw_len, raw_data)-1900; |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 98 | break; |
| 99 | case HID_USAGE_SENSOR_TIME_MONTH: |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 100 | /* sensors are sending the month as 1-12, we need 0-11 */ |
| 101 | time_buf->tm_mon = (int)hid_time_value(raw_len, raw_data)-1; |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 102 | break; |
| 103 | case HID_USAGE_SENSOR_TIME_DAY: |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 104 | time_buf->tm_mday = (int)hid_time_value(raw_len, raw_data); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 105 | break; |
| 106 | case HID_USAGE_SENSOR_TIME_HOUR: |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 107 | time_buf->tm_hour = (int)hid_time_value(raw_len, raw_data); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 108 | break; |
| 109 | case HID_USAGE_SENSOR_TIME_MINUTE: |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 110 | time_buf->tm_min = (int)hid_time_value(raw_len, raw_data); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 111 | break; |
| 112 | case HID_USAGE_SENSOR_TIME_SECOND: |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 113 | time_buf->tm_sec = (int)hid_time_value(raw_len, raw_data); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 114 | break; |
| 115 | default: |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* small helper, haven't found any other way */ |
| 122 | static const char *hid_time_attrib_name(u32 attrib_id) |
| 123 | { |
| 124 | static const char unknown[] = "unknown"; |
| 125 | unsigned i; |
| 126 | |
| 127 | for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i) { |
| 128 | if (hid_time_addresses[i] == attrib_id) |
| 129 | return hid_time_channel_names[i]; |
| 130 | } |
| 131 | return unknown; /* should never happen */ |
| 132 | } |
| 133 | |
| 134 | static int hid_time_parse_report(struct platform_device *pdev, |
| 135 | struct hid_sensor_hub_device *hsdev, |
| 136 | unsigned usage_id, |
| 137 | struct hid_time_state *time_state) |
| 138 | { |
| 139 | int report_id, i; |
| 140 | |
| 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 | } |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 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", |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 163 | hid_time_attrib_name( |
| 164 | time_state->info[i].attrib_id)); |
Alexander Holler | c67d96e | 2013-07-03 15:07:02 -0700 | [diff] [blame] | 165 | return -EINVAL; |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 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 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int hid_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 193 | { |
| 194 | unsigned long flags; |
Kefeng Wang | 527bd75 | 2019-04-23 15:50:16 +0800 | [diff] [blame] | 195 | struct hid_time_state *time_state = dev_get_drvdata(dev); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 196 | int ret; |
| 197 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 198 | reinit_completion(&time_state->comp_last_time); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 199 | /* get a report with all values through requesting one value */ |
| 200 | sensor_hub_input_attr_get_raw_value(time_state->common_attributes.hsdev, |
| 201 | HID_USAGE_SENSOR_TIME, hid_time_addresses[0], |
Hans de Goede | 0145b50 | 2018-10-31 15:20:05 +0100 | [diff] [blame] | 202 | time_state->info[0].report_id, SENSOR_HUB_SYNC, false); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 203 | /* wait for all values (event) */ |
| 204 | ret = wait_for_completion_killable_timeout( |
| 205 | &time_state->comp_last_time, HZ*6); |
| 206 | if (ret > 0) { |
| 207 | /* no error */ |
| 208 | spin_lock_irqsave(&time_state->lock_last_time, flags); |
| 209 | *tm = time_state->last_time; |
| 210 | spin_unlock_irqrestore(&time_state->lock_last_time, flags); |
| 211 | return 0; |
| 212 | } |
| 213 | if (!ret) |
| 214 | return -EIO; /* timeouted */ |
| 215 | return ret; /* killed (-ERESTARTSYS) */ |
| 216 | } |
| 217 | |
| 218 | static const struct rtc_class_ops hid_time_rtc_ops = { |
| 219 | .read_time = hid_rtc_read_time, |
| 220 | }; |
| 221 | |
| 222 | static int hid_time_probe(struct platform_device *pdev) |
| 223 | { |
| 224 | int ret = 0; |
Jingoo Han | d5ceede | 2013-11-14 14:31:59 -0800 | [diff] [blame] | 225 | struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 226 | struct hid_time_state *time_state = devm_kzalloc(&pdev->dev, |
| 227 | sizeof(struct hid_time_state), GFP_KERNEL); |
| 228 | |
| 229 | if (time_state == NULL) |
| 230 | return -ENOMEM; |
| 231 | |
| 232 | platform_set_drvdata(pdev, time_state); |
| 233 | |
Axel Lin | b775dce | 2013-01-17 01:49:00 +0000 | [diff] [blame] | 234 | spin_lock_init(&time_state->lock_last_time); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 235 | init_completion(&time_state->comp_last_time); |
| 236 | time_state->common_attributes.hsdev = hsdev; |
| 237 | time_state->common_attributes.pdev = pdev; |
| 238 | |
| 239 | ret = hid_sensor_parse_common_attributes(hsdev, |
| 240 | HID_USAGE_SENSOR_TIME, |
Ye Xiang | 0e41fd5 | 2021-02-01 13:49:20 +0800 | [diff] [blame] | 241 | &time_state->common_attributes, |
| 242 | NULL, |
| 243 | 0); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 244 | if (ret) { |
| 245 | dev_err(&pdev->dev, "failed to setup common attributes!\n"); |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | ret = hid_time_parse_report(pdev, hsdev, HID_USAGE_SENSOR_TIME, |
| 250 | time_state); |
| 251 | if (ret) { |
| 252 | dev_err(&pdev->dev, "failed to setup attributes!\n"); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | time_state->callbacks.send_event = hid_time_proc_event; |
| 257 | time_state->callbacks.capture_sample = hid_time_capture_sample; |
| 258 | time_state->callbacks.pdev = pdev; |
| 259 | ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TIME, |
| 260 | &time_state->callbacks); |
| 261 | if (ret < 0) { |
| 262 | dev_err(&pdev->dev, "register callback failed!\n"); |
| 263 | return ret; |
| 264 | } |
| 265 | |
Srinivas Pandruvada | 476b954 | 2013-10-01 17:22:00 +0100 | [diff] [blame] | 266 | ret = sensor_hub_device_open(hsdev); |
| 267 | if (ret) { |
| 268 | dev_err(&pdev->dev, "failed to open sensor hub device!\n"); |
| 269 | goto err_open; |
| 270 | } |
| 271 | |
Alexander Holler | 406bf31 | 2013-11-14 14:32:00 -0800 | [diff] [blame] | 272 | /* |
| 273 | * Enable HID input processing early in order to be able to read the |
| 274 | * clock already in devm_rtc_device_register(). |
| 275 | */ |
| 276 | hid_device_io_start(hsdev->hdev); |
| 277 | |
Jingoo Han | 479243b | 2013-04-29 16:19:38 -0700 | [diff] [blame] | 278 | time_state->rtc = devm_rtc_device_register(&pdev->dev, |
| 279 | "hid-sensor-time", &hid_time_rtc_ops, |
| 280 | THIS_MODULE); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 281 | |
Dan Carpenter | e73ef75 | 2017-04-12 11:32:53 +0300 | [diff] [blame] | 282 | if (IS_ERR(time_state->rtc)) { |
Alexander Holler | 406bf31 | 2013-11-14 14:32:00 -0800 | [diff] [blame] | 283 | hid_device_io_stop(hsdev->hdev); |
Dan Carpenter | e73ef75 | 2017-04-12 11:32:53 +0300 | [diff] [blame] | 284 | ret = PTR_ERR(time_state->rtc); |
Alexander Holler | 7707bda | 2013-09-11 14:24:20 -0700 | [diff] [blame] | 285 | time_state->rtc = NULL; |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 286 | dev_err(&pdev->dev, "rtc device register failed!\n"); |
Srinivas Pandruvada | 476b954 | 2013-10-01 17:22:00 +0100 | [diff] [blame] | 287 | goto err_rtc; |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | return ret; |
Srinivas Pandruvada | 476b954 | 2013-10-01 17:22:00 +0100 | [diff] [blame] | 291 | |
| 292 | err_rtc: |
| 293 | sensor_hub_device_close(hsdev); |
| 294 | err_open: |
| 295 | sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TIME); |
| 296 | return ret; |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | static int hid_time_remove(struct platform_device *pdev) |
| 300 | { |
Jingoo Han | d5ceede | 2013-11-14 14:31:59 -0800 | [diff] [blame] | 301 | struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 302 | |
Srinivas Pandruvada | 476b954 | 2013-10-01 17:22:00 +0100 | [diff] [blame] | 303 | sensor_hub_device_close(hsdev); |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 304 | sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TIME); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
Krzysztof Kozlowski | 19fd685 | 2015-05-02 00:44:36 +0900 | [diff] [blame] | 309 | static const struct platform_device_id hid_time_ids[] = { |
Alexander Holler | ae25992 | 2013-09-11 14:24:15 -0700 | [diff] [blame] | 310 | { |
| 311 | /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ |
| 312 | .name = "HID-SENSOR-2000a0", |
| 313 | }, |
| 314 | { /* sentinel */ } |
| 315 | }; |
| 316 | MODULE_DEVICE_TABLE(platform, hid_time_ids); |
| 317 | |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 318 | static struct platform_driver hid_time_platform_driver = { |
Alexander Holler | ae25992 | 2013-09-11 14:24:15 -0700 | [diff] [blame] | 319 | .id_table = hid_time_ids, |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 320 | .driver = { |
Alexander Holler | ae25992 | 2013-09-11 14:24:15 -0700 | [diff] [blame] | 321 | .name = KBUILD_MODNAME, |
Alexander Holler | 62e00cb | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 322 | }, |
| 323 | .probe = hid_time_probe, |
| 324 | .remove = hid_time_remove, |
| 325 | }; |
| 326 | module_platform_driver(hid_time_platform_driver); |
| 327 | |
| 328 | MODULE_DESCRIPTION("HID Sensor Time"); |
| 329 | MODULE_AUTHOR("Alexander Holler <holler@ahsoftware.de>"); |
| 330 | MODULE_LICENSE("GPL"); |
Andy Shevchenko | 12f13d1 | 2021-06-14 19:24:47 +0300 | [diff] [blame] | 331 | MODULE_IMPORT_NS(IIO_HID); |