4 * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2.
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/module.h>
17 #include <linux/usb.h>
20 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com"
21 #define DRIVER_DESC "USB LED Driver"
23 #define VENDOR_ID 0x0fc5
24 #define PRODUCT_ID 0x1223
26 /* table of devices that work with this driver */
27 static struct usb_device_id id_table
[] = {
28 { USB_DEVICE(VENDOR_ID
, PRODUCT_ID
) },
31 MODULE_DEVICE_TABLE (usb
, id_table
);
34 struct usb_device
* udev
;
43 static void change_color(struct usb_led
*led
)
46 unsigned char color
= 0x07;
47 unsigned char *buffer
;
49 buffer
= kmalloc(8, GFP_KERNEL
);
51 dev_err(&led
->udev
->dev
, "out of memory\n");
61 dev_dbg(&led
->udev
->dev
,
62 "blue = %d, red = %d, green = %d, color = %.2x\n",
63 led
->blue
, led
->red
, led
->green
, color
);
65 retval
= usb_control_msg(led
->udev
,
66 usb_sndctrlpipe(led
->udev
, 0),
69 (0x02 * 0x100) + 0x0a,
70 (0x00 * 0x100) + color
,
75 dev_dbg(&led
->udev
->dev
, "retval = %d\n", retval
);
79 #define show_set(value) \
80 static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
82 struct usb_interface *intf = to_usb_interface(dev); \
83 struct usb_led *led = usb_get_intfdata(intf); \
85 return sprintf(buf, "%d\n", led->value); \
87 static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
89 struct usb_interface *intf = to_usb_interface(dev); \
90 struct usb_led *led = usb_get_intfdata(intf); \
91 int temp = simple_strtoul(buf, NULL, 10); \
97 static DEVICE_ATTR(value, S_IWUGO | S_IRUGO, show_##value, set_##value);
102 static int led_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
104 struct usb_device
*udev
= interface_to_usbdev(interface
);
105 struct usb_led
*dev
= NULL
;
106 int retval
= -ENOMEM
;
108 dev
= kzalloc(sizeof(struct usb_led
), GFP_KERNEL
);
110 dev_err(&interface
->dev
, "Out of memory\n");
114 dev
->udev
= usb_get_dev(udev
);
116 usb_set_intfdata (interface
, dev
);
118 retval
= device_create_file(&interface
->dev
, &dev_attr_blue
);
121 retval
= device_create_file(&interface
->dev
, &dev_attr_red
);
124 retval
= device_create_file(&interface
->dev
, &dev_attr_green
);
128 dev_info(&interface
->dev
, "USB LED device now attached\n");
132 device_remove_file(&interface
->dev
, &dev_attr_blue
);
133 device_remove_file(&interface
->dev
, &dev_attr_red
);
134 device_remove_file(&interface
->dev
, &dev_attr_green
);
135 usb_set_intfdata (interface
, NULL
);
136 usb_put_dev(dev
->udev
);
142 static void led_disconnect(struct usb_interface
*interface
)
146 dev
= usb_get_intfdata (interface
);
147 usb_set_intfdata (interface
, NULL
);
149 device_remove_file(&interface
->dev
, &dev_attr_blue
);
150 device_remove_file(&interface
->dev
, &dev_attr_red
);
151 device_remove_file(&interface
->dev
, &dev_attr_green
);
153 usb_put_dev(dev
->udev
);
157 dev_info(&interface
->dev
, "USB LED now disconnected\n");
160 static struct usb_driver led_driver
= {
163 .disconnect
= led_disconnect
,
164 .id_table
= id_table
,
167 static int __init
usb_led_init(void)
171 retval
= usb_register(&led_driver
);
173 err("usb_register failed. Error number %d", retval
);
177 static void __exit
usb_led_exit(void)
179 usb_deregister(&led_driver
);
182 module_init (usb_led_init
);
183 module_exit (usb_led_exit
);
185 MODULE_AUTHOR(DRIVER_AUTHOR
);
186 MODULE_DESCRIPTION(DRIVER_DESC
);
187 MODULE_LICENSE("GPL");