Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 2 | * USB LED driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com) |
| 5 | * |
| 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. |
| 9 | * |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/usb.h> |
| 17 | |
| 18 | |
| 19 | #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com" |
| 20 | #define DRIVER_DESC "USB LED Driver" |
| 21 | |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 22 | enum led_type { |
| 23 | DELCOM_VISUAL_SIGNAL_INDICATOR, |
| 24 | DREAM_CHEEKY_WEBMAIL_NOTIFIER, |
| 25 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* table of devices that work with this driver */ |
Németh Márton | 33b9e16 | 2010-01-10 15:34:45 +0100 | [diff] [blame] | 28 | static const struct usb_device_id id_table[] = { |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 29 | { USB_DEVICE(0x0fc5, 0x1223), |
| 30 | .driver_info = DELCOM_VISUAL_SIGNAL_INDICATOR }, |
| 31 | { USB_DEVICE(0x1d34, 0x0004), |
| 32 | .driver_info = DREAM_CHEEKY_WEBMAIL_NOTIFIER }, |
Dan Delaney | 789aaa2 | 2011-11-20 10:21:30 +0100 | [diff] [blame] | 33 | { USB_DEVICE(0x1d34, 0x000a), |
| 34 | .driver_info = DREAM_CHEEKY_WEBMAIL_NOTIFIER }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { }, |
| 36 | }; |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 37 | MODULE_DEVICE_TABLE(usb, id_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | struct usb_led { |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 40 | struct usb_device *udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | unsigned char blue; |
| 42 | unsigned char red; |
| 43 | unsigned char green; |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 44 | enum led_type type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static void change_color(struct usb_led *led) |
| 48 | { |
Melchior FRANZ | 952eca0 | 2010-12-22 13:55:24 +0100 | [diff] [blame] | 49 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | unsigned char *buffer; |
| 51 | |
| 52 | buffer = kmalloc(8, GFP_KERNEL); |
| 53 | if (!buffer) { |
| 54 | dev_err(&led->udev->dev, "out of memory\n"); |
| 55 | return; |
| 56 | } |
| 57 | |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 58 | switch (led->type) { |
| 59 | case DELCOM_VISUAL_SIGNAL_INDICATOR: { |
| 60 | unsigned char color = 0x07; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 62 | if (led->blue) |
| 63 | color &= ~0x04; |
| 64 | if (led->red) |
| 65 | color &= ~0x02; |
| 66 | if (led->green) |
| 67 | color &= ~0x01; |
| 68 | dev_dbg(&led->udev->dev, |
| 69 | "blue = %d, red = %d, green = %d, color = %.2x\n", |
| 70 | led->blue, led->red, led->green, color); |
| 71 | |
| 72 | retval = usb_control_msg(led->udev, |
| 73 | usb_sndctrlpipe(led->udev, 0), |
| 74 | 0x12, |
| 75 | 0xc8, |
| 76 | (0x02 * 0x100) + 0x0a, |
| 77 | (0x00 * 0x100) + color, |
| 78 | buffer, |
| 79 | 8, |
| 80 | 2000); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | case DREAM_CHEEKY_WEBMAIL_NOTIFIER: |
| 85 | dev_dbg(&led->udev->dev, |
| 86 | "red = %d, green = %d, blue = %d\n", |
| 87 | led->red, led->green, led->blue); |
| 88 | |
| 89 | buffer[0] = led->red; |
| 90 | buffer[1] = led->green; |
| 91 | buffer[2] = led->blue; |
| 92 | buffer[3] = buffer[4] = buffer[5] = 0; |
| 93 | buffer[6] = 0x1a; |
| 94 | buffer[7] = 0x05; |
| 95 | |
| 96 | retval = usb_control_msg(led->udev, |
| 97 | usb_sndctrlpipe(led->udev, 0), |
| 98 | 0x09, |
| 99 | 0x21, |
| 100 | 0x200, |
| 101 | 0, |
| 102 | buffer, |
| 103 | 8, |
| 104 | 2000); |
| 105 | break; |
| 106 | |
| 107 | default: |
| 108 | dev_err(&led->udev->dev, "unknown device type %d\n", led->type); |
| 109 | } |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (retval) |
| 112 | dev_dbg(&led->udev->dev, "retval = %d\n", retval); |
| 113 | kfree(buffer); |
| 114 | } |
| 115 | |
| 116 | #define show_set(value) \ |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 117 | static ssize_t show_##value(struct device *dev, struct device_attribute *attr,\ |
| 118 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { \ |
| 120 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 121 | struct usb_led *led = usb_get_intfdata(intf); \ |
| 122 | \ |
| 123 | return sprintf(buf, "%d\n", led->value); \ |
| 124 | } \ |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 125 | static ssize_t set_##value(struct device *dev, struct device_attribute *attr,\ |
| 126 | const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { \ |
| 128 | struct usb_interface *intf = to_usb_interface(dev); \ |
| 129 | struct usb_led *led = usb_get_intfdata(intf); \ |
| 130 | int temp = simple_strtoul(buf, NULL, 10); \ |
| 131 | \ |
| 132 | led->value = temp; \ |
| 133 | change_color(led); \ |
| 134 | return count; \ |
| 135 | } \ |
Greg Kroah-Hartman | 48f1154 | 2010-11-15 11:35:49 -0800 | [diff] [blame] | 136 | static DEVICE_ATTR(value, S_IRUGO | S_IWUSR, show_##value, set_##value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | show_set(blue); |
| 138 | show_set(red); |
| 139 | show_set(green); |
| 140 | |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 141 | static int led_probe(struct usb_interface *interface, |
| 142 | const struct usb_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | struct usb_device *udev = interface_to_usbdev(interface); |
| 145 | struct usb_led *dev = NULL; |
| 146 | int retval = -ENOMEM; |
| 147 | |
Oliver Neukum | 06d6947 | 2006-01-06 22:44:52 +0100 | [diff] [blame] | 148 | dev = kzalloc(sizeof(struct usb_led), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (dev == NULL) { |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 150 | dev_err(&interface->dev, "out of memory\n"); |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 151 | goto error_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | dev->udev = usb_get_dev(udev); |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 155 | dev->type = id->driver_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 157 | usb_set_intfdata(interface, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 159 | retval = device_create_file(&interface->dev, &dev_attr_blue); |
| 160 | if (retval) |
| 161 | goto error; |
| 162 | retval = device_create_file(&interface->dev, &dev_attr_red); |
| 163 | if (retval) |
| 164 | goto error; |
| 165 | retval = device_create_file(&interface->dev, &dev_attr_green); |
| 166 | if (retval) |
| 167 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Melchior FRANZ | 73bc7d3 | 2010-12-22 02:04:33 +0100 | [diff] [blame] | 169 | if (dev->type == DREAM_CHEEKY_WEBMAIL_NOTIFIER) { |
| 170 | unsigned char *enable; |
| 171 | |
| 172 | enable = kmemdup("\x1f\x02\0\x5f\0\0\x1a\x03", 8, GFP_KERNEL); |
| 173 | if (!enable) { |
| 174 | dev_err(&interface->dev, "out of memory\n"); |
| 175 | retval = -ENOMEM; |
| 176 | goto error; |
| 177 | } |
| 178 | |
| 179 | retval = usb_control_msg(udev, |
| 180 | usb_sndctrlpipe(udev, 0), |
| 181 | 0x09, |
| 182 | 0x21, |
| 183 | 0x200, |
| 184 | 0, |
| 185 | enable, |
| 186 | 8, |
| 187 | 2000); |
| 188 | |
| 189 | kfree(enable); |
| 190 | if (retval != 8) |
| 191 | goto error; |
| 192 | } |
| 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | dev_info(&interface->dev, "USB LED device now attached\n"); |
| 195 | return 0; |
| 196 | |
| 197 | error: |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 198 | device_remove_file(&interface->dev, &dev_attr_blue); |
| 199 | device_remove_file(&interface->dev, &dev_attr_red); |
| 200 | device_remove_file(&interface->dev, &dev_attr_green); |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 201 | usb_set_intfdata(interface, NULL); |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 202 | usb_put_dev(dev->udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | kfree(dev); |
Greg Kroah-Hartman | 4d42e1b | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 204 | error_mem: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | return retval; |
| 206 | } |
| 207 | |
| 208 | static void led_disconnect(struct usb_interface *interface) |
| 209 | { |
| 210 | struct usb_led *dev; |
| 211 | |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 212 | dev = usb_get_intfdata(interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | device_remove_file(&interface->dev, &dev_attr_blue); |
| 215 | device_remove_file(&interface->dev, &dev_attr_red); |
| 216 | device_remove_file(&interface->dev, &dev_attr_green); |
| 217 | |
Oliver Neukum | ed206ec | 2007-10-28 08:21:59 +0100 | [diff] [blame] | 218 | /* first remove the files, then set the pointer to NULL */ |
Zack Parsons | ea83586 | 2011-07-29 00:17:57 -0700 | [diff] [blame] | 219 | usb_set_intfdata(interface, NULL); |
Oliver Neukum | ed206ec | 2007-10-28 08:21:59 +0100 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | usb_put_dev(dev->udev); |
| 222 | |
| 223 | kfree(dev); |
| 224 | |
| 225 | dev_info(&interface->dev, "USB LED now disconnected\n"); |
| 226 | } |
| 227 | |
| 228 | static struct usb_driver led_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | .name = "usbled", |
| 230 | .probe = led_probe, |
| 231 | .disconnect = led_disconnect, |
| 232 | .id_table = id_table, |
| 233 | }; |
| 234 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 235 | module_usb_driver(led_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 238 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 239 | MODULE_LICENSE("GPL"); |