summaryrefslogtreecommitdiff
path: root/drivers/rtc/rtc-hid-sensor-time.c
blob: 4aca12d1d564f6917156707e4805e1c047319f34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * HID Sensor Time Driver
 * Copyright (c) 2012, Alexander Holler.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/hid-sensor-hub.h>
#include <linux/iio/iio.h>
#include <linux/rtc.h>

/* Format: HID-SENSOR-usage_id_in_hex */
/* Usage ID from spec for Time: 0x2000A0 */
#define DRIVER_NAME "HID-SENSOR-2000a0" /* must be lowercase */

static bool hid_time_hctosys_enabled = 1;
module_param_named(hctosys, hid_time_hctosys_enabled, bool, 0644);
MODULE_PARM_DESC(hctosys,
	"set the system time (once) if it is before 1970-01-02");
static bool hid_time_time_set_once;

enum hid_time_channel {
	CHANNEL_SCAN_INDEX_YEAR,
	CHANNEL_SCAN_INDEX_MONTH,
	CHANNEL_SCAN_INDEX_DAY,
	CHANNEL_SCAN_INDEX_HOUR,
	CHANNEL_SCAN_INDEX_MINUTE,
	CHANNEL_SCAN_INDEX_SECOND,
	CHANNEL_SCAN_INDEX_MILLISECOND, /* optional */
	TIME_RTC_CHANNEL_MAX,
};

struct hid_time_workts {
	struct work_struct work;
	struct hid_time_state *time_state;
};

struct hid_time_state {
	struct hid_sensor_hub_callbacks callbacks;
	struct hid_sensor_common common_attributes;
	struct hid_sensor_hub_attribute_info info[TIME_RTC_CHANNEL_MAX];
	struct rtc_time last_time;
	unsigned last_ms; /* == UINT_MAX to indicate ms aren't supported */
	spinlock_t lock_last_time;
	struct completion comp_last_time;
	struct rtc_time time_buf;
	unsigned ms_buf;
	struct rtc_device *rtc;
	struct hid_time_workts *workts;
};

static const u32 hid_time_addresses[TIME_RTC_CHANNEL_MAX] = {
	HID_USAGE_SENSOR_TIME_YEAR,
	HID_USAGE_SENSOR_TIME_MONTH,
	HID_USAGE_SENSOR_TIME_DAY,
	HID_USAGE_SENSOR_TIME_HOUR,
	HID_USAGE_SENSOR_TIME_MINUTE,
	HID_USAGE_SENSOR_TIME_SECOND,
	HID_USAGE_SENSOR_TIME_MILLISECOND, /* optional */
};

/* Channel names for verbose error messages */
static const char * const hid_time_channel_names[TIME_RTC_CHANNEL_MAX] = {
	"year", "month", "day", "hour", "minute", "second", "millisecond",
};

static void hid_time_hctosys(struct hid_time_state *time_state)
{
	struct timespec ts;
	char msbuf[5];
	int rc = rtc_valid_tm(&time_state->last_time);

	if (rc) {
		dev_err(time_state->rtc->dev.parent,
			"hctosys: invalid date/time\n");
		return;
	}

	getnstimeofday(&ts);
	if (ts.tv_sec >= 86400) /* 1970-01-02 00:00:00 UTC */
		/*
		 * Security: don't let a hot-pluggable device change
		 * a valid system time.
		 * In absence of a kernel-wide flag like 'systime_was_set',
		 * we check if the system time is earlier than 1970-01-02.
		 * Just using a flag inside the RTC subsystem (e.g. from
		 * hctosys) doesn't work, because the time could be set
		 * by userspace (NTP, date, user, ...) or something else
		 * in the kernel too.
		 * Using a whole day to decide if something else has set the
		 * time should be enough for even the longest boot to complete
		 * (including possible forced fscks) and load this module.
		 */
		return;

	if (time_state->last_ms != UINT_MAX) {
		ts.tv_nsec = time_state->last_ms * NSEC_PER_MSEC;
		snprintf(msbuf, sizeof(msbuf), ":%03d", time_state->last_ms);
	} else {
		ts.tv_nsec = NSEC_PER_SEC >> 1;
		*msbuf = 0;
	}
	rtc_tm_to_time(&time_state->last_time, &ts.tv_sec);
	rc = do_settimeofday(&ts);
	dev_info(time_state->rtc->dev.parent,
		"hctosys: setting system clock to "
		"%d-%02d-%02d %02d:%02d:%02d%s UTC (%u)\n",
		time_state->last_time.tm_year + 1900,
		time_state->last_time.tm_mon + 1,
		time_state->last_time.tm_mday,
		time_state->last_time.tm_hour,
		time_state->last_time.tm_min,
		time_state->last_time.tm_sec,
		msbuf,
		(unsigned int) ts.tv_sec);
}

/* Callback handler to send event after all samples are received and captured */
static int hid_time_proc_event(struct hid_sensor_hub_device *hsdev,
				unsigned usage_id, void *priv)
{
	unsigned long flags;
	struct hid_time_state *time_state = platform_get_drvdata(priv);

	spin_lock_irqsave(&time_state->lock_last_time, flags);
	time_state->last_time = time_state->time_buf;
	if (time_state->last_ms != UINT_MAX)
		time_state->last_ms = time_state->ms_buf;
	spin_unlock_irqrestore(&time_state->lock_last_time, flags);
	complete(&time_state->comp_last_time);
	if (unlikely(!hid_time_time_set_once && hid_time_hctosys_enabled)) {
		hid_time_time_set_once = 1;
		hid_time_hctosys(time_state);
	}
	return 0;
}

static u32 hid_time_value(size_t raw_len, char *raw_data)
{
	switch (raw_len) {
	case 1:
		return *(u8 *)raw_data;
	case 2:
		return *(u16 *)raw_data;
	case 4:
		return *(u32 *)raw_data;
	default:
		return (u32)(~0U); /* 0xff... or -1 to denote an error */
	}
}

static int hid_time_capture_sample(struct hid_sensor_hub_device *hsdev,
				unsigned usage_id, size_t raw_len,
				char *raw_data, void *priv)
{
	struct hid_time_state *time_state = platform_get_drvdata(priv);
	struct rtc_time *time_buf = &time_state->time_buf;

	switch (usage_id) {
	case HID_USAGE_SENSOR_TIME_YEAR:
		/*
		 * The draft for HID-sensors (HUTRR39) currently doesn't define
		 * the range for the year attribute. Therefor we support
		 * 8 bit (0-99) and 16 or 32 bits (full) as size for the year.
		 */
		if (raw_len == 1) {
			time_buf->tm_year = *(u8 *)raw_data;
			if (time_buf->tm_year < 70)
				/* assume we are in 1970...2069 */
				time_buf->tm_year += 100;
		} else
			time_buf->tm_year =
				(int)hid_time_value(raw_len, raw_data)-1900;
		break;
	case HID_USAGE_SENSOR_TIME_MONTH:
		/* sensors are sending the month as 1-12, we need 0-11 */
		time_buf->tm_mon = (int)hid_time_value(raw_len, raw_data)-1;
		break;
	case HID_USAGE_SENSOR_TIME_DAY:
		time_buf->tm_mday = (int)hid_time_value(raw_len, raw_data);
		break;
	case HID_USAGE_SENSOR_TIME_HOUR:
		time_buf->tm_hour = (int)hid_time_value(raw_len, raw_data);
		break;
	case HID_USAGE_SENSOR_TIME_MINUTE:
		time_buf->tm_min = (int)hid_time_value(raw_len, raw_data);
		break;
	case HID_USAGE_SENSOR_TIME_SECOND:
		time_buf->tm_sec = (int)hid_time_value(raw_len, raw_data);
		break;
	case HID_USAGE_SENSOR_TIME_MILLISECOND:
		time_state->ms_buf = hid_time_value(raw_len, raw_data);
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

/* small helper, haven't found any other way */
static const char *hid_time_attrib_name(u32 attrib_id)
{
	static const char unknown[] = "unknown";
	unsigned i;

	for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i) {
		if (hid_time_addresses[i] == attrib_id)
			return hid_time_channel_names[i];
	}
	return unknown; /* should never happen */
}

static int hid_time_parse_report(struct platform_device *pdev,
				struct hid_sensor_hub_device *hsdev,
				unsigned usage_id,
				struct hid_time_state *time_state)
{
	int report_id, i;

	/* Check if all required attributes are available */
	for (i = 0; i < CHANNEL_SCAN_INDEX_MILLISECOND; ++i)
		if (sensor_hub_input_get_attribute_info(hsdev,
				HID_INPUT_REPORT, usage_id,
				hid_time_addresses[i],
				&time_state->info[i]) < 0)
			return -EINVAL;
	if (!sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
		usage_id, hid_time_addresses[i], &time_state->info[i])) {
		dev_info(&pdev->dev, "milliseconds supported\n");
		time_state->last_ms = 0;
	}
	/* Check the (needed) attributes for sanity */
	report_id = time_state->info[0].report_id;
	if (report_id < 0) {
		dev_err(&pdev->dev, "bad report ID!\n");
		return -EINVAL;
	}
	for (i = 0; i < TIME_RTC_CHANNEL_MAX; ++i) {
		if (time_state->info[i].attrib_id ==
				HID_USAGE_SENSOR_TIME_MILLISECOND &&
				time_state->last_ms == UINT_MAX)
			continue;
		if (time_state->info[i].report_id != report_id) {
			dev_err(&pdev->dev,
				"not all needed attributes inside the same report!\n");
			return -EINVAL;
		}
		if (time_state->info[i].size == 3 ||
				time_state->info[i].size > 4) {
			dev_err(&pdev->dev,
				"attribute '%s' not 8, 16 or 32 bits wide!\n",
				hid_time_attrib_name(
					time_state->info[i].attrib_id));
			return -EINVAL;
		}
		if (time_state->info[i].units !=
				HID_USAGE_SENSOR_UNITS_NOT_SPECIFIED &&
				/* allow attribute seconds with unit seconds */
				!(time_state->info[i].attrib_id ==
				HID_USAGE_SENSOR_TIME_SECOND &&
				time_state->info[i].units ==
				HID_USAGE_SENSOR_UNITS_SECOND)) {
			dev_err(&pdev->dev,
				"attribute '%s' hasn't a unit of type 'none'!\n",
				hid_time_attrib_name(
					time_state->info[i].attrib_id));
			return -EINVAL;
		}
		if (time_state->info[i].unit_expo) {
			dev_err(&pdev->dev,
				"attribute '%s' hasn't a unit exponent of 1!\n",
				hid_time_attrib_name(
					time_state->info[i].attrib_id));
			return -EINVAL;
		}
	}

	return 0;
}

static int hid_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
	unsigned long flags;
	struct hid_time_state *time_state =
		platform_get_drvdata(to_platform_device(dev));
	int ret;

	INIT_COMPLETION(time_state->comp_last_time);
	/* get a report with all values through requesting one value */
	sensor_hub_input_attr_get_raw_value(time_state->common_attributes.hsdev,
			HID_USAGE_SENSOR_TIME, hid_time_addresses[0],
			time_state->info[0].report_id);
	/* wait for all values (event) */
	ret = wait_for_completion_killable_timeout(
			&time_state->comp_last_time, HZ*6);
	if (ret > 0) {
		/* no error */
		spin_lock_irqsave(&time_state->lock_last_time, flags);
		*tm = time_state->last_time;
		spin_unlock_irqrestore(&time_state->lock_last_time, flags);
		return 0;
	}
	if (!ret)
		return -EIO; /* timeouted */
	return ret; /* killed (-ERESTARTSYS) */
}

static const struct rtc_class_ops hid_time_rtc_ops = {
	.read_time = hid_rtc_read_time,
};

static void hid_time_request_report_work(struct work_struct *work)
{
	struct hid_time_state *time_state =
		container_of(work, struct hid_time_workts, work)
			->time_state;
	/* get a report with all values through requesting one value */
	sensor_hub_input_attr_get_raw_value(
		time_state->common_attributes.hsdev, HID_USAGE_SENSOR_TIME,
		hid_time_addresses[0], time_state->info[0].report_id);
	time_state->workts = NULL;
	kfree(work);
}

static int hid_time_probe(struct platform_device *pdev)
{
	int ret = 0;
	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
	struct hid_time_state *time_state = devm_kzalloc(&pdev->dev,
		sizeof(struct hid_time_state), GFP_KERNEL);

	if (time_state == NULL)
		return -ENOMEM;

	time_state->last_ms = UINT_MAX;

	platform_set_drvdata(pdev, time_state);

	spin_lock_init(&time_state->lock_last_time);
	init_completion(&time_state->comp_last_time);
	time_state->common_attributes.hsdev = hsdev;
	time_state->common_attributes.pdev = pdev;

	ret = hid_sensor_parse_common_attributes(hsdev,
				HID_USAGE_SENSOR_TIME,
				&time_state->common_attributes);
	if (ret) {
		dev_err(&pdev->dev, "failed to setup common attributes!\n");
		return ret;
	}

	ret = hid_time_parse_report(pdev, hsdev, HID_USAGE_SENSOR_TIME,
					time_state);
	if (ret) {
		dev_err(&pdev->dev, "failed to setup attributes!\n");
		return ret;
	}

	time_state->callbacks.send_event = hid_time_proc_event;
	time_state->callbacks.capture_sample = hid_time_capture_sample;
	time_state->callbacks.pdev = pdev;
	ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_TIME,
					&time_state->callbacks);
	if (ret < 0) {
		dev_err(&pdev->dev, "register callback failed!\n");
		return ret;
	}

	time_state->rtc = devm_rtc_device_register(&pdev->dev,
					"hid-sensor-time", &hid_time_rtc_ops,
					THIS_MODULE);

	if (IS_ERR(time_state->rtc)) {
		dev_err(&pdev->dev, "rtc device register failed!\n");
		return PTR_ERR(time_state->rtc);
	}

	if (!hid_time_time_set_once && hid_time_hctosys_enabled) {
		/*
		 * Request a HID report to set the time.
		 * Calling sensor_hub_..._get_raw_value() here directly
		 * doesn't work, therefor we have to use a work.
		 */
		time_state->workts = kmalloc(sizeof(struct hid_time_workts),
			GFP_KERNEL);
		if (time_state->workts == NULL)
			return -ENOMEM;
		time_state->workts->time_state = time_state;
		INIT_WORK(&time_state->workts->work,
			hid_time_request_report_work);
		schedule_work(&time_state->workts->work);
	}

	return ret;
}

static int hid_time_remove(struct platform_device *pdev)
{
	struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
	struct hid_time_state *time_state = platform_get_drvdata(pdev);

	if (time_state->workts) {
		cancel_work_sync(&time_state->workts->work);
		kfree(time_state->workts);
		time_state->workts = NULL;
	}
	sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TIME);

	return 0;
}

static struct platform_driver hid_time_platform_driver = {
	.driver = {
		.name	= DRIVER_NAME,
		.owner	= THIS_MODULE,
	},
	.probe		= hid_time_probe,
	.remove		= hid_time_remove,
};
module_platform_driver(hid_time_platform_driver);

MODULE_DESCRIPTION("HID Sensor Time");
MODULE_AUTHOR("Alexander Holler <holler@ahsoftware.de>");
MODULE_LICENSE("GPL");