xref: /openbmc/linux/drivers/net/wwan/wwan_core.c (revision b923cda9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */
3 
4 #include <linux/err.h>
5 #include <linux/errno.h>
6 #include <linux/fs.h>
7 #include <linux/init.h>
8 #include <linux/idr.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/poll.h>
12 #include <linux/skbuff.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/wwan.h>
16 
17 #define WWAN_MAX_MINORS 256 /* 256 minors allowed with register_chrdev() */
18 
19 static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */
20 static DEFINE_IDA(minors); /* minors for WWAN port chardevs */
21 static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */
22 static struct class *wwan_class;
23 static int wwan_major;
24 
25 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev)
26 #define to_wwan_port(d) container_of(d, struct wwan_port, dev)
27 
28 /* WWAN port flags */
29 #define WWAN_PORT_TX_OFF	0
30 
31 /**
32  * struct wwan_device - The structure that defines a WWAN device
33  *
34  * @id: WWAN device unique ID.
35  * @dev: Underlying device.
36  * @port_id: Current available port ID to pick.
37  */
38 struct wwan_device {
39 	unsigned int id;
40 	struct device dev;
41 	atomic_t port_id;
42 };
43 
44 /**
45  * struct wwan_port - The structure that defines a WWAN port
46  * @type: Port type
47  * @start_count: Port start counter
48  * @flags: Store port state and capabilities
49  * @ops: Pointer to WWAN port operations
50  * @ops_lock: Protect port ops
51  * @dev: Underlying device
52  * @rxq: Buffer inbound queue
53  * @waitqueue: The waitqueue for port fops (read/write/poll)
54  */
55 struct wwan_port {
56 	enum wwan_port_type type;
57 	unsigned int start_count;
58 	unsigned long flags;
59 	const struct wwan_port_ops *ops;
60 	struct mutex ops_lock; /* Serialize ops + protect against removal */
61 	struct device dev;
62 	struct sk_buff_head rxq;
63 	wait_queue_head_t waitqueue;
64 };
65 
66 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf)
67 {
68 	struct wwan_device *wwan = to_wwan_dev(dev);
69 
70 	return sprintf(buf, "%d\n", wwan->id);
71 }
72 static DEVICE_ATTR_RO(index);
73 
74 static struct attribute *wwan_dev_attrs[] = {
75 	&dev_attr_index.attr,
76 	NULL,
77 };
78 ATTRIBUTE_GROUPS(wwan_dev);
79 
80 static void wwan_dev_destroy(struct device *dev)
81 {
82 	struct wwan_device *wwandev = to_wwan_dev(dev);
83 
84 	ida_free(&wwan_dev_ids, wwandev->id);
85 	kfree(wwandev);
86 }
87 
88 static const struct device_type wwan_dev_type = {
89 	.name    = "wwan_dev",
90 	.release = wwan_dev_destroy,
91 	.groups = wwan_dev_groups,
92 };
93 
94 static int wwan_dev_parent_match(struct device *dev, const void *parent)
95 {
96 	return (dev->type == &wwan_dev_type && dev->parent == parent);
97 }
98 
99 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent)
100 {
101 	struct device *dev;
102 
103 	dev = class_find_device(wwan_class, NULL, parent, wwan_dev_parent_match);
104 	if (!dev)
105 		return ERR_PTR(-ENODEV);
106 
107 	return to_wwan_dev(dev);
108 }
109 
110 /* This function allocates and registers a new WWAN device OR if a WWAN device
111  * already exist for the given parent, it gets a reference and return it.
112  * This function is not exported (for now), it is called indirectly via
113  * wwan_create_port().
114  */
115 static struct wwan_device *wwan_create_dev(struct device *parent)
116 {
117 	struct wwan_device *wwandev;
118 	int err, id;
119 
120 	/* The 'find-alloc-register' operation must be protected against
121 	 * concurrent execution, a WWAN device is possibly shared between
122 	 * multiple callers or concurrently unregistered from wwan_remove_dev().
123 	 */
124 	mutex_lock(&wwan_register_lock);
125 
126 	/* If wwandev already exists, return it */
127 	wwandev = wwan_dev_get_by_parent(parent);
128 	if (!IS_ERR(wwandev))
129 		goto done_unlock;
130 
131 	id = ida_alloc(&wwan_dev_ids, GFP_KERNEL);
132 	if (id < 0)
133 		goto done_unlock;
134 
135 	wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL);
136 	if (!wwandev) {
137 		ida_free(&wwan_dev_ids, id);
138 		goto done_unlock;
139 	}
140 
141 	wwandev->dev.parent = parent;
142 	wwandev->dev.class = wwan_class;
143 	wwandev->dev.type = &wwan_dev_type;
144 	wwandev->id = id;
145 	dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
146 
147 	err = device_register(&wwandev->dev);
148 	if (err) {
149 		put_device(&wwandev->dev);
150 		wwandev = NULL;
151 	}
152 
153 done_unlock:
154 	mutex_unlock(&wwan_register_lock);
155 
156 	return wwandev;
157 }
158 
159 static int is_wwan_child(struct device *dev, void *data)
160 {
161 	return dev->class == wwan_class;
162 }
163 
164 static void wwan_remove_dev(struct wwan_device *wwandev)
165 {
166 	int ret;
167 
168 	/* Prevent concurrent picking from wwan_create_dev */
169 	mutex_lock(&wwan_register_lock);
170 
171 	/* WWAN device is created and registered (get+add) along with its first
172 	 * child port, and subsequent port registrations only grab a reference
173 	 * (get). The WWAN device must then be unregistered (del+put) along with
174 	 * its latest port, and reference simply dropped (put) otherwise.
175 	 */
176 	ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child);
177 	if (!ret)
178 		device_unregister(&wwandev->dev);
179 	else
180 		put_device(&wwandev->dev);
181 
182 	mutex_unlock(&wwan_register_lock);
183 }
184 
185 /* ------- WWAN port management ------- */
186 
187 /* Keep aligned with wwan_port_type enum */
188 static const char * const wwan_port_type_str[] = {
189 	"AT",
190 	"MBIM",
191 	"QMI",
192 	"QCDM",
193 	"FIREHOSE"
194 };
195 
196 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
197 			 char *buf)
198 {
199 	struct wwan_port *port = to_wwan_port(dev);
200 
201 	return sprintf(buf, "%s\n", wwan_port_type_str[port->type]);
202 }
203 static DEVICE_ATTR_RO(type);
204 
205 static struct attribute *wwan_port_attrs[] = {
206 	&dev_attr_type.attr,
207 	NULL,
208 };
209 ATTRIBUTE_GROUPS(wwan_port);
210 
211 static void wwan_port_destroy(struct device *dev)
212 {
213 	struct wwan_port *port = to_wwan_port(dev);
214 
215 	ida_free(&minors, MINOR(port->dev.devt));
216 	skb_queue_purge(&port->rxq);
217 	mutex_destroy(&port->ops_lock);
218 	kfree(port);
219 }
220 
221 static const struct device_type wwan_port_dev_type = {
222 	.name = "wwan_port",
223 	.release = wwan_port_destroy,
224 	.groups = wwan_port_groups,
225 };
226 
227 static int wwan_port_minor_match(struct device *dev, const void *minor)
228 {
229 	return (dev->type == &wwan_port_dev_type &&
230 		MINOR(dev->devt) == *(unsigned int *)minor);
231 }
232 
233 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
234 {
235 	struct device *dev;
236 
237 	dev = class_find_device(wwan_class, NULL, &minor, wwan_port_minor_match);
238 	if (!dev)
239 		return ERR_PTR(-ENODEV);
240 
241 	return to_wwan_port(dev);
242 }
243 
244 struct wwan_port *wwan_create_port(struct device *parent,
245 				   enum wwan_port_type type,
246 				   const struct wwan_port_ops *ops,
247 				   void *drvdata)
248 {
249 	struct wwan_device *wwandev;
250 	struct wwan_port *port;
251 	int minor, err = -ENOMEM;
252 
253 	if (type >= WWAN_PORT_MAX || !ops)
254 		return ERR_PTR(-EINVAL);
255 
256 	/* A port is always a child of a WWAN device, retrieve (allocate or
257 	 * pick) the WWAN device based on the provided parent device.
258 	 */
259 	wwandev = wwan_create_dev(parent);
260 	if (IS_ERR(wwandev))
261 		return ERR_CAST(wwandev);
262 
263 	/* A port is exposed as character device, get a minor */
264 	minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
265 	if (minor < 0)
266 		goto error_wwandev_remove;
267 
268 	port = kzalloc(sizeof(*port), GFP_KERNEL);
269 	if (!port) {
270 		ida_free(&minors, minor);
271 		goto error_wwandev_remove;
272 	}
273 
274 	port->type = type;
275 	port->ops = ops;
276 	mutex_init(&port->ops_lock);
277 	skb_queue_head_init(&port->rxq);
278 	init_waitqueue_head(&port->waitqueue);
279 
280 	port->dev.parent = &wwandev->dev;
281 	port->dev.class = wwan_class;
282 	port->dev.type = &wwan_port_dev_type;
283 	port->dev.devt = MKDEV(wwan_major, minor);
284 	dev_set_drvdata(&port->dev, drvdata);
285 
286 	/* create unique name based on wwan device id, port index and type */
287 	dev_set_name(&port->dev, "wwan%up%u%s", wwandev->id,
288 		     atomic_inc_return(&wwandev->port_id),
289 		     wwan_port_type_str[port->type]);
290 
291 	err = device_register(&port->dev);
292 	if (err)
293 		goto error_put_device;
294 
295 	return port;
296 
297 error_put_device:
298 	put_device(&port->dev);
299 error_wwandev_remove:
300 	wwan_remove_dev(wwandev);
301 
302 	return ERR_PTR(err);
303 }
304 EXPORT_SYMBOL_GPL(wwan_create_port);
305 
306 void wwan_remove_port(struct wwan_port *port)
307 {
308 	struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
309 
310 	mutex_lock(&port->ops_lock);
311 	if (port->start_count)
312 		port->ops->stop(port);
313 	port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
314 	mutex_unlock(&port->ops_lock);
315 
316 	wake_up_interruptible(&port->waitqueue);
317 
318 	skb_queue_purge(&port->rxq);
319 	dev_set_drvdata(&port->dev, NULL);
320 	device_unregister(&port->dev);
321 
322 	/* Release related wwan device */
323 	wwan_remove_dev(wwandev);
324 }
325 EXPORT_SYMBOL_GPL(wwan_remove_port);
326 
327 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
328 {
329 	skb_queue_tail(&port->rxq, skb);
330 	wake_up_interruptible(&port->waitqueue);
331 }
332 EXPORT_SYMBOL_GPL(wwan_port_rx);
333 
334 void wwan_port_txon(struct wwan_port *port)
335 {
336 	clear_bit(WWAN_PORT_TX_OFF, &port->flags);
337 	wake_up_interruptible(&port->waitqueue);
338 }
339 EXPORT_SYMBOL_GPL(wwan_port_txon);
340 
341 void wwan_port_txoff(struct wwan_port *port)
342 {
343 	set_bit(WWAN_PORT_TX_OFF, &port->flags);
344 }
345 EXPORT_SYMBOL_GPL(wwan_port_txoff);
346 
347 void *wwan_port_get_drvdata(struct wwan_port *port)
348 {
349 	return dev_get_drvdata(&port->dev);
350 }
351 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
352 
353 static int wwan_port_op_start(struct wwan_port *port)
354 {
355 	int ret = 0;
356 
357 	mutex_lock(&port->ops_lock);
358 	if (!port->ops) { /* Port got unplugged */
359 		ret = -ENODEV;
360 		goto out_unlock;
361 	}
362 
363 	/* If port is already started, don't start again */
364 	if (!port->start_count)
365 		ret = port->ops->start(port);
366 
367 	if (!ret)
368 		port->start_count++;
369 
370 out_unlock:
371 	mutex_unlock(&port->ops_lock);
372 
373 	return ret;
374 }
375 
376 static void wwan_port_op_stop(struct wwan_port *port)
377 {
378 	mutex_lock(&port->ops_lock);
379 	port->start_count--;
380 	if (port->ops && !port->start_count)
381 		port->ops->stop(port);
382 	mutex_unlock(&port->ops_lock);
383 }
384 
385 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb)
386 {
387 	int ret;
388 
389 	mutex_lock(&port->ops_lock);
390 	if (!port->ops) { /* Port got unplugged */
391 		ret = -ENODEV;
392 		goto out_unlock;
393 	}
394 
395 	ret = port->ops->tx(port, skb);
396 
397 out_unlock:
398 	mutex_unlock(&port->ops_lock);
399 
400 	return ret;
401 }
402 
403 static bool is_read_blocked(struct wwan_port *port)
404 {
405 	return skb_queue_empty(&port->rxq) && port->ops;
406 }
407 
408 static bool is_write_blocked(struct wwan_port *port)
409 {
410 	return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
411 }
412 
413 static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
414 {
415 	if (!is_read_blocked(port))
416 		return 0;
417 
418 	if (nonblock)
419 		return -EAGAIN;
420 
421 	if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
422 		return -ERESTARTSYS;
423 
424 	return 0;
425 }
426 
427 static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
428 {
429 	if (!is_write_blocked(port))
430 		return 0;
431 
432 	if (nonblock)
433 		return -EAGAIN;
434 
435 	if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
436 		return -ERESTARTSYS;
437 
438 	return 0;
439 }
440 
441 static int wwan_port_fops_open(struct inode *inode, struct file *file)
442 {
443 	struct wwan_port *port;
444 	int err = 0;
445 
446 	port = wwan_port_get_by_minor(iminor(inode));
447 	if (IS_ERR(port))
448 		return PTR_ERR(port);
449 
450 	file->private_data = port;
451 	stream_open(inode, file);
452 
453 	err = wwan_port_op_start(port);
454 	if (err)
455 		put_device(&port->dev);
456 
457 	return err;
458 }
459 
460 static int wwan_port_fops_release(struct inode *inode, struct file *filp)
461 {
462 	struct wwan_port *port = filp->private_data;
463 
464 	wwan_port_op_stop(port);
465 	put_device(&port->dev);
466 
467 	return 0;
468 }
469 
470 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
471 				   size_t count, loff_t *ppos)
472 {
473 	struct wwan_port *port = filp->private_data;
474 	struct sk_buff *skb;
475 	size_t copied;
476 	int ret;
477 
478 	ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
479 	if (ret)
480 		return ret;
481 
482 	skb = skb_dequeue(&port->rxq);
483 	if (!skb)
484 		return -EIO;
485 
486 	copied = min_t(size_t, count, skb->len);
487 	if (copy_to_user(buf, skb->data, copied)) {
488 		kfree_skb(skb);
489 		return -EFAULT;
490 	}
491 	skb_pull(skb, copied);
492 
493 	/* skb is not fully consumed, keep it in the queue */
494 	if (skb->len)
495 		skb_queue_head(&port->rxq, skb);
496 	else
497 		consume_skb(skb);
498 
499 	return copied;
500 }
501 
502 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
503 				    size_t count, loff_t *offp)
504 {
505 	struct wwan_port *port = filp->private_data;
506 	struct sk_buff *skb;
507 	int ret;
508 
509 	ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
510 	if (ret)
511 		return ret;
512 
513 	skb = alloc_skb(count, GFP_KERNEL);
514 	if (!skb)
515 		return -ENOMEM;
516 
517 	if (copy_from_user(skb_put(skb, count), buf, count)) {
518 		kfree_skb(skb);
519 		return -EFAULT;
520 	}
521 
522 	ret = wwan_port_op_tx(port, skb);
523 	if (ret) {
524 		kfree_skb(skb);
525 		return ret;
526 	}
527 
528 	return count;
529 }
530 
531 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
532 {
533 	struct wwan_port *port = filp->private_data;
534 	__poll_t mask = 0;
535 
536 	poll_wait(filp, &port->waitqueue, wait);
537 
538 	if (!is_write_blocked(port))
539 		mask |= EPOLLOUT | EPOLLWRNORM;
540 	if (!is_read_blocked(port))
541 		mask |= EPOLLIN | EPOLLRDNORM;
542 	if (!port->ops)
543 		mask |= EPOLLHUP | EPOLLERR;
544 
545 	return mask;
546 }
547 
548 static const struct file_operations wwan_port_fops = {
549 	.owner = THIS_MODULE,
550 	.open = wwan_port_fops_open,
551 	.release = wwan_port_fops_release,
552 	.read = wwan_port_fops_read,
553 	.write = wwan_port_fops_write,
554 	.poll = wwan_port_fops_poll,
555 	.llseek = noop_llseek,
556 };
557 
558 static int __init wwan_init(void)
559 {
560 	wwan_class = class_create(THIS_MODULE, "wwan");
561 	if (IS_ERR(wwan_class))
562 		return PTR_ERR(wwan_class);
563 
564 	/* chrdev used for wwan ports */
565 	wwan_major = register_chrdev(0, "wwan_port", &wwan_port_fops);
566 	if (wwan_major < 0) {
567 		class_destroy(wwan_class);
568 		return wwan_major;
569 	}
570 
571 	return 0;
572 }
573 
574 static void __exit wwan_exit(void)
575 {
576 	unregister_chrdev(wwan_major, "wwan_port");
577 	class_destroy(wwan_class);
578 }
579 
580 module_init(wwan_init);
581 module_exit(wwan_exit);
582 
583 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
584 MODULE_DESCRIPTION("WWAN core");
585 MODULE_LICENSE("GPL v2");
586