19a44c1ccSLoic Poulain // SPDX-License-Identifier: GPL-2.0-only
29a44c1ccSLoic Poulain /* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */
39a44c1ccSLoic Poulain
49a44c1ccSLoic Poulain #include <linux/err.h>
59a44c1ccSLoic Poulain #include <linux/errno.h>
6c4804670SM Chetan Kumar #include <linux/debugfs.h>
79a44c1ccSLoic Poulain #include <linux/fs.h>
89a44c1ccSLoic Poulain #include <linux/init.h>
99a44c1ccSLoic Poulain #include <linux/idr.h>
109a44c1ccSLoic Poulain #include <linux/kernel.h>
119a44c1ccSLoic Poulain #include <linux/module.h>
129a44c1ccSLoic Poulain #include <linux/poll.h>
139a44c1ccSLoic Poulain #include <linux/skbuff.h>
149a44c1ccSLoic Poulain #include <linux/slab.h>
159a44c1ccSLoic Poulain #include <linux/types.h>
1689bbeb7eSAl Viro #include <linux/uaccess.h>
17e263c5b2SSergey Ryazanov #include <linux/termios.h>
189a44c1ccSLoic Poulain #include <linux/wwan.h>
1988b71053SJohannes Berg #include <net/rtnetlink.h>
2088b71053SJohannes Berg #include <uapi/linux/wwan.h>
219a44c1ccSLoic Poulain
2272eedfc4SSergey Ryazanov /* Maximum number of minors in use */
2372eedfc4SSergey Ryazanov #define WWAN_MAX_MINORS (1 << MINORBITS)
249a44c1ccSLoic Poulain
259a44c1ccSLoic Poulain static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
269a44c1ccSLoic Poulain static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
279a44c1ccSLoic Poulain static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */
289a44c1ccSLoic Poulain static struct class *wwan_class;
299a44c1ccSLoic Poulain static int wwan_major;
30c4804670SM Chetan Kumar static struct dentry *wwan_debugfs_dir;
319a44c1ccSLoic Poulain
329a44c1ccSLoic Poulain #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
339a44c1ccSLoic Poulain #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
349a44c1ccSLoic Poulain
359a44c1ccSLoic Poulain /* WWAN port flags */
36b8c55ce2SLoic Poulain #define WWAN_PORT_TX_OFF 0
379a44c1ccSLoic Poulain
389a44c1ccSLoic Poulain /**
399a44c1ccSLoic Poulain * struct wwan_device - The structure that defines a WWAN device
409a44c1ccSLoic Poulain *
419a44c1ccSLoic Poulain * @id: WWAN device unique ID.
429a44c1ccSLoic Poulain * @dev: Underlying device.
4388b71053SJohannes Berg * @port_id: Current available port ID to pick.
4488b71053SJohannes Berg * @ops: wwan device ops
4588b71053SJohannes Berg * @ops_ctxt: context to pass to ops
46c4804670SM Chetan Kumar * @debugfs_dir: WWAN device debugfs dir
479a44c1ccSLoic Poulain */
489a44c1ccSLoic Poulain struct wwan_device {
499a44c1ccSLoic Poulain unsigned int id;
509a44c1ccSLoic Poulain struct device dev;
5188b71053SJohannes Berg atomic_t port_id;
5288b71053SJohannes Berg const struct wwan_ops *ops;
5388b71053SJohannes Berg void *ops_ctxt;
54283e6f5aSSergey Ryazanov #ifdef CONFIG_WWAN_DEBUGFS
55c4804670SM Chetan Kumar struct dentry *debugfs_dir;
56283e6f5aSSergey Ryazanov #endif
579a44c1ccSLoic Poulain };
589a44c1ccSLoic Poulain
599a44c1ccSLoic Poulain /**
609a44c1ccSLoic Poulain * struct wwan_port - The structure that defines a WWAN port
619a44c1ccSLoic Poulain * @type: Port type
629a44c1ccSLoic Poulain * @start_count: Port start counter
639a44c1ccSLoic Poulain * @flags: Store port state and capabilities
649a44c1ccSLoic Poulain * @ops: Pointer to WWAN port operations
659a44c1ccSLoic Poulain * @ops_lock: Protect port ops
669a44c1ccSLoic Poulain * @dev: Underlying device
679a44c1ccSLoic Poulain * @rxq: Buffer inbound queue
689a44c1ccSLoic Poulain * @waitqueue: The waitqueue for port fops (read/write/poll)
69c230035cSSergey Ryazanov * @data_lock: Port specific data access serialization
7036bd28c1Shaozhe chang * @headroom_len: SKB reserved headroom size
7136bd28c1Shaozhe chang * @frag_len: Length to fragment packet
72c230035cSSergey Ryazanov * @at_data: AT port specific data
739a44c1ccSLoic Poulain */
749a44c1ccSLoic Poulain struct wwan_port {
759a44c1ccSLoic Poulain enum wwan_port_type type;
769a44c1ccSLoic Poulain unsigned int start_count;
779a44c1ccSLoic Poulain unsigned long flags;
789a44c1ccSLoic Poulain const struct wwan_port_ops *ops;
799a44c1ccSLoic Poulain struct mutex ops_lock; /* Serialize ops + protect against removal */
809a44c1ccSLoic Poulain struct device dev;
819a44c1ccSLoic Poulain struct sk_buff_head rxq;
829a44c1ccSLoic Poulain wait_queue_head_t waitqueue;
83c230035cSSergey Ryazanov struct mutex data_lock; /* Port specific data access serialization */
8436bd28c1Shaozhe chang size_t headroom_len;
8536bd28c1Shaozhe chang size_t frag_len;
86c230035cSSergey Ryazanov union {
87c230035cSSergey Ryazanov struct {
88c230035cSSergey Ryazanov struct ktermios termios;
89c230035cSSergey Ryazanov int mdmbits;
90c230035cSSergey Ryazanov } at_data;
91c230035cSSergey Ryazanov };
929a44c1ccSLoic Poulain };
939a44c1ccSLoic Poulain
index_show(struct device * dev,struct device_attribute * attr,char * buf)94e4e92ee7SLoic Poulain static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
95e4e92ee7SLoic Poulain {
96e4e92ee7SLoic Poulain struct wwan_device *wwan = to_wwan_dev(dev);
97e4e92ee7SLoic Poulain
98e4e92ee7SLoic Poulain return sprintf(buf, "%d\n", wwan->id);
99e4e92ee7SLoic Poulain }
100e4e92ee7SLoic Poulain static DEVICE_ATTR_RO(index);
101e4e92ee7SLoic Poulain
102e4e92ee7SLoic Poulain static struct attribute *wwan_dev_attrs[] = {
103e4e92ee7SLoic Poulain &dev_attr_index.attr,
104e4e92ee7SLoic Poulain NULL,
105e4e92ee7SLoic Poulain };
106e4e92ee7SLoic Poulain ATTRIBUTE_GROUPS(wwan_dev);
107e4e92ee7SLoic Poulain
wwan_dev_destroy(struct device * dev)1089a44c1ccSLoic Poulain static void wwan_dev_destroy(struct device *dev)
1099a44c1ccSLoic Poulain {
1109a44c1ccSLoic Poulain struct wwan_device *wwandev = to_wwan_dev(dev);
1119a44c1ccSLoic Poulain
1129a44c1ccSLoic Poulain ida_free(&wwan_dev_ids, wwandev->id);
1139a44c1ccSLoic Poulain kfree(wwandev);
1149a44c1ccSLoic Poulain }
1159a44c1ccSLoic Poulain
1169a44c1ccSLoic Poulain static const struct device_type wwan_dev_type = {
1179a44c1ccSLoic Poulain .name = "wwan_dev",
1189a44c1ccSLoic Poulain .release = wwan_dev_destroy,
119e4e92ee7SLoic Poulain .groups = wwan_dev_groups,
1209a44c1ccSLoic Poulain };
1219a44c1ccSLoic Poulain
wwan_dev_parent_match(struct device * dev,const void * parent)1229a44c1ccSLoic Poulain static int wwan_dev_parent_match(struct device *dev, const void *parent)
1239a44c1ccSLoic Poulain {
12488b71053SJohannes Berg return (dev->type == &wwan_dev_type &&
12588b71053SJohannes Berg (dev->parent == parent || dev == parent));
1269a44c1ccSLoic Poulain }
1279a44c1ccSLoic Poulain
wwan_dev_get_by_parent(struct device * parent)1289a44c1ccSLoic Poulain static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
1299a44c1ccSLoic Poulain {
1309a44c1ccSLoic Poulain struct device *dev;
1319a44c1ccSLoic Poulain
1329a44c1ccSLoic Poulain dev = class_find_device(wwan_class, NULL, parent, wwan_dev_parent_match);
1339a44c1ccSLoic Poulain if (!dev)
1349a44c1ccSLoic Poulain return ERR_PTR(-ENODEV);
1359a44c1ccSLoic Poulain
1369a44c1ccSLoic Poulain return to_wwan_dev(dev);
1379a44c1ccSLoic Poulain }
1389a44c1ccSLoic Poulain
wwan_dev_name_match(struct device * dev,const void * name)13988b71053SJohannes Berg static int wwan_dev_name_match(struct device *dev, const void *name)
14088b71053SJohannes Berg {
14188b71053SJohannes Berg return dev->type == &wwan_dev_type &&
14288b71053SJohannes Berg strcmp(dev_name(dev), name) == 0;
14388b71053SJohannes Berg }
14488b71053SJohannes Berg
wwan_dev_get_by_name(const char * name)14588b71053SJohannes Berg static struct wwan_device *wwan_dev_get_by_name(const char *name)
14688b71053SJohannes Berg {
14788b71053SJohannes Berg struct device *dev;
14888b71053SJohannes Berg
14988b71053SJohannes Berg dev = class_find_device(wwan_class, NULL, name, wwan_dev_name_match);
15088b71053SJohannes Berg if (!dev)
15188b71053SJohannes Berg return ERR_PTR(-ENODEV);
15288b71053SJohannes Berg
15388b71053SJohannes Berg return to_wwan_dev(dev);
15488b71053SJohannes Berg }
15588b71053SJohannes Berg
156283e6f5aSSergey Ryazanov #ifdef CONFIG_WWAN_DEBUGFS
wwan_get_debugfs_dir(struct device * parent)157c4804670SM Chetan Kumar struct dentry *wwan_get_debugfs_dir(struct device *parent)
158c4804670SM Chetan Kumar {
159c4804670SM Chetan Kumar struct wwan_device *wwandev;
160c4804670SM Chetan Kumar
161c4804670SM Chetan Kumar wwandev = wwan_dev_get_by_parent(parent);
162c4804670SM Chetan Kumar if (IS_ERR(wwandev))
163c4804670SM Chetan Kumar return ERR_CAST(wwandev);
164c4804670SM Chetan Kumar
165c4804670SM Chetan Kumar return wwandev->debugfs_dir;
166c4804670SM Chetan Kumar }
167c4804670SM Chetan Kumar EXPORT_SYMBOL_GPL(wwan_get_debugfs_dir);
16876f05d88SM Chetan Kumar
wwan_dev_debugfs_match(struct device * dev,const void * dir)16976f05d88SM Chetan Kumar static int wwan_dev_debugfs_match(struct device *dev, const void *dir)
17076f05d88SM Chetan Kumar {
17176f05d88SM Chetan Kumar struct wwan_device *wwandev;
17276f05d88SM Chetan Kumar
17376f05d88SM Chetan Kumar if (dev->type != &wwan_dev_type)
17476f05d88SM Chetan Kumar return 0;
17576f05d88SM Chetan Kumar
17676f05d88SM Chetan Kumar wwandev = to_wwan_dev(dev);
17776f05d88SM Chetan Kumar
17876f05d88SM Chetan Kumar return wwandev->debugfs_dir == dir;
17976f05d88SM Chetan Kumar }
18076f05d88SM Chetan Kumar
wwan_dev_get_by_debugfs(struct dentry * dir)18176f05d88SM Chetan Kumar static struct wwan_device *wwan_dev_get_by_debugfs(struct dentry *dir)
18276f05d88SM Chetan Kumar {
18376f05d88SM Chetan Kumar struct device *dev;
18476f05d88SM Chetan Kumar
18576f05d88SM Chetan Kumar dev = class_find_device(wwan_class, NULL, dir, wwan_dev_debugfs_match);
18676f05d88SM Chetan Kumar if (!dev)
18776f05d88SM Chetan Kumar return ERR_PTR(-ENODEV);
18876f05d88SM Chetan Kumar
18976f05d88SM Chetan Kumar return to_wwan_dev(dev);
19076f05d88SM Chetan Kumar }
19176f05d88SM Chetan Kumar
wwan_put_debugfs_dir(struct dentry * dir)19276f05d88SM Chetan Kumar void wwan_put_debugfs_dir(struct dentry *dir)
19376f05d88SM Chetan Kumar {
19476f05d88SM Chetan Kumar struct wwan_device *wwandev = wwan_dev_get_by_debugfs(dir);
19576f05d88SM Chetan Kumar
19676f05d88SM Chetan Kumar if (WARN_ON(IS_ERR(wwandev)))
19776f05d88SM Chetan Kumar return;
19876f05d88SM Chetan Kumar
19976f05d88SM Chetan Kumar /* wwan_dev_get_by_debugfs() also got a reference */
20076f05d88SM Chetan Kumar put_device(&wwandev->dev);
20176f05d88SM Chetan Kumar put_device(&wwandev->dev);
20276f05d88SM Chetan Kumar }
20376f05d88SM Chetan Kumar EXPORT_SYMBOL_GPL(wwan_put_debugfs_dir);
204283e6f5aSSergey Ryazanov #endif
205c4804670SM Chetan Kumar
2069a44c1ccSLoic Poulain /* This function allocates and registers a new WWAN device OR if a WWAN device
2079a44c1ccSLoic Poulain * already exist for the given parent, it gets a reference and return it.
2089a44c1ccSLoic Poulain * This function is not exported (for now), it is called indirectly via
2099a44c1ccSLoic Poulain * wwan_create_port().
2109a44c1ccSLoic Poulain */
wwan_create_dev(struct device * parent)2119a44c1ccSLoic Poulain static struct wwan_device *wwan_create_dev(struct device *parent)
2129a44c1ccSLoic Poulain {
2139a44c1ccSLoic Poulain struct wwan_device *wwandev;
2149a44c1ccSLoic Poulain int err, id;
2159a44c1ccSLoic Poulain
2169a44c1ccSLoic Poulain /* The 'find-alloc-register' operation must be protected against
2179a44c1ccSLoic Poulain * concurrent execution, a WWAN device is possibly shared between
2189a44c1ccSLoic Poulain * multiple callers or concurrently unregistered from wwan_remove_dev().
2199a44c1ccSLoic Poulain */
2209a44c1ccSLoic Poulain mutex_lock(&wwan_register_lock);
2219a44c1ccSLoic Poulain
2229a44c1ccSLoic Poulain /* If wwandev already exists, return it */
2239a44c1ccSLoic Poulain wwandev = wwan_dev_get_by_parent(parent);
2249a44c1ccSLoic Poulain if (!IS_ERR(wwandev))
2259a44c1ccSLoic Poulain goto done_unlock;
2269a44c1ccSLoic Poulain
2279a44c1ccSLoic Poulain id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
228d9d5b896SAndy Shevchenko if (id < 0) {
229d9d5b896SAndy Shevchenko wwandev = ERR_PTR(id);
2309a44c1ccSLoic Poulain goto done_unlock;
231d9d5b896SAndy Shevchenko }
2329a44c1ccSLoic Poulain
2339a44c1ccSLoic Poulain wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL);
2349a44c1ccSLoic Poulain if (!wwandev) {
235d9d5b896SAndy Shevchenko wwandev = ERR_PTR(-ENOMEM);
2369a44c1ccSLoic Poulain ida_free(&wwan_dev_ids, id);
2379a44c1ccSLoic Poulain goto done_unlock;
2389a44c1ccSLoic Poulain }
2399a44c1ccSLoic Poulain
2409a44c1ccSLoic Poulain wwandev->dev.parent = parent;
2419a44c1ccSLoic Poulain wwandev->dev.class = wwan_class;
2429a44c1ccSLoic Poulain wwandev->dev.type = &wwan_dev_type;
2439a44c1ccSLoic Poulain wwandev->id = id;
2449a44c1ccSLoic Poulain dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
2459a44c1ccSLoic Poulain
2469a44c1ccSLoic Poulain err = device_register(&wwandev->dev);
2479a44c1ccSLoic Poulain if (err) {
2489a44c1ccSLoic Poulain put_device(&wwandev->dev);
249d9d5b896SAndy Shevchenko wwandev = ERR_PTR(err);
250d9d5b896SAndy Shevchenko goto done_unlock;
2519a44c1ccSLoic Poulain }
2529a44c1ccSLoic Poulain
253283e6f5aSSergey Ryazanov #ifdef CONFIG_WWAN_DEBUGFS
254283e6f5aSSergey Ryazanov wwandev->debugfs_dir =
255283e6f5aSSergey Ryazanov debugfs_create_dir(kobject_name(&wwandev->dev.kobj),
256c4804670SM Chetan Kumar wwan_debugfs_dir);
257283e6f5aSSergey Ryazanov #endif
258c4804670SM Chetan Kumar
2599a44c1ccSLoic Poulain done_unlock:
2609a44c1ccSLoic Poulain mutex_unlock(&wwan_register_lock);
2619a44c1ccSLoic Poulain
2629a44c1ccSLoic Poulain return wwandev;
2639a44c1ccSLoic Poulain }
2649a44c1ccSLoic Poulain
is_wwan_child(struct device * dev,void * data)2659a44c1ccSLoic Poulain static int is_wwan_child(struct device *dev, void *data)
2669a44c1ccSLoic Poulain {
2679a44c1ccSLoic Poulain return dev->class == wwan_class;
2689a44c1ccSLoic Poulain }
2699a44c1ccSLoic Poulain
wwan_remove_dev(struct wwan_device * wwandev)2709a44c1ccSLoic Poulain static void wwan_remove_dev(struct wwan_device *wwandev)
2719a44c1ccSLoic Poulain {
2729a44c1ccSLoic Poulain int ret;
2739a44c1ccSLoic Poulain
2749a44c1ccSLoic Poulain /* Prevent concurrent picking from wwan_create_dev */
2759a44c1ccSLoic Poulain mutex_lock(&wwan_register_lock);
2769a44c1ccSLoic Poulain
2779a44c1ccSLoic Poulain /* WWAN device is created and registered (get+add) along with its first
2789a44c1ccSLoic Poulain * child port, and subsequent port registrations only grab a reference
2799a44c1ccSLoic Poulain * (get). The WWAN device must then be unregistered (del+put) along with
28088b71053SJohannes Berg * its last port, and reference simply dropped (put) otherwise. In the
28188b71053SJohannes Berg * same fashion, we must not unregister it when the ops are still there.
2829a44c1ccSLoic Poulain */
28388b71053SJohannes Berg if (wwandev->ops)
28488b71053SJohannes Berg ret = 1;
28588b71053SJohannes Berg else
2869a44c1ccSLoic Poulain ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child);
28788b71053SJohannes Berg
288c4804670SM Chetan Kumar if (!ret) {
289283e6f5aSSergey Ryazanov #ifdef CONFIG_WWAN_DEBUGFS
290c4804670SM Chetan Kumar debugfs_remove_recursive(wwandev->debugfs_dir);
291283e6f5aSSergey Ryazanov #endif
2929a44c1ccSLoic Poulain device_unregister(&wwandev->dev);
293c4804670SM Chetan Kumar } else {
2949a44c1ccSLoic Poulain put_device(&wwandev->dev);
295c4804670SM Chetan Kumar }
2969a44c1ccSLoic Poulain
2979a44c1ccSLoic Poulain mutex_unlock(&wwan_register_lock);
2989a44c1ccSLoic Poulain }
2999a44c1ccSLoic Poulain
3009a44c1ccSLoic Poulain /* ------- WWAN port management ------- */
3019a44c1ccSLoic Poulain
302392c26f7SSergey Ryazanov static const struct {
303392c26f7SSergey Ryazanov const char * const name; /* Port type name */
304392c26f7SSergey Ryazanov const char * const devsuf; /* Port devce name suffix */
305392c26f7SSergey Ryazanov } wwan_port_types[WWAN_PORT_MAX + 1] = {
306392c26f7SSergey Ryazanov [WWAN_PORT_AT] = {
307392c26f7SSergey Ryazanov .name = "AT",
308392c26f7SSergey Ryazanov .devsuf = "at",
309392c26f7SSergey Ryazanov },
310392c26f7SSergey Ryazanov [WWAN_PORT_MBIM] = {
311392c26f7SSergey Ryazanov .name = "MBIM",
312392c26f7SSergey Ryazanov .devsuf = "mbim",
313392c26f7SSergey Ryazanov },
314392c26f7SSergey Ryazanov [WWAN_PORT_QMI] = {
315392c26f7SSergey Ryazanov .name = "QMI",
316392c26f7SSergey Ryazanov .devsuf = "qmi",
317392c26f7SSergey Ryazanov },
318392c26f7SSergey Ryazanov [WWAN_PORT_QCDM] = {
319392c26f7SSergey Ryazanov .name = "QCDM",
320392c26f7SSergey Ryazanov .devsuf = "qcdm",
321392c26f7SSergey Ryazanov },
322392c26f7SSergey Ryazanov [WWAN_PORT_FIREHOSE] = {
323392c26f7SSergey Ryazanov .name = "FIREHOSE",
324392c26f7SSergey Ryazanov .devsuf = "firehose",
325392c26f7SSergey Ryazanov },
326d08b0f8fSShane Parslow [WWAN_PORT_XMMRPC] = {
327d08b0f8fSShane Parslow .name = "XMMRPC",
328d08b0f8fSShane Parslow .devsuf = "xmmrpc",
329d08b0f8fSShane Parslow },
330b3e22e10SLoic Poulain };
331b3e22e10SLoic Poulain
type_show(struct device * dev,struct device_attribute * attr,char * buf)332b3e22e10SLoic Poulain static ssize_t type_show(struct device *dev, struct device_attribute *attr,
333b3e22e10SLoic Poulain char *buf)
334b3e22e10SLoic Poulain {
335b3e22e10SLoic Poulain struct wwan_port *port = to_wwan_port(dev);
336b3e22e10SLoic Poulain
337392c26f7SSergey Ryazanov return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
338b3e22e10SLoic Poulain }
339b3e22e10SLoic Poulain static DEVICE_ATTR_RO(type);
340b3e22e10SLoic Poulain
341b3e22e10SLoic Poulain static struct attribute *wwan_port_attrs[] = {
342b3e22e10SLoic Poulain &dev_attr_type.attr,
343b3e22e10SLoic Poulain NULL,
344b3e22e10SLoic Poulain };
345b3e22e10SLoic Poulain ATTRIBUTE_GROUPS(wwan_port);
346b3e22e10SLoic Poulain
wwan_port_destroy(struct device * dev)3479a44c1ccSLoic Poulain static void wwan_port_destroy(struct device *dev)
3489a44c1ccSLoic Poulain {
3499a44c1ccSLoic Poulain struct wwan_port *port = to_wwan_port(dev);
3509a44c1ccSLoic Poulain
3519a44c1ccSLoic Poulain ida_free(&minors, MINOR(port->dev.devt));
352c230035cSSergey Ryazanov mutex_destroy(&port->data_lock);
3539a44c1ccSLoic Poulain mutex_destroy(&port->ops_lock);
3549a44c1ccSLoic Poulain kfree(port);
3559a44c1ccSLoic Poulain }
3569a44c1ccSLoic Poulain
3579a44c1ccSLoic Poulain static const struct device_type wwan_port_dev_type = {
3589a44c1ccSLoic Poulain .name = "wwan_port",
3599a44c1ccSLoic Poulain .release = wwan_port_destroy,
360b3e22e10SLoic Poulain .groups = wwan_port_groups,
3619a44c1ccSLoic Poulain };
3629a44c1ccSLoic Poulain
wwan_port_minor_match(struct device * dev,const void * minor)3639a44c1ccSLoic Poulain static int wwan_port_minor_match(struct device *dev, const void *minor)
3649a44c1ccSLoic Poulain {
3659a44c1ccSLoic Poulain return (dev->type == &wwan_port_dev_type &&
3669a44c1ccSLoic Poulain MINOR(dev->devt) == *(unsigned int *)minor);
3679a44c1ccSLoic Poulain }
3689a44c1ccSLoic Poulain
wwan_port_get_by_minor(unsigned int minor)3699a44c1ccSLoic Poulain static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
3709a44c1ccSLoic Poulain {
3719a44c1ccSLoic Poulain struct device *dev;
3729a44c1ccSLoic Poulain
3739a44c1ccSLoic Poulain dev = class_find_device(wwan_class, NULL, &minor, wwan_port_minor_match);
3749a44c1ccSLoic Poulain if (!dev)
3759a44c1ccSLoic Poulain return ERR_PTR(-ENODEV);
3769a44c1ccSLoic Poulain
3779a44c1ccSLoic Poulain return to_wwan_port(dev);
3789a44c1ccSLoic Poulain }
3799a44c1ccSLoic Poulain
380f458709fSSergey Ryazanov /* Allocate and set unique name based on passed format
381f458709fSSergey Ryazanov *
382f458709fSSergey Ryazanov * Name allocation approach is highly inspired by the __dev_alloc_name()
383f458709fSSergey Ryazanov * function.
384f458709fSSergey Ryazanov *
385f458709fSSergey Ryazanov * To avoid names collision, the caller must prevent the new port device
386f458709fSSergey Ryazanov * registration as well as concurrent invocation of this function.
387f458709fSSergey Ryazanov */
__wwan_port_dev_assign_name(struct wwan_port * port,const char * fmt)388f458709fSSergey Ryazanov static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
389f458709fSSergey Ryazanov {
390f458709fSSergey Ryazanov struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
391f458709fSSergey Ryazanov const unsigned int max_ports = PAGE_SIZE * 8;
392f458709fSSergey Ryazanov struct class_dev_iter iter;
393f458709fSSergey Ryazanov unsigned long *idmap;
394f458709fSSergey Ryazanov struct device *dev;
395f458709fSSergey Ryazanov char buf[0x20];
396f458709fSSergey Ryazanov int id;
397f458709fSSergey Ryazanov
398f458709fSSergey Ryazanov idmap = (unsigned long *)get_zeroed_page(GFP_KERNEL);
399f458709fSSergey Ryazanov if (!idmap)
400f458709fSSergey Ryazanov return -ENOMEM;
401f458709fSSergey Ryazanov
402f458709fSSergey Ryazanov /* Collect ids of same name format ports */
403f458709fSSergey Ryazanov class_dev_iter_init(&iter, wwan_class, NULL, &wwan_port_dev_type);
404f458709fSSergey Ryazanov while ((dev = class_dev_iter_next(&iter))) {
405f458709fSSergey Ryazanov if (dev->parent != &wwandev->dev)
406f458709fSSergey Ryazanov continue;
407f458709fSSergey Ryazanov if (sscanf(dev_name(dev), fmt, &id) != 1)
408f458709fSSergey Ryazanov continue;
409f458709fSSergey Ryazanov if (id < 0 || id >= max_ports)
410f458709fSSergey Ryazanov continue;
411f458709fSSergey Ryazanov set_bit(id, idmap);
412f458709fSSergey Ryazanov }
413f458709fSSergey Ryazanov class_dev_iter_exit(&iter);
414f458709fSSergey Ryazanov
415f458709fSSergey Ryazanov /* Allocate unique id */
416f458709fSSergey Ryazanov id = find_first_zero_bit(idmap, max_ports);
417f458709fSSergey Ryazanov free_page((unsigned long)idmap);
418f458709fSSergey Ryazanov
419f458709fSSergey Ryazanov snprintf(buf, sizeof(buf), fmt, id); /* Name generation */
420f458709fSSergey Ryazanov
421f458709fSSergey Ryazanov dev = device_find_child_by_name(&wwandev->dev, buf);
422f458709fSSergey Ryazanov if (dev) {
423f458709fSSergey Ryazanov put_device(dev);
424f458709fSSergey Ryazanov return -ENFILE;
425f458709fSSergey Ryazanov }
426f458709fSSergey Ryazanov
427f458709fSSergey Ryazanov return dev_set_name(&port->dev, buf);
428f458709fSSergey Ryazanov }
429f458709fSSergey Ryazanov
wwan_create_port(struct device * parent,enum wwan_port_type type,const struct wwan_port_ops * ops,struct wwan_port_caps * caps,void * drvdata)4309a44c1ccSLoic Poulain struct wwan_port *wwan_create_port(struct device *parent,
4319a44c1ccSLoic Poulain enum wwan_port_type type,
4329a44c1ccSLoic Poulain const struct wwan_port_ops *ops,
43336bd28c1Shaozhe chang struct wwan_port_caps *caps,
4349a44c1ccSLoic Poulain void *drvdata)
4359a44c1ccSLoic Poulain {
4369a44c1ccSLoic Poulain struct wwan_device *wwandev;
4379a44c1ccSLoic Poulain struct wwan_port *port;
438f458709fSSergey Ryazanov char namefmt[0x20];
4390de6fd5fSAndy Shevchenko int minor, err;
4409a44c1ccSLoic Poulain
441b64d76b7SSergey Ryazanov if (type > WWAN_PORT_MAX || !ops)
4429a44c1ccSLoic Poulain return ERR_PTR(-EINVAL);
4439a44c1ccSLoic Poulain
4449a44c1ccSLoic Poulain /* A port is always a child of a WWAN device, retrieve (allocate or
4459a44c1ccSLoic Poulain * pick) the WWAN device based on the provided parent device.
4469a44c1ccSLoic Poulain */
4479a44c1ccSLoic Poulain wwandev = wwan_create_dev(parent);
4489a44c1ccSLoic Poulain if (IS_ERR(wwandev))
4499a44c1ccSLoic Poulain return ERR_CAST(wwandev);
4509a44c1ccSLoic Poulain
4519a44c1ccSLoic Poulain /* A port is exposed as character device, get a minor */
4529a44c1ccSLoic Poulain minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
4530de6fd5fSAndy Shevchenko if (minor < 0) {
4540de6fd5fSAndy Shevchenko err = minor;
4559a44c1ccSLoic Poulain goto error_wwandev_remove;
4560de6fd5fSAndy Shevchenko }
4579a44c1ccSLoic Poulain
4589a44c1ccSLoic Poulain port = kzalloc(sizeof(*port), GFP_KERNEL);
4599a44c1ccSLoic Poulain if (!port) {
4600de6fd5fSAndy Shevchenko err = -ENOMEM;
4619a44c1ccSLoic Poulain ida_free(&minors, minor);
4629a44c1ccSLoic Poulain goto error_wwandev_remove;
4639a44c1ccSLoic Poulain }
4649a44c1ccSLoic Poulain
4659a44c1ccSLoic Poulain port->type = type;
4669a44c1ccSLoic Poulain port->ops = ops;
46736bd28c1Shaozhe chang port->frag_len = caps ? caps->frag_len : SIZE_MAX;
46836bd28c1Shaozhe chang port->headroom_len = caps ? caps->headroom_len : 0;
4699a44c1ccSLoic Poulain mutex_init(&port->ops_lock);
4709a44c1ccSLoic Poulain skb_queue_head_init(&port->rxq);
4719a44c1ccSLoic Poulain init_waitqueue_head(&port->waitqueue);
472c230035cSSergey Ryazanov mutex_init(&port->data_lock);
4739a44c1ccSLoic Poulain
4749a44c1ccSLoic Poulain port->dev.parent = &wwandev->dev;
4759a44c1ccSLoic Poulain port->dev.class = wwan_class;
4769a44c1ccSLoic Poulain port->dev.type = &wwan_port_dev_type;
4779a44c1ccSLoic Poulain port->dev.devt = MKDEV(wwan_major, minor);
4789a44c1ccSLoic Poulain dev_set_drvdata(&port->dev, drvdata);
4799a44c1ccSLoic Poulain
480f458709fSSergey Ryazanov /* allocate unique name based on wwan device id, port type and number */
481f458709fSSergey Ryazanov snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
482392c26f7SSergey Ryazanov wwan_port_types[port->type].devsuf);
4839a44c1ccSLoic Poulain
484f458709fSSergey Ryazanov /* Serialize ports registration */
485f458709fSSergey Ryazanov mutex_lock(&wwan_register_lock);
486f458709fSSergey Ryazanov
487f458709fSSergey Ryazanov __wwan_port_dev_assign_name(port, namefmt);
4889a44c1ccSLoic Poulain err = device_register(&port->dev);
489f458709fSSergey Ryazanov
490f458709fSSergey Ryazanov mutex_unlock(&wwan_register_lock);
491f458709fSSergey Ryazanov
4929a44c1ccSLoic Poulain if (err)
4939a44c1ccSLoic Poulain goto error_put_device;
4949a44c1ccSLoic Poulain
495*787e6144SSlark Xiao dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev));
4969a44c1ccSLoic Poulain return port;
4979a44c1ccSLoic Poulain
4989a44c1ccSLoic Poulain error_put_device:
4999a44c1ccSLoic Poulain put_device(&port->dev);
5009a44c1ccSLoic Poulain error_wwandev_remove:
5019a44c1ccSLoic Poulain wwan_remove_dev(wwandev);
5029a44c1ccSLoic Poulain
5039a44c1ccSLoic Poulain return ERR_PTR(err);
5049a44c1ccSLoic Poulain }
5059a44c1ccSLoic Poulain EXPORT_SYMBOL_GPL(wwan_create_port);
5069a44c1ccSLoic Poulain
wwan_remove_port(struct wwan_port * port)5079a44c1ccSLoic Poulain void wwan_remove_port(struct wwan_port *port)
5089a44c1ccSLoic Poulain {
5099a44c1ccSLoic Poulain struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
5109a44c1ccSLoic Poulain
5119a44c1ccSLoic Poulain mutex_lock(&port->ops_lock);
5129a44c1ccSLoic Poulain if (port->start_count)
5139a44c1ccSLoic Poulain port->ops->stop(port);
5149a44c1ccSLoic Poulain port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
5159a44c1ccSLoic Poulain mutex_unlock(&port->ops_lock);
5169a44c1ccSLoic Poulain
5179a44c1ccSLoic Poulain wake_up_interruptible(&port->waitqueue);
5189a44c1ccSLoic Poulain
5199a44c1ccSLoic Poulain skb_queue_purge(&port->rxq);
5209a44c1ccSLoic Poulain dev_set_drvdata(&port->dev, NULL);
521*787e6144SSlark Xiao
522*787e6144SSlark Xiao dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&port->dev));
5239a44c1ccSLoic Poulain device_unregister(&port->dev);
5249a44c1ccSLoic Poulain
5259a44c1ccSLoic Poulain /* Release related wwan device */
5269a44c1ccSLoic Poulain wwan_remove_dev(wwandev);
5279a44c1ccSLoic Poulain }
5289a44c1ccSLoic Poulain EXPORT_SYMBOL_GPL(wwan_remove_port);
5299a44c1ccSLoic Poulain
wwan_port_rx(struct wwan_port * port,struct sk_buff * skb)5309a44c1ccSLoic Poulain void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
5319a44c1ccSLoic Poulain {
5329a44c1ccSLoic Poulain skb_queue_tail(&port->rxq, skb);
5339a44c1ccSLoic Poulain wake_up_interruptible(&port->waitqueue);
5349a44c1ccSLoic Poulain }
5359a44c1ccSLoic Poulain EXPORT_SYMBOL_GPL(wwan_port_rx);
5369a44c1ccSLoic Poulain
wwan_port_txon(struct wwan_port * port)5379a44c1ccSLoic Poulain void wwan_port_txon(struct wwan_port *port)
5389a44c1ccSLoic Poulain {
5399a44c1ccSLoic Poulain clear_bit(WWAN_PORT_TX_OFF, &port->flags);
5409a44c1ccSLoic Poulain wake_up_interruptible(&port->waitqueue);
5419a44c1ccSLoic Poulain }
5429a44c1ccSLoic Poulain EXPORT_SYMBOL_GPL(wwan_port_txon);
5439a44c1ccSLoic Poulain
wwan_port_txoff(struct wwan_port * port)5449a44c1ccSLoic Poulain void wwan_port_txoff(struct wwan_port *port)
5459a44c1ccSLoic Poulain {
5469a44c1ccSLoic Poulain set_bit(WWAN_PORT_TX_OFF, &port->flags);
5479a44c1ccSLoic Poulain }
5489a44c1ccSLoic Poulain EXPORT_SYMBOL_GPL(wwan_port_txoff);
5499a44c1ccSLoic Poulain
wwan_port_get_drvdata(struct wwan_port * port)5509a44c1ccSLoic Poulain void *wwan_port_get_drvdata(struct wwan_port *port)
5519a44c1ccSLoic Poulain {
5529a44c1ccSLoic Poulain return dev_get_drvdata(&port->dev);
5539a44c1ccSLoic Poulain }
5549a44c1ccSLoic Poulain EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
5559a44c1ccSLoic Poulain
wwan_port_op_start(struct wwan_port * port)5569a44c1ccSLoic Poulain static int wwan_port_op_start(struct wwan_port *port)
5579a44c1ccSLoic Poulain {
5589a44c1ccSLoic Poulain int ret = 0;
5599a44c1ccSLoic Poulain
5609a44c1ccSLoic Poulain mutex_lock(&port->ops_lock);
5619a44c1ccSLoic Poulain if (!port->ops) { /* Port got unplugged */
5629a44c1ccSLoic Poulain ret = -ENODEV;
5639a44c1ccSLoic Poulain goto out_unlock;
5649a44c1ccSLoic Poulain }
5659a44c1ccSLoic Poulain
5669a44c1ccSLoic Poulain /* If port is already started, don't start again */
5679a44c1ccSLoic Poulain if (!port->start_count)
5689a44c1ccSLoic Poulain ret = port->ops->start(port);
5699a44c1ccSLoic Poulain
5709a44c1ccSLoic Poulain if (!ret)
5719a44c1ccSLoic Poulain port->start_count++;
5729a44c1ccSLoic Poulain
5739a44c1ccSLoic Poulain out_unlock:
5749a44c1ccSLoic Poulain mutex_unlock(&port->ops_lock);
5759a44c1ccSLoic Poulain
5769a44c1ccSLoic Poulain return ret;
5779a44c1ccSLoic Poulain }
5789a44c1ccSLoic Poulain
wwan_port_op_stop(struct wwan_port * port)5799a44c1ccSLoic Poulain static void wwan_port_op_stop(struct wwan_port *port)
5809a44c1ccSLoic Poulain {
5819a44c1ccSLoic Poulain mutex_lock(&port->ops_lock);
5829a44c1ccSLoic Poulain port->start_count--;
58350467203SSergey Ryazanov if (!port->start_count) {
58450467203SSergey Ryazanov if (port->ops)
5859a44c1ccSLoic Poulain port->ops->stop(port);
58650467203SSergey Ryazanov skb_queue_purge(&port->rxq);
58750467203SSergey Ryazanov }
5889a44c1ccSLoic Poulain mutex_unlock(&port->ops_lock);
5899a44c1ccSLoic Poulain }
5909a44c1ccSLoic Poulain
wwan_port_op_tx(struct wwan_port * port,struct sk_buff * skb,bool nonblock)59131c143f7SStephan Gerhold static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
59231c143f7SStephan Gerhold bool nonblock)
5939a44c1ccSLoic Poulain {
5949a44c1ccSLoic Poulain int ret;
5959a44c1ccSLoic Poulain
5969a44c1ccSLoic Poulain mutex_lock(&port->ops_lock);
5979a44c1ccSLoic Poulain if (!port->ops) { /* Port got unplugged */
5989a44c1ccSLoic Poulain ret = -ENODEV;
5999a44c1ccSLoic Poulain goto out_unlock;
6009a44c1ccSLoic Poulain }
6019a44c1ccSLoic Poulain
60231c143f7SStephan Gerhold if (nonblock || !port->ops->tx_blocking)
6039a44c1ccSLoic Poulain ret = port->ops->tx(port, skb);
60431c143f7SStephan Gerhold else
60531c143f7SStephan Gerhold ret = port->ops->tx_blocking(port, skb);
6069a44c1ccSLoic Poulain
6079a44c1ccSLoic Poulain out_unlock:
6089a44c1ccSLoic Poulain mutex_unlock(&port->ops_lock);
6099a44c1ccSLoic Poulain
6109a44c1ccSLoic Poulain return ret;
6119a44c1ccSLoic Poulain }
6129a44c1ccSLoic Poulain
is_read_blocked(struct wwan_port * port)6139a44c1ccSLoic Poulain static bool is_read_blocked(struct wwan_port *port)
6149a44c1ccSLoic Poulain {
6159a44c1ccSLoic Poulain return skb_queue_empty(&port->rxq) && port->ops;
6169a44c1ccSLoic Poulain }
6179a44c1ccSLoic Poulain
is_write_blocked(struct wwan_port * port)6189a44c1ccSLoic Poulain static bool is_write_blocked(struct wwan_port *port)
6199a44c1ccSLoic Poulain {
6209a44c1ccSLoic Poulain return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
6219a44c1ccSLoic Poulain }
6229a44c1ccSLoic Poulain
wwan_wait_rx(struct wwan_port * port,bool nonblock)6239a44c1ccSLoic Poulain static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
6249a44c1ccSLoic Poulain {
6259a44c1ccSLoic Poulain if (!is_read_blocked(port))
6269a44c1ccSLoic Poulain return 0;
6279a44c1ccSLoic Poulain
6289a44c1ccSLoic Poulain if (nonblock)
6299a44c1ccSLoic Poulain return -EAGAIN;
6309a44c1ccSLoic Poulain
6319a44c1ccSLoic Poulain if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
6329a44c1ccSLoic Poulain return -ERESTARTSYS;
6339a44c1ccSLoic Poulain
6349a44c1ccSLoic Poulain return 0;
6359a44c1ccSLoic Poulain }
6369a44c1ccSLoic Poulain
wwan_wait_tx(struct wwan_port * port,bool nonblock)6379a44c1ccSLoic Poulain static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
6389a44c1ccSLoic Poulain {
6399a44c1ccSLoic Poulain if (!is_write_blocked(port))
6409a44c1ccSLoic Poulain return 0;
6419a44c1ccSLoic Poulain
6429a44c1ccSLoic Poulain if (nonblock)
6439a44c1ccSLoic Poulain return -EAGAIN;
6449a44c1ccSLoic Poulain
6459a44c1ccSLoic Poulain if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
6469a44c1ccSLoic Poulain return -ERESTARTSYS;
6479a44c1ccSLoic Poulain
6489a44c1ccSLoic Poulain return 0;
6499a44c1ccSLoic Poulain }
6509a44c1ccSLoic Poulain
wwan_port_fops_open(struct inode * inode,struct file * file)6519a44c1ccSLoic Poulain static int wwan_port_fops_open(struct inode *inode, struct file *file)
6529a44c1ccSLoic Poulain {
6539a44c1ccSLoic Poulain struct wwan_port *port;
6549a44c1ccSLoic Poulain int err = 0;
6559a44c1ccSLoic Poulain
6569a44c1ccSLoic Poulain port = wwan_port_get_by_minor(iminor(inode));
6579a44c1ccSLoic Poulain if (IS_ERR(port))
6589a44c1ccSLoic Poulain return PTR_ERR(port);
6599a44c1ccSLoic Poulain
6609a44c1ccSLoic Poulain file->private_data = port;
6619a44c1ccSLoic Poulain stream_open(inode, file);
6629a44c1ccSLoic Poulain
6639a44c1ccSLoic Poulain err = wwan_port_op_start(port);
6649a44c1ccSLoic Poulain if (err)
6659a44c1ccSLoic Poulain put_device(&port->dev);
6669a44c1ccSLoic Poulain
6679a44c1ccSLoic Poulain return err;
6689a44c1ccSLoic Poulain }
6699a44c1ccSLoic Poulain
wwan_port_fops_release(struct inode * inode,struct file * filp)6709a44c1ccSLoic Poulain static int wwan_port_fops_release(struct inode *inode, struct file *filp)
6719a44c1ccSLoic Poulain {
6729a44c1ccSLoic Poulain struct wwan_port *port = filp->private_data;
6739a44c1ccSLoic Poulain
6749a44c1ccSLoic Poulain wwan_port_op_stop(port);
6759a44c1ccSLoic Poulain put_device(&port->dev);
6769a44c1ccSLoic Poulain
6779a44c1ccSLoic Poulain return 0;
6789a44c1ccSLoic Poulain }
6799a44c1ccSLoic Poulain
wwan_port_fops_read(struct file * filp,char __user * buf,size_t count,loff_t * ppos)6809a44c1ccSLoic Poulain static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
6819a44c1ccSLoic Poulain size_t count, loff_t *ppos)
6829a44c1ccSLoic Poulain {
6839a44c1ccSLoic Poulain struct wwan_port *port = filp->private_data;
6849a44c1ccSLoic Poulain struct sk_buff *skb;
6859a44c1ccSLoic Poulain size_t copied;
6869a44c1ccSLoic Poulain int ret;
6879a44c1ccSLoic Poulain
6889a44c1ccSLoic Poulain ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
6899a44c1ccSLoic Poulain if (ret)
6909a44c1ccSLoic Poulain return ret;
6919a44c1ccSLoic Poulain
6929a44c1ccSLoic Poulain skb = skb_dequeue(&port->rxq);
6939a44c1ccSLoic Poulain if (!skb)
6949a44c1ccSLoic Poulain return -EIO;
6959a44c1ccSLoic Poulain
6969a44c1ccSLoic Poulain copied = min_t(size_t, count, skb->len);
6979a44c1ccSLoic Poulain if (copy_to_user(buf, skb->data, copied)) {
6989a44c1ccSLoic Poulain kfree_skb(skb);
6999a44c1ccSLoic Poulain return -EFAULT;
7009a44c1ccSLoic Poulain }
7019a44c1ccSLoic Poulain skb_pull(skb, copied);
7029a44c1ccSLoic Poulain
7039a44c1ccSLoic Poulain /* skb is not fully consumed, keep it in the queue */
7049a44c1ccSLoic Poulain if (skb->len)
7059a44c1ccSLoic Poulain skb_queue_head(&port->rxq, skb);
7069a44c1ccSLoic Poulain else
7079a44c1ccSLoic Poulain consume_skb(skb);
7089a44c1ccSLoic Poulain
7099a44c1ccSLoic Poulain return copied;
7109a44c1ccSLoic Poulain }
7119a44c1ccSLoic Poulain
wwan_port_fops_write(struct file * filp,const char __user * buf,size_t count,loff_t * offp)7129a44c1ccSLoic Poulain static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
7139a44c1ccSLoic Poulain size_t count, loff_t *offp)
7149a44c1ccSLoic Poulain {
71536bd28c1Shaozhe chang struct sk_buff *skb, *head = NULL, *tail = NULL;
7169a44c1ccSLoic Poulain struct wwan_port *port = filp->private_data;
71736bd28c1Shaozhe chang size_t frag_len, remain = count;
7189a44c1ccSLoic Poulain int ret;
7199a44c1ccSLoic Poulain
7209a44c1ccSLoic Poulain ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
7219a44c1ccSLoic Poulain if (ret)
7229a44c1ccSLoic Poulain return ret;
7239a44c1ccSLoic Poulain
72436bd28c1Shaozhe chang do {
72536bd28c1Shaozhe chang frag_len = min(remain, port->frag_len);
72636bd28c1Shaozhe chang skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL);
72736bd28c1Shaozhe chang if (!skb) {
72836bd28c1Shaozhe chang ret = -ENOMEM;
72936bd28c1Shaozhe chang goto freeskb;
73036bd28c1Shaozhe chang }
73136bd28c1Shaozhe chang skb_reserve(skb, port->headroom_len);
7329a44c1ccSLoic Poulain
73336bd28c1Shaozhe chang if (!head) {
73436bd28c1Shaozhe chang head = skb;
73536bd28c1Shaozhe chang } else if (!tail) {
73636bd28c1Shaozhe chang skb_shinfo(head)->frag_list = skb;
73736bd28c1Shaozhe chang tail = skb;
73836bd28c1Shaozhe chang } else {
73936bd28c1Shaozhe chang tail->next = skb;
74036bd28c1Shaozhe chang tail = skb;
7419a44c1ccSLoic Poulain }
7429a44c1ccSLoic Poulain
74336bd28c1Shaozhe chang if (copy_from_user(skb_put(skb, frag_len), buf + count - remain, frag_len)) {
74436bd28c1Shaozhe chang ret = -EFAULT;
74536bd28c1Shaozhe chang goto freeskb;
7469a44c1ccSLoic Poulain }
7479a44c1ccSLoic Poulain
74836bd28c1Shaozhe chang if (skb != head) {
74936bd28c1Shaozhe chang head->data_len += skb->len;
75036bd28c1Shaozhe chang head->len += skb->len;
75136bd28c1Shaozhe chang head->truesize += skb->truesize;
75236bd28c1Shaozhe chang }
75336bd28c1Shaozhe chang } while (remain -= frag_len);
75436bd28c1Shaozhe chang
75536bd28c1Shaozhe chang ret = wwan_port_op_tx(port, head, !!(filp->f_flags & O_NONBLOCK));
75636bd28c1Shaozhe chang if (!ret)
7579a44c1ccSLoic Poulain return count;
75836bd28c1Shaozhe chang
75936bd28c1Shaozhe chang freeskb:
76036bd28c1Shaozhe chang kfree_skb(head);
76136bd28c1Shaozhe chang return ret;
7629a44c1ccSLoic Poulain }
7639a44c1ccSLoic Poulain
wwan_port_fops_poll(struct file * filp,poll_table * wait)7649a44c1ccSLoic Poulain static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
7659a44c1ccSLoic Poulain {
7669a44c1ccSLoic Poulain struct wwan_port *port = filp->private_data;
7679a44c1ccSLoic Poulain __poll_t mask = 0;
7689a44c1ccSLoic Poulain
7699a44c1ccSLoic Poulain poll_wait(filp, &port->waitqueue, wait);
7709a44c1ccSLoic Poulain
77131c143f7SStephan Gerhold mutex_lock(&port->ops_lock);
77231c143f7SStephan Gerhold if (port->ops && port->ops->tx_poll)
77331c143f7SStephan Gerhold mask |= port->ops->tx_poll(port, filp, wait);
77431c143f7SStephan Gerhold else if (!is_write_blocked(port))
7759a44c1ccSLoic Poulain mask |= EPOLLOUT | EPOLLWRNORM;
7769a44c1ccSLoic Poulain if (!is_read_blocked(port))
7779a44c1ccSLoic Poulain mask |= EPOLLIN | EPOLLRDNORM;
77857e22247SLoic Poulain if (!port->ops)
77957e22247SLoic Poulain mask |= EPOLLHUP | EPOLLERR;
78031c143f7SStephan Gerhold mutex_unlock(&port->ops_lock);
7819a44c1ccSLoic Poulain
7829a44c1ccSLoic Poulain return mask;
7839a44c1ccSLoic Poulain }
7849a44c1ccSLoic Poulain
785c230035cSSergey Ryazanov /* Implements minimalistic stub terminal IOCTLs support */
wwan_port_fops_at_ioctl(struct wwan_port * port,unsigned int cmd,unsigned long arg)786c230035cSSergey Ryazanov static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
787c230035cSSergey Ryazanov unsigned long arg)
788c230035cSSergey Ryazanov {
789c230035cSSergey Ryazanov int ret = 0;
790c230035cSSergey Ryazanov
791c230035cSSergey Ryazanov mutex_lock(&port->data_lock);
792c230035cSSergey Ryazanov
793c230035cSSergey Ryazanov switch (cmd) {
794c230035cSSergey Ryazanov case TCFLSH:
795c230035cSSergey Ryazanov break;
796c230035cSSergey Ryazanov
797c230035cSSergey Ryazanov case TCGETS:
798c230035cSSergey Ryazanov if (copy_to_user((void __user *)arg, &port->at_data.termios,
799c230035cSSergey Ryazanov sizeof(struct termios)))
800c230035cSSergey Ryazanov ret = -EFAULT;
801c230035cSSergey Ryazanov break;
802c230035cSSergey Ryazanov
803c230035cSSergey Ryazanov case TCSETS:
804c230035cSSergey Ryazanov case TCSETSW:
805c230035cSSergey Ryazanov case TCSETSF:
806c230035cSSergey Ryazanov if (copy_from_user(&port->at_data.termios, (void __user *)arg,
807c230035cSSergey Ryazanov sizeof(struct termios)))
808c230035cSSergey Ryazanov ret = -EFAULT;
809c230035cSSergey Ryazanov break;
810c230035cSSergey Ryazanov
811c230035cSSergey Ryazanov #ifdef TCGETS2
812c230035cSSergey Ryazanov case TCGETS2:
813c230035cSSergey Ryazanov if (copy_to_user((void __user *)arg, &port->at_data.termios,
814c230035cSSergey Ryazanov sizeof(struct termios2)))
815c230035cSSergey Ryazanov ret = -EFAULT;
816c230035cSSergey Ryazanov break;
817c230035cSSergey Ryazanov
818c230035cSSergey Ryazanov case TCSETS2:
819c230035cSSergey Ryazanov case TCSETSW2:
820c230035cSSergey Ryazanov case TCSETSF2:
821c230035cSSergey Ryazanov if (copy_from_user(&port->at_data.termios, (void __user *)arg,
822c230035cSSergey Ryazanov sizeof(struct termios2)))
823c230035cSSergey Ryazanov ret = -EFAULT;
824c230035cSSergey Ryazanov break;
825c230035cSSergey Ryazanov #endif
826c230035cSSergey Ryazanov
827c230035cSSergey Ryazanov case TIOCMGET:
828c230035cSSergey Ryazanov ret = put_user(port->at_data.mdmbits, (int __user *)arg);
829c230035cSSergey Ryazanov break;
830c230035cSSergey Ryazanov
831c230035cSSergey Ryazanov case TIOCMSET:
832c230035cSSergey Ryazanov case TIOCMBIC:
833c230035cSSergey Ryazanov case TIOCMBIS: {
834c230035cSSergey Ryazanov int mdmbits;
835c230035cSSergey Ryazanov
836c230035cSSergey Ryazanov if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
837c230035cSSergey Ryazanov ret = -EFAULT;
838c230035cSSergey Ryazanov break;
839c230035cSSergey Ryazanov }
840c230035cSSergey Ryazanov if (cmd == TIOCMBIC)
841c230035cSSergey Ryazanov port->at_data.mdmbits &= ~mdmbits;
842c230035cSSergey Ryazanov else if (cmd == TIOCMBIS)
843c230035cSSergey Ryazanov port->at_data.mdmbits |= mdmbits;
844c230035cSSergey Ryazanov else
845c230035cSSergey Ryazanov port->at_data.mdmbits = mdmbits;
846c230035cSSergey Ryazanov break;
847c230035cSSergey Ryazanov }
848c230035cSSergey Ryazanov
849c230035cSSergey Ryazanov default:
850c230035cSSergey Ryazanov ret = -ENOIOCTLCMD;
851c230035cSSergey Ryazanov }
852c230035cSSergey Ryazanov
853c230035cSSergey Ryazanov mutex_unlock(&port->data_lock);
854c230035cSSergey Ryazanov
855c230035cSSergey Ryazanov return ret;
856c230035cSSergey Ryazanov }
857c230035cSSergey Ryazanov
wwan_port_fops_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)858e263c5b2SSergey Ryazanov static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
859e263c5b2SSergey Ryazanov unsigned long arg)
860e263c5b2SSergey Ryazanov {
861e263c5b2SSergey Ryazanov struct wwan_port *port = filp->private_data;
862c230035cSSergey Ryazanov int res;
863c230035cSSergey Ryazanov
864c230035cSSergey Ryazanov if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */
865c230035cSSergey Ryazanov res = wwan_port_fops_at_ioctl(port, cmd, arg);
866c230035cSSergey Ryazanov if (res != -ENOIOCTLCMD)
867c230035cSSergey Ryazanov return res;
868c230035cSSergey Ryazanov }
869e263c5b2SSergey Ryazanov
870e263c5b2SSergey Ryazanov switch (cmd) {
871e263c5b2SSergey Ryazanov case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
872e263c5b2SSergey Ryazanov unsigned long flags;
873e263c5b2SSergey Ryazanov struct sk_buff *skb;
874e263c5b2SSergey Ryazanov int amount = 0;
875e263c5b2SSergey Ryazanov
876e263c5b2SSergey Ryazanov spin_lock_irqsave(&port->rxq.lock, flags);
877e263c5b2SSergey Ryazanov skb_queue_walk(&port->rxq, skb)
878e263c5b2SSergey Ryazanov amount += skb->len;
879e263c5b2SSergey Ryazanov spin_unlock_irqrestore(&port->rxq.lock, flags);
880e263c5b2SSergey Ryazanov
881e263c5b2SSergey Ryazanov return put_user(amount, (int __user *)arg);
882e263c5b2SSergey Ryazanov }
883e263c5b2SSergey Ryazanov
884e263c5b2SSergey Ryazanov default:
885e263c5b2SSergey Ryazanov return -ENOIOCTLCMD;
886e263c5b2SSergey Ryazanov }
887e263c5b2SSergey Ryazanov }
888e263c5b2SSergey Ryazanov
8899a44c1ccSLoic Poulain static const struct file_operations wwan_port_fops = {
8909a44c1ccSLoic Poulain .owner = THIS_MODULE,
8919a44c1ccSLoic Poulain .open = wwan_port_fops_open,
8929a44c1ccSLoic Poulain .release = wwan_port_fops_release,
8939a44c1ccSLoic Poulain .read = wwan_port_fops_read,
8949a44c1ccSLoic Poulain .write = wwan_port_fops_write,
8959a44c1ccSLoic Poulain .poll = wwan_port_fops_poll,
896e263c5b2SSergey Ryazanov .unlocked_ioctl = wwan_port_fops_ioctl,
897e263c5b2SSergey Ryazanov #ifdef CONFIG_COMPAT
898e263c5b2SSergey Ryazanov .compat_ioctl = compat_ptr_ioctl,
899e263c5b2SSergey Ryazanov #endif
9009a44c1ccSLoic Poulain .llseek = noop_llseek,
9019a44c1ccSLoic Poulain };
9029a44c1ccSLoic Poulain
wwan_rtnl_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)90388b71053SJohannes Berg static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
90488b71053SJohannes Berg struct netlink_ext_ack *extack)
90588b71053SJohannes Berg {
90688b71053SJohannes Berg if (!data)
90788b71053SJohannes Berg return -EINVAL;
90888b71053SJohannes Berg
90988b71053SJohannes Berg if (!tb[IFLA_PARENT_DEV_NAME])
91088b71053SJohannes Berg return -EINVAL;
91188b71053SJohannes Berg
91288b71053SJohannes Berg if (!data[IFLA_WWAN_LINK_ID])
91388b71053SJohannes Berg return -EINVAL;
91488b71053SJohannes Berg
91588b71053SJohannes Berg return 0;
91688b71053SJohannes Berg }
91788b71053SJohannes Berg
91888b71053SJohannes Berg static struct device_type wwan_type = { .name = "wwan" };
91988b71053SJohannes Berg
wwan_rtnl_alloc(struct nlattr * tb[],const char * ifname,unsigned char name_assign_type,unsigned int num_tx_queues,unsigned int num_rx_queues)92088b71053SJohannes Berg static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],
92188b71053SJohannes Berg const char *ifname,
92288b71053SJohannes Berg unsigned char name_assign_type,
92388b71053SJohannes Berg unsigned int num_tx_queues,
92488b71053SJohannes Berg unsigned int num_rx_queues)
92588b71053SJohannes Berg {
92688b71053SJohannes Berg const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]);
92788b71053SJohannes Berg struct wwan_device *wwandev = wwan_dev_get_by_name(devname);
92888b71053SJohannes Berg struct net_device *dev;
92969940924SSergey Ryazanov unsigned int priv_size;
93088b71053SJohannes Berg
93188b71053SJohannes Berg if (IS_ERR(wwandev))
93288b71053SJohannes Berg return ERR_CAST(wwandev);
93388b71053SJohannes Berg
93488b71053SJohannes Berg /* only supported if ops were registered (not just ports) */
93588b71053SJohannes Berg if (!wwandev->ops) {
93688b71053SJohannes Berg dev = ERR_PTR(-EOPNOTSUPP);
93788b71053SJohannes Berg goto out;
93888b71053SJohannes Berg }
93988b71053SJohannes Berg
94069940924SSergey Ryazanov priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size;
94169940924SSergey Ryazanov dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type,
94288b71053SJohannes Berg wwandev->ops->setup, num_tx_queues, num_rx_queues);
94388b71053SJohannes Berg
94488b71053SJohannes Berg if (dev) {
94588b71053SJohannes Berg SET_NETDEV_DEV(dev, &wwandev->dev);
94688b71053SJohannes Berg SET_NETDEV_DEVTYPE(dev, &wwan_type);
94788b71053SJohannes Berg }
94888b71053SJohannes Berg
94988b71053SJohannes Berg out:
95088b71053SJohannes Berg /* release the reference */
95188b71053SJohannes Berg put_device(&wwandev->dev);
95288b71053SJohannes Berg return dev;
95388b71053SJohannes Berg }
95488b71053SJohannes Berg
wwan_rtnl_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)95588b71053SJohannes Berg static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev,
95688b71053SJohannes Berg struct nlattr *tb[], struct nlattr *data[],
95788b71053SJohannes Berg struct netlink_ext_ack *extack)
95888b71053SJohannes Berg {
95988b71053SJohannes Berg struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
96088b71053SJohannes Berg u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]);
96169940924SSergey Ryazanov struct wwan_netdev_priv *priv = netdev_priv(dev);
96288b71053SJohannes Berg int ret;
96388b71053SJohannes Berg
96488b71053SJohannes Berg if (IS_ERR(wwandev))
96588b71053SJohannes Berg return PTR_ERR(wwandev);
96688b71053SJohannes Berg
96788b71053SJohannes Berg /* shouldn't have a netdev (left) with us as parent so WARN */
96888b71053SJohannes Berg if (WARN_ON(!wwandev->ops)) {
96988b71053SJohannes Berg ret = -EOPNOTSUPP;
97088b71053SJohannes Berg goto out;
97188b71053SJohannes Berg }
97288b71053SJohannes Berg
97369940924SSergey Ryazanov priv->link_id = link_id;
97488b71053SJohannes Berg if (wwandev->ops->newlink)
97588b71053SJohannes Berg ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev,
97688b71053SJohannes Berg link_id, extack);
97788b71053SJohannes Berg else
97888b71053SJohannes Berg ret = register_netdevice(dev);
97988b71053SJohannes Berg
98088b71053SJohannes Berg out:
98188b71053SJohannes Berg /* release the reference */
98288b71053SJohannes Berg put_device(&wwandev->dev);
98388b71053SJohannes Berg return ret;
98488b71053SJohannes Berg }
98588b71053SJohannes Berg
wwan_rtnl_dellink(struct net_device * dev,struct list_head * head)98688b71053SJohannes Berg static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head)
98788b71053SJohannes Berg {
98888b71053SJohannes Berg struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
98988b71053SJohannes Berg
99088b71053SJohannes Berg if (IS_ERR(wwandev))
99188b71053SJohannes Berg return;
99288b71053SJohannes Berg
99388b71053SJohannes Berg /* shouldn't have a netdev (left) with us as parent so WARN */
99488b71053SJohannes Berg if (WARN_ON(!wwandev->ops))
99588b71053SJohannes Berg goto out;
99688b71053SJohannes Berg
99788b71053SJohannes Berg if (wwandev->ops->dellink)
99888b71053SJohannes Berg wwandev->ops->dellink(wwandev->ops_ctxt, dev, head);
99988b71053SJohannes Berg else
1000f492fccfSSergey Ryazanov unregister_netdevice_queue(dev, head);
100188b71053SJohannes Berg
100288b71053SJohannes Berg out:
100388b71053SJohannes Berg /* release the reference */
100488b71053SJohannes Berg put_device(&wwandev->dev);
100588b71053SJohannes Berg }
100688b71053SJohannes Berg
wwan_rtnl_get_size(const struct net_device * dev)100769940924SSergey Ryazanov static size_t wwan_rtnl_get_size(const struct net_device *dev)
100869940924SSergey Ryazanov {
100969940924SSergey Ryazanov return
101069940924SSergey Ryazanov nla_total_size(4) + /* IFLA_WWAN_LINK_ID */
101169940924SSergey Ryazanov 0;
101269940924SSergey Ryazanov }
101369940924SSergey Ryazanov
wwan_rtnl_fill_info(struct sk_buff * skb,const struct net_device * dev)101469940924SSergey Ryazanov static int wwan_rtnl_fill_info(struct sk_buff *skb,
101569940924SSergey Ryazanov const struct net_device *dev)
101669940924SSergey Ryazanov {
101769940924SSergey Ryazanov struct wwan_netdev_priv *priv = netdev_priv(dev);
101869940924SSergey Ryazanov
101969940924SSergey Ryazanov if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id))
102069940924SSergey Ryazanov goto nla_put_failure;
102169940924SSergey Ryazanov
102269940924SSergey Ryazanov return 0;
102369940924SSergey Ryazanov
102469940924SSergey Ryazanov nla_put_failure:
102569940924SSergey Ryazanov return -EMSGSIZE;
102669940924SSergey Ryazanov }
102769940924SSergey Ryazanov
102888b71053SJohannes Berg static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = {
102988b71053SJohannes Berg [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 },
103088b71053SJohannes Berg };
103188b71053SJohannes Berg
103288b71053SJohannes Berg static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = {
103388b71053SJohannes Berg .kind = "wwan",
103488b71053SJohannes Berg .maxtype = __IFLA_WWAN_MAX,
103588b71053SJohannes Berg .alloc = wwan_rtnl_alloc,
103688b71053SJohannes Berg .validate = wwan_rtnl_validate,
103788b71053SJohannes Berg .newlink = wwan_rtnl_newlink,
103888b71053SJohannes Berg .dellink = wwan_rtnl_dellink,
103969940924SSergey Ryazanov .get_size = wwan_rtnl_get_size,
104069940924SSergey Ryazanov .fill_info = wwan_rtnl_fill_info,
104188b71053SJohannes Berg .policy = wwan_rtnl_policy,
104288b71053SJohannes Berg };
104388b71053SJohannes Berg
wwan_create_default_link(struct wwan_device * wwandev,u32 def_link_id)1044ca374290SSergey Ryazanov static void wwan_create_default_link(struct wwan_device *wwandev,
1045ca374290SSergey Ryazanov u32 def_link_id)
1046ca374290SSergey Ryazanov {
1047ca374290SSergey Ryazanov struct nlattr *tb[IFLA_MAX + 1], *linkinfo[IFLA_INFO_MAX + 1];
1048ca374290SSergey Ryazanov struct nlattr *data[IFLA_WWAN_MAX + 1];
1049ca374290SSergey Ryazanov struct net_device *dev;
1050ca374290SSergey Ryazanov struct nlmsghdr *nlh;
1051ca374290SSergey Ryazanov struct sk_buff *msg;
1052ca374290SSergey Ryazanov
1053ca374290SSergey Ryazanov /* Forge attributes required to create a WWAN netdev. We first
1054ca374290SSergey Ryazanov * build a netlink message and then parse it. This looks
1055ca374290SSergey Ryazanov * odd, but such approach is less error prone.
1056ca374290SSergey Ryazanov */
1057ca374290SSergey Ryazanov msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1058ca374290SSergey Ryazanov if (WARN_ON(!msg))
1059ca374290SSergey Ryazanov return;
1060ca374290SSergey Ryazanov nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0);
1061ca374290SSergey Ryazanov if (WARN_ON(!nlh))
1062ca374290SSergey Ryazanov goto free_attrs;
1063ca374290SSergey Ryazanov
1064ca374290SSergey Ryazanov if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev)))
1065ca374290SSergey Ryazanov goto free_attrs;
1066ca374290SSergey Ryazanov tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO);
1067ca374290SSergey Ryazanov if (!tb[IFLA_LINKINFO])
1068ca374290SSergey Ryazanov goto free_attrs;
1069ca374290SSergey Ryazanov linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA);
1070ca374290SSergey Ryazanov if (!linkinfo[IFLA_INFO_DATA])
1071ca374290SSergey Ryazanov goto free_attrs;
1072ca374290SSergey Ryazanov if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id))
1073ca374290SSergey Ryazanov goto free_attrs;
1074ca374290SSergey Ryazanov nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]);
1075ca374290SSergey Ryazanov nla_nest_end(msg, tb[IFLA_LINKINFO]);
1076ca374290SSergey Ryazanov
1077ca374290SSergey Ryazanov nlmsg_end(msg, nlh);
1078ca374290SSergey Ryazanov
1079ca374290SSergey Ryazanov /* The next three parsing calls can not fail */
1080ca374290SSergey Ryazanov nlmsg_parse_deprecated(nlh, 0, tb, IFLA_MAX, NULL, NULL);
1081ca374290SSergey Ryazanov nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
1082ca374290SSergey Ryazanov NULL, NULL);
1083ca374290SSergey Ryazanov nla_parse_nested_deprecated(data, IFLA_WWAN_MAX,
1084ca374290SSergey Ryazanov linkinfo[IFLA_INFO_DATA], NULL, NULL);
1085ca374290SSergey Ryazanov
1086ca374290SSergey Ryazanov rtnl_lock();
1087ca374290SSergey Ryazanov
1088ca374290SSergey Ryazanov dev = rtnl_create_link(&init_net, "wwan%d", NET_NAME_ENUM,
1089ca374290SSergey Ryazanov &wwan_rtnl_link_ops, tb, NULL);
1090ca374290SSergey Ryazanov if (WARN_ON(IS_ERR(dev)))
1091ca374290SSergey Ryazanov goto unlock;
1092ca374290SSergey Ryazanov
1093ca374290SSergey Ryazanov if (WARN_ON(wwan_rtnl_newlink(&init_net, dev, tb, data, NULL))) {
1094ca374290SSergey Ryazanov free_netdev(dev);
1095ca374290SSergey Ryazanov goto unlock;
1096ca374290SSergey Ryazanov }
1097ca374290SSergey Ryazanov
10981d997f10SHangbin Liu rtnl_configure_link(dev, NULL, 0, NULL); /* Link initialized, notify new link */
10998cc236dbSLoic Poulain
1100ca374290SSergey Ryazanov unlock:
1101ca374290SSergey Ryazanov rtnl_unlock();
1102ca374290SSergey Ryazanov
1103ca374290SSergey Ryazanov free_attrs:
1104ca374290SSergey Ryazanov nlmsg_free(msg);
1105ca374290SSergey Ryazanov }
1106ca374290SSergey Ryazanov
1107355a4e7eSSergey Ryazanov /**
1108355a4e7eSSergey Ryazanov * wwan_register_ops - register WWAN device ops
1109355a4e7eSSergey Ryazanov * @parent: Device to use as parent and shared by all WWAN ports and
1110355a4e7eSSergey Ryazanov * created netdevs
1111355a4e7eSSergey Ryazanov * @ops: operations to register
1112355a4e7eSSergey Ryazanov * @ctxt: context to pass to operations
1113ca374290SSergey Ryazanov * @def_link_id: id of the default link that will be automatically created by
1114ca374290SSergey Ryazanov * the WWAN core for the WWAN device. The default link will not be created
1115ca374290SSergey Ryazanov * if the passed value is WWAN_NO_DEFAULT_LINK.
1116355a4e7eSSergey Ryazanov *
1117355a4e7eSSergey Ryazanov * Returns: 0 on success, a negative error code on failure
1118355a4e7eSSergey Ryazanov */
wwan_register_ops(struct device * parent,const struct wwan_ops * ops,void * ctxt,u32 def_link_id)1119355a4e7eSSergey Ryazanov int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
1120ca374290SSergey Ryazanov void *ctxt, u32 def_link_id)
1121355a4e7eSSergey Ryazanov {
1122355a4e7eSSergey Ryazanov struct wwan_device *wwandev;
1123355a4e7eSSergey Ryazanov
112458c3b421SSergey Ryazanov if (WARN_ON(!parent || !ops || !ops->setup))
1125355a4e7eSSergey Ryazanov return -EINVAL;
1126355a4e7eSSergey Ryazanov
1127355a4e7eSSergey Ryazanov wwandev = wwan_create_dev(parent);
1128d9d5b896SAndy Shevchenko if (IS_ERR(wwandev))
1129d9d5b896SAndy Shevchenko return PTR_ERR(wwandev);
1130355a4e7eSSergey Ryazanov
1131355a4e7eSSergey Ryazanov if (WARN_ON(wwandev->ops)) {
1132355a4e7eSSergey Ryazanov wwan_remove_dev(wwandev);
1133355a4e7eSSergey Ryazanov return -EBUSY;
1134355a4e7eSSergey Ryazanov }
1135355a4e7eSSergey Ryazanov
1136355a4e7eSSergey Ryazanov wwandev->ops = ops;
1137355a4e7eSSergey Ryazanov wwandev->ops_ctxt = ctxt;
1138355a4e7eSSergey Ryazanov
1139ca374290SSergey Ryazanov /* NB: we do not abort ops registration in case of default link
1140ca374290SSergey Ryazanov * creation failure. Link ops is the management interface, while the
1141ca374290SSergey Ryazanov * default link creation is a service option. And we should not prevent
1142ca374290SSergey Ryazanov * a user from manually creating a link latter if service option failed
1143ca374290SSergey Ryazanov * now.
1144ca374290SSergey Ryazanov */
1145ca374290SSergey Ryazanov if (def_link_id != WWAN_NO_DEFAULT_LINK)
1146ca374290SSergey Ryazanov wwan_create_default_link(wwandev, def_link_id);
1147ca374290SSergey Ryazanov
1148355a4e7eSSergey Ryazanov return 0;
1149355a4e7eSSergey Ryazanov }
1150355a4e7eSSergey Ryazanov EXPORT_SYMBOL_GPL(wwan_register_ops);
1151355a4e7eSSergey Ryazanov
11522f752380SSergey Ryazanov /* Enqueue child netdev deletion */
wwan_child_dellink(struct device * dev,void * data)11532f752380SSergey Ryazanov static int wwan_child_dellink(struct device *dev, void *data)
11542f752380SSergey Ryazanov {
11552f752380SSergey Ryazanov struct list_head *kill_list = data;
11562f752380SSergey Ryazanov
11572f752380SSergey Ryazanov if (dev->type == &wwan_type)
11582f752380SSergey Ryazanov wwan_rtnl_dellink(to_net_dev(dev), kill_list);
11592f752380SSergey Ryazanov
11602f752380SSergey Ryazanov return 0;
11612f752380SSergey Ryazanov }
11622f752380SSergey Ryazanov
1163355a4e7eSSergey Ryazanov /**
1164355a4e7eSSergey Ryazanov * wwan_unregister_ops - remove WWAN device ops
1165355a4e7eSSergey Ryazanov * @parent: Device to use as parent and shared by all WWAN ports and
1166355a4e7eSSergey Ryazanov * created netdevs
1167355a4e7eSSergey Ryazanov */
wwan_unregister_ops(struct device * parent)1168355a4e7eSSergey Ryazanov void wwan_unregister_ops(struct device *parent)
1169355a4e7eSSergey Ryazanov {
1170355a4e7eSSergey Ryazanov struct wwan_device *wwandev = wwan_dev_get_by_parent(parent);
11712f752380SSergey Ryazanov LIST_HEAD(kill_list);
1172355a4e7eSSergey Ryazanov
1173355a4e7eSSergey Ryazanov if (WARN_ON(IS_ERR(wwandev)))
1174355a4e7eSSergey Ryazanov return;
11752f752380SSergey Ryazanov if (WARN_ON(!wwandev->ops)) {
11762f752380SSergey Ryazanov put_device(&wwandev->dev);
11772f752380SSergey Ryazanov return;
11782f752380SSergey Ryazanov }
1179355a4e7eSSergey Ryazanov
1180355a4e7eSSergey Ryazanov /* put the reference obtained by wwan_dev_get_by_parent(),
1181355a4e7eSSergey Ryazanov * we should still have one (that the owner is giving back
11822f752380SSergey Ryazanov * now) due to the ops being assigned.
1183355a4e7eSSergey Ryazanov */
1184355a4e7eSSergey Ryazanov put_device(&wwandev->dev);
1185355a4e7eSSergey Ryazanov
11862f752380SSergey Ryazanov rtnl_lock(); /* Prevent concurent netdev(s) creation/destroying */
1187355a4e7eSSergey Ryazanov
11882f752380SSergey Ryazanov /* Remove all child netdev(s), using batch removing */
11892f752380SSergey Ryazanov device_for_each_child(&wwandev->dev, &kill_list,
11902f752380SSergey Ryazanov wwan_child_dellink);
11912f752380SSergey Ryazanov unregister_netdevice_many(&kill_list);
11922f752380SSergey Ryazanov
11932f752380SSergey Ryazanov wwandev->ops = NULL; /* Finally remove ops */
11942f752380SSergey Ryazanov
11952f752380SSergey Ryazanov rtnl_unlock();
11962f752380SSergey Ryazanov
1197355a4e7eSSergey Ryazanov wwandev->ops_ctxt = NULL;
1198355a4e7eSSergey Ryazanov wwan_remove_dev(wwandev);
1199355a4e7eSSergey Ryazanov }
1200355a4e7eSSergey Ryazanov EXPORT_SYMBOL_GPL(wwan_unregister_ops);
1201355a4e7eSSergey Ryazanov
wwan_init(void)12029a44c1ccSLoic Poulain static int __init wwan_init(void)
12039a44c1ccSLoic Poulain {
120488b71053SJohannes Berg int err;
120588b71053SJohannes Berg
120688b71053SJohannes Berg err = rtnl_link_register(&wwan_rtnl_link_ops);
120788b71053SJohannes Berg if (err)
120888b71053SJohannes Berg return err;
120988b71053SJohannes Berg
12109a44c1ccSLoic Poulain wwan_class = class_create("wwan");
121188b71053SJohannes Berg if (IS_ERR(wwan_class)) {
121288b71053SJohannes Berg err = PTR_ERR(wwan_class);
121388b71053SJohannes Berg goto unregister;
121488b71053SJohannes Berg }
12159a44c1ccSLoic Poulain
12169a44c1ccSLoic Poulain /* chrdev used for wwan ports */
121772eedfc4SSergey Ryazanov wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
121872eedfc4SSergey Ryazanov &wwan_port_fops);
12199a44c1ccSLoic Poulain if (wwan_major < 0) {
122088b71053SJohannes Berg err = wwan_major;
122188b71053SJohannes Berg goto destroy;
12229a44c1ccSLoic Poulain }
12239a44c1ccSLoic Poulain
1224283e6f5aSSergey Ryazanov #ifdef CONFIG_WWAN_DEBUGFS
1225c4804670SM Chetan Kumar wwan_debugfs_dir = debugfs_create_dir("wwan", NULL);
1226283e6f5aSSergey Ryazanov #endif
1227c4804670SM Chetan Kumar
12289a44c1ccSLoic Poulain return 0;
122988b71053SJohannes Berg
123088b71053SJohannes Berg destroy:
123188b71053SJohannes Berg class_destroy(wwan_class);
123288b71053SJohannes Berg unregister:
123388b71053SJohannes Berg rtnl_link_unregister(&wwan_rtnl_link_ops);
123488b71053SJohannes Berg return err;
12359a44c1ccSLoic Poulain }
12369a44c1ccSLoic Poulain
wwan_exit(void)12379a44c1ccSLoic Poulain static void __exit wwan_exit(void)
12389a44c1ccSLoic Poulain {
1239c4804670SM Chetan Kumar debugfs_remove_recursive(wwan_debugfs_dir);
124072eedfc4SSergey Ryazanov __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port");
124188b71053SJohannes Berg rtnl_link_unregister(&wwan_rtnl_link_ops);
12429a44c1ccSLoic Poulain class_destroy(wwan_class);
12439a44c1ccSLoic Poulain }
12449a44c1ccSLoic Poulain
12459a44c1ccSLoic Poulain module_init(wwan_init);
12469a44c1ccSLoic Poulain module_exit(wwan_exit);
12479a44c1ccSLoic Poulain
12489a44c1ccSLoic Poulain MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
12499a44c1ccSLoic Poulain MODULE_DESCRIPTION("WWAN core");
12509a44c1ccSLoic Poulain MODULE_LICENSE("GPL v2");
1251