2021-06-08 04:05:04

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 00/10] net: WWAN subsystem improvements

While working on WWAN netdev creation support, I notice a few things
that could be done to make the wwan subsystem more developer and user
friendly. This series implements them.

The series begins with a WWAN HW simulator designed simplify testing
and make the WWAN subsystem available for a wider audience. The next two
patches are intended to make the code a bit more clearer. This is
followed by a few patches to make the port device naming more
user-friendly. The series is finishes with a set of changes that allow
the WWAN AT port to be used with terminal emulation software.

All changes were tested with the HW simulator that was introduced in
this series, as well as with a Huawei E3372 LTE modem (a CDC-NCM
device), which I finally found on my desk.

Sergey Ryazanov (10):
wwan_hwsim: WWAN device simulator
wwan_hwsim: add debugfs management interface
net: wwan: make WWAN_PORT_MAX meaning less surprised
net: wwan: core: init port type string array using enum values
net: wwan: core: spell port device name in lowercase
net: wwan: core: make port names more user-friendly
net: wwan: core: expand ports number limit
net: wwan: core: implement TIOCINQ ioctl
net: wwan: core: implement terminal ioctls for AT port
net: wwan: core: purge rx queue on port close

drivers/net/wwan/Kconfig | 10 +
drivers/net/wwan/Makefile | 2 +
drivers/net/wwan/wwan_core.c | 238 ++++++++++++++--
drivers/net/wwan/wwan_hwsim.c | 500 ++++++++++++++++++++++++++++++++++
include/linux/wwan.h | 12 +-
5 files changed, 738 insertions(+), 24 deletions(-)
create mode 100644 drivers/net/wwan/wwan_hwsim.c

--
2.26.3


2021-06-08 04:05:04

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 08/10] net: wwan: core: implement TIOCINQ ioctl

It is quite common for a userpace program to fetch the buffered amount
of data in the rx queue to avoid the read block. Implement the TIOCINQ
ioctl to make the migration to the WWAN port usage smooth.

Despite the fact that the read call will return no more data than the
size of a first skb in the queue, TIOCINQ returns the entire amount of
buffered data (sum of all queued skbs). This is done to prevent the
breaking of programs that optimize reading, avoiding it if the buffered
amount of data is too small.

Signed-off-by: Sergey Ryazanov <[email protected]>
---
drivers/net/wwan/wwan_core.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)

diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c
index 9346b2661eb3..d5a197da4a41 100644
--- a/drivers/net/wwan/wwan_core.c
+++ b/drivers/net/wwan/wwan_core.c
@@ -12,6 +12,7 @@
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/types.h>
+#include <linux/termios.h>
#include <linux/wwan.h>

/* Maximum number of minors in use */
@@ -618,6 +619,30 @@ static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
return mask;
}

+static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
+ unsigned long arg)
+{
+ struct wwan_port *port = filp->private_data;
+
+ switch (cmd) {
+ case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
+ unsigned long flags;
+ struct sk_buff *skb;
+ int amount = 0;
+
+ spin_lock_irqsave(&port->rxq.lock, flags);
+ skb_queue_walk(&port->rxq, skb)
+ amount += skb->len;
+ spin_unlock_irqrestore(&port->rxq.lock, flags);
+
+ return put_user(amount, (int __user *)arg);
+ }
+
+ default:
+ return -ENOIOCTLCMD;
+ }
+}
+
static const struct file_operations wwan_port_fops = {
.owner = THIS_MODULE,
.open = wwan_port_fops_open,
@@ -625,6 +650,10 @@ static const struct file_operations wwan_port_fops = {
.read = wwan_port_fops_read,
.write = wwan_port_fops_write,
.poll = wwan_port_fops_poll,
+ .unlocked_ioctl = wwan_port_fops_ioctl,
+#ifdef CONFIG_COMPAT
+ .compat_ioctl = compat_ptr_ioctl,
+#endif
.llseek = noop_llseek,
};

--
2.26.3

2021-06-08 04:05:32

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 04/10] net: wwan: core: init port type string array using enum values

This array is indexed by port type. Make it self-descriptive by using
the port type enum values as indices in the array initializer.

Signed-off-by: Sergey Ryazanov <[email protected]>
---
drivers/net/wwan/wwan_core.c | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c
index 632ff86398ac..97d77b06d222 100644
--- a/drivers/net/wwan/wwan_core.c
+++ b/drivers/net/wwan/wwan_core.c
@@ -184,13 +184,12 @@ static void wwan_remove_dev(struct wwan_device *wwandev)

/* ------- WWAN port management ------- */

-/* Keep aligned with wwan_port_type enum */
-static const char * const wwan_port_type_str[] = {
- "AT",
- "MBIM",
- "QMI",
- "QCDM",
- "FIREHOSE"
+static const char * const wwan_port_type_str[WWAN_PORT_MAX + 1] = {
+ [WWAN_PORT_AT] = "AT",
+ [WWAN_PORT_MBIM] = "MBIM",
+ [WWAN_PORT_QMI] = "QMI",
+ [WWAN_PORT_QCDM] = "QCDM",
+ [WWAN_PORT_FIREHOSE] = "FIREHOSE",
};

static ssize_t type_show(struct device *dev, struct device_attribute *attr,
--
2.26.3

2021-06-08 04:05:38

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 07/10] net: wwan: core: expand ports number limit

Currently, we limit the total ports number to 256. It is quite common
for PBX or SMS gateway to be equipped with a lot of modems. In now days,
a modem could have 2-4 control ports or even more, what only accelerates
the ports exhausing rate.

To avoid facing the port number limitation issue reports, increase the
limit up the maximum number of minors (i.e. up to 1 << MINORBITS).

Signed-off-by: Sergey Ryazanov <[email protected]>
---
drivers/net/wwan/wwan_core.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c
index 2844b17a724c..9346b2661eb3 100644
--- a/drivers/net/wwan/wwan_core.c
+++ b/drivers/net/wwan/wwan_core.c
@@ -14,7 +14,8 @@
#include <linux/types.h>
#include <linux/wwan.h>

-#define WWAN_MAX_MINORS 256 /* 256 minors allowed with register_chrdev() */
+/* Maximum number of minors in use */
+#define WWAN_MAX_MINORS (1 << MINORBITS)

static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
@@ -634,7 +635,8 @@ static int __init wwan_init(void)
return PTR_ERR(wwan_class);

/* chrdev used for wwan ports */
- wwan_major = register_chrdev(0, "wwan_port", &wwan_port_fops);
+ wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
+ &wwan_port_fops);
if (wwan_major < 0) {
class_destroy(wwan_class);
return wwan_major;
@@ -645,7 +647,7 @@ static int __init wwan_init(void)

static void __exit wwan_exit(void)
{
- unregister_chrdev(wwan_major, "wwan_port");
+ __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port");
class_destroy(wwan_class);
}

--
2.26.3

2021-06-08 04:05:42

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 09/10] net: wwan: core: implement terminal ioctls for AT port

It is not unreasonable to assume that users will use terminal emulation
software to communicate directly with a WWAN device over the AT port.
But terminal emulators will refuse to work with a device that does not
support terminal IOCTLs (e.g. TCGETS, TCSETS, TIOCMSET, etc.). To make
it possible to interact with the WWAN AT port using a terminal emulator,
implement a minimal set of terminal IOCTLs.

The implementation is rather stub, no passed data are actually used to
control a port behaviour. An obtained configuration is kept inside the
port structure and returned back by a request. The latter is done to
fool a program that will test the configuration status by comparing the
readed back data from the device with earlier configured ones.

Tested with fresh versions of minicom and picocom terminal apps.

MBIM, QMI and other ports for binary protocols can hardly be considered
a terminal device, so terminal IOCTLs are only implemented for the AT
port.

Signed-off-by: Sergey Ryazanov <[email protected]>
---
drivers/net/wwan/wwan_core.c | 91 ++++++++++++++++++++++++++++++++++++
1 file changed, 91 insertions(+)

diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c
index d5a197da4a41..38da3124d81e 100644
--- a/drivers/net/wwan/wwan_core.c
+++ b/drivers/net/wwan/wwan_core.c
@@ -51,6 +51,8 @@ struct wwan_device {
* @dev: Underlying device
* @rxq: Buffer inbound queue
* @waitqueue: The waitqueue for port fops (read/write/poll)
+ * @data_lock: Port specific data access serialization
+ * @at_data: AT port specific data
*/
struct wwan_port {
enum wwan_port_type type;
@@ -61,6 +63,13 @@ struct wwan_port {
struct device dev;
struct sk_buff_head rxq;
wait_queue_head_t waitqueue;
+ struct mutex data_lock; /* Port specific data access serialization */
+ union {
+ struct {
+ struct ktermios termios;
+ int mdmbits;
+ } at_data;
+ };
};

static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
@@ -230,6 +239,7 @@ static void wwan_port_destroy(struct device *dev)
struct wwan_port *port = to_wwan_port(dev);

ida_free(&minors, MINOR(port->dev.devt));
+ mutex_destroy(&port->data_lock);
skb_queue_purge(&port->rxq);
mutex_destroy(&port->ops_lock);
kfree(port);
@@ -344,6 +354,7 @@ struct wwan_port *wwan_create_port(struct device *parent,
mutex_init(&port->ops_lock);
skb_queue_head_init(&port->rxq);
init_waitqueue_head(&port->waitqueue);
+ mutex_init(&port->data_lock);

port->dev.parent = &wwandev->dev;
port->dev.class = wwan_class;
@@ -619,10 +630,90 @@ static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
return mask;
}

+/* Implements minimalistic stub terminal IOCTLs support */
+static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
+ unsigned long arg)
+{
+ int ret = 0;
+
+ mutex_lock(&port->data_lock);
+
+ switch (cmd) {
+ case TCFLSH:
+ break;
+
+ case TCGETS:
+ if (copy_to_user((void __user *)arg, &port->at_data.termios,
+ sizeof(struct termios)))
+ ret = -EFAULT;
+ break;
+
+ case TCSETS:
+ case TCSETSW:
+ case TCSETSF:
+ if (copy_from_user(&port->at_data.termios, (void __user *)arg,
+ sizeof(struct termios)))
+ ret = -EFAULT;
+ break;
+
+#ifdef TCGETS2
+ case TCGETS2:
+ if (copy_to_user((void __user *)arg, &port->at_data.termios,
+ sizeof(struct termios2)))
+ ret = -EFAULT;
+ break;
+
+ case TCSETS2:
+ case TCSETSW2:
+ case TCSETSF2:
+ if (copy_from_user(&port->at_data.termios, (void __user *)arg,
+ sizeof(struct termios2)))
+ ret = -EFAULT;
+ break;
+#endif
+
+ case TIOCMGET:
+ ret = put_user(port->at_data.mdmbits, (int __user *)arg);
+ break;
+
+ case TIOCMSET:
+ case TIOCMBIC:
+ case TIOCMBIS: {
+ int mdmbits;
+
+ if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
+ ret = -EFAULT;
+ break;
+ }
+ if (cmd == TIOCMBIC)
+ port->at_data.mdmbits &= ~mdmbits;
+ else if (cmd == TIOCMBIS)
+ port->at_data.mdmbits |= mdmbits;
+ else
+ port->at_data.mdmbits = mdmbits;
+ break;
+ }
+
+ default:
+ ret = -ENOIOCTLCMD;
+ }
+
+ mutex_unlock(&port->data_lock);
+
+ return ret;
+}
+
static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
unsigned long arg)
{
struct wwan_port *port = filp->private_data;
+ int res;
+
+ if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */
+ res = wwan_port_fops_at_ioctl(port, cmd, arg);
+ if (res != -ENOIOCTLCMD)
+ return res;
+ }

switch (cmd) {
case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
--
2.26.3

2021-06-08 04:06:44

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 10/10] net: wwan: core: purge rx queue on port close

Purge the rx queue as soon as a user closes the port, just after the
port stop callback invocation. This is to prevent feeding a user that
will open the port next time with outdated and possibly unrelated
data.

While at it also remove the odd skb_queue_purge() call in the port
device destroy callback. The queue will be purged just before the
callback is ivoncated in the wwan_remove_port() function.

Signed-off-by: Sergey Ryazanov <[email protected]>
---
drivers/net/wwan/wwan_core.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c
index 38da3124d81e..45a41aee8958 100644
--- a/drivers/net/wwan/wwan_core.c
+++ b/drivers/net/wwan/wwan_core.c
@@ -240,7 +240,6 @@ static void wwan_port_destroy(struct device *dev)

ida_free(&minors, MINOR(port->dev.devt));
mutex_destroy(&port->data_lock);
- skb_queue_purge(&port->rxq);
mutex_destroy(&port->ops_lock);
kfree(port);
}
@@ -462,8 +461,11 @@ static void wwan_port_op_stop(struct wwan_port *port)
{
mutex_lock(&port->ops_lock);
port->start_count--;
- if (port->ops && !port->start_count)
- port->ops->stop(port);
+ if (!port->start_count) {
+ if (port->ops)
+ port->ops->stop(port);
+ skb_queue_purge(&port->rxq);
+ }
mutex_unlock(&port->ops_lock);
}

--
2.26.3

2021-06-08 04:06:44

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 05/10] net: wwan: core: spell port device name in lowercase

Usually a device name is spelled in lowercase, let us follow this
practice in the WWAN subsystem as well. The bottom line is that such
name is easier to type.

To keep the device type attribute contents more natural (i.e., spell
abbreviations in uppercase), while making the device name lowercase,
turn the port type strings array to an array of structure that contains
both the port type name and the device name suffix.

Signed-off-by: Sergey Ryazanov <[email protected]>
---
drivers/net/wwan/wwan_core.c | 34 ++++++++++++++++++++++++++--------
1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c
index 97d77b06d222..ba4392d71b80 100644
--- a/drivers/net/wwan/wwan_core.c
+++ b/drivers/net/wwan/wwan_core.c
@@ -184,12 +184,30 @@ static void wwan_remove_dev(struct wwan_device *wwandev)

/* ------- WWAN port management ------- */

-static const char * const wwan_port_type_str[WWAN_PORT_MAX + 1] = {
- [WWAN_PORT_AT] = "AT",
- [WWAN_PORT_MBIM] = "MBIM",
- [WWAN_PORT_QMI] = "QMI",
- [WWAN_PORT_QCDM] = "QCDM",
- [WWAN_PORT_FIREHOSE] = "FIREHOSE",
+static const struct {
+ const char * const name; /* Port type name */
+ const char * const devsuf; /* Port devce name suffix */
+} wwan_port_types[WWAN_PORT_MAX + 1] = {
+ [WWAN_PORT_AT] = {
+ .name = "AT",
+ .devsuf = "at",
+ },
+ [WWAN_PORT_MBIM] = {
+ .name = "MBIM",
+ .devsuf = "mbim",
+ },
+ [WWAN_PORT_QMI] = {
+ .name = "QMI",
+ .devsuf = "qmi",
+ },
+ [WWAN_PORT_QCDM] = {
+ .name = "QCDM",
+ .devsuf = "qcdm",
+ },
+ [WWAN_PORT_FIREHOSE] = {
+ .name = "FIREHOSE",
+ .devsuf = "firehose",
+ },
};

static ssize_t type_show(struct device *dev, struct device_attribute *attr,
@@ -197,7 +215,7 @@ static ssize_t type_show(struct device *dev, struct device_attribute *attr,
{
struct wwan_port *port = to_wwan_port(dev);

- return sprintf(buf, "%s\n", wwan_port_type_str[port->type]);
+ return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
}
static DEVICE_ATTR_RO(type);

@@ -285,7 +303,7 @@ struct wwan_port *wwan_create_port(struct device *parent,
/* create unique name based on wwan device id, port index and type */
dev_set_name(&port->dev, "wwan%up%u%s", wwandev->id,
atomic_inc_return(&wwandev->port_id),
- wwan_port_type_str[port->type]);
+ wwan_port_types[port->type].devsuf);

err = device_register(&port->dev);
if (err)
--
2.26.3

2021-06-08 04:07:12

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 03/10] net: wwan: make WWAN_PORT_MAX meaning less surprised

It is quite unusual when some value can not be equal to a defined range
max value. Also most subsystems defines FOO_TYPE_MAX as a maximum valid
value. So turn the WAN_PORT_MAX meaning from the number of supported
port types to the maximum valid port type.

Signed-off-by: Sergey Ryazanov <[email protected]>
---
drivers/net/wwan/wwan_core.c | 2 +-
include/linux/wwan.h | 12 +++++++++---
2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c
index 6e8f19c71a9e..632ff86398ac 100644
--- a/drivers/net/wwan/wwan_core.c
+++ b/drivers/net/wwan/wwan_core.c
@@ -250,7 +250,7 @@ struct wwan_port *wwan_create_port(struct device *parent,
struct wwan_port *port;
int minor, err = -ENOMEM;

- if (type >= WWAN_PORT_MAX || !ops)
+ if (type > WWAN_PORT_MAX || !ops)
return ERR_PTR(-EINVAL);

/* A port is always a child of a WWAN device, retrieve (allocate or
diff --git a/include/linux/wwan.h b/include/linux/wwan.h
index 7216c114d758..fa33cc16d931 100644
--- a/include/linux/wwan.h
+++ b/include/linux/wwan.h
@@ -15,8 +15,10 @@
* @WWAN_PORT_QMI: Qcom modem/MSM interface for modem control
* @WWAN_PORT_QCDM: Qcom Modem diagnostic interface
* @WWAN_PORT_FIREHOSE: XML based command protocol
- * @WWAN_PORT_UNKNOWN: Unknown port type
- * @WWAN_PORT_MAX: Number of supported port types
+ *
+ * @WWAN_PORT_MAX: Highest supported port types
+ * @WWAN_PORT_UNKNOWN: Special value to indicate an unknown port type
+ * @__WWAN_PORT_MAX: Internal use
*/
enum wwan_port_type {
WWAN_PORT_AT,
@@ -24,8 +26,12 @@ enum wwan_port_type {
WWAN_PORT_QMI,
WWAN_PORT_QCDM,
WWAN_PORT_FIREHOSE,
+
+ /* Add new port types above this line */
+
+ __WWAN_PORT_MAX,
+ WWAN_PORT_MAX = __WWAN_PORT_MAX - 1,
WWAN_PORT_UNKNOWN,
- WWAN_PORT_MAX = WWAN_PORT_UNKNOWN,
};

struct wwan_port;
--
2.26.3

2021-06-08 04:07:23

by Sergey Ryazanov

[permalink] [raw]
Subject: [PATCH 01/10] wwan_hwsim: WWAN device simulator

This driver simulates a set of WWAN device with a set of AT control
ports. It can be used to test WWAN kernel framework as well as user
space tools.

Signed-off-by: Sergey Ryazanov <[email protected]>
---
drivers/net/wwan/Kconfig | 10 ++
drivers/net/wwan/Makefile | 2 +
drivers/net/wwan/wwan_hwsim.c | 318 ++++++++++++++++++++++++++++++++++
3 files changed, 330 insertions(+)
create mode 100644 drivers/net/wwan/wwan_hwsim.c

diff --git a/drivers/net/wwan/Kconfig b/drivers/net/wwan/Kconfig
index 7ad1920120bc..ec0b194a373c 100644
--- a/drivers/net/wwan/Kconfig
+++ b/drivers/net/wwan/Kconfig
@@ -20,6 +20,16 @@ config WWAN_CORE
To compile this driver as a module, choose M here: the module will be
called wwan.

+config WWAN_HWSIM
+ tristate "Simulated WWAN device"
+ depends on WWAN_CORE
+ help
+ This driver is a developer testing tool that can be used to test WWAN
+ framework.
+
+ To compile this driver as a module, choose M here: the module will be
+ called wwan_hwsim. If unsure, say N.
+
config MHI_WWAN_CTRL
tristate "MHI WWAN control driver for QCOM-based PCIe modems"
select WWAN_CORE
diff --git a/drivers/net/wwan/Makefile b/drivers/net/wwan/Makefile
index 556cd90958ca..f33f77ca1021 100644
--- a/drivers/net/wwan/Makefile
+++ b/drivers/net/wwan/Makefile
@@ -6,4 +6,6 @@
obj-$(CONFIG_WWAN_CORE) += wwan.o
wwan-objs += wwan_core.o

+obj-$(CONFIG_WWAN_HWSIM) += wwan_hwsim.o
+
obj-$(CONFIG_MHI_WWAN_CTRL) += mhi_wwan_ctrl.o
diff --git a/drivers/net/wwan/wwan_hwsim.c b/drivers/net/wwan/wwan_hwsim.c
new file mode 100644
index 000000000000..96d25d7e5bb8
--- /dev/null
+++ b/drivers/net/wwan/wwan_hwsim.c
@@ -0,0 +1,318 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * WWAN device simulator for WWAN framework testing.
+ *
+ * Copyright (c) 2021, Sergey Ryazanov <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/device.h>
+#include <linux/spinlock.h>
+#include <linux/list.h>
+#include <linux/skbuff.h>
+#include <linux/wwan.h>
+
+static int wwan_hwsim_devsnum = 2;
+module_param_named(devices, wwan_hwsim_devsnum, int, 0444);
+MODULE_PARM_DESC(devices, "Number of simulated devices");
+
+static struct class *wwan_hwsim_class;
+
+static DEFINE_SPINLOCK(wwan_hwsim_devs_lock);
+static LIST_HEAD(wwan_hwsim_devs);
+static unsigned int wwan_hwsim_dev_idx;
+
+struct wwan_hwsim_dev {
+ struct list_head list;
+ unsigned int id;
+ struct device dev;
+ spinlock_t ports_lock; /* Serialize ports creation/deletion */
+ unsigned int port_idx;
+ struct list_head ports;
+};
+
+struct wwan_hwsim_port {
+ struct list_head list;
+ unsigned int id;
+ struct wwan_hwsim_dev *dev;
+ struct wwan_port *wwan;
+ enum { /* AT command parser state */
+ AT_PARSER_WAIT_A,
+ AT_PARSER_WAIT_T,
+ AT_PARSER_WAIT_TERM,
+ AT_PARSER_SKIP_LINE,
+ } pstate;
+};
+
+static int wwan_hwsim_port_start(struct wwan_port *wport)
+{
+ struct wwan_hwsim_port *port = wwan_port_get_drvdata(wport);
+
+ port->pstate = AT_PARSER_WAIT_A;
+
+ return 0;
+}
+
+static void wwan_hwsim_port_stop(struct wwan_port *wport)
+{
+}
+
+/* Implements a minimalistic AT commands parser that echo input back and
+ * reply with 'OK' to each input command. See AT command protocol details in the
+ * ITU-T V.250 recomendations document.
+ *
+ * Be aware that this processor is not fully V.250 compliant.
+ */
+static int wwan_hwsim_port_tx(struct wwan_port *wport, struct sk_buff *in)
+{
+ struct wwan_hwsim_port *port = wwan_port_get_drvdata(wport);
+ struct sk_buff *out;
+ int i, n, s;
+
+ /* Estimate a max possible number of commands by counting the number of
+ * termination chars (S3 param, CR by default). And then allocate the
+ * output buffer that will be enough to fit the echo and result codes of
+ * all commands.
+ */
+ for (i = 0, n = 0; i < in->len; ++i)
+ if (in->data[i] == '\r')
+ n++;
+ n = in->len + n * (2 + 2 + 2); /* Output buffer size */
+ out = alloc_skb(n, GFP_KERNEL);
+ if (!out)
+ return -ENOMEM;
+
+ for (i = 0, s = 0; i < in->len; ++i) {
+ char c = in->data[i];
+
+ if (port->pstate == AT_PARSER_WAIT_A) {
+ if (c == 'A' || c == 'a')
+ port->pstate = AT_PARSER_WAIT_T;
+ else if (c != '\n') /* Ignore formating char */
+ port->pstate = AT_PARSER_SKIP_LINE;
+ } else if (port->pstate == AT_PARSER_WAIT_T) {
+ if (c == 'T' || c == 't')
+ port->pstate = AT_PARSER_WAIT_TERM;
+ else
+ port->pstate = AT_PARSER_SKIP_LINE;
+ } else if (port->pstate == AT_PARSER_WAIT_TERM) {
+ if (c != '\r')
+ continue;
+ /* Consume the trailing formatting char as well */
+ if ((i + 1) < in->len && in->data[i + 1] == '\n')
+ i++;
+ n = i - s + 1;
+ memcpy(skb_put(out, n), &in->data[s], n);/* Echo */
+ memcpy(skb_put(out, 6), "\r\nOK\r\n", 6);
+ s = i + 1;
+ port->pstate = AT_PARSER_WAIT_A;
+ } else if (port->pstate == AT_PARSER_SKIP_LINE) {
+ if (c != '\r')
+ continue;
+ port->pstate = AT_PARSER_WAIT_A;
+ }
+ }
+
+ if (i > s) {
+ /* Echo the processed portion of a not yet completed command */
+ n = i - s;
+ memcpy(skb_put(out, n), &in->data[s], n);
+ }
+
+ consume_skb(in);
+
+ wwan_port_rx(wport, out);
+
+ return 0;
+}
+
+static const struct wwan_port_ops wwan_hwsim_port_ops = {
+ .start = wwan_hwsim_port_start,
+ .stop = wwan_hwsim_port_stop,
+ .tx = wwan_hwsim_port_tx,
+};
+
+static struct wwan_hwsim_port *wwan_hwsim_port_new(struct wwan_hwsim_dev *dev)
+{
+ struct wwan_hwsim_port *port;
+ int err;
+
+ port = kzalloc(sizeof(*port), GFP_KERNEL);
+ if (!port)
+ return ERR_PTR(-ENOMEM);
+
+ port->dev = dev;
+
+ spin_lock(&dev->ports_lock);
+ port->id = dev->port_idx++;
+ spin_unlock(&dev->ports_lock);
+
+ port->wwan = wwan_create_port(&dev->dev, WWAN_PORT_AT,
+ &wwan_hwsim_port_ops,
+ port);
+ if (IS_ERR(port->wwan)) {
+ err = PTR_ERR(port->wwan);
+ goto err_free_port;
+ }
+
+ return port;
+
+err_free_port:
+ kfree(port);
+
+ return ERR_PTR(err);
+}
+
+static void wwan_hwsim_port_del(struct wwan_hwsim_port *port)
+{
+ wwan_remove_port(port->wwan);
+ kfree(port);
+}
+
+static void wwan_hwsim_dev_release(struct device *sysdev)
+{
+ struct wwan_hwsim_dev *dev = container_of(sysdev, typeof(*dev), dev);
+
+ kfree(dev);
+}
+
+static struct wwan_hwsim_dev *wwan_hwsim_dev_new(void)
+{
+ struct wwan_hwsim_dev *dev;
+ int err;
+
+ dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+ if (!dev)
+ return ERR_PTR(-ENOMEM);
+
+ spin_lock(&wwan_hwsim_devs_lock);
+ dev->id = wwan_hwsim_dev_idx++;
+ spin_unlock(&wwan_hwsim_devs_lock);
+
+ dev->dev.release = wwan_hwsim_dev_release;
+ dev->dev.class = wwan_hwsim_class;
+ dev_set_name(&dev->dev, "hwsim%u", dev->id);
+
+ spin_lock_init(&dev->ports_lock);
+ INIT_LIST_HEAD(&dev->ports);
+
+ err = device_register(&dev->dev);
+ if (err)
+ goto err_free_dev;
+
+ return dev;
+
+err_free_dev:
+ kfree(dev);
+
+ return ERR_PTR(err);
+}
+
+static void wwan_hwsim_dev_del(struct wwan_hwsim_dev *dev)
+{
+ spin_lock(&dev->ports_lock);
+ while (!list_empty(&dev->ports)) {
+ struct wwan_hwsim_port *port;
+
+ port = list_first_entry(&dev->ports, struct wwan_hwsim_port,
+ list);
+ list_del(&port->list);
+ spin_unlock(&dev->ports_lock);
+ wwan_hwsim_port_del(port);
+ spin_lock(&dev->ports_lock);
+ }
+ spin_unlock(&dev->ports_lock);
+
+ device_unregister(&dev->dev);
+ /* Memory will be freed in the device release callback */
+}
+
+static int __init wwan_hwsim_init_devs(void)
+{
+ struct wwan_hwsim_dev *dev;
+ int i, j;
+
+ for (i = 0; i < wwan_hwsim_devsnum; ++i) {
+ dev = wwan_hwsim_dev_new();
+ if (IS_ERR(dev))
+ return PTR_ERR(dev);
+
+ spin_lock(&wwan_hwsim_devs_lock);
+ list_add_tail(&dev->list, &wwan_hwsim_devs);
+ spin_unlock(&wwan_hwsim_devs_lock);
+
+ /* Create a couple of ports per each device to accelerate
+ * the simulator readiness time.
+ */
+ for (j = 0; j < 2; ++j) {
+ struct wwan_hwsim_port *port;
+
+ port = wwan_hwsim_port_new(dev);
+ if (IS_ERR(port))
+ return PTR_ERR(port);
+
+ spin_lock(&dev->ports_lock);
+ list_add_tail(&port->list, &dev->ports);
+ spin_unlock(&dev->ports_lock);
+ }
+ }
+
+ return 0;
+}
+
+static void wwan_hwsim_free_devs(void)
+{
+ struct wwan_hwsim_dev *dev;
+
+ spin_lock(&wwan_hwsim_devs_lock);
+ while (!list_empty(&wwan_hwsim_devs)) {
+ dev = list_first_entry(&wwan_hwsim_devs, struct wwan_hwsim_dev,
+ list);
+ list_del(&dev->list);
+ spin_unlock(&wwan_hwsim_devs_lock);
+ wwan_hwsim_dev_del(dev);
+ spin_lock(&wwan_hwsim_devs_lock);
+ }
+ spin_unlock(&wwan_hwsim_devs_lock);
+}
+
+static int __init wwan_hwsim_init(void)
+{
+ int err;
+
+ if (wwan_hwsim_devsnum < 0 || wwan_hwsim_devsnum > 128)
+ return -EINVAL;
+
+ wwan_hwsim_class = class_create(THIS_MODULE, "wwan_hwsim");
+ if (IS_ERR(wwan_hwsim_class))
+ return PTR_ERR(wwan_hwsim_class);
+
+ err = wwan_hwsim_init_devs();
+ if (err)
+ goto err_clean_devs;
+
+ return 0;
+
+err_clean_devs:
+ wwan_hwsim_free_devs();
+ class_destroy(wwan_hwsim_class);
+
+ return err;
+}
+
+static void __exit wwan_hwsim_exit(void)
+{
+ wwan_hwsim_free_devs();
+ class_destroy(wwan_hwsim_class);
+}
+
+module_init(wwan_hwsim_init);
+module_exit(wwan_hwsim_exit);
+
+MODULE_AUTHOR("Sergey Ryazanov");
+MODULE_DESCRIPTION("Device simulator for WWAN framework");
+MODULE_LICENSE("GPL");
--
2.26.3

2021-06-08 08:28:33

by Loic Poulain

[permalink] [raw]
Subject: Re: [PATCH 03/10] net: wwan: make WWAN_PORT_MAX meaning less surprised

On Tue, 8 Jun 2021 at 06:02, Sergey Ryazanov <[email protected]> wrote:
>
> It is quite unusual when some value can not be equal to a defined range
> max value. Also most subsystems defines FOO_TYPE_MAX as a maximum valid
> value. So turn the WAN_PORT_MAX meaning from the number of supported
> port types to the maximum valid port type.
>
> Signed-off-by: Sergey Ryazanov <[email protected]>

Reviewed-by: Loic Poulain <[email protected]>

2021-06-08 08:31:42

by Loic Poulain

[permalink] [raw]
Subject: Re: [PATCH 04/10] net: wwan: core: init port type string array using enum values

On Tue, 8 Jun 2021 at 06:02, Sergey Ryazanov <[email protected]> wrote:
>
> This array is indexed by port type. Make it self-descriptive by using
> the port type enum values as indices in the array initializer.
>
> Signed-off-by: Sergey Ryazanov <[email protected]>

Reviewed-by: Loic Poulain <[email protected]>

2021-06-08 08:31:53

by Loic Poulain

[permalink] [raw]
Subject: Re: [PATCH 05/10] net: wwan: core: spell port device name in lowercase

On Tue, 8 Jun 2021 at 06:02, Sergey Ryazanov <[email protected]> wrote:
>
> Usually a device name is spelled in lowercase, let us follow this
> practice in the WWAN subsystem as well. The bottom line is that such
> name is easier to type.
>
> To keep the device type attribute contents more natural (i.e., spell
> abbreviations in uppercase), while making the device name lowercase,
> turn the port type strings array to an array of structure that contains
> both the port type name and the device name suffix.
>
> Signed-off-by: Sergey Ryazanov <[email protected]>

Reviewed-by: Loic Poulain <[email protected]>

2021-06-08 08:38:25

by Loic Poulain

[permalink] [raw]
Subject: Re: [PATCH 07/10] net: wwan: core: expand ports number limit

On Tue, 8 Jun 2021 at 06:02, Sergey Ryazanov <[email protected]> wrote:
>
> Currently, we limit the total ports number to 256. It is quite common
> for PBX or SMS gateway to be equipped with a lot of modems. In now days,
> a modem could have 2-4 control ports or even more, what only accelerates
> the ports exhausing rate.
>
> To avoid facing the port number limitation issue reports, increase the
> limit up the maximum number of minors (i.e. up to 1 << MINORBITS).
>
> Signed-off-by: Sergey Ryazanov <[email protected]>

Reviewed-by: Loic Poulain <[email protected]>

2021-06-08 08:44:47

by Loic Poulain

[permalink] [raw]
Subject: Re: [PATCH 08/10] net: wwan: core: implement TIOCINQ ioctl

On Tue, 8 Jun 2021 at 06:02, Sergey Ryazanov <[email protected]> wrote:
>
> It is quite common for a userpace program to fetch the buffered amount
> of data in the rx queue to avoid the read block. Implement the TIOCINQ
> ioctl to make the migration to the WWAN port usage smooth.
>
> Despite the fact that the read call will return no more data than the
> size of a first skb in the queue, TIOCINQ returns the entire amount of
> buffered data (sum of all queued skbs). This is done to prevent the
> breaking of programs that optimize reading, avoiding it if the buffered
> amount of data is too small.
>
> Signed-off-by: Sergey Ryazanov <[email protected]>

Reviewed-by: Loic Poulain <[email protected]>

2021-06-08 21:51:57

by patchwork-bot+netdevbpf

[permalink] [raw]
Subject: Re: [PATCH 00/10] net: WWAN subsystem improvements

Hello:

This series was applied to netdev/net-next.git (refs/heads/master):

On Tue, 8 Jun 2021 07:02:31 +0300 you wrote:
> While working on WWAN netdev creation support, I notice a few things
> that could be done to make the wwan subsystem more developer and user
> friendly. This series implements them.
>
> The series begins with a WWAN HW simulator designed simplify testing
> and make the WWAN subsystem available for a wider audience. The next two
> patches are intended to make the code a bit more clearer. This is
> followed by a few patches to make the port device naming more
> user-friendly. The series is finishes with a set of changes that allow
> the WWAN AT port to be used with terminal emulation software.
>
> [...]

Here is the summary with links:
- [01/10] wwan_hwsim: WWAN device simulator
https://git.kernel.org/netdev/net-next/c/f36a111a74e7
- [02/10] wwan_hwsim: add debugfs management interface
https://git.kernel.org/netdev/net-next/c/9ee23f48f670
- [03/10] net: wwan: make WWAN_PORT_MAX meaning less surprised
https://git.kernel.org/netdev/net-next/c/b64d76b78226
- [04/10] net: wwan: core: init port type string array using enum values
https://git.kernel.org/netdev/net-next/c/64cc80c0ff2e
- [05/10] net: wwan: core: spell port device name in lowercase
https://git.kernel.org/netdev/net-next/c/392c26f7f133
- [06/10] net: wwan: core: make port names more user-friendly
https://git.kernel.org/netdev/net-next/c/f458709ff40b
- [07/10] net: wwan: core: expand ports number limit
https://git.kernel.org/netdev/net-next/c/72eedfc4bbc7
- [08/10] net: wwan: core: implement TIOCINQ ioctl
https://git.kernel.org/netdev/net-next/c/e263c5b2e891
- [09/10] net: wwan: core: implement terminal ioctls for AT port
https://git.kernel.org/netdev/net-next/c/c230035c2f2f
- [10/10] net: wwan: core: purge rx queue on port close
https://git.kernel.org/netdev/net-next/c/504672038b17

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html