[PATCH 1/2] hwrng: chaoskey - Add support for Araneus Alea I USB RNG

2016-06-03 Thread Bob Ham
Adds support for the Araneus Alea I USB hardware Random Number
Generator which is interfaced with in exactly the same way as the
Altus Metrum ChaosKey.  We just add the appropriate device ID and
modify the config help text.

Signed-off-by: Bob Ham <bob@collabora.com>
---
 drivers/usb/misc/Kconfig| 11 ++-
 drivers/usb/misc/chaoskey.c |  4 
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
index e9e5ae5..6e70597 100644
--- a/drivers/usb/misc/Kconfig
+++ b/drivers/usb/misc/Kconfig
@@ -260,11 +260,12 @@ config USB_CHAOSKEY
tristate "ChaosKey random number generator driver support"
depends on HW_RANDOM
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
+ Say Y here if you want to connect an AltusMetrum ChaosKey or
+ Araneus Alea I to your computer's USB port. These devices
+ are hardware random number generators which hook 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/chaoskey.c b/drivers/usb/misc/chaoskey.c
index 76350e4..9aef46b 100644
--- a/drivers/usb/misc/chaoskey.c
+++ b/drivers/usb/misc/chaoskey.c
@@ -55,6 +55,9 @@ MODULE_LICENSE("GPL");
 #define CHAOSKEY_VENDOR_ID 0x1d50  /* OpenMoko */
 #define CHAOSKEY_PRODUCT_ID0x60c6  /* ChaosKey */
 
+#define ALEA_VENDOR_ID 0x12d8  /* Araneus */
+#define ALEA_PRODUCT_ID0x0001  /* Alea I */
+
 #define CHAOSKEY_BUF_LEN   64  /* max size of USB full speed packet */
 
 #define NAK_TIMEOUT (HZ)   /* stall/wait timeout for device */
@@ -69,6 +72,7 @@ MODULE_LICENSE("GPL");
 
 static const struct usb_device_id chaoskey_table[] = {
{ USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
+   { USB_DEVICE(ALEA_VENDOR_ID, ALEA_PRODUCT_ID) },
{ },
 };
 MODULE_DEVICE_TABLE(usb, chaoskey_table);
-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 2/2] hwrng: chaoskey - Fix URB warning due to timeout on Alea

2016-06-03 Thread Bob Ham
The first read on an Alea takes about 1.8 seconds, more than the
timeout value waiting for the read.  As a consequence, later URB reuse
causes the warning given below.  To avoid this, we increase the wait
time for the first read on the Alea.

[   78.293247] WARNING: CPU: 3 PID: 1892 at drivers/usb/core/urb.c:338 
usb_submit_urb+0x2b4/0x580 [usbcore]
[   78.293250] URB 8802135be3c0 submitted while active
[   78.293252] Modules linked in: chaoskey(+) rng_core rfcomm binfmt_misc bnep 
cfg80211 nfsd auth_rpcgss oid_registry nfs_acl nfs lockd grace fscache sunrpc 
bridge stp llc tun snd_hda_codec_hdmi snd_hda_codec_realtek 
snd_hda_codec_generic iTCO_wdt iTCO_vendor_support nls_utf8 nls_cp437 vfat fat 
intel_rapl x86_pkg_temp_thermal intel_powerclamp coretemp kvm_intel efi_pstore 
kvm irqbypass pcspkr btusb btrtl btbcm btintel uvcvideo joydev bluetooth 
videobuf2_vmalloc videobuf2_memops efivars videobuf2_v4l2 serio_raw i2c_i801 
videobuf2_core videodev cdc_mbim media lpc_ich shpchp mfd_core cdc_ncm usbnet 
mii cdc_wdm cdc_acm evdev snd_hda_intel snd_hda_codec snd_hwdep snd_hda_core 
i915 snd_pcm snd_timer i2c_algo_bit drm_kms_helper wmi thinkpad_acpi drm nvram 
mei_me mei snd soundcore rfkill ac battery i2c_core
[   78.293335]  video button tpm_tis tpm fuse parport_pc ppdev lp parport 
autofs4 ext4 crc16 jbd2 mbcache algif_skcipher af_alg hid_generic usbhid hid 
dm_crypt dm_mod sg sr_mod cdrom sd_mod crct10dif_pclmul crc32_pclmul 
crc32c_intel jitterentropy_rng sha256_generic hmac drbg aesni_intel xhci_pci 
aes_x86_64 ahci glue_helper xhci_hcd ehci_pci lrw libahci gf128mul ablk_helper 
cryptd libata sdhci_pci psmouse sdhci scsi_mod ehci_hcd mmc_core usbcore 
usb_common thermal
[   78.293402] CPU: 3 PID: 1892 Comm: hwrng Not tainted 4.7.0-rc1-linux-14+ #16
[   78.293405] Hardware name: LENOVO 232577G/232577G, BIOS G2ET92WW (2.52 ) 
02/22/2013
[   78.293408]   812dfa0f 8801fa5b3d68 

[   78.293413]  81072224 8802135be3c0 8801fa5b3db8 
880212e44210
[   78.293418]  0040 880209fb32c0 880212e44200 
8107228f
[   78.293422] Call Trace:
[   78.293432]  [] ? dump_stack+0x5c/0x7d
[   78.293437]  [] ? __warn+0xc4/0xe0
[   78.293441]  [] ? warn_slowpath_fmt+0x4f/0x60
[   78.293451]  [] ? enqueue_task_fair+0xcd2/0x1260
[   78.293463]  [] ? usb_submit_urb+0x2b4/0x580 [usbcore]
[   78.293474]  [] ? __pm_runtime_resume+0x55/0x70
[   78.293484]  [] ? _chaoskey_fill+0x132/0x250 [chaoskey]
[   78.293485] usbcore: registered new interface driver chaoskey
[   78.293493]  [] ? wait_woken+0x90/0x90
[   78.293500]  [] ? devm_hwrng_register+0x80/0x80 [rng_core]
[   78.293505]  [] ? chaoskey_rng_read+0x127/0x140 [chaoskey]
[   78.293511]  [] ? devm_hwrng_register+0x80/0x80 [rng_core]
[   78.293515]  [] ? hwrng_fillfn+0x6e/0x120 [rng_core]
[   78.293520]  [] ? kthread+0xcf/0xf0
[   78.293529]  [] ? ret_from_fork+0x1f/0x40
[   78.293535]  [] ? kthread_park+0x50/0x50

Signed-off-by: Bob Ham <bob@collabora.com>
---
 drivers/usb/misc/chaoskey.c | 17 +++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/misc/chaoskey.c b/drivers/usb/misc/chaoskey.c
index 9aef46b..6ddd08a 100644
--- a/drivers/usb/misc/chaoskey.c
+++ b/drivers/usb/misc/chaoskey.c
@@ -60,7 +60,8 @@ MODULE_LICENSE("GPL");
 
 #define CHAOSKEY_BUF_LEN   64  /* max size of USB full speed packet */
 
-#define NAK_TIMEOUT (HZ)   /* stall/wait timeout for device */
+#define NAK_TIMEOUT (HZ)   /* normal stall/wait timeout */
+#define ALEA_FIRST_TIMEOUT (HZ*3)  /* first stall/wait timeout for Alea */
 
 #ifdef CONFIG_USB_DYNAMIC_MINORS
 #define USB_CHAOSKEY_MINOR_BASE 0
@@ -88,6 +89,7 @@ struct chaoskey {
int open;   /* open count */
bool present;   /* device not disconnected */
bool reading;   /* ongoing IO */
+   bool reads_started; /* track first read for Alea */
int size;   /* size of buf */
int valid;  /* bytes of buf read */
int used;   /* bytes of buf consumed */
@@ -192,6 +194,9 @@ static int chaoskey_probe(struct usb_interface *interface,
 
dev->in_ep = in_ep;
 
+   if (udev->descriptor.idVendor != ALEA_VENDOR_ID)
+   dev->reads_started = 1;
+
dev->size = size;
dev->present = 1;
 
@@ -361,6 +366,7 @@ static int _chaoskey_fill(struct chaoskey *dev)
 {
DEFINE_WAIT(wait);
int result;
+   bool started;
 
usb_dbg(dev->interface, "fill");
 
@@ -393,10 +399,17 @@ static int _chaoskey_fill(struct chaoskey *dev)
goto out;
}
 
+   /* The first read on the Alea takes a little under 2 seconds.
+* Reads after the first read take only a few microseconds
+* though.  Presumably the entropy-gene

[PATCH 0/2] hwrng: chaoskey - Add support for Araneus Alea I USB RNG

2016-06-03 Thread Bob Ham
Two patches to add support for the Araneus Alea I USB RNG to the
chaoskey driver.  The first simply includes the Alea I as a device,
the second fixes an issue with the timeout on the first read.

Bob Ham (2):
  hwrng: chaoskey - Add support for Araneus Alea I USB RNG
  hwrng: chaoskey - Fix URB warning due to timeout on Alea

 drivers/usb/misc/Kconfig| 11 ++-
 drivers/usb/misc/chaoskey.c | 21 +++--
 2 files changed, 25 insertions(+), 7 deletions(-)

-- 
2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH] hwrng: alea - Add support for Araneus Alea I USB RNG

2016-06-02 Thread Bob Ham
Adds support for the Araneus Alea I USB hardware Random Number
Generator.  This RNG creates entropy at a high rate, about 100kb/s.

Signed-off-by: Bob Ham <bob@collabora.com>
---

Just a note about the name of the module, I haven't append "-rng" to
the name, like every other module in hw_random, because those modules
contain drivers for the RNG part of some more complex device. By
contrast, the Alea is solely an RNG so adding "-rng" to the name seems
redundant.


 drivers/char/hw_random/Kconfig  |  13 ++
 drivers/char/hw_random/Makefile |   1 +
 drivers/char/hw_random/alea.c   | 370 
 3 files changed, 384 insertions(+)
 create mode 100644 drivers/char/hw_random/alea.c

diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index ac51149..b3f5a89 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -396,6 +396,19 @@ config HW_RANDOM_PIC32
 
  If unsure, say Y.
 
+config HW_RANDOM_ALEA
+   tristate "Araneus Alea I USB Random Number Generator support"
+   depends on HW_RANDOM && USB
+   default n
+   ---help---
+ This driver provides kernel-side support for the Araneus
+ Alea I USB hardware Random Number Generator.
+
+ To compile this driver as a module, choose M here. the
+ module will be called alea.
+
+ If unsure, say N.
+
 endif # HW_RANDOM
 
 config UML_RANDOM
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 63022b4..3709906 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -34,3 +34,4 @@ obj-$(CONFIG_HW_RANDOM_ST) += st-rng.o
 obj-$(CONFIG_HW_RANDOM_XGENE) += xgene-rng.o
 obj-$(CONFIG_HW_RANDOM_STM32) += stm32-rng.o
 obj-$(CONFIG_HW_RANDOM_PIC32) += pic32-rng.o
+obj-$(CONFIG_HW_RANDOM_ALEA) += alea.o
diff --git a/drivers/char/hw_random/alea.c b/drivers/char/hw_random/alea.c
new file mode 100644
index 000..50909da
--- /dev/null
+++ b/drivers/char/hw_random/alea.c
@@ -0,0 +1,370 @@
+/*
+ * Copyright (C) 2016 Collabora Ltd
+ * Written by Bob Ham <bob@collabora.com>
+ *
+ * An HWRNG driver to pull data from an Araneus Alea I
+ *
+ * derived from:
+ *
+ * USB Skeleton driver - 2.2
+ *
+ * Copyright (C) 2001-2004 Greg Kroah-Hartman (g...@kroah.com)
+ *
+ * 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.
+ *
+ * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
+ * but has been rewritten to be easier to read and use.
+ *
+ */
+
+/*
+ * The Alea I is a really simple device.  There is one bulk read
+ * endpoint.  It spits out data in 64-byte chunks.  Each chunk
+ * contains entropy.  Simple.
+ *
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+
+#define MODULE_NAME "alea"
+
+#define ARANEUS_VENDOR_ID  0x12d8
+#define ARANEUS_ALEA_I_PRODUCT_ID  0x0001
+
+/* table of devices that work with this driver */
+static const struct usb_device_id alea_table[] = {
+   { USB_DEVICE(ARANEUS_VENDOR_ID, ARANEUS_ALEA_I_PRODUCT_ID) },
+   { } /* Terminating entry */
+};
+
+MODULE_DEVICE_TABLE (usb, alea_table);
+
+
+/* Structure to hold all of our device specific stuff */
+struct alea {
+   struct usb_device   *udev;  /* the usb device for 
this device */
+   struct usb_interface*interface; /* the interface for 
this device */
+   struct urb  *bulk_in_urb;   /* the urb to read data 
with */
+   unsigned char   *bulk_in_buffer;/* the buffer to 
receive data */
+   size_t  bulk_in_size;   /* the size of the 
receive buffer */
+   size_t  bulk_in_filled; /* number of bytes in 
the buffer */
+   __u8bulk_in_endpointAddr;   /* the address of the 
bulk in endpoint */
+   int errors; /* the last request 
tanked */
+   boolongoing_read;   /* a read is going on */
+   spinlock_t  err_lock;   /* lock for errors */
+   struct kref kref;
+   struct mutexio_mutex;   /* synchronize I/O with 
disconnect */
+   wait_queue_head_t   bulk_in_wait;   /* to wait for an 
ongoing read */
+   char*rng_name;  /* name for the hwrng 
subsystem */
+   struct hwrngrng;/* the hwrng info */
+};
+#define kref_to_alea(d) container_of(d, struct alea, kref)
+#define rng_to_alea(d) container_of(d, struct alea, rng)
+
+static struct usb_driver alea_driver;
+
+static voi

Re: [PATCH] hwrng: alea - Add support for Araneus Alea I USB RNG

2016-06-02 Thread Bob Ham
On Thu, 2016-06-02 at 12:43 +0200, Clemens Ladisch wrote:
> Bob Ham wrote:
> > Adds support for the Araneus Alea I USB hardware Random Number
> > Generator.  This RNG creates entropy at a high rate, about 100kb/s.
> >
> > Signed-off-by: Bob Ham <bob@collabora.com>
> > ---
> >
> > +++ b/drivers/char/hw_random/alea.c
> 
> Why didn't you just add the device ID to chaoskey.c?
> (Because that one is hidden in drivers/usb/misc? ;-)

Argh!  Because that one is hidden in drivers/usb/misc!  Pfft :-)

Thanks.

-- 
Bob Ham <bob@collabora.com>
Software Engineer
Collabora

>>>>>>>>
Open First
Collabora is hiring!
Please check out our latest opportunities here:
http://bit.ly/Collabora-Careers
<<<<<<<<


--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html