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/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_INTERFACEKIT01616 0x0044
29 #define USB_DEVICE_ID_INTERFACEKIT888 0x0045
30 #define USB_DEVICE_ID_INTERFACEKIT047 0x0051
31 #define USB_DEVICE_ID_INTERFACEKIT088 0x0053
33 #define USB_VENDOR_ID_WISEGROUP 0x0925
34 #define USB_DEVICE_ID_INTERFACEKIT884 0x8201
36 #define MAX_INTERFACES 16
38 #define URB_INT_SIZE 8
40 struct driver_interfacekit {
46 #define ifkit(_sensors, _inputs, _outputs, _lcd) \
47 static struct driver_interfacekit ph_##_sensors##_inputs##_outputs = { \
48 .sensors = _sensors, \
50 .outputs = _outputs, \
61 struct usb_device *udev;
62 struct usb_interface *intf;
63 struct driver_interfacekit *ifkit;
64 unsigned long outputs;
65 u8 inputs[MAX_INTERFACES];
66 u16 sensors[MAX_INTERFACES];
73 struct work_struct do_notify;
74 unsigned long input_events;
75 unsigned long sensor_events;
78 static 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},
87 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT01616),
88 .driver_info = (kernel_ulong_t)&ph_01616},
89 {USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_INTERFACEKIT884),
90 .driver_info = (kernel_ulong_t)&ph_884},
93 MODULE_DEVICE_TABLE(usb, id_table);
95 static int change_outputs(struct interfacekit *kit, int output_num, int enable)
101 set_bit(output_num, &kit->outputs);
103 clear_bit(output_num, &kit->outputs);
105 buffer = kzalloc(4, GFP_KERNEL);
107 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
110 buffer[0] = (u8)kit->outputs;
111 buffer[1] = (u8)(kit->outputs >> 8);
113 dev_dbg(&kit->udev->dev, "sending data: 0x%04x\n", (u16)kit->outputs);
115 retval = usb_control_msg(kit->udev,
116 usb_sndctrlpipe(kit->udev, 0),
117 0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2000);
120 dev_err(&kit->udev->dev, "usb_control_msg returned %d\n",
124 return retval < 0 ? retval : 0;
127 static int change_string(struct interfacekit *kit, const char *display, unsigned char row)
129 unsigned char *buffer;
130 unsigned char *form_buffer;
131 int retval = -ENOMEM;
132 int i,j, len, buf_ptr;
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__);
141 len = strlen(display);
145 dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
147 form_buffer[0] = row * 0x40 + 0x80;
148 form_buffer[1] = 0x02;
150 for (i = 0; i<len; i++)
151 form_buffer[buf_ptr++] = display[i];
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);
158 for (i = 0; i < buf_ptr; i += 7) {
159 if ((buf_ptr - i) > 7)
163 for (j = 0; j < len; j++)
164 buffer[j] = form_buffer[i + j];
167 retval = usb_control_msg(kit->udev,
168 usb_sndctrlpipe(kit->udev, 0),
169 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
182 #define set_lcd_line(number) \
183 static ssize_t lcd_line_##number(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
185 struct usb_interface *intf = to_usb_interface(dev); \
186 struct interfacekit *kit = usb_get_intfdata(intf); \
187 change_string(kit, buf, number - 1); \
190 static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
194 static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
196 struct usb_interface *intf = to_usb_interface(dev);
197 struct interfacekit *kit = usb_get_intfdata(intf);
199 unsigned char *buffer;
200 int retval = -ENOMEM;
202 buffer = kzalloc(8, GFP_KERNEL);
204 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
208 if (sscanf(buf, "%d", &enabled) < 1) {
216 dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
218 retval = usb_control_msg(kit->udev,
219 usb_sndctrlpipe(kit->udev, 0),
220 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
229 static DEVICE_ATTR(backlight, S_IWUGO, NULL, set_backlight);
231 static void remove_lcd_files(struct interfacekit *kit)
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);
241 static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
243 struct usb_interface *intf = to_usb_interface(dev);
244 struct interfacekit *kit = usb_get_intfdata(intf);
247 if (kit->ifkit->has_lcd == 0)
250 if (sscanf(buf, "%d", &enable) < 1)
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;
262 if (kit->lcd_files_on) {
263 remove_lcd_files(kit);
264 kit->lcd_files_on = 0;
270 static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
272 static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
274 struct interfacekit *kit = urb->context;
275 unsigned char *buffer = kit->data;
276 int i, level, sensor;
279 switch (urb->status) {
280 case 0: /* success */
282 case -ECONNRESET: /* unlink */
286 /* -EPIPE: should clear the halt */
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);
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);
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);
317 if (kit->ifkit->sensors) {
318 sensor = (buffer[0] & 1) ? 4 : 0;
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);
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);
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);
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);
345 if (kit->input_events || kit->sensor_events)
346 schedule_work(&kit->do_notify);
349 status = usb_submit_urb(urb, SLAB_ATOMIC);
351 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
352 kit->udev->bus->bus_name,
353 kit->udev->devpath, status);
356 static void do_notify(void *data)
358 struct interfacekit *kit = data;
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);
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);
377 #define show_set_output(value) \
378 static ssize_t set_output##value(struct device *dev, struct device_attribute *attr, const char *buf, \
381 struct usb_interface *intf = to_usb_interface(dev); \
382 struct interfacekit *kit = usb_get_intfdata(intf); \
386 if (sscanf(buf, "%d", &enabled) < 1) \
389 retval = change_outputs(kit, value - 1, enabled); \
391 return retval ? retval : count; \
394 static ssize_t show_output##value(struct device *dev, struct device_attribute *attr, char *buf) \
396 struct usb_interface *intf = to_usb_interface(dev); \
397 struct interfacekit *kit = usb_get_intfdata(intf); \
399 return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
401 static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO, \
402 show_output##value, set_output##value);
420 #define show_input(value) \
421 static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf) \
423 struct usb_interface *intf = to_usb_interface(dev); \
424 struct interfacekit *kit = usb_get_intfdata(intf); \
426 return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
428 static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
447 #define show_sensor(value) \
448 static ssize_t show_sensor##value(struct device *dev, struct device_attribute *attr, char *buf) \
450 struct usb_interface *intf = to_usb_interface(dev); \
451 struct interfacekit *kit = usb_get_intfdata(intf); \
453 return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
455 static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
466 static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
468 struct usb_device *dev = interface_to_usbdev(intf);
469 struct usb_host_interface *interface;
470 struct usb_endpoint_descriptor *endpoint;
471 struct interfacekit *kit;
472 struct driver_interfacekit *ifkit;
473 int pipe, maxp, rc = -ENOMEM;
475 ifkit = (struct driver_interfacekit *)id->driver_info;
479 interface = intf->cur_altsetting;
480 if (interface->desc.bNumEndpoints != 1)
483 endpoint = &interface->endpoint[0].desc;
484 if (!(endpoint->bEndpointAddress & 0x80))
489 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
490 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
492 kit = kzalloc(sizeof(*kit), GFP_KERNEL);
497 kit->data = usb_buffer_alloc(dev, URB_INT_SIZE, SLAB_ATOMIC, &kit->data_dma);
501 kit->irq = usb_alloc_urb(0, GFP_KERNEL);
505 kit->udev = usb_get_dev(dev);
507 INIT_WORK(&kit->do_notify, do_notify, kit);
508 usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
509 maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
510 interfacekit_irq, kit, endpoint->bInterval);
511 kit->irq->transfer_dma = kit->data_dma;
512 kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
514 usb_set_intfdata(intf, kit);
516 if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
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);
527 if (ifkit->outputs >= 8) {
528 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);
533 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);
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);
550 if (ifkit->inputs >= 8) {
551 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);
556 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);
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);
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);
578 if (ifkit->sensors == 8)
579 device_create_file(&intf->dev, &dev_attr_sensor8);
582 device_create_file(&intf->dev, &dev_attr_lcd);
584 dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
585 ifkit->sensors, ifkit->inputs, ifkit->outputs);
592 usb_free_urb(kit->irq);
594 usb_buffer_free(dev, URB_INT_SIZE, kit->data, kit->data_dma);
601 static void interfacekit_disconnect(struct usb_interface *interface)
603 struct interfacekit *kit;
605 kit = usb_get_intfdata(interface);
606 usb_set_intfdata(interface, NULL);
610 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);
614 cancel_delayed_work(&kit->do_notify);
616 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);
622 if (kit->ifkit->outputs >= 8) {
623 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);
628 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);
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);
645 if (kit->ifkit->inputs >= 8) {
646 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);
651 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);
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);
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);
673 if (kit->ifkit->sensors == 8)
674 device_remove_file(&interface->dev, &dev_attr_sensor8);
676 if (kit->ifkit->has_lcd)
677 device_remove_file(&interface->dev, &dev_attr_lcd);
679 dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
680 kit->ifkit->sensors, kit->ifkit->inputs, kit->ifkit->outputs);
682 usb_put_dev(kit->udev);
686 static struct usb_driver interfacekit_driver = {
687 .name = "phidgetkit",
688 .probe = interfacekit_probe,
689 .disconnect = interfacekit_disconnect,
693 static int __init interfacekit_init(void)
697 retval = usb_register(&interfacekit_driver);
699 err("usb_register failed. Error number %d", retval);
704 static void __exit interfacekit_exit(void)
706 usb_deregister(&interfacekit_driver);
709 module_init(interfacekit_init);
710 module_exit(interfacekit_exit);
712 MODULE_AUTHOR(DRIVER_AUTHOR);
713 MODULE_DESCRIPTION(DRIVER_DESC);
714 MODULE_LICENSE("GPL");