1 /*
2 * USB PhidgetInterfaceKit driver 1.0
3 *
4 * Copyright (C) 2004 Sean Young <sean@mess.org>
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 *
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.
11 *
12 * This is a driver for the USB PhidgetInterfaceKit.
13 */
14
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>
22
23 #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
24 #define DRIVER_DESC "USB PhidgetInterfaceKit Driver"
25
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
31
32 #define USB_VENDOR_ID_WISEGROUP 0x0925
33 #define USB_DEVICE_ID_INTERFACEKIT884 0x8201
34
35 #define MAX_INTERFACES 8
36
37 struct driver_interfacekit {
38 int sensors;
39 int inputs;
40 int outputs;
41 int has_lcd;
42 };
43 #define ifkit(_sensors, _inputs, _outputs, _lcd) \
44 static struct driver_interfacekit ph_##_sensors##_inputs##_outputs = { \
45 .sensors = _sensors, \
46 .inputs = _inputs, \
47 .outputs = _outputs, \
48 .has_lcd = _lcd, \
49 };
50 ifkit(0, 0, 4, 0);
51 ifkit(8, 8, 8, 0);
52 ifkit(0, 4, 7, 1);
53 ifkit(8, 8, 4, 0);
54 ifkit(0, 8, 8, 1);
55
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];
63 u8 lcd_files_on;
64
65 struct urb *irq;
66 unsigned char *data;
67 dma_addr_t data_dma;
68 };
69
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},
81 {}
82 };
83 MODULE_DEVICE_TABLE(usb, id_table);
84
85 static int change_outputs(struct phidget_interfacekit *kit, int output_num, int enable)
86 {
87 unsigned char *buffer;
88 int retval;
89 int n;
90
91 buffer = kmalloc(4, GFP_KERNEL);
92 if (!buffer) {
93 dev_err(&kit->udev->dev, "%s - out of memory\n",
94 __FUNCTION__);
95 return -ENOMEM;
96 }
97
98 kit->outputs[output_num] = enable;
99 memset(buffer, 0, 4);
100 for (n=0; n<8; n++) {
101 if (kit->outputs[n]) {
102 buffer[0] |= 1 << n;
103 }
104 }
105
106 dev_dbg(&kit->udev->dev, "data: %02x %02x\n", buffer[0], buffer[1]);
107
108 retval = usb_control_msg(kit->udev,
109 usb_sndctrlpipe(kit->udev, 0),
110 0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2 * HZ);
111
112 if (retval != 4)
113 dev_err(&kit->udev->dev, "retval = %d\n", retval);
114 kfree(buffer);
115
116 return retval < 0 ? retval : 0;
117 }
118
119 static int change_string(struct phidget_interfacekit *kit, const char *display, unsigned char row)
120 {
121 unsigned char *buffer;
122 unsigned char *form_buffer;
123 int retval = -ENOMEM;
124 int i,j, len, buf_ptr;
125
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__);
130 goto exit;
131 }
132
133 len = strlen(display);
134 if (len > 20)
135 len = 20;
136
137 dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
138
139 form_buffer[0] = row * 0x40 + 0x80;
140 form_buffer[1] = 0x02;
141 buf_ptr = 2;
142 for (i = 0; i<len; i++)
143 form_buffer[buf_ptr++] = display[i];
144
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);
149
150 for (i = 0; i < buf_ptr; i += 7) {
151 if ((buf_ptr - i) > 7)
152 len = 7;
153 else
154 len = (buf_ptr - i);
155 for (j = 0; j < len; j++)
156 buffer[j] = form_buffer[i + j];
157 buffer[7] = len;
158
159 retval = usb_control_msg(kit->udev,
160 usb_sndctrlpipe(kit->udev, 0),
161 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2 * HZ);
162 if (retval < 0)
163 goto exit;
164 }
165
166 retval = 0;
167 exit:
168 kfree(buffer);
169 kfree(form_buffer);
170
171 return retval;
172 }
173
174 #define set_lcd_line(number) \
175 static ssize_t lcd_line_##number(struct device *dev, const char *buf, size_t count) \
176 { \
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); \
180 return count; \
181 } \
182 static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
183 set_lcd_line(1);
184 set_lcd_line(2);
185
186 static ssize_t set_backlight(struct device *dev, const char *buf, size_t count)
187 {
188 struct usb_interface *intf = to_usb_interface(dev);
189 struct phidget_interfacekit *kit = usb_get_intfdata(intf);
190 int enabled;
191 unsigned char *buffer;
192 int retval = -ENOMEM;
193
194 buffer = kmalloc(8, GFP_KERNEL);
195 if (!buffer) {
196 dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
197 goto exit;
198 }
199
200 if (sscanf(buf, "%d", &enabled) < 1) {
201 retval = -EINVAL;
202 goto exit;
203 }
204 memset(buffer, 0x00, 8);
205 if (enabled)
206 buffer[0] = 0x01;
207 buffer[7] = 0x11;
208
209 dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
210
211 retval = usb_control_msg(kit->udev,
212 usb_sndctrlpipe(kit->udev, 0),
213 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2 * HZ);
214 if (retval < 0)
215 goto exit;
216
217 retval = count;
218 exit:
219 kfree(buffer);
220 return retval;
221 }
222 static DEVICE_ATTR(backlight, S_IWUGO, NULL, set_backlight);
223
224 static void remove_lcd_files(struct phidget_interfacekit *kit)
225 {
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);
231 }
232 }
233
234 static ssize_t enable_lcd_files(struct device *dev, const char *buf, size_t count)
235 {
236 struct usb_interface *intf = to_usb_interface(dev);
237 struct phidget_interfacekit *kit = usb_get_intfdata(intf);
238 int enable;
239
240 if (kit->ifkit->has_lcd == 0)
241 return -ENODEV;
242
243 if (sscanf(buf, "%d", &enable) < 1)
244 return -EINVAL;
245
246 if (enable) {
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;
253 }
254 } else {
255 if (kit->lcd_files_on) {
256 remove_lcd_files(kit);
257 kit->lcd_files_on = 0;
258 }
259 }
260
261 return count;
262 }
263 static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
264
265 static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
266 {
267 struct phidget_interfacekit *kit = urb->context;
268 unsigned char *buffer = kit->data;
269 int status;
270 int n;
271
272 switch (urb->status) {
273 case 0: /* success */
274 break;
275 case -ECONNRESET: /* unlink */
276 case -ENOENT:
277 case -ESHUTDOWN:
278 return;
279 /* -EPIPE: should clear the halt */
280 default: /* error */
281 goto resubmit;
282 }
283
284 for (n=0; n<8; n++) {
285 kit->inputs[n] = buffer[1] & (1 << n) ? 1 : 0;
286 }
287
288 if (buffer[0] & 1) {
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;
293 } else {
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;
298 }
299
300 resubmit:
301 status = usb_submit_urb(urb, SLAB_ATOMIC);
302 if (status)
303 err("can't resubmit intr, %s-%s/interfacekit0, status %d",
304 kit->udev->bus->bus_name,
305 kit->udev->devpath, status);
306 }
307
308 #define show_set_output(value) \
309 static ssize_t set_output##value(struct device *dev, const char *buf, \
310 size_t count) \
311 { \
312 struct usb_interface *intf = to_usb_interface(dev); \
313 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
314 int enabled; \
315 int retval; \
316 \
317 if (sscanf(buf, "%d", &enabled) < 1) { \
318 return -EINVAL; \
319 } \
320 \
321 retval = change_outputs(kit, value - 1, enabled ? 1 : 0); \
322 \
323 return retval ? retval : count; \
324 } \
325 \
326 static ssize_t show_output##value(struct device *dev, char *buf) \
327 { \
328 struct usb_interface *intf = to_usb_interface(dev); \
329 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
330 \
331 return sprintf(buf, "%d\n", kit->outputs[value - 1 ]); \
332 } \
333 static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO, \
334 show_output##value, set_output##value);
335 show_set_output(1);
336 show_set_output(2);
337 show_set_output(3);
338 show_set_output(4);
339 show_set_output(5);
340 show_set_output(6);
341 show_set_output(7);
342 show_set_output(8); /* should be MAX_INTERFACES - 1 */
343
344 #define show_input(value) \
345 static ssize_t show_input##value(struct device *dev, char *buf) \
346 { \
347 struct usb_interface *intf = to_usb_interface(dev); \
348 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
349 \
350 return sprintf(buf, "%d\n", kit->inputs[value - 1]); \
351 } \
352 static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
353
354 show_input(1);
355 show_input(2);
356 show_input(3);
357 show_input(4);
358 show_input(5);
359 show_input(6);
360 show_input(7);
361 show_input(8); /* should be MAX_INTERFACES - 1 */
362
363 #define show_sensor(value) \
364 static ssize_t show_sensor##value(struct device *dev, char *buf) \
365 { \
366 struct usb_interface *intf = to_usb_interface(dev); \
367 struct phidget_interfacekit *kit = usb_get_intfdata(intf); \
368 \
369 return sprintf(buf, "%d\n", kit->sensors[value - 1]); \
370 } \
371 static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
372
373 show_sensor(1);
374 show_sensor(2);
375 show_sensor(3);
376 show_sensor(4);
377 show_sensor(5);
378 show_sensor(6);
379 show_sensor(7);
380 show_sensor(8); /* should be MAX_INTERFACES - 1 */
381
382 static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
383 {
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;
389 int pipe, maxp;
390
391 ifkit = (struct driver_interfacekit *)id->driver_info;
392 if (!ifkit)
393 return -ENODEV;
394
395 interface = intf->cur_altsetting;
396 if (interface->desc.bNumEndpoints != 1)
397 return -ENODEV;
398
399 endpoint = &interface->endpoint[0].desc;
400 if (!(endpoint->bEndpointAddress & 0x80))
401 return -ENODEV;
402 /*
403 * bmAttributes
404 */
405 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
406 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
407
408 kit = kmalloc(sizeof(*kit), GFP_KERNEL);
409 if (kit == NULL) {
410 dev_err(&intf->dev, "%s - out of memory\n", __FUNCTION__);
411 return -ENOMEM;
412 }
413 memset(kit, 0, sizeof(*kit));
414 kit->ifkit = ifkit;
415
416 kit->data = usb_buffer_alloc(dev, 8, SLAB_ATOMIC, &kit->data_dma);
417 if (!kit->data) {
418 kfree(kit);
419 return -ENOMEM;
420 }
421
422 kit->irq = usb_alloc_urb(0, GFP_KERNEL);
423 if (!kit->irq) {
424 usb_buffer_free(dev, 8, kit->data, kit->data_dma);
425 kfree(kit);
426 return -ENOMEM;
427 }
428
429 kit->udev = usb_get_dev(dev);
430 kit->intf = intf;
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;
436
437 usb_set_intfdata(intf, kit);
438
439 if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
440 return -EIO;
441 }
442
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);
452 }
453
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);
459 }
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);
465 }
466
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);
472 }
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);
477 }
478 if (ifkit->sensors == 8) {
479 device_create_file(&intf->dev, &dev_attr_sensor8);
480 }
481
482 if (ifkit->has_lcd)
483 device_create_file(&intf->dev, &dev_attr_lcd);
484
485 dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
486 ifkit->inputs, ifkit->outputs, ifkit->sensors);
487
488 return 0;
489 }
490
491 static void interfacekit_disconnect(struct usb_interface *interface)
492 {
493 struct phidget_interfacekit *kit;
494
495 kit = usb_get_intfdata(interface);
496 usb_set_intfdata(interface, NULL);
497 if (!kit)
498 return;
499
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);
509 }
510
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);
516 }
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);
522 }
523
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);
529 }
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);
534 }
535 if (kit->ifkit->sensors == 8) {
536 device_remove_file(&interface->dev, &dev_attr_sensor8);
537 }
538 if (kit->ifkit->has_lcd)
539 device_create_file(&interface->dev, &dev_attr_lcd);
540
541 dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
542 kit->ifkit->inputs, kit->ifkit->outputs, kit->ifkit->sensors);
543
544 usb_kill_urb(kit->irq);
545 usb_free_urb(kit->irq);
546 usb_buffer_free(kit->udev, 8, kit->data, kit->data_dma);
547
548 usb_put_dev(kit->udev);
549 kfree(kit);
550 }
551
552 static struct usb_driver interfacekit_driver = {
553 .owner = THIS_MODULE,
554 .name = "phidgetkit",
555 .probe = interfacekit_probe,
556 .disconnect = interfacekit_disconnect,
557 .id_table = id_table
558 };
559
560 static int __init interfacekit_init(void)
561 {
562 int retval = 0;
563
564 retval = usb_register(&interfacekit_driver);
565 if (retval)
566 err("usb_register failed. Error number %d", retval);
567
568 return retval;
569 }
570
571 static void __exit interfacekit_exit(void)
572 {
573 usb_deregister(&interfacekit_driver);
574 }
575
576 module_init(interfacekit_init);
577 module_exit(interfacekit_exit);
578
579 MODULE_AUTHOR(DRIVER_AUTHOR);
580 MODULE_DESCRIPTION(DRIVER_DESC);
581 MODULE_LICENSE("GPL");
582
|
This page was automatically generated by the
LXR engine.
|