chaoskey.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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_VERSION "v0.1"
  41. #define DRIVER_AUTHOR "Keith Packard, keithp@keithp.com"
  42. #define DRIVER_DESC "Altus Metrum ChaosKey driver"
  43. #define DRIVER_SHORT "chaoskey"
  44. MODULE_VERSION(DRIVER_VERSION);
  45. MODULE_AUTHOR(DRIVER_AUTHOR);
  46. MODULE_DESCRIPTION(DRIVER_DESC);
  47. MODULE_LICENSE("GPL");
  48. #define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
  49. #define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
  50. #define ALEA_VENDOR_ID 0x12d8 /* Araneus */
  51. #define ALEA_PRODUCT_ID 0x0001 /* Alea I */
  52. #define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
  53. #define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */
  54. #define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */
  55. #ifdef CONFIG_USB_DYNAMIC_MINORS
  56. #define USB_CHAOSKEY_MINOR_BASE 0
  57. #else
  58. /* IOWARRIOR_MINOR_BASE + 16, not official yet */
  59. #define USB_CHAOSKEY_MINOR_BASE 224
  60. #endif
  61. static const struct usb_device_id chaoskey_table[] = {
  62. { USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
  63. { USB_DEVICE(ALEA_VENDOR_ID, ALEA_PRODUCT_ID) },
  64. { },
  65. };
  66. MODULE_DEVICE_TABLE(usb, chaoskey_table);
  67. static void chaos_read_callback(struct urb *urb);
  68. /* Driver-local specific stuff */
  69. struct chaoskey {
  70. struct usb_interface *interface;
  71. char in_ep;
  72. struct mutex lock;
  73. struct mutex rng_lock;
  74. int open; /* open count */
  75. bool present; /* device not disconnected */
  76. bool reading; /* ongoing IO */
  77. bool reads_started; /* track first read for Alea */
  78. int size; /* size of buf */
  79. int valid; /* bytes of buf read */
  80. int used; /* bytes of buf consumed */
  81. char *name; /* product + serial */
  82. struct hwrng hwrng; /* Embedded struct for hwrng */
  83. int hwrng_registered; /* registered with hwrng API */
  84. wait_queue_head_t wait_q; /* for timeouts */
  85. struct urb *urb; /* for performing IO */
  86. char *buf;
  87. };
  88. static void chaoskey_free(struct chaoskey *dev)
  89. {
  90. if (dev) {
  91. usb_dbg(dev->interface, "free");
  92. usb_free_urb(dev->urb);
  93. kfree(dev->name);
  94. kfree(dev->buf);
  95. kfree(dev);
  96. }
  97. }
  98. static int chaoskey_probe(struct usb_interface *interface,
  99. const struct usb_device_id *id)
  100. {
  101. struct usb_device *udev = interface_to_usbdev(interface);
  102. struct usb_host_interface *altsetting = interface->cur_altsetting;
  103. struct usb_endpoint_descriptor *epd;
  104. int in_ep;
  105. struct chaoskey *dev;
  106. int result = -ENOMEM;
  107. int size;
  108. int res;
  109. usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
  110. /* Find the first bulk IN endpoint and its packet size */
  111. res = usb_find_bulk_in_endpoint(altsetting, &epd);
  112. if (res) {
  113. usb_dbg(interface, "no IN endpoint found");
  114. return res;
  115. }
  116. in_ep = usb_endpoint_num(epd);
  117. size = usb_endpoint_maxp(epd);
  118. /* Validate endpoint and size */
  119. if (size <= 0) {
  120. usb_dbg(interface, "invalid size (%d)", size);
  121. return -ENODEV;
  122. }
  123. if (size > CHAOSKEY_BUF_LEN) {
  124. usb_dbg(interface, "size reduced from %d to %d\n",
  125. size, CHAOSKEY_BUF_LEN);
  126. size = CHAOSKEY_BUF_LEN;
  127. }
  128. /* Looks good, allocate and initialize */
  129. dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
  130. if (dev == NULL)
  131. goto out;
  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->interface = interface;
  158. dev->in_ep = in_ep;
  159. if (le16_to_cpu(udev->descriptor.idVendor) != ALEA_VENDOR_ID)
  160. dev->reads_started = 1;
  161. dev->size = size;
  162. dev->present = 1;
  163. init_waitqueue_head(&dev->wait_q);
  164. mutex_init(&dev->lock);
  165. mutex_init(&dev->rng_lock);
  166. usb_set_intfdata(interface, dev);
  167. result = usb_register_dev(interface, &chaoskey_class);
  168. if (result) {
  169. usb_err(interface, "Unable to allocate minor number.");
  170. goto out;
  171. }
  172. dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
  173. dev->hwrng.read = chaoskey_rng_read;
  174. dev->hwrng.quality = 1024;
  175. dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
  176. if (!dev->hwrng_registered)
  177. usb_err(interface, "Unable to register with hwrng");
  178. usb_enable_autosuspend(udev);
  179. usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
  180. return 0;
  181. out:
  182. usb_set_intfdata(interface, NULL);
  183. chaoskey_free(dev);
  184. return result;
  185. }
  186. static void chaoskey_disconnect(struct usb_interface *interface)
  187. {
  188. struct chaoskey *dev;
  189. usb_dbg(interface, "disconnect");
  190. dev = usb_get_intfdata(interface);
  191. if (!dev) {
  192. usb_dbg(interface, "disconnect failed - no dev");
  193. return;
  194. }
  195. if (dev->hwrng_registered)
  196. hwrng_unregister(&dev->hwrng);
  197. usb_deregister_dev(interface, &chaoskey_class);
  198. usb_set_intfdata(interface, NULL);
  199. mutex_lock(&dev->lock);
  200. dev->present = 0;
  201. usb_poison_urb(dev->urb);
  202. if (!dev->open) {
  203. mutex_unlock(&dev->lock);
  204. chaoskey_free(dev);
  205. } else
  206. mutex_unlock(&dev->lock);
  207. usb_dbg(interface, "disconnect done");
  208. }
  209. static int chaoskey_open(struct inode *inode, struct file *file)
  210. {
  211. struct chaoskey *dev;
  212. struct usb_interface *interface;
  213. /* get the interface from minor number and driver information */
  214. interface = usb_find_interface(&chaoskey_driver, iminor(inode));
  215. if (!interface)
  216. return -ENODEV;
  217. usb_dbg(interface, "open");
  218. dev = usb_get_intfdata(interface);
  219. if (!dev) {
  220. usb_dbg(interface, "open (dev)");
  221. return -ENODEV;
  222. }
  223. file->private_data = dev;
  224. mutex_lock(&dev->lock);
  225. ++dev->open;
  226. mutex_unlock(&dev->lock);
  227. usb_dbg(interface, "open success");
  228. return 0;
  229. }
  230. static int chaoskey_release(struct inode *inode, struct file *file)
  231. {
  232. struct chaoskey *dev = file->private_data;
  233. struct usb_interface *interface;
  234. if (dev == NULL)
  235. return -ENODEV;
  236. interface = dev->interface;
  237. usb_dbg(interface, "release");
  238. mutex_lock(&dev->lock);
  239. usb_dbg(interface, "open count at release is %d", dev->open);
  240. if (dev->open <= 0) {
  241. usb_dbg(interface, "invalid open count (%d)", dev->open);
  242. mutex_unlock(&dev->lock);
  243. return -ENODEV;
  244. }
  245. --dev->open;
  246. if (!dev->present) {
  247. if (dev->open == 0) {
  248. mutex_unlock(&dev->lock);
  249. chaoskey_free(dev);
  250. } else
  251. mutex_unlock(&dev->lock);
  252. } else
  253. mutex_unlock(&dev->lock);
  254. usb_dbg(interface, "release success");
  255. return 0;
  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. goto out;
  318. if (result == 0)
  319. result = -ETIMEDOUT;
  320. else
  321. result = dev->valid;
  322. out:
  323. /* Let the device go back to sleep eventually */
  324. usb_autopm_put_interface(dev->interface);
  325. usb_dbg(dev->interface, "read %d bytes", dev->valid);
  326. return result;
  327. }
  328. static ssize_t chaoskey_read(struct file *file,
  329. char __user *buffer,
  330. size_t count,
  331. loff_t *ppos)
  332. {
  333. struct chaoskey *dev;
  334. ssize_t read_count = 0;
  335. int this_time;
  336. int result = 0;
  337. unsigned long remain;
  338. dev = file->private_data;
  339. if (dev == NULL || !dev->present)
  340. return -ENODEV;
  341. usb_dbg(dev->interface, "read %zu", count);
  342. while (count > 0) {
  343. /* Grab the rng_lock briefly to ensure that the hwrng interface
  344. * gets priority over other user access
  345. */
  346. result = mutex_lock_interruptible(&dev->rng_lock);
  347. if (result)
  348. goto bail;
  349. mutex_unlock(&dev->rng_lock);
  350. result = mutex_lock_interruptible(&dev->lock);
  351. if (result)
  352. goto bail;
  353. if (dev->valid == dev->used) {
  354. result = _chaoskey_fill(dev);
  355. if (result < 0) {
  356. mutex_unlock(&dev->lock);
  357. goto bail;
  358. }
  359. }
  360. this_time = dev->valid - dev->used;
  361. if (this_time > count)
  362. this_time = count;
  363. remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
  364. if (remain) {
  365. result = -EFAULT;
  366. /* Consume the bytes that were copied so we don't leak
  367. * data to user space
  368. */
  369. dev->used += this_time - remain;
  370. mutex_unlock(&dev->lock);
  371. goto bail;
  372. }
  373. count -= this_time;
  374. read_count += this_time;
  375. buffer += this_time;
  376. dev->used += this_time;
  377. mutex_unlock(&dev->lock);
  378. }
  379. bail:
  380. if (read_count) {
  381. usb_dbg(dev->interface, "read %zu bytes", read_count);
  382. return read_count;
  383. }
  384. usb_dbg(dev->interface, "empty read, result %d", result);
  385. if (result == -ETIMEDOUT)
  386. result = -EAGAIN;
  387. return result;
  388. }
  389. static int chaoskey_rng_read(struct hwrng *rng, void *data,
  390. size_t max, bool wait)
  391. {
  392. struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
  393. int this_time;
  394. usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
  395. if (!dev->present) {
  396. usb_dbg(dev->interface, "device not present");
  397. return 0;
  398. }
  399. /* Hold the rng_lock until we acquire the device lock so that
  400. * this operation gets priority over other user access to the
  401. * device
  402. */
  403. mutex_lock(&dev->rng_lock);
  404. mutex_lock(&dev->lock);
  405. mutex_unlock(&dev->rng_lock);
  406. /* Try to fill the buffer if empty. It doesn't actually matter
  407. * if _chaoskey_fill works; we'll just return zero bytes as
  408. * the buffer will still be empty
  409. */
  410. if (dev->valid == dev->used)
  411. (void) _chaoskey_fill(dev);
  412. this_time = dev->valid - dev->used;
  413. if (this_time > max)
  414. this_time = max;
  415. memcpy(data, dev->buf + dev->used, this_time);
  416. dev->used += this_time;
  417. mutex_unlock(&dev->lock);
  418. usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
  419. return this_time;
  420. }
  421. #ifdef CONFIG_PM
  422. static int chaoskey_suspend(struct usb_interface *interface,
  423. pm_message_t message)
  424. {
  425. usb_dbg(interface, "suspend");
  426. return 0;
  427. }
  428. static int chaoskey_resume(struct usb_interface *interface)
  429. {
  430. usb_dbg(interface, "resume");
  431. return 0;
  432. }
  433. #else
  434. #define chaoskey_suspend NULL
  435. #define chaoskey_resume NULL
  436. #endif
  437. /* file operation pointers */
  438. static const struct file_operations chaoskey_fops = {
  439. .owner = THIS_MODULE,
  440. .read = chaoskey_read,
  441. .open = chaoskey_open,
  442. .release = chaoskey_release,
  443. .llseek = default_llseek,
  444. };
  445. /* class driver information */
  446. static struct usb_class_driver chaoskey_class = {
  447. .name = "chaoskey%d",
  448. .fops = &chaoskey_fops,
  449. .minor_base = USB_CHAOSKEY_MINOR_BASE,
  450. };
  451. /* usb specific object needed to register this driver with the usb subsystem */
  452. static struct usb_driver chaoskey_driver = {
  453. .name = DRIVER_SHORT,
  454. .probe = chaoskey_probe,
  455. .disconnect = chaoskey_disconnect,
  456. .suspend = chaoskey_suspend,
  457. .resume = chaoskey_resume,
  458. .reset_resume = chaoskey_resume,
  459. .id_table = chaoskey_table,
  460. .supports_autosuspend = 1,
  461. };
  462. module_usb_driver(chaoskey_driver);