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