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