/* 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,
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 completion comp_last_time;
struct rtc_time time_buf;
struct rtc_device *rtc;
+ struct hid_time_workts *workts;
};
static const u32 hid_time_addresses[TIME_RTC_CHANNEL_MAX] = {
"year", "month", "day", "hour", "minute", "second",
};
+static void hid_time_hctosys(struct hid_time_state *time_state)
+{
+ struct timespec ts;
+ 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;
+
+ ts.tv_nsec = NSEC_PER_SEC >> 1;
+ 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 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,
+ (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)
time_state->last_time = time_state->time_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;
}
.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;
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;