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 static int MW100_USB = 0;
62 #define MW100_USB_PORT (SERIAL_TTY_MINORS - 10)
65 static int MT6229_USB = 0;
66 #define MT6229_USB_PORT (SERIAL_TTY_MINORS - 10)
69 static int SEW868_USB = 0;
70 #define SEW868_USB_PORT (SERIAL_TTY_MINORS - 10)
73 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
74 the MODULE_DEVICE_TABLE declarations in each serial driver
75 cause the "hotplug" program to pull in whatever module is necessary
76 via modprobe, and modprobe will load usbserial because the serial
81 /* initially all NULL */
82 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
83 static DEFINE_MUTEX(table_lock);
84 static LIST_HEAD(usb_serial_driver_list);
87 * Look up the serial structure. If it is found and it hasn't been
88 * disconnected, return with its disc_mutex held and its refcount
89 * incremented. Otherwise return NULL.
91 struct usb_serial *usb_serial_get_by_index(unsigned index)
93 struct usb_serial *serial;
95 mutex_lock(&table_lock);
96 serial = serial_table[index];
99 mutex_lock(&serial->disc_mutex);
100 if (serial->disconnected) {
101 mutex_unlock(&serial->disc_mutex);
104 kref_get(&serial->kref);
107 mutex_unlock(&table_lock);
111 static struct usb_serial *get_free_serial(struct usb_serial *serial,
112 int num_ports, unsigned int *minor)
118 dbg("%s %d", __func__, num_ports);
121 mutex_lock(&table_lock);
138 for (i = a; i < SERIAL_TTY_MINORS; ++i) {
143 for (j = 1; j <= num_ports-1; ++j)
144 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
154 dbg("%s - minor base = %d", __func__, *minor);
155 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
156 serial_table[i] = serial;
157 serial->port[j++]->number = i;
159 mutex_unlock(&table_lock);
162 mutex_unlock(&table_lock);
166 static void return_serial(struct usb_serial *serial)
172 mutex_lock(&table_lock);
173 for (i = 0; i < serial->num_ports; ++i)
174 serial_table[serial->minor + i] = NULL;
175 mutex_unlock(&table_lock);
178 static void destroy_serial(struct kref *kref)
180 struct usb_serial *serial;
181 struct usb_serial_port *port;
184 serial = to_usb_serial(kref);
186 dbg("%s - %s", __func__, serial->type->description);
188 /* return the minor range that this device had */
189 if (serial->minor != SERIAL_TTY_NO_MINOR)
190 return_serial(serial);
192 if (serial->attached)
193 serial->type->release(serial);
195 /* Now that nothing is using the ports, they can be freed */
196 for (i = 0; i < serial->num_port_pointers; ++i) {
197 port = serial->port[i];
200 put_device(&port->dev);
204 usb_put_dev(serial->dev);
208 void usb_serial_put(struct usb_serial *serial)
210 kref_put(&serial->kref, destroy_serial);
213 /*****************************************************************************
214 * Driver tty interface functions
215 *****************************************************************************/
218 * serial_install - install tty
219 * @driver: the driver (USB in our case)
220 * @tty: the tty being created
222 * Create the termios objects for this tty. We use the default
223 * USB serial settings but permit them to be overridden by
224 * serial->type->init_termios.
226 * This is the first place a new tty gets used. Hence this is where we
227 * acquire references to the usb_serial structure and the driver module,
228 * where we store a pointer to the port, and where we do an autoresume.
229 * All these actions are reversed in serial_cleanup().
231 static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
233 int idx = tty->index;
234 struct usb_serial *serial;
235 struct usb_serial_port *port;
236 int retval = -ENODEV;
240 serial = usb_serial_get_by_index(idx);
244 port = serial->port[idx - serial->minor];
247 if (!try_module_get(serial->type->driver.owner))
248 goto error_module_get;
250 /* perform the standard setup */
251 retval = tty_init_termios(tty);
253 goto error_init_termios;
255 retval = usb_autopm_get_interface(serial->interface);
257 goto error_get_interface;
259 mutex_unlock(&serial->disc_mutex);
261 /* allow the driver to update the settings */
262 if (serial->type->init_termios)
263 serial->type->init_termios(tty);
265 tty->driver_data = port;
267 /* Final install (we use the default method) */
268 tty_driver_kref_get(driver);
270 driver->ttys[idx] = tty;
275 module_put(serial->type->driver.owner);
278 usb_serial_put(serial);
279 mutex_unlock(&serial->disc_mutex);
283 static int serial_activate(struct tty_port *tport, struct tty_struct *tty)
285 struct usb_serial_port *port =
286 container_of(tport, struct usb_serial_port, port);
287 struct usb_serial *serial = port->serial;
290 mutex_lock(&serial->disc_mutex);
291 if (serial->disconnected)
294 retval = port->serial->type->open(tty, port);
295 mutex_unlock(&serial->disc_mutex);
299 static int serial_open(struct tty_struct *tty, struct file *filp)
301 struct usb_serial_port *port = tty->driver_data;
303 dbg("%s - port %d", __func__, port->number);
304 return tty_port_open(&port->port, tty, filp);
308 * serial_down - shut down hardware
309 * @tport: tty port to shut down
311 * Shut down a USB serial port unless it is the console. We never
312 * shut down the console hardware as it will always be in use. Serialized
313 * against activate by the tport mutex and kept to matching open/close pairs
314 * of calls by the ASYNCB_INITIALIZED flag.
316 static void serial_down(struct tty_port *tport)
318 struct usb_serial_port *port =
319 container_of(tport, struct usb_serial_port, port);
320 struct usb_serial_driver *drv = port->serial->type;
322 * The console is magical. Do not hang up the console hardware
323 * or there will be tears.
325 if (port->port.console)
331 static void serial_hangup(struct tty_struct *tty)
333 struct usb_serial_port *port = tty->driver_data;
334 dbg("%s - port %d", __func__, port->number);
335 tty_port_hangup(&port->port);
338 static void serial_close(struct tty_struct *tty, struct file *filp)
340 struct usb_serial_port *port = tty->driver_data;
341 dbg("%s - port %d", __func__, port->number);
342 tty_port_close(&port->port, tty, filp);
346 * serial_cleanup - free resources post close/hangup
347 * @port: port to free up
349 * Do the resource freeing and refcount dropping for the port.
350 * Avoid freeing the console.
352 * Called asynchronously after the last tty kref is dropped,
353 * and the tty layer has already done the tty_shutdown(tty);
355 static void serial_cleanup(struct tty_struct *tty)
357 struct usb_serial_port *port = tty->driver_data;
358 struct usb_serial *serial;
359 struct module *owner;
361 /* The console is magical. Do not hang up the console hardware
362 * or there will be tears.
364 if (port->port.console)
367 dbg("%s - port %d", __func__, port->number);
369 tty->driver_data = NULL;
371 serial = port->serial;
372 owner = serial->type->driver.owner;
374 mutex_lock(&serial->disc_mutex);
375 if (!serial->disconnected)
376 usb_autopm_put_interface(serial->interface);
377 mutex_unlock(&serial->disc_mutex);
379 usb_serial_put(serial);
383 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
386 struct usb_serial_port *port = tty->driver_data;
387 int retval = -ENODEV;
389 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
392 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
394 /* pass on to the driver specific version of this function */
395 retval = port->serial->type->write(tty, port, buf, count);
401 static int serial_write_room(struct tty_struct *tty)
403 struct usb_serial_port *port = tty->driver_data;
404 dbg("%s - port %d", __func__, port->number);
405 /* pass on to the driver specific version of this function */
406 return port->serial->type->write_room(tty);
409 static int serial_chars_in_buffer(struct tty_struct *tty)
411 struct usb_serial_port *port = tty->driver_data;
412 dbg("%s - port %d", __func__, port->number);
414 /* if the device was unplugged then any remaining characters
415 fell out of the connector ;) */
416 if (port->serial->disconnected)
418 /* pass on to the driver specific version of this function */
419 return port->serial->type->chars_in_buffer(tty);
422 static void serial_throttle(struct tty_struct *tty)
424 struct usb_serial_port *port = tty->driver_data;
425 dbg("%s - port %d", __func__, port->number);
427 /* pass on to the driver specific version of this function */
428 if (port->serial->type->throttle)
429 port->serial->type->throttle(tty);
432 static void serial_unthrottle(struct tty_struct *tty)
434 struct usb_serial_port *port = tty->driver_data;
435 dbg("%s - port %d", __func__, port->number);
437 /* pass on to the driver specific version of this function */
438 if (port->serial->type->unthrottle)
439 port->serial->type->unthrottle(tty);
442 static int serial_ioctl(struct tty_struct *tty,
443 unsigned int cmd, unsigned long arg)
445 struct usb_serial_port *port = tty->driver_data;
446 int retval = -ENODEV;
448 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
450 /* pass on to the driver specific version of this function
451 if it is available */
452 if (port->serial->type->ioctl) {
453 retval = port->serial->type->ioctl(tty, cmd, arg);
455 retval = -ENOIOCTLCMD;
459 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
461 struct usb_serial_port *port = tty->driver_data;
462 dbg("%s - port %d", __func__, port->number);
464 /* pass on to the driver specific version of this function
465 if it is available */
466 if (port->serial->type->set_termios)
467 port->serial->type->set_termios(tty, port, old);
469 tty_termios_copy_hw(tty->termios, old);
472 static int serial_break(struct tty_struct *tty, int break_state)
474 struct usb_serial_port *port = tty->driver_data;
476 dbg("%s - port %d", __func__, port->number);
478 /* pass on to the driver specific version of this function
479 if it is available */
480 if (port->serial->type->break_ctl)
481 port->serial->type->break_ctl(tty, break_state);
485 static int serial_proc_show(struct seq_file *m, void *v)
487 struct usb_serial *serial;
492 seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
493 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
494 serial = usb_serial_get_by_index(i);
498 seq_printf(m, "%d:", i);
499 if (serial->type->driver.owner)
500 seq_printf(m, " module:%s",
501 module_name(serial->type->driver.owner));
502 seq_printf(m, " name:\"%s\"",
503 serial->type->description);
504 seq_printf(m, " vendor:%04x product:%04x",
505 le16_to_cpu(serial->dev->descriptor.idVendor),
506 le16_to_cpu(serial->dev->descriptor.idProduct));
507 seq_printf(m, " num_ports:%d", serial->num_ports);
508 seq_printf(m, " port:%d", i - serial->minor + 1);
509 usb_make_path(serial->dev, tmp, sizeof(tmp));
510 seq_printf(m, " path:%s", tmp);
513 usb_serial_put(serial);
514 mutex_unlock(&serial->disc_mutex);
519 static int serial_proc_open(struct inode *inode, struct file *file)
521 return single_open(file, serial_proc_show, NULL);
524 static const struct file_operations serial_proc_fops = {
525 .owner = THIS_MODULE,
526 .open = serial_proc_open,
529 .release = single_release,
532 static int serial_tiocmget(struct tty_struct *tty)
534 struct usb_serial_port *port = tty->driver_data;
536 dbg("%s - port %d", __func__, port->number);
538 if (port->serial->type->tiocmget)
539 return port->serial->type->tiocmget(tty);
543 static int serial_tiocmset(struct tty_struct *tty,
544 unsigned int set, unsigned int clear)
546 struct usb_serial_port *port = tty->driver_data;
548 dbg("%s - port %d", __func__, port->number);
550 if (port->serial->type->tiocmset)
551 return port->serial->type->tiocmset(tty, set, clear);
555 static int serial_get_icount(struct tty_struct *tty,
556 struct serial_icounter_struct *icount)
558 struct usb_serial_port *port = tty->driver_data;
560 dbg("%s - port %d", __func__, port->number);
562 if (port->serial->type->get_icount)
563 return port->serial->type->get_icount(tty, icount);
568 * We would be calling tty_wakeup here, but unfortunately some line
569 * disciplines have an annoying habit of calling tty->write from
570 * the write wakeup callback (e.g. n_hdlc.c).
572 void usb_serial_port_softint(struct usb_serial_port *port)
574 schedule_work(&port->work);
576 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
578 static void usb_serial_port_work(struct work_struct *work)
580 struct usb_serial_port *port =
581 container_of(work, struct usb_serial_port, work);
582 struct tty_struct *tty;
584 dbg("%s - port %d", __func__, port->number);
586 tty = tty_port_tty_get(&port->port);
594 static void kill_traffic(struct usb_serial_port *port)
598 usb_kill_urb(port->read_urb);
599 usb_kill_urb(port->write_urb);
600 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i)
601 usb_kill_urb(port->write_urbs[i]);
604 * Some drivers submit the read_urb in the
605 * handler for the write_urb or vice versa
606 * this order determines the order in which
607 * usb_kill_urb() must be used to reliably
608 * kill the URBs. As it is unknown here,
609 * both orders must be used in turn.
610 * The call below is not redundant.
612 usb_kill_urb(port->read_urb);
613 usb_kill_urb(port->interrupt_in_urb);
614 usb_kill_urb(port->interrupt_out_urb);
617 static void port_release(struct device *dev)
619 struct usb_serial_port *port = to_usb_serial_port(dev);
622 dbg ("%s - %s", __func__, dev_name(dev));
625 * Stop all the traffic before cancelling the work, so that
626 * nobody will restart it by calling usb_serial_port_softint.
629 cancel_work_sync(&port->work);
631 usb_free_urb(port->read_urb);
632 usb_free_urb(port->write_urb);
633 usb_free_urb(port->interrupt_in_urb);
634 usb_free_urb(port->interrupt_out_urb);
635 for (i = 0; i < ARRAY_SIZE(port->write_urbs); ++i) {
636 usb_free_urb(port->write_urbs[i]);
637 kfree(port->bulk_out_buffers[i]);
639 kfifo_free(&port->write_fifo);
640 kfree(port->bulk_in_buffer);
641 kfree(port->bulk_out_buffer);
642 kfree(port->interrupt_in_buffer);
643 kfree(port->interrupt_out_buffer);
647 static struct usb_serial *create_serial(struct usb_device *dev,
648 struct usb_interface *interface,
649 struct usb_serial_driver *driver)
651 struct usb_serial *serial;
653 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
655 dev_err(&dev->dev, "%s - out of memory\n", __func__);
658 serial->dev = usb_get_dev(dev);
659 serial->type = driver;
660 serial->interface = interface;
661 kref_init(&serial->kref);
662 mutex_init(&serial->disc_mutex);
663 serial->minor = SERIAL_TTY_NO_MINOR;
668 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
669 struct usb_serial_driver *drv)
671 struct usb_dynid *dynid;
673 spin_lock(&drv->dynids.lock);
674 list_for_each_entry(dynid, &drv->dynids.list, node) {
675 if (usb_match_one_id(intf, &dynid->id)) {
676 spin_unlock(&drv->dynids.lock);
680 spin_unlock(&drv->dynids.lock);
684 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
685 struct usb_interface *intf)
687 const struct usb_device_id *id;
689 id = usb_match_id(intf, drv->id_table);
691 dbg("static descriptor matches");
694 id = match_dynamic_id(intf, drv);
696 dbg("dynamic descriptor matches");
701 /* Caller must hold table_lock */
702 static struct usb_serial_driver *search_serial_device(
703 struct usb_interface *iface)
705 const struct usb_device_id *id;
706 struct usb_serial_driver *drv;
708 /* Check if the usb id matches a known device */
709 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
710 id = get_iface_id(drv, iface);
718 static int serial_carrier_raised(struct tty_port *port)
720 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
721 struct usb_serial_driver *drv = p->serial->type;
722 if (drv->carrier_raised)
723 return drv->carrier_raised(p);
724 /* No carrier control - don't block */
728 static void serial_dtr_rts(struct tty_port *port, int on)
730 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
731 struct usb_serial_driver *drv = p->serial->type;
736 static const struct tty_port_operations serial_port_ops = {
737 .carrier_raised = serial_carrier_raised,
738 .dtr_rts = serial_dtr_rts,
739 .activate = serial_activate,
740 .shutdown = serial_down,
743 int usb_serial_probe(struct usb_interface *interface,
744 const struct usb_device_id *id)
746 struct usb_device *dev = interface_to_usbdev(interface);
747 struct usb_serial *serial = NULL;
748 struct usb_serial_port *port;
749 struct usb_host_interface *iface_desc;
750 struct usb_endpoint_descriptor *endpoint;
751 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
752 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
753 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
754 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
755 struct usb_serial_driver *type = NULL;
760 int num_interrupt_in = 0;
761 int num_interrupt_out = 0;
763 int num_bulk_out = 0;
767 mutex_lock(&table_lock);
768 type = search_serial_device(interface);
770 mutex_unlock(&table_lock);
775 if (!try_module_get(type->driver.owner)) {
776 mutex_unlock(&table_lock);
777 dev_err(&interface->dev, "module get failed, exiting\n");
780 mutex_unlock(&table_lock);
782 serial = create_serial(dev, interface, type);
784 module_put(type->driver.owner);
785 dev_err(&interface->dev, "%s - out of memory\n", __func__);
789 /* if this device type has a probe function, call it */
791 const struct usb_device_id *id;
793 id = get_iface_id(type, interface);
794 retval = type->probe(serial, id);
797 dbg("sub driver rejected device");
799 module_put(type->driver.owner);
804 /* descriptor matches, let's find the endpoints needed */
805 /* check out the endpoints */
806 iface_desc = interface->cur_altsetting;
807 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
808 endpoint = &iface_desc->endpoint[i].desc;
810 if (usb_endpoint_is_bulk_in(endpoint)) {
811 /* we found a bulk in endpoint */
812 dbg("found bulk in on endpoint %d", i);
813 bulk_in_endpoint[num_bulk_in] = endpoint;
817 if (usb_endpoint_is_bulk_out(endpoint)) {
818 /* we found a bulk out endpoint */
819 dbg("found bulk out on endpoint %d", i);
820 bulk_out_endpoint[num_bulk_out] = endpoint;
824 if (usb_endpoint_is_int_in(endpoint)) {
825 /* we found a interrupt in endpoint */
826 dbg("found interrupt in on endpoint %d", i);
827 interrupt_in_endpoint[num_interrupt_in] = endpoint;
831 if (usb_endpoint_is_int_out(endpoint)) {
832 /* we found an interrupt out endpoint */
833 dbg("found interrupt out on endpoint %d", i);
834 interrupt_out_endpoint[num_interrupt_out] = endpoint;
839 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
840 /* BEGIN HORRIBLE HACK FOR PL2303 */
841 /* this is needed due to the looney way its endpoints are set up */
842 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
843 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
844 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
845 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
846 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
847 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
848 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
849 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
850 if (interface != dev->actconfig->interface[0]) {
851 /* check out the endpoints of the other interface*/
852 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
853 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
854 endpoint = &iface_desc->endpoint[i].desc;
855 if (usb_endpoint_is_int_in(endpoint)) {
856 /* we found a interrupt in endpoint */
857 dbg("found interrupt in for Prolific device on separate interface");
858 interrupt_in_endpoint[num_interrupt_in] = endpoint;
864 /* Now make sure the PL-2303 is configured correctly.
865 * If not, give up now and hope this hack will work
866 * properly during a later invocation of usb_serial_probe
868 if (num_bulk_in == 0 || num_bulk_out == 0) {
869 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
871 module_put(type->driver.owner);
875 /* END HORRIBLE HACK FOR PL2303 */
878 #ifdef CONFIG_USB_SERIAL_GENERIC
879 if (type == &usb_serial_generic_device) {
880 num_ports = num_bulk_out;
881 if (num_ports == 0) {
882 dev_err(&interface->dev,
883 "Generic device with no bulk out, not allowed.\n");
885 module_put(type->driver.owner);
891 /* if this device type has a calc_num_ports function, call it */
892 if (type->calc_num_ports)
893 num_ports = type->calc_num_ports(serial);
895 num_ports = type->num_ports;
898 serial->num_ports = num_ports;
899 serial->num_bulk_in = num_bulk_in;
900 serial->num_bulk_out = num_bulk_out;
901 serial->num_interrupt_in = num_interrupt_in;
902 serial->num_interrupt_out = num_interrupt_out;
904 /* found all that we need */
905 dev_info(&interface->dev, "%s converter detected\n",
908 /* create our ports, we need as many as the max endpoints */
909 /* we don't use num_ports here because some devices have more
910 endpoint pairs than ports */
911 max_endpoints = max(num_bulk_in, num_bulk_out);
912 max_endpoints = max(max_endpoints, num_interrupt_in);
913 max_endpoints = max(max_endpoints, num_interrupt_out);
914 max_endpoints = max(max_endpoints, (int)serial->num_ports);
915 serial->num_port_pointers = max_endpoints;
917 dbg("%s - setting up %d port structures for this device",
918 __func__, max_endpoints);
919 for (i = 0; i < max_endpoints; ++i) {
920 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
923 tty_port_init(&port->port);
924 port->port.ops = &serial_port_ops;
925 port->serial = serial;
926 spin_lock_init(&port->lock);
927 /* Keep this for private driver use for the moment but
928 should probably go away */
929 INIT_WORK(&port->work, usb_serial_port_work);
930 serial->port[i] = port;
931 port->dev.parent = &interface->dev;
932 port->dev.driver = NULL;
933 port->dev.bus = &usb_serial_bus_type;
934 port->dev.release = &port_release;
935 device_initialize(&port->dev);
938 /* set up the endpoint information */
939 for (i = 0; i < num_bulk_in; ++i) {
940 endpoint = bulk_in_endpoint[i];
941 port = serial->port[i];
942 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
943 if (!port->read_urb) {
944 dev_err(&interface->dev, "No free urbs available\n");
947 buffer_size = max_t(int, serial->type->bulk_in_size,
948 le16_to_cpu(endpoint->wMaxPacketSize));
949 port->bulk_in_size = buffer_size;
950 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
951 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
952 if (!port->bulk_in_buffer) {
953 dev_err(&interface->dev,
954 "Couldn't allocate bulk_in_buffer\n");
957 usb_fill_bulk_urb(port->read_urb, dev,
959 endpoint->bEndpointAddress),
960 port->bulk_in_buffer, buffer_size,
961 serial->type->read_bulk_callback, port);
964 for (i = 0; i < num_bulk_out; ++i) {
967 endpoint = bulk_out_endpoint[i];
968 port = serial->port[i];
969 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
970 if (!port->write_urb) {
971 dev_err(&interface->dev, "No free urbs available\n");
974 if (kfifo_alloc(&port->write_fifo, PAGE_SIZE, GFP_KERNEL))
976 buffer_size = serial->type->bulk_out_size;
978 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
979 port->bulk_out_size = buffer_size;
980 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
981 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
982 if (!port->bulk_out_buffer) {
983 dev_err(&interface->dev,
984 "Couldn't allocate bulk_out_buffer\n");
987 usb_fill_bulk_urb(port->write_urb, dev,
989 endpoint->bEndpointAddress),
990 port->bulk_out_buffer, buffer_size,
991 serial->type->write_bulk_callback, port);
992 for (j = 0; j < ARRAY_SIZE(port->write_urbs); ++j) {
993 set_bit(j, &port->write_urbs_free);
994 port->write_urbs[j] = usb_alloc_urb(0, GFP_KERNEL);
995 if (!port->write_urbs[j]) {
996 dev_err(&interface->dev,
997 "No free urbs available\n");
1000 port->bulk_out_buffers[j] = kmalloc(buffer_size,
1002 if (!port->bulk_out_buffers[j]) {
1003 dev_err(&interface->dev,
1004 "Couldn't allocate bulk_out_buffer\n");
1007 usb_fill_bulk_urb(port->write_urbs[j], dev,
1008 usb_sndbulkpipe(dev,
1009 endpoint->bEndpointAddress),
1010 port->bulk_out_buffers[j], buffer_size,
1011 serial->type->write_bulk_callback,
1016 if (serial->type->read_int_callback) {
1017 for (i = 0; i < num_interrupt_in; ++i) {
1018 endpoint = interrupt_in_endpoint[i];
1019 port = serial->port[i];
1020 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
1021 if (!port->interrupt_in_urb) {
1022 dev_err(&interface->dev,
1023 "No free urbs available\n");
1026 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1027 port->interrupt_in_endpointAddress =
1028 endpoint->bEndpointAddress;
1029 port->interrupt_in_buffer = kmalloc(buffer_size,
1031 if (!port->interrupt_in_buffer) {
1032 dev_err(&interface->dev,
1033 "Couldn't allocate interrupt_in_buffer\n");
1036 usb_fill_int_urb(port->interrupt_in_urb, dev,
1038 endpoint->bEndpointAddress),
1039 port->interrupt_in_buffer, buffer_size,
1040 serial->type->read_int_callback, port,
1041 endpoint->bInterval);
1043 } else if (num_interrupt_in) {
1044 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
1047 if (serial->type->write_int_callback) {
1048 for (i = 0; i < num_interrupt_out; ++i) {
1049 endpoint = interrupt_out_endpoint[i];
1050 port = serial->port[i];
1051 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
1052 if (!port->interrupt_out_urb) {
1053 dev_err(&interface->dev,
1054 "No free urbs available\n");
1057 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1058 port->interrupt_out_size = buffer_size;
1059 port->interrupt_out_endpointAddress =
1060 endpoint->bEndpointAddress;
1061 port->interrupt_out_buffer = kmalloc(buffer_size,
1063 if (!port->interrupt_out_buffer) {
1064 dev_err(&interface->dev,
1065 "Couldn't allocate interrupt_out_buffer\n");
1068 usb_fill_int_urb(port->interrupt_out_urb, dev,
1070 endpoint->bEndpointAddress),
1071 port->interrupt_out_buffer, buffer_size,
1072 serial->type->write_int_callback, port,
1073 endpoint->bInterval);
1075 } else if (num_interrupt_out) {
1076 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
1079 /* if this device type has an attach function, call it */
1081 retval = type->attach(serial);
1084 serial->attached = 1;
1086 /* quietly accept this device, but don't bind to a
1087 serial port as it's about to disappear */
1088 serial->num_ports = 0;
1092 serial->attached = 1;
1095 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x12D1 ) && (le16_to_cpu(dev->descriptor.idProduct) == 0x1001))
1101 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x19f5) && (le16_to_cpu(dev->descriptor.idProduct) == 0x9013))
1106 #ifdef CONFIG_MT6229
1107 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x0E8D) && (le16_to_cpu(dev->descriptor.idProduct) == 0x00A2))
1112 #ifdef CONFIG_SEW868
1113 if ((le16_to_cpu(dev->descriptor.idVendor) == 0x19d2) && (le16_to_cpu(dev->descriptor.idProduct) == 0xffeb))
1119 if (get_free_serial(serial, num_ports, &minor) == NULL) {
1120 dev_err(&interface->dev, "No more free serial devices\n");
1123 serial->minor = minor;
1125 /* register all of the individual ports with the driver core */
1126 for (i = 0; i < num_ports; ++i) {
1127 port = serial->port[i];
1128 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1129 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1130 port->dev_state = PORT_REGISTERING;
1131 device_enable_async_suspend(&port->dev);
1133 retval = device_add(&port->dev);
1135 dev_err(&port->dev, "Error registering port device, "
1137 port->dev_state = PORT_UNREGISTERED;
1139 port->dev_state = PORT_REGISTERED;
1143 usb_serial_console_init(debug, minor);
1147 usb_set_intfdata(interface, serial);
1148 module_put(type->driver.owner);
1152 usb_serial_put(serial);
1153 module_put(type->driver.owner);
1156 EXPORT_SYMBOL_GPL(usb_serial_probe);
1158 void usb_serial_disconnect(struct usb_interface *interface)
1161 struct usb_serial *serial = usb_get_intfdata(interface);
1162 struct device *dev = &interface->dev;
1163 struct usb_serial_port *port;
1165 usb_serial_console_disconnect(serial);
1166 dbg("%s", __func__);
1168 mutex_lock(&serial->disc_mutex);
1169 usb_set_intfdata(interface, NULL);
1170 /* must set a flag, to signal subdrivers */
1171 serial->disconnected = 1;
1172 mutex_unlock(&serial->disc_mutex);
1174 for (i = 0; i < serial->num_ports; ++i) {
1175 port = serial->port[i];
1177 struct tty_struct *tty = tty_port_tty_get(&port->port);
1183 cancel_work_sync(&port->work);
1184 if (port->dev_state == PORT_REGISTERED) {
1186 /* Make sure the port is bound so that the
1187 * driver's port_remove method is called.
1189 if (!port->dev.driver) {
1193 &serial->type->driver;
1194 rc = device_bind_driver(&port->dev);
1196 port->dev_state = PORT_UNREGISTERING;
1197 device_del(&port->dev);
1198 port->dev_state = PORT_UNREGISTERED;
1202 serial->type->disconnect(serial);
1204 /* let the last holder of this object cause it to be cleaned up */
1205 usb_serial_put(serial);
1206 dev_info(dev, "device disconnected\n");
1208 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1210 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1212 struct usb_serial *serial = usb_get_intfdata(intf);
1213 struct usb_serial_port *port;
1216 serial->suspending = 1;
1218 if (serial->type->suspend) {
1219 r = serial->type->suspend(serial, message);
1221 serial->suspending = 0;
1226 for (i = 0; i < serial->num_ports; ++i) {
1227 port = serial->port[i];
1235 EXPORT_SYMBOL(usb_serial_suspend);
1237 int usb_serial_resume(struct usb_interface *intf)
1239 struct usb_serial *serial = usb_get_intfdata(intf);
1242 serial->suspending = 0;
1243 if (serial->type->resume)
1244 rv = serial->type->resume(serial);
1246 rv = usb_serial_generic_resume(serial);
1250 EXPORT_SYMBOL(usb_serial_resume);
1252 static const struct tty_operations serial_ops = {
1253 .open = serial_open,
1254 .close = serial_close,
1255 .write = serial_write,
1256 .hangup = serial_hangup,
1257 .write_room = serial_write_room,
1258 .ioctl = serial_ioctl,
1259 .set_termios = serial_set_termios,
1260 .throttle = serial_throttle,
1261 .unthrottle = serial_unthrottle,
1262 .break_ctl = serial_break,
1263 .chars_in_buffer = serial_chars_in_buffer,
1264 .tiocmget = serial_tiocmget,
1265 .tiocmset = serial_tiocmset,
1266 .get_icount = serial_get_icount,
1267 .cleanup = serial_cleanup,
1268 .install = serial_install,
1269 .proc_fops = &serial_proc_fops,
1273 struct tty_driver *usb_serial_tty_driver;
1275 static int __init usb_serial_init(void)
1280 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1281 if (!usb_serial_tty_driver)
1284 /* Initialize our global data */
1285 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1286 serial_table[i] = NULL;
1288 result = bus_register(&usb_serial_bus_type);
1290 printk(KERN_ERR "usb-serial: %s - registering bus driver "
1291 "failed\n", __func__);
1295 usb_serial_tty_driver->owner = THIS_MODULE;
1296 usb_serial_tty_driver->driver_name = "usbserial";
1297 usb_serial_tty_driver->name = "ttyUSB";
1298 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1299 usb_serial_tty_driver->minor_start = 0;
1300 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1301 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1302 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1303 TTY_DRIVER_DYNAMIC_DEV;
1304 usb_serial_tty_driver->init_termios = tty_std_termios;
1305 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1307 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1308 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1309 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1310 result = tty_register_driver(usb_serial_tty_driver);
1312 printk(KERN_ERR "usb-serial: %s - tty_register_driver failed\n",
1314 goto exit_reg_driver;
1317 /* register the USB driver */
1318 result = usb_register(&usb_serial_driver);
1320 printk(KERN_ERR "usb-serial: %s - usb_register failed\n",
1325 /* register the generic driver, if we should */
1326 result = usb_serial_generic_register(debug);
1328 printk(KERN_ERR "usb-serial: %s - registering generic "
1329 "driver failed\n", __func__);
1333 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1338 usb_deregister(&usb_serial_driver);
1341 tty_unregister_driver(usb_serial_tty_driver);
1344 bus_unregister(&usb_serial_bus_type);
1347 printk(KERN_ERR "usb-serial: %s - returning with error %d\n",
1349 put_tty_driver(usb_serial_tty_driver);
1354 static void __exit usb_serial_exit(void)
1356 usb_serial_console_exit();
1358 usb_serial_generic_deregister();
1360 usb_deregister(&usb_serial_driver);
1361 tty_unregister_driver(usb_serial_tty_driver);
1362 put_tty_driver(usb_serial_tty_driver);
1363 bus_unregister(&usb_serial_bus_type);
1367 module_init(usb_serial_init);
1368 module_exit(usb_serial_exit);
1370 #define set_to_generic_if_null(type, function) \
1372 if (!type->function) { \
1373 type->function = usb_serial_generic_##function; \
1374 dbg("Had to override the " #function \
1375 " usb serial operation with the generic one.");\
1379 static void fixup_generic(struct usb_serial_driver *device)
1381 set_to_generic_if_null(device, open);
1382 set_to_generic_if_null(device, write);
1383 set_to_generic_if_null(device, close);
1384 set_to_generic_if_null(device, write_room);
1385 set_to_generic_if_null(device, chars_in_buffer);
1386 set_to_generic_if_null(device, read_bulk_callback);
1387 set_to_generic_if_null(device, write_bulk_callback);
1388 set_to_generic_if_null(device, disconnect);
1389 set_to_generic_if_null(device, release);
1390 set_to_generic_if_null(device, process_read_urb);
1391 set_to_generic_if_null(device, prepare_write_buffer);
1394 int usb_serial_register(struct usb_serial_driver *driver)
1396 /* must be called with BKL held */
1402 fixup_generic(driver);
1404 if (!driver->description)
1405 driver->description = driver->driver.name;
1406 if (!driver->usb_driver) {
1407 WARN(1, "Serial driver %s has no usb_driver\n",
1408 driver->description);
1411 driver->usb_driver->supports_autosuspend = 1;
1413 /* Add this device to our list of devices */
1414 mutex_lock(&table_lock);
1415 list_add(&driver->driver_list, &usb_serial_driver_list);
1417 retval = usb_serial_bus_register(driver);
1419 printk(KERN_ERR "usb-serial: problem %d when registering "
1420 "driver %s\n", retval, driver->description);
1421 list_del(&driver->driver_list);
1423 printk(KERN_INFO "USB Serial support registered for %s\n",
1424 driver->description);
1426 mutex_unlock(&table_lock);
1429 EXPORT_SYMBOL_GPL(usb_serial_register);
1432 void usb_serial_deregister(struct usb_serial_driver *device)
1434 /* must be called with BKL held */
1435 printk(KERN_INFO "USB Serial deregistering driver %s\n",
1436 device->description);
1437 mutex_lock(&table_lock);
1438 list_del(&device->driver_list);
1439 usb_serial_bus_deregister(device);
1440 mutex_unlock(&table_lock);
1442 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1444 /* Module information */
1445 MODULE_AUTHOR(DRIVER_AUTHOR);
1446 MODULE_DESCRIPTION(DRIVER_DESC);
1447 MODULE_LICENSE("GPL");
1449 module_param(debug, bool, S_IRUGO | S_IWUSR);
1450 MODULE_PARM_DESC(debug, "Debug enabled or not");