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>
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
= {
47 .probe
= usb_serial_probe
,
48 .disconnect
= usb_serial_disconnect
,
49 .suspend
= usb_serial_suspend
,
50 .resume
= usb_serial_resume
,
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
61 static ushort maxSize
= 0;
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
];
75 kref_get(&serial
->kref
);
76 mutex_unlock(&table_lock
);
80 static struct usb_serial
*get_free_serial (struct usb_serial
*serial
, int num_ports
, unsigned int *minor
)
85 dbg("%s %d", __FUNCTION__
, num_ports
);
88 mutex_lock(&table_lock
);
89 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
94 for (j
= 1; j
<= num_ports
-1; ++j
)
95 if ((i
+j
>= SERIAL_TTY_MINORS
) || (serial_table
[i
+j
])) {
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
);
113 mutex_unlock(&table_lock
);
117 static void return_serial(struct usb_serial
*serial
)
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
;
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
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
];
154 usb_put_dev(serial
->dev
);
156 /* free up any memory that we allocated */
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
;
177 dbg("%s", __FUNCTION__
);
179 /* get the serial object associated with this tty pointer */
180 serial
= usb_serial_get_by_index(tty
->index
);
182 tty
->driver_data
= NULL
;
186 mutex_lock(&serial
->disc_mutex
);
187 portNumber
= tty
->index
- serial
->minor
;
188 port
= serial
->port
[portNumber
];
189 if (!port
|| serial
->disconnected
)
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
);
199 goto bailout_serial_put
;
201 if (mutex_lock_interruptible(&port
->mutex
)) {
202 retval
= -ERESTARTSYS
;
203 goto bailout_port_put
;
208 /* set up our port structure making the tty driver
209 * remember our port object, and us it */
210 tty
->driver_data
= port
;
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
)) {
220 goto bailout_mutex_unlock
;
223 mutex_lock(&serial
->disc_mutex
);
224 if (serial
->disconnected
)
227 retval
= usb_autopm_get_interface(serial
->interface
);
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
);
235 goto bailout_interface_put
;
236 mutex_unlock(&serial
->disc_mutex
);
239 mutex_unlock(&port
->mutex
);
242 bailout_interface_put
:
243 usb_autopm_put_interface(serial
->interface
);
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
;
251 mutex_unlock(&port
->mutex
);
253 put_device(&port
->dev
);
255 usb_serial_put(serial
);
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
;
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
);
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
286 serial
->type
->close(port
, filp
);
288 if (port
->open_count
== (port
->console
? 2 : 0)) {
290 /* We must do this before we drop the port count to
292 if (port
->tty
->driver_data
)
293 port
->tty
->driver_data
= NULL
;
299 count
= port
->open_count
;
300 mutex_unlock(&port
->mutex
);
301 put_device(&port
->dev
);
303 /* Mustn't dereference port any more */
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 */
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
)
325 dbg("%s - port %d, %d byte(s)", __FUNCTION__
, port
->number
, count
);
327 if (!port
->open_count
) {
329 dbg("%s - port not opened", __FUNCTION__
);
333 /* pass on to the driver specific version of this function */
334 retval
= port
->serial
->type
->write(port
, buf
, count
);
340 static int serial_write_room (struct tty_struct
*tty
)
342 struct usb_serial_port
*port
= tty
->driver_data
;
343 int retval
= -ENODEV
;
348 dbg("%s - port %d", __FUNCTION__
, port
->number
);
350 if (!port
->open_count
) {
351 dbg("%s - port not open", __FUNCTION__
);
355 /* pass on to the driver specific version of this function */
356 retval
= port
->serial
->type
->write_room(port
);
362 static int serial_chars_in_buffer (struct tty_struct
*tty
)
364 struct usb_serial_port
*port
= tty
->driver_data
;
365 int retval
= -ENODEV
;
370 dbg("%s = port %d", __FUNCTION__
, port
->number
);
372 if (!port
->open_count
) {
373 dbg("%s - port not open", __FUNCTION__
);
377 /* if the device was unplugged then any remaining characters
378 fell out of the connector ;) */
379 if (port
->serial
->disconnected
)
381 /* pass on to the driver specific version of this function */
382 retval
= port
->serial
->type
->chars_in_buffer(port
);
388 static void serial_throttle (struct tty_struct
* tty
)
390 struct usb_serial_port
*port
= tty
->driver_data
;
395 dbg("%s - port %d", __FUNCTION__
, port
->number
);
397 if (!port
->open_count
) {
398 dbg ("%s - port not open", __FUNCTION__
);
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
;
414 dbg("%s - port %d", __FUNCTION__
, port
->number
);
416 if (!port
->open_count
) {
417 dbg("%s - port not open", __FUNCTION__
);
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
;
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__
);
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
);
447 retval
= -ENOIOCTLCMD
;
453 static void serial_set_termios (struct tty_struct
*tty
, struct ktermios
* old
)
455 struct usb_serial_port
*port
= tty
->driver_data
;
460 dbg("%s - port %d", __FUNCTION__
, port
->number
);
462 if (!port
->open_count
) {
463 dbg("%s - port not open", __FUNCTION__
);
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
);
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
;
484 dbg("%s - port %d", __FUNCTION__
, port
->number
);
486 if (!port
->open_count
) {
487 dbg("%s - port not open", __FUNCTION__
);
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
);
498 static int serial_read_proc (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
500 struct usb_serial
*serial
;
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
);
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
);
531 if ((length
+ begin
) < off
) {
535 usb_serial_put(serial
);
539 if (off
>= (length
+ begin
))
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
;
552 dbg("%s - port %d", __FUNCTION__
, port
->number
);
554 if (!port
->open_count
) {
555 dbg("%s - port not open", __FUNCTION__
);
559 if (port
->serial
->type
->tiocmget
)
560 return port
->serial
->type
->tiocmget(port
, file
);
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
;
573 dbg("%s - port %d", __FUNCTION__
, port
->number
);
575 if (!port
->open_count
) {
576 dbg("%s - port not open", __FUNCTION__
);
580 if (port
->serial
->type
->tiocmset
)
581 return port
->serial
->type
->tiocmset(port
, file
, set
, clear
);
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
;
605 dbg("%s - port %d", __FUNCTION__
, port
->number
);
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
);
622 static void kill_traffic(struct usb_serial_port
*port
)
624 usb_kill_urb(port
->read_urb
);
625 usb_kill_urb(port
->write_urb
);
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.
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
);
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
);
669 dev_err(&dev
->dev
, "%s - out of memory\n", __FUNCTION__
);
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
;
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
);
694 spin_unlock(&drv
->dynids
.lock
);
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
);
705 dbg("static descriptor matches");
708 id
= match_dynamic_id(intf
, drv
);
710 dbg("dynamic descriptor matches");
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
);
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
;
747 int num_interrupt_in
= 0;
748 int num_interrupt_out
= 0;
750 int num_bulk_out
= 0;
754 lock_kernel(); /* guard against unloading a serial driver module */
755 type
= search_serial_device(interface
);
762 serial
= create_serial (dev
, interface
, type
);
765 dev_err(&interface
->dev
, "%s - out of memory\n", __FUNCTION__
);
769 /* if this device type has a probe function, call it */
771 const struct usb_device_id
*id
;
773 if (!try_module_get(type
->driver
.owner
)) {
775 dev_err(&interface
->dev
, "module get failed, exiting\n");
780 id
= get_iface_id(type
, interface
);
781 retval
= type
->probe(serial
, id
);
782 module_put(type
->driver
.owner
);
786 dbg ("sub driver rejected device");
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
;
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
;
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
;
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
;
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
;
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) {
858 dev_info(&interface
->dev
, "PL-2303 hack: descriptors matched but endpoints did not\n");
863 /* END HORRIBLE HACK FOR PL2303 */
866 #ifdef CONFIG_USB_SERIAL_GENERIC
867 if (type
== &usb_serial_generic_device
) {
868 num_ports
= num_bulk_out
;
869 if (num_ports
== 0) {
871 dev_err(&interface
->dev
, "Generic device with no bulk out, not allowed.\n");
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
)) {
882 dev_err(&interface
->dev
, "module get failed, exiting\n");
886 num_ports
= type
->calc_num_ports (serial
);
887 module_put(type
->driver
.owner
);
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
;
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");
915 /* found all that we need */
916 dev_info(&interface
->dev
, "%s converter detected\n",
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
;
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
);
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");
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");
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
,
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");
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");
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
,
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");
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");
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");
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");
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 */
1048 if (!try_module_get(type
->driver
.owner
)) {
1049 dev_err(&interface
->dev
, "module get failed, exiting\n");
1052 retval
= type
->attach (serial
);
1053 module_put(type
->driver
.owner
);
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;
1064 if (get_free_serial (serial
, num_ports
, &minor
) == NULL
) {
1065 dev_err(&interface
->dev
, "No more free serial devices\n");
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
);
1082 dev_err(&port
->dev
, "Error registering port device, "
1086 usb_serial_console_init (debug
, minor
);
1090 usb_set_intfdata (interface
, serial
);
1094 for (i
= 0; i
< num_bulk_in
; ++i
) {
1095 port
= serial
->port
[i
];
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
];
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
];
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
];
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
]);
1130 void usb_serial_disconnect(struct usb_interface
*interface
)
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
];
1154 tty_hangup(port
->tty
);
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
];
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
;
1181 serial
->suspending
= 1;
1183 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1184 port
= serial
->port
[i
];
1189 if (serial
->type
->suspend
)
1190 r
= serial
->type
->suspend(serial
, message
);
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
);
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)
1231 usb_serial_tty_driver
= alloc_tty_driver(SERIAL_TTY_MINORS
);
1232 if (!usb_serial_tty_driver
)
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
);
1242 err("%s - registering bus driver failed", __FUNCTION__
);
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
);
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
);
1268 err("%s - usb_register failed", __FUNCTION__
);
1272 /* register the generic driver, if we should */
1273 result
= usb_serial_generic_register(debug
);
1275 err("%s - registering generic driver failed", __FUNCTION__
);
1284 usb_deregister(&usb_serial_driver
);
1287 tty_unregister_driver(usb_serial_tty_driver
);
1290 bus_unregister(&usb_serial_bus_type
);
1293 err ("%s - returning with error %d", __FUNCTION__
, result
);
1294 put_tty_driver(usb_serial_tty_driver
);
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) \
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.");\
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 */
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
);
1354 err("problem %d when registering driver %s", retval
, driver
->description
);
1355 list_del(&driver
->driver_list
);
1358 info("USB Serial support registered for %s", driver
->description
);
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");