1 // SPDX-License-Identifier: GPL-2.0
3 * chaoskey - driver for ChaosKey device from Altus Metrum.
5 * This device provides true random numbers using a noise source based
6 * on a reverse-biased p-n junction in avalanche breakdown. More
7 * details can be found at http://chaoskey.org
9 * The driver connects to the kernel hardware RNG interface to provide
10 * entropy for /dev/random and other kernel activities. It also offers
11 * a separate /dev/ entry to allow for direct access to the random
14 * Copyright © 2015 Keith Packard <keithp@keithp.com>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/usb.h>
20 #include <linux/wait.h>
21 #include <linux/hw_random.h>
22 #include <linux/mutex.h>
23 #include <linux/uaccess.h>
25 static struct usb_driver chaoskey_driver
;
26 static struct usb_class_driver chaoskey_class
;
27 static int chaoskey_rng_read(struct hwrng
*rng
, void *data
,
28 size_t max
, bool wait
);
30 static DEFINE_MUTEX(chaoskey_list_lock
);
32 #define usb_dbg(usb_if, format, arg...) \
33 dev_dbg(&(usb_if)->dev, format, ## arg)
35 #define usb_err(usb_if, format, arg...) \
36 dev_err(&(usb_if)->dev, format, ## arg)
38 /* Version Information */
39 #define DRIVER_AUTHOR "Keith Packard, keithp@keithp.com"
40 #define DRIVER_DESC "Altus Metrum ChaosKey driver"
41 #define DRIVER_SHORT "chaoskey"
43 MODULE_AUTHOR(DRIVER_AUTHOR
);
44 MODULE_DESCRIPTION(DRIVER_DESC
);
45 MODULE_LICENSE("GPL");
47 #define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
48 #define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
50 #define ALEA_VENDOR_ID 0x12d8 /* Araneus */
51 #define ALEA_PRODUCT_ID 0x0001 /* Alea I */
53 #define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
55 #define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */
56 #define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */
58 #ifdef CONFIG_USB_DYNAMIC_MINORS
59 #define USB_CHAOSKEY_MINOR_BASE 0
62 /* IOWARRIOR_MINOR_BASE + 16, not official yet */
63 #define USB_CHAOSKEY_MINOR_BASE 224
66 static const struct usb_device_id chaoskey_table
[] = {
67 { USB_DEVICE(CHAOSKEY_VENDOR_ID
, CHAOSKEY_PRODUCT_ID
) },
68 { USB_DEVICE(ALEA_VENDOR_ID
, ALEA_PRODUCT_ID
) },
71 MODULE_DEVICE_TABLE(usb
, chaoskey_table
);
73 static void chaos_read_callback(struct urb
*urb
);
75 /* Driver-local specific stuff */
77 struct usb_interface
*interface
;
80 struct mutex rng_lock
;
81 int open
; /* open count */
82 bool present
; /* device not disconnected */
83 bool reading
; /* ongoing IO */
84 bool reads_started
; /* track first read for Alea */
85 int size
; /* size of buf */
86 int valid
; /* bytes of buf read */
87 int used
; /* bytes of buf consumed */
88 char *name
; /* product + serial */
89 struct hwrng hwrng
; /* Embedded struct for hwrng */
90 int hwrng_registered
; /* registered with hwrng API */
91 wait_queue_head_t wait_q
; /* for timeouts */
92 struct urb
*urb
; /* for performing IO */
96 static void chaoskey_free(struct chaoskey
*dev
)
99 usb_dbg(dev
->interface
, "free");
100 usb_free_urb(dev
->urb
);
103 usb_put_intf(dev
->interface
);
108 static int chaoskey_probe(struct usb_interface
*interface
,
109 const struct usb_device_id
*id
)
111 struct usb_device
*udev
= interface_to_usbdev(interface
);
112 struct usb_host_interface
*altsetting
= interface
->cur_altsetting
;
113 struct usb_endpoint_descriptor
*epd
;
115 struct chaoskey
*dev
;
116 int result
= -ENOMEM
;
120 usb_dbg(interface
, "probe %s-%s", udev
->product
, udev
->serial
);
122 /* Find the first bulk IN endpoint and its packet size */
123 res
= usb_find_bulk_in_endpoint(altsetting
, &epd
);
125 usb_dbg(interface
, "no IN endpoint found");
129 in_ep
= usb_endpoint_num(epd
);
130 size
= usb_endpoint_maxp(epd
);
132 /* Validate endpoint and size */
134 usb_dbg(interface
, "invalid size (%d)", size
);
138 if (size
> CHAOSKEY_BUF_LEN
) {
139 usb_dbg(interface
, "size reduced from %d to %d\n",
140 size
, CHAOSKEY_BUF_LEN
);
141 size
= CHAOSKEY_BUF_LEN
;
144 /* Looks good, allocate and initialize */
146 dev
= kzalloc(sizeof(struct chaoskey
), GFP_KERNEL
);
151 dev
->interface
= usb_get_intf(interface
);
153 dev
->buf
= kmalloc(size
, GFP_KERNEL
);
155 if (dev
->buf
== NULL
)
158 dev
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
163 usb_fill_bulk_urb(dev
->urb
,
165 usb_rcvbulkpipe(udev
, in_ep
),
171 /* Construct a name using the product and serial values. Each
172 * device needs a unique name for the hwrng code
175 if (udev
->product
&& udev
->serial
) {
176 dev
->name
= kasprintf(GFP_KERNEL
, "%s-%s", udev
->product
,
178 if (dev
->name
== NULL
)
184 if (le16_to_cpu(udev
->descriptor
.idVendor
) != ALEA_VENDOR_ID
)
185 dev
->reads_started
= true;
190 init_waitqueue_head(&dev
->wait_q
);
192 mutex_init(&dev
->lock
);
193 mutex_init(&dev
->rng_lock
);
195 usb_set_intfdata(interface
, dev
);
197 result
= usb_register_dev(interface
, &chaoskey_class
);
199 usb_err(interface
, "Unable to allocate minor number.");
203 dev
->hwrng
.name
= dev
->name
? dev
->name
: chaoskey_driver
.name
;
204 dev
->hwrng
.read
= chaoskey_rng_read
;
206 dev
->hwrng_registered
= (hwrng_register(&dev
->hwrng
) == 0);
207 if (!dev
->hwrng_registered
)
208 usb_err(interface
, "Unable to register with hwrng");
210 usb_enable_autosuspend(udev
);
212 usb_dbg(interface
, "chaoskey probe success, size %d", dev
->size
);
216 usb_set_intfdata(interface
, NULL
);
221 static void chaoskey_disconnect(struct usb_interface
*interface
)
223 struct chaoskey
*dev
;
225 usb_dbg(interface
, "disconnect");
226 dev
= usb_get_intfdata(interface
);
228 usb_dbg(interface
, "disconnect failed - no dev");
232 if (dev
->hwrng_registered
)
233 hwrng_unregister(&dev
->hwrng
);
235 usb_deregister_dev(interface
, &chaoskey_class
);
237 usb_set_intfdata(interface
, NULL
);
238 mutex_lock(&chaoskey_list_lock
);
239 mutex_lock(&dev
->lock
);
241 dev
->present
= false;
242 usb_poison_urb(dev
->urb
);
245 mutex_unlock(&dev
->lock
);
248 mutex_unlock(&dev
->lock
);
250 mutex_unlock(&chaoskey_list_lock
);
251 usb_dbg(interface
, "disconnect done");
254 static int chaoskey_open(struct inode
*inode
, struct file
*file
)
256 struct chaoskey
*dev
;
257 struct usb_interface
*interface
;
260 /* get the interface from minor number and driver information */
261 interface
= usb_find_interface(&chaoskey_driver
, iminor(inode
));
265 usb_dbg(interface
, "open");
267 dev
= usb_get_intfdata(interface
);
269 usb_dbg(interface
, "open (dev)");
273 file
->private_data
= dev
;
274 mutex_lock(&chaoskey_list_lock
);
275 mutex_lock(&dev
->lock
);
280 mutex_unlock(&dev
->lock
);
281 mutex_unlock(&chaoskey_list_lock
);
286 static int chaoskey_release(struct inode
*inode
, struct file
*file
)
288 struct chaoskey
*dev
= file
->private_data
;
289 struct usb_interface
*interface
;
295 interface
= dev
->interface
;
297 usb_dbg(interface
, "release");
299 mutex_lock(&chaoskey_list_lock
);
300 mutex_lock(&dev
->lock
);
302 usb_dbg(interface
, "open count at release is %d", dev
->open
);
304 if (dev
->open
<= 0) {
305 usb_dbg(interface
, "invalid open count (%d)", dev
->open
);
313 if (dev
->open
== 0) {
314 mutex_unlock(&dev
->lock
);
320 mutex_unlock(&dev
->lock
);
322 mutex_unlock(&chaoskey_list_lock
);
323 usb_dbg(interface
, "release success");
327 static void chaos_read_callback(struct urb
*urb
)
329 struct chaoskey
*dev
= urb
->context
;
330 int status
= urb
->status
;
332 usb_dbg(dev
->interface
, "callback status (%d)", status
);
335 dev
->valid
= urb
->actual_length
;
341 /* must be seen first before validity is announced */
344 dev
->reading
= false;
345 wake_up(&dev
->wait_q
);
348 /* Fill the buffer. Called with dev->lock held
350 static int _chaoskey_fill(struct chaoskey
*dev
)
356 usb_dbg(dev
->interface
, "fill");
358 /* Return immediately if someone called before the buffer was
360 if (dev
->valid
!= dev
->used
) {
361 usb_dbg(dev
->interface
, "not empty yet (valid %d used %d)",
362 dev
->valid
, dev
->used
);
366 /* Bail if the device has been removed */
368 usb_dbg(dev
->interface
, "device not present");
372 /* Make sure the device is awake */
373 result
= usb_autopm_get_interface(dev
->interface
);
375 usb_dbg(dev
->interface
, "wakeup failed (result %d)", result
);
380 result
= usb_submit_urb(dev
->urb
, GFP_KERNEL
);
382 result
= usb_translate_errors(result
);
383 dev
->reading
= false;
387 /* The first read on the Alea takes a little under 2 seconds.
388 * Reads after the first read take only a few microseconds
389 * though. Presumably the entropy-generating circuit needs
390 * time to ramp up. So, we wait longer on the first read.
392 started
= dev
->reads_started
;
393 dev
->reads_started
= true;
394 result
= wait_event_interruptible_timeout(
397 (started
? NAK_TIMEOUT
: ALEA_FIRST_TIMEOUT
) );
400 usb_kill_urb(dev
->urb
);
406 usb_kill_urb(dev
->urb
);
411 /* Let the device go back to sleep eventually */
412 usb_autopm_put_interface(dev
->interface
);
414 usb_dbg(dev
->interface
, "read %d bytes", dev
->valid
);
419 static ssize_t
chaoskey_read(struct file
*file
,
424 struct chaoskey
*dev
;
425 ssize_t read_count
= 0;
428 unsigned long remain
;
430 dev
= file
->private_data
;
432 if (dev
== NULL
|| !dev
->present
)
435 usb_dbg(dev
->interface
, "read %zu", count
);
439 /* Grab the rng_lock briefly to ensure that the hwrng interface
440 * gets priority over other user access
442 result
= mutex_lock_interruptible(&dev
->rng_lock
);
445 mutex_unlock(&dev
->rng_lock
);
447 result
= mutex_lock_interruptible(&dev
->lock
);
450 if (dev
->valid
== dev
->used
) {
451 result
= _chaoskey_fill(dev
);
453 mutex_unlock(&dev
->lock
);
458 this_time
= dev
->valid
- dev
->used
;
459 if (this_time
> count
)
462 remain
= copy_to_user(buffer
, dev
->buf
+ dev
->used
, this_time
);
466 /* Consume the bytes that were copied so we don't leak
469 dev
->used
+= this_time
- remain
;
470 mutex_unlock(&dev
->lock
);
475 read_count
+= this_time
;
477 dev
->used
+= this_time
;
478 mutex_unlock(&dev
->lock
);
482 usb_dbg(dev
->interface
, "read %zu bytes", read_count
);
485 usb_dbg(dev
->interface
, "empty read, result %d", result
);
486 if (result
== -ETIMEDOUT
)
491 static int chaoskey_rng_read(struct hwrng
*rng
, void *data
,
492 size_t max
, bool wait
)
494 struct chaoskey
*dev
= container_of(rng
, struct chaoskey
, hwrng
);
497 usb_dbg(dev
->interface
, "rng_read max %zu wait %d", max
, wait
);
500 usb_dbg(dev
->interface
, "device not present");
504 /* Hold the rng_lock until we acquire the device lock so that
505 * this operation gets priority over other user access to the
508 mutex_lock(&dev
->rng_lock
);
510 mutex_lock(&dev
->lock
);
512 mutex_unlock(&dev
->rng_lock
);
514 /* Try to fill the buffer if empty. It doesn't actually matter
515 * if _chaoskey_fill works; we'll just return zero bytes as
516 * the buffer will still be empty
518 if (dev
->valid
== dev
->used
)
519 (void) _chaoskey_fill(dev
);
521 this_time
= dev
->valid
- dev
->used
;
525 memcpy(data
, dev
->buf
+ dev
->used
, this_time
);
527 dev
->used
+= this_time
;
529 mutex_unlock(&dev
->lock
);
531 usb_dbg(dev
->interface
, "rng_read this_time %d\n", this_time
);
536 static int chaoskey_suspend(struct usb_interface
*interface
,
537 pm_message_t message
)
539 usb_dbg(interface
, "suspend");
543 static int chaoskey_resume(struct usb_interface
*interface
)
545 struct chaoskey
*dev
;
546 struct usb_device
*udev
= interface_to_usbdev(interface
);
548 usb_dbg(interface
, "resume");
549 dev
= usb_get_intfdata(interface
);
552 * We may have lost power.
553 * In that case the device that needs a long time
554 * for the first requests needs an extended timeout
557 if (le16_to_cpu(udev
->descriptor
.idVendor
) == ALEA_VENDOR_ID
)
558 dev
->reads_started
= false;
563 #define chaoskey_suspend NULL
564 #define chaoskey_resume NULL
567 /* file operation pointers */
568 static const struct file_operations chaoskey_fops
= {
569 .owner
= THIS_MODULE
,
570 .read
= chaoskey_read
,
571 .open
= chaoskey_open
,
572 .release
= chaoskey_release
,
573 .llseek
= default_llseek
,
576 /* class driver information */
577 static struct usb_class_driver chaoskey_class
= {
578 .name
= "chaoskey%d",
579 .fops
= &chaoskey_fops
,
580 .minor_base
= USB_CHAOSKEY_MINOR_BASE
,
583 /* usb specific object needed to register this driver with the usb subsystem */
584 static struct usb_driver chaoskey_driver
= {
585 .name
= DRIVER_SHORT
,
586 .probe
= chaoskey_probe
,
587 .disconnect
= chaoskey_disconnect
,
588 .suspend
= chaoskey_suspend
,
589 .resume
= chaoskey_resume
,
590 .reset_resume
= chaoskey_resume
,
591 .id_table
= chaoskey_table
,
592 .supports_autosuspend
= 1,
595 module_usb_driver(chaoskey_driver
);