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/mutex.h>
31 #include <linux/list.h>
32 #include <linux/smp_lock.h>
33 #include <asm/uaccess.h>
34 #include <linux/usb.h>
35 #include "usb-serial.h"
41 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
42 #define DRIVER_DESC "USB Serial Driver core"
44 /* Driver structure we register with the USB core */
45 static struct usb_driver usb_serial_driver = {
47 .probe = usb_serial_probe,
48 .disconnect = usb_serial_disconnect,
52 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
53 the MODULE_DEVICE_TABLE declarations in each serial driver
54 cause the "hotplug" program to pull in whatever module is necessary
55 via modprobe, and modprobe will load usbserial because the serial
60 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
61 static LIST_HEAD(usb_serial_driver_list);
63 struct usb_serial *usb_serial_get_by_index(unsigned index)
65 struct usb_serial *serial = serial_table[index];
68 kref_get(&serial->kref);
72 static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
77 dbg("%s %d", __FUNCTION__, num_ports);
80 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
85 for (j = 1; j <= num_ports-1; ++j)
86 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
95 dbg("%s - minor base = %d", __FUNCTION__, *minor);
96 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
97 serial_table[i] = serial;
103 static void return_serial(struct usb_serial *serial)
107 dbg("%s", __FUNCTION__);
112 for (i = 0; i < serial->num_ports; ++i) {
113 serial_table[serial->minor + i] = NULL;
117 static void destroy_serial(struct kref *kref)
119 struct usb_serial *serial;
120 struct usb_serial_port *port;
123 serial = to_usb_serial(kref);
125 dbg("%s - %s", __FUNCTION__, serial->type->description);
127 serial->type->shutdown(serial);
129 /* return the minor range that this device had */
130 return_serial(serial);
132 for (i = 0; i < serial->num_ports; ++i)
133 serial->port[i]->open_count = 0;
135 /* the ports are cleaned up and released in port_release() */
136 for (i = 0; i < serial->num_ports; ++i)
137 if (serial->port[i]->dev.parent != NULL) {
138 device_unregister(&serial->port[i]->dev);
139 serial->port[i] = NULL;
142 /* If this is a "fake" port, we have to clean it up here, as it will
143 * not get cleaned up in port_release() as it was never registered with
145 if (serial->num_ports < serial->num_port_pointers) {
146 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
147 port = serial->port[i];
150 usb_kill_urb(port->read_urb);
151 usb_free_urb(port->read_urb);
152 usb_kill_urb(port->write_urb);
153 usb_free_urb(port->write_urb);
154 usb_kill_urb(port->interrupt_in_urb);
155 usb_free_urb(port->interrupt_in_urb);
156 usb_kill_urb(port->interrupt_out_urb);
157 usb_free_urb(port->interrupt_out_urb);
158 kfree(port->bulk_in_buffer);
159 kfree(port->bulk_out_buffer);
160 kfree(port->interrupt_in_buffer);
161 kfree(port->interrupt_out_buffer);
165 usb_put_dev(serial->dev);
167 /* free up any memory that we allocated */
171 void usb_serial_put(struct usb_serial *serial)
173 kref_put(&serial->kref, destroy_serial);
176 /*****************************************************************************
177 * Driver tty interface functions
178 *****************************************************************************/
179 static int serial_open (struct tty_struct *tty, struct file * filp)
181 struct usb_serial *serial;
182 struct usb_serial_port *port;
183 unsigned int portNumber;
186 dbg("%s", __FUNCTION__);
188 /* get the serial object associated with this tty pointer */
189 serial = usb_serial_get_by_index(tty->index);
191 tty->driver_data = NULL;
195 portNumber = tty->index - serial->minor;
196 port = serial->port[portNumber];
199 goto bailout_kref_put;
202 if (mutex_lock_interruptible(&port->mutex)) {
203 retval = -ERESTARTSYS;
204 goto bailout_kref_put;
209 /* set up our port structure making the tty driver
210 * remember our port object, and us it */
211 tty->driver_data = port;
214 if (port->open_count == 1) {
216 /* lock this module before we call it
217 * this may fail, which means we must bail out,
218 * safe because we are called with BKL held */
219 if (!try_module_get(serial->type->driver.owner)) {
221 goto bailout_mutex_unlock;
224 /* only call the device specific open if this
225 * is the first time the port is opened */
226 retval = serial->type->open(port, filp);
228 goto bailout_module_put;
231 mutex_unlock(&port->mutex);
235 module_put(serial->type->driver.owner);
236 bailout_mutex_unlock:
237 port->open_count = 0;
238 mutex_unlock(&port->mutex);
240 usb_serial_put(serial);
244 static void serial_close(struct tty_struct *tty, struct file * filp)
246 struct usb_serial_port *port = tty->driver_data;
251 dbg("%s - port %d", __FUNCTION__, port->number);
253 mutex_lock(&port->mutex);
255 if (port->open_count == 0) {
256 mutex_unlock(&port->mutex);
261 if (port->open_count == 0) {
262 /* only call the device specific close if this
263 * port is being closed by the last owner */
264 port->serial->type->close(port, filp);
267 if (port->tty->driver_data)
268 port->tty->driver_data = NULL;
272 module_put(port->serial->type->driver.owner);
275 mutex_unlock(&port->mutex);
276 usb_serial_put(port->serial);
279 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
281 struct usb_serial_port *port = tty->driver_data;
282 int retval = -EINVAL;
284 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
287 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
289 if (!port->open_count) {
290 dbg("%s - port not opened", __FUNCTION__);
294 /* pass on to the driver specific version of this function */
295 retval = port->serial->type->write(port, buf, count);
301 static int serial_write_room (struct tty_struct *tty)
303 struct usb_serial_port *port = tty->driver_data;
304 int retval = -EINVAL;
309 dbg("%s - port %d", __FUNCTION__, port->number);
311 if (!port->open_count) {
312 dbg("%s - port not open", __FUNCTION__);
316 /* pass on to the driver specific version of this function */
317 retval = port->serial->type->write_room(port);
323 static int serial_chars_in_buffer (struct tty_struct *tty)
325 struct usb_serial_port *port = tty->driver_data;
326 int retval = -EINVAL;
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 retval = port->serial->type->chars_in_buffer(port);
345 static void serial_throttle (struct tty_struct * tty)
347 struct usb_serial_port *port = tty->driver_data;
352 dbg("%s - port %d", __FUNCTION__, port->number);
354 if (!port->open_count) {
355 dbg ("%s - port not open", __FUNCTION__);
359 /* pass on to the driver specific version of this function */
360 if (port->serial->type->throttle)
361 port->serial->type->throttle(port);
364 static void serial_unthrottle (struct tty_struct * tty)
366 struct usb_serial_port *port = tty->driver_data;
371 dbg("%s - port %d", __FUNCTION__, port->number);
373 if (!port->open_count) {
374 dbg("%s - port not open", __FUNCTION__);
378 /* pass on to the driver specific version of this function */
379 if (port->serial->type->unthrottle)
380 port->serial->type->unthrottle(port);
383 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
385 struct usb_serial_port *port = tty->driver_data;
386 int retval = -ENODEV;
391 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
393 if (!port->open_count) {
394 dbg ("%s - port not open", __FUNCTION__);
398 /* pass on to the driver specific version of this function if it is available */
399 if (port->serial->type->ioctl)
400 retval = port->serial->type->ioctl(port, file, cmd, arg);
402 retval = -ENOIOCTLCMD;
408 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
410 struct usb_serial_port *port = tty->driver_data;
415 dbg("%s - port %d", __FUNCTION__, port->number);
417 if (!port->open_count) {
418 dbg("%s - port not open", __FUNCTION__);
422 /* pass on to the driver specific version of this function if it is available */
423 if (port->serial->type->set_termios)
424 port->serial->type->set_termios(port, old);
427 static void serial_break (struct tty_struct *tty, int break_state)
429 struct usb_serial_port *port = tty->driver_data;
434 dbg("%s - port %d", __FUNCTION__, port->number);
436 if (!port->open_count) {
437 dbg("%s - port not open", __FUNCTION__);
441 /* pass on to the driver specific version of this function if it is available */
442 if (port->serial->type->break_ctl)
443 port->serial->type->break_ctl(port, break_state);
446 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
448 struct usb_serial *serial;
454 dbg("%s", __FUNCTION__);
455 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
456 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
457 serial = usb_serial_get_by_index(i);
461 length += sprintf (page+length, "%d:", i);
462 if (serial->type->driver.owner)
463 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
464 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
465 length += sprintf (page+length, " vendor:%04x product:%04x",
466 le16_to_cpu(serial->dev->descriptor.idVendor),
467 le16_to_cpu(serial->dev->descriptor.idProduct));
468 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
469 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
471 usb_make_path(serial->dev, tmp, sizeof(tmp));
472 length += sprintf (page+length, " path:%s", tmp);
474 length += sprintf (page+length, "\n");
475 if ((length + begin) > (off + count))
477 if ((length + begin) < off) {
481 usb_serial_put(serial);
485 if (off >= (length + begin))
487 *start = page + (off-begin);
488 return ((count < begin+length-off) ? count : begin+length-off);
491 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
493 struct usb_serial_port *port = tty->driver_data;
498 dbg("%s - port %d", __FUNCTION__, port->number);
500 if (!port->open_count) {
501 dbg("%s - port not open", __FUNCTION__);
505 if (port->serial->type->tiocmget)
506 return port->serial->type->tiocmget(port, file);
512 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
513 unsigned int set, unsigned int clear)
515 struct usb_serial_port *port = tty->driver_data;
520 dbg("%s - port %d", __FUNCTION__, port->number);
522 if (!port->open_count) {
523 dbg("%s - port not open", __FUNCTION__);
527 if (port->serial->type->tiocmset)
528 return port->serial->type->tiocmset(port, file, set, clear);
534 void usb_serial_port_softint(void *private)
536 struct usb_serial_port *port = private;
537 struct tty_struct *tty;
539 dbg("%s - port %d", __FUNCTION__, port->number);
551 static void port_release(struct device *dev)
553 struct usb_serial_port *port = to_usb_serial_port(dev);
555 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
556 usb_kill_urb(port->read_urb);
557 usb_free_urb(port->read_urb);
558 usb_kill_urb(port->write_urb);
559 usb_free_urb(port->write_urb);
560 usb_kill_urb(port->interrupt_in_urb);
561 usb_free_urb(port->interrupt_in_urb);
562 usb_kill_urb(port->interrupt_out_urb);
563 usb_free_urb(port->interrupt_out_urb);
564 kfree(port->bulk_in_buffer);
565 kfree(port->bulk_out_buffer);
566 kfree(port->interrupt_in_buffer);
567 kfree(port->interrupt_out_buffer);
571 static struct usb_serial * create_serial (struct usb_device *dev,
572 struct usb_interface *interface,
573 struct usb_serial_driver *driver)
575 struct usb_serial *serial;
577 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
579 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
582 serial->dev = usb_get_dev(dev);
583 serial->type = driver;
584 serial->interface = interface;
585 kref_init(&serial->kref);
590 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
593 const struct usb_device_id *id;
594 struct usb_serial_driver *t;
596 /* Check if the usb id matches a known device */
597 list_for_each(p, &usb_serial_driver_list) {
598 t = list_entry(p, struct usb_serial_driver, driver_list);
599 id = usb_match_id(iface, t->id_table);
601 dbg("descriptor matches");
609 int usb_serial_probe(struct usb_interface *interface,
610 const struct usb_device_id *id)
612 struct usb_device *dev = interface_to_usbdev (interface);
613 struct usb_serial *serial = NULL;
614 struct usb_serial_port *port;
615 struct usb_host_interface *iface_desc;
616 struct usb_endpoint_descriptor *endpoint;
617 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
618 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
619 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
620 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
621 struct usb_serial_driver *type = NULL;
626 int num_interrupt_in = 0;
627 int num_interrupt_out = 0;
629 int num_bulk_out = 0;
633 type = search_serial_device(interface);
639 serial = create_serial (dev, interface, type);
641 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
645 /* if this device type has a probe function, call it */
647 const struct usb_device_id *id;
649 if (!try_module_get(type->driver.owner)) {
650 dev_err(&interface->dev, "module get failed, exiting\n");
655 id = usb_match_id(interface, type->id_table);
656 retval = type->probe(serial, id);
657 module_put(type->driver.owner);
660 dbg ("sub driver rejected device");
666 /* descriptor matches, let's find the endpoints needed */
667 /* check out the endpoints */
668 iface_desc = interface->cur_altsetting;
669 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
670 endpoint = &iface_desc->endpoint[i].desc;
672 if ((endpoint->bEndpointAddress & 0x80) &&
673 ((endpoint->bmAttributes & 3) == 0x02)) {
674 /* we found a bulk in endpoint */
675 dbg("found bulk in on endpoint %d", i);
676 bulk_in_endpoint[num_bulk_in] = endpoint;
680 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
681 ((endpoint->bmAttributes & 3) == 0x02)) {
682 /* we found a bulk out endpoint */
683 dbg("found bulk out on endpoint %d", i);
684 bulk_out_endpoint[num_bulk_out] = endpoint;
688 if ((endpoint->bEndpointAddress & 0x80) &&
689 ((endpoint->bmAttributes & 3) == 0x03)) {
690 /* we found a interrupt in endpoint */
691 dbg("found interrupt in on endpoint %d", i);
692 interrupt_in_endpoint[num_interrupt_in] = endpoint;
696 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
697 ((endpoint->bmAttributes & 3) == 0x03)) {
698 /* we found an interrupt out endpoint */
699 dbg("found interrupt out on endpoint %d", i);
700 interrupt_out_endpoint[num_interrupt_out] = endpoint;
705 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
706 /* BEGIN HORRIBLE HACK FOR PL2303 */
707 /* this is needed due to the looney way its endpoints are set up */
708 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
709 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
710 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
711 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
712 if (interface != dev->actconfig->interface[0]) {
713 /* check out the endpoints of the other interface*/
714 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
715 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
716 endpoint = &iface_desc->endpoint[i].desc;
717 if ((endpoint->bEndpointAddress & 0x80) &&
718 ((endpoint->bmAttributes & 3) == 0x03)) {
719 /* we found a interrupt in endpoint */
720 dbg("found interrupt in for Prolific device on separate interface");
721 interrupt_in_endpoint[num_interrupt_in] = endpoint;
727 /* Now make sure the PL-2303 is configured correctly.
728 * If not, give up now and hope this hack will work
729 * properly during a later invocation of usb_serial_probe
731 if (num_bulk_in == 0 || num_bulk_out == 0) {
732 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
737 /* END HORRIBLE HACK FOR PL2303 */
740 /* found all that we need */
741 dev_info(&interface->dev, "%s converter detected\n", type->description);
743 #ifdef CONFIG_USB_SERIAL_GENERIC
744 if (type == &usb_serial_generic_device) {
745 num_ports = num_bulk_out;
746 if (num_ports == 0) {
747 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
754 /* if this device type has a calc_num_ports function, call it */
755 if (type->calc_num_ports) {
756 if (!try_module_get(type->driver.owner)) {
757 dev_err(&interface->dev, "module get failed, exiting\n");
761 num_ports = type->calc_num_ports (serial);
762 module_put(type->driver.owner);
765 num_ports = type->num_ports;
768 if (get_free_serial (serial, num_ports, &minor) == NULL) {
769 dev_err(&interface->dev, "No more free serial devices\n");
774 serial->minor = minor;
775 serial->num_ports = num_ports;
776 serial->num_bulk_in = num_bulk_in;
777 serial->num_bulk_out = num_bulk_out;
778 serial->num_interrupt_in = num_interrupt_in;
779 serial->num_interrupt_out = num_interrupt_out;
781 /* create our ports, we need as many as the max endpoints */
782 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
783 max_endpoints = max(num_bulk_in, num_bulk_out);
784 max_endpoints = max(max_endpoints, num_interrupt_in);
785 max_endpoints = max(max_endpoints, num_interrupt_out);
786 max_endpoints = max(max_endpoints, (int)serial->num_ports);
787 serial->num_port_pointers = max_endpoints;
788 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
789 for (i = 0; i < max_endpoints; ++i) {
790 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
793 port->number = i + serial->minor;
794 port->serial = serial;
795 spin_lock_init(&port->lock);
796 mutex_init(&port->mutex);
797 INIT_WORK(&port->work, usb_serial_port_softint, port);
798 serial->port[i] = port;
801 /* set up the endpoint information */
802 for (i = 0; i < num_bulk_in; ++i) {
803 endpoint = bulk_in_endpoint[i];
804 port = serial->port[i];
805 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
806 if (!port->read_urb) {
807 dev_err(&interface->dev, "No free urbs available\n");
810 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
811 port->bulk_in_size = buffer_size;
812 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
813 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
814 if (!port->bulk_in_buffer) {
815 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
818 usb_fill_bulk_urb (port->read_urb, dev,
819 usb_rcvbulkpipe (dev,
820 endpoint->bEndpointAddress),
821 port->bulk_in_buffer, buffer_size,
822 serial->type->read_bulk_callback,
826 for (i = 0; i < num_bulk_out; ++i) {
827 endpoint = bulk_out_endpoint[i];
828 port = serial->port[i];
829 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
830 if (!port->write_urb) {
831 dev_err(&interface->dev, "No free urbs available\n");
834 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
835 port->bulk_out_size = buffer_size;
836 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
837 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
838 if (!port->bulk_out_buffer) {
839 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
842 usb_fill_bulk_urb (port->write_urb, dev,
843 usb_sndbulkpipe (dev,
844 endpoint->bEndpointAddress),
845 port->bulk_out_buffer, buffer_size,
846 serial->type->write_bulk_callback,
850 if (serial->type->read_int_callback) {
851 for (i = 0; i < num_interrupt_in; ++i) {
852 endpoint = interrupt_in_endpoint[i];
853 port = serial->port[i];
854 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
855 if (!port->interrupt_in_urb) {
856 dev_err(&interface->dev, "No free urbs available\n");
859 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
860 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
861 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
862 if (!port->interrupt_in_buffer) {
863 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
866 usb_fill_int_urb (port->interrupt_in_urb, dev,
868 endpoint->bEndpointAddress),
869 port->interrupt_in_buffer, buffer_size,
870 serial->type->read_int_callback, port,
871 endpoint->bInterval);
873 } else if (num_interrupt_in) {
874 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
877 if (serial->type->write_int_callback) {
878 for (i = 0; i < num_interrupt_out; ++i) {
879 endpoint = interrupt_out_endpoint[i];
880 port = serial->port[i];
881 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
882 if (!port->interrupt_out_urb) {
883 dev_err(&interface->dev, "No free urbs available\n");
886 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
887 port->interrupt_out_size = buffer_size;
888 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
889 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
890 if (!port->interrupt_out_buffer) {
891 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
894 usb_fill_int_urb (port->interrupt_out_urb, dev,
896 endpoint->bEndpointAddress),
897 port->interrupt_out_buffer, buffer_size,
898 serial->type->write_int_callback, port,
899 endpoint->bInterval);
901 } else if (num_interrupt_out) {
902 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
905 /* if this device type has an attach function, call it */
907 if (!try_module_get(type->driver.owner)) {
908 dev_err(&interface->dev, "module get failed, exiting\n");
911 retval = type->attach (serial);
912 module_put(type->driver.owner);
916 /* quietly accept this device, but don't bind to a serial port
917 * as it's about to disappear */
922 /* register all of the individual ports with the driver core */
923 for (i = 0; i < num_ports; ++i) {
924 port = serial->port[i];
925 port->dev.parent = &interface->dev;
926 port->dev.driver = NULL;
927 port->dev.bus = &usb_serial_bus_type;
928 port->dev.release = &port_release;
930 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
931 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
932 device_register (&port->dev);
935 usb_serial_console_init (debug, minor);
939 usb_set_intfdata (interface, serial);
943 for (i = 0; i < num_bulk_in; ++i) {
944 port = serial->port[i];
948 usb_free_urb (port->read_urb);
949 kfree(port->bulk_in_buffer);
951 for (i = 0; i < num_bulk_out; ++i) {
952 port = serial->port[i];
956 usb_free_urb (port->write_urb);
957 kfree(port->bulk_out_buffer);
959 for (i = 0; i < num_interrupt_in; ++i) {
960 port = serial->port[i];
963 if (port->interrupt_in_urb)
964 usb_free_urb (port->interrupt_in_urb);
965 kfree(port->interrupt_in_buffer);
967 for (i = 0; i < num_interrupt_out; ++i) {
968 port = serial->port[i];
971 if (port->interrupt_out_urb)
972 usb_free_urb (port->interrupt_out_urb);
973 kfree(port->interrupt_out_buffer);
976 /* return the minor range that this device had */
977 return_serial (serial);
979 /* free up any memory that we allocated */
980 for (i = 0; i < serial->num_port_pointers; ++i)
981 kfree(serial->port[i]);
986 void usb_serial_disconnect(struct usb_interface *interface)
989 struct usb_serial *serial = usb_get_intfdata (interface);
990 struct device *dev = &interface->dev;
991 struct usb_serial_port *port;
993 usb_serial_console_disconnect(serial);
994 dbg ("%s", __FUNCTION__);
996 usb_set_intfdata (interface, NULL);
998 for (i = 0; i < serial->num_ports; ++i) {
999 port = serial->port[i];
1000 if (port && port->tty)
1001 tty_hangup(port->tty);
1003 /* let the last holder of this object
1004 * cause it to be cleaned up */
1005 usb_serial_put(serial);
1007 dev_info(dev, "device disconnected\n");
1010 static struct tty_operations serial_ops = {
1011 .open = serial_open,
1012 .close = serial_close,
1013 .write = serial_write,
1014 .write_room = serial_write_room,
1015 .ioctl = serial_ioctl,
1016 .set_termios = serial_set_termios,
1017 .throttle = serial_throttle,
1018 .unthrottle = serial_unthrottle,
1019 .break_ctl = serial_break,
1020 .chars_in_buffer = serial_chars_in_buffer,
1021 .read_proc = serial_read_proc,
1022 .tiocmget = serial_tiocmget,
1023 .tiocmset = serial_tiocmset,
1026 struct tty_driver *usb_serial_tty_driver;
1028 static int __init usb_serial_init(void)
1033 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1034 if (!usb_serial_tty_driver)
1037 /* Initialize our global data */
1038 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1039 serial_table[i] = NULL;
1042 result = bus_register(&usb_serial_bus_type);
1044 err("%s - registering bus driver failed", __FUNCTION__);
1048 usb_serial_tty_driver->owner = THIS_MODULE;
1049 usb_serial_tty_driver->driver_name = "usbserial";
1050 usb_serial_tty_driver->devfs_name = "usb/tts/";
1051 usb_serial_tty_driver->name = "ttyUSB";
1052 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1053 usb_serial_tty_driver->minor_start = 0;
1054 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1055 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1056 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1057 usb_serial_tty_driver->init_termios = tty_std_termios;
1058 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1059 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1060 result = tty_register_driver(usb_serial_tty_driver);
1062 err("%s - tty_register_driver failed", __FUNCTION__);
1063 goto exit_reg_driver;
1066 /* register the USB driver */
1067 result = usb_register(&usb_serial_driver);
1069 err("%s - usb_register failed", __FUNCTION__);
1073 /* register the generic driver, if we should */
1074 result = usb_serial_generic_register(debug);
1076 err("%s - registering generic driver failed", __FUNCTION__);
1085 usb_deregister(&usb_serial_driver);
1088 tty_unregister_driver(usb_serial_tty_driver);
1091 bus_unregister(&usb_serial_bus_type);
1094 err ("%s - returning with error %d", __FUNCTION__, result);
1095 put_tty_driver(usb_serial_tty_driver);
1100 static void __exit usb_serial_exit(void)
1102 usb_serial_console_exit();
1104 usb_serial_generic_deregister();
1106 usb_deregister(&usb_serial_driver);
1107 tty_unregister_driver(usb_serial_tty_driver);
1108 put_tty_driver(usb_serial_tty_driver);
1109 bus_unregister(&usb_serial_bus_type);
1113 module_init(usb_serial_init);
1114 module_exit(usb_serial_exit);
1116 #define set_to_generic_if_null(type, function) \
1118 if (!type->function) { \
1119 type->function = usb_serial_generic_##function; \
1120 dbg("Had to override the " #function \
1121 " usb serial operation with the generic one.");\
1125 static void fixup_generic(struct usb_serial_driver *device)
1127 set_to_generic_if_null(device, open);
1128 set_to_generic_if_null(device, write);
1129 set_to_generic_if_null(device, close);
1130 set_to_generic_if_null(device, write_room);
1131 set_to_generic_if_null(device, chars_in_buffer);
1132 set_to_generic_if_null(device, read_bulk_callback);
1133 set_to_generic_if_null(device, write_bulk_callback);
1134 set_to_generic_if_null(device, shutdown);
1137 int usb_serial_register(struct usb_serial_driver *driver)
1141 fixup_generic(driver);
1143 if (!driver->description)
1144 driver->description = driver->driver.name;
1146 /* Add this device to our list of devices */
1147 list_add(&driver->driver_list, &usb_serial_driver_list);
1149 retval = usb_serial_bus_register(driver);
1151 err("problem %d when registering driver %s", retval, driver->description);
1152 list_del(&driver->driver_list);
1155 info("USB Serial support registered for %s", driver->description);
1161 void usb_serial_deregister(struct usb_serial_driver *device)
1163 info("USB Serial deregistering driver %s", device->description);
1164 list_del(&device->driver_list);
1165 usb_serial_bus_deregister(device);
1170 /* If the usb-serial core is built into the core, the usb-serial drivers
1171 need these symbols to load properly as modules. */
1172 EXPORT_SYMBOL_GPL(usb_serial_register);
1173 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1174 EXPORT_SYMBOL_GPL(usb_serial_probe);
1175 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1176 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1179 /* Module information */
1180 MODULE_AUTHOR( DRIVER_AUTHOR );
1181 MODULE_DESCRIPTION( DRIVER_DESC );
1182 MODULE_LICENSE("GPL");
1184 module_param(debug, bool, S_IRUGO | S_IWUSR);
1185 MODULE_PARM_DESC(debug, "Debug enabled or not");