chaoskey.c 14 KB

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