1 // 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 * This program is free software; you can redistribute it and/or modify 17 * it under the terms of the GNU General Public License as published by 18 * the Free Software Foundation; version 2 of the License. 19 * 20 * This program is distributed in the hope that it will be useful, but 21 * WITHOUT ANY WARRANTY; without even the implied warranty of 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 * General Public License for more details. 24 */ 25 26 #include <linux/module.h> 27 #include <linux/slab.h> 28 #include <linux/usb.h> 29 #include <linux/wait.h> 30 #include <linux/hw_random.h> 31 #include <linux/mutex.h> 32 #include <linux/uaccess.h> 33 34 static struct usb_driver chaoskey_driver; 35 static struct usb_class_driver chaoskey_class; 36 static int chaoskey_rng_read(struct hwrng *rng, void *data, 37 size_t max, bool wait); 38 39 #define usb_dbg(usb_if, format, arg...) \ 40 dev_dbg(&(usb_if)->dev, format, ## arg) 41 42 #define usb_err(usb_if, format, arg...) \ 43 dev_err(&(usb_if)->dev, format, ## arg) 44 45 /* Version Information */ 46 #define DRIVER_AUTHOR "Keith Packard, keithp@keithp.com" 47 #define DRIVER_DESC "Altus Metrum ChaosKey driver" 48 #define DRIVER_SHORT "chaoskey" 49 50 MODULE_AUTHOR(DRIVER_AUTHOR); 51 MODULE_DESCRIPTION(DRIVER_DESC); 52 MODULE_LICENSE("GPL"); 53 54 #define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */ 55 #define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */ 56 57 #define ALEA_VENDOR_ID 0x12d8 /* Araneus */ 58 #define ALEA_PRODUCT_ID 0x0001 /* Alea I */ 59 60 #define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */ 61 62 #define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */ 63 #define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */ 64 65 #ifdef CONFIG_USB_DYNAMIC_MINORS 66 #define USB_CHAOSKEY_MINOR_BASE 0 67 #else 68 69 /* IOWARRIOR_MINOR_BASE + 16, not official yet */ 70 #define USB_CHAOSKEY_MINOR_BASE 224 71 #endif 72 73 static const struct usb_device_id chaoskey_table[] = { 74 { USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) }, 75 { USB_DEVICE(ALEA_VENDOR_ID, ALEA_PRODUCT_ID) }, 76 { }, 77 }; 78 MODULE_DEVICE_TABLE(usb, chaoskey_table); 79 80 static void chaos_read_callback(struct urb *urb); 81 82 /* Driver-local specific stuff */ 83 struct chaoskey { 84 struct usb_interface *interface; 85 char in_ep; 86 struct mutex lock; 87 struct mutex rng_lock; 88 int open; /* open count */ 89 bool present; /* device not disconnected */ 90 bool reading; /* ongoing IO */ 91 bool reads_started; /* track first read for Alea */ 92 int size; /* size of buf */ 93 int valid; /* bytes of buf read */ 94 int used; /* bytes of buf consumed */ 95 char *name; /* product + serial */ 96 struct hwrng hwrng; /* Embedded struct for hwrng */ 97 int hwrng_registered; /* registered with hwrng API */ 98 wait_queue_head_t wait_q; /* for timeouts */ 99 struct urb *urb; /* for performing IO */ 100 char *buf; 101 }; 102 103 static void chaoskey_free(struct chaoskey *dev) 104 { 105 if (dev) { 106 usb_dbg(dev->interface, "free"); 107 usb_free_urb(dev->urb); 108 kfree(dev->name); 109 kfree(dev->buf); 110 kfree(dev); 111 } 112 } 113 114 static 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->buf = kmalloc(size, GFP_KERNEL); 158 159 if (dev->buf == NULL) 160 goto out; 161 162 dev->urb = usb_alloc_urb(0, GFP_KERNEL); 163 164 if (!dev->urb) 165 goto out; 166 167 usb_fill_bulk_urb(dev->urb, 168 udev, 169 usb_rcvbulkpipe(udev, in_ep), 170 dev->buf, 171 size, 172 chaos_read_callback, 173 dev); 174 175 /* Construct a name using the product and serial values. Each 176 * device needs a unique name for the hwrng code 177 */ 178 179 if (udev->product && udev->serial) { 180 dev->name = kmalloc(strlen(udev->product) + 1 + 181 strlen(udev->serial) + 1, GFP_KERNEL); 182 if (dev->name == NULL) 183 goto out; 184 185 strcpy(dev->name, udev->product); 186 strcat(dev->name, "-"); 187 strcat(dev->name, udev->serial); 188 } 189 190 dev->interface = interface; 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 226 out: 227 usb_set_intfdata(interface, NULL); 228 chaoskey_free(dev); 229 return result; 230 } 231 232 static 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 263 static 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 290 static 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 327 static 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 */ 350 static 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 goto out; 401 402 if (result == 0) 403 result = -ETIMEDOUT; 404 else 405 result = dev->valid; 406 out: 407 /* Let the device go back to sleep eventually */ 408 usb_autopm_put_interface(dev->interface); 409 410 usb_dbg(dev->interface, "read %d bytes", dev->valid); 411 412 return result; 413 } 414 415 static ssize_t chaoskey_read(struct file *file, 416 char __user *buffer, 417 size_t count, 418 loff_t *ppos) 419 { 420 struct chaoskey *dev; 421 ssize_t read_count = 0; 422 int this_time; 423 int result = 0; 424 unsigned long remain; 425 426 dev = file->private_data; 427 428 if (dev == NULL || !dev->present) 429 return -ENODEV; 430 431 usb_dbg(dev->interface, "read %zu", count); 432 433 while (count > 0) { 434 435 /* Grab the rng_lock briefly to ensure that the hwrng interface 436 * gets priority over other user access 437 */ 438 result = mutex_lock_interruptible(&dev->rng_lock); 439 if (result) 440 goto bail; 441 mutex_unlock(&dev->rng_lock); 442 443 result = mutex_lock_interruptible(&dev->lock); 444 if (result) 445 goto bail; 446 if (dev->valid == dev->used) { 447 result = _chaoskey_fill(dev); 448 if (result < 0) { 449 mutex_unlock(&dev->lock); 450 goto bail; 451 } 452 } 453 454 this_time = dev->valid - dev->used; 455 if (this_time > count) 456 this_time = count; 457 458 remain = copy_to_user(buffer, dev->buf + dev->used, this_time); 459 if (remain) { 460 result = -EFAULT; 461 462 /* Consume the bytes that were copied so we don't leak 463 * data to user space 464 */ 465 dev->used += this_time - remain; 466 mutex_unlock(&dev->lock); 467 goto bail; 468 } 469 470 count -= this_time; 471 read_count += this_time; 472 buffer += this_time; 473 dev->used += this_time; 474 mutex_unlock(&dev->lock); 475 } 476 bail: 477 if (read_count) { 478 usb_dbg(dev->interface, "read %zu bytes", read_count); 479 return read_count; 480 } 481 usb_dbg(dev->interface, "empty read, result %d", result); 482 if (result == -ETIMEDOUT) 483 result = -EAGAIN; 484 return result; 485 } 486 487 static int chaoskey_rng_read(struct hwrng *rng, void *data, 488 size_t max, bool wait) 489 { 490 struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng); 491 int this_time; 492 493 usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait); 494 495 if (!dev->present) { 496 usb_dbg(dev->interface, "device not present"); 497 return 0; 498 } 499 500 /* Hold the rng_lock until we acquire the device lock so that 501 * this operation gets priority over other user access to the 502 * device 503 */ 504 mutex_lock(&dev->rng_lock); 505 506 mutex_lock(&dev->lock); 507 508 mutex_unlock(&dev->rng_lock); 509 510 /* Try to fill the buffer if empty. It doesn't actually matter 511 * if _chaoskey_fill works; we'll just return zero bytes as 512 * the buffer will still be empty 513 */ 514 if (dev->valid == dev->used) 515 (void) _chaoskey_fill(dev); 516 517 this_time = dev->valid - dev->used; 518 if (this_time > max) 519 this_time = max; 520 521 memcpy(data, dev->buf + dev->used, this_time); 522 523 dev->used += this_time; 524 525 mutex_unlock(&dev->lock); 526 527 usb_dbg(dev->interface, "rng_read this_time %d\n", this_time); 528 return this_time; 529 } 530 531 #ifdef CONFIG_PM 532 static int chaoskey_suspend(struct usb_interface *interface, 533 pm_message_t message) 534 { 535 usb_dbg(interface, "suspend"); 536 return 0; 537 } 538 539 static int chaoskey_resume(struct usb_interface *interface) 540 { 541 usb_dbg(interface, "resume"); 542 return 0; 543 } 544 #else 545 #define chaoskey_suspend NULL 546 #define chaoskey_resume NULL 547 #endif 548 549 /* file operation pointers */ 550 static const struct file_operations chaoskey_fops = { 551 .owner = THIS_MODULE, 552 .read = chaoskey_read, 553 .open = chaoskey_open, 554 .release = chaoskey_release, 555 .llseek = default_llseek, 556 }; 557 558 /* class driver information */ 559 static struct usb_class_driver chaoskey_class = { 560 .name = "chaoskey%d", 561 .fops = &chaoskey_fops, 562 .minor_base = USB_CHAOSKEY_MINOR_BASE, 563 }; 564 565 /* usb specific object needed to register this driver with the usb subsystem */ 566 static struct usb_driver chaoskey_driver = { 567 .name = DRIVER_SHORT, 568 .probe = chaoskey_probe, 569 .disconnect = chaoskey_disconnect, 570 .suspend = chaoskey_suspend, 571 .resume = chaoskey_resume, 572 .reset_resume = chaoskey_resume, 573 .id_table = chaoskey_table, 574 .supports_autosuspend = 1, 575 }; 576 577 module_usb_driver(chaoskey_driver); 578 579