Import 2.3.50pre1
[davej-history.git] / drivers / usb / usb-serial.c
blob40c935ce930b2d15d83372706f65f67c0bf8a17f
1 /*
2 * USB Serial Converter driver
4 * (C) 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 * (02/21/2000) gkh
18 * Made it so that any serial devices only have to specify which functions
19 * they want to overload from the generic function calls (great,
20 * inheritance in C, in a driver, just what I wanted...)
21 * Added support for set_termios and ioctl function calls. No drivers take
22 * advantage of this yet.
23 * Removed the #ifdef MODULE, now there is no module specific code.
24 * Cleaned up a few comments in usb-serial.h that were wrong (thanks again
25 * to Miles Lott).
26 * Small fix to get_free_serial.
28 * (02/14/2000) gkh
29 * Removed the Belkin and Peracom functionality from the driver due to
30 * the lack of support from the vendor, and me not wanting people to
31 * accidenatly buy the device, expecting it to work with Linux.
32 * Added read_bulk_callback and write_bulk_callback to the type structure
33 * for the needs of the FTDI and WhiteHEAT driver.
34 * Changed all reverences to FTDI to FTDI_SIO at the request of Bill
35 * Ryder.
36 * Changed the output urb size back to the max endpoint size to make
37 * the ftdi_sio driver have it easier, and due to the fact that it didn't
38 * really increase the speed any.
40 * (02/11/2000) gkh
41 * Added VISOR_FUNCTION_CONSOLE to the visor startup function. This was a
42 * patch from Miles Lott (milos@insync.net).
43 * Fixed bug with not restoring the minor range that a device grabs, if
44 * the startup function fails (thanks Miles for finding this).
46 * (02/05/2000) gkh
47 * Added initial framework for the Keyspan PDA serial converter so that
48 * Brian Warner has a place to put his code.
49 * Made the ezusb specific functions generic enough that different
50 * devices can use them (whiteheat and keyspan_pda both need them).
51 * Split out a whole bunch of structure and other stuff to a seperate
52 * usb-serial.h file.
53 * Made the Visor connection messages a little more understandable, now
54 * that Miles Lott (milos@insync.net) has gotten the Generic channel to
55 * work. Also made them always show up in the log file.
57 * (01/25/2000) gkh
58 * Added initial framework for FTDI serial converter so that Bill Ryder
59 * has a place to put his code.
60 * Added the vendor specific info from Handspring. Now we can print out
61 * informational debug messages as well as understand what is happening.
63 * (01/23/2000) gkh
64 * Fixed problem of crash when trying to open a port that didn't have a
65 * device assigned to it. Made the minor node finding a little smarter,
66 * now it looks to find a continous space for the new device.
68 * (01/21/2000) gkh
69 * Fixed bug in visor_startup with patch from Miles Lott (milos@insync.net)
70 * Fixed get_serial_by_minor which was all messed up for multi port
71 * devices. Fixed multi port problem for generic devices. Now the number
72 * of ports is determined by the number of bulk out endpoints for the
73 * generic device.
75 * (01/19/2000) gkh
76 * Removed lots of cruft that was around from the old (pre urb) driver
77 * interface.
78 * Made the serial_table dynamic. This should save lots of memory when
79 * the number of minor nodes goes up to 256.
80 * Added initial support for devices that have more than one port.
81 * Added more debugging comments for the Visor, and added a needed
82 * set_configuration call.
84 * (01/17/2000) gkh
85 * Fixed the WhiteHEAT firmware (my processing tool had a bug)
86 * and added new debug loader firmware for it.
87 * Removed the put_char function as it isn't really needed.
88 * Added visor startup commands as found by the Win98 dump.
90 * (01/13/2000) gkh
91 * Fixed the vendor id for the generic driver to the one I meant it to be.
93 * (01/12/2000) gkh
94 * Forget the version numbering...that's pretty useless...
95 * Made the driver able to be compiled so that the user can select which
96 * converter they want to use. This allows people who only want the Visor
97 * support to not pay the memory size price of the WhiteHEAT.
98 * Fixed bug where the generic driver (idVendor=0000 and idProduct=0000)
99 * grabbed the root hub. Not good.
101 * version 0.4.0 (01/10/2000) gkh
102 * Added whiteheat.h containing the firmware for the ConnectTech WhiteHEAT
103 * device. Added startup function to allow firmware to be downloaded to
104 * a device if it needs to be.
105 * Added firmware download logic to the WhiteHEAT device.
106 * Started to add #defines to split up the different drivers for potential
107 * configuration option.
109 * version 0.3.1 (12/30/99) gkh
110 * Fixed problems with urb for bulk out.
111 * Added initial support for multiple sets of endpoints. This enables
112 * the Handspring Visor to be attached successfully. Only the first
113 * bulk in / bulk out endpoint pair is being used right now.
115 * version 0.3.0 (12/27/99) gkh
116 * Added initial support for the Handspring Visor based on a patch from
117 * Miles Lott (milos@sneety.insync.net)
118 * Cleaned up the code a bunch and converted over to using urbs only.
120 * version 0.2.3 (12/21/99) gkh
121 * Added initial support for the Connect Tech WhiteHEAT converter.
122 * Incremented the number of ports in expectation of getting the
123 * WhiteHEAT to work properly (4 ports per connection).
124 * Added notification on insertion and removal of what port the
125 * device is/was connected to (and what kind of device it was).
127 * version 0.2.2 (12/16/99) gkh
128 * Changed major number to the new allocated number. We're legal now!
130 * version 0.2.1 (12/14/99) gkh
131 * Fixed bug that happens when device node is opened when there isn't a
132 * device attached to it. Thanks to marek@webdesign.no for noticing this.
134 * version 0.2.0 (11/10/99) gkh
135 * Split up internals to make it easier to add different types of serial
136 * converters to the code.
137 * Added a "generic" driver that gets it's vendor and product id
138 * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
139 * for the idea and sample code (from the usb scanner driver.)
140 * Cleared up any licensing questions by releasing it under the GNU GPL.
142 * version 0.1.2 (10/25/99) gkh
143 * Fixed bug in detecting device.
145 * version 0.1.1 (10/05/99) gkh
146 * Changed the major number to not conflict with anything else.
148 * version 0.1 (09/28/99) gkh
149 * Can recognize the two different devices and start up a read from
150 * device when asked to. Writes also work. No control signals yet, this
151 * all is vendor specific data (i.e. no spec), also no control for
152 * different baud rates or other bit settings.
153 * Currently we are using the same devid as the acm driver. This needs
154 * to change.
158 #include <linux/config.h>
159 #include <linux/kernel.h>
160 #include <linux/sched.h>
161 #include <linux/signal.h>
162 #include <linux/errno.h>
163 #include <linux/poll.h>
164 #include <linux/init.h>
165 #include <linux/malloc.h>
166 #include <linux/fcntl.h>
167 #include <linux/tty_driver.h>
168 #include <linux/tty_flip.h>
169 #include <linux/tty.h>
170 #include <linux/module.h>
171 #include <linux/spinlock.h>
173 #define DEBUG
175 #include "usb.h"
177 #ifdef CONFIG_USB_SERIAL_WHITEHEAT
178 #include "whiteheat.h" /* firmware for the ConnectTech WhiteHEAT device */
179 #endif
181 #include "usb-serial.h"
183 /* parity check flag */
184 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
186 /* local function prototypes */
187 static int serial_open (struct tty_struct *tty, struct file * filp);
188 static void serial_close (struct tty_struct *tty, struct file * filp);
189 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
190 static int serial_write_room (struct tty_struct *tty);
191 static int serial_chars_in_buffer (struct tty_struct *tty);
192 static void serial_throttle (struct tty_struct * tty);
193 static void serial_unthrottle (struct tty_struct * tty);
194 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg);
195 static void serial_set_termios (struct tty_struct *tty, struct termios * old);
197 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum);
198 static void usb_serial_disconnect(struct usb_device *dev, void *ptr);
200 static struct usb_driver usb_serial_driver = {
201 "serial",
202 usb_serial_probe,
203 usb_serial_disconnect,
204 { NULL, NULL }
207 static int serial_refcount;
208 static struct tty_struct * serial_tty[SERIAL_TTY_MINORS];
209 static struct termios * serial_termios[SERIAL_TTY_MINORS];
210 static struct termios * serial_termios_locked[SERIAL_TTY_MINORS];
211 static struct usb_serial *serial_table[SERIAL_TTY_MINORS] = {NULL, };
215 static struct usb_serial *get_serial_by_minor (int minor)
217 int i;
219 dbg("get_serial_by_minor %d", minor);
221 if (serial_table[minor] == NULL)
222 return (NULL);
224 if (serial_table[minor] != SERIAL_PTR_EMPTY)
225 return (serial_table[minor]);
227 i = minor;
228 while (serial_table[i] == SERIAL_PTR_EMPTY) {
229 if (i == 0)
230 return (NULL);
231 --i;
233 return (serial_table[i]);
237 static struct usb_serial *get_free_serial (int num_ports, int *minor)
239 struct usb_serial *serial = NULL;
240 int i, j;
241 int good_spot;
243 dbg("get_free_serial %d", num_ports);
245 *minor = 0;
246 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
247 if (serial_table[i])
248 continue;
250 good_spot = 1;
251 for (j = 1; j <= num_ports-1; ++j)
252 if (serial_table[i+j])
253 good_spot = 0;
254 if (good_spot == 0)
255 continue;
257 if (!(serial = kmalloc(sizeof(struct usb_serial), GFP_KERNEL))) {
258 err("Out of memory");
259 return NULL;
261 memset(serial, 0, sizeof(struct usb_serial));
262 serial_table[i] = serial;
263 *minor = i;
264 dbg("minor base = %d", *minor);
265 for (i = *minor+1; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
266 serial_table[i] = SERIAL_PTR_EMPTY;
267 return (serial);
269 return (NULL);
273 static void return_serial (struct usb_serial *serial)
275 int i;
277 dbg("return_serial");
279 if (serial == NULL)
280 return;
282 for (i = 0; i < serial->num_ports; ++i) {
283 serial_table[serial->minor + i] = NULL;
286 return;
290 #ifdef USES_EZUSB_FUNCTIONS
291 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
292 #define CPUCS_REG 0x7F92
294 static int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest)
296 int result;
297 unsigned char *transfer_buffer = kmalloc (length, GFP_KERNEL);
299 // dbg("ezusb_writememory %x, %d", address, length);
301 if (!transfer_buffer) {
302 err("ezusb_writememory: kmalloc(%d) failed.", length);
303 return -ENOMEM;
305 memcpy (transfer_buffer, data, length);
306 result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0), bRequest, 0x40, address, 0, transfer_buffer, length, 300);
307 kfree (transfer_buffer);
308 return result;
312 static int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit)
314 int response;
315 dbg("ezusb_set_reset: %d", reset_bit);
316 response = ezusb_writememory (serial, CPUCS_REG, &reset_bit, 1, 0xa0);
317 if (response < 0) {
318 err("ezusb_set_reset %d failed", reset_bit);
320 return (response);
323 #endif /* USES_EZUSB_FUNCTIONS */
326 /*****************************************************************************
327 * Driver tty interface functions
328 *****************************************************************************/
329 static int serial_open (struct tty_struct *tty, struct file * filp)
331 struct usb_serial *serial;
333 dbg("serial_open");
335 /* initialize the pointer incase something fails */
336 tty->driver_data = NULL;
338 /* get the serial object associated with this tty pointer */
339 serial = get_serial_by_minor (MINOR(tty->device));
341 /* do some sanity checking that we really have a device present */
342 if (!serial) {
343 dbg("serial == NULL!");
344 return (-ENODEV);
346 if (!serial->type) {
347 dbg("serial->type == NULL!");
348 return (-ENODEV);
351 /* make the tty driver remember our serial object, and us it */
352 tty->driver_data = serial;
353 serial->tty = tty;
355 /* pass on to the driver specific version of this function if it is available */
356 if (serial->type->open) {
357 return (serial->type->open(tty, filp));
358 } else {
359 return (generic_serial_open(tty, filp));
364 static void serial_close(struct tty_struct *tty, struct file * filp)
366 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
367 int port;
369 dbg("serial_close");
371 if (!serial) {
372 dbg("serial == NULL!");
373 return;
376 port = MINOR(tty->device) - serial->minor;
378 dbg("serial_close port %d", port);
380 /* do some sanity checking that we really have a device present */
381 if (!serial->type) {
382 dbg("serial->type == NULL!");
383 return;
385 if (!serial->active[port]) {
386 dbg ("device already open");
387 return;
390 /* pass on to the driver specific version of this function if it is available */
391 if (serial->type->close) {
392 serial->type->close(tty, filp);
393 } else {
394 generic_serial_close(tty, filp);
399 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
401 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
402 int port = MINOR(tty->device) - serial->minor;
404 dbg("serial_write port %d, %d byte(s)", port, count);
406 /* do some sanity checking that we really have a device present */
407 if (!serial) {
408 dbg("serial == NULL!");
409 return (-ENODEV);
411 if (!serial->type) {
412 dbg("serial->type == NULL!");
413 return (-ENODEV);
415 if (!serial->active[port]) {
416 dbg ("device not opened");
417 return (-EINVAL);
420 /* pass on to the driver specific version of this function if it is available */
421 if (serial->type->write) {
422 return (serial->type->write(tty, from_user, buf, count));
423 } else {
424 return (generic_serial_write(tty, from_user, buf, count));
429 static int serial_write_room (struct tty_struct *tty)
431 struct usb_serial *serial = (struct usb_serial *)tty->driver_data;
432 int port = MINOR(tty->device) - serial->minor;
434 dbg("serial_write_room port %d", port);
436 /* do some sanity checking that we really have a device present */
437 if (!serial) {
438 dbg("serial == NULL!");
439 return (-ENODEV);
441 if (!serial->type) {
442 dbg("serial->type == NULL!");
443 return (-ENODEV);
445 if (!serial->active[port]) {
446 dbg ("device not open");
447 return (-EINVAL);
450 /* pass on to the driver specific version of this function if it is available */
451 if (serial->type->write_room) {
452 return (serial->type->write_room(tty));
453 } else {
454 return (generic_write_room(tty));
459 static int serial_chars_in_buffer (struct tty_struct *tty)
461 struct usb_serial *serial = (struct usb_serial *)tty->driver_data;
462 int port = MINOR(tty->device) - serial->minor;
464 dbg("serial_chars_in_buffer port %d", port);
466 /* do some sanity checking that we really have a device present */
467 if (!serial) {
468 dbg("serial == NULL!");
469 return (-ENODEV);
471 if (!serial->type) {
472 dbg("serial->type == NULL!");
473 return (-ENODEV);
475 if (!serial->active[port]) {
476 dbg ("device not open");
477 return (-EINVAL);
480 /* pass on to the driver specific version of this function if it is available */
481 if (serial->type->chars_in_buffer) {
482 return (serial->type->chars_in_buffer(tty));
483 } else {
484 return (generic_chars_in_buffer(tty));
489 static void serial_throttle (struct tty_struct * tty)
491 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
492 int port = MINOR(tty->device) - serial->minor;
494 dbg("serial_throttle port %d", port);
496 /* do some sanity checking that we really have a device present */
497 if (!serial) {
498 dbg("serial == NULL!");
499 return;
501 if (!serial->type) {
502 dbg("serial->type == NULL!");
503 return;
505 if (!serial->active[port]) {
506 dbg ("device not open");
507 return;
510 /* pass on to the driver specific version of this function */
511 if (serial->type->throttle) {
512 serial->type->throttle(tty);
513 } else {
514 generic_throttle(tty);
517 return;
521 static void serial_unthrottle (struct tty_struct * tty)
523 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
524 int port = MINOR(tty->device) - serial->minor;
526 dbg("serial_unthrottle port %d", port);
528 /* do some sanity checking that we really have a device present */
529 if (!serial) {
530 dbg("serial == NULL!");
531 return;
533 if (!serial->type) {
534 dbg("serial->type == NULL!");
535 return;
537 if (!serial->active[port]) {
538 dbg ("device not open");
539 return;
542 /* pass on to the driver specific version of this function */
543 if (serial->type->unthrottle) {
544 serial->type->unthrottle(tty);
545 } else {
546 generic_unthrottle(tty);
549 return;
553 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
555 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
556 int port;
558 dbg("serial_ioctl");
560 if (!serial) {
561 dbg("serial == NULL!");
562 return -ENODEV;
565 port = MINOR(tty->device) - serial->minor;
567 dbg("serial_ioctl port %d", port);
569 /* do some sanity checking that we really have a device present */
570 if (!serial->type) {
571 dbg("serial->type == NULL!");
572 return -ENODEV;
574 if (!serial->active[port]) {
575 dbg ("device not open");
576 return -ENODEV;
579 /* pass on to the driver specific version of this function if it is available */
580 if (serial->type->ioctl) {
581 return (serial->type->ioctl(tty, file, cmd, arg));
582 } else {
583 return (generic_ioctl (tty, file, cmd, arg));
588 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
590 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
591 int port;
593 dbg("serial_set_termios");
595 if (!serial) {
596 dbg("serial == NULL!");
597 return;
600 port = MINOR(tty->device) - serial->minor;
602 dbg("serial_set_termios port %d", port);
604 /* do some sanity checking that we really have a device present */
605 if (!serial->type) {
606 dbg("serial->type == NULL!");
607 return;
609 if (!serial->active[port]) {
610 dbg ("device not open");
611 return;
614 /* pass on to the driver specific version of this function if it is available */
615 if (serial->type->set_termios) {
616 serial->type->set_termios(tty, old);
617 } else {
618 generic_set_termios (tty, old);
621 return;
625 #ifdef CONFIG_USB_SERIAL_WHITEHEAT
626 /*****************************************************************************
627 * Connect Tech's White Heat specific driver functions
628 *****************************************************************************/
629 static int whiteheat_serial_open (struct tty_struct *tty, struct file *filp)
631 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
632 int port = MINOR(tty->device) - serial->minor;
634 dbg("whiteheat_serial_open port %d", port);
636 if (serial->active[port]) {
637 dbg ("device already open");
638 return -EINVAL;
640 serial->active[port] = 1;
642 /*Start reading from the device*/
643 if (usb_submit_urb(&serial->read_urb[port]))
644 dbg("usb_submit_urb(read bulk) failed");
646 /* Need to do device specific setup here (control lines, baud rate, etc.) */
647 /* FIXME!!! */
649 return (0);
653 static void whiteheat_serial_close(struct tty_struct *tty, struct file * filp)
655 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
656 int port = MINOR(tty->device) - serial->minor;
658 dbg("whiteheat_serial_close port %d", port);
660 /* Need to change the control lines here */
661 /* FIXME */
663 /* shutdown our bulk reads and writes */
664 usb_unlink_urb (&serial->write_urb[port]);
665 usb_unlink_urb (&serial->read_urb[port]);
666 serial->active[port] = 0;
670 static void whiteheat_set_termios (struct tty_struct *tty, struct termios *old_termios)
672 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
673 int port = MINOR(tty->device) - serial->minor;
674 unsigned int cflag = tty->termios->c_cflag;
676 dbg("whiteheat_set_termios port %d", port);
678 /* check that they really want us to change something */
679 if (old_termios) {
680 if ((cflag == old_termios->c_cflag) &&
681 (RELEVANT_IFLAG(tty->termios->c_iflag) == RELEVANT_IFLAG(old_termios->c_iflag))) {
682 dbg("nothing to change...");
683 return;
687 /* do the parsing of the cflag to see what to set the line to */
688 /* FIXME!! */
690 return;
693 static void whiteheat_throttle (struct tty_struct * tty)
695 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
696 int port = MINOR(tty->device) - serial->minor;
698 dbg("whiteheat_throttle port %d", port);
700 /* Change the control signals */
701 /* FIXME!!! */
703 return;
707 static void whiteheat_unthrottle (struct tty_struct * tty)
709 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
710 int port = MINOR(tty->device) - serial->minor;
712 dbg("whiteheat_unthrottle port %d", port);
714 /* Change the control signals */
715 /* FIXME!!! */
717 return;
721 /* steps to download the firmware to the WhiteHEAT device:
722 - hold the reset (by writing to the reset bit of the CPUCS register)
723 - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
724 - release the reset (by writing to the CPUCS register)
725 - download the WH.HEX file for all addresses greater than 0x1b3f using
726 VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
727 - hold the reset
728 - download the WH.HEX file for all addresses less than 0x1b40 using
729 VENDOR_REQUEST_ANCHOR_LOAD
730 - release the reset
731 - device renumerated itself and comes up as new device id with all
732 firmware download completed.
734 static int whiteheat_startup (struct usb_serial *serial)
736 int response;
737 const struct whiteheat_hex_record *record;
739 dbg("whiteheat_startup");
741 response = ezusb_set_reset (serial, 1);
743 record = &whiteheat_loader[0];
744 while (record->address != 0xffff) {
745 response = ezusb_writememory (serial, record->address,
746 (unsigned char *)record->data, record->data_size, 0xa0);
747 if (response < 0) {
748 err("ezusb_writememory failed for loader (%d %04X %p %d)",
749 response, record->address, record->data, record->data_size);
750 break;
752 ++record;
755 response = ezusb_set_reset (serial, 0);
757 record = &whiteheat_firmware[0];
758 while (record->address < 0x1b40) {
759 ++record;
761 while (record->address != 0xffff) {
762 response = ezusb_writememory (serial, record->address,
763 (unsigned char *)record->data, record->data_size, 0xa0);
764 if (response < 0) {
765 err("ezusb_writememory failed for first firmware step (%d %04X %p %d)",
766 response, record->address, record->data, record->data_size);
767 break;
769 ++record;
772 response = ezusb_set_reset (serial, 1);
774 record = &whiteheat_firmware[0];
775 while (record->address < 0x1b40) {
776 response = ezusb_writememory (serial, record->address,
777 (unsigned char *)record->data, record->data_size, 0xa0);
778 if (response < 0) {
779 err("ezusb_writememory failed for second firmware step (%d %04X %p %d)",
780 response, record->address, record->data, record->data_size);
781 break;
783 ++record;
786 response = ezusb_set_reset (serial, 0);
788 /* we want this device to fail to have a driver assigned to it. */
789 return (1);
791 #endif /* CONFIG_USB_SERIAL_WHITEHEAT */
794 #ifdef CONFIG_USB_SERIAL_VISOR
795 /******************************************************************************
796 * Handspring Visor specific driver functions
797 ******************************************************************************/
798 static int visor_serial_open (struct tty_struct *tty, struct file *filp)
800 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
801 int port = MINOR(tty->device) - serial->minor;
803 dbg("visor_serial_open port %d", port);
805 if (serial->active[port]) {
806 dbg ("device already open");
807 return -EINVAL;
810 serial->active[port] = 1;
812 /*Start reading from the device*/
813 if (usb_submit_urb(&serial->read_urb[port]))
814 dbg("usb_submit_urb(read bulk) failed");
816 return (0);
819 static void visor_serial_close(struct tty_struct *tty, struct file * filp)
821 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
822 int port = MINOR(tty->device) - serial->minor;
823 unsigned char *transfer_buffer = kmalloc (0x12, GFP_KERNEL);
825 dbg("visor_serial_close port %d", port);
827 if (!transfer_buffer) {
828 err("visor_serial_close: kmalloc(%d) failed.", 0x12);
829 } else {
830 /* send a shutdown message to the device */
831 usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_CLOSE_NOTIFICATION,
832 0xc2, 0x0000, 0x0000, transfer_buffer, 0x12, 300);
835 /* shutdown our bulk reads and writes */
836 usb_unlink_urb (&serial->write_urb[port]);
837 usb_unlink_urb (&serial->read_urb[port]);
838 serial->active[port] = 0;
842 static void visor_throttle (struct tty_struct * tty)
844 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
845 int port = MINOR(tty->device) - serial->minor;
847 dbg("visor_throttle port %d", port);
849 usb_unlink_urb (&serial->read_urb[port]);
851 return;
855 static void visor_unthrottle (struct tty_struct * tty)
857 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
858 int port = MINOR(tty->device) - serial->minor;
860 dbg("visor_unthrottle port %d", port);
862 if (usb_unlink_urb (&serial->read_urb[port]))
863 dbg("usb_submit_urb(read bulk) failed");
865 return;
869 static int visor_startup (struct usb_serial *serial)
871 int response;
872 int i;
873 unsigned char *transfer_buffer = kmalloc (256, GFP_KERNEL);
875 if (!transfer_buffer) {
876 err("visor_startup: kmalloc(%d) failed.", 256);
877 return -ENOMEM;
880 dbg("visor_startup");
882 dbg("visor_setup: Set config to 1");
883 usb_set_configuration (serial->dev, 1);
885 /* send a get connection info request */
886 response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_GET_CONNECTION_INFORMATION,
887 0xc2, 0x0000, 0x0000, transfer_buffer, 0x12, 300);
888 if (response < 0) {
889 err("visor_startup: error getting connection information");
890 } else {
891 struct visor_connection_info *connection_info = (struct visor_connection_info *)transfer_buffer;
892 char *string;
893 info("%s: Number of ports: %d", serial->type->name, connection_info->num_ports);
894 for (i = 0; i < connection_info->num_ports; ++i) {
895 switch (connection_info->connections[i].port_function_id) {
896 case VISOR_FUNCTION_GENERIC:
897 string = "Generic";
898 break;
899 case VISOR_FUNCTION_DEBUGGER:
900 string = "Debugger";
901 break;
902 case VISOR_FUNCTION_HOTSYNC:
903 string = "HotSync";
904 break;
905 case VISOR_FUNCTION_CONSOLE:
906 string = "Console";
907 break;
908 case VISOR_FUNCTION_REMOTE_FILE_SYS:
909 string = "Remote File System";
910 break;
911 default:
912 string = "unknown";
913 break;
915 info("%s: port %d, is for %s use and is bound to ttyUSB%d", serial->type->name, connection_info->connections[i].port, string, serial->minor + i);
919 /* ask for the number of bytes available, but ignore the response as it is broken */
920 response = usb_control_msg (serial->dev, usb_rcvctrlpipe(serial->dev, 0), VISOR_REQUEST_BYTES_AVAILABLE,
921 0xc2, 0x0000, 0x0005, transfer_buffer, 0x02, 300);
922 if (response < 0) {
923 err("visor_startup: error getting bytes available request");
926 kfree (transfer_buffer);
928 /* continue on with initialization */
929 return (0);
933 #endif /* CONFIG_USB_SERIAL_VISOR*/
936 #ifdef CONFIG_USB_SERIAL_FTDI_SIO
937 /******************************************************************************
938 * FTDI SIO Serial Converter specific driver functions
939 ******************************************************************************/
941 /* Bill Ryder - bryder@sgi.com - wrote the FTDI_SIO implementation */
942 /* Thanx to FTDI for so kindly providing details of the protocol required */
943 /* to talk to the device */
945 #include "ftdi_sio.h"
947 static int ftdi_sio_serial_open (struct tty_struct *tty, struct file *filp)
949 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
950 char buf[1]; /* Needed for the usb_control_msg I think */
951 int port = MINOR(tty->device) - serial->minor;
953 dbg("ftdi_sio_serial_open port %d", port);
955 if (serial->active[port]) {
956 dbg ("device already open");
957 return -EINVAL;
959 serial->active[port] = 1;
961 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
962 FTDI_SIO_RESET_REQUEST, FTDI_SIO_RESET_REQUEST_TYPE,
963 FTDI_SIO_RESET_SIO,
964 0, buf, 0, HZ * 5);
966 /* FIXME - Should I really purge the buffers? */
967 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
968 FTDI_SIO_RESET_REQUEST, FTDI_SIO_RESET_REQUEST_TYPE,
969 FTDI_SIO_RESET_PURGE_RX,
970 0, buf, 0, HZ * 5);
972 usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
973 FTDI_SIO_RESET_REQUEST, FTDI_SIO_RESET_REQUEST_TYPE,
974 FTDI_SIO_RESET_PURGE_TX,
975 0, buf, 0, HZ * 5);
978 /* As per usb_serial_init s/be CS8, B9600, 1 STOP BIT */
979 if ( usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
980 FTDI_SIO_SET_BAUDRATE_REQUEST,
981 FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE,
982 ftdi_sio_b9600, 0,
983 buf, 0, HZ * 5) < 0){
984 dbg("Error from baudrate urb");
985 return(-EINVAL);
988 if ( usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
989 FTDI_SIO_SET_DATA_REQUEST,
990 FTDI_SIO_SET_DATA_REQUEST_TYPE,
991 8 | FTDI_SIO_SET_DATA_PARITY_NONE |
992 FTDI_SIO_SET_DATA_STOP_BITS_1, 0,
993 buf, 0, HZ * 5) < 0){
994 dbg("Error from cs8/noparity/1 stopbit urb");
995 return(-EINVAL);
998 /* Disable flow control */
999 if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1000 FTDI_SIO_SET_FLOW_CTRL_REQUEST,
1001 FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1002 0, 0,
1003 buf, 0, HZ * 5) < 0) {
1004 dbg("error from flowcontrol urb");
1005 return(-EINVAL);
1008 /* Turn on RTS and DTR since we are not flow controlling*/
1009 /* FIXME - check for correct behaviour clocal vs non clocal */
1010 /* FIXME - might be able to do both simultaneously */
1011 if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1012 FTDI_SIO_SET_MODEM_CTRL_REQUEST,
1013 FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1014 (unsigned)FTDI_SIO_SET_DTR_HIGH, 0,
1015 buf, 0, HZ * 5) < 0) {
1016 dbg("Error from DTR HIGH urb");
1018 if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1019 FTDI_SIO_SET_MODEM_CTRL_REQUEST,
1020 FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1021 (unsigned)FTDI_SIO_SET_RTS_HIGH, 0,
1022 buf, 0, HZ * 5) < 0) {
1023 dbg("Error from RTS HIGH urb");
1026 /*Start reading from the device*/
1027 if (usb_submit_urb(&serial->read_urb[port]))
1028 dbg("usb_submit_urb(read bulk) failed");
1031 return (0);
1035 static void ftdi_sio_serial_close (struct tty_struct *tty, struct file *filp)
1037 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1038 char buf[1];
1039 int port = MINOR(tty->device) - serial->minor;
1041 dbg("ftdi_sio_serial_close port %d", port);
1043 /* FIXME - might be able to do both simultaneously */
1044 if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1045 FTDI_SIO_SET_MODEM_CTRL_REQUEST,
1046 FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1047 (unsigned)FTDI_SIO_SET_DTR_LOW, 0,
1048 buf, 0, HZ * 5) < 0) {
1049 dbg("Error from DTR LOW urb");
1051 if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1052 FTDI_SIO_SET_MODEM_CTRL_REQUEST,
1053 FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1054 (unsigned)FTDI_SIO_SET_RTS_LOW, 0,
1055 buf, 0, HZ * 5) < 0) {
1056 dbg("Error from RTS LOW urb");
1059 /* FIXME Should I flush the device here? - not doing it for now */
1061 /* shutdown our bulk reads and writes */
1062 usb_unlink_urb (&serial->write_urb[port]);
1063 usb_unlink_urb (&serial->read_urb[port]);
1064 serial->active[port] = 0;
1069 /* The ftdi_sio requires the first byte to have:
1070 B0 1
1071 B1 0
1072 B2..7 length of message excluding byte 0
1074 static int ftdi_sio_serial_write (struct tty_struct * tty, int from_user,
1075 const unsigned char *buf, int count)
1077 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1078 int port = MINOR(tty->device) - serial->minor;
1079 const int data_offset = 1;
1081 dbg("ftdi_sio_serial_write port %d, %d bytes", port, count);
1083 if (count == 0) {
1084 dbg("write request of 0 bytes");
1085 return (0);
1088 /* only do something if we have a bulk out endpoint */
1089 if (serial->num_bulk_out) {
1090 unsigned char *first_byte = serial->write_urb[port].transfer_buffer;
1092 if (serial->write_urb[port].status == -EINPROGRESS) {
1093 dbg ("already writing");
1094 return (0);
1097 count += data_offset;
1098 count = (count > serial->bulk_out_size[port]) ?
1099 serial->bulk_out_size[port] : count;
1102 /* Copy in the data to send */
1103 if (from_user) {
1104 copy_from_user(serial->write_urb[port].transfer_buffer + data_offset ,
1105 buf, count - data_offset );
1107 else {
1108 memcpy(serial->write_urb[port].transfer_buffer + data_offset,
1109 buf, count - data_offset );
1112 /* Write the control byte at the front of the packet*/
1113 first_byte = serial->write_urb[port].transfer_buffer;
1114 *first_byte = 1 | ((count-data_offset) << 2) ;
1116 #ifdef DEBUG
1117 dbg("Bytes: %d, Control Byte: 0o%03o",count, first_byte[0]);
1119 if (count) {
1120 int i;
1121 printk (KERN_DEBUG __FILE__ ": data written - length = %d, data = ", count);
1122 for (i = 0; i < count; ++i) {
1123 printk ("0x%02x ", first_byte[i]);
1124 if (first_byte[i] > ' ' && first_byte[i] < '~') {
1125 printk("%c ", first_byte[i]);
1126 } else {
1127 printk(" ");
1132 printk ("\n");
1135 #endif
1138 /* send the data out the bulk port */
1139 serial->write_urb[port].transfer_buffer_length = count;
1141 if (usb_submit_urb(&serial->write_urb[port]))
1142 dbg("usb_submit_urb(write bulk) failed");
1144 dbg("write returning: %d",count - data_offset);
1145 return (count - data_offset);
1148 /* no bulk out, so return 0 bytes written */
1149 return (0);
1153 static void ftdi_sio_read_bulk_callback (struct urb *urb)
1154 { /* ftdi_sio_serial_buld_callback */
1155 struct usb_serial *serial = (struct usb_serial *)urb->context;
1156 struct tty_struct *tty = serial->tty;
1157 unsigned char *data = urb->transfer_buffer;
1158 const int data_offset = 2;
1159 int i;
1161 dbg("ftdi_sio_read_bulk_callback");
1163 if (urb->status) {
1164 dbg("nonzero read bulk status received: %d", urb->status);
1165 return;
1168 #ifdef DEBUG
1169 if (urb->actual_length > 2) {
1170 printk (KERN_DEBUG __FILE__ ": data read - length = %d, data = ", urb->actual_length);
1171 for (i = 0; i < urb->actual_length; ++i) {
1172 printk ("0x%.2x ", data[i]);
1173 if (data[i] > ' ' && data[i] < '~') {
1174 printk("%c ", data[i]);
1175 } else {
1176 printk(" ");
1179 printk ("\n");
1181 #endif
1184 if (urb->actual_length > data_offset) {
1185 for (i = data_offset ; i < urb->actual_length ; ++i) {
1186 tty_insert_flip_char(tty, data[i], 0);
1188 tty_flip_buffer_push(tty);
1191 /* Continue trying to always read */
1192 if (usb_submit_urb(urb))
1193 dbg("failed resubmitting read urb");
1195 return;
1196 } /* ftdi_sio_serial_read_bulk_callback */
1198 static void ftdi_sio_set_termios (struct tty_struct *tty, struct termios *old_termios)
1200 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1201 int port = MINOR(tty->device) - serial->minor;
1202 unsigned int cflag = tty->termios->c_cflag;
1203 __u16 urb_value; /* Will hold the new flags */
1204 char buf[1]; /* Perhaps I should dynamically alloc this? */
1205 dbg("ftdi_sio_set_termios port %d", port);
1207 /* FIXME - we should keep the old termios really */
1208 /* FIXME -For this cut I don't care if the line is really changing or
1209 not - so just do the change regardless */
1211 /* Set number of data bits, parity, stop bits */
1213 urb_value = 0;
1214 urb_value |= (cflag & CSTOPB ? FTDI_SIO_SET_DATA_STOP_BITS_2 :
1215 FTDI_SIO_SET_DATA_STOP_BITS_1);
1216 urb_value |= (cflag & PARENB ?
1217 (cflag & PARODD ? FTDI_SIO_SET_DATA_PARITY_ODD :
1218 FTDI_SIO_SET_DATA_PARITY_EVEN) :
1219 FTDI_SIO_SET_DATA_PARITY_NONE);
1220 if (cflag & CSIZE) {
1221 switch (cflag & CSIZE) {
1222 case CS5: urb_value |= 5; dbg("Setting CS5"); break;
1223 case CS6: urb_value |= 6; dbg("Setting CS6"); break;
1224 case CS7: urb_value |= 7; dbg("Setting CS7"); break;
1225 case CS8: urb_value |= 8; dbg("Setting CS8"); break;
1226 default:
1227 dbg("CSIZE was set but not CS5-CS8");
1230 if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1231 FTDI_SIO_SET_DATA_REQUEST,
1232 FTDI_SIO_SET_DATA_REQUEST_TYPE,
1233 urb_value , 0,
1234 buf, 0, 100) < 0) {
1235 dbg("FAILED to set databits/stopbits/parity");
1238 /* Now do the baudrate */
1239 /* FIXME - should drop lines on B0 */
1240 /* FIXME Should also handle CLOCAL here */
1242 switch(cflag & CBAUD){
1243 case B300: urb_value = ftdi_sio_b300; dbg("Set to 300"); break;
1244 case B600: urb_value = ftdi_sio_b600; dbg("Set to 600") ; break;
1245 case B1200: urb_value = ftdi_sio_b1200; dbg("Set to 1200") ; break;
1246 case B2400: urb_value = ftdi_sio_b2400; dbg("Set to 2400") ; break;
1247 case B4800: urb_value = ftdi_sio_b4800; dbg("Set to 4800") ; break;
1248 case B9600: urb_value = ftdi_sio_b9600; dbg("Set to 9600") ; break;
1249 case B19200: urb_value = ftdi_sio_b19200; dbg("Set to 19200") ; break;
1250 case B38400: urb_value = ftdi_sio_b38400; dbg("Set to 38400") ; break;
1251 case B57600: urb_value = ftdi_sio_b57600; dbg("Set to 57600") ; break;
1252 case B115200: urb_value = ftdi_sio_b115200; dbg("Set to 115200") ; break;
1253 default: dbg("FTDI_SIO does not support the baudrate requested");
1254 /* FIXME - how to return an error for this? */ break;
1256 /* Send the URB */
1257 if (usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
1258 FTDI_SIO_SET_BAUDRATE_REQUEST,
1259 FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE,
1260 urb_value, 0,
1261 buf, 0, 100) < 0) {
1262 dbg("urb failed to set baurdrate");
1264 return;
1267 /*FIXME - the beginnings of this implementation - not even hooked into the driver yet */
1268 static int ftdi_sio_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
1270 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1271 int port = MINOR(tty->device) - serial->minor;
1272 __u16 urb_value=0; /* Will hold the new flags */
1273 char buf[1];
1274 int ret, mask;
1275 dbg("ftdi_sio_ioctl port %d", port);
1277 /* Based on code from acm.c */
1278 switch (cmd) {
1280 case TIOCMGET:
1281 /* Request the status from the device */
1282 if ((ret = usb_control_msg(serial->dev,
1283 usb_sndctrlpipe(serial->dev, 0),
1284 FTDI_SIO_GET_MODEM_STATUS_REQUEST,
1285 FTDI_SIO_GET_MODEM_STATUS_REQUEST_TYPE,
1286 0, 0,
1287 buf, 1, HZ * 5)) < 0 ) {
1288 dbg("Get not get modem status of device");
1289 return(ret);
1292 return put_user((buf[0] & FTDI_SIO_DSR_MASK ? TIOCM_DSR : 0) |
1293 (buf[0] & FTDI_SIO_CTS_MASK ? TIOCM_CTS : 0) |
1294 (buf[0] & FTDI_SIO_RI_MASK ? TIOCM_RI : 0) |
1295 (buf[0] & FTDI_SIO_RLSD_MASK ? TIOCM_CD : 0),
1296 (unsigned long *) arg);
1297 break;
1299 case TIOCMSET:
1300 case TIOCMBIS:
1301 case TIOCMBIC:
1302 if ((ret = get_user(mask, (unsigned long *) arg))) return ret;
1304 /* FIXME Need to remember if we have set DTR or RTS since we
1305 can't ask the device */
1306 /* FIXME - also need to find the meaning of TIOCMBIS/BIC/SET */
1307 if (mask & TIOCM_DTR) {
1308 switch(cmd) {
1309 case TIOCMSET:
1310 urb_value = FTDI_SIO_SET_DTR_HIGH | FTDI_SIO_SET_RTS_LOW;
1311 break;
1313 case TIOCMBIS:
1314 /* Will leave RTS alone and set DTR */
1315 urb_value = FTDI_SIO_SET_DTR_HIGH;
1316 break;
1318 case TIOCMBIC:
1319 urb_value = FTDI_SIO_SET_DTR_LOW;
1320 break;
1324 if (mask & TIOCM_RTS) {
1325 switch(cmd) {
1326 case TIOCMSET:
1327 urb_value = FTDI_SIO_SET_DTR_LOW | FTDI_SIO_SET_RTS_HIGH;
1328 break;
1330 case TIOCMBIS:
1331 /* Will leave DTR and set RTS */
1332 urb_value = FTDI_SIO_SET_RTS_HIGH;
1333 break;
1335 case TIOCMBIC:
1336 /* Will unset RTS */
1337 urb_value = FTDI_SIO_SET_RTS_LOW;
1338 break;
1343 return(usb_control_msg(serial->dev,
1344 usb_sndctrlpipe(serial->dev, 0),
1345 FTDI_SIO_SET_MODEM_CTRL_REQUEST,
1346 FTDI_SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1347 urb_value , 0,
1348 buf, 0, HZ * 5));
1351 return -ENOIOCTLCMD;
1354 #endif /* CONFIG_USB_SERIAL_FTDI_SIO */
1357 #ifdef CONFIG_USB_SERIAL_KEYSPAN_PDA
1358 /*****************************************************************************
1359 * Keyspan PDA specific driver functions
1360 *****************************************************************************/
1361 static int keyspan_pda_serial_open (struct tty_struct *tty, struct file *filp)
1363 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1364 int port = MINOR(tty->device) - serial->minor;
1366 dbg("keyspan_pda_serial_open port %d", port);
1368 if (serial->active[port]) {
1369 dbg ("device already open");
1370 return -EINVAL;
1372 serial->active[port] = 1;
1374 /*Start reading from the device*/
1375 if (usb_submit_urb(&serial->read_urb[port]))
1376 dbg("usb_submit_urb(read bulk) failed");
1378 /* Need to do device specific setup here (control lines, baud rate, etc.) */
1379 /* FIXME */
1381 return (0);
1385 static void keyspan_pda_serial_close(struct tty_struct *tty, struct file * filp)
1387 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1388 int port = MINOR(tty->device) - serial->minor;
1390 dbg("keyspan_pda_serial_close port %d", port);
1392 /* Need to change the control lines here */
1393 /* FIXME */
1395 /* shutdown our bulk reads and writes */
1396 usb_unlink_urb (&serial->write_urb[port]);
1397 usb_unlink_urb (&serial->read_urb[port]);
1398 serial->active[port] = 0;
1402 static int keyspan_pda_startup (struct usb_serial *serial)
1404 dbg("keyspan_pda_startup");
1406 /* download the firmware here ... */
1407 /* FIXME */
1409 /* we want this device to fail to have a driver assigned to it. */
1410 return (1);
1412 #endif /* CONFIG_USB_SERIAL_KEYSPAN_PDA */
1415 /*****************************************************************************
1416 * generic devices specific driver functions
1417 *****************************************************************************/
1418 static int generic_serial_open (struct tty_struct *tty, struct file *filp)
1420 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1421 int port = MINOR(tty->device) - serial->minor;
1423 dbg("generic_serial_open port %d", port);
1425 if (serial->active[port]) {
1426 dbg ("device already open");
1427 return -EINVAL;
1429 serial->active[port] = 1;
1431 /* if we have a bulk interrupt, start reading from it */
1432 if (serial->num_bulk_in) {
1433 /*Start reading from the device*/
1434 if (usb_submit_urb(&serial->read_urb[port]))
1435 dbg("usb_submit_urb(read bulk) failed");
1438 return (0);
1442 static void generic_serial_close(struct tty_struct *tty, struct file * filp)
1444 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1445 int port = MINOR(tty->device) - serial->minor;
1447 dbg("generic_serial_close port %d", port);
1449 /* shutdown any bulk reads that might be going on */
1450 if (serial->num_bulk_out) {
1451 usb_unlink_urb (&serial->write_urb[port]);
1453 if (serial->num_bulk_in) {
1454 usb_unlink_urb (&serial->read_urb[port]);
1457 serial->active[port] = 0;
1461 static int generic_serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
1463 struct usb_serial *serial = (struct usb_serial *) tty->driver_data;
1464 int port = MINOR(tty->device) - serial->minor;
1466 dbg("generic_serial_write port %d", port);
1468 if (count == 0) {
1469 dbg("write request of 0 bytes");
1470 return (0);
1473 /* only do something if we have a bulk out endpoint */
1474 if (serial->num_bulk_out) {
1475 if (serial->write_urb[port].status == -EINPROGRESS) {
1476 dbg ("already writing");
1477 return (0);
1480 count = (count > serial->bulk_out_size[port]) ? serial->bulk_out_size[port] : count;
1482 if (from_user) {
1483 copy_from_user(serial->write_urb[port].transfer_buffer, buf, count);
1485 else {
1486 memcpy (serial->write_urb[port].transfer_buffer, buf, count);
1489 /* send the data out the bulk port */
1490 serial->write_urb[port].transfer_buffer_length = count;
1492 if (usb_submit_urb(&serial->write_urb[port]))
1493 dbg("usb_submit_urb(write bulk) failed");
1495 return (count);
1498 /* no bulk out, so return 0 bytes written */
1499 return (0);
1503 static int generic_write_room (struct tty_struct *tty)
1505 struct usb_serial *serial = (struct usb_serial *)tty->driver_data;
1506 int port = MINOR(tty->device) - serial->minor;
1507 int room;
1509 dbg("generic_write_room port %d", port);
1511 if (serial->num_bulk_out) {
1512 if (serial->write_urb[port].status == -EINPROGRESS)
1513 room = 0;
1514 else
1515 room = serial->bulk_out_size[port];
1516 dbg("generic_write_room returns %d", room);
1517 return (room);
1520 return (0);
1524 static int generic_chars_in_buffer (struct tty_struct *tty)
1526 struct usb_serial *serial = (struct usb_serial *)tty->driver_data;
1527 int port = MINOR(tty->device) - serial->minor;
1529 dbg("generic_chars_in_buffer port %d", port);
1531 if (serial->num_bulk_out) {
1532 if (serial->write_urb[port].status == -EINPROGRESS) {
1533 return (serial->bulk_out_size[port]);
1537 return (0);
1541 static void generic_throttle (struct tty_struct *tty)
1543 /* do nothing for the generic device */
1544 return;
1548 static void generic_unthrottle (struct tty_struct *tty)
1550 /* do nothing for the generic device */
1551 return;
1555 static int generic_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
1557 /* generic driver doesn't support any ioctls yet */
1558 return -ENOIOCTLCMD;
1562 static void generic_set_termios (struct tty_struct *tty, struct termios * old)
1564 /* generic driver doesn't really care about setting any line settings */
1565 return;
1569 static void generic_read_bulk_callback (struct urb *urb)
1571 struct usb_serial *serial = (struct usb_serial *)urb->context;
1572 struct tty_struct *tty = serial->tty;
1573 unsigned char *data = urb->transfer_buffer;
1574 int i;
1576 dbg("generic_read_bulk_callback");
1578 if (urb->status) {
1579 dbg("nonzero read bulk status received: %d", urb->status);
1580 return;
1583 #ifdef DEBUG
1584 if (urb->actual_length) {
1585 printk (KERN_DEBUG __FILE__ ": data read - length = %d, data = ", urb->actual_length);
1586 for (i = 0; i < urb->actual_length; ++i) {
1587 printk ("%.2x ", data[i]);
1589 printk ("\n");
1591 #endif
1593 if (urb->actual_length) {
1594 for (i = 0; i < urb->actual_length ; ++i) {
1595 tty_insert_flip_char(tty, data[i], 0);
1597 tty_flip_buffer_push(tty);
1600 /* Continue trying to always read */
1601 if (usb_submit_urb(urb))
1602 dbg("failed resubmitting read urb");
1604 return;
1608 static void generic_write_bulk_callback (struct urb *urb)
1610 struct usb_serial *serial = (struct usb_serial *) urb->context;
1611 struct tty_struct *tty = serial->tty;
1613 dbg("generic_write_bulk_callback");
1615 if (urb->status) {
1616 dbg("nonzero write bulk status received: %d", urb->status);
1617 return;
1620 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1621 (tty->ldisc.write_wakeup)(tty);
1623 wake_up_interruptible(&tty->write_wait);
1625 return;
1630 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum)
1632 struct usb_serial *serial = NULL;
1633 struct usb_interface_descriptor *interface;
1634 struct usb_endpoint_descriptor *endpoint;
1635 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
1636 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
1637 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
1638 struct usb_serial_device_type *type;
1639 int device_num;
1640 int minor;
1641 int buffer_size;
1642 int i;
1643 char interrupt_pipe;
1644 char bulk_in_pipe;
1645 char bulk_out_pipe;
1646 int num_interrupt_in = 0;
1647 int num_bulk_in = 0;
1648 int num_bulk_out = 0;
1649 int num_ports;
1651 /* loop through our list of known serial converters, and see if this device matches */
1652 device_num = 0;
1653 while (usb_serial_devices[device_num] != NULL) {
1654 type = usb_serial_devices[device_num];
1655 dbg ("Looking at %s Vendor id=%.4x Product id=%.4x", type->name, *(type->idVendor), *(type->idProduct));
1657 /* look at the device descriptor */
1658 if ((dev->descriptor.idVendor == *(type->idVendor)) &&
1659 (dev->descriptor.idProduct == *(type->idProduct))) {
1661 dbg("descriptor matches...looking at the endpoints");
1663 /* descriptor matches, let's try to find the endpoints needed */
1664 interrupt_pipe = bulk_in_pipe = bulk_out_pipe = HAS_NOT;
1666 /* check out the endpoints */
1667 interface = &dev->actconfig->interface[ifnum].altsetting[0];
1668 for (i = 0; i < interface->bNumEndpoints; ++i) {
1669 endpoint = &interface->endpoint[i];
1671 if ((endpoint->bEndpointAddress & 0x80) &&
1672 ((endpoint->bmAttributes & 3) == 0x02)) {
1673 /* we found a bulk in endpoint */
1674 dbg("found bulk in");
1675 bulk_in_pipe = HAS;
1676 bulk_in_endpoint[num_bulk_in] = endpoint;
1677 ++num_bulk_in;
1680 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1681 ((endpoint->bmAttributes & 3) == 0x02)) {
1682 /* we found a bulk out endpoint */
1683 dbg("found bulk out");
1684 bulk_out_pipe = HAS;
1685 bulk_out_endpoint[num_bulk_out] = endpoint;
1686 ++num_bulk_out;
1689 if ((endpoint->bEndpointAddress & 0x80) &&
1690 ((endpoint->bmAttributes & 3) == 0x03)) {
1691 /* we found a interrupt in endpoint */
1692 dbg("found interrupt in");
1693 interrupt_pipe = HAS;
1694 interrupt_in_endpoint[num_interrupt_in] = endpoint;
1695 ++num_interrupt_in;
1700 /* verify that we found all of the endpoints that we need */
1701 if ((interrupt_pipe & type->needs_interrupt_in) &&
1702 (bulk_in_pipe & type->needs_bulk_in) &&
1703 (bulk_out_pipe & type->needs_bulk_out)) {
1704 /* found all that we need */
1705 info("%s converter detected", type->name);
1707 #ifdef CONFIG_USB_SERIAL_GENERIC
1708 if (type == &generic_device)
1709 num_ports = num_bulk_out;
1710 else
1711 #endif
1712 num_ports = type->num_ports;
1714 serial = get_free_serial (num_ports, &minor);
1715 if (serial == NULL) {
1716 err("No more free serial devices");
1717 return NULL;
1720 serial->dev = dev;
1721 serial->type = type;
1722 serial->minor = minor;
1723 serial->num_ports = num_ports;
1724 serial->num_bulk_in = num_bulk_in;
1725 serial->num_bulk_out = num_bulk_out;
1726 serial->num_interrupt_in = num_interrupt_in;
1728 /* if this device type has a startup function, call it */
1729 if (type->startup) {
1730 if (type->startup (serial)) {
1731 return_serial (serial);
1732 return NULL;
1736 /* set up the endpoint information */
1737 for (i = 0; i < num_bulk_in; ++i) {
1738 buffer_size = bulk_in_endpoint[i]->wMaxPacketSize;
1739 serial->bulk_in_buffer[i] = kmalloc (buffer_size, GFP_KERNEL);
1740 if (!serial->bulk_in_buffer[i]) {
1741 err("Couldn't allocate bulk_in_buffer");
1742 goto probe_error;
1744 if (serial->type->read_bulk_callback) {
1745 FILL_BULK_URB(&serial->read_urb[i], dev, usb_rcvbulkpipe (dev, bulk_in_endpoint[i]->bEndpointAddress),
1746 serial->bulk_in_buffer[i], buffer_size, serial->type->read_bulk_callback, serial);
1747 } else {
1748 FILL_BULK_URB(&serial->read_urb[i], dev, usb_rcvbulkpipe (dev, bulk_in_endpoint[i]->bEndpointAddress),
1749 serial->bulk_in_buffer[i], buffer_size, generic_read_bulk_callback, serial);
1753 for (i = 0; i < num_bulk_out; ++i) {
1754 serial->bulk_out_size[i] = bulk_out_endpoint[i]->wMaxPacketSize;
1755 serial->bulk_out_buffer[i] = kmalloc (serial->bulk_out_size[i], GFP_KERNEL);
1756 if (!serial->bulk_out_buffer[i]) {
1757 err("Couldn't allocate bulk_out_buffer");
1758 goto probe_error;
1760 if (serial->type->write_bulk_callback) {
1761 FILL_BULK_URB(&serial->write_urb[i], dev, usb_sndbulkpipe (dev, bulk_out_endpoint[i]->bEndpointAddress),
1762 serial->bulk_out_buffer[i], serial->bulk_out_size[i], serial->type->write_bulk_callback, serial);
1763 } else {
1764 FILL_BULK_URB(&serial->write_urb[i], dev, usb_sndbulkpipe (dev, bulk_out_endpoint[i]->bEndpointAddress),
1765 serial->bulk_out_buffer[i], serial->bulk_out_size[i], generic_write_bulk_callback, serial);
1769 #if 0 /* use this code when WhiteHEAT is up and running */
1770 for (i = 0; i < num_interrupt_in; ++i) {
1771 buffer_size = interrupt_in_endpoint[i]->wMaxPacketSize;
1772 serial->interrupt_in_buffer[i] = kmalloc (buffer_size, GFP_KERNEL);
1773 if (!serial->interrupt_in_buffer[i]) {
1774 err("Couldn't allocate interrupt_in_buffer");
1775 goto probe_error;
1777 FILL_INT_URB(&serial->control_urb[i], dev, usb_rcvintpipe (dev, interrupt_in_endpoint[i]->bEndpointAddress),
1778 serial->interrupt_in_buffer[i], buffer_size, serial_control_irq,
1779 serial, interrupt_in_endpoint[i]->bInterval);
1781 #endif
1783 for (i = 0; i < serial->num_ports; ++i) {
1784 info("%s converter now attached to ttyUSB%d", type->name, serial->minor + i);
1787 MOD_INC_USE_COUNT;
1789 return serial;
1790 } else {
1791 info("descriptors matched, but endpoints did not");
1795 /* look at the next type in our list */
1796 ++device_num;
1799 probe_error:
1800 if (serial) {
1801 for (i = 0; i < num_bulk_in; ++i)
1802 if (serial->bulk_in_buffer[i])
1803 kfree (serial->bulk_in_buffer[i]);
1804 for (i = 0; i < num_bulk_out; ++i)
1805 if (serial->bulk_out_buffer[i])
1806 kfree (serial->bulk_out_buffer[i]);
1807 for (i = 0; i < num_interrupt_in; ++i)
1808 if (serial->interrupt_in_buffer[i])
1809 kfree (serial->interrupt_in_buffer[i]);
1811 return NULL;
1815 static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
1817 struct usb_serial *serial = (struct usb_serial *) ptr;
1818 int i;
1820 if (serial) {
1821 /* need to stop any transfers...*/
1822 for (i = 0; i < serial->num_ports; ++i) {
1823 usb_unlink_urb (&serial->write_urb[i]);
1824 usb_unlink_urb (&serial->read_urb[i]);
1825 serial->active[i] = 0;
1828 /* free up any memory that we allocated */
1829 for (i = 0; i < serial->num_bulk_in; ++i)
1830 if (serial->bulk_in_buffer[i])
1831 kfree (serial->bulk_in_buffer[i]);
1832 for (i = 0; i < serial->num_bulk_out; ++i)
1833 if (serial->bulk_out_buffer[i])
1834 kfree (serial->bulk_out_buffer[i]);
1835 for (i = 0; i < serial->num_interrupt_in; ++i)
1836 if (serial->interrupt_in_buffer[i])
1837 kfree (serial->interrupt_in_buffer[i]);
1839 for (i = 0; i < serial->num_ports; ++i) {
1840 info("%s converter now disconnected from ttyUSB%d", serial->type->name, serial->minor + i);
1843 /* return the minor range that this device had */
1844 return_serial (serial);
1846 /* free up any memory that we allocated */
1847 kfree (serial);
1849 } else {
1850 info("device disconnected");
1853 MOD_DEC_USE_COUNT;
1857 static struct tty_driver serial_tty_driver = {
1858 magic: TTY_DRIVER_MAGIC,
1859 driver_name: "usb",
1860 name: "ttyUSB",
1861 major: SERIAL_TTY_MAJOR,
1862 minor_start: 0,
1863 num: SERIAL_TTY_MINORS,
1864 type: TTY_DRIVER_TYPE_SERIAL,
1865 subtype: SERIAL_TYPE_NORMAL,
1866 flags: TTY_DRIVER_REAL_RAW,
1867 refcount: &serial_refcount,
1868 table: serial_tty,
1869 proc_entry: NULL,
1870 other: NULL,
1871 termios: serial_termios,
1872 termios_locked: serial_termios_locked,
1874 open: serial_open,
1875 close: serial_close,
1876 write: serial_write,
1877 put_char: NULL,
1878 flush_chars: NULL,
1879 write_room: serial_write_room,
1880 ioctl: serial_ioctl,
1881 set_termios: serial_set_termios,
1882 set_ldisc: NULL,
1883 throttle: serial_throttle,
1884 unthrottle: serial_unthrottle,
1885 stop: NULL,
1886 start: NULL,
1887 hangup: NULL,
1888 break_ctl: NULL,
1889 wait_until_sent: NULL,
1890 send_xchar: NULL,
1891 read_proc: NULL,
1892 chars_in_buffer: serial_chars_in_buffer,
1893 flush_buffer: NULL
1897 int usb_serial_init(void)
1899 int i;
1901 /* Initalize our global data */
1902 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1903 serial_table[i] = NULL;
1906 /* register the tty driver */
1907 serial_tty_driver.init_termios = tty_std_termios;
1908 serial_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1909 if (tty_register_driver (&serial_tty_driver)) {
1910 err("failed to register tty driver");
1911 return -EPERM;
1914 /* register the USB driver */
1915 if (usb_register(&usb_serial_driver) < 0) {
1916 tty_unregister_driver(&serial_tty_driver);
1917 return -1;
1920 info("support registered");
1921 return 0;
1925 void usb_serial_exit(void)
1927 tty_unregister_driver(&serial_tty_driver);
1928 usb_deregister(&usb_serial_driver);
1932 module_init(usb_serial_init);
1933 module_exit(usb_serial_exit);