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/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/tty.h>
24 #include <linux/tty_driver.h>
25 #include <linux/tty_flip.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/spinlock.h>
29 #include <linux/mutex.h>
30 #include <linux/list.h>
31 #include <linux/smp_lock.h>
32 #include <asm/uaccess.h>
33 #include <linux/usb.h>
34 #include <linux/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 static void port_free(struct usb_serial_port *port);
45 /* Driver structure we register with the USB core */
46 static struct usb_driver usb_serial_driver = {
48 .probe = usb_serial_probe,
49 .disconnect = usb_serial_disconnect,
53 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
54 the MODULE_DEVICE_TABLE declarations in each serial driver
55 cause the "hotplug" program to pull in whatever module is necessary
56 via modprobe, and modprobe will load usbserial because the serial
61 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
62 static LIST_HEAD(usb_serial_driver_list);
64 struct usb_serial *usb_serial_get_by_index(unsigned index)
66 struct usb_serial *serial = serial_table[index];
69 kref_get(&serial->kref);
73 static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
78 dbg("%s %d", __FUNCTION__, num_ports);
81 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
86 for (j = 1; j <= num_ports-1; ++j)
87 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
96 dbg("%s - minor base = %d", __FUNCTION__, *minor);
97 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i)
98 serial_table[i] = serial;
104 static void return_serial(struct usb_serial *serial)
108 dbg("%s", __FUNCTION__);
113 for (i = 0; i < serial->num_ports; ++i) {
114 serial_table[serial->minor + i] = NULL;
118 static void destroy_serial(struct kref *kref)
120 struct usb_serial *serial;
121 struct usb_serial_port *port;
124 serial = to_usb_serial(kref);
126 dbg("%s - %s", __FUNCTION__, serial->type->description);
128 serial->type->shutdown(serial);
130 /* return the minor range that this device had */
131 return_serial(serial);
133 for (i = 0; i < serial->num_ports; ++i)
134 serial->port[i]->open_count = 0;
136 /* the ports are cleaned up and released in port_release() */
137 for (i = 0; i < serial->num_ports; ++i)
138 if (serial->port[i]->dev.parent != NULL) {
139 device_unregister(&serial->port[i]->dev);
140 serial->port[i] = NULL;
143 /* If this is a "fake" port, we have to clean it up here, as it will
144 * not get cleaned up in port_release() as it was never registered with
146 if (serial->num_ports < serial->num_port_pointers) {
147 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
148 port = serial->port[i];
155 usb_put_dev(serial->dev);
157 /* free up any memory that we allocated */
161 void usb_serial_put(struct usb_serial *serial)
163 kref_put(&serial->kref, destroy_serial);
166 /*****************************************************************************
167 * Driver tty interface functions
168 *****************************************************************************/
169 static int serial_open (struct tty_struct *tty, struct file * filp)
171 struct usb_serial *serial;
172 struct usb_serial_port *port;
173 unsigned int portNumber;
176 dbg("%s", __FUNCTION__);
178 /* get the serial object associated with this tty pointer */
179 serial = usb_serial_get_by_index(tty->index);
181 tty->driver_data = NULL;
185 portNumber = tty->index - serial->minor;
186 port = serial->port[portNumber];
189 goto bailout_kref_put;
192 if (mutex_lock_interruptible(&port->mutex)) {
193 retval = -ERESTARTSYS;
194 goto bailout_kref_put;
199 /* set up our port structure making the tty driver
200 * remember our port object, and us it */
201 tty->driver_data = port;
204 if (port->open_count == 1) {
206 /* lock this module before we call it
207 * this may fail, which means we must bail out,
208 * safe because we are called with BKL held */
209 if (!try_module_get(serial->type->driver.owner)) {
211 goto bailout_mutex_unlock;
214 /* only call the device specific open if this
215 * is the first time the port is opened */
216 retval = serial->type->open(port, filp);
218 goto bailout_module_put;
221 mutex_unlock(&port->mutex);
225 module_put(serial->type->driver.owner);
226 bailout_mutex_unlock:
227 port->open_count = 0;
228 tty->driver_data = NULL;
230 mutex_unlock(&port->mutex);
232 usb_serial_put(serial);
236 static void serial_close(struct tty_struct *tty, struct file * filp)
238 struct usb_serial_port *port = tty->driver_data;
243 dbg("%s - port %d", __FUNCTION__, port->number);
245 mutex_lock(&port->mutex);
247 if (port->open_count == 0) {
248 mutex_unlock(&port->mutex);
253 if (port->open_count == 0) {
254 /* only call the device specific close if this
255 * port is being closed by the last owner */
256 port->serial->type->close(port, filp);
259 if (port->tty->driver_data)
260 port->tty->driver_data = NULL;
264 module_put(port->serial->type->driver.owner);
267 mutex_unlock(&port->mutex);
268 usb_serial_put(port->serial);
271 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
273 struct usb_serial_port *port = tty->driver_data;
274 int retval = -EINVAL;
276 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
279 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
281 if (!port->open_count) {
282 dbg("%s - port not opened", __FUNCTION__);
286 /* pass on to the driver specific version of this function */
287 retval = port->serial->type->write(port, buf, count);
293 static int serial_write_room (struct tty_struct *tty)
295 struct usb_serial_port *port = tty->driver_data;
296 int retval = -ENODEV;
301 dbg("%s - port %d", __FUNCTION__, port->number);
303 if (!port->open_count) {
304 dbg("%s - port not open", __FUNCTION__);
308 /* pass on to the driver specific version of this function */
309 retval = port->serial->type->write_room(port);
315 static int serial_chars_in_buffer (struct tty_struct *tty)
317 struct usb_serial_port *port = tty->driver_data;
318 int retval = -ENODEV;
323 dbg("%s = port %d", __FUNCTION__, port->number);
325 if (!port->open_count) {
326 dbg("%s - port not open", __FUNCTION__);
330 /* pass on to the driver specific version of this function */
331 retval = port->serial->type->chars_in_buffer(port);
337 static void serial_throttle (struct tty_struct * tty)
339 struct usb_serial_port *port = tty->driver_data;
344 dbg("%s - port %d", __FUNCTION__, port->number);
346 if (!port->open_count) {
347 dbg ("%s - port not open", __FUNCTION__);
351 /* pass on to the driver specific version of this function */
352 if (port->serial->type->throttle)
353 port->serial->type->throttle(port);
356 static void serial_unthrottle (struct tty_struct * tty)
358 struct usb_serial_port *port = tty->driver_data;
363 dbg("%s - port %d", __FUNCTION__, port->number);
365 if (!port->open_count) {
366 dbg("%s - port not open", __FUNCTION__);
370 /* pass on to the driver specific version of this function */
371 if (port->serial->type->unthrottle)
372 port->serial->type->unthrottle(port);
375 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
377 struct usb_serial_port *port = tty->driver_data;
378 int retval = -ENODEV;
383 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
385 if (!port->open_count) {
386 dbg ("%s - port not open", __FUNCTION__);
390 /* pass on to the driver specific version of this function if it is available */
391 if (port->serial->type->ioctl)
392 retval = port->serial->type->ioctl(port, file, cmd, arg);
394 retval = -ENOIOCTLCMD;
400 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
402 struct usb_serial_port *port = tty->driver_data;
407 dbg("%s - port %d", __FUNCTION__, port->number);
409 if (!port->open_count) {
410 dbg("%s - port not open", __FUNCTION__);
414 /* pass on to the driver specific version of this function if it is available */
415 if (port->serial->type->set_termios)
416 port->serial->type->set_termios(port, old);
419 static void serial_break (struct tty_struct *tty, int break_state)
421 struct usb_serial_port *port = tty->driver_data;
426 dbg("%s - port %d", __FUNCTION__, port->number);
428 if (!port->open_count) {
429 dbg("%s - port not open", __FUNCTION__);
433 /* pass on to the driver specific version of this function if it is available */
434 if (port->serial->type->break_ctl)
435 port->serial->type->break_ctl(port, break_state);
438 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
440 struct usb_serial *serial;
446 dbg("%s", __FUNCTION__);
447 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
448 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
449 serial = usb_serial_get_by_index(i);
453 length += sprintf (page+length, "%d:", i);
454 if (serial->type->driver.owner)
455 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
456 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
457 length += sprintf (page+length, " vendor:%04x product:%04x",
458 le16_to_cpu(serial->dev->descriptor.idVendor),
459 le16_to_cpu(serial->dev->descriptor.idProduct));
460 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
461 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
463 usb_make_path(serial->dev, tmp, sizeof(tmp));
464 length += sprintf (page+length, " path:%s", tmp);
466 length += sprintf (page+length, "\n");
467 if ((length + begin) > (off + count)) {
468 usb_serial_put(serial);
471 if ((length + begin) < off) {
475 usb_serial_put(serial);
479 if (off >= (length + begin))
481 *start = page + (off-begin);
482 return ((count < begin+length-off) ? count : begin+length-off);
485 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
487 struct usb_serial_port *port = tty->driver_data;
492 dbg("%s - port %d", __FUNCTION__, port->number);
494 if (!port->open_count) {
495 dbg("%s - port not open", __FUNCTION__);
499 if (port->serial->type->tiocmget)
500 return port->serial->type->tiocmget(port, file);
505 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
506 unsigned int set, unsigned int clear)
508 struct usb_serial_port *port = tty->driver_data;
513 dbg("%s - port %d", __FUNCTION__, port->number);
515 if (!port->open_count) {
516 dbg("%s - port not open", __FUNCTION__);
520 if (port->serial->type->tiocmset)
521 return port->serial->type->tiocmset(port, file, set, clear);
527 * We would be calling tty_wakeup here, but unfortunately some line
528 * disciplines have an annoying habit of calling tty->write from
529 * the write wakeup callback (e.g. n_hdlc.c).
531 void usb_serial_port_softint(struct usb_serial_port *port)
533 schedule_work(&port->work);
536 static void usb_serial_port_work(void *private)
538 struct usb_serial_port *port = private;
539 struct tty_struct *tty;
541 dbg("%s - port %d", __FUNCTION__, port->number);
553 static void port_release(struct device *dev)
555 struct usb_serial_port *port = to_usb_serial_port(dev);
557 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
561 static void port_free(struct usb_serial_port *port)
563 usb_kill_urb(port->read_urb);
564 usb_free_urb(port->read_urb);
565 usb_kill_urb(port->write_urb);
566 usb_free_urb(port->write_urb);
567 usb_kill_urb(port->interrupt_in_urb);
568 usb_free_urb(port->interrupt_in_urb);
569 usb_kill_urb(port->interrupt_out_urb);
570 usb_free_urb(port->interrupt_out_urb);
571 kfree(port->bulk_in_buffer);
572 kfree(port->bulk_out_buffer);
573 kfree(port->interrupt_in_buffer);
574 kfree(port->interrupt_out_buffer);
575 flush_scheduled_work(); /* port->work */
579 static struct usb_serial * create_serial (struct usb_device *dev,
580 struct usb_interface *interface,
581 struct usb_serial_driver *driver)
583 struct usb_serial *serial;
585 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
587 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
590 serial->dev = usb_get_dev(dev);
591 serial->type = driver;
592 serial->interface = interface;
593 kref_init(&serial->kref);
598 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
601 const struct usb_device_id *id;
602 struct usb_serial_driver *t;
604 /* Check if the usb id matches a known device */
605 list_for_each(p, &usb_serial_driver_list) {
606 t = list_entry(p, struct usb_serial_driver, driver_list);
607 id = usb_match_id(iface, t->id_table);
609 dbg("descriptor matches");
617 int usb_serial_probe(struct usb_interface *interface,
618 const struct usb_device_id *id)
620 struct usb_device *dev = interface_to_usbdev (interface);
621 struct usb_serial *serial = NULL;
622 struct usb_serial_port *port;
623 struct usb_host_interface *iface_desc;
624 struct usb_endpoint_descriptor *endpoint;
625 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
626 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
627 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
628 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
629 struct usb_serial_driver *type = NULL;
634 int num_interrupt_in = 0;
635 int num_interrupt_out = 0;
637 int num_bulk_out = 0;
641 type = search_serial_device(interface);
647 serial = create_serial (dev, interface, type);
649 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
653 /* if this device type has a probe function, call it */
655 const struct usb_device_id *id;
657 if (!try_module_get(type->driver.owner)) {
658 dev_err(&interface->dev, "module get failed, exiting\n");
663 id = usb_match_id(interface, type->id_table);
664 retval = type->probe(serial, id);
665 module_put(type->driver.owner);
668 dbg ("sub driver rejected device");
674 /* descriptor matches, let's find the endpoints needed */
675 /* check out the endpoints */
676 iface_desc = interface->cur_altsetting;
677 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
678 endpoint = &iface_desc->endpoint[i].desc;
680 if (usb_endpoint_is_bulk_in(endpoint)) {
681 /* we found a bulk in endpoint */
682 dbg("found bulk in on endpoint %d", i);
683 bulk_in_endpoint[num_bulk_in] = endpoint;
687 if (usb_endpoint_is_bulk_out(endpoint)) {
688 /* we found a bulk out endpoint */
689 dbg("found bulk out on endpoint %d", i);
690 bulk_out_endpoint[num_bulk_out] = endpoint;
694 if (usb_endpoint_is_int_in(endpoint)) {
695 /* we found a interrupt in endpoint */
696 dbg("found interrupt in on endpoint %d", i);
697 interrupt_in_endpoint[num_interrupt_in] = endpoint;
701 if (usb_endpoint_is_int_out(endpoint)) {
702 /* we found an interrupt out endpoint */
703 dbg("found interrupt out on endpoint %d", i);
704 interrupt_out_endpoint[num_interrupt_out] = endpoint;
709 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
710 /* BEGIN HORRIBLE HACK FOR PL2303 */
711 /* this is needed due to the looney way its endpoints are set up */
712 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
713 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
714 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
715 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
716 if (interface != dev->actconfig->interface[0]) {
717 /* check out the endpoints of the other interface*/
718 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
719 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
720 endpoint = &iface_desc->endpoint[i].desc;
721 if (usb_endpoint_is_int_in(endpoint)) {
722 /* we found a interrupt in endpoint */
723 dbg("found interrupt in for Prolific device on separate interface");
724 interrupt_in_endpoint[num_interrupt_in] = endpoint;
730 /* Now make sure the PL-2303 is configured correctly.
731 * If not, give up now and hope this hack will work
732 * properly during a later invocation of usb_serial_probe
734 if (num_bulk_in == 0 || num_bulk_out == 0) {
735 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
740 /* END HORRIBLE HACK FOR PL2303 */
743 /* found all that we need */
744 dev_info(&interface->dev, "%s converter detected\n", type->description);
746 #ifdef CONFIG_USB_SERIAL_GENERIC
747 if (type == &usb_serial_generic_device) {
748 num_ports = num_bulk_out;
749 if (num_ports == 0) {
750 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
757 /* if this device type has a calc_num_ports function, call it */
758 if (type->calc_num_ports) {
759 if (!try_module_get(type->driver.owner)) {
760 dev_err(&interface->dev, "module get failed, exiting\n");
764 num_ports = type->calc_num_ports (serial);
765 module_put(type->driver.owner);
768 num_ports = type->num_ports;
771 if (get_free_serial (serial, num_ports, &minor) == NULL) {
772 dev_err(&interface->dev, "No more free serial devices\n");
777 serial->minor = minor;
778 serial->num_ports = num_ports;
779 serial->num_bulk_in = num_bulk_in;
780 serial->num_bulk_out = num_bulk_out;
781 serial->num_interrupt_in = num_interrupt_in;
782 serial->num_interrupt_out = num_interrupt_out;
784 /* create our ports, we need as many as the max endpoints */
785 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
786 max_endpoints = max(num_bulk_in, num_bulk_out);
787 max_endpoints = max(max_endpoints, num_interrupt_in);
788 max_endpoints = max(max_endpoints, num_interrupt_out);
789 max_endpoints = max(max_endpoints, (int)serial->num_ports);
790 serial->num_port_pointers = max_endpoints;
791 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
792 for (i = 0; i < max_endpoints; ++i) {
793 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
796 port->number = i + serial->minor;
797 port->serial = serial;
798 spin_lock_init(&port->lock);
799 mutex_init(&port->mutex);
800 INIT_WORK(&port->work, usb_serial_port_work, port);
801 serial->port[i] = port;
804 /* set up the endpoint information */
805 for (i = 0; i < num_bulk_in; ++i) {
806 endpoint = bulk_in_endpoint[i];
807 port = serial->port[i];
808 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
809 if (!port->read_urb) {
810 dev_err(&interface->dev, "No free urbs available\n");
813 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
814 port->bulk_in_size = buffer_size;
815 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
816 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
817 if (!port->bulk_in_buffer) {
818 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
821 usb_fill_bulk_urb (port->read_urb, dev,
822 usb_rcvbulkpipe (dev,
823 endpoint->bEndpointAddress),
824 port->bulk_in_buffer, buffer_size,
825 serial->type->read_bulk_callback,
829 for (i = 0; i < num_bulk_out; ++i) {
830 endpoint = bulk_out_endpoint[i];
831 port = serial->port[i];
832 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
833 if (!port->write_urb) {
834 dev_err(&interface->dev, "No free urbs available\n");
837 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
838 port->bulk_out_size = buffer_size;
839 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
840 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
841 if (!port->bulk_out_buffer) {
842 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
845 usb_fill_bulk_urb (port->write_urb, dev,
846 usb_sndbulkpipe (dev,
847 endpoint->bEndpointAddress),
848 port->bulk_out_buffer, buffer_size,
849 serial->type->write_bulk_callback,
853 if (serial->type->read_int_callback) {
854 for (i = 0; i < num_interrupt_in; ++i) {
855 endpoint = interrupt_in_endpoint[i];
856 port = serial->port[i];
857 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
858 if (!port->interrupt_in_urb) {
859 dev_err(&interface->dev, "No free urbs available\n");
862 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
863 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
864 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
865 if (!port->interrupt_in_buffer) {
866 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
869 usb_fill_int_urb (port->interrupt_in_urb, dev,
871 endpoint->bEndpointAddress),
872 port->interrupt_in_buffer, buffer_size,
873 serial->type->read_int_callback, port,
874 endpoint->bInterval);
876 } else if (num_interrupt_in) {
877 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
880 if (serial->type->write_int_callback) {
881 for (i = 0; i < num_interrupt_out; ++i) {
882 endpoint = interrupt_out_endpoint[i];
883 port = serial->port[i];
884 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
885 if (!port->interrupt_out_urb) {
886 dev_err(&interface->dev, "No free urbs available\n");
889 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
890 port->interrupt_out_size = buffer_size;
891 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
892 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
893 if (!port->interrupt_out_buffer) {
894 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
897 usb_fill_int_urb (port->interrupt_out_urb, dev,
899 endpoint->bEndpointAddress),
900 port->interrupt_out_buffer, buffer_size,
901 serial->type->write_int_callback, port,
902 endpoint->bInterval);
904 } else if (num_interrupt_out) {
905 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
908 /* if this device type has an attach function, call it */
910 if (!try_module_get(type->driver.owner)) {
911 dev_err(&interface->dev, "module get failed, exiting\n");
914 retval = type->attach (serial);
915 module_put(type->driver.owner);
919 /* quietly accept this device, but don't bind to a serial port
920 * as it's about to disappear */
925 /* register all of the individual ports with the driver core */
926 for (i = 0; i < num_ports; ++i) {
927 port = serial->port[i];
928 port->dev.parent = &interface->dev;
929 port->dev.driver = NULL;
930 port->dev.bus = &usb_serial_bus_type;
931 port->dev.release = &port_release;
933 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
934 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
935 device_register (&port->dev);
938 usb_serial_console_init (debug, minor);
942 usb_set_intfdata (interface, serial);
946 for (i = 0; i < num_bulk_in; ++i) {
947 port = serial->port[i];
951 usb_free_urb (port->read_urb);
952 kfree(port->bulk_in_buffer);
954 for (i = 0; i < num_bulk_out; ++i) {
955 port = serial->port[i];
959 usb_free_urb (port->write_urb);
960 kfree(port->bulk_out_buffer);
962 for (i = 0; i < num_interrupt_in; ++i) {
963 port = serial->port[i];
966 if (port->interrupt_in_urb)
967 usb_free_urb (port->interrupt_in_urb);
968 kfree(port->interrupt_in_buffer);
970 for (i = 0; i < num_interrupt_out; ++i) {
971 port = serial->port[i];
974 if (port->interrupt_out_urb)
975 usb_free_urb (port->interrupt_out_urb);
976 kfree(port->interrupt_out_buffer);
979 /* return the minor range that this device had */
980 return_serial (serial);
982 /* free up any memory that we allocated */
983 for (i = 0; i < serial->num_port_pointers; ++i)
984 kfree(serial->port[i]);
989 void usb_serial_disconnect(struct usb_interface *interface)
992 struct usb_serial *serial = usb_get_intfdata (interface);
993 struct device *dev = &interface->dev;
994 struct usb_serial_port *port;
996 usb_serial_console_disconnect(serial);
997 dbg ("%s", __FUNCTION__);
999 usb_set_intfdata (interface, NULL);
1001 for (i = 0; i < serial->num_ports; ++i) {
1002 port = serial->port[i];
1003 if (port && port->tty)
1004 tty_hangup(port->tty);
1006 /* let the last holder of this object
1007 * cause it to be cleaned up */
1008 usb_serial_put(serial);
1010 dev_info(dev, "device disconnected\n");
1013 static struct tty_operations serial_ops = {
1014 .open = serial_open,
1015 .close = serial_close,
1016 .write = serial_write,
1017 .write_room = serial_write_room,
1018 .ioctl = serial_ioctl,
1019 .set_termios = serial_set_termios,
1020 .throttle = serial_throttle,
1021 .unthrottle = serial_unthrottle,
1022 .break_ctl = serial_break,
1023 .chars_in_buffer = serial_chars_in_buffer,
1024 .read_proc = serial_read_proc,
1025 .tiocmget = serial_tiocmget,
1026 .tiocmset = serial_tiocmset,
1029 struct tty_driver *usb_serial_tty_driver;
1031 static int __init usb_serial_init(void)
1036 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1037 if (!usb_serial_tty_driver)
1040 /* Initialize our global data */
1041 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1042 serial_table[i] = NULL;
1045 result = bus_register(&usb_serial_bus_type);
1047 err("%s - registering bus driver failed", __FUNCTION__);
1051 usb_serial_tty_driver->owner = THIS_MODULE;
1052 usb_serial_tty_driver->driver_name = "usbserial";
1053 usb_serial_tty_driver->name = "ttyUSB";
1054 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1055 usb_serial_tty_driver->minor_start = 0;
1056 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1057 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1058 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1059 usb_serial_tty_driver->init_termios = tty_std_termios;
1060 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1061 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1062 result = tty_register_driver(usb_serial_tty_driver);
1064 err("%s - tty_register_driver failed", __FUNCTION__);
1065 goto exit_reg_driver;
1068 /* register the USB driver */
1069 result = usb_register(&usb_serial_driver);
1071 err("%s - usb_register failed", __FUNCTION__);
1075 /* register the generic driver, if we should */
1076 result = usb_serial_generic_register(debug);
1078 err("%s - registering generic driver failed", __FUNCTION__);
1087 usb_deregister(&usb_serial_driver);
1090 tty_unregister_driver(usb_serial_tty_driver);
1093 bus_unregister(&usb_serial_bus_type);
1096 err ("%s - returning with error %d", __FUNCTION__, result);
1097 put_tty_driver(usb_serial_tty_driver);
1102 static void __exit usb_serial_exit(void)
1104 usb_serial_console_exit();
1106 usb_serial_generic_deregister();
1108 usb_deregister(&usb_serial_driver);
1109 tty_unregister_driver(usb_serial_tty_driver);
1110 put_tty_driver(usb_serial_tty_driver);
1111 bus_unregister(&usb_serial_bus_type);
1115 module_init(usb_serial_init);
1116 module_exit(usb_serial_exit);
1118 #define set_to_generic_if_null(type, function) \
1120 if (!type->function) { \
1121 type->function = usb_serial_generic_##function; \
1122 dbg("Had to override the " #function \
1123 " usb serial operation with the generic one.");\
1127 static void fixup_generic(struct usb_serial_driver *device)
1129 set_to_generic_if_null(device, open);
1130 set_to_generic_if_null(device, write);
1131 set_to_generic_if_null(device, close);
1132 set_to_generic_if_null(device, write_room);
1133 set_to_generic_if_null(device, chars_in_buffer);
1134 set_to_generic_if_null(device, read_bulk_callback);
1135 set_to_generic_if_null(device, write_bulk_callback);
1136 set_to_generic_if_null(device, shutdown);
1139 int usb_serial_register(struct usb_serial_driver *driver)
1143 fixup_generic(driver);
1145 if (!driver->description)
1146 driver->description = driver->driver.name;
1148 /* Add this device to our list of devices */
1149 list_add(&driver->driver_list, &usb_serial_driver_list);
1151 retval = usb_serial_bus_register(driver);
1153 err("problem %d when registering driver %s", retval, driver->description);
1154 list_del(&driver->driver_list);
1157 info("USB Serial support registered for %s", driver->description);
1163 void usb_serial_deregister(struct usb_serial_driver *device)
1165 info("USB Serial deregistering driver %s", device->description);
1166 list_del(&device->driver_list);
1167 usb_serial_bus_deregister(device);
1172 /* If the usb-serial core is built into the core, the usb-serial drivers
1173 need these symbols to load properly as modules. */
1174 EXPORT_SYMBOL_GPL(usb_serial_register);
1175 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1176 EXPORT_SYMBOL_GPL(usb_serial_probe);
1177 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1178 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1181 /* Module information */
1182 MODULE_AUTHOR( DRIVER_AUTHOR );
1183 MODULE_DESCRIPTION( DRIVER_DESC );
1184 MODULE_LICENSE("GPL");
1186 module_param(debug, bool, S_IRUGO | S_IWUSR);
1187 MODULE_PARM_DESC(debug, "Debug enabled or not");