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