Merge with Linux 2.4.0-test3-pre2.
[linux-2.6/linux-mips.git] / drivers / usb / serial / usbserial.c
blob261d5ac006ad406cfa4d3a0572f3625a1d745e2e
1 /*
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
17 * (06/25/2000) gkh
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.
21 * (06/23/2000) gkh
22 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
24 * (05/22/2000) gkh
25 * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
26 * removed from the individual device source files.
28 * (05/03/2000) gkh
29 * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
31 * (05/02/2000) gkh
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.
38 * (04/23/2000) gkh
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.
42 * (04/19/2000) gkh
43 * Added driver for ZyXEL omni.net lcd plus ISDN TA
44 * Made startup info message specify which drivers were compiled in.
46 * (04/03/2000) gkh
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.
51 * (03/26/2000) gkh
52 * Split driver up into device specific pieces.
54 * (03/19/2000) gkh
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
58 * dynamically.
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.
64 * (03/17/2000) gkh
65 * Added config option for debugging messages.
66 * Added patch for keyspan pda from Brian Warner.
68 * (03/06/2000) gkh
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)
74 * (02/21/2000) gkh
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
82 * to Miles Lott).
83 * Small fix to get_free_serial.
85 * (02/14/2000) gkh
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
92 * Ryder.
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.
97 * (02/11/2000) gkh
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).
103 * (02/05/2000) gkh
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
109 * usb-serial.h file.
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.
114 * (01/25/2000) gkh
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.
120 * (01/23/2000) gkh
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.
125 * (01/21/2000) gkh
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
130 * generic device.
132 * (01/19/2000) gkh
133 * Removed lots of cruft that was around from the old (pre urb) driver
134 * interface.
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.
141 * (01/17/2000) gkh
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.
147 * (01/13/2000) gkh
148 * Fixed the vendor id for the generic driver to the one I meant it to be.
150 * (01/12/2000) gkh
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
211 * to change.
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
233 #define DEBUG
234 #else
235 #undef DEBUG
236 #endif
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 = {
270 name: "Generic",
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,
279 num_ports: 1,
281 #endif
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
289 &generic_device,
290 #endif
291 #ifdef CONFIG_USB_SERIAL_WHITEHEAT
292 &whiteheat_fake_device,
293 &whiteheat_device,
294 #endif
295 #ifdef CONFIG_USB_SERIAL_VISOR
296 &handspring_device,
297 #endif
298 #ifdef CONFIG_USB_SERIAL_FTDI_SIO
299 &ftdi_sio_device,
300 #endif
301 #ifdef CONFIG_USB_SERIAL_KEYSPAN_PDA
302 &keyspan_pda_fake_device,
303 &keyspan_pda_device,
304 #endif
305 #ifdef CONFIG_USB_SERIAL_OMNINET
306 &zyxel_omninet_device,
307 #endif
308 #ifdef CONFIG_USB_SERIAL_DIGI_ACCELEPORT
309 &digi_acceleport_device,
310 #endif
311 NULL
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 = {
330 name: "serial",
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 */
346 if (!port ||
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 */
351 return NULL;
354 return port->serial;
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;
367 int i, j;
368 int good_spot;
370 dbg(__FUNCTION__ " %d", num_ports);
372 *minor = 0;
373 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
374 if (serial_table[i])
375 continue;
377 good_spot = 1;
378 for (j = 1; j <= num_ports-1; ++j)
379 if (serial_table[i+j])
380 good_spot = 0;
381 if (good_spot == 0)
382 continue;
384 if (!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) {
385 err(__FUNCTION__ " - Out of memory");
386 return NULL;
388 memset(serial, 0, sizeof(struct usb_serial));
389 serial->magic = USB_SERIAL_MAGIC;
390 serial_table[i] = serial;
391 *minor = i;
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;
395 return serial;
397 return NULL;
401 static void return_serial (struct usb_serial *serial)
403 int i;
405 dbg(__FUNCTION__);
407 if (serial == NULL)
408 return;
410 for (i = 0; i < serial->num_ports; ++i) {
411 serial_table[serial->minor + i] = NULL;
414 return;
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)
424 int result;
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);
431 return -ENOMEM;
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);
436 return result;
440 int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
442 int response;
443 dbg(__FUNCTION__ " - %d", reset_bit);
444 response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0);
445 if (response < 0) {
446 err(__FUNCTION__ "- %d failed", reset_bit);
448 return response;
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;
461 int portNumber;
463 dbg(__FUNCTION__);
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__)) {
472 return -ENODEV;
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;
479 port->tty = tty;
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));
484 } else {
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__);
495 if (!serial) {
496 return;
499 dbg(__FUNCTION__ " - port %d", port->number);
501 if (!port->active) {
502 dbg (__FUNCTION__ " - port not opened");
503 return;
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);
509 } else {
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__);
520 if (!serial) {
521 return -ENODEV;
524 dbg(__FUNCTION__ " - port %d, %d byte(s)", port->number, count);
526 if (!port->active) {
527 dbg (__FUNCTION__ " - port not opened");
528 return -EINVAL;
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));
534 } else {
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__);
545 if (!serial) {
546 return -ENODEV;
549 dbg(__FUNCTION__ " - port %d", port->number);
551 if (!port->active) {
552 dbg (__FUNCTION__ " - port not open");
553 return -EINVAL;
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));
559 } else {
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__);
570 if (!serial) {
571 return -ENODEV;
574 if (!port->active) {
575 dbg (__FUNCTION__ " - port not open");
576 return -EINVAL;
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));
582 } else {
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__);
593 if (!serial) {
594 return;
597 dbg(__FUNCTION__ " - port %d", port->number);
599 if (!port->active) {
600 dbg (__FUNCTION__ " - port not open");
601 return;
604 /* pass on to the driver specific version of this function */
605 if (serial->type->throttle) {
606 serial->type->throttle(port);
609 return;
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__);
618 if (!serial) {
619 return;
622 dbg(__FUNCTION__ " - port %d", port->number);
624 if (!port->active) {
625 dbg (__FUNCTION__ " - port not open");
626 return;
629 /* pass on to the driver specific version of this function */
630 if (serial->type->unthrottle) {
631 serial->type->unthrottle(port);
634 return;
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__);
643 if (!serial) {
644 return -ENODEV;
647 dbg(__FUNCTION__ " - port %d", port->number);
649 if (!port->active) {
650 dbg (__FUNCTION__ " - port not open");
651 return -ENODEV;
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));
657 } else {
658 return -ENOIOCTLCMD;
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__);
668 if (!serial) {
669 return;
672 dbg(__FUNCTION__ " - port %d", port->number);
674 if (!port->active) {
675 dbg (__FUNCTION__ " - port not open");
676 return;
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);
684 return;
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__);
693 if (!serial) {
694 return;
697 dbg(__FUNCTION__ " - port %d", port->number);
699 if (!port->active) {
700 dbg (__FUNCTION__ " - port not open");
701 return;
704 /* pass on to the driver specific version of this function if it is
705 available */
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);
722 if (port->active) {
723 dbg (__FUNCTION__ " - device already open");
724 return -EINVAL;
726 port->active = 1;
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");
735 return (0);
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);
753 port->active = 0;
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);
763 if (count == 0) {
764 dbg(__FUNCTION__ " - write request of 0 bytes");
765 return (0);
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");
772 return (0);
775 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
777 #ifdef DEBUG
779 int i;
780 printk (KERN_DEBUG __FILE__ ": " __FUNCTION__ " - length = %d, data = ", count);
781 for (i = 0; i < count; ++i) {
782 printk ("%.2x ", buf[i]);
784 printk ("\n");
786 #endif
788 if (from_user) {
789 copy_from_user(port->write_urb->transfer_buffer, buf, count);
791 else {
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");
801 return (count);
804 /* no bulk out, so return 0 bytes written */
805 return (0);
809 static int generic_write_room (struct usb_serial_port *port)
811 struct usb_serial *serial = port->serial;
812 int room = 0;
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);
821 return (room);
825 static int generic_chars_in_buffer (struct usb_serial_port *port)
827 struct usb_serial *serial = port->serial;
828 int chars = 0;
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);
837 return (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;
847 int i;
849 dbg(__FUNCTION__ " - port %d", port->number);
851 if (!serial) {
852 dbg(__FUNCTION__ " - bad serial pointer, exiting");
853 return;
856 if (urb->status) {
857 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
858 return;
861 #ifdef DEBUG
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]);
867 printk ("\n");
869 #endif
871 tty = port->tty;
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");
883 return;
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);
894 if (!serial) {
895 dbg(__FUNCTION__ " - bad serial pointer, exiting");
896 return;
899 if (urb->status) {
900 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
901 return;
904 queue_task(&port->tqueue, &tq_immediate);
905 mark_bh(IMMEDIATE_BH);
907 return;
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);
919 if (!serial) {
920 return;
923 tty = port->tty;
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;
944 int device_num;
945 int minor;
946 int buffer_size;
947 int i;
948 char interrupt_pipe;
949 char bulk_in_pipe;
950 char bulk_out_pipe;
951 int num_interrupt_in = 0;
952 int num_bulk_in = 0;
953 int num_bulk_out = 0;
954 int num_ports;
955 int max_endpoints;
957 /* loop through our list of known serial converters, and see if this
958 device matches. */
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");
968 break;
971 if (!usb_serial_devices[device_num]) {
972 /* no match */
973 dbg("none matched");
974 return(NULL);
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");
989 bulk_in_pipe = HAS;
990 bulk_in_endpoint[num_bulk_in] = endpoint;
991 ++num_bulk_in;
994 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
995 ((endpoint->bmAttributes & 3) == 0x02)) {
996 /* we found a bulk out endpoint */
997 dbg("found bulk out");
998 bulk_out_pipe = HAS;
999 bulk_out_endpoint[num_bulk_out] = endpoint;
1000 ++num_bulk_out;
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;
1009 ++num_interrupt_in;
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");
1019 return NULL;
1022 /* found all that we need */
1023 MOD_INC_USE_COUNT;
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.");
1031 MOD_DEC_USE_COUNT;
1032 return NULL;
1034 } else
1035 #endif
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");
1041 MOD_DEC_USE_COUNT;
1042 return NULL;
1045 serial->dev = dev;
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)) {
1056 goto probe_error;
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");
1067 goto probe_error;
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");
1073 goto probe_error;
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),
1081 port);
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");
1090 goto probe_error;
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");
1097 goto probe_error;
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),
1105 port);
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");
1114 goto probe_error;
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");
1120 goto probe_error;
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,
1126 port,
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 */
1153 probe_error:
1154 for (i = 0; i < num_bulk_in; ++i) {
1155 port = &serial->port[i];
1156 if (port->read_urb)
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 */
1180 kfree (serial);
1181 MOD_DEC_USE_COUNT;
1182 return NULL;
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;
1190 int i;
1192 if (serial) {
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 */
1236 kfree (serial);
1238 } else {
1239 info("device disconnected");
1242 MOD_DEC_USE_COUNT;
1246 static struct tty_driver serial_tty_driver = {
1247 magic: TTY_DRIVER_MAGIC,
1248 driver_name: "usb-serial",
1249 name: "usb/tts/%d",
1250 major: SERIAL_TTY_MAJOR,
1251 minor_start: 0,
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,
1258 table: serial_tty,
1259 termios: serial_termios,
1260 termios_locked: serial_termios_locked,
1262 open: serial_open,
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)
1277 int i;
1278 int something;
1279 int result;
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 */
1287 something = 0;
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);
1291 something = 1;
1294 if (!something)
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");
1302 return -1;
1305 /* register the USB driver */
1306 result = usb_register(&usb_serial_driver);
1307 if (result < 0) {
1308 tty_unregister_driver(&serial_tty_driver);
1309 err("usb_register failed for the usb-serial driver. Error number %d", result);
1310 return -1;
1313 return 0;
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);