xref: /linux/drivers/net/wwan/wwan_core.c (revision 21ef2d065ad3f0cfbf2ae51260bf962a9fa2c643)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* WWAN Driver Core
3  *
4  * Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org>
5  * Copyright (c) 2025, Sergey Ryazanov <ryazanov.s.a@gmail.com>
6  */
7 
8 #include <linux/bitmap.h>
9 #include <linux/err.h>
10 #include <linux/errno.h>
11 #include <linux/debugfs.h>
12 #include <linux/fs.h>
13 #include <linux/init.h>
14 #include <linux/idr.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/poll.h>
18 #include <linux/skbuff.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/uaccess.h>
22 #include <linux/termios.h>
23 #include <linux/gnss.h>
24 #include <linux/wwan.h>
25 #include <net/rtnetlink.h>
26 #include <uapi/linux/wwan.h>
27 
28 /* Maximum number of minors in use */
29 #define WWAN_MAX_MINORS		(1 << MINORBITS)
30 
31 static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
32 static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
33 static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */
34 static const struct class wwan_class = {
35 	.name = "wwan",
36 };
37 static int wwan_major;
38 static struct dentry *wwan_debugfs_dir;
39 
40 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
41 #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
42 
43 /* WWAN port flags */
44 #define WWAN_PORT_TX_OFF	0
45 #define WWAN_PORT_EXCLUSIVE	1
46 
47 /**
48  * struct wwan_device - The structure that defines a WWAN device
49  *
50  * @id: WWAN device unique ID.
51  * @refcount: Reference count of this WWAN device. When this refcount reaches
52  * zero, the device is deleted. NB: access is protected by global
53  * wwan_register_lock mutex.
54  * @dev: Underlying device.
55  * @ops: wwan device ops
56  * @ops_ctxt: context to pass to ops
57  * @debugfs_dir:  WWAN device debugfs dir
58  */
59 struct wwan_device {
60 	unsigned int id;
61 	int refcount;
62 	struct device dev;
63 	const struct wwan_ops *ops;
64 	void *ops_ctxt;
65 #ifdef CONFIG_WWAN_DEBUGFS
66 	struct dentry *debugfs_dir;
67 #endif
68 };
69 
70 /**
71  * struct wwan_port - The structure that defines a WWAN port
72  * @type: Port type
73  * @start_count: Port start counter
74  * @flags: Store port state and capabilities
75  * @ops: Pointer to WWAN port operations
76  * @ops_lock: Protect port ops
77  * @dev: Underlying device
78  * @rxq: Buffer inbound queue
79  * @waitqueue: The waitqueue for port fops (read/write/poll)
80  * @data_lock: Port specific data access serialization
81  * @headroom_len: SKB reserved headroom size
82  * @frag_len: Length to fragment packet
83  * @at_data: AT port specific data
84  * @gnss: Pointer to GNSS device associated with this port
85  */
86 struct wwan_port {
87 	enum wwan_port_type type;
88 	unsigned int start_count;
89 	unsigned long flags;
90 	const struct wwan_port_ops *ops;
91 	struct mutex ops_lock; /* Serialize ops + protect against removal */
92 	struct device dev;
93 	struct sk_buff_head rxq;
94 	wait_queue_head_t waitqueue;
95 	struct mutex data_lock;	/* Port specific data access serialization */
96 	size_t headroom_len;
97 	size_t frag_len;
98 	union {
99 		struct {
100 			struct ktermios termios;
101 			int mdmbits;
102 		} at_data;
103 		struct gnss_device *gnss;
104 	};
105 };
106 
107 static int wwan_port_op_start(struct wwan_port *port);
108 static void wwan_port_op_stop(struct wwan_port *port);
109 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
110 			   bool nonblock);
111 static int wwan_wait_tx(struct wwan_port *port, bool nonblock);
112 
113 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
114 {
115 	struct wwan_device *wwan = to_wwan_dev(dev);
116 
117 	return sprintf(buf, "%d\n", wwan->id);
118 }
119 static DEVICE_ATTR_RO(index);
120 
121 static struct attribute *wwan_dev_attrs[] = {
122 	&dev_attr_index.attr,
123 	NULL,
124 };
125 ATTRIBUTE_GROUPS(wwan_dev);
126 
127 static void wwan_dev_destroy(struct device *dev)
128 {
129 	struct wwan_device *wwandev = to_wwan_dev(dev);
130 
131 	ida_free(&wwan_dev_ids, wwandev->id);
132 	kfree(wwandev);
133 }
134 
135 static const struct device_type wwan_dev_type = {
136 	.name    = "wwan_dev",
137 	.release = wwan_dev_destroy,
138 	.groups = wwan_dev_groups,
139 };
140 
141 static int wwan_dev_parent_match(struct device *dev, const void *parent)
142 {
143 	return (dev->type == &wwan_dev_type &&
144 		(dev->parent == parent || dev == parent));
145 }
146 
147 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
148 {
149 	struct device *dev;
150 
151 	dev = class_find_device(&wwan_class, NULL, parent, wwan_dev_parent_match);
152 	if (!dev)
153 		return ERR_PTR(-ENODEV);
154 
155 	return to_wwan_dev(dev);
156 }
157 
158 static int wwan_dev_name_match(struct device *dev, const void *name)
159 {
160 	return dev->type == &wwan_dev_type &&
161 	       strcmp(dev_name(dev), name) == 0;
162 }
163 
164 static struct wwan_device *wwan_dev_get_by_name(const char *name)
165 {
166 	struct device *dev;
167 
168 	dev = class_find_device(&wwan_class, NULL, name, wwan_dev_name_match);
169 	if (!dev)
170 		return ERR_PTR(-ENODEV);
171 
172 	return to_wwan_dev(dev);
173 }
174 
175 #ifdef CONFIG_WWAN_DEBUGFS
176 struct dentry *wwan_get_debugfs_dir(struct device *parent)
177 {
178 	struct wwan_device *wwandev;
179 
180 	wwandev = wwan_dev_get_by_parent(parent);
181 	if (IS_ERR(wwandev))
182 		return ERR_CAST(wwandev);
183 
184 	return wwandev->debugfs_dir;
185 }
186 EXPORT_SYMBOL_GPL(wwan_get_debugfs_dir);
187 
188 static int wwan_dev_debugfs_match(struct device *dev, const void *dir)
189 {
190 	struct wwan_device *wwandev;
191 
192 	if (dev->type != &wwan_dev_type)
193 		return 0;
194 
195 	wwandev = to_wwan_dev(dev);
196 
197 	return wwandev->debugfs_dir == dir;
198 }
199 
200 static struct wwan_device *wwan_dev_get_by_debugfs(struct dentry *dir)
201 {
202 	struct device *dev;
203 
204 	dev = class_find_device(&wwan_class, NULL, dir, wwan_dev_debugfs_match);
205 	if (!dev)
206 		return ERR_PTR(-ENODEV);
207 
208 	return to_wwan_dev(dev);
209 }
210 
211 void wwan_put_debugfs_dir(struct dentry *dir)
212 {
213 	struct wwan_device *wwandev = wwan_dev_get_by_debugfs(dir);
214 
215 	if (WARN_ON(IS_ERR(wwandev)))
216 		return;
217 
218 	/* wwan_dev_get_by_debugfs() also got a reference */
219 	put_device(&wwandev->dev);
220 	put_device(&wwandev->dev);
221 }
222 EXPORT_SYMBOL_GPL(wwan_put_debugfs_dir);
223 #endif
224 
225 /* This function allocates and registers a new WWAN device OR if a WWAN device
226  * already exist for the given parent, it gets a reference and return it.
227  * This function is not exported (for now), it is called indirectly via
228  * wwan_create_port().
229  */
230 static struct wwan_device *wwan_create_dev(struct device *parent)
231 {
232 	struct wwan_device *wwandev;
233 	int err, id;
234 
235 	/* The 'find-alloc-register' operation must be protected against
236 	 * concurrent execution, a WWAN device is possibly shared between
237 	 * multiple callers or concurrently unregistered from wwan_remove_dev().
238 	 */
239 	mutex_lock(&wwan_register_lock);
240 
241 	/* If wwandev already exists, return it */
242 	wwandev = wwan_dev_get_by_parent(parent);
243 	if (!IS_ERR(wwandev)) {
244 		wwandev->refcount++;
245 		goto done_unlock;
246 	}
247 
248 	id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
249 	if (id < 0) {
250 		wwandev = ERR_PTR(id);
251 		goto done_unlock;
252 	}
253 
254 	wwandev = kzalloc_obj(*wwandev);
255 	if (!wwandev) {
256 		wwandev = ERR_PTR(-ENOMEM);
257 		ida_free(&wwan_dev_ids, id);
258 		goto done_unlock;
259 	}
260 
261 	wwandev->dev.parent = parent;
262 	wwandev->dev.class = &wwan_class;
263 	wwandev->dev.type = &wwan_dev_type;
264 	wwandev->id = id;
265 	wwandev->refcount = 1;
266 	dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
267 
268 	err = device_register(&wwandev->dev);
269 	if (err) {
270 		put_device(&wwandev->dev);
271 		wwandev = ERR_PTR(err);
272 		goto done_unlock;
273 	}
274 
275 #ifdef CONFIG_WWAN_DEBUGFS
276 	wwandev->debugfs_dir =
277 			debugfs_create_dir(kobject_name(&wwandev->dev.kobj),
278 					   wwan_debugfs_dir);
279 #endif
280 
281 done_unlock:
282 	mutex_unlock(&wwan_register_lock);
283 
284 	return wwandev;
285 }
286 
287 static void wwan_remove_dev(struct wwan_device *wwandev)
288 {
289 	/* Prevent concurrent picking from wwan_create_dev */
290 	mutex_lock(&wwan_register_lock);
291 
292 	if (--wwandev->refcount <= 0) {
293 		struct device *child = device_find_any_child(&wwandev->dev);
294 
295 		put_device(child);
296 		if (WARN_ON(wwandev->ops || child))	/* Paranoid */
297 			goto out_unlock;
298 
299 #ifdef CONFIG_WWAN_DEBUGFS
300 		debugfs_remove_recursive(wwandev->debugfs_dir);
301 #endif
302 		device_unregister(&wwandev->dev);
303 	} else {
304 		put_device(&wwandev->dev);
305 	}
306 
307 out_unlock:
308 	mutex_unlock(&wwan_register_lock);
309 }
310 
311 /* ------- WWAN port management ------- */
312 
313 static const struct {
314 	const char * const name;	/* Port type name */
315 	const char * const devsuf;	/* Port device name suffix */
316 } wwan_port_types[WWAN_PORT_MAX + 1] = {
317 	[WWAN_PORT_AT] = {
318 		.name = "AT",
319 		.devsuf = "at",
320 	},
321 	[WWAN_PORT_MBIM] = {
322 		.name = "MBIM",
323 		.devsuf = "mbim",
324 	},
325 	[WWAN_PORT_QMI] = {
326 		.name = "QMI",
327 		.devsuf = "qmi",
328 	},
329 	[WWAN_PORT_QCDM] = {
330 		.name = "QCDM",
331 		.devsuf = "qcdm",
332 	},
333 	[WWAN_PORT_FIREHOSE] = {
334 		.name = "FIREHOSE",
335 		.devsuf = "firehose",
336 	},
337 	[WWAN_PORT_XMMRPC] = {
338 		.name = "XMMRPC",
339 		.devsuf = "xmmrpc",
340 	},
341 	[WWAN_PORT_FASTBOOT] = {
342 		.name = "FASTBOOT",
343 		.devsuf = "fastboot",
344 	},
345 	[WWAN_PORT_ADB] = {
346 		.name = "ADB",
347 		.devsuf = "adb",
348 	},
349 	[WWAN_PORT_MIPC] = {
350 		.name = "MIPC",
351 		.devsuf = "mipc",
352 	},
353 	/* WWAN_PORT_NMEA is exported via the GNSS subsystem */
354 };
355 
356 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
357 			 char *buf)
358 {
359 	struct wwan_port *port = to_wwan_port(dev);
360 
361 	return sprintf(buf, "%s\n", wwan_port_types[port->type].name);
362 }
363 static DEVICE_ATTR_RO(type);
364 
365 static struct attribute *wwan_port_attrs[] = {
366 	&dev_attr_type.attr,
367 	NULL,
368 };
369 ATTRIBUTE_GROUPS(wwan_port);
370 
371 static void wwan_port_destroy(struct device *dev)
372 {
373 	struct wwan_port *port = to_wwan_port(dev);
374 
375 	if (dev->class == &wwan_class)
376 		ida_free(&minors, MINOR(dev->devt));
377 	mutex_destroy(&port->data_lock);
378 	mutex_destroy(&port->ops_lock);
379 	kfree(port);
380 }
381 
382 static const struct device_type wwan_port_dev_type = {
383 	.name = "wwan_port",
384 	.release = wwan_port_destroy,
385 	.groups = wwan_port_groups,
386 };
387 
388 static int wwan_port_minor_match(struct device *dev, const void *minor)
389 {
390 	return (dev->type == &wwan_port_dev_type &&
391 		MINOR(dev->devt) == *(unsigned int *)minor);
392 }
393 
394 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
395 {
396 	struct device *dev;
397 
398 	dev = class_find_device(&wwan_class, NULL, &minor, wwan_port_minor_match);
399 	if (!dev)
400 		return ERR_PTR(-ENODEV);
401 
402 	return to_wwan_port(dev);
403 }
404 
405 /* Allocate and set unique name based on passed format
406  *
407  * Name allocation approach is highly inspired by the __dev_alloc_name()
408  * function.
409  *
410  * To avoid names collision, the caller must prevent the new port device
411  * registration as well as concurrent invocation of this function.
412  */
413 static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt)
414 {
415 	struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
416 	const unsigned int max_ports = PAGE_SIZE * 8;
417 	struct class_dev_iter iter;
418 	unsigned long *idmap;
419 	struct device *dev;
420 	char buf[0x20];
421 	int id;
422 
423 	idmap = bitmap_zalloc(max_ports, GFP_KERNEL);
424 	if (!idmap)
425 		return -ENOMEM;
426 
427 	/* Collect ids of same name format ports */
428 	class_dev_iter_init(&iter, &wwan_class, NULL, &wwan_port_dev_type);
429 	while ((dev = class_dev_iter_next(&iter))) {
430 		if (dev->parent != &wwandev->dev)
431 			continue;
432 		if (sscanf(dev_name(dev), fmt, &id) != 1)
433 			continue;
434 		if (id < 0 || id >= max_ports)
435 			continue;
436 		set_bit(id, idmap);
437 	}
438 	class_dev_iter_exit(&iter);
439 
440 	/* Allocate unique id */
441 	id = find_first_zero_bit(idmap, max_ports);
442 	bitmap_free(idmap);
443 
444 	snprintf(buf, sizeof(buf), fmt, id);	/* Name generation */
445 
446 	dev = device_find_child_by_name(&wwandev->dev, buf);
447 	if (dev) {
448 		put_device(dev);
449 		return -ENFILE;
450 	}
451 
452 	return dev_set_name(&port->dev, "%s", buf);
453 }
454 
455 /* Register a regular WWAN port device (e.g. AT, MBIM, etc.) */
456 static int wwan_port_register_wwan(struct wwan_port *port)
457 {
458 	struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
459 	char namefmt[0x20];
460 	int minor, err;
461 
462 	/* A port is exposed as character device, get a minor */
463 	minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
464 	if (minor < 0)
465 		return minor;
466 
467 	port->dev.class = &wwan_class;
468 	port->dev.devt = MKDEV(wwan_major, minor);
469 
470 	/* allocate unique name based on wwan device id, port type and number */
471 	snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id,
472 		 wwan_port_types[port->type].devsuf);
473 
474 	/* Serialize ports registration */
475 	mutex_lock(&wwan_register_lock);
476 
477 	__wwan_port_dev_assign_name(port, namefmt);
478 	err = device_add(&port->dev);
479 
480 	mutex_unlock(&wwan_register_lock);
481 
482 	if (err) {
483 		ida_free(&minors, minor);
484 		port->dev.class = NULL;
485 		return err;
486 	}
487 
488 	dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev));
489 
490 	return 0;
491 }
492 
493 /* Unregister a regular WWAN port (e.g. AT, MBIM, etc) */
494 static void wwan_port_unregister_wwan(struct wwan_port *port)
495 {
496 	struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
497 
498 	dev_set_drvdata(&port->dev, NULL);
499 
500 	dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&port->dev));
501 
502 	device_del(&port->dev);
503 }
504 
505 #if IS_ENABLED(CONFIG_GNSS)
506 static int wwan_gnss_open(struct gnss_device *gdev)
507 {
508 	return wwan_port_op_start(gnss_get_drvdata(gdev));
509 }
510 
511 static void wwan_gnss_close(struct gnss_device *gdev)
512 {
513 	wwan_port_op_stop(gnss_get_drvdata(gdev));
514 }
515 
516 static int wwan_gnss_write(struct gnss_device *gdev, const unsigned char *buf,
517 			   size_t count)
518 {
519 	struct wwan_port *port = gnss_get_drvdata(gdev);
520 	struct sk_buff *skb, *head = NULL, *tail = NULL;
521 	size_t frag_len, remain = count;
522 	int ret;
523 
524 	ret = wwan_wait_tx(port, false);
525 	if (ret)
526 		return ret;
527 
528 	do {
529 		frag_len = min(remain, port->frag_len);
530 		skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL);
531 		if (!skb) {
532 			ret = -ENOMEM;
533 			goto freeskb;
534 		}
535 		skb_reserve(skb, port->headroom_len);
536 		memcpy(skb_put(skb, frag_len), buf + count - remain, frag_len);
537 
538 		if (!head) {
539 			head = skb;
540 		} else {
541 			if (!tail)
542 				skb_shinfo(head)->frag_list = skb;
543 			else
544 				tail->next = skb;
545 
546 			tail = skb;
547 			head->data_len += skb->len;
548 			head->len += skb->len;
549 			head->truesize += skb->truesize;
550 		}
551 	} while (remain -= frag_len);
552 
553 	ret = wwan_port_op_tx(port, head, false);
554 	if (!ret)
555 		return count;
556 
557 freeskb:
558 	kfree_skb(head);
559 	return ret;
560 }
561 
562 static struct gnss_operations wwan_gnss_ops = {
563 	.open = wwan_gnss_open,
564 	.close = wwan_gnss_close,
565 	.write_raw = wwan_gnss_write,
566 };
567 
568 /* GNSS port specific device registration */
569 static int wwan_port_register_gnss(struct wwan_port *port)
570 {
571 	struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
572 	struct gnss_device *gdev;
573 	int err;
574 
575 	gdev = gnss_allocate_device(&wwandev->dev);
576 	if (!gdev)
577 		return -ENOMEM;
578 
579 	/* NB: for now we support only NMEA WWAN port type, so hardcode
580 	 * the GNSS port type. If more GNSS WWAN port types will be added,
581 	 * then we should dynamically map WWAN port type to GNSS type.
582 	 */
583 	gdev->type = GNSS_TYPE_NMEA;
584 	gdev->ops = &wwan_gnss_ops;
585 	gnss_set_drvdata(gdev, port);
586 
587 	port->gnss = gdev;
588 
589 	err = gnss_register_device(gdev);
590 	if (err) {
591 		gnss_put_device(gdev);
592 		return err;
593 	}
594 
595 	dev_info(&wwandev->dev, "port %s attached\n", dev_name(&gdev->dev));
596 
597 	return 0;
598 }
599 
600 /* GNSS port specific device unregistration */
601 static void wwan_port_unregister_gnss(struct wwan_port *port)
602 {
603 	struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
604 	struct gnss_device *gdev = port->gnss;
605 
606 	dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&gdev->dev));
607 
608 	gnss_deregister_device(gdev);
609 	gnss_put_device(gdev);
610 }
611 #else
612 static int wwan_port_register_gnss(struct wwan_port *port)
613 {
614 	return -EOPNOTSUPP;
615 }
616 
617 static void wwan_port_unregister_gnss(struct wwan_port *port)
618 {
619 	WARN_ON(1);	/* This handler cannot be called */
620 }
621 #endif
622 
623 struct wwan_port *wwan_create_port(struct device *parent,
624 				   enum wwan_port_type type,
625 				   const struct wwan_port_ops *ops,
626 				   struct wwan_port_caps *caps,
627 				   void *drvdata)
628 {
629 	struct wwan_device *wwandev;
630 	struct wwan_port *port;
631 	int err;
632 
633 	if (type > WWAN_PORT_MAX || !ops)
634 		return ERR_PTR(-EINVAL);
635 
636 	/* A port is always a child of a WWAN device, retrieve (allocate or
637 	 * pick) the WWAN device based on the provided parent device.
638 	 */
639 	wwandev = wwan_create_dev(parent);
640 	if (IS_ERR(wwandev))
641 		return ERR_CAST(wwandev);
642 
643 	port = kzalloc_obj(*port);
644 	if (!port) {
645 		err = -ENOMEM;
646 		goto error_wwandev_remove;
647 	}
648 
649 	port->type = type;
650 	port->ops = ops;
651 	port->frag_len = caps ? caps->frag_len : SIZE_MAX;
652 	port->headroom_len = caps ? caps->headroom_len : 0;
653 	mutex_init(&port->ops_lock);
654 	skb_queue_head_init(&port->rxq);
655 	init_waitqueue_head(&port->waitqueue);
656 	mutex_init(&port->data_lock);
657 
658 	port->dev.parent = &wwandev->dev;
659 	port->dev.type = &wwan_port_dev_type;
660 	dev_set_drvdata(&port->dev, drvdata);
661 	device_initialize(&port->dev);
662 
663 	if (port->type == WWAN_PORT_NMEA)
664 		err = wwan_port_register_gnss(port);
665 	else
666 		err = wwan_port_register_wwan(port);
667 
668 	if (err)
669 		goto error_put_device;
670 
671 	return port;
672 
673 error_put_device:
674 	put_device(&port->dev);
675 error_wwandev_remove:
676 	wwan_remove_dev(wwandev);
677 
678 	return ERR_PTR(err);
679 }
680 EXPORT_SYMBOL_GPL(wwan_create_port);
681 
682 void wwan_remove_port(struct wwan_port *port)
683 {
684 	struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
685 
686 	mutex_lock(&port->ops_lock);
687 	if (port->start_count) {
688 		port->ops->stop(port);
689 		port->start_count = 0;
690 	}
691 	port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
692 	mutex_unlock(&port->ops_lock);
693 
694 	wake_up_interruptible(&port->waitqueue);
695 	skb_queue_purge(&port->rxq);
696 
697 	if (port->type == WWAN_PORT_NMEA)
698 		wwan_port_unregister_gnss(port);
699 	else
700 		wwan_port_unregister_wwan(port);
701 
702 	put_device(&port->dev);
703 
704 	/* Release related wwan device */
705 	wwan_remove_dev(wwandev);
706 }
707 EXPORT_SYMBOL_GPL(wwan_remove_port);
708 
709 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
710 {
711 	if (port->type == WWAN_PORT_NMEA) {
712 #if IS_ENABLED(CONFIG_GNSS)
713 		gnss_insert_raw(port->gnss, skb->data, skb->len);
714 #endif
715 		consume_skb(skb);
716 	} else {
717 		skb_queue_tail(&port->rxq, skb);
718 		wake_up_interruptible(&port->waitqueue);
719 	}
720 }
721 EXPORT_SYMBOL_GPL(wwan_port_rx);
722 
723 void wwan_port_txon(struct wwan_port *port)
724 {
725 	clear_bit(WWAN_PORT_TX_OFF, &port->flags);
726 	wake_up_interruptible(&port->waitqueue);
727 }
728 EXPORT_SYMBOL_GPL(wwan_port_txon);
729 
730 void wwan_port_txoff(struct wwan_port *port)
731 {
732 	set_bit(WWAN_PORT_TX_OFF, &port->flags);
733 }
734 EXPORT_SYMBOL_GPL(wwan_port_txoff);
735 
736 void *wwan_port_get_drvdata(struct wwan_port *port)
737 {
738 	return dev_get_drvdata(&port->dev);
739 }
740 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
741 
742 static int wwan_port_op_start(struct wwan_port *port)
743 {
744 	int ret = 0;
745 
746 	mutex_lock(&port->ops_lock);
747 	if (!port->ops) { /* Port got unplugged */
748 		ret = -ENODEV;
749 		goto out_unlock;
750 	}
751 
752 	if (test_bit(WWAN_PORT_EXCLUSIVE, &port->flags) &&
753 	    !capable(CAP_SYS_ADMIN)) {
754 		ret = -EBUSY;
755 		goto out_unlock;
756 	}
757 
758 	/* If port is already started, don't start again */
759 	if (!port->start_count)
760 		ret = port->ops->start(port);
761 
762 	if (!ret)
763 		port->start_count++;
764 
765 out_unlock:
766 	mutex_unlock(&port->ops_lock);
767 
768 	return ret;
769 }
770 
771 static void wwan_port_op_stop(struct wwan_port *port)
772 {
773 	mutex_lock(&port->ops_lock);
774 	port->start_count--;
775 	if (!port->start_count) {
776 		if (port->ops)
777 			port->ops->stop(port);
778 		skb_queue_purge(&port->rxq);
779 		clear_bit(WWAN_PORT_EXCLUSIVE, &port->flags);
780 	}
781 	mutex_unlock(&port->ops_lock);
782 }
783 
784 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb,
785 			   bool nonblock)
786 {
787 	int ret;
788 
789 	mutex_lock(&port->ops_lock);
790 	if (!port->ops) { /* Port got unplugged */
791 		ret = -ENODEV;
792 		goto out_unlock;
793 	}
794 
795 	if (nonblock || !port->ops->tx_blocking)
796 		ret = port->ops->tx(port, skb);
797 	else
798 		ret = port->ops->tx_blocking(port, skb);
799 
800 out_unlock:
801 	mutex_unlock(&port->ops_lock);
802 
803 	return ret;
804 }
805 
806 static bool is_read_blocked(struct wwan_port *port)
807 {
808 	return skb_queue_empty(&port->rxq) && port->ops;
809 }
810 
811 static bool is_write_blocked(struct wwan_port *port)
812 {
813 	return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
814 }
815 
816 static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
817 {
818 	if (!is_read_blocked(port))
819 		return 0;
820 
821 	if (nonblock)
822 		return -EAGAIN;
823 
824 	if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
825 		return -ERESTARTSYS;
826 
827 	return 0;
828 }
829 
830 static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
831 {
832 	if (!is_write_blocked(port))
833 		return 0;
834 
835 	if (nonblock)
836 		return -EAGAIN;
837 
838 	if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
839 		return -ERESTARTSYS;
840 
841 	return 0;
842 }
843 
844 static int wwan_port_fops_open(struct inode *inode, struct file *file)
845 {
846 	struct wwan_port *port;
847 	int err = 0;
848 
849 	port = wwan_port_get_by_minor(iminor(inode));
850 	if (IS_ERR(port))
851 		return PTR_ERR(port);
852 
853 	file->private_data = port;
854 	stream_open(inode, file);
855 
856 	err = wwan_port_op_start(port);
857 	if (err)
858 		put_device(&port->dev);
859 
860 	return err;
861 }
862 
863 static int wwan_port_fops_release(struct inode *inode, struct file *filp)
864 {
865 	struct wwan_port *port = filp->private_data;
866 
867 	wwan_port_op_stop(port);
868 	put_device(&port->dev);
869 
870 	return 0;
871 }
872 
873 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
874 				   size_t count, loff_t *ppos)
875 {
876 	struct wwan_port *port = filp->private_data;
877 	struct sk_buff *skb;
878 	size_t copied;
879 	int ret;
880 
881 	ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
882 	if (ret)
883 		return ret;
884 
885 	skb = skb_dequeue(&port->rxq);
886 	if (!skb)
887 		return -EIO;
888 
889 	copied = min_t(size_t, count, skb->len);
890 	if (copy_to_user(buf, skb->data, copied)) {
891 		kfree_skb(skb);
892 		return -EFAULT;
893 	}
894 	skb_pull(skb, copied);
895 
896 	/* skb is not fully consumed, keep it in the queue */
897 	if (skb->len)
898 		skb_queue_head(&port->rxq, skb);
899 	else
900 		consume_skb(skb);
901 
902 	return copied;
903 }
904 
905 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
906 				    size_t count, loff_t *offp)
907 {
908 	struct sk_buff *skb, *head = NULL, *tail = NULL;
909 	struct wwan_port *port = filp->private_data;
910 	size_t frag_len, remain = count;
911 	int ret;
912 
913 	ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
914 	if (ret)
915 		return ret;
916 
917 	do {
918 		frag_len = min(remain, port->frag_len);
919 		skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL);
920 		if (!skb) {
921 			ret = -ENOMEM;
922 			goto freeskb;
923 		}
924 		skb_reserve(skb, port->headroom_len);
925 
926 		if (!head) {
927 			head = skb;
928 		} else if (!tail) {
929 			skb_shinfo(head)->frag_list = skb;
930 			tail = skb;
931 		} else {
932 			tail->next = skb;
933 			tail = skb;
934 		}
935 
936 		if (copy_from_user(skb_put(skb, frag_len), buf + count - remain, frag_len)) {
937 			ret = -EFAULT;
938 			goto freeskb;
939 		}
940 
941 		if (skb != head) {
942 			head->data_len += skb->len;
943 			head->len += skb->len;
944 			head->truesize += skb->truesize;
945 		}
946 	} while (remain -= frag_len);
947 
948 	ret = wwan_port_op_tx(port, head, !!(filp->f_flags & O_NONBLOCK));
949 	if (!ret)
950 		return count;
951 
952 freeskb:
953 	kfree_skb(head);
954 	return ret;
955 }
956 
957 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
958 {
959 	struct wwan_port *port = filp->private_data;
960 	__poll_t mask = 0;
961 
962 	poll_wait(filp, &port->waitqueue, wait);
963 
964 	mutex_lock(&port->ops_lock);
965 	if (port->ops && port->ops->tx_poll)
966 		mask |= port->ops->tx_poll(port, filp, wait);
967 	else if (!is_write_blocked(port))
968 		mask |= EPOLLOUT | EPOLLWRNORM;
969 	if (!is_read_blocked(port))
970 		mask |= EPOLLIN | EPOLLRDNORM;
971 	if (!port->ops)
972 		mask |= EPOLLHUP | EPOLLERR;
973 	mutex_unlock(&port->ops_lock);
974 
975 	return mask;
976 }
977 
978 /* Implements minimalistic stub terminal IOCTLs support */
979 static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd,
980 				    unsigned long arg)
981 {
982 	int ret = 0;
983 
984 	mutex_lock(&port->data_lock);
985 
986 	switch (cmd) {
987 	case TCFLSH:
988 		break;
989 
990 	case TCGETS:
991 		if (copy_to_user((void __user *)arg, &port->at_data.termios,
992 				 sizeof(struct termios)))
993 			ret = -EFAULT;
994 		break;
995 
996 	case TCSETS:
997 	case TCSETSW:
998 	case TCSETSF:
999 		if (copy_from_user(&port->at_data.termios, (void __user *)arg,
1000 				   sizeof(struct termios)))
1001 			ret = -EFAULT;
1002 		break;
1003 
1004 #ifdef TCGETS2
1005 	case TCGETS2:
1006 		if (copy_to_user((void __user *)arg, &port->at_data.termios,
1007 				 sizeof(struct termios2)))
1008 			ret = -EFAULT;
1009 		break;
1010 
1011 	case TCSETS2:
1012 	case TCSETSW2:
1013 	case TCSETSF2:
1014 		if (copy_from_user(&port->at_data.termios, (void __user *)arg,
1015 				   sizeof(struct termios2)))
1016 			ret = -EFAULT;
1017 		break;
1018 #endif
1019 
1020 	case TIOCMGET:
1021 		ret = put_user(port->at_data.mdmbits, (int __user *)arg);
1022 		break;
1023 
1024 	case TIOCMSET:
1025 	case TIOCMBIC:
1026 	case TIOCMBIS: {
1027 		int mdmbits;
1028 
1029 		if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) {
1030 			ret = -EFAULT;
1031 			break;
1032 		}
1033 		if (cmd == TIOCMBIC)
1034 			port->at_data.mdmbits &= ~mdmbits;
1035 		else if (cmd == TIOCMBIS)
1036 			port->at_data.mdmbits |= mdmbits;
1037 		else
1038 			port->at_data.mdmbits = mdmbits;
1039 		break;
1040 	}
1041 
1042 	case TIOCEXCL:
1043 		set_bit(WWAN_PORT_EXCLUSIVE, &port->flags);
1044 		break;
1045 
1046 	case TIOCNXCL:
1047 		clear_bit(WWAN_PORT_EXCLUSIVE, &port->flags);
1048 		break;
1049 
1050 	case TIOCGEXCL:
1051 	{
1052 		int excl = test_bit(WWAN_PORT_EXCLUSIVE, &port->flags);
1053 
1054 		ret = put_user(excl, (int __user *)arg);
1055 		break;
1056 	}
1057 
1058 	default:
1059 		ret = -ENOIOCTLCMD;
1060 	}
1061 
1062 	mutex_unlock(&port->data_lock);
1063 
1064 	return ret;
1065 }
1066 
1067 static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd,
1068 				 unsigned long arg)
1069 {
1070 	struct wwan_port *port = filp->private_data;
1071 	int res;
1072 
1073 	if (port->type == WWAN_PORT_AT || port->type == WWAN_PORT_QCDM) {
1074 		/* AT and QCDM port specific IOCTLs */
1075 		res = wwan_port_fops_at_ioctl(port, cmd, arg);
1076 		if (res != -ENOIOCTLCMD)
1077 			return res;
1078 	}
1079 
1080 	switch (cmd) {
1081 	case TIOCINQ: {	/* aka SIOCINQ aka FIONREAD */
1082 		unsigned long flags;
1083 		struct sk_buff *skb;
1084 		int amount = 0;
1085 
1086 		spin_lock_irqsave(&port->rxq.lock, flags);
1087 		skb_queue_walk(&port->rxq, skb)
1088 			amount += skb->len;
1089 		spin_unlock_irqrestore(&port->rxq.lock, flags);
1090 
1091 		return put_user(amount, (int __user *)arg);
1092 	}
1093 
1094 	default:
1095 		return -ENOIOCTLCMD;
1096 	}
1097 }
1098 
1099 static const struct file_operations wwan_port_fops = {
1100 	.owner = THIS_MODULE,
1101 	.open = wwan_port_fops_open,
1102 	.release = wwan_port_fops_release,
1103 	.read = wwan_port_fops_read,
1104 	.write = wwan_port_fops_write,
1105 	.poll = wwan_port_fops_poll,
1106 	.unlocked_ioctl = wwan_port_fops_ioctl,
1107 #ifdef CONFIG_COMPAT
1108 	.compat_ioctl = compat_ptr_ioctl,
1109 #endif
1110 	.llseek = noop_llseek,
1111 };
1112 
1113 static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[],
1114 			      struct netlink_ext_ack *extack)
1115 {
1116 	if (!data)
1117 		return -EINVAL;
1118 
1119 	if (!tb[IFLA_PARENT_DEV_NAME])
1120 		return -EINVAL;
1121 
1122 	if (!data[IFLA_WWAN_LINK_ID])
1123 		return -EINVAL;
1124 
1125 	return 0;
1126 }
1127 
1128 static const struct device_type wwan_type = { .name = "wwan" };
1129 
1130 static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[],
1131 					  const char *ifname,
1132 					  unsigned char name_assign_type,
1133 					  unsigned int num_tx_queues,
1134 					  unsigned int num_rx_queues)
1135 {
1136 	const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]);
1137 	struct wwan_device *wwandev = wwan_dev_get_by_name(devname);
1138 	struct net_device *dev;
1139 	unsigned int priv_size;
1140 
1141 	if (IS_ERR(wwandev))
1142 		return ERR_CAST(wwandev);
1143 
1144 	/* only supported if ops were registered (not just ports) */
1145 	if (!wwandev->ops) {
1146 		dev = ERR_PTR(-EOPNOTSUPP);
1147 		goto out;
1148 	}
1149 
1150 	priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size;
1151 	dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type,
1152 			       wwandev->ops->setup, num_tx_queues, num_rx_queues);
1153 
1154 	if (dev) {
1155 		SET_NETDEV_DEV(dev, &wwandev->dev);
1156 		SET_NETDEV_DEVTYPE(dev, &wwan_type);
1157 	}
1158 
1159 out:
1160 	/* release the reference */
1161 	put_device(&wwandev->dev);
1162 	return dev;
1163 }
1164 
1165 static int wwan_rtnl_newlink(struct net_device *dev,
1166 			     struct rtnl_newlink_params *params,
1167 			     struct netlink_ext_ack *extack)
1168 {
1169 	struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
1170 	struct wwan_netdev_priv *priv = netdev_priv(dev);
1171 	struct nlattr **data = params->data;
1172 	u32 link_id;
1173 	int ret;
1174 
1175 	link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]);
1176 
1177 	if (IS_ERR(wwandev))
1178 		return PTR_ERR(wwandev);
1179 
1180 	/* shouldn't have a netdev (left) with us as parent so WARN */
1181 	if (WARN_ON(!wwandev->ops)) {
1182 		ret = -EOPNOTSUPP;
1183 		goto out;
1184 	}
1185 
1186 	priv->link_id = link_id;
1187 	if (wwandev->ops->newlink)
1188 		ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev,
1189 					    link_id, extack);
1190 	else
1191 		ret = register_netdevice(dev);
1192 
1193 out:
1194 	/* release the reference */
1195 	put_device(&wwandev->dev);
1196 	return ret;
1197 }
1198 
1199 static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head)
1200 {
1201 	struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent);
1202 
1203 	if (IS_ERR(wwandev))
1204 		return;
1205 
1206 	/* shouldn't have a netdev (left) with us as parent so WARN */
1207 	if (WARN_ON(!wwandev->ops))
1208 		goto out;
1209 
1210 	if (wwandev->ops->dellink)
1211 		wwandev->ops->dellink(wwandev->ops_ctxt, dev, head);
1212 	else
1213 		unregister_netdevice_queue(dev, head);
1214 
1215 out:
1216 	/* release the reference */
1217 	put_device(&wwandev->dev);
1218 }
1219 
1220 static size_t wwan_rtnl_get_size(const struct net_device *dev)
1221 {
1222 	return
1223 		nla_total_size(4) +	/* IFLA_WWAN_LINK_ID */
1224 		0;
1225 }
1226 
1227 static int wwan_rtnl_fill_info(struct sk_buff *skb,
1228 			       const struct net_device *dev)
1229 {
1230 	struct wwan_netdev_priv *priv = netdev_priv(dev);
1231 
1232 	if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id))
1233 		goto nla_put_failure;
1234 
1235 	return 0;
1236 
1237 nla_put_failure:
1238 	return -EMSGSIZE;
1239 }
1240 
1241 static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = {
1242 	[IFLA_WWAN_LINK_ID] = { .type = NLA_U32 },
1243 };
1244 
1245 static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = {
1246 	.kind = "wwan",
1247 	.maxtype = IFLA_WWAN_MAX,
1248 	.alloc = wwan_rtnl_alloc,
1249 	.validate = wwan_rtnl_validate,
1250 	.newlink = wwan_rtnl_newlink,
1251 	.dellink = wwan_rtnl_dellink,
1252 	.get_size = wwan_rtnl_get_size,
1253 	.fill_info = wwan_rtnl_fill_info,
1254 	.policy = wwan_rtnl_policy,
1255 };
1256 
1257 static void wwan_create_default_link(struct wwan_device *wwandev,
1258 				     u32 def_link_id)
1259 {
1260 	struct nlattr *tb[IFLA_MAX + 1], *linkinfo[IFLA_INFO_MAX + 1];
1261 	struct nlattr *data[IFLA_WWAN_MAX + 1];
1262 	struct rtnl_newlink_params params = {
1263 		.src_net = &init_net,
1264 		.tb = tb,
1265 		.data = data,
1266 	};
1267 	struct net_device *dev;
1268 	struct nlmsghdr *nlh;
1269 	struct sk_buff *msg;
1270 
1271 	/* Forge attributes required to create a WWAN netdev. We first
1272 	 * build a netlink message and then parse it. This looks
1273 	 * odd, but such approach is less error prone.
1274 	 */
1275 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1276 	if (WARN_ON(!msg))
1277 		return;
1278 	nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0);
1279 	if (WARN_ON(!nlh))
1280 		goto free_attrs;
1281 
1282 	if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev)))
1283 		goto free_attrs;
1284 	tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO);
1285 	if (!tb[IFLA_LINKINFO])
1286 		goto free_attrs;
1287 	linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA);
1288 	if (!linkinfo[IFLA_INFO_DATA])
1289 		goto free_attrs;
1290 	if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id))
1291 		goto free_attrs;
1292 	nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]);
1293 	nla_nest_end(msg, tb[IFLA_LINKINFO]);
1294 
1295 	nlmsg_end(msg, nlh);
1296 
1297 	/* The next three parsing calls can not fail */
1298 	nlmsg_parse_deprecated(nlh, 0, tb, IFLA_MAX, NULL, NULL);
1299 	nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO],
1300 				    NULL, NULL);
1301 	nla_parse_nested_deprecated(data, IFLA_WWAN_MAX,
1302 				    linkinfo[IFLA_INFO_DATA], NULL, NULL);
1303 
1304 	rtnl_lock();
1305 
1306 	dev = rtnl_create_link(&init_net, "wwan%d", NET_NAME_ENUM,
1307 			       &wwan_rtnl_link_ops, tb, NULL);
1308 	if (WARN_ON(IS_ERR(dev)))
1309 		goto unlock;
1310 
1311 	if (WARN_ON(wwan_rtnl_newlink(dev, &params, NULL))) {
1312 		free_netdev(dev);
1313 		goto unlock;
1314 	}
1315 
1316 	rtnl_configure_link(dev, NULL, 0, NULL); /* Link initialized, notify new link */
1317 
1318 unlock:
1319 	rtnl_unlock();
1320 
1321 free_attrs:
1322 	nlmsg_free(msg);
1323 }
1324 
1325 /**
1326  * wwan_register_ops - register WWAN device ops
1327  * @parent: Device to use as parent and shared by all WWAN ports and
1328  *	created netdevs
1329  * @ops: operations to register
1330  * @ctxt: context to pass to operations
1331  * @def_link_id: id of the default link that will be automatically created by
1332  *	the WWAN core for the WWAN device. The default link will not be created
1333  *	if the passed value is WWAN_NO_DEFAULT_LINK.
1334  *
1335  * Returns: 0 on success, a negative error code on failure
1336  */
1337 int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
1338 		      void *ctxt, u32 def_link_id)
1339 {
1340 	struct wwan_device *wwandev;
1341 
1342 	if (WARN_ON(!parent || !ops || !ops->setup))
1343 		return -EINVAL;
1344 
1345 	wwandev = wwan_create_dev(parent);
1346 	if (IS_ERR(wwandev))
1347 		return PTR_ERR(wwandev);
1348 
1349 	if (WARN_ON(wwandev->ops)) {
1350 		wwan_remove_dev(wwandev);
1351 		return -EBUSY;
1352 	}
1353 
1354 	wwandev->ops = ops;
1355 	wwandev->ops_ctxt = ctxt;
1356 
1357 	/* NB: we do not abort ops registration in case of default link
1358 	 * creation failure. Link ops is the management interface, while the
1359 	 * default link creation is a service option. And we should not prevent
1360 	 * a user from manually creating a link latter if service option failed
1361 	 * now.
1362 	 */
1363 	if (def_link_id != WWAN_NO_DEFAULT_LINK)
1364 		wwan_create_default_link(wwandev, def_link_id);
1365 
1366 	return 0;
1367 }
1368 EXPORT_SYMBOL_GPL(wwan_register_ops);
1369 
1370 /* Enqueue child netdev deletion */
1371 static int wwan_child_dellink(struct device *dev, void *data)
1372 {
1373 	struct list_head *kill_list = data;
1374 
1375 	if (dev->type == &wwan_type)
1376 		wwan_rtnl_dellink(to_net_dev(dev), kill_list);
1377 
1378 	return 0;
1379 }
1380 
1381 /**
1382  * wwan_unregister_ops - remove WWAN device ops
1383  * @parent: Device to use as parent and shared by all WWAN ports and
1384  *	created netdevs
1385  */
1386 void wwan_unregister_ops(struct device *parent)
1387 {
1388 	struct wwan_device *wwandev = wwan_dev_get_by_parent(parent);
1389 	LIST_HEAD(kill_list);
1390 
1391 	if (WARN_ON(IS_ERR(wwandev)))
1392 		return;
1393 	if (WARN_ON(!wwandev->ops)) {
1394 		put_device(&wwandev->dev);
1395 		return;
1396 	}
1397 
1398 	/* put the reference obtained by wwan_dev_get_by_parent(),
1399 	 * we should still have one (that the owner is giving back
1400 	 * now) due to the ops being assigned.
1401 	 */
1402 	put_device(&wwandev->dev);
1403 
1404 	rtnl_lock();	/* Prevent concurrent netdev(s) creation/destroying */
1405 
1406 	/* Remove all child netdev(s), using batch removing */
1407 	device_for_each_child(&wwandev->dev, &kill_list,
1408 			      wwan_child_dellink);
1409 	unregister_netdevice_many(&kill_list);
1410 
1411 	wwandev->ops = NULL;	/* Finally remove ops */
1412 
1413 	rtnl_unlock();
1414 
1415 	wwandev->ops_ctxt = NULL;
1416 	wwan_remove_dev(wwandev);
1417 }
1418 EXPORT_SYMBOL_GPL(wwan_unregister_ops);
1419 
1420 static int __init wwan_init(void)
1421 {
1422 	int err;
1423 
1424 	err = rtnl_link_register(&wwan_rtnl_link_ops);
1425 	if (err)
1426 		return err;
1427 
1428 	err = class_register(&wwan_class);
1429 	if (err)
1430 		goto unregister;
1431 
1432 	/* chrdev used for wwan ports */
1433 	wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port",
1434 				       &wwan_port_fops);
1435 	if (wwan_major < 0) {
1436 		err = wwan_major;
1437 		goto destroy;
1438 	}
1439 
1440 #ifdef CONFIG_WWAN_DEBUGFS
1441 	wwan_debugfs_dir = debugfs_create_dir("wwan", NULL);
1442 #endif
1443 
1444 	return 0;
1445 
1446 destroy:
1447 	class_unregister(&wwan_class);
1448 unregister:
1449 	rtnl_link_unregister(&wwan_rtnl_link_ops);
1450 	return err;
1451 }
1452 
1453 static void __exit wwan_exit(void)
1454 {
1455 	debugfs_remove_recursive(wwan_debugfs_dir);
1456 	__unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port");
1457 	rtnl_link_unregister(&wwan_rtnl_link_ops);
1458 	class_unregister(&wwan_class);
1459 }
1460 
1461 module_init(wwan_init);
1462 module_exit(wwan_exit);
1463 
1464 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
1465 MODULE_DESCRIPTION("WWAN core");
1466 MODULE_LICENSE("GPL v2");
1467