1 /* Hey EMACS -*- linux-c -*-
3 * tiglusb -- Texas Instruments' USB GraphLink (aka SilverLink) driver.
4 * Target: Texas Instruments graphing calculators (http://lpg.ticalc.org).
6 * Copyright (C) 2001-2004:
7 * Romain Lievin <roms@lpg.ticalc.org>
8 * Julien BLACHE <jb@technologeek.org>
9 * under the terms of the GNU General Public License.
11 * Based on dabusb.c, printer.c & scanner.c
13 * Please see the file: Documentation/usb/silverlink.txt
14 * and the website at: http://lpg.ticalc.org/prj_usb/
18 * 1.0x, Romain & Julien: initial submit.
19 * 1.03, Greg Kroah: modifications.
20 * 1.04, Julien: clean-up & fixes; Romain: 2.4 backport.
21 * 1.05, Randy Dunlap: bug fix with the timeout parameter (divide-by-zero).
22 * 1.06, Romain: synched with 2.5, version/firmware changed (confusing).
23 * 1.07, Romain: fixed bad use of usb_clear_halt (invalid argument);
24 * timeout argument checked in ioctl + clean-up.
27 #include <linux/module.h>
28 #include <linux/socket.h>
29 #include <linux/slab.h>
30 #include <linux/init.h>
31 #include <asm/uaccess.h>
32 #include <linux/delay.h>
33 #include <linux/usb.h>
34 #include <linux/smp_lock.h>
35 #include <linux/devfs_fs_kernel.h>
36 #include <linux/device.h>
38 #include <linux/ticable.h>
44 #define DRIVER_VERSION "1.07"
45 #define DRIVER_AUTHOR "Romain Lievin <roms@tilp.info> & Julien Blache <jb@jblache.org>"
46 #define DRIVER_DESC "TI-GRAPH LINK USB (aka SilverLink) driver"
47 #define DRIVER_LICENSE "GPL"
49 /* ----- global variables --------------------------------------------- */
51 static tiglusb_t tiglusb[MAXTIGL];
52 static int timeout = TIMAXTIME; /* timeout in tenth of seconds */
53 static struct class_simple *tiglusb_class;
55 /*---------- misc functions ------------------------------------------- */
58 * Re-initialize device
61 clear_device (struct usb_device *dev)
63 if (usb_reset_configuration (dev) < 0) {
64 err ("clear_device failed");
72 * Clear input & output pipes (endpoints)
75 clear_pipes (struct usb_device *dev)
79 pipe = usb_sndbulkpipe (dev, 2);
80 if (usb_clear_halt (dev, pipe)) {
81 err ("clear_pipe (w), request failed");
85 pipe = usb_rcvbulkpipe (dev, 1);
86 if (usb_clear_halt (dev, pipe)) {
87 err ("clear_pipe (r), request failed");
94 /* ----- file operations functions--------------------------------------- */
97 tiglusb_open (struct inode *inode, struct file *filp)
99 int devnum = iminor(inode);
102 if (devnum < TIUSB_MINOR || devnum >= (TIUSB_MINOR + MAXTIGL))
105 s = &tiglusb[devnum - TIUSB_MINOR];
107 if (down_interruptible (&s->mutex)) {
111 while (!s->dev || s->opened) {
114 if (filp->f_flags & O_NONBLOCK) {
118 schedule_timeout (HZ / 2);
120 if (signal_pending (current)) {
124 if (down_interruptible (&s->mutex)) {
133 filp->private_data = s;
135 return nonseekable_open(inode, filp);
139 tiglusb_release (struct inode *inode, struct file *filp)
141 ptiglusb_t s = (ptiglusb_t) filp->private_data;
143 if (down_interruptible (&s->mutex)) {
150 if (!s->remove_pending)
151 clear_device (s->dev);
153 wake_up (&s->remove_ok);
161 tiglusb_read (struct file *filp, char __user *buf, size_t count, loff_t * f_pos)
163 ptiglusb_t s = (ptiglusb_t) filp->private_data;
165 int bytes_to_read = 0;
174 if (s->remove_pending)
180 buffer = kmalloc(BULK_RCV_MAX, GFP_KERNEL);
184 bytes_to_read = (count >= BULK_RCV_MAX) ? BULK_RCV_MAX : count;
186 pipe = usb_rcvbulkpipe (s->dev, 1);
187 result = usb_bulk_msg (s->dev, pipe, buffer, bytes_to_read,
188 &bytes_read, (HZ * timeout) / 10);
189 if (result == -ETIMEDOUT) { /* NAK */
192 warn ("tiglusb_read, NAK received.");
195 } else if (result == -EPIPE) { /* STALL -- shouldn't happen */
196 warn ("clear_halt request to remove STALL condition.");
197 if (usb_clear_halt (s->dev, pipe))
198 err ("clear_halt, request failed");
199 clear_device (s->dev);
202 } else if (result < 0) { /* We should not get any I/O errors */
203 err ("funky result: %d. Please notify maintainer.", result);
208 if (copy_to_user (buf, buffer, bytes_read)) {
214 return ret ? ret : bytes_read;
218 tiglusb_write (struct file *filp, const char __user *buf, size_t count, loff_t * f_pos)
220 ptiglusb_t s = (ptiglusb_t) filp->private_data;
222 int bytes_to_write = 0;
223 int bytes_written = 0;
231 if (s->remove_pending)
237 buffer = kmalloc(BULK_SND_MAX, GFP_KERNEL);
241 bytes_to_write = (count >= BULK_SND_MAX) ? BULK_SND_MAX : count;
242 if (copy_from_user (buffer, buf, bytes_to_write)) {
247 pipe = usb_sndbulkpipe (s->dev, 2);
248 result = usb_bulk_msg (s->dev, pipe, buffer, bytes_to_write,
249 &bytes_written, (HZ * timeout) / 10);
251 if (result == -ETIMEDOUT) { /* NAK */
252 warn ("tiglusb_write, NAK received.");
255 } else if (result == -EPIPE) { /* STALL -- shouldn't happen */
256 warn ("clear_halt request to remove STALL condition.");
257 if (usb_clear_halt (s->dev, pipe))
258 err ("clear_halt, request failed");
259 clear_device (s->dev);
262 } else if (result < 0) { /* We should not get any I/O errors */
263 warn ("funky result: %d. Please notify maintainer.", result);
268 if (bytes_written != bytes_to_write) {
274 return ret ? ret : bytes_written;
278 tiglusb_ioctl (struct inode *inode, struct file *filp,
279 unsigned int cmd, unsigned long arg)
281 ptiglusb_t s = (ptiglusb_t) filp->private_data;
284 if (s->remove_pending)
287 if (down_interruptible (&s->mutex)) {
297 case IOCTL_TIUSB_TIMEOUT:
303 case IOCTL_TIUSB_RESET_DEVICE:
304 if (clear_device (s->dev))
307 case IOCTL_TIUSB_RESET_PIPES:
308 if (clear_pipes (s->dev))
321 /* ----- kernel module registering ------------------------------------ */
323 static struct file_operations tiglusb_fops = {
324 .owner = THIS_MODULE,
326 .read = tiglusb_read,
327 .write = tiglusb_write,
328 .ioctl = tiglusb_ioctl,
329 .open = tiglusb_open,
330 .release = tiglusb_release,
333 /* --- initialisation code ------------------------------------- */
336 tiglusb_probe (struct usb_interface *intf,
337 const struct usb_device_id *id)
339 struct usb_device *dev = interface_to_usbdev(intf);
344 dbg ("probing vendor id 0x%x, device id 0x%x",
345 dev->descriptor.idVendor, dev->descriptor.idProduct);
348 * We don't handle multiple configurations. As of version 0x0103 of
349 * the TIGL hardware, there's only 1 configuration.
352 if (dev->descriptor.bNumConfigurations != 1) {
357 if ((dev->descriptor.idProduct != 0xe001)
358 && (dev->descriptor.idVendor != 0x451)) {
363 // NOTE: it's already in this config, this shouldn't be needed.
364 // is this working around some hardware bug?
365 if (usb_reset_configuration (dev) < 0) {
366 err ("tiglusb_probe: reset_configuration failed");
372 * Find a tiglusb struct
374 for (i = 0; i < MAXTIGL; i++) {
375 ptiglusb_t s = &tiglusb[i];
390 s->remove_pending = 0;
393 dbg ("bound to interface");
395 class_simple_device_add(tiglusb_class, MKDEV(TIUSB_MAJOR, TIUSB_MINOR + s->minor),
396 NULL, "usb%d", s->minor);
397 err = devfs_mk_cdev(MKDEV(TIUSB_MAJOR, TIUSB_MINOR) + s->minor,
398 S_IFCHR | S_IRUGO | S_IWUGO,
399 "ticables/usb/%d", s->minor);
404 /* Display firmware version */
405 info ("firmware revision %i.%02x",
406 dev->descriptor.bcdDevice >> 8,
407 dev->descriptor.bcdDevice & 0xff);
409 usb_set_intfdata (intf, s);
414 class_simple_device_remove(MKDEV(TIUSB_MAJOR, TIUSB_MINOR + s->minor));
420 tiglusb_disconnect (struct usb_interface *intf)
423 ptiglusb_t s = usb_get_intfdata (intf);
425 init_waitqueue_entry(&__wait, current);
428 usb_set_intfdata (intf, NULL);
430 info ("bogus disconnect");
434 s->remove_pending = 1;
436 add_wait_queue(&s->wait, &__wait);
437 set_current_state(TASK_UNINTERRUPTIBLE);
438 if (s->state == _started)
440 current->state = TASK_RUNNING;
441 remove_wait_queue(&s->wait, &__wait);
446 class_simple_device_remove(MKDEV(TIUSB_MAJOR, TIUSB_MINOR + s->minor));
447 devfs_remove("ticables/usb/%d", s->minor);
449 info ("device %d removed", s->minor);
454 static struct usb_device_id tiglusb_ids[] = {
455 {USB_DEVICE (0x0451, 0xe001)},
459 MODULE_DEVICE_TABLE (usb, tiglusb_ids);
461 static struct usb_driver tiglusb_driver = {
462 .owner = THIS_MODULE,
464 .probe = tiglusb_probe,
465 .disconnect = tiglusb_disconnect,
466 .id_table = tiglusb_ids,
469 /* --- initialisation code ------------------------------------- */
473 * You can use 'tiusb=timeout' to set timeout.
476 tiglusb_setup (char *str)
480 str = get_options (str, ARRAY_SIZE (ints), ints);
486 info ("tiglusb: wrong timeout value (0), using default value.");
499 /* initialize struct */
500 for (u = 0; u < MAXTIGL; u++) {
501 ptiglusb_t s = &tiglusb[u];
502 memset (s, 0, sizeof (tiglusb_t));
503 init_MUTEX (&s->mutex);
507 init_waitqueue_head (&s->wait);
508 init_waitqueue_head (&s->remove_ok);
511 /* register device */
512 if (register_chrdev (TIUSB_MAJOR, "tiglusb", &tiglusb_fops)) {
513 err ("unable to get major %d", TIUSB_MAJOR);
518 /* Use devfs, tree: /dev/ticables/usb/[0..3] */
519 devfs_mk_dir ("ticables/usb");
521 tiglusb_class = class_simple_create(THIS_MODULE, "tiglusb");
522 if (IS_ERR(tiglusb_class)) {
523 err = PTR_ERR(tiglusb_class);
526 /* register USB module */
527 result = usb_register (&tiglusb_driver);
533 info (DRIVER_DESC ", version " DRIVER_VERSION);
539 unregister_chrdev (TIUSB_MAJOR, "tiglusb");
545 tiglusb_cleanup (void)
547 usb_deregister (&tiglusb_driver);
548 class_simple_destroy(tiglusb_class);
549 devfs_remove("ticables/usb");
550 unregister_chrdev (TIUSB_MAJOR, "tiglusb");
553 /* --------------------------------------------------------------------- */
555 __setup ("tiusb=", tiglusb_setup);
556 module_init (tiglusb_init);
557 module_exit (tiglusb_cleanup);
559 MODULE_AUTHOR (DRIVER_AUTHOR);
560 MODULE_DESCRIPTION (DRIVER_DESC);
561 MODULE_LICENSE (DRIVER_LICENSE);
563 module_param(timeout, int, 0);
564 MODULE_PARM_DESC (timeout, "Timeout in tenths of seconds (default=1.5 seconds)");
566 /* --------------------------------------------------------------------- */