1 |
1275 |
phoenix |
/*
|
2 |
|
|
* ether.c -- Ethernet gadget driver, with CDC and non-CDC options
|
3 |
|
|
*
|
4 |
|
|
* Copyright (C) 2003 David Brownell
|
5 |
|
|
*
|
6 |
|
|
* This program is free software; you can redistribute it and/or modify
|
7 |
|
|
* it under the terms of the GNU General Public License as published by
|
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
9 |
|
|
* (at your option) any later version.
|
10 |
|
|
*
|
11 |
|
|
* This program is distributed in the hope that it will be useful,
|
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
* GNU General Public License for more details.
|
15 |
|
|
*
|
16 |
|
|
* You should have received a copy of the GNU General Public License
|
17 |
|
|
* along with this program; if not, write to the Free Software
|
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 |
|
|
*/
|
20 |
|
|
|
21 |
|
|
|
22 |
|
|
// #define DEBUG 1
|
23 |
|
|
// #define VERBOSE
|
24 |
|
|
|
25 |
|
|
#include <linux/config.h>
|
26 |
|
|
#include <linux/kernel.h>
|
27 |
|
|
#include <linux/module.h>
|
28 |
|
|
#include <linux/delay.h>
|
29 |
|
|
#include <linux/ioport.h>
|
30 |
|
|
#include <linux/sched.h>
|
31 |
|
|
#include <linux/slab.h>
|
32 |
|
|
#include <linux/smp_lock.h>
|
33 |
|
|
#include <linux/errno.h>
|
34 |
|
|
#include <linux/init.h>
|
35 |
|
|
#include <linux/timer.h>
|
36 |
|
|
#include <linux/list.h>
|
37 |
|
|
#include <linux/interrupt.h>
|
38 |
|
|
#include <linux/uts.h>
|
39 |
|
|
#include <linux/version.h>
|
40 |
|
|
|
41 |
|
|
#include <asm/byteorder.h>
|
42 |
|
|
#include <asm/io.h>
|
43 |
|
|
#include <asm/irq.h>
|
44 |
|
|
#include <asm/system.h>
|
45 |
|
|
#include <asm/uaccess.h>
|
46 |
|
|
#include <asm/unaligned.h>
|
47 |
|
|
|
48 |
|
|
#include <linux/usb_ch9.h>
|
49 |
|
|
#include <linux/usb_gadget.h>
|
50 |
|
|
|
51 |
|
|
#include <linux/random.h>
|
52 |
|
|
#include <linux/netdevice.h>
|
53 |
|
|
#include <linux/etherdevice.h>
|
54 |
|
|
#include <linux/ethtool.h>
|
55 |
|
|
|
56 |
|
|
/*-------------------------------------------------------------------------*/
|
57 |
|
|
|
58 |
|
|
/*
|
59 |
|
|
* Ethernet gadget driver -- with CDC and non-CDC options
|
60 |
|
|
*
|
61 |
|
|
* CDC Ethernet is the standard USB solution for sending Ethernet frames
|
62 |
|
|
* using USB. Real hardware tends to use the same framing protocol but look
|
63 |
|
|
* different for control features. And Microsoft pushes their own approach
|
64 |
|
|
* (RNDIS) instead of the standard.
|
65 |
|
|
*
|
66 |
|
|
* There's some hardware that can't talk CDC. We make that hardware
|
67 |
|
|
* implement a "minimalist" vendor-agnostic CDC core: same framing, but
|
68 |
|
|
* link-level setup only requires activating the configuration.
|
69 |
|
|
*/
|
70 |
|
|
|
71 |
|
|
#define DRIVER_DESC "Ethernet Gadget"
|
72 |
|
|
#define DRIVER_VERSION "Bastille Day 2003"
|
73 |
|
|
|
74 |
|
|
static const char shortname [] = "ether";
|
75 |
|
|
static const char driver_desc [] = DRIVER_DESC;
|
76 |
|
|
|
77 |
|
|
#define MIN_PACKET sizeof(struct ethhdr)
|
78 |
|
|
#define MAX_PACKET ETH_DATA_LEN /* biggest packet we'll rx/tx */
|
79 |
|
|
#define RX_EXTRA 20 /* guard against rx overflows */
|
80 |
|
|
|
81 |
|
|
/* FIXME allow high speed jumbograms */
|
82 |
|
|
|
83 |
|
|
/*-------------------------------------------------------------------------*/
|
84 |
|
|
|
85 |
|
|
#ifndef container_of
|
86 |
|
|
#define container_of list_entry
|
87 |
|
|
#endif
|
88 |
|
|
|
89 |
|
|
/* 2.5 modified and renamed these */
|
90 |
|
|
#ifndef INIT_WORK
|
91 |
|
|
#define work_struct tq_struct
|
92 |
|
|
#define INIT_WORK INIT_TQUEUE
|
93 |
|
|
#define schedule_work schedule_task
|
94 |
|
|
#define flush_scheduled_work flush_scheduled_tasks
|
95 |
|
|
#endif
|
96 |
|
|
|
97 |
|
|
/*-------------------------------------------------------------------------*/
|
98 |
|
|
|
99 |
|
|
struct eth_dev {
|
100 |
|
|
spinlock_t lock;
|
101 |
|
|
struct usb_gadget *gadget;
|
102 |
|
|
struct usb_request *req; /* for control responses */
|
103 |
|
|
|
104 |
|
|
u8 config;
|
105 |
|
|
struct usb_ep *in_ep, *out_ep, *status_ep;
|
106 |
|
|
const struct usb_endpoint_descriptor
|
107 |
|
|
*in, *out, *status;
|
108 |
|
|
struct list_head tx_reqs, rx_reqs;
|
109 |
|
|
|
110 |
|
|
struct net_device *net;
|
111 |
|
|
struct net_device_stats stats;
|
112 |
|
|
atomic_t tx_qlen;
|
113 |
|
|
|
114 |
|
|
struct work_struct work;
|
115 |
|
|
unsigned long todo;
|
116 |
|
|
#define WORK_RX_MEMORY 0
|
117 |
|
|
};
|
118 |
|
|
|
119 |
|
|
/*-------------------------------------------------------------------------*/
|
120 |
|
|
|
121 |
|
|
/* Thanks to NetChip Technologies for donating this product ID.
|
122 |
|
|
*
|
123 |
|
|
* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
|
124 |
|
|
* Instead: allocate your own, using normal USB-IF procedures.
|
125 |
|
|
*/
|
126 |
|
|
#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
|
127 |
|
|
#define DRIVER_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
|
128 |
|
|
|
129 |
|
|
/*-------------------------------------------------------------------------*/
|
130 |
|
|
|
131 |
|
|
/*
|
132 |
|
|
* hardware-specific configuration, controlled by which device
|
133 |
|
|
* controller driver was configured.
|
134 |
|
|
*
|
135 |
|
|
* CHIP ... hardware identifier
|
136 |
|
|
* DRIVER_VERSION_NUM ... alerts the host side driver to differences
|
137 |
|
|
* EP_*_NAME ... which endpoints do we use for which purpose?
|
138 |
|
|
* EP_*_NUM ... numbers for them (often limited by hardware)
|
139 |
|
|
* WAKEUP ... if hardware supports remote wakeup AND we will issue the
|
140 |
|
|
* usb_gadget_wakeup() call to initiate it, USB_CONFIG_ATT_WAKEUP
|
141 |
|
|
*
|
142 |
|
|
* hw_optimize(gadget) ... for any hardware tweaks we want to kick in
|
143 |
|
|
* before we enable our endpoints
|
144 |
|
|
*
|
145 |
|
|
* add other defines for other portability issues, like hardware that
|
146 |
|
|
* for some reason doesn't handle full speed bulk maxpacket of 64.
|
147 |
|
|
*/
|
148 |
|
|
|
149 |
|
|
#define DEV_CONFIG_VALUE 3 /* some hardware cares */
|
150 |
|
|
|
151 |
|
|
/* #undef on hardware that can't implement CDC */
|
152 |
|
|
#define DEV_CONFIG_CDC
|
153 |
|
|
|
154 |
|
|
/* undef on bus-powered hardware, and #define MAX_USB_POWER */
|
155 |
|
|
#define SELFPOWER
|
156 |
|
|
|
157 |
|
|
/*
|
158 |
|
|
* NetChip 2280, PCI based.
|
159 |
|
|
*
|
160 |
|
|
* use DMA with fat fifos for all data traffic, PIO for the status channel
|
161 |
|
|
* where its 64 byte maxpacket ceiling is no issue.
|
162 |
|
|
*
|
163 |
|
|
* performance note: only PIO needs per-usb-packet IRQs (ep0, ep-e, ep-f)
|
164 |
|
|
* otherwise IRQs are per-Ethernet-packet unless TX_DELAY and chaining help.
|
165 |
|
|
*/
|
166 |
|
|
#ifdef CONFIG_USB_GADGET_NET2280
|
167 |
|
|
#define CHIP "net2280"
|
168 |
|
|
#define DEFAULT_QLEN 4 /* has dma chaining */
|
169 |
|
|
#define DRIVER_VERSION_NUM 0x0111
|
170 |
|
|
static const char EP_OUT_NAME [] = "ep-a";
|
171 |
|
|
#define EP_OUT_NUM 1
|
172 |
|
|
static const char EP_IN_NAME [] = "ep-b";
|
173 |
|
|
#define EP_IN_NUM 2
|
174 |
|
|
static const char EP_STATUS_NAME [] = "ep-f";
|
175 |
|
|
#define EP_STATUS_NUM 3
|
176 |
|
|
/* supports remote wakeup, but this driver doesn't */
|
177 |
|
|
|
178 |
|
|
extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
|
179 |
|
|
|
180 |
|
|
static inline void hw_optimize (struct usb_gadget *gadget)
|
181 |
|
|
{
|
182 |
|
|
/* we can have bigger ep-a/ep-b fifos (2KB each, 4 USB packets
|
183 |
|
|
* for highspeed bulk) because we're not using ep-c/ep-d.
|
184 |
|
|
*/
|
185 |
|
|
net2280_set_fifo_mode (gadget, 1);
|
186 |
|
|
}
|
187 |
|
|
#endif
|
188 |
|
|
|
189 |
|
|
/*
|
190 |
|
|
* PXA-2xx UDC: widely used in second gen Linux-capable ARM PDAs
|
191 |
|
|
* and other products. The IXP-42x UDC is register-compatible.
|
192 |
|
|
*
|
193 |
|
|
* multiple interfaces (or altsettings) aren't usable. so this hardware
|
194 |
|
|
* can't implement CDC, which needs both capabilities.
|
195 |
|
|
*/
|
196 |
|
|
#ifdef CONFIG_USB_GADGET_PXA2XX
|
197 |
|
|
#undef DEV_CONFIG_CDC
|
198 |
|
|
#define CHIP "pxa2xx"
|
199 |
|
|
#define DRIVER_VERSION_NUM 0x0113
|
200 |
|
|
static const char EP_OUT_NAME [] = "ep2out-bulk";
|
201 |
|
|
#define EP_OUT_NUM 2
|
202 |
|
|
static const char EP_IN_NAME [] = "ep1in-bulk";
|
203 |
|
|
#define EP_IN_NUM 1
|
204 |
|
|
/* supports remote wakeup, but this driver doesn't */
|
205 |
|
|
|
206 |
|
|
/* no hw optimizations to apply */
|
207 |
|
|
#define hw_optimize(g) do {} while (0)
|
208 |
|
|
#endif
|
209 |
|
|
|
210 |
|
|
/*
|
211 |
|
|
* SA-1100 UDC: widely used in first gen Linux-capable PDAs.
|
212 |
|
|
*
|
213 |
|
|
* can't have a notification endpoint, since there are only the two
|
214 |
|
|
* bulk-capable ones. the CDC spec allows that.
|
215 |
|
|
*/
|
216 |
|
|
#ifdef CONFIG_USB_GADGET_SA1100
|
217 |
|
|
#define CHIP "sa1100"
|
218 |
|
|
#define DRIVER_VERSION_NUM 0x0115
|
219 |
|
|
static const char EP_OUT_NAME [] = "ep1out-bulk";
|
220 |
|
|
#define EP_OUT_NUM 1
|
221 |
|
|
static const char EP_IN_NAME [] = "ep2in-bulk";
|
222 |
|
|
#define EP_IN_NUM 2
|
223 |
|
|
// EP_STATUS_NUM is undefined
|
224 |
|
|
/* doesn't support remote wakeup? */
|
225 |
|
|
|
226 |
|
|
/* no hw optimizations to apply */
|
227 |
|
|
#define hw_optimize(g) do {} while (0)
|
228 |
|
|
#endif
|
229 |
|
|
|
230 |
|
|
/*
|
231 |
|
|
* Toshiba TC86C001 ("Goku-S") UDC
|
232 |
|
|
*
|
233 |
|
|
* This has three semi-configurable full speed bulk/interrupt endpoints.
|
234 |
|
|
*/
|
235 |
|
|
#ifdef CONFIG_USB_GADGET_GOKU
|
236 |
|
|
#define CHIP "goku"
|
237 |
|
|
#define DRIVER_VERSION_NUM 0x0116
|
238 |
|
|
static const char EP_OUT_NAME [] = "ep1-bulk";
|
239 |
|
|
#define EP_OUT_NUM 1
|
240 |
|
|
static const char EP_IN_NAME [] = "ep2-bulk";
|
241 |
|
|
#define EP_IN_NUM 2
|
242 |
|
|
static const char EP_STATUS_NAME [] = "ep3-bulk";
|
243 |
|
|
#define EP_STATUS_NUM 3
|
244 |
|
|
/* doesn't support remote wakeup */
|
245 |
|
|
|
246 |
|
|
#define hw_optimize(g) do {} while (0)
|
247 |
|
|
#endif
|
248 |
|
|
|
249 |
|
|
/*
|
250 |
|
|
* SuperH UDC: UDC built-in to some Renesas SH processors.
|
251 |
|
|
*
|
252 |
|
|
* This has three semi-configurable full speed bulk/interrupt endpoints.
|
253 |
|
|
*
|
254 |
|
|
* Only one configuration and interface is supported. So this hardware
|
255 |
|
|
* can't implement CDC.
|
256 |
|
|
*/
|
257 |
|
|
#ifdef CONFIG_USB_GADGET_SUPERH
|
258 |
|
|
#undef DEV_CONFIG_CDC
|
259 |
|
|
#define CHIP "superh"
|
260 |
|
|
#define DRIVER_VERSION_NUM 0x0117
|
261 |
|
|
static const char EP_OUT_NAME[] = "ep1out-bulk";
|
262 |
|
|
#define EP_OUT_NUM 1
|
263 |
|
|
static const char EP_IN_NAME[] = "ep2in-bulk";
|
264 |
|
|
#define EP_IN_NUM 2
|
265 |
|
|
|
266 |
|
|
#define hw_optimize(g) do {} while (0)
|
267 |
|
|
#endif
|
268 |
|
|
|
269 |
|
|
/*-------------------------------------------------------------------------*/
|
270 |
|
|
|
271 |
|
|
#ifndef CHIP
|
272 |
|
|
# error Configure some USB peripheral controller driver!
|
273 |
|
|
#endif
|
274 |
|
|
|
275 |
|
|
/* We normally expect hardware that can talk CDC. That involves
|
276 |
|
|
* using multiple interfaces and altsettings, and maybe a status
|
277 |
|
|
* interrupt. Driver binding to be done according to USB-IF class,
|
278 |
|
|
* though you can use different VENDOR and PRODUCT numbers if you
|
279 |
|
|
* want (and they're officially assigned).
|
280 |
|
|
*
|
281 |
|
|
* For hardware that can't talk CDC, we use the same vendor ID that
|
282 |
|
|
* ARM Linux has used for ethernet-over-usb, both with sa1100 and
|
283 |
|
|
* with pxa250. We're protocol-compatible, if the host-side drivers
|
284 |
|
|
* use the endpoint descriptors. DRIVER_VERSION_NUM is nonzero, so
|
285 |
|
|
* drivers that need to hard-wire endpoint numbers have a hook.
|
286 |
|
|
*/
|
287 |
|
|
#ifdef DEV_CONFIG_CDC
|
288 |
|
|
#define DEV_CONFIG_CLASS USB_CLASS_COMM
|
289 |
|
|
#else
|
290 |
|
|
#define DEV_CONFIG_CLASS USB_CLASS_VENDOR_SPEC
|
291 |
|
|
#undef EP_STATUS_NUM
|
292 |
|
|
#undef DRIVER_VENDOR_NUM
|
293 |
|
|
#undef DRIVER_PRODUCT_NUM
|
294 |
|
|
#define DRIVER_VENDOR_NUM 0x049f
|
295 |
|
|
#define DRIVER_PRODUCT_NUM 0x505a
|
296 |
|
|
#endif /* CONFIG_CDC_ETHER */
|
297 |
|
|
|
298 |
|
|
/* power usage is config specific.
|
299 |
|
|
* hardware that supports remote wakeup defaults to disabling it.
|
300 |
|
|
*/
|
301 |
|
|
|
302 |
|
|
#ifndef MAX_USB_POWER
|
303 |
|
|
#ifdef SELFPOWER
|
304 |
|
|
/* some hosts are confused by 0mA */
|
305 |
|
|
#define MAX_USB_POWER 2 /* mA */
|
306 |
|
|
#else
|
307 |
|
|
/* bus powered */
|
308 |
|
|
#error Define your bus power consumption!
|
309 |
|
|
#endif
|
310 |
|
|
#endif /* MAX_USB_POWER */
|
311 |
|
|
|
312 |
|
|
#ifndef WAKEUP
|
313 |
|
|
/* default: this driver won't do remote wakeup */
|
314 |
|
|
#define WAKEUP 0
|
315 |
|
|
/* else value must be USB_CONFIG_ATT_WAKEUP */
|
316 |
|
|
#endif
|
317 |
|
|
|
318 |
|
|
/*-------------------------------------------------------------------------*/
|
319 |
|
|
|
320 |
|
|
#ifndef DEFAULT_QLEN
|
321 |
|
|
#define DEFAULT_QLEN 2 /* double buffering by default */
|
322 |
|
|
#endif
|
323 |
|
|
|
324 |
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
325 |
|
|
|
326 |
|
|
static unsigned qmult = 5;
|
327 |
|
|
MODULE_PARM (qmult, "i");
|
328 |
|
|
|
329 |
|
|
|
330 |
|
|
/* for dual-speed hardware, use deeper queues at highspeed */
|
331 |
|
|
#define qlen(gadget) \
|
332 |
|
|
(DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
|
333 |
|
|
|
334 |
|
|
/* also defer IRQs on highspeed TX */
|
335 |
|
|
#define TX_DELAY DEFAULT_QLEN
|
336 |
|
|
|
337 |
|
|
#else /* full speed (low speed doesn't do bulk) */
|
338 |
|
|
#define qlen(gadget) DEFAULT_QLEN
|
339 |
|
|
#endif
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
/*-------------------------------------------------------------------------*/
|
343 |
|
|
|
344 |
|
|
#define xprintk(d,level,fmt,args...) \
|
345 |
|
|
printk(level "%s: " fmt , (d)->net->name , ## args)
|
346 |
|
|
|
347 |
|
|
#ifdef DEBUG
|
348 |
|
|
#undef DEBUG
|
349 |
|
|
#define DEBUG(dev,fmt,args...) \
|
350 |
|
|
xprintk(dev , KERN_DEBUG , fmt , ## args)
|
351 |
|
|
#else
|
352 |
|
|
#define DEBUG(dev,fmt,args...) \
|
353 |
|
|
do { } while (0)
|
354 |
|
|
#endif /* DEBUG */
|
355 |
|
|
|
356 |
|
|
#ifdef VERBOSE
|
357 |
|
|
#define VDEBUG DEBUG
|
358 |
|
|
#else
|
359 |
|
|
#define VDEBUG(dev,fmt,args...) \
|
360 |
|
|
do { } while (0)
|
361 |
|
|
#endif /* DEBUG */
|
362 |
|
|
|
363 |
|
|
#define ERROR(dev,fmt,args...) \
|
364 |
|
|
xprintk(dev , KERN_ERR , fmt , ## args)
|
365 |
|
|
#define WARN(dev,fmt,args...) \
|
366 |
|
|
xprintk(dev , KERN_WARNING , fmt , ## args)
|
367 |
|
|
#define INFO(dev,fmt,args...) \
|
368 |
|
|
xprintk(dev , KERN_INFO , fmt , ## args)
|
369 |
|
|
|
370 |
|
|
/*-------------------------------------------------------------------------*/
|
371 |
|
|
|
372 |
|
|
/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
|
373 |
|
|
* ep0 implementation: descriptors, config management, setup().
|
374 |
|
|
* also optional class-specific notification interrupt transfer.
|
375 |
|
|
*/
|
376 |
|
|
|
377 |
|
|
/*
|
378 |
|
|
* DESCRIPTORS ... most are static, but strings and (full) configuration
|
379 |
|
|
* descriptors are built on demand. Notice how most of the cdc descriptors
|
380 |
|
|
* aren't needed in the "minimalist" mode.
|
381 |
|
|
*/
|
382 |
|
|
|
383 |
|
|
#define STRING_MANUFACTURER 1
|
384 |
|
|
#define STRING_PRODUCT 2
|
385 |
|
|
#define STRING_ETHADDR 3
|
386 |
|
|
#define STRING_DATA 4
|
387 |
|
|
#define STRING_CONTROL 5
|
388 |
|
|
|
389 |
|
|
#define USB_BUFSIZ 256 /* holds our biggest descriptor */
|
390 |
|
|
|
391 |
|
|
/*
|
392 |
|
|
* This device advertises one configuration.
|
393 |
|
|
*/
|
394 |
|
|
static struct usb_device_descriptor
|
395 |
|
|
device_desc = {
|
396 |
|
|
.bLength = sizeof device_desc,
|
397 |
|
|
.bDescriptorType = USB_DT_DEVICE,
|
398 |
|
|
|
399 |
|
|
.bcdUSB = __constant_cpu_to_le16 (0x0200),
|
400 |
|
|
|
401 |
|
|
.bDeviceClass = DEV_CONFIG_CLASS,
|
402 |
|
|
.bDeviceSubClass = 0,
|
403 |
|
|
.bDeviceProtocol = 0,
|
404 |
|
|
|
405 |
|
|
.idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
|
406 |
|
|
.idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
|
407 |
|
|
.bcdDevice = __constant_cpu_to_le16 (DRIVER_VERSION_NUM),
|
408 |
|
|
.iManufacturer = STRING_MANUFACTURER,
|
409 |
|
|
.iProduct = STRING_PRODUCT,
|
410 |
|
|
.bNumConfigurations = 1,
|
411 |
|
|
};
|
412 |
|
|
|
413 |
|
|
static struct usb_config_descriptor
|
414 |
|
|
eth_config = {
|
415 |
|
|
.bLength = sizeof eth_config,
|
416 |
|
|
.bDescriptorType = USB_DT_CONFIG,
|
417 |
|
|
|
418 |
|
|
/* compute wTotalLength on the fly */
|
419 |
|
|
#ifdef DEV_CONFIG_CDC
|
420 |
|
|
.bNumInterfaces = 2,
|
421 |
|
|
#else
|
422 |
|
|
.bNumInterfaces = 1,
|
423 |
|
|
#endif
|
424 |
|
|
.bConfigurationValue = DEV_CONFIG_VALUE,
|
425 |
|
|
.iConfiguration = STRING_PRODUCT,
|
426 |
|
|
.bmAttributes = USB_CONFIG_ATT_ONE | WAKEUP,
|
427 |
|
|
.bMaxPower = (MAX_USB_POWER + 1) / 2,
|
428 |
|
|
};
|
429 |
|
|
|
430 |
|
|
#ifdef DEV_CONFIG_CDC
|
431 |
|
|
|
432 |
|
|
/*
|
433 |
|
|
* Compared to the "minimalist" non-CDC model, the CDC model adds
|
434 |
|
|
* three class descriptors, two interface descrioptors, and a status
|
435 |
|
|
* endpoint. Both have a "data" interface and two bulk endpoints.
|
436 |
|
|
* There are also differences in how control requests are handled.
|
437 |
|
|
*/
|
438 |
|
|
|
439 |
|
|
/* master comm interface optionally has a status notification endpoint */
|
440 |
|
|
|
441 |
|
|
static const struct usb_interface_descriptor
|
442 |
|
|
control_intf = {
|
443 |
|
|
.bLength = sizeof control_intf,
|
444 |
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
445 |
|
|
|
446 |
|
|
.bInterfaceNumber = 0,
|
447 |
|
|
#ifdef EP_STATUS_NUM
|
448 |
|
|
.bNumEndpoints = 1,
|
449 |
|
|
#else
|
450 |
|
|
.bNumEndpoints = 0,
|
451 |
|
|
#endif
|
452 |
|
|
.bInterfaceClass = USB_CLASS_COMM,
|
453 |
|
|
.bInterfaceSubClass = 6, /* ethernet control model */
|
454 |
|
|
.bInterfaceProtocol = 0,
|
455 |
|
|
.iInterface = STRING_CONTROL,
|
456 |
|
|
};
|
457 |
|
|
|
458 |
|
|
/* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
|
459 |
|
|
struct header_desc {
|
460 |
|
|
u8 bLength;
|
461 |
|
|
u8 bDescriptorType;
|
462 |
|
|
u8 bDescriptorSubType;
|
463 |
|
|
|
464 |
|
|
u16 bcdCDC;
|
465 |
|
|
} __attribute__ ((packed));
|
466 |
|
|
|
467 |
|
|
static const struct header_desc header_desc = {
|
468 |
|
|
.bLength = sizeof header_desc,
|
469 |
|
|
.bDescriptorType = 0x24,
|
470 |
|
|
.bDescriptorSubType = 0,
|
471 |
|
|
|
472 |
|
|
.bcdCDC = __constant_cpu_to_le16 (0x0110),
|
473 |
|
|
};
|
474 |
|
|
|
475 |
|
|
/* "Union Functional Descriptor" from CDC spec 5.2.3.X */
|
476 |
|
|
struct union_desc {
|
477 |
|
|
u8 bLength;
|
478 |
|
|
u8 bDescriptorType;
|
479 |
|
|
u8 bDescriptorSubType;
|
480 |
|
|
|
481 |
|
|
u8 bMasterInterface0;
|
482 |
|
|
u8 bSlaveInterface0;
|
483 |
|
|
/* ... and there could be other slave interfaces */
|
484 |
|
|
} __attribute__ ((packed));
|
485 |
|
|
|
486 |
|
|
static const struct union_desc union_desc = {
|
487 |
|
|
.bLength = sizeof union_desc,
|
488 |
|
|
.bDescriptorType = 0x24,
|
489 |
|
|
.bDescriptorSubType = 6,
|
490 |
|
|
|
491 |
|
|
.bMasterInterface0 = 0, /* index of control interface */
|
492 |
|
|
.bSlaveInterface0 = 1, /* index of DATA interface */
|
493 |
|
|
};
|
494 |
|
|
|
495 |
|
|
/* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
|
496 |
|
|
struct ether_desc {
|
497 |
|
|
u8 bLength;
|
498 |
|
|
u8 bDescriptorType;
|
499 |
|
|
u8 bDescriptorSubType;
|
500 |
|
|
|
501 |
|
|
u8 iMACAddress;
|
502 |
|
|
u32 bmEthernetStatistics;
|
503 |
|
|
u16 wMaxSegmentSize;
|
504 |
|
|
u16 wNumberMCFilters;
|
505 |
|
|
u8 bNumberPowerFilters;
|
506 |
|
|
} __attribute__ ((packed));
|
507 |
|
|
|
508 |
|
|
static const struct ether_desc ether_desc = {
|
509 |
|
|
.bLength = sizeof ether_desc,
|
510 |
|
|
.bDescriptorType = 0x24,
|
511 |
|
|
.bDescriptorSubType = 0x0f,
|
512 |
|
|
|
513 |
|
|
/* this descriptor actually adds value, surprise! */
|
514 |
|
|
.iMACAddress = STRING_ETHADDR,
|
515 |
|
|
.bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
|
516 |
|
|
.wMaxSegmentSize = __constant_cpu_to_le16 (MAX_PACKET + ETH_HLEN),
|
517 |
|
|
.wNumberMCFilters = __constant_cpu_to_le16 (0),
|
518 |
|
|
.bNumberPowerFilters = 0,
|
519 |
|
|
};
|
520 |
|
|
|
521 |
|
|
#ifdef EP_STATUS_NUM
|
522 |
|
|
|
523 |
|
|
/* include the status endpoint if we can, even though it's optional.
|
524 |
|
|
*
|
525 |
|
|
* some drivers (like current Linux cdc-ether!) "need" it to exist even
|
526 |
|
|
* if they ignore the connect/disconnect notifications that real aether
|
527 |
|
|
* can provide. more advanced cdc configurations might want to support
|
528 |
|
|
* encapsulated commands (vendor-specific, using control-OUT).
|
529 |
|
|
*/
|
530 |
|
|
|
531 |
|
|
#define LOG2_STATUS_INTERVAL_MSEC 6
|
532 |
|
|
#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
|
533 |
|
|
static const struct usb_endpoint_descriptor
|
534 |
|
|
fs_status_desc = {
|
535 |
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
536 |
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
537 |
|
|
|
538 |
|
|
.bEndpointAddress = EP_STATUS_NUM | USB_DIR_IN,
|
539 |
|
|
.bmAttributes = USB_ENDPOINT_XFER_INT,
|
540 |
|
|
.wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
|
541 |
|
|
.bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
|
542 |
|
|
};
|
543 |
|
|
#endif
|
544 |
|
|
|
545 |
|
|
/* the default data interface has no endpoints ... */
|
546 |
|
|
|
547 |
|
|
static const struct usb_interface_descriptor
|
548 |
|
|
data_nop_intf = {
|
549 |
|
|
.bLength = sizeof data_nop_intf,
|
550 |
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
551 |
|
|
|
552 |
|
|
.bInterfaceNumber = 1,
|
553 |
|
|
.bAlternateSetting = 0,
|
554 |
|
|
.bNumEndpoints = 0,
|
555 |
|
|
.bInterfaceClass = USB_CLASS_CDC_DATA,
|
556 |
|
|
.bInterfaceSubClass = 0,
|
557 |
|
|
.bInterfaceProtocol = 0,
|
558 |
|
|
.iInterface = STRING_DATA,
|
559 |
|
|
};
|
560 |
|
|
|
561 |
|
|
/* ... but the "real" data interface has two full speed bulk endpoints */
|
562 |
|
|
|
563 |
|
|
static const struct usb_interface_descriptor
|
564 |
|
|
data_intf = {
|
565 |
|
|
.bLength = sizeof data_intf,
|
566 |
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
567 |
|
|
|
568 |
|
|
.bInterfaceNumber = 1,
|
569 |
|
|
.bAlternateSetting = 1,
|
570 |
|
|
.bNumEndpoints = 2,
|
571 |
|
|
.bInterfaceClass = USB_CLASS_CDC_DATA,
|
572 |
|
|
.bInterfaceSubClass = 0,
|
573 |
|
|
.bInterfaceProtocol = 0,
|
574 |
|
|
.iInterface = STRING_DATA,
|
575 |
|
|
};
|
576 |
|
|
#else
|
577 |
|
|
|
578 |
|
|
/*
|
579 |
|
|
* "Minimalist" non-CDC option is a simple vendor-neutral model that most
|
580 |
|
|
* full speed controllers can handle: one interface, two bulk endpoints.
|
581 |
|
|
*/
|
582 |
|
|
|
583 |
|
|
static const struct usb_interface_descriptor
|
584 |
|
|
data_intf = {
|
585 |
|
|
.bLength = sizeof data_intf,
|
586 |
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
587 |
|
|
|
588 |
|
|
.bInterfaceNumber = 0,
|
589 |
|
|
.bAlternateSetting = 0,
|
590 |
|
|
.bNumEndpoints = 2,
|
591 |
|
|
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
|
592 |
|
|
.bInterfaceSubClass = 0,
|
593 |
|
|
.bInterfaceProtocol = 0,
|
594 |
|
|
.iInterface = STRING_DATA,
|
595 |
|
|
};
|
596 |
|
|
|
597 |
|
|
#endif /* DEV_CONFIG_CDC */
|
598 |
|
|
|
599 |
|
|
|
600 |
|
|
static const struct usb_endpoint_descriptor
|
601 |
|
|
fs_source_desc = {
|
602 |
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
603 |
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
604 |
|
|
|
605 |
|
|
.bEndpointAddress = EP_IN_NUM | USB_DIR_IN,
|
606 |
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
607 |
|
|
.wMaxPacketSize = __constant_cpu_to_le16 (64),
|
608 |
|
|
};
|
609 |
|
|
|
610 |
|
|
static const struct usb_endpoint_descriptor
|
611 |
|
|
fs_sink_desc = {
|
612 |
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
613 |
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
614 |
|
|
|
615 |
|
|
.bEndpointAddress = EP_OUT_NUM,
|
616 |
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
617 |
|
|
.wMaxPacketSize = __constant_cpu_to_le16 (64),
|
618 |
|
|
};
|
619 |
|
|
|
620 |
|
|
static const struct usb_descriptor_header *fs_function [] = {
|
621 |
|
|
#ifdef DEV_CONFIG_CDC
|
622 |
|
|
/* "cdc" mode descriptors */
|
623 |
|
|
(struct usb_descriptor_header *) &control_intf,
|
624 |
|
|
(struct usb_descriptor_header *) &header_desc,
|
625 |
|
|
(struct usb_descriptor_header *) &union_desc,
|
626 |
|
|
(struct usb_descriptor_header *) ðer_desc,
|
627 |
|
|
#ifdef EP_STATUS_NUM
|
628 |
|
|
(struct usb_descriptor_header *) &fs_status_desc,
|
629 |
|
|
#endif
|
630 |
|
|
(struct usb_descriptor_header *) &data_nop_intf,
|
631 |
|
|
#endif /* DEV_CONFIG_CDC */
|
632 |
|
|
/* minimalist core */
|
633 |
|
|
(struct usb_descriptor_header *) &data_intf,
|
634 |
|
|
(struct usb_descriptor_header *) &fs_source_desc,
|
635 |
|
|
(struct usb_descriptor_header *) &fs_sink_desc,
|
636 |
|
|
0,
|
637 |
|
|
};
|
638 |
|
|
|
639 |
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
640 |
|
|
|
641 |
|
|
/*
|
642 |
|
|
* usb 2.0 devices need to expose both high speed and full speed
|
643 |
|
|
* descriptors, unless they only run at full speed.
|
644 |
|
|
*/
|
645 |
|
|
|
646 |
|
|
#ifdef EP_STATUS_NUM
|
647 |
|
|
static const struct usb_endpoint_descriptor
|
648 |
|
|
hs_status_desc = {
|
649 |
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
650 |
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
651 |
|
|
|
652 |
|
|
.bEndpointAddress = EP_STATUS_NUM | USB_DIR_IN,
|
653 |
|
|
.bmAttributes = USB_ENDPOINT_XFER_INT,
|
654 |
|
|
.wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
|
655 |
|
|
.bInterval = LOG2_STATUS_INTERVAL_MSEC + 3,
|
656 |
|
|
};
|
657 |
|
|
#endif
|
658 |
|
|
|
659 |
|
|
static const struct usb_endpoint_descriptor
|
660 |
|
|
hs_source_desc = {
|
661 |
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
662 |
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
663 |
|
|
|
664 |
|
|
.bEndpointAddress = EP_IN_NUM | USB_DIR_IN,
|
665 |
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
666 |
|
|
.wMaxPacketSize = __constant_cpu_to_le16 (512),
|
667 |
|
|
.bInterval = 1,
|
668 |
|
|
};
|
669 |
|
|
|
670 |
|
|
static const struct usb_endpoint_descriptor
|
671 |
|
|
hs_sink_desc = {
|
672 |
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
673 |
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
674 |
|
|
|
675 |
|
|
.bEndpointAddress = EP_OUT_NUM,
|
676 |
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
677 |
|
|
.wMaxPacketSize = __constant_cpu_to_le16 (512),
|
678 |
|
|
.bInterval = 1,
|
679 |
|
|
};
|
680 |
|
|
|
681 |
|
|
static struct usb_qualifier_descriptor
|
682 |
|
|
dev_qualifier = {
|
683 |
|
|
.bLength = sizeof dev_qualifier,
|
684 |
|
|
.bDescriptorType = USB_DT_DEVICE_QUALIFIER,
|
685 |
|
|
|
686 |
|
|
.bcdUSB = __constant_cpu_to_le16 (0x0200),
|
687 |
|
|
.bDeviceClass = DEV_CONFIG_CLASS,
|
688 |
|
|
|
689 |
|
|
.bNumConfigurations = 1,
|
690 |
|
|
};
|
691 |
|
|
|
692 |
|
|
static const struct usb_descriptor_header *hs_function [] = {
|
693 |
|
|
#ifdef DEV_CONFIG_CDC
|
694 |
|
|
/* "cdc" mode descriptors */
|
695 |
|
|
(struct usb_descriptor_header *) &control_intf,
|
696 |
|
|
(struct usb_descriptor_header *) &header_desc,
|
697 |
|
|
(struct usb_descriptor_header *) &union_desc,
|
698 |
|
|
(struct usb_descriptor_header *) ðer_desc,
|
699 |
|
|
#ifdef EP_STATUS_NUM
|
700 |
|
|
(struct usb_descriptor_header *) &hs_status_desc,
|
701 |
|
|
#endif
|
702 |
|
|
(struct usb_descriptor_header *) &data_nop_intf,
|
703 |
|
|
#endif /* DEV_CONFIG_CDC */
|
704 |
|
|
/* minimalist core */
|
705 |
|
|
(struct usb_descriptor_header *) &data_intf,
|
706 |
|
|
(struct usb_descriptor_header *) &hs_source_desc,
|
707 |
|
|
(struct usb_descriptor_header *) &hs_sink_desc,
|
708 |
|
|
0,
|
709 |
|
|
};
|
710 |
|
|
|
711 |
|
|
|
712 |
|
|
/* maxpacket and other transfer characteristics vary by speed. */
|
713 |
|
|
#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
|
714 |
|
|
|
715 |
|
|
#else
|
716 |
|
|
|
717 |
|
|
/* if there's no high speed support, maxpacket doesn't change. */
|
718 |
|
|
#define ep_desc(g,hs,fs) fs
|
719 |
|
|
|
720 |
|
|
#endif /* !CONFIG_USB_GADGET_DUALSPEED */
|
721 |
|
|
|
722 |
|
|
/*-------------------------------------------------------------------------*/
|
723 |
|
|
|
724 |
|
|
/* descriptors that are built on-demand */
|
725 |
|
|
|
726 |
|
|
#ifdef DEV_CONFIG_CDC
|
727 |
|
|
/* address that the host will use ... usually assigned at random */
|
728 |
|
|
static char ethaddr [2 * ETH_ALEN + 1];
|
729 |
|
|
#endif
|
730 |
|
|
|
731 |
|
|
/* static strings, in iso 8859/1 */
|
732 |
|
|
static struct usb_string strings [] = {
|
733 |
|
|
{ STRING_MANUFACTURER, UTS_SYSNAME " " UTS_RELEASE "/" CHIP, },
|
734 |
|
|
{ STRING_PRODUCT, driver_desc, },
|
735 |
|
|
#ifdef DEV_CONFIG_CDC
|
736 |
|
|
{ STRING_ETHADDR, ethaddr, },
|
737 |
|
|
{ STRING_CONTROL, "CDC Communications Control", },
|
738 |
|
|
#endif
|
739 |
|
|
{ STRING_DATA, "Ethernet Data", },
|
740 |
|
|
{ } /* end of list */
|
741 |
|
|
};
|
742 |
|
|
|
743 |
|
|
static struct usb_gadget_strings stringtab = {
|
744 |
|
|
.language = 0x0409, /* en-us */
|
745 |
|
|
.strings = strings,
|
746 |
|
|
};
|
747 |
|
|
|
748 |
|
|
/*
|
749 |
|
|
* one config, two interfaces: control, data.
|
750 |
|
|
* complications: class descriptors, and an altsetting.
|
751 |
|
|
*/
|
752 |
|
|
static int
|
753 |
|
|
config_buf (enum usb_device_speed speed, u8 *buf, u8 type, unsigned index)
|
754 |
|
|
{
|
755 |
|
|
int len;
|
756 |
|
|
const struct usb_descriptor_header **function = fs_function;
|
757 |
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
758 |
|
|
int hs = (speed == USB_SPEED_HIGH);
|
759 |
|
|
|
760 |
|
|
if (type == USB_DT_OTHER_SPEED_CONFIG)
|
761 |
|
|
hs = !hs;
|
762 |
|
|
if (hs)
|
763 |
|
|
function = hs_function;
|
764 |
|
|
#endif
|
765 |
|
|
|
766 |
|
|
/* a single configuration must always be index 0 */
|
767 |
|
|
if (index > 0)
|
768 |
|
|
return -EINVAL;
|
769 |
|
|
len = usb_gadget_config_buf (ð_config, buf, USB_BUFSIZ, function);
|
770 |
|
|
if (len < 0)
|
771 |
|
|
return len;
|
772 |
|
|
((struct usb_config_descriptor *) buf)->bDescriptorType = type;
|
773 |
|
|
return len;
|
774 |
|
|
}
|
775 |
|
|
|
776 |
|
|
/*-------------------------------------------------------------------------*/
|
777 |
|
|
|
778 |
|
|
static void eth_start (struct eth_dev *dev, int gfp_flags);
|
779 |
|
|
static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags);
|
780 |
|
|
|
781 |
|
|
static int
|
782 |
|
|
set_ether_config (struct eth_dev *dev, int gfp_flags)
|
783 |
|
|
{
|
784 |
|
|
int result = 0;
|
785 |
|
|
struct usb_ep *ep;
|
786 |
|
|
struct usb_gadget *gadget = dev->gadget;
|
787 |
|
|
|
788 |
|
|
gadget_for_each_ep (ep, gadget) {
|
789 |
|
|
const struct usb_endpoint_descriptor *d;
|
790 |
|
|
|
791 |
|
|
#ifdef DEV_CONFIG_CDC
|
792 |
|
|
/* With CDC, the host isn't allowed to use these two data
|
793 |
|
|
* endpoints in the default altsetting for the interface.
|
794 |
|
|
* so we don't activate them yet.
|
795 |
|
|
*/
|
796 |
|
|
|
797 |
|
|
/* one endpoint writes data back IN to the host */
|
798 |
|
|
if (strcmp (ep->name, EP_IN_NAME) == 0) {
|
799 |
|
|
d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
|
800 |
|
|
ep->driver_data = dev;
|
801 |
|
|
dev->in_ep = ep;
|
802 |
|
|
dev->in = d;
|
803 |
|
|
continue;
|
804 |
|
|
|
805 |
|
|
/* one endpoint just reads OUT packets */
|
806 |
|
|
} else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
|
807 |
|
|
d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
|
808 |
|
|
ep->driver_data = dev;
|
809 |
|
|
dev->out_ep = ep;
|
810 |
|
|
dev->out = d;
|
811 |
|
|
continue;
|
812 |
|
|
}
|
813 |
|
|
|
814 |
|
|
#ifdef EP_STATUS_NUM
|
815 |
|
|
/* optional status/notification endpoint */
|
816 |
|
|
else if (strcmp (ep->name, EP_STATUS_NAME) == 0) {
|
817 |
|
|
d = ep_desc (gadget, &hs_status_desc, &fs_status_desc);
|
818 |
|
|
result = usb_ep_enable (ep, d);
|
819 |
|
|
if (result == 0) {
|
820 |
|
|
ep->driver_data = dev;
|
821 |
|
|
dev->status_ep = ep;
|
822 |
|
|
dev->status = d;
|
823 |
|
|
continue;
|
824 |
|
|
}
|
825 |
|
|
}
|
826 |
|
|
#endif
|
827 |
|
|
|
828 |
|
|
#else /* !CONFIG_CDC_ETHER */
|
829 |
|
|
|
830 |
|
|
/* non-CDC is simpler: if the device is there,
|
831 |
|
|
* it's live with rx and tx endpoints.
|
832 |
|
|
*/
|
833 |
|
|
/* one endpoint writes data back IN to the host */
|
834 |
|
|
if (strcmp (ep->name, EP_IN_NAME) == 0) {
|
835 |
|
|
d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
|
836 |
|
|
result = usb_ep_enable (ep, d);
|
837 |
|
|
if (result == 0) {
|
838 |
|
|
ep->driver_data = dev;
|
839 |
|
|
dev->in_ep = ep;
|
840 |
|
|
dev->in = d;
|
841 |
|
|
continue;
|
842 |
|
|
}
|
843 |
|
|
|
844 |
|
|
/* one endpoint just reads OUT packets */
|
845 |
|
|
} else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
|
846 |
|
|
d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
|
847 |
|
|
result = usb_ep_enable (ep, d);
|
848 |
|
|
if (result == 0) {
|
849 |
|
|
ep->driver_data = dev;
|
850 |
|
|
dev->out_ep = ep;
|
851 |
|
|
dev->out = d;
|
852 |
|
|
continue;
|
853 |
|
|
}
|
854 |
|
|
}
|
855 |
|
|
|
856 |
|
|
#endif /* !CONFIG_CDC_ETHER */
|
857 |
|
|
|
858 |
|
|
/* ignore any other endpoints */
|
859 |
|
|
else
|
860 |
|
|
continue;
|
861 |
|
|
|
862 |
|
|
/* stop on error */
|
863 |
|
|
ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
|
864 |
|
|
break;
|
865 |
|
|
}
|
866 |
|
|
if (!result && (!dev->in_ep || !dev->out_ep))
|
867 |
|
|
result = -ENODEV;
|
868 |
|
|
|
869 |
|
|
if (result == 0)
|
870 |
|
|
result = alloc_requests (dev, qlen (gadget), gfp_flags);
|
871 |
|
|
|
872 |
|
|
#ifndef DEV_CONFIG_CDC
|
873 |
|
|
if (result == 0) {
|
874 |
|
|
netif_carrier_on (dev->net);
|
875 |
|
|
if (netif_running (dev->net)) {
|
876 |
|
|
spin_unlock (&dev->lock);
|
877 |
|
|
eth_start (dev, GFP_ATOMIC);
|
878 |
|
|
spin_lock (&dev->lock);
|
879 |
|
|
}
|
880 |
|
|
} else {
|
881 |
|
|
(void) usb_ep_disable (dev->in_ep);
|
882 |
|
|
dev->in_ep = 0;
|
883 |
|
|
dev->in = 0;
|
884 |
|
|
(void) usb_ep_disable (dev->out_ep);
|
885 |
|
|
dev->out_ep = 0;
|
886 |
|
|
dev->out = 0;
|
887 |
|
|
}
|
888 |
|
|
#endif /* !CONFIG_CDC_ETHER */
|
889 |
|
|
|
890 |
|
|
if (result == 0)
|
891 |
|
|
DEBUG (dev, "qlen %d\n", qlen (gadget));
|
892 |
|
|
|
893 |
|
|
/* caller is responsible for cleanup on error */
|
894 |
|
|
return result;
|
895 |
|
|
}
|
896 |
|
|
|
897 |
|
|
static void eth_reset_config (struct eth_dev *dev)
|
898 |
|
|
{
|
899 |
|
|
struct usb_request *req;
|
900 |
|
|
|
901 |
|
|
if (dev->config == 0)
|
902 |
|
|
return;
|
903 |
|
|
|
904 |
|
|
DEBUG (dev, "%s\n", __FUNCTION__);
|
905 |
|
|
|
906 |
|
|
netif_stop_queue (dev->net);
|
907 |
|
|
netif_carrier_off (dev->net);
|
908 |
|
|
|
909 |
|
|
/* disable endpoints, forcing (synchronous) completion of
|
910 |
|
|
* pending i/o. then free the requests.
|
911 |
|
|
*/
|
912 |
|
|
if (dev->in_ep) {
|
913 |
|
|
usb_ep_disable (dev->in_ep);
|
914 |
|
|
while (likely (!list_empty (&dev->tx_reqs))) {
|
915 |
|
|
req = container_of (dev->tx_reqs.next,
|
916 |
|
|
struct usb_request, list);
|
917 |
|
|
list_del (&req->list);
|
918 |
|
|
usb_ep_free_request (dev->in_ep, req);
|
919 |
|
|
}
|
920 |
|
|
dev->in_ep = 0;
|
921 |
|
|
}
|
922 |
|
|
if (dev->out_ep) {
|
923 |
|
|
usb_ep_disable (dev->out_ep);
|
924 |
|
|
while (likely (!list_empty (&dev->rx_reqs))) {
|
925 |
|
|
req = container_of (dev->rx_reqs.next,
|
926 |
|
|
struct usb_request, list);
|
927 |
|
|
list_del (&req->list);
|
928 |
|
|
usb_ep_free_request (dev->out_ep, req);
|
929 |
|
|
}
|
930 |
|
|
dev->out_ep = 0;
|
931 |
|
|
}
|
932 |
|
|
|
933 |
|
|
#ifdef EP_STATUS_NUM
|
934 |
|
|
if (dev->status_ep) {
|
935 |
|
|
usb_ep_disable (dev->status_ep);
|
936 |
|
|
dev->status_ep = 0;
|
937 |
|
|
}
|
938 |
|
|
#endif
|
939 |
|
|
dev->config = 0;
|
940 |
|
|
}
|
941 |
|
|
|
942 |
|
|
/* change our operational config. must agree with the code
|
943 |
|
|
* that returns config descriptors, and altsetting code.
|
944 |
|
|
*/
|
945 |
|
|
static int
|
946 |
|
|
eth_set_config (struct eth_dev *dev, unsigned number, int gfp_flags)
|
947 |
|
|
{
|
948 |
|
|
int result = 0;
|
949 |
|
|
struct usb_gadget *gadget = dev->gadget;
|
950 |
|
|
|
951 |
|
|
if (number == dev->config)
|
952 |
|
|
return 0;
|
953 |
|
|
|
954 |
|
|
#ifdef CONFIG_USB_GADGET_SA1100
|
955 |
|
|
if (dev->config && atomic_read (&dev->tx_qlen) != 0) {
|
956 |
|
|
/* tx fifo is full, but we can't clear it...*/
|
957 |
|
|
INFO (dev, "can't change configurations\n");
|
958 |
|
|
return -ESPIPE;
|
959 |
|
|
}
|
960 |
|
|
#endif
|
961 |
|
|
eth_reset_config (dev);
|
962 |
|
|
hw_optimize (gadget);
|
963 |
|
|
|
964 |
|
|
switch (number) {
|
965 |
|
|
case DEV_CONFIG_VALUE:
|
966 |
|
|
result = set_ether_config (dev, gfp_flags);
|
967 |
|
|
break;
|
968 |
|
|
default:
|
969 |
|
|
result = -EINVAL;
|
970 |
|
|
/* FALL THROUGH */
|
971 |
|
|
case 0:
|
972 |
|
|
return result;
|
973 |
|
|
}
|
974 |
|
|
|
975 |
|
|
if (result)
|
976 |
|
|
eth_reset_config (dev);
|
977 |
|
|
else {
|
978 |
|
|
char *speed;
|
979 |
|
|
|
980 |
|
|
switch (gadget->speed) {
|
981 |
|
|
case USB_SPEED_FULL: speed = "full"; break;
|
982 |
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
983 |
|
|
case USB_SPEED_HIGH: speed = "high"; break;
|
984 |
|
|
#endif
|
985 |
|
|
default: speed = "?"; break;
|
986 |
|
|
}
|
987 |
|
|
|
988 |
|
|
dev->config = number;
|
989 |
|
|
INFO (dev, "%s speed config #%d: %s\n", speed, number,
|
990 |
|
|
driver_desc);
|
991 |
|
|
}
|
992 |
|
|
return result;
|
993 |
|
|
}
|
994 |
|
|
|
995 |
|
|
/*-------------------------------------------------------------------------*/
|
996 |
|
|
|
997 |
|
|
#ifdef EP_STATUS_NUM
|
998 |
|
|
|
999 |
|
|
/* section 3.8.2 table 11 of the CDC spec lists Ethernet notifications */
|
1000 |
|
|
#define CDC_NOTIFY_NETWORK_CONNECTION 0x00 /* required; 6.3.1 */
|
1001 |
|
|
#define CDC_NOTIFY_RESPONSE_AVAILABLE 0x01 /* optional; 6.3.2 */
|
1002 |
|
|
#define CDC_NOTIFY_SPEED_CHANGE 0x2a /* required; 6.3.8 */
|
1003 |
|
|
|
1004 |
|
|
struct cdc_notification {
|
1005 |
|
|
u8 bmRequestType;
|
1006 |
|
|
u8 bNotificationType;
|
1007 |
|
|
u16 wValue;
|
1008 |
|
|
u16 wIndex;
|
1009 |
|
|
u16 wLength;
|
1010 |
|
|
|
1011 |
|
|
/* SPEED_CHANGE data looks like this */
|
1012 |
|
|
u32 data [2];
|
1013 |
|
|
};
|
1014 |
|
|
|
1015 |
|
|
static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
|
1016 |
|
|
{
|
1017 |
|
|
struct cdc_notification *event = req->buf;
|
1018 |
|
|
int value = req->status;
|
1019 |
|
|
struct eth_dev *dev = ep->driver_data;
|
1020 |
|
|
|
1021 |
|
|
/* issue the second notification if host reads the first */
|
1022 |
|
|
if (event->bNotificationType == CDC_NOTIFY_NETWORK_CONNECTION
|
1023 |
|
|
&& value == 0) {
|
1024 |
|
|
event->bmRequestType = 0xA1;
|
1025 |
|
|
event->bNotificationType = CDC_NOTIFY_SPEED_CHANGE;
|
1026 |
|
|
event->wValue = __constant_cpu_to_le16 (0);
|
1027 |
|
|
event->wIndex = __constant_cpu_to_le16 (1);
|
1028 |
|
|
event->wLength = __constant_cpu_to_le16 (8);
|
1029 |
|
|
|
1030 |
|
|
/* SPEED_CHANGE data is up/down speeds in bits/sec */
|
1031 |
|
|
event->data [0] = event->data [1] =
|
1032 |
|
|
(dev->gadget->speed == USB_SPEED_HIGH)
|
1033 |
|
|
? (13 * 512 * 8 * 1000 * 8)
|
1034 |
|
|
: (19 * 64 * 1 * 1000 * 8);
|
1035 |
|
|
|
1036 |
|
|
req->length = 16;
|
1037 |
|
|
value = usb_ep_queue (ep, req, GFP_ATOMIC);
|
1038 |
|
|
DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
|
1039 |
|
|
if (value == 0)
|
1040 |
|
|
return;
|
1041 |
|
|
} else
|
1042 |
|
|
DEBUG (dev, "event %02x --> %d\n",
|
1043 |
|
|
event->bNotificationType, value);
|
1044 |
|
|
|
1045 |
|
|
/* free when done */
|
1046 |
|
|
usb_ep_free_buffer (ep, req->buf, req->dma, 16);
|
1047 |
|
|
usb_ep_free_request (ep, req);
|
1048 |
|
|
}
|
1049 |
|
|
|
1050 |
|
|
static void issue_start_status (struct eth_dev *dev)
|
1051 |
|
|
{
|
1052 |
|
|
struct usb_request *req;
|
1053 |
|
|
struct cdc_notification *event;
|
1054 |
|
|
int value;
|
1055 |
|
|
|
1056 |
|
|
DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
|
1057 |
|
|
|
1058 |
|
|
/* flush old status
|
1059 |
|
|
*
|
1060 |
|
|
* FIXME ugly idiom, maybe we'd be better with just
|
1061 |
|
|
* a "cancel the whole queue" primitive since any
|
1062 |
|
|
* unlink-one primitive has way too many error modes.
|
1063 |
|
|
* here, we "know" toggle is already clear...
|
1064 |
|
|
*/
|
1065 |
|
|
usb_ep_disable (dev->status_ep);
|
1066 |
|
|
usb_ep_enable (dev->status_ep, dev->status);
|
1067 |
|
|
|
1068 |
|
|
/* FIXME make these allocations static like dev->req */
|
1069 |
|
|
req = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
|
1070 |
|
|
if (req == 0) {
|
1071 |
|
|
DEBUG (dev, "status ENOMEM\n");
|
1072 |
|
|
return;
|
1073 |
|
|
}
|
1074 |
|
|
req->buf = usb_ep_alloc_buffer (dev->status_ep, 16,
|
1075 |
|
|
&dev->req->dma, GFP_ATOMIC);
|
1076 |
|
|
if (req->buf == 0) {
|
1077 |
|
|
DEBUG (dev, "status buf ENOMEM\n");
|
1078 |
|
|
free_req:
|
1079 |
|
|
usb_ep_free_request (dev->status_ep, req);
|
1080 |
|
|
return;
|
1081 |
|
|
}
|
1082 |
|
|
|
1083 |
|
|
/* 3.8.1 says to issue first NETWORK_CONNECTION, then
|
1084 |
|
|
* a SPEED_CHANGE. could be useful in some configs.
|
1085 |
|
|
*/
|
1086 |
|
|
event = req->buf;
|
1087 |
|
|
event->bmRequestType = 0xA1;
|
1088 |
|
|
event->bNotificationType = CDC_NOTIFY_NETWORK_CONNECTION;
|
1089 |
|
|
event->wValue = __constant_cpu_to_le16 (1); /* connected */
|
1090 |
|
|
event->wIndex = __constant_cpu_to_le16 (1);
|
1091 |
|
|
event->wLength = 0;
|
1092 |
|
|
|
1093 |
|
|
req->length = 8;
|
1094 |
|
|
req->complete = eth_status_complete;
|
1095 |
|
|
value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
|
1096 |
|
|
if (value < 0) {
|
1097 |
|
|
DEBUG (dev, "status buf queue --> %d\n", value);
|
1098 |
|
|
usb_ep_free_buffer (dev->status_ep,
|
1099 |
|
|
req->buf, dev->req->dma, 16);
|
1100 |
|
|
goto free_req;
|
1101 |
|
|
}
|
1102 |
|
|
}
|
1103 |
|
|
|
1104 |
|
|
#endif
|
1105 |
|
|
|
1106 |
|
|
/*-------------------------------------------------------------------------*/
|
1107 |
|
|
|
1108 |
|
|
static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
|
1109 |
|
|
{
|
1110 |
|
|
if (req->status || req->actual != req->length)
|
1111 |
|
|
DEBUG ((struct eth_dev *) ep->driver_data,
|
1112 |
|
|
"setup complete --> %d, %d/%d\n",
|
1113 |
|
|
req->status, req->actual, req->length);
|
1114 |
|
|
}
|
1115 |
|
|
|
1116 |
|
|
/* see section 3.8.2 table 10 of the CDC spec for more ethernet
|
1117 |
|
|
* requests, mostly for filters (multicast, pm) and statistics
|
1118 |
|
|
*/
|
1119 |
|
|
#define CDC_SEND_ENCAPSULATED_REQUEST 0x00 /* optional */
|
1120 |
|
|
#define CDC_GET_ENCAPSULATED_RESPONSE 0x01 /* optional */
|
1121 |
|
|
#define CDC_SET_ETHERNET_PACKET_FILTER 0x43 /* required */
|
1122 |
|
|
|
1123 |
|
|
/*
|
1124 |
|
|
* The setup() callback implements all the ep0 functionality that's not
|
1125 |
|
|
* handled lower down. CDC has a number of less-common features:
|
1126 |
|
|
*
|
1127 |
|
|
* - two interfaces: control, and ethernet data
|
1128 |
|
|
* - data interface has two altsettings: default, and active
|
1129 |
|
|
* - class-specific descriptors for the control interface
|
1130 |
|
|
* - a mandatory class-specific control request
|
1131 |
|
|
*/
|
1132 |
|
|
static int
|
1133 |
|
|
eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
|
1134 |
|
|
{
|
1135 |
|
|
struct eth_dev *dev = get_gadget_data (gadget);
|
1136 |
|
|
struct usb_request *req = dev->req;
|
1137 |
|
|
int value = -EOPNOTSUPP;
|
1138 |
|
|
|
1139 |
|
|
/* descriptors just go into the pre-allocated ep0 buffer,
|
1140 |
|
|
* while config change events may enable network traffic.
|
1141 |
|
|
*/
|
1142 |
|
|
switch (ctrl->bRequest) {
|
1143 |
|
|
|
1144 |
|
|
case USB_REQ_GET_DESCRIPTOR:
|
1145 |
|
|
if (ctrl->bRequestType != USB_DIR_IN)
|
1146 |
|
|
break;
|
1147 |
|
|
switch (ctrl->wValue >> 8) {
|
1148 |
|
|
|
1149 |
|
|
case USB_DT_DEVICE:
|
1150 |
|
|
value = min (ctrl->wLength, (u16) sizeof device_desc);
|
1151 |
|
|
memcpy (req->buf, &device_desc, value);
|
1152 |
|
|
break;
|
1153 |
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
1154 |
|
|
case USB_DT_DEVICE_QUALIFIER:
|
1155 |
|
|
if (!gadget->is_dualspeed)
|
1156 |
|
|
break;
|
1157 |
|
|
value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
|
1158 |
|
|
memcpy (req->buf, &dev_qualifier, value);
|
1159 |
|
|
break;
|
1160 |
|
|
|
1161 |
|
|
case USB_DT_OTHER_SPEED_CONFIG:
|
1162 |
|
|
if (!gadget->is_dualspeed)
|
1163 |
|
|
break;
|
1164 |
|
|
// FALLTHROUGH
|
1165 |
|
|
#endif /* CONFIG_USB_GADGET_DUALSPEED */
|
1166 |
|
|
case USB_DT_CONFIG:
|
1167 |
|
|
value = config_buf (gadget->speed, req->buf,
|
1168 |
|
|
ctrl->wValue >> 8,
|
1169 |
|
|
ctrl->wValue & 0xff);
|
1170 |
|
|
if (value >= 0)
|
1171 |
|
|
value = min (ctrl->wLength, (u16) value);
|
1172 |
|
|
break;
|
1173 |
|
|
|
1174 |
|
|
case USB_DT_STRING:
|
1175 |
|
|
value = usb_gadget_get_string (&stringtab,
|
1176 |
|
|
ctrl->wValue & 0xff, req->buf);
|
1177 |
|
|
if (value >= 0)
|
1178 |
|
|
value = min (ctrl->wLength, (u16) value);
|
1179 |
|
|
break;
|
1180 |
|
|
}
|
1181 |
|
|
break;
|
1182 |
|
|
|
1183 |
|
|
case USB_REQ_SET_CONFIGURATION:
|
1184 |
|
|
if (ctrl->bRequestType != 0)
|
1185 |
|
|
break;
|
1186 |
|
|
spin_lock (&dev->lock);
|
1187 |
|
|
value = eth_set_config (dev, ctrl->wValue, GFP_ATOMIC);
|
1188 |
|
|
spin_unlock (&dev->lock);
|
1189 |
|
|
break;
|
1190 |
|
|
#ifdef CONFIG_USB_GADGET_PXA2XX
|
1191 |
|
|
/* PXA UDC prevents us from using SET_INTERFACE in normal ways.
|
1192 |
|
|
* And it hides GET_CONFIGURATION and GET_INTERFACE too.
|
1193 |
|
|
*/
|
1194 |
|
|
case USB_REQ_SET_INTERFACE:
|
1195 |
|
|
spin_lock (&dev->lock);
|
1196 |
|
|
value = eth_set_config (dev, DEV_CONFIG_VALUE, GFP_ATOMIC);
|
1197 |
|
|
spin_unlock (&dev->lock);
|
1198 |
|
|
break;
|
1199 |
|
|
|
1200 |
|
|
#else /* hardware that that stays out of our way */
|
1201 |
|
|
case USB_REQ_GET_CONFIGURATION:
|
1202 |
|
|
if (ctrl->bRequestType != USB_DIR_IN)
|
1203 |
|
|
break;
|
1204 |
|
|
*(u8 *)req->buf = dev->config;
|
1205 |
|
|
value = min (ctrl->wLength, (u16) 1);
|
1206 |
|
|
break;
|
1207 |
|
|
|
1208 |
|
|
case USB_REQ_SET_INTERFACE:
|
1209 |
|
|
if (ctrl->bRequestType != USB_RECIP_INTERFACE
|
1210 |
|
|
|| !dev->config
|
1211 |
|
|
|| ctrl->wIndex > 1)
|
1212 |
|
|
break;
|
1213 |
|
|
spin_lock (&dev->lock);
|
1214 |
|
|
switch (ctrl->wIndex) {
|
1215 |
|
|
case 0: /* control/master intf */
|
1216 |
|
|
if (ctrl->wValue != 0)
|
1217 |
|
|
break;
|
1218 |
|
|
#ifdef EP_STATUS_NUM
|
1219 |
|
|
if (dev->status_ep) {
|
1220 |
|
|
usb_ep_disable (dev->status_ep);
|
1221 |
|
|
usb_ep_enable (dev->status_ep, dev->status);
|
1222 |
|
|
}
|
1223 |
|
|
#endif
|
1224 |
|
|
value = 0;
|
1225 |
|
|
break;
|
1226 |
|
|
case 1: /* data intf */
|
1227 |
|
|
if (ctrl->wValue > 1)
|
1228 |
|
|
break;
|
1229 |
|
|
usb_ep_disable (dev->in_ep);
|
1230 |
|
|
usb_ep_disable (dev->out_ep);
|
1231 |
|
|
|
1232 |
|
|
/* CDC requires the data transfers not be done from
|
1233 |
|
|
* the default interface setting ... also, setting
|
1234 |
|
|
* the non-default interface clears filters etc.
|
1235 |
|
|
*/
|
1236 |
|
|
if (ctrl->wValue == 1) {
|
1237 |
|
|
usb_ep_enable (dev->in_ep, dev->in);
|
1238 |
|
|
usb_ep_enable (dev->out_ep, dev->out);
|
1239 |
|
|
netif_carrier_on (dev->net);
|
1240 |
|
|
#ifdef EP_STATUS_NUM
|
1241 |
|
|
issue_start_status (dev);
|
1242 |
|
|
#endif
|
1243 |
|
|
if (netif_running (dev->net)) {
|
1244 |
|
|
spin_unlock (&dev->lock);
|
1245 |
|
|
eth_start (dev, GFP_ATOMIC);
|
1246 |
|
|
spin_lock (&dev->lock);
|
1247 |
|
|
}
|
1248 |
|
|
} else {
|
1249 |
|
|
netif_stop_queue (dev->net);
|
1250 |
|
|
netif_carrier_off (dev->net);
|
1251 |
|
|
}
|
1252 |
|
|
value = 0;
|
1253 |
|
|
break;
|
1254 |
|
|
}
|
1255 |
|
|
spin_unlock (&dev->lock);
|
1256 |
|
|
break;
|
1257 |
|
|
case USB_REQ_GET_INTERFACE:
|
1258 |
|
|
if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
|
1259 |
|
|
|| !dev->config
|
1260 |
|
|
|| ctrl->wIndex > 1)
|
1261 |
|
|
break;
|
1262 |
|
|
|
1263 |
|
|
/* if carrier is on, data interface is active. */
|
1264 |
|
|
*(u8 *)req->buf =
|
1265 |
|
|
((ctrl->wIndex == 1) && netif_carrier_ok (dev->net))
|
1266 |
|
|
? 1
|
1267 |
|
|
: 0,
|
1268 |
|
|
value = min (ctrl->wLength, (u16) 1);
|
1269 |
|
|
break;
|
1270 |
|
|
#endif
|
1271 |
|
|
|
1272 |
|
|
#ifdef DEV_CONFIG_CDC
|
1273 |
|
|
case CDC_SET_ETHERNET_PACKET_FILTER:
|
1274 |
|
|
/* see 6.2.30: no data, wIndex = interface,
|
1275 |
|
|
* wValue = packet filter bitmap
|
1276 |
|
|
*/
|
1277 |
|
|
if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
|
1278 |
|
|
|| ctrl->wLength != 0
|
1279 |
|
|
|| ctrl->wIndex > 1)
|
1280 |
|
|
DEBUG (dev, "NOP packet filter %04x\n", ctrl->wValue);
|
1281 |
|
|
/* NOTE: table 62 has 5 filter bits to reduce traffic,
|
1282 |
|
|
* and we "must" support multicast and promiscuous.
|
1283 |
|
|
* this NOP implements a bad filter...
|
1284 |
|
|
*/
|
1285 |
|
|
value = 0;
|
1286 |
|
|
break;
|
1287 |
|
|
#endif /* DEV_CONFIG_CDC */
|
1288 |
|
|
|
1289 |
|
|
default:
|
1290 |
|
|
VDEBUG (dev,
|
1291 |
|
|
"unknown control req%02x.%02x v%04x i%04x l%d\n",
|
1292 |
|
|
ctrl->bRequestType, ctrl->bRequest,
|
1293 |
|
|
ctrl->wValue, ctrl->wIndex, ctrl->wLength);
|
1294 |
|
|
}
|
1295 |
|
|
|
1296 |
|
|
/* respond with data transfer before status phase? */
|
1297 |
|
|
if (value >= 0) {
|
1298 |
|
|
req->length = value;
|
1299 |
|
|
value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
|
1300 |
|
|
if (value < 0) {
|
1301 |
|
|
DEBUG (dev, "ep_queue --> %d\n", value);
|
1302 |
|
|
req->status = 0;
|
1303 |
|
|
eth_setup_complete (gadget->ep0, req);
|
1304 |
|
|
}
|
1305 |
|
|
}
|
1306 |
|
|
|
1307 |
|
|
/* host either stalls (value < 0) or reports success */
|
1308 |
|
|
return value;
|
1309 |
|
|
}
|
1310 |
|
|
|
1311 |
|
|
static void
|
1312 |
|
|
eth_disconnect (struct usb_gadget *gadget)
|
1313 |
|
|
{
|
1314 |
|
|
struct eth_dev *dev = get_gadget_data (gadget);
|
1315 |
|
|
unsigned long flags;
|
1316 |
|
|
|
1317 |
|
|
spin_lock_irqsave (&dev->lock, flags);
|
1318 |
|
|
netif_stop_queue (dev->net);
|
1319 |
|
|
netif_carrier_off (dev->net);
|
1320 |
|
|
eth_reset_config (dev);
|
1321 |
|
|
spin_unlock_irqrestore (&dev->lock, flags);
|
1322 |
|
|
|
1323 |
|
|
/* next we may get setup() calls to enumerate new connections;
|
1324 |
|
|
* or an unbind() during shutdown (including removing module).
|
1325 |
|
|
*/
|
1326 |
|
|
}
|
1327 |
|
|
|
1328 |
|
|
/*-------------------------------------------------------------------------*/
|
1329 |
|
|
|
1330 |
|
|
/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
|
1331 |
|
|
|
1332 |
|
|
static int eth_change_mtu (struct net_device *net, int new_mtu)
|
1333 |
|
|
{
|
1334 |
|
|
struct eth_dev *dev = (struct eth_dev *) net->priv;
|
1335 |
|
|
|
1336 |
|
|
if (new_mtu <= MIN_PACKET || new_mtu > MAX_PACKET)
|
1337 |
|
|
return -ERANGE;
|
1338 |
|
|
/* no zero-length packet read wanted after mtu-sized packets */
|
1339 |
|
|
if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
|
1340 |
|
|
return -EDOM;
|
1341 |
|
|
net->mtu = new_mtu;
|
1342 |
|
|
return 0;
|
1343 |
|
|
}
|
1344 |
|
|
|
1345 |
|
|
static struct net_device_stats *eth_get_stats (struct net_device *net)
|
1346 |
|
|
{
|
1347 |
|
|
return &((struct eth_dev *) net->priv)->stats;
|
1348 |
|
|
}
|
1349 |
|
|
|
1350 |
|
|
static int eth_ethtool_ioctl (struct net_device *net, void *useraddr)
|
1351 |
|
|
{
|
1352 |
|
|
struct eth_dev *dev = (struct eth_dev *) net->priv;
|
1353 |
|
|
u32 cmd;
|
1354 |
|
|
|
1355 |
|
|
if (get_user (cmd, (u32 *)useraddr))
|
1356 |
|
|
return -EFAULT;
|
1357 |
|
|
switch (cmd) {
|
1358 |
|
|
|
1359 |
|
|
case ETHTOOL_GDRVINFO: { /* get driver info */
|
1360 |
|
|
struct ethtool_drvinfo info;
|
1361 |
|
|
|
1362 |
|
|
memset (&info, 0, sizeof info);
|
1363 |
|
|
info.cmd = ETHTOOL_GDRVINFO;
|
1364 |
|
|
strncpy (info.driver, shortname, sizeof info.driver);
|
1365 |
|
|
strncpy (info.version, DRIVER_VERSION, sizeof info.version);
|
1366 |
|
|
strncpy (info.fw_version, CHIP, sizeof info.fw_version);
|
1367 |
|
|
strncpy (info.bus_info, dev->gadget->dev.bus_id,
|
1368 |
|
|
sizeof info.bus_info);
|
1369 |
|
|
if (copy_to_user (useraddr, &info, sizeof (info)))
|
1370 |
|
|
return -EFAULT;
|
1371 |
|
|
return 0;
|
1372 |
|
|
}
|
1373 |
|
|
|
1374 |
|
|
case ETHTOOL_GLINK: { /* get link status */
|
1375 |
|
|
struct ethtool_value edata = { ETHTOOL_GLINK };
|
1376 |
|
|
|
1377 |
|
|
edata.data = (dev->gadget->speed != USB_SPEED_UNKNOWN);
|
1378 |
|
|
if (copy_to_user (useraddr, &edata, sizeof (edata)))
|
1379 |
|
|
return -EFAULT;
|
1380 |
|
|
return 0;
|
1381 |
|
|
}
|
1382 |
|
|
|
1383 |
|
|
}
|
1384 |
|
|
/* Note that the ethtool user space code requires EOPNOTSUPP */
|
1385 |
|
|
return -EOPNOTSUPP;
|
1386 |
|
|
}
|
1387 |
|
|
|
1388 |
|
|
static int eth_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
|
1389 |
|
|
{
|
1390 |
|
|
switch (cmd) {
|
1391 |
|
|
case SIOCETHTOOL:
|
1392 |
|
|
return eth_ethtool_ioctl (net, (void *)rq->ifr_data);
|
1393 |
|
|
default:
|
1394 |
|
|
return -EOPNOTSUPP;
|
1395 |
|
|
}
|
1396 |
|
|
}
|
1397 |
|
|
|
1398 |
|
|
static void defer_kevent (struct eth_dev *dev, int flag)
|
1399 |
|
|
{
|
1400 |
|
|
if (test_and_set_bit (flag, &dev->todo))
|
1401 |
|
|
return;
|
1402 |
|
|
if (!schedule_work (&dev->work))
|
1403 |
|
|
ERROR (dev, "kevent %d may have been dropped\n", flag);
|
1404 |
|
|
else
|
1405 |
|
|
DEBUG (dev, "kevent %d scheduled\n", flag);
|
1406 |
|
|
}
|
1407 |
|
|
|
1408 |
|
|
static void rx_complete (struct usb_ep *ep, struct usb_request *req);
|
1409 |
|
|
|
1410 |
|
|
static int
|
1411 |
|
|
rx_submit (struct eth_dev *dev, struct usb_request *req, int gfp_flags)
|
1412 |
|
|
{
|
1413 |
|
|
struct sk_buff *skb;
|
1414 |
|
|
int retval = -ENOMEM;
|
1415 |
|
|
size_t size;
|
1416 |
|
|
|
1417 |
|
|
size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
|
1418 |
|
|
|
1419 |
|
|
if ((skb = alloc_skb (size, gfp_flags)) == 0) {
|
1420 |
|
|
DEBUG (dev, "no rx skb\n");
|
1421 |
|
|
goto enomem;
|
1422 |
|
|
}
|
1423 |
|
|
|
1424 |
|
|
req->buf = skb->data;
|
1425 |
|
|
req->length = size;
|
1426 |
|
|
req->complete = rx_complete;
|
1427 |
|
|
req->context = skb;
|
1428 |
|
|
|
1429 |
|
|
retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
|
1430 |
|
|
if (retval == -ENOMEM)
|
1431 |
|
|
enomem:
|
1432 |
|
|
defer_kevent (dev, WORK_RX_MEMORY);
|
1433 |
|
|
if (retval) {
|
1434 |
|
|
DEBUG (dev, "rx submit --> %d\n", retval);
|
1435 |
|
|
dev_kfree_skb_any (skb);
|
1436 |
|
|
spin_lock (&dev->lock);
|
1437 |
|
|
list_add (&req->list, &dev->rx_reqs);
|
1438 |
|
|
spin_unlock (&dev->lock);
|
1439 |
|
|
}
|
1440 |
|
|
return retval;
|
1441 |
|
|
}
|
1442 |
|
|
|
1443 |
|
|
static void rx_complete (struct usb_ep *ep, struct usb_request *req)
|
1444 |
|
|
{
|
1445 |
|
|
struct sk_buff *skb = req->context;
|
1446 |
|
|
struct eth_dev *dev = ep->driver_data;
|
1447 |
|
|
int status = req->status;
|
1448 |
|
|
|
1449 |
|
|
switch (status) {
|
1450 |
|
|
|
1451 |
|
|
/* normal completion */
|
1452 |
|
|
case 0:
|
1453 |
|
|
skb_put (skb, req->actual);
|
1454 |
|
|
if (MIN_PACKET > skb->len
|
1455 |
|
|
|| skb->len > (MAX_PACKET + ETH_HLEN)) {
|
1456 |
|
|
dev->stats.rx_errors++;
|
1457 |
|
|
dev->stats.rx_length_errors++;
|
1458 |
|
|
DEBUG (dev, "rx length %d\n", skb->len);
|
1459 |
|
|
break;
|
1460 |
|
|
}
|
1461 |
|
|
|
1462 |
|
|
skb->dev = dev->net;
|
1463 |
|
|
skb->protocol = eth_type_trans (skb, dev->net);
|
1464 |
|
|
dev->stats.rx_packets++;
|
1465 |
|
|
dev->stats.rx_bytes += skb->len;
|
1466 |
|
|
|
1467 |
|
|
/* no buffer copies needed, unless hardware can't
|
1468 |
|
|
* use skb buffers.
|
1469 |
|
|
*/
|
1470 |
|
|
status = netif_rx (skb);
|
1471 |
|
|
skb = 0;
|
1472 |
|
|
break;
|
1473 |
|
|
|
1474 |
|
|
/* software-driven interface shutdown */
|
1475 |
|
|
case -ECONNRESET: // unlink
|
1476 |
|
|
case -ESHUTDOWN: // disconnect etc
|
1477 |
|
|
VDEBUG (dev, "rx shutdown, code %d\n", status);
|
1478 |
|
|
goto quiesce;
|
1479 |
|
|
|
1480 |
|
|
/* for hardware automagic (such as pxa) */
|
1481 |
|
|
case -ECONNABORTED: // endpoint reset
|
1482 |
|
|
DEBUG (dev, "rx %s reset\n", ep->name);
|
1483 |
|
|
defer_kevent (dev, WORK_RX_MEMORY);
|
1484 |
|
|
quiesce:
|
1485 |
|
|
dev_kfree_skb_any (skb);
|
1486 |
|
|
goto clean;
|
1487 |
|
|
|
1488 |
|
|
/* data overrun */
|
1489 |
|
|
case -EOVERFLOW:
|
1490 |
|
|
dev->stats.rx_over_errors++;
|
1491 |
|
|
// FALLTHROUGH
|
1492 |
|
|
|
1493 |
|
|
default:
|
1494 |
|
|
dev->stats.rx_errors++;
|
1495 |
|
|
DEBUG (dev, "rx status %d\n", status);
|
1496 |
|
|
break;
|
1497 |
|
|
}
|
1498 |
|
|
|
1499 |
|
|
if (skb)
|
1500 |
|
|
dev_kfree_skb_any (skb);
|
1501 |
|
|
if (!netif_running (dev->net)) {
|
1502 |
|
|
clean:
|
1503 |
|
|
/* nobody reading rx_reqs, so no dev->lock */
|
1504 |
|
|
list_add (&req->list, &dev->rx_reqs);
|
1505 |
|
|
req = 0;
|
1506 |
|
|
}
|
1507 |
|
|
if (req)
|
1508 |
|
|
rx_submit (dev, req, GFP_ATOMIC);
|
1509 |
|
|
}
|
1510 |
|
|
|
1511 |
|
|
static int prealloc (struct list_head *list, struct usb_ep *ep,
|
1512 |
|
|
unsigned n, int gfp_flags)
|
1513 |
|
|
{
|
1514 |
|
|
unsigned i;
|
1515 |
|
|
struct usb_request *req;
|
1516 |
|
|
|
1517 |
|
|
if (!n)
|
1518 |
|
|
return -ENOMEM;
|
1519 |
|
|
|
1520 |
|
|
/* queue/recycle up to N requests */
|
1521 |
|
|
i = n;
|
1522 |
|
|
list_for_each_entry (req, list, list) {
|
1523 |
|
|
if (i-- == 0)
|
1524 |
|
|
goto extra;
|
1525 |
|
|
}
|
1526 |
|
|
while (i--) {
|
1527 |
|
|
req = usb_ep_alloc_request (ep, gfp_flags);
|
1528 |
|
|
if (!req)
|
1529 |
|
|
return list_empty (list) ? -ENOMEM : 0;
|
1530 |
|
|
list_add (&req->list, list);
|
1531 |
|
|
}
|
1532 |
|
|
return 0;
|
1533 |
|
|
|
1534 |
|
|
extra:
|
1535 |
|
|
/* free extras */
|
1536 |
|
|
for (;;) {
|
1537 |
|
|
struct list_head *next;
|
1538 |
|
|
|
1539 |
|
|
next = req->list.next;
|
1540 |
|
|
list_del (&req->list);
|
1541 |
|
|
usb_ep_free_request (ep, req);
|
1542 |
|
|
|
1543 |
|
|
if (next == list)
|
1544 |
|
|
break;
|
1545 |
|
|
|
1546 |
|
|
req = container_of (next, struct usb_request, list);
|
1547 |
|
|
}
|
1548 |
|
|
return 0;
|
1549 |
|
|
}
|
1550 |
|
|
|
1551 |
|
|
static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags)
|
1552 |
|
|
{
|
1553 |
|
|
int status;
|
1554 |
|
|
|
1555 |
|
|
status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
|
1556 |
|
|
if (status < 0)
|
1557 |
|
|
goto fail;
|
1558 |
|
|
status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
|
1559 |
|
|
if (status < 0)
|
1560 |
|
|
goto fail;
|
1561 |
|
|
return 0;
|
1562 |
|
|
fail:
|
1563 |
|
|
DEBUG (dev, "can't alloc requests\n");
|
1564 |
|
|
return status;
|
1565 |
|
|
}
|
1566 |
|
|
|
1567 |
|
|
static void rx_fill (struct eth_dev *dev, int gfp_flags)
|
1568 |
|
|
{
|
1569 |
|
|
struct usb_request *req;
|
1570 |
|
|
unsigned long flags;
|
1571 |
|
|
|
1572 |
|
|
clear_bit (WORK_RX_MEMORY, &dev->todo);
|
1573 |
|
|
|
1574 |
|
|
/* fill unused rxq slots with some skb */
|
1575 |
|
|
spin_lock_irqsave (&dev->lock, flags);
|
1576 |
|
|
while (!list_empty (&dev->rx_reqs)) {
|
1577 |
|
|
req = container_of (dev->rx_reqs.next,
|
1578 |
|
|
struct usb_request, list);
|
1579 |
|
|
list_del_init (&req->list);
|
1580 |
|
|
spin_unlock_irqrestore (&dev->lock, flags);
|
1581 |
|
|
|
1582 |
|
|
if (rx_submit (dev, req, gfp_flags) < 0) {
|
1583 |
|
|
defer_kevent (dev, WORK_RX_MEMORY);
|
1584 |
|
|
return;
|
1585 |
|
|
}
|
1586 |
|
|
|
1587 |
|
|
spin_lock_irqsave (&dev->lock, flags);
|
1588 |
|
|
}
|
1589 |
|
|
spin_unlock_irqrestore (&dev->lock, flags);
|
1590 |
|
|
}
|
1591 |
|
|
|
1592 |
|
|
static void eth_work (void *_dev)
|
1593 |
|
|
{
|
1594 |
|
|
struct eth_dev *dev = _dev;
|
1595 |
|
|
|
1596 |
|
|
if (test_bit (WORK_RX_MEMORY, &dev->todo)) {
|
1597 |
|
|
if (netif_running (dev->net))
|
1598 |
|
|
rx_fill (dev, GFP_KERNEL);
|
1599 |
|
|
else
|
1600 |
|
|
clear_bit (WORK_RX_MEMORY, &dev->todo);
|
1601 |
|
|
}
|
1602 |
|
|
|
1603 |
|
|
if (dev->todo)
|
1604 |
|
|
DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
|
1605 |
|
|
}
|
1606 |
|
|
|
1607 |
|
|
static void tx_complete (struct usb_ep *ep, struct usb_request *req)
|
1608 |
|
|
{
|
1609 |
|
|
struct sk_buff *skb = req->context;
|
1610 |
|
|
struct eth_dev *dev = ep->driver_data;
|
1611 |
|
|
|
1612 |
|
|
switch (req->status) {
|
1613 |
|
|
default:
|
1614 |
|
|
dev->stats.tx_errors++;
|
1615 |
|
|
VDEBUG (dev, "tx err %d\n", req->status);
|
1616 |
|
|
/* FALLTHROUGH */
|
1617 |
|
|
case -ECONNRESET: // unlink
|
1618 |
|
|
case -ESHUTDOWN: // disconnect etc
|
1619 |
|
|
break;
|
1620 |
|
|
case 0:
|
1621 |
|
|
dev->stats.tx_bytes += skb->len;
|
1622 |
|
|
}
|
1623 |
|
|
dev->stats.tx_packets++;
|
1624 |
|
|
|
1625 |
|
|
spin_lock (&dev->lock);
|
1626 |
|
|
list_add (&req->list, &dev->tx_reqs);
|
1627 |
|
|
spin_unlock (&dev->lock);
|
1628 |
|
|
dev_kfree_skb_any (skb);
|
1629 |
|
|
|
1630 |
|
|
atomic_dec (&dev->tx_qlen);
|
1631 |
|
|
if (netif_carrier_ok (dev->net))
|
1632 |
|
|
netif_wake_queue (dev->net);
|
1633 |
|
|
}
|
1634 |
|
|
|
1635 |
|
|
static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
|
1636 |
|
|
{
|
1637 |
|
|
struct eth_dev *dev = (struct eth_dev *) net->priv;
|
1638 |
|
|
int length = skb->len;
|
1639 |
|
|
int retval;
|
1640 |
|
|
struct usb_request *req = 0;
|
1641 |
|
|
unsigned long flags;
|
1642 |
|
|
|
1643 |
|
|
spin_lock_irqsave (&dev->lock, flags);
|
1644 |
|
|
req = container_of (dev->tx_reqs.next, struct usb_request, list);
|
1645 |
|
|
list_del (&req->list);
|
1646 |
|
|
if (list_empty (&dev->tx_reqs))
|
1647 |
|
|
netif_stop_queue (net);
|
1648 |
|
|
spin_unlock_irqrestore (&dev->lock, flags);
|
1649 |
|
|
|
1650 |
|
|
/* no buffer copies needed, unless the network stack did it
|
1651 |
|
|
* or the hardware can't use skb buffers.
|
1652 |
|
|
*/
|
1653 |
|
|
req->buf = skb->data;
|
1654 |
|
|
req->context = skb;
|
1655 |
|
|
req->complete = tx_complete;
|
1656 |
|
|
|
1657 |
|
|
#ifdef CONFIG_USB_GADGET_SA1100
|
1658 |
|
|
/* don't demand zlp (req->zero) support from all hardware */
|
1659 |
|
|
if ((length % dev->in_ep->maxpacket) == 0)
|
1660 |
|
|
length++;
|
1661 |
|
|
#else
|
1662 |
|
|
/* use zlp framing on tx for strict CDC-Ether conformance,
|
1663 |
|
|
* though any robust network rx path ignores extra padding.
|
1664 |
|
|
*/
|
1665 |
|
|
req->zero = 1;
|
1666 |
|
|
#endif
|
1667 |
|
|
req->length = length;
|
1668 |
|
|
|
1669 |
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
1670 |
|
|
/* throttle highspeed IRQ rate back slightly */
|
1671 |
|
|
req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
|
1672 |
|
|
? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
|
1673 |
|
|
: 0;
|
1674 |
|
|
#endif
|
1675 |
|
|
|
1676 |
|
|
retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
|
1677 |
|
|
switch (retval) {
|
1678 |
|
|
default:
|
1679 |
|
|
DEBUG (dev, "tx queue err %d\n", retval);
|
1680 |
|
|
break;
|
1681 |
|
|
case 0:
|
1682 |
|
|
net->trans_start = jiffies;
|
1683 |
|
|
atomic_inc (&dev->tx_qlen);
|
1684 |
|
|
}
|
1685 |
|
|
|
1686 |
|
|
if (retval) {
|
1687 |
|
|
dev->stats.tx_dropped++;
|
1688 |
|
|
dev_kfree_skb_any (skb);
|
1689 |
|
|
spin_lock_irqsave (&dev->lock, flags);
|
1690 |
|
|
if (list_empty (&dev->tx_reqs))
|
1691 |
|
|
netif_start_queue (net);
|
1692 |
|
|
list_add (&req->list, &dev->tx_reqs);
|
1693 |
|
|
spin_unlock_irqrestore (&dev->lock, flags);
|
1694 |
|
|
}
|
1695 |
|
|
return 0;
|
1696 |
|
|
}
|
1697 |
|
|
|
1698 |
|
|
static void eth_start (struct eth_dev *dev, int gfp_flags)
|
1699 |
|
|
{
|
1700 |
|
|
DEBUG (dev, "%s\n", __FUNCTION__);
|
1701 |
|
|
|
1702 |
|
|
/* fill the rx queue */
|
1703 |
|
|
rx_fill (dev, gfp_flags);
|
1704 |
|
|
|
1705 |
|
|
/* and open the tx floodgates */
|
1706 |
|
|
atomic_set (&dev->tx_qlen, 0);
|
1707 |
|
|
netif_wake_queue (dev->net);
|
1708 |
|
|
}
|
1709 |
|
|
|
1710 |
|
|
static int eth_open (struct net_device *net)
|
1711 |
|
|
{
|
1712 |
|
|
struct eth_dev *dev = (struct eth_dev *) net->priv;
|
1713 |
|
|
|
1714 |
|
|
DEBUG (dev, "%s\n", __FUNCTION__);
|
1715 |
|
|
if (netif_carrier_ok (dev->net))
|
1716 |
|
|
eth_start (dev, GFP_KERNEL);
|
1717 |
|
|
return 0;
|
1718 |
|
|
}
|
1719 |
|
|
|
1720 |
|
|
static int eth_stop (struct net_device *net)
|
1721 |
|
|
{
|
1722 |
|
|
struct eth_dev *dev = (struct eth_dev *) net->priv;
|
1723 |
|
|
|
1724 |
|
|
VDEBUG (dev, "%s\n", __FUNCTION__);
|
1725 |
|
|
netif_stop_queue (net);
|
1726 |
|
|
|
1727 |
|
|
DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
|
1728 |
|
|
dev->stats.rx_packets, dev->stats.tx_packets,
|
1729 |
|
|
dev->stats.rx_errors, dev->stats.tx_errors
|
1730 |
|
|
);
|
1731 |
|
|
|
1732 |
|
|
/* ensure there are no more active requests */
|
1733 |
|
|
if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
|
1734 |
|
|
usb_ep_disable (dev->in_ep);
|
1735 |
|
|
usb_ep_disable (dev->out_ep);
|
1736 |
|
|
if (netif_carrier_ok (dev->net)) {
|
1737 |
|
|
DEBUG (dev, "host still using in/out endpoints\n");
|
1738 |
|
|
// FIXME idiom may leave toggle wrong here
|
1739 |
|
|
usb_ep_enable (dev->in_ep, dev->in);
|
1740 |
|
|
usb_ep_enable (dev->out_ep, dev->out);
|
1741 |
|
|
}
|
1742 |
|
|
#ifdef EP_STATUS_NUM
|
1743 |
|
|
usb_ep_disable (dev->status_ep);
|
1744 |
|
|
usb_ep_enable (dev->status_ep, dev->status);
|
1745 |
|
|
#endif
|
1746 |
|
|
}
|
1747 |
|
|
|
1748 |
|
|
return 0;
|
1749 |
|
|
}
|
1750 |
|
|
|
1751 |
|
|
/*-------------------------------------------------------------------------*/
|
1752 |
|
|
|
1753 |
|
|
static void
|
1754 |
|
|
eth_unbind (struct usb_gadget *gadget)
|
1755 |
|
|
{
|
1756 |
|
|
struct eth_dev *dev = get_gadget_data (gadget);
|
1757 |
|
|
|
1758 |
|
|
DEBUG (dev, "unbind\n");
|
1759 |
|
|
|
1760 |
|
|
/* we've already been disconnected ... no i/o is active */
|
1761 |
|
|
if (dev->req) {
|
1762 |
|
|
usb_ep_free_buffer (gadget->ep0,
|
1763 |
|
|
dev->req->buf, dev->req->dma,
|
1764 |
|
|
USB_BUFSIZ);
|
1765 |
|
|
usb_ep_free_request (gadget->ep0, dev->req);
|
1766 |
|
|
dev->req = 0;
|
1767 |
|
|
}
|
1768 |
|
|
|
1769 |
|
|
unregister_netdev (dev->net);
|
1770 |
|
|
dev_put (dev->net);
|
1771 |
|
|
|
1772 |
|
|
/* assuming we used keventd, it must quiesce too */
|
1773 |
|
|
flush_scheduled_work ();
|
1774 |
|
|
set_gadget_data (gadget, 0);
|
1775 |
|
|
}
|
1776 |
|
|
|
1777 |
|
|
static int
|
1778 |
|
|
eth_bind (struct usb_gadget *gadget)
|
1779 |
|
|
{
|
1780 |
|
|
struct eth_dev *dev;
|
1781 |
|
|
struct net_device *net;
|
1782 |
|
|
int status = -ENOMEM;
|
1783 |
|
|
#ifdef DEV_CONFIG_CDC
|
1784 |
|
|
u8 node_id [ETH_ALEN];
|
1785 |
|
|
|
1786 |
|
|
/* just one upstream link at a time */
|
1787 |
|
|
if (ethaddr [0] != 0)
|
1788 |
|
|
return -ENODEV;
|
1789 |
|
|
#endif
|
1790 |
|
|
|
1791 |
|
|
device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
|
1792 |
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
1793 |
|
|
/* assumes ep0 uses the same value for both speeds ... */
|
1794 |
|
|
dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
|
1795 |
|
|
#endif
|
1796 |
|
|
|
1797 |
|
|
#ifdef SELFPOWERED
|
1798 |
|
|
eth_config.bmAttributes |= USB_CONFIG_ATT_SELFPOWERED;
|
1799 |
|
|
usb_gadget_set_selfpowered (gadget);
|
1800 |
|
|
#endif
|
1801 |
|
|
|
1802 |
|
|
net = alloc_etherdev (sizeof *dev);
|
1803 |
|
|
if (!net)
|
1804 |
|
|
return status;
|
1805 |
|
|
dev = net->priv;
|
1806 |
|
|
spin_lock_init (&dev->lock);
|
1807 |
|
|
INIT_WORK (&dev->work, eth_work, dev);
|
1808 |
|
|
INIT_LIST_HEAD (&dev->tx_reqs);
|
1809 |
|
|
INIT_LIST_HEAD (&dev->rx_reqs);
|
1810 |
|
|
|
1811 |
|
|
/* network device setup */
|
1812 |
|
|
dev->net = net;
|
1813 |
|
|
SET_MODULE_OWNER (net);
|
1814 |
|
|
strcpy (net->name, "usb%d");
|
1815 |
|
|
|
1816 |
|
|
/* one random address for the gadget device ... both of these could
|
1817 |
|
|
* reasonably come from an id prom or a module parameter.
|
1818 |
|
|
*/
|
1819 |
|
|
get_random_bytes (net->dev_addr, ETH_ALEN);
|
1820 |
|
|
net->dev_addr [0] &= 0xfe; // clear multicast bit
|
1821 |
|
|
net->dev_addr [0] |= 0x02; // set local assignment bit (IEEE802)
|
1822 |
|
|
|
1823 |
|
|
#ifdef DEV_CONFIG_CDC
|
1824 |
|
|
/* ... another address for the host, on the other end of the
|
1825 |
|
|
* link, gets exported through CDC (see CDC spec table 41)
|
1826 |
|
|
*/
|
1827 |
|
|
get_random_bytes (node_id, sizeof node_id);
|
1828 |
|
|
node_id [0] &= 0xfe; // clear multicast bit
|
1829 |
|
|
node_id [0] |= 0x02; // set local assignment bit (IEEE802)
|
1830 |
|
|
snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
|
1831 |
|
|
node_id [0], node_id [1], node_id [2],
|
1832 |
|
|
node_id [3], node_id [4], node_id [5]);
|
1833 |
|
|
#endif
|
1834 |
|
|
|
1835 |
|
|
net->change_mtu = eth_change_mtu;
|
1836 |
|
|
net->get_stats = eth_get_stats;
|
1837 |
|
|
net->hard_start_xmit = eth_start_xmit;
|
1838 |
|
|
net->open = eth_open;
|
1839 |
|
|
net->stop = eth_stop;
|
1840 |
|
|
// watchdog_timeo, tx_timeout ...
|
1841 |
|
|
// set_multicast_list
|
1842 |
|
|
net->do_ioctl = eth_ioctl;
|
1843 |
|
|
|
1844 |
|
|
/* preallocate control response and buffer */
|
1845 |
|
|
dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
|
1846 |
|
|
if (!dev->req)
|
1847 |
|
|
goto fail;
|
1848 |
|
|
dev->req->complete = eth_setup_complete;
|
1849 |
|
|
dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
|
1850 |
|
|
&dev->req->dma, GFP_KERNEL);
|
1851 |
|
|
if (!dev->req->buf) {
|
1852 |
|
|
usb_ep_free_request (gadget->ep0, dev->req);
|
1853 |
|
|
goto fail;
|
1854 |
|
|
}
|
1855 |
|
|
|
1856 |
|
|
/* finish hookup to lower layer ... */
|
1857 |
|
|
dev->gadget = gadget;
|
1858 |
|
|
set_gadget_data (gadget, dev);
|
1859 |
|
|
gadget->ep0->driver_data = dev;
|
1860 |
|
|
|
1861 |
|
|
/* two kinds of host-initiated state changes:
|
1862 |
|
|
* - iff DATA transfer is active, carrier is "on"
|
1863 |
|
|
* - tx queueing enabled if open *and* carrier is "on"
|
1864 |
|
|
*/
|
1865 |
|
|
netif_stop_queue (dev->net);
|
1866 |
|
|
netif_carrier_off (dev->net);
|
1867 |
|
|
|
1868 |
|
|
// SET_NETDEV_DEV (dev->net, &gadget->dev);
|
1869 |
|
|
status = register_netdev (dev->net);
|
1870 |
|
|
if (status == 0) {
|
1871 |
|
|
|
1872 |
|
|
INFO (dev, "%s, " CHIP ", version: " DRIVER_VERSION "\n",
|
1873 |
|
|
driver_desc);
|
1874 |
|
|
#ifdef DEV_CONFIG_CDC
|
1875 |
|
|
INFO (dev, "CDC host enet %s\n", ethaddr);
|
1876 |
|
|
#endif
|
1877 |
|
|
return status;
|
1878 |
|
|
}
|
1879 |
|
|
pr_debug("%s: register_netdev failed, %d\n", shortname, status);
|
1880 |
|
|
fail:
|
1881 |
|
|
eth_unbind (gadget);
|
1882 |
|
|
return status;
|
1883 |
|
|
}
|
1884 |
|
|
|
1885 |
|
|
/*-------------------------------------------------------------------------*/
|
1886 |
|
|
|
1887 |
|
|
static struct usb_gadget_driver eth_driver = {
|
1888 |
|
|
#ifdef CONFIG_USB_GADGET_DUALSPEED
|
1889 |
|
|
.speed = USB_SPEED_HIGH,
|
1890 |
|
|
#else
|
1891 |
|
|
.speed = USB_SPEED_FULL,
|
1892 |
|
|
#endif
|
1893 |
|
|
.function = (char *) driver_desc,
|
1894 |
|
|
.bind = eth_bind,
|
1895 |
|
|
.unbind = eth_unbind,
|
1896 |
|
|
|
1897 |
|
|
.setup = eth_setup,
|
1898 |
|
|
.disconnect = eth_disconnect,
|
1899 |
|
|
|
1900 |
|
|
.driver = {
|
1901 |
|
|
.name = (char *) shortname,
|
1902 |
|
|
// .shutdown = ...
|
1903 |
|
|
// .suspend = ...
|
1904 |
|
|
// .resume = ...
|
1905 |
|
|
},
|
1906 |
|
|
};
|
1907 |
|
|
|
1908 |
|
|
MODULE_DESCRIPTION (DRIVER_DESC);
|
1909 |
|
|
MODULE_AUTHOR ("David Brownell");
|
1910 |
|
|
MODULE_LICENSE ("GPL");
|
1911 |
|
|
|
1912 |
|
|
|
1913 |
|
|
static int __init init (void)
|
1914 |
|
|
{
|
1915 |
|
|
return usb_gadget_register_driver (ð_driver);
|
1916 |
|
|
}
|
1917 |
|
|
module_init (init);
|
1918 |
|
|
|
1919 |
|
|
static void __exit cleanup (void)
|
1920 |
|
|
{
|
1921 |
|
|
usb_gadget_unregister_driver (ð_driver);
|
1922 |
|
|
}
|
1923 |
|
|
module_exit (cleanup);
|
1924 |
|
|
|