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