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>
25 #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
26 #define DRIVER_DESC "USB PhidgetInterfaceKit Driver"
28 #define USB_VENDOR_ID_GLAB 0x06c2
29 #define USB_DEVICE_ID_INTERFACEKIT004 0x0040
30 #define USB_DEVICE_ID_INTERFACEKIT01616 0x0044
31 #define USB_DEVICE_ID_INTERFACEKIT888 0x0045
32 #define USB_DEVICE_ID_INTERFACEKIT047 0x0051
33 #define USB_DEVICE_ID_INTERFACEKIT088 0x0053
35 #define USB_VENDOR_ID_WISEGROUP 0x0925
36 #define USB_DEVICE_ID_INTERFACEKIT884 0x8201
38 #define MAX_INTERFACES 16
40 #define URB_INT_SIZE 8
42 struct driver_interfacekit {
50 #define ifkit(_sensors, _inputs, _outputs, _lcd, _amnesiac) \
52 .sensors = _sensors, \
54 .outputs = _outputs, \
56 .amnesiac = _amnesiac \
59 static const struct driver_interfacekit ph_004 = ifkit(0, 0, 4, 0, 0);
60 static const struct driver_interfacekit ph_888n = ifkit(8, 8, 8, 0, 1);
61 static const struct driver_interfacekit ph_888o = ifkit(8, 8, 8, 0, 0);
62 static const struct driver_interfacekit ph_047 = ifkit(0, 4, 7, 1, 0);
63 static const struct driver_interfacekit ph_884 = ifkit(8, 8, 4, 0, 0);
64 static const struct driver_interfacekit ph_088 = ifkit(0, 8, 8, 1, 0);
65 static const struct driver_interfacekit ph_01616 = ifkit(0, 16, 16, 0, 0);
67 static unsigned long device_no;
70 struct usb_device *udev;
71 struct usb_interface *intf;
72 struct driver_interfacekit *ifkit;
74 unsigned long outputs;
76 u8 inputs[MAX_INTERFACES];
77 u16 sensors[MAX_INTERFACES];
84 struct delayed_work do_notify;
85 struct delayed_work do_resubmit;
86 unsigned long input_events;
87 unsigned long sensor_events;
90 static 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},
93 {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},
97 {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},
101 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT01616),
102 .driver_info = (kernel_ulong_t)&ph_01616},
103 {USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_INTERFACEKIT884),
104 .driver_info = (kernel_ulong_t)&ph_884},
107 MODULE_DEVICE_TABLE(usb, id_table);
109 static int set_outputs(struct interfacekit *kit)
114 buffer = kzalloc(4, GFP_KERNEL);
116 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
119 buffer[0] = (u8)kit->outputs;
120 buffer[1] = (u8)(kit->outputs >> 8);
122 dev_dbg(&kit->udev->dev, "sending data: 0x%04x\n", (u16)kit->outputs);
124 retval = usb_control_msg(kit->udev,
125 usb_sndctrlpipe(kit->udev, 0),
126 0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2000);
129 dev_err(&kit->udev->dev, "usb_control_msg returned %d\n",
133 if (kit->ifkit->amnesiac)
134 schedule_delayed_work(&kit->do_resubmit, HZ / 2);
136 return retval < 0 ? retval : 0;
139 static int change_string(struct interfacekit *kit, const char *display, unsigned char row)
141 unsigned char *buffer;
142 unsigned char *form_buffer;
143 int retval = -ENOMEM;
144 int i,j, len, buf_ptr;
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__);
153 len = strlen(display);
157 dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
159 form_buffer[0] = row * 0x40 + 0x80;
160 form_buffer[1] = 0x02;
162 for (i = 0; i<len; i++)
163 form_buffer[buf_ptr++] = display[i];
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);
170 for (i = 0; i < buf_ptr; i += 7) {
171 if ((buf_ptr - i) > 7)
175 for (j = 0; j < len; j++)
176 buffer[j] = form_buffer[i + j];
179 retval = usb_control_msg(kit->udev,
180 usb_sndctrlpipe(kit->udev, 0),
181 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
194 #define set_lcd_line(number) \
195 static ssize_t lcd_line_##number(struct device *dev, \
196 struct device_attribute *attr, \
197 const char *buf, size_t count) \
199 struct interfacekit *kit = dev_get_drvdata(dev); \
200 change_string(kit, buf, number - 1); \
204 #define lcd_line_attr(number) \
205 __ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number)
210 static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
212 struct interfacekit *kit = dev_get_drvdata(dev);
214 unsigned char *buffer;
215 int retval = -ENOMEM;
217 buffer = kzalloc(8, GFP_KERNEL);
219 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
223 if (sscanf(buf, "%d", &enabled) < 1) {
231 dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
233 retval = usb_control_msg(kit->udev,
234 usb_sndctrlpipe(kit->udev, 0),
235 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
245 static struct device_attribute dev_lcd_line_attrs[] = {
248 __ATTR(backlight, S_IWUGO, NULL, set_backlight)
251 static void remove_lcd_files(struct interfacekit *kit)
255 if (kit->lcd_files_on) {
256 dev_dbg(&kit->udev->dev, "Removing lcd files\n");
258 for (i=0; i<ARRAY_SIZE(dev_lcd_line_attrs); i++)
259 device_remove_file(kit->dev, &dev_lcd_line_attrs[i]);
263 static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
265 struct interfacekit *kit = dev_get_drvdata(dev);
269 if (kit->ifkit->has_lcd == 0)
272 if (sscanf(buf, "%d", &enable) < 1)
276 if (!kit->lcd_files_on) {
277 dev_dbg(&kit->udev->dev, "Adding lcd files\n");
278 for (i=0; i<ARRAY_SIZE(dev_lcd_line_attrs); i++) {
279 rc = device_create_file(kit->dev,
280 &dev_lcd_line_attrs[i]);
284 kit->lcd_files_on = 1;
287 if (kit->lcd_files_on) {
288 remove_lcd_files(kit);
289 kit->lcd_files_on = 0;
296 device_remove_file(kit->dev, &dev_lcd_line_attrs[i]);
301 static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
303 static void interfacekit_irq(struct urb *urb)
305 struct interfacekit *kit = urb->context;
306 unsigned char *buffer = kit->data;
307 int i, level, sensor;
310 switch (urb->status) {
311 case 0: /* success */
313 case -ECONNRESET: /* unlink */
317 /* -EPIPE: should clear the halt */
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);
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);
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);
348 if (kit->ifkit->sensors) {
349 sensor = (buffer[0] & 1) ? 4 : 0;
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);
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);
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);
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);
376 if (kit->input_events || kit->sensor_events)
377 schedule_delayed_work(&kit->do_notify, 0);
380 status = usb_submit_urb(urb, GFP_ATOMIC);
382 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
383 kit->udev->bus->bus_name,
384 kit->udev->devpath, status);
387 static void do_notify(struct work_struct *work)
389 struct interfacekit *kit =
390 container_of(work, struct interfacekit, do_notify.work);
394 for (i=0; i<kit->ifkit->inputs; i++) {
395 if (test_and_clear_bit(i, &kit->input_events)) {
396 sprintf(sysfs_file, "input%d", i + 1);
397 sysfs_notify(&kit->dev->kobj, NULL, sysfs_file);
401 for (i=0; i<kit->ifkit->sensors; i++) {
402 if (test_and_clear_bit(i, &kit->sensor_events)) {
403 sprintf(sysfs_file, "sensor%d", i + 1);
404 sysfs_notify(&kit->dev->kobj, NULL, sysfs_file);
409 static void do_resubmit(struct work_struct *work)
411 struct interfacekit *kit =
412 container_of(work, struct interfacekit, do_resubmit.work);
416 #define show_set_output(value) \
417 static ssize_t set_output##value(struct device *dev, \
418 struct device_attribute *attr, \
419 const char *buf, size_t count) \
421 struct interfacekit *kit = dev_get_drvdata(dev); \
425 if (sscanf(buf, "%d", &enable) < 1) \
429 set_bit(value - 1, &kit->outputs); \
431 clear_bit(value - 1, &kit->outputs); \
433 retval = set_outputs(kit); \
435 return retval ? retval : count; \
438 static ssize_t show_output##value(struct device *dev, \
439 struct device_attribute *attr, \
442 struct interfacekit *kit = dev_get_drvdata(dev); \
444 return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
447 #define output_attr(value) \
448 __ATTR(output##value, S_IWUGO | S_IRUGO, \
449 show_output##value, set_output##value)
468 static struct device_attribute dev_output_attrs[] = {
469 output_attr(1), output_attr(2), output_attr(3), output_attr(4),
470 output_attr(5), output_attr(6), output_attr(7), output_attr(8),
471 output_attr(9), output_attr(10), output_attr(11), output_attr(12),
472 output_attr(13), output_attr(14), output_attr(15), output_attr(16)
475 #define show_input(value) \
476 static ssize_t show_input##value(struct device *dev, \
477 struct device_attribute *attr, char *buf) \
479 struct interfacekit *kit = dev_get_drvdata(dev); \
481 return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
484 #define input_attr(value) \
485 __ATTR(input##value, S_IRUGO, show_input##value, NULL)
504 static struct device_attribute dev_input_attrs[] = {
505 input_attr(1), input_attr(2), input_attr(3), input_attr(4),
506 input_attr(5), input_attr(6), input_attr(7), input_attr(8),
507 input_attr(9), input_attr(10), input_attr(11), input_attr(12),
508 input_attr(13), input_attr(14), input_attr(15), input_attr(16)
511 #define show_sensor(value) \
512 static ssize_t show_sensor##value(struct device *dev, \
513 struct device_attribute *attr, \
516 struct interfacekit *kit = dev_get_drvdata(dev); \
518 return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
521 #define sensor_attr(value) \
522 __ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL)
533 static struct device_attribute dev_sensor_attrs[] = {
534 sensor_attr(1), sensor_attr(2), sensor_attr(3), sensor_attr(4),
535 sensor_attr(5), sensor_attr(6), sensor_attr(7), sensor_attr(8)
538 static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
540 struct usb_device *dev = interface_to_usbdev(intf);
541 struct usb_host_interface *interface;
542 struct usb_endpoint_descriptor *endpoint;
543 struct interfacekit *kit;
544 struct driver_interfacekit *ifkit;
545 int pipe, maxp, rc = -ENOMEM;
548 ifkit = (struct driver_interfacekit *)id->driver_info;
552 interface = intf->cur_altsetting;
553 if (interface->desc.bNumEndpoints != 1)
556 endpoint = &interface->endpoint[0].desc;
557 if (!usb_endpoint_dir_in(endpoint))
562 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
563 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
565 kit = kzalloc(sizeof(*kit), GFP_KERNEL);
571 kit->data = usb_buffer_alloc(dev, URB_INT_SIZE, GFP_ATOMIC, &kit->data_dma);
575 kit->irq = usb_alloc_urb(0, GFP_KERNEL);
579 kit->udev = usb_get_dev(dev);
581 INIT_DELAYED_WORK(&kit->do_notify, do_notify);
582 INIT_DELAYED_WORK(&kit->do_resubmit, do_resubmit);
583 usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
584 maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
585 interfacekit_irq, kit, endpoint->bInterval);
586 kit->irq->transfer_dma = kit->data_dma;
587 kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
589 usb_set_intfdata(intf, kit);
592 bit = find_first_zero_bit(&device_no, sizeof(device_no));
593 value = test_and_set_bit(bit, &device_no);
597 kit->dev = device_create(phidget_class, &kit->udev->dev, 0,
598 "interfacekit%d", kit->dev_no);
599 if (IS_ERR(kit->dev)) {
600 rc = PTR_ERR(kit->dev);
604 dev_set_drvdata(kit->dev, kit);
606 if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
611 for (i=0; i<ifkit->outputs; i++ ) {
612 rc = device_create_file(kit->dev, &dev_output_attrs[i]);
617 for (i=0; i<ifkit->inputs; i++ ) {
618 rc = device_create_file(kit->dev, &dev_input_attrs[i]);
623 for (i=0; i<ifkit->sensors; i++ ) {
624 rc = device_create_file(kit->dev, &dev_sensor_attrs[i]);
629 if (ifkit->has_lcd) {
630 rc = device_create_file(kit->dev, &dev_attr_lcd);
636 dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
637 ifkit->sensors, ifkit->inputs, ifkit->outputs);
643 device_remove_file(kit->dev, &dev_sensor_attrs[i]);
648 device_remove_file(kit->dev, &dev_input_attrs[i]);
653 device_remove_file(kit->dev, &dev_output_attrs[i]);
656 usb_free_urb(kit->irq);
658 usb_buffer_free(dev, URB_INT_SIZE, kit->data, kit->data_dma);
660 device_unregister(kit->dev);
661 if (kit->dev_no >= 0)
662 clear_bit(kit->dev_no, &device_no);
670 static void interfacekit_disconnect(struct usb_interface *interface)
672 struct interfacekit *kit;
675 kit = usb_get_intfdata(interface);
676 usb_set_intfdata(interface, NULL);
680 usb_kill_urb(kit->irq);
681 usb_free_urb(kit->irq);
682 usb_buffer_free(kit->udev, URB_INT_SIZE, kit->data, kit->data_dma);
684 cancel_delayed_work(&kit->do_notify);
685 cancel_delayed_work(&kit->do_resubmit);
687 for (i=0; i<kit->ifkit->outputs; i++)
688 device_remove_file(kit->dev, &dev_output_attrs[i]);
690 for (i=0; i<kit->ifkit->inputs; i++)
691 device_remove_file(kit->dev, &dev_input_attrs[i]);
693 for (i=0; i<kit->ifkit->sensors; i++)
694 device_remove_file(kit->dev, &dev_sensor_attrs[i]);
696 if (kit->ifkit->has_lcd) {
697 device_remove_file(kit->dev, &dev_attr_lcd);
698 remove_lcd_files(kit);
701 device_unregister(kit->dev);
703 dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
704 kit->ifkit->sensors, kit->ifkit->inputs, kit->ifkit->outputs);
706 usb_put_dev(kit->udev);
707 clear_bit(kit->dev_no, &device_no);
712 static struct usb_driver interfacekit_driver = {
713 .name = "phidgetkit",
714 .probe = interfacekit_probe,
715 .disconnect = interfacekit_disconnect,
719 static int __init interfacekit_init(void)
723 retval = usb_register(&interfacekit_driver);
725 err("usb_register failed. Error number %d", retval);
730 static void __exit interfacekit_exit(void)
732 usb_deregister(&interfacekit_driver);
735 module_init(interfacekit_init);
736 module_exit(interfacekit_exit);
738 MODULE_AUTHOR(DRIVER_AUTHOR);
739 MODULE_DESCRIPTION(DRIVER_DESC);
740 MODULE_LICENSE("GPL");