chaoskey.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. kfree(dev);
  86. }
  87. }
  88. static int chaoskey_probe(struct usb_interface *interface,
  89. const struct usb_device_id *id)
  90. {
  91. struct usb_device *udev = interface_to_usbdev(interface);
  92. struct usb_host_interface *altsetting = interface->cur_altsetting;
  93. struct usb_endpoint_descriptor *epd;
  94. int in_ep;
  95. struct chaoskey *dev;
  96. int result = -ENOMEM;
  97. int size;
  98. int res;
  99. usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
  100. /* Find the first bulk IN endpoint and its packet size */
  101. res = usb_find_bulk_in_endpoint(altsetting, &epd);
  102. if (res) {
  103. usb_dbg(interface, "no IN endpoint found");
  104. return res;
  105. }
  106. in_ep = usb_endpoint_num(epd);
  107. size = usb_endpoint_maxp(epd);
  108. /* Validate endpoint and size */
  109. if (size <= 0) {
  110. usb_dbg(interface, "invalid size (%d)", size);
  111. return -ENODEV;
  112. }
  113. if (size > CHAOSKEY_BUF_LEN) {
  114. usb_dbg(interface, "size reduced from %d to %d\n",
  115. size, CHAOSKEY_BUF_LEN);
  116. size = CHAOSKEY_BUF_LEN;
  117. }
  118. /* Looks good, allocate and initialize */
  119. dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
  120. if (dev == NULL)
  121. goto out;
  122. dev->buf = kmalloc(size, GFP_KERNEL);
  123. if (dev->buf == NULL)
  124. goto out;
  125. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  126. if (!dev->urb)
  127. goto out;
  128. usb_fill_bulk_urb(dev->urb,
  129. udev,
  130. usb_rcvbulkpipe(udev, in_ep),
  131. dev->buf,
  132. size,
  133. chaos_read_callback,
  134. dev);
  135. /* Construct a name using the product and serial values. Each
  136. * device needs a unique name for the hwrng code
  137. */
  138. if (udev->product && udev->serial) {
  139. dev->name = kmalloc(strlen(udev->product) + 1 +
  140. strlen(udev->serial) + 1, GFP_KERNEL);
  141. if (dev->name == NULL)
  142. goto out;
  143. strcpy(dev->name, udev->product);
  144. strcat(dev->name, "-");
  145. strcat(dev->name, udev->serial);
  146. }
  147. dev->interface = interface;
  148. dev->in_ep = in_ep;
  149. if (le16_to_cpu(udev->descriptor.idVendor) != ALEA_VENDOR_ID)
  150. dev->reads_started = true;
  151. dev->size = size;
  152. dev->present = true;
  153. init_waitqueue_head(&dev->wait_q);
  154. mutex_init(&dev->lock);
  155. mutex_init(&dev->rng_lock);
  156. usb_set_intfdata(interface, dev);
  157. result = usb_register_dev(interface, &chaoskey_class);
  158. if (result) {
  159. usb_err(interface, "Unable to allocate minor number.");
  160. goto out;
  161. }
  162. dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
  163. dev->hwrng.read = chaoskey_rng_read;
  164. dev->hwrng.quality = 1024;
  165. dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
  166. if (!dev->hwrng_registered)
  167. usb_err(interface, "Unable to register with hwrng");
  168. usb_enable_autosuspend(udev);
  169. usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
  170. return 0;
  171. out:
  172. usb_set_intfdata(interface, NULL);
  173. chaoskey_free(dev);
  174. return result;
  175. }
  176. static void chaoskey_disconnect(struct usb_interface *interface)
  177. {
  178. struct chaoskey *dev;
  179. usb_dbg(interface, "disconnect");
  180. dev = usb_get_intfdata(interface);
  181. if (!dev) {
  182. usb_dbg(interface, "disconnect failed - no dev");
  183. return;
  184. }
  185. if (dev->hwrng_registered)
  186. hwrng_unregister(&dev->hwrng);
  187. usb_deregister_dev(interface, &chaoskey_class);
  188. usb_set_intfdata(interface, NULL);
  189. mutex_lock(&dev->lock);
  190. dev->present = false;
  191. usb_poison_urb(dev->urb);
  192. if (!dev->open) {
  193. mutex_unlock(&dev->lock);
  194. chaoskey_free(dev);
  195. } else
  196. mutex_unlock(&dev->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. /* get the interface from minor number and driver information */
  204. interface = usb_find_interface(&chaoskey_driver, iminor(inode));
  205. if (!interface)
  206. return -ENODEV;
  207. usb_dbg(interface, "open");
  208. dev = usb_get_intfdata(interface);
  209. if (!dev) {
  210. usb_dbg(interface, "open (dev)");
  211. return -ENODEV;
  212. }
  213. file->private_data = dev;
  214. mutex_lock(&dev->lock);
  215. ++dev->open;
  216. mutex_unlock(&dev->lock);
  217. usb_dbg(interface, "open success");
  218. return 0;
  219. }
  220. static int chaoskey_release(struct inode *inode, struct file *file)
  221. {
  222. struct chaoskey *dev = file->private_data;
  223. struct usb_interface *interface;
  224. if (dev == NULL)
  225. return -ENODEV;
  226. interface = dev->interface;
  227. usb_dbg(interface, "release");
  228. mutex_lock(&dev->lock);
  229. usb_dbg(interface, "open count at release is %d", dev->open);
  230. if (dev->open <= 0) {
  231. usb_dbg(interface, "invalid open count (%d)", dev->open);
  232. mutex_unlock(&dev->lock);
  233. return -ENODEV;
  234. }
  235. --dev->open;
  236. if (!dev->present) {
  237. if (dev->open == 0) {
  238. mutex_unlock(&dev->lock);
  239. chaoskey_free(dev);
  240. } else
  241. mutex_unlock(&dev->lock);
  242. } else
  243. mutex_unlock(&dev->lock);
  244. usb_dbg(interface, "release success");
  245. return 0;
  246. }
  247. static void chaos_read_callback(struct urb *urb)
  248. {
  249. struct chaoskey *dev = urb->context;
  250. int status = urb->status;
  251. usb_dbg(dev->interface, "callback status (%d)", status);
  252. if (status == 0)
  253. dev->valid = urb->actual_length;
  254. else
  255. dev->valid = 0;
  256. dev->used = 0;
  257. /* must be seen first before validity is announced */
  258. smp_wmb();
  259. dev->reading = false;
  260. wake_up(&dev->wait_q);
  261. }
  262. /* Fill the buffer. Called with dev->lock held
  263. */
  264. static int _chaoskey_fill(struct chaoskey *dev)
  265. {
  266. DEFINE_WAIT(wait);
  267. int result;
  268. bool started;
  269. usb_dbg(dev->interface, "fill");
  270. /* Return immediately if someone called before the buffer was
  271. * empty */
  272. if (dev->valid != dev->used) {
  273. usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
  274. dev->valid, dev->used);
  275. return 0;
  276. }
  277. /* Bail if the device has been removed */
  278. if (!dev->present) {
  279. usb_dbg(dev->interface, "device not present");
  280. return -ENODEV;
  281. }
  282. /* Make sure the device is awake */
  283. result = usb_autopm_get_interface(dev->interface);
  284. if (result) {
  285. usb_dbg(dev->interface, "wakeup failed (result %d)", result);
  286. return result;
  287. }
  288. dev->reading = true;
  289. result = usb_submit_urb(dev->urb, GFP_KERNEL);
  290. if (result < 0) {
  291. result = usb_translate_errors(result);
  292. dev->reading = false;
  293. goto out;
  294. }
  295. /* The first read on the Alea takes a little under 2 seconds.
  296. * Reads after the first read take only a few microseconds
  297. * though. Presumably the entropy-generating circuit needs
  298. * time to ramp up. So, we wait longer on the first read.
  299. */
  300. started = dev->reads_started;
  301. dev->reads_started = true;
  302. result = wait_event_interruptible_timeout(
  303. dev->wait_q,
  304. !dev->reading,
  305. (started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) );
  306. if (result < 0)
  307. goto out;
  308. if (result == 0)
  309. result = -ETIMEDOUT;
  310. else
  311. result = dev->valid;
  312. out:
  313. /* Let the device go back to sleep eventually */
  314. usb_autopm_put_interface(dev->interface);
  315. usb_dbg(dev->interface, "read %d bytes", dev->valid);
  316. return result;
  317. }
  318. static ssize_t chaoskey_read(struct file *file,
  319. char __user *buffer,
  320. size_t count,
  321. loff_t *ppos)
  322. {
  323. struct chaoskey *dev;
  324. ssize_t read_count = 0;
  325. int this_time;
  326. int result = 0;
  327. unsigned long remain;
  328. dev = file->private_data;
  329. if (dev == NULL || !dev->present)
  330. return -ENODEV;
  331. usb_dbg(dev->interface, "read %zu", count);
  332. while (count > 0) {
  333. /* Grab the rng_lock briefly to ensure that the hwrng interface
  334. * gets priority over other user access
  335. */
  336. result = mutex_lock_interruptible(&dev->rng_lock);
  337. if (result)
  338. goto bail;
  339. mutex_unlock(&dev->rng_lock);
  340. result = mutex_lock_interruptible(&dev->lock);
  341. if (result)
  342. goto bail;
  343. if (dev->valid == dev->used) {
  344. result = _chaoskey_fill(dev);
  345. if (result < 0) {
  346. mutex_unlock(&dev->lock);
  347. goto bail;
  348. }
  349. }
  350. this_time = dev->valid - dev->used;
  351. if (this_time > count)
  352. this_time = count;
  353. remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
  354. if (remain) {
  355. result = -EFAULT;
  356. /* Consume the bytes that were copied so we don't leak
  357. * data to user space
  358. */
  359. dev->used += this_time - remain;
  360. mutex_unlock(&dev->lock);
  361. goto bail;
  362. }
  363. count -= this_time;
  364. read_count += this_time;
  365. buffer += this_time;
  366. dev->used += this_time;
  367. mutex_unlock(&dev->lock);
  368. }
  369. bail:
  370. if (read_count) {
  371. usb_dbg(dev->interface, "read %zu bytes", read_count);
  372. return read_count;
  373. }
  374. usb_dbg(dev->interface, "empty read, result %d", result);
  375. if (result == -ETIMEDOUT)
  376. result = -EAGAIN;
  377. return result;
  378. }
  379. static int chaoskey_rng_read(struct hwrng *rng, void *data,
  380. size_t max, bool wait)
  381. {
  382. struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
  383. int this_time;
  384. usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
  385. if (!dev->present) {
  386. usb_dbg(dev->interface, "device not present");
  387. return 0;
  388. }
  389. /* Hold the rng_lock until we acquire the device lock so that
  390. * this operation gets priority over other user access to the
  391. * device
  392. */
  393. mutex_lock(&dev->rng_lock);
  394. mutex_lock(&dev->lock);
  395. mutex_unlock(&dev->rng_lock);
  396. /* Try to fill the buffer if empty. It doesn't actually matter
  397. * if _chaoskey_fill works; we'll just return zero bytes as
  398. * the buffer will still be empty
  399. */
  400. if (dev->valid == dev->used)
  401. (void) _chaoskey_fill(dev);
  402. this_time = dev->valid - dev->used;
  403. if (this_time > max)
  404. this_time = max;
  405. memcpy(data, dev->buf + dev->used, this_time);
  406. dev->used += this_time;
  407. mutex_unlock(&dev->lock);
  408. usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
  409. return this_time;
  410. }
  411. #ifdef CONFIG_PM
  412. static int chaoskey_suspend(struct usb_interface *interface,
  413. pm_message_t message)
  414. {
  415. usb_dbg(interface, "suspend");
  416. return 0;
  417. }
  418. static int chaoskey_resume(struct usb_interface *interface)
  419. {
  420. usb_dbg(interface, "resume");
  421. return 0;
  422. }
  423. #else
  424. #define chaoskey_suspend NULL
  425. #define chaoskey_resume NULL
  426. #endif
  427. /* file operation pointers */
  428. static const struct file_operations chaoskey_fops = {
  429. .owner = THIS_MODULE,
  430. .read = chaoskey_read,
  431. .open = chaoskey_open,
  432. .release = chaoskey_release,
  433. .llseek = default_llseek,
  434. };
  435. /* class driver information */
  436. static struct usb_class_driver chaoskey_class = {
  437. .name = "chaoskey%d",
  438. .fops = &chaoskey_fops,
  439. .minor_base = USB_CHAOSKEY_MINOR_BASE,
  440. };
  441. /* usb specific object needed to register this driver with the usb subsystem */
  442. static struct usb_driver chaoskey_driver = {
  443. .name = DRIVER_SHORT,
  444. .probe = chaoskey_probe,
  445. .disconnect = chaoskey_disconnect,
  446. .suspend = chaoskey_suspend,
  447. .resume = chaoskey_resume,
  448. .reset_resume = chaoskey_resume,
  449. .id_table = chaoskey_table,
  450. .supports_autosuspend = 1,
  451. };
  452. module_usb_driver(chaoskey_driver);