2 * USB PhidgetInterfaceKit driver 1.0
4 * Copyright (C) 2004, 2006 Sean Young <sean@mess.org>
5 * Copyright (C) 2005 Daniel Saakes <daniel@saakes.net>
6 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
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.
13 * This is a driver for the USB PhidgetInterfaceKit.
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/usb.h>
24 #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
25 #define DRIVER_DESC "USB PhidgetInterfaceKit Driver"
27 #define USB_VENDOR_ID_GLAB 0x06c2
28 #define USB_DEVICE_ID_INTERFACEKIT004 0x0040
29 #define USB_DEVICE_ID_INTERFACEKIT01616 0x0044
30 #define USB_DEVICE_ID_INTERFACEKIT888 0x0045
31 #define USB_DEVICE_ID_INTERFACEKIT047 0x0051
32 #define USB_DEVICE_ID_INTERFACEKIT088 0x0053
34 #define USB_VENDOR_ID_WISEGROUP 0x0925
35 #define USB_DEVICE_ID_INTERFACEKIT884 0x8201
37 #define MAX_INTERFACES 16
39 #define URB_INT_SIZE 8
41 struct driver_interfacekit {
47 #define ifkit(_sensors, _inputs, _outputs, _lcd) \
48 static struct driver_interfacekit ph_##_sensors##_inputs##_outputs = { \
49 .sensors = _sensors, \
51 .outputs = _outputs, \
62 struct usb_device *udev;
63 struct usb_interface *intf;
64 struct driver_interfacekit *ifkit;
65 unsigned long outputs;
66 u8 inputs[MAX_INTERFACES];
67 u16 sensors[MAX_INTERFACES];
74 struct work_struct do_notify;
75 unsigned long input_events;
76 unsigned long sensor_events;
79 static struct usb_device_id id_table[] = {
80 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT004),
81 .driver_info = (kernel_ulong_t)&ph_004},
82 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT888),
83 .driver_info = (kernel_ulong_t)&ph_888},
84 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT047),
85 .driver_info = (kernel_ulong_t)&ph_047},
86 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT088),
87 .driver_info = (kernel_ulong_t)&ph_088},
88 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT01616),
89 .driver_info = (kernel_ulong_t)&ph_01616},
90 {USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_INTERFACEKIT884),
91 .driver_info = (kernel_ulong_t)&ph_884},
94 MODULE_DEVICE_TABLE(usb, id_table);
96 static int change_outputs(struct interfacekit *kit, int output_num, int enable)
102 set_bit(output_num, &kit->outputs);
104 clear_bit(output_num, &kit->outputs);
106 buffer = kzalloc(4, GFP_KERNEL);
108 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
111 buffer[0] = (u8)kit->outputs;
112 buffer[1] = (u8)(kit->outputs >> 8);
114 dev_dbg(&kit->udev->dev, "sending data: 0x%04x\n", (u16)kit->outputs);
116 retval = usb_control_msg(kit->udev,
117 usb_sndctrlpipe(kit->udev, 0),
118 0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2000);
121 dev_err(&kit->udev->dev, "usb_control_msg returned %d\n",
125 return retval < 0 ? retval : 0;
128 static int change_string(struct interfacekit *kit, const char *display, unsigned char row)
130 unsigned char *buffer;
131 unsigned char *form_buffer;
132 int retval = -ENOMEM;
133 int i,j, len, buf_ptr;
135 buffer = kmalloc(8, GFP_KERNEL);
136 form_buffer = kmalloc(30, GFP_KERNEL);
137 if ((!buffer) || (!form_buffer)) {
138 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
142 len = strlen(display);
146 dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
148 form_buffer[0] = row * 0x40 + 0x80;
149 form_buffer[1] = 0x02;
151 for (i = 0; i<len; i++)
152 form_buffer[buf_ptr++] = display[i];
154 for (i = 0; i < (20 - len); i++)
155 form_buffer[buf_ptr++] = 0x20;
156 form_buffer[buf_ptr++] = 0x01;
157 form_buffer[buf_ptr++] = row * 0x40 + 0x80 + strlen(display);
159 for (i = 0; i < buf_ptr; i += 7) {
160 if ((buf_ptr - i) > 7)
164 for (j = 0; j < len; j++)
165 buffer[j] = form_buffer[i + j];
168 retval = usb_control_msg(kit->udev,
169 usb_sndctrlpipe(kit->udev, 0),
170 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
183 #define set_lcd_line(number) \
184 static ssize_t lcd_line_##number(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
186 struct usb_interface *intf = to_usb_interface(dev); \
187 struct interfacekit *kit = usb_get_intfdata(intf); \
188 change_string(kit, buf, number - 1); \
191 static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
195 static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
197 struct usb_interface *intf = to_usb_interface(dev);
198 struct interfacekit *kit = usb_get_intfdata(intf);
200 unsigned char *buffer;
201 int retval = -ENOMEM;
203 buffer = kzalloc(8, GFP_KERNEL);
205 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
209 if (sscanf(buf, "%d", &enabled) < 1) {
217 dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
219 retval = usb_control_msg(kit->udev,
220 usb_sndctrlpipe(kit->udev, 0),
221 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
230 static DEVICE_ATTR(backlight, S_IWUGO, NULL, set_backlight);
232 static void remove_lcd_files(struct interfacekit *kit)
234 if (kit->lcd_files_on) {
235 dev_dbg(&kit->udev->dev, "Removing lcd files\n");
236 device_remove_file(&kit->intf->dev, &dev_attr_lcd_line_1);
237 device_remove_file(&kit->intf->dev, &dev_attr_lcd_line_2);
238 device_remove_file(&kit->intf->dev, &dev_attr_backlight);
242 static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
244 struct usb_interface *intf = to_usb_interface(dev);
245 struct interfacekit *kit = usb_get_intfdata(intf);
248 if (kit->ifkit->has_lcd == 0)
251 if (sscanf(buf, "%d", &enable) < 1)
255 if (!kit->lcd_files_on) {
256 dev_dbg(&kit->udev->dev, "Adding lcd files\n");
257 device_create_file(&kit->intf->dev, &dev_attr_lcd_line_1);
258 device_create_file(&kit->intf->dev, &dev_attr_lcd_line_2);
259 device_create_file(&kit->intf->dev, &dev_attr_backlight);
260 kit->lcd_files_on = 1;
263 if (kit->lcd_files_on) {
264 remove_lcd_files(kit);
265 kit->lcd_files_on = 0;
271 static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
273 static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
275 struct interfacekit *kit = urb->context;
276 unsigned char *buffer = kit->data;
277 int i, level, sensor;
280 switch (urb->status) {
281 case 0: /* success */
283 case -ECONNRESET: /* unlink */
287 /* -EPIPE: should clear the halt */
293 if (kit->ifkit->inputs == 16) {
294 for (i=0; i < 8; i++) {
295 level = (buffer[0] >> i) & 1;
296 if (kit->inputs[i] != level) {
297 kit->inputs[i] = level;
298 set_bit(i, &kit->input_events);
300 level = (buffer[1] >> i) & 1;
301 if (kit->inputs[8 + i] != level) {
302 kit->inputs[8 + i] = level;
303 set_bit(8 + i, &kit->input_events);
307 else if (kit->ifkit->inputs == 8) {
308 for (i=0; i < 8; i++) {
309 level = (buffer[1] >> i) & 1;
310 if (kit->inputs[i] != level) {
311 kit->inputs[i] = level;
312 set_bit(i, &kit->input_events);
318 if (kit->ifkit->sensors) {
319 sensor = (buffer[0] & 1) ? 4 : 0;
321 level = buffer[2] + (buffer[3] & 0x0f) * 256;
322 if (level != kit->sensors[sensor]) {
323 kit->sensors[sensor] = level;
324 set_bit(sensor, &kit->sensor_events);
327 level = buffer[4] + (buffer[3] & 0xf0) * 16;
328 if (level != kit->sensors[sensor]) {
329 kit->sensors[sensor] = level;
330 set_bit(sensor, &kit->sensor_events);
333 level = buffer[5] + (buffer[6] & 0x0f) * 256;
334 if (level != kit->sensors[sensor]) {
335 kit->sensors[sensor] = level;
336 set_bit(sensor, &kit->sensor_events);
339 level = buffer[7] + (buffer[6] & 0xf0) * 16;
340 if (level != kit->sensors[sensor]) {
341 kit->sensors[sensor] = level;
342 set_bit(sensor, &kit->sensor_events);
346 if (kit->input_events || kit->sensor_events)
347 schedule_work(&kit->do_notify);
350 status = usb_submit_urb(urb, SLAB_ATOMIC);
352 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
353 kit->udev->bus->bus_name,
354 kit->udev->devpath, status);
357 static void do_notify(void *data)
359 struct interfacekit *kit = data;
363 for (i=0; i<kit->ifkit->inputs; i++) {
364 if (test_and_clear_bit(i, &kit->input_events)) {
365 sprintf(sysfs_file, "input%d", i + 1);
366 sysfs_notify(&kit->intf->dev.kobj, NULL, sysfs_file);
370 for (i=0; i<kit->ifkit->sensors; i++) {
371 if (test_and_clear_bit(i, &kit->sensor_events)) {
372 sprintf(sysfs_file, "sensor%d", i + 1);
373 sysfs_notify(&kit->intf->dev.kobj, NULL, sysfs_file);
378 #define show_set_output(value) \
379 static ssize_t set_output##value(struct device *dev, struct device_attribute *attr, const char *buf, \
382 struct usb_interface *intf = to_usb_interface(dev); \
383 struct interfacekit *kit = usb_get_intfdata(intf); \
387 if (sscanf(buf, "%d", &enabled) < 1) \
390 retval = change_outputs(kit, value - 1, enabled); \
392 return retval ? retval : count; \
395 static ssize_t show_output##value(struct device *dev, struct device_attribute *attr, char *buf) \
397 struct usb_interface *intf = to_usb_interface(dev); \
398 struct interfacekit *kit = usb_get_intfdata(intf); \
400 return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
402 static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO, \
403 show_output##value, set_output##value);
421 #define show_input(value) \
422 static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf) \
424 struct usb_interface *intf = to_usb_interface(dev); \
425 struct interfacekit *kit = usb_get_intfdata(intf); \
427 return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
429 static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
448 #define show_sensor(value) \
449 static ssize_t show_sensor##value(struct device *dev, struct device_attribute *attr, char *buf) \
451 struct usb_interface *intf = to_usb_interface(dev); \
452 struct interfacekit *kit = usb_get_intfdata(intf); \
454 return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
456 static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
467 static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
469 struct usb_device *dev = interface_to_usbdev(intf);
470 struct usb_host_interface *interface;
471 struct usb_endpoint_descriptor *endpoint;
472 struct interfacekit *kit;
473 struct driver_interfacekit *ifkit;
474 int pipe, maxp, rc = -ENOMEM;
476 ifkit = (struct driver_interfacekit *)id->driver_info;
480 interface = intf->cur_altsetting;
481 if (interface->desc.bNumEndpoints != 1)
484 endpoint = &interface->endpoint[0].desc;
485 if (!(endpoint->bEndpointAddress & 0x80))
490 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
491 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
493 kit = kzalloc(sizeof(*kit), GFP_KERNEL);
498 kit->data = usb_buffer_alloc(dev, URB_INT_SIZE, SLAB_ATOMIC, &kit->data_dma);
502 kit->irq = usb_alloc_urb(0, GFP_KERNEL);
506 kit->udev = usb_get_dev(dev);
508 INIT_WORK(&kit->do_notify, do_notify, kit);
509 usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
510 maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
511 interfacekit_irq, kit, endpoint->bInterval);
512 kit->irq->transfer_dma = kit->data_dma;
513 kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
515 usb_set_intfdata(intf, kit);
517 if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
522 if (ifkit->outputs >= 4) {
523 device_create_file(&intf->dev, &dev_attr_output1);
524 device_create_file(&intf->dev, &dev_attr_output2);
525 device_create_file(&intf->dev, &dev_attr_output3);
526 device_create_file(&intf->dev, &dev_attr_output4);
528 if (ifkit->outputs >= 8) {
529 device_create_file(&intf->dev, &dev_attr_output5);
530 device_create_file(&intf->dev, &dev_attr_output6);
531 device_create_file(&intf->dev, &dev_attr_output7);
532 device_create_file(&intf->dev, &dev_attr_output8);
534 if (ifkit->outputs == 16) {
535 device_create_file(&intf->dev, &dev_attr_output9);
536 device_create_file(&intf->dev, &dev_attr_output10);
537 device_create_file(&intf->dev, &dev_attr_output11);
538 device_create_file(&intf->dev, &dev_attr_output12);
539 device_create_file(&intf->dev, &dev_attr_output13);
540 device_create_file(&intf->dev, &dev_attr_output14);
541 device_create_file(&intf->dev, &dev_attr_output15);
542 device_create_file(&intf->dev, &dev_attr_output16);
545 if (ifkit->inputs >= 4) {
546 device_create_file(&intf->dev, &dev_attr_input1);
547 device_create_file(&intf->dev, &dev_attr_input2);
548 device_create_file(&intf->dev, &dev_attr_input3);
549 device_create_file(&intf->dev, &dev_attr_input4);
551 if (ifkit->inputs >= 8) {
552 device_create_file(&intf->dev, &dev_attr_input5);
553 device_create_file(&intf->dev, &dev_attr_input6);
554 device_create_file(&intf->dev, &dev_attr_input7);
555 device_create_file(&intf->dev, &dev_attr_input8);
557 if (ifkit->inputs == 16) {
558 device_create_file(&intf->dev, &dev_attr_input9);
559 device_create_file(&intf->dev, &dev_attr_input10);
560 device_create_file(&intf->dev, &dev_attr_input11);
561 device_create_file(&intf->dev, &dev_attr_input12);
562 device_create_file(&intf->dev, &dev_attr_input13);
563 device_create_file(&intf->dev, &dev_attr_input14);
564 device_create_file(&intf->dev, &dev_attr_input15);
565 device_create_file(&intf->dev, &dev_attr_input16);
568 if (ifkit->sensors >= 4) {
569 device_create_file(&intf->dev, &dev_attr_sensor1);
570 device_create_file(&intf->dev, &dev_attr_sensor2);
571 device_create_file(&intf->dev, &dev_attr_sensor3);
572 device_create_file(&intf->dev, &dev_attr_sensor4);
574 if (ifkit->sensors >= 7) {
575 device_create_file(&intf->dev, &dev_attr_sensor5);
576 device_create_file(&intf->dev, &dev_attr_sensor6);
577 device_create_file(&intf->dev, &dev_attr_sensor7);
579 if (ifkit->sensors == 8)
580 device_create_file(&intf->dev, &dev_attr_sensor8);
583 device_create_file(&intf->dev, &dev_attr_lcd);
585 dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
586 ifkit->sensors, ifkit->inputs, ifkit->outputs);
593 usb_free_urb(kit->irq);
595 usb_buffer_free(dev, URB_INT_SIZE, kit->data, kit->data_dma);
602 static void interfacekit_disconnect(struct usb_interface *interface)
604 struct interfacekit *kit;
606 kit = usb_get_intfdata(interface);
607 usb_set_intfdata(interface, NULL);
611 usb_kill_urb(kit->irq);
612 usb_free_urb(kit->irq);
613 usb_buffer_free(kit->udev, URB_INT_SIZE, kit->data, kit->data_dma);
615 cancel_delayed_work(&kit->do_notify);
617 if (kit->ifkit->outputs >= 4) {
618 device_remove_file(&interface->dev, &dev_attr_output1);
619 device_remove_file(&interface->dev, &dev_attr_output2);
620 device_remove_file(&interface->dev, &dev_attr_output3);
621 device_remove_file(&interface->dev, &dev_attr_output4);
623 if (kit->ifkit->outputs >= 8) {
624 device_remove_file(&interface->dev, &dev_attr_output5);
625 device_remove_file(&interface->dev, &dev_attr_output6);
626 device_remove_file(&interface->dev, &dev_attr_output7);
627 device_remove_file(&interface->dev, &dev_attr_output8);
629 if (kit->ifkit->outputs == 16) {
630 device_remove_file(&interface->dev, &dev_attr_output9);
631 device_remove_file(&interface->dev, &dev_attr_output10);
632 device_remove_file(&interface->dev, &dev_attr_output11);
633 device_remove_file(&interface->dev, &dev_attr_output12);
634 device_remove_file(&interface->dev, &dev_attr_output13);
635 device_remove_file(&interface->dev, &dev_attr_output14);
636 device_remove_file(&interface->dev, &dev_attr_output15);
637 device_remove_file(&interface->dev, &dev_attr_output16);
640 if (kit->ifkit->inputs >= 4) {
641 device_remove_file(&interface->dev, &dev_attr_input1);
642 device_remove_file(&interface->dev, &dev_attr_input2);
643 device_remove_file(&interface->dev, &dev_attr_input3);
644 device_remove_file(&interface->dev, &dev_attr_input4);
646 if (kit->ifkit->inputs >= 8) {
647 device_remove_file(&interface->dev, &dev_attr_input5);
648 device_remove_file(&interface->dev, &dev_attr_input6);
649 device_remove_file(&interface->dev, &dev_attr_input7);
650 device_remove_file(&interface->dev, &dev_attr_input8);
652 if (kit->ifkit->inputs == 16) {
653 device_remove_file(&interface->dev, &dev_attr_input9);
654 device_remove_file(&interface->dev, &dev_attr_input10);
655 device_remove_file(&interface->dev, &dev_attr_input11);
656 device_remove_file(&interface->dev, &dev_attr_input12);
657 device_remove_file(&interface->dev, &dev_attr_input13);
658 device_remove_file(&interface->dev, &dev_attr_input14);
659 device_remove_file(&interface->dev, &dev_attr_input15);
660 device_remove_file(&interface->dev, &dev_attr_input16);
663 if (kit->ifkit->sensors >= 4) {
664 device_remove_file(&interface->dev, &dev_attr_sensor1);
665 device_remove_file(&interface->dev, &dev_attr_sensor2);
666 device_remove_file(&interface->dev, &dev_attr_sensor3);
667 device_remove_file(&interface->dev, &dev_attr_sensor4);
669 if (kit->ifkit->sensors >= 7) {
670 device_remove_file(&interface->dev, &dev_attr_sensor5);
671 device_remove_file(&interface->dev, &dev_attr_sensor6);
672 device_remove_file(&interface->dev, &dev_attr_sensor7);
674 if (kit->ifkit->sensors == 8)
675 device_remove_file(&interface->dev, &dev_attr_sensor8);
677 if (kit->ifkit->has_lcd)
678 device_remove_file(&interface->dev, &dev_attr_lcd);
680 dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
681 kit->ifkit->sensors, kit->ifkit->inputs, kit->ifkit->outputs);
683 usb_put_dev(kit->udev);
687 static struct usb_driver interfacekit_driver = {
688 .name = "phidgetkit",
689 .probe = interfacekit_probe,
690 .disconnect = interfacekit_disconnect,
694 static int __init interfacekit_init(void)
698 retval = usb_register(&interfacekit_driver);
700 err("usb_register failed. Error number %d", retval);
705 static void __exit interfacekit_exit(void)
707 usb_deregister(&interfacekit_driver);
710 module_init(interfacekit_init);
711 module_exit(interfacekit_exit);
713 MODULE_AUTHOR(DRIVER_AUTHOR);
714 MODULE_DESCRIPTION(DRIVER_DESC);
715 MODULE_LICENSE("GPL");