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 /* Driver structure we register with the USB core */
47 static struct usb_driver usb_serial_driver = {
49 .probe = usb_serial_probe,
50 .disconnect = usb_serial_disconnect,
51 .suspend = usb_serial_suspend,
52 .resume = usb_serial_resume,
54 .supports_autosuspend = 1,
57 static int MU509_USB = 0;
58 #define MU509_USB_PORT (SERIAL_TTY_MINORS - 10)
61 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
62 the MODULE_DEVICE_TABLE declarations in each serial driver
63 cause the "hotplug" program to pull in whatever module is necessary
64 via modprobe, and modprobe will load usbserial because the serial
69 /* initially all NULL */
70 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
71 static DEFINE_MUTEX(table_lock);
72 static LIST_HEAD(usb_serial_driver_list);
75 * Look up the serial structure. If it is found and it hasn't been
76 * disconnected, return with its disc_mutex held and its refcount
77 * incremented. Otherwise return NULL.
79 struct usb_serial *usb_serial_get_by_index(unsigned index)
81 struct usb_serial *serial;
83 mutex_lock(&table_lock);
84 serial = serial_table[index];
87 mutex_lock(&serial->disc_mutex);
88 if (serial->disconnected) {
89 mutex_unlock(&serial->disc_mutex);
92 kref_get(&serial->kref);
95 mutex_unlock(&table_lock);
99 static struct usb_serial *get_free_serial(struct usb_serial *serial,
100 int num_ports, unsigned int *minor)
106 dbg("%s %d", __func__, num_ports);
109 mutex_lock(&table_lock);
114 for (i = a; i < SERIAL_TTY_MINORS; ++i) {
119 for (j = 1; j <= num_ports-1; ++j)
120 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
130 dbg("%s - minor base = %d", __func__, *minor);
131 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
132 serial_table[i] = serial;
133 serial->port[j++]->number = i;
135 mutex_unlock(&table_lock);
138 mutex_unlock(&table_lock);
142 static void return_serial(struct usb_serial *serial)
148 mutex_lock(&table_lock);
149 for (i = 0; i < serial->num_ports; ++i)
150 serial_table[serial->minor + i] = NULL;
151 mutex_unlock(&table_lock);
154 static void destroy_serial(struct kref *kref)
156 struct usb_serial *serial;
157 struct usb_serial_port *port;
160 serial = to_usb_serial(kref);
162 dbg("%s - %s", __func__, serial->type->description);
164 /* return the minor range that this device had */
165 if (serial->minor != SERIAL_TTY_NO_MINOR)
166 return_serial(serial);
168 if (serial->attached)
169 serial->type->release(serial);
171 /* Now that nothing is using the ports, they can be freed */
172 for (i = 0; i < serial->num_port_pointers; ++i) {
173 port = serial->port[i];
176 put_device(&port->dev);
180 usb_put_dev(serial->dev);
184 void usb_serial_put(struct usb_serial *serial)
186 kref_put(&serial->kref, destroy_serial);
189 /*****************************************************************************
190 * Driver tty interface functions
191 *****************************************************************************/
194 * serial_install - install tty
195 * @driver: the driver (USB in our case)
196 * @tty: the tty being created
198 * Create the termios objects for this tty. We use the default
199 * USB serial settings but permit them to be overridden by
200 * serial->type->init_termios.
202 * This is the first place a new tty gets used. Hence this is where we
203 * acquire references to the usb_serial structure and the driver module,
204 * where we store a pointer to the port, and where we do an autoresume.
205 * All these actions are reversed in serial_cleanup().
207 static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
209 int idx = tty->index;
210 struct usb_serial *serial;
211 struct usb_serial_port *port;
212 int retval = -ENODEV;
216 serial = usb_serial_get_by_index(idx);
220 port = serial->port[idx - serial->minor];
223 if (!try_module_get(serial->type->driver.owner))
224 goto error_module_get;
226 /* perform the standard setup */
227 retval = tty_init_termios(tty);
229 goto error_init_termios;
231 retval = usb_autopm_get_interface(serial->interface);
233 goto error_get_interface;
235 mutex_unlock(&serial->disc_mutex);
237 /* allow the driver to update the settings */
238 if (serial->type->init_termios)
239 serial->type->init_termios(tty);
241 tty->driver_data = port;
243 /* Final install (we use the default method) */
244 tty_driver_kref_get(driver);
246 driver->ttys[idx] = tty;
251 module_put(serial->type->driver.owner);
254 usb_serial_put(serial);
255 mutex_unlock(&serial->disc_mutex);
259 static int serial_activate(struct tty_port *tport, struct tty_struct *tty)
261 struct usb_serial_port *port =
262 container_of(tport, struct usb_serial_port, port);
263 struct usb_serial *serial = port->serial;
266 mutex_lock(&serial->disc_mutex);
267 if (serial->disconnected)
270 retval = port->serial->type->open(tty, port);
271 mutex_unlock(&serial->disc_mutex);
275 static int serial_open(struct tty_struct *tty, struct file *filp)
277 struct usb_serial_port *port = tty->driver_data;
279 dbg("%s - port %d", __func__, port->number);
280 return tty_port_open(&port->port, tty, filp);
284 * serial_down - shut down hardware
285 * @tport: tty port to shut down
287 * Shut down a USB serial port unless it is the console. We never
288 * shut down the console hardware as it will always be in use. Serialized
289 * against activate by the tport mutex and kept to matching open/close pairs
290 * of calls by the ASYNCB_INITIALIZED flag.
292 static void serial_down(struct tty_port *tport)
294 struct usb_serial_port *port =
295 container_of(tport, struct usb_serial_port, port);
296 struct usb_serial_driver *drv = port->serial->type;
298 * The console is magical. Do not hang up the console hardware
299 * or there will be tears.
301 if (port->port.console)
307 static void serial_hangup(struct tty_struct *tty)
309 struct usb_serial_port *port = tty->driver_data;
310 dbg("%s - port %d", __func__, port->number);
311 tty_port_hangup(&port->port);
314 static void serial_close(struct tty_struct *tty, struct file *filp)
316 struct usb_serial_port *port = tty->driver_data;
317 dbg("%s - port %d", __func__, port->number);
318 tty_port_close(&port->port, tty, filp);
322 * serial_cleanup - free resources post close/hangup
323 * @port: port to free up
325 * Do the resource freeing and refcount dropping for the port.
326 * Avoid freeing the console.
328 * Called asynchronously after the last tty kref is dropped,
329 * and the tty layer has already done the tty_shutdown(tty);
331 static void serial_cleanup(struct tty_struct *tty)
333 struct usb_serial_port *port = tty->driver_data;
334 struct usb_serial *serial;
335 struct module *owner;
337 /* The console is magical. Do not hang up the console hardware
338 * or there will be tears.
340 if (port->port.console)
343 dbg("%s - port %d", __func__, port->number);
345 tty->driver_data = NULL;
347 serial = port->serial;
348 owner = serial->type->driver.owner;
350 mutex_lock(&serial->disc_mutex);
351 if (!serial->disconnected)
352 usb_autopm_put_interface(serial->interface);
353 mutex_unlock(&serial->disc_mutex);
355 usb_serial_put(serial);
359 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
362 struct usb_serial_port *port = tty->driver_data;
363 int retval = -ENODEV;
365 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
368 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
370 /* pass on to the driver specific version of this function */
371 retval = port->serial->type->write(tty, port, buf, count);
377 static int serial_write_room(struct tty_struct *tty)
379 struct usb_serial_port *port = tty->driver_data;
380 dbg("%s - port %d", __func__, port->number);
381 /* pass on to the driver specific version of this function */
382 return port->serial->type->write_room(tty);
385 static int serial_chars_in_buffer(struct tty_struct *tty)
387 struct usb_serial_port *port = tty->driver_data;
388 dbg("%s - port %d", __func__, port->number);
390 /* if the device was unplugged then any remaining characters
391 fell out of the connector ;) */
392 if (port->serial->disconnected)
394 /* pass on to the driver specific version of this function */
395 return port->serial->type->chars_in_buffer(tty);
398 static void serial_throttle(struct tty_struct *tty)
400 struct usb_serial_port *port = tty->driver_data;
401 dbg("%s - port %d", __func__, port->number);
403 /* pass on to the driver specific version of this function */
404 if (port->serial->type->throttle)
405 port->serial->type->throttle(tty);
408 static void serial_unthrottle(struct tty_struct *tty)
410 struct usb_serial_port *port = tty->driver_data;
411 dbg("%s - port %d", __func__, port->number);
413 /* pass on to the driver specific version of this function */
414 if (port->serial->type->unthrottle)
415 port->serial->type->unthrottle(tty);
418 static int serial_ioctl(struct tty_struct *tty,
419 unsigned int cmd, unsigned long arg)
421 struct usb_serial_port *port = tty->driver_data;
422 int retval = -ENODEV;
424 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
426 /* pass on to the driver specific version of this function
427 if it is available */
428 if (port->serial->type->ioctl) {
429 retval = port->serial->type->ioctl(tty, cmd, arg);
431 retval = -ENOIOCTLCMD;
435 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
437 struct usb_serial_port *port = tty->driver_data;
438 dbg("%s - port %d", __func__, port->number);
440 /* pass on to the driver specific version of this function
441 if it is available */
442 if (port->serial->type->set_termios)
443 port->serial->type->set_termios(tty, port, old);
445 tty_termios_copy_hw(tty->termios, old);
448 static int serial_break(struct tty_struct *tty, int break_state)
450 struct usb_serial_port *port = tty->driver_data;
452 dbg("%s - port %d", __func__, port->number);
454 /* pass on to the driver specific version of this function
455 if it is available */
456 if (port->serial->type->break_ctl)
457 port->serial->type->break_ctl(tty, break_state);
461 static int serial_proc_show(struct seq_file *m, void *v)
463 struct usb_serial *serial;
468 seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
469 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
470 serial = usb_serial_get_by_index(i);
474 seq_printf(m, "%d:", i);
475 if (serial->type->driver.owner)
476 seq_printf(m, " module:%s",
477 module_name(serial->type->driver.owner));
478 seq_printf(m, " name:\"%s\"",
479 serial->type->description);
480 seq_printf(m, " vendor:%04x product:%04x",
481 le16_to_cpu(serial->dev->descriptor.idVendor),
482 le16_to_cpu(serial->dev->descriptor.idProduct));
483 seq_printf(m, " num_ports:%d", serial->num_ports);
484 seq_printf(m, " port:%d", i - serial->minor + 1);
485 usb_make_path(serial->dev, tmp, sizeof(tmp));
486 seq_printf(m, " path:%s", tmp);
489 usb_serial_put(serial);
490 mutex_unlock(&serial->disc_mutex);
495 static int serial_proc_open(struct inode *inode, struct file *file)
497 return single_open(file, serial_proc_show, NULL);
500 static const struct file_operations serial_proc_fops = {
501 .owner = THIS_MODULE,
502 .open = serial_proc_open,
505 .release = single_release,
508 static int serial_tiocmget(struct tty_struct *tty)
510 struct usb_serial_port *port = tty->driver_data;
512 dbg("%s - port %d", __func__, port->number);
514 if (port->serial->type->tiocmget)
515 return port->serial->type->tiocmget(tty);
519 static int serial_tiocmset(struct tty_struct *tty,
520 unsigned int set, unsigned int clear)
522 struct usb_serial_port *port = tty->driver_data;
524 dbg("%s - port %d", __func__, port->number);
526 if (port->serial->type->tiocmset)
527 return port->serial->type->tiocmset(tty, set, clear);
531 static int serial_get_icount(struct tty_struct *tty,
532 struct serial_icounter_struct *icount)
534 struct usb_serial_port *port = tty->driver_data;
536 dbg("%s - port %d", __func__, port->number);
538 if (port->serial->type->get_icount)
539 return port->serial->type->get_icount(tty, icount);
544 * We would be calling tty_wakeup here, but unfortunately some line
545 * disciplines have an annoying habit of calling tty->write from
546 * the write wakeup callback (e.g. n_hdlc.c).
548 void usb_serial_port_softint(struct usb_serial_port *port)
550 schedule_work(&port->work);
552 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
554 static void usb_serial_port_work(struct work_struct *work)
556 struct usb_serial_port *port =
557 container_of(work, struct usb_serial_port, work);
558 struct tty_struct *tty;
560 dbg("%s - port %d", __func__, port->number);
562 tty = tty_port_tty_get(&port->port);
570 static void kill_traffic(struct usb_serial_port *port)
574 usb_kill_urb(port->read_urb);
575 usb_kill_urb(port->write_urb);
576 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
577 usb_kill_urb(port->write_urbs[i]);
580 * Some drivers submit the read_urb in the
581 * handler for the write_urb or vice versa
582 * this order determines the order in which
583 * usb_kill_urb() must be used to reliably
584 * kill the URBs. As it is unknown here,
585 * both orders must be used in turn.
586 * The call below is not redundant.
588 usb_kill_urb(port->read_urb);
589 usb_kill_urb(port->interrupt_in_urb);
590 usb_kill_urb(port->interrupt_out_urb);
593 static void port_release(struct device *dev)
595 struct usb_serial_port *port = to_usb_serial_port(dev);
598 dbg ("%s - %s", __func__, dev_name(dev));
601 * Stop all the traffic before cancelling the work, so that
602 * nobody will restart it by calling usb_serial_port_softint.
605 cancel_work_sync(&port->work);
607 usb_free_urb(port->read_urb);
608 usb_free_urb(port->write_urb);
609 usb_free_urb(port->interrupt_in_urb);
610 usb_free_urb(port->interrupt_out_urb);
611 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
612 usb_free_urb(port->write_urbs[i]);
613 kfree(port->bulk_out_buffers[i]);
615 kfifo_free(&port->write_fifo);
616 kfree(port->bulk_in_buffer);
617 kfree(port->bulk_out_buffer);
618 kfree(port->interrupt_in_buffer);
619 kfree(port->interrupt_out_buffer);
623 static struct usb_serial *create_serial(struct usb_device *dev,
624 struct usb_interface *interface,
625 struct usb_serial_driver *driver)
627 struct usb_serial *serial;
629 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
631 dev_err(&dev->dev, "%s - out of memory\n", __func__);
634 serial->dev = usb_get_dev(dev);
635 serial->type = driver;
636 serial->interface = interface;
637 kref_init(&serial->kref);
638 mutex_init(&serial->disc_mutex);
639 serial->minor = SERIAL_TTY_NO_MINOR;
644 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
645 struct usb_serial_driver *drv)
647 struct usb_dynid *dynid;
649 spin_lock(&drv->dynids.lock);
650 list_for_each_entry(dynid, &drv->dynids.list, node) {
651 if (usb_match_one_id(intf, &dynid->id)) {
652 spin_unlock(&drv->dynids.lock);
656 spin_unlock(&drv->dynids.lock);
660 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
661 struct usb_interface *intf)
663 const struct usb_device_id *id;
665 id = usb_match_id(intf, drv->id_table);
667 dbg("static descriptor matches");
670 id = match_dynamic_id(intf, drv);
672 dbg("dynamic descriptor matches");
677 /* Caller must hold table_lock */
678 static struct usb_serial_driver *search_serial_device(
679 struct usb_interface *iface)
681 const struct usb_device_id *id;
682 struct usb_serial_driver *drv;
684 /* Check if the usb id matches a known device */
685 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
686 id = get_iface_id(drv, iface);
694 static int serial_carrier_raised(struct tty_port *port)
696 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
697 struct usb_serial_driver *drv = p->serial->type;
698 if (drv->carrier_raised)
699 return drv->carrier_raised(p);
700 /* No carrier control - don't block */
704 static void serial_dtr_rts(struct tty_port *port, int on)
706 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
707 struct usb_serial_driver *drv = p->serial->type;
712 static const struct tty_port_operations serial_port_ops = {
713 .carrier_raised = serial_carrier_raised,
714 .dtr_rts = serial_dtr_rts,
715 .activate = serial_activate,
716 .shutdown = serial_down,
719 int usb_serial_probe(struct usb_interface *interface,
720 const struct usb_device_id *id)
722 struct usb_device *dev = interface_to_usbdev(interface);
723 struct usb_serial *serial = NULL;
724 struct usb_serial_port *port;
725 struct usb_host_interface *iface_desc;
726 struct usb_endpoint_descriptor *endpoint;
727 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
728 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
729 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
730 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
731 struct usb_serial_driver *type = NULL;
736 int num_interrupt_in = 0;
737 int num_interrupt_out = 0;
739 int num_bulk_out = 0;
743 mutex_lock(&table_lock);
744 type = search_serial_device(interface);
746 mutex_unlock(&table_lock);
751 if (!try_module_get(type->driver.owner)) {
752 mutex_unlock(&table_lock);
753 dev_err(&interface->dev, "module get failed, exiting\n");
756 mutex_unlock(&table_lock);
758 serial = create_serial(dev, interface, type);
760 module_put(type->driver.owner);
761 dev_err(&interface->dev, "%s - out of memory\n", __func__);
765 /* if this device type has a probe function, call it */
767 const struct usb_device_id *id;
769 id = get_iface_id(type, interface);
770 retval = type->probe(serial, id);
773 dbg("sub driver rejected device");
775 module_put(type->driver.owner);
780 /* descriptor matches, let's find the endpoints needed */
781 /* check out the endpoints */
782 iface_desc = interface->cur_altsetting;
783 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
784 endpoint = &iface_desc->endpoint[i].desc;
786 if (usb_endpoint_is_bulk_in(endpoint)) {
787 /* we found a bulk in endpoint */
788 dbg("found bulk in on endpoint %d", i);
789 bulk_in_endpoint[num_bulk_in] = endpoint;
793 if (usb_endpoint_is_bulk_out(endpoint)) {
794 /* we found a bulk out endpoint */
795 dbg("found bulk out on endpoint %d", i);
796 bulk_out_endpoint[num_bulk_out] = endpoint;
800 if (usb_endpoint_is_int_in(endpoint)) {
801 /* we found a interrupt in endpoint */
802 dbg("found interrupt in on endpoint %d", i);
803 interrupt_in_endpoint[num_interrupt_in] = endpoint;
807 if (usb_endpoint_is_int_out(endpoint)) {
808 /* we found an interrupt out endpoint */
809 dbg("found interrupt out on endpoint %d", i);
810 interrupt_out_endpoint[num_interrupt_out] = endpoint;
815 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
816 /* BEGIN HORRIBLE HACK FOR PL2303 */
817 /* this is needed due to the looney way its endpoints are set up */
818 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
819 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
820 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
821 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
822 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
823 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
824 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
825 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
826 if (interface != dev->actconfig->interface[0]) {
827 /* check out the endpoints of the other interface*/
828 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
829 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
830 endpoint = &iface_desc->endpoint[i].desc;
831 if (usb_endpoint_is_int_in(endpoint)) {
832 /* we found a interrupt in endpoint */
833 dbg("found interrupt in for Prolific device on separate interface");
834 interrupt_in_endpoint[num_interrupt_in] = endpoint;
840 /* Now make sure the PL-2303 is configured correctly.
841 * If not, give up now and hope this hack will work
842 * properly during a later invocation of usb_serial_probe
844 if (num_bulk_in == 0 || num_bulk_out == 0) {
845 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
847 module_put(type->driver.owner);
851 /* END HORRIBLE HACK FOR PL2303 */
854 #ifdef CONFIG_USB_SERIAL_GENERIC
855 if (type == &usb_serial_generic_device) {
856 num_ports = num_bulk_out;
857 if (num_ports == 0) {
858 dev_err(&interface->dev,
859 "Generic device with no bulk out, not allowed.\n");
861 module_put(type->driver.owner);
867 /* if this device type has a calc_num_ports function, call it */
868 if (type->calc_num_ports)
869 num_ports = type->calc_num_ports(serial);
871 num_ports = type->num_ports;
874 serial->num_ports = num_ports;
875 serial->num_bulk_in = num_bulk_in;
876 serial->num_bulk_out = num_bulk_out;
877 serial->num_interrupt_in = num_interrupt_in;
878 serial->num_interrupt_out = num_interrupt_out;
880 /* found all that we need */
881 dev_info(&interface->dev, "%s converter detected\n",
884 /* create our ports, we need as many as the max endpoints */
885 /* we don't use num_ports here because some devices have more
886 endpoint pairs than ports */
887 max_endpoints = max(num_bulk_in, num_bulk_out);
888 max_endpoints = max(max_endpoints, num_interrupt_in);
889 max_endpoints = max(max_endpoints, num_interrupt_out);
890 max_endpoints = max(max_endpoints, (int)serial->num_ports);
891 serial->num_port_pointers = max_endpoints;
893 dbg("%s - setting up %d port structures for this device",
894 __func__, max_endpoints);
895 for (i = 0; i < max_endpoints; ++i) {
896 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
899 tty_port_init(&port->port);
900 port->port.ops = &serial_port_ops;
901 port->serial = serial;
902 spin_lock_init(&port->lock);
903 /* Keep this for private driver use for the moment but
904 should probably go away */
905 INIT_WORK(&port->work, usb_serial_port_work);
906 serial->port[i] = port;
907 port->dev.parent = &interface->dev;
908 port->dev.driver = NULL;
909 port->dev.bus = &usb_serial_bus_type;
910 port->dev.release = &port_release;
911 device_initialize(&port->dev);
914 /* set up the endpoint information */
915 for (i = 0; i < num_bulk_in; ++i) {
916 endpoint = bulk_in_endpoint[i];
917 port = serial->port[i];
918 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
919 if (!port->read_urb) {
920 dev_err(&interface->dev, "No free urbs available\n");
923 buffer_size = max_t(int, serial->type->bulk_in_size,
924 le16_to_cpu(endpoint->wMaxPacketSize));
925 port->bulk_in_size = buffer_size;
926 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
927 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
928 if (!port->bulk_in_buffer) {
929 dev_err(&interface->dev,
930 "Couldn't allocate bulk_in_buffer\n");
933 usb_fill_bulk_urb(port->read_urb, dev,
935 endpoint->bEndpointAddress),
936 port->bulk_in_buffer, buffer_size,
937 serial->type->read_bulk_callback, port);
940 for (i = 0; i < num_bulk_out; ++i) {
943 endpoint = bulk_out_endpoint[i];
944 port = serial->port[i];
945 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
946 if (!port->write_urb) {
947 dev_err(&interface->dev, "No free urbs available\n");
950 if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
952 buffer_size = serial->type->bulk_out_size;
954 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
955 port->bulk_out_size = buffer_size;
956 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
957 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
958 if (!port->bulk_out_buffer) {
959 dev_err(&interface->dev,
960 "Couldn't allocate bulk_out_buffer\n");
963 usb_fill_bulk_urb(port->write_urb, dev,
965 endpoint->bEndpointAddress),
966 port->bulk_out_buffer, buffer_size,
967 serial->type->write_bulk_callback, port);
968 for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
969 set_bit(j, &port->write_urbs_free);
970 port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
971 if (!port->write_urbs[j]) {
972 dev_err(&interface->dev,
973 "No free urbs available\n");
976 port->bulk_out_buffers[j] = kmalloc(buffer_size,
978 if (!port->bulk_out_buffers[j]) {
979 dev_err(&interface->dev,
980 "Couldn't allocate bulk_out_buffer\n");
983 usb_fill_bulk_urb(port->write_urbs[j], dev,
985 endpoint->bEndpointAddress),
986 port->bulk_out_buffers[j], buffer_size,
987 serial->type->write_bulk_callback,
992 if (serial->type->read_int_callback) {
993 for (i = 0; i < num_interrupt_in; ++i) {
994 endpoint = interrupt_in_endpoint[i];
995 port = serial->port[i];
996 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
997 if (!port->interrupt_in_urb) {
998 dev_err(&interface->dev,
999 "No free urbs available\n");
1002 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1003 port->interrupt_in_endpointAddress =
1004 endpoint->bEndpointAddress;
1005 port->interrupt_in_buffer = kmalloc(buffer_size,
1007 if (!port->interrupt_in_buffer) {
1008 dev_err(&interface->dev,
1009 "Couldn't allocate interrupt_in_buffer\n");
1012 usb_fill_int_urb(port->interrupt_in_urb, dev,
1014 endpoint->bEndpointAddress),
1015 port->interrupt_in_buffer, buffer_size,
1016 serial->type->read_int_callback, port,
1017 endpoint->bInterval);
1019 } else if (num_interrupt_in) {
1020 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
1023 if (serial->type->write_int_callback) {
1024 for (i = 0; i < num_interrupt_out; ++i) {
1025 endpoint = interrupt_out_endpoint[i];
1026 port = serial->port[i];
1027 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
1028 if (!port->interrupt_out_urb) {
1029 dev_err(&interface->dev,
1030 "No free urbs available\n");
1033 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1034 port->interrupt_out_size = buffer_size;
1035 port->interrupt_out_endpointAddress =
1036 endpoint->bEndpointAddress;
1037 port->interrupt_out_buffer = kmalloc(buffer_size,
1039 if (!port->interrupt_out_buffer) {
1040 dev_err(&interface->dev,
1041 "Couldn't allocate interrupt_out_buffer\n");
1044 usb_fill_int_urb(port->interrupt_out_urb, dev,
1046 endpoint->bEndpointAddress),
1047 port->interrupt_out_buffer, buffer_size,
1048 serial->type->write_int_callback, port,
1049 endpoint->bInterval);
1051 } else if (num_interrupt_out) {
1052 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
1055 /* if this device type has an attach function, call it */
1057 retval = type->attach(serial);
1060 serial->attached = 1;
1062 /* quietly accept this device, but don't bind to a
1063 serial port as it's about to disappear */
1064 serial->num_ports = 0;
1068 serial->attached = 1;
1071 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x12D1 ) && (le16_to_cpu(dev->descriptor.idProduct) == 0x1001))
1077 if (get_free_serial(serial, num_ports, &minor) == NULL) {
1078 dev_err(&interface->dev, "No more free serial devices\n");
1081 serial->minor = minor;
1083 /* register all of the individual ports with the driver core */
1084 for (i = 0; i < num_ports; ++i) {
1085 port = serial->port[i];
1086 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1087 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1088 port->dev_state = PORT_REGISTERING;
1089 device_enable_async_suspend(&port->dev);
1091 retval = device_add(&port->dev);
1093 dev_err(&port->dev, "Error registering port device, "
1095 port->dev_state = PORT_UNREGISTERED;
1097 port->dev_state = PORT_REGISTERED;
1101 usb_serial_console_init(debug, minor);
1105 usb_set_intfdata(interface, serial);
1106 module_put(type->driver.owner);
1110 usb_serial_put(serial);
1111 module_put(type->driver.owner);
1114 EXPORT_SYMBOL_GPL(usb_serial_probe);
1116 void usb_serial_disconnect(struct usb_interface *interface)
1119 struct usb_serial *serial = usb_get_intfdata(interface);
1120 struct device *dev = &interface->dev;
1121 struct usb_serial_port *port;
1123 usb_serial_console_disconnect(serial);
1124 dbg("%s", __func__);
1126 mutex_lock(&serial->disc_mutex);
1127 usb_set_intfdata(interface, NULL);
1128 /* must set a flag, to signal subdrivers */
1129 serial->disconnected = 1;
1130 mutex_unlock(&serial->disc_mutex);
1132 for (i = 0; i < serial->num_ports; ++i) {
1133 port = serial->port[i];
1135 struct tty_struct *tty = tty_port_tty_get(&port->port);
1141 cancel_work_sync(&port->work);
1142 if (port->dev_state == PORT_REGISTERED) {
1144 /* Make sure the port is bound so that the
1145 * driver's port_remove method is called.
1147 if (!port->dev.driver) {
1151 &serial->type->driver;
1152 rc = device_bind_driver(&port->dev);
1154 port->dev_state = PORT_UNREGISTERING;
1155 device_del(&port->dev);
1156 port->dev_state = PORT_UNREGISTERED;
1160 serial->type->disconnect(serial);
1162 /* let the last holder of this object cause it to be cleaned up */
1163 usb_serial_put(serial);
1164 dev_info(dev, "device disconnected\n");
1166 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1168 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1170 struct usb_serial *serial = usb_get_intfdata(intf);
1171 struct usb_serial_port *port;
1174 serial->suspending = 1;
1176 if (serial->type->suspend) {
1177 r = serial->type->suspend(serial, message);
1179 serial->suspending = 0;
1184 for (i = 0; i < serial->num_ports; ++i) {
1185 port = serial->port[i];
1193 EXPORT_SYMBOL(usb_serial_suspend);
1195 int usb_serial_resume(struct usb_interface *intf)
1197 struct usb_serial *serial = usb_get_intfdata(intf);
1200 serial->suspending = 0;
1201 if (serial->type->resume)
1202 rv = serial->type->resume(serial);
1204 rv = usb_serial_generic_resume(serial);
1208 EXPORT_SYMBOL(usb_serial_resume);
1210 static const struct tty_operations serial_ops = {
1211 .open = serial_open,
1212 .close = serial_close,
1213 .write = serial_write,
1214 .hangup = serial_hangup,
1215 .write_room = serial_write_room,
1216 .ioctl = serial_ioctl,
1217 .set_termios = serial_set_termios,
1218 .throttle = serial_throttle,
1219 .unthrottle = serial_unthrottle,
1220 .break_ctl = serial_break,
1221 .chars_in_buffer = serial_chars_in_buffer,
1222 .tiocmget = serial_tiocmget,
1223 .tiocmset = serial_tiocmset,
1224 .get_icount = serial_get_icount,
1225 .cleanup = serial_cleanup,
1226 .install = serial_install,
1227 .proc_fops = &serial_proc_fops,
1231 struct tty_driver *usb_serial_tty_driver;
1233 static int __init usb_serial_init(void)
1238 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1239 if (!usb_serial_tty_driver)
1242 /* Initialize our global data */
1243 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1244 serial_table[i] = NULL;
1246 result = bus_register(&usb_serial_bus_type);
1248 printk(KERN_ERR "usb-serial: %s - registering bus driver "
1249 "failed\n", __func__);
1253 usb_serial_tty_driver->owner = THIS_MODULE;
1254 usb_serial_tty_driver->driver_name = "usbserial";
1255 usb_serial_tty_driver->name = "ttyUSB";
1256 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1257 usb_serial_tty_driver->minor_start = 0;
1258 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1259 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1260 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1261 TTY_DRIVER_DYNAMIC_DEV;
1262 usb_serial_tty_driver->init_termios = tty_std_termios;
1263 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1265 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1266 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1267 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1268 result = tty_register_driver(usb_serial_tty_driver);
1270 printk(KERN_ERR "usb-serial: %s - tty_register_driver failed\n",
1272 goto exit_reg_driver;
1275 /* register the USB driver */
1276 result = usb_register(&usb_serial_driver);
1278 printk(KERN_ERR "usb-serial: %s - usb_register failed\n",
1283 /* register the generic driver, if we should */
1284 result = usb_serial_generic_register(debug);
1286 printk(KERN_ERR "usb-serial: %s - registering generic "
1287 "driver failed\n", __func__);
1291 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1296 usb_deregister(&usb_serial_driver);
1299 tty_unregister_driver(usb_serial_tty_driver);
1302 bus_unregister(&usb_serial_bus_type);
1305 printk(KERN_ERR "usb-serial: %s - returning with error %d\n",
1307 put_tty_driver(usb_serial_tty_driver);
1312 static void __exit usb_serial_exit(void)
1314 usb_serial_console_exit();
1316 usb_serial_generic_deregister();
1318 usb_deregister(&usb_serial_driver);
1319 tty_unregister_driver(usb_serial_tty_driver);
1320 put_tty_driver(usb_serial_tty_driver);
1321 bus_unregister(&usb_serial_bus_type);
1325 module_init(usb_serial_init);
1326 module_exit(usb_serial_exit);
1328 #define set_to_generic_if_null(type, function) \
1330 if (!type->function) { \
1331 type->function = usb_serial_generic_##function; \
1332 dbg("Had to override the " #function \
1333 " usb serial operation with the generic one.");\
1337 static void fixup_generic(struct usb_serial_driver *device)
1339 set_to_generic_if_null(device, open);
1340 set_to_generic_if_null(device, write);
1341 set_to_generic_if_null(device, close);
1342 set_to_generic_if_null(device, write_room);
1343 set_to_generic_if_null(device, chars_in_buffer);
1344 set_to_generic_if_null(device, read_bulk_callback);
1345 set_to_generic_if_null(device, write_bulk_callback);
1346 set_to_generic_if_null(device, disconnect);
1347 set_to_generic_if_null(device, release);
1348 set_to_generic_if_null(device, process_read_urb);
1349 set_to_generic_if_null(device, prepare_write_buffer);
1352 int usb_serial_register(struct usb_serial_driver *driver)
1354 /* must be called with BKL held */
1360 fixup_generic(driver);
1362 if (!driver->description)
1363 driver->description = driver->driver.name;
1364 if (!driver->usb_driver) {
1365 WARN(1, "Serial driver %s has no usb_driver\n",
1366 driver->description);
1369 driver->usb_driver->supports_autosuspend = 1;
1371 /* Add this device to our list of devices */
1372 mutex_lock(&table_lock);
1373 list_add(&driver->driver_list, &usb_serial_driver_list);
1375 retval = usb_serial_bus_register(driver);
1377 printk(KERN_ERR "usb-serial: problem %d when registering "
1378 "driver %s\n", retval, driver->description);
1379 list_del(&driver->driver_list);
1381 printk(KERN_INFO "USB Serial support registered for %s\n",
1382 driver->description);
1384 mutex_unlock(&table_lock);
1387 EXPORT_SYMBOL_GPL(usb_serial_register);
1390 void usb_serial_deregister(struct usb_serial_driver *device)
1392 /* must be called with BKL held */
1393 printk(KERN_INFO "USB Serial deregistering driver %s\n",
1394 device->description);
1395 mutex_lock(&table_lock);
1396 list_del(&device->driver_list);
1397 usb_serial_bus_deregister(device);
1398 mutex_unlock(&table_lock);
1400 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1402 /* Module information */
1403 MODULE_AUTHOR(DRIVER_AUTHOR);
1404 MODULE_DESCRIPTION(DRIVER_DESC);
1405 MODULE_LICENSE("GPL");
1407 module_param(debug, bool, S_IRUGO | S_IWUSR);
1408 MODULE_PARM_DESC(debug, "Debug enabled or not");