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/slab.h>
16 #include <linux/module.h>
17 #include <linux/string.h>
18 #include <linux/usb.h>
21 #define DRIVER_AUTHOR "Harrison Metzger <harrisonmetz@gmail.com>"
22 #define DRIVER_DESC "USB 7 Segment Driver"
24 #define VENDOR_ID 0x0fc5
25 #define PRODUCT_ID 0x1227
28 /* table of devices that work with this driver */
29 static const struct usb_device_id id_table
[] = {
30 { USB_DEVICE(VENDOR_ID
, PRODUCT_ID
) },
33 MODULE_DEVICE_TABLE(usb
, id_table
);
35 /* the different text display modes the device is capable of */
36 static const char *display_textmodes
[] = {"raw", "hex", "ascii"};
38 struct usb_sevsegdev
{
39 struct usb_device
*udev
;
40 struct usb_interface
*intf
;
50 u8 shadow_power
; /* for PM */
54 /* sysfs_streq can't replace this completely
55 * If the device was in hex mode, and the user wanted a 0,
56 * if str commands are used, we would assume the end of string
57 * so mem commands are used.
59 static inline size_t my_memlen(const char *buf
, size_t count
)
61 if (count
> 0 && buf
[count
-1] == '\n')
67 static void update_display_powered(struct usb_sevsegdev
*mydev
)
71 if (mydev
->powered
&& !mydev
->has_interface_pm
) {
72 rc
= usb_autopm_get_interface(mydev
->intf
);
75 mydev
->has_interface_pm
= 1;
78 if (mydev
->shadow_power
!= 1)
81 rc
= usb_control_msg(mydev
->udev
,
82 usb_sndctrlpipe(mydev
->udev
, 0),
85 (80 * 0x100) + 10, /* (power mode) */
86 (0x00 * 0x100) + (mydev
->powered
? 1 : 0),
91 dev_dbg(&mydev
->udev
->dev
, "power retval = %d\n", rc
);
93 if (!mydev
->powered
&& mydev
->has_interface_pm
) {
94 usb_autopm_put_interface(mydev
->intf
);
95 mydev
->has_interface_pm
= 0;
99 static void update_display_mode(struct usb_sevsegdev
*mydev
)
103 if(mydev
->shadow_power
!= 1)
106 rc
= usb_control_msg(mydev
->udev
,
107 usb_sndctrlpipe(mydev
->udev
, 0),
110 (82 * 0x100) + 10, /* (set mode) */
111 (mydev
->mode_msb
* 0x100) + mydev
->mode_lsb
,
117 dev_dbg(&mydev
->udev
->dev
, "mode retval = %d\n", rc
);
120 static void update_display_visual(struct usb_sevsegdev
*mydev
, gfp_t mf
)
124 unsigned char *buffer
;
127 if(mydev
->shadow_power
!= 1)
130 buffer
= kzalloc(MAXLEN
, mf
);
134 /* The device is right to left, where as you write left to right */
135 for (i
= 0; i
< mydev
->textlength
; i
++)
136 buffer
[i
] = mydev
->text
[mydev
->textlength
-1-i
];
138 rc
= usb_control_msg(mydev
->udev
,
139 usb_sndctrlpipe(mydev
->udev
, 0),
142 (85 * 0x100) + 10, /* (write text) */
143 (0 * 0x100) + mydev
->textmode
, /* mode */
149 dev_dbg(&mydev
->udev
->dev
, "write retval = %d\n", rc
);
153 /* The device is right to left, where as you write left to right */
154 for (i
= 0; i
< sizeof(mydev
->decimals
); i
++)
155 decimals
|= mydev
->decimals
[i
] << i
;
157 rc
= usb_control_msg(mydev
->udev
,
158 usb_sndctrlpipe(mydev
->udev
, 0),
161 (86 * 0x100) + 10, /* (set decimal) */
162 (0 * 0x100) + decimals
, /* decimals */
168 dev_dbg(&mydev
->udev
->dev
, "decimal retval = %d\n", rc
);
171 #define MYDEV_ATTR_SIMPLE_UNSIGNED(name, update_fcn) \
172 static ssize_t show_attr_##name(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 sprintf(buf, "%u\n", mydev->name); \
181 static ssize_t set_attr_##name(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); \
187 mydev->name = simple_strtoul(buf, NULL, 10); \
192 static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show_attr_##name, set_attr_##name);
194 static ssize_t
show_attr_text(struct device
*dev
,
195 struct device_attribute
*attr
, char *buf
)
197 struct usb_interface
*intf
= to_usb_interface(dev
);
198 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
200 return snprintf(buf
, mydev
->textlength
, "%s\n", mydev
->text
);
203 static ssize_t
set_attr_text(struct device
*dev
,
204 struct device_attribute
*attr
, const char *buf
, size_t count
)
206 struct usb_interface
*intf
= to_usb_interface(dev
);
207 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
208 size_t end
= my_memlen(buf
, count
);
210 if (end
> sizeof(mydev
->text
))
213 memset(mydev
->text
, 0, sizeof(mydev
->text
));
214 mydev
->textlength
= end
;
217 memcpy(mydev
->text
, buf
, end
);
219 update_display_visual(mydev
, GFP_KERNEL
);
223 static DEVICE_ATTR(text
, S_IRUGO
| S_IWUSR
, show_attr_text
, set_attr_text
);
225 static ssize_t
show_attr_decimals(struct device
*dev
,
226 struct device_attribute
*attr
, char *buf
)
228 struct usb_interface
*intf
= to_usb_interface(dev
);
229 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
233 for (i
= 0; i
< sizeof(mydev
->decimals
); i
++) {
234 pos
= sizeof(mydev
->decimals
) - 1 - i
;
235 if (mydev
->decimals
[i
] == 0)
237 else if (mydev
->decimals
[i
] == 1)
243 buf
[sizeof(mydev
->decimals
)] = '\n';
244 return sizeof(mydev
->decimals
) + 1;
247 static ssize_t
set_attr_decimals(struct device
*dev
,
248 struct device_attribute
*attr
, const char *buf
, size_t count
)
250 struct usb_interface
*intf
= to_usb_interface(dev
);
251 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
252 size_t end
= my_memlen(buf
, count
);
255 if (end
> sizeof(mydev
->decimals
))
258 for (i
= 0; i
< end
; i
++)
259 if (buf
[i
] != '0' && buf
[i
] != '1')
262 memset(mydev
->decimals
, 0, sizeof(mydev
->decimals
));
263 for (i
= 0; i
< end
; i
++)
265 mydev
->decimals
[end
-1-i
] = 1;
267 update_display_visual(mydev
, GFP_KERNEL
);
272 static DEVICE_ATTR(decimals
, S_IRUGO
| S_IWUSR
, show_attr_decimals
, set_attr_decimals
);
274 static ssize_t
show_attr_textmode(struct device
*dev
,
275 struct device_attribute
*attr
, char *buf
)
277 struct usb_interface
*intf
= to_usb_interface(dev
);
278 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
283 for (i
= 0; i
< ARRAY_SIZE(display_textmodes
); i
++) {
284 if (mydev
->textmode
== i
) {
286 strcat(buf
, display_textmodes
[i
]);
290 strcat(buf
, display_textmodes
[i
]);
300 static ssize_t
set_attr_textmode(struct device
*dev
,
301 struct device_attribute
*attr
, const char *buf
, size_t count
)
303 struct usb_interface
*intf
= to_usb_interface(dev
);
304 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
307 i
= sysfs_match_string(display_textmodes
, buf
);
312 update_display_visual(mydev
, GFP_KERNEL
);
316 static DEVICE_ATTR(textmode
, S_IRUGO
| S_IWUSR
, show_attr_textmode
, set_attr_textmode
);
319 MYDEV_ATTR_SIMPLE_UNSIGNED(powered
, update_display_powered
);
320 MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb
, update_display_mode
);
321 MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb
, update_display_mode
);
323 static struct attribute
*dev_attrs
[] = {
324 &dev_attr_powered
.attr
,
326 &dev_attr_textmode
.attr
,
327 &dev_attr_decimals
.attr
,
328 &dev_attr_mode_msb
.attr
,
329 &dev_attr_mode_lsb
.attr
,
333 static struct attribute_group dev_attr_grp
= {
337 static int sevseg_probe(struct usb_interface
*interface
,
338 const struct usb_device_id
*id
)
340 struct usb_device
*udev
= interface_to_usbdev(interface
);
341 struct usb_sevsegdev
*mydev
= NULL
;
344 mydev
= kzalloc(sizeof(struct usb_sevsegdev
), GFP_KERNEL
);
348 mydev
->udev
= usb_get_dev(udev
);
349 mydev
->intf
= interface
;
350 usb_set_intfdata(interface
, mydev
);
353 mydev
->shadow_power
= 1; /* currently active */
354 mydev
->has_interface_pm
= 0; /* have not issued autopm_get */
357 mydev
->textmode
= 0x02; /* ascii mode */
358 mydev
->mode_msb
= 0x06; /* 6 characters */
359 mydev
->mode_lsb
= 0x3f; /* scanmode for 6 chars */
361 rc
= sysfs_create_group(&interface
->dev
.kobj
, &dev_attr_grp
);
365 dev_info(&interface
->dev
, "USB 7 Segment device now attached\n");
369 usb_set_intfdata(interface
, NULL
);
370 usb_put_dev(mydev
->udev
);
376 static void sevseg_disconnect(struct usb_interface
*interface
)
378 struct usb_sevsegdev
*mydev
;
380 mydev
= usb_get_intfdata(interface
);
381 sysfs_remove_group(&interface
->dev
.kobj
, &dev_attr_grp
);
382 usb_set_intfdata(interface
, NULL
);
383 usb_put_dev(mydev
->udev
);
385 dev_info(&interface
->dev
, "USB 7 Segment now disconnected\n");
388 static int sevseg_suspend(struct usb_interface
*intf
, pm_message_t message
)
390 struct usb_sevsegdev
*mydev
;
392 mydev
= usb_get_intfdata(intf
);
393 mydev
->shadow_power
= 0;
398 static int sevseg_resume(struct usb_interface
*intf
)
400 struct usb_sevsegdev
*mydev
;
402 mydev
= usb_get_intfdata(intf
);
403 mydev
->shadow_power
= 1;
404 update_display_mode(mydev
);
405 update_display_visual(mydev
, GFP_NOIO
);
410 static int sevseg_reset_resume(struct usb_interface
*intf
)
412 struct usb_sevsegdev
*mydev
;
414 mydev
= usb_get_intfdata(intf
);
415 mydev
->shadow_power
= 1;
416 update_display_mode(mydev
);
417 update_display_visual(mydev
, GFP_NOIO
);
422 static struct usb_driver sevseg_driver
= {
424 .probe
= sevseg_probe
,
425 .disconnect
= sevseg_disconnect
,
426 .suspend
= sevseg_suspend
,
427 .resume
= sevseg_resume
,
428 .reset_resume
= sevseg_reset_resume
,
429 .id_table
= id_table
,
430 .supports_autosuspend
= 1,
433 module_usb_driver(sevseg_driver
);
435 MODULE_AUTHOR(DRIVER_AUTHOR
);
436 MODULE_DESCRIPTION(DRIVER_DESC
);
437 MODULE_LICENSE("GPL");