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