2 * USB Serial Converter driver
4 * Copyright (C) 1999 - 2005 Greg Kroah-Hartman (greg@kroah.com)
5 * Copyright (C) 2000 Peter Berger (pberger@brimson.com)
6 * Copyright (C) 2000 Al Borchers (borchers@steinerpoint.com)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
12 * This driver was originally based on the ACM driver by Armin Fuerst (which was
13 * based on a driver by Brad Keryan)
15 * See Documentation/usb/usb-serial.txt for more information on using this driver
19 #include <linux/config.h>
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/tty.h>
25 #include <linux/tty_driver.h>
26 #include <linux/tty_flip.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <linux/smp_lock.h>
32 #include <asm/uaccess.h>
33 #include <linux/usb.h>
34 #include "usb-serial.h"
40 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
41 #define DRIVER_DESC "USB Serial Driver core"
43 /* Driver structure we register with the USB core */
44 static struct usb_driver usb_serial_driver = {
46 .probe = usb_serial_probe,
47 .disconnect = usb_serial_disconnect,
51 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
52 the MODULE_DEVICE_TABLE declarations in each serial driver
53 cause the "hotplug" program to pull in whatever module is necessary
54 via modprobe, and modprobe will load usbserial because the serial
59 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
60 static LIST_HEAD(usb_serial_driver_list);
62 struct usb_serial *usb_serial_get_by_index(unsigned index)
64 struct usb_serial *serial = serial_table[index];
67 kref_get(&serial->kref);
71 static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
76 dbg("%s %d", __FUNCTION__, num_ports);
79 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
84 for (j = 1; j <= num_ports-1; ++j)
85 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
94 dbg("%s - minor base = %d", __FUNCTION__, *minor);
95 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
96 serial_table[i] = serial;
102 static void return_serial(struct usb_serial *serial)
106 dbg("%s", __FUNCTION__);
111 for (i = 0; i < serial->num_ports; ++i) {
112 serial_table[serial->minor + i] = NULL;
116 static void destroy_serial(struct kref *kref)
118 struct usb_serial *serial;
119 struct usb_serial_port *port;
122 serial = to_usb_serial(kref);
124 dbg("%s - %s", __FUNCTION__, serial->type->description);
126 serial->type->shutdown(serial);
128 /* return the minor range that this device had */
129 return_serial(serial);
131 for (i = 0; i < serial->num_ports; ++i)
132 serial->port[i]->open_count = 0;
134 /* the ports are cleaned up and released in port_release() */
135 for (i = 0; i < serial->num_ports; ++i)
136 if (serial->port[i]->dev.parent != NULL) {
137 device_unregister(&serial->port[i]->dev);
138 serial->port[i] = NULL;
141 /* If this is a "fake" port, we have to clean it up here, as it will
142 * not get cleaned up in port_release() as it was never registered with
144 if (serial->num_ports < serial->num_port_pointers) {
145 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
146 port = serial->port[i];
149 usb_kill_urb(port->read_urb);
150 usb_free_urb(port->read_urb);
151 usb_kill_urb(port->write_urb);
152 usb_free_urb(port->write_urb);
153 usb_kill_urb(port->interrupt_in_urb);
154 usb_free_urb(port->interrupt_in_urb);
155 usb_kill_urb(port->interrupt_out_urb);
156 usb_free_urb(port->interrupt_out_urb);
157 kfree(port->bulk_in_buffer);
158 kfree(port->bulk_out_buffer);
159 kfree(port->interrupt_in_buffer);
160 kfree(port->interrupt_out_buffer);
164 usb_put_dev(serial->dev);
166 /* free up any memory that we allocated */
170 /*****************************************************************************
171 * Driver tty interface functions
172 *****************************************************************************/
173 static int serial_open (struct tty_struct *tty, struct file * filp)
175 struct usb_serial *serial;
176 struct usb_serial_port *port;
177 unsigned int portNumber;
180 dbg("%s", __FUNCTION__);
182 /* get the serial object associated with this tty pointer */
183 serial = usb_serial_get_by_index(tty->index);
185 tty->driver_data = NULL;
189 portNumber = tty->index - serial->minor;
190 port = serial->port[portNumber];
196 if (port->open_count == 1) {
198 /* set up our port structure making the tty driver
199 * remember our port object, and us it */
200 tty->driver_data = port;
203 /* lock this module before we call it
204 * this may fail, which means we must bail out,
205 * safe because we are called with BKL held */
206 if (!try_module_get(serial->type->driver.owner)) {
208 goto bailout_kref_put;
211 /* only call the device specific open if this
212 * is the first time the port is opened */
213 retval = serial->type->open(port, filp);
215 goto bailout_module_put;
221 module_put(serial->type->driver.owner);
223 kref_put(&serial->kref, destroy_serial);
224 port->open_count = 0;
228 static void serial_close(struct tty_struct *tty, struct file * filp)
230 struct usb_serial_port *port = tty->driver_data;
235 dbg("%s - port %d", __FUNCTION__, port->number);
237 if (port->open_count == 0)
241 if (port->open_count == 0) {
242 /* only call the device specific close if this
243 * port is being closed by the last owner */
244 port->serial->type->close(port, filp);
247 if (port->tty->driver_data)
248 port->tty->driver_data = NULL;
252 module_put(port->serial->type->driver.owner);
255 kref_put(&port->serial->kref, destroy_serial);
258 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
260 struct usb_serial_port *port = tty->driver_data;
261 int retval = -EINVAL;
266 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
268 if (!port->open_count) {
269 dbg("%s - port not opened", __FUNCTION__);
273 /* pass on to the driver specific version of this function */
274 retval = port->serial->type->write(port, buf, count);
280 static int serial_write_room (struct tty_struct *tty)
282 struct usb_serial_port *port = tty->driver_data;
283 int retval = -EINVAL;
288 dbg("%s - port %d", __FUNCTION__, port->number);
290 if (!port->open_count) {
291 dbg("%s - port not open", __FUNCTION__);
295 /* pass on to the driver specific version of this function */
296 retval = port->serial->type->write_room(port);
302 static int serial_chars_in_buffer (struct tty_struct *tty)
304 struct usb_serial_port *port = tty->driver_data;
305 int retval = -EINVAL;
310 dbg("%s = port %d", __FUNCTION__, port->number);
312 if (!port->open_count) {
313 dbg("%s - port not open", __FUNCTION__);
317 /* pass on to the driver specific version of this function */
318 retval = port->serial->type->chars_in_buffer(port);
324 static void serial_throttle (struct tty_struct * tty)
326 struct usb_serial_port *port = tty->driver_data;
331 dbg("%s - port %d", __FUNCTION__, port->number);
333 if (!port->open_count) {
334 dbg ("%s - port not open", __FUNCTION__);
338 /* pass on to the driver specific version of this function */
339 if (port->serial->type->throttle)
340 port->serial->type->throttle(port);
343 static void serial_unthrottle (struct tty_struct * tty)
345 struct usb_serial_port *port = tty->driver_data;
350 dbg("%s - port %d", __FUNCTION__, port->number);
352 if (!port->open_count) {
353 dbg("%s - port not open", __FUNCTION__);
357 /* pass on to the driver specific version of this function */
358 if (port->serial->type->unthrottle)
359 port->serial->type->unthrottle(port);
362 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
364 struct usb_serial_port *port = tty->driver_data;
365 int retval = -ENODEV;
370 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
372 if (!port->open_count) {
373 dbg ("%s - port not open", __FUNCTION__);
377 /* pass on to the driver specific version of this function if it is available */
378 if (port->serial->type->ioctl)
379 retval = port->serial->type->ioctl(port, file, cmd, arg);
381 retval = -ENOIOCTLCMD;
387 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
389 struct usb_serial_port *port = tty->driver_data;
394 dbg("%s - port %d", __FUNCTION__, port->number);
396 if (!port->open_count) {
397 dbg("%s - port not open", __FUNCTION__);
401 /* pass on to the driver specific version of this function if it is available */
402 if (port->serial->type->set_termios)
403 port->serial->type->set_termios(port, old);
406 static void serial_break (struct tty_struct *tty, int break_state)
408 struct usb_serial_port *port = tty->driver_data;
413 dbg("%s - port %d", __FUNCTION__, port->number);
415 if (!port->open_count) {
416 dbg("%s - port not open", __FUNCTION__);
420 /* pass on to the driver specific version of this function if it is available */
421 if (port->serial->type->break_ctl)
422 port->serial->type->break_ctl(port, break_state);
425 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
427 struct usb_serial *serial;
433 dbg("%s", __FUNCTION__);
434 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
435 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
436 serial = usb_serial_get_by_index(i);
440 length += sprintf (page+length, "%d:", i);
441 if (serial->type->driver.owner)
442 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
443 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
444 length += sprintf (page+length, " vendor:%04x product:%04x",
445 le16_to_cpu(serial->dev->descriptor.idVendor),
446 le16_to_cpu(serial->dev->descriptor.idProduct));
447 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
448 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
450 usb_make_path(serial->dev, tmp, sizeof(tmp));
451 length += sprintf (page+length, " path:%s", tmp);
453 length += sprintf (page+length, "\n");
454 if ((length + begin) > (off + count))
456 if ((length + begin) < off) {
460 kref_put(&serial->kref, destroy_serial);
464 if (off >= (length + begin))
466 *start = page + (off-begin);
467 return ((count < begin+length-off) ? count : begin+length-off);
470 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
472 struct usb_serial_port *port = tty->driver_data;
477 dbg("%s - port %d", __FUNCTION__, port->number);
479 if (!port->open_count) {
480 dbg("%s - port not open", __FUNCTION__);
484 if (port->serial->type->tiocmget)
485 return port->serial->type->tiocmget(port, file);
491 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
492 unsigned int set, unsigned int clear)
494 struct usb_serial_port *port = tty->driver_data;
499 dbg("%s - port %d", __FUNCTION__, port->number);
501 if (!port->open_count) {
502 dbg("%s - port not open", __FUNCTION__);
506 if (port->serial->type->tiocmset)
507 return port->serial->type->tiocmset(port, file, set, clear);
513 void usb_serial_port_softint(void *private)
515 struct usb_serial_port *port = private;
516 struct tty_struct *tty;
518 dbg("%s - port %d", __FUNCTION__, port->number);
530 static void port_release(struct device *dev)
532 struct usb_serial_port *port = to_usb_serial_port(dev);
534 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
535 usb_kill_urb(port->read_urb);
536 usb_free_urb(port->read_urb);
537 usb_kill_urb(port->write_urb);
538 usb_free_urb(port->write_urb);
539 usb_kill_urb(port->interrupt_in_urb);
540 usb_free_urb(port->interrupt_in_urb);
541 usb_kill_urb(port->interrupt_out_urb);
542 usb_free_urb(port->interrupt_out_urb);
543 kfree(port->bulk_in_buffer);
544 kfree(port->bulk_out_buffer);
545 kfree(port->interrupt_in_buffer);
546 kfree(port->interrupt_out_buffer);
550 static struct usb_serial * create_serial (struct usb_device *dev,
551 struct usb_interface *interface,
552 struct usb_serial_driver *driver)
554 struct usb_serial *serial;
556 serial = kmalloc (sizeof (*serial), GFP_KERNEL);
558 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
561 memset (serial, 0, sizeof(*serial));
562 serial->dev = usb_get_dev(dev);
563 serial->type = driver;
564 serial->interface = interface;
565 kref_init(&serial->kref);
570 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
573 const struct usb_device_id *id;
574 struct usb_serial_driver *t;
576 /* List trough know devices and see if the usb id matches */
577 list_for_each(p, &usb_serial_driver_list) {
578 t = list_entry(p, struct usb_serial_driver, driver_list);
579 id = usb_match_id(iface, t->id_table);
581 dbg("descriptor matches");
589 int usb_serial_probe(struct usb_interface *interface,
590 const struct usb_device_id *id)
592 struct usb_device *dev = interface_to_usbdev (interface);
593 struct usb_serial *serial = NULL;
594 struct usb_serial_port *port;
595 struct usb_host_interface *iface_desc;
596 struct usb_endpoint_descriptor *endpoint;
597 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
598 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
599 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
600 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
601 struct usb_serial_driver *type = NULL;
606 int num_interrupt_in = 0;
607 int num_interrupt_out = 0;
609 int num_bulk_out = 0;
613 type = search_serial_device(interface);
619 serial = create_serial (dev, interface, type);
621 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
625 /* if this device type has a probe function, call it */
627 const struct usb_device_id *id;
629 if (!try_module_get(type->driver.owner)) {
630 dev_err(&interface->dev, "module get failed, exiting\n");
635 id = usb_match_id(interface, type->id_table);
636 retval = type->probe(serial, id);
637 module_put(type->driver.owner);
640 dbg ("sub driver rejected device");
646 /* descriptor matches, let's find the endpoints needed */
647 /* check out the endpoints */
648 iface_desc = interface->cur_altsetting;
649 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
650 endpoint = &iface_desc->endpoint[i].desc;
652 if ((endpoint->bEndpointAddress & 0x80) &&
653 ((endpoint->bmAttributes & 3) == 0x02)) {
654 /* we found a bulk in endpoint */
655 dbg("found bulk in on endpoint %d", i);
656 bulk_in_endpoint[num_bulk_in] = endpoint;
660 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
661 ((endpoint->bmAttributes & 3) == 0x02)) {
662 /* we found a bulk out endpoint */
663 dbg("found bulk out on endpoint %d", i);
664 bulk_out_endpoint[num_bulk_out] = endpoint;
668 if ((endpoint->bEndpointAddress & 0x80) &&
669 ((endpoint->bmAttributes & 3) == 0x03)) {
670 /* we found a interrupt in endpoint */
671 dbg("found interrupt in on endpoint %d", i);
672 interrupt_in_endpoint[num_interrupt_in] = endpoint;
676 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
677 ((endpoint->bmAttributes & 3) == 0x03)) {
678 /* we found an interrupt out endpoint */
679 dbg("found interrupt out on endpoint %d", i);
680 interrupt_out_endpoint[num_interrupt_out] = endpoint;
685 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
686 /* BEGIN HORRIBLE HACK FOR PL2303 */
687 /* this is needed due to the looney way its endpoints are set up */
688 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
689 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
690 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
691 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
692 if (interface != dev->actconfig->interface[0]) {
693 /* check out the endpoints of the other interface*/
694 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
695 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
696 endpoint = &iface_desc->endpoint[i].desc;
697 if ((endpoint->bEndpointAddress & 0x80) &&
698 ((endpoint->bmAttributes & 3) == 0x03)) {
699 /* we found a interrupt in endpoint */
700 dbg("found interrupt in for Prolific device on separate interface");
701 interrupt_in_endpoint[num_interrupt_in] = endpoint;
707 /* Now make sure the PL-2303 is configured correctly.
708 * If not, give up now and hope this hack will work
709 * properly during a later invocation of usb_serial_probe
711 if (num_bulk_in == 0 || num_bulk_out == 0) {
712 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
717 /* END HORRIBLE HACK FOR PL2303 */
720 /* found all that we need */
721 dev_info(&interface->dev, "%s converter detected\n", type->description);
723 #ifdef CONFIG_USB_SERIAL_GENERIC
724 if (type == &usb_serial_generic_device) {
725 num_ports = num_bulk_out;
726 if (num_ports == 0) {
727 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
734 /* if this device type has a calc_num_ports function, call it */
735 if (type->calc_num_ports) {
736 if (!try_module_get(type->driver.owner)) {
737 dev_err(&interface->dev, "module get failed, exiting\n");
741 num_ports = type->calc_num_ports (serial);
742 module_put(type->driver.owner);
745 num_ports = type->num_ports;
748 if (get_free_serial (serial, num_ports, &minor) == NULL) {
749 dev_err(&interface->dev, "No more free serial devices\n");
754 serial->minor = minor;
755 serial->num_ports = num_ports;
756 serial->num_bulk_in = num_bulk_in;
757 serial->num_bulk_out = num_bulk_out;
758 serial->num_interrupt_in = num_interrupt_in;
759 serial->num_interrupt_out = num_interrupt_out;
761 /* create our ports, we need as many as the max endpoints */
762 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
763 max_endpoints = max(num_bulk_in, num_bulk_out);
764 max_endpoints = max(max_endpoints, num_interrupt_in);
765 max_endpoints = max(max_endpoints, num_interrupt_out);
766 max_endpoints = max(max_endpoints, (int)serial->num_ports);
767 serial->num_port_pointers = max_endpoints;
768 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
769 for (i = 0; i < max_endpoints; ++i) {
770 port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
773 memset(port, 0x00, sizeof(struct usb_serial_port));
774 port->number = i + serial->minor;
775 port->serial = serial;
776 spin_lock_init(&port->lock);
777 INIT_WORK(&port->work, usb_serial_port_softint, port);
778 serial->port[i] = port;
781 /* set up the endpoint information */
782 for (i = 0; i < num_bulk_in; ++i) {
783 endpoint = bulk_in_endpoint[i];
784 port = serial->port[i];
785 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
786 if (!port->read_urb) {
787 dev_err(&interface->dev, "No free urbs available\n");
790 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
791 port->bulk_in_size = buffer_size;
792 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
793 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
794 if (!port->bulk_in_buffer) {
795 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
798 usb_fill_bulk_urb (port->read_urb, dev,
799 usb_rcvbulkpipe (dev,
800 endpoint->bEndpointAddress),
801 port->bulk_in_buffer, buffer_size,
802 serial->type->read_bulk_callback,
806 for (i = 0; i < num_bulk_out; ++i) {
807 endpoint = bulk_out_endpoint[i];
808 port = serial->port[i];
809 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
810 if (!port->write_urb) {
811 dev_err(&interface->dev, "No free urbs available\n");
814 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
815 port->bulk_out_size = buffer_size;
816 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
817 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
818 if (!port->bulk_out_buffer) {
819 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
822 usb_fill_bulk_urb (port->write_urb, dev,
823 usb_sndbulkpipe (dev,
824 endpoint->bEndpointAddress),
825 port->bulk_out_buffer, buffer_size,
826 serial->type->write_bulk_callback,
830 if (serial->type->read_int_callback) {
831 for (i = 0; i < num_interrupt_in; ++i) {
832 endpoint = interrupt_in_endpoint[i];
833 port = serial->port[i];
834 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
835 if (!port->interrupt_in_urb) {
836 dev_err(&interface->dev, "No free urbs available\n");
839 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
840 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
841 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
842 if (!port->interrupt_in_buffer) {
843 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
846 usb_fill_int_urb (port->interrupt_in_urb, dev,
848 endpoint->bEndpointAddress),
849 port->interrupt_in_buffer, buffer_size,
850 serial->type->read_int_callback, port,
851 endpoint->bInterval);
853 } else if (num_interrupt_in) {
854 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
857 if (serial->type->write_int_callback) {
858 for (i = 0; i < num_interrupt_out; ++i) {
859 endpoint = interrupt_out_endpoint[i];
860 port = serial->port[i];
861 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
862 if (!port->interrupt_out_urb) {
863 dev_err(&interface->dev, "No free urbs available\n");
866 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
867 port->interrupt_out_size = buffer_size;
868 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
869 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
870 if (!port->interrupt_out_buffer) {
871 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
874 usb_fill_int_urb (port->interrupt_out_urb, dev,
876 endpoint->bEndpointAddress),
877 port->interrupt_out_buffer, buffer_size,
878 serial->type->write_int_callback, port,
879 endpoint->bInterval);
881 } else if (num_interrupt_out) {
882 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
885 /* if this device type has an attach function, call it */
887 if (!try_module_get(type->driver.owner)) {
888 dev_err(&interface->dev, "module get failed, exiting\n");
891 retval = type->attach (serial);
892 module_put(type->driver.owner);
896 /* quietly accept this device, but don't bind to a serial port
897 * as it's about to disappear */
902 /* register all of the individual ports with the driver core */
903 for (i = 0; i < num_ports; ++i) {
904 port = serial->port[i];
905 port->dev.parent = &interface->dev;
906 port->dev.driver = NULL;
907 port->dev.bus = &usb_serial_bus_type;
908 port->dev.release = &port_release;
910 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
911 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
912 device_register (&port->dev);
915 usb_serial_console_init (debug, minor);
919 usb_set_intfdata (interface, serial);
923 for (i = 0; i < num_bulk_in; ++i) {
924 port = serial->port[i];
928 usb_free_urb (port->read_urb);
929 kfree(port->bulk_in_buffer);
931 for (i = 0; i < num_bulk_out; ++i) {
932 port = serial->port[i];
936 usb_free_urb (port->write_urb);
937 kfree(port->bulk_out_buffer);
939 for (i = 0; i < num_interrupt_in; ++i) {
940 port = serial->port[i];
943 if (port->interrupt_in_urb)
944 usb_free_urb (port->interrupt_in_urb);
945 kfree(port->interrupt_in_buffer);
947 for (i = 0; i < num_interrupt_out; ++i) {
948 port = serial->port[i];
951 if (port->interrupt_out_urb)
952 usb_free_urb (port->interrupt_out_urb);
953 kfree(port->interrupt_out_buffer);
956 /* return the minor range that this device had */
957 return_serial (serial);
959 /* free up any memory that we allocated */
960 for (i = 0; i < serial->num_port_pointers; ++i)
961 kfree(serial->port[i]);
966 void usb_serial_disconnect(struct usb_interface *interface)
969 struct usb_serial *serial = usb_get_intfdata (interface);
970 struct device *dev = &interface->dev;
971 struct usb_serial_port *port;
973 dbg ("%s", __FUNCTION__);
975 usb_set_intfdata (interface, NULL);
977 for (i = 0; i < serial->num_ports; ++i) {
978 port = serial->port[i];
979 if (port && port->tty)
980 tty_hangup(port->tty);
982 /* let the last holder of this object
983 * cause it to be cleaned up */
984 kref_put(&serial->kref, destroy_serial);
986 dev_info(dev, "device disconnected\n");
989 static struct tty_operations serial_ops = {
991 .close = serial_close,
992 .write = serial_write,
993 .write_room = serial_write_room,
994 .ioctl = serial_ioctl,
995 .set_termios = serial_set_termios,
996 .throttle = serial_throttle,
997 .unthrottle = serial_unthrottle,
998 .break_ctl = serial_break,
999 .chars_in_buffer = serial_chars_in_buffer,
1000 .read_proc = serial_read_proc,
1001 .tiocmget = serial_tiocmget,
1002 .tiocmset = serial_tiocmset,
1005 struct tty_driver *usb_serial_tty_driver;
1007 static int __init usb_serial_init(void)
1012 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1013 if (!usb_serial_tty_driver)
1016 /* Initialize our global data */
1017 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1018 serial_table[i] = NULL;
1021 result = bus_register(&usb_serial_bus_type);
1023 err("%s - registering bus driver failed", __FUNCTION__);
1027 usb_serial_tty_driver->owner = THIS_MODULE;
1028 usb_serial_tty_driver->driver_name = "usbserial";
1029 usb_serial_tty_driver->devfs_name = "usb/tts/";
1030 usb_serial_tty_driver->name = "ttyUSB";
1031 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1032 usb_serial_tty_driver->minor_start = 0;
1033 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1034 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1035 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1036 usb_serial_tty_driver->init_termios = tty_std_termios;
1037 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1038 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1039 result = tty_register_driver(usb_serial_tty_driver);
1041 err("%s - tty_register_driver failed", __FUNCTION__);
1042 goto exit_reg_driver;
1045 /* register the USB driver */
1046 result = usb_register(&usb_serial_driver);
1048 err("%s - usb_register failed", __FUNCTION__);
1052 /* register the generic driver, if we should */
1053 result = usb_serial_generic_register(debug);
1055 err("%s - registering generic driver failed", __FUNCTION__);
1064 usb_deregister(&usb_serial_driver);
1067 tty_unregister_driver(usb_serial_tty_driver);
1070 bus_unregister(&usb_serial_bus_type);
1073 err ("%s - returning with error %d", __FUNCTION__, result);
1074 put_tty_driver(usb_serial_tty_driver);
1079 static void __exit usb_serial_exit(void)
1081 usb_serial_console_exit();
1083 usb_serial_generic_deregister();
1085 usb_deregister(&usb_serial_driver);
1086 tty_unregister_driver(usb_serial_tty_driver);
1087 put_tty_driver(usb_serial_tty_driver);
1088 bus_unregister(&usb_serial_bus_type);
1092 module_init(usb_serial_init);
1093 module_exit(usb_serial_exit);
1095 #define set_to_generic_if_null(type, function) \
1097 if (!type->function) { \
1098 type->function = usb_serial_generic_##function; \
1099 dbg("Had to override the " #function \
1100 " usb serial operation with the generic one.");\
1104 static void fixup_generic(struct usb_serial_driver *device)
1106 set_to_generic_if_null(device, open);
1107 set_to_generic_if_null(device, write);
1108 set_to_generic_if_null(device, close);
1109 set_to_generic_if_null(device, write_room);
1110 set_to_generic_if_null(device, chars_in_buffer);
1111 set_to_generic_if_null(device, read_bulk_callback);
1112 set_to_generic_if_null(device, write_bulk_callback);
1113 set_to_generic_if_null(device, shutdown);
1116 int usb_serial_register(struct usb_serial_driver *driver)
1120 fixup_generic(driver);
1122 if (!driver->description)
1123 driver->description = driver->driver.name;
1125 /* Add this device to our list of devices */
1126 list_add(&driver->driver_list, &usb_serial_driver_list);
1128 retval = usb_serial_bus_register(driver);
1130 err("problem %d when registering driver %s", retval, driver->description);
1131 list_del(&driver->driver_list);
1134 info("USB Serial support registered for %s", driver->description);
1140 void usb_serial_deregister(struct usb_serial_driver *device)
1142 info("USB Serial deregistering driver %s", device->description);
1143 list_del(&device->driver_list);
1144 usb_serial_bus_deregister(device);
1149 /* If the usb-serial core is built into the core, the usb-serial drivers
1150 need these symbols to load properly as modules. */
1151 EXPORT_SYMBOL_GPL(usb_serial_register);
1152 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1153 EXPORT_SYMBOL_GPL(usb_serial_probe);
1154 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1155 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1158 /* Module information */
1159 MODULE_AUTHOR( DRIVER_AUTHOR );
1160 MODULE_DESCRIPTION( DRIVER_DESC );
1161 MODULE_LICENSE("GPL");
1163 module_param(debug, bool, S_IRUGO | S_IWUSR);
1164 MODULE_PARM_DESC(debug, "Debug enabled or not");