Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */
0003 
0004 #include <linux/err.h>
0005 #include <linux/errno.h>
0006 #include <linux/debugfs.h>
0007 #include <linux/fs.h>
0008 #include <linux/init.h>
0009 #include <linux/idr.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/poll.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/slab.h>
0015 #include <linux/types.h>
0016 #include <linux/termios.h>
0017 #include <linux/wwan.h>
0018 #include <net/rtnetlink.h>
0019 #include <uapi/linux/wwan.h>
0020 
0021 /* Maximum number of minors in use */
0022 #define WWAN_MAX_MINORS     (1 << MINORBITS)
0023 
0024 static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
0025 static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
0026 static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */
0027 static struct class *wwan_class;
0028 static int wwan_major;
0029 static struct dentry *wwan_debugfs_dir;
0030 
0031 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
0032 #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
0033 
0034 /* WWAN port flags */
0035 #define WWAN_PORT_TX_OFF    0
0036 
0037 /**
0038  * struct wwan_device - The structure that defines a WWAN device
0039  *
0040  * @id: WWAN device unique ID.
0041  * @dev: Underlying device.
0042  * @port_id: Current available port ID to pick.
0043  * @ops: wwan device ops
0044  * @ops_ctxt: context to pass to ops
0045  * @debugfs_dir:  WWAN device debugfs dir
0046  */
0047 struct wwan_device {
0048     unsigned int id;
0049     struct device dev;
0050     atomic_t port_id;
0051     const struct wwan_ops *ops;
0052     void *ops_ctxt;
0053 #ifdef CONFIG_WWAN_DEBUGFS
0054     struct dentry *debugfs_dir;
0055 #endif
0056 };
0057 
0058 /**
0059  * struct wwan_port - The structure that defines a WWAN port
0060  * @type: Port type
0061  * @start_count: Port start counter
0062  * @flags: Store port state and capabilities
0063  * @ops: Pointer to WWAN port operations
0064  * @ops_lock: Protect port ops
0065  * @dev: Underlying device
0066  * @rxq: Buffer inbound queue
0067  * @waitqueue: The waitqueue for port fops (read/write/poll)
0068  * @data_lock: Port specific data access serialization
0069  * @at_data: AT port specific data
0070  */
0071 struct wwan_port {
0072     enum wwan_port_type type;
0073     unsigned int start_count;
0074     unsigned long flags;
0075     const struct wwan_port_ops *ops;
0076     struct mutex ops_lock; /* Serialize ops + protect against removal */
0077     struct device dev;
0078     struct sk_buff_head rxq;
0079     wait_queue_head_t waitqueue;
0080     struct mutex data_lock; /* Port specific data access serialization */
0081     union {
0082         struct {
0083             struct ktermios termios;
0084             int mdmbits;
0085         } at_data;
0086     };
0087 };
0088 
0089 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
0090 {
0091     struct wwan_device *wwan = to_wwan_dev(dev);
0092 
0093     return sprintf(buf, "%d\n", wwan->id);
0094 }
0095 static DEVICE_ATTR_RO(index);
0096 
0097 static struct attribute *wwan_dev_attrs[] = {
0098     &dev_attr_index.attr,
0099     NULL,
0100 };
0101 ATTRIBUTE_GROUPS(wwan_dev);
0102 
0103 static void wwan_dev_destroy(struct device *dev)
0104 {
0105     struct wwan_device *wwandev = to_wwan_dev(dev);
0106 
0107     ida_free(&wwan_dev_ids, wwandev->id);
0108     kfree(wwandev);
0109 }
0110 
0111 static const struct device_type wwan_dev_type = {
0112     .name    = "wwan_dev",
0113     .release = wwan_dev_destroy,
0114     .groups = wwan_dev_groups,
0115 };
0116 
0117 static int wwan_dev_parent_match(struct device *dev, const void *parent)
0118 {
0119     return (dev->type == &wwan_dev_type &&
0120         (dev->parent == parent || dev == parent));
0121 }
0122 
0123 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
0124 {
0125     struct device *dev;
0126 
0127     dev = class_find_device(wwan_class, NULL, parent, wwan_dev_parent_match);
0128     if (!dev)
0129         return ERR_PTR(-ENODEV);
0130 
0131     return to_wwan_dev(dev);
0132 }
0133 
0134 static int wwan_dev_name_match(struct device *dev, const void *name)
0135 {
0136     return dev->type == &wwan_dev_type &&
0137            strcmp(dev_name(dev), name) == 0;
0138 }
0139 
0140 static struct wwan_device *wwan_dev_get_by_name(const char *name)
0141 {
0142     struct device *dev;
0143 
0144     dev = class_find_device(wwan_class, NULL, name, wwan_dev_name_match);
0145     if (!dev)
0146         return ERR_PTR(-ENODEV);
0147 
0148     return to_wwan_dev(dev);
0149 }
0150 
0151 #ifdef CONFIG_WWAN_DEBUGFS
0152 struct dentry *wwan_get_debugfs_dir(struct device *parent)
0153 {
0154     struct wwan_device *wwandev;
0155 
0156     wwandev = wwan_dev_get_by_parent(parent);
0157     if (IS_ERR(wwandev))
0158         return ERR_CAST(wwandev);
0159 
0160     return wwandev->debugfs_dir;
0161 }
0162 EXPORT_SYMBOL_GPL(wwan_get_debugfs_dir);
0163 
0164 static int wwan_dev_debugfs_match(struct device *dev, const void *dir)
0165 {
0166     struct wwan_device *wwandev;
0167 
0168     if (dev->type != &wwan_dev_type)
0169         return 0;
0170 
0171     wwandev = to_wwan_dev(dev);
0172 
0173     return wwandev->debugfs_dir == dir;
0174 }
0175 
0176 static struct wwan_device *wwan_dev_get_by_debugfs(struct dentry *dir)
0177 {
0178     struct device *dev;
0179 
0180     dev = class_find_device(wwan_class, NULL, dir, wwan_dev_debugfs_match);
0181     if (!dev)
0182         return ERR_PTR(-ENODEV);
0183 
0184     return to_wwan_dev(dev);
0185 }
0186 
0187 void wwan_put_debugfs_dir(struct dentry *dir)
0188 {
0189     struct wwan_device *wwandev = wwan_dev_get_by_debugfs(dir);
0190 
0191     if (WARN_ON(IS_ERR(wwandev)))
0192         return;
0193 
0194     /* wwan_dev_get_by_debugfs() also got a reference */
0195     put_device(&wwandev->dev);
0196     put_device(&wwandev->dev);
0197 }
0198 EXPORT_SYMBOL_GPL(wwan_put_debugfs_dir);
0199 #endif
0200 
0201 /* This function allocates and registers a new WWAN device OR if a WWAN device
0202  * already exist for the given parent, it gets a reference and return it.
0203  * This function is not exported (for now), it is called indirectly via
0204  * wwan_create_port().
0205  */
0206 static struct wwan_device *wwan_create_dev(struct device *parent)
0207 {
0208     struct wwan_device *wwandev;
0209     int err, id;
0210 
0211     /* The 'find-alloc-register' operation must be protected against
0212      * concurrent execution, a WWAN device is possibly shared between
0213      * multiple callers or concurrently unregistered from wwan_remove_dev().
0214      */
0215     mutex_lock(&wwan_register_lock);
0216 
0217     /* If wwandev already exists, return it */
0218     wwandev = wwan_dev_get_by_parent(parent);
0219     if (!IS_ERR(wwandev))
0220         goto done_unlock;
0221 
0222     id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
0223     if (id < 0) {
0224         wwandev = ERR_PTR(id);
0225         goto done_unlock;
0226     }
0227 
0228     wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL);
0229     if (!wwandev) {
0230         wwandev = ERR_PTR(-ENOMEM);
0231         ida_free(&wwan_dev_ids, id);
0232         goto done_unlock;
0233     }
0234 
0235     wwandev->dev.parent = parent;
0236     wwandev->dev.class = wwan_class;
0237     wwandev->dev.type = &wwan_dev_type;
0238     wwandev->id = id;
0239     dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
0240 
0241     err = device_register(&wwandev->dev);
0242     if (err) {
0243         put_device(&wwandev->dev);
0244         wwandev = ERR_PTR(err);
0245         goto done_unlock;
0246     }
0247 
0248 #ifdef CONFIG_WWAN_DEBUGFS
0249     wwandev->debugfs_dir =
0250             debugfs_create_dir(kobject_name(&wwandev->dev.kobj),
0251                        wwan_debugfs_dir);
0252 #endif
0253 
0254 done_unlock:
0255     mutex_unlock(&wwan_register_lock);
0256 
0257     return wwandev;
0258 }
0259 
0260 static int is_wwan_child(struct device *dev, void *data)
0261 {
0262     return dev->class == wwan_class;
0263 }
0264 
0265 static void wwan_remove_dev(struct wwan_device *wwandev)
0266 {
0267     int ret;
0268 
0269     /* Prevent concurrent picking from wwan_create_dev */
0270     mutex_lock(&wwan_register_lock);
0271 
0272     /* WWAN device is created and registered (get+add) along with its first
0273      * child port, and subsequent port registrations only grab a reference
0274      * (get). The WWAN device must then be unregistered (del+put) along with
0275      * its last port, and reference simply dropped (put) otherwise. In the
0276      * same fashion, we must not unregister it when the ops are still there.
0277      */
0278     if (wwandev->ops)
0279         ret = 1;
0280     else
0281         ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child);
0282 
0283     if (!ret) {
0284 #ifdef CONFIG_WWAN_DEBUGFS
0285         debugfs_remove_recursive(wwandev->debugfs_dir);
0286 #endif
0287         device_unregister(&wwandev->dev);
0288     } else {
0289         put_device(&wwandev->dev);
0290     }
0291 
0292     mutex_unlock(&wwan_register_lock);
0293 }
0294 
0295 /* ------- WWAN port management ------- */
0296 
0297 static const struct {
0298     const char * const name;    /* Port type name */
0299     const char * const devsuf;  /* Port devce name suffix */
0300 } wwan_port_types[WWAN_PORT_MAX + 1] = {
0301     [WWAN_PORT_AT] = {
0302         .name = "AT",
0303         .devsuf = "at",
0304     },
0305     [WWAN_PORT_MBIM] = {
0306         .name = "MBIM",
0307         .devsuf = "mbim",
0308     },
0309     [WWAN_PORT_QMI] = {
0310         .name = "QMI",
0311         .devsuf = "qmi",
0312     },
0313     [WWAN_PORT_QCDM] = {
0314         .name = "QCDM",
0315         .devsuf = "qcdm",
0316     },
0317     [WWAN_PORT_FIREHOSE] = {
0318         .name = "FIREHOSE",
0319         .devsuf = "firehose",
0320     },
0321 };
0322 
0323 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
0324              char *buf)
0325 {
0326     struct wwan_port *port = to_wwan_port(dev);
0327 
0328     return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
0329 }
0330 static DEVICE_ATTR_RO(type);
0331 
0332 static struct attribute *wwan_port_attrs[] = {
0333     &dev_attr_type.attr,
0334     NULL,
0335 };
0336 ATTRIBUTE_GROUPS(wwan_port);
0337 
0338 static void wwan_port_destroy(struct device *dev)
0339 {
0340     struct wwan_port *port = to_wwan_port(dev);
0341 
0342     ida_free(&minors, MINOR(port->dev.devt));
0343     mutex_destroy(&port->data_lock);
0344     mutex_destroy(&port->ops_lock);
0345     kfree(port);
0346 }
0347 
0348 static const struct device_type wwan_port_dev_type = {
0349     .name = "wwan_port",
0350     .release = wwan_port_destroy,
0351     .groups = wwan_port_groups,
0352 };
0353 
0354 static int wwan_port_minor_match(struct device *dev, const void *minor)
0355 {
0356     return (dev->type == &wwan_port_dev_type &&
0357         MINOR(dev->devt) == *(unsigned int *)minor);
0358 }
0359 
0360 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
0361 {
0362     struct device *dev;
0363 
0364     dev = class_find_device(wwan_class, NULL, &minor, wwan_port_minor_match);
0365     if (!dev)
0366         return ERR_PTR(-ENODEV);
0367 
0368     return to_wwan_port(dev);
0369 }
0370 
0371 /* Allocate and set unique name based on passed format
0372  *
0373  * Name allocation approach is highly inspired by the __dev_alloc_name()
0374  * function.
0375  *
0376  * To avoid names collision, the caller must prevent the new port device
0377  * registration as well as concurrent invocation of this function.
0378  */
0379 static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
0380 {
0381     struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
0382     const unsigned int max_ports = PAGE_SIZE * 8;
0383     struct class_dev_iter iter;
0384     unsigned long *idmap;
0385     struct device *dev;
0386     char buf[0x20];
0387     int id;
0388 
0389     idmap = (unsigned long *)get_zeroed_page(GFP_KERNEL);
0390     if (!idmap)
0391         return -ENOMEM;
0392 
0393     /* Collect ids of same name format ports */
0394     class_dev_iter_init(&iter, wwan_class, NULL, &wwan_port_dev_type);
0395     while ((dev = class_dev_iter_next(&iter))) {
0396         if (dev->parent != &wwandev->dev)
0397             continue;
0398         if (sscanf(dev_name(dev), fmt, &id) != 1)
0399             continue;
0400         if (id < 0 || id >= max_ports)
0401             continue;
0402         set_bit(id, idmap);
0403     }
0404     class_dev_iter_exit(&iter);
0405 
0406     /* Allocate unique id */
0407     id = find_first_zero_bit(idmap, max_ports);
0408     free_page((unsigned long)idmap);
0409 
0410     snprintf(buf, sizeof(buf), fmt, id);    /* Name generation */
0411 
0412     dev = device_find_child_by_name(&wwandev->dev, buf);
0413     if (dev) {
0414         put_device(dev);
0415         return -ENFILE;
0416     }
0417 
0418     return dev_set_name(&port->dev, buf);
0419 }
0420 
0421 struct wwan_port *wwan_create_port(struct device *parent,
0422                    enum wwan_port_type type,
0423                    const struct wwan_port_ops *ops,
0424                    void *drvdata)
0425 {
0426     struct wwan_device *wwandev;
0427     struct wwan_port *port;
0428     char namefmt[0x20];
0429     int minor, err;
0430 
0431     if (type > WWAN_PORT_MAX || !ops)
0432         return ERR_PTR(-EINVAL);
0433 
0434     /* A port is always a child of a WWAN device, retrieve (allocate or
0435      * pick) the WWAN device based on the provided parent device.
0436      */
0437     wwandev = wwan_create_dev(parent);
0438     if (IS_ERR(wwandev))
0439         return ERR_CAST(wwandev);
0440 
0441     /* A port is exposed as character device, get a minor */
0442     minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
0443     if (minor < 0) {
0444         err = minor;
0445         goto error_wwandev_remove;
0446     }
0447 
0448     port = kzalloc(sizeof(*port), GFP_KERNEL);
0449     if (!port) {
0450         err = -ENOMEM;
0451         ida_free(&minors, minor);
0452         goto error_wwandev_remove;
0453     }
0454 
0455     port->type = type;
0456     port->ops = ops;
0457     mutex_init(&port->ops_lock);
0458     skb_queue_head_init(&port->rxq);
0459     init_waitqueue_head(&port->waitqueue);
0460     mutex_init(&port->data_lock);
0461 
0462     port->dev.parent = &wwandev->dev;
0463     port->dev.class = wwan_class;
0464     port->dev.type = &wwan_port_dev_type;
0465     port->dev.devt = MKDEV(wwan_major, minor);
0466     dev_set_drvdata(&port->dev, drvdata);
0467 
0468     /* allocate unique name based on wwan device id, port type and number */
0469     snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
0470          wwan_port_types[port->type].devsuf);
0471 
0472     /* Serialize ports registration */
0473     mutex_lock(&wwan_register_lock);
0474 
0475     __wwan_port_dev_assign_name(port, namefmt);
0476     err = device_register(&port->dev);
0477 
0478     mutex_unlock(&wwan_register_lock);
0479 
0480     if (err)
0481         goto error_put_device;
0482 
0483     return port;
0484 
0485 error_put_device:
0486     put_device(&port->dev);
0487 error_wwandev_remove:
0488     wwan_remove_dev(wwandev);
0489 
0490     return ERR_PTR(err);
0491 }
0492 EXPORT_SYMBOL_GPL(wwan_create_port);
0493 
0494 void wwan_remove_port(struct wwan_port *port)
0495 {
0496     struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
0497 
0498     mutex_lock(&port->ops_lock);
0499     if (port->start_count)
0500         port->ops->stop(port);
0501     port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
0502     mutex_unlock(&port->ops_lock);
0503 
0504     wake_up_interruptible(&port->waitqueue);
0505 
0506     skb_queue_purge(&port->rxq);
0507     dev_set_drvdata(&port->dev, NULL);
0508     device_unregister(&port->dev);
0509 
0510     /* Release related wwan device */
0511     wwan_remove_dev(wwandev);
0512 }
0513 EXPORT_SYMBOL_GPL(wwan_remove_port);
0514 
0515 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
0516 {
0517     skb_queue_tail(&port->rxq, skb);
0518     wake_up_interruptible(&port->waitqueue);
0519 }
0520 EXPORT_SYMBOL_GPL(wwan_port_rx);
0521 
0522 void wwan_port_txon(struct wwan_port *port)
0523 {
0524     clear_bit(WWAN_PORT_TX_OFF, &port->flags);
0525     wake_up_interruptible(&port->waitqueue);
0526 }
0527 EXPORT_SYMBOL_GPL(wwan_port_txon);
0528 
0529 void wwan_port_txoff(struct wwan_port *port)
0530 {
0531     set_bit(WWAN_PORT_TX_OFF, &port->flags);
0532 }
0533 EXPORT_SYMBOL_GPL(wwan_port_txoff);
0534 
0535 void *wwan_port_get_drvdata(struct wwan_port *port)
0536 {
0537     return dev_get_drvdata(&port->dev);
0538 }
0539 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
0540 
0541 static int wwan_port_op_start(struct wwan_port *port)
0542 {
0543     int ret = 0;
0544 
0545     mutex_lock(&port->ops_lock);
0546     if (!port->ops) { /* Port got unplugged */
0547         ret = -ENODEV;
0548         goto out_unlock;
0549     }
0550 
0551     /* If port is already started, don't start again */
0552     if (!port->start_count)
0553         ret = port->ops->start(port);
0554 
0555     if (!ret)
0556         port->start_count++;
0557 
0558 out_unlock:
0559     mutex_unlock(&port->ops_lock);
0560 
0561     return ret;
0562 }
0563 
0564 static void wwan_port_op_stop(struct wwan_port *port)
0565 {
0566     mutex_lock(&port->ops_lock);
0567     port->start_count--;
0568     if (!port->start_count) {
0569         if (port->ops)
0570             port->ops->stop(port);
0571         skb_queue_purge(&port->rxq);
0572     }
0573     mutex_unlock(&port->ops_lock);
0574 }
0575 
0576 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
0577                bool nonblock)
0578 {
0579     int ret;
0580 
0581     mutex_lock(&port->ops_lock);
0582     if (!port->ops) { /* Port got unplugged */
0583         ret = -ENODEV;
0584         goto out_unlock;
0585     }
0586 
0587     if (nonblock || !port->ops->tx_blocking)
0588         ret = port->ops->tx(port, skb);
0589     else
0590         ret = port->ops->tx_blocking(port, skb);
0591 
0592 out_unlock:
0593     mutex_unlock(&port->ops_lock);
0594 
0595     return ret;
0596 }
0597 
0598 static bool is_read_blocked(struct wwan_port *port)
0599 {
0600     return skb_queue_empty(&port->rxq) && port->ops;
0601 }
0602 
0603 static bool is_write_blocked(struct wwan_port *port)
0604 {
0605     return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
0606 }
0607 
0608 static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
0609 {
0610     if (!is_read_blocked(port))
0611         return 0;
0612 
0613     if (nonblock)
0614         return -EAGAIN;
0615 
0616     if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
0617         return -ERESTARTSYS;
0618 
0619     return 0;
0620 }
0621 
0622 static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
0623 {
0624     if (!is_write_blocked(port))
0625         return 0;
0626 
0627     if (nonblock)
0628         return -EAGAIN;
0629 
0630     if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
0631         return -ERESTARTSYS;
0632 
0633     return 0;
0634 }
0635 
0636 static int wwan_port_fops_open(struct inode *inode, struct file *file)
0637 {
0638     struct wwan_port *port;
0639     int err = 0;
0640 
0641     port = wwan_port_get_by_minor(iminor(inode));
0642     if (IS_ERR(port))
0643         return PTR_ERR(port);
0644 
0645     file->private_data = port;
0646     stream_open(inode, file);
0647 
0648     err = wwan_port_op_start(port);
0649     if (err)
0650         put_device(&port->dev);
0651 
0652     return err;
0653 }
0654 
0655 static int wwan_port_fops_release(struct inode *inode, struct file *filp)
0656 {
0657     struct wwan_port *port = filp->private_data;
0658 
0659     wwan_port_op_stop(port);
0660     put_device(&port->dev);
0661 
0662     return 0;
0663 }
0664 
0665 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
0666                    size_t count, loff_t *ppos)
0667 {
0668     struct wwan_port *port = filp->private_data;
0669     struct sk_buff *skb;
0670     size_t copied;
0671     int ret;
0672 
0673     ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
0674     if (ret)
0675         return ret;
0676 
0677     skb = skb_dequeue(&port->rxq);
0678     if (!skb)
0679         return -EIO;
0680 
0681     copied = min_t(size_t, count, skb->len);
0682     if (copy_to_user(buf, skb->data, copied)) {
0683         kfree_skb(skb);
0684         return -EFAULT;
0685     }
0686     skb_pull(skb, copied);
0687 
0688     /* skb is not fully consumed, keep it in the queue */
0689     if (skb->len)
0690         skb_queue_head(&port->rxq, skb);
0691     else
0692         consume_skb(skb);
0693 
0694     return copied;
0695 }
0696 
0697 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
0698                     size_t count, loff_t *offp)
0699 {
0700     struct wwan_port *port = filp->private_data;
0701     struct sk_buff *skb;
0702     int ret;
0703 
0704     ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
0705     if (ret)
0706         return ret;
0707 
0708     skb = alloc_skb(count, GFP_KERNEL);
0709     if (!skb)
0710         return -ENOMEM;
0711 
0712     if (copy_from_user(skb_put(skb, count), buf, count)) {
0713         kfree_skb(skb);
0714         return -EFAULT;
0715     }
0716 
0717     ret = wwan_port_op_tx(port, skb, !!(filp->f_flags & O_NONBLOCK));
0718     if (ret) {
0719         kfree_skb(skb);
0720         return ret;
0721     }
0722 
0723     return count;
0724 }
0725 
0726 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
0727 {
0728     struct wwan_port *port = filp->private_data;
0729     __poll_t mask = 0;
0730 
0731     poll_wait(filp, &port->waitqueue, wait);
0732 
0733     mutex_lock(&port->ops_lock);
0734     if (port->ops && port->ops->tx_poll)
0735         mask |= port->ops->tx_poll(port, filp, wait);
0736     else if (!is_write_blocked(port))
0737         mask |= EPOLLOUT | EPOLLWRNORM;
0738     if (!is_read_blocked(port))
0739         mask |= EPOLLIN | EPOLLRDNORM;
0740     if (!port->ops)
0741         mask |= EPOLLHUP | EPOLLERR;
0742     mutex_unlock(&port->ops_lock);
0743 
0744     return mask;
0745 }
0746 
0747 /* Implements minimalistic stub terminal IOCTLs support */
0748 static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
0749                     unsigned long arg)
0750 {
0751     int ret = 0;
0752 
0753     mutex_lock(&port->data_lock);
0754 
0755     switch (cmd) {
0756     case TCFLSH:
0757         break;
0758 
0759     case TCGETS:
0760         if (copy_to_user((void __user *)arg, &port->at_data.termios,
0761                  sizeof(struct termios)))
0762             ret = -EFAULT;
0763         break;
0764 
0765     case TCSETS:
0766     case TCSETSW:
0767     case TCSETSF:
0768         if (copy_from_user(&port->at_data.termios, (void __user *)arg,
0769                    sizeof(struct termios)))
0770             ret = -EFAULT;
0771         break;
0772 
0773 #ifdef TCGETS2
0774     case TCGETS2:
0775         if (copy_to_user((void __user *)arg, &port->at_data.termios,
0776                  sizeof(struct termios2)))
0777             ret = -EFAULT;
0778         break;
0779 
0780     case TCSETS2:
0781     case TCSETSW2:
0782     case TCSETSF2:
0783         if (copy_from_user(&port->at_data.termios, (void __user *)arg,
0784                    sizeof(struct termios2)))
0785             ret = -EFAULT;
0786         break;
0787 #endif
0788 
0789     case TIOCMGET:
0790         ret = put_user(port->at_data.mdmbits, (int __user *)arg);
0791         break;
0792 
0793     case TIOCMSET:
0794     case TIOCMBIC:
0795     case TIOCMBIS: {
0796         int mdmbits;
0797 
0798         if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
0799             ret = -EFAULT;
0800             break;
0801         }
0802         if (cmd == TIOCMBIC)
0803             port->at_data.mdmbits &= ~mdmbits;
0804         else if (cmd == TIOCMBIS)
0805             port->at_data.mdmbits |= mdmbits;
0806         else
0807             port->at_data.mdmbits = mdmbits;
0808         break;
0809     }
0810 
0811     default:
0812         ret = -ENOIOCTLCMD;
0813     }
0814 
0815     mutex_unlock(&port->data_lock);
0816 
0817     return ret;
0818 }
0819 
0820 static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
0821                  unsigned long arg)
0822 {
0823     struct wwan_port *port = filp->private_data;
0824     int res;
0825 
0826     if (port->type == WWAN_PORT_AT) {   /* AT port specific IOCTLs */
0827         res = wwan_port_fops_at_ioctl(port, cmd, arg);
0828         if (res != -ENOIOCTLCMD)
0829             return res;
0830     }
0831 
0832     switch (cmd) {
0833     case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
0834         unsigned long flags;
0835         struct sk_buff *skb;
0836         int amount = 0;
0837 
0838         spin_lock_irqsave(&port->rxq.lock, flags);
0839         skb_queue_walk(&port->rxq, skb)
0840             amount += skb->len;
0841         spin_unlock_irqrestore(&port->rxq.lock, flags);
0842 
0843         return put_user(amount, (int __user *)arg);
0844     }
0845 
0846     default:
0847         return -ENOIOCTLCMD;
0848     }
0849 }
0850 
0851 static const struct file_operations wwan_port_fops = {
0852     .owner = THIS_MODULE,
0853     .open = wwan_port_fops_open,
0854     .release = wwan_port_fops_release,
0855     .read = wwan_port_fops_read,
0856     .write = wwan_port_fops_write,
0857     .poll = wwan_port_fops_poll,
0858     .unlocked_ioctl = wwan_port_fops_ioctl,
0859 #ifdef CONFIG_COMPAT
0860     .compat_ioctl = compat_ptr_ioctl,
0861 #endif
0862     .llseek = noop_llseek,
0863 };
0864 
0865 static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
0866                   struct netlink_ext_ack *extack)
0867 {
0868     if (!data)
0869         return -EINVAL;
0870 
0871     if (!tb[IFLA_PARENT_DEV_NAME])
0872         return -EINVAL;
0873 
0874     if (!data[IFLA_WWAN_LINK_ID])
0875         return -EINVAL;
0876 
0877     return 0;
0878 }
0879 
0880 static struct device_type wwan_type = { .name = "wwan" };
0881 
0882 static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],
0883                       const char *ifname,
0884                       unsigned char name_assign_type,
0885                       unsigned int num_tx_queues,
0886                       unsigned int num_rx_queues)
0887 {
0888     const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]);
0889     struct wwan_device *wwandev = wwan_dev_get_by_name(devname);
0890     struct net_device *dev;
0891     unsigned int priv_size;
0892 
0893     if (IS_ERR(wwandev))
0894         return ERR_CAST(wwandev);
0895 
0896     /* only supported if ops were registered (not just ports) */
0897     if (!wwandev->ops) {
0898         dev = ERR_PTR(-EOPNOTSUPP);
0899         goto out;
0900     }
0901 
0902     priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size;
0903     dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type,
0904                    wwandev->ops->setup, num_tx_queues, num_rx_queues);
0905 
0906     if (dev) {
0907         SET_NETDEV_DEV(dev, &wwandev->dev);
0908         SET_NETDEV_DEVTYPE(dev, &wwan_type);
0909     }
0910 
0911 out:
0912     /* release the reference */
0913     put_device(&wwandev->dev);
0914     return dev;
0915 }
0916 
0917 static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev,
0918                  struct nlattr *tb[], struct nlattr *data[],
0919                  struct netlink_ext_ack *extack)
0920 {
0921     struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
0922     u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]);
0923     struct wwan_netdev_priv *priv = netdev_priv(dev);
0924     int ret;
0925 
0926     if (IS_ERR(wwandev))
0927         return PTR_ERR(wwandev);
0928 
0929     /* shouldn't have a netdev (left) with us as parent so WARN */
0930     if (WARN_ON(!wwandev->ops)) {
0931         ret = -EOPNOTSUPP;
0932         goto out;
0933     }
0934 
0935     priv->link_id = link_id;
0936     if (wwandev->ops->newlink)
0937         ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev,
0938                         link_id, extack);
0939     else
0940         ret = register_netdevice(dev);
0941 
0942 out:
0943     /* release the reference */
0944     put_device(&wwandev->dev);
0945     return ret;
0946 }
0947 
0948 static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head)
0949 {
0950     struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
0951 
0952     if (IS_ERR(wwandev))
0953         return;
0954 
0955     /* shouldn't have a netdev (left) with us as parent so WARN */
0956     if (WARN_ON(!wwandev->ops))
0957         goto out;
0958 
0959     if (wwandev->ops->dellink)
0960         wwandev->ops->dellink(wwandev->ops_ctxt, dev, head);
0961     else
0962         unregister_netdevice_queue(dev, head);
0963 
0964 out:
0965     /* release the reference */
0966     put_device(&wwandev->dev);
0967 }
0968 
0969 static size_t wwan_rtnl_get_size(const struct net_device *dev)
0970 {
0971     return
0972         nla_total_size(4) + /* IFLA_WWAN_LINK_ID */
0973         0;
0974 }
0975 
0976 static int wwan_rtnl_fill_info(struct sk_buff *skb,
0977                    const struct net_device *dev)
0978 {
0979     struct wwan_netdev_priv *priv = netdev_priv(dev);
0980 
0981     if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id))
0982         goto nla_put_failure;
0983 
0984     return 0;
0985 
0986 nla_put_failure:
0987     return -EMSGSIZE;
0988 }
0989 
0990 static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = {
0991     [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 },
0992 };
0993 
0994 static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = {
0995     .kind = "wwan",
0996     .maxtype = __IFLA_WWAN_MAX,
0997     .alloc = wwan_rtnl_alloc,
0998     .validate = wwan_rtnl_validate,
0999     .newlink = wwan_rtnl_newlink,
1000     .dellink = wwan_rtnl_dellink,
1001     .get_size = wwan_rtnl_get_size,
1002     .fill_info = wwan_rtnl_fill_info,
1003     .policy = wwan_rtnl_policy,
1004 };
1005 
1006 static void wwan_create_default_link(struct wwan_device *wwandev,
1007                      u32 def_link_id)
1008 {
1009     struct nlattr *tb[IFLA_MAX + 1], *linkinfo[IFLA_INFO_MAX + 1];
1010     struct nlattr *data[IFLA_WWAN_MAX + 1];
1011     struct net_device *dev;
1012     struct nlmsghdr *nlh;
1013     struct sk_buff *msg;
1014 
1015     /* Forge attributes required to create a WWAN netdev. We first
1016      * build a netlink message and then parse it. This looks
1017      * odd, but such approach is less error prone.
1018      */
1019     msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1020     if (WARN_ON(!msg))
1021         return;
1022     nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0);
1023     if (WARN_ON(!nlh))
1024         goto free_attrs;
1025 
1026     if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev)))
1027         goto free_attrs;
1028     tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO);
1029     if (!tb[IFLA_LINKINFO])
1030         goto free_attrs;
1031     linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA);
1032     if (!linkinfo[IFLA_INFO_DATA])
1033         goto free_attrs;
1034     if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id))
1035         goto free_attrs;
1036     nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]);
1037     nla_nest_end(msg, tb[IFLA_LINKINFO]);
1038 
1039     nlmsg_end(msg, nlh);
1040 
1041     /* The next three parsing calls can not fail */
1042     nlmsg_parse_deprecated(nlh, 0, tb, IFLA_MAX, NULL, NULL);
1043     nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
1044                     NULL, NULL);
1045     nla_parse_nested_deprecated(data, IFLA_WWAN_MAX,
1046                     linkinfo[IFLA_INFO_DATA], NULL, NULL);
1047 
1048     rtnl_lock();
1049 
1050     dev = rtnl_create_link(&init_net, "wwan%d", NET_NAME_ENUM,
1051                    &wwan_rtnl_link_ops, tb, NULL);
1052     if (WARN_ON(IS_ERR(dev)))
1053         goto unlock;
1054 
1055     if (WARN_ON(wwan_rtnl_newlink(&init_net, dev, tb, data, NULL))) {
1056         free_netdev(dev);
1057         goto unlock;
1058     }
1059 
1060     rtnl_configure_link(dev, NULL); /* Link initialized, notify new link */
1061 
1062 unlock:
1063     rtnl_unlock();
1064 
1065 free_attrs:
1066     nlmsg_free(msg);
1067 }
1068 
1069 /**
1070  * wwan_register_ops - register WWAN device ops
1071  * @parent: Device to use as parent and shared by all WWAN ports and
1072  *  created netdevs
1073  * @ops: operations to register
1074  * @ctxt: context to pass to operations
1075  * @def_link_id: id of the default link that will be automatically created by
1076  *  the WWAN core for the WWAN device. The default link will not be created
1077  *  if the passed value is WWAN_NO_DEFAULT_LINK.
1078  *
1079  * Returns: 0 on success, a negative error code on failure
1080  */
1081 int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
1082               void *ctxt, u32 def_link_id)
1083 {
1084     struct wwan_device *wwandev;
1085 
1086     if (WARN_ON(!parent || !ops || !ops->setup))
1087         return -EINVAL;
1088 
1089     wwandev = wwan_create_dev(parent);
1090     if (IS_ERR(wwandev))
1091         return PTR_ERR(wwandev);
1092 
1093     if (WARN_ON(wwandev->ops)) {
1094         wwan_remove_dev(wwandev);
1095         return -EBUSY;
1096     }
1097 
1098     wwandev->ops = ops;
1099     wwandev->ops_ctxt = ctxt;
1100 
1101     /* NB: we do not abort ops registration in case of default link
1102      * creation failure. Link ops is the management interface, while the
1103      * default link creation is a service option. And we should not prevent
1104      * a user from manually creating a link latter if service option failed
1105      * now.
1106      */
1107     if (def_link_id != WWAN_NO_DEFAULT_LINK)
1108         wwan_create_default_link(wwandev, def_link_id);
1109 
1110     return 0;
1111 }
1112 EXPORT_SYMBOL_GPL(wwan_register_ops);
1113 
1114 /* Enqueue child netdev deletion */
1115 static int wwan_child_dellink(struct device *dev, void *data)
1116 {
1117     struct list_head *kill_list = data;
1118 
1119     if (dev->type == &wwan_type)
1120         wwan_rtnl_dellink(to_net_dev(dev), kill_list);
1121 
1122     return 0;
1123 }
1124 
1125 /**
1126  * wwan_unregister_ops - remove WWAN device ops
1127  * @parent: Device to use as parent and shared by all WWAN ports and
1128  *  created netdevs
1129  */
1130 void wwan_unregister_ops(struct device *parent)
1131 {
1132     struct wwan_device *wwandev = wwan_dev_get_by_parent(parent);
1133     LIST_HEAD(kill_list);
1134 
1135     if (WARN_ON(IS_ERR(wwandev)))
1136         return;
1137     if (WARN_ON(!wwandev->ops)) {
1138         put_device(&wwandev->dev);
1139         return;
1140     }
1141 
1142     /* put the reference obtained by wwan_dev_get_by_parent(),
1143      * we should still have one (that the owner is giving back
1144      * now) due to the ops being assigned.
1145      */
1146     put_device(&wwandev->dev);
1147 
1148     rtnl_lock();    /* Prevent concurent netdev(s) creation/destroying */
1149 
1150     /* Remove all child netdev(s), using batch removing */
1151     device_for_each_child(&wwandev->dev, &kill_list,
1152                   wwan_child_dellink);
1153     unregister_netdevice_many(&kill_list);
1154 
1155     wwandev->ops = NULL;    /* Finally remove ops */
1156 
1157     rtnl_unlock();
1158 
1159     wwandev->ops_ctxt = NULL;
1160     wwan_remove_dev(wwandev);
1161 }
1162 EXPORT_SYMBOL_GPL(wwan_unregister_ops);
1163 
1164 static int __init wwan_init(void)
1165 {
1166     int err;
1167 
1168     err = rtnl_link_register(&wwan_rtnl_link_ops);
1169     if (err)
1170         return err;
1171 
1172     wwan_class = class_create(THIS_MODULE, "wwan");
1173     if (IS_ERR(wwan_class)) {
1174         err = PTR_ERR(wwan_class);
1175         goto unregister;
1176     }
1177 
1178     /* chrdev used for wwan ports */
1179     wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
1180                        &wwan_port_fops);
1181     if (wwan_major < 0) {
1182         err = wwan_major;
1183         goto destroy;
1184     }
1185 
1186 #ifdef CONFIG_WWAN_DEBUGFS
1187     wwan_debugfs_dir = debugfs_create_dir("wwan", NULL);
1188 #endif
1189 
1190     return 0;
1191 
1192 destroy:
1193     class_destroy(wwan_class);
1194 unregister:
1195     rtnl_link_unregister(&wwan_rtnl_link_ops);
1196     return err;
1197 }
1198 
1199 static void __exit wwan_exit(void)
1200 {
1201     debugfs_remove_recursive(wwan_debugfs_dir);
1202     __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port");
1203     rtnl_link_unregister(&wwan_rtnl_link_ops);
1204     class_destroy(wwan_class);
1205 }
1206 
1207 module_init(wwan_init);
1208 module_exit(wwan_exit);
1209 
1210 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
1211 MODULE_DESCRIPTION("WWAN core");
1212 MODULE_LICENSE("GPL v2");