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, "data: %02x %02x\n", buffer[0], buffer[1]);
108 retval = usb_control_msg(kit->udev,
109 usb_sndctrlpipe(kit->udev, 0),
110 0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2 * HZ);
113 dev_err(&kit->udev->dev, "retval = %d\n", retval);
116 return retval < 0 ? retval : 0;
119 static int change_string(struct phidget_interfacekit *kit, const char *display, unsigned char row)
121 unsigned char *buffer;
122 unsigned char *form_buffer;
123 int retval = -ENOMEM;
124 int i,j, len, buf_ptr;
126 buffer = kmalloc(8, GFP_KERNEL);
127 form_buffer = kmalloc(30, GFP_KERNEL);
128 if ((!buffer) || (!form_buffer)) {
129 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
133 len = strlen(display);
137 dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
139 form_buffer[0] = row * 0x40 + 0x80;
140 form_buffer[1] = 0x02;
142 for (i = 0; i<len; i++)
143 form_buffer[buf_ptr++] = display[i];
145 for (i = 0; i < (20 - len); i++)
146 form_buffer[buf_ptr++] = 0x20;
147 form_buffer[buf_ptr++] = 0x01;
148 form_buffer[buf_ptr++] = row * 0x40 + 0x80 + strlen(display);
150 for (i = 0; i < buf_ptr; i += 7) {
151 if ((buf_ptr - i) > 7)
155 for (j = 0; j < len; j++)
156 buffer[j] = form_buffer[i + j];
159 retval = usb_control_msg(kit->udev,
160 usb_sndctrlpipe(kit->udev, 0),
161 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2 * HZ);
174 #define set_lcd_line(number) \
175 static ssize_t lcd_line_##number(struct device *dev, const char *buf, size_t count) \
177 struct usb_interface *intf = to_usb_interface(dev); \
178 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
179 change_string(kit, buf, number - 1); \
182 static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
186 static ssize_t set_backlight(struct device *dev, const char *buf, size_t count)
188 struct usb_interface *intf = to_usb_interface(dev);
189 struct phidget_interfacekit *kit = usb_get_intfdata(intf);
191 unsigned char *buffer;
192 int retval = -ENOMEM;
194 buffer = kmalloc(8, GFP_KERNEL);
196 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
200 if (sscanf(buf, "%d", &enabled) < 1) {
204 memset(buffer, 0x00, 8);
209 dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
211 retval = usb_control_msg(kit->udev,
212 usb_sndctrlpipe(kit->udev, 0),
213 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2 * HZ);
222 static DEVICE_ATTR(backlight, S_IWUGO, NULL, set_backlight);
224 static void remove_lcd_files(struct phidget_interfacekit *kit)
226 if (kit->lcd_files_on) {
227 dev_dbg(&kit->udev->dev, "Removing lcd files\n");
228 device_remove_file(&kit->intf->dev, &dev_attr_lcd_line_1);
229 device_remove_file(&kit->intf->dev, &dev_attr_lcd_line_2);
230 device_remove_file(&kit->intf->dev, &dev_attr_backlight);
234 static ssize_t enable_lcd_files(struct device *dev, const char *buf, size_t count)
236 struct usb_interface *intf = to_usb_interface(dev);
237 struct phidget_interfacekit *kit = usb_get_intfdata(intf);
240 if (kit->ifkit->has_lcd == 0)
243 if (sscanf(buf, "%d", &enable) < 1)
247 if (!kit->lcd_files_on) {
248 dev_dbg(&kit->udev->dev, "Adding lcd files\n");
249 device_create_file(&kit->intf->dev, &dev_attr_lcd_line_1);
250 device_create_file(&kit->intf->dev, &dev_attr_lcd_line_2);
251 device_create_file(&kit->intf->dev, &dev_attr_backlight);
252 kit->lcd_files_on = 1;
255 if (kit->lcd_files_on) {
256 remove_lcd_files(kit);
257 kit->lcd_files_on = 0;
263 static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
265 static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
267 struct phidget_interfacekit *kit = urb->context;
268 unsigned char *buffer = kit->data;
272 switch (urb->status) {
273 case 0: /* success */
275 case -ECONNRESET: /* unlink */
279 /* -EPIPE: should clear the halt */
284 for (n=0; n<8; n++) {
285 kit->inputs[n] = buffer[1] & (1 << n) ? 1 : 0;
289 kit->sensors[4] = buffer[2] + (buffer[3] & 0x0f) * 256;
290 kit->sensors[5] = buffer[4] + (buffer[3] & 0xf0) * 16;
291 kit->sensors[6] = buffer[5] + (buffer[6] & 0x0f) * 256;
292 kit->sensors[7] = buffer[7] + (buffer[6] & 0xf0) * 16;
294 kit->sensors[0] = buffer[2] + (buffer[3] & 0x0f) * 256;
295 kit->sensors[1] = buffer[4] + (buffer[3] & 0xf0) * 16;
296 kit->sensors[2] = buffer[5] + (buffer[6] & 0x0f) * 256;
297 kit->sensors[3] = buffer[7] + (buffer[6] & 0xf0) * 16;
301 status = usb_submit_urb(urb, SLAB_ATOMIC);
303 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
304 kit->udev->bus->bus_name,
305 kit->udev->devpath, status);
308 #define show_set_output(value) \
309 static ssize_t set_output##value(struct device *dev, const char *buf, \
312 struct usb_interface *intf = to_usb_interface(dev); \
313 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
317 if (sscanf(buf, "%d", &enabled) < 1) { \
321 retval = change_outputs(kit, value - 1, enabled ? 1 : 0); \
323 return retval ? retval : count; \
326 static ssize_t show_output##value(struct device *dev, char *buf) \
328 struct usb_interface *intf = to_usb_interface(dev); \
329 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
331 return sprintf(buf, "%d\n", kit->outputs[value - 1 ]); \
333 static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO, \
334 show_output##value, set_output##value);
342 show_set_output(8); /* should be MAX_INTERFACES - 1 */
344 #define show_input(value) \
345 static ssize_t show_input##value(struct device *dev, char *buf) \
347 struct usb_interface *intf = to_usb_interface(dev); \
348 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
350 return sprintf(buf, "%d\n", kit->inputs[value - 1]); \
352 static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
361 show_input(8); /* should be MAX_INTERFACES - 1 */
363 #define show_sensor(value) \
364 static ssize_t show_sensor##value(struct device *dev, char *buf) \
366 struct usb_interface *intf = to_usb_interface(dev); \
367 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
369 return sprintf(buf, "%d\n", kit->sensors[value - 1]); \
371 static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
380 show_sensor(8); /* should be MAX_INTERFACES - 1 */
382 static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
384 struct usb_device *dev = interface_to_usbdev(intf);
385 struct usb_host_interface *interface;
386 struct usb_endpoint_descriptor *endpoint;
387 struct phidget_interfacekit *kit;
388 struct driver_interfacekit *ifkit;
391 ifkit = (struct driver_interfacekit *)id->driver_info;
395 interface = intf->cur_altsetting;
396 if (interface->desc.bNumEndpoints != 1)
399 endpoint = &interface->endpoint[0].desc;
400 if (!(endpoint->bEndpointAddress & 0x80))
405 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
406 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
408 kit = kmalloc(sizeof(*kit), GFP_KERNEL);
410 dev_err(&intf->dev, "%s - out of memory\n", __FUNCTION__);
413 memset(kit, 0, sizeof(*kit));
416 kit->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kit->data_dma);
422 kit->irq = usb_alloc_urb(0, GFP_KERNEL);
424 usb_buffer_free(dev, 8, kit->data, kit->data_dma);
429 kit->udev = usb_get_dev(dev);
431 usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
432 (maxp > 8 ? 8 : maxp),
433 interfacekit_irq, kit, endpoint->bInterval);
434 kit->irq->transfer_dma = kit->data_dma;
435 kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
437 usb_set_intfdata(intf, kit);
439 if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
443 if (ifkit->outputs == 8) {
444 device_create_file(&intf->dev, &dev_attr_output1);
445 device_create_file(&intf->dev, &dev_attr_output2);
446 device_create_file(&intf->dev, &dev_attr_output3);
447 device_create_file(&intf->dev, &dev_attr_output4);
448 device_create_file(&intf->dev, &dev_attr_output5);
449 device_create_file(&intf->dev, &dev_attr_output6);
450 device_create_file(&intf->dev, &dev_attr_output7);
451 device_create_file(&intf->dev, &dev_attr_output8);
454 if (ifkit->inputs >= 4) {
455 device_create_file(&intf->dev, &dev_attr_input1);
456 device_create_file(&intf->dev, &dev_attr_input2);
457 device_create_file(&intf->dev, &dev_attr_input3);
458 device_create_file(&intf->dev, &dev_attr_input4);
460 if (ifkit->inputs == 8) {
461 device_create_file(&intf->dev, &dev_attr_input5);
462 device_create_file(&intf->dev, &dev_attr_input6);
463 device_create_file(&intf->dev, &dev_attr_input7);
464 device_create_file(&intf->dev, &dev_attr_input8);
467 if (ifkit->sensors >= 4) {
468 device_create_file(&intf->dev, &dev_attr_sensor1);
469 device_create_file(&intf->dev, &dev_attr_sensor2);
470 device_create_file(&intf->dev, &dev_attr_sensor3);
471 device_create_file(&intf->dev, &dev_attr_sensor4);
473 if (ifkit->sensors >= 7) {
474 device_create_file(&intf->dev, &dev_attr_sensor5);
475 device_create_file(&intf->dev, &dev_attr_sensor6);
476 device_create_file(&intf->dev, &dev_attr_sensor7);
478 if (ifkit->sensors == 8) {
479 device_create_file(&intf->dev, &dev_attr_sensor8);
483 device_create_file(&intf->dev, &dev_attr_lcd);
485 dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
486 ifkit->inputs, ifkit->outputs, ifkit->sensors);
491 static void interfacekit_disconnect(struct usb_interface *interface)
493 struct phidget_interfacekit *kit;
495 kit = usb_get_intfdata(interface);
496 usb_set_intfdata(interface, NULL);
500 if (kit->ifkit->outputs == MAX_INTERFACES) {
501 device_remove_file(&interface->dev, &dev_attr_output1);
502 device_remove_file(&interface->dev, &dev_attr_output2);
503 device_remove_file(&interface->dev, &dev_attr_output3);
504 device_remove_file(&interface->dev, &dev_attr_output4);
505 device_remove_file(&interface->dev, &dev_attr_output5);
506 device_remove_file(&interface->dev, &dev_attr_output6);
507 device_remove_file(&interface->dev, &dev_attr_output7);
508 device_remove_file(&interface->dev, &dev_attr_output7);
511 if (kit->ifkit->inputs >= 4) {
512 device_remove_file(&interface->dev, &dev_attr_input1);
513 device_remove_file(&interface->dev, &dev_attr_input2);
514 device_remove_file(&interface->dev, &dev_attr_input3);
515 device_remove_file(&interface->dev, &dev_attr_input4);
517 if (kit->ifkit->inputs == 8) {
518 device_remove_file(&interface->dev, &dev_attr_input5);
519 device_remove_file(&interface->dev, &dev_attr_input6);
520 device_remove_file(&interface->dev, &dev_attr_input7);
521 device_remove_file(&interface->dev, &dev_attr_input8);
524 if (kit->ifkit->sensors >= 4) {
525 device_remove_file(&interface->dev, &dev_attr_sensor1);
526 device_remove_file(&interface->dev, &dev_attr_sensor2);
527 device_remove_file(&interface->dev, &dev_attr_sensor3);
528 device_remove_file(&interface->dev, &dev_attr_sensor4);
530 if (kit->ifkit->sensors >= 7) {
531 device_remove_file(&interface->dev, &dev_attr_sensor5);
532 device_remove_file(&interface->dev, &dev_attr_sensor6);
533 device_remove_file(&interface->dev, &dev_attr_sensor7);
535 if (kit->ifkit->sensors == 8) {
536 device_remove_file(&interface->dev, &dev_attr_sensor8);
538 if (kit->ifkit->has_lcd)
539 device_create_file(&interface->dev, &dev_attr_lcd);
541 dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
542 kit->ifkit->inputs, kit->ifkit->outputs, kit->ifkit->sensors);
544 usb_kill_urb(kit->irq);
545 usb_free_urb(kit->irq);
546 usb_buffer_free(kit->udev, 8, kit->data, kit->data_dma);
548 usb_put_dev(kit->udev);
552 static struct usb_driver interfacekit_driver = {
553 .owner = THIS_MODULE,
554 .name = "phidgetkit",
555 .probe = interfacekit_probe,
556 .disconnect = interfacekit_disconnect,
560 static int __init interfacekit_init(void)
564 retval = usb_register(&interfacekit_driver);
566 err("usb_register failed. Error number %d", retval);
571 static void __exit interfacekit_exit(void)
573 usb_deregister(&interfacekit_driver);
576 module_init(interfacekit_init);
577 module_exit(interfacekit_exit);
579 MODULE_AUTHOR(DRIVER_AUTHOR);
580 MODULE_DESCRIPTION(DRIVER_DESC);
581 MODULE_LICENSE("GPL");