chaoskey.c 13 KB

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