Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * USB Serial Converter Bus specific functions |
| 4 | * |
| 5 | * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/errno.h> |
| 10 | #include <linux/tty.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 14 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 16 | static int usb_serial_device_match(struct device *dev, |
| 17 | struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 19 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | const struct usb_serial_port *port; |
| 21 | |
| 22 | /* |
| 23 | * drivers are already assigned to ports in serial_probe so it's |
| 24 | * a simple check here. |
| 25 | */ |
| 26 | port = to_usb_serial_port(dev); |
| 27 | if (!port) |
| 28 | return 0; |
| 29 | |
| 30 | driver = to_usb_serial_driver(drv); |
| 31 | |
| 32 | if (driver == port->serial->type) |
| 33 | return 1; |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 38 | static int usb_serial_device_probe(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 40 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct usb_serial_port *port; |
Johan Hovold | ca4383a | 2015-02-18 10:34:51 +0700 | [diff] [blame] | 42 | struct device *tty_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | int retval = 0; |
| 44 | int minor; |
| 45 | |
| 46 | port = to_usb_serial_port(dev); |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 47 | if (!port) |
| 48 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 50 | /* make sure suspend/resume doesn't race against port_probe */ |
| 51 | retval = usb_autopm_get_interface(port->serial->interface); |
| 52 | if (retval) |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 53 | return retval; |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | driver = port->serial->type; |
| 56 | if (driver->port_probe) { |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 57 | retval = driver->port_probe(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | if (retval) |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 59 | goto err_autopm_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Greg Kroah-Hartman | 1143832 | 2013-06-06 10:32:00 -0700 | [diff] [blame] | 62 | minor = port->minor; |
Johan Hovold | 79ef518 | 2018-05-16 11:42:07 +0200 | [diff] [blame] | 63 | tty_dev = tty_port_register_device(&port->port, usb_serial_tty_driver, |
| 64 | minor, dev); |
Johan Hovold | ca4383a | 2015-02-18 10:34:51 +0700 | [diff] [blame] | 65 | if (IS_ERR(tty_dev)) { |
| 66 | retval = PTR_ERR(tty_dev); |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 67 | goto err_port_remove; |
Johan Hovold | ca4383a | 2015-02-18 10:34:51 +0700 | [diff] [blame] | 68 | } |
| 69 | |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 70 | usb_autopm_put_interface(port->serial->interface); |
| 71 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 72 | dev_info(&port->serial->dev->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | "%s converter now attached to ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 74 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 76 | return 0; |
| 77 | |
| 78 | err_port_remove: |
| 79 | if (driver->port_remove) |
| 80 | driver->port_remove(port); |
| 81 | err_autopm_put: |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 82 | usb_autopm_put_interface(port->serial->interface); |
Johan Hovold | d6f7f41 | 2015-02-18 11:04:46 +0700 | [diff] [blame] | 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return retval; |
| 85 | } |
| 86 | |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 87 | static int usb_serial_device_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 89 | struct usb_serial_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | struct usb_serial_port *port; |
| 91 | int retval = 0; |
| 92 | int minor; |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 93 | int autopm_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | port = to_usb_serial_port(dev); |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 96 | if (!port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Make sure suspend/resume doesn't race against port_remove. |
| 101 | * |
| 102 | * Note that no further runtime PM callbacks will be made if |
| 103 | * autopm_get fails. |
| 104 | */ |
| 105 | autopm_err = usb_autopm_get_interface(port->serial->interface); |
Bjørn Mork | d5fd650 | 2012-07-27 01:11:42 +0200 | [diff] [blame] | 106 | |
Greg Kroah-Hartman | 1143832 | 2013-06-06 10:32:00 -0700 | [diff] [blame] | 107 | minor = port->minor; |
Johan Hovold | 5cb27dd | 2013-03-21 12:36:50 +0100 | [diff] [blame] | 108 | tty_unregister_device(usb_serial_tty_driver, minor); |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | driver = port->serial->type; |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 111 | if (driver->port_remove) |
Alan Cox | c97611c | 2008-07-22 11:09:48 +0100 | [diff] [blame] | 112 | retval = driver->port_remove(port); |
Alan Stern | c706ebd | 2009-06-02 11:54:11 -0400 | [diff] [blame] | 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | dev_info(dev, "%s converter now disconnected from ttyUSB%d\n", |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 115 | driver->description, minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Johan Hovold | c14829f | 2014-05-26 19:23:33 +0200 | [diff] [blame] | 117 | if (!autopm_err) |
| 118 | usb_autopm_put_interface(port->serial->interface); |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return retval; |
| 121 | } |
| 122 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 123 | static ssize_t new_id_store(struct device_driver *driver, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 124 | const char *buf, size_t count) |
| 125 | { |
| 126 | struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); |
Wolfram Sang | 2fc82c2 | 2014-01-10 19:36:42 +0100 | [diff] [blame] | 127 | ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, |
| 128 | driver, buf, count); |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 129 | |
| 130 | if (retval >= 0 && usb_drv->usb_driver != NULL) |
| 131 | retval = usb_store_new_id(&usb_drv->usb_driver->dynids, |
Wolfram Sang | 2fc82c2 | 2014-01-10 19:36:42 +0100 | [diff] [blame] | 132 | usb_drv->usb_driver->id_table, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 133 | &usb_drv->usb_driver->drvwrap.driver, |
| 134 | buf, count); |
| 135 | return retval; |
| 136 | } |
| 137 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 138 | static ssize_t new_id_show(struct device_driver *driver, char *buf) |
Bjørn Mork | ef206f3 | 2012-05-13 12:35:00 +0200 | [diff] [blame] | 139 | { |
| 140 | struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver); |
| 141 | |
| 142 | return usb_show_dynids(&usb_drv->dynids, buf); |
| 143 | } |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 144 | static DRIVER_ATTR_RW(new_id); |
Bjørn Mork | ef206f3 | 2012-05-13 12:35:00 +0200 | [diff] [blame] | 145 | |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 146 | static struct attribute *usb_serial_drv_attrs[] = { |
| 147 | &driver_attr_new_id.attr, |
| 148 | NULL, |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 149 | }; |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 150 | ATTRIBUTE_GROUPS(usb_serial_drv); |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 151 | |
| 152 | static void free_dynids(struct usb_serial_driver *drv) |
| 153 | { |
| 154 | struct usb_dynid *dynid, *n; |
| 155 | |
| 156 | spin_lock(&drv->dynids.lock); |
| 157 | list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) { |
| 158 | list_del(&dynid->node); |
| 159 | kfree(dynid); |
| 160 | } |
| 161 | spin_unlock(&drv->dynids.lock); |
| 162 | } |
| 163 | |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 164 | struct bus_type usb_serial_bus_type = { |
| 165 | .name = "usb-serial", |
| 166 | .match = usb_serial_device_match, |
| 167 | .probe = usb_serial_device_probe, |
| 168 | .remove = usb_serial_device_remove, |
Greg Kroah-Hartman | d13a280 | 2013-08-23 14:24:36 -0700 | [diff] [blame] | 169 | .drv_groups = usb_serial_drv_groups, |
Russell King | ac33bc3 | 2006-01-05 14:43:11 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 172 | int usb_serial_bus_register(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
| 174 | int retval; |
| 175 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 176 | driver->driver.bus = &usb_serial_bus_type; |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 177 | spin_lock_init(&driver->dynids.lock); |
| 178 | INIT_LIST_HEAD(&driver->dynids.list); |
| 179 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 180 | retval = driver_register(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | return retval; |
| 183 | } |
| 184 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 185 | void usb_serial_bus_deregister(struct usb_serial_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 187 | free_dynids(driver); |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 188 | driver_unregister(&driver->driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |