20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/tty.h>
29 #include <linux/module.h>
34 #include <linux/list.h>
36 #include <linux/serial.h>
43 #define DRIVER_DESC "USB Serial Driver core"
67 serial = serial_table[
index];
75 kref_get(&serial->
kref);
97 for (j = 1; j <= num_ports-1; ++
j)
98 if ((i+j >= SERIAL_TTY_MINORS) || (serial_table[i+j])) {
108 dev_dbg(&serial->
interface->dev,
"%s - minor base = %d\n", __func__, *minor);
111 serial->
port[j++]->number =
i;
120 static void return_serial(
struct usb_serial *serial)
130 static void destroy_serial(
struct kref *
kref)
140 return_serial(serial);
143 serial->
type->release(serial);
147 port = serial->
port[
i];
160 kref_put(&serial->
kref, destroy_serial);
192 port = serial->
port[idx - serial->
minor];
195 if (!try_module_get(serial->
type->driver.owner))
196 goto error_module_get;
198 retval = usb_autopm_get_interface(serial->
interface);
200 goto error_get_interface;
204 goto error_init_termios;
209 if (serial->
type->init_termios)
210 serial->
type->init_termios(tty);
217 usb_autopm_put_interface(serial->
interface);
219 module_put(serial->
type->driver.owner);
238 retval = port->
serial->type->open(tty, port);
242 retval = usb_translate_errors(retval);
264 static void serial_down(
struct tty_port *tport)
273 if (port->
port.console)
279 static void serial_hangup(
struct tty_struct *tty)
287 static void serial_close(
struct tty_struct *tty,
struct file *filp)
304 static void serial_cleanup(
struct tty_struct *tty)
313 if (port->
port.console)
321 owner = serial->
type->driver.owner;
325 usb_autopm_put_interface(serial->
interface);
332 static int serial_write(
struct tty_struct *tty,
const unsigned char *
buf,
341 dev_dbg(tty->
dev,
"%s - port %d, %d byte(s)\n", __func__,
345 retval = port->
serial->type->write(tty, port, buf, count);
347 retval = usb_translate_errors(retval);
352 static int serial_write_room(
struct tty_struct *tty)
358 return port->
serial->type->write_room(tty);
361 static int serial_chars_in_buffer(
struct tty_struct *tty)
369 if (port->
serial->disconnected)
372 return port->
serial->type->chars_in_buffer(tty);
375 static void serial_throttle(
struct tty_struct *tty)
382 if (port->
serial->type->throttle)
383 port->
serial->type->throttle(tty);
386 static void serial_unthrottle(
struct tty_struct *tty)
393 if (port->
serial->type->unthrottle)
394 port->
serial->type->unthrottle(tty);
397 static int serial_ioctl(
struct tty_struct *tty,
398 unsigned int cmd,
unsigned long arg)
403 dev_dbg(tty->
dev,
"%s - port %d, cmd 0x%.4x\n", __func__,
408 if (port->
serial->type->ioctl) {
409 retval = port->
serial->type->ioctl(tty, cmd, arg);
423 if (port->
serial->type->set_termios)
424 port->
serial->type->set_termios(tty, port, old);
429 static int serial_break(
struct tty_struct *tty,
int break_state)
437 if (port->
serial->type->break_ctl)
438 port->
serial->type->break_ctl(tty, break_state);
442 static int serial_proc_show(
struct seq_file *
m,
void *
v)
448 seq_puts(m,
"usbserinfo:1.0 driver:2.0\n");
455 if (serial->
type->driver.owner)
459 serial->
type->description);
465 usb_make_path(serial->
dev, tmp,
sizeof(tmp));
482 .open = serial_proc_open,
488 static int serial_tiocmget(
struct tty_struct *tty)
494 if (port->
serial->type->tiocmget)
495 return port->
serial->type->tiocmget(tty);
499 static int serial_tiocmset(
struct tty_struct *tty,
500 unsigned int set,
unsigned int clear)
506 if (port->
serial->type->tiocmset)
507 return port->
serial->type->tiocmset(tty,
set, clear);
511 static int serial_get_icount(
struct tty_struct *tty,
518 if (port->
serial->type->get_icount)
519 return port->
serial->type->get_icount(tty, icount);
573 static void port_release(
struct device *
dev)
578 dev_dbg(dev,
"%s\n", __func__);
603 static struct usb_serial *create_serial(
struct usb_device *dev,
609 serial = kzalloc(
sizeof(*serial),
GFP_KERNEL);
611 dev_err(&dev->dev,
"%s - out of memory\n", __func__);
617 kref_init(&serial->
kref);
627 struct usb_dynid *dynid;
629 spin_lock(&drv->
dynids.lock);
632 spin_unlock(&drv->
dynids.lock);
636 spin_unlock(&drv->
dynids.lock);
647 dev_dbg(&intf->dev,
"static descriptor matches\n");
650 id = match_dynamic_id(intf, drv);
652 dev_dbg(&intf->dev,
"dynamic descriptor matches\n");
663 struct usb_driver *driver = to_usb_driver(iface->dev.driver);
668 id = get_iface_id(drv, iface);
676 static int serial_carrier_raised(
struct tty_port *port)
687 static void serial_dtr_rts(
struct tty_port *port,
int on)
697 .carrier_raised = serial_carrier_raised,
698 .dtr_rts = serial_dtr_rts,
699 .activate = serial_activate,
700 .shutdown = serial_down,
706 struct device *ddev = &interface->dev;
707 struct usb_device *dev = interface_to_usbdev(interface);
710 struct usb_host_interface *iface_desc;
722 int num_interrupt_in = 0;
723 int num_interrupt_out = 0;
725 int num_bulk_out = 0;
730 type = search_serial_device(interface);
733 dev_dbg(ddev,
"none matched\n");
737 if (!try_module_get(type->
driver.owner)) {
739 dev_err(ddev,
"module get failed, exiting\n");
744 serial = create_serial(dev, interface, type);
746 module_put(type->
driver.owner);
747 dev_err(ddev,
"%s - out of memory\n", __func__);
755 id = get_iface_id(type, interface);
756 retval = type->
probe(serial,
id);
759 dev_dbg(ddev,
"sub driver rejected device\n");
761 module_put(type->
driver.owner);
768 iface_desc = interface->cur_altsetting;
769 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
770 endpoint = &iface_desc->endpoint[
i].desc;
772 if (usb_endpoint_is_bulk_in(endpoint)) {
774 dev_dbg(ddev,
"found bulk in on endpoint %d\n", i);
775 bulk_in_endpoint[num_bulk_in] = endpoint;
779 if (usb_endpoint_is_bulk_out(endpoint)) {
781 dev_dbg(ddev,
"found bulk out on endpoint %d\n", i);
782 bulk_out_endpoint[num_bulk_out] = endpoint;
786 if (usb_endpoint_is_int_in(endpoint)) {
788 dev_dbg(ddev,
"found interrupt in on endpoint %d\n", i);
789 interrupt_in_endpoint[num_interrupt_in] = endpoint;
793 if (usb_endpoint_is_int_out(endpoint)) {
795 dev_dbg(ddev,
"found interrupt out on endpoint %d\n", i);
796 interrupt_out_endpoint[num_interrupt_out] = endpoint;
801 #if defined(CONFIG_USB_SERIAL_PL2303) || defined(CONFIG_USB_SERIAL_PL2303_MODULE)
812 if (interface != dev->actconfig->interface[0]) {
814 iface_desc = dev->actconfig->interface[0]->cur_altsetting;
815 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
816 endpoint = &iface_desc->endpoint[
i].desc;
817 if (usb_endpoint_is_int_in(endpoint)) {
819 dev_dbg(ddev,
"found interrupt in for Prolific device on separate interface\n");
820 interrupt_in_endpoint[num_interrupt_in] = endpoint;
830 if (num_bulk_in == 0 || num_bulk_out == 0) {
831 dev_info(ddev,
"PL-2303 hack: descriptors matched but endpoints did not\n");
833 module_put(type->
driver.owner);
840 #ifdef CONFIG_USB_SERIAL_GENERIC
841 if (type == &usb_serial_generic_device) {
842 num_ports = num_bulk_out;
843 if (num_ports == 0) {
844 dev_err(ddev,
"Generic device with no bulk out, not allowed.\n");
846 module_put(type->
driver.owner);
849 dev_info(ddev,
"The \"generic\" usb-serial driver is only for testing and one-off prototypes.\n");
873 max_endpoints =
max(num_bulk_in, num_bulk_out);
874 max_endpoints =
max(max_endpoints, num_interrupt_in);
875 max_endpoints =
max(max_endpoints, num_interrupt_out);
876 max_endpoints =
max(max_endpoints, (
int)serial->
num_ports);
879 dev_dbg(ddev,
"setting up %d port structures for this device", max_endpoints);
880 for (i = 0; i < max_endpoints; ++
i) {
885 port->
port.ops = &serial_port_ops;
892 port->
dev.parent = &interface->dev;
895 port->
dev.release = &port_release;
900 for (i = 0; i < num_bulk_in; ++
i) {
901 endpoint = bulk_in_endpoint[
i];
902 port = serial->
port[
i];
903 buffer_size =
max_t(
int, serial->
type->bulk_in_size,
904 usb_endpoint_maxp(endpoint));
912 dev_err(ddev,
"No free urbs available\n");
918 dev_err(ddev,
"Couldn't allocate bulk_in_buffer\n");
921 usb_fill_bulk_urb(port->
read_urbs[j], dev,
925 serial->
type->read_bulk_callback,
933 for (i = 0; i < num_bulk_out; ++
i) {
934 endpoint = bulk_out_endpoint[
i];
935 port = serial->
port[
i];
938 buffer_size = serial->
type->bulk_out_size;
940 buffer_size = usb_endpoint_maxp(endpoint);
948 dev_err(ddev,
"No free urbs available\n");
954 dev_err(ddev,
"Couldn't allocate bulk_out_buffer\n");
961 serial->
type->write_bulk_callback,
969 if (serial->
type->read_int_callback) {
970 for (i = 0; i < num_interrupt_in; ++
i) {
971 endpoint = interrupt_in_endpoint[
i];
972 port = serial->
port[
i];
975 dev_err(ddev,
"No free urbs available\n");
978 buffer_size = usb_endpoint_maxp(endpoint);
984 dev_err(ddev,
"Couldn't allocate interrupt_in_buffer\n");
991 serial->
type->read_int_callback, port,
994 }
else if (num_interrupt_in) {
995 dev_dbg(ddev,
"The device claims to support interrupt in transfers, but read_int_callback is not defined\n");
998 if (serial->
type->write_int_callback) {
999 for (i = 0; i < num_interrupt_out; ++
i) {
1000 endpoint = interrupt_out_endpoint[
i];
1001 port = serial->
port[
i];
1004 dev_err(ddev,
"No free urbs available\n");
1007 buffer_size = usb_endpoint_maxp(endpoint);
1014 dev_err(ddev,
"Couldn't allocate interrupt_out_buffer\n");
1021 serial->
type->write_int_callback, port,
1024 }
else if (num_interrupt_out) {
1025 dev_dbg(ddev,
"The device claims to support interrupt out transfers, but write_int_callback is not defined\n");
1028 usb_set_intfdata(interface, serial);
1032 retval = type->
attach(serial);
1052 if (get_free_serial(serial, num_ports, &minor) ==
NULL) {
1053 dev_err(ddev,
"No more free serial devices\n");
1056 serial->
minor = minor;
1059 for (i = 0; i < num_ports; ++
i) {
1060 port = serial->
port[
i];
1062 dev_dbg(ddev,
"registering %s", dev_name(&port->
dev));
1063 device_enable_async_suspend(&port->
dev);
1067 dev_err(ddev,
"Error registering port device, continuing\n");
1074 module_put(type->
driver.owner);
1079 module_put(type->
driver.owner);
1083 static void usb_serial_disconnect(
struct usb_interface *interface)
1086 struct usb_serial *serial = usb_get_intfdata(interface);
1087 struct device *dev = &interface->dev;
1098 port = serial->
port[
i];
1107 if (device_is_registered(&port->
dev))
1111 serial->
type->disconnect(serial);
1115 dev_info(dev,
"device disconnected\n");
1120 struct usb_serial *serial = usb_get_intfdata(intf);
1126 if (serial->
type->suspend) {
1127 r = serial->
type->suspend(serial, message);
1135 port = serial->
port[
i];
1147 struct usb_serial *serial = usb_get_intfdata(intf);
1151 if (serial->
type->resume)
1152 rv = serial->
type->resume(serial);
1160 static int usb_serial_reset_resume(
struct usb_interface *intf)
1162 struct usb_serial *serial = usb_get_intfdata(intf);
1166 if (serial->
type->reset_resume)
1167 rv = serial->
type->reset_resume(serial);
1170 intf->needs_binding = 1;
1177 .open = serial_open,
1178 .close = serial_close,
1179 .write = serial_write,
1180 .hangup = serial_hangup,
1181 .write_room = serial_write_room,
1182 .ioctl = serial_ioctl,
1183 .set_termios = serial_set_termios,
1184 .throttle = serial_throttle,
1185 .unthrottle = serial_unthrottle,
1186 .break_ctl = serial_break,
1187 .chars_in_buffer = serial_chars_in_buffer,
1188 .tiocmget = serial_tiocmget,
1189 .tiocmset = serial_tiocmset,
1190 .get_icount = serial_get_icount,
1191 .cleanup = serial_cleanup,
1192 .install = serial_install,
1193 .proc_fops = &serial_proc_fops,
1201 .name =
"usbserial",
1202 .
probe = usb_serial_probe,
1203 .disconnect = usb_serial_disconnect,
1207 .supports_autosuspend = 1,
1210 static int __init usb_serial_init(
void)
1215 usb_serial_tty_driver = alloc_tty_driver(SERIAL_TTY_MINORS);
1216 if (!usb_serial_tty_driver)
1221 serial_table[i] =
NULL;
1225 pr_err(
"%s - registering bus driver failed\n", __func__);
1230 usb_serial_tty_driver->
name =
"ttyUSB";
1245 pr_err(
"%s - tty_register_driver failed\n", __func__);
1246 goto exit_reg_driver;
1250 result = usb_register(&usb_serial_driver);
1252 pr_err(
"%s - usb_register failed\n", __func__);
1259 pr_err(
"%s - registering generic driver failed\n", __func__);
1275 pr_err(
"%s - returning with error %d\n", __func__, result);
1281 static void __exit usb_serial_exit(
void)
1297 #define set_to_generic_if_null(type, function) \
1299 if (!type->function) { \
1300 type->function = usb_serial_generic_##function; \
1301 pr_debug("Had to override the " #function \
1302 " usb serial operation with the generic one.");\
1306 static void fixup_generic(
struct usb_serial_driver *
device)
1321 static int usb_serial_register(
struct usb_serial_driver *driver)
1328 fixup_generic(driver);
1333 WARN(1,
"Serial driver %s has no usb_driver\n",
1340 list_add(&driver->
driver_list, &usb_serial_driver_list);
1353 static void usb_serial_deregister(
struct usb_serial_driver *
device)
1376 struct usb_driver *udriver;
1377 struct usb_serial_driver *
const *
sd;
1392 udriver = kzalloc(
sizeof(*udriver),
GFP_KERNEL);
1396 udriver->name =
name;
1397 udriver->no_dynamic_id = 1;
1398 udriver->supports_autosuspend = 1;
1401 udriver->probe = usb_serial_probe;
1402 udriver->disconnect = usb_serial_disconnect;
1405 for (sd = serial_drivers; *sd; ++sd) {
1406 if ((*sd)->reset_resume) {
1407 udriver->reset_resume = usb_serial_reset_resume;
1412 rc = usb_register(udriver);
1416 for (sd = serial_drivers; *sd; ++sd) {
1417 (*sd)->usb_driver = udriver;
1418 rc = usb_serial_register(*sd);
1429 while (sd-- > serial_drivers)
1430 usb_serial_deregister(*sd);
1446 struct usb_driver *udriver = (*serial_drivers)->usb_driver;
1448 for (; *serial_drivers; ++serial_drivers)
1449 usb_serial_deregister(*serial_drivers);