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/smp_lock.h>
25 #include <linux/tty.h>
26 #include <linux/tty_driver.h>
27 #include <linux/tty_flip.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/seq_file.h>
31 #include <linux/spinlock.h>
32 #include <linux/mutex.h>
33 #include <linux/list.h>
34 #include <linux/uaccess.h>
35 #include <linux/usb.h>
36 #include <linux/usb/serial.h>
42 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
43 #define DRIVER_DESC "USB Serial Driver core"
45 static void port_free(struct usb_serial_port *port);
47 /* Driver structure we register with the USB core */
48 static struct usb_driver usb_serial_driver = {
50 .probe = usb_serial_probe,
51 .disconnect = usb_serial_disconnect,
52 .suspend = usb_serial_suspend,
53 .resume = usb_serial_resume,
57 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
58 the MODULE_DEVICE_TABLE declarations in each serial driver
59 cause the "hotplug" program to pull in whatever module is necessary
60 via modprobe, and modprobe will load usbserial because the serial
65 /* initially all NULL */
66 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
67 static DEFINE_MUTEX(table_lock);
68 static LIST_HEAD(usb_serial_driver_list);
70 struct usb_serial *usb_serial_get_by_index(unsigned index)
72 struct usb_serial *serial;
74 mutex_lock(&table_lock);
75 serial = serial_table[index];
78 kref_get(&serial->kref);
79 mutex_unlock(&table_lock);
83 static struct usb_serial *get_free_serial(struct usb_serial *serial,
84 int num_ports, unsigned int *minor)
89 dbg("%s %d", __func__, num_ports);
92 mutex_lock(&table_lock);
93 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
98 for (j = 1; j <= num_ports-1; ++j)
99 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
109 dbg("%s - minor base = %d", __func__, *minor);
110 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
111 serial_table[i] = serial;
112 serial->port[j++]->number = i;
114 mutex_unlock(&table_lock);
117 mutex_unlock(&table_lock);
121 static void return_serial(struct usb_serial *serial)
127 for (i = 0; i < serial->num_ports; ++i)
128 serial_table[serial->minor + i] = NULL;
131 static void destroy_serial(struct kref *kref)
133 struct usb_serial *serial;
134 struct usb_serial_port *port;
137 serial = to_usb_serial(kref);
139 dbg("%s - %s", __func__, serial->type->description);
141 /* return the minor range that this device had */
142 if (serial->minor != SERIAL_TTY_NO_MINOR)
143 return_serial(serial);
145 serial->type->release(serial);
147 for (i = 0; i < serial->num_ports; ++i) {
148 port = serial->port[i];
150 put_device(&port->dev);
153 /* If this is a "fake" port, we have to clean it up here, as it will
154 * not get cleaned up in port_release() as it was never registered with
156 if (serial->num_ports < serial->num_port_pointers) {
157 for (i = serial->num_ports;
158 i < serial->num_port_pointers; ++i) {
159 port = serial->port[i];
165 usb_put_dev(serial->dev);
167 /* free up any memory that we allocated */
171 void usb_serial_put(struct usb_serial *serial)
173 mutex_lock(&table_lock);
174 kref_put(&serial->kref, destroy_serial);
175 mutex_unlock(&table_lock);
178 /*****************************************************************************
179 * Driver tty interface functions
180 *****************************************************************************/
181 static int serial_open (struct tty_struct *tty, struct file *filp)
183 struct usb_serial *serial;
184 struct usb_serial_port *port;
185 unsigned int portNumber;
190 /* get the serial object associated with this tty pointer */
191 serial = usb_serial_get_by_index(tty->index);
193 tty->driver_data = NULL;
197 mutex_lock(&serial->disc_mutex);
198 portNumber = tty->index - serial->minor;
199 port = serial->port[portNumber];
200 if (!port || serial->disconnected)
203 get_device(&port->dev);
205 * Note: Our locking order requirement does not allow port->mutex
206 * to be acquired while serial->disc_mutex is held.
208 mutex_unlock(&serial->disc_mutex);
210 goto bailout_serial_put;
212 if (mutex_lock_interruptible(&port->mutex)) {
213 retval = -ERESTARTSYS;
214 goto bailout_port_put;
219 /* set up our port structure making the tty driver
220 * remember our port object, and us it */
221 tty->driver_data = port;
222 tty_port_tty_set(&port->port, tty);
224 if (port->port.count == 1) {
226 /* lock this module before we call it
227 * this may fail, which means we must bail out,
228 * safe because we are called with BKL held */
229 if (!try_module_get(serial->type->driver.owner)) {
231 goto bailout_mutex_unlock;
234 mutex_lock(&serial->disc_mutex);
235 if (serial->disconnected)
238 retval = usb_autopm_get_interface(serial->interface);
240 goto bailout_module_put;
242 /* only call the device specific open if this
243 * is the first time the port is opened */
244 retval = serial->type->open(tty, port, filp);
246 goto bailout_interface_put;
247 mutex_unlock(&serial->disc_mutex);
249 mutex_unlock(&port->mutex);
250 /* Now do the correct tty layer semantics */
251 retval = tty_port_block_til_ready(&port->port, tty, filp);
255 bailout_interface_put:
256 usb_autopm_put_interface(serial->interface);
258 mutex_unlock(&serial->disc_mutex);
259 module_put(serial->type->driver.owner);
260 bailout_mutex_unlock:
261 port->port.count = 0;
262 tty->driver_data = NULL;
263 tty_port_tty_set(&port->port, NULL);
264 mutex_unlock(&port->mutex);
266 put_device(&port->dev);
268 usb_serial_put(serial);
273 * serial_do_down - shut down hardware
274 * @port: port to shut down
276 * Shut down a USB port unless it is the console. We never shut down the
277 * console hardware as it will always be in use.
279 * Don't free any resources at this point
281 static void serial_do_down(struct usb_serial_port *port)
283 struct usb_serial_driver *drv = port->serial->type;
284 struct usb_serial *serial;
285 struct module *owner;
287 /* The console is magical, do not hang up the console hardware
288 or there will be tears */
292 mutex_lock(&port->mutex);
293 serial = port->serial;
294 owner = serial->type->driver.owner;
299 mutex_unlock(&port->mutex);
303 * serial_do_free - free resources post close/hangup
304 * @port: port to free up
306 * Do the resource freeing and refcount dropping for the port. We must
307 * be careful about ordering and we must avoid freeing up the console.
310 static void serial_do_free(struct usb_serial_port *port)
312 struct usb_serial *serial;
313 struct module *owner;
315 /* The console is magical, do not hang up the console hardware
316 or there will be tears */
320 serial = port->serial;
321 owner = serial->type->driver.owner;
322 put_device(&port->dev);
323 /* Mustn't dereference port any more */
324 mutex_lock(&serial->disc_mutex);
325 if (!serial->disconnected)
326 usb_autopm_put_interface(serial->interface);
327 mutex_unlock(&serial->disc_mutex);
328 usb_serial_put(serial);
329 /* Mustn't dereference serial any more */
333 static void serial_close(struct tty_struct *tty, struct file *filp)
335 struct usb_serial_port *port = tty->driver_data;
340 dbg("%s - port %d", __func__, port->number);
343 if (tty_port_close_start(&port->port, tty, filp) == 0)
346 serial_do_down(port);
347 tty_port_close_end(&port->port, tty);
348 tty_port_tty_set(&port->port, NULL);
349 serial_do_free(port);
352 static void serial_hangup(struct tty_struct *tty)
354 struct usb_serial_port *port = tty->driver_data;
355 serial_do_down(port);
356 tty_port_hangup(&port->port);
357 serial_do_free(port);
360 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
363 struct usb_serial_port *port = tty->driver_data;
364 int retval = -ENODEV;
366 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
369 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
371 /* count is managed under the mutex lock for the tty so cannot
372 drop to zero until after the last close completes */
373 WARN_ON(!port->port.count);
375 /* pass on to the driver specific version of this function */
376 retval = port->serial->type->write(tty, port, buf, count);
382 static int serial_write_room(struct tty_struct *tty)
384 struct usb_serial_port *port = tty->driver_data;
385 dbg("%s - port %d", __func__, port->number);
386 WARN_ON(!port->port.count);
387 /* pass on to the driver specific version of this function */
388 return port->serial->type->write_room(tty);
391 static int serial_chars_in_buffer(struct tty_struct *tty)
393 struct usb_serial_port *port = tty->driver_data;
394 dbg("%s = port %d", __func__, port->number);
396 WARN_ON(!port->port.count);
397 /* if the device was unplugged then any remaining characters
398 fell out of the connector ;) */
399 if (port->serial->disconnected)
401 /* pass on to the driver specific version of this function */
402 return port->serial->type->chars_in_buffer(tty);
405 static void serial_throttle(struct tty_struct *tty)
407 struct usb_serial_port *port = tty->driver_data;
408 dbg("%s - port %d", __func__, port->number);
410 WARN_ON(!port->port.count);
411 /* pass on to the driver specific version of this function */
412 if (port->serial->type->throttle)
413 port->serial->type->throttle(tty);
416 static void serial_unthrottle(struct tty_struct *tty)
418 struct usb_serial_port *port = tty->driver_data;
419 dbg("%s - port %d", __func__, port->number);
421 WARN_ON(!port->port.count);
422 /* pass on to the driver specific version of this function */
423 if (port->serial->type->unthrottle)
424 port->serial->type->unthrottle(tty);
427 static int serial_ioctl(struct tty_struct *tty, struct file *file,
428 unsigned int cmd, unsigned long arg)
430 struct usb_serial_port *port = tty->driver_data;
431 int retval = -ENODEV;
433 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
435 WARN_ON(!port->port.count);
437 /* pass on to the driver specific version of this function
438 if it is available */
439 if (port->serial->type->ioctl) {
440 retval = port->serial->type->ioctl(tty, file, cmd, arg);
442 retval = -ENOIOCTLCMD;
446 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
448 struct usb_serial_port *port = tty->driver_data;
449 dbg("%s - port %d", __func__, port->number);
451 WARN_ON(!port->port.count);
452 /* pass on to the driver specific version of this function
453 if it is available */
454 if (port->serial->type->set_termios)
455 port->serial->type->set_termios(tty, port, old);
457 tty_termios_copy_hw(tty->termios, old);
460 static int serial_break(struct tty_struct *tty, int break_state)
462 struct usb_serial_port *port = tty->driver_data;
464 dbg("%s - port %d", __func__, port->number);
466 WARN_ON(!port->port.count);
467 /* pass on to the driver specific version of this function
468 if it is available */
469 if (port->serial->type->break_ctl)
470 port->serial->type->break_ctl(tty, break_state);
474 static int serial_proc_show(struct seq_file *m, void *v)
476 struct usb_serial *serial;
481 seq_puts(m, "usbserinfo:1.0 driver:2.0\n");
482 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
483 serial = usb_serial_get_by_index(i);
487 seq_printf(m, "%d:", i);
488 if (serial->type->driver.owner)
489 seq_printf(m, " module:%s",
490 module_name(serial->type->driver.owner));
491 seq_printf(m, " name:\"%s\"",
492 serial->type->description);
493 seq_printf(m, " vendor:%04x product:%04x",
494 le16_to_cpu(serial->dev->descriptor.idVendor),
495 le16_to_cpu(serial->dev->descriptor.idProduct));
496 seq_printf(m, " num_ports:%d", serial->num_ports);
497 seq_printf(m, " port:%d", i - serial->minor + 1);
498 usb_make_path(serial->dev, tmp, sizeof(tmp));
499 seq_printf(m, " path:%s", tmp);
502 usb_serial_put(serial);
507 static int serial_proc_open(struct inode *inode, struct file *file)
509 return single_open(file, serial_proc_show, NULL);
512 static const struct file_operations serial_proc_fops = {
513 .owner = THIS_MODULE,
514 .open = serial_proc_open,
517 .release = single_release,
520 static int serial_tiocmget(struct tty_struct *tty, struct file *file)
522 struct usb_serial_port *port = tty->driver_data;
524 dbg("%s - port %d", __func__, port->number);
526 WARN_ON(!port->port.count);
527 if (port->serial->type->tiocmget)
528 return port->serial->type->tiocmget(tty, file);
532 static int serial_tiocmset(struct tty_struct *tty, struct file *file,
533 unsigned int set, unsigned int clear)
535 struct usb_serial_port *port = tty->driver_data;
537 dbg("%s - port %d", __func__, port->number);
539 WARN_ON(!port->port.count);
540 if (port->serial->type->tiocmset)
541 return port->serial->type->tiocmset(tty, file, set, clear);
546 * We would be calling tty_wakeup here, but unfortunately some line
547 * disciplines have an annoying habit of calling tty->write from
548 * the write wakeup callback (e.g. n_hdlc.c).
550 void usb_serial_port_softint(struct usb_serial_port *port)
552 schedule_work(&port->work);
554 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
556 static void usb_serial_port_work(struct work_struct *work)
558 struct usb_serial_port *port =
559 container_of(work, struct usb_serial_port, work);
560 struct tty_struct *tty;
562 dbg("%s - port %d", __func__, port->number);
564 tty = tty_port_tty_get(&port->port);
572 static void port_release(struct device *dev)
574 struct usb_serial_port *port = to_usb_serial_port(dev);
576 dbg ("%s - %s", __func__, dev_name(dev));
580 static void kill_traffic(struct usb_serial_port *port)
582 usb_kill_urb(port->read_urb);
583 usb_kill_urb(port->write_urb);
586 * Some drivers submit the read_urb in the
587 * handler for the write_urb or vice versa
588 * this order determines the order in which
589 * usb_kill_urb() must be used to reliably
590 * kill the URBs. As it is unknown here,
591 * both orders must be used in turn.
592 * The call below is not redundant.
594 usb_kill_urb(port->read_urb);
595 usb_kill_urb(port->interrupt_in_urb);
596 usb_kill_urb(port->interrupt_out_urb);
599 static void port_free(struct usb_serial_port *port)
602 * Stop all the traffic before cancelling the work, so that
603 * nobody will restart it by calling usb_serial_port_softint.
606 cancel_work_sync(&port->work);
608 usb_free_urb(port->read_urb);
609 usb_free_urb(port->write_urb);
610 usb_free_urb(port->interrupt_in_urb);
611 usb_free_urb(port->interrupt_out_urb);
612 kfree(port->bulk_in_buffer);
613 kfree(port->bulk_out_buffer);
614 kfree(port->interrupt_in_buffer);
615 kfree(port->interrupt_out_buffer);
619 static struct usb_serial *create_serial(struct usb_device *dev,
620 struct usb_interface *interface,
621 struct usb_serial_driver *driver)
623 struct usb_serial *serial;
625 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
627 dev_err(&dev->dev, "%s - out of memory\n", __func__);
630 serial->dev = usb_get_dev(dev);
631 serial->type = driver;
632 serial->interface = interface;
633 kref_init(&serial->kref);
634 mutex_init(&serial->disc_mutex);
635 serial->minor = SERIAL_TTY_NO_MINOR;
640 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
641 struct usb_serial_driver *drv)
643 struct usb_dynid *dynid;
645 spin_lock(&drv->dynids.lock);
646 list_for_each_entry(dynid, &drv->dynids.list, node) {
647 if (usb_match_one_id(intf, &dynid->id)) {
648 spin_unlock(&drv->dynids.lock);
652 spin_unlock(&drv->dynids.lock);
656 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
657 struct usb_interface *intf)
659 const struct usb_device_id *id;
661 id = usb_match_id(intf, drv->id_table);
663 dbg("static descriptor matches");
666 id = match_dynamic_id(intf, drv);
668 dbg("dynamic descriptor matches");
673 static struct usb_serial_driver *search_serial_device(
674 struct usb_interface *iface)
676 const struct usb_device_id *id;
677 struct usb_serial_driver *drv;
679 /* Check if the usb id matches a known device */
680 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
681 id = get_iface_id(drv, iface);
689 static int serial_carrier_raised(struct tty_port *port)
691 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
692 struct usb_serial_driver *drv = p->serial->type;
693 if (drv->carrier_raised)
694 return drv->carrier_raised(p);
695 /* No carrier control - don't block */
699 static void serial_dtr_rts(struct tty_port *port, int on)
701 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
702 struct usb_serial_driver *drv = p->serial->type;
707 static const struct tty_port_operations serial_port_ops = {
708 .carrier_raised = serial_carrier_raised,
709 .dtr_rts = serial_dtr_rts,
712 int usb_serial_probe(struct usb_interface *interface,
713 const struct usb_device_id *id)
715 struct usb_device *dev = interface_to_usbdev(interface);
716 struct usb_serial *serial = NULL;
717 struct usb_serial_port *port;
718 struct usb_host_interface *iface_desc;
719 struct usb_endpoint_descriptor *endpoint;
720 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
721 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
722 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
723 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
724 struct usb_serial_driver *type = NULL;
729 int num_interrupt_in = 0;
730 int num_interrupt_out = 0;
732 int num_bulk_out = 0;
736 lock_kernel(); /* guard against unloading a serial driver module */
737 type = search_serial_device(interface);
744 serial = create_serial(dev, interface, type);
747 dev_err(&interface->dev, "%s - out of memory\n", __func__);
751 /* if this device type has a probe function, call it */
753 const struct usb_device_id *id;
755 if (!try_module_get(type->driver.owner)) {
757 dev_err(&interface->dev,
758 "module get failed, exiting\n");
763 id = get_iface_id(type, interface);
764 retval = type->probe(serial, id);
765 module_put(type->driver.owner);
769 dbg("sub driver rejected device");
775 /* descriptor matches, let's find the endpoints needed */
776 /* check out the endpoints */
777 iface_desc = interface->cur_altsetting;
778 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
779 endpoint = &iface_desc->endpoint[i].desc;
781 if (usb_endpoint_is_bulk_in(endpoint)) {
782 /* we found a bulk in endpoint */
783 dbg("found bulk in on endpoint %d", i);
784 bulk_in_endpoint[num_bulk_in] = endpoint;
788 if (usb_endpoint_is_bulk_out(endpoint)) {
789 /* we found a bulk out endpoint */
790 dbg("found bulk out on endpoint %d", i);
791 bulk_out_endpoint[num_bulk_out] = endpoint;
795 if (usb_endpoint_is_int_in(endpoint)) {
796 /* we found a interrupt in endpoint */
797 dbg("found interrupt in on endpoint %d", i);
798 interrupt_in_endpoint[num_interrupt_in] = endpoint;
802 if (usb_endpoint_is_int_out(endpoint)) {
803 /* we found an interrupt out endpoint */
804 dbg("found interrupt out on endpoint %d", i);
805 interrupt_out_endpoint[num_interrupt_out] = endpoint;
810 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
811 /* BEGIN HORRIBLE HACK FOR PL2303 */
812 /* this is needed due to the looney way its endpoints are set up */
813 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
814 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
815 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
816 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
817 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
818 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
819 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
820 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
821 if (interface != dev->actconfig->interface[0]) {
822 /* check out the endpoints of the other interface*/
823 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
824 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
825 endpoint = &iface_desc->endpoint[i].desc;
826 if (usb_endpoint_is_int_in(endpoint)) {
827 /* we found a interrupt in endpoint */
828 dbg("found interrupt in for Prolific device on separate interface");
829 interrupt_in_endpoint[num_interrupt_in] = endpoint;
835 /* Now make sure the PL-2303 is configured correctly.
836 * If not, give up now and hope this hack will work
837 * properly during a later invocation of usb_serial_probe
839 if (num_bulk_in == 0 || num_bulk_out == 0) {
841 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
846 /* END HORRIBLE HACK FOR PL2303 */
849 #ifdef CONFIG_USB_SERIAL_GENERIC
850 if (type == &usb_serial_generic_device) {
851 num_ports = num_bulk_out;
852 if (num_ports == 0) {
854 dev_err(&interface->dev,
855 "Generic device with no bulk out, not allowed.\n");
862 /* if this device type has a calc_num_ports function, call it */
863 if (type->calc_num_ports) {
864 if (!try_module_get(type->driver.owner)) {
866 dev_err(&interface->dev,
867 "module get failed, exiting\n");
871 num_ports = type->calc_num_ports(serial);
872 module_put(type->driver.owner);
875 num_ports = type->num_ports;
878 serial->num_ports = num_ports;
879 serial->num_bulk_in = num_bulk_in;
880 serial->num_bulk_out = num_bulk_out;
881 serial->num_interrupt_in = num_interrupt_in;
882 serial->num_interrupt_out = num_interrupt_out;
884 /* found all that we need */
885 dev_info(&interface->dev, "%s converter detected\n",
888 /* create our ports, we need as many as the max endpoints */
889 /* we don't use num_ports here because some devices have more
890 endpoint pairs than ports */
891 max_endpoints = max(num_bulk_in, num_bulk_out);
892 max_endpoints = max(max_endpoints, num_interrupt_in);
893 max_endpoints = max(max_endpoints, num_interrupt_out);
894 max_endpoints = max(max_endpoints, (int)serial->num_ports);
895 serial->num_port_pointers = max_endpoints;
898 dbg("%s - setting up %d port structures for this device",
899 __func__, max_endpoints);
900 for (i = 0; i < max_endpoints; ++i) {
901 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
904 tty_port_init(&port->port);
905 port->port.ops = &serial_port_ops;
906 port->serial = serial;
907 spin_lock_init(&port->lock);
908 mutex_init(&port->mutex);
909 INIT_WORK(&port->work, usb_serial_port_work);
910 serial->port[i] = port;
913 /* set up the endpoint information */
914 for (i = 0; i < num_bulk_in; ++i) {
915 endpoint = bulk_in_endpoint[i];
916 port = serial->port[i];
917 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
918 if (!port->read_urb) {
919 dev_err(&interface->dev, "No free urbs available\n");
922 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
923 port->bulk_in_size = buffer_size;
924 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
925 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
926 if (!port->bulk_in_buffer) {
927 dev_err(&interface->dev,
928 "Couldn't allocate bulk_in_buffer\n");
931 usb_fill_bulk_urb(port->read_urb, dev,
933 endpoint->bEndpointAddress),
934 port->bulk_in_buffer, buffer_size,
935 serial->type->read_bulk_callback, port);
938 for (i = 0; i < num_bulk_out; ++i) {
939 endpoint = bulk_out_endpoint[i];
940 port = serial->port[i];
941 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
942 if (!port->write_urb) {
943 dev_err(&interface->dev, "No free urbs available\n");
946 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
947 port->bulk_out_size = buffer_size;
948 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
949 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
950 if (!port->bulk_out_buffer) {
951 dev_err(&interface->dev,
952 "Couldn't allocate bulk_out_buffer\n");
955 usb_fill_bulk_urb(port->write_urb, dev,
957 endpoint->bEndpointAddress),
958 port->bulk_out_buffer, buffer_size,
959 serial->type->write_bulk_callback, port);
962 if (serial->type->read_int_callback) {
963 for (i = 0; i < num_interrupt_in; ++i) {
964 endpoint = interrupt_in_endpoint[i];
965 port = serial->port[i];
966 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
967 if (!port->interrupt_in_urb) {
968 dev_err(&interface->dev,
969 "No free urbs available\n");
972 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
973 port->interrupt_in_endpointAddress =
974 endpoint->bEndpointAddress;
975 port->interrupt_in_buffer = kmalloc(buffer_size,
977 if (!port->interrupt_in_buffer) {
978 dev_err(&interface->dev,
979 "Couldn't allocate interrupt_in_buffer\n");
982 usb_fill_int_urb(port->interrupt_in_urb, dev,
984 endpoint->bEndpointAddress),
985 port->interrupt_in_buffer, buffer_size,
986 serial->type->read_int_callback, port,
987 endpoint->bInterval);
989 } else if (num_interrupt_in) {
990 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
993 if (serial->type->write_int_callback) {
994 for (i = 0; i < num_interrupt_out; ++i) {
995 endpoint = interrupt_out_endpoint[i];
996 port = serial->port[i];
997 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
998 if (!port->interrupt_out_urb) {
999 dev_err(&interface->dev,
1000 "No free urbs available\n");
1003 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1004 port->interrupt_out_size = buffer_size;
1005 port->interrupt_out_endpointAddress =
1006 endpoint->bEndpointAddress;
1007 port->interrupt_out_buffer = kmalloc(buffer_size,
1009 if (!port->interrupt_out_buffer) {
1010 dev_err(&interface->dev,
1011 "Couldn't allocate interrupt_out_buffer\n");
1014 usb_fill_int_urb(port->interrupt_out_urb, dev,
1016 endpoint->bEndpointAddress),
1017 port->interrupt_out_buffer, buffer_size,
1018 serial->type->write_int_callback, port,
1019 endpoint->bInterval);
1021 } else if (num_interrupt_out) {
1022 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
1025 /* if this device type has an attach function, call it */
1027 if (!try_module_get(type->driver.owner)) {
1028 dev_err(&interface->dev,
1029 "module get failed, exiting\n");
1032 retval = type->attach(serial);
1033 module_put(type->driver.owner);
1037 /* quietly accept this device, but don't bind to a
1038 serial port as it's about to disappear */
1039 serial->num_ports = 0;
1044 if (get_free_serial(serial, num_ports, &minor) == NULL) {
1045 dev_err(&interface->dev, "No more free serial devices\n");
1048 serial->minor = minor;
1050 /* register all of the individual ports with the driver core */
1051 for (i = 0; i < num_ports; ++i) {
1052 port = serial->port[i];
1053 port->dev.parent = &interface->dev;
1054 port->dev.driver = NULL;
1055 port->dev.bus = &usb_serial_bus_type;
1056 port->dev.release = &port_release;
1058 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1059 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1060 port->dev_state = PORT_REGISTERING;
1061 retval = device_register(&port->dev);
1063 dev_err(&port->dev, "Error registering port device, "
1065 port->dev_state = PORT_UNREGISTERED;
1067 port->dev_state = PORT_REGISTERED;
1071 usb_serial_console_init(debug, minor);
1075 usb_set_intfdata(interface, serial);
1079 for (i = 0; i < num_bulk_in; ++i) {
1080 port = serial->port[i];
1083 usb_free_urb(port->read_urb);
1084 kfree(port->bulk_in_buffer);
1086 for (i = 0; i < num_bulk_out; ++i) {
1087 port = serial->port[i];
1090 usb_free_urb(port->write_urb);
1091 kfree(port->bulk_out_buffer);
1093 for (i = 0; i < num_interrupt_in; ++i) {
1094 port = serial->port[i];
1097 usb_free_urb(port->interrupt_in_urb);
1098 kfree(port->interrupt_in_buffer);
1100 for (i = 0; i < num_interrupt_out; ++i) {
1101 port = serial->port[i];
1104 usb_free_urb(port->interrupt_out_urb);
1105 kfree(port->interrupt_out_buffer);
1108 /* free up any memory that we allocated */
1109 for (i = 0; i < serial->num_port_pointers; ++i)
1110 kfree(serial->port[i]);
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);
1137 /* The hangup will occur asynchronously but
1138 the object refcounts will sort out all the
1144 cancel_work_sync(&port->work);
1145 if (port->dev_state == PORT_REGISTERED) {
1147 /* Make sure the port is bound so that the
1148 * driver's port_remove method is called.
1150 if (!port->dev.driver) {
1154 &serial->type->driver;
1155 rc = device_bind_driver(&port->dev);
1157 port->dev_state = PORT_UNREGISTERING;
1158 device_del(&port->dev);
1159 port->dev_state = PORT_UNREGISTERED;
1163 serial->type->disconnect(serial);
1165 /* let the last holder of this object
1166 * cause it to be cleaned up */
1167 usb_serial_put(serial);
1168 dev_info(dev, "device disconnected\n");
1170 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1172 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1174 struct usb_serial *serial = usb_get_intfdata(intf);
1175 struct usb_serial_port *port;
1178 serial->suspending = 1;
1180 for (i = 0; i < serial->num_ports; ++i) {
1181 port = serial->port[i];
1186 if (serial->type->suspend)
1187 r = serial->type->suspend(serial, message);
1191 EXPORT_SYMBOL(usb_serial_suspend);
1193 int usb_serial_resume(struct usb_interface *intf)
1195 struct usb_serial *serial = usb_get_intfdata(intf);
1198 serial->suspending = 0;
1199 if (serial->type->resume)
1200 rv = serial->type->resume(serial);
1202 rv = usb_serial_generic_resume(serial);
1206 EXPORT_SYMBOL(usb_serial_resume);
1208 static const struct tty_operations serial_ops = {
1209 .open = serial_open,
1210 .close = serial_close,
1211 .write = serial_write,
1212 .hangup = serial_hangup,
1213 .write_room = serial_write_room,
1214 .ioctl = serial_ioctl,
1215 .set_termios = serial_set_termios,
1216 .throttle = serial_throttle,
1217 .unthrottle = serial_unthrottle,
1218 .break_ctl = serial_break,
1219 .chars_in_buffer = serial_chars_in_buffer,
1220 .tiocmget = serial_tiocmget,
1221 .tiocmset = serial_tiocmset,
1222 .proc_fops = &serial_proc_fops,
1226 struct tty_driver *usb_serial_tty_driver;
1228 static int __init usb_serial_init(void)
1233 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1234 if (!usb_serial_tty_driver)
1237 /* Initialize our global data */
1238 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1239 serial_table[i] = NULL;
1241 result = bus_register(&usb_serial_bus_type);
1243 printk(KERN_ERR "usb-serial: %s - registering bus driver "
1244 "failed\n", __func__);
1248 usb_serial_tty_driver->owner = THIS_MODULE;
1249 usb_serial_tty_driver->driver_name = "usbserial";
1250 usb_serial_tty_driver->name = "ttyUSB";
1251 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1252 usb_serial_tty_driver->minor_start = 0;
1253 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1254 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1255 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1256 TTY_DRIVER_DYNAMIC_DEV;
1257 usb_serial_tty_driver->init_termios = tty_std_termios;
1258 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1260 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1261 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1262 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1263 result = tty_register_driver(usb_serial_tty_driver);
1265 printk(KERN_ERR "usb-serial: %s - tty_register_driver failed\n",
1267 goto exit_reg_driver;
1270 /* register the USB driver */
1271 result = usb_register(&usb_serial_driver);
1273 printk(KERN_ERR "usb-serial: %s - usb_register failed\n",
1278 /* register the generic driver, if we should */
1279 result = usb_serial_generic_register(debug);
1281 printk(KERN_ERR "usb-serial: %s - registering generic "
1282 "driver failed\n", __func__);
1286 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1291 usb_deregister(&usb_serial_driver);
1294 tty_unregister_driver(usb_serial_tty_driver);
1297 bus_unregister(&usb_serial_bus_type);
1300 printk(KERN_ERR "usb-serial: %s - returning with error %d\n",
1302 put_tty_driver(usb_serial_tty_driver);
1307 static void __exit usb_serial_exit(void)
1309 usb_serial_console_exit();
1311 usb_serial_generic_deregister();
1313 usb_deregister(&usb_serial_driver);
1314 tty_unregister_driver(usb_serial_tty_driver);
1315 put_tty_driver(usb_serial_tty_driver);
1316 bus_unregister(&usb_serial_bus_type);
1320 module_init(usb_serial_init);
1321 module_exit(usb_serial_exit);
1323 #define set_to_generic_if_null(type, function) \
1325 if (!type->function) { \
1326 type->function = usb_serial_generic_##function; \
1327 dbg("Had to override the " #function \
1328 " usb serial operation with the generic one.");\
1332 static void fixup_generic(struct usb_serial_driver *device)
1334 set_to_generic_if_null(device, open);
1335 set_to_generic_if_null(device, write);
1336 set_to_generic_if_null(device, close);
1337 set_to_generic_if_null(device, write_room);
1338 set_to_generic_if_null(device, chars_in_buffer);
1339 set_to_generic_if_null(device, read_bulk_callback);
1340 set_to_generic_if_null(device, write_bulk_callback);
1341 set_to_generic_if_null(device, disconnect);
1342 set_to_generic_if_null(device, release);
1345 int usb_serial_register(struct usb_serial_driver *driver)
1347 /* must be called with BKL held */
1353 fixup_generic(driver);
1355 if (!driver->description)
1356 driver->description = driver->driver.name;
1358 /* Add this device to our list of devices */
1359 list_add(&driver->driver_list, &usb_serial_driver_list);
1361 retval = usb_serial_bus_register(driver);
1363 printk(KERN_ERR "usb-serial: problem %d when registering "
1364 "driver %s\n", retval, driver->description);
1365 list_del(&driver->driver_list);
1367 printk(KERN_INFO "USB Serial support registered for %s\n",
1368 driver->description);
1372 EXPORT_SYMBOL_GPL(usb_serial_register);
1375 void usb_serial_deregister(struct usb_serial_driver *device)
1377 /* must be called with BKL held */
1378 printk(KERN_INFO "USB Serial deregistering driver %s\n",
1379 device->description);
1380 list_del(&device->driver_list);
1381 usb_serial_bus_deregister(device);
1383 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1385 /* Module information */
1386 MODULE_AUTHOR(DRIVER_AUTHOR);
1387 MODULE_DESCRIPTION(DRIVER_DESC);
1388 MODULE_LICENSE("GPL");
1390 module_param(debug, bool, S_IRUGO | S_IWUSR);
1391 MODULE_PARM_DESC(debug, "Debug enabled or not");