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