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
29 /* We only use broadcast (CID-less) messages */
30 #define CID_BROADCAST 0xffffffff
39 u8 data[HID_REPORT_SIZE - 7];
43 u8 data[HID_REPORT_SIZE - 5];
48 struct u2f_hid_report {
50 struct u2f_hid_msg msg;
53 #define U2F_HID_MSG_LEN(f) (size_t)(((f).init.bcnth << 8) + (f).init.bcntl)
55 /* Custom extensions to the U2FHID protocol */
56 #define U2F_CUSTOM_GET_RNG 0x21
57 #define U2F_CUSTOM_WINK 0x24
59 struct u2fzero_device {
60 struct hid_device *hdev;
61 struct urb *urb; /* URB for the RNG data */
62 struct led_classdev ldev; /* Embedded struct for led */
63 struct hwrng hwrng; /* Embedded struct for hwrng */
72 static int u2fzero_send(struct u2fzero_device *dev, struct u2f_hid_report *req)
76 mutex_lock(&dev->lock);
78 memcpy(dev->buf_out, req, sizeof(struct u2f_hid_report));
80 ret = hid_hw_output_report(dev->hdev, dev->buf_out,
81 sizeof(struct u2f_hid_msg));
83 mutex_unlock(&dev->lock);
88 return ret == sizeof(struct u2f_hid_msg) ? 0 : -EMSGSIZE;
91 struct u2fzero_transfer_context {
92 struct completion done;
96 static void u2fzero_read_callback(struct urb *urb)
98 struct u2fzero_transfer_context *ctx = urb->context;
100 ctx->status = urb->status;
101 complete(&ctx->done);
104 static int u2fzero_recv(struct u2fzero_device *dev,
105 struct u2f_hid_report *req,
106 struct u2f_hid_msg *resp)
109 struct hid_device *hdev = dev->hdev;
110 struct u2fzero_transfer_context ctx;
112 mutex_lock(&dev->lock);
114 memcpy(dev->buf_out, req, sizeof(struct u2f_hid_report));
116 dev->urb->context = &ctx;
117 init_completion(&ctx.done);
119 ret = usb_submit_urb(dev->urb, GFP_NOIO);
121 hid_err(hdev, "usb_submit_urb failed: %d", ret);
125 ret = hid_hw_output_report(dev->hdev, dev->buf_out,
126 sizeof(struct u2f_hid_msg));
129 hid_err(hdev, "hid_hw_output_report failed: %d", ret);
133 ret = (wait_for_completion_timeout(
134 &ctx.done, msecs_to_jiffies(USB_CTRL_SET_TIMEOUT)));
136 usb_kill_urb(dev->urb);
137 hid_err(hdev, "urb submission timed out");
139 ret = dev->urb->actual_length;
140 memcpy(resp, dev->buf_in, ret);
144 mutex_unlock(&dev->lock);
149 static int u2fzero_blink(struct led_classdev *ldev)
151 struct u2fzero_device *dev = container_of(ldev,
152 struct u2fzero_device, ldev);
153 struct u2f_hid_report req = {
155 .msg.cid = CID_BROADCAST,
157 .cmd = U2F_CUSTOM_WINK,
163 return u2fzero_send(dev, &req);
166 static int u2fzero_brightness_set(struct led_classdev *ldev,
167 enum led_brightness brightness)
169 ldev->brightness = LED_OFF;
171 return u2fzero_blink(ldev);
176 static int u2fzero_rng_read(struct hwrng *rng, void *data,
177 size_t max, bool wait)
179 struct u2fzero_device *dev = container_of(rng,
180 struct u2fzero_device, hwrng);
181 struct u2f_hid_report req = {
183 .msg.cid = CID_BROADCAST,
185 .cmd = U2F_CUSTOM_GET_RNG,
191 struct u2f_hid_msg resp;
193 size_t actual_length;
196 hid_dbg(dev->hdev, "device not present");
200 ret = u2fzero_recv(dev, &req, &resp);
204 /* only take the minimum amount of data it is safe to take */
205 actual_length = min3((size_t)ret - offsetof(struct u2f_hid_msg,
206 init.data), U2F_HID_MSG_LEN(resp), max);
208 memcpy(data, resp.init.data, actual_length);
210 return actual_length;
213 static int u2fzero_init_led(struct u2fzero_device *dev,
216 dev->led_name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
217 "%s%u", DRIVER_SHORT, minor);
218 if (dev->led_name == NULL)
221 dev->ldev.name = dev->led_name;
222 dev->ldev.max_brightness = LED_ON;
223 dev->ldev.flags = LED_HW_PLUGGABLE;
224 dev->ldev.brightness_set_blocking = u2fzero_brightness_set;
226 return devm_led_classdev_register(&dev->hdev->dev, &dev->ldev);
229 static int u2fzero_init_hwrng(struct u2fzero_device *dev,
232 dev->rng_name = devm_kasprintf(&dev->hdev->dev, GFP_KERNEL,
233 "%s-rng%u", DRIVER_SHORT, minor);
234 if (dev->rng_name == NULL)
237 dev->hwrng.name = dev->rng_name;
238 dev->hwrng.read = u2fzero_rng_read;
239 dev->hwrng.quality = 1;
241 return devm_hwrng_register(&dev->hdev->dev, &dev->hwrng);
244 static int u2fzero_fill_in_urb(struct u2fzero_device *dev)
246 struct hid_device *hdev = dev->hdev;
247 struct usb_device *udev;
248 struct usbhid_device *usbhid = hdev->driver_data;
249 unsigned int pipe_in;
250 struct usb_host_endpoint *ep;
252 if (dev->hdev->bus != BUS_USB)
255 udev = hid_to_usb_dev(hdev);
257 if (!usbhid->urbout || !usbhid->urbin)
260 ep = usb_pipe_endpoint(udev, usbhid->urbin->pipe);
264 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
268 pipe_in = (usbhid->urbin->pipe & ~(3 << 30)) | (PIPE_INTERRUPT << 30);
270 usb_fill_int_urb(dev->urb,
275 u2fzero_read_callback,
282 static int u2fzero_probe(struct hid_device *hdev,
283 const struct hid_device_id *id)
285 struct u2fzero_device *dev;
289 if (!hid_is_using_ll_driver(hdev, &usb_hid_driver))
292 dev = devm_kzalloc(&hdev->dev, sizeof(*dev), GFP_KERNEL);
296 dev->buf_out = devm_kmalloc(&hdev->dev,
297 sizeof(struct u2f_hid_report), GFP_KERNEL);
298 if (dev->buf_out == NULL)
301 dev->buf_in = devm_kmalloc(&hdev->dev,
302 sizeof(struct u2f_hid_msg), GFP_KERNEL);
303 if (dev->buf_in == NULL)
306 ret = hid_parse(hdev);
311 hid_set_drvdata(hdev, dev);
312 mutex_init(&dev->lock);
314 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
318 u2fzero_fill_in_urb(dev);
322 minor = ((struct hidraw *) hdev->hidraw)->minor;
324 ret = u2fzero_init_led(dev, minor);
330 hid_info(hdev, "U2F Zero LED initialised\n");
332 ret = u2fzero_init_hwrng(dev, minor);
338 hid_info(hdev, "U2F Zero RNG initialised\n");
343 static void u2fzero_remove(struct hid_device *hdev)
345 struct u2fzero_device *dev = hid_get_drvdata(hdev);
347 mutex_lock(&dev->lock);
348 dev->present = false;
349 mutex_unlock(&dev->lock);
352 usb_poison_urb(dev->urb);
353 usb_free_urb(dev->urb);
356 static const struct hid_device_id u2fzero_table[] = {
357 { HID_USB_DEVICE(USB_VENDOR_ID_CYGNAL,
358 USB_DEVICE_ID_U2F_ZERO) },
361 MODULE_DEVICE_TABLE(hid, u2fzero_table);
363 static struct hid_driver u2fzero_driver = {
364 .name = "hid-" DRIVER_SHORT,
365 .probe = u2fzero_probe,
366 .remove = u2fzero_remove,
367 .id_table = u2fzero_table,
370 module_hid_driver(u2fzero_driver);
372 MODULE_LICENSE("GPL");
373 MODULE_AUTHOR("Andrej Shadura <andrew@shadura.me>");
374 MODULE_DESCRIPTION("U2F Zero LED and RNG driver");