2 * chaoskey - driver for ChaosKey device from Altus Metrum.
4 * This device provides true random numbers using a noise source based
5 * on a reverse-biased p-n junction in avalanche breakdown. More
6 * details can be found at http://chaoskey.org
8 * The driver connects to the kernel hardware RNG interface to provide
9 * entropy for /dev/random and other kernel activities. It also offers
10 * a separate /dev/ entry to allow for direct access to the random
13 * Copyright © 2015 Keith Packard <keithp@keithp.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; version 2 of the License.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/usb.h>
28 #include <linux/wait.h>
29 #include <linux/hw_random.h>
30 #include <linux/mutex.h>
31 #include <linux/uaccess.h>
33 static struct usb_driver chaoskey_driver
;
34 static struct usb_class_driver chaoskey_class
;
35 static int chaoskey_rng_read(struct hwrng
*rng
, void *data
,
36 size_t max
, bool wait
);
38 #define usb_dbg(usb_if, format, arg...) \
39 dev_dbg(&(usb_if)->dev, format, ## arg)
41 #define usb_err(usb_if, format, arg...) \
42 dev_err(&(usb_if)->dev, format, ## arg)
44 /* Version Information */
45 #define DRIVER_VERSION "v0.1"
46 #define DRIVER_AUTHOR "Keith Packard, keithp@keithp.com"
47 #define DRIVER_DESC "Altus Metrum ChaosKey driver"
48 #define DRIVER_SHORT "chaoskey"
50 MODULE_VERSION(DRIVER_VERSION
);
51 MODULE_AUTHOR(DRIVER_AUTHOR
);
52 MODULE_DESCRIPTION(DRIVER_DESC
);
53 MODULE_LICENSE("GPL");
55 #define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
56 #define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
58 #define ALEA_VENDOR_ID 0x12d8 /* Araneus */
59 #define ALEA_PRODUCT_ID 0x0001 /* Alea I */
61 #define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
63 #define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */
64 #define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */
66 #ifdef CONFIG_USB_DYNAMIC_MINORS
67 #define USB_CHAOSKEY_MINOR_BASE 0
70 /* IOWARRIOR_MINOR_BASE + 16, not official yet */
71 #define USB_CHAOSKEY_MINOR_BASE 224
74 static const struct usb_device_id chaoskey_table
[] = {
75 { USB_DEVICE(CHAOSKEY_VENDOR_ID
, CHAOSKEY_PRODUCT_ID
) },
76 { USB_DEVICE(ALEA_VENDOR_ID
, ALEA_PRODUCT_ID
) },
79 MODULE_DEVICE_TABLE(usb
, chaoskey_table
);
81 static void chaos_read_callback(struct urb
*urb
);
83 /* Driver-local specific stuff */
85 struct usb_interface
*interface
;
88 struct mutex rng_lock
;
89 int open
; /* open count */
90 bool present
; /* device not disconnected */
91 bool reading
; /* ongoing IO */
92 bool reads_started
; /* track first read for Alea */
93 int size
; /* size of buf */
94 int valid
; /* bytes of buf read */
95 int used
; /* bytes of buf consumed */
96 char *name
; /* product + serial */
97 struct hwrng hwrng
; /* Embedded struct for hwrng */
98 int hwrng_registered
; /* registered with hwrng API */
99 wait_queue_head_t wait_q
; /* for timeouts */
100 struct urb
*urb
; /* for performing IO */
104 static void chaoskey_free(struct chaoskey
*dev
)
107 usb_dbg(dev
->interface
, "free");
108 usb_free_urb(dev
->urb
);
115 static int chaoskey_probe(struct usb_interface
*interface
,
116 const struct usb_device_id
*id
)
118 struct usb_device
*udev
= interface_to_usbdev(interface
);
119 struct usb_host_interface
*altsetting
= interface
->cur_altsetting
;
122 struct chaoskey
*dev
;
123 int result
= -ENOMEM
;
126 usb_dbg(interface
, "probe %s-%s", udev
->product
, udev
->serial
);
128 /* Find the first bulk IN endpoint and its packet size */
129 for (i
= 0; i
< altsetting
->desc
.bNumEndpoints
; i
++) {
130 if (usb_endpoint_is_bulk_in(&altsetting
->endpoint
[i
].desc
)) {
131 in_ep
= usb_endpoint_num(&altsetting
->endpoint
[i
].desc
);
132 size
= usb_endpoint_maxp(&altsetting
->endpoint
[i
].desc
);
137 /* Validate endpoint and size */
139 usb_dbg(interface
, "no IN endpoint found");
143 usb_dbg(interface
, "invalid size (%d)", size
);
147 if (size
> CHAOSKEY_BUF_LEN
) {
148 usb_dbg(interface
, "size reduced from %d to %d\n",
149 size
, CHAOSKEY_BUF_LEN
);
150 size
= CHAOSKEY_BUF_LEN
;
153 /* Looks good, allocate and initialize */
155 dev
= kzalloc(sizeof(struct chaoskey
), GFP_KERNEL
);
160 dev
->buf
= kmalloc(size
, GFP_KERNEL
);
162 if (dev
->buf
== NULL
)
165 dev
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
170 usb_fill_bulk_urb(dev
->urb
,
172 usb_rcvbulkpipe(udev
, in_ep
),
178 /* Construct a name using the product and serial values. Each
179 * device needs a unique name for the hwrng code
182 if (udev
->product
&& udev
->serial
) {
183 dev
->name
= kmalloc(strlen(udev
->product
) + 1 +
184 strlen(udev
->serial
) + 1, GFP_KERNEL
);
185 if (dev
->name
== NULL
)
188 strcpy(dev
->name
, udev
->product
);
189 strcat(dev
->name
, "-");
190 strcat(dev
->name
, udev
->serial
);
193 dev
->interface
= interface
;
197 if (udev
->descriptor
.idVendor
!= ALEA_VENDOR_ID
)
198 dev
->reads_started
= 1;
203 init_waitqueue_head(&dev
->wait_q
);
205 mutex_init(&dev
->lock
);
206 mutex_init(&dev
->rng_lock
);
208 usb_set_intfdata(interface
, dev
);
210 result
= usb_register_dev(interface
, &chaoskey_class
);
212 usb_err(interface
, "Unable to allocate minor number.");
216 dev
->hwrng
.name
= dev
->name
? dev
->name
: chaoskey_driver
.name
;
217 dev
->hwrng
.read
= chaoskey_rng_read
;
219 /* Set the 'quality' metric. Quality is measured in units of
220 * 1/1024's of a bit ("mills"). This should be set to 1024,
221 * but there is a bug in the hwrng core which masks it with
224 * The patch that has been merged to the crypto development
225 * tree for that bug limits the value to 1024 at most, so by
226 * setting this to 1024 + 1023, we get 1023 before the fix is
227 * merged and 1024 afterwards. We'll patch this driver once
228 * both bits of code are in the same tree.
230 dev
->hwrng
.quality
= 1024 + 1023;
232 dev
->hwrng_registered
= (hwrng_register(&dev
->hwrng
) == 0);
233 if (!dev
->hwrng_registered
)
234 usb_err(interface
, "Unable to register with hwrng");
236 usb_enable_autosuspend(udev
);
238 usb_dbg(interface
, "chaoskey probe success, size %d", dev
->size
);
242 usb_set_intfdata(interface
, NULL
);
247 static void chaoskey_disconnect(struct usb_interface
*interface
)
249 struct chaoskey
*dev
;
251 usb_dbg(interface
, "disconnect");
252 dev
= usb_get_intfdata(interface
);
254 usb_dbg(interface
, "disconnect failed - no dev");
258 if (dev
->hwrng_registered
)
259 hwrng_unregister(&dev
->hwrng
);
261 usb_deregister_dev(interface
, &chaoskey_class
);
263 usb_set_intfdata(interface
, NULL
);
264 mutex_lock(&dev
->lock
);
267 usb_poison_urb(dev
->urb
);
270 mutex_unlock(&dev
->lock
);
273 mutex_unlock(&dev
->lock
);
275 usb_dbg(interface
, "disconnect done");
278 static int chaoskey_open(struct inode
*inode
, struct file
*file
)
280 struct chaoskey
*dev
;
281 struct usb_interface
*interface
;
283 /* get the interface from minor number and driver information */
284 interface
= usb_find_interface(&chaoskey_driver
, iminor(inode
));
288 usb_dbg(interface
, "open");
290 dev
= usb_get_intfdata(interface
);
292 usb_dbg(interface
, "open (dev)");
296 file
->private_data
= dev
;
297 mutex_lock(&dev
->lock
);
299 mutex_unlock(&dev
->lock
);
301 usb_dbg(interface
, "open success");
305 static int chaoskey_release(struct inode
*inode
, struct file
*file
)
307 struct chaoskey
*dev
= file
->private_data
;
308 struct usb_interface
*interface
;
313 interface
= dev
->interface
;
315 usb_dbg(interface
, "release");
317 mutex_lock(&dev
->lock
);
319 usb_dbg(interface
, "open count at release is %d", dev
->open
);
321 if (dev
->open
<= 0) {
322 usb_dbg(interface
, "invalid open count (%d)", dev
->open
);
323 mutex_unlock(&dev
->lock
);
330 if (dev
->open
== 0) {
331 mutex_unlock(&dev
->lock
);
334 mutex_unlock(&dev
->lock
);
336 mutex_unlock(&dev
->lock
);
338 usb_dbg(interface
, "release success");
342 static void chaos_read_callback(struct urb
*urb
)
344 struct chaoskey
*dev
= urb
->context
;
345 int status
= urb
->status
;
347 usb_dbg(dev
->interface
, "callback status (%d)", status
);
350 dev
->valid
= urb
->actual_length
;
356 /* must be seen first before validity is announced */
359 dev
->reading
= false;
360 wake_up(&dev
->wait_q
);
363 /* Fill the buffer. Called with dev->lock held
365 static int _chaoskey_fill(struct chaoskey
*dev
)
371 usb_dbg(dev
->interface
, "fill");
373 /* Return immediately if someone called before the buffer was
375 if (dev
->valid
!= dev
->used
) {
376 usb_dbg(dev
->interface
, "not empty yet (valid %d used %d)",
377 dev
->valid
, dev
->used
);
381 /* Bail if the device has been removed */
383 usb_dbg(dev
->interface
, "device not present");
387 /* Make sure the device is awake */
388 result
= usb_autopm_get_interface(dev
->interface
);
390 usb_dbg(dev
->interface
, "wakeup failed (result %d)", result
);
395 result
= usb_submit_urb(dev
->urb
, GFP_KERNEL
);
397 result
= usb_translate_errors(result
);
398 dev
->reading
= false;
402 /* The first read on the Alea takes a little under 2 seconds.
403 * Reads after the first read take only a few microseconds
404 * though. Presumably the entropy-generating circuit needs
405 * time to ramp up. So, we wait longer on the first read.
407 started
= dev
->reads_started
;
408 dev
->reads_started
= true;
409 result
= wait_event_interruptible_timeout(
412 (started
? NAK_TIMEOUT
: ALEA_FIRST_TIMEOUT
) );
422 /* Let the device go back to sleep eventually */
423 usb_autopm_put_interface(dev
->interface
);
425 usb_dbg(dev
->interface
, "read %d bytes", dev
->valid
);
430 static ssize_t
chaoskey_read(struct file
*file
,
435 struct chaoskey
*dev
;
436 ssize_t read_count
= 0;
439 unsigned long remain
;
441 dev
= file
->private_data
;
443 if (dev
== NULL
|| !dev
->present
)
446 usb_dbg(dev
->interface
, "read %zu", count
);
450 /* Grab the rng_lock briefly to ensure that the hwrng interface
451 * gets priority over other user access
453 result
= mutex_lock_interruptible(&dev
->rng_lock
);
456 mutex_unlock(&dev
->rng_lock
);
458 result
= mutex_lock_interruptible(&dev
->lock
);
461 if (dev
->valid
== dev
->used
) {
462 result
= _chaoskey_fill(dev
);
464 mutex_unlock(&dev
->lock
);
469 this_time
= dev
->valid
- dev
->used
;
470 if (this_time
> count
)
473 remain
= copy_to_user(buffer
, dev
->buf
+ dev
->used
, this_time
);
477 /* Consume the bytes that were copied so we don't leak
480 dev
->used
+= this_time
- remain
;
481 mutex_unlock(&dev
->lock
);
486 read_count
+= this_time
;
488 dev
->used
+= this_time
;
489 mutex_unlock(&dev
->lock
);
493 usb_dbg(dev
->interface
, "read %zu bytes", read_count
);
496 usb_dbg(dev
->interface
, "empty read, result %d", result
);
497 if (result
== -ETIMEDOUT
)
502 static int chaoskey_rng_read(struct hwrng
*rng
, void *data
,
503 size_t max
, bool wait
)
505 struct chaoskey
*dev
= container_of(rng
, struct chaoskey
, hwrng
);
508 usb_dbg(dev
->interface
, "rng_read max %zu wait %d", max
, wait
);
511 usb_dbg(dev
->interface
, "device not present");
515 /* Hold the rng_lock until we acquire the device lock so that
516 * this operation gets priority over other user access to the
519 mutex_lock(&dev
->rng_lock
);
521 mutex_lock(&dev
->lock
);
523 mutex_unlock(&dev
->rng_lock
);
525 /* Try to fill the buffer if empty. It doesn't actually matter
526 * if _chaoskey_fill works; we'll just return zero bytes as
527 * the buffer will still be empty
529 if (dev
->valid
== dev
->used
)
530 (void) _chaoskey_fill(dev
);
532 this_time
= dev
->valid
- dev
->used
;
536 memcpy(data
, dev
->buf
+ dev
->used
, this_time
);
538 dev
->used
+= this_time
;
540 mutex_unlock(&dev
->lock
);
542 usb_dbg(dev
->interface
, "rng_read this_time %d\n", this_time
);
547 static int chaoskey_suspend(struct usb_interface
*interface
,
548 pm_message_t message
)
550 usb_dbg(interface
, "suspend");
554 static int chaoskey_resume(struct usb_interface
*interface
)
556 usb_dbg(interface
, "resume");
560 #define chaoskey_suspend NULL
561 #define chaoskey_resume NULL
564 /* file operation pointers */
565 static const struct file_operations chaoskey_fops
= {
566 .owner
= THIS_MODULE
,
567 .read
= chaoskey_read
,
568 .open
= chaoskey_open
,
569 .release
= chaoskey_release
,
570 .llseek
= default_llseek
,
573 /* class driver information */
574 static struct usb_class_driver chaoskey_class
= {
575 .name
= "chaoskey%d",
576 .fops
= &chaoskey_fops
,
577 .minor_base
= USB_CHAOSKEY_MINOR_BASE
,
580 /* usb specific object needed to register this driver with the usb subsystem */
581 static struct usb_driver chaoskey_driver
= {
582 .name
= DRIVER_SHORT
,
583 .probe
= chaoskey_probe
,
584 .disconnect
= chaoskey_disconnect
,
585 .suspend
= chaoskey_suspend
,
586 .resume
= chaoskey_resume
,
587 .reset_resume
= chaoskey_resume
,
588 .id_table
= chaoskey_table
,
589 .supports_autosuspend
= 1,
592 module_usb_driver(chaoskey_driver
);