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
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/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,
50 .suspend = usb_serial_suspend,
51 .resume = usb_serial_resume,
55 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
56 the MODULE_DEVICE_TABLE declarations in each serial driver
57 cause the "hotplug" program to pull in whatever module is necessary
58 via modprobe, and modprobe will load usbserial because the serial
63 /* initially all NULL */
64 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
65 static DEFINE_MUTEX(table_lock);
66 static LIST_HEAD(usb_serial_driver_list);
68 struct usb_serial *usb_serial_get_by_index(unsigned index)
70 struct usb_serial *serial;
72 mutex_lock(&table_lock);
73 serial = serial_table[index];
76 kref_get(&serial->kref);
77 mutex_unlock(&table_lock);
81 static struct usb_serial *get_free_serial(struct usb_serial *serial,
82 int num_ports, unsigned int *minor)
87 dbg("%s %d", __func__, num_ports);
90 mutex_lock(&table_lock);
91 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
96 for (j = 1; j <= num_ports-1; ++j)
97 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
107 dbg("%s - minor base = %d", __func__, *minor);
108 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
109 serial_table[i] = serial;
110 serial->port[j++]->number = i;
112 mutex_unlock(&table_lock);
115 mutex_unlock(&table_lock);
119 static void return_serial(struct usb_serial *serial)
125 for (i = 0; i < serial->num_ports; ++i)
126 serial_table[serial->minor + i] = NULL;
129 static void destroy_serial(struct kref *kref)
131 struct usb_serial *serial;
132 struct usb_serial_port *port;
135 serial = to_usb_serial(kref);
137 dbg("%s - %s", __func__, serial->type->description);
139 serial->type->shutdown(serial);
141 /* return the minor range that this device had */
142 if (serial->minor != SERIAL_TTY_NO_MINOR)
143 return_serial(serial);
145 for (i = 0; i < serial->num_ports; ++i)
146 serial->port[i]->port.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;
160 i < serial->num_port_pointers; ++i) {
161 port = serial->port[i];
168 usb_put_dev(serial->dev);
170 /* free up any memory that we allocated */
174 void usb_serial_put(struct usb_serial *serial)
176 mutex_lock(&table_lock);
177 kref_put(&serial->kref, destroy_serial);
178 mutex_unlock(&table_lock);
181 /*****************************************************************************
182 * Driver tty interface functions
183 *****************************************************************************/
184 static int serial_open (struct tty_struct *tty, struct file *filp)
186 struct usb_serial *serial;
187 struct usb_serial_port *port;
188 unsigned int portNumber;
193 /* get the serial object associated with this tty pointer */
194 serial = usb_serial_get_by_index(tty->index);
196 tty->driver_data = NULL;
200 portNumber = tty->index - serial->minor;
201 port = serial->port[portNumber];
204 goto bailout_kref_put;
207 if (port->serial->disconnected) {
209 goto bailout_kref_put;
212 if (mutex_lock_interruptible(&port->mutex)) {
213 retval = -ERESTARTSYS;
214 goto bailout_kref_put;
219 /* set up our port structure making the tty driver
220 * remember our port object, and us it */
221 tty->driver_data = port;
222 tty_port_tty_set(&port->port, tty);
224 if (port->port.count == 1) {
226 /* lock this module before we call it
227 * this may fail, which means we must bail out,
228 * safe because we are called with BKL held */
229 if (!try_module_get(serial->type->driver.owner)) {
231 goto bailout_mutex_unlock;
234 retval = usb_autopm_get_interface(serial->interface);
236 goto bailout_module_put;
237 /* only call the device specific open if this
238 * is the first time the port is opened */
239 retval = serial->type->open(tty, port, filp);
241 goto bailout_interface_put;
244 mutex_unlock(&port->mutex);
247 bailout_interface_put:
248 usb_autopm_put_interface(serial->interface);
250 module_put(serial->type->driver.owner);
251 bailout_mutex_unlock:
252 port->port.count = 0;
253 tty->driver_data = NULL;
254 tty_port_tty_set(&port->port, NULL);
255 mutex_unlock(&port->mutex);
257 usb_serial_put(serial);
261 static void serial_close(struct tty_struct *tty, struct file *filp)
263 struct usb_serial_port *port = tty->driver_data;
268 dbg("%s - port %d", __func__, port->number);
270 mutex_lock(&port->mutex);
272 if (port->port.count == 0) {
273 mutex_unlock(&port->mutex);
277 if (port->port.count == 1)
278 /* only call the device specific close if this
279 * port is being closed by the last owner. Ensure we do
280 * this before we drop the port count. The call is protected
283 port->serial->type->close(tty, port, filp);
285 if (port->port.count == (port->console ? 2 : 1)) {
286 struct tty_struct *tty = tty_port_tty_get(&port->port);
288 /* We must do this before we drop the port count to
290 if (tty->driver_data)
291 tty->driver_data = NULL;
292 tty_port_tty_set(&port->port, NULL);
297 if (port->port.count == 1) {
298 mutex_lock(&port->serial->disc_mutex);
299 if (!port->serial->disconnected)
300 usb_autopm_put_interface(port->serial->interface);
301 mutex_unlock(&port->serial->disc_mutex);
302 module_put(port->serial->type->driver.owner);
306 mutex_unlock(&port->mutex);
307 usb_serial_put(port->serial);
310 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
313 struct usb_serial_port *port = tty->driver_data;
314 int retval = -ENODEV;
316 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
319 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
321 /* count is managed under the mutex lock for the tty so cannot
322 drop to zero until after the last close completes */
323 WARN_ON(!port->port.count);
325 /* pass on to the driver specific version of this function */
326 retval = port->serial->type->write(tty, port, buf, count);
332 static int serial_write_room(struct tty_struct *tty)
334 struct usb_serial_port *port = tty->driver_data;
335 dbg("%s - port %d", __func__, port->number);
336 WARN_ON(!port->port.count);
337 /* pass on to the driver specific version of this function */
338 return port->serial->type->write_room(tty);
341 static int serial_chars_in_buffer(struct tty_struct *tty)
343 struct usb_serial_port *port = tty->driver_data;
344 dbg("%s = port %d", __func__, port->number);
346 WARN_ON(!port->port.count);
347 /* if the device was unplugged then any remaining characters
348 fell out of the connector ;) */
349 if (port->serial->disconnected)
351 /* pass on to the driver specific version of this function */
352 return port->serial->type->chars_in_buffer(tty);
355 static void serial_throttle(struct tty_struct *tty)
357 struct usb_serial_port *port = tty->driver_data;
358 dbg("%s - port %d", __func__, port->number);
360 WARN_ON(!port->port.count);
361 /* pass on to the driver specific version of this function */
362 if (port->serial->type->throttle)
363 port->serial->type->throttle(tty);
366 static void serial_unthrottle(struct tty_struct *tty)
368 struct usb_serial_port *port = tty->driver_data;
369 dbg("%s - port %d", __func__, port->number);
371 WARN_ON(!port->port.count);
372 /* pass on to the driver specific version of this function */
373 if (port->serial->type->unthrottle)
374 port->serial->type->unthrottle(tty);
377 static int serial_ioctl(struct tty_struct *tty, struct file *file,
378 unsigned int cmd, unsigned long arg)
380 struct usb_serial_port *port = tty->driver_data;
381 int retval = -ENODEV;
383 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
385 WARN_ON(!port->port.count);
387 /* pass on to the driver specific version of this function
388 if it is available */
389 if (port->serial->type->ioctl) {
390 retval = port->serial->type->ioctl(tty, file, cmd, arg);
392 retval = -ENOIOCTLCMD;
396 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
398 struct usb_serial_port *port = tty->driver_data;
399 dbg("%s - port %d", __func__, port->number);
401 WARN_ON(!port->port.count);
402 /* pass on to the driver specific version of this function
403 if it is available */
404 if (port->serial->type->set_termios)
405 port->serial->type->set_termios(tty, port, old);
407 tty_termios_copy_hw(tty->termios, old);
410 static int serial_break(struct tty_struct *tty, int break_state)
412 struct usb_serial_port *port = tty->driver_data;
414 dbg("%s - port %d", __func__, port->number);
416 WARN_ON(!port->port.count);
417 /* pass on to the driver specific version of this function
418 if it is available */
419 if (port->serial->type->break_ctl)
420 port->serial->type->break_ctl(tty, break_state);
424 static int serial_read_proc(char *page, char **start, off_t off, int count,
425 int *eof, void *data)
427 struct usb_serial *serial;
434 length += sprintf(page, "usbserinfo:1.0 driver:2.0\n");
435 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
436 serial = usb_serial_get_by_index(i);
440 length += sprintf(page+length, "%d:", i);
441 if (serial->type->driver.owner)
442 length += sprintf(page+length, " module:%s",
443 module_name(serial->type->driver.owner));
444 length += sprintf(page+length, " name:\"%s\"",
445 serial->type->description);
446 length += sprintf(page+length, " vendor:%04x product:%04x",
447 le16_to_cpu(serial->dev->descriptor.idVendor),
448 le16_to_cpu(serial->dev->descriptor.idProduct));
449 length += sprintf(page+length, " num_ports:%d",
451 length += sprintf(page+length, " port:%d",
452 i - serial->minor + 1);
453 usb_make_path(serial->dev, tmp, sizeof(tmp));
454 length += sprintf(page+length, " path:%s", tmp);
456 length += sprintf(page+length, "\n");
457 if ((length + begin) > (off + count)) {
458 usb_serial_put(serial);
461 if ((length + begin) < off) {
465 usb_serial_put(serial);
469 if (off >= (length + begin))
471 *start = page + (off-begin);
472 return (count < begin+length-off) ? count : begin+length-off;
475 static int serial_tiocmget(struct tty_struct *tty, struct file *file)
477 struct usb_serial_port *port = tty->driver_data;
479 dbg("%s - port %d", __func__, port->number);
481 WARN_ON(!port->port.count);
482 if (port->serial->type->tiocmget)
483 return port->serial->type->tiocmget(tty, file);
487 static int serial_tiocmset(struct tty_struct *tty, struct file *file,
488 unsigned int set, unsigned int clear)
490 struct usb_serial_port *port = tty->driver_data;
492 dbg("%s - port %d", __func__, port->number);
494 WARN_ON(!port->port.count);
495 if (port->serial->type->tiocmset)
496 return port->serial->type->tiocmset(tty, file, set, clear);
501 * We would be calling tty_wakeup here, but unfortunately some line
502 * disciplines have an annoying habit of calling tty->write from
503 * the write wakeup callback (e.g. n_hdlc.c).
505 void usb_serial_port_softint(struct usb_serial_port *port)
507 schedule_work(&port->work);
509 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
511 static void usb_serial_port_work(struct work_struct *work)
513 struct usb_serial_port *port =
514 container_of(work, struct usb_serial_port, work);
515 struct tty_struct *tty;
517 dbg("%s - port %d", __func__, port->number);
519 tty = tty_port_tty_get(&port->port);
527 static void port_release(struct device *dev)
529 struct usb_serial_port *port = to_usb_serial_port(dev);
531 dbg ("%s - %s", __func__, dev_name(dev));
535 static void kill_traffic(struct usb_serial_port *port)
537 usb_kill_urb(port->read_urb);
538 usb_kill_urb(port->write_urb);
541 * Some drivers submit the read_urb in the
542 * handler for the write_urb or vice versa
543 * this order determines the order in which
544 * usb_kill_urb() must be used to reliably
545 * kill the URBs. As it is unknown here,
546 * both orders must be used in turn.
547 * The call below is not redundant.
549 usb_kill_urb(port->read_urb);
550 usb_kill_urb(port->interrupt_in_urb);
551 usb_kill_urb(port->interrupt_out_urb);
554 static void port_free(struct usb_serial_port *port)
557 usb_free_urb(port->read_urb);
558 usb_free_urb(port->write_urb);
559 usb_free_urb(port->interrupt_in_urb);
560 usb_free_urb(port->interrupt_out_urb);
561 kfree(port->bulk_in_buffer);
562 kfree(port->bulk_out_buffer);
563 kfree(port->interrupt_in_buffer);
564 kfree(port->interrupt_out_buffer);
565 flush_scheduled_work(); /* port->work */
569 static struct usb_serial *create_serial(struct usb_device *dev,
570 struct usb_interface *interface,
571 struct usb_serial_driver *driver)
573 struct usb_serial *serial;
575 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
577 dev_err(&dev->dev, "%s - out of memory\n", __func__);
580 serial->dev = usb_get_dev(dev);
581 serial->type = driver;
582 serial->interface = interface;
583 kref_init(&serial->kref);
584 mutex_init(&serial->disc_mutex);
585 serial->minor = SERIAL_TTY_NO_MINOR;
590 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
591 struct usb_serial_driver *drv)
593 struct usb_dynid *dynid;
595 spin_lock(&drv->dynids.lock);
596 list_for_each_entry(dynid, &drv->dynids.list, node) {
597 if (usb_match_one_id(intf, &dynid->id)) {
598 spin_unlock(&drv->dynids.lock);
602 spin_unlock(&drv->dynids.lock);
606 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
607 struct usb_interface *intf)
609 const struct usb_device_id *id;
611 id = usb_match_id(intf, drv->id_table);
613 dbg("static descriptor matches");
616 id = match_dynamic_id(intf, drv);
618 dbg("dynamic descriptor matches");
623 static struct usb_serial_driver *search_serial_device(
624 struct usb_interface *iface)
626 const struct usb_device_id *id;
627 struct usb_serial_driver *drv;
629 /* Check if the usb id matches a known device */
630 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
631 id = get_iface_id(drv, iface);
639 int usb_serial_probe(struct usb_interface *interface,
640 const struct usb_device_id *id)
642 struct usb_device *dev = interface_to_usbdev(interface);
643 struct usb_serial *serial = NULL;
644 struct usb_serial_port *port;
645 struct usb_host_interface *iface_desc;
646 struct usb_endpoint_descriptor *endpoint;
647 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
648 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
649 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
650 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
651 struct usb_serial_driver *type = NULL;
656 int num_interrupt_in = 0;
657 int num_interrupt_out = 0;
659 int num_bulk_out = 0;
663 lock_kernel(); /* guard against unloading a serial driver module */
664 type = search_serial_device(interface);
671 serial = create_serial(dev, interface, type);
674 dev_err(&interface->dev, "%s - out of memory\n", __func__);
678 /* if this device type has a probe function, call it */
680 const struct usb_device_id *id;
682 if (!try_module_get(type->driver.owner)) {
684 dev_err(&interface->dev,
685 "module get failed, exiting\n");
690 id = get_iface_id(type, interface);
691 retval = type->probe(serial, id);
692 module_put(type->driver.owner);
696 dbg("sub driver rejected device");
702 /* descriptor matches, let's find the endpoints needed */
703 /* check out the endpoints */
704 iface_desc = interface->cur_altsetting;
705 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
706 endpoint = &iface_desc->endpoint[i].desc;
708 if (usb_endpoint_is_bulk_in(endpoint)) {
709 /* we found a bulk in endpoint */
710 dbg("found bulk in on endpoint %d", i);
711 bulk_in_endpoint[num_bulk_in] = endpoint;
715 if (usb_endpoint_is_bulk_out(endpoint)) {
716 /* we found a bulk out endpoint */
717 dbg("found bulk out on endpoint %d", i);
718 bulk_out_endpoint[num_bulk_out] = endpoint;
722 if (usb_endpoint_is_int_in(endpoint)) {
723 /* we found a interrupt in endpoint */
724 dbg("found interrupt in on endpoint %d", i);
725 interrupt_in_endpoint[num_interrupt_in] = endpoint;
729 if (usb_endpoint_is_int_out(endpoint)) {
730 /* we found an interrupt out endpoint */
731 dbg("found interrupt out on endpoint %d", i);
732 interrupt_out_endpoint[num_interrupt_out] = endpoint;
737 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
738 /* BEGIN HORRIBLE HACK FOR PL2303 */
739 /* this is needed due to the looney way its endpoints are set up */
740 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
741 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
742 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
743 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
744 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
745 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
746 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
747 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
748 if (interface != dev->actconfig->interface[0]) {
749 /* check out the endpoints of the other interface*/
750 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
751 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
752 endpoint = &iface_desc->endpoint[i].desc;
753 if (usb_endpoint_is_int_in(endpoint)) {
754 /* we found a interrupt in endpoint */
755 dbg("found interrupt in for Prolific device on separate interface");
756 interrupt_in_endpoint[num_interrupt_in] = endpoint;
762 /* Now make sure the PL-2303 is configured correctly.
763 * If not, give up now and hope this hack will work
764 * properly during a later invocation of usb_serial_probe
766 if (num_bulk_in == 0 || num_bulk_out == 0) {
768 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
773 /* END HORRIBLE HACK FOR PL2303 */
776 #ifdef CONFIG_USB_SERIAL_GENERIC
777 if (type == &usb_serial_generic_device) {
778 num_ports = num_bulk_out;
779 if (num_ports == 0) {
781 dev_err(&interface->dev,
782 "Generic device with no bulk out, not allowed.\n");
789 /* if this device type has a calc_num_ports function, call it */
790 if (type->calc_num_ports) {
791 if (!try_module_get(type->driver.owner)) {
793 dev_err(&interface->dev,
794 "module get failed, exiting\n");
798 num_ports = type->calc_num_ports(serial);
799 module_put(type->driver.owner);
802 num_ports = type->num_ports;
805 serial->num_ports = num_ports;
806 serial->num_bulk_in = num_bulk_in;
807 serial->num_bulk_out = num_bulk_out;
808 serial->num_interrupt_in = num_interrupt_in;
809 serial->num_interrupt_out = num_interrupt_out;
811 /* found all that we need */
812 dev_info(&interface->dev, "%s converter detected\n",
815 /* create our ports, we need as many as the max endpoints */
816 /* we don't use num_ports here because some devices have more
817 endpoint pairs than ports */
818 max_endpoints = max(num_bulk_in, num_bulk_out);
819 max_endpoints = max(max_endpoints, num_interrupt_in);
820 max_endpoints = max(max_endpoints, num_interrupt_out);
821 max_endpoints = max(max_endpoints, (int)serial->num_ports);
822 serial->num_port_pointers = max_endpoints;
825 dbg("%s - setting up %d port structures for this device",
826 __func__, max_endpoints);
827 for (i = 0; i < max_endpoints; ++i) {
828 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
831 tty_port_init(&port->port);
832 port->serial = serial;
833 spin_lock_init(&port->lock);
834 mutex_init(&port->mutex);
835 INIT_WORK(&port->work, usb_serial_port_work);
836 serial->port[i] = port;
839 /* set up the endpoint information */
840 for (i = 0; i < num_bulk_in; ++i) {
841 endpoint = bulk_in_endpoint[i];
842 port = serial->port[i];
843 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
844 if (!port->read_urb) {
845 dev_err(&interface->dev, "No free urbs available\n");
848 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
849 port->bulk_in_size = buffer_size;
850 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
851 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
852 if (!port->bulk_in_buffer) {
853 dev_err(&interface->dev,
854 "Couldn't allocate bulk_in_buffer\n");
857 usb_fill_bulk_urb(port->read_urb, dev,
859 endpoint->bEndpointAddress),
860 port->bulk_in_buffer, buffer_size,
861 serial->type->read_bulk_callback, port);
864 for (i = 0; i < num_bulk_out; ++i) {
865 endpoint = bulk_out_endpoint[i];
866 port = serial->port[i];
867 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
868 if (!port->write_urb) {
869 dev_err(&interface->dev, "No free urbs available\n");
872 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
873 port->bulk_out_size = buffer_size;
874 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
875 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
876 if (!port->bulk_out_buffer) {
877 dev_err(&interface->dev,
878 "Couldn't allocate bulk_out_buffer\n");
881 usb_fill_bulk_urb(port->write_urb, dev,
883 endpoint->bEndpointAddress),
884 port->bulk_out_buffer, buffer_size,
885 serial->type->write_bulk_callback, port);
888 if (serial->type->read_int_callback) {
889 for (i = 0; i < num_interrupt_in; ++i) {
890 endpoint = interrupt_in_endpoint[i];
891 port = serial->port[i];
892 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
893 if (!port->interrupt_in_urb) {
894 dev_err(&interface->dev,
895 "No free urbs available\n");
898 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
899 port->interrupt_in_endpointAddress =
900 endpoint->bEndpointAddress;
901 port->interrupt_in_buffer = kmalloc(buffer_size,
903 if (!port->interrupt_in_buffer) {
904 dev_err(&interface->dev,
905 "Couldn't allocate interrupt_in_buffer\n");
908 usb_fill_int_urb(port->interrupt_in_urb, dev,
910 endpoint->bEndpointAddress),
911 port->interrupt_in_buffer, buffer_size,
912 serial->type->read_int_callback, port,
913 endpoint->bInterval);
915 } else if (num_interrupt_in) {
916 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
919 if (serial->type->write_int_callback) {
920 for (i = 0; i < num_interrupt_out; ++i) {
921 endpoint = interrupt_out_endpoint[i];
922 port = serial->port[i];
923 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
924 if (!port->interrupt_out_urb) {
925 dev_err(&interface->dev,
926 "No free urbs available\n");
929 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
930 port->interrupt_out_size = buffer_size;
931 port->interrupt_out_endpointAddress =
932 endpoint->bEndpointAddress;
933 port->interrupt_out_buffer = kmalloc(buffer_size,
935 if (!port->interrupt_out_buffer) {
936 dev_err(&interface->dev,
937 "Couldn't allocate interrupt_out_buffer\n");
940 usb_fill_int_urb(port->interrupt_out_urb, dev,
942 endpoint->bEndpointAddress),
943 port->interrupt_out_buffer, buffer_size,
944 serial->type->write_int_callback, port,
945 endpoint->bInterval);
947 } else if (num_interrupt_out) {
948 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
951 /* if this device type has an attach function, call it */
953 if (!try_module_get(type->driver.owner)) {
954 dev_err(&interface->dev,
955 "module get failed, exiting\n");
958 retval = type->attach(serial);
959 module_put(type->driver.owner);
963 /* quietly accept this device, but don't bind to a
964 serial port as it's about to disappear */
969 if (get_free_serial(serial, num_ports, &minor) == NULL) {
970 dev_err(&interface->dev, "No more free serial devices\n");
973 serial->minor = minor;
975 /* register all of the individual ports with the driver core */
976 for (i = 0; i < num_ports; ++i) {
977 port = serial->port[i];
978 port->dev.parent = &interface->dev;
979 port->dev.driver = NULL;
980 port->dev.bus = &usb_serial_bus_type;
981 port->dev.release = &port_release;
983 dev_set_name(&port->dev, "ttyUSB%d", port->number);
984 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
985 retval = device_register(&port->dev);
987 dev_err(&port->dev, "Error registering port device, "
991 usb_serial_console_init(debug, minor);
995 usb_set_intfdata(interface, serial);
999 for (i = 0; i < num_bulk_in; ++i) {
1000 port = serial->port[i];
1003 usb_free_urb(port->read_urb);
1004 kfree(port->bulk_in_buffer);
1006 for (i = 0; i < num_bulk_out; ++i) {
1007 port = serial->port[i];
1010 usb_free_urb(port->write_urb);
1011 kfree(port->bulk_out_buffer);
1013 for (i = 0; i < num_interrupt_in; ++i) {
1014 port = serial->port[i];
1017 usb_free_urb(port->interrupt_in_urb);
1018 kfree(port->interrupt_in_buffer);
1020 for (i = 0; i < num_interrupt_out; ++i) {
1021 port = serial->port[i];
1024 usb_free_urb(port->interrupt_out_urb);
1025 kfree(port->interrupt_out_buffer);
1028 /* free up any memory that we allocated */
1029 for (i = 0; i < serial->num_port_pointers; ++i)
1030 kfree(serial->port[i]);
1034 EXPORT_SYMBOL_GPL(usb_serial_probe);
1036 void usb_serial_disconnect(struct usb_interface *interface)
1039 struct usb_serial *serial = usb_get_intfdata(interface);
1040 struct device *dev = &interface->dev;
1041 struct usb_serial_port *port;
1043 usb_serial_console_disconnect(serial);
1044 dbg("%s", __func__);
1046 mutex_lock(&serial->disc_mutex);
1047 usb_set_intfdata(interface, NULL);
1048 /* must set a flag, to signal subdrivers */
1049 serial->disconnected = 1;
1050 for (i = 0; i < serial->num_ports; ++i) {
1051 port = serial->port[i];
1053 struct tty_struct *tty = tty_port_tty_get(&port->port);
1061 /* let the last holder of this object
1062 * cause it to be cleaned up */
1063 mutex_unlock(&serial->disc_mutex);
1064 usb_serial_put(serial);
1065 dev_info(dev, "device disconnected\n");
1067 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1069 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1071 struct usb_serial *serial = usb_get_intfdata(intf);
1072 struct usb_serial_port *port;
1075 serial->suspending = 1;
1077 for (i = 0; i < serial->num_ports; ++i) {
1078 port = serial->port[i];
1083 if (serial->type->suspend)
1084 r = serial->type->suspend(serial, message);
1088 EXPORT_SYMBOL(usb_serial_suspend);
1090 int usb_serial_resume(struct usb_interface *intf)
1092 struct usb_serial *serial = usb_get_intfdata(intf);
1095 serial->suspending = 0;
1096 if (serial->type->resume)
1097 rv = serial->type->resume(serial);
1099 rv = usb_serial_generic_resume(serial);
1103 EXPORT_SYMBOL(usb_serial_resume);
1105 static const struct tty_operations serial_ops = {
1106 .open = serial_open,
1107 .close = serial_close,
1108 .write = serial_write,
1109 .write_room = serial_write_room,
1110 .ioctl = serial_ioctl,
1111 .set_termios = serial_set_termios,
1112 .throttle = serial_throttle,
1113 .unthrottle = serial_unthrottle,
1114 .break_ctl = serial_break,
1115 .chars_in_buffer = serial_chars_in_buffer,
1116 .read_proc = serial_read_proc,
1117 .tiocmget = serial_tiocmget,
1118 .tiocmset = serial_tiocmset,
1121 struct tty_driver *usb_serial_tty_driver;
1123 static int __init usb_serial_init(void)
1128 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1129 if (!usb_serial_tty_driver)
1132 /* Initialize our global data */
1133 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1134 serial_table[i] = NULL;
1136 result = bus_register(&usb_serial_bus_type);
1138 printk(KERN_ERR "usb-serial: %s - registering bus driver "
1139 "failed\n", __func__);
1143 usb_serial_tty_driver->owner = THIS_MODULE;
1144 usb_serial_tty_driver->driver_name = "usbserial";
1145 usb_serial_tty_driver->name = "ttyUSB";
1146 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1147 usb_serial_tty_driver->minor_start = 0;
1148 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1149 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1150 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1151 TTY_DRIVER_DYNAMIC_DEV;
1152 usb_serial_tty_driver->init_termios = tty_std_termios;
1153 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1155 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1156 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1157 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1158 result = tty_register_driver(usb_serial_tty_driver);
1160 printk(KERN_ERR "usb-serial: %s - tty_register_driver failed\n",
1162 goto exit_reg_driver;
1165 /* register the USB driver */
1166 result = usb_register(&usb_serial_driver);
1168 printk(KERN_ERR "usb-serial: %s - usb_register failed\n",
1173 /* register the generic driver, if we should */
1174 result = usb_serial_generic_register(debug);
1176 printk(KERN_ERR "usb-serial: %s - registering generic "
1177 "driver failed\n", __func__);
1181 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1186 usb_deregister(&usb_serial_driver);
1189 tty_unregister_driver(usb_serial_tty_driver);
1192 bus_unregister(&usb_serial_bus_type);
1195 printk(KERN_ERR "usb-serial: %s - returning with error %d\n",
1197 put_tty_driver(usb_serial_tty_driver);
1202 static void __exit usb_serial_exit(void)
1204 usb_serial_console_exit();
1206 usb_serial_generic_deregister();
1208 usb_deregister(&usb_serial_driver);
1209 tty_unregister_driver(usb_serial_tty_driver);
1210 put_tty_driver(usb_serial_tty_driver);
1211 bus_unregister(&usb_serial_bus_type);
1215 module_init(usb_serial_init);
1216 module_exit(usb_serial_exit);
1218 #define set_to_generic_if_null(type, function) \
1220 if (!type->function) { \
1221 type->function = usb_serial_generic_##function; \
1222 dbg("Had to override the " #function \
1223 " usb serial operation with the generic one.");\
1227 static void fixup_generic(struct usb_serial_driver *device)
1229 set_to_generic_if_null(device, open);
1230 set_to_generic_if_null(device, write);
1231 set_to_generic_if_null(device, close);
1232 set_to_generic_if_null(device, write_room);
1233 set_to_generic_if_null(device, chars_in_buffer);
1234 set_to_generic_if_null(device, read_bulk_callback);
1235 set_to_generic_if_null(device, write_bulk_callback);
1236 set_to_generic_if_null(device, shutdown);
1239 int usb_serial_register(struct usb_serial_driver *driver)
1241 /* must be called with BKL held */
1247 fixup_generic(driver);
1249 if (!driver->description)
1250 driver->description = driver->driver.name;
1252 /* Add this device to our list of devices */
1253 list_add(&driver->driver_list, &usb_serial_driver_list);
1255 retval = usb_serial_bus_register(driver);
1257 printk(KERN_ERR "usb-serial: problem %d when registering "
1258 "driver %s\n", retval, driver->description);
1259 list_del(&driver->driver_list);
1261 printk(KERN_INFO "USB Serial support registered for %s\n",
1262 driver->description);
1266 EXPORT_SYMBOL_GPL(usb_serial_register);
1269 void usb_serial_deregister(struct usb_serial_driver *device)
1271 /* must be called with BKL held */
1272 printk(KERN_INFO "USB Serial deregistering driver %s\n",
1273 device->description);
1274 list_del(&device->driver_list);
1275 usb_serial_bus_deregister(device);
1277 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1279 /* Module information */
1280 MODULE_AUTHOR(DRIVER_AUTHOR);
1281 MODULE_DESCRIPTION(DRIVER_DESC);
1282 MODULE_LICENSE("GPL");
1284 module_param(debug, bool, S_IRUGO | S_IWUSR);
1285 MODULE_PARM_DESC(debug, "Debug enabled or not");