usb-serial: fix crash when sub-driver updates firmware
[linux-2.6/mini2440.git] / drivers / usb / serial / usb-serial.c
blob61b7d9e20fc226cc97a739beb075dafa2cdf0508
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 tty_port_tty_set(&port->port, 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 tty_port_tty_set(&port->port, 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 if (port->port.count == 1)
283 /* only call the device specific close if this
284 * port is being closed by the last owner. Ensure we do
285 * this before we drop the port count. The call is protected
286 * by the port mutex
288 serial->type->close(tty, port, filp);
290 if (port->port.count == (port->console ? 2 : 1)) {
291 struct tty_struct *tty = tty_port_tty_get(&port->port);
292 if (tty) {
293 /* We must do this before we drop the port count to
294 zero. */
295 if (tty->driver_data)
296 tty->driver_data = NULL;
297 tty_port_tty_set(&port->port, NULL);
298 tty_kref_put(tty);
302 --port->port.count;
303 count = port->port.count;
304 mutex_unlock(&port->mutex);
305 put_device(&port->dev);
307 /* Mustn't dereference port any more */
308 if (count == 0) {
309 mutex_lock(&serial->disc_mutex);
310 if (!serial->disconnected)
311 usb_autopm_put_interface(serial->interface);
312 mutex_unlock(&serial->disc_mutex);
314 usb_serial_put(serial);
316 /* Mustn't dereference serial any more */
317 if (count == 0)
318 module_put(owner);
321 static int serial_write(struct tty_struct *tty, const unsigned char *buf,
322 int count)
324 struct usb_serial_port *port = tty->driver_data;
325 int retval = -ENODEV;
327 if (port->serial->dev->state == USB_STATE_NOTATTACHED)
328 goto exit;
330 dbg("%s - port %d, %d byte(s)", __func__, port->number, count);
332 /* count is managed under the mutex lock for the tty so cannot
333 drop to zero until after the last close completes */
334 WARN_ON(!port->port.count);
336 /* pass on to the driver specific version of this function */
337 retval = port->serial->type->write(tty, port, buf, count);
339 exit:
340 return retval;
343 static int serial_write_room(struct tty_struct *tty)
345 struct usb_serial_port *port = tty->driver_data;
346 dbg("%s - port %d", __func__, port->number);
347 WARN_ON(!port->port.count);
348 /* pass on to the driver specific version of this function */
349 return port->serial->type->write_room(tty);
352 static int serial_chars_in_buffer(struct tty_struct *tty)
354 struct usb_serial_port *port = tty->driver_data;
355 dbg("%s = port %d", __func__, port->number);
357 WARN_ON(!port->port.count);
358 /* if the device was unplugged then any remaining characters
359 fell out of the connector ;) */
360 if (port->serial->disconnected)
361 return 0;
362 /* pass on to the driver specific version of this function */
363 return port->serial->type->chars_in_buffer(tty);
366 static void serial_throttle(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->throttle)
374 port->serial->type->throttle(tty);
377 static void serial_unthrottle(struct tty_struct *tty)
379 struct usb_serial_port *port = tty->driver_data;
380 dbg("%s - port %d", __func__, port->number);
382 WARN_ON(!port->port.count);
383 /* pass on to the driver specific version of this function */
384 if (port->serial->type->unthrottle)
385 port->serial->type->unthrottle(tty);
388 static int serial_ioctl(struct tty_struct *tty, struct file *file,
389 unsigned int cmd, unsigned long arg)
391 struct usb_serial_port *port = tty->driver_data;
392 int retval = -ENODEV;
394 dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
396 WARN_ON(!port->port.count);
398 /* pass on to the driver specific version of this function
399 if it is available */
400 if (port->serial->type->ioctl) {
401 retval = port->serial->type->ioctl(tty, file, cmd, arg);
402 } else
403 retval = -ENOIOCTLCMD;
404 return retval;
407 static void serial_set_termios(struct tty_struct *tty, struct ktermios *old)
409 struct usb_serial_port *port = tty->driver_data;
410 dbg("%s - port %d", __func__, port->number);
412 WARN_ON(!port->port.count);
413 /* pass on to the driver specific version of this function
414 if it is available */
415 if (port->serial->type->set_termios)
416 port->serial->type->set_termios(tty, port, old);
417 else
418 tty_termios_copy_hw(tty->termios, old);
421 static int serial_break(struct tty_struct *tty, int break_state)
423 struct usb_serial_port *port = tty->driver_data;
425 dbg("%s - port %d", __func__, port->number);
427 WARN_ON(!port->port.count);
428 /* pass on to the driver specific version of this function
429 if it is available */
430 if (port->serial->type->break_ctl)
431 port->serial->type->break_ctl(tty, break_state);
432 return 0;
435 static int serial_read_proc(char *page, char **start, off_t off, int count,
436 int *eof, void *data)
438 struct usb_serial *serial;
439 int length = 0;
440 int i;
441 off_t begin = 0;
442 char tmp[40];
444 dbg("%s", __func__);
445 length += sprintf(page, "usbserinfo:1.0 driver:2.0\n");
446 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
447 serial = usb_serial_get_by_index(i);
448 if (serial == NULL)
449 continue;
451 length += sprintf(page+length, "%d:", i);
452 if (serial->type->driver.owner)
453 length += sprintf(page+length, " module:%s",
454 module_name(serial->type->driver.owner));
455 length += sprintf(page+length, " name:\"%s\"",
456 serial->type->description);
457 length += sprintf(page+length, " vendor:%04x product:%04x",
458 le16_to_cpu(serial->dev->descriptor.idVendor),
459 le16_to_cpu(serial->dev->descriptor.idProduct));
460 length += sprintf(page+length, " num_ports:%d",
461 serial->num_ports);
462 length += sprintf(page+length, " port:%d",
463 i - serial->minor + 1);
464 usb_make_path(serial->dev, tmp, sizeof(tmp));
465 length += sprintf(page+length, " path:%s", tmp);
467 length += sprintf(page+length, "\n");
468 if ((length + begin) > (off + count)) {
469 usb_serial_put(serial);
470 goto done;
472 if ((length + begin) < off) {
473 begin += length;
474 length = 0;
476 usb_serial_put(serial);
478 *eof = 1;
479 done:
480 if (off >= (length + begin))
481 return 0;
482 *start = page + (off-begin);
483 return (count < begin+length-off) ? count : begin+length-off;
486 static int serial_tiocmget(struct tty_struct *tty, struct file *file)
488 struct usb_serial_port *port = tty->driver_data;
490 dbg("%s - port %d", __func__, port->number);
492 WARN_ON(!port->port.count);
493 if (port->serial->type->tiocmget)
494 return port->serial->type->tiocmget(tty, file);
495 return -EINVAL;
498 static int serial_tiocmset(struct tty_struct *tty, struct file *file,
499 unsigned int set, unsigned int clear)
501 struct usb_serial_port *port = tty->driver_data;
503 dbg("%s - port %d", __func__, port->number);
505 WARN_ON(!port->port.count);
506 if (port->serial->type->tiocmset)
507 return port->serial->type->tiocmset(tty, file, set, clear);
508 return -EINVAL;
512 * We would be calling tty_wakeup here, but unfortunately some line
513 * disciplines have an annoying habit of calling tty->write from
514 * the write wakeup callback (e.g. n_hdlc.c).
516 void usb_serial_port_softint(struct usb_serial_port *port)
518 schedule_work(&port->work);
520 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
522 static void usb_serial_port_work(struct work_struct *work)
524 struct usb_serial_port *port =
525 container_of(work, struct usb_serial_port, work);
526 struct tty_struct *tty;
528 dbg("%s - port %d", __func__, port->number);
530 tty = tty_port_tty_get(&port->port);
531 if (!tty)
532 return;
534 tty_wakeup(tty);
535 tty_kref_put(tty);
538 static void port_release(struct device *dev)
540 struct usb_serial_port *port = to_usb_serial_port(dev);
542 dbg ("%s - %s", __func__, dev_name(dev));
543 port_free(port);
546 static void kill_traffic(struct usb_serial_port *port)
548 usb_kill_urb(port->read_urb);
549 usb_kill_urb(port->write_urb);
551 * This is tricky.
552 * Some drivers submit the read_urb in the
553 * handler for the write_urb or vice versa
554 * this order determines the order in which
555 * usb_kill_urb() must be used to reliably
556 * kill the URBs. As it is unknown here,
557 * both orders must be used in turn.
558 * The call below is not redundant.
560 usb_kill_urb(port->read_urb);
561 usb_kill_urb(port->interrupt_in_urb);
562 usb_kill_urb(port->interrupt_out_urb);
565 static void port_free(struct usb_serial_port *port)
568 * Stop all the traffic before cancelling the work, so that
569 * nobody will restart it by calling usb_serial_port_softint.
571 kill_traffic(port);
572 cancel_work_sync(&port->work);
574 usb_free_urb(port->read_urb);
575 usb_free_urb(port->write_urb);
576 usb_free_urb(port->interrupt_in_urb);
577 usb_free_urb(port->interrupt_out_urb);
578 kfree(port->bulk_in_buffer);
579 kfree(port->bulk_out_buffer);
580 kfree(port->interrupt_in_buffer);
581 kfree(port->interrupt_out_buffer);
582 kfree(port);
585 static struct usb_serial *create_serial(struct usb_device *dev,
586 struct usb_interface *interface,
587 struct usb_serial_driver *driver)
589 struct usb_serial *serial;
591 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
592 if (!serial) {
593 dev_err(&dev->dev, "%s - out of memory\n", __func__);
594 return NULL;
596 serial->dev = usb_get_dev(dev);
597 serial->type = driver;
598 serial->interface = interface;
599 kref_init(&serial->kref);
600 mutex_init(&serial->disc_mutex);
601 serial->minor = SERIAL_TTY_NO_MINOR;
603 return serial;
606 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
607 struct usb_serial_driver *drv)
609 struct usb_dynid *dynid;
611 spin_lock(&drv->dynids.lock);
612 list_for_each_entry(dynid, &drv->dynids.list, node) {
613 if (usb_match_one_id(intf, &dynid->id)) {
614 spin_unlock(&drv->dynids.lock);
615 return &dynid->id;
618 spin_unlock(&drv->dynids.lock);
619 return NULL;
622 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
623 struct usb_interface *intf)
625 const struct usb_device_id *id;
627 id = usb_match_id(intf, drv->id_table);
628 if (id) {
629 dbg("static descriptor matches");
630 goto exit;
632 id = match_dynamic_id(intf, drv);
633 if (id)
634 dbg("dynamic descriptor matches");
635 exit:
636 return id;
639 static struct usb_serial_driver *search_serial_device(
640 struct usb_interface *iface)
642 const struct usb_device_id *id;
643 struct usb_serial_driver *drv;
645 /* Check if the usb id matches a known device */
646 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
647 id = get_iface_id(drv, iface);
648 if (id)
649 return drv;
652 return NULL;
655 int usb_serial_probe(struct usb_interface *interface,
656 const struct usb_device_id *id)
658 struct usb_device *dev = interface_to_usbdev(interface);
659 struct usb_serial *serial = NULL;
660 struct usb_serial_port *port;
661 struct usb_host_interface *iface_desc;
662 struct usb_endpoint_descriptor *endpoint;
663 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
664 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
665 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
666 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
667 struct usb_serial_driver *type = NULL;
668 int retval;
669 unsigned int minor;
670 int buffer_size;
671 int i;
672 int num_interrupt_in = 0;
673 int num_interrupt_out = 0;
674 int num_bulk_in = 0;
675 int num_bulk_out = 0;
676 int num_ports = 0;
677 int max_endpoints;
679 lock_kernel(); /* guard against unloading a serial driver module */
680 type = search_serial_device(interface);
681 if (!type) {
682 unlock_kernel();
683 dbg("none matched");
684 return -ENODEV;
687 serial = create_serial(dev, interface, type);
688 if (!serial) {
689 unlock_kernel();
690 dev_err(&interface->dev, "%s - out of memory\n", __func__);
691 return -ENOMEM;
694 /* if this device type has a probe function, call it */
695 if (type->probe) {
696 const struct usb_device_id *id;
698 if (!try_module_get(type->driver.owner)) {
699 unlock_kernel();
700 dev_err(&interface->dev,
701 "module get failed, exiting\n");
702 kfree(serial);
703 return -EIO;
706 id = get_iface_id(type, interface);
707 retval = type->probe(serial, id);
708 module_put(type->driver.owner);
710 if (retval) {
711 unlock_kernel();
712 dbg("sub driver rejected device");
713 kfree(serial);
714 return retval;
718 /* descriptor matches, let's find the endpoints needed */
719 /* check out the endpoints */
720 iface_desc = interface->cur_altsetting;
721 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
722 endpoint = &iface_desc->endpoint[i].desc;
724 if (usb_endpoint_is_bulk_in(endpoint)) {
725 /* we found a bulk in endpoint */
726 dbg("found bulk in on endpoint %d", i);
727 bulk_in_endpoint[num_bulk_in] = endpoint;
728 ++num_bulk_in;
731 if (usb_endpoint_is_bulk_out(endpoint)) {
732 /* we found a bulk out endpoint */
733 dbg("found bulk out on endpoint %d", i);
734 bulk_out_endpoint[num_bulk_out] = endpoint;
735 ++num_bulk_out;
738 if (usb_endpoint_is_int_in(endpoint)) {
739 /* we found a interrupt in endpoint */
740 dbg("found interrupt in on endpoint %d", i);
741 interrupt_in_endpoint[num_interrupt_in] = endpoint;
742 ++num_interrupt_in;
745 if (usb_endpoint_is_int_out(endpoint)) {
746 /* we found an interrupt out endpoint */
747 dbg("found interrupt out on endpoint %d", i);
748 interrupt_out_endpoint[num_interrupt_out] = endpoint;
749 ++num_interrupt_out;
753 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
754 /* BEGIN HORRIBLE HACK FOR PL2303 */
755 /* this is needed due to the looney way its endpoints are set up */
756 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
757 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
758 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
759 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
760 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
761 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
762 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
763 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
764 if (interface != dev->actconfig->interface[0]) {
765 /* check out the endpoints of the other interface*/
766 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
767 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
768 endpoint = &iface_desc->endpoint[i].desc;
769 if (usb_endpoint_is_int_in(endpoint)) {
770 /* we found a interrupt in endpoint */
771 dbg("found interrupt in for Prolific device on separate interface");
772 interrupt_in_endpoint[num_interrupt_in] = endpoint;
773 ++num_interrupt_in;
778 /* Now make sure the PL-2303 is configured correctly.
779 * If not, give up now and hope this hack will work
780 * properly during a later invocation of usb_serial_probe
782 if (num_bulk_in == 0 || num_bulk_out == 0) {
783 unlock_kernel();
784 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
785 kfree(serial);
786 return -ENODEV;
789 /* END HORRIBLE HACK FOR PL2303 */
790 #endif
792 #ifdef CONFIG_USB_SERIAL_GENERIC
793 if (type == &usb_serial_generic_device) {
794 num_ports = num_bulk_out;
795 if (num_ports == 0) {
796 unlock_kernel();
797 dev_err(&interface->dev,
798 "Generic device with no bulk out, not allowed.\n");
799 kfree(serial);
800 return -EIO;
803 #endif
804 if (!num_ports) {
805 /* if this device type has a calc_num_ports function, call it */
806 if (type->calc_num_ports) {
807 if (!try_module_get(type->driver.owner)) {
808 unlock_kernel();
809 dev_err(&interface->dev,
810 "module get failed, exiting\n");
811 kfree(serial);
812 return -EIO;
814 num_ports = type->calc_num_ports(serial);
815 module_put(type->driver.owner);
817 if (!num_ports)
818 num_ports = type->num_ports;
821 serial->num_ports = num_ports;
822 serial->num_bulk_in = num_bulk_in;
823 serial->num_bulk_out = num_bulk_out;
824 serial->num_interrupt_in = num_interrupt_in;
825 serial->num_interrupt_out = num_interrupt_out;
827 /* found all that we need */
828 dev_info(&interface->dev, "%s converter detected\n",
829 type->description);
831 /* create our ports, we need as many as the max endpoints */
832 /* we don't use num_ports here because some devices have more
833 endpoint pairs than ports */
834 max_endpoints = max(num_bulk_in, num_bulk_out);
835 max_endpoints = max(max_endpoints, num_interrupt_in);
836 max_endpoints = max(max_endpoints, num_interrupt_out);
837 max_endpoints = max(max_endpoints, (int)serial->num_ports);
838 serial->num_port_pointers = max_endpoints;
839 unlock_kernel();
841 dbg("%s - setting up %d port structures for this device",
842 __func__, max_endpoints);
843 for (i = 0; i < max_endpoints; ++i) {
844 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
845 if (!port)
846 goto probe_error;
847 tty_port_init(&port->port);
848 port->serial = serial;
849 spin_lock_init(&port->lock);
850 mutex_init(&port->mutex);
851 INIT_WORK(&port->work, usb_serial_port_work);
852 serial->port[i] = port;
855 /* set up the endpoint information */
856 for (i = 0; i < num_bulk_in; ++i) {
857 endpoint = bulk_in_endpoint[i];
858 port = serial->port[i];
859 port->read_urb = usb_alloc_urb(0, GFP_KERNEL);
860 if (!port->read_urb) {
861 dev_err(&interface->dev, "No free urbs available\n");
862 goto probe_error;
864 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
865 port->bulk_in_size = buffer_size;
866 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
867 port->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
868 if (!port->bulk_in_buffer) {
869 dev_err(&interface->dev,
870 "Couldn't allocate bulk_in_buffer\n");
871 goto probe_error;
873 usb_fill_bulk_urb(port->read_urb, dev,
874 usb_rcvbulkpipe(dev,
875 endpoint->bEndpointAddress),
876 port->bulk_in_buffer, buffer_size,
877 serial->type->read_bulk_callback, port);
880 for (i = 0; i < num_bulk_out; ++i) {
881 endpoint = bulk_out_endpoint[i];
882 port = serial->port[i];
883 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
884 if (!port->write_urb) {
885 dev_err(&interface->dev, "No free urbs available\n");
886 goto probe_error;
888 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
889 port->bulk_out_size = buffer_size;
890 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
891 port->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
892 if (!port->bulk_out_buffer) {
893 dev_err(&interface->dev,
894 "Couldn't allocate bulk_out_buffer\n");
895 goto probe_error;
897 usb_fill_bulk_urb(port->write_urb, dev,
898 usb_sndbulkpipe(dev,
899 endpoint->bEndpointAddress),
900 port->bulk_out_buffer, buffer_size,
901 serial->type->write_bulk_callback, port);
904 if (serial->type->read_int_callback) {
905 for (i = 0; i < num_interrupt_in; ++i) {
906 endpoint = interrupt_in_endpoint[i];
907 port = serial->port[i];
908 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
909 if (!port->interrupt_in_urb) {
910 dev_err(&interface->dev,
911 "No free urbs available\n");
912 goto probe_error;
914 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
915 port->interrupt_in_endpointAddress =
916 endpoint->bEndpointAddress;
917 port->interrupt_in_buffer = kmalloc(buffer_size,
918 GFP_KERNEL);
919 if (!port->interrupt_in_buffer) {
920 dev_err(&interface->dev,
921 "Couldn't allocate interrupt_in_buffer\n");
922 goto probe_error;
924 usb_fill_int_urb(port->interrupt_in_urb, dev,
925 usb_rcvintpipe(dev,
926 endpoint->bEndpointAddress),
927 port->interrupt_in_buffer, buffer_size,
928 serial->type->read_int_callback, port,
929 endpoint->bInterval);
931 } else if (num_interrupt_in) {
932 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
935 if (serial->type->write_int_callback) {
936 for (i = 0; i < num_interrupt_out; ++i) {
937 endpoint = interrupt_out_endpoint[i];
938 port = serial->port[i];
939 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
940 if (!port->interrupt_out_urb) {
941 dev_err(&interface->dev,
942 "No free urbs available\n");
943 goto probe_error;
945 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
946 port->interrupt_out_size = buffer_size;
947 port->interrupt_out_endpointAddress =
948 endpoint->bEndpointAddress;
949 port->interrupt_out_buffer = kmalloc(buffer_size,
950 GFP_KERNEL);
951 if (!port->interrupt_out_buffer) {
952 dev_err(&interface->dev,
953 "Couldn't allocate interrupt_out_buffer\n");
954 goto probe_error;
956 usb_fill_int_urb(port->interrupt_out_urb, dev,
957 usb_sndintpipe(dev,
958 endpoint->bEndpointAddress),
959 port->interrupt_out_buffer, buffer_size,
960 serial->type->write_int_callback, port,
961 endpoint->bInterval);
963 } else if (num_interrupt_out) {
964 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
967 /* if this device type has an attach function, call it */
968 if (type->attach) {
969 if (!try_module_get(type->driver.owner)) {
970 dev_err(&interface->dev,
971 "module get failed, exiting\n");
972 goto probe_error;
974 retval = type->attach(serial);
975 module_put(type->driver.owner);
976 if (retval < 0)
977 goto probe_error;
978 if (retval > 0) {
979 /* quietly accept this device, but don't bind to a
980 serial port as it's about to disappear */
981 serial->num_ports = 0;
982 goto exit;
986 if (get_free_serial(serial, num_ports, &minor) == NULL) {
987 dev_err(&interface->dev, "No more free serial devices\n");
988 goto probe_error;
990 serial->minor = minor;
992 /* register all of the individual ports with the driver core */
993 for (i = 0; i < num_ports; ++i) {
994 port = serial->port[i];
995 port->dev.parent = &interface->dev;
996 port->dev.driver = NULL;
997 port->dev.bus = &usb_serial_bus_type;
998 port->dev.release = &port_release;
1000 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1001 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1002 retval = device_register(&port->dev);
1003 if (retval)
1004 dev_err(&port->dev, "Error registering port device, "
1005 "continuing\n");
1008 usb_serial_console_init(debug, minor);
1010 exit:
1011 /* success */
1012 usb_set_intfdata(interface, serial);
1013 return 0;
1015 probe_error:
1016 for (i = 0; i < num_bulk_in; ++i) {
1017 port = serial->port[i];
1018 if (!port)
1019 continue;
1020 usb_free_urb(port->read_urb);
1021 kfree(port->bulk_in_buffer);
1023 for (i = 0; i < num_bulk_out; ++i) {
1024 port = serial->port[i];
1025 if (!port)
1026 continue;
1027 usb_free_urb(port->write_urb);
1028 kfree(port->bulk_out_buffer);
1030 for (i = 0; i < num_interrupt_in; ++i) {
1031 port = serial->port[i];
1032 if (!port)
1033 continue;
1034 usb_free_urb(port->interrupt_in_urb);
1035 kfree(port->interrupt_in_buffer);
1037 for (i = 0; i < num_interrupt_out; ++i) {
1038 port = serial->port[i];
1039 if (!port)
1040 continue;
1041 usb_free_urb(port->interrupt_out_urb);
1042 kfree(port->interrupt_out_buffer);
1045 /* free up any memory that we allocated */
1046 for (i = 0; i < serial->num_port_pointers; ++i)
1047 kfree(serial->port[i]);
1048 kfree(serial);
1049 return -EIO;
1051 EXPORT_SYMBOL_GPL(usb_serial_probe);
1053 void usb_serial_disconnect(struct usb_interface *interface)
1055 int i;
1056 struct usb_serial *serial = usb_get_intfdata(interface);
1057 struct device *dev = &interface->dev;
1058 struct usb_serial_port *port;
1060 usb_serial_console_disconnect(serial);
1061 dbg("%s", __func__);
1063 mutex_lock(&serial->disc_mutex);
1064 usb_set_intfdata(interface, NULL);
1065 /* must set a flag, to signal subdrivers */
1066 serial->disconnected = 1;
1067 mutex_unlock(&serial->disc_mutex);
1069 /* Unfortunately, many of the sub-drivers expect the port structures
1070 * to exist when their shutdown method is called, so we have to go
1071 * through this awkward two-step unregistration procedure.
1073 for (i = 0; i < serial->num_ports; ++i) {
1074 port = serial->port[i];
1075 if (port) {
1076 struct tty_struct *tty = tty_port_tty_get(&port->port);
1077 if (tty) {
1078 tty_hangup(tty);
1079 tty_kref_put(tty);
1081 kill_traffic(port);
1082 cancel_work_sync(&port->work);
1083 device_del(&port->dev);
1086 serial->type->shutdown(serial);
1087 for (i = 0; i < serial->num_ports; ++i) {
1088 port = serial->port[i];
1089 if (port) {
1090 put_device(&port->dev);
1091 serial->port[i] = NULL;
1095 /* let the last holder of this object
1096 * cause it to be cleaned up */
1097 usb_serial_put(serial);
1098 dev_info(dev, "device disconnected\n");
1100 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1102 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1104 struct usb_serial *serial = usb_get_intfdata(intf);
1105 struct usb_serial_port *port;
1106 int i, r = 0;
1108 for (i = 0; i < serial->num_ports; ++i) {
1109 port = serial->port[i];
1110 if (port)
1111 kill_traffic(port);
1114 if (serial->type->suspend)
1115 r = serial->type->suspend(serial, message);
1117 return r;
1119 EXPORT_SYMBOL(usb_serial_suspend);
1121 int usb_serial_resume(struct usb_interface *intf)
1123 struct usb_serial *serial = usb_get_intfdata(intf);
1125 if (serial->type->resume)
1126 return serial->type->resume(serial);
1127 return 0;
1129 EXPORT_SYMBOL(usb_serial_resume);
1131 static const struct tty_operations serial_ops = {
1132 .open = serial_open,
1133 .close = serial_close,
1134 .write = serial_write,
1135 .write_room = serial_write_room,
1136 .ioctl = serial_ioctl,
1137 .set_termios = serial_set_termios,
1138 .throttle = serial_throttle,
1139 .unthrottle = serial_unthrottle,
1140 .break_ctl = serial_break,
1141 .chars_in_buffer = serial_chars_in_buffer,
1142 .read_proc = serial_read_proc,
1143 .tiocmget = serial_tiocmget,
1144 .tiocmset = serial_tiocmset,
1147 struct tty_driver *usb_serial_tty_driver;
1149 static int __init usb_serial_init(void)
1151 int i;
1152 int result;
1154 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1155 if (!usb_serial_tty_driver)
1156 return -ENOMEM;
1158 /* Initialize our global data */
1159 for (i = 0; i < SERIAL_TTY_MINORS; ++i)
1160 serial_table[i] = NULL;
1162 result = bus_register(&usb_serial_bus_type);
1163 if (result) {
1164 printk(KERN_ERR "usb-serial: %s - registering bus driver "
1165 "failed\n", __func__);
1166 goto exit_bus;
1169 usb_serial_tty_driver->owner = THIS_MODULE;
1170 usb_serial_tty_driver->driver_name = "usbserial";
1171 usb_serial_tty_driver->name = "ttyUSB";
1172 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1173 usb_serial_tty_driver->minor_start = 0;
1174 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1175 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1176 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW |
1177 TTY_DRIVER_DYNAMIC_DEV;
1178 usb_serial_tty_driver->init_termios = tty_std_termios;
1179 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD
1180 | HUPCL | CLOCAL;
1181 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1182 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1183 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1184 result = tty_register_driver(usb_serial_tty_driver);
1185 if (result) {
1186 printk(KERN_ERR "usb-serial: %s - tty_register_driver failed\n",
1187 __func__);
1188 goto exit_reg_driver;
1191 /* register the USB driver */
1192 result = usb_register(&usb_serial_driver);
1193 if (result < 0) {
1194 printk(KERN_ERR "usb-serial: %s - usb_register failed\n",
1195 __func__);
1196 goto exit_tty;
1199 /* register the generic driver, if we should */
1200 result = usb_serial_generic_register(debug);
1201 if (result < 0) {
1202 printk(KERN_ERR "usb-serial: %s - registering generic "
1203 "driver failed\n", __func__);
1204 goto exit_generic;
1207 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
1209 return result;
1211 exit_generic:
1212 usb_deregister(&usb_serial_driver);
1214 exit_tty:
1215 tty_unregister_driver(usb_serial_tty_driver);
1217 exit_reg_driver:
1218 bus_unregister(&usb_serial_bus_type);
1220 exit_bus:
1221 printk(KERN_ERR "usb-serial: %s - returning with error %d\n",
1222 __func__, result);
1223 put_tty_driver(usb_serial_tty_driver);
1224 return result;
1228 static void __exit usb_serial_exit(void)
1230 usb_serial_console_exit();
1232 usb_serial_generic_deregister();
1234 usb_deregister(&usb_serial_driver);
1235 tty_unregister_driver(usb_serial_tty_driver);
1236 put_tty_driver(usb_serial_tty_driver);
1237 bus_unregister(&usb_serial_bus_type);
1241 module_init(usb_serial_init);
1242 module_exit(usb_serial_exit);
1244 #define set_to_generic_if_null(type, function) \
1245 do { \
1246 if (!type->function) { \
1247 type->function = usb_serial_generic_##function; \
1248 dbg("Had to override the " #function \
1249 " usb serial operation with the generic one.");\
1251 } while (0)
1253 static void fixup_generic(struct usb_serial_driver *device)
1255 set_to_generic_if_null(device, open);
1256 set_to_generic_if_null(device, write);
1257 set_to_generic_if_null(device, close);
1258 set_to_generic_if_null(device, write_room);
1259 set_to_generic_if_null(device, chars_in_buffer);
1260 set_to_generic_if_null(device, read_bulk_callback);
1261 set_to_generic_if_null(device, write_bulk_callback);
1262 set_to_generic_if_null(device, shutdown);
1263 set_to_generic_if_null(device, resume);
1266 int usb_serial_register(struct usb_serial_driver *driver)
1268 /* must be called with BKL held */
1269 int retval;
1271 fixup_generic(driver);
1273 if (!driver->description)
1274 driver->description = driver->driver.name;
1276 /* Add this device to our list of devices */
1277 list_add(&driver->driver_list, &usb_serial_driver_list);
1279 retval = usb_serial_bus_register(driver);
1280 if (retval) {
1281 printk(KERN_ERR "usb-serial: problem %d when registering "
1282 "driver %s\n", retval, driver->description);
1283 list_del(&driver->driver_list);
1284 } else
1285 printk(KERN_INFO "USB Serial support registered for %s\n",
1286 driver->description);
1288 return retval;
1290 EXPORT_SYMBOL_GPL(usb_serial_register);
1293 void usb_serial_deregister(struct usb_serial_driver *device)
1295 /* must be called with BKL held */
1296 printk(KERN_INFO "USB Serial deregistering driver %s\n",
1297 device->description);
1298 list_del(&device->driver_list);
1299 usb_serial_bus_deregister(device);
1301 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1303 /* Module information */
1304 MODULE_AUTHOR(DRIVER_AUTHOR);
1305 MODULE_DESCRIPTION(DRIVER_DESC);
1306 MODULE_LICENSE("GPL");
1308 module_param(debug, bool, S_IRUGO | S_IWUSR);
1309 MODULE_PARM_DESC(debug, "Debug enabled or not");