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