USB: Added driver for a Delcom USB 7-segment LED Display
Harrison Metzger [Thu, 14 Aug 2008 16:29:32 +0000 (11:29 -0500)]
Added basic support for a Delcom USB 7-segment LED Display

Signed-off by: Harrison Metzger <harrisonmetz@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

Documentation/ABI/testing/sysfs-bus-usb-devices-usbsevseg [new file with mode: 0644]
Documentation/usb/misc_usbsevseg.txt [new file with mode: 0644]
drivers/usb/misc/Kconfig
drivers/usb/misc/Makefile
drivers/usb/misc/usbsevseg.c [new file with mode: 0644]

diff --git a/Documentation/ABI/testing/sysfs-bus-usb-devices-usbsevseg b/Documentation/ABI/testing/sysfs-bus-usb-devices-usbsevseg
new file mode 100644 (file)
index 0000000..cb830df
--- /dev/null
@@ -0,0 +1,43 @@
+Where:         /sys/bus/usb/.../powered
+Date:          August 2008
+Kernel Version:        2.6.26
+Contact:       Harrison Metzger <harrisonmetz@gmail.com>
+Description:   Controls whether the device's display will powered.
+               A value of 0 is off and a non-zero value is on.
+
+Where:         /sys/bus/usb/.../mode_msb
+Where:         /sys/bus/usb/.../mode_lsb
+Date:          August 2008
+Kernel Version:        2.6.26
+Contact:       Harrison Metzger <harrisonmetz@gmail.com>
+Description:   Controls the devices display mode.
+               For a 6 character display the values are
+                       MSB 0x06; LSB 0x3F, and
+               for an 8 character display the values are
+                       MSB 0x08; LSB 0xFF.
+
+Where:         /sys/bus/usb/.../textmode
+Date:          August 2008
+Kernel Version:        2.6.26
+Contact:       Harrison Metzger <harrisonmetz@gmail.com>
+Description:   Controls the way the device interprets its text buffer.
+               raw:    each character controls its segment manually
+               hex:    each character is between 0-15
+               ascii:  each character is between '0'-'9' and 'A'-'F'.
+
+Where:         /sys/bus/usb/.../text
+Date:          August 2008
+Kernel Version:        2.6.26
+Contact:       Harrison Metzger <harrisonmetz@gmail.com>
+Description:   The text (or data) for the device to display
+
+Where:         /sys/bus/usb/.../decimals
+Date:          August 2008
+Kernel Version:        2.6.26
+Contact:       Harrison Metzger <harrisonmetz@gmail.com>
+Description:   Controls the decimal places on the device.
+               To set the nth decimal place, give this field
+               the value of 10 ** n. Assume this field has
+               the value k and has 1 or more decimal places set,
+               to set the mth place (where m is not already set),
+               change this fields value to k + 10 ** m.
\ No newline at end of file
diff --git a/Documentation/usb/misc_usbsevseg.txt b/Documentation/usb/misc_usbsevseg.txt
new file mode 100644 (file)
index 0000000..0f6be4f
--- /dev/null
@@ -0,0 +1,46 @@
+USB 7-Segment Numeric Display
+Manufactured by Delcom Engineering
+
+Device Information
+------------------
+USB VENDOR_ID  0x0fc5
+USB PRODUCT_ID 0x1227
+Both the 6 character and 8 character displays have PRODUCT_ID,
+and according to Delcom Engineering no queryable information
+can be obtained from the device to tell them apart.
+
+Device Modes
+------------
+By default, the driver assumes the display is only 6 characters
+The mode for 6 characters is:
+       MSB 0x06; LSB 0x3f
+For the 8 character display:
+       MSB 0x08; LSB 0xff
+The device can accept "text" either in raw, hex, or ascii textmode.
+raw controls each segment manually,
+hex expects a value between 0-15 per character,
+ascii expects a value between '0'-'9' and 'A'-'F'.
+The default is ascii.
+
+Device Operation
+----------------
+1.     Turn on the device:
+       echo 1 > /sys/bus/usb/.../powered
+2.     Set the device's mode:
+       echo $mode_msb > /sys/bus/usb/.../mode_msb
+       echo $mode_lsb > /sys/bus/usb/.../mode_lsb
+3.     Set the textmode:
+       echo $textmode > /sys/bus/usb/.../textmode
+4.     set the text (for example):
+       echo "123ABC" > /sys/bus/usb/.../text (ascii)
+       echo "A1B2" > /sys/bus/usb/.../text (ascii)
+       echo -ne "\x01\x02\x03" > /sys/bus/usb/.../text (hex)
+5.     Set the decimal places.
+       The device has either 6 or 8 decimal points.
+       to set the nth decimal place calculate 10 ** n
+       and echo it in to /sys/bus/usb/.../decimals
+       To set multiple decimals points sum up each power.
+       For example, to set the 0th and 3rd decimal place
+       echo 1001 > /sys/bus/usb/.../decimals
+
+
index 4ea50e0..25e1157 100644 (file)
@@ -42,6 +42,15 @@ config USB_ADUTUX
          To compile this driver as a module, choose M here.  The module
          will be called adutux.
 
+config USB_SEVSEG
+       tristate "USB 7-Segment LED Display"
+       depends on USB
+       help
+         Say Y here if you have a USB 7-Segment Display by Delcom
+
+         To compile this driver as a module, choose M here: the
+         module will be called usbsevseg.
+
 config USB_RIO500
        tristate "USB Diamond Rio500 support"
        depends on USB
index 45b4e12..39ce4a1 100644 (file)
@@ -26,6 +26,7 @@ obj-$(CONFIG_USB_RIO500)      += rio500.o
 obj-$(CONFIG_USB_TEST)         += usbtest.o
 obj-$(CONFIG_USB_TRANCEVIBRATOR)       += trancevibrator.o
 obj-$(CONFIG_USB_USS720)       += uss720.o
+obj-$(CONFIG_USB_SEVSEG)       += usbsevseg.o
 
 obj-$(CONFIG_USB_SISUSBVGA)    += sisusbvga/
 
diff --git a/drivers/usb/misc/usbsevseg.c b/drivers/usb/misc/usbsevseg.c
new file mode 100644 (file)
index 0000000..28a6a3a
--- /dev/null
@@ -0,0 +1,394 @@
+/*
+ * USB 7 Segment Driver
+ *
+ * Copyright (C) 2008 Harrison Metzger <harrisonmetz@gmail.com>
+ * Based on usbled.c by Greg Kroah-Hartman (greg@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.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/string.h>
+#include <linux/usb.h>
+
+
+#define DRIVER_AUTHOR "Harrison Metzger <harrisonmetz@gmail.com>"
+#define DRIVER_DESC "USB 7 Segment Driver"
+
+#define VENDOR_ID      0x0fc5
+#define PRODUCT_ID     0x1227
+#define MAXLEN         6
+
+/* table of devices that work with this driver */
+static struct usb_device_id id_table[] = {
+       { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
+       { },
+};
+MODULE_DEVICE_TABLE(usb, id_table);
+
+/* the different text display modes the device is capable of */
+static char *display_textmodes[] = {"raw", "hex", "ascii", NULL};
+
+struct usb_sevsegdev {
+       struct usb_device *udev;
+
+       u8 powered;
+       u8 mode_msb;
+       u8 mode_lsb;
+       u8 decimals[MAXLEN];
+       u8 textmode;
+       u8 text[MAXLEN];
+       u16 textlength;
+};
+
+/* sysfs_streq can't replace this completely
+ * If the device was in hex mode, and the user wanted a 0,
+ * if str commands are used, we would assume the end of string
+ * so mem commands are used.
+ */
+inline size_t my_memlen(const char *buf, size_t count)
+{
+       if (count > 0 && buf[count-1] == '\n')
+               return count - 1;
+       else
+               return count;
+}
+
+static void update_display_powered(struct usb_sevsegdev *mydev)
+{
+       int rc;
+
+       rc = usb_control_msg(mydev->udev,
+                       usb_sndctrlpipe(mydev->udev, 0),
+                       0x12,
+                       0x48,
+                       (80 * 0x100) + 10, /*  (power mode) */
+                       (0x00 * 0x100) + (mydev->powered ? 1 : 0),
+                       NULL,
+                       0,
+                       2000);
+       if (rc < 0)
+               dev_dbg(&mydev->udev->dev, "power retval = %d\n", rc);
+}
+
+static void update_display_mode(struct usb_sevsegdev *mydev)
+{
+       int rc;
+
+       rc = usb_control_msg(mydev->udev,
+                       usb_sndctrlpipe(mydev->udev, 0),
+                       0x12,
+                       0x48,
+                       (82 * 0x100) + 10, /* (set mode) */
+                       (mydev->mode_msb * 0x100) + mydev->mode_lsb,
+                       NULL,
+                       0,
+                       2000);
+
+       if (rc < 0)
+               dev_dbg(&mydev->udev->dev, "mode retval = %d\n", rc);
+}
+
+static void update_display_visual(struct usb_sevsegdev *mydev)
+{
+       int rc;
+       int i;
+       unsigned char *buffer;
+       u8 decimals = 0;
+
+       buffer = kzalloc(MAXLEN, GFP_KERNEL);
+       if (!buffer) {
+               dev_err(&mydev->udev->dev, "out of memory\n");
+               return;
+       }
+
+       /* The device is right to left, where as you write left to right */
+       for (i = 0; i < mydev->textlength; i++)
+               buffer[i] = mydev->text[mydev->textlength-1-i];
+
+       rc = usb_control_msg(mydev->udev,
+                       usb_sndctrlpipe(mydev->udev, 0),
+                       0x12,
+                       0x48,
+                       (85 * 0x100) + 10, /* (write text) */
+                       (0 * 0x100) + mydev->textmode, /* mode  */
+                       buffer,
+                       mydev->textlength,
+                       2000);
+
+       if (rc < 0)
+               dev_dbg(&mydev->udev->dev, "write retval = %d\n", rc);
+
+       kfree(buffer);
+
+       /* The device is right to left, where as you write left to right */
+       for (i = 0; i < sizeof(mydev->decimals); i++)
+               decimals |= mydev->decimals[i] << i;
+
+       rc = usb_control_msg(mydev->udev,
+                       usb_sndctrlpipe(mydev->udev, 0),
+                       0x12,
+                       0x48,
+                       (86 * 0x100) + 10, /* (set decimal) */
+                       (0 * 0x100) + decimals, /* decimals */
+                       NULL,
+                       0,
+                       2000);
+
+       if (rc < 0)
+               dev_dbg(&mydev->udev->dev, "decimal retval = %d\n", rc);
+}
+
+#define MYDEV_ATTR_SIMPLE_UNSIGNED(name, update_fcn)           \
+static ssize_t show_attr_##name(struct device *dev,            \
+       struct device_attribute *attr, char *buf)               \
+{                                                              \
+       struct usb_interface *intf = to_usb_interface(dev);     \
+       struct usb_sevsegdev *mydev = usb_get_intfdata(intf);   \
+                                                               \
+       return sprintf(buf, "%u\n", mydev->name);               \
+}                                                              \
+                                                               \
+static ssize_t set_attr_##name(struct device *dev,             \
+       struct device_attribute *attr, const char *buf, size_t count) \
+{                                                              \
+       struct usb_interface *intf = to_usb_interface(dev);     \
+       struct usb_sevsegdev *mydev = usb_get_intfdata(intf);   \
+                                                               \
+       mydev->name = simple_strtoul(buf, NULL, 10);            \
+       update_fcn(mydev); \
+                                                               \
+       return count;                                           \
+}                                                              \
+static DEVICE_ATTR(name, S_IWUGO | S_IRUGO, show_attr_##name, set_attr_##name);
+
+static ssize_t show_attr_text(struct device *dev,
+       struct device_attribute *attr, char *buf)
+{
+       struct usb_interface *intf = to_usb_interface(dev);
+       struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
+
+       return snprintf(buf, mydev->textlength, "%s\n", mydev->text);
+}
+
+static ssize_t set_attr_text(struct device *dev,
+       struct device_attribute *attr, const char *buf, size_t count)
+{
+       struct usb_interface *intf = to_usb_interface(dev);
+       struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
+       size_t end = my_memlen(buf, count);
+
+       if (end > sizeof(mydev->text))
+               return -EINVAL;
+
+       memset(mydev->text, 0, sizeof(mydev->text));
+       mydev->textlength = end;
+
+       if (end > 0)
+               memcpy(mydev->text, buf, end);
+
+       update_display_visual(mydev);
+       return count;
+}
+
+static DEVICE_ATTR(text, S_IWUGO | S_IRUGO, show_attr_text, set_attr_text);
+
+static ssize_t show_attr_decimals(struct device *dev,
+       struct device_attribute *attr, char *buf)
+{
+       struct usb_interface *intf = to_usb_interface(dev);
+       struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
+       int i;
+       int pos;
+
+       for (i = 0; i < sizeof(mydev->decimals); i++) {
+               pos = sizeof(mydev->decimals) - 1 - i;
+               if (mydev->decimals[i] == 0)
+                       buf[pos] = '0';
+               else if (mydev->decimals[i] == 1)
+                       buf[pos] = '1';
+               else
+                       buf[pos] = 'x';
+       }
+
+       buf[sizeof(mydev->decimals)] = '\n';
+       return sizeof(mydev->decimals) + 1;
+}
+
+static ssize_t set_attr_decimals(struct device *dev,
+       struct device_attribute *attr, const char *buf, size_t count)
+{
+       struct usb_interface *intf = to_usb_interface(dev);
+       struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
+       size_t end = my_memlen(buf, count);
+       int i;
+
+       if (end > sizeof(mydev->decimals))
+               return -EINVAL;
+
+       for (i = 0; i < end; i++)
+               if (buf[i] != '0' && buf[i] != '1')
+                       return -EINVAL;
+
+       memset(mydev->decimals, 0, sizeof(mydev->decimals));
+       for (i = 0; i < end; i++)
+               if (buf[i] == '1')
+                       mydev->decimals[end-1-i] = 1;
+
+       update_display_visual(mydev);
+
+       return count;
+}
+
+static DEVICE_ATTR(decimals, S_IWUGO | S_IRUGO,
+       show_attr_decimals, set_attr_decimals);
+
+static ssize_t show_attr_textmode(struct device *dev,
+       struct device_attribute *attr, char *buf)
+{
+       struct usb_interface *intf = to_usb_interface(dev);
+       struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
+       int i;
+
+       buf[0] = 0;
+
+       for (i = 0; display_textmodes[i]; i++) {
+               if (mydev->textmode == i) {
+                       strcat(buf, " [");
+                       strcat(buf, display_textmodes[i]);
+                       strcat(buf, "] ");
+               } else {
+                       strcat(buf, " ");
+                       strcat(buf, display_textmodes[i]);
+                       strcat(buf, " ");
+               }
+       }
+       strcat(buf, "\n");
+
+
+       return strlen(buf);
+}
+
+static ssize_t set_attr_textmode(struct device *dev,
+       struct device_attribute *attr, const char *buf, size_t count)
+{
+       struct usb_interface *intf = to_usb_interface(dev);
+       struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
+       int i;
+
+       for (i = 0; display_textmodes[i]; i++) {
+               if (sysfs_streq(display_textmodes[i], buf)) {
+                       mydev->textmode = i;
+                       update_display_visual(mydev);
+                       return count;
+               }
+       }
+
+       return -EINVAL;
+}
+
+static DEVICE_ATTR(textmode, S_IWUGO | S_IRUGO,
+       show_attr_textmode, set_attr_textmode);
+
+
+MYDEV_ATTR_SIMPLE_UNSIGNED(powered, update_display_powered);
+MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb, update_display_mode);
+MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb, update_display_mode);
+
+static struct attribute *dev_attrs[] = {
+       &dev_attr_powered.attr,
+       &dev_attr_text.attr,
+       &dev_attr_textmode.attr,
+       &dev_attr_decimals.attr,
+       &dev_attr_mode_msb.attr,
+       &dev_attr_mode_lsb.attr,
+       NULL
+};
+
+static struct attribute_group dev_attr_grp = {
+       .attrs = dev_attrs,
+};
+
+static int sevseg_probe(struct usb_interface *interface,
+       const struct usb_device_id *id)
+{
+       struct usb_device *udev = interface_to_usbdev(interface);
+       struct usb_sevsegdev *mydev = NULL;
+       int rc = -ENOMEM;
+
+       mydev = kzalloc(sizeof(struct usb_sevsegdev), GFP_KERNEL);
+       if (mydev == NULL) {
+               dev_err(&interface->dev, "Out of memory\n");
+               goto error_mem;
+       }
+
+       mydev->udev = usb_get_dev(udev);
+       usb_set_intfdata(interface, mydev);
+
+       /*set defaults */
+       mydev->textmode = 0x02; /* ascii mode */
+       mydev->mode_msb = 0x06; /* 6 characters */
+       mydev->mode_lsb = 0x3f; /* scanmode for 6 chars */
+
+       rc = sysfs_create_group(&interface->dev.kobj, &dev_attr_grp);
+       if (rc)
+               goto error;
+
+       dev_info(&interface->dev, "USB 7 Segment device now attached\n");
+       return 0;
+
+error:
+       usb_set_intfdata(interface, NULL);
+       usb_put_dev(mydev->udev);
+       kfree(mydev);
+error_mem:
+       return rc;
+}
+
+static void sevseg_disconnect(struct usb_interface *interface)
+{
+       struct usb_sevsegdev *mydev;
+
+       mydev = usb_get_intfdata(interface);
+       sysfs_remove_group(&interface->dev.kobj, &dev_attr_grp);
+       usb_set_intfdata(interface, NULL);
+       usb_put_dev(mydev->udev);
+       kfree(mydev);
+       dev_info(&interface->dev, "USB 7 Segment now disconnected\n");
+}
+
+static struct usb_driver sevseg_driver = {
+       .name =         "usbsevseg",
+       .probe =        sevseg_probe,
+       .disconnect =   sevseg_disconnect,
+       .id_table =     id_table,
+};
+
+static int __init usb_sevseg_init(void)
+{
+       int rc = 0;
+
+       rc = usb_register(&sevseg_driver);
+       if (rc)
+               err("usb_register failed. Error number %d", rc);
+       return rc;
+}
+
+static void __exit usb_sevseg_exit(void)
+{
+       usb_deregister(&sevseg_driver);
+}
+
+module_init(usb_sevseg_init);
+module_exit(usb_sevseg_exit);
+
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL");