1
2
3
4
5
6
7
8
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
19static 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
27
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
41static ssize_t port_number_show(struct device *dev,
42 struct device_attribute *attr, char *buf)
43{
44 struct usb_serial_port *port = to_usb_serial_port(dev);
45
46 return sprintf(buf, "%d\n", port->port_number);
47}
48static DEVICE_ATTR_RO(port_number);
49
50static int usb_serial_device_probe(struct device *dev)
51{
52 struct usb_serial_driver *driver;
53 struct usb_serial_port *port;
54 int retval = 0;
55 int minor;
56
57 port = to_usb_serial_port(dev);
58 if (!port) {
59 retval = -ENODEV;
60 goto exit;
61 }
62
63
64 retval = usb_autopm_get_interface(port->serial->interface);
65 if (retval)
66 goto exit;
67
68 driver = port->serial->type;
69 if (driver->port_probe) {
70 retval = driver->port_probe(port);
71 if (retval)
72 goto exit_with_autopm;
73 }
74
75 retval = device_create_file(dev, &dev_attr_port_number);
76 if (retval) {
77 if (driver->port_remove)
78 retval = driver->port_remove(port);
79 goto exit_with_autopm;
80 }
81
82 minor = port->minor;
83 tty_register_device(usb_serial_tty_driver, minor, dev);
84 dev_info(&port->serial->dev->dev,
85 "%s converter now attached to ttyUSB%d\n",
86 driver->description, minor);
87
88exit_with_autopm:
89 usb_autopm_put_interface(port->serial->interface);
90exit:
91 return retval;
92}
93
94static int usb_serial_device_remove(struct device *dev)
95{
96 struct usb_serial_driver *driver;
97 struct usb_serial_port *port;
98 int retval = 0;
99 int minor;
100
101 port = to_usb_serial_port(dev);
102 if (!port)
103 return -ENODEV;
104
105
106 usb_autopm_get_interface(port->serial->interface);
107
108 minor = port->minor;
109 tty_unregister_device(usb_serial_tty_driver, minor);
110
111 device_remove_file(&port->dev, &dev_attr_port_number);
112
113 driver = port->serial->type;
114 if (driver->port_remove)
115 retval = driver->port_remove(port);
116
117 dev_info(dev, "%s converter now disconnected from ttyUSB%d\n",
118 driver->description, minor);
119
120 usb_autopm_put_interface(port->serial->interface);
121 return retval;
122}
123
124static ssize_t new_id_store(struct device_driver *driver,
125 const char *buf, size_t count)
126{
127 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
128 ssize_t retval = usb_store_new_id(&usb_drv->dynids, usb_drv->id_table,
129 driver, buf, count);
130
131 if (retval >= 0 && usb_drv->usb_driver != NULL)
132 retval = usb_store_new_id(&usb_drv->usb_driver->dynids,
133 usb_drv->usb_driver->id_table,
134 &usb_drv->usb_driver->drvwrap.driver,
135 buf, count);
136 return retval;
137}
138
139static ssize_t new_id_show(struct device_driver *driver, char *buf)
140{
141 struct usb_serial_driver *usb_drv = to_usb_serial_driver(driver);
142
143 return usb_show_dynids(&usb_drv->dynids, buf);
144}
145static DRIVER_ATTR_RW(new_id);
146
147static struct attribute *usb_serial_drv_attrs[] = {
148 &driver_attr_new_id.attr,
149 NULL,
150};
151ATTRIBUTE_GROUPS(usb_serial_drv);
152
153static void free_dynids(struct usb_serial_driver *drv)
154{
155 struct usb_dynid *dynid, *n;
156
157 spin_lock(&drv->dynids.lock);
158 list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
159 list_del(&dynid->node);
160 kfree(dynid);
161 }
162 spin_unlock(&drv->dynids.lock);
163}
164
165struct bus_type usb_serial_bus_type = {
166 .name = "usb-serial",
167 .match = usb_serial_device_match,
168 .probe = usb_serial_device_probe,
169 .remove = usb_serial_device_remove,
170 .drv_groups = usb_serial_drv_groups,
171};
172
173int usb_serial_bus_register(struct usb_serial_driver *driver)
174{
175 int retval;
176
177 driver->driver.bus = &usb_serial_bus_type;
178 spin_lock_init(&driver->dynids.lock);
179 INIT_LIST_HEAD(&driver->dynids.list);
180
181 retval = driver_register(&driver->driver);
182
183 return retval;
184}
185
186void usb_serial_bus_deregister(struct usb_serial_driver *driver)
187{
188 free_dynids(driver);
189 driver_unregister(&driver->driver);
190}
191
192