xref: /openbmc/linux/drivers/net/wwan/wwan_core.c (revision 64cc80c0)
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 static const char * const wwan_port_type_str[WWAN_PORT_MAX + 1] = {
188 	[WWAN_PORT_AT] = "AT",
189 	[WWAN_PORT_MBIM] = "MBIM",
190 	[WWAN_PORT_QMI] = "QMI",
191 	[WWAN_PORT_QCDM] = "QCDM",
192 	[WWAN_PORT_FIREHOSE] = "FIREHOSE",
193 };
194 
195 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
196 			 char *buf)
197 {
198 	struct wwan_port *port = to_wwan_port(dev);
199 
200 	return sprintf(buf, "%s\n", wwan_port_type_str[port->type]);
201 }
202 static DEVICE_ATTR_RO(type);
203 
204 static struct attribute *wwan_port_attrs[] = {
205 	&dev_attr_type.attr,
206 	NULL,
207 };
208 ATTRIBUTE_GROUPS(wwan_port);
209 
210 static void wwan_port_destroy(struct device *dev)
211 {
212 	struct wwan_port *port = to_wwan_port(dev);
213 
214 	ida_free(&minors, MINOR(port->dev.devt));
215 	skb_queue_purge(&port->rxq);
216 	mutex_destroy(&port->ops_lock);
217 	kfree(port);
218 }
219 
220 static const struct device_type wwan_port_dev_type = {
221 	.name = "wwan_port",
222 	.release = wwan_port_destroy,
223 	.groups = wwan_port_groups,
224 };
225 
226 static int wwan_port_minor_match(struct device *dev, const void *minor)
227 {
228 	return (dev->type == &wwan_port_dev_type &&
229 		MINOR(dev->devt) == *(unsigned int *)minor);
230 }
231 
232 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor)
233 {
234 	struct device *dev;
235 
236 	dev = class_find_device(wwan_class, NULL, &minor, wwan_port_minor_match);
237 	if (!dev)
238 		return ERR_PTR(-ENODEV);
239 
240 	return to_wwan_port(dev);
241 }
242 
243 struct wwan_port *wwan_create_port(struct device *parent,
244 				   enum wwan_port_type type,
245 				   const struct wwan_port_ops *ops,
246 				   void *drvdata)
247 {
248 	struct wwan_device *wwandev;
249 	struct wwan_port *port;
250 	int minor, err = -ENOMEM;
251 
252 	if (type > WWAN_PORT_MAX || !ops)
253 		return ERR_PTR(-EINVAL);
254 
255 	/* A port is always a child of a WWAN device, retrieve (allocate or
256 	 * pick) the WWAN device based on the provided parent device.
257 	 */
258 	wwandev = wwan_create_dev(parent);
259 	if (IS_ERR(wwandev))
260 		return ERR_CAST(wwandev);
261 
262 	/* A port is exposed as character device, get a minor */
263 	minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL);
264 	if (minor < 0)
265 		goto error_wwandev_remove;
266 
267 	port = kzalloc(sizeof(*port), GFP_KERNEL);
268 	if (!port) {
269 		ida_free(&minors, minor);
270 		goto error_wwandev_remove;
271 	}
272 
273 	port->type = type;
274 	port->ops = ops;
275 	mutex_init(&port->ops_lock);
276 	skb_queue_head_init(&port->rxq);
277 	init_waitqueue_head(&port->waitqueue);
278 
279 	port->dev.parent = &wwandev->dev;
280 	port->dev.class = wwan_class;
281 	port->dev.type = &wwan_port_dev_type;
282 	port->dev.devt = MKDEV(wwan_major, minor);
283 	dev_set_drvdata(&port->dev, drvdata);
284 
285 	/* create unique name based on wwan device id, port index and type */
286 	dev_set_name(&port->dev, "wwan%up%u%s", wwandev->id,
287 		     atomic_inc_return(&wwandev->port_id),
288 		     wwan_port_type_str[port->type]);
289 
290 	err = device_register(&port->dev);
291 	if (err)
292 		goto error_put_device;
293 
294 	return port;
295 
296 error_put_device:
297 	put_device(&port->dev);
298 error_wwandev_remove:
299 	wwan_remove_dev(wwandev);
300 
301 	return ERR_PTR(err);
302 }
303 EXPORT_SYMBOL_GPL(wwan_create_port);
304 
305 void wwan_remove_port(struct wwan_port *port)
306 {
307 	struct wwan_device *wwandev = to_wwan_dev(port->dev.parent);
308 
309 	mutex_lock(&port->ops_lock);
310 	if (port->start_count)
311 		port->ops->stop(port);
312 	port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */
313 	mutex_unlock(&port->ops_lock);
314 
315 	wake_up_interruptible(&port->waitqueue);
316 
317 	skb_queue_purge(&port->rxq);
318 	dev_set_drvdata(&port->dev, NULL);
319 	device_unregister(&port->dev);
320 
321 	/* Release related wwan device */
322 	wwan_remove_dev(wwandev);
323 }
324 EXPORT_SYMBOL_GPL(wwan_remove_port);
325 
326 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb)
327 {
328 	skb_queue_tail(&port->rxq, skb);
329 	wake_up_interruptible(&port->waitqueue);
330 }
331 EXPORT_SYMBOL_GPL(wwan_port_rx);
332 
333 void wwan_port_txon(struct wwan_port *port)
334 {
335 	clear_bit(WWAN_PORT_TX_OFF, &port->flags);
336 	wake_up_interruptible(&port->waitqueue);
337 }
338 EXPORT_SYMBOL_GPL(wwan_port_txon);
339 
340 void wwan_port_txoff(struct wwan_port *port)
341 {
342 	set_bit(WWAN_PORT_TX_OFF, &port->flags);
343 }
344 EXPORT_SYMBOL_GPL(wwan_port_txoff);
345 
346 void *wwan_port_get_drvdata(struct wwan_port *port)
347 {
348 	return dev_get_drvdata(&port->dev);
349 }
350 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata);
351 
352 static int wwan_port_op_start(struct wwan_port *port)
353 {
354 	int ret = 0;
355 
356 	mutex_lock(&port->ops_lock);
357 	if (!port->ops) { /* Port got unplugged */
358 		ret = -ENODEV;
359 		goto out_unlock;
360 	}
361 
362 	/* If port is already started, don't start again */
363 	if (!port->start_count)
364 		ret = port->ops->start(port);
365 
366 	if (!ret)
367 		port->start_count++;
368 
369 out_unlock:
370 	mutex_unlock(&port->ops_lock);
371 
372 	return ret;
373 }
374 
375 static void wwan_port_op_stop(struct wwan_port *port)
376 {
377 	mutex_lock(&port->ops_lock);
378 	port->start_count--;
379 	if (port->ops && !port->start_count)
380 		port->ops->stop(port);
381 	mutex_unlock(&port->ops_lock);
382 }
383 
384 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb)
385 {
386 	int ret;
387 
388 	mutex_lock(&port->ops_lock);
389 	if (!port->ops) { /* Port got unplugged */
390 		ret = -ENODEV;
391 		goto out_unlock;
392 	}
393 
394 	ret = port->ops->tx(port, skb);
395 
396 out_unlock:
397 	mutex_unlock(&port->ops_lock);
398 
399 	return ret;
400 }
401 
402 static bool is_read_blocked(struct wwan_port *port)
403 {
404 	return skb_queue_empty(&port->rxq) && port->ops;
405 }
406 
407 static bool is_write_blocked(struct wwan_port *port)
408 {
409 	return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops;
410 }
411 
412 static int wwan_wait_rx(struct wwan_port *port, bool nonblock)
413 {
414 	if (!is_read_blocked(port))
415 		return 0;
416 
417 	if (nonblock)
418 		return -EAGAIN;
419 
420 	if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port)))
421 		return -ERESTARTSYS;
422 
423 	return 0;
424 }
425 
426 static int wwan_wait_tx(struct wwan_port *port, bool nonblock)
427 {
428 	if (!is_write_blocked(port))
429 		return 0;
430 
431 	if (nonblock)
432 		return -EAGAIN;
433 
434 	if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port)))
435 		return -ERESTARTSYS;
436 
437 	return 0;
438 }
439 
440 static int wwan_port_fops_open(struct inode *inode, struct file *file)
441 {
442 	struct wwan_port *port;
443 	int err = 0;
444 
445 	port = wwan_port_get_by_minor(iminor(inode));
446 	if (IS_ERR(port))
447 		return PTR_ERR(port);
448 
449 	file->private_data = port;
450 	stream_open(inode, file);
451 
452 	err = wwan_port_op_start(port);
453 	if (err)
454 		put_device(&port->dev);
455 
456 	return err;
457 }
458 
459 static int wwan_port_fops_release(struct inode *inode, struct file *filp)
460 {
461 	struct wwan_port *port = filp->private_data;
462 
463 	wwan_port_op_stop(port);
464 	put_device(&port->dev);
465 
466 	return 0;
467 }
468 
469 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf,
470 				   size_t count, loff_t *ppos)
471 {
472 	struct wwan_port *port = filp->private_data;
473 	struct sk_buff *skb;
474 	size_t copied;
475 	int ret;
476 
477 	ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK));
478 	if (ret)
479 		return ret;
480 
481 	skb = skb_dequeue(&port->rxq);
482 	if (!skb)
483 		return -EIO;
484 
485 	copied = min_t(size_t, count, skb->len);
486 	if (copy_to_user(buf, skb->data, copied)) {
487 		kfree_skb(skb);
488 		return -EFAULT;
489 	}
490 	skb_pull(skb, copied);
491 
492 	/* skb is not fully consumed, keep it in the queue */
493 	if (skb->len)
494 		skb_queue_head(&port->rxq, skb);
495 	else
496 		consume_skb(skb);
497 
498 	return copied;
499 }
500 
501 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf,
502 				    size_t count, loff_t *offp)
503 {
504 	struct wwan_port *port = filp->private_data;
505 	struct sk_buff *skb;
506 	int ret;
507 
508 	ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK));
509 	if (ret)
510 		return ret;
511 
512 	skb = alloc_skb(count, GFP_KERNEL);
513 	if (!skb)
514 		return -ENOMEM;
515 
516 	if (copy_from_user(skb_put(skb, count), buf, count)) {
517 		kfree_skb(skb);
518 		return -EFAULT;
519 	}
520 
521 	ret = wwan_port_op_tx(port, skb);
522 	if (ret) {
523 		kfree_skb(skb);
524 		return ret;
525 	}
526 
527 	return count;
528 }
529 
530 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait)
531 {
532 	struct wwan_port *port = filp->private_data;
533 	__poll_t mask = 0;
534 
535 	poll_wait(filp, &port->waitqueue, wait);
536 
537 	if (!is_write_blocked(port))
538 		mask |= EPOLLOUT | EPOLLWRNORM;
539 	if (!is_read_blocked(port))
540 		mask |= EPOLLIN | EPOLLRDNORM;
541 	if (!port->ops)
542 		mask |= EPOLLHUP | EPOLLERR;
543 
544 	return mask;
545 }
546 
547 static const struct file_operations wwan_port_fops = {
548 	.owner = THIS_MODULE,
549 	.open = wwan_port_fops_open,
550 	.release = wwan_port_fops_release,
551 	.read = wwan_port_fops_read,
552 	.write = wwan_port_fops_write,
553 	.poll = wwan_port_fops_poll,
554 	.llseek = noop_llseek,
555 };
556 
557 static int __init wwan_init(void)
558 {
559 	wwan_class = class_create(THIS_MODULE, "wwan");
560 	if (IS_ERR(wwan_class))
561 		return PTR_ERR(wwan_class);
562 
563 	/* chrdev used for wwan ports */
564 	wwan_major = register_chrdev(0, "wwan_port", &wwan_port_fops);
565 	if (wwan_major < 0) {
566 		class_destroy(wwan_class);
567 		return wwan_major;
568 	}
569 
570 	return 0;
571 }
572 
573 static void __exit wwan_exit(void)
574 {
575 	unregister_chrdev(wwan_major, "wwan_port");
576 	class_destroy(wwan_class);
577 }
578 
579 module_init(wwan_init);
580 module_exit(wwan_exit);
581 
582 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
583 MODULE_DESCRIPTION("WWAN core");
584 MODULE_LICENSE("GPL v2");
585