1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
56#define CHAOSKEY_PRODUCT_ID 0x60c6
57
58#define ALEA_VENDOR_ID 0x12d8
59#define ALEA_PRODUCT_ID 0x0001
60
61#define CHAOSKEY_BUF_LEN 64
62
63#define NAK_TIMEOUT (HZ)
64#define ALEA_FIRST_TIMEOUT (HZ*3)
65
66#ifdef CONFIG_USB_DYNAMIC_MINORS
67#define USB_CHAOSKEY_MINOR_BASE 0
68#else
69
70
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) },
76 { USB_DEVICE(ALEA_VENDOR_ID, ALEA_PRODUCT_ID) },
77 { },
78};
79MODULE_DEVICE_TABLE(usb, chaoskey_table);
80
81static void chaos_read_callback(struct urb *urb);
82
83
84struct chaoskey {
85 struct usb_interface *interface;
86 char in_ep;
87 struct mutex lock;
88 struct mutex rng_lock;
89 int open;
90 bool present;
91 bool reading;
92 bool reads_started;
93 int size;
94 int valid;
95 int used;
96 char *name;
97 struct hwrng hwrng;
98 int hwrng_registered;
99 wait_queue_head_t wait_q;
100 struct urb *urb;
101 char *buf;
102};
103
104static void chaoskey_free(struct chaoskey *dev)
105{
106 if (dev) {
107 usb_dbg(dev->interface, "free");
108 usb_free_urb(dev->urb);
109 kfree(dev->name);
110 kfree(dev->buf);
111 kfree(dev);
112 }
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;
120 int i;
121 int in_ep = -1;
122 struct chaoskey *dev;
123 int result = -ENOMEM;
124 int size;
125
126 usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
127
128
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);
133 break;
134 }
135 }
136
137
138 if (in_ep == -1) {
139 usb_dbg(interface, "no IN endpoint found");
140 return -ENODEV;
141 }
142 if (size <= 0) {
143 usb_dbg(interface, "invalid size (%d)", size);
144 return -ENODEV;
145 }
146
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;
151 }
152
153
154
155 dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
156
157 if (dev == NULL)
158 goto out;
159
160 dev->buf = kmalloc(size, GFP_KERNEL);
161
162 if (dev->buf == NULL)
163 goto out;
164
165 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
166
167 if (!dev->urb)
168 goto out;
169
170 usb_fill_bulk_urb(dev->urb,
171 udev,
172 usb_rcvbulkpipe(udev, in_ep),
173 dev->buf,
174 size,
175 chaos_read_callback,
176 dev);
177
178
179
180
181
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)
186 goto out;
187
188 strcpy(dev->name, udev->product);
189 strcat(dev->name, "-");
190 strcat(dev->name, udev->serial);
191 }
192
193 dev->interface = interface;
194
195 dev->in_ep = in_ep;
196
197 if (udev->descriptor.idVendor != ALEA_VENDOR_ID)
198 dev->reads_started = 1;
199
200 dev->size = size;
201 dev->present = 1;
202
203 init_waitqueue_head(&dev->wait_q);
204
205 mutex_init(&dev->lock);
206 mutex_init(&dev->rng_lock);
207
208 usb_set_intfdata(interface, dev);
209
210 result = usb_register_dev(interface, &chaoskey_class);
211 if (result) {
212 usb_err(interface, "Unable to allocate minor number.");
213 goto out;
214 }
215
216 dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
217 dev->hwrng.read = chaoskey_rng_read;
218
219
220
221
222
223
224
225
226
227
228
229
230 dev->hwrng.quality = 1024 + 1023;
231
232 dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
233 if (!dev->hwrng_registered)
234 usb_err(interface, "Unable to register with hwrng");
235
236 usb_enable_autosuspend(udev);
237
238 usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
239 return 0;
240
241out:
242 usb_set_intfdata(interface, NULL);
243 chaoskey_free(dev);
244 return result;
245}
246
247static void chaoskey_disconnect(struct usb_interface *interface)
248{
249 struct chaoskey *dev;
250
251 usb_dbg(interface, "disconnect");
252 dev = usb_get_intfdata(interface);
253 if (!dev) {
254 usb_dbg(interface, "disconnect failed - no dev");
255 return;
256 }
257
258 if (dev->hwrng_registered)
259 hwrng_unregister(&dev->hwrng);
260
261 usb_deregister_dev(interface, &chaoskey_class);
262
263 usb_set_intfdata(interface, NULL);
264 mutex_lock(&dev->lock);
265
266 dev->present = 0;
267 usb_poison_urb(dev->urb);
268
269 if (!dev->open) {
270 mutex_unlock(&dev->lock);
271 chaoskey_free(dev);
272 } else
273 mutex_unlock(&dev->lock);
274
275 usb_dbg(interface, "disconnect done");
276}
277
278static int chaoskey_open(struct inode *inode, struct file *file)
279{
280 struct chaoskey *dev;
281 struct usb_interface *interface;
282
283
284 interface = usb_find_interface(&chaoskey_driver, iminor(inode));
285 if (!interface)
286 return -ENODEV;
287
288 usb_dbg(interface, "open");
289
290 dev = usb_get_intfdata(interface);
291 if (!dev) {
292 usb_dbg(interface, "open (dev)");
293 return -ENODEV;
294 }
295
296 file->private_data = dev;
297 mutex_lock(&dev->lock);
298 ++dev->open;
299 mutex_unlock(&dev->lock);
300
301 usb_dbg(interface, "open success");
302 return 0;
303}
304
305static int chaoskey_release(struct inode *inode, struct file *file)
306{
307 struct chaoskey *dev = file->private_data;
308 struct usb_interface *interface;
309
310 if (dev == NULL)
311 return -ENODEV;
312
313 interface = dev->interface;
314
315 usb_dbg(interface, "release");
316
317 mutex_lock(&dev->lock);
318
319 usb_dbg(interface, "open count at release is %d", dev->open);
320
321 if (dev->open <= 0) {
322 usb_dbg(interface, "invalid open count (%d)", dev->open);
323 mutex_unlock(&dev->lock);
324 return -ENODEV;
325 }
326
327 --dev->open;
328
329 if (!dev->present) {
330 if (dev->open == 0) {
331 mutex_unlock(&dev->lock);
332 chaoskey_free(dev);
333 } else
334 mutex_unlock(&dev->lock);
335 } else
336 mutex_unlock(&dev->lock);
337
338 usb_dbg(interface, "release success");
339 return 0;
340}
341
342static void chaos_read_callback(struct urb *urb)
343{
344 struct chaoskey *dev = urb->context;
345 int status = urb->status;
346
347 usb_dbg(dev->interface, "callback status (%d)", status);
348
349 if (status == 0)
350 dev->valid = urb->actual_length;
351 else
352 dev->valid = 0;
353
354 dev->used = 0;
355
356
357 smp_wmb();
358
359 dev->reading = false;
360 wake_up(&dev->wait_q);
361}
362
363
364
365static int _chaoskey_fill(struct chaoskey *dev)
366{
367 DEFINE_WAIT(wait);
368 int result;
369 bool started;
370
371 usb_dbg(dev->interface, "fill");
372
373
374
375 if (dev->valid != dev->used) {
376 usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
377 dev->valid, dev->used);
378 return 0;
379 }
380
381
382 if (!dev->present) {
383 usb_dbg(dev->interface, "device not present");
384 return -ENODEV;
385 }
386
387
388 result = usb_autopm_get_interface(dev->interface);
389 if (result) {
390 usb_dbg(dev->interface, "wakeup failed (result %d)", result);
391 return result;
392 }
393
394 dev->reading = true;
395 result = usb_submit_urb(dev->urb, GFP_KERNEL);
396 if (result < 0) {
397 result = usb_translate_errors(result);
398 dev->reading = false;
399 goto out;
400 }
401
402
403
404
405
406
407 started = dev->reads_started;
408 dev->reads_started = true;
409 result = wait_event_interruptible_timeout(
410 dev->wait_q,
411 !dev->reading,
412 (started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) );
413
414 if (result < 0)
415 goto out;
416
417 if (result == 0)
418 result = -ETIMEDOUT;
419 else
420 result = dev->valid;
421out:
422
423 usb_autopm_put_interface(dev->interface);
424
425 usb_dbg(dev->interface, "read %d bytes", dev->valid);
426
427 return result;
428}
429
430static ssize_t chaoskey_read(struct file *file,
431 char __user *buffer,
432 size_t count,
433 loff_t *ppos)
434{
435 struct chaoskey *dev;
436 ssize_t read_count = 0;
437 int this_time;
438 int result = 0;
439 unsigned long remain;
440
441 dev = file->private_data;
442
443 if (dev == NULL || !dev->present)
444 return -ENODEV;
445
446 usb_dbg(dev->interface, "read %zu", count);
447
448 while (count > 0) {
449
450
451
452
453 result = mutex_lock_interruptible(&dev->rng_lock);
454 if (result)
455 goto bail;
456 mutex_unlock(&dev->rng_lock);
457
458 result = mutex_lock_interruptible(&dev->lock);
459 if (result)
460 goto bail;
461 if (dev->valid == dev->used) {
462 result = _chaoskey_fill(dev);
463 if (result < 0) {
464 mutex_unlock(&dev->lock);
465 goto bail;
466 }
467 }
468
469 this_time = dev->valid - dev->used;
470 if (this_time > count)
471 this_time = count;
472
473 remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
474 if (remain) {
475 result = -EFAULT;
476
477
478
479
480 dev->used += this_time - remain;
481 mutex_unlock(&dev->lock);
482 goto bail;
483 }
484
485 count -= this_time;
486 read_count += this_time;
487 buffer += this_time;
488 dev->used += this_time;
489 mutex_unlock(&dev->lock);
490 }
491bail:
492 if (read_count) {
493 usb_dbg(dev->interface, "read %zu bytes", read_count);
494 return read_count;
495 }
496 usb_dbg(dev->interface, "empty read, result %d", result);
497 if (result == -ETIMEDOUT)
498 result = -EAGAIN;
499 return result;
500}
501
502static int chaoskey_rng_read(struct hwrng *rng, void *data,
503 size_t max, bool wait)
504{
505 struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
506 int this_time;
507
508 usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
509
510 if (!dev->present) {
511 usb_dbg(dev->interface, "device not present");
512 return 0;
513 }
514
515
516
517
518
519 mutex_lock(&dev->rng_lock);
520
521 mutex_lock(&dev->lock);
522
523 mutex_unlock(&dev->rng_lock);
524
525
526
527
528
529 if (dev->valid == dev->used)
530 (void) _chaoskey_fill(dev);
531
532 this_time = dev->valid - dev->used;
533 if (this_time > max)
534 this_time = max;
535
536 memcpy(data, dev->buf + dev->used, this_time);
537
538 dev->used += this_time;
539
540 mutex_unlock(&dev->lock);
541
542 usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
543 return this_time;
544}
545
546#ifdef CONFIG_PM
547static int chaoskey_suspend(struct usb_interface *interface,
548 pm_message_t message)
549{
550 usb_dbg(interface, "suspend");
551 return 0;
552}
553
554static int chaoskey_resume(struct usb_interface *interface)
555{
556 usb_dbg(interface, "resume");
557 return 0;
558}
559#else
560#define chaoskey_suspend NULL
561#define chaoskey_resume NULL
562#endif
563
564
565static 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,
571};
572
573
574static struct usb_class_driver chaoskey_class = {
575 .name = "chaoskey%d",
576 .fops = &chaoskey_fops,
577 .minor_base = USB_CHAOSKEY_MINOR_BASE,
578};
579
580
581static 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,
590};
591
592module_usb_driver(chaoskey_driver);
593
594