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 generic_write_bulk_callback to not call wake_up_interruptible
19 * directly, but to have port_softint do it at a safer time.
22 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
25 * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
26 * removed from the individual device source files.
29 * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
32 * Changed devfs and tty register code to work properly now. This was based on
33 * the ACM driver changes by Vojtech Pavlik.
35 * (04/27/2000) Ryan VanderBijl
36 * Put calls to *_paranoia_checks into one function.
39 * Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
40 * Moved when the startup code printed out the devices that are supported.
43 * Added driver for ZyXEL omni.net lcd plus ISDN TA
44 * Made startup info message specify which drivers were compiled in.
47 * Changed the probe process to remove the module unload races.
48 * Changed where the tty layer gets initialized to have devfs work nicer.
49 * Added initial devfs support.
52 * Split driver up into device specific pieces.
55 * Fixed oops that could happen when device was removed while a program
56 * was talking to the device.
57 * Removed the static urbs and now all urbs are created and destroyed
59 * Reworked the internal interface. Now everything is based on the
60 * usb_serial_port structure instead of the larger usb_serial structure.
61 * This fixes the bug that a multiport device could not have more than
62 * one port open at one time.
65 * Added config option for debugging messages.
66 * Added patch for keyspan pda from Brian Warner.
69 * Added the keyspan pda code from Brian Warner <warner@lothar.com>
70 * Moved a bunch of the port specific stuff into its own structure. This
71 * is in anticipation of the true multiport devices (there's a bug if you
72 * try to access more than one port of any multiport device right now)
75 * Made it so that any serial devices only have to specify which functions
76 * they want to overload from the generic function calls (great,
77 * inheritance in C, in a driver, just what I wanted...)
78 * Added support for set_termios and ioctl function calls. No drivers take
79 * advantage of this yet.
80 * Removed the #ifdef MODULE, now there is no module specific code.
81 * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
83 * Small fix to get_free_serial.
86 * Removed the Belkin and Peracom functionality from the driver due to
87 * the lack of support from the vendor, and me not wanting people to
88 * accidenatly buy the device, expecting it to work with Linux.
89 * Added read_bulk_callback and write_bulk_callback to the type structure
90 * for the needs of the FTDI and WhiteHEAT driver.
91 * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
93 * Changed the output urb size back to the max endpoint size to make
94 * the ftdi_sio driver have it easier, and due to the fact that it didn't
95 * really increase the speed any.
98 * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
99 * patch from Miles Lott (milos@insync.net).
100 * Fixed bug with not restoring the minor range that a device grabs, if
101 * the startup function fails (thanks Miles for finding this).
104 * Added initial framework for the Keyspan PDA serial converter so that
105 * Brian Warner has a place to put his code.
106 * Made the ezusb specific functions generic enough that different
107 * devices can use them (whiteheat and keyspan_pda both need them).
108 * Split out a whole bunch of structure and other stuff to a seperate
110 * Made the Visor connection messages a little more understandable, now
111 * that Miles Lott (milos@insync.net) has gotten the Generic channel to
112 * work. Also made them always show up in the log file.
115 * Added initial framework for FTDI serial converter so that Bill Ryder
116 * has a place to put his code.
117 * Added the vendor specific info from Handspring. Now we can print out
118 * informational debug messages as well as understand what is happening.
121 * Fixed problem of crash when trying to open a port that didn't have a
122 * device assigned to it. Made the minor node finding a little smarter,
123 * now it looks to find a continous space for the new device.
126 * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
127 * Fixed get_serial_by_minor which was all messed up for multi port
128 * devices. Fixed multi port problem for generic devices. Now the number
129 * of ports is determined by the number of bulk out endpoints for the
133 * Removed lots of cruft that was around from the old (pre urb) driver
135 * Made the serial_table dynamic. This should save lots of memory when
136 * the number of minor nodes goes up to 256.
137 * Added initial support for devices that have more than one port.
138 * Added more debugging comments for the Visor, and added a needed
139 * set_configuration call.
142 * Fixed the WhiteHEAT firmware (my processing tool had a bug)
143 * and added new debug loader firmware for it.
144 * Removed the put_char function as it isn't really needed.
145 * Added visor startup commands as found by the Win98 dump.
148 * Fixed the vendor id for the generic driver to the one I meant it to be.
151 * Forget the version numbering...that's pretty useless...
152 * Made the driver able to be compiled so that the user can select which
153 * converter they want to use. This allows people who only want the Visor
154 * support to not pay the memory size price of the WhiteHEAT.
155 * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
156 * grabbed the root hub. Not good.
158 * version 0.4.0 (01/10/2000) gkh
159 * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
160 * device. Added startup function to allow firmware to be downloaded to
161 * a device if it needs to be.
162 * Added firmware download logic to the WhiteHEAT device.
163 * Started to add #defines to split up the different drivers for potential
164 * configuration option.
166 * version 0.3.1 (12/30/99) gkh
167 * Fixed problems with urb for bulk out.
168 * Added initial support for multiple sets of endpoints. This enables
169 * the Handspring Visor to be attached successfully. Only the first
170 * bulk in / bulk out endpoint pair is being used right now.
172 * version 0.3.0 (12/27/99) gkh
173 * Added initial support for the Handspring Visor based on a patch from
174 * Miles Lott (milos@sneety.insync.net)
175 * Cleaned up the code a bunch and converted over to using urbs only.
177 * version 0.2.3 (12/21/99) gkh
178 * Added initial support for the Connect Tech WhiteHEAT converter.
179 * Incremented the number of ports in expectation of getting the
180 * WhiteHEAT to work properly (4 ports per connection).
181 * Added notification on insertion and removal of what port the
182 * device is/was connected to (and what kind of device it was).
184 * version 0.2.2 (12/16/99) gkh
185 * Changed major number to the new allocated number. We're legal now!
187 * version 0.2.1 (12/14/99) gkh
188 * Fixed bug that happens when device node is opened when there isn't a
189 * device attached to it. Thanks to marek@webdesign.no for noticing this.
191 * version 0.2.0 (11/10/99) gkh
192 * Split up internals to make it easier to add different types of serial
193 * converters to the code.
194 * Added a "generic" driver that gets it's vendor and product id
195 * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
196 * for the idea and sample code (from the usb scanner driver.)
197 * Cleared up any licensing questions by releasing it under the GNU GPL.
199 * version 0.1.2 (10/25/99) gkh
200 * Fixed bug in detecting device.
202 * version 0.1.1 (10/05/99) gkh
203 * Changed the major number to not conflict with anything else.
205 * version 0.1 (09/28/99) gkh
206 * Can recognize the two different devices and start up a read from
207 * device when asked to. Writes also work. No control signals yet, this
208 * all is vendor specific data (i.e. no spec), also no control for
209 * different baud rates or other bit settings.
210 * Currently we are using the same devid as the acm driver. This needs
215 #include <linux/config.h>
216 #include <linux/kernel.h>
217 #include <linux/sched.h>
218 #include <linux/signal.h>
219 #include <linux/errno.h>
220 #include <linux/poll.h>
221 #include <linux/init.h>
222 #include <linux/malloc.h>
223 #include <linux/fcntl.h>
224 #include <linux/tty_driver.h>
225 #include <linux/tty_flip.h>
226 #include <linux/tty.h>
227 #include <linux/module.h>
228 #include <linux/spinlock.h>
229 #include <linux/list.h>
230 #include <linux/smp_lock.h>
232 #ifdef CONFIG_USB_SERIAL_DEBUG
237 #include <linux/usb.h>
239 /* Module information */
240 MODULE_AUTHOR("Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/");
241 MODULE_DESCRIPTION("USB Serial Driver");
243 #include "usb-serial.h"
245 #define MAX(a,b) (((a)>(b))?(a):(b))
247 /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
248 /* need to always compile these in, as some of the other devices use these functions as their own. */
249 /* if a driver does not provide a function pointer, the generic function will be called. */
250 static int generic_open (struct usb_serial_port
*port
, struct file
*filp
);
251 static void generic_close (struct usb_serial_port
*port
, struct file
*filp
);
252 static int generic_write (struct usb_serial_port
*port
, int from_user
, const unsigned char *buf
, int count
);
253 static int generic_write_room (struct usb_serial_port
*port
);
254 static int generic_chars_in_buffer (struct usb_serial_port
*port
);
255 static void generic_read_bulk_callback (struct urb
*urb
);
256 static void generic_write_bulk_callback (struct urb
*urb
);
259 #ifdef CONFIG_USB_SERIAL_GENERIC
260 static __u16 vendor
= 0x05f9;
261 static __u16 product
= 0xffff;
262 MODULE_PARM(vendor
, "i");
263 MODULE_PARM_DESC(vendor
, "User specified USB idVendor");
265 MODULE_PARM(product
, "i");
266 MODULE_PARM_DESC(product
, "User specified USB idProduct");
268 /* All of the device info needed for the Generic Serial Converter */
269 static struct usb_serial_device_type generic_device
= {
271 idVendor
: &vendor
, /* use the user specified vendor id */
272 idProduct
: &product
, /* use the user specified product id */
273 needs_interrupt_in
: DONT_CARE
, /* don't have to have an interrupt in endpoint */
274 needs_bulk_in
: DONT_CARE
, /* don't have to have a bulk in endpoint */
275 needs_bulk_out
: DONT_CARE
, /* don't have to have a bulk out endpoint */
276 num_interrupt_in
: NUM_DONT_CARE
,
277 num_bulk_in
: NUM_DONT_CARE
,
278 num_bulk_out
: NUM_DONT_CARE
,
284 /* To add support for another serial converter, create a usb_serial_device_type
285 structure for that device, and add it to this list, making sure that the
286 last entry is NULL. */
287 static struct usb_serial_device_type
*usb_serial_devices
[] = {
288 #ifdef CONFIG_USB_SERIAL_GENERIC
291 #ifdef CONFIG_USB_SERIAL_WHITEHEAT
292 &whiteheat_fake_device
,
295 #ifdef CONFIG_USB_SERIAL_VISOR
298 #ifdef CONFIG_USB_SERIAL_FTDI_SIO
301 #ifdef CONFIG_USB_SERIAL_KEYSPAN_PDA
302 &keyspan_pda_fake_device
,
305 #ifdef CONFIG_USB_SERIAL_OMNINET
306 &zyxel_omninet_device
,
308 #ifdef CONFIG_USB_SERIAL_DIGI_ACCELEPORT
309 &digi_acceleport_device
,
315 /* local function prototypes */
316 static int serial_open (struct tty_struct
*tty
, struct file
* filp
);
317 static void serial_close (struct tty_struct
*tty
, struct file
* filp
);
318 static int serial_write (struct tty_struct
* tty
, int from_user
, const unsigned char *buf
, int count
);
319 static int serial_write_room (struct tty_struct
*tty
);
320 static int serial_chars_in_buffer (struct tty_struct
*tty
);
321 static void serial_throttle (struct tty_struct
* tty
);
322 static void serial_unthrottle (struct tty_struct
* tty
);
323 static int serial_ioctl (struct tty_struct
*tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
);
324 static void serial_set_termios (struct tty_struct
*tty
, struct termios
* old
);
326 static void * usb_serial_probe(struct usb_device
*dev
, unsigned int ifnum
);
327 static void usb_serial_disconnect(struct usb_device
*dev
, void *ptr
);
329 static struct usb_driver usb_serial_driver
= {
331 probe
: usb_serial_probe
,
332 disconnect
: usb_serial_disconnect
,
335 static int serial_refcount
;
336 static struct tty_driver serial_tty_driver
;
337 static struct tty_struct
* serial_tty
[SERIAL_TTY_MINORS
];
338 static struct termios
* serial_termios
[SERIAL_TTY_MINORS
];
339 static struct termios
* serial_termios_locked
[SERIAL_TTY_MINORS
];
340 static struct usb_serial
*serial_table
[SERIAL_TTY_MINORS
] = {NULL
, };
343 static inline struct usb_serial
* get_usb_serial (struct usb_serial_port
*port
, const char *function
)
345 /* if no port was specified, or it fails a paranoia check */
347 port_paranoia_check (port
, function
) ||
348 serial_paranoia_check (port
->serial
, function
)) {
349 /* then say that we dont have a valid usb_serial thing, which will
350 * end up genrating -ENODEV return values */
358 static struct usb_serial
*get_serial_by_minor (int minor
)
360 return serial_table
[minor
];
364 static struct usb_serial
*get_free_serial (int num_ports
, int *minor
)
366 struct usb_serial
*serial
= NULL
;
370 dbg(__FUNCTION__
" %d", num_ports
);
373 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
378 for (j
= 1; j
<= num_ports
-1; ++j
)
379 if (serial_table
[i
+j
])
384 if (!(serial
= kmalloc(sizeof(struct usb_serial
), GFP_KERNEL
))) {
385 err(__FUNCTION__
" - Out of memory");
388 memset(serial
, 0, sizeof(struct usb_serial
));
389 serial
->magic
= USB_SERIAL_MAGIC
;
390 serial_table
[i
] = serial
;
392 dbg(__FUNCTION__
" - minor base = %d", *minor
);
393 for (i
= *minor
+1; (i
< (*minor
+ num_ports
)) && (i
< SERIAL_TTY_MINORS
); ++i
)
394 serial_table
[i
] = serial
;
401 static void return_serial (struct usb_serial
*serial
)
410 for (i
= 0; i
< serial
->num_ports
; ++i
) {
411 serial_table
[serial
->minor
+ i
] = NULL
;
418 #ifdef USES_EZUSB_FUNCTIONS
419 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
420 #define CPUCS_REG 0x7F92
422 int ezusb_writememory (struct usb_serial
*serial
, int address
, unsigned char *data
, int length
, __u8 bRequest
)
425 unsigned char *transfer_buffer
= kmalloc (length
, GFP_KERNEL
);
427 // dbg("ezusb_writememory %x, %d", address, length);
429 if (!transfer_buffer
) {
430 err(__FUNCTION__
" - kmalloc(%d) failed.", length
);
433 memcpy (transfer_buffer
, data
, length
);
434 result
= usb_control_msg (serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0), bRequest
, 0x40, address
, 0, transfer_buffer
, length
, 300);
435 kfree (transfer_buffer
);
440 int ezusb_set_reset (struct usb_serial
*serial
, unsigned char reset_bit
)
443 dbg(__FUNCTION__
" - %d", reset_bit
);
444 response
= ezusb_writememory (serial
, CPUCS_REG
, &reset_bit
, 1, 0xa0);
446 err(__FUNCTION__
"- %d failed", reset_bit
);
451 #endif /* USES_EZUSB_FUNCTIONS */
454 /*****************************************************************************
455 * Driver tty interface functions
456 *****************************************************************************/
457 static int serial_open (struct tty_struct
*tty
, struct file
* filp
)
459 struct usb_serial
*serial
;
460 struct usb_serial_port
*port
;
465 /* initialize the pointer incase something fails */
466 tty
->driver_data
= NULL
;
468 /* get the serial object associated with this tty pointer */
469 serial
= get_serial_by_minor (MINOR(tty
->device
));
471 if (serial_paranoia_check (serial
, __FUNCTION__
)) {
475 /* set up our port structure making the tty driver remember our port object, and us it */
476 portNumber
= MINOR(tty
->device
) - serial
->minor
;
477 port
= &serial
->port
[portNumber
];
478 tty
->driver_data
= port
;
481 /* pass on to the driver specific version of this function if it is available */
482 if (serial
->type
->open
) {
483 return (serial
->type
->open(port
, filp
));
485 return (generic_open(port
, filp
));
490 static void serial_close(struct tty_struct
*tty
, struct file
* filp
)
492 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
493 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
499 dbg(__FUNCTION__
" - port %d", port
->number
);
502 dbg (__FUNCTION__
" - port not opened");
506 /* pass on to the driver specific version of this function if it is available */
507 if (serial
->type
->close
) {
508 serial
->type
->close(port
, filp
);
510 generic_close(port
, filp
);
515 static int serial_write (struct tty_struct
* tty
, int from_user
, const unsigned char *buf
, int count
)
517 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
518 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
524 dbg(__FUNCTION__
" - port %d, %d byte(s)", port
->number
, count
);
527 dbg (__FUNCTION__
" - port not opened");
531 /* pass on to the driver specific version of this function if it is available */
532 if (serial
->type
->write
) {
533 return (serial
->type
->write(port
, from_user
, buf
, count
));
535 return (generic_write(port
, from_user
, buf
, count
));
540 static int serial_write_room (struct tty_struct
*tty
)
542 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
543 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
549 dbg(__FUNCTION__
" - port %d", port
->number
);
552 dbg (__FUNCTION__
" - port not open");
556 /* pass on to the driver specific version of this function if it is available */
557 if (serial
->type
->write_room
) {
558 return (serial
->type
->write_room(port
));
560 return (generic_write_room(port
));
565 static int serial_chars_in_buffer (struct tty_struct
*tty
)
567 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
568 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
575 dbg (__FUNCTION__
" - port not open");
579 /* pass on to the driver specific version of this function if it is available */
580 if (serial
->type
->chars_in_buffer
) {
581 return (serial
->type
->chars_in_buffer(port
));
583 return (generic_chars_in_buffer(port
));
588 static void serial_throttle (struct tty_struct
* tty
)
590 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
591 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
597 dbg(__FUNCTION__
" - port %d", port
->number
);
600 dbg (__FUNCTION__
" - port not open");
604 /* pass on to the driver specific version of this function */
605 if (serial
->type
->throttle
) {
606 serial
->type
->throttle(port
);
613 static void serial_unthrottle (struct tty_struct
* tty
)
615 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
616 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
622 dbg(__FUNCTION__
" - port %d", port
->number
);
625 dbg (__FUNCTION__
" - port not open");
629 /* pass on to the driver specific version of this function */
630 if (serial
->type
->unthrottle
) {
631 serial
->type
->unthrottle(port
);
638 static int serial_ioctl (struct tty_struct
*tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
640 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
641 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
647 dbg(__FUNCTION__
" - port %d", port
->number
);
650 dbg (__FUNCTION__
" - port not open");
654 /* pass on to the driver specific version of this function if it is available */
655 if (serial
->type
->ioctl
) {
656 return (serial
->type
->ioctl(port
, file
, cmd
, arg
));
663 static void serial_set_termios (struct tty_struct
*tty
, struct termios
* old
)
665 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
666 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
672 dbg(__FUNCTION__
" - port %d", port
->number
);
675 dbg (__FUNCTION__
" - port not open");
679 /* pass on to the driver specific version of this function if it is available */
680 if (serial
->type
->set_termios
) {
681 serial
->type
->set_termios(port
, old
);
688 static void serial_break (struct tty_struct
*tty
, int break_state
)
690 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
691 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
697 dbg(__FUNCTION__
" - port %d", port
->number
);
700 dbg (__FUNCTION__
" - port not open");
704 /* pass on to the driver specific version of this function if it is
706 if (serial
->type
->break_ctl
) {
707 serial
->type
->break_ctl(port
, break_state
);
713 /*****************************************************************************
714 * generic devices specific driver functions
715 *****************************************************************************/
716 static int generic_open (struct usb_serial_port
*port
, struct file
*filp
)
718 struct usb_serial
*serial
= port
->serial
;
720 dbg(__FUNCTION__
" - port %d", port
->number
);
723 dbg (__FUNCTION__
" - device already open");
728 /* if we have a bulk interrupt, start reading from it */
729 if (serial
->num_bulk_in
) {
730 /*Start reading from the device*/
731 if (usb_submit_urb(port
->read_urb
))
732 dbg(__FUNCTION__
" - usb_submit_urb(read bulk) failed");
739 static void generic_close (struct usb_serial_port
*port
, struct file
* filp
)
741 struct usb_serial
*serial
= port
->serial
;
743 dbg(__FUNCTION__
" - port %d", port
->number
);
745 /* shutdown any bulk reads that might be going on */
746 if (serial
->num_bulk_out
) {
747 usb_unlink_urb (port
->write_urb
);
749 if (serial
->num_bulk_in
) {
750 usb_unlink_urb (port
->read_urb
);
757 static int generic_write (struct usb_serial_port
*port
, int from_user
, const unsigned char *buf
, int count
)
759 struct usb_serial
*serial
= port
->serial
;
761 dbg(__FUNCTION__
" - port %d", port
->number
);
764 dbg(__FUNCTION__
" - write request of 0 bytes");
768 /* only do something if we have a bulk out endpoint */
769 if (serial
->num_bulk_out
) {
770 if (port
->write_urb
->status
== -EINPROGRESS
) {
771 dbg (__FUNCTION__
" - already writing");
775 count
= (count
> port
->bulk_out_size
) ? port
->bulk_out_size
: count
;
780 printk (KERN_DEBUG __FILE__
": " __FUNCTION__
" - length = %d, data = ", count
);
781 for (i
= 0; i
< count
; ++i
) {
782 printk ("%.2x ", buf
[i
]);
789 copy_from_user(port
->write_urb
->transfer_buffer
, buf
, count
);
792 memcpy (port
->write_urb
->transfer_buffer
, buf
, count
);
795 /* send the data out the bulk port */
796 port
->write_urb
->transfer_buffer_length
= count
;
798 if (usb_submit_urb(port
->write_urb
))
799 dbg(__FUNCTION__
" - usb_submit_urb(write bulk) failed");
804 /* no bulk out, so return 0 bytes written */
809 static int generic_write_room (struct usb_serial_port
*port
)
811 struct usb_serial
*serial
= port
->serial
;
814 dbg(__FUNCTION__
" - port %d", port
->number
);
816 if (serial
->num_bulk_out
)
817 if (port
->write_urb
->status
!= -EINPROGRESS
)
818 room
= port
->bulk_out_size
;
820 dbg(__FUNCTION__
" - returns %d", room
);
825 static int generic_chars_in_buffer (struct usb_serial_port
*port
)
827 struct usb_serial
*serial
= port
->serial
;
830 dbg(__FUNCTION__
" - port %d", port
->number
);
832 if (serial
->num_bulk_out
)
833 if (port
->write_urb
->status
== -EINPROGRESS
)
834 chars
= port
->write_urb
->transfer_buffer_length
;
836 dbg (__FUNCTION__
" - returns %d", chars
);
841 static void generic_read_bulk_callback (struct urb
*urb
)
843 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
844 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
845 struct tty_struct
*tty
;
846 unsigned char *data
= urb
->transfer_buffer
;
849 dbg(__FUNCTION__
" - port %d", port
->number
);
852 dbg(__FUNCTION__
" - bad serial pointer, exiting");
857 dbg(__FUNCTION__
" - nonzero read bulk status received: %d", urb
->status
);
862 if (urb
->actual_length
) {
863 printk (KERN_DEBUG __FILE__
": " __FUNCTION__
"- length = %d, data = ", urb
->actual_length
);
864 for (i
= 0; i
< urb
->actual_length
; ++i
) {
865 printk ("%.2x ", data
[i
]);
872 if (urb
->actual_length
) {
873 for (i
= 0; i
< urb
->actual_length
; ++i
) {
874 tty_insert_flip_char(tty
, data
[i
], 0);
876 tty_flip_buffer_push(tty
);
879 /* Continue trying to always read */
880 if (usb_submit_urb(urb
))
881 dbg(__FUNCTION__
" - failed resubmitting read urb");
887 static void generic_write_bulk_callback (struct urb
*urb
)
889 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
890 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
892 dbg(__FUNCTION__
" - port %d", port
->number
);
895 dbg(__FUNCTION__
" - bad serial pointer, exiting");
900 dbg(__FUNCTION__
" - nonzero write bulk status received: %d", urb
->status
);
904 queue_task(&port
->tqueue
, &tq_immediate
);
905 mark_bh(IMMEDIATE_BH
);
911 static void port_softint(void *private)
913 struct usb_serial_port
*port
= (struct usb_serial_port
*)private;
914 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
915 struct tty_struct
*tty
;
917 dbg(__FUNCTION__
" - port %d", port
->number
);
924 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) && tty
->ldisc
.write_wakeup
) {
925 dbg(__FUNCTION__
" - write wakeup call.");
926 (tty
->ldisc
.write_wakeup
)(tty
);
929 wake_up_interruptible(&tty
->write_wait
);
934 static void * usb_serial_probe(struct usb_device
*dev
, unsigned int ifnum
)
936 struct usb_serial
*serial
= NULL
;
937 struct usb_serial_port
*port
;
938 struct usb_interface_descriptor
*interface
;
939 struct usb_endpoint_descriptor
*endpoint
;
940 struct usb_endpoint_descriptor
*interrupt_in_endpoint
[MAX_NUM_PORTS
];
941 struct usb_endpoint_descriptor
*bulk_in_endpoint
[MAX_NUM_PORTS
];
942 struct usb_endpoint_descriptor
*bulk_out_endpoint
[MAX_NUM_PORTS
];
943 struct usb_serial_device_type
*type
;
951 int num_interrupt_in
= 0;
953 int num_bulk_out
= 0;
957 /* loop through our list of known serial converters, and see if this
959 for (device_num
= 0; usb_serial_devices
[device_num
]; device_num
++) {
960 type
= usb_serial_devices
[device_num
];
961 dbg ("Looking at %s Vendor id=%.4x Product id=%.4x",
962 type
->name
, *(type
->idVendor
), *(type
->idProduct
));
964 /* look at the device descriptor */
965 if ((dev
->descriptor
.idVendor
== *(type
->idVendor
)) &&
966 (dev
->descriptor
.idProduct
== *(type
->idProduct
))) {
967 dbg("descriptor matches");
971 if (!usb_serial_devices
[device_num
]) {
977 /* descriptor matches, let's find the endpoints needed */
978 interrupt_pipe
= bulk_in_pipe
= bulk_out_pipe
= HAS_NOT
;
980 /* check out the endpoints */
981 interface
= &dev
->actconfig
->interface
[ifnum
].altsetting
[0];
982 for (i
= 0; i
< interface
->bNumEndpoints
; ++i
) {
983 endpoint
= &interface
->endpoint
[i
];
985 if ((endpoint
->bEndpointAddress
& 0x80) &&
986 ((endpoint
->bmAttributes
& 3) == 0x02)) {
987 /* we found a bulk in endpoint */
988 dbg("found bulk in");
990 bulk_in_endpoint
[num_bulk_in
] = endpoint
;
994 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
995 ((endpoint
->bmAttributes
& 3) == 0x02)) {
996 /* we found a bulk out endpoint */
997 dbg("found bulk out");
999 bulk_out_endpoint
[num_bulk_out
] = endpoint
;
1003 if ((endpoint
->bEndpointAddress
& 0x80) &&
1004 ((endpoint
->bmAttributes
& 3) == 0x03)) {
1005 /* we found a interrupt in endpoint */
1006 dbg("found interrupt in");
1007 interrupt_pipe
= HAS
;
1008 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
1013 /* verify that we found all of the endpoints that we need */
1014 if (!((interrupt_pipe
& type
->needs_interrupt_in
) &&
1015 (bulk_in_pipe
& type
->needs_bulk_in
) &&
1016 (bulk_out_pipe
& type
->needs_bulk_out
))) {
1017 /* nope, they don't match what we expected */
1018 info("descriptors matched, but endpoints did not");
1022 /* found all that we need */
1024 info("%s converter detected", type
->name
);
1026 #ifdef CONFIG_USB_SERIAL_GENERIC
1027 if (type
== &generic_device
) {
1028 num_ports
= num_bulk_out
;
1029 if (num_ports
== 0) {
1030 err("Generic device with no bulk out, not allowed.");
1036 num_ports
= type
->num_ports
;
1038 serial
= get_free_serial (num_ports
, &minor
);
1039 if (serial
== NULL
) {
1040 err("No more free serial devices");
1046 serial
->type
= type
;
1047 serial
->minor
= minor
;
1048 serial
->num_ports
= num_ports
;
1049 serial
->num_bulk_in
= num_bulk_in
;
1050 serial
->num_bulk_out
= num_bulk_out
;
1051 serial
->num_interrupt_in
= num_interrupt_in
;
1053 /* if this device type has a startup function, call it */
1054 if (type
->startup
) {
1055 if (type
->startup (serial
)) {
1060 /* set up the endpoint information */
1061 for (i
= 0; i
< num_bulk_in
; ++i
) {
1062 endpoint
= bulk_in_endpoint
[i
];
1063 port
= &serial
->port
[i
];
1064 port
->read_urb
= usb_alloc_urb (0);
1065 if (!port
->read_urb
) {
1066 err("No free urbs available");
1069 buffer_size
= endpoint
->wMaxPacketSize
;
1070 port
->bulk_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1071 if (!port
->bulk_in_buffer
) {
1072 err("Couldn't allocate bulk_in_buffer");
1075 FILL_BULK_URB(port
->read_urb
, dev
,
1076 usb_rcvbulkpipe(dev
, endpoint
->bEndpointAddress
),
1077 port
->bulk_in_buffer
, buffer_size
,
1078 ((serial
->type
->read_bulk_callback
) ?
1079 serial
->type
->read_bulk_callback
:
1080 generic_read_bulk_callback
),
1084 for (i
= 0; i
< num_bulk_out
; ++i
) {
1085 endpoint
= bulk_out_endpoint
[i
];
1086 port
= &serial
->port
[i
];
1087 port
->write_urb
= usb_alloc_urb(0);
1088 if (!port
->write_urb
) {
1089 err("No free urbs available");
1092 buffer_size
= endpoint
->wMaxPacketSize
;
1093 port
->bulk_out_size
= buffer_size
;
1094 port
->bulk_out_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1095 if (!port
->bulk_out_buffer
) {
1096 err("Couldn't allocate bulk_out_buffer");
1099 FILL_BULK_URB(port
->write_urb
, dev
,
1100 usb_sndbulkpipe(dev
, endpoint
->bEndpointAddress
),
1101 port
->bulk_out_buffer
, buffer_size
,
1102 ((serial
->type
->write_bulk_callback
) ?
1103 serial
->type
->write_bulk_callback
:
1104 generic_write_bulk_callback
),
1108 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1109 endpoint
= interrupt_in_endpoint
[i
];
1110 port
= &serial
->port
[i
];
1111 port
->interrupt_in_urb
= usb_alloc_urb(0);
1112 if (!port
->interrupt_in_urb
) {
1113 err("No free urbs available");
1116 buffer_size
= endpoint
->wMaxPacketSize
;
1117 port
->interrupt_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1118 if (!port
->interrupt_in_buffer
) {
1119 err("Couldn't allocate interrupt_in_buffer");
1122 FILL_INT_URB(port
->interrupt_in_urb
, dev
,
1123 usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
),
1124 port
->interrupt_in_buffer
, buffer_size
,
1125 serial
->type
->read_int_callback
,
1127 endpoint
->bInterval
);
1130 /* initialize some parts of the port structures */
1131 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1132 max_endpoints
= MAX(num_bulk_in
, num_bulk_out
);
1133 max_endpoints
= MAX(max_endpoints
, num_interrupt_in
);
1134 for (i
= 0; i
< max_endpoints
; ++i
) {
1135 port
= &serial
->port
[i
];
1136 port
->number
= i
+ serial
->minor
;
1137 port
->serial
= serial
;
1138 port
->magic
= USB_SERIAL_PORT_MAGIC
;
1139 port
->tqueue
.routine
= port_softint
;
1140 port
->tqueue
.data
= port
;
1143 /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
1144 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1145 tty_register_devfs (&serial_tty_driver
, 0, serial
->port
[i
].number
);
1146 info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)",
1147 type
->name
, serial
->port
[i
].number
, serial
->port
[i
].number
);
1150 return serial
; /* success */
1154 for (i
= 0; i
< num_bulk_in
; ++i
) {
1155 port
= &serial
->port
[i
];
1157 usb_free_urb (port
->read_urb
);
1158 if (port
->bulk_in_buffer
)
1159 kfree (port
->bulk_in_buffer
);
1161 for (i
= 0; i
< num_bulk_out
; ++i
) {
1162 port
= &serial
->port
[i
];
1163 if (port
->write_urb
)
1164 usb_free_urb (port
->write_urb
);
1165 if (port
->bulk_out_buffer
)
1166 kfree (port
->bulk_out_buffer
);
1168 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1169 port
= &serial
->port
[i
];
1170 if (port
->interrupt_in_urb
)
1171 usb_free_urb (port
->interrupt_in_urb
);
1172 if (port
->interrupt_in_buffer
)
1173 kfree (port
->interrupt_in_buffer
);
1176 /* return the minor range that this device had */
1177 return_serial (serial
);
1179 /* free up any memory that we allocated */
1186 static void usb_serial_disconnect(struct usb_device
*dev
, void *ptr
)
1188 struct usb_serial
*serial
= (struct usb_serial
*) ptr
;
1189 struct usb_serial_port
*port
;
1193 if (serial
->type
->shutdown
)
1194 serial
->type
->shutdown(serial
);
1196 for (i
= 0; i
< serial
->num_ports
; ++i
)
1197 serial
->port
[i
].active
= 0;
1199 for (i
= 0; i
< serial
->num_bulk_in
; ++i
) {
1200 port
= &serial
->port
[i
];
1201 if (port
->read_urb
) {
1202 usb_unlink_urb (port
->read_urb
);
1203 usb_free_urb (port
->read_urb
);
1205 if (port
->bulk_in_buffer
)
1206 kfree (port
->bulk_in_buffer
);
1208 for (i
= 0; i
< serial
->num_bulk_out
; ++i
) {
1209 port
= &serial
->port
[i
];
1210 if (port
->write_urb
) {
1211 usb_unlink_urb (port
->write_urb
);
1212 usb_free_urb (port
->write_urb
);
1214 if (port
->bulk_out_buffer
)
1215 kfree (port
->bulk_out_buffer
);
1217 for (i
= 0; i
< serial
->num_interrupt_in
; ++i
) {
1218 port
= &serial
->port
[i
];
1219 if (port
->interrupt_in_urb
) {
1220 usb_unlink_urb (port
->interrupt_in_urb
);
1221 usb_free_urb (port
->interrupt_in_urb
);
1223 if (port
->interrupt_in_buffer
)
1224 kfree (port
->interrupt_in_buffer
);
1227 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1228 tty_unregister_devfs (&serial_tty_driver
, serial
->port
[i
].number
);
1229 info("%s converter now disconnected from ttyUSB%d", serial
->type
->name
, serial
->port
[i
].number
);
1232 /* return the minor range that this device had */
1233 return_serial (serial
);
1235 /* free up any memory that we allocated */
1239 info("device disconnected");
1246 static struct tty_driver serial_tty_driver
= {
1247 magic
: TTY_DRIVER_MAGIC
,
1248 driver_name
: "usb-serial",
1250 major
: SERIAL_TTY_MAJOR
,
1252 num
: SERIAL_TTY_MINORS
,
1253 type
: TTY_DRIVER_TYPE_SERIAL
,
1254 subtype
: SERIAL_TYPE_NORMAL
,
1255 flags
: TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
,
1257 refcount
: &serial_refcount
,
1259 termios
: serial_termios
,
1260 termios_locked
: serial_termios_locked
,
1263 close
: serial_close
,
1264 write
: serial_write
,
1265 write_room
: serial_write_room
,
1266 ioctl
: serial_ioctl
,
1267 set_termios
: serial_set_termios
,
1268 throttle
: serial_throttle
,
1269 unthrottle
: serial_unthrottle
,
1270 break_ctl
: serial_break
,
1271 chars_in_buffer
: serial_chars_in_buffer
,
1275 int usb_serial_init(void)
1281 /* Initalize our global data */
1282 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
1283 serial_table
[i
] = NULL
;
1286 /* tell the world what devices this driver currently supports */
1288 for (i
= 0; usb_serial_devices
[i
]; ++i
) {
1289 if (!strstr (usb_serial_devices
[i
]->name
, "prerenumeration")) {
1290 info ("USB Serial support registered for %s", usb_serial_devices
[i
]->name
);
1295 info ("USB Serial driver is not configured for any devices!");
1297 /* register the tty driver */
1298 serial_tty_driver
.init_termios
= tty_std_termios
;
1299 serial_tty_driver
.init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1300 if (tty_register_driver (&serial_tty_driver
)) {
1301 err(__FUNCTION__
" - failed to register tty driver");
1305 /* register the USB driver */
1306 result
= usb_register(&usb_serial_driver
);
1308 tty_unregister_driver(&serial_tty_driver
);
1309 err("usb_register failed for the usb-serial driver. Error number %d", result
);
1317 void usb_serial_exit(void)
1319 usb_deregister(&usb_serial_driver
);
1320 tty_unregister_driver(&serial_tty_driver
);
1324 module_init(usb_serial_init
);
1325 module_exit(usb_serial_exit
);