1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2008 Harrison Metzger <harrisonmetz@gmail.com>
6 * Based on usbled.c by Greg Kroah-Hartman (greg@kroah.com)
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/usb.h>
17 #define DRIVER_AUTHOR "Harrison Metzger <harrisonmetz@gmail.com>"
18 #define DRIVER_DESC "USB 7 Segment Driver"
20 #define VENDOR_ID 0x0fc5
21 #define PRODUCT_ID 0x1227
24 /* table of devices that work with this driver */
25 static const struct usb_device_id id_table
[] = {
26 { USB_DEVICE(VENDOR_ID
, PRODUCT_ID
) },
29 MODULE_DEVICE_TABLE(usb
, id_table
);
31 /* the different text display modes the device is capable of */
32 static const char *display_textmodes
[] = {"raw", "hex", "ascii"};
34 struct usb_sevsegdev
{
35 struct usb_device
*udev
;
36 struct usb_interface
*intf
;
46 u8 shadow_power
; /* for PM */
50 /* sysfs_streq can't replace this completely
51 * If the device was in hex mode, and the user wanted a 0,
52 * if str commands are used, we would assume the end of string
53 * so mem commands are used.
55 static inline size_t my_memlen(const char *buf
, size_t count
)
57 if (count
> 0 && buf
[count
-1] == '\n')
63 static void update_display_powered(struct usb_sevsegdev
*mydev
)
67 if (mydev
->powered
&& !mydev
->has_interface_pm
) {
68 rc
= usb_autopm_get_interface(mydev
->intf
);
71 mydev
->has_interface_pm
= 1;
74 if (mydev
->shadow_power
!= 1)
77 rc
= usb_control_msg(mydev
->udev
,
78 usb_sndctrlpipe(mydev
->udev
, 0),
81 (80 * 0x100) + 10, /* (power mode) */
82 (0x00 * 0x100) + (mydev
->powered
? 1 : 0),
87 dev_dbg(&mydev
->udev
->dev
, "power retval = %d\n", rc
);
89 if (!mydev
->powered
&& mydev
->has_interface_pm
) {
90 usb_autopm_put_interface(mydev
->intf
);
91 mydev
->has_interface_pm
= 0;
95 static void update_display_mode(struct usb_sevsegdev
*mydev
)
99 if(mydev
->shadow_power
!= 1)
102 rc
= usb_control_msg(mydev
->udev
,
103 usb_sndctrlpipe(mydev
->udev
, 0),
106 (82 * 0x100) + 10, /* (set mode) */
107 (mydev
->mode_msb
* 0x100) + mydev
->mode_lsb
,
113 dev_dbg(&mydev
->udev
->dev
, "mode retval = %d\n", rc
);
116 static void update_display_visual(struct usb_sevsegdev
*mydev
, gfp_t mf
)
120 unsigned char *buffer
;
123 if(mydev
->shadow_power
!= 1)
126 buffer
= kzalloc(MAXLEN
, mf
);
130 /* The device is right to left, where as you write left to right */
131 for (i
= 0; i
< mydev
->textlength
; i
++)
132 buffer
[i
] = mydev
->text
[mydev
->textlength
-1-i
];
134 rc
= usb_control_msg(mydev
->udev
,
135 usb_sndctrlpipe(mydev
->udev
, 0),
138 (85 * 0x100) + 10, /* (write text) */
139 (0 * 0x100) + mydev
->textmode
, /* mode */
145 dev_dbg(&mydev
->udev
->dev
, "write retval = %d\n", rc
);
149 /* The device is right to left, where as you write left to right */
150 for (i
= 0; i
< sizeof(mydev
->decimals
); i
++)
151 decimals
|= mydev
->decimals
[i
] << i
;
153 rc
= usb_control_msg(mydev
->udev
,
154 usb_sndctrlpipe(mydev
->udev
, 0),
157 (86 * 0x100) + 10, /* (set decimal) */
158 (0 * 0x100) + decimals
, /* decimals */
164 dev_dbg(&mydev
->udev
->dev
, "decimal retval = %d\n", rc
);
167 #define MYDEV_ATTR_SIMPLE_UNSIGNED(name, update_fcn) \
168 static ssize_t name##_show(struct device *dev, \
169 struct device_attribute *attr, char *buf) \
171 struct usb_interface *intf = to_usb_interface(dev); \
172 struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
174 return sprintf(buf, "%u\n", mydev->name); \
177 static ssize_t name##_store(struct device *dev, \
178 struct device_attribute *attr, const char *buf, size_t count) \
180 struct usb_interface *intf = to_usb_interface(dev); \
181 struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
183 mydev->name = simple_strtoul(buf, NULL, 10); \
188 static DEVICE_ATTR_RW(name);
190 static ssize_t
text_show(struct device
*dev
,
191 struct device_attribute
*attr
, char *buf
)
193 struct usb_interface
*intf
= to_usb_interface(dev
);
194 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
196 return snprintf(buf
, mydev
->textlength
, "%s\n", mydev
->text
);
199 static ssize_t
text_store(struct device
*dev
,
200 struct device_attribute
*attr
, const char *buf
, size_t count
)
202 struct usb_interface
*intf
= to_usb_interface(dev
);
203 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
204 size_t end
= my_memlen(buf
, count
);
206 if (end
> sizeof(mydev
->text
))
209 memset(mydev
->text
, 0, sizeof(mydev
->text
));
210 mydev
->textlength
= end
;
213 memcpy(mydev
->text
, buf
, end
);
215 update_display_visual(mydev
, GFP_KERNEL
);
219 static DEVICE_ATTR_RW(text
);
221 static ssize_t
decimals_show(struct device
*dev
,
222 struct device_attribute
*attr
, char *buf
)
224 struct usb_interface
*intf
= to_usb_interface(dev
);
225 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
229 for (i
= 0; i
< sizeof(mydev
->decimals
); i
++) {
230 pos
= sizeof(mydev
->decimals
) - 1 - i
;
231 if (mydev
->decimals
[i
] == 0)
233 else if (mydev
->decimals
[i
] == 1)
239 buf
[sizeof(mydev
->decimals
)] = '\n';
240 return sizeof(mydev
->decimals
) + 1;
243 static ssize_t
decimals_store(struct device
*dev
,
244 struct device_attribute
*attr
, const char *buf
, size_t count
)
246 struct usb_interface
*intf
= to_usb_interface(dev
);
247 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
248 size_t end
= my_memlen(buf
, count
);
251 if (end
> sizeof(mydev
->decimals
))
254 for (i
= 0; i
< end
; i
++)
255 if (buf
[i
] != '0' && buf
[i
] != '1')
258 memset(mydev
->decimals
, 0, sizeof(mydev
->decimals
));
259 for (i
= 0; i
< end
; i
++)
261 mydev
->decimals
[end
-1-i
] = 1;
263 update_display_visual(mydev
, GFP_KERNEL
);
268 static DEVICE_ATTR_RW(decimals
);
270 static ssize_t
textmode_show(struct device
*dev
,
271 struct device_attribute
*attr
, char *buf
)
273 struct usb_interface
*intf
= to_usb_interface(dev
);
274 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
279 for (i
= 0; i
< ARRAY_SIZE(display_textmodes
); i
++) {
280 if (mydev
->textmode
== i
) {
282 strcat(buf
, display_textmodes
[i
]);
286 strcat(buf
, display_textmodes
[i
]);
296 static ssize_t
textmode_store(struct device
*dev
,
297 struct device_attribute
*attr
, const char *buf
, size_t count
)
299 struct usb_interface
*intf
= to_usb_interface(dev
);
300 struct usb_sevsegdev
*mydev
= usb_get_intfdata(intf
);
303 i
= sysfs_match_string(display_textmodes
, buf
);
308 update_display_visual(mydev
, GFP_KERNEL
);
312 static DEVICE_ATTR_RW(textmode
);
315 MYDEV_ATTR_SIMPLE_UNSIGNED(powered
, update_display_powered
);
316 MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb
, update_display_mode
);
317 MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb
, update_display_mode
);
319 static struct attribute
*dev_attrs
[] = {
320 &dev_attr_powered
.attr
,
322 &dev_attr_textmode
.attr
,
323 &dev_attr_decimals
.attr
,
324 &dev_attr_mode_msb
.attr
,
325 &dev_attr_mode_lsb
.attr
,
329 static const struct attribute_group dev_attr_grp
= {
333 static int sevseg_probe(struct usb_interface
*interface
,
334 const struct usb_device_id
*id
)
336 struct usb_device
*udev
= interface_to_usbdev(interface
);
337 struct usb_sevsegdev
*mydev
= NULL
;
340 mydev
= kzalloc(sizeof(struct usb_sevsegdev
), GFP_KERNEL
);
344 mydev
->udev
= usb_get_dev(udev
);
345 mydev
->intf
= interface
;
346 usb_set_intfdata(interface
, mydev
);
349 mydev
->shadow_power
= 1; /* currently active */
350 mydev
->has_interface_pm
= 0; /* have not issued autopm_get */
353 mydev
->textmode
= 0x02; /* ascii mode */
354 mydev
->mode_msb
= 0x06; /* 6 characters */
355 mydev
->mode_lsb
= 0x3f; /* scanmode for 6 chars */
357 rc
= sysfs_create_group(&interface
->dev
.kobj
, &dev_attr_grp
);
361 dev_info(&interface
->dev
, "USB 7 Segment device now attached\n");
365 usb_set_intfdata(interface
, NULL
);
366 usb_put_dev(mydev
->udev
);
372 static void sevseg_disconnect(struct usb_interface
*interface
)
374 struct usb_sevsegdev
*mydev
;
376 mydev
= usb_get_intfdata(interface
);
377 sysfs_remove_group(&interface
->dev
.kobj
, &dev_attr_grp
);
378 usb_set_intfdata(interface
, NULL
);
379 usb_put_dev(mydev
->udev
);
381 dev_info(&interface
->dev
, "USB 7 Segment now disconnected\n");
384 static int sevseg_suspend(struct usb_interface
*intf
, pm_message_t message
)
386 struct usb_sevsegdev
*mydev
;
388 mydev
= usb_get_intfdata(intf
);
389 mydev
->shadow_power
= 0;
394 static int sevseg_resume(struct usb_interface
*intf
)
396 struct usb_sevsegdev
*mydev
;
398 mydev
= usb_get_intfdata(intf
);
399 mydev
->shadow_power
= 1;
400 update_display_mode(mydev
);
401 update_display_visual(mydev
, GFP_NOIO
);
406 static int sevseg_reset_resume(struct usb_interface
*intf
)
408 struct usb_sevsegdev
*mydev
;
410 mydev
= usb_get_intfdata(intf
);
411 mydev
->shadow_power
= 1;
412 update_display_mode(mydev
);
413 update_display_visual(mydev
, GFP_NOIO
);
418 static struct usb_driver sevseg_driver
= {
420 .probe
= sevseg_probe
,
421 .disconnect
= sevseg_disconnect
,
422 .suspend
= sevseg_suspend
,
423 .resume
= sevseg_resume
,
424 .reset_resume
= sevseg_reset_resume
,
425 .id_table
= id_table
,
426 .supports_autosuspend
= 1,
429 module_usb_driver(sevseg_driver
);
431 MODULE_AUTHOR(DRIVER_AUTHOR
);
432 MODULE_DESCRIPTION(DRIVER_DESC
);
433 MODULE_LICENSE("GPL");