aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/serial/usb-serial.c
diff options
context:
space:
mode:
authorAlan Cox <alan@lxorguk.ukuu.org.uk>2009-06-11 07:26:29 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2009-06-11 11:50:56 -0400
commit335f8514f200e63d689113d29cb7253a5c282967 (patch)
tree11504d090e8e2cd3c1ada3e6765f69f216065d00 /drivers/usb/serial/usb-serial.c
parent1ec739be75a6cb961a46ba0b1982d0edb7f27558 (diff)
tty: Bring the usb tty port structure into more use
This allows us to clean stuff up, but is probably also going to cause some app breakage with buggy apps as we now implement proper POSIX behaviour for USB ports matching all the other ports. This does also mean other apps that break on USB will now work properly. Signed-off-by: Alan Cox <alan@lxorguk.ukuu.org.uk> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/usb/serial/usb-serial.c')
-rw-r--r--drivers/usb/serial/usb-serial.c144
1 files changed, 100 insertions, 44 deletions
diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index f331e2bde88a..1967a7edc10c 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -238,9 +238,11 @@ static int serial_open (struct tty_struct *tty, struct file *filp)
238 goto bailout_interface_put; 238 goto bailout_interface_put;
239 mutex_unlock(&serial->disc_mutex); 239 mutex_unlock(&serial->disc_mutex);
240 } 240 }
241
242 mutex_unlock(&port->mutex); 241 mutex_unlock(&port->mutex);
243 return 0; 242 /* Now do the correct tty layer semantics */
243 retval = tty_port_block_til_ready(&port->port, tty, filp);
244 if (retval == 0)
245 return 0;
244 246
245bailout_interface_put: 247bailout_interface_put:
246 usb_autopm_put_interface(serial->interface); 248 usb_autopm_put_interface(serial->interface);
@@ -259,64 +261,89 @@ bailout_serial_put:
259 return retval; 261 return retval;
260} 262}
261 263
262static void serial_close(struct tty_struct *tty, struct file *filp) 264/**
265 * serial_do_down - shut down hardware
266 * @port: port to shut down
267 *
268 * Shut down a USB port unless it is the console. We never shut down the
269 * console hardware as it will always be in use.
270 *
271 * Don't free any resources at this point
272 */
273static void serial_do_down(struct usb_serial_port *port)
263{ 274{
264 struct usb_serial_port *port = tty->driver_data; 275 struct usb_serial_driver *drv = port->serial->type;
265 struct usb_serial *serial; 276 struct usb_serial *serial;
266 struct module *owner; 277 struct module *owner;
267 int count;
268 278
269 if (!port) 279 /* The console is magical, do not hang up the console hardware
280 or there will be tears */
281 if (port->console)
270 return; 282 return;
271 283
272 dbg("%s - port %d", __func__, port->number);
273
274 mutex_lock(&port->mutex); 284 mutex_lock(&port->mutex);
275 serial = port->serial; 285 serial = port->serial;
276 owner = serial->type->driver.owner; 286 owner = serial->type->driver.owner;
277 287
278 if (port->port.count == 0) { 288 if (drv->close)
279 mutex_unlock(&port->mutex); 289 drv->close(port);
280 return;
281 }
282
283 if (port->port.count == 1)
284 /* only call the device specific close if this
285 * port is being closed by the last owner. Ensure we do
286 * this before we drop the port count. The call is protected
287 * by the port mutex
288 */
289 serial->type->close(tty, port, filp);
290
291 if (port->port.count == (port->console ? 2 : 1)) {
292 struct tty_struct *tty = tty_port_tty_get(&port->port);
293 if (tty) {
294 /* We must do this before we drop the port count to
295 zero. */
296 if (tty->driver_data)
297 tty->driver_data = NULL;
298 tty_port_tty_set(&port->port, NULL);
299 tty_kref_put(tty);
300 }
301 }
302 290
303 --port->port.count;
304 count = port->port.count;
305 mutex_unlock(&port->mutex); 291 mutex_unlock(&port->mutex);
306 put_device(&port->dev); 292}
293
294/**
295 * serial_do_free - free resources post close/hangup
296 * @port: port to free up
297 *
298 * Do the resource freeing and refcount dropping for the port. We must
299 * be careful about ordering and we must avoid freeing up the console.
300 */
307 301
302static void serial_do_free(struct usb_serial_port *port)
303{
304 struct usb_serial *serial;
305 struct module *owner;
306
307 /* The console is magical, do not hang up the console hardware
308 or there will be tears */
309 if (port->console)
310 return;
311
312 serial = port->serial;
313 owner = serial->type->driver.owner;
314 put_device(&port->dev);
308 /* Mustn't dereference port any more */ 315 /* Mustn't dereference port any more */
309 if (count == 0) { 316 mutex_lock(&serial->disc_mutex);
310 mutex_lock(&serial->disc_mutex); 317 if (!serial->disconnected)
311 if (!serial->disconnected) 318 usb_autopm_put_interface(serial->interface);
312 usb_autopm_put_interface(serial->interface); 319 mutex_unlock(&serial->disc_mutex);
313 mutex_unlock(&serial->disc_mutex);
314 }
315 usb_serial_put(serial); 320 usb_serial_put(serial);
316
317 /* Mustn't dereference serial any more */ 321 /* Mustn't dereference serial any more */
318 if (count == 0) 322 module_put(owner);
319 module_put(owner); 323}
324
325static void serial_close(struct tty_struct *tty, struct file *filp)
326{
327 struct usb_serial_port *port = tty->driver_data;
328
329 dbg("%s - port %d", __func__, port->number);
330
331
332 if (tty_port_close_start(&port->port, tty, filp) == 0)
333 return;
334
335 serial_do_down(port);
336 tty_port_close_end(&port->port, tty);
337 tty_port_tty_set(&port->port, NULL);
338 serial_do_free(port);
339}
340
341static void serial_hangup(struct tty_struct *tty)
342{
343 struct usb_serial_port *port = tty->driver_data;
344 serial_do_down(port);
345 tty_port_hangup(&port->port);
346 serial_do_free(port);
320} 347}
321 348
322static int serial_write(struct tty_struct *tty, const unsigned char *buf, 349static int serial_write(struct tty_struct *tty, const unsigned char *buf,
@@ -648,6 +675,29 @@ static struct usb_serial_driver *search_serial_device(
648 return NULL; 675 return NULL;
649} 676}
650 677
678static int serial_carrier_raised(struct tty_port *port)
679{
680 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
681 struct usb_serial_driver *drv = p->serial->type;
682 if (drv->carrier_raised)
683 return drv->carrier_raised(p);
684 /* No carrier control - don't block */
685 return 1;
686}
687
688static void serial_dtr_rts(struct tty_port *port, int on)
689{
690 struct usb_serial_port *p = container_of(port, struct usb_serial_port, port);
691 struct usb_serial_driver *drv = p->serial->type;
692 if (drv->dtr_rts)
693 drv->dtr_rts(p, on);
694}
695
696static const struct tty_port_operations serial_port_ops = {
697 .carrier_raised = serial_carrier_raised,
698 .dtr_rts = serial_dtr_rts,
699};
700
651int usb_serial_probe(struct usb_interface *interface, 701int usb_serial_probe(struct usb_interface *interface,
652 const struct usb_device_id *id) 702 const struct usb_device_id *id)
653{ 703{
@@ -841,6 +891,7 @@ int usb_serial_probe(struct usb_interface *interface,
841 if (!port) 891 if (!port)
842 goto probe_error; 892 goto probe_error;
843 tty_port_init(&port->port); 893 tty_port_init(&port->port);
894 port->port.ops = &serial_port_ops;
844 port->serial = serial; 895 port->serial = serial;
845 spin_lock_init(&port->lock); 896 spin_lock_init(&port->lock);
846 mutex_init(&port->mutex); 897 mutex_init(&port->mutex);
@@ -1071,6 +1122,9 @@ void usb_serial_disconnect(struct usb_interface *interface)
1071 if (port) { 1122 if (port) {
1072 struct tty_struct *tty = tty_port_tty_get(&port->port); 1123 struct tty_struct *tty = tty_port_tty_get(&port->port);
1073 if (tty) { 1124 if (tty) {
1125 /* The hangup will occur asynchronously but
1126 the object refcounts will sort out all the
1127 cleanup */
1074 tty_hangup(tty); 1128 tty_hangup(tty);
1075 tty_kref_put(tty); 1129 tty_kref_put(tty);
1076 } 1130 }
@@ -1135,6 +1189,7 @@ static const struct tty_operations serial_ops = {
1135 .open = serial_open, 1189 .open = serial_open,
1136 .close = serial_close, 1190 .close = serial_close,
1137 .write = serial_write, 1191 .write = serial_write,
1192 .hangup = serial_hangup,
1138 .write_room = serial_write_room, 1193 .write_room = serial_write_room,
1139 .ioctl = serial_ioctl, 1194 .ioctl = serial_ioctl,
1140 .set_termios = serial_set_termios, 1195 .set_termios = serial_set_termios,
@@ -1147,6 +1202,7 @@ static const struct tty_operations serial_ops = {
1147 .proc_fops = &serial_proc_fops, 1202 .proc_fops = &serial_proc_fops,
1148}; 1203};
1149 1204
1205
1150struct tty_driver *usb_serial_tty_driver; 1206struct tty_driver *usb_serial_tty_driver;
1151 1207
1152static int __init usb_serial_init(void) 1208static int __init usb_serial_init(void)