blob: d99d424c05a7aac0133845ac4ae8147194981fd5 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * chaoskey - driver for ChaosKey device from Altus Metrum.
4 *
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
8 *
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
12 * bit stream.
13 *
14 * Copyright © 2015 Keith Packard <keithp@keithp.com>
15 */
16
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>
24
25static struct usb_driver chaoskey_driver;
26static struct usb_class_driver chaoskey_class;
27static int chaoskey_rng_read(struct hwrng *rng, void *data,
28 size_t max, bool wait);
29
30static DEFINE_MUTEX(chaoskey_list_lock);
31
32#define usb_dbg(usb_if, format, arg...) \
33 dev_dbg(&(usb_if)->dev, format, ## arg)
34
35#define usb_err(usb_if, format, arg...) \
36 dev_err(&(usb_if)->dev, format, ## arg)
37
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"
42
43MODULE_AUTHOR(DRIVER_AUTHOR);
44MODULE_DESCRIPTION(DRIVER_DESC);
45MODULE_LICENSE("GPL");
46
47#define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
48#define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
49
50#define ALEA_VENDOR_ID 0x12d8 /* Araneus */
51#define ALEA_PRODUCT_ID 0x0001 /* Alea I */
52
53#define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
54
55#define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */
56#define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */
57
58#ifdef CONFIG_USB_DYNAMIC_MINORS
59#define USB_CHAOSKEY_MINOR_BASE 0
60#else
61
62/* IOWARRIOR_MINOR_BASE + 16, not official yet */
63#define USB_CHAOSKEY_MINOR_BASE 224
64#endif
65
66static 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) },
69 { },
70};
71MODULE_DEVICE_TABLE(usb, chaoskey_table);
72
73static void chaos_read_callback(struct urb *urb);
74
75/* Driver-local specific stuff */
76struct chaoskey {
77 struct usb_interface *interface;
78 char in_ep;
79 struct mutex lock;
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 */
93 char *buf;
94};
95
96static void chaoskey_free(struct chaoskey *dev)
97{
98 if (dev) {
99 usb_dbg(dev->interface, "free");
100 usb_free_urb(dev->urb);
101 kfree(dev->name);
102 kfree(dev->buf);
103 usb_put_intf(dev->interface);
104 kfree(dev);
105 }
106}
107
108static int chaoskey_probe(struct usb_interface *interface,
109 const struct usb_device_id *id)
110{
111 struct usb_device *udev = interface_to_usbdev(interface);
112 struct usb_host_interface *altsetting = interface->cur_altsetting;
113 struct usb_endpoint_descriptor *epd;
114 int in_ep;
115 struct chaoskey *dev;
116 int result = -ENOMEM;
117 int size;
118 int res;
119
120 usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
121
122 /* Find the first bulk IN endpoint and its packet size */
123 res = usb_find_bulk_in_endpoint(altsetting, &epd);
124 if (res) {
125 usb_dbg(interface, "no IN endpoint found");
126 return res;
127 }
128
129 in_ep = usb_endpoint_num(epd);
130 size = usb_endpoint_maxp(epd);
131
132 /* Validate endpoint and size */
133 if (size <= 0) {
134 usb_dbg(interface, "invalid size (%d)", size);
135 return -ENODEV;
136 }
137
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;
142 }
143
144 /* Looks good, allocate and initialize */
145
146 dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
147
148 if (dev == NULL)
149 goto out;
150
151 dev->interface = usb_get_intf(interface);
152
153 dev->buf = kmalloc(size, GFP_KERNEL);
154
155 if (dev->buf == NULL)
156 goto out;
157
158 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
159
160 if (!dev->urb)
161 goto out;
162
163 usb_fill_bulk_urb(dev->urb,
164 udev,
165 usb_rcvbulkpipe(udev, in_ep),
166 dev->buf,
167 size,
168 chaos_read_callback,
169 dev);
170
171 /* Construct a name using the product and serial values. Each
172 * device needs a unique name for the hwrng code
173 */
174
175 if (udev->product && udev->serial) {
176 dev->name = kasprintf(GFP_KERNEL, "%s-%s", udev->product,
177 udev->serial);
178 if (dev->name == NULL)
179 goto out;
180 }
181
182 dev->in_ep = in_ep;
183
184 if (le16_to_cpu(udev->descriptor.idVendor) != ALEA_VENDOR_ID)
185 dev->reads_started = true;
186
187 dev->size = size;
188 dev->present = true;
189
190 init_waitqueue_head(&dev->wait_q);
191
192 mutex_init(&dev->lock);
193 mutex_init(&dev->rng_lock);
194
195 usb_set_intfdata(interface, dev);
196
197 result = usb_register_dev(interface, &chaoskey_class);
198 if (result) {
199 usb_err(interface, "Unable to allocate minor number.");
200 goto out;
201 }
202
203 dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
204 dev->hwrng.read = chaoskey_rng_read;
205 dev->hwrng.quality = 1024;
206
207 dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
208 if (!dev->hwrng_registered)
209 usb_err(interface, "Unable to register with hwrng");
210
211 usb_enable_autosuspend(udev);
212
213 usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
214 return 0;
215
216out:
217 usb_set_intfdata(interface, NULL);
218 chaoskey_free(dev);
219 return result;
220}
221
222static void chaoskey_disconnect(struct usb_interface *interface)
223{
224 struct chaoskey *dev;
225
226 usb_dbg(interface, "disconnect");
227 dev = usb_get_intfdata(interface);
228 if (!dev) {
229 usb_dbg(interface, "disconnect failed - no dev");
230 return;
231 }
232
233 if (dev->hwrng_registered)
234 hwrng_unregister(&dev->hwrng);
235
236 usb_deregister_dev(interface, &chaoskey_class);
237
238 usb_set_intfdata(interface, NULL);
239 mutex_lock(&chaoskey_list_lock);
240 mutex_lock(&dev->lock);
241
242 dev->present = false;
243 usb_poison_urb(dev->urb);
244
245 if (!dev->open) {
246 mutex_unlock(&dev->lock);
247 chaoskey_free(dev);
248 } else
249 mutex_unlock(&dev->lock);
250
251 mutex_unlock(&chaoskey_list_lock);
252 usb_dbg(interface, "disconnect done");
253}
254
255static int chaoskey_open(struct inode *inode, struct file *file)
256{
257 struct chaoskey *dev;
258 struct usb_interface *interface;
259 int rv = 0;
260
261 /* get the interface from minor number and driver information */
262 interface = usb_find_interface(&chaoskey_driver, iminor(inode));
263 if (!interface)
264 return -ENODEV;
265
266 usb_dbg(interface, "open");
267
268 dev = usb_get_intfdata(interface);
269 if (!dev) {
270 usb_dbg(interface, "open (dev)");
271 return -ENODEV;
272 }
273
274 file->private_data = dev;
275 mutex_lock(&chaoskey_list_lock);
276 mutex_lock(&dev->lock);
277 if (dev->present)
278 ++dev->open;
279 else
280 rv = -ENODEV;
281 mutex_unlock(&dev->lock);
282 mutex_unlock(&chaoskey_list_lock);
283
284 return rv;
285}
286
287static int chaoskey_release(struct inode *inode, struct file *file)
288{
289 struct chaoskey *dev = file->private_data;
290 struct usb_interface *interface;
291 int rv = 0;
292
293 if (dev == NULL)
294 return -ENODEV;
295
296 interface = dev->interface;
297
298 usb_dbg(interface, "release");
299
300 mutex_lock(&chaoskey_list_lock);
301 mutex_lock(&dev->lock);
302
303 usb_dbg(interface, "open count at release is %d", dev->open);
304
305 if (dev->open <= 0) {
306 usb_dbg(interface, "invalid open count (%d)", dev->open);
307 rv = -ENODEV;
308 goto bail;
309 }
310
311 --dev->open;
312
313 if (!dev->present) {
314 if (dev->open == 0) {
315 mutex_unlock(&dev->lock);
316 chaoskey_free(dev);
317 goto destruction;
318 }
319 }
320bail:
321 mutex_unlock(&dev->lock);
322destruction:
323 mutex_unlock(&chaoskey_list_lock);
324 usb_dbg(interface, "release success");
325 return rv;
326}
327
328static void chaos_read_callback(struct urb *urb)
329{
330 struct chaoskey *dev = urb->context;
331 int status = urb->status;
332
333 usb_dbg(dev->interface, "callback status (%d)", status);
334
335 if (status == 0)
336 dev->valid = urb->actual_length;
337 else
338 dev->valid = 0;
339
340 dev->used = 0;
341
342 /* must be seen first before validity is announced */
343 smp_wmb();
344
345 dev->reading = false;
346 wake_up(&dev->wait_q);
347}
348
349/* Fill the buffer. Called with dev->lock held
350 */
351static int _chaoskey_fill(struct chaoskey *dev)
352{
353 DEFINE_WAIT(wait);
354 int result;
355 bool started;
356
357 usb_dbg(dev->interface, "fill");
358
359 /* Return immediately if someone called before the buffer was
360 * empty */
361 if (dev->valid != dev->used) {
362 usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
363 dev->valid, dev->used);
364 return 0;
365 }
366
367 /* Bail if the device has been removed */
368 if (!dev->present) {
369 usb_dbg(dev->interface, "device not present");
370 return -ENODEV;
371 }
372
373 /* Make sure the device is awake */
374 result = usb_autopm_get_interface(dev->interface);
375 if (result) {
376 usb_dbg(dev->interface, "wakeup failed (result %d)", result);
377 return result;
378 }
379
380 dev->reading = true;
381 result = usb_submit_urb(dev->urb, GFP_KERNEL);
382 if (result < 0) {
383 result = usb_translate_errors(result);
384 dev->reading = false;
385 goto out;
386 }
387
388 /* The first read on the Alea takes a little under 2 seconds.
389 * Reads after the first read take only a few microseconds
390 * though. Presumably the entropy-generating circuit needs
391 * time to ramp up. So, we wait longer on the first read.
392 */
393 started = dev->reads_started;
394 dev->reads_started = true;
395 result = wait_event_interruptible_timeout(
396 dev->wait_q,
397 !dev->reading,
398 (started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) );
399
400 if (result < 0) {
401 usb_kill_urb(dev->urb);
402 goto out;
403 }
404
405 if (result == 0) {
406 result = -ETIMEDOUT;
407 usb_kill_urb(dev->urb);
408 } else {
409 result = dev->valid;
410 }
411out:
412 /* Let the device go back to sleep eventually */
413 usb_autopm_put_interface(dev->interface);
414
415 usb_dbg(dev->interface, "read %d bytes", dev->valid);
416
417 return result;
418}
419
420static ssize_t chaoskey_read(struct file *file,
421 char __user *buffer,
422 size_t count,
423 loff_t *ppos)
424{
425 struct chaoskey *dev;
426 ssize_t read_count = 0;
427 int this_time;
428 int result = 0;
429 unsigned long remain;
430
431 dev = file->private_data;
432
433 if (dev == NULL || !dev->present)
434 return -ENODEV;
435
436 usb_dbg(dev->interface, "read %zu", count);
437
438 while (count > 0) {
439
440 /* Grab the rng_lock briefly to ensure that the hwrng interface
441 * gets priority over other user access
442 */
443 result = mutex_lock_interruptible(&dev->rng_lock);
444 if (result)
445 goto bail;
446 mutex_unlock(&dev->rng_lock);
447
448 result = mutex_lock_interruptible(&dev->lock);
449 if (result)
450 goto bail;
451 if (dev->valid == dev->used) {
452 result = _chaoskey_fill(dev);
453 if (result < 0) {
454 mutex_unlock(&dev->lock);
455 goto bail;
456 }
457 }
458
459 this_time = dev->valid - dev->used;
460 if (this_time > count)
461 this_time = count;
462
463 remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
464 if (remain) {
465 result = -EFAULT;
466
467 /* Consume the bytes that were copied so we don't leak
468 * data to user space
469 */
470 dev->used += this_time - remain;
471 mutex_unlock(&dev->lock);
472 goto bail;
473 }
474
475 count -= this_time;
476 read_count += this_time;
477 buffer += this_time;
478 dev->used += this_time;
479 mutex_unlock(&dev->lock);
480 }
481bail:
482 if (read_count) {
483 usb_dbg(dev->interface, "read %zu bytes", read_count);
484 return read_count;
485 }
486 usb_dbg(dev->interface, "empty read, result %d", result);
487 if (result == -ETIMEDOUT)
488 result = -EAGAIN;
489 return result;
490}
491
492static int chaoskey_rng_read(struct hwrng *rng, void *data,
493 size_t max, bool wait)
494{
495 struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
496 int this_time;
497
498 usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
499
500 if (!dev->present) {
501 usb_dbg(dev->interface, "device not present");
502 return 0;
503 }
504
505 /* Hold the rng_lock until we acquire the device lock so that
506 * this operation gets priority over other user access to the
507 * device
508 */
509 mutex_lock(&dev->rng_lock);
510
511 mutex_lock(&dev->lock);
512
513 mutex_unlock(&dev->rng_lock);
514
515 /* Try to fill the buffer if empty. It doesn't actually matter
516 * if _chaoskey_fill works; we'll just return zero bytes as
517 * the buffer will still be empty
518 */
519 if (dev->valid == dev->used)
520 (void) _chaoskey_fill(dev);
521
522 this_time = dev->valid - dev->used;
523 if (this_time > max)
524 this_time = max;
525
526 memcpy(data, dev->buf + dev->used, this_time);
527
528 dev->used += this_time;
529
530 mutex_unlock(&dev->lock);
531
532 usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
533 return this_time;
534}
535
536#ifdef CONFIG_PM
537static int chaoskey_suspend(struct usb_interface *interface,
538 pm_message_t message)
539{
540 usb_dbg(interface, "suspend");
541 return 0;
542}
543
544static int chaoskey_resume(struct usb_interface *interface)
545{
546 struct chaoskey *dev;
547 struct usb_device *udev = interface_to_usbdev(interface);
548
549 usb_dbg(interface, "resume");
550 dev = usb_get_intfdata(interface);
551
552 /*
553 * We may have lost power.
554 * In that case the device that needs a long time
555 * for the first requests needs an extended timeout
556 * again
557 */
558 if (le16_to_cpu(udev->descriptor.idVendor) == ALEA_VENDOR_ID)
559 dev->reads_started = false;
560
561 return 0;
562}
563#else
564#define chaoskey_suspend NULL
565#define chaoskey_resume NULL
566#endif
567
568/* file operation pointers */
569static const struct file_operations chaoskey_fops = {
570 .owner = THIS_MODULE,
571 .read = chaoskey_read,
572 .open = chaoskey_open,
573 .release = chaoskey_release,
574 .llseek = default_llseek,
575};
576
577/* class driver information */
578static struct usb_class_driver chaoskey_class = {
579 .name = "chaoskey%d",
580 .fops = &chaoskey_fops,
581 .minor_base = USB_CHAOSKEY_MINOR_BASE,
582};
583
584/* usb specific object needed to register this driver with the usb subsystem */
585static struct usb_driver chaoskey_driver = {
586 .name = DRIVER_SHORT,
587 .probe = chaoskey_probe,
588 .disconnect = chaoskey_disconnect,
589 .suspend = chaoskey_suspend,
590 .resume = chaoskey_resume,
591 .reset_resume = chaoskey_resume,
592 .id_table = chaoskey_table,
593 .supports_autosuspend = 1,
594};
595
596module_usb_driver(chaoskey_driver);
597