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
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>
71 /*#define SERIAL_DEBUG 1*/
74 #define debug_info(message); printk(message);
76 #define debug_info(message);
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
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
{
134 char needs_interrupt_in
;
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
= {
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 */
171 etek_serial_put_char
,
173 etek_chars_in_buffer
,
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
= {
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 */
191 etek_serial_put_char
,
193 etek_chars_in_buffer
,
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
,
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
= {
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 */
247 generic_serial_close
,
248 generic_serial_write
,
249 generic_serial_put_char
,
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
260 static struct usb_serial_device_type
*usb_serial_devices
[] = {
270 struct usb_serial_state
{
271 struct usb_device
* dev
;
272 struct usb_serial_device_type
* type
;
274 unsigned int irqpipe
;
275 struct tty_struct
* tty
; /* the coresponding tty for this device */
276 unsigned char number
;
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
= {
311 usb_serial_disconnect
,
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
;
331 debug_info("USB Serial: serial_read_irq\n");
335 printk("%d %s\n", count
, data
);
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 */
349 /* No more transfer, let the irq schedule us again */
350 serial
->bulk_in_inuse
= 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");
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
);
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);
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 */
411 debug_info("USB Serial: serial == NULL!\n");
415 debug_info("USB Serial: serial->type == NULL!\n");
419 /* make the tty driver remember our serial object, and us it */
420 tty
->driver_data
= serial
;
423 /* pass on to the driver specific version of this function */
424 if (serial
->type
->open
) {
425 return (serial
->type
->open(tty
, filp
));
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 */
439 debug_info("USB Serial: serial == NULL!\n");
443 debug_info("USB Serial: serial->type == NULL!\n");
446 if (!serial
->present
) {
447 debug_info("USB Serial: no device registered\n");
450 if (!serial
->active
) {
451 debug_info ("USB Serial: device already open\n");
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 */
470 debug_info("USB Serial: serial == NULL!\n");
474 debug_info("USB Serial: serial->type == NULL!\n");
477 if (!serial
->present
) {
478 debug_info("USB Serial: device not registered\n");
481 if (!serial
->active
) {
482 debug_info ("USB Serial: device not opened\n");
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 */
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 */
504 debug_info("USB Serial: serial == NULL!\n");
508 debug_info("USB Serial: serial->type == NULL!\n");
511 if (!serial
->present
) {
512 debug_info("USB Serial: no device registered\n");
515 if (!serial
->active
) {
516 debug_info ("USB Serial: device not open\n");
520 /* pass on to the driver specific version of this function */
521 if (serial
->type
->put_char
) {
522 serial
->type
->put_char(tty
, ch
);
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 */
537 debug_info("USB Serial: serial == NULL!\n");
541 debug_info("USB Serial: serial->type == NULL!\n");
544 if (!serial
->present
) {
545 debug_info("USB Serial: no device registered\n");
548 if (!serial
->active
) {
549 debug_info ("USB Serial: device not open\n");
553 /* pass on to the driver specific version of this function */
554 if (serial
->type
->write_room
) {
555 return (serial
->type
->write_room(tty
));
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 */
570 debug_info("USB Serial: serial == NULL!\n");
574 debug_info("USB Serial: serial->type == NULL!\n");
577 if (!serial
->present
) {
578 debug_info("USB Serial: no device registered\n");
581 if (!serial
->active
) {
582 debug_info ("USB Serial: device not open\n");
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
));
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 */
603 debug_info("USB Serial: serial == NULL!\n");
607 debug_info("USB Serial: serial->type == NULL!\n");
610 if (!serial
->present
) {
611 debug_info("USB Serial: no device registered\n");
614 if (!serial
->active
) {
615 debug_info ("USB Serial: device not open\n");
619 /* pass on to the driver specific version of this function */
620 if (serial
->type
->throttle
) {
621 serial
->type
->throttle(tty
);
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 */
636 debug_info("USB Serial: serial == NULL!\n");
640 debug_info("USB Serial: serial->type == NULL!\n");
643 if (!serial
->present
) {
644 debug_info("USB Serial: no device registered\n");
647 if (!serial
->active
) {
648 debug_info ("USB Serial: device not open\n");
653 /* pass on to the driver specific version of this function */
654 if (serial
->type
->unthrottle
) {
655 serial
->type
->unthrottle(tty
);
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");
676 if (serial
->active
) {
677 debug_info ("USB Serial: device already open\n");
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.) */
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 */
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? */
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
;
722 debug_info("USB Serial: etek_serial_write\n");
724 if (serial
->bulk_out_inuse
) {
725 debug_info ("USB Serial: already writing\n");
729 written
= (count
> serial
->bulk_out_size
) ? serial
->bulk_out_size
: count
;
732 copy_from_user(serial
->bulk_out_buffer
, buf
, written
);
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
);
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");
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
);
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
) {
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
);
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");
808 if (serial
->active
) {
809 debug_info ("USB Serial: device already open\n");
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.) */
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 */
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? */
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
;
854 debug_info("USB Serial: whiteheat_serial_write\n");
856 if (serial
->bulk_out_inuse
) {
857 debug_info ("USB Serial: already writing\n");
861 written
= (count
> serial
->bulk_out_size
) ? serial
->bulk_out_size
: count
;
864 copy_from_user(serial
->bulk_out_buffer
, buf
, written
);
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
);
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");
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
);
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
) {
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
);
926 static void whiteheat_throttle (struct tty_struct
* tty
)
928 debug_info("USB Serial: whiteheat_throttle\n");
930 /* Change the control signals */
937 static void whiteheat_unthrottle (struct tty_struct
* tty
)
939 debug_info("USB Serial: whiteheat_unthrottle\n");
941 /* Change the control signals */
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");
962 if (serial
->active
) {
963 debug_info ("USB Serial: device already open\n");
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
);
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;
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
;
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");
1012 written
= (count
> serial
->bulk_out_size
) ? serial
->bulk_out_size
: count
;
1015 copy_from_user(serial
->bulk_out_buffer
, buf
, written
);
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
);
1028 /* no bulk out, so return 0 bytes written */
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");
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
);
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
) {
1066 return (serial
->bulk_out_size
);
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
);
1089 static int Get_Free_Serial (void)
1093 for (i
=0; i
< NUM_PORTS
; ++i
) {
1094 if (!serial_state_table
[i
].present
)
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
;
1113 char interrupt_pipe
;
1117 /* loop through our list of known serial converters, and see if this device matches */
1119 while (usb_serial_devices
[device_num
] != NULL
) {
1120 type
= usb_serial_devices
[device_num
];
1122 printk ("USB Serial: Looking at %s Vendor id=%.4x Product id=%.4x\n", type
->name
, *(type
->idVendor
), *(type
->idProduct
));
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");
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");
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");
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");
1189 serial
= &serial_state_table
[serial_num
];
1191 memset(serial
, 0, sizeof(struct usb_serial_state
));
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");
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");
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");
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
);
1248 printk(KERN_INFO
"USB Serial: failed usb_request_irq (0x%x)\n", ret
);
1253 serial
->present
= 1;
1256 printk(KERN_INFO
"USB Serial: %s converter now attached to ttyUSB%d\n", type
->name
, serial_num
);
1259 printk(KERN_INFO
"USB Serial: descriptors matched, but endpoints did not\n");
1263 /* look at the next type in our list */
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
);
1280 static void usb_serial_disconnect(struct usb_device
*dev
, void *ptr
)
1282 struct usb_serial_state
*serial
= (struct usb_serial_state
*) ptr
;
1285 if (!serial
->present
) {
1286 /* something strange is going on */
1287 debug_info("USB Serial: disconnect but not present?\n")
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;
1311 printk (KERN_INFO
"USB Serial: %s converter now disconnected from ttyUSB%d\n", serial
->type
->name
, serial
->number
);
1314 printk (KERN_INFO
"USB Serial: device disconnected.\n");
1323 int usb_serial_init(void)
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" );
1377 /* register the USB driver */
1378 if (usb_register(&usb_serial_driver
) < 0) {
1379 tty_unregister_driver(&serial_tty_driver
);
1383 printk(KERN_INFO
"USB Serial: support registered.\n");
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
);