Linux Audio

Check our new training course

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