Nishad Kamdar | 2e75973 | 2020-03-28 16:41:15 +0530 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 2 | /* |
| 3 | * u_ether.h -- interface to USB gadget "ethernet link" utilities |
| 4 | * |
| 5 | * Copyright (C) 2003-2005,2008 David Brownell |
| 6 | * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger |
| 7 | * Copyright (C) 2008 Nokia Corporation |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #ifndef __U_ETHER_H |
| 11 | #define __U_ETHER_H |
| 12 | |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/if_ether.h> |
| 15 | #include <linux/usb/composite.h> |
| 16 | #include <linux/usb/cdc.h> |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 17 | #include <linux/netdevice.h> |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 18 | |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 19 | #define QMULT_DEFAULT 5 |
| 20 | |
| 21 | /* |
| 22 | * dev_addr: initial value |
| 23 | * changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" |
| 24 | * host_addr: this address is invisible to ifconfig |
| 25 | */ |
| 26 | #define USB_ETHERNET_MODULE_PARAMETERS() \ |
| 27 | static unsigned qmult = QMULT_DEFAULT; \ |
| 28 | module_param(qmult, uint, S_IRUGO|S_IWUSR); \ |
| 29 | MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");\ |
| 30 | \ |
| 31 | static char *dev_addr; \ |
| 32 | module_param(dev_addr, charp, S_IRUGO); \ |
| 33 | MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); \ |
| 34 | \ |
| 35 | static char *host_addr; \ |
| 36 | module_param(host_addr, charp, S_IRUGO); \ |
| 37 | MODULE_PARM_DESC(host_addr, "Host Ethernet Address") |
| 38 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 39 | struct eth_dev; |
David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 40 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 41 | /* |
| 42 | * This represents the USB side of an "ethernet" link, managed by a USB |
| 43 | * function which provides control and (maybe) framing. Two functions |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 44 | * in different configurations could share the same ethernet link/netdev, |
| 45 | * using different host interaction models. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 46 | * |
David Brownell | 45fe3b8 | 2008-06-19 18:20:04 -0700 | [diff] [blame] | 47 | * There is a current limitation that only one instance of this link may |
| 48 | * be present in any given configuration. When that's a problem, network |
| 49 | * layer facilities can be used to package multiple logical links on this |
| 50 | * single "physical" one. |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 51 | */ |
| 52 | struct gether { |
| 53 | struct usb_function func; |
| 54 | |
| 55 | /* updated by gether_{connect,disconnect} */ |
| 56 | struct eth_dev *ioport; |
| 57 | |
| 58 | /* endpoints handle full and/or high speeds */ |
| 59 | struct usb_ep *in_ep; |
| 60 | struct usb_ep *out_ep; |
| 61 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 62 | bool is_zlp_ok; |
| 63 | |
| 64 | u16 cdc_filter; |
| 65 | |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 66 | /* hooks for added framing, as needed for RNDIS and EEM. */ |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 67 | u32 header_len; |
Yauheni Kaliuta | 5c1168d | 2010-12-08 13:12:04 +0200 | [diff] [blame] | 68 | /* NCM requires fixed size bundles */ |
| 69 | bool is_fixed; |
| 70 | u32 fixed_out_len; |
| 71 | u32 fixed_in_len; |
Jim Baxter | 6d3865f | 2014-07-07 18:33:18 +0100 | [diff] [blame] | 72 | bool supports_multi_frame; |
Brian Niebuhr | 9b39e9d | 2009-08-14 10:04:22 -0500 | [diff] [blame] | 73 | struct sk_buff *(*wrap)(struct gether *port, |
| 74 | struct sk_buff *skb); |
| 75 | int (*unwrap)(struct gether *port, |
| 76 | struct sk_buff *skb, |
| 77 | struct sk_buff_head *list); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 78 | |
| 79 | /* called on network open/close */ |
| 80 | void (*open)(struct gether *); |
| 81 | void (*close)(struct gether *); |
| 82 | }; |
| 83 | |
| 84 | #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ |
| 85 | |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ |
| 86 | |USB_CDC_PACKET_TYPE_PROMISCUOUS \ |
| 87 | |USB_CDC_PACKET_TYPE_DIRECTED) |
| 88 | |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 89 | /* variant of gether_setup that allows customizing network device name */ |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 90 | struct eth_dev *gether_setup_name(struct usb_gadget *g, |
| 91 | const char *dev_addr, const char *host_addr, |
| 92 | u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 93 | |
| 94 | /* netdev setup/teardown as directed by the gadget driver */ |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 95 | /* gether_setup - initialize one ethernet-over-usb link |
| 96 | * @g: gadget to associated with these links |
| 97 | * @ethaddr: NULL, or a buffer in which the ethernet address of the |
| 98 | * host side of the link is recorded |
| 99 | * Context: may sleep |
| 100 | * |
| 101 | * This sets up the single network link that may be exported by a |
| 102 | * gadget driver using this framework. The link layer addresses are |
| 103 | * set up using module parameters. |
| 104 | * |
Dan Carpenter | 574f24f | 2013-11-14 11:42:11 +0300 | [diff] [blame] | 105 | * Returns a eth_dev pointer on success, or an ERR_PTR on failure |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 106 | */ |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 107 | static inline struct eth_dev *gether_setup(struct usb_gadget *g, |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 108 | const char *dev_addr, const char *host_addr, |
| 109 | u8 ethaddr[ETH_ALEN], unsigned qmult) |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 110 | { |
Andrzej Pietrasiewicz | f1a1823 | 2013-05-23 09:22:03 +0200 | [diff] [blame] | 111 | return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb"); |
Mike Lockwood | 036e98b | 2012-05-10 10:08:02 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 114 | /* |
| 115 | * variant of gether_setup_default that allows customizing |
| 116 | * network device name |
| 117 | */ |
| 118 | struct net_device *gether_setup_name_default(const char *netname); |
| 119 | |
| 120 | /* |
| 121 | * gether_register_netdev - register the net device |
| 122 | * @net: net device to register |
| 123 | * |
| 124 | * Registers the net device associated with this ethernet-over-usb link |
| 125 | * |
| 126 | */ |
| 127 | int gether_register_netdev(struct net_device *net); |
| 128 | |
| 129 | /* gether_setup_default - initialize one ethernet-over-usb link |
| 130 | * Context: may sleep |
| 131 | * |
| 132 | * This sets up the single network link that may be exported by a |
| 133 | * gadget driver using this framework. The link layer addresses |
| 134 | * are set to random values. |
| 135 | * |
| 136 | * Returns negative errno, or zero on success |
| 137 | */ |
| 138 | static inline struct net_device *gether_setup_default(void) |
| 139 | { |
| 140 | return gether_setup_name_default("usb"); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * gether_set_gadget - initialize one ethernet-over-usb link with a gadget |
| 145 | * @net: device representing this link |
| 146 | * @g: the gadget to initialize with |
| 147 | * |
| 148 | * This associates one ethernet-over-usb link with a gadget. |
| 149 | */ |
| 150 | void gether_set_gadget(struct net_device *net, struct usb_gadget *g); |
| 151 | |
| 152 | /** |
| 153 | * gether_set_dev_addr - initialize an ethernet-over-usb link with eth address |
| 154 | * @net: device representing this link |
| 155 | * @dev_addr: eth address of this device |
| 156 | * |
| 157 | * This sets the device-side Ethernet address of this ethernet-over-usb link |
| 158 | * if dev_addr is correct. |
| 159 | * Returns negative errno if the new address is incorrect. |
| 160 | */ |
| 161 | int gether_set_dev_addr(struct net_device *net, const char *dev_addr); |
| 162 | |
| 163 | /** |
| 164 | * gether_get_dev_addr - get an ethernet-over-usb link eth address |
| 165 | * @net: device representing this link |
| 166 | * @dev_addr: place to store device's eth address |
| 167 | * @len: length of the @dev_addr buffer |
| 168 | * |
| 169 | * This gets the device-side Ethernet address of this ethernet-over-usb link. |
| 170 | * Returns zero on success, else negative errno. |
| 171 | */ |
| 172 | int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len); |
| 173 | |
| 174 | /** |
| 175 | * gether_set_host_addr - initialize an ethernet-over-usb link with host address |
| 176 | * @net: device representing this link |
| 177 | * @host_addr: eth address of the host |
| 178 | * |
| 179 | * This sets the host-side Ethernet address of this ethernet-over-usb link |
| 180 | * if host_addr is correct. |
| 181 | * Returns negative errno if the new address is incorrect. |
| 182 | */ |
| 183 | int gether_set_host_addr(struct net_device *net, const char *host_addr); |
| 184 | |
| 185 | /** |
| 186 | * gether_get_host_addr - get an ethernet-over-usb link host address |
| 187 | * @net: device representing this link |
| 188 | * @host_addr: place to store eth address of the host |
| 189 | * @len: length of the @host_addr buffer |
| 190 | * |
| 191 | * This gets the host-side Ethernet address of this ethernet-over-usb link. |
| 192 | * Returns zero on success, else negative errno. |
| 193 | */ |
| 194 | int gether_get_host_addr(struct net_device *net, char *host_addr, int len); |
| 195 | |
| 196 | /** |
| 197 | * gether_get_host_addr_cdc - get an ethernet-over-usb link host address |
| 198 | * @net: device representing this link |
| 199 | * @host_addr: place to store eth address of the host |
| 200 | * @len: length of the @host_addr buffer |
| 201 | * |
| 202 | * This gets the CDC formatted host-side Ethernet address of this |
| 203 | * ethernet-over-usb link. |
| 204 | * Returns zero on success, else negative errno. |
| 205 | */ |
| 206 | int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len); |
| 207 | |
| 208 | /** |
Andrzej Pietrasiewicz | bf4277c | 2013-05-28 09:15:45 +0200 | [diff] [blame] | 209 | * gether_get_host_addr_u8 - get an ethernet-over-usb link host address |
| 210 | * @net: device representing this link |
| 211 | * @host_mac: place to store the eth address of the host |
| 212 | * |
| 213 | * This gets the binary formatted host-side Ethernet address of this |
| 214 | * ethernet-over-usb link. |
| 215 | */ |
| 216 | void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]); |
| 217 | |
| 218 | /** |
Andrzej Pietrasiewicz | bcd4a1c | 2013-05-23 09:22:05 +0200 | [diff] [blame] | 219 | * gether_set_qmult - initialize an ethernet-over-usb link with a multiplier |
| 220 | * @net: device representing this link |
| 221 | * @qmult: queue multiplier |
| 222 | * |
| 223 | * This sets the queue length multiplier of this ethernet-over-usb link. |
| 224 | * For higher speeds use longer queues. |
| 225 | */ |
| 226 | void gether_set_qmult(struct net_device *net, unsigned qmult); |
| 227 | |
| 228 | /** |
| 229 | * gether_get_qmult - get an ethernet-over-usb link multiplier |
| 230 | * @net: device representing this link |
| 231 | * |
| 232 | * This gets the queue length multiplier of this ethernet-over-usb link. |
| 233 | */ |
| 234 | unsigned gether_get_qmult(struct net_device *net); |
| 235 | |
| 236 | /** |
| 237 | * gether_get_ifname - get an ethernet-over-usb link interface name |
| 238 | * @net: device representing this link |
| 239 | * @name: place to store the interface name |
| 240 | * @len: length of the @name buffer |
| 241 | * |
| 242 | * This gets the interface name of this ethernet-over-usb link. |
| 243 | * Returns zero on success, else negative errno. |
| 244 | */ |
| 245 | int gether_get_ifname(struct net_device *net, char *name, int len); |
| 246 | |
Lorenzo Colitti | 63d1521 | 2021-01-14 08:42:22 +0900 | [diff] [blame] | 247 | /** |
| 248 | * gether_set_ifname - set an ethernet-over-usb link interface name |
| 249 | * @net: device representing this link |
| 250 | * @name: new interface name |
| 251 | * @len: length of @name |
| 252 | * |
| 253 | * This sets the interface name of this ethernet-over-usb link. |
| 254 | * A single terminating newline, if any, is ignored. |
| 255 | * Returns zero on success, else negative errno. |
| 256 | */ |
| 257 | int gether_set_ifname(struct net_device *net, const char *name, int len); |
| 258 | |
Sebastian Andrzej Siewior | d6a0143 | 2012-12-23 21:10:12 +0100 | [diff] [blame] | 259 | void gether_cleanup(struct eth_dev *dev); |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 260 | |
| 261 | /* connect/disconnect is handled by individual functions */ |
| 262 | struct net_device *gether_connect(struct gether *); |
| 263 | void gether_disconnect(struct gether *); |
| 264 | |
David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 265 | /* Some controllers can't support CDC Ethernet (ECM) ... */ |
| 266 | static inline bool can_support_ecm(struct usb_gadget *gadget) |
| 267 | { |
Robert Baldyga | 736d093 | 2015-07-28 07:20:03 +0200 | [diff] [blame] | 268 | if (!gadget_is_altset_supported(gadget)) |
David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 269 | return false; |
| 270 | |
David Brownell | da741b8 | 2008-06-19 18:19:46 -0700 | [diff] [blame] | 271 | /* Everything else is *presumably* fine ... but this is a bit |
| 272 | * chancy, so be **CERTAIN** there are no hardware issues with |
| 273 | * your controller. Add it above if it can't handle CDC. |
| 274 | */ |
| 275 | return true; |
| 276 | } |
| 277 | |
David Brownell | 2b3d942 | 2008-06-19 18:19:28 -0700 | [diff] [blame] | 278 | #endif /* __U_ETHER_H */ |