2 * USB Serial Converter driver
4 * Greg Kroah-Hartman (greg@kroah.com)
6 * This was based on the ACM driver by Armin Fuerst (which was based
7 * on a driver by Brad Keryan)
9 * Currently only works for the Belkin and Peracom Serial converters.
10 * Should also work on the Etek serial converter, if anyone knows the
11 * vendor and device ids for that device.
14 * version 0.1.1 (10/05/99) gkh
15 * Changed the major number to not conflict with anything else.
17 * version 0.1 (09/28/99) gkh
18 * Can recognize the two different devices and start up a read from
19 * device when asked to. Writes also work. No control signals yet, this
20 * all is vendor specific data (i.e. no spec), also no control for
21 * different baud rates or other bit settings.
22 * Currently we are using the same devid as the acm driver. This needs
25 * (C) Copyright 1999 Greg Kroah-Hartman (greg@kroah.com)
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/signal.h>
32 #include <linux/errno.h>
33 #include <linux/poll.h>
34 #include <linux/init.h>
35 #include <linux/malloc.h>
36 #include <linux/fcntl.h>
37 #include <linux/tty_driver.h>
38 #include <linux/tty_flip.h>
39 #include <linux/tty.h>
40 #include <linux/module.h>
41 #include <linux/spinlock.h>
45 /*#define SERIAL_DEBUG 1*/
48 #define debug_info(message); printk(message);
50 #define debug_info(message);
54 /* USB Serial devices vendor ids and device ids that this driver supports */
55 #define BELKIN_VENDOR_ID 0x056c
56 #define BELKIN_SERIAL_CONVERTER 0x8007
57 #define PERACOM_VENDOR_ID 0x0565
58 #define PERACOM_SERIAL_CONVERTER 0x0001
61 #define SERIAL_MAJOR 240
63 #define NUM_PORTS 4 /* Have to pick a number for now. Need to look */
64 /* into dynamically creating them at insertion time. */
67 static int usb_serial_probe(struct usb_device
*dev
);
68 static void usb_serial_disconnect(struct usb_device
*dev
);
76 struct usb_serial_state
{
77 struct usb_device
* dev
;
78 SERIAL_TYPE type
; /* what manufacturer's type of converter */
81 struct tty_struct
*tty
; /* the coresponding tty for this device */
85 char interrupt_in_inuse
;
86 __u8 interrupt_in_endpoint
;
87 __u8 interrupt_in_interval
;
88 __u16 interrupt_in_size
;
89 unsigned int interrupt_in_pipe
;
90 unsigned char * interrupt_in_buffer
;
91 void * interrupt_in_transfer
;
94 __u8 bulk_in_endpoint
;
95 __u8 bulk_in_interval
;
97 unsigned int bulk_in_pipe
;
98 unsigned char * bulk_in_buffer
;
99 void * bulk_in_transfer
;
102 __u8 bulk_out_endpoint
;
103 __u8 bulk_out_interval
;
105 unsigned int bulk_out_pipe
;
106 unsigned char * bulk_out_buffer
;
107 void * bulk_out_transfer
;
110 static struct usb_driver usb_serial_driver
= {
113 usb_serial_disconnect
,
117 static int serial_refcount
;
118 static struct tty_driver serial_tty_driver
;
119 static struct tty_struct
* serial_tty
[NUM_PORTS
];
120 static struct termios
* serial_termios
[NUM_PORTS
];
121 static struct termios
* serial_termios_locked
[NUM_PORTS
];
122 static struct usb_serial_state serial_state_table
[NUM_PORTS
];
126 static int serial_read_irq (int state
, void *buffer
, int count
, void *dev_id
)
128 struct usb_serial_state
*serial
= (struct usb_serial_state
*)dev_id
;
129 struct tty_struct
*tty
= serial
->tty
;
130 unsigned char* data
= buffer
;
133 debug_info("USB: serial_read_irq\n");
137 printk("%d %s\n", count
, data
);
142 for (i
=0;i
<count
;i
++) {
143 tty_insert_flip_char(tty
,data
[i
],0);
145 tty_flip_buffer_push(tty
);
148 /* Continue transfer */
151 /* No more transfer, let the irq schedule us again */
152 serial
->bulk_in_inuse
= 0;
157 static int serial_write_irq (int state
, void *buffer
, int count
, void *dev_id
)
159 struct usb_serial_state
*serial
= (struct usb_serial_state
*) dev_id
;
160 struct tty_struct
*tty
= serial
->tty
;
162 debug_info("USB Serial: serial_write_irq\n");
164 if (!serial
->bulk_out_inuse
) {
165 debug_info("USB Serial: write irq for a finished pipe?\n");
169 usb_terminate_bulk (serial
->dev
, serial
->bulk_out_transfer
);
170 serial
->bulk_out_inuse
= 0;
172 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) && tty
->ldisc
.write_wakeup
)
173 (tty
->ldisc
.write_wakeup
)(tty
);
175 wake_up_interruptible(&tty
->write_wait
);
181 static int usb_serial_irq (int state
, void *buffer
, int len
, void *dev_id
)
183 // struct usb_serial_state *serial = (struct usb_serial_state *) dev_id;
185 debug_info("USB Serial: usb_serial_irq\n");
187 /* ask for a bulk read */
188 // serial->bulk_in_inuse = 1;
189 // 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);
198 /* tty interface functions */
199 static int serial_open (struct tty_struct
*tty
, struct file
* filp
)
201 struct usb_serial_state
*serial
;
203 debug_info("USB: serial_open\n");
205 serial
= &serial_state_table
[MINOR(tty
->device
)-tty
->driver
.minor_start
];
206 tty
->driver_data
= serial
;
209 if (!serial
->present
) {
210 debug_info("USB Serial: no device registered\n");
214 if (serial
->active
) {
215 debug_info ("USB Serial: device already open\n");
220 /*Start reading from the device*/
221 serial
->bulk_in_inuse
= 1;
222 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
);
224 /* Need to do device specific setup here (control lines, baud rate, etc.) */
231 static void serial_close(struct tty_struct
*tty
, struct file
* filp
)
233 struct usb_serial_state
*serial
= (struct usb_serial_state
*) tty
->driver_data
;
234 debug_info("USB: serial_close\n");
236 if (!serial
->present
) {
237 debug_info("USB Serial: no device registered\n");
241 if (!serial
->active
) {
242 debug_info ("USB Serial: device already open\n");
246 /* Need to change the control lines here */
249 if (serial
->bulk_out_inuse
){
250 usb_terminate_bulk (serial
->dev
, serial
->bulk_out_transfer
);
251 serial
->bulk_out_inuse
= 0;
253 if (serial
->bulk_in_inuse
){
254 usb_terminate_bulk (serial
->dev
, serial
->bulk_in_transfer
);
255 serial
->bulk_in_inuse
= 0;
258 /* release the irq? */
264 static int serial_write (struct tty_struct
* tty
, int from_user
, const unsigned char *buf
, int count
)
266 struct usb_serial_state
*serial
= (struct usb_serial_state
*) tty
->driver_data
;
269 debug_info("USB Serial: serial_write\n");
271 if (!serial
->present
) {
272 debug_info("USB Serial: device not registered\n");
276 if (!serial
->active
) {
277 debug_info ("USB Serial: device not opened\n");
281 if (serial
->bulk_out_inuse
) {
282 debug_info ("USB Serial: already writing\n");
286 written
= (count
> serial
->bulk_out_size
) ? serial
->bulk_out_size
: count
;
289 copy_from_user(serial
->bulk_out_buffer
, buf
, written
);
292 memcpy (serial
->bulk_out_buffer
, buf
, written
);
295 /* send the data out the bulk port */
296 serial
->bulk_out_inuse
= 1;
297 serial
->bulk_out_transfer
= usb_request_bulk (serial
->dev
, serial
->bulk_out_pipe
, serial_write_irq
, serial
->bulk_out_buffer
, written
, serial
);
303 static void serial_put_char (struct tty_struct
*tty
, unsigned char ch
)
305 struct usb_serial_state
*serial
= (struct usb_serial_state
*)tty
->driver_data
;
307 debug_info("USB Serial: serial_put_char\n");
309 if (!serial
->present
) {
310 debug_info("USB Serial: no device registered\n");
314 if (!serial
->active
) {
315 debug_info ("USB Serial: device not open\n");
319 if (serial
->bulk_out_inuse
) {
320 debug_info ("USB Serial: already writing\n");
324 /* send the single character out the bulk port */
325 serial
->bulk_out_buffer
[0] = ch
;
326 serial
->bulk_out_inuse
= 1;
327 serial
->bulk_out_transfer
= usb_request_bulk (serial
->dev
, serial
->bulk_out_pipe
, serial_write_irq
, serial
->bulk_out_buffer
, 1, serial
);
333 static int serial_write_room (struct tty_struct
*tty
)
335 struct usb_serial_state
*serial
= (struct usb_serial_state
*)tty
->driver_data
;
337 debug_info("USB Serial: serial_write_room\n");
339 if (!serial
->present
) {
340 debug_info("USB Serial: no device registered\n");
344 if (!serial
->active
) {
345 debug_info ("USB Serial: device not open\n");
349 if (serial
->bulk_out_inuse
) {
353 return serial
->bulk_out_size
;
357 static int serial_chars_in_buffer (struct tty_struct
*tty
)
359 struct usb_serial_state
*serial
= (struct usb_serial_state
*)tty
->driver_data
;
361 debug_info("USB Serial: serial_chars_in_buffer\n");
363 if (!serial
->present
) {
364 debug_info("USB Serial: no device registered\n");
368 if (!serial
->active
) {
369 debug_info ("USB Serial: device not open\n");
373 if (serial
->bulk_out_inuse
) {
374 return (serial
->bulk_out_size
);
381 static void serial_throttle (struct tty_struct
* tty
)
383 struct usb_serial_state
*serial
= (struct usb_serial_state
*) tty
->driver_data
;
385 debug_info("USB Serial: serial_throttle\n");
387 if (!serial
->present
) {
388 debug_info("USB Serial: no device registered\n");
392 if (!serial
->active
) {
393 debug_info ("USB Serial: device not open\n");
398 /* Change the control signals */
405 static void serial_unthrottle (struct tty_struct
* tty
)
407 struct usb_serial_state
*serial
= (struct usb_serial_state
*) tty
->driver_data
;
409 debug_info("USB Serial: serial_unthrottle\n");
411 if (!serial
->present
) {
412 debug_info("USB Serial: no device registered\n");
416 if (!serial
->active
) {
417 debug_info ("USB Serial: device not open\n");
422 /* Change the control signals */
429 static int Get_Free_Serial (void)
433 for (i
=0; i
< NUM_PORTS
; ++i
) {
434 if (!serial_state_table
[i
].present
)
441 static int usb_serial_probe(struct usb_device
*dev
)
443 struct usb_serial_state
*serial
;
444 struct usb_interface_descriptor
*interface
;
445 struct usb_endpoint_descriptor
*endpoint
;
451 /* look at the device descriptor to see if it is a type that we recognize */
453 if ((dev
->descriptor
.idVendor
== BELKIN_VENDOR_ID
) &&
454 (dev
->descriptor
.idProduct
== BELKIN_SERIAL_CONVERTER
)) {
455 /* This is the Belkin serial convertor */
459 if ((dev
->descriptor
.idVendor
== PERACOM_VENDOR_ID
) &&
460 (dev
->descriptor
.idProduct
== PERACOM_SERIAL_CONVERTER
)) {
461 /* This is the Peracom serial convertor */
468 printk (KERN_INFO
"USB serial converter detected.\n");
470 if (usb_set_configuration(dev
, dev
->config
[0].bConfigurationValue
)) {
471 printk (KERN_INFO
" Failed usb_set_configuration: serial\n");
475 if (0>(serial_num
= Get_Free_Serial())) {
476 debug_info("USB Serial: Too many devices connected\n");
480 serial
= &serial_state_table
[serial_num
];
482 memset(serial
, 0, sizeof(serial
));
485 dev
->private = serial
;
487 /* we should have 1 bulk in, 1 bulk out, and 1 interrupt in endpoints */
488 interface
= &dev
->config
[0].interface
[0].altsetting
[0];
489 for (i
= 0; i
< interface
->bNumEndpoints
; ++i
) {
490 endpoint
= &interface
->endpoint
[i
];
492 if ((endpoint
->bEndpointAddress
& 0x80) &&
493 ((endpoint
->bmAttributes
& 3) == 0x02)) {
494 /* we found the bulk in endpoint */
495 serial
->bulk_in_inuse
= 0;
496 serial
->bulk_in_endpoint
= endpoint
->bEndpointAddress
;
497 serial
->bulk_in_size
= endpoint
->wMaxPacketSize
;
498 serial
->bulk_in_interval
= endpoint
->bInterval
;
499 serial
->bulk_in_pipe
= usb_rcvbulkpipe (dev
, serial
->bulk_in_endpoint
);
500 serial
->bulk_in_buffer
= kmalloc (serial
->bulk_in_size
, GFP_KERNEL
);
501 if (!serial
->bulk_in_buffer
) {
502 printk("USB Serial: Couldn't allocate bulk_in_buffer\n");
507 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
508 ((endpoint
->bmAttributes
& 3) == 0x02)) {
509 /* we found the bulk out endpoint */
510 serial
->bulk_out_inuse
= 0;
511 serial
->bulk_out_endpoint
= endpoint
->bEndpointAddress
;
512 serial
->bulk_out_size
= endpoint
->wMaxPacketSize
;
513 serial
->bulk_out_interval
= endpoint
->bInterval
;
514 serial
->bulk_out_pipe
= usb_rcvbulkpipe (dev
, serial
->bulk_out_endpoint
);
515 serial
->bulk_out_buffer
= kmalloc (serial
->bulk_out_size
, GFP_KERNEL
);
516 if (!serial
->bulk_out_buffer
) {
517 printk("USB Serial: Couldn't allocate bulk_out_buffer\n");
522 if ((endpoint
->bEndpointAddress
& 0x80) &&
523 ((endpoint
->bmAttributes
& 3) == 0x03)) {
524 /* we found the interrupt in endpoint */
525 serial
->interrupt_in_inuse
= 0;
526 serial
->interrupt_in_endpoint
= endpoint
->bEndpointAddress
;
527 serial
->interrupt_in_size
= endpoint
->wMaxPacketSize
;
528 serial
->interrupt_in_interval
= endpoint
->bInterval
;
529 /* serial->interrupt_in_pipe = usb_rcvbulkpipe (dev, serial->bulk_in_endpoint); */
530 serial
->interrupt_in_buffer
= kmalloc (serial
->bulk_in_size
, GFP_KERNEL
);
531 if (!serial
->bulk_in_buffer
) {
532 printk("USB Serial: Couldn't allocate interrupt_in_buffer\n");
540 /* verify that we found all of the endpoints that we need */
541 if ((!serial
->bulk_in_buffer
) ||
542 (!serial
->bulk_out_buffer
) ||
543 (!serial
->interrupt_in_buffer
)) {
544 printk("USB Serial: did not find all of the required endpoints\n");
549 /* set up an interrupt for out bulk in pipe */
550 /* ask for a bulk read */
551 // serial->bulk_in_inuse = 1;
552 // 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);
554 /* set up our interrupt to be the time for the bulk in read */
555 // ret = usb_request_irq (dev, serial->bulk_in_pipe, usb_serial_irq, serial->bulk_in_interval, serial, &serial->irq_handle);
557 // printk(KERN_INFO "USB Serial failed usb_request_irq (0x%x)\n", ret);
568 if (serial
->bulk_in_buffer
)
569 kfree (serial
->bulk_in_buffer
);
570 if (serial
->bulk_out_buffer
)
571 kfree (serial
->bulk_out_buffer
);
572 if (serial
->interrupt_in_buffer
)
573 kfree (serial
->interrupt_in_buffer
);
579 static void usb_serial_disconnect(struct usb_device
*dev
)
581 struct usb_serial_state
*serial
= (struct usb_serial_state
*)dev
->private;
584 if (!serial
->present
) {
585 /* something strange is going on */
586 debug_info("USB Serial: disconnect but not present?\n")
590 /* need to stop any transfers...*/
591 if (serial
->bulk_in_inuse
) {
592 usb_terminate_bulk (serial
->dev
, serial
->bulk_in_transfer
);
593 serial
->bulk_in_inuse
= 0;
595 if (serial
->bulk_out_inuse
) {
596 usb_terminate_bulk (serial
->dev
, serial
->bulk_out_transfer
);
597 serial
->bulk_out_inuse
= 0;
599 // usb_release_irq (serial->dev, serial->irq_handle, serial->bulk_in_pipe);
600 if (serial
->bulk_in_buffer
)
601 kfree (serial
->bulk_in_buffer
);
602 if (serial
->bulk_out_buffer
)
603 kfree (serial
->bulk_out_buffer
);
604 if (serial
->interrupt_in_buffer
)
605 kfree (serial
->interrupt_in_buffer
);
614 printk (KERN_INFO
"USB Serial device disconnected.\n");
619 int usb_serial_init(void)
623 /* Initalize our global data */
624 for (i
= 0; i
< NUM_PORTS
; ++i
) {
625 memset(&serial_state_table
[i
], 0x00, sizeof(struct usb_serial_state
));
628 /* register the tty driver */
629 memset (&serial_tty_driver
, 0, sizeof(struct tty_driver
));
630 serial_tty_driver
.magic
= TTY_DRIVER_MAGIC
;
631 serial_tty_driver
.driver_name
= "usb";
632 serial_tty_driver
.name
= "ttyUSB";
633 serial_tty_driver
.major
= SERIAL_MAJOR
;
634 serial_tty_driver
.minor_start
= 0;
635 serial_tty_driver
.num
= NUM_PORTS
;
636 serial_tty_driver
.type
= TTY_DRIVER_TYPE_SERIAL
;
637 serial_tty_driver
.subtype
= SERIAL_TYPE_NORMAL
;
638 serial_tty_driver
.init_termios
= tty_std_termios
;
639 serial_tty_driver
.init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
640 serial_tty_driver
.flags
= TTY_DRIVER_REAL_RAW
;
641 serial_tty_driver
.refcount
= &serial_refcount
;
642 serial_tty_driver
.table
= serial_tty
;
643 serial_tty_driver
.termios
= serial_termios
;
644 serial_tty_driver
.termios_locked
= serial_termios_locked
;
646 serial_tty_driver
.open
= serial_open
;
647 serial_tty_driver
.close
= serial_close
;
648 serial_tty_driver
.write
= serial_write
;
649 serial_tty_driver
.put_char
= serial_put_char
;
650 serial_tty_driver
.flush_chars
= NULL
; //serial_flush_chars;
651 serial_tty_driver
.write_room
= serial_write_room
;
652 serial_tty_driver
.ioctl
= NULL
; //serial_ioctl;
653 serial_tty_driver
.set_termios
= NULL
; //serial_set_termios;
654 serial_tty_driver
.set_ldisc
= NULL
;
655 serial_tty_driver
.throttle
= serial_throttle
;
656 serial_tty_driver
.unthrottle
= serial_unthrottle
;
657 serial_tty_driver
.stop
= NULL
; //serial_stop;
658 serial_tty_driver
.start
= NULL
; //serial_start;
659 serial_tty_driver
.hangup
= NULL
; //serial_hangup;
660 serial_tty_driver
.break_ctl
= NULL
; //serial_break;
661 serial_tty_driver
.wait_until_sent
= NULL
; //serial_wait_until_sent;
662 serial_tty_driver
.send_xchar
= NULL
; //serial_send_xchar;
663 serial_tty_driver
.read_proc
= NULL
; //serial_read_proc;
664 serial_tty_driver
.chars_in_buffer
= serial_chars_in_buffer
;
665 serial_tty_driver
.flush_buffer
= NULL
; //serial_flush_buffer;
666 if (tty_register_driver (&serial_tty_driver
)) {
667 printk( "USB Serial: failed to register tty driver\n" );
671 /* register the USB driver */
672 usb_register(&usb_serial_driver
);
673 printk(KERN_INFO
"USB Serial support registered.\n");
679 int init_module(void)
681 return usb_serial_init();
684 void cleanup_module(void)
686 tty_unregister_driver(&serial_tty_driver
);
687 usb_deregister(&usb_serial_driver
);