Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / usb / misc / phidgetkit.c
1 /*
2  * USB PhidgetInterfaceKit driver 1.0
3  *
4  * Copyright (C) 2004 Sean Young <sean@mess.org>
5  * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This is a driver for the USB PhidgetInterfaceKit.
13  */
14
15 #include <linux/config.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/usb.h>
22
23 #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
24 #define DRIVER_DESC "USB PhidgetInterfaceKit Driver"
25
26 #define USB_VENDOR_ID_GLAB              0x06c2
27 #define USB_DEVICE_ID_INTERFACEKIT004   0x0040
28 #define USB_DEVICE_ID_INTERFACEKIT888   0x0045
29 #define USB_DEVICE_ID_INTERFACEKIT047   0x0051
30 #define USB_DEVICE_ID_INTERFACEKIT088   0x0053
31
32 #define USB_VENDOR_ID_WISEGROUP         0x0925
33 #define USB_DEVICE_ID_INTERFACEKIT884   0x8201
34
35 #define MAX_INTERFACES                  8
36
37 struct driver_interfacekit {
38         int sensors;
39         int inputs;
40         int outputs;
41         int has_lcd;
42 };
43 #define ifkit(_sensors, _inputs, _outputs, _lcd)                        \
44 static struct driver_interfacekit ph_##_sensors##_inputs##_outputs = {  \
45         .sensors        = _sensors,                                     \
46         .inputs         = _inputs,                                      \
47         .outputs        = _outputs,                                     \
48         .has_lcd        = _lcd,                                         \
49 };
50 ifkit(0, 0, 4, 0);
51 ifkit(8, 8, 8, 0);
52 ifkit(0, 4, 7, 1);
53 ifkit(8, 8, 4, 0);
54 ifkit(0, 8, 8, 1);
55
56 struct phidget_interfacekit {
57         struct usb_device *udev;
58         struct usb_interface *intf;
59         struct driver_interfacekit *ifkit;
60         int outputs[MAX_INTERFACES];
61         int inputs[MAX_INTERFACES];
62         int sensors[MAX_INTERFACES];
63         u8 lcd_files_on;
64
65         struct urb *irq;
66         unsigned char *data;
67         dma_addr_t data_dma;
68 };
69
70 static struct usb_device_id id_table[] = {
71         {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT004),
72                 .driver_info = (kernel_ulong_t)&ph_004},
73         {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT888),
74                 .driver_info = (kernel_ulong_t)&ph_888},
75         {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT047),
76                 .driver_info = (kernel_ulong_t)&ph_047},
77         {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT088),
78                 .driver_info = (kernel_ulong_t)&ph_088},
79         {USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_INTERFACEKIT884),
80                 .driver_info = (kernel_ulong_t)&ph_884},
81         {}
82 };
83 MODULE_DEVICE_TABLE(usb, id_table);
84
85 static int change_outputs(struct phidget_interfacekit *kit, int output_num, int enable)
86 {
87         unsigned char *buffer;
88         int retval;
89         int n;
90
91         buffer = kzalloc(4, GFP_KERNEL);
92         if (!buffer) {
93                 dev_err(&kit->udev->dev, "%s - out of memory\n",
94                         __FUNCTION__);
95                 return -ENOMEM;
96         }
97
98         kit->outputs[output_num] = enable;
99         for (n=0; n<8; n++) {
100                 if (kit->outputs[n]) {
101                         buffer[0] |= 1 << n;
102                 }
103         }
104
105         dev_dbg(&kit->udev->dev, "sending data: %02x\n", buffer[0]);
106
107         retval = usb_control_msg(kit->udev,
108                          usb_sndctrlpipe(kit->udev, 0),
109                          0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2000);
110
111         if (retval != 4)
112                 dev_err(&kit->udev->dev, "usb_control_msg returned %d\n", 
113                                 retval);
114         kfree(buffer);
115
116         return retval < 0 ? retval : 0;
117 }
118
119 static int change_string(struct phidget_interfacekit *kit, const char *display, unsigned char row)
120 {
121         unsigned char *buffer;
122         unsigned char *form_buffer;
123         int retval = -ENOMEM;
124         int i,j, len, buf_ptr;
125         
126         buffer = kmalloc(8, GFP_KERNEL);
127         form_buffer = kmalloc(30, GFP_KERNEL);
128         if ((!buffer) || (!form_buffer)) {
129                 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
130                 goto exit;
131         }
132
133         len = strlen(display);
134         if (len > 20)
135                 len = 20;
136
137         dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
138
139         form_buffer[0] = row * 0x40 + 0x80;
140         form_buffer[1] = 0x02;
141         buf_ptr = 2;
142         for (i = 0; i<len; i++)
143                 form_buffer[buf_ptr++] = display[i];
144
145         for (i = 0; i < (20 - len); i++)
146                 form_buffer[buf_ptr++] = 0x20;
147         form_buffer[buf_ptr++] = 0x01;
148         form_buffer[buf_ptr++] = row * 0x40 + 0x80 + strlen(display);
149
150         for (i = 0; i < buf_ptr; i += 7) {
151                 if ((buf_ptr - i) > 7)
152                         len = 7;
153                 else
154                         len = (buf_ptr - i);
155                 for (j = 0; j < len; j++)
156                         buffer[j] = form_buffer[i + j];
157                 buffer[7] = len;
158
159                 retval = usb_control_msg(kit->udev,
160                                  usb_sndctrlpipe(kit->udev, 0),
161                                  0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
162                 if (retval < 0)
163                         goto exit;
164         }
165
166         retval = 0;
167 exit:
168         kfree(buffer);
169         kfree(form_buffer);
170
171         return retval;
172 }
173
174 #define set_lcd_line(number)    \
175 static ssize_t lcd_line_##number(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)      \
176 {                                                                                       \
177         struct usb_interface *intf = to_usb_interface(dev);                             \
178         struct phidget_interfacekit *kit = usb_get_intfdata(intf);                      \
179         change_string(kit, buf, number - 1);                                            \
180         return count;                                                                   \
181 }                                                                                       \
182 static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
183 set_lcd_line(1);
184 set_lcd_line(2);
185
186 static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
187 {
188         struct usb_interface *intf = to_usb_interface(dev);
189         struct phidget_interfacekit *kit = usb_get_intfdata(intf);
190         int enabled;
191         unsigned char *buffer;
192         int retval = -ENOMEM;
193         
194         buffer = kzalloc(8, GFP_KERNEL);
195         if (!buffer) {
196                 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
197                 goto exit;
198         }
199
200         if (sscanf(buf, "%d", &enabled) < 1) {
201                 retval = -EINVAL;
202                 goto exit;
203         }
204         if (enabled)
205                 buffer[0] = 0x01;
206         buffer[7] = 0x11;
207
208         dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
209         
210         retval = usb_control_msg(kit->udev,
211                          usb_sndctrlpipe(kit->udev, 0),
212                          0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
213         if (retval < 0)
214                 goto exit;
215
216         retval = count;
217 exit:
218         kfree(buffer);
219         return retval;
220 }
221 static DEVICE_ATTR(backlight, S_IWUGO, NULL, set_backlight);
222
223 static void remove_lcd_files(struct phidget_interfacekit *kit)
224 {
225         if (kit->lcd_files_on) {
226                 dev_dbg(&kit->udev->dev, "Removing lcd files\n");
227                 device_remove_file(&kit->intf->dev, &dev_attr_lcd_line_1);
228                 device_remove_file(&kit->intf->dev, &dev_attr_lcd_line_2);
229                 device_remove_file(&kit->intf->dev, &dev_attr_backlight);
230         }
231 }
232
233 static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
234 {
235         struct usb_interface *intf = to_usb_interface(dev);
236         struct phidget_interfacekit *kit = usb_get_intfdata(intf);
237         int enable;
238         
239         if (kit->ifkit->has_lcd == 0)
240                 return -ENODEV;
241
242         if (sscanf(buf, "%d", &enable) < 1)
243                 return -EINVAL;
244
245         if (enable) {
246                 if (!kit->lcd_files_on) {
247                         dev_dbg(&kit->udev->dev, "Adding lcd files\n");
248                         device_create_file(&kit->intf->dev, &dev_attr_lcd_line_1);
249                         device_create_file(&kit->intf->dev, &dev_attr_lcd_line_2);
250                         device_create_file(&kit->intf->dev, &dev_attr_backlight);
251                         kit->lcd_files_on = 1;
252                 }
253         } else {
254                 if (kit->lcd_files_on) {
255                         remove_lcd_files(kit);
256                         kit->lcd_files_on = 0;
257                 }
258         }
259         
260         return count;
261 }
262 static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
263
264 static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
265 {
266         struct phidget_interfacekit *kit = urb->context;
267         unsigned char *buffer = kit->data;
268         int status;
269         int n;
270
271         switch (urb->status) {
272         case 0:                 /* success */
273                 break;
274         case -ECONNRESET:       /* unlink */
275         case -ENOENT:
276         case -ESHUTDOWN:
277                 return;
278         /* -EPIPE:  should clear the halt */
279         default:                /* error */
280                 goto resubmit;
281         }
282
283         for (n=0; n<8; n++) {
284                 kit->inputs[n] = buffer[1] & (1 << n) ? 1 : 0;
285         }
286
287         if (buffer[0] & 1) {
288                 kit->sensors[4] = buffer[2] + (buffer[3] & 0x0f) * 256;
289                 kit->sensors[5] = buffer[4] + (buffer[3] & 0xf0) * 16;
290                 kit->sensors[6] = buffer[5] + (buffer[6] & 0x0f) * 256;
291                 kit->sensors[7] = buffer[7] + (buffer[6] & 0xf0) * 16;
292         } else {
293                 kit->sensors[0] = buffer[2] + (buffer[3] & 0x0f) * 256;
294                 kit->sensors[1] = buffer[4] + (buffer[3] & 0xf0) * 16;
295                 kit->sensors[2] = buffer[5] + (buffer[6] & 0x0f) * 256;
296                 kit->sensors[3] = buffer[7] + (buffer[6] & 0xf0) * 16;
297         }
298
299 resubmit:
300         status = usb_submit_urb(urb, SLAB_ATOMIC);
301         if (status)
302                 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
303                         kit->udev->bus->bus_name,
304                         kit->udev->devpath, status);
305 }
306
307 #define show_set_output(value)          \
308 static ssize_t set_output##value(struct device *dev, struct device_attribute *attr, const char *buf,    \
309                                                         size_t count)   \
310 {                                                                       \
311         struct usb_interface *intf = to_usb_interface(dev);             \
312         struct phidget_interfacekit *kit = usb_get_intfdata(intf);      \
313         int enabled;                                                    \
314         int retval;                                                     \
315                                                                         \
316         if (sscanf(buf, "%d", &enabled) < 1) {                          \
317                 return -EINVAL;                                         \
318         }                                                               \
319                                                                         \
320         retval = change_outputs(kit, value - 1, enabled ? 1 : 0);       \
321                                                                         \
322         return retval ? retval : count;                                 \
323 }                                                                       \
324                                                                         \
325 static ssize_t show_output##value(struct device *dev, struct device_attribute *attr, char *buf) \
326 {                                                                       \
327         struct usb_interface *intf = to_usb_interface(dev);             \
328         struct phidget_interfacekit *kit = usb_get_intfdata(intf);      \
329                                                                         \
330         return sprintf(buf, "%d\n", kit->outputs[value - 1]);           \
331 }                                                                       \
332 static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO,                    \
333                 show_output##value, set_output##value);
334 show_set_output(1);
335 show_set_output(2);
336 show_set_output(3);
337 show_set_output(4);
338 show_set_output(5);
339 show_set_output(6);
340 show_set_output(7);
341 show_set_output(8);     /* should be MAX_INTERFACES - 1 */
342
343 #define show_input(value)       \
344 static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf)  \
345 {                                                                       \
346         struct usb_interface *intf = to_usb_interface(dev);             \
347         struct phidget_interfacekit *kit = usb_get_intfdata(intf);      \
348                                                                         \
349         return sprintf(buf, "%d\n", kit->inputs[value - 1]);            \
350 }                                                                       \
351 static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
352
353 show_input(1);
354 show_input(2);
355 show_input(3);
356 show_input(4);
357 show_input(5);
358 show_input(6);
359 show_input(7);
360 show_input(8);          /* should be MAX_INTERFACES - 1 */
361
362 #define show_sensor(value)      \
363 static ssize_t show_sensor##value(struct device *dev, struct device_attribute *attr, char *buf) \
364 {                                                                       \
365         struct usb_interface *intf = to_usb_interface(dev);             \
366         struct phidget_interfacekit *kit = usb_get_intfdata(intf);      \
367                                                                         \
368         return sprintf(buf, "%d\n", kit->sensors[value - 1]);           \
369 }                                                                       \
370 static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
371
372 show_sensor(1);
373 show_sensor(2);
374 show_sensor(3);
375 show_sensor(4);
376 show_sensor(5);
377 show_sensor(6);
378 show_sensor(7);
379 show_sensor(8);         /* should be MAX_INTERFACES - 1 */
380
381 static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
382 {
383         struct usb_device *dev = interface_to_usbdev(intf);
384         struct usb_host_interface *interface;
385         struct usb_endpoint_descriptor *endpoint;
386         struct phidget_interfacekit *kit;
387         struct driver_interfacekit *ifkit;
388         int pipe, maxp;
389
390         ifkit = (struct driver_interfacekit *)id->driver_info;
391         if (!ifkit)
392                 return -ENODEV;
393
394         interface = intf->cur_altsetting;
395         if (interface->desc.bNumEndpoints != 1)
396                 return -ENODEV;
397
398         endpoint = &interface->endpoint[0].desc;
399         if (!(endpoint->bEndpointAddress & 0x80)) 
400                 return -ENODEV;
401         /*
402          * bmAttributes
403          */
404         pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
405         maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
406         
407         kit = kzalloc(sizeof(*kit), GFP_KERNEL);
408         if (kit  == NULL) {
409                 dev_err(&intf->dev, "%s - out of memory\n", __FUNCTION__);
410                 return -ENOMEM;
411         }
412         kit->ifkit = ifkit;
413
414         kit->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kit->data_dma);
415         if (!kit->data) {
416                 kfree(kit);
417                 return -ENOMEM;
418         }
419
420         kit->irq = usb_alloc_urb(0, GFP_KERNEL);
421         if (!kit->irq) {
422                 usb_buffer_free(dev, 8, kit->data, kit->data_dma);
423                 kfree(kit);
424                 return -ENOMEM;
425         }
426
427         kit->udev = usb_get_dev(dev);
428         kit->intf = intf;
429         usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
430                         (maxp > 8 ? 8 : maxp),
431                         interfacekit_irq, kit, endpoint->bInterval);
432         kit->irq->transfer_dma = kit->data_dma;
433         kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
434
435         usb_set_intfdata(intf, kit);
436
437         if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
438                 return -EIO;
439         }
440
441         if (ifkit->outputs >= 4) {
442                 device_create_file(&intf->dev, &dev_attr_output1);
443                 device_create_file(&intf->dev, &dev_attr_output2);
444                 device_create_file(&intf->dev, &dev_attr_output3);
445                 device_create_file(&intf->dev, &dev_attr_output4);
446         }
447         if (ifkit->outputs == 8) {
448                 device_create_file(&intf->dev, &dev_attr_output5);
449                 device_create_file(&intf->dev, &dev_attr_output6);
450                 device_create_file(&intf->dev, &dev_attr_output7);
451                 device_create_file(&intf->dev, &dev_attr_output8);
452         } 
453
454         if (ifkit->inputs >= 4) {
455                 device_create_file(&intf->dev, &dev_attr_input1);
456                 device_create_file(&intf->dev, &dev_attr_input2);
457                 device_create_file(&intf->dev, &dev_attr_input3);
458                 device_create_file(&intf->dev, &dev_attr_input4);
459         }
460         if (ifkit->inputs == 8) {
461                 device_create_file(&intf->dev, &dev_attr_input5);
462                 device_create_file(&intf->dev, &dev_attr_input6);
463                 device_create_file(&intf->dev, &dev_attr_input7);
464                 device_create_file(&intf->dev, &dev_attr_input8);
465         }
466
467         if (ifkit->sensors >= 4) {
468                 device_create_file(&intf->dev, &dev_attr_sensor1);
469                 device_create_file(&intf->dev, &dev_attr_sensor2);
470                 device_create_file(&intf->dev, &dev_attr_sensor3);
471                 device_create_file(&intf->dev, &dev_attr_sensor4);
472         }
473         if (ifkit->sensors >= 7) {
474                 device_create_file(&intf->dev, &dev_attr_sensor5);
475                 device_create_file(&intf->dev, &dev_attr_sensor6);
476                 device_create_file(&intf->dev, &dev_attr_sensor7);
477         }
478         if (ifkit->sensors == 8) {
479                 device_create_file(&intf->dev, &dev_attr_sensor8);
480         }
481
482         if (ifkit->has_lcd)
483                 device_create_file(&intf->dev, &dev_attr_lcd);
484
485         dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
486                         ifkit->sensors, ifkit->inputs, ifkit->outputs);
487
488         return 0;
489 }
490
491 static void interfacekit_disconnect(struct usb_interface *interface)
492 {
493         struct phidget_interfacekit *kit;
494
495         kit = usb_get_intfdata(interface);
496         usb_set_intfdata(interface, NULL);
497         if (!kit)
498                 return;
499
500         if (kit->ifkit->outputs >= 4) {
501                 device_remove_file(&interface->dev, &dev_attr_output1);
502                 device_remove_file(&interface->dev, &dev_attr_output2);
503                 device_remove_file(&interface->dev, &dev_attr_output3);
504                 device_remove_file(&interface->dev, &dev_attr_output4);
505         }
506         if (kit->ifkit->outputs == 8) {
507                 device_remove_file(&interface->dev, &dev_attr_output5);
508                 device_remove_file(&interface->dev, &dev_attr_output6);
509                 device_remove_file(&interface->dev, &dev_attr_output7);
510                 device_remove_file(&interface->dev, &dev_attr_output8);
511         }
512
513         if (kit->ifkit->inputs >= 4) {
514                 device_remove_file(&interface->dev, &dev_attr_input1);
515                 device_remove_file(&interface->dev, &dev_attr_input2);
516                 device_remove_file(&interface->dev, &dev_attr_input3);
517                 device_remove_file(&interface->dev, &dev_attr_input4);
518         }
519         if (kit->ifkit->inputs == 8) {
520                 device_remove_file(&interface->dev, &dev_attr_input5);
521                 device_remove_file(&interface->dev, &dev_attr_input6);
522                 device_remove_file(&interface->dev, &dev_attr_input7);
523                 device_remove_file(&interface->dev, &dev_attr_input8);
524         }
525
526         if (kit->ifkit->sensors >= 4) {
527                 device_remove_file(&interface->dev, &dev_attr_sensor1);
528                 device_remove_file(&interface->dev, &dev_attr_sensor2);
529                 device_remove_file(&interface->dev, &dev_attr_sensor3);
530                 device_remove_file(&interface->dev, &dev_attr_sensor4);
531         }
532         if (kit->ifkit->sensors >= 7) {
533                 device_remove_file(&interface->dev, &dev_attr_sensor5);
534                 device_remove_file(&interface->dev, &dev_attr_sensor6);
535                 device_remove_file(&interface->dev, &dev_attr_sensor7);
536         }
537         if (kit->ifkit->sensors == 8) {
538                 device_remove_file(&interface->dev, &dev_attr_sensor8);
539         }
540         if (kit->ifkit->has_lcd)
541                 device_remove_file(&interface->dev, &dev_attr_lcd);
542
543         dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
544                 kit->ifkit->sensors, kit->ifkit->inputs, kit->ifkit->outputs);
545
546         usb_kill_urb(kit->irq);
547         usb_free_urb(kit->irq);
548         usb_buffer_free(kit->udev, 8, kit->data, kit->data_dma);
549
550         usb_put_dev(kit->udev);
551         kfree(kit);
552 }
553
554 static struct usb_driver interfacekit_driver = {
555         .name = "phidgetkit",
556         .probe = interfacekit_probe,
557         .disconnect = interfacekit_disconnect,
558         .id_table = id_table
559 };
560
561 static int __init interfacekit_init(void)
562 {
563         int retval = 0;
564
565         retval = usb_register(&interfacekit_driver);
566         if (retval)
567                 err("usb_register failed. Error number %d", retval);
568
569         return retval;
570 }
571
572 static void __exit interfacekit_exit(void)
573 {
574         usb_deregister(&interfacekit_driver);
575 }
576
577 module_init(interfacekit_init);
578 module_exit(interfacekit_exit);
579
580 MODULE_AUTHOR(DRIVER_AUTHOR);
581 MODULE_DESCRIPTION(DRIVER_DESC);
582 MODULE_LICENSE("GPL");