blob: bfbbbfbb92bcca4ef3b56fe8aea5241ef057ca7b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB PhidgetInterfaceKit driver 1.0
3 *
Sean Young69165c22006-05-02 11:44:43 +00004 * Copyright (C) 2004, 2006 Sean Young <sean@mess.org>
5 * Copyright (C) 2005 Daniel Saakes <daniel@saakes.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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
Sean Young69165c22006-05-02 11:44:43 +000028#define USB_DEVICE_ID_INTERFACEKIT01616 0x0044
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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
Sean Young69165c22006-05-02 11:44:43 +000036#define MAX_INTERFACES 16
37
38#define URB_INT_SIZE 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
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);
Sean Young69165c22006-05-02 11:44:43 +000058ifkit(0, 16, 16, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Sean Young69165c22006-05-02 11:44:43 +000060struct interfacekit {
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct usb_device *udev;
62 struct usb_interface *intf;
63 struct driver_interfacekit *ifkit;
Sean Young69165c22006-05-02 11:44:43 +000064 unsigned long outputs;
65 u8 inputs[MAX_INTERFACES];
66 u16 sensors[MAX_INTERFACES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 u8 lcd_files_on;
68
69 struct urb *irq;
70 unsigned char *data;
71 dma_addr_t data_dma;
Sean Young69165c22006-05-02 11:44:43 +000072
73 struct work_struct do_notify;
74 unsigned long input_events;
75 unsigned long sensor_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
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},
Sean Young69165c22006-05-02 11:44:43 +000087 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT01616),
88 .driver_info = (kernel_ulong_t)&ph_01616},
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 {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
Sean Young69165c22006-05-02 11:44:43 +000095static int change_outputs(struct interfacekit *kit, int output_num, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Sean Young69165c22006-05-02 11:44:43 +000097 u8 *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 int retval;
Sean Young69165c22006-05-02 11:44:43 +000099
100 if (enable)
101 set_bit(output_num, &kit->outputs);
102 else
103 clear_bit(output_num, &kit->outputs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Oliver Neukum17590842006-01-06 22:41:51 +0100105 buffer = kzalloc(4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (!buffer) {
Sean Young69165c22006-05-02 11:44:43 +0000107 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -ENOMEM;
109 }
Sean Young69165c22006-05-02 11:44:43 +0000110 buffer[0] = (u8)kit->outputs;
111 buffer[1] = (u8)(kit->outputs >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Sean Young69165c22006-05-02 11:44:43 +0000113 dev_dbg(&kit->udev->dev, "sending data: 0x%04x\n", (u16)kit->outputs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
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
Sean Young69165c22006-05-02 11:44:43 +0000127static int change_string(struct interfacekit *kit, const char *display, unsigned char row)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 unsigned char *buffer;
Sean Young69165c22006-05-02 11:44:43 +0000130 unsigned char *form_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 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) \
Yani Ioannou060b8842005-05-17 06:44:04 -0400183static ssize_t lcd_line_##number(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{ \
185 struct usb_interface *intf = to_usb_interface(dev); \
Sean Young69165c22006-05-02 11:44:43 +0000186 struct interfacekit *kit = usb_get_intfdata(intf); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 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
Yani Ioannou060b8842005-05-17 06:44:04 -0400194static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct usb_interface *intf = to_usb_interface(dev);
Sean Young69165c22006-05-02 11:44:43 +0000197 struct interfacekit *kit = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int enabled;
199 unsigned char *buffer;
200 int retval = -ENOMEM;
201
Oliver Neukum17590842006-01-06 22:41:51 +0100202 buffer = kzalloc(8, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 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
Sean Young69165c22006-05-02 11:44:43 +0000231static void remove_lcd_files(struct interfacekit *kit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
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
Yani Ioannou060b8842005-05-17 06:44:04 -0400241static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct usb_interface *intf = to_usb_interface(dev);
Sean Young69165c22006-05-02 11:44:43 +0000244 struct interfacekit *kit = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 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{
Sean Young69165c22006-05-02 11:44:43 +0000274 struct interfacekit *kit = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 unsigned char *buffer = kit->data;
Sean Young69165c22006-05-02 11:44:43 +0000276 int i, level, sensor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
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
Sean Young69165c22006-05-02 11:44:43 +0000291 /* 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
Sean Young69165c22006-05-02 11:44:43 +0000316 /* 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Sean Young69165c22006-05-02 11:44:43 +0000345 if (kit->input_events || kit->sensor_events)
346 schedule_work(&kit->do_notify);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348resubmit:
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
Sean Young69165c22006-05-02 11:44:43 +0000356static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#define show_set_output(value) \
Sean Young69165c22006-05-02 11:44:43 +0000378static ssize_t set_output##value(struct device *dev, struct device_attribute *attr, const char *buf, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 size_t count) \
380{ \
381 struct usb_interface *intf = to_usb_interface(dev); \
Sean Young69165c22006-05-02 11:44:43 +0000382 struct interfacekit *kit = usb_get_intfdata(intf); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int enabled; \
384 int retval; \
385 \
Sean Young69165c22006-05-02 11:44:43 +0000386 if (sscanf(buf, "%d", &enabled) < 1) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 \
Sean Young69165c22006-05-02 11:44:43 +0000389 retval = change_outputs(kit, value - 1, enabled); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 \
391 return retval ? retval : count; \
392} \
393 \
Yani Ioannou060b8842005-05-17 06:44:04 -0400394static ssize_t show_output##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{ \
396 struct usb_interface *intf = to_usb_interface(dev); \
Sean Young69165c22006-05-02 11:44:43 +0000397 struct interfacekit *kit = usb_get_intfdata(intf); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 \
Sean Young69165c22006-05-02 11:44:43 +0000399 return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400} \
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);
Sean Young69165c22006-05-02 11:44:43 +0000410show_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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420#define show_input(value) \
Yani Ioannou060b8842005-05-17 06:44:04 -0400421static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{ \
423 struct usb_interface *intf = to_usb_interface(dev); \
Sean Young69165c22006-05-02 11:44:43 +0000424 struct interfacekit *kit = usb_get_intfdata(intf); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 \
Sean Young69165c22006-05-02 11:44:43 +0000426 return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427} \
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);
Sean Young69165c22006-05-02 11:44:43 +0000437show_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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447#define show_sensor(value) \
Yani Ioannou060b8842005-05-17 06:44:04 -0400448static ssize_t show_sensor##value(struct device *dev, struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{ \
450 struct usb_interface *intf = to_usb_interface(dev); \
Sean Young69165c22006-05-02 11:44:43 +0000451 struct interfacekit *kit = usb_get_intfdata(intf); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 \
Sean Young69165c22006-05-02 11:44:43 +0000453 return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454} \
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);
Sean Young69165c22006-05-02 11:44:43 +0000464show_sensor(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
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;
Sean Young69165c22006-05-02 11:44:43 +0000471 struct interfacekit *kit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 struct driver_interfacekit *ifkit;
Sean Young69165c22006-05-02 11:44:43 +0000473 int pipe, maxp, rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
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
Oliver Neukum17590842006-01-06 22:41:51 +0100492 kit = kzalloc(sizeof(*kit), GFP_KERNEL);
Sean Young69165c22006-05-02 11:44:43 +0000493 if (!kit)
494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Sean Young69165c22006-05-02 11:44:43 +0000496 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 kit->irq = usb_alloc_urb(0, GFP_KERNEL);
Sean Young69165c22006-05-02 11:44:43 +0000502 if (!kit->irq)
503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 kit->udev = usb_get_dev(dev);
506 kit->intf = intf;
Sean Young69165c22006-05-02 11:44:43 +0000507 INIT_WORK(&kit->do_notify, do_notify, kit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
Sean Young69165c22006-05-02 11:44:43 +0000509 maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 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)) {
Sean Young69165c22006-05-02 11:44:43 +0000517 rc = -EIO;
518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
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 }
Sean Young69165c22006-05-02 11:44:43 +0000527 if (ifkit->outputs >= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 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 }
Sean Young69165c22006-05-02 11:44:43 +0000533 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
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 }
Sean Young69165c22006-05-02 11:44:43 +0000550 if (ifkit->inputs >= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 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 }
Sean Young69165c22006-05-02 11:44:43 +0000556 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
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 }
Sean Young69165c22006-05-02 11:44:43 +0000578 if (ifkit->sensors == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 device_create_file(&intf->dev, &dev_attr_sensor8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
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;
Sean Young69165c22006-05-02 11:44:43 +0000588
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601static void interfacekit_disconnect(struct usb_interface *interface)
602{
Sean Young69165c22006-05-02 11:44:43 +0000603 struct interfacekit *kit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 kit = usb_get_intfdata(interface);
606 usb_set_intfdata(interface, NULL);
607 if (!kit)
608 return;
609
Sean Young69165c22006-05-02 11:44:43 +0000610 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 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 }
Sean Young69165c22006-05-02 11:44:43 +0000622 if (kit->ifkit->outputs >= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 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 }
Sean Young69165c22006-05-02 11:44:43 +0000628 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
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 }
Sean Young69165c22006-05-02 11:44:43 +0000645 if (kit->ifkit->inputs >= 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 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 }
Sean Young69165c22006-05-02 11:44:43 +0000651 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
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 }
Sean Young69165c22006-05-02 11:44:43 +0000673 if (kit->ifkit->sensors == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 device_remove_file(&interface->dev, &dev_attr_sensor8);
Sean Young69165c22006-05-02 11:44:43 +0000675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 usb_put_dev(kit->udev);
683 kfree(kit);
684}
685
686static struct usb_driver interfacekit_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 .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");