2 * USB Serial Converter driver
4 * Copyright (C) 1999 - 2004 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
18 * Split the ports off into their own struct device, and added a
19 * usb-serial bus driver.
22 * removed a few #ifdefs for the generic code and cleaned up the failure
23 * logic in initialization.
26 * moved the console code to console.c and out of this file.
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.
34 * added serial_read_proc function which creates a
35 * /proc/tty/driver/usb-serial file.
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.
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.
51 * Moved all locking into the main serial_* functions, instead of having
52 * the individual drivers have to grab the port semaphore. This should
54 * Reworked the MOD_INC logic a bit to always increment and decrement, even
55 * if the generic driver is being used.
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
63 * Moved generic driver initialize after we have registered with the USB
64 * core. Thanks to Randy Dunlap for pointing this problem out.
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.
74 * added evil hack that is needed for the prolific pl2303 device due to the
75 * crazy way its endpoints are set up.
78 * switched from using spinlock to a semaphore, which fixes lots of problems.
81 * Identify version on module load.
84 * Fixed buffer overflows bug with the generic serial driver. Thanks to
85 * Todd Squires <squirest@ct0.com> for fixing this.
88 * Fixed bug where the generic serial adaptor grabbed _any_ device that was
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.)
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.
106 * Fixed bug with urb->dev not being set properly, now that the usb
110 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
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.
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.
124 * Added more debugging to serial_ioctl call
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.
131 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
134 * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
135 * removed from the individual device source files.
138 * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
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.
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.
152 * Added driver for ZyXEL omni.net lcd plus ISDN TA
153 * Made startup info message specify which drivers were compiled in.
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.
161 * Split driver up into device specific pieces.
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
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.
174 * Added config option for debugging messages.
175 * Added patch for keyspan pda from Brian Warner.
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)
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
192 * Small fix to get_free_serial.
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
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.
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).
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 separate
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.
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.
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 continuous space for the new device.
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
242 * Removed lots of cruft that was around from the old (pre urb) driver
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.
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.
257 * Fixed the vendor id for the generic driver to the one I meant it to be.
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
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/moduleparam.h>
334 #include <linux/spinlock.h>
335 #include <linux/list.h>
336 #include <linux/smp_lock.h>
337 #include <asm/uaccess.h>
338 #include <linux/usb.h>
339 #include "usb-serial.h"
343 * Version Information
345 #define DRIVER_VERSION "v2.0"
346 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
347 #define DRIVER_DESC "USB Serial Driver core"
349 /* Driver structure we register with the USB core */
350 static struct usb_driver usb_serial_driver
= {
351 .owner
= THIS_MODULE
,
353 .probe
= usb_serial_probe
,
354 .disconnect
= usb_serial_disconnect
,
357 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
358 the MODULE_DEVICE_TABLE declarations in each serial driver
359 cause the "hotplug" program to pull in whatever module is necessary
360 via modprobe, and modprobe will load usbserial because the serial
361 drivers depend on it.
365 static struct usb_serial
*serial_table
[SERIAL_TTY_MINORS
]; /* initially all NULL */
366 static LIST_HEAD(usb_serial_driver_list
);
368 struct usb_serial
*usb_serial_get_by_index(unsigned index
)
370 struct usb_serial
*serial
= serial_table
[index
];
373 kref_get(&serial
->kref
);
377 static struct usb_serial
*get_free_serial (struct usb_serial
*serial
, int num_ports
, unsigned int *minor
)
382 dbg("%s %d", __FUNCTION__
, num_ports
);
385 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
390 for (j
= 1; j
<= num_ports
-1; ++j
)
391 if ((i
+j
>= SERIAL_TTY_MINORS
) || (serial_table
[i
+j
])) {
400 dbg("%s - minor base = %d", __FUNCTION__
, *minor
);
401 for (i
= *minor
; (i
< (*minor
+ num_ports
)) && (i
< SERIAL_TTY_MINORS
); ++i
)
402 serial_table
[i
] = serial
;
408 static void return_serial(struct usb_serial
*serial
)
412 dbg("%s", __FUNCTION__
);
417 for (i
= 0; i
< serial
->num_ports
; ++i
) {
418 serial_table
[serial
->minor
+ i
] = NULL
;
422 static void destroy_serial(struct kref
*kref
)
424 struct usb_serial
*serial
;
425 struct usb_serial_port
*port
;
428 serial
= to_usb_serial(kref
);
430 dbg ("%s - %s", __FUNCTION__
, serial
->type
->name
);
432 serial
->type
->shutdown(serial
);
434 /* return the minor range that this device had */
435 return_serial(serial
);
437 for (i
= 0; i
< serial
->num_ports
; ++i
)
438 serial
->port
[i
]->open_count
= 0;
440 /* the ports are cleaned up and released in port_release() */
441 for (i
= 0; i
< serial
->num_ports
; ++i
)
442 if (serial
->port
[i
]->dev
.parent
!= NULL
) {
443 device_unregister(&serial
->port
[i
]->dev
);
444 serial
->port
[i
] = NULL
;
447 /* If this is a "fake" port, we have to clean it up here, as it will
448 * not get cleaned up in port_release() as it was never registered with
450 if (serial
->num_ports
< serial
->num_port_pointers
) {
451 for (i
= serial
->num_ports
; i
< serial
->num_port_pointers
; ++i
) {
452 port
= serial
->port
[i
];
455 usb_kill_urb(port
->read_urb
);
456 usb_free_urb(port
->read_urb
);
457 usb_kill_urb(port
->write_urb
);
458 usb_free_urb(port
->write_urb
);
459 usb_kill_urb(port
->interrupt_in_urb
);
460 usb_free_urb(port
->interrupt_in_urb
);
461 usb_kill_urb(port
->interrupt_out_urb
);
462 usb_free_urb(port
->interrupt_out_urb
);
463 kfree(port
->bulk_in_buffer
);
464 kfree(port
->bulk_out_buffer
);
465 kfree(port
->interrupt_in_buffer
);
466 kfree(port
->interrupt_out_buffer
);
470 usb_put_dev(serial
->dev
);
472 /* free up any memory that we allocated */
476 /*****************************************************************************
477 * Driver tty interface functions
478 *****************************************************************************/
479 static int serial_open (struct tty_struct
*tty
, struct file
* filp
)
481 struct usb_serial
*serial
;
482 struct usb_serial_port
*port
;
483 unsigned int portNumber
;
486 dbg("%s", __FUNCTION__
);
488 /* get the serial object associated with this tty pointer */
489 serial
= usb_serial_get_by_index(tty
->index
);
491 tty
->driver_data
= NULL
;
495 portNumber
= tty
->index
- serial
->minor
;
496 port
= serial
->port
[portNumber
];
500 if (port
->open_count
== 1) {
502 /* set up our port structure making the tty driver
503 * remember our port object, and us it */
504 tty
->driver_data
= port
;
507 /* lock this module before we call it
508 * this may fail, which means we must bail out,
509 * safe because we are called with BKL held */
510 if (!try_module_get(serial
->type
->owner
)) {
512 goto bailout_kref_put
;
515 /* only call the device specific open if this
516 * is the first time the port is opened */
517 retval
= serial
->type
->open(port
, filp
);
519 goto bailout_module_put
;
525 module_put(serial
->type
->owner
);
527 kref_put(&serial
->kref
, destroy_serial
);
528 port
->open_count
= 0;
532 static void serial_close(struct tty_struct
*tty
, struct file
* filp
)
534 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
539 dbg("%s - port %d", __FUNCTION__
, port
->number
);
541 if (port
->open_count
== 0)
545 if (port
->open_count
== 0) {
546 /* only call the device specific close if this
547 * port is being closed by the last owner */
548 port
->serial
->type
->close(port
, filp
);
551 if (port
->tty
->driver_data
)
552 port
->tty
->driver_data
= NULL
;
556 module_put(port
->serial
->type
->owner
);
559 kref_put(&port
->serial
->kref
, destroy_serial
);
562 static int serial_write (struct tty_struct
* tty
, const unsigned char *buf
, int count
)
564 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
565 int retval
= -EINVAL
;
567 dbg("%s - port %d, %d byte(s)", __FUNCTION__
, port
->number
, count
);
569 if (!port
->open_count
) {
570 dbg("%s - port not opened", __FUNCTION__
);
574 /* pass on to the driver specific version of this function */
575 retval
= port
->serial
->type
->write(port
, buf
, count
);
581 static int serial_write_room (struct tty_struct
*tty
)
583 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
584 int retval
= -EINVAL
;
586 dbg("%s - port %d", __FUNCTION__
, port
->number
);
588 if (!port
->open_count
) {
589 dbg("%s - port not open", __FUNCTION__
);
593 /* pass on to the driver specific version of this function */
594 retval
= port
->serial
->type
->write_room(port
);
600 static int serial_chars_in_buffer (struct tty_struct
*tty
)
602 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
603 int retval
= -EINVAL
;
605 dbg("%s = port %d", __FUNCTION__
, port
->number
);
607 if (!port
->open_count
) {
608 dbg("%s - port not open", __FUNCTION__
);
612 /* pass on to the driver specific version of this function */
613 retval
= port
->serial
->type
->chars_in_buffer(port
);
619 static void serial_throttle (struct tty_struct
* tty
)
621 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
623 dbg("%s - port %d", __FUNCTION__
, port
->number
);
625 if (!port
->open_count
) {
626 dbg ("%s - port not open", __FUNCTION__
);
630 /* pass on to the driver specific version of this function */
631 if (port
->serial
->type
->throttle
)
632 port
->serial
->type
->throttle(port
);
635 static void serial_unthrottle (struct tty_struct
* tty
)
637 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
639 dbg("%s - port %d", __FUNCTION__
, port
->number
);
641 if (!port
->open_count
) {
642 dbg("%s - port not open", __FUNCTION__
);
646 /* pass on to the driver specific version of this function */
647 if (port
->serial
->type
->unthrottle
)
648 port
->serial
->type
->unthrottle(port
);
651 static int serial_ioctl (struct tty_struct
*tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
653 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
654 int retval
= -ENODEV
;
656 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__
, port
->number
, cmd
);
658 if (!port
->open_count
) {
659 dbg ("%s - port not open", __FUNCTION__
);
663 /* pass on to the driver specific version of this function if it is available */
664 if (port
->serial
->type
->ioctl
)
665 retval
= port
->serial
->type
->ioctl(port
, file
, cmd
, arg
);
667 retval
= -ENOIOCTLCMD
;
673 static void serial_set_termios (struct tty_struct
*tty
, struct termios
* old
)
675 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
677 dbg("%s - port %d", __FUNCTION__
, port
->number
);
679 if (!port
->open_count
) {
680 dbg("%s - port not open", __FUNCTION__
);
684 /* pass on to the driver specific version of this function if it is available */
685 if (port
->serial
->type
->set_termios
)
686 port
->serial
->type
->set_termios(port
, old
);
689 static void serial_break (struct tty_struct
*tty
, int break_state
)
691 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
693 dbg("%s - port %d", __FUNCTION__
, port
->number
);
695 if (!port
->open_count
) {
696 dbg("%s - port not open", __FUNCTION__
);
700 /* pass on to the driver specific version of this function if it is available */
701 if (port
->serial
->type
->break_ctl
)
702 port
->serial
->type
->break_ctl(port
, break_state
);
705 static int serial_read_proc (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
707 struct usb_serial
*serial
;
713 dbg("%s", __FUNCTION__
);
714 length
+= sprintf (page
, "usbserinfo:1.0 driver:%s\n", DRIVER_VERSION
);
715 for (i
= 0; i
< SERIAL_TTY_MINORS
&& length
< PAGE_SIZE
; ++i
) {
716 serial
= usb_serial_get_by_index(i
);
720 length
+= sprintf (page
+length
, "%d:", i
);
721 if (serial
->type
->owner
)
722 length
+= sprintf (page
+length
, " module:%s", module_name(serial
->type
->owner
));
723 length
+= sprintf (page
+length
, " name:\"%s\"", serial
->type
->name
);
724 length
+= sprintf (page
+length
, " vendor:%04x product:%04x",
725 le16_to_cpu(serial
->dev
->descriptor
.idVendor
),
726 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
727 length
+= sprintf (page
+length
, " num_ports:%d", serial
->num_ports
);
728 length
+= sprintf (page
+length
, " port:%d", i
- serial
->minor
+ 1);
730 usb_make_path(serial
->dev
, tmp
, sizeof(tmp
));
731 length
+= sprintf (page
+length
, " path:%s", tmp
);
733 length
+= sprintf (page
+length
, "\n");
734 if ((length
+ begin
) > (off
+ count
))
736 if ((length
+ begin
) < off
) {
740 kref_put(&serial
->kref
, destroy_serial
);
744 if (off
>= (length
+ begin
))
746 *start
= page
+ (off
-begin
);
747 return ((count
< begin
+length
-off
) ? count
: begin
+length
-off
);
750 static int serial_tiocmget (struct tty_struct
*tty
, struct file
*file
)
752 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
754 dbg("%s - port %d", __FUNCTION__
, port
->number
);
756 if (!port
->open_count
) {
757 dbg("%s - port not open", __FUNCTION__
);
761 if (port
->serial
->type
->tiocmget
)
762 return port
->serial
->type
->tiocmget(port
, file
);
768 static int serial_tiocmset (struct tty_struct
*tty
, struct file
*file
,
769 unsigned int set
, unsigned int clear
)
771 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
773 dbg("%s - port %d", __FUNCTION__
, port
->number
);
775 if (!port
->open_count
) {
776 dbg("%s - port not open", __FUNCTION__
);
780 if (port
->serial
->type
->tiocmset
)
781 return port
->serial
->type
->tiocmset(port
, file
, set
, clear
);
787 void usb_serial_port_softint(void *private)
789 struct usb_serial_port
*port
= (struct usb_serial_port
*)private;
790 struct tty_struct
*tty
;
792 dbg("%s - port %d", __FUNCTION__
, port
->number
);
804 static void port_release(struct device
*dev
)
806 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
808 dbg ("%s - %s", __FUNCTION__
, dev
->bus_id
);
809 usb_kill_urb(port
->read_urb
);
810 usb_free_urb(port
->read_urb
);
811 usb_kill_urb(port
->write_urb
);
812 usb_free_urb(port
->write_urb
);
813 usb_kill_urb(port
->interrupt_in_urb
);
814 usb_free_urb(port
->interrupt_in_urb
);
815 usb_kill_urb(port
->interrupt_out_urb
);
816 usb_free_urb(port
->interrupt_out_urb
);
817 kfree(port
->bulk_in_buffer
);
818 kfree(port
->bulk_out_buffer
);
819 kfree(port
->interrupt_in_buffer
);
820 kfree(port
->interrupt_out_buffer
);
824 static struct usb_serial
* create_serial (struct usb_device
*dev
,
825 struct usb_interface
*interface
,
826 struct usb_serial_device_type
*type
)
828 struct usb_serial
*serial
;
830 serial
= kmalloc (sizeof (*serial
), GFP_KERNEL
);
832 dev_err(&dev
->dev
, "%s - out of memory\n", __FUNCTION__
);
835 memset (serial
, 0, sizeof(*serial
));
836 serial
->dev
= usb_get_dev(dev
);
838 serial
->interface
= interface
;
839 kref_init(&serial
->kref
);
844 static struct usb_serial_device_type
*search_serial_device(struct usb_interface
*iface
)
847 const struct usb_device_id
*id
;
848 struct usb_serial_device_type
*t
;
850 /* List trough know devices and see if the usb id matches */
851 list_for_each(p
, &usb_serial_driver_list
) {
852 t
= list_entry(p
, struct usb_serial_device_type
, driver_list
);
853 id
= usb_match_id(iface
, t
->id_table
);
855 dbg("descriptor matches");
863 int usb_serial_probe(struct usb_interface
*interface
,
864 const struct usb_device_id
*id
)
866 struct usb_device
*dev
= interface_to_usbdev (interface
);
867 struct usb_serial
*serial
= NULL
;
868 struct usb_serial_port
*port
;
869 struct usb_host_interface
*iface_desc
;
870 struct usb_endpoint_descriptor
*endpoint
;
871 struct usb_endpoint_descriptor
*interrupt_in_endpoint
[MAX_NUM_PORTS
];
872 struct usb_endpoint_descriptor
*interrupt_out_endpoint
[MAX_NUM_PORTS
];
873 struct usb_endpoint_descriptor
*bulk_in_endpoint
[MAX_NUM_PORTS
];
874 struct usb_endpoint_descriptor
*bulk_out_endpoint
[MAX_NUM_PORTS
];
875 struct usb_serial_device_type
*type
= NULL
;
880 int num_interrupt_in
= 0;
881 int num_interrupt_out
= 0;
883 int num_bulk_out
= 0;
887 type
= search_serial_device(interface
);
893 serial
= create_serial (dev
, interface
, type
);
895 dev_err(&interface
->dev
, "%s - out of memory\n", __FUNCTION__
);
899 /* if this device type has a probe function, call it */
901 const struct usb_device_id
*id
;
903 if (!try_module_get(type
->owner
)) {
904 dev_err(&interface
->dev
, "module get failed, exiting\n");
909 id
= usb_match_id(interface
, type
->id_table
);
910 retval
= type
->probe(serial
, id
);
911 module_put(type
->owner
);
914 dbg ("sub driver rejected device");
920 /* descriptor matches, let's find the endpoints needed */
921 /* check out the endpoints */
922 iface_desc
= interface
->cur_altsetting
;
923 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
924 endpoint
= &iface_desc
->endpoint
[i
].desc
;
926 if ((endpoint
->bEndpointAddress
& 0x80) &&
927 ((endpoint
->bmAttributes
& 3) == 0x02)) {
928 /* we found a bulk in endpoint */
929 dbg("found bulk in on endpoint %d", i
);
930 bulk_in_endpoint
[num_bulk_in
] = endpoint
;
934 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
935 ((endpoint
->bmAttributes
& 3) == 0x02)) {
936 /* we found a bulk out endpoint */
937 dbg("found bulk out on endpoint %d", i
);
938 bulk_out_endpoint
[num_bulk_out
] = endpoint
;
942 if ((endpoint
->bEndpointAddress
& 0x80) &&
943 ((endpoint
->bmAttributes
& 3) == 0x03)) {
944 /* we found a interrupt in endpoint */
945 dbg("found interrupt in on endpoint %d", i
);
946 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
950 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
951 ((endpoint
->bmAttributes
& 3) == 0x03)) {
952 /* we found an interrupt out endpoint */
953 dbg("found interrupt out on endpoint %d", i
);
954 interrupt_out_endpoint
[num_interrupt_out
] = endpoint
;
959 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
960 /* BEGIN HORRIBLE HACK FOR PL2303 */
961 /* this is needed due to the looney way its endpoints are set up */
962 if (((le16_to_cpu(dev
->descriptor
.idVendor
) == PL2303_VENDOR_ID
) &&
963 (le16_to_cpu(dev
->descriptor
.idProduct
) == PL2303_PRODUCT_ID
)) ||
964 ((le16_to_cpu(dev
->descriptor
.idVendor
) == ATEN_VENDOR_ID
) &&
965 (le16_to_cpu(dev
->descriptor
.idProduct
) == ATEN_PRODUCT_ID
))) {
966 if (interface
!= dev
->actconfig
->interface
[0]) {
967 /* check out the endpoints of the other interface*/
968 iface_desc
= dev
->actconfig
->interface
[0]->cur_altsetting
;
969 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
970 endpoint
= &iface_desc
->endpoint
[i
].desc
;
971 if ((endpoint
->bEndpointAddress
& 0x80) &&
972 ((endpoint
->bmAttributes
& 3) == 0x03)) {
973 /* we found a interrupt in endpoint */
974 dbg("found interrupt in for Prolific device on separate interface");
975 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
981 /* Now make sure the PL-2303 is configured correctly.
982 * If not, give up now and hope this hack will work
983 * properly during a later invocation of usb_serial_probe
985 if (num_bulk_in
== 0 || num_bulk_out
== 0) {
986 dev_info(&interface
->dev
, "PL-2303 hack: descriptors matched but endpoints did not\n");
991 /* END HORRIBLE HACK FOR PL2303 */
994 /* found all that we need */
995 dev_info(&interface
->dev
, "%s converter detected\n", type
->name
);
997 #ifdef CONFIG_USB_SERIAL_GENERIC
998 if (type
== &usb_serial_generic_device
) {
999 num_ports
= num_bulk_out
;
1000 if (num_ports
== 0) {
1001 dev_err(&interface
->dev
, "Generic device with no bulk out, not allowed.\n");
1008 /* if this device type has a calc_num_ports function, call it */
1009 if (type
->calc_num_ports
) {
1010 if (!try_module_get(type
->owner
)) {
1011 dev_err(&interface
->dev
, "module get failed, exiting\n");
1015 num_ports
= type
->calc_num_ports (serial
);
1016 module_put(type
->owner
);
1019 num_ports
= type
->num_ports
;
1022 if (get_free_serial (serial
, num_ports
, &minor
) == NULL
) {
1023 dev_err(&interface
->dev
, "No more free serial devices\n");
1028 serial
->minor
= minor
;
1029 serial
->num_ports
= num_ports
;
1030 serial
->num_bulk_in
= num_bulk_in
;
1031 serial
->num_bulk_out
= num_bulk_out
;
1032 serial
->num_interrupt_in
= num_interrupt_in
;
1033 serial
->num_interrupt_out
= num_interrupt_out
;
1035 /* create our ports, we need as many as the max endpoints */
1036 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1037 max_endpoints
= max(num_bulk_in
, num_bulk_out
);
1038 max_endpoints
= max(max_endpoints
, num_interrupt_in
);
1039 max_endpoints
= max(max_endpoints
, num_interrupt_out
);
1040 max_endpoints
= max(max_endpoints
, (int)serial
->num_ports
);
1041 serial
->num_port_pointers
= max_endpoints
;
1042 dbg("%s - setting up %d port structures for this device", __FUNCTION__
, max_endpoints
);
1043 for (i
= 0; i
< max_endpoints
; ++i
) {
1044 port
= kmalloc(sizeof(struct usb_serial_port
), GFP_KERNEL
);
1047 memset(port
, 0x00, sizeof(struct usb_serial_port
));
1048 port
->number
= i
+ serial
->minor
;
1049 port
->serial
= serial
;
1050 INIT_WORK(&port
->work
, usb_serial_port_softint
, port
);
1051 serial
->port
[i
] = port
;
1054 /* set up the endpoint information */
1055 for (i
= 0; i
< num_bulk_in
; ++i
) {
1056 endpoint
= bulk_in_endpoint
[i
];
1057 port
= serial
->port
[i
];
1058 port
->read_urb
= usb_alloc_urb (0, GFP_KERNEL
);
1059 if (!port
->read_urb
) {
1060 dev_err(&interface
->dev
, "No free urbs available\n");
1063 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1064 port
->bulk_in_size
= buffer_size
;
1065 port
->bulk_in_endpointAddress
= endpoint
->bEndpointAddress
;
1066 port
->bulk_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1067 if (!port
->bulk_in_buffer
) {
1068 dev_err(&interface
->dev
, "Couldn't allocate bulk_in_buffer\n");
1071 usb_fill_bulk_urb (port
->read_urb
, dev
,
1072 usb_rcvbulkpipe (dev
,
1073 endpoint
->bEndpointAddress
),
1074 port
->bulk_in_buffer
, buffer_size
,
1075 serial
->type
->read_bulk_callback
,
1079 for (i
= 0; i
< num_bulk_out
; ++i
) {
1080 endpoint
= bulk_out_endpoint
[i
];
1081 port
= serial
->port
[i
];
1082 port
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1083 if (!port
->write_urb
) {
1084 dev_err(&interface
->dev
, "No free urbs available\n");
1087 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1088 port
->bulk_out_size
= buffer_size
;
1089 port
->bulk_out_endpointAddress
= endpoint
->bEndpointAddress
;
1090 port
->bulk_out_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1091 if (!port
->bulk_out_buffer
) {
1092 dev_err(&interface
->dev
, "Couldn't allocate bulk_out_buffer\n");
1095 usb_fill_bulk_urb (port
->write_urb
, dev
,
1096 usb_sndbulkpipe (dev
,
1097 endpoint
->bEndpointAddress
),
1098 port
->bulk_out_buffer
, buffer_size
,
1099 serial
->type
->write_bulk_callback
,
1103 if (serial
->type
->read_int_callback
) {
1104 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1105 endpoint
= interrupt_in_endpoint
[i
];
1106 port
= serial
->port
[i
];
1107 port
->interrupt_in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1108 if (!port
->interrupt_in_urb
) {
1109 dev_err(&interface
->dev
, "No free urbs available\n");
1112 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1113 port
->interrupt_in_endpointAddress
= endpoint
->bEndpointAddress
;
1114 port
->interrupt_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1115 if (!port
->interrupt_in_buffer
) {
1116 dev_err(&interface
->dev
, "Couldn't allocate interrupt_in_buffer\n");
1119 usb_fill_int_urb (port
->interrupt_in_urb
, dev
,
1120 usb_rcvintpipe (dev
,
1121 endpoint
->bEndpointAddress
),
1122 port
->interrupt_in_buffer
, buffer_size
,
1123 serial
->type
->read_int_callback
, port
,
1124 endpoint
->bInterval
);
1126 } else if (num_interrupt_in
) {
1127 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
1130 if (serial
->type
->write_int_callback
) {
1131 for (i
= 0; i
< num_interrupt_out
; ++i
) {
1132 endpoint
= interrupt_out_endpoint
[i
];
1133 port
= serial
->port
[i
];
1134 port
->interrupt_out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1135 if (!port
->interrupt_out_urb
) {
1136 dev_err(&interface
->dev
, "No free urbs available\n");
1139 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
1140 port
->interrupt_out_size
= buffer_size
;
1141 port
->interrupt_out_endpointAddress
= endpoint
->bEndpointAddress
;
1142 port
->interrupt_out_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1143 if (!port
->interrupt_out_buffer
) {
1144 dev_err(&interface
->dev
, "Couldn't allocate interrupt_out_buffer\n");
1147 usb_fill_int_urb (port
->interrupt_out_urb
, dev
,
1148 usb_sndintpipe (dev
,
1149 endpoint
->bEndpointAddress
),
1150 port
->interrupt_out_buffer
, buffer_size
,
1151 serial
->type
->write_int_callback
, port
,
1152 endpoint
->bInterval
);
1154 } else if (num_interrupt_out
) {
1155 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
1158 /* if this device type has an attach function, call it */
1160 if (!try_module_get(type
->owner
)) {
1161 dev_err(&interface
->dev
, "module get failed, exiting\n");
1164 retval
= type
->attach (serial
);
1165 module_put(type
->owner
);
1169 /* quietly accept this device, but don't bind to a serial port
1170 * as it's about to disappear */
1175 /* register all of the individual ports with the driver core */
1176 for (i
= 0; i
< num_ports
; ++i
) {
1177 port
= serial
->port
[i
];
1178 port
->dev
.parent
= &interface
->dev
;
1179 port
->dev
.driver
= NULL
;
1180 port
->dev
.bus
= &usb_serial_bus_type
;
1181 port
->dev
.release
= &port_release
;
1183 snprintf (&port
->dev
.bus_id
[0], sizeof(port
->dev
.bus_id
), "ttyUSB%d", port
->number
);
1184 dbg ("%s - registering %s", __FUNCTION__
, port
->dev
.bus_id
);
1185 device_register (&port
->dev
);
1188 usb_serial_console_init (debug
, minor
);
1192 usb_set_intfdata (interface
, serial
);
1196 for (i
= 0; i
< num_bulk_in
; ++i
) {
1197 port
= serial
->port
[i
];
1201 usb_free_urb (port
->read_urb
);
1202 kfree(port
->bulk_in_buffer
);
1204 for (i
= 0; i
< num_bulk_out
; ++i
) {
1205 port
= serial
->port
[i
];
1208 if (port
->write_urb
)
1209 usb_free_urb (port
->write_urb
);
1210 kfree(port
->bulk_out_buffer
);
1212 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1213 port
= serial
->port
[i
];
1216 if (port
->interrupt_in_urb
)
1217 usb_free_urb (port
->interrupt_in_urb
);
1218 kfree(port
->interrupt_in_buffer
);
1220 for (i
= 0; i
< num_interrupt_out
; ++i
) {
1221 port
= serial
->port
[i
];
1224 if (port
->interrupt_out_urb
)
1225 usb_free_urb (port
->interrupt_out_urb
);
1226 kfree(port
->interrupt_out_buffer
);
1229 /* return the minor range that this device had */
1230 return_serial (serial
);
1232 /* free up any memory that we allocated */
1233 for (i
= 0; i
< serial
->num_port_pointers
; ++i
)
1234 kfree(serial
->port
[i
]);
1239 void usb_serial_disconnect(struct usb_interface
*interface
)
1242 struct usb_serial
*serial
= usb_get_intfdata (interface
);
1243 struct device
*dev
= &interface
->dev
;
1244 struct usb_serial_port
*port
;
1246 dbg ("%s", __FUNCTION__
);
1248 usb_set_intfdata (interface
, NULL
);
1250 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1251 port
= serial
->port
[i
];
1252 if (port
&& port
->tty
)
1253 tty_hangup(port
->tty
);
1255 /* let the last holder of this object
1256 * cause it to be cleaned up */
1257 kref_put(&serial
->kref
, destroy_serial
);
1259 dev_info(dev
, "device disconnected\n");
1262 static struct tty_operations serial_ops
= {
1263 .open
= serial_open
,
1264 .close
= serial_close
,
1265 .write
= serial_write
,
1266 .write_room
= serial_write_room
,
1267 .ioctl
= serial_ioctl
,
1268 .set_termios
= serial_set_termios
,
1269 .throttle
= serial_throttle
,
1270 .unthrottle
= serial_unthrottle
,
1271 .break_ctl
= serial_break
,
1272 .chars_in_buffer
= serial_chars_in_buffer
,
1273 .read_proc
= serial_read_proc
,
1274 .tiocmget
= serial_tiocmget
,
1275 .tiocmset
= serial_tiocmset
,
1278 struct tty_driver
*usb_serial_tty_driver
;
1280 static int __init
usb_serial_init(void)
1285 usb_serial_tty_driver
= alloc_tty_driver(SERIAL_TTY_MINORS
);
1286 if (!usb_serial_tty_driver
)
1289 /* Initialize our global data */
1290 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
1291 serial_table
[i
] = NULL
;
1294 result
= bus_register(&usb_serial_bus_type
);
1296 err("%s - registering bus driver failed", __FUNCTION__
);
1300 /* register the generic driver, if we should */
1301 result
= usb_serial_generic_register(debug
);
1303 err("%s - registering generic driver failed", __FUNCTION__
);
1307 usb_serial_tty_driver
->owner
= THIS_MODULE
;
1308 usb_serial_tty_driver
->driver_name
= "usbserial";
1309 usb_serial_tty_driver
->devfs_name
= "usb/tts/";
1310 usb_serial_tty_driver
->name
= "ttyUSB";
1311 usb_serial_tty_driver
->major
= SERIAL_TTY_MAJOR
;
1312 usb_serial_tty_driver
->minor_start
= 0;
1313 usb_serial_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1314 usb_serial_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1315 usb_serial_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
;
1316 usb_serial_tty_driver
->init_termios
= tty_std_termios
;
1317 usb_serial_tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1318 tty_set_operations(usb_serial_tty_driver
, &serial_ops
);
1319 result
= tty_register_driver(usb_serial_tty_driver
);
1321 err("%s - tty_register_driver failed", __FUNCTION__
);
1322 goto exit_reg_driver
;
1325 /* register the USB driver */
1326 result
= usb_register(&usb_serial_driver
);
1328 err("%s - usb_register failed", __FUNCTION__
);
1332 info(DRIVER_DESC
" " DRIVER_VERSION
);
1337 tty_unregister_driver(usb_serial_tty_driver
);
1340 usb_serial_generic_deregister();
1343 bus_unregister(&usb_serial_bus_type
);
1346 err ("%s - returning with error %d", __FUNCTION__
, result
);
1347 put_tty_driver(usb_serial_tty_driver
);
1352 static void __exit
usb_serial_exit(void)
1354 usb_serial_console_exit();
1356 usb_serial_generic_deregister();
1358 usb_deregister(&usb_serial_driver
);
1359 tty_unregister_driver(usb_serial_tty_driver
);
1360 put_tty_driver(usb_serial_tty_driver
);
1361 bus_unregister(&usb_serial_bus_type
);
1365 module_init(usb_serial_init
);
1366 module_exit(usb_serial_exit
);
1368 #define set_to_generic_if_null(type, function) \
1370 if (!type->function) { \
1371 type->function = usb_serial_generic_##function; \
1372 dbg("Had to override the " #function \
1373 " usb serial operation with the generic one.");\
1377 static void fixup_generic(struct usb_serial_device_type
*device
)
1379 set_to_generic_if_null(device
, open
);
1380 set_to_generic_if_null(device
, write
);
1381 set_to_generic_if_null(device
, close
);
1382 set_to_generic_if_null(device
, write_room
);
1383 set_to_generic_if_null(device
, chars_in_buffer
);
1384 set_to_generic_if_null(device
, read_bulk_callback
);
1385 set_to_generic_if_null(device
, write_bulk_callback
);
1386 set_to_generic_if_null(device
, shutdown
);
1389 int usb_serial_register(struct usb_serial_device_type
*new_device
)
1393 fixup_generic(new_device
);
1395 /* Add this device to our list of devices */
1396 list_add(&new_device
->driver_list
, &usb_serial_driver_list
);
1398 retval
= usb_serial_bus_register(new_device
);
1400 err("problem %d when registering driver %s", retval
, new_device
->name
);
1401 list_del(&new_device
->driver_list
);
1404 info("USB Serial support registered for %s", new_device
->name
);
1410 void usb_serial_deregister(struct usb_serial_device_type
*device
)
1412 info("USB Serial deregistering driver %s", device
->name
);
1413 list_del(&device
->driver_list
);
1414 usb_serial_bus_deregister(device
);
1419 /* If the usb-serial core is built into the core, the usb-serial drivers
1420 need these symbols to load properly as modules. */
1421 EXPORT_SYMBOL_GPL(usb_serial_register
);
1422 EXPORT_SYMBOL_GPL(usb_serial_deregister
);
1423 EXPORT_SYMBOL_GPL(usb_serial_probe
);
1424 EXPORT_SYMBOL_GPL(usb_serial_disconnect
);
1425 EXPORT_SYMBOL_GPL(usb_serial_port_softint
);
1428 /* Module information */
1429 MODULE_AUTHOR( DRIVER_AUTHOR
);
1430 MODULE_DESCRIPTION( DRIVER_DESC
);
1431 MODULE_VERSION( DRIVER_VERSION
);
1432 MODULE_LICENSE("GPL");
1434 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1435 MODULE_PARM_DESC(debug
, "Debug enabled or not");