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