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