x86/topology: Fix function name in documentation
[cris-mirror.git] / drivers / usb / serial / bus.c
blob9e265eb926111d7177509dd99b03e10895b58400
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Serial Converter Bus specific functions
5 * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com)
6 */
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/tty.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/usb.h>
14 #include <linux/usb/serial.h>
16 static int usb_serial_device_match(struct device *dev,
17 struct device_driver *drv)
19 struct usb_serial_driver *driver;
20 const struct usb_serial_port *port;
23 * drivers are already assigned to ports in serial_probe so it's
24 * a simple check here.
26 port = to_usb_serial_port(dev);
27 if (!port)
28 return 0;
30 driver = to_usb_serial_driver(drv);
32 if (driver == port->serial->type)
33 return 1;
35 return 0;
38 static int usb_serial_device_probe(struct device *dev)
40 struct usb_serial_driver *driver;
41 struct usb_serial_port *port;
42 struct device *tty_dev;
43 int retval = 0;
44 int minor;
46 port = to_usb_serial_port(dev);
47 if (!port)
48 return -ENODEV;
50 /* make sure suspend/resume doesn't race against port_probe */
51 retval = usb_autopm_get_interface(port->serial->interface);
52 if (retval)
53 return retval;
55 driver = port->serial->type;
56 if (driver->port_probe) {
57 retval = driver->port_probe(port);
58 if (retval)
59 goto err_autopm_put;
62 minor = port->minor;
63 tty_dev = tty_register_device(usb_serial_tty_driver, minor, dev);
64 if (IS_ERR(tty_dev)) {
65 retval = PTR_ERR(tty_dev);
66 goto err_port_remove;
69 usb_autopm_put_interface(port->serial->interface);
71 dev_info(&port->serial->dev->dev,
72 "%s converter now attached to ttyUSB%d\n",
73 driver->description, minor);
75 return 0;
77 err_port_remove:
78 if (driver->port_remove)
79 driver->port_remove(port);
80 err_autopm_put:
81 usb_autopm_put_interface(port->serial->interface);
83 return retval;
86 static int usb_serial_device_remove(struct device *dev)
88 struct usb_serial_driver *driver;
89 struct usb_serial_port *port;
90 int retval = 0;
91 int minor;
92 int autopm_err;
94 port = to_usb_serial_port(dev);
95 if (!port)
96 return -ENODEV;
99 * Make sure suspend/resume doesn't race against port_remove.
101 * Note that no further runtime PM callbacks will be made if
102 * autopm_get fails.
104 autopm_err = usb_autopm_get_interface(port->serial->interface);
106 minor = port->minor;
107 tty_unregister_device(usb_serial_tty_driver, minor);
109 driver = port->serial->type;
110 if (driver->port_remove)
111 retval = driver->port_remove(port);
113 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
114 driver->description, minor);
116 if (!autopm_err)
117 usb_autopm_put_interface(port->serial->interface);
119 return retval;
122 static ssize_t new_id_store(struct device_driver *driver,
123 const char *buf, size_t count)
125 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
126 ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
127 driver, buf, count);
129 if (retval >= 0 && usb_drv->usb_driver != NULL)
130 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
131 usb_drv->usb_driver->id_table,
132 &usb_drv->usb_driver->drvwrap.driver,
133 buf, count);
134 return retval;
137 static ssize_t new_id_show(struct device_driver *driver, char *buf)
139 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
141 return usb_show_dynids(&usb_drv->dynids, buf);
143 static DRIVER_ATTR_RW(new_id);
145 static struct attribute *usb_serial_drv_attrs[] = {
146 &driver_attr_new_id.attr,
147 NULL,
149 ATTRIBUTE_GROUPS(usb_serial_drv);
151 static void free_dynids(struct usb_serial_driver *drv)
153 struct usb_dynid *dynid, *n;
155 spin_lock(&drv->dynids.lock);
156 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
157 list_del(&dynid->node);
158 kfree(dynid);
160 spin_unlock(&drv->dynids.lock);
163 struct bus_type usb_serial_bus_type = {
164 .name = "usb-serial",
165 .match = usb_serial_device_match,
166 .probe = usb_serial_device_probe,
167 .remove = usb_serial_device_remove,
168 .drv_groups = usb_serial_drv_groups,
171 int usb_serial_bus_register(struct usb_serial_driver *driver)
173 int retval;
175 driver->driver.bus = &usb_serial_bus_type;
176 spin_lock_init(&driver->dynids.lock);
177 INIT_LIST_HEAD(&driver->dynids.list);
179 retval = driver_register(&driver->driver);
181 return retval;
184 void usb_serial_bus_deregister(struct usb_serial_driver *driver)
186 free_dynids(driver);
187 driver_unregister(&driver->driver);