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