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