1 /* SPDX-License-Identifier: GPL-2.0+ */ 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 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> 17 #include <linux/netdevice.h> 18 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 39 struct eth_dev; 40 41 /** 42 * struct gether_opts - Options for Ethernet gadget function instances 43 * @name: Pattern for the network interface name (e.g., "usb%d"). 44 * Used to generate the net device name. 45 * @qmult: Queue length multiplier for high/super speed. 46 * @host_mac: The MAC address to be used by the host side. 47 * @dev_mac: The MAC address to be used by the device side. 48 * @ifname_set: True if the interface name pattern has been set by userspace. 49 * @addr_assign_type: The method used for assigning the device MAC address 50 * (e.g., NET_ADDR_RANDOM, NET_ADDR_SET). 51 * 52 * This structure caches network-related settings provided through configfs 53 * before the net_device is fully instantiated. This allows for early 54 * configuration while deferring net_device allocation until the function 55 * is bound. 56 */ 57 struct gether_opts { 58 char name[IFNAMSIZ]; 59 unsigned int qmult; 60 u8 host_mac[ETH_ALEN]; 61 u8 dev_mac[ETH_ALEN]; 62 bool ifname_set; 63 unsigned char addr_assign_type; 64 }; 65 66 /* 67 * This represents the USB side of an "ethernet" link, managed by a USB 68 * function which provides control and (maybe) framing. Two functions 69 * in different configurations could share the same ethernet link/netdev, 70 * using different host interaction models. 71 * 72 * There is a current limitation that only one instance of this link may 73 * be present in any given configuration. When that's a problem, network 74 * layer facilities can be used to package multiple logical links on this 75 * single "physical" one. 76 */ 77 struct gether { 78 struct usb_function func; 79 80 /* updated by gether_{connect,disconnect} */ 81 struct eth_dev *ioport; 82 83 /* endpoints handle full and/or high speeds */ 84 struct usb_ep *in_ep; 85 struct usb_ep *out_ep; 86 87 bool is_zlp_ok; 88 89 u16 cdc_filter; 90 91 /* hooks for added framing, as needed for RNDIS and EEM. */ 92 u32 header_len; 93 /* NCM requires fixed size bundles */ 94 bool is_fixed; 95 u32 fixed_out_len; 96 u32 fixed_in_len; 97 bool supports_multi_frame; 98 struct sk_buff *(*wrap)(struct gether *port, 99 struct sk_buff *skb); 100 int (*unwrap)(struct gether *port, 101 struct sk_buff *skb, 102 struct sk_buff_head *list); 103 104 /* called on network open/close */ 105 void (*open)(struct gether *); 106 void (*close)(struct gether *); 107 bool is_suspend; 108 }; 109 110 #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \ 111 |USB_CDC_PACKET_TYPE_ALL_MULTICAST \ 112 |USB_CDC_PACKET_TYPE_PROMISCUOUS \ 113 |USB_CDC_PACKET_TYPE_DIRECTED) 114 115 /* variant of gether_setup that allows customizing network device name */ 116 struct eth_dev *gether_setup_name(struct usb_gadget *g, 117 const char *dev_addr, const char *host_addr, 118 u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname); 119 120 /* netdev setup/teardown as directed by the gadget driver */ 121 /* gether_setup - initialize one ethernet-over-usb link 122 * @g: gadget to associated with these links 123 * @ethaddr: NULL, or a buffer in which the ethernet address of the 124 * host side of the link is recorded 125 * Context: may sleep 126 * 127 * This sets up the single network link that may be exported by a 128 * gadget driver using this framework. The link layer addresses are 129 * set up using module parameters. 130 * 131 * Returns a eth_dev pointer on success, or an ERR_PTR on failure 132 */ 133 static inline struct eth_dev *gether_setup(struct usb_gadget *g, 134 const char *dev_addr, const char *host_addr, 135 u8 ethaddr[ETH_ALEN], unsigned qmult) 136 { 137 return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb"); 138 } 139 140 /* 141 * variant of gether_setup_default that allows customizing 142 * network device name 143 */ 144 struct net_device *gether_setup_name_default(const char *netname); 145 146 /* 147 * gether_register_netdev - register the net device 148 * @net: net device to register 149 * 150 * Registers the net device associated with this ethernet-over-usb link 151 * 152 */ 153 int gether_register_netdev(struct net_device *net); 154 155 /* gether_setup_default - initialize one ethernet-over-usb link 156 * Context: may sleep 157 * 158 * This sets up the single network link that may be exported by a 159 * gadget driver using this framework. The link layer addresses 160 * are set to random values. 161 * 162 * Returns negative errno, or zero on success 163 */ 164 static inline struct net_device *gether_setup_default(void) 165 { 166 return gether_setup_name_default("usb"); 167 } 168 169 /** 170 * gether_set_gadget - initialize one ethernet-over-usb link with a gadget 171 * @net: device representing this link 172 * @g: the gadget to initialize with 173 * 174 * This associates one ethernet-over-usb link with a gadget. 175 */ 176 void gether_set_gadget(struct net_device *net, struct usb_gadget *g); 177 178 /** 179 * gether_set_dev_addr - initialize an ethernet-over-usb link with eth address 180 * @net: device representing this link 181 * @dev_addr: eth address of this device 182 * 183 * This sets the device-side Ethernet address of this ethernet-over-usb link 184 * if dev_addr is correct. 185 * Returns negative errno if the new address is incorrect. 186 */ 187 int gether_set_dev_addr(struct net_device *net, const char *dev_addr); 188 189 /** 190 * gether_get_dev_addr - get an ethernet-over-usb link eth address 191 * @net: device representing this link 192 * @dev_addr: place to store device's eth address 193 * @len: length of the @dev_addr buffer 194 * 195 * This gets the device-side Ethernet address of this ethernet-over-usb link. 196 * Returns zero on success, else negative errno. 197 */ 198 int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len); 199 200 /** 201 * gether_set_host_addr - initialize an ethernet-over-usb link with host address 202 * @net: device representing this link 203 * @host_addr: eth address of the host 204 * 205 * This sets the host-side Ethernet address of this ethernet-over-usb link 206 * if host_addr is correct. 207 * Returns negative errno if the new address is incorrect. 208 */ 209 int gether_set_host_addr(struct net_device *net, const char *host_addr); 210 211 /** 212 * gether_get_host_addr - get an ethernet-over-usb link host address 213 * @net: device representing this link 214 * @host_addr: place to store eth address of the host 215 * @len: length of the @host_addr buffer 216 * 217 * This gets the host-side Ethernet address of this ethernet-over-usb link. 218 * Returns zero on success, else negative errno. 219 */ 220 int gether_get_host_addr(struct net_device *net, char *host_addr, int len); 221 222 /** 223 * gether_get_host_addr_cdc - get an ethernet-over-usb link host address 224 * @net: device representing this link 225 * @host_addr: place to store eth address of the host 226 * @len: length of the @host_addr buffer 227 * 228 * This gets the CDC formatted host-side Ethernet address of this 229 * ethernet-over-usb link. 230 * Returns zero on success, else negative errno. 231 */ 232 int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len); 233 234 /** 235 * gether_get_host_addr_u8 - get an ethernet-over-usb link host address 236 * @net: device representing this link 237 * @host_mac: place to store the eth address of the host 238 * 239 * This gets the binary formatted host-side Ethernet address of this 240 * ethernet-over-usb link. 241 */ 242 void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]); 243 244 /** 245 * gether_set_qmult - initialize an ethernet-over-usb link with a multiplier 246 * @net: device representing this link 247 * @qmult: queue multiplier 248 * 249 * This sets the queue length multiplier of this ethernet-over-usb link. 250 * For higher speeds use longer queues. 251 */ 252 void gether_set_qmult(struct net_device *net, unsigned qmult); 253 254 /** 255 * gether_get_qmult - get an ethernet-over-usb link multiplier 256 * @net: device representing this link 257 * 258 * This gets the queue length multiplier of this ethernet-over-usb link. 259 */ 260 unsigned gether_get_qmult(struct net_device *net); 261 262 /** 263 * gether_get_ifname - get an ethernet-over-usb link interface name 264 * @net: device representing this link 265 * @name: place to store the interface name 266 * @len: length of the @name buffer 267 * 268 * This gets the interface name of this ethernet-over-usb link. 269 * Returns zero on success, else negative errno. 270 */ 271 int gether_get_ifname(struct net_device *net, char *name, int len); 272 273 /** 274 * gether_set_ifname - set an ethernet-over-usb link interface name 275 * @net: device representing this link 276 * @name: new interface name 277 * @len: length of @name 278 * 279 * This sets the interface name of this ethernet-over-usb link. 280 * A single terminating newline, if any, is ignored. 281 * Returns zero on success, else negative errno. 282 */ 283 int gether_set_ifname(struct net_device *net, const char *name, int len); 284 285 void gether_cleanup(struct eth_dev *dev); 286 void gether_unregister_free_netdev(struct net_device *net); 287 DEFINE_FREE(free_gether_netdev, struct net_device *, gether_unregister_free_netdev(_T)); 288 289 void gether_setup_opts_default(struct gether_opts *opts, const char *name); 290 void gether_apply_opts(struct net_device *net, struct gether_opts *opts); 291 292 void gether_suspend(struct gether *link); 293 void gether_resume(struct gether *link); 294 295 /* connect/disconnect is handled by individual functions */ 296 struct net_device *gether_connect(struct gether *); 297 void gether_disconnect(struct gether *); 298 299 /* Some controllers can't support CDC Ethernet (ECM) ... */ 300 static inline bool can_support_ecm(struct usb_gadget *gadget) 301 { 302 if (!gadget_is_altset_supported(gadget)) 303 return false; 304 305 /* Everything else is *presumably* fine ... but this is a bit 306 * chancy, so be **CERTAIN** there are no hardware issues with 307 * your controller. Add it above if it can't handle CDC. 308 */ 309 return true; 310 } 311 312 /* peak (theoretical) bulk transfer rate in bits-per-second */ 313 static inline unsigned int gether_bitrate(struct usb_gadget *g) 314 { 315 if (g->speed >= USB_SPEED_SUPER_PLUS) 316 return 4250000000U; 317 if (g->speed == USB_SPEED_SUPER) 318 return 3750000000U; 319 else if (g->speed == USB_SPEED_HIGH) 320 return 13 * 512 * 8 * 1000 * 8; 321 else 322 return 19 * 64 * 1 * 1000 * 8; 323 } 324 325 #endif /* __U_ETHER_H */ 326