~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/usb/serial/usbserial.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.