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