aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/serial/usb-serial.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/serial/usb-serial.c')
-rw-r--r--drivers/usb/serial/usb-serial.c385
1 files changed, 173 insertions, 212 deletions
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index 99188c92068b..9d7ca4868d37 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -43,8 +43,6 @@
43#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/" 43#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com, http://www.kroah.com/linux/"
44#define DRIVER_DESC "USB Serial Driver core" 44#define DRIVER_DESC "USB Serial Driver core"
45 45
46static void port_free(struct usb_serial_port *port);
47
48/* Driver structure we register with the USB core */ 46/* Driver structure we register with the USB core */
49static struct usb_driver usb_serial_driver = { 47static struct usb_driver usb_serial_driver = {
50 .name = "usbserial", 48 .name = "usbserial",
@@ -68,6 +66,11 @@ static struct usb_serial *serial_table[SERIAL_TTY_MINORS];
68static DEFINE_MUTEX(table_lock); 66static DEFINE_MUTEX(table_lock);
69static LIST_HEAD(usb_serial_driver_list); 67static LIST_HEAD(usb_serial_driver_list);
70 68
69/*
70 * Look up the serial structure. If it is found and it hasn't been
71 * disconnected, return with its disc_mutex held and its refcount
72 * incremented. Otherwise return NULL.
73 */
71struct usb_serial *usb_serial_get_by_index(unsigned index) 74struct usb_serial *usb_serial_get_by_index(unsigned index)
72{ 75{
73 struct usb_serial *serial; 76 struct usb_serial *serial;
@@ -75,8 +78,15 @@ struct usb_serial *usb_serial_get_by_index(unsigned index)
75 mutex_lock(&table_lock); 78 mutex_lock(&table_lock);
76 serial = serial_table[index]; 79 serial = serial_table[index];
77 80
78 if (serial) 81 if (serial) {
79 kref_get(&serial->kref); 82 mutex_lock(&serial->disc_mutex);
83 if (serial->disconnected) {
84 mutex_unlock(&serial->disc_mutex);
85 serial = NULL;
86 } else {
87 kref_get(&serial->kref);
88 }
89 }
80 mutex_unlock(&table_lock); 90 mutex_unlock(&table_lock);
81 return serial; 91 return serial;
82} 92}
@@ -125,8 +135,10 @@ static void return_serial(struct usb_serial *serial)
125 135
126 dbg("%s", __func__); 136 dbg("%s", __func__);
127 137
138 mutex_lock(&table_lock);
128 for (i = 0; i < serial->num_ports; ++i) 139 for (i = 0; i < serial->num_ports; ++i)
129 serial_table[serial->minor + i] = NULL; 140 serial_table[serial->minor + i] = NULL;
141 mutex_unlock(&table_lock);
130} 142}
131 143
132static void destroy_serial(struct kref *kref) 144static void destroy_serial(struct kref *kref)
@@ -145,161 +157,157 @@ static void destroy_serial(struct kref *kref)
145 157
146 serial->type->release(serial); 158 serial->type->release(serial);
147 159
148 for (i = 0; i < serial->num_ports; ++i) { 160 /* Now that nothing is using the ports, they can be freed */
161 for (i = 0; i < serial->num_port_pointers; ++i) {
149 port = serial->port[i]; 162 port = serial->port[i];
150 if (port) 163 if (port) {
164 port->serial = NULL;
151 put_device(&port->dev); 165 put_device(&port->dev);
152 }
153
154 /* If this is a "fake" port, we have to clean it up here, as it will
155 * not get cleaned up in port_release() as it was never registered with
156 * the driver core */
157 if (serial->num_ports < serial->num_port_pointers) {
158 for (i = serial->num_ports;
159 i < serial->num_port_pointers; ++i) {
160 port = serial->port[i];
161 if (port)
162 port_free(port);
163 } 166 }
164 } 167 }
165 168
166 usb_put_dev(serial->dev); 169 usb_put_dev(serial->dev);
167
168 /* free up any memory that we allocated */
169 kfree(serial); 170 kfree(serial);
170} 171}
171 172
172void usb_serial_put(struct usb_serial *serial) 173void usb_serial_put(struct usb_serial *serial)
173{ 174{
174 mutex_lock(&table_lock);
175 kref_put(&serial->kref, destroy_serial); 175 kref_put(&serial->kref, destroy_serial);
176 mutex_unlock(&table_lock);
177} 176}
178 177
179/***************************************************************************** 178/*****************************************************************************
180 * Driver tty interface functions 179 * Driver tty interface functions
181 *****************************************************************************/ 180 *****************************************************************************/
182static int serial_open (struct tty_struct *tty, struct file *filp) 181
182/**
183 * serial_install - install tty
184 * @driver: the driver (USB in our case)
185 * @tty: the tty being created
186 *
187 * Create the termios objects for this tty. We use the default
188 * USB serial settings but permit them to be overridden by
189 * serial->type->init_termios.
190 *
191 * This is the first place a new tty gets used. Hence this is where we
192 * acquire references to the usb_serial structure and the driver module,
193 * where we store a pointer to the port, and where we do an autoresume.
194 * All these actions are reversed in serial_release().
195 */
196static int serial_install(struct tty_driver *driver, struct tty_struct *tty)
183{ 197{
198 int idx = tty->index;
184 struct usb_serial *serial; 199 struct usb_serial *serial;
185 struct usb_serial_port *port; 200 struct usb_serial_port *port;
186 unsigned int portNumber; 201 int retval = -ENODEV;
187 int retval = 0;
188 int first = 0;
189 202
190 dbg("%s", __func__); 203 dbg("%s", __func__);
191 204
192 /* get the serial object associated with this tty pointer */ 205 serial = usb_serial_get_by_index(idx);
193 serial = usb_serial_get_by_index(tty->index); 206 if (!serial)
194 if (!serial) { 207 return retval;
195 tty->driver_data = NULL;
196 return -ENODEV;
197 }
198 208
199 mutex_lock(&serial->disc_mutex); 209 port = serial->port[idx - serial->minor];
200 portNumber = tty->index - serial->minor; 210 if (!port)
201 port = serial->port[portNumber]; 211 goto error_no_port;
202 if (!port || serial->disconnected) 212 if (!try_module_get(serial->type->driver.owner))
203 retval = -ENODEV; 213 goto error_module_get;
204 else 214
205 get_device(&port->dev); 215 /* perform the standard setup */
206 /* 216 retval = tty_init_termios(tty);
207 * Note: Our locking order requirement does not allow port->mutex
208 * to be acquired while serial->disc_mutex is held.
209 */
210 mutex_unlock(&serial->disc_mutex);
211 if (retval) 217 if (retval)
212 goto bailout_serial_put; 218 goto error_init_termios;
213 219
214 if (mutex_lock_interruptible(&port->mutex)) { 220 retval = usb_autopm_get_interface(serial->interface);
215 retval = -ERESTARTSYS; 221 if (retval)
216 goto bailout_port_put; 222 goto error_get_interface;
217 } 223
224 mutex_unlock(&serial->disc_mutex);
218 225
219 ++port->port.count; 226 /* allow the driver to update the settings */
227 if (serial->type->init_termios)
228 serial->type->init_termios(tty);
220 229
221 /* set up our port structure making the tty driver
222 * remember our port object, and us it */
223 tty->driver_data = port; 230 tty->driver_data = port;
224 tty_port_tty_set(&port->port, tty);
225 231
226 /* If the console is attached, the device is already open */ 232 /* Final install (we use the default method) */
227 if (port->port.count == 1 && !port->console) { 233 tty_driver_kref_get(driver);
228 first = 1; 234 tty->count++;
229 /* lock this module before we call it 235 driver->ttys[idx] = tty;
230 * this may fail, which means we must bail out, 236 return retval;
231 * safe because we are called with BKL held */
232 if (!try_module_get(serial->type->driver.owner)) {
233 retval = -ENODEV;
234 goto bailout_mutex_unlock;
235 }
236 237
238 error_get_interface:
239 error_init_termios:
240 module_put(serial->type->driver.owner);
241 error_module_get:
242 error_no_port:
243 usb_serial_put(serial);
244 mutex_unlock(&serial->disc_mutex);
245 return retval;
246}
247
248static int serial_open(struct tty_struct *tty, struct file *filp)
249{
250 struct usb_serial_port *port = tty->driver_data;
251 struct usb_serial *serial = port->serial;
252 int retval;
253
254 dbg("%s - port %d", __func__, port->number);
255
256 spin_lock_irq(&port->port.lock);
257 if (!tty_hung_up_p(filp))
258 ++port->port.count;
259 spin_unlock_irq(&port->port.lock);
260 tty_port_tty_set(&port->port, tty);
261
262 /* Do the device-specific open only if the hardware isn't
263 * already initialized.
264 */
265 if (!test_bit(ASYNCB_INITIALIZED, &port->port.flags)) {
266 if (mutex_lock_interruptible(&port->mutex))
267 return -ERESTARTSYS;
237 mutex_lock(&serial->disc_mutex); 268 mutex_lock(&serial->disc_mutex);
238 if (serial->disconnected) 269 if (serial->disconnected)
239 retval = -ENODEV; 270 retval = -ENODEV;
240 else 271 else
241 retval = usb_autopm_get_interface(serial->interface); 272 retval = port->serial->type->open(tty, port);
242 if (retval)
243 goto bailout_module_put;
244
245 /* only call the device specific open if this
246 * is the first time the port is opened */
247 retval = serial->type->open(tty, port, filp);
248 if (retval)
249 goto bailout_interface_put;
250 mutex_unlock(&serial->disc_mutex); 273 mutex_unlock(&serial->disc_mutex);
274 mutex_unlock(&port->mutex);
275 if (retval)
276 return retval;
251 set_bit(ASYNCB_INITIALIZED, &port->port.flags); 277 set_bit(ASYNCB_INITIALIZED, &port->port.flags);
252 } 278 }
253 mutex_unlock(&port->mutex); 279
254 /* Now do the correct tty layer semantics */ 280 /* Now do the correct tty layer semantics */
255 retval = tty_port_block_til_ready(&port->port, tty, filp); 281 retval = tty_port_block_til_ready(&port->port, tty, filp);
256 if (retval == 0) {
257 if (!first)
258 usb_serial_put(serial);
259 return 0;
260 }
261 mutex_lock(&port->mutex);
262 if (first == 0)
263 goto bailout_mutex_unlock;
264 /* Undo the initial port actions */
265 mutex_lock(&serial->disc_mutex);
266bailout_interface_put:
267 usb_autopm_put_interface(serial->interface);
268bailout_module_put:
269 mutex_unlock(&serial->disc_mutex);
270 module_put(serial->type->driver.owner);
271bailout_mutex_unlock:
272 port->port.count = 0;
273 tty->driver_data = NULL;
274 tty_port_tty_set(&port->port, NULL);
275 mutex_unlock(&port->mutex);
276bailout_port_put:
277 put_device(&port->dev);
278bailout_serial_put:
279 usb_serial_put(serial);
280 return retval; 282 return retval;
281} 283}
282 284
283/** 285/**
284 * serial_do_down - shut down hardware 286 * serial_down - shut down hardware
285 * @port: port to shut down 287 * @port: port to shut down
286 *
287 * Shut down a USB port unless it is the console. We never shut down the
288 * console hardware as it will always be in use.
289 * 288 *
290 * Don't free any resources at this point 289 * Shut down a USB serial port unless it is the console. We never
290 * shut down the console hardware as it will always be in use.
291 */ 291 */
292static void serial_do_down(struct usb_serial_port *port) 292static void serial_down(struct usb_serial_port *port)
293{ 293{
294 struct usb_serial_driver *drv = port->serial->type; 294 struct usb_serial_driver *drv = port->serial->type;
295 struct usb_serial *serial; 295 struct usb_serial *serial;
296 struct module *owner; 296 struct module *owner;
297 297
298 /* The console is magical, do not hang up the console hardware 298 /*
299 or there will be tears */ 299 * The console is magical. Do not hang up the console hardware
300 * or there will be tears.
301 */
300 if (port->console) 302 if (port->console)
301 return; 303 return;
302 304
305 /* Don't call the close method if the hardware hasn't been
306 * initialized.
307 */
308 if (!test_and_clear_bit(ASYNCB_INITIALIZED, &port->port.flags))
309 return;
310
303 mutex_lock(&port->mutex); 311 mutex_lock(&port->mutex);
304 serial = port->serial; 312 serial = port->serial;
305 owner = serial->type->driver.owner; 313 owner = serial->type->driver.owner;
@@ -310,79 +318,69 @@ static void serial_do_down(struct usb_serial_port *port)
310 mutex_unlock(&port->mutex); 318 mutex_unlock(&port->mutex);
311} 319}
312 320
313/** 321static void serial_hangup(struct tty_struct *tty)
314 * serial_do_free - free resources post close/hangup
315 * @port: port to free up
316 *
317 * Do the resource freeing and refcount dropping for the port. We must
318 * be careful about ordering and we must avoid freeing up the console.
319 */
320
321static void serial_do_free(struct usb_serial_port *port)
322{ 322{
323 struct usb_serial *serial; 323 struct usb_serial_port *port = tty->driver_data;
324 struct module *owner;
325 324
326 /* The console is magical, do not hang up the console hardware 325 dbg("%s - port %d", __func__, port->number);
327 or there will be tears */
328 if (port->console)
329 return;
330 326
331 serial = port->serial; 327 serial_down(port);
332 owner = serial->type->driver.owner; 328 tty_port_hangup(&port->port);
333 put_device(&port->dev);
334 /* Mustn't dereference port any more */
335 mutex_lock(&serial->disc_mutex);
336 if (!serial->disconnected)
337 usb_autopm_put_interface(serial->interface);
338 mutex_unlock(&serial->disc_mutex);
339 usb_serial_put(serial);
340 /* Mustn't dereference serial any more */
341 module_put(owner);
342} 329}
343 330
344static void serial_close(struct tty_struct *tty, struct file *filp) 331static void serial_close(struct tty_struct *tty, struct file *filp)
345{ 332{
346 struct usb_serial_port *port = tty->driver_data; 333 struct usb_serial_port *port = tty->driver_data;
347 334
348 if (!port)
349 return;
350
351 dbg("%s - port %d", __func__, port->number); 335 dbg("%s - port %d", __func__, port->number);
352 336
353 /* FIXME: 337 if (tty_hung_up_p(filp))
354 This leaves a very narrow race. Really we should do the
355 serial_do_free() on tty->shutdown(), but tty->shutdown can
356 be called from IRQ context and serial_do_free can sleep.
357
358 The right fix is probably to make the tty free (which is rare)
359 and thus tty->shutdown() occur via a work queue and simplify all
360 the drivers that use it.
361 */
362 if (tty_hung_up_p(filp)) {
363 /* serial_hangup already called serial_down at this point.
364 Another user may have already reopened the port but
365 serial_do_free is refcounted */
366 serial_do_free(port);
367 return; 338 return;
368 }
369
370 if (tty_port_close_start(&port->port, tty, filp) == 0) 339 if (tty_port_close_start(&port->port, tty, filp) == 0)
371 return; 340 return;
372 341 serial_down(port);
373 serial_do_down(port);
374 tty_port_close_end(&port->port, tty); 342 tty_port_close_end(&port->port, tty);
375 tty_port_tty_set(&port->port, NULL); 343 tty_port_tty_set(&port->port, NULL);
376 serial_do_free(port);
377} 344}
378 345
379static void serial_hangup(struct tty_struct *tty) 346/**
347 * serial_release - free resources post close/hangup
348 * @port: port to free up
349 *
350 * Do the resource freeing and refcount dropping for the port.
351 * Avoid freeing the console.
352 *
353 * Called when the last tty kref is dropped.
354 */
355static void serial_release(struct tty_struct *tty)
380{ 356{
381 struct usb_serial_port *port = tty->driver_data; 357 struct usb_serial_port *port = tty->driver_data;
382 serial_do_down(port); 358 struct usb_serial *serial;
383 tty_port_hangup(&port->port); 359 struct module *owner;
384 /* We must not free port yet - the USB serial layer depends on it's 360
385 continued existence */ 361 /* The console is magical. Do not hang up the console hardware
362 * or there will be tears.
363 */
364 if (port->console)
365 return;
366
367 dbg("%s - port %d", __func__, port->number);
368
369 /* Standard shutdown processing */
370 tty_shutdown(tty);
371
372 tty->driver_data = NULL;
373
374 serial = port->serial;
375 owner = serial->type->driver.owner;
376
377 mutex_lock(&serial->disc_mutex);
378 if (!serial->disconnected)
379 usb_autopm_put_interface(serial->interface);
380 mutex_unlock(&serial->disc_mutex);
381
382 usb_serial_put(serial);
383 module_put(owner);
386} 384}
387 385
388static int serial_write(struct tty_struct *tty, const unsigned char *buf, 386static int serial_write(struct tty_struct *tty, const unsigned char *buf,
@@ -527,6 +525,7 @@ static int serial_proc_show(struct seq_file *m, void *v)
527 525
528 seq_putc(m, '\n'); 526 seq_putc(m, '\n');
529 usb_serial_put(serial); 527 usb_serial_put(serial);
528 mutex_unlock(&serial->disc_mutex);
530 } 529 }
531 return 0; 530 return 0;
532} 531}
@@ -596,14 +595,6 @@ static void usb_serial_port_work(struct work_struct *work)
596 tty_kref_put(tty); 595 tty_kref_put(tty);
597} 596}
598 597
599static void port_release(struct device *dev)
600{
601 struct usb_serial_port *port = to_usb_serial_port(dev);
602
603 dbg ("%s - %s", __func__, dev_name(dev));
604 port_free(port);
605}
606
607static void kill_traffic(struct usb_serial_port *port) 598static void kill_traffic(struct usb_serial_port *port)
608{ 599{
609 usb_kill_urb(port->read_urb); 600 usb_kill_urb(port->read_urb);
@@ -623,8 +614,12 @@ static void kill_traffic(struct usb_serial_port *port)
623 usb_kill_urb(port->interrupt_out_urb); 614 usb_kill_urb(port->interrupt_out_urb);
624} 615}
625 616
626static void port_free(struct usb_serial_port *port) 617static void port_release(struct device *dev)
627{ 618{
619 struct usb_serial_port *port = to_usb_serial_port(dev);
620
621 dbg ("%s - %s", __func__, dev_name(dev));
622
628 /* 623 /*
629 * Stop all the traffic before cancelling the work, so that 624 * Stop all the traffic before cancelling the work, so that
630 * nobody will restart it by calling usb_serial_port_softint. 625 * nobody will restart it by calling usb_serial_port_softint.
@@ -935,6 +930,11 @@ int usb_serial_probe(struct usb_interface *interface,
935 mutex_init(&port->mutex); 930 mutex_init(&port->mutex);
936 INIT_WORK(&port->work, usb_serial_port_work); 931 INIT_WORK(&port->work, usb_serial_port_work);
937 serial->port[i] = port; 932 serial->port[i] = port;
933 port->dev.parent = &interface->dev;
934 port->dev.driver = NULL;
935 port->dev.bus = &usb_serial_bus_type;
936 port->dev.release = &port_release;
937 device_initialize(&port->dev);
938 } 938 }
939 939
940 /* set up the endpoint information */ 940 /* set up the endpoint information */
@@ -1077,15 +1077,10 @@ int usb_serial_probe(struct usb_interface *interface,
1077 /* register all of the individual ports with the driver core */ 1077 /* register all of the individual ports with the driver core */
1078 for (i = 0; i < num_ports; ++i) { 1078 for (i = 0; i < num_ports; ++i) {
1079 port = serial->port[i]; 1079 port = serial->port[i];
1080 port->dev.parent = &interface->dev;
1081 port->dev.driver = NULL;
1082 port->dev.bus = &usb_serial_bus_type;
1083 port->dev.release = &port_release;
1084
1085 dev_set_name(&port->dev, "ttyUSB%d", port->number); 1080 dev_set_name(&port->dev, "ttyUSB%d", port->number);
1086 dbg ("%s - registering %s", __func__, dev_name(&port->dev)); 1081 dbg ("%s - registering %s", __func__, dev_name(&port->dev));
1087 port->dev_state = PORT_REGISTERING; 1082 port->dev_state = PORT_REGISTERING;
1088 retval = device_register(&port->dev); 1083 retval = device_add(&port->dev);
1089 if (retval) { 1084 if (retval) {
1090 dev_err(&port->dev, "Error registering port device, " 1085 dev_err(&port->dev, "Error registering port device, "
1091 "continuing\n"); 1086 "continuing\n");
@@ -1103,39 +1098,7 @@ exit:
1103 return 0; 1098 return 0;
1104 1099
1105probe_error: 1100probe_error:
1106 for (i = 0; i < num_bulk_in; ++i) { 1101 usb_serial_put(serial);
1107 port = serial->port[i];
1108 if (!port)
1109 continue;
1110 usb_free_urb(port->read_urb);
1111 kfree(port->bulk_in_buffer);
1112 }
1113 for (i = 0; i < num_bulk_out; ++i) {
1114 port = serial->port[i];
1115 if (!port)
1116 continue;
1117 usb_free_urb(port->write_urb);
1118 kfree(port->bulk_out_buffer);
1119 }
1120 for (i = 0; i < num_interrupt_in; ++i) {
1121 port = serial->port[i];
1122 if (!port)
1123 continue;
1124 usb_free_urb(port->interrupt_in_urb);
1125 kfree(port->interrupt_in_buffer);
1126 }
1127 for (i = 0; i < num_interrupt_out; ++i) {
1128 port = serial->port[i];
1129 if (!port)
1130 continue;
1131 usb_free_urb(port->interrupt_out_urb);
1132 kfree(port->interrupt_out_buffer);
1133 }
1134
1135 /* free up any memory that we allocated */
1136 for (i = 0; i < serial->num_port_pointers; ++i)
1137 kfree(serial->port[i]);
1138 kfree(serial);
1139 return -EIO; 1102 return -EIO;
1140} 1103}
1141EXPORT_SYMBOL_GPL(usb_serial_probe); 1104EXPORT_SYMBOL_GPL(usb_serial_probe);
@@ -1161,10 +1124,7 @@ void usb_serial_disconnect(struct usb_interface *interface)
1161 if (port) { 1124 if (port) {
1162 struct tty_struct *tty = tty_port_tty_get(&port->port); 1125 struct tty_struct *tty = tty_port_tty_get(&port->port);
1163 if (tty) { 1126 if (tty) {
1164 /* The hangup will occur asynchronously but 1127 tty_vhangup(tty);
1165 the object refcounts will sort out all the
1166 cleanup */
1167 tty_hangup(tty);
1168 tty_kref_put(tty); 1128 tty_kref_put(tty);
1169 } 1129 }
1170 kill_traffic(port); 1130 kill_traffic(port);
@@ -1189,8 +1149,7 @@ void usb_serial_disconnect(struct usb_interface *interface)
1189 } 1149 }
1190 serial->type->disconnect(serial); 1150 serial->type->disconnect(serial);
1191 1151
1192 /* let the last holder of this object 1152 /* let the last holder of this object cause it to be cleaned up */
1193 * cause it to be cleaned up */
1194 usb_serial_put(serial); 1153 usb_serial_put(serial);
1195 dev_info(dev, "device disconnected\n"); 1154 dev_info(dev, "device disconnected\n");
1196} 1155}
@@ -1246,6 +1205,8 @@ static const struct tty_operations serial_ops = {
1246 .chars_in_buffer = serial_chars_in_buffer, 1205 .chars_in_buffer = serial_chars_in_buffer,
1247 .tiocmget = serial_tiocmget, 1206 .tiocmget = serial_tiocmget,
1248 .tiocmset = serial_tiocmset, 1207 .tiocmset = serial_tiocmset,
1208 .shutdown = serial_release,
1209 .install = serial_install,
1249 .proc_fops = &serial_proc_fops, 1210 .proc_fops = &serial_proc_fops,
1250}; 1211};
1251 1212