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