mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@suse.de>
To: linux-kernel@vger.kernel.org
Cc: Alan Cox <alan@linux.intel.com>, Greg Kroah-Hartman <gregkh@suse.de>
Subject: [PATCH 30/58] tty: sdio_uart: Switch to the open/close helpers
Date: Fri, 11 Dec 2009 15:28:14 -0800	[thread overview]
Message-ID: <1260574122-10676-30-git-send-email-gregkh@suse.de> (raw)
In-Reply-To: <20091211232805.GA10652@kroah.com>

From: Alan Cox <alan@linux.intel.com>

Gets us proper tty semantics, removes some code and fixes up a few corner
case races (hangup during open etc)

Signed-off-by: Alan Cox <alan@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
---
 drivers/mmc/card/sdio_uart.c |  201 +++++++++++++++++++++++++-----------------
 1 files changed, 119 insertions(+), 82 deletions(-)

diff --git a/drivers/mmc/card/sdio_uart.c b/drivers/mmc/card/sdio_uart.c
index 31f7023..95027b0 100644
--- a/drivers/mmc/card/sdio_uart.c
+++ b/drivers/mmc/card/sdio_uart.c
@@ -77,7 +77,6 @@ struct sdio_uart_port {
 	struct kref		kref;
 	struct tty_struct	*tty;
 	unsigned int		index;
-	unsigned int		opened;
 	struct sdio_func	*func;
 	struct mutex		func_lock;
 	struct task_struct	*in_sdio_uart_irq;
@@ -150,6 +149,7 @@ static void sdio_uart_port_put(struct sdio_uart_port *port)
 static void sdio_uart_port_remove(struct sdio_uart_port *port)
 {
 	struct sdio_func *func;
+	struct tty_struct *tty;
 
 	BUG_ON(sdio_uart_table[port->index] != port);
 
@@ -170,11 +170,10 @@ static void sdio_uart_port_remove(struct sdio_uart_port *port)
 	sdio_claim_host(func);
 	port->func = NULL;
 	mutex_unlock(&port->func_lock);
-	if (port->opened) {
-		struct tty_struct *tty = tty_port_tty_get(&port->port);
-		/* tty_hangup is async so is this safe as is ?? */
-		if (tty)
-			tty_hangup(tty);
+	tty = tty_port_tty_get(&port->port);
+	/* tty_hangup is async so is this safe as is ?? */
+	if (tty) {
+		tty_hangup(tty);
 		tty_kref_put(tty);
 	}
 	mutex_unlock(&port->port.mutex);
@@ -560,13 +559,46 @@ static void sdio_uart_irq(struct sdio_func *func)
 	port->in_sdio_uart_irq = NULL;
 }
 
-static int sdio_uart_startup(struct sdio_uart_port *port)
+/**
+ *	uart_dtr_rts		-	 port helper to set uart signals
+ *	@tport: tty port to be updated
+ *	@onoff: set to turn on DTR/RTS
+ *
+ *	Called by the tty port helpers when the modem signals need to be
+ *	adjusted during an open, close and hangup.
+ */
+
+static void uart_dtr_rts(struct tty_port *tport, int onoff)
 {
-	unsigned long page;
-	int ret = -ENOMEM;
-	struct tty_struct *tty = tty_port_tty_get(&port->port);
+	struct sdio_uart_port *port =
+			container_of(tport, struct sdio_uart_port, port);
+	if (onoff == 0)
+		sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
+	else
+		sdio_uart_set_mctrl(port, TIOCM_DTR | TIOCM_RTS);
+}
 
-	/* FIXME: What if it is NULL ?? */
+/**
+ *	sdio_uart_activate	-	start up hardware
+ *	@tport: tty port to activate
+ *	@tty: tty bound to this port
+ *
+ *	Activate a tty port. The port locking guarantees us this will be
+ *	run exactly once per set of opens, and if successful will see the
+ *	shutdown method run exactly once to match. Start up and shutdown are
+ *	protected from each other by the internal locking and will not run
+ *	at the same time even during a hangup event.
+ *
+ *	If we successfully start up the port we take an extra kref as we
+ *	will keep it around until shutdown when the kref is dropped.
+ */
+
+static int sdio_uart_activate(struct tty_port *tport, struct tty_struct *tty)
+{
+	struct sdio_uart_port *port =
+			container_of(tport, struct sdio_uart_port, port);
+	unsigned long page;
+	int ret;
 
 	/*
 	 * Set the TTY IO error marker - we will only clear this
@@ -577,7 +609,7 @@ static int sdio_uart_startup(struct sdio_uart_port *port)
 	/* Initialise and allocate the transmit buffer. */
 	page = __get_free_page(GFP_KERNEL);
 	if (!page)
-		goto err0;
+		return -ENOMEM;
 	port->xmit.buf = (unsigned char *)page;
 	circ_clear(&port->xmit);
 
@@ -631,7 +663,6 @@ static int sdio_uart_startup(struct sdio_uart_port *port)
 	sdio_uart_irq(port->func);
 
 	sdio_uart_release_func(port);
-	tty_kref_put(tty);
 	return 0;
 
 err3:
@@ -640,15 +671,25 @@ err2:
 	sdio_uart_release_func(port);
 err1:
 	free_page((unsigned long)port->xmit.buf);
-err0:
-	tty_kref_put(tty);
 	return ret;
 }
 
-static void sdio_uart_shutdown(struct sdio_uart_port *port)
+
+/**
+ *	sdio_uart_shutdown	-	stop hardware
+ *	@tport: tty port to shut down
+ *
+ *	Deactivate a tty port. The port locking guarantees us this will be
+ *	run only if a successful matching activate already ran. The two are
+ *	protected from each other by the internal locking and will not run
+ *	at the same time even during a hangup event.
+ */
+
+static void sdio_uart_shutdown(struct tty_port *tport)
 {
+	struct sdio_uart_port *port =
+			container_of(tport, struct sdio_uart_port, port);
 	int ret;
-	struct tty_struct *tty;
 
 	ret = sdio_uart_claim_func(port);
 	if (ret)
@@ -656,14 +697,6 @@ static void sdio_uart_shutdown(struct sdio_uart_port *port)
 
 	sdio_uart_stop_rx(port);
 
-	/* TODO: wait here for TX FIFO to drain */
-
-	tty = tty_port_tty_get(&port->port);
-	/* Turn off DTR and RTS early. */
-	if (tty && (tty->termios->c_cflag & HUPCL))
-		sdio_uart_clear_mctrl(port, TIOCM_DTR | TIOCM_RTS);
-	tty_kref_put(tty);
-
 	/* Disable interrupts from this port */
 	sdio_release_irq(port->func);
 	port->ier = 0;
@@ -688,74 +721,68 @@ skip:
 	free_page((unsigned long)port->xmit.buf);
 }
 
-static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
-{
-	struct sdio_uart_port *port;
-	int ret;
-
-	port = sdio_uart_port_get(tty->index);
-	if (!port)
-		return -ENODEV;
-
-	mutex_lock(&port->port.mutex);
+/**
+ *	sdio_uart_install	-	install method
+ *	@driver: the driver in use (sdio_uart in our case)
+ *	@tty: the tty being bound
+ *
+ *	Look up and bind the tty and the driver together. Initialize
+ *	any needed private data (in our case the termios)
+ */
 
-	/*
-	 * Make sure not to mess up with a dead port
-	 * which has not been closed yet.
-	 */
-	if (tty->driver_data && tty->driver_data != port) {
-		mutex_unlock(&port->port.mutex);
+static int sdio_uart_install(struct tty_driver *driver, struct tty_struct *tty)
+{
+	int idx = tty->index;
+	struct sdio_uart_port *port = sdio_uart_port_get(idx);
+	int ret = tty_init_termios(tty);
+
+	if (ret == 0) {
+		tty_driver_kref_get(driver);
+		tty->count++;
+		/* This is the ref sdio_uart_port get provided */
+		tty->driver_data = port;
+		driver->ttys[idx] = tty;
+	} else
 		sdio_uart_port_put(port);
-		return -EBUSY;
-	}
+	return ret;
 
-	if (!port->opened) {
-		tty->driver_data = port;
-		tty_port_tty_set(&port->port, tty);
-		ret = sdio_uart_startup(port);
-		if (ret) {
-			tty->driver_data = NULL;
-			tty_port_tty_set(&port->port, NULL);
-			mutex_unlock(&port->port.mutex);
-			sdio_uart_port_put(port);
-			return ret;
-		}
-	}
-	port->opened++;
-	mutex_unlock(&port->port.mutex);
-	return 0;
 }
 
-static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
+/**
+ *	sdio_uart_cleanup	-	called on the last tty kref drop
+ *	@tty: the tty being destroyed
+ *
+ *	Called asynchronously when the last reference to the tty is dropped.
+ *	We cannot destroy the tty->driver_data port kref until this point
+ */
+
+static void sdio_uart_cleanup(struct tty_struct *tty)
 {
 	struct sdio_uart_port *port = tty->driver_data;
+	tty->driver_data = NULL;	/* Bug trap */
+	sdio_uart_port_put(port);
+}
 
-	if (!port)
-		return;
+/*
+ *	Open/close/hangup is now entirely boilerplate
+ */
 
-	mutex_lock(&port->port.mutex);
-	BUG_ON(!port->opened);
+static int sdio_uart_open(struct tty_struct *tty, struct file *filp)
+{
+	struct sdio_uart_port *port = tty->driver_data;
+	return tty_port_open(&port->port, tty, filp);
+}
 
-	/*
-	 * This is messy.  The tty layer calls us even when open()
-	 * returned an error.  Ignore this close request if tty->count
-	 * is larger than port->count.
-	 */
-	if (tty->count > port->opened) {
-		mutex_unlock(&port->port.mutex);
-		return;
-	}
+static void sdio_uart_close(struct tty_struct *tty, struct file * filp)
+{
+	struct sdio_uart_port *port = tty->driver_data;
+	tty_port_close(&port->port, tty, filp);
+}
 
-	if (--port->opened == 0) {
-		tty->closing = 1;
-		sdio_uart_shutdown(port);
-		tty_ldisc_flush(tty);
-		tty_port_tty_set(&port->port, NULL);
-		tty->driver_data = NULL;
-		tty->closing = 0;
-	}
-	mutex_unlock(&port->port.mutex);
-	sdio_uart_port_put(port);
+static void sdio_uart_hangup(struct tty_struct *tty)
+{
+	struct sdio_uart_port *port = tty->driver_data;
+	tty_port_hangup(&port->port);
 }
 
 static int sdio_uart_write(struct tty_struct * tty, const unsigned char *buf,
@@ -1021,6 +1048,12 @@ static const struct file_operations sdio_uart_proc_fops = {
 	.release	= single_release,
 };
 
+static const struct tty_port_operations sdio_uart_port_ops = {
+	.dtr_rts = uart_dtr_rts,
+	.shutdown = sdio_uart_shutdown,
+	.activate = sdio_uart_activate,
+};
+
 static const struct tty_operations sdio_uart_ops = {
 	.open			= sdio_uart_open,
 	.close			= sdio_uart_close,
@@ -1031,9 +1064,12 @@ static const struct tty_operations sdio_uart_ops = {
 	.throttle		= sdio_uart_throttle,
 	.unthrottle		= sdio_uart_unthrottle,
 	.set_termios		= sdio_uart_set_termios,
+	.hangup			= sdio_uart_hangup,
 	.break_ctl		= sdio_uart_break_ctl,
 	.tiocmget		= sdio_uart_tiocmget,
 	.tiocmset		= sdio_uart_tiocmset,
+	.install		= sdio_uart_install,
+	.cleanup		= sdio_uart_cleanup,
 	.proc_fops		= &sdio_uart_proc_fops,
 };
 
@@ -1096,6 +1132,7 @@ static int sdio_uart_probe(struct sdio_func *func,
 	port->func = func;
 	sdio_set_drvdata(func, port);
 	tty_port_init(&port->port);
+	port->port.ops = &sdio_uart_port_ops;
 
 	ret = sdio_uart_add_port(port);
 	if (ret) {
-- 
1.6.5.5


  parent reply	other threads:[~2009-12-11 23:37 UTC|newest]

Thread overview: 99+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-11 23:28 [GIT PATCH] TTY patches for 2.6.33-git Greg KH
2009-12-11 23:27 ` [PATCH 01/58] jsm: IRQ handlers doesn't need to have IRQ_DISABLED enabled Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 02/58] jsm: Rewriting a bad log message Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 03/58] jsm: remove the ch_custom_speed field Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 04/58] jsm: removing ch_old_baud field Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 05/58] jsm: Remove ch_cpstime field Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 06/58] jsm: Removing unused jsm_channel->ch_wopen field Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 07/58] jsm: removing the field jsm_board->intr_count Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 08/58] jsm: adding EEH handlers Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 09/58] tty: const: constify remaining tty_operations Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 10/58] tty: esp: remove broken driver Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 11/58] tty: istallion: Kill off the BKL ioctl Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 12/58] tty: stallion: kill " Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 13/58] tty_port: add "tty_port_open" helper Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 14/58] tty_port: coding style cleaning pass Greg Kroah-Hartman
2009-12-11 23:27 ` [PATCH 15/58] usb_serial: Use the shutdown() operation Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 16/58] usb_serial: Kill port mutex Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 17/58] opticon: Fix resume logic Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 18/58] serial: fix NULL pointer dereference Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 19/58] serial: cascade needless conditionals Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 20/58] serial, 8250: calculate irqflags bitmask before loop Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 21/58] Serial: pxa: work around Errata #75 Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 22/58] tty: docs: serial/tty, add to ldisc methods Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 23/58] Serial: Do not read IIR in serial8250_start_tx when UART_BUG_TXEN Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 24/58] devpts_get_tty() should validate inode Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 25/58] tty_port: Move hupcl handling Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 26/58] sdio_uart: use tty_port Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 27/58] sdio_uart: Fix oops caused by the previous changeset Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 28/58] sdio_uart: refcount the tty objects Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 29/58] sdio_uart: Move the open lock Greg Kroah-Hartman
2009-12-11 23:28 ` Greg Kroah-Hartman [this message]
2009-12-11 23:28 ` [PATCH 31/58] tty: sdio_uart: Fix termios handling Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 32/58] tty: sdio_uart: Style fixes Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 33/58] tty: sdio_uart: add modem functionality Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 34/58] tty: sdio_uart: Fix the locking on "func" for new code Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 35/58] tty: tty_port: Change the buffer allocator locking Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 36/58] tty: riscom8: switch to the tty_port_open API Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 37/58] tty: tty_port: Add IO_ERROR bit handling Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 38/58] tty: tty_port: Move the IO_ERROR clear Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 39/58] tty: stallion: Convert to the tty_port_open/close methods Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 40/58] tty: istallion: tty port open/close methods Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 41/58] tty: tty_port: Add a kref object to the tty port Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 42/58] tty: isicom: switch to the new tty_port_open helper Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 43/58] tty: isicom: sort out the board init logic Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 44/58] tty: mxser: use the tty_port_open method Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 45/58] tty: mxser: Use the new locking rules to fix setserial properly Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 46/58] tty: isicom: fix deadlock on shutdown Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 47/58] tty: moxa: Use more tty_port ops Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 48/58] tty: moxa: rework the locking a bit Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 49/58] tty: moxa: Locking clean up Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 50/58] tty: moxa: Kill off the throttle method Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 51/58] tty: moxa: Fix modem op locking Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 52/58] tty: moxa: Kill the use of lock_kernel Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 53/58] tty: moxa: split open lock Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 54/58] tty: push the BKL down into the handlers a bit Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 55/58] tty: Push the lock down further into the ldisc code Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 56/58] tty: Push the bkl down a bit in the hangup code Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 57/58] tty: Move the leader test in disassociate Greg Kroah-Hartman
2009-12-11 23:28 ` [PATCH 58/58] tty: split the lock up a bit further Greg Kroah-Hartman
2009-12-12  8:46 ` [GIT PATCH] TTY patches for 2.6.33-git Ingo Molnar
2009-12-12  9:39   ` Andrew Morton
2009-12-12 10:06     ` Ingo Molnar
2009-12-12 10:15       ` Ingo Molnar
2009-12-12 10:41         ` Andrew Morton
2009-12-12 10:52           ` Ingo Molnar
2009-12-12 10:10     ` Ingo Molnar
2009-12-12 10:36       ` Andrew Morton
2009-12-12 17:16         ` Linus Torvalds
2009-12-12 17:26           ` Linus Torvalds
2009-12-12 21:42             ` Alan Cox
2009-12-12 21:48               ` Thomas Gleixner
2009-12-12 22:25                 ` Linus Torvalds
2009-12-12 22:31                   ` Thomas Gleixner
2009-12-12 22:21               ` Linus Torvalds
2009-12-12 22:33                 ` Thomas Gleixner
2009-12-12 23:17                   ` Linus Torvalds
2009-12-13  6:58                     ` Ingo Molnar
2009-12-13 11:36                       ` Alan Cox
2009-12-13 12:13                         ` Ingo Molnar
2009-12-13 17:46                       ` Linus Torvalds
2009-12-13 18:17                         ` Ingo Molnar
2009-12-13 18:33                           ` Trond Myklebust
2009-12-13 19:07                             ` Linus Torvalds
2009-12-13 19:17                               ` Trond Myklebust
2009-12-13 19:04                           ` Frederic Weisbecker
2009-12-13 19:09                             ` Trond Myklebust
2009-12-13 19:19                               ` Linus Torvalds
2009-12-13 20:04                                 ` Trond Myklebust
2009-12-13 19:20                               ` Frederic Weisbecker
2009-12-13 17:55                       ` Arjan van de Ven
2009-12-13 19:16                         ` Frederic Weisbecker
2009-12-14  5:30                           ` Arjan van de Ven
2009-12-14 10:39                             ` Oliver Neukum
2009-12-14 16:02                               ` Arjan van de Ven
2009-12-16  9:15                             ` Arnd Bergmann
2009-12-12 11:02     ` Alan Cox
2009-12-12 22:34       ` Thomas Gleixner
2009-12-12 10:42   ` Alan Cox
2009-12-12 10:42     ` Ingo Molnar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1260574122-10676-30-git-send-email-gregkh@suse.de \
    --to=gregkh@suse.de \
    --cc=alan@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®