2 * USB Serial Converter driver
4 * Copyright (C) 1999, 2000
5 * Greg Kroah-Hartman (greg@kroah.com)
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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 * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
19 * removed from the individual device source files.
22 * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
25 * Changed devfs and tty register code to work properly now. This was based on
26 * the ACM driver changes by Vojtech Pavlik.
28 * (04/27/2000) Ryan VanderBijl
29 * Put calls to *_paranoia_checks into one function.
32 * Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
33 * Moved when the startup code printed out the devices that are supported.
36 * Added driver for ZyXEL omni.net lcd plus ISDN TA
37 * Made startup info message specify which drivers were compiled in.
40 * Changed the probe process to remove the module unload races.
41 * Changed where the tty layer gets initialized to have devfs work nicer.
42 * Added initial devfs support.
45 * Split driver up into device specific pieces.
48 * Fixed oops that could happen when device was removed while a program
49 * was talking to the device.
50 * Removed the static urbs and now all urbs are created and destroyed
52 * Reworked the internal interface. Now everything is based on the
53 * usb_serial_port structure instead of the larger usb_serial structure.
54 * This fixes the bug that a multiport device could not have more than
55 * one port open at one time.
58 * Added config option for debugging messages.
59 * Added patch for keyspan pda from Brian Warner.
62 * Added the keyspan pda code from Brian Warner <warner@lothar.com>
63 * Moved a bunch of the port specific stuff into its own structure. This
64 * is in anticipation of the true multiport devices (there's a bug if you
65 * try to access more than one port of any multiport device right now)
68 * Made it so that any serial devices only have to specify which functions
69 * they want to overload from the generic function calls (great,
70 * inheritance in C, in a driver, just what I wanted...)
71 * Added support for set_termios and ioctl function calls. No drivers take
72 * advantage of this yet.
73 * Removed the #ifdef MODULE, now there is no module specific code.
74 * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
76 * Small fix to get_free_serial.
79 * Removed the Belkin and Peracom functionality from the driver due to
80 * the lack of support from the vendor, and me not wanting people to
81 * accidenatly buy the device, expecting it to work with Linux.
82 * Added read_bulk_callback and write_bulk_callback to the type structure
83 * for the needs of the FTDI and WhiteHEAT driver.
84 * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
86 * Changed the output urb size back to the max endpoint size to make
87 * the ftdi_sio driver have it easier, and due to the fact that it didn't
88 * really increase the speed any.
91 * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
92 * patch from Miles Lott (milos@insync.net).
93 * Fixed bug with not restoring the minor range that a device grabs, if
94 * the startup function fails (thanks Miles for finding this).
97 * Added initial framework for the Keyspan PDA serial converter so that
98 * Brian Warner has a place to put his code.
99 * Made the ezusb specific functions generic enough that different
100 * devices can use them (whiteheat and keyspan_pda both need them).
101 * Split out a whole bunch of structure and other stuff to a seperate
103 * Made the Visor connection messages a little more understandable, now
104 * that Miles Lott (milos@insync.net) has gotten the Generic channel to
105 * work. Also made them always show up in the log file.
108 * Added initial framework for FTDI serial converter so that Bill Ryder
109 * has a place to put his code.
110 * Added the vendor specific info from Handspring. Now we can print out
111 * informational debug messages as well as understand what is happening.
114 * Fixed problem of crash when trying to open a port that didn't have a
115 * device assigned to it. Made the minor node finding a little smarter,
116 * now it looks to find a continous space for the new device.
119 * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
120 * Fixed get_serial_by_minor which was all messed up for multi port
121 * devices. Fixed multi port problem for generic devices. Now the number
122 * of ports is determined by the number of bulk out endpoints for the
126 * Removed lots of cruft that was around from the old (pre urb) driver
128 * Made the serial_table dynamic. This should save lots of memory when
129 * the number of minor nodes goes up to 256.
130 * Added initial support for devices that have more than one port.
131 * Added more debugging comments for the Visor, and added a needed
132 * set_configuration call.
135 * Fixed the WhiteHEAT firmware (my processing tool had a bug)
136 * and added new debug loader firmware for it.
137 * Removed the put_char function as it isn't really needed.
138 * Added visor startup commands as found by the Win98 dump.
141 * Fixed the vendor id for the generic driver to the one I meant it to be.
144 * Forget the version numbering...that's pretty useless...
145 * Made the driver able to be compiled so that the user can select which
146 * converter they want to use. This allows people who only want the Visor
147 * support to not pay the memory size price of the WhiteHEAT.
148 * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
149 * grabbed the root hub. Not good.
151 * version 0.4.0 (01/10/2000) gkh
152 * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
153 * device. Added startup function to allow firmware to be downloaded to
154 * a device if it needs to be.
155 * Added firmware download logic to the WhiteHEAT device.
156 * Started to add #defines to split up the different drivers for potential
157 * configuration option.
159 * version 0.3.1 (12/30/99) gkh
160 * Fixed problems with urb for bulk out.
161 * Added initial support for multiple sets of endpoints. This enables
162 * the Handspring Visor to be attached successfully. Only the first
163 * bulk in / bulk out endpoint pair is being used right now.
165 * version 0.3.0 (12/27/99) gkh
166 * Added initial support for the Handspring Visor based on a patch from
167 * Miles Lott (milos@sneety.insync.net)
168 * Cleaned up the code a bunch and converted over to using urbs only.
170 * version 0.2.3 (12/21/99) gkh
171 * Added initial support for the Connect Tech WhiteHEAT converter.
172 * Incremented the number of ports in expectation of getting the
173 * WhiteHEAT to work properly (4 ports per connection).
174 * Added notification on insertion and removal of what port the
175 * device is/was connected to (and what kind of device it was).
177 * version 0.2.2 (12/16/99) gkh
178 * Changed major number to the new allocated number. We're legal now!
180 * version 0.2.1 (12/14/99) gkh
181 * Fixed bug that happens when device node is opened when there isn't a
182 * device attached to it. Thanks to marek@webdesign.no for noticing this.
184 * version 0.2.0 (11/10/99) gkh
185 * Split up internals to make it easier to add different types of serial
186 * converters to the code.
187 * Added a "generic" driver that gets it's vendor and product id
188 * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
189 * for the idea and sample code (from the usb scanner driver.)
190 * Cleared up any licensing questions by releasing it under the GNU GPL.
192 * version 0.1.2 (10/25/99) gkh
193 * Fixed bug in detecting device.
195 * version 0.1.1 (10/05/99) gkh
196 * Changed the major number to not conflict with anything else.
198 * version 0.1 (09/28/99) gkh
199 * Can recognize the two different devices and start up a read from
200 * device when asked to. Writes also work. No control signals yet, this
201 * all is vendor specific data (i.e. no spec), also no control for
202 * different baud rates or other bit settings.
203 * Currently we are using the same devid as the acm driver. This needs
208 #include <linux/config.h>
209 #include <linux/kernel.h>
210 #include <linux/sched.h>
211 #include <linux/signal.h>
212 #include <linux/errno.h>
213 #include <linux/poll.h>
214 #include <linux/init.h>
215 #include <linux/malloc.h>
216 #include <linux/fcntl.h>
217 #include <linux/tty_driver.h>
218 #include <linux/tty_flip.h>
219 #include <linux/tty.h>
220 #include <linux/module.h>
221 #include <linux/spinlock.h>
223 #ifdef CONFIG_USB_SERIAL_DEBUG
228 #include <linux/usb.h>
230 /* Module information */
231 MODULE_AUTHOR("Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/");
232 MODULE_DESCRIPTION("USB Serial Driver");
234 #include "usb-serial.h"
236 #define MAX(a,b) (((a)>(b))?(a):(b))
238 /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
239 /* need to always compile these in, as some of the other devices use these functions as their own. */
240 /* if a driver does not provide a function pointer, the generic function will be called. */
241 static int generic_open (struct usb_serial_port
*port
, struct file
*filp
);
242 static void generic_close (struct usb_serial_port
*port
, struct file
*filp
);
243 static int generic_write (struct usb_serial_port
*port
, int from_user
, const unsigned char *buf
, int count
);
244 static int generic_write_room (struct usb_serial_port
*port
);
245 static int generic_chars_in_buffer (struct usb_serial_port
*port
);
246 static void generic_read_bulk_callback (struct urb
*urb
);
247 static void generic_write_bulk_callback (struct urb
*urb
);
250 #ifdef CONFIG_USB_SERIAL_GENERIC
251 static __u16 vendor
= 0x05f9;
252 static __u16 product
= 0xffff;
253 MODULE_PARM(vendor
, "i");
254 MODULE_PARM_DESC(vendor
, "User specified USB idVendor");
256 MODULE_PARM(product
, "i");
257 MODULE_PARM_DESC(product
, "User specified USB idProduct");
259 /* All of the device info needed for the Generic Serial Converter */
260 static struct usb_serial_device_type generic_device
= {
262 idVendor
: &vendor
, /* use the user specified vendor id */
263 idProduct
: &product
, /* use the user specified product id */
264 needs_interrupt_in
: DONT_CARE
, /* don't have to have an interrupt in endpoint */
265 needs_bulk_in
: DONT_CARE
, /* don't have to have a bulk in endpoint */
266 needs_bulk_out
: DONT_CARE
, /* don't have to have a bulk out endpoint */
267 num_interrupt_in
: NUM_DONT_CARE
,
268 num_bulk_in
: NUM_DONT_CARE
,
269 num_bulk_out
: NUM_DONT_CARE
,
275 /* To add support for another serial converter, create a usb_serial_device_type
276 structure for that device, and add it to this list, making sure that the
277 last entry is NULL. */
278 static struct usb_serial_device_type
*usb_serial_devices
[] = {
279 #ifdef CONFIG_USB_SERIAL_GENERIC
282 #ifdef CONFIG_USB_SERIAL_WHITEHEAT
283 &whiteheat_fake_device
,
286 #ifdef CONFIG_USB_SERIAL_VISOR
289 #ifdef CONFIG_USB_SERIAL_FTDI_SIO
292 #ifdef CONFIG_USB_SERIAL_KEYSPAN_PDA
293 &keyspan_pda_fake_device
,
296 #ifdef CONFIG_USB_SERIAL_OMNINET
297 &zyxel_omninet_device
,
299 #ifdef CONFIG_USB_SERIAL_DIGI_ACCELEPORT
300 &digi_acceleport_device
,
306 /* local function prototypes */
307 static int serial_open (struct tty_struct
*tty
, struct file
* filp
);
308 static void serial_close (struct tty_struct
*tty
, struct file
* filp
);
309 static int serial_write (struct tty_struct
* tty
, int from_user
, const unsigned char *buf
, int count
);
310 static int serial_write_room (struct tty_struct
*tty
);
311 static int serial_chars_in_buffer (struct tty_struct
*tty
);
312 static void serial_throttle (struct tty_struct
* tty
);
313 static void serial_unthrottle (struct tty_struct
* tty
);
314 static int serial_ioctl (struct tty_struct
*tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
);
315 static void serial_set_termios (struct tty_struct
*tty
, struct termios
* old
);
317 static void * usb_serial_probe(struct usb_device
*dev
, unsigned int ifnum
);
318 static void usb_serial_disconnect(struct usb_device
*dev
, void *ptr
);
320 static struct usb_driver usb_serial_driver
= {
322 probe
: usb_serial_probe
,
323 disconnect
: usb_serial_disconnect
,
326 static int serial_refcount
;
327 static struct tty_driver serial_tty_driver
;
328 static struct tty_struct
* serial_tty
[SERIAL_TTY_MINORS
];
329 static struct termios
* serial_termios
[SERIAL_TTY_MINORS
];
330 static struct termios
* serial_termios_locked
[SERIAL_TTY_MINORS
];
331 static struct usb_serial
*serial_table
[SERIAL_TTY_MINORS
] = {NULL
, };
334 static inline struct usb_serial
* get_usb_serial (struct usb_serial_port
*port
, const char *function
)
336 /* if no port was specified, or it fails a paranoia check */
338 port_paranoia_check (port
, function
) ||
339 serial_paranoia_check (port
->serial
, function
)) {
340 /* then say that we dont have a valid usb_serial thing, which will
341 * end up genrating -ENODEV return values */
349 static struct usb_serial
*get_serial_by_minor (int minor
)
351 return serial_table
[minor
];
355 static struct usb_serial
*get_free_serial (int num_ports
, int *minor
)
357 struct usb_serial
*serial
= NULL
;
361 dbg("get_free_serial %d", num_ports
);
364 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
369 for (j
= 1; j
<= num_ports
-1; ++j
)
370 if (serial_table
[i
+j
])
375 if (!(serial
= kmalloc(sizeof(struct usb_serial
), GFP_KERNEL
))) {
376 err("Out of memory");
379 memset(serial
, 0, sizeof(struct usb_serial
));
380 serial
->magic
= USB_SERIAL_MAGIC
;
381 serial_table
[i
] = serial
;
383 dbg("minor base = %d", *minor
);
384 for (i
= *minor
+1; (i
< (*minor
+ num_ports
)) && (i
< SERIAL_TTY_MINORS
); ++i
)
385 serial_table
[i
] = serial
;
392 static void return_serial (struct usb_serial
*serial
)
396 dbg("return_serial");
401 for (i
= 0; i
< serial
->num_ports
; ++i
) {
402 serial_table
[serial
->minor
+ i
] = NULL
;
409 #ifdef USES_EZUSB_FUNCTIONS
410 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
411 #define CPUCS_REG 0x7F92
413 int ezusb_writememory (struct usb_serial
*serial
, int address
, unsigned char *data
, int length
, __u8 bRequest
)
416 unsigned char *transfer_buffer
= kmalloc (length
, GFP_KERNEL
);
418 // dbg("ezusb_writememory %x, %d", address, length);
420 if (!transfer_buffer
) {
421 err("ezusb_writememory: kmalloc(%d) failed.", length
);
424 memcpy (transfer_buffer
, data
, length
);
425 result
= usb_control_msg (serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0), bRequest
, 0x40, address
, 0, transfer_buffer
, length
, 300);
426 kfree (transfer_buffer
);
431 int ezusb_set_reset (struct usb_serial
*serial
, unsigned char reset_bit
)
434 dbg("ezusb_set_reset: %d", reset_bit
);
435 response
= ezusb_writememory (serial
, CPUCS_REG
, &reset_bit
, 1, 0xa0);
437 err("ezusb_set_reset %d failed", reset_bit
);
442 #endif /* USES_EZUSB_FUNCTIONS */
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
;
456 /* initialize the pointer incase something fails */
457 tty
->driver_data
= NULL
;
459 /* get the serial object associated with this tty pointer */
460 serial
= get_serial_by_minor (MINOR(tty
->device
));
462 if (serial_paranoia_check (serial
, "serial_open")) {
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
;
472 /* pass on to the driver specific version of this function if it is available */
473 if (serial
->type
->open
) {
474 return (serial
->type
->open(port
, filp
));
476 return (generic_open(port
, filp
));
481 static void serial_close(struct tty_struct
*tty
, struct file
* filp
)
483 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
484 struct usb_serial
*serial
= get_usb_serial (port
, "serial_close");
490 dbg("serial_close port %d", port
->number
);
493 dbg ("port not opened");
497 /* pass on to the driver specific version of this function if it is available */
498 if (serial
->type
->close
) {
499 serial
->type
->close(port
, filp
);
501 generic_close(port
, filp
);
506 static int serial_write (struct tty_struct
* tty
, int from_user
, const unsigned char *buf
, int count
)
508 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
509 struct usb_serial
*serial
= get_usb_serial (port
, "serial_write");
515 dbg("serial_write port %d, %d byte(s)", port
->number
, count
);
518 dbg ("port not opened");
522 /* pass on to the driver specific version of this function if it is available */
523 if (serial
->type
->write
) {
524 return (serial
->type
->write(port
, from_user
, buf
, count
));
526 return (generic_write(port
, from_user
, buf
, count
));
531 static int serial_write_room (struct tty_struct
*tty
)
533 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
534 struct usb_serial
*serial
= get_usb_serial (port
, "serial_write_room");
540 dbg("serial_write_room port %d", port
->number
);
543 dbg ("port not open");
547 /* pass on to the driver specific version of this function if it is available */
548 if (serial
->type
->write_room
) {
549 return (serial
->type
->write_room(port
));
551 return (generic_write_room(port
));
556 static int serial_chars_in_buffer (struct tty_struct
*tty
)
558 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
559 struct usb_serial
*serial
= get_usb_serial (port
, "serial_chars_in_buffer");
566 dbg ("port not open");
570 /* pass on to the driver specific version of this function if it is available */
571 if (serial
->type
->chars_in_buffer
) {
572 return (serial
->type
->chars_in_buffer(port
));
574 return (generic_chars_in_buffer(port
));
579 static void serial_throttle (struct tty_struct
* tty
)
581 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
582 struct usb_serial
*serial
= get_usb_serial (port
, "serial_throttle");
588 dbg("serial_throttle port %d", port
->number
);
591 dbg ("port not open");
595 /* pass on to the driver specific version of this function */
596 if (serial
->type
->throttle
) {
597 serial
->type
->throttle(port
);
604 static void serial_unthrottle (struct tty_struct
* tty
)
606 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
607 struct usb_serial
*serial
= get_usb_serial (port
, "serial_unthrottle");
613 dbg("serial_unthrottle port %d", port
->number
);
616 dbg ("port not open");
620 /* pass on to the driver specific version of this function */
621 if (serial
->type
->unthrottle
) {
622 serial
->type
->unthrottle(port
);
629 static int serial_ioctl (struct tty_struct
*tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
631 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
632 struct usb_serial
*serial
= get_usb_serial (port
, "serial_ioctl");
638 dbg("serial_ioctl port %d", port
->number
);
641 dbg ("port not open");
645 /* pass on to the driver specific version of this function if it is available */
646 if (serial
->type
->ioctl
) {
647 return (serial
->type
->ioctl(port
, file
, cmd
, arg
));
654 static void serial_set_termios (struct tty_struct
*tty
, struct termios
* old
)
656 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
657 struct usb_serial
*serial
= get_usb_serial (port
, "serial_set_termios");
663 dbg("serial_set_termios port %d", port
->number
);
666 dbg ("port not open");
670 /* pass on to the driver specific version of this function if it is available */
671 if (serial
->type
->set_termios
) {
672 serial
->type
->set_termios(port
, old
);
679 static void serial_break (struct tty_struct
*tty
, int break_state
)
681 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
682 struct usb_serial
*serial
= get_usb_serial (port
, "serial_break");
688 dbg("serial_break port %d", port
->number
);
691 dbg ("port not open");
695 /* pass on to the driver specific version of this function if it is
697 if (serial
->type
->break_ctl
) {
698 serial
->type
->break_ctl(port
, break_state
);
704 /*****************************************************************************
705 * generic devices specific driver functions
706 *****************************************************************************/
707 static int generic_open (struct usb_serial_port
*port
, struct file
*filp
)
709 struct usb_serial
*serial
= port
->serial
;
711 dbg("generic_open port %d", port
->number
);
714 dbg ("device already open");
719 /* if we have a bulk interrupt, start reading from it */
720 if (serial
->num_bulk_in
) {
721 /*Start reading from the device*/
722 if (usb_submit_urb(port
->read_urb
))
723 dbg("usb_submit_urb(read bulk) failed");
730 static void generic_close (struct usb_serial_port
*port
, struct file
* filp
)
732 struct usb_serial
*serial
= port
->serial
;
734 dbg("generic_close port %d", port
->number
);
736 /* shutdown any bulk reads that might be going on */
737 if (serial
->num_bulk_out
) {
738 usb_unlink_urb (port
->write_urb
);
740 if (serial
->num_bulk_in
) {
741 usb_unlink_urb (port
->read_urb
);
748 static int generic_write (struct usb_serial_port
*port
, int from_user
, const unsigned char *buf
, int count
)
750 struct usb_serial
*serial
= port
->serial
;
752 dbg("generic_serial_write port %d", port
->number
);
755 dbg("write request of 0 bytes");
759 /* only do something if we have a bulk out endpoint */
760 if (serial
->num_bulk_out
) {
761 if (port
->write_urb
->status
== -EINPROGRESS
) {
762 dbg ("already writing");
766 count
= (count
> port
->bulk_out_size
) ? port
->bulk_out_size
: count
;
769 copy_from_user(port
->write_urb
->transfer_buffer
, buf
, count
);
772 memcpy (port
->write_urb
->transfer_buffer
, buf
, count
);
775 /* send the data out the bulk port */
776 port
->write_urb
->transfer_buffer_length
= count
;
778 if (usb_submit_urb(port
->write_urb
))
779 dbg("usb_submit_urb(write bulk) failed");
784 /* no bulk out, so return 0 bytes written */
789 static int generic_write_room (struct usb_serial_port
*port
)
791 struct usb_serial
*serial
= port
->serial
;
794 dbg("generic_write_room port %d", port
->number
);
796 if (serial
->num_bulk_out
) {
797 if (port
->write_urb
->status
== -EINPROGRESS
)
800 room
= port
->bulk_out_size
;
801 dbg("generic_write_room returns %d", room
);
809 static int generic_chars_in_buffer (struct usb_serial_port
*port
)
811 struct usb_serial
*serial
= port
->serial
;
813 dbg("generic_chars_in_buffer port %d", port
->number
);
815 if (serial
->num_bulk_out
) {
816 if (port
->write_urb
->status
== -EINPROGRESS
) {
817 return (port
->bulk_out_size
);
825 static void generic_read_bulk_callback (struct urb
*urb
)
827 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
828 struct usb_serial
*serial
= get_usb_serial (port
, "generic_read_bulk_callback");
829 struct tty_struct
*tty
;
830 unsigned char *data
= urb
->transfer_buffer
;
838 dbg("nonzero read bulk status received: %d", urb
->status
);
843 if (urb
->actual_length
) {
844 printk (KERN_DEBUG __FILE__
": data read - length = %d, data = ", urb
->actual_length
);
845 for (i
= 0; i
< urb
->actual_length
; ++i
) {
846 printk ("%.2x ", data
[i
]);
853 if (urb
->actual_length
) {
854 for (i
= 0; i
< urb
->actual_length
; ++i
) {
855 tty_insert_flip_char(tty
, data
[i
], 0);
857 tty_flip_buffer_push(tty
);
860 /* Continue trying to always read */
861 if (usb_submit_urb(urb
))
862 dbg("failed resubmitting read urb");
868 static void generic_write_bulk_callback (struct urb
*urb
)
870 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
871 struct usb_serial
*serial
= get_usb_serial (port
, "generic_write_bulk_callback");
872 struct tty_struct
*tty
;
879 dbg("nonzero write bulk status received: %d", urb
->status
);
884 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) && tty
->ldisc
.write_wakeup
)
885 (tty
->ldisc
.write_wakeup
)(tty
);
887 wake_up_interruptible(&tty
->write_wait
);
893 static void * usb_serial_probe(struct usb_device
*dev
, unsigned int ifnum
)
895 struct usb_serial
*serial
= NULL
;
896 struct usb_serial_port
*port
;
897 struct usb_interface_descriptor
*interface
;
898 struct usb_endpoint_descriptor
*endpoint
;
899 struct usb_endpoint_descriptor
*interrupt_in_endpoint
[MAX_NUM_PORTS
];
900 struct usb_endpoint_descriptor
*bulk_in_endpoint
[MAX_NUM_PORTS
];
901 struct usb_endpoint_descriptor
*bulk_out_endpoint
[MAX_NUM_PORTS
];
902 struct usb_serial_device_type
*type
;
910 int num_interrupt_in
= 0;
912 int num_bulk_out
= 0;
916 /* loop through our list of known serial converters, and see if this
918 for (device_num
= 0; usb_serial_devices
[device_num
]; device_num
++) {
919 type
= usb_serial_devices
[device_num
];
920 dbg ("Looking at %s Vendor id=%.4x Product id=%.4x",
921 type
->name
, *(type
->idVendor
), *(type
->idProduct
));
923 /* look at the device descriptor */
924 if ((dev
->descriptor
.idVendor
== *(type
->idVendor
)) &&
925 (dev
->descriptor
.idProduct
== *(type
->idProduct
))) {
926 dbg("descriptor matches");
930 if (!usb_serial_devices
[device_num
]) {
936 /* descriptor matches, let's find the endpoints needed */
937 interrupt_pipe
= bulk_in_pipe
= bulk_out_pipe
= HAS_NOT
;
939 /* check out the endpoints */
940 interface
= &dev
->actconfig
->interface
[ifnum
].altsetting
[0];
941 for (i
= 0; i
< interface
->bNumEndpoints
; ++i
) {
942 endpoint
= &interface
->endpoint
[i
];
944 if ((endpoint
->bEndpointAddress
& 0x80) &&
945 ((endpoint
->bmAttributes
& 3) == 0x02)) {
946 /* we found a bulk in endpoint */
947 dbg("found bulk in");
949 bulk_in_endpoint
[num_bulk_in
] = endpoint
;
953 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
954 ((endpoint
->bmAttributes
& 3) == 0x02)) {
955 /* we found a bulk out endpoint */
956 dbg("found bulk out");
958 bulk_out_endpoint
[num_bulk_out
] = endpoint
;
962 if ((endpoint
->bEndpointAddress
& 0x80) &&
963 ((endpoint
->bmAttributes
& 3) == 0x03)) {
964 /* we found a interrupt in endpoint */
965 dbg("found interrupt in");
966 interrupt_pipe
= HAS
;
967 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
972 /* verify that we found all of the endpoints that we need */
973 if (!((interrupt_pipe
& type
->needs_interrupt_in
) &&
974 (bulk_in_pipe
& type
->needs_bulk_in
) &&
975 (bulk_out_pipe
& type
->needs_bulk_out
))) {
976 /* nope, they don't match what we expected */
977 info("descriptors matched, but endpoints did not");
981 /* found all that we need */
983 info("%s converter detected", type
->name
);
985 #ifdef CONFIG_USB_SERIAL_GENERIC
986 if (type
== &generic_device
) {
987 num_ports
= num_bulk_out
;
988 if (num_ports
== 0) {
989 err("Generic device with no bulk out, not allowed.");
995 num_ports
= type
->num_ports
;
997 serial
= get_free_serial (num_ports
, &minor
);
998 if (serial
== NULL
) {
999 err("No more free serial devices");
1005 serial
->type
= type
;
1006 serial
->minor
= minor
;
1007 serial
->num_ports
= num_ports
;
1008 serial
->num_bulk_in
= num_bulk_in
;
1009 serial
->num_bulk_out
= num_bulk_out
;
1010 serial
->num_interrupt_in
= num_interrupt_in
;
1012 /* if this device type has a startup function, call it */
1013 if (type
->startup
) {
1014 if (type
->startup (serial
)) {
1019 /* set up the endpoint information */
1020 for (i
= 0; i
< num_bulk_in
; ++i
) {
1021 endpoint
= bulk_in_endpoint
[i
];
1022 port
= &serial
->port
[i
];
1023 port
->read_urb
= usb_alloc_urb (0);
1024 if (!port
->read_urb
) {
1025 err("No free urbs available");
1028 buffer_size
= endpoint
->wMaxPacketSize
;
1029 port
->bulk_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1030 if (!port
->bulk_in_buffer
) {
1031 err("Couldn't allocate bulk_in_buffer");
1034 FILL_BULK_URB(port
->read_urb
, dev
,
1035 usb_rcvbulkpipe(dev
, endpoint
->bEndpointAddress
),
1036 port
->bulk_in_buffer
, buffer_size
,
1037 ((serial
->type
->read_bulk_callback
) ?
1038 serial
->type
->read_bulk_callback
:
1039 generic_read_bulk_callback
),
1043 for (i
= 0; i
< num_bulk_out
; ++i
) {
1044 endpoint
= bulk_out_endpoint
[i
];
1045 port
= &serial
->port
[i
];
1046 port
->write_urb
= usb_alloc_urb(0);
1047 if (!port
->write_urb
) {
1048 err("No free urbs available");
1051 buffer_size
= endpoint
->wMaxPacketSize
;
1052 port
->bulk_out_size
= buffer_size
;
1053 port
->bulk_out_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1054 if (!port
->bulk_out_buffer
) {
1055 err("Couldn't allocate bulk_out_buffer");
1058 FILL_BULK_URB(port
->write_urb
, dev
,
1059 usb_sndbulkpipe(dev
, endpoint
->bEndpointAddress
),
1060 port
->bulk_out_buffer
, buffer_size
,
1061 ((serial
->type
->write_bulk_callback
) ?
1062 serial
->type
->write_bulk_callback
:
1063 generic_write_bulk_callback
),
1067 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1068 endpoint
= interrupt_in_endpoint
[i
];
1069 port
= &serial
->port
[i
];
1070 port
->interrupt_in_urb
= usb_alloc_urb(0);
1071 if (!port
->interrupt_in_urb
) {
1072 err("No free urbs available");
1075 buffer_size
= endpoint
->wMaxPacketSize
;
1076 port
->interrupt_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1077 if (!port
->interrupt_in_buffer
) {
1078 err("Couldn't allocate interrupt_in_buffer");
1081 FILL_INT_URB(port
->interrupt_in_urb
, dev
,
1082 usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
),
1083 port
->interrupt_in_buffer
, buffer_size
,
1084 serial
->type
->read_int_callback
,
1086 endpoint
->bInterval
);
1089 /* initialize some parts of the port structures */
1090 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1091 max_endpoints
= MAX(num_bulk_in
, num_bulk_out
);
1092 max_endpoints
= MAX(max_endpoints
, num_interrupt_in
);
1093 for (i
= 0; i
< max_endpoints
; ++i
) {
1094 port
= &serial
->port
[i
];
1095 port
->number
= i
+ serial
->minor
;
1096 port
->serial
= serial
;
1097 port
->magic
= USB_SERIAL_PORT_MAGIC
;
1100 /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
1101 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1102 tty_register_devfs (&serial_tty_driver
, 0, serial
->port
[i
].number
);
1103 info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)",
1104 type
->name
, serial
->port
[i
].number
, serial
->port
[i
].number
);
1107 return serial
; /* success */
1111 for (i
= 0; i
< num_bulk_in
; ++i
) {
1112 port
= &serial
->port
[i
];
1114 usb_free_urb (port
->read_urb
);
1115 if (port
->bulk_in_buffer
)
1116 kfree (port
->bulk_in_buffer
);
1118 for (i
= 0; i
< num_bulk_out
; ++i
) {
1119 port
= &serial
->port
[i
];
1120 if (port
->write_urb
)
1121 usb_free_urb (port
->write_urb
);
1122 if (port
->bulk_out_buffer
)
1123 kfree (port
->bulk_out_buffer
);
1125 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1126 port
= &serial
->port
[i
];
1127 if (port
->interrupt_in_urb
)
1128 usb_free_urb (port
->interrupt_in_urb
);
1129 if (port
->interrupt_in_buffer
)
1130 kfree (port
->interrupt_in_buffer
);
1133 /* return the minor range that this device had */
1134 return_serial (serial
);
1136 /* free up any memory that we allocated */
1143 static void usb_serial_disconnect(struct usb_device
*dev
, void *ptr
)
1145 struct usb_serial
*serial
= (struct usb_serial
*) ptr
;
1146 struct usb_serial_port
*port
;
1150 if (serial
->type
->shutdown
)
1151 serial
->type
->shutdown(serial
);
1153 for (i
= 0; i
< serial
->num_ports
; ++i
)
1154 serial
->port
[i
].active
= 0;
1156 for (i
= 0; i
< serial
->num_bulk_in
; ++i
) {
1157 port
= &serial
->port
[i
];
1158 if (port
->read_urb
) {
1159 usb_unlink_urb (port
->read_urb
);
1160 usb_free_urb (port
->read_urb
);
1162 if (port
->bulk_in_buffer
)
1163 kfree (port
->bulk_in_buffer
);
1165 for (i
= 0; i
< serial
->num_bulk_out
; ++i
) {
1166 port
= &serial
->port
[i
];
1167 if (port
->write_urb
) {
1168 usb_unlink_urb (port
->write_urb
);
1169 usb_free_urb (port
->write_urb
);
1171 if (port
->bulk_out_buffer
)
1172 kfree (port
->bulk_out_buffer
);
1174 for (i
= 0; i
< serial
->num_interrupt_in
; ++i
) {
1175 port
= &serial
->port
[i
];
1176 if (port
->interrupt_in_urb
) {
1177 usb_unlink_urb (port
->interrupt_in_urb
);
1178 usb_free_urb (port
->interrupt_in_urb
);
1180 if (port
->interrupt_in_buffer
)
1181 kfree (port
->interrupt_in_buffer
);
1184 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1185 tty_unregister_devfs (&serial_tty_driver
, serial
->port
[i
].number
);
1186 info("%s converter now disconnected from ttyUSB%d", serial
->type
->name
, serial
->port
[i
].number
);
1189 /* return the minor range that this device had */
1190 return_serial (serial
);
1192 /* free up any memory that we allocated */
1196 info("device disconnected");
1203 static struct tty_driver serial_tty_driver
= {
1204 magic
: TTY_DRIVER_MAGIC
,
1205 driver_name
: "usb-serial",
1207 major
: SERIAL_TTY_MAJOR
,
1209 num
: SERIAL_TTY_MINORS
,
1210 type
: TTY_DRIVER_TYPE_SERIAL
,
1211 subtype
: SERIAL_TYPE_NORMAL
,
1212 flags
: TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
,
1214 refcount
: &serial_refcount
,
1216 termios
: serial_termios
,
1217 termios_locked
: serial_termios_locked
,
1220 close
: serial_close
,
1221 write
: serial_write
,
1222 write_room
: serial_write_room
,
1223 ioctl
: serial_ioctl
,
1224 set_termios
: serial_set_termios
,
1225 throttle
: serial_throttle
,
1226 unthrottle
: serial_unthrottle
,
1227 break_ctl
: serial_break
,
1228 chars_in_buffer
: serial_chars_in_buffer
,
1232 int usb_serial_init(void)
1238 /* Initalize our global data */
1239 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
1240 serial_table
[i
] = NULL
;
1243 /* tell the world what devices this driver currently supports */
1245 for (i
= 0; usb_serial_devices
[i
]; ++i
) {
1246 if (!strstr (usb_serial_devices
[i
]->name
, "prerenumeration")) {
1247 info ("USB Serial support registered for %s", usb_serial_devices
[i
]->name
);
1252 info ("USB Serial driver is not configured for any devices!");
1254 /* register the tty driver */
1255 serial_tty_driver
.init_termios
= tty_std_termios
;
1256 serial_tty_driver
.init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1257 if (tty_register_driver (&serial_tty_driver
)) {
1258 err("failed to register tty driver");
1262 /* register the USB driver */
1263 result
= usb_register(&usb_serial_driver
);
1265 tty_unregister_driver(&serial_tty_driver
);
1266 err("usb_register failed for the usb-serial driver. Error number %d", result
);
1274 void usb_serial_exit(void)
1276 usb_deregister(&usb_serial_driver
);
1277 tty_unregister_driver(&serial_tty_driver
);
1281 module_init(usb_serial_init
);
1282 module_exit(usb_serial_exit
);