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