1 // SPDX-License-Identifier: GPL-2.0-only
5 #include <linux/errno.h>
6 #include <linux/debugfs.h>
8 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/poll.h>
13 #include <linux/skbuff.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/termios.h>
17 #include <linux/wwan.h>
18 #include <net/rtnetlink.h>
19 #include <uapi/linux/wwan.h>
21 /* Maximum number of minors in use */
22 #define WWAN_MAX_MINORS (1 << MINORBITS)
24 static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
25 static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
26 static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */
27 static struct class *wwan_class;
28 static int wwan_major;
29 static struct dentry *wwan_debugfs_dir;
31 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
32 #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
35 #define WWAN_PORT_TX_OFF 0
38 * struct wwan_device - The structure that defines a WWAN device
40 * @id: WWAN device unique ID.
41 * @dev: Underlying device.
42 * @port_id: Current available port ID to pick.
43 * @ops: wwan device ops
44 * @ops_ctxt: context to pass to ops
45 * @debugfs_dir: WWAN device debugfs dir
51 const struct wwan_ops *ops;
53 #ifdef CONFIG_WWAN_DEBUGFS
54 struct dentry *debugfs_dir;
59 * struct wwan_port - The structure that defines a WWAN port
61 * @start_count: Port start counter
62 * @flags: Store port state and capabilities
63 * @ops: Pointer to WWAN port operations
64 * @ops_lock: Protect port ops
65 * @dev: Underlying device
66 * @rxq: Buffer inbound queue
67 * @waitqueue: The waitqueue for port fops (read/write/poll)
68 * @data_lock: Port specific data access serialization
69 * @at_data: AT port specific data
72 enum wwan_port_type type;
73 unsigned int start_count;
75 const struct wwan_port_ops *ops;
76 struct mutex ops_lock; /* Serialize ops + protect against removal */
78 struct sk_buff_head rxq;
79 wait_queue_head_t waitqueue;
80 struct mutex data_lock; /* Port specific data access serialization */
83 struct ktermios termios;
89 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
91 struct wwan_device *wwan = to_wwan_dev(dev);
93 return sprintf(buf, "%d\n", wwan->id);
95 static DEVICE_ATTR_RO(index);
97 static struct attribute *wwan_dev_attrs[] = {
101 ATTRIBUTE_GROUPS(wwan_dev);
103 static void wwan_dev_destroy(struct device *dev)
105 struct wwan_device *wwandev = to_wwan_dev(dev);
107 ida_free(&wwan_dev_ids, wwandev->id);
111 static const struct device_type wwan_dev_type = {
113 .release = wwan_dev_destroy,
114 .groups = wwan_dev_groups,
117 static int wwan_dev_parent_match(struct device *dev, const void *parent)
119 return (dev->type == &wwan_dev_type &&
120 (dev->parent == parent || dev == parent));
123 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
127 dev = class_find_device(wwan_class, NULL, parent, wwan_dev_parent_match);
129 return ERR_PTR(-ENODEV);
131 return to_wwan_dev(dev);
134 static int wwan_dev_name_match(struct device *dev, const void *name)
136 return dev->type == &wwan_dev_type &&
137 strcmp(dev_name(dev), name) == 0;
140 static struct wwan_device *wwan_dev_get_by_name(const char *name)
144 dev = class_find_device(wwan_class, NULL, name, wwan_dev_name_match);
146 return ERR_PTR(-ENODEV);
148 return to_wwan_dev(dev);
151 #ifdef CONFIG_WWAN_DEBUGFS
152 struct dentry *wwan_get_debugfs_dir(struct device *parent)
154 struct wwan_device *wwandev;
156 wwandev = wwan_dev_get_by_parent(parent);
158 return ERR_CAST(wwandev);
160 return wwandev->debugfs_dir;
162 EXPORT_SYMBOL_GPL(wwan_get_debugfs_dir);
164 static int wwan_dev_debugfs_match(struct device *dev, const void *dir)
166 struct wwan_device *wwandev;
168 if (dev->type != &wwan_dev_type)
171 wwandev = to_wwan_dev(dev);
173 return wwandev->debugfs_dir == dir;
176 static struct wwan_device *wwan_dev_get_by_debugfs(struct dentry *dir)
180 dev = class_find_device(wwan_class, NULL, dir, wwan_dev_debugfs_match);
182 return ERR_PTR(-ENODEV);
184 return to_wwan_dev(dev);
187 void wwan_put_debugfs_dir(struct dentry *dir)
189 struct wwan_device *wwandev = wwan_dev_get_by_debugfs(dir);
191 if (WARN_ON(IS_ERR(wwandev)))
194 /* wwan_dev_get_by_debugfs() also got a reference */
195 put_device(&wwandev->dev);
196 put_device(&wwandev->dev);
198 EXPORT_SYMBOL_GPL(wwan_put_debugfs_dir);
201 /* This function allocates and registers a new WWAN device OR if a WWAN device
202 * already exist for the given parent, it gets a reference and return it.
203 * This function is not exported (for now), it is called indirectly via
204 * wwan_create_port().
206 static struct wwan_device *wwan_create_dev(struct device *parent)
208 struct wwan_device *wwandev;
211 /* The 'find-alloc-register' operation must be protected against
212 * concurrent execution, a WWAN device is possibly shared between
213 * multiple callers or concurrently unregistered from wwan_remove_dev().
215 mutex_lock(&wwan_register_lock);
217 /* If wwandev already exists, return it */
218 wwandev = wwan_dev_get_by_parent(parent);
219 if (!IS_ERR(wwandev))
222 id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
224 wwandev = ERR_PTR(id);
228 wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL);
230 wwandev = ERR_PTR(-ENOMEM);
231 ida_free(&wwan_dev_ids, id);
235 wwandev->dev.parent = parent;
236 wwandev->dev.class = wwan_class;
237 wwandev->dev.type = &wwan_dev_type;
239 dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
241 err = device_register(&wwandev->dev);
243 put_device(&wwandev->dev);
244 wwandev = ERR_PTR(err);
248 #ifdef CONFIG_WWAN_DEBUGFS
249 wwandev->debugfs_dir =
250 debugfs_create_dir(kobject_name(&wwandev->dev.kobj),
255 mutex_unlock(&wwan_register_lock);
260 static int is_wwan_child(struct device *dev, void *data)
262 return dev->class == wwan_class;
265 static void wwan_remove_dev(struct wwan_device *wwandev)
269 /* Prevent concurrent picking from wwan_create_dev */
270 mutex_lock(&wwan_register_lock);
272 /* WWAN device is created and registered (get+add) along with its first
273 * child port, and subsequent port registrations only grab a reference
274 * (get). The WWAN device must then be unregistered (del+put) along with
275 * its last port, and reference simply dropped (put) otherwise. In the
276 * same fashion, we must not unregister it when the ops are still there.
281 ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child);
284 #ifdef CONFIG_WWAN_DEBUGFS
285 debugfs_remove_recursive(wwandev->debugfs_dir);
287 device_unregister(&wwandev->dev);
289 put_device(&wwandev->dev);
292 mutex_unlock(&wwan_register_lock);
295 /* ------- WWAN port management ------- */
297 static const struct {
298 const char * const name; /* Port type name */
299 const char * const devsuf; /* Port devce name suffix */
300 } wwan_port_types[WWAN_PORT_MAX + 1] = {
317 [WWAN_PORT_FIREHOSE] = {
319 .devsuf = "firehose",
323 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
326 struct wwan_port *port = to_wwan_port(dev);
328 return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
330 static DEVICE_ATTR_RO(type);
332 static struct attribute *wwan_port_attrs[] = {
336 ATTRIBUTE_GROUPS(wwan_port);
338 static void wwan_port_destroy(struct device *dev)
340 struct wwan_port *port = to_wwan_port(dev);
342 ida_free(&minors, MINOR(port->dev.devt));
343 mutex_destroy(&port->data_lock);
344 mutex_destroy(&port->ops_lock);
348 static const struct device_type wwan_port_dev_type = {
350 .release = wwan_port_destroy,
351 .groups = wwan_port_groups,
354 static int wwan_port_minor_match(struct device *dev, const void *minor)
356 return (dev->type == &wwan_port_dev_type &&
357 MINOR(dev->devt) == *(unsigned int *)minor);
360 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
364 dev = class_find_device(wwan_class, NULL, &minor, wwan_port_minor_match);
366 return ERR_PTR(-ENODEV);
368 return to_wwan_port(dev);
371 /* Allocate and set unique name based on passed format
373 * Name allocation approach is highly inspired by the __dev_alloc_name()
376 * To avoid names collision, the caller must prevent the new port device
377 * registration as well as concurrent invocation of this function.
379 static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
381 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
382 const unsigned int max_ports = PAGE_SIZE * 8;
383 struct class_dev_iter iter;
384 unsigned long *idmap;
389 idmap = (unsigned long *)get_zeroed_page(GFP_KERNEL);
393 /* Collect ids of same name format ports */
394 class_dev_iter_init(&iter, wwan_class, NULL, &wwan_port_dev_type);
395 while ((dev = class_dev_iter_next(&iter))) {
396 if (dev->parent != &wwandev->dev)
398 if (sscanf(dev_name(dev), fmt, &id) != 1)
400 if (id < 0 || id >= max_ports)
404 class_dev_iter_exit(&iter);
406 /* Allocate unique id */
407 id = find_first_zero_bit(idmap, max_ports);
408 free_page((unsigned long)idmap);
410 snprintf(buf, sizeof(buf), fmt, id); /* Name generation */
412 dev = device_find_child_by_name(&wwandev->dev, buf);
418 return dev_set_name(&port->dev, buf);
421 struct wwan_port *wwan_create_port(struct device *parent,
422 enum wwan_port_type type,
423 const struct wwan_port_ops *ops,
426 struct wwan_device *wwandev;
427 struct wwan_port *port;
431 if (type > WWAN_PORT_MAX || !ops)
432 return ERR_PTR(-EINVAL);
434 /* A port is always a child of a WWAN device, retrieve (allocate or
435 * pick) the WWAN device based on the provided parent device.
437 wwandev = wwan_create_dev(parent);
439 return ERR_CAST(wwandev);
441 /* A port is exposed as character device, get a minor */
442 minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
445 goto error_wwandev_remove;
448 port = kzalloc(sizeof(*port), GFP_KERNEL);
451 ida_free(&minors, minor);
452 goto error_wwandev_remove;
457 mutex_init(&port->ops_lock);
458 skb_queue_head_init(&port->rxq);
459 init_waitqueue_head(&port->waitqueue);
460 mutex_init(&port->data_lock);
462 port->dev.parent = &wwandev->dev;
463 port->dev.class = wwan_class;
464 port->dev.type = &wwan_port_dev_type;
465 port->dev.devt = MKDEV(wwan_major, minor);
466 dev_set_drvdata(&port->dev, drvdata);
468 /* allocate unique name based on wwan device id, port type and number */
469 snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
470 wwan_port_types[port->type].devsuf);
472 /* Serialize ports registration */
473 mutex_lock(&wwan_register_lock);
475 __wwan_port_dev_assign_name(port, namefmt);
476 err = device_register(&port->dev);
478 mutex_unlock(&wwan_register_lock);
481 goto error_put_device;
486 put_device(&port->dev);
487 error_wwandev_remove:
488 wwan_remove_dev(wwandev);
492 EXPORT_SYMBOL_GPL(wwan_create_port);
494 void wwan_remove_port(struct wwan_port *port)
496 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
498 mutex_lock(&port->ops_lock);
499 if (port->start_count)
500 port->ops->stop(port);
501 port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
502 mutex_unlock(&port->ops_lock);
504 wake_up_interruptible(&port->waitqueue);
506 skb_queue_purge(&port->rxq);
507 dev_set_drvdata(&port->dev, NULL);
508 device_unregister(&port->dev);
510 /* Release related wwan device */
511 wwan_remove_dev(wwandev);
513 EXPORT_SYMBOL_GPL(wwan_remove_port);
515 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
517 skb_queue_tail(&port->rxq, skb);
518 wake_up_interruptible(&port->waitqueue);
520 EXPORT_SYMBOL_GPL(wwan_port_rx);
522 void wwan_port_txon(struct wwan_port *port)
524 clear_bit(WWAN_PORT_TX_OFF, &port->flags);
525 wake_up_interruptible(&port->waitqueue);
527 EXPORT_SYMBOL_GPL(wwan_port_txon);
529 void wwan_port_txoff(struct wwan_port *port)
531 set_bit(WWAN_PORT_TX_OFF, &port->flags);
533 EXPORT_SYMBOL_GPL(wwan_port_txoff);
535 void *wwan_port_get_drvdata(struct wwan_port *port)
537 return dev_get_drvdata(&port->dev);
539 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
541 static int wwan_port_op_start(struct wwan_port *port)
545 mutex_lock(&port->ops_lock);
546 if (!port->ops) { /* Port got unplugged */
551 /* If port is already started, don't start again */
552 if (!port->start_count)
553 ret = port->ops->start(port);
559 mutex_unlock(&port->ops_lock);
564 static void wwan_port_op_stop(struct wwan_port *port)
566 mutex_lock(&port->ops_lock);
568 if (!port->start_count) {
570 port->ops->stop(port);
571 skb_queue_purge(&port->rxq);
573 mutex_unlock(&port->ops_lock);
576 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
581 mutex_lock(&port->ops_lock);
582 if (!port->ops) { /* Port got unplugged */
587 if (nonblock || !port->ops->tx_blocking)
588 ret = port->ops->tx(port, skb);
590 ret = port->ops->tx_blocking(port, skb);
593 mutex_unlock(&port->ops_lock);
598 static bool is_read_blocked(struct wwan_port *port)
600 return skb_queue_empty(&port->rxq) && port->ops;
603 static bool is_write_blocked(struct wwan_port *port)
605 return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
608 static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
610 if (!is_read_blocked(port))
616 if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
622 static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
624 if (!is_write_blocked(port))
630 if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
636 static int wwan_port_fops_open(struct inode *inode, struct file *file)
638 struct wwan_port *port;
641 port = wwan_port_get_by_minor(iminor(inode));
643 return PTR_ERR(port);
645 file->private_data = port;
646 stream_open(inode, file);
648 err = wwan_port_op_start(port);
650 put_device(&port->dev);
655 static int wwan_port_fops_release(struct inode *inode, struct file *filp)
657 struct wwan_port *port = filp->private_data;
659 wwan_port_op_stop(port);
660 put_device(&port->dev);
665 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
666 size_t count, loff_t *ppos)
668 struct wwan_port *port = filp->private_data;
673 ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
677 skb = skb_dequeue(&port->rxq);
681 copied = min_t(size_t, count, skb->len);
682 if (copy_to_user(buf, skb->data, copied)) {
686 skb_pull(skb, copied);
688 /* skb is not fully consumed, keep it in the queue */
690 skb_queue_head(&port->rxq, skb);
697 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
698 size_t count, loff_t *offp)
700 struct wwan_port *port = filp->private_data;
704 ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
708 skb = alloc_skb(count, GFP_KERNEL);
712 if (copy_from_user(skb_put(skb, count), buf, count)) {
717 ret = wwan_port_op_tx(port, skb, !!(filp->f_flags & O_NONBLOCK));
726 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
728 struct wwan_port *port = filp->private_data;
731 poll_wait(filp, &port->waitqueue, wait);
733 mutex_lock(&port->ops_lock);
734 if (port->ops && port->ops->tx_poll)
735 mask |= port->ops->tx_poll(port, filp, wait);
736 else if (!is_write_blocked(port))
737 mask |= EPOLLOUT | EPOLLWRNORM;
738 if (!is_read_blocked(port))
739 mask |= EPOLLIN | EPOLLRDNORM;
741 mask |= EPOLLHUP | EPOLLERR;
742 mutex_unlock(&port->ops_lock);
747 /* Implements minimalistic stub terminal IOCTLs support */
748 static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
753 mutex_lock(&port->data_lock);
760 if (copy_to_user((void __user *)arg, &port->at_data.termios,
761 sizeof(struct termios)))
768 if (copy_from_user(&port->at_data.termios, (void __user *)arg,
769 sizeof(struct termios)))
775 if (copy_to_user((void __user *)arg, &port->at_data.termios,
776 sizeof(struct termios2)))
783 if (copy_from_user(&port->at_data.termios, (void __user *)arg,
784 sizeof(struct termios2)))
790 ret = put_user(port->at_data.mdmbits, (int __user *)arg);
798 if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
803 port->at_data.mdmbits &= ~mdmbits;
804 else if (cmd == TIOCMBIS)
805 port->at_data.mdmbits |= mdmbits;
807 port->at_data.mdmbits = mdmbits;
815 mutex_unlock(&port->data_lock);
820 static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
823 struct wwan_port *port = filp->private_data;
826 if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */
827 res = wwan_port_fops_at_ioctl(port, cmd, arg);
828 if (res != -ENOIOCTLCMD)
833 case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
838 spin_lock_irqsave(&port->rxq.lock, flags);
839 skb_queue_walk(&port->rxq, skb)
841 spin_unlock_irqrestore(&port->rxq.lock, flags);
843 return put_user(amount, (int __user *)arg);
851 static const struct file_operations wwan_port_fops = {
852 .owner = THIS_MODULE,
853 .open = wwan_port_fops_open,
854 .release = wwan_port_fops_release,
855 .read = wwan_port_fops_read,
856 .write = wwan_port_fops_write,
857 .poll = wwan_port_fops_poll,
858 .unlocked_ioctl = wwan_port_fops_ioctl,
860 .compat_ioctl = compat_ptr_ioctl,
862 .llseek = noop_llseek,
865 static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
866 struct netlink_ext_ack *extack)
871 if (!tb[IFLA_PARENT_DEV_NAME])
874 if (!data[IFLA_WWAN_LINK_ID])
880 static struct device_type wwan_type = { .name = "wwan" };
882 static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],
884 unsigned char name_assign_type,
885 unsigned int num_tx_queues,
886 unsigned int num_rx_queues)
888 const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]);
889 struct wwan_device *wwandev = wwan_dev_get_by_name(devname);
890 struct net_device *dev;
891 unsigned int priv_size;
894 return ERR_CAST(wwandev);
896 /* only supported if ops were registered (not just ports) */
898 dev = ERR_PTR(-EOPNOTSUPP);
902 priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size;
903 dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type,
904 wwandev->ops->setup, num_tx_queues, num_rx_queues);
907 SET_NETDEV_DEV(dev, &wwandev->dev);
908 SET_NETDEV_DEVTYPE(dev, &wwan_type);
912 /* release the reference */
913 put_device(&wwandev->dev);
917 static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev,
918 struct nlattr *tb[], struct nlattr *data[],
919 struct netlink_ext_ack *extack)
921 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
922 u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]);
923 struct wwan_netdev_priv *priv = netdev_priv(dev);
927 return PTR_ERR(wwandev);
929 /* shouldn't have a netdev (left) with us as parent so WARN */
930 if (WARN_ON(!wwandev->ops)) {
935 priv->link_id = link_id;
936 if (wwandev->ops->newlink)
937 ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev,
940 ret = register_netdevice(dev);
943 /* release the reference */
944 put_device(&wwandev->dev);
948 static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head)
950 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
955 /* shouldn't have a netdev (left) with us as parent so WARN */
956 if (WARN_ON(!wwandev->ops))
959 if (wwandev->ops->dellink)
960 wwandev->ops->dellink(wwandev->ops_ctxt, dev, head);
962 unregister_netdevice_queue(dev, head);
965 /* release the reference */
966 put_device(&wwandev->dev);
969 static size_t wwan_rtnl_get_size(const struct net_device *dev)
972 nla_total_size(4) + /* IFLA_WWAN_LINK_ID */
976 static int wwan_rtnl_fill_info(struct sk_buff *skb,
977 const struct net_device *dev)
979 struct wwan_netdev_priv *priv = netdev_priv(dev);
981 if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id))
982 goto nla_put_failure;
990 static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = {
991 [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 },
994 static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = {
996 .maxtype = __IFLA_WWAN_MAX,
997 .alloc = wwan_rtnl_alloc,
998 .validate = wwan_rtnl_validate,
999 .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,
1006 static void wwan_create_default_link(struct wwan_device *wwandev,
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;
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.
1019 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1022 nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0);
1026 if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev)))
1028 tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO);
1029 if (!tb[IFLA_LINKINFO])
1031 linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA);
1032 if (!linkinfo[IFLA_INFO_DATA])
1034 if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id))
1036 nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]);
1037 nla_nest_end(msg, tb[IFLA_LINKINFO]);
1039 nlmsg_end(msg, nlh);
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],
1045 nla_parse_nested_deprecated(data, IFLA_WWAN_MAX,
1046 linkinfo[IFLA_INFO_DATA], NULL, NULL);
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)))
1055 if (WARN_ON(wwan_rtnl_newlink(&init_net, dev, tb, data, NULL))) {
1060 rtnl_configure_link(dev, NULL); /* Link initialized, notify new link */
1070 * wwan_register_ops - register WWAN device ops
1071 * @parent: Device to use as parent and shared by all WWAN ports and
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.
1079 * Returns: 0 on success, a negative error code on failure
1081 int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
1082 void *ctxt, u32 def_link_id)
1084 struct wwan_device *wwandev;
1086 if (WARN_ON(!parent || !ops || !ops->setup))
1089 wwandev = wwan_create_dev(parent);
1090 if (IS_ERR(wwandev))
1091 return PTR_ERR(wwandev);
1093 if (WARN_ON(wwandev->ops)) {
1094 wwan_remove_dev(wwandev);
1099 wwandev->ops_ctxt = ctxt;
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
1107 if (def_link_id != WWAN_NO_DEFAULT_LINK)
1108 wwan_create_default_link(wwandev, def_link_id);
1112 EXPORT_SYMBOL_GPL(wwan_register_ops);
1114 /* Enqueue child netdev deletion */
1115 static int wwan_child_dellink(struct device *dev, void *data)
1117 struct list_head *kill_list = data;
1119 if (dev->type == &wwan_type)
1120 wwan_rtnl_dellink(to_net_dev(dev), kill_list);
1126 * wwan_unregister_ops - remove WWAN device ops
1127 * @parent: Device to use as parent and shared by all WWAN ports and
1130 void wwan_unregister_ops(struct device *parent)
1132 struct wwan_device *wwandev = wwan_dev_get_by_parent(parent);
1133 LIST_HEAD(kill_list);
1135 if (WARN_ON(IS_ERR(wwandev)))
1137 if (WARN_ON(!wwandev->ops)) {
1138 put_device(&wwandev->dev);
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.
1146 put_device(&wwandev->dev);
1148 rtnl_lock(); /* Prevent concurent netdev(s) creation/destroying */
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);
1155 wwandev->ops = NULL; /* Finally remove ops */
1159 wwandev->ops_ctxt = NULL;
1160 wwan_remove_dev(wwandev);
1162 EXPORT_SYMBOL_GPL(wwan_unregister_ops);
1164 static int __init wwan_init(void)
1168 err = rtnl_link_register(&wwan_rtnl_link_ops);
1172 wwan_class = class_create(THIS_MODULE, "wwan");
1173 if (IS_ERR(wwan_class)) {
1174 err = PTR_ERR(wwan_class);
1178 /* chrdev used for wwan ports */
1179 wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
1181 if (wwan_major < 0) {
1186 #ifdef CONFIG_WWAN_DEBUGFS
1187 wwan_debugfs_dir = debugfs_create_dir("wwan", NULL);
1193 class_destroy(wwan_class);
1195 rtnl_link_unregister(&wwan_rtnl_link_ops);
1199 static void __exit wwan_exit(void)
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);
1207 module_init(wwan_init);
1208 module_exit(wwan_exit);
1211 MODULE_DESCRIPTION("WWAN core");
1212 MODULE_LICENSE("GPL v2");