1/* SPDX-License-Identifier: GPL-2.0-only */
2/* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */
3
4#ifndef __WWAN_H
5#define __WWAN_H
6
7#include <linux/poll.h>
8#include <linux/netdevice.h>
9#include <linux/types.h>
10
11/**
12 * enum wwan_port_type - WWAN port types
13 * @WWAN_PORT_AT: AT commands
14 * @WWAN_PORT_MBIM: Mobile Broadband Interface Model control
15 * @WWAN_PORT_QMI: Qcom modem/MSM interface for modem control
16 * @WWAN_PORT_QCDM: Qcom Modem diagnostic interface
17 * @WWAN_PORT_FIREHOSE: XML based command protocol
18 * @WWAN_PORT_XMMRPC: Control protocol for Intel XMM modems
19 * @WWAN_PORT_FASTBOOT: Fastboot protocol control
20 *
21 * @WWAN_PORT_MAX: Highest supported port types
22 * @WWAN_PORT_UNKNOWN: Special value to indicate an unknown port type
23 * @__WWAN_PORT_MAX: Internal use
24 */
25enum wwan_port_type {
26	WWAN_PORT_AT,
27	WWAN_PORT_MBIM,
28	WWAN_PORT_QMI,
29	WWAN_PORT_QCDM,
30	WWAN_PORT_FIREHOSE,
31	WWAN_PORT_XMMRPC,
32	WWAN_PORT_FASTBOOT,
33
34	/* Add new port types above this line */
35
36	__WWAN_PORT_MAX,
37	WWAN_PORT_MAX = __WWAN_PORT_MAX - 1,
38	WWAN_PORT_UNKNOWN,
39};
40
41struct device;
42struct file;
43struct netlink_ext_ack;
44struct sk_buff;
45struct wwan_port;
46
47/** struct wwan_port_ops - The WWAN port operations
48 * @start: The routine for starting the WWAN port device.
49 * @stop: The routine for stopping the WWAN port device.
50 * @tx: Non-blocking routine that sends WWAN port protocol data to the device.
51 * @tx_blocking: Optional blocking routine that sends WWAN port protocol data
52 *               to the device.
53 * @tx_poll: Optional routine that sets additional TX poll flags.
54 *
55 * The wwan_port_ops structure contains a list of low-level operations
56 * that control a WWAN port device. All functions are mandatory unless specified.
57 */
58struct wwan_port_ops {
59	int (*start)(struct wwan_port *port);
60	void (*stop)(struct wwan_port *port);
61	int (*tx)(struct wwan_port *port, struct sk_buff *skb);
62
63	/* Optional operations */
64	int (*tx_blocking)(struct wwan_port *port, struct sk_buff *skb);
65	__poll_t (*tx_poll)(struct wwan_port *port, struct file *filp,
66			    poll_table *wait);
67};
68
69/** struct wwan_port_caps - The WWAN port capbilities
70 * @frag_len: WWAN port TX fragments length
71 * @headroom_len: WWAN port TX fragments reserved headroom length
72 */
73struct wwan_port_caps {
74	size_t frag_len;
75	unsigned int headroom_len;
76};
77
78/**
79 * wwan_create_port - Add a new WWAN port
80 * @parent: Device to use as parent and shared by all WWAN ports
81 * @type: WWAN port type
82 * @ops: WWAN port operations
83 * @caps: WWAN port capabilities
84 * @drvdata: Pointer to caller driver data
85 *
86 * Allocate and register a new WWAN port. The port will be automatically exposed
87 * to user as a character device and attached to the right virtual WWAN device,
88 * based on the parent pointer. The parent pointer is the device shared by all
89 * components of a same WWAN modem (e.g. USB dev, PCI dev, MHI controller...).
90 *
91 * drvdata will be placed in the WWAN port device driver data and can be
92 * retrieved with wwan_port_get_drvdata().
93 *
94 * This function must be balanced with a call to wwan_remove_port().
95 *
96 * Returns a valid pointer to wwan_port on success or PTR_ERR on failure
97 */
98struct wwan_port *wwan_create_port(struct device *parent,
99				   enum wwan_port_type type,
100				   const struct wwan_port_ops *ops,
101				   struct wwan_port_caps *caps,
102				   void *drvdata);
103
104/**
105 * wwan_remove_port - Remove a WWAN port
106 * @port: WWAN port to remove
107 *
108 * Remove a previously created port.
109 */
110void wwan_remove_port(struct wwan_port *port);
111
112/**
113 * wwan_port_rx - Receive data from the WWAN port
114 * @port: WWAN port for which data is received
115 * @skb: Pointer to the rx buffer
116 *
117 * A port driver calls this function upon data reception (MBIM, AT...).
118 */
119void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb);
120
121/**
122 * wwan_port_txoff - Stop TX on WWAN port
123 * @port: WWAN port for which TX must be stopped
124 *
125 * Used for TX flow control, a port driver calls this function to indicate TX
126 * is temporary unavailable (e.g. due to ring buffer fullness).
127 */
128void wwan_port_txoff(struct wwan_port *port);
129
130
131/**
132 * wwan_port_txon - Restart TX on WWAN port
133 * @port: WWAN port for which TX must be restarted
134 *
135 * Used for TX flow control, a port driver calls this function to indicate TX
136 * is available again.
137 */
138void wwan_port_txon(struct wwan_port *port);
139
140/**
141 * wwan_port_get_drvdata - Retrieve driver data from a WWAN port
142 * @port: Related WWAN port
143 */
144void *wwan_port_get_drvdata(struct wwan_port *port);
145
146/**
147 * struct wwan_netdev_priv - WWAN core network device private data
148 * @link_id: WWAN device data link id
149 * @drv_priv: driver private data area, size is determined in &wwan_ops
150 */
151struct wwan_netdev_priv {
152	u32 link_id;
153
154	/* must be last */
155	u8 drv_priv[] __aligned(sizeof(void *));
156};
157
158static inline void *wwan_netdev_drvpriv(struct net_device *dev)
159{
160	return ((struct wwan_netdev_priv *)netdev_priv(dev))->drv_priv;
161}
162
163/*
164 * Used to indicate that the WWAN core should not create a default network
165 * link.
166 */
167#define WWAN_NO_DEFAULT_LINK		U32_MAX
168
169/**
170 * struct wwan_ops - WWAN device ops
171 * @priv_size: size of private netdev data area
172 * @setup: set up a new netdev
173 * @newlink: register the new netdev
174 * @dellink: remove the given netdev
175 */
176struct wwan_ops {
177	unsigned int priv_size;
178	void (*setup)(struct net_device *dev);
179	int (*newlink)(void *ctxt, struct net_device *dev,
180		       u32 if_id, struct netlink_ext_ack *extack);
181	void (*dellink)(void *ctxt, struct net_device *dev,
182			struct list_head *head);
183};
184
185int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
186		      void *ctxt, u32 def_link_id);
187
188void wwan_unregister_ops(struct device *parent);
189
190#ifdef CONFIG_WWAN_DEBUGFS
191struct dentry *wwan_get_debugfs_dir(struct device *parent);
192void wwan_put_debugfs_dir(struct dentry *dir);
193#else
194static inline struct dentry *wwan_get_debugfs_dir(struct device *parent)
195{
196	return ERR_PTR(-ENODEV);
197}
198static inline void wwan_put_debugfs_dir(struct dentry *dir) {}
199#endif
200
201#endif /* __WWAN_H */
202