xref: /openbmc/linux/drivers/usb/serial/bus.c (revision dd1431e5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * USB Serial Converter Bus specific functions
4  *
5  * Copyright (C) 2002 Greg Kroah-Hartman (greg@kroah.com)
6  */
7 
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>
15 
16 static int usb_serial_device_match(struct device *dev,
17 						struct device_driver *drv)
18 {
19 	const struct usb_serial_port *port = to_usb_serial_port(dev);
20 	struct usb_serial_driver *driver = to_usb_serial_driver(drv);
21 
22 	/*
23 	 * drivers are already assigned to ports in serial_probe so it's
24 	 * a simple check here.
25 	 */
26 	if (driver == port->serial->type)
27 		return 1;
28 
29 	return 0;
30 }
31 
32 static int usb_serial_device_probe(struct device *dev)
33 {
34 	struct usb_serial_port *port = to_usb_serial_port(dev);
35 	struct usb_serial_driver *driver;
36 	struct device *tty_dev;
37 	int retval = 0;
38 	int minor;
39 
40 	/* make sure suspend/resume doesn't race against port_probe */
41 	retval = usb_autopm_get_interface(port->serial->interface);
42 	if (retval)
43 		return retval;
44 
45 	driver = port->serial->type;
46 	if (driver->port_probe) {
47 		retval = driver->port_probe(port);
48 		if (retval)
49 			goto err_autopm_put;
50 	}
51 
52 	minor = port->minor;
53 	tty_dev = tty_port_register_device(&port->port, usb_serial_tty_driver,
54 					   minor, dev);
55 	if (IS_ERR(tty_dev)) {
56 		retval = PTR_ERR(tty_dev);
57 		goto err_port_remove;
58 	}
59 
60 	usb_autopm_put_interface(port->serial->interface);
61 
62 	dev_info(&port->serial->dev->dev,
63 		 "%s converter now attached to ttyUSB%d\n",
64 		 driver->description, minor);
65 
66 	return 0;
67 
68 err_port_remove:
69 	if (driver->port_remove)
70 		driver->port_remove(port);
71 err_autopm_put:
72 	usb_autopm_put_interface(port->serial->interface);
73 
74 	return retval;
75 }
76 
77 static int usb_serial_device_remove(struct device *dev)
78 {
79 	struct usb_serial_port *port = to_usb_serial_port(dev);
80 	struct usb_serial_driver *driver;
81 	int minor;
82 	int autopm_err;
83 
84 	/*
85 	 * Make sure suspend/resume doesn't race against port_remove.
86 	 *
87 	 * Note that no further runtime PM callbacks will be made if
88 	 * autopm_get fails.
89 	 */
90 	autopm_err = usb_autopm_get_interface(port->serial->interface);
91 
92 	minor = port->minor;
93 	tty_unregister_device(usb_serial_tty_driver, minor);
94 
95 	driver = port->serial->type;
96 	if (driver->port_remove)
97 		driver->port_remove(port);
98 
99 	dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
100 		 driver->description, minor);
101 
102 	if (!autopm_err)
103 		usb_autopm_put_interface(port->serial->interface);
104 
105 	return 0;
106 }
107 
108 static ssize_t new_id_store(struct device_driver *driver,
109 			    const char *buf, size_t count)
110 {
111 	struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
112 	ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
113 					 driver, buf, count);
114 
115 	if (retval >= 0 && usb_drv->usb_driver != NULL)
116 		retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
117 					  usb_drv->usb_driver->id_table,
118 					  &usb_drv->usb_driver->drvwrap.driver,
119 					  buf, count);
120 	return retval;
121 }
122 
123 static ssize_t new_id_show(struct device_driver *driver, char *buf)
124 {
125 	struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
126 
127 	return usb_show_dynids(&usb_drv->dynids, buf);
128 }
129 static DRIVER_ATTR_RW(new_id);
130 
131 static struct attribute *usb_serial_drv_attrs[] = {
132 	&driver_attr_new_id.attr,
133 	NULL,
134 };
135 ATTRIBUTE_GROUPS(usb_serial_drv);
136 
137 static void free_dynids(struct usb_serial_driver *drv)
138 {
139 	struct usb_dynid *dynid, *n;
140 
141 	spin_lock(&drv->dynids.lock);
142 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
143 		list_del(&dynid->node);
144 		kfree(dynid);
145 	}
146 	spin_unlock(&drv->dynids.lock);
147 }
148 
149 struct bus_type usb_serial_bus_type = {
150 	.name =		"usb-serial",
151 	.match =	usb_serial_device_match,
152 	.probe =	usb_serial_device_probe,
153 	.remove =	usb_serial_device_remove,
154 	.drv_groups = 	usb_serial_drv_groups,
155 };
156 
157 int usb_serial_bus_register(struct usb_serial_driver *driver)
158 {
159 	int retval;
160 
161 	driver->driver.bus = &usb_serial_bus_type;
162 	spin_lock_init(&driver->dynids.lock);
163 	INIT_LIST_HEAD(&driver->dynids.list);
164 
165 	retval = driver_register(&driver->driver);
166 
167 	return retval;
168 }
169 
170 void usb_serial_bus_deregister(struct usb_serial_driver *driver)
171 {
172 	free_dynids(driver);
173 	driver_unregister(&driver->driver);
174 }
175 
176