- Andries Brouwer: final isofs pieces.
[davej-history.git] / drivers / usb / serial / usbserial.c
blob1a07a03d5f6ee0e0746e842e6235da7567ee06e3
1 /*
2 * USB Serial Converter driver
4 * Copyright (C) 1999, 2000 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (c) 2000 Peter Berger (pberger@brimson.com)
6 * Copyright (c) 2000 Al Borchers (borchers@steinerpoint.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver was originally based on the ACM driver by Armin Fuerst (which was
14 * based on a driver by Brad Keryan)
16 * See Documentation/usb/usb-serial.txt for more information on using this driver
18 * (12/29/2000) gkh
19 * Small NULL pointer initialization cleanup which saves a bit of disk image
21 * (11/01/2000) Adam J. Richter
22 * instead of using idVendor/idProduct pairs, usb serial drivers
23 * now identify their hardware interest with usb_device_id tables,
24 * which they usually have anyhow for use with MODULE_DEVICE_TABLE.
26 * (10/05/2000) gkh
27 * Fixed bug with urb->dev not being set properly, now that the usb
28 * core needs it.
30 * (09/11/2000) gkh
31 * Removed DEBUG #ifdefs with call to usb_serial_debug_data
33 * (08/28/2000) gkh
34 * Added port_lock to port structure.
35 * Added locks for SMP safeness to generic driver
36 * Fixed the ability to open a generic device's port more than once.
38 * (07/23/2000) gkh
39 * Added bulk_out_endpointAddress to port structure.
41 * (07/19/2000) gkh, pberger, and borchers
42 * Modifications to allow usb-serial drivers to be modules.
44 * (07/03/2000) gkh
45 * Added more debugging to serial_ioctl call
47 * (06/25/2000) gkh
48 * Changed generic_write_bulk_callback to not call wake_up_interruptible
49 * directly, but to have port_softint do it at a safer time.
51 * (06/23/2000) gkh
52 * Cleaned up debugging statements in a quest to find UHCI timeout bug.
54 * (05/22/2000) gkh
55 * Changed the makefile, enabling the big CONFIG_USB_SERIAL_SOMTHING to be
56 * removed from the individual device source files.
58 * (05/03/2000) gkh
59 * Added the Digi Acceleport driver from Al Borchers and Peter Berger.
61 * (05/02/2000) gkh
62 * Changed devfs and tty register code to work properly now. This was based on
63 * the ACM driver changes by Vojtech Pavlik.
65 * (04/27/2000) Ryan VanderBijl
66 * Put calls to *_paranoia_checks into one function.
68 * (04/23/2000) gkh
69 * Fixed bug that Randy Dunlap found for Generic devices with no bulk out ports.
70 * Moved when the startup code printed out the devices that are supported.
72 * (04/19/2000) gkh
73 * Added driver for ZyXEL omni.net lcd plus ISDN TA
74 * Made startup info message specify which drivers were compiled in.
76 * (04/03/2000) gkh
77 * Changed the probe process to remove the module unload races.
78 * Changed where the tty layer gets initialized to have devfs work nicer.
79 * Added initial devfs support.
81 * (03/26/2000) gkh
82 * Split driver up into device specific pieces.
84 * (03/19/2000) gkh
85 * Fixed oops that could happen when device was removed while a program
86 * was talking to the device.
87 * Removed the static urbs and now all urbs are created and destroyed
88 * dynamically.
89 * Reworked the internal interface. Now everything is based on the
90 * usb_serial_port structure instead of the larger usb_serial structure.
91 * This fixes the bug that a multiport device could not have more than
92 * one port open at one time.
94 * (03/17/2000) gkh
95 * Added config option for debugging messages.
96 * Added patch for keyspan pda from Brian Warner.
98 * (03/06/2000) gkh
99 * Added the keyspan pda code from Brian Warner <warner@lothar.com>
100 * Moved a bunch of the port specific stuff into its own structure. This
101 * is in anticipation of the true multiport devices (there's a bug if you
102 * try to access more than one port of any multiport device right now)
104 * (02/21/2000) gkh
105 * Made it so that any serial devices only have to specify which functions
106 * they want to overload from the generic function calls (great,
107 * inheritance in C, in a driver, just what I wanted...)
108 * Added support for set_termios and ioctl function calls. No drivers take
109 * advantage of this yet.
110 * Removed the #ifdef MODULE, now there is no module specific code.
111 * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
112 * to Miles Lott).
113 * Small fix to get_free_serial.
115 * (02/14/2000) gkh
116 * Removed the Belkin and Peracom functionality from the driver due to
117 * the lack of support from the vendor, and me not wanting people to
118 * accidenatly buy the device, expecting it to work with Linux.
119 * Added read_bulk_callback and write_bulk_callback to the type structure
120 * for the needs of the FTDI and WhiteHEAT driver.
121 * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
122 * Ryder.
123 * Changed the output urb size back to the max endpoint size to make
124 * the ftdi_sio driver have it easier, and due to the fact that it didn't
125 * really increase the speed any.
127 * (02/11/2000) gkh
128 * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
129 * patch from Miles Lott (milos@insync.net).
130 * Fixed bug with not restoring the minor range that a device grabs, if
131 * the startup function fails (thanks Miles for finding this).
133 * (02/05/2000) gkh
134 * Added initial framework for the Keyspan PDA serial converter so that
135 * Brian Warner has a place to put his code.
136 * Made the ezusb specific functions generic enough that different
137 * devices can use them (whiteheat and keyspan_pda both need them).
138 * Split out a whole bunch of structure and other stuff to a seperate
139 * usb-serial.h file.
140 * Made the Visor connection messages a little more understandable, now
141 * that Miles Lott (milos@insync.net) has gotten the Generic channel to
142 * work. Also made them always show up in the log file.
144 * (01/25/2000) gkh
145 * Added initial framework for FTDI serial converter so that Bill Ryder
146 * has a place to put his code.
147 * Added the vendor specific info from Handspring. Now we can print out
148 * informational debug messages as well as understand what is happening.
150 * (01/23/2000) gkh
151 * Fixed problem of crash when trying to open a port that didn't have a
152 * device assigned to it. Made the minor node finding a little smarter,
153 * now it looks to find a continous space for the new device.
155 * (01/21/2000) gkh
156 * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
157 * Fixed get_serial_by_minor which was all messed up for multi port
158 * devices. Fixed multi port problem for generic devices. Now the number
159 * of ports is determined by the number of bulk out endpoints for the
160 * generic device.
162 * (01/19/2000) gkh
163 * Removed lots of cruft that was around from the old (pre urb) driver
164 * interface.
165 * Made the serial_table dynamic. This should save lots of memory when
166 * the number of minor nodes goes up to 256.
167 * Added initial support for devices that have more than one port.
168 * Added more debugging comments for the Visor, and added a needed
169 * set_configuration call.
171 * (01/17/2000) gkh
172 * Fixed the WhiteHEAT firmware (my processing tool had a bug)
173 * and added new debug loader firmware for it.
174 * Removed the put_char function as it isn't really needed.
175 * Added visor startup commands as found by the Win98 dump.
177 * (01/13/2000) gkh
178 * Fixed the vendor id for the generic driver to the one I meant it to be.
180 * (01/12/2000) gkh
181 * Forget the version numbering...that's pretty useless...
182 * Made the driver able to be compiled so that the user can select which
183 * converter they want to use. This allows people who only want the Visor
184 * support to not pay the memory size price of the WhiteHEAT.
185 * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
186 * grabbed the root hub. Not good.
188 * version 0.4.0 (01/10/2000) gkh
189 * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
190 * device. Added startup function to allow firmware to be downloaded to
191 * a device if it needs to be.
192 * Added firmware download logic to the WhiteHEAT device.
193 * Started to add #defines to split up the different drivers for potential
194 * configuration option.
196 * version 0.3.1 (12/30/99) gkh
197 * Fixed problems with urb for bulk out.
198 * Added initial support for multiple sets of endpoints. This enables
199 * the Handspring Visor to be attached successfully. Only the first
200 * bulk in / bulk out endpoint pair is being used right now.
202 * version 0.3.0 (12/27/99) gkh
203 * Added initial support for the Handspring Visor based on a patch from
204 * Miles Lott (milos@sneety.insync.net)
205 * Cleaned up the code a bunch and converted over to using urbs only.
207 * version 0.2.3 (12/21/99) gkh
208 * Added initial support for the Connect Tech WhiteHEAT converter.
209 * Incremented the number of ports in expectation of getting the
210 * WhiteHEAT to work properly (4 ports per connection).
211 * Added notification on insertion and removal of what port the
212 * device is/was connected to (and what kind of device it was).
214 * version 0.2.2 (12/16/99) gkh
215 * Changed major number to the new allocated number. We're legal now!
217 * version 0.2.1 (12/14/99) gkh
218 * Fixed bug that happens when device node is opened when there isn't a
219 * device attached to it. Thanks to marek@webdesign.no for noticing this.
221 * version 0.2.0 (11/10/99) gkh
222 * Split up internals to make it easier to add different types of serial
223 * converters to the code.
224 * Added a "generic" driver that gets it's vendor and product id
225 * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
226 * for the idea and sample code (from the usb scanner driver.)
227 * Cleared up any licensing questions by releasing it under the GNU GPL.
229 * version 0.1.2 (10/25/99) gkh
230 * Fixed bug in detecting device.
232 * version 0.1.1 (10/05/99) gkh
233 * Changed the major number to not conflict with anything else.
235 * version 0.1 (09/28/99) gkh
236 * Can recognize the two different devices and start up a read from
237 * device when asked to. Writes also work. No control signals yet, this
238 * all is vendor specific data (i.e. no spec), also no control for
239 * different baud rates or other bit settings.
240 * Currently we are using the same devid as the acm driver. This needs
241 * to change.
245 #include <linux/config.h>
246 #include <linux/kernel.h>
247 #include <linux/sched.h>
248 #include <linux/signal.h>
249 #include <linux/errno.h>
250 #include <linux/poll.h>
251 #include <linux/init.h>
252 #include <linux/malloc.h>
253 #include <linux/fcntl.h>
254 #include <linux/tty_driver.h>
255 #include <linux/tty_flip.h>
256 #include <linux/tty.h>
257 #include <linux/module.h>
258 #include <linux/spinlock.h>
259 #include <linux/list.h>
260 #include <linux/smp_lock.h>
262 #ifdef CONFIG_USB_SERIAL_DEBUG
263 #define DEBUG
264 #else
265 #undef DEBUG
266 #endif
267 #include <linux/usb.h>
269 /* Module information */
270 MODULE_AUTHOR("Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/");
271 MODULE_DESCRIPTION("USB Serial Driver");
273 #include "usb-serial.h"
275 #define MAX(a,b) (((a)>(b))?(a):(b))
277 /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
278 /* need to always compile these in, as some of the other devices use these functions as their own. */
279 /* if a driver does not provide a function pointer, the generic function will be called. */
280 static int generic_open (struct usb_serial_port *port, struct file *filp);
281 static void generic_close (struct usb_serial_port *port, struct file *filp);
282 static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
283 static int generic_write_room (struct usb_serial_port *port);
284 static int generic_chars_in_buffer (struct usb_serial_port *port);
285 static void generic_read_bulk_callback (struct urb *urb);
286 static void generic_write_bulk_callback (struct urb *urb);
287 static void generic_shutdown (struct usb_serial *serial);
290 #ifdef CONFIG_USB_SERIAL_GENERIC
291 static __u16 vendor = 0x05f9;
292 static __u16 product = 0xffff;
293 MODULE_PARM(vendor, "i");
294 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
296 MODULE_PARM(product, "i");
297 MODULE_PARM_DESC(product, "User specified USB idProduct");
299 static struct usb_device_id generic_device_ids[2]; /* Initially all zeroes. */
301 /* All of the device info needed for the Generic Serial Converter */
302 static struct usb_serial_device_type generic_device = {
303 name: "Generic",
304 id_table: generic_device_ids,
305 needs_interrupt_in: DONT_CARE, /* don't have to have an interrupt in endpoint */
306 needs_bulk_in: DONT_CARE, /* don't have to have a bulk in endpoint */
307 needs_bulk_out: DONT_CARE, /* don't have to have a bulk out endpoint */
308 num_interrupt_in: NUM_DONT_CARE,
309 num_bulk_in: NUM_DONT_CARE,
310 num_bulk_out: NUM_DONT_CARE,
311 num_ports: 1,
312 shutdown: generic_shutdown,
314 #endif
317 /* local function prototypes */
318 static int serial_open (struct tty_struct *tty, struct file * filp);
319 static void serial_close (struct tty_struct *tty, struct file * filp);
320 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
321 static int serial_write_room (struct tty_struct *tty);
322 static int serial_chars_in_buffer (struct tty_struct *tty);
323 static void serial_throttle (struct tty_struct * tty);
324 static void serial_unthrottle (struct tty_struct * tty);
325 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg);
326 static void serial_set_termios (struct tty_struct *tty, struct termios * old);
328 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
329 const struct usb_device_id *id);
330 static void usb_serial_disconnect(struct usb_device *dev, void *ptr);
332 static struct usb_driver usb_serial_driver = {
333 name: "serial",
334 probe: usb_serial_probe,
335 disconnect: usb_serial_disconnect,
336 id_table: NULL, /* check all devices */
339 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
340 the MODULE_DEVICE_TABLE declarations in each serial driver
341 cause the "hotplug" program to pull in whatever module is necessary
342 via modprobe, and modprobe will load usbserial because the serial
343 drivers depend on it.
347 static int serial_refcount;
348 static struct tty_driver serial_tty_driver;
349 static struct tty_struct * serial_tty[SERIAL_TTY_MINORS];
350 static struct termios * serial_termios[SERIAL_TTY_MINORS];
351 static struct termios * serial_termios_locked[SERIAL_TTY_MINORS];
352 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
354 LIST_HEAD(usb_serial_driver_list);
357 static struct usb_serial *get_serial_by_minor (int minor)
359 return serial_table[minor];
363 static struct usb_serial *get_free_serial (int num_ports, int *minor)
365 struct usb_serial *serial = NULL;
366 int i, j;
367 int good_spot;
369 dbg(__FUNCTION__ " %d", num_ports);
371 *minor = 0;
372 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
373 if (serial_table[i])
374 continue;
376 good_spot = 1;
377 for (j = 1; j <= num_ports-1; ++j)
378 if (serial_table[i+j])
379 good_spot = 0;
380 if (good_spot == 0)
381 continue;
383 if (!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) {
384 err(__FUNCTION__ " - Out of memory");
385 return NULL;
387 memset(serial, 0, sizeof(struct usb_serial));
388 serial->magic = USB_SERIAL_MAGIC;
389 serial_table[i] = serial;
390 *minor = i;
391 dbg(__FUNCTION__ " - minor base = %d", *minor);
392 for (i = *minor+1; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
393 serial_table[i] = serial;
394 return serial;
396 return NULL;
400 static void return_serial (struct usb_serial *serial)
402 int i;
404 dbg(__FUNCTION__);
406 if (serial == NULL)
407 return;
409 for (i = 0; i < serial->num_ports; ++i) {
410 serial_table[serial->minor + i] = NULL;
413 return;
417 #ifdef USES_EZUSB_FUNCTIONS
418 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
419 #define CPUCS_REG 0x7F92
421 int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest)
423 int result;
424 unsigned char *transfer_buffer = kmalloc (length, GFP_KERNEL);
426 // dbg("ezusb_writememory %x, %d", address, length);
428 if (!transfer_buffer) {
429 err(__FUNCTION__ " - kmalloc(%d) failed.", length);
430 return -ENOMEM;
432 memcpy (transfer_buffer, data, length);
433 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), bRequest, 0x40, address, 0, transfer_buffer, length, 300);
434 kfree (transfer_buffer);
435 return result;
439 int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
441 int response;
442 dbg(__FUNCTION__ " - %d", reset_bit);
443 response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0);
444 if (response < 0) {
445 err(__FUNCTION__ "- %d failed", reset_bit);
447 return response;
450 #endif /* USES_EZUSB_FUNCTIONS */
453 /*****************************************************************************
454 * Driver tty interface functions
455 *****************************************************************************/
456 static int serial_open (struct tty_struct *tty, struct file * filp)
458 struct usb_serial *serial;
459 struct usb_serial_port *port;
460 int portNumber;
462 dbg(__FUNCTION__);
464 /* initialize the pointer incase something fails */
465 tty->driver_data = NULL;
467 /* get the serial object associated with this tty pointer */
468 serial = get_serial_by_minor (MINOR(tty->device));
470 if (serial_paranoia_check (serial, __FUNCTION__)) {
471 return -ENODEV;
474 /* set up our port structure making the tty driver remember our port object, and us it */
475 portNumber = MINOR(tty->device) - serial->minor;
476 port = &serial->port[portNumber];
477 tty->driver_data = port;
478 port->tty = tty;
480 /* pass on to the driver specific version of this function if it is available */
481 if (serial->type->open) {
482 return (serial->type->open(port, filp));
483 } else {
484 return (generic_open(port, filp));
489 static void serial_close(struct tty_struct *tty, struct file * filp)
491 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
492 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
494 if (!serial) {
495 return;
498 dbg(__FUNCTION__ " - port %d", port->number);
500 if (!port->active) {
501 dbg (__FUNCTION__ " - port not opened");
502 return;
505 /* pass on to the driver specific version of this function if it is available */
506 if (serial->type->close) {
507 serial->type->close(port, filp);
508 } else {
509 generic_close(port, filp);
514 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
516 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
517 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
519 if (!serial) {
520 return -ENODEV;
523 dbg(__FUNCTION__ " - port %d, %d byte(s)", port->number, count);
525 if (!port->active) {
526 dbg (__FUNCTION__ " - port not opened");
527 return -EINVAL;
530 /* pass on to the driver specific version of this function if it is available */
531 if (serial->type->write) {
532 return (serial->type->write(port, from_user, buf, count));
533 } else {
534 return (generic_write(port, from_user, buf, count));
539 static int serial_write_room (struct tty_struct *tty)
541 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
542 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
544 if (!serial) {
545 return -ENODEV;
548 dbg(__FUNCTION__ " - port %d", port->number);
550 if (!port->active) {
551 dbg (__FUNCTION__ " - port not open");
552 return -EINVAL;
555 /* pass on to the driver specific version of this function if it is available */
556 if (serial->type->write_room) {
557 return (serial->type->write_room(port));
558 } else {
559 return (generic_write_room(port));
564 static int serial_chars_in_buffer (struct tty_struct *tty)
566 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
567 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
569 if (!serial) {
570 return -ENODEV;
573 if (!port->active) {
574 dbg (__FUNCTION__ " - port not open");
575 return -EINVAL;
578 /* pass on to the driver specific version of this function if it is available */
579 if (serial->type->chars_in_buffer) {
580 return (serial->type->chars_in_buffer(port));
581 } else {
582 return (generic_chars_in_buffer(port));
587 static void serial_throttle (struct tty_struct * tty)
589 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
590 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
592 if (!serial) {
593 return;
596 dbg(__FUNCTION__ " - port %d", port->number);
598 if (!port->active) {
599 dbg (__FUNCTION__ " - port not open");
600 return;
603 /* pass on to the driver specific version of this function */
604 if (serial->type->throttle) {
605 serial->type->throttle(port);
608 return;
612 static void serial_unthrottle (struct tty_struct * tty)
614 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
615 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
617 if (!serial) {
618 return;
621 dbg(__FUNCTION__ " - port %d", port->number);
623 if (!port->active) {
624 dbg (__FUNCTION__ " - port not open");
625 return;
628 /* pass on to the driver specific version of this function */
629 if (serial->type->unthrottle) {
630 serial->type->unthrottle(port);
633 return;
637 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
639 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
640 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
642 if (!serial) {
643 return -ENODEV;
646 dbg(__FUNCTION__ " - port %d, cmd 0x%.4x", port->number, cmd);
648 if (!port->active) {
649 dbg (__FUNCTION__ " - port not open");
650 return -ENODEV;
653 /* pass on to the driver specific version of this function if it is available */
654 if (serial->type->ioctl) {
655 return (serial->type->ioctl(port, file, cmd, arg));
656 } else {
657 return -ENOIOCTLCMD;
662 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
664 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
665 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
667 if (!serial) {
668 return;
671 dbg(__FUNCTION__ " - port %d", port->number);
673 if (!port->active) {
674 dbg (__FUNCTION__ " - port not open");
675 return;
678 /* pass on to the driver specific version of this function if it is available */
679 if (serial->type->set_termios) {
680 serial->type->set_termios(port, old);
683 return;
687 static void serial_break (struct tty_struct *tty, int break_state)
689 struct usb_serial_port *port = (struct usb_serial_port *) tty->driver_data;
690 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
692 if (!serial) {
693 return;
696 dbg(__FUNCTION__ " - port %d", port->number);
698 if (!port->active) {
699 dbg (__FUNCTION__ " - port not open");
700 return;
703 /* pass on to the driver specific version of this function if it is
704 available */
705 if (serial->type->break_ctl) {
706 serial->type->break_ctl(port, break_state);
712 /*****************************************************************************
713 * generic devices specific driver functions
714 *****************************************************************************/
715 static int generic_open (struct usb_serial_port *port, struct file *filp)
717 struct usb_serial *serial = port->serial;
718 unsigned long flags;
719 int result;
721 if (port_paranoia_check (port, __FUNCTION__))
722 return -ENODEV;
724 dbg(__FUNCTION__ " - port %d", port->number);
726 spin_lock_irqsave (&port->port_lock, flags);
728 ++port->open_count;
729 MOD_INC_USE_COUNT;
731 if (!port->active) {
732 port->active = 1;
734 /* if we have a bulk interrupt, start reading from it */
735 if (serial->num_bulk_in) {
736 /* Start reading from the device */
737 FILL_BULK_URB(port->read_urb, serial->dev,
738 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
739 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
740 ((serial->type->read_bulk_callback) ?
741 serial->type->read_bulk_callback :
742 generic_read_bulk_callback),
743 port);
744 result = usb_submit_urb(port->read_urb);
745 if (result)
746 err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
750 spin_unlock_irqrestore (&port->port_lock, flags);
752 return 0;
756 static void generic_close (struct usb_serial_port *port, struct file * filp)
758 struct usb_serial *serial = port->serial;
759 unsigned long flags;
761 dbg(__FUNCTION__ " - port %d", port->number);
763 spin_lock_irqsave (&port->port_lock, flags);
765 --port->open_count;
767 if (port->open_count <= 0) {
768 /* shutdown any bulk reads that might be going on */
769 if (serial->num_bulk_out)
770 usb_unlink_urb (port->write_urb);
771 if (serial->num_bulk_in)
772 usb_unlink_urb (port->read_urb);
774 port->active = 0;
775 port->open_count = 0;
778 spin_unlock_irqrestore (&port->port_lock, flags);
782 static int generic_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
784 struct usb_serial *serial = port->serial;
785 unsigned long flags;
786 int result;
788 dbg(__FUNCTION__ " - port %d", port->number);
790 if (count == 0) {
791 dbg(__FUNCTION__ " - write request of 0 bytes");
792 return (0);
795 /* only do something if we have a bulk out endpoint */
796 if (serial->num_bulk_out) {
797 if (port->write_urb->status == -EINPROGRESS) {
798 dbg (__FUNCTION__ " - already writing");
799 return (0);
802 spin_lock_irqsave (&port->port_lock, flags);
803 count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
805 usb_serial_debug_data (__FILE__, __FUNCTION__, count, buf);
807 if (from_user) {
808 copy_from_user(port->write_urb->transfer_buffer, buf, count);
810 else {
811 memcpy (port->write_urb->transfer_buffer, buf, count);
814 /* set up our urb */
815 FILL_BULK_URB(port->write_urb, serial->dev,
816 usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
817 port->write_urb->transfer_buffer, count,
818 ((serial->type->write_bulk_callback) ?
819 serial->type->write_bulk_callback :
820 generic_write_bulk_callback),
821 port);
823 /* send the data out the bulk port */
824 result = usb_submit_urb(port->write_urb);
825 if (result) {
826 err(__FUNCTION__ " - failed submitting write urb, error %d", result);
827 spin_unlock_irqrestore (&port->port_lock, flags);
828 return 0;
831 spin_unlock_irqrestore (&port->port_lock, flags);
832 return (count);
835 /* no bulk out, so return 0 bytes written */
836 return (0);
840 static int generic_write_room (struct usb_serial_port *port)
842 struct usb_serial *serial = port->serial;
843 int room = 0;
845 dbg(__FUNCTION__ " - port %d", port->number);
847 if (serial->num_bulk_out)
848 if (port->write_urb->status != -EINPROGRESS)
849 room = port->bulk_out_size;
851 dbg(__FUNCTION__ " - returns %d", room);
852 return (room);
856 static int generic_chars_in_buffer (struct usb_serial_port *port)
858 struct usb_serial *serial = port->serial;
859 int chars = 0;
861 dbg(__FUNCTION__ " - port %d", port->number);
863 if (serial->num_bulk_out)
864 if (port->write_urb->status == -EINPROGRESS)
865 chars = port->write_urb->transfer_buffer_length;
867 dbg (__FUNCTION__ " - returns %d", chars);
868 return (chars);
872 static void generic_read_bulk_callback (struct urb *urb)
874 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
875 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
876 struct tty_struct *tty;
877 unsigned char *data = urb->transfer_buffer;
878 int i;
879 int result;
881 dbg(__FUNCTION__ " - port %d", port->number);
883 if (!serial) {
884 dbg(__FUNCTION__ " - bad serial pointer, exiting");
885 return;
888 if (urb->status) {
889 dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb->status);
890 return;
893 usb_serial_debug_data (__FILE__, __FUNCTION__, urb->actual_length, data);
895 tty = port->tty;
896 if (urb->actual_length) {
897 for (i = 0; i < urb->actual_length ; ++i) {
898 tty_insert_flip_char(tty, data[i], 0);
900 tty_flip_buffer_push(tty);
903 /* Continue trying to always read */
904 FILL_BULK_URB(port->read_urb, serial->dev,
905 usb_rcvbulkpipe(serial->dev, port->bulk_in_endpointAddress),
906 port->read_urb->transfer_buffer, port->read_urb->transfer_buffer_length,
907 ((serial->type->read_bulk_callback) ?
908 serial->type->read_bulk_callback :
909 generic_read_bulk_callback),
910 port);
911 result = usb_submit_urb(port->read_urb);
912 if (result)
913 err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
917 static void generic_write_bulk_callback (struct urb *urb)
919 struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
920 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
922 dbg(__FUNCTION__ " - port %d", port->number);
924 if (!serial) {
925 dbg(__FUNCTION__ " - bad serial pointer, exiting");
926 return;
929 if (urb->status) {
930 dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb->status);
931 return;
934 queue_task(&port->tqueue, &tq_immediate);
935 mark_bh(IMMEDIATE_BH);
937 return;
941 static void generic_shutdown (struct usb_serial *serial)
943 int i;
945 dbg (__FUNCTION__);
947 /* stop reads and writes on all ports */
948 for (i=0; i < serial->num_ports; ++i) {
949 while (serial->port[i].open_count > 0) {
950 generic_close (&serial->port[i], NULL);
956 static void port_softint(void *private)
958 struct usb_serial_port *port = (struct usb_serial_port *)private;
959 struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
960 struct tty_struct *tty;
962 dbg(__FUNCTION__ " - port %d", port->number);
964 if (!serial) {
965 return;
968 tty = port->tty;
969 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) {
970 dbg(__FUNCTION__ " - write wakeup call.");
971 (tty->ldisc.write_wakeup)(tty);
974 wake_up_interruptible(&tty->write_wait);
979 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum,
980 const struct usb_device_id *id)
982 struct usb_serial *serial = NULL;
983 struct usb_serial_port *port;
984 struct usb_interface *interface;
985 struct usb_interface_descriptor *iface_desc;
986 struct usb_endpoint_descriptor *endpoint;
987 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
988 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
989 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
990 struct usb_serial_device_type *type = NULL;
991 struct list_head *tmp;
992 int found;
993 int minor;
994 int buffer_size;
995 int i;
996 char interrupt_pipe;
997 char bulk_in_pipe;
998 char bulk_out_pipe;
999 int num_interrupt_in = 0;
1000 int num_bulk_in = 0;
1001 int num_bulk_out = 0;
1002 int num_ports;
1003 int max_endpoints;
1004 const struct usb_device_id *id_pattern = NULL;
1007 /* loop through our list of known serial converters, and see if this
1008 device matches. */
1009 found = 0;
1010 interface = &dev->actconfig->interface[ifnum];
1011 list_for_each (tmp, &usb_serial_driver_list) {
1012 type = list_entry(tmp, struct usb_serial_device_type, driver_list);
1013 id_pattern = usb_match_id(dev, interface, type->id_table);
1014 if (id_pattern != NULL) {
1015 dbg("descriptor matches");
1016 found = 1;
1017 break;
1020 if (!found) {
1021 /* no match */
1022 dbg("none matched");
1023 return(NULL);
1026 /* descriptor matches, let's find the endpoints needed */
1027 interrupt_pipe = bulk_in_pipe = bulk_out_pipe = HAS_NOT;
1029 /* check out the endpoints */
1030 iface_desc = &interface->altsetting[0];
1031 for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1032 endpoint = &iface_desc->endpoint[i];
1034 if ((endpoint->bEndpointAddress & 0x80) &&
1035 ((endpoint->bmAttributes & 3) == 0x02)) {
1036 /* we found a bulk in endpoint */
1037 dbg("found bulk in");
1038 bulk_in_pipe = HAS;
1039 bulk_in_endpoint[num_bulk_in] = endpoint;
1040 ++num_bulk_in;
1043 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1044 ((endpoint->bmAttributes & 3) == 0x02)) {
1045 /* we found a bulk out endpoint */
1046 dbg("found bulk out");
1047 bulk_out_pipe = HAS;
1048 bulk_out_endpoint[num_bulk_out] = endpoint;
1049 ++num_bulk_out;
1052 if ((endpoint->bEndpointAddress & 0x80) &&
1053 ((endpoint->bmAttributes & 3) == 0x03)) {
1054 /* we found a interrupt in endpoint */
1055 dbg("found interrupt in");
1056 interrupt_pipe = HAS;
1057 interrupt_in_endpoint[num_interrupt_in] = endpoint;
1058 ++num_interrupt_in;
1062 /* verify that we found all of the endpoints that we need */
1063 if (!((interrupt_pipe & type->needs_interrupt_in) &&
1064 (bulk_in_pipe & type->needs_bulk_in) &&
1065 (bulk_out_pipe & type->needs_bulk_out))) {
1066 /* nope, they don't match what we expected */
1067 info("descriptors matched, but endpoints did not");
1068 return NULL;
1071 /* found all that we need */
1072 MOD_INC_USE_COUNT;
1073 info("%s converter detected", type->name);
1075 #ifdef CONFIG_USB_SERIAL_GENERIC
1076 if (type == &generic_device) {
1077 num_ports = num_bulk_out;
1078 if (num_ports == 0) {
1079 err("Generic device with no bulk out, not allowed.");
1080 MOD_DEC_USE_COUNT;
1081 return NULL;
1083 } else
1084 #endif
1085 num_ports = type->num_ports;
1087 serial = get_free_serial (num_ports, &minor);
1088 if (serial == NULL) {
1089 err("No more free serial devices");
1090 MOD_DEC_USE_COUNT;
1091 return NULL;
1094 serial->dev = dev;
1095 serial->type = type;
1096 serial->interface = interface;
1097 serial->minor = minor;
1098 serial->num_ports = num_ports;
1099 serial->num_bulk_in = num_bulk_in;
1100 serial->num_bulk_out = num_bulk_out;
1101 serial->num_interrupt_in = num_interrupt_in;
1103 /* if this device type has a startup function, call it */
1104 if (type->startup) {
1105 if (type->startup (serial)) {
1106 goto probe_error;
1110 /* set up the endpoint information */
1111 for (i = 0; i < num_bulk_in; ++i) {
1112 endpoint = bulk_in_endpoint[i];
1113 port = &serial->port[i];
1114 port->read_urb = usb_alloc_urb (0);
1115 if (!port->read_urb) {
1116 err("No free urbs available");
1117 goto probe_error;
1119 buffer_size = endpoint->wMaxPacketSize;
1120 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
1121 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1122 if (!port->bulk_in_buffer) {
1123 err("Couldn't allocate bulk_in_buffer");
1124 goto probe_error;
1126 FILL_BULK_URB(port->read_urb, dev,
1127 usb_rcvbulkpipe(dev, endpoint->bEndpointAddress),
1128 port->bulk_in_buffer, buffer_size,
1129 ((serial->type->read_bulk_callback) ?
1130 serial->type->read_bulk_callback :
1131 generic_read_bulk_callback),
1132 port);
1135 for (i = 0; i < num_bulk_out; ++i) {
1136 endpoint = bulk_out_endpoint[i];
1137 port = &serial->port[i];
1138 port->write_urb = usb_alloc_urb(0);
1139 if (!port->write_urb) {
1140 err("No free urbs available");
1141 goto probe_error;
1143 buffer_size = endpoint->wMaxPacketSize;
1144 port->bulk_out_size = buffer_size;
1145 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
1146 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
1147 if (!port->bulk_out_buffer) {
1148 err("Couldn't allocate bulk_out_buffer");
1149 goto probe_error;
1151 FILL_BULK_URB(port->write_urb, dev,
1152 usb_sndbulkpipe(dev, endpoint->bEndpointAddress),
1153 port->bulk_out_buffer, buffer_size,
1154 ((serial->type->write_bulk_callback) ?
1155 serial->type->write_bulk_callback :
1156 generic_write_bulk_callback),
1157 port);
1160 for (i = 0; i < num_interrupt_in; ++i) {
1161 endpoint = interrupt_in_endpoint[i];
1162 port = &serial->port[i];
1163 port->interrupt_in_urb = usb_alloc_urb(0);
1164 if (!port->interrupt_in_urb) {
1165 err("No free urbs available");
1166 goto probe_error;
1168 buffer_size = endpoint->wMaxPacketSize;
1169 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1170 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1171 if (!port->interrupt_in_buffer) {
1172 err("Couldn't allocate interrupt_in_buffer");
1173 goto probe_error;
1175 FILL_INT_URB(port->interrupt_in_urb, dev,
1176 usb_rcvintpipe(dev, endpoint->bEndpointAddress),
1177 port->interrupt_in_buffer, buffer_size,
1178 serial->type->read_int_callback,
1179 port,
1180 endpoint->bInterval);
1183 /* initialize some parts of the port structures */
1184 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
1185 max_endpoints = MAX(num_bulk_in, num_bulk_out);
1186 max_endpoints = MAX(max_endpoints, num_interrupt_in);
1187 dbg (__FUNCTION__ " - setting up %d port structures for this device", max_endpoints);
1188 for (i = 0; i < max_endpoints; ++i) {
1189 port = &serial->port[i];
1190 port->number = i + serial->minor;
1191 port->serial = serial;
1192 port->magic = USB_SERIAL_PORT_MAGIC;
1193 port->tqueue.routine = port_softint;
1194 port->tqueue.data = port;
1195 spin_lock_init (&port->port_lock);
1198 /* initialize the devfs nodes for this device and let the user know what ports we are bound to */
1199 for (i = 0; i < serial->num_ports; ++i) {
1200 tty_register_devfs (&serial_tty_driver, 0, serial->port[i].number);
1201 info("%s converter now attached to ttyUSB%d (or usb/tts/%d for devfs)",
1202 type->name, serial->port[i].number, serial->port[i].number);
1205 return serial; /* success */
1208 probe_error:
1209 for (i = 0; i < num_bulk_in; ++i) {
1210 port = &serial->port[i];
1211 if (port->read_urb)
1212 usb_free_urb (port->read_urb);
1213 if (port->bulk_in_buffer)
1214 kfree (port->bulk_in_buffer);
1216 for (i = 0; i < num_bulk_out; ++i) {
1217 port = &serial->port[i];
1218 if (port->write_urb)
1219 usb_free_urb (port->write_urb);
1220 if (port->bulk_out_buffer)
1221 kfree (port->bulk_out_buffer);
1223 for (i = 0; i < num_interrupt_in; ++i) {
1224 port = &serial->port[i];
1225 if (port->interrupt_in_urb)
1226 usb_free_urb (port->interrupt_in_urb);
1227 if (port->interrupt_in_buffer)
1228 kfree (port->interrupt_in_buffer);
1231 /* return the minor range that this device had */
1232 return_serial (serial);
1234 /* free up any memory that we allocated */
1235 kfree (serial);
1236 MOD_DEC_USE_COUNT;
1237 return NULL;
1241 static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
1243 struct usb_serial *serial = (struct usb_serial *) ptr;
1244 struct usb_serial_port *port;
1245 int i;
1247 if (serial) {
1248 /* fail all future close/read/write/ioctl/etc calls */
1249 for (i = 0; i < serial->num_ports; ++i) {
1250 if (serial->port[i].tty != NULL)
1251 serial->port[i].tty->driver_data = NULL;
1254 if (serial->type->shutdown)
1255 serial->type->shutdown(serial);
1257 for (i = 0; i < serial->num_ports; ++i)
1258 serial->port[i].active = 0;
1260 for (i = 0; i < serial->num_bulk_in; ++i) {
1261 port = &serial->port[i];
1262 if (port->read_urb) {
1263 usb_unlink_urb (port->read_urb);
1264 usb_free_urb (port->read_urb);
1266 if (port->bulk_in_buffer)
1267 kfree (port->bulk_in_buffer);
1269 for (i = 0; i < serial->num_bulk_out; ++i) {
1270 port = &serial->port[i];
1271 if (port->write_urb) {
1272 usb_unlink_urb (port->write_urb);
1273 usb_free_urb (port->write_urb);
1275 if (port->bulk_out_buffer)
1276 kfree (port->bulk_out_buffer);
1278 for (i = 0; i < serial->num_interrupt_in; ++i) {
1279 port = &serial->port[i];
1280 if (port->interrupt_in_urb) {
1281 usb_unlink_urb (port->interrupt_in_urb);
1282 usb_free_urb (port->interrupt_in_urb);
1284 if (port->interrupt_in_buffer)
1285 kfree (port->interrupt_in_buffer);
1288 for (i = 0; i < serial->num_ports; ++i) {
1289 tty_unregister_devfs (&serial_tty_driver, serial->port[i].number);
1290 info("%s converter now disconnected from ttyUSB%d", serial->type->name, serial->port[i].number);
1293 /* return the minor range that this device had */
1294 return_serial (serial);
1296 /* free up any memory that we allocated */
1297 kfree (serial);
1299 } else {
1300 info("device disconnected");
1303 MOD_DEC_USE_COUNT;
1307 static struct tty_driver serial_tty_driver = {
1308 magic: TTY_DRIVER_MAGIC,
1309 driver_name: "usb-serial",
1310 name: "usb/tts/%d",
1311 major: SERIAL_TTY_MAJOR,
1312 minor_start: 0,
1313 num: SERIAL_TTY_MINORS,
1314 type: TTY_DRIVER_TYPE_SERIAL,
1315 subtype: SERIAL_TYPE_NORMAL,
1316 flags: TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
1318 refcount: &serial_refcount,
1319 table: serial_tty,
1320 termios: serial_termios,
1321 termios_locked: serial_termios_locked,
1323 open: serial_open,
1324 close: serial_close,
1325 write: serial_write,
1326 write_room: serial_write_room,
1327 ioctl: serial_ioctl,
1328 set_termios: serial_set_termios,
1329 throttle: serial_throttle,
1330 unthrottle: serial_unthrottle,
1331 break_ctl: serial_break,
1332 chars_in_buffer: serial_chars_in_buffer,
1336 int usb_serial_init(void)
1338 int i;
1339 int result;
1341 /* Initalize our global data */
1342 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1343 serial_table[i] = NULL;
1346 /* register the tty driver */
1347 serial_tty_driver.init_termios = tty_std_termios;
1348 serial_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1349 if (tty_register_driver (&serial_tty_driver)) {
1350 err(__FUNCTION__ " - failed to register tty driver");
1351 return -1;
1354 #ifdef CONFIG_USB_SERIAL_GENERIC
1355 generic_device_ids[0].idVendor = vendor;
1356 generic_device_ids[0].idProduct = product;
1357 /* register our generic driver with ourselves */
1358 usb_serial_register (&generic_device);
1359 #endif
1361 /* register the USB driver */
1362 result = usb_register(&usb_serial_driver);
1363 if (result < 0) {
1364 tty_unregister_driver(&serial_tty_driver);
1365 err("usb_register failed for the usb-serial driver. Error number %d", result);
1366 return -1;
1370 return 0;
1374 void usb_serial_exit(void)
1377 #ifdef CONFIG_USB_SERIAL_GENERIC
1378 /* remove our generic driver */
1379 usb_serial_deregister (&generic_device);
1380 #endif
1382 usb_deregister(&usb_serial_driver);
1383 tty_unregister_driver(&serial_tty_driver);
1387 module_init(usb_serial_init);
1388 module_exit(usb_serial_exit);
1391 int usb_serial_register(struct usb_serial_device_type *new_device)
1393 /* Add this device to our list of devices */
1394 list_add(&new_device->driver_list, &usb_serial_driver_list);
1396 info ("USB Serial support registered for %s", new_device->name);
1398 usb_scan_devices();
1400 return 0;
1404 void usb_serial_deregister(struct usb_serial_device_type *device)
1406 struct usb_serial *serial;
1407 int i;
1409 info("USB Serial deregistering driver %s", device->name);
1411 /* clear out the serial_table if the device is attached to a port */
1412 for(i = 0; i < SERIAL_TTY_MINORS; ++i) {
1413 serial = serial_table[i];
1414 if ((serial != NULL) && (serial->type == device)) {
1415 usb_driver_release_interface (&usb_serial_driver, serial->interface);
1416 usb_serial_disconnect (NULL, serial);
1420 list_del(&device->driver_list);
1425 /* If the usb-serial core is build into the core, the usb-serial drivers
1426 need these symbols to load properly as modules. */
1427 EXPORT_SYMBOL(usb_serial_register);
1428 EXPORT_SYMBOL(usb_serial_deregister);
1429 #ifdef USES_EZUSB_FUNCTIONS
1430 EXPORT_SYMBOL(ezusb_writememory);
1431 EXPORT_SYMBOL(ezusb_set_reset);
1432 #endif