Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | /* Hey EMACS -*- linux-c -*- * * tiglusb -- Texas Instruments' USB GraphLink (aka SilverLink) driver. * Target: Texas Instruments graphing calculators (http://lpg.ticalc.org). * * Copyright (C) 2001-2002: * Romain Lievin <roms@lpg.ticalc.org> * Julien BLACHE <jb@technologeek.org> * under the terms of the GNU General Public License. * * Based on dabusb.c, printer.c & scanner.c * * Please see the file: linux/Documentation/usb/SilverLink.txt * and the website at: http://lpg.ticalc.org/prj_usb/ * for more info. * */ #include <linux/module.h> #include <linux/socket.h> #include <linux/miscdevice.h> #include <linux/slab.h> #include <linux/init.h> #include <asm/uaccess.h> #include <linux/delay.h> #include <linux/usb.h> #include <linux/smp_lock.h> #include <linux/devfs_fs_kernel.h> #include <linux/ticable.h> #include "tiglusb.h" /* * Version Information */ #define DRIVER_VERSION "1.03" #define DRIVER_AUTHOR "Romain Lievin <roms@lpg.ticalc.org> & Julien Blache <jb@jblache.org>" #define DRIVER_DESC "TI-GRAPH LINK USB (aka SilverLink) driver" #define DRIVER_LICENSE "GPL" /* ----- global variables --------------------------------------------- */ static tiglusb_t tiglusb[MAXTIGL]; static int timeout = TIMAXTIME; /* timeout in tenth of seconds */ static devfs_handle_t devfs_handle; /*---------- misc functions ------------------------------------------- */ /* Unregister device */ static void usblp_cleanup (tiglusb_t * s) { devfs_unregister (s->devfs); //memset(tiglusb[s->minor], 0, sizeof(tiglusb_t)); info ("tiglusb%d removed", s->minor); } /* Re-initialize device */ static int clear_device (struct usb_device *dev) { if (usb_set_configuration (dev, dev->config[0].bConfigurationValue) < 0) { printk ("tiglusb: clear_device failed\n"); return -1; } return 0; } /* Clear input & output pipes (endpoints) */ static int clear_pipes (struct usb_device *dev) { unsigned int pipe; pipe = usb_sndbulkpipe (dev, 1); if (usb_clear_halt (dev, usb_pipeendpoint (pipe))) { printk ("tiglusb: clear_pipe (r), request failed\n"); return -1; } pipe = usb_sndbulkpipe (dev, 2); if (usb_clear_halt (dev, usb_pipeendpoint (pipe))) { printk ("tiglusb: clear_pipe (w), request failed\n"); return -1; } return 0; } /* ----- kernel module functions--------------------------------------- */ static int tiglusb_open (struct inode *inode, struct file *file) { int devnum = minor (inode->i_rdev); ptiglusb_t s; if (devnum < TIUSB_MINOR || devnum >= (TIUSB_MINOR + MAXTIGL)) return -EIO; s = &tiglusb[devnum - TIUSB_MINOR]; down (&s->mutex); while (!s->dev || s->opened) { up (&s->mutex); if (file->f_flags & O_NONBLOCK) { return -EBUSY; } schedule_timeout (HZ / 2); if (signal_pending (current)) { return -EAGAIN; } down (&s->mutex); } s->opened = 1; up (&s->mutex); file->f_pos = 0; file->private_data = s; return 0; } static int tiglusb_release (struct inode *inode, struct file *file) { ptiglusb_t s = (ptiglusb_t) file->private_data; lock_kernel (); down (&s->mutex); s->state = _stopped; up (&s->mutex); if (!s->remove_pending) clear_device (s->dev); else wake_up (&s->remove_ok); s->opened = 0; unlock_kernel (); return 0; } static ssize_t tiglusb_read (struct file *file, char *buf, size_t count, loff_t * ppos) { ptiglusb_t s = (ptiglusb_t) file->private_data; ssize_t ret = 0; int bytes_to_read = 0; int bytes_read = 0; int result = 0; char buffer[BULK_RCV_MAX]; unsigned int pipe; if (*ppos) return -ESPIPE; if (s->remove_pending) return -EIO; if (!s->dev) return -EIO; bytes_to_read = (count >= BULK_RCV_MAX) ? BULK_RCV_MAX : count; pipe = usb_rcvbulkpipe (s->dev, 1); result = usb_bulk_msg (s->dev, pipe, buffer, bytes_to_read, &bytes_read, HZ / (timeout / 10)); if (result == -ETIMEDOUT) { /* NAK */ ret = result; if (!bytes_read) { printk ("quirk !\n"); } warn ("tiglusb_read, NAK received."); goto out; } else if (result == -EPIPE) { /* STALL -- shouldn't happen */ warn ("CLEAR_FEATURE request to remove STALL condition.\n"); if (usb_clear_halt (s->dev, usb_pipeendpoint (pipe))) warn ("send_packet, request failed\n"); //clear_device(s->dev); ret = result; goto out; } else if (result < 0) { /* We should not get any I/O errors */ warn ("funky result: %d. Please notify maintainer.", result); ret = -EIO; goto out; } if (copy_to_user (buf, buffer, bytes_read)) { ret = -EFAULT; goto out; } out: return ret ? ret : bytes_read; } static ssize_t tiglusb_write (struct file *file, const char *buf, size_t count, loff_t * ppos) { ptiglusb_t s = (ptiglusb_t) file->private_data; ssize_t ret = 0; int bytes_to_write = 0; int bytes_written = 0; int result = 0; char buffer[BULK_SND_MAX]; unsigned int pipe; if (*ppos) return -ESPIPE; if (s->remove_pending) return -EIO; if (!s->dev) return -EIO; bytes_to_write = (count >= BULK_SND_MAX) ? BULK_SND_MAX : count; if (copy_from_user (buffer, buf, bytes_to_write)) { ret = -EFAULT; goto out; } pipe = usb_sndbulkpipe (s->dev, 2); result = usb_bulk_msg (s->dev, pipe, buffer, bytes_to_write, &bytes_written, HZ / (timeout / 10)); if (result == -ETIMEDOUT) { /* NAK */ warn ("tiglusb_write, NAK received."); ret = result; goto out; } else if (result == -EPIPE) { /* STALL -- shouldn't happen */ warn ("CLEAR_FEATURE request to remove STALL condition."); if (usb_clear_halt (s->dev, usb_pipeendpoint (pipe))) warn ("send_packet, request failed\n"); //clear_device(s->dev); ret = result; goto out; } else if (result < 0) { /* We should not get any I/O errors */ warn ("funky result: %d. Please notify maintainer.", result); ret = -EIO; goto out; } if (bytes_written != bytes_to_write) { ret = -EIO; goto out; } out: return ret ? ret : bytes_written; } static int tiglusb_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { ptiglusb_t s = (ptiglusb_t) file->private_data; int ret = 0; if (s->remove_pending) return -EIO; down (&s->mutex); if (!s->dev) { up (&s->mutex); return -EIO; } switch (cmd) { case IOCTL_TIUSB_TIMEOUT: timeout = arg; // timeout value in tenth of seconds break; case IOCTL_TIUSB_RESET_DEVICE: printk (KERN_DEBUG "IOCTL_TIGLUSB_RESET_DEVICE\n"); if (clear_device (s->dev)) ret = -EIO; break; case IOCTL_TIUSB_RESET_PIPES: printk (KERN_DEBUG "IOCTL_TIGLUSB_RESET_PIPES\n"); if (clear_pipes (s->dev)) ret = -EIO; break; default: ret = -ENOTTY; break; } up (&s->mutex); return ret; } /* ----- kernel module registering ------------------------------------ */ static struct file_operations tiglusb_fops = { owner: THIS_MODULE, llseek: no_llseek, read: tiglusb_read, write: tiglusb_write, ioctl: tiglusb_ioctl, open: tiglusb_open, release: tiglusb_release, }; static int tiglusb_find_struct (void) { int u; for (u = 0; u < MAXTIGL; u++) { ptiglusb_t s = &tiglusb[u]; if (!s->dev) return u; } return -1; } /* --- initialisation code ------------------------------------- */ static void *tiglusb_probe (struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id) { int minor; ptiglusb_t s; char name[8]; printk ("tiglusb: probing vendor id 0x%x, device id 0x%x ifnum:%d\n", dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum); /* * We don't handle multiple configurations. As of version 0x0103 of * the TIGL hardware, there's only 1 configuration. */ if (dev->descriptor.bNumConfigurations != 1) return NULL; if ((dev->descriptor.idProduct != 0xe001) && (dev->descriptor.idVendor != 0x451)) return NULL; if (usb_set_configuration (dev, dev->config[0].bConfigurationValue) < 0) { printk ("tiglusb_probe: set_configuration failed\n"); return NULL; } minor = tiglusb_find_struct (); if (minor == -1) return NULL; s = &tiglusb[minor]; down (&s->mutex); s->remove_pending = 0; s->dev = dev; up (&s->mutex); dbg ("bound to interface: %d", ifnum); sprintf (name, "%d", s->minor); printk ("tiglusb: registering to devfs : major = %d, minor = %d, node = %s\n", TIUSB_MAJOR, (TIUSB_MINOR + s->minor), name); s->devfs = devfs_register (devfs_handle, name, DEVFS_FL_DEFAULT, TIUSB_MAJOR, TIUSB_MINOR + s->minor, S_IFCHR | S_IRUGO | S_IWUGO, &tiglusb_fops, NULL); /* Display firmware version */ printk ("tiglusb: link cable version %i.%02x\n", dev->descriptor.bcdDevice >> 8, dev->descriptor.bcdDevice & 0xff); return s; } static void tiglusb_disconnect (struct usb_device *dev, void *drv_context) { ptiglusb_t s = (ptiglusb_t) drv_context; if (!s || !s->dev) printk ("bogus disconnect"); s->remove_pending = 1; wake_up (&s->wait); if (s->state == _started) sleep_on (&s->remove_ok); down (&s->mutex); s->dev = NULL; s->opened = 0; /* cleanup now or later, on close */ if (!s->opened) usblp_cleanup (s); else up (&s->mutex); /* unregister device */ devfs_unregister (s->devfs); s->devfs = NULL; printk ("tiglusb: device disconnected\n"); } static struct usb_device_id tiglusb_ids[] = { {USB_DEVICE (0x0451, 0xe001)}, {} }; MODULE_DEVICE_TABLE (usb, tiglusb_ids); static struct usb_driver tiglusb_driver = { owner: THIS_MODULE, name: "tiglusb", probe: tiglusb_probe, disconnect: tiglusb_disconnect, id_table: tiglusb_ids, }; /* --- initialisation code ------------------------------------- */ #ifndef MODULE /* You must set these - there is no sane way to probe for this cable. * You can use 'tipar=timeout,delay' to set these now. */ static int __init tiglusb_setup (char *str) { int ints[2]; str = get_options (str, ARRAY_SIZE (ints), ints); if (ints[0] > 0) { timeout = ints[1]; } return 1; } #endif static int __init tiglusb_init (void) { unsigned u; int result; /* initialize struct */ for (u = 0; u < MAXTIGL; u++) { ptiglusb_t s = &tiglusb[u]; memset (s, 0, sizeof (tiglusb_t)); init_MUTEX (&s->mutex); s->dev = NULL; s->minor = u; s->opened = 0; init_waitqueue_head (&s->wait); init_waitqueue_head (&s->remove_ok); } /* register device */ if (devfs_register_chrdev (TIUSB_MAJOR, "tiglusb", &tiglusb_fops)) { printk ("tiglusb: unable to get major %d\n", TIUSB_MAJOR); return -EIO; } /* Use devfs, tree: /dev/ticables/usb/[0..3] */ devfs_handle = devfs_mk_dir (NULL, "ticables/usb", NULL); /* register USB module */ result = usb_register (&tiglusb_driver); if (result < 0) { devfs_unregister_chrdev (TIUSB_MAJOR, "tiglusb"); return -1; } info (DRIVER_DESC ", " DRIVER_VERSION); return 0; } static void __exit tiglusb_cleanup (void) { usb_deregister (&tiglusb_driver); devfs_unregister (devfs_handle); devfs_unregister_chrdev (TIUSB_MAJOR, "tiglusb"); } /* --------------------------------------------------------------------- */ __setup ("tiusb=", tiglusb_setup); module_init (tiglusb_init); module_exit (tiglusb_cleanup); MODULE_AUTHOR (DRIVER_AUTHOR); MODULE_DESCRIPTION (DRIVER_DESC); MODULE_LICENSE (DRIVER_LICENSE); MODULE_PARM (timeout, "i"); MODULE_PARM_DESC (timeout, "Timeout (default=1.5 seconds)"); /* --------------------------------------------------------------------- */ |