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 = {
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 /*****************************************************************************
172 * Driver tty interface functions
173 *****************************************************************************/
174 static int serial_open (struct tty_struct *tty, struct file * filp)
176 struct usb_serial *serial;
177 struct usb_serial_port *port;
178 unsigned int portNumber;
181 dbg("%s", __FUNCTION__);
183 /* get the serial object associated with this tty pointer */
184 serial = usb_serial_get_by_index(tty->index);
186 tty->driver_data = NULL;
190 portNumber = tty->index - serial->minor;
191 port = serial->port[portNumber];
195 if (port->open_count == 1) {
197 /* set up our port structure making the tty driver
198 * remember our port object, and us it */
199 tty->driver_data = port;
202 /* lock this module before we call it
203 * this may fail, which means we must bail out,
204 * safe because we are called with BKL held */
205 if (!try_module_get(serial->type->driver.owner)) {
207 goto bailout_kref_put;
210 /* only call the device specific open if this
211 * is the first time the port is opened */
212 retval = serial->type->open(port, filp);
214 goto bailout_module_put;
220 module_put(serial->type->driver.owner);
222 kref_put(&serial->kref, destroy_serial);
223 port->open_count = 0;
227 static void serial_close(struct tty_struct *tty, struct file * filp)
229 struct usb_serial_port *port = tty->driver_data;
234 dbg("%s - port %d", __FUNCTION__, port->number);
236 if (port->open_count == 0)
240 if (port->open_count == 0) {
241 /* only call the device specific close if this
242 * port is being closed by the last owner */
243 port->serial->type->close(port, filp);
246 if (port->tty->driver_data)
247 port->tty->driver_data = NULL;
251 module_put(port->serial->type->driver.owner);
254 kref_put(&port->serial->kref, destroy_serial);
257 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
259 struct usb_serial_port *port = tty->driver_data;
260 int retval = -EINVAL;
262 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
264 if (!port->open_count) {
265 dbg("%s - port not opened", __FUNCTION__);
269 /* pass on to the driver specific version of this function */
270 retval = port->serial->type->write(port, buf, count);
276 static int serial_write_room (struct tty_struct *tty)
278 struct usb_serial_port *port = tty->driver_data;
279 int retval = -EINVAL;
281 dbg("%s - port %d", __FUNCTION__, port->number);
283 if (!port->open_count) {
284 dbg("%s - port not open", __FUNCTION__);
288 /* pass on to the driver specific version of this function */
289 retval = port->serial->type->write_room(port);
295 static int serial_chars_in_buffer (struct tty_struct *tty)
297 struct usb_serial_port *port = tty->driver_data;
298 int retval = -EINVAL;
300 dbg("%s = port %d", __FUNCTION__, port->number);
302 if (!port->open_count) {
303 dbg("%s - port not open", __FUNCTION__);
307 /* pass on to the driver specific version of this function */
308 retval = port->serial->type->chars_in_buffer(port);
314 static void serial_throttle (struct tty_struct * tty)
316 struct usb_serial_port *port = tty->driver_data;
318 dbg("%s - port %d", __FUNCTION__, port->number);
320 if (!port->open_count) {
321 dbg ("%s - port not open", __FUNCTION__);
325 /* pass on to the driver specific version of this function */
326 if (port->serial->type->throttle)
327 port->serial->type->throttle(port);
330 static void serial_unthrottle (struct tty_struct * tty)
332 struct usb_serial_port *port = tty->driver_data;
334 dbg("%s - port %d", __FUNCTION__, port->number);
336 if (!port->open_count) {
337 dbg("%s - port not open", __FUNCTION__);
341 /* pass on to the driver specific version of this function */
342 if (port->serial->type->unthrottle)
343 port->serial->type->unthrottle(port);
346 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
348 struct usb_serial_port *port = tty->driver_data;
349 int retval = -ENODEV;
351 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
353 if (!port->open_count) {
354 dbg ("%s - port not open", __FUNCTION__);
358 /* pass on to the driver specific version of this function if it is available */
359 if (port->serial->type->ioctl)
360 retval = port->serial->type->ioctl(port, file, cmd, arg);
362 retval = -ENOIOCTLCMD;
368 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
370 struct usb_serial_port *port = tty->driver_data;
372 dbg("%s - port %d", __FUNCTION__, port->number);
374 if (!port->open_count) {
375 dbg("%s - port not open", __FUNCTION__);
379 /* pass on to the driver specific version of this function if it is available */
380 if (port->serial->type->set_termios)
381 port->serial->type->set_termios(port, old);
384 static void serial_break (struct tty_struct *tty, int break_state)
386 struct usb_serial_port *port = tty->driver_data;
388 dbg("%s - port %d", __FUNCTION__, port->number);
390 if (!port->open_count) {
391 dbg("%s - port not open", __FUNCTION__);
395 /* pass on to the driver specific version of this function if it is available */
396 if (port->serial->type->break_ctl)
397 port->serial->type->break_ctl(port, break_state);
400 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
402 struct usb_serial *serial;
408 dbg("%s", __FUNCTION__);
409 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
410 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
411 serial = usb_serial_get_by_index(i);
415 length += sprintf (page+length, "%d:", i);
416 if (serial->type->driver.owner)
417 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
418 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
419 length += sprintf (page+length, " vendor:%04x product:%04x",
420 le16_to_cpu(serial->dev->descriptor.idVendor),
421 le16_to_cpu(serial->dev->descriptor.idProduct));
422 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
423 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
425 usb_make_path(serial->dev, tmp, sizeof(tmp));
426 length += sprintf (page+length, " path:%s", tmp);
428 length += sprintf (page+length, "\n");
429 if ((length + begin) > (off + count))
431 if ((length + begin) < off) {
435 kref_put(&serial->kref, destroy_serial);
439 if (off >= (length + begin))
441 *start = page + (off-begin);
442 return ((count < begin+length-off) ? count : begin+length-off);
445 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
447 struct usb_serial_port *port = tty->driver_data;
449 dbg("%s - port %d", __FUNCTION__, port->number);
451 if (!port->open_count) {
452 dbg("%s - port not open", __FUNCTION__);
456 if (port->serial->type->tiocmget)
457 return port->serial->type->tiocmget(port, file);
463 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
464 unsigned int set, unsigned int clear)
466 struct usb_serial_port *port = tty->driver_data;
468 dbg("%s - port %d", __FUNCTION__, port->number);
470 if (!port->open_count) {
471 dbg("%s - port not open", __FUNCTION__);
475 if (port->serial->type->tiocmset)
476 return port->serial->type->tiocmset(port, file, set, clear);
482 void usb_serial_port_softint(void *private)
484 struct usb_serial_port *port = private;
485 struct tty_struct *tty;
487 dbg("%s - port %d", __FUNCTION__, port->number);
499 static void port_release(struct device *dev)
501 struct usb_serial_port *port = to_usb_serial_port(dev);
503 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
504 usb_kill_urb(port->read_urb);
505 usb_free_urb(port->read_urb);
506 usb_kill_urb(port->write_urb);
507 usb_free_urb(port->write_urb);
508 usb_kill_urb(port->interrupt_in_urb);
509 usb_free_urb(port->interrupt_in_urb);
510 usb_kill_urb(port->interrupt_out_urb);
511 usb_free_urb(port->interrupt_out_urb);
512 kfree(port->bulk_in_buffer);
513 kfree(port->bulk_out_buffer);
514 kfree(port->interrupt_in_buffer);
515 kfree(port->interrupt_out_buffer);
519 static struct usb_serial * create_serial (struct usb_device *dev,
520 struct usb_interface *interface,
521 struct usb_serial_driver *driver)
523 struct usb_serial *serial;
525 serial = kmalloc (sizeof (*serial), GFP_KERNEL);
527 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
530 memset (serial, 0, sizeof(*serial));
531 serial->dev = usb_get_dev(dev);
532 serial->type = driver;
533 serial->interface = interface;
534 kref_init(&serial->kref);
539 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
542 const struct usb_device_id *id;
543 struct usb_serial_driver *t;
545 /* List trough know devices and see if the usb id matches */
546 list_for_each(p, &usb_serial_driver_list) {
547 t = list_entry(p, struct usb_serial_driver, driver_list);
548 id = usb_match_id(iface, t->id_table);
550 dbg("descriptor matches");
558 int usb_serial_probe(struct usb_interface *interface,
559 const struct usb_device_id *id)
561 struct usb_device *dev = interface_to_usbdev (interface);
562 struct usb_serial *serial = NULL;
563 struct usb_serial_port *port;
564 struct usb_host_interface *iface_desc;
565 struct usb_endpoint_descriptor *endpoint;
566 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
567 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
568 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
569 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
570 struct usb_serial_driver *type = NULL;
575 int num_interrupt_in = 0;
576 int num_interrupt_out = 0;
578 int num_bulk_out = 0;
582 type = search_serial_device(interface);
588 serial = create_serial (dev, interface, type);
590 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
594 /* if this device type has a probe function, call it */
596 const struct usb_device_id *id;
598 if (!try_module_get(type->driver.owner)) {
599 dev_err(&interface->dev, "module get failed, exiting\n");
604 id = usb_match_id(interface, type->id_table);
605 retval = type->probe(serial, id);
606 module_put(type->driver.owner);
609 dbg ("sub driver rejected device");
615 /* descriptor matches, let's find the endpoints needed */
616 /* check out the endpoints */
617 iface_desc = interface->cur_altsetting;
618 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
619 endpoint = &iface_desc->endpoint[i].desc;
621 if ((endpoint->bEndpointAddress & 0x80) &&
622 ((endpoint->bmAttributes & 3) == 0x02)) {
623 /* we found a bulk in endpoint */
624 dbg("found bulk in on endpoint %d", i);
625 bulk_in_endpoint[num_bulk_in] = endpoint;
629 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
630 ((endpoint->bmAttributes & 3) == 0x02)) {
631 /* we found a bulk out endpoint */
632 dbg("found bulk out on endpoint %d", i);
633 bulk_out_endpoint[num_bulk_out] = endpoint;
637 if ((endpoint->bEndpointAddress & 0x80) &&
638 ((endpoint->bmAttributes & 3) == 0x03)) {
639 /* we found a interrupt in endpoint */
640 dbg("found interrupt in on endpoint %d", i);
641 interrupt_in_endpoint[num_interrupt_in] = endpoint;
645 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
646 ((endpoint->bmAttributes & 3) == 0x03)) {
647 /* we found an interrupt out endpoint */
648 dbg("found interrupt out on endpoint %d", i);
649 interrupt_out_endpoint[num_interrupt_out] = endpoint;
654 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
655 /* BEGIN HORRIBLE HACK FOR PL2303 */
656 /* this is needed due to the looney way its endpoints are set up */
657 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
658 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
659 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
660 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
661 if (interface != dev->actconfig->interface[0]) {
662 /* check out the endpoints of the other interface*/
663 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
664 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
665 endpoint = &iface_desc->endpoint[i].desc;
666 if ((endpoint->bEndpointAddress & 0x80) &&
667 ((endpoint->bmAttributes & 3) == 0x03)) {
668 /* we found a interrupt in endpoint */
669 dbg("found interrupt in for Prolific device on separate interface");
670 interrupt_in_endpoint[num_interrupt_in] = endpoint;
676 /* Now make sure the PL-2303 is configured correctly.
677 * If not, give up now and hope this hack will work
678 * properly during a later invocation of usb_serial_probe
680 if (num_bulk_in == 0 || num_bulk_out == 0) {
681 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
686 /* END HORRIBLE HACK FOR PL2303 */
689 /* found all that we need */
690 dev_info(&interface->dev, "%s converter detected\n", type->description);
692 #ifdef CONFIG_USB_SERIAL_GENERIC
693 if (type == &usb_serial_generic_device) {
694 num_ports = num_bulk_out;
695 if (num_ports == 0) {
696 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
703 /* if this device type has a calc_num_ports function, call it */
704 if (type->calc_num_ports) {
705 if (!try_module_get(type->driver.owner)) {
706 dev_err(&interface->dev, "module get failed, exiting\n");
710 num_ports = type->calc_num_ports (serial);
711 module_put(type->driver.owner);
714 num_ports = type->num_ports;
717 if (get_free_serial (serial, num_ports, &minor) == NULL) {
718 dev_err(&interface->dev, "No more free serial devices\n");
723 serial->minor = minor;
724 serial->num_ports = num_ports;
725 serial->num_bulk_in = num_bulk_in;
726 serial->num_bulk_out = num_bulk_out;
727 serial->num_interrupt_in = num_interrupt_in;
728 serial->num_interrupt_out = num_interrupt_out;
730 /* create our ports, we need as many as the max endpoints */
731 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
732 max_endpoints = max(num_bulk_in, num_bulk_out);
733 max_endpoints = max(max_endpoints, num_interrupt_in);
734 max_endpoints = max(max_endpoints, num_interrupt_out);
735 max_endpoints = max(max_endpoints, (int)serial->num_ports);
736 serial->num_port_pointers = max_endpoints;
737 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
738 for (i = 0; i < max_endpoints; ++i) {
739 port = kmalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
742 memset(port, 0x00, sizeof(struct usb_serial_port));
743 port->number = i + serial->minor;
744 port->serial = serial;
745 spin_lock_init(&port->lock);
746 INIT_WORK(&port->work, usb_serial_port_softint, port);
747 serial->port[i] = port;
750 /* set up the endpoint information */
751 for (i = 0; i < num_bulk_in; ++i) {
752 endpoint = bulk_in_endpoint[i];
753 port = serial->port[i];
754 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
755 if (!port->read_urb) {
756 dev_err(&interface->dev, "No free urbs available\n");
759 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
760 port->bulk_in_size = buffer_size;
761 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
762 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
763 if (!port->bulk_in_buffer) {
764 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
767 usb_fill_bulk_urb (port->read_urb, dev,
768 usb_rcvbulkpipe (dev,
769 endpoint->bEndpointAddress),
770 port->bulk_in_buffer, buffer_size,
771 serial->type->read_bulk_callback,
775 for (i = 0; i < num_bulk_out; ++i) {
776 endpoint = bulk_out_endpoint[i];
777 port = serial->port[i];
778 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
779 if (!port->write_urb) {
780 dev_err(&interface->dev, "No free urbs available\n");
783 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
784 port->bulk_out_size = buffer_size;
785 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
786 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
787 if (!port->bulk_out_buffer) {
788 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
791 usb_fill_bulk_urb (port->write_urb, dev,
792 usb_sndbulkpipe (dev,
793 endpoint->bEndpointAddress),
794 port->bulk_out_buffer, buffer_size,
795 serial->type->write_bulk_callback,
799 if (serial->type->read_int_callback) {
800 for (i = 0; i < num_interrupt_in; ++i) {
801 endpoint = interrupt_in_endpoint[i];
802 port = serial->port[i];
803 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
804 if (!port->interrupt_in_urb) {
805 dev_err(&interface->dev, "No free urbs available\n");
808 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
809 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
810 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
811 if (!port->interrupt_in_buffer) {
812 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
815 usb_fill_int_urb (port->interrupt_in_urb, dev,
817 endpoint->bEndpointAddress),
818 port->interrupt_in_buffer, buffer_size,
819 serial->type->read_int_callback, port,
820 endpoint->bInterval);
822 } else if (num_interrupt_in) {
823 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
826 if (serial->type->write_int_callback) {
827 for (i = 0; i < num_interrupt_out; ++i) {
828 endpoint = interrupt_out_endpoint[i];
829 port = serial->port[i];
830 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
831 if (!port->interrupt_out_urb) {
832 dev_err(&interface->dev, "No free urbs available\n");
835 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
836 port->interrupt_out_size = buffer_size;
837 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
838 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
839 if (!port->interrupt_out_buffer) {
840 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
843 usb_fill_int_urb (port->interrupt_out_urb, dev,
845 endpoint->bEndpointAddress),
846 port->interrupt_out_buffer, buffer_size,
847 serial->type->write_int_callback, port,
848 endpoint->bInterval);
850 } else if (num_interrupt_out) {
851 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
854 /* if this device type has an attach function, call it */
856 if (!try_module_get(type->driver.owner)) {
857 dev_err(&interface->dev, "module get failed, exiting\n");
860 retval = type->attach (serial);
861 module_put(type->driver.owner);
865 /* quietly accept this device, but don't bind to a serial port
866 * as it's about to disappear */
871 /* register all of the individual ports with the driver core */
872 for (i = 0; i < num_ports; ++i) {
873 port = serial->port[i];
874 port->dev.parent = &interface->dev;
875 port->dev.driver = NULL;
876 port->dev.bus = &usb_serial_bus_type;
877 port->dev.release = &port_release;
879 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
880 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
881 device_register (&port->dev);
884 usb_serial_console_init (debug, minor);
888 usb_set_intfdata (interface, serial);
892 for (i = 0; i < num_bulk_in; ++i) {
893 port = serial->port[i];
897 usb_free_urb (port->read_urb);
898 kfree(port->bulk_in_buffer);
900 for (i = 0; i < num_bulk_out; ++i) {
901 port = serial->port[i];
905 usb_free_urb (port->write_urb);
906 kfree(port->bulk_out_buffer);
908 for (i = 0; i < num_interrupt_in; ++i) {
909 port = serial->port[i];
912 if (port->interrupt_in_urb)
913 usb_free_urb (port->interrupt_in_urb);
914 kfree(port->interrupt_in_buffer);
916 for (i = 0; i < num_interrupt_out; ++i) {
917 port = serial->port[i];
920 if (port->interrupt_out_urb)
921 usb_free_urb (port->interrupt_out_urb);
922 kfree(port->interrupt_out_buffer);
925 /* return the minor range that this device had */
926 return_serial (serial);
928 /* free up any memory that we allocated */
929 for (i = 0; i < serial->num_port_pointers; ++i)
930 kfree(serial->port[i]);
935 void usb_serial_disconnect(struct usb_interface *interface)
938 struct usb_serial *serial = usb_get_intfdata (interface);
939 struct device *dev = &interface->dev;
940 struct usb_serial_port *port;
942 dbg ("%s", __FUNCTION__);
944 usb_set_intfdata (interface, NULL);
946 for (i = 0; i < serial->num_ports; ++i) {
947 port = serial->port[i];
948 if (port && port->tty)
949 tty_hangup(port->tty);
951 /* let the last holder of this object
952 * cause it to be cleaned up */
953 kref_put(&serial->kref, destroy_serial);
955 dev_info(dev, "device disconnected\n");
958 static struct tty_operations serial_ops = {
960 .close = serial_close,
961 .write = serial_write,
962 .write_room = serial_write_room,
963 .ioctl = serial_ioctl,
964 .set_termios = serial_set_termios,
965 .throttle = serial_throttle,
966 .unthrottle = serial_unthrottle,
967 .break_ctl = serial_break,
968 .chars_in_buffer = serial_chars_in_buffer,
969 .read_proc = serial_read_proc,
970 .tiocmget = serial_tiocmget,
971 .tiocmset = serial_tiocmset,
974 struct tty_driver *usb_serial_tty_driver;
976 static int __init usb_serial_init(void)
981 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
982 if (!usb_serial_tty_driver)
985 /* Initialize our global data */
986 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
987 serial_table[i] = NULL;
990 result = bus_register(&usb_serial_bus_type);
992 err("%s - registering bus driver failed", __FUNCTION__);
996 usb_serial_tty_driver->owner = THIS_MODULE;
997 usb_serial_tty_driver->driver_name = "usbserial";
998 usb_serial_tty_driver->devfs_name = "usb/tts/";
999 usb_serial_tty_driver->name = "ttyUSB";
1000 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1001 usb_serial_tty_driver->minor_start = 0;
1002 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1003 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1004 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1005 usb_serial_tty_driver->init_termios = tty_std_termios;
1006 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1007 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1008 result = tty_register_driver(usb_serial_tty_driver);
1010 err("%s - tty_register_driver failed", __FUNCTION__);
1011 goto exit_reg_driver;
1014 /* register the USB driver */
1015 result = usb_register(&usb_serial_driver);
1017 err("%s - usb_register failed", __FUNCTION__);
1021 /* register the generic driver, if we should */
1022 result = usb_serial_generic_register(debug);
1024 err("%s - registering generic driver failed", __FUNCTION__);
1033 usb_deregister(&usb_serial_driver);
1036 tty_unregister_driver(usb_serial_tty_driver);
1039 bus_unregister(&usb_serial_bus_type);
1042 err ("%s - returning with error %d", __FUNCTION__, result);
1043 put_tty_driver(usb_serial_tty_driver);
1048 static void __exit usb_serial_exit(void)
1050 usb_serial_console_exit();
1052 usb_serial_generic_deregister();
1054 usb_deregister(&usb_serial_driver);
1055 tty_unregister_driver(usb_serial_tty_driver);
1056 put_tty_driver(usb_serial_tty_driver);
1057 bus_unregister(&usb_serial_bus_type);
1061 module_init(usb_serial_init);
1062 module_exit(usb_serial_exit);
1064 #define set_to_generic_if_null(type, function) \
1066 if (!type->function) { \
1067 type->function = usb_serial_generic_##function; \
1068 dbg("Had to override the " #function \
1069 " usb serial operation with the generic one.");\
1073 static void fixup_generic(struct usb_serial_driver *device)
1075 set_to_generic_if_null(device, open);
1076 set_to_generic_if_null(device, write);
1077 set_to_generic_if_null(device, close);
1078 set_to_generic_if_null(device, write_room);
1079 set_to_generic_if_null(device, chars_in_buffer);
1080 set_to_generic_if_null(device, read_bulk_callback);
1081 set_to_generic_if_null(device, write_bulk_callback);
1082 set_to_generic_if_null(device, shutdown);
1085 int usb_serial_register(struct usb_serial_driver *driver)
1089 fixup_generic(driver);
1091 if (!driver->description)
1092 driver->description = driver->driver.name;
1094 /* Add this device to our list of devices */
1095 list_add(&driver->driver_list, &usb_serial_driver_list);
1097 retval = usb_serial_bus_register(driver);
1099 err("problem %d when registering driver %s", retval, driver->description);
1100 list_del(&driver->driver_list);
1103 info("USB Serial support registered for %s", driver->description);
1109 void usb_serial_deregister(struct usb_serial_driver *device)
1111 info("USB Serial deregistering driver %s", device->description);
1112 list_del(&device->driver_list);
1113 usb_serial_bus_deregister(device);
1118 /* If the usb-serial core is built into the core, the usb-serial drivers
1119 need these symbols to load properly as modules. */
1120 EXPORT_SYMBOL_GPL(usb_serial_register);
1121 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1122 EXPORT_SYMBOL_GPL(usb_serial_probe);
1123 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1124 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1127 /* Module information */
1128 MODULE_AUTHOR( DRIVER_AUTHOR );
1129 MODULE_DESCRIPTION( DRIVER_DESC );
1130 MODULE_LICENSE("GPL");
1132 module_param(debug, bool, S_IRUGO | S_IWUSR);
1133 MODULE_PARM_DESC(debug, "Debug enabled or not");