1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/bitmap.h>
6 #include <linux/errno.h>
7 #include <linux/debugfs.h>
9 #include <linux/init.h>
10 #include <linux/idr.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/poll.h>
14 #include <linux/skbuff.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/uaccess.h>
18 #include <linux/termios.h>
19 #include <linux/wwan.h>
20 #include <net/rtnetlink.h>
21 #include <uapi/linux/wwan.h>
23 /* Maximum number of minors in use */
24 #define WWAN_MAX_MINORS (1 << MINORBITS)
26 static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
27 static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
28 static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */
29 static const struct class wwan_class = {
32 static int wwan_major;
33 static struct dentry *wwan_debugfs_dir;
35 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
36 #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
39 #define WWAN_PORT_TX_OFF 0
42 * struct wwan_device - The structure that defines a WWAN device
44 * @id: WWAN device unique ID.
45 * @dev: Underlying device.
46 * @port_id: Current available port ID to pick.
47 * @ops: wwan device ops
48 * @ops_ctxt: context to pass to ops
49 * @debugfs_dir: WWAN device debugfs dir
55 const struct wwan_ops *ops;
57 #ifdef CONFIG_WWAN_DEBUGFS
58 struct dentry *debugfs_dir;
63 * struct wwan_port - The structure that defines a WWAN port
65 * @start_count: Port start counter
66 * @flags: Store port state and capabilities
67 * @ops: Pointer to WWAN port operations
68 * @ops_lock: Protect port ops
69 * @dev: Underlying device
70 * @rxq: Buffer inbound queue
71 * @waitqueue: The waitqueue for port fops (read/write/poll)
72 * @data_lock: Port specific data access serialization
73 * @headroom_len: SKB reserved headroom size
74 * @frag_len: Length to fragment packet
75 * @at_data: AT port specific data
78 enum wwan_port_type type;
79 unsigned int start_count;
81 const struct wwan_port_ops *ops;
82 struct mutex ops_lock; /* Serialize ops + protect against removal */
84 struct sk_buff_head rxq;
85 wait_queue_head_t waitqueue;
86 struct mutex data_lock; /* Port specific data access serialization */
91 struct ktermios termios;
97 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
99 struct wwan_device *wwan = to_wwan_dev(dev);
101 return sprintf(buf, "%d\n", wwan->id);
103 static DEVICE_ATTR_RO(index);
105 static struct attribute *wwan_dev_attrs[] = {
106 &dev_attr_index.attr,
109 ATTRIBUTE_GROUPS(wwan_dev);
111 static void wwan_dev_destroy(struct device *dev)
113 struct wwan_device *wwandev = to_wwan_dev(dev);
115 ida_free(&wwan_dev_ids, wwandev->id);
119 static const struct device_type wwan_dev_type = {
121 .release = wwan_dev_destroy,
122 .groups = wwan_dev_groups,
125 static int wwan_dev_parent_match(struct device *dev, const void *parent)
127 return (dev->type == &wwan_dev_type &&
128 (dev->parent == parent || dev == parent));
131 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
135 dev = class_find_device(&wwan_class, NULL, parent, wwan_dev_parent_match);
137 return ERR_PTR(-ENODEV);
139 return to_wwan_dev(dev);
142 static int wwan_dev_name_match(struct device *dev, const void *name)
144 return dev->type == &wwan_dev_type &&
145 strcmp(dev_name(dev), name) == 0;
148 static struct wwan_device *wwan_dev_get_by_name(const char *name)
152 dev = class_find_device(&wwan_class, NULL, name, wwan_dev_name_match);
154 return ERR_PTR(-ENODEV);
156 return to_wwan_dev(dev);
159 #ifdef CONFIG_WWAN_DEBUGFS
160 struct dentry *wwan_get_debugfs_dir(struct device *parent)
162 struct wwan_device *wwandev;
164 wwandev = wwan_dev_get_by_parent(parent);
166 return ERR_CAST(wwandev);
168 return wwandev->debugfs_dir;
170 EXPORT_SYMBOL_GPL(wwan_get_debugfs_dir);
172 static int wwan_dev_debugfs_match(struct device *dev, const void *dir)
174 struct wwan_device *wwandev;
176 if (dev->type != &wwan_dev_type)
179 wwandev = to_wwan_dev(dev);
181 return wwandev->debugfs_dir == dir;
184 static struct wwan_device *wwan_dev_get_by_debugfs(struct dentry *dir)
188 dev = class_find_device(&wwan_class, NULL, dir, wwan_dev_debugfs_match);
190 return ERR_PTR(-ENODEV);
192 return to_wwan_dev(dev);
195 void wwan_put_debugfs_dir(struct dentry *dir)
197 struct wwan_device *wwandev = wwan_dev_get_by_debugfs(dir);
199 if (WARN_ON(IS_ERR(wwandev)))
202 /* wwan_dev_get_by_debugfs() also got a reference */
203 put_device(&wwandev->dev);
204 put_device(&wwandev->dev);
206 EXPORT_SYMBOL_GPL(wwan_put_debugfs_dir);
209 /* This function allocates and registers a new WWAN device OR if a WWAN device
210 * already exist for the given parent, it gets a reference and return it.
211 * This function is not exported (for now), it is called indirectly via
212 * wwan_create_port().
214 static struct wwan_device *wwan_create_dev(struct device *parent)
216 struct wwan_device *wwandev;
219 /* The 'find-alloc-register' operation must be protected against
220 * concurrent execution, a WWAN device is possibly shared between
221 * multiple callers or concurrently unregistered from wwan_remove_dev().
223 mutex_lock(&wwan_register_lock);
225 /* If wwandev already exists, return it */
226 wwandev = wwan_dev_get_by_parent(parent);
227 if (!IS_ERR(wwandev))
230 id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
232 wwandev = ERR_PTR(id);
236 wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL);
238 wwandev = ERR_PTR(-ENOMEM);
239 ida_free(&wwan_dev_ids, id);
243 wwandev->dev.parent = parent;
244 wwandev->dev.class = &wwan_class;
245 wwandev->dev.type = &wwan_dev_type;
247 dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
249 err = device_register(&wwandev->dev);
251 put_device(&wwandev->dev);
252 wwandev = ERR_PTR(err);
256 #ifdef CONFIG_WWAN_DEBUGFS
257 wwandev->debugfs_dir =
258 debugfs_create_dir(kobject_name(&wwandev->dev.kobj),
263 mutex_unlock(&wwan_register_lock);
268 static int is_wwan_child(struct device *dev, void *data)
270 return dev->class == &wwan_class;
273 static void wwan_remove_dev(struct wwan_device *wwandev)
277 /* Prevent concurrent picking from wwan_create_dev */
278 mutex_lock(&wwan_register_lock);
280 /* WWAN device is created and registered (get+add) along with its first
281 * child port, and subsequent port registrations only grab a reference
282 * (get). The WWAN device must then be unregistered (del+put) along with
283 * its last port, and reference simply dropped (put) otherwise. In the
284 * same fashion, we must not unregister it when the ops are still there.
289 ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child);
292 #ifdef CONFIG_WWAN_DEBUGFS
293 debugfs_remove_recursive(wwandev->debugfs_dir);
295 device_unregister(&wwandev->dev);
297 put_device(&wwandev->dev);
300 mutex_unlock(&wwan_register_lock);
303 /* ------- WWAN port management ------- */
305 static const struct {
306 const char * const name; /* Port type name */
307 const char * const devsuf; /* Port device name suffix */
308 } wwan_port_types[WWAN_PORT_MAX + 1] = {
325 [WWAN_PORT_FIREHOSE] = {
327 .devsuf = "firehose",
329 [WWAN_PORT_XMMRPC] = {
333 [WWAN_PORT_FASTBOOT] = {
335 .devsuf = "fastboot",
347 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
350 struct wwan_port *port = to_wwan_port(dev);
352 return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
354 static DEVICE_ATTR_RO(type);
356 static struct attribute *wwan_port_attrs[] = {
360 ATTRIBUTE_GROUPS(wwan_port);
362 static void wwan_port_destroy(struct device *dev)
364 struct wwan_port *port = to_wwan_port(dev);
366 ida_free(&minors, MINOR(port->dev.devt));
367 mutex_destroy(&port->data_lock);
368 mutex_destroy(&port->ops_lock);
372 static const struct device_type wwan_port_dev_type = {
374 .release = wwan_port_destroy,
375 .groups = wwan_port_groups,
378 static int wwan_port_minor_match(struct device *dev, const void *minor)
380 return (dev->type == &wwan_port_dev_type &&
381 MINOR(dev->devt) == *(unsigned int *)minor);
384 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
388 dev = class_find_device(&wwan_class, NULL, &minor, wwan_port_minor_match);
390 return ERR_PTR(-ENODEV);
392 return to_wwan_port(dev);
395 /* Allocate and set unique name based on passed format
397 * Name allocation approach is highly inspired by the __dev_alloc_name()
400 * To avoid names collision, the caller must prevent the new port device
401 * registration as well as concurrent invocation of this function.
403 static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
405 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
406 const unsigned int max_ports = PAGE_SIZE * 8;
407 struct class_dev_iter iter;
408 unsigned long *idmap;
413 idmap = bitmap_zalloc(max_ports, GFP_KERNEL);
417 /* Collect ids of same name format ports */
418 class_dev_iter_init(&iter, &wwan_class, NULL, &wwan_port_dev_type);
419 while ((dev = class_dev_iter_next(&iter))) {
420 if (dev->parent != &wwandev->dev)
422 if (sscanf(dev_name(dev), fmt, &id) != 1)
424 if (id < 0 || id >= max_ports)
428 class_dev_iter_exit(&iter);
430 /* Allocate unique id */
431 id = find_first_zero_bit(idmap, max_ports);
434 snprintf(buf, sizeof(buf), fmt, id); /* Name generation */
436 dev = device_find_child_by_name(&wwandev->dev, buf);
442 return dev_set_name(&port->dev, "%s", buf);
445 struct wwan_port *wwan_create_port(struct device *parent,
446 enum wwan_port_type type,
447 const struct wwan_port_ops *ops,
448 struct wwan_port_caps *caps,
451 struct wwan_device *wwandev;
452 struct wwan_port *port;
456 if (type > WWAN_PORT_MAX || !ops)
457 return ERR_PTR(-EINVAL);
459 /* A port is always a child of a WWAN device, retrieve (allocate or
460 * pick) the WWAN device based on the provided parent device.
462 wwandev = wwan_create_dev(parent);
464 return ERR_CAST(wwandev);
466 /* A port is exposed as character device, get a minor */
467 minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
470 goto error_wwandev_remove;
473 port = kzalloc(sizeof(*port), GFP_KERNEL);
476 ida_free(&minors, minor);
477 goto error_wwandev_remove;
482 port->frag_len = caps ? caps->frag_len : SIZE_MAX;
483 port->headroom_len = caps ? caps->headroom_len : 0;
484 mutex_init(&port->ops_lock);
485 skb_queue_head_init(&port->rxq);
486 init_waitqueue_head(&port->waitqueue);
487 mutex_init(&port->data_lock);
489 port->dev.parent = &wwandev->dev;
490 port->dev.class = &wwan_class;
491 port->dev.type = &wwan_port_dev_type;
492 port->dev.devt = MKDEV(wwan_major, minor);
493 dev_set_drvdata(&port->dev, drvdata);
495 /* allocate unique name based on wwan device id, port type and number */
496 snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
497 wwan_port_types[port->type].devsuf);
499 /* Serialize ports registration */
500 mutex_lock(&wwan_register_lock);
502 __wwan_port_dev_assign_name(port, namefmt);
503 err = device_register(&port->dev);
505 mutex_unlock(&wwan_register_lock);
508 goto error_put_device;
510 dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev));
514 put_device(&port->dev);
515 error_wwandev_remove:
516 wwan_remove_dev(wwandev);
520 EXPORT_SYMBOL_GPL(wwan_create_port);
522 void wwan_remove_port(struct wwan_port *port)
524 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
526 mutex_lock(&port->ops_lock);
527 if (port->start_count)
528 port->ops->stop(port);
529 port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
530 mutex_unlock(&port->ops_lock);
532 wake_up_interruptible(&port->waitqueue);
534 skb_queue_purge(&port->rxq);
535 dev_set_drvdata(&port->dev, NULL);
537 dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&port->dev));
538 device_unregister(&port->dev);
540 /* Release related wwan device */
541 wwan_remove_dev(wwandev);
543 EXPORT_SYMBOL_GPL(wwan_remove_port);
545 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
547 skb_queue_tail(&port->rxq, skb);
548 wake_up_interruptible(&port->waitqueue);
550 EXPORT_SYMBOL_GPL(wwan_port_rx);
552 void wwan_port_txon(struct wwan_port *port)
554 clear_bit(WWAN_PORT_TX_OFF, &port->flags);
555 wake_up_interruptible(&port->waitqueue);
557 EXPORT_SYMBOL_GPL(wwan_port_txon);
559 void wwan_port_txoff(struct wwan_port *port)
561 set_bit(WWAN_PORT_TX_OFF, &port->flags);
563 EXPORT_SYMBOL_GPL(wwan_port_txoff);
565 void *wwan_port_get_drvdata(struct wwan_port *port)
567 return dev_get_drvdata(&port->dev);
569 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
571 static int wwan_port_op_start(struct wwan_port *port)
575 mutex_lock(&port->ops_lock);
576 if (!port->ops) { /* Port got unplugged */
581 /* If port is already started, don't start again */
582 if (!port->start_count)
583 ret = port->ops->start(port);
589 mutex_unlock(&port->ops_lock);
594 static void wwan_port_op_stop(struct wwan_port *port)
596 mutex_lock(&port->ops_lock);
598 if (!port->start_count) {
600 port->ops->stop(port);
601 skb_queue_purge(&port->rxq);
603 mutex_unlock(&port->ops_lock);
606 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
611 mutex_lock(&port->ops_lock);
612 if (!port->ops) { /* Port got unplugged */
617 if (nonblock || !port->ops->tx_blocking)
618 ret = port->ops->tx(port, skb);
620 ret = port->ops->tx_blocking(port, skb);
623 mutex_unlock(&port->ops_lock);
628 static bool is_read_blocked(struct wwan_port *port)
630 return skb_queue_empty(&port->rxq) && port->ops;
633 static bool is_write_blocked(struct wwan_port *port)
635 return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
638 static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
640 if (!is_read_blocked(port))
646 if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
652 static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
654 if (!is_write_blocked(port))
660 if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
666 static int wwan_port_fops_open(struct inode *inode, struct file *file)
668 struct wwan_port *port;
671 port = wwan_port_get_by_minor(iminor(inode));
673 return PTR_ERR(port);
675 file->private_data = port;
676 stream_open(inode, file);
678 err = wwan_port_op_start(port);
680 put_device(&port->dev);
685 static int wwan_port_fops_release(struct inode *inode, struct file *filp)
687 struct wwan_port *port = filp->private_data;
689 wwan_port_op_stop(port);
690 put_device(&port->dev);
695 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
696 size_t count, loff_t *ppos)
698 struct wwan_port *port = filp->private_data;
703 ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
707 skb = skb_dequeue(&port->rxq);
711 copied = min_t(size_t, count, skb->len);
712 if (copy_to_user(buf, skb->data, copied)) {
716 skb_pull(skb, copied);
718 /* skb is not fully consumed, keep it in the queue */
720 skb_queue_head(&port->rxq, skb);
727 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
728 size_t count, loff_t *offp)
730 struct sk_buff *skb, *head = NULL, *tail = NULL;
731 struct wwan_port *port = filp->private_data;
732 size_t frag_len, remain = count;
735 ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
740 frag_len = min(remain, port->frag_len);
741 skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL);
746 skb_reserve(skb, port->headroom_len);
751 skb_shinfo(head)->frag_list = skb;
758 if (copy_from_user(skb_put(skb, frag_len), buf + count - remain, frag_len)) {
764 head->data_len += skb->len;
765 head->len += skb->len;
766 head->truesize += skb->truesize;
768 } while (remain -= frag_len);
770 ret = wwan_port_op_tx(port, head, !!(filp->f_flags & O_NONBLOCK));
779 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
781 struct wwan_port *port = filp->private_data;
784 poll_wait(filp, &port->waitqueue, wait);
786 mutex_lock(&port->ops_lock);
787 if (port->ops && port->ops->tx_poll)
788 mask |= port->ops->tx_poll(port, filp, wait);
789 else if (!is_write_blocked(port))
790 mask |= EPOLLOUT | EPOLLWRNORM;
791 if (!is_read_blocked(port))
792 mask |= EPOLLIN | EPOLLRDNORM;
794 mask |= EPOLLHUP | EPOLLERR;
795 mutex_unlock(&port->ops_lock);
800 /* Implements minimalistic stub terminal IOCTLs support */
801 static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
806 mutex_lock(&port->data_lock);
813 if (copy_to_user((void __user *)arg, &port->at_data.termios,
814 sizeof(struct termios)))
821 if (copy_from_user(&port->at_data.termios, (void __user *)arg,
822 sizeof(struct termios)))
828 if (copy_to_user((void __user *)arg, &port->at_data.termios,
829 sizeof(struct termios2)))
836 if (copy_from_user(&port->at_data.termios, (void __user *)arg,
837 sizeof(struct termios2)))
843 ret = put_user(port->at_data.mdmbits, (int __user *)arg);
851 if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
856 port->at_data.mdmbits &= ~mdmbits;
857 else if (cmd == TIOCMBIS)
858 port->at_data.mdmbits |= mdmbits;
860 port->at_data.mdmbits = mdmbits;
868 mutex_unlock(&port->data_lock);
873 static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
876 struct wwan_port *port = filp->private_data;
879 if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */
880 res = wwan_port_fops_at_ioctl(port, cmd, arg);
881 if (res != -ENOIOCTLCMD)
886 case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
891 spin_lock_irqsave(&port->rxq.lock, flags);
892 skb_queue_walk(&port->rxq, skb)
894 spin_unlock_irqrestore(&port->rxq.lock, flags);
896 return put_user(amount, (int __user *)arg);
904 static const struct file_operations wwan_port_fops = {
905 .owner = THIS_MODULE,
906 .open = wwan_port_fops_open,
907 .release = wwan_port_fops_release,
908 .read = wwan_port_fops_read,
909 .write = wwan_port_fops_write,
910 .poll = wwan_port_fops_poll,
911 .unlocked_ioctl = wwan_port_fops_ioctl,
913 .compat_ioctl = compat_ptr_ioctl,
915 .llseek = noop_llseek,
918 static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
919 struct netlink_ext_ack *extack)
924 if (!tb[IFLA_PARENT_DEV_NAME])
927 if (!data[IFLA_WWAN_LINK_ID])
933 static const struct device_type wwan_type = { .name = "wwan" };
935 static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],
937 unsigned char name_assign_type,
938 unsigned int num_tx_queues,
939 unsigned int num_rx_queues)
941 const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]);
942 struct wwan_device *wwandev = wwan_dev_get_by_name(devname);
943 struct net_device *dev;
944 unsigned int priv_size;
947 return ERR_CAST(wwandev);
949 /* only supported if ops were registered (not just ports) */
951 dev = ERR_PTR(-EOPNOTSUPP);
955 priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size;
956 dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type,
957 wwandev->ops->setup, num_tx_queues, num_rx_queues);
960 SET_NETDEV_DEV(dev, &wwandev->dev);
961 SET_NETDEV_DEVTYPE(dev, &wwan_type);
965 /* release the reference */
966 put_device(&wwandev->dev);
970 static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev,
971 struct nlattr *tb[], struct nlattr *data[],
972 struct netlink_ext_ack *extack)
974 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
975 u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]);
976 struct wwan_netdev_priv *priv = netdev_priv(dev);
980 return PTR_ERR(wwandev);
982 /* shouldn't have a netdev (left) with us as parent so WARN */
983 if (WARN_ON(!wwandev->ops)) {
988 priv->link_id = link_id;
989 if (wwandev->ops->newlink)
990 ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev,
993 ret = register_netdevice(dev);
996 /* release the reference */
997 put_device(&wwandev->dev);
1001 static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head)
1003 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
1005 if (IS_ERR(wwandev))
1008 /* shouldn't have a netdev (left) with us as parent so WARN */
1009 if (WARN_ON(!wwandev->ops))
1012 if (wwandev->ops->dellink)
1013 wwandev->ops->dellink(wwandev->ops_ctxt, dev, head);
1015 unregister_netdevice_queue(dev, head);
1018 /* release the reference */
1019 put_device(&wwandev->dev);
1022 static size_t wwan_rtnl_get_size(const struct net_device *dev)
1025 nla_total_size(4) + /* IFLA_WWAN_LINK_ID */
1029 static int wwan_rtnl_fill_info(struct sk_buff *skb,
1030 const struct net_device *dev)
1032 struct wwan_netdev_priv *priv = netdev_priv(dev);
1034 if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id))
1035 goto nla_put_failure;
1043 static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = {
1044 [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 },
1047 static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = {
1049 .maxtype = IFLA_WWAN_MAX,
1050 .alloc = wwan_rtnl_alloc,
1051 .validate = wwan_rtnl_validate,
1052 .newlink = wwan_rtnl_newlink,
1053 .dellink = wwan_rtnl_dellink,
1054 .get_size = wwan_rtnl_get_size,
1055 .fill_info = wwan_rtnl_fill_info,
1056 .policy = wwan_rtnl_policy,
1059 static void wwan_create_default_link(struct wwan_device *wwandev,
1062 struct nlattr *tb[IFLA_MAX + 1], *linkinfo[IFLA_INFO_MAX + 1];
1063 struct nlattr *data[IFLA_WWAN_MAX + 1];
1064 struct net_device *dev;
1065 struct nlmsghdr *nlh;
1066 struct sk_buff *msg;
1068 /* Forge attributes required to create a WWAN netdev. We first
1069 * build a netlink message and then parse it. This looks
1070 * odd, but such approach is less error prone.
1072 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1075 nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0);
1079 if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev)))
1081 tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO);
1082 if (!tb[IFLA_LINKINFO])
1084 linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA);
1085 if (!linkinfo[IFLA_INFO_DATA])
1087 if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id))
1089 nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]);
1090 nla_nest_end(msg, tb[IFLA_LINKINFO]);
1092 nlmsg_end(msg, nlh);
1094 /* The next three parsing calls can not fail */
1095 nlmsg_parse_deprecated(nlh, 0, tb, IFLA_MAX, NULL, NULL);
1096 nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
1098 nla_parse_nested_deprecated(data, IFLA_WWAN_MAX,
1099 linkinfo[IFLA_INFO_DATA], NULL, NULL);
1103 dev = rtnl_create_link(&init_net, "wwan%d", NET_NAME_ENUM,
1104 &wwan_rtnl_link_ops, tb, NULL);
1105 if (WARN_ON(IS_ERR(dev)))
1108 if (WARN_ON(wwan_rtnl_newlink(&init_net, dev, tb, data, NULL))) {
1113 rtnl_configure_link(dev, NULL, 0, NULL); /* Link initialized, notify new link */
1123 * wwan_register_ops - register WWAN device ops
1124 * @parent: Device to use as parent and shared by all WWAN ports and
1126 * @ops: operations to register
1127 * @ctxt: context to pass to operations
1128 * @def_link_id: id of the default link that will be automatically created by
1129 * the WWAN core for the WWAN device. The default link will not be created
1130 * if the passed value is WWAN_NO_DEFAULT_LINK.
1132 * Returns: 0 on success, a negative error code on failure
1134 int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
1135 void *ctxt, u32 def_link_id)
1137 struct wwan_device *wwandev;
1139 if (WARN_ON(!parent || !ops || !ops->setup))
1142 wwandev = wwan_create_dev(parent);
1143 if (IS_ERR(wwandev))
1144 return PTR_ERR(wwandev);
1146 if (WARN_ON(wwandev->ops)) {
1147 wwan_remove_dev(wwandev);
1152 wwandev->ops_ctxt = ctxt;
1154 /* NB: we do not abort ops registration in case of default link
1155 * creation failure. Link ops is the management interface, while the
1156 * default link creation is a service option. And we should not prevent
1157 * a user from manually creating a link latter if service option failed
1160 if (def_link_id != WWAN_NO_DEFAULT_LINK)
1161 wwan_create_default_link(wwandev, def_link_id);
1165 EXPORT_SYMBOL_GPL(wwan_register_ops);
1167 /* Enqueue child netdev deletion */
1168 static int wwan_child_dellink(struct device *dev, void *data)
1170 struct list_head *kill_list = data;
1172 if (dev->type == &wwan_type)
1173 wwan_rtnl_dellink(to_net_dev(dev), kill_list);
1179 * wwan_unregister_ops - remove WWAN device ops
1180 * @parent: Device to use as parent and shared by all WWAN ports and
1183 void wwan_unregister_ops(struct device *parent)
1185 struct wwan_device *wwandev = wwan_dev_get_by_parent(parent);
1186 LIST_HEAD(kill_list);
1188 if (WARN_ON(IS_ERR(wwandev)))
1190 if (WARN_ON(!wwandev->ops)) {
1191 put_device(&wwandev->dev);
1195 /* put the reference obtained by wwan_dev_get_by_parent(),
1196 * we should still have one (that the owner is giving back
1197 * now) due to the ops being assigned.
1199 put_device(&wwandev->dev);
1201 rtnl_lock(); /* Prevent concurrent netdev(s) creation/destroying */
1203 /* Remove all child netdev(s), using batch removing */
1204 device_for_each_child(&wwandev->dev, &kill_list,
1205 wwan_child_dellink);
1206 unregister_netdevice_many(&kill_list);
1208 wwandev->ops = NULL; /* Finally remove ops */
1212 wwandev->ops_ctxt = NULL;
1213 wwan_remove_dev(wwandev);
1215 EXPORT_SYMBOL_GPL(wwan_unregister_ops);
1217 static int __init wwan_init(void)
1221 err = rtnl_link_register(&wwan_rtnl_link_ops);
1225 err = class_register(&wwan_class);
1229 /* chrdev used for wwan ports */
1230 wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
1232 if (wwan_major < 0) {
1237 #ifdef CONFIG_WWAN_DEBUGFS
1238 wwan_debugfs_dir = debugfs_create_dir("wwan", NULL);
1244 class_unregister(&wwan_class);
1246 rtnl_link_unregister(&wwan_rtnl_link_ops);
1250 static void __exit wwan_exit(void)
1252 debugfs_remove_recursive(wwan_debugfs_dir);
1253 __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port");
1254 rtnl_link_unregister(&wwan_rtnl_link_ops);
1255 class_unregister(&wwan_class);
1258 module_init(wwan_init);
1259 module_exit(wwan_exit);
1262 MODULE_DESCRIPTION("WWAN core");
1263 MODULE_LICENSE("GPL v2");