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 <asm/uaccess.h>
32 #include <linux/usb.h>
33 #include <linux/usb/serial.h>
39 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
40 #define DRIVER_DESC "USB Serial Driver core"
42 static void port_free(struct usb_serial_port *port);
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 spinlock_t table_lock;
62 static LIST_HEAD(usb_serial_driver_list);
64 struct usb_serial *usb_serial_get_by_index(unsigned index)
66 struct usb_serial *serial;
68 spin_lock(&table_lock);
69 serial = serial_table[index];
72 kref_get(&serial->kref);
73 spin_unlock(&table_lock);
77 static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
82 dbg("%s %d", __FUNCTION__, num_ports);
85 spin_lock(&table_lock);
86 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
91 for (j = 1; j <= num_ports-1; ++j)
92 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
102 dbg("%s - minor base = %d", __FUNCTION__, *minor);
103 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
104 serial_table[i] = serial;
105 serial->port[j++]->number = i;
107 spin_unlock(&table_lock);
110 spin_unlock(&table_lock);
114 static void return_serial(struct usb_serial *serial)
118 dbg("%s", __FUNCTION__);
123 spin_lock(&table_lock);
124 for (i = 0; i < serial->num_ports; ++i) {
125 serial_table[serial->minor + i] = NULL;
127 spin_unlock(&table_lock);
130 static void destroy_serial(struct kref *kref)
132 struct usb_serial *serial;
133 struct usb_serial_port *port;
136 serial = to_usb_serial(kref);
138 dbg("%s - %s", __FUNCTION__, serial->type->description);
140 serial->type->shutdown(serial);
142 /* return the minor range that this device had */
143 return_serial(serial);
145 for (i = 0; i < serial->num_ports; ++i)
146 serial->port[i]->open_count = 0;
148 /* the ports are cleaned up and released in port_release() */
149 for (i = 0; i < serial->num_ports; ++i)
150 if (serial->port[i]->dev.parent != NULL) {
151 device_unregister(&serial->port[i]->dev);
152 serial->port[i] = NULL;
155 /* If this is a "fake" port, we have to clean it up here, as it will
156 * not get cleaned up in port_release() as it was never registered with
158 if (serial->num_ports < serial->num_port_pointers) {
159 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
160 port = serial->port[i];
167 usb_put_dev(serial->dev);
169 /* free up any memory that we allocated */
173 void usb_serial_put(struct usb_serial *serial)
175 kref_put(&serial->kref, destroy_serial);
178 /*****************************************************************************
179 * Driver tty interface functions
180 *****************************************************************************/
181 static int serial_open (struct tty_struct *tty, struct file * filp)
183 struct usb_serial *serial;
184 struct usb_serial_port *port;
185 unsigned int portNumber;
188 dbg("%s", __FUNCTION__);
190 /* get the serial object associated with this tty pointer */
191 serial = usb_serial_get_by_index(tty->index);
193 tty->driver_data = NULL;
197 portNumber = tty->index - serial->minor;
198 port = serial->port[portNumber];
201 goto bailout_kref_put;
204 if (mutex_lock_interruptible(&port->mutex)) {
205 retval = -ERESTARTSYS;
206 goto bailout_kref_put;
211 /* set up our port structure making the tty driver
212 * remember our port object, and us it */
213 tty->driver_data = port;
216 if (port->open_count == 1) {
218 /* lock this module before we call it
219 * this may fail, which means we must bail out,
220 * safe because we are called with BKL held */
221 if (!try_module_get(serial->type->driver.owner)) {
223 goto bailout_mutex_unlock;
226 /* only call the device specific open if this
227 * is the first time the port is opened */
228 retval = serial->type->open(port, filp);
230 goto bailout_module_put;
233 mutex_unlock(&port->mutex);
237 module_put(serial->type->driver.owner);
238 bailout_mutex_unlock:
239 port->open_count = 0;
240 tty->driver_data = NULL;
242 mutex_unlock(&port->mutex);
244 usb_serial_put(serial);
248 static void serial_close(struct tty_struct *tty, struct file * filp)
250 struct usb_serial_port *port = tty->driver_data;
255 dbg("%s - port %d", __FUNCTION__, port->number);
257 mutex_lock(&port->mutex);
259 if (port->open_count == 0) {
260 mutex_unlock(&port->mutex);
265 if (port->open_count == 0) {
266 /* only call the device specific close if this
267 * port is being closed by the last owner */
268 port->serial->type->close(port, filp);
271 if (port->tty->driver_data)
272 port->tty->driver_data = NULL;
276 module_put(port->serial->type->driver.owner);
279 mutex_unlock(&port->mutex);
280 usb_serial_put(port->serial);
283 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
285 struct usb_serial_port *port = tty->driver_data;
286 int retval = -ENODEV;
288 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
291 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
293 if (!port->open_count) {
295 dbg("%s - port not opened", __FUNCTION__);
299 /* pass on to the driver specific version of this function */
300 retval = port->serial->type->write(port, buf, count);
306 static int serial_write_room (struct tty_struct *tty)
308 struct usb_serial_port *port = tty->driver_data;
309 int retval = -ENODEV;
314 dbg("%s - port %d", __FUNCTION__, port->number);
316 if (!port->open_count) {
317 dbg("%s - port not open", __FUNCTION__);
321 /* pass on to the driver specific version of this function */
322 retval = port->serial->type->write_room(port);
328 static int serial_chars_in_buffer (struct tty_struct *tty)
330 struct usb_serial_port *port = tty->driver_data;
331 int retval = -ENODEV;
336 dbg("%s = port %d", __FUNCTION__, port->number);
338 if (!port->open_count) {
339 dbg("%s - port not open", __FUNCTION__);
343 /* pass on to the driver specific version of this function */
344 retval = port->serial->type->chars_in_buffer(port);
350 static void serial_throttle (struct tty_struct * tty)
352 struct usb_serial_port *port = tty->driver_data;
357 dbg("%s - port %d", __FUNCTION__, port->number);
359 if (!port->open_count) {
360 dbg ("%s - port not open", __FUNCTION__);
364 /* pass on to the driver specific version of this function */
365 if (port->serial->type->throttle)
366 port->serial->type->throttle(port);
369 static void serial_unthrottle (struct tty_struct * tty)
371 struct usb_serial_port *port = tty->driver_data;
376 dbg("%s - port %d", __FUNCTION__, port->number);
378 if (!port->open_count) {
379 dbg("%s - port not open", __FUNCTION__);
383 /* pass on to the driver specific version of this function */
384 if (port->serial->type->unthrottle)
385 port->serial->type->unthrottle(port);
388 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
390 struct usb_serial_port *port = tty->driver_data;
391 int retval = -ENODEV;
396 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
398 if (!port->open_count) {
399 dbg ("%s - port not open", __FUNCTION__);
403 /* pass on to the driver specific version of this function if it is available */
404 if (port->serial->type->ioctl)
405 retval = port->serial->type->ioctl(port, file, cmd, arg);
407 retval = -ENOIOCTLCMD;
413 static void serial_set_termios (struct tty_struct *tty, struct ktermios * old)
415 struct usb_serial_port *port = tty->driver_data;
420 dbg("%s - port %d", __FUNCTION__, port->number);
422 if (!port->open_count) {
423 dbg("%s - port not open", __FUNCTION__);
427 /* pass on to the driver specific version of this function if it is available */
428 if (port->serial->type->set_termios)
429 port->serial->type->set_termios(port, old);
432 static void serial_break (struct tty_struct *tty, int break_state)
434 struct usb_serial_port *port = tty->driver_data;
439 dbg("%s - port %d", __FUNCTION__, port->number);
441 if (!port->open_count) {
442 dbg("%s - port not open", __FUNCTION__);
446 /* pass on to the driver specific version of this function if it is available */
447 if (port->serial->type->break_ctl)
448 port->serial->type->break_ctl(port, break_state);
451 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
453 struct usb_serial *serial;
459 dbg("%s", __FUNCTION__);
460 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
461 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
462 serial = usb_serial_get_by_index(i);
466 length += sprintf (page+length, "%d:", i);
467 if (serial->type->driver.owner)
468 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
469 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
470 length += sprintf (page+length, " vendor:%04x product:%04x",
471 le16_to_cpu(serial->dev->descriptor.idVendor),
472 le16_to_cpu(serial->dev->descriptor.idProduct));
473 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
474 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
476 usb_make_path(serial->dev, tmp, sizeof(tmp));
477 length += sprintf (page+length, " path:%s", tmp);
479 length += sprintf (page+length, "\n");
480 if ((length + begin) > (off + count)) {
481 usb_serial_put(serial);
484 if ((length + begin) < off) {
488 usb_serial_put(serial);
492 if (off >= (length + begin))
494 *start = page + (off-begin);
495 return ((count < begin+length-off) ? count : begin+length-off);
498 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
500 struct usb_serial_port *port = tty->driver_data;
505 dbg("%s - port %d", __FUNCTION__, port->number);
507 if (!port->open_count) {
508 dbg("%s - port not open", __FUNCTION__);
512 if (port->serial->type->tiocmget)
513 return port->serial->type->tiocmget(port, file);
518 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
519 unsigned int set, unsigned int clear)
521 struct usb_serial_port *port = tty->driver_data;
526 dbg("%s - port %d", __FUNCTION__, port->number);
528 if (!port->open_count) {
529 dbg("%s - port not open", __FUNCTION__);
533 if (port->serial->type->tiocmset)
534 return port->serial->type->tiocmset(port, file, set, clear);
540 * We would be calling tty_wakeup here, but unfortunately some line
541 * disciplines have an annoying habit of calling tty->write from
542 * the write wakeup callback (e.g. n_hdlc.c).
544 void usb_serial_port_softint(struct usb_serial_port *port)
546 schedule_work(&port->work);
549 static void usb_serial_port_work(struct work_struct *work)
551 struct usb_serial_port *port =
552 container_of(work, struct usb_serial_port, work);
553 struct tty_struct *tty;
555 dbg("%s - port %d", __FUNCTION__, port->number);
567 static void port_release(struct device *dev)
569 struct usb_serial_port *port = to_usb_serial_port(dev);
571 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
575 static void kill_traffic(struct usb_serial_port *port)
577 usb_kill_urb(port->read_urb);
578 usb_kill_urb(port->write_urb);
579 usb_kill_urb(port->interrupt_in_urb);
580 usb_kill_urb(port->interrupt_out_urb);
583 static void port_free(struct usb_serial_port *port)
586 usb_free_urb(port->read_urb);
587 usb_free_urb(port->write_urb);
588 usb_free_urb(port->interrupt_in_urb);
589 usb_free_urb(port->interrupt_out_urb);
590 kfree(port->bulk_in_buffer);
591 kfree(port->bulk_out_buffer);
592 kfree(port->interrupt_in_buffer);
593 kfree(port->interrupt_out_buffer);
594 flush_scheduled_work(); /* port->work */
598 static struct usb_serial * create_serial (struct usb_device *dev,
599 struct usb_interface *interface,
600 struct usb_serial_driver *driver)
602 struct usb_serial *serial;
604 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
606 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
609 serial->dev = usb_get_dev(dev);
610 serial->type = driver;
611 serial->interface = interface;
612 kref_init(&serial->kref);
617 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
618 struct usb_serial_driver *drv)
620 struct usb_dynid *dynid;
622 spin_lock(&drv->dynids.lock);
623 list_for_each_entry(dynid, &drv->dynids.list, node) {
624 if (usb_match_one_id(intf, &dynid->id)) {
625 spin_unlock(&drv->dynids.lock);
629 spin_unlock(&drv->dynids.lock);
633 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
634 struct usb_interface *intf)
636 const struct usb_device_id *id;
638 id = usb_match_id(intf, drv->id_table);
640 dbg("static descriptor matches");
643 id = match_dynamic_id(intf, drv);
645 dbg("dynamic descriptor matches");
650 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
653 const struct usb_device_id *id;
654 struct usb_serial_driver *t;
656 /* Check if the usb id matches a known device */
657 list_for_each(p, &usb_serial_driver_list) {
658 t = list_entry(p, struct usb_serial_driver, driver_list);
659 id = get_iface_id(t, iface);
667 int usb_serial_probe(struct usb_interface *interface,
668 const struct usb_device_id *id)
670 struct usb_device *dev = interface_to_usbdev (interface);
671 struct usb_serial *serial = NULL;
672 struct usb_serial_port *port;
673 struct usb_host_interface *iface_desc;
674 struct usb_endpoint_descriptor *endpoint;
675 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
676 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
677 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
678 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
679 struct usb_serial_driver *type = NULL;
684 int num_interrupt_in = 0;
685 int num_interrupt_out = 0;
687 int num_bulk_out = 0;
691 lock_kernel(); /* guard against unloading a serial driver module */
692 type = search_serial_device(interface);
699 serial = create_serial (dev, interface, type);
702 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
706 /* if this device type has a probe function, call it */
708 const struct usb_device_id *id;
710 if (!try_module_get(type->driver.owner)) {
712 dev_err(&interface->dev, "module get failed, exiting\n");
717 id = get_iface_id(type, interface);
718 retval = type->probe(serial, id);
719 module_put(type->driver.owner);
723 dbg ("sub driver rejected device");
729 /* descriptor matches, let's find the endpoints needed */
730 /* check out the endpoints */
731 iface_desc = interface->cur_altsetting;
732 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
733 endpoint = &iface_desc->endpoint[i].desc;
735 if (usb_endpoint_is_bulk_in(endpoint)) {
736 /* we found a bulk in endpoint */
737 dbg("found bulk in on endpoint %d", i);
738 bulk_in_endpoint[num_bulk_in] = endpoint;
742 if (usb_endpoint_is_bulk_out(endpoint)) {
743 /* we found a bulk out endpoint */
744 dbg("found bulk out on endpoint %d", i);
745 bulk_out_endpoint[num_bulk_out] = endpoint;
749 if (usb_endpoint_is_int_in(endpoint)) {
750 /* we found a interrupt in endpoint */
751 dbg("found interrupt in on endpoint %d", i);
752 interrupt_in_endpoint[num_interrupt_in] = endpoint;
756 if (usb_endpoint_is_int_out(endpoint)) {
757 /* we found an interrupt out endpoint */
758 dbg("found interrupt out on endpoint %d", i);
759 interrupt_out_endpoint[num_interrupt_out] = endpoint;
764 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
765 /* BEGIN HORRIBLE HACK FOR PL2303 */
766 /* this is needed due to the looney way its endpoints are set up */
767 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
768 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
769 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
770 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
771 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
772 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID))) {
773 if (interface != dev->actconfig->interface[0]) {
774 /* check out the endpoints of the other interface*/
775 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
776 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
777 endpoint = &iface_desc->endpoint[i].desc;
778 if (usb_endpoint_is_int_in(endpoint)) {
779 /* we found a interrupt in endpoint */
780 dbg("found interrupt in for Prolific device on separate interface");
781 interrupt_in_endpoint[num_interrupt_in] = endpoint;
787 /* Now make sure the PL-2303 is configured correctly.
788 * If not, give up now and hope this hack will work
789 * properly during a later invocation of usb_serial_probe
791 if (num_bulk_in == 0 || num_bulk_out == 0) {
793 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
798 /* END HORRIBLE HACK FOR PL2303 */
801 /* found all that we need */
802 dev_info(&interface->dev, "%s converter detected\n", type->description);
804 #ifdef CONFIG_USB_SERIAL_GENERIC
805 if (type == &usb_serial_generic_device) {
806 num_ports = num_bulk_out;
807 if (num_ports == 0) {
809 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
816 /* if this device type has a calc_num_ports function, call it */
817 if (type->calc_num_ports) {
818 if (!try_module_get(type->driver.owner)) {
820 dev_err(&interface->dev, "module get failed, exiting\n");
824 num_ports = type->calc_num_ports (serial);
825 module_put(type->driver.owner);
828 num_ports = type->num_ports;
831 serial->num_ports = num_ports;
832 serial->num_bulk_in = num_bulk_in;
833 serial->num_bulk_out = num_bulk_out;
834 serial->num_interrupt_in = num_interrupt_in;
835 serial->num_interrupt_out = num_interrupt_out;
837 /* create our ports, we need as many as the max endpoints */
838 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
839 max_endpoints = max(num_bulk_in, num_bulk_out);
840 max_endpoints = max(max_endpoints, num_interrupt_in);
841 max_endpoints = max(max_endpoints, num_interrupt_out);
842 max_endpoints = max(max_endpoints, (int)serial->num_ports);
843 serial->num_port_pointers = max_endpoints;
846 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
847 for (i = 0; i < max_endpoints; ++i) {
848 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
851 port->serial = serial;
852 spin_lock_init(&port->lock);
853 mutex_init(&port->mutex);
854 INIT_WORK(&port->work, usb_serial_port_work);
855 serial->port[i] = port;
858 /* set up the endpoint information */
859 for (i = 0; i < num_bulk_in; ++i) {
860 endpoint = bulk_in_endpoint[i];
861 port = serial->port[i];
862 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
863 if (!port->read_urb) {
864 dev_err(&interface->dev, "No free urbs available\n");
867 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
868 port->bulk_in_size = buffer_size;
869 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
870 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
871 if (!port->bulk_in_buffer) {
872 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
875 usb_fill_bulk_urb (port->read_urb, dev,
876 usb_rcvbulkpipe (dev,
877 endpoint->bEndpointAddress),
878 port->bulk_in_buffer, buffer_size,
879 serial->type->read_bulk_callback,
883 for (i = 0; i < num_bulk_out; ++i) {
884 endpoint = bulk_out_endpoint[i];
885 port = serial->port[i];
886 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
887 if (!port->write_urb) {
888 dev_err(&interface->dev, "No free urbs available\n");
891 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
892 port->bulk_out_size = buffer_size;
893 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
894 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
895 if (!port->bulk_out_buffer) {
896 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
899 usb_fill_bulk_urb (port->write_urb, dev,
900 usb_sndbulkpipe (dev,
901 endpoint->bEndpointAddress),
902 port->bulk_out_buffer, buffer_size,
903 serial->type->write_bulk_callback,
907 if (serial->type->read_int_callback) {
908 for (i = 0; i < num_interrupt_in; ++i) {
909 endpoint = interrupt_in_endpoint[i];
910 port = serial->port[i];
911 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
912 if (!port->interrupt_in_urb) {
913 dev_err(&interface->dev, "No free urbs available\n");
916 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
917 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
918 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
919 if (!port->interrupt_in_buffer) {
920 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
923 usb_fill_int_urb (port->interrupt_in_urb, dev,
925 endpoint->bEndpointAddress),
926 port->interrupt_in_buffer, buffer_size,
927 serial->type->read_int_callback, port,
928 endpoint->bInterval);
930 } else if (num_interrupt_in) {
931 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
934 if (serial->type->write_int_callback) {
935 for (i = 0; i < num_interrupt_out; ++i) {
936 endpoint = interrupt_out_endpoint[i];
937 port = serial->port[i];
938 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
939 if (!port->interrupt_out_urb) {
940 dev_err(&interface->dev, "No free urbs available\n");
943 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
944 port->interrupt_out_size = buffer_size;
945 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
946 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
947 if (!port->interrupt_out_buffer) {
948 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
951 usb_fill_int_urb (port->interrupt_out_urb, dev,
953 endpoint->bEndpointAddress),
954 port->interrupt_out_buffer, buffer_size,
955 serial->type->write_int_callback, port,
956 endpoint->bInterval);
958 } else if (num_interrupt_out) {
959 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
962 /* if this device type has an attach function, call it */
964 if (!try_module_get(type->driver.owner)) {
965 dev_err(&interface->dev, "module get failed, exiting\n");
968 retval = type->attach (serial);
969 module_put(type->driver.owner);
973 /* quietly accept this device, but don't bind to a serial port
974 * as it's about to disappear */
979 if (get_free_serial (serial, num_ports, &minor) == NULL) {
980 dev_err(&interface->dev, "No more free serial devices\n");
983 serial->minor = minor;
985 /* register all of the individual ports with the driver core */
986 for (i = 0; i < num_ports; ++i) {
987 port = serial->port[i];
988 port->dev.parent = &interface->dev;
989 port->dev.driver = NULL;
990 port->dev.bus = &usb_serial_bus_type;
991 port->dev.release = &port_release;
993 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
994 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
995 retval = device_register(&port->dev);
997 dev_err(&port->dev, "Error registering port device, "
1001 usb_serial_console_init (debug, minor);
1005 usb_set_intfdata (interface, serial);
1009 for (i = 0; i < num_bulk_in; ++i) {
1010 port = serial->port[i];
1013 usb_free_urb(port->read_urb);
1014 kfree(port->bulk_in_buffer);
1016 for (i = 0; i < num_bulk_out; ++i) {
1017 port = serial->port[i];
1020 usb_free_urb(port->write_urb);
1021 kfree(port->bulk_out_buffer);
1023 for (i = 0; i < num_interrupt_in; ++i) {
1024 port = serial->port[i];
1027 usb_free_urb(port->interrupt_in_urb);
1028 kfree(port->interrupt_in_buffer);
1030 for (i = 0; i < num_interrupt_out; ++i) {
1031 port = serial->port[i];
1034 usb_free_urb(port->interrupt_out_urb);
1035 kfree(port->interrupt_out_buffer);
1038 /* free up any memory that we allocated */
1039 for (i = 0; i < serial->num_port_pointers; ++i)
1040 kfree(serial->port[i]);
1045 void usb_serial_disconnect(struct usb_interface *interface)
1048 struct usb_serial *serial = usb_get_intfdata (interface);
1049 struct device *dev = &interface->dev;
1050 struct usb_serial_port *port;
1052 usb_serial_console_disconnect(serial);
1053 dbg ("%s", __FUNCTION__);
1055 usb_set_intfdata (interface, NULL);
1057 for (i = 0; i < serial->num_ports; ++i) {
1058 port = serial->port[i];
1061 tty_hangup(port->tty);
1065 /* let the last holder of this object
1066 * cause it to be cleaned up */
1067 usb_serial_put(serial);
1069 dev_info(dev, "device disconnected\n");
1072 static const struct tty_operations serial_ops = {
1073 .open = serial_open,
1074 .close = serial_close,
1075 .write = serial_write,
1076 .write_room = serial_write_room,
1077 .ioctl = serial_ioctl,
1078 .set_termios = serial_set_termios,
1079 .throttle = serial_throttle,
1080 .unthrottle = serial_unthrottle,
1081 .break_ctl = serial_break,
1082 .chars_in_buffer = serial_chars_in_buffer,
1083 .read_proc = serial_read_proc,
1084 .tiocmget = serial_tiocmget,
1085 .tiocmset = serial_tiocmset,
1088 struct tty_driver *usb_serial_tty_driver;
1090 static int __init usb_serial_init(void)
1095 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1096 if (!usb_serial_tty_driver)
1099 /* Initialize our global data */
1100 spin_lock_init(&table_lock);
1101 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1102 serial_table[i] = NULL;
1105 result = bus_register(&usb_serial_bus_type);
1107 err("%s - registering bus driver failed", __FUNCTION__);
1111 usb_serial_tty_driver->owner = THIS_MODULE;
1112 usb_serial_tty_driver->driver_name = "usbserial";
1113 usb_serial_tty_driver->name = "ttyUSB";
1114 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1115 usb_serial_tty_driver->minor_start = 0;
1116 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1117 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1118 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1119 usb_serial_tty_driver->init_termios = tty_std_termios;
1120 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1121 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1122 result = tty_register_driver(usb_serial_tty_driver);
1124 err("%s - tty_register_driver failed", __FUNCTION__);
1125 goto exit_reg_driver;
1128 /* register the USB driver */
1129 result = usb_register(&usb_serial_driver);
1131 err("%s - usb_register failed", __FUNCTION__);
1135 /* register the generic driver, if we should */
1136 result = usb_serial_generic_register(debug);
1138 err("%s - registering generic driver failed", __FUNCTION__);
1147 usb_deregister(&usb_serial_driver);
1150 tty_unregister_driver(usb_serial_tty_driver);
1153 bus_unregister(&usb_serial_bus_type);
1156 err ("%s - returning with error %d", __FUNCTION__, result);
1157 put_tty_driver(usb_serial_tty_driver);
1162 static void __exit usb_serial_exit(void)
1164 usb_serial_console_exit();
1166 usb_serial_generic_deregister();
1168 usb_deregister(&usb_serial_driver);
1169 tty_unregister_driver(usb_serial_tty_driver);
1170 put_tty_driver(usb_serial_tty_driver);
1171 bus_unregister(&usb_serial_bus_type);
1175 module_init(usb_serial_init);
1176 module_exit(usb_serial_exit);
1178 #define set_to_generic_if_null(type, function) \
1180 if (!type->function) { \
1181 type->function = usb_serial_generic_##function; \
1182 dbg("Had to override the " #function \
1183 " usb serial operation with the generic one.");\
1187 static void fixup_generic(struct usb_serial_driver *device)
1189 set_to_generic_if_null(device, open);
1190 set_to_generic_if_null(device, write);
1191 set_to_generic_if_null(device, close);
1192 set_to_generic_if_null(device, write_room);
1193 set_to_generic_if_null(device, chars_in_buffer);
1194 set_to_generic_if_null(device, read_bulk_callback);
1195 set_to_generic_if_null(device, write_bulk_callback);
1196 set_to_generic_if_null(device, shutdown);
1199 int usb_serial_register(struct usb_serial_driver *driver) /* must be called with BKL held */
1203 fixup_generic(driver);
1205 if (!driver->description)
1206 driver->description = driver->driver.name;
1208 /* Add this device to our list of devices */
1209 list_add(&driver->driver_list, &usb_serial_driver_list);
1211 retval = usb_serial_bus_register(driver);
1213 err("problem %d when registering driver %s", retval, driver->description);
1214 list_del(&driver->driver_list);
1217 info("USB Serial support registered for %s", driver->description);
1223 void usb_serial_deregister(struct usb_serial_driver *device) /* must be called with BKL held */
1225 info("USB Serial deregistering driver %s", device->description);
1226 list_del(&device->driver_list);
1227 usb_serial_bus_deregister(device);
1232 /* If the usb-serial core is built into the core, the usb-serial drivers
1233 need these symbols to load properly as modules. */
1234 EXPORT_SYMBOL_GPL(usb_serial_register);
1235 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1236 EXPORT_SYMBOL_GPL(usb_serial_probe);
1237 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1238 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1241 /* Module information */
1242 MODULE_AUTHOR( DRIVER_AUTHOR );
1243 MODULE_DESCRIPTION( DRIVER_DESC );
1244 MODULE_LICENSE("GPL");
1246 module_param(debug, bool, S_IRUGO | S_IWUSR);
1247 MODULE_PARM_DESC(debug, "Debug enabled or not");