]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/misc/usbsevseg.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / misc / usbsevseg.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
eb86be54
HM
2/*
3 * USB 7 Segment Driver
4 *
5 * Copyright (C) 2008 Harrison Metzger <harrisonmetz@gmail.com>
6 * Based on usbled.c by Greg Kroah-Hartman (greg@kroah.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/errno.h>
eb86be54
HM
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/string.h>
19#include <linux/usb.h>
20
21
22#define DRIVER_AUTHOR "Harrison Metzger <harrisonmetz@gmail.com>"
23#define DRIVER_DESC "USB 7 Segment Driver"
24
25#define VENDOR_ID 0x0fc5
26#define PRODUCT_ID 0x1227
1097cceb 27#define MAXLEN 8
eb86be54
HM
28
29/* table of devices that work with this driver */
33b9e162 30static const struct usb_device_id id_table[] = {
eb86be54
HM
31 { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
32 { },
33};
34MODULE_DEVICE_TABLE(usb, id_table);
35
36/* the different text display modes the device is capable of */
0bd08fc8 37static const char *display_textmodes[] = {"raw", "hex", "ascii"};
eb86be54
HM
38
39struct usb_sevsegdev {
40 struct usb_device *udev;
4d155eb5 41 struct usb_interface *intf;
eb86be54
HM
42
43 u8 powered;
44 u8 mode_msb;
45 u8 mode_lsb;
46 u8 decimals[MAXLEN];
47 u8 textmode;
48 u8 text[MAXLEN];
49 u16 textlength;
4d155eb5
ON
50
51 u8 shadow_power; /* for PM */
4c1f5c88 52 u8 has_interface_pm;
eb86be54
HM
53};
54
55/* sysfs_streq can't replace this completely
56 * If the device was in hex mode, and the user wanted a 0,
57 * if str commands are used, we would assume the end of string
58 * so mem commands are used.
59 */
ff4708e6 60static inline size_t my_memlen(const char *buf, size_t count)
eb86be54
HM
61{
62 if (count > 0 && buf[count-1] == '\n')
63 return count - 1;
64 else
65 return count;
66}
67
68static void update_display_powered(struct usb_sevsegdev *mydev)
69{
70 int rc;
71
4c1f5c88 72 if (mydev->powered && !mydev->has_interface_pm) {
4d155eb5
ON
73 rc = usb_autopm_get_interface(mydev->intf);
74 if (rc < 0)
75 return;
4c1f5c88 76 mydev->has_interface_pm = 1;
4d155eb5
ON
77 }
78
4c1f5c88
HM
79 if (mydev->shadow_power != 1)
80 return;
81
eb86be54
HM
82 rc = usb_control_msg(mydev->udev,
83 usb_sndctrlpipe(mydev->udev, 0),
84 0x12,
85 0x48,
86 (80 * 0x100) + 10, /* (power mode) */
87 (0x00 * 0x100) + (mydev->powered ? 1 : 0),
88 NULL,
89 0,
90 2000);
91 if (rc < 0)
92 dev_dbg(&mydev->udev->dev, "power retval = %d\n", rc);
4d155eb5 93
4c1f5c88 94 if (!mydev->powered && mydev->has_interface_pm) {
4d155eb5 95 usb_autopm_put_interface(mydev->intf);
4c1f5c88
HM
96 mydev->has_interface_pm = 0;
97 }
eb86be54
HM
98}
99
100static void update_display_mode(struct usb_sevsegdev *mydev)
101{
102 int rc;
103
4d155eb5
ON
104 if(mydev->shadow_power != 1)
105 return;
106
eb86be54
HM
107 rc = usb_control_msg(mydev->udev,
108 usb_sndctrlpipe(mydev->udev, 0),
109 0x12,
110 0x48,
111 (82 * 0x100) + 10, /* (set mode) */
112 (mydev->mode_msb * 0x100) + mydev->mode_lsb,
113 NULL,
114 0,
115 2000);
116
117 if (rc < 0)
118 dev_dbg(&mydev->udev->dev, "mode retval = %d\n", rc);
119}
120
4d155eb5 121static void update_display_visual(struct usb_sevsegdev *mydev, gfp_t mf)
eb86be54
HM
122{
123 int rc;
124 int i;
125 unsigned char *buffer;
126 u8 decimals = 0;
127
4d155eb5
ON
128 if(mydev->shadow_power != 1)
129 return;
130
131 buffer = kzalloc(MAXLEN, mf);
bcf0848d 132 if (!buffer)
eb86be54 133 return;
eb86be54
HM
134
135 /* The device is right to left, where as you write left to right */
136 for (i = 0; i < mydev->textlength; i++)
137 buffer[i] = mydev->text[mydev->textlength-1-i];
138
139 rc = usb_control_msg(mydev->udev,
140 usb_sndctrlpipe(mydev->udev, 0),
141 0x12,
142 0x48,
143 (85 * 0x100) + 10, /* (write text) */
144 (0 * 0x100) + mydev->textmode, /* mode */
145 buffer,
146 mydev->textlength,
147 2000);
148
149 if (rc < 0)
150 dev_dbg(&mydev->udev->dev, "write retval = %d\n", rc);
151
152 kfree(buffer);
153
154 /* The device is right to left, where as you write left to right */
155 for (i = 0; i < sizeof(mydev->decimals); i++)
156 decimals |= mydev->decimals[i] << i;
157
158 rc = usb_control_msg(mydev->udev,
159 usb_sndctrlpipe(mydev->udev, 0),
160 0x12,
161 0x48,
162 (86 * 0x100) + 10, /* (set decimal) */
163 (0 * 0x100) + decimals, /* decimals */
164 NULL,
165 0,
166 2000);
167
168 if (rc < 0)
169 dev_dbg(&mydev->udev->dev, "decimal retval = %d\n", rc);
170}
171
172#define MYDEV_ATTR_SIMPLE_UNSIGNED(name, update_fcn) \
173static ssize_t show_attr_##name(struct device *dev, \
174 struct device_attribute *attr, char *buf) \
175{ \
176 struct usb_interface *intf = to_usb_interface(dev); \
177 struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
178 \
179 return sprintf(buf, "%u\n", mydev->name); \
180} \
181 \
182static ssize_t set_attr_##name(struct device *dev, \
183 struct device_attribute *attr, const char *buf, size_t count) \
184{ \
185 struct usb_interface *intf = to_usb_interface(dev); \
186 struct usb_sevsegdev *mydev = usb_get_intfdata(intf); \
187 \
188 mydev->name = simple_strtoul(buf, NULL, 10); \
4d155eb5 189 update_fcn(mydev); \
eb86be54
HM
190 \
191 return count; \
192} \
e24d7ace 193static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, show_attr_##name, set_attr_##name);
eb86be54
HM
194
195static ssize_t show_attr_text(struct device *dev,
196 struct device_attribute *attr, char *buf)
197{
198 struct usb_interface *intf = to_usb_interface(dev);
199 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
200
201 return snprintf(buf, mydev->textlength, "%s\n", mydev->text);
202}
203
204static ssize_t set_attr_text(struct device *dev,
205 struct device_attribute *attr, const char *buf, size_t count)
206{
207 struct usb_interface *intf = to_usb_interface(dev);
208 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
209 size_t end = my_memlen(buf, count);
210
211 if (end > sizeof(mydev->text))
212 return -EINVAL;
213
214 memset(mydev->text, 0, sizeof(mydev->text));
215 mydev->textlength = end;
216
217 if (end > 0)
218 memcpy(mydev->text, buf, end);
219
4d155eb5 220 update_display_visual(mydev, GFP_KERNEL);
eb86be54
HM
221 return count;
222}
223
e24d7ace 224static DEVICE_ATTR(text, S_IRUGO | S_IWUSR, show_attr_text, set_attr_text);
eb86be54
HM
225
226static ssize_t show_attr_decimals(struct device *dev,
227 struct device_attribute *attr, char *buf)
228{
229 struct usb_interface *intf = to_usb_interface(dev);
230 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
231 int i;
232 int pos;
233
234 for (i = 0; i < sizeof(mydev->decimals); i++) {
235 pos = sizeof(mydev->decimals) - 1 - i;
236 if (mydev->decimals[i] == 0)
237 buf[pos] = '0';
238 else if (mydev->decimals[i] == 1)
239 buf[pos] = '1';
240 else
241 buf[pos] = 'x';
242 }
243
244 buf[sizeof(mydev->decimals)] = '\n';
245 return sizeof(mydev->decimals) + 1;
246}
247
248static ssize_t set_attr_decimals(struct device *dev,
249 struct device_attribute *attr, const char *buf, size_t count)
250{
251 struct usb_interface *intf = to_usb_interface(dev);
252 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
253 size_t end = my_memlen(buf, count);
254 int i;
255
256 if (end > sizeof(mydev->decimals))
257 return -EINVAL;
258
259 for (i = 0; i < end; i++)
260 if (buf[i] != '0' && buf[i] != '1')
261 return -EINVAL;
262
263 memset(mydev->decimals, 0, sizeof(mydev->decimals));
264 for (i = 0; i < end; i++)
265 if (buf[i] == '1')
266 mydev->decimals[end-1-i] = 1;
267
4d155eb5 268 update_display_visual(mydev, GFP_KERNEL);
eb86be54
HM
269
270 return count;
271}
272
e24d7ace 273static DEVICE_ATTR(decimals, S_IRUGO | S_IWUSR, show_attr_decimals, set_attr_decimals);
eb86be54
HM
274
275static ssize_t show_attr_textmode(struct device *dev,
276 struct device_attribute *attr, char *buf)
277{
278 struct usb_interface *intf = to_usb_interface(dev);
279 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
280 int i;
281
282 buf[0] = 0;
283
0bd08fc8 284 for (i = 0; i < ARRAY_SIZE(display_textmodes); i++) {
eb86be54
HM
285 if (mydev->textmode == i) {
286 strcat(buf, " [");
287 strcat(buf, display_textmodes[i]);
288 strcat(buf, "] ");
289 } else {
290 strcat(buf, " ");
291 strcat(buf, display_textmodes[i]);
292 strcat(buf, " ");
293 }
294 }
295 strcat(buf, "\n");
296
297
298 return strlen(buf);
299}
300
301static ssize_t set_attr_textmode(struct device *dev,
302 struct device_attribute *attr, const char *buf, size_t count)
303{
304 struct usb_interface *intf = to_usb_interface(dev);
305 struct usb_sevsegdev *mydev = usb_get_intfdata(intf);
306 int i;
307
0bd08fc8
AS
308 i = sysfs_match_string(display_textmodes, buf);
309 if (i < 0)
310 return i;
eb86be54 311
0bd08fc8
AS
312 mydev->textmode = i;
313 update_display_visual(mydev, GFP_KERNEL);
314 return count;
eb86be54
HM
315}
316
e24d7ace 317static DEVICE_ATTR(textmode, S_IRUGO | S_IWUSR, show_attr_textmode, set_attr_textmode);
eb86be54
HM
318
319
320MYDEV_ATTR_SIMPLE_UNSIGNED(powered, update_display_powered);
321MYDEV_ATTR_SIMPLE_UNSIGNED(mode_msb, update_display_mode);
322MYDEV_ATTR_SIMPLE_UNSIGNED(mode_lsb, update_display_mode);
323
324static struct attribute *dev_attrs[] = {
325 &dev_attr_powered.attr,
326 &dev_attr_text.attr,
327 &dev_attr_textmode.attr,
328 &dev_attr_decimals.attr,
329 &dev_attr_mode_msb.attr,
330 &dev_attr_mode_lsb.attr,
331 NULL
332};
333
cc122789 334static const struct attribute_group dev_attr_grp = {
eb86be54
HM
335 .attrs = dev_attrs,
336};
337
338static int sevseg_probe(struct usb_interface *interface,
339 const struct usb_device_id *id)
340{
341 struct usb_device *udev = interface_to_usbdev(interface);
342 struct usb_sevsegdev *mydev = NULL;
343 int rc = -ENOMEM;
344
345 mydev = kzalloc(sizeof(struct usb_sevsegdev), GFP_KERNEL);
bcf0848d 346 if (!mydev)
eb86be54 347 goto error_mem;
eb86be54
HM
348
349 mydev->udev = usb_get_dev(udev);
4d155eb5 350 mydev->intf = interface;
eb86be54
HM
351 usb_set_intfdata(interface, mydev);
352
4c1f5c88
HM
353 /* PM */
354 mydev->shadow_power = 1; /* currently active */
355 mydev->has_interface_pm = 0; /* have not issued autopm_get */
356
eb86be54
HM
357 /*set defaults */
358 mydev->textmode = 0x02; /* ascii mode */
359 mydev->mode_msb = 0x06; /* 6 characters */
360 mydev->mode_lsb = 0x3f; /* scanmode for 6 chars */
361
362 rc = sysfs_create_group(&interface->dev.kobj, &dev_attr_grp);
363 if (rc)
364 goto error;
365
366 dev_info(&interface->dev, "USB 7 Segment device now attached\n");
367 return 0;
368
369error:
370 usb_set_intfdata(interface, NULL);
371 usb_put_dev(mydev->udev);
372 kfree(mydev);
373error_mem:
374 return rc;
375}
376
377static void sevseg_disconnect(struct usb_interface *interface)
378{
379 struct usb_sevsegdev *mydev;
380
381 mydev = usb_get_intfdata(interface);
382 sysfs_remove_group(&interface->dev.kobj, &dev_attr_grp);
383 usb_set_intfdata(interface, NULL);
384 usb_put_dev(mydev->udev);
385 kfree(mydev);
386 dev_info(&interface->dev, "USB 7 Segment now disconnected\n");
387}
388
4d155eb5
ON
389static int sevseg_suspend(struct usb_interface *intf, pm_message_t message)
390{
391 struct usb_sevsegdev *mydev;
392
393 mydev = usb_get_intfdata(intf);
394 mydev->shadow_power = 0;
395
396 return 0;
397}
398
399static int sevseg_resume(struct usb_interface *intf)
400{
401 struct usb_sevsegdev *mydev;
402
403 mydev = usb_get_intfdata(intf);
404 mydev->shadow_power = 1;
405 update_display_mode(mydev);
406 update_display_visual(mydev, GFP_NOIO);
407
408 return 0;
409}
410
411static int sevseg_reset_resume(struct usb_interface *intf)
412{
413 struct usb_sevsegdev *mydev;
414
415 mydev = usb_get_intfdata(intf);
416 mydev->shadow_power = 1;
417 update_display_mode(mydev);
418 update_display_visual(mydev, GFP_NOIO);
419
420 return 0;
421}
422
eb86be54
HM
423static struct usb_driver sevseg_driver = {
424 .name = "usbsevseg",
425 .probe = sevseg_probe,
426 .disconnect = sevseg_disconnect,
4d155eb5
ON
427 .suspend = sevseg_suspend,
428 .resume = sevseg_resume,
429 .reset_resume = sevseg_reset_resume,
eb86be54 430 .id_table = id_table,
4d155eb5 431 .supports_autosuspend = 1,
eb86be54
HM
432};
433
65db4305 434module_usb_driver(sevseg_driver);
eb86be54
HM
435
436MODULE_AUTHOR(DRIVER_AUTHOR);
437MODULE_DESCRIPTION(DRIVER_DESC);
438MODULE_LICENSE("GPL");