]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/usb/misc/chaoskey.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-kernel.git] / drivers / usb / misc / chaoskey.c
CommitLineData
66e3e591
KP
1/*
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>
8b86ed07
KP
30#include <linux/mutex.h>
31#include <linux/uaccess.h>
66e3e591
KP
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_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"
49
50MODULE_VERSION(DRIVER_VERSION);
51MODULE_AUTHOR(DRIVER_AUTHOR);
52MODULE_DESCRIPTION(DRIVER_DESC);
53MODULE_LICENSE("GPL");
54
55#define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
56#define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
57
d3ede2db
BH
58#define ALEA_VENDOR_ID 0x12d8 /* Araneus */
59#define ALEA_PRODUCT_ID 0x0001 /* Alea I */
60
66e3e591
KP
61#define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
62
e4a886e8
BH
63#define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */
64#define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */
66e3e591
KP
65
66#ifdef CONFIG_USB_DYNAMIC_MINORS
67#define USB_CHAOSKEY_MINOR_BASE 0
68#else
69
70/* IOWARRIOR_MINOR_BASE + 16, not official yet */
71#define USB_CHAOSKEY_MINOR_BASE 224
72#endif
73
74static const struct usb_device_id chaoskey_table[] = {
75 { USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
d3ede2db 76 { USB_DEVICE(ALEA_VENDOR_ID, ALEA_PRODUCT_ID) },
66e3e591
KP
77 { },
78};
79MODULE_DEVICE_TABLE(usb, chaoskey_table);
80
0ca10122
ON
81static void chaos_read_callback(struct urb *urb);
82
66e3e591
KP
83/* Driver-local specific stuff */
84struct chaoskey {
85 struct usb_interface *interface;
86 char in_ep;
87 struct mutex lock;
88 struct mutex rng_lock;
89 int open; /* open count */
0ca10122
ON
90 bool present; /* device not disconnected */
91 bool reading; /* ongoing IO */
e4a886e8 92 bool reads_started; /* track first read for Alea */
66e3e591
KP
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 */
0ca10122 100 struct urb *urb; /* for performing IO */
66e3e591
KP
101 char *buf;
102};
103
104static void chaoskey_free(struct chaoskey *dev)
105{
0a15e24c
ON
106 if (dev) {
107 usb_dbg(dev->interface, "free");
0ca10122 108 usb_free_urb(dev->urb);
0a15e24c
ON
109 kfree(dev->name);
110 kfree(dev->buf);
111 kfree(dev);
112 }
66e3e591
KP
113}
114
115static int chaoskey_probe(struct usb_interface *interface,
116 const struct usb_device_id *id)
117{
118 struct usb_device *udev = interface_to_usbdev(interface);
119 struct usb_host_interface *altsetting = interface->cur_altsetting;
a8bbb0f8
JH
120 struct usb_endpoint_descriptor *epd;
121 int in_ep;
66e3e591 122 struct chaoskey *dev;
0a15e24c 123 int result = -ENOMEM;
66e3e591 124 int size;
a8bbb0f8 125 int res;
66e3e591
KP
126
127 usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
128
129 /* Find the first bulk IN endpoint and its packet size */
a8bbb0f8
JH
130 res = usb_find_bulk_in_endpoint(altsetting, &epd);
131 if (res) {
132 usb_dbg(interface, "no IN endpoint found");
133 return res;
66e3e591
KP
134 }
135
a8bbb0f8
JH
136 in_ep = usb_endpoint_num(epd);
137 size = usb_endpoint_maxp(epd);
138
66e3e591 139 /* Validate endpoint and size */
66e3e591
KP
140 if (size <= 0) {
141 usb_dbg(interface, "invalid size (%d)", size);
142 return -ENODEV;
143 }
144
145 if (size > CHAOSKEY_BUF_LEN) {
146 usb_dbg(interface, "size reduced from %d to %d\n",
147 size, CHAOSKEY_BUF_LEN);
148 size = CHAOSKEY_BUF_LEN;
149 }
150
151 /* Looks good, allocate and initialize */
152
153 dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
154
155 if (dev == NULL)
0a15e24c 156 goto out;
66e3e591
KP
157
158 dev->buf = kmalloc(size, GFP_KERNEL);
159
0a15e24c
ON
160 if (dev->buf == NULL)
161 goto out;
66e3e591 162
0ca10122
ON
163 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
164
165 if (!dev->urb)
166 goto out;
167
168 usb_fill_bulk_urb(dev->urb,
169 udev,
170 usb_rcvbulkpipe(udev, in_ep),
171 dev->buf,
172 size,
173 chaos_read_callback,
174 dev);
175
66e3e591
KP
176 /* Construct a name using the product and serial values. Each
177 * device needs a unique name for the hwrng code
178 */
179
180 if (udev->product && udev->serial) {
181 dev->name = kmalloc(strlen(udev->product) + 1 +
182 strlen(udev->serial) + 1, GFP_KERNEL);
0a15e24c
ON
183 if (dev->name == NULL)
184 goto out;
66e3e591
KP
185
186 strcpy(dev->name, udev->product);
187 strcat(dev->name, "-");
188 strcat(dev->name, udev->serial);
189 }
190
191 dev->interface = interface;
192
193 dev->in_ep = in_ep;
194
63afd5cc 195 if (le16_to_cpu(udev->descriptor.idVendor) != ALEA_VENDOR_ID)
e4a886e8
BH
196 dev->reads_started = 1;
197
66e3e591
KP
198 dev->size = size;
199 dev->present = 1;
200
201 init_waitqueue_head(&dev->wait_q);
202
203 mutex_init(&dev->lock);
204 mutex_init(&dev->rng_lock);
205
206 usb_set_intfdata(interface, dev);
207
208 result = usb_register_dev(interface, &chaoskey_class);
209 if (result) {
210 usb_err(interface, "Unable to allocate minor number.");
0a15e24c 211 goto out;
66e3e591
KP
212 }
213
214 dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
215 dev->hwrng.read = chaoskey_rng_read;
6fbbcf38 216 dev->hwrng.quality = 1024;
66e3e591
KP
217
218 dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
219 if (!dev->hwrng_registered)
220 usb_err(interface, "Unable to register with hwrng");
221
222 usb_enable_autosuspend(udev);
223
224 usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
225 return 0;
0a15e24c
ON
226
227out:
228 usb_set_intfdata(interface, NULL);
229 chaoskey_free(dev);
230 return result;
66e3e591
KP
231}
232
233static void chaoskey_disconnect(struct usb_interface *interface)
234{
235 struct chaoskey *dev;
236
237 usb_dbg(interface, "disconnect");
238 dev = usb_get_intfdata(interface);
239 if (!dev) {
240 usb_dbg(interface, "disconnect failed - no dev");
241 return;
242 }
243
244 if (dev->hwrng_registered)
245 hwrng_unregister(&dev->hwrng);
246
247 usb_deregister_dev(interface, &chaoskey_class);
248
249 usb_set_intfdata(interface, NULL);
250 mutex_lock(&dev->lock);
251
252 dev->present = 0;
0ca10122 253 usb_poison_urb(dev->urb);
66e3e591
KP
254
255 if (!dev->open) {
256 mutex_unlock(&dev->lock);
257 chaoskey_free(dev);
258 } else
259 mutex_unlock(&dev->lock);
260
261 usb_dbg(interface, "disconnect done");
262}
263
264static int chaoskey_open(struct inode *inode, struct file *file)
265{
266 struct chaoskey *dev;
267 struct usb_interface *interface;
268
269 /* get the interface from minor number and driver information */
270 interface = usb_find_interface(&chaoskey_driver, iminor(inode));
271 if (!interface)
272 return -ENODEV;
273
274 usb_dbg(interface, "open");
275
276 dev = usb_get_intfdata(interface);
277 if (!dev) {
278 usb_dbg(interface, "open (dev)");
279 return -ENODEV;
280 }
281
282 file->private_data = dev;
283 mutex_lock(&dev->lock);
284 ++dev->open;
285 mutex_unlock(&dev->lock);
286
287 usb_dbg(interface, "open success");
288 return 0;
289}
290
291static int chaoskey_release(struct inode *inode, struct file *file)
292{
293 struct chaoskey *dev = file->private_data;
294 struct usb_interface *interface;
295
296 if (dev == NULL)
297 return -ENODEV;
298
299 interface = dev->interface;
300
301 usb_dbg(interface, "release");
302
303 mutex_lock(&dev->lock);
304
305 usb_dbg(interface, "open count at release is %d", dev->open);
306
307 if (dev->open <= 0) {
308 usb_dbg(interface, "invalid open count (%d)", dev->open);
309 mutex_unlock(&dev->lock);
310 return -ENODEV;
311 }
312
313 --dev->open;
314
315 if (!dev->present) {
316 if (dev->open == 0) {
317 mutex_unlock(&dev->lock);
318 chaoskey_free(dev);
319 } else
320 mutex_unlock(&dev->lock);
321 } else
322 mutex_unlock(&dev->lock);
323
324 usb_dbg(interface, "release success");
325 return 0;
326}
327
0ca10122
ON
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
66e3e591
KP
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;
e4a886e8 355 bool started;
66e3e591
KP
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
0ca10122
ON
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
e4a886e8
BH
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;
0ca10122
ON
395 result = wait_event_interruptible_timeout(
396 dev->wait_q,
397 !dev->reading,
e4a886e8 398 (started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) );
66e3e591 399
0ca10122
ON
400 if (result < 0)
401 goto out;
402
403 if (result == 0)
404 result = -ETIMEDOUT;
405 else
406 result = dev->valid;
407out:
66e3e591
KP
408 /* Let the device go back to sleep eventually */
409 usb_autopm_put_interface(dev->interface);
410
0ca10122 411 usb_dbg(dev->interface, "read %d bytes", dev->valid);
66e3e591
KP
412
413 return result;
414}
415
416static ssize_t chaoskey_read(struct file *file,
417 char __user *buffer,
418 size_t count,
419 loff_t *ppos)
420{
421 struct chaoskey *dev;
422 ssize_t read_count = 0;
423 int this_time;
424 int result = 0;
425 unsigned long remain;
426
427 dev = file->private_data;
428
429 if (dev == NULL || !dev->present)
430 return -ENODEV;
431
432 usb_dbg(dev->interface, "read %zu", count);
433
434 while (count > 0) {
435
436 /* Grab the rng_lock briefly to ensure that the hwrng interface
437 * gets priority over other user access
438 */
439 result = mutex_lock_interruptible(&dev->rng_lock);
440 if (result)
441 goto bail;
442 mutex_unlock(&dev->rng_lock);
443
444 result = mutex_lock_interruptible(&dev->lock);
445 if (result)
446 goto bail;
447 if (dev->valid == dev->used) {
448 result = _chaoskey_fill(dev);
0ca10122 449 if (result < 0) {
66e3e591
KP
450 mutex_unlock(&dev->lock);
451 goto bail;
452 }
453 }
454
455 this_time = dev->valid - dev->used;
456 if (this_time > count)
457 this_time = count;
458
459 remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
460 if (remain) {
461 result = -EFAULT;
462
463 /* Consume the bytes that were copied so we don't leak
464 * data to user space
465 */
466 dev->used += this_time - remain;
467 mutex_unlock(&dev->lock);
468 goto bail;
469 }
470
471 count -= this_time;
472 read_count += this_time;
473 buffer += this_time;
474 dev->used += this_time;
475 mutex_unlock(&dev->lock);
476 }
477bail:
478 if (read_count) {
479 usb_dbg(dev->interface, "read %zu bytes", read_count);
480 return read_count;
481 }
482 usb_dbg(dev->interface, "empty read, result %d", result);
0ca10122
ON
483 if (result == -ETIMEDOUT)
484 result = -EAGAIN;
66e3e591
KP
485 return result;
486}
487
488static int chaoskey_rng_read(struct hwrng *rng, void *data,
489 size_t max, bool wait)
490{
491 struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
492 int this_time;
493
494 usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
495
496 if (!dev->present) {
497 usb_dbg(dev->interface, "device not present");
498 return 0;
499 }
500
501 /* Hold the rng_lock until we acquire the device lock so that
502 * this operation gets priority over other user access to the
503 * device
504 */
505 mutex_lock(&dev->rng_lock);
506
507 mutex_lock(&dev->lock);
508
509 mutex_unlock(&dev->rng_lock);
510
511 /* Try to fill the buffer if empty. It doesn't actually matter
512 * if _chaoskey_fill works; we'll just return zero bytes as
513 * the buffer will still be empty
514 */
515 if (dev->valid == dev->used)
516 (void) _chaoskey_fill(dev);
517
518 this_time = dev->valid - dev->used;
519 if (this_time > max)
520 this_time = max;
521
1d5c47f5 522 memcpy(data, dev->buf + dev->used, this_time);
66e3e591
KP
523
524 dev->used += this_time;
525
526 mutex_unlock(&dev->lock);
527
528 usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
529 return this_time;
530}
531
532#ifdef CONFIG_PM
533static int chaoskey_suspend(struct usb_interface *interface,
534 pm_message_t message)
535{
536 usb_dbg(interface, "suspend");
537 return 0;
538}
539
540static int chaoskey_resume(struct usb_interface *interface)
541{
542 usb_dbg(interface, "resume");
543 return 0;
544}
545#else
546#define chaoskey_suspend NULL
547#define chaoskey_resume NULL
548#endif
549
550/* file operation pointers */
551static const struct file_operations chaoskey_fops = {
552 .owner = THIS_MODULE,
553 .read = chaoskey_read,
554 .open = chaoskey_open,
555 .release = chaoskey_release,
556 .llseek = default_llseek,
557};
558
559/* class driver information */
560static struct usb_class_driver chaoskey_class = {
561 .name = "chaoskey%d",
562 .fops = &chaoskey_fops,
563 .minor_base = USB_CHAOSKEY_MINOR_BASE,
564};
565
566/* usb specific object needed to register this driver with the usb subsystem */
567static struct usb_driver chaoskey_driver = {
568 .name = DRIVER_SHORT,
569 .probe = chaoskey_probe,
570 .disconnect = chaoskey_disconnect,
571 .suspend = chaoskey_suspend,
572 .resume = chaoskey_resume,
573 .reset_resume = chaoskey_resume,
574 .id_table = chaoskey_table,
575 .supports_autosuspend = 1,
576};
577
578module_usb_driver(chaoskey_driver);
579