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 {
48 #define ifkit(_sensors, _inputs, _outputs, _lcd) \
49 static struct driver_interfacekit ph_##_sensors##_inputs##_outputs = { \
50 .sensors = _sensors, \
52 .outputs = _outputs, \
62 static unsigned long device_no;
65 struct usb_device *udev;
66 struct usb_interface *intf;
67 struct driver_interfacekit *ifkit;
69 unsigned long outputs;
71 u8 inputs[MAX_INTERFACES];
72 u16 sensors[MAX_INTERFACES];
79 struct work_struct do_notify;
80 unsigned long input_events;
81 unsigned long sensor_events;
84 static struct usb_device_id id_table[] = {
85 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT004),
86 .driver_info = (kernel_ulong_t)&ph_004},
87 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT888),
88 .driver_info = (kernel_ulong_t)&ph_888},
89 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT047),
90 .driver_info = (kernel_ulong_t)&ph_047},
91 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT088),
92 .driver_info = (kernel_ulong_t)&ph_088},
93 {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT01616),
94 .driver_info = (kernel_ulong_t)&ph_01616},
95 {USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_INTERFACEKIT884),
96 .driver_info = (kernel_ulong_t)&ph_884},
99 MODULE_DEVICE_TABLE(usb, id_table);
101 static int change_outputs(struct interfacekit *kit, int output_num, int enable)
107 set_bit(output_num, &kit->outputs);
109 clear_bit(output_num, &kit->outputs);
111 buffer = kzalloc(4, GFP_KERNEL);
113 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
116 buffer[0] = (u8)kit->outputs;
117 buffer[1] = (u8)(kit->outputs >> 8);
119 dev_dbg(&kit->udev->dev, "sending data: 0x%04x\n", (u16)kit->outputs);
121 retval = usb_control_msg(kit->udev,
122 usb_sndctrlpipe(kit->udev, 0),
123 0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2000);
126 dev_err(&kit->udev->dev, "usb_control_msg returned %d\n",
130 return retval < 0 ? retval : 0;
133 static int change_string(struct interfacekit *kit, const char *display, unsigned char row)
135 unsigned char *buffer;
136 unsigned char *form_buffer;
137 int retval = -ENOMEM;
138 int i,j, len, buf_ptr;
140 buffer = kmalloc(8, GFP_KERNEL);
141 form_buffer = kmalloc(30, GFP_KERNEL);
142 if ((!buffer) || (!form_buffer)) {
143 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
147 len = strlen(display);
151 dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
153 form_buffer[0] = row * 0x40 + 0x80;
154 form_buffer[1] = 0x02;
156 for (i = 0; i<len; i++)
157 form_buffer[buf_ptr++] = display[i];
159 for (i = 0; i < (20 - len); i++)
160 form_buffer[buf_ptr++] = 0x20;
161 form_buffer[buf_ptr++] = 0x01;
162 form_buffer[buf_ptr++] = row * 0x40 + 0x80 + strlen(display);
164 for (i = 0; i < buf_ptr; i += 7) {
165 if ((buf_ptr - i) > 7)
169 for (j = 0; j < len; j++)
170 buffer[j] = form_buffer[i + j];
173 retval = usb_control_msg(kit->udev,
174 usb_sndctrlpipe(kit->udev, 0),
175 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
188 #define set_lcd_line(number) \
189 static ssize_t lcd_line_##number(struct device *dev, \
190 struct device_attribute *attr, \
191 const char *buf, size_t count) \
193 struct interfacekit *kit = dev_get_drvdata(dev); \
194 change_string(kit, buf, number - 1); \
198 #define lcd_line_attr(number) \
199 __ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number)
204 static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
206 struct interfacekit *kit = dev_get_drvdata(dev);
208 unsigned char *buffer;
209 int retval = -ENOMEM;
211 buffer = kzalloc(8, GFP_KERNEL);
213 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
217 if (sscanf(buf, "%d", &enabled) < 1) {
225 dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
227 retval = usb_control_msg(kit->udev,
228 usb_sndctrlpipe(kit->udev, 0),
229 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
239 static struct device_attribute dev_lcd_line_attrs[] = {
242 __ATTR(backlight, S_IWUGO, NULL, set_backlight)
245 static void remove_lcd_files(struct interfacekit *kit)
249 if (kit->lcd_files_on) {
250 dev_dbg(&kit->udev->dev, "Removing lcd files\n");
252 for (i=0; i<ARRAY_SIZE(dev_lcd_line_attrs); i++)
253 device_remove_file(kit->dev, &dev_lcd_line_attrs[i]);
257 static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
259 struct interfacekit *kit = dev_get_drvdata(dev);
263 if (kit->ifkit->has_lcd == 0)
266 if (sscanf(buf, "%d", &enable) < 1)
270 if (!kit->lcd_files_on) {
271 dev_dbg(&kit->udev->dev, "Adding lcd files\n");
272 for (i=0; i<ARRAY_SIZE(dev_lcd_line_attrs); i++) {
273 rc = device_create_file(kit->dev,
274 &dev_lcd_line_attrs[i]);
278 kit->lcd_files_on = 1;
281 if (kit->lcd_files_on) {
282 remove_lcd_files(kit);
283 kit->lcd_files_on = 0;
290 device_remove_file(kit->dev, &dev_lcd_line_attrs[i]);
295 static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
297 static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
299 struct interfacekit *kit = urb->context;
300 unsigned char *buffer = kit->data;
301 int i, level, sensor;
304 switch (urb->status) {
305 case 0: /* success */
307 case -ECONNRESET: /* unlink */
311 /* -EPIPE: should clear the halt */
317 if (kit->ifkit->inputs == 16) {
318 for (i=0; i < 8; i++) {
319 level = (buffer[0] >> i) & 1;
320 if (kit->inputs[i] != level) {
321 kit->inputs[i] = level;
322 set_bit(i, &kit->input_events);
324 level = (buffer[1] >> i) & 1;
325 if (kit->inputs[8 + i] != level) {
326 kit->inputs[8 + i] = level;
327 set_bit(8 + i, &kit->input_events);
331 else if (kit->ifkit->inputs == 8) {
332 for (i=0; i < 8; i++) {
333 level = (buffer[1] >> i) & 1;
334 if (kit->inputs[i] != level) {
335 kit->inputs[i] = level;
336 set_bit(i, &kit->input_events);
342 if (kit->ifkit->sensors) {
343 sensor = (buffer[0] & 1) ? 4 : 0;
345 level = buffer[2] + (buffer[3] & 0x0f) * 256;
346 if (level != kit->sensors[sensor]) {
347 kit->sensors[sensor] = level;
348 set_bit(sensor, &kit->sensor_events);
351 level = buffer[4] + (buffer[3] & 0xf0) * 16;
352 if (level != kit->sensors[sensor]) {
353 kit->sensors[sensor] = level;
354 set_bit(sensor, &kit->sensor_events);
357 level = buffer[5] + (buffer[6] & 0x0f) * 256;
358 if (level != kit->sensors[sensor]) {
359 kit->sensors[sensor] = level;
360 set_bit(sensor, &kit->sensor_events);
363 level = buffer[7] + (buffer[6] & 0xf0) * 16;
364 if (level != kit->sensors[sensor]) {
365 kit->sensors[sensor] = level;
366 set_bit(sensor, &kit->sensor_events);
370 if (kit->input_events || kit->sensor_events)
371 schedule_work(&kit->do_notify);
374 status = usb_submit_urb(urb, SLAB_ATOMIC);
376 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
377 kit->udev->bus->bus_name,
378 kit->udev->devpath, status);
381 static void do_notify(void *data)
383 struct interfacekit *kit = data;
387 for (i=0; i<kit->ifkit->inputs; i++) {
388 if (test_and_clear_bit(i, &kit->input_events)) {
389 sprintf(sysfs_file, "input%d", i + 1);
390 sysfs_notify(&kit->dev->kobj, NULL, sysfs_file);
394 for (i=0; i<kit->ifkit->sensors; i++) {
395 if (test_and_clear_bit(i, &kit->sensor_events)) {
396 sprintf(sysfs_file, "sensor%d", i + 1);
397 sysfs_notify(&kit->dev->kobj, NULL, sysfs_file);
402 #define show_set_output(value) \
403 static ssize_t set_output##value(struct device *dev, \
404 struct device_attribute *attr, \
405 const char *buf, size_t count) \
407 struct interfacekit *kit = dev_get_drvdata(dev); \
411 if (sscanf(buf, "%d", &enabled) < 1) \
414 retval = change_outputs(kit, value - 1, enabled); \
416 return retval ? retval : count; \
419 static ssize_t show_output##value(struct device *dev, \
420 struct device_attribute *attr, \
423 struct interfacekit *kit = dev_get_drvdata(dev); \
425 return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
428 #define output_attr(value) \
429 __ATTR(output##value, S_IWUGO | S_IRUGO, \
430 show_output##value, set_output##value)
449 static struct device_attribute dev_output_attrs[] = {
450 output_attr(1), output_attr(2), output_attr(3), output_attr(4),
451 output_attr(5), output_attr(6), output_attr(7), output_attr(8),
452 output_attr(9), output_attr(10), output_attr(11), output_attr(12),
453 output_attr(13), output_attr(14), output_attr(15), output_attr(16)
456 #define show_input(value) \
457 static ssize_t show_input##value(struct device *dev, \
458 struct device_attribute *attr, char *buf) \
460 struct interfacekit *kit = dev_get_drvdata(dev); \
462 return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
465 #define input_attr(value) \
466 __ATTR(input##value, S_IRUGO, show_input##value, NULL)
485 static struct device_attribute dev_input_attrs[] = {
486 input_attr(1), input_attr(2), input_attr(3), input_attr(4),
487 input_attr(5), input_attr(6), input_attr(7), input_attr(8),
488 input_attr(9), input_attr(10), input_attr(11), input_attr(12),
489 input_attr(13), input_attr(14), input_attr(15), input_attr(16)
492 #define show_sensor(value) \
493 static ssize_t show_sensor##value(struct device *dev, \
494 struct device_attribute *attr, \
497 struct interfacekit *kit = dev_get_drvdata(dev); \
499 return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
502 #define sensor_attr(value) \
503 __ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL)
514 static struct device_attribute dev_sensor_attrs[] = {
515 sensor_attr(1), sensor_attr(2), sensor_attr(3), sensor_attr(4),
516 sensor_attr(5), sensor_attr(6), sensor_attr(7), sensor_attr(8)
519 static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
521 struct usb_device *dev = interface_to_usbdev(intf);
522 struct usb_host_interface *interface;
523 struct usb_endpoint_descriptor *endpoint;
524 struct interfacekit *kit;
525 struct driver_interfacekit *ifkit;
526 int pipe, maxp, rc = -ENOMEM;
529 ifkit = (struct driver_interfacekit *)id->driver_info;
533 interface = intf->cur_altsetting;
534 if (interface->desc.bNumEndpoints != 1)
537 endpoint = &interface->endpoint[0].desc;
538 if (!(endpoint->bEndpointAddress & 0x80))
543 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
544 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
546 kit = kzalloc(sizeof(*kit), GFP_KERNEL);
552 kit->data = usb_buffer_alloc(dev, URB_INT_SIZE, SLAB_ATOMIC, &kit->data_dma);
556 kit->irq = usb_alloc_urb(0, GFP_KERNEL);
560 kit->udev = usb_get_dev(dev);
562 INIT_WORK(&kit->do_notify, do_notify, kit);
563 usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
564 maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
565 interfacekit_irq, kit, endpoint->bInterval);
566 kit->irq->transfer_dma = kit->data_dma;
567 kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
569 usb_set_intfdata(intf, kit);
572 bit = find_first_zero_bit(&device_no, sizeof(device_no));
573 value = test_and_set_bit(bit, &device_no);
577 kit->dev = device_create(phidget_class, &kit->udev->dev, 0,
578 "interfacekit%d", kit->dev_no);
579 if (IS_ERR(kit->dev)) {
580 rc = PTR_ERR(kit->dev);
584 dev_set_drvdata(kit->dev, kit);
586 if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
591 for (i=0; i<ifkit->outputs; i++ ) {
592 rc = device_create_file(kit->dev, &dev_output_attrs[i]);
597 for (i=0; i<ifkit->inputs; i++ ) {
598 rc = device_create_file(kit->dev, &dev_input_attrs[i]);
603 for (i=0; i<ifkit->sensors; i++ ) {
604 rc = device_create_file(kit->dev, &dev_sensor_attrs[i]);
609 if (ifkit->has_lcd) {
610 rc = device_create_file(kit->dev, &dev_attr_lcd);
616 dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
617 ifkit->sensors, ifkit->inputs, ifkit->outputs);
623 device_remove_file(kit->dev, &dev_sensor_attrs[i]);
628 device_remove_file(kit->dev, &dev_input_attrs[i]);
633 device_remove_file(kit->dev, &dev_output_attrs[i]);
637 usb_free_urb(kit->irq);
639 usb_buffer_free(dev, URB_INT_SIZE, kit->data, kit->data_dma);
641 device_unregister(kit->dev);
642 if (kit->dev_no >= 0)
643 clear_bit(kit->dev_no, &device_no);
651 static void interfacekit_disconnect(struct usb_interface *interface)
653 struct interfacekit *kit;
656 kit = usb_get_intfdata(interface);
657 usb_set_intfdata(interface, NULL);
661 usb_kill_urb(kit->irq);
662 usb_free_urb(kit->irq);
663 usb_buffer_free(kit->udev, URB_INT_SIZE, kit->data, kit->data_dma);
665 cancel_delayed_work(&kit->do_notify);
667 for (i=0; i<kit->ifkit->outputs; i++)
668 device_remove_file(kit->dev, &dev_output_attrs[i]);
670 for (i=0; i<kit->ifkit->inputs; i++)
671 device_remove_file(kit->dev, &dev_input_attrs[i]);
673 for (i=0; i<kit->ifkit->sensors; i++)
674 device_remove_file(kit->dev, &dev_sensor_attrs[i]);
676 if (kit->ifkit->has_lcd) {
677 device_remove_file(kit->dev, &dev_attr_lcd);
678 remove_lcd_files(kit);
681 device_unregister(kit->dev);
683 dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
684 kit->ifkit->sensors, kit->ifkit->inputs, kit->ifkit->outputs);
686 usb_put_dev(kit->udev);
687 clear_bit(kit->dev_no, &device_no);
692 static struct usb_driver interfacekit_driver = {
693 .name = "phidgetkit",
694 .probe = interfacekit_probe,
695 .disconnect = interfacekit_disconnect,
699 static int __init interfacekit_init(void)
703 retval = usb_register(&interfacekit_driver);
705 err("usb_register failed. Error number %d", retval);
710 static void __exit interfacekit_exit(void)
712 usb_deregister(&interfacekit_driver);
715 module_init(interfacekit_init);
716 module_exit(interfacekit_exit);
718 MODULE_AUTHOR(DRIVER_AUTHOR);
719 MODULE_DESCRIPTION(DRIVER_DESC);
720 MODULE_LICENSE("GPL");