4 * Copyright (C) 2008 Harrison Metzger <harrisonmetz@gmail.com>
5 * Based on usbled.c by Greg Kroah-Hartman (greg@kroah.com)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, version 2.
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/string.h>
19 #include <linux/usb.h>
22 #define DRIVER_AUTHOR "Harrison Metzger <harrisonmetz@gmail.com>"
23 #define DRIVER_DESC "USB 7 Segment Driver"
25 #define VENDOR_ID 0x0fc5
26 #define PRODUCT_ID 0x1227
29 /* table of devices that work with this driver */
30 static struct usb_device_id id_table[] = {
31 { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
34 MODULE_DEVICE_TABLE(usb, id_table);
36 /* the different text display modes the device is capable of */
37 static char *display_textmodes[] = {"raw", "hex", "ascii", NULL};
39 struct usb_sevsegdev {
40 struct usb_device *udev;
51 /* sysfs_streq can't replace this completely
52 * If the device was in hex mode, and the user wanted a 0,
53 * if str commands are used, we would assume the end of string
54 * so mem commands are used.
56 inline size_t my_memlen(const char *buf, size_t count)
58 if (count > 0 && buf[count-1] == '\n')
64 static void update_display_powered(struct usb_sevsegdev *mydev)
68 rc = usb_control_msg(mydev->udev,
69 usb_sndctrlpipe(mydev->udev, 0),
72 (80 * 0x100) + 10, /* (power mode) */
73 (0x00 * 0x100) + (mydev->powered ? 1 : 0),
78 dev_dbg(&mydev->udev->dev, "power retval = %d\n", rc);
81 static void update_display_mode(struct usb_sevsegdev *mydev)
85 rc = usb_control_msg(mydev->udev,
86 usb_sndctrlpipe(mydev->udev, 0),
89 (82 * 0x100) + 10, /* (set mode) */
90 (mydev->mode_msb * 0x100) + mydev->mode_lsb,
96 dev_dbg(&mydev->udev->dev, "mode retval = %d\n", rc);
99 static void update_display_visual(struct usb_sevsegdev *mydev)
103 unsigned char *buffer;
106 buffer = kzalloc(MAXLEN, GFP_KERNEL);
108 dev_err(&mydev->udev->dev, "out of memory\n");
112 /* The device is right to left, where as you write left to right */
113 for (i = 0; i < mydev->textlength; i++)
114 buffer[i] = mydev->text[mydev->textlength-1-i];
116 rc = usb_control_msg(mydev->udev,
117 usb_sndctrlpipe(mydev->udev, 0),
120 (85 * 0x100) + 10, /* (write text) */
121 (0 * 0x100) + mydev->textmode, /* mode */
127 dev_dbg(&mydev->udev->dev, "write retval = %d\n", rc);
131 /* The device is right to left, where as you write left to right */
132 for (i = 0; i < sizeof(mydev->decimals); i++)
133 decimals |= mydev->decimals[i] << i;
135 rc = usb_control_msg(mydev->udev,
136 usb_sndctrlpipe(mydev->udev, 0),
139 (86 * 0x100) + 10, /* (set decimal) */
140 (0 * 0x100) + decimals, /* decimals */
146 dev_dbg(&mydev->udev->dev, "decimal retval = %d\n", rc);
149 #define MYDEV_ATTR_SIMPLE_UNSIGNED(name, update_fcn) \
150 static ssize_t show_attr_##name(struct device *dev, \
151 struct device_attribute *attr, char *buf) \
153 struct usb_interface *intf = to_usb_interface(dev); \
154 struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
156 return sprintf(buf, "%u\n", mydev->name); \
159 static ssize_t set_attr_##name(struct device *dev, \
160 struct device_attribute *attr, const char *buf, size_t count) \
162 struct usb_interface *intf = to_usb_interface(dev); \
163 struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
165 mydev->name = simple_strtoul(buf, NULL, 10); \
170 static DEVICE_ATTR(name, S_IWUGO | S_IRUGO, show_attr_##name, set_attr_##name);
172 static ssize_t show_attr_text(struct device *dev,
173 struct device_attribute *attr, char *buf)
175 struct usb_interface *intf = to_usb_interface(dev);
176 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
178 return snprintf(buf, mydev->textlength, "%s\n", mydev->text);
181 static ssize_t set_attr_text(struct device *dev,
182 struct device_attribute *attr, const char *buf, size_t count)
184 struct usb_interface *intf = to_usb_interface(dev);
185 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
186 size_t end = my_memlen(buf, count);
188 if (end > sizeof(mydev->text))
191 memset(mydev->text, 0, sizeof(mydev->text));
192 mydev->textlength = end;
195 memcpy(mydev->text, buf, end);
197 update_display_visual(mydev);
201 static DEVICE_ATTR(text, S_IWUGO | S_IRUGO, show_attr_text, set_attr_text);
203 static ssize_t show_attr_decimals(struct device *dev,
204 struct device_attribute *attr, char *buf)
206 struct usb_interface *intf = to_usb_interface(dev);
207 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
211 for (i = 0; i < sizeof(mydev->decimals); i++) {
212 pos = sizeof(mydev->decimals) - 1 - i;
213 if (mydev->decimals[i] == 0)
215 else if (mydev->decimals[i] == 1)
221 buf[sizeof(mydev->decimals)] = '\n';
222 return sizeof(mydev->decimals) + 1;
225 static ssize_t set_attr_decimals(struct device *dev,
226 struct device_attribute *attr, const char *buf, size_t count)
228 struct usb_interface *intf = to_usb_interface(dev);
229 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
230 size_t end = my_memlen(buf, count);
233 if (end > sizeof(mydev->decimals))
236 for (i = 0; i < end; i++)
237 if (buf[i] != '0' && buf[i] != '1')
240 memset(mydev->decimals, 0, sizeof(mydev->decimals));
241 for (i = 0; i < end; i++)
243 mydev->decimals[end-1-i] = 1;
245 update_display_visual(mydev);
250 static DEVICE_ATTR(decimals, S_IWUGO | S_IRUGO,
251 show_attr_decimals, set_attr_decimals);
253 static ssize_t show_attr_textmode(struct device *dev,
254 struct device_attribute *attr, char *buf)
256 struct usb_interface *intf = to_usb_interface(dev);
257 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
262 for (i = 0; display_textmodes[i]; i++) {
263 if (mydev->textmode == i) {
265 strcat(buf, display_textmodes[i]);
269 strcat(buf, display_textmodes[i]);
279 static ssize_t set_attr_textmode(struct device *dev,
280 struct device_attribute *attr, const char *buf, size_t count)
282 struct usb_interface *intf = to_usb_interface(dev);
283 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
286 for (i = 0; display_textmodes[i]; i++) {
287 if (sysfs_streq(display_textmodes[i], buf)) {
289 update_display_visual(mydev);
297 static DEVICE_ATTR(textmode, S_IWUGO | S_IRUGO,
298 show_attr_textmode, set_attr_textmode);
301 MYDEV_ATTR_SIMPLE_UNSIGNED(powered, update_display_powered);
302 MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb, update_display_mode);
303 MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb, update_display_mode);
305 static struct attribute *dev_attrs[] = {
306 &dev_attr_powered.attr,
308 &dev_attr_textmode.attr,
309 &dev_attr_decimals.attr,
310 &dev_attr_mode_msb.attr,
311 &dev_attr_mode_lsb.attr,
315 static struct attribute_group dev_attr_grp = {
319 static int sevseg_probe(struct usb_interface *interface,
320 const struct usb_device_id *id)
322 struct usb_device *udev = interface_to_usbdev(interface);
323 struct usb_sevsegdev *mydev = NULL;
326 mydev = kzalloc(sizeof(struct usb_sevsegdev), GFP_KERNEL);
328 dev_err(&interface->dev, "Out of memory\n");
332 mydev->udev = usb_get_dev(udev);
333 usb_set_intfdata(interface, mydev);
336 mydev->textmode = 0x02; /* ascii mode */
337 mydev->mode_msb = 0x06; /* 6 characters */
338 mydev->mode_lsb = 0x3f; /* scanmode for 6 chars */
340 rc = sysfs_create_group(&interface->dev.kobj, &dev_attr_grp);
344 dev_info(&interface->dev, "USB 7 Segment device now attached\n");
348 usb_set_intfdata(interface, NULL);
349 usb_put_dev(mydev->udev);
355 static void sevseg_disconnect(struct usb_interface *interface)
357 struct usb_sevsegdev *mydev;
359 mydev = usb_get_intfdata(interface);
360 sysfs_remove_group(&interface->dev.kobj, &dev_attr_grp);
361 usb_set_intfdata(interface, NULL);
362 usb_put_dev(mydev->udev);
364 dev_info(&interface->dev, "USB 7 Segment now disconnected\n");
367 static struct usb_driver sevseg_driver = {
369 .probe = sevseg_probe,
370 .disconnect = sevseg_disconnect,
371 .id_table = id_table,
374 static int __init usb_sevseg_init(void)
378 rc = usb_register(&sevseg_driver);
380 err("usb_register failed. Error number %d", rc);
384 static void __exit usb_sevseg_exit(void)
386 usb_deregister(&sevseg_driver);
389 module_init(usb_sevseg_init);
390 module_exit(usb_sevseg_exit);
392 MODULE_AUTHOR(DRIVER_AUTHOR);
393 MODULE_DESCRIPTION(DRIVER_DESC);
394 MODULE_LICENSE("GPL");