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 /*****************************************************************************
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];
194 goto bailout_kref_put;
197 if (mutex_lock_interruptible(&port->mutex)) {
198 retval = -ERESTARTSYS;
199 goto bailout_kref_put;
204 /* set up our port structure making the tty driver
205 * remember our port object, and us it */
206 tty->driver_data = port;
209 if (port->open_count == 1) {
211 /* lock this module before we call it
212 * this may fail, which means we must bail out,
213 * safe because we are called with BKL held */
214 if (!try_module_get(serial->type->driver.owner)) {
216 goto bailout_mutex_unlock;
219 /* only call the device specific open if this
220 * is the first time the port is opened */
221 retval = serial->type->open(port, filp);
223 goto bailout_module_put;
226 mutex_unlock(&port->mutex);
230 module_put(serial->type->driver.owner);
231 bailout_mutex_unlock:
232 port->open_count = 0;
233 mutex_unlock(&port->mutex);
235 kref_put(&serial->kref, destroy_serial);
239 static void serial_close(struct tty_struct *tty, struct file * filp)
241 struct usb_serial_port *port = tty->driver_data;
246 dbg("%s - port %d", __FUNCTION__, port->number);
248 mutex_lock(&port->mutex);
250 if (port->open_count == 0) {
251 mutex_unlock(&port->mutex);
256 if (port->open_count == 0) {
257 /* only call the device specific close if this
258 * port is being closed by the last owner */
259 port->serial->type->close(port, filp);
262 if (port->tty->driver_data)
263 port->tty->driver_data = NULL;
267 module_put(port->serial->type->driver.owner);
270 mutex_unlock(&port->mutex);
271 kref_put(&port->serial->kref, destroy_serial);
274 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
276 struct usb_serial_port *port = tty->driver_data;
277 int retval = -EINVAL;
282 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
284 if (!port->open_count) {
285 dbg("%s - port not opened", __FUNCTION__);
289 /* pass on to the driver specific version of this function */
290 retval = port->serial->type->write(port, buf, count);
296 static int serial_write_room (struct tty_struct *tty)
298 struct usb_serial_port *port = tty->driver_data;
299 int retval = -EINVAL;
304 dbg("%s - port %d", __FUNCTION__, port->number);
306 if (!port->open_count) {
307 dbg("%s - port not open", __FUNCTION__);
311 /* pass on to the driver specific version of this function */
312 retval = port->serial->type->write_room(port);
318 static int serial_chars_in_buffer (struct tty_struct *tty)
320 struct usb_serial_port *port = tty->driver_data;
321 int retval = -EINVAL;
326 dbg("%s = port %d", __FUNCTION__, port->number);
328 if (!port->open_count) {
329 dbg("%s - port not open", __FUNCTION__);
333 /* pass on to the driver specific version of this function */
334 retval = port->serial->type->chars_in_buffer(port);
340 static void serial_throttle (struct tty_struct * tty)
342 struct usb_serial_port *port = tty->driver_data;
347 dbg("%s - port %d", __FUNCTION__, port->number);
349 if (!port->open_count) {
350 dbg ("%s - port not open", __FUNCTION__);
354 /* pass on to the driver specific version of this function */
355 if (port->serial->type->throttle)
356 port->serial->type->throttle(port);
359 static void serial_unthrottle (struct tty_struct * tty)
361 struct usb_serial_port *port = tty->driver_data;
366 dbg("%s - port %d", __FUNCTION__, port->number);
368 if (!port->open_count) {
369 dbg("%s - port not open", __FUNCTION__);
373 /* pass on to the driver specific version of this function */
374 if (port->serial->type->unthrottle)
375 port->serial->type->unthrottle(port);
378 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
380 struct usb_serial_port *port = tty->driver_data;
381 int retval = -ENODEV;
386 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
388 if (!port->open_count) {
389 dbg ("%s - port not open", __FUNCTION__);
393 /* pass on to the driver specific version of this function if it is available */
394 if (port->serial->type->ioctl)
395 retval = port->serial->type->ioctl(port, file, cmd, arg);
397 retval = -ENOIOCTLCMD;
403 static void serial_set_termios (struct tty_struct *tty, struct termios * old)
405 struct usb_serial_port *port = tty->driver_data;
410 dbg("%s - port %d", __FUNCTION__, port->number);
412 if (!port->open_count) {
413 dbg("%s - port not open", __FUNCTION__);
417 /* pass on to the driver specific version of this function if it is available */
418 if (port->serial->type->set_termios)
419 port->serial->type->set_termios(port, old);
422 static void serial_break (struct tty_struct *tty, int break_state)
424 struct usb_serial_port *port = tty->driver_data;
429 dbg("%s - port %d", __FUNCTION__, port->number);
431 if (!port->open_count) {
432 dbg("%s - port not open", __FUNCTION__);
436 /* pass on to the driver specific version of this function if it is available */
437 if (port->serial->type->break_ctl)
438 port->serial->type->break_ctl(port, break_state);
441 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
443 struct usb_serial *serial;
449 dbg("%s", __FUNCTION__);
450 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
451 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
452 serial = usb_serial_get_by_index(i);
456 length += sprintf (page+length, "%d:", i);
457 if (serial->type->driver.owner)
458 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
459 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
460 length += sprintf (page+length, " vendor:%04x product:%04x",
461 le16_to_cpu(serial->dev->descriptor.idVendor),
462 le16_to_cpu(serial->dev->descriptor.idProduct));
463 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
464 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
466 usb_make_path(serial->dev, tmp, sizeof(tmp));
467 length += sprintf (page+length, " path:%s", tmp);
469 length += sprintf (page+length, "\n");
470 if ((length + begin) > (off + count))
472 if ((length + begin) < off) {
476 kref_put(&serial->kref, destroy_serial);
480 if (off >= (length + begin))
482 *start = page + (off-begin);
483 return ((count < begin+length-off) ? count : begin+length-off);
486 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
488 struct usb_serial_port *port = tty->driver_data;
493 dbg("%s - port %d", __FUNCTION__, port->number);
495 if (!port->open_count) {
496 dbg("%s - port not open", __FUNCTION__);
500 if (port->serial->type->tiocmget)
501 return port->serial->type->tiocmget(port, file);
507 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
508 unsigned int set, unsigned int clear)
510 struct usb_serial_port *port = tty->driver_data;
515 dbg("%s - port %d", __FUNCTION__, port->number);
517 if (!port->open_count) {
518 dbg("%s - port not open", __FUNCTION__);
522 if (port->serial->type->tiocmset)
523 return port->serial->type->tiocmset(port, file, set, clear);
529 void usb_serial_port_softint(void *private)
531 struct usb_serial_port *port = private;
532 struct tty_struct *tty;
534 dbg("%s - port %d", __FUNCTION__, port->number);
546 static void port_release(struct device *dev)
548 struct usb_serial_port *port = to_usb_serial_port(dev);
550 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
551 usb_kill_urb(port->read_urb);
552 usb_free_urb(port->read_urb);
553 usb_kill_urb(port->write_urb);
554 usb_free_urb(port->write_urb);
555 usb_kill_urb(port->interrupt_in_urb);
556 usb_free_urb(port->interrupt_in_urb);
557 usb_kill_urb(port->interrupt_out_urb);
558 usb_free_urb(port->interrupt_out_urb);
559 kfree(port->bulk_in_buffer);
560 kfree(port->bulk_out_buffer);
561 kfree(port->interrupt_in_buffer);
562 kfree(port->interrupt_out_buffer);
566 static struct usb_serial * create_serial (struct usb_device *dev,
567 struct usb_interface *interface,
568 struct usb_serial_driver *driver)
570 struct usb_serial *serial;
572 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
574 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
577 serial->dev = usb_get_dev(dev);
578 serial->type = driver;
579 serial->interface = interface;
580 kref_init(&serial->kref);
585 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
588 const struct usb_device_id *id;
589 struct usb_serial_driver *t;
591 /* Check if the usb id matches a known device */
592 list_for_each(p, &usb_serial_driver_list) {
593 t = list_entry(p, struct usb_serial_driver, driver_list);
594 id = usb_match_id(iface, t->id_table);
596 dbg("descriptor matches");
604 int usb_serial_probe(struct usb_interface *interface,
605 const struct usb_device_id *id)
607 struct usb_device *dev = interface_to_usbdev (interface);
608 struct usb_serial *serial = NULL;
609 struct usb_serial_port *port;
610 struct usb_host_interface *iface_desc;
611 struct usb_endpoint_descriptor *endpoint;
612 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
613 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
614 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
615 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
616 struct usb_serial_driver *type = NULL;
621 int num_interrupt_in = 0;
622 int num_interrupt_out = 0;
624 int num_bulk_out = 0;
628 type = search_serial_device(interface);
634 serial = create_serial (dev, interface, type);
636 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
640 /* if this device type has a probe function, call it */
642 const struct usb_device_id *id;
644 if (!try_module_get(type->driver.owner)) {
645 dev_err(&interface->dev, "module get failed, exiting\n");
650 id = usb_match_id(interface, type->id_table);
651 retval = type->probe(serial, id);
652 module_put(type->driver.owner);
655 dbg ("sub driver rejected device");
661 /* descriptor matches, let's find the endpoints needed */
662 /* check out the endpoints */
663 iface_desc = interface->cur_altsetting;
664 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
665 endpoint = &iface_desc->endpoint[i].desc;
667 if ((endpoint->bEndpointAddress & 0x80) &&
668 ((endpoint->bmAttributes & 3) == 0x02)) {
669 /* we found a bulk in endpoint */
670 dbg("found bulk in on endpoint %d", i);
671 bulk_in_endpoint[num_bulk_in] = endpoint;
675 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
676 ((endpoint->bmAttributes & 3) == 0x02)) {
677 /* we found a bulk out endpoint */
678 dbg("found bulk out on endpoint %d", i);
679 bulk_out_endpoint[num_bulk_out] = endpoint;
683 if ((endpoint->bEndpointAddress & 0x80) &&
684 ((endpoint->bmAttributes & 3) == 0x03)) {
685 /* we found a interrupt in endpoint */
686 dbg("found interrupt in on endpoint %d", i);
687 interrupt_in_endpoint[num_interrupt_in] = endpoint;
691 if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
692 ((endpoint->bmAttributes & 3) == 0x03)) {
693 /* we found an interrupt out endpoint */
694 dbg("found interrupt out on endpoint %d", i);
695 interrupt_out_endpoint[num_interrupt_out] = endpoint;
700 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
701 /* BEGIN HORRIBLE HACK FOR PL2303 */
702 /* this is needed due to the looney way its endpoints are set up */
703 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
704 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
705 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
706 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID))) {
707 if (interface != dev->actconfig->interface[0]) {
708 /* check out the endpoints of the other interface*/
709 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
710 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
711 endpoint = &iface_desc->endpoint[i].desc;
712 if ((endpoint->bEndpointAddress & 0x80) &&
713 ((endpoint->bmAttributes & 3) == 0x03)) {
714 /* we found a interrupt in endpoint */
715 dbg("found interrupt in for Prolific device on separate interface");
716 interrupt_in_endpoint[num_interrupt_in] = endpoint;
722 /* Now make sure the PL-2303 is configured correctly.
723 * If not, give up now and hope this hack will work
724 * properly during a later invocation of usb_serial_probe
726 if (num_bulk_in == 0 || num_bulk_out == 0) {
727 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
732 /* END HORRIBLE HACK FOR PL2303 */
735 /* found all that we need */
736 dev_info(&interface->dev, "%s converter detected\n", type->description);
738 #ifdef CONFIG_USB_SERIAL_GENERIC
739 if (type == &usb_serial_generic_device) {
740 num_ports = num_bulk_out;
741 if (num_ports == 0) {
742 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
749 /* if this device type has a calc_num_ports function, call it */
750 if (type->calc_num_ports) {
751 if (!try_module_get(type->driver.owner)) {
752 dev_err(&interface->dev, "module get failed, exiting\n");
756 num_ports = type->calc_num_ports (serial);
757 module_put(type->driver.owner);
760 num_ports = type->num_ports;
763 if (get_free_serial (serial, num_ports, &minor) == NULL) {
764 dev_err(&interface->dev, "No more free serial devices\n");
769 serial->minor = minor;
770 serial->num_ports = num_ports;
771 serial->num_bulk_in = num_bulk_in;
772 serial->num_bulk_out = num_bulk_out;
773 serial->num_interrupt_in = num_interrupt_in;
774 serial->num_interrupt_out = num_interrupt_out;
776 /* create our ports, we need as many as the max endpoints */
777 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
778 max_endpoints = max(num_bulk_in, num_bulk_out);
779 max_endpoints = max(max_endpoints, num_interrupt_in);
780 max_endpoints = max(max_endpoints, num_interrupt_out);
781 max_endpoints = max(max_endpoints, (int)serial->num_ports);
782 serial->num_port_pointers = max_endpoints;
783 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
784 for (i = 0; i < max_endpoints; ++i) {
785 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
788 port->number = i + serial->minor;
789 port->serial = serial;
790 spin_lock_init(&port->lock);
791 mutex_init(&port->mutex);
792 INIT_WORK(&port->work, usb_serial_port_softint, port);
793 serial->port[i] = port;
796 /* set up the endpoint information */
797 for (i = 0; i < num_bulk_in; ++i) {
798 endpoint = bulk_in_endpoint[i];
799 port = serial->port[i];
800 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
801 if (!port->read_urb) {
802 dev_err(&interface->dev, "No free urbs available\n");
805 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
806 port->bulk_in_size = buffer_size;
807 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
808 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
809 if (!port->bulk_in_buffer) {
810 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
813 usb_fill_bulk_urb (port->read_urb, dev,
814 usb_rcvbulkpipe (dev,
815 endpoint->bEndpointAddress),
816 port->bulk_in_buffer, buffer_size,
817 serial->type->read_bulk_callback,
821 for (i = 0; i < num_bulk_out; ++i) {
822 endpoint = bulk_out_endpoint[i];
823 port = serial->port[i];
824 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
825 if (!port->write_urb) {
826 dev_err(&interface->dev, "No free urbs available\n");
829 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
830 port->bulk_out_size = buffer_size;
831 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
832 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
833 if (!port->bulk_out_buffer) {
834 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
837 usb_fill_bulk_urb (port->write_urb, dev,
838 usb_sndbulkpipe (dev,
839 endpoint->bEndpointAddress),
840 port->bulk_out_buffer, buffer_size,
841 serial->type->write_bulk_callback,
845 if (serial->type->read_int_callback) {
846 for (i = 0; i < num_interrupt_in; ++i) {
847 endpoint = interrupt_in_endpoint[i];
848 port = serial->port[i];
849 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
850 if (!port->interrupt_in_urb) {
851 dev_err(&interface->dev, "No free urbs available\n");
854 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
855 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
856 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
857 if (!port->interrupt_in_buffer) {
858 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
861 usb_fill_int_urb (port->interrupt_in_urb, dev,
863 endpoint->bEndpointAddress),
864 port->interrupt_in_buffer, buffer_size,
865 serial->type->read_int_callback, port,
866 endpoint->bInterval);
868 } else if (num_interrupt_in) {
869 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
872 if (serial->type->write_int_callback) {
873 for (i = 0; i < num_interrupt_out; ++i) {
874 endpoint = interrupt_out_endpoint[i];
875 port = serial->port[i];
876 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
877 if (!port->interrupt_out_urb) {
878 dev_err(&interface->dev, "No free urbs available\n");
881 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
882 port->interrupt_out_size = buffer_size;
883 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
884 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
885 if (!port->interrupt_out_buffer) {
886 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
889 usb_fill_int_urb (port->interrupt_out_urb, dev,
891 endpoint->bEndpointAddress),
892 port->interrupt_out_buffer, buffer_size,
893 serial->type->write_int_callback, port,
894 endpoint->bInterval);
896 } else if (num_interrupt_out) {
897 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
900 /* if this device type has an attach function, call it */
902 if (!try_module_get(type->driver.owner)) {
903 dev_err(&interface->dev, "module get failed, exiting\n");
906 retval = type->attach (serial);
907 module_put(type->driver.owner);
911 /* quietly accept this device, but don't bind to a serial port
912 * as it's about to disappear */
917 /* register all of the individual ports with the driver core */
918 for (i = 0; i < num_ports; ++i) {
919 port = serial->port[i];
920 port->dev.parent = &interface->dev;
921 port->dev.driver = NULL;
922 port->dev.bus = &usb_serial_bus_type;
923 port->dev.release = &port_release;
925 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
926 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
927 device_register (&port->dev);
930 usb_serial_console_init (debug, minor);
934 usb_set_intfdata (interface, serial);
938 for (i = 0; i < num_bulk_in; ++i) {
939 port = serial->port[i];
943 usb_free_urb (port->read_urb);
944 kfree(port->bulk_in_buffer);
946 for (i = 0; i < num_bulk_out; ++i) {
947 port = serial->port[i];
951 usb_free_urb (port->write_urb);
952 kfree(port->bulk_out_buffer);
954 for (i = 0; i < num_interrupt_in; ++i) {
955 port = serial->port[i];
958 if (port->interrupt_in_urb)
959 usb_free_urb (port->interrupt_in_urb);
960 kfree(port->interrupt_in_buffer);
962 for (i = 0; i < num_interrupt_out; ++i) {
963 port = serial->port[i];
966 if (port->interrupt_out_urb)
967 usb_free_urb (port->interrupt_out_urb);
968 kfree(port->interrupt_out_buffer);
971 /* return the minor range that this device had */
972 return_serial (serial);
974 /* free up any memory that we allocated */
975 for (i = 0; i < serial->num_port_pointers; ++i)
976 kfree(serial->port[i]);
981 void usb_serial_disconnect(struct usb_interface *interface)
984 struct usb_serial *serial = usb_get_intfdata (interface);
985 struct device *dev = &interface->dev;
986 struct usb_serial_port *port;
988 dbg ("%s", __FUNCTION__);
990 usb_set_intfdata (interface, NULL);
992 for (i = 0; i < serial->num_ports; ++i) {
993 port = serial->port[i];
994 if (port && port->tty)
995 tty_hangup(port->tty);
997 /* let the last holder of this object
998 * cause it to be cleaned up */
999 kref_put(&serial->kref, destroy_serial);
1001 dev_info(dev, "device disconnected\n");
1004 static struct tty_operations serial_ops = {
1005 .open = serial_open,
1006 .close = serial_close,
1007 .write = serial_write,
1008 .write_room = serial_write_room,
1009 .ioctl = serial_ioctl,
1010 .set_termios = serial_set_termios,
1011 .throttle = serial_throttle,
1012 .unthrottle = serial_unthrottle,
1013 .break_ctl = serial_break,
1014 .chars_in_buffer = serial_chars_in_buffer,
1015 .read_proc = serial_read_proc,
1016 .tiocmget = serial_tiocmget,
1017 .tiocmset = serial_tiocmset,
1020 struct tty_driver *usb_serial_tty_driver;
1022 static int __init usb_serial_init(void)
1027 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1028 if (!usb_serial_tty_driver)
1031 /* Initialize our global data */
1032 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1033 serial_table[i] = NULL;
1036 result = bus_register(&usb_serial_bus_type);
1038 err("%s - registering bus driver failed", __FUNCTION__);
1042 usb_serial_tty_driver->owner = THIS_MODULE;
1043 usb_serial_tty_driver->driver_name = "usbserial";
1044 usb_serial_tty_driver->devfs_name = "usb/tts/";
1045 usb_serial_tty_driver->name = "ttyUSB";
1046 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1047 usb_serial_tty_driver->minor_start = 0;
1048 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1049 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1050 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1051 usb_serial_tty_driver->init_termios = tty_std_termios;
1052 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1053 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1054 result = tty_register_driver(usb_serial_tty_driver);
1056 err("%s - tty_register_driver failed", __FUNCTION__);
1057 goto exit_reg_driver;
1060 /* register the USB driver */
1061 result = usb_register(&usb_serial_driver);
1063 err("%s - usb_register failed", __FUNCTION__);
1067 /* register the generic driver, if we should */
1068 result = usb_serial_generic_register(debug);
1070 err("%s - registering generic driver failed", __FUNCTION__);
1079 usb_deregister(&usb_serial_driver);
1082 tty_unregister_driver(usb_serial_tty_driver);
1085 bus_unregister(&usb_serial_bus_type);
1088 err ("%s - returning with error %d", __FUNCTION__, result);
1089 put_tty_driver(usb_serial_tty_driver);
1094 static void __exit usb_serial_exit(void)
1096 usb_serial_console_exit();
1098 usb_serial_generic_deregister();
1100 usb_deregister(&usb_serial_driver);
1101 tty_unregister_driver(usb_serial_tty_driver);
1102 put_tty_driver(usb_serial_tty_driver);
1103 bus_unregister(&usb_serial_bus_type);
1107 module_init(usb_serial_init);
1108 module_exit(usb_serial_exit);
1110 #define set_to_generic_if_null(type, function) \
1112 if (!type->function) { \
1113 type->function = usb_serial_generic_##function; \
1114 dbg("Had to override the " #function \
1115 " usb serial operation with the generic one.");\
1119 static void fixup_generic(struct usb_serial_driver *device)
1121 set_to_generic_if_null(device, open);
1122 set_to_generic_if_null(device, write);
1123 set_to_generic_if_null(device, close);
1124 set_to_generic_if_null(device, write_room);
1125 set_to_generic_if_null(device, chars_in_buffer);
1126 set_to_generic_if_null(device, read_bulk_callback);
1127 set_to_generic_if_null(device, write_bulk_callback);
1128 set_to_generic_if_null(device, shutdown);
1131 int usb_serial_register(struct usb_serial_driver *driver)
1135 fixup_generic(driver);
1137 if (!driver->description)
1138 driver->description = driver->driver.name;
1140 /* Add this device to our list of devices */
1141 list_add(&driver->driver_list, &usb_serial_driver_list);
1143 retval = usb_serial_bus_register(driver);
1145 err("problem %d when registering driver %s", retval, driver->description);
1146 list_del(&driver->driver_list);
1149 info("USB Serial support registered for %s", driver->description);
1155 void usb_serial_deregister(struct usb_serial_driver *device)
1157 info("USB Serial deregistering driver %s", device->description);
1158 list_del(&device->driver_list);
1159 usb_serial_bus_deregister(device);
1164 /* If the usb-serial core is built into the core, the usb-serial drivers
1165 need these symbols to load properly as modules. */
1166 EXPORT_SYMBOL_GPL(usb_serial_register);
1167 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1168 EXPORT_SYMBOL_GPL(usb_serial_probe);
1169 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1170 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1173 /* Module information */
1174 MODULE_AUTHOR( DRIVER_AUTHOR );
1175 MODULE_DESCRIPTION( DRIVER_DESC );
1176 MODULE_LICENSE("GPL");
1178 module_param(debug, bool, S_IRUGO | S_IWUSR);
1179 MODULE_PARM_DESC(debug, "Debug enabled or not");