allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / serial / usb-serial.c
blobb2d7cc6c11ecf9d701bd1f538f17a5a510e576b6
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 driver
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/tty.h>
24 #include <linux/tty_driver.h>
25 #include <linux/tty_flip.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/spinlock.h>
29 #include <linux/mutex.h>
30 #include <linux/list.h>
31 #include <asm/uaccess.h>
32 #include <linux/usb.h>
33 #include <linux/usb/serial.h>
34 #include "pl2303.h"
37 * Version Information
39 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
40 #define DRIVER_DESC "USB Serial Driver core"
42 static void port_free(struct usb_serial_port *port);
44 /* Driver structure we register with the USB core */
45 static struct usb_driver usb_serial_driver = {
46 .name = "usbserial",
47 .probe = usb_serial_probe,
48 .disconnect = usb_serial_disconnect,
49 .suspend = usb_serial_suspend,
50 .resume = usb_serial_resume,
51 .no_dynamic_id = 1,
54 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
55 the MODULE_DEVICE_TABLE declarations in each serial driver
56 cause the "hotplug" program to pull in whatever module is necessary
57 via modprobe, and modprobe will load usbserial because the serial
58 drivers depend on it.
61 static ushort maxSize = 0;
62 static int debug;
63 static struct usb_serial *serial_table[SERIAL_TTY_MINORS]; /* initially all NULL */
64 static DEFINE_MUTEX(table_lock);
65 static LIST_HEAD(usb_serial_driver_list);
67 struct usb_serial *usb_serial_get_by_index(unsigned index)
69 struct usb_serial *serial;
71 mutex_lock(&table_lock);
72 serial = serial_table[index];
74 if (serial)
75 kref_get(&serial->kref);
76 mutex_unlock(&table_lock);
77 return serial;
80 static struct usb_serial *get_free_serial (struct usb_serial *serial, int num_ports, unsigned int *minor)
82 unsigned int i, j;
83 int good_spot;
85 dbg("%s %d", __FUNCTION__, num_ports);
87 *minor = 0;
88 mutex_lock(&table_lock);
89 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
90 if (serial_table[i])
91 continue;
93 good_spot = 1;
94 for (j = 1; j <= num_ports-1; ++j)
95 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
96 good_spot = 0;
97 i += j;
98 break;
100 if (good_spot == 0)
101 continue;
103 *minor = i;
104 j = 0;
105 dbg("%s - minor base = %d", __FUNCTION__, *minor);
106 for (i = *minor; (i < (*minor + num_ports)) && (i < SERIAL_TTY_MINORS); ++i) {
107 serial_table[i] = serial;
108 serial->port[j++]->number = i;
110 mutex_unlock(&table_lock);
111 return serial;
113 mutex_unlock(&table_lock);
114 return NULL;
117 static void return_serial(struct usb_serial *serial)
119 int i;
121 dbg("%s", __FUNCTION__);
123 for (i = 0; i < serial->num_ports; ++i) {
124 serial_table[serial->minor + i] = NULL;
128 static void destroy_serial(struct kref *kref)
130 struct usb_serial *serial;
131 struct usb_serial_port *port;
132 int i;
134 serial = to_usb_serial(kref);
136 dbg("%s - %s", __FUNCTION__, serial->type->description);
138 /* return the minor range that this device had */
139 if (serial->minor != SERIAL_TTY_NO_MINOR)
140 return_serial(serial);
142 /* If this is a "fake" port, we have to clean it up here, as it will
143 * not get cleaned up in port_release() as it was never registered with
144 * the driver core */
145 if (serial->num_ports < serial->num_port_pointers) {
146 for (i = serial->num_ports; i < serial->num_port_pointers; ++i) {
147 port = serial->port[i];
148 if (!port)
149 continue;
150 port_free(port);
154 usb_put_dev(serial->dev);
156 /* free up any memory that we allocated */
157 kfree (serial);
160 void usb_serial_put(struct usb_serial *serial)
162 mutex_lock(&table_lock);
163 kref_put(&serial->kref, destroy_serial);
164 mutex_unlock(&table_lock);
167 /*****************************************************************************
168 * Driver tty interface functions
169 *****************************************************************************/
170 static int serial_open (struct tty_struct *tty, struct file * filp)
172 struct usb_serial *serial;
173 struct usb_serial_port *port;
174 unsigned int portNumber;
175 int retval = 0;
177 dbg("%s", __FUNCTION__);
179 /* get the serial object associated with this tty pointer */
180 serial = usb_serial_get_by_index(tty->index);
181 if (!serial) {
182 tty->driver_data = NULL;
183 return -ENODEV;
186 mutex_lock(&serial->disc_mutex);
187 portNumber = tty->index - serial->minor;
188 port = serial->port[portNumber];
189 if (!port || serial->disconnected)
190 retval = -ENODEV;
191 else
192 get_device(&port->dev);
194 * Note: Our locking order requirement does not allow port->mutex
195 * to be acquired while serial->disc_mutex is held.
197 mutex_unlock(&serial->disc_mutex);
198 if (retval)
199 goto bailout_serial_put;
201 if (mutex_lock_interruptible(&port->mutex)) {
202 retval = -ERESTARTSYS;
203 goto bailout_port_put;
206 ++port->open_count;
208 /* set up our port structure making the tty driver
209 * remember our port object, and us it */
210 tty->driver_data = port;
211 port->tty = tty;
213 if (port->open_count == 1) {
215 /* lock this module before we call it
216 * this may fail, which means we must bail out,
217 * safe because we are called with BKL held */
218 if (!try_module_get(serial->type->driver.owner)) {
219 retval = -ENODEV;
220 goto bailout_mutex_unlock;
223 mutex_lock(&serial->disc_mutex);
224 if (serial->disconnected)
225 retval = -ENODEV;
226 else
227 retval = usb_autopm_get_interface(serial->interface);
228 if (retval)
229 goto bailout_module_put;
231 /* only call the device specific open if this
232 * is the first time the port is opened */
233 retval = serial->type->open(port, filp);
234 if (retval)
235 goto bailout_interface_put;
236 mutex_unlock(&serial->disc_mutex);
239 mutex_unlock(&port->mutex);
240 return 0;
242 bailout_interface_put:
243 usb_autopm_put_interface(serial->interface);
244 bailout_module_put:
245 mutex_unlock(&serial->disc_mutex);
246 module_put(serial->type->driver.owner);
247 bailout_mutex_unlock:
248 port->open_count = 0;
249 tty->driver_data = NULL;
250 port->tty = NULL;
251 mutex_unlock(&port->mutex);
252 bailout_port_put:
253 put_device(&port->dev);
254 bailout_serial_put:
255 usb_serial_put(serial);
256 return retval;
259 static void serial_close(struct tty_struct *tty, struct file * filp)
261 struct usb_serial_port *port = tty->driver_data;
262 struct usb_serial *serial;
263 struct module *owner;
264 int count;
266 if (!port)
267 return;
269 dbg("%s - port %d", __FUNCTION__, port->number);
271 mutex_lock(&port->mutex);
272 serial = port->serial;
273 owner = serial->type->driver.owner;
275 if (port->open_count == 0) {
276 mutex_unlock(&port->mutex);
277 return;
280 if (port->open_count == 1)
281 /* only call the device specific close if this
282 * port is being closed by the last owner. Ensure we do
283 * this before we drop the port count. The call is protected
284 * by the port mutex
286 serial->type->close(port, filp);
288 if (port->open_count == (port->console ? 2 : 0)) {
289 if (port->tty) {
290 /* We must do this before we drop the port count to
291 zero. */
292 if (port->tty->driver_data)
293 port->tty->driver_data = NULL;
294 port->tty = NULL;
298 --port->open_count;
299 count = port->open_count;
300 mutex_unlock(&port->mutex);
301 put_device(&port->dev);
303 /* Mustn't dereference port any more */
304 if (count == 0) {
305 mutex_lock(&serial->disc_mutex);
306 if (!serial->disconnected)
307 usb_autopm_put_interface(serial->interface);
308 mutex_unlock(&serial->disc_mutex);
310 usb_serial_put(serial);
312 /* Mustn't dereference serial any more */
313 if (count == 0)
314 module_put(owner);
317 static int serial_write (struct tty_struct * tty, const unsigned char *buf, int count)
319 struct usb_serial_port *port = tty->driver_data;
320 int retval = -ENODEV;
322 if (!port || port->serial->dev->state == USB_STATE_NOTATTACHED)
323 goto exit;
325 dbg("%s - port %d, %d byte(s)", __FUNCTION__, port->number, count);
327 if (!port->open_count) {
328 retval = -EINVAL;
329 dbg("%s - port not opened", __FUNCTION__);
330 goto exit;
333 /* pass on to the driver specific version of this function */
334 retval = port->serial->type->write(port, buf, count);
336 exit:
337 return retval;
340 static int serial_write_room (struct tty_struct *tty)
342 struct usb_serial_port *port = tty->driver_data;
343 int retval = -ENODEV;
345 if (!port)
346 goto exit;
348 dbg("%s - port %d", __FUNCTION__, port->number);
350 if (!port->open_count) {
351 dbg("%s - port not open", __FUNCTION__);
352 goto exit;
355 /* pass on to the driver specific version of this function */
356 retval = port->serial->type->write_room(port);
358 exit:
359 return retval;
362 static int serial_chars_in_buffer (struct tty_struct *tty)
364 struct usb_serial_port *port = tty->driver_data;
365 int retval = -ENODEV;
367 if (!port)
368 goto exit;
370 dbg("%s = port %d", __FUNCTION__, port->number);
372 if (!port->open_count) {
373 dbg("%s - port not open", __FUNCTION__);
374 goto exit;
377 /* if the device was unplugged then any remaining characters
378 fell out of the connector ;) */
379 if (port->serial->disconnected)
380 return 0;
381 /* pass on to the driver specific version of this function */
382 retval = port->serial->type->chars_in_buffer(port);
384 exit:
385 return retval;
388 static void serial_throttle (struct tty_struct * tty)
390 struct usb_serial_port *port = tty->driver_data;
392 if (!port)
393 return;
395 dbg("%s - port %d", __FUNCTION__, port->number);
397 if (!port->open_count) {
398 dbg ("%s - port not open", __FUNCTION__);
399 return;
402 /* pass on to the driver specific version of this function */
403 if (port->serial->type->throttle)
404 port->serial->type->throttle(port);
407 static void serial_unthrottle (struct tty_struct * tty)
409 struct usb_serial_port *port = tty->driver_data;
411 if (!port)
412 return;
414 dbg("%s - port %d", __FUNCTION__, port->number);
416 if (!port->open_count) {
417 dbg("%s - port not open", __FUNCTION__);
418 return;
421 /* pass on to the driver specific version of this function */
422 if (port->serial->type->unthrottle)
423 port->serial->type->unthrottle(port);
426 static int serial_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg)
428 struct usb_serial_port *port = tty->driver_data;
429 int retval = -ENODEV;
431 lock_kernel();
432 if (!port)
433 goto exit;
435 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
437 /* Caution - port->open_count is BKL protected */
438 if (!port->open_count) {
439 dbg ("%s - port not open", __FUNCTION__);
440 goto exit;
443 /* pass on to the driver specific version of this function if it is available */
444 if (port->serial->type->ioctl)
445 retval = port->serial->type->ioctl(port, file, cmd, arg);
446 else
447 retval = -ENOIOCTLCMD;
448 exit:
449 unlock_kernel();
450 return retval;
453 static void serial_set_termios (struct tty_struct *tty, struct ktermios * old)
455 struct usb_serial_port *port = tty->driver_data;
457 if (!port)
458 return;
460 dbg("%s - port %d", __FUNCTION__, port->number);
462 if (!port->open_count) {
463 dbg("%s - port not open", __FUNCTION__);
464 return;
467 /* pass on to the driver specific version of this function if it is available */
468 if (port->serial->type->set_termios)
469 port->serial->type->set_termios(port, old);
470 else
471 tty_termios_copy_hw(tty->termios, old);
474 static void serial_break (struct tty_struct *tty, int break_state)
476 struct usb_serial_port *port = tty->driver_data;
478 lock_kernel();
479 if (!port) {
480 unlock_kernel();
481 return;
484 dbg("%s - port %d", __FUNCTION__, port->number);
486 if (!port->open_count) {
487 dbg("%s - port not open", __FUNCTION__);
488 unlock_kernel();
489 return;
492 /* pass on to the driver specific version of this function if it is available */
493 if (port->serial->type->break_ctl)
494 port->serial->type->break_ctl(port, break_state);
495 unlock_kernel();
498 static int serial_read_proc (char *page, char **start, off_t off, int count, int *eof, void *data)
500 struct usb_serial *serial;
501 int length = 0;
502 int i;
503 off_t begin = 0;
504 char tmp[40];
506 dbg("%s", __FUNCTION__);
507 length += sprintf (page, "usbserinfo:1.0 driver:2.0\n");
508 for (i = 0; i < SERIAL_TTY_MINORS && length < PAGE_SIZE; ++i) {
509 serial = usb_serial_get_by_index(i);
510 if (serial == NULL)
511 continue;
513 length += sprintf (page+length, "%d:", i);
514 if (serial->type->driver.owner)
515 length += sprintf (page+length, " module:%s", module_name(serial->type->driver.owner));
516 length += sprintf (page+length, " name:\"%s\"", serial->type->description);
517 length += sprintf (page+length, " vendor:%04x product:%04x",
518 le16_to_cpu(serial->dev->descriptor.idVendor),
519 le16_to_cpu(serial->dev->descriptor.idProduct));
520 length += sprintf (page+length, " num_ports:%d", serial->num_ports);
521 length += sprintf (page+length, " port:%d", i - serial->minor + 1);
523 usb_make_path(serial->dev, tmp, sizeof(tmp));
524 length += sprintf (page+length, " path:%s", tmp);
526 length += sprintf (page+length, "\n");
527 if ((length + begin) > (off + count)) {
528 usb_serial_put(serial);
529 goto done;
531 if ((length + begin) < off) {
532 begin += length;
533 length = 0;
535 usb_serial_put(serial);
537 *eof = 1;
538 done:
539 if (off >= (length + begin))
540 return 0;
541 *start = page + (off-begin);
542 return ((count < begin+length-off) ? count : begin+length-off);
545 static int serial_tiocmget (struct tty_struct *tty, struct file *file)
547 struct usb_serial_port *port = tty->driver_data;
549 if (!port)
550 return -ENODEV;
552 dbg("%s - port %d", __FUNCTION__, port->number);
554 if (!port->open_count) {
555 dbg("%s - port not open", __FUNCTION__);
556 return -ENODEV;
559 if (port->serial->type->tiocmget)
560 return port->serial->type->tiocmget(port, file);
562 return -EINVAL;
565 static int serial_tiocmset (struct tty_struct *tty, struct file *file,
566 unsigned int set, unsigned int clear)
568 struct usb_serial_port *port = tty->driver_data;
570 if (!port)
571 return -ENODEV;
573 dbg("%s - port %d", __FUNCTION__, port->number);
575 if (!port->open_count) {
576 dbg("%s - port not open", __FUNCTION__);
577 return -ENODEV;
580 if (port->serial->type->tiocmset)
581 return port->serial->type->tiocmset(port, file, set, clear);
583 return -EINVAL;
587 * We would be calling tty_wakeup here, but unfortunately some line
588 * disciplines have an annoying habit of calling tty->write from
589 * the write wakeup callback (e.g. n_hdlc.c).
591 void usb_serial_port_softint(struct usb_serial_port *port)
593 schedule_work(&port->work);
596 static void usb_serial_port_work(struct work_struct *work)
598 struct usb_serial_port *port =
599 container_of(work, struct usb_serial_port, work);
600 struct tty_struct *tty;
602 if (!port)
603 return;
605 dbg("%s - port %d", __FUNCTION__, port->number);
607 tty = port->tty;
608 if (!tty)
609 return;
611 tty_wakeup(tty);
614 static void port_release(struct device *dev)
616 struct usb_serial_port *port = to_usb_serial_port(dev);
618 dbg ("%s - %s", __FUNCTION__, dev->bus_id);
619 port_free(port);
622 static void kill_traffic(struct usb_serial_port *port)
624 usb_kill_urb(port->read_urb);
625 usb_kill_urb(port->write_urb);
627 * This is tricky.
628 * Some drivers submit the read_urb in the
629 * handler for the write_urb or vice versa
630 * this order determines the order in which
631 * usb_kill_urb() must be used to reliably
632 * kill the URBs. As it is unknown here,
633 * both orders must be used in turn.
634 * The call below is not redundant.
636 usb_kill_urb(port->read_urb);
637 usb_kill_urb(port->interrupt_in_urb);
638 usb_kill_urb(port->interrupt_out_urb);
641 static void port_free(struct usb_serial_port *port)
644 * Stop all the traffic before cancelling the work, so that
645 * nobody will restart it by calling usb_serial_port_softint.
647 kill_traffic(port);
648 cancel_work_sync(&port->work);
650 usb_free_urb(port->read_urb);
651 usb_free_urb(port->write_urb);
652 usb_free_urb(port->interrupt_in_urb);
653 usb_free_urb(port->interrupt_out_urb);
654 kfree(port->bulk_in_buffer);
655 kfree(port->bulk_out_buffer);
656 kfree(port->interrupt_in_buffer);
657 kfree(port->interrupt_out_buffer);
658 kfree(port);
661 static struct usb_serial * create_serial (struct usb_device *dev,
662 struct usb_interface *interface,
663 struct usb_serial_driver *driver)
665 struct usb_serial *serial;
667 serial = kzalloc(sizeof(*serial), GFP_KERNEL);
668 if (!serial) {
669 dev_err(&dev->dev, "%s - out of memory\n", __FUNCTION__);
670 return NULL;
672 serial->dev = usb_get_dev(dev);
673 serial->type = driver;
674 serial->interface = interface;
675 kref_init(&serial->kref);
676 mutex_init(&serial->disc_mutex);
677 serial->minor = SERIAL_TTY_NO_MINOR;
679 return serial;
682 static const struct usb_device_id *match_dynamic_id(struct usb_interface *intf,
683 struct usb_serial_driver *drv)
685 struct usb_dynid *dynid;
687 spin_lock(&drv->dynids.lock);
688 list_for_each_entry(dynid, &drv->dynids.list, node) {
689 if (usb_match_one_id(intf, &dynid->id)) {
690 spin_unlock(&drv->dynids.lock);
691 return &dynid->id;
694 spin_unlock(&drv->dynids.lock);
695 return NULL;
698 static const struct usb_device_id *get_iface_id(struct usb_serial_driver *drv,
699 struct usb_interface *intf)
701 const struct usb_device_id *id;
703 id = usb_match_id(intf, drv->id_table);
704 if (id) {
705 dbg("static descriptor matches");
706 goto exit;
708 id = match_dynamic_id(intf, drv);
709 if (id)
710 dbg("dynamic descriptor matches");
711 exit:
712 return id;
715 static struct usb_serial_driver *search_serial_device(struct usb_interface *iface)
717 const struct usb_device_id *id;
718 struct usb_serial_driver *drv;
720 /* Check if the usb id matches a known device */
721 list_for_each_entry(drv, &usb_serial_driver_list, driver_list) {
722 id = get_iface_id(drv, iface);
723 if (id)
724 return drv;
727 return NULL;
730 int usb_serial_probe(struct usb_interface *interface,
731 const struct usb_device_id *id)
733 struct usb_device *dev = interface_to_usbdev (interface);
734 struct usb_serial *serial = NULL;
735 struct usb_serial_port *port;
736 struct usb_host_interface *iface_desc;
737 struct usb_endpoint_descriptor *endpoint;
738 struct usb_endpoint_descriptor *interrupt_in_endpoint[MAX_NUM_PORTS];
739 struct usb_endpoint_descriptor *interrupt_out_endpoint[MAX_NUM_PORTS];
740 struct usb_endpoint_descriptor *bulk_in_endpoint[MAX_NUM_PORTS];
741 struct usb_endpoint_descriptor *bulk_out_endpoint[MAX_NUM_PORTS];
742 struct usb_serial_driver *type = NULL;
743 int retval;
744 unsigned int minor;
745 int buffer_size;
746 int i;
747 int num_interrupt_in = 0;
748 int num_interrupt_out = 0;
749 int num_bulk_in = 0;
750 int num_bulk_out = 0;
751 int num_ports = 0;
752 int max_endpoints;
754 lock_kernel(); /* guard against unloading a serial driver module */
755 type = search_serial_device(interface);
756 if (!type) {
757 unlock_kernel();
758 dbg("none matched");
759 return -ENODEV;
762 serial = create_serial (dev, interface, type);
763 if (!serial) {
764 unlock_kernel();
765 dev_err(&interface->dev, "%s - out of memory\n", __FUNCTION__);
766 return -ENOMEM;
769 /* if this device type has a probe function, call it */
770 if (type->probe) {
771 const struct usb_device_id *id;
773 if (!try_module_get(type->driver.owner)) {
774 unlock_kernel();
775 dev_err(&interface->dev, "module get failed, exiting\n");
776 kfree (serial);
777 return -EIO;
780 id = get_iface_id(type, interface);
781 retval = type->probe(serial, id);
782 module_put(type->driver.owner);
784 if (retval) {
785 unlock_kernel();
786 dbg ("sub driver rejected device");
787 kfree (serial);
788 return retval;
792 /* descriptor matches, let's find the endpoints needed */
793 /* check out the endpoints */
794 iface_desc = interface->cur_altsetting;
795 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
796 endpoint = &iface_desc->endpoint[i].desc;
798 if (usb_endpoint_is_bulk_in(endpoint)) {
799 /* we found a bulk in endpoint */
800 dbg("found bulk in on endpoint %d", i);
801 bulk_in_endpoint[num_bulk_in] = endpoint;
802 ++num_bulk_in;
805 if (usb_endpoint_is_bulk_out(endpoint)) {
806 /* we found a bulk out endpoint */
807 dbg("found bulk out on endpoint %d", i);
808 bulk_out_endpoint[num_bulk_out] = endpoint;
809 ++num_bulk_out;
812 if (usb_endpoint_is_int_in(endpoint)) {
813 /* we found a interrupt in endpoint */
814 dbg("found interrupt in on endpoint %d", i);
815 interrupt_in_endpoint[num_interrupt_in] = endpoint;
816 ++num_interrupt_in;
819 if (usb_endpoint_is_int_out(endpoint)) {
820 /* we found an interrupt out endpoint */
821 dbg("found interrupt out on endpoint %d", i);
822 interrupt_out_endpoint[num_interrupt_out] = endpoint;
823 ++num_interrupt_out;
827 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
828 /* BEGIN HORRIBLE HACK FOR PL2303 */
829 /* this is needed due to the looney way its endpoints are set up */
830 if (((le16_to_cpu(dev->descriptor.idVendor) == PL2303_VENDOR_ID) &&
831 (le16_to_cpu(dev->descriptor.idProduct) == PL2303_PRODUCT_ID)) ||
832 ((le16_to_cpu(dev->descriptor.idVendor) == ATEN_VENDOR_ID) &&
833 (le16_to_cpu(dev->descriptor.idProduct) == ATEN_PRODUCT_ID)) ||
834 ((le16_to_cpu(dev->descriptor.idVendor) == ALCOR_VENDOR_ID) &&
835 (le16_to_cpu(dev->descriptor.idProduct) == ALCOR_PRODUCT_ID)) ||
836 ((le16_to_cpu(dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
837 (le16_to_cpu(dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_EF81))) {
838 if (interface != dev->actconfig->interface[0]) {
839 /* check out the endpoints of the other interface*/
840 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
841 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
842 endpoint = &iface_desc->endpoint[i].desc;
843 if (usb_endpoint_is_int_in(endpoint)) {
844 /* we found a interrupt in endpoint */
845 dbg("found interrupt in for Prolific device on separate interface");
846 interrupt_in_endpoint[num_interrupt_in] = endpoint;
847 ++num_interrupt_in;
852 /* Now make sure the PL-2303 is configured correctly.
853 * If not, give up now and hope this hack will work
854 * properly during a later invocation of usb_serial_probe
856 if (num_bulk_in == 0 || num_bulk_out == 0) {
857 unlock_kernel();
858 dev_info(&interface->dev, "PL-2303 hack: descriptors matched but endpoints did not\n");
859 kfree (serial);
860 return -ENODEV;
863 /* END HORRIBLE HACK FOR PL2303 */
864 #endif
866 #ifdef CONFIG_USB_SERIAL_GENERIC
867 if (type == &usb_serial_generic_device) {
868 num_ports = num_bulk_out;
869 if (num_ports == 0) {
870 unlock_kernel();
871 dev_err(&interface->dev, "Generic device with no bulk out, not allowed.\n");
872 kfree (serial);
873 return -EIO;
876 #endif
877 if (!num_ports) {
878 /* if this device type has a calc_num_ports function, call it */
879 if (type->calc_num_ports) {
880 if (!try_module_get(type->driver.owner)) {
881 unlock_kernel();
882 dev_err(&interface->dev, "module get failed, exiting\n");
883 kfree (serial);
884 return -EIO;
886 num_ports = type->calc_num_ports (serial);
887 module_put(type->driver.owner);
889 if (!num_ports)
890 num_ports = type->num_ports;
893 serial->num_ports = num_ports;
894 serial->num_bulk_in = num_bulk_in;
895 serial->num_bulk_out = num_bulk_out;
896 serial->num_interrupt_in = num_interrupt_in;
897 serial->num_interrupt_out = num_interrupt_out;
899 #if 0
900 /* check that the device meets the driver's requirements */
901 if ((type->num_interrupt_in != NUM_DONT_CARE &&
902 type->num_interrupt_in != num_interrupt_in)
903 || (type->num_interrupt_out != NUM_DONT_CARE &&
904 type->num_interrupt_out != num_interrupt_out)
905 || (type->num_bulk_in != NUM_DONT_CARE &&
906 type->num_bulk_in != num_bulk_in)
907 || (type->num_bulk_out != NUM_DONT_CARE &&
908 type->num_bulk_out != num_bulk_out)) {
909 dbg("wrong number of endpoints");
910 kfree(serial);
911 return -EIO;
913 #endif
915 /* found all that we need */
916 dev_info(&interface->dev, "%s converter detected\n",
917 type->description);
919 /* create our ports, we need as many as the max endpoints */
920 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
921 max_endpoints = max(num_bulk_in, num_bulk_out);
922 max_endpoints = max(max_endpoints, num_interrupt_in);
923 max_endpoints = max(max_endpoints, num_interrupt_out);
924 max_endpoints = max(max_endpoints, (int)serial->num_ports);
925 serial->num_port_pointers = max_endpoints;
926 unlock_kernel();
928 dbg("%s - setting up %d port structures for this device", __FUNCTION__, max_endpoints);
929 for (i = 0; i < max_endpoints; ++i) {
930 port = kzalloc(sizeof(struct usb_serial_port), GFP_KERNEL);
931 if (!port)
932 goto probe_error;
933 port->serial = serial;
934 spin_lock_init(&port->lock);
935 mutex_init(&port->mutex);
936 INIT_WORK(&port->work, usb_serial_port_work);
937 serial->port[i] = port;
940 /* set up the endpoint information */
941 for (i = 0; i < num_bulk_in; ++i) {
942 endpoint = bulk_in_endpoint[i];
943 port = serial->port[i];
944 port->read_urb = usb_alloc_urb (0, GFP_KERNEL);
945 if (!port->read_urb) {
946 dev_err(&interface->dev, "No free urbs available\n");
947 goto probe_error;
949 buffer_size = le16_to_cpu((endpoint->wMaxPacketSize > maxSize) ?
950 endpoint->wMaxPacketSize : maxSize);
951 port->bulk_in_size = buffer_size;
952 port->bulk_in_endpointAddress = endpoint->bEndpointAddress;
953 port->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
954 if (!port->bulk_in_buffer) {
955 dev_err(&interface->dev, "Couldn't allocate bulk_in_buffer\n");
956 goto probe_error;
958 usb_fill_bulk_urb (port->read_urb, dev,
959 usb_rcvbulkpipe (dev,
960 endpoint->bEndpointAddress),
961 port->bulk_in_buffer, buffer_size,
962 serial->type->read_bulk_callback,
963 port);
966 for (i = 0; i < num_bulk_out; ++i) {
967 endpoint = bulk_out_endpoint[i];
968 port = serial->port[i];
969 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
970 if (!port->write_urb) {
971 dev_err(&interface->dev, "No free urbs available\n");
972 goto probe_error;
974 buffer_size = le16_to_cpu((endpoint->wMaxPacketSize > maxSize) ?
975 endpoint->wMaxPacketSize : maxSize);
976 port->bulk_out_size = buffer_size;
977 port->bulk_out_endpointAddress = endpoint->bEndpointAddress;
978 port->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
979 if (!port->bulk_out_buffer) {
980 dev_err(&interface->dev, "Couldn't allocate bulk_out_buffer\n");
981 goto probe_error;
983 usb_fill_bulk_urb (port->write_urb, dev,
984 usb_sndbulkpipe (dev,
985 endpoint->bEndpointAddress),
986 port->bulk_out_buffer, buffer_size,
987 serial->type->write_bulk_callback,
988 port);
991 if (serial->type->read_int_callback) {
992 for (i = 0; i < num_interrupt_in; ++i) {
993 endpoint = interrupt_in_endpoint[i];
994 port = serial->port[i];
995 port->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
996 if (!port->interrupt_in_urb) {
997 dev_err(&interface->dev, "No free urbs available\n");
998 goto probe_error;
1000 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1001 port->interrupt_in_endpointAddress = endpoint->bEndpointAddress;
1002 port->interrupt_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1003 if (!port->interrupt_in_buffer) {
1004 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
1005 goto probe_error;
1007 usb_fill_int_urb (port->interrupt_in_urb, dev,
1008 usb_rcvintpipe (dev,
1009 endpoint->bEndpointAddress),
1010 port->interrupt_in_buffer, buffer_size,
1011 serial->type->read_int_callback, port,
1012 endpoint->bInterval);
1014 } else if (num_interrupt_in) {
1015 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
1018 if (serial->type->write_int_callback) {
1019 for (i = 0; i < num_interrupt_out; ++i) {
1020 endpoint = interrupt_out_endpoint[i];
1021 port = serial->port[i];
1022 port->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
1023 if (!port->interrupt_out_urb) {
1024 dev_err(&interface->dev, "No free urbs available\n");
1025 goto probe_error;
1027 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
1028 port->interrupt_out_size = buffer_size;
1029 port->interrupt_out_endpointAddress = endpoint->bEndpointAddress;
1030 port->interrupt_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
1031 if (!port->interrupt_out_buffer) {
1032 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
1033 goto probe_error;
1035 usb_fill_int_urb (port->interrupt_out_urb, dev,
1036 usb_sndintpipe (dev,
1037 endpoint->bEndpointAddress),
1038 port->interrupt_out_buffer, buffer_size,
1039 serial->type->write_int_callback, port,
1040 endpoint->bInterval);
1042 } else if (num_interrupt_out) {
1043 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
1046 /* if this device type has an attach function, call it */
1047 if (type->attach) {
1048 if (!try_module_get(type->driver.owner)) {
1049 dev_err(&interface->dev, "module get failed, exiting\n");
1050 goto probe_error;
1052 retval = type->attach (serial);
1053 module_put(type->driver.owner);
1054 if (retval < 0)
1055 goto probe_error;
1056 if (retval > 0) {
1057 /* quietly accept this device, but don't bind to a serial port
1058 * as it's about to disappear */
1059 serial->num_ports = 0;
1060 goto exit;
1064 if (get_free_serial (serial, num_ports, &minor) == NULL) {
1065 dev_err(&interface->dev, "No more free serial devices\n");
1066 goto probe_error;
1068 serial->minor = minor;
1070 /* register all of the individual ports with the driver core */
1071 for (i = 0; i < num_ports; ++i) {
1072 port = serial->port[i];
1073 port->dev.parent = &interface->dev;
1074 port->dev.driver = NULL;
1075 port->dev.bus = &usb_serial_bus_type;
1076 port->dev.release = &port_release;
1078 snprintf (&port->dev.bus_id[0], sizeof(port->dev.bus_id), "ttyUSB%d", port->number);
1079 dbg ("%s - registering %s", __FUNCTION__, port->dev.bus_id);
1080 retval = device_register(&port->dev);
1081 if (retval)
1082 dev_err(&port->dev, "Error registering port device, "
1083 "continuing\n");
1086 usb_serial_console_init (debug, minor);
1088 exit:
1089 /* success */
1090 usb_set_intfdata (interface, serial);
1091 return 0;
1093 probe_error:
1094 for (i = 0; i < num_bulk_in; ++i) {
1095 port = serial->port[i];
1096 if (!port)
1097 continue;
1098 usb_free_urb(port->read_urb);
1099 kfree(port->bulk_in_buffer);
1101 for (i = 0; i < num_bulk_out; ++i) {
1102 port = serial->port[i];
1103 if (!port)
1104 continue;
1105 usb_free_urb(port->write_urb);
1106 kfree(port->bulk_out_buffer);
1108 for (i = 0; i < num_interrupt_in; ++i) {
1109 port = serial->port[i];
1110 if (!port)
1111 continue;
1112 usb_free_urb(port->interrupt_in_urb);
1113 kfree(port->interrupt_in_buffer);
1115 for (i = 0; i < num_interrupt_out; ++i) {
1116 port = serial->port[i];
1117 if (!port)
1118 continue;
1119 usb_free_urb(port->interrupt_out_urb);
1120 kfree(port->interrupt_out_buffer);
1123 /* free up any memory that we allocated */
1124 for (i = 0; i < serial->num_port_pointers; ++i)
1125 kfree(serial->port[i]);
1126 kfree (serial);
1127 return -EIO;
1130 void usb_serial_disconnect(struct usb_interface *interface)
1132 int i;
1133 struct usb_serial *serial = usb_get_intfdata (interface);
1134 struct device *dev = &interface->dev;
1135 struct usb_serial_port *port;
1137 usb_serial_console_disconnect(serial);
1138 dbg ("%s", __FUNCTION__);
1140 mutex_lock(&serial->disc_mutex);
1141 usb_set_intfdata (interface, NULL);
1142 /* must set a flag, to signal subdrivers */
1143 serial->disconnected = 1;
1144 mutex_unlock(&serial->disc_mutex);
1146 /* Unfortunately, many of the sub-drivers expect the port structures
1147 * to exist when their shutdown method is called, so we have to go
1148 * through this awkward two-step unregistration procedure.
1150 for (i = 0; i < serial->num_ports; ++i) {
1151 port = serial->port[i];
1152 if (port) {
1153 if (port->tty)
1154 tty_hangup(port->tty);
1155 kill_traffic(port);
1156 cancel_work_sync(&port->work);
1157 device_del(&port->dev);
1160 serial->type->shutdown(serial);
1161 for (i = 0; i < serial->num_ports; ++i) {
1162 port = serial->port[i];
1163 if (port) {
1164 put_device(&port->dev);
1165 serial->port[i] = NULL;
1169 /* let the last holder of this object
1170 * cause it to be cleaned up */
1171 usb_serial_put(serial);
1172 dev_info(dev, "device disconnected\n");
1175 int usb_serial_suspend(struct usb_interface *intf, pm_message_t message)
1177 struct usb_serial *serial = usb_get_intfdata(intf);
1178 struct usb_serial_port *port;
1179 int i, r = 0;
1181 serial->suspending = 1;
1183 for (i = 0; i < serial->num_ports; ++i) {
1184 port = serial->port[i];
1185 if (port)
1186 kill_traffic(port);
1189 if (serial->type->suspend)
1190 r = serial->type->suspend(serial, message);
1192 return r;
1194 EXPORT_SYMBOL(usb_serial_suspend);
1196 int usb_serial_resume(struct usb_interface *intf)
1198 struct usb_serial *serial = usb_get_intfdata(intf);
1200 serial->suspending = 0;
1201 if (serial->type->resume)
1202 return serial->type->resume(serial);
1204 return 0;
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 .write_room = serial_write_room,
1213 .ioctl = serial_ioctl,
1214 .set_termios = serial_set_termios,
1215 .throttle = serial_throttle,
1216 .unthrottle = serial_unthrottle,
1217 .break_ctl = serial_break,
1218 .chars_in_buffer = serial_chars_in_buffer,
1219 .read_proc = serial_read_proc,
1220 .tiocmget = serial_tiocmget,
1221 .tiocmset = serial_tiocmset,
1224 struct tty_driver *usb_serial_tty_driver;
1226 static int __init usb_serial_init(void)
1228 int i;
1229 int result;
1231 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1232 if (!usb_serial_tty_driver)
1233 return -ENOMEM;
1235 /* Initialize our global data */
1236 for (i = 0; i < SERIAL_TTY_MINORS; ++i) {
1237 serial_table[i] = NULL;
1240 result = bus_register(&usb_serial_bus_type);
1241 if (result) {
1242 err("%s - registering bus driver failed", __FUNCTION__);
1243 goto exit_bus;
1246 usb_serial_tty_driver->owner = THIS_MODULE;
1247 usb_serial_tty_driver->driver_name = "usbserial";
1248 usb_serial_tty_driver->name = "ttyUSB";
1249 usb_serial_tty_driver->major = SERIAL_TTY_MAJOR;
1250 usb_serial_tty_driver->minor_start = 0;
1251 usb_serial_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1252 usb_serial_tty_driver->subtype = SERIAL_TYPE_NORMAL;
1253 usb_serial_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
1254 usb_serial_tty_driver->init_termios = tty_std_termios;
1255 usb_serial_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1256 usb_serial_tty_driver->init_termios.c_ispeed = 9600;
1257 usb_serial_tty_driver->init_termios.c_ospeed = 9600;
1258 tty_set_operations(usb_serial_tty_driver, &serial_ops);
1259 result = tty_register_driver(usb_serial_tty_driver);
1260 if (result) {
1261 err("%s - tty_register_driver failed", __FUNCTION__);
1262 goto exit_reg_driver;
1265 /* register the USB driver */
1266 result = usb_register(&usb_serial_driver);
1267 if (result < 0) {
1268 err("%s - usb_register failed", __FUNCTION__);
1269 goto exit_tty;
1272 /* register the generic driver, if we should */
1273 result = usb_serial_generic_register(debug);
1274 if (result < 0) {
1275 err("%s - registering generic driver failed", __FUNCTION__);
1276 goto exit_generic;
1279 info(DRIVER_DESC);
1281 return result;
1283 exit_generic:
1284 usb_deregister(&usb_serial_driver);
1286 exit_tty:
1287 tty_unregister_driver(usb_serial_tty_driver);
1289 exit_reg_driver:
1290 bus_unregister(&usb_serial_bus_type);
1292 exit_bus:
1293 err ("%s - returning with error %d", __FUNCTION__, result);
1294 put_tty_driver(usb_serial_tty_driver);
1295 return result;
1299 static void __exit usb_serial_exit(void)
1301 usb_serial_console_exit();
1303 usb_serial_generic_deregister();
1305 usb_deregister(&usb_serial_driver);
1306 tty_unregister_driver(usb_serial_tty_driver);
1307 put_tty_driver(usb_serial_tty_driver);
1308 bus_unregister(&usb_serial_bus_type);
1312 module_init(usb_serial_init);
1313 module_exit(usb_serial_exit);
1315 #define set_to_generic_if_null(type, function) \
1316 do { \
1317 if (!type->function) { \
1318 type->function = usb_serial_generic_##function; \
1319 dbg("Had to override the " #function \
1320 " usb serial operation with the generic one.");\
1322 } while (0)
1324 static void fixup_generic(struct usb_serial_driver *device)
1326 set_to_generic_if_null(device, open);
1327 set_to_generic_if_null(device, write);
1328 set_to_generic_if_null(device, close);
1329 set_to_generic_if_null(device, write_room);
1330 set_to_generic_if_null(device, chars_in_buffer);
1331 set_to_generic_if_null(device, read_bulk_callback);
1332 set_to_generic_if_null(device, write_bulk_callback);
1333 set_to_generic_if_null(device, shutdown);
1334 set_to_generic_if_null(device, resume);
1337 int usb_serial_register(struct usb_serial_driver *driver) /* must be called with BKL held */
1339 int retval;
1341 if (usb_disabled())
1342 return -ENODEV;
1344 fixup_generic(driver);
1346 if (!driver->description)
1347 driver->description = driver->driver.name;
1349 /* Add this device to our list of devices */
1350 list_add(&driver->driver_list, &usb_serial_driver_list);
1352 retval = usb_serial_bus_register(driver);
1353 if (retval) {
1354 err("problem %d when registering driver %s", retval, driver->description);
1355 list_del(&driver->driver_list);
1357 else
1358 info("USB Serial support registered for %s", driver->description);
1360 return retval;
1364 void usb_serial_deregister(struct usb_serial_driver *device) /* must be called with BKL held */
1366 info("USB Serial deregistering driver %s", device->description);
1367 list_del(&device->driver_list);
1368 usb_serial_bus_deregister(device);
1373 /* If the usb-serial core is built into the core, the usb-serial drivers
1374 need these symbols to load properly as modules. */
1375 EXPORT_SYMBOL_GPL(usb_serial_register);
1376 EXPORT_SYMBOL_GPL(usb_serial_deregister);
1377 EXPORT_SYMBOL_GPL(usb_serial_probe);
1378 EXPORT_SYMBOL_GPL(usb_serial_disconnect);
1379 EXPORT_SYMBOL_GPL(usb_serial_port_softint);
1382 /* Module information */
1383 MODULE_AUTHOR( DRIVER_AUTHOR );
1384 MODULE_DESCRIPTION( DRIVER_DESC );
1385 MODULE_LICENSE("GPL");
1387 module_param(debug, bool, S_IRUGO | S_IWUSR);
1388 MODULE_PARM_DESC(debug, "Debug enabled or not");
1389 module_param(maxSize, ushort, 0);
1390 MODULE_PARM_DESC(maxSize, "User specified USB endpoint size");