blob: abb4dcd811ac1394d6de4ff48acc6baed489c3e1 [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
Sean Young912b24c2006-07-10 09:56:25 +000023#include "phidget.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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
Sean Young69165c22006-05-02 11:44:43 +000030#define USB_DEVICE_ID_INTERFACEKIT01616 0x0044
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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
Sean Young69165c22006-05-02 11:44:43 +000038#define MAX_INTERFACES 16
39
40#define URB_INT_SIZE 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42struct driver_interfacekit {
43 int sensors;
44 int inputs;
45 int outputs;
46 int has_lcd;
Sean Young238d0e72006-09-24 19:26:57 +000047 int amnesiac;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
Sean Young238d0e72006-09-24 19:26:57 +000049
50#define ifkit(_sensors, _inputs, _outputs, _lcd, _amnesiac) \
51{ \
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 .sensors = _sensors, \
53 .inputs = _inputs, \
54 .outputs = _outputs, \
55 .has_lcd = _lcd, \
Sean Young238d0e72006-09-24 19:26:57 +000056 .amnesiac = _amnesiac \
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
Sean Young238d0e72006-09-24 19:26:57 +000058
59static const struct driver_interfacekit ph_004 = ifkit(0, 0, 4, 0, 0);
60static const struct driver_interfacekit ph_888n = ifkit(8, 8, 8, 0, 1);
61static const struct driver_interfacekit ph_888o = ifkit(8, 8, 8, 0, 0);
62static const struct driver_interfacekit ph_047 = ifkit(0, 4, 7, 1, 0);
63static const struct driver_interfacekit ph_884 = ifkit(8, 8, 4, 0, 0);
64static const struct driver_interfacekit ph_088 = ifkit(0, 8, 8, 1, 0);
65static const struct driver_interfacekit ph_01616 = ifkit(0, 16, 16, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Sean Young912b24c2006-07-10 09:56:25 +000067static unsigned long device_no;
68
Sean Young69165c22006-05-02 11:44:43 +000069struct interfacekit {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct usb_device *udev;
71 struct usb_interface *intf;
72 struct driver_interfacekit *ifkit;
Sean Young912b24c2006-07-10 09:56:25 +000073 struct device *dev;
Sean Young69165c22006-05-02 11:44:43 +000074 unsigned long outputs;
Sean Young912b24c2006-07-10 09:56:25 +000075 int dev_no;
Sean Young69165c22006-05-02 11:44:43 +000076 u8 inputs[MAX_INTERFACES];
77 u16 sensors[MAX_INTERFACES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 u8 lcd_files_on;
79
80 struct urb *irq;
81 unsigned char *data;
82 dma_addr_t data_dma;
Sean Young69165c22006-05-02 11:44:43 +000083
84 struct work_struct do_notify;
Sean Young238d0e72006-09-24 19:26:57 +000085 struct work_struct do_resubmit;
Sean Young69165c22006-05-02 11:44:43 +000086 unsigned long input_events;
87 unsigned long sensor_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
90static struct usb_device_id id_table[] = {
91 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT004),
92 .driver_info = (kernel_ulong_t)&ph_004},
Sean Young238d0e72006-09-24 19:26:57 +000093 {USB_DEVICE_VER(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT888, 0, 0x814),
94 .driver_info = (kernel_ulong_t)&ph_888o},
95 {USB_DEVICE_VER(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT888, 0x0815, 0xffff),
96 .driver_info = (kernel_ulong_t)&ph_888n},
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT047),
98 .driver_info = (kernel_ulong_t)&ph_047},
99 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT088),
100 .driver_info = (kernel_ulong_t)&ph_088},
Sean Young69165c22006-05-02 11:44:43 +0000101 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT01616),
102 .driver_info = (kernel_ulong_t)&ph_01616},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 {USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_INTERFACEKIT884),
104 .driver_info = (kernel_ulong_t)&ph_884},
105 {}
106};
107MODULE_DEVICE_TABLE(usb, id_table);
108
Sean Young238d0e72006-09-24 19:26:57 +0000109static int set_outputs(struct interfacekit *kit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Sean Young69165c22006-05-02 11:44:43 +0000111 u8 *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int retval;
Sean Young69165c22006-05-02 11:44:43 +0000113
Oliver Neukum17590842006-01-06 22:41:51 +0100114 buffer = kzalloc(4, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (!buffer) {
Sean Young69165c22006-05-02 11:44:43 +0000116 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -ENOMEM;
118 }
Sean Young69165c22006-05-02 11:44:43 +0000119 buffer[0] = (u8)kit->outputs;
120 buffer[1] = (u8)(kit->outputs >> 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Sean Young69165c22006-05-02 11:44:43 +0000122 dev_dbg(&kit->udev->dev, "sending data: 0x%04x\n", (u16)kit->outputs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 retval = usb_control_msg(kit->udev,
125 usb_sndctrlpipe(kit->udev, 0),
126 0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2000);
127
128 if (retval != 4)
129 dev_err(&kit->udev->dev, "usb_control_msg returned %d\n",
130 retval);
131 kfree(buffer);
132
Sean Young238d0e72006-09-24 19:26:57 +0000133 if (kit->ifkit->amnesiac)
134 schedule_delayed_work(&kit->do_resubmit, HZ / 2);
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return retval < 0 ? retval : 0;
137}
138
Sean Young69165c22006-05-02 11:44:43 +0000139static int change_string(struct interfacekit *kit, const char *display, unsigned char row)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 unsigned char *buffer;
Sean Young69165c22006-05-02 11:44:43 +0000142 unsigned char *form_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int retval = -ENOMEM;
144 int i,j, len, buf_ptr;
145
146 buffer = kmalloc(8, GFP_KERNEL);
147 form_buffer = kmalloc(30, GFP_KERNEL);
148 if ((!buffer) || (!form_buffer)) {
149 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
150 goto exit;
151 }
152
153 len = strlen(display);
154 if (len > 20)
155 len = 20;
156
157 dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
158
159 form_buffer[0] = row * 0x40 + 0x80;
160 form_buffer[1] = 0x02;
161 buf_ptr = 2;
162 for (i = 0; i<len; i++)
163 form_buffer[buf_ptr++] = display[i];
164
165 for (i = 0; i < (20 - len); i++)
166 form_buffer[buf_ptr++] = 0x20;
167 form_buffer[buf_ptr++] = 0x01;
168 form_buffer[buf_ptr++] = row * 0x40 + 0x80 + strlen(display);
169
170 for (i = 0; i < buf_ptr; i += 7) {
171 if ((buf_ptr - i) > 7)
172 len = 7;
173 else
174 len = (buf_ptr - i);
175 for (j = 0; j < len; j++)
176 buffer[j] = form_buffer[i + j];
177 buffer[7] = len;
178
179 retval = usb_control_msg(kit->udev,
180 usb_sndctrlpipe(kit->udev, 0),
181 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
182 if (retval < 0)
183 goto exit;
184 }
185
186 retval = 0;
187exit:
188 kfree(buffer);
189 kfree(form_buffer);
190
191 return retval;
192}
193
194#define set_lcd_line(number) \
Sean Young912b24c2006-07-10 09:56:25 +0000195static ssize_t lcd_line_##number(struct device *dev, \
196 struct device_attribute *attr, \
197 const char *buf, size_t count) \
198{ \
199 struct interfacekit *kit = dev_get_drvdata(dev); \
200 change_string(kit, buf, number - 1); \
201 return count; \
Sean Youngda308e82006-08-11 09:28:28 +0000202}
203
204#define lcd_line_attr(number) \
205 __ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number)
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207set_lcd_line(1);
208set_lcd_line(2);
209
Yani Ioannou060b8842005-05-17 06:44:04 -0400210static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Sean Young912b24c2006-07-10 09:56:25 +0000212 struct interfacekit *kit = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 int enabled;
214 unsigned char *buffer;
215 int retval = -ENOMEM;
216
Oliver Neukum17590842006-01-06 22:41:51 +0100217 buffer = kzalloc(8, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!buffer) {
219 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
220 goto exit;
221 }
222
223 if (sscanf(buf, "%d", &enabled) < 1) {
224 retval = -EINVAL;
225 goto exit;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (enabled)
228 buffer[0] = 0x01;
229 buffer[7] = 0x11;
230
231 dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
232
233 retval = usb_control_msg(kit->udev,
234 usb_sndctrlpipe(kit->udev, 0),
235 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
236 if (retval < 0)
237 goto exit;
238
239 retval = count;
240exit:
241 kfree(buffer);
242 return retval;
243}
Sean Youngda308e82006-08-11 09:28:28 +0000244
245static struct device_attribute dev_lcd_line_attrs[] = {
246 lcd_line_attr(1),
247 lcd_line_attr(2),
248 __ATTR(backlight, S_IWUGO, NULL, set_backlight)
249};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Sean Young69165c22006-05-02 11:44:43 +0000251static void remove_lcd_files(struct interfacekit *kit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Sean Youngda308e82006-08-11 09:28:28 +0000253 int i;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (kit->lcd_files_on) {
256 dev_dbg(&kit->udev->dev, "Removing lcd files\n");
Sean Youngda308e82006-08-11 09:28:28 +0000257
258 for (i=0; i<ARRAY_SIZE(dev_lcd_line_attrs); i++)
259 device_remove_file(kit->dev, &dev_lcd_line_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261}
262
Yani Ioannou060b8842005-05-17 06:44:04 -0400263static 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 -0700264{
Sean Young912b24c2006-07-10 09:56:25 +0000265 struct interfacekit *kit = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int enable;
Sean Youngda308e82006-08-11 09:28:28 +0000267 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 if (kit->ifkit->has_lcd == 0)
270 return -ENODEV;
271
272 if (sscanf(buf, "%d", &enable) < 1)
273 return -EINVAL;
274
275 if (enable) {
276 if (!kit->lcd_files_on) {
277 dev_dbg(&kit->udev->dev, "Adding lcd files\n");
Sean Youngda308e82006-08-11 09:28:28 +0000278 for (i=0; i<ARRAY_SIZE(dev_lcd_line_attrs); i++) {
279 rc = device_create_file(kit->dev,
280 &dev_lcd_line_attrs[i]);
281 if (rc)
282 goto out;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 kit->lcd_files_on = 1;
285 }
286 } else {
287 if (kit->lcd_files_on) {
288 remove_lcd_files(kit);
289 kit->lcd_files_on = 0;
290 }
291 }
292
293 return count;
Sean Youngda308e82006-08-11 09:28:28 +0000294out:
295 while (i-- > 0)
296 device_remove_file(kit->dev, &dev_lcd_line_attrs[i]);
297
298 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
Sean Youngda308e82006-08-11 09:28:28 +0000300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
302
David Howells7d12e782006-10-05 14:55:46 +0100303static void interfacekit_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Sean Young69165c22006-05-02 11:44:43 +0000305 struct interfacekit *kit = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned char *buffer = kit->data;
Sean Young69165c22006-05-02 11:44:43 +0000307 int i, level, sensor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 switch (urb->status) {
311 case 0: /* success */
312 break;
313 case -ECONNRESET: /* unlink */
314 case -ENOENT:
315 case -ESHUTDOWN:
316 return;
317 /* -EPIPE: should clear the halt */
318 default: /* error */
319 goto resubmit;
320 }
321
Sean Young69165c22006-05-02 11:44:43 +0000322 /* digital inputs */
323 if (kit->ifkit->inputs == 16) {
324 for (i=0; i < 8; i++) {
325 level = (buffer[0] >> i) & 1;
326 if (kit->inputs[i] != level) {
327 kit->inputs[i] = level;
328 set_bit(i, &kit->input_events);
329 }
330 level = (buffer[1] >> i) & 1;
331 if (kit->inputs[8 + i] != level) {
332 kit->inputs[8 + i] = level;
333 set_bit(8 + i, &kit->input_events);
334 }
335 }
336 }
337 else if (kit->ifkit->inputs == 8) {
338 for (i=0; i < 8; i++) {
339 level = (buffer[1] >> i) & 1;
340 if (kit->inputs[i] != level) {
341 kit->inputs[i] = level;
342 set_bit(i, &kit->input_events);
343 }
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Sean Young69165c22006-05-02 11:44:43 +0000347 /* analog inputs */
348 if (kit->ifkit->sensors) {
349 sensor = (buffer[0] & 1) ? 4 : 0;
350
351 level = buffer[2] + (buffer[3] & 0x0f) * 256;
352 if (level != kit->sensors[sensor]) {
353 kit->sensors[sensor] = level;
354 set_bit(sensor, &kit->sensor_events);
355 }
356 sensor++;
357 level = buffer[4] + (buffer[3] & 0xf0) * 16;
358 if (level != kit->sensors[sensor]) {
359 kit->sensors[sensor] = level;
360 set_bit(sensor, &kit->sensor_events);
361 }
362 sensor++;
363 level = buffer[5] + (buffer[6] & 0x0f) * 256;
364 if (level != kit->sensors[sensor]) {
365 kit->sensors[sensor] = level;
366 set_bit(sensor, &kit->sensor_events);
367 }
368 sensor++;
369 level = buffer[7] + (buffer[6] & 0xf0) * 16;
370 if (level != kit->sensors[sensor]) {
371 kit->sensors[sensor] = level;
372 set_bit(sensor, &kit->sensor_events);
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Sean Young69165c22006-05-02 11:44:43 +0000376 if (kit->input_events || kit->sensor_events)
377 schedule_work(&kit->do_notify);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379resubmit:
380 status = usb_submit_urb(urb, SLAB_ATOMIC);
381 if (status)
382 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
383 kit->udev->bus->bus_name,
384 kit->udev->devpath, status);
385}
386
Sean Young69165c22006-05-02 11:44:43 +0000387static void do_notify(void *data)
388{
389 struct interfacekit *kit = data;
390 int i;
391 char sysfs_file[8];
392
393 for (i=0; i<kit->ifkit->inputs; i++) {
394 if (test_and_clear_bit(i, &kit->input_events)) {
395 sprintf(sysfs_file, "input%d", i + 1);
Sean Young912b24c2006-07-10 09:56:25 +0000396 sysfs_notify(&kit->dev->kobj, NULL, sysfs_file);
Sean Young69165c22006-05-02 11:44:43 +0000397 }
398 }
399
400 for (i=0; i<kit->ifkit->sensors; i++) {
401 if (test_and_clear_bit(i, &kit->sensor_events)) {
402 sprintf(sysfs_file, "sensor%d", i + 1);
Sean Young912b24c2006-07-10 09:56:25 +0000403 sysfs_notify(&kit->dev->kobj, NULL, sysfs_file);
Sean Young69165c22006-05-02 11:44:43 +0000404 }
405 }
406}
407
Sean Young238d0e72006-09-24 19:26:57 +0000408static void do_resubmit(void *data)
409{
410 set_outputs(data);
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#define show_set_output(value) \
Sean Young912b24c2006-07-10 09:56:25 +0000414static ssize_t set_output##value(struct device *dev, \
415 struct device_attribute *attr, \
416 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{ \
Sean Young912b24c2006-07-10 09:56:25 +0000418 struct interfacekit *kit = dev_get_drvdata(dev); \
Sean Young238d0e72006-09-24 19:26:57 +0000419 int enable; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int retval; \
421 \
Sean Young238d0e72006-09-24 19:26:57 +0000422 if (sscanf(buf, "%d", &enable) < 1) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -EINVAL; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 \
Sean Young238d0e72006-09-24 19:26:57 +0000425 if (enable) \
426 set_bit(value - 1, &kit->outputs); \
427 else \
428 clear_bit(value - 1, &kit->outputs); \
429 \
430 retval = set_outputs(kit); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 \
432 return retval ? retval : count; \
433} \
434 \
Sean Young912b24c2006-07-10 09:56:25 +0000435static ssize_t show_output##value(struct device *dev, \
436 struct device_attribute *attr, \
437 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{ \
Sean Young912b24c2006-07-10 09:56:25 +0000439 struct interfacekit *kit = dev_get_drvdata(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 \
Sean Young69165c22006-05-02 11:44:43 +0000441 return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
Sean Youngda308e82006-08-11 09:28:28 +0000442}
443
444#define output_attr(value) \
445 __ATTR(output##value, S_IWUGO | S_IRUGO, \
446 show_output##value, set_output##value)
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448show_set_output(1);
449show_set_output(2);
450show_set_output(3);
451show_set_output(4);
452show_set_output(5);
453show_set_output(6);
454show_set_output(7);
Sean Young69165c22006-05-02 11:44:43 +0000455show_set_output(8);
456show_set_output(9);
457show_set_output(10);
458show_set_output(11);
459show_set_output(12);
460show_set_output(13);
461show_set_output(14);
462show_set_output(15);
463show_set_output(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Sean Youngda308e82006-08-11 09:28:28 +0000465static struct device_attribute dev_output_attrs[] = {
466 output_attr(1), output_attr(2), output_attr(3), output_attr(4),
467 output_attr(5), output_attr(6), output_attr(7), output_attr(8),
468 output_attr(9), output_attr(10), output_attr(11), output_attr(12),
469 output_attr(13), output_attr(14), output_attr(15), output_attr(16)
470};
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#define show_input(value) \
Sean Youngda308e82006-08-11 09:28:28 +0000473static ssize_t show_input##value(struct device *dev, \
474 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{ \
Sean Young912b24c2006-07-10 09:56:25 +0000476 struct interfacekit *kit = dev_get_drvdata(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 \
Sean Young69165c22006-05-02 11:44:43 +0000478 return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
Sean Youngda308e82006-08-11 09:28:28 +0000479}
480
481#define input_attr(value) \
482 __ATTR(input##value, S_IRUGO, show_input##value, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484show_input(1);
485show_input(2);
486show_input(3);
487show_input(4);
488show_input(5);
489show_input(6);
490show_input(7);
Sean Young69165c22006-05-02 11:44:43 +0000491show_input(8);
492show_input(9);
493show_input(10);
494show_input(11);
495show_input(12);
496show_input(13);
497show_input(14);
498show_input(15);
499show_input(16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Sean Youngda308e82006-08-11 09:28:28 +0000501static struct device_attribute dev_input_attrs[] = {
502 input_attr(1), input_attr(2), input_attr(3), input_attr(4),
503 input_attr(5), input_attr(6), input_attr(7), input_attr(8),
504 input_attr(9), input_attr(10), input_attr(11), input_attr(12),
505 input_attr(13), input_attr(14), input_attr(15), input_attr(16)
506};
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508#define show_sensor(value) \
Sean Young912b24c2006-07-10 09:56:25 +0000509static ssize_t show_sensor##value(struct device *dev, \
510 struct device_attribute *attr, \
511 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{ \
Sean Young912b24c2006-07-10 09:56:25 +0000513 struct interfacekit *kit = dev_get_drvdata(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 \
Sean Young69165c22006-05-02 11:44:43 +0000515 return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
Sean Youngda308e82006-08-11 09:28:28 +0000516}
517
518#define sensor_attr(value) \
519 __ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521show_sensor(1);
522show_sensor(2);
523show_sensor(3);
524show_sensor(4);
525show_sensor(5);
526show_sensor(6);
527show_sensor(7);
Sean Young69165c22006-05-02 11:44:43 +0000528show_sensor(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Sean Youngda308e82006-08-11 09:28:28 +0000530static struct device_attribute dev_sensor_attrs[] = {
531 sensor_attr(1), sensor_attr(2), sensor_attr(3), sensor_attr(4),
532 sensor_attr(5), sensor_attr(6), sensor_attr(7), sensor_attr(8)
533};
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
536{
537 struct usb_device *dev = interface_to_usbdev(intf);
538 struct usb_host_interface *interface;
539 struct usb_endpoint_descriptor *endpoint;
Sean Young69165c22006-05-02 11:44:43 +0000540 struct interfacekit *kit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct driver_interfacekit *ifkit;
Sean Young69165c22006-05-02 11:44:43 +0000542 int pipe, maxp, rc = -ENOMEM;
Sean Youngda308e82006-08-11 09:28:28 +0000543 int bit, value, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 ifkit = (struct driver_interfacekit *)id->driver_info;
546 if (!ifkit)
547 return -ENODEV;
548
549 interface = intf->cur_altsetting;
550 if (interface->desc.bNumEndpoints != 1)
551 return -ENODEV;
552
553 endpoint = &interface->endpoint[0].desc;
554 if (!(endpoint->bEndpointAddress & 0x80))
555 return -ENODEV;
556 /*
557 * bmAttributes
558 */
559 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
560 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
561
Oliver Neukum17590842006-01-06 22:41:51 +0100562 kit = kzalloc(sizeof(*kit), GFP_KERNEL);
Sean Young69165c22006-05-02 11:44:43 +0000563 if (!kit)
564 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Sean Young912b24c2006-07-10 09:56:25 +0000566 kit->dev_no = -1;
Sean Young69165c22006-05-02 11:44:43 +0000567 kit->ifkit = ifkit;
568 kit->data = usb_buffer_alloc(dev, URB_INT_SIZE, SLAB_ATOMIC, &kit->data_dma);
569 if (!kit->data)
570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 kit->irq = usb_alloc_urb(0, GFP_KERNEL);
Sean Young69165c22006-05-02 11:44:43 +0000573 if (!kit->irq)
574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 kit->udev = usb_get_dev(dev);
577 kit->intf = intf;
Sean Young69165c22006-05-02 11:44:43 +0000578 INIT_WORK(&kit->do_notify, do_notify, kit);
Sean Young238d0e72006-09-24 19:26:57 +0000579 INIT_WORK(&kit->do_resubmit, do_resubmit, kit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
Sean Young69165c22006-05-02 11:44:43 +0000581 maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 interfacekit_irq, kit, endpoint->bInterval);
583 kit->irq->transfer_dma = kit->data_dma;
584 kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
585
586 usb_set_intfdata(intf, kit);
587
Sean Young912b24c2006-07-10 09:56:25 +0000588 do {
589 bit = find_first_zero_bit(&device_no, sizeof(device_no));
590 value = test_and_set_bit(bit, &device_no);
591 } while(value);
592 kit->dev_no = bit;
593
594 kit->dev = device_create(phidget_class, &kit->udev->dev, 0,
595 "interfacekit%d", kit->dev_no);
596 if (IS_ERR(kit->dev)) {
597 rc = PTR_ERR(kit->dev);
598 kit->dev = NULL;
599 goto out;
600 }
601 dev_set_drvdata(kit->dev, kit);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
Sean Young69165c22006-05-02 11:44:43 +0000604 rc = -EIO;
605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
Sean Youngda308e82006-08-11 09:28:28 +0000608 for (i=0; i<ifkit->outputs; i++ ) {
609 rc = device_create_file(kit->dev, &dev_output_attrs[i]);
610 if (rc)
611 goto out2;
Sean Young69165c22006-05-02 11:44:43 +0000612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Sean Youngda308e82006-08-11 09:28:28 +0000614 for (i=0; i<ifkit->inputs; i++ ) {
615 rc = device_create_file(kit->dev, &dev_input_attrs[i]);
616 if (rc)
617 goto out3;
Sean Young69165c22006-05-02 11:44:43 +0000618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Sean Youngda308e82006-08-11 09:28:28 +0000620 for (i=0; i<ifkit->sensors; i++ ) {
621 rc = device_create_file(kit->dev, &dev_sensor_attrs[i]);
622 if (rc)
623 goto out4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Sean Youngda308e82006-08-11 09:28:28 +0000626 if (ifkit->has_lcd) {
627 rc = device_create_file(kit->dev, &dev_attr_lcd);
628 if (rc)
629 goto out4;
630
631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
634 ifkit->sensors, ifkit->inputs, ifkit->outputs);
635
636 return 0;
Sean Young69165c22006-05-02 11:44:43 +0000637
Sean Youngda308e82006-08-11 09:28:28 +0000638out4:
639 while (i-- > 0)
640 device_remove_file(kit->dev, &dev_sensor_attrs[i]);
641
642 i = ifkit->inputs;
643out3:
644 while (i-- > 0)
645 device_remove_file(kit->dev, &dev_input_attrs[i]);
646
647 i = ifkit->outputs;
648out2:
649 while (i-- > 0)
650 device_remove_file(kit->dev, &dev_output_attrs[i]);
Sean Young69165c22006-05-02 11:44:43 +0000651out:
652 if (kit) {
653 if (kit->irq)
654 usb_free_urb(kit->irq);
655 if (kit->data)
656 usb_buffer_free(dev, URB_INT_SIZE, kit->data, kit->data_dma);
Sean Young912b24c2006-07-10 09:56:25 +0000657 if (kit->dev)
658 device_unregister(kit->dev);
659 if (kit->dev_no >= 0)
660 clear_bit(kit->dev_no, &device_no);
661
Sean Young69165c22006-05-02 11:44:43 +0000662 kfree(kit);
663 }
664
665 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668static void interfacekit_disconnect(struct usb_interface *interface)
669{
Sean Young69165c22006-05-02 11:44:43 +0000670 struct interfacekit *kit;
Sean Youngda308e82006-08-11 09:28:28 +0000671 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 kit = usb_get_intfdata(interface);
674 usb_set_intfdata(interface, NULL);
675 if (!kit)
676 return;
677
Sean Young69165c22006-05-02 11:44:43 +0000678 usb_kill_urb(kit->irq);
679 usb_free_urb(kit->irq);
680 usb_buffer_free(kit->udev, URB_INT_SIZE, kit->data, kit->data_dma);
681
682 cancel_delayed_work(&kit->do_notify);
Sean Young238d0e72006-09-24 19:26:57 +0000683 cancel_delayed_work(&kit->do_resubmit);
Sean Young69165c22006-05-02 11:44:43 +0000684
Sean Youngda308e82006-08-11 09:28:28 +0000685 for (i=0; i<kit->ifkit->outputs; i++)
686 device_remove_file(kit->dev, &dev_output_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Sean Youngda308e82006-08-11 09:28:28 +0000688 for (i=0; i<kit->ifkit->inputs; i++)
689 device_remove_file(kit->dev, &dev_input_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Sean Youngda308e82006-08-11 09:28:28 +0000691 for (i=0; i<kit->ifkit->sensors; i++)
692 device_remove_file(kit->dev, &dev_sensor_attrs[i]);
Sean Young69165c22006-05-02 11:44:43 +0000693
Sean Youngda308e82006-08-11 09:28:28 +0000694 if (kit->ifkit->has_lcd) {
Sean Young912b24c2006-07-10 09:56:25 +0000695 device_remove_file(kit->dev, &dev_attr_lcd);
Sean Youngda308e82006-08-11 09:28:28 +0000696 remove_lcd_files(kit);
697 }
Sean Young912b24c2006-07-10 09:56:25 +0000698
699 device_unregister(kit->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
702 kit->ifkit->sensors, kit->ifkit->inputs, kit->ifkit->outputs);
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 usb_put_dev(kit->udev);
Sean Young912b24c2006-07-10 09:56:25 +0000705 clear_bit(kit->dev_no, &device_no);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 kfree(kit);
708}
709
710static struct usb_driver interfacekit_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 .name = "phidgetkit",
712 .probe = interfacekit_probe,
713 .disconnect = interfacekit_disconnect,
714 .id_table = id_table
715};
716
717static int __init interfacekit_init(void)
718{
719 int retval = 0;
720
721 retval = usb_register(&interfacekit_driver);
722 if (retval)
723 err("usb_register failed. Error number %d", retval);
724
725 return retval;
726}
727
728static void __exit interfacekit_exit(void)
729{
730 usb_deregister(&interfacekit_driver);
731}
732
733module_init(interfacekit_init);
734module_exit(interfacekit_exit);
735
736MODULE_AUTHOR(DRIVER_AUTHOR);
737MODULE_DESCRIPTION(DRIVER_DESC);
738MODULE_LICENSE("GPL");