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 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
21 * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
22 * removed from the individual device source files.
25 * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
28 * Changed devfs and tty register code to work properly now. This was based on
29 * the ACM driver changes by Vojtech Pavlik.
31 * (04/27/2000) Ryan VanderBijl
32 * Put calls to *_paranoia_checks into one function.
35 * Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
36 * Moved when the startup code printed out the devices that are supported.
39 * Added driver for ZyXEL omni.net lcd plus ISDN TA
40 * Made startup info message specify which drivers were compiled in.
43 * Changed the probe process to remove the module unload races.
44 * Changed where the tty layer gets initialized to have devfs work nicer.
45 * Added initial devfs support.
48 * Split driver up into device specific pieces.
51 * Fixed oops that could happen when device was removed while a program
52 * was talking to the device.
53 * Removed the static urbs and now all urbs are created and destroyed
55 * Reworked the internal interface. Now everything is based on the
56 * usb_serial_port structure instead of the larger usb_serial structure.
57 * This fixes the bug that a multiport device could not have more than
58 * one port open at one time.
61 * Added config option for debugging messages.
62 * Added patch for keyspan pda from Brian Warner.
65 * Added the keyspan pda code from Brian Warner <warner@lothar.com>
66 * Moved a bunch of the port specific stuff into its own structure. This
67 * is in anticipation of the true multiport devices (there's a bug if you
68 * try to access more than one port of any multiport device right now)
71 * Made it so that any serial devices only have to specify which functions
72 * they want to overload from the generic function calls (great,
73 * inheritance in C, in a driver, just what I wanted...)
74 * Added support for set_termios and ioctl function calls. No drivers take
75 * advantage of this yet.
76 * Removed the #ifdef MODULE, now there is no module specific code.
77 * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
79 * Small fix to get_free_serial.
82 * Removed the Belkin and Peracom functionality from the driver due to
83 * the lack of support from the vendor, and me not wanting people to
84 * accidenatly buy the device, expecting it to work with Linux.
85 * Added read_bulk_callback and write_bulk_callback to the type structure
86 * for the needs of the FTDI and WhiteHEAT driver.
87 * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
89 * Changed the output urb size back to the max endpoint size to make
90 * the ftdi_sio driver have it easier, and due to the fact that it didn't
91 * really increase the speed any.
94 * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
95 * patch from Miles Lott (milos@insync.net).
96 * Fixed bug with not restoring the minor range that a device grabs, if
97 * the startup function fails (thanks Miles for finding this).
100 * Added initial framework for the Keyspan PDA serial converter so that
101 * Brian Warner has a place to put his code.
102 * Made the ezusb specific functions generic enough that different
103 * devices can use them (whiteheat and keyspan_pda both need them).
104 * Split out a whole bunch of structure and other stuff to a seperate
106 * Made the Visor connection messages a little more understandable, now
107 * that Miles Lott (milos@insync.net) has gotten the Generic channel to
108 * work. Also made them always show up in the log file.
111 * Added initial framework for FTDI serial converter so that Bill Ryder
112 * has a place to put his code.
113 * Added the vendor specific info from Handspring. Now we can print out
114 * informational debug messages as well as understand what is happening.
117 * Fixed problem of crash when trying to open a port that didn't have a
118 * device assigned to it. Made the minor node finding a little smarter,
119 * now it looks to find a continous space for the new device.
122 * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
123 * Fixed get_serial_by_minor which was all messed up for multi port
124 * devices. Fixed multi port problem for generic devices. Now the number
125 * of ports is determined by the number of bulk out endpoints for the
129 * Removed lots of cruft that was around from the old (pre urb) driver
131 * Made the serial_table dynamic. This should save lots of memory when
132 * the number of minor nodes goes up to 256.
133 * Added initial support for devices that have more than one port.
134 * Added more debugging comments for the Visor, and added a needed
135 * set_configuration call.
138 * Fixed the WhiteHEAT firmware (my processing tool had a bug)
139 * and added new debug loader firmware for it.
140 * Removed the put_char function as it isn't really needed.
141 * Added visor startup commands as found by the Win98 dump.
144 * Fixed the vendor id for the generic driver to the one I meant it to be.
147 * Forget the version numbering...that's pretty useless...
148 * Made the driver able to be compiled so that the user can select which
149 * converter they want to use. This allows people who only want the Visor
150 * support to not pay the memory size price of the WhiteHEAT.
151 * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
152 * grabbed the root hub. Not good.
154 * version 0.4.0 (01/10/2000) gkh
155 * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
156 * device. Added startup function to allow firmware to be downloaded to
157 * a device if it needs to be.
158 * Added firmware download logic to the WhiteHEAT device.
159 * Started to add #defines to split up the different drivers for potential
160 * configuration option.
162 * version 0.3.1 (12/30/99) gkh
163 * Fixed problems with urb for bulk out.
164 * Added initial support for multiple sets of endpoints. This enables
165 * the Handspring Visor to be attached successfully. Only the first
166 * bulk in / bulk out endpoint pair is being used right now.
168 * version 0.3.0 (12/27/99) gkh
169 * Added initial support for the Handspring Visor based on a patch from
170 * Miles Lott (milos@sneety.insync.net)
171 * Cleaned up the code a bunch and converted over to using urbs only.
173 * version 0.2.3 (12/21/99) gkh
174 * Added initial support for the Connect Tech WhiteHEAT converter.
175 * Incremented the number of ports in expectation of getting the
176 * WhiteHEAT to work properly (4 ports per connection).
177 * Added notification on insertion and removal of what port the
178 * device is/was connected to (and what kind of device it was).
180 * version 0.2.2 (12/16/99) gkh
181 * Changed major number to the new allocated number. We're legal now!
183 * version 0.2.1 (12/14/99) gkh
184 * Fixed bug that happens when device node is opened when there isn't a
185 * device attached to it. Thanks to marek@webdesign.no for noticing this.
187 * version 0.2.0 (11/10/99) gkh
188 * Split up internals to make it easier to add different types of serial
189 * converters to the code.
190 * Added a "generic" driver that gets it's vendor and product id
191 * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
192 * for the idea and sample code (from the usb scanner driver.)
193 * Cleared up any licensing questions by releasing it under the GNU GPL.
195 * version 0.1.2 (10/25/99) gkh
196 * Fixed bug in detecting device.
198 * version 0.1.1 (10/05/99) gkh
199 * Changed the major number to not conflict with anything else.
201 * version 0.1 (09/28/99) gkh
202 * Can recognize the two different devices and start up a read from
203 * device when asked to. Writes also work. No control signals yet, this
204 * all is vendor specific data (i.e. no spec), also no control for
205 * different baud rates or other bit settings.
206 * Currently we are using the same devid as the acm driver. This needs
211 #include <linux/config.h>
212 #include <linux/kernel.h>
213 #include <linux/sched.h>
214 #include <linux/signal.h>
215 #include <linux/errno.h>
216 #include <linux/poll.h>
217 #include <linux/init.h>
218 #include <linux/malloc.h>
219 #include <linux/fcntl.h>
220 #include <linux/tty_driver.h>
221 #include <linux/tty_flip.h>
222 #include <linux/tty.h>
223 #include <linux/module.h>
224 #include <linux/spinlock.h>
226 #ifdef CONFIG_USB_SERIAL_DEBUG
231 #include <linux/usb.h>
233 /* Module information */
234 MODULE_AUTHOR("Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/");
235 MODULE_DESCRIPTION("USB Serial Driver");
237 #include "usb-serial.h"
239 #define MAX(a,b) (((a)>(b))?(a):(b))
241 /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
242 /* need to always compile these in, as some of the other devices use these functions as their own. */
243 /* if a driver does not provide a function pointer, the generic function will be called. */
244 static int generic_open (struct usb_serial_port
*port
, struct file
*filp
);
245 static void generic_close (struct usb_serial_port
*port
, struct file
*filp
);
246 static int generic_write (struct usb_serial_port
*port
, int from_user
, const unsigned char *buf
, int count
);
247 static int generic_write_room (struct usb_serial_port
*port
);
248 static int generic_chars_in_buffer (struct usb_serial_port
*port
);
249 static void generic_read_bulk_callback (struct urb
*urb
);
250 static void generic_write_bulk_callback (struct urb
*urb
);
253 #ifdef CONFIG_USB_SERIAL_GENERIC
254 static __u16 vendor
= 0x05f9;
255 static __u16 product
= 0xffff;
256 MODULE_PARM(vendor
, "i");
257 MODULE_PARM_DESC(vendor
, "User specified USB idVendor");
259 MODULE_PARM(product
, "i");
260 MODULE_PARM_DESC(product
, "User specified USB idProduct");
262 /* All of the device info needed for the Generic Serial Converter */
263 static struct usb_serial_device_type generic_device
= {
265 idVendor
: &vendor
, /* use the user specified vendor id */
266 idProduct
: &product
, /* use the user specified product id */
267 needs_interrupt_in
: DONT_CARE
, /* don't have to have an interrupt in endpoint */
268 needs_bulk_in
: DONT_CARE
, /* don't have to have a bulk in endpoint */
269 needs_bulk_out
: DONT_CARE
, /* don't have to have a bulk out endpoint */
270 num_interrupt_in
: NUM_DONT_CARE
,
271 num_bulk_in
: NUM_DONT_CARE
,
272 num_bulk_out
: NUM_DONT_CARE
,
278 /* To add support for another serial converter, create a usb_serial_device_type
279 structure for that device, and add it to this list, making sure that the
280 last entry is NULL. */
281 static struct usb_serial_device_type
*usb_serial_devices
[] = {
282 #ifdef CONFIG_USB_SERIAL_GENERIC
285 #ifdef CONFIG_USB_SERIAL_WHITEHEAT
286 &whiteheat_fake_device
,
289 #ifdef CONFIG_USB_SERIAL_VISOR
292 #ifdef CONFIG_USB_SERIAL_FTDI_SIO
295 #ifdef CONFIG_USB_SERIAL_KEYSPAN_PDA
296 &keyspan_pda_fake_device
,
299 #ifdef CONFIG_USB_SERIAL_OMNINET
300 &zyxel_omninet_device
,
302 #ifdef CONFIG_USB_SERIAL_DIGI_ACCELEPORT
303 &digi_acceleport_device
,
309 /* local function prototypes */
310 static int serial_open (struct tty_struct
*tty
, struct file
* filp
);
311 static void serial_close (struct tty_struct
*tty
, struct file
* filp
);
312 static int serial_write (struct tty_struct
* tty
, int from_user
, const unsigned char *buf
, int count
);
313 static int serial_write_room (struct tty_struct
*tty
);
314 static int serial_chars_in_buffer (struct tty_struct
*tty
);
315 static void serial_throttle (struct tty_struct
* tty
);
316 static void serial_unthrottle (struct tty_struct
* tty
);
317 static int serial_ioctl (struct tty_struct
*tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
);
318 static void serial_set_termios (struct tty_struct
*tty
, struct termios
* old
);
320 static void * usb_serial_probe(struct usb_device
*dev
, unsigned int ifnum
);
321 static void usb_serial_disconnect(struct usb_device
*dev
, void *ptr
);
323 static struct usb_driver usb_serial_driver
= {
325 probe
: usb_serial_probe
,
326 disconnect
: usb_serial_disconnect
,
329 static int serial_refcount
;
330 static struct tty_driver serial_tty_driver
;
331 static struct tty_struct
* serial_tty
[SERIAL_TTY_MINORS
];
332 static struct termios
* serial_termios
[SERIAL_TTY_MINORS
];
333 static struct termios
* serial_termios_locked
[SERIAL_TTY_MINORS
];
334 static struct usb_serial
*serial_table
[SERIAL_TTY_MINORS
] = {NULL
, };
337 static inline struct usb_serial
* get_usb_serial (struct usb_serial_port
*port
, const char *function
)
339 /* if no port was specified, or it fails a paranoia check */
341 port_paranoia_check (port
, function
) ||
342 serial_paranoia_check (port
->serial
, function
)) {
343 /* then say that we dont have a valid usb_serial thing, which will
344 * end up genrating -ENODEV return values */
352 static struct usb_serial
*get_serial_by_minor (int minor
)
354 return serial_table
[minor
];
358 static struct usb_serial
*get_free_serial (int num_ports
, int *minor
)
360 struct usb_serial
*serial
= NULL
;
364 dbg(__FUNCTION__
" %d", num_ports
);
367 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
372 for (j
= 1; j
<= num_ports
-1; ++j
)
373 if (serial_table
[i
+j
])
378 if (!(serial
= kmalloc(sizeof(struct usb_serial
), GFP_KERNEL
))) {
379 err(__FUNCTION__
" - Out of memory");
382 memset(serial
, 0, sizeof(struct usb_serial
));
383 serial
->magic
= USB_SERIAL_MAGIC
;
384 serial_table
[i
] = serial
;
386 dbg(__FUNCTION__
" - minor base = %d", *minor
);
387 for (i
= *minor
+1; (i
< (*minor
+ num_ports
)) && (i
< SERIAL_TTY_MINORS
); ++i
)
388 serial_table
[i
] = serial
;
395 static void return_serial (struct usb_serial
*serial
)
404 for (i
= 0; i
< serial
->num_ports
; ++i
) {
405 serial_table
[serial
->minor
+ i
] = NULL
;
412 #ifdef USES_EZUSB_FUNCTIONS
413 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
414 #define CPUCS_REG 0x7F92
416 int ezusb_writememory (struct usb_serial
*serial
, int address
, unsigned char *data
, int length
, __u8 bRequest
)
419 unsigned char *transfer_buffer
= kmalloc (length
, GFP_KERNEL
);
421 // dbg("ezusb_writememory %x, %d", address, length);
423 if (!transfer_buffer
) {
424 err(__FUNCTION__
" - kmalloc(%d) failed.", length
);
427 memcpy (transfer_buffer
, data
, length
);
428 result
= usb_control_msg (serial
->dev
, usb_sndctrlpipe(serial
->dev
, 0), bRequest
, 0x40, address
, 0, transfer_buffer
, length
, 300);
429 kfree (transfer_buffer
);
434 int ezusb_set_reset (struct usb_serial
*serial
, unsigned char reset_bit
)
437 dbg(__FUNCTION__
" - %d", reset_bit
);
438 response
= ezusb_writememory (serial
, CPUCS_REG
, &reset_bit
, 1, 0xa0);
440 err(__FUNCTION__
"- %d failed", reset_bit
);
445 #endif /* USES_EZUSB_FUNCTIONS */
448 /*****************************************************************************
449 * Driver tty interface functions
450 *****************************************************************************/
451 static int serial_open (struct tty_struct
*tty
, struct file
* filp
)
453 struct usb_serial
*serial
;
454 struct usb_serial_port
*port
;
459 /* initialize the pointer incase something fails */
460 tty
->driver_data
= NULL
;
462 /* get the serial object associated with this tty pointer */
463 serial
= get_serial_by_minor (MINOR(tty
->device
));
465 if (serial_paranoia_check (serial
, __FUNCTION__
)) {
469 /* set up our port structure making the tty driver remember our port object, and us it */
470 portNumber
= MINOR(tty
->device
) - serial
->minor
;
471 port
= &serial
->port
[portNumber
];
472 tty
->driver_data
= port
;
475 /* pass on to the driver specific version of this function if it is available */
476 if (serial
->type
->open
) {
477 return (serial
->type
->open(port
, filp
));
479 return (generic_open(port
, filp
));
484 static void serial_close(struct tty_struct
*tty
, struct file
* filp
)
486 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
487 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
493 dbg(__FUNCTION__
" - port %d", port
->number
);
496 dbg (__FUNCTION__
" - port not opened");
500 /* pass on to the driver specific version of this function if it is available */
501 if (serial
->type
->close
) {
502 serial
->type
->close(port
, filp
);
504 generic_close(port
, filp
);
509 static int serial_write (struct tty_struct
* tty
, int from_user
, const unsigned char *buf
, int count
)
511 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
512 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
518 dbg(__FUNCTION__
" - port %d, %d byte(s)", port
->number
, count
);
521 dbg (__FUNCTION__
" - port not opened");
525 /* pass on to the driver specific version of this function if it is available */
526 if (serial
->type
->write
) {
527 return (serial
->type
->write(port
, from_user
, buf
, count
));
529 return (generic_write(port
, from_user
, buf
, count
));
534 static int serial_write_room (struct tty_struct
*tty
)
536 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
537 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
543 dbg(__FUNCTION__
" - port %d", port
->number
);
546 dbg (__FUNCTION__
" - port not open");
550 /* pass on to the driver specific version of this function if it is available */
551 if (serial
->type
->write_room
) {
552 return (serial
->type
->write_room(port
));
554 return (generic_write_room(port
));
559 static int serial_chars_in_buffer (struct tty_struct
*tty
)
561 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
562 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
569 dbg (__FUNCTION__
" - port not open");
573 /* pass on to the driver specific version of this function if it is available */
574 if (serial
->type
->chars_in_buffer
) {
575 return (serial
->type
->chars_in_buffer(port
));
577 return (generic_chars_in_buffer(port
));
582 static void serial_throttle (struct tty_struct
* tty
)
584 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
585 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
591 dbg(__FUNCTION__
" - port %d", port
->number
);
594 dbg (__FUNCTION__
" - port not open");
598 /* pass on to the driver specific version of this function */
599 if (serial
->type
->throttle
) {
600 serial
->type
->throttle(port
);
607 static void serial_unthrottle (struct tty_struct
* tty
)
609 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
610 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
616 dbg(__FUNCTION__
" - port %d", port
->number
);
619 dbg (__FUNCTION__
" - port not open");
623 /* pass on to the driver specific version of this function */
624 if (serial
->type
->unthrottle
) {
625 serial
->type
->unthrottle(port
);
632 static int serial_ioctl (struct tty_struct
*tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
634 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
635 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
641 dbg(__FUNCTION__
" - port %d", port
->number
);
644 dbg (__FUNCTION__
" - port not open");
648 /* pass on to the driver specific version of this function if it is available */
649 if (serial
->type
->ioctl
) {
650 return (serial
->type
->ioctl(port
, file
, cmd
, arg
));
657 static void serial_set_termios (struct tty_struct
*tty
, struct termios
* old
)
659 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
660 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
666 dbg(__FUNCTION__
" - port %d", port
->number
);
669 dbg (__FUNCTION__
" - port not open");
673 /* pass on to the driver specific version of this function if it is available */
674 if (serial
->type
->set_termios
) {
675 serial
->type
->set_termios(port
, old
);
682 static void serial_break (struct tty_struct
*tty
, int break_state
)
684 struct usb_serial_port
*port
= (struct usb_serial_port
*) tty
->driver_data
;
685 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
691 dbg(__FUNCTION__
" - port %d", port
->number
);
694 dbg (__FUNCTION__
" - port not open");
698 /* pass on to the driver specific version of this function if it is
700 if (serial
->type
->break_ctl
) {
701 serial
->type
->break_ctl(port
, break_state
);
707 /*****************************************************************************
708 * generic devices specific driver functions
709 *****************************************************************************/
710 static int generic_open (struct usb_serial_port
*port
, struct file
*filp
)
712 struct usb_serial
*serial
= port
->serial
;
714 dbg(__FUNCTION__
" - port %d", port
->number
);
717 dbg (__FUNCTION__
" - device already open");
722 /* if we have a bulk interrupt, start reading from it */
723 if (serial
->num_bulk_in
) {
724 /*Start reading from the device*/
725 if (usb_submit_urb(port
->read_urb
))
726 dbg(__FUNCTION__
" - usb_submit_urb(read bulk) failed");
733 static void generic_close (struct usb_serial_port
*port
, struct file
* filp
)
735 struct usb_serial
*serial
= port
->serial
;
737 dbg(__FUNCTION__
" - port %d", port
->number
);
739 /* shutdown any bulk reads that might be going on */
740 if (serial
->num_bulk_out
) {
741 usb_unlink_urb (port
->write_urb
);
743 if (serial
->num_bulk_in
) {
744 usb_unlink_urb (port
->read_urb
);
751 static int generic_write (struct usb_serial_port
*port
, int from_user
, const unsigned char *buf
, int count
)
753 struct usb_serial
*serial
= port
->serial
;
755 dbg(__FUNCTION__
" - port %d", port
->number
);
758 dbg(__FUNCTION__
" - write request of 0 bytes");
762 /* only do something if we have a bulk out endpoint */
763 if (serial
->num_bulk_out
) {
764 if (port
->write_urb
->status
== -EINPROGRESS
) {
765 dbg (__FUNCTION__
" - already writing");
769 count
= (count
> port
->bulk_out_size
) ? port
->bulk_out_size
: count
;
774 printk (KERN_DEBUG __FILE__
": " __FUNCTION__
" - length = %d, data = ", count
);
775 for (i
= 0; i
< count
; ++i
) {
776 printk ("%.2x ", buf
[i
]);
783 copy_from_user(port
->write_urb
->transfer_buffer
, buf
, count
);
786 memcpy (port
->write_urb
->transfer_buffer
, buf
, count
);
789 /* send the data out the bulk port */
790 port
->write_urb
->transfer_buffer_length
= count
;
792 if (usb_submit_urb(port
->write_urb
))
793 dbg(__FUNCTION__
" - usb_submit_urb(write bulk) failed");
798 /* no bulk out, so return 0 bytes written */
803 static int generic_write_room (struct usb_serial_port
*port
)
805 struct usb_serial
*serial
= port
->serial
;
808 dbg(__FUNCTION__
" - port %d", port
->number
);
810 if (serial
->num_bulk_out
) {
811 if (port
->write_urb
->status
== -EINPROGRESS
)
814 room
= port
->bulk_out_size
;
815 dbg(__FUNCTION__
" returns %d", room
);
823 static int generic_chars_in_buffer (struct usb_serial_port
*port
)
825 struct usb_serial
*serial
= port
->serial
;
827 dbg(__FUNCTION__
" - port %d", port
->number
);
829 if (serial
->num_bulk_out
) {
830 if (port
->write_urb
->status
== -EINPROGRESS
) {
831 return (port
->bulk_out_size
);
839 static void generic_read_bulk_callback (struct urb
*urb
)
841 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
842 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
843 struct tty_struct
*tty
;
844 unsigned char *data
= urb
->transfer_buffer
;
847 dbg (__FUNCTION__
" - enter");
854 dbg(__FUNCTION__
" - nonzero read bulk status received: %d", urb
->status
);
859 if (urb
->actual_length
) {
860 printk (KERN_DEBUG __FILE__
": " __FUNCTION__
"- length = %d, data = ", urb
->actual_length
);
861 for (i
= 0; i
< urb
->actual_length
; ++i
) {
862 printk ("%.2x ", data
[i
]);
869 if (urb
->actual_length
) {
870 for (i
= 0; i
< urb
->actual_length
; ++i
) {
871 tty_insert_flip_char(tty
, data
[i
], 0);
873 tty_flip_buffer_push(tty
);
876 /* Continue trying to always read */
877 if (usb_submit_urb(urb
))
878 dbg(__FUNCTION__
" - failed resubmitting read urb");
880 dbg (__FUNCTION__
" - exit");
886 static void generic_write_bulk_callback (struct urb
*urb
)
888 struct usb_serial_port
*port
= (struct usb_serial_port
*)urb
->context
;
889 struct usb_serial
*serial
= get_usb_serial (port
, __FUNCTION__
);
890 struct tty_struct
*tty
;
892 dbg (__FUNCTION__
" - enter");
899 dbg(__FUNCTION__
" - nonzero write bulk status received: %d", urb
->status
);
904 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) && tty
->ldisc
.write_wakeup
)
905 (tty
->ldisc
.write_wakeup
)(tty
);
907 wake_up_interruptible(&tty
->write_wait
);
909 dbg (__FUNCTION__
" - exit");
915 static void * usb_serial_probe(struct usb_device
*dev
, unsigned int ifnum
)
917 struct usb_serial
*serial
= NULL
;
918 struct usb_serial_port
*port
;
919 struct usb_interface_descriptor
*interface
;
920 struct usb_endpoint_descriptor
*endpoint
;
921 struct usb_endpoint_descriptor
*interrupt_in_endpoint
[MAX_NUM_PORTS
];
922 struct usb_endpoint_descriptor
*bulk_in_endpoint
[MAX_NUM_PORTS
];
923 struct usb_endpoint_descriptor
*bulk_out_endpoint
[MAX_NUM_PORTS
];
924 struct usb_serial_device_type
*type
;
932 int num_interrupt_in
= 0;
934 int num_bulk_out
= 0;
938 /* loop through our list of known serial converters, and see if this
940 for (device_num
= 0; usb_serial_devices
[device_num
]; device_num
++) {
941 type
= usb_serial_devices
[device_num
];
942 dbg ("Looking at %s Vendor id=%.4x Product id=%.4x",
943 type
->name
, *(type
->idVendor
), *(type
->idProduct
));
945 /* look at the device descriptor */
946 if ((dev
->descriptor
.idVendor
== *(type
->idVendor
)) &&
947 (dev
->descriptor
.idProduct
== *(type
->idProduct
))) {
948 dbg("descriptor matches");
952 if (!usb_serial_devices
[device_num
]) {
958 /* descriptor matches, let's find the endpoints needed */
959 interrupt_pipe
= bulk_in_pipe
= bulk_out_pipe
= HAS_NOT
;
961 /* check out the endpoints */
962 interface
= &dev
->actconfig
->interface
[ifnum
].altsetting
[0];
963 for (i
= 0; i
< interface
->bNumEndpoints
; ++i
) {
964 endpoint
= &interface
->endpoint
[i
];
966 if ((endpoint
->bEndpointAddress
& 0x80) &&
967 ((endpoint
->bmAttributes
& 3) == 0x02)) {
968 /* we found a bulk in endpoint */
969 dbg("found bulk in");
971 bulk_in_endpoint
[num_bulk_in
] = endpoint
;
975 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
976 ((endpoint
->bmAttributes
& 3) == 0x02)) {
977 /* we found a bulk out endpoint */
978 dbg("found bulk out");
980 bulk_out_endpoint
[num_bulk_out
] = endpoint
;
984 if ((endpoint
->bEndpointAddress
& 0x80) &&
985 ((endpoint
->bmAttributes
& 3) == 0x03)) {
986 /* we found a interrupt in endpoint */
987 dbg("found interrupt in");
988 interrupt_pipe
= HAS
;
989 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
994 /* verify that we found all of the endpoints that we need */
995 if (!((interrupt_pipe
& type
->needs_interrupt_in
) &&
996 (bulk_in_pipe
& type
->needs_bulk_in
) &&
997 (bulk_out_pipe
& type
->needs_bulk_out
))) {
998 /* nope, they don't match what we expected */
999 info("descriptors matched, but endpoints did not");
1003 /* found all that we need */
1005 info("%s converter detected", type
->name
);
1007 #ifdef CONFIG_USB_SERIAL_GENERIC
1008 if (type
== &generic_device
) {
1009 num_ports
= num_bulk_out
;
1010 if (num_ports
== 0) {
1011 err("Generic device with no bulk out, not allowed.");
1017 num_ports
= type
->num_ports
;
1019 serial
= get_free_serial (num_ports
, &minor
);
1020 if (serial
== NULL
) {
1021 err("No more free serial devices");
1027 serial
->type
= type
;
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
;
1034 /* if this device type has a startup function, call it */
1035 if (type
->startup
) {
1036 if (type
->startup (serial
)) {
1041 /* set up the endpoint information */
1042 for (i
= 0; i
< num_bulk_in
; ++i
) {
1043 endpoint
= bulk_in_endpoint
[i
];
1044 port
= &serial
->port
[i
];
1045 port
->read_urb
= usb_alloc_urb (0);
1046 if (!port
->read_urb
) {
1047 err("No free urbs available");
1050 buffer_size
= endpoint
->wMaxPacketSize
;
1051 port
->bulk_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1052 if (!port
->bulk_in_buffer
) {
1053 err("Couldn't allocate bulk_in_buffer");
1056 FILL_BULK_URB(port
->read_urb
, dev
,
1057 usb_rcvbulkpipe(dev
, endpoint
->bEndpointAddress
),
1058 port
->bulk_in_buffer
, buffer_size
,
1059 ((serial
->type
->read_bulk_callback
) ?
1060 serial
->type
->read_bulk_callback
:
1061 generic_read_bulk_callback
),
1065 for (i
= 0; i
< num_bulk_out
; ++i
) {
1066 endpoint
= bulk_out_endpoint
[i
];
1067 port
= &serial
->port
[i
];
1068 port
->write_urb
= usb_alloc_urb(0);
1069 if (!port
->write_urb
) {
1070 err("No free urbs available");
1073 buffer_size
= endpoint
->wMaxPacketSize
;
1074 port
->bulk_out_size
= buffer_size
;
1075 port
->bulk_out_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1076 if (!port
->bulk_out_buffer
) {
1077 err("Couldn't allocate bulk_out_buffer");
1080 FILL_BULK_URB(port
->write_urb
, dev
,
1081 usb_sndbulkpipe(dev
, endpoint
->bEndpointAddress
),
1082 port
->bulk_out_buffer
, buffer_size
,
1083 ((serial
->type
->write_bulk_callback
) ?
1084 serial
->type
->write_bulk_callback
:
1085 generic_write_bulk_callback
),
1089 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1090 endpoint
= interrupt_in_endpoint
[i
];
1091 port
= &serial
->port
[i
];
1092 port
->interrupt_in_urb
= usb_alloc_urb(0);
1093 if (!port
->interrupt_in_urb
) {
1094 err("No free urbs available");
1097 buffer_size
= endpoint
->wMaxPacketSize
;
1098 port
->interrupt_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
1099 if (!port
->interrupt_in_buffer
) {
1100 err("Couldn't allocate interrupt_in_buffer");
1103 FILL_INT_URB(port
->interrupt_in_urb
, dev
,
1104 usb_rcvintpipe(dev
, endpoint
->bEndpointAddress
),
1105 port
->interrupt_in_buffer
, buffer_size
,
1106 serial
->type
->read_int_callback
,
1108 endpoint
->bInterval
);
1111 /* initialize some parts of the port structures */
1112 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1113 max_endpoints
= MAX(num_bulk_in
, num_bulk_out
);
1114 max_endpoints
= MAX(max_endpoints
, num_interrupt_in
);
1115 for (i
= 0; i
< max_endpoints
; ++i
) {
1116 port
= &serial
->port
[i
];
1117 port
->number
= i
+ serial
->minor
;
1118 port
->serial
= serial
;
1119 port
->magic
= USB_SERIAL_PORT_MAGIC
;
1122 /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
1123 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1124 tty_register_devfs (&serial_tty_driver
, 0, serial
->port
[i
].number
);
1125 info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)",
1126 type
->name
, serial
->port
[i
].number
, serial
->port
[i
].number
);
1129 return serial
; /* success */
1133 for (i
= 0; i
< num_bulk_in
; ++i
) {
1134 port
= &serial
->port
[i
];
1136 usb_free_urb (port
->read_urb
);
1137 if (port
->bulk_in_buffer
)
1138 kfree (port
->bulk_in_buffer
);
1140 for (i
= 0; i
< num_bulk_out
; ++i
) {
1141 port
= &serial
->port
[i
];
1142 if (port
->write_urb
)
1143 usb_free_urb (port
->write_urb
);
1144 if (port
->bulk_out_buffer
)
1145 kfree (port
->bulk_out_buffer
);
1147 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1148 port
= &serial
->port
[i
];
1149 if (port
->interrupt_in_urb
)
1150 usb_free_urb (port
->interrupt_in_urb
);
1151 if (port
->interrupt_in_buffer
)
1152 kfree (port
->interrupt_in_buffer
);
1155 /* return the minor range that this device had */
1156 return_serial (serial
);
1158 /* free up any memory that we allocated */
1165 static void usb_serial_disconnect(struct usb_device
*dev
, void *ptr
)
1167 struct usb_serial
*serial
= (struct usb_serial
*) ptr
;
1168 struct usb_serial_port
*port
;
1172 if (serial
->type
->shutdown
)
1173 serial
->type
->shutdown(serial
);
1175 for (i
= 0; i
< serial
->num_ports
; ++i
)
1176 serial
->port
[i
].active
= 0;
1178 for (i
= 0; i
< serial
->num_bulk_in
; ++i
) {
1179 port
= &serial
->port
[i
];
1180 if (port
->read_urb
) {
1181 usb_unlink_urb (port
->read_urb
);
1182 usb_free_urb (port
->read_urb
);
1184 if (port
->bulk_in_buffer
)
1185 kfree (port
->bulk_in_buffer
);
1187 for (i
= 0; i
< serial
->num_bulk_out
; ++i
) {
1188 port
= &serial
->port
[i
];
1189 if (port
->write_urb
) {
1190 usb_unlink_urb (port
->write_urb
);
1191 usb_free_urb (port
->write_urb
);
1193 if (port
->bulk_out_buffer
)
1194 kfree (port
->bulk_out_buffer
);
1196 for (i
= 0; i
< serial
->num_interrupt_in
; ++i
) {
1197 port
= &serial
->port
[i
];
1198 if (port
->interrupt_in_urb
) {
1199 usb_unlink_urb (port
->interrupt_in_urb
);
1200 usb_free_urb (port
->interrupt_in_urb
);
1202 if (port
->interrupt_in_buffer
)
1203 kfree (port
->interrupt_in_buffer
);
1206 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1207 tty_unregister_devfs (&serial_tty_driver
, serial
->port
[i
].number
);
1208 info("%s converter now disconnected from ttyUSB%d", serial
->type
->name
, serial
->port
[i
].number
);
1211 /* return the minor range that this device had */
1212 return_serial (serial
);
1214 /* free up any memory that we allocated */
1218 info("device disconnected");
1225 static struct tty_driver serial_tty_driver
= {
1226 magic
: TTY_DRIVER_MAGIC
,
1227 driver_name
: "usb-serial",
1229 major
: SERIAL_TTY_MAJOR
,
1231 num
: SERIAL_TTY_MINORS
,
1232 type
: TTY_DRIVER_TYPE_SERIAL
,
1233 subtype
: SERIAL_TYPE_NORMAL
,
1234 flags
: TTY_DRIVER_REAL_RAW
| TTY_DRIVER_NO_DEVFS
,
1236 refcount
: &serial_refcount
,
1238 termios
: serial_termios
,
1239 termios_locked
: serial_termios_locked
,
1242 close
: serial_close
,
1243 write
: serial_write
,
1244 write_room
: serial_write_room
,
1245 ioctl
: serial_ioctl
,
1246 set_termios
: serial_set_termios
,
1247 throttle
: serial_throttle
,
1248 unthrottle
: serial_unthrottle
,
1249 break_ctl
: serial_break
,
1250 chars_in_buffer
: serial_chars_in_buffer
,
1254 int usb_serial_init(void)
1260 /* Initalize our global data */
1261 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
1262 serial_table
[i
] = NULL
;
1265 /* tell the world what devices this driver currently supports */
1267 for (i
= 0; usb_serial_devices
[i
]; ++i
) {
1268 if (!strstr (usb_serial_devices
[i
]->name
, "prerenumeration")) {
1269 info ("USB Serial support registered for %s", usb_serial_devices
[i
]->name
);
1274 info ("USB Serial driver is not configured for any devices!");
1276 /* register the tty driver */
1277 serial_tty_driver
.init_termios
= tty_std_termios
;
1278 serial_tty_driver
.init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1279 if (tty_register_driver (&serial_tty_driver
)) {
1280 err(__FUNCTION__
" - failed to register tty driver");
1284 /* register the USB driver */
1285 result
= usb_register(&usb_serial_driver
);
1287 tty_unregister_driver(&serial_tty_driver
);
1288 err("usb_register failed for the usb-serial driver. Error number %d", result
);
1296 void usb_serial_exit(void)
1298 usb_deregister(&usb_serial_driver
);
1299 tty_unregister_driver(&serial_tty_driver
);
1303 module_init(usb_serial_init
);
1304 module_exit(usb_serial_exit
);