usbserial.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:45k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * USB Serial Converter driver
  3.  *
  4.  * Copyright (C) 1999 - 2001 Greg Kroah-Hartman (greg@kroah.com)
  5.  * Copyright (c) 2000 Peter Berger (pberger@brimson.com)
  6.  * Copyright (c) 2000 Al Borchers (borchers@steinerpoint.com)
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This driver was originally based on the ACM driver by Armin Fuerst (which was 
  14.  * based on a driver by Brad Keryan)
  15.  *
  16.  * See Documentation/usb/usb-serial.txt for more information on using this driver
  17.  *
  18.  * (10/10/2001) gkh
  19.  * usb_serial_disconnect() now sets the serial->dev pointer is to NULL to
  20.  * help prevent child drivers from accessing the device since it is now
  21.  * gone.
  22.  *
  23.  * (09/13/2001) gkh
  24.  * Moved generic driver initialize after we have registered with the USB
  25.  * core.  Thanks to Randy Dunlap for pointing this problem out.
  26.  *
  27.  * (07/03/2001) gkh
  28.  * Fixed module paramater size.  Thanks to John Brockmeyer for the pointer.
  29.  * Fixed vendor and product getting defined through the MODULE_PARM macro
  30.  * if the Generic driver wasn't compiled in.
  31.  * Fixed problem with generic_shutdown() not being called for drivers that
  32.  * don't have a shutdown() function.
  33.  *
  34.  * (06/06/2001) gkh
  35.  * added evil hack that is needed for the prolific pl2303 device due to the
  36.  * crazy way its endpoints are set up.
  37.  *
  38.  * (05/30/2001) gkh
  39.  * switched from using spinlock to a semaphore, which fixes lots of problems.
  40.  *
  41.  * (04/08/2001) gb
  42.  * Identify version on module load.
  43.  *
  44.  * 2001_02_05 gkh
  45.  * Fixed buffer overflows bug with the generic serial driver.  Thanks to
  46.  * Todd Squires <squirest@ct0.com> for fixing this.
  47.  *
  48.  * (01/10/2001) gkh
  49.  * Fixed bug where the generic serial adaptor grabbed _any_ device that was
  50.  * offered to it.
  51.  *
  52.  * (12/12/2000) gkh
  53.  * Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
  54.  * moved them to the serial_open and serial_close functions.
  55.  * Also fixed bug with there not being a MOD_DEC for the generic driver
  56.  * (thanks to Gary Brubaker for finding this.)
  57.  *
  58.  * (11/29/2000) gkh
  59.  * Small NULL pointer initialization cleanup which saves a bit of disk image
  60.  *
  61.  * (11/01/2000) Adam J. Richter
  62.  * instead of using idVendor/idProduct pairs, usb serial drivers
  63.  * now identify their hardware interest with usb_device_id tables,
  64.  * which they usually have anyhow for use with MODULE_DEVICE_TABLE.
  65.  *
  66.  * (10/05/2000) gkh
  67.  * Fixed bug with urb->dev not being set properly, now that the usb
  68.  * core needs it.
  69.  * 
  70.  * (09/11/2000) gkh
  71.  * Removed DEBUG #ifdefs with call to usb_serial_debug_data
  72.  *
  73.  * (08/28/2000) gkh
  74.  * Added port_lock to port structure.
  75.  * Added locks for SMP safeness to generic driver
  76.  * Fixed the ability to open a generic device's port more than once.
  77.  *
  78.  * (07/23/2000) gkh
  79.  * Added bulk_out_endpointAddress to port structure.
  80.  *
  81.  * (07/19/2000) gkh, pberger, and borchers
  82.  * Modifications to allow usb-serial drivers to be modules.
  83.  *
  84.  * (07/03/2000) gkh
  85.  * Added more debugging to serial_ioctl call
  86.  * 
  87.  * (06/25/2000) gkh
  88.  * Changed generic_write_bulk_callback to not call wake_up_interruptible
  89.  * directly, but to have port_softint do it at a safer time.
  90.  *
  91.  * (06/23/2000) gkh
  92.  * Cleaned up debugging statements in a quest to find UHCI timeout bug.
  93.  *
  94.  * (05/22/2000) gkh
  95.  * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be 
  96.  * removed from the individual device source files.
  97.  *
  98.  * (05/03/2000) gkh
  99.  * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
  100.  * 
  101.  * (05/02/2000) gkh
  102.  * Changed devfs and tty register code to work properly now. This was based on
  103.  * the ACM driver changes by Vojtech Pavlik.
  104.  *
  105.  * (04/27/2000) Ryan VanderBijl
  106.  *  Put calls to *_paranoia_checks into one function.
  107.  * 
  108.  * (04/23/2000) gkh
  109.  * Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
  110.  * Moved when the startup code printed out the devices that are supported.
  111.  *
  112.  * (04/19/2000) gkh
  113.  * Added driver for ZyXEL omni.net lcd plus ISDN TA
  114.  * Made startup info message specify which drivers were compiled in.
  115.  *
  116.  * (04/03/2000) gkh
  117.  * Changed the probe process to remove the module unload races.
  118.  * Changed where the tty layer gets initialized to have devfs work nicer.
  119.  * Added initial devfs support.
  120.  *
  121.  * (03/26/2000) gkh
  122.  * Split driver up into device specific pieces.
  123.  * 
  124.  * (03/19/2000) gkh
  125.  * Fixed oops that could happen when device was removed while a program
  126.  * was talking to the device.
  127.  * Removed the static urbs and now all urbs are created and destroyed
  128.  * dynamically.
  129.  * Reworked the internal interface. Now everything is based on the 
  130.  * usb_serial_port structure instead of the larger usb_serial structure.
  131.  * This fixes the bug that a multiport device could not have more than
  132.  * one port open at one time.
  133.  *
  134.  * (03/17/2000) gkh
  135.  * Added config option for debugging messages.
  136.  * Added patch for keyspan pda from Brian Warner.
  137.  *
  138.  * (03/06/2000) gkh
  139.  * Added the keyspan pda code from Brian Warner <warner@lothar.com>
  140.  * Moved a bunch of the port specific stuff into its own structure. This
  141.  * is in anticipation of the true multiport devices (there's a bug if you
  142.  * try to access more than one port of any multiport device right now)
  143.  *
  144.  * (02/21/2000) gkh
  145.  * Made it so that any serial devices only have to specify which functions
  146.  * they want to overload from the generic function calls (great, 
  147.  * inheritance in C, in a driver, just what I wanted...)
  148.  * Added support for set_termios and ioctl function calls. No drivers take
  149.  * advantage of this yet.
  150.  * Removed the #ifdef MODULE, now there is no module specific code.
  151.  * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
  152.  * to Miles Lott).
  153.  * Small fix to get_free_serial.
  154.  *
  155.  * (02/14/2000) gkh
  156.  * Removed the Belkin and Peracom functionality from the driver due to
  157.  * the lack of support from the vendor, and me not wanting people to 
  158.  * accidenatly buy the device, expecting it to work with Linux.
  159.  * Added read_bulk_callback and write_bulk_callback to the type structure
  160.  * for the needs of the FTDI and WhiteHEAT driver.
  161.  * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
  162.  * Ryder.
  163.  * Changed the output urb size back to the max endpoint size to make
  164.  * the ftdi_sio driver have it easier, and due to the fact that it didn't
  165.  * really increase the speed any.
  166.  *
  167.  * (02/11/2000) gkh
  168.  * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
  169.  * patch from Miles Lott (milos@insync.net).
  170.  * Fixed bug with not restoring the minor range that a device grabs, if
  171.  * the startup function fails (thanks Miles for finding this).
  172.  *
  173.  * (02/05/2000) gkh
  174.  * Added initial framework for the Keyspan PDA serial converter so that
  175.  * Brian Warner has a place to put his code.
  176.  * Made the ezusb specific functions generic enough that different
  177.  * devices can use them (whiteheat and keyspan_pda both need them).
  178.  * Split out a whole bunch of structure and other stuff to a seperate
  179.  * usb-serial.h file.
  180.  * Made the Visor connection messages a little more understandable, now
  181.  * that Miles Lott (milos@insync.net) has gotten the Generic channel to
  182.  * work. Also made them always show up in the log file.
  183.  * 
  184.  * (01/25/2000) gkh
  185.  * Added initial framework for FTDI serial converter so that Bill Ryder
  186.  * has a place to put his code.
  187.  * Added the vendor specific info from Handspring. Now we can print out
  188.  * informational debug messages as well as understand what is happening.
  189.  *
  190.  * (01/23/2000) gkh
  191.  * Fixed problem of crash when trying to open a port that didn't have a
  192.  * device assigned to it. Made the minor node finding a little smarter,
  193.  * now it looks to find a continous space for the new device.
  194.  *
  195.  * (01/21/2000) gkh
  196.  * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
  197.  * Fixed get_serial_by_minor which was all messed up for multi port 
  198.  * devices. Fixed multi port problem for generic devices. Now the number
  199.  * of ports is determined by the number of bulk out endpoints for the
  200.  * generic device.
  201.  *
  202.  * (01/19/2000) gkh
  203.  * Removed lots of cruft that was around from the old (pre urb) driver 
  204.  * interface.
  205.  * Made the serial_table dynamic. This should save lots of memory when
  206.  * the number of minor nodes goes up to 256.
  207.  * Added initial support for devices that have more than one port. 
  208.  * Added more debugging comments for the Visor, and added a needed 
  209.  * set_configuration call.
  210.  *
  211.  * (01/17/2000) gkh
  212.  * Fixed the WhiteHEAT firmware (my processing tool had a bug)
  213.  * and added new debug loader firmware for it.
  214.  * Removed the put_char function as it isn't really needed.
  215.  * Added visor startup commands as found by the Win98 dump.
  216.  * 
  217.  * (01/13/2000) gkh
  218.  * Fixed the vendor id for the generic driver to the one I meant it to be.
  219.  *
  220.  * (01/12/2000) gkh
  221.  * Forget the version numbering...that's pretty useless...
  222.  * Made the driver able to be compiled so that the user can select which
  223.  * converter they want to use. This allows people who only want the Visor
  224.  * support to not pay the memory size price of the WhiteHEAT.
  225.  * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
  226.  * grabbed the root hub. Not good.
  227.  * 
  228.  * version 0.4.0 (01/10/2000) gkh
  229.  * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
  230.  * device. Added startup function to allow firmware to be downloaded to
  231.  * a device if it needs to be.
  232.  * Added firmware download logic to the WhiteHEAT device.
  233.  * Started to add #defines to split up the different drivers for potential
  234.  * configuration option.
  235.  *
  236.  * version 0.3.1 (12/30/99) gkh
  237.  *      Fixed problems with urb for bulk out.
  238.  *      Added initial support for multiple sets of endpoints. This enables
  239.  *      the Handspring Visor to be attached successfully. Only the first
  240.  *      bulk in / bulk out endpoint pair is being used right now.
  241.  *
  242.  * version 0.3.0 (12/27/99) gkh
  243.  * Added initial support for the Handspring Visor based on a patch from
  244.  * Miles Lott (milos@sneety.insync.net)
  245.  * Cleaned up the code a bunch and converted over to using urbs only.
  246.  *
  247.  * version 0.2.3 (12/21/99) gkh
  248.  * Added initial support for the Connect Tech WhiteHEAT converter.
  249.  * Incremented the number of ports in expectation of getting the
  250.  * WhiteHEAT to work properly (4 ports per connection).
  251.  * Added notification on insertion and removal of what port the
  252.  * device is/was connected to (and what kind of device it was).
  253.  *
  254.  * version 0.2.2 (12/16/99) gkh
  255.  * Changed major number to the new allocated number. We're legal now!
  256.  *
  257.  * version 0.2.1 (12/14/99) gkh
  258.  * Fixed bug that happens when device node is opened when there isn't a
  259.  * device attached to it. Thanks to marek@webdesign.no for noticing this.
  260.  *
  261.  * version 0.2.0 (11/10/99) gkh
  262.  * Split up internals to make it easier to add different types of serial 
  263.  * converters to the code.
  264.  * Added a "generic" driver that gets it's vendor and product id
  265.  * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
  266.  * for the idea and sample code (from the usb scanner driver.)
  267.  * Cleared up any licensing questions by releasing it under the GNU GPL.
  268.  *
  269.  * version 0.1.2 (10/25/99) gkh
  270.  *  Fixed bug in detecting device.
  271.  *
  272.  * version 0.1.1 (10/05/99) gkh
  273.  *  Changed the major number to not conflict with anything else.
  274.  *
  275.  * version 0.1 (09/28/99) gkh
  276.  *  Can recognize the two different devices and start up a read from
  277.  * device when asked to. Writes also work. No control signals yet, this
  278.  * all is vendor specific data (i.e. no spec), also no control for
  279.  * different baud rates or other bit settings.
  280.  * Currently we are using the same devid as the acm driver. This needs
  281.  * to change.
  282.  * 
  283.  */
  284. #include <linux/config.h>
  285. #include <linux/kernel.h>
  286. #include <linux/sched.h>
  287. #include <linux/signal.h>
  288. #include <linux/errno.h>
  289. #include <linux/poll.h>
  290. #include <linux/init.h>
  291. #include <linux/slab.h>
  292. #include <linux/fcntl.h>
  293. #include <linux/tty.h>
  294. #include <linux/tty_driver.h>
  295. #include <linux/tty_flip.h>
  296. #include <linux/module.h>
  297. #include <linux/spinlock.h>
  298. #include <linux/list.h>
  299. #include <linux/smp_lock.h>
  300. #include <linux/usb.h>
  301. #ifdef CONFIG_USB_SERIAL_DEBUG
  302. static int debug = 1;
  303. #else
  304. static int debug;
  305. #endif
  306. #include "usb-serial.h"
  307. #include "pl2303.h"
  308. /*
  309.  * Version Information
  310.  */
  311. #define DRIVER_VERSION "v1.4"
  312. #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/"
  313. #define DRIVER_DESC "USB Serial Driver core"
  314. /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
  315. /* need to always compile these in, as some of the other devices use these functions as their own. */
  316. /* if a driver does not provide a function pointer, the generic function will be called. */
  317. static int  generic_open (struct usb_serial_port *port, struct file *filp);
  318. static void generic_close (struct usb_serial_port *port, struct file *filp);
  319. static int  generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
  320. static int  generic_write_room (struct usb_serial_port *port);
  321. static int  generic_chars_in_buffer (struct usb_serial_port *port);
  322. static void generic_read_bulk_callback (struct urb *urb);
  323. static void generic_write_bulk_callback (struct urb *urb);
  324. static void generic_shutdown (struct usb_serial *serial);
  325. #ifdef CONFIG_USB_SERIAL_GENERIC
  326. static __u16 vendor = 0x05f9;
  327. static __u16 product = 0xffff;
  328. static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
  329. /* All of the device info needed for the Generic Serial Converter */
  330. static struct usb_serial_device_type generic_device = {
  331. name: "Generic",
  332. id_table: generic_device_ids,
  333. needs_interrupt_in: DONT_CARE, /* don't have to have an interrupt in endpoint */
  334. needs_bulk_in: DONT_CARE, /* don't have to have a bulk in endpoint */
  335. needs_bulk_out: DONT_CARE, /* don't have to have a bulk out endpoint */
  336. num_interrupt_in: NUM_DONT_CARE,
  337. num_bulk_in: NUM_DONT_CARE,
  338. num_bulk_out: NUM_DONT_CARE,
  339. num_ports: 1,
  340. shutdown: generic_shutdown,
  341. };
  342. #define if_generic_do(x)
  343. if ((serial->vendor == vendor) &&
  344.     (serial->product == product))
  345.                 x
  346. #else
  347. #define if_generic_do(x)
  348. #endif
  349. /* local function prototypes */
  350. static int  serial_open (struct tty_struct *tty, struct file * filp);
  351. static void serial_close (struct tty_struct *tty, struct file * filp);
  352. static int  serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
  353. static int  serial_write_room (struct tty_struct *tty);
  354. static int  serial_chars_in_buffer (struct tty_struct *tty);
  355. static void serial_throttle (struct tty_struct * tty);
  356. static void serial_unthrottle (struct tty_struct * tty);
  357. static int  serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg);
  358. static void serial_set_termios (struct tty_struct *tty, struct termios * old);
  359. static void serial_shutdown (struct usb_serial *serial);
  360. static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
  361.        const struct usb_device_id *id);
  362. static void usb_serial_disconnect(struct usb_device *dev, void *ptr);
  363. static struct usb_driver usb_serial_driver = {
  364. name: "serial",
  365. probe: usb_serial_probe,
  366. disconnect: usb_serial_disconnect,
  367. id_table: NULL,  /* check all devices */
  368. };
  369. /* There is no MODULE_DEVICE_TABLE for usbserial.c.  Instead
  370.    the MODULE_DEVICE_TABLE declarations in each serial driver
  371.    cause the "hotplug" program to pull in whatever module is necessary
  372.    via modprobe, and modprobe will load usbserial because the serial
  373.    drivers depend on it.
  374. */
  375.    
  376. static int serial_refcount;
  377. static struct tty_driver serial_tty_driver;
  378. static struct tty_struct * serial_tty[SERIAL_TTY_MINORS];
  379. static struct termios * serial_termios[SERIAL_TTY_MINORS];
  380. static struct termios * serial_termios_locked[SERIAL_TTY_MINORS];
  381. static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
  382. static LIST_HEAD(usb_serial_driver_list);
  383. static struct usb_serial *get_serial_by_minor (int minor)
  384. {
  385. return serial_table[minor];
  386. }
  387. static struct usb_serial *get_free_serial (int num_ports, int *minor)
  388. {
  389. struct usb_serial *serial = NULL;
  390. int i, j;
  391. int good_spot;
  392. dbg(__FUNCTION__ " %d", num_ports);
  393. *minor = 0;
  394. for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
  395. if (serial_table[i])
  396. continue;
  397. good_spot = 1;
  398. for (j = 1; j <= num_ports-1; ++j)
  399. if (serial_table[i+j])
  400. good_spot = 0;
  401. if (good_spot == 0)
  402. continue;
  403. if (!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) {
  404. err(__FUNCTION__ " - Out of memory");
  405. return NULL;
  406. }
  407. memset(serial, 0, sizeof(struct usb_serial));
  408. serial->magic = USB_SERIAL_MAGIC;
  409. serial_table[i] = serial;
  410. *minor = i;
  411. dbg(__FUNCTION__ " - minor base = %d", *minor);
  412. for (i = *minor+1; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
  413. serial_table[i] = serial;
  414. return serial;
  415. }
  416. return NULL;
  417. }
  418. static void return_serial (struct usb_serial *serial)
  419. {
  420. int i;
  421. dbg(__FUNCTION__);
  422. if (serial == NULL)
  423. return;
  424. for (i = 0; i < serial->num_ports; ++i) {
  425. serial_table[serial->minor + i] = NULL;
  426. }
  427. return;
  428. }
  429. #ifdef USES_EZUSB_FUNCTIONS
  430. /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
  431. #define CPUCS_REG    0x7F92
  432. int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest)
  433. {
  434. int result;
  435. unsigned char *transfer_buffer;
  436. /* dbg("ezusb_writememory %x, %d", address, length); */
  437. if (!serial->dev) {
  438. dbg(__FUNCTION__ " - no physical device present, failing.");
  439. return -ENODEV;
  440. }
  441. transfer_buffer =  kmalloc (length, GFP_KERNEL);
  442. if (!transfer_buffer) {
  443. err(__FUNCTION__ " - kmalloc(%d) failed.", length);
  444. return -ENOMEM;
  445. }
  446. memcpy (transfer_buffer, data, length);
  447. result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), bRequest, 0x40, address, 0, transfer_buffer, length, 300);
  448. kfree (transfer_buffer);
  449. return result;
  450. }
  451. int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
  452. {
  453. int response;
  454. dbg(__FUNCTION__ " - %d", reset_bit);
  455. response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0);
  456. if (response < 0) {
  457. err(__FUNCTION__ "- %d failed", reset_bit);
  458. }
  459. return response;
  460. }
  461. #endif /* USES_EZUSB_FUNCTIONS */
  462. /*****************************************************************************
  463.  * Driver tty interface functions
  464.  *****************************************************************************/
  465. static int serial_open (struct tty_struct *tty, struct file * filp)
  466. {
  467. struct usb_serial *serial;
  468. struct usb_serial_port *port;
  469. int portNumber;
  470. dbg(__FUNCTION__);
  471. /* initialize the pointer incase something fails */
  472. tty->driver_data = NULL;
  473. /* get the serial object associated with this tty pointer */
  474. serial = get_serial_by_minor (MINOR(tty->device));
  475. if (serial_paranoia_check (serial, __FUNCTION__)) {
  476. return -ENODEV;
  477. }
  478. /* set up our port structure making the tty driver remember our port object, and us it */
  479. portNumber = MINOR(tty->device) - serial->minor;
  480. port = &serial->port[portNumber];
  481. tty->driver_data = port;
  482. port->tty = tty;
  483.  
  484. /* pass on to the driver specific version of this function if it is available */
  485. if (serial->type->open) {
  486. return (serial->type->open(port, filp));
  487. } else {
  488. return (generic_open(port, filp));
  489. }
  490. }
  491. static void serial_close(struct tty_struct *tty, struct file * filp)
  492. {
  493. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  494. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  495. if (!serial) {
  496. return;
  497. }
  498. dbg(__FUNCTION__ " - port %d", port->number);
  499. if (!port->active) {
  500. dbg (__FUNCTION__ " - port not opened");
  501. return;
  502. }
  503. /* pass on to the driver specific version of this function if it is available */
  504. if (serial->type->close) {
  505. serial->type->close(port, filp);
  506. } else {
  507. generic_close(port, filp);
  508. }
  509. }
  510. static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
  511. {
  512. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  513. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  514. if (!serial) {
  515. return -ENODEV;
  516. }
  517. dbg(__FUNCTION__ " - port %d, %d byte(s)", port->number, count);
  518. if (!port->active) {
  519. dbg (__FUNCTION__ " - port not opened");
  520. return -EINVAL;
  521. }
  522. /* pass on to the driver specific version of this function if it is available */
  523. if (serial->type->write) {
  524. return (serial->type->write(port, from_user, buf, count));
  525. } else {
  526. return (generic_write(port, from_user, buf, count));
  527. }
  528. }
  529. static int serial_write_room (struct tty_struct *tty) 
  530. {
  531. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  532. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  533. if (!serial) {
  534. return -ENODEV;
  535. }
  536. dbg(__FUNCTION__ " - port %d", port->number);
  537. if (!port->active) {
  538. dbg (__FUNCTION__ " - port not open");
  539. return -EINVAL;
  540. }
  541. /* pass on to the driver specific version of this function if it is available */
  542. if (serial->type->write_room) {
  543. return (serial->type->write_room(port));
  544. } else {
  545. return (generic_write_room(port));
  546. }
  547. }
  548. static int serial_chars_in_buffer (struct tty_struct *tty) 
  549. {
  550. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  551. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  552. if (!serial) {
  553. return -ENODEV;
  554. }
  555. if (!port->active) {
  556. dbg (__FUNCTION__ " - port not open");
  557. return -EINVAL;
  558. }
  559. /* pass on to the driver specific version of this function if it is available */
  560. if (serial->type->chars_in_buffer) {
  561. return (serial->type->chars_in_buffer(port));
  562. } else {
  563. return (generic_chars_in_buffer(port));
  564. }
  565. }
  566. static void serial_throttle (struct tty_struct * tty)
  567. {
  568. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  569. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  570. if (!serial) {
  571. return;
  572. }
  573. dbg(__FUNCTION__ " - port %d", port->number);
  574. if (!port->active) {
  575. dbg (__FUNCTION__ " - port not open");
  576. return;
  577. }
  578. /* pass on to the driver specific version of this function */
  579. if (serial->type->throttle) {
  580. serial->type->throttle(port);
  581. }
  582. return;
  583. }
  584. static void serial_unthrottle (struct tty_struct * tty)
  585. {
  586. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  587. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  588. if (!serial) {
  589. return;
  590. }
  591. dbg(__FUNCTION__ " - port %d", port->number);
  592. if (!port->active) {
  593. dbg (__FUNCTION__ " - port not open");
  594. return;
  595. }
  596. /* pass on to the driver specific version of this function */
  597. if (serial->type->unthrottle) {
  598. serial->type->unthrottle(port);
  599. }
  600. return;
  601. }
  602. static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
  603. {
  604. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  605. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  606. if (!serial) {
  607. return -ENODEV;
  608. }
  609. dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);
  610. if (!port->active) {
  611. dbg (__FUNCTION__ " - port not open");
  612. return -ENODEV;
  613. }
  614. /* pass on to the driver specific version of this function if it is available */
  615. if (serial->type->ioctl) {
  616. return (serial->type->ioctl(port, file, cmd, arg));
  617. } else {
  618. return -ENOIOCTLCMD;
  619. }
  620. }
  621. static void serial_set_termios (struct tty_struct *tty, struct termios * old)
  622. {
  623. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  624. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  625. if (!serial) {
  626. return;
  627. }
  628. dbg(__FUNCTION__ " - port %d", port->number);
  629. if (!port->active) {
  630. dbg (__FUNCTION__ " - port not open");
  631. return;
  632. }
  633. /* pass on to the driver specific version of this function if it is available */
  634. if (serial->type->set_termios) {
  635. serial->type->set_termios(port, old);
  636. }
  637. return;
  638. }
  639. static void serial_break (struct tty_struct *tty, int break_state)
  640. {
  641. struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
  642. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  643. if (!serial) {
  644. return;
  645. }
  646. dbg(__FUNCTION__ " - port %d", port->number);
  647. if (!port->active) {
  648. dbg (__FUNCTION__ " - port not open");
  649. return;
  650. }
  651. /* pass on to the driver specific version of this function if it is
  652.            available */
  653. if (serial->type->break_ctl) {
  654. serial->type->break_ctl(port, break_state);
  655. }
  656. }
  657. static void serial_shutdown (struct usb_serial *serial)
  658. {
  659. if (serial->type->shutdown) {
  660. serial->type->shutdown(serial);
  661. } else {
  662. generic_shutdown(serial);
  663. }
  664. }
  665. /*****************************************************************************
  666.  * generic devices specific driver functions
  667.  *****************************************************************************/
  668. static int generic_open (struct usb_serial_port *port, struct file *filp)
  669. {
  670. struct usb_serial *serial = port->serial;
  671. int result = 0;
  672. if (port_paranoia_check (port, __FUNCTION__))
  673. return -ENODEV;
  674. /* only increment our usage count, if this device is _really_ a generic device */
  675. if_generic_do(MOD_INC_USE_COUNT);
  676. dbg(__FUNCTION__ " - port %d", port->number);
  677. down (&port->sem);
  678. ++port->open_count;
  679. if (!port->active) {
  680. port->active = 1;
  681. /* force low_latency on so that our tty_push actually forces the data through, 
  682.    otherwise it is scheduled, and with high data rates (like with OHCI) data
  683.    can get lost. */
  684. port->tty->low_latency = 1;
  685. /* if we have a bulk interrupt, start reading from it */
  686. if (serial->num_bulk_in) {
  687. /* Start reading from the device */
  688. FILL_BULK_URB(port->read_urb, serial->dev, 
  689.       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  690.       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
  691.       ((serial->type->read_bulk_callback) ?
  692.        serial->type->read_bulk_callback :
  693.        generic_read_bulk_callback), 
  694.       port);
  695. result = usb_submit_urb(port->read_urb);
  696. if (result)
  697. err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
  698. }
  699. }
  700. up (&port->sem);
  701. return result;
  702. }
  703. static void generic_close (struct usb_serial_port *port, struct file * filp)
  704. {
  705. struct usb_serial *serial = port->serial;
  706. dbg(__FUNCTION__ " - port %d", port->number);
  707. down (&port->sem);
  708. --port->open_count;
  709. if (port->open_count <= 0) {
  710. if (serial->dev) {
  711. /* shutdown any bulk reads that might be going on */
  712. if (serial->num_bulk_out)
  713. usb_unlink_urb (port->write_urb);
  714. if (serial->num_bulk_in)
  715. usb_unlink_urb (port->read_urb);
  716. }
  717. port->active = 0;
  718. port->open_count = 0;
  719. }
  720. up (&port->sem);
  721. /* only decrement our usage count, if this device is _really_ a generic device */
  722. if_generic_do(MOD_DEC_USE_COUNT);
  723. }
  724. static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
  725. {
  726. struct usb_serial *serial = port->serial;
  727. int result;
  728. dbg(__FUNCTION__ " - port %d", port->number);
  729. if (count == 0) {
  730. dbg(__FUNCTION__ " - write request of 0 bytes");
  731. return (0);
  732. }
  733. /* only do something if we have a bulk out endpoint */
  734. if (serial->num_bulk_out) {
  735. if (port->write_urb->status == -EINPROGRESS) {
  736. dbg (__FUNCTION__ " - already writing");
  737. return (0);
  738. }
  739. count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  740. if (from_user) {
  741. if (copy_from_user(port->write_urb->transfer_buffer, buf, count))
  742. return -EFAULT;
  743. }
  744. else {
  745. memcpy (port->write_urb->transfer_buffer, buf, count);
  746. }  
  747. usb_serial_debug_data (__FILE__, __FUNCTION__, count, port->write_urb->transfer_buffer);
  748. /* set up our urb */
  749. FILL_BULK_URB(port->write_urb, serial->dev, 
  750.       usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
  751.       port->write_urb->transfer_buffer, count,
  752.       ((serial->type->write_bulk_callback) ? 
  753.        serial->type->write_bulk_callback : 
  754.        generic_write_bulk_callback), 
  755.       port);
  756. /* send the data out the bulk port */
  757. result = usb_submit_urb(port->write_urb);
  758. if (result)
  759. err(__FUNCTION__ " - failed submitting write urb, error %d", result);
  760. else
  761. result = count;
  762. return result;
  763. }
  764. /* no bulk out, so return 0 bytes written */
  765. return (0);
  766. static int generic_write_room (struct usb_serial_port *port)
  767. {
  768. struct usb_serial *serial = port->serial;
  769. int room = 0;
  770. dbg(__FUNCTION__ " - port %d", port->number);
  771. if (serial->num_bulk_out) {
  772. if (port->write_urb->status != -EINPROGRESS)
  773. room = port->bulk_out_size;
  774. }
  775. dbg(__FUNCTION__ " - returns %d", room);
  776. return (room);
  777. }
  778. static int generic_chars_in_buffer (struct usb_serial_port *port)
  779. {
  780. struct usb_serial *serial = port->serial;
  781. int chars = 0;
  782. dbg(__FUNCTION__ " - port %d", port->number);
  783. if (serial->num_bulk_out) {
  784. if (port->write_urb->status == -EINPROGRESS)
  785. chars = port->write_urb->transfer_buffer_length;
  786. }
  787. dbg (__FUNCTION__ " - returns %d", chars);
  788. return (chars);
  789. }
  790. static void generic_read_bulk_callback (struct urb *urb)
  791. {
  792. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  793. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  794. struct tty_struct *tty;
  795. unsigned char *data = urb->transfer_buffer;
  796. int i;
  797. int result;
  798. dbg(__FUNCTION__ " - port %d", port->number);
  799. if (!serial) {
  800. dbg(__FUNCTION__ " - bad serial pointer, exiting");
  801. return;
  802. }
  803. if (urb->status) {
  804. dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
  805. return;
  806. }
  807. usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
  808. tty = port->tty;
  809. if (urb->actual_length) {
  810. for (i = 0; i < urb->actual_length ; ++i) {
  811. /* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
  812. if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
  813. tty_flip_buffer_push(tty);
  814. }
  815. /* this doesn't actually push the data through unless tty->low_latency is set */
  816. tty_insert_flip_char(tty, data[i], 0);
  817. }
  818.    tty_flip_buffer_push(tty);
  819. }
  820. /* Continue trying to always read  */
  821. FILL_BULK_URB(port->read_urb, serial->dev, 
  822.       usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
  823.       port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
  824.       ((serial->type->read_bulk_callback) ?
  825.        serial->type->read_bulk_callback :
  826.        generic_read_bulk_callback), 
  827.       port);
  828. result = usb_submit_urb(port->read_urb);
  829. if (result)
  830. err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
  831. }
  832. static void generic_write_bulk_callback (struct urb *urb)
  833. {
  834. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  835. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  836. dbg(__FUNCTION__ " - port %d", port->number);
  837. if (!serial) {
  838. dbg(__FUNCTION__ " - bad serial pointer, exiting");
  839. return;
  840. }
  841. if (urb->status) {
  842. dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
  843. return;
  844. }
  845. queue_task(&port->tqueue, &tq_immediate);
  846. mark_bh(IMMEDIATE_BH);
  847. return;
  848. }
  849. static void generic_shutdown (struct usb_serial *serial)
  850. {
  851. int i;
  852. dbg (__FUNCTION__);
  853. /* stop reads and writes on all ports */
  854. for (i=0; i < serial->num_ports; ++i) {
  855. while (serial->port[i].open_count > 0) {
  856. generic_close (&serial->port[i], NULL);
  857. }
  858. }
  859. }
  860. static void port_softint(void *private)
  861. {
  862. struct usb_serial_port *port = (struct usb_serial_port *)private;
  863. struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
  864. struct tty_struct *tty;
  865. dbg(__FUNCTION__ " - port %d", port->number);
  866. if (!serial) {
  867. return;
  868. }
  869.  
  870. tty = port->tty;
  871. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
  872. dbg(__FUNCTION__ " - write wakeup call.");
  873. (tty->ldisc.write_wakeup)(tty);
  874. }
  875. wake_up_interruptible(&tty->write_wait);
  876. }
  877. static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
  878.        const struct usb_device_id *id)
  879. {
  880. struct usb_serial *serial = NULL;
  881. struct usb_serial_port *port;
  882. struct usb_interface *interface;
  883. struct usb_interface_descriptor *iface_desc;
  884. struct usb_endpoint_descriptor *endpoint;
  885. struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
  886. struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
  887. struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
  888. struct usb_serial_device_type *type = NULL;
  889. struct list_head *tmp;
  890. int found;
  891. int minor;
  892. int buffer_size;
  893. int i;
  894. char interrupt_pipe;
  895. char bulk_in_pipe;
  896. char bulk_out_pipe;
  897. int num_interrupt_in = 0;
  898. int num_bulk_in = 0;
  899. int num_bulk_out = 0;
  900. int num_ports;
  901. int max_endpoints;
  902. const struct usb_device_id *id_pattern = NULL;
  903. /* loop through our list of known serial converters, and see if this
  904.    device matches. */
  905. found = 0;
  906. interface = &dev->actconfig->interface[ifnum];
  907. list_for_each (tmp, &usb_serial_driver_list) {
  908. type = list_entry(tmp, struct usb_serial_device_type, driver_list);
  909. id_pattern = usb_match_id(dev, interface, type->id_table);
  910. if (id_pattern != NULL) {
  911. dbg("descriptor matches");
  912. found = 1;
  913. break;
  914. }
  915. }
  916. if (!found) {
  917. /* no match */
  918. dbg("none matched");
  919. return(NULL);
  920. }
  921. /* descriptor matches, let's find the endpoints needed */
  922. interrupt_pipe = bulk_in_pipe = bulk_out_pipe = HAS_NOT;
  923. /* check out the endpoints */
  924. iface_desc = &interface->altsetting[0];
  925. for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
  926. endpoint = &iface_desc->endpoint[i];
  927. if ((endpoint->bEndpointAddress & 0x80) &&
  928.     ((endpoint->bmAttributes & 3) == 0x02)) {
  929. /* we found a bulk in endpoint */
  930. dbg("found bulk in");
  931. bulk_in_pipe = HAS;
  932. bulk_in_endpoint[num_bulk_in] = endpoint;
  933. ++num_bulk_in;
  934. }
  935. if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
  936.     ((endpoint->bmAttributes & 3) == 0x02)) {
  937. /* we found a bulk out endpoint */
  938. dbg("found bulk out");
  939. bulk_out_pipe = HAS;
  940. bulk_out_endpoint[num_bulk_out] = endpoint;
  941. ++num_bulk_out;
  942. }
  943. if ((endpoint->bEndpointAddress & 0x80) &&
  944.     ((endpoint->bmAttributes & 3) == 0x03)) {
  945. /* we found a interrupt in endpoint */
  946. dbg("found interrupt in");
  947. interrupt_pipe = HAS;
  948. interrupt_in_endpoint[num_interrupt_in] = endpoint;
  949. ++num_interrupt_in;
  950. }
  951. }
  952. #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
  953. /* BEGIN HORRIBLE HACK FOR PL2303 */ 
  954. /* this is needed due to the looney way its endpoints are set up */
  955. if (ifnum == 1) {
  956. if (((dev->descriptor.idVendor == PL2303_VENDOR_ID) &&
  957.      (dev->descriptor.idProduct == PL2303_PRODUCT_ID)) ||
  958.     ((dev->descriptor.idVendor == ATEN_VENDOR_ID) &&
  959.      (dev->descriptor.idProduct == ATEN_PRODUCT_ID))) {
  960. /* check out the endpoints of the other interface*/
  961. interface = &dev->actconfig->interface[ifnum ^ 1];
  962. iface_desc = &interface->altsetting[0];
  963. for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
  964. endpoint = &iface_desc->endpoint[i];
  965. if ((endpoint->bEndpointAddress & 0x80) &&
  966.     ((endpoint->bmAttributes & 3) == 0x03)) {
  967. /* we found a interrupt in endpoint */
  968. dbg("found interrupt in for Prolific device on separate interface");
  969. interrupt_pipe = HAS;
  970. interrupt_in_endpoint[num_interrupt_in] = endpoint;
  971. ++num_interrupt_in;
  972. }
  973. }
  974. }
  975. }
  976. /* END HORRIBLE HACK FOR PL2303 */
  977. #endif
  978. /* verify that we found all of the endpoints that we need */
  979. if (!((interrupt_pipe & type->needs_interrupt_in) &&
  980.       (bulk_in_pipe & type->needs_bulk_in) &&
  981.       (bulk_out_pipe & type->needs_bulk_out))) {
  982. /* nope, they don't match what we expected */
  983. info("descriptors matched, but endpoints did not");
  984. return NULL;
  985. }
  986. /* found all that we need */
  987. info("%s converter detected", type->name);
  988. #ifdef CONFIG_USB_SERIAL_GENERIC
  989. if (type == &generic_device) {
  990. num_ports = num_bulk_out;
  991. if (num_ports == 0) {
  992. err("Generic device with no bulk out, not allowed.");
  993. return NULL;
  994. }
  995. } else
  996. #endif
  997. num_ports = type->num_ports;
  998. serial = get_free_serial (num_ports, &minor);
  999. if (serial == NULL) {
  1000. err("No more free serial devices");
  1001. return NULL;
  1002. }
  1003. serial->dev = dev;
  1004. serial->type = type;
  1005. serial->interface = interface;
  1006. serial->minor = minor;
  1007. serial->num_ports = num_ports;
  1008. serial->num_bulk_in = num_bulk_in;
  1009. serial->num_bulk_out = num_bulk_out;
  1010. serial->num_interrupt_in = num_interrupt_in;
  1011. serial->vendor = dev->descriptor.idVendor;
  1012. serial->product = dev->descriptor.idProduct;
  1013. /* if this device type has a startup function, call it */
  1014. if (type->startup) {
  1015. i = type->startup (serial);
  1016. if (i < 0)
  1017. goto probe_error;
  1018. if (i > 0)
  1019. return serial;
  1020. }
  1021. /* set up the endpoint information */
  1022. for (i = 0; i < num_bulk_in; ++i) {
  1023. endpoint = bulk_in_endpoint[i];
  1024. port = &serial->port[i];
  1025. port->read_urb = usb_alloc_urb (0);
  1026. if (!port->read_urb) {
  1027. err("No free urbs available");
  1028. goto probe_error;
  1029. }
  1030. buffer_size = endpoint->wMaxPacketSize;
  1031. port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
  1032. port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  1033. if (!port->bulk_in_buffer) {
  1034. err("Couldn't allocate bulk_in_buffer");
  1035. goto probe_error;
  1036. }
  1037. FILL_BULK_URB(port->read_urb, dev, 
  1038.       usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
  1039.       port->bulk_in_buffer, buffer_size, 
  1040.       ((serial->type->read_bulk_callback) ?
  1041.        serial->type->read_bulk_callback :
  1042.        generic_read_bulk_callback), 
  1043.       port);
  1044. }
  1045. for (i = 0; i < num_bulk_out; ++i) {
  1046. endpoint = bulk_out_endpoint[i];
  1047. port = &serial->port[i];
  1048. port->write_urb = usb_alloc_urb(0);
  1049. if (!port->write_urb) {
  1050. err("No free urbs available");
  1051. goto probe_error;
  1052. }
  1053. buffer_size = endpoint->wMaxPacketSize;
  1054. port->bulk_out_size = buffer_size;
  1055. port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
  1056. port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
  1057. if (!port->bulk_out_buffer) {
  1058. err("Couldn't allocate bulk_out_buffer");
  1059. goto probe_error;
  1060. }
  1061. FILL_BULK_URB(port->write_urb, dev, 
  1062.       usb_sndbulkpipe(dev, endpoint->bEndpointAddress),
  1063.       port->bulk_out_buffer, buffer_size,
  1064.       ((serial->type->write_bulk_callback) ? 
  1065.        serial->type->write_bulk_callback : 
  1066.        generic_write_bulk_callback), 
  1067.       port);
  1068. }
  1069. for (i = 0; i < num_interrupt_in; ++i) {
  1070. endpoint = interrupt_in_endpoint[i];
  1071. port = &serial->port[i];
  1072. port->interrupt_in_urb = usb_alloc_urb(0);
  1073. if (!port->interrupt_in_urb) {
  1074. err("No free urbs available");
  1075. goto probe_error;
  1076. }
  1077. buffer_size = endpoint->wMaxPacketSize;
  1078. port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
  1079. port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
  1080. if (!port->interrupt_in_buffer) {
  1081. err("Couldn't allocate interrupt_in_buffer");
  1082. goto probe_error;
  1083. }
  1084. FILL_INT_URB(port->interrupt_in_urb, dev, 
  1085.      usb_rcvintpipe(dev, endpoint->bEndpointAddress),
  1086.      port->interrupt_in_buffer, buffer_size, 
  1087.      serial->type->read_int_callback,
  1088.      port, 
  1089.      endpoint->bInterval);
  1090. }
  1091. /* initialize some parts of the port structures */
  1092. /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
  1093. max_endpoints = max(num_bulk_in, num_bulk_out);
  1094. max_endpoints = max(max_endpoints, num_interrupt_in);
  1095. max_endpoints = max(max_endpoints, (int)serial->num_ports);
  1096. dbg (__FUNCTION__ " - setting up %d port structures for this device", max_endpoints);
  1097. for (i = 0; i < max_endpoints; ++i) {
  1098. port = &serial->port[i];
  1099. port->number = i + serial->minor;
  1100. port->serial = serial;
  1101. port->magic = USB_SERIAL_PORT_MAGIC;
  1102. port->tqueue.routine = port_softint;
  1103. port->tqueue.data = port;
  1104. init_MUTEX (&port->sem);
  1105. }
  1106. /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
  1107. for (i = 0; i < serial->num_ports; ++i) {
  1108. tty_register_devfs (&serial_tty_driver, 0, serial->port[i].number);
  1109. info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)", 
  1110.      type->name, serial->port[i].number, serial->port[i].number);
  1111. }
  1112. return serial; /* success */
  1113. probe_error:
  1114. for (i = 0; i < num_bulk_in; ++i) {
  1115. port = &serial->port[i];
  1116. if (port->read_urb)
  1117. usb_free_urb (port->read_urb);
  1118. if (port->bulk_in_buffer)
  1119. kfree (port->bulk_in_buffer);
  1120. }
  1121. for (i = 0; i < num_bulk_out; ++i) {
  1122. port = &serial->port[i];
  1123. if (port->write_urb)
  1124. usb_free_urb (port->write_urb);
  1125. if (port->bulk_out_buffer)
  1126. kfree (port->bulk_out_buffer);
  1127. }
  1128. for (i = 0; i < num_interrupt_in; ++i) {
  1129. port = &serial->port[i];
  1130. if (port->interrupt_in_urb)
  1131. usb_free_urb (port->interrupt_in_urb);
  1132. if (port->interrupt_in_buffer)
  1133. kfree (port->interrupt_in_buffer);
  1134. }
  1135. /* return the minor range that this device had */
  1136. return_serial (serial);
  1137. /* free up any memory that we allocated */
  1138. kfree (serial);
  1139. return NULL;
  1140. }
  1141. static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
  1142. {
  1143. struct usb_serial *serial = (struct usb_serial *) ptr;
  1144. struct usb_serial_port *port;
  1145. int i;
  1146. if (serial) {
  1147. /* fail all future close/read/write/ioctl/etc calls */
  1148. for (i = 0; i < serial->num_ports; ++i) {
  1149. if (serial->port[i].tty != NULL)
  1150. serial->port[i].tty->driver_data = NULL;
  1151. }
  1152. serial->dev = NULL;
  1153. serial_shutdown (serial);
  1154. for (i = 0; i < serial->num_ports; ++i)
  1155. serial->port[i].active = 0;
  1156. for (i = 0; i < serial->num_bulk_in; ++i) {
  1157. port = &serial->port[i];
  1158. if (port->read_urb) {
  1159. usb_unlink_urb (port->read_urb);
  1160. usb_free_urb (port->read_urb);
  1161. }
  1162. if (port->bulk_in_buffer)
  1163. kfree (port->bulk_in_buffer);
  1164. }
  1165. for (i = 0; i < serial->num_bulk_out; ++i) {
  1166. port = &serial->port[i];
  1167. if (port->write_urb) {
  1168. usb_unlink_urb (port->write_urb);
  1169. usb_free_urb (port->write_urb);
  1170. }
  1171. if (port->bulk_out_buffer)
  1172. kfree (port->bulk_out_buffer);
  1173. }
  1174. for (i = 0; i < serial->num_interrupt_in; ++i) {
  1175. port = &serial->port[i];
  1176. if (port->interrupt_in_urb) {
  1177. usb_unlink_urb (port->interrupt_in_urb);
  1178. usb_free_urb (port->interrupt_in_urb);
  1179. }
  1180. if (port->interrupt_in_buffer)
  1181. kfree (port->interrupt_in_buffer);
  1182. }
  1183. for (i = 0; i < serial->num_ports; ++i) {
  1184. tty_unregister_devfs (&serial_tty_driver, serial->port[i].number);
  1185. info("%s converter now disconnected from ttyUSB%d", serial->type->name, serial->port[i].number);
  1186. }
  1187. /* return the minor range that this device had */
  1188. return_serial (serial);
  1189. /* free up any memory that we allocated */
  1190. kfree (serial);
  1191. } else {
  1192. info("device disconnected");
  1193. }
  1194. }
  1195. static struct tty_driver serial_tty_driver = {
  1196. magic: TTY_DRIVER_MAGIC,
  1197. driver_name: "usb-serial",
  1198. name: "usb/tts/%d",
  1199. major: SERIAL_TTY_MAJOR,
  1200. minor_start: 0,
  1201. num: SERIAL_TTY_MINORS,
  1202. type: TTY_DRIVER_TYPE_SERIAL,
  1203. subtype: SERIAL_TYPE_NORMAL,
  1204. flags: TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
  1205. refcount: &serial_refcount,
  1206. table: serial_tty,
  1207. termios: serial_termios,
  1208. termios_locked: serial_termios_locked,
  1209. open: serial_open,
  1210. close: serial_close,
  1211. write: serial_write,
  1212. write_room: serial_write_room,
  1213. ioctl: serial_ioctl,
  1214. set_termios: serial_set_termios,
  1215. throttle: serial_throttle,
  1216. unthrottle: serial_unthrottle,
  1217. break_ctl: serial_break,
  1218. chars_in_buffer: serial_chars_in_buffer,
  1219. };
  1220. static int __init usb_serial_init(void)
  1221. {
  1222. int i;
  1223. int result;
  1224. /* Initalize our global data */
  1225. for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
  1226. serial_table[i] = NULL;
  1227. }
  1228. /* register the tty driver */
  1229. serial_tty_driver.init_termios          = tty_std_termios;
  1230. serial_tty_driver.init_termios.c_cflag  = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  1231. if (tty_register_driver (&serial_tty_driver)) {
  1232. err(__FUNCTION__ " - failed to register tty driver");
  1233. return -1;
  1234. }
  1235. /* register the USB driver */
  1236. result = usb_register(&usb_serial_driver);
  1237. if (result < 0) {
  1238. tty_unregister_driver(&serial_tty_driver);
  1239. err("usb_register failed for the usb-serial driver. Error number %d", result);
  1240. return -1;
  1241. }
  1242. #ifdef CONFIG_USB_SERIAL_GENERIC
  1243. generic_device_ids[0].idVendor = vendor;
  1244. generic_device_ids[0].idProduct = product;
  1245. generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
  1246. /* register our generic driver with ourselves */
  1247. usb_serial_register (&generic_device);
  1248. #endif
  1249. info(DRIVER_DESC " " DRIVER_VERSION);
  1250. return 0;
  1251. }
  1252. static void __exit usb_serial_exit(void)
  1253. {
  1254. #ifdef CONFIG_USB_SERIAL_GENERIC
  1255. /* remove our generic driver */
  1256. usb_serial_deregister (&generic_device);
  1257. #endif
  1258. usb_deregister(&usb_serial_driver);
  1259. tty_unregister_driver(&serial_tty_driver);
  1260. }
  1261. module_init(usb_serial_init);
  1262. module_exit(usb_serial_exit);
  1263. int usb_serial_register(struct usb_serial_device_type *new_device)
  1264. {
  1265. /* Add this device to our list of devices */
  1266. list_add(&new_device->driver_list, &usb_serial_driver_list);
  1267. info ("USB Serial support registered for %s", new_device->name);
  1268. usb_scan_devices();
  1269. return 0;
  1270. }
  1271. void usb_serial_deregister(struct usb_serial_device_type *device)
  1272. {
  1273. struct usb_serial *serial;
  1274. int i;
  1275. info("USB Serial deregistering driver %s", device->name);
  1276. /* clear out the serial_table if the device is attached to a port */
  1277. for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
  1278. serial = serial_table[i];
  1279. if ((serial != NULL) && (serial->type == device)) {
  1280. usb_driver_release_interface (&usb_serial_driver, serial->interface);
  1281. usb_serial_disconnect (NULL, serial);
  1282. }
  1283. }
  1284. list_del(&device->driver_list);
  1285. }
  1286. /* If the usb-serial core is build into the core, the usb-serial drivers
  1287.    need these symbols to load properly as modules. */
  1288. EXPORT_SYMBOL(usb_serial_register);
  1289. EXPORT_SYMBOL(usb_serial_deregister);
  1290. #ifdef USES_EZUSB_FUNCTIONS
  1291. EXPORT_SYMBOL(ezusb_writememory);
  1292. EXPORT_SYMBOL(ezusb_set_reset);
  1293. #endif
  1294. /* Module information */
  1295. MODULE_AUTHOR( DRIVER_AUTHOR );
  1296. MODULE_DESCRIPTION( DRIVER_DESC );
  1297. MODULE_LICENSE("GPL");
  1298. MODULE_PARM(debug, "i");
  1299. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1300. #ifdef CONFIG_USB_SERIAL_GENERIC
  1301. MODULE_PARM(vendor, "h");
  1302. MODULE_PARM_DESC(vendor, "User specified USB idVendor");
  1303. MODULE_PARM(product, "h");
  1304. MODULE_PARM_DESC(product, "User specified USB idProduct");
  1305. #endif