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