USB: serial: fix lifetime and locking problems
[linux/fpc-iii.git] / drivers / usb / serial / usb-serial.c
blob9fcc272c147eb5dd90472a8fd4c766912b3476e0
1 /*
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
16 * driver
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/tty.h>
25 #include <linux/tty_driver.h>
26 #include <linux/tty_flip.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/spinlock.h>
30 #include <linux/mutex.h>
31 #include <linux/list.h>
32 #include <linux/uaccess.h>
33 #include <linux/usb.h>
34 #include <linux/usb/serial.h>
35 #include "pl2303.h"
38 * Version Information
40 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
41 #define DRIVER_DESC "USB Serial Driver core"
43 static void port_free(struct usb_serial_port *port);
45 /* Driver structure we register with the USB core */
46 static struct usb_driver usb_serial_driver = {
47 .name = "usbserial",
48 .probe = usb_serial_probe,
49 .disconnect = usb_serial_disconnect,
50 .suspend = usb_serial_suspend,
51 .resume = usb_serial_resume,
52 .no_dynamic_id = 1,
55 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
56 the MODULE_DEVICE_TABLE declarations in each serial driver
57 cause the "hotplug" program to pull in whatever module is necessary
58 via modprobe, and modprobe will load usbserial because the serial
59 drivers depend on it.
62 static int debug;
63 /* initially all NULL */
64 static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
65 static DEFINE_MUTEX(table_lock);
66 static LIST_HEAD(usb_serial_driver_list);
68 struct usb_serial *usb_serial_get_by_index(unsigned index)
70 struct usb_serial *serial;
72 mutex_lock(&table_lock);
73 serial = serial_table[index];
75 if (serial)
76 kref_get(&serial->kref);
77 mutex_unlock(&table_lock);
78 return serial;
81 static struct usb_serial *get_free_serial(struct usb_serial *serial,
82 int num_ports, unsigned int *minor)
84 unsigned int i, j;
85 int good_spot;
87 dbg("%s %d", __func__, num_ports);
89 *minor = 0;
90 mutex_lock(&table_lock);
91 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
92 if (serial_table[i])
93 continue;
95 good_spot = 1;
96 for (j = 1; j <= num_ports-1; ++j)
97 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
98 good_spot = 0;
99 i += j;
100 break;
102 if (good_spot == 0)
103 continue;
105 *minor = i;
106 j = 0;
107 dbg("%s - minor base = %d", __func__, *minor);
108 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
109 serial_table[i] = serial;
110 serial->port[j++]->number = i;
112 mutex_unlock(&table_lock);
113 return serial;
115 mutex_unlock(&table_lock);
116 return NULL;
119 static void return_serial(struct usb_serial *serial)
121 int i;
123 dbg("%s", __func__);
125 for (i = 0; i < serial->num_ports; ++i)
126 serial_table[serial->minor + i] = NULL;
129 static void destroy_serial(struct kref *kref)
131 struct usb_serial *serial;
132 struct usb_serial_port *port;
133 int i;
135 serial = to_usb_serial(kref);
137 dbg("%s - %s", __func__, serial->type->description);
139 /* return the minor range that this device had */
140 if (serial->minor != SERIAL_TTY_NO_MINOR)
141 return_serial(serial);
143 /* If this is a "fake" port, we have to clean it up here, as it will
144 * not get cleaned up in port_release() as it was never registered with
145 * the driver core */
146 if (serial->num_ports < serial->num_port_pointers) {
147 for (i = serial->num_ports;
148 i < serial->num_port_pointers; ++i) {
149 port = serial->port[i];
150 if (!port)
151 continue;
152 port_free(port);
156 usb_put_dev(serial->dev);
158 /* free up any memory that we allocated */
159 kfree(serial);
162 void usb_serial_put(struct usb_serial *serial)
164 mutex_lock(&table_lock);
165 kref_put(&serial->kref, destroy_serial);
166 mutex_unlock(&table_lock);
169 /*****************************************************************************
170 * Driver tty interface functions
171 *****************************************************************************/
172 static int serial_open (struct tty_struct *tty, struct file *filp)
174 struct usb_serial *serial;
175 struct usb_serial_port *port;
176 unsigned int portNumber;
177 int retval = 0;
179 dbg("%s", __func__);
181 /* get the serial object associated with this tty pointer */
182 serial = usb_serial_get_by_index(tty->index);
183 if (!serial) {
184 tty->driver_data = NULL;
185 return -ENODEV;
188 mutex_lock(&serial->disc_mutex);
189 portNumber = tty->index - serial->minor;
190 port = serial->port[portNumber];
191 if (!port || serial->disconnected)
192 retval = -ENODEV;
193 else
194 get_device(&port->dev);
196 * Note: Our locking order requirement does not allow port->mutex
197 * to be acquired while serial->disc_mutex is held.
199 mutex_unlock(&serial->disc_mutex);
200 if (retval)
201 goto bailout_serial_put;
203 if (mutex_lock_interruptible(&port->mutex)) {
204 retval = -ERESTARTSYS;
205 goto bailout_port_put;
208 ++port->port.count;
210 /* set up our port structure making the tty driver
211 * remember our port object, and us it */
212 tty->driver_data = port;
213 port->port.tty = tty;
215 if (port->port.count == 1) {
217 /* lock this module before we call it
218 * this may fail, which means we must bail out,
219 * safe because we are called with BKL held */
220 if (!try_module_get(serial->type->driver.owner)) {
221 retval = -ENODEV;
222 goto bailout_mutex_unlock;
225 mutex_lock(&serial->disc_mutex);
226 if (serial->disconnected)
227 retval = -ENODEV;
228 else
229 retval = usb_autopm_get_interface(serial->interface);
230 if (retval)
231 goto bailout_module_put;
233 /* only call the device specific open if this
234 * is the first time the port is opened */
235 retval = serial->type->open(tty, port, filp);
236 if (retval)
237 goto bailout_interface_put;
238 mutex_unlock(&serial->disc_mutex);
241 mutex_unlock(&port->mutex);
242 return 0;
244 bailout_interface_put:
245 usb_autopm_put_interface(serial->interface);
246 bailout_module_put:
247 mutex_unlock(&serial->disc_mutex);
248 module_put(serial->type->driver.owner);
249 bailout_mutex_unlock:
250 port->port.count = 0;
251 tty->driver_data = NULL;
252 port->port.tty = NULL;
253 mutex_unlock(&port->mutex);
254 bailout_port_put:
255 put_device(&port->dev);
256 bailout_serial_put:
257 usb_serial_put(serial);
258 return retval;
261 static void serial_close(struct tty_struct *tty, struct file *filp)
263 struct usb_serial_port *port = tty->driver_data;
264 struct usb_serial *serial;
265 struct module *owner;
266 int count;
268 if (!port)
269 return;
271 dbg("%s - port %d", __func__, port->number);
273 mutex_lock(&port->mutex);
274 serial = port->serial;
275 owner = serial->type->driver.owner;
277 if (port->port.count == 0) {
278 mutex_unlock(&port->mutex);
279 return;
282 --port->port.count;
283 if (port->port.count == 0)
284 /* only call the device specific close if this
285 * port is being closed by the last owner */
286 serial->type->close(tty, port, filp);
288 if (port->port.count == (port->console? 1 : 0)) {
289 if (port->port.tty) {
290 if (port->port.tty->driver_data)
291 port->port.tty->driver_data = NULL;
292 port->port.tty = NULL;
296 count = port->port.count;
297 mutex_unlock(&port->mutex);
298 put_device(&port->dev);
300 /* Mustn't dereference port any more */
301 if (count == 0) {
302 mutex_lock(&serial->disc_mutex);
303 if (!serial->disconnected)
304 usb_autopm_put_interface(serial->interface);
305 mutex_unlock(&serial->disc_mutex);
307 usb_serial_put(serial);
309 /* Mustn't dereference serial any more */
310 if (count == 0)
311 module_put(owner);
314 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
315 int count)
317 struct usb_serial_port *port = tty->driver_data;
318 int retval = -ENODEV;
320 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
321 goto exit;
323 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
325 /* count is managed under the mutex lock for the tty so cannot
326 drop to zero until after the last close completes */
327 WARN_ON(!port->port.count);
329 /* pass on to the driver specific version of this function */
330 retval = port->serial->type->write(tty, port, buf, count);
332 exit:
333 return retval;
336 static int serial_write_room(struct tty_struct *tty)
338 struct usb_serial_port *port = tty->driver_data;
339 dbg("%s - port %d", __func__, port->number);
340 WARN_ON(!port->port.count);
341 /* pass on to the driver specific version of this function */
342 return port->serial->type->write_room(tty);
345 static int serial_chars_in_buffer(struct tty_struct *tty)
347 struct usb_serial_port *port = tty->driver_data;
348 dbg("%s = port %d", __func__, port->number);
350 WARN_ON(!port->port.count);
351 /* pass on to the driver specific version of this function */
352 return port->serial->type->chars_in_buffer(tty);
355 static void serial_throttle(struct tty_struct *tty)
357 struct usb_serial_port *port = tty->driver_data;
358 dbg("%s - port %d", __func__, port->number);
360 WARN_ON(!port->port.count);
361 /* pass on to the driver specific version of this function */
362 if (port->serial->type->throttle)
363 port->serial->type->throttle(tty);
366 static void serial_unthrottle(struct tty_struct *tty)
368 struct usb_serial_port *port = tty->driver_data;
369 dbg("%s - port %d", __func__, port->number);
371 WARN_ON(!port->port.count);
372 /* pass on to the driver specific version of this function */
373 if (port->serial->type->unthrottle)
374 port->serial->type->unthrottle(tty);
377 static int serial_ioctl(struct tty_struct *tty, struct file *file,
378 unsigned int cmd, unsigned long arg)
380 struct usb_serial_port *port = tty->driver_data;
381 int retval = -ENODEV;
383 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
385 WARN_ON(!port->port.count);
387 /* pass on to the driver specific version of this function
388 if it is available */
389 if (port->serial->type->ioctl) {
390 lock_kernel();
391 retval = port->serial->type->ioctl(tty, file, cmd, arg);
392 unlock_kernel();
393 } else
394 retval = -ENOIOCTLCMD;
395 return retval;
398 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
400 struct usb_serial_port *port = tty->driver_data;
401 dbg("%s - port %d", __func__, port->number);
403 WARN_ON(!port->port.count);
404 /* pass on to the driver specific version of this function
405 if it is available */
406 if (port->serial->type->set_termios)
407 port->serial->type->set_termios(tty, port, old);
408 else
409 tty_termios_copy_hw(tty->termios, old);
412 static int serial_break(struct tty_struct *tty, int break_state)
414 struct usb_serial_port *port = tty->driver_data;
416 dbg("%s - port %d", __func__, port->number);
418 WARN_ON(!port->port.count);
419 /* pass on to the driver specific version of this function
420 if it is available */
421 if (port->serial->type->break_ctl) {
422 lock_kernel();
423 port->serial->type->break_ctl(tty, break_state);
424 unlock_kernel();
426 return 0;
429 static int serial_read_proc(char *page, char **start, off_t off, int count,
430 int *eof, void *data)
432 struct usb_serial *serial;
433 int length = 0;
434 int i;
435 off_t begin = 0;
436 char tmp[40];
438 dbg("%s", __func__);
439 length += sprintf(page, "usbserinfo:1.0 driver:2.0\n");
440 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
441 serial = usb_serial_get_by_index(i);
442 if (serial == NULL)
443 continue;
445 length += sprintf(page+length, "%d:", i);
446 if (serial->type->driver.owner)
447 length += sprintf(page+length, " module:%s",
448 module_name(serial->type->driver.owner));
449 length += sprintf(page+length, " name:\"%s\"",
450 serial->type->description);
451 length += sprintf(page+length, " vendor:%04x product:%04x",
452 le16_to_cpu(serial->dev->descriptor.idVendor),
453 le16_to_cpu(serial->dev->descriptor.idProduct));
454 length += sprintf(page+length, " num_ports:%d",
455 serial->num_ports);
456 length += sprintf(page+length, " port:%d",
457 i - serial->minor + 1);
458 usb_make_path(serial->dev, tmp, sizeof(tmp));
459 length += sprintf(page+length, " path:%s", tmp);
461 length += sprintf(page+length, "\n");
462 if ((length + begin) > (off + count)) {
463 usb_serial_put(serial);
464 goto done;
466 if ((length + begin) < off) {
467 begin += length;
468 length = 0;
470 usb_serial_put(serial);
472 *eof = 1;
473 done:
474 if (off >= (length + begin))
475 return 0;
476 *start = page + (off-begin);
477 return (count < begin+length-off) ? count : begin+length-off;
480 static int serial_tiocmget(struct tty_struct *tty, struct file *file)
482 struct usb_serial_port *port = tty->driver_data;
484 dbg("%s - port %d", __func__, port->number);
486 WARN_ON(!port->port.count);
487 if (port->serial->type->tiocmget)
488 return port->serial->type->tiocmget(tty, file);
489 return -EINVAL;
492 static int serial_tiocmset(struct tty_struct *tty, struct file *file,
493 unsigned int set, unsigned int clear)
495 struct usb_serial_port *port = tty->driver_data;
497 dbg("%s - port %d", __func__, port->number);
499 WARN_ON(!port->port.count);
500 if (port->serial->type->tiocmset)
501 return port->serial->type->tiocmset(tty, file, set, clear);
502 return -EINVAL;
506 * We would be calling tty_wakeup here, but unfortunately some line
507 * disciplines have an annoying habit of calling tty->write from
508 * the write wakeup callback (e.g. n_hdlc.c).
510 void usb_serial_port_softint(struct usb_serial_port *port)
512 schedule_work(&port->work);
514 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
516 static void usb_serial_port_work(struct work_struct *work)
518 struct usb_serial_port *port =
519 container_of(work, struct usb_serial_port, work);
520 struct tty_struct *tty;
522 dbg("%s - port %d", __func__, port->number);
524 if (!port)
525 return;
527 tty = port->port.tty;
528 if (!tty)
529 return;
531 tty_wakeup(tty);
534 static void port_release(struct device *dev)
536 struct usb_serial_port *port = to_usb_serial_port(dev);
538 dbg ("%s - %s", __func__, dev_name(dev));
539 port_free(port);
542 static void kill_traffic(struct usb_serial_port *port)
544 usb_kill_urb(port->read_urb);
545 usb_kill_urb(port->write_urb);
547 * This is tricky.
548 * Some drivers submit the read_urb in the
549 * handler for the write_urb or vice versa
550 * this order determines the order in which
551 * usb_kill_urb() must be used to reliably
552 * kill the URBs. As it is unknown here,
553 * both orders must be used in turn.
554 * The call below is not redundant.
556 usb_kill_urb(port->read_urb);
557 usb_kill_urb(port->interrupt_in_urb);
558 usb_kill_urb(port->interrupt_out_urb);
561 static void port_free(struct usb_serial_port *port)
564 * Stop all the traffic before cancelling the work, so that
565 * nobody will restart it by calling usb_serial_port_softint.
567 kill_traffic(port);
568 cancel_work_sync(&port->work);
570 usb_free_urb(port->read_urb);
571 usb_free_urb(port->write_urb);
572 usb_free_urb(port->interrupt_in_urb);
573 usb_free_urb(port->interrupt_out_urb);
574 kfree(port->bulk_in_buffer);
575 kfree(port->bulk_out_buffer);
576 kfree(port->interrupt_in_buffer);
577 kfree(port->interrupt_out_buffer);
578 kfree(port);
581 static struct usb_serial *create_serial(struct usb_device *dev,
582 struct usb_interface *interface,
583 struct usb_serial_driver *driver)
585 struct usb_serial *serial;
587 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
588 if (!serial) {
589 dev_err(&dev->dev, "%s - out of memory\n", __func__);
590 return NULL;
592 serial->dev = usb_get_dev(dev);
593 serial->type = driver;
594 serial->interface = interface;
595 kref_init(&serial->kref);
596 mutex_init(&serial->disc_mutex);
597 serial->minor = SERIAL_TTY_NO_MINOR;
599 return serial;
602 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
603 struct usb_serial_driver *drv)
605 struct usb_dynid *dynid;
607 spin_lock(&drv->dynids.lock);
608 list_for_each_entry(dynid, &drv->dynids.list, node) {
609 if (usb_match_one_id(intf, &dynid->id)) {
610 spin_unlock(&drv->dynids.lock);
611 return &dynid->id;
614 spin_unlock(&drv->dynids.lock);
615 return NULL;
618 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
619 struct usb_interface *intf)
621 const struct usb_device_id *id;
623 id = usb_match_id(intf, drv->id_table);
624 if (id) {
625 dbg("static descriptor matches");
626 goto exit;
628 id = match_dynamic_id(intf, drv);
629 if (id)
630 dbg("dynamic descriptor matches");
631 exit:
632 return id;
635 static struct usb_serial_driver *search_serial_device(
636 struct usb_interface *iface)
638 const struct usb_device_id *id;
639 struct usb_serial_driver *drv;
641 /* Check if the usb id matches a known device */
642 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
643 id = get_iface_id(drv, iface);
644 if (id)
645 return drv;
648 return NULL;
651 int usb_serial_probe(struct usb_interface *interface,
652 const struct usb_device_id *id)
654 struct usb_device *dev = interface_to_usbdev(interface);
655 struct usb_serial *serial = NULL;
656 struct usb_serial_port *port;
657 struct usb_host_interface *iface_desc;
658 struct usb_endpoint_descriptor *endpoint;
659 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
660 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
661 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
662 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
663 struct usb_serial_driver *type = NULL;
664 int retval;
665 unsigned int minor;
666 int buffer_size;
667 int i;
668 int num_interrupt_in = 0;
669 int num_interrupt_out = 0;
670 int num_bulk_in = 0;
671 int num_bulk_out = 0;
672 int num_ports = 0;
673 int max_endpoints;
675 lock_kernel(); /* guard against unloading a serial driver module */
676 type = search_serial_device(interface);
677 if (!type) {
678 unlock_kernel();
679 dbg("none matched");
680 return -ENODEV;
683 serial = create_serial(dev, interface, type);
684 if (!serial) {
685 unlock_kernel();
686 dev_err(&interface->dev, "%s - out of memory\n", __func__);
687 return -ENOMEM;
690 /* if this device type has a probe function, call it */
691 if (type->probe) {
692 const struct usb_device_id *id;
694 if (!try_module_get(type->driver.owner)) {
695 unlock_kernel();
696 dev_err(&interface->dev,
697 "module get failed, exiting\n");
698 kfree(serial);
699 return -EIO;
702 id = get_iface_id(type, interface);
703 retval = type->probe(serial, id);
704 module_put(type->driver.owner);
706 if (retval) {
707 unlock_kernel();
708 dbg("sub driver rejected device");
709 kfree(serial);
710 return retval;
714 /* descriptor matches, let's find the endpoints needed */
715 /* check out the endpoints */
716 iface_desc = interface->cur_altsetting;
717 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
718 endpoint = &iface_desc->endpoint[i].desc;
720 if (usb_endpoint_is_bulk_in(endpoint)) {
721 /* we found a bulk in endpoint */
722 dbg("found bulk in on endpoint %d", i);
723 bulk_in_endpoint[num_bulk_in] = endpoint;
724 ++num_bulk_in;
727 if (usb_endpoint_is_bulk_out(endpoint)) {
728 /* we found a bulk out endpoint */
729 dbg("found bulk out on endpoint %d", i);
730 bulk_out_endpoint[num_bulk_out] = endpoint;
731 ++num_bulk_out;
734 if (usb_endpoint_is_int_in(endpoint)) {
735 /* we found a interrupt in endpoint */
736 dbg("found interrupt in on endpoint %d", i);
737 interrupt_in_endpoint[num_interrupt_in] = endpoint;
738 ++num_interrupt_in;
741 if (usb_endpoint_is_int_out(endpoint)) {
742 /* we found an interrupt out endpoint */
743 dbg("found interrupt out on endpoint %d", i);
744 interrupt_out_endpoint[num_interrupt_out] = endpoint;
745 ++num_interrupt_out;
749 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
750 /* BEGIN HORRIBLE HACK FOR PL2303 */
751 /* this is needed due to the looney way its endpoints are set up */
752 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
753 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
754 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
755 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
756 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
757 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
758 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
759 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
760 if (interface != dev->actconfig->interface[0]) {
761 /* check out the endpoints of the other interface*/
762 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
763 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
764 endpoint = &iface_desc->endpoint[i].desc;
765 if (usb_endpoint_is_int_in(endpoint)) {
766 /* we found a interrupt in endpoint */
767 dbg("found interrupt in for Prolific device on separate interface");
768 interrupt_in_endpoint[num_interrupt_in] = endpoint;
769 ++num_interrupt_in;
774 /* Now make sure the PL-2303 is configured correctly.
775 * If not, give up now and hope this hack will work
776 * properly during a later invocation of usb_serial_probe
778 if (num_bulk_in == 0 || num_bulk_out == 0) {
779 unlock_kernel();
780 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
781 kfree(serial);
782 return -ENODEV;
785 /* END HORRIBLE HACK FOR PL2303 */
786 #endif
788 #ifdef CONFIG_USB_SERIAL_GENERIC
789 if (type == &usb_serial_generic_device) {
790 num_ports = num_bulk_out;
791 if (num_ports == 0) {
792 unlock_kernel();
793 dev_err(&interface->dev,
794 "Generic device with no bulk out, not allowed.\n");
795 kfree(serial);
796 return -EIO;
799 #endif
800 if (!num_ports) {
801 /* if this device type has a calc_num_ports function, call it */
802 if (type->calc_num_ports) {
803 if (!try_module_get(type->driver.owner)) {
804 unlock_kernel();
805 dev_err(&interface->dev,
806 "module get failed, exiting\n");
807 kfree(serial);
808 return -EIO;
810 num_ports = type->calc_num_ports(serial);
811 module_put(type->driver.owner);
813 if (!num_ports)
814 num_ports = type->num_ports;
817 serial->num_ports = num_ports;
818 serial->num_bulk_in = num_bulk_in;
819 serial->num_bulk_out = num_bulk_out;
820 serial->num_interrupt_in = num_interrupt_in;
821 serial->num_interrupt_out = num_interrupt_out;
823 /* found all that we need */
824 dev_info(&interface->dev, "%s converter detected\n",
825 type->description);
827 /* create our ports, we need as many as the max endpoints */
828 /* we don't use num_ports here because some devices have more
829 endpoint pairs than ports */
830 max_endpoints = max(num_bulk_in, num_bulk_out);
831 max_endpoints = max(max_endpoints, num_interrupt_in);
832 max_endpoints = max(max_endpoints, num_interrupt_out);
833 max_endpoints = max(max_endpoints, (int)serial->num_ports);
834 serial->num_port_pointers = max_endpoints;
835 unlock_kernel();
837 dbg("%s - setting up %d port structures for this device",
838 __func__, max_endpoints);
839 for (i = 0; i < max_endpoints; ++i) {
840 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
841 if (!port)
842 goto probe_error;
843 port->serial = serial;
844 spin_lock_init(&port->lock);
845 mutex_init(&port->mutex);
846 INIT_WORK(&port->work, usb_serial_port_work);
847 serial->port[i] = port;
850 /* set up the endpoint information */
851 for (i = 0; i < num_bulk_in; ++i) {
852 endpoint = bulk_in_endpoint[i];
853 port = serial->port[i];
854 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
855 if (!port->read_urb) {
856 dev_err(&interface->dev, "No free urbs available\n");
857 goto probe_error;
859 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
860 port->bulk_in_size = buffer_size;
861 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
862 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
863 if (!port->bulk_in_buffer) {
864 dev_err(&interface->dev,
865 "Couldn't allocate bulk_in_buffer\n");
866 goto probe_error;
868 usb_fill_bulk_urb(port->read_urb, dev,
869 usb_rcvbulkpipe(dev,
870 endpoint->bEndpointAddress),
871 port->bulk_in_buffer, buffer_size,
872 serial->type->read_bulk_callback, port);
875 for (i = 0; i < num_bulk_out; ++i) {
876 endpoint = bulk_out_endpoint[i];
877 port = serial->port[i];
878 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
879 if (!port->write_urb) {
880 dev_err(&interface->dev, "No free urbs available\n");
881 goto probe_error;
883 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
884 port->bulk_out_size = buffer_size;
885 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
886 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
887 if (!port->bulk_out_buffer) {
888 dev_err(&interface->dev,
889 "Couldn't allocate bulk_out_buffer\n");
890 goto probe_error;
892 usb_fill_bulk_urb(port->write_urb, dev,
893 usb_sndbulkpipe(dev,
894 endpoint->bEndpointAddress),
895 port->bulk_out_buffer, buffer_size,
896 serial->type->write_bulk_callback, port);
899 if (serial->type->read_int_callback) {
900 for (i = 0; i < num_interrupt_in; ++i) {
901 endpoint = interrupt_in_endpoint[i];
902 port = serial->port[i];
903 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
904 if (!port->interrupt_in_urb) {
905 dev_err(&interface->dev,
906 "No free urbs available\n");
907 goto probe_error;
909 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
910 port->interrupt_in_endpointAddress =
911 endpoint->bEndpointAddress;
912 port->interrupt_in_buffer = kmalloc(buffer_size,
913 GFP_KERNEL);
914 if (!port->interrupt_in_buffer) {
915 dev_err(&interface->dev,
916 "Couldn't allocate interrupt_in_buffer\n");
917 goto probe_error;
919 usb_fill_int_urb(port->interrupt_in_urb, dev,
920 usb_rcvintpipe(dev,
921 endpoint->bEndpointAddress),
922 port->interrupt_in_buffer, buffer_size,
923 serial->type->read_int_callback, port,
924 endpoint->bInterval);
926 } else if (num_interrupt_in) {
927 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
930 if (serial->type->write_int_callback) {
931 for (i = 0; i < num_interrupt_out; ++i) {
932 endpoint = interrupt_out_endpoint[i];
933 port = serial->port[i];
934 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
935 if (!port->interrupt_out_urb) {
936 dev_err(&interface->dev,
937 "No free urbs available\n");
938 goto probe_error;
940 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
941 port->interrupt_out_size = buffer_size;
942 port->interrupt_out_endpointAddress =
943 endpoint->bEndpointAddress;
944 port->interrupt_out_buffer = kmalloc(buffer_size,
945 GFP_KERNEL);
946 if (!port->interrupt_out_buffer) {
947 dev_err(&interface->dev,
948 "Couldn't allocate interrupt_out_buffer\n");
949 goto probe_error;
951 usb_fill_int_urb(port->interrupt_out_urb, dev,
952 usb_sndintpipe(dev,
953 endpoint->bEndpointAddress),
954 port->interrupt_out_buffer, buffer_size,
955 serial->type->write_int_callback, port,
956 endpoint->bInterval);
958 } else if (num_interrupt_out) {
959 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
962 /* if this device type has an attach function, call it */
963 if (type->attach) {
964 if (!try_module_get(type->driver.owner)) {
965 dev_err(&interface->dev,
966 "module get failed, exiting\n");
967 goto probe_error;
969 retval = type->attach(serial);
970 module_put(type->driver.owner);
971 if (retval < 0)
972 goto probe_error;
973 if (retval > 0) {
974 /* quietly accept this device, but don't bind to a
975 serial port as it's about to disappear */
976 goto exit;
980 if (get_free_serial(serial, num_ports, &minor) == NULL) {
981 dev_err(&interface->dev, "No more free serial devices\n");
982 goto probe_error;
984 serial->minor = minor;
986 /* register all of the individual ports with the driver core */
987 for (i = 0; i < num_ports; ++i) {
988 port = serial->port[i];
989 port->dev.parent = &interface->dev;
990 port->dev.driver = NULL;
991 port->dev.bus = &usb_serial_bus_type;
992 port->dev.release = &port_release;
994 dev_set_name(&port->dev, "ttyUSB%d", port->number);
995 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
996 retval = device_register(&port->dev);
997 if (retval)
998 dev_err(&port->dev, "Error registering port device, "
999 "continuing\n");
1002 usb_serial_console_init(debug, minor);
1004 exit:
1005 /* success */
1006 usb_set_intfdata(interface, serial);
1007 return 0;
1009 probe_error:
1010 for (i = 0; i < num_bulk_in; ++i) {
1011 port = serial->port[i];
1012 if (!port)
1013 continue;
1014 usb_free_urb(port->read_urb);
1015 kfree(port->bulk_in_buffer);
1017 for (i = 0; i < num_bulk_out; ++i) {
1018 port = serial->port[i];
1019 if (!port)
1020 continue;
1021 usb_free_urb(port->write_urb);
1022 kfree(port->bulk_out_buffer);
1024 for (i = 0; i < num_interrupt_in; ++i) {
1025 port = serial->port[i];
1026 if (!port)
1027 continue;
1028 usb_free_urb(port->interrupt_in_urb);
1029 kfree(port->interrupt_in_buffer);
1031 for (i = 0; i < num_interrupt_out; ++i) {
1032 port = serial->port[i];
1033 if (!port)
1034 continue;
1035 usb_free_urb(port->interrupt_out_urb);
1036 kfree(port->interrupt_out_buffer);
1039 /* free up any memory that we allocated */
1040 for (i = 0; i < serial->num_port_pointers; ++i)
1041 kfree(serial->port[i]);
1042 kfree(serial);
1043 return -EIO;
1045 EXPORT_SYMBOL_GPL(usb_serial_probe);
1047 void usb_serial_disconnect(struct usb_interface *interface)
1049 int i;
1050 struct usb_serial *serial = usb_get_intfdata(interface);
1051 struct device *dev = &interface->dev;
1052 struct usb_serial_port *port;
1054 usb_serial_console_disconnect(serial);
1055 dbg("%s", __func__);
1057 mutex_lock(&serial->disc_mutex);
1058 usb_set_intfdata(interface, NULL);
1059 /* must set a flag, to signal subdrivers */
1060 serial->disconnected = 1;
1061 mutex_unlock(&serial->disc_mutex);
1063 /* Unfortunately, many of the sub-drivers expect the port structures
1064 * to exist when their shutdown method is called, so we have to go
1065 * through this awkward two-step unregistration procedure.
1067 for (i = 0; i < serial->num_ports; ++i) {
1068 port = serial->port[i];
1069 if (port) {
1070 if (port->port.tty)
1071 tty_hangup(port->port.tty);
1072 kill_traffic(port);
1073 cancel_work_sync(&port->work);
1074 device_del(&port->dev);
1077 serial->type->shutdown(serial);
1078 for (i = 0; i < serial->num_ports; ++i) {
1079 port = serial->port[i];
1080 if (port) {
1081 put_device(&port->dev);
1082 serial->port[i] = NULL;
1086 /* let the last holder of this object
1087 * cause it to be cleaned up */
1088 usb_serial_put(serial);
1089 dev_info(dev, "device disconnected\n");
1091 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1093 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1095 struct usb_serial *serial = usb_get_intfdata(intf);
1096 struct usb_serial_port *port;
1097 int i, r = 0;
1099 for (i = 0; i < serial->num_ports; ++i) {
1100 port = serial->port[i];
1101 if (port)
1102 kill_traffic(port);
1105 if (serial->type->suspend)
1106 r = serial->type->suspend(serial, message);
1108 return r;
1110 EXPORT_SYMBOL(usb_serial_suspend);
1112 int usb_serial_resume(struct usb_interface *intf)
1114 struct usb_serial *serial = usb_get_intfdata(intf);
1116 if (serial->type->resume)
1117 return serial->type->resume(serial);
1118 return 0;
1120 EXPORT_SYMBOL(usb_serial_resume);
1122 static const struct tty_operations serial_ops = {
1123 .open = serial_open,
1124 .close = serial_close,
1125 .write = serial_write,
1126 .write_room = serial_write_room,
1127 .ioctl = serial_ioctl,
1128 .set_termios = serial_set_termios,
1129 .throttle = serial_throttle,
1130 .unthrottle = serial_unthrottle,
1131 .break_ctl = serial_break,
1132 .chars_in_buffer = serial_chars_in_buffer,
1133 .read_proc = serial_read_proc,
1134 .tiocmget = serial_tiocmget,
1135 .tiocmset = serial_tiocmset,
1138 struct tty_driver *usb_serial_tty_driver;
1140 static int __init usb_serial_init(void)
1142 int i;
1143 int result;
1145 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1146 if (!usb_serial_tty_driver)
1147 return -ENOMEM;
1149 /* Initialize our global data */
1150 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1151 serial_table[i] = NULL;
1153 result = bus_register(&usb_serial_bus_type);
1154 if (result) {
1155 err("%s - registering bus driver failed", __func__);
1156 goto exit_bus;
1159 usb_serial_tty_driver->owner = THIS_MODULE;
1160 usb_serial_tty_driver->driver_name = "usbserial";
1161 usb_serial_tty_driver->name = "ttyUSB";
1162 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1163 usb_serial_tty_driver->minor_start = 0;
1164 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1165 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1166 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1167 TTY_DRIVER_DYNAMIC_DEV;
1168 usb_serial_tty_driver->init_termios = tty_std_termios;
1169 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1170 | HUPCL | CLOCAL;
1171 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1172 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1173 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1174 result = tty_register_driver(usb_serial_tty_driver);
1175 if (result) {
1176 err("%s - tty_register_driver failed", __func__);
1177 goto exit_reg_driver;
1180 /* register the USB driver */
1181 result = usb_register(&usb_serial_driver);
1182 if (result < 0) {
1183 err("%s - usb_register failed", __func__);
1184 goto exit_tty;
1187 /* register the generic driver, if we should */
1188 result = usb_serial_generic_register(debug);
1189 if (result < 0) {
1190 err("%s - registering generic driver failed", __func__);
1191 goto exit_generic;
1194 info(DRIVER_DESC);
1196 return result;
1198 exit_generic:
1199 usb_deregister(&usb_serial_driver);
1201 exit_tty:
1202 tty_unregister_driver(usb_serial_tty_driver);
1204 exit_reg_driver:
1205 bus_unregister(&usb_serial_bus_type);
1207 exit_bus:
1208 err("%s - returning with error %d", __func__, result);
1209 put_tty_driver(usb_serial_tty_driver);
1210 return result;
1214 static void __exit usb_serial_exit(void)
1216 usb_serial_console_exit();
1218 usb_serial_generic_deregister();
1220 usb_deregister(&usb_serial_driver);
1221 tty_unregister_driver(usb_serial_tty_driver);
1222 put_tty_driver(usb_serial_tty_driver);
1223 bus_unregister(&usb_serial_bus_type);
1227 module_init(usb_serial_init);
1228 module_exit(usb_serial_exit);
1230 #define set_to_generic_if_null(type, function) \
1231 do { \
1232 if (!type->function) { \
1233 type->function = usb_serial_generic_##function; \
1234 dbg("Had to override the " #function \
1235 " usb serial operation with the generic one.");\
1237 } while (0)
1239 static void fixup_generic(struct usb_serial_driver *device)
1241 set_to_generic_if_null(device, open);
1242 set_to_generic_if_null(device, write);
1243 set_to_generic_if_null(device, close);
1244 set_to_generic_if_null(device, write_room);
1245 set_to_generic_if_null(device, chars_in_buffer);
1246 set_to_generic_if_null(device, read_bulk_callback);
1247 set_to_generic_if_null(device, write_bulk_callback);
1248 set_to_generic_if_null(device, shutdown);
1249 set_to_generic_if_null(device, resume);
1252 int usb_serial_register(struct usb_serial_driver *driver)
1254 /* must be called with BKL held */
1255 int retval;
1257 fixup_generic(driver);
1259 if (!driver->description)
1260 driver->description = driver->driver.name;
1262 /* Add this device to our list of devices */
1263 list_add(&driver->driver_list, &usb_serial_driver_list);
1265 retval = usb_serial_bus_register(driver);
1266 if (retval) {
1267 err("problem %d when registering driver %s",
1268 retval, driver->description);
1269 list_del(&driver->driver_list);
1270 } else
1271 info("USB Serial support registered for %s",
1272 driver->description);
1274 return retval;
1276 EXPORT_SYMBOL_GPL(usb_serial_register);
1279 void usb_serial_deregister(struct usb_serial_driver *device)
1281 /* must be called with BKL held */
1282 info("USB Serial deregistering driver %s", device->description);
1283 list_del(&device->driver_list);
1284 usb_serial_bus_deregister(device);
1286 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1288 /* Module information */
1289 MODULE_AUTHOR(DRIVER_AUTHOR);
1290 MODULE_DESCRIPTION(DRIVER_DESC);
1291 MODULE_LICENSE("GPL");
1293 module_param(debug, bool, S_IRUGO | S_IWUSR);
1294 MODULE_PARM_DESC(debug, "Debug enabled or not");