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/seq_file.h>
30 #include <linux/spinlock.h>
31 #include <linux/mutex.h>
32 #include <linux/list.h>
33 #include <linux/uaccess.h>
34 #include <linux/serial.h>
35 #include <linux/usb.h>
36 #include <linux/usb/serial.h>
37 #include <linux/kfifo.h>
43 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
44 #define DRIVER_DESC "USB Serial Driver core"
46 extern int get_current_bp_id();
48 /* Driver structure we register with the USB core */
49 static struct usb_driver usb_serial_driver = {
51 .probe = usb_serial_probe,
52 .disconnect = usb_serial_disconnect,
53 .suspend = usb_serial_suspend,
54 .resume = usb_serial_resume,
56 .supports_autosuspend = 1,
59 static int BP_USB = 0;
60 #define BP_USB_PORT (SERIAL_TTY_MINORS - 10)
64 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
65 the MODULE_DEVICE_TABLE declarations in each serial driver
66 cause the "hotplug" program to pull in whatever module is necessary
67 via modprobe, and modprobe will load usbserial because the serial
72 /* initially all NULL */
73 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
74 static DEFINE_MUTEX(table_lock);
75 static LIST_HEAD(usb_serial_driver_list);
78 * Look up the serial structure. If it is found and it hasn't been
79 * disconnected, return with its disc_mutex held and its refcount
80 * incremented. Otherwise return NULL.
82 struct usb_serial *usb_serial_get_by_index(unsigned index)
84 struct usb_serial *serial;
86 mutex_lock(&table_lock);
87 serial = serial_table[index];
90 mutex_lock(&serial->disc_mutex);
91 if (serial->disconnected) {
92 mutex_unlock(&serial->disc_mutex);
95 kref_get(&serial->kref);
98 mutex_unlock(&table_lock);
102 static struct usb_serial *get_free_serial(struct usb_serial *serial,
103 int num_ports, unsigned int *minor)
109 dbg("%s %d", __func__, num_ports);
112 mutex_lock(&table_lock);
113 #ifdef CONFIG_BP_AUTO
118 for (i = a; i < SERIAL_TTY_MINORS; ++i) {
123 for (j = 1; j <= num_ports-1; ++j)
124 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
134 dbg("%s - minor base = %d", __func__, *minor);
135 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
136 serial_table[i] = serial;
137 serial->port[j++]->number = i;
139 mutex_unlock(&table_lock);
142 mutex_unlock(&table_lock);
146 static void return_serial(struct usb_serial *serial)
152 mutex_lock(&table_lock);
153 for (i = 0; i < serial->num_ports; ++i)
154 serial_table[serial->minor + i] = NULL;
155 mutex_unlock(&table_lock);
158 static void destroy_serial(struct kref *kref)
160 struct usb_serial *serial;
161 struct usb_serial_port *port;
164 serial = to_usb_serial(kref);
166 dbg("%s - %s", __func__, serial->type->description);
168 /* return the minor range that this device had */
169 if (serial->minor != SERIAL_TTY_NO_MINOR)
170 return_serial(serial);
172 if (serial->attached)
173 serial->type->release(serial);
175 /* Now that nothing is using the ports, they can be freed */
176 for (i = 0; i < serial->num_port_pointers; ++i) {
177 port = serial->port[i];
180 put_device(&port->dev);
184 usb_put_dev(serial->dev);
188 void usb_serial_put(struct usb_serial *serial)
190 kref_put(&serial->kref, destroy_serial);
193 /*****************************************************************************
194 * Driver tty interface functions
195 *****************************************************************************/
198 * serial_install - install tty
199 * @driver: the driver (USB in our case)
200 * @tty: the tty being created
202 * Create the termios objects for this tty. We use the default
203 * USB serial settings but permit them to be overridden by
204 * serial->type->init_termios.
206 * This is the first place a new tty gets used. Hence this is where we
207 * acquire references to the usb_serial structure and the driver module,
208 * where we store a pointer to the port, and where we do an autoresume.
209 * All these actions are reversed in serial_cleanup().
211 static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
213 int idx = tty->index;
214 struct usb_serial *serial;
215 struct usb_serial_port *port;
216 int retval = -ENODEV;
220 serial = usb_serial_get_by_index(idx);
224 port = serial->port[idx - serial->minor];
227 if (!try_module_get(serial->type->driver.owner))
228 goto error_module_get;
230 /* perform the standard setup */
231 retval = tty_init_termios(tty);
233 goto error_init_termios;
235 retval = usb_autopm_get_interface(serial->interface);
237 goto error_get_interface;
239 mutex_unlock(&serial->disc_mutex);
241 /* allow the driver to update the settings */
242 if (serial->type->init_termios)
243 serial->type->init_termios(tty);
245 tty->driver_data = port;
247 /* Final install (we use the default method) */
248 tty_driver_kref_get(driver);
250 driver->ttys[idx] = tty;
255 module_put(serial->type->driver.owner);
258 usb_serial_put(serial);
259 mutex_unlock(&serial->disc_mutex);
263 static int serial_activate(struct tty_port *tport, struct tty_struct *tty)
265 struct usb_serial_port *port =
266 container_of(tport, struct usb_serial_port, port);
267 struct usb_serial *serial = port->serial;
270 mutex_lock(&serial->disc_mutex);
271 if (serial->disconnected)
274 retval = port->serial->type->open(tty, port);
275 mutex_unlock(&serial->disc_mutex);
279 static int serial_open(struct tty_struct *tty, struct file *filp)
281 struct usb_serial_port *port = tty->driver_data;
283 dbg("%s - port %d", __func__, port->number);
284 return tty_port_open(&port->port, tty, filp);
288 * serial_down - shut down hardware
289 * @tport: tty port to shut down
291 * Shut down a USB serial port unless it is the console. We never
292 * shut down the console hardware as it will always be in use. Serialized
293 * against activate by the tport mutex and kept to matching open/close pairs
294 * of calls by the ASYNCB_INITIALIZED flag.
296 static void serial_down(struct tty_port *tport)
298 struct usb_serial_port *port =
299 container_of(tport, struct usb_serial_port, port);
300 struct usb_serial_driver *drv = port->serial->type;
302 * The console is magical. Do not hang up the console hardware
303 * or there will be tears.
305 if (port->port.console)
311 static void serial_hangup(struct tty_struct *tty)
313 struct usb_serial_port *port = tty->driver_data;
314 dbg("%s - port %d", __func__, port->number);
315 tty_port_hangup(&port->port);
318 static void serial_close(struct tty_struct *tty, struct file *filp)
320 struct usb_serial_port *port = tty->driver_data;
321 dbg("%s - port %d", __func__, port->number);
322 tty_port_close(&port->port, tty, filp);
326 * serial_cleanup - free resources post close/hangup
327 * @port: port to free up
329 * Do the resource freeing and refcount dropping for the port.
330 * Avoid freeing the console.
332 * Called asynchronously after the last tty kref is dropped,
333 * and the tty layer has already done the tty_shutdown(tty);
335 static void serial_cleanup(struct tty_struct *tty)
337 struct usb_serial_port *port = tty->driver_data;
338 struct usb_serial *serial;
339 struct module *owner;
341 /* The console is magical. Do not hang up the console hardware
342 * or there will be tears.
344 if (port->port.console)
347 dbg("%s - port %d", __func__, port->number);
349 tty->driver_data = NULL;
351 serial = port->serial;
352 owner = serial->type->driver.owner;
354 mutex_lock(&serial->disc_mutex);
355 if (!serial->disconnected)
356 usb_autopm_put_interface(serial->interface);
357 mutex_unlock(&serial->disc_mutex);
359 usb_serial_put(serial);
363 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
366 struct usb_serial_port *port = tty->driver_data;
367 int retval = -ENODEV;
369 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
372 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
374 /* pass on to the driver specific version of this function */
375 retval = port->serial->type->write(tty, port, buf, count);
381 static int serial_write_room(struct tty_struct *tty)
383 struct usb_serial_port *port = tty->driver_data;
384 dbg("%s - port %d", __func__, port->number);
385 /* pass on to the driver specific version of this function */
386 return port->serial->type->write_room(tty);
389 static int serial_chars_in_buffer(struct tty_struct *tty)
391 struct usb_serial_port *port = tty->driver_data;
392 dbg("%s - port %d", __func__, port->number);
394 /* if the device was unplugged then any remaining characters
395 fell out of the connector ;) */
396 if (port->serial->disconnected)
398 /* pass on to the driver specific version of this function */
399 return port->serial->type->chars_in_buffer(tty);
402 static void serial_throttle(struct tty_struct *tty)
404 struct usb_serial_port *port = tty->driver_data;
405 dbg("%s - port %d", __func__, port->number);
407 /* pass on to the driver specific version of this function */
408 if (port->serial->type->throttle)
409 port->serial->type->throttle(tty);
412 static void serial_unthrottle(struct tty_struct *tty)
414 struct usb_serial_port *port = tty->driver_data;
415 dbg("%s - port %d", __func__, port->number);
417 /* pass on to the driver specific version of this function */
418 if (port->serial->type->unthrottle)
419 port->serial->type->unthrottle(tty);
422 static int serial_ioctl(struct tty_struct *tty,
423 unsigned int cmd, unsigned long arg)
425 struct usb_serial_port *port = tty->driver_data;
426 int retval = -ENODEV;
428 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
430 /* pass on to the driver specific version of this function
431 if it is available */
432 if (port->serial->type->ioctl) {
433 retval = port->serial->type->ioctl(tty, cmd, arg);
435 retval = -ENOIOCTLCMD;
439 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
441 struct usb_serial_port *port = tty->driver_data;
442 dbg("%s - port %d", __func__, port->number);
444 /* pass on to the driver specific version of this function
445 if it is available */
446 if (port->serial->type->set_termios)
447 port->serial->type->set_termios(tty, port, old);
449 tty_termios_copy_hw(tty->termios, old);
452 static int serial_break(struct tty_struct *tty, int break_state)
454 struct usb_serial_port *port = tty->driver_data;
456 dbg("%s - port %d", __func__, port->number);
458 /* pass on to the driver specific version of this function
459 if it is available */
460 if (port->serial->type->break_ctl)
461 port->serial->type->break_ctl(tty, break_state);
465 static int serial_proc_show(struct seq_file *m, void *v)
467 struct usb_serial *serial;
472 seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
473 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
474 serial = usb_serial_get_by_index(i);
478 seq_printf(m, "%d:", i);
479 if (serial->type->driver.owner)
480 seq_printf(m, " module:%s",
481 module_name(serial->type->driver.owner));
482 seq_printf(m, " name:\"%s\"",
483 serial->type->description);
484 seq_printf(m, " vendor:%04x product:%04x",
485 le16_to_cpu(serial->dev->descriptor.idVendor),
486 le16_to_cpu(serial->dev->descriptor.idProduct));
487 seq_printf(m, " num_ports:%d", serial->num_ports);
488 seq_printf(m, " port:%d", i - serial->minor + 1);
489 usb_make_path(serial->dev, tmp, sizeof(tmp));
490 seq_printf(m, " path:%s", tmp);
493 usb_serial_put(serial);
494 mutex_unlock(&serial->disc_mutex);
499 static int serial_proc_open(struct inode *inode, struct file *file)
501 return single_open(file, serial_proc_show, NULL);
504 static const struct file_operations serial_proc_fops = {
505 .owner = THIS_MODULE,
506 .open = serial_proc_open,
509 .release = single_release,
512 static int serial_tiocmget(struct tty_struct *tty)
514 struct usb_serial_port *port = tty->driver_data;
516 dbg("%s - port %d", __func__, port->number);
518 if (port->serial->type->tiocmget)
519 return port->serial->type->tiocmget(tty);
523 static int serial_tiocmset(struct tty_struct *tty,
524 unsigned int set, unsigned int clear)
526 struct usb_serial_port *port = tty->driver_data;
528 dbg("%s - port %d", __func__, port->number);
530 if (port->serial->type->tiocmset)
531 return port->serial->type->tiocmset(tty, set, clear);
535 static int serial_get_icount(struct tty_struct *tty,
536 struct serial_icounter_struct *icount)
538 struct usb_serial_port *port = tty->driver_data;
540 dbg("%s - port %d", __func__, port->number);
542 if (port->serial->type->get_icount)
543 return port->serial->type->get_icount(tty, icount);
548 * We would be calling tty_wakeup here, but unfortunately some line
549 * disciplines have an annoying habit of calling tty->write from
550 * the write wakeup callback (e.g. n_hdlc.c).
552 void usb_serial_port_softint(struct usb_serial_port *port)
554 schedule_work(&port->work);
556 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
558 static void usb_serial_port_work(struct work_struct *work)
560 struct usb_serial_port *port =
561 container_of(work, struct usb_serial_port, work);
562 struct tty_struct *tty;
564 dbg("%s - port %d", __func__, port->number);
566 tty = tty_port_tty_get(&port->port);
574 static void kill_traffic(struct usb_serial_port *port)
578 usb_kill_urb(port->read_urb);
579 usb_kill_urb(port->write_urb);
580 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
581 usb_kill_urb(port->write_urbs[i]);
584 * Some drivers submit the read_urb in the
585 * handler for the write_urb or vice versa
586 * this order determines the order in which
587 * usb_kill_urb() must be used to reliably
588 * kill the URBs. As it is unknown here,
589 * both orders must be used in turn.
590 * The call below is not redundant.
592 usb_kill_urb(port->read_urb);
593 usb_kill_urb(port->interrupt_in_urb);
594 usb_kill_urb(port->interrupt_out_urb);
597 static void port_release(struct device *dev)
599 struct usb_serial_port *port = to_usb_serial_port(dev);
602 dbg ("%s - %s", __func__, dev_name(dev));
605 * Stop all the traffic before cancelling the work, so that
606 * nobody will restart it by calling usb_serial_port_softint.
609 cancel_work_sync(&port->work);
611 usb_free_urb(port->read_urb);
612 usb_free_urb(port->write_urb);
613 usb_free_urb(port->interrupt_in_urb);
614 usb_free_urb(port->interrupt_out_urb);
615 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
616 usb_free_urb(port->write_urbs[i]);
617 kfree(port->bulk_out_buffers[i]);
619 kfifo_free(&port->write_fifo);
620 kfree(port->bulk_in_buffer);
621 kfree(port->bulk_out_buffer);
622 kfree(port->interrupt_in_buffer);
623 kfree(port->interrupt_out_buffer);
627 static struct usb_serial *create_serial(struct usb_device *dev,
628 struct usb_interface *interface,
629 struct usb_serial_driver *driver)
631 struct usb_serial *serial;
633 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
635 dev_err(&dev->dev, "%s - out of memory\n", __func__);
638 serial->dev = usb_get_dev(dev);
639 serial->type = driver;
640 serial->interface = interface;
641 kref_init(&serial->kref);
642 mutex_init(&serial->disc_mutex);
643 serial->minor = SERIAL_TTY_NO_MINOR;
648 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
649 struct usb_serial_driver *drv)
651 struct usb_dynid *dynid;
653 spin_lock(&drv->dynids.lock);
654 list_for_each_entry(dynid, &drv->dynids.list, node) {
655 if (usb_match_one_id(intf, &dynid->id)) {
656 spin_unlock(&drv->dynids.lock);
660 spin_unlock(&drv->dynids.lock);
664 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
665 struct usb_interface *intf)
667 const struct usb_device_id *id;
669 id = usb_match_id(intf, drv->id_table);
671 dbg("static descriptor matches");
674 id = match_dynamic_id(intf, drv);
676 dbg("dynamic descriptor matches");
681 /* Caller must hold table_lock */
682 static struct usb_serial_driver *search_serial_device(
683 struct usb_interface *iface)
685 const struct usb_device_id *id = NULL;
686 struct usb_serial_driver *drv;
687 struct usb_driver *driver = to_usb_driver(iface->dev.driver);
689 /* Check if the usb id matches a known device */
690 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
691 if (drv->usb_driver == driver)
692 id = get_iface_id(drv, iface);
700 static int serial_carrier_raised(struct tty_port *port)
702 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
703 struct usb_serial_driver *drv = p->serial->type;
704 if (drv->carrier_raised)
705 return drv->carrier_raised(p);
706 /* No carrier control - don't block */
710 static void serial_dtr_rts(struct tty_port *port, int on)
712 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
713 struct usb_serial_driver *drv = p->serial->type;
718 static const struct tty_port_operations serial_port_ops = {
719 .carrier_raised = serial_carrier_raised,
720 .dtr_rts = serial_dtr_rts,
721 .activate = serial_activate,
722 .shutdown = serial_down,
725 int usb_serial_probe(struct usb_interface *interface,
726 const struct usb_device_id *id)
728 struct usb_device *dev = interface_to_usbdev(interface);
729 struct usb_serial *serial = NULL;
730 struct usb_serial_port *port;
731 struct usb_host_interface *iface_desc;
732 struct usb_endpoint_descriptor *endpoint;
733 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
734 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
735 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
736 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
737 struct usb_serial_driver *type = NULL;
742 int num_interrupt_in = 0;
743 int num_interrupt_out = 0;
745 int num_bulk_out = 0;
749 mutex_lock(&table_lock);
750 type = search_serial_device(interface);
752 mutex_unlock(&table_lock);
757 if (!try_module_get(type->driver.owner)) {
758 mutex_unlock(&table_lock);
759 dev_err(&interface->dev, "module get failed, exiting\n");
762 mutex_unlock(&table_lock);
764 serial = create_serial(dev, interface, type);
766 module_put(type->driver.owner);
767 dev_err(&interface->dev, "%s - out of memory\n", __func__);
771 /* if this device type has a probe function, call it */
773 const struct usb_device_id *id;
775 id = get_iface_id(type, interface);
776 retval = type->probe(serial, id);
779 dbg("sub driver rejected device");
780 usb_serial_put(serial);
781 module_put(type->driver.owner);
786 /* descriptor matches, let's find the endpoints needed */
787 /* check out the endpoints */
788 iface_desc = interface->cur_altsetting;
789 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
790 endpoint = &iface_desc->endpoint[i].desc;
792 if (usb_endpoint_is_bulk_in(endpoint)) {
793 /* we found a bulk in endpoint */
794 dbg("found bulk in on endpoint %d", i);
795 bulk_in_endpoint[num_bulk_in] = endpoint;
799 if (usb_endpoint_is_bulk_out(endpoint)) {
800 /* we found a bulk out endpoint */
801 dbg("found bulk out on endpoint %d", i);
802 bulk_out_endpoint[num_bulk_out] = endpoint;
806 if (usb_endpoint_is_int_in(endpoint)) {
807 /* we found a interrupt in endpoint */
808 dbg("found interrupt in on endpoint %d", i);
809 interrupt_in_endpoint[num_interrupt_in] = endpoint;
813 if (usb_endpoint_is_int_out(endpoint)) {
814 /* we found an interrupt out endpoint */
815 dbg("found interrupt out on endpoint %d", i);
816 interrupt_out_endpoint[num_interrupt_out] = endpoint;
821 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
822 /* BEGIN HORRIBLE HACK FOR PL2303 */
823 /* this is needed due to the looney way its endpoints are set up */
824 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
825 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
826 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
827 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
828 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
829 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
830 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
831 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
832 if (interface != dev->actconfig->interface[0]) {
833 /* check out the endpoints of the other interface*/
834 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
835 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
836 endpoint = &iface_desc->endpoint[i].desc;
837 if (usb_endpoint_is_int_in(endpoint)) {
838 /* we found a interrupt in endpoint */
839 dbg("found interrupt in for Prolific device on separate interface");
840 interrupt_in_endpoint[num_interrupt_in] = endpoint;
846 /* Now make sure the PL-2303 is configured correctly.
847 * If not, give up now and hope this hack will work
848 * properly during a later invocation of usb_serial_probe
850 if (num_bulk_in == 0 || num_bulk_out == 0) {
851 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
852 usb_serial_put(serial);
853 module_put(type->driver.owner);
857 /* END HORRIBLE HACK FOR PL2303 */
860 #ifdef CONFIG_USB_SERIAL_GENERIC
861 if (type == &usb_serial_generic_device) {
862 num_ports = num_bulk_out;
863 if (num_ports == 0) {
864 dev_err(&interface->dev,
865 "Generic device with no bulk out, not allowed.\n");
866 usb_serial_put(serial);
867 module_put(type->driver.owner);
873 /* if this device type has a calc_num_ports function, call it */
874 if (type->calc_num_ports)
875 num_ports = type->calc_num_ports(serial);
877 num_ports = type->num_ports;
880 serial->num_ports = num_ports;
881 serial->num_bulk_in = num_bulk_in;
882 serial->num_bulk_out = num_bulk_out;
883 serial->num_interrupt_in = num_interrupt_in;
884 serial->num_interrupt_out = num_interrupt_out;
886 /* found all that we need */
887 dev_info(&interface->dev, "%s converter detected\n",
890 /* create our ports, we need as many as the max endpoints */
891 /* we don't use num_ports here because some devices have more
892 endpoint pairs than ports */
893 max_endpoints = max(num_bulk_in, num_bulk_out);
894 max_endpoints = max(max_endpoints, num_interrupt_in);
895 max_endpoints = max(max_endpoints, num_interrupt_out);
896 max_endpoints = max(max_endpoints, (int)serial->num_ports);
897 serial->num_port_pointers = max_endpoints;
899 dbg("%s - setting up %d port structures for this device",
900 __func__, max_endpoints);
901 for (i = 0; i < max_endpoints; ++i) {
902 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
905 tty_port_init(&port->port);
906 port->port.ops = &serial_port_ops;
907 port->serial = serial;
908 spin_lock_init(&port->lock);
909 /* Keep this for private driver use for the moment but
910 should probably go away */
911 INIT_WORK(&port->work, usb_serial_port_work);
912 serial->port[i] = port;
913 port->dev.parent = &interface->dev;
914 port->dev.driver = NULL;
915 port->dev.bus = &usb_serial_bus_type;
916 port->dev.release = &port_release;
917 device_initialize(&port->dev);
920 /* set up the endpoint information */
921 for (i = 0; i < num_bulk_in; ++i) {
922 endpoint = bulk_in_endpoint[i];
923 port = serial->port[i];
924 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
925 if (!port->read_urb) {
926 dev_err(&interface->dev, "No free urbs available\n");
929 buffer_size = max_t(int, serial->type->bulk_in_size,
930 le16_to_cpu(endpoint->wMaxPacketSize));
931 port->bulk_in_size = buffer_size;
932 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
933 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
934 if (!port->bulk_in_buffer) {
935 dev_err(&interface->dev,
936 "Couldn't allocate bulk_in_buffer\n");
939 usb_fill_bulk_urb(port->read_urb, dev,
941 endpoint->bEndpointAddress),
942 port->bulk_in_buffer, buffer_size,
943 serial->type->read_bulk_callback, port);
946 for (i = 0; i < num_bulk_out; ++i) {
949 endpoint = bulk_out_endpoint[i];
950 port = serial->port[i];
951 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
952 if (!port->write_urb) {
953 dev_err(&interface->dev, "No free urbs available\n");
956 if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
958 buffer_size = serial->type->bulk_out_size;
960 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
961 port->bulk_out_size = buffer_size;
962 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
963 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
964 if (!port->bulk_out_buffer) {
965 dev_err(&interface->dev,
966 "Couldn't allocate bulk_out_buffer\n");
969 usb_fill_bulk_urb(port->write_urb, dev,
971 endpoint->bEndpointAddress),
972 port->bulk_out_buffer, buffer_size,
973 serial->type->write_bulk_callback, port);
974 for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
975 set_bit(j, &port->write_urbs_free);
976 port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
977 if (!port->write_urbs[j]) {
978 dev_err(&interface->dev,
979 "No free urbs available\n");
982 port->bulk_out_buffers[j] = kmalloc(buffer_size,
984 if (!port->bulk_out_buffers[j]) {
985 dev_err(&interface->dev,
986 "Couldn't allocate bulk_out_buffer\n");
989 usb_fill_bulk_urb(port->write_urbs[j], dev,
991 endpoint->bEndpointAddress),
992 port->bulk_out_buffers[j], buffer_size,
993 serial->type->write_bulk_callback,
998 if (serial->type->read_int_callback) {
999 for (i = 0; i < num_interrupt_in; ++i) {
1000 endpoint = interrupt_in_endpoint[i];
1001 port = serial->port[i];
1002 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1003 if (!port->interrupt_in_urb) {
1004 dev_err(&interface->dev,
1005 "No free urbs available\n");
1008 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1009 port->interrupt_in_endpointAddress =
1010 endpoint->bEndpointAddress;
1011 port->interrupt_in_buffer = kmalloc(buffer_size,
1013 if (!port->interrupt_in_buffer) {
1014 dev_err(&interface->dev,
1015 "Couldn't allocate interrupt_in_buffer\n");
1018 usb_fill_int_urb(port->interrupt_in_urb, dev,
1020 endpoint->bEndpointAddress),
1021 port->interrupt_in_buffer, buffer_size,
1022 serial->type->read_int_callback, port,
1023 endpoint->bInterval);
1025 } else if (num_interrupt_in) {
1026 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
1029 if (serial->type->write_int_callback) {
1030 for (i = 0; i < num_interrupt_out; ++i) {
1031 endpoint = interrupt_out_endpoint[i];
1032 port = serial->port[i];
1033 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
1034 if (!port->interrupt_out_urb) {
1035 dev_err(&interface->dev,
1036 "No free urbs available\n");
1039 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1040 port->interrupt_out_size = buffer_size;
1041 port->interrupt_out_endpointAddress =
1042 endpoint->bEndpointAddress;
1043 port->interrupt_out_buffer = kmalloc(buffer_size,
1045 if (!port->interrupt_out_buffer) {
1046 dev_err(&interface->dev,
1047 "Couldn't allocate interrupt_out_buffer\n");
1050 usb_fill_int_urb(port->interrupt_out_urb, dev,
1052 endpoint->bEndpointAddress),
1053 port->interrupt_out_buffer, buffer_size,
1054 serial->type->write_int_callback, port,
1055 endpoint->bInterval);
1057 } else if (num_interrupt_out) {
1058 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
1061 /* if this device type has an attach function, call it */
1063 retval = type->attach(serial);
1066 serial->attached = 1;
1068 /* quietly accept this device, but don't bind to a
1069 serial port as it's about to disappear */
1070 serial->num_ports = 0;
1074 serial->attached = 1;
1076 #ifdef CONFIG_BP_AUTO
1077 int bp_id = get_current_bp_id();
1078 if (((le16_to_cpu(dev->descriptor.idVendor) == 0x1782 ) && (le16_to_cpu(dev->descriptor.idProduct) == 0x0002) && (bp_id == 12))
1079 || ((le16_to_cpu(dev->descriptor.idVendor) == 0x19f5) && (le16_to_cpu(dev->descriptor.idProduct) == 0x9013) && (bp_id == 4))
1080 || ((le16_to_cpu(dev->descriptor.idVendor) == 0x0E8D) && (le16_to_cpu(dev->descriptor.idProduct) == 0x00A2) && (bp_id == 1))
1081 || ((le16_to_cpu(dev->descriptor.idVendor) == 0x21f5) && (le16_to_cpu(dev->descriptor.idProduct) == 0x2012) && (bp_id == 10))
1092 /* Avoid race with tty_open and serial_install by setting the
1093 * disconnected flag and not clearing it until all ports have been
1096 serial->disconnected = 1;
1098 if (get_free_serial(serial, num_ports, &minor) == NULL) {
1099 dev_err(&interface->dev, "No more free serial devices\n");
1102 serial->minor = minor;
1104 /* register all of the individual ports with the driver core */
1105 for (i = 0; i < num_ports; ++i) {
1106 port = serial->port[i];
1107 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1108 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1109 port->dev_state = PORT_REGISTERING;
1110 device_enable_async_suspend(&port->dev);
1112 retval = device_add(&port->dev);
1114 dev_err(&port->dev, "Error registering port device, "
1116 port->dev_state = PORT_UNREGISTERED;
1118 port->dev_state = PORT_REGISTERED;
1122 serial->disconnected = 0;
1124 usb_serial_console_init(debug, minor);
1128 usb_set_intfdata(interface, serial);
1129 module_put(type->driver.owner);
1133 usb_serial_put(serial);
1134 module_put(type->driver.owner);
1137 EXPORT_SYMBOL_GPL(usb_serial_probe);
1139 void usb_serial_disconnect(struct usb_interface *interface)
1142 struct usb_serial *serial = usb_get_intfdata(interface);
1143 struct device *dev = &interface->dev;
1144 struct usb_serial_port *port;
1146 usb_serial_console_disconnect(serial);
1147 dbg("%s", __func__);
1149 mutex_lock(&serial->disc_mutex);
1150 usb_set_intfdata(interface, NULL);
1151 /* must set a flag, to signal subdrivers */
1152 serial->disconnected = 1;
1153 mutex_unlock(&serial->disc_mutex);
1155 for (i = 0; i < serial->num_ports; ++i) {
1156 port = serial->port[i];
1158 struct tty_struct *tty = tty_port_tty_get(&port->port);
1164 cancel_work_sync(&port->work);
1165 if (port->dev_state == PORT_REGISTERED) {
1167 /* Make sure the port is bound so that the
1168 * driver's port_remove method is called.
1170 if (!port->dev.driver) {
1174 &serial->type->driver;
1175 rc = device_bind_driver(&port->dev);
1177 port->dev_state = PORT_UNREGISTERING;
1178 device_del(&port->dev);
1179 port->dev_state = PORT_UNREGISTERED;
1183 serial->type->disconnect(serial);
1185 /* let the last holder of this object cause it to be cleaned up */
1186 usb_serial_put(serial);
1187 dev_info(dev, "device disconnected\n");
1189 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1191 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1193 struct usb_serial *serial = usb_get_intfdata(intf);
1194 struct usb_serial_port *port;
1197 serial->suspending = 1;
1199 if (serial->type->suspend) {
1200 r = serial->type->suspend(serial, message);
1202 serial->suspending = 0;
1207 for (i = 0; i < serial->num_ports; ++i) {
1208 port = serial->port[i];
1216 EXPORT_SYMBOL(usb_serial_suspend);
1218 int usb_serial_resume(struct usb_interface *intf)
1220 struct usb_serial *serial = usb_get_intfdata(intf);
1223 serial->suspending = 0;
1224 if (serial->type->resume)
1225 rv = serial->type->resume(serial);
1227 rv = usb_serial_generic_resume(serial);
1231 EXPORT_SYMBOL(usb_serial_resume);
1233 static const struct tty_operations serial_ops = {
1234 .open = serial_open,
1235 .close = serial_close,
1236 .write = serial_write,
1237 .hangup = serial_hangup,
1238 .write_room = serial_write_room,
1239 .ioctl = serial_ioctl,
1240 .set_termios = serial_set_termios,
1241 .throttle = serial_throttle,
1242 .unthrottle = serial_unthrottle,
1243 .break_ctl = serial_break,
1244 .chars_in_buffer = serial_chars_in_buffer,
1245 .tiocmget = serial_tiocmget,
1246 .tiocmset = serial_tiocmset,
1247 .get_icount = serial_get_icount,
1248 .cleanup = serial_cleanup,
1249 .install = serial_install,
1250 .proc_fops = &serial_proc_fops,
1254 struct tty_driver *usb_serial_tty_driver;
1256 static int __init usb_serial_init(void)
1261 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1262 if (!usb_serial_tty_driver)
1265 /* Initialize our global data */
1266 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1267 serial_table[i] = NULL;
1269 result = bus_register(&usb_serial_bus_type);
1271 printk(KERN_ERR "usb-serial: %s - registering bus driver "
1272 "failed\n", __func__);
1276 usb_serial_tty_driver->owner = THIS_MODULE;
1277 usb_serial_tty_driver->driver_name = "usbserial";
1278 usb_serial_tty_driver->name = "ttyUSB";
1279 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1280 usb_serial_tty_driver->minor_start = 0;
1281 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1282 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1283 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1284 TTY_DRIVER_DYNAMIC_DEV;
1285 usb_serial_tty_driver->init_termios = tty_std_termios;
1286 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1288 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1289 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1290 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1291 result = tty_register_driver(usb_serial_tty_driver);
1293 printk(KERN_ERR "usb-serial: %s - tty_register_driver failed\n",
1295 goto exit_reg_driver;
1298 /* register the USB driver */
1299 result = usb_register(&usb_serial_driver);
1301 printk(KERN_ERR "usb-serial: %s - usb_register failed\n",
1306 /* register the generic driver, if we should */
1307 result = usb_serial_generic_register(debug);
1309 printk(KERN_ERR "usb-serial: %s - registering generic "
1310 "driver failed\n", __func__);
1314 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1319 usb_deregister(&usb_serial_driver);
1322 tty_unregister_driver(usb_serial_tty_driver);
1325 bus_unregister(&usb_serial_bus_type);
1328 printk(KERN_ERR "usb-serial: %s - returning with error %d\n",
1330 put_tty_driver(usb_serial_tty_driver);
1335 static void __exit usb_serial_exit(void)
1337 usb_serial_console_exit();
1339 usb_serial_generic_deregister();
1341 usb_deregister(&usb_serial_driver);
1342 tty_unregister_driver(usb_serial_tty_driver);
1343 put_tty_driver(usb_serial_tty_driver);
1344 bus_unregister(&usb_serial_bus_type);
1348 module_init(usb_serial_init);
1349 module_exit(usb_serial_exit);
1351 #define set_to_generic_if_null(type, function) \
1353 if (!type->function) { \
1354 type->function = usb_serial_generic_##function; \
1355 dbg("Had to override the " #function \
1356 " usb serial operation with the generic one.");\
1360 static void fixup_generic(struct usb_serial_driver *device)
1362 set_to_generic_if_null(device, open);
1363 set_to_generic_if_null(device, write);
1364 set_to_generic_if_null(device, close);
1365 set_to_generic_if_null(device, write_room);
1366 set_to_generic_if_null(device, chars_in_buffer);
1367 set_to_generic_if_null(device, read_bulk_callback);
1368 set_to_generic_if_null(device, write_bulk_callback);
1369 set_to_generic_if_null(device, disconnect);
1370 set_to_generic_if_null(device, release);
1371 set_to_generic_if_null(device, process_read_urb);
1372 set_to_generic_if_null(device, prepare_write_buffer);
1375 int usb_serial_register(struct usb_serial_driver *driver)
1377 /* must be called with BKL held */
1383 fixup_generic(driver);
1385 if (!driver->description)
1386 driver->description = driver->driver.name;
1387 if (!driver->usb_driver) {
1388 WARN(1, "Serial driver %s has no usb_driver\n",
1389 driver->description);
1392 driver->usb_driver->supports_autosuspend = 1;
1394 /* Add this device to our list of devices */
1395 mutex_lock(&table_lock);
1396 list_add(&driver->driver_list, &usb_serial_driver_list);
1398 retval = usb_serial_bus_register(driver);
1400 printk(KERN_ERR "usb-serial: problem %d when registering "
1401 "driver %s\n", retval, driver->description);
1402 list_del(&driver->driver_list);
1404 printk(KERN_INFO "USB Serial support registered for %s\n",
1405 driver->description);
1407 mutex_unlock(&table_lock);
1410 EXPORT_SYMBOL_GPL(usb_serial_register);
1413 void usb_serial_deregister(struct usb_serial_driver *device)
1415 /* must be called with BKL held */
1416 printk(KERN_INFO "USB Serial deregistering driver %s\n",
1417 device->description);
1418 mutex_lock(&table_lock);
1419 list_del(&device->driver_list);
1420 usb_serial_bus_deregister(device);
1421 mutex_unlock(&table_lock);
1423 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1425 /* Module information */
1426 MODULE_AUTHOR(DRIVER_AUTHOR);
1427 MODULE_DESCRIPTION(DRIVER_DESC);
1428 MODULE_LICENSE("GPL");
1430 module_param(debug, bool, S_IRUGO | S_IWUSR);
1431 MODULE_PARM_DESC(debug, "Debug enabled or not");