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 <linux/smp_lock.h>
32 #include <asm/uaccess.h>
33 #include <linux/usb.h>
34 #include "usb-serial.h"
40 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
41 #define DRIVER_DESC "USB Serial Driver core"
43 /* Driver structure we register with the USB core */
44 static struct usb_driver usb_serial_driver
= {
46 .probe
= usb_serial_probe
,
47 .disconnect
= usb_serial_disconnect
,
51 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
52 the MODULE_DEVICE_TABLE declarations in each serial driver
53 cause the "hotplug" program to pull in whatever module is necessary
54 via modprobe, and modprobe will load usbserial because the serial
59 static struct usb_serial
*serial_table
[SERIAL_TTY_MINORS
]; /* initially all NULL */
60 static LIST_HEAD(usb_serial_driver_list
);
62 struct usb_serial
*usb_serial_get_by_index(unsigned index
)
64 struct usb_serial
*serial
= serial_table
[index
];
67 kref_get(&serial
->kref
);
71 static struct usb_serial
*get_free_serial (struct usb_serial
*serial
, int num_ports
, unsigned int *minor
)
76 dbg("%s %d", __FUNCTION__
, num_ports
);
79 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
84 for (j
= 1; j
<= num_ports
-1; ++j
)
85 if ((i
+j
>= SERIAL_TTY_MINORS
) || (serial_table
[i
+j
])) {
94 dbg("%s - minor base = %d", __FUNCTION__
, *minor
);
95 for (i
= *minor
; (i
< (*minor
+ num_ports
)) && (i
< SERIAL_TTY_MINORS
); ++i
)
96 serial_table
[i
] = serial
;
102 static void return_serial(struct usb_serial
*serial
)
106 dbg("%s", __FUNCTION__
);
111 for (i
= 0; i
< serial
->num_ports
; ++i
) {
112 serial_table
[serial
->minor
+ i
] = NULL
;
116 static void destroy_serial(struct kref
*kref
)
118 struct usb_serial
*serial
;
119 struct usb_serial_port
*port
;
122 serial
= to_usb_serial(kref
);
124 dbg("%s - %s", __FUNCTION__
, serial
->type
->description
);
126 serial
->type
->shutdown(serial
);
128 /* return the minor range that this device had */
129 return_serial(serial
);
131 for (i
= 0; i
< serial
->num_ports
; ++i
)
132 serial
->port
[i
]->open_count
= 0;
134 /* the ports are cleaned up and released in port_release() */
135 for (i
= 0; i
< serial
->num_ports
; ++i
)
136 if (serial
->port
[i
]->dev
.parent
!= NULL
) {
137 device_unregister(&serial
->port
[i
]->dev
);
138 serial
->port
[i
] = NULL
;
141 /* If this is a "fake" port, we have to clean it up here, as it will
142 * not get cleaned up in port_release() as it was never registered with
144 if (serial
->num_ports
< serial
->num_port_pointers
) {
145 for (i
= serial
->num_ports
; i
< serial
->num_port_pointers
; ++i
) {
146 port
= serial
->port
[i
];
149 usb_kill_urb(port
->read_urb
);
150 usb_free_urb(port
->read_urb
);
151 usb_kill_urb(port
->write_urb
);
152 usb_free_urb(port
->write_urb
);
153 usb_kill_urb(port
->interrupt_in_urb
);
154 usb_free_urb(port
->interrupt_in_urb
);
155 usb_kill_urb(port
->interrupt_out_urb
);
156 usb_free_urb(port
->interrupt_out_urb
);
157 kfree(port
->bulk_in_buffer
);
158 kfree(port
->bulk_out_buffer
);
159 kfree(port
->interrupt_in_buffer
);
160 kfree(port
->interrupt_out_buffer
);
164 flush_scheduled_work(); /* port->work */
166 usb_put_dev(serial
->dev
);
168 /* free up any memory that we allocated */
172 void usb_serial_put(struct usb_serial
*serial
)
174 kref_put(&serial
->kref
, destroy_serial
);
177 /*****************************************************************************
178 * Driver tty interface functions
179 *****************************************************************************/
180 static int serial_open (struct tty_struct
*tty
, struct file
* filp
)
182 struct usb_serial
*serial
;
183 struct usb_serial_port
*port
;
184 unsigned int portNumber
;
187 dbg("%s", __FUNCTION__
);
189 /* get the serial object associated with this tty pointer */
190 serial
= usb_serial_get_by_index(tty
->index
);
192 tty
->driver_data
= NULL
;
196 portNumber
= tty
->index
- serial
->minor
;
197 port
= serial
->port
[portNumber
];
200 goto bailout_kref_put
;
203 if (mutex_lock_interruptible(&port
->mutex
)) {
204 retval
= -ERESTARTSYS
;
205 goto bailout_kref_put
;
210 /* set up our port structure making the tty driver
211 * remember our port object, and us it */
212 tty
->driver_data
= port
;
215 if (port
->open_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
)) {
222 goto bailout_mutex_unlock
;
225 /* only call the device specific open if this
226 * is the first time the port is opened */
227 retval
= serial
->type
->open(port
, filp
);
229 goto bailout_module_put
;
232 mutex_unlock(&port
->mutex
);
236 module_put(serial
->type
->driver
.owner
);
237 bailout_mutex_unlock
:
238 port
->open_count
= 0;
239 tty
->driver_data
= NULL
;
241 mutex_unlock(&port
->mutex
);
243 usb_serial_put(serial
);
247 static void serial_close(struct tty_struct
*tty
, struct file
* filp
)
249 struct usb_serial_port
*port
= tty
->driver_data
;
254 dbg("%s - port %d", __FUNCTION__
, port
->number
);
256 mutex_lock(&port
->mutex
);
258 if (port
->open_count
== 0) {
259 mutex_unlock(&port
->mutex
);
264 if (port
->open_count
== 0) {
265 /* only call the device specific close if this
266 * port is being closed by the last owner */
267 port
->serial
->type
->close(port
, filp
);
270 if (port
->tty
->driver_data
)
271 port
->tty
->driver_data
= NULL
;
275 module_put(port
->serial
->type
->driver
.owner
);
278 mutex_unlock(&port
->mutex
);
279 usb_serial_put(port
->serial
);
282 static int serial_write (struct tty_struct
* tty
, const unsigned char *buf
, int count
)
284 struct usb_serial_port
*port
= tty
->driver_data
;
285 int retval
= -EINVAL
;
287 if (!port
|| port
->serial
->dev
->state
== USB_STATE_NOTATTACHED
)
290 dbg("%s - port %d, %d byte(s)", __FUNCTION__
, port
->number
, count
);
292 if (!port
->open_count
) {
293 dbg("%s - port not opened", __FUNCTION__
);
297 /* pass on to the driver specific version of this function */
298 retval
= port
->serial
->type
->write(port
, buf
, count
);
304 static int serial_write_room (struct tty_struct
*tty
)
306 struct usb_serial_port
*port
= tty
->driver_data
;
307 int retval
= -ENODEV
;
312 dbg("%s - port %d", __FUNCTION__
, port
->number
);
314 if (!port
->open_count
) {
315 dbg("%s - port not open", __FUNCTION__
);
319 /* pass on to the driver specific version of this function */
320 retval
= port
->serial
->type
->write_room(port
);
326 static int serial_chars_in_buffer (struct tty_struct
*tty
)
328 struct usb_serial_port
*port
= tty
->driver_data
;
329 int retval
= -ENODEV
;
334 dbg("%s = port %d", __FUNCTION__
, port
->number
);
336 if (!port
->open_count
) {
337 dbg("%s - port not open", __FUNCTION__
);
341 /* pass on to the driver specific version of this function */
342 retval
= port
->serial
->type
->chars_in_buffer(port
);
348 static void serial_throttle (struct tty_struct
* tty
)
350 struct usb_serial_port
*port
= tty
->driver_data
;
355 dbg("%s - port %d", __FUNCTION__
, port
->number
);
357 if (!port
->open_count
) {
358 dbg ("%s - port not open", __FUNCTION__
);
362 /* pass on to the driver specific version of this function */
363 if (port
->serial
->type
->throttle
)
364 port
->serial
->type
->throttle(port
);
367 static void serial_unthrottle (struct tty_struct
* tty
)
369 struct usb_serial_port
*port
= tty
->driver_data
;
374 dbg("%s - port %d", __FUNCTION__
, port
->number
);
376 if (!port
->open_count
) {
377 dbg("%s - port not open", __FUNCTION__
);
381 /* pass on to the driver specific version of this function */
382 if (port
->serial
->type
->unthrottle
)
383 port
->serial
->type
->unthrottle(port
);
386 static int serial_ioctl (struct tty_struct
*tty
, struct file
* file
, unsigned int cmd
, unsigned long arg
)
388 struct usb_serial_port
*port
= tty
->driver_data
;
389 int retval
= -ENODEV
;
394 dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__
, port
->number
, cmd
);
396 if (!port
->open_count
) {
397 dbg ("%s - port not open", __FUNCTION__
);
401 /* pass on to the driver specific version of this function if it is available */
402 if (port
->serial
->type
->ioctl
)
403 retval
= port
->serial
->type
->ioctl(port
, file
, cmd
, arg
);
405 retval
= -ENOIOCTLCMD
;
411 static void serial_set_termios (struct tty_struct
*tty
, struct termios
* old
)
413 struct usb_serial_port
*port
= tty
->driver_data
;
418 dbg("%s - port %d", __FUNCTION__
, port
->number
);
420 if (!port
->open_count
) {
421 dbg("%s - port not open", __FUNCTION__
);
425 /* pass on to the driver specific version of this function if it is available */
426 if (port
->serial
->type
->set_termios
)
427 port
->serial
->type
->set_termios(port
, old
);
430 static void serial_break (struct tty_struct
*tty
, int break_state
)
432 struct usb_serial_port
*port
= tty
->driver_data
;
437 dbg("%s - port %d", __FUNCTION__
, port
->number
);
439 if (!port
->open_count
) {
440 dbg("%s - port not open", __FUNCTION__
);
444 /* pass on to the driver specific version of this function if it is available */
445 if (port
->serial
->type
->break_ctl
)
446 port
->serial
->type
->break_ctl(port
, break_state
);
449 static int serial_read_proc (char *page
, char **start
, off_t off
, int count
, int *eof
, void *data
)
451 struct usb_serial
*serial
;
457 dbg("%s", __FUNCTION__
);
458 length
+= sprintf (page
, "usbserinfo:1.0 driver:2.0\n");
459 for (i
= 0; i
< SERIAL_TTY_MINORS
&& length
< PAGE_SIZE
; ++i
) {
460 serial
= usb_serial_get_by_index(i
);
464 length
+= sprintf (page
+length
, "%d:", i
);
465 if (serial
->type
->driver
.owner
)
466 length
+= sprintf (page
+length
, " module:%s", module_name(serial
->type
->driver
.owner
));
467 length
+= sprintf (page
+length
, " name:\"%s\"", serial
->type
->description
);
468 length
+= sprintf (page
+length
, " vendor:%04x product:%04x",
469 le16_to_cpu(serial
->dev
->descriptor
.idVendor
),
470 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
471 length
+= sprintf (page
+length
, " num_ports:%d", serial
->num_ports
);
472 length
+= sprintf (page
+length
, " port:%d", i
- serial
->minor
+ 1);
474 usb_make_path(serial
->dev
, tmp
, sizeof(tmp
));
475 length
+= sprintf (page
+length
, " path:%s", tmp
);
477 length
+= sprintf (page
+length
, "\n");
478 if ((length
+ begin
) > (off
+ count
))
480 if ((length
+ begin
) < off
) {
484 usb_serial_put(serial
);
488 if (off
>= (length
+ begin
))
490 *start
= page
+ (off
-begin
);
491 return ((count
< begin
+length
-off
) ? count
: begin
+length
-off
);
494 static int serial_tiocmget (struct tty_struct
*tty
, struct file
*file
)
496 struct usb_serial_port
*port
= tty
->driver_data
;
501 dbg("%s - port %d", __FUNCTION__
, port
->number
);
503 if (!port
->open_count
) {
504 dbg("%s - port not open", __FUNCTION__
);
508 if (port
->serial
->type
->tiocmget
)
509 return port
->serial
->type
->tiocmget(port
, file
);
514 static int serial_tiocmset (struct tty_struct
*tty
, struct file
*file
,
515 unsigned int set
, unsigned int clear
)
517 struct usb_serial_port
*port
= tty
->driver_data
;
522 dbg("%s - port %d", __FUNCTION__
, port
->number
);
524 if (!port
->open_count
) {
525 dbg("%s - port not open", __FUNCTION__
);
529 if (port
->serial
->type
->tiocmset
)
530 return port
->serial
->type
->tiocmset(port
, file
, set
, clear
);
536 * We would be calling tty_wakeup here, but unfortunately some line
537 * disciplines have an annoying habit of calling tty->write from
538 * the write wakeup callback (e.g. n_hdlc.c).
540 void usb_serial_port_softint(struct usb_serial_port
*port
)
542 schedule_work(&port
->work
);
545 static void usb_serial_port_work(void *private)
547 struct usb_serial_port
*port
= private;
548 struct tty_struct
*tty
;
550 dbg("%s - port %d", __FUNCTION__
, port
->number
);
562 static void port_release(struct device
*dev
)
564 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
566 dbg ("%s - %s", __FUNCTION__
, dev
->bus_id
);
567 usb_kill_urb(port
->read_urb
);
568 usb_free_urb(port
->read_urb
);
569 usb_kill_urb(port
->write_urb
);
570 usb_free_urb(port
->write_urb
);
571 usb_kill_urb(port
->interrupt_in_urb
);
572 usb_free_urb(port
->interrupt_in_urb
);
573 usb_kill_urb(port
->interrupt_out_urb
);
574 usb_free_urb(port
->interrupt_out_urb
);
575 kfree(port
->bulk_in_buffer
);
576 kfree(port
->bulk_out_buffer
);
577 kfree(port
->interrupt_in_buffer
);
578 kfree(port
->interrupt_out_buffer
);
582 static struct usb_serial
* create_serial (struct usb_device
*dev
,
583 struct usb_interface
*interface
,
584 struct usb_serial_driver
*driver
)
586 struct usb_serial
*serial
;
588 serial
= kzalloc(sizeof(*serial
), GFP_KERNEL
);
590 dev_err(&dev
->dev
, "%s - out of memory\n", __FUNCTION__
);
593 serial
->dev
= usb_get_dev(dev
);
594 serial
->type
= driver
;
595 serial
->interface
= interface
;
596 kref_init(&serial
->kref
);
601 static struct usb_serial_driver
*search_serial_device(struct usb_interface
*iface
)
604 const struct usb_device_id
*id
;
605 struct usb_serial_driver
*t
;
607 /* Check if the usb id matches a known device */
608 list_for_each(p
, &usb_serial_driver_list
) {
609 t
= list_entry(p
, struct usb_serial_driver
, driver_list
);
610 id
= usb_match_id(iface
, t
->id_table
);
612 dbg("descriptor matches");
620 int usb_serial_probe(struct usb_interface
*interface
,
621 const struct usb_device_id
*id
)
623 struct usb_device
*dev
= interface_to_usbdev (interface
);
624 struct usb_serial
*serial
= NULL
;
625 struct usb_serial_port
*port
;
626 struct usb_host_interface
*iface_desc
;
627 struct usb_endpoint_descriptor
*endpoint
;
628 struct usb_endpoint_descriptor
*interrupt_in_endpoint
[MAX_NUM_PORTS
];
629 struct usb_endpoint_descriptor
*interrupt_out_endpoint
[MAX_NUM_PORTS
];
630 struct usb_endpoint_descriptor
*bulk_in_endpoint
[MAX_NUM_PORTS
];
631 struct usb_endpoint_descriptor
*bulk_out_endpoint
[MAX_NUM_PORTS
];
632 struct usb_serial_driver
*type
= NULL
;
637 int num_interrupt_in
= 0;
638 int num_interrupt_out
= 0;
640 int num_bulk_out
= 0;
644 type
= search_serial_device(interface
);
650 serial
= create_serial (dev
, interface
, type
);
652 dev_err(&interface
->dev
, "%s - out of memory\n", __FUNCTION__
);
656 /* if this device type has a probe function, call it */
658 const struct usb_device_id
*id
;
660 if (!try_module_get(type
->driver
.owner
)) {
661 dev_err(&interface
->dev
, "module get failed, exiting\n");
666 id
= usb_match_id(interface
, type
->id_table
);
667 retval
= type
->probe(serial
, id
);
668 module_put(type
->driver
.owner
);
671 dbg ("sub driver rejected device");
677 /* descriptor matches, let's find the endpoints needed */
678 /* check out the endpoints */
679 iface_desc
= interface
->cur_altsetting
;
680 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
681 endpoint
= &iface_desc
->endpoint
[i
].desc
;
683 if ((endpoint
->bEndpointAddress
& 0x80) &&
684 ((endpoint
->bmAttributes
& 3) == 0x02)) {
685 /* we found a bulk in endpoint */
686 dbg("found bulk in on endpoint %d", i
);
687 bulk_in_endpoint
[num_bulk_in
] = endpoint
;
691 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
692 ((endpoint
->bmAttributes
& 3) == 0x02)) {
693 /* we found a bulk out endpoint */
694 dbg("found bulk out on endpoint %d", i
);
695 bulk_out_endpoint
[num_bulk_out
] = endpoint
;
699 if ((endpoint
->bEndpointAddress
& 0x80) &&
700 ((endpoint
->bmAttributes
& 3) == 0x03)) {
701 /* we found a interrupt in endpoint */
702 dbg("found interrupt in on endpoint %d", i
);
703 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
707 if (((endpoint
->bEndpointAddress
& 0x80) == 0x00) &&
708 ((endpoint
->bmAttributes
& 3) == 0x03)) {
709 /* we found an interrupt out endpoint */
710 dbg("found interrupt out on endpoint %d", i
);
711 interrupt_out_endpoint
[num_interrupt_out
] = endpoint
;
716 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
717 /* BEGIN HORRIBLE HACK FOR PL2303 */
718 /* this is needed due to the looney way its endpoints are set up */
719 if (((le16_to_cpu(dev
->descriptor
.idVendor
) == PL2303_VENDOR_ID
) &&
720 (le16_to_cpu(dev
->descriptor
.idProduct
) == PL2303_PRODUCT_ID
)) ||
721 ((le16_to_cpu(dev
->descriptor
.idVendor
) == ATEN_VENDOR_ID
) &&
722 (le16_to_cpu(dev
->descriptor
.idProduct
) == ATEN_PRODUCT_ID
))) {
723 if (interface
!= dev
->actconfig
->interface
[0]) {
724 /* check out the endpoints of the other interface*/
725 iface_desc
= dev
->actconfig
->interface
[0]->cur_altsetting
;
726 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
727 endpoint
= &iface_desc
->endpoint
[i
].desc
;
728 if ((endpoint
->bEndpointAddress
& 0x80) &&
729 ((endpoint
->bmAttributes
& 3) == 0x03)) {
730 /* we found a interrupt in endpoint */
731 dbg("found interrupt in for Prolific device on separate interface");
732 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
738 /* Now make sure the PL-2303 is configured correctly.
739 * If not, give up now and hope this hack will work
740 * properly during a later invocation of usb_serial_probe
742 if (num_bulk_in
== 0 || num_bulk_out
== 0) {
743 dev_info(&interface
->dev
, "PL-2303 hack: descriptors matched but endpoints did not\n");
748 /* END HORRIBLE HACK FOR PL2303 */
751 /* found all that we need */
752 dev_info(&interface
->dev
, "%s converter detected\n", type
->description
);
754 #ifdef CONFIG_USB_SERIAL_GENERIC
755 if (type
== &usb_serial_generic_device
) {
756 num_ports
= num_bulk_out
;
757 if (num_ports
== 0) {
758 dev_err(&interface
->dev
, "Generic device with no bulk out, not allowed.\n");
765 /* if this device type has a calc_num_ports function, call it */
766 if (type
->calc_num_ports
) {
767 if (!try_module_get(type
->driver
.owner
)) {
768 dev_err(&interface
->dev
, "module get failed, exiting\n");
772 num_ports
= type
->calc_num_ports (serial
);
773 module_put(type
->driver
.owner
);
776 num_ports
= type
->num_ports
;
779 if (get_free_serial (serial
, num_ports
, &minor
) == NULL
) {
780 dev_err(&interface
->dev
, "No more free serial devices\n");
785 serial
->minor
= minor
;
786 serial
->num_ports
= num_ports
;
787 serial
->num_bulk_in
= num_bulk_in
;
788 serial
->num_bulk_out
= num_bulk_out
;
789 serial
->num_interrupt_in
= num_interrupt_in
;
790 serial
->num_interrupt_out
= num_interrupt_out
;
792 /* create our ports, we need as many as the max endpoints */
793 /* we don't use num_ports here cauz some devices have more endpoint pairs than ports */
794 max_endpoints
= max(num_bulk_in
, num_bulk_out
);
795 max_endpoints
= max(max_endpoints
, num_interrupt_in
);
796 max_endpoints
= max(max_endpoints
, num_interrupt_out
);
797 max_endpoints
= max(max_endpoints
, (int)serial
->num_ports
);
798 serial
->num_port_pointers
= max_endpoints
;
799 dbg("%s - setting up %d port structures for this device", __FUNCTION__
, max_endpoints
);
800 for (i
= 0; i
< max_endpoints
; ++i
) {
801 port
= kzalloc(sizeof(struct usb_serial_port
), GFP_KERNEL
);
804 port
->number
= i
+ serial
->minor
;
805 port
->serial
= serial
;
806 spin_lock_init(&port
->lock
);
807 mutex_init(&port
->mutex
);
808 INIT_WORK(&port
->work
, usb_serial_port_work
, port
);
809 serial
->port
[i
] = port
;
812 /* set up the endpoint information */
813 for (i
= 0; i
< num_bulk_in
; ++i
) {
814 endpoint
= bulk_in_endpoint
[i
];
815 port
= serial
->port
[i
];
816 port
->read_urb
= usb_alloc_urb (0, GFP_KERNEL
);
817 if (!port
->read_urb
) {
818 dev_err(&interface
->dev
, "No free urbs available\n");
821 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
822 port
->bulk_in_size
= buffer_size
;
823 port
->bulk_in_endpointAddress
= endpoint
->bEndpointAddress
;
824 port
->bulk_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
825 if (!port
->bulk_in_buffer
) {
826 dev_err(&interface
->dev
, "Couldn't allocate bulk_in_buffer\n");
829 usb_fill_bulk_urb (port
->read_urb
, dev
,
830 usb_rcvbulkpipe (dev
,
831 endpoint
->bEndpointAddress
),
832 port
->bulk_in_buffer
, buffer_size
,
833 serial
->type
->read_bulk_callback
,
837 for (i
= 0; i
< num_bulk_out
; ++i
) {
838 endpoint
= bulk_out_endpoint
[i
];
839 port
= serial
->port
[i
];
840 port
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
841 if (!port
->write_urb
) {
842 dev_err(&interface
->dev
, "No free urbs available\n");
845 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
846 port
->bulk_out_size
= buffer_size
;
847 port
->bulk_out_endpointAddress
= endpoint
->bEndpointAddress
;
848 port
->bulk_out_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
849 if (!port
->bulk_out_buffer
) {
850 dev_err(&interface
->dev
, "Couldn't allocate bulk_out_buffer\n");
853 usb_fill_bulk_urb (port
->write_urb
, dev
,
854 usb_sndbulkpipe (dev
,
855 endpoint
->bEndpointAddress
),
856 port
->bulk_out_buffer
, buffer_size
,
857 serial
->type
->write_bulk_callback
,
861 if (serial
->type
->read_int_callback
) {
862 for (i
= 0; i
< num_interrupt_in
; ++i
) {
863 endpoint
= interrupt_in_endpoint
[i
];
864 port
= serial
->port
[i
];
865 port
->interrupt_in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
866 if (!port
->interrupt_in_urb
) {
867 dev_err(&interface
->dev
, "No free urbs available\n");
870 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
871 port
->interrupt_in_endpointAddress
= endpoint
->bEndpointAddress
;
872 port
->interrupt_in_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
873 if (!port
->interrupt_in_buffer
) {
874 dev_err(&interface
->dev
, "Couldn't allocate interrupt_in_buffer\n");
877 usb_fill_int_urb (port
->interrupt_in_urb
, dev
,
879 endpoint
->bEndpointAddress
),
880 port
->interrupt_in_buffer
, buffer_size
,
881 serial
->type
->read_int_callback
, port
,
882 endpoint
->bInterval
);
884 } else if (num_interrupt_in
) {
885 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
888 if (serial
->type
->write_int_callback
) {
889 for (i
= 0; i
< num_interrupt_out
; ++i
) {
890 endpoint
= interrupt_out_endpoint
[i
];
891 port
= serial
->port
[i
];
892 port
->interrupt_out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
893 if (!port
->interrupt_out_urb
) {
894 dev_err(&interface
->dev
, "No free urbs available\n");
897 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
898 port
->interrupt_out_size
= buffer_size
;
899 port
->interrupt_out_endpointAddress
= endpoint
->bEndpointAddress
;
900 port
->interrupt_out_buffer
= kmalloc (buffer_size
, GFP_KERNEL
);
901 if (!port
->interrupt_out_buffer
) {
902 dev_err(&interface
->dev
, "Couldn't allocate interrupt_out_buffer\n");
905 usb_fill_int_urb (port
->interrupt_out_urb
, dev
,
907 endpoint
->bEndpointAddress
),
908 port
->interrupt_out_buffer
, buffer_size
,
909 serial
->type
->write_int_callback
, port
,
910 endpoint
->bInterval
);
912 } else if (num_interrupt_out
) {
913 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
916 /* if this device type has an attach function, call it */
918 if (!try_module_get(type
->driver
.owner
)) {
919 dev_err(&interface
->dev
, "module get failed, exiting\n");
922 retval
= type
->attach (serial
);
923 module_put(type
->driver
.owner
);
927 /* quietly accept this device, but don't bind to a serial port
928 * as it's about to disappear */
933 /* register all of the individual ports with the driver core */
934 for (i
= 0; i
< num_ports
; ++i
) {
935 port
= serial
->port
[i
];
936 port
->dev
.parent
= &interface
->dev
;
937 port
->dev
.driver
= NULL
;
938 port
->dev
.bus
= &usb_serial_bus_type
;
939 port
->dev
.release
= &port_release
;
941 snprintf (&port
->dev
.bus_id
[0], sizeof(port
->dev
.bus_id
), "ttyUSB%d", port
->number
);
942 dbg ("%s - registering %s", __FUNCTION__
, port
->dev
.bus_id
);
943 device_register (&port
->dev
);
946 usb_serial_console_init (debug
, minor
);
950 usb_set_intfdata (interface
, serial
);
954 for (i
= 0; i
< num_bulk_in
; ++i
) {
955 port
= serial
->port
[i
];
959 usb_free_urb (port
->read_urb
);
960 kfree(port
->bulk_in_buffer
);
962 for (i
= 0; i
< num_bulk_out
; ++i
) {
963 port
= serial
->port
[i
];
967 usb_free_urb (port
->write_urb
);
968 kfree(port
->bulk_out_buffer
);
970 for (i
= 0; i
< num_interrupt_in
; ++i
) {
971 port
= serial
->port
[i
];
974 if (port
->interrupt_in_urb
)
975 usb_free_urb (port
->interrupt_in_urb
);
976 kfree(port
->interrupt_in_buffer
);
978 for (i
= 0; i
< num_interrupt_out
; ++i
) {
979 port
= serial
->port
[i
];
982 if (port
->interrupt_out_urb
)
983 usb_free_urb (port
->interrupt_out_urb
);
984 kfree(port
->interrupt_out_buffer
);
987 /* return the minor range that this device had */
988 return_serial (serial
);
990 /* free up any memory that we allocated */
991 for (i
= 0; i
< serial
->num_port_pointers
; ++i
)
992 kfree(serial
->port
[i
]);
997 void usb_serial_disconnect(struct usb_interface
*interface
)
1000 struct usb_serial
*serial
= usb_get_intfdata (interface
);
1001 struct device
*dev
= &interface
->dev
;
1002 struct usb_serial_port
*port
;
1004 usb_serial_console_disconnect(serial
);
1005 dbg ("%s", __FUNCTION__
);
1007 usb_set_intfdata (interface
, NULL
);
1009 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1010 port
= serial
->port
[i
];
1011 if (port
&& port
->tty
)
1012 tty_hangup(port
->tty
);
1014 /* let the last holder of this object
1015 * cause it to be cleaned up */
1016 usb_serial_put(serial
);
1018 dev_info(dev
, "device disconnected\n");
1021 static struct tty_operations serial_ops
= {
1022 .open
= serial_open
,
1023 .close
= serial_close
,
1024 .write
= serial_write
,
1025 .write_room
= serial_write_room
,
1026 .ioctl
= serial_ioctl
,
1027 .set_termios
= serial_set_termios
,
1028 .throttle
= serial_throttle
,
1029 .unthrottle
= serial_unthrottle
,
1030 .break_ctl
= serial_break
,
1031 .chars_in_buffer
= serial_chars_in_buffer
,
1032 .read_proc
= serial_read_proc
,
1033 .tiocmget
= serial_tiocmget
,
1034 .tiocmset
= serial_tiocmset
,
1037 struct tty_driver
*usb_serial_tty_driver
;
1039 static int __init
usb_serial_init(void)
1044 usb_serial_tty_driver
= alloc_tty_driver(SERIAL_TTY_MINORS
);
1045 if (!usb_serial_tty_driver
)
1048 /* Initialize our global data */
1049 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
1050 serial_table
[i
] = NULL
;
1053 result
= bus_register(&usb_serial_bus_type
);
1055 err("%s - registering bus driver failed", __FUNCTION__
);
1059 usb_serial_tty_driver
->owner
= THIS_MODULE
;
1060 usb_serial_tty_driver
->driver_name
= "usbserial";
1061 usb_serial_tty_driver
->name
= "ttyUSB";
1062 usb_serial_tty_driver
->major
= SERIAL_TTY_MAJOR
;
1063 usb_serial_tty_driver
->minor_start
= 0;
1064 usb_serial_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1065 usb_serial_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1066 usb_serial_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_DYNAMIC_DEV
;
1067 usb_serial_tty_driver
->init_termios
= tty_std_termios
;
1068 usb_serial_tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1069 tty_set_operations(usb_serial_tty_driver
, &serial_ops
);
1070 result
= tty_register_driver(usb_serial_tty_driver
);
1072 err("%s - tty_register_driver failed", __FUNCTION__
);
1073 goto exit_reg_driver
;
1076 /* register the USB driver */
1077 result
= usb_register(&usb_serial_driver
);
1079 err("%s - usb_register failed", __FUNCTION__
);
1083 /* register the generic driver, if we should */
1084 result
= usb_serial_generic_register(debug
);
1086 err("%s - registering generic driver failed", __FUNCTION__
);
1095 usb_deregister(&usb_serial_driver
);
1098 tty_unregister_driver(usb_serial_tty_driver
);
1101 bus_unregister(&usb_serial_bus_type
);
1104 err ("%s - returning with error %d", __FUNCTION__
, result
);
1105 put_tty_driver(usb_serial_tty_driver
);
1110 static void __exit
usb_serial_exit(void)
1112 usb_serial_console_exit();
1114 usb_serial_generic_deregister();
1116 usb_deregister(&usb_serial_driver
);
1117 tty_unregister_driver(usb_serial_tty_driver
);
1118 put_tty_driver(usb_serial_tty_driver
);
1119 bus_unregister(&usb_serial_bus_type
);
1123 module_init(usb_serial_init
);
1124 module_exit(usb_serial_exit
);
1126 #define set_to_generic_if_null(type, function) \
1128 if (!type->function) { \
1129 type->function = usb_serial_generic_##function; \
1130 dbg("Had to override the " #function \
1131 " usb serial operation with the generic one.");\
1135 static void fixup_generic(struct usb_serial_driver
*device
)
1137 set_to_generic_if_null(device
, open
);
1138 set_to_generic_if_null(device
, write
);
1139 set_to_generic_if_null(device
, close
);
1140 set_to_generic_if_null(device
, write_room
);
1141 set_to_generic_if_null(device
, chars_in_buffer
);
1142 set_to_generic_if_null(device
, read_bulk_callback
);
1143 set_to_generic_if_null(device
, write_bulk_callback
);
1144 set_to_generic_if_null(device
, shutdown
);
1147 int usb_serial_register(struct usb_serial_driver
*driver
)
1151 fixup_generic(driver
);
1153 if (!driver
->description
)
1154 driver
->description
= driver
->driver
.name
;
1156 /* Add this device to our list of devices */
1157 list_add(&driver
->driver_list
, &usb_serial_driver_list
);
1159 retval
= usb_serial_bus_register(driver
);
1161 err("problem %d when registering driver %s", retval
, driver
->description
);
1162 list_del(&driver
->driver_list
);
1165 info("USB Serial support registered for %s", driver
->description
);
1171 void usb_serial_deregister(struct usb_serial_driver
*device
)
1173 info("USB Serial deregistering driver %s", device
->description
);
1174 list_del(&device
->driver_list
);
1175 usb_serial_bus_deregister(device
);
1180 /* If the usb-serial core is built into the core, the usb-serial drivers
1181 need these symbols to load properly as modules. */
1182 EXPORT_SYMBOL_GPL(usb_serial_register
);
1183 EXPORT_SYMBOL_GPL(usb_serial_deregister
);
1184 EXPORT_SYMBOL_GPL(usb_serial_probe
);
1185 EXPORT_SYMBOL_GPL(usb_serial_disconnect
);
1186 EXPORT_SYMBOL_GPL(usb_serial_port_softint
);
1189 /* Module information */
1190 MODULE_AUTHOR( DRIVER_AUTHOR
);
1191 MODULE_DESCRIPTION( DRIVER_DESC
);
1192 MODULE_LICENSE("GPL");
1194 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1195 MODULE_PARM_DESC(debug
, "Debug enabled or not");