* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / usb / serial.c
blobb85f2b5c6cd36097d25531e262eb6e3d12c7e77b
1 /*
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
23 * to change.
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>
44 #include "usb.h"
45 /*#define SERIAL_DEBUG 1*/
47 #ifdef SERIAL_DEBUG
48 #define debug_info(message); printk(message);
49 #else
50 #define debug_info(message);
51 #endif
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);
70 typedef enum {
71 unknown = 0,
72 Belkin = 1,
73 Peracom = 2
74 } SERIAL_TYPE;
76 struct usb_serial_state {
77 struct usb_device * dev;
78 SERIAL_TYPE type; /* what manufacturer's type of converter */
79 void * irq_handle;
80 unsigned int irqpipe;
81 struct tty_struct *tty; /* the coresponding tty for this device */
82 char present;
83 char active;
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;
93 char bulk_in_inuse;
94 __u8 bulk_in_endpoint;
95 __u8 bulk_in_interval;
96 __u16 bulk_in_size;
97 unsigned int bulk_in_pipe;
98 unsigned char * bulk_in_buffer;
99 void * bulk_in_transfer;
101 char bulk_out_inuse;
102 __u8 bulk_out_endpoint;
103 __u8 bulk_out_interval;
104 __u16 bulk_out_size;
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 = {
111 "serial",
112 usb_serial_probe,
113 usb_serial_disconnect,
114 { NULL, NULL }
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;
131 int i;
133 debug_info("USB: serial_read_irq\n");
135 #ifdef SERIAL_DEBUG
136 if (count) {
137 printk("%d %s\n", count, data);
139 #endif
141 if (count) {
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 */
149 /* return (1); */
151 /* No more transfer, let the irq schedule us again */
152 serial->bulk_in_inuse = 0;
153 return (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");
166 return (0);
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);
177 return 0;
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);
191 return (1);
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;
207 serial->tty = tty;
209 if (!serial->present) {
210 debug_info("USB Serial: no device registered\n");
211 return -EINVAL;
214 if (serial->active) {
215 debug_info ("USB Serial: device already open\n");
216 return -EINVAL;
218 serial->active = 1;
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.) */
225 /* FIXME!!! */
227 return (0);
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");
238 return;
241 if (!serial->active) {
242 debug_info ("USB Serial: device already open\n");
243 return;
246 /* Need to change the control lines here */
247 /* FIXME */
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? */
260 serial->active = 0;
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;
267 int written;
269 debug_info("USB Serial: serial_write\n");
271 if (!serial->present) {
272 debug_info("USB Serial: device not registered\n");
273 return (-EINVAL);
276 if (!serial->active) {
277 debug_info ("USB Serial: device not opened\n");
278 return (-EINVAL);
281 if (serial->bulk_out_inuse) {
282 debug_info ("USB Serial: already writing\n");
283 return (0);
286 written = (count > serial->bulk_out_size) ? serial->bulk_out_size : count;
288 if (from_user) {
289 copy_from_user(serial->bulk_out_buffer, buf, written);
291 else {
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);
299 return (written);
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");
311 return;
314 if (!serial->active) {
315 debug_info ("USB Serial: device not open\n");
316 return;
319 if (serial->bulk_out_inuse) {
320 debug_info ("USB Serial: already writing\n");
321 return;
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);
329 return;
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");
341 return (-EINVAL);
344 if (!serial->active) {
345 debug_info ("USB Serial: device not open\n");
346 return (-EINVAL);
349 if (serial->bulk_out_inuse) {
350 return (0);
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");
365 return (-EINVAL);
368 if (!serial->active) {
369 debug_info ("USB Serial: device not open\n");
370 return (-EINVAL);
373 if (serial->bulk_out_inuse) {
374 return (serial->bulk_out_size);
377 return (0);
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");
389 return;
392 if (!serial->active) {
393 debug_info ("USB Serial: device not open\n");
394 return;
398 /* Change the control signals */
399 /* FIXME!!! */
401 return;
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");
413 return;
416 if (!serial->active) {
417 debug_info ("USB Serial: device not open\n");
418 return;
422 /* Change the control signals */
423 /* FIXME!!! */
425 return;
429 static int Get_Free_Serial (void)
431 int i;
433 for (i=0; i < NUM_PORTS; ++i) {
434 if (!serial_state_table[i].present)
435 return (i);
437 return (-1);
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;
446 SERIAL_TYPE type;
447 int serial_num;
448 // int ret;
449 int i;
451 /* look at the device descriptor to see if it is a type that we recognize */
452 type = unknown;
453 if ((dev->descriptor.idVendor == BELKIN_VENDOR_ID) &&
454 (dev->descriptor.idProduct == BELKIN_SERIAL_CONVERTER)) {
455 /* This is the Belkin serial convertor */
456 type = Belkin;
459 if ((dev->descriptor.idVendor == PERACOM_VENDOR_ID) &&
460 (dev->descriptor.idProduct == PERACOM_SERIAL_CONVERTER)) {
461 /* This is the Peracom serial convertor */
462 type = Peracom;
465 if (type == unknown)
466 return (-1);
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");
472 return (-1);
475 if (0>(serial_num = Get_Free_Serial())) {
476 debug_info("USB Serial: Too many devices connected\n");
477 return (-1);
480 serial = &serial_state_table[serial_num];
482 memset(serial, 0, sizeof(serial));
483 serial->dev = dev;
484 serial->type = type;
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");
503 goto probe_error;
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");
518 goto probe_error;
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");
533 goto probe_error;
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");
545 goto probe_error;
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);
556 // if (ret) {
557 // printk(KERN_INFO "USB Serial failed usb_request_irq (0x%x)\n", ret);
558 // goto probe_error;
559 // }
561 serial->present = 1;
562 MOD_INC_USE_COUNT;
564 return (0);
566 probe_error:
567 if (serial) {
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);
575 return (-1);
579 static void usb_serial_disconnect(struct usb_device *dev)
581 struct usb_serial_state *serial = (struct usb_serial_state *)dev->private;
583 if (serial) {
584 if (!serial->present) {
585 /* something strange is going on */
586 debug_info("USB Serial: disconnect but not present?\n")
587 return;
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);
607 serial->present = 0;
608 serial->active = 0;
610 dev->private = NULL;
612 MOD_DEC_USE_COUNT;
614 printk (KERN_INFO "USB Serial device disconnected.\n");
619 int usb_serial_init(void)
621 int i;
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" );
668 return -EPERM;
671 /* register the USB driver */
672 usb_register(&usb_serial_driver);
673 printk(KERN_INFO "USB Serial support registered.\n");
674 return 0;
678 #ifdef MODULE
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);
690 #endif