[3/3] tty: n_gsm: add TIOCMIWAIT support

Message ID 20230201080151.2068-3-daniel.starke@siemens.com
State New
Headers
Series [1/3] tty: n_gsm: add keep alive support |

Commit Message

D. Starke Feb. 1, 2023, 8:01 a.m. UTC
  From: Daniel Starke <daniel.starke@siemens.com>

Add support for the TIOCMIWAIT ioctl on the virtual ttys. This enables the
user to wait for virtual modem signals like RING.

Signed-off-by: Daniel Starke <daniel.starke@siemens.com>
---
 drivers/tty/n_gsm.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
  

Comments

Greg KH Feb. 1, 2023, 8:41 a.m. UTC | #1
On Wed, Feb 01, 2023 at 09:01:51AM +0100, D. Starke wrote:
> From: Daniel Starke <daniel.starke@siemens.com>
> 
> Add support for the TIOCMIWAIT ioctl on the virtual ttys. This enables the
> user to wait for virtual modem signals like RING.
> 
> Signed-off-by: Daniel Starke <daniel.starke@siemens.com>
> ---
>  drivers/tty/n_gsm.c | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
> index 118511c1fa37..48fb7dad44cd 100644
> --- a/drivers/tty/n_gsm.c
> +++ b/drivers/tty/n_gsm.c
> @@ -1542,6 +1542,7 @@ static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
>  	if (brk & 0x01)
>  		tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
>  	dlci->modem_rx = mlines;
> +	wake_up_interruptible(&dlci->gsm->event);
>  }
>  
>  /**
> @@ -3848,6 +3849,35 @@ static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk)
>  	return -EPROTONOSUPPORT;
>  }
>  
> +/**
> + * gsm_wait_modem_change	-	wait for modem status line change

No need for tabs.

Or for kernel doc for static functions, but that's not a big deal.

> + * @dlci: channel
> + * @mask: modem status line bits
> + */
> +

No blank line please, didn't checkpatch warn about that?

> +static int gsm_wait_modem_change(struct gsm_dlci *dlci, u32 mask)
> +{
> +	struct gsm_mux *gsm = dlci->gsm;
> +	u32 old = dlci->modem_rx & mask;
> +	int ret;
> +
> +	if (gsm->dead)
> +		return -ENODEV;
> +
> +	do {
> +		ret = wait_event_interruptible(gsm->event, gsm->dead
> +					      || old ^ (dlci->modem_rx & mask));
> +		if (ret)
> +			return ret;
> +		if (dlci->state != DLCI_OPEN)
> +			return -EL2NSYNC;
> +		if (gsm->dead)
> +			return -ENODEV;
> +	} while ((old ^ (dlci->modem_rx & mask)) == 0);

No way to break out of the loop if it goes for forever?

thanks,

greg k-h
  
D. Starke Feb. 1, 2023, 9:30 a.m. UTC | #2
> > Add support for the TIOCMIWAIT ioctl on the virtual ttys. This enables the
> > user to wait for virtual modem signals like RING.
> > 
> > Signed-off-by: Daniel Starke <daniel.starke@siemens.com>
> > ---
> >  drivers/tty/n_gsm.c | 32 ++++++++++++++++++++++++++++++++
> >  1 file changed, 32 insertions(+)
> > 
> > diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
> > index 118511c1fa37..48fb7dad44cd 100644
> > --- a/drivers/tty/n_gsm.c
> > +++ b/drivers/tty/n_gsm.c
> > @@ -1542,6 +1542,7 @@ static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
> >  	if (brk & 0x01)
> >  		tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
> >  	dlci->modem_rx = mlines;
> > +	wake_up_interruptible(&dlci->gsm->event);
> >  }
> >  
> >  /**
> > @@ -3848,6 +3849,35 @@ static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk)
> >  	return -EPROTONOSUPPORT;
> >  }
> >  
> > +/**
> > + * gsm_wait_modem_change	-	wait for modem status line change
> 
> No need for tabs.
> 
> Or for kernel doc for static functions, but that's not a big deal.

All other functions follow the same style here. Nevertheless, I will
replace the tabs with spaces. Originally, I had planned a code clean-up
after committing all the changes from my original RFC.

> > + * @dlci: channel
> > + * @mask: modem status line bits
> > + */
> > +
> 
> No blank line please, didn't checkpatch warn about that?

No, checkpatch did not warn about this and all other functions follow the
same style. I will remove the blank line.

> > +static int gsm_wait_modem_change(struct gsm_dlci *dlci, u32 mask)
> > +{
> > +	struct gsm_mux *gsm = dlci->gsm;
> > +	u32 old = dlci->modem_rx & mask;
> > +	int ret;
> > +
> > +	if (gsm->dead)
> > +		return -ENODEV;
> > +
> > +	do {
> > +		ret = wait_event_interruptible(gsm->event, gsm->dead
> > +					      || old ^ (dlci->modem_rx & mask));
> > +		if (ret)
> > +			return ret;
> > +		if (dlci->state != DLCI_OPEN)
> > +			return -EL2NSYNC;
> > +		if (gsm->dead)
> > +			return -ENODEV;
> > +	} while ((old ^ (dlci->modem_rx & mask)) == 0);
> 
> No way to break out of the loop if it goes for forever?

I assume that this is the expected behavior for TIOCMIWAIT. The functions
returns if:
- the requested modem signal changed
- the wait function got interrupted (e.g. by a signal)
- the underlying DLCI was closed
- the underlying ldisc device was removed

I can add that the function returns immediate if a mask has been passed
which matches no modem signal if this is preferred?

Best regards,
Daniel Starke
  
Greg KH Feb. 1, 2023, 12:51 p.m. UTC | #3
On Wed, Feb 01, 2023 at 09:30:28AM +0000, Starke, Daniel wrote:
> > > Add support for the TIOCMIWAIT ioctl on the virtual ttys. This enables the
> > > user to wait for virtual modem signals like RING.
> > > 
> > > Signed-off-by: Daniel Starke <daniel.starke@siemens.com>
> > > ---
> > >  drivers/tty/n_gsm.c | 32 ++++++++++++++++++++++++++++++++
> > >  1 file changed, 32 insertions(+)
> > > 
> > > diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
> > > index 118511c1fa37..48fb7dad44cd 100644
> > > --- a/drivers/tty/n_gsm.c
> > > +++ b/drivers/tty/n_gsm.c
> > > @@ -1542,6 +1542,7 @@ static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
> > >  	if (brk & 0x01)
> > >  		tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
> > >  	dlci->modem_rx = mlines;
> > > +	wake_up_interruptible(&dlci->gsm->event);
> > >  }
> > >  
> > >  /**
> > > @@ -3848,6 +3849,35 @@ static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk)
> > >  	return -EPROTONOSUPPORT;
> > >  }
> > >  
> > > +/**
> > > + * gsm_wait_modem_change	-	wait for modem status line change
> > 
> > No need for tabs.
> > 
> > Or for kernel doc for static functions, but that's not a big deal.
> 
> All other functions follow the same style here. Nevertheless, I will
> replace the tabs with spaces. Originally, I had planned a code clean-up
> after committing all the changes from my original RFC.

Ah, ok, nevermind then, I didn't see that as it's not in the diffs.

Cleaning up later is fine.

> > > +static int gsm_wait_modem_change(struct gsm_dlci *dlci, u32 mask)
> > > +{
> > > +	struct gsm_mux *gsm = dlci->gsm;
> > > +	u32 old = dlci->modem_rx & mask;
> > > +	int ret;
> > > +
> > > +	if (gsm->dead)
> > > +		return -ENODEV;
> > > +
> > > +	do {
> > > +		ret = wait_event_interruptible(gsm->event, gsm->dead
> > > +					      || old ^ (dlci->modem_rx & mask));
> > > +		if (ret)
> > > +			return ret;
> > > +		if (dlci->state != DLCI_OPEN)
> > > +			return -EL2NSYNC;
> > > +		if (gsm->dead)
> > > +			return -ENODEV;
> > > +	} while ((old ^ (dlci->modem_rx & mask)) == 0);
> > 
> > No way to break out of the loop if it goes for forever?
> 
> I assume that this is the expected behavior for TIOCMIWAIT. The functions
> returns if:
> - the requested modem signal changed
> - the wait function got interrupted (e.g. by a signal)
> - the underlying DLCI was closed
> - the underlying ldisc device was removed

Hm, I guess you are right.  But wow, reading that
wait_event_interruptible() condition is crazy, please document that
really well to explain this properly so you will be able to understand
it in a year when you next have to fix it up :)

> I can add that the function returns immediate if a mask has been passed
> which matches no modem signal if this is preferred?

I don't think that would work, try it on some existing serial ports to
see what they do.

thanks,

greg k-h
  

Patch

diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index 118511c1fa37..48fb7dad44cd 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -1542,6 +1542,7 @@  static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
 	if (brk & 0x01)
 		tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
 	dlci->modem_rx = mlines;
+	wake_up_interruptible(&dlci->gsm->event);
 }
 
 /**
@@ -3848,6 +3849,35 @@  static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk)
 	return -EPROTONOSUPPORT;
 }
 
+/**
+ * gsm_wait_modem_change	-	wait for modem status line change
+ * @dlci: channel
+ * @mask: modem status line bits
+ */
+
+static int gsm_wait_modem_change(struct gsm_dlci *dlci, u32 mask)
+{
+	struct gsm_mux *gsm = dlci->gsm;
+	u32 old = dlci->modem_rx & mask;
+	int ret;
+
+	if (gsm->dead)
+		return -ENODEV;
+
+	do {
+		ret = wait_event_interruptible(gsm->event, gsm->dead
+					      || old ^ (dlci->modem_rx & mask));
+		if (ret)
+			return ret;
+		if (dlci->state != DLCI_OPEN)
+			return -EL2NSYNC;
+		if (gsm->dead)
+			return -ENODEV;
+	} while ((old ^ (dlci->modem_rx & mask)) == 0);
+
+	return 0;
+}
+
 static bool gsm_carrier_raised(struct tty_port *port)
 {
 	struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
@@ -4107,6 +4137,8 @@  static int gsmtty_ioctl(struct tty_struct *tty,
 		gsm_destroy_network(dlci);
 		mutex_unlock(&dlci->mutex);
 		return 0;
+	case TIOCMIWAIT:
+		return gsm_wait_modem_change(dlci, arg);
 	default:
 		return -ENOIOCTLCMD;
 	}