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