12e759738SNishad Kamdar /* SPDX-License-Identifier: GPL-2.0+ */
200a2430fSAndrzej Pietrasiewicz /*
300a2430fSAndrzej Pietrasiewicz * u_ether.h -- interface to USB gadget "ethernet link" utilities
400a2430fSAndrzej Pietrasiewicz *
500a2430fSAndrzej Pietrasiewicz * Copyright (C) 2003-2005,2008 David Brownell
600a2430fSAndrzej Pietrasiewicz * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
700a2430fSAndrzej Pietrasiewicz * Copyright (C) 2008 Nokia Corporation
800a2430fSAndrzej Pietrasiewicz */
900a2430fSAndrzej Pietrasiewicz
1000a2430fSAndrzej Pietrasiewicz #ifndef __U_ETHER_H
1100a2430fSAndrzej Pietrasiewicz #define __U_ETHER_H
1200a2430fSAndrzej Pietrasiewicz
1300a2430fSAndrzej Pietrasiewicz #include <linux/err.h>
1400a2430fSAndrzej Pietrasiewicz #include <linux/if_ether.h>
1500a2430fSAndrzej Pietrasiewicz #include <linux/usb/composite.h>
1600a2430fSAndrzej Pietrasiewicz #include <linux/usb/cdc.h>
1700a2430fSAndrzej Pietrasiewicz #include <linux/netdevice.h>
1800a2430fSAndrzej Pietrasiewicz
1900a2430fSAndrzej Pietrasiewicz #define QMULT_DEFAULT 5
2000a2430fSAndrzej Pietrasiewicz
2100a2430fSAndrzej Pietrasiewicz /*
2200a2430fSAndrzej Pietrasiewicz * dev_addr: initial value
2300a2430fSAndrzej Pietrasiewicz * changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx"
2400a2430fSAndrzej Pietrasiewicz * host_addr: this address is invisible to ifconfig
2500a2430fSAndrzej Pietrasiewicz */
2600a2430fSAndrzej Pietrasiewicz #define USB_ETHERNET_MODULE_PARAMETERS() \
2700a2430fSAndrzej Pietrasiewicz static unsigned qmult = QMULT_DEFAULT; \
2800a2430fSAndrzej Pietrasiewicz module_param(qmult, uint, S_IRUGO|S_IWUSR); \
2900a2430fSAndrzej Pietrasiewicz MODULE_PARM_DESC(qmult, "queue length multiplier at high/super speed");\
3000a2430fSAndrzej Pietrasiewicz \
3100a2430fSAndrzej Pietrasiewicz static char *dev_addr; \
3200a2430fSAndrzej Pietrasiewicz module_param(dev_addr, charp, S_IRUGO); \
3300a2430fSAndrzej Pietrasiewicz MODULE_PARM_DESC(dev_addr, "Device Ethernet Address"); \
3400a2430fSAndrzej Pietrasiewicz \
3500a2430fSAndrzej Pietrasiewicz static char *host_addr; \
3600a2430fSAndrzej Pietrasiewicz module_param(host_addr, charp, S_IRUGO); \
3700a2430fSAndrzej Pietrasiewicz MODULE_PARM_DESC(host_addr, "Host Ethernet Address")
3800a2430fSAndrzej Pietrasiewicz
3900a2430fSAndrzej Pietrasiewicz struct eth_dev;
4000a2430fSAndrzej Pietrasiewicz
4100a2430fSAndrzej Pietrasiewicz /*
4200a2430fSAndrzej Pietrasiewicz * This represents the USB side of an "ethernet" link, managed by a USB
4300a2430fSAndrzej Pietrasiewicz * function which provides control and (maybe) framing. Two functions
4400a2430fSAndrzej Pietrasiewicz * in different configurations could share the same ethernet link/netdev,
4500a2430fSAndrzej Pietrasiewicz * using different host interaction models.
4600a2430fSAndrzej Pietrasiewicz *
4700a2430fSAndrzej Pietrasiewicz * There is a current limitation that only one instance of this link may
4800a2430fSAndrzej Pietrasiewicz * be present in any given configuration. When that's a problem, network
4900a2430fSAndrzej Pietrasiewicz * layer facilities can be used to package multiple logical links on this
5000a2430fSAndrzej Pietrasiewicz * single "physical" one.
5100a2430fSAndrzej Pietrasiewicz */
5200a2430fSAndrzej Pietrasiewicz struct gether {
5300a2430fSAndrzej Pietrasiewicz struct usb_function func;
5400a2430fSAndrzej Pietrasiewicz
5500a2430fSAndrzej Pietrasiewicz /* updated by gether_{connect,disconnect} */
5600a2430fSAndrzej Pietrasiewicz struct eth_dev *ioport;
5700a2430fSAndrzej Pietrasiewicz
5800a2430fSAndrzej Pietrasiewicz /* endpoints handle full and/or high speeds */
5900a2430fSAndrzej Pietrasiewicz struct usb_ep *in_ep;
6000a2430fSAndrzej Pietrasiewicz struct usb_ep *out_ep;
6100a2430fSAndrzej Pietrasiewicz
6200a2430fSAndrzej Pietrasiewicz bool is_zlp_ok;
6300a2430fSAndrzej Pietrasiewicz
6400a2430fSAndrzej Pietrasiewicz u16 cdc_filter;
6500a2430fSAndrzej Pietrasiewicz
6600a2430fSAndrzej Pietrasiewicz /* hooks for added framing, as needed for RNDIS and EEM. */
6700a2430fSAndrzej Pietrasiewicz u32 header_len;
6800a2430fSAndrzej Pietrasiewicz /* NCM requires fixed size bundles */
6900a2430fSAndrzej Pietrasiewicz bool is_fixed;
7000a2430fSAndrzej Pietrasiewicz u32 fixed_out_len;
7100a2430fSAndrzej Pietrasiewicz u32 fixed_in_len;
7200a2430fSAndrzej Pietrasiewicz bool supports_multi_frame;
7300a2430fSAndrzej Pietrasiewicz struct sk_buff *(*wrap)(struct gether *port,
7400a2430fSAndrzej Pietrasiewicz struct sk_buff *skb);
7500a2430fSAndrzej Pietrasiewicz int (*unwrap)(struct gether *port,
7600a2430fSAndrzej Pietrasiewicz struct sk_buff *skb,
7700a2430fSAndrzej Pietrasiewicz struct sk_buff_head *list);
7800a2430fSAndrzej Pietrasiewicz
7900a2430fSAndrzej Pietrasiewicz /* called on network open/close */
8000a2430fSAndrzej Pietrasiewicz void (*open)(struct gether *);
8100a2430fSAndrzej Pietrasiewicz void (*close)(struct gether *);
820a1af6dfSElson Roy Serrao bool is_suspend;
8300a2430fSAndrzej Pietrasiewicz };
8400a2430fSAndrzej Pietrasiewicz
8500a2430fSAndrzej Pietrasiewicz #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
8600a2430fSAndrzej Pietrasiewicz |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
8700a2430fSAndrzej Pietrasiewicz |USB_CDC_PACKET_TYPE_PROMISCUOUS \
8800a2430fSAndrzej Pietrasiewicz |USB_CDC_PACKET_TYPE_DIRECTED)
8900a2430fSAndrzej Pietrasiewicz
9000a2430fSAndrzej Pietrasiewicz /* variant of gether_setup that allows customizing network device name */
9100a2430fSAndrzej Pietrasiewicz struct eth_dev *gether_setup_name(struct usb_gadget *g,
9200a2430fSAndrzej Pietrasiewicz const char *dev_addr, const char *host_addr,
9300a2430fSAndrzej Pietrasiewicz u8 ethaddr[ETH_ALEN], unsigned qmult, const char *netname);
9400a2430fSAndrzej Pietrasiewicz
9500a2430fSAndrzej Pietrasiewicz /* netdev setup/teardown as directed by the gadget driver */
9600a2430fSAndrzej Pietrasiewicz /* gether_setup - initialize one ethernet-over-usb link
9700a2430fSAndrzej Pietrasiewicz * @g: gadget to associated with these links
9800a2430fSAndrzej Pietrasiewicz * @ethaddr: NULL, or a buffer in which the ethernet address of the
9900a2430fSAndrzej Pietrasiewicz * host side of the link is recorded
10000a2430fSAndrzej Pietrasiewicz * Context: may sleep
10100a2430fSAndrzej Pietrasiewicz *
10200a2430fSAndrzej Pietrasiewicz * This sets up the single network link that may be exported by a
10300a2430fSAndrzej Pietrasiewicz * gadget driver using this framework. The link layer addresses are
10400a2430fSAndrzej Pietrasiewicz * set up using module parameters.
10500a2430fSAndrzej Pietrasiewicz *
10600a2430fSAndrzej Pietrasiewicz * Returns a eth_dev pointer on success, or an ERR_PTR on failure
10700a2430fSAndrzej Pietrasiewicz */
gether_setup(struct usb_gadget * g,const char * dev_addr,const char * host_addr,u8 ethaddr[ETH_ALEN],unsigned qmult)10800a2430fSAndrzej Pietrasiewicz static inline struct eth_dev *gether_setup(struct usb_gadget *g,
10900a2430fSAndrzej Pietrasiewicz const char *dev_addr, const char *host_addr,
11000a2430fSAndrzej Pietrasiewicz u8 ethaddr[ETH_ALEN], unsigned qmult)
11100a2430fSAndrzej Pietrasiewicz {
11200a2430fSAndrzej Pietrasiewicz return gether_setup_name(g, dev_addr, host_addr, ethaddr, qmult, "usb");
11300a2430fSAndrzej Pietrasiewicz }
11400a2430fSAndrzej Pietrasiewicz
11500a2430fSAndrzej Pietrasiewicz /*
11600a2430fSAndrzej Pietrasiewicz * variant of gether_setup_default that allows customizing
11700a2430fSAndrzej Pietrasiewicz * network device name
11800a2430fSAndrzej Pietrasiewicz */
11900a2430fSAndrzej Pietrasiewicz struct net_device *gether_setup_name_default(const char *netname);
12000a2430fSAndrzej Pietrasiewicz
12100a2430fSAndrzej Pietrasiewicz /*
12200a2430fSAndrzej Pietrasiewicz * gether_register_netdev - register the net device
12300a2430fSAndrzej Pietrasiewicz * @net: net device to register
12400a2430fSAndrzej Pietrasiewicz *
12500a2430fSAndrzej Pietrasiewicz * Registers the net device associated with this ethernet-over-usb link
12600a2430fSAndrzej Pietrasiewicz *
12700a2430fSAndrzej Pietrasiewicz */
12800a2430fSAndrzej Pietrasiewicz int gether_register_netdev(struct net_device *net);
12900a2430fSAndrzej Pietrasiewicz
13000a2430fSAndrzej Pietrasiewicz /* gether_setup_default - initialize one ethernet-over-usb link
13100a2430fSAndrzej Pietrasiewicz * Context: may sleep
13200a2430fSAndrzej Pietrasiewicz *
13300a2430fSAndrzej Pietrasiewicz * This sets up the single network link that may be exported by a
13400a2430fSAndrzej Pietrasiewicz * gadget driver using this framework. The link layer addresses
13500a2430fSAndrzej Pietrasiewicz * are set to random values.
13600a2430fSAndrzej Pietrasiewicz *
13700a2430fSAndrzej Pietrasiewicz * Returns negative errno, or zero on success
13800a2430fSAndrzej Pietrasiewicz */
gether_setup_default(void)13900a2430fSAndrzej Pietrasiewicz static inline struct net_device *gether_setup_default(void)
14000a2430fSAndrzej Pietrasiewicz {
14100a2430fSAndrzej Pietrasiewicz return gether_setup_name_default("usb");
14200a2430fSAndrzej Pietrasiewicz }
14300a2430fSAndrzej Pietrasiewicz
14400a2430fSAndrzej Pietrasiewicz /**
14500a2430fSAndrzej Pietrasiewicz * gether_set_gadget - initialize one ethernet-over-usb link with a gadget
14600a2430fSAndrzej Pietrasiewicz * @net: device representing this link
14700a2430fSAndrzej Pietrasiewicz * @g: the gadget to initialize with
14800a2430fSAndrzej Pietrasiewicz *
14900a2430fSAndrzej Pietrasiewicz * This associates one ethernet-over-usb link with a gadget.
15000a2430fSAndrzej Pietrasiewicz */
15100a2430fSAndrzej Pietrasiewicz void gether_set_gadget(struct net_device *net, struct usb_gadget *g);
15200a2430fSAndrzej Pietrasiewicz
15300a2430fSAndrzej Pietrasiewicz /**
15400a2430fSAndrzej Pietrasiewicz * gether_set_dev_addr - initialize an ethernet-over-usb link with eth address
15500a2430fSAndrzej Pietrasiewicz * @net: device representing this link
15600a2430fSAndrzej Pietrasiewicz * @dev_addr: eth address of this device
15700a2430fSAndrzej Pietrasiewicz *
15800a2430fSAndrzej Pietrasiewicz * This sets the device-side Ethernet address of this ethernet-over-usb link
15900a2430fSAndrzej Pietrasiewicz * if dev_addr is correct.
16000a2430fSAndrzej Pietrasiewicz * Returns negative errno if the new address is incorrect.
16100a2430fSAndrzej Pietrasiewicz */
16200a2430fSAndrzej Pietrasiewicz int gether_set_dev_addr(struct net_device *net, const char *dev_addr);
16300a2430fSAndrzej Pietrasiewicz
16400a2430fSAndrzej Pietrasiewicz /**
16500a2430fSAndrzej Pietrasiewicz * gether_get_dev_addr - get an ethernet-over-usb link eth address
16600a2430fSAndrzej Pietrasiewicz * @net: device representing this link
16700a2430fSAndrzej Pietrasiewicz * @dev_addr: place to store device's eth address
16800a2430fSAndrzej Pietrasiewicz * @len: length of the @dev_addr buffer
16900a2430fSAndrzej Pietrasiewicz *
17000a2430fSAndrzej Pietrasiewicz * This gets the device-side Ethernet address of this ethernet-over-usb link.
17100a2430fSAndrzej Pietrasiewicz * Returns zero on success, else negative errno.
17200a2430fSAndrzej Pietrasiewicz */
17300a2430fSAndrzej Pietrasiewicz int gether_get_dev_addr(struct net_device *net, char *dev_addr, int len);
17400a2430fSAndrzej Pietrasiewicz
17500a2430fSAndrzej Pietrasiewicz /**
17600a2430fSAndrzej Pietrasiewicz * gether_set_host_addr - initialize an ethernet-over-usb link with host address
17700a2430fSAndrzej Pietrasiewicz * @net: device representing this link
17800a2430fSAndrzej Pietrasiewicz * @host_addr: eth address of the host
17900a2430fSAndrzej Pietrasiewicz *
18000a2430fSAndrzej Pietrasiewicz * This sets the host-side Ethernet address of this ethernet-over-usb link
18100a2430fSAndrzej Pietrasiewicz * if host_addr is correct.
18200a2430fSAndrzej Pietrasiewicz * Returns negative errno if the new address is incorrect.
18300a2430fSAndrzej Pietrasiewicz */
18400a2430fSAndrzej Pietrasiewicz int gether_set_host_addr(struct net_device *net, const char *host_addr);
18500a2430fSAndrzej Pietrasiewicz
18600a2430fSAndrzej Pietrasiewicz /**
18700a2430fSAndrzej Pietrasiewicz * gether_get_host_addr - get an ethernet-over-usb link host address
18800a2430fSAndrzej Pietrasiewicz * @net: device representing this link
18900a2430fSAndrzej Pietrasiewicz * @host_addr: place to store eth address of the host
19000a2430fSAndrzej Pietrasiewicz * @len: length of the @host_addr buffer
19100a2430fSAndrzej Pietrasiewicz *
19200a2430fSAndrzej Pietrasiewicz * This gets the host-side Ethernet address of this ethernet-over-usb link.
19300a2430fSAndrzej Pietrasiewicz * Returns zero on success, else negative errno.
19400a2430fSAndrzej Pietrasiewicz */
19500a2430fSAndrzej Pietrasiewicz int gether_get_host_addr(struct net_device *net, char *host_addr, int len);
19600a2430fSAndrzej Pietrasiewicz
19700a2430fSAndrzej Pietrasiewicz /**
19800a2430fSAndrzej Pietrasiewicz * gether_get_host_addr_cdc - get an ethernet-over-usb link host address
19900a2430fSAndrzej Pietrasiewicz * @net: device representing this link
20000a2430fSAndrzej Pietrasiewicz * @host_addr: place to store eth address of the host
20100a2430fSAndrzej Pietrasiewicz * @len: length of the @host_addr buffer
20200a2430fSAndrzej Pietrasiewicz *
20300a2430fSAndrzej Pietrasiewicz * This gets the CDC formatted host-side Ethernet address of this
20400a2430fSAndrzej Pietrasiewicz * ethernet-over-usb link.
20500a2430fSAndrzej Pietrasiewicz * Returns zero on success, else negative errno.
20600a2430fSAndrzej Pietrasiewicz */
20700a2430fSAndrzej Pietrasiewicz int gether_get_host_addr_cdc(struct net_device *net, char *host_addr, int len);
20800a2430fSAndrzej Pietrasiewicz
20900a2430fSAndrzej Pietrasiewicz /**
21000a2430fSAndrzej Pietrasiewicz * gether_get_host_addr_u8 - get an ethernet-over-usb link host address
21100a2430fSAndrzej Pietrasiewicz * @net: device representing this link
21200a2430fSAndrzej Pietrasiewicz * @host_mac: place to store the eth address of the host
21300a2430fSAndrzej Pietrasiewicz *
21400a2430fSAndrzej Pietrasiewicz * This gets the binary formatted host-side Ethernet address of this
21500a2430fSAndrzej Pietrasiewicz * ethernet-over-usb link.
21600a2430fSAndrzej Pietrasiewicz */
21700a2430fSAndrzej Pietrasiewicz void gether_get_host_addr_u8(struct net_device *net, u8 host_mac[ETH_ALEN]);
21800a2430fSAndrzej Pietrasiewicz
21900a2430fSAndrzej Pietrasiewicz /**
22000a2430fSAndrzej Pietrasiewicz * gether_set_qmult - initialize an ethernet-over-usb link with a multiplier
22100a2430fSAndrzej Pietrasiewicz * @net: device representing this link
22200a2430fSAndrzej Pietrasiewicz * @qmult: queue multiplier
22300a2430fSAndrzej Pietrasiewicz *
22400a2430fSAndrzej Pietrasiewicz * This sets the queue length multiplier of this ethernet-over-usb link.
22500a2430fSAndrzej Pietrasiewicz * For higher speeds use longer queues.
22600a2430fSAndrzej Pietrasiewicz */
22700a2430fSAndrzej Pietrasiewicz void gether_set_qmult(struct net_device *net, unsigned qmult);
22800a2430fSAndrzej Pietrasiewicz
22900a2430fSAndrzej Pietrasiewicz /**
23000a2430fSAndrzej Pietrasiewicz * gether_get_qmult - get an ethernet-over-usb link multiplier
23100a2430fSAndrzej Pietrasiewicz * @net: device representing this link
23200a2430fSAndrzej Pietrasiewicz *
23300a2430fSAndrzej Pietrasiewicz * This gets the queue length multiplier of this ethernet-over-usb link.
23400a2430fSAndrzej Pietrasiewicz */
23500a2430fSAndrzej Pietrasiewicz unsigned gether_get_qmult(struct net_device *net);
23600a2430fSAndrzej Pietrasiewicz
23700a2430fSAndrzej Pietrasiewicz /**
23800a2430fSAndrzej Pietrasiewicz * gether_get_ifname - get an ethernet-over-usb link interface name
23900a2430fSAndrzej Pietrasiewicz * @net: device representing this link
24000a2430fSAndrzej Pietrasiewicz * @name: place to store the interface name
24100a2430fSAndrzej Pietrasiewicz * @len: length of the @name buffer
24200a2430fSAndrzej Pietrasiewicz *
24300a2430fSAndrzej Pietrasiewicz * This gets the interface name of this ethernet-over-usb link.
24400a2430fSAndrzej Pietrasiewicz * Returns zero on success, else negative errno.
24500a2430fSAndrzej Pietrasiewicz */
24600a2430fSAndrzej Pietrasiewicz int gether_get_ifname(struct net_device *net, char *name, int len);
24700a2430fSAndrzej Pietrasiewicz
24863d15214SLorenzo Colitti /**
24963d15214SLorenzo Colitti * gether_set_ifname - set an ethernet-over-usb link interface name
25063d15214SLorenzo Colitti * @net: device representing this link
25163d15214SLorenzo Colitti * @name: new interface name
25263d15214SLorenzo Colitti * @len: length of @name
25363d15214SLorenzo Colitti *
25463d15214SLorenzo Colitti * This sets the interface name of this ethernet-over-usb link.
25563d15214SLorenzo Colitti * A single terminating newline, if any, is ignored.
25663d15214SLorenzo Colitti * Returns zero on success, else negative errno.
25763d15214SLorenzo Colitti */
25863d15214SLorenzo Colitti int gether_set_ifname(struct net_device *net, const char *name, int len);
25963d15214SLorenzo Colitti
26000a2430fSAndrzej Pietrasiewicz void gether_cleanup(struct eth_dev *dev);
26100a2430fSAndrzej Pietrasiewicz
2620a1af6dfSElson Roy Serrao void gether_suspend(struct gether *link);
2630a1af6dfSElson Roy Serrao void gether_resume(struct gether *link);
2640a1af6dfSElson Roy Serrao
26500a2430fSAndrzej Pietrasiewicz /* connect/disconnect is handled by individual functions */
26600a2430fSAndrzej Pietrasiewicz struct net_device *gether_connect(struct gether *);
26700a2430fSAndrzej Pietrasiewicz void gether_disconnect(struct gether *);
26800a2430fSAndrzej Pietrasiewicz
26900a2430fSAndrzej Pietrasiewicz /* Some controllers can't support CDC Ethernet (ECM) ... */
can_support_ecm(struct usb_gadget * gadget)27000a2430fSAndrzej Pietrasiewicz static inline bool can_support_ecm(struct usb_gadget *gadget)
27100a2430fSAndrzej Pietrasiewicz {
272736d093bSRobert Baldyga if (!gadget_is_altset_supported(gadget))
27300a2430fSAndrzej Pietrasiewicz return false;
27400a2430fSAndrzej Pietrasiewicz
27500a2430fSAndrzej Pietrasiewicz /* Everything else is *presumably* fine ... but this is a bit
27600a2430fSAndrzej Pietrasiewicz * chancy, so be **CERTAIN** there are no hardware issues with
27700a2430fSAndrzej Pietrasiewicz * your controller. Add it above if it can't handle CDC.
27800a2430fSAndrzej Pietrasiewicz */
27900a2430fSAndrzej Pietrasiewicz return true;
28000a2430fSAndrzej Pietrasiewicz }
28100a2430fSAndrzej Pietrasiewicz
282*8165763fSLinyu Yuan /* peak (theoretical) bulk transfer rate in bits-per-second */
gether_bitrate(struct usb_gadget * g)283*8165763fSLinyu Yuan static inline unsigned int gether_bitrate(struct usb_gadget *g)
284*8165763fSLinyu Yuan {
285*8165763fSLinyu Yuan if (g->speed >= USB_SPEED_SUPER_PLUS)
286*8165763fSLinyu Yuan return 4250000000U;
287*8165763fSLinyu Yuan if (g->speed == USB_SPEED_SUPER)
288*8165763fSLinyu Yuan return 3750000000U;
289*8165763fSLinyu Yuan else if (g->speed == USB_SPEED_HIGH)
290*8165763fSLinyu Yuan return 13 * 512 * 8 * 1000 * 8;
291*8165763fSLinyu Yuan else
292*8165763fSLinyu Yuan return 19 * 64 * 1 * 1000 * 8;
293*8165763fSLinyu Yuan }
294*8165763fSLinyu Yuan
29500a2430fSAndrzej Pietrasiewicz #endif /* __U_ETHER_H */
296