2 * USB PhidgetInterfaceKit driver 1.0
4 * Copyright (C) 2004 Sean Young <sean@mess.org>
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This is a driver for the USB PhidgetInterfaceKit.
15 #include <linux/config.h>
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>
23 #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
24 #define DRIVER_DESC "USB PhidgetInterfaceKit Driver"
26 #define USB_VENDOR_ID_GLAB 0x06c2
27 #define USB_DEVICE_ID_INTERFACEKIT004 0x0040
28 #define USB_DEVICE_ID_INTERFACEKIT888 0x0045
29 #define USB_DEVICE_ID_INTERFACEKIT047 0x0051
30 #define USB_DEVICE_ID_INTERFACEKIT088 0x0053
32 #define USB_VENDOR_ID_WISEGROUP 0x0925
33 #define USB_DEVICE_ID_INTERFACEKIT884 0x8201
35 #define MAX_INTERFACES 8
37 struct driver_interfacekit
{
43 #define ifkit(_sensors, _inputs, _outputs, _lcd) \
44 static struct driver_interfacekit ph_##_sensors##_inputs##_outputs = { \
45 .sensors = _sensors, \
47 .outputs = _outputs, \
56 struct phidget_interfacekit
{
57 struct usb_device
*udev
;
58 struct usb_interface
*intf
;
59 struct driver_interfacekit
*ifkit
;
60 int outputs
[MAX_INTERFACES
];
61 int inputs
[MAX_INTERFACES
];
62 int sensors
[MAX_INTERFACES
];
70 static struct usb_device_id id_table
[] = {
71 {USB_DEVICE(USB_VENDOR_ID_GLAB
, USB_DEVICE_ID_INTERFACEKIT004
),
72 .driver_info
= (kernel_ulong_t
)&ph_004
},
73 {USB_DEVICE(USB_VENDOR_ID_GLAB
, USB_DEVICE_ID_INTERFACEKIT888
),
74 .driver_info
= (kernel_ulong_t
)&ph_888
},
75 {USB_DEVICE(USB_VENDOR_ID_GLAB
, USB_DEVICE_ID_INTERFACEKIT047
),
76 .driver_info
= (kernel_ulong_t
)&ph_047
},
77 {USB_DEVICE(USB_VENDOR_ID_GLAB
, USB_DEVICE_ID_INTERFACEKIT088
),
78 .driver_info
= (kernel_ulong_t
)&ph_088
},
79 {USB_DEVICE(USB_VENDOR_ID_WISEGROUP
, USB_DEVICE_ID_INTERFACEKIT884
),
80 .driver_info
= (kernel_ulong_t
)&ph_884
},
83 MODULE_DEVICE_TABLE(usb
, id_table
);
85 static int change_outputs(struct phidget_interfacekit
*kit
, int output_num
, int enable
)
87 unsigned char *buffer
;
91 buffer
= kzalloc(4, GFP_KERNEL
);
93 dev_err(&kit
->udev
->dev
, "%s - out of memory\n",
98 kit
->outputs
[output_num
] = enable
;
100 if (kit
->outputs
[n
]) {
105 dev_dbg(&kit
->udev
->dev
, "sending data: %02x\n", buffer
[0]);
107 retval
= usb_control_msg(kit
->udev
,
108 usb_sndctrlpipe(kit
->udev
, 0),
109 0x09, 0x21, 0x0200, 0x0000, buffer
, 4, 2000);
112 dev_err(&kit
->udev
->dev
, "usb_control_msg returned %d\n",
116 return retval
< 0 ? retval
: 0;
119 static int change_string(struct phidget_interfacekit
*kit
, const char *display
, unsigned char row
)
121 unsigned char *buffer
;
122 unsigned char *form_buffer
;
123 int retval
= -ENOMEM
;
124 int i
,j
, len
, buf_ptr
;
126 buffer
= kmalloc(8, GFP_KERNEL
);
127 form_buffer
= kmalloc(30, GFP_KERNEL
);
128 if ((!buffer
) || (!form_buffer
)) {
129 dev_err(&kit
->udev
->dev
, "%s - out of memory\n", __FUNCTION__
);
133 len
= strlen(display
);
137 dev_dbg(&kit
->udev
->dev
, "Setting LCD line %d to %s\n", row
, display
);
139 form_buffer
[0] = row
* 0x40 + 0x80;
140 form_buffer
[1] = 0x02;
142 for (i
= 0; i
<len
; i
++)
143 form_buffer
[buf_ptr
++] = display
[i
];
145 for (i
= 0; i
< (20 - len
); i
++)
146 form_buffer
[buf_ptr
++] = 0x20;
147 form_buffer
[buf_ptr
++] = 0x01;
148 form_buffer
[buf_ptr
++] = row
* 0x40 + 0x80 + strlen(display
);
150 for (i
= 0; i
< buf_ptr
; i
+= 7) {
151 if ((buf_ptr
- i
) > 7)
155 for (j
= 0; j
< len
; j
++)
156 buffer
[j
] = form_buffer
[i
+ j
];
159 retval
= usb_control_msg(kit
->udev
,
160 usb_sndctrlpipe(kit
->udev
, 0),
161 0x09, 0x21, 0x0200, 0x0000, buffer
, 8, 2000);
174 #define set_lcd_line(number) \
175 static ssize_t lcd_line_##number(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
177 struct usb_interface *intf = to_usb_interface(dev); \
178 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
179 change_string(kit, buf, number - 1); \
182 static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
186 static ssize_t
set_backlight(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
188 struct usb_interface
*intf
= to_usb_interface(dev
);
189 struct phidget_interfacekit
*kit
= usb_get_intfdata(intf
);
191 unsigned char *buffer
;
192 int retval
= -ENOMEM
;
194 buffer
= kzalloc(8, GFP_KERNEL
);
196 dev_err(&kit
->udev
->dev
, "%s - out of memory\n", __FUNCTION__
);
200 if (sscanf(buf
, "%d", &enabled
) < 1) {
208 dev_dbg(&kit
->udev
->dev
, "Setting backlight to %s\n", enabled
? "on" : "off");
210 retval
= usb_control_msg(kit
->udev
,
211 usb_sndctrlpipe(kit
->udev
, 0),
212 0x09, 0x21, 0x0200, 0x0000, buffer
, 8, 2000);
221 static DEVICE_ATTR(backlight
, S_IWUGO
, NULL
, set_backlight
);
223 static void remove_lcd_files(struct phidget_interfacekit
*kit
)
225 if (kit
->lcd_files_on
) {
226 dev_dbg(&kit
->udev
->dev
, "Removing lcd files\n");
227 device_remove_file(&kit
->intf
->dev
, &dev_attr_lcd_line_1
);
228 device_remove_file(&kit
->intf
->dev
, &dev_attr_lcd_line_2
);
229 device_remove_file(&kit
->intf
->dev
, &dev_attr_backlight
);
233 static ssize_t
enable_lcd_files(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
235 struct usb_interface
*intf
= to_usb_interface(dev
);
236 struct phidget_interfacekit
*kit
= usb_get_intfdata(intf
);
239 if (kit
->ifkit
->has_lcd
== 0)
242 if (sscanf(buf
, "%d", &enable
) < 1)
246 if (!kit
->lcd_files_on
) {
247 dev_dbg(&kit
->udev
->dev
, "Adding lcd files\n");
248 device_create_file(&kit
->intf
->dev
, &dev_attr_lcd_line_1
);
249 device_create_file(&kit
->intf
->dev
, &dev_attr_lcd_line_2
);
250 device_create_file(&kit
->intf
->dev
, &dev_attr_backlight
);
251 kit
->lcd_files_on
= 1;
254 if (kit
->lcd_files_on
) {
255 remove_lcd_files(kit
);
256 kit
->lcd_files_on
= 0;
262 static DEVICE_ATTR(lcd
, S_IWUGO
, NULL
, enable_lcd_files
);
264 static void interfacekit_irq(struct urb
*urb
, struct pt_regs
*regs
)
266 struct phidget_interfacekit
*kit
= urb
->context
;
267 unsigned char *buffer
= kit
->data
;
271 switch (urb
->status
) {
272 case 0: /* success */
274 case -ECONNRESET
: /* unlink */
278 /* -EPIPE: should clear the halt */
283 for (n
=0; n
<8; n
++) {
284 kit
->inputs
[n
] = buffer
[1] & (1 << n
) ? 1 : 0;
288 kit
->sensors
[4] = buffer
[2] + (buffer
[3] & 0x0f) * 256;
289 kit
->sensors
[5] = buffer
[4] + (buffer
[3] & 0xf0) * 16;
290 kit
->sensors
[6] = buffer
[5] + (buffer
[6] & 0x0f) * 256;
291 kit
->sensors
[7] = buffer
[7] + (buffer
[6] & 0xf0) * 16;
293 kit
->sensors
[0] = buffer
[2] + (buffer
[3] & 0x0f) * 256;
294 kit
->sensors
[1] = buffer
[4] + (buffer
[3] & 0xf0) * 16;
295 kit
->sensors
[2] = buffer
[5] + (buffer
[6] & 0x0f) * 256;
296 kit
->sensors
[3] = buffer
[7] + (buffer
[6] & 0xf0) * 16;
300 status
= usb_submit_urb(urb
, SLAB_ATOMIC
);
302 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
303 kit
->udev
->bus
->bus_name
,
304 kit
->udev
->devpath
, status
);
307 #define show_set_output(value) \
308 static ssize_t set_output##value(struct device *dev, struct device_attribute *attr, const char *buf, \
311 struct usb_interface *intf = to_usb_interface(dev); \
312 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
316 if (sscanf(buf, "%d", &enabled) < 1) { \
320 retval = change_outputs(kit, value - 1, enabled ? 1 : 0); \
322 return retval ? retval : count; \
325 static ssize_t show_output##value(struct device *dev, struct device_attribute *attr, char *buf) \
327 struct usb_interface *intf = to_usb_interface(dev); \
328 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
330 return sprintf(buf, "%d\n", kit->outputs[value - 1]); \
332 static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO, \
333 show_output##value, set_output##value);
341 show_set_output(8); /* should be MAX_INTERFACES - 1 */
343 #define show_input(value) \
344 static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf) \
346 struct usb_interface *intf = to_usb_interface(dev); \
347 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
349 return sprintf(buf, "%d\n", kit->inputs[value - 1]); \
351 static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
360 show_input(8); /* should be MAX_INTERFACES - 1 */
362 #define show_sensor(value) \
363 static ssize_t show_sensor##value(struct device *dev, struct device_attribute *attr, char *buf) \
365 struct usb_interface *intf = to_usb_interface(dev); \
366 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
368 return sprintf(buf, "%d\n", kit->sensors[value - 1]); \
370 static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
379 show_sensor(8); /* should be MAX_INTERFACES - 1 */
381 static int interfacekit_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
383 struct usb_device
*dev
= interface_to_usbdev(intf
);
384 struct usb_host_interface
*interface
;
385 struct usb_endpoint_descriptor
*endpoint
;
386 struct phidget_interfacekit
*kit
;
387 struct driver_interfacekit
*ifkit
;
390 ifkit
= (struct driver_interfacekit
*)id
->driver_info
;
394 interface
= intf
->cur_altsetting
;
395 if (interface
->desc
.bNumEndpoints
!= 1)
398 endpoint
= &interface
->endpoint
[0].desc
;
399 if (!(endpoint
->bEndpointAddress
& 0x80))
404 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
405 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
407 kit
= kzalloc(sizeof(*kit
), GFP_KERNEL
);
409 dev_err(&intf
->dev
, "%s - out of memory\n", __FUNCTION__
);
414 kit
->data
= usb_buffer_alloc(dev
, 8, SLAB_ATOMIC
, &kit
->data_dma
);
420 kit
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
422 usb_buffer_free(dev
, 8, kit
->data
, kit
->data_dma
);
427 kit
->udev
= usb_get_dev(dev
);
429 usb_fill_int_urb(kit
->irq
, kit
->udev
, pipe
, kit
->data
,
430 (maxp
> 8 ? 8 : maxp
),
431 interfacekit_irq
, kit
, endpoint
->bInterval
);
432 kit
->irq
->transfer_dma
= kit
->data_dma
;
433 kit
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
435 usb_set_intfdata(intf
, kit
);
437 if (usb_submit_urb(kit
->irq
, GFP_KERNEL
)) {
441 if (ifkit
->outputs
>= 4) {
442 device_create_file(&intf
->dev
, &dev_attr_output1
);
443 device_create_file(&intf
->dev
, &dev_attr_output2
);
444 device_create_file(&intf
->dev
, &dev_attr_output3
);
445 device_create_file(&intf
->dev
, &dev_attr_output4
);
447 if (ifkit
->outputs
== 8) {
448 device_create_file(&intf
->dev
, &dev_attr_output5
);
449 device_create_file(&intf
->dev
, &dev_attr_output6
);
450 device_create_file(&intf
->dev
, &dev_attr_output7
);
451 device_create_file(&intf
->dev
, &dev_attr_output8
);
454 if (ifkit
->inputs
>= 4) {
455 device_create_file(&intf
->dev
, &dev_attr_input1
);
456 device_create_file(&intf
->dev
, &dev_attr_input2
);
457 device_create_file(&intf
->dev
, &dev_attr_input3
);
458 device_create_file(&intf
->dev
, &dev_attr_input4
);
460 if (ifkit
->inputs
== 8) {
461 device_create_file(&intf
->dev
, &dev_attr_input5
);
462 device_create_file(&intf
->dev
, &dev_attr_input6
);
463 device_create_file(&intf
->dev
, &dev_attr_input7
);
464 device_create_file(&intf
->dev
, &dev_attr_input8
);
467 if (ifkit
->sensors
>= 4) {
468 device_create_file(&intf
->dev
, &dev_attr_sensor1
);
469 device_create_file(&intf
->dev
, &dev_attr_sensor2
);
470 device_create_file(&intf
->dev
, &dev_attr_sensor3
);
471 device_create_file(&intf
->dev
, &dev_attr_sensor4
);
473 if (ifkit
->sensors
>= 7) {
474 device_create_file(&intf
->dev
, &dev_attr_sensor5
);
475 device_create_file(&intf
->dev
, &dev_attr_sensor6
);
476 device_create_file(&intf
->dev
, &dev_attr_sensor7
);
478 if (ifkit
->sensors
== 8) {
479 device_create_file(&intf
->dev
, &dev_attr_sensor8
);
483 device_create_file(&intf
->dev
, &dev_attr_lcd
);
485 dev_info(&intf
->dev
, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
486 ifkit
->sensors
, ifkit
->inputs
, ifkit
->outputs
);
491 static void interfacekit_disconnect(struct usb_interface
*interface
)
493 struct phidget_interfacekit
*kit
;
495 kit
= usb_get_intfdata(interface
);
496 usb_set_intfdata(interface
, NULL
);
500 if (kit
->ifkit
->outputs
>= 4) {
501 device_remove_file(&interface
->dev
, &dev_attr_output1
);
502 device_remove_file(&interface
->dev
, &dev_attr_output2
);
503 device_remove_file(&interface
->dev
, &dev_attr_output3
);
504 device_remove_file(&interface
->dev
, &dev_attr_output4
);
506 if (kit
->ifkit
->outputs
== 8) {
507 device_remove_file(&interface
->dev
, &dev_attr_output5
);
508 device_remove_file(&interface
->dev
, &dev_attr_output6
);
509 device_remove_file(&interface
->dev
, &dev_attr_output7
);
510 device_remove_file(&interface
->dev
, &dev_attr_output8
);
513 if (kit
->ifkit
->inputs
>= 4) {
514 device_remove_file(&interface
->dev
, &dev_attr_input1
);
515 device_remove_file(&interface
->dev
, &dev_attr_input2
);
516 device_remove_file(&interface
->dev
, &dev_attr_input3
);
517 device_remove_file(&interface
->dev
, &dev_attr_input4
);
519 if (kit
->ifkit
->inputs
== 8) {
520 device_remove_file(&interface
->dev
, &dev_attr_input5
);
521 device_remove_file(&interface
->dev
, &dev_attr_input6
);
522 device_remove_file(&interface
->dev
, &dev_attr_input7
);
523 device_remove_file(&interface
->dev
, &dev_attr_input8
);
526 if (kit
->ifkit
->sensors
>= 4) {
527 device_remove_file(&interface
->dev
, &dev_attr_sensor1
);
528 device_remove_file(&interface
->dev
, &dev_attr_sensor2
);
529 device_remove_file(&interface
->dev
, &dev_attr_sensor3
);
530 device_remove_file(&interface
->dev
, &dev_attr_sensor4
);
532 if (kit
->ifkit
->sensors
>= 7) {
533 device_remove_file(&interface
->dev
, &dev_attr_sensor5
);
534 device_remove_file(&interface
->dev
, &dev_attr_sensor6
);
535 device_remove_file(&interface
->dev
, &dev_attr_sensor7
);
537 if (kit
->ifkit
->sensors
== 8) {
538 device_remove_file(&interface
->dev
, &dev_attr_sensor8
);
540 if (kit
->ifkit
->has_lcd
)
541 device_remove_file(&interface
->dev
, &dev_attr_lcd
);
543 dev_info(&interface
->dev
, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
544 kit
->ifkit
->sensors
, kit
->ifkit
->inputs
, kit
->ifkit
->outputs
);
546 usb_kill_urb(kit
->irq
);
547 usb_free_urb(kit
->irq
);
548 usb_buffer_free(kit
->udev
, 8, kit
->data
, kit
->data_dma
);
550 usb_put_dev(kit
->udev
);
554 static struct usb_driver interfacekit_driver
= {
555 .name
= "phidgetkit",
556 .probe
= interfacekit_probe
,
557 .disconnect
= interfacekit_disconnect
,
561 static int __init
interfacekit_init(void)
565 retval
= usb_register(&interfacekit_driver
);
567 err("usb_register failed. Error number %d", retval
);
572 static void __exit
interfacekit_exit(void)
574 usb_deregister(&interfacekit_driver
);
577 module_init(interfacekit_init
);
578 module_exit(interfacekit_exit
);
580 MODULE_AUTHOR(DRIVER_AUTHOR
);
581 MODULE_DESCRIPTION(DRIVER_DESC
);
582 MODULE_LICENSE("GPL");