Skip to content
Permalink
Browse files
net: Add a WWAN subsystem
This change introduces initial support for a WWAN subsystem. Given the
complexity and heterogeneity of existing WWAN hardwares and interfaces,
there is no strict definition of what a WWAN device is and how it should
be represented. It's often a collection of multiple components/devices
that perform the global WWAN feature (netdev, tty, chardev, etc).

One usual way to expose modem controls and configuration is via high
level protocols such as the well known AT command protocol, MBIM or
QMI. The USB modems started to expose that as character devices, and
user daemons such as ModemManager learnt how to deal with that. This
initial version adds the concept of WWAN port, which can be registered
by any driver to expose one of these protocols. The WWAN core takes
care of the generic part, including character device creation and lets
the driver implementing access (fops) to the selected protocol.

Since the different components/devices do no necesserarly know about
each others, and can be created/removed in different orders, the
WWAN core ensures that devices being part of the same hardware are
also represented as a unique WWAN device, relying on the provided
parent device (e.g. mhi controller, USB device). It's a 'trick' I
copied from Johannes's earlier WWAN subsystem proposal.

This initial version is purposely minimalist, it's essentially moving
the generic part of the previously proposed mhi_wwan_ctrl driver inside
a common WWAN framework, but the implementation is open and flexible
enough to allow extension for further drivers.

Signed-off-by: Loic Poulain <loic.poulain@linaro.org>
  • Loading branch information
Loic Poulain authored and intel-lab-lkp committed Mar 11, 2021
1 parent 34bb975 commit 259571a34d6f035f99a8343c5fcd12b6dfc92c40
Show file tree
Hide file tree
Showing 8 changed files with 457 additions and 0 deletions.
@@ -502,6 +502,8 @@ source "drivers/net/wan/Kconfig"

source "drivers/net/ieee802154/Kconfig"

source "drivers/net/wwan/Kconfig"

config XEN_NETDEV_FRONTEND
tristate "Xen network device frontend driver"
depends on XEN
@@ -68,6 +68,7 @@ obj-$(CONFIG_SUNGEM_PHY) += sungem_phy.o
obj-$(CONFIG_WAN) += wan/
obj-$(CONFIG_WLAN) += wireless/
obj-$(CONFIG_IEEE802154) += ieee802154/
obj-$(CONFIG_WWAN) += wwan/

obj-$(CONFIG_VMXNET3) += vmxnet3/
obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: GPL-2.0-only
#
# Wireless WAN device configuration
#

menuconfig WWAN
bool "Wireless WAN"
help
This section contains Wireless WAN driver configurations.

if WWAN

config WWAN_CORE
tristate "WWAN Driver Core"
help
Say Y here if you want to use the WWAN driver core. This driver
provides a common framework for WWAN drivers.

endif # WWAN
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: GPL-2.0
#
# Makefile for the Linux WWAN device drivers.
#

obj-$(CONFIG_WWAN_CORE) += wwan.o
wwan-objs += wwan_core.o wwan_port.o

@@ -0,0 +1,150 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */

#include <linux/err.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/idr.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/wwan.h>

#include "wwan_core.h"

static LIST_HEAD(wwan_list); /* list of registered wwan devices */
static DEFINE_IDA(wwan_ida);
static DEFINE_MUTEX(wwan_global_lock);
struct class *wwan_class;

struct wwan_device *__wwan_find_by_parent(struct device *parent)
{
struct wwan_device *wwandev;

if (!parent)
return NULL;

list_for_each_entry(wwandev, &wwan_list, list) {
if (wwandev->dev.parent == parent)
return wwandev;
}

return NULL;
}

static void wwan_dev_release(struct device *dev)
{
struct wwan_device *wwandev = to_wwan_dev(dev);

kfree(wwandev);
}

static const struct device_type wwan_type = {
.name = "wwan",
.release = wwan_dev_release,
};

struct wwan_device *wwan_create_dev(struct device *parent)
{
struct wwan_device *wwandev;
int err, id;

mutex_lock(&wwan_global_lock);

wwandev = __wwan_find_by_parent(parent);
if (wwandev) {
get_device(&wwandev->dev);
wwandev->usage++;
goto done_unlock;
}

id = ida_alloc(&wwan_ida, GFP_KERNEL);
if (id < 0)
goto done_unlock;

wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL);
if (!wwandev) {
ida_free(&wwan_ida, id);
goto done_unlock;
}

wwandev->dev.parent = parent;
wwandev->dev.class = wwan_class;
wwandev->dev.type = &wwan_type;
wwandev->id = id;
dev_set_name(&wwandev->dev, "wwan%d", wwandev->id);
wwandev->usage = 1;
INIT_LIST_HEAD(&wwandev->ports);

err = device_register(&wwandev->dev);
if (err) {
put_device(&wwandev->dev);
ida_free(&wwan_ida, id);
wwandev = NULL;
goto done_unlock;
}

list_add_tail(&wwandev->list, &wwan_list);

done_unlock:
mutex_unlock(&wwan_global_lock);

return wwandev;
}
EXPORT_SYMBOL_GPL(wwan_create_dev);

void wwan_destroy_dev(struct wwan_device *wwandev)
{
mutex_lock(&wwan_global_lock);
wwandev->usage--;

if (wwandev->usage)
goto done_unlock;

/* Someone destroyed the wwan device without removing ports */
WARN_ON(!list_empty(&wwandev->ports));

list_del(&wwandev->list);
device_unregister(&wwandev->dev);
ida_free(&wwan_ida, wwandev->id);
put_device(&wwandev->dev);

done_unlock:
mutex_unlock(&wwan_global_lock);
}
EXPORT_SYMBOL_GPL(wwan_destroy_dev);

static int __init wwan_init(void)
{
int err;

wwan_class = class_create(THIS_MODULE, "wwan");
if (IS_ERR(wwan_class))
return PTR_ERR(wwan_class);

err = wwan_port_init();
if (err)
goto err_class_destroy;

return 0;

err_class_destroy:
class_destroy(wwan_class);
return err;
}

static void __exit wwan_exit(void)
{
wwan_port_deinit();
class_destroy(wwan_class);
}

//subsys_initcall(wwan_init);
module_init(wwan_init);
module_exit(wwan_exit);

MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>");
MODULE_DESCRIPTION("WWAN core");
MODULE_LICENSE("GPL v2");
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */

#ifndef __WWAN_CORE_H
#define __WWAN_CORE_H

#include <linux/device.h>
#include <linux/wwan.h>

#define to_wwan_dev(d) container_of(d, struct wwan_device, dev)

struct wwan_device *wwan_create_dev(struct device *parent);
void wwan_destroy_dev(struct wwan_device *wwandev);

int wwan_port_init(void);
void wwan_port_deinit(void);

extern struct class *wwan_class;

#endif /* WWAN_CORE_H */
@@ -0,0 +1,136 @@
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */

#include <linux/err.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/idr.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/wwan.h>

#include "wwan_core.h"

#define WWAN_MAX_MINORS 128

static int wwan_major;
static DEFINE_IDR(wwan_port_idr);
static DEFINE_MUTEX(wwan_port_idr_lock);

static const char * const wwan_port_type_str[] = {
"AT",
"MBIM",
"QMI",
"QCDM",
"FIREHOSE"
};

int wwan_add_port(struct wwan_port *port)
{
struct wwan_device *wwandev = port->wwandev;
struct device *dev;
int minor, err;

if (port->type >= WWAN_PORT_MAX || !port->fops || !wwandev)
return -EINVAL;

mutex_lock(&wwan_port_idr_lock);
minor = idr_alloc(&wwan_port_idr, port, 0, WWAN_MAX_MINORS, GFP_KERNEL);
mutex_unlock(&wwan_port_idr_lock);

if (minor < 0)
return minor;

mutex_lock(&wwandev->lock);

dev = device_create(wwan_class, &wwandev->dev,
MKDEV(wwan_major, minor), port,
"wwan%dp%u%s", wwandev->id, wwandev->port_idx,
wwan_port_type_str[port->type]);
if (IS_ERR(dev)) {
err = PTR_ERR(dev);
mutex_unlock(&wwandev->lock);
goto error_free_idr;
}

port->id = wwandev->port_idx++;
port->minor = minor;

list_add(&port->list, &wwandev->ports);

mutex_unlock(&port->wwandev->lock);

return 0;

error_free_idr:
mutex_lock(&wwan_port_idr_lock);
idr_remove(&wwan_port_idr, minor);
mutex_unlock(&wwan_port_idr_lock);

return err;
}
EXPORT_SYMBOL_GPL(wwan_add_port);

void wwan_remove_port(struct wwan_port *port)
{
struct wwan_device *wwandev = port->wwandev;

WARN_ON(!wwandev);

mutex_lock(&wwandev->lock);
device_destroy(wwan_class, MKDEV(wwan_major, port->minor));
list_del(&port->list);
mutex_unlock(&wwandev->lock);

mutex_lock(&wwan_port_idr_lock);
idr_remove(&wwan_port_idr, port->minor);
mutex_unlock(&wwan_port_idr_lock);
}
EXPORT_SYMBOL_GPL(wwan_remove_port);

static int wwan_port_open(struct inode *inode, struct file *file)
{
const struct file_operations *new_fops;
unsigned int minor = iminor(inode);
struct wwan_port *port;
int err = 0;

mutex_lock(&wwan_port_idr_lock);
port = idr_find(&wwan_port_idr, minor);
if (!port) {
mutex_unlock(&wwan_port_idr_lock);
return -ENODEV;
}
mutex_unlock(&wwan_port_idr_lock);

file->private_data = port->private_data ? port->private_data : port;
stream_open(inode, file);

new_fops = fops_get(port->fops);
replace_fops(file, new_fops);
if (file->f_op->open)
err = file->f_op->open(inode, file);

return err;
}

static const struct file_operations wwan_port_fops = {
.owner = THIS_MODULE,
.open = wwan_port_open,
.llseek = noop_llseek,
};

int wwan_port_init(void)
{
wwan_major = register_chrdev(0, "wwanport", &wwan_port_fops);
if (wwan_major < 0)
return wwan_major;

return 0;
}

void wwan_port_deinit(void)
{
unregister_chrdev(wwan_major, "wwanport");
idr_destroy(&wwan_port_idr);
}

0 comments on commit 259571a

Please sign in to comment.