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 #define usb_dbg(usb_if, format, arg...) \
31 dev_dbg(&(usb_if)->dev, format, ## arg)
33 #define usb_err(usb_if, format, arg...) \
34 dev_err(&(usb_if)->dev, format, ## arg)
36 /* Version Information */
37 #define DRIVER_AUTHOR "Keith Packard, keithp@keithp.com"
38 #define DRIVER_DESC "Altus Metrum ChaosKey driver"
39 #define DRIVER_SHORT "chaoskey"
41 MODULE_AUTHOR(DRIVER_AUTHOR
);
42 MODULE_DESCRIPTION(DRIVER_DESC
);
43 MODULE_LICENSE("GPL");
45 #define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
46 #define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
48 #define ALEA_VENDOR_ID 0x12d8 /* Araneus */
49 #define ALEA_PRODUCT_ID 0x0001 /* Alea I */
51 #define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
53 #define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */
54 #define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */
56 #ifdef CONFIG_USB_DYNAMIC_MINORS
57 #define USB_CHAOSKEY_MINOR_BASE 0
60 /* IOWARRIOR_MINOR_BASE + 16, not official yet */
61 #define USB_CHAOSKEY_MINOR_BASE 224
64 static const struct usb_device_id chaoskey_table
[] = {
65 { USB_DEVICE(CHAOSKEY_VENDOR_ID
, CHAOSKEY_PRODUCT_ID
) },
66 { USB_DEVICE(ALEA_VENDOR_ID
, ALEA_PRODUCT_ID
) },
69 MODULE_DEVICE_TABLE(usb
, chaoskey_table
);
71 static void chaos_read_callback(struct urb
*urb
);
73 /* Driver-local specific stuff */
75 struct usb_interface
*interface
;
78 struct mutex rng_lock
;
79 int open
; /* open count */
80 bool present
; /* device not disconnected */
81 bool reading
; /* ongoing IO */
82 bool reads_started
; /* track first read for Alea */
83 int size
; /* size of buf */
84 int valid
; /* bytes of buf read */
85 int used
; /* bytes of buf consumed */
86 char *name
; /* product + serial */
87 struct hwrng hwrng
; /* Embedded struct for hwrng */
88 int hwrng_registered
; /* registered with hwrng API */
89 wait_queue_head_t wait_q
; /* for timeouts */
90 struct urb
*urb
; /* for performing IO */
94 static void chaoskey_free(struct chaoskey
*dev
)
97 usb_dbg(dev
->interface
, "free");
98 usb_free_urb(dev
->urb
);
105 static int chaoskey_probe(struct usb_interface
*interface
,
106 const struct usb_device_id
*id
)
108 struct usb_device
*udev
= interface_to_usbdev(interface
);
109 struct usb_host_interface
*altsetting
= interface
->cur_altsetting
;
110 struct usb_endpoint_descriptor
*epd
;
112 struct chaoskey
*dev
;
113 int result
= -ENOMEM
;
117 usb_dbg(interface
, "probe %s-%s", udev
->product
, udev
->serial
);
119 /* Find the first bulk IN endpoint and its packet size */
120 res
= usb_find_bulk_in_endpoint(altsetting
, &epd
);
122 usb_dbg(interface
, "no IN endpoint found");
126 in_ep
= usb_endpoint_num(epd
);
127 size
= usb_endpoint_maxp(epd
);
129 /* Validate endpoint and size */
131 usb_dbg(interface
, "invalid size (%d)", size
);
135 if (size
> CHAOSKEY_BUF_LEN
) {
136 usb_dbg(interface
, "size reduced from %d to %d\n",
137 size
, CHAOSKEY_BUF_LEN
);
138 size
= CHAOSKEY_BUF_LEN
;
141 /* Looks good, allocate and initialize */
143 dev
= kzalloc(sizeof(struct chaoskey
), GFP_KERNEL
);
148 dev
->buf
= kmalloc(size
, GFP_KERNEL
);
150 if (dev
->buf
== NULL
)
153 dev
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
158 usb_fill_bulk_urb(dev
->urb
,
160 usb_rcvbulkpipe(udev
, in_ep
),
166 /* Construct a name using the product and serial values. Each
167 * device needs a unique name for the hwrng code
170 if (udev
->product
&& udev
->serial
) {
171 dev
->name
= kasprintf(GFP_KERNEL
, "%s-%s", udev
->product
,
173 if (dev
->name
== NULL
)
177 dev
->interface
= interface
;
181 if (le16_to_cpu(udev
->descriptor
.idVendor
) != ALEA_VENDOR_ID
)
182 dev
->reads_started
= true;
187 init_waitqueue_head(&dev
->wait_q
);
189 mutex_init(&dev
->lock
);
190 mutex_init(&dev
->rng_lock
);
192 usb_set_intfdata(interface
, dev
);
194 result
= usb_register_dev(interface
, &chaoskey_class
);
196 usb_err(interface
, "Unable to allocate minor number.");
200 dev
->hwrng
.name
= dev
->name
? dev
->name
: chaoskey_driver
.name
;
201 dev
->hwrng
.read
= chaoskey_rng_read
;
202 dev
->hwrng
.quality
= 1024;
204 dev
->hwrng_registered
= (hwrng_register(&dev
->hwrng
) == 0);
205 if (!dev
->hwrng_registered
)
206 usb_err(interface
, "Unable to register with hwrng");
208 usb_enable_autosuspend(udev
);
210 usb_dbg(interface
, "chaoskey probe success, size %d", dev
->size
);
214 usb_set_intfdata(interface
, NULL
);
219 static void chaoskey_disconnect(struct usb_interface
*interface
)
221 struct chaoskey
*dev
;
223 usb_dbg(interface
, "disconnect");
224 dev
= usb_get_intfdata(interface
);
226 usb_dbg(interface
, "disconnect failed - no dev");
230 if (dev
->hwrng_registered
)
231 hwrng_unregister(&dev
->hwrng
);
233 usb_deregister_dev(interface
, &chaoskey_class
);
235 usb_set_intfdata(interface
, NULL
);
236 mutex_lock(&dev
->lock
);
238 dev
->present
= false;
239 usb_poison_urb(dev
->urb
);
242 mutex_unlock(&dev
->lock
);
245 mutex_unlock(&dev
->lock
);
247 usb_dbg(interface
, "disconnect done");
250 static int chaoskey_open(struct inode
*inode
, struct file
*file
)
252 struct chaoskey
*dev
;
253 struct usb_interface
*interface
;
255 /* get the interface from minor number and driver information */
256 interface
= usb_find_interface(&chaoskey_driver
, iminor(inode
));
260 usb_dbg(interface
, "open");
262 dev
= usb_get_intfdata(interface
);
264 usb_dbg(interface
, "open (dev)");
268 file
->private_data
= dev
;
269 mutex_lock(&dev
->lock
);
271 mutex_unlock(&dev
->lock
);
273 usb_dbg(interface
, "open success");
277 static int chaoskey_release(struct inode
*inode
, struct file
*file
)
279 struct chaoskey
*dev
= file
->private_data
;
280 struct usb_interface
*interface
;
285 interface
= dev
->interface
;
287 usb_dbg(interface
, "release");
289 mutex_lock(&dev
->lock
);
291 usb_dbg(interface
, "open count at release is %d", dev
->open
);
293 if (dev
->open
<= 0) {
294 usb_dbg(interface
, "invalid open count (%d)", dev
->open
);
295 mutex_unlock(&dev
->lock
);
302 if (dev
->open
== 0) {
303 mutex_unlock(&dev
->lock
);
306 mutex_unlock(&dev
->lock
);
308 mutex_unlock(&dev
->lock
);
310 usb_dbg(interface
, "release success");
314 static void chaos_read_callback(struct urb
*urb
)
316 struct chaoskey
*dev
= urb
->context
;
317 int status
= urb
->status
;
319 usb_dbg(dev
->interface
, "callback status (%d)", status
);
322 dev
->valid
= urb
->actual_length
;
328 /* must be seen first before validity is announced */
331 dev
->reading
= false;
332 wake_up(&dev
->wait_q
);
335 /* Fill the buffer. Called with dev->lock held
337 static int _chaoskey_fill(struct chaoskey
*dev
)
343 usb_dbg(dev
->interface
, "fill");
345 /* Return immediately if someone called before the buffer was
347 if (dev
->valid
!= dev
->used
) {
348 usb_dbg(dev
->interface
, "not empty yet (valid %d used %d)",
349 dev
->valid
, dev
->used
);
353 /* Bail if the device has been removed */
355 usb_dbg(dev
->interface
, "device not present");
359 /* Make sure the device is awake */
360 result
= usb_autopm_get_interface(dev
->interface
);
362 usb_dbg(dev
->interface
, "wakeup failed (result %d)", result
);
367 result
= usb_submit_urb(dev
->urb
, GFP_KERNEL
);
369 result
= usb_translate_errors(result
);
370 dev
->reading
= false;
374 /* The first read on the Alea takes a little under 2 seconds.
375 * Reads after the first read take only a few microseconds
376 * though. Presumably the entropy-generating circuit needs
377 * time to ramp up. So, we wait longer on the first read.
379 started
= dev
->reads_started
;
380 dev
->reads_started
= true;
381 result
= wait_event_interruptible_timeout(
384 (started
? NAK_TIMEOUT
: ALEA_FIRST_TIMEOUT
) );
394 /* Let the device go back to sleep eventually */
395 usb_autopm_put_interface(dev
->interface
);
397 usb_dbg(dev
->interface
, "read %d bytes", dev
->valid
);
402 static ssize_t
chaoskey_read(struct file
*file
,
407 struct chaoskey
*dev
;
408 ssize_t read_count
= 0;
411 unsigned long remain
;
413 dev
= file
->private_data
;
415 if (dev
== NULL
|| !dev
->present
)
418 usb_dbg(dev
->interface
, "read %zu", count
);
422 /* Grab the rng_lock briefly to ensure that the hwrng interface
423 * gets priority over other user access
425 result
= mutex_lock_interruptible(&dev
->rng_lock
);
428 mutex_unlock(&dev
->rng_lock
);
430 result
= mutex_lock_interruptible(&dev
->lock
);
433 if (dev
->valid
== dev
->used
) {
434 result
= _chaoskey_fill(dev
);
436 mutex_unlock(&dev
->lock
);
441 this_time
= dev
->valid
- dev
->used
;
442 if (this_time
> count
)
445 remain
= copy_to_user(buffer
, dev
->buf
+ dev
->used
, this_time
);
449 /* Consume the bytes that were copied so we don't leak
452 dev
->used
+= this_time
- remain
;
453 mutex_unlock(&dev
->lock
);
458 read_count
+= this_time
;
460 dev
->used
+= this_time
;
461 mutex_unlock(&dev
->lock
);
465 usb_dbg(dev
->interface
, "read %zu bytes", read_count
);
468 usb_dbg(dev
->interface
, "empty read, result %d", result
);
469 if (result
== -ETIMEDOUT
)
474 static int chaoskey_rng_read(struct hwrng
*rng
, void *data
,
475 size_t max
, bool wait
)
477 struct chaoskey
*dev
= container_of(rng
, struct chaoskey
, hwrng
);
480 usb_dbg(dev
->interface
, "rng_read max %zu wait %d", max
, wait
);
483 usb_dbg(dev
->interface
, "device not present");
487 /* Hold the rng_lock until we acquire the device lock so that
488 * this operation gets priority over other user access to the
491 mutex_lock(&dev
->rng_lock
);
493 mutex_lock(&dev
->lock
);
495 mutex_unlock(&dev
->rng_lock
);
497 /* Try to fill the buffer if empty. It doesn't actually matter
498 * if _chaoskey_fill works; we'll just return zero bytes as
499 * the buffer will still be empty
501 if (dev
->valid
== dev
->used
)
502 (void) _chaoskey_fill(dev
);
504 this_time
= dev
->valid
- dev
->used
;
508 memcpy(data
, dev
->buf
+ dev
->used
, this_time
);
510 dev
->used
+= this_time
;
512 mutex_unlock(&dev
->lock
);
514 usb_dbg(dev
->interface
, "rng_read this_time %d\n", this_time
);
519 static int chaoskey_suspend(struct usb_interface
*interface
,
520 pm_message_t message
)
522 usb_dbg(interface
, "suspend");
526 static int chaoskey_resume(struct usb_interface
*interface
)
528 usb_dbg(interface
, "resume");
532 #define chaoskey_suspend NULL
533 #define chaoskey_resume NULL
536 /* file operation pointers */
537 static const struct file_operations chaoskey_fops
= {
538 .owner
= THIS_MODULE
,
539 .read
= chaoskey_read
,
540 .open
= chaoskey_open
,
541 .release
= chaoskey_release
,
542 .llseek
= default_llseek
,
545 /* class driver information */
546 static struct usb_class_driver chaoskey_class
= {
547 .name
= "chaoskey%d",
548 .fops
= &chaoskey_fops
,
549 .minor_base
= USB_CHAOSKEY_MINOR_BASE
,
552 /* usb specific object needed to register this driver with the usb subsystem */
553 static struct usb_driver chaoskey_driver
= {
554 .name
= DRIVER_SHORT
,
555 .probe
= chaoskey_probe
,
556 .disconnect
= chaoskey_disconnect
,
557 .suspend
= chaoskey_suspend
,
558 .resume
= chaoskey_resume
,
559 .reset_resume
= chaoskey_resume
,
560 .id_table
= chaoskey_table
,
561 .supports_autosuspend
= 1,
564 module_usb_driver(chaoskey_driver
);