public inbox for speakup@linux-speakup.org
 help / color / mirror / Atom feed
From: Okash Khawaja <okash.khawaja@gmail.com>
To: samuel.thibault@ens-lyon.org
Cc: speakup@linux-speakup.org
Subject: [patch 1/6] tty_port: allow a port to be opened with a tty that has no file handle
Date: Sat, 25 Feb 2017 19:21:32 +0000	[thread overview]
Message-ID: <20170225192132.GA4492@sanghar> (raw)
In-Reply-To: <20170225191801.GA4482@sanghar>

Allow access to TTY device from kernel. This is based on Alan Cox's patch
(http://www.mail-archive.com/linux-kernel at vger.kernel.org/msg1215095.html),
with description quoted below.

"tty_port: allow a port to be opened with a tty that has no file handle

Let us create tty objects entirely in kernel space.

With this a kernel created non file backed tty object could be used to handle
data, and set terminal modes. Not all ldiscs can cope with this as N_TTY in
particular has to work back to the fs/tty layer.

The tty_port code is however otherwise clean of file handles as far as I can
tell as is the low level tty port write path used by the ldisc, the
configuration low level interfaces and most of the ldiscs.

Currently you don't have any exposure to see tty hangups because those are
built around the file layer. However a) it's a fixed port so you probably
don't care about that b) if you do we can add a callback and c) you almost
certainly don't want the userspace tear down/rebuild behaviour anyway.

This should however be sufficient if we wanted for example to enumerate all
the bluetooth bound fixed ports via ACPI and make them directly available.

It doesn't deal with the case of a user opening a port that's also kernel
opened and that would need some locking out (so it returned EBUSY if bound
to a kernel device of some kind). That needs resolving along with how you
"up" or "down" your new bluetooth device, or enumerate it while providing
the existing tty API to avoid regressions (and to debug)."

Signed-off-by: Okash Khawaja <okash.khawaja@gmail.com>

Index: linux-stable/drivers/tty/tty_io.c
===================================================================
--- linux-stable.orig/drivers/tty/tty_io.c
+++ linux-stable/drivers/tty/tty_io.c
@@ -855,7 +855,7 @@ static void tty_vhangup_session(struct t
 
 int tty_hung_up_p(struct file *filp)
 {
-	return (filp->f_op == &hung_up_tty_fops);
+	return (filp && filp->f_op == &hung_up_tty_fops);
 }
 
 EXPORT_SYMBOL(tty_hung_up_p);
@@ -1368,7 +1368,10 @@ static struct tty_struct *tty_driver_loo
 	struct tty_struct *tty;
 
 	if (driver->ops->lookup)
-		tty = driver->ops->lookup(driver, file, idx);
+		if (!file)
+			tty = ERR_PTR(-EIO);
+		else
+			tty = driver->ops->lookup(driver, file, idx);
 	else
 		tty = driver->ttys[idx];
 
@@ -1986,7 +1989,7 @@ static struct tty_driver *tty_lookup_dri
 		struct tty_driver *console_driver = console_device(index);
 		if (console_driver) {
 			driver = tty_driver_kref_get(console_driver);
-			if (driver) {
+			if (driver && filp) {
 				/* Don't let /dev/console block */
 				filp->f_flags |= O_NONBLOCK;
 				break;
@@ -2019,7 +2022,7 @@ static struct tty_driver *tty_lookup_dri
  *	  - concurrent tty driver removal w/ lookup
  *	  - concurrent tty removal from driver table
  */
-static struct tty_struct *tty_open_by_driver(dev_t device, struct inode *inode,
+struct tty_struct *tty_open_by_driver(dev_t device, struct inode *inode,
 					     struct file *filp)
 {
 	struct tty_struct *tty;
@@ -2064,6 +2067,7 @@ out:
 	tty_driver_kref_put(driver);
 	return tty;
 }
+EXPORT_SYMBOL(tty_open_by_driver);
 
 /**
  *	tty_open		-	open a tty device
Index: linux-stable/drivers/tty/tty_port.c
===================================================================
--- linux-stable.orig/drivers/tty/tty_port.c
+++ linux-stable/drivers/tty/tty_port.c
@@ -335,7 +335,7 @@ EXPORT_SYMBOL(tty_port_lower_dtr_rts);
  *	tty_port_block_til_ready	-	Waiting logic for tty open
  *	@port: the tty port being opened
  *	@tty: the tty device being bound
- *	@filp: the file pointer of the opener
+ *	@filp: the file pointer of the opener or NULL
  *
  *	Implement the core POSIX/SuS tty behaviour when opening a tty device.
  *	Handles:
@@ -369,7 +369,7 @@ int tty_port_block_til_ready(struct tty_
 		tty_port_set_active(port, 1);
 		return 0;
 	}
-	if (filp->f_flags & O_NONBLOCK) {
+	if (filp ==  NULL || filp->f_flags & O_NONBLOCK) {
 		/* Indicate we are open */
 		if (C_BAUD(tty))
 			tty_port_raise_dtr_rts(port);
Index: linux-stable/include/linux/tty.h
===================================================================
--- linux-stable.orig/include/linux/tty.h
+++ linux-stable/include/linux/tty.h
@@ -394,6 +394,8 @@ extern struct tty_struct *get_current_tt
 /* tty_io.c */
 extern int __init tty_init(void);
 extern const char *tty_name(const struct tty_struct *tty);
+extern struct tty_struct *tty_open_by_driver(dev_t device, struct inode *inode,
+					     struct file *filp);
 #else
 static inline void console_init(void)
 { }

  reply	other threads:[~ UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
 [patch 0/6] staging: speakup: Introduce TTY-based comms Okash Khawaja
 ` Okash Khawaja [this message]
     [not found]   ` <20170225192358.GA4499@sanghar>
     ` [patch 3/6] staging: speakup: refactor spk_stop_serial_interrupt Okash Khawaja
       ` [patch 4/6] staging: speakup: Add spk_ttyio Okash Khawaja
         ` [patch 5/6] staging: speakup: Make speakup_dummy use ttyio Okash Khawaja
           ` [patch 6/6] staging: speakup: Migrate acntsa, bns, dectlk and txprt to ttyio Okash Khawaja
             ` Samuel Thibault
             ` Samuel Thibault
               ` Samuel Thibault
                 ` Samuel Thibault
                   ` Okash Khawaja
                     ` Samuel Thibault
                       ` Okash Khawaja
                 ` Okash Khawaja
                   ` Samuel Thibault
                     ` Okash Khawaja
                       ` Samuel Thibault
             ` Samuel Thibault
             ` Samuel Thibault
               ` Okash Khawaja
                 ` Samuel Thibault
                   ` Okash Khawaja
                     ` Samuel Thibault
             ` Samuel Thibault
             ` Samuel Thibault
         ` [patch 4/6] staging: speakup: Add spk_ttyio Samuel Thibault
         ` Samuel Thibault
       ` [patch 3/6] staging: speakup: refactor spk_stop_serial_interrupt Samuel Thibault
         ` Okash Khawaja
           ` Samuel Thibault
     ` [patch 2/6] staging: speakup: Add serial_out method Samuel Thibault
     ` Samuel Thibault
       ` Okash Khawaja
         ` Samuel Thibault
   ` [patch 1/6] tty_port: allow a port to be opened with a tty that has no file handle Samuel Thibault
     ` Samuel Thibault
       ` John Covici
         ` Samuel Thibault
       ` Okash Khawaja
         ` Samuel Thibault
           ` Okash Khawaja
             ` Samuel Thibault
               ` Okash Khawaja
                 ` Samuel Thibault
                   ` Okash Khawaja
 ` [patch 0/6] staging: speakup: Introduce TTY-based comms Trevor Astrope
   ` Okash Khawaja
 ` Samuel Thibault
   ` Samuel Thibault
 ` Samuel Thibault
 ` Read this first :) " Samuel Thibault
   ` Okash Khawaja
     ` Samuel Thibault

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=20170225192132.GA4492@sanghar \
    --to=okash.khawaja@gmail.com \
    --cc=samuel.thibault@ens-lyon.org \
    --cc=speakup@linux-speakup.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).