[PATCHv4] tty: n_gsm: add ioctl to map serial device to mux'ed tty

From: Martin HundebÃll
Date: Mon Aug 12 2019 - 17:12:57 EST


Guessing the first tty for a gsm0710 multiplexed serial device is not
currently possible, which makes it racy to use with multiple modems.

Add a way to map the physical serial tty to its related mux devices
using an ioctl.

Signed-off-by: Martin HundebÃll <martin@xxxxxxxxxx>
---

Changes since v3:
* use __u32 instead of uint32_t

Changes since v2:
* rename IOCTL from GSMIOC_GETBASE to GSMIOC_GETFIRST
* return first usable line instead of private control line
* return uint32_t instead of int

Changes since v1:
* use put_user() instead of copy_to_user()
* add missing opening quote

Documentation/driver-api/serial/n_gsm.rst | 11 +++++++++--
drivers/tty/n_gsm.c | 4 ++++
include/uapi/linux/gsmmux.h | 2 ++
3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/Documentation/driver-api/serial/n_gsm.rst b/Documentation/driver-api/serial/n_gsm.rst
index 0ba731ab00b2..286e7ff4d2d9 100644
--- a/Documentation/driver-api/serial/n_gsm.rst
+++ b/Documentation/driver-api/serial/n_gsm.rst
@@ -18,10 +18,13 @@ How to use it
2. switch the serial line to using the n_gsm line discipline by using
TIOCSETD ioctl,
3. configure the mux using GSMIOC_GETCONF / GSMIOC_SETCONF ioctl,
+4. obtain base gsmtty number for the used serial port,

Major parts of the initialization program :
(a good starting point is util-linux-ng/sys-utils/ldattach.c)::

+ #include <stdio.h>
+ #include <stdint.h>
#include <linux/gsmmux.h>
#include <linux/tty.h>
#define DEFAULT_SPEED B115200
@@ -30,6 +33,7 @@ Major parts of the initialization program :
int ldisc = N_GSM0710;
struct gsm_config c;
struct termios configuration;
+ uint32_t first;

/* open the serial port connected to the modem */
fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
@@ -58,19 +62,22 @@ Major parts of the initialization program :
c.mtu = 127;
/* set the new configuration */
ioctl(fd, GSMIOC_SETCONF, &c);
+ /* get first gsmtty device node */
+ ioctl(fd, GSMIOC_GETFIRST, &first);
+ printf("first muxed line: /dev/gsmtty%i\n", first);

/* and wait for ever to keep the line discipline enabled */
daemon(0,0);
pause();

-4. use these devices as plain serial ports.
+5. use these devices as plain serial ports.

for example, it's possible:

- and to use gnokii to send / receive SMS on ttygsm1
- to use ppp to establish a datalink on ttygsm2

-5. first close all virtual ports before closing the physical port.
+6. first close all virtual ports before closing the physical port.

Note that after closing the physical port the modem is still in multiplexing
mode. This may prevent a successful re-opening of the port later. To avoid
diff --git a/drivers/tty/n_gsm.c b/drivers/tty/n_gsm.c
index a60be591f1fc..d30525946892 100644
--- a/drivers/tty/n_gsm.c
+++ b/drivers/tty/n_gsm.c
@@ -2613,6 +2613,7 @@ static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
{
struct gsm_config c;
struct gsm_mux *gsm = tty->disc_data;
+ unsigned int base;

switch (cmd) {
case GSMIOC_GETCONF:
@@ -2624,6 +2625,9 @@ static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
if (copy_from_user(&c, (void *)arg, sizeof(c)))
return -EFAULT;
return gsm_config(gsm, &c);
+ case GSMIOC_GETFIRST:
+ base = mux_num_to_base(gsm);
+ return put_user(base + 1, (__u32 __user *)arg);
default:
return n_tty_ioctl_helper(tty, file, cmd, arg);
}
diff --git a/include/uapi/linux/gsmmux.h b/include/uapi/linux/gsmmux.h
index 101d3c469acb..cb8693b39cb7 100644
--- a/include/uapi/linux/gsmmux.h
+++ b/include/uapi/linux/gsmmux.h
@@ -37,5 +37,7 @@ struct gsm_netconfig {
#define GSMIOC_ENABLE_NET _IOW('G', 2, struct gsm_netconfig)
#define GSMIOC_DISABLE_NET _IO('G', 3)

+/* get the base tty number for a configured gsmmux tty */
+#define GSMIOC_GETFIRST _IOR('G', 4, __u32)

#endif
--
2.22.0