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 struct class *wwan_class;
30 static int wwan_major;
31 static struct dentry *wwan_debugfs_dir;
33 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
34 #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
37 #define WWAN_PORT_TX_OFF 0
40 * struct wwan_device - The structure that defines a WWAN device
42 * @id: WWAN device unique ID.
43 * @dev: Underlying device.
44 * @port_id: Current available port ID to pick.
45 * @ops: wwan device ops
46 * @ops_ctxt: context to pass to ops
47 * @debugfs_dir: WWAN device debugfs dir
53 const struct wwan_ops *ops;
55 #ifdef CONFIG_WWAN_DEBUGFS
56 struct dentry *debugfs_dir;
61 * struct wwan_port - The structure that defines a WWAN port
63 * @start_count: Port start counter
64 * @flags: Store port state and capabilities
65 * @ops: Pointer to WWAN port operations
66 * @ops_lock: Protect port ops
67 * @dev: Underlying device
68 * @rxq: Buffer inbound queue
69 * @waitqueue: The waitqueue for port fops (read/write/poll)
70 * @data_lock: Port specific data access serialization
71 * @headroom_len: SKB reserved headroom size
72 * @frag_len: Length to fragment packet
73 * @at_data: AT port specific data
76 enum wwan_port_type type;
77 unsigned int start_count;
79 const struct wwan_port_ops *ops;
80 struct mutex ops_lock; /* Serialize ops + protect against removal */
82 struct sk_buff_head rxq;
83 wait_queue_head_t waitqueue;
84 struct mutex data_lock; /* Port specific data access serialization */
89 struct ktermios termios;
95 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
97 struct wwan_device *wwan = to_wwan_dev(dev);
99 return sprintf(buf, "%d\n", wwan->id);
101 static DEVICE_ATTR_RO(index);
103 static struct attribute *wwan_dev_attrs[] = {
104 &dev_attr_index.attr,
107 ATTRIBUTE_GROUPS(wwan_dev);
109 static void wwan_dev_destroy(struct device *dev)
111 struct wwan_device *wwandev = to_wwan_dev(dev);
113 ida_free(&wwan_dev_ids, wwandev->id);
117 static const struct device_type wwan_dev_type = {
119 .release = wwan_dev_destroy,
120 .groups = wwan_dev_groups,
123 static int wwan_dev_parent_match(struct device *dev, const void *parent)
125 return (dev->type == &wwan_dev_type &&
126 (dev->parent == parent || dev == parent));
129 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
133 dev = class_find_device(wwan_class, NULL, parent, wwan_dev_parent_match);
135 return ERR_PTR(-ENODEV);
137 return to_wwan_dev(dev);
140 static int wwan_dev_name_match(struct device *dev, const void *name)
142 return dev->type == &wwan_dev_type &&
143 strcmp(dev_name(dev), name) == 0;
146 static struct wwan_device *wwan_dev_get_by_name(const char *name)
150 dev = class_find_device(wwan_class, NULL, name, wwan_dev_name_match);
152 return ERR_PTR(-ENODEV);
154 return to_wwan_dev(dev);
157 #ifdef CONFIG_WWAN_DEBUGFS
158 struct dentry *wwan_get_debugfs_dir(struct device *parent)
160 struct wwan_device *wwandev;
162 wwandev = wwan_dev_get_by_parent(parent);
164 return ERR_CAST(wwandev);
166 return wwandev->debugfs_dir;
168 EXPORT_SYMBOL_GPL(wwan_get_debugfs_dir);
170 static int wwan_dev_debugfs_match(struct device *dev, const void *dir)
172 struct wwan_device *wwandev;
174 if (dev->type != &wwan_dev_type)
177 wwandev = to_wwan_dev(dev);
179 return wwandev->debugfs_dir == dir;
182 static struct wwan_device *wwan_dev_get_by_debugfs(struct dentry *dir)
186 dev = class_find_device(wwan_class, NULL, dir, wwan_dev_debugfs_match);
188 return ERR_PTR(-ENODEV);
190 return to_wwan_dev(dev);
193 void wwan_put_debugfs_dir(struct dentry *dir)
195 struct wwan_device *wwandev = wwan_dev_get_by_debugfs(dir);
197 if (WARN_ON(IS_ERR(wwandev)))
200 /* wwan_dev_get_by_debugfs() also got a reference */
201 put_device(&wwandev->dev);
202 put_device(&wwandev->dev);
204 EXPORT_SYMBOL_GPL(wwan_put_debugfs_dir);
207 /* This function allocates and registers a new WWAN device OR if a WWAN device
208 * already exist for the given parent, it gets a reference and return it.
209 * This function is not exported (for now), it is called indirectly via
210 * wwan_create_port().
212 static struct wwan_device *wwan_create_dev(struct device *parent)
214 struct wwan_device *wwandev;
217 /* The 'find-alloc-register' operation must be protected against
218 * concurrent execution, a WWAN device is possibly shared between
219 * multiple callers or concurrently unregistered from wwan_remove_dev().
221 mutex_lock(&wwan_register_lock);
223 /* If wwandev already exists, return it */
224 wwandev = wwan_dev_get_by_parent(parent);
225 if (!IS_ERR(wwandev))
228 id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
230 wwandev = ERR_PTR(id);
234 wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL);
236 wwandev = ERR_PTR(-ENOMEM);
237 ida_free(&wwan_dev_ids, id);
241 wwandev->dev.parent = parent;
242 wwandev->dev.class = wwan_class;
243 wwandev->dev.type = &wwan_dev_type;
245 dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
247 err = device_register(&wwandev->dev);
249 put_device(&wwandev->dev);
250 wwandev = ERR_PTR(err);
254 #ifdef CONFIG_WWAN_DEBUGFS
255 wwandev->debugfs_dir =
256 debugfs_create_dir(kobject_name(&wwandev->dev.kobj),
261 mutex_unlock(&wwan_register_lock);
266 static int is_wwan_child(struct device *dev, void *data)
268 return dev->class == wwan_class;
271 static void wwan_remove_dev(struct wwan_device *wwandev)
275 /* Prevent concurrent picking from wwan_create_dev */
276 mutex_lock(&wwan_register_lock);
278 /* WWAN device is created and registered (get+add) along with its first
279 * child port, and subsequent port registrations only grab a reference
280 * (get). The WWAN device must then be unregistered (del+put) along with
281 * its last port, and reference simply dropped (put) otherwise. In the
282 * same fashion, we must not unregister it when the ops are still there.
287 ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child);
290 #ifdef CONFIG_WWAN_DEBUGFS
291 debugfs_remove_recursive(wwandev->debugfs_dir);
293 device_unregister(&wwandev->dev);
295 put_device(&wwandev->dev);
298 mutex_unlock(&wwan_register_lock);
301 /* ------- WWAN port management ------- */
303 static const struct {
304 const char * const name; /* Port type name */
305 const char * const devsuf; /* Port device name suffix */
306 } wwan_port_types[WWAN_PORT_MAX + 1] = {
323 [WWAN_PORT_FIREHOSE] = {
325 .devsuf = "firehose",
327 [WWAN_PORT_XMMRPC] = {
333 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
336 struct wwan_port *port = to_wwan_port(dev);
338 return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
340 static DEVICE_ATTR_RO(type);
342 static struct attribute *wwan_port_attrs[] = {
346 ATTRIBUTE_GROUPS(wwan_port);
348 static void wwan_port_destroy(struct device *dev)
350 struct wwan_port *port = to_wwan_port(dev);
352 ida_free(&minors, MINOR(port->dev.devt));
353 mutex_destroy(&port->data_lock);
354 mutex_destroy(&port->ops_lock);
358 static const struct device_type wwan_port_dev_type = {
360 .release = wwan_port_destroy,
361 .groups = wwan_port_groups,
364 static int wwan_port_minor_match(struct device *dev, const void *minor)
366 return (dev->type == &wwan_port_dev_type &&
367 MINOR(dev->devt) == *(unsigned int *)minor);
370 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
374 dev = class_find_device(wwan_class, NULL, &minor, wwan_port_minor_match);
376 return ERR_PTR(-ENODEV);
378 return to_wwan_port(dev);
381 /* Allocate and set unique name based on passed format
383 * Name allocation approach is highly inspired by the __dev_alloc_name()
386 * To avoid names collision, the caller must prevent the new port device
387 * registration as well as concurrent invocation of this function.
389 static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
391 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
392 const unsigned int max_ports = PAGE_SIZE * 8;
393 struct class_dev_iter iter;
394 unsigned long *idmap;
399 idmap = bitmap_zalloc(max_ports, GFP_KERNEL);
403 /* Collect ids of same name format ports */
404 class_dev_iter_init(&iter, wwan_class, NULL, &wwan_port_dev_type);
405 while ((dev = class_dev_iter_next(&iter))) {
406 if (dev->parent != &wwandev->dev)
408 if (sscanf(dev_name(dev), fmt, &id) != 1)
410 if (id < 0 || id >= max_ports)
414 class_dev_iter_exit(&iter);
416 /* Allocate unique id */
417 id = find_first_zero_bit(idmap, max_ports);
420 snprintf(buf, sizeof(buf), fmt, id); /* Name generation */
422 dev = device_find_child_by_name(&wwandev->dev, buf);
428 return dev_set_name(&port->dev, buf);
431 struct wwan_port *wwan_create_port(struct device *parent,
432 enum wwan_port_type type,
433 const struct wwan_port_ops *ops,
434 struct wwan_port_caps *caps,
437 struct wwan_device *wwandev;
438 struct wwan_port *port;
442 if (type > WWAN_PORT_MAX || !ops)
443 return ERR_PTR(-EINVAL);
445 /* A port is always a child of a WWAN device, retrieve (allocate or
446 * pick) the WWAN device based on the provided parent device.
448 wwandev = wwan_create_dev(parent);
450 return ERR_CAST(wwandev);
452 /* A port is exposed as character device, get a minor */
453 minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
456 goto error_wwandev_remove;
459 port = kzalloc(sizeof(*port), GFP_KERNEL);
462 ida_free(&minors, minor);
463 goto error_wwandev_remove;
468 port->frag_len = caps ? caps->frag_len : SIZE_MAX;
469 port->headroom_len = caps ? caps->headroom_len : 0;
470 mutex_init(&port->ops_lock);
471 skb_queue_head_init(&port->rxq);
472 init_waitqueue_head(&port->waitqueue);
473 mutex_init(&port->data_lock);
475 port->dev.parent = &wwandev->dev;
476 port->dev.class = wwan_class;
477 port->dev.type = &wwan_port_dev_type;
478 port->dev.devt = MKDEV(wwan_major, minor);
479 dev_set_drvdata(&port->dev, drvdata);
481 /* allocate unique name based on wwan device id, port type and number */
482 snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
483 wwan_port_types[port->type].devsuf);
485 /* Serialize ports registration */
486 mutex_lock(&wwan_register_lock);
488 __wwan_port_dev_assign_name(port, namefmt);
489 err = device_register(&port->dev);
491 mutex_unlock(&wwan_register_lock);
494 goto error_put_device;
496 dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev));
500 put_device(&port->dev);
501 error_wwandev_remove:
502 wwan_remove_dev(wwandev);
506 EXPORT_SYMBOL_GPL(wwan_create_port);
508 void wwan_remove_port(struct wwan_port *port)
510 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
512 mutex_lock(&port->ops_lock);
513 if (port->start_count)
514 port->ops->stop(port);
515 port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
516 mutex_unlock(&port->ops_lock);
518 wake_up_interruptible(&port->waitqueue);
520 skb_queue_purge(&port->rxq);
521 dev_set_drvdata(&port->dev, NULL);
523 dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&port->dev));
524 device_unregister(&port->dev);
526 /* Release related wwan device */
527 wwan_remove_dev(wwandev);
529 EXPORT_SYMBOL_GPL(wwan_remove_port);
531 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
533 skb_queue_tail(&port->rxq, skb);
534 wake_up_interruptible(&port->waitqueue);
536 EXPORT_SYMBOL_GPL(wwan_port_rx);
538 void wwan_port_txon(struct wwan_port *port)
540 clear_bit(WWAN_PORT_TX_OFF, &port->flags);
541 wake_up_interruptible(&port->waitqueue);
543 EXPORT_SYMBOL_GPL(wwan_port_txon);
545 void wwan_port_txoff(struct wwan_port *port)
547 set_bit(WWAN_PORT_TX_OFF, &port->flags);
549 EXPORT_SYMBOL_GPL(wwan_port_txoff);
551 void *wwan_port_get_drvdata(struct wwan_port *port)
553 return dev_get_drvdata(&port->dev);
555 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
557 static int wwan_port_op_start(struct wwan_port *port)
561 mutex_lock(&port->ops_lock);
562 if (!port->ops) { /* Port got unplugged */
567 /* If port is already started, don't start again */
568 if (!port->start_count)
569 ret = port->ops->start(port);
575 mutex_unlock(&port->ops_lock);
580 static void wwan_port_op_stop(struct wwan_port *port)
582 mutex_lock(&port->ops_lock);
584 if (!port->start_count) {
586 port->ops->stop(port);
587 skb_queue_purge(&port->rxq);
589 mutex_unlock(&port->ops_lock);
592 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
597 mutex_lock(&port->ops_lock);
598 if (!port->ops) { /* Port got unplugged */
603 if (nonblock || !port->ops->tx_blocking)
604 ret = port->ops->tx(port, skb);
606 ret = port->ops->tx_blocking(port, skb);
609 mutex_unlock(&port->ops_lock);
614 static bool is_read_blocked(struct wwan_port *port)
616 return skb_queue_empty(&port->rxq) && port->ops;
619 static bool is_write_blocked(struct wwan_port *port)
621 return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
624 static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
626 if (!is_read_blocked(port))
632 if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
638 static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
640 if (!is_write_blocked(port))
646 if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
652 static int wwan_port_fops_open(struct inode *inode, struct file *file)
654 struct wwan_port *port;
657 port = wwan_port_get_by_minor(iminor(inode));
659 return PTR_ERR(port);
661 file->private_data = port;
662 stream_open(inode, file);
664 err = wwan_port_op_start(port);
666 put_device(&port->dev);
671 static int wwan_port_fops_release(struct inode *inode, struct file *filp)
673 struct wwan_port *port = filp->private_data;
675 wwan_port_op_stop(port);
676 put_device(&port->dev);
681 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
682 size_t count, loff_t *ppos)
684 struct wwan_port *port = filp->private_data;
689 ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
693 skb = skb_dequeue(&port->rxq);
697 copied = min_t(size_t, count, skb->len);
698 if (copy_to_user(buf, skb->data, copied)) {
702 skb_pull(skb, copied);
704 /* skb is not fully consumed, keep it in the queue */
706 skb_queue_head(&port->rxq, skb);
713 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
714 size_t count, loff_t *offp)
716 struct sk_buff *skb, *head = NULL, *tail = NULL;
717 struct wwan_port *port = filp->private_data;
718 size_t frag_len, remain = count;
721 ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
726 frag_len = min(remain, port->frag_len);
727 skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL);
732 skb_reserve(skb, port->headroom_len);
737 skb_shinfo(head)->frag_list = skb;
744 if (copy_from_user(skb_put(skb, frag_len), buf + count - remain, frag_len)) {
750 head->data_len += skb->len;
751 head->len += skb->len;
752 head->truesize += skb->truesize;
754 } while (remain -= frag_len);
756 ret = wwan_port_op_tx(port, head, !!(filp->f_flags & O_NONBLOCK));
765 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
767 struct wwan_port *port = filp->private_data;
770 poll_wait(filp, &port->waitqueue, wait);
772 mutex_lock(&port->ops_lock);
773 if (port->ops && port->ops->tx_poll)
774 mask |= port->ops->tx_poll(port, filp, wait);
775 else if (!is_write_blocked(port))
776 mask |= EPOLLOUT | EPOLLWRNORM;
777 if (!is_read_blocked(port))
778 mask |= EPOLLIN | EPOLLRDNORM;
780 mask |= EPOLLHUP | EPOLLERR;
781 mutex_unlock(&port->ops_lock);
786 /* Implements minimalistic stub terminal IOCTLs support */
787 static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
792 mutex_lock(&port->data_lock);
799 if (copy_to_user((void __user *)arg, &port->at_data.termios,
800 sizeof(struct termios)))
807 if (copy_from_user(&port->at_data.termios, (void __user *)arg,
808 sizeof(struct termios)))
814 if (copy_to_user((void __user *)arg, &port->at_data.termios,
815 sizeof(struct termios2)))
822 if (copy_from_user(&port->at_data.termios, (void __user *)arg,
823 sizeof(struct termios2)))
829 ret = put_user(port->at_data.mdmbits, (int __user *)arg);
837 if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
842 port->at_data.mdmbits &= ~mdmbits;
843 else if (cmd == TIOCMBIS)
844 port->at_data.mdmbits |= mdmbits;
846 port->at_data.mdmbits = mdmbits;
854 mutex_unlock(&port->data_lock);
859 static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
862 struct wwan_port *port = filp->private_data;
865 if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */
866 res = wwan_port_fops_at_ioctl(port, cmd, arg);
867 if (res != -ENOIOCTLCMD)
872 case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
877 spin_lock_irqsave(&port->rxq.lock, flags);
878 skb_queue_walk(&port->rxq, skb)
880 spin_unlock_irqrestore(&port->rxq.lock, flags);
882 return put_user(amount, (int __user *)arg);
890 static const struct file_operations wwan_port_fops = {
891 .owner = THIS_MODULE,
892 .open = wwan_port_fops_open,
893 .release = wwan_port_fops_release,
894 .read = wwan_port_fops_read,
895 .write = wwan_port_fops_write,
896 .poll = wwan_port_fops_poll,
897 .unlocked_ioctl = wwan_port_fops_ioctl,
899 .compat_ioctl = compat_ptr_ioctl,
901 .llseek = noop_llseek,
904 static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
905 struct netlink_ext_ack *extack)
910 if (!tb[IFLA_PARENT_DEV_NAME])
913 if (!data[IFLA_WWAN_LINK_ID])
919 static struct device_type wwan_type = { .name = "wwan" };
921 static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],
923 unsigned char name_assign_type,
924 unsigned int num_tx_queues,
925 unsigned int num_rx_queues)
927 const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]);
928 struct wwan_device *wwandev = wwan_dev_get_by_name(devname);
929 struct net_device *dev;
930 unsigned int priv_size;
933 return ERR_CAST(wwandev);
935 /* only supported if ops were registered (not just ports) */
937 dev = ERR_PTR(-EOPNOTSUPP);
941 priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size;
942 dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type,
943 wwandev->ops->setup, num_tx_queues, num_rx_queues);
946 SET_NETDEV_DEV(dev, &wwandev->dev);
947 SET_NETDEV_DEVTYPE(dev, &wwan_type);
951 /* release the reference */
952 put_device(&wwandev->dev);
956 static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev,
957 struct nlattr *tb[], struct nlattr *data[],
958 struct netlink_ext_ack *extack)
960 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
961 u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]);
962 struct wwan_netdev_priv *priv = netdev_priv(dev);
966 return PTR_ERR(wwandev);
968 /* shouldn't have a netdev (left) with us as parent so WARN */
969 if (WARN_ON(!wwandev->ops)) {
974 priv->link_id = link_id;
975 if (wwandev->ops->newlink)
976 ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev,
979 ret = register_netdevice(dev);
982 /* release the reference */
983 put_device(&wwandev->dev);
987 static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head)
989 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
994 /* shouldn't have a netdev (left) with us as parent so WARN */
995 if (WARN_ON(!wwandev->ops))
998 if (wwandev->ops->dellink)
999 wwandev->ops->dellink(wwandev->ops_ctxt, dev, head);
1001 unregister_netdevice_queue(dev, head);
1004 /* release the reference */
1005 put_device(&wwandev->dev);
1008 static size_t wwan_rtnl_get_size(const struct net_device *dev)
1011 nla_total_size(4) + /* IFLA_WWAN_LINK_ID */
1015 static int wwan_rtnl_fill_info(struct sk_buff *skb,
1016 const struct net_device *dev)
1018 struct wwan_netdev_priv *priv = netdev_priv(dev);
1020 if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id))
1021 goto nla_put_failure;
1029 static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = {
1030 [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 },
1033 static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = {
1035 .maxtype = __IFLA_WWAN_MAX,
1036 .alloc = wwan_rtnl_alloc,
1037 .validate = wwan_rtnl_validate,
1038 .newlink = wwan_rtnl_newlink,
1039 .dellink = wwan_rtnl_dellink,
1040 .get_size = wwan_rtnl_get_size,
1041 .fill_info = wwan_rtnl_fill_info,
1042 .policy = wwan_rtnl_policy,
1045 static void wwan_create_default_link(struct wwan_device *wwandev,
1048 struct nlattr *tb[IFLA_MAX + 1], *linkinfo[IFLA_INFO_MAX + 1];
1049 struct nlattr *data[IFLA_WWAN_MAX + 1];
1050 struct net_device *dev;
1051 struct nlmsghdr *nlh;
1052 struct sk_buff *msg;
1054 /* Forge attributes required to create a WWAN netdev. We first
1055 * build a netlink message and then parse it. This looks
1056 * odd, but such approach is less error prone.
1058 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1061 nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0);
1065 if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev)))
1067 tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO);
1068 if (!tb[IFLA_LINKINFO])
1070 linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA);
1071 if (!linkinfo[IFLA_INFO_DATA])
1073 if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id))
1075 nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]);
1076 nla_nest_end(msg, tb[IFLA_LINKINFO]);
1078 nlmsg_end(msg, nlh);
1080 /* The next three parsing calls can not fail */
1081 nlmsg_parse_deprecated(nlh, 0, tb, IFLA_MAX, NULL, NULL);
1082 nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
1084 nla_parse_nested_deprecated(data, IFLA_WWAN_MAX,
1085 linkinfo[IFLA_INFO_DATA], NULL, NULL);
1089 dev = rtnl_create_link(&init_net, "wwan%d", NET_NAME_ENUM,
1090 &wwan_rtnl_link_ops, tb, NULL);
1091 if (WARN_ON(IS_ERR(dev)))
1094 if (WARN_ON(wwan_rtnl_newlink(&init_net, dev, tb, data, NULL))) {
1099 rtnl_configure_link(dev, NULL, 0, NULL); /* Link initialized, notify new link */
1109 * wwan_register_ops - register WWAN device ops
1110 * @parent: Device to use as parent and shared by all WWAN ports and
1112 * @ops: operations to register
1113 * @ctxt: context to pass to operations
1114 * @def_link_id: id of the default link that will be automatically created by
1115 * the WWAN core for the WWAN device. The default link will not be created
1116 * if the passed value is WWAN_NO_DEFAULT_LINK.
1118 * Returns: 0 on success, a negative error code on failure
1120 int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
1121 void *ctxt, u32 def_link_id)
1123 struct wwan_device *wwandev;
1125 if (WARN_ON(!parent || !ops || !ops->setup))
1128 wwandev = wwan_create_dev(parent);
1129 if (IS_ERR(wwandev))
1130 return PTR_ERR(wwandev);
1132 if (WARN_ON(wwandev->ops)) {
1133 wwan_remove_dev(wwandev);
1138 wwandev->ops_ctxt = ctxt;
1140 /* NB: we do not abort ops registration in case of default link
1141 * creation failure. Link ops is the management interface, while the
1142 * default link creation is a service option. And we should not prevent
1143 * a user from manually creating a link latter if service option failed
1146 if (def_link_id != WWAN_NO_DEFAULT_LINK)
1147 wwan_create_default_link(wwandev, def_link_id);
1151 EXPORT_SYMBOL_GPL(wwan_register_ops);
1153 /* Enqueue child netdev deletion */
1154 static int wwan_child_dellink(struct device *dev, void *data)
1156 struct list_head *kill_list = data;
1158 if (dev->type == &wwan_type)
1159 wwan_rtnl_dellink(to_net_dev(dev), kill_list);
1165 * wwan_unregister_ops - remove WWAN device ops
1166 * @parent: Device to use as parent and shared by all WWAN ports and
1169 void wwan_unregister_ops(struct device *parent)
1171 struct wwan_device *wwandev = wwan_dev_get_by_parent(parent);
1172 LIST_HEAD(kill_list);
1174 if (WARN_ON(IS_ERR(wwandev)))
1176 if (WARN_ON(!wwandev->ops)) {
1177 put_device(&wwandev->dev);
1181 /* put the reference obtained by wwan_dev_get_by_parent(),
1182 * we should still have one (that the owner is giving back
1183 * now) due to the ops being assigned.
1185 put_device(&wwandev->dev);
1187 rtnl_lock(); /* Prevent concurrent netdev(s) creation/destroying */
1189 /* Remove all child netdev(s), using batch removing */
1190 device_for_each_child(&wwandev->dev, &kill_list,
1191 wwan_child_dellink);
1192 unregister_netdevice_many(&kill_list);
1194 wwandev->ops = NULL; /* Finally remove ops */
1198 wwandev->ops_ctxt = NULL;
1199 wwan_remove_dev(wwandev);
1201 EXPORT_SYMBOL_GPL(wwan_unregister_ops);
1203 static int __init wwan_init(void)
1207 err = rtnl_link_register(&wwan_rtnl_link_ops);
1211 wwan_class = class_create("wwan");
1212 if (IS_ERR(wwan_class)) {
1213 err = PTR_ERR(wwan_class);
1217 /* chrdev used for wwan ports */
1218 wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
1220 if (wwan_major < 0) {
1225 #ifdef CONFIG_WWAN_DEBUGFS
1226 wwan_debugfs_dir = debugfs_create_dir("wwan", NULL);
1232 class_destroy(wwan_class);
1234 rtnl_link_unregister(&wwan_rtnl_link_ops);
1238 static void __exit wwan_exit(void)
1240 debugfs_remove_recursive(wwan_debugfs_dir);
1241 __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port");
1242 rtnl_link_unregister(&wwan_rtnl_link_ops);
1243 class_destroy(wwan_class);
1246 module_init(wwan_init);
1247 module_exit(wwan_exit);
1250 MODULE_DESCRIPTION("WWAN core");
1251 MODULE_LICENSE("GPL v2");