chaoskey.c 14 KB

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