1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */
5 #include <linux/errno.h>
7 #include <linux/init.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/poll.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/termios.h>
16 #include <linux/wwan.h>
17 #include <net/rtnetlink.h>
18 #include <uapi/linux/wwan.h>
20 /* Maximum number of minors in use */
21 #define WWAN_MAX_MINORS (1 << MINORBITS)
23 static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
24 static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
25 static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */
26 static struct class *wwan_class;
27 static int wwan_major;
29 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
30 #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
33 #define WWAN_PORT_TX_OFF 0
36 * struct wwan_device - The structure that defines a WWAN device
38 * @id: WWAN device unique ID.
39 * @dev: Underlying device.
40 * @port_id: Current available port ID to pick.
41 * @ops: wwan device ops
42 * @ops_ctxt: context to pass to ops
48 const struct wwan_ops *ops;
53 * struct wwan_port - The structure that defines a WWAN port
55 * @start_count: Port start counter
56 * @flags: Store port state and capabilities
57 * @ops: Pointer to WWAN port operations
58 * @ops_lock: Protect port ops
59 * @dev: Underlying device
60 * @rxq: Buffer inbound queue
61 * @waitqueue: The waitqueue for port fops (read/write/poll)
62 * @data_lock: Port specific data access serialization
63 * @at_data: AT port specific data
66 enum wwan_port_type type;
67 unsigned int start_count;
69 const struct wwan_port_ops *ops;
70 struct mutex ops_lock; /* Serialize ops + protect against removal */
72 struct sk_buff_head rxq;
73 wait_queue_head_t waitqueue;
74 struct mutex data_lock; /* Port specific data access serialization */
77 struct ktermios termios;
83 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
85 struct wwan_device *wwan = to_wwan_dev(dev);
87 return sprintf(buf, "%d\n", wwan->id);
89 static DEVICE_ATTR_RO(index);
91 static struct attribute *wwan_dev_attrs[] = {
95 ATTRIBUTE_GROUPS(wwan_dev);
97 static void wwan_dev_destroy(struct device *dev)
99 struct wwan_device *wwandev = to_wwan_dev(dev);
101 ida_free(&wwan_dev_ids, wwandev->id);
105 static const struct device_type wwan_dev_type = {
107 .release = wwan_dev_destroy,
108 .groups = wwan_dev_groups,
111 static int wwan_dev_parent_match(struct device *dev, const void *parent)
113 return (dev->type == &wwan_dev_type &&
114 (dev->parent == parent || dev == parent));
117 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
121 dev = class_find_device(wwan_class, NULL, parent, wwan_dev_parent_match);
123 return ERR_PTR(-ENODEV);
125 return to_wwan_dev(dev);
128 static int wwan_dev_name_match(struct device *dev, const void *name)
130 return dev->type == &wwan_dev_type &&
131 strcmp(dev_name(dev), name) == 0;
134 static struct wwan_device *wwan_dev_get_by_name(const char *name)
138 dev = class_find_device(wwan_class, NULL, name, wwan_dev_name_match);
140 return ERR_PTR(-ENODEV);
142 return to_wwan_dev(dev);
145 /* This function allocates and registers a new WWAN device OR if a WWAN device
146 * already exist for the given parent, it gets a reference and return it.
147 * This function is not exported (for now), it is called indirectly via
148 * wwan_create_port().
150 static struct wwan_device *wwan_create_dev(struct device *parent)
152 struct wwan_device *wwandev;
155 /* The 'find-alloc-register' operation must be protected against
156 * concurrent execution, a WWAN device is possibly shared between
157 * multiple callers or concurrently unregistered from wwan_remove_dev().
159 mutex_lock(&wwan_register_lock);
161 /* If wwandev already exists, return it */
162 wwandev = wwan_dev_get_by_parent(parent);
163 if (!IS_ERR(wwandev))
166 id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
168 wwandev = ERR_PTR(id);
172 wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL);
174 wwandev = ERR_PTR(-ENOMEM);
175 ida_free(&wwan_dev_ids, id);
179 wwandev->dev.parent = parent;
180 wwandev->dev.class = wwan_class;
181 wwandev->dev.type = &wwan_dev_type;
183 dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
185 err = device_register(&wwandev->dev);
187 put_device(&wwandev->dev);
188 wwandev = ERR_PTR(err);
193 mutex_unlock(&wwan_register_lock);
198 static int is_wwan_child(struct device *dev, void *data)
200 return dev->class == wwan_class;
203 static void wwan_remove_dev(struct wwan_device *wwandev)
207 /* Prevent concurrent picking from wwan_create_dev */
208 mutex_lock(&wwan_register_lock);
210 /* WWAN device is created and registered (get+add) along with its first
211 * child port, and subsequent port registrations only grab a reference
212 * (get). The WWAN device must then be unregistered (del+put) along with
213 * its last port, and reference simply dropped (put) otherwise. In the
214 * same fashion, we must not unregister it when the ops are still there.
219 ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child);
222 device_unregister(&wwandev->dev);
224 put_device(&wwandev->dev);
226 mutex_unlock(&wwan_register_lock);
229 /* ------- WWAN port management ------- */
231 static const struct {
232 const char * const name; /* Port type name */
233 const char * const devsuf; /* Port devce name suffix */
234 } wwan_port_types[WWAN_PORT_MAX + 1] = {
251 [WWAN_PORT_FIREHOSE] = {
253 .devsuf = "firehose",
257 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
260 struct wwan_port *port = to_wwan_port(dev);
262 return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
264 static DEVICE_ATTR_RO(type);
266 static struct attribute *wwan_port_attrs[] = {
270 ATTRIBUTE_GROUPS(wwan_port);
272 static void wwan_port_destroy(struct device *dev)
274 struct wwan_port *port = to_wwan_port(dev);
276 ida_free(&minors, MINOR(port->dev.devt));
277 mutex_destroy(&port->data_lock);
278 mutex_destroy(&port->ops_lock);
282 static const struct device_type wwan_port_dev_type = {
284 .release = wwan_port_destroy,
285 .groups = wwan_port_groups,
288 static int wwan_port_minor_match(struct device *dev, const void *minor)
290 return (dev->type == &wwan_port_dev_type &&
291 MINOR(dev->devt) == *(unsigned int *)minor);
294 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
298 dev = class_find_device(wwan_class, NULL, &minor, wwan_port_minor_match);
300 return ERR_PTR(-ENODEV);
302 return to_wwan_port(dev);
305 /* Allocate and set unique name based on passed format
307 * Name allocation approach is highly inspired by the __dev_alloc_name()
310 * To avoid names collision, the caller must prevent the new port device
311 * registration as well as concurrent invocation of this function.
313 static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
315 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
316 const unsigned int max_ports = PAGE_SIZE * 8;
317 struct class_dev_iter iter;
318 unsigned long *idmap;
323 idmap = (unsigned long *)get_zeroed_page(GFP_KERNEL);
327 /* Collect ids of same name format ports */
328 class_dev_iter_init(&iter, wwan_class, NULL, &wwan_port_dev_type);
329 while ((dev = class_dev_iter_next(&iter))) {
330 if (dev->parent != &wwandev->dev)
332 if (sscanf(dev_name(dev), fmt, &id) != 1)
334 if (id < 0 || id >= max_ports)
338 class_dev_iter_exit(&iter);
340 /* Allocate unique id */
341 id = find_first_zero_bit(idmap, max_ports);
342 free_page((unsigned long)idmap);
344 snprintf(buf, sizeof(buf), fmt, id); /* Name generation */
346 dev = device_find_child_by_name(&wwandev->dev, buf);
352 return dev_set_name(&port->dev, buf);
355 struct wwan_port *wwan_create_port(struct device *parent,
356 enum wwan_port_type type,
357 const struct wwan_port_ops *ops,
360 struct wwan_device *wwandev;
361 struct wwan_port *port;
365 if (type > WWAN_PORT_MAX || !ops)
366 return ERR_PTR(-EINVAL);
368 /* A port is always a child of a WWAN device, retrieve (allocate or
369 * pick) the WWAN device based on the provided parent device.
371 wwandev = wwan_create_dev(parent);
373 return ERR_CAST(wwandev);
375 /* A port is exposed as character device, get a minor */
376 minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
379 goto error_wwandev_remove;
382 port = kzalloc(sizeof(*port), GFP_KERNEL);
385 ida_free(&minors, minor);
386 goto error_wwandev_remove;
391 mutex_init(&port->ops_lock);
392 skb_queue_head_init(&port->rxq);
393 init_waitqueue_head(&port->waitqueue);
394 mutex_init(&port->data_lock);
396 port->dev.parent = &wwandev->dev;
397 port->dev.class = wwan_class;
398 port->dev.type = &wwan_port_dev_type;
399 port->dev.devt = MKDEV(wwan_major, minor);
400 dev_set_drvdata(&port->dev, drvdata);
402 /* allocate unique name based on wwan device id, port type and number */
403 snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
404 wwan_port_types[port->type].devsuf);
406 /* Serialize ports registration */
407 mutex_lock(&wwan_register_lock);
409 __wwan_port_dev_assign_name(port, namefmt);
410 err = device_register(&port->dev);
412 mutex_unlock(&wwan_register_lock);
415 goto error_put_device;
420 put_device(&port->dev);
421 error_wwandev_remove:
422 wwan_remove_dev(wwandev);
426 EXPORT_SYMBOL_GPL(wwan_create_port);
428 void wwan_remove_port(struct wwan_port *port)
430 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
432 mutex_lock(&port->ops_lock);
433 if (port->start_count)
434 port->ops->stop(port);
435 port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
436 mutex_unlock(&port->ops_lock);
438 wake_up_interruptible(&port->waitqueue);
440 skb_queue_purge(&port->rxq);
441 dev_set_drvdata(&port->dev, NULL);
442 device_unregister(&port->dev);
444 /* Release related wwan device */
445 wwan_remove_dev(wwandev);
447 EXPORT_SYMBOL_GPL(wwan_remove_port);
449 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
451 skb_queue_tail(&port->rxq, skb);
452 wake_up_interruptible(&port->waitqueue);
454 EXPORT_SYMBOL_GPL(wwan_port_rx);
456 void wwan_port_txon(struct wwan_port *port)
458 clear_bit(WWAN_PORT_TX_OFF, &port->flags);
459 wake_up_interruptible(&port->waitqueue);
461 EXPORT_SYMBOL_GPL(wwan_port_txon);
463 void wwan_port_txoff(struct wwan_port *port)
465 set_bit(WWAN_PORT_TX_OFF, &port->flags);
467 EXPORT_SYMBOL_GPL(wwan_port_txoff);
469 void *wwan_port_get_drvdata(struct wwan_port *port)
471 return dev_get_drvdata(&port->dev);
473 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
475 static int wwan_port_op_start(struct wwan_port *port)
479 mutex_lock(&port->ops_lock);
480 if (!port->ops) { /* Port got unplugged */
485 /* If port is already started, don't start again */
486 if (!port->start_count)
487 ret = port->ops->start(port);
493 mutex_unlock(&port->ops_lock);
498 static void wwan_port_op_stop(struct wwan_port *port)
500 mutex_lock(&port->ops_lock);
502 if (!port->start_count) {
504 port->ops->stop(port);
505 skb_queue_purge(&port->rxq);
507 mutex_unlock(&port->ops_lock);
510 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
515 mutex_lock(&port->ops_lock);
516 if (!port->ops) { /* Port got unplugged */
521 if (nonblock || !port->ops->tx_blocking)
522 ret = port->ops->tx(port, skb);
524 ret = port->ops->tx_blocking(port, skb);
527 mutex_unlock(&port->ops_lock);
532 static bool is_read_blocked(struct wwan_port *port)
534 return skb_queue_empty(&port->rxq) && port->ops;
537 static bool is_write_blocked(struct wwan_port *port)
539 return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
542 static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
544 if (!is_read_blocked(port))
550 if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
556 static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
558 if (!is_write_blocked(port))
564 if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
570 static int wwan_port_fops_open(struct inode *inode, struct file *file)
572 struct wwan_port *port;
575 port = wwan_port_get_by_minor(iminor(inode));
577 return PTR_ERR(port);
579 file->private_data = port;
580 stream_open(inode, file);
582 err = wwan_port_op_start(port);
584 put_device(&port->dev);
589 static int wwan_port_fops_release(struct inode *inode, struct file *filp)
591 struct wwan_port *port = filp->private_data;
593 wwan_port_op_stop(port);
594 put_device(&port->dev);
599 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
600 size_t count, loff_t *ppos)
602 struct wwan_port *port = filp->private_data;
607 ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
611 skb = skb_dequeue(&port->rxq);
615 copied = min_t(size_t, count, skb->len);
616 if (copy_to_user(buf, skb->data, copied)) {
620 skb_pull(skb, copied);
622 /* skb is not fully consumed, keep it in the queue */
624 skb_queue_head(&port->rxq, skb);
631 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
632 size_t count, loff_t *offp)
634 struct wwan_port *port = filp->private_data;
638 ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
642 skb = alloc_skb(count, GFP_KERNEL);
646 if (copy_from_user(skb_put(skb, count), buf, count)) {
651 ret = wwan_port_op_tx(port, skb, !!(filp->f_flags & O_NONBLOCK));
660 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
662 struct wwan_port *port = filp->private_data;
665 poll_wait(filp, &port->waitqueue, wait);
667 mutex_lock(&port->ops_lock);
668 if (port->ops && port->ops->tx_poll)
669 mask |= port->ops->tx_poll(port, filp, wait);
670 else if (!is_write_blocked(port))
671 mask |= EPOLLOUT | EPOLLWRNORM;
672 if (!is_read_blocked(port))
673 mask |= EPOLLIN | EPOLLRDNORM;
675 mask |= EPOLLHUP | EPOLLERR;
676 mutex_unlock(&port->ops_lock);
681 /* Implements minimalistic stub terminal IOCTLs support */
682 static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
687 mutex_lock(&port->data_lock);
694 if (copy_to_user((void __user *)arg, &port->at_data.termios,
695 sizeof(struct termios)))
702 if (copy_from_user(&port->at_data.termios, (void __user *)arg,
703 sizeof(struct termios)))
709 if (copy_to_user((void __user *)arg, &port->at_data.termios,
710 sizeof(struct termios2)))
717 if (copy_from_user(&port->at_data.termios, (void __user *)arg,
718 sizeof(struct termios2)))
724 ret = put_user(port->at_data.mdmbits, (int __user *)arg);
732 if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
737 port->at_data.mdmbits &= ~mdmbits;
738 else if (cmd == TIOCMBIS)
739 port->at_data.mdmbits |= mdmbits;
741 port->at_data.mdmbits = mdmbits;
749 mutex_unlock(&port->data_lock);
754 static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
757 struct wwan_port *port = filp->private_data;
760 if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */
761 res = wwan_port_fops_at_ioctl(port, cmd, arg);
762 if (res != -ENOIOCTLCMD)
767 case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */
772 spin_lock_irqsave(&port->rxq.lock, flags);
773 skb_queue_walk(&port->rxq, skb)
775 spin_unlock_irqrestore(&port->rxq.lock, flags);
777 return put_user(amount, (int __user *)arg);
785 static const struct file_operations wwan_port_fops = {
786 .owner = THIS_MODULE,
787 .open = wwan_port_fops_open,
788 .release = wwan_port_fops_release,
789 .read = wwan_port_fops_read,
790 .write = wwan_port_fops_write,
791 .poll = wwan_port_fops_poll,
792 .unlocked_ioctl = wwan_port_fops_ioctl,
794 .compat_ioctl = compat_ptr_ioctl,
796 .llseek = noop_llseek,
799 static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
800 struct netlink_ext_ack *extack)
805 if (!tb[IFLA_PARENT_DEV_NAME])
808 if (!data[IFLA_WWAN_LINK_ID])
814 static struct device_type wwan_type = { .name = "wwan" };
816 static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],
818 unsigned char name_assign_type,
819 unsigned int num_tx_queues,
820 unsigned int num_rx_queues)
822 const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]);
823 struct wwan_device *wwandev = wwan_dev_get_by_name(devname);
824 struct net_device *dev;
825 unsigned int priv_size;
828 return ERR_CAST(wwandev);
830 /* only supported if ops were registered (not just ports) */
832 dev = ERR_PTR(-EOPNOTSUPP);
836 priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size;
837 dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type,
838 wwandev->ops->setup, num_tx_queues, num_rx_queues);
841 SET_NETDEV_DEV(dev, &wwandev->dev);
842 SET_NETDEV_DEVTYPE(dev, &wwan_type);
846 /* release the reference */
847 put_device(&wwandev->dev);
851 static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev,
852 struct nlattr *tb[], struct nlattr *data[],
853 struct netlink_ext_ack *extack)
855 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
856 u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]);
857 struct wwan_netdev_priv *priv = netdev_priv(dev);
861 return PTR_ERR(wwandev);
863 /* shouldn't have a netdev (left) with us as parent so WARN */
864 if (WARN_ON(!wwandev->ops)) {
869 priv->link_id = link_id;
870 if (wwandev->ops->newlink)
871 ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev,
874 ret = register_netdevice(dev);
877 /* release the reference */
878 put_device(&wwandev->dev);
882 static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head)
884 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
889 /* shouldn't have a netdev (left) with us as parent so WARN */
890 if (WARN_ON(!wwandev->ops))
893 if (wwandev->ops->dellink)
894 wwandev->ops->dellink(wwandev->ops_ctxt, dev, head);
896 unregister_netdevice_queue(dev, head);
899 /* release the reference */
900 put_device(&wwandev->dev);
903 static size_t wwan_rtnl_get_size(const struct net_device *dev)
906 nla_total_size(4) + /* IFLA_WWAN_LINK_ID */
910 static int wwan_rtnl_fill_info(struct sk_buff *skb,
911 const struct net_device *dev)
913 struct wwan_netdev_priv *priv = netdev_priv(dev);
915 if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id))
916 goto nla_put_failure;
924 static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = {
925 [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 },
928 static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = {
930 .maxtype = __IFLA_WWAN_MAX,
931 .alloc = wwan_rtnl_alloc,
932 .validate = wwan_rtnl_validate,
933 .newlink = wwan_rtnl_newlink,
934 .dellink = wwan_rtnl_dellink,
935 .get_size = wwan_rtnl_get_size,
936 .fill_info = wwan_rtnl_fill_info,
937 .policy = wwan_rtnl_policy,
940 static void wwan_create_default_link(struct wwan_device *wwandev,
943 struct nlattr *tb[IFLA_MAX + 1], *linkinfo[IFLA_INFO_MAX + 1];
944 struct nlattr *data[IFLA_WWAN_MAX + 1];
945 struct net_device *dev;
946 struct nlmsghdr *nlh;
949 /* Forge attributes required to create a WWAN netdev. We first
950 * build a netlink message and then parse it. This looks
951 * odd, but such approach is less error prone.
953 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
956 nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0);
960 if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev)))
962 tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO);
963 if (!tb[IFLA_LINKINFO])
965 linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA);
966 if (!linkinfo[IFLA_INFO_DATA])
968 if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id))
970 nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]);
971 nla_nest_end(msg, tb[IFLA_LINKINFO]);
975 /* The next three parsing calls can not fail */
976 nlmsg_parse_deprecated(nlh, 0, tb, IFLA_MAX, NULL, NULL);
977 nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
979 nla_parse_nested_deprecated(data, IFLA_WWAN_MAX,
980 linkinfo[IFLA_INFO_DATA], NULL, NULL);
984 dev = rtnl_create_link(&init_net, "wwan%d", NET_NAME_ENUM,
985 &wwan_rtnl_link_ops, tb, NULL);
986 if (WARN_ON(IS_ERR(dev)))
989 if (WARN_ON(wwan_rtnl_newlink(&init_net, dev, tb, data, NULL))) {
994 rtnl_configure_link(dev, NULL); /* Link initialized, notify new link */
1004 * wwan_register_ops - register WWAN device ops
1005 * @parent: Device to use as parent and shared by all WWAN ports and
1007 * @ops: operations to register
1008 * @ctxt: context to pass to operations
1009 * @def_link_id: id of the default link that will be automatically created by
1010 * the WWAN core for the WWAN device. The default link will not be created
1011 * if the passed value is WWAN_NO_DEFAULT_LINK.
1013 * Returns: 0 on success, a negative error code on failure
1015 int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
1016 void *ctxt, u32 def_link_id)
1018 struct wwan_device *wwandev;
1020 if (WARN_ON(!parent || !ops || !ops->setup))
1023 wwandev = wwan_create_dev(parent);
1024 if (IS_ERR(wwandev))
1025 return PTR_ERR(wwandev);
1027 if (WARN_ON(wwandev->ops)) {
1028 wwan_remove_dev(wwandev);
1033 wwandev->ops_ctxt = ctxt;
1035 /* NB: we do not abort ops registration in case of default link
1036 * creation failure. Link ops is the management interface, while the
1037 * default link creation is a service option. And we should not prevent
1038 * a user from manually creating a link latter if service option failed
1041 if (def_link_id != WWAN_NO_DEFAULT_LINK)
1042 wwan_create_default_link(wwandev, def_link_id);
1046 EXPORT_SYMBOL_GPL(wwan_register_ops);
1048 /* Enqueue child netdev deletion */
1049 static int wwan_child_dellink(struct device *dev, void *data)
1051 struct list_head *kill_list = data;
1053 if (dev->type == &wwan_type)
1054 wwan_rtnl_dellink(to_net_dev(dev), kill_list);
1060 * wwan_unregister_ops - remove WWAN device ops
1061 * @parent: Device to use as parent and shared by all WWAN ports and
1064 void wwan_unregister_ops(struct device *parent)
1066 struct wwan_device *wwandev = wwan_dev_get_by_parent(parent);
1067 LIST_HEAD(kill_list);
1069 if (WARN_ON(IS_ERR(wwandev)))
1071 if (WARN_ON(!wwandev->ops)) {
1072 put_device(&wwandev->dev);
1076 /* put the reference obtained by wwan_dev_get_by_parent(),
1077 * we should still have one (that the owner is giving back
1078 * now) due to the ops being assigned.
1080 put_device(&wwandev->dev);
1082 rtnl_lock(); /* Prevent concurent netdev(s) creation/destroying */
1084 /* Remove all child netdev(s), using batch removing */
1085 device_for_each_child(&wwandev->dev, &kill_list,
1086 wwan_child_dellink);
1087 unregister_netdevice_many(&kill_list);
1089 wwandev->ops = NULL; /* Finally remove ops */
1093 wwandev->ops_ctxt = NULL;
1094 wwan_remove_dev(wwandev);
1096 EXPORT_SYMBOL_GPL(wwan_unregister_ops);
1098 static int __init wwan_init(void)
1102 err = rtnl_link_register(&wwan_rtnl_link_ops);
1106 wwan_class = class_create(THIS_MODULE, "wwan");
1107 if (IS_ERR(wwan_class)) {
1108 err = PTR_ERR(wwan_class);
1112 /* chrdev used for wwan ports */
1113 wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
1115 if (wwan_major < 0) {
1123 class_destroy(wwan_class);
1125 rtnl_link_unregister(&wwan_rtnl_link_ops);
1129 static void __exit wwan_exit(void)
1131 __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port");
1132 rtnl_link_unregister(&wwan_rtnl_link_ops);
1133 class_destroy(wwan_class);
1136 module_init(wwan_init);
1137 module_exit(wwan_exit);
1139 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
1140 MODULE_DESCRIPTION("WWAN core");
1141 MODULE_LICENSE("GPL v2");