blob: 5ce2acf444fb3bd65b7bc3fc94389895745b165a [file] [log] [blame]
Loic Poulain9a44c1cc2021-04-16 10:36:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* Copyright (c) 2021, Linaro Ltd <[email protected]> */
3
4#ifndef __WWAN_H
5#define __WWAN_H
6
Stephan Gerhold31c143f2021-06-18 19:36:11 +02007#include <linux/poll.h>
Sergey Ryazanov69940922021-06-22 01:51:00 +03008#include <linux/netdevice.h>
Andy Shevchenko30be4552021-12-22 18:32:56 +02009#include <linux/types.h>
Loic Poulain9a44c1cc2021-04-16 10:36:33 +020010
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
Sergey Ryazanovb64d76b2021-06-08 07:02:34 +030018 *
19 * @WWAN_PORT_MAX: Highest supported port types
20 * @WWAN_PORT_UNKNOWN: Special value to indicate an unknown port type
21 * @__WWAN_PORT_MAX: Internal use
Loic Poulain9a44c1cc2021-04-16 10:36:33 +020022 */
23enum wwan_port_type {
24 WWAN_PORT_AT,
25 WWAN_PORT_MBIM,
26 WWAN_PORT_QMI,
27 WWAN_PORT_QCDM,
28 WWAN_PORT_FIREHOSE,
Sergey Ryazanovb64d76b2021-06-08 07:02:34 +030029
30 /* Add new port types above this line */
31
32 __WWAN_PORT_MAX,
33 WWAN_PORT_MAX = __WWAN_PORT_MAX - 1,
Loic Poulainbf303962021-05-11 16:42:22 +020034 WWAN_PORT_UNKNOWN,
Loic Poulain9a44c1cc2021-04-16 10:36:33 +020035};
36
Andy Shevchenko30be4552021-12-22 18:32:56 +020037struct device;
38struct file;
39struct netlink_ext_ack;
40struct sk_buff;
Loic Poulain9a44c1cc2021-04-16 10:36:33 +020041struct wwan_port;
42
43/** struct wwan_port_ops - The WWAN port operations
44 * @start: The routine for starting the WWAN port device.
45 * @stop: The routine for stopping the WWAN port device.
Stephan Gerhold31c143f2021-06-18 19:36:11 +020046 * @tx: Non-blocking routine that sends WWAN port protocol data to the device.
47 * @tx_blocking: Optional blocking routine that sends WWAN port protocol data
48 * to the device.
49 * @tx_poll: Optional routine that sets additional TX poll flags.
Loic Poulain9a44c1cc2021-04-16 10:36:33 +020050 *
51 * The wwan_port_ops structure contains a list of low-level operations
Stephan Gerhold31c143f2021-06-18 19:36:11 +020052 * that control a WWAN port device. All functions are mandatory unless specified.
Loic Poulain9a44c1cc2021-04-16 10:36:33 +020053 */
54struct wwan_port_ops {
55 int (*start)(struct wwan_port *port);
56 void (*stop)(struct wwan_port *port);
57 int (*tx)(struct wwan_port *port, struct sk_buff *skb);
Stephan Gerhold31c143f2021-06-18 19:36:11 +020058
59 /* Optional operations */
60 int (*tx_blocking)(struct wwan_port *port, struct sk_buff *skb);
61 __poll_t (*tx_poll)(struct wwan_port *port, struct file *filp,
62 poll_table *wait);
Loic Poulain9a44c1cc2021-04-16 10:36:33 +020063};
64
65/**
66 * wwan_create_port - Add a new WWAN port
67 * @parent: Device to use as parent and shared by all WWAN ports
68 * @type: WWAN port type
69 * @ops: WWAN port operations
70 * @drvdata: Pointer to caller driver data
71 *
72 * Allocate and register a new WWAN port. The port will be automatically exposed
73 * to user as a character device and attached to the right virtual WWAN device,
74 * based on the parent pointer. The parent pointer is the device shared by all
75 * components of a same WWAN modem (e.g. USB dev, PCI dev, MHI controller...).
76 *
77 * drvdata will be placed in the WWAN port device driver data and can be
78 * retrieved with wwan_port_get_drvdata().
79 *
80 * This function must be balanced with a call to wwan_remove_port().
81 *
82 * Returns a valid pointer to wwan_port on success or PTR_ERR on failure
83 */
84struct wwan_port *wwan_create_port(struct device *parent,
85 enum wwan_port_type type,
86 const struct wwan_port_ops *ops,
87 void *drvdata);
88
89/**
90 * wwan_remove_port - Remove a WWAN port
91 * @port: WWAN port to remove
92 *
93 * Remove a previously created port.
94 */
95void wwan_remove_port(struct wwan_port *port);
96
97/**
98 * wwan_port_rx - Receive data from the WWAN port
99 * @port: WWAN port for which data is received
100 * @skb: Pointer to the rx buffer
101 *
102 * A port driver calls this function upon data reception (MBIM, AT...).
103 */
104void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb);
105
106/**
107 * wwan_port_txoff - Stop TX on WWAN port
108 * @port: WWAN port for which TX must be stopped
109 *
110 * Used for TX flow control, a port driver calls this function to indicate TX
111 * is temporary unavailable (e.g. due to ring buffer fullness).
112 */
113void wwan_port_txoff(struct wwan_port *port);
114
115
116/**
117 * wwan_port_txon - Restart TX on WWAN port
118 * @port: WWAN port for which TX must be restarted
119 *
120 * Used for TX flow control, a port driver calls this function to indicate TX
121 * is available again.
122 */
123void wwan_port_txon(struct wwan_port *port);
124
125/**
126 * wwan_port_get_drvdata - Retrieve driver data from a WWAN port
127 * @port: Related WWAN port
128 */
129void *wwan_port_get_drvdata(struct wwan_port *port);
130
Sergey Ryazanov69940922021-06-22 01:51:00 +0300131/**
132 * struct wwan_netdev_priv - WWAN core network device private data
133 * @link_id: WWAN device data link id
134 * @drv_priv: driver private data area, size is determined in &wwan_ops
135 */
136struct wwan_netdev_priv {
137 u32 link_id;
138
139 /* must be last */
140 u8 drv_priv[] __aligned(sizeof(void *));
141};
142
143static inline void *wwan_netdev_drvpriv(struct net_device *dev)
144{
145 return ((struct wwan_netdev_priv *)netdev_priv(dev))->drv_priv;
146}
147
Sergey Ryazanovca374292021-06-22 01:50:58 +0300148/*
149 * Used to indicate that the WWAN core should not create a default network
150 * link.
151 */
152#define WWAN_NO_DEFAULT_LINK U32_MAX
153
Johannes Berg88b71052021-06-12 10:20:56 +0200154/**
155 * struct wwan_ops - WWAN device ops
Johannes Berg88b71052021-06-12 10:20:56 +0200156 * @priv_size: size of private netdev data area
157 * @setup: set up a new netdev
158 * @newlink: register the new netdev
159 * @dellink: remove the given netdev
160 */
161struct wwan_ops {
Johannes Berg88b71052021-06-12 10:20:56 +0200162 unsigned int priv_size;
163 void (*setup)(struct net_device *dev);
164 int (*newlink)(void *ctxt, struct net_device *dev,
165 u32 if_id, struct netlink_ext_ack *extack);
166 void (*dellink)(void *ctxt, struct net_device *dev,
167 struct list_head *head);
168};
169
170int wwan_register_ops(struct device *parent, const struct wwan_ops *ops,
Sergey Ryazanovca374292021-06-22 01:50:58 +0300171 void *ctxt, u32 def_link_id);
Johannes Berg88b71052021-06-12 10:20:56 +0200172
173void wwan_unregister_ops(struct device *parent);
174
Sergey Ryazanov283e6f5a2021-12-07 12:21:40 +0300175#ifdef CONFIG_WWAN_DEBUGFS
M Chetan Kumarc4804672021-11-20 21:51:54 +0530176struct dentry *wwan_get_debugfs_dir(struct device *parent);
M Chetan Kumar76f05d82022-02-14 12:46:52 +0530177void wwan_put_debugfs_dir(struct dentry *dir);
Sergey Ryazanov283e6f5a2021-12-07 12:21:40 +0300178#else
179static inline struct dentry *wwan_get_debugfs_dir(struct device *parent)
180{
181 return ERR_PTR(-ENODEV);
182}
M Chetan Kumar76f05d82022-02-14 12:46:52 +0530183static inline void wwan_put_debugfs_dir(struct dentry *dir) {}
Sergey Ryazanov283e6f5a2021-12-07 12:21:40 +0300184#endif
M Chetan Kumarc4804672021-11-20 21:51:54 +0530185
Loic Poulain9a44c1cc2021-04-16 10:36:33 +0200186#endif /* __WWAN_H */