Import 2.3.35pre2
[davej-history.git] / drivers / usb / usb-serial.c
blob2ee72a057a77fb7d85fbd371df1dbfa7b216dba9
1 /*
2 * USB Serial Converter driver
4 * (C) Copyright (C) 1999
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 README.serial for more information on using this driver.
17 * version 0.2.3 (12/21/99) gkh
18 * Added initial support for the Connect Tech WhiteHEAT converter.
19 * Incremented the number of ports in expectation of getting the
20 * WhiteHEAT to work properly (4 ports per connection).
21 * Added notification on insertion and removal of what port the
22 * device is/was connected to (and what kind of device it was).
24 * version 0.2.2 (12/16/99) gkh
25 * Changed major number to the new allocated number. We're legal now!
27 * version 0.2.1 (12/14/99) gkh
28 * Fixed bug that happens when device node is opened when there isn't a
29 * device attached to it. Thanks to marek@webdesign.no for noticing this.
31 * version 0.2.0 (11/10/99) gkh
32 * Split up internals to make it easier to add different types of serial
33 * converters to the code.
34 * Added a "generic" driver that gets it's vendor and product id
35 * from when the module is loaded. Thanks to David E. Nelson (dnelson@jump.net)
36 * for the idea and sample code (from the usb scanner driver.)
37 * Cleared up any licensing questions by releasing it under the GNU GPL.
39 * version 0.1.2 (10/25/99) gkh
40 * Fixed bug in detecting device.
42 * version 0.1.1 (10/05/99) gkh
43 * Changed the major number to not conflict with anything else.
45 * version 0.1 (09/28/99) gkh
46 * Can recognize the two different devices and start up a read from
47 * device when asked to. Writes also work. No control signals yet, this
48 * all is vendor specific data (i.e. no spec), also no control for
49 * different baud rates or other bit settings.
50 * Currently we are using the same devid as the acm driver. This needs
51 * to change.
55 #include <linux/kernel.h>
56 #include <linux/sched.h>
57 #include <linux/signal.h>
58 #include <linux/errno.h>
59 #include <linux/poll.h>
60 #include <linux/init.h>
61 #include <linux/malloc.h>
62 #include <linux/fcntl.h>
63 #include <linux/tty_driver.h>
64 #include <linux/tty_flip.h>
65 #include <linux/tty.h>
66 #include <linux/module.h>
67 #include <linux/spinlock.h>
69 #include "usb.h"
71 /*#define SERIAL_DEBUG 1*/
73 #ifdef SERIAL_DEBUG
74 #define debug_info(message); printk(message);
75 #else
76 #define debug_info(message);
77 #endif
80 /* Module information */
81 MODULE_AUTHOR("Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux-usb/");
82 MODULE_DESCRIPTION("USB Serial Driver");
84 static __u16 vendor = 0;
85 static __u16 product = 0;
86 MODULE_PARM(vendor, "i");
87 MODULE_PARM_DESC(vendor, "User specified USB idVendor");
89 MODULE_PARM(product, "i");
90 MODULE_PARM_DESC(product, "User specified USB idProduct");
93 /* USB Serial devices vendor ids and device ids that this driver supports */
94 #define BELKIN_VENDOR_ID 0x056c
95 #define BELKIN_SERIAL_CONVERTER 0x8007
96 #define PERACOM_VENDOR_ID 0x0565
97 #define PERACOM_SERIAL_CONVERTER 0x0001
98 #define CONNECT_TECH_VENDOR_ID 0x0710
99 #define CONNECT_TECH_WHITE_HEAT_ID 0x0001
102 #define SERIAL_MAJOR 188 /* Nice legal number now */
103 #define NUM_PORTS 16 /* Actually we are allowed 255, but this is good for now */
106 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum);
107 static void usb_serial_disconnect(struct usb_device *dev, void *ptr);
110 #define MUST_HAVE_NOT 0x01
111 #define MUST_HAVE 0x02
112 #define DONT_CARE 0x03
114 #define HAS 0x02
115 #define HAS_NOT 0x01
118 /* local function prototypes */
119 static int serial_open (struct tty_struct *tty, struct file * filp);
120 static void serial_close (struct tty_struct *tty, struct file * filp);
121 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count);
122 static void serial_put_char (struct tty_struct *tty, unsigned char ch);
123 static int serial_write_room (struct tty_struct *tty);
124 static int serial_chars_in_buffer (struct tty_struct *tty);
125 static void serial_throttle (struct tty_struct * tty);
126 static void serial_unthrottle (struct tty_struct * tty);
129 /* This structure defines the individual serial converter. */
130 struct usb_serial_device_type {
131 char *name;
132 __u16 *idVendor;
133 __u16 *idProduct;
134 char needs_interrupt_in;
135 char needs_bulk_in;
136 char needs_bulk_out;
137 // add function calls
139 int (*open)(struct tty_struct * tty, struct file * filp);
140 void (*close)(struct tty_struct * tty, struct file * filp);
141 int (*write)(struct tty_struct * tty, int from_user,const unsigned char *buf, int count);
142 void (*put_char)(struct tty_struct *tty, unsigned char ch);
143 int (*write_room)(struct tty_struct *tty);
144 int (*chars_in_buffer)(struct tty_struct *tty);
145 void (*throttle)(struct tty_struct * tty);
146 void (*unthrottle)(struct tty_struct * tty);
150 /* function prototypes for the eTek type converters (this included Belkin and Peracom) */
151 static int etek_serial_open (struct tty_struct *tty, struct file *filp);
152 static void etek_serial_close (struct tty_struct *tty, struct file *filp);
153 static int etek_serial_write (struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
154 static void etek_serial_put_char (struct tty_struct *tty, unsigned char ch);
155 static int etek_write_room (struct tty_struct *tty);
156 static int etek_chars_in_buffer (struct tty_struct *tty);
158 /* All of the device info needed for the Belkin Serial Converter */
159 static __u16 belkin_vendor_id = BELKIN_VENDOR_ID;
160 static __u16 belkin_product_id = BELKIN_SERIAL_CONVERTER;
161 static struct usb_serial_device_type belkin_device = {
162 "Belkin",
163 &belkin_vendor_id, /* the Belkin vendor id */
164 &belkin_product_id, /* the Belkin serial converter product id */
165 MUST_HAVE, /* this device must have an interrupt in endpoint */
166 MUST_HAVE, /* this device must have a bulk in endpoint */
167 MUST_HAVE, /* this device must have a bulk out endpoint */
168 etek_serial_open,
169 etek_serial_close,
170 etek_serial_write,
171 etek_serial_put_char,
172 etek_write_room,
173 etek_chars_in_buffer,
174 NULL,
175 NULL
178 /* All of the device info needed for the Peracom Serial Converter */
179 static __u16 peracom_vendor_id = PERACOM_VENDOR_ID;
180 static __u16 peracom_product_id = PERACOM_SERIAL_CONVERTER;
181 static struct usb_serial_device_type peracom_device = {
182 "Peracom",
183 &peracom_vendor_id, /* the Peracom vendor id */
184 &peracom_product_id, /* the Peracom serial converter product id */
185 MUST_HAVE, /* this device must have an interrupt in endpoint */
186 MUST_HAVE, /* this device must have a bulk in endpoint */
187 MUST_HAVE, /* this device must have a bulk out endpoint */
188 etek_serial_open,
189 etek_serial_close,
190 etek_serial_write,
191 etek_serial_put_char,
192 etek_write_room,
193 etek_chars_in_buffer,
194 NULL,
195 NULL
199 /* function prototypes for the Connect Tech WhiteHEAT serial converter */
200 static int whiteheat_serial_open (struct tty_struct *tty, struct file *filp);
201 static void whiteheat_serial_close (struct tty_struct *tty, struct file *filp);
202 static int whiteheat_serial_write (struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
203 static void whiteheat_serial_put_char (struct tty_struct *tty, unsigned char ch);
204 static int whiteheat_write_room (struct tty_struct *tty);
205 static int whiteheat_chars_in_buffer (struct tty_struct *tty);
206 static void whiteheat_throttle (struct tty_struct *tty);
207 static void whiteheat_unthrottle (struct tty_struct *tty);
209 /* All of the device info needed for the Connect Tech WhiteHEAT */
210 static __u16 connecttech_vendor_id = CONNECT_TECH_VENDOR_ID;
211 static __u16 connecttech_whiteheat_product_id = CONNECT_TECH_WHITE_HEAT_ID;
212 static struct usb_serial_device_type whiteheat_device = {
213 "Connect Tech - WhiteHEAT",
214 &connecttech_vendor_id, /* the Connect Tech vendor id */
215 &connecttech_whiteheat_product_id, /* the White Heat product id */
216 DONT_CARE, /* don't have to have an interrupt in endpoint */
217 DONT_CARE, /* don't have to have a bulk in endpoint */
218 DONT_CARE, /* don't have to have a bulk out endpoint */
219 whiteheat_serial_open,
220 whiteheat_serial_close,
221 whiteheat_serial_write,
222 whiteheat_serial_put_char,
223 whiteheat_write_room,
224 whiteheat_chars_in_buffer,
225 whiteheat_throttle,
226 whiteheat_unthrottle
230 /* function prototypes for a "generic" type serial converter (no flow control, not all endpoints needed) */
231 static int generic_serial_open (struct tty_struct *tty, struct file *filp);
232 static void generic_serial_close (struct tty_struct *tty, struct file *filp);
233 static int generic_serial_write (struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
234 static void generic_serial_put_char (struct tty_struct *tty, unsigned char ch);
235 static int generic_write_room (struct tty_struct *tty);
236 static int generic_chars_in_buffer (struct tty_struct *tty);
238 /* All of the device info needed for the Generic Serial Converter */
239 static struct usb_serial_device_type generic_device = {
240 "Generic",
241 &vendor, /* use the user specified vendor id */
242 &product, /* use the user specified product id */
243 DONT_CARE, /* don't have to have an interrupt in endpoint */
244 DONT_CARE, /* don't have to have a bulk in endpoint */
245 DONT_CARE, /* don't have to have a bulk out endpoint */
246 generic_serial_open,
247 generic_serial_close,
248 generic_serial_write,
249 generic_serial_put_char,
250 generic_write_room,
251 generic_chars_in_buffer,
252 NULL, /* generic driver does not implement any flow control */
253 NULL /* generic driver does not implement any flow control */
257 /* To add support for another serial converter, create a usb_serial_device_type
258 structure for that device, and add it to this list, making sure that the last
259 entry is NULL. */
260 static struct usb_serial_device_type *usb_serial_devices[] = {
261 &generic_device,
262 &whiteheat_device,
263 &belkin_device,
264 &peracom_device,
265 NULL
270 struct usb_serial_state {
271 struct usb_device * dev;
272 struct usb_serial_device_type * type;
273 void * irq_handle;
274 unsigned int irqpipe;
275 struct tty_struct * tty; /* the coresponding tty for this device */
276 unsigned char number;
277 char present;
278 char active;
280 char has_interrupt_in; /* if this device has an interrupt in pipe or not */
281 char interrupt_in_inuse; /* if the interrupt in endpoint is in use */
282 __u8 interrupt_in_endpoint;
283 __u8 interrupt_in_interval;
284 __u16 interrupt_in_size; /* the size of the interrupt in endpoint */
285 unsigned int interrupt_in_pipe;
286 unsigned char * interrupt_in_buffer;
287 void * interrupt_in_transfer;
289 char has_bulk_in; /* if thie device has a bulk in pipe or not */
290 char bulk_in_inuse; /* if the bulk in endpoint is in use */
291 __u8 bulk_in_endpoint;
292 __u8 bulk_in_interval;
293 __u16 bulk_in_size; /* the size of the bulk in endpoint */
294 unsigned int bulk_in_pipe;
295 unsigned char * bulk_in_buffer;
296 void * bulk_in_transfer;
298 char has_bulk_out; /* if this device has a bulk out pipe or not */
299 char bulk_out_inuse; /* if the bulk out endpoint is in use */
300 __u8 bulk_out_endpoint;
301 __u8 bulk_out_interval;
302 __u16 bulk_out_size; /* the size of the bulk out endpoint */
303 unsigned int bulk_out_pipe;
304 unsigned char * bulk_out_buffer;
305 void * bulk_out_transfer;
308 static struct usb_driver usb_serial_driver = {
309 "serial",
310 usb_serial_probe,
311 usb_serial_disconnect,
312 { NULL, NULL }
315 static int serial_refcount;
316 static struct tty_driver serial_tty_driver;
317 static struct tty_struct * serial_tty[NUM_PORTS];
318 static struct termios * serial_termios[NUM_PORTS];
319 static struct termios * serial_termios_locked[NUM_PORTS];
320 static struct usb_serial_state serial_state_table[NUM_PORTS];
324 static int serial_read_irq (int state, void *buffer, int count, void *dev_id)
326 struct usb_serial_state *serial = (struct usb_serial_state *)dev_id;
327 struct tty_struct *tty = serial->tty;
328 unsigned char* data = buffer;
329 int i;
331 debug_info("USB Serial: serial_read_irq\n");
333 #ifdef SERIAL_DEBUG
334 if (count) {
335 printk("%d %s\n", count, data);
337 #endif
339 if (count) {
340 for (i=0;i<count;i++) {
341 tty_insert_flip_char(tty,data[i],0);
343 tty_flip_buffer_push(tty);
346 /* Continue transfer */
347 /* return (1); */
349 /* No more transfer, let the irq schedule us again */
350 serial->bulk_in_inuse = 0;
351 return (0);
355 static int serial_write_irq (int state, void *buffer, int count, void *dev_id)
357 struct usb_serial_state *serial = (struct usb_serial_state *) dev_id;
358 struct tty_struct *tty = serial->tty;
360 debug_info("USB Serial: serial_write_irq\n");
362 if (!serial->bulk_out_inuse) {
363 debug_info("USB Serial: write irq for a finished pipe?\n");
364 return (0);
367 usb_terminate_bulk (serial->dev, serial->bulk_out_transfer);
368 serial->bulk_out_inuse = 0;
370 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
371 (tty->ldisc.write_wakeup)(tty);
373 wake_up_interruptible(&tty->write_wait);
375 return 0;
379 #if 0
380 /* we will need this soon... removed for now to keep the compile warnings down */
381 static int usb_serial_irq (int state, void *buffer, int len, void *dev_id)
383 struct usb_serial_state *serial = (struct usb_serial_state *) dev_id;
385 debug_info("USB Serial: usb_serial_irq\n");
387 /* ask for a bulk read */
388 // serial->bulk_in_inuse = 1;
389 // serial->bulk_in_transfer = usb_request_bulk (serial->dev, serial->bulk_in_pipe, serial_read_irq, serial->bulk_in_buffer, serial->bulk_in_size, serial);
391 return (1);
393 #endif
397 /*****************************************************************************
398 * Driver tty interface functions
399 *****************************************************************************/
400 static int serial_open (struct tty_struct *tty, struct file * filp)
402 struct usb_serial_state *serial;
404 debug_info("USB Serial: serial_open\n");
406 /* assign a serial object to the tty pointer */
407 serial = &serial_state_table [MINOR(tty->device)-tty->driver.minor_start];
409 /* do some sanity checking that we really have a device present */
410 if (!serial) {
411 debug_info("USB Serial: serial == NULL!\n");
412 return (-ENODEV);
414 if (!serial->type) {
415 debug_info("USB Serial: serial->type == NULL!\n");
416 return (-ENODEV);
419 /* make the tty driver remember our serial object, and us it */
420 tty->driver_data = serial;
421 serial->tty = tty;
423 /* pass on to the driver specific version of this function */
424 if (serial->type->open) {
425 return (serial->type->open(tty, filp));
428 return (0);
432 static void serial_close(struct tty_struct *tty, struct file * filp)
434 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
435 debug_info("USB Serial: serial_close\n");
437 /* do some sanity checking that we really have a device present */
438 if (!serial) {
439 debug_info("USB Serial: serial == NULL!\n");
440 return;
442 if (!serial->type) {
443 debug_info("USB Serial: serial->type == NULL!\n");
444 return;
446 if (!serial->present) {
447 debug_info("USB Serial: no device registered\n");
448 return;
450 if (!serial->active) {
451 debug_info ("USB Serial: device already open\n");
452 return;
455 /* pass on to the driver specific version of this function */
456 if (serial->type->close) {
457 serial->type->close(tty, filp);
462 static int serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
464 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
466 debug_info("USB Serial: serial_write\n");
468 /* do some sanity checking that we really have a device present */
469 if (!serial) {
470 debug_info("USB Serial: serial == NULL!\n");
471 return (-ENODEV);
473 if (!serial->type) {
474 debug_info("USB Serial: serial->type == NULL!\n");
475 return (-ENODEV);
477 if (!serial->present) {
478 debug_info("USB Serial: device not registered\n");
479 return (-EINVAL);
481 if (!serial->active) {
482 debug_info ("USB Serial: device not opened\n");
483 return (-EINVAL);
486 /* pass on to the driver specific version of this function */
487 if (serial->type->write) {
488 return (serial->type->write(tty, from_user, buf, count));
491 /* no specific driver, so return that we didn't write anything */
492 return (0);
496 static void serial_put_char (struct tty_struct *tty, unsigned char ch)
498 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
500 debug_info("USB Serial: serial_put_char\n");
502 /* do some sanity checking that we really have a device present */
503 if (!serial) {
504 debug_info("USB Serial: serial == NULL!\n");
505 return;
507 if (!serial->type) {
508 debug_info("USB Serial: serial->type == NULL!\n");
509 return;
511 if (!serial->present) {
512 debug_info("USB Serial: no device registered\n");
513 return;
515 if (!serial->active) {
516 debug_info ("USB Serial: device not open\n");
517 return;
520 /* pass on to the driver specific version of this function */
521 if (serial->type->put_char) {
522 serial->type->put_char(tty, ch);
525 return;
529 static int serial_write_room (struct tty_struct *tty)
531 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
533 debug_info("USB Serial: serial_write_room\n");
535 /* do some sanity checking that we really have a device present */
536 if (!serial) {
537 debug_info("USB Serial: serial == NULL!\n");
538 return (-ENODEV);
540 if (!serial->type) {
541 debug_info("USB Serial: serial->type == NULL!\n");
542 return (-ENODEV);
544 if (!serial->present) {
545 debug_info("USB Serial: no device registered\n");
546 return (-EINVAL);
548 if (!serial->active) {
549 debug_info ("USB Serial: device not open\n");
550 return (-EINVAL);
553 /* pass on to the driver specific version of this function */
554 if (serial->type->write_room) {
555 return (serial->type->write_room(tty));
558 return (0);
562 static int serial_chars_in_buffer (struct tty_struct *tty)
564 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
566 debug_info("USB Serial: serial_chars_in_buffer\n");
568 /* do some sanity checking that we really have a device present */
569 if (!serial) {
570 debug_info("USB Serial: serial == NULL!\n");
571 return (-ENODEV);
573 if (!serial->type) {
574 debug_info("USB Serial: serial->type == NULL!\n");
575 return (-ENODEV);
577 if (!serial->present) {
578 debug_info("USB Serial: no device registered\n");
579 return (-EINVAL);
581 if (!serial->active) {
582 debug_info ("USB Serial: device not open\n");
583 return (-EINVAL);
586 /* pass on to the driver specific version of this function */
587 if (serial->type->chars_in_buffer) {
588 return (serial->type->chars_in_buffer(tty));
591 return (0);
595 static void serial_throttle (struct tty_struct * tty)
597 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
599 debug_info("USB Serial: serial_throttle\n");
601 /* do some sanity checking that we really have a device present */
602 if (!serial) {
603 debug_info("USB Serial: serial == NULL!\n");
604 return;
606 if (!serial->type) {
607 debug_info("USB Serial: serial->type == NULL!\n");
608 return;
610 if (!serial->present) {
611 debug_info("USB Serial: no device registered\n");
612 return;
614 if (!serial->active) {
615 debug_info ("USB Serial: device not open\n");
616 return;
619 /* pass on to the driver specific version of this function */
620 if (serial->type->throttle) {
621 serial->type->throttle(tty);
624 return;
628 static void serial_unthrottle (struct tty_struct * tty)
630 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
632 debug_info("USB Serial: serial_unthrottle\n");
634 /* do some sanity checking that we really have a device present */
635 if (!serial) {
636 debug_info("USB Serial: serial == NULL!\n");
637 return;
639 if (!serial->type) {
640 debug_info("USB Serial: serial->type == NULL!\n");
641 return;
643 if (!serial->present) {
644 debug_info("USB Serial: no device registered\n");
645 return;
647 if (!serial->active) {
648 debug_info ("USB Serial: device not open\n");
649 return;
653 /* pass on to the driver specific version of this function */
654 if (serial->type->unthrottle) {
655 serial->type->unthrottle(tty);
658 return;
662 /*****************************************************************************
663 * eTek specific driver functions
664 *****************************************************************************/
665 static int etek_serial_open (struct tty_struct *tty, struct file *filp)
667 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
669 debug_info("USB Serial: etek_serial_open\n");
671 if (!serial->present) {
672 debug_info("USB Serial: no device registered\n");
673 return -EINVAL;
676 if (serial->active) {
677 debug_info ("USB Serial: device already open\n");
678 return -EINVAL;
680 serial->active = 1;
682 /*Start reading from the device*/
683 serial->bulk_in_inuse = 1;
684 serial->bulk_in_transfer = usb_request_bulk (serial->dev, serial->bulk_in_pipe, serial_read_irq, serial->bulk_in_buffer, serial->bulk_in_size, serial);
686 /* Need to do device specific setup here (control lines, baud rate, etc.) */
687 /* FIXME!!! */
689 return (0);
693 static void etek_serial_close(struct tty_struct *tty, struct file * filp)
695 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
696 debug_info("USB Serial: etek_serial_close\n");
698 /* Need to change the control lines here */
699 /* FIXME */
701 /* shutdown our bulk reads and writes */
702 if (serial->bulk_out_inuse){
703 usb_terminate_bulk (serial->dev, serial->bulk_out_transfer);
704 serial->bulk_out_inuse = 0;
706 if (serial->bulk_in_inuse){
707 usb_terminate_bulk (serial->dev, serial->bulk_in_transfer);
708 serial->bulk_in_inuse = 0;
711 /* release the irq? */
713 serial->active = 0;
717 static int etek_serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
719 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
720 int written;
722 debug_info("USB Serial: etek_serial_write\n");
724 if (serial->bulk_out_inuse) {
725 debug_info ("USB Serial: already writing\n");
726 return (0);
729 written = (count > serial->bulk_out_size) ? serial->bulk_out_size : count;
731 if (from_user) {
732 copy_from_user(serial->bulk_out_buffer, buf, written);
734 else {
735 memcpy (serial->bulk_out_buffer, buf, written);
738 /* send the data out the bulk port */
739 serial->bulk_out_inuse = 1;
740 serial->bulk_out_transfer = usb_request_bulk (serial->dev, serial->bulk_out_pipe, serial_write_irq, serial->bulk_out_buffer, written, serial);
742 return (written);
746 static void etek_serial_put_char (struct tty_struct *tty, unsigned char ch)
748 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
750 debug_info("USB Serial: etek_serial_put_char\n");
752 if (serial->bulk_out_inuse) {
753 debug_info ("USB Serial: already writing\n");
754 return;
757 /* send the single character out the bulk port */
758 serial->bulk_out_buffer[0] = ch;
759 serial->bulk_out_inuse = 1;
760 serial->bulk_out_transfer = usb_request_bulk (serial->dev, serial->bulk_out_pipe, serial_write_irq, serial->bulk_out_buffer, 1, serial);
762 return;
766 static int etek_write_room (struct tty_struct *tty)
768 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
770 debug_info("USB Serial: etek_write_room\n");
772 if (serial->bulk_out_inuse) {
773 return (0);
776 return (serial->bulk_out_size);
780 static int etek_chars_in_buffer (struct tty_struct *tty)
782 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
784 debug_info("USB Serial: etek_chars_in_buffer\n");
786 if (serial->bulk_out_inuse) {
787 return (serial->bulk_out_size);
790 return (0);
794 /*****************************************************************************
795 * Connect Tech's White Heat specific driver functions
796 *****************************************************************************/
797 static int whiteheat_serial_open (struct tty_struct *tty, struct file *filp)
799 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
801 debug_info("USB Serial: whiteheat_serial_open\n");
803 if (!serial->present) {
804 debug_info("USB Serial: no device registered\n");
805 return -EINVAL;
808 if (serial->active) {
809 debug_info ("USB Serial: device already open\n");
810 return -EINVAL;
812 serial->active = 1;
814 /*Start reading from the device*/
815 serial->bulk_in_inuse = 1;
816 serial->bulk_in_transfer = usb_request_bulk (serial->dev, serial->bulk_in_pipe, serial_read_irq, serial->bulk_in_buffer, serial->bulk_in_size, serial);
818 /* Need to do device specific setup here (control lines, baud rate, etc.) */
819 /* FIXME!!! */
821 return (0);
825 static void whiteheat_serial_close(struct tty_struct *tty, struct file * filp)
827 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
828 debug_info("USB Serial: whiteheat_serial_close\n");
830 /* Need to change the control lines here */
831 /* FIXME */
833 /* shutdown our bulk reads and writes */
834 if (serial->bulk_out_inuse){
835 usb_terminate_bulk (serial->dev, serial->bulk_out_transfer);
836 serial->bulk_out_inuse = 0;
838 if (serial->bulk_in_inuse){
839 usb_terminate_bulk (serial->dev, serial->bulk_in_transfer);
840 serial->bulk_in_inuse = 0;
843 /* release the irq? */
845 serial->active = 0;
849 static int whiteheat_serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
851 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
852 int written;
854 debug_info("USB Serial: whiteheat_serial_write\n");
856 if (serial->bulk_out_inuse) {
857 debug_info ("USB Serial: already writing\n");
858 return (0);
861 written = (count > serial->bulk_out_size) ? serial->bulk_out_size : count;
863 if (from_user) {
864 copy_from_user(serial->bulk_out_buffer, buf, written);
866 else {
867 memcpy (serial->bulk_out_buffer, buf, written);
870 /* send the data out the bulk port */
871 serial->bulk_out_inuse = 1;
872 serial->bulk_out_transfer = usb_request_bulk (serial->dev, serial->bulk_out_pipe, serial_write_irq, serial->bulk_out_buffer, written, serial);
874 return (written);
878 static void whiteheat_serial_put_char (struct tty_struct *tty, unsigned char ch)
880 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
882 debug_info("USB Serial: whiteheat_serial_put_char\n");
884 if (serial->bulk_out_inuse) {
885 debug_info ("USB Serial: already writing\n");
886 return;
889 /* send the single character out the bulk port */
890 serial->bulk_out_buffer[0] = ch;
891 serial->bulk_out_inuse = 1;
892 serial->bulk_out_transfer = usb_request_bulk (serial->dev, serial->bulk_out_pipe, serial_write_irq, serial->bulk_out_buffer, 1, serial);
894 return;
898 static int whiteheat_write_room (struct tty_struct *tty)
900 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
902 debug_info("USB Serial: whiteheat_write_room\n");
904 if (serial->bulk_out_inuse) {
905 return (0);
908 return (serial->bulk_out_size);
912 static int whiteheat_chars_in_buffer (struct tty_struct *tty)
914 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
916 debug_info("USB Serial: whiteheat_chars_in_buffer\n");
918 if (serial->bulk_out_inuse) {
919 return (serial->bulk_out_size);
922 return (0);
926 static void whiteheat_throttle (struct tty_struct * tty)
928 debug_info("USB Serial: whiteheat_throttle\n");
930 /* Change the control signals */
931 /* FIXME!!! */
933 return;
937 static void whiteheat_unthrottle (struct tty_struct * tty)
939 debug_info("USB Serial: whiteheat_unthrottle\n");
941 /* Change the control signals */
942 /* FIXME!!! */
944 return;
948 /*****************************************************************************
949 * generic devices specific driver functions
950 *****************************************************************************/
951 static int generic_serial_open (struct tty_struct *tty, struct file *filp)
953 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
955 debug_info("USB Serial: generic_serial_open\n");
957 if (!serial->present) {
958 debug_info("USB Serial: no device registered\n");
959 return -EINVAL;
962 if (serial->active) {
963 debug_info ("USB Serial: device already open\n");
964 return -EINVAL;
966 serial->active = 1;
968 /* if we have a bulk interrupt, start reading from it */
969 if (serial->has_bulk_in) {
970 /*Start reading from the device*/
971 serial->bulk_in_inuse = 1;
972 serial->bulk_in_transfer = usb_request_bulk (serial->dev, serial->bulk_in_pipe, serial_read_irq, serial->bulk_in_buffer, serial->bulk_in_size, serial);
975 return (0);
979 static void generic_serial_close(struct tty_struct *tty, struct file * filp)
981 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
982 debug_info("USB Serial: generic_serial_close\n");
984 /* shutdown any bulk reads that might be going on */
985 if (serial->bulk_out_inuse){
986 usb_terminate_bulk (serial->dev, serial->bulk_out_transfer);
987 serial->bulk_out_inuse = 0;
989 if (serial->bulk_in_inuse){
990 usb_terminate_bulk (serial->dev, serial->bulk_in_transfer);
991 serial->bulk_in_inuse = 0;
994 serial->active = 0;
998 static int generic_serial_write (struct tty_struct * tty, int from_user, const unsigned char *buf, int count)
1000 struct usb_serial_state *serial = (struct usb_serial_state *) tty->driver_data;
1001 int written;
1003 debug_info("USB Serial: generic_serial_write\n");
1005 /* only do something if we have a bulk out endpoint */
1006 if (serial->has_bulk_out) {
1007 if (serial->bulk_out_inuse) {
1008 debug_info ("USB Serial: already writing\n");
1009 return (0);
1012 written = (count > serial->bulk_out_size) ? serial->bulk_out_size : count;
1014 if (from_user) {
1015 copy_from_user(serial->bulk_out_buffer, buf, written);
1017 else {
1018 memcpy (serial->bulk_out_buffer, buf, written);
1021 /* send the data out the bulk port */
1022 serial->bulk_out_inuse = 1;
1023 serial->bulk_out_transfer = usb_request_bulk (serial->dev, serial->bulk_out_pipe, serial_write_irq, serial->bulk_out_buffer, written, serial);
1025 return (written);
1028 /* no bulk out, so return 0 bytes written */
1029 return (0);
1033 static void generic_serial_put_char (struct tty_struct *tty, unsigned char ch)
1035 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
1037 debug_info("USB Serial: generic_serial_put_char\n");
1039 /* if we have a bulk out endpoint, then shove a character out it */
1040 if (serial->has_bulk_out) {
1041 if (serial->bulk_out_inuse) {
1042 debug_info ("USB Serial: already writing\n");
1043 return;
1046 /* send the single character out the bulk port */
1047 serial->bulk_out_buffer[0] = ch;
1048 serial->bulk_out_inuse = 1;
1049 serial->bulk_out_transfer = usb_request_bulk (serial->dev, serial->bulk_out_pipe, serial_write_irq, serial->bulk_out_buffer, 1, serial);
1052 return;
1056 static int generic_write_room (struct tty_struct *tty)
1058 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
1060 debug_info("USB Serial: generic_write_room\n");
1062 if (serial->has_bulk_out) {
1063 if (serial->bulk_out_inuse) {
1064 return (0);
1066 return (serial->bulk_out_size);
1069 return (0);
1073 static int generic_chars_in_buffer (struct tty_struct *tty)
1075 struct usb_serial_state *serial = (struct usb_serial_state *)tty->driver_data;
1077 debug_info("USB Serial: generic_chars_in_buffer\n");
1079 if (serial->has_bulk_out) {
1080 if (serial->bulk_out_inuse) {
1081 return (serial->bulk_out_size);
1085 return (0);
1089 static int Get_Free_Serial (void)
1091 int i;
1093 for (i=0; i < NUM_PORTS; ++i) {
1094 if (!serial_state_table[i].present)
1095 return (i);
1097 return (-1);
1101 static void * usb_serial_probe(struct usb_device *dev, unsigned int ifnum)
1103 struct usb_serial_state *serial = NULL;
1104 struct usb_interface_descriptor *interface;
1105 struct usb_endpoint_descriptor *endpoint;
1106 struct usb_endpoint_descriptor *interrupt_in_endpoint = NULL;
1107 struct usb_endpoint_descriptor *bulk_in_endpoint = NULL;
1108 struct usb_endpoint_descriptor *bulk_out_endpoint = NULL;
1109 struct usb_serial_device_type *type;
1110 int device_num;
1111 int serial_num;
1112 int i;
1113 char interrupt_pipe;
1114 char bulk_in_pipe;
1115 char bulk_out_pipe;
1117 /* loop through our list of known serial converters, and see if this device matches */
1118 device_num = 0;
1119 while (usb_serial_devices[device_num] != NULL) {
1120 type = usb_serial_devices[device_num];
1121 #ifdef SERIAL_DEBUG
1122 printk ("USB Serial: Looking at %s Vendor id=%.4x Product id=%.4x\n", type->name, *(type->idVendor), *(type->idProduct));
1123 #endif
1125 /* look at the device descriptor */
1126 if ((dev->descriptor.idVendor == *(type->idVendor)) &&
1127 (dev->descriptor.idProduct == *(type->idProduct))) {
1129 debug_info("USB Serial: descriptor matches...looking at the endpoints\n")
1131 /* descriptor matches, let's try to find the endpoints needed */
1132 interrupt_pipe = bulk_in_pipe = bulk_out_pipe = HAS_NOT;
1134 /* check out the endpoints */
1135 interface = &dev->actconfig->interface[ifnum].altsetting[0];
1136 for (i = 0; i < interface->bNumEndpoints; ++i) {
1137 endpoint = &interface->endpoint[i];
1139 if ((endpoint->bEndpointAddress & 0x80) &&
1140 ((endpoint->bmAttributes & 3) == 0x02)) {
1141 /* we found a bulk in endpoint */
1142 debug_info("USB Serial: found bulk in\n");
1143 if (bulk_in_pipe == HAS) {
1144 printk("USB Serial: can't have more than one bulk in endpoint\n");
1145 goto probe_error;
1147 bulk_in_pipe = HAS;
1148 bulk_in_endpoint = endpoint;
1151 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1152 ((endpoint->bmAttributes & 3) == 0x02)) {
1153 /* we found a bulk out endpoint */
1154 debug_info("USB Serial: found bulk out\n");
1155 if (bulk_out_pipe == HAS) {
1156 printk("USB Serial: can't have more than one bulk out endpoint\n");
1157 goto probe_error;
1159 bulk_out_pipe = HAS;
1160 bulk_out_endpoint = endpoint;
1163 if ((endpoint->bEndpointAddress & 0x80) &&
1164 ((endpoint->bmAttributes & 3) == 0x03)) {
1165 /* we found a interrupt in endpoint */
1166 debug_info("USB Serial: found interrupt in\n");
1167 if (interrupt_pipe == HAS) {
1168 printk("USB Serial: can't have more than one interrupt in endpoint\n");
1169 goto probe_error;
1171 interrupt_pipe = HAS;
1172 interrupt_in_endpoint = endpoint;
1177 /* verify that we found all of the endpoints that we need */
1178 if ((interrupt_pipe & type->needs_interrupt_in) &&
1179 (bulk_in_pipe & type->needs_bulk_in) &&
1180 (bulk_out_pipe & type->needs_bulk_out)) {
1181 /* found all that we need */
1182 printk (KERN_INFO "USB Serial: %s converter detected.\n", type->name);
1184 if (0>(serial_num = Get_Free_Serial())) {
1185 debug_info("USB Serial: Too many devices connected\n");
1186 return NULL;
1189 serial = &serial_state_table[serial_num];
1191 memset(serial, 0, sizeof(struct usb_serial_state));
1192 serial->dev = dev;
1193 serial->type = type;
1194 serial->number = serial_num;
1196 /* set up the endpoint information */
1197 if (bulk_in_endpoint) {
1198 serial->has_bulk_in = 1;
1199 serial->bulk_in_inuse = 0;
1200 serial->bulk_in_endpoint = bulk_in_endpoint->bEndpointAddress;
1201 serial->bulk_in_size = bulk_in_endpoint->wMaxPacketSize;
1202 serial->bulk_in_interval = bulk_in_endpoint->bInterval;
1203 serial->bulk_in_pipe = usb_rcvbulkpipe (dev, serial->bulk_in_endpoint);
1204 serial->bulk_in_buffer = kmalloc (serial->bulk_in_size, GFP_KERNEL);
1205 if (!serial->bulk_in_buffer) {
1206 printk("USB Serial: Couldn't allocate bulk_in_buffer\n");
1207 goto probe_error;
1211 if (bulk_out_endpoint) {
1212 serial->has_bulk_out = 1;
1213 serial->bulk_out_inuse = 0;
1214 serial->bulk_out_endpoint = bulk_out_endpoint->bEndpointAddress;
1215 serial->bulk_out_size = bulk_out_endpoint->wMaxPacketSize;
1216 serial->bulk_out_interval = bulk_out_endpoint->bInterval;
1217 serial->bulk_out_pipe = usb_rcvbulkpipe (dev, serial->bulk_out_endpoint);
1218 serial->bulk_out_buffer = kmalloc (serial->bulk_out_size, GFP_KERNEL);
1219 if (!serial->bulk_out_buffer) {
1220 printk("USB Serial: Couldn't allocate bulk_out_buffer\n");
1221 goto probe_error;
1225 if (interrupt_in_endpoint) {
1226 serial->has_interrupt_in = 1;
1227 serial->interrupt_in_inuse = 0;
1228 serial->interrupt_in_endpoint = interrupt_in_endpoint->bEndpointAddress;
1229 serial->interrupt_in_size = interrupt_in_endpoint->wMaxPacketSize;
1230 serial->interrupt_in_interval = interrupt_in_endpoint->bInterval;
1231 /* serial->interrupt_in_pipe = usb_rcvbulkpipe (dev, serial->bulk_in_endpoint); */
1232 serial->interrupt_in_buffer = kmalloc (serial->bulk_in_size, GFP_KERNEL);
1233 if (!serial->interrupt_in_buffer) {
1234 printk("USB Serial: Couldn't allocate interrupt_in_buffer\n");
1235 goto probe_error;
1239 #if 0
1240 /* set up an interrupt for out bulk in pipe */
1241 /* ask for a bulk read */
1242 serial->bulk_in_inuse = 1;
1243 serial->bulk_in_transfer = usb_request_bulk (serial->dev, serial->bulk_in_pipe, serial_read_irq, serial->bulk_in_buffer, serial->bulk_in_size, serial);
1245 /* set up our interrupt to be the time for the bulk in read */
1246 ret = usb_request_irq (dev, serial->bulk_in_pipe, usb_serial_irq, serial->bulk_in_interval, serial, &serial->irq_handle);
1247 if (ret) {
1248 printk(KERN_INFO "USB Serial: failed usb_request_irq (0x%x)\n", ret);
1249 goto probe_error;
1251 #endif
1253 serial->present = 1;
1254 MOD_INC_USE_COUNT;
1256 printk(KERN_INFO "USB Serial: %s converter now attached to ttyUSB%d\n", type->name, serial_num);
1257 return serial;
1258 } else {
1259 printk(KERN_INFO "USB Serial: descriptors matched, but endpoints did not\n");
1263 /* look at the next type in our list */
1264 ++device_num;
1267 probe_error:
1268 if (serial) {
1269 if (serial->bulk_in_buffer)
1270 kfree (serial->bulk_in_buffer);
1271 if (serial->bulk_out_buffer)
1272 kfree (serial->bulk_out_buffer);
1273 if (serial->interrupt_in_buffer)
1274 kfree (serial->interrupt_in_buffer);
1276 return NULL;
1280 static void usb_serial_disconnect(struct usb_device *dev, void *ptr)
1282 struct usb_serial_state *serial = (struct usb_serial_state *) ptr;
1284 if (serial) {
1285 if (!serial->present) {
1286 /* something strange is going on */
1287 debug_info("USB Serial: disconnect but not present?\n")
1288 return;
1291 /* need to stop any transfers...*/
1292 if (serial->bulk_in_inuse) {
1293 usb_terminate_bulk (serial->dev, serial->bulk_in_transfer);
1294 serial->bulk_in_inuse = 0;
1296 if (serial->bulk_out_inuse) {
1297 usb_terminate_bulk (serial->dev, serial->bulk_out_transfer);
1298 serial->bulk_out_inuse = 0;
1300 // usb_release_irq (serial->dev, serial->irq_handle, serial->bulk_in_pipe);
1301 if (serial->bulk_in_buffer)
1302 kfree (serial->bulk_in_buffer);
1303 if (serial->bulk_out_buffer)
1304 kfree (serial->bulk_out_buffer);
1305 if (serial->interrupt_in_buffer)
1306 kfree (serial->interrupt_in_buffer);
1308 serial->present = 0;
1309 serial->active = 0;
1311 printk (KERN_INFO "USB Serial: %s converter now disconnected from ttyUSB%d\n", serial->type->name, serial->number);
1313 } else {
1314 printk (KERN_INFO "USB Serial: device disconnected.\n");
1317 MOD_DEC_USE_COUNT;
1323 int usb_serial_init(void)
1325 int i;
1327 /* Initalize our global data */
1328 for (i = 0; i < NUM_PORTS; ++i) {
1329 memset(&serial_state_table[i], 0x00, sizeof(struct usb_serial_state));
1332 /* register the tty driver */
1333 memset (&serial_tty_driver, 0, sizeof(struct tty_driver));
1334 serial_tty_driver.magic = TTY_DRIVER_MAGIC;
1335 serial_tty_driver.driver_name = "usb";
1336 serial_tty_driver.name = "ttyUSB";
1337 serial_tty_driver.major = SERIAL_MAJOR;
1338 serial_tty_driver.minor_start = 0;
1339 serial_tty_driver.num = NUM_PORTS;
1340 serial_tty_driver.type = TTY_DRIVER_TYPE_SERIAL;
1341 serial_tty_driver.subtype = SERIAL_TYPE_NORMAL;
1342 serial_tty_driver.init_termios = tty_std_termios;
1343 serial_tty_driver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1344 serial_tty_driver.flags = TTY_DRIVER_REAL_RAW;
1345 serial_tty_driver.refcount = &serial_refcount;
1346 serial_tty_driver.table = serial_tty;
1347 serial_tty_driver.proc_entry = NULL;
1348 serial_tty_driver.other = NULL;
1349 serial_tty_driver.termios = serial_termios;
1350 serial_tty_driver.termios_locked = serial_termios_locked;
1352 serial_tty_driver.open = serial_open;
1353 serial_tty_driver.close = serial_close;
1354 serial_tty_driver.write = serial_write;
1355 serial_tty_driver.put_char = serial_put_char;
1356 serial_tty_driver.flush_chars = NULL; //serial_flush_chars;
1357 serial_tty_driver.write_room = serial_write_room;
1358 serial_tty_driver.ioctl = NULL; //serial_ioctl;
1359 serial_tty_driver.set_termios = NULL; //serial_set_termios;
1360 serial_tty_driver.set_ldisc = NULL;
1361 serial_tty_driver.throttle = serial_throttle;
1362 serial_tty_driver.unthrottle = serial_unthrottle;
1363 serial_tty_driver.stop = NULL; //serial_stop;
1364 serial_tty_driver.start = NULL; //serial_start;
1365 serial_tty_driver.hangup = NULL; //serial_hangup;
1366 serial_tty_driver.break_ctl = NULL; //serial_break;
1367 serial_tty_driver.wait_until_sent = NULL; //serial_wait_until_sent;
1368 serial_tty_driver.send_xchar = NULL; //serial_send_xchar;
1369 serial_tty_driver.read_proc = NULL; //serial_read_proc;
1370 serial_tty_driver.chars_in_buffer = serial_chars_in_buffer;
1371 serial_tty_driver.flush_buffer = NULL; //serial_flush_buffer;
1372 if (tty_register_driver (&serial_tty_driver)) {
1373 printk( "USB Serial: failed to register tty driver\n" );
1374 return -EPERM;
1377 /* register the USB driver */
1378 if (usb_register(&usb_serial_driver) < 0) {
1379 tty_unregister_driver(&serial_tty_driver);
1380 return -1;
1383 printk(KERN_INFO "USB Serial: support registered.\n");
1384 return 0;
1388 #ifdef MODULE
1389 int init_module(void)
1391 return usb_serial_init();
1394 void cleanup_module(void)
1396 tty_unregister_driver(&serial_tty_driver);
1397 usb_deregister(&usb_serial_driver);
1400 #endif