Merge with Linux 2.5.56.
[linux-2.6/linux-mips.git] / drivers / usb / serial / usb-serial.c
blobf9fe3cc108643624dbbd69f93a20976a549e557e
1 /*
2 * USB Serial Converter driver
4 * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (c) 2000 Peter Berger (pberger@brimson.com)
6 * Copyright (c) 2000 Al Borchers (borchers@steinerpoint.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
12 * This driver was originally based on the ACM driver by Armin Fuerst (which was
13 * based on a driver by Brad Keryan)
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
17 * (12/10/2002) gkh
18 * Split the ports off into their own struct device, and added a
19 * usb-serial bus driver.
21 * (11/19/2002) gkh
22 * removed a few #ifdefs for the generic code and cleaned up the failure
23 * logic in initialization.
25 * (10/02/2002) gkh
26 * moved the console code to console.c and out of this file.
28 * (06/05/2002) gkh
29 * moved location of startup() call in serial_probe() until after all
30 * of the port information and endpoints are initialized. This makes
31 * things easier for some drivers.
33 * (04/10/2002) gkh
34 * added serial_read_proc function which creates a
35 * /proc/tty/driver/usb-serial file.
37 * (03/27/2002) gkh
38 * Got USB serial console code working properly and merged into the main
39 * version of the tree. Thanks to Randy Dunlap for the initial version
40 * of this code, and for pushing me to finish it up.
41 * The USB serial console works with any usb serial driver device.
43 * (03/21/2002) gkh
44 * Moved all manipulation of port->open_count into the core. Now the
45 * individual driver's open and close functions are called only when the
46 * first open() and last close() is called. Making the drivers a bit
47 * smaller and simpler.
48 * Fixed a bug if a driver didn't have the owner field set.
50 * (02/26/2002) gkh
51 * Moved all locking into the main serial_* functions, instead of having
52 * the individual drivers have to grab the port semaphore. This should
53 * reduce races.
54 * Reworked the MOD_INC logic a bit to always increment and decrement, even
55 * if the generic driver is being used.
57 * (10/10/2001) gkh
58 * usb_serial_disconnect() now sets the serial->dev pointer is to NULL to
59 * help prevent child drivers from accessing the device since it is now
60 * gone.
62 * (09/13/2001) gkh
63 * Moved generic driver initialize after we have registered with the USB
64 * core. Thanks to Randy Dunlap for pointing this problem out.
66 * (07/03/2001) gkh
67 * Fixed module paramater size. Thanks to John Brockmeyer for the pointer.
68 * Fixed vendor and product getting defined through the MODULE_PARM macro
69 * if the Generic driver wasn't compiled in.
70 * Fixed problem with generic_shutdown() not being called for drivers that
71 * don't have a shutdown() function.
73 * (06/06/2001) gkh
74 * added evil hack that is needed for the prolific pl2303 device due to the
75 * crazy way its endpoints are set up.
77 * (05/30/2001) gkh
78 * switched from using spinlock to a semaphore, which fixes lots of problems.
80 * (04/08/2001) gb
81 * Identify version on module load.
83 * 2001_02_05 gkh
84 * Fixed buffer overflows bug with the generic serial driver. Thanks to
85 * Todd Squires <squirest@ct0.com> for fixing this.
87 * (01/10/2001) gkh
88 * Fixed bug where the generic serial adaptor grabbed _any_ device that was
89 * offered to it.
91 * (12/12/2000) gkh
92 * Removed MOD_INC and MOD_DEC from poll and disconnect functions, and
93 * moved them to the serial_open and serial_close functions.
94 * Also fixed bug with there not being a MOD_DEC for the generic driver
95 * (thanks to Gary Brubaker for finding this.)
97 * (11/29/2000) gkh
98 * Small NULL pointer initialization cleanup which saves a bit of disk image
100 * (11/01/2000) Adam J. Richter
101 * instead of using idVendor/idProduct pairs, usb serial drivers
102 * now identify their hardware interest with usb_device_id tables,
103 * which they usually have anyhow for use with MODULE_DEVICE_TABLE.
105 * (10/05/2000) gkh
106 * Fixed bug with urb->dev not being set properly, now that the usb
107 * core needs it.
109 * (09/11/2000) gkh
110 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
112 * (08/28/2000) gkh
113 * Added port_lock to port structure.
114 * Added locks for SMP safeness to generic driver
115 * Fixed the ability to open a generic device's port more than once.
117 * (07/23/2000) gkh
118 * Added bulk_out_endpointAddress to port structure.
120 * (07/19/2000) gkh, pberger, and borchers
121 * Modifications to allow usb-serial drivers to be modules.
123 * (07/03/2000) gkh
124 * Added more debugging to serial_ioctl call
126 * (06/25/2000) gkh
127 * Changed generic_write_bulk_callback to not call wake_up_interruptible
128 * directly, but to have port_softint do it at a safer time.
130 * (06/23/2000) gkh
131 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
133 * (05/22/2000) gkh
134 * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
135 * removed from the individual device source files.
137 * (05/03/2000) gkh
138 * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
140 * (05/02/2000) gkh
141 * Changed devfs and tty register code to work properly now. This was based on
142 * the ACM driver changes by Vojtech Pavlik.
144 * (04/27/2000) Ryan VanderBijl
145 * Put calls to *_paranoia_checks into one function.
147 * (04/23/2000) gkh
148 * Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
149 * Moved when the startup code printed out the devices that are supported.
151 * (04/19/2000) gkh
152 * Added driver for ZyXEL omni.net lcd plus ISDN TA
153 * Made startup info message specify which drivers were compiled in.
155 * (04/03/2000) gkh
156 * Changed the probe process to remove the module unload races.
157 * Changed where the tty layer gets initialized to have devfs work nicer.
158 * Added initial devfs support.
160 * (03/26/2000) gkh
161 * Split driver up into device specific pieces.
163 * (03/19/2000) gkh
164 * Fixed oops that could happen when device was removed while a program
165 * was talking to the device.
166 * Removed the static urbs and now all urbs are created and destroyed
167 * dynamically.
168 * Reworked the internal interface. Now everything is based on the
169 * usb_serial_port structure instead of the larger usb_serial structure.
170 * This fixes the bug that a multiport device could not have more than
171 * one port open at one time.
173 * (03/17/2000) gkh
174 * Added config option for debugging messages.
175 * Added patch for keyspan pda from Brian Warner.
177 * (03/06/2000) gkh
178 * Added the keyspan pda code from Brian Warner <warner@lothar.com>
179 * Moved a bunch of the port specific stuff into its own structure. This
180 * is in anticipation of the true multiport devices (there's a bug if you
181 * try to access more than one port of any multiport device right now)
183 * (02/21/2000) gkh
184 * Made it so that any serial devices only have to specify which functions
185 * they want to overload from the generic function calls (great,
186 * inheritance in C, in a driver, just what I wanted...)
187 * Added support for set_termios and ioctl function calls. No drivers take
188 * advantage of this yet.
189 * Removed the #ifdef MODULE, now there is no module specific code.
190 * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
191 * to Miles Lott).
192 * Small fix to get_free_serial.
194 * (02/14/2000) gkh
195 * Removed the Belkin and Peracom functionality from the driver due to
196 * the lack of support from the vendor, and me not wanting people to
197 * accidenatly buy the device, expecting it to work with Linux.
198 * Added read_bulk_callback and write_bulk_callback to the type structure
199 * for the needs of the FTDI and WhiteHEAT driver.
200 * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
201 * Ryder.
202 * Changed the output urb size back to the max endpoint size to make
203 * the ftdi_sio driver have it easier, and due to the fact that it didn't
204 * really increase the speed any.
206 * (02/11/2000) gkh
207 * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
208 * patch from Miles Lott (milos@insync.net).
209 * Fixed bug with not restoring the minor range that a device grabs, if
210 * the startup function fails (thanks Miles for finding this).
212 * (02/05/2000) gkh
213 * Added initial framework for the Keyspan PDA serial converter so that
214 * Brian Warner has a place to put his code.
215 * Made the ezusb specific functions generic enough that different
216 * devices can use them (whiteheat and keyspan_pda both need them).
217 * Split out a whole bunch of structure and other stuff to a seperate
218 * usb-serial.h file.
219 * Made the Visor connection messages a little more understandable, now
220 * that Miles Lott (milos@insync.net) has gotten the Generic channel to
221 * work. Also made them always show up in the log file.
223 * (01/25/2000) gkh
224 * Added initial framework for FTDI serial converter so that Bill Ryder
225 * has a place to put his code.
226 * Added the vendor specific info from Handspring. Now we can print out
227 * informational debug messages as well as understand what is happening.
229 * (01/23/2000) gkh
230 * Fixed problem of crash when trying to open a port that didn't have a
231 * device assigned to it. Made the minor node finding a little smarter,
232 * now it looks to find a continous space for the new device.
234 * (01/21/2000) gkh
235 * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
236 * Fixed get_serial_by_minor which was all messed up for multi port
237 * devices. Fixed multi port problem for generic devices. Now the number
238 * of ports is determined by the number of bulk out endpoints for the
239 * generic device.
241 * (01/19/2000) gkh
242 * Removed lots of cruft that was around from the old (pre urb) driver
243 * interface.
244 * Made the serial_table dynamic. This should save lots of memory when
245 * the number of minor nodes goes up to 256.
246 * Added initial support for devices that have more than one port.
247 * Added more debugging comments for the Visor, and added a needed
248 * set_configuration call.
250 * (01/17/2000) gkh
251 * Fixed the WhiteHEAT firmware (my processing tool had a bug)
252 * and added new debug loader firmware for it.
253 * Removed the put_char function as it isn't really needed.
254 * Added visor startup commands as found by the Win98 dump.
256 * (01/13/2000) gkh
257 * Fixed the vendor id for the generic driver to the one I meant it to be.
259 * (01/12/2000) gkh
260 * Forget the version numbering...that's pretty useless...
261 * Made the driver able to be compiled so that the user can select which
262 * converter they want to use. This allows people who only want the Visor
263 * support to not pay the memory size price of the WhiteHEAT.
264 * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
265 * grabbed the root hub. Not good.
267 * version 0.4.0 (01/10/2000) gkh
268 * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
269 * device. Added startup function to allow firmware to be downloaded to
270 * a device if it needs to be.
271 * Added firmware download logic to the WhiteHEAT device.
272 * Started to add #defines to split up the different drivers for potential
273 * configuration option.
275 * version 0.3.1 (12/30/99) gkh
276 * Fixed problems with urb for bulk out.
277 * Added initial support for multiple sets of endpoints. This enables
278 * the Handspring Visor to be attached successfully. Only the first
279 * bulk in / bulk out endpoint pair is being used right now.
281 * version 0.3.0 (12/27/99) gkh
282 * Added initial support for the Handspring Visor based on a patch from
283 * Miles Lott (milos@sneety.insync.net)
284 * Cleaned up the code a bunch and converted over to using urbs only.
286 * version 0.2.3 (12/21/99) gkh
287 * Added initial support for the Connect Tech WhiteHEAT converter.
288 * Incremented the number of ports in expectation of getting the
289 * WhiteHEAT to work properly (4 ports per connection).
290 * Added notification on insertion and removal of what port the
291 * device is/was connected to (and what kind of device it was).
293 * version 0.2.2 (12/16/99) gkh
294 * Changed major number to the new allocated number. We're legal now!
296 * version 0.2.1 (12/14/99) gkh
297 * Fixed bug that happens when device node is opened when there isn't a
298 * device attached to it. Thanks to marek@webdesign.no for noticing this.
300 * version 0.2.0 (11/10/99) gkh
301 * Split up internals to make it easier to add different types of serial
302 * converters to the code.
303 * Added a "generic" driver that gets it's vendor and product id
304 * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
305 * for the idea and sample code (from the usb scanner driver.)
306 * Cleared up any licensing questions by releasing it under the GNU GPL.
308 * version 0.1.2 (10/25/99) gkh
309 * Fixed bug in detecting device.
311 * version 0.1.1 (10/05/99) gkh
312 * Changed the major number to not conflict with anything else.
314 * version 0.1 (09/28/99) gkh
315 * Can recognize the two different devices and start up a read from
316 * device when asked to. Writes also work. No control signals yet, this
317 * all is vendor specific data (i.e. no spec), also no control for
318 * different baud rates or other bit settings.
319 * Currently we are using the same devid as the acm driver. This needs
320 * to change.
324 #include <linux/config.h>
325 #include <linux/kernel.h>
326 #include <linux/errno.h>
327 #include <linux/init.h>
328 #include <linux/slab.h>
329 #include <linux/tty.h>
330 #include <linux/tty_driver.h>
331 #include <linux/tty_flip.h>
332 #include <linux/module.h>
333 #include <linux/spinlock.h>
334 #include <linux/list.h>
335 #include <linux/smp_lock.h>
336 #include <asm/uaccess.h>
337 #include <linux/usb.h>
340 #ifdef CONFIG_USB_SERIAL_DEBUG
341 static int debug = 1;
342 #else
343 static int debug;
344 #endif
346 #include "usb-serial.h"
347 #include "pl2303.h"
350 * Version Information
352 #define DRIVER_VERSION "v2.0"
353 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
354 #define DRIVER_DESC "USB Serial Driver core"
357 #ifdef CONFIG_USB_SERIAL_GENERIC
358 /* we want to look at all devices, as the vendor/product id can change
359 * depending on the command line argument */
360 static struct usb_device_id generic_serial_ids[] = {
361 {.driver_info = 42},
365 #endif /* CONFIG_USB_SERIAL_GENERIC */
367 /* Driver structure we register with the USB core */
368 static struct usb_driver usb_serial_driver = {
369 .name = "usbserial",
370 .probe = usb_serial_probe,
371 .disconnect = usb_serial_disconnect,
372 #ifdef CONFIG_USB_SERIAL_GENERIC
373 .id_table = generic_serial_ids,
374 #endif
377 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
378 the MODULE_DEVICE_TABLE declarations in each serial driver
379 cause the "hotplug" program to pull in whatever module is necessary
380 via modprobe, and modprobe will load usbserial because the serial
381 drivers depend on it.
384 static int serial_refcount;
385 static struct tty_struct * serial_tty[SERIAL_TTY_MINORS];
386 static struct termios * serial_termios[SERIAL_TTY_MINORS];
387 static struct termios * serial_termios_locked[SERIAL_TTY_MINORS];
388 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
389 static LIST_HEAD(usb_serial_driver_list);
392 struct usb_serial *usb_serial_get_by_minor (unsigned int minor)
394 return serial_table[minor];
397 static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
399 unsigned int i, j;
400 int good_spot;
402 dbg("%s %d", __FUNCTION__, num_ports);
404 *minor = 0;
405 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
406 if (serial_table[i])
407 continue;
409 good_spot = 1;
410 for (j = 1; j <= num_ports-1; ++j)
411 if ((serial_table[i+j]) || (i+j >= SERIAL_TTY_MINORS)) {
412 good_spot = 0;
413 i += j;
414 break;
416 if (good_spot == 0)
417 continue;
419 serial->magic = USB_SERIAL_MAGIC;
420 *minor = i;
421 dbg("%s - minor base = %d", __FUNCTION__, *minor);
422 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
423 serial_table[i] = serial;
424 return serial;
426 return NULL;
429 static void return_serial (struct usb_serial *serial)
431 int i;
433 dbg("%s", __FUNCTION__);
435 if (serial == NULL)
436 return;
438 for (i = 0; i < serial->num_ports; ++i) {
439 serial_table[serial->minor + i] = NULL;
442 return;
445 /*****************************************************************************
446 * Driver tty interface functions
447 *****************************************************************************/
448 static int serial_open (struct tty_struct *tty, struct file * filp)
450 struct usb_serial *serial;
451 struct usb_serial_port *port;
452 unsigned int portNumber;
453 int retval = 0;
455 dbg("%s", __FUNCTION__);
457 /* initialize the pointer incase something fails */
458 tty->driver_data = NULL;
460 /* get the serial object associated with this tty pointer */
461 serial = usb_serial_get_by_minor (minor(tty->device));
463 if (serial_paranoia_check (serial, __FUNCTION__))
464 return -ENODEV;
466 /* set up our port structure making the tty driver remember our port object, and us it */
467 portNumber = minor(tty->device) - serial->minor;
468 port = &serial->port[portNumber];
469 tty->driver_data = port;
471 down (&port->sem);
472 port->tty = tty;
474 /* lock this module before we call it,
475 this may, which means we must bail out, safe because we are called with BKL held */
476 if (!try_module_get(serial->type->owner)) {
477 retval = -ENODEV;
478 goto bailout;
481 ++port->open_count;
482 if (port->open_count == 1) {
483 /* only call the device specific open if this
484 * is the first time the port is opened */
485 if (serial->type->open)
486 retval = serial->type->open(port, filp);
487 else
488 retval = usb_serial_generic_open(port, filp);
489 if (retval) {
490 port->open_count = 0;
491 module_put(serial->type->owner);
494 bailout:
496 up (&port->sem);
497 return retval;
500 static void __serial_close(struct usb_serial_port *port, struct file *filp)
502 if (!port->open_count) {
503 dbg ("%s - port not opened", __FUNCTION__);
504 return;
507 --port->open_count;
508 if (port->open_count <= 0) {
509 /* only call the device specific close if this
510 * port is being closed by the last owner */
511 if (port->serial->type->close)
512 port->serial->type->close(port, filp);
513 else
514 usb_serial_generic_close(port, filp);
515 port->open_count = 0;
518 module_put(port->serial->type->owner);
521 static void serial_close(struct tty_struct *tty, struct file * filp)
523 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
524 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
526 if (!serial)
527 return;
529 down (&port->sem);
531 dbg("%s - port %d", __FUNCTION__, port->number);
533 /* if disconnect beat us to the punch here, there's nothing to do */
534 if (tty->driver_data) {
535 __serial_close(port, filp);
538 up (&port->sem);
541 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
543 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
544 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
545 int retval = -EINVAL;
547 if (!serial)
548 return -ENODEV;
550 down (&port->sem);
552 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
554 if (!port->open_count) {
555 dbg("%s - port not opened", __FUNCTION__);
556 goto exit;
559 /* pass on to the driver specific version of this function if it is available */
560 if (serial->type->write)
561 retval = serial->type->write(port, from_user, buf, count);
562 else
563 retval = usb_serial_generic_write(port, from_user, buf, count);
565 exit:
566 up (&port->sem);
567 return retval;
570 static int serial_write_room (struct tty_struct *tty)
572 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
573 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
574 int retval = -EINVAL;
576 if (!serial)
577 return -ENODEV;
579 down (&port->sem);
581 dbg("%s - port %d", __FUNCTION__, port->number);
583 if (!port->open_count) {
584 dbg("%s - port not open", __FUNCTION__);
585 goto exit;
588 /* pass on to the driver specific version of this function if it is available */
589 if (serial->type->write_room)
590 retval = serial->type->write_room(port);
591 else
592 retval = usb_serial_generic_write_room(port);
594 exit:
595 up (&port->sem);
596 return retval;
599 static int serial_chars_in_buffer (struct tty_struct *tty)
601 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
602 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
603 int retval = -EINVAL;
605 if (!serial)
606 return -ENODEV;
608 down (&port->sem);
610 dbg("%s = port %d", __FUNCTION__, port->number);
612 if (!port->open_count) {
613 dbg("%s - port not open", __FUNCTION__);
614 goto exit;
617 /* pass on to the driver specific version of this function if it is available */
618 if (serial->type->chars_in_buffer)
619 retval = serial->type->chars_in_buffer(port);
620 else
621 retval = usb_serial_generic_chars_in_buffer(port);
623 exit:
624 up (&port->sem);
625 return retval;
628 static void serial_throttle (struct tty_struct * tty)
630 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
631 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
633 if (!serial)
634 return;
636 down (&port->sem);
638 dbg("%s - port %d", __FUNCTION__, port->number);
640 if (!port->open_count) {
641 dbg ("%s - port not open", __FUNCTION__);
642 goto exit;
645 /* pass on to the driver specific version of this function */
646 if (serial->type->throttle)
647 serial->type->throttle(port);
649 exit:
650 up (&port->sem);
653 static void serial_unthrottle (struct tty_struct * tty)
655 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
656 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
658 if (!serial)
659 return;
661 down (&port->sem);
663 dbg("%s - port %d", __FUNCTION__, port->number);
665 if (!port->open_count) {
666 dbg("%s - port not open", __FUNCTION__);
667 goto exit;
670 /* pass on to the driver specific version of this function */
671 if (serial->type->unthrottle)
672 serial->type->unthrottle(port);
674 exit:
675 up (&port->sem);
678 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
680 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
681 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
682 int retval = -ENODEV;
684 if (!serial)
685 return -ENODEV;
687 down (&port->sem);
689 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
691 if (!port->open_count) {
692 dbg ("%s - port not open", __FUNCTION__);
693 goto exit;
696 /* pass on to the driver specific version of this function if it is available */
697 if (serial->type->ioctl)
698 retval = serial->type->ioctl(port, file, cmd, arg);
699 else
700 retval = -ENOIOCTLCMD;
702 exit:
703 up (&port->sem);
704 return retval;
707 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
709 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
710 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
712 if (!serial)
713 return;
715 down (&port->sem);
717 dbg("%s - port %d", __FUNCTION__, port->number);
719 if (!port->open_count) {
720 dbg("%s - port not open", __FUNCTION__);
721 goto exit;
724 /* pass on to the driver specific version of this function if it is available */
725 if (serial->type->set_termios)
726 serial->type->set_termios(port, old);
728 exit:
729 up (&port->sem);
732 static void serial_break (struct tty_struct *tty, int break_state)
734 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
735 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
737 if (!serial)
738 return;
740 down (&port->sem);
742 dbg("%s - port %d", __FUNCTION__, port->number);
744 if (!port->open_count) {
745 dbg("%s - port not open", __FUNCTION__);
746 goto exit;
749 /* pass on to the driver specific version of this function if it is available */
750 if (serial->type->break_ctl)
751 serial->type->break_ctl(port, break_state);
753 exit:
754 up (&port->sem);
757 static void serial_shutdown (struct usb_serial *serial)
759 dbg ("%s", __FUNCTION__);
761 if (serial->type->shutdown)
762 serial->type->shutdown(serial);
763 else
764 usb_serial_generic_shutdown(serial);
767 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
769 struct usb_serial *serial;
770 int length = 0;
771 int i;
772 off_t begin = 0;
773 char tmp[40];
775 dbg("%s", __FUNCTION__);
776 length += sprintf (page, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION);
777 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
778 serial = usb_serial_get_by_minor(i);
779 if (serial == NULL)
780 continue;
782 length += sprintf (page+length, "%d:", i);
783 if (serial->type->owner)
784 length += sprintf (page+length, " module:%s", module_name(serial->type->owner));
785 length += sprintf (page+length, " name:\"%s\"", serial->type->name);
786 length += sprintf (page+length, " vendor:%04x product:%04x", serial->vendor, serial->product);
787 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
788 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
790 usb_make_path(serial->dev, tmp, sizeof(tmp));
791 length += sprintf (page+length, " path:%s", tmp);
793 length += sprintf (page+length, "\n");
794 if ((length + begin) > (off + count))
795 goto done;
796 if ((length + begin) < off) {
797 begin += length;
798 length = 0;
801 *eof = 1;
802 done:
803 if (off >= (length + begin))
804 return 0;
805 *start = page + (off-begin);
806 return ((count < begin+length-off) ? count : begin+length-off);
809 void usb_serial_port_softint(void *private)
811 struct usb_serial_port *port = (struct usb_serial_port *)private;
812 struct usb_serial *serial;
813 struct tty_struct *tty;
815 dbg("%s - port %d", __FUNCTION__, port->number);
817 if (!port)
818 return;
820 serial = get_usb_serial (port, __FUNCTION__);
821 if (!serial)
822 return;
824 tty = port->tty;
825 if (!tty)
826 return;
828 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
829 dbg("%s - write wakeup call.", __FUNCTION__);
830 (tty->ldisc.write_wakeup)(tty);
833 wake_up_interruptible(&tty->write_wait);
836 static struct usb_serial * create_serial (struct usb_device *dev,
837 struct usb_interface *interface,
838 struct usb_serial_device_type *type)
840 struct usb_serial *serial;
842 serial = kmalloc (sizeof (*serial), GFP_KERNEL);
843 if (!serial) {
844 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
845 return NULL;
847 memset (serial, 0, sizeof(*serial));
848 serial->dev = dev;
849 serial->type = type;
850 serial->interface = interface;
851 serial->vendor = dev->descriptor.idVendor;
852 serial->product = dev->descriptor.idProduct;
854 return serial;
857 int usb_serial_probe(struct usb_interface *interface,
858 const struct usb_device_id *id)
860 struct usb_device *dev = interface_to_usbdev (interface);
861 struct usb_serial *serial = NULL;
862 struct usb_serial_port *port;
863 struct usb_host_interface *iface_desc;
864 struct usb_endpoint_descriptor *endpoint;
865 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
866 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
867 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
868 struct usb_serial_device_type *type = NULL;
869 struct list_head *tmp;
870 int retval;
871 int found;
872 int minor;
873 int buffer_size;
874 int i;
875 int num_interrupt_in = 0;
876 int num_bulk_in = 0;
877 int num_bulk_out = 0;
878 int num_ports = 0;
879 int max_endpoints;
880 const struct usb_device_id *id_pattern = NULL;
882 /* loop through our list of known serial converters, and see if this
883 device matches. */
884 found = 0;
885 list_for_each (tmp, &usb_serial_driver_list) {
886 type = list_entry(tmp, struct usb_serial_device_type, driver_list);
887 id_pattern = usb_match_id(interface, type->id_table);
888 if (id_pattern != NULL) {
889 dbg("descriptor matches");
890 found = 1;
891 break;
894 if (!found) {
895 /* no match */
896 dbg("none matched");
897 return -ENODEV;
900 serial = create_serial (dev, interface, type);
901 if (!serial) {
902 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
903 return -ENODEV;
906 /* if this device type has a probe function, call it */
907 if (type->probe) {
908 if (!try_module_get(type->owner)) {
909 dev_err(&interface->dev, "module get failed, exiting\n");
910 kfree (serial);
911 return -EIO;
913 retval = type->probe (serial, id);
914 module_put(type->owner);
916 if (retval < 0) {
917 dbg ("sub driver rejected device");
918 kfree (serial);
919 return -ENODEV;
923 /* descriptor matches, let's find the endpoints needed */
924 /* check out the endpoints */
925 iface_desc = &interface->altsetting[0];
926 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
927 endpoint = &iface_desc->endpoint[i].desc;
929 if ((endpoint->bEndpointAddress & 0x80) &&
930 ((endpoint->bmAttributes & 3) == 0x02)) {
931 /* we found a bulk in endpoint */
932 dbg("found bulk in");
933 bulk_in_endpoint[num_bulk_in] = endpoint;
934 ++num_bulk_in;
937 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
938 ((endpoint->bmAttributes & 3) == 0x02)) {
939 /* we found a bulk out endpoint */
940 dbg("found bulk out");
941 bulk_out_endpoint[num_bulk_out] = endpoint;
942 ++num_bulk_out;
945 if ((endpoint->bEndpointAddress & 0x80) &&
946 ((endpoint->bmAttributes & 3) == 0x03)) {
947 /* we found a interrupt in endpoint */
948 dbg("found interrupt in");
949 interrupt_in_endpoint[num_interrupt_in] = endpoint;
950 ++num_interrupt_in;
954 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
955 /* BEGIN HORRIBLE HACK FOR PL2303 */
956 /* this is needed due to the looney way its endpoints are set up */
957 if (((dev->descriptor.idVendor == PL2303_VENDOR_ID) &&
958 (dev->descriptor.idProduct == PL2303_PRODUCT_ID)) ||
959 ((dev->descriptor.idVendor == ATEN_VENDOR_ID) &&
960 (dev->descriptor.idProduct == ATEN_PRODUCT_ID))) {
961 if (interface != &dev->actconfig->interface[0]) {
962 /* check out the endpoints of the other interface*/
963 iface_desc = &dev->actconfig->interface[0].altsetting[0];
964 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
965 endpoint = &iface_desc->endpoint[i].desc;
966 if ((endpoint->bEndpointAddress & 0x80) &&
967 ((endpoint->bmAttributes & 3) == 0x03)) {
968 /* we found a interrupt in endpoint */
969 dbg("found interrupt in for Prolific device on separate interface");
970 interrupt_in_endpoint[num_interrupt_in] = endpoint;
971 ++num_interrupt_in;
976 /* Now make sure the PL-2303 is configured correctly.
977 * If not, give up now and hope this hack will work
978 * properly during a later invocation of usb_serial_probe
980 if (num_bulk_in == 0 || num_bulk_out == 0) {
981 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
982 kfree (serial);
983 return -ENODEV;
986 /* END HORRIBLE HACK FOR PL2303 */
987 #endif
989 /* found all that we need */
990 dev_info(&interface->dev, "%s converter detected\n", type->name);
992 #ifdef CONFIG_USB_SERIAL_GENERIC
993 if (type == &usb_serial_generic_device) {
994 num_ports = num_bulk_out;
995 if (num_ports == 0) {
996 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
997 kfree (serial);
998 return -EIO;
1001 #endif
1002 if (!num_ports) {
1003 /* if this device type has a calc_num_ports function, call it */
1004 if (type->calc_num_ports) {
1005 if (!try_module_get(type->owner)) {
1006 dev_err(&interface->dev, "module get failed, exiting\n");
1007 kfree (serial);
1008 return -EIO;
1010 num_ports = type->calc_num_ports (serial);
1011 module_put(type->owner);
1013 if (!num_ports)
1014 num_ports = type->num_ports;
1017 if (get_free_serial (serial, num_ports, &minor) == NULL) {
1018 dev_err(&interface->dev, "No more free serial devices\n");
1019 kfree (serial);
1020 return -ENOMEM;
1023 serial->minor = minor;
1024 serial->num_ports = num_ports;
1025 serial->num_bulk_in = num_bulk_in;
1026 serial->num_bulk_out = num_bulk_out;
1027 serial->num_interrupt_in = num_interrupt_in;
1029 /* set up the endpoint information */
1030 for (i = 0; i < num_bulk_in; ++i) {
1031 endpoint = bulk_in_endpoint[i];
1032 port = &serial->port[i];
1033 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
1034 if (!port->read_urb) {
1035 dev_err(&interface->dev, "No free urbs available\n");
1036 goto probe_error;
1038 buffer_size = endpoint->wMaxPacketSize;
1039 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1040 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1041 if (!port->bulk_in_buffer) {
1042 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
1043 goto probe_error;
1045 usb_fill_bulk_urb (port->read_urb, dev,
1046 usb_rcvbulkpipe (dev,
1047 endpoint->bEndpointAddress),
1048 port->bulk_in_buffer, buffer_size,
1049 ((serial->type->read_bulk_callback) ?
1050 serial->type->read_bulk_callback :
1051 usb_serial_generic_read_bulk_callback),
1052 port);
1055 for (i = 0; i < num_bulk_out; ++i) {
1056 endpoint = bulk_out_endpoint[i];
1057 port = &serial->port[i];
1058 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
1059 if (!port->write_urb) {
1060 dev_err(&interface->dev, "No free urbs available\n");
1061 goto probe_error;
1063 buffer_size = endpoint->wMaxPacketSize;
1064 port->bulk_out_size = buffer_size;
1065 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1066 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
1067 if (!port->bulk_out_buffer) {
1068 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
1069 goto probe_error;
1071 usb_fill_bulk_urb (port->write_urb, dev,
1072 usb_sndbulkpipe (dev,
1073 endpoint->bEndpointAddress),
1074 port->bulk_out_buffer, buffer_size,
1075 ((serial->type->write_bulk_callback) ?
1076 serial->type->write_bulk_callback :
1077 usb_serial_generic_write_bulk_callback),
1078 port);
1081 for (i = 0; i < num_interrupt_in; ++i) {
1082 endpoint = interrupt_in_endpoint[i];
1083 port = &serial->port[i];
1084 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1085 if (!port->interrupt_in_urb) {
1086 dev_err(&interface->dev, "No free urbs available\n");
1087 goto probe_error;
1089 buffer_size = endpoint->wMaxPacketSize;
1090 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1091 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1092 if (!port->interrupt_in_buffer) {
1093 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
1094 goto probe_error;
1096 usb_fill_int_urb (port->interrupt_in_urb, dev,
1097 usb_rcvintpipe (dev,
1098 endpoint->bEndpointAddress),
1099 port->interrupt_in_buffer, buffer_size,
1100 serial->type->read_int_callback, port,
1101 endpoint->bInterval);
1104 /* initialize some parts of the port structures */
1105 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1106 max_endpoints = max(num_bulk_in, num_bulk_out);
1107 max_endpoints = max(max_endpoints, num_interrupt_in);
1108 max_endpoints = max(max_endpoints, (int)serial->num_ports);
1109 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
1110 for (i = 0; i < max_endpoints; ++i) {
1111 port = &serial->port[i];
1112 port->number = i + serial->minor;
1113 port->serial = serial;
1114 port->magic = USB_SERIAL_PORT_MAGIC;
1115 INIT_WORK(&port->work, usb_serial_port_softint, port);
1116 init_MUTEX (&port->sem);
1119 /* if this device type has an attach function, call it */
1120 if (type->attach) {
1121 if (!try_module_get(type->owner)) {
1122 dev_err(&interface->dev, "module get failed, exiting\n");
1123 goto probe_error;
1125 retval = type->attach (serial);
1126 module_put(type->owner);
1127 if (retval < 0)
1128 goto probe_error;
1129 if (retval > 0) {
1130 /* quietly accept this device, but don't bind to a serial port
1131 * as it's about to disappear */
1132 goto exit;
1136 /* register all of the individual ports with the driver core */
1137 for (i = 0; i < num_ports; ++i) {
1138 port = &serial->port[i];
1139 port->dev.parent = &serial->dev->dev;
1140 port->dev.driver = NULL;
1141 port->dev.bus = &usb_serial_bus_type;
1143 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
1144 snprintf (&port->dev.name[0], sizeof(port->dev.name), "usb serial port %d", port->number);
1145 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
1146 device_register (&port->dev);
1149 usb_serial_console_init (debug, minor);
1151 exit:
1152 /* success */
1153 usb_set_intfdata (interface, serial);
1154 return 0;
1157 probe_error:
1158 for (i = 0; i < num_bulk_in; ++i) {
1159 port = &serial->port[i];
1160 if (port->read_urb)
1161 usb_free_urb (port->read_urb);
1162 if (port->bulk_in_buffer)
1163 kfree (port->bulk_in_buffer);
1165 for (i = 0; i < num_bulk_out; ++i) {
1166 port = &serial->port[i];
1167 if (port->write_urb)
1168 usb_free_urb (port->write_urb);
1169 if (port->bulk_out_buffer)
1170 kfree (port->bulk_out_buffer);
1172 for (i = 0; i < num_interrupt_in; ++i) {
1173 port = &serial->port[i];
1174 if (port->interrupt_in_urb)
1175 usb_free_urb (port->interrupt_in_urb);
1176 if (port->interrupt_in_buffer)
1177 kfree (port->interrupt_in_buffer);
1180 /* return the minor range that this device had */
1181 return_serial (serial);
1183 /* free up any memory that we allocated */
1184 kfree (serial);
1185 return -EIO;
1188 void usb_serial_disconnect(struct usb_interface *interface)
1190 struct usb_serial *serial = usb_get_intfdata (interface);
1191 struct device *dev = &interface->dev;
1192 struct usb_serial_port *port;
1193 int i;
1195 dbg ("%s", __FUNCTION__);
1197 usb_set_intfdata (interface, NULL);
1198 if (serial) {
1199 /* fail all future close/read/write/ioctl/etc calls */
1200 for (i = 0; i < serial->num_ports; ++i) {
1201 port = &serial->port[i];
1202 down (&port->sem);
1203 if (port->tty != NULL) {
1204 port->tty->driver_data = NULL;
1205 while (port->open_count > 0) {
1206 __serial_close(port, NULL);
1209 up (&port->sem);
1212 serial->dev = NULL;
1213 serial_shutdown (serial);
1215 for (i = 0; i < serial->num_ports; ++i)
1216 device_unregister(&serial->port[i].dev);
1218 for (i = 0; i < serial->num_ports; ++i)
1219 serial->port[i].open_count = 0;
1221 for (i = 0; i < serial->num_bulk_in; ++i) {
1222 port = &serial->port[i];
1223 if (port->read_urb) {
1224 usb_unlink_urb (port->read_urb);
1225 usb_free_urb (port->read_urb);
1227 if (port->bulk_in_buffer)
1228 kfree (port->bulk_in_buffer);
1230 for (i = 0; i < serial->num_bulk_out; ++i) {
1231 port = &serial->port[i];
1232 if (port->write_urb) {
1233 usb_unlink_urb (port->write_urb);
1234 usb_free_urb (port->write_urb);
1236 if (port->bulk_out_buffer)
1237 kfree (port->bulk_out_buffer);
1239 for (i = 0; i < serial->num_interrupt_in; ++i) {
1240 port = &serial->port[i];
1241 if (port->interrupt_in_urb) {
1242 usb_unlink_urb (port->interrupt_in_urb);
1243 usb_free_urb (port->interrupt_in_urb);
1245 if (port->interrupt_in_buffer)
1246 kfree (port->interrupt_in_buffer);
1248 /* return the minor range that this device had */
1249 return_serial (serial);
1251 /* free up any memory that we allocated */
1252 kfree (serial);
1254 dev_info(dev, "device disconnected\n");
1258 struct tty_driver usb_serial_tty_driver = {
1259 .magic = TTY_DRIVER_MAGIC,
1260 .driver_name = "usbserial",
1261 #ifndef CONFIG_DEVFS_FS
1262 .name = "ttyUSB",
1263 #else
1264 .name = "usb/tts/%d",
1265 #endif
1266 .major = SERIAL_TTY_MAJOR,
1267 .minor_start = 0,
1268 .num = SERIAL_TTY_MINORS,
1269 .type = TTY_DRIVER_TYPE_SERIAL,
1270 .subtype = SERIAL_TYPE_NORMAL,
1271 .flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1273 .refcount = &serial_refcount,
1274 .table = serial_tty,
1275 .termios = serial_termios,
1276 .termios_locked = serial_termios_locked,
1278 .open = serial_open,
1279 .close = serial_close,
1280 .write = serial_write,
1281 .write_room = serial_write_room,
1282 .ioctl = serial_ioctl,
1283 .set_termios = serial_set_termios,
1284 .throttle = serial_throttle,
1285 .unthrottle = serial_unthrottle,
1286 .break_ctl = serial_break,
1287 .chars_in_buffer = serial_chars_in_buffer,
1288 .read_proc = serial_read_proc,
1292 static int __init usb_serial_init(void)
1294 int i;
1295 int result = 0;
1297 /* Initalize our global data */
1298 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1299 serial_table[i] = NULL;
1302 bus_register(&usb_serial_bus_type);
1304 /* register the generic driver, if we should */
1305 result = usb_serial_generic_register(debug);
1306 if (result < 0) {
1307 err("%s - registering generic driver failed", __FUNCTION__);
1308 goto exit;
1311 /* register the tty driver */
1312 usb_serial_tty_driver.init_termios = tty_std_termios;
1313 usb_serial_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1314 result = tty_register_driver (&usb_serial_tty_driver);
1315 if (result) {
1316 err("%s - tty_register_driver failed", __FUNCTION__);
1317 goto exit_generic;
1320 /* register the USB driver */
1321 result = usb_register(&usb_serial_driver);
1322 if (result < 0) {
1323 err("%s - usb_register failed", __FUNCTION__);
1324 goto exit_tty;
1327 info(DRIVER_DESC " " DRIVER_VERSION);
1329 return result;
1331 exit_tty:
1332 tty_unregister_driver(&usb_serial_tty_driver);
1334 exit_generic:
1335 usb_serial_generic_deregister();
1337 exit:
1338 err ("%s - returning with error %d", __FUNCTION__, result);
1339 return result;
1343 static void __exit usb_serial_exit(void)
1345 usb_serial_console_exit();
1347 usb_serial_generic_deregister();
1349 usb_deregister(&usb_serial_driver);
1350 tty_unregister_driver(&usb_serial_tty_driver);
1351 bus_unregister(&usb_serial_bus_type);
1355 module_init(usb_serial_init);
1356 module_exit(usb_serial_exit);
1359 int usb_serial_register(struct usb_serial_device_type *new_device)
1361 int retval;
1363 /* Add this device to our list of devices */
1364 list_add(&new_device->driver_list, &usb_serial_driver_list);
1366 retval = usb_serial_bus_register (new_device);
1368 if (retval)
1369 goto error;
1371 info("USB Serial support registered for %s", new_device->name);
1373 return retval;
1374 error:
1375 err("problem %d when registering driver %s", retval, new_device->name);
1376 list_del(&new_device->driver_list);
1378 return retval;
1382 void usb_serial_deregister(struct usb_serial_device_type *device)
1384 struct usb_serial *serial;
1385 int i;
1387 info("USB Serial deregistering driver %s", device->name);
1389 /* clear out the serial_table if the device is attached to a port */
1390 for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
1391 serial = serial_table[i];
1392 if ((serial != NULL) && (serial->type == device)) {
1393 usb_driver_release_interface (&usb_serial_driver, serial->interface);
1394 usb_serial_disconnect (serial->interface);
1398 list_del(&device->driver_list);
1399 usb_serial_bus_deregister (device);
1404 /* If the usb-serial core is built into the core, the usb-serial drivers
1405 need these symbols to load properly as modules. */
1406 EXPORT_SYMBOL(usb_serial_register);
1407 EXPORT_SYMBOL(usb_serial_deregister);
1408 EXPORT_SYMBOL(usb_serial_probe);
1409 EXPORT_SYMBOL(usb_serial_disconnect);
1410 EXPORT_SYMBOL(usb_serial_port_softint);
1413 /* Module information */
1414 MODULE_AUTHOR( DRIVER_AUTHOR );
1415 MODULE_DESCRIPTION( DRIVER_DESC );
1416 MODULE_LICENSE("GPL");
1418 MODULE_PARM(debug, "i");
1419 MODULE_PARM_DESC(debug, "Debug enabled or not");