Merge branch 'wwan-improvements'
authorDavid S. Miller <davem@davemloft.net>
Tue, 8 Jun 2021 21:33:44 +0000 (14:33 -0700)
committerDavid S. Miller <davem@davemloft.net>
Tue, 8 Jun 2021 21:33:44 +0000 (14:33 -0700)
Sergey Ryazanov says:

====================
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.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/wwan/Kconfig
drivers/net/wwan/Makefile
drivers/net/wwan/wwan_core.c
drivers/net/wwan/wwan_hwsim.c [new file with mode: 0644]
include/linux/wwan.h

index 7ad1920..ec0b194 100644 (file)
@@ -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
index 556cd90..f33f77c 100644 (file)
@@ -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
index 6e8f19c..45a41ae 100644 (file)
 #include <linux/skbuff.h>
 #include <linux/slab.h>
 #include <linux/types.h>
+#include <linux/termios.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 */
@@ -33,12 +35,10 @@ static int wwan_major;
  *
  * @id: WWAN device unique ID.
  * @dev: Underlying device.
- * @port_id: Current available port ID to pick.
  */
 struct wwan_device {
        unsigned int id;
        struct device dev;
-       atomic_t port_id;
 };
 
 /**
@@ -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)
@@ -184,13 +193,30 @@ 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 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,
@@ -198,7 +224,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);
 
@@ -213,7 +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));
-       skb_queue_purge(&port->rxq);
+       mutex_destroy(&port->data_lock);
        mutex_destroy(&port->ops_lock);
        kfree(port);
 }
@@ -241,6 +267,56 @@ static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
        return to_wwan_port(dev);
 }
 
+/* Allocate and set unique name based on passed format
+ *
+ * Name allocation approach is highly inspired by the __dev_alloc_name()
+ * function.
+ *
+ * To avoid names collision, the caller must prevent the new port device
+ * registration as well as concurrent invocation of this function.
+ */
+static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
+{
+       struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
+       const unsigned int max_ports = PAGE_SIZE * 8;
+       struct class_dev_iter iter;
+       unsigned long *idmap;
+       struct device *dev;
+       char buf[0x20];
+       int id;
+
+       idmap = (unsigned long *)get_zeroed_page(GFP_KERNEL);
+       if (!idmap)
+               return -ENOMEM;
+
+       /* Collect ids of same name format ports */
+       class_dev_iter_init(&iter, wwan_class, NULL, &wwan_port_dev_type);
+       while ((dev = class_dev_iter_next(&iter))) {
+               if (dev->parent != &wwandev->dev)
+                       continue;
+               if (sscanf(dev_name(dev), fmt, &id) != 1)
+                       continue;
+               if (id < 0 || id >= max_ports)
+                       continue;
+               set_bit(id, idmap);
+       }
+       class_dev_iter_exit(&iter);
+
+       /* Allocate unique id */
+       id = find_first_zero_bit(idmap, max_ports);
+       free_page((unsigned long)idmap);
+
+       snprintf(buf, sizeof(buf), fmt, id);    /* Name generation */
+
+       dev = device_find_child_by_name(&wwandev->dev, buf);
+       if (dev) {
+               put_device(dev);
+               return -ENFILE;
+       }
+
+       return dev_set_name(&port->dev, buf);
+}
+
 struct wwan_port *wwan_create_port(struct device *parent,
                                   enum wwan_port_type type,
                                   const struct wwan_port_ops *ops,
@@ -249,8 +325,9 @@ struct wwan_port *wwan_create_port(struct device *parent,
        struct wwan_device *wwandev;
        struct wwan_port *port;
        int minor, err = -ENOMEM;
+       char namefmt[0x20];
 
-       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
@@ -276,6 +353,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;
@@ -283,12 +361,18 @@ struct wwan_port *wwan_create_port(struct device *parent,
        port->dev.devt = MKDEV(wwan_major, minor);
        dev_set_drvdata(&port->dev, drvdata);
 
-       /* 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]);
+       /* allocate unique name based on wwan device id, port type and number */
+       snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
+                wwan_port_types[port->type].devsuf);
 
+       /* Serialize ports registration */
+       mutex_lock(&wwan_register_lock);
+
+       __wwan_port_dev_assign_name(port, namefmt);
        err = device_register(&port->dev);
+
+       mutex_unlock(&wwan_register_lock);
+
        if (err)
                goto error_put_device;
 
@@ -377,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);
 }
 
@@ -545,6 +632,110 @@ 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 */
+               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,
@@ -552,6 +743,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,
 };
 
@@ -562,7 +757,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;
@@ -573,7 +769,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);
 }
 
diff --git a/drivers/net/wwan/wwan_hwsim.c b/drivers/net/wwan/wwan_hwsim.c
new file mode 100644 (file)
index 0000000..472cae5
--- /dev/null
@@ -0,0 +1,500 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * WWAN device simulator for WWAN framework testing.
+ *
+ * Copyright (c) 2021, Sergey Ryazanov <ryazanov.s.a@gmail.com>
+ */
+
+#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>
+#include <linux/debugfs.h>
+#include <linux/workqueue.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 struct dentry *wwan_hwsim_debugfs_topdir;
+static struct dentry *wwan_hwsim_debugfs_devcreate;
+
+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;
+       struct work_struct del_work;
+       struct dentry *debugfs_topdir;
+       struct dentry *debugfs_portcreate;
+       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;
+       struct work_struct del_work;
+       struct dentry *debugfs_topdir;
+       enum {                  /* AT command parser state */
+               AT_PARSER_WAIT_A,
+               AT_PARSER_WAIT_T,
+               AT_PARSER_WAIT_TERM,
+               AT_PARSER_SKIP_LINE,
+       } pstate;
+};
+
+static const struct file_operations wwan_hwsim_debugfs_portdestroy_fops;
+static const struct file_operations wwan_hwsim_debugfs_portcreate_fops;
+static const struct file_operations wwan_hwsim_debugfs_devdestroy_fops;
+static void wwan_hwsim_port_del_work(struct work_struct *work);
+static void wwan_hwsim_dev_del_work(struct work_struct *work);
+
+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;
+       char name[0x10];
+       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;
+       }
+
+       INIT_WORK(&port->del_work, wwan_hwsim_port_del_work);
+
+       snprintf(name, sizeof(name), "port%u", port->id);
+       port->debugfs_topdir = debugfs_create_dir(name, dev->debugfs_topdir);
+       debugfs_create_file("destroy", 0200, port->debugfs_topdir, port,
+                           &wwan_hwsim_debugfs_portdestroy_fops);
+
+       return port;
+
+err_free_port:
+       kfree(port);
+
+       return ERR_PTR(err);
+}
+
+static void wwan_hwsim_port_del(struct wwan_hwsim_port *port)
+{
+       debugfs_remove(port->debugfs_topdir);
+
+       /* Make sure that there is no pending deletion work */
+       if (current_work() != &port->del_work)
+               cancel_work_sync(&port->del_work);
+
+       wwan_remove_port(port->wwan);
+       kfree(port);
+}
+
+static void wwan_hwsim_port_del_work(struct work_struct *work)
+{
+       struct wwan_hwsim_port *port =
+                               container_of(work, typeof(*port), del_work);
+       struct wwan_hwsim_dev *dev = port->dev;
+
+       spin_lock(&dev->ports_lock);
+       if (list_empty(&port->list)) {
+               /* Someone else deleting port at the moment */
+               spin_unlock(&dev->ports_lock);
+               return;
+       }
+       list_del_init(&port->list);
+       spin_unlock(&dev->ports_lock);
+
+       wwan_hwsim_port_del(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;
+
+       INIT_WORK(&dev->del_work, wwan_hwsim_dev_del_work);
+
+       dev->debugfs_topdir = debugfs_create_dir(dev_name(&dev->dev),
+                                                wwan_hwsim_debugfs_topdir);
+       debugfs_create_file("destroy", 0200, dev->debugfs_topdir, dev,
+                           &wwan_hwsim_debugfs_devdestroy_fops);
+       dev->debugfs_portcreate =
+               debugfs_create_file("portcreate", 0200,
+                                   dev->debugfs_topdir, dev,
+                                   &wwan_hwsim_debugfs_portcreate_fops);
+
+       return dev;
+
+err_free_dev:
+       kfree(dev);
+
+       return ERR_PTR(err);
+}
+
+static void wwan_hwsim_dev_del(struct wwan_hwsim_dev *dev)
+{
+       debugfs_remove(dev->debugfs_portcreate);        /* Avoid new ports */
+
+       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_init(&port->list);
+               spin_unlock(&dev->ports_lock);
+               wwan_hwsim_port_del(port);
+               spin_lock(&dev->ports_lock);
+       }
+       spin_unlock(&dev->ports_lock);
+
+       debugfs_remove(dev->debugfs_topdir);
+
+       /* Make sure that there is no pending deletion work */
+       if (current_work() != &dev->del_work)
+               cancel_work_sync(&dev->del_work);
+
+       device_unregister(&dev->dev);
+       /* Memory will be freed in the device release callback */
+}
+
+static void wwan_hwsim_dev_del_work(struct work_struct *work)
+{
+       struct wwan_hwsim_dev *dev = container_of(work, typeof(*dev), del_work);
+
+       spin_lock(&wwan_hwsim_devs_lock);
+       if (list_empty(&dev->list)) {
+               /* Someone else deleting device at the moment */
+               spin_unlock(&wwan_hwsim_devs_lock);
+               return;
+       }
+       list_del_init(&dev->list);
+       spin_unlock(&wwan_hwsim_devs_lock);
+
+       wwan_hwsim_dev_del(dev);
+}
+
+static ssize_t wwan_hwsim_debugfs_portdestroy_write(struct file *file,
+                                                   const char __user *usrbuf,
+                                                   size_t count, loff_t *ppos)
+{
+       struct wwan_hwsim_port *port = file->private_data;
+
+       /* We can not delete port here since it will cause a deadlock due to
+        * waiting this callback to finish in the debugfs_remove() call. So,
+        * use workqueue.
+        */
+       schedule_work(&port->del_work);
+
+       return count;
+}
+
+static const struct file_operations wwan_hwsim_debugfs_portdestroy_fops = {
+       .write = wwan_hwsim_debugfs_portdestroy_write,
+       .open = simple_open,
+       .llseek = noop_llseek,
+};
+
+static ssize_t wwan_hwsim_debugfs_portcreate_write(struct file *file,
+                                                  const char __user *usrbuf,
+                                                  size_t count, loff_t *ppos)
+{
+       struct wwan_hwsim_dev *dev = file->private_data;
+       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 count;
+}
+
+static const struct file_operations wwan_hwsim_debugfs_portcreate_fops = {
+       .write = wwan_hwsim_debugfs_portcreate_write,
+       .open = simple_open,
+       .llseek = noop_llseek,
+};
+
+static ssize_t wwan_hwsim_debugfs_devdestroy_write(struct file *file,
+                                                  const char __user *usrbuf,
+                                                  size_t count, loff_t *ppos)
+{
+       struct wwan_hwsim_dev *dev = file->private_data;
+
+       /* We can not delete device here since it will cause a deadlock due to
+        * waiting this callback to finish in the debugfs_remove() call. So,
+        * use workqueue.
+        */
+       schedule_work(&dev->del_work);
+
+       return count;
+}
+
+static const struct file_operations wwan_hwsim_debugfs_devdestroy_fops = {
+       .write = wwan_hwsim_debugfs_devdestroy_write,
+       .open = simple_open,
+       .llseek = noop_llseek,
+};
+
+static ssize_t wwan_hwsim_debugfs_devcreate_write(struct file *file,
+                                                 const char __user *usrbuf,
+                                                 size_t count, loff_t *ppos)
+{
+       struct wwan_hwsim_dev *dev;
+
+       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);
+
+       return count;
+}
+
+static const struct file_operations wwan_hwsim_debugfs_devcreate_fops = {
+       .write = wwan_hwsim_debugfs_devcreate_write,
+       .open = simple_open,
+       .llseek = noop_llseek,
+};
+
+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_init(&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);
+
+       wwan_hwsim_debugfs_topdir = debugfs_create_dir("wwan_hwsim", NULL);
+       wwan_hwsim_debugfs_devcreate =
+                       debugfs_create_file("devcreate", 0200,
+                                           wwan_hwsim_debugfs_topdir, NULL,
+                                           &wwan_hwsim_debugfs_devcreate_fops);
+
+       err = wwan_hwsim_init_devs();
+       if (err)
+               goto err_clean_devs;
+
+       return 0;
+
+err_clean_devs:
+       wwan_hwsim_free_devs();
+       debugfs_remove(wwan_hwsim_debugfs_topdir);
+       class_destroy(wwan_hwsim_class);
+
+       return err;
+}
+
+static void __exit wwan_hwsim_exit(void)
+{
+       debugfs_remove(wwan_hwsim_debugfs_devcreate);   /* Avoid new devs */
+       wwan_hwsim_free_devs();
+       flush_scheduled_work();         /* Wait deletion works completion */
+       debugfs_remove(wwan_hwsim_debugfs_topdir);
+       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");
index 7216c11..fa33cc1 100644 (file)
  * @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;