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: linux/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/miscdevice.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <asm/uaccess.h>
33 #include <linux/delay.h>
34 #include <linux/usb.h>
35 #include <linux/smp_lock.h>
36 #include <linux/devfs_fs_kernel.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 */
54 static devfs_handle_t devfs_handle;
56 /*---------- misc functions ------------------------------------------- */
59 * Re-initialize device
62 clear_device (struct usb_device *dev)
64 if (usb_set_configuration (dev, dev->config[0].bConfigurationValue) < 0) {
65 err ("clear_device failed");
73 * Clear input & output pipes (endpoints)
76 clear_pipes (struct usb_device *dev)
80 pipe = usb_sndbulkpipe (dev, 2);
81 if (usb_clear_halt (dev, pipe)) {
82 err ("clear_pipe (w), request failed");
86 pipe = usb_rcvbulkpipe (dev, 1);
87 if (usb_clear_halt (dev, pipe)) {
88 err ("clear_pipe (r), request failed");
95 /* ----- file operations functions--------------------------------------- */
98 tiglusb_open (struct inode *inode, struct file *filp)
100 int devnum = minor (inode->i_rdev);
103 if (devnum < TIUSB_MINOR || devnum >= (TIUSB_MINOR + MAXTIGL))
106 s = &tiglusb[devnum - TIUSB_MINOR];
108 if (down_interruptible (&s->mutex)) {
112 while (!s->dev || s->opened) {
115 if (filp->f_flags & O_NONBLOCK) {
119 schedule_timeout (HZ / 2);
121 if (signal_pending (current)) {
125 if (down_interruptible (&s->mutex)) {
134 filp->private_data = s;
140 tiglusb_release (struct inode *inode, struct file *filp)
142 ptiglusb_t s = (ptiglusb_t) filp->private_data;
144 if (down_interruptible (&s->mutex)) {
151 if (!s->remove_pending)
152 clear_device (s->dev);
154 wake_up (&s->remove_ok);
162 tiglusb_read (struct file *filp, char *buf, size_t count, loff_t * f_pos)
164 ptiglusb_t s = (ptiglusb_t) filp->private_data;
166 int bytes_to_read = 0;
169 char buffer[BULK_RCV_MAX];
175 if (s->remove_pending)
181 bytes_to_read = (count >= BULK_RCV_MAX) ? BULK_RCV_MAX : count;
183 pipe = usb_rcvbulkpipe (s->dev, 1);
184 result = usb_bulk_msg (s->dev, pipe, buffer, bytes_to_read,
185 &bytes_read, HZ * 10 / timeout);
186 if (result == -ETIMEDOUT) { /* NAK */
189 warn ("tiglusb_read, NAK received.");
192 } else if (result == -EPIPE) { /* STALL -- shouldn't happen */
193 warn ("clear_halt request to remove STALL condition.");
194 if (usb_clear_halt (s->dev, pipe))
195 err ("clear_halt, request failed");
196 clear_device (s->dev);
199 } else if (result < 0) { /* We should not get any I/O errors */
200 err ("funky result: %d. Please notify maintainer.", result);
205 if (copy_to_user (buf, buffer, bytes_read)) {
210 return ret ? ret : bytes_read;
214 tiglusb_write (struct file *filp, const char *buf, size_t count, loff_t * f_pos)
216 ptiglusb_t s = (ptiglusb_t) filp->private_data;
218 int bytes_to_write = 0;
219 int bytes_written = 0;
221 char buffer[BULK_SND_MAX];
227 if (s->remove_pending)
233 bytes_to_write = (count >= BULK_SND_MAX) ? BULK_SND_MAX : count;
234 if (copy_from_user (buffer, buf, bytes_to_write)) {
239 pipe = usb_sndbulkpipe (s->dev, 2);
240 result = usb_bulk_msg (s->dev, pipe, buffer, bytes_to_write,
241 &bytes_written, HZ * 10 / timeout);
243 if (result == -ETIMEDOUT) { /* NAK */
244 warn ("tiglusb_write, NAK received.");
247 } else if (result == -EPIPE) { /* STALL -- shouldn't happen */
248 warn ("clear_halt request to remove STALL condition.");
249 if (usb_clear_halt (s->dev, pipe))
250 err ("clear_halt, request failed");
251 clear_device (s->dev);
254 } else if (result < 0) { /* We should not get any I/O errors */
255 warn ("funky result: %d. Please notify maintainer.", result);
260 if (bytes_written != bytes_to_write) {
265 return ret ? ret : bytes_written;
269 tiglusb_ioctl (struct inode *inode, struct file *filp,
270 unsigned int cmd, unsigned long arg)
272 ptiglusb_t s = (ptiglusb_t) filp->private_data;
275 if (s->remove_pending)
278 if (down_interruptible (&s->mutex)) {
288 case IOCTL_TIUSB_TIMEOUT:
294 case IOCTL_TIUSB_RESET_DEVICE:
295 if (clear_device (s->dev))
298 case IOCTL_TIUSB_RESET_PIPES:
299 if (clear_pipes (s->dev))
312 /* ----- kernel module registering ------------------------------------ */
314 static struct file_operations tiglusb_fops = {
315 .owner = THIS_MODULE,
317 .read = tiglusb_read,
318 .write = tiglusb_write,
319 .ioctl = tiglusb_ioctl,
320 .open = tiglusb_open,
321 .release = tiglusb_release,
324 /* --- initialisation code ------------------------------------- */
327 tiglusb_probe (struct usb_device *dev, unsigned int ifnum,
328 const struct usb_device_id *id)
335 dbg ("probing vendor id 0x%x, device id 0x%x ifnum:%d",
336 dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
339 * We don't handle multiple configurations. As of version 0x0103 of
340 * the TIGL hardware, there's only 1 configuration.
343 if (dev->descriptor.bNumConfigurations != 1)
346 if ((dev->descriptor.idProduct != 0xe001)
347 && (dev->descriptor.idVendor != 0x451))
350 if (usb_set_configuration (dev, dev->config[0].bConfigurationValue) < 0) {
351 err ("tiglusb_probe: set_configuration failed");
356 * Find a tiglusb struct
358 for (i = 0; i < MAXTIGL; i++) {
359 ptiglusb_t s = &tiglusb[i];
372 s->remove_pending = 0;
375 dbg ("bound to interface: %d", ifnum);
377 sprintf (name, "%d", s->minor);
378 dbg ("registering to devfs : major = %d, minor = %d, node = %s",
379 TIUSB_MAJOR, (TIUSB_MINOR + s->minor), name);
381 devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT, TIUSB_MAJOR,
382 TIUSB_MINOR + s->minor, S_IFCHR | S_IRUGO | S_IWUGO,
383 &tiglusb_fops, NULL);
385 /* Display firmware version */
386 info ("firmware revision %i.%02x",
387 dev->descriptor.bcdDevice >> 8,
388 dev->descriptor.bcdDevice & 0xff);
394 tiglusb_disconnect (struct usb_device *dev, void *drv_context)
396 ptiglusb_t s = (ptiglusb_t) drv_context;
399 info ("bogus disconnect");
401 s->remove_pending = 1;
403 if (s->state == _started)
404 sleep_on (&s->remove_ok);
409 devfs_unregister (s->devfs);
412 info ("device %d removed", s->minor);
417 static struct usb_device_id tiglusb_ids[] = {
418 {USB_DEVICE (0x0451, 0xe001)},
422 MODULE_DEVICE_TABLE (usb, tiglusb_ids);
424 static struct usb_driver tiglusb_driver = {
425 .owner = THIS_MODULE,
427 .probe = tiglusb_probe,
428 .disconnect = tiglusb_disconnect,
429 .id_table = tiglusb_ids,
432 /* --- initialisation code ------------------------------------- */
436 * You can use 'tiusb=timeout' to set timeout.
439 tiglusb_setup (char *str)
443 str = get_options (str, ARRAY_SIZE (ints), ints);
449 info ("tiglusb: wrong timeout value (0), using default value.");
462 /* initialize struct */
463 for (u = 0; u < MAXTIGL; u++) {
464 ptiglusb_t s = &tiglusb[u];
465 memset (s, 0, sizeof (tiglusb_t));
466 init_MUTEX (&s->mutex);
470 init_waitqueue_head (&s->wait);
471 init_waitqueue_head (&s->remove_ok);
474 /* register device */
475 if (register_chrdev (TIUSB_MAJOR, "tiglusb", &tiglusb_fops)) {
476 err ("unable to get major %d", TIUSB_MAJOR);
480 /* Use devfs, tree: /dev/ticables/usb/[0..3] */
481 devfs_handle = devfs_mk_dir (NULL, "ticables/usb", NULL);
483 /* register USB module */
484 result = usb_register (&tiglusb_driver);
486 unregister_chrdev (TIUSB_MAJOR, "tiglusb");
490 info (DRIVER_DESC ", version " DRIVER_VERSION);
496 tiglusb_cleanup (void)
498 usb_deregister (&tiglusb_driver);
499 devfs_unregister (devfs_handle);
500 unregister_chrdev (TIUSB_MAJOR, "tiglusb");
503 /* --------------------------------------------------------------------- */
505 __setup ("tiusb=", tiglusb_setup);
506 module_init (tiglusb_init);
507 module_exit (tiglusb_cleanup);
509 MODULE_AUTHOR (DRIVER_AUTHOR);
510 MODULE_DESCRIPTION (DRIVER_DESC);
511 MODULE_LICENSE (DRIVER_LICENSE);
513 MODULE_PARM (timeout, "i");
514 MODULE_PARM_DESC (timeout, "Timeout in tenths of seconds (default=1.5 seconds)");
516 /* --------------------------------------------------------------------- */