1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * USB Networking Link Interface
4  *
5  * Copyright (C) 2000-2005 by David Brownell <dbrownell@users.sourceforge.net>
6  * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
7  */
8 
9 #ifndef	__LINUX_USB_USBNET_H
10 #define	__LINUX_USB_USBNET_H
11 
12 #include <linux/mii.h>
13 #include <linux/netdevice.h>
14 #include <linux/skbuff.h>
15 #include <linux/types.h>
16 #include <linux/usb.h>
17 #include <linux/android_kabi.h>
18 
19 /* interface from usbnet core to each USB networking link we handle */
20 struct usbnet {
21 	/* housekeeping */
22 	struct usb_device	*udev;
23 	struct usb_interface	*intf;
24 	const struct driver_info *driver_info;
25 	const char		*driver_name;
26 	void			*driver_priv;
27 	wait_queue_head_t	wait;
28 	struct mutex		phy_mutex;
29 	unsigned char		suspend_count;
30 	unsigned char		pkt_cnt, pkt_err;
31 	unsigned short		rx_qlen, tx_qlen;
32 	unsigned		can_dma_sg:1;
33 
34 	/* i/o info: pipes etc */
35 	unsigned		in, out;
36 	struct usb_host_endpoint *status;
37 	unsigned		maxpacket;
38 	struct timer_list	delay;
39 	const char		*padding_pkt;
40 
41 	/* protocol/interface state */
42 	struct net_device	*net;
43 	int			msg_enable;
44 	unsigned long		data[5];
45 	u32			xid;
46 	u32			hard_mtu;	/* count any extra framing */
47 	size_t			rx_urb_size;	/* size for rx urbs */
48 	struct mii_if_info	mii;
49 	long			rx_speed;	/* If MII not used */
50 	long			tx_speed;	/* If MII not used */
51 #		define SPEED_UNSET	-1
52 
53 	/* various kinds of pending driver work */
54 	struct sk_buff_head	rxq;
55 	struct sk_buff_head	txq;
56 	struct sk_buff_head	done;
57 	struct sk_buff_head	rxq_pause;
58 	struct urb		*interrupt;
59 	unsigned		interrupt_count;
60 	struct mutex		interrupt_mutex;
61 	struct usb_anchor	deferred;
62 	struct tasklet_struct	bh;
63 
64 	struct work_struct	kevent;
65 	unsigned long		flags;
66 #		define EVENT_TX_HALT	0
67 #		define EVENT_RX_HALT	1
68 #		define EVENT_RX_MEMORY	2
69 #		define EVENT_STS_SPLIT	3
70 #		define EVENT_LINK_RESET	4
71 #		define EVENT_RX_PAUSED	5
72 #		define EVENT_DEV_ASLEEP 6
73 #		define EVENT_DEV_OPEN	7
74 #		define EVENT_DEVICE_REPORT_IDLE	8
75 #		define EVENT_NO_RUNTIME_PM	9
76 #		define EVENT_RX_KILL	10
77 #		define EVENT_LINK_CHANGE	11
78 #		define EVENT_SET_RX_MODE	12
79 #		define EVENT_NO_IP_ALIGN	13
80 #		define EVENT_LINK_CARRIER_ON	14
81 /* This one is special, as it indicates that the device is going away
82  * there are cyclic dependencies between tasklet, timer and bh
83  * that must be broken
84  */
85 #		define EVENT_UNPLUG		31
86 
87 	ANDROID_KABI_RESERVE(1);
88 	ANDROID_KABI_RESERVE(2);
89 	ANDROID_KABI_RESERVE(3);
90 	ANDROID_KABI_RESERVE(4);
91 };
92 
usbnet_going_away(struct usbnet * ubn)93 static inline bool usbnet_going_away(struct usbnet *ubn)
94 {
95 	return test_bit(EVENT_UNPLUG, &ubn->flags);
96 }
97 
usbnet_mark_going_away(struct usbnet * ubn)98 static inline void usbnet_mark_going_away(struct usbnet *ubn)
99 {
100 	set_bit(EVENT_UNPLUG, &ubn->flags);
101 }
102 
driver_of(struct usb_interface * intf)103 static inline struct usb_driver *driver_of(struct usb_interface *intf)
104 {
105 	return to_usb_driver(intf->dev.driver);
106 }
107 
108 /* interface from the device/framing level "minidriver" to core */
109 struct driver_info {
110 	char		*description;
111 
112 	int		flags;
113 /* framing is CDC Ethernet, not writing ZLPs (hw issues), or optionally: */
114 #define FLAG_FRAMING_NC	0x0001		/* guard against device dropouts */
115 #define FLAG_FRAMING_GL	0x0002		/* genelink batches packets */
116 #define FLAG_FRAMING_Z	0x0004		/* zaurus adds a trailer */
117 #define FLAG_FRAMING_RN	0x0008		/* RNDIS batches, plus huge header */
118 
119 #define FLAG_NO_SETINT	0x0010		/* device can't set_interface() */
120 #define FLAG_ETHER	0x0020		/* maybe use "eth%d" names */
121 
122 #define FLAG_FRAMING_AX 0x0040		/* AX88772/178 packets */
123 #define FLAG_WLAN	0x0080		/* use "wlan%d" names */
124 #define FLAG_AVOID_UNLINK_URBS 0x0100	/* don't unlink urbs at usbnet_stop() */
125 #define FLAG_SEND_ZLP	0x0200		/* hw requires ZLPs are sent */
126 #define FLAG_WWAN	0x0400		/* use "wwan%d" names */
127 
128 #define FLAG_LINK_INTR	0x0800		/* updates link (carrier) status */
129 
130 #define FLAG_POINTTOPOINT 0x1000	/* possibly use "usb%d" names */
131 
132 /*
133  * Indicates to usbnet, that USB driver accumulates multiple IP packets.
134  * Affects statistic (counters) and short packet handling.
135  */
136 #define FLAG_MULTI_PACKET	0x2000
137 #define FLAG_RX_ASSEMBLE	0x4000	/* rx packets may span >1 frames */
138 #define FLAG_NOARP		0x8000	/* device can't do ARP */
139 
140 	/* init device ... can sleep, or cause probe() failure */
141 	int	(*bind)(struct usbnet *, struct usb_interface *);
142 
143 	/* cleanup device ... can sleep, but can't fail */
144 	void	(*unbind)(struct usbnet *, struct usb_interface *);
145 
146 	/* reset device ... can sleep */
147 	int	(*reset)(struct usbnet *);
148 
149 	/* stop device ... can sleep */
150 	int	(*stop)(struct usbnet *);
151 
152 	/* see if peer is connected ... can sleep */
153 	int	(*check_connect)(struct usbnet *);
154 
155 	/* (dis)activate runtime power management */
156 	int	(*manage_power)(struct usbnet *, int);
157 
158 	/* for status polling */
159 	void	(*status)(struct usbnet *, struct urb *);
160 
161 	/* link reset handling, called from defer_kevent */
162 	int	(*link_reset)(struct usbnet *);
163 
164 	/* fixup rx packet (strip framing) */
165 	int	(*rx_fixup)(struct usbnet *dev, struct sk_buff *skb);
166 
167 	/* fixup tx packet (add framing) */
168 	struct sk_buff	*(*tx_fixup)(struct usbnet *dev,
169 				struct sk_buff *skb, gfp_t flags);
170 
171 	/* recover from timeout */
172 	void	(*recover)(struct usbnet *dev);
173 
174 	/* early initialization code, can sleep. This is for minidrivers
175 	 * having 'subminidrivers' that need to do extra initialization
176 	 * right after minidriver have initialized hardware. */
177 	int	(*early_init)(struct usbnet *dev);
178 
179 	/* called by minidriver when receiving indication */
180 	void	(*indication)(struct usbnet *dev, void *ind, int indlen);
181 
182 	/* rx mode change (device changes address list filtering) */
183 	void	(*set_rx_mode)(struct usbnet *dev);
184 
185 	/* for new devices, use the descriptor-reading code instead */
186 	int		in;		/* rx endpoint */
187 	int		out;		/* tx endpoint */
188 
189 	unsigned long	data;		/* Misc driver specific data */
190 
191 	ANDROID_KABI_RESERVE(1);
192 	ANDROID_KABI_RESERVE(2);
193 };
194 
195 /* Minidrivers are just drivers using the "usbnet" core as a powerful
196  * network-specific subroutine library ... that happens to do pretty
197  * much everything except custom framing and chip-specific stuff.
198  */
199 extern int usbnet_probe(struct usb_interface *, const struct usb_device_id *);
200 extern int usbnet_suspend(struct usb_interface *, pm_message_t);
201 extern int usbnet_resume(struct usb_interface *);
202 extern void usbnet_disconnect(struct usb_interface *);
203 extern void usbnet_device_suggests_idle(struct usbnet *dev);
204 
205 extern int usbnet_read_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
206 		    u16 value, u16 index, void *data, u16 size);
207 extern int usbnet_write_cmd(struct usbnet *dev, u8 cmd, u8 reqtype,
208 		    u16 value, u16 index, const void *data, u16 size);
209 extern int usbnet_read_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
210 		    u16 value, u16 index, void *data, u16 size);
211 extern int usbnet_write_cmd_nopm(struct usbnet *dev, u8 cmd, u8 reqtype,
212 		    u16 value, u16 index, const void *data, u16 size);
213 extern int usbnet_write_cmd_async(struct usbnet *dev, u8 cmd, u8 reqtype,
214 		    u16 value, u16 index, const void *data, u16 size);
215 
216 /* Drivers that reuse some of the standard USB CDC infrastructure
217  * (notably, using multiple interfaces according to the CDC
218  * union descriptor) get some helper code.
219  */
220 struct cdc_state {
221 	struct usb_cdc_header_desc	*header;
222 	struct usb_cdc_union_desc	*u;
223 	struct usb_cdc_ether_desc	*ether;
224 	struct usb_interface		*control;
225 	struct usb_interface		*data;
226 };
227 
228 extern void usbnet_cdc_update_filter(struct usbnet *dev);
229 extern int usbnet_generic_cdc_bind(struct usbnet *, struct usb_interface *);
230 extern int usbnet_ether_cdc_bind(struct usbnet *dev, struct usb_interface *intf);
231 extern int usbnet_cdc_bind(struct usbnet *, struct usb_interface *);
232 extern void usbnet_cdc_unbind(struct usbnet *, struct usb_interface *);
233 extern void usbnet_cdc_status(struct usbnet *, struct urb *);
234 extern int usbnet_cdc_zte_rx_fixup(struct usbnet *dev, struct sk_buff *skb);
235 
236 /* CDC and RNDIS support the same host-chosen packet filters for IN transfers */
237 #define	DEFAULT_FILTER	(USB_CDC_PACKET_TYPE_BROADCAST \
238 			|USB_CDC_PACKET_TYPE_ALL_MULTICAST \
239 			|USB_CDC_PACKET_TYPE_PROMISCUOUS \
240 			|USB_CDC_PACKET_TYPE_DIRECTED)
241 
242 
243 /* we record the state for each of our queued skbs */
244 enum skb_state {
245 	illegal = 0,
246 	tx_start, tx_done,
247 	rx_start, rx_done, rx_cleanup,
248 	unlink_start
249 };
250 
251 struct skb_data {	/* skb->cb is one of these */
252 	struct urb		*urb;
253 	struct usbnet		*dev;
254 	enum skb_state		state;
255 	long			length;
256 	unsigned long		packets;
257 };
258 
259 /* Drivers that set FLAG_MULTI_PACKET must call this in their
260  * tx_fixup method before returning an skb.
261  */
262 static inline void
usbnet_set_skb_tx_stats(struct sk_buff * skb,unsigned long packets,long bytes_delta)263 usbnet_set_skb_tx_stats(struct sk_buff *skb,
264 			unsigned long packets, long bytes_delta)
265 {
266 	struct skb_data *entry = (struct skb_data *) skb->cb;
267 
268 	entry->packets = packets;
269 	entry->length = bytes_delta;
270 }
271 
272 extern int usbnet_open(struct net_device *net);
273 extern int usbnet_stop(struct net_device *net);
274 extern netdev_tx_t usbnet_start_xmit(struct sk_buff *skb,
275 				     struct net_device *net);
276 extern void usbnet_tx_timeout(struct net_device *net, unsigned int txqueue);
277 extern int usbnet_change_mtu(struct net_device *net, int new_mtu);
278 
279 extern int usbnet_get_endpoints(struct usbnet *, struct usb_interface *);
280 extern int usbnet_get_ethernet_addr(struct usbnet *, int);
281 extern void usbnet_defer_kevent(struct usbnet *, int);
282 extern void usbnet_skb_return(struct usbnet *, struct sk_buff *);
283 extern void usbnet_unlink_rx_urbs(struct usbnet *);
284 
285 extern void usbnet_pause_rx(struct usbnet *);
286 extern void usbnet_resume_rx(struct usbnet *);
287 extern void usbnet_purge_paused_rxq(struct usbnet *);
288 
289 extern int usbnet_get_link_ksettings_mii(struct net_device *net,
290 				     struct ethtool_link_ksettings *cmd);
291 extern int usbnet_set_link_ksettings_mii(struct net_device *net,
292 				     const struct ethtool_link_ksettings *cmd);
293 extern int usbnet_get_link_ksettings_internal(struct net_device *net,
294 				     struct ethtool_link_ksettings *cmd);
295 extern u32 usbnet_get_link(struct net_device *net);
296 extern u32 usbnet_get_msglevel(struct net_device *);
297 extern void usbnet_set_msglevel(struct net_device *, u32);
298 extern void usbnet_set_rx_mode(struct net_device *net);
299 extern void usbnet_get_drvinfo(struct net_device *, struct ethtool_drvinfo *);
300 extern int usbnet_nway_reset(struct net_device *net);
301 
302 extern int usbnet_manage_power(struct usbnet *, int);
303 extern void usbnet_link_change(struct usbnet *, bool, bool);
304 
305 extern int usbnet_status_start(struct usbnet *dev, gfp_t mem_flags);
306 extern void usbnet_status_stop(struct usbnet *dev);
307 
308 extern void usbnet_update_max_qlen(struct usbnet *dev);
309 
310 #endif /* __LINUX_USB_USBNET_H */
311