| From: |
| Keith Packard <keithp-aN4HjG94KOLQT0dZR+AlfA@public.gmane.org> |
| To: |
| Greg Kroah-Hartman <gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org>, linux-usb-u79uwXL29TY76Z2rM5mHXA@public.gmane.org |
| Subject: |
| [PATCH] usb: Add driver for Altus Metrum ChaosKey device |
| Date: |
| Thu, 19 Mar 2015 03:07:23 -0700 |
| Message-ID: |
| <1426759643-17097-1-git-send-email-keithp@keithp.com> |
| Cc: |
| keithp-aN4HjG94KOLQT0dZR+AlfA@public.gmane.org |
This is a hardware random number generator. The driver provides both a
/dev/chaoskeyX entry and hooks the entropy source up to the kernel
hwrng interface. More information about the device can be found at
http://chaoskey.org
The USB ID for ChaosKey was allocated from the OpenMoko USB vendor
space and is visible as 'USBtrng' here:
http://wiki.openmoko.org/wiki/USB_Product_IDs
Signed-off-by: Keith Packard <keithp-aN4HjG94KOLQT0dZR+AlfA@public.gmane.org>
---
drivers/usb/misc/Kconfig | 12 +
drivers/usb/misc/Makefile | 1 +
drivers/usb/misc/chaoskey.c | 550 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 563 insertions(+)
create mode 100644 drivers/usb/misc/chaoskey.c
diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
index 76d7720..8c331f1 100644
--- a/drivers/usb/misc/Kconfig
+++ b/drivers/usb/misc/Kconfig
@@ -255,3 +255,15 @@ config USB_LINK_LAYER_TEST
This driver is for generating specific traffic for Super Speed Link
Layer Test Device. Say Y only when you want to conduct USB Super Speed
Link Layer Test for host controllers.
+
+config USB_CHAOSKEY
+ tristate "ChaosKey random number generator driver support"
+ help
+ Say Y here if you want to connect an AltusMetrum ChaosKey to
+ your computer's USB port. The ChaosKey is a hardware random
+ number generator which hooks into the kernel entropy pool to
+ ensure a large supply of entropy for /dev/random and
+ /dev/urandom and also provides direct access via /dev/chaoskeyX
+
+ To compile this driver as a module, choose M here: the
+ module will be called chaoskey.
diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
index 65b0402..45fd4ac 100644
--- a/drivers/usb/misc/Makefile
+++ b/drivers/usb/misc/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_USB_USS720) += uss720.o
obj-$(CONFIG_USB_SEVSEG) += usbsevseg.o
obj-$(CONFIG_USB_YUREX) += yurex.o
obj-$(CONFIG_USB_HSIC_USB3503) += usb3503.o
+obj-$(CONFIG_USB_CHAOSKEY) += chaoskey.o
obj-$(CONFIG_USB_SISUSBVGA) += sisusbvga/
obj-$(CONFIG_USB_LINK_LAYER_TEST) += lvstest.o
diff --git a/drivers/usb/misc/chaoskey.c b/drivers/usb/misc/chaoskey.c
new file mode 100644
index 0000000..5b32e84
--- /dev/null
+++ b/drivers/usb/misc/chaoskey.c
@@ -0,0 +1,550 @@
+/*
+ * chaoskey - driver for ChaosKey device from Altus Metrum.
+ *
+ * This device provides true random numbers using a noise source based
+ * on a reverse-biased p-n junction in avalanche breakdown. More
+ * details can be found at http://chaoskey.org
+ *
+ * The driver connects to the kernel hardware RNG interface to provide
+ * entropy for /dev/random and other kernel activities. It also offers
+ * a separate /dev/ entry to allow for direct access to the random
+ * bit stream.
+ *
+ * Copyright © 2015 Keith Packard <keithp-aN4HjG94KOLQT0dZR+AlfA@public.gmane.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+#include <linux/wait.h>
+#include <linux/hw_random.h>
+
+#define DEBUG_INIT 1
+#define DEBUG_POWER 2
+#define DEBUG_DEV 4
+#define DEBUG_RNG 8
+#define DEBUG_IO (DEBUG_DEV|DEBUG_RNG)
+#define DEBUG_API 16
+
+static int debug = 0; /* bitmask of above values */
+
+static struct usb_driver chaoskey_driver;
+static struct usb_class_driver chaoskey_class;
+static int chaoskey_rng_read(struct hwrng *rng, void *data, size_t max, bool wait);
+
+#define CK_DEBUG(mask, fmt, args...) do { \
+ if (unlikely(debug & (mask))) \
+ printk(KERN_INFO " chaoskey: %s: %d " fmt, __func__, __LINE__, ##args); \
+ } while (0)
+
+#define CK_ENTER(mask, what) CK_DEBUG(mask, "Enter " what)
+#define CK_LEAVE(mask, what,fmt,ret) CK_DEBUG(mask, "Leave " what ":" fmt, ret)
+#define CK_LEAVE_INT(mask, what,ret) CK_LEAVE(mask, what,"%d",ret)
+#define CK_LEAVE_SIZE(mask, what,ret) CK_LEAVE(mask, what,"%zu",ret)
+#define CK_LEAVE_SSIZE(mask, what,ret) CK_LEAVE(mask, what,"%zd",ret)
+#define CK_LEAVE_VOID(mask, what) CK_DEBUG(mask, "Leave " what)
+
+/* Version Information */
+#define DRIVER_VERSION "v0.1"
+#define DRIVER_AUTHOR "Keith Packard, keithp-aN4HjG94KOLQT0dZR+AlfA@public.gmane.org"
+#define DRIVER_DESC "Altus Metrum ChaosKey driver"
+#define DRIVER_SHORT "chaoskey"
+
+MODULE_VERSION(DRIVER_VERSION);
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL");
+MODULE_PARM_DESC(debug, "Enable debug output");
+
+module_param(debug, int, 0644);
+
+#define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
+#define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
+
+#define CHAOSKEY_BUF_LEN 64 /* maximum size of USB full speed packet */
+
+#define NAK_TIMEOUT (HZ) /* stall/wait timeout for device */
+
+#ifdef CONFIG_USB_DYNAMIC_MINORS
+#define USB_CHAOSKEY_MINOR_BASE 0
+#else
+#define USB_CHAOSKEY_MINOR_BASE 224 /* IOWARRIOR_MINOR_BASE + 16, not official yet */
+#endif
+
+static const struct usb_device_id chaoskey_table[] = {
+ { USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
+ { },
+};
+MODULE_DEVICE_TABLE (usb, chaoskey_table);
+
+/* Driver-local specific stuff */
+struct chaoskey {
+ struct usb_device *udev;
+ struct usb_interface *interface;
+ char in_ep;
+ struct mutex lock;
+ struct mutex rng_lock;
+ int open; /* open count */
+ int present; /* device not disconnected */
+ int size; /* size of buf */
+ int valid; /* bytes of buf read */
+ int used; /* bytes of buf consumed */
+ char *name; /* product + serial */
+ struct hwrng hwrng; /* Embedded struct for hwrng */
+ int hwrng_registered; /* registered with hwrng API */
+ wait_queue_head_t wait_q; /* for timeouts */
+ char buf[0];
+};
+
+static void chaoskey_free(struct chaoskey *dev)
+{
+ CK_ENTER(DEBUG_INIT, "free");
+ if (dev->name)
+ kfree(dev->name);
+ kfree(dev);
+ CK_LEAVE_VOID(DEBUG_INIT, "free");
+}
+
+static int chaoskey_probe(struct usb_interface *interface,
+ const struct usb_device_id *id)
+{
+ struct usb_device *udev = interface_to_usbdev(interface);
+ struct usb_host_interface *altsetting = interface->cur_altsetting;
+ int i;
+ int in_ep = -1;
+ struct chaoskey *dev;
+ int result;
+ int size;
+
+ CK_ENTER(DEBUG_INIT, "probe");
+
+ /* Find the first bulk IN endpoint and its packet size */
+ for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
+ if (usb_endpoint_is_bulk_in(&altsetting->endpoint[i].desc)) {
+ in_ep = altsetting->endpoint[i].desc.bEndpointAddress;
+ size = altsetting->endpoint[i].desc.wMaxPacketSize;
+ break;
+ }
+ }
+
+ /* Validate endpoint and size */
+ if (in_ep == -1) {
+ CK_LEAVE_INT(DEBUG_INIT, "probe (ep)", -ENODEV);
+ return -ENODEV;
+ }
+ if (size <= 0) {
+ CK_LEAVE_INT(DEBUG_INIT, "probe (size)", -ENODEV);
+ return -ENODEV;
+ }
+
+ /* Looks good, allocate and initialize */
+ CK_DEBUG(DEBUG_INIT, "New chaoskey, in_ep %d size %d\n", in_ep, size);
+
+ dev = kzalloc (sizeof (struct chaoskey) + size, GFP_KERNEL);
+
+ if (dev == NULL) {
+ CK_LEAVE_INT(DEBUG_INIT, "probe (dev)", -ENOMEM);
+ return -ENOMEM;
+ }
+
+ dev->udev = udev;
+ dev->interface = interface;
+
+ dev->in_ep = in_ep;
+
+ if (size > CHAOSKEY_BUF_LEN) {
+ CK_DEBUG(DEBUG_INIT, "chaoskey size %d reduced to %d\n", size,
+ CHAOSKEY_BUF_LEN);
+ size = CHAOSKEY_BUF_LEN;
+ }
+
+ dev->size = size;
+ dev->present = 1;
+
+ init_waitqueue_head(&dev->wait_q);
+
+ usb_set_intfdata(interface, dev);
+
+ result = usb_register_dev(interface, &chaoskey_class);
+ if (result) {
+ dev_err(&interface->dev, "Unable to allocate minor number.\n");
+ usb_set_intfdata(interface, NULL);
+ chaoskey_free(dev);
+ CK_LEAVE_INT(DEBUG_INIT, "probe (register)", result);
+ return result;
+ }
+
+ mutex_init(&dev->lock);
+ mutex_init(&dev->rng_lock);
+
+ /* Construct a name using the product and serial values. Each
+ * device needs a unique name for the hwrng code
+ */
+ dev->name = NULL;
+
+ if (udev->product && udev->serial) {
+ dev->name = kmalloc(strlen(udev->product) + 1 + strlen(udev->serial) + 1, GFP_KERNEL);
+ if (dev->name) {
+ strcpy(dev->name, udev->product);
+ strcat(dev->name, "-");
+ strcat(dev->name, udev->serial);
+ }
+ }
+
+ dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
+ dev->hwrng.read = chaoskey_rng_read;
+
+ /* Set the 'quality' metric. Quality is measured in units of
+ * 1/1024's of a bit ("mills"). This should be set to 1024,
+ * but there is a bug in the hwrng core which masks it with
+ * 1023.
+ *
+ * The patch that has been merged to the crypto development
+ * tree for that bug limits the value to 1024 at most, so by
+ * setting this to 1024 + 1023, we get 1023 before the fix is
+ * merged and 1024 afterwards. We'll patch this driver once
+ * both bits of code are in the same tree.
+ */
+ dev->hwrng.quality = 1024 + 1023;
+
+ dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
+ if (!dev->hwrng_registered)
+ CK_DEBUG(DEBUG_INIT, "probe hwrng register failed: %d\n", result);
+
+ usb_enable_autosuspend(udev);
+
+ CK_LEAVE_INT(DEBUG_INIT, "probe", 0);
+ return 0;
+}
+
+static void chaoskey_disconnect(struct usb_interface *interface)
+{
+ struct chaoskey *dev;
+
+ CK_ENTER(DEBUG_INIT, "disconnect");
+ dev = usb_get_intfdata(interface);
+ if (!dev) {
+ CK_LEAVE_VOID(DEBUG_INIT, "disconnect (dev)");
+ return;
+ }
+
+ if (dev->hwrng_registered)
+ hwrng_unregister(&dev->hwrng);
+
+ usb_deregister_dev(interface, &chaoskey_class);
+
+ usb_set_intfdata(interface, NULL);
+ mutex_lock(&dev->lock);
+
+ dev->present = 0;
+
+ if (!dev->open) {
+ mutex_unlock(&dev->lock);
+ chaoskey_free(dev);
+ } else
+ mutex_unlock(&dev->lock);
+
+ CK_LEAVE_VOID(DEBUG_INIT, "disconnect");
+ return;
+}
+
+static int chaoskey_open(struct inode *inode, struct file *file)
+{
+ struct chaoskey *dev;
+ struct usb_interface *interface;
+
+ CK_ENTER(DEBUG_DEV, "open");
+ /* get the interface from minor number and driver information */
+ interface = usb_find_interface (&chaoskey_driver, iminor (inode));
+ if (!interface) {
+ CK_LEAVE_INT(DEBUG_DEV, "open (interface)", -ENODEV);
+ return -ENODEV;
+ }
+
+ dev = usb_get_intfdata(interface);
+ if (!dev) {
+ CK_LEAVE_INT(DEBUG_DEV, "open (dev)", -ENODEV);
+ return -ENODEV;
+ }
+
+ file->private_data = dev;
+ mutex_lock(&dev->lock);
+ ++dev->open;
+ mutex_unlock(&dev->lock);
+
+ CK_LEAVE_INT(DEBUG_DEV, "open", 0);
+ return 0;
+}
+
+static int chaoskey_release(struct inode *inode, struct file *file)
+{
+ struct chaoskey *dev = file->private_data;
+
+ CK_ENTER(DEBUG_DEV, "release");
+
+ if (dev == NULL) {
+ CK_LEAVE_INT(DEBUG_DEV, "release (dev)", -ENODEV);
+ return -ENODEV;
+ }
+
+ mutex_lock(&dev->lock);
+
+ CK_DEBUG(DEBUG_DEV, "open count %d\n", dev->open);
+
+ if (dev->open <= 0) {
+ mutex_unlock(&dev->lock);
+ CK_LEAVE_INT(DEBUG_DEV, "release (open)", -ENODEV);
+ return -ENODEV;
+ }
+
+ --dev->open;
+
+ if (!dev->present) {
+ if (dev->open == 0) {
+ mutex_unlock(&dev->lock);
+ chaoskey_free(dev);
+ } else
+ mutex_unlock(&dev->lock);
+ } else
+ mutex_unlock(&dev->lock);
+
+ CK_LEAVE_INT(DEBUG_DEV, "release", 0);
+ return 0;
+}
+
+/* Fill the buffer. Called with dev->lock held
+ */
+static int _chaoskey_fill(struct chaoskey *dev)
+{
+ DEFINE_WAIT(wait);
+ int result;
+ int this_read;
+
+ CK_ENTER(DEBUG_IO, "fill");
+
+ /* Return immediately if someone called before the buffer was
+ * empty */
+ if (dev->valid != dev->used) {
+ CK_LEAVE_INT(DEBUG_IO, "fill (valid)", 0);
+ return 0;
+ }
+
+ /* Bail if the device has been removed */
+ if (!dev->present) {
+ CK_LEAVE_INT(DEBUG_IO, "fill (present)", -ENODEV);
+ return -ENODEV;
+ }
+
+ /* Make sure the device is awake */
+ result = usb_autopm_get_interface(dev->interface);
+ if (result) {
+ CK_LEAVE_INT(DEBUG_IO, "fill (interface)", result);
+ return result;
+ }
+
+ result = usb_bulk_msg(dev->udev,
+ usb_rcvbulkpipe(dev->udev, dev->in_ep),
+ dev->buf, dev->size, &this_read,
+ NAK_TIMEOUT);
+
+ /* Let the device go back to sleep eventually */
+ usb_autopm_put_interface(dev->interface);
+
+ CK_DEBUG(DEBUG_IO, "bulk msg result %d partial %d", result, this_read);
+
+ if (result == 0) {
+ dev->valid = this_read;
+ dev->used = 0;
+ }
+
+ CK_LEAVE_INT(DEBUG_IO, "fill", result);
+ return result;
+}
+
+static ssize_t chaoskey_read(struct file *file,
+ char __user *buffer,
+ size_t count,
+ loff_t * ppos)
+{
+ struct chaoskey *dev;
+ int intr;
+ ssize_t read_count = 0;
+ int this_time;
+ int result;
+
+ CK_ENTER(DEBUG_DEV, "read");
+ dev = file->private_data;
+
+ if (dev == NULL || !dev->present) {
+ CK_LEAVE_INT(DEBUG_DEV, "read (present)", -ENODEV);
+ return -ENODEV;
+ }
+
+
+ CK_DEBUG(DEBUG_DEV, "read %zu\n", count);
+
+ while (count > 0) {
+
+ /* Grab the rng_lock briefly to ensure that the hwrng interface
+ * gets priority over other user access
+ */
+ intr = mutex_lock_interruptible(&dev->rng_lock);
+ if (intr) {
+ CK_LEAVE_INT(DEBUG_DEV, "read (rng_lock)", -EINTR);
+ return -EINTR;
+ }
+ mutex_unlock(&dev->rng_lock);
+
+ intr = mutex_lock_interruptible(&dev->lock);
+ if (intr) {
+ CK_LEAVE_INT(DEBUG_DEV, "read (lock)", -EINTR);
+ return -EINTR;
+ }
+ if (dev->valid == dev->used) {
+ result = _chaoskey_fill(dev);
+ if (result) {
+ if (read_count) {
+ CK_LEAVE_SIZE(DEBUG_DEV, "read (count)",
+ read_count);
+ return read_count;
+ }
+ CK_LEAVE_INT(DEBUG_DEV, "read (result)", result);
+ return result;
+ }
+
+ /* Read returned zero bytes */
+ if (dev->used == dev->valid) {
+ CK_LEAVE_SSIZE(DEBUG_DEV, "read (zero)", read_count);
+ return read_count;
+ }
+ }
+
+ this_time = dev->valid - dev->used;
+ if (this_time > count)
+ this_time = count;
+
+ if (copy_to_user(buffer, dev->buf + dev->used, this_time)) {
+ mutex_unlock(&dev->lock);
+ CK_LEAVE_INT(DEBUG_DEV, "read (copyout)", -EFAULT);
+ return -EFAULT;
+ }
+
+ count -= this_time;
+ read_count += this_time;
+ buffer += this_time;
+ dev->used += this_time;
+ mutex_unlock(&dev->lock);
+ }
+
+ CK_LEAVE_SSIZE(DEBUG_DEV, "read", read_count);
+ return read_count;
+}
+
+static int chaoskey_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
+{
+ struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
+ int result;
+ int this_time;
+
+ CK_ENTER(DEBUG_RNG, "rng_read");
+ if (dev == NULL || !dev->present) {
+ CK_LEAVE_INT(DEBUG_RNG, "rng_read (dev)", 0);
+ return 0;
+ }
+
+ /* Hold the rng_lock until we acquire the device lock so that
+ * this operation gets priority over other user access to the
+ * device
+ */
+ mutex_lock(&dev->rng_lock);
+
+ mutex_lock(&dev->lock);
+
+ mutex_unlock(&dev->rng_lock);
+
+ CK_DEBUG(DEBUG_RNG, "rng_read max %zu wait %d\n", max, wait);
+
+ if (dev->valid == dev->used) {
+ result = _chaoskey_fill(dev);
+ if (result) {
+ mutex_unlock(&dev->lock);
+ CK_LEAVE_INT(DEBUG_RNG, "rng_read (fill)", 0);
+ return 0;
+ }
+ }
+
+ this_time = dev->valid - dev->used;
+
+ if (this_time > max)
+ this_time = max;
+
+ memcpy(data, dev->buf, this_time);
+
+ dev->used += this_time;
+
+ mutex_unlock(&dev->lock);
+
+ CK_LEAVE_INT(DEBUG_RNG, "read", this_time);
+ return this_time;
+}
+
+#ifdef CONFIG_PM
+static int chaoskey_suspend(struct usb_interface *intf, pm_message_t message)
+{
+ CK_DEBUG(DEBUG_POWER, "suspend");
+ return 0;
+}
+
+static int chaoskey_resume(struct usb_interface *intf)
+{
+ CK_DEBUG(DEBUG_POWER, "resume");
+ return 0;
+}
+#else
+#define chaoskey_suspend NULL
+#define chaoskey_resume NULL
+#endif
+
+/* file operation pointers */
+static const struct file_operations chaoskey_fops = {
+ .owner = THIS_MODULE,
+ .read = chaoskey_read,
+ .open = chaoskey_open,
+ .release = chaoskey_release,
+ .llseek = default_llseek,
+};
+
+/* class driver information */
+static struct usb_class_driver chaoskey_class = {
+ .name = "chaoskey%d",
+ .fops = &chaoskey_fops,
+ .minor_base = USB_CHAOSKEY_MINOR_BASE,
+};
+
+/* usb specific object needed to register this driver with the usb subsystem */
+static struct usb_driver chaoskey_driver = {
+ .name = DRIVER_SHORT,
+ .probe = chaoskey_probe,
+ .disconnect = chaoskey_disconnect,
+ .suspend = chaoskey_suspend,
+ .resume = chaoskey_resume,
+ .reset_resume = chaoskey_resume,
+ .id_table = chaoskey_table,
+ .supports_autosuspend = 1,
+};
+
+module_usb_driver(chaoskey_driver);
+
--
2.1.4
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html