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