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
20 #include <linux/kernel.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/tty.h>
25 #include <linux/tty_driver.h>
26 #include <linux/tty_flip.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/seq_file.h>
30 #include <linux/spinlock.h>
31 #include <linux/mutex.h>
32 #include <linux/list.h>
33 #include <linux/uaccess.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
41 #define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
42 #define DRIVER_DESC "USB Serial Driver core"
44 static void port_free(struct usb_serial_port
*port
);
46 /* Driver structure we register with the USB core */
47 static struct usb_driver usb_serial_driver
= {
49 .probe
= usb_serial_probe
,
50 .disconnect
= usb_serial_disconnect
,
51 .suspend
= usb_serial_suspend
,
52 .resume
= usb_serial_resume
,
56 /* There is no MODULE_DEVICE_TABLE for usbserial.c. Instead
57 the MODULE_DEVICE_TABLE declarations in each serial driver
58 cause the "hotplug" program to pull in whatever module is necessary
59 via modprobe, and modprobe will load usbserial because the serial
64 /* initially all NULL */
65 static struct usb_serial
*serial_table
[SERIAL_TTY_MINORS
];
66 static DEFINE_MUTEX(table_lock
);
67 static LIST_HEAD(usb_serial_driver_list
);
69 struct usb_serial
*usb_serial_get_by_index(unsigned index
)
71 struct usb_serial
*serial
;
73 mutex_lock(&table_lock
);
74 serial
= serial_table
[index
];
77 kref_get(&serial
->kref
);
78 mutex_unlock(&table_lock
);
82 static struct usb_serial
*get_free_serial(struct usb_serial
*serial
,
83 int num_ports
, unsigned int *minor
)
88 dbg("%s %d", __func__
, num_ports
);
91 mutex_lock(&table_lock
);
92 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
97 for (j
= 1; j
<= num_ports
-1; ++j
)
98 if ((i
+j
>= SERIAL_TTY_MINORS
) || (serial_table
[i
+j
])) {
108 dbg("%s - minor base = %d", __func__
, *minor
);
109 for (i
= *minor
; (i
< (*minor
+ num_ports
)) && (i
< SERIAL_TTY_MINORS
); ++i
) {
110 serial_table
[i
] = serial
;
111 serial
->port
[j
++]->number
= i
;
113 mutex_unlock(&table_lock
);
116 mutex_unlock(&table_lock
);
120 static void return_serial(struct usb_serial
*serial
)
126 for (i
= 0; i
< serial
->num_ports
; ++i
)
127 serial_table
[serial
->minor
+ i
] = NULL
;
130 static void destroy_serial(struct kref
*kref
)
132 struct usb_serial
*serial
;
133 struct usb_serial_port
*port
;
136 serial
= to_usb_serial(kref
);
138 dbg("%s - %s", __func__
, serial
->type
->description
);
140 /* return the minor range that this device had */
141 if (serial
->minor
!= SERIAL_TTY_NO_MINOR
)
142 return_serial(serial
);
144 /* If this is a "fake" port, we have to clean it up here, as it will
145 * not get cleaned up in port_release() as it was never registered with
147 if (serial
->num_ports
< serial
->num_port_pointers
) {
148 for (i
= serial
->num_ports
;
149 i
< serial
->num_port_pointers
; ++i
) {
150 port
= serial
->port
[i
];
157 usb_put_dev(serial
->dev
);
159 /* free up any memory that we allocated */
163 void usb_serial_put(struct usb_serial
*serial
)
165 mutex_lock(&table_lock
);
166 kref_put(&serial
->kref
, destroy_serial
);
167 mutex_unlock(&table_lock
);
170 /*****************************************************************************
171 * Driver tty interface functions
172 *****************************************************************************/
173 static int serial_open (struct tty_struct
*tty
, struct file
*filp
)
175 struct usb_serial
*serial
;
176 struct usb_serial_port
*port
;
177 unsigned int portNumber
;
182 /* get the serial object associated with this tty pointer */
183 serial
= usb_serial_get_by_index(tty
->index
);
185 tty
->driver_data
= NULL
;
189 mutex_lock(&serial
->disc_mutex
);
190 portNumber
= tty
->index
- serial
->minor
;
191 port
= serial
->port
[portNumber
];
192 if (!port
|| serial
->disconnected
)
195 get_device(&port
->dev
);
197 * Note: Our locking order requirement does not allow port->mutex
198 * to be acquired while serial->disc_mutex is held.
200 mutex_unlock(&serial
->disc_mutex
);
202 goto bailout_serial_put
;
204 if (mutex_lock_interruptible(&port
->mutex
)) {
205 retval
= -ERESTARTSYS
;
206 goto bailout_port_put
;
211 /* set up our port structure making the tty driver
212 * remember our port object, and us it */
213 tty
->driver_data
= port
;
214 tty_port_tty_set(&port
->port
, tty
);
216 if (port
->port
.count
== 1) {
218 /* lock this module before we call it
219 * this may fail, which means we must bail out,
220 * safe because we are called with BKL held */
221 if (!try_module_get(serial
->type
->driver
.owner
)) {
223 goto bailout_mutex_unlock
;
226 mutex_lock(&serial
->disc_mutex
);
227 if (serial
->disconnected
)
230 retval
= usb_autopm_get_interface(serial
->interface
);
232 goto bailout_module_put
;
234 /* only call the device specific open if this
235 * is the first time the port is opened */
236 retval
= serial
->type
->open(tty
, port
, filp
);
238 goto bailout_interface_put
;
239 mutex_unlock(&serial
->disc_mutex
);
242 mutex_unlock(&port
->mutex
);
245 bailout_interface_put
:
246 usb_autopm_put_interface(serial
->interface
);
248 mutex_unlock(&serial
->disc_mutex
);
249 module_put(serial
->type
->driver
.owner
);
250 bailout_mutex_unlock
:
251 port
->port
.count
= 0;
252 tty
->driver_data
= NULL
;
253 tty_port_tty_set(&port
->port
, NULL
);
254 mutex_unlock(&port
->mutex
);
256 put_device(&port
->dev
);
258 usb_serial_put(serial
);
262 static void serial_close(struct tty_struct
*tty
, struct file
*filp
)
264 struct usb_serial_port
*port
= tty
->driver_data
;
265 struct usb_serial
*serial
;
266 struct module
*owner
;
272 dbg("%s - port %d", __func__
, port
->number
);
274 mutex_lock(&port
->mutex
);
275 serial
= port
->serial
;
276 owner
= serial
->type
->driver
.owner
;
278 if (port
->port
.count
== 0) {
279 mutex_unlock(&port
->mutex
);
283 if (port
->port
.count
== 1)
284 /* only call the device specific close if this
285 * port is being closed by the last owner. Ensure we do
286 * this before we drop the port count. The call is protected
289 serial
->type
->close(tty
, port
, filp
);
291 if (port
->port
.count
== (port
->console
? 2 : 1)) {
292 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
294 /* We must do this before we drop the port count to
296 if (tty
->driver_data
)
297 tty
->driver_data
= NULL
;
298 tty_port_tty_set(&port
->port
, NULL
);
304 count
= port
->port
.count
;
305 mutex_unlock(&port
->mutex
);
306 put_device(&port
->dev
);
308 /* Mustn't dereference port any more */
310 mutex_lock(&serial
->disc_mutex
);
311 if (!serial
->disconnected
)
312 usb_autopm_put_interface(serial
->interface
);
313 mutex_unlock(&serial
->disc_mutex
);
315 usb_serial_put(serial
);
317 /* Mustn't dereference serial any more */
322 static int serial_write(struct tty_struct
*tty
, const unsigned char *buf
,
325 struct usb_serial_port
*port
= tty
->driver_data
;
326 int retval
= -ENODEV
;
328 if (port
->serial
->dev
->state
== USB_STATE_NOTATTACHED
)
331 dbg("%s - port %d, %d byte(s)", __func__
, port
->number
, count
);
333 /* count is managed under the mutex lock for the tty so cannot
334 drop to zero until after the last close completes */
335 WARN_ON(!port
->port
.count
);
337 /* pass on to the driver specific version of this function */
338 retval
= port
->serial
->type
->write(tty
, port
, buf
, count
);
344 static int serial_write_room(struct tty_struct
*tty
)
346 struct usb_serial_port
*port
= tty
->driver_data
;
347 dbg("%s - port %d", __func__
, port
->number
);
348 WARN_ON(!port
->port
.count
);
349 /* pass on to the driver specific version of this function */
350 return port
->serial
->type
->write_room(tty
);
353 static int serial_chars_in_buffer(struct tty_struct
*tty
)
355 struct usb_serial_port
*port
= tty
->driver_data
;
356 dbg("%s = port %d", __func__
, port
->number
);
358 WARN_ON(!port
->port
.count
);
359 /* if the device was unplugged then any remaining characters
360 fell out of the connector ;) */
361 if (port
->serial
->disconnected
)
363 /* pass on to the driver specific version of this function */
364 return port
->serial
->type
->chars_in_buffer(tty
);
367 static void serial_throttle(struct tty_struct
*tty
)
369 struct usb_serial_port
*port
= tty
->driver_data
;
370 dbg("%s - port %d", __func__
, port
->number
);
372 WARN_ON(!port
->port
.count
);
373 /* pass on to the driver specific version of this function */
374 if (port
->serial
->type
->throttle
)
375 port
->serial
->type
->throttle(tty
);
378 static void serial_unthrottle(struct tty_struct
*tty
)
380 struct usb_serial_port
*port
= tty
->driver_data
;
381 dbg("%s - port %d", __func__
, port
->number
);
383 WARN_ON(!port
->port
.count
);
384 /* pass on to the driver specific version of this function */
385 if (port
->serial
->type
->unthrottle
)
386 port
->serial
->type
->unthrottle(tty
);
389 static int serial_ioctl(struct tty_struct
*tty
, struct file
*file
,
390 unsigned int cmd
, unsigned long arg
)
392 struct usb_serial_port
*port
= tty
->driver_data
;
393 int retval
= -ENODEV
;
395 dbg("%s - port %d, cmd 0x%.4x", __func__
, port
->number
, cmd
);
397 WARN_ON(!port
->port
.count
);
399 /* pass on to the driver specific version of this function
400 if it is available */
401 if (port
->serial
->type
->ioctl
) {
402 retval
= port
->serial
->type
->ioctl(tty
, file
, cmd
, arg
);
404 retval
= -ENOIOCTLCMD
;
408 static void serial_set_termios(struct tty_struct
*tty
, struct ktermios
*old
)
410 struct usb_serial_port
*port
= tty
->driver_data
;
411 dbg("%s - port %d", __func__
, port
->number
);
413 WARN_ON(!port
->port
.count
);
414 /* pass on to the driver specific version of this function
415 if it is available */
416 if (port
->serial
->type
->set_termios
)
417 port
->serial
->type
->set_termios(tty
, port
, old
);
419 tty_termios_copy_hw(tty
->termios
, old
);
422 static int serial_break(struct tty_struct
*tty
, int break_state
)
424 struct usb_serial_port
*port
= tty
->driver_data
;
426 dbg("%s - port %d", __func__
, port
->number
);
428 WARN_ON(!port
->port
.count
);
429 /* pass on to the driver specific version of this function
430 if it is available */
431 if (port
->serial
->type
->break_ctl
)
432 port
->serial
->type
->break_ctl(tty
, break_state
);
436 static int serial_proc_show(struct seq_file
*m
, void *v
)
438 struct usb_serial
*serial
;
443 seq_puts(m
, "usbserinfo:1.0 driver:2.0\n");
444 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
) {
445 serial
= usb_serial_get_by_index(i
);
449 seq_printf(m
, "%d:", i
);
450 if (serial
->type
->driver
.owner
)
451 seq_printf(m
, " module:%s",
452 module_name(serial
->type
->driver
.owner
));
453 seq_printf(m
, " name:\"%s\"",
454 serial
->type
->description
);
455 seq_printf(m
, " vendor:%04x product:%04x",
456 le16_to_cpu(serial
->dev
->descriptor
.idVendor
),
457 le16_to_cpu(serial
->dev
->descriptor
.idProduct
));
458 seq_printf(m
, " num_ports:%d", serial
->num_ports
);
459 seq_printf(m
, " port:%d", i
- serial
->minor
+ 1);
460 usb_make_path(serial
->dev
, tmp
, sizeof(tmp
));
461 seq_printf(m
, " path:%s", tmp
);
464 usb_serial_put(serial
);
469 static int serial_proc_open(struct inode
*inode
, struct file
*file
)
471 return single_open(file
, serial_proc_show
, NULL
);
474 static const struct file_operations serial_proc_fops
= {
475 .owner
= THIS_MODULE
,
476 .open
= serial_proc_open
,
479 .release
= single_release
,
482 static int serial_tiocmget(struct tty_struct
*tty
, struct file
*file
)
484 struct usb_serial_port
*port
= tty
->driver_data
;
486 dbg("%s - port %d", __func__
, port
->number
);
488 WARN_ON(!port
->port
.count
);
489 if (port
->serial
->type
->tiocmget
)
490 return port
->serial
->type
->tiocmget(tty
, file
);
494 static int serial_tiocmset(struct tty_struct
*tty
, struct file
*file
,
495 unsigned int set
, unsigned int clear
)
497 struct usb_serial_port
*port
= tty
->driver_data
;
499 dbg("%s - port %d", __func__
, port
->number
);
501 WARN_ON(!port
->port
.count
);
502 if (port
->serial
->type
->tiocmset
)
503 return port
->serial
->type
->tiocmset(tty
, file
, set
, clear
);
508 * We would be calling tty_wakeup here, but unfortunately some line
509 * disciplines have an annoying habit of calling tty->write from
510 * the write wakeup callback (e.g. n_hdlc.c).
512 void usb_serial_port_softint(struct usb_serial_port
*port
)
514 schedule_work(&port
->work
);
516 EXPORT_SYMBOL_GPL(usb_serial_port_softint
);
518 static void usb_serial_port_work(struct work_struct
*work
)
520 struct usb_serial_port
*port
=
521 container_of(work
, struct usb_serial_port
, work
);
522 struct tty_struct
*tty
;
524 dbg("%s - port %d", __func__
, port
->number
);
526 tty
= tty_port_tty_get(&port
->port
);
534 static void port_release(struct device
*dev
)
536 struct usb_serial_port
*port
= to_usb_serial_port(dev
);
538 dbg ("%s - %s", __func__
, dev_name(dev
));
542 static void kill_traffic(struct usb_serial_port
*port
)
544 usb_kill_urb(port
->read_urb
);
545 usb_kill_urb(port
->write_urb
);
548 * Some drivers submit the read_urb in the
549 * handler for the write_urb or vice versa
550 * this order determines the order in which
551 * usb_kill_urb() must be used to reliably
552 * kill the URBs. As it is unknown here,
553 * both orders must be used in turn.
554 * The call below is not redundant.
556 usb_kill_urb(port
->read_urb
);
557 usb_kill_urb(port
->interrupt_in_urb
);
558 usb_kill_urb(port
->interrupt_out_urb
);
561 static void port_free(struct usb_serial_port
*port
)
564 * Stop all the traffic before cancelling the work, so that
565 * nobody will restart it by calling usb_serial_port_softint.
568 cancel_work_sync(&port
->work
);
570 usb_free_urb(port
->read_urb
);
571 usb_free_urb(port
->write_urb
);
572 usb_free_urb(port
->interrupt_in_urb
);
573 usb_free_urb(port
->interrupt_out_urb
);
574 kfree(port
->bulk_in_buffer
);
575 kfree(port
->bulk_out_buffer
);
576 kfree(port
->interrupt_in_buffer
);
577 kfree(port
->interrupt_out_buffer
);
581 static struct usb_serial
*create_serial(struct usb_device
*dev
,
582 struct usb_interface
*interface
,
583 struct usb_serial_driver
*driver
)
585 struct usb_serial
*serial
;
587 serial
= kzalloc(sizeof(*serial
), GFP_KERNEL
);
589 dev_err(&dev
->dev
, "%s - out of memory\n", __func__
);
592 serial
->dev
= usb_get_dev(dev
);
593 serial
->type
= driver
;
594 serial
->interface
= interface
;
595 kref_init(&serial
->kref
);
596 mutex_init(&serial
->disc_mutex
);
597 serial
->minor
= SERIAL_TTY_NO_MINOR
;
602 static const struct usb_device_id
*match_dynamic_id(struct usb_interface
*intf
,
603 struct usb_serial_driver
*drv
)
605 struct usb_dynid
*dynid
;
607 spin_lock(&drv
->dynids
.lock
);
608 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
609 if (usb_match_one_id(intf
, &dynid
->id
)) {
610 spin_unlock(&drv
->dynids
.lock
);
614 spin_unlock(&drv
->dynids
.lock
);
618 static const struct usb_device_id
*get_iface_id(struct usb_serial_driver
*drv
,
619 struct usb_interface
*intf
)
621 const struct usb_device_id
*id
;
623 id
= usb_match_id(intf
, drv
->id_table
);
625 dbg("static descriptor matches");
628 id
= match_dynamic_id(intf
, drv
);
630 dbg("dynamic descriptor matches");
635 static struct usb_serial_driver
*search_serial_device(
636 struct usb_interface
*iface
)
638 const struct usb_device_id
*id
;
639 struct usb_serial_driver
*drv
;
641 /* Check if the usb id matches a known device */
642 list_for_each_entry(drv
, &usb_serial_driver_list
, driver_list
) {
643 id
= get_iface_id(drv
, iface
);
651 int usb_serial_probe(struct usb_interface
*interface
,
652 const struct usb_device_id
*id
)
654 struct usb_device
*dev
= interface_to_usbdev(interface
);
655 struct usb_serial
*serial
= NULL
;
656 struct usb_serial_port
*port
;
657 struct usb_host_interface
*iface_desc
;
658 struct usb_endpoint_descriptor
*endpoint
;
659 struct usb_endpoint_descriptor
*interrupt_in_endpoint
[MAX_NUM_PORTS
];
660 struct usb_endpoint_descriptor
*interrupt_out_endpoint
[MAX_NUM_PORTS
];
661 struct usb_endpoint_descriptor
*bulk_in_endpoint
[MAX_NUM_PORTS
];
662 struct usb_endpoint_descriptor
*bulk_out_endpoint
[MAX_NUM_PORTS
];
663 struct usb_serial_driver
*type
= NULL
;
668 int num_interrupt_in
= 0;
669 int num_interrupt_out
= 0;
671 int num_bulk_out
= 0;
675 lock_kernel(); /* guard against unloading a serial driver module */
676 type
= search_serial_device(interface
);
683 serial
= create_serial(dev
, interface
, type
);
686 dev_err(&interface
->dev
, "%s - out of memory\n", __func__
);
690 /* if this device type has a probe function, call it */
692 const struct usb_device_id
*id
;
694 if (!try_module_get(type
->driver
.owner
)) {
696 dev_err(&interface
->dev
,
697 "module get failed, exiting\n");
702 id
= get_iface_id(type
, interface
);
703 retval
= type
->probe(serial
, id
);
704 module_put(type
->driver
.owner
);
708 dbg("sub driver rejected device");
714 /* descriptor matches, let's find the endpoints needed */
715 /* check out the endpoints */
716 iface_desc
= interface
->cur_altsetting
;
717 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
718 endpoint
= &iface_desc
->endpoint
[i
].desc
;
720 if (usb_endpoint_is_bulk_in(endpoint
)) {
721 /* we found a bulk in endpoint */
722 dbg("found bulk in on endpoint %d", i
);
723 bulk_in_endpoint
[num_bulk_in
] = endpoint
;
727 if (usb_endpoint_is_bulk_out(endpoint
)) {
728 /* we found a bulk out endpoint */
729 dbg("found bulk out on endpoint %d", i
);
730 bulk_out_endpoint
[num_bulk_out
] = endpoint
;
734 if (usb_endpoint_is_int_in(endpoint
)) {
735 /* we found a interrupt in endpoint */
736 dbg("found interrupt in on endpoint %d", i
);
737 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
741 if (usb_endpoint_is_int_out(endpoint
)) {
742 /* we found an interrupt out endpoint */
743 dbg("found interrupt out on endpoint %d", i
);
744 interrupt_out_endpoint
[num_interrupt_out
] = endpoint
;
749 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
750 /* BEGIN HORRIBLE HACK FOR PL2303 */
751 /* this is needed due to the looney way its endpoints are set up */
752 if (((le16_to_cpu(dev
->descriptor
.idVendor
) == PL2303_VENDOR_ID
) &&
753 (le16_to_cpu(dev
->descriptor
.idProduct
) == PL2303_PRODUCT_ID
)) ||
754 ((le16_to_cpu(dev
->descriptor
.idVendor
) == ATEN_VENDOR_ID
) &&
755 (le16_to_cpu(dev
->descriptor
.idProduct
) == ATEN_PRODUCT_ID
)) ||
756 ((le16_to_cpu(dev
->descriptor
.idVendor
) == ALCOR_VENDOR_ID
) &&
757 (le16_to_cpu(dev
->descriptor
.idProduct
) == ALCOR_PRODUCT_ID
)) ||
758 ((le16_to_cpu(dev
->descriptor
.idVendor
) == SIEMENS_VENDOR_ID
) &&
759 (le16_to_cpu(dev
->descriptor
.idProduct
) == SIEMENS_PRODUCT_ID_EF81
))) {
760 if (interface
!= dev
->actconfig
->interface
[0]) {
761 /* check out the endpoints of the other interface*/
762 iface_desc
= dev
->actconfig
->interface
[0]->cur_altsetting
;
763 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
764 endpoint
= &iface_desc
->endpoint
[i
].desc
;
765 if (usb_endpoint_is_int_in(endpoint
)) {
766 /* we found a interrupt in endpoint */
767 dbg("found interrupt in for Prolific device on separate interface");
768 interrupt_in_endpoint
[num_interrupt_in
] = endpoint
;
774 /* Now make sure the PL-2303 is configured correctly.
775 * If not, give up now and hope this hack will work
776 * properly during a later invocation of usb_serial_probe
778 if (num_bulk_in
== 0 || num_bulk_out
== 0) {
780 dev_info(&interface
->dev
, "PL-2303 hack: descriptors matched but endpoints did not\n");
785 /* END HORRIBLE HACK FOR PL2303 */
788 #ifdef CONFIG_USB_SERIAL_GENERIC
789 if (type
== &usb_serial_generic_device
) {
790 num_ports
= num_bulk_out
;
791 if (num_ports
== 0) {
793 dev_err(&interface
->dev
,
794 "Generic device with no bulk out, not allowed.\n");
801 /* if this device type has a calc_num_ports function, call it */
802 if (type
->calc_num_ports
) {
803 if (!try_module_get(type
->driver
.owner
)) {
805 dev_err(&interface
->dev
,
806 "module get failed, exiting\n");
810 num_ports
= type
->calc_num_ports(serial
);
811 module_put(type
->driver
.owner
);
814 num_ports
= type
->num_ports
;
817 serial
->num_ports
= num_ports
;
818 serial
->num_bulk_in
= num_bulk_in
;
819 serial
->num_bulk_out
= num_bulk_out
;
820 serial
->num_interrupt_in
= num_interrupt_in
;
821 serial
->num_interrupt_out
= num_interrupt_out
;
823 /* found all that we need */
824 dev_info(&interface
->dev
, "%s converter detected\n",
827 /* create our ports, we need as many as the max endpoints */
828 /* we don't use num_ports here because some devices have more
829 endpoint pairs than ports */
830 max_endpoints
= max(num_bulk_in
, num_bulk_out
);
831 max_endpoints
= max(max_endpoints
, num_interrupt_in
);
832 max_endpoints
= max(max_endpoints
, num_interrupt_out
);
833 max_endpoints
= max(max_endpoints
, (int)serial
->num_ports
);
834 serial
->num_port_pointers
= max_endpoints
;
837 dbg("%s - setting up %d port structures for this device",
838 __func__
, max_endpoints
);
839 for (i
= 0; i
< max_endpoints
; ++i
) {
840 port
= kzalloc(sizeof(struct usb_serial_port
), GFP_KERNEL
);
843 tty_port_init(&port
->port
);
844 port
->serial
= serial
;
845 spin_lock_init(&port
->lock
);
846 mutex_init(&port
->mutex
);
847 INIT_WORK(&port
->work
, usb_serial_port_work
);
848 serial
->port
[i
] = port
;
851 /* set up the endpoint information */
852 for (i
= 0; i
< num_bulk_in
; ++i
) {
853 endpoint
= bulk_in_endpoint
[i
];
854 port
= serial
->port
[i
];
855 port
->read_urb
= usb_alloc_urb(0, GFP_KERNEL
);
856 if (!port
->read_urb
) {
857 dev_err(&interface
->dev
, "No free urbs available\n");
860 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
861 port
->bulk_in_size
= buffer_size
;
862 port
->bulk_in_endpointAddress
= endpoint
->bEndpointAddress
;
863 port
->bulk_in_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
864 if (!port
->bulk_in_buffer
) {
865 dev_err(&interface
->dev
,
866 "Couldn't allocate bulk_in_buffer\n");
869 usb_fill_bulk_urb(port
->read_urb
, dev
,
871 endpoint
->bEndpointAddress
),
872 port
->bulk_in_buffer
, buffer_size
,
873 serial
->type
->read_bulk_callback
, port
);
876 for (i
= 0; i
< num_bulk_out
; ++i
) {
877 endpoint
= bulk_out_endpoint
[i
];
878 port
= serial
->port
[i
];
879 port
->write_urb
= usb_alloc_urb(0, GFP_KERNEL
);
880 if (!port
->write_urb
) {
881 dev_err(&interface
->dev
, "No free urbs available\n");
884 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
885 port
->bulk_out_size
= buffer_size
;
886 port
->bulk_out_endpointAddress
= endpoint
->bEndpointAddress
;
887 port
->bulk_out_buffer
= kmalloc(buffer_size
, GFP_KERNEL
);
888 if (!port
->bulk_out_buffer
) {
889 dev_err(&interface
->dev
,
890 "Couldn't allocate bulk_out_buffer\n");
893 usb_fill_bulk_urb(port
->write_urb
, dev
,
895 endpoint
->bEndpointAddress
),
896 port
->bulk_out_buffer
, buffer_size
,
897 serial
->type
->write_bulk_callback
, port
);
900 if (serial
->type
->read_int_callback
) {
901 for (i
= 0; i
< num_interrupt_in
; ++i
) {
902 endpoint
= interrupt_in_endpoint
[i
];
903 port
= serial
->port
[i
];
904 port
->interrupt_in_urb
= usb_alloc_urb(0, GFP_KERNEL
);
905 if (!port
->interrupt_in_urb
) {
906 dev_err(&interface
->dev
,
907 "No free urbs available\n");
910 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
911 port
->interrupt_in_endpointAddress
=
912 endpoint
->bEndpointAddress
;
913 port
->interrupt_in_buffer
= kmalloc(buffer_size
,
915 if (!port
->interrupt_in_buffer
) {
916 dev_err(&interface
->dev
,
917 "Couldn't allocate interrupt_in_buffer\n");
920 usb_fill_int_urb(port
->interrupt_in_urb
, dev
,
922 endpoint
->bEndpointAddress
),
923 port
->interrupt_in_buffer
, buffer_size
,
924 serial
->type
->read_int_callback
, port
,
925 endpoint
->bInterval
);
927 } else if (num_interrupt_in
) {
928 dbg("the device claims to support interrupt in transfers, but read_int_callback is not defined");
931 if (serial
->type
->write_int_callback
) {
932 for (i
= 0; i
< num_interrupt_out
; ++i
) {
933 endpoint
= interrupt_out_endpoint
[i
];
934 port
= serial
->port
[i
];
935 port
->interrupt_out_urb
= usb_alloc_urb(0, GFP_KERNEL
);
936 if (!port
->interrupt_out_urb
) {
937 dev_err(&interface
->dev
,
938 "No free urbs available\n");
941 buffer_size
= le16_to_cpu(endpoint
->wMaxPacketSize
);
942 port
->interrupt_out_size
= buffer_size
;
943 port
->interrupt_out_endpointAddress
=
944 endpoint
->bEndpointAddress
;
945 port
->interrupt_out_buffer
= kmalloc(buffer_size
,
947 if (!port
->interrupt_out_buffer
) {
948 dev_err(&interface
->dev
,
949 "Couldn't allocate interrupt_out_buffer\n");
952 usb_fill_int_urb(port
->interrupt_out_urb
, dev
,
954 endpoint
->bEndpointAddress
),
955 port
->interrupt_out_buffer
, buffer_size
,
956 serial
->type
->write_int_callback
, port
,
957 endpoint
->bInterval
);
959 } else if (num_interrupt_out
) {
960 dbg("the device claims to support interrupt out transfers, but write_int_callback is not defined");
963 /* if this device type has an attach function, call it */
965 if (!try_module_get(type
->driver
.owner
)) {
966 dev_err(&interface
->dev
,
967 "module get failed, exiting\n");
970 retval
= type
->attach(serial
);
971 module_put(type
->driver
.owner
);
975 /* quietly accept this device, but don't bind to a
976 serial port as it's about to disappear */
981 if (get_free_serial(serial
, num_ports
, &minor
) == NULL
) {
982 dev_err(&interface
->dev
, "No more free serial devices\n");
985 serial
->minor
= minor
;
987 /* register all of the individual ports with the driver core */
988 for (i
= 0; i
< num_ports
; ++i
) {
989 port
= serial
->port
[i
];
990 port
->dev
.parent
= &interface
->dev
;
991 port
->dev
.driver
= NULL
;
992 port
->dev
.bus
= &usb_serial_bus_type
;
993 port
->dev
.release
= &port_release
;
995 dev_set_name(&port
->dev
, "ttyUSB%d", port
->number
);
996 dbg ("%s - registering %s", __func__
, dev_name(&port
->dev
));
997 retval
= device_register(&port
->dev
);
999 dev_err(&port
->dev
, "Error registering port device, "
1003 usb_serial_console_init(debug
, minor
);
1007 usb_set_intfdata(interface
, serial
);
1011 for (i
= 0; i
< num_bulk_in
; ++i
) {
1012 port
= serial
->port
[i
];
1015 usb_free_urb(port
->read_urb
);
1016 kfree(port
->bulk_in_buffer
);
1018 for (i
= 0; i
< num_bulk_out
; ++i
) {
1019 port
= serial
->port
[i
];
1022 usb_free_urb(port
->write_urb
);
1023 kfree(port
->bulk_out_buffer
);
1025 for (i
= 0; i
< num_interrupt_in
; ++i
) {
1026 port
= serial
->port
[i
];
1029 usb_free_urb(port
->interrupt_in_urb
);
1030 kfree(port
->interrupt_in_buffer
);
1032 for (i
= 0; i
< num_interrupt_out
; ++i
) {
1033 port
= serial
->port
[i
];
1036 usb_free_urb(port
->interrupt_out_urb
);
1037 kfree(port
->interrupt_out_buffer
);
1040 /* free up any memory that we allocated */
1041 for (i
= 0; i
< serial
->num_port_pointers
; ++i
)
1042 kfree(serial
->port
[i
]);
1046 EXPORT_SYMBOL_GPL(usb_serial_probe
);
1048 void usb_serial_disconnect(struct usb_interface
*interface
)
1051 struct usb_serial
*serial
= usb_get_intfdata(interface
);
1052 struct device
*dev
= &interface
->dev
;
1053 struct usb_serial_port
*port
;
1055 usb_serial_console_disconnect(serial
);
1056 dbg("%s", __func__
);
1058 mutex_lock(&serial
->disc_mutex
);
1059 usb_set_intfdata(interface
, NULL
);
1060 /* must set a flag, to signal subdrivers */
1061 serial
->disconnected
= 1;
1062 mutex_unlock(&serial
->disc_mutex
);
1064 /* Unfortunately, many of the sub-drivers expect the port structures
1065 * to exist when their shutdown method is called, so we have to go
1066 * through this awkward two-step unregistration procedure.
1068 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1069 port
= serial
->port
[i
];
1071 struct tty_struct
*tty
= tty_port_tty_get(&port
->port
);
1077 cancel_work_sync(&port
->work
);
1078 device_del(&port
->dev
);
1081 serial
->type
->shutdown(serial
);
1082 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1083 port
= serial
->port
[i
];
1085 put_device(&port
->dev
);
1086 serial
->port
[i
] = NULL
;
1090 /* let the last holder of this object
1091 * cause it to be cleaned up */
1092 usb_serial_put(serial
);
1093 dev_info(dev
, "device disconnected\n");
1095 EXPORT_SYMBOL_GPL(usb_serial_disconnect
);
1097 int usb_serial_suspend(struct usb_interface
*intf
, pm_message_t message
)
1099 struct usb_serial
*serial
= usb_get_intfdata(intf
);
1100 struct usb_serial_port
*port
;
1103 serial
->suspending
= 1;
1105 for (i
= 0; i
< serial
->num_ports
; ++i
) {
1106 port
= serial
->port
[i
];
1111 if (serial
->type
->suspend
)
1112 r
= serial
->type
->suspend(serial
, message
);
1116 EXPORT_SYMBOL(usb_serial_suspend
);
1118 int usb_serial_resume(struct usb_interface
*intf
)
1120 struct usb_serial
*serial
= usb_get_intfdata(intf
);
1123 serial
->suspending
= 0;
1124 if (serial
->type
->resume
)
1125 rv
= serial
->type
->resume(serial
);
1127 rv
= usb_serial_generic_resume(serial
);
1131 EXPORT_SYMBOL(usb_serial_resume
);
1133 static const struct tty_operations serial_ops
= {
1134 .open
= serial_open
,
1135 .close
= serial_close
,
1136 .write
= serial_write
,
1137 .write_room
= serial_write_room
,
1138 .ioctl
= serial_ioctl
,
1139 .set_termios
= serial_set_termios
,
1140 .throttle
= serial_throttle
,
1141 .unthrottle
= serial_unthrottle
,
1142 .break_ctl
= serial_break
,
1143 .chars_in_buffer
= serial_chars_in_buffer
,
1144 .tiocmget
= serial_tiocmget
,
1145 .tiocmset
= serial_tiocmset
,
1146 .proc_fops
= &serial_proc_fops
,
1149 struct tty_driver
*usb_serial_tty_driver
;
1151 static int __init
usb_serial_init(void)
1156 usb_serial_tty_driver
= alloc_tty_driver(SERIAL_TTY_MINORS
);
1157 if (!usb_serial_tty_driver
)
1160 /* Initialize our global data */
1161 for (i
= 0; i
< SERIAL_TTY_MINORS
; ++i
)
1162 serial_table
[i
] = NULL
;
1164 result
= bus_register(&usb_serial_bus_type
);
1166 printk(KERN_ERR
"usb-serial: %s - registering bus driver "
1167 "failed\n", __func__
);
1171 usb_serial_tty_driver
->owner
= THIS_MODULE
;
1172 usb_serial_tty_driver
->driver_name
= "usbserial";
1173 usb_serial_tty_driver
->name
= "ttyUSB";
1174 usb_serial_tty_driver
->major
= SERIAL_TTY_MAJOR
;
1175 usb_serial_tty_driver
->minor_start
= 0;
1176 usb_serial_tty_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1177 usb_serial_tty_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1178 usb_serial_tty_driver
->flags
= TTY_DRIVER_REAL_RAW
|
1179 TTY_DRIVER_DYNAMIC_DEV
;
1180 usb_serial_tty_driver
->init_termios
= tty_std_termios
;
1181 usb_serial_tty_driver
->init_termios
.c_cflag
= B9600
| CS8
| CREAD
1183 usb_serial_tty_driver
->init_termios
.c_ispeed
= 9600;
1184 usb_serial_tty_driver
->init_termios
.c_ospeed
= 9600;
1185 tty_set_operations(usb_serial_tty_driver
, &serial_ops
);
1186 result
= tty_register_driver(usb_serial_tty_driver
);
1188 printk(KERN_ERR
"usb-serial: %s - tty_register_driver failed\n",
1190 goto exit_reg_driver
;
1193 /* register the USB driver */
1194 result
= usb_register(&usb_serial_driver
);
1196 printk(KERN_ERR
"usb-serial: %s - usb_register failed\n",
1201 /* register the generic driver, if we should */
1202 result
= usb_serial_generic_register(debug
);
1204 printk(KERN_ERR
"usb-serial: %s - registering generic "
1205 "driver failed\n", __func__
);
1209 printk(KERN_INFO KBUILD_MODNAME
": " DRIVER_DESC
"\n");
1214 usb_deregister(&usb_serial_driver
);
1217 tty_unregister_driver(usb_serial_tty_driver
);
1220 bus_unregister(&usb_serial_bus_type
);
1223 printk(KERN_ERR
"usb-serial: %s - returning with error %d\n",
1225 put_tty_driver(usb_serial_tty_driver
);
1230 static void __exit
usb_serial_exit(void)
1232 usb_serial_console_exit();
1234 usb_serial_generic_deregister();
1236 usb_deregister(&usb_serial_driver
);
1237 tty_unregister_driver(usb_serial_tty_driver
);
1238 put_tty_driver(usb_serial_tty_driver
);
1239 bus_unregister(&usb_serial_bus_type
);
1243 module_init(usb_serial_init
);
1244 module_exit(usb_serial_exit
);
1246 #define set_to_generic_if_null(type, function) \
1248 if (!type->function) { \
1249 type->function = usb_serial_generic_##function; \
1250 dbg("Had to override the " #function \
1251 " usb serial operation with the generic one.");\
1255 static void fixup_generic(struct usb_serial_driver
*device
)
1257 set_to_generic_if_null(device
, open
);
1258 set_to_generic_if_null(device
, write
);
1259 set_to_generic_if_null(device
, close
);
1260 set_to_generic_if_null(device
, write_room
);
1261 set_to_generic_if_null(device
, chars_in_buffer
);
1262 set_to_generic_if_null(device
, read_bulk_callback
);
1263 set_to_generic_if_null(device
, write_bulk_callback
);
1264 set_to_generic_if_null(device
, shutdown
);
1267 int usb_serial_register(struct usb_serial_driver
*driver
)
1269 /* must be called with BKL held */
1275 fixup_generic(driver
);
1277 if (!driver
->description
)
1278 driver
->description
= driver
->driver
.name
;
1280 /* Add this device to our list of devices */
1281 list_add(&driver
->driver_list
, &usb_serial_driver_list
);
1283 retval
= usb_serial_bus_register(driver
);
1285 printk(KERN_ERR
"usb-serial: problem %d when registering "
1286 "driver %s\n", retval
, driver
->description
);
1287 list_del(&driver
->driver_list
);
1289 printk(KERN_INFO
"USB Serial support registered for %s\n",
1290 driver
->description
);
1294 EXPORT_SYMBOL_GPL(usb_serial_register
);
1297 void usb_serial_deregister(struct usb_serial_driver
*device
)
1299 /* must be called with BKL held */
1300 printk(KERN_INFO
"USB Serial deregistering driver %s\n",
1301 device
->description
);
1302 list_del(&device
->driver_list
);
1303 usb_serial_bus_deregister(device
);
1305 EXPORT_SYMBOL_GPL(usb_serial_deregister
);
1307 /* Module information */
1308 MODULE_AUTHOR(DRIVER_AUTHOR
);
1309 MODULE_DESCRIPTION(DRIVER_DESC
);
1310 MODULE_LICENSE("GPL");
1312 module_param(debug
, bool, S_IRUGO
| S_IWUSR
);
1313 MODULE_PARM_DESC(debug
, "Debug enabled or not");