Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * chaoskey - driver for ChaosKey device from Altus Metrum.
0004  *
0005  * This device provides true random numbers using a noise source based
0006  * on a reverse-biased p-n junction in avalanche breakdown. More
0007  * details can be found at http://chaoskey.org
0008  *
0009  * The driver connects to the kernel hardware RNG interface to provide
0010  * entropy for /dev/random and other kernel activities. It also offers
0011  * a separate /dev/ entry to allow for direct access to the random
0012  * bit stream.
0013  *
0014  * Copyright © 2015 Keith Packard <keithp@keithp.com>
0015  */
0016 
0017 #include <linux/module.h>
0018 #include <linux/slab.h>
0019 #include <linux/usb.h>
0020 #include <linux/wait.h>
0021 #include <linux/hw_random.h>
0022 #include <linux/mutex.h>
0023 #include <linux/uaccess.h>
0024 
0025 static struct usb_driver chaoskey_driver;
0026 static struct usb_class_driver chaoskey_class;
0027 static int chaoskey_rng_read(struct hwrng *rng, void *data,
0028                  size_t max, bool wait);
0029 
0030 #define usb_dbg(usb_if, format, arg...) \
0031     dev_dbg(&(usb_if)->dev, format, ## arg)
0032 
0033 #define usb_err(usb_if, format, arg...) \
0034     dev_err(&(usb_if)->dev, format, ## arg)
0035 
0036 /* Version Information */
0037 #define DRIVER_AUTHOR   "Keith Packard, keithp@keithp.com"
0038 #define DRIVER_DESC "Altus Metrum ChaosKey driver"
0039 #define DRIVER_SHORT    "chaoskey"
0040 
0041 MODULE_AUTHOR(DRIVER_AUTHOR);
0042 MODULE_DESCRIPTION(DRIVER_DESC);
0043 MODULE_LICENSE("GPL");
0044 
0045 #define CHAOSKEY_VENDOR_ID  0x1d50  /* OpenMoko */
0046 #define CHAOSKEY_PRODUCT_ID 0x60c6  /* ChaosKey */
0047 
0048 #define ALEA_VENDOR_ID      0x12d8  /* Araneus */
0049 #define ALEA_PRODUCT_ID     0x0001  /* Alea I */
0050 
0051 #define CHAOSKEY_BUF_LEN    64  /* max size of USB full speed packet */
0052 
0053 #define NAK_TIMEOUT (HZ)        /* normal stall/wait timeout */
0054 #define ALEA_FIRST_TIMEOUT (HZ*3)   /* first stall/wait timeout for Alea */
0055 
0056 #ifdef CONFIG_USB_DYNAMIC_MINORS
0057 #define USB_CHAOSKEY_MINOR_BASE 0
0058 #else
0059 
0060 /* IOWARRIOR_MINOR_BASE + 16, not official yet */
0061 #define USB_CHAOSKEY_MINOR_BASE 224
0062 #endif
0063 
0064 static const struct usb_device_id chaoskey_table[] = {
0065     { USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
0066     { USB_DEVICE(ALEA_VENDOR_ID, ALEA_PRODUCT_ID) },
0067     { },
0068 };
0069 MODULE_DEVICE_TABLE(usb, chaoskey_table);
0070 
0071 static void chaos_read_callback(struct urb *urb);
0072 
0073 /* Driver-local specific stuff */
0074 struct chaoskey {
0075     struct usb_interface *interface;
0076     char in_ep;
0077     struct mutex lock;
0078     struct mutex rng_lock;
0079     int open;           /* open count */
0080     bool present;           /* device not disconnected */
0081     bool reading;           /* ongoing IO */
0082     bool reads_started;     /* track first read for Alea */
0083     int size;           /* size of buf */
0084     int valid;          /* bytes of buf read */
0085     int used;           /* bytes of buf consumed */
0086     char *name;         /* product + serial */
0087     struct hwrng hwrng;     /* Embedded struct for hwrng */
0088     int hwrng_registered;       /* registered with hwrng API */
0089     wait_queue_head_t wait_q;   /* for timeouts */
0090     struct urb *urb;        /* for performing IO */
0091     char *buf;
0092 };
0093 
0094 static void chaoskey_free(struct chaoskey *dev)
0095 {
0096     if (dev) {
0097         usb_dbg(dev->interface, "free");
0098         usb_free_urb(dev->urb);
0099         kfree(dev->name);
0100         kfree(dev->buf);
0101         usb_put_intf(dev->interface);
0102         kfree(dev);
0103     }
0104 }
0105 
0106 static int chaoskey_probe(struct usb_interface *interface,
0107               const struct usb_device_id *id)
0108 {
0109     struct usb_device *udev = interface_to_usbdev(interface);
0110     struct usb_host_interface *altsetting = interface->cur_altsetting;
0111     struct usb_endpoint_descriptor *epd;
0112     int in_ep;
0113     struct chaoskey *dev;
0114     int result = -ENOMEM;
0115     int size;
0116     int res;
0117 
0118     usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
0119 
0120     /* Find the first bulk IN endpoint and its packet size */
0121     res = usb_find_bulk_in_endpoint(altsetting, &epd);
0122     if (res) {
0123         usb_dbg(interface, "no IN endpoint found");
0124         return res;
0125     }
0126 
0127     in_ep = usb_endpoint_num(epd);
0128     size = usb_endpoint_maxp(epd);
0129 
0130     /* Validate endpoint and size */
0131     if (size <= 0) {
0132         usb_dbg(interface, "invalid size (%d)", size);
0133         return -ENODEV;
0134     }
0135 
0136     if (size > CHAOSKEY_BUF_LEN) {
0137         usb_dbg(interface, "size reduced from %d to %d\n",
0138             size, CHAOSKEY_BUF_LEN);
0139         size = CHAOSKEY_BUF_LEN;
0140     }
0141 
0142     /* Looks good, allocate and initialize */
0143 
0144     dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
0145 
0146     if (dev == NULL)
0147         goto out;
0148 
0149     dev->interface = usb_get_intf(interface);
0150 
0151     dev->buf = kmalloc(size, GFP_KERNEL);
0152 
0153     if (dev->buf == NULL)
0154         goto out;
0155 
0156     dev->urb = usb_alloc_urb(0, GFP_KERNEL);
0157 
0158     if (!dev->urb)
0159         goto out;
0160 
0161     usb_fill_bulk_urb(dev->urb,
0162         udev,
0163         usb_rcvbulkpipe(udev, in_ep),
0164         dev->buf,
0165         size,
0166         chaos_read_callback,
0167         dev);
0168 
0169     /* Construct a name using the product and serial values. Each
0170      * device needs a unique name for the hwrng code
0171      */
0172 
0173     if (udev->product && udev->serial) {
0174         dev->name = kasprintf(GFP_KERNEL, "%s-%s", udev->product,
0175                       udev->serial);
0176         if (dev->name == NULL)
0177             goto out;
0178     }
0179 
0180     dev->in_ep = in_ep;
0181 
0182     if (le16_to_cpu(udev->descriptor.idVendor) != ALEA_VENDOR_ID)
0183         dev->reads_started = true;
0184 
0185     dev->size = size;
0186     dev->present = true;
0187 
0188     init_waitqueue_head(&dev->wait_q);
0189 
0190     mutex_init(&dev->lock);
0191     mutex_init(&dev->rng_lock);
0192 
0193     usb_set_intfdata(interface, dev);
0194 
0195     result = usb_register_dev(interface, &chaoskey_class);
0196     if (result) {
0197         usb_err(interface, "Unable to allocate minor number.");
0198         goto out;
0199     }
0200 
0201     dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
0202     dev->hwrng.read = chaoskey_rng_read;
0203     dev->hwrng.quality = 1024;
0204 
0205     dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
0206     if (!dev->hwrng_registered)
0207         usb_err(interface, "Unable to register with hwrng");
0208 
0209     usb_enable_autosuspend(udev);
0210 
0211     usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
0212     return 0;
0213 
0214 out:
0215     usb_set_intfdata(interface, NULL);
0216     chaoskey_free(dev);
0217     return result;
0218 }
0219 
0220 static void chaoskey_disconnect(struct usb_interface *interface)
0221 {
0222     struct chaoskey *dev;
0223 
0224     usb_dbg(interface, "disconnect");
0225     dev = usb_get_intfdata(interface);
0226     if (!dev) {
0227         usb_dbg(interface, "disconnect failed - no dev");
0228         return;
0229     }
0230 
0231     if (dev->hwrng_registered)
0232         hwrng_unregister(&dev->hwrng);
0233 
0234     usb_deregister_dev(interface, &chaoskey_class);
0235 
0236     usb_set_intfdata(interface, NULL);
0237     mutex_lock(&dev->lock);
0238 
0239     dev->present = false;
0240     usb_poison_urb(dev->urb);
0241 
0242     if (!dev->open) {
0243         mutex_unlock(&dev->lock);
0244         chaoskey_free(dev);
0245     } else
0246         mutex_unlock(&dev->lock);
0247 
0248     usb_dbg(interface, "disconnect done");
0249 }
0250 
0251 static int chaoskey_open(struct inode *inode, struct file *file)
0252 {
0253     struct chaoskey *dev;
0254     struct usb_interface *interface;
0255 
0256     /* get the interface from minor number and driver information */
0257     interface = usb_find_interface(&chaoskey_driver, iminor(inode));
0258     if (!interface)
0259         return -ENODEV;
0260 
0261     usb_dbg(interface, "open");
0262 
0263     dev = usb_get_intfdata(interface);
0264     if (!dev) {
0265         usb_dbg(interface, "open (dev)");
0266         return -ENODEV;
0267     }
0268 
0269     file->private_data = dev;
0270     mutex_lock(&dev->lock);
0271     ++dev->open;
0272     mutex_unlock(&dev->lock);
0273 
0274     usb_dbg(interface, "open success");
0275     return 0;
0276 }
0277 
0278 static int chaoskey_release(struct inode *inode, struct file *file)
0279 {
0280     struct chaoskey *dev = file->private_data;
0281     struct usb_interface *interface;
0282 
0283     if (dev == NULL)
0284         return -ENODEV;
0285 
0286     interface = dev->interface;
0287 
0288     usb_dbg(interface, "release");
0289 
0290     mutex_lock(&dev->lock);
0291 
0292     usb_dbg(interface, "open count at release is %d", dev->open);
0293 
0294     if (dev->open <= 0) {
0295         usb_dbg(interface, "invalid open count (%d)", dev->open);
0296         mutex_unlock(&dev->lock);
0297         return -ENODEV;
0298     }
0299 
0300     --dev->open;
0301 
0302     if (!dev->present) {
0303         if (dev->open == 0) {
0304             mutex_unlock(&dev->lock);
0305             chaoskey_free(dev);
0306         } else
0307             mutex_unlock(&dev->lock);
0308     } else
0309         mutex_unlock(&dev->lock);
0310 
0311     usb_dbg(interface, "release success");
0312     return 0;
0313 }
0314 
0315 static void chaos_read_callback(struct urb *urb)
0316 {
0317     struct chaoskey *dev = urb->context;
0318     int status = urb->status;
0319 
0320     usb_dbg(dev->interface, "callback status (%d)", status);
0321 
0322     if (status == 0)
0323         dev->valid = urb->actual_length;
0324     else
0325         dev->valid = 0;
0326 
0327     dev->used = 0;
0328 
0329     /* must be seen first before validity is announced */
0330     smp_wmb();
0331 
0332     dev->reading = false;
0333     wake_up(&dev->wait_q);
0334 }
0335 
0336 /* Fill the buffer. Called with dev->lock held
0337  */
0338 static int _chaoskey_fill(struct chaoskey *dev)
0339 {
0340     DEFINE_WAIT(wait);
0341     int result;
0342     bool started;
0343 
0344     usb_dbg(dev->interface, "fill");
0345 
0346     /* Return immediately if someone called before the buffer was
0347      * empty */
0348     if (dev->valid != dev->used) {
0349         usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
0350             dev->valid, dev->used);
0351         return 0;
0352     }
0353 
0354     /* Bail if the device has been removed */
0355     if (!dev->present) {
0356         usb_dbg(dev->interface, "device not present");
0357         return -ENODEV;
0358     }
0359 
0360     /* Make sure the device is awake */
0361     result = usb_autopm_get_interface(dev->interface);
0362     if (result) {
0363         usb_dbg(dev->interface, "wakeup failed (result %d)", result);
0364         return result;
0365     }
0366 
0367     dev->reading = true;
0368     result = usb_submit_urb(dev->urb, GFP_KERNEL);
0369     if (result < 0) {
0370         result = usb_translate_errors(result);
0371         dev->reading = false;
0372         goto out;
0373     }
0374 
0375     /* The first read on the Alea takes a little under 2 seconds.
0376      * Reads after the first read take only a few microseconds
0377      * though.  Presumably the entropy-generating circuit needs
0378      * time to ramp up.  So, we wait longer on the first read.
0379      */
0380     started = dev->reads_started;
0381     dev->reads_started = true;
0382     result = wait_event_interruptible_timeout(
0383         dev->wait_q,
0384         !dev->reading,
0385         (started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) );
0386 
0387     if (result < 0) {
0388         usb_kill_urb(dev->urb);
0389         goto out;
0390     }
0391 
0392     if (result == 0) {
0393         result = -ETIMEDOUT;
0394         usb_kill_urb(dev->urb);
0395     } else {
0396         result = dev->valid;
0397     }
0398 out:
0399     /* Let the device go back to sleep eventually */
0400     usb_autopm_put_interface(dev->interface);
0401 
0402     usb_dbg(dev->interface, "read %d bytes", dev->valid);
0403 
0404     return result;
0405 }
0406 
0407 static ssize_t chaoskey_read(struct file *file,
0408                  char __user *buffer,
0409                  size_t count,
0410                  loff_t *ppos)
0411 {
0412     struct chaoskey *dev;
0413     ssize_t read_count = 0;
0414     int this_time;
0415     int result = 0;
0416     unsigned long remain;
0417 
0418     dev = file->private_data;
0419 
0420     if (dev == NULL || !dev->present)
0421         return -ENODEV;
0422 
0423     usb_dbg(dev->interface, "read %zu", count);
0424 
0425     while (count > 0) {
0426 
0427         /* Grab the rng_lock briefly to ensure that the hwrng interface
0428          * gets priority over other user access
0429          */
0430         result = mutex_lock_interruptible(&dev->rng_lock);
0431         if (result)
0432             goto bail;
0433         mutex_unlock(&dev->rng_lock);
0434 
0435         result = mutex_lock_interruptible(&dev->lock);
0436         if (result)
0437             goto bail;
0438         if (dev->valid == dev->used) {
0439             result = _chaoskey_fill(dev);
0440             if (result < 0) {
0441                 mutex_unlock(&dev->lock);
0442                 goto bail;
0443             }
0444         }
0445 
0446         this_time = dev->valid - dev->used;
0447         if (this_time > count)
0448             this_time = count;
0449 
0450         remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
0451         if (remain) {
0452             result = -EFAULT;
0453 
0454             /* Consume the bytes that were copied so we don't leak
0455              * data to user space
0456              */
0457             dev->used += this_time - remain;
0458             mutex_unlock(&dev->lock);
0459             goto bail;
0460         }
0461 
0462         count -= this_time;
0463         read_count += this_time;
0464         buffer += this_time;
0465         dev->used += this_time;
0466         mutex_unlock(&dev->lock);
0467     }
0468 bail:
0469     if (read_count) {
0470         usb_dbg(dev->interface, "read %zu bytes", read_count);
0471         return read_count;
0472     }
0473     usb_dbg(dev->interface, "empty read, result %d", result);
0474     if (result == -ETIMEDOUT)
0475         result = -EAGAIN;
0476     return result;
0477 }
0478 
0479 static int chaoskey_rng_read(struct hwrng *rng, void *data,
0480                  size_t max, bool wait)
0481 {
0482     struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
0483     int this_time;
0484 
0485     usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
0486 
0487     if (!dev->present) {
0488         usb_dbg(dev->interface, "device not present");
0489         return 0;
0490     }
0491 
0492     /* Hold the rng_lock until we acquire the device lock so that
0493      * this operation gets priority over other user access to the
0494      * device
0495      */
0496     mutex_lock(&dev->rng_lock);
0497 
0498     mutex_lock(&dev->lock);
0499 
0500     mutex_unlock(&dev->rng_lock);
0501 
0502     /* Try to fill the buffer if empty. It doesn't actually matter
0503      * if _chaoskey_fill works; we'll just return zero bytes as
0504      * the buffer will still be empty
0505      */
0506     if (dev->valid == dev->used)
0507         (void) _chaoskey_fill(dev);
0508 
0509     this_time = dev->valid - dev->used;
0510     if (this_time > max)
0511         this_time = max;
0512 
0513     memcpy(data, dev->buf + dev->used, this_time);
0514 
0515     dev->used += this_time;
0516 
0517     mutex_unlock(&dev->lock);
0518 
0519     usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
0520     return this_time;
0521 }
0522 
0523 #ifdef CONFIG_PM
0524 static int chaoskey_suspend(struct usb_interface *interface,
0525                 pm_message_t message)
0526 {
0527     usb_dbg(interface, "suspend");
0528     return 0;
0529 }
0530 
0531 static int chaoskey_resume(struct usb_interface *interface)
0532 {
0533     struct chaoskey *dev;
0534     struct usb_device *udev = interface_to_usbdev(interface);
0535 
0536     usb_dbg(interface, "resume");
0537     dev = usb_get_intfdata(interface);
0538 
0539     /*
0540      * We may have lost power.
0541      * In that case the device that needs a long time
0542      * for the first requests needs an extended timeout
0543      * again
0544      */
0545     if (le16_to_cpu(udev->descriptor.idVendor) == ALEA_VENDOR_ID)
0546         dev->reads_started = false;
0547 
0548     return 0;
0549 }
0550 #else
0551 #define chaoskey_suspend NULL
0552 #define chaoskey_resume NULL
0553 #endif
0554 
0555 /* file operation pointers */
0556 static const struct file_operations chaoskey_fops = {
0557     .owner = THIS_MODULE,
0558     .read = chaoskey_read,
0559     .open = chaoskey_open,
0560     .release = chaoskey_release,
0561     .llseek = default_llseek,
0562 };
0563 
0564 /* class driver information */
0565 static struct usb_class_driver chaoskey_class = {
0566     .name = "chaoskey%d",
0567     .fops = &chaoskey_fops,
0568     .minor_base = USB_CHAOSKEY_MINOR_BASE,
0569 };
0570 
0571 /* usb specific object needed to register this driver with the usb subsystem */
0572 static struct usb_driver chaoskey_driver = {
0573     .name = DRIVER_SHORT,
0574     .probe = chaoskey_probe,
0575     .disconnect = chaoskey_disconnect,
0576     .suspend = chaoskey_suspend,
0577     .resume = chaoskey_resume,
0578     .reset_resume = chaoskey_resume,
0579     .id_table = chaoskey_table,
0580     .supports_autosuspend = 1,
0581 };
0582 
0583 module_usb_driver(chaoskey_driver);
0584