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