1 // SPDX-License-Identifier: GPL-2.0
3 * U2F Zero LED and RNG driver
5 * Copyright 2018 Andrej Shadura <andrew@shadura.me>
6 * Loosely based on drivers/hid/hid-led.c
7 * and drivers/usb/misc/chaoskey.c
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2.
14 #include <linux/hid.h>
15 #include <linux/hidraw.h>
16 #include <linux/hw_random.h>
17 #include <linux/leds.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/usb.h>
22 #include "usbhid/usbhid.h"
25 #define DRIVER_SHORT "u2fzero"
27 #define HID_REPORT_SIZE 64
34 struct hw_revision_config {
40 static const struct hw_revision_config hw_configs[] = {
49 .name = "NitroKey U2F",
53 /* We only use broadcast (CID-less) messages */
54 #define CID_BROADCAST 0xffffffff
63 u8 data[HID_REPORT_SIZE - 7];
67 u8 data[HID_REPORT_SIZE - 5];
72 struct u2f_hid_report {
74 struct u2f_hid_msg msg;
77 #define U2F_HID_MSG_LEN(f) (size_t)(((f).init.bcnth << 8) + (f).init.bcntl)
79 struct u2fzero_device {
80 struct hid_device *hdev;
81 struct urb *urb; /* URB for the RNG data */
82 struct led_classdev ldev; /* Embedded struct for led */
83 struct hwrng hwrng; /* Embedded struct for hwrng */
90 kernel_ulong_t hw_revision;
93 static int u2fzero_send(struct u2fzero_device *dev, struct u2f_hid_report *req)
97 mutex_lock(&dev->lock);
99 memcpy(dev->buf_out, req, sizeof(struct u2f_hid_report));
101 ret = hid_hw_output_report(dev->hdev, dev->buf_out,
102 sizeof(struct u2f_hid_msg));
104 mutex_unlock(&dev->lock);
109 return ret == sizeof(struct u2f_hid_msg) ? 0 : -EMSGSIZE;
112 struct u2fzero_transfer_context {
113 struct completion done;
117 static void u2fzero_read_callback(struct urb *urb)
119 struct u2fzero_transfer_context *ctx = urb->context;
121 ctx->status = urb->status;
122 complete(&ctx->done);
125 static int u2fzero_recv(struct u2fzero_device *dev,
126 struct u2f_hid_report *req,
127 struct u2f_hid_msg *resp)
130 struct hid_device *hdev = dev->hdev;
131 struct u2fzero_transfer_context ctx;
133 mutex_lock(&dev->lock);
135 memcpy(dev->buf_out, req, sizeof(struct u2f_hid_report));
137 dev->urb->context = &ctx;
138 init_completion(&ctx.done);
140 ret = usb_submit_urb(dev->urb, GFP_NOIO);
142 hid_err(hdev, "usb_submit_urb failed: %d", ret);
146 ret = hid_hw_output_report(dev->hdev, dev->buf_out,
147 sizeof(struct u2f_hid_msg));
150 hid_err(hdev, "hid_hw_output_report failed: %d", ret);
154 ret = (wait_for_completion_timeout(
155 &ctx.done, msecs_to_jiffies(USB_CTRL_SET_TIMEOUT)));
157 usb_kill_urb(dev->urb);
158 hid_err(hdev, "urb submission timed out");
160 ret = dev->urb->actual_length;
161 memcpy(resp, dev->buf_in, ret);
165 mutex_unlock(&dev->lock);
170 static int u2fzero_blink(struct led_classdev *ldev)
172 struct u2fzero_device *dev = container_of(ldev,
173 struct u2fzero_device, ldev);
174 struct u2f_hid_report req = {
176 .msg.cid = CID_BROADCAST,
178 .cmd = hw_configs[dev->hw_revision].wink_cmd,
184 return u2fzero_send(dev, &req);
187 static int u2fzero_brightness_set(struct led_classdev *ldev,
188 enum led_brightness brightness)
190 ldev->brightness = LED_OFF;
192 return u2fzero_blink(ldev);
197 static int u2fzero_rng_read(struct hwrng *rng, void *data,
198 size_t max, bool wait)
200 struct u2fzero_device *dev = container_of(rng,
201 struct u2fzero_device, hwrng);
202 struct u2f_hid_report req = {
204 .msg.cid = CID_BROADCAST,
206 .cmd = hw_configs[dev->hw_revision].rng_cmd,
212 struct u2f_hid_msg resp;
214 size_t actual_length;
215 /* valid packets must have a correct header */
216 int min_length = offsetof(struct u2f_hid_msg, init.data);
219 hid_dbg(dev->hdev, "device not present");
223 ret = u2fzero_recv(dev, &req, &resp);
225 /* ignore errors or packets without data */
226 if (ret < min_length)
229 /* only take the minimum amount of data it is safe to take */
230 actual_length = min3((size_t)ret - min_length,
231 U2F_HID_MSG_LEN(resp), max);
233 memcpy(data, resp.init.data, actual_length);
235 return actual_length;
238 static int u2fzero_init_led(struct u2fzero_device *dev,
241 dev->led_name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
242 "%s%u", DRIVER_SHORT, minor);
243 if (dev->led_name == NULL)
246 dev->ldev.name = dev->led_name;
247 dev->ldev.max_brightness = LED_ON;
248 dev->ldev.flags = LED_HW_PLUGGABLE;
249 dev->ldev.brightness_set_blocking = u2fzero_brightness_set;
251 return devm_led_classdev_register(&dev->hdev->dev, &dev->ldev);
254 static int u2fzero_init_hwrng(struct u2fzero_device *dev,
257 dev->rng_name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
258 "%s-rng%u", DRIVER_SHORT, minor);
259 if (dev->rng_name == NULL)
262 dev->hwrng.name = dev->rng_name;
263 dev->hwrng.read = u2fzero_rng_read;
265 return devm_hwrng_register(&dev->hdev->dev, &dev->hwrng);
268 static int u2fzero_fill_in_urb(struct u2fzero_device *dev)
270 struct hid_device *hdev = dev->hdev;
271 struct usb_device *udev;
272 struct usbhid_device *usbhid = hdev->driver_data;
273 unsigned int pipe_in;
274 struct usb_host_endpoint *ep;
276 if (dev->hdev->bus != BUS_USB)
279 udev = hid_to_usb_dev(hdev);
281 if (!usbhid->urbout || !usbhid->urbin)
284 ep = usb_pipe_endpoint(udev, usbhid->urbin->pipe);
288 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
292 pipe_in = (usbhid->urbin->pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
294 usb_fill_int_urb(dev->urb,
299 u2fzero_read_callback,
306 static int u2fzero_probe(struct hid_device *hdev,
307 const struct hid_device_id *id)
309 struct u2fzero_device *dev;
313 if (!hid_is_usb(hdev))
316 dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
320 dev->hw_revision = id->driver_data;
322 dev->buf_out = devm_kmalloc(&hdev->dev,
323 sizeof(struct u2f_hid_report), GFP_KERNEL);
324 if (dev->buf_out == NULL)
327 dev->buf_in = devm_kmalloc(&hdev->dev,
328 sizeof(struct u2f_hid_msg), GFP_KERNEL);
329 if (dev->buf_in == NULL)
332 ret = hid_parse(hdev);
337 hid_set_drvdata(hdev, dev);
338 mutex_init(&dev->lock);
340 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
344 u2fzero_fill_in_urb(dev);
348 minor = ((struct hidraw *) hdev->hidraw)->minor;
350 ret = u2fzero_init_led(dev, minor);
356 hid_info(hdev, "%s LED initialised\n", hw_configs[dev->hw_revision].name);
358 ret = u2fzero_init_hwrng(dev, minor);
364 hid_info(hdev, "%s RNG initialised\n", hw_configs[dev->hw_revision].name);
369 static void u2fzero_remove(struct hid_device *hdev)
371 struct u2fzero_device *dev = hid_get_drvdata(hdev);
373 mutex_lock(&dev->lock);
374 dev->present = false;
375 mutex_unlock(&dev->lock);
378 usb_poison_urb(dev->urb);
379 usb_free_urb(dev->urb);
382 static const struct hid_device_id u2fzero_table[] = {
383 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
384 USB_DEVICE_ID_U2F_ZERO),
385 .driver_data = HW_U2FZERO },
386 { HID_USB_DEVICE(USB_VENDOR_ID_CLAY_LOGIC,
387 USB_DEVICE_ID_NITROKEY_U2F),
388 .driver_data = HW_NITROKEY_U2F },
391 MODULE_DEVICE_TABLE(hid, u2fzero_table);
393 static struct hid_driver u2fzero_driver = {
394 .name = "hid-" DRIVER_SHORT,
395 .probe = u2fzero_probe,
396 .remove = u2fzero_remove,
397 .id_table = u2fzero_table,
400 module_hid_driver(u2fzero_driver);
402 MODULE_LICENSE("GPL");
403 MODULE_AUTHOR("Andrej Shadura <andrew@shadura.me>");
404 MODULE_DESCRIPTION("U2F Zero LED and RNG driver");