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