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
= kmalloc(4, GFP_KERNEL
);
93 dev_err(&kit
->udev
->dev
, "%s - out of memory\n",
98 kit
->outputs
[output_num
] = enable
;
100 for (n
=0; n
<8; n
++) {
101 if (kit
->outputs
[n
]) {
106 dev_dbg(&kit
->udev
->dev
, "sending data: %02x\n", buffer
[0]);
108 retval
= usb_control_msg(kit
->udev
,
109 usb_sndctrlpipe(kit
->udev
, 0),
110 0x09, 0x21, 0x0200, 0x0000, buffer
, 4, 2000);
113 dev_err(&kit
->udev
->dev
, "usb_control_msg returned %d\n",
117 return retval
< 0 ? retval
: 0;
120 static int change_string(struct phidget_interfacekit
*kit
, const char *display
, unsigned char row
)
122 unsigned char *buffer
;
123 unsigned char *form_buffer
;
124 int retval
= -ENOMEM
;
125 int i
,j
, len
, buf_ptr
;
127 buffer
= kmalloc(8, GFP_KERNEL
);
128 form_buffer
= kmalloc(30, GFP_KERNEL
);
129 if ((!buffer
) || (!form_buffer
)) {
130 dev_err(&kit
->udev
->dev
, "%s - out of memory\n", __FUNCTION__
);
134 len
= strlen(display
);
138 dev_dbg(&kit
->udev
->dev
, "Setting LCD line %d to %s\n", row
, display
);
140 form_buffer
[0] = row
* 0x40 + 0x80;
141 form_buffer
[1] = 0x02;
143 for (i
= 0; i
<len
; i
++)
144 form_buffer
[buf_ptr
++] = display
[i
];
146 for (i
= 0; i
< (20 - len
); i
++)
147 form_buffer
[buf_ptr
++] = 0x20;
148 form_buffer
[buf_ptr
++] = 0x01;
149 form_buffer
[buf_ptr
++] = row
* 0x40 + 0x80 + strlen(display
);
151 for (i
= 0; i
< buf_ptr
; i
+= 7) {
152 if ((buf_ptr
- i
) > 7)
156 for (j
= 0; j
< len
; j
++)
157 buffer
[j
] = form_buffer
[i
+ j
];
160 retval
= usb_control_msg(kit
->udev
,
161 usb_sndctrlpipe(kit
->udev
, 0),
162 0x09, 0x21, 0x0200, 0x0000, buffer
, 8, 2000);
175 #define set_lcd_line(number) \
176 static ssize_t lcd_line_##number(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
178 struct usb_interface *intf = to_usb_interface(dev); \
179 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
180 change_string(kit, buf, number - 1); \
183 static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
187 static ssize_t
set_backlight(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
189 struct usb_interface
*intf
= to_usb_interface(dev
);
190 struct phidget_interfacekit
*kit
= usb_get_intfdata(intf
);
192 unsigned char *buffer
;
193 int retval
= -ENOMEM
;
195 buffer
= kmalloc(8, GFP_KERNEL
);
197 dev_err(&kit
->udev
->dev
, "%s - out of memory\n", __FUNCTION__
);
201 if (sscanf(buf
, "%d", &enabled
) < 1) {
205 memset(buffer
, 0x00, 8);
210 dev_dbg(&kit
->udev
->dev
, "Setting backlight to %s\n", enabled
? "on" : "off");
212 retval
= usb_control_msg(kit
->udev
,
213 usb_sndctrlpipe(kit
->udev
, 0),
214 0x09, 0x21, 0x0200, 0x0000, buffer
, 8, 2000);
223 static DEVICE_ATTR(backlight
, S_IWUGO
, NULL
, set_backlight
);
225 static void remove_lcd_files(struct phidget_interfacekit
*kit
)
227 if (kit
->lcd_files_on
) {
228 dev_dbg(&kit
->udev
->dev
, "Removing lcd files\n");
229 device_remove_file(&kit
->intf
->dev
, &dev_attr_lcd_line_1
);
230 device_remove_file(&kit
->intf
->dev
, &dev_attr_lcd_line_2
);
231 device_remove_file(&kit
->intf
->dev
, &dev_attr_backlight
);
235 static ssize_t
enable_lcd_files(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
237 struct usb_interface
*intf
= to_usb_interface(dev
);
238 struct phidget_interfacekit
*kit
= usb_get_intfdata(intf
);
241 if (kit
->ifkit
->has_lcd
== 0)
244 if (sscanf(buf
, "%d", &enable
) < 1)
248 if (!kit
->lcd_files_on
) {
249 dev_dbg(&kit
->udev
->dev
, "Adding lcd files\n");
250 device_create_file(&kit
->intf
->dev
, &dev_attr_lcd_line_1
);
251 device_create_file(&kit
->intf
->dev
, &dev_attr_lcd_line_2
);
252 device_create_file(&kit
->intf
->dev
, &dev_attr_backlight
);
253 kit
->lcd_files_on
= 1;
256 if (kit
->lcd_files_on
) {
257 remove_lcd_files(kit
);
258 kit
->lcd_files_on
= 0;
264 static DEVICE_ATTR(lcd
, S_IWUGO
, NULL
, enable_lcd_files
);
266 static void interfacekit_irq(struct urb
*urb
, struct pt_regs
*regs
)
268 struct phidget_interfacekit
*kit
= urb
->context
;
269 unsigned char *buffer
= kit
->data
;
273 switch (urb
->status
) {
274 case 0: /* success */
276 case -ECONNRESET
: /* unlink */
280 /* -EPIPE: should clear the halt */
285 for (n
=0; n
<8; n
++) {
286 kit
->inputs
[n
] = buffer
[1] & (1 << n
) ? 1 : 0;
290 kit
->sensors
[4] = buffer
[2] + (buffer
[3] & 0x0f) * 256;
291 kit
->sensors
[5] = buffer
[4] + (buffer
[3] & 0xf0) * 16;
292 kit
->sensors
[6] = buffer
[5] + (buffer
[6] & 0x0f) * 256;
293 kit
->sensors
[7] = buffer
[7] + (buffer
[6] & 0xf0) * 16;
295 kit
->sensors
[0] = buffer
[2] + (buffer
[3] & 0x0f) * 256;
296 kit
->sensors
[1] = buffer
[4] + (buffer
[3] & 0xf0) * 16;
297 kit
->sensors
[2] = buffer
[5] + (buffer
[6] & 0x0f) * 256;
298 kit
->sensors
[3] = buffer
[7] + (buffer
[6] & 0xf0) * 16;
302 status
= usb_submit_urb(urb
, SLAB_ATOMIC
);
304 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
305 kit
->udev
->bus
->bus_name
,
306 kit
->udev
->devpath
, status
);
309 #define show_set_output(value) \
310 static ssize_t set_output##value(struct device *dev, struct device_attribute *attr, const char *buf, \
313 struct usb_interface *intf = to_usb_interface(dev); \
314 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
318 if (sscanf(buf, "%d", &enabled) < 1) { \
322 retval = change_outputs(kit, value - 1, enabled ? 1 : 0); \
324 return retval ? retval : count; \
327 static ssize_t show_output##value(struct device *dev, struct device_attribute *attr, char *buf) \
329 struct usb_interface *intf = to_usb_interface(dev); \
330 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
332 return sprintf(buf, "%d\n", kit->outputs[value - 1]); \
334 static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO, \
335 show_output##value, set_output##value);
343 show_set_output(8); /* should be MAX_INTERFACES - 1 */
345 #define show_input(value) \
346 static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf) \
348 struct usb_interface *intf = to_usb_interface(dev); \
349 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
351 return sprintf(buf, "%d\n", kit->inputs[value - 1]); \
353 static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
362 show_input(8); /* should be MAX_INTERFACES - 1 */
364 #define show_sensor(value) \
365 static ssize_t show_sensor##value(struct device *dev, struct device_attribute *attr, char *buf) \
367 struct usb_interface *intf = to_usb_interface(dev); \
368 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
370 return sprintf(buf, "%d\n", kit->sensors[value - 1]); \
372 static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
381 show_sensor(8); /* should be MAX_INTERFACES - 1 */
383 static int interfacekit_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
385 struct usb_device
*dev
= interface_to_usbdev(intf
);
386 struct usb_host_interface
*interface
;
387 struct usb_endpoint_descriptor
*endpoint
;
388 struct phidget_interfacekit
*kit
;
389 struct driver_interfacekit
*ifkit
;
392 ifkit
= (struct driver_interfacekit
*)id
->driver_info
;
396 interface
= intf
->cur_altsetting
;
397 if (interface
->desc
.bNumEndpoints
!= 1)
400 endpoint
= &interface
->endpoint
[0].desc
;
401 if (!(endpoint
->bEndpointAddress
& 0x80))
406 pipe
= usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
);
407 maxp
= usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
));
409 kit
= kmalloc(sizeof(*kit
), GFP_KERNEL
);
411 dev_err(&intf
->dev
, "%s - out of memory\n", __FUNCTION__
);
414 memset(kit
, 0, sizeof(*kit
));
417 kit
->data
= usb_buffer_alloc(dev
, 8, SLAB_ATOMIC
, &kit
->data_dma
);
423 kit
->irq
= usb_alloc_urb(0, GFP_KERNEL
);
425 usb_buffer_free(dev
, 8, kit
->data
, kit
->data_dma
);
430 kit
->udev
= usb_get_dev(dev
);
432 usb_fill_int_urb(kit
->irq
, kit
->udev
, pipe
, kit
->data
,
433 (maxp
> 8 ? 8 : maxp
),
434 interfacekit_irq
, kit
, endpoint
->bInterval
);
435 kit
->irq
->transfer_dma
= kit
->data_dma
;
436 kit
->irq
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
438 usb_set_intfdata(intf
, kit
);
440 if (usb_submit_urb(kit
->irq
, GFP_KERNEL
)) {
444 if (ifkit
->outputs
>= 4) {
445 device_create_file(&intf
->dev
, &dev_attr_output1
);
446 device_create_file(&intf
->dev
, &dev_attr_output2
);
447 device_create_file(&intf
->dev
, &dev_attr_output3
);
448 device_create_file(&intf
->dev
, &dev_attr_output4
);
450 if (ifkit
->outputs
== 8) {
451 device_create_file(&intf
->dev
, &dev_attr_output5
);
452 device_create_file(&intf
->dev
, &dev_attr_output6
);
453 device_create_file(&intf
->dev
, &dev_attr_output7
);
454 device_create_file(&intf
->dev
, &dev_attr_output8
);
457 if (ifkit
->inputs
>= 4) {
458 device_create_file(&intf
->dev
, &dev_attr_input1
);
459 device_create_file(&intf
->dev
, &dev_attr_input2
);
460 device_create_file(&intf
->dev
, &dev_attr_input3
);
461 device_create_file(&intf
->dev
, &dev_attr_input4
);
463 if (ifkit
->inputs
== 8) {
464 device_create_file(&intf
->dev
, &dev_attr_input5
);
465 device_create_file(&intf
->dev
, &dev_attr_input6
);
466 device_create_file(&intf
->dev
, &dev_attr_input7
);
467 device_create_file(&intf
->dev
, &dev_attr_input8
);
470 if (ifkit
->sensors
>= 4) {
471 device_create_file(&intf
->dev
, &dev_attr_sensor1
);
472 device_create_file(&intf
->dev
, &dev_attr_sensor2
);
473 device_create_file(&intf
->dev
, &dev_attr_sensor3
);
474 device_create_file(&intf
->dev
, &dev_attr_sensor4
);
476 if (ifkit
->sensors
>= 7) {
477 device_create_file(&intf
->dev
, &dev_attr_sensor5
);
478 device_create_file(&intf
->dev
, &dev_attr_sensor6
);
479 device_create_file(&intf
->dev
, &dev_attr_sensor7
);
481 if (ifkit
->sensors
== 8) {
482 device_create_file(&intf
->dev
, &dev_attr_sensor8
);
486 device_create_file(&intf
->dev
, &dev_attr_lcd
);
488 dev_info(&intf
->dev
, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
489 ifkit
->sensors
, ifkit
->inputs
, ifkit
->outputs
);
494 static void interfacekit_disconnect(struct usb_interface
*interface
)
496 struct phidget_interfacekit
*kit
;
498 kit
= usb_get_intfdata(interface
);
499 usb_set_intfdata(interface
, NULL
);
503 if (kit
->ifkit
->outputs
>= 4) {
504 device_remove_file(&interface
->dev
, &dev_attr_output1
);
505 device_remove_file(&interface
->dev
, &dev_attr_output2
);
506 device_remove_file(&interface
->dev
, &dev_attr_output3
);
507 device_remove_file(&interface
->dev
, &dev_attr_output4
);
509 if (kit
->ifkit
->outputs
== 8) {
510 device_remove_file(&interface
->dev
, &dev_attr_output5
);
511 device_remove_file(&interface
->dev
, &dev_attr_output6
);
512 device_remove_file(&interface
->dev
, &dev_attr_output7
);
513 device_remove_file(&interface
->dev
, &dev_attr_output8
);
516 if (kit
->ifkit
->inputs
>= 4) {
517 device_remove_file(&interface
->dev
, &dev_attr_input1
);
518 device_remove_file(&interface
->dev
, &dev_attr_input2
);
519 device_remove_file(&interface
->dev
, &dev_attr_input3
);
520 device_remove_file(&interface
->dev
, &dev_attr_input4
);
522 if (kit
->ifkit
->inputs
== 8) {
523 device_remove_file(&interface
->dev
, &dev_attr_input5
);
524 device_remove_file(&interface
->dev
, &dev_attr_input6
);
525 device_remove_file(&interface
->dev
, &dev_attr_input7
);
526 device_remove_file(&interface
->dev
, &dev_attr_input8
);
529 if (kit
->ifkit
->sensors
>= 4) {
530 device_remove_file(&interface
->dev
, &dev_attr_sensor1
);
531 device_remove_file(&interface
->dev
, &dev_attr_sensor2
);
532 device_remove_file(&interface
->dev
, &dev_attr_sensor3
);
533 device_remove_file(&interface
->dev
, &dev_attr_sensor4
);
535 if (kit
->ifkit
->sensors
>= 7) {
536 device_remove_file(&interface
->dev
, &dev_attr_sensor5
);
537 device_remove_file(&interface
->dev
, &dev_attr_sensor6
);
538 device_remove_file(&interface
->dev
, &dev_attr_sensor7
);
540 if (kit
->ifkit
->sensors
== 8) {
541 device_remove_file(&interface
->dev
, &dev_attr_sensor8
);
543 if (kit
->ifkit
->has_lcd
)
544 device_remove_file(&interface
->dev
, &dev_attr_lcd
);
546 dev_info(&interface
->dev
, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
547 kit
->ifkit
->sensors
, kit
->ifkit
->inputs
, kit
->ifkit
->outputs
);
549 usb_kill_urb(kit
->irq
);
550 usb_free_urb(kit
->irq
);
551 usb_buffer_free(kit
->udev
, 8, kit
->data
, kit
->data_dma
);
553 usb_put_dev(kit
->udev
);
557 static struct usb_driver interfacekit_driver
= {
558 .name
= "phidgetkit",
559 .probe
= interfacekit_probe
,
560 .disconnect
= interfacekit_disconnect
,
564 static int __init
interfacekit_init(void)
568 retval
= usb_register(&interfacekit_driver
);
570 err("usb_register failed. Error number %d", retval
);
575 static void __exit
interfacekit_exit(void)
577 usb_deregister(&interfacekit_driver
);
580 module_init(interfacekit_init
);
581 module_exit(interfacekit_exit
);
583 MODULE_AUTHOR(DRIVER_AUTHOR
);
584 MODULE_DESCRIPTION(DRIVER_DESC
);
585 MODULE_LICENSE("GPL");