ti-ocf-crypto-module: add crypto module for hw accel
[openembedded.git] / recipes / linux / files / linux-2.4-usb-gadget.patch
blob0864ee98f54605b558a8f2639af1f55e7e12e998
1 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/Documentation/Configure.help kernel/Documentation/Configure.help
2 --- /tmp/kernel/Documentation/Configure.help 2005-04-22 17:52:12.265476882 +0200
3 +++ kernel/Documentation/Configure.help 2005-04-22 17:57:15.940717930 +0200
4 @@ -23701,6 +23701,163 @@
5 brave people. System crashes and other bad things are likely to occur if
6 you use this driver. If in doubt, select N.
8 +CONFIG_USB_GADGET
9 + USB is a master/slave protocol, organized with one master
10 + host (such as a PC) controlling up to 127 peripheral devices.
11 + The USB hardware is asymmetric, which makes it easier to set up:
12 + you can't connect two "to-the-host" connectors to each other.
14 + Linux can run in the host, or in the peripheral. In both cases
15 + you need a low level bus controller driver, and some software
16 + talking to it. Peripheral controllers are often discrete silicon,
17 + or are integrated with the CPU in a microcontroller. The more
18 + familiar host side controllers have names like like "EHCI", "OHCI",
19 + or "UHCI", and are usually integrated into southbridges on PC
20 + motherboards.
22 + Enable this configuration option if you want to run Linux inside
23 + a USB peripheral device. Configure one hardware driver for your
24 + peripheral/device side bus controller, and a "gadget driver" for
25 + your peripheral protocol. (If you use modular gadget drivers,
26 + you may configure more than one.)
28 + If in doubt, say "N" and don't enable these drivers; most people
29 + don't have this kind of hardware (except maybe inside Linux PDAs).
31 +CONFIG_USB_GADGET_NET2280
32 + NetChip 2280 is a PCI based USB peripheral controller which
33 + supports both full and high speed USB 2.0 data transfers.
35 + It has six configurable endpoints, as well as endpoint zero
36 + (for control transfers) and several endpoints with dedicated
37 + functions.
39 + Say "y" to link the driver statically, or "m" to build a
40 + dynamically linked module called "net2280" and force all
41 + gadget drivers to also be dynamically linked.
43 +CONFIG_USB_GADGET_GOKU
44 + The Toshiba TC86C001 is a PCI device which includes controllers
45 + for full speed USB devices, IDE, I2C, SIO, plus a USB host (OHCI).
47 + The device controller has three configurable (bulk or interrupt)
48 + endpoints, plus endpoint zero (for control transfers).
50 + Say "y" to link the driver statically, or "m" to build a
51 + dynamically linked module called "goku_udc" and force all
52 + gadget drivers to also be dynamically linked.
54 +CONFIG_USB_GADGET_PXA2XX
55 + Intel's PXA 2xx series XScale ARM-5TE processors include
56 + an integrated full speed USB 1.1 device controller.
58 + It has fifteen fixed-function endpoints, as well as endpoint
59 + zero (for control transfers).
61 + Say "y" to link the driver statically, or "m" to build a
62 + dynamically linked module called "pxa2xx_udc" and force all
63 + gadget drivers to also be dynamically linked.
65 +CONFIG_USB_GADGET_SUPERH
66 + Some Renesas SuperH processors (SH7705, SH7727...) include an
67 + integrated high speed USB 1.1 device controller.
69 + It has three fixed-function endpoints, as well as endpoint zero (for
70 + control transfers).
72 + Say "y" to link the driver statically, or "m" to build a
73 + dynamically linked module called "superh_udc" and force all
74 + gadget drivers to also be dynamically linked.
76 +CONFIG_USB_ZERO
77 + Gadget Zero is a two-configuration device. It either sinks and
78 + sources bulk data; or it loops back a configurable number of
79 + transfers. It also implements control requests, for "chapter 9"
80 + conformance. The driver needs only two bulk-capable endpoints, so
81 + it can work on top of most device-side usb controllers. It's
82 + useful for testing, and is also a working example showing how
83 + USB "gadget drivers" can be written.
85 + Make this be the first driver you try using on top of any new
86 + USB peripheral controller driver. Then you can use host-side
87 + test software, like the "usbtest" driver, to put your hardware
88 + and its driver through a basic set of functional tests.
90 + Gadget Zero also works with the host-side "usb-skeleton" driver,
91 + and with many kinds of host-side test software. You may need
92 + to tweak product and vendor IDs before host software knows about
93 + this device, and arrange to select an appropriate configuration.
95 + Say "y" to link the driver statically, or "m" to build a
96 + dynamically linked module called "g_zero".
98 +CONFIG_USB_ETH
99 + This driver implements Ethernet style communication, in either
100 + of two ways:
102 + - The "Communication Device Class" (CDC) Ethernet Control Model.
103 + That protocol is often avoided with pure Ethernet adapters, in
104 + favor of simpler vendor-specific hardware, but is widely
105 + supported by firmware for smart network devices.
107 + - On hardware can't implement that protocol, a simpler approach
108 + is used, placing fewer demands on USB.
110 + Within the USB device, this gadget driver exposes a network device
111 + "usbX", where X depends on what other networking devices you have.
112 + Treat it like a two-node Ethernet link: host, and gadget.
114 + The Linux-USB host-side "usbnet" driver interoperates with this
115 + driver, so that deep I/O queues can be supported. On 2.4 kernels,
116 + use "CDCEther" instead, if you're using the CDC option. That CDC
117 + mode should also interoperate with standard CDC Ethernet class
118 + drivers on other host operating systems.
120 + Say "y" to link the driver statically, or "m" to build a
121 + dynamically linked module called "g_ether".
123 +CONFIG_USB_ETH_RNDIS
124 + Microsoft Windows XP bundles the "Remote NDIS" (RNDIS) protocol,
125 + and Microsoft provides redistributable binary RNDIS drivers for
126 + older versions of Windows.
128 + If you say "y" here, the Ethernet gadget driver will try to provide
129 + a second device configuration, supporting RNDIS to talk to such
130 + Microsoft USB hosts.
132 +CONFIG_USB_FILE_STORAGE
133 + The File-backed Storage Gadget acts as a USB Mass Storage
134 + disk drive. As its storage repository it can use a regular
135 + file or a block device (in much the same way as the "loop"
136 + device driver), specified as a module parameter.
138 +CONFIG_USB_FILE_STORAGE_TEST
139 + Say "y" to generate the larger testing version of the
140 + File-backed Storage Gadget, useful for probing the
141 + behavior of USB Mass Storage hosts. Not needed for
142 + normal operation.
144 +CONFIG_USB_ETH_RNDIS
145 + Microsoft Windows XP bundles the "Remote NDIS" (RNDIS) protocol,
146 + and Microsoft provides redistributable binary RNDIS drivers for
147 + older versions of Windows.
149 + If you say "y" here, the Ethernet gadget driver will try to provide
150 + a second device configuration, supporting RNDIS to talk to such
151 + Microsoft USB hosts.
153 +CONFIG_USB_FILE_STORAGE
154 + The File-backed Storage Gadget acts as a USB Mass Storage
155 + disk drive. As its storage repository it can use a regular
156 + file or a block device (in much the same way as the "loop"
157 + device driver), specified as a module parameter.
159 +CONFIG_USB_FILE_STORAGE_TEST
160 + Say "y" to generate the larger testing version of the
161 + File-backed Storage Gadget, useful for probing the
162 + behavior of USB Mass Storage hosts. Not needed for
163 + normal operation.
165 Winbond W83977AF IrDA Device Driver
166 CONFIG_WINBOND_FIR
167 Say Y here if you want to build IrDA support for the Winbond
168 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/Makefile kernel/Makefile
169 --- /tmp/kernel/Makefile 2005-04-22 17:52:12.362461090 +0200
170 +++ kernel/Makefile 2005-04-22 17:53:19.374549284 +0200
171 @@ -196,6 +196,7 @@
172 DRIVERS-$(CONFIG_HAMRADIO) += drivers/net/hamradio/hamradio.o
173 DRIVERS-$(CONFIG_TC) += drivers/tc/tc.a
174 DRIVERS-$(CONFIG_USB) += drivers/usb/usbdrv.o
175 +DRIVERS-$(CONFIG_USB_GADGET) += drivers/usb/gadget/built-in.o
176 DRIVERS-$(CONFIG_LAB) += drivers/bootldr/labmod.o
177 DRIVERS-$(CONFIG_INPUT) += drivers/input/inputdrv.o
178 DRIVERS-$(CONFIG_I2O) += drivers/message/i2o/i2o.o
179 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/Makefile kernel/drivers/Makefile
180 --- /tmp/kernel/drivers/Makefile 2005-04-22 17:52:12.728401503 +0200
181 +++ kernel/drivers/Makefile 2005-04-22 17:53:19.523525026 +0200
182 @@ -27,6 +27,7 @@
183 subdir-$(CONFIG_MAC) += macintosh
184 subdir-$(CONFIG_PPC) += macintosh
185 subdir-$(CONFIG_USB) += usb
186 +subdir-$(CONFIG_USB_GADGET) += usb/gadget
187 subdir-$(CONFIG_INPUT) += input
188 subdir-$(CONFIG_PHONE) += telephony
189 subdir-$(CONFIG_SGI) += sgi
190 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/Config.in kernel/drivers/usb/Config.in
191 --- /tmp/kernel/drivers/usb/Config.in 2005-04-22 17:52:20.663109467 +0200
192 +++ kernel/drivers/usb/Config.in 2005-04-22 17:53:19.376548959 +0200
193 @@ -120,4 +120,7 @@
194 dep_tristate ' USB Auerswald ISDN support (EXPERIMENTAL)' CONFIG_USB_AUERSWALD $CONFIG_USB $CONFIG_EXPERIMENTAL
195 dep_tristate ' Tieman Voyager USB Braille display support (EXPERIMENTAL)' CONFIG_USB_BRLVGER $CONFIG_USB $CONFIG_EXPERIMENTAL
198 +source drivers/usb/gadget/Config.in
200 endmenu
201 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/Config.in kernel/drivers/usb/gadget/Config.in
202 --- /tmp/kernel/drivers/usb/gadget/Config.in 1970-01-01 01:00:00.000000000 +0100
203 +++ kernel/drivers/usb/gadget/Config.in 2005-04-22 17:53:19.403544563 +0200
204 @@ -0,0 +1,128 @@
206 +# USB device-side configuration
207 +# for 2.4 kbuild, drivers/usb/gadget/Config.in
209 +# Long term, this likely doesn't all belong in one directory
210 +# Plan to split it up eventually.
212 +mainmenu_option next_comment
213 +comment 'Support for USB gadgets'
215 +tristate 'Support for USB Gadgets' CONFIG_USB_GADGET
216 +if [ "$CONFIG_USB_GADGET" = "y" -o "$CONFIG_USB_GADGET" = "m" ]; then
219 + # really want _exactly one_ device controller driver at a time,
220 + # since they control compile options for gadget drivers.
222 + choice 'USB Peripheral Controller Driver' "\
223 + Intel-PXA2xx/IXP4xx CONFIG_USB_GADGET_PXA2XX \
224 + National-N9603/N9604 CONFIG_USB_GADGET_N9604 \
225 + NetChip-2280 CONFIG_USB_GADGET_NET2280 \
226 + Renesas-SH7705/7727 CONFIG_USB_GADGET_SUPERH \
227 + Toshiba-TC86C001(Goku-S) CONFIG_USB_GADGET_GOKU \
228 + " NetChip-2280
230 + define_tristate CONFIG_USB_GADGET_CONTROLLER n
232 + if [ "$CONFIG_ARCH_PXA" = "y" -o "$CONFIG_ARCH_IXP425" = "y" ] ; then
233 + if [ "$CONFIG_USB_GADGET_PXA2XX" = "y" ] ; then
234 + define_tristate CONFIG_USB_PXA2XX $CONFIG_USB_GADGET
235 + define_tristate CONFIG_USB_GADGET_CONTROLLER $CONFIG_USB_PXA2XX
236 + fi
237 + fi
238 + if [ "$CONFIG_PCI" = "y" -a "$CONFIG_USB_GADGET_NET2280" = "y" ] ; then
239 + define_tristate CONFIG_USB_NET2280 $CONFIG_USB_GADGET
240 + define_tristate CONFIG_USB_GADGET_CONTROLLER $CONFIG_USB_NET2280
241 + fi
242 + if [ "$CONFIG_SUPERH" = "y" -a "$CONFIG_USB_GADGET_SUPERH" = "y" ] ; then
243 + define_tristate CONFIG_USB_SUPERH $CONFIG_USB_GADGET
244 + define_tristate CONFIG_USB_GADGET_CONTROLLER $CONFIG_USB_SUPERH
245 + fi
246 + if [ "$CONFIG_PCI" = "y" -a "$CONFIG_USB_GADGET_GOKU" = "y" ] ; then
247 + define_tristate CONFIG_USB_GOKU $CONFIG_USB_GADGET
248 + define_tristate CONFIG_USB_GADGET_CONTROLLER $CONFIG_USB_GOKU
249 + fi
250 + if [ "$CONFIG_USB_GADGET_N9604" = "y" ] ; then
251 + define_tristate CONFIG_USB_N9604 $CONFIG_USB_GADGET
252 + define_tristate CONFIG_USB_GADGET_CONTROLLER $CONFIG_USB_N9604
253 + fi
255 + # or any other controller that supports high speed transfers ...
256 + define_bool CONFIG_USB_GADGET_DUALSPEED $CONFIG_USB_GADGET_NET2280
258 + if [ "$CONFIG_USB_GADGET_CONTROLLER" = "y" -o "$CONFIG_USB_GADGET_CONTROLLER" = "m" ] ; then
261 + # no reason not to enable more than one gadget driver module, but
262 + # for static linking that would make no sense since the usb model
263 + # has exactly one of these upstream connections and only one
264 + # lowest-level driver can control it.
266 + # gadget drivers are compiled to work on specific hardware, since
268 + # (a) gadget driver need hardware-specific configuration, like what
269 + # endpoint names and numbers to use, maxpacket sizes, etc
271 + # (b) specific hardware features like iso endpoints may be required
273 + comment 'USB Gadget Drivers'
275 + # FIXME when drivers all use #ifdef CONFIG_USB_GADGET_* tests,
276 + # just remove all this driver-specific define_bool logic
278 + dep_tristate ' Gadget Zero (DEVELOPMENT)' CONFIG_USB_ZERO $CONFIG_USB_GADGET_CONTROLLER
279 + dep_tristate ' Ethernet Gadget (EXPERIMENTAL)' CONFIG_USB_ETH $CONFIG_USB_GADGET_CONTROLLER $CONFIG_NET
280 + if [ "$CONFIG_USB_ETH" = "y" -o "$CONFIG_USB_ETH" = "m" ] ; then
281 + bool ' RNDIS support (EXPERIMENTAL)' CONFIG_USB_ETH_RNDIS
282 + fi
283 + dep_tristate ' Gadget Filesystem API (EXPERIMENTAL)' CONFIG_USB_GADGETFS $CONFIG_USB_GADGET_CONTROLLER
284 + dep_tristate ' File-backed Storage Gadget (DEVELOPMENT)' CONFIG_USB_FILE_STORAGE $CONFIG_USB_GADGET_CONTROLLER
285 + dep_mbool ' File-backed Storage Gadget test mode' CONFIG_USB_FILE_STORAGE_TEST $CONFIG_USB_FILE_STORAGE
286 + dep_tristate ' Serial Gadget (EXPERIMENTAL)' CONFIG_USB_G_SERIAL $CONFIG_USB_GADGET_CONTROLLER
289 + # enforce the "only one statically linked gadget driver" rule
291 + if [ "$CONFIG_USB_ZERO" = "y" ]; then
292 + # zero = y
293 + define_tristate CONFIG_USB_ETH n
294 + define_tristate CONFIG_USB_GADGETFS n
295 + define_tristate CONFIG_USB_FILE_STORAGE n
296 + define_tristate CONFIG_USB_G_SERIAL n
297 + fi
299 + if [ "$CONFIG_USB_ETH" = "y" ]; then
300 + define_tristate CONFIG_USB_ZERO n
301 + # eth = y
302 + define_tristate CONFIG_USB_GADGETFS n
303 + define_tristate CONFIG_USB_FILE_STORAGE n
304 + define_tristate CONFIG_USB_G_SERIAL n
305 + fi
307 + if [ "$CONFIG_USB_GADGETFS" = "y" ]; then
308 + define_tristate CONFIG_USB_ZERO n
309 + define_tristate CONFIG_USB_ETH n
310 + # gadgetfs = y
311 + define_tristate CONFIG_USB_FILE_STORAGE n
312 + define_tristate CONFIG_USB_G_SERIAL n
313 + fi
315 + if [ "$CONFIG_USB_FILE_STORAGE" = "y" ]; then
316 + define_tristate CONFIG_USB_ZERO n
317 + define_tristate CONFIG_USB_ETH n
318 + define_tristate CONFIG_USB_GADGETFS n
319 + # file_storage = y
320 + define_tristate CONFIG_USB_G_SERIAL n
321 + fi
323 + if [ "$CONFIG_USB_G_SERIAL" = "y" ]; then
324 + define_tristate CONFIG_USB_ZERO n
325 + define_tristate CONFIG_USB_ETH n
326 + define_tristate CONFIG_USB_GADGETFS n
327 + define_tristate CONFIG_USB_FILE_STORAGE n
328 + # g_serial = y
329 + fi
330 + fi
332 +endmenu
333 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/Makefile kernel/drivers/usb/gadget/Makefile
334 --- /tmp/kernel/drivers/usb/gadget/Makefile 1970-01-01 01:00:00.000000000 +0100
335 +++ kernel/drivers/usb/gadget/Makefile 2005-04-22 17:53:19.405544237 +0200
336 @@ -0,0 +1,58 @@
338 +# Makefile for USB peripheral controller and gadget drivers
339 +# for kbuild 2.4
342 +# for static linking
343 +O_TARGET := built-in.o
345 +list-multi := g_zero.o g_ether.o gadgetfs.o g_file_storage.o g_serial.o
347 +obj-$(CONFIG_USB_NET2280) += net2280.o
348 +obj-$(CONFIG_USB_PXA2XX) += pxa2xx_udc.o
349 +obj-$(CONFIG_USB_GOKU) += goku_udc.o
350 +obj-$(CONFIG_USB_SUPERH) += superh_udc.o
351 +obj-$(CONFIG_USB_N9604) += n9604.o
353 +# only one of these may be statically linked ...
354 +controller-$(CONFIG_USB_NET2280) += net2280.o
355 +controller-$(CONFIG_USB_PXA2XX) += pxa2xx_udc.o
356 +controller-$(CONFIG_USB_GOKU) += goku_udc.o
357 +controller-$(CONFIG_USB_SUPERH) += superh_udc.o
358 +controller-$(CONFIG_USB_N9604) += n9604.o
360 +# ... and only one of these, too; kbuild/kconfig don't help though.
361 +g_zero-objs := zero.o usbstring.o config.o epautoconf.o
362 +obj-$(CONFIG_USB_ZERO) += g_zero.o
364 +g_ether-objs := ether.o usbstring.o config.o epautoconf.o
365 +obj-$(CONFIG_USB_ETH) += g_ether.o
367 +ifeq ($(CONFIG_USB_ETH_RNDIS),y)
368 + g_ether-objs += rndis.o
369 +endif
371 +gadgetfs-objs := inode.o usbstring.o
372 +obj-$(CONFIG_USB_GADGETFS) += gadgetfs.o
374 +g_file_storage-objs := file_storage.o usbstring.o config.o \
375 + epautoconf.o
376 +obj-$(CONFIG_USB_FILE_STORAGE) += g_file_storage.o
378 +g_serial-objs := gserial.o usbstring.o epautoconf.o
379 +obj-$(CONFIG_USB_G_SERIAL) += g_serial.o
381 +export-objs := $(controller-y) $(controller-m)
383 +include $(TOPDIR)/Rules.make
385 +g_zero.o: $(g_zero-objs)
386 + $(LD) -r -o $@ $(g_zero-objs)
387 +g_ether.o: $(g_ether-objs)
388 + $(LD) -r -o $@ $(g_ether-objs)
389 +gadgetfs.o: $(gadgetfs-objs)
390 + $(LD) -r -o $@ $(gadgetfs-objs)
391 +g_file_storage.o: $(g_file_storage-objs)
392 + $(LD) -r -o $@ $(g_file_storage-objs)
393 +g_serial.o: $(g_serial-objs)
394 + $(LD) -r -o $@ $(g_serial-objs)
395 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/config.c kernel/drivers/usb/gadget/config.c
396 --- /tmp/kernel/drivers/usb/gadget/config.c 1970-01-01 01:00:00.000000000 +0100
397 +++ kernel/drivers/usb/gadget/config.c 2005-04-22 17:53:19.408543749 +0200
398 @@ -0,0 +1,116 @@
400 + * usb/gadget/config.c -- simplify building config descriptors
402 + * Copyright (C) 2003 David Brownell
404 + * This program is free software; you can redistribute it and/or modify
405 + * it under the terms of the GNU General Public License as published by
406 + * the Free Software Foundation; either version 2 of the License, or
407 + * (at your option) any later version.
409 + * This program is distributed in the hope that it will be useful,
410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
412 + * GNU General Public License for more details.
414 + * You should have received a copy of the GNU General Public License
415 + * along with this program; if not, write to the Free Software
416 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
417 + */
419 +#include <linux/errno.h>
420 +#include <linux/kernel.h>
421 +#include <linux/list.h>
422 +#include <linux/string.h>
423 +#include <asm/byteorder.h>
425 +#include <linux/usb_ch9.h>
428 +/**
429 + * usb_descriptor_fillbuf - fill buffer with descriptors
430 + * @buf: Buffer to be filled
431 + * @buflen: Size of buf
432 + * @src: Array of descriptor pointers, terminated by null pointer.
434 + * Copies descriptors into the buffer, returning the length or a
435 + * negative error code if they can't all be copied. Useful when
436 + * assembling descriptors for an associated set of interfaces used
437 + * as part of configuring a composite device; or in other cases where
438 + * sets of descriptors need to be marshaled.
439 + */
440 +int
441 +usb_descriptor_fillbuf(void *buf, unsigned buflen,
442 + const struct usb_descriptor_header **src)
444 + u8 *dest = buf;
446 + if (!src)
447 + return -EINVAL;
449 + /* fill buffer from src[] until null descriptor ptr */
450 + for (; 0 != *src; src++) {
451 + unsigned len = (*src)->bLength;
453 + if (len > buflen)
454 + return -EINVAL;
455 + memcpy(dest, *src, len);
456 + buflen -= len;
457 + dest += len;
459 + return dest - (u8 *)buf;
463 +/**
464 + * usb_gadget_config_buf - builts a complete configuration descriptor
465 + * @config: Header for the descriptor, including characteristics such
466 + * as power requirements and number of interfaces.
467 + * @desc: Null-terminated vector of pointers to the descriptors (interface,
468 + * endpoint, etc) defining all functions in this device configuration.
469 + * @buf: Buffer for the resulting configuration descriptor.
470 + * @length: Length of buffer. If this is not big enough to hold the
471 + * entire configuration descriptor, an error code will be returned.
473 + * This copies descriptors into the response buffer, building a descriptor
474 + * for that configuration. It returns the buffer length or a negative
475 + * status code. The config.wTotalLength field is set to match the length
476 + * of the result, but other descriptor fields (including power usage and
477 + * interface count) must be set by the caller.
479 + * Gadget drivers could use this when constructing a config descriptor
480 + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
481 + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
482 + */
483 +int usb_gadget_config_buf(
484 + const struct usb_config_descriptor *config,
485 + void *buf,
486 + unsigned length,
487 + const struct usb_descriptor_header **desc
490 + struct usb_config_descriptor *cp = buf;
491 + int len;
493 + /* config descriptor first */
494 + if (length < USB_DT_CONFIG_SIZE || !desc)
495 + return -EINVAL;
496 + *cp = *config;
498 + /* then interface/endpoint/class/vendor/... */
499 + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
500 + length - USB_DT_CONFIG_SIZE, desc);
501 + if (len < 0)
502 + return len;
503 + len += USB_DT_CONFIG_SIZE;
504 + if (len > 0xffff)
505 + return -EINVAL;
507 + /* patch up the config descriptor */
508 + cp->bLength = USB_DT_CONFIG_SIZE;
509 + cp->bDescriptorType = USB_DT_CONFIG;
510 + cp->wTotalLength = cpu_to_le16(len);
511 + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
512 + return len;
515 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/epautoconf.c kernel/drivers/usb/gadget/epautoconf.c
516 --- /tmp/kernel/drivers/usb/gadget/epautoconf.c 1970-01-01 01:00:00.000000000 +0100
517 +++ kernel/drivers/usb/gadget/epautoconf.c 2005-04-22 17:53:19.410543423 +0200
518 @@ -0,0 +1,311 @@
520 + * epautoconf.c -- endpoint autoconfiguration for usb gadget drivers
522 + * Copyright (C) 2004 David Brownell
524 + * This program is free software; you can redistribute it and/or modify
525 + * it under the terms of the GNU General Public License as published by
526 + * the Free Software Foundation; either version 2 of the License, or
527 + * (at your option) any later version.
529 + * This program is distributed in the hope that it will be useful,
530 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
531 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
532 + * GNU General Public License for more details.
534 + * You should have received a copy of the GNU General Public License
535 + * along with this program; if not, write to the Free Software
536 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
538 + */
540 +#include <linux/kernel.h>
541 +#include <linux/init.h>
542 +#include <linux/types.h>
543 +#include <linux/list.h>
544 +#include <linux/errno.h>
545 +#include <linux/ctype.h>
546 +#include <linux/string.h>
547 +#include <linux/usb_ch9.h>
548 +#include <linux/usb_gadget.h>
550 +#include <asm/byteorder.h>
552 +#include "gadget_chips.h"
555 +/* we must assign addresses for configurable endpoints (like net2280) */
556 +static __initdata unsigned epnum;
558 +// #define MANY_ENDPOINTS
559 +#ifdef MANY_ENDPOINTS
560 +/* more than 15 configurable endpoints */
561 +static __initdata unsigned in_epnum;
562 +#endif
566 + * This should work with endpoints from controller drivers sharing the
567 + * same endpoint naming convention. By example:
569 + * - ep1, ep2, ... address is fixed, not direction or type
570 + * - ep1in, ep2out, ... address and direction are fixed, not type
571 + * - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
572 + * - ep1in-bulk, ep2out-iso, ... all three are fixed
573 + * - ep-* ... no functionality restrictions
575 + * Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
576 + * Less common restrictions are implied by gadget_is_*().
578 + * NOTE: each endpoint is unidirectional, as specified by its USB
579 + * descriptor; and isn't specific to a configuration or altsetting.
580 + */
581 +static int __init
582 +ep_matches (
583 + struct usb_gadget *gadget,
584 + struct usb_ep *ep,
585 + struct usb_endpoint_descriptor *desc
588 + u8 type;
589 + const char *tmp;
590 + u16 max;
592 + /* endpoint already claimed? */
593 + if (0 != ep->driver_data)
594 + return 0;
596 + /* only support ep0 for portable CONTROL traffic */
597 + type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
598 + if (USB_ENDPOINT_XFER_CONTROL == type)
599 + return 0;
601 + /* some other naming convention */
602 + if ('e' != ep->name[0])
603 + return 0;
605 + /* type-restriction: "-iso", "-bulk", or "-int".
606 + * direction-restriction: "in", "out".
607 + */
608 + if ('-' != ep->name[2]) {
609 + tmp = strrchr (ep->name, '-');
610 + if (tmp) {
611 + switch (type) {
612 + case USB_ENDPOINT_XFER_INT:
613 + /* bulk endpoints handle interrupt transfers,
614 + * except the toggle-quirky iso-synch kind
615 + */
616 + if ('s' == tmp[2]) // == "-iso"
617 + return 0;
618 + /* for now, avoid PXA "interrupt-in";
619 + * it's documented as never using DATA1.
620 + */
621 + if (gadget_is_pxa (gadget)
622 + && 'i' == tmp [1])
623 + return 0;
624 + break;
625 + case USB_ENDPOINT_XFER_BULK:
626 + if ('b' != tmp[1]) // != "-bulk"
627 + return 0;
628 + break;
629 + case USB_ENDPOINT_XFER_ISOC:
630 + if ('s' != tmp[2]) // != "-iso"
631 + return 0;
633 + } else {
634 + tmp = ep->name + strlen (ep->name);
637 + /* direction-restriction: "..in-..", "out-.." */
638 + tmp--;
639 + if (!isdigit (*tmp)) {
640 + if (desc->bEndpointAddress & USB_DIR_IN) {
641 + if ('n' != *tmp)
642 + return 0;
643 + } else {
644 + if ('t' != *tmp)
645 + return 0;
650 + /* endpoint maxpacket size is an input parameter, except for bulk
651 + * where it's an output parameter representing the full speed limit.
652 + * the usb spec fixes high speed bulk maxpacket at 512 bytes.
653 + */
654 + max = 0x7ff & le16_to_cpup (&desc->wMaxPacketSize);
655 + switch (type) {
656 + case USB_ENDPOINT_XFER_INT:
657 + /* INT: limit 64 bytes full speed, 1024 high speed */
658 + if (!gadget->is_dualspeed && max > 64)
659 + return 0;
660 + /* FALLTHROUGH */
662 + case USB_ENDPOINT_XFER_ISOC:
663 + /* ISO: limit 1023 bytes full speed, 1024 high speed */
664 + if (ep->maxpacket < max)
665 + return 0;
666 + if (!gadget->is_dualspeed && max > 1023)
667 + return 0;
669 + /* BOTH: "high bandwidth" works only at high speed */
670 + if ((desc->wMaxPacketSize & __constant_cpu_to_le16(3<<11))) {
671 + if (!gadget->is_dualspeed)
672 + return 0;
673 + /* configure your hardware with enough buffering!! */
675 + break;
678 + /* MATCH!! */
680 + /* report address */
681 + if (isdigit (ep->name [2])) {
682 + u8 num = simple_strtol (&ep->name [2], NULL, 10);
683 + desc->bEndpointAddress |= num;
684 +#ifdef MANY_ENDPOINTS
685 + } else if (desc->bEndpointAddress & USB_DIR_IN) {
686 + if (++in_epnum > 15)
687 + return 0;
688 + desc->bEndpointAddress = USB_DIR_IN | in_epnum;
689 +#endif
690 + } else {
691 + if (++epnum > 15)
692 + return 0;
693 + desc->bEndpointAddress |= epnum;
696 + /* report (variable) full speed bulk maxpacket */
697 + if (USB_ENDPOINT_XFER_BULK == type) {
698 + int size = ep->maxpacket;
700 + /* min() doesn't work on bitfields with gcc-3.5 */
701 + if (size > 64)
702 + size = 64;
703 + desc->wMaxPacketSize = cpu_to_le16(size);
705 + return 1;
708 +static struct usb_ep * __init
709 +find_ep (struct usb_gadget *gadget, const char *name)
711 + struct usb_ep *ep;
713 + list_for_each_entry (ep, &gadget->ep_list, ep_list) {
714 + if (0 == strcmp (ep->name, name))
715 + return ep;
717 + return NULL;
720 +/**
721 + * usb_ep_autoconfig - choose an endpoint matching the descriptor
722 + * @gadget: The device to which the endpoint must belong.
723 + * @desc: Endpoint descriptor, with endpoint direction and transfer mode
724 + * initialized. For periodic transfers, the maximum packet
725 + * size must also be initialized. This is modified on success.
727 + * By choosing an endpoint to use with the specified descriptor, this
728 + * routine simplifies writing gadget drivers that work with multiple
729 + * USB device controllers. The endpoint would be passed later to
730 + * usb_ep_enable(), along with some descriptor.
732 + * That second descriptor won't always be the same as the first one.
733 + * For example, isochronous endpoints can be autoconfigured for high
734 + * bandwidth, and then used in several lower bandwidth altsettings.
735 + * Also, high and full speed descriptors will be different.
737 + * Be sure to examine and test the results of autoconfiguration on your
738 + * hardware. This code may not make the best choices about how to use the
739 + * USB controller, and it can't know all the restrictions that may apply.
740 + * Some combinations of driver and hardware won't be able to autoconfigure.
742 + * On success, this returns an un-claimed usb_ep, and modifies the endpoint
743 + * descriptor bEndpointAddress. For bulk endpoints, the wMaxPacket value
744 + * is initialized as if the endpoint were used at full speed. To prevent
745 + * the endpoint from being returned by a later autoconfig call, claim it
746 + * by assigning ep->driver_data to some non-null value.
748 + * On failure, this returns a null endpoint descriptor.
749 + */
750 +struct usb_ep * __init usb_ep_autoconfig (
751 + struct usb_gadget *gadget,
752 + struct usb_endpoint_descriptor *desc
755 + struct usb_ep *ep;
756 + u8 type;
758 + type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
760 + /* First, apply chip-specific "best usage" knowledge.
761 + * This might make a good usb_gadget_ops hook ...
762 + */
763 + if (gadget_is_net2280 (gadget) && type == USB_ENDPOINT_XFER_INT) {
764 + /* ep-e, ep-f are PIO with only 64 byte fifos */
765 + ep = find_ep (gadget, "ep-e");
766 + if (ep && ep_matches (gadget, ep, desc))
767 + return ep;
768 + ep = find_ep (gadget, "ep-f");
769 + if (ep && ep_matches (gadget, ep, desc))
770 + return ep;
772 + } else if (gadget_is_goku (gadget)) {
773 + if (USB_ENDPOINT_XFER_INT == type) {
774 + /* single buffering is enough */
775 + ep = find_ep (gadget, "ep3-bulk");
776 + if (ep && ep_matches (gadget, ep, desc))
777 + return ep;
778 + } else if (USB_ENDPOINT_XFER_BULK == type
779 + && (USB_DIR_IN & desc->bEndpointAddress)) {
780 + /* DMA may be available */
781 + ep = find_ep (gadget, "ep2-bulk");
782 + if (ep && ep_matches (gadget, ep, desc))
783 + return ep;
786 + } else if (gadget_is_sh (gadget) && USB_ENDPOINT_XFER_INT == type) {
787 + /* single buffering is enough; maybe 8 byte fifo is too */
788 + ep = find_ep (gadget, "ep3in-bulk");
789 + if (ep && ep_matches (gadget, ep, desc))
790 + return ep;
792 + } else if (gadget_is_mq11xx (gadget) && USB_ENDPOINT_XFER_INT == type) {
793 + ep = find_ep (gadget, "ep1-bulk");
794 + if (ep && ep_matches (gadget, ep, desc))
795 + return ep;
798 + /* Second, look at endpoints until an unclaimed one looks usable */
799 + list_for_each_entry (ep, &gadget->ep_list, ep_list) {
800 + if (ep_matches (gadget, ep, desc))
801 + return ep;
804 + /* Fail */
805 + return NULL;
808 +/**
809 + * usb_ep_autoconfig_reset - reset endpoint autoconfig state
810 + * @gadget: device for which autoconfig state will be reset
812 + * Use this for devices where one configuration may need to assign
813 + * endpoint resources very differently from the next one. It clears
814 + * state such as ep->driver_data and the record of assigned endpoints
815 + * used by usb_ep_autoconfig().
816 + */
817 +void __init usb_ep_autoconfig_reset (struct usb_gadget *gadget)
819 + struct usb_ep *ep;
821 + list_for_each_entry (ep, &gadget->ep_list, ep_list) {
822 + ep->driver_data = NULL;
824 +#ifdef MANY_ENDPOINTS
825 + in_epnum = 0;
826 +#endif
827 + epnum = 0;
830 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/ether.c kernel/drivers/usb/gadget/ether.c
831 --- /tmp/kernel/drivers/usb/gadget/ether.c 1970-01-01 01:00:00.000000000 +0100
832 +++ kernel/drivers/usb/gadget/ether.c 2005-04-22 18:01:31.044861540 +0200
833 @@ -0,0 +1,2734 @@
835 + * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
837 + * Copyright (C) 2003-2005 David Brownell
838 + * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
840 + * This program is free software; you can redistribute it and/or modify
841 + * it under the terms of the GNU General Public License as published by
842 + * the Free Software Foundation; either version 2 of the License, or
843 + * (at your option) any later version.
845 + * This program is distributed in the hope that it will be useful,
846 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
847 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
848 + * GNU General Public License for more details.
850 + * You should have received a copy of the GNU General Public License
851 + * along with this program; if not, write to the Free Software
852 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
853 + */
856 +// #define DEBUG 1
857 +// #define VERBOSE
859 +#include <linux/config.h>
860 +#include <linux/module.h>
861 +#include <linux/kernel.h>
862 +#include <linux/delay.h>
863 +#include <linux/ioport.h>
864 +#include <linux/sched.h>
865 +#include <linux/slab.h>
866 +#include <linux/smp_lock.h>
867 +#include <linux/errno.h>
868 +#include <linux/init.h>
869 +#include <linux/timer.h>
870 +#include <linux/list.h>
871 +#include <linux/interrupt.h>
872 +#include <linux/uts.h>
873 +#include <linux/version.h>
874 +#include <linux/moduleparam.h>
875 +#include <linux/ctype.h>
877 +#include <asm/byteorder.h>
878 +#include <asm/io.h>
879 +#include <asm/irq.h>
880 +#include <asm/system.h>
881 +#include <asm/uaccess.h>
882 +#include <asm/unaligned.h>
884 +#include <linux/usb_ch9.h>
885 +#include <linux/usb_cdc.h>
886 +#include <linux/usb_gadget.h>
888 +#include <linux/random.h>
889 +#include <linux/netdevice.h>
890 +#include <linux/etherdevice.h>
891 +#include <linux/ethtool.h>
893 +#include "gadget_chips.h"
895 +/*-------------------------------------------------------------------------*/
898 + * Ethernet gadget driver -- with CDC and non-CDC options
899 + * Builds on hardware support for a full duplex link.
901 + * CDC Ethernet is the standard USB solution for sending Ethernet frames
902 + * using USB. Real hardware tends to use the same framing protocol but look
903 + * different for control features. This driver strongly prefers to use
904 + * this USB-IF standard as its open-systems interoperability solution;
905 + * most host side USB stacks (except from Microsoft) support it.
907 + * There's some hardware that can't talk CDC. We make that hardware
908 + * implement a "minimalist" vendor-agnostic CDC core: same framing, but
909 + * link-level setup only requires activating the configuration.
910 + * Linux supports it, but other host operating systems may not.
911 + * (This is a subset of CDC Ethernet.)
913 + * A third option is also in use. Rather than CDC Ethernet, or something
914 + * simpler, Microsoft pushes their own approach: RNDIS. The published
915 + * RNDIS specs are ambiguous and appear to be incomplete, and are also
916 + * needlessly complex.
917 + */
919 +#define DRIVER_DESC "Ethernet Gadget"
920 +#define DRIVER_VERSION "Equinox 2004"
922 +static const char shortname [] = "ether";
923 +static const char driver_desc [] = DRIVER_DESC;
925 +#define RX_EXTRA 20 /* guard against rx overflows */
927 +#ifdef CONFIG_USB_ETH_RNDIS
928 +#include "rndis.h"
929 +#else
930 +#define rndis_init() 0
931 +#define rndis_exit() do{}while(0)
932 +#endif
934 +/* 2.6-compat */
935 +#ifndef container_of
936 +#define container_of list_entry
937 +#endif
939 +/** PO: really needed? */
940 +#include <linux/tqueue.h>
941 +#define work_struct tq_struct
942 +#define INIT_WORK INIT_TQUEUE
943 +#define schedule_work schedule_task
944 +#define flush_scheduled_work flush_scheduled_tasks
946 +static void random_ether_addr (u8 *addr)
948 + get_random_bytes (addr, ETH_ALEN);
949 + addr [0] &= 0xfe; // clear multicast bit
950 + addr [0] |= 0x02; // set local assignment bit (IEEE802)
953 +/* CDC and RNDIS support the same host-chosen outgoing packet filters. */
954 +#define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
955 + |USB_CDC_PACKET_TYPE_DIRECTED)
958 +/*-------------------------------------------------------------------------*/
960 +struct eth_dev {
961 + spinlock_t lock;
962 + struct usb_gadget *gadget;
963 + struct usb_request *req; /* for control responses */
964 + struct usb_request *stat_req; /* for cdc & rndis status */
966 + u8 config;
967 + struct usb_ep *in_ep, *out_ep, *status_ep;
968 + const struct usb_endpoint_descriptor
969 + *in, *out, *status;
970 + struct list_head tx_reqs, rx_reqs;
972 + struct net_device *net;
973 + struct net_device_stats stats;
974 + atomic_t tx_qlen;
976 + struct work_struct work;
977 + unsigned zlp:1;
978 + unsigned cdc:1;
979 + unsigned rndis:1;
980 + unsigned suspended:1;
981 + u16 cdc_filter;
982 + unsigned long todo;
983 +#define WORK_RX_MEMORY 0
984 + int rndis_config;
985 + u8 host_mac [ETH_ALEN];
988 +/* This version autoconfigures as much as possible at run-time.
990 + * It also ASSUMES a self-powered device, without remote wakeup,
991 + * although remote wakeup support would make sense.
992 + */
993 +static const char *EP_IN_NAME;
994 +static const char *EP_OUT_NAME;
995 +static const char *EP_STATUS_NAME;
997 +/*-------------------------------------------------------------------------*/
999 +/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
1000 + * Instead: allocate your own, using normal USB-IF procedures.
1001 + */
1003 +/* Thanks to NetChip Technologies for donating this product ID.
1004 + * It's for devices with only CDC Ethernet configurations.
1005 + */
1006 +#define CDC_VENDOR_NUM 0x0525 /* NetChip */
1007 +#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
1009 +/* For hardware that can't talk CDC, we use the same vendor ID that
1010 + * ARM Linux has used for ethernet-over-usb, both with sa1100 and
1011 + * with pxa250. We're protocol-compatible, if the host-side drivers
1012 + * use the endpoint descriptors. bcdDevice (version) is nonzero, so
1013 + * drivers that need to hard-wire endpoint numbers have a hook.
1015 + * The protocol is a minimal subset of CDC Ether, which works on any bulk
1016 + * hardware that's not deeply broken ... even on hardware that can't talk
1017 + * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
1018 + * doesn't handle control-OUT).
1019 + */
1020 +#define SIMPLE_VENDOR_NUM 0x049f
1021 +#define SIMPLE_PRODUCT_NUM 0x505a
1023 +/* For hardware that can talk RNDIS and either of the above protocols,
1024 + * use this ID ... the windows INF files will know it. Unless it's
1025 + * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
1026 + * the non-RNDIS configuration.
1027 + */
1028 +#define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
1029 +#define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
1032 +/* Some systems will want different product identifers published in the
1033 + * device descriptor, either numbers or strings or both. These string
1034 + * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
1035 + */
1037 +static ushort __initdata idVendor;
1038 +MODULE_PARM(idVendor, "h");
1039 +MODULE_PARM_DESC(idVendor, "USB Vendor ID");
1041 +static ushort __initdata idProduct;
1042 +MODULE_PARM(idProduct, "h");
1043 +MODULE_PARM_DESC(idProduct, "USB Product ID");
1045 +static ushort __initdata bcdDevice;
1046 +MODULE_PARM(bcdDevice, "h");
1047 +MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
1049 +static char *__initdata iManufacturer;
1050 +MODULE_PARM(iManufacturer, "s");
1051 +MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
1053 +static char *__initdata iProduct;
1054 +MODULE_PARM(iProduct, "s");
1055 +MODULE_PARM_DESC(iProduct, "USB Product string");
1057 +/* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
1058 +static char *__initdata dev_addr;
1059 +MODULE_PARM(dev_addr, "s");
1060 +MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
1062 +/* this address is invisible to ifconfig */
1063 +static char *__initdata host_addr;
1064 +MODULE_PARM(host_addr, "s");
1065 +MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
1068 +/*-------------------------------------------------------------------------*/
1070 +/* Include CDC support if we could run on CDC-capable hardware. */
1072 +#ifdef CONFIG_USB_GADGET_NET2280
1073 +#define DEV_CONFIG_CDC
1074 +#endif
1076 +#ifdef CONFIG_USB_GADGET_DUMMY_HCD
1077 +#define DEV_CONFIG_CDC
1078 +#endif
1080 +#ifdef CONFIG_USB_GADGET_GOKU
1081 +#define DEV_CONFIG_CDC
1082 +#endif
1084 +#ifdef CONFIG_USB_GADGET_LH7A40X
1085 +#define DEV_CONFIG_CDC
1086 +#endif
1088 +#ifdef CONFIG_USB_GADGET_MQ11XX
1089 +#define DEV_CONFIG_CDC
1090 +#endif
1092 +#ifdef CONFIG_USB_GADGET_OMAP
1093 +#define DEV_CONFIG_CDC
1094 +#endif
1096 +#ifdef CONFIG_USB_GADGET_N9604
1097 +#define DEV_CONFIG_CDC
1098 +#endif
1100 +#ifdef CONFIG_USB_GADGET_PXA27X
1101 +#define DEV_CONFIG_CDC
1102 +#endif
1104 +#ifdef CONFIG_USB_GADGET_AT91
1105 +#define DEV_CONFIG_CDC
1106 +#endif
1109 +/* For CDC-incapable hardware, choose the simple cdc subset.
1110 + * Anything that talks bulk (without notable bugs) can do this.
1111 + */
1112 +#ifdef CONFIG_USB_GADGET_PXA2XX
1113 +#define DEV_CONFIG_SUBSET
1114 +#endif
1116 +#ifdef CONFIG_USB_GADGET_SH
1117 +#define DEV_CONFIG_SUBSET
1118 +#endif
1120 +#ifdef CONFIG_USB_GADGET_SA1100
1121 +/* use non-CDC for backwards compatibility */
1122 +#define DEV_CONFIG_SUBSET
1123 +#endif
1125 +#ifdef CONFIG_USB_GADGET_S3C2410
1126 +#define DEV_CONFIG_CDC
1127 +#endif
1129 +/*-------------------------------------------------------------------------*/
1131 +/* "main" config is either CDC, or its simple subset */
1132 +static inline int is_cdc(struct eth_dev *dev)
1134 +#if !defined(DEV_CONFIG_SUBSET)
1135 + return 1; /* only cdc possible */
1136 +#elif !defined (DEV_CONFIG_CDC)
1137 + return 0; /* only subset possible */
1138 +#else
1139 + return dev->cdc; /* depends on what hardware we found */
1140 +#endif
1143 +/* "secondary" RNDIS config may sometimes be activated */
1144 +static inline int rndis_active(struct eth_dev *dev)
1146 +#ifdef CONFIG_USB_ETH_RNDIS
1147 + return dev->rndis;
1148 +#else
1149 + return 0;
1150 +#endif
1153 +#define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
1154 +#define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
1158 +#define DEFAULT_QLEN 2 /* double buffering by default */
1160 +/* peak bulk transfer bits-per-second */
1161 +#define HS_BPS (13 * 512 * 8 * 1000 * 8)
1162 +#define FS_BPS (19 * 64 * 1 * 1000 * 8)
1164 +#ifdef CONFIG_USB_GADGET_DUALSPEED
1166 +static unsigned qmult = 5;
1167 +MODULE_PARM(qmult, "i");
1170 +/* for dual-speed hardware, use deeper queues at highspeed */
1171 +#define qlen(gadget) \
1172 + (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
1174 +/* also defer IRQs on highspeed TX */
1175 +#define TX_DELAY qmult
1177 +#define BITRATE(g) (((g)->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS)
1179 +#else /* full speed (low speed doesn't do bulk) */
1180 +#define qlen(gadget) DEFAULT_QLEN
1182 +#define BITRATE(g) FS_BPS
1183 +#endif
1186 +/*-------------------------------------------------------------------------*/
1188 +#define xprintk(d,level,fmt,args...) \
1189 + printk(level "%s: " fmt , (d)->net->name , ## args)
1191 +#ifdef DEBUG
1192 +#undef DEBUG
1193 +#define DEBUG(dev,fmt,args...) \
1194 + xprintk(dev , KERN_DEBUG , fmt , ## args)
1195 +#else
1196 +#define DEBUG(dev,fmt,args...) \
1197 + do { } while (0)
1198 +#endif /* DEBUG */
1200 +#ifdef VERBOSE
1201 +#define VDEBUG DEBUG
1202 +#else
1203 +#define VDEBUG(dev,fmt,args...) \
1204 + do { } while (0)
1205 +#endif /* DEBUG */
1207 +#define ERROR(dev,fmt,args...) \
1208 + xprintk(dev , KERN_ERR , fmt , ## args)
1209 +#define WARN(dev,fmt,args...) \
1210 + xprintk(dev , KERN_WARNING , fmt , ## args)
1211 +#define INFO(dev,fmt,args...) \
1212 + xprintk(dev , KERN_INFO , fmt , ## args)
1214 +/*-------------------------------------------------------------------------*/
1216 +/* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
1217 + * ep0 implementation: descriptors, config management, setup().
1218 + * also optional class-specific notification interrupt transfer.
1219 + */
1222 + * DESCRIPTORS ... most are static, but strings and (full) configuration
1223 + * descriptors are built on demand. For now we do either full CDC, or
1224 + * our simple subset, with RNDIS as an optional second configuration.
1226 + * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
1227 + * the class descriptors match a modem (they're ignored; it's really just
1228 + * Ethernet functionality), they don't need the NOP altsetting, and the
1229 + * status transfer endpoint isn't optional.
1230 + */
1232 +#define STRING_MANUFACTURER 1
1233 +#define STRING_PRODUCT 2
1234 +#define STRING_ETHADDR 3
1235 +#define STRING_DATA 4
1236 +#define STRING_CONTROL 5
1237 +#define STRING_RNDIS_CONTROL 6
1238 +#define STRING_CDC 7
1239 +#define STRING_SUBSET 8
1240 +#define STRING_RNDIS 9
1242 +#define USB_BUFSIZ 256 /* holds our biggest descriptor */
1245 + * This device advertises one configuration, eth_config, unless RNDIS
1246 + * is enabled (rndis_config) on hardware supporting at least two configs.
1248 + * NOTE: Controllers like superh_udc should probably be able to use
1249 + * an RNDIS-only configuration.
1251 + * FIXME define some higher-powered configurations to make it easier
1252 + * to recharge batteries ...
1253 + */
1255 +#define DEV_CONFIG_VALUE 1 /* cdc or subset */
1256 +#define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
1258 +static struct usb_device_descriptor
1259 +device_desc = {
1260 + .bLength = sizeof device_desc,
1261 + .bDescriptorType = USB_DT_DEVICE,
1263 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
1265 + .bDeviceClass = USB_CLASS_COMM,
1266 + .bDeviceSubClass = 0,
1267 + .bDeviceProtocol = 0,
1269 + .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
1270 + .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
1271 + .iManufacturer = STRING_MANUFACTURER,
1272 + .iProduct = STRING_PRODUCT,
1273 + .bNumConfigurations = 1,
1276 +static struct usb_otg_descriptor
1277 +otg_descriptor = {
1278 + .bLength = sizeof otg_descriptor,
1279 + .bDescriptorType = USB_DT_OTG,
1281 + .bmAttributes = USB_OTG_SRP,
1284 +static struct usb_config_descriptor
1285 +eth_config = {
1286 + .bLength = sizeof eth_config,
1287 + .bDescriptorType = USB_DT_CONFIG,
1289 + /* compute wTotalLength on the fly */
1290 + .bNumInterfaces = 2,
1291 + .bConfigurationValue = DEV_CONFIG_VALUE,
1292 + .iConfiguration = STRING_CDC,
1293 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1294 + .bMaxPower = 50,
1297 +#ifdef CONFIG_USB_ETH_RNDIS
1298 +static struct usb_config_descriptor
1299 +rndis_config = {
1300 + .bLength = sizeof rndis_config,
1301 + .bDescriptorType = USB_DT_CONFIG,
1303 + /* compute wTotalLength on the fly */
1304 + .bNumInterfaces = 2,
1305 + .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
1306 + .iConfiguration = STRING_RNDIS,
1307 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
1308 + .bMaxPower = 50,
1310 +#endif
1313 + * Compared to the simple CDC subset, the full CDC Ethernet model adds
1314 + * three class descriptors, two interface descriptors, optional status
1315 + * endpoint. Both have a "data" interface and two bulk endpoints.
1316 + * There are also differences in how control requests are handled.
1318 + * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
1319 + * the CDC-ACM (modem) spec.
1320 + */
1322 +#ifdef DEV_CONFIG_CDC
1323 +static struct usb_interface_descriptor
1324 +control_intf = {
1325 + .bLength = sizeof control_intf,
1326 + .bDescriptorType = USB_DT_INTERFACE,
1328 + .bInterfaceNumber = 0,
1329 + /* status endpoint is optional; this may be patched later */
1330 + .bNumEndpoints = 1,
1331 + .bInterfaceClass = USB_CLASS_COMM,
1332 + .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
1333 + .bInterfaceProtocol = USB_CDC_PROTO_NONE,
1334 + .iInterface = STRING_CONTROL,
1336 +#endif
1338 +#ifdef CONFIG_USB_ETH_RNDIS
1339 +static const struct usb_interface_descriptor
1340 +rndis_control_intf = {
1341 + .bLength = sizeof rndis_control_intf,
1342 + .bDescriptorType = USB_DT_INTERFACE,
1344 + .bInterfaceNumber = 0,
1345 + .bNumEndpoints = 1,
1346 + .bInterfaceClass = USB_CLASS_COMM,
1347 + .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
1348 + .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
1349 + .iInterface = STRING_RNDIS_CONTROL,
1351 +#endif
1353 +#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1355 +static const struct usb_cdc_header_desc header_desc = {
1356 + .bLength = sizeof header_desc,
1357 + .bDescriptorType = USB_DT_CS_INTERFACE,
1358 + .bDescriptorSubType = USB_CDC_HEADER_TYPE,
1360 + .bcdCDC = __constant_cpu_to_le16 (0x0110),
1363 +static const struct usb_cdc_union_desc union_desc = {
1364 + .bLength = sizeof union_desc,
1365 + .bDescriptorType = USB_DT_CS_INTERFACE,
1366 + .bDescriptorSubType = USB_CDC_UNION_TYPE,
1368 + .bMasterInterface0 = 0, /* index of control interface */
1369 + .bSlaveInterface0 = 1, /* index of DATA interface */
1372 +#endif /* CDC || RNDIS */
1374 +#ifdef CONFIG_USB_ETH_RNDIS
1376 +static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
1377 + .bLength = sizeof call_mgmt_descriptor,
1378 + .bDescriptorType = USB_DT_CS_INTERFACE,
1379 + .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
1381 + .bmCapabilities = 0x00,
1382 + .bDataInterface = 0x01,
1385 +static struct usb_cdc_acm_descriptor acm_descriptor = {
1386 + .bLength = sizeof acm_descriptor,
1387 + .bDescriptorType = USB_DT_CS_INTERFACE,
1388 + .bDescriptorSubType = USB_CDC_ACM_TYPE,
1390 + .bmCapabilities = 0x00,
1393 +#endif
1395 +#ifdef DEV_CONFIG_CDC
1397 +static const struct usb_cdc_ether_desc ether_desc = {
1398 + .bLength = sizeof ether_desc,
1399 + .bDescriptorType = USB_DT_CS_INTERFACE,
1400 + .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
1402 + /* this descriptor actually adds value, surprise! */
1403 + .iMACAddress = STRING_ETHADDR,
1404 + .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
1405 + .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
1406 + .wNumberMCFilters = __constant_cpu_to_le16 (0),
1407 + .bNumberPowerFilters = 0,
1410 +#endif
1412 +#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1414 +/* include the status endpoint if we can, even where it's optional.
1415 + * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
1416 + * packet, to simplify cancelation; and a big transfer interval, to
1417 + * waste less bandwidth.
1419 + * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
1420 + * if they ignore the connect/disconnect notifications that real aether
1421 + * can provide. more advanced cdc configurations might want to support
1422 + * encapsulated commands (vendor-specific, using control-OUT).
1424 + * RNDIS requires the status endpoint, since it uses that encapsulation
1425 + * mechanism for its funky RPC scheme.
1426 + */
1428 +#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
1429 +#define STATUS_BYTECOUNT 16 /* 8 byte header + data */
1431 +static struct usb_endpoint_descriptor
1432 +fs_status_desc = {
1433 + .bLength = USB_DT_ENDPOINT_SIZE,
1434 + .bDescriptorType = USB_DT_ENDPOINT,
1436 + .bEndpointAddress = USB_DIR_IN,
1437 + .bmAttributes = USB_ENDPOINT_XFER_INT,
1438 + .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
1439 + .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
1441 +#endif
1443 +#ifdef DEV_CONFIG_CDC
1445 +/* the default data interface has no endpoints ... */
1447 +static const struct usb_interface_descriptor
1448 +data_nop_intf = {
1449 + .bLength = sizeof data_nop_intf,
1450 + .bDescriptorType = USB_DT_INTERFACE,
1452 + .bInterfaceNumber = 1,
1453 + .bAlternateSetting = 0,
1454 + .bNumEndpoints = 0,
1455 + .bInterfaceClass = USB_CLASS_CDC_DATA,
1456 + .bInterfaceSubClass = 0,
1457 + .bInterfaceProtocol = 0,
1460 +/* ... but the "real" data interface has two bulk endpoints */
1462 +static const struct usb_interface_descriptor
1463 +data_intf = {
1464 + .bLength = sizeof data_intf,
1465 + .bDescriptorType = USB_DT_INTERFACE,
1467 + .bInterfaceNumber = 1,
1468 + .bAlternateSetting = 1,
1469 + .bNumEndpoints = 2,
1470 + .bInterfaceClass = USB_CLASS_CDC_DATA,
1471 + .bInterfaceSubClass = 0,
1472 + .bInterfaceProtocol = 0,
1473 + .iInterface = STRING_DATA,
1476 +#endif
1478 +#ifdef CONFIG_USB_ETH_RNDIS
1480 +/* RNDIS doesn't activate by changing to the "real" altsetting */
1482 +static const struct usb_interface_descriptor
1483 +rndis_data_intf = {
1484 + .bLength = sizeof rndis_data_intf,
1485 + .bDescriptorType = USB_DT_INTERFACE,
1487 + .bInterfaceNumber = 1,
1488 + .bAlternateSetting = 0,
1489 + .bNumEndpoints = 2,
1490 + .bInterfaceClass = USB_CLASS_CDC_DATA,
1491 + .bInterfaceSubClass = 0,
1492 + .bInterfaceProtocol = 0,
1493 + .iInterface = STRING_DATA,
1496 +#endif
1498 +#ifdef DEV_CONFIG_SUBSET
1501 + * "Simple" CDC-subset option is a simple vendor-neutral model that most
1502 + * full speed controllers can handle: one interface, two bulk endpoints.
1503 + */
1505 +static const struct usb_interface_descriptor
1506 +subset_data_intf = {
1507 + .bLength = sizeof subset_data_intf,
1508 + .bDescriptorType = USB_DT_INTERFACE,
1510 + .bInterfaceNumber = 0,
1511 + .bAlternateSetting = 0,
1512 + .bNumEndpoints = 2,
1513 + .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
1514 + .bInterfaceSubClass = 0,
1515 + .bInterfaceProtocol = 0,
1516 + .iInterface = STRING_DATA,
1519 +#endif /* SUBSET */
1522 +static struct usb_endpoint_descriptor
1523 +fs_source_desc = {
1524 + .bLength = USB_DT_ENDPOINT_SIZE,
1525 + .bDescriptorType = USB_DT_ENDPOINT,
1527 + .bEndpointAddress = USB_DIR_IN,
1528 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
1531 +static struct usb_endpoint_descriptor
1532 +fs_sink_desc = {
1533 + .bLength = USB_DT_ENDPOINT_SIZE,
1534 + .bDescriptorType = USB_DT_ENDPOINT,
1536 + .bEndpointAddress = USB_DIR_OUT,
1537 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
1540 +static const struct usb_descriptor_header *fs_eth_function [11] = {
1541 + (struct usb_descriptor_header *) &otg_descriptor,
1542 +#ifdef DEV_CONFIG_CDC
1543 + /* "cdc" mode descriptors */
1544 + (struct usb_descriptor_header *) &control_intf,
1545 + (struct usb_descriptor_header *) &header_desc,
1546 + (struct usb_descriptor_header *) &union_desc,
1547 + (struct usb_descriptor_header *) &ether_desc,
1548 + /* NOTE: status endpoint may need to be removed */
1549 + (struct usb_descriptor_header *) &fs_status_desc,
1550 + /* data interface, with altsetting */
1551 + (struct usb_descriptor_header *) &data_nop_intf,
1552 + (struct usb_descriptor_header *) &data_intf,
1553 + (struct usb_descriptor_header *) &fs_source_desc,
1554 + (struct usb_descriptor_header *) &fs_sink_desc,
1555 + NULL,
1556 +#endif /* DEV_CONFIG_CDC */
1559 +static inline void __init fs_subset_descriptors(void)
1561 +#ifdef DEV_CONFIG_SUBSET
1562 + fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
1563 + fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
1564 + fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
1565 + fs_eth_function[4] = NULL;
1566 +#else
1567 + fs_eth_function[1] = NULL;
1568 +#endif
1571 +#ifdef CONFIG_USB_ETH_RNDIS
1572 +static const struct usb_descriptor_header *fs_rndis_function [] = {
1573 + (struct usb_descriptor_header *) &otg_descriptor,
1574 + /* control interface matches ACM, not Ethernet */
1575 + (struct usb_descriptor_header *) &rndis_control_intf,
1576 + (struct usb_descriptor_header *) &header_desc,
1577 + (struct usb_descriptor_header *) &call_mgmt_descriptor,
1578 + (struct usb_descriptor_header *) &acm_descriptor,
1579 + (struct usb_descriptor_header *) &union_desc,
1580 + (struct usb_descriptor_header *) &fs_status_desc,
1581 + /* data interface has no altsetting */
1582 + (struct usb_descriptor_header *) &rndis_data_intf,
1583 + (struct usb_descriptor_header *) &fs_source_desc,
1584 + (struct usb_descriptor_header *) &fs_sink_desc,
1585 + NULL,
1587 +#endif
1589 +#ifdef CONFIG_USB_GADGET_DUALSPEED
1592 + * usb 2.0 devices need to expose both high speed and full speed
1593 + * descriptors, unless they only run at full speed.
1594 + */
1596 +#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1597 +static struct usb_endpoint_descriptor
1598 +hs_status_desc = {
1599 + .bLength = USB_DT_ENDPOINT_SIZE,
1600 + .bDescriptorType = USB_DT_ENDPOINT,
1602 + .bmAttributes = USB_ENDPOINT_XFER_INT,
1603 + .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
1604 + .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
1606 +#endif /* DEV_CONFIG_CDC */
1608 +static struct usb_endpoint_descriptor
1609 +hs_source_desc = {
1610 + .bLength = USB_DT_ENDPOINT_SIZE,
1611 + .bDescriptorType = USB_DT_ENDPOINT,
1613 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
1614 + .wMaxPacketSize = __constant_cpu_to_le16 (512),
1617 +static struct usb_endpoint_descriptor
1618 +hs_sink_desc = {
1619 + .bLength = USB_DT_ENDPOINT_SIZE,
1620 + .bDescriptorType = USB_DT_ENDPOINT,
1622 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
1623 + .wMaxPacketSize = __constant_cpu_to_le16 (512),
1626 +static struct usb_qualifier_descriptor
1627 +dev_qualifier = {
1628 + .bLength = sizeof dev_qualifier,
1629 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
1631 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
1632 + .bDeviceClass = USB_CLASS_COMM,
1634 + .bNumConfigurations = 1,
1637 +static const struct usb_descriptor_header *hs_eth_function [11] = {
1638 + (struct usb_descriptor_header *) &otg_descriptor,
1639 +#ifdef DEV_CONFIG_CDC
1640 + /* "cdc" mode descriptors */
1641 + (struct usb_descriptor_header *) &control_intf,
1642 + (struct usb_descriptor_header *) &header_desc,
1643 + (struct usb_descriptor_header *) &union_desc,
1644 + (struct usb_descriptor_header *) &ether_desc,
1645 + /* NOTE: status endpoint may need to be removed */
1646 + (struct usb_descriptor_header *) &hs_status_desc,
1647 + /* data interface, with altsetting */
1648 + (struct usb_descriptor_header *) &data_nop_intf,
1649 + (struct usb_descriptor_header *) &data_intf,
1650 + (struct usb_descriptor_header *) &hs_source_desc,
1651 + (struct usb_descriptor_header *) &hs_sink_desc,
1652 + NULL,
1653 +#endif /* DEV_CONFIG_CDC */
1656 +static inline void __init hs_subset_descriptors(void)
1658 +#ifdef DEV_CONFIG_SUBSET
1659 + hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
1660 + hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
1661 + hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
1662 + hs_eth_function[4] = NULL;
1663 +#else
1664 + hs_eth_function[1] = NULL;
1665 +#endif
1668 +#ifdef CONFIG_USB_ETH_RNDIS
1669 +static const struct usb_descriptor_header *hs_rndis_function [] = {
1670 + (struct usb_descriptor_header *) &otg_descriptor,
1671 + /* control interface matches ACM, not Ethernet */
1672 + (struct usb_descriptor_header *) &rndis_control_intf,
1673 + (struct usb_descriptor_header *) &header_desc,
1674 + (struct usb_descriptor_header *) &call_mgmt_descriptor,
1675 + (struct usb_descriptor_header *) &acm_descriptor,
1676 + (struct usb_descriptor_header *) &union_desc,
1677 + (struct usb_descriptor_header *) &hs_status_desc,
1678 + /* data interface has no altsetting */
1679 + (struct usb_descriptor_header *) &rndis_data_intf,
1680 + (struct usb_descriptor_header *) &hs_source_desc,
1681 + (struct usb_descriptor_header *) &hs_sink_desc,
1682 + NULL,
1684 +#endif
1687 +/* maxpacket and other transfer characteristics vary by speed. */
1688 +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
1690 +#else
1692 +/* if there's no high speed support, maxpacket doesn't change. */
1693 +#define ep_desc(g,hs,fs) fs
1695 +static inline void __init hs_subset_descriptors(void)
1699 +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
1701 +/*-------------------------------------------------------------------------*/
1703 +/* descriptors that are built on-demand */
1705 +static char manufacturer [50];
1706 +static char product_desc [40] = DRIVER_DESC;
1708 +#ifdef DEV_CONFIG_CDC
1709 +/* address that the host will use ... usually assigned at random */
1710 +static char ethaddr [2 * ETH_ALEN + 1];
1711 +#endif
1713 +/* static strings, in UTF-8 */
1714 +static struct usb_string strings [] = {
1715 + { STRING_MANUFACTURER, manufacturer, },
1716 + { STRING_PRODUCT, product_desc, },
1717 + { STRING_DATA, "Ethernet Data", },
1718 +#ifdef DEV_CONFIG_CDC
1719 + { STRING_CDC, "CDC Ethernet", },
1720 + { STRING_ETHADDR, ethaddr, },
1721 + { STRING_CONTROL, "CDC Communications Control", },
1722 +#endif
1723 +#ifdef DEV_CONFIG_SUBSET
1724 + { STRING_SUBSET, "CDC Ethernet Subset", },
1725 +#endif
1726 +#ifdef CONFIG_USB_ETH_RNDIS
1727 + { STRING_RNDIS, "RNDIS", },
1728 + { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
1729 +#endif
1730 + { } /* end of list */
1733 +static struct usb_gadget_strings stringtab = {
1734 + .language = 0x0409, /* en-us */
1735 + .strings = strings,
1739 + * one config, two interfaces: control, data.
1740 + * complications: class descriptors, and an altsetting.
1741 + */
1742 +static int
1743 +config_buf (enum usb_device_speed speed,
1744 + u8 *buf, u8 type,
1745 + unsigned index, int is_otg)
1747 + int len;
1748 + const struct usb_config_descriptor *config;
1749 + const struct usb_descriptor_header **function;
1750 +#ifdef CONFIG_USB_GADGET_DUALSPEED
1751 + int hs = (speed == USB_SPEED_HIGH);
1753 + if (type == USB_DT_OTHER_SPEED_CONFIG)
1754 + hs = !hs;
1755 +#define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
1756 +#else
1757 +#define which_fn(t) (fs_ ## t ## _function)
1758 +#endif
1760 + if (index >= device_desc.bNumConfigurations)
1761 + return -EINVAL;
1763 +#ifdef CONFIG_USB_ETH_RNDIS
1764 + /* list the RNDIS config first, to make Microsoft's drivers
1765 + * happy. DOCSIS 1.0 needs this too.
1766 + */
1767 + if (device_desc.bNumConfigurations == 2 && index == 0) {
1768 + config = &rndis_config;
1769 + function = which_fn (rndis);
1770 + } else
1771 +#endif
1773 + config = &eth_config;
1774 + function = which_fn (eth);
1777 + /* for now, don't advertise srp-only devices */
1778 + if (!is_otg)
1779 + function++;
1781 + len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
1782 + if (len < 0)
1783 + return len;
1784 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
1785 + return len;
1788 +/*-------------------------------------------------------------------------*/
1790 +static void eth_start (struct eth_dev *dev, int gfp_flags);
1791 +static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags);
1793 +#ifdef DEV_CONFIG_CDC
1794 +static inline int ether_alt_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
1796 + const struct usb_endpoint_descriptor *d;
1798 + /* With CDC, the host isn't allowed to use these two data
1799 + * endpoints in the default altsetting for the interface.
1800 + * so we don't activate them yet. Reset from SET_INTERFACE.
1802 + * Strictly speaking RNDIS should work the same: activation is
1803 + * a side effect of setting a packet filter. Deactivation is
1804 + * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
1805 + */
1807 + /* one endpoint writes data back IN to the host */
1808 + if (strcmp (ep->name, EP_IN_NAME) == 0) {
1809 + d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
1810 + ep->driver_data = dev;
1811 + dev->in = d;
1813 + /* one endpoint just reads OUT packets */
1814 + } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
1815 + d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
1816 + ep->driver_data = dev;
1817 + dev->out = d;
1819 + /* optional status/notification endpoint */
1820 + } else if (EP_STATUS_NAME &&
1821 + strcmp (ep->name, EP_STATUS_NAME) == 0) {
1822 + int result;
1824 + d = ep_desc (dev->gadget, &hs_status_desc, &fs_status_desc);
1825 + result = usb_ep_enable (ep, d);
1826 + if (result < 0)
1827 + return result;
1829 + ep->driver_data = dev;
1830 + dev->status = d;
1832 + return 0;
1834 +#endif
1836 +#if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1837 +static inline int ether_ep_setup (struct eth_dev *dev, struct usb_ep *ep)
1839 + int result;
1840 + const struct usb_endpoint_descriptor *d;
1842 + /* CDC subset is simpler: if the device is there,
1843 + * it's live with rx and tx endpoints.
1845 + * Do this as a shortcut for RNDIS too.
1846 + */
1848 + /* one endpoint writes data back IN to the host */
1849 + if (strcmp (ep->name, EP_IN_NAME) == 0) {
1850 + d = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
1851 + result = usb_ep_enable (ep, d);
1852 + if (result < 0)
1853 + return result;
1855 + ep->driver_data = dev;
1856 + dev->in = d;
1858 + /* one endpoint just reads OUT packets */
1859 + } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
1860 + d = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
1861 + result = usb_ep_enable (ep, d);
1862 + if (result < 0)
1863 + return result;
1865 + ep->driver_data = dev;
1866 + dev->out = d;
1869 + return 0;
1871 +#endif
1873 +static int
1874 +set_ether_config (struct eth_dev *dev, int gfp_flags)
1876 + int result = 0;
1877 + struct usb_ep *ep;
1878 + struct usb_gadget *gadget = dev->gadget;
1880 + gadget_for_each_ep (ep, gadget) {
1881 +#ifdef DEV_CONFIG_CDC
1882 + if (!dev->rndis && dev->cdc) {
1883 + result = ether_alt_ep_setup (dev, ep);
1884 + if (result == 0)
1885 + continue;
1887 +#endif
1889 +#ifdef CONFIG_USB_ETH_RNDIS
1890 + if (dev->rndis && strcmp (ep->name, EP_STATUS_NAME) == 0) {
1891 + const struct usb_endpoint_descriptor *d;
1892 + d = ep_desc (gadget, &hs_status_desc, &fs_status_desc);
1893 + result = usb_ep_enable (ep, d);
1894 + if (result == 0) {
1895 + ep->driver_data = dev;
1896 + dev->status = d;
1897 + continue;
1899 + } else
1900 +#endif
1903 +#if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1904 + result = ether_ep_setup (dev, ep);
1905 + if (result == 0)
1906 + continue;
1907 +#endif
1910 + /* stop on error */
1911 + ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
1912 + break;
1914 + if (!result && (!dev->in_ep || !dev->out_ep))
1915 + result = -ENODEV;
1917 + if (result == 0)
1918 + result = alloc_requests (dev, qlen (gadget), gfp_flags);
1920 + /* on error, disable any endpoints */
1921 + if (result < 0) {
1922 +#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
1923 + if (dev->status)
1924 + (void) usb_ep_disable (dev->status_ep);
1925 +#endif
1926 + dev->status = NULL;
1927 +#if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1928 + if (dev->rndis || !dev->cdc) {
1929 + if (dev->in)
1930 + (void) usb_ep_disable (dev->in_ep);
1931 + if (dev->out)
1932 + (void) usb_ep_disable (dev->out_ep);
1934 +#endif
1935 + dev->in = NULL;
1936 + dev->out = NULL;
1937 + } else
1939 + /* activate non-CDC configs right away
1940 + * this isn't strictly according to the RNDIS spec
1941 + */
1942 +#if defined(DEV_CONFIG_SUBSET) || defined(CONFIG_USB_ETH_RNDIS)
1943 + if (dev->rndis || !dev->cdc) {
1944 + netif_carrier_on (dev->net);
1945 + if (netif_running (dev->net)) {
1946 + spin_unlock (&dev->lock);
1947 + eth_start (dev, GFP_ATOMIC);
1948 + spin_lock (&dev->lock);
1951 +#endif
1953 + if (result == 0)
1954 + DEBUG (dev, "qlen %d\n", qlen (gadget));
1956 + /* caller is responsible for cleanup on error */
1957 + return result;
1960 +static void eth_reset_config (struct eth_dev *dev)
1962 + struct usb_request *req;
1964 + if (dev->config == 0)
1965 + return;
1967 + DEBUG (dev, "%s\n", __FUNCTION__);
1969 + netif_stop_queue (dev->net);
1970 + netif_carrier_off (dev->net);
1972 + /* disable endpoints, forcing (synchronous) completion of
1973 + * pending i/o. then free the requests.
1974 + */
1975 + if (dev->in) {
1976 + usb_ep_disable (dev->in_ep);
1977 + while (likely (!list_empty (&dev->tx_reqs))) {
1978 + req = container_of (dev->tx_reqs.next,
1979 + struct usb_request, list);
1980 + list_del (&req->list);
1981 + usb_ep_free_request (dev->in_ep, req);
1984 + if (dev->out) {
1985 + usb_ep_disable (dev->out_ep);
1986 + while (likely (!list_empty (&dev->rx_reqs))) {
1987 + req = container_of (dev->rx_reqs.next,
1988 + struct usb_request, list);
1989 + list_del (&req->list);
1990 + usb_ep_free_request (dev->out_ep, req);
1994 + if (dev->status) {
1995 + usb_ep_disable (dev->status_ep);
1997 + dev->config = 0;
2000 +/* change our operational config. must agree with the code
2001 + * that returns config descriptors, and altsetting code.
2002 + */
2003 +static int
2004 +eth_set_config (struct eth_dev *dev, unsigned number, int gfp_flags)
2006 + int result = 0;
2007 + struct usb_gadget *gadget = dev->gadget;
2009 + if (number == dev->config)
2010 + return 0;
2012 + if (gadget_is_sa1100 (gadget)
2013 + && dev->config
2014 + && atomic_read (&dev->tx_qlen) != 0) {
2015 + /* tx fifo is full, but we can't clear it...*/
2016 + INFO (dev, "can't change configurations\n");
2017 + return -ESPIPE;
2019 + eth_reset_config (dev);
2021 + /* default: pass all packets, no multicast filtering */
2022 + dev->cdc_filter = 0x000f;
2024 + switch (number) {
2025 + case DEV_CONFIG_VALUE:
2026 + dev->rndis = 0;
2027 + result = set_ether_config (dev, gfp_flags);
2028 + break;
2029 +#ifdef CONFIG_USB_ETH_RNDIS
2030 + case DEV_RNDIS_CONFIG_VALUE:
2031 + dev->rndis = 1;
2032 + result = set_ether_config (dev, gfp_flags);
2033 + break;
2034 +#endif
2035 + default:
2036 + result = -EINVAL;
2037 + /* FALL THROUGH */
2038 + case 0:
2039 + break;
2042 + if (result) {
2043 + if (number)
2044 + eth_reset_config (dev);
2045 + usb_gadget_vbus_draw(dev->gadget,
2046 + dev->gadget->is_otg ? 8 : 100);
2047 + } else {
2048 + char *speed;
2049 + unsigned power;
2051 + power = 2 * eth_config.bMaxPower;
2052 + usb_gadget_vbus_draw(dev->gadget, power);
2054 + switch (gadget->speed) {
2055 + case USB_SPEED_FULL: speed = "full"; break;
2056 +#ifdef CONFIG_USB_GADGET_DUALSPEED
2057 + case USB_SPEED_HIGH: speed = "high"; break;
2058 +#endif
2059 + default: speed = "?"; break;
2062 + dev->config = number;
2063 + INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
2064 + speed, number, power, driver_desc,
2065 + dev->rndis
2066 + ? "RNDIS"
2067 + : (dev->cdc
2068 + ? "CDC Ethernet"
2069 + : "CDC Ethernet Subset"));
2071 + return result;
2074 +/*-------------------------------------------------------------------------*/
2076 +#ifdef DEV_CONFIG_CDC
2078 +static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
2080 + struct usb_cdc_notification *event = req->buf;
2081 + int value = req->status;
2082 + struct eth_dev *dev = ep->driver_data;
2084 + /* issue the second notification if host reads the first */
2085 + if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
2086 + && value == 0) {
2087 + __le32 *data = req->buf + sizeof *event;
2089 + event->bmRequestType = 0xA1;
2090 + event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
2091 + event->wValue = __constant_cpu_to_le16 (0);
2092 + event->wIndex = __constant_cpu_to_le16 (1);
2093 + event->wLength = __constant_cpu_to_le16 (8);
2095 + /* SPEED_CHANGE data is up/down speeds in bits/sec */
2096 + data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
2098 + req->length = STATUS_BYTECOUNT;
2099 + value = usb_ep_queue (ep, req, GFP_ATOMIC);
2100 + DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
2101 + if (value == 0)
2102 + return;
2103 + } else if (value != -ECONNRESET)
2104 + DEBUG (dev, "event %02x --> %d\n",
2105 + event->bNotificationType, value);
2106 + event->bmRequestType = 0xff;
2109 +static void issue_start_status (struct eth_dev *dev)
2111 + struct usb_request *req = dev->stat_req;
2112 + struct usb_cdc_notification *event;
2113 + int value;
2115 + DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
2117 + /* flush old status
2119 + * FIXME ugly idiom, maybe we'd be better with just
2120 + * a "cancel the whole queue" primitive since any
2121 + * unlink-one primitive has way too many error modes.
2122 + * here, we "know" toggle is already clear...
2123 + */
2124 + usb_ep_disable (dev->status_ep);
2125 + usb_ep_enable (dev->status_ep, dev->status);
2127 + /* 3.8.1 says to issue first NETWORK_CONNECTION, then
2128 + * a SPEED_CHANGE. could be useful in some configs.
2129 + */
2130 + event = req->buf;
2131 + event->bmRequestType = 0xA1;
2132 + event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
2133 + event->wValue = __constant_cpu_to_le16 (1); /* connected */
2134 + event->wIndex = __constant_cpu_to_le16 (1);
2135 + event->wLength = 0;
2137 + req->length = sizeof *event;
2138 + req->complete = eth_status_complete;
2139 + value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
2140 + if (value < 0)
2141 + DEBUG (dev, "status buf queue --> %d\n", value);
2144 +#endif
2146 +/*-------------------------------------------------------------------------*/
2148 +static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
2150 + if (req->status || req->actual != req->length)
2151 + DEBUG ((struct eth_dev *) ep->driver_data,
2152 + "setup complete --> %d, %d/%d\n",
2153 + req->status, req->actual, req->length);
2156 +#ifdef CONFIG_USB_ETH_RNDIS
2158 +static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
2160 + if (req->status || req->actual != req->length)
2161 + DEBUG ((struct eth_dev *) ep->driver_data,
2162 + "rndis response complete --> %d, %d/%d\n",
2163 + req->status, req->actual, req->length);
2165 + /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
2168 +static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
2170 + struct eth_dev *dev = ep->driver_data;
2171 + int status;
2173 + /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
2174 + spin_lock(&dev->lock);
2175 + status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
2176 + if (status < 0)
2177 + ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
2178 + spin_unlock(&dev->lock);
2181 +#endif /* RNDIS */
2184 + * The setup() callback implements all the ep0 functionality that's not
2185 + * handled lower down. CDC has a number of less-common features:
2187 + * - two interfaces: control, and ethernet data
2188 + * - Ethernet data interface has two altsettings: default, and active
2189 + * - class-specific descriptors for the control interface
2190 + * - class-specific control requests
2191 + */
2192 +static int
2193 +eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
2195 + struct eth_dev *dev = get_gadget_data (gadget);
2196 + struct usb_request *req = dev->req;
2197 + int value = -EOPNOTSUPP;
2198 + u16 wIndex = ctrl->wIndex;
2199 + u16 wValue = ctrl->wValue;
2200 + u16 wLength = ctrl->wLength;
2202 + /* descriptors just go into the pre-allocated ep0 buffer,
2203 + * while config change events may enable network traffic.
2204 + */
2205 + req->complete = eth_setup_complete;
2206 + switch (ctrl->bRequest) {
2208 + case USB_REQ_GET_DESCRIPTOR:
2209 + if (ctrl->bRequestType != USB_DIR_IN)
2210 + break;
2211 + switch (wValue >> 8) {
2213 + case USB_DT_DEVICE:
2214 + value = min (wLength, (u16) sizeof device_desc);
2215 + memcpy (req->buf, &device_desc, value);
2216 + break;
2217 +#ifdef CONFIG_USB_GADGET_DUALSPEED
2218 + case USB_DT_DEVICE_QUALIFIER:
2219 + if (!gadget->is_dualspeed)
2220 + break;
2221 + value = min (wLength, (u16) sizeof dev_qualifier);
2222 + memcpy (req->buf, &dev_qualifier, value);
2223 + break;
2225 + case USB_DT_OTHER_SPEED_CONFIG:
2226 + if (!gadget->is_dualspeed)
2227 + break;
2228 + // FALLTHROUGH
2229 +#endif /* CONFIG_USB_GADGET_DUALSPEED */
2230 + case USB_DT_CONFIG:
2231 + value = config_buf (gadget->speed, req->buf,
2232 + wValue >> 8,
2233 + wValue & 0xff,
2234 + gadget->is_otg);
2235 + if (value >= 0)
2236 + value = min (wLength, (u16) value);
2237 + break;
2239 + case USB_DT_STRING:
2240 + value = usb_gadget_get_string (&stringtab,
2241 + wValue & 0xff, req->buf);
2242 + if (value >= 0)
2243 + value = min (wLength, (u16) value);
2244 + break;
2246 + break;
2248 + case USB_REQ_SET_CONFIGURATION:
2249 + if (ctrl->bRequestType != 0)
2250 + break;
2251 + if (gadget->a_hnp_support)
2252 + DEBUG (dev, "HNP available\n");
2253 + else if (gadget->a_alt_hnp_support)
2254 + DEBUG (dev, "HNP needs a different root port\n");
2255 + spin_lock (&dev->lock);
2256 + value = eth_set_config (dev, wValue, GFP_ATOMIC);
2257 + spin_unlock (&dev->lock);
2258 + break;
2259 + case USB_REQ_GET_CONFIGURATION:
2260 + if (ctrl->bRequestType != USB_DIR_IN)
2261 + break;
2262 + *(u8 *)req->buf = dev->config;
2263 + value = min (wLength, (u16) 1);
2264 + break;
2266 + case USB_REQ_SET_INTERFACE:
2267 + if (ctrl->bRequestType != USB_RECIP_INTERFACE
2268 + || !dev->config
2269 + || wIndex > 1)
2270 + break;
2271 + if (!dev->cdc && wIndex != 0)
2272 + break;
2273 + spin_lock (&dev->lock);
2275 + /* PXA hardware partially handles SET_INTERFACE;
2276 + * we need to kluge around that interference.
2277 + */
2278 + if (gadget_is_pxa (gadget)) {
2279 + value = eth_set_config (dev, DEV_CONFIG_VALUE,
2280 + GFP_ATOMIC);
2281 + goto done_set_intf;
2284 +#ifdef DEV_CONFIG_CDC
2285 + switch (wIndex) {
2286 + case 0: /* control/master intf */
2287 + if (wValue != 0)
2288 + break;
2289 + if (dev->status) {
2290 + usb_ep_disable (dev->status_ep);
2291 + usb_ep_enable (dev->status_ep, dev->status);
2293 + value = 0;
2294 + break;
2295 + case 1: /* data intf */
2296 + if (wValue > 1)
2297 + break;
2298 + usb_ep_disable (dev->in_ep);
2299 + usb_ep_disable (dev->out_ep);
2301 + /* CDC requires the data transfers not be done from
2302 + * the default interface setting ... also, setting
2303 + * the non-default interface clears filters etc.
2304 + */
2305 + if (wValue == 1) {
2306 + usb_ep_enable (dev->in_ep, dev->in);
2307 + usb_ep_enable (dev->out_ep, dev->out);
2308 + dev->cdc_filter = DEFAULT_FILTER;
2309 + netif_carrier_on (dev->net);
2310 + if (dev->status)
2311 + issue_start_status (dev);
2312 + if (netif_running (dev->net)) {
2313 + spin_unlock (&dev->lock);
2314 + eth_start (dev, GFP_ATOMIC);
2315 + spin_lock (&dev->lock);
2317 + } else {
2318 + netif_stop_queue (dev->net);
2319 + netif_carrier_off (dev->net);
2321 + value = 0;
2322 + break;
2324 +#else
2325 + /* FIXME this is wrong, as is the assumption that
2326 + * all non-PXA hardware talks real CDC ...
2327 + */
2328 + WARN(dev, "set_interface ignored!\n");
2329 +#endif /* DEV_CONFIG_CDC */
2331 +done_set_intf:
2332 + spin_unlock (&dev->lock);
2333 + break;
2334 + case USB_REQ_GET_INTERFACE:
2335 + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
2336 + || !dev->config
2337 + || wIndex > 1)
2338 + break;
2339 + if (!(dev->cdc || dev->rndis) && wIndex != 0)
2340 + break;
2342 + /* for CDC, iff carrier is on, data interface is active. */
2343 + if (dev->rndis || wIndex != 1)
2344 + *(u8 *)req->buf = 0;
2345 + else
2346 + *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
2347 + value = min (wLength, (u16) 1);
2348 + break;
2350 +#ifdef DEV_CONFIG_CDC
2351 + case USB_CDC_SET_ETHERNET_PACKET_FILTER:
2352 + /* see 6.2.30: no data, wIndex = interface,
2353 + * wValue = packet filter bitmap
2354 + */
2355 + if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
2356 + || !dev->cdc
2357 + || dev->rndis
2358 + || wLength != 0
2359 + || wIndex > 1)
2360 + break;
2361 + DEBUG (dev, "packet filter %02x\n", wValue);
2362 + dev->cdc_filter = wValue;
2363 + value = 0;
2364 + break;
2366 + /* and potentially:
2367 + * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
2368 + * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
2369 + * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
2370 + * case USB_CDC_GET_ETHERNET_STATISTIC:
2371 + */
2373 +#endif /* DEV_CONFIG_CDC */
2375 +#ifdef CONFIG_USB_ETH_RNDIS
2376 + /* RNDIS uses the CDC command encapsulation mechanism to implement
2377 + * an RPC scheme, with much getting/setting of attributes by OID.
2378 + */
2379 + case USB_CDC_SEND_ENCAPSULATED_COMMAND:
2380 + if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
2381 + || !dev->rndis
2382 + || wLength > USB_BUFSIZ
2383 + || wValue
2384 + || rndis_control_intf.bInterfaceNumber
2385 + != wIndex)
2386 + break;
2387 + /* read the request, then process it */
2388 + value = wLength;
2389 + req->complete = rndis_command_complete;
2390 + /* later, rndis_control_ack () sends a notification */
2391 + break;
2393 + case USB_CDC_GET_ENCAPSULATED_RESPONSE:
2394 + if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
2395 + == ctrl->bRequestType
2396 + && dev->rndis
2397 + // && wLength >= 0x0400
2398 + && !wValue
2399 + && rndis_control_intf.bInterfaceNumber
2400 + == wIndex) {
2401 + u8 *buf;
2403 + /* return the result */
2404 + buf = rndis_get_next_response (dev->rndis_config,
2405 + &value);
2406 + if (buf) {
2407 + memcpy (req->buf, buf, value);
2408 + req->complete = rndis_response_complete;
2409 + rndis_free_response(dev->rndis_config, buf);
2411 + /* else stalls ... spec says to avoid that */
2413 + break;
2414 +#endif /* RNDIS */
2416 + default:
2417 + VDEBUG (dev,
2418 + "unknown control req%02x.%02x v%04x i%04x l%d\n",
2419 + ctrl->bRequestType, ctrl->bRequest,
2420 + wValue, wIndex, wLength);
2423 + /* respond with data transfer before status phase? */
2424 + if (value >= 0) {
2425 + req->length = value;
2426 + req->zero = value < wLength
2427 + && (value % gadget->ep0->maxpacket) == 0;
2428 + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
2429 + if (value < 0) {
2430 + DEBUG (dev, "ep_queue --> %d\n", value);
2431 + req->status = 0;
2432 + eth_setup_complete (gadget->ep0, req);
2436 + /* host either stalls (value < 0) or reports success */
2437 + return value;
2440 +static void
2441 +eth_disconnect (struct usb_gadget *gadget)
2443 + struct eth_dev *dev = get_gadget_data (gadget);
2444 + unsigned long flags;
2446 + spin_lock_irqsave (&dev->lock, flags);
2447 + netif_stop_queue (dev->net);
2448 + netif_carrier_off (dev->net);
2449 + eth_reset_config (dev);
2450 + spin_unlock_irqrestore (&dev->lock, flags);
2452 + /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
2454 + /* next we may get setup() calls to enumerate new connections;
2455 + * or an unbind() during shutdown (including removing module).
2456 + */
2459 +/*-------------------------------------------------------------------------*/
2461 +/* NETWORK DRIVER HOOKUP (to the layer above this driver) */
2463 +/* glue code: in more recent 2.4 kernels these functions are contained in netdev.h */
2465 +#ifndef HAVE_NETDEV_PRIV
2466 +static inline void *netdev_priv(struct net_device *net)
2468 + return net->priv;
2470 +#endif
2472 +#ifndef HAVE_FREE_NETDEV
2473 +static inline void free_netdev(struct net_device *dev)
2475 + kfree(dev);
2477 +#endif
2479 +static int eth_change_mtu (struct net_device *net, int new_mtu)
2481 + struct eth_dev *dev = netdev_priv(net);
2483 + // FIXME if rndis, don't change while link's live
2485 + if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
2486 + return -ERANGE;
2487 + /* no zero-length packet read wanted after mtu-sized packets */
2488 + if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
2489 + return -EDOM;
2490 + net->mtu = new_mtu;
2491 + return 0;
2494 +static struct net_device_stats *eth_get_stats (struct net_device *net)
2496 + return &((struct eth_dev *)netdev_priv(net))->stats;
2499 +static int eth_ethtool_ioctl (struct net_device *net, void *useraddr)
2501 + struct eth_dev *dev = (struct eth_dev *) net->priv;
2502 + u32 cmd;
2504 + if (get_user (cmd, (u32 *)useraddr))
2505 + return -EFAULT;
2506 + switch (cmd) {
2508 + case ETHTOOL_GDRVINFO: { /* get driver info */
2509 + struct ethtool_drvinfo info;
2511 + memset (&info, 0, sizeof info);
2512 + info.cmd = ETHTOOL_GDRVINFO;
2513 + strncpy (info.driver, shortname, sizeof info.driver);
2514 + strncpy (info.version, DRIVER_VERSION, sizeof info.version);
2515 + strncpy (info.fw_version, dev->gadget->name, sizeof info.fw_version);
2516 + strncpy (info.bus_info, dev->gadget->dev.bus_id,
2517 + sizeof info.bus_info);
2518 + if (copy_to_user (useraddr, &info, sizeof (info)))
2519 + return -EFAULT;
2520 + return 0;
2523 + case ETHTOOL_GLINK: { /* get link status */
2524 + struct ethtool_value edata = { ETHTOOL_GLINK };
2526 + edata.data = (dev->gadget->speed != USB_SPEED_UNKNOWN);
2527 + if (copy_to_user (useraddr, &edata, sizeof (edata)))
2528 + return -EFAULT;
2529 + return 0;
2533 + /* Note that the ethtool user space code requires EOPNOTSUPP */
2534 + return -EOPNOTSUPP;
2537 +static int eth_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
2539 + switch (cmd) {
2540 + case SIOCETHTOOL:
2541 + return eth_ethtool_ioctl (net, (void *)rq->ifr_data);
2542 + default:
2543 + return -EOPNOTSUPP;
2547 +static void defer_kevent (struct eth_dev *dev, int flag)
2549 + if (test_and_set_bit (flag, &dev->todo))
2550 + return;
2551 + if (!schedule_work (&dev->work))
2552 + ERROR (dev, "kevent %d may have been dropped\n", flag);
2553 + else
2554 + DEBUG (dev, "kevent %d scheduled\n", flag);
2557 +static void rx_complete (struct usb_ep *ep, struct usb_request *req);
2559 +#ifndef NET_IP_ALIGN
2560 +/* this can be a cpu-specific value */
2561 +#define NET_IP_ALIGN 2
2562 +#endif
2564 +static int
2565 +rx_submit (struct eth_dev *dev, struct usb_request *req, int gfp_flags)
2567 + struct sk_buff *skb;
2568 + int retval = -ENOMEM;
2569 + size_t size;
2571 + /* Padding up to RX_EXTRA handles minor disagreements with host.
2572 + * Normally we use the USB "terminate on short read" convention;
2573 + * so allow up to (N*maxpacket), since that memory is normally
2574 + * already allocated. Some hardware doesn't deal well with short
2575 + * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
2576 + * byte off the end (to force hardware errors on overflow).
2578 + * RNDIS uses internal framing, and explicitly allows senders to
2579 + * pad to end-of-packet. That's potentially nice for speed,
2580 + * but means receivers can't recover synch on their own.
2581 + */
2582 + size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
2583 + size += dev->out_ep->maxpacket - 1;
2584 +#ifdef CONFIG_USB_ETH_RNDIS
2585 + if (dev->rndis)
2586 + size += sizeof (struct rndis_packet_msg_type);
2587 +#endif
2588 + size -= size % dev->out_ep->maxpacket;
2590 + if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
2591 + DEBUG (dev, "no rx skb\n");
2592 + goto enomem;
2595 + /* Some platforms perform better when IP packets are aligned,
2596 + * but on at least one, checksumming fails otherwise. Note:
2597 + * this doesn't account for variable-sized RNDIS headers.
2598 + */
2599 + skb_reserve(skb, NET_IP_ALIGN);
2601 + req->buf = skb->data;
2602 + req->length = size;
2603 + req->complete = rx_complete;
2604 + req->context = skb;
2606 + retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
2607 + if (retval == -ENOMEM)
2608 +enomem:
2609 + defer_kevent (dev, WORK_RX_MEMORY);
2610 + if (retval) {
2611 + DEBUG (dev, "rx submit --> %d\n", retval);
2612 + dev_kfree_skb_any (skb);
2613 + spin_lock (&dev->lock);
2614 + list_add (&req->list, &dev->rx_reqs);
2615 + spin_unlock (&dev->lock);
2617 + return retval;
2620 +static void rx_complete (struct usb_ep *ep, struct usb_request *req)
2622 + struct sk_buff *skb = req->context;
2623 + struct eth_dev *dev = ep->driver_data;
2624 + int status = req->status;
2626 + switch (status) {
2628 + /* normal completion */
2629 + case 0:
2630 + skb_put (skb, req->actual);
2631 +#ifdef CONFIG_USB_ETH_RNDIS
2632 + /* we know MaxPacketsPerTransfer == 1 here */
2633 + if (dev->rndis)
2634 + rndis_rm_hdr (req->buf, &(skb->len));
2635 +#endif
2636 + if (ETH_HLEN > skb->len || skb->len > ETH_FRAME_LEN) {
2637 + dev->stats.rx_errors++;
2638 + dev->stats.rx_length_errors++;
2639 + DEBUG (dev, "rx length %d\n", skb->len);
2640 + break;
2643 + skb->dev = dev->net;
2644 + skb->protocol = eth_type_trans (skb, dev->net);
2645 + dev->stats.rx_packets++;
2646 + dev->stats.rx_bytes += skb->len;
2648 + /* no buffer copies needed, unless hardware can't
2649 + * use skb buffers.
2650 + */
2651 + status = netif_rx (skb);
2652 + skb = NULL;
2653 + break;
2655 + /* software-driven interface shutdown */
2656 + case -ECONNRESET: // unlink
2657 + case -ESHUTDOWN: // disconnect etc
2658 + VDEBUG (dev, "rx shutdown, code %d\n", status);
2659 + goto quiesce;
2661 + /* for hardware automagic (such as pxa) */
2662 + case -ECONNABORTED: // endpoint reset
2663 + DEBUG (dev, "rx %s reset\n", ep->name);
2664 + defer_kevent (dev, WORK_RX_MEMORY);
2665 +quiesce:
2666 + dev_kfree_skb_any (skb);
2667 + goto clean;
2669 + /* data overrun */
2670 + case -EOVERFLOW:
2671 + dev->stats.rx_over_errors++;
2672 + // FALLTHROUGH
2674 + default:
2675 + dev->stats.rx_errors++;
2676 + DEBUG (dev, "rx status %d\n", status);
2677 + break;
2680 + if (skb)
2681 + dev_kfree_skb_any (skb);
2682 + if (!netif_running (dev->net)) {
2683 +clean:
2684 + /* nobody reading rx_reqs, so no dev->lock */
2685 + list_add (&req->list, &dev->rx_reqs);
2686 + req = NULL;
2688 + if (req)
2689 + rx_submit (dev, req, GFP_ATOMIC);
2692 +static int prealloc (struct list_head *list, struct usb_ep *ep,
2693 + unsigned n, int gfp_flags)
2695 + unsigned i;
2696 + struct usb_request *req;
2698 + if (!n)
2699 + return -ENOMEM;
2701 + /* queue/recycle up to N requests */
2702 + i = n;
2703 + list_for_each_entry (req, list, list) {
2704 + if (i-- == 0)
2705 + goto extra;
2707 + while (i--) {
2708 + req = usb_ep_alloc_request (ep, gfp_flags);
2709 + if (!req)
2710 + return list_empty (list) ? -ENOMEM : 0;
2711 + list_add (&req->list, list);
2713 + return 0;
2715 +extra:
2716 + /* free extras */
2717 + for (;;) {
2718 + struct list_head *next;
2720 + next = req->list.next;
2721 + list_del (&req->list);
2722 + usb_ep_free_request (ep, req);
2724 + if (next == list)
2725 + break;
2727 + req = container_of (next, struct usb_request, list);
2729 + return 0;
2732 +static int alloc_requests (struct eth_dev *dev, unsigned n, int gfp_flags)
2734 + int status;
2736 + status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
2737 + if (status < 0)
2738 + goto fail;
2739 + status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
2740 + if (status < 0)
2741 + goto fail;
2742 + return 0;
2743 +fail:
2744 + DEBUG (dev, "can't alloc requests\n");
2745 + return status;
2748 +static void rx_fill (struct eth_dev *dev, int gfp_flags)
2750 + struct usb_request *req;
2751 + unsigned long flags;
2753 + clear_bit (WORK_RX_MEMORY, &dev->todo);
2755 + /* fill unused rxq slots with some skb */
2756 + spin_lock_irqsave (&dev->lock, flags);
2757 + while (!list_empty (&dev->rx_reqs)) {
2758 + req = container_of (dev->rx_reqs.next,
2759 + struct usb_request, list);
2760 + list_del_init (&req->list);
2761 + spin_unlock_irqrestore (&dev->lock, flags);
2763 + if (rx_submit (dev, req, gfp_flags) < 0) {
2764 + defer_kevent (dev, WORK_RX_MEMORY);
2765 + return;
2768 + spin_lock_irqsave (&dev->lock, flags);
2770 + spin_unlock_irqrestore (&dev->lock, flags);
2773 +static void eth_work (void *_dev)
2775 + struct eth_dev *dev = _dev;
2777 + if (test_bit (WORK_RX_MEMORY, &dev->todo)) {
2778 + if (netif_running (dev->net))
2779 + rx_fill (dev, GFP_KERNEL);
2780 + else
2781 + clear_bit (WORK_RX_MEMORY, &dev->todo);
2784 + if (dev->todo)
2785 + DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
2788 +static void tx_complete (struct usb_ep *ep, struct usb_request *req)
2790 + struct sk_buff *skb = req->context;
2791 + struct eth_dev *dev = ep->driver_data;
2793 + switch (req->status) {
2794 + default:
2795 + dev->stats.tx_errors++;
2796 + VDEBUG (dev, "tx err %d\n", req->status);
2797 + /* FALLTHROUGH */
2798 + case -ECONNRESET: // unlink
2799 + case -ESHUTDOWN: // disconnect etc
2800 + break;
2801 + case 0:
2802 + dev->stats.tx_bytes += skb->len;
2804 + dev->stats.tx_packets++;
2806 + spin_lock (&dev->lock);
2807 + list_add (&req->list, &dev->tx_reqs);
2808 + spin_unlock (&dev->lock);
2809 + dev_kfree_skb_any (skb);
2811 + atomic_dec (&dev->tx_qlen);
2812 + if (netif_carrier_ok (dev->net))
2813 + netif_wake_queue (dev->net);
2816 +static inline int eth_is_promisc (struct eth_dev *dev)
2818 + /* no filters for the CDC subset; always promisc */
2819 + if (subset_active (dev))
2820 + return 1;
2821 + return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
2824 +static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
2826 + struct eth_dev *dev = netdev_priv(net);
2827 + int length = skb->len;
2828 + int retval;
2829 + struct usb_request *req = NULL;
2830 + unsigned long flags;
2832 + /* apply outgoing CDC or RNDIS filters */
2833 + if (!eth_is_promisc (dev)) {
2834 + u8 *dest = skb->data;
2836 + if (dest [0] & 0x01) {
2837 + u16 type;
2839 + /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
2840 + * SET_ETHERNET_MULTICAST_FILTERS requests
2841 + */
2842 + if (memcmp (dest, net->broadcast, ETH_ALEN) == 0)
2843 + type = USB_CDC_PACKET_TYPE_BROADCAST;
2844 + else
2845 + type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
2846 + if (!(dev->cdc_filter & type)) {
2847 + dev_kfree_skb_any (skb);
2848 + return 0;
2851 + /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
2854 + spin_lock_irqsave (&dev->lock, flags);
2855 + req = container_of (dev->tx_reqs.next, struct usb_request, list);
2856 + list_del (&req->list);
2857 + if (list_empty (&dev->tx_reqs))
2858 + netif_stop_queue (net);
2859 + spin_unlock_irqrestore (&dev->lock, flags);
2861 + /* no buffer copies needed, unless the network stack did it
2862 + * or the hardware can't use skb buffers.
2863 + * or there's not enough space for any RNDIS headers we need
2864 + */
2865 +#ifdef CONFIG_USB_ETH_RNDIS
2866 + if (dev->rndis) {
2867 + struct sk_buff *skb_rndis;
2869 + skb_rndis = skb_realloc_headroom (skb,
2870 + sizeof (struct rndis_packet_msg_type));
2871 + if (!skb_rndis)
2872 + goto drop;
2874 + dev_kfree_skb_any (skb);
2875 + skb = skb_rndis;
2876 + rndis_add_hdr (skb);
2877 + length = skb->len;
2879 +#endif
2880 + req->buf = skb->data;
2881 + req->context = skb;
2882 + req->complete = tx_complete;
2884 + /* use zlp framing on tx for strict CDC-Ether conformance,
2885 + * though any robust network rx path ignores extra padding.
2886 + * and some hardware doesn't like to write zlps.
2887 + */
2888 + req->zero = 1;
2889 + if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
2890 + length++;
2892 + req->length = length;
2894 +#ifdef CONFIG_USB_GADGET_DUALSPEED
2895 + /* throttle highspeed IRQ rate back slightly */
2896 + req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
2897 + ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
2898 + : 0;
2899 +#endif
2901 + retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
2902 + switch (retval) {
2903 + default:
2904 + DEBUG (dev, "tx queue err %d\n", retval);
2905 + break;
2906 + case 0:
2907 + net->trans_start = jiffies;
2908 + atomic_inc (&dev->tx_qlen);
2911 + if (retval) {
2912 +#ifdef CONFIG_USB_ETH_RNDIS
2913 +drop:
2914 +#endif
2915 + dev->stats.tx_dropped++;
2916 + dev_kfree_skb_any (skb);
2917 + spin_lock_irqsave (&dev->lock, flags);
2918 + if (list_empty (&dev->tx_reqs))
2919 + netif_start_queue (net);
2920 + list_add (&req->list, &dev->tx_reqs);
2921 + spin_unlock_irqrestore (&dev->lock, flags);
2923 + return 0;
2926 +/*-------------------------------------------------------------------------*/
2928 +#ifdef CONFIG_USB_ETH_RNDIS
2930 +static void rndis_send_media_state (struct eth_dev *dev, int connect)
2932 + if (!dev)
2933 + return;
2935 + if (connect) {
2936 + if (rndis_signal_connect (dev->rndis_config))
2937 + return;
2938 + } else {
2939 + if (rndis_signal_disconnect (dev->rndis_config))
2940 + return;
2944 +static void
2945 +rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
2947 + if (req->status || req->actual != req->length)
2948 + DEBUG ((struct eth_dev *) ep->driver_data,
2949 + "rndis control ack complete --> %d, %d/%d\n",
2950 + req->status, req->actual, req->length);
2952 + usb_ep_free_buffer(ep, req->buf, req->dma, 8);
2953 + usb_ep_free_request(ep, req);
2956 +static int rndis_control_ack (struct net_device *net)
2958 + struct eth_dev *dev = netdev_priv(net);
2959 + u32 length;
2960 + struct usb_request *resp;
2962 + /* in case RNDIS calls this after disconnect */
2963 + if (!dev->status_ep) {
2964 + DEBUG (dev, "status ENODEV\n");
2965 + return -ENODEV;
2968 + /* Allocate memory for notification ie. ACK */
2969 + resp = usb_ep_alloc_request (dev->status_ep, GFP_ATOMIC);
2970 + if (!resp) {
2971 + DEBUG (dev, "status ENOMEM\n");
2972 + return -ENOMEM;
2975 + resp->buf = usb_ep_alloc_buffer (dev->status_ep, 8,
2976 + &resp->dma, GFP_ATOMIC);
2977 + if (!resp->buf) {
2978 + DEBUG (dev, "status buf ENOMEM\n");
2979 + usb_ep_free_request (dev->status_ep, resp);
2980 + return -ENOMEM;
2983 + /* Send RNDIS RESPONSE_AVAILABLE notification;
2984 + * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
2985 + */
2986 + resp->length = 8;
2987 + resp->complete = rndis_control_ack_complete;
2989 + *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
2990 + *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
2992 + length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
2993 + if (length < 0) {
2994 + resp->status = 0;
2995 + rndis_control_ack_complete (dev->status_ep, resp);
2998 + return 0;
3001 +#endif /* RNDIS */
3003 +static void eth_start (struct eth_dev *dev, int gfp_flags)
3005 + DEBUG (dev, "%s\n", __FUNCTION__);
3007 + /* fill the rx queue */
3008 + rx_fill (dev, gfp_flags);
3010 + /* and open the tx floodgates */
3011 + atomic_set (&dev->tx_qlen, 0);
3012 + netif_wake_queue (dev->net);
3013 +#ifdef CONFIG_USB_ETH_RNDIS
3014 + if (dev->rndis) {
3015 + rndis_set_param_medium (dev->rndis_config,
3016 + NDIS_MEDIUM_802_3,
3017 + BITRATE(dev->gadget));
3018 + rndis_send_media_state (dev, 1);
3020 +#endif
3023 +static int eth_open (struct net_device *net)
3025 + struct eth_dev *dev = netdev_priv(net);
3027 + DEBUG (dev, "%s\n", __FUNCTION__);
3028 + if (netif_carrier_ok (dev->net))
3029 + eth_start (dev, GFP_KERNEL);
3030 + return 0;
3033 +static int eth_stop (struct net_device *net)
3035 + struct eth_dev *dev = netdev_priv(net);
3037 + VDEBUG (dev, "%s\n", __FUNCTION__);
3038 + netif_stop_queue (net);
3040 + DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
3041 + dev->stats.rx_packets, dev->stats.tx_packets,
3042 + dev->stats.rx_errors, dev->stats.tx_errors
3043 + );
3045 + /* ensure there are no more active requests */
3046 + if (dev->config) {
3047 + usb_ep_disable (dev->in_ep);
3048 + usb_ep_disable (dev->out_ep);
3049 + if (netif_carrier_ok (dev->net)) {
3050 + DEBUG (dev, "host still using in/out endpoints\n");
3051 + // FIXME idiom may leave toggle wrong here
3052 + usb_ep_enable (dev->in_ep, dev->in);
3053 + usb_ep_enable (dev->out_ep, dev->out);
3055 + if (dev->status_ep) {
3056 + usb_ep_disable (dev->status_ep);
3057 + usb_ep_enable (dev->status_ep, dev->status);
3061 +#ifdef CONFIG_USB_ETH_RNDIS
3062 + if (dev->rndis) {
3063 + rndis_set_param_medium (dev->rndis_config,
3064 + NDIS_MEDIUM_802_3, 0);
3065 + rndis_send_media_state (dev, 0);
3067 +#endif
3069 + return 0;
3072 +/*-------------------------------------------------------------------------*/
3074 +static struct usb_request *eth_req_alloc (struct usb_ep *ep, unsigned size)
3076 + struct usb_request *req;
3078 + req = usb_ep_alloc_request (ep, GFP_KERNEL);
3079 + if (!req)
3080 + return NULL;
3082 + req->buf = kmalloc (size, GFP_KERNEL);
3083 + if (!req->buf) {
3084 + usb_ep_free_request (ep, req);
3085 + req = NULL;
3087 + return req;
3090 +static void
3091 +eth_req_free (struct usb_ep *ep, struct usb_request *req)
3093 + kfree (req->buf);
3094 + usb_ep_free_request (ep, req);
3098 +static void
3099 +eth_unbind (struct usb_gadget *gadget)
3101 + struct eth_dev *dev = get_gadget_data (gadget);
3103 + DEBUG (dev, "unbind\n");
3104 +#ifdef CONFIG_USB_ETH_RNDIS
3105 + rndis_deregister (dev->rndis_config);
3106 + rndis_exit ();
3107 +#endif
3109 + /* we've already been disconnected ... no i/o is active */
3110 + if (dev->req) {
3111 + eth_req_free (gadget->ep0, dev->req);
3112 + dev->req = NULL;
3114 + if (dev->stat_req) {
3115 + eth_req_free (dev->status_ep, dev->stat_req);
3116 + dev->stat_req = NULL;
3119 + unregister_netdev (dev->net);
3120 + free_netdev(dev->net);
3122 + /* assuming we used keventd, it must quiesce too */
3123 + flush_scheduled_work ();
3124 + set_gadget_data (gadget, NULL);
3127 +static u8 __init nibble (unsigned char c)
3129 + if (likely (isdigit (c)))
3130 + return c - '0';
3131 + c = toupper (c);
3132 + if (likely (isxdigit (c)))
3133 + return 10 + c - 'A';
3134 + return 0;
3137 +static void __init get_ether_addr (const char *str, u8 *dev_addr)
3139 + if (str) {
3140 + unsigned i;
3142 + for (i = 0; i < 6; i++) {
3143 + unsigned char num;
3145 + if((*str == '.') || (*str == ':'))
3146 + str++;
3147 + num = nibble(*str++) << 4;
3148 + num |= (nibble(*str++));
3149 + dev_addr [i] = num;
3151 + if (is_valid_ether_addr (dev_addr))
3152 + return;
3154 + random_ether_addr(dev_addr);
3157 +static int __init
3158 +eth_bind (struct usb_gadget *gadget)
3160 + struct eth_dev *dev;
3161 + struct net_device *net;
3162 + u8 cdc = 1, zlp = 1, rndis = 1;
3163 + struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
3164 + int status = -ENOMEM;
3166 + /* these flags are only ever cleared; compiler take note */
3167 +#ifndef DEV_CONFIG_CDC
3168 + cdc = 0;
3169 +#endif
3170 +#ifndef CONFIG_USB_ETH_RNDIS
3171 + rndis = 0;
3172 +#endif
3174 + /* Because most host side USB stacks handle CDC Ethernet, that
3175 + * standard protocol is _strongly_ preferred for interop purposes.
3176 + * (By everyone except Microsoft.)
3177 + */
3178 + if (gadget_is_net2280 (gadget)) {
3179 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
3180 + } else if (gadget_is_dummy (gadget)) {
3181 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0202);
3182 + } else if (gadget_is_pxa (gadget)) {
3183 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
3184 + /* pxa doesn't support altsettings */
3185 + cdc = 0;
3186 + } else if (gadget_is_sh(gadget)) {
3187 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
3188 + /* sh doesn't support multiple interfaces or configs */
3189 + cdc = 0;
3190 + rndis = 0;
3191 + } else if (gadget_is_sa1100 (gadget)) {
3192 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
3193 + /* hardware can't write zlps */
3194 + zlp = 0;
3195 + /* sa1100 CAN do CDC, without status endpoint ... we use
3196 + * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
3197 + */
3198 + cdc = 0;
3199 + } else if (gadget_is_goku (gadget)) {
3200 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
3201 + } else if (gadget_is_mq11xx (gadget)) {
3202 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
3203 + } else if (gadget_is_omap (gadget)) {
3204 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
3205 + } else if (gadget_is_lh7a40x(gadget)) {
3206 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0209);
3207 + } else if (gadget_is_n9604(gadget)) {
3208 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0210);
3209 + } else if (gadget_is_pxa27x(gadget)) {
3210 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0211);
3211 + } else if (gadget_is_s3c2410(gadget)) {
3212 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0212);
3213 + } else if (gadget_is_at91(gadget)) {
3214 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0213);
3215 + } else {
3216 + /* can't assume CDC works. don't want to default to
3217 + * anything less functional on CDC-capable hardware,
3218 + * so we fail in this case.
3219 + */
3220 + printk (KERN_ERR "%s: "
3221 + "controller '%s' not recognized\n",
3222 + shortname, gadget->name);
3223 + return -ENODEV;
3225 + snprintf (manufacturer, sizeof manufacturer,
3226 + UTS_SYSNAME " " UTS_RELEASE "/%s",
3227 + gadget->name);
3229 + /* If there's an RNDIS configuration, that's what Windows wants to
3230 + * be using ... so use these product IDs here and in the "linux.inf"
3231 + * needed to install MSFT drivers. Current Linux kernels will use
3232 + * the second configuration if it's CDC Ethernet, and need some help
3233 + * to choose the right configuration otherwise.
3234 + */
3235 + if (rndis) {
3236 + device_desc.idVendor =
3237 + __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
3238 + device_desc.idProduct =
3239 + __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
3240 + snprintf (product_desc, sizeof product_desc,
3241 + "RNDIS/%s", driver_desc);
3243 + /* CDC subset ... recognized by Linux since 2.4.10, but Windows
3244 + * drivers aren't widely available.
3245 + */
3246 + } else if (!cdc) {
3247 + device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
3248 + device_desc.idVendor =
3249 + __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
3250 + device_desc.idProduct =
3251 + __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
3254 + /* support optional vendor/distro customization */
3255 + if (idVendor) {
3256 + if (!idProduct) {
3257 + printk (KERN_ERR "%s: idVendor needs idProduct!\n",
3258 + shortname);
3259 + return -ENODEV;
3261 + device_desc.idVendor = cpu_to_le16(idVendor);
3262 + device_desc.idProduct = cpu_to_le16(idProduct);
3263 + if (bcdDevice)
3264 + device_desc.bcdDevice = cpu_to_le16(bcdDevice);
3266 + if (iManufacturer)
3267 + strncpy (manufacturer, iManufacturer, sizeof manufacturer);
3268 + if (iProduct)
3269 + strncpy (product_desc, iProduct, sizeof product_desc);
3271 + /* all we really need is bulk IN/OUT */
3272 + usb_ep_autoconfig_reset (gadget);
3273 + in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
3274 + if (!in_ep) {
3275 +autoconf_fail:
3276 + printk (KERN_ERR "%s: can't autoconfigure on %s\n",
3277 + shortname, gadget->name);
3278 + return -ENODEV;
3280 + EP_IN_NAME = in_ep->name;
3281 + in_ep->driver_data = in_ep; /* claim */
3283 + out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
3284 + if (!out_ep)
3285 + goto autoconf_fail;
3286 + EP_OUT_NAME = out_ep->name;
3287 + out_ep->driver_data = out_ep; /* claim */
3289 +#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
3290 + /* CDC Ethernet control interface doesn't require a status endpoint.
3291 + * Since some hosts expect one, try to allocate one anyway.
3292 + */
3293 + if (cdc || rndis) {
3294 + status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
3295 + if (status_ep) {
3296 + EP_STATUS_NAME = status_ep->name;
3297 + status_ep->driver_data = status_ep; /* claim */
3298 + } else if (rndis) {
3299 + printk (KERN_ERR "%s: "
3300 + "can't run RNDIS on %s\n",
3301 + shortname, gadget->name);
3302 + return -ENODEV;
3303 +#ifdef DEV_CONFIG_CDC
3304 + /* pxa25x only does CDC subset; often used with RNDIS */
3305 + } else if (cdc) {
3306 + control_intf.bNumEndpoints = 0;
3307 + /* FIXME remove endpoint from descriptor list */
3308 +#endif
3311 +#endif
3313 + /* one config: cdc, else minimal subset */
3314 + if (!cdc) {
3315 + eth_config.bNumInterfaces = 1;
3316 + eth_config.iConfiguration = STRING_SUBSET;
3317 + fs_subset_descriptors();
3318 + hs_subset_descriptors();
3321 + /* For now RNDIS is always a second config */
3322 + if (rndis)
3323 + device_desc.bNumConfigurations = 2;
3325 +#ifdef CONFIG_USB_GADGET_DUALSPEED
3326 + if (rndis)
3327 + dev_qualifier.bNumConfigurations = 2;
3328 + else if (!cdc)
3329 + dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
3331 + /* assumes ep0 uses the same value for both speeds ... */
3332 + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
3334 + /* and that all endpoints are dual-speed */
3335 + hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
3336 + hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
3337 +#if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
3338 + if (EP_STATUS_NAME)
3339 + hs_status_desc.bEndpointAddress =
3340 + fs_status_desc.bEndpointAddress;
3341 +#endif
3342 +#endif /* DUALSPEED */
3344 + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
3345 + usb_gadget_set_selfpowered (gadget);
3347 + if (gadget->is_otg) {
3348 + otg_descriptor.bmAttributes |= USB_OTG_HNP,
3349 + eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
3350 + eth_config.bMaxPower = 4;
3351 +#ifdef CONFIG_USB_ETH_RNDIS
3352 + rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
3353 + rndis_config.bMaxPower = 4;
3354 +#endif
3357 + net = alloc_etherdev (sizeof *dev);
3358 + if (!net)
3359 + return status;
3360 + dev = netdev_priv(net);
3361 + spin_lock_init (&dev->lock);
3362 + INIT_WORK (&dev->work, eth_work, dev);
3363 + INIT_LIST_HEAD (&dev->tx_reqs);
3364 + INIT_LIST_HEAD (&dev->rx_reqs);
3366 + /* network device setup */
3367 + dev->net = net;
3368 + SET_MODULE_OWNER (net);
3369 + strcpy (net->name, "usb%d");
3370 + dev->cdc = cdc;
3371 + dev->zlp = zlp;
3373 + dev->in_ep = in_ep;
3374 + dev->out_ep = out_ep;
3375 + dev->status_ep = status_ep;
3377 + /* Module params for these addresses should come from ID proms.
3378 + * The host side address is used with CDC and RNDIS, and commonly
3379 + * ends up in a persistent config database.
3380 + */
3381 + get_ether_addr(dev_addr, net->dev_addr);
3382 + if (cdc || rndis) {
3383 + get_ether_addr(host_addr, dev->host_mac);
3384 +#ifdef DEV_CONFIG_CDC
3385 + snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
3386 + dev->host_mac [0], dev->host_mac [1],
3387 + dev->host_mac [2], dev->host_mac [3],
3388 + dev->host_mac [4], dev->host_mac [5]);
3389 +#endif
3392 + if (rndis) {
3393 + status = rndis_init();
3394 + if (status < 0) {
3395 + printk (KERN_ERR "%s: can't init RNDIS, %d\n",
3396 + shortname, status);
3397 + goto fail;
3401 + net->change_mtu = eth_change_mtu;
3402 + net->get_stats = eth_get_stats;
3403 + net->hard_start_xmit = eth_start_xmit;
3404 + net->open = eth_open;
3405 + net->stop = eth_stop;
3406 + // watchdog_timeo, tx_timeout ...
3407 + // set_multicast_list
3408 + net->do_ioctl = eth_ioctl;
3410 + /* preallocate control message data and buffer */
3411 + dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ);
3412 + if (!dev->req)
3413 + goto fail;
3414 + dev->req->complete = eth_setup_complete;
3416 + /* PO: this code my be reached with STATUS_BYTECOUNT undefined
3417 + Don't allocate stat_req then?!
3418 + */
3419 +#ifdef STATUS_BYTECOUNT
3420 + /* ... and maybe likewise for status transfer */
3421 + if (dev->status_ep) {
3422 + dev->stat_req = eth_req_alloc (dev->status_ep,
3423 + STATUS_BYTECOUNT);
3424 + if (!dev->stat_req) {
3425 + eth_req_free (gadget->ep0, dev->req);
3426 + goto fail;
3429 +#endif
3431 + /* finish hookup to lower layer ... */
3432 + dev->gadget = gadget;
3433 + set_gadget_data (gadget, dev);
3434 + gadget->ep0->driver_data = dev;
3436 + /* two kinds of host-initiated state changes:
3437 + * - iff DATA transfer is active, carrier is "on"
3438 + * - tx queueing enabled if open *and* carrier is "on"
3439 + */
3440 + netif_stop_queue (dev->net);
3441 + netif_carrier_off (dev->net);
3443 + // SET_NETDEV_DEV (dev->net, &gadget->dev);
3444 + status = register_netdev (dev->net);
3445 + if (status < 0)
3446 + goto fail1;
3448 + INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
3449 + INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
3450 + EP_OUT_NAME, EP_IN_NAME,
3451 + EP_STATUS_NAME ? " STATUS " : "",
3452 + EP_STATUS_NAME ? EP_STATUS_NAME : ""
3453 + );
3454 + INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
3455 + net->dev_addr [0], net->dev_addr [1],
3456 + net->dev_addr [2], net->dev_addr [3],
3457 + net->dev_addr [4], net->dev_addr [5]);
3459 + if (cdc || rndis)
3460 + INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
3461 + dev->host_mac [0], dev->host_mac [1],
3462 + dev->host_mac [2], dev->host_mac [3],
3463 + dev->host_mac [4], dev->host_mac [5]);
3465 +#ifdef CONFIG_USB_ETH_RNDIS
3466 + if (rndis) {
3467 + u32 vendorID = 0;
3469 + /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
3471 + dev->rndis_config = rndis_register (rndis_control_ack);
3472 + if (dev->rndis_config < 0) {
3473 +fail0:
3474 + unregister_netdev (dev->net);
3475 + status = -ENODEV;
3476 + goto fail;
3479 + /* these set up a lot of the OIDs that RNDIS needs */
3480 + rndis_set_host_mac (dev->rndis_config, dev->host_mac);
3481 + if (rndis_set_param_dev (dev->rndis_config, dev->net,
3482 + &dev->stats))
3483 + goto fail0;
3484 + if (rndis_set_param_vendor (dev->rndis_config, vendorID,
3485 + manufacturer))
3486 + goto fail0;
3487 + if (rndis_set_param_medium (dev->rndis_config,
3488 + NDIS_MEDIUM_802_3,
3489 + 0))
3490 + goto fail0;
3491 + INFO (dev, "RNDIS ready\n");
3493 +#endif
3495 + return status;
3497 +fail1:
3498 + DEBUG (dev, "register_netdev failed, %d\n", status);
3499 +fail:
3500 + eth_unbind (gadget);
3501 + return status;
3504 +/*-------------------------------------------------------------------------*/
3506 +static void
3507 +eth_suspend (struct usb_gadget *gadget)
3509 + struct eth_dev *dev = get_gadget_data (gadget);
3511 + DEBUG (dev, "suspend\n");
3512 + dev->suspended = 1;
3515 +static void
3516 +eth_resume (struct usb_gadget *gadget)
3518 + struct eth_dev *dev = get_gadget_data (gadget);
3520 + DEBUG (dev, "resume\n");
3521 + dev->suspended = 0;
3524 +/*-------------------------------------------------------------------------*/
3526 +static struct usb_gadget_driver eth_driver = {
3527 +#ifdef CONFIG_USB_GADGET_DUALSPEED
3528 + .speed = USB_SPEED_HIGH,
3529 +#else
3530 + .speed = USB_SPEED_FULL,
3531 +#endif
3532 + .function = (char *) driver_desc,
3533 + .bind = eth_bind,
3534 + .unbind = eth_unbind,
3536 + .setup = eth_setup,
3537 + .disconnect = eth_disconnect,
3539 + /* PO: is this available? */
3540 + .suspend = eth_suspend,
3541 + .resume = eth_resume,
3543 + .driver = {
3544 + .name = (char *) shortname,
3545 + // .shutdown = ...
3546 + // .suspend = ...
3547 + // .resume = ...
3548 + },
3551 +MODULE_DESCRIPTION (DRIVER_DESC);
3552 +MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
3553 +MODULE_LICENSE ("GPL");
3556 +static int __init init (void)
3558 + return usb_gadget_register_driver (&eth_driver);
3560 +module_init (init);
3562 +static void __exit cleanup (void)
3564 + usb_gadget_unregister_driver (&eth_driver);
3566 +module_exit (cleanup);
3568 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/file_storage.c kernel/drivers/usb/gadget/file_storage.c
3569 --- /tmp/kernel/drivers/usb/gadget/file_storage.c 1970-01-01 01:00:00.000000000 +0100
3570 +++ kernel/drivers/usb/gadget/file_storage.c 2005-04-22 17:53:19.431540004 +0200
3571 @@ -0,0 +1,3939 @@
3573 + * file_storage.c -- File-backed USB Storage Gadget, for USB development
3575 + * Copyright (C) 2003, 2004 Alan Stern
3576 + * All rights reserved.
3578 + * Redistribution and use in source and binary forms, with or without
3579 + * modification, are permitted provided that the following conditions
3580 + * are met:
3581 + * 1. Redistributions of source code must retain the above copyright
3582 + * notice, this list of conditions, and the following disclaimer,
3583 + * without modification.
3584 + * 2. Redistributions in binary form must reproduce the above copyright
3585 + * notice, this list of conditions and the following disclaimer in the
3586 + * documentation and/or other materials provided with the distribution.
3587 + * 3. The names of the above-listed copyright holders may not be used
3588 + * to endorse or promote products derived from this software without
3589 + * specific prior written permission.
3591 + * ALTERNATIVELY, this software may be distributed under the terms of the
3592 + * GNU General Public License ("GPL") as published by the Free Software
3593 + * Foundation, either version 2 of that License or (at your option) any
3594 + * later version.
3596 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
3597 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
3598 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3599 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3600 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3601 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3602 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3603 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3604 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3605 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3606 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3607 + */
3611 + * The File-backed Storage Gadget acts as a USB Mass Storage device,
3612 + * appearing to the host as a disk drive. In addition to providing an
3613 + * example of a genuinely useful gadget driver for a USB device, it also
3614 + * illustrates a technique of double-buffering for increased throughput.
3615 + * Last but not least, it gives an easy way to probe the behavior of the
3616 + * Mass Storage drivers in a USB host.
3618 + * Backing storage is provided by a regular file or a block device, specified
3619 + * by the "file" module parameter. Access can be limited to read-only by
3620 + * setting the optional "ro" module parameter.
3622 + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
3623 + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
3624 + * by the optional "transport" module parameter. It also supports the
3625 + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
3626 + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
3627 + * the optional "protocol" module parameter. For testing purposes the
3628 + * gadget will indicate that it has removable media if the optional
3629 + * "removable" module parameter is set. In addition, the default Vendor ID,
3630 + * Product ID, and release number can be overridden.
3632 + * There is support for multiple logical units (LUNs), each of which has
3633 + * its own backing file. The number of LUNs can be set using the optional
3634 + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
3635 + * files are specified using comma-separated lists for "file" and "ro".
3636 + * The default number of LUNs is taken from the number of "file" elements;
3637 + * it is 1 if "file" is not given. If "removable" is not set then a backing
3638 + * file must be specified for each LUN. If it is set, then an unspecified
3639 + * or empty backing filename means the LUN's medium is not loaded.
3641 + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
3642 + * needed (an interrupt-out endpoint is also needed for CBI). The memory
3643 + * requirement amounts to two 16K buffers, size configurable by a parameter.
3644 + * Support is included for both full-speed and high-speed operation.
3646 + * Module options:
3648 + * file=filename[,filename...]
3649 + * Required if "removable" is not set, names of
3650 + * the files or block devices used for
3651 + * backing storage
3652 + * ro=b[,b...] Default false, booleans for read-only access
3653 + * luns=N Default N = number of filenames, number of
3654 + * LUNs to support
3655 + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
3656 + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
3657 + * ATAPI, QIC, UFI, 8070, or SCSI;
3658 + * also 1 - 6)
3659 + * removable Default false, boolean for removable media
3660 + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
3661 + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
3662 + * release=0xRRRR Override the USB release number (bcdDevice)
3663 + * buflen=N Default N=16384, buffer size used (will be
3664 + * rounded down to a multiple of
3665 + * PAGE_CACHE_SIZE)
3666 + * stall Default determined according to the type of
3667 + * USB device controller (usually true),
3668 + * boolean to permit the driver to halt
3669 + * bulk endpoints
3671 + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file" and "ro"
3672 + * options are available; default values are used for everything else.
3674 + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
3675 + */
3679 + * Driver Design
3681 + * The FSG driver is fairly straightforward. There is a main kernel
3682 + * thread that handles most of the work. Interrupt routines field
3683 + * callbacks from the controller driver: bulk- and interrupt-request
3684 + * completion notifications, endpoint-0 events, and disconnect events.
3685 + * Completion events are passed to the main thread by wakeup calls. Many
3686 + * ep0 requests are handled at interrupt time, but SetInterface,
3687 + * SetConfiguration, and device reset requests are forwarded to the
3688 + * thread in the form of "exceptions" using SIGUSR1 signals (since they
3689 + * should interrupt any ongoing file I/O operations).
3691 + * The thread's main routine implements the standard command/data/status
3692 + * parts of a SCSI interaction. It and its subroutines are full of tests
3693 + * for pending signals/exceptions -- all this polling is necessary since
3694 + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
3695 + * indication that the driver really wants to be running in userspace.)
3696 + * An important point is that so long as the thread is alive it keeps an
3697 + * open reference to the backing file. This will prevent unmounting
3698 + * the backing file's underlying filesystem and could cause problems
3699 + * during system shutdown, for example. To prevent such problems, the
3700 + * thread catches INT, TERM, and KILL signals and converts them into
3701 + * an EXIT exception.
3703 + * In normal operation the main thread is started during the gadget's
3704 + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
3705 + * exit when it receives a signal, and there's no point leaving the
3706 + * gadget running when the thread is dead. So just before the thread
3707 + * exits, it deregisters the gadget driver. This makes things a little
3708 + * tricky: The driver is deregistered at two places, and the exiting
3709 + * thread can indirectly call fsg_unbind() which in turn can tell the
3710 + * thread to exit. The first problem is resolved through the use of the
3711 + * REGISTERED atomic bitflag; the driver will only be deregistered once.
3712 + * The second problem is resolved by having fsg_unbind() check
3713 + * fsg->state; it won't try to stop the thread if the state is already
3714 + * FSG_STATE_TERMINATED.
3716 + * To provide maximum throughput, the driver uses a circular pipeline of
3717 + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
3718 + * arbitrarily long; in practice the benefits don't justify having more
3719 + * than 2 stages (i.e., double buffering). But it helps to think of the
3720 + * pipeline as being a long one. Each buffer head contains a bulk-in and
3721 + * a bulk-out request pointer (since the buffer can be used for both
3722 + * output and input -- directions always are given from the host's
3723 + * point of view) as well as a pointer to the buffer and various state
3724 + * variables.
3726 + * Use of the pipeline follows a simple protocol. There is a variable
3727 + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
3728 + * At any time that buffer head may still be in use from an earlier
3729 + * request, so each buffer head has a state variable indicating whether
3730 + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
3731 + * buffer head to be EMPTY, filling the buffer either by file I/O or by
3732 + * USB I/O (during which the buffer head is BUSY), and marking the buffer
3733 + * head FULL when the I/O is complete. Then the buffer will be emptied
3734 + * (again possibly by USB I/O, during which it is marked BUSY) and
3735 + * finally marked EMPTY again (possibly by a completion routine).
3737 + * A module parameter tells the driver to avoid stalling the bulk
3738 + * endpoints wherever the transport specification allows. This is
3739 + * necessary for some UDCs like the SuperH, which cannot reliably clear a
3740 + * halt on a bulk endpoint. However, under certain circumstances the
3741 + * Bulk-only specification requires a stall. In such cases the driver
3742 + * will halt the endpoint and set a flag indicating that it should clear
3743 + * the halt in software during the next device reset. Hopefully this
3744 + * will permit everything to work correctly.
3746 + * One subtle point concerns sending status-stage responses for ep0
3747 + * requests. Some of these requests, such as device reset, can involve
3748 + * interrupting an ongoing file I/O operation, which might take an
3749 + * arbitrarily long time. During that delay the host might give up on
3750 + * the original ep0 request and issue a new one. When that happens the
3751 + * driver should not notify the host about completion of the original
3752 + * request, as the host will no longer be waiting for it. So the driver
3753 + * assigns to each ep0 request a unique tag, and it keeps track of the
3754 + * tag value of the request associated with a long-running exception
3755 + * (device-reset, interface-change, or configuration-change). When the
3756 + * exception handler is finished, the status-stage response is submitted
3757 + * only if the current ep0 request tag is equal to the exception request
3758 + * tag. Thus only the most recently received ep0 request will get a
3759 + * status-stage response.
3761 + * Warning: This driver source file is too long. It ought to be split up
3762 + * into a header file plus about 3 separate .c files, to handle the details
3763 + * of the Gadget, USB Mass Storage, and SCSI protocols.
3764 + */
3767 +#undef DEBUG
3768 +#undef VERBOSE
3769 +#undef DUMP_MSGS
3771 +#include <linux/config.h>
3773 +#include <asm/system.h>
3774 +#include <asm/uaccess.h>
3776 +#include <linux/bitops.h>
3777 +#include <linux/blkdev.h>
3778 +#include <linux/compiler.h>
3779 +#include <linux/completion.h>
3780 +#include <linux/dcache.h>
3781 +#include <linux/fcntl.h>
3782 +#include <linux/file.h>
3783 +#include <linux/fs.h>
3784 +#include <linux/init.h>
3785 +#include <linux/kernel.h>
3786 +#include <linux/limits.h>
3787 +#include <linux/list.h>
3788 +#include <linux/module.h>
3789 +#include <linux/pagemap.h>
3790 +#include <linux/rwsem.h>
3791 +#include <linux/sched.h>
3792 +#include <linux/signal.h>
3793 +#include <linux/slab.h>
3794 +#include <linux/spinlock.h>
3795 +#include <linux/string.h>
3796 +#include <linux/uts.h>
3797 +#include <linux/version.h>
3798 +#include <linux/wait.h>
3800 +#include <linux/usb_ch9.h>
3801 +#include <linux/usb_gadget.h>
3803 +#include "gadget_chips.h"
3806 +/*-------------------------------------------------------------------------*/
3808 +#define DRIVER_DESC "File-backed Storage Gadget"
3809 +#define DRIVER_NAME "g_file_storage"
3810 +#define DRIVER_VERSION "05 June 2004"
3812 +static const char longname[] = DRIVER_DESC;
3813 +static const char shortname[] = DRIVER_NAME;
3815 +MODULE_DESCRIPTION(DRIVER_DESC);
3816 +MODULE_AUTHOR("Alan Stern");
3817 +MODULE_LICENSE("Dual BSD/GPL");
3819 +/* Thanks to NetChip Technologies for donating this product ID.
3821 + * DO NOT REUSE THESE IDs with any other driver!! Ever!!
3822 + * Instead: allocate your own, using normal USB-IF procedures. */
3823 +#define DRIVER_VENDOR_ID 0x0525 // NetChip
3824 +#define DRIVER_PRODUCT_ID 0xa4a5 // Linux-USB File-backed Storage Gadget
3828 + * This driver assumes self-powered hardware and has no way for users to
3829 + * trigger remote wakeup. It uses autoconfiguration to select endpoints
3830 + * and endpoint addresses.
3831 + */
3834 +/*-------------------------------------------------------------------------*/
3836 +#define fakedev_printk(level, dev, format, args...) \
3837 + printk(level "%s %s: " format , DRIVER_NAME , (dev)->name , ## args)
3839 +#define xprintk(f,level,fmt,args...) \
3840 + fakedev_printk(level , (f)->gadget , fmt , ## args)
3841 +#define yprintk(l,level,fmt,args...) \
3842 + fakedev_printk(level , &(l)->dev , fmt , ## args)
3844 +#ifdef DEBUG
3845 +#define DBG(fsg,fmt,args...) \
3846 + xprintk(fsg , KERN_DEBUG , fmt , ## args)
3847 +#define LDBG(lun,fmt,args...) \
3848 + yprintk(lun , KERN_DEBUG , fmt , ## args)
3849 +#define MDBG(fmt,args...) \
3850 + printk(KERN_DEBUG DRIVER_NAME ": " fmt , ## args)
3851 +#else
3852 +#define DBG(fsg,fmt,args...) \
3853 + do { } while (0)
3854 +#define LDBG(lun,fmt,args...) \
3855 + do { } while (0)
3856 +#define MDBG(fmt,args...) \
3857 + do { } while (0)
3858 +#undef VERBOSE
3859 +#undef DUMP_MSGS
3860 +#endif /* DEBUG */
3862 +#ifdef VERBOSE
3863 +#define VDBG DBG
3864 +#define VLDBG LDBG
3865 +#else
3866 +#define VDBG(fsg,fmt,args...) \
3867 + do { } while (0)
3868 +#define VLDBG(lun,fmt,args...) \
3869 + do { } while (0)
3870 +#endif /* VERBOSE */
3872 +#define ERROR(fsg,fmt,args...) \
3873 + xprintk(fsg , KERN_ERR , fmt , ## args)
3874 +#define LERROR(lun,fmt,args...) \
3875 + yprintk(lun , KERN_ERR , fmt , ## args)
3877 +#define WARN(fsg,fmt,args...) \
3878 + xprintk(fsg , KERN_WARNING , fmt , ## args)
3879 +#define LWARN(lun,fmt,args...) \
3880 + yprintk(lun , KERN_WARNING , fmt , ## args)
3882 +#define INFO(fsg,fmt,args...) \
3883 + xprintk(fsg , KERN_INFO , fmt , ## args)
3884 +#define LINFO(lun,fmt,args...) \
3885 + yprintk(lun , KERN_INFO , fmt , ## args)
3887 +#define MINFO(fmt,args...) \
3888 + printk(KERN_INFO DRIVER_NAME ": " fmt , ## args)
3891 +/*-------------------------------------------------------------------------*/
3893 +/* Encapsulate the module parameter settings */
3895 +#define MAX_LUNS 8
3897 +static char *file[MAX_LUNS] = {NULL, };
3898 +static int ro[MAX_LUNS] = {0, };
3899 +static unsigned int luns = 0;
3900 + // Default values
3901 +static char *transport = "BBB";
3902 +static char *protocol = "SCSI";
3903 +static int removable = 0;
3904 +static unsigned short vendor = DRIVER_VENDOR_ID;
3905 +static unsigned short product = DRIVER_PRODUCT_ID;
3906 +static unsigned short release = 0xffff; // Use controller chip type
3907 +static unsigned int buflen = 16384;
3908 +static int stall = 1;
3910 +static struct {
3911 + unsigned int nluns;
3913 + char *transport_parm;
3914 + char *protocol_parm;
3915 + int removable;
3916 + unsigned short vendor;
3917 + unsigned short product;
3918 + unsigned short release;
3919 + unsigned int buflen;
3920 + int can_stall;
3922 + int transport_type;
3923 + char *transport_name;
3924 + int protocol_type;
3925 + char *protocol_name;
3927 +} mod_data;
3930 +MODULE_PARM(file, "1-8s");
3931 +MODULE_PARM_DESC(file, "names of backing files or devices");
3933 +MODULE_PARM(ro, "1-8b");
3934 +MODULE_PARM_DESC(ro, "true to force read-only");
3937 +/* In the non-TEST version, only the file and ro module parameters
3938 + * are available. */
3939 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
3941 +MODULE_PARM(luns, "i");
3942 +MODULE_PARM_DESC(luns, "number of LUNs");
3944 +MODULE_PARM(transport, "s");
3945 +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
3947 +MODULE_PARM(protocol, "s");
3948 +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
3949 + "8070, or SCSI)");
3951 +MODULE_PARM(removable, "b");
3952 +MODULE_PARM_DESC(removable, "true to simulate removable media");
3954 +MODULE_PARM(vendor, "h");
3955 +MODULE_PARM_DESC(vendor, "USB Vendor ID");
3957 +MODULE_PARM(product, "h");
3958 +MODULE_PARM_DESC(product, "USB Product ID");
3960 +MODULE_PARM(release, "h");
3961 +MODULE_PARM_DESC(release, "USB release number");
3963 +MODULE_PARM(buflen, "i");
3964 +MODULE_PARM_DESC(buflen, "I/O buffer size");
3966 +MODULE_PARM(stall, "i");
3967 +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
3969 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
3972 +/*-------------------------------------------------------------------------*/
3974 +/* USB protocol value = the transport method */
3975 +#define USB_PR_CBI 0x00 // Control/Bulk/Interrupt
3976 +#define USB_PR_CB 0x01 // Control/Bulk w/o interrupt
3977 +#define USB_PR_BULK 0x50 // Bulk-only
3979 +/* USB subclass value = the protocol encapsulation */
3980 +#define USB_SC_RBC 0x01 // Reduced Block Commands (flash)
3981 +#define USB_SC_8020 0x02 // SFF-8020i, MMC-2, ATAPI (CD-ROM)
3982 +#define USB_SC_QIC 0x03 // QIC-157 (tape)
3983 +#define USB_SC_UFI 0x04 // UFI (floppy)
3984 +#define USB_SC_8070 0x05 // SFF-8070i (removable)
3985 +#define USB_SC_SCSI 0x06 // Transparent SCSI
3987 +/* Bulk-only data structures */
3989 +/* Command Block Wrapper */
3990 +struct bulk_cb_wrap {
3991 + u32 Signature; // Contains 'USBC'
3992 + u32 Tag; // Unique per command id
3993 + u32 DataTransferLength; // Size of the data
3994 + u8 Flags; // Direction in bit 7
3995 + u8 Lun; // LUN (normally 0)
3996 + u8 Length; // Of the CDB, <= MAX_COMMAND_SIZE
3997 + u8 CDB[16]; // Command Data Block
4000 +#define USB_BULK_CB_WRAP_LEN 31
4001 +#define USB_BULK_CB_SIG 0x43425355 // Spells out USBC
4002 +#define USB_BULK_IN_FLAG 0x80
4004 +/* Command Status Wrapper */
4005 +struct bulk_cs_wrap {
4006 + u32 Signature; // Should = 'USBS'
4007 + u32 Tag; // Same as original command
4008 + u32 Residue; // Amount not transferred
4009 + u8 Status; // See below
4012 +#define USB_BULK_CS_WRAP_LEN 13
4013 +#define USB_BULK_CS_SIG 0x53425355 // Spells out 'USBS'
4014 +#define USB_STATUS_PASS 0
4015 +#define USB_STATUS_FAIL 1
4016 +#define USB_STATUS_PHASE_ERROR 2
4018 +/* Bulk-only class specific requests */
4019 +#define USB_BULK_RESET_REQUEST 0xff
4020 +#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
4023 +/* CBI Interrupt data structure */
4024 +struct interrupt_data {
4025 + u8 bType;
4026 + u8 bValue;
4029 +#define CBI_INTERRUPT_DATA_LEN 2
4031 +/* CBI Accept Device-Specific Command request */
4032 +#define USB_CBI_ADSC_REQUEST 0x00
4035 +#define MAX_COMMAND_SIZE 16 // Length of a SCSI Command Data Block
4037 +/* SCSI commands that we recognize */
4038 +#define SC_FORMAT_UNIT 0x04
4039 +#define SC_INQUIRY 0x12
4040 +#define SC_MODE_SELECT_6 0x15
4041 +#define SC_MODE_SELECT_10 0x55
4042 +#define SC_MODE_SENSE_6 0x1a
4043 +#define SC_MODE_SENSE_10 0x5a
4044 +#define SC_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1e
4045 +#define SC_READ_6 0x08
4046 +#define SC_READ_10 0x28
4047 +#define SC_READ_12 0xa8
4048 +#define SC_READ_CAPACITY 0x25
4049 +#define SC_READ_FORMAT_CAPACITIES 0x23
4050 +#define SC_RELEASE 0x17
4051 +#define SC_REQUEST_SENSE 0x03
4052 +#define SC_RESERVE 0x16
4053 +#define SC_SEND_DIAGNOSTIC 0x1d
4054 +#define SC_START_STOP_UNIT 0x1b
4055 +#define SC_SYNCHRONIZE_CACHE 0x35
4056 +#define SC_TEST_UNIT_READY 0x00
4057 +#define SC_VERIFY 0x2f
4058 +#define SC_WRITE_6 0x0a
4059 +#define SC_WRITE_10 0x2a
4060 +#define SC_WRITE_12 0xaa
4062 +/* SCSI Sense Key/Additional Sense Code/ASC Qualifier values */
4063 +#define SS_NO_SENSE 0
4064 +#define SS_COMMUNICATION_FAILURE 0x040800
4065 +#define SS_INVALID_COMMAND 0x052000
4066 +#define SS_INVALID_FIELD_IN_CDB 0x052400
4067 +#define SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE 0x052100
4068 +#define SS_LOGICAL_UNIT_NOT_SUPPORTED 0x052500
4069 +#define SS_MEDIUM_NOT_PRESENT 0x023a00
4070 +#define SS_MEDIUM_REMOVAL_PREVENTED 0x055302
4071 +#define SS_NOT_READY_TO_READY_TRANSITION 0x062800
4072 +#define SS_RESET_OCCURRED 0x062900
4073 +#define SS_SAVING_PARAMETERS_NOT_SUPPORTED 0x053900
4074 +#define SS_UNRECOVERED_READ_ERROR 0x031100
4075 +#define SS_WRITE_ERROR 0x030c02
4076 +#define SS_WRITE_PROTECTED 0x072700
4078 +#define SK(x) ((u8) ((x) >> 16)) // Sense Key byte, etc.
4079 +#define ASC(x) ((u8) ((x) >> 8))
4080 +#define ASCQ(x) ((u8) (x))
4083 +/*-------------------------------------------------------------------------*/
4086 + * These definitions will permit the compiler to avoid generating code for
4087 + * parts of the driver that aren't used in the non-TEST version. Even gcc
4088 + * can recognize when a test of a constant expression yields a dead code
4089 + * path.
4091 + * Also, in the non-TEST version, open_backing_file() is only used during
4092 + * initialization and the sysfs attribute store_xxx routines aren't used
4093 + * at all. We will define NORMALLY_INIT to mark them as __init so they
4094 + * don't occupy kernel code space unnecessarily.
4095 + */
4097 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
4099 +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
4100 +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
4101 +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
4102 +#define backing_file_is_open(curlun) ((curlun)->filp != NULL)
4103 +#define NORMALLY_INIT
4105 +#else
4107 +#define transport_is_bbb() 1
4108 +#define transport_is_cbi() 0
4109 +#define protocol_is_scsi() 1
4110 +#define backing_file_is_open(curlun) 1
4111 +#define NORMALLY_INIT __init
4113 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
4116 +struct lun {
4117 + struct file *filp;
4118 + loff_t file_length;
4119 + loff_t num_sectors;
4121 + unsigned int ro : 1;
4122 + unsigned int prevent_medium_removal : 1;
4123 + unsigned int registered : 1;
4125 + u32 sense_data;
4126 + u32 sense_data_info;
4127 + u32 unit_attention_data;
4129 +#define BUS_ID_SIZE 20
4130 + struct __lun_device {
4131 + char name[BUS_ID_SIZE];
4132 + void *driver_data;
4133 + } dev;
4137 +/* Big enough to hold our biggest descriptor */
4138 +#define EP0_BUFSIZE 256
4139 +#define DELAYED_STATUS (EP0_BUFSIZE + 999) // An impossibly large value
4141 +/* Number of buffers we will use. 2 is enough for double-buffering */
4142 +#define NUM_BUFFERS 2
4144 +enum fsg_buffer_state {
4145 + BUF_STATE_EMPTY = 0,
4146 + BUF_STATE_FULL,
4147 + BUF_STATE_BUSY
4150 +struct fsg_buffhd {
4151 + void *buf;
4152 + dma_addr_t dma;
4153 + volatile enum fsg_buffer_state state;
4154 + struct fsg_buffhd *next;
4156 + /* The NetChip 2280 is faster, and handles some protocol faults
4157 + * better, if we don't submit any short bulk-out read requests.
4158 + * So we will record the intended request length here. */
4159 + unsigned int bulk_out_intended_length;
4161 + struct usb_request *inreq;
4162 + volatile int inreq_busy;
4163 + struct usb_request *outreq;
4164 + volatile int outreq_busy;
4167 +enum fsg_state {
4168 + FSG_STATE_COMMAND_PHASE = -10, // This one isn't used anywhere
4169 + FSG_STATE_DATA_PHASE,
4170 + FSG_STATE_STATUS_PHASE,
4172 + FSG_STATE_IDLE = 0,
4173 + FSG_STATE_ABORT_BULK_OUT,
4174 + FSG_STATE_RESET,
4175 + FSG_STATE_INTERFACE_CHANGE,
4176 + FSG_STATE_CONFIG_CHANGE,
4177 + FSG_STATE_DISCONNECT,
4178 + FSG_STATE_EXIT,
4179 + FSG_STATE_TERMINATED
4182 +enum data_direction {
4183 + DATA_DIR_UNKNOWN = 0,
4184 + DATA_DIR_FROM_HOST,
4185 + DATA_DIR_TO_HOST,
4186 + DATA_DIR_NONE
4189 +struct fsg_dev {
4190 + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
4191 + spinlock_t lock;
4192 + struct usb_gadget *gadget;
4194 + /* filesem protects: backing files in use */
4195 + struct rw_semaphore filesem;
4197 + struct usb_ep *ep0; // Handy copy of gadget->ep0
4198 + struct usb_request *ep0req; // For control responses
4199 + volatile unsigned int ep0_req_tag;
4200 + const char *ep0req_name;
4202 + struct usb_request *intreq; // For interrupt responses
4203 + volatile int intreq_busy;
4204 + struct fsg_buffhd *intr_buffhd;
4206 + unsigned int bulk_out_maxpacket;
4207 + enum fsg_state state; // For exception handling
4208 + unsigned int exception_req_tag;
4210 + u8 config, new_config;
4212 + unsigned int running : 1;
4213 + unsigned int bulk_in_enabled : 1;
4214 + unsigned int bulk_out_enabled : 1;
4215 + unsigned int intr_in_enabled : 1;
4216 + unsigned int phase_error : 1;
4217 + unsigned int short_packet_received : 1;
4218 + unsigned int bad_lun_okay : 1;
4220 + unsigned long atomic_bitflags;
4221 +#define REGISTERED 0
4222 +#define CLEAR_BULK_HALTS 1
4224 + struct usb_ep *bulk_in;
4225 + struct usb_ep *bulk_out;
4226 + struct usb_ep *intr_in;
4228 + struct fsg_buffhd *next_buffhd_to_fill;
4229 + struct fsg_buffhd *next_buffhd_to_drain;
4230 + struct fsg_buffhd buffhds[NUM_BUFFERS];
4232 + wait_queue_head_t thread_wqh;
4233 + int thread_wakeup_needed;
4234 + struct completion thread_notifier;
4235 + int thread_pid;
4236 + struct task_struct *thread_task;
4237 + sigset_t thread_signal_mask;
4239 + int cmnd_size;
4240 + u8 cmnd[MAX_COMMAND_SIZE];
4241 + enum data_direction data_dir;
4242 + u32 data_size;
4243 + u32 data_size_from_cmnd;
4244 + u32 tag;
4245 + unsigned int lun;
4246 + u32 residue;
4247 + u32 usb_amount_left;
4249 + /* The CB protocol offers no way for a host to know when a command
4250 + * has completed. As a result the next command may arrive early,
4251 + * and we will still have to handle it. For that reason we need
4252 + * a buffer to store new commands when using CB (or CBI, which
4253 + * does not oblige a host to wait for command completion either). */
4254 + int cbbuf_cmnd_size;
4255 + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
4257 + unsigned int nluns;
4258 + struct lun *luns;
4259 + struct lun *curlun;
4262 +typedef void (*fsg_routine_t)(struct fsg_dev *);
4264 +static int inline exception_in_progress(struct fsg_dev *fsg)
4266 + return (fsg->state > FSG_STATE_IDLE);
4269 +/* Make bulk-out requests be divisible by the maxpacket size */
4270 +static void inline set_bulk_out_req_length(struct fsg_dev *fsg,
4271 + struct fsg_buffhd *bh, unsigned int length)
4273 + unsigned int rem;
4275 + bh->bulk_out_intended_length = length;
4276 + rem = length % fsg->bulk_out_maxpacket;
4277 + if (rem > 0)
4278 + length += fsg->bulk_out_maxpacket - rem;
4279 + bh->outreq->length = length;
4282 +static struct fsg_dev *the_fsg;
4283 +static struct usb_gadget_driver fsg_driver;
4285 +static void close_backing_file(struct lun *curlun);
4286 +static void close_all_backing_files(struct fsg_dev *fsg);
4289 +/*-------------------------------------------------------------------------*/
4291 +#ifdef DUMP_MSGS
4293 +static void dump_msg(struct fsg_dev *fsg, const char *label,
4294 + const u8 *buf, unsigned int length)
4296 + unsigned int start, num, i;
4297 + char line[52], *p;
4299 + if (length >= 512)
4300 + return;
4301 + DBG(fsg, "%s, length %u:\n", label, length);
4303 + start = 0;
4304 + while (length > 0) {
4305 + num = min(length, 16u);
4306 + p = line;
4307 + for (i = 0; i < num; ++i) {
4308 + if (i == 8)
4309 + *p++ = ' ';
4310 + sprintf(p, " %02x", buf[i]);
4311 + p += 3;
4313 + *p = 0;
4314 + printk(KERN_DEBUG "%6x: %s\n", start, line);
4315 + buf += num;
4316 + start += num;
4317 + length -= num;
4321 +static void inline dump_cdb(struct fsg_dev *fsg)
4324 +#else
4326 +static void inline dump_msg(struct fsg_dev *fsg, const char *label,
4327 + const u8 *buf, unsigned int length)
4330 +static void inline dump_cdb(struct fsg_dev *fsg)
4332 + int i;
4333 + char cmdbuf[3*MAX_COMMAND_SIZE + 1];
4335 + for (i = 0; i < fsg->cmnd_size; ++i)
4336 + sprintf(cmdbuf + i*3, " %02x", fsg->cmnd[i]);
4337 + VDBG(fsg, "SCSI CDB: %s\n", cmdbuf);
4340 +#endif /* DUMP_MSGS */
4343 +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
4345 + const char *name;
4347 + if (ep == fsg->bulk_in)
4348 + name = "bulk-in";
4349 + else if (ep == fsg->bulk_out)
4350 + name = "bulk-out";
4351 + else
4352 + name = ep->name;
4353 + DBG(fsg, "%s set halt\n", name);
4354 + return usb_ep_set_halt(ep);
4358 +/*-------------------------------------------------------------------------*/
4360 +/* Routines for unaligned data access */
4362 +static u16 inline get_be16(u8 *buf)
4364 + return ((u16) buf[0] << 8) | ((u16) buf[1]);
4367 +static u32 inline get_be32(u8 *buf)
4369 + return ((u32) buf[0] << 24) | ((u32) buf[1] << 16) |
4370 + ((u32) buf[2] << 8) | ((u32) buf[3]);
4373 +static void inline put_be16(u8 *buf, u16 val)
4375 + buf[0] = val >> 8;
4376 + buf[1] = val;
4379 +static void inline put_be32(u8 *buf, u32 val)
4381 + buf[0] = val >> 24;
4382 + buf[1] = val >> 16;
4383 + buf[2] = val >> 8;
4384 + buf[3] = val;
4388 +/*-------------------------------------------------------------------------*/
4391 + * DESCRIPTORS ... most are static, but strings and (full) configuration
4392 + * descriptors are built on demand. Also the (static) config and interface
4393 + * descriptors are adjusted during fsg_bind().
4394 + */
4395 +#define STRING_MANUFACTURER 1
4396 +#define STRING_PRODUCT 2
4397 +#define STRING_SERIAL 3
4399 +/* There is only one configuration. */
4400 +#define CONFIG_VALUE 1
4402 +static struct usb_device_descriptor
4403 +device_desc = {
4404 + .bLength = sizeof device_desc,
4405 + .bDescriptorType = USB_DT_DEVICE,
4407 + .bcdUSB = __constant_cpu_to_le16(0x0200),
4408 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
4410 + /* The next three values can be overridden by module parameters */
4411 + .idVendor = __constant_cpu_to_le16(DRIVER_VENDOR_ID),
4412 + .idProduct = __constant_cpu_to_le16(DRIVER_PRODUCT_ID),
4413 + .bcdDevice = __constant_cpu_to_le16(0xffff),
4415 + .iManufacturer = STRING_MANUFACTURER,
4416 + .iProduct = STRING_PRODUCT,
4417 + .iSerialNumber = STRING_SERIAL,
4418 + .bNumConfigurations = 1,
4421 +static struct usb_config_descriptor
4422 +config_desc = {
4423 + .bLength = sizeof config_desc,
4424 + .bDescriptorType = USB_DT_CONFIG,
4426 + /* wTotalLength computed by usb_gadget_config_buf() */
4427 + .bNumInterfaces = 1,
4428 + .bConfigurationValue = CONFIG_VALUE,
4429 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
4430 + .bMaxPower = 1, // self-powered
4433 +/* There is only one interface. */
4435 +static struct usb_interface_descriptor
4436 +intf_desc = {
4437 + .bLength = sizeof intf_desc,
4438 + .bDescriptorType = USB_DT_INTERFACE,
4440 + .bNumEndpoints = 2, // Adjusted during fsg_bind()
4441 + .bInterfaceClass = USB_CLASS_MASS_STORAGE,
4442 + .bInterfaceSubClass = USB_SC_SCSI, // Adjusted during fsg_bind()
4443 + .bInterfaceProtocol = USB_PR_BULK, // Adjusted during fsg_bind()
4446 +/* Three full-speed endpoint descriptors: bulk-in, bulk-out,
4447 + * and interrupt-in. */
4449 +static struct usb_endpoint_descriptor
4450 +fs_bulk_in_desc = {
4451 + .bLength = USB_DT_ENDPOINT_SIZE,
4452 + .bDescriptorType = USB_DT_ENDPOINT,
4454 + .bEndpointAddress = USB_DIR_IN,
4455 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
4456 + /* wMaxPacketSize set by autoconfiguration */
4459 +static struct usb_endpoint_descriptor
4460 +fs_bulk_out_desc = {
4461 + .bLength = USB_DT_ENDPOINT_SIZE,
4462 + .bDescriptorType = USB_DT_ENDPOINT,
4464 + .bEndpointAddress = USB_DIR_OUT,
4465 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
4466 + /* wMaxPacketSize set by autoconfiguration */
4469 +static struct usb_endpoint_descriptor
4470 +fs_intr_in_desc = {
4471 + .bLength = USB_DT_ENDPOINT_SIZE,
4472 + .bDescriptorType = USB_DT_ENDPOINT,
4474 + .bEndpointAddress = USB_DIR_IN,
4475 + .bmAttributes = USB_ENDPOINT_XFER_INT,
4476 + .wMaxPacketSize = __constant_cpu_to_le16(2),
4477 + .bInterval = 32, // frames -> 32 ms
4480 +static const struct usb_descriptor_header *fs_function[] = {
4481 + (struct usb_descriptor_header *) &intf_desc,
4482 + (struct usb_descriptor_header *) &fs_bulk_in_desc,
4483 + (struct usb_descriptor_header *) &fs_bulk_out_desc,
4484 + (struct usb_descriptor_header *) &fs_intr_in_desc,
4485 + NULL,
4489 +#ifdef CONFIG_USB_GADGET_DUALSPEED
4492 + * USB 2.0 devices need to expose both high speed and full speed
4493 + * descriptors, unless they only run at full speed.
4495 + * That means alternate endpoint descriptors (bigger packets)
4496 + * and a "device qualifier" ... plus more construction options
4497 + * for the config descriptor.
4498 + */
4499 +static struct usb_qualifier_descriptor
4500 +dev_qualifier = {
4501 + .bLength = sizeof dev_qualifier,
4502 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
4504 + .bcdUSB = __constant_cpu_to_le16(0x0200),
4505 + .bDeviceClass = USB_CLASS_PER_INTERFACE,
4507 + .bNumConfigurations = 1,
4510 +static struct usb_endpoint_descriptor
4511 +hs_bulk_in_desc = {
4512 + .bLength = USB_DT_ENDPOINT_SIZE,
4513 + .bDescriptorType = USB_DT_ENDPOINT,
4515 + /* bEndpointAddress copied from fs_bulk_in_desc during fsg_bind() */
4516 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
4517 + .wMaxPacketSize = __constant_cpu_to_le16(512),
4520 +static struct usb_endpoint_descriptor
4521 +hs_bulk_out_desc = {
4522 + .bLength = USB_DT_ENDPOINT_SIZE,
4523 + .bDescriptorType = USB_DT_ENDPOINT,
4525 + /* bEndpointAddress copied from fs_bulk_out_desc during fsg_bind() */
4526 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
4527 + .wMaxPacketSize = __constant_cpu_to_le16(512),
4528 + .bInterval = 1, // NAK every 1 uframe
4531 +static struct usb_endpoint_descriptor
4532 +hs_intr_in_desc = {
4533 + .bLength = USB_DT_ENDPOINT_SIZE,
4534 + .bDescriptorType = USB_DT_ENDPOINT,
4536 + /* bEndpointAddress copied from fs_intr_in_desc during fsg_bind() */
4537 + .bmAttributes = USB_ENDPOINT_XFER_INT,
4538 + .wMaxPacketSize = __constant_cpu_to_le16(2),
4539 + .bInterval = 9, // 2**(9-1) = 256 uframes -> 32 ms
4542 +static const struct usb_descriptor_header *hs_function[] = {
4543 + (struct usb_descriptor_header *) &intf_desc,
4544 + (struct usb_descriptor_header *) &hs_bulk_in_desc,
4545 + (struct usb_descriptor_header *) &hs_bulk_out_desc,
4546 + (struct usb_descriptor_header *) &hs_intr_in_desc,
4547 + NULL,
4550 +/* Maxpacket and other transfer characteristics vary by speed. */
4551 +#define ep_desc(g,fs,hs) (((g)->speed==USB_SPEED_HIGH) ? (hs) : (fs))
4553 +#else
4555 +/* If there's no high speed support, always use the full-speed descriptor. */
4556 +#define ep_desc(g,fs,hs) fs
4558 +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
4561 +/* The CBI specification limits the serial string to 12 uppercase hexadecimal
4562 + * characters. */
4563 +static char manufacturer[40];
4564 +static char serial[13];
4566 +/* Static strings, in UTF-8 (for simplicity we use only ASCII characters) */
4567 +static struct usb_string strings[] = {
4568 + {STRING_MANUFACTURER, manufacturer},
4569 + {STRING_PRODUCT, longname},
4570 + {STRING_SERIAL, serial},
4571 + {}
4574 +static struct usb_gadget_strings stringtab = {
4575 + .language = 0x0409, // en-us
4576 + .strings = strings,
4581 + * Config descriptors must agree with the code that sets configurations
4582 + * and with code managing interfaces and their altsettings. They must
4583 + * also handle different speeds and other-speed requests.
4584 + */
4585 +static int populate_config_buf(enum usb_device_speed speed,
4586 + u8 *buf, u8 type, unsigned index)
4588 + int len;
4589 + const struct usb_descriptor_header **function;
4591 + if (index > 0)
4592 + return -EINVAL;
4594 +#ifdef CONFIG_USB_GADGET_DUALSPEED
4595 + if (type == USB_DT_OTHER_SPEED_CONFIG)
4596 + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
4597 + if (speed == USB_SPEED_HIGH)
4598 + function = hs_function;
4599 + else
4600 +#endif
4601 + function = fs_function;
4603 + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
4604 + if (len < 0)
4605 + return len;
4606 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
4607 + return len;
4611 +/*-------------------------------------------------------------------------*/
4613 +/* These routines may be called in process context or in_irq */
4615 +static void wakeup_thread(struct fsg_dev *fsg)
4617 + /* Tell the main thread that something has happened */
4618 + fsg->thread_wakeup_needed = 1;
4619 + wake_up_all(&fsg->thread_wqh);
4623 +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
4625 + unsigned long flags;
4626 + struct task_struct *thread_task;
4628 + /* Do nothing if a higher-priority exception is already in progress.
4629 + * If a lower-or-equal priority exception is in progress, preempt it
4630 + * and notify the main thread by sending it a signal. */
4631 + spin_lock_irqsave(&fsg->lock, flags);
4632 + if (fsg->state <= new_state) {
4633 + fsg->exception_req_tag = fsg->ep0_req_tag;
4634 + fsg->state = new_state;
4635 + thread_task = fsg->thread_task;
4636 + if (thread_task)
4637 + send_sig_info(SIGUSR1, (void *) 1L, thread_task);
4639 + spin_unlock_irqrestore(&fsg->lock, flags);
4643 +/*-------------------------------------------------------------------------*/
4645 +/* The disconnect callback and ep0 routines. These always run in_irq,
4646 + * except that ep0_queue() is called in the main thread to acknowledge
4647 + * completion of various requests: set config, set interface, and
4648 + * Bulk-only device reset. */
4650 +static void fsg_disconnect(struct usb_gadget *gadget)
4652 + struct fsg_dev *fsg = get_gadget_data(gadget);
4654 + DBG(fsg, "disconnect or port reset\n");
4655 + raise_exception(fsg, FSG_STATE_DISCONNECT);
4659 +static int ep0_queue(struct fsg_dev *fsg)
4661 + int rc;
4663 + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
4664 + if (rc != 0 && rc != -ESHUTDOWN) {
4666 + /* We can't do much more than wait for a reset */
4667 + WARN(fsg, "error in submission: %s --> %d\n",
4668 + fsg->ep0->name, rc);
4670 + return rc;
4673 +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
4675 + struct fsg_dev *fsg = (struct fsg_dev *) ep->driver_data;
4677 + if (req->actual > 0)
4678 + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
4679 + if (req->status || req->actual != req->length)
4680 + DBG(fsg, "%s --> %d, %u/%u\n", __FUNCTION__,
4681 + req->status, req->actual, req->length);
4682 + if (req->status == -ECONNRESET) // Request was cancelled
4683 + usb_ep_fifo_flush(ep);
4685 + if (req->status == 0 && req->context)
4686 + ((fsg_routine_t) (req->context))(fsg);
4690 +/*-------------------------------------------------------------------------*/
4692 +/* Bulk and interrupt endpoint completion handlers.
4693 + * These always run in_irq. */
4695 +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
4697 + struct fsg_dev *fsg = (struct fsg_dev *) ep->driver_data;
4698 + struct fsg_buffhd *bh = (struct fsg_buffhd *) req->context;
4700 + if (req->status || req->actual != req->length)
4701 + DBG(fsg, "%s --> %d, %u/%u\n", __FUNCTION__,
4702 + req->status, req->actual, req->length);
4703 + if (req->status == -ECONNRESET) // Request was cancelled
4704 + usb_ep_fifo_flush(ep);
4706 + /* Hold the lock while we update the request and buffer states */
4707 + spin_lock(&fsg->lock);
4708 + bh->inreq_busy = 0;
4709 + bh->state = BUF_STATE_EMPTY;
4710 + spin_unlock(&fsg->lock);
4711 + wakeup_thread(fsg);
4714 +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
4716 + struct fsg_dev *fsg = (struct fsg_dev *) ep->driver_data;
4717 + struct fsg_buffhd *bh = (struct fsg_buffhd *) req->context;
4719 + dump_msg(fsg, "bulk-out", req->buf, req->actual);
4720 + if (req->status || req->actual != bh->bulk_out_intended_length)
4721 + DBG(fsg, "%s --> %d, %u/%u\n", __FUNCTION__,
4722 + req->status, req->actual,
4723 + bh->bulk_out_intended_length);
4724 + if (req->status == -ECONNRESET) // Request was cancelled
4725 + usb_ep_fifo_flush(ep);
4727 + /* Hold the lock while we update the request and buffer states */
4728 + spin_lock(&fsg->lock);
4729 + bh->outreq_busy = 0;
4730 + bh->state = BUF_STATE_FULL;
4731 + spin_unlock(&fsg->lock);
4732 + wakeup_thread(fsg);
4735 +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
4737 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
4738 + struct fsg_dev *fsg = (struct fsg_dev *) ep->driver_data;
4739 + struct fsg_buffhd *bh = (struct fsg_buffhd *) req->context;
4741 + if (req->status || req->actual != req->length)
4742 + DBG(fsg, "%s --> %d, %u/%u\n", __FUNCTION__,
4743 + req->status, req->actual, req->length);
4744 + if (req->status == -ECONNRESET) // Request was cancelled
4745 + usb_ep_fifo_flush(ep);
4747 + /* Hold the lock while we update the request and buffer states */
4748 + spin_lock(&fsg->lock);
4749 + fsg->intreq_busy = 0;
4750 + bh->state = BUF_STATE_EMPTY;
4751 + spin_unlock(&fsg->lock);
4752 + wakeup_thread(fsg);
4753 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
4757 +/*-------------------------------------------------------------------------*/
4759 +/* Ep0 class-specific handlers. These always run in_irq. */
4761 +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
4763 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
4764 + struct usb_request *req = fsg->ep0req;
4765 + static u8 cbi_reset_cmnd[6] = {
4766 + SC_SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
4768 + /* Error in command transfer? */
4769 + if (req->status || req->length != req->actual ||
4770 + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
4772 + /* Not all controllers allow a protocol stall after
4773 + * receiving control-out data, but we'll try anyway. */
4774 + fsg_set_halt(fsg, fsg->ep0);
4775 + return; // Wait for reset
4778 + /* Is it the special reset command? */
4779 + if (req->actual >= sizeof cbi_reset_cmnd &&
4780 + memcmp(req->buf, cbi_reset_cmnd,
4781 + sizeof cbi_reset_cmnd) == 0) {
4783 + /* Raise an exception to stop the current operation
4784 + * and reinitialize our state. */
4785 + DBG(fsg, "cbi reset request\n");
4786 + raise_exception(fsg, FSG_STATE_RESET);
4787 + return;
4790 + VDBG(fsg, "CB[I] accept device-specific command\n");
4791 + spin_lock(&fsg->lock);
4793 + /* Save the command for later */
4794 + if (fsg->cbbuf_cmnd_size)
4795 + WARN(fsg, "CB[I] overwriting previous command\n");
4796 + fsg->cbbuf_cmnd_size = req->actual;
4797 + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
4799 + spin_unlock(&fsg->lock);
4800 + wakeup_thread(fsg);
4801 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
4805 +static int class_setup_req(struct fsg_dev *fsg,
4806 + const struct usb_ctrlrequest *ctrl)
4808 + struct usb_request *req = fsg->ep0req;
4809 + int value = -EOPNOTSUPP;
4811 + if (!fsg->config)
4812 + return value;
4814 + /* Handle Bulk-only class-specific requests */
4815 + if (transport_is_bbb()) {
4816 + switch (ctrl->bRequest) {
4818 + case USB_BULK_RESET_REQUEST:
4819 + if (ctrl->bRequestType != (USB_DIR_OUT |
4820 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
4821 + break;
4822 + if (ctrl->wIndex != 0) {
4823 + value = -EDOM;
4824 + break;
4827 + /* Raise an exception to stop the current operation
4828 + * and reinitialize our state. */
4829 + DBG(fsg, "bulk reset request\n");
4830 + raise_exception(fsg, FSG_STATE_RESET);
4831 + value = DELAYED_STATUS;
4832 + break;
4834 + case USB_BULK_GET_MAX_LUN_REQUEST:
4835 + if (ctrl->bRequestType != (USB_DIR_IN |
4836 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
4837 + break;
4838 + if (ctrl->wIndex != 0) {
4839 + value = -EDOM;
4840 + break;
4842 + VDBG(fsg, "get max LUN\n");
4843 + *(u8 *) req->buf = fsg->nluns - 1;
4844 + value = min(ctrl->wLength, (u16) 1);
4845 + break;
4849 + /* Handle CBI class-specific requests */
4850 + else {
4851 + switch (ctrl->bRequest) {
4853 + case USB_CBI_ADSC_REQUEST:
4854 + if (ctrl->bRequestType != (USB_DIR_OUT |
4855 + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
4856 + break;
4857 + if (ctrl->wIndex != 0) {
4858 + value = -EDOM;
4859 + break;
4861 + if (ctrl->wLength > MAX_COMMAND_SIZE) {
4862 + value = -EOVERFLOW;
4863 + break;
4865 + value = ctrl->wLength;
4866 + fsg->ep0req->context = received_cbi_adsc;
4867 + break;
4871 + if (value == -EOPNOTSUPP)
4872 + VDBG(fsg,
4873 + "unknown class-specific control req "
4874 + "%02x.%02x v%04x i%04x l%u\n",
4875 + ctrl->bRequestType, ctrl->bRequest,
4876 + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
4877 + return value;
4881 +/*-------------------------------------------------------------------------*/
4883 +/* Ep0 standard request handlers. These always run in_irq. */
4885 +static int standard_setup_req(struct fsg_dev *fsg,
4886 + const struct usb_ctrlrequest *ctrl)
4888 + struct usb_request *req = fsg->ep0req;
4889 + int value = -EOPNOTSUPP;
4891 + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
4892 + * but config change events will also reconfigure hardware. */
4893 + switch (ctrl->bRequest) {
4895 + case USB_REQ_GET_DESCRIPTOR:
4896 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
4897 + USB_RECIP_DEVICE))
4898 + break;
4899 + switch (ctrl->wValue >> 8) {
4901 + case USB_DT_DEVICE:
4902 + VDBG(fsg, "get device descriptor\n");
4903 + value = min(ctrl->wLength, (u16) sizeof device_desc);
4904 + memcpy(req->buf, &device_desc, value);
4905 + break;
4906 +#ifdef CONFIG_USB_GADGET_DUALSPEED
4907 + case USB_DT_DEVICE_QUALIFIER:
4908 + VDBG(fsg, "get device qualifier\n");
4909 + if (!fsg->gadget->is_dualspeed)
4910 + break;
4911 + value = min(ctrl->wLength, (u16) sizeof dev_qualifier);
4912 + memcpy(req->buf, &dev_qualifier, value);
4913 + break;
4915 + case USB_DT_OTHER_SPEED_CONFIG:
4916 + VDBG(fsg, "get other-speed config descriptor\n");
4917 + if (!fsg->gadget->is_dualspeed)
4918 + break;
4919 + goto get_config;
4920 +#endif
4921 + case USB_DT_CONFIG:
4922 + VDBG(fsg, "get configuration descriptor\n");
4923 +#ifdef CONFIG_USB_GADGET_DUALSPEED
4924 + get_config:
4925 +#endif
4926 + value = populate_config_buf(fsg->gadget->speed,
4927 + req->buf,
4928 + ctrl->wValue >> 8,
4929 + ctrl->wValue & 0xff);
4930 + if (value >= 0)
4931 + value = min(ctrl->wLength, (u16) value);
4932 + break;
4934 + case USB_DT_STRING:
4935 + VDBG(fsg, "get string descriptor\n");
4937 + /* wIndex == language code */
4938 + value = usb_gadget_get_string(&stringtab,
4939 + ctrl->wValue & 0xff, req->buf);
4940 + if (value >= 0)
4941 + value = min(ctrl->wLength, (u16) value);
4942 + break;
4944 + break;
4946 + /* One config, two speeds */
4947 + case USB_REQ_SET_CONFIGURATION:
4948 + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
4949 + USB_RECIP_DEVICE))
4950 + break;
4951 + VDBG(fsg, "set configuration\n");
4952 + if (ctrl->wValue == CONFIG_VALUE || ctrl->wValue == 0) {
4953 + fsg->new_config = ctrl->wValue;
4955 + /* Raise an exception to wipe out previous transaction
4956 + * state (queued bufs, etc) and set the new config. */
4957 + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
4958 + value = DELAYED_STATUS;
4960 + break;
4961 + case USB_REQ_GET_CONFIGURATION:
4962 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
4963 + USB_RECIP_DEVICE))
4964 + break;
4965 + VDBG(fsg, "get configuration\n");
4966 + *(u8 *) req->buf = fsg->config;
4967 + value = min(ctrl->wLength, (u16) 1);
4968 + break;
4970 + case USB_REQ_SET_INTERFACE:
4971 + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
4972 + USB_RECIP_INTERFACE))
4973 + break;
4974 + if (fsg->config && ctrl->wIndex == 0) {
4976 + /* Raise an exception to wipe out previous transaction
4977 + * state (queued bufs, etc) and install the new
4978 + * interface altsetting. */
4979 + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
4980 + value = DELAYED_STATUS;
4982 + break;
4983 + case USB_REQ_GET_INTERFACE:
4984 + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
4985 + USB_RECIP_INTERFACE))
4986 + break;
4987 + if (!fsg->config)
4988 + break;
4989 + if (ctrl->wIndex != 0) {
4990 + value = -EDOM;
4991 + break;
4993 + VDBG(fsg, "get interface\n");
4994 + *(u8 *) req->buf = 0;
4995 + value = min(ctrl->wLength, (u16) 1);
4996 + break;
4998 + default:
4999 + VDBG(fsg,
5000 + "unknown control req %02x.%02x v%04x i%04x l%u\n",
5001 + ctrl->bRequestType, ctrl->bRequest,
5002 + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
5005 + return value;
5009 +static int fsg_setup(struct usb_gadget *gadget,
5010 + const struct usb_ctrlrequest *ctrl)
5012 + struct fsg_dev *fsg = get_gadget_data(gadget);
5013 + int rc;
5015 + ++fsg->ep0_req_tag; // Record arrival of a new request
5016 + fsg->ep0req->context = NULL;
5017 + fsg->ep0req->length = 0;
5018 + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
5020 + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
5021 + rc = class_setup_req(fsg, ctrl);
5022 + else
5023 + rc = standard_setup_req(fsg, ctrl);
5025 + /* Respond with data/status or defer until later? */
5026 + if (rc >= 0 && rc != DELAYED_STATUS) {
5027 + fsg->ep0req->length = rc;
5028 + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
5029 + "ep0-in" : "ep0-out");
5030 + rc = ep0_queue(fsg);
5033 + /* Device either stalls (rc < 0) or reports success */
5034 + return rc;
5038 +/*-------------------------------------------------------------------------*/
5040 +/* All the following routines run in process context */
5043 +/* Use this for bulk or interrupt transfers, not ep0 */
5044 +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
5045 + struct usb_request *req, volatile int *pbusy,
5046 + volatile enum fsg_buffer_state *state)
5048 + int rc;
5050 + if (ep == fsg->bulk_in)
5051 + dump_msg(fsg, "bulk-in", req->buf, req->length);
5052 + else if (ep == fsg->intr_in)
5053 + dump_msg(fsg, "intr-in", req->buf, req->length);
5054 + *pbusy = 1;
5055 + *state = BUF_STATE_BUSY;
5056 + rc = usb_ep_queue(ep, req, GFP_KERNEL);
5057 + if (rc != 0) {
5058 + *pbusy = 0;
5059 + *state = BUF_STATE_EMPTY;
5061 + /* We can't do much more than wait for a reset */
5063 + /* Note: currently the net2280 driver fails zero-length
5064 + * submissions if DMA is enabled. */
5065 + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
5066 + req->length == 0))
5067 + WARN(fsg, "error in submission: %s --> %d\n",
5068 + ep->name, rc);
5073 +static int sleep_thread(struct fsg_dev *fsg)
5075 + int rc;
5077 + /* Wait until a signal arrives or we are woken up */
5078 + rc = wait_event_interruptible(fsg->thread_wqh,
5079 + fsg->thread_wakeup_needed);
5080 + fsg->thread_wakeup_needed = 0;
5081 + return (rc ? -EINTR : 0);
5085 +/*-------------------------------------------------------------------------*/
5087 +static int do_read(struct fsg_dev *fsg)
5089 + struct lun *curlun = fsg->curlun;
5090 + u32 lba;
5091 + struct fsg_buffhd *bh;
5092 + int rc;
5093 + u32 amount_left;
5094 + loff_t file_offset, file_offset_tmp;
5095 + unsigned int amount;
5096 + unsigned int partial_page;
5097 + ssize_t nread;
5099 + /* Get the starting Logical Block Address and check that it's
5100 + * not too big */
5101 + if (fsg->cmnd[0] == SC_READ_6)
5102 + lba = (fsg->cmnd[1] << 16) | get_be16(&fsg->cmnd[2]);
5103 + else {
5104 + lba = get_be32(&fsg->cmnd[2]);
5106 + /* We allow DPO (Disable Page Out = don't save data in the
5107 + * cache) and FUA (Force Unit Access = don't read from the
5108 + * cache), but we don't implement them. */
5109 + if ((fsg->cmnd[1] & ~0x18) != 0) {
5110 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
5111 + return -EINVAL;
5114 + if (lba >= curlun->num_sectors) {
5115 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
5116 + return -EINVAL;
5118 + file_offset = ((loff_t) lba) << 9;
5120 + /* Carry out the file reads */
5121 + amount_left = fsg->data_size_from_cmnd;
5122 + if (unlikely(amount_left == 0))
5123 + return -EIO; // No default reply
5125 + for (;;) {
5127 + /* Figure out how much we need to read:
5128 + * Try to read the remaining amount.
5129 + * But don't read more than the buffer size.
5130 + * And don't try to read past the end of the file.
5131 + * Finally, if we're not at a page boundary, don't read past
5132 + * the next page.
5133 + * If this means reading 0 then we were asked to read past
5134 + * the end of file. */
5135 + amount = min((unsigned int) amount_left, mod_data.buflen);
5136 + amount = min((loff_t) amount,
5137 + curlun->file_length - file_offset);
5138 + partial_page = file_offset & (PAGE_CACHE_SIZE - 1);
5139 + if (partial_page > 0)
5140 + amount = min(amount, (unsigned int) PAGE_CACHE_SIZE -
5141 + partial_page);
5143 + /* Wait for the next buffer to become available */
5144 + bh = fsg->next_buffhd_to_fill;
5145 + while (bh->state != BUF_STATE_EMPTY) {
5146 + if ((rc = sleep_thread(fsg)) != 0)
5147 + return rc;
5150 + /* If we were asked to read past the end of file,
5151 + * end with an empty buffer. */
5152 + if (amount == 0) {
5153 + curlun->sense_data =
5154 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
5155 + curlun->sense_data_info = file_offset >> 9;
5156 + bh->inreq->length = 0;
5157 + bh->state = BUF_STATE_FULL;
5158 + break;
5161 + /* Perform the read */
5162 + file_offset_tmp = file_offset;
5163 + nread = curlun->filp->f_op->read(curlun->filp,
5164 + (char *) bh->buf,
5165 + amount, &file_offset_tmp);
5166 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
5167 + (unsigned long long) file_offset,
5168 + (int) nread);
5169 + if (signal_pending(current))
5170 + return -EINTR;
5172 + if (nread < 0) {
5173 + LDBG(curlun, "error in file read: %d\n",
5174 + (int) nread);
5175 + nread = 0;
5176 + } else if (nread < amount) {
5177 + LDBG(curlun, "partial file read: %d/%u\n",
5178 + (int) nread, amount);
5179 + nread -= (nread & 511); // Round down to a block
5181 + file_offset += nread;
5182 + amount_left -= nread;
5183 + fsg->residue -= nread;
5184 + bh->inreq->length = nread;
5185 + bh->state = BUF_STATE_FULL;
5187 + /* If an error occurred, report it and its position */
5188 + if (nread < amount) {
5189 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
5190 + curlun->sense_data_info = file_offset >> 9;
5191 + break;
5194 + if (amount_left == 0)
5195 + break; // No more left to read
5197 + /* Send this buffer and go read some more */
5198 + bh->inreq->zero = 0;
5199 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
5200 + &bh->inreq_busy, &bh->state);
5201 + fsg->next_buffhd_to_fill = bh->next;
5204 + return -EIO; // No default reply
5208 +/*-------------------------------------------------------------------------*/
5210 +static int do_write(struct fsg_dev *fsg)
5212 + struct lun *curlun = fsg->curlun;
5213 + u32 lba;
5214 + struct fsg_buffhd *bh;
5215 + int get_some_more;
5216 + u32 amount_left_to_req, amount_left_to_write;
5217 + loff_t usb_offset, file_offset, file_offset_tmp;
5218 + unsigned int amount;
5219 + unsigned int partial_page;
5220 + ssize_t nwritten;
5221 + int rc;
5223 + if (curlun->ro) {
5224 + curlun->sense_data = SS_WRITE_PROTECTED;
5225 + return -EINVAL;
5227 + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
5229 + /* Get the starting Logical Block Address and check that it's
5230 + * not too big */
5231 + if (fsg->cmnd[0] == SC_WRITE_6)
5232 + lba = (fsg->cmnd[1] << 16) | get_be16(&fsg->cmnd[2]);
5233 + else {
5234 + lba = get_be32(&fsg->cmnd[2]);
5236 + /* We allow DPO (Disable Page Out = don't save data in the
5237 + * cache) and FUA (Force Unit Access = write directly to the
5238 + * medium). We don't implement DPO; we implement FUA by
5239 + * performing synchronous output. */
5240 + if ((fsg->cmnd[1] & ~0x18) != 0) {
5241 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
5242 + return -EINVAL;
5244 + if (fsg->cmnd[1] & 0x08) // FUA
5245 + curlun->filp->f_flags |= O_SYNC;
5247 + if (lba >= curlun->num_sectors) {
5248 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
5249 + return -EINVAL;
5252 + /* Carry out the file writes */
5253 + get_some_more = 1;
5254 + file_offset = usb_offset = ((loff_t) lba) << 9;
5255 + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
5257 + while (amount_left_to_write > 0) {
5259 + /* Queue a request for more data from the host */
5260 + bh = fsg->next_buffhd_to_fill;
5261 + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
5263 + /* Figure out how much we want to get:
5264 + * Try to get the remaining amount.
5265 + * But don't get more than the buffer size.
5266 + * And don't try to go past the end of the file.
5267 + * If we're not at a page boundary,
5268 + * don't go past the next page.
5269 + * If this means getting 0, then we were asked
5270 + * to write past the end of file.
5271 + * Finally, round down to a block boundary. */
5272 + amount = min(amount_left_to_req, mod_data.buflen);
5273 + amount = min((loff_t) amount, curlun->file_length -
5274 + usb_offset);
5275 + partial_page = usb_offset & (PAGE_CACHE_SIZE - 1);
5276 + if (partial_page > 0)
5277 + amount = min(amount,
5278 + (unsigned int) PAGE_CACHE_SIZE - partial_page);
5280 + if (amount == 0) {
5281 + get_some_more = 0;
5282 + curlun->sense_data =
5283 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
5284 + curlun->sense_data_info = usb_offset >> 9;
5285 + continue;
5287 + amount -= (amount & 511);
5288 + if (amount == 0) {
5290 + /* Why were we were asked to transfer a
5291 + * partial block? */
5292 + get_some_more = 0;
5293 + continue;
5296 + /* Get the next buffer */
5297 + usb_offset += amount;
5298 + fsg->usb_amount_left -= amount;
5299 + amount_left_to_req -= amount;
5300 + if (amount_left_to_req == 0)
5301 + get_some_more = 0;
5303 + /* amount is always divisible by 512, hence by
5304 + * the bulk-out maxpacket size */
5305 + bh->outreq->length = bh->bulk_out_intended_length =
5306 + amount;
5307 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
5308 + &bh->outreq_busy, &bh->state);
5309 + fsg->next_buffhd_to_fill = bh->next;
5310 + continue;
5313 + /* Write the received data to the backing file */
5314 + bh = fsg->next_buffhd_to_drain;
5315 + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
5316 + break; // We stopped early
5317 + if (bh->state == BUF_STATE_FULL) {
5318 + fsg->next_buffhd_to_drain = bh->next;
5319 + bh->state = BUF_STATE_EMPTY;
5321 + /* Did something go wrong with the transfer? */
5322 + if (bh->outreq->status != 0) {
5323 + curlun->sense_data = SS_COMMUNICATION_FAILURE;
5324 + curlun->sense_data_info = file_offset >> 9;
5325 + break;
5328 + amount = bh->outreq->actual;
5329 + if (curlun->file_length - file_offset < amount) {
5330 + LERROR(curlun,
5331 + "write %u @ %llu beyond end %llu\n",
5332 + amount, (unsigned long long) file_offset,
5333 + (unsigned long long) curlun->file_length);
5334 + amount = curlun->file_length - file_offset;
5337 + /* Perform the write */
5338 + file_offset_tmp = file_offset;
5339 + nwritten = curlun->filp->f_op->write(curlun->filp,
5340 + (char *) bh->buf,
5341 + amount, &file_offset_tmp);
5342 + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
5343 + (unsigned long long) file_offset,
5344 + (int) nwritten);
5345 + if (signal_pending(current))
5346 + return -EINTR; // Interrupted!
5348 + if (nwritten < 0) {
5349 + LDBG(curlun, "error in file write: %d\n",
5350 + (int) nwritten);
5351 + nwritten = 0;
5352 + } else if (nwritten < amount) {
5353 + LDBG(curlun, "partial file write: %d/%u\n",
5354 + (int) nwritten, amount);
5355 + nwritten -= (nwritten & 511);
5356 + // Round down to a block
5358 + file_offset += nwritten;
5359 + amount_left_to_write -= nwritten;
5360 + fsg->residue -= nwritten;
5362 + /* If an error occurred, report it and its position */
5363 + if (nwritten < amount) {
5364 + curlun->sense_data = SS_WRITE_ERROR;
5365 + curlun->sense_data_info = file_offset >> 9;
5366 + break;
5369 + /* Did the host decide to stop early? */
5370 + if (bh->outreq->actual != bh->outreq->length) {
5371 + fsg->short_packet_received = 1;
5372 + break;
5374 + continue;
5377 + /* Wait for something to happen */
5378 + if ((rc = sleep_thread(fsg)) != 0)
5379 + return rc;
5382 + return -EIO; // No default reply
5386 +/*-------------------------------------------------------------------------*/
5388 +/* Sync the file data, don't bother with the metadata.
5389 + * This code was copied from fs/buffer.c:sys_fdatasync(). */
5390 +static int fsync_sub(struct lun *curlun)
5392 + struct file *filp = curlun->filp;
5393 + struct inode *inode;
5394 + int rc, err;
5396 + if (curlun->ro || !filp)
5397 + return 0;
5398 + if (!filp->f_op->fsync)
5399 + return -EINVAL;
5401 + inode = filp->f_dentry->d_inode;
5402 + down(&inode->i_sem);
5403 + rc = filemap_fdatasync(inode->i_mapping);
5404 + err = filp->f_op->fsync(filp, filp->f_dentry, 1);
5405 + if (!rc)
5406 + rc = err;
5407 + err = filemap_fdatawait(inode->i_mapping);
5408 + if (!rc)
5409 + rc = err;
5410 + up(&inode->i_sem);
5411 + VLDBG(curlun, "fdatasync -> %d\n", rc);
5412 + return rc;
5415 +static void fsync_all(struct fsg_dev *fsg)
5417 + int i;
5419 + for (i = 0; i < fsg->nluns; ++i)
5420 + fsync_sub(&fsg->luns[i]);
5423 +static int do_synchronize_cache(struct fsg_dev *fsg)
5425 + struct lun *curlun = fsg->curlun;
5426 + int rc;
5428 + /* We ignore the requested LBA and write out all file's
5429 + * dirty data buffers. */
5430 + rc = fsync_sub(curlun);
5431 + if (rc)
5432 + curlun->sense_data = SS_WRITE_ERROR;
5433 + return 0;
5437 +/*-------------------------------------------------------------------------*/
5439 +static void invalidate_sub(struct lun *curlun)
5441 + struct file *filp = curlun->filp;
5442 + struct inode *inode = filp->f_dentry->d_inode;
5444 + invalidate_inode_pages(inode);
5447 +static int do_verify(struct fsg_dev *fsg)
5449 + struct lun *curlun = fsg->curlun;
5450 + u32 lba;
5451 + u32 verification_length;
5452 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
5453 + loff_t file_offset, file_offset_tmp;
5454 + u32 amount_left;
5455 + unsigned int amount;
5456 + ssize_t nread;
5458 + /* Get the starting Logical Block Address and check that it's
5459 + * not too big */
5460 + lba = get_be32(&fsg->cmnd[2]);
5461 + if (lba >= curlun->num_sectors) {
5462 + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
5463 + return -EINVAL;
5466 + /* We allow DPO (Disable Page Out = don't save data in the
5467 + * cache) but we don't implement it. */
5468 + if ((fsg->cmnd[1] & ~0x10) != 0) {
5469 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
5470 + return -EINVAL;
5473 + verification_length = get_be16(&fsg->cmnd[7]);
5474 + if (unlikely(verification_length == 0))
5475 + return -EIO; // No default reply
5477 + /* Prepare to carry out the file verify */
5478 + amount_left = verification_length << 9;
5479 + file_offset = ((loff_t) lba) << 9;
5481 + /* Write out all the dirty buffers before invalidating them */
5482 + fsync_sub(curlun);
5483 + if (signal_pending(current))
5484 + return -EINTR;
5486 + invalidate_sub(curlun);
5487 + if (signal_pending(current))
5488 + return -EINTR;
5490 + /* Just try to read the requested blocks */
5491 + while (amount_left > 0) {
5493 + /* Figure out how much we need to read:
5494 + * Try to read the remaining amount, but not more than
5495 + * the buffer size.
5496 + * And don't try to read past the end of the file.
5497 + * If this means reading 0 then we were asked to read
5498 + * past the end of file. */
5499 + amount = min((unsigned int) amount_left, mod_data.buflen);
5500 + amount = min((loff_t) amount,
5501 + curlun->file_length - file_offset);
5502 + if (amount == 0) {
5503 + curlun->sense_data =
5504 + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
5505 + curlun->sense_data_info = file_offset >> 9;
5506 + break;
5509 + /* Perform the read */
5510 + file_offset_tmp = file_offset;
5511 + nread = curlun->filp->f_op->read(curlun->filp,
5512 + (char *) bh->buf,
5513 + amount, &file_offset_tmp);
5514 + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
5515 + (unsigned long long) file_offset,
5516 + (int) nread);
5517 + if (signal_pending(current))
5518 + return -EINTR;
5520 + if (nread < 0) {
5521 + LDBG(curlun, "error in file verify: %d\n",
5522 + (int) nread);
5523 + nread = 0;
5524 + } else if (nread < amount) {
5525 + LDBG(curlun, "partial file verify: %d/%u\n",
5526 + (int) nread, amount);
5527 + nread -= (nread & 511); // Round down to a sector
5529 + if (nread == 0) {
5530 + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
5531 + curlun->sense_data_info = file_offset >> 9;
5532 + break;
5534 + file_offset += nread;
5535 + amount_left -= nread;
5537 + return 0;
5541 +/*-------------------------------------------------------------------------*/
5543 +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
5545 + u8 *buf = (u8 *) bh->buf;
5547 + static char vendor_id[] = "Linux ";
5548 + static char product_id[] = "File-Stor Gadget";
5550 + if (!fsg->curlun) { // Unsupported LUNs are okay
5551 + fsg->bad_lun_okay = 1;
5552 + memset(buf, 0, 36);
5553 + buf[0] = 0x7f; // Unsupported, no device-type
5554 + return 36;
5557 + memset(buf, 0, 8); // Non-removable, direct-access device
5558 + if (mod_data.removable)
5559 + buf[1] = 0x80;
5560 + buf[2] = 2; // ANSI SCSI level 2
5561 + buf[3] = 2; // SCSI-2 INQUIRY data format
5562 + buf[4] = 31; // Additional length
5563 + // No special options
5564 + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id, product_id,
5565 + mod_data.release);
5566 + return 36;
5570 +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
5572 + struct lun *curlun = fsg->curlun;
5573 + u8 *buf = (u8 *) bh->buf;
5574 + u32 sd, sdinfo;
5576 + /*
5577 + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
5579 + * If a REQUEST SENSE command is received from an initiator
5580 + * with a pending unit attention condition (before the target
5581 + * generates the contingent allegiance condition), then the
5582 + * target shall either:
5583 + * a) report any pending sense data and preserve the unit
5584 + * attention condition on the logical unit, or,
5585 + * b) report the unit attention condition, may discard any
5586 + * pending sense data, and clear the unit attention
5587 + * condition on the logical unit for that initiator.
5589 + * FSG normally uses option a); enable this code to use option b).
5590 + */
5591 +#if 0
5592 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
5593 + curlun->sense_data = curlun->unit_attention_data;
5594 + curlun->unit_attention_data = SS_NO_SENSE;
5596 +#endif
5598 + if (!curlun) { // Unsupported LUNs are okay
5599 + fsg->bad_lun_okay = 1;
5600 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
5601 + sdinfo = 0;
5602 + } else {
5603 + sd = curlun->sense_data;
5604 + sdinfo = curlun->sense_data_info;
5605 + curlun->sense_data = SS_NO_SENSE;
5606 + curlun->sense_data_info = 0;
5609 + memset(buf, 0, 18);
5610 + buf[0] = 0x80 | 0x70; // Valid, current error
5611 + buf[2] = SK(sd);
5612 + put_be32(&buf[3], sdinfo); // Sense information
5613 + buf[7] = 18 - 8; // Additional sense length
5614 + buf[12] = ASC(sd);
5615 + buf[13] = ASCQ(sd);
5616 + return 18;
5620 +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
5622 + struct lun *curlun = fsg->curlun;
5623 + u32 lba = get_be32(&fsg->cmnd[2]);
5624 + int pmi = fsg->cmnd[8];
5625 + u8 *buf = (u8 *) bh->buf;
5627 + /* Check the PMI and LBA fields */
5628 + if (pmi > 1 || (pmi == 0 && lba != 0)) {
5629 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
5630 + return -EINVAL;
5633 + put_be32(&buf[0], curlun->num_sectors - 1); // Max logical block
5634 + put_be32(&buf[4], 512); // Block length
5635 + return 8;
5639 +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
5641 + struct lun *curlun = fsg->curlun;
5642 + int mscmnd = fsg->cmnd[0];
5643 + u8 *buf = (u8 *) bh->buf;
5644 + u8 *buf0 = buf;
5645 + int pc, page_code;
5646 + int changeable_values, all_pages;
5647 + int valid_page = 0;
5648 + int len, limit;
5650 + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
5651 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
5652 + return -EINVAL;
5654 + pc = fsg->cmnd[2] >> 6;
5655 + page_code = fsg->cmnd[2] & 0x3f;
5656 + if (pc == 3) {
5657 + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
5658 + return -EINVAL;
5660 + changeable_values = (pc == 1);
5661 + all_pages = (page_code == 0x3f);
5663 + /* Write the mode parameter header. Fixed values are: default
5664 + * medium type, no cache control (DPOFUA), and no block descriptors.
5665 + * The only variable value is the WriteProtect bit. We will fill in
5666 + * the mode data length later. */
5667 + memset(buf, 0, 8);
5668 + if (mscmnd == SC_MODE_SENSE_6) {
5669 + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
5670 + buf += 4;
5671 + limit = 255;
5672 + } else { // SC_MODE_SENSE_10
5673 + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
5674 + buf += 8;
5675 + limit = 65535; // Should really be mod_data.buflen
5678 + /* No block descriptors */
5680 + /* The mode pages, in numerical order. The only page we support
5681 + * is the Caching page. */
5682 + if (page_code == 0x08 || all_pages) {
5683 + valid_page = 1;
5684 + buf[0] = 0x08; // Page code
5685 + buf[1] = 10; // Page length
5686 + memset(buf+2, 0, 10); // None of the fields are changeable
5688 + if (!changeable_values) {
5689 + buf[2] = 0x04; // Write cache enable,
5690 + // Read cache not disabled
5691 + // No cache retention priorities
5692 + put_be16(&buf[4], 0xffff); // Don't disable prefetch
5693 + // Minimum prefetch = 0
5694 + put_be16(&buf[8], 0xffff); // Maximum prefetch
5695 + put_be16(&buf[10], 0xffff); // Maximum prefetch ceiling
5697 + buf += 12;
5700 + /* Check that a valid page was requested and the mode data length
5701 + * isn't too long. */
5702 + len = buf - buf0;
5703 + if (!valid_page || len > limit) {
5704 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
5705 + return -EINVAL;
5708 + /* Store the mode data length */
5709 + if (mscmnd == SC_MODE_SENSE_6)
5710 + buf0[0] = len - 1;
5711 + else
5712 + put_be16(buf0, len - 2);
5713 + return len;
5717 +static int do_start_stop(struct fsg_dev *fsg)
5719 + struct lun *curlun = fsg->curlun;
5720 + int loej, start;
5722 + if (!mod_data.removable) {
5723 + curlun->sense_data = SS_INVALID_COMMAND;
5724 + return -EINVAL;
5727 + // int immed = fsg->cmnd[1] & 0x01;
5728 + loej = fsg->cmnd[4] & 0x02;
5729 + start = fsg->cmnd[4] & 0x01;
5731 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
5732 + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
5733 + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
5734 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
5735 + return -EINVAL;
5738 + if (!start) {
5740 + /* Are we allowed to unload the media? */
5741 + if (curlun->prevent_medium_removal) {
5742 + LDBG(curlun, "unload attempt prevented\n");
5743 + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
5744 + return -EINVAL;
5746 + if (loej) { // Simulate an unload/eject
5747 + up_read(&fsg->filesem);
5748 + down_write(&fsg->filesem);
5749 + close_backing_file(curlun);
5750 + up_write(&fsg->filesem);
5751 + down_read(&fsg->filesem);
5753 + } else {
5755 + /* Our emulation doesn't support mounting; the medium is
5756 + * available for use as soon as it is loaded. */
5757 + if (!backing_file_is_open(curlun)) {
5758 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
5759 + return -EINVAL;
5762 +#endif
5763 + return 0;
5767 +static int do_prevent_allow(struct fsg_dev *fsg)
5769 + struct lun *curlun = fsg->curlun;
5770 + int prevent;
5772 + if (!mod_data.removable) {
5773 + curlun->sense_data = SS_INVALID_COMMAND;
5774 + return -EINVAL;
5777 + prevent = fsg->cmnd[4] & 0x01;
5778 + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
5779 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
5780 + return -EINVAL;
5783 + if (curlun->prevent_medium_removal && !prevent)
5784 + fsync_sub(curlun);
5785 + curlun->prevent_medium_removal = prevent;
5786 + return 0;
5790 +static int do_read_format_capacities(struct fsg_dev *fsg,
5791 + struct fsg_buffhd *bh)
5793 + struct lun *curlun = fsg->curlun;
5794 + u8 *buf = (u8 *) bh->buf;
5796 + buf[0] = buf[1] = buf[2] = 0;
5797 + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
5798 + buf += 4;
5800 + put_be32(&buf[0], curlun->num_sectors); // Number of blocks
5801 + put_be32(&buf[4], 512); // Block length
5802 + buf[4] = 0x02; // Current capacity
5803 + return 12;
5807 +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
5809 + struct lun *curlun = fsg->curlun;
5811 + /* We don't support MODE SELECT */
5812 + curlun->sense_data = SS_INVALID_COMMAND;
5813 + return -EINVAL;
5817 +/*-------------------------------------------------------------------------*/
5819 +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
5821 + int rc;
5823 + rc = fsg_set_halt(fsg, fsg->bulk_in);
5824 + if (rc == -EAGAIN)
5825 + VDBG(fsg, "delayed bulk-in endpoint halt\n");
5826 + while (rc != 0) {
5827 + if (rc != -EAGAIN) {
5828 + WARN(fsg, "usb_ep_set_halt -> %d\n", rc);
5829 + rc = 0;
5830 + break;
5833 + /* Wait for a short time and then try again */
5834 + set_current_state(TASK_INTERRUPTIBLE);
5835 + if (schedule_timeout(HZ / 10) != 0)
5836 + return -EINTR;
5837 + rc = usb_ep_set_halt(fsg->bulk_in);
5839 + return rc;
5842 +static int pad_with_zeros(struct fsg_dev *fsg)
5844 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
5845 + u32 nkeep = bh->inreq->length;
5846 + u32 nsend;
5847 + int rc;
5849 + bh->state = BUF_STATE_EMPTY; // For the first iteration
5850 + fsg->usb_amount_left = nkeep + fsg->residue;
5851 + while (fsg->usb_amount_left > 0) {
5853 + /* Wait for the next buffer to be free */
5854 + while (bh->state != BUF_STATE_EMPTY) {
5855 + if ((rc = sleep_thread(fsg)) != 0)
5856 + return rc;
5859 + nsend = min(fsg->usb_amount_left, (u32) mod_data.buflen);
5860 + memset(bh->buf + nkeep, 0, nsend - nkeep);
5861 + bh->inreq->length = nsend;
5862 + bh->inreq->zero = 0;
5863 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
5864 + &bh->inreq_busy, &bh->state);
5865 + bh = fsg->next_buffhd_to_fill = bh->next;
5866 + fsg->usb_amount_left -= nsend;
5867 + nkeep = 0;
5869 + return 0;
5872 +static int throw_away_data(struct fsg_dev *fsg)
5874 + struct fsg_buffhd *bh;
5875 + u32 amount;
5876 + int rc;
5878 + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
5879 + fsg->usb_amount_left > 0) {
5881 + /* Throw away the data in a filled buffer */
5882 + if (bh->state == BUF_STATE_FULL) {
5883 + bh->state = BUF_STATE_EMPTY;
5884 + fsg->next_buffhd_to_drain = bh->next;
5886 + /* A short packet or an error ends everything */
5887 + if (bh->outreq->actual != bh->outreq->length ||
5888 + bh->outreq->status != 0) {
5889 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
5890 + return -EINTR;
5892 + continue;
5895 + /* Try to submit another request if we need one */
5896 + bh = fsg->next_buffhd_to_fill;
5897 + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
5898 + amount = min(fsg->usb_amount_left,
5899 + (u32) mod_data.buflen);
5901 + /* amount is always divisible by 512, hence by
5902 + * the bulk-out maxpacket size */
5903 + bh->outreq->length = bh->bulk_out_intended_length =
5904 + amount;
5905 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
5906 + &bh->outreq_busy, &bh->state);
5907 + fsg->next_buffhd_to_fill = bh->next;
5908 + fsg->usb_amount_left -= amount;
5909 + continue;
5912 + /* Otherwise wait for something to happen */
5913 + if ((rc = sleep_thread(fsg)) != 0)
5914 + return rc;
5916 + return 0;
5920 +static int finish_reply(struct fsg_dev *fsg)
5922 + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
5923 + int rc = 0;
5925 + switch (fsg->data_dir) {
5926 + case DATA_DIR_NONE:
5927 + break; // Nothing to send
5929 + /* If we don't know whether the host wants to read or write,
5930 + * this must be CB or CBI with an unknown command. We mustn't
5931 + * try to send or receive any data. So stall both bulk pipes
5932 + * if we can and wait for a reset. */
5933 + case DATA_DIR_UNKNOWN:
5934 + if (mod_data.can_stall) {
5935 + fsg_set_halt(fsg, fsg->bulk_out);
5936 + rc = halt_bulk_in_endpoint(fsg);
5938 + break;
5940 + /* All but the last buffer of data must have already been sent */
5941 + case DATA_DIR_TO_HOST:
5942 + if (fsg->data_size == 0)
5943 + ; // Nothing to send
5945 + /* If there's no residue, simply send the last buffer */
5946 + else if (fsg->residue == 0) {
5947 + bh->inreq->zero = 0;
5948 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
5949 + &bh->inreq_busy, &bh->state);
5950 + fsg->next_buffhd_to_fill = bh->next;
5953 + /* There is a residue. For CB and CBI, simply mark the end
5954 + * of the data with a short packet. However, if we are
5955 + * allowed to stall, there was no data at all (residue ==
5956 + * data_size), and the command failed (invalid LUN or
5957 + * sense data is set), then halt the bulk-in endpoint
5958 + * instead. */
5959 + else if (!transport_is_bbb()) {
5960 + if (mod_data.can_stall &&
5961 + fsg->residue == fsg->data_size &&
5962 + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
5963 + bh->state = BUF_STATE_EMPTY;
5964 + rc = halt_bulk_in_endpoint(fsg);
5965 + } else {
5966 + bh->inreq->zero = 1;
5967 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
5968 + &bh->inreq_busy, &bh->state);
5969 + fsg->next_buffhd_to_fill = bh->next;
5973 + /* For Bulk-only, if we're allowed to stall then send the
5974 + * short packet and halt the bulk-in endpoint. If we can't
5975 + * stall, pad out the remaining data with 0's. */
5976 + else {
5977 + if (mod_data.can_stall) {
5978 + bh->inreq->zero = 1;
5979 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
5980 + &bh->inreq_busy, &bh->state);
5981 + fsg->next_buffhd_to_fill = bh->next;
5982 + rc = halt_bulk_in_endpoint(fsg);
5983 + } else
5984 + rc = pad_with_zeros(fsg);
5986 + break;
5988 + /* We have processed all we want from the data the host has sent.
5989 + * There may still be outstanding bulk-out requests. */
5990 + case DATA_DIR_FROM_HOST:
5991 + if (fsg->residue == 0)
5992 + ; // Nothing to receive
5994 + /* Did the host stop sending unexpectedly early? */
5995 + else if (fsg->short_packet_received) {
5996 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
5997 + rc = -EINTR;
6000 + /* We haven't processed all the incoming data. If we are
6001 + * allowed to stall, halt the bulk-out endpoint and cancel
6002 + * any outstanding requests. */
6003 + else if (mod_data.can_stall) {
6004 + fsg_set_halt(fsg, fsg->bulk_out);
6005 + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
6006 + rc = -EINTR;
6009 + /* We can't stall. Read in the excess data and throw it
6010 + * all away. */
6011 + else
6012 + rc = throw_away_data(fsg);
6013 + break;
6015 + return rc;
6019 +static int send_status(struct fsg_dev *fsg)
6021 + struct lun *curlun = fsg->curlun;
6022 + struct fsg_buffhd *bh;
6023 + int rc;
6024 + u8 status = USB_STATUS_PASS;
6025 + u32 sd, sdinfo = 0;
6027 + /* Wait for the next buffer to become available */
6028 + bh = fsg->next_buffhd_to_fill;
6029 + while (bh->state != BUF_STATE_EMPTY) {
6030 + if ((rc = sleep_thread(fsg)) != 0)
6031 + return rc;
6034 + if (curlun) {
6035 + sd = curlun->sense_data;
6036 + sdinfo = curlun->sense_data_info;
6037 + } else if (fsg->bad_lun_okay)
6038 + sd = SS_NO_SENSE;
6039 + else
6040 + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
6042 + if (fsg->phase_error) {
6043 + DBG(fsg, "sending phase-error status\n");
6044 + status = USB_STATUS_PHASE_ERROR;
6045 + sd = SS_INVALID_COMMAND;
6046 + } else if (sd != SS_NO_SENSE) {
6047 + DBG(fsg, "sending command-failure status\n");
6048 + status = USB_STATUS_FAIL;
6049 + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
6050 + " info x%x\n",
6051 + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
6054 + if (transport_is_bbb()) {
6055 + struct bulk_cs_wrap *csw = (struct bulk_cs_wrap *) bh->buf;
6057 + /* Store and send the Bulk-only CSW */
6058 + csw->Signature = __constant_cpu_to_le32(USB_BULK_CS_SIG);
6059 + csw->Tag = fsg->tag;
6060 + csw->Residue = cpu_to_le32(fsg->residue);
6061 + csw->Status = status;
6063 + bh->inreq->length = USB_BULK_CS_WRAP_LEN;
6064 + bh->inreq->zero = 0;
6065 + start_transfer(fsg, fsg->bulk_in, bh->inreq,
6066 + &bh->inreq_busy, &bh->state);
6068 + } else if (mod_data.transport_type == USB_PR_CB) {
6070 + /* Control-Bulk transport has no status stage! */
6071 + return 0;
6073 + } else { // USB_PR_CBI
6074 + struct interrupt_data *buf = (struct interrupt_data *)
6075 + bh->buf;
6077 + /* Store and send the Interrupt data. UFI sends the ASC
6078 + * and ASCQ bytes. Everything else sends a Type (which
6079 + * is always 0) and the status Value. */
6080 + if (mod_data.protocol_type == USB_SC_UFI) {
6081 + buf->bType = ASC(sd);
6082 + buf->bValue = ASCQ(sd);
6083 + } else {
6084 + buf->bType = 0;
6085 + buf->bValue = status;
6087 + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
6089 + fsg->intr_buffhd = bh; // Point to the right buffhd
6090 + fsg->intreq->buf = bh->inreq->buf;
6091 + fsg->intreq->dma = bh->inreq->dma;
6092 + fsg->intreq->context = bh;
6093 + start_transfer(fsg, fsg->intr_in, fsg->intreq,
6094 + &fsg->intreq_busy, &bh->state);
6097 + fsg->next_buffhd_to_fill = bh->next;
6098 + return 0;
6102 +/*-------------------------------------------------------------------------*/
6104 +/* Check whether the command is properly formed and whether its data size
6105 + * and direction agree with the values we already have. */
6106 +static int check_command(struct fsg_dev *fsg, int cmnd_size,
6107 + enum data_direction data_dir, unsigned int mask,
6108 + int needs_medium, const char *name)
6110 + int i;
6111 + int lun = fsg->cmnd[1] >> 5;
6112 + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
6113 + char hdlen[20];
6114 + struct lun *curlun;
6116 + /* Adjust the expected cmnd_size for protocol encapsulation padding.
6117 + * Transparent SCSI doesn't pad. */
6118 + if (protocol_is_scsi())
6121 + /* There's some disagreement as to whether RBC pads commands or not.
6122 + * We'll play it safe and accept either form. */
6123 + else if (mod_data.protocol_type == USB_SC_RBC) {
6124 + if (fsg->cmnd_size == 12)
6125 + cmnd_size = 12;
6127 + /* All the other protocols pad to 12 bytes */
6128 + } else
6129 + cmnd_size = 12;
6131 + hdlen[0] = 0;
6132 + if (fsg->data_dir != DATA_DIR_UNKNOWN)
6133 + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
6134 + fsg->data_size);
6135 + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
6136 + name, cmnd_size, dirletter[(int) data_dir],
6137 + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
6139 + /* We can't reply at all until we know the correct data direction
6140 + * and size. */
6141 + if (fsg->data_size_from_cmnd == 0)
6142 + data_dir = DATA_DIR_NONE;
6143 + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
6144 + fsg->data_dir = data_dir;
6145 + fsg->data_size = fsg->data_size_from_cmnd;
6147 + } else { // Bulk-only
6148 + if (fsg->data_size < fsg->data_size_from_cmnd) {
6150 + /* Host data size < Device data size is a phase error.
6151 + * Carry out the command, but only transfer as much
6152 + * as we are allowed. */
6153 + fsg->data_size_from_cmnd = fsg->data_size;
6154 + fsg->phase_error = 1;
6157 + fsg->residue = fsg->usb_amount_left = fsg->data_size;
6159 + /* Conflicting data directions is a phase error */
6160 + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0)
6161 + goto phase_error;
6163 + /* Verify the length of the command itself */
6164 + if (cmnd_size != fsg->cmnd_size) {
6166 + /* Special case workaround: MS-Windows issues REQUEST SENSE
6167 + * with cbw->Length == 12 (it should be 6). */
6168 + if (fsg->cmnd[0] == SC_REQUEST_SENSE && fsg->cmnd_size == 12)
6169 + cmnd_size = fsg->cmnd_size;
6170 + else
6171 + goto phase_error;
6174 + /* Check that the LUN values are oonsistent */
6175 + if (transport_is_bbb()) {
6176 + if (fsg->lun != lun)
6177 + DBG(fsg, "using LUN %d from CBW, "
6178 + "not LUN %d from CDB\n",
6179 + fsg->lun, lun);
6180 + } else
6181 + fsg->lun = lun; // Use LUN from the command
6183 + /* Check the LUN */
6184 + if (fsg->lun >= 0 && fsg->lun < fsg->nluns) {
6185 + fsg->curlun = curlun = &fsg->luns[fsg->lun];
6186 + if (fsg->cmnd[0] != SC_REQUEST_SENSE) {
6187 + curlun->sense_data = SS_NO_SENSE;
6188 + curlun->sense_data_info = 0;
6190 + } else {
6191 + fsg->curlun = curlun = NULL;
6192 + fsg->bad_lun_okay = 0;
6194 + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
6195 + * to use unsupported LUNs; all others may not. */
6196 + if (fsg->cmnd[0] != SC_INQUIRY &&
6197 + fsg->cmnd[0] != SC_REQUEST_SENSE) {
6198 + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
6199 + return -EINVAL;
6203 + /* If a unit attention condition exists, only INQUIRY and
6204 + * REQUEST SENSE commands are allowed; anything else must fail. */
6205 + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
6206 + fsg->cmnd[0] != SC_INQUIRY &&
6207 + fsg->cmnd[0] != SC_REQUEST_SENSE) {
6208 + curlun->sense_data = curlun->unit_attention_data;
6209 + curlun->unit_attention_data = SS_NO_SENSE;
6210 + return -EINVAL;
6213 + /* Check that only command bytes listed in the mask are non-zero */
6214 + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
6215 + for (i = 1; i < cmnd_size; ++i) {
6216 + if (fsg->cmnd[i] && !(mask & (1 << i))) {
6217 + if (curlun)
6218 + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
6219 + return -EINVAL;
6223 + /* If the medium isn't mounted and the command needs to access
6224 + * it, return an error. */
6225 + if (curlun && !backing_file_is_open(curlun) && needs_medium) {
6226 + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
6227 + return -EINVAL;
6230 + return 0;
6232 +phase_error:
6233 + fsg->phase_error = 1;
6234 + return -EINVAL;
6238 +static int do_scsi_command(struct fsg_dev *fsg)
6240 + struct fsg_buffhd *bh;
6241 + int rc;
6242 + int reply = -EINVAL;
6243 + int i;
6244 + static char unknown[16];
6246 + dump_cdb(fsg);
6248 + /* Wait for the next buffer to become available for data or status */
6249 + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
6250 + while (bh->state != BUF_STATE_EMPTY) {
6251 + if ((rc = sleep_thread(fsg)) != 0)
6252 + return rc;
6254 + fsg->phase_error = 0;
6255 + fsg->short_packet_received = 0;
6257 + down_read(&fsg->filesem); // We're using the backing file
6258 + switch (fsg->cmnd[0]) {
6260 + case SC_INQUIRY:
6261 + fsg->data_size_from_cmnd = fsg->cmnd[4];
6262 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
6263 + (1<<4), 0,
6264 + "INQUIRY")) == 0)
6265 + reply = do_inquiry(fsg, bh);
6266 + break;
6268 + case SC_MODE_SELECT_6:
6269 + fsg->data_size_from_cmnd = fsg->cmnd[4];
6270 + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
6271 + (1<<1) | (1<<4), 0,
6272 + "MODE SELECT(6)")) == 0)
6273 + reply = do_mode_select(fsg, bh);
6274 + break;
6276 + case SC_MODE_SELECT_10:
6277 + fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]);
6278 + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
6279 + (1<<1) | (3<<7), 0,
6280 + "MODE SELECT(10)")) == 0)
6281 + reply = do_mode_select(fsg, bh);
6282 + break;
6284 + case SC_MODE_SENSE_6:
6285 + fsg->data_size_from_cmnd = fsg->cmnd[4];
6286 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
6287 + (1<<1) | (1<<2) | (1<<4), 0,
6288 + "MODE SENSE(6)")) == 0)
6289 + reply = do_mode_sense(fsg, bh);
6290 + break;
6292 + case SC_MODE_SENSE_10:
6293 + fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]);
6294 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
6295 + (1<<1) | (1<<2) | (3<<7), 0,
6296 + "MODE SENSE(10)")) == 0)
6297 + reply = do_mode_sense(fsg, bh);
6298 + break;
6300 + case SC_PREVENT_ALLOW_MEDIUM_REMOVAL:
6301 + fsg->data_size_from_cmnd = 0;
6302 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
6303 + (1<<4), 0,
6304 + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
6305 + reply = do_prevent_allow(fsg);
6306 + break;
6308 + case SC_READ_6:
6309 + i = fsg->cmnd[4];
6310 + fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
6311 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
6312 + (7<<1) | (1<<4), 1,
6313 + "READ(6)")) == 0)
6314 + reply = do_read(fsg);
6315 + break;
6317 + case SC_READ_10:
6318 + fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]) << 9;
6319 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
6320 + (1<<1) | (0xf<<2) | (3<<7), 1,
6321 + "READ(10)")) == 0)
6322 + reply = do_read(fsg);
6323 + break;
6325 + case SC_READ_12:
6326 + fsg->data_size_from_cmnd = get_be32(&fsg->cmnd[6]) << 9;
6327 + if ((reply = check_command(fsg, 12, DATA_DIR_TO_HOST,
6328 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
6329 + "READ(12)")) == 0)
6330 + reply = do_read(fsg);
6331 + break;
6333 + case SC_READ_CAPACITY:
6334 + fsg->data_size_from_cmnd = 8;
6335 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
6336 + (0xf<<2) | (1<<8), 1,
6337 + "READ CAPACITY")) == 0)
6338 + reply = do_read_capacity(fsg, bh);
6339 + break;
6341 + case SC_READ_FORMAT_CAPACITIES:
6342 + fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]);
6343 + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
6344 + (3<<7), 1,
6345 + "READ FORMAT CAPACITIES")) == 0)
6346 + reply = do_read_format_capacities(fsg, bh);
6347 + break;
6349 + case SC_REQUEST_SENSE:
6350 + fsg->data_size_from_cmnd = fsg->cmnd[4];
6351 + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
6352 + (1<<4), 0,
6353 + "REQUEST SENSE")) == 0)
6354 + reply = do_request_sense(fsg, bh);
6355 + break;
6357 + case SC_START_STOP_UNIT:
6358 + fsg->data_size_from_cmnd = 0;
6359 + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
6360 + (1<<1) | (1<<4), 0,
6361 + "START-STOP UNIT")) == 0)
6362 + reply = do_start_stop(fsg);
6363 + break;
6365 + case SC_SYNCHRONIZE_CACHE:
6366 + fsg->data_size_from_cmnd = 0;
6367 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
6368 + (0xf<<2) | (3<<7), 1,
6369 + "SYNCHRONIZE CACHE")) == 0)
6370 + reply = do_synchronize_cache(fsg);
6371 + break;
6373 + case SC_TEST_UNIT_READY:
6374 + fsg->data_size_from_cmnd = 0;
6375 + reply = check_command(fsg, 6, DATA_DIR_NONE,
6376 + 0, 1,
6377 + "TEST UNIT READY");
6378 + break;
6380 + /* Although optional, this command is used by MS-Windows. We
6381 + * support a minimal version: BytChk must be 0. */
6382 + case SC_VERIFY:
6383 + fsg->data_size_from_cmnd = 0;
6384 + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
6385 + (1<<1) | (0xf<<2) | (3<<7), 1,
6386 + "VERIFY")) == 0)
6387 + reply = do_verify(fsg);
6388 + break;
6390 + case SC_WRITE_6:
6391 + i = fsg->cmnd[4];
6392 + fsg->data_size_from_cmnd = (i == 0 ? 256 : i) << 9;
6393 + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
6394 + (7<<1) | (1<<4), 1,
6395 + "WRITE(6)")) == 0)
6396 + reply = do_write(fsg);
6397 + break;
6399 + case SC_WRITE_10:
6400 + fsg->data_size_from_cmnd = get_be16(&fsg->cmnd[7]) << 9;
6401 + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
6402 + (1<<1) | (0xf<<2) | (3<<7), 1,
6403 + "WRITE(10)")) == 0)
6404 + reply = do_write(fsg);
6405 + break;
6407 + case SC_WRITE_12:
6408 + fsg->data_size_from_cmnd = get_be32(&fsg->cmnd[6]) << 9;
6409 + if ((reply = check_command(fsg, 12, DATA_DIR_FROM_HOST,
6410 + (1<<1) | (0xf<<2) | (0xf<<6), 1,
6411 + "WRITE(12)")) == 0)
6412 + reply = do_write(fsg);
6413 + break;
6415 + /* Some mandatory commands that we recognize but don't implement.
6416 + * They don't mean much in this setting. It's left as an exercise
6417 + * for anyone interested to implement RESERVE and RELEASE in terms
6418 + * of Posix locks. */
6419 + case SC_FORMAT_UNIT:
6420 + case SC_RELEASE:
6421 + case SC_RESERVE:
6422 + case SC_SEND_DIAGNOSTIC:
6423 + // Fall through
6425 + default:
6426 + fsg->data_size_from_cmnd = 0;
6427 + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
6428 + if ((reply = check_command(fsg, fsg->cmnd_size,
6429 + DATA_DIR_UNKNOWN, 0xff, 0, unknown)) == 0) {
6430 + fsg->curlun->sense_data = SS_INVALID_COMMAND;
6431 + reply = -EINVAL;
6433 + break;
6435 + up_read(&fsg->filesem);
6437 + if (reply == -EINTR || signal_pending(current))
6438 + return -EINTR;
6440 + /* Set up the single reply buffer for finish_reply() */
6441 + if (reply == -EINVAL)
6442 + reply = 0; // Error reply length
6443 + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
6444 + reply = min((u32) reply, fsg->data_size_from_cmnd);
6445 + bh->inreq->length = reply;
6446 + bh->state = BUF_STATE_FULL;
6447 + fsg->residue -= reply;
6448 + } // Otherwise it's already set
6450 + return 0;
6454 +/*-------------------------------------------------------------------------*/
6456 +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
6458 + struct usb_request *req = bh->outreq;
6459 + struct bulk_cb_wrap *cbw = (struct bulk_cb_wrap *) req->buf;
6461 + /* Was this a real packet? */
6462 + if (req->status)
6463 + return -EINVAL;
6465 + /* Is the CBW valid? */
6466 + if (req->actual != USB_BULK_CB_WRAP_LEN ||
6467 + cbw->Signature != __constant_cpu_to_le32(
6468 + USB_BULK_CB_SIG)) {
6469 + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
6470 + req->actual,
6471 + le32_to_cpu(cbw->Signature));
6473 + /* The Bulk-only spec says we MUST stall the bulk pipes!
6474 + * If we want to avoid stalls, set a flag so that we will
6475 + * clear the endpoint halts at the next reset. */
6476 + if (!mod_data.can_stall)
6477 + set_bit(CLEAR_BULK_HALTS, &fsg->atomic_bitflags);
6478 + fsg_set_halt(fsg, fsg->bulk_out);
6479 + halt_bulk_in_endpoint(fsg);
6480 + return -EINVAL;
6483 + /* Is the CBW meaningful? */
6484 + if (cbw->Lun >= MAX_LUNS || cbw->Flags & ~USB_BULK_IN_FLAG ||
6485 + cbw->Length < 6 || cbw->Length > MAX_COMMAND_SIZE) {
6486 + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
6487 + "cmdlen %u\n",
6488 + cbw->Lun, cbw->Flags, cbw->Length);
6490 + /* We can do anything we want here, so let's stall the
6491 + * bulk pipes if we are allowed to. */
6492 + if (mod_data.can_stall) {
6493 + fsg_set_halt(fsg, fsg->bulk_out);
6494 + halt_bulk_in_endpoint(fsg);
6496 + return -EINVAL;
6499 + /* Save the command for later */
6500 + fsg->cmnd_size = cbw->Length;
6501 + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
6502 + if (cbw->Flags & USB_BULK_IN_FLAG)
6503 + fsg->data_dir = DATA_DIR_TO_HOST;
6504 + else
6505 + fsg->data_dir = DATA_DIR_FROM_HOST;
6506 + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
6507 + if (fsg->data_size == 0)
6508 + fsg->data_dir = DATA_DIR_NONE;
6509 + fsg->lun = cbw->Lun;
6510 + fsg->tag = cbw->Tag;
6511 + return 0;
6515 +static int get_next_command(struct fsg_dev *fsg)
6517 + struct fsg_buffhd *bh;
6518 + int rc = 0;
6520 + if (transport_is_bbb()) {
6522 + /* Wait for the next buffer to become available */
6523 + bh = fsg->next_buffhd_to_fill;
6524 + while (bh->state != BUF_STATE_EMPTY) {
6525 + if ((rc = sleep_thread(fsg)) != 0)
6526 + return rc;
6529 + /* Queue a request to read a Bulk-only CBW */
6530 + set_bulk_out_req_length(fsg, bh, USB_BULK_CB_WRAP_LEN);
6531 + start_transfer(fsg, fsg->bulk_out, bh->outreq,
6532 + &bh->outreq_busy, &bh->state);
6534 + /* We will drain the buffer in software, which means we
6535 + * can reuse it for the next filling. No need to advance
6536 + * next_buffhd_to_fill. */
6538 + /* Wait for the CBW to arrive */
6539 + while (bh->state != BUF_STATE_FULL) {
6540 + if ((rc = sleep_thread(fsg)) != 0)
6541 + return rc;
6543 + rc = received_cbw(fsg, bh);
6544 + bh->state = BUF_STATE_EMPTY;
6546 + } else { // USB_PR_CB or USB_PR_CBI
6548 + /* Wait for the next command to arrive */
6549 + while (fsg->cbbuf_cmnd_size == 0) {
6550 + if ((rc = sleep_thread(fsg)) != 0)
6551 + return rc;
6554 + /* Is the previous status interrupt request still busy?
6555 + * The host is allowed to skip reading the status,
6556 + * so we must cancel it. */
6557 + if (fsg->intreq_busy)
6558 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
6560 + /* Copy the command and mark the buffer empty */
6561 + fsg->data_dir = DATA_DIR_UNKNOWN;
6562 + spin_lock_irq(&fsg->lock);
6563 + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
6564 + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
6565 + fsg->cbbuf_cmnd_size = 0;
6566 + spin_unlock_irq(&fsg->lock);
6568 + return rc;
6572 +/*-------------------------------------------------------------------------*/
6574 +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
6575 + const struct usb_endpoint_descriptor *d)
6577 + int rc;
6579 + ep->driver_data = fsg;
6580 + rc = usb_ep_enable(ep, d);
6581 + if (rc)
6582 + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
6583 + return rc;
6586 +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
6587 + struct usb_request **preq)
6589 + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
6590 + if (*preq)
6591 + return 0;
6592 + ERROR(fsg, "can't allocate request for %s\n", ep->name);
6593 + return -ENOMEM;
6597 + * Reset interface setting and re-init endpoint state (toggle etc).
6598 + * Call with altsetting < 0 to disable the interface. The only other
6599 + * available altsetting is 0, which enables the interface.
6600 + */
6601 +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
6603 + int rc = 0;
6604 + int i;
6605 + const struct usb_endpoint_descriptor *d;
6607 + if (fsg->running)
6608 + DBG(fsg, "reset interface\n");
6610 +reset:
6611 + /* Deallocate the requests */
6612 + for (i = 0; i < NUM_BUFFERS; ++i) {
6613 + struct fsg_buffhd *bh = &fsg->buffhds[i];
6615 + if (bh->inreq) {
6616 + usb_ep_free_request(fsg->bulk_in, bh->inreq);
6617 + bh->inreq = NULL;
6619 + if (bh->outreq) {
6620 + usb_ep_free_request(fsg->bulk_out, bh->outreq);
6621 + bh->outreq = NULL;
6624 + if (fsg->intreq) {
6625 + usb_ep_free_request(fsg->intr_in, fsg->intreq);
6626 + fsg->intreq = NULL;
6629 + /* Disable the endpoints */
6630 + if (fsg->bulk_in_enabled) {
6631 + usb_ep_disable(fsg->bulk_in);
6632 + fsg->bulk_in_enabled = 0;
6634 + if (fsg->bulk_out_enabled) {
6635 + usb_ep_disable(fsg->bulk_out);
6636 + fsg->bulk_out_enabled = 0;
6638 + if (fsg->intr_in_enabled) {
6639 + usb_ep_disable(fsg->intr_in);
6640 + fsg->intr_in_enabled = 0;
6643 + fsg->running = 0;
6644 + if (altsetting < 0 || rc != 0)
6645 + return rc;
6647 + DBG(fsg, "set interface %d\n", altsetting);
6649 + /* Enable the endpoints */
6650 + d = ep_desc(fsg->gadget, &fs_bulk_in_desc, &hs_bulk_in_desc);
6651 + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
6652 + goto reset;
6653 + fsg->bulk_in_enabled = 1;
6655 + d = ep_desc(fsg->gadget, &fs_bulk_out_desc, &hs_bulk_out_desc);
6656 + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
6657 + goto reset;
6658 + fsg->bulk_out_enabled = 1;
6659 + fsg->bulk_out_maxpacket = d->wMaxPacketSize;
6661 + if (transport_is_cbi()) {
6662 + d = ep_desc(fsg->gadget, &fs_intr_in_desc, &hs_intr_in_desc);
6663 + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
6664 + goto reset;
6665 + fsg->intr_in_enabled = 1;
6668 + /* Allocate the requests */
6669 + for (i = 0; i < NUM_BUFFERS; ++i) {
6670 + struct fsg_buffhd *bh = &fsg->buffhds[i];
6672 + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
6673 + goto reset;
6674 + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
6675 + goto reset;
6676 + bh->inreq->buf = bh->outreq->buf = bh->buf;
6677 + bh->inreq->dma = bh->outreq->dma = bh->dma;
6678 + bh->inreq->context = bh->outreq->context = bh;
6679 + bh->inreq->complete = bulk_in_complete;
6680 + bh->outreq->complete = bulk_out_complete;
6682 + if (transport_is_cbi()) {
6683 + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
6684 + goto reset;
6685 + fsg->intreq->complete = intr_in_complete;
6688 + fsg->running = 1;
6689 + for (i = 0; i < fsg->nluns; ++i)
6690 + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
6691 + return rc;
6696 + * Change our operational configuration. This code must agree with the code
6697 + * that returns config descriptors, and with interface altsetting code.
6699 + * It's also responsible for power management interactions. Some
6700 + * configurations might not work with our current power sources.
6701 + * For now we just assume the gadget is always self-powered.
6702 + */
6703 +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
6705 + int rc = 0;
6707 + /* Disable the single interface */
6708 + if (fsg->config != 0) {
6709 + DBG(fsg, "reset config\n");
6710 + fsg->config = 0;
6711 + rc = do_set_interface(fsg, -1);
6714 + /* Enable the interface */
6715 + if (new_config != 0) {
6716 + fsg->config = new_config;
6717 + if ((rc = do_set_interface(fsg, 0)) != 0)
6718 + fsg->config = 0; // Reset on errors
6719 + else {
6720 + char *speed;
6722 + switch (fsg->gadget->speed) {
6723 + case USB_SPEED_LOW: speed = "low"; break;
6724 + case USB_SPEED_FULL: speed = "full"; break;
6725 + case USB_SPEED_HIGH: speed = "high"; break;
6726 + default: speed = "?"; break;
6728 + INFO(fsg, "%s speed config #%d\n", speed, fsg->config);
6731 + return rc;
6735 +/*-------------------------------------------------------------------------*/
6737 +static void handle_exception(struct fsg_dev *fsg)
6739 + siginfo_t info;
6740 + int sig;
6741 + int i;
6742 + int num_active;
6743 + struct fsg_buffhd *bh;
6744 + enum fsg_state old_state;
6745 + u8 new_config;
6746 + struct lun *curlun;
6747 + unsigned int exception_req_tag;
6748 + int rc;
6750 + /* Clear the existing signals. Anything but SIGUSR1 is converted
6751 + * into a high-priority EXIT exception. */
6752 + for (;;) {
6753 + spin_lock_irq(&current->sigmask_lock);
6754 + sig = dequeue_signal(&fsg->thread_signal_mask, &info);
6755 + spin_unlock_irq(&current->sigmask_lock);
6756 + if (!sig)
6757 + break;
6758 + if (sig != SIGUSR1) {
6759 + if (fsg->state < FSG_STATE_EXIT)
6760 + DBG(fsg, "Main thread exiting on signal\n");
6761 + raise_exception(fsg, FSG_STATE_EXIT);
6765 + /* Cancel all the pending transfers */
6766 + if (fsg->intreq_busy)
6767 + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
6768 + for (i = 0; i < NUM_BUFFERS; ++i) {
6769 + bh = &fsg->buffhds[i];
6770 + if (bh->inreq_busy)
6771 + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
6772 + if (bh->outreq_busy)
6773 + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
6776 + /* Wait until everything is idle */
6777 + for (;;) {
6778 + num_active = fsg->intreq_busy;
6779 + for (i = 0; i < NUM_BUFFERS; ++i) {
6780 + bh = &fsg->buffhds[i];
6781 + num_active += bh->inreq_busy + bh->outreq_busy;
6783 + if (num_active == 0)
6784 + break;
6785 + if (sleep_thread(fsg))
6786 + return;
6789 + /* Clear out the controller's fifos */
6790 + if (fsg->bulk_in_enabled)
6791 + usb_ep_fifo_flush(fsg->bulk_in);
6792 + if (fsg->bulk_out_enabled)
6793 + usb_ep_fifo_flush(fsg->bulk_out);
6794 + if (fsg->intr_in_enabled)
6795 + usb_ep_fifo_flush(fsg->intr_in);
6797 + /* Reset the I/O buffer states and pointers, the SCSI
6798 + * state, and the exception. Then invoke the handler. */
6799 + spin_lock_irq(&fsg->lock);
6801 + for (i = 0; i < NUM_BUFFERS; ++i) {
6802 + bh = &fsg->buffhds[i];
6803 + bh->state = BUF_STATE_EMPTY;
6805 + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
6806 + &fsg->buffhds[0];
6808 + exception_req_tag = fsg->exception_req_tag;
6809 + new_config = fsg->new_config;
6810 + old_state = fsg->state;
6812 + if (old_state == FSG_STATE_ABORT_BULK_OUT)
6813 + fsg->state = FSG_STATE_STATUS_PHASE;
6814 + else {
6815 + for (i = 0; i < fsg->nluns; ++i) {
6816 + curlun = &fsg->luns[i];
6817 + curlun->prevent_medium_removal = 0;
6818 + curlun->sense_data = curlun->unit_attention_data =
6819 + SS_NO_SENSE;
6820 + curlun->sense_data_info = 0;
6822 + fsg->state = FSG_STATE_IDLE;
6824 + spin_unlock_irq(&fsg->lock);
6826 + /* Carry out any extra actions required for the exception */
6827 + switch (old_state) {
6828 + default:
6829 + break;
6831 + case FSG_STATE_ABORT_BULK_OUT:
6832 + send_status(fsg);
6833 + spin_lock_irq(&fsg->lock);
6834 + if (fsg->state == FSG_STATE_STATUS_PHASE)
6835 + fsg->state = FSG_STATE_IDLE;
6836 + spin_unlock_irq(&fsg->lock);
6837 + break;
6839 + case FSG_STATE_RESET:
6840 + /* In case we were forced against our will to halt a
6841 + * bulk endpoint, clear the halt now. (The SuperH UDC
6842 + * requires this.) */
6843 + if (test_and_clear_bit(CLEAR_BULK_HALTS,
6844 + &fsg->atomic_bitflags)) {
6845 + usb_ep_clear_halt(fsg->bulk_in);
6846 + usb_ep_clear_halt(fsg->bulk_out);
6849 + if (transport_is_bbb()) {
6850 + if (fsg->ep0_req_tag == exception_req_tag)
6851 + ep0_queue(fsg); // Complete the status stage
6853 + } else if (transport_is_cbi())
6854 + send_status(fsg); // Status by interrupt pipe
6856 + /* Technically this should go here, but it would only be
6857 + * a waste of time. Ditto for the INTERFACE_CHANGE and
6858 + * CONFIG_CHANGE cases. */
6859 + // for (i = 0; i < fsg->nluns; ++i)
6860 + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
6861 + break;
6863 + case FSG_STATE_INTERFACE_CHANGE:
6864 + rc = do_set_interface(fsg, 0);
6865 + if (fsg->ep0_req_tag != exception_req_tag)
6866 + break;
6867 + if (rc != 0) // STALL on errors
6868 + fsg_set_halt(fsg, fsg->ep0);
6869 + else // Complete the status stage
6870 + ep0_queue(fsg);
6871 + break;
6873 + case FSG_STATE_CONFIG_CHANGE:
6874 + rc = do_set_config(fsg, new_config);
6875 + if (fsg->ep0_req_tag != exception_req_tag)
6876 + break;
6877 + if (rc != 0) // STALL on errors
6878 + fsg_set_halt(fsg, fsg->ep0);
6879 + else // Complete the status stage
6880 + ep0_queue(fsg);
6881 + break;
6883 + case FSG_STATE_DISCONNECT:
6884 + fsync_all(fsg);
6885 + do_set_config(fsg, 0); // Unconfigured state
6886 + break;
6888 + case FSG_STATE_EXIT:
6889 + case FSG_STATE_TERMINATED:
6890 + do_set_config(fsg, 0); // Free resources
6891 + spin_lock_irq(&fsg->lock);
6892 + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
6893 + spin_unlock_irq(&fsg->lock);
6894 + break;
6899 +/*-------------------------------------------------------------------------*/
6901 +static int fsg_main_thread(void *fsg_)
6903 + struct fsg_dev *fsg = (struct fsg_dev *) fsg_;
6905 + fsg->thread_task = current;
6907 + /* Release all our userspace resources */
6908 + daemonize();
6909 + reparent_to_init();
6910 + strncpy(current->comm, "file-storage-gadget",
6911 + sizeof(current->comm) - 1);
6913 + /* Allow the thread to be killed by a signal, but set the signal mask
6914 + * to block everything but INT, TERM, KILL, and USR1. */
6915 + siginitsetinv(&fsg->thread_signal_mask, sigmask(SIGINT) |
6916 + sigmask(SIGTERM) | sigmask(SIGKILL) |
6917 + sigmask(SIGUSR1));
6918 + spin_lock_irq(&current->sigmask_lock);
6919 + flush_signals(current);
6920 + current->blocked = fsg->thread_signal_mask;
6921 + recalc_sigpending(current);
6922 + spin_unlock_irq(&current->sigmask_lock);
6924 + /* Arrange for userspace references to be interpreted as kernel
6925 + * pointers. That way we can pass a kernel pointer to a routine
6926 + * that expects a __user pointer and it will work okay. */
6927 + set_fs(get_ds());
6929 + /* Wait for the gadget registration to finish up */
6930 + wait_for_completion(&fsg->thread_notifier);
6932 + /* The main loop */
6933 + while (fsg->state != FSG_STATE_TERMINATED) {
6934 + if (exception_in_progress(fsg) || signal_pending(current)) {
6935 + handle_exception(fsg);
6936 + continue;
6939 + if (!fsg->running) {
6940 + sleep_thread(fsg);
6941 + continue;
6944 + if (get_next_command(fsg))
6945 + continue;
6947 + spin_lock_irq(&fsg->lock);
6948 + if (!exception_in_progress(fsg))
6949 + fsg->state = FSG_STATE_DATA_PHASE;
6950 + spin_unlock_irq(&fsg->lock);
6952 + if (do_scsi_command(fsg) || finish_reply(fsg))
6953 + continue;
6955 + spin_lock_irq(&fsg->lock);
6956 + if (!exception_in_progress(fsg))
6957 + fsg->state = FSG_STATE_STATUS_PHASE;
6958 + spin_unlock_irq(&fsg->lock);
6960 + if (send_status(fsg))
6961 + continue;
6963 + spin_lock_irq(&fsg->lock);
6964 + if (!exception_in_progress(fsg))
6965 + fsg->state = FSG_STATE_IDLE;
6966 + spin_unlock_irq(&fsg->lock);
6969 + fsg->thread_task = NULL;
6970 + flush_signals(current);
6972 + /* In case we are exiting because of a signal, unregister the
6973 + * gadget driver and close the backing file. */
6974 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags)) {
6975 + usb_gadget_unregister_driver(&fsg_driver);
6976 + close_all_backing_files(fsg);
6979 + /* Let the unbind and cleanup routines know the thread has exited */
6980 + complete_and_exit(&fsg->thread_notifier, 0);
6984 +/*-------------------------------------------------------------------------*/
6986 +/* If the next two routines are called while the gadget is registered,
6987 + * the caller must own fsg->filesem for writing. */
6989 +static int NORMALLY_INIT open_backing_file(struct lun *curlun,
6990 + const char *filename)
6992 + int ro;
6993 + struct file *filp = NULL;
6994 + int rc = -EINVAL;
6995 + struct inode *inode = NULL;
6996 + loff_t size;
6997 + loff_t num_sectors;
6999 + /* R/W if we can, R/O if we must */
7000 + ro = curlun->ro;
7001 + if (!ro) {
7002 + filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
7003 + if (-EROFS == PTR_ERR(filp))
7004 + ro = 1;
7006 + if (ro)
7007 + filp = filp_open(filename, O_RDONLY | O_LARGEFILE, 0);
7008 + if (IS_ERR(filp)) {
7009 + LINFO(curlun, "unable to open backing file: %s\n", filename);
7010 + return PTR_ERR(filp);
7013 + if (!(filp->f_mode & FMODE_WRITE))
7014 + ro = 1;
7016 + if (filp->f_dentry)
7017 + inode = filp->f_dentry->d_inode;
7018 + if (inode && S_ISBLK(inode->i_mode)) {
7019 + kdev_t dev = inode->i_rdev;
7021 + if (blk_size[MAJOR(dev)])
7022 + size = (loff_t) blk_size[MAJOR(dev)][MINOR(dev)] <<
7023 + BLOCK_SIZE_BITS;
7024 + else {
7025 + LINFO(curlun, "unable to find file size: %s\n",
7026 + filename);
7027 + goto out;
7029 + } else if (inode && S_ISREG(inode->i_mode))
7030 + size = inode->i_size;
7031 + else {
7032 + LINFO(curlun, "invalid file type: %s\n", filename);
7033 + goto out;
7036 + /* If we can't read the file, it's no good.
7037 + * If we can't write the file, use it read-only. */
7038 + if (!filp->f_op || !filp->f_op->read) {
7039 + LINFO(curlun, "file not readable: %s\n", filename);
7040 + goto out;
7042 + if (IS_RDONLY(inode) || !filp->f_op->write)
7043 + ro = 1;
7045 + num_sectors = size >> 9; // File size in 512-byte sectors
7046 + if (num_sectors == 0) {
7047 + LINFO(curlun, "file too small: %s\n", filename);
7048 + rc = -ETOOSMALL;
7049 + goto out;
7052 + get_file(filp);
7053 + curlun->ro = ro;
7054 + curlun->filp = filp;
7055 + curlun->file_length = size;
7056 + curlun->num_sectors = num_sectors;
7057 + LDBG(curlun, "open backing file: %s\n", filename);
7058 + rc = 0;
7060 +out:
7061 + filp_close(filp, current->files);
7062 + return rc;
7066 +static void close_backing_file(struct lun *curlun)
7068 + if (curlun->filp) {
7069 + LDBG(curlun, "close backing file\n");
7070 + fput(curlun->filp);
7071 + curlun->filp = NULL;
7075 +static void close_all_backing_files(struct fsg_dev *fsg)
7077 + int i;
7079 + for (i = 0; i < fsg->nluns; ++i)
7080 + close_backing_file(&fsg->luns[i]);
7084 +/*-------------------------------------------------------------------------*/
7086 +static void fsg_unbind(struct usb_gadget *gadget)
7088 + struct fsg_dev *fsg = get_gadget_data(gadget);
7089 + int i;
7090 + struct usb_request *req = fsg->ep0req;
7092 + DBG(fsg, "unbind\n");
7093 + clear_bit(REGISTERED, &fsg->atomic_bitflags);
7095 + /* If the thread isn't already dead, tell it to exit now */
7096 + if (fsg->state != FSG_STATE_TERMINATED) {
7097 + raise_exception(fsg, FSG_STATE_EXIT);
7098 + wait_for_completion(&fsg->thread_notifier);
7100 + /* The cleanup routine waits for this completion also */
7101 + complete(&fsg->thread_notifier);
7104 + /* Free the data buffers */
7105 + for (i = 0; i < NUM_BUFFERS; ++i) {
7106 + struct fsg_buffhd *bh = &fsg->buffhds[i];
7108 + if (bh->buf)
7109 + usb_ep_free_buffer(fsg->bulk_in, bh->buf, bh->dma,
7110 + mod_data.buflen);
7113 + /* Free the request and buffer for endpoint 0 */
7114 + if (req) {
7115 + if (req->buf)
7116 + usb_ep_free_buffer(fsg->ep0, req->buf,
7117 + req->dma, EP0_BUFSIZE);
7118 + usb_ep_free_request(fsg->ep0, req);
7121 + set_gadget_data(gadget, 0);
7125 +static int __init check_parameters(struct fsg_dev *fsg)
7127 + int prot;
7129 + /* Store the default values */
7130 + mod_data.transport_type = USB_PR_BULK;
7131 + mod_data.transport_name = "Bulk-only";
7132 + mod_data.protocol_type = USB_SC_SCSI;
7133 + mod_data.protocol_name = "Transparent SCSI";
7135 + if (gadget_is_sh(fsg->gadget))
7136 + mod_data.can_stall = 0;
7138 + if (mod_data.release == 0xffff) { // Parameter wasn't set
7139 + if (gadget_is_net2280(fsg->gadget))
7140 + mod_data.release = __constant_cpu_to_le16(0x0221);
7141 + else if (gadget_is_dummy(fsg->gadget))
7142 + mod_data.release = __constant_cpu_to_le16(0x0222);
7143 + else if (gadget_is_pxa(fsg->gadget))
7144 + mod_data.release = __constant_cpu_to_le16(0x0223);
7145 + else if (gadget_is_sh(fsg->gadget))
7146 + mod_data.release = __constant_cpu_to_le16(0x0224);
7148 + /* The sa1100 controller is not supported */
7150 + else if (gadget_is_goku(fsg->gadget))
7151 + mod_data.release = __constant_cpu_to_le16(0x0226);
7152 + else if (gadget_is_mq11xx(fsg->gadget))
7153 + mod_data.release = __constant_cpu_to_le16(0x0227);
7154 + else if (gadget_is_omap(fsg->gadget))
7155 + mod_data.release = __constant_cpu_to_le16(0x0228);
7156 + else {
7157 + WARN(fsg, "controller '%s' not recognized\n",
7158 + fsg->gadget->name);
7159 + mod_data.release = __constant_cpu_to_le16(0x0299);
7163 + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
7165 +#ifdef CONFIG_USB_FILE_STORAGE_TEST
7166 + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
7167 + ; // Use default setting
7168 + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
7169 + mod_data.transport_type = USB_PR_CB;
7170 + mod_data.transport_name = "Control-Bulk";
7171 + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
7172 + mod_data.transport_type = USB_PR_CBI;
7173 + mod_data.transport_name = "Control-Bulk-Interrupt";
7174 + } else {
7175 + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
7176 + return -EINVAL;
7179 + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
7180 + prot == USB_SC_SCSI) {
7181 + ; // Use default setting
7182 + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
7183 + prot == USB_SC_RBC) {
7184 + mod_data.protocol_type = USB_SC_RBC;
7185 + mod_data.protocol_name = "RBC";
7186 + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
7187 + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
7188 + prot == USB_SC_8020) {
7189 + mod_data.protocol_type = USB_SC_8020;
7190 + mod_data.protocol_name = "8020i (ATAPI)";
7191 + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
7192 + prot == USB_SC_QIC) {
7193 + mod_data.protocol_type = USB_SC_QIC;
7194 + mod_data.protocol_name = "QIC-157";
7195 + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
7196 + prot == USB_SC_UFI) {
7197 + mod_data.protocol_type = USB_SC_UFI;
7198 + mod_data.protocol_name = "UFI";
7199 + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
7200 + prot == USB_SC_8070) {
7201 + mod_data.protocol_type = USB_SC_8070;
7202 + mod_data.protocol_name = "8070i";
7203 + } else {
7204 + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
7205 + return -EINVAL;
7208 + mod_data.buflen &= PAGE_CACHE_MASK;
7209 + if (mod_data.buflen <= 0) {
7210 + ERROR(fsg, "invalid buflen\n");
7211 + return -ETOOSMALL;
7213 +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
7215 + return 0;
7219 +static int __init fsg_bind(struct usb_gadget *gadget)
7221 + struct fsg_dev *fsg = the_fsg;
7222 + int rc;
7223 + int i;
7224 + struct lun *curlun;
7225 + struct usb_ep *ep;
7226 + struct usb_request *req;
7227 + char *pathbuf, *p;
7229 + fsg->gadget = gadget;
7230 + set_gadget_data(gadget, fsg);
7231 + fsg->ep0 = gadget->ep0;
7232 + fsg->ep0->driver_data = fsg;
7234 + if ((rc = check_parameters(fsg)) != 0)
7235 + goto out;
7237 + /* Find out how many LUNs there should be */
7238 + i = mod_data.nluns;
7239 + if (i == 0) {
7240 + for (i = MAX_LUNS; i > 1; --i) {
7241 + if (file[i - 1])
7242 + break;
7245 + if (i > MAX_LUNS) {
7246 + ERROR(fsg, "invalid number of LUNs: %d\n", i);
7247 + rc = -EINVAL;
7248 + goto out;
7251 + /* Create the LUNs and open their backing files. We can't register
7252 + * the LUN devices until the gadget itself is registered, which
7253 + * doesn't happen until after fsg_bind() returns. */
7254 + fsg->luns = kmalloc(i * sizeof(struct lun), GFP_KERNEL);
7255 + if (!fsg->luns) {
7256 + rc = -ENOMEM;
7257 + goto out;
7259 + memset(fsg->luns, 0, i * sizeof(struct lun));
7260 + fsg->nluns = i;
7262 + for (i = 0; i < fsg->nluns; ++i) {
7263 + curlun = &fsg->luns[i];
7264 + curlun->ro = ro[i];
7265 + curlun->dev.driver_data = fsg;
7266 + snprintf(curlun->dev.name, BUS_ID_SIZE,
7267 + "%s-lun%d", gadget->name, i);
7269 + if (file[i] && *file[i]) {
7270 + if ((rc = open_backing_file(curlun, file[i])) != 0)
7271 + goto out;
7272 + } else if (!mod_data.removable) {
7273 + ERROR(fsg, "no file given for LUN%d\n", i);
7274 + rc = -EINVAL;
7275 + goto out;
7279 + /* Find all the endpoints we will use */
7280 + usb_ep_autoconfig_reset(gadget);
7281 + ep = usb_ep_autoconfig(gadget, &fs_bulk_in_desc);
7282 + if (!ep)
7283 + goto autoconf_fail;
7284 + ep->driver_data = fsg; // claim the endpoint
7285 + fsg->bulk_in = ep;
7287 + ep = usb_ep_autoconfig(gadget, &fs_bulk_out_desc);
7288 + if (!ep)
7289 + goto autoconf_fail;
7290 + ep->driver_data = fsg; // claim the endpoint
7291 + fsg->bulk_out = ep;
7293 + if (transport_is_cbi()) {
7294 + ep = usb_ep_autoconfig(gadget, &fs_intr_in_desc);
7295 + if (!ep)
7296 + goto autoconf_fail;
7297 + ep->driver_data = fsg; // claim the endpoint
7298 + fsg->intr_in = ep;
7301 + /* Fix up the descriptors */
7302 + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
7303 + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
7304 + device_desc.idProduct = cpu_to_le16(mod_data.product);
7305 + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
7307 + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
7308 + intf_desc.bNumEndpoints = i;
7309 + intf_desc.bInterfaceSubClass = mod_data.protocol_type;
7310 + intf_desc.bInterfaceProtocol = mod_data.transport_type;
7311 + fs_function[i+1] = NULL;
7313 +#ifdef CONFIG_USB_GADGET_DUALSPEED
7314 + hs_function[i+1] = NULL;
7316 + /* Assume ep0 uses the same maxpacket value for both speeds */
7317 + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
7319 + /* Assume that all endpoint addresses are the same for both speeds */
7320 + hs_bulk_in_desc.bEndpointAddress = fs_bulk_in_desc.bEndpointAddress;
7321 + hs_bulk_out_desc.bEndpointAddress = fs_bulk_out_desc.bEndpointAddress;
7322 + hs_intr_in_desc.bEndpointAddress = fs_intr_in_desc.bEndpointAddress;
7323 +#endif
7325 + rc = -ENOMEM;
7327 + /* Allocate the request and buffer for endpoint 0 */
7328 + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
7329 + if (!req)
7330 + goto out;
7331 + req->buf = usb_ep_alloc_buffer(fsg->ep0, EP0_BUFSIZE,
7332 + &req->dma, GFP_KERNEL);
7333 + if (!req->buf)
7334 + goto out;
7335 + req->complete = ep0_complete;
7337 + /* Allocate the data buffers */
7338 + for (i = 0; i < NUM_BUFFERS; ++i) {
7339 + struct fsg_buffhd *bh = &fsg->buffhds[i];
7341 + bh->buf = usb_ep_alloc_buffer(fsg->bulk_in, mod_data.buflen,
7342 + &bh->dma, GFP_KERNEL);
7343 + if (!bh->buf)
7344 + goto out;
7345 + bh->next = bh + 1;
7347 + fsg->buffhds[NUM_BUFFERS - 1].next = &fsg->buffhds[0];
7349 + /* This should reflect the actual gadget power source */
7350 + usb_gadget_set_selfpowered(gadget);
7352 + snprintf(manufacturer, sizeof manufacturer,
7353 + UTS_SYSNAME " " UTS_RELEASE " with %s",
7354 + gadget->name);
7356 + /* On a real device, serial[] would be loaded from permanent
7357 + * storage. We just encode it from the driver version string. */
7358 + for (i = 0; i < sizeof(serial) - 2; i += 2) {
7359 + unsigned char c = DRIVER_VERSION[i / 2];
7361 + if (!c)
7362 + break;
7363 + sprintf(&serial[i], "%02X", c);
7366 + if ((rc = kernel_thread(fsg_main_thread, fsg, (CLONE_VM | CLONE_FS |
7367 + CLONE_FILES))) < 0)
7368 + goto out;
7369 + fsg->thread_pid = rc;
7371 + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
7372 + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
7374 + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
7375 + for (i = 0; i < fsg->nluns; ++i) {
7376 + curlun = &fsg->luns[i];
7377 + if (backing_file_is_open(curlun)) {
7378 + p = NULL;
7379 + if (pathbuf) {
7380 + p = d_path(curlun->filp->f_dentry,
7381 + curlun->filp->f_vfsmnt,
7382 + pathbuf, PATH_MAX);
7383 + if (IS_ERR(p))
7384 + p = NULL;
7386 + LINFO(curlun, "ro=%d, file: %s\n",
7387 + curlun->ro, (p ? p : "(error)"));
7390 + kfree(pathbuf);
7392 + DBG(fsg, "transport=%s (x%02x)\n",
7393 + mod_data.transport_name, mod_data.transport_type);
7394 + DBG(fsg, "protocol=%s (x%02x)\n",
7395 + mod_data.protocol_name, mod_data.protocol_type);
7396 + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
7397 + mod_data.vendor, mod_data.product, mod_data.release);
7398 + DBG(fsg, "removable=%d, stall=%d, buflen=%u\n",
7399 + mod_data.removable, mod_data.can_stall,
7400 + mod_data.buflen);
7401 + DBG(fsg, "I/O thread pid: %d\n", fsg->thread_pid);
7402 + return 0;
7404 +autoconf_fail:
7405 + ERROR(fsg, "unable to autoconfigure all endpoints\n");
7406 + rc = -ENOTSUPP;
7408 +out:
7409 + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
7410 + fsg_unbind(gadget);
7411 + close_all_backing_files(fsg);
7412 + return rc;
7416 +/*-------------------------------------------------------------------------*/
7418 +static struct usb_gadget_driver fsg_driver = {
7419 +#ifdef CONFIG_USB_GADGET_DUALSPEED
7420 + .speed = USB_SPEED_HIGH,
7421 +#else
7422 + .speed = USB_SPEED_FULL,
7423 +#endif
7424 + .function = (char *) longname,
7425 + .bind = fsg_bind,
7426 + .unbind = fsg_unbind,
7427 + .disconnect = fsg_disconnect,
7428 + .setup = fsg_setup,
7430 + .driver = {
7431 + .name = (char *) shortname,
7432 + // .release = ...
7433 + // .suspend = ...
7434 + // .resume = ...
7435 + },
7439 +static int __init fsg_alloc(void)
7441 + struct fsg_dev *fsg;
7443 + fsg = kmalloc(sizeof *fsg, GFP_KERNEL);
7444 + if (!fsg)
7445 + return -ENOMEM;
7446 + memset(fsg, 0, sizeof *fsg);
7447 + spin_lock_init(&fsg->lock);
7448 + init_rwsem(&fsg->filesem);
7449 + init_waitqueue_head(&fsg->thread_wqh);
7450 + init_completion(&fsg->thread_notifier);
7452 + the_fsg = fsg;
7453 + return 0;
7457 +static void fsg_free(struct fsg_dev *fsg)
7459 + kfree(fsg->luns);
7460 + kfree(fsg);
7464 +static int __init fsg_init(void)
7466 + int rc;
7467 + struct fsg_dev *fsg;
7469 + /* Put the module parameters where they belong -- arghh! */
7470 + mod_data.nluns = luns;
7471 + mod_data.transport_parm = transport;
7472 + mod_data.protocol_parm = protocol;
7473 + mod_data.removable = removable;
7474 + mod_data.vendor = vendor;
7475 + mod_data.product = product;
7476 + mod_data.release = release;
7477 + mod_data.buflen = buflen;
7478 + mod_data.can_stall = stall;
7480 + if ((rc = fsg_alloc()) != 0)
7481 + return rc;
7482 + fsg = the_fsg;
7483 + if ((rc = usb_gadget_register_driver(&fsg_driver)) != 0) {
7484 + fsg_free(fsg);
7485 + return rc;
7487 + set_bit(REGISTERED, &fsg->atomic_bitflags);
7489 + /* Tell the thread to start working */
7490 + complete(&fsg->thread_notifier);
7491 + return 0;
7493 +module_init(fsg_init);
7496 +static void __exit fsg_cleanup(void)
7498 + struct fsg_dev *fsg = the_fsg;
7500 + /* Unregister the driver iff the thread hasn't already done so */
7501 + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
7502 + usb_gadget_unregister_driver(&fsg_driver);
7504 + /* Wait for the thread to finish up */
7505 + wait_for_completion(&fsg->thread_notifier);
7507 + close_all_backing_files(fsg);
7508 + fsg_free(fsg);
7510 +module_exit(fsg_cleanup);
7511 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/gadget_chips.h kernel/drivers/usb/gadget/gadget_chips.h
7512 --- /tmp/kernel/drivers/usb/gadget/gadget_chips.h 1970-01-01 01:00:00.000000000 +0100
7513 +++ kernel/drivers/usb/gadget/gadget_chips.h 2005-04-22 17:53:19.434539516 +0200
7514 @@ -0,0 +1,92 @@
7516 + * USB device controllers have lots of quirks. Use these macros in
7517 + * gadget drivers or other code that needs to deal with them, and which
7518 + * autoconfigures instead of using early binding to the hardware.
7520 + * This could eventually work like the ARM mach_is_*() stuff, driven by
7521 + * some config file that gets updated as new hardware is supported.
7523 + * NOTE: some of these controller drivers may not be available yet.
7524 + */
7525 +#ifdef CONFIG_USB_GADGET_NET2280
7526 +#define gadget_is_net2280(g) !strcmp("net2280", (g)->name)
7527 +#else
7528 +#define gadget_is_net2280(g) 0
7529 +#endif
7531 +#ifdef CONFIG_USB_GADGET_DUMMY_HCD
7532 +#define gadget_is_dummy(g) !strcmp("dummy_udc", (g)->name)
7533 +#else
7534 +#define gadget_is_dummy(g) 0
7535 +#endif
7537 +#ifdef CONFIG_USB_GADGET_PXA2XX
7538 +#define gadget_is_pxa(g) !strcmp("pxa2xx_udc", (g)->name)
7539 +#else
7540 +#define gadget_is_pxa(g) 0
7541 +#endif
7543 +#ifdef CONFIG_USB_GADGET_GOKU
7544 +#define gadget_is_goku(g) !strcmp("goku_udc", (g)->name)
7545 +#else
7546 +#define gadget_is_goku(g) 0
7547 +#endif
7549 +#ifdef CONFIG_USB_GADGET_SUPERH
7550 +#define gadget_is_sh(g) !strcmp("sh_udc", (g)->name)
7551 +#else
7552 +#define gadget_is_sh(g) 0
7553 +#endif
7555 +#ifdef CONFIG_USB_GADGET_SA1100
7556 +#define gadget_is_sa1100(g) !strcmp("sa1100_udc", (g)->name)
7557 +#else
7558 +#define gadget_is_sa1100(g) 0
7559 +#endif
7561 +#ifdef CONFIG_USB_GADGET_LH7A40X
7562 +#define gadget_is_lh7a40x(g) !strcmp("lh7a40x_udc", (g)->name)
7563 +#else
7564 +#define gadget_is_lh7a40x(g) 0
7565 +#endif
7567 +#ifdef CONFIG_USB_GADGET_MQ11XX
7568 +#define gadget_is_mq11xx(g) !strcmp("mq11xx_udc", (g)->name)
7569 +#else
7570 +#define gadget_is_mq11xx(g) 0
7571 +#endif
7573 +#ifdef CONFIG_USB_GADGET_OMAP
7574 +#define gadget_is_omap(g) !strcmp("omap_udc", (g)->name)
7575 +#else
7576 +#define gadget_is_omap(g) 0
7577 +#endif
7579 +#ifdef CONFIG_USB_GADGET_N9604
7580 +#define gadget_is_n9604(g) !strcmp("n9604_udc", (g)->name)
7581 +#else
7582 +#define gadget_is_n9604(g) 0
7583 +#endif
7585 +#ifdef CONFIG_USB_GADGET_PXA27X
7586 +#define gadget_is_pxa27x(g) !strcmp("pxa27x_udc", (g)->name)
7587 +#else
7588 +#define gadget_is_pxa27x(g) 0
7589 +#endif
7591 +#ifdef CONFIG_USB_GADGET_S3C2410
7592 +#define gadget_is_s3c2410(g) !strcmp("s3c2410_udc", (g)->name)
7593 +#else
7594 +#define gadget_is_s3c2410(g) 0
7595 +#endif
7597 +#ifdef CONFIG_USB_GADGET_AT91
7598 +#define gadget_is_at91(g) !strcmp("at91_udc", (g)->name)
7599 +#else
7600 +#define gadget_is_at91(g) 0
7601 +#endif
7603 +// CONFIG_USB_GADGET_SX2
7604 +// CONFIG_USB_GADGET_AU1X00
7605 +// ...
7607 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/goku_udc.c kernel/drivers/usb/gadget/goku_udc.c
7608 --- /tmp/kernel/drivers/usb/gadget/goku_udc.c 1970-01-01 01:00:00.000000000 +0100
7609 +++ kernel/drivers/usb/gadget/goku_udc.c 2005-04-22 17:53:19.440538539 +0200
7610 @@ -0,0 +1,1975 @@
7612 + * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
7614 + * Copyright (C) 2000-2002 Lineo
7615 + * by Stuart Lynne, Tom Rushworth, and Bruce Balden
7616 + * Copyright (C) 2002 Toshiba Corporation
7617 + * Copyright (C) 2003 MontaVista Software (source@mvista.com)
7619 + * This file is licensed under the terms of the GNU General Public
7620 + * License version 2. This program is licensed "as is" without any
7621 + * warranty of any kind, whether express or implied.
7622 + */
7625 + * This device has ep0 and three semi-configurable bulk/interrupt endpoints.
7627 + * - Endpoint numbering is fixed: ep{1,2,3}-bulk
7628 + * - Gadget drivers can choose ep maxpacket (8/16/32/64)
7629 + * - Gadget drivers can choose direction (IN, OUT)
7630 + * - DMA works with ep1 (OUT transfers) and ep2 (IN transfers).
7631 + */
7633 +#undef DEBUG
7634 +// #define VERBOSE /* extra debug messages (success too) */
7635 +// #define USB_TRACE /* packet-level success messages */
7637 +#include <linux/config.h>
7638 +#include <linux/kernel.h>
7639 +#include <linux/module.h>
7640 +#include <linux/pci.h>
7641 +#include <linux/delay.h>
7642 +#include <linux/ioport.h>
7643 +#include <linux/sched.h>
7644 +#include <linux/slab.h>
7645 +#include <linux/smp_lock.h>
7646 +#include <linux/errno.h>
7647 +#include <linux/init.h>
7648 +#include <linux/timer.h>
7649 +#include <linux/list.h>
7650 +#include <linux/interrupt.h>
7651 +#include <linux/proc_fs.h>
7652 +#include <linux/usb_ch9.h>
7653 +#include <linux/usb_gadget.h>
7655 +#include <asm/byteorder.h>
7656 +#include <asm/io.h>
7657 +#include <asm/irq.h>
7658 +#include <asm/system.h>
7659 +#include <asm/unaligned.h>
7662 +#include "goku_udc.h"
7664 +#define DRIVER_DESC "TC86C001 USB Device Controller"
7665 +#define DRIVER_VERSION "30-Oct 2003"
7667 +#define DMA_ADDR_INVALID (~(dma_addr_t)0)
7669 +static const char driver_name [] = "goku_udc";
7670 +static const char driver_desc [] = DRIVER_DESC;
7672 +MODULE_AUTHOR("source@mvista.com");
7673 +MODULE_DESCRIPTION(DRIVER_DESC);
7674 +MODULE_LICENSE("GPL");
7678 + * IN dma behaves ok under testing, though the IN-dma abort paths don't
7679 + * seem to behave quite as expected. Used by default.
7681 + * OUT dma documents design problems handling the common "short packet"
7682 + * transfer termination policy; it couldn't enabled by default, even
7683 + * if the OUT-dma abort problems had a resolution.
7684 + */
7685 +static unsigned use_dma = 1;
7687 +#if 0
7688 +//#include <linux/moduleparam.h>
7689 +/* "modprobe goku_udc use_dma=1" etc
7690 + * 0 to disable dma
7691 + * 1 to use IN dma only (normal operation)
7692 + * 2 to use IN and OUT dma
7693 + */
7694 +module_param(use_dma, uint, S_IRUGO);
7695 +#endif
7697 +/*-------------------------------------------------------------------------*/
7699 +static void nuke(struct goku_ep *, int status);
7701 +static inline void
7702 +command(struct goku_udc_regs *regs, int command, unsigned epnum)
7704 + writel(COMMAND_EP(epnum) | command, &regs->Command);
7705 + udelay(300);
7708 +static int
7709 +goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
7711 + struct goku_udc *dev;
7712 + struct goku_ep *ep;
7713 + u32 mode;
7714 + u16 max;
7715 + unsigned long flags;
7717 + ep = container_of(_ep, struct goku_ep, ep);
7718 + if (!_ep || !desc || ep->desc
7719 + || desc->bDescriptorType != USB_DT_ENDPOINT)
7720 + return -EINVAL;
7721 + dev = ep->dev;
7722 + if (ep == &dev->ep[0])
7723 + return -EINVAL;
7724 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
7725 + return -ESHUTDOWN;
7726 + if (ep->num != (desc->bEndpointAddress & 0x0f))
7727 + return -EINVAL;
7729 + switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
7730 + case USB_ENDPOINT_XFER_BULK:
7731 + case USB_ENDPOINT_XFER_INT:
7732 + break;
7733 + default:
7734 + return -EINVAL;
7737 + if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
7738 + != EPxSTATUS_EP_INVALID)
7739 + return -EBUSY;
7741 + /* enabling the no-toggle interrupt mode would need an api hook */
7742 + mode = 0;
7743 + max = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
7744 + switch (max) {
7745 + case 64: mode++;
7746 + case 32: mode++;
7747 + case 16: mode++;
7748 + case 8: mode <<= 3;
7749 + break;
7750 + default:
7751 + return -EINVAL;
7753 + mode |= 2 << 1; /* bulk, or intr-with-toggle */
7755 + /* ep1/ep2 dma direction is chosen early; it works in the other
7756 + * direction, with pio. be cautious with out-dma.
7757 + */
7758 + ep->is_in = (USB_DIR_IN & desc->bEndpointAddress) != 0;
7759 + if (ep->is_in) {
7760 + mode |= 1;
7761 + ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
7762 + } else {
7763 + ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
7764 + if (ep->dma)
7765 + DBG(dev, "%s out-dma hides short packets\n",
7766 + ep->ep.name);
7769 + spin_lock_irqsave(&ep->dev->lock, flags);
7771 + /* ep1 and ep2 can do double buffering and/or dma */
7772 + if (ep->num < 3) {
7773 + struct goku_udc_regs *regs = ep->dev->regs;
7774 + u32 tmp;
7776 + /* double buffer except (for now) with pio in */
7777 + tmp = ((ep->dma || !ep->is_in)
7778 + ? 0x10 /* double buffered */
7779 + : 0x11 /* single buffer */
7780 + ) << ep->num;
7781 + tmp |= readl(&regs->EPxSingle);
7782 + writel(tmp, &regs->EPxSingle);
7784 + tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
7785 + tmp |= readl(&regs->EPxBCS);
7786 + writel(tmp, &regs->EPxBCS);
7788 + writel(mode, ep->reg_mode);
7789 + command(ep->dev->regs, COMMAND_RESET, ep->num);
7790 + ep->ep.maxpacket = max;
7791 + ep->stopped = 0;
7792 + ep->desc = desc;
7793 + spin_unlock_irqrestore(&ep->dev->lock, flags);
7795 + DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
7796 + ep->is_in ? "IN" : "OUT",
7797 + ep->dma ? "dma" : "pio",
7798 + max);
7800 + return 0;
7803 +static void ep_reset(struct goku_udc_regs *regs, struct goku_ep *ep)
7805 + struct goku_udc *dev = ep->dev;
7807 + if (regs) {
7808 + command(regs, COMMAND_INVALID, ep->num);
7809 + if (ep->num) {
7810 + if (ep->num == UDC_MSTWR_ENDPOINT)
7811 + dev->int_enable &= ~(INT_MSTWREND
7812 + |INT_MSTWRTMOUT);
7813 + else if (ep->num == UDC_MSTRD_ENDPOINT)
7814 + dev->int_enable &= ~INT_MSTRDEND;
7815 + dev->int_enable &= ~INT_EPxDATASET (ep->num);
7816 + } else
7817 + dev->int_enable &= ~INT_EP0;
7818 + writel(dev->int_enable, &regs->int_enable);
7819 + readl(&regs->int_enable);
7820 + if (ep->num < 3) {
7821 + struct goku_udc_regs *regs = ep->dev->regs;
7822 + u32 tmp;
7824 + tmp = readl(&regs->EPxSingle);
7825 + tmp &= ~(0x11 << ep->num);
7826 + writel(tmp, &regs->EPxSingle);
7828 + tmp = readl(&regs->EPxBCS);
7829 + tmp &= ~(0x11 << ep->num);
7830 + writel(tmp, &regs->EPxBCS);
7832 + /* reset dma in case we're still using it */
7833 + if (ep->dma) {
7834 + u32 master;
7836 + master = readl(&regs->dma_master) & MST_RW_BITS;
7837 + if (ep->num == UDC_MSTWR_ENDPOINT) {
7838 + master &= ~MST_W_BITS;
7839 + master |= MST_WR_RESET;
7840 + } else {
7841 + master &= ~MST_R_BITS;
7842 + master |= MST_RD_RESET;
7844 + writel(master, &regs->dma_master);
7848 + ep->ep.maxpacket = MAX_FIFO_SIZE;
7849 + ep->desc = 0;
7850 + ep->stopped = 1;
7851 + ep->irqs = 0;
7852 + ep->dma = 0;
7855 +static int goku_ep_disable(struct usb_ep *_ep)
7857 + struct goku_ep *ep;
7858 + struct goku_udc *dev;
7859 + unsigned long flags;
7861 + ep = container_of(_ep, struct goku_ep, ep);
7862 + if (!_ep || !ep->desc)
7863 + return -ENODEV;
7864 + dev = ep->dev;
7865 + if (dev->ep0state == EP0_SUSPEND)
7866 + return -EBUSY;
7868 + VDBG(dev, "disable %s\n", _ep->name);
7870 + spin_lock_irqsave(&dev->lock, flags);
7871 + nuke(ep, -ESHUTDOWN);
7872 + ep_reset(dev->regs, ep);
7873 + spin_unlock_irqrestore(&dev->lock, flags);
7875 + return 0;
7878 +/*-------------------------------------------------------------------------*/
7880 +static struct usb_request *
7881 +goku_alloc_request(struct usb_ep *_ep, int gfp_flags)
7883 + struct goku_request *req;
7885 + if (!_ep)
7886 + return 0;
7887 + req = kmalloc(sizeof *req, gfp_flags);
7888 + if (!req)
7889 + return 0;
7891 + memset(req, 0, sizeof *req);
7892 + req->req.dma = DMA_ADDR_INVALID;
7893 + INIT_LIST_HEAD(&req->queue);
7894 + return &req->req;
7897 +static void
7898 +goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
7900 + struct goku_request *req;
7902 + if (!_ep || !_req)
7903 + return;
7905 + req = container_of(_req, struct goku_request, req);
7906 + WARN_ON(!list_empty(&req->queue));
7907 + kfree(req);
7910 +/*-------------------------------------------------------------------------*/
7912 +#undef USE_KMALLOC
7914 +/* many common platforms have dma-coherent caches, which means that it's
7915 + * safe to use kmalloc() memory for all i/o buffers without using any
7916 + * cache flushing calls. (unless you're trying to share cache lines
7917 + * between dma and non-dma activities, which is a slow idea in any case.)
7919 + * other platforms need more care, with 2.6 having a moderately general
7920 + * solution except for the common "buffer is smaller than a page" case.
7921 + */
7922 +#if defined(CONFIG_X86)
7923 +#define USE_KMALLOC
7925 +#elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
7926 +#define USE_KMALLOC
7928 +#elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
7929 +#define USE_KMALLOC
7931 +#endif
7933 +/* allocating buffers this way eliminates dma mapping overhead, which
7934 + * on some platforms will mean eliminating a per-io buffer copy. with
7935 + * some kinds of system caches, further tweaks may still be needed.
7936 + */
7937 +static void *
7938 +goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
7939 + dma_addr_t *dma, int gfp_flags)
7941 + void *retval;
7942 + struct goku_ep *ep;
7944 + ep = container_of(_ep, struct goku_ep, ep);
7945 + if (!_ep)
7946 + return 0;
7947 + *dma = DMA_ADDR_INVALID;
7949 +#if defined(USE_KMALLOC)
7950 + retval = kmalloc(bytes, gfp_flags);
7951 + if (retval)
7952 + *dma = virt_to_phys(retval);
7953 +#else
7954 + if (ep->dma) {
7955 + /* one problem with this call is that it wastes memory on
7956 + * typical 1/N page allocations: it allocates 1-N pages.
7957 + * another is that it always uses GFP_ATOMIC.
7958 + */
7959 +#warning Using pci_alloc_consistent even with buffers smaller than a page.
7960 + retval = pci_alloc_consistent(ep->dev->pdev, bytes, dma);
7961 + } else
7962 + retval = kmalloc(bytes, gfp_flags);
7963 +#endif
7964 + return retval;
7967 +static void
7968 +goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
7970 + /* free memory into the right allocator */
7971 +#ifndef USE_KMALLOC
7972 + if (dma != DMA_ADDR_INVALID) {
7973 + struct goku_ep *ep;
7975 + ep = container_of(_ep, struct goku_ep, ep);
7976 + if (!_ep)
7977 + return;
7978 + /* one problem with this call is that some platforms
7979 + * don't allow it to be used in_irq().
7980 + */
7981 + pci_free_consistent(ep->dev->pdev, bytes, buf, dma);
7982 + } else
7983 +#endif
7984 + kfree (buf);
7987 +/*-------------------------------------------------------------------------*/
7989 +static void
7990 +done(struct goku_ep *ep, struct goku_request *req, int status)
7992 + struct goku_udc *dev;
7993 + unsigned stopped = ep->stopped;
7995 + list_del_init(&req->queue);
7997 + if (likely(req->req.status == -EINPROGRESS))
7998 + req->req.status = status;
7999 + else
8000 + status = req->req.status;
8002 + dev = ep->dev;
8003 + if (req->mapped) {
8004 + pci_unmap_single(dev->pdev, req->req.dma, req->req.length,
8005 + ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
8006 + req->req.dma = DMA_ADDR_INVALID;
8007 + req->mapped = 0;
8010 +#ifndef USB_TRACE
8011 + if (status && status != -ESHUTDOWN)
8012 +#endif
8013 + VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
8014 + ep->ep.name, &req->req, status,
8015 + req->req.actual, req->req.length);
8017 + /* don't modify queue heads during completion callback */
8018 + ep->stopped = 1;
8019 + spin_unlock(&dev->lock);
8020 + req->req.complete(&ep->ep, &req->req);
8021 + spin_lock(&dev->lock);
8022 + ep->stopped = stopped;
8025 +/*-------------------------------------------------------------------------*/
8027 +static inline int
8028 +write_packet(u32 *fifo, u8 *buf, struct goku_request *req, unsigned max)
8030 + unsigned length, count;
8032 + length = min(req->req.length - req->req.actual, max);
8033 + req->req.actual += length;
8035 + count = length;
8036 + while (likely(count--))
8037 + writel(*buf++, fifo);
8038 + return length;
8041 +// return: 0 = still running, 1 = completed, negative = errno
8042 +static int write_fifo(struct goku_ep *ep, struct goku_request *req)
8044 + struct goku_udc *dev = ep->dev;
8045 + u32 tmp;
8046 + u8 *buf;
8047 + unsigned count;
8048 + int is_last;
8050 + tmp = readl(&dev->regs->DataSet);
8051 + buf = req->req.buf + req->req.actual;
8052 + prefetch(buf);
8054 + dev = ep->dev;
8055 + if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
8056 + return -EL2HLT;
8058 + /* NOTE: just single-buffered PIO-IN for now. */
8059 + if (unlikely((tmp & DATASET_A(ep->num)) != 0))
8060 + return 0;
8062 + /* clear our "packet available" irq */
8063 + if (ep->num != 0)
8064 + writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
8066 + count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
8068 + /* last packet often short (sometimes a zlp, especially on ep0) */
8069 + if (unlikely(count != ep->ep.maxpacket)) {
8070 + writel(~(1<<ep->num), &dev->regs->EOP);
8071 + if (ep->num == 0) {
8072 + dev->ep[0].stopped = 1;
8073 + dev->ep0state = EP0_STATUS;
8075 + is_last = 1;
8076 + } else {
8077 + if (likely(req->req.length != req->req.actual)
8078 + || req->req.zero)
8079 + is_last = 0;
8080 + else
8081 + is_last = 1;
8083 +#if 0 /* printk seemed to trash is_last...*/
8084 +//#ifdef USB_TRACE
8085 + VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
8086 + ep->ep.name, count, is_last ? "/last" : "",
8087 + req->req.length - req->req.actual, req);
8088 +#endif
8090 + /* requests complete when all IN data is in the FIFO,
8091 + * or sometimes later, if a zlp was needed.
8092 + */
8093 + if (is_last) {
8094 + done(ep, req, 0);
8095 + return 1;
8098 + return 0;
8101 +static int read_fifo(struct goku_ep *ep, struct goku_request *req)
8103 + struct goku_udc_regs *regs;
8104 + u32 size, set;
8105 + u8 *buf;
8106 + unsigned bufferspace, is_short, dbuff;
8108 + regs = ep->dev->regs;
8109 +top:
8110 + buf = req->req.buf + req->req.actual;
8111 + prefetchw(buf);
8113 + if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
8114 + return -EL2HLT;
8116 + dbuff = (ep->num == 1 || ep->num == 2);
8117 + do {
8118 + /* ack dataset irq matching the status we'll handle */
8119 + if (ep->num != 0)
8120 + writel(~INT_EPxDATASET(ep->num), &regs->int_status);
8122 + set = readl(&regs->DataSet) & DATASET_AB(ep->num);
8123 + size = readl(&regs->EPxSizeLA[ep->num]);
8124 + bufferspace = req->req.length - req->req.actual;
8126 + /* usually do nothing without an OUT packet */
8127 + if (likely(ep->num != 0 || bufferspace != 0)) {
8128 + if (unlikely(set == 0))
8129 + break;
8130 + /* use ep1/ep2 double-buffering for OUT */
8131 + if (!(size & PACKET_ACTIVE))
8132 + size = readl(&regs->EPxSizeLB[ep->num]);
8133 + if (!(size & PACKET_ACTIVE)) // "can't happen"
8134 + break;
8135 + size &= DATASIZE; /* EPxSizeH == 0 */
8137 + /* ep0out no-out-data case for set_config, etc */
8138 + } else
8139 + size = 0;
8141 + /* read all bytes from this packet */
8142 + req->req.actual += size;
8143 + is_short = (size < ep->ep.maxpacket);
8144 +#ifdef USB_TRACE
8145 + VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
8146 + ep->ep.name, size, is_short ? "/S" : "",
8147 + req, req->req.actual, req->req.length);
8148 +#endif
8149 + while (likely(size-- != 0)) {
8150 + u8 byte = (u8) readl(ep->reg_fifo);
8152 + if (unlikely(bufferspace == 0)) {
8153 + /* this happens when the driver's buffer
8154 + * is smaller than what the host sent.
8155 + * discard the extra data in this packet.
8156 + */
8157 + if (req->req.status != -EOVERFLOW)
8158 + DBG(ep->dev, "%s overflow %u\n",
8159 + ep->ep.name, size);
8160 + req->req.status = -EOVERFLOW;
8161 + } else {
8162 + *buf++ = byte;
8163 + bufferspace--;
8167 + /* completion */
8168 + if (unlikely(is_short || req->req.actual == req->req.length)) {
8169 + if (unlikely(ep->num == 0)) {
8170 + /* non-control endpoints now usable? */
8171 + if (ep->dev->req_config)
8172 + writel(ep->dev->configured
8173 + ? USBSTATE_CONFIGURED
8174 + : 0,
8175 + &regs->UsbState);
8176 + /* ep0out status stage */
8177 + writel(~(1<<0), &regs->EOP);
8178 + ep->stopped = 1;
8179 + ep->dev->ep0state = EP0_STATUS;
8181 + done(ep, req, 0);
8183 + /* empty the second buffer asap */
8184 + if (dbuff && !list_empty(&ep->queue)) {
8185 + req = list_entry(ep->queue.next,
8186 + struct goku_request, queue);
8187 + goto top;
8189 + return 1;
8191 + } while (dbuff);
8192 + return 0;
8195 +static inline void
8196 +pio_irq_enable(struct goku_udc *dev, struct goku_udc_regs *regs, int epnum)
8198 + dev->int_enable |= INT_EPxDATASET (epnum);
8199 + writel(dev->int_enable, &regs->int_enable);
8200 + /* write may still be posted */
8203 +static inline void
8204 +pio_irq_disable(struct goku_udc *dev, struct goku_udc_regs *regs, int epnum)
8206 + dev->int_enable &= ~INT_EPxDATASET (epnum);
8207 + writel(dev->int_enable, &regs->int_enable);
8208 + /* write may still be posted */
8211 +static inline void
8212 +pio_advance(struct goku_ep *ep)
8214 + struct goku_request *req;
8216 + if (unlikely(list_empty (&ep->queue)))
8217 + return;
8218 + req = list_entry(ep->queue.next, struct goku_request, queue);
8219 + (ep->is_in ? write_fifo : read_fifo)(ep, req);
8223 +/*-------------------------------------------------------------------------*/
8225 +// return: 0 = q running, 1 = q stopped, negative = errno
8226 +static int start_dma(struct goku_ep *ep, struct goku_request *req)
8228 + struct goku_udc_regs *regs = ep->dev->regs;
8229 + u32 master;
8230 + u32 start = req->req.dma;
8231 + u32 end = start + req->req.length - 1;
8233 + master = readl(&regs->dma_master) & MST_RW_BITS;
8235 + /* re-init the bits affecting IN dma; careful with zlps */
8236 + if (likely(ep->is_in)) {
8237 + if (unlikely(master & MST_RD_ENA)) {
8238 + DBG (ep->dev, "start, IN active dma %03x!!\n",
8239 + master);
8240 +// return -EL2HLT;
8242 + writel(end, &regs->in_dma_end);
8243 + writel(start, &regs->in_dma_start);
8245 + master &= ~MST_R_BITS;
8246 + if (unlikely(req->req.length == 0))
8247 + master = MST_RD_ENA | MST_RD_EOPB;
8248 + else if ((req->req.length % ep->ep.maxpacket) != 0
8249 + || req->req.zero)
8250 + master = MST_RD_ENA | MST_EOPB_ENA;
8251 + else
8252 + master = MST_RD_ENA | MST_EOPB_DIS;
8254 + ep->dev->int_enable |= INT_MSTRDEND;
8256 + /* Goku DMA-OUT merges short packets, which plays poorly with
8257 + * protocols where short packets mark the transfer boundaries.
8258 + * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
8259 + * ending transfers after 3 SOFs; we don't turn it on.
8260 + */
8261 + } else {
8262 + if (unlikely(master & MST_WR_ENA)) {
8263 + DBG (ep->dev, "start, OUT active dma %03x!!\n",
8264 + master);
8265 +// return -EL2HLT;
8267 + writel(end, &regs->out_dma_end);
8268 + writel(start, &regs->out_dma_start);
8270 + master &= ~MST_W_BITS;
8271 + master |= MST_WR_ENA | MST_TIMEOUT_DIS;
8273 + ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
8276 + writel(master, &regs->dma_master);
8277 + writel(ep->dev->int_enable, &regs->int_enable);
8278 + return 0;
8281 +static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
8283 + struct goku_request *req;
8284 + struct goku_udc_regs *regs = ep->dev->regs;
8285 + u32 master;
8287 + master = readl(&regs->dma_master);
8289 + if (unlikely(list_empty(&ep->queue))) {
8290 +stop:
8291 + if (ep->is_in)
8292 + dev->int_enable &= ~INT_MSTRDEND;
8293 + else
8294 + dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
8295 + writel(dev->int_enable, &regs->int_enable);
8296 + return;
8298 + req = list_entry(ep->queue.next, struct goku_request, queue);
8300 + /* normal hw dma completion (not abort) */
8301 + if (likely(ep->is_in)) {
8302 + if (unlikely(master & MST_RD_ENA))
8303 + return;
8304 + req->req.actual = readl(&regs->in_dma_current);
8305 + } else {
8306 + if (unlikely(master & MST_WR_ENA))
8307 + return;
8309 + /* hardware merges short packets, and also hides packet
8310 + * overruns. a partial packet MAY be in the fifo here.
8311 + */
8312 + req->req.actual = readl(&regs->out_dma_current);
8314 + req->req.actual -= req->req.dma;
8315 + req->req.actual++;
8317 +#ifdef USB_TRACE
8318 + VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
8319 + ep->ep.name, ep->is_in ? "IN" : "OUT",
8320 + req->req.actual, req->req.length, req);
8321 +#endif
8322 + done(ep, req, 0);
8323 + if (list_empty(&ep->queue))
8324 + goto stop;
8325 + req = list_entry(ep->queue.next, struct goku_request, queue);
8326 + (void) start_dma(ep, req);
8329 +static void abort_dma(struct goku_ep *ep, int status)
8331 + struct goku_udc_regs *regs = ep->dev->regs;
8332 + struct goku_request *req;
8333 + u32 curr, master;
8335 + /* NAK future host requests, hoping the implicit delay lets the
8336 + * dma engine finish reading (or writing) its latest packet and
8337 + * empty the dma buffer (up to 16 bytes).
8339 + * This avoids needing to clean up a partial packet in the fifo;
8340 + * we can't do that for IN without side effects to HALT and TOGGLE.
8341 + */
8342 + command(regs, COMMAND_FIFO_DISABLE, ep->num);
8343 + req = list_entry(ep->queue.next, struct goku_request, queue);
8344 + master = readl(&regs->dma_master) & MST_RW_BITS;
8346 + /* FIXME using these resets isn't usably documented. this may
8347 + * not work unless it's followed by disabling the endpoint.
8349 + * FIXME the OUT reset path doesn't even behave consistently.
8350 + */
8351 + if (ep->is_in) {
8352 + if (unlikely((readl(&regs->dma_master) & MST_RD_ENA) == 0))
8353 + goto finished;
8354 + curr = readl(&regs->in_dma_current);
8356 + writel(curr, &regs->in_dma_end);
8357 + writel(curr, &regs->in_dma_start);
8359 + master &= ~MST_R_BITS;
8360 + master |= MST_RD_RESET;
8361 + writel(master, &regs->dma_master);
8363 + if (readl(&regs->dma_master) & MST_RD_ENA)
8364 + DBG(ep->dev, "IN dma active after reset!\n");
8366 + } else {
8367 + if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
8368 + goto finished;
8369 + curr = readl(&regs->out_dma_current);
8371 + writel(curr, &regs->out_dma_end);
8372 + writel(curr, &regs->out_dma_start);
8374 + master &= ~MST_W_BITS;
8375 + master |= MST_WR_RESET;
8376 + writel(master, &regs->dma_master);
8378 + if (readl(&regs->dma_master) & MST_WR_ENA)
8379 + DBG(ep->dev, "OUT dma active after reset!\n");
8381 + req->req.actual = (curr - req->req.dma) + 1;
8382 + req->req.status = status;
8384 + VDBG(ep->dev, "%s %s %s %d/%d\n", __FUNCTION__, ep->ep.name,
8385 + ep->is_in ? "IN" : "OUT",
8386 + req->req.actual, req->req.length);
8388 + command(regs, COMMAND_FIFO_ENABLE, ep->num);
8390 + return;
8392 +finished:
8393 + /* dma already completed; no abort needed */
8394 + command(regs, COMMAND_FIFO_ENABLE, ep->num);
8395 + req->req.actual = req->req.length;
8396 + req->req.status = 0;
8399 +/*-------------------------------------------------------------------------*/
8401 +static int
8402 +goku_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
8404 + struct goku_request *req;
8405 + struct goku_ep *ep;
8406 + struct goku_udc *dev;
8407 + unsigned long flags;
8408 + int status;
8410 + /* always require a cpu-view buffer so pio works */
8411 + req = container_of(_req, struct goku_request, req);
8412 + if (unlikely(!_req || !_req->complete
8413 + || !_req->buf || !list_empty(&req->queue)))
8414 + return -EINVAL;
8415 + ep = container_of(_ep, struct goku_ep, ep);
8416 + if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
8417 + return -EINVAL;
8418 + dev = ep->dev;
8419 + if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
8420 + return -ESHUTDOWN;
8422 + /* can't touch registers when suspended */
8423 + if (dev->ep0state == EP0_SUSPEND)
8424 + return -EBUSY;
8426 + /* set up dma mapping in case the caller didn't */
8427 + if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
8428 + _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length,
8429 + ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
8430 + req->mapped = 1;
8433 +#ifdef USB_TRACE
8434 + VDBG(dev, "%s queue req %p, len %u buf %p\n",
8435 + _ep->name, _req, _req->length, _req->buf);
8436 +#endif
8438 + spin_lock_irqsave(&dev->lock, flags);
8440 + _req->status = -EINPROGRESS;
8441 + _req->actual = 0;
8443 + /* for ep0 IN without premature status, zlp is required and
8444 + * writing EOP starts the status stage (OUT).
8445 + */
8446 + if (unlikely(ep->num == 0 && ep->is_in))
8447 + _req->zero = 1;
8449 + /* kickstart this i/o queue? */
8450 + status = 0;
8451 + if (list_empty(&ep->queue) && likely(!ep->stopped)) {
8452 + /* dma: done after dma completion IRQ (or error)
8453 + * pio: done after last fifo operation
8454 + */
8455 + if (ep->dma)
8456 + status = start_dma(ep, req);
8457 + else
8458 + status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
8460 + if (unlikely(status != 0)) {
8461 + if (status > 0)
8462 + status = 0;
8463 + req = 0;
8466 + } /* else pio or dma irq handler advances the queue. */
8468 + if (likely(req != 0))
8469 + list_add_tail(&req->queue, &ep->queue);
8471 + if (likely(!list_empty(&ep->queue))
8472 + && likely(ep->num != 0)
8473 + && !ep->dma
8474 + && !(dev->int_enable & INT_EPxDATASET (ep->num)))
8475 + pio_irq_enable(dev, dev->regs, ep->num);
8477 + spin_unlock_irqrestore(&dev->lock, flags);
8479 + /* pci writes may still be posted */
8480 + return status;
8483 +/* dequeue ALL requests */
8484 +static void nuke(struct goku_ep *ep, int status)
8486 + struct goku_request *req;
8488 + ep->stopped = 1;
8489 + if (list_empty(&ep->queue))
8490 + return;
8491 + if (ep->dma)
8492 + abort_dma(ep, status);
8493 + while (!list_empty(&ep->queue)) {
8494 + req = list_entry(ep->queue.next, struct goku_request, queue);
8495 + done(ep, req, status);
8499 +/* dequeue JUST ONE request */
8500 +static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
8502 + struct goku_request *req;
8503 + struct goku_ep *ep;
8504 + struct goku_udc *dev;
8505 + unsigned long flags;
8507 + ep = container_of(_ep, struct goku_ep, ep);
8508 + if (!_ep || !_req || (!ep->desc && ep->num != 0))
8509 + return -EINVAL;
8510 + dev = ep->dev;
8511 + if (!dev->driver)
8512 + return -ESHUTDOWN;
8514 + /* we can't touch (dma) registers when suspended */
8515 + if (dev->ep0state == EP0_SUSPEND)
8516 + return -EBUSY;
8518 + VDBG(dev, "%s %s %s %s %p\n", __FUNCTION__, _ep->name,
8519 + ep->is_in ? "IN" : "OUT",
8520 + ep->dma ? "dma" : "pio",
8521 + _req);
8523 + spin_lock_irqsave(&dev->lock, flags);
8525 + /* make sure it's actually queued on this endpoint */
8526 + list_for_each_entry (req, &ep->queue, queue) {
8527 + if (&req->req == _req)
8528 + break;
8530 + if (&req->req != _req) {
8531 + spin_unlock_irqrestore (&dev->lock, flags);
8532 + return -EINVAL;
8535 + if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
8536 + abort_dma(ep, -ECONNRESET);
8537 + done(ep, req, -ECONNRESET);
8538 + dma_advance(dev, ep);
8539 + } else if (!list_empty(&req->queue))
8540 + done(ep, req, -ECONNRESET);
8541 + else
8542 + req = 0;
8543 + spin_unlock_irqrestore(&dev->lock, flags);
8545 + return req ? 0 : -EOPNOTSUPP;
8548 +/*-------------------------------------------------------------------------*/
8550 +static void goku_clear_halt(struct goku_ep *ep)
8552 + // assert (ep->num !=0)
8553 + VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
8554 + command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
8555 + command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
8556 + if (ep->stopped) {
8557 + ep->stopped = 0;
8558 + if (ep->dma) {
8559 + struct goku_request *req;
8561 + if (list_empty(&ep->queue))
8562 + return;
8563 + req = list_entry(ep->queue.next, struct goku_request,
8564 + queue);
8565 + (void) start_dma(ep, req);
8566 + } else
8567 + pio_advance(ep);
8571 +static int goku_set_halt(struct usb_ep *_ep, int value)
8573 + struct goku_ep *ep;
8574 + unsigned long flags;
8575 + int retval = 0;
8577 + if (!_ep)
8578 + return -ENODEV;
8579 + ep = container_of (_ep, struct goku_ep, ep);
8581 + if (ep->num == 0) {
8582 + if (value) {
8583 + ep->dev->ep0state = EP0_STALL;
8584 + ep->dev->ep[0].stopped = 1;
8585 + } else
8586 + return -EINVAL;
8588 + /* don't change EPxSTATUS_EP_INVALID to READY */
8589 + } else if (!ep->desc) {
8590 + DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
8591 + return -EINVAL;
8594 + spin_lock_irqsave(&ep->dev->lock, flags);
8595 + if (!list_empty(&ep->queue))
8596 + retval = -EAGAIN;
8597 + else if (ep->is_in && value
8598 + /* data in (either) packet buffer? */
8599 + && (ep->dev->regs->DataSet & DATASET_AB(ep->num)))
8600 + retval = -EAGAIN;
8601 + else if (!value)
8602 + goku_clear_halt(ep);
8603 + else {
8604 + ep->stopped = 1;
8605 + VDBG(ep->dev, "%s set halt\n", ep->ep.name);
8606 + command(ep->dev->regs, COMMAND_STALL, ep->num);
8607 + readl(ep->reg_status);
8609 + spin_unlock_irqrestore(&ep->dev->lock, flags);
8610 + return retval;
8613 +static int goku_fifo_status(struct usb_ep *_ep)
8615 + struct goku_ep *ep;
8616 + struct goku_udc_regs *regs;
8617 + u32 size;
8619 + if (!_ep)
8620 + return -ENODEV;
8621 + ep = container_of(_ep, struct goku_ep, ep);
8623 + /* size is only reported sanely for OUT */
8624 + if (ep->is_in)
8625 + return -EOPNOTSUPP;
8627 + /* ignores 16-byte dma buffer; SizeH == 0 */
8628 + regs = ep->dev->regs;
8629 + size = readl(&regs->EPxSizeLA[ep->num]) & DATASIZE;
8630 + size += readl(&regs->EPxSizeLB[ep->num]) & DATASIZE;
8631 + VDBG(ep->dev, "%s %s %u\n", __FUNCTION__, ep->ep.name, size);
8632 + return size;
8635 +static void goku_fifo_flush(struct usb_ep *_ep)
8637 + struct goku_ep *ep;
8638 + struct goku_udc_regs *regs;
8639 + u32 size;
8641 + if (!_ep)
8642 + return;
8643 + ep = container_of(_ep, struct goku_ep, ep);
8644 + VDBG(ep->dev, "%s %s\n", __FUNCTION__, ep->ep.name);
8646 + /* don't change EPxSTATUS_EP_INVALID to READY */
8647 + if (!ep->desc && ep->num != 0) {
8648 + DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
8649 + return;
8652 + regs = ep->dev->regs;
8653 + size = readl(&regs->EPxSizeLA[ep->num]);
8654 + size &= DATASIZE;
8656 + /* Non-desirable behavior: FIFO_CLEAR also clears the
8657 + * endpoint halt feature. For OUT, we _could_ just read
8658 + * the bytes out (PIO, if !ep->dma); for in, no choice.
8659 + */
8660 + if (size)
8661 + command(regs, COMMAND_FIFO_CLEAR, ep->num);
8664 +static struct usb_ep_ops goku_ep_ops = {
8665 + .enable = goku_ep_enable,
8666 + .disable = goku_ep_disable,
8668 + .alloc_request = goku_alloc_request,
8669 + .free_request = goku_free_request,
8671 + .alloc_buffer = goku_alloc_buffer,
8672 + .free_buffer = goku_free_buffer,
8674 + .queue = goku_queue,
8675 + .dequeue = goku_dequeue,
8677 + .set_halt = goku_set_halt,
8678 + .fifo_status = goku_fifo_status,
8679 + .fifo_flush = goku_fifo_flush,
8682 +/*-------------------------------------------------------------------------*/
8684 +static int goku_get_frame(struct usb_gadget *_gadget)
8686 + return -EOPNOTSUPP;
8689 +static const struct usb_gadget_ops goku_ops = {
8690 + .get_frame = goku_get_frame,
8691 + // no remote wakeup
8692 + // not selfpowered
8695 +/*-------------------------------------------------------------------------*/
8697 +static inline char *dmastr(void)
8699 + if (use_dma == 0)
8700 + return "(dma disabled)";
8701 + else if (use_dma == 2)
8702 + return "(dma IN and OUT)";
8703 + else
8704 + return "(dma IN)";
8707 +/* if we're trying to save space, don't bother with this proc file */
8709 +#if defined(CONFIG_PROC_FS) && !defined(CONFIG_EMBEDDED)
8710 +# define UDC_PROC_FILE
8711 +#endif
8713 +#ifdef UDC_PROC_FILE
8715 +static const char proc_node_name [] = "driver/udc";
8717 +#define FOURBITS "%s%s%s%s"
8718 +#define EIGHTBITS FOURBITS FOURBITS
8720 +static void
8721 +dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
8723 + int t;
8725 + /* int_status is the same format ... */
8726 + t = snprintf(*next, *size,
8727 + "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
8728 + label, mask,
8729 + (mask & INT_PWRDETECT) ? " power" : "",
8730 + (mask & INT_SYSERROR) ? " sys" : "",
8731 + (mask & INT_MSTRDEND) ? " in-dma" : "",
8732 + (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
8734 + (mask & INT_MSTWREND) ? " out-dma" : "",
8735 + (mask & INT_MSTWRSET) ? " wrset" : "",
8736 + (mask & INT_ERR) ? " err" : "",
8737 + (mask & INT_SOF) ? " sof" : "",
8739 + (mask & INT_EP3NAK) ? " ep3nak" : "",
8740 + (mask & INT_EP2NAK) ? " ep2nak" : "",
8741 + (mask & INT_EP1NAK) ? " ep1nak" : "",
8742 + (mask & INT_EP3DATASET) ? " ep3" : "",
8744 + (mask & INT_EP2DATASET) ? " ep2" : "",
8745 + (mask & INT_EP1DATASET) ? " ep1" : "",
8746 + (mask & INT_STATUSNAK) ? " ep0snak" : "",
8747 + (mask & INT_STATUS) ? " ep0status" : "",
8749 + (mask & INT_SETUP) ? " setup" : "",
8750 + (mask & INT_ENDPOINT0) ? " ep0" : "",
8751 + (mask & INT_USBRESET) ? " reset" : "",
8752 + (mask & INT_SUSPEND) ? " suspend" : "");
8753 + *size -= t;
8754 + *next += t;
8758 +static int
8759 +udc_proc_read(char *buffer, char **start, off_t off, int count,
8760 + int *eof, void *_dev)
8762 + char *buf = buffer;
8763 + struct goku_udc *dev = _dev;
8764 + struct goku_udc_regs *regs = dev->regs;
8765 + char *next = buf;
8766 + unsigned size = count;
8767 + unsigned long flags;
8768 + int i, t, is_usb_connected;
8769 + u32 tmp;
8771 + if (off != 0)
8772 + return 0;
8774 + local_irq_save(flags);
8776 + /* basic device status */
8777 + tmp = readl(&regs->power_detect);
8778 + is_usb_connected = tmp & PW_DETECT;
8779 + t = snprintf(next, size,
8780 + "%s - %s\n"
8781 + "%s version: %s %s\n"
8782 + "Gadget driver: %s\n"
8783 + "Host %s, %s\n"
8784 + "\n",
8785 + pci_name(dev->pdev), driver_desc,
8786 + driver_name, DRIVER_VERSION, dmastr(),
8787 + dev->driver ? dev->driver->driver.name : "(none)",
8788 + is_usb_connected
8789 + ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
8790 + : "disconnected",
8791 + ({char *tmp;
8792 + switch(dev->ep0state){
8793 + case EP0_DISCONNECT: tmp = "ep0_disconnect"; break;
8794 + case EP0_IDLE: tmp = "ep0_idle"; break;
8795 + case EP0_IN: tmp = "ep0_in"; break;
8796 + case EP0_OUT: tmp = "ep0_out"; break;
8797 + case EP0_STATUS: tmp = "ep0_status"; break;
8798 + case EP0_STALL: tmp = "ep0_stall"; break;
8799 + case EP0_SUSPEND: tmp = "ep0_suspend"; break;
8800 + default: tmp = "ep0_?"; break;
8801 + } tmp; })
8802 + );
8803 + size -= t;
8804 + next += t;
8806 + dump_intmask("int_status", readl(&regs->int_status), &next, &size);
8807 + dump_intmask("int_enable", readl(&regs->int_enable), &next, &size);
8809 + if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
8810 + goto done;
8812 + /* registers for (active) device and ep0 */
8813 + t = snprintf(next, size, "\nirqs %lu\ndataset %02x "
8814 + "single.bcs %02x.%02x state %x addr %u\n",
8815 + dev->irqs, readl(&regs->DataSet),
8816 + readl(&regs->EPxSingle), readl(&regs->EPxBCS),
8817 + readl(&regs->UsbState),
8818 + readl(&regs->address));
8819 + size -= t;
8820 + next += t;
8822 + tmp = readl(&regs->dma_master);
8823 + t = snprintf(next, size,
8824 + "dma %03X =" EIGHTBITS "%s %s\n", tmp,
8825 + (tmp & MST_EOPB_DIS) ? " eopb-" : "",
8826 + (tmp & MST_EOPB_ENA) ? " eopb+" : "",
8827 + (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
8828 + (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
8830 + (tmp & MST_RD_EOPB) ? " eopb" : "",
8831 + (tmp & MST_RD_RESET) ? " in_reset" : "",
8832 + (tmp & MST_WR_RESET) ? " out_reset" : "",
8833 + (tmp & MST_RD_ENA) ? " IN" : "",
8835 + (tmp & MST_WR_ENA) ? " OUT" : "",
8836 + (tmp & MST_CONNECTION)
8837 + ? "ep1in/ep2out"
8838 + : "ep1out/ep2in");
8839 + size -= t;
8840 + next += t;
8842 + /* dump endpoint queues */
8843 + for (i = 0; i < 4; i++) {
8844 + struct goku_ep *ep = &dev->ep [i];
8845 + struct goku_request *req;
8846 + int t;
8848 + if (i && !ep->desc)
8849 + continue;
8851 + tmp = readl(ep->reg_status);
8852 + t = snprintf(next, size,
8853 + "%s %s max %u %s, irqs %lu, "
8854 + "status %02x (%s) " FOURBITS "\n",
8855 + ep->ep.name,
8856 + ep->is_in ? "in" : "out",
8857 + ep->ep.maxpacket,
8858 + ep->dma ? "dma" : "pio",
8859 + ep->irqs,
8860 + tmp, ({ char *s;
8861 + switch (tmp & EPxSTATUS_EP_MASK) {
8862 + case EPxSTATUS_EP_READY:
8863 + s = "ready"; break;
8864 + case EPxSTATUS_EP_DATAIN:
8865 + s = "packet"; break;
8866 + case EPxSTATUS_EP_FULL:
8867 + s = "full"; break;
8868 + case EPxSTATUS_EP_TX_ERR: // host will retry
8869 + s = "tx_err"; break;
8870 + case EPxSTATUS_EP_RX_ERR:
8871 + s = "rx_err"; break;
8872 + case EPxSTATUS_EP_BUSY: /* ep0 only */
8873 + s = "busy"; break;
8874 + case EPxSTATUS_EP_STALL:
8875 + s = "stall"; break;
8876 + case EPxSTATUS_EP_INVALID: // these "can't happen"
8877 + s = "invalid"; break;
8878 + default:
8879 + s = "?"; break;
8880 + }; s; }),
8881 + (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
8882 + (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
8883 + (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
8884 + (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
8885 + );
8886 + if (t <= 0 || t > size)
8887 + goto done;
8888 + size -= t;
8889 + next += t;
8891 + if (list_empty(&ep->queue)) {
8892 + t = snprintf(next, size, "\t(nothing queued)\n");
8893 + if (t <= 0 || t > size)
8894 + goto done;
8895 + size -= t;
8896 + next += t;
8897 + continue;
8899 + list_for_each_entry(req, &ep->queue, queue) {
8900 + if (ep->dma && req->queue.prev == &ep->queue) {
8901 + if (i == UDC_MSTRD_ENDPOINT)
8902 + tmp = readl(&regs->in_dma_current);
8903 + else
8904 + tmp = readl(&regs->out_dma_current);
8905 + tmp -= req->req.dma;
8906 + tmp++;
8907 + } else
8908 + tmp = req->req.actual;
8910 + t = snprintf(next, size,
8911 + "\treq %p len %u/%u buf %p\n",
8912 + &req->req, tmp, req->req.length,
8913 + req->req.buf);
8914 + if (t <= 0 || t > size)
8915 + goto done;
8916 + size -= t;
8917 + next += t;
8921 +done:
8922 + local_irq_restore(flags);
8923 + *eof = 1;
8924 + return count - size;
8927 +#endif /* UDC_PROC_FILE */
8929 +/*-------------------------------------------------------------------------*/
8931 +static void udc_reinit (struct goku_udc *dev)
8933 + static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
8935 + unsigned i;
8937 + INIT_LIST_HEAD (&dev->gadget.ep_list);
8938 + dev->gadget.ep0 = &dev->ep [0].ep;
8939 + dev->gadget.speed = USB_SPEED_UNKNOWN;
8940 + dev->ep0state = EP0_DISCONNECT;
8941 + dev->irqs = 0;
8943 + for (i = 0; i < 4; i++) {
8944 + struct goku_ep *ep = &dev->ep[i];
8946 + ep->num = i;
8947 + ep->ep.name = names[i];
8948 + ep->reg_fifo = &dev->regs->ep_fifo [i];
8949 + ep->reg_status = &dev->regs->ep_status [i];
8950 + ep->reg_mode = &dev->regs->ep_mode[i];
8952 + ep->ep.ops = &goku_ep_ops;
8953 + list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
8954 + ep->dev = dev;
8955 + INIT_LIST_HEAD (&ep->queue);
8957 + ep_reset(0, ep);
8960 + dev->ep[0].reg_mode = 0;
8961 + dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
8962 + list_del_init (&dev->ep[0].ep.ep_list);
8965 +static void udc_reset(struct goku_udc *dev)
8967 + struct goku_udc_regs *regs = dev->regs;
8969 + writel(0, &regs->power_detect);
8970 + writel(0, &regs->int_enable);
8971 + readl(&regs->int_enable);
8972 + dev->int_enable = 0;
8974 + /* deassert reset, leave USB D+ at hi-Z (no pullup)
8975 + * don't let INT_PWRDETECT sequence begin
8976 + */
8977 + udelay(250);
8978 + writel(PW_RESETB, &regs->power_detect);
8979 + readl(&regs->int_enable);
8982 +static void ep0_start(struct goku_udc *dev)
8984 + struct goku_udc_regs *regs = dev->regs;
8985 + unsigned i;
8987 + VDBG(dev, "%s\n", __FUNCTION__);
8989 + udc_reset(dev);
8990 + udc_reinit (dev);
8991 + //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
8993 + /* hw handles set_address, set_feature, get_status; maybe more */
8994 + writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
8995 + | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
8996 + | G_REQMODE_GET_DESC
8997 + | G_REQMODE_CLEAR_FEAT
8998 + , &regs->reqmode);
9000 + for (i = 0; i < 4; i++)
9001 + dev->ep[i].irqs = 0;
9003 + /* can't modify descriptors after writing UsbReady */
9004 + for (i = 0; i < DESC_LEN; i++)
9005 + writel(0, &regs->descriptors[i]);
9006 + writel(0, &regs->UsbReady);
9008 + /* expect ep0 requests when the host drops reset */
9009 + writel(PW_RESETB | PW_PULLUP, &regs->power_detect);
9010 + dev->int_enable = INT_DEVWIDE | INT_EP0;
9011 + writel(dev->int_enable, &dev->regs->int_enable);
9012 + readl(&regs->int_enable);
9013 + dev->gadget.speed = USB_SPEED_FULL;
9014 + dev->ep0state = EP0_IDLE;
9017 +static void udc_enable(struct goku_udc *dev)
9019 + /* start enumeration now, or after power detect irq */
9020 + if (readl(&dev->regs->power_detect) & PW_DETECT)
9021 + ep0_start(dev);
9022 + else {
9023 + DBG(dev, "%s\n", __FUNCTION__);
9024 + dev->int_enable = INT_PWRDETECT;
9025 + writel(dev->int_enable, &dev->regs->int_enable);
9029 +/*-------------------------------------------------------------------------*/
9031 +/* keeping it simple:
9032 + * - one bus driver, initted first;
9033 + * - one function driver, initted second
9034 + */
9036 +static struct goku_udc *the_controller;
9038 +/* when a driver is successfully registered, it will receive
9039 + * control requests including set_configuration(), which enables
9040 + * non-control requests. then usb traffic follows until a
9041 + * disconnect is reported. then a host may connect again, or
9042 + * the driver might get unbound.
9043 + */
9044 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
9046 + struct goku_udc *dev = the_controller;
9047 + int retval;
9049 + if (!driver
9050 + || driver->speed != USB_SPEED_FULL
9051 + || !driver->bind
9052 + || !driver->unbind
9053 + || !driver->disconnect
9054 + || !driver->setup)
9055 + return -EINVAL;
9056 + if (!dev)
9057 + return -ENODEV;
9058 + if (dev->driver)
9059 + return -EBUSY;
9061 + /* hook up the driver */
9062 + dev->driver = driver;
9063 + retval = driver->bind(&dev->gadget);
9064 + if (retval) {
9065 + DBG(dev, "bind to driver %s --> error %d\n",
9066 + driver->driver.name, retval);
9067 + dev->driver = 0;
9068 + return retval;
9071 + /* then enable host detection and ep0; and we're ready
9072 + * for set_configuration as well as eventual disconnect.
9073 + */
9074 + udc_enable(dev);
9076 + DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
9077 + return 0;
9079 +EXPORT_SYMBOL(usb_gadget_register_driver);
9081 +static void
9082 +stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
9084 + unsigned i;
9086 + DBG (dev, "%s\n", __FUNCTION__);
9088 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
9089 + driver = 0;
9091 + /* disconnect gadget driver after quiesceing hw and the driver */
9092 + udc_reset (dev);
9093 + for (i = 0; i < 4; i++)
9094 + nuke(&dev->ep [i], -ESHUTDOWN);
9095 + if (driver) {
9096 + spin_unlock(&dev->lock);
9097 + driver->disconnect(&dev->gadget);
9098 + spin_lock(&dev->lock);
9101 + if (dev->driver)
9102 + udc_enable(dev);
9105 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
9107 + struct goku_udc *dev = the_controller;
9108 + unsigned long flags;
9110 + if (!dev)
9111 + return -ENODEV;
9112 + if (!driver || driver != dev->driver)
9113 + return -EINVAL;
9115 + spin_lock_irqsave(&dev->lock, flags);
9116 + dev->driver = 0;
9117 + stop_activity(dev, driver);
9118 + spin_unlock_irqrestore(&dev->lock, flags);
9120 + driver->unbind(&dev->gadget);
9122 + DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
9123 + return 0;
9125 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
9128 +/*-------------------------------------------------------------------------*/
9130 +static void ep0_setup(struct goku_udc *dev)
9132 + struct goku_udc_regs *regs = dev->regs;
9133 + struct usb_ctrlrequest ctrl;
9134 + int tmp;
9136 + /* read SETUP packet and enter DATA stage */
9137 + ctrl.bRequestType = readl(&regs->bRequestType);
9138 + ctrl.bRequest = readl(&regs->bRequest);
9139 + ctrl.wValue = (readl(&regs->wValueH) << 8) | readl(&regs->wValueL);
9140 + ctrl.wIndex = (readl(&regs->wIndexH) << 8) | readl(&regs->wIndexL);
9141 + ctrl.wLength = (readl(&regs->wLengthH) << 8) | readl(&regs->wLengthL);
9142 + writel(0, &regs->SetupRecv);
9144 + nuke(&dev->ep[0], 0);
9145 + dev->ep[0].stopped = 0;
9146 + if (likely(ctrl.bRequestType & USB_DIR_IN)) {
9147 + dev->ep[0].is_in = 1;
9148 + dev->ep0state = EP0_IN;
9149 + /* detect early status stages */
9150 + writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
9151 + } else {
9152 + dev->ep[0].is_in = 0;
9153 + dev->ep0state = EP0_OUT;
9155 + /* NOTE: CLEAR_FEATURE is done in software so that we can
9156 + * synchronize transfer restarts after bulk IN stalls. data
9157 + * won't even enter the fifo until the halt is cleared.
9158 + */
9159 + switch (ctrl.bRequest) {
9160 + case USB_REQ_CLEAR_FEATURE:
9161 + switch (ctrl.bRequestType) {
9162 + case USB_RECIP_ENDPOINT:
9163 + tmp = ctrl.wIndex & 0x0f;
9164 + /* active endpoint */
9165 + if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
9166 + goto stall;
9167 + if (ctrl.wIndex & USB_DIR_IN) {
9168 + if (!dev->ep[tmp].is_in)
9169 + goto stall;
9170 + } else {
9171 + if (dev->ep[tmp].is_in)
9172 + goto stall;
9174 + /* endpoint halt */
9175 + if (ctrl.wValue != 0)
9176 + goto stall;
9177 + if (tmp)
9178 + goku_clear_halt(&dev->ep[tmp]);
9179 +succeed:
9180 + /* start ep0out status stage */
9181 + writel(~(1<<0), &regs->EOP);
9182 + dev->ep[0].stopped = 1;
9183 + dev->ep0state = EP0_STATUS;
9184 + return;
9185 + case USB_RECIP_DEVICE:
9186 + /* device remote wakeup: always clear */
9187 + if (ctrl.wValue != 1)
9188 + goto stall;
9189 + VDBG(dev, "clear dev remote wakeup\n");
9190 + goto succeed;
9191 + case USB_RECIP_INTERFACE:
9192 + goto stall;
9193 + default: /* pass to gadget driver */
9194 + break;
9196 + break;
9197 + default:
9198 + break;
9202 +#ifdef USB_TRACE
9203 + VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
9204 + ctrl.bRequestType, ctrl.bRequest,
9205 + ctrl.wValue, ctrl.wIndex, ctrl.wLength);
9206 +#endif
9208 + /* hw wants to know when we're configured (or not) */
9209 + dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
9210 + && ctrl.bRequestType == USB_RECIP_DEVICE);
9211 + if (unlikely(dev->req_config))
9212 + dev->configured = (ctrl.wValue != 0);
9214 + /* delegate everything to the gadget driver.
9215 + * it may respond after this irq handler returns.
9216 + */
9217 + spin_unlock (&dev->lock);
9218 + tmp = dev->driver->setup(&dev->gadget, &ctrl);
9219 + spin_lock (&dev->lock);
9220 + if (unlikely(tmp < 0)) {
9221 +stall:
9222 +#ifdef USB_TRACE
9223 + VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
9224 + ctrl.bRequestType, ctrl.bRequest, tmp);
9225 +#endif
9226 + command(regs, COMMAND_STALL, 0);
9227 + dev->ep[0].stopped = 1;
9228 + dev->ep0state = EP0_STALL;
9231 + /* expect at least one data or status stage irq */
9234 +#define ACK(irqbit) { \
9235 + stat &= ~irqbit; \
9236 + writel(~irqbit, &regs->int_status); \
9237 + handled = 1; \
9240 +static irqreturn_t goku_irq(int irq, void *_dev, struct pt_regs *r)
9242 + struct goku_udc *dev = _dev;
9243 + struct goku_udc_regs *regs = dev->regs;
9244 + struct goku_ep *ep;
9245 + u32 stat, handled = 0;
9246 + unsigned i, rescans = 5;
9248 + spin_lock(&dev->lock);
9250 +rescan:
9251 + stat = readl(&regs->int_status) & dev->int_enable;
9252 + if (!stat)
9253 + goto done;
9254 + dev->irqs++;
9256 + /* device-wide irqs */
9257 + if (unlikely(stat & INT_DEVWIDE)) {
9258 + if (stat & INT_SYSERROR) {
9259 + ERROR(dev, "system error\n");
9260 + stop_activity(dev, dev->driver);
9261 + stat = 0;
9262 + handled = 1;
9263 + // FIXME have a neater way to prevent re-enumeration
9264 + dev->driver = 0;
9265 + goto done;
9267 + if (stat & INT_PWRDETECT) {
9268 + writel(~stat, &regs->int_status);
9269 + if (readl(&dev->regs->power_detect) & PW_DETECT) {
9270 + VDBG(dev, "connect\n");
9271 + ep0_start(dev);
9272 + } else {
9273 + DBG(dev, "disconnect\n");
9274 + if (dev->gadget.speed == USB_SPEED_FULL)
9275 + stop_activity(dev, dev->driver);
9276 + dev->ep0state = EP0_DISCONNECT;
9277 + dev->int_enable = INT_DEVWIDE;
9278 + writel(dev->int_enable, &dev->regs->int_enable);
9280 + stat = 0;
9281 + handled = 1;
9282 + goto done;
9284 + if (stat & INT_SUSPEND) {
9285 + ACK(INT_SUSPEND);
9286 + if (readl(&regs->ep_status[0]) & EPxSTATUS_SUSPEND) {
9287 + switch (dev->ep0state) {
9288 + case EP0_DISCONNECT:
9289 + case EP0_SUSPEND:
9290 + goto pm_next;
9291 + default:
9292 + break;
9294 + DBG(dev, "USB suspend\n");
9295 + dev->ep0state = EP0_SUSPEND;
9296 + if (dev->gadget.speed != USB_SPEED_UNKNOWN
9297 + && dev->driver
9298 + && dev->driver->suspend) {
9299 + spin_unlock(&dev->lock);
9300 + dev->driver->suspend(&dev->gadget);
9301 + spin_lock(&dev->lock);
9303 + } else {
9304 + if (dev->ep0state != EP0_SUSPEND) {
9305 + DBG(dev, "bogus USB resume %d\n",
9306 + dev->ep0state);
9307 + goto pm_next;
9309 + DBG(dev, "USB resume\n");
9310 + dev->ep0state = EP0_IDLE;
9311 + if (dev->gadget.speed != USB_SPEED_UNKNOWN
9312 + && dev->driver
9313 + && dev->driver->resume) {
9314 + spin_unlock(&dev->lock);
9315 + dev->driver->resume(&dev->gadget);
9316 + spin_lock(&dev->lock);
9320 +pm_next:
9321 + if (stat & INT_USBRESET) { /* hub reset done */
9322 + ACK(INT_USBRESET);
9323 + INFO(dev, "USB reset done, gadget %s\n",
9324 + dev->driver->driver.name);
9326 + // and INT_ERR on some endpoint's crc/bitstuff/... problem
9329 + /* progress ep0 setup, data, or status stages.
9330 + * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
9331 + */
9332 + if (stat & INT_SETUP) {
9333 + ACK(INT_SETUP);
9334 + dev->ep[0].irqs++;
9335 + ep0_setup(dev);
9337 + if (stat & INT_STATUSNAK) {
9338 + ACK(INT_STATUSNAK|INT_ENDPOINT0);
9339 + if (dev->ep0state == EP0_IN) {
9340 + ep = &dev->ep[0];
9341 + ep->irqs++;
9342 + nuke(ep, 0);
9343 + writel(~(1<<0), &regs->EOP);
9344 + dev->ep0state = EP0_STATUS;
9347 + if (stat & INT_ENDPOINT0) {
9348 + ACK(INT_ENDPOINT0);
9349 + ep = &dev->ep[0];
9350 + ep->irqs++;
9351 + pio_advance(ep);
9354 + /* dma completion */
9355 + if (stat & INT_MSTRDEND) { /* IN */
9356 + ACK(INT_MSTRDEND);
9357 + ep = &dev->ep[UDC_MSTRD_ENDPOINT];
9358 + ep->irqs++;
9359 + dma_advance(dev, ep);
9361 + if (stat & INT_MSTWREND) { /* OUT */
9362 + ACK(INT_MSTWREND);
9363 + ep = &dev->ep[UDC_MSTWR_ENDPOINT];
9364 + ep->irqs++;
9365 + dma_advance(dev, ep);
9367 + if (stat & INT_MSTWRTMOUT) { /* OUT */
9368 + ACK(INT_MSTWRTMOUT);
9369 + ep = &dev->ep[UDC_MSTWR_ENDPOINT];
9370 + ep->irqs++;
9371 + ERROR(dev, "%s write timeout ?\n", ep->ep.name);
9372 + // reset dma? then dma_advance()
9375 + /* pio */
9376 + for (i = 1; i < 4; i++) {
9377 + u32 tmp = INT_EPxDATASET(i);
9379 + if (!(stat & tmp))
9380 + continue;
9381 + ep = &dev->ep[i];
9382 + pio_advance(ep);
9383 + if (list_empty (&ep->queue))
9384 + pio_irq_disable(dev, regs, i);
9385 + stat &= ~tmp;
9386 + handled = 1;
9387 + ep->irqs++;
9390 + if (rescans--)
9391 + goto rescan;
9393 +done:
9394 + (void)readl(&regs->int_enable);
9395 + spin_unlock(&dev->lock);
9396 + if (stat)
9397 + DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
9398 + readl(&regs->int_status), dev->int_enable);
9399 + return IRQ_RETVAL(handled);
9402 +#undef ACK
9404 +/*-------------------------------------------------------------------------*/
9406 +/* tear down the binding between this driver and the pci device */
9408 +static void goku_remove(struct pci_dev *pdev)
9410 + struct goku_udc *dev = pci_get_drvdata(pdev);
9412 + DBG(dev, "%s\n", __FUNCTION__);
9413 + /* start with the driver above us */
9414 + if (dev->driver) {
9415 + /* should have been done already by driver model core */
9416 + WARN(dev, "pci remove, driver '%s' is still registered\n",
9417 + dev->driver->driver.name);
9418 + usb_gadget_unregister_driver(dev->driver);
9421 +#ifdef UDC_PROC_FILE
9422 + remove_proc_entry(proc_node_name, NULL);
9423 +#endif
9424 + if (dev->regs)
9425 + udc_reset(dev);
9426 + if (dev->got_irq)
9427 + free_irq(pdev->irq, dev);
9428 + if (dev->regs)
9429 + iounmap(dev->regs);
9430 + if (dev->got_region)
9431 + release_mem_region(pci_resource_start (pdev, 0),
9432 + pci_resource_len (pdev, 0));
9433 + if (dev->enabled)
9434 + pci_disable_device(pdev);
9436 + pci_set_drvdata(pdev, 0);
9437 + dev->regs = 0;
9438 + the_controller = 0;
9440 + INFO(dev, "unbind\n");
9443 +/* wrap this driver around the specified pci device, but
9444 + * don't respond over USB until a gadget driver binds to us.
9445 + */
9447 +static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
9449 + struct goku_udc *dev = 0;
9450 + unsigned long resource, len;
9451 + void *base = 0;
9452 + int retval;
9453 + char buf [8], *bufp;
9455 + /* if you want to support more than one controller in a system,
9456 + * usb_gadget_driver_{register,unregister}() must change.
9457 + */
9458 + if (the_controller) {
9459 + WARN(dev, "ignoring %s\n", pci_name(pdev));
9460 + return -EBUSY;
9462 + if (!pdev->irq) {
9463 + printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
9464 + retval = -ENODEV;
9465 + goto done;
9468 + /* alloc, and start init */
9469 + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
9470 + if (dev == NULL){
9471 + pr_debug("enomem %s\n", pci_name(pdev));
9472 + retval = -ENOMEM;
9473 + goto done;
9476 + memset(dev, 0, sizeof *dev);
9477 + spin_lock_init(&dev->lock);
9478 + dev->pdev = pdev;
9479 + dev->gadget.ops = &goku_ops;
9481 + /* the "gadget" abstracts/virtualizes the controller */
9482 + dev->gadget.dev.bus_id = "gadget";
9483 + dev->gadget.name = driver_name;
9485 + /* now all the pci goodies ... */
9486 + retval = pci_enable_device(pdev);
9487 + if (retval < 0) {
9488 + DBG(dev, "can't enable, %d\n", retval);
9489 + goto done;
9491 + dev->enabled = 1;
9493 + resource = pci_resource_start(pdev, 0);
9494 + len = pci_resource_len(pdev, 0);
9495 + if (!request_mem_region(resource, len, driver_name)) {
9496 + DBG(dev, "controller already in use\n");
9497 + retval = -EBUSY;
9498 + goto done;
9500 + dev->got_region = 1;
9502 + base = ioremap_nocache(resource, len);
9503 + if (base == NULL) {
9504 + DBG(dev, "can't map memory\n");
9505 + retval = -EFAULT;
9506 + goto done;
9508 + dev->regs = (struct goku_udc_regs *) base;
9510 + pci_set_drvdata(pdev, dev);
9511 + INFO(dev, "%s\n", driver_desc);
9512 + INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
9513 +#ifndef __sparc__
9514 + snprintf(buf, sizeof buf, "%d", pdev->irq);
9515 + bufp = buf;
9516 +#else
9517 + bufp = __irq_itoa(pdev->irq);
9518 +#endif
9519 + INFO(dev, "irq %s, pci mem %p\n", bufp, base);
9521 + /* init to known state, then setup irqs */
9522 + udc_reset(dev);
9523 + udc_reinit (dev);
9524 + if (request_irq(pdev->irq, goku_irq, SA_SHIRQ/*|SA_SAMPLE_RANDOM*/,
9525 + driver_name, dev) != 0) {
9526 + DBG(dev, "request interrupt %s failed\n", bufp);
9527 + retval = -EBUSY;
9528 + goto done;
9530 + dev->got_irq = 1;
9531 + if (use_dma)
9532 + pci_set_master(pdev);
9535 +#ifdef UDC_PROC_FILE
9536 + create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
9537 +#endif
9539 + /* done */
9540 + the_controller = dev;
9542 + return 0;
9544 +done:
9545 + if (dev)
9546 + goku_remove (pdev);
9547 + return retval;
9551 +/*-------------------------------------------------------------------------*/
9553 +static struct pci_device_id pci_ids [] = { {
9554 + .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
9555 + .class_mask = ~0,
9556 + .vendor = 0x102f, /* Toshiba */
9557 + .device = 0x0107, /* this UDC */
9558 + .subvendor = PCI_ANY_ID,
9559 + .subdevice = PCI_ANY_ID,
9561 +}, { /* end: all zeroes */ }
9563 +MODULE_DEVICE_TABLE (pci, pci_ids);
9565 +static struct pci_driver goku_pci_driver = {
9566 + .name = (char *) driver_name,
9567 + .id_table = pci_ids,
9569 + .probe = goku_probe,
9570 + .remove = goku_remove,
9572 + /* FIXME add power management support */
9575 +static int __init init (void)
9577 + return pci_module_init (&goku_pci_driver);
9579 +module_init (init);
9581 +static void __exit cleanup (void)
9583 + pci_unregister_driver (&goku_pci_driver);
9585 +module_exit (cleanup);
9586 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/goku_udc.h kernel/drivers/usb/gadget/goku_udc.h
9587 --- /tmp/kernel/drivers/usb/gadget/goku_udc.h 1970-01-01 01:00:00.000000000 +0100
9588 +++ kernel/drivers/usb/gadget/goku_udc.h 2005-04-22 17:53:19.443538050 +0200
9589 @@ -0,0 +1,321 @@
9591 + * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
9593 + * Copyright (C) 2000-2002 Lineo
9594 + * by Stuart Lynne, Tom Rushworth, and Bruce Balden
9595 + * Copyright (C) 2002 Toshiba Corporation
9596 + * Copyright (C) 2003 MontaVista Software (source@mvista.com)
9598 + * This file is licensed under the terms of the GNU General Public
9599 + * License version 2. This program is licensed "as is" without any
9600 + * warranty of any kind, whether express or implied.
9601 + */
9604 + * PCI BAR 0 points to these registers.
9605 + */
9606 +struct goku_udc_regs {
9607 + /* irq management */
9608 + u32 int_status; /* 0x000 */
9609 + u32 int_enable;
9610 +#define INT_SUSPEND 0x00001 /* or resume */
9611 +#define INT_USBRESET 0x00002
9612 +#define INT_ENDPOINT0 0x00004
9613 +#define INT_SETUP 0x00008
9614 +#define INT_STATUS 0x00010
9615 +#define INT_STATUSNAK 0x00020
9616 +#define INT_EPxDATASET(n) (0x00020 << (n)) /* 0 < n < 4 */
9617 +# define INT_EP1DATASET 0x00040
9618 +# define INT_EP2DATASET 0x00080
9619 +# define INT_EP3DATASET 0x00100
9620 +#define INT_EPnNAK(n) (0x00100 < (n)) /* 0 < n < 4 */
9621 +# define INT_EP1NAK 0x00200
9622 +# define INT_EP2NAK 0x00400
9623 +# define INT_EP3NAK 0x00800
9624 +#define INT_SOF 0x01000
9625 +#define INT_ERR 0x02000
9626 +#define INT_MSTWRSET 0x04000
9627 +#define INT_MSTWREND 0x08000
9628 +#define INT_MSTWRTMOUT 0x10000
9629 +#define INT_MSTRDEND 0x20000
9630 +#define INT_SYSERROR 0x40000
9631 +#define INT_PWRDETECT 0x80000
9633 +#define INT_DEVWIDE (INT_PWRDETECT|INT_SYSERROR/*|INT_ERR*/|INT_USBRESET|INT_SUSPEND)
9634 +#define INT_EP0 (INT_SETUP|INT_ENDPOINT0/*|INT_STATUS*/|INT_STATUSNAK)
9636 + u32 dma_master;
9637 +#define MST_EOPB_DIS 0x0800
9638 +#define MST_EOPB_ENA 0x0400
9639 +#define MST_TIMEOUT_DIS 0x0200
9640 +#define MST_TIMEOUT_ENA 0x0100
9641 +#define MST_RD_EOPB 0x0080 /* write-only */
9642 +#define MST_RD_RESET 0x0040
9643 +#define MST_WR_RESET 0x0020
9644 +#define MST_RD_ENA 0x0004 /* 1:start, 0:ignore */
9645 +#define MST_WR_ENA 0x0002 /* 1:start, 0:ignore */
9646 +#define MST_CONNECTION 0x0001 /* 0 for ep1out/ep2in */
9648 +#define MST_R_BITS (MST_EOPB_DIS|MST_EOPB_ENA \
9649 + |MST_RD_ENA|MST_RD_RESET)
9650 +#define MST_W_BITS (MST_TIMEOUT_DIS|MST_TIMEOUT_ENA \
9651 + |MST_WR_ENA|MST_WR_RESET)
9652 +#define MST_RW_BITS (MST_R_BITS|MST_W_BITS \
9653 + |MST_CONNECTION)
9655 +/* these values assume (dma_master & MST_CONNECTION) == 0 */
9656 +#define UDC_MSTWR_ENDPOINT 1
9657 +#define UDC_MSTRD_ENDPOINT 2
9659 + /* dma master write */
9660 + u32 out_dma_start;
9661 + u32 out_dma_end;
9662 + u32 out_dma_current;
9664 + /* dma master read */
9665 + u32 in_dma_start;
9666 + u32 in_dma_end;
9667 + u32 in_dma_current;
9669 + u32 power_detect;
9670 +#define PW_DETECT 0x04
9671 +#define PW_RESETB 0x02
9672 +#define PW_PULLUP 0x01
9674 + u8 _reserved0 [0x1d8];
9676 + /* endpoint registers */
9677 + u32 ep_fifo [4]; /* 0x200 */
9678 + u8 _reserved1 [0x10];
9679 + u32 ep_mode [4]; /* only 1-3 valid */
9680 + u8 _reserved2 [0x10];
9682 + u32 ep_status [4];
9683 +#define EPxSTATUS_TOGGLE 0x40
9684 +#define EPxSTATUS_SUSPEND 0x20
9685 +#define EPxSTATUS_EP_MASK (0x07<<2)
9686 +# define EPxSTATUS_EP_READY (0<<2)
9687 +# define EPxSTATUS_EP_DATAIN (1<<2)
9688 +# define EPxSTATUS_EP_FULL (2<<2)
9689 +# define EPxSTATUS_EP_TX_ERR (3<<2)
9690 +# define EPxSTATUS_EP_RX_ERR (4<<2)
9691 +# define EPxSTATUS_EP_BUSY (5<<2)
9692 +# define EPxSTATUS_EP_STALL (6<<2)
9693 +# define EPxSTATUS_EP_INVALID (7<<2)
9694 +#define EPxSTATUS_FIFO_DISABLE 0x02
9695 +#define EPxSTATUS_STAGE_ERROR 0x01
9697 + u8 _reserved3 [0x10];
9698 + u32 EPxSizeLA[4];
9699 +#define PACKET_ACTIVE (1<<7)
9700 +#define DATASIZE 0x7f
9701 + u8 _reserved3a [0x10];
9702 + u32 EPxSizeLB[4]; /* only 1,2 valid */
9703 + u8 _reserved3b [0x10];
9704 + u32 EPxSizeHA[4]; /* only 1-3 valid */
9705 + u8 _reserved3c [0x10];
9706 + u32 EPxSizeHB[4]; /* only 1,2 valid */
9707 + u8 _reserved4[0x30];
9709 + /* SETUP packet contents */
9710 + u32 bRequestType; /* 0x300 */
9711 + u32 bRequest;
9712 + u32 wValueL;
9713 + u32 wValueH;
9714 + u32 wIndexL;
9715 + u32 wIndexH;
9716 + u32 wLengthL;
9717 + u32 wLengthH;
9719 + /* command interaction/handshaking */
9720 + u32 SetupRecv; /* 0x320 */
9721 + u32 CurrConfig;
9722 + u32 StdRequest;
9723 + u32 Request;
9724 + u32 DataSet;
9725 +#define DATASET_A(epnum) (1<<(2*(epnum)))
9726 +#define DATASET_B(epnum) (2<<(2*(epnum)))
9727 +#define DATASET_AB(epnum) (3<<(2*(epnum)))
9728 + u8 _reserved5[4];
9730 + u32 UsbState;
9731 +#define USBSTATE_CONFIGURED 0x04
9732 +#define USBSTATE_ADDRESSED 0x02
9733 +#define USBSTATE_DEFAULT 0x01
9735 + u32 EOP;
9737 + u32 Command; /* 0x340 */
9738 +#define COMMAND_SETDATA0 2
9739 +#define COMMAND_RESET 3
9740 +#define COMMAND_STALL 4
9741 +#define COMMAND_INVALID 5
9742 +#define COMMAND_FIFO_DISABLE 7
9743 +#define COMMAND_FIFO_ENABLE 8
9744 +#define COMMAND_INIT_DESCRIPTOR 9
9745 +#define COMMAND_FIFO_CLEAR 10 /* also stall */
9746 +#define COMMAND_STALL_CLEAR 11
9747 +#define COMMAND_EP(n) ((n) << 4)
9749 + u32 EPxSingle;
9750 + u8 _reserved6[4];
9751 + u32 EPxBCS;
9752 + u8 _reserved7[8];
9753 + u32 IntControl;
9754 +#define ICONTROL_STATUSNAK 1
9755 + u8 _reserved8[4];
9757 + u32 reqmode; // 0x360 standard request mode, low 8 bits
9758 +#define G_REQMODE_SET_INTF (1<<7)
9759 +#define G_REQMODE_GET_INTF (1<<6)
9760 +#define G_REQMODE_SET_CONF (1<<5)
9761 +#define G_REQMODE_GET_CONF (1<<4)
9762 +#define G_REQMODE_GET_DESC (1<<3)
9763 +#define G_REQMODE_SET_FEAT (1<<2)
9764 +#define G_REQMODE_CLEAR_FEAT (1<<1)
9765 +#define G_REQMODE_GET_STATUS (1<<0)
9767 + u32 ReqMode;
9768 + u8 _reserved9[0x18];
9769 + u32 PortStatus; /* 0x380 */
9770 + u8 _reserved10[8];
9771 + u32 address;
9772 + u32 buff_test;
9773 + u8 _reserved11[4];
9774 + u32 UsbReady;
9775 + u8 _reserved12[4];
9776 + u32 SetDescStall; /* 0x3a0 */
9777 + u8 _reserved13[0x45c];
9779 + /* hardware could handle limited GET_DESCRIPTOR duties */
9780 +#define DESC_LEN 0x80
9781 + u32 descriptors[DESC_LEN]; /* 0x800 */
9782 + u8 _reserved14[0x600];
9784 +} __attribute__ ((packed));
9786 +#define MAX_FIFO_SIZE 64
9787 +#define MAX_EP0_SIZE 8 /* ep0 fifo is bigger, though */
9790 +/*-------------------------------------------------------------------------*/
9792 +/* DRIVER DATA STRUCTURES and UTILITIES */
9794 +struct goku_ep {
9795 + struct usb_ep ep;
9796 + struct goku_udc *dev;
9797 + unsigned long irqs;
9799 + unsigned num:8,
9800 + dma:1,
9801 + is_in:1,
9802 + stopped:1;
9804 + /* analogous to a host-side qh */
9805 + struct list_head queue;
9806 + const struct usb_endpoint_descriptor *desc;
9808 + u32 *reg_fifo;
9809 + u32 *reg_mode;
9810 + u32 *reg_status;
9813 +struct goku_request {
9814 + struct usb_request req;
9815 + struct list_head queue;
9817 + unsigned mapped:1;
9820 +enum ep0state {
9821 + EP0_DISCONNECT, /* no host */
9822 + EP0_IDLE, /* between STATUS ack and SETUP report */
9823 + EP0_IN, EP0_OUT, /* data stage */
9824 + EP0_STATUS, /* status stage */
9825 + EP0_STALL, /* data or status stages */
9826 + EP0_SUSPEND, /* usb suspend */
9829 +struct goku_udc {
9830 + /* each pci device provides one gadget, several endpoints */
9831 + struct usb_gadget gadget;
9832 + spinlock_t lock;
9833 + struct goku_ep ep[4];
9834 + struct usb_gadget_driver *driver;
9836 + enum ep0state ep0state;
9837 + unsigned got_irq:1,
9838 + got_region:1,
9839 + req_config:1,
9840 + configured:1,
9841 + enabled:1;
9843 + /* pci state used to access those endpoints */
9844 + struct pci_dev *pdev;
9845 + struct goku_udc_regs *regs;
9846 + u32 int_enable;
9848 + /* statistics... */
9849 + unsigned long irqs;
9852 +/*-------------------------------------------------------------------------*/
9854 +#define xprintk(dev,level,fmt,args...) \
9855 + printk(level "%s %s: " fmt , driver_name , \
9856 + pci_name(dev->pdev) , ## args)
9858 +#ifdef DEBUG
9859 +#define DBG(dev,fmt,args...) \
9860 + xprintk(dev , KERN_DEBUG , fmt , ## args)
9861 +#else
9862 +#define DBG(dev,fmt,args...) \
9863 + do { } while (0)
9864 +#endif /* DEBUG */
9866 +#ifdef VERBOSE
9867 +#define VDBG DBG
9868 +#else
9869 +#define VDBG(dev,fmt,args...) \
9870 + do { } while (0)
9871 +#endif /* VERBOSE */
9873 +#define ERROR(dev,fmt,args...) \
9874 + xprintk(dev , KERN_ERR , fmt , ## args)
9875 +#define WARN(dev,fmt,args...) \
9876 + xprintk(dev , KERN_WARNING , fmt , ## args)
9877 +#define INFO(dev,fmt,args...) \
9878 + xprintk(dev , KERN_INFO , fmt , ## args)
9880 +/*-------------------------------------------------------------------------*/
9882 +/* 2.5 stuff that's sometimes missing in 2.4 */
9884 +#ifndef container_of
9885 +#define container_of list_entry
9886 +#endif
9888 +#ifndef likely
9889 +#define likely(x) (x)
9890 +#define unlikely(x) (x)
9891 +#endif
9893 +#ifndef BUG_ON
9894 +#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
9895 +#endif
9897 +#ifndef WARN_ON
9898 +#define WARN_ON(x) do { } while (0)
9899 +#endif
9901 +#ifndef IRQ_NONE
9902 +typedef void irqreturn_t;
9903 +#define IRQ_NONE
9904 +#define IRQ_HANDLED
9905 +#define IRQ_RETVAL(x)
9906 +#endif
9908 +#ifndef pci_name
9909 +#define pci_name(pdev) ((pdev)->slot_name)
9910 +#endif
9911 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/gserial.c kernel/drivers/usb/gadget/gserial.c
9912 --- /tmp/kernel/drivers/usb/gadget/gserial.c 1970-01-01 01:00:00.000000000 +0100
9913 +++ kernel/drivers/usb/gadget/gserial.c 2005-04-22 17:53:19.450536911 +0200
9914 @@ -0,0 +1,2301 @@
9916 + * g_serial.c -- USB gadget serial driver
9918 + * $Id: gserial.c,v 1.17 2003/10/01 06:31:57 borchers Exp $
9920 + * Copyright 2003 (c) Al Borchers (alborchers@steinerpoint.com)
9922 + * This code is based in part on the Gadget Zero driver, which
9923 + * is Copyright (C) 2003 by David Brownell, all rights reserved.
9925 + * This code also borrows from usbserial.c, which is
9926 + * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
9927 + * Copyright (c) 2000 Peter Berger (pberger@brimson.com)
9928 + * Copyright (c) 2000 Al Borchers (alborchers@steinerpoint.com)
9930 + * This software is distributed under the terms of the GNU General
9931 + * Public License ("GPL") as published by the Free Software Foundation,
9932 + * either version 2 of that License or (at your option) any later version.
9934 + */
9936 +#ifndef __KERNEL__
9937 +#define __KERNEL__
9938 +#endif
9940 +#ifndef MODULE
9941 +#define MODULE
9942 +#endif
9945 +/* Includes */
9947 +#include <linux/config.h>
9948 +#include <linux/module.h>
9949 +#include <linux/kernel.h>
9950 +#include <linux/delay.h>
9951 +#include <linux/ioport.h>
9952 +#include <linux/sched.h>
9953 +#include <linux/slab.h>
9954 +#include <linux/smp_lock.h>
9955 +#include <linux/errno.h>
9956 +#include <linux/init.h>
9957 +#include <linux/timer.h>
9958 +#include <linux/list.h>
9959 +#include <linux/interrupt.h>
9960 +#include <linux/uts.h>
9961 +#include <linux/version.h>
9962 +#include <linux/wait.h>
9963 +#include <linux/list.h>
9964 +#include <linux/proc_fs.h>
9965 +#include <linux/tty_flip.h>
9967 +#include <asm/byteorder.h>
9968 +#include <asm/io.h>
9969 +#include <asm/irq.h>
9970 +#include <asm/system.h>
9971 +#include <asm/unaligned.h>
9972 +#include <asm/uaccess.h>
9974 +#include <linux/usb_ch9.h>
9975 +#include <linux/usb_gadget.h>
9977 +#include "gadget_chips.h"
9980 +/* Wait Cond */
9982 +#define __wait_cond_interruptible(wq, condition, lock, flags, ret) \
9983 +do { \
9984 + wait_queue_t __wait; \
9985 + init_waitqueue_entry(&__wait, current); \
9987 + add_wait_queue(&wq, &__wait); \
9988 + for (;;) { \
9989 + set_current_state(TASK_INTERRUPTIBLE); \
9990 + if (condition) \
9991 + break; \
9992 + if (!signal_pending(current)) { \
9993 + spin_unlock_irqrestore(lock, flags); \
9994 + schedule(); \
9995 + spin_lock_irqsave(lock, flags); \
9996 + continue; \
9997 + } \
9998 + ret = -ERESTARTSYS; \
9999 + break; \
10000 + } \
10001 + current->state = TASK_RUNNING; \
10002 + remove_wait_queue(&wq, &__wait); \
10003 +} while (0)
10005 +#define wait_cond_interruptible(wq, condition, lock, flags) \
10006 +({ \
10007 + int __ret = 0; \
10008 + if (!(condition)) \
10009 + __wait_cond_interruptible(wq, condition, lock, flags, \
10010 + __ret); \
10011 + __ret; \
10014 +#define __wait_cond_interruptible_timeout(wq, condition, lock, flags, \
10015 + timeout, ret) \
10016 +do { \
10017 + signed long __timeout = timeout; \
10018 + wait_queue_t __wait; \
10019 + init_waitqueue_entry(&__wait, current); \
10021 + add_wait_queue(&wq, &__wait); \
10022 + for (;;) { \
10023 + set_current_state(TASK_INTERRUPTIBLE); \
10024 + if (__timeout == 0) \
10025 + break; \
10026 + if (condition) \
10027 + break; \
10028 + if (!signal_pending(current)) { \
10029 + spin_unlock_irqrestore(lock, flags); \
10030 + __timeout = schedule_timeout(__timeout); \
10031 + spin_lock_irqsave(lock, flags); \
10032 + continue; \
10033 + } \
10034 + ret = -ERESTARTSYS; \
10035 + break; \
10036 + } \
10037 + current->state = TASK_RUNNING; \
10038 + remove_wait_queue(&wq, &__wait); \
10039 +} while (0)
10041 +#define wait_cond_interruptible_timeout(wq, condition, lock, flags, \
10042 + timeout) \
10043 +({ \
10044 + int __ret = 0; \
10045 + if (!(condition)) \
10046 + __wait_cond_interruptible_timeout(wq, condition, lock, \
10047 + flags, timeout, __ret); \
10048 + __ret; \
10052 +/* Defines */
10054 +#define GS_VERSION_STR "v1.0"
10055 +#define GS_VERSION_NUM 0x0100
10057 +#define GS_LONG_NAME "Gadget Serial"
10058 +#define GS_SHORT_NAME "g_serial"
10060 +#define GS_MAJOR 127
10061 +#define GS_MINOR_START 0
10063 +#define GS_NUM_PORTS 16
10065 +#define GS_NUM_CONFIGS 1
10066 +#define GS_NO_CONFIG_ID 0
10067 +#define GS_BULK_CONFIG_ID 2
10069 +#define GS_NUM_INTERFACES 1
10070 +#define GS_INTERFACE_ID 0
10071 +#define GS_ALT_INTERFACE_ID 0
10073 +#define GS_NUM_ENDPOINTS 2
10075 +#define GS_MAX_DESC_LEN 256
10077 +#define GS_DEFAULT_READ_Q_SIZE 32
10078 +#define GS_DEFAULT_WRITE_Q_SIZE 32
10080 +#define GS_DEFAULT_WRITE_BUF_SIZE 8192
10081 +#define GS_TMP_BUF_SIZE 8192
10083 +#define GS_CLOSE_TIMEOUT 15
10085 +/* debug macro */
10086 +#if G_SERIAL_DEBUG
10088 +static int debug = G_SERIAL_DEBUG;
10090 +#define gs_debug(format, arg...) \
10091 + do { if(debug) printk( KERN_DEBUG format, ## arg ); } while(0)
10092 +#define gs_debug_level(level, format, arg...) \
10093 + do { if(debug>=level) printk( KERN_DEBUG format, ## arg ); } while(0)
10095 +#else
10097 +#define gs_debug(format, arg...) \
10098 + do { } while(0)
10099 +#define gs_debug_level(level, format, arg...) \
10100 + do { } while(0)
10102 +#endif /* G_SERIAL_DEBUG */
10105 +/* Thanks to NetChip Technologies for donating this product ID.
10107 + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
10108 + * Instead: allocate your own, using normal USB-IF procedures.
10109 + */
10110 +#define GS_VENDOR_ID 0x0525 /* NetChip */
10111 +#define GS_PRODUCT_ID 0xa4a6 /* Linux-USB Serial Gadget */
10114 +/* Structures */
10116 +struct gs_dev;
10118 +/* circular buffer */
10119 +struct gs_buf {
10120 + unsigned int buf_size;
10121 + char *buf_buf;
10122 + char *buf_get;
10123 + char *buf_put;
10126 +/* list of requests */
10127 +struct gs_req_entry {
10128 + struct list_head re_entry;
10129 + struct usb_request *re_req;
10132 +/* the port structure holds info for each port, one for each minor number */
10133 +struct gs_port {
10134 + struct gs_dev *port_dev; /* pointer to device struct */
10135 + struct tty_struct *port_tty; /* pointer to tty struct */
10136 + spinlock_t port_lock;
10137 + int port_num;
10138 + int port_open_count;
10139 + int port_in_use; /* open/close in progress */
10140 + wait_queue_head_t port_write_wait;/* waiting to write */
10141 + struct gs_buf *port_write_buf;
10144 +/* the device structure holds info for the USB device */
10145 +struct gs_dev {
10146 + struct usb_gadget *dev_gadget; /* gadget device pointer */
10147 + spinlock_t dev_lock; /* lock for set/reset config */
10148 + int dev_config; /* configuration number */
10149 + struct usb_ep *dev_in_ep; /* address of in endpoint */
10150 + struct usb_ep *dev_out_ep; /* address of out endpoint */
10151 + struct usb_request *dev_ctrl_req; /* control request */
10152 + struct list_head dev_req_list; /* list of write requests */
10153 + int dev_sched_port; /* round robin port scheduled */
10154 + struct gs_port *dev_port[GS_NUM_PORTS]; /* the ports */
10158 +/* Functions */
10160 +/* module */
10161 +static int __init gs_module_init( void );
10162 +static void __exit gs_module_exit( void );
10164 +/* tty driver */
10165 +static int gs_open( struct tty_struct *tty, struct file *file );
10166 +static void gs_close( struct tty_struct *tty, struct file *file );
10167 +static int gs_write( struct tty_struct *tty, int from_user,
10168 + const unsigned char *buf, int count );
10169 +static void gs_put_char( struct tty_struct *tty, unsigned char ch );
10170 +static void gs_flush_chars( struct tty_struct *tty );
10171 +static int gs_write_room( struct tty_struct *tty );
10172 +static int gs_chars_in_buffer( struct tty_struct *tty );
10173 +static void gs_throttle( struct tty_struct * tty );
10174 +static void gs_unthrottle( struct tty_struct * tty );
10175 +static void gs_break( struct tty_struct *tty, int break_state );
10176 +static int gs_ioctl( struct tty_struct *tty, struct file *file,
10177 + unsigned int cmd, unsigned long arg );
10178 +static void gs_set_termios( struct tty_struct *tty, struct termios *old );
10179 +static int gs_read_proc( char *page, char **start, off_t off, int count,
10180 + int *eof, void *data );
10182 +static int gs_send( struct gs_dev *dev );
10183 +static int gs_send_packet( struct gs_dev *dev, char *packet,
10184 + unsigned int size );
10185 +static int gs_recv_packet( struct gs_dev *dev, char *packet,
10186 + unsigned int size );
10187 +static void gs_read_complete( struct usb_ep *ep, struct usb_request *req );
10188 +static void gs_write_complete( struct usb_ep *ep, struct usb_request *req );
10190 +/* gadget driver */
10191 +static int gs_bind( struct usb_gadget *gadget );
10192 +static void gs_unbind( struct usb_gadget *gadget );
10193 +static int gs_setup( struct usb_gadget *gadget,
10194 + const struct usb_ctrlrequest *ctrl );
10195 +static void gs_setup_complete( struct usb_ep *ep, struct usb_request *req );
10196 +static void gs_disconnect( struct usb_gadget *gadget );
10197 +static int gs_set_config( struct gs_dev *dev, unsigned config );
10198 +static void gs_reset_config( struct gs_dev *dev );
10199 +static int gs_build_config_desc( u8 *buf, enum usb_device_speed speed,
10200 + u8 type, unsigned int index );
10202 +static struct usb_request *gs_alloc_req( struct usb_ep *ep, unsigned int len,
10203 + int kmalloc_flags );
10204 +static void gs_free_req( struct usb_ep *ep, struct usb_request *req );
10206 +static struct gs_req_entry *gs_alloc_req_entry( struct usb_ep *ep, unsigned len,
10207 + int kmalloc_flags );
10208 +static void gs_free_req_entry( struct usb_ep *ep, struct gs_req_entry *req );
10210 +static int gs_alloc_ports( struct gs_dev *dev, int kmalloc_flags );
10211 +static void gs_free_ports( struct gs_dev *dev );
10213 +/* circular buffer */
10214 +static struct gs_buf *gs_buf_alloc( unsigned int size, int kmalloc_flags );
10215 +static void gs_buf_free( struct gs_buf *gb );
10216 +static void gs_buf_clear( struct gs_buf *gb );
10217 +static unsigned int gs_buf_data_avail( struct gs_buf *gb );
10218 +static unsigned int gs_buf_space_avail( struct gs_buf *gb );
10219 +static unsigned int gs_buf_put( struct gs_buf *gb, const char *buf,
10220 + unsigned int count );
10221 +static unsigned int gs_buf_get( struct gs_buf *gb, char *buf,
10222 + unsigned int count );
10224 +/* external functions */
10225 +extern int net2280_set_fifo_mode(struct usb_gadget *gadget, int mode);
10228 +/* Globals */
10230 +static struct gs_dev *gs_device;
10232 +static const char *EP_IN_NAME;
10233 +static const char *EP_OUT_NAME;
10235 +static struct semaphore gs_open_close_sem[GS_NUM_PORTS];
10237 +static unsigned int read_q_size = GS_DEFAULT_READ_Q_SIZE;
10238 +static unsigned int write_q_size = GS_DEFAULT_WRITE_Q_SIZE;
10240 +static unsigned int write_buf_size = GS_DEFAULT_WRITE_BUF_SIZE;
10242 +static unsigned char gs_tmp_buf[GS_TMP_BUF_SIZE];
10243 +static struct semaphore gs_tmp_buf_sem;
10245 +/* tty variables */
10246 +static int gs_refcount;
10247 +static struct tty_struct *gs_tty[GS_NUM_PORTS];
10248 +static struct termios *gs_termios[GS_NUM_PORTS];
10249 +static struct termios *gs_termios_locked[GS_NUM_PORTS];
10251 +/* tty driver struct */
10252 +static struct tty_driver gs_tty_driver = {
10253 + .magic = TTY_DRIVER_MAGIC,
10254 + .driver_name = GS_SHORT_NAME,
10255 + .name = "ttygs",
10256 + .major = GS_MAJOR,
10257 + .minor_start = GS_MINOR_START,
10258 + .num = GS_NUM_PORTS,
10259 + .type = TTY_DRIVER_TYPE_SERIAL,
10260 + .subtype = SERIAL_TYPE_NORMAL,
10261 + .flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS,
10262 + .refcount = &gs_refcount,
10263 + .table = gs_tty,
10264 + .termios = gs_termios,
10265 + .termios_locked = gs_termios_locked,
10267 + .open = gs_open,
10268 + .close = gs_close,
10269 + .write = gs_write,
10270 + .put_char = gs_put_char,
10271 + .flush_chars = gs_flush_chars,
10272 + .write_room = gs_write_room,
10273 + .ioctl = gs_ioctl,
10274 + .set_termios = gs_set_termios,
10275 + .throttle = gs_throttle,
10276 + .unthrottle = gs_unthrottle,
10277 + .break_ctl = gs_break,
10278 + .chars_in_buffer = gs_chars_in_buffer,
10279 + .read_proc = gs_read_proc,
10282 +/* gadget driver struct */
10283 +static struct usb_gadget_driver gs_gadget_driver = {
10284 +#ifdef CONFIG_USB_GADGET_DUALSPEED
10285 + .speed = USB_SPEED_HIGH,
10286 +#else
10287 + .speed = USB_SPEED_FULL,
10288 +#endif
10289 + .function = GS_LONG_NAME,
10290 + .bind = gs_bind,
10291 + .unbind = gs_unbind,
10292 + .setup = gs_setup,
10293 + .disconnect = gs_disconnect,
10294 + .driver = {
10295 + .name = GS_SHORT_NAME,
10296 + /* .shutdown = ... */
10297 + /* .suspend = ... */
10298 + /* .resume = ... */
10299 + },
10303 +/* USB descriptors */
10305 +#define GS_MANUFACTURER_STR_ID 1
10306 +#define GS_PRODUCT_STR_ID 2
10307 +#define GS_SERIAL_STR_ID 3
10308 +#define GS_CONFIG_STR_ID 4
10310 +/* static strings, in iso 8859/1 */
10311 +static char manufacturer[40];
10312 +static struct usb_string gs_strings[] = {
10313 + { GS_MANUFACTURER_STR_ID, manufacturer },
10314 + { GS_PRODUCT_STR_ID, GS_LONG_NAME },
10315 + { GS_SERIAL_STR_ID, "0" },
10316 + { GS_CONFIG_STR_ID, "Bulk" },
10317 + { } /* end of list */
10320 +static struct usb_gadget_strings gs_string_table = {
10321 + .language = 0x0409, /* en-us */
10322 + .strings = gs_strings,
10325 +static struct usb_device_descriptor gs_device_desc = {
10326 + .bLength = USB_DT_DEVICE_SIZE,
10327 + .bDescriptorType = USB_DT_DEVICE,
10328 + .bcdUSB = __constant_cpu_to_le16(0x0200),
10329 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
10330 + .idVendor = __constant_cpu_to_le16(GS_VENDOR_ID),
10331 + .idProduct = __constant_cpu_to_le16(GS_PRODUCT_ID),
10332 + .iManufacturer = GS_MANUFACTURER_STR_ID,
10333 + .iProduct = GS_PRODUCT_STR_ID,
10334 + .iSerialNumber = GS_SERIAL_STR_ID,
10335 + .bNumConfigurations = GS_NUM_CONFIGS,
10338 +static const struct usb_config_descriptor gs_config_desc = {
10339 + .bLength = USB_DT_CONFIG_SIZE,
10340 + .bDescriptorType = USB_DT_CONFIG,
10341 + /* .wTotalLength set by gs_build_config_desc */
10342 + .bNumInterfaces = GS_NUM_INTERFACES,
10343 + .bConfigurationValue = GS_BULK_CONFIG_ID,
10344 + .iConfiguration = GS_CONFIG_STR_ID,
10345 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
10346 + .bMaxPower = 1,
10349 +static const struct usb_interface_descriptor gs_interface_desc = {
10350 + .bLength = USB_DT_INTERFACE_SIZE,
10351 + .bDescriptorType = USB_DT_INTERFACE,
10352 + .bNumEndpoints = GS_NUM_ENDPOINTS,
10353 + .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
10354 + .iInterface = GS_CONFIG_STR_ID,
10357 +static struct usb_endpoint_descriptor gs_fullspeed_in_desc = {
10358 + .bLength = USB_DT_ENDPOINT_SIZE,
10359 + .bDescriptorType = USB_DT_ENDPOINT,
10360 + .bEndpointAddress = USB_DIR_IN,
10361 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
10364 +static struct usb_endpoint_descriptor gs_fullspeed_out_desc = {
10365 + .bLength = USB_DT_ENDPOINT_SIZE,
10366 + .bDescriptorType = USB_DT_ENDPOINT,
10367 + .bEndpointAddress = USB_DIR_OUT,
10368 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
10371 +static struct usb_endpoint_descriptor gs_highspeed_in_desc = {
10372 + .bLength = USB_DT_ENDPOINT_SIZE,
10373 + .bDescriptorType = USB_DT_ENDPOINT,
10374 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
10375 + .wMaxPacketSize = __constant_cpu_to_le16(512),
10378 +static struct usb_endpoint_descriptor gs_highspeed_out_desc = {
10379 + .bLength = USB_DT_ENDPOINT_SIZE,
10380 + .bDescriptorType = USB_DT_ENDPOINT,
10381 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
10382 + .wMaxPacketSize = __constant_cpu_to_le16(512),
10385 +#ifdef CONFIG_USB_GADGET_DUALSPEED
10386 +static struct usb_qualifier_descriptor gs_qualifier_desc = {
10387 + .bLength = sizeof(struct usb_qualifier_descriptor),
10388 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
10389 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
10390 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
10391 + /* assumes ep0 uses the same value for both speeds ... */
10392 + .bNumConfigurations = GS_NUM_CONFIGS,
10394 +#endif
10397 +/* Module */
10399 +MODULE_DESCRIPTION( GS_LONG_NAME );
10400 +MODULE_AUTHOR( "Al Borchers" );
10401 +MODULE_LICENSE( "GPL" );
10403 +#if G_SERIAL_DEBUG
10404 +MODULE_PARM( debug, "i" );
10405 +MODULE_PARM_DESC( debug, "Enable debugging, 0=off, 1=on" );
10406 +#endif
10408 +MODULE_PARM( read_q_size, "i" );
10409 +MODULE_PARM_DESC( read_q_size, "Read request queue size, default=32" );
10411 +MODULE_PARM( write_q_size, "i" );
10412 +MODULE_PARM_DESC( write_q_size, "Write request queue size, default=32" );
10414 +MODULE_PARM( write_buf_size, "i" );
10415 +MODULE_PARM_DESC( write_buf_size, "Write buffer size, default=8192" );
10417 +module_init( gs_module_init );
10418 +module_exit( gs_module_exit );
10421 +* gs_module_init
10423 +* Register as a USB gadget driver and a tty driver.
10426 +static int __init gs_module_init( void )
10429 + int i,ret;
10432 + if( (ret=usb_gadget_register_driver( &gs_gadget_driver )) ) {
10433 + printk( KERN_ERR
10434 + "gs_module_init: cannot register gadget driver, ret=%d\n",
10435 + ret );
10436 + return( ret );
10439 + /* initial stty settings */
10440 + gs_tty_driver.init_termios = tty_std_termios;
10441 + gs_tty_driver.init_termios.c_cflag
10442 + = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
10444 + for( i=0; i<GS_NUM_PORTS; i++ )
10445 + sema_init( &gs_open_close_sem[i], 1 );
10447 + sema_init( &gs_tmp_buf_sem, 1 );
10449 + if( (ret=tty_register_driver( &gs_tty_driver )) ) {
10450 + usb_gadget_unregister_driver( &gs_gadget_driver );
10451 + printk( KERN_ERR
10452 + "gs_module_init: cannot register tty driver, ret=%d\n",
10453 + ret );
10454 + return( ret );
10455 + }
10457 + printk( KERN_INFO "gs_module_init: %s %s loaded\n", GS_LONG_NAME,
10458 + GS_VERSION_STR );
10460 + return( 0 );
10466 +* gs_module_exit
10468 +* Unregister as a tty driver and a USB gadget driver.
10471 +static void __exit gs_module_exit( void )
10474 + tty_unregister_driver( &gs_tty_driver );
10475 + usb_gadget_unregister_driver( &gs_gadget_driver );
10477 + printk( KERN_INFO "gs_module_exit: %s %s unloaded\n",
10478 + GS_LONG_NAME, GS_VERSION_STR );
10483 +/* TTY Driver */
10486 + * gs_open
10487 + */
10489 +static int gs_open( struct tty_struct *tty, struct file *file )
10492 + int port_num;
10493 + unsigned long flags;
10494 + struct gs_port *port;
10495 + struct gs_dev *dev;
10496 + struct gs_buf *buf;
10497 + struct semaphore *sem;
10500 + port_num = MINOR( tty->device ) - GS_MINOR_START;
10502 + gs_debug( "gs_open: (%d,%p,%p)\n", port_num, tty, file );
10504 + tty->driver_data = NULL;
10506 + if( port_num < 0 || port_num >= GS_NUM_PORTS ) {
10507 + printk( KERN_ERR "gs_open: (%d,%p,%p) invalid port number\n",
10508 + port_num, tty, file );
10509 + return( -ENODEV );
10512 + dev = gs_device;
10514 + if( dev == NULL ) {
10515 + printk( KERN_ERR "gs_open: (%d,%p,%p) NULL device pointer\n",
10516 + port_num, tty, file );
10517 + return( -ENODEV );
10520 + sem = &gs_open_close_sem[port_num];
10521 + if( down_interruptible( sem ) ) {
10522 + printk( KERN_ERR
10523 + "gs_open: (%d,%p,%p) interrupted waiting for semaphore\n",
10524 + port_num, tty, file );
10525 + return( -ERESTARTSYS );
10528 + spin_lock_irqsave(&dev->dev_lock, flags );
10530 + if( dev->dev_config == GS_NO_CONFIG_ID ) {
10531 + printk( KERN_ERR
10532 + "gs_open: (%d,%p,%p) device is not connected\n",
10533 + port_num, tty, file );
10534 + spin_unlock_irqrestore(&dev->dev_lock, flags );
10535 + up( sem );
10536 + return( -ENODEV );
10539 + port = dev->dev_port[port_num];
10541 + if( port == NULL ) {
10542 + printk( KERN_ERR "gs_open: (%d,%p,%p) NULL port pointer\n",
10543 + port_num, tty, file );
10544 + spin_unlock_irqrestore(&dev->dev_lock, flags );
10545 + up( sem );
10546 + return( -ENODEV );
10549 + spin_lock(&port->port_lock );
10550 + spin_unlock(&dev->dev_lock );
10552 + if( port->port_dev == NULL ) {
10553 + printk( KERN_ERR "gs_open: (%d,%p,%p) port disconnected (1)\n",
10554 + port_num, tty, file );
10555 + spin_unlock_irqrestore(&port->port_lock, flags );
10556 + up( sem );
10557 + return( -EIO );
10560 + if( port->port_open_count > 0 ) {
10561 + ++port->port_open_count;
10562 + spin_unlock_irqrestore(&port->port_lock, flags );
10563 + gs_debug( "gs_open: (%d,%p,%p) already open\n",
10564 + port_num, tty, file );
10565 + up( sem );
10566 + return( 0 );
10569 + /* mark port as in use, we can drop port lock and sleep if necessary */
10570 + port->port_in_use = 1;
10572 + /* allocate write buffer on first open */
10573 + if( port->port_write_buf == NULL ) {
10575 + spin_unlock_irqrestore(&port->port_lock, flags );
10576 + buf = gs_buf_alloc( write_buf_size, GFP_KERNEL );
10577 + spin_lock_irqsave(&port->port_lock, flags );
10579 + /* might have been disconnected while asleep, check */
10580 + if( port->port_dev == NULL ) {
10581 + printk( KERN_ERR
10582 + "gs_open: (%d,%p,%p) port disconnected (2)\n",
10583 + port_num, tty, file );
10584 + port->port_in_use = 0;
10585 + spin_unlock_irqrestore(&port->port_lock, flags );
10586 + up( sem );
10587 + return( -EIO );
10590 + if( (port->port_write_buf=buf) == NULL ) {
10591 + printk( KERN_ERR "gs_open: (%d,%p,%p) cannot allocate port write buffer\n",
10592 + port_num, tty, file );
10593 + port->port_in_use = 0;
10594 + spin_unlock_irqrestore(&port->port_lock, flags );
10595 + up( sem );
10596 + return( -ENOMEM );
10601 + /* wait for carrier detect (not implemented) */
10603 + /* might have been disconnected while asleep, check */
10604 + if( port->port_dev == NULL ) {
10605 + printk( KERN_ERR "gs_open: (%d,%p,%p) port disconnected (3)\n",
10606 + port_num, tty, file );
10607 + port->port_in_use = 0;
10608 + spin_unlock_irqrestore(&port->port_lock, flags );
10609 + up( sem );
10610 + return( -EIO );
10613 + tty->driver_data = port;
10614 + port->port_tty = tty;
10615 + port->port_open_count = 1;
10616 + port->port_in_use = 0;
10618 + spin_unlock_irqrestore(&port->port_lock, flags );
10619 + up( sem );
10621 + gs_debug( "gs_open: (%d,%p,%p) completed\n", port_num, tty, file );
10623 + return( 0 );
10629 + * gs_close
10630 + */
10632 +static void gs_close( struct tty_struct *tty, struct file *file )
10635 + unsigned long flags;
10636 + struct gs_port *port = tty->driver_data;
10637 + struct semaphore *sem;
10640 + if( port == NULL ) {
10641 + printk( KERN_ERR "gs_close: NULL port pointer\n" );
10642 + return;
10645 + gs_debug( "gs_close: (%d,%p,%p)\n", port->port_num, tty, file );
10647 + sem = &gs_open_close_sem[port->port_num];
10648 + down( sem );
10650 + spin_lock_irqsave(&port->port_lock, flags );
10652 + if( port->port_open_count == 0 ) {
10653 + printk( KERN_ERR
10654 + "gs_close: (%d,%p,%p) port is already closed\n",
10655 + port->port_num, tty, file );
10656 + spin_unlock_irqrestore(&port->port_lock, flags );
10657 + up( sem );
10658 + return;
10661 + if( port->port_open_count > 0 ) {
10662 + --port->port_open_count;
10663 + spin_unlock_irqrestore(&port->port_lock, flags );
10664 + up( sem );
10665 + return;
10668 + /* free disconnected port on final close */
10669 + if( port->port_dev == NULL ) {
10670 + kfree( port );
10671 + spin_unlock_irqrestore(&port->port_lock, flags );
10672 + up( sem );
10673 + return;
10676 + /* mark port as closed but in use, we can drop port lock */
10677 + /* and sleep if necessary */
10678 + port->port_in_use = 1;
10679 + port->port_open_count = 0;
10681 + /* wait for write buffer to drain, or */
10682 + /* at most GS_CLOSE_TIMEOUT seconds */
10683 + if( gs_buf_data_avail( port->port_write_buf ) > 0 ) {
10684 + wait_cond_interruptible_timeout( port->port_write_wait,
10685 + port->port_dev == NULL
10686 + || gs_buf_data_avail(port->port_write_buf) == 0,
10687 + &port->port_lock, flags, GS_CLOSE_TIMEOUT * HZ );
10690 + /* free disconnected port on final close */
10691 + /* (might have happened during the above sleep) */
10692 + if( port->port_dev == NULL ) {
10693 + kfree( port );
10694 + spin_unlock_irqrestore(&port->port_lock, flags );
10695 + up( sem );
10696 + return;
10699 + gs_buf_clear( port->port_write_buf );
10701 + tty->driver_data = NULL;
10702 + port->port_tty = NULL;
10703 + port->port_in_use = 0;
10705 + spin_unlock_irqrestore(&port->port_lock, flags );
10706 + up( sem );
10708 + gs_debug( "gs_close: (%d,%p,%p) completed\n",
10709 + port->port_num, tty, file );
10715 + * gs_write
10716 + */
10718 +static int gs_write( struct tty_struct *tty, int from_user,
10719 + const unsigned char *buf, int count )
10722 + unsigned long flags;
10723 + struct gs_port *port = tty->driver_data;
10726 + if( port == NULL ) {
10727 + printk( KERN_ERR "gs_write: NULL port pointer\n" );
10728 + return( -EIO );
10731 + gs_debug( "gs_write: (%d,%p) writing %d bytes\n", port->port_num, tty,
10732 + count );
10734 + if( count == 0 )
10735 + return( 0 );
10737 + /* copy from user into tmp buffer, get tmp_buf semaphore */
10738 + if( from_user ) {
10739 + if( count > GS_TMP_BUF_SIZE )
10740 + count = GS_TMP_BUF_SIZE;
10741 + down( &gs_tmp_buf_sem );
10742 + if( copy_from_user( gs_tmp_buf, buf, count ) != 0 ) {
10743 + up( &gs_tmp_buf_sem );
10744 + printk( KERN_ERR
10745 + "gs_write: (%d,%p) cannot copy from user space\n",
10746 + port->port_num, tty );
10747 + return( -EFAULT );
10749 + buf = gs_tmp_buf;
10752 + spin_lock_irqsave(&port->port_lock, flags );
10754 + if( port->port_dev == NULL ) {
10755 + printk( KERN_ERR "gs_write: (%d,%p) port is not connected\n",
10756 + port->port_num, tty );
10757 + spin_unlock_irqrestore(&port->port_lock, flags );
10758 + if( from_user )
10759 + up( &gs_tmp_buf_sem );
10760 + return( -EIO );
10763 + if( port->port_open_count == 0 ) {
10764 + printk( KERN_ERR "gs_write: (%d,%p) port is closed\n",
10765 + port->port_num, tty );
10766 + spin_unlock_irqrestore(&port->port_lock, flags );
10767 + if( from_user )
10768 + up( &gs_tmp_buf_sem );
10769 + return( -EBADF );
10772 + count = gs_buf_put( port->port_write_buf, buf, count );
10774 + spin_unlock_irqrestore(&port->port_lock, flags );
10776 + if( from_user )
10777 + up( &gs_tmp_buf_sem );
10779 + gs_send( gs_device );
10781 + gs_debug( "gs_write: (%d,%p) wrote %d bytes\n", port->port_num, tty,
10782 + count );
10784 + return( count );
10790 + * gs_put_char
10791 + */
10793 +static void gs_put_char( struct tty_struct *tty, unsigned char ch )
10796 + unsigned long flags;
10797 + struct gs_port *port = tty->driver_data;
10800 + if( port == NULL ) {
10801 + printk( KERN_ERR "gs_put_char: NULL port pointer\n" );
10802 + return;
10805 + gs_debug( "gs_put_char: (%d,%p) char=0x%x, called from %p, %p, %p\n", port->port_num, tty, ch, __builtin_return_address(0), __builtin_return_address(1), __builtin_return_address(2) );
10807 + spin_lock_irqsave(&port->port_lock, flags );
10809 + if( port->port_dev == NULL ) {
10810 + printk( KERN_ERR "gs_put_char: (%d,%p) port is not connected\n",
10811 + port->port_num, tty );
10812 + spin_unlock_irqrestore(&port->port_lock, flags );
10813 + return;
10816 + if( port->port_open_count == 0 ) {
10817 + printk( KERN_ERR "gs_put_char: (%d,%p) port is closed\n",
10818 + port->port_num, tty );
10819 + spin_unlock_irqrestore(&port->port_lock, flags );
10820 + return;
10823 + gs_buf_put( port->port_write_buf, &ch, 1 );
10825 + spin_unlock_irqrestore(&port->port_lock, flags );
10831 + * gs_flush_chars
10832 + */
10834 +static void gs_flush_chars( struct tty_struct *tty )
10837 + unsigned long flags;
10838 + struct gs_port *port = tty->driver_data;
10841 + if( port == NULL ) {
10842 + printk( KERN_ERR "gs_flush_chars: NULL port pointer\n" );
10843 + return;
10846 + gs_debug( "gs_flush_chars: (%d,%p)\n", port->port_num, tty );
10848 + spin_lock_irqsave(&port->port_lock, flags );
10850 + if( port->port_dev == NULL ) {
10851 + printk( KERN_ERR
10852 + "gs_flush_chars: (%d,%p) port is not connected\n",
10853 + port->port_num, tty );
10854 + spin_unlock_irqrestore(&port->port_lock, flags );
10855 + return;
10858 + if( port->port_open_count == 0 ) {
10859 + printk( KERN_ERR "gs_flush_chars: (%d,%p) port is closed\n",
10860 + port->port_num, tty );
10861 + spin_unlock_irqrestore(&port->port_lock, flags );
10862 + return;
10865 + spin_unlock_irqrestore(&port->port_lock, flags );
10867 + gs_send( gs_device );
10873 + * gs_write_room
10874 + */
10876 +static int gs_write_room( struct tty_struct *tty )
10879 + int room = 0;
10880 + unsigned long flags;
10881 + struct gs_port *port = tty->driver_data;
10884 + if( port == NULL )
10885 + return( 0 );
10887 + spin_lock_irqsave(&port->port_lock, flags );
10889 + if( port->port_dev != NULL && port->port_open_count > 0
10890 + && port->port_write_buf != NULL )
10891 + room = gs_buf_space_avail( port->port_write_buf );
10893 + spin_unlock_irqrestore(&port->port_lock, flags );
10895 + gs_debug( "gs_write_room: (%d,%p) room=%d\n",
10896 + port->port_num, tty, room );
10898 + return( room );
10904 + * gs_chars_in_buffer
10905 + */
10907 +static int gs_chars_in_buffer( struct tty_struct *tty )
10910 + int chars = 0;
10911 + unsigned long flags;
10912 + struct gs_port *port = tty->driver_data;
10915 + if( port == NULL )
10916 + return( 0 );
10918 + spin_lock_irqsave(&port->port_lock, flags );
10920 + if( port->port_dev != NULL && port->port_open_count > 0
10921 + && port->port_write_buf != NULL )
10922 + chars = gs_buf_data_avail( port->port_write_buf );
10924 + spin_unlock_irqrestore(&port->port_lock, flags );
10926 + gs_debug( "gs_chars_in_buffer: (%d,%p) chars=%d\n",
10927 + port->port_num, tty, chars );
10929 + return( chars );
10935 + * gs_throttle
10936 + */
10938 +static void gs_throttle( struct tty_struct *tty )
10945 + * gs_unthrottle
10946 + */
10948 +static void gs_unthrottle( struct tty_struct *tty )
10955 + * gs_break
10956 + */
10958 +static void gs_break( struct tty_struct *tty, int break_state )
10965 + * gs_ioctl
10966 + */
10968 +static int gs_ioctl( struct tty_struct *tty, struct file *file,
10969 + unsigned int cmd, unsigned long arg )
10972 + struct gs_port *port = tty->driver_data;
10975 + if( port == NULL ) {
10976 + printk( KERN_ERR "gs_ioctl: NULL port pointer\n" );
10977 + return( -EIO );
10980 + gs_debug( "gs_ioctl: (%d,%p,%p) cmd=0x%4.4x, arg=%lu\n",
10981 + port->port_num, tty, file, cmd, arg );
10983 + /* handle ioctls */
10985 + /* could not handle ioctl */
10986 + return( -ENOIOCTLCMD );
10992 + * gs_set_termios
10993 + */
10995 +static void gs_set_termios( struct tty_struct *tty, struct termios *old )
11002 + * gs_read_proc
11003 + */
11005 +static int gs_read_proc( char *page, char **start, off_t off, int count,
11006 + int *eof, void *data )
11009 + return( 0 );
11015 +* gs_send
11017 +* This function finds available write requests, calls
11018 +* gs_send_packet to fill these packets with data, and
11019 +* continues until either there are no more write requests
11020 +* available or no more data to send. This function is
11021 +* run whenever data arrives or write requests are available.
11024 +static int gs_send( struct gs_dev *dev )
11027 + int ret,len;
11028 + unsigned long flags;
11029 + struct usb_ep *ep;
11030 + struct usb_request *req;
11031 + struct gs_req_entry *req_entry;
11034 + if( dev == NULL ) {
11035 + printk( KERN_ERR "gs_send: NULL device pointer\n" );
11036 + return( -ENODEV );
11039 + spin_lock_irqsave(&dev->dev_lock, flags );
11041 + ep = dev->dev_in_ep;
11043 + while( !list_empty( &dev->dev_req_list ) ) {
11045 + req_entry = list_entry( dev->dev_req_list.next,
11046 + struct gs_req_entry, re_entry );
11048 + req = req_entry->re_req;
11050 + len = gs_send_packet( dev, req->buf, ep->maxpacket );
11052 + if( len > 0 ) {
11053 +gs_debug_level( 3, "gs_send: len=%d, 0x%2.2x 0x%2.2x 0x%2.2x ...\n", len, *((unsigned char *)req->buf), *((unsigned char *)req->buf+1), *((unsigned char *)req->buf+2) );
11054 + list_del( &req_entry->re_entry );
11055 + req->length = len;
11056 + if( (ret=usb_ep_queue( ep, req, GFP_ATOMIC )) ) {
11057 + printk( KERN_ERR
11058 + "gs_send: cannot queue read request, ret=%d\n",
11059 + ret );
11060 + break;
11062 + } else {
11063 + break;
11068 + spin_unlock_irqrestore(&dev->dev_lock, flags );
11070 + return( 0 );
11076 + * gs_send_packet
11078 + * If there is data to send, a packet is built in the given
11079 + * buffer and the size is returned. If there is no data to
11080 + * send, 0 is returned. If there is any error a negative
11081 + * error number is returned.
11083 + * Called during USB completion routine, on interrupt time.
11085 + * We assume that disconnect will not happen until all completion
11086 + * routines have completed, so we can assume that the dev_port
11087 + * array does not change during the lifetime of this function.
11088 + */
11090 +static int gs_send_packet( struct gs_dev *dev, char *packet, unsigned int size )
11093 + unsigned int len;
11094 + struct gs_port *port;
11097 + /* TEMPORARY -- only port 0 is supported right now */
11098 + port = dev->dev_port[0];
11100 + if( port == NULL ) {
11101 + printk( KERN_ERR
11102 + "gs_send_packet: port=%d, NULL port pointer\n",
11103 + 0 );
11104 + return( -EIO );
11107 + spin_lock(&port->port_lock );
11109 + len = gs_buf_data_avail( port->port_write_buf );
11110 + if( len < size )
11111 + size = len;
11113 + if( size == 0 ) {
11114 + spin_unlock(&port->port_lock );
11115 + return( 0 );
11118 + size = gs_buf_get( port->port_write_buf, packet, size );
11120 + wake_up_interruptible( &port->port_tty->write_wait );
11122 + spin_unlock(&port->port_lock );
11124 + return( size );
11130 + * gs_recv_packet
11132 + * Called for each USB packet received. Reads the packet
11133 + * header and stuffs the data in the appropriate tty buffer.
11134 + * Returns 0 if successful, or a negative error number.
11136 + * Called during USB completion routine, on interrupt time.
11138 + * We assume that disconnect will not happen until all completion
11139 + * routines have completed, so we can assume that the dev_port
11140 + * array does not change during the lifetime of this function.
11141 + */
11143 +static int gs_recv_packet( struct gs_dev *dev, char *packet, unsigned int size )
11146 + unsigned int len;
11147 + struct gs_port *port;
11150 + /* TEMPORARY -- only port 0 is supported right now */
11151 + port = dev->dev_port[0];
11153 + if( port == NULL ) {
11154 + printk( KERN_ERR "gs_recv_packet: port=%d, NULL port pointer\n",
11155 + port->port_num );
11156 + return( -EIO );
11159 + spin_lock(&port->port_lock );
11161 + if( port->port_tty == NULL ) {
11162 + printk( KERN_ERR "gs_recv_packet: port=%d, NULL tty pointer\n",
11163 + port->port_num );
11164 + spin_unlock(&port->port_lock );
11165 + return( -EIO );
11168 + if( port->port_tty->magic != TTY_MAGIC ) {
11169 + printk( KERN_ERR "gs_recv_packet: port=%d, bad tty magic\n",
11170 + port->port_num );
11171 + spin_unlock(&port->port_lock );
11172 + return( -EIO );
11175 + len = (unsigned int)(TTY_FLIPBUF_SIZE - port->port_tty->flip.count);
11176 + if( len < size )
11177 + size = len;
11179 + if( size > 0 ) {
11180 + memcpy( port->port_tty->flip.char_buf_ptr, packet, size );
11181 + port->port_tty->flip.char_buf_ptr += size;
11182 + port->port_tty->flip.count += size;
11183 + tty_flip_buffer_push( port->port_tty );
11184 + wake_up_interruptible( &port->port_tty->read_wait );
11187 + spin_unlock(&port->port_lock );
11189 + return( 0 );
11195 +* gs_read_complete
11198 +static void gs_read_complete( struct usb_ep *ep, struct usb_request *req )
11201 + int ret;
11202 + struct gs_dev *dev = ep->driver_data;
11205 + if( dev == NULL ) {
11206 + printk( KERN_ERR "gs_read_complete: NULL device pointer\n" );
11207 + return;
11210 + switch( req->status ) {
11212 + case 0:
11213 + /* normal completion */
11214 + gs_recv_packet( dev, req->buf, req->actual );
11215 +requeue:
11216 + req->length = ep->maxpacket;
11217 + if( (ret=usb_ep_queue( ep, req, GFP_ATOMIC )) ) {
11218 + printk( KERN_ERR
11219 + "gs_read_complete: cannot queue read request, ret=%d\n",
11220 + ret );
11222 + break;
11224 + case -ESHUTDOWN:
11225 + /* disconnect */
11226 + gs_debug( "gs_read_complete: shutdown\n" );
11227 + gs_free_req( ep, req );
11228 + break;
11230 + default:
11231 + /* unexpected */
11232 + printk( KERN_ERR
11233 + "gs_read_complete: unexpected status error, status=%d\n",
11234 + req->status );
11235 + goto requeue;
11236 + break;
11244 +* gs_write_complete
11247 +static void gs_write_complete( struct usb_ep *ep, struct usb_request *req )
11250 + struct gs_dev *dev = ep->driver_data;
11251 + struct gs_req_entry *gs_req = req->context;
11254 + if( dev == NULL ) {
11255 + printk( KERN_ERR "gs_write_complete: NULL device pointer\n" );
11256 + return;
11259 + switch( req->status ) {
11261 + case 0:
11262 + /* normal completion */
11263 +requeue:
11264 + if( gs_req == NULL ) {
11265 + printk( KERN_ERR
11266 + "gs_write_complete: NULL request pointer\n" );
11267 + return;
11270 + spin_lock(&dev->dev_lock );
11271 + list_add( &gs_req->re_entry, &dev->dev_req_list );
11272 + spin_unlock(&dev->dev_lock );
11274 + gs_send( dev );
11276 + break;
11278 + case -ESHUTDOWN:
11279 + /* disconnect */
11280 + gs_debug( "gs_write_complete: shutdown\n" );
11281 + gs_free_req( ep, req );
11282 + break;
11284 + default:
11285 + printk( KERN_ERR
11286 + "gs_write_complete: unexpected status error, status=%d\n",
11287 + req->status );
11288 + goto requeue;
11289 + break;
11296 +/* Gadget Driver */
11299 + * gs_bind
11301 + * Called on module load. Allocates and initializes the device
11302 + * structure and a control request.
11303 + */
11304 +static int gs_bind(struct usb_gadget *gadget)
11306 + int ret;
11307 + struct usb_ep *ep;
11308 + struct gs_dev *dev;
11310 + usb_ep_autoconfig_reset(gadget);
11312 + ep = usb_ep_autoconfig(gadget, &gs_fullspeed_in_desc);
11313 + if (!ep)
11314 + goto autoconf_fail;
11315 + EP_IN_NAME = ep->name;
11316 + ep->driver_data = ep; /* claim the endpoint */
11318 + ep = usb_ep_autoconfig(gadget, &gs_fullspeed_out_desc);
11319 + if (!ep)
11320 + goto autoconf_fail;
11321 + EP_OUT_NAME = ep->name;
11322 + ep->driver_data = ep; /* claim the endpoint */
11324 + /* device specific bcdDevice value in device descriptor */
11325 + if (gadget_is_net2280(gadget)) {
11326 + gs_device_desc.bcdDevice =
11327 + __constant_cpu_to_le16(GS_VERSION_NUM|0x0001);
11328 + } else if (gadget_is_pxa(gadget)) {
11329 + gs_device_desc.bcdDevice =
11330 + __constant_cpu_to_le16(GS_VERSION_NUM|0x0002);
11331 + } else if (gadget_is_sh(gadget)) {
11332 + gs_device_desc.bcdDevice =
11333 + __constant_cpu_to_le16(GS_VERSION_NUM|0x0003);
11334 + } else if (gadget_is_sa1100(gadget)) {
11335 + gs_device_desc.bcdDevice =
11336 + __constant_cpu_to_le16(GS_VERSION_NUM|0x0004);
11337 + } else if (gadget_is_goku(gadget)) {
11338 + gs_device_desc.bcdDevice =
11339 + __constant_cpu_to_le16(GS_VERSION_NUM|0x0005);
11340 + } else if (gadget_is_mq11xx(gadget)) {
11341 + gs_device_desc.bcdDevice =
11342 + __constant_cpu_to_le16(GS_VERSION_NUM|0x0006);
11343 + } else if (gadget_is_omap(gadget)) {
11344 + gs_device_desc.bcdDevice =
11345 + __constant_cpu_to_le16(GS_VERSION_NUM|0x0007);
11346 + } else {
11347 + printk(KERN_WARNING "gs_bind: controller '%s' not recognized\n",
11348 + gadget->name);
11349 + /* unrecognized, but safe unless bulk is REALLY quirky */
11350 + gs_device_desc.bcdDevice =
11351 + __constant_cpu_to_le16(GS_VERSION_NUM|0x0099);
11354 + gs_device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
11355 +#ifdef CONFIG_USB_GADGET_DUALSPEED
11356 + /* assume ep0 uses the same packet size for both speeds */
11357 + gs_qualifier_desc.bMaxPacketSize0 = gs_device_desc.bMaxPacketSize0;
11358 + /* assume endpoints are dual-speed */
11359 + gs_highspeed_in_desc.bEndpointAddress =
11360 + gs_fullspeed_in_desc.bEndpointAddress;
11361 + gs_highspeed_out_desc.bEndpointAddress =
11362 + gs_fullspeed_out_desc.bEndpointAddress;
11363 +#endif /* CONFIG_USB_GADGET_DUALSPEED */
11365 + usb_gadget_set_selfpowered(gadget);
11367 + gs_device = dev = kmalloc(sizeof(struct gs_dev), GFP_KERNEL);
11368 + if (dev == NULL)
11369 + return -ENOMEM;
11371 + snprintf (manufacturer, sizeof(manufacturer),
11372 + UTS_SYSNAME " " UTS_RELEASE " with %s", gadget->name);
11374 + memset(dev, 0, sizeof(struct gs_dev));
11375 + dev->dev_gadget = gadget;
11376 + spin_lock_init(&dev->dev_lock);
11377 + INIT_LIST_HEAD(&dev->dev_req_list);
11378 + set_gadget_data(gadget, dev);
11380 + if ((ret = gs_alloc_ports(dev, GFP_KERNEL)) != 0) {
11381 + printk(KERN_ERR "gs_bind: cannot allocate ports\n");
11382 + gs_unbind(gadget);
11383 + return ret;
11386 + /* preallocate control response and buffer */
11387 + dev->dev_ctrl_req = gs_alloc_req(gadget->ep0, GS_MAX_DESC_LEN,
11388 + GFP_KERNEL);
11389 + if (dev->dev_ctrl_req == NULL) {
11390 + gs_unbind(gadget);
11391 + return -ENOMEM;
11393 + dev->dev_ctrl_req->complete = gs_setup_complete;
11395 + gadget->ep0->driver_data = dev;
11397 + printk(KERN_INFO "gs_bind: %s %s bound\n",
11398 + GS_LONG_NAME, GS_VERSION_STR);
11400 + return 0;
11402 +autoconf_fail:
11403 + printk(KERN_ERR "gs_bind: cannot autoconfigure on %s\n", gadget->name);
11404 + return -ENODEV;
11409 + * gs_unbind
11411 + * Called on module unload. Frees the control request and device
11412 + * structure.
11413 + */
11415 +static void gs_unbind( struct usb_gadget *gadget )
11418 + struct gs_dev *dev = get_gadget_data( gadget );
11421 + gs_device = NULL;
11423 + /* read/write requests already freed, only control request remains */
11424 + if( dev != NULL ) {
11425 + if( dev->dev_ctrl_req != NULL )
11426 + gs_free_req( gadget->ep0, dev->dev_ctrl_req );
11427 + gs_free_ports( dev );
11428 + kfree( dev );
11429 + set_gadget_data( gadget, NULL );
11432 + printk( KERN_INFO "gs_unbind: %s %s unbound\n", GS_LONG_NAME,
11433 + GS_VERSION_STR );
11439 + * gs_setup
11441 + * Implements all the control endpoint functionality that's not
11442 + * handled in hardware or the hardware driver.
11444 + * Returns the size of the data sent to the host, or a negative
11445 + * error number.
11446 + */
11448 +static int gs_setup( struct usb_gadget *gadget,
11449 + const struct usb_ctrlrequest *ctrl )
11452 + int ret = -EOPNOTSUPP;
11453 + unsigned int sv_config;
11454 + struct gs_dev *dev = get_gadget_data( gadget );
11455 + struct usb_request *req = dev->dev_ctrl_req;
11458 + switch (ctrl->bRequest) {
11460 + case USB_REQ_GET_DESCRIPTOR:
11462 + if( ctrl->bRequestType != USB_DIR_IN )
11463 + break;
11465 + switch (ctrl->wValue >> 8) {
11467 + case USB_DT_DEVICE:
11468 + ret = min( ctrl->wLength,
11469 + (u16)sizeof(struct usb_device_descriptor) );
11470 + memcpy( req->buf, &gs_device_desc, ret );
11471 + break;
11473 +#ifdef CONFIG_USB_GADGET_DUALSPEED
11474 + case USB_DT_DEVICE_QUALIFIER:
11475 + if (!gadget->is_dualspeed)
11476 + break;
11477 + ret = min( ctrl->wLength,
11478 + (u16)sizeof(struct usb_qualifier_descriptor) );
11479 + memcpy( req->buf, &gs_qualifier_desc, ret );
11480 + break;
11482 + case USB_DT_OTHER_SPEED_CONFIG:
11483 +#endif /* CONFIG_USB_GADGET_DUALSPEED */
11484 + case USB_DT_CONFIG:
11485 + ret = gs_build_config_desc( req->buf, gadget->speed,
11486 + ctrl->wValue >> 8, ctrl->wValue & 0xff );
11487 + if( ret >= 0 )
11488 + ret = min( ctrl->wLength, (u16)ret );
11489 + break;
11491 + case USB_DT_STRING:
11492 + /* wIndex == language code. */
11493 + ret = usb_gadget_get_string( &gs_string_table,
11494 + ctrl->wValue & 0xff, req->buf );
11495 + if( ret >= 0 )
11496 + ret = min( ctrl->wLength, (u16)ret );
11497 + break;
11499 + break;
11501 + case USB_REQ_SET_CONFIGURATION:
11502 + if( ctrl->bRequestType != 0 )
11503 + break;
11504 + spin_lock( &dev->dev_lock );
11505 + ret = gs_set_config( dev, ctrl->wValue );
11506 + spin_unlock( &dev->dev_lock );
11507 + break;
11509 + case USB_REQ_GET_CONFIGURATION:
11510 + if( ctrl->bRequestType != USB_DIR_IN )
11511 + break;
11512 + *(u8 *)req->buf = dev->dev_config;
11513 + ret = min( ctrl->wLength, (u16)1 );
11514 + break;
11516 + case USB_REQ_SET_INTERFACE:
11517 + if( ctrl->bRequestType != USB_RECIP_INTERFACE )
11518 + break;
11519 + spin_lock( &dev->dev_lock );
11520 + if( dev->dev_config == GS_BULK_CONFIG_ID
11521 + && ctrl->wIndex == GS_INTERFACE_ID
11522 + && ctrl->wValue == GS_ALT_INTERFACE_ID ) {
11523 + sv_config = dev->dev_config;
11524 + /* since there is only one interface, setting the */
11525 + /* interface is equivalent to setting the config */
11526 + gs_reset_config( dev );
11527 + gs_set_config( dev, sv_config );
11528 + ret = 0;
11530 + spin_unlock( &dev->dev_lock );
11531 + break;
11533 + case USB_REQ_GET_INTERFACE:
11534 + if( ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE) )
11535 + break;
11536 + if( dev->dev_config == GS_NO_CONFIG_ID )
11537 + break;
11538 + if( ctrl->wIndex != GS_INTERFACE_ID ) {
11539 + ret = -EDOM;
11540 + break;
11542 + *(u8 *)req->buf = GS_ALT_INTERFACE_ID;
11543 + ret = min( ctrl->wLength, (u16)1 );
11544 + break;
11546 + default:
11547 + printk( KERN_ERR "gs_setup: unknown request, type=%02x, request=%02x, value=%04x, index=%04x, length=%d\n",
11548 + ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
11549 + ctrl->wIndex, ctrl->wLength );
11550 + break;
11554 + /* respond with data transfer before status phase? */
11555 + if( ret >= 0 ) {
11556 + req->length = ret;
11557 + ret = usb_ep_queue( gadget->ep0, req, GFP_ATOMIC );
11558 + if( ret < 0 ) {
11559 + printk( KERN_ERR
11560 + "gs_setup: cannot queue response, ret=%d\n",
11561 + ret );
11562 + req->status = 0;
11563 + gs_setup_complete( gadget->ep0, req );
11567 + /* device either stalls (ret < 0) or reports success */
11568 + return( ret );
11574 + * gs_setup_complete
11575 + */
11577 +static void gs_setup_complete( struct usb_ep *ep, struct usb_request *req )
11579 + if( req->status || req->actual != req->length ) {
11580 + printk( KERN_ERR "gs_setup_complete: status error, status=%d, actual=%d, length=%d\n",
11581 + req->status, req->actual, req->length );
11587 + * gs_disconnect
11589 + * Called when the device is disconnected. Frees the closed
11590 + * ports and disconnects open ports. Open ports will be freed
11591 + * on close. Then reallocates the ports for the next connection.
11592 + */
11594 +static void gs_disconnect( struct usb_gadget *gadget )
11597 + unsigned long flags;
11598 + struct gs_dev *dev = get_gadget_data( gadget );
11601 + spin_lock_irqsave( &dev->dev_lock, flags );
11603 + gs_reset_config( dev );
11605 + /* free closed ports and disconnect open ports */
11606 + /* (open ports will be freed when closed) */
11607 + gs_free_ports( dev );
11609 + /* re-allocate ports for the next connection */
11610 + if( gs_alloc_ports( dev, GFP_ATOMIC ) != 0 )
11611 + printk( KERN_ERR "gs_disconnect: cannot re-allocate ports\n" );
11613 + spin_unlock_irqrestore( &dev->dev_lock, flags );
11615 + printk( KERN_INFO "gs_disconnect: %s disconnected\n", GS_LONG_NAME );
11621 + * gs_set_config
11623 + * Configures the device by enabling device specific
11624 + * optimizations, setting up the endpoints, allocating
11625 + * read and write requests and queuing read requests.
11627 + * The device lock must be held when calling this function.
11628 + */
11630 +static int gs_set_config( struct gs_dev *dev, unsigned config )
11633 + int i;
11634 + int ret = 0;
11635 + struct usb_gadget *gadget = dev->dev_gadget;
11636 + struct usb_ep *ep;
11637 + struct usb_request *req;
11638 + struct gs_req_entry *req_entry;
11641 + if( dev == NULL ) {
11642 + printk( KERN_ERR "gs_set_config: NULL device pointer\n" );
11643 + return( 0 );
11646 + if( config == dev->dev_config )
11647 + return( 0 );
11649 + gs_reset_config( dev );
11651 + if( config == GS_NO_CONFIG_ID )
11652 + return( 0 );
11654 + if( config != GS_BULK_CONFIG_ID )
11655 + return( -EINVAL );
11657 + /* device specific optimizations */
11658 + if (gadget_is_net2280(gadget))
11659 + net2280_set_fifo_mode(gadget, 1);
11661 + gadget_for_each_ep( ep, gadget ) {
11663 + if( strcmp( ep->name, EP_IN_NAME ) == 0 ) {
11664 + ret = usb_ep_enable( ep,
11665 + gadget->speed == USB_SPEED_HIGH ?
11666 + &gs_highspeed_in_desc : &gs_fullspeed_in_desc );
11667 + if( ret == 0 ) {
11668 + ep->driver_data = dev;
11669 + dev->dev_in_ep = ep;
11670 + } else {
11671 + printk( KERN_ERR "gs_set_config: cannot enable in endpoint %s, ret=%d\n",
11672 + ep->name, ret );
11673 + gs_reset_config( dev );
11674 + return( ret );
11678 + else if( strcmp( ep->name, EP_OUT_NAME ) == 0 ) {
11679 + ret = usb_ep_enable( ep,
11680 + gadget->speed == USB_SPEED_HIGH ?
11681 + &gs_highspeed_out_desc :
11682 + &gs_fullspeed_out_desc );
11683 + if( ret == 0 ) {
11684 + ep->driver_data = dev;
11685 + dev->dev_out_ep = ep;
11686 + } else {
11687 + printk( KERN_ERR "gs_set_config: cannot enable out endpoint %s, ret=%d\n",
11688 + ep->name, ret );
11689 + gs_reset_config( dev );
11690 + return( ret );
11696 + if( dev->dev_in_ep == NULL || dev->dev_out_ep == NULL ) {
11697 + gs_reset_config( dev );
11698 + printk( KERN_ERR "gs_set_config: cannot find endpoints\n" );
11699 + return( -ENODEV );
11702 + /* allocate and queue read requests */
11703 + ep = dev->dev_out_ep;
11704 + for( i=0; i<read_q_size && ret == 0; i++ ) {
11705 + if( (req=gs_alloc_req( ep, ep->maxpacket, GFP_ATOMIC )) ) {
11706 + req->complete = gs_read_complete;
11707 + if( (ret=usb_ep_queue( ep, req, GFP_ATOMIC )) ) {
11708 + printk( KERN_ERR "gs_set_config: cannot queue read request, ret=%d\n",
11709 + ret );
11711 + } else {
11712 + gs_reset_config( dev );
11713 + printk( KERN_ERR
11714 + "gs_set_config: cannot allocate read requests\n" );
11715 + return( -ENOMEM );
11719 + /* allocate write requests, and put on free list */
11720 + ep = dev->dev_in_ep;
11721 + for( i=0; i<write_q_size; i++ ) {
11722 + if( (req_entry=gs_alloc_req_entry( ep, ep->maxpacket,
11723 + GFP_ATOMIC )) ) {
11724 + req_entry->re_req->complete = gs_write_complete;
11725 + list_add( &req_entry->re_entry, &dev->dev_req_list );
11726 + } else {
11727 + gs_reset_config( dev );
11728 + printk( KERN_ERR
11729 + "gs_set_config: cannot allocate write requests\n" );
11730 + return( -ENOMEM );
11734 + dev->dev_config = config;
11736 + printk( KERN_INFO "gs_set_config: %s configured for %s speed\n",
11737 + GS_LONG_NAME,
11738 + gadget->speed == USB_SPEED_HIGH ? "high" : "full" );
11740 + return( 0 );
11746 + * gs_reset_config
11748 + * Mark the device as not configured, disable all endpoints,
11749 + * which forces completion of pending I/O and frees queued
11750 + * requests, and free the remaining write requests on the
11751 + * free list.
11753 + * The device lock must be held when calling this function.
11754 + */
11756 +static void gs_reset_config( struct gs_dev *dev )
11759 + struct gs_req_entry *req_entry;
11762 + if( dev == NULL ) {
11763 + printk( KERN_ERR "gs_reset_config: NULL device pointer\n" );
11764 + return;
11767 + if( dev->dev_config == GS_NO_CONFIG_ID )
11768 + return;
11770 + dev->dev_config = GS_NO_CONFIG_ID;
11772 + /* free write requests on the free list */
11773 + while( !list_empty( &dev->dev_req_list ) ) {
11774 + req_entry = list_entry( dev->dev_req_list.next,
11775 + struct gs_req_entry, re_entry );
11776 + list_del( &req_entry->re_entry );
11777 + gs_free_req_entry( dev->dev_in_ep, req_entry );
11780 + /* disable endpoints, forcing completion of pending i/o; */
11781 + /* completion handlers free their requests in this case */
11782 + if( dev->dev_in_ep ) {
11783 + usb_ep_disable( dev->dev_in_ep );
11784 + dev->dev_in_ep = NULL;
11786 + if( dev->dev_out_ep ) {
11787 + usb_ep_disable( dev->dev_out_ep );
11788 + dev->dev_out_ep = NULL;
11795 + * gs_build_config_desc
11797 + * Builds a config descriptor in the given buffer and returns the
11798 + * length, or a negative error number.
11799 + */
11801 +static int gs_build_config_desc( u8 *buf, enum usb_device_speed speed,
11802 + u8 type, unsigned int index )
11805 + int high_speed;
11806 + int len = USB_DT_CONFIG_SIZE + USB_DT_INTERFACE_SIZE
11807 + + GS_NUM_ENDPOINTS * USB_DT_ENDPOINT_SIZE;
11810 + /* only one config */
11811 + if( index != 0 )
11812 + return( -EINVAL );
11814 + memcpy( buf, &gs_config_desc, USB_DT_CONFIG_SIZE );
11815 + ((struct usb_config_descriptor *)buf)->bDescriptorType = type;
11816 + ((struct usb_config_descriptor *)buf)->wTotalLength =
11817 + __constant_cpu_to_le16( len );
11818 + buf += USB_DT_CONFIG_SIZE;
11820 + memcpy( buf, &gs_interface_desc, USB_DT_INTERFACE_SIZE );
11821 + buf += USB_DT_INTERFACE_SIZE;
11823 + /* other speed switches high and full speed */
11824 + high_speed = (speed == USB_SPEED_HIGH);
11825 + if( type == USB_DT_OTHER_SPEED_CONFIG )
11826 + high_speed = !high_speed;
11828 + memcpy( buf,
11829 + high_speed ? &gs_highspeed_in_desc : &gs_fullspeed_in_desc,
11830 + USB_DT_ENDPOINT_SIZE );
11831 + buf += USB_DT_ENDPOINT_SIZE;
11832 + memcpy( buf,
11833 + high_speed ? &gs_highspeed_out_desc : &gs_fullspeed_out_desc,
11834 + USB_DT_ENDPOINT_SIZE );
11836 + return( len );
11842 + * gs_alloc_req
11844 + * Allocate a usb_request and its buffer. Returns a pointer to the
11845 + * usb_request or NULL if there is an error.
11846 + */
11848 +static struct usb_request *gs_alloc_req( struct usb_ep *ep, unsigned int len,
11849 + int kmalloc_flags )
11852 + struct usb_request *req;
11855 + if( ep == NULL )
11856 + return( NULL );
11858 + req = usb_ep_alloc_request( ep, kmalloc_flags );
11860 + if( req != NULL ) {
11861 + req->length = len;
11862 + req->buf = usb_ep_alloc_buffer( ep, len, &req->dma,
11863 + kmalloc_flags );
11864 + if( req->buf == NULL ) {
11865 + usb_ep_free_request( ep, req );
11866 + return( NULL );
11870 + return( req );
11876 + * gs_free_req
11878 + * Free a usb_request and its buffer.
11879 + */
11881 +static void gs_free_req( struct usb_ep *ep, struct usb_request *req )
11883 + if( ep != NULL && req != NULL ) {
11884 + if( req->buf != NULL )
11885 + usb_ep_free_buffer( ep, req->buf, req->dma,
11886 + req->length );
11887 + usb_ep_free_request( ep, req );
11893 + * gs_alloc_req_entry
11895 + * Allocates a request and its buffer, using the given
11896 + * endpoint, buffer len, and kmalloc flags.
11897 + */
11899 +static struct gs_req_entry *gs_alloc_req_entry( struct usb_ep *ep,
11900 + unsigned len, int kmalloc_flags )
11903 + struct gs_req_entry *req;
11906 + req = kmalloc( sizeof(struct gs_req_entry), kmalloc_flags );
11907 + if( req == NULL )
11908 + return( NULL );
11910 + req->re_req = gs_alloc_req( ep, len, kmalloc_flags );
11911 + if( req->re_req == NULL ) {
11912 + kfree( req );
11913 + return( NULL );
11916 + req->re_req->context = req;
11918 + return( req );
11924 + * gs_free_req_entry
11926 + * Frees a request and its buffer.
11927 + */
11929 +static void gs_free_req_entry( struct usb_ep *ep, struct gs_req_entry *req )
11931 + if( ep != NULL && req != NULL ) {
11932 + if( req->re_req != NULL )
11933 + gs_free_req( ep, req->re_req );
11934 + kfree( req );
11940 + * gs_alloc_ports
11942 + * Allocate all ports and set the gs_dev struct to point to them.
11943 + * Return 0 if successful, or a negative error number.
11945 + * The device lock is normally held when calling this function.
11946 + */
11948 +static int gs_alloc_ports( struct gs_dev *dev, int kmalloc_flags )
11951 + int i;
11952 + struct gs_port *port;
11955 + if( dev == NULL )
11956 + return( -EIO );
11958 + for( i=0; i<GS_NUM_PORTS; i++ ) {
11960 + if( (port=(struct gs_port *)kmalloc( sizeof(struct gs_port),
11961 + kmalloc_flags )) == NULL )
11962 + return( -ENOMEM );
11964 + memset( port, 0, sizeof( struct gs_port ) );
11965 + port->port_dev = dev;
11966 + port->port_num = i;
11967 + spin_lock_init( &port->port_lock );
11968 + init_waitqueue_head( &port->port_write_wait );
11970 + dev->dev_port[i] = port;
11974 + return( 0 );
11980 + * gs_free_ports
11982 + * Free all closed ports. Open ports are disconnected by
11983 + * freeing their write buffers, setting their device pointers
11984 + * and the pointers to them in the device to NULL. These
11985 + * ports will be freed when closed.
11987 + * The device lock is normally held when calling this function.
11988 + */
11990 +static void gs_free_ports( struct gs_dev *dev )
11993 + int i;
11994 + unsigned long flags;
11995 + struct gs_port *port;
11998 + if( dev == NULL )
11999 + return;
12001 + for( i=0; i<GS_NUM_PORTS; i++ ) {
12003 + if( (port=dev->dev_port[i]) != NULL ) {
12005 + dev->dev_port[i] = NULL;
12007 + spin_lock_irqsave(&port->port_lock, flags );
12009 + if( port->port_write_buf != NULL ) {
12010 + gs_buf_free( port->port_write_buf );
12011 + port->port_write_buf = NULL;
12014 + if( port->port_open_count > 0 || port->port_in_use ) {
12015 + port->port_dev = NULL;
12016 + wake_up_interruptible( &port->port_write_wait );
12017 + wake_up_interruptible( &port->port_tty->read_wait );
12018 + wake_up_interruptible( &port->port_tty->write_wait );
12019 + } else {
12020 + kfree( port );
12023 + spin_unlock_irqrestore(&port->port_lock, flags );
12032 +/* Circular Buffer */
12035 + * gs_buf_alloc
12037 + * Allocate a circular buffer and all associated memory.
12038 + */
12040 +static struct gs_buf *gs_buf_alloc( unsigned int size, int kmalloc_flags )
12043 + struct gs_buf *gb;
12046 + if( size == 0 )
12047 + return( NULL );
12049 + gb = (struct gs_buf *)kmalloc( sizeof(struct gs_buf), kmalloc_flags );
12050 + if( gb == NULL )
12051 + return( NULL );
12053 + gb->buf_buf = kmalloc( size, kmalloc_flags );
12054 + if( gb->buf_buf == NULL ) {
12055 + kfree( gb );
12056 + return( NULL );
12059 + gb->buf_size = size;
12060 + gb->buf_get = gb->buf_put = gb->buf_buf;
12062 + return( gb );
12068 + * gs_buf_free
12070 + * Free the buffer and all associated memory.
12071 + */
12073 +void gs_buf_free( struct gs_buf *gb )
12075 + if( gb != NULL ) {
12076 + if( gb->buf_buf != NULL )
12077 + kfree( gb->buf_buf );
12078 + kfree( gb );
12084 + * gs_buf_clear
12086 + * Clear out all data in the circular buffer.
12087 + */
12089 +void gs_buf_clear( struct gs_buf *gb )
12091 + if( gb != NULL )
12092 + gb->buf_get = gb->buf_put;
12093 + /* equivalent to a get of all data available */
12098 + * gs_buf_data_avail
12100 + * Return the number of bytes of data available in the circular
12101 + * buffer.
12102 + */
12104 +unsigned int gs_buf_data_avail( struct gs_buf *gb )
12106 + if( gb != NULL )
12107 + return( (gb->buf_size + gb->buf_put - gb->buf_get)
12108 + % gb->buf_size );
12109 + else
12110 + return( 0 );
12115 + * gs_buf_space_avail
12117 + * Return the number of bytes of space available in the circular
12118 + * buffer.
12119 + */
12121 +unsigned int gs_buf_space_avail( struct gs_buf *gb )
12123 + if( gb != NULL )
12124 + return( (gb->buf_size + gb->buf_get - gb->buf_put - 1)
12125 + % gb->buf_size );
12126 + else
12127 + return( 0 );
12132 + * gs_buf_put
12134 + * Copy data data from a user buffer and put it into the circular buffer.
12135 + * Restrict to the amount of space available.
12137 + * Return the number of bytes copied.
12138 + */
12140 +unsigned int gs_buf_put( struct gs_buf *gb, const char *buf,
12141 + unsigned int count )
12144 + unsigned int len;
12147 + if( gb == NULL )
12148 + return( 0 );
12150 + len = gs_buf_space_avail( gb );
12151 + if( count > len )
12152 + count = len;
12154 + if( count == 0 )
12155 + return( 0 );
12157 + len = gb->buf_buf + gb->buf_size - gb->buf_put;
12158 + if( count > len ) {
12159 + memcpy( gb->buf_put, buf, len );
12160 + memcpy( gb->buf_buf, buf+len, count - len );
12161 + gb->buf_put = gb->buf_buf + count - len;
12162 + } else {
12163 + memcpy( gb->buf_put, buf, count );
12164 + if( count < len )
12165 + gb->buf_put += count;
12166 + else /* count == len */
12167 + gb->buf_put = gb->buf_buf;
12170 + return( count );
12176 + * gs_buf_get
12178 + * Get data from the circular buffer and copy to the given buffer.
12179 + * Restrict to the amount of data available.
12181 + * Return the number of bytes copied.
12182 + */
12184 +unsigned int gs_buf_get( struct gs_buf *gb, char *buf, unsigned int count )
12187 + unsigned int len;
12190 + if( gb == NULL )
12191 + return( 0 );
12193 + len = gs_buf_data_avail( gb );
12194 + if( count > len )
12195 + count = len;
12197 + if( count == 0 )
12198 + return( 0 );
12200 + len = gb->buf_buf + gb->buf_size - gb->buf_get;
12201 + if( count > len ) {
12202 + memcpy( buf, gb->buf_get, len );
12203 + memcpy( buf+len, gb->buf_buf, count - len );
12204 + gb->buf_get = gb->buf_buf + count - len;
12205 + } else {
12206 + memcpy( buf, gb->buf_get, count );
12207 + if( count < len )
12208 + gb->buf_get += count;
12209 + else /* count == len */
12210 + gb->buf_get = gb->buf_buf;
12213 + return( count );
12216 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/inode.c kernel/drivers/usb/gadget/inode.c
12217 --- /tmp/kernel/drivers/usb/gadget/inode.c 1970-01-01 01:00:00.000000000 +0100
12218 +++ kernel/drivers/usb/gadget/inode.c 2005-04-22 17:53:19.456535934 +0200
12219 @@ -0,0 +1,1807 @@
12221 + * inode.c -- user mode filesystem api for usb gadget controllers
12223 + * Copyright (C) 2003 David Brownell
12224 + * Copyright (C) 2003 Agilent Technologies
12226 + * This program is free software; you can redistribute it and/or modify
12227 + * it under the terms of the GNU General Public License as published by
12228 + * the Free Software Foundation; either version 2 of the License, or
12229 + * (at your option) any later version.
12231 + * This program is distributed in the hope that it will be useful,
12232 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12233 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12234 + * GNU General Public License for more details.
12236 + * You should have received a copy of the GNU General Public License
12237 + * along with this program; if not, write to the Free Software
12238 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
12239 + */
12242 +#define DEBUG 1 /* data to help fault diagnosis */
12243 +// #define VERBOSE /* extra debug messages (success too) */
12245 +#include <linux/init.h>
12246 +#include <linux/kernel.h>
12247 +#include <linux/module.h>
12248 +#include <linux/fs.h>
12249 +#include <linux/pagemap.h>
12250 +#include <linux/uts.h>
12251 +#include <linux/version.h>
12252 +#include <linux/wait.h>
12253 +#include <linux/compiler.h>
12254 +#include <asm/uaccess.h>
12255 +#include <linux/slab.h>
12257 +#ifndef BUG_ON
12258 +#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
12259 +#endif
12261 +#include <linux/usb_gadgetfs.h>
12262 +#include <linux/usb_gadget.h>
12266 + * The gadgetfs API maps each endpoint to a file descriptor so that you
12267 + * can use standard synchronous read/write calls for I/O. There's some
12268 + * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
12269 + * drivers show how this works in practice.
12271 + * Key parts that must be USB-specific are protocols defining how the
12272 + * read/write operations relate to the hardware state machines. There
12273 + * are two types of files. One type is for the device, implementing ep0.
12274 + * The other type is for each IN or OUT endpoint. In both cases, the
12275 + * user mode driver must configure the hardware before using it.
12277 + * - First, dev_config() is called when /dev/gadget/$CHIP is configured
12278 + * (by writing configuration and device descriptors). Afterwards it
12279 + * may serve as a source of device events, used to handle all control
12280 + * requests other than basic enumeration.
12282 + * - Then either immediately, or after a SET_CONFIGURATION control request,
12283 + * ep_config() is called when each /dev/gadget/ep* file is configured
12284 + * (by writing endpoint descriptors). Afterwards these files are used
12285 + * to write() IN data or to read() OUT data. To halt the endpoint, a
12286 + * "wrong direction" request is issued (like reading an IN endpoint).
12288 + * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
12289 + * not possible on all hardware. For example, precise fault handling with
12290 + * respect to data left in endpoint fifos after aborted operations; or
12291 + * selective clearing of endpoint halts, to implement SET_INTERFACE.
12292 + */
12294 +#define DRIVER_DESC "USB Gadget filesystem"
12295 +#define DRIVER_VERSION "20 Aug 2003"
12297 +static const char driver_desc [] = DRIVER_DESC;
12298 +static const char shortname [] = "gadgetfs";
12300 +MODULE_DESCRIPTION (DRIVER_DESC);
12301 +MODULE_AUTHOR ("David Brownell");
12302 +MODULE_LICENSE ("GPL");
12305 +/*----------------------------------------------------------------------*/
12307 +#define GADGETFS_MAGIC 0xaee71ee7
12308 +#define DMA_ADDR_INVALID (~(dma_addr_t)0)
12310 +/* /dev/gadget/$CHIP represents ep0 and the whole device */
12311 +enum ep0_state {
12312 + /* DISBLED is the initial state.
12313 + */
12314 + STATE_DEV_DISABLED = 0,
12316 + /* Only one open() of /dev/gadget/$CHIP; only one file tracks
12317 + * ep0/device i/o modes and binding to the controller. Driver
12318 + * must always write descriptors to initialize the device, then
12319 + * the device becomes UNCONNECTED until enumeration.
12320 + */
12321 + STATE_OPENED,
12323 + /* From then on, ep0 fd is in either of two basic modes:
12324 + * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
12325 + * - SETUP: read/write will transfer control data and succeed;
12326 + * or if "wrong direction", performs protocol stall
12327 + */
12328 + STATE_UNCONNECTED,
12329 + STATE_CONNECTED,
12330 + STATE_SETUP,
12332 + /* UNBOUND means the driver closed ep0, so the device won't be
12333 + * accessible again (DEV_DISABLED) until all fds are closed.
12334 + */
12335 + STATE_DEV_UNBOUND,
12338 +/* enough for the whole queue: most events invalidate others */
12339 +#define N_EVENT 5
12341 +struct dev_data {
12342 + spinlock_t lock;
12343 + atomic_t count;
12344 + enum ep0_state state;
12345 + struct usb_gadgetfs_event event [N_EVENT];
12346 + unsigned ev_next;
12347 + struct fasync_struct *fasync;
12348 + u8 current_config;
12350 + /* drivers reading ep0 MUST handle control requests (SETUP)
12351 + * reported that way; else the host will time out.
12352 + */
12353 + unsigned usermode_setup : 1,
12354 + setup_in : 1,
12355 + setup_can_stall : 1,
12356 + setup_out_ready : 1,
12357 + setup_out_error : 1,
12358 + setup_abort : 1;
12360 + /* the rest is basically write-once */
12361 + struct usb_config_descriptor *config, *hs_config;
12362 + struct usb_device_descriptor *dev;
12363 + struct usb_request *req;
12364 + struct usb_gadget *gadget;
12365 + struct list_head epfiles;
12366 + void *buf;
12367 + wait_queue_head_t wait;
12368 + struct super_block *sb;
12369 + struct dentry *dentry;
12371 + /* except this scratch i/o buffer for ep0 */
12372 + u8 rbuf [256];
12375 +static inline void get_dev (struct dev_data *data)
12377 + atomic_inc (&data->count);
12380 +static void put_dev (struct dev_data *data)
12382 + if (likely (!atomic_dec_and_test (&data->count)))
12383 + return;
12384 + /* needs no more cleanup */
12385 + BUG_ON (waitqueue_active (&data->wait));
12386 + kfree (data);
12389 +static struct dev_data *dev_new (void)
12391 + struct dev_data *dev;
12393 + dev = kmalloc (sizeof *dev, GFP_KERNEL);
12394 + if (!dev)
12395 + return 0;
12396 + memset (dev, 0, sizeof *dev);
12397 + dev->state = STATE_DEV_DISABLED;
12398 + atomic_set (&dev->count, 1);
12399 + spin_lock_init (&dev->lock);
12400 + INIT_LIST_HEAD (&dev->epfiles);
12401 + init_waitqueue_head (&dev->wait);
12402 + return dev;
12405 +/*----------------------------------------------------------------------*/
12407 +/* other /dev/gadget/$ENDPOINT files represent endpoints */
12408 +enum ep_state {
12409 + STATE_EP_DISABLED = 0,
12410 + STATE_EP_READY,
12411 + STATE_EP_DEFER_ENABLE,
12412 + STATE_EP_ENABLED,
12413 + STATE_EP_UNBOUND,
12416 +struct ep_data {
12417 + struct semaphore lock;
12418 + enum ep_state state;
12419 + atomic_t count;
12420 + struct dev_data *dev;
12421 + /* must hold dev->lock before accessing ep or req */
12422 + struct usb_ep *ep;
12423 + struct usb_request *req;
12424 + ssize_t status;
12425 + char name [16];
12426 + struct usb_endpoint_descriptor desc, hs_desc;
12427 + struct list_head epfiles;
12428 + wait_queue_head_t wait;
12429 + struct dentry *dentry;
12430 + struct inode *inode;
12433 +static inline void get_ep (struct ep_data *data)
12435 + atomic_inc (&data->count);
12438 +static void put_ep (struct ep_data *data)
12440 + if (likely (!atomic_dec_and_test (&data->count)))
12441 + return;
12442 + put_dev (data->dev);
12443 + /* needs no more cleanup */
12444 + BUG_ON (!list_empty (&data->epfiles));
12445 + BUG_ON (waitqueue_active (&data->wait));
12446 + BUG_ON (down_trylock (&data->lock) != 0);
12447 + kfree (data);
12450 +/*----------------------------------------------------------------------*/
12452 +/* most "how to use the hardware" policy choices are in userspace:
12453 + * mapping endpoint roles the driver needs to the capabilities that
12454 + * the usb controller exposes.
12455 + */
12457 + // FIXME the 2.6 version just probes the controller
12458 + // driver to find out the chip name; we should too.
12460 +#ifdef CONFIG_USB_GADGET_NET2280
12461 +#define CHIP "net2280"
12462 +#define HIGHSPEED
12463 +#endif
12465 +#ifdef CONFIG_USB_GADGET_PXA2XX
12466 +#define CHIP "pxa2xx_udc"
12467 +/* earlier hardware doesn't have UDCCFR, races set_{config,interface} */
12468 +#warning works best with pxa255 or newer
12469 +#endif
12471 +#ifdef CONFIG_USB_GADGET_GOKU
12472 +#define CHIP "goku_udc"
12473 +#endif
12475 +#ifdef CONFIG_USB_GADGET_SA1100
12476 +#define CHIP "sa1100"
12477 +#endif
12479 +#ifdef CONFIG_USB_GADGET_SUPERH
12480 +#define CHIP "superh_udc"
12481 +#endif
12483 +#ifdef CONFIG_USB_GADGET_N9604
12484 +#define CHIP "n9604_udc"
12485 +#endif
12488 +/*----------------------------------------------------------------------*/
12490 +/* NOTE: don't use dev_printk calls before binding to the gadget
12491 + * at the end of ep0 configuration, or after unbind.
12492 + */
12494 +/* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
12495 +#define xprintk(d,level,fmt,args...) \
12496 + printk(level "%s: " fmt , shortname , ## args)
12498 +#ifdef DEBUG
12499 +#define DBG(dev,fmt,args...) \
12500 + xprintk(dev , KERN_DEBUG , fmt , ## args)
12501 +#else
12502 +#define DBG(dev,fmt,args...) \
12503 + do { } while (0)
12504 +#endif /* DEBUG */
12506 +#ifdef VERBOSE
12507 +#define VDEBUG DBG
12508 +#else
12509 +#define VDEBUG(dev,fmt,args...) \
12510 + do { } while (0)
12511 +#endif /* DEBUG */
12513 +#define ERROR(dev,fmt,args...) \
12514 + xprintk(dev , KERN_ERR , fmt , ## args)
12515 +#define WARN(dev,fmt,args...) \
12516 + xprintk(dev , KERN_WARNING , fmt , ## args)
12517 +#define INFO(dev,fmt,args...) \
12518 + xprintk(dev , KERN_INFO , fmt , ## args)
12521 +/*----------------------------------------------------------------------*/
12523 +/* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
12525 + * After opening, configure non-control endpoints. Then use normal
12526 + * stream read() and write() requests; and maybe ioctl() to get more
12527 + * precise FIFO status when recovering from cancelation.
12528 + */
12530 +static void epio_complete (struct usb_ep *ep, struct usb_request *req)
12532 + struct ep_data *epdata = ep->driver_data;
12534 + if (!req->context)
12535 + return;
12536 + if (req->status)
12537 + epdata->status = req->status;
12538 + else
12539 + epdata->status = req->actual;
12540 + complete ((struct completion *)req->context);
12543 +/* tasklock endpoint, returning when it's connected.
12544 + * still need dev->lock to use epdata->ep.
12545 + */
12546 +static int
12547 +get_ready_ep (unsigned f_flags, struct ep_data *epdata)
12549 + int val;
12551 + if (f_flags & O_NONBLOCK) {
12552 + if (down_trylock (&epdata->lock) != 0)
12553 + goto nonblock;
12554 + if (epdata->state != STATE_EP_ENABLED) {
12555 + up (&epdata->lock);
12556 +nonblock:
12557 + val = -EAGAIN;
12558 + } else
12559 + val = 0;
12560 + return val;
12563 + if ((val = down_interruptible (&epdata->lock)) < 0)
12564 + return val;
12565 +newstate:
12566 + switch (epdata->state) {
12567 + case STATE_EP_ENABLED:
12568 + break;
12569 + case STATE_EP_DEFER_ENABLE:
12570 + DBG (epdata->dev, "%s wait for host\n", epdata->name);
12571 + if ((val = wait_event_interruptible (epdata->wait,
12572 + epdata->state != STATE_EP_DEFER_ENABLE
12573 + || epdata->dev->state == STATE_DEV_UNBOUND
12574 + )) < 0)
12575 + goto fail;
12576 + goto newstate;
12577 + // case STATE_EP_DISABLED: /* "can't happen" */
12578 + // case STATE_EP_READY: /* "can't happen" */
12579 + default: /* error! */
12580 + pr_debug ("%s: ep %p not available, state %d\n",
12581 + shortname, epdata, epdata->state);
12582 + // FALLTHROUGH
12583 + case STATE_EP_UNBOUND: /* clean disconnect */
12584 + val = -ENODEV;
12585 +fail:
12586 + up (&epdata->lock);
12588 + return val;
12591 +static ssize_t
12592 +ep_io (struct ep_data *epdata, void *buf, unsigned len)
12594 + DECLARE_COMPLETION (done);
12595 + int value;
12597 + spin_lock_irq (&epdata->dev->lock);
12598 + if (likely (epdata->ep != NULL)) {
12599 + struct usb_request *req = epdata->req;
12601 + req->context = &done;
12602 + req->complete = epio_complete;
12603 + req->buf = buf;
12604 + req->length = len;
12605 + value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
12606 + } else
12607 + value = -ENODEV;
12608 + spin_unlock_irq (&epdata->dev->lock);
12610 + if (likely (value == 0)) {
12611 + value = wait_event_interruptible (done.wait, done.done);
12612 + if (value != 0) {
12613 + spin_lock_irq (&epdata->dev->lock);
12614 + if (likely (epdata->ep != NULL)) {
12615 + DBG (epdata->dev, "%s i/o interrupted\n",
12616 + epdata->name);
12617 + usb_ep_dequeue (epdata->ep, epdata->req);
12618 + spin_unlock_irq (&epdata->dev->lock);
12620 + wait_event (done.wait, done.done);
12621 + if (epdata->status == -ECONNRESET)
12622 + epdata->status = -EINTR;
12623 + } else {
12624 + spin_unlock_irq (&epdata->dev->lock);
12626 + DBG (epdata->dev, "endpoint gone\n");
12627 + epdata->status = -ENODEV;
12630 + return epdata->status;
12632 + return value;
12636 +/* handle a synchronous OUT bulk/intr/iso transfer */
12637 +static ssize_t
12638 +ep_read (struct file *fd, char *buf, size_t len, loff_t *ptr)
12640 + struct ep_data *data = fd->private_data;
12641 + void *kbuf;
12642 + ssize_t value;
12644 + if ((value = get_ready_ep (fd->f_flags, data)) < 0)
12645 + return value;
12647 + /* halt any endpoint by doing a "wrong direction" i/o call */
12648 + if (data->desc.bEndpointAddress & USB_DIR_IN) {
12649 + if ((data->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
12650 + == USB_ENDPOINT_XFER_ISOC)
12651 + return -EINVAL;
12652 + DBG (data->dev, "%s halt\n", data->name);
12653 + spin_lock_irq (&data->dev->lock);
12654 + if (likely (data->ep != NULL))
12655 + usb_ep_set_halt (data->ep);
12656 + spin_unlock_irq (&data->dev->lock);
12657 + up (&data->lock);
12658 + return -EBADMSG;
12661 + /* FIXME readahead for O_NONBLOCK and poll(); careful with ZLPs */
12663 + value = -ENOMEM;
12664 + kbuf = kmalloc (len, SLAB_KERNEL);
12665 + if (unlikely (!kbuf))
12666 + goto free1;
12668 + value = ep_io (data, kbuf, len);
12669 + VDEBUG (data->dev, "%s read %d OUT, status %d\n",
12670 + data->name, len, value);
12671 + if (value >= 0 && copy_to_user (buf, kbuf, value))
12672 + value = -EFAULT;
12674 +free1:
12675 + up (&data->lock);
12676 + kfree (kbuf);
12677 + return value;
12680 +/* handle a synchronous IN bulk/intr/iso transfer */
12681 +static ssize_t
12682 +ep_write (struct file *fd, const char *buf, size_t len, loff_t *ptr)
12684 + struct ep_data *data = fd->private_data;
12685 + void *kbuf;
12686 + ssize_t value;
12688 + if ((value = get_ready_ep (fd->f_flags, data)) < 0)
12689 + return value;
12691 + /* halt any endpoint by doing a "wrong direction" i/o call */
12692 + if (!(data->desc.bEndpointAddress & USB_DIR_IN)) {
12693 + if ((data->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
12694 + == USB_ENDPOINT_XFER_ISOC)
12695 + return -EINVAL;
12696 + DBG (data->dev, "%s halt\n", data->name);
12697 + spin_lock_irq (&data->dev->lock);
12698 + if (likely (data->ep != NULL))
12699 + usb_ep_set_halt (data->ep);
12700 + spin_unlock_irq (&data->dev->lock);
12701 + up (&data->lock);
12702 + return -EBADMSG;
12705 + /* FIXME writebehind for O_NONBLOCK and poll(), qlen = 1 */
12707 + value = -ENOMEM;
12708 + kbuf = kmalloc (len, SLAB_KERNEL);
12709 + if (!kbuf)
12710 + goto free1;
12711 + if (copy_from_user (kbuf, buf, len)) {
12712 + value = -EFAULT;
12713 + goto free1;
12716 + value = ep_io (data, kbuf, len);
12717 + VDEBUG (data->dev, "%s write %d IN, status %d\n",
12718 + data->name, len, value);
12719 +free1:
12720 + up (&data->lock);
12721 + kfree (kbuf);
12722 + return value;
12725 +static int
12726 +ep_release (struct inode *inode, struct file *fd)
12728 + struct ep_data *data = fd->private_data;
12730 + /* clean up if this can be reopened */
12731 + if (data->state != STATE_EP_UNBOUND) {
12732 + data->state = STATE_EP_DISABLED;
12733 + data->desc.bDescriptorType = 0;
12734 + data->hs_desc.bDescriptorType = 0;
12736 + put_ep (data);
12737 + return 0;
12740 +static int ep_ioctl (struct inode *inode, struct file *fd,
12741 + unsigned code, unsigned long value)
12743 + struct ep_data *data = fd->private_data;
12744 + int status;
12746 + if ((status = get_ready_ep (fd->f_flags, data)) < 0)
12747 + return status;
12749 + spin_lock_irq (&data->dev->lock);
12750 + if (likely (data->ep != NULL)) {
12751 + switch (code) {
12752 + case GADGETFS_FIFO_STATUS:
12753 + status = usb_ep_fifo_status (data->ep);
12754 + break;
12755 + case GADGETFS_FIFO_FLUSH:
12756 + usb_ep_fifo_flush (data->ep);
12757 + break;
12758 + case GADGETFS_CLEAR_HALT:
12759 + status = usb_ep_clear_halt (data->ep);
12760 + break;
12761 + default:
12762 + status = -ENOTTY;
12764 + } else
12765 + status = -ENODEV;
12766 + spin_unlock_irq (&data->dev->lock);
12767 + up (&data->lock);
12768 + return status;
12771 +/* used after endpoint configuration */
12772 +static struct file_operations ep_io_operations = {
12773 + .owner = THIS_MODULE,
12774 + .read = ep_read,
12775 + .write = ep_write,
12776 + .ioctl = ep_ioctl,
12777 + .release = ep_release,
12779 + // .aio_read = ep_aio_read,
12780 + // .aio_write = ep_aio_write,
12783 +/* ENDPOINT INITIALIZATION
12785 + * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
12786 + * status = write (fd, descriptors, sizeof descriptors)
12788 + * That write establishes the endpoint configuration, configuring
12789 + * the controller to process bulk, interrupt, or isochronous transfers
12790 + * at the right maxpacket size, and so on.
12792 + * The descriptors are message type 1, identified by a host order u32
12793 + * at the beginning of what's written. Descriptor order is: full/low
12794 + * speed descriptor, then optional high speed descriptor.
12795 + */
12796 +static ssize_t
12797 +ep_config (struct file *fd, const char *buf, size_t len, loff_t *ptr)
12799 + struct ep_data *data = fd->private_data;
12800 + struct usb_ep *ep;
12801 + u32 tag;
12802 + int value;
12804 + if ((value = down_interruptible (&data->lock)) < 0)
12805 + return value;
12807 + if (data->state != STATE_EP_READY) {
12808 + value = -EL2HLT;
12809 + goto fail;
12812 + value = len;
12813 + if (len < USB_DT_ENDPOINT_SIZE + 4)
12814 + goto fail0;
12816 + /* we might need to change message format someday */
12817 + if (copy_from_user (&tag, buf, 4)) {
12818 + goto fail1;
12820 + if (tag != 1) {
12821 + DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
12822 + goto fail0;
12824 + buf += 4;
12825 + len -= 4;
12827 + /* NOTE: audio endpoint extensions not accepted here;
12828 + * just don't include the extra bytes.
12829 + */
12831 + /* full/low speed descriptor, then high speed */
12832 + if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
12833 + goto fail1;
12835 + if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
12836 + || data->desc.bDescriptorType != USB_DT_ENDPOINT)
12837 + goto fail0;
12838 + if (len != USB_DT_ENDPOINT_SIZE) {
12839 + if (len != 2 * USB_DT_ENDPOINT_SIZE)
12840 + goto fail0;
12841 + if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
12842 + USB_DT_ENDPOINT_SIZE)) {
12843 + goto fail1;
12845 + if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
12846 + || data->hs_desc.bDescriptorType
12847 + != USB_DT_ENDPOINT) {
12848 + DBG(data->dev, "config %s, bad hs length or type\n",
12849 + data->name);
12850 + goto fail0;
12853 + value = len;
12855 + spin_lock_irq (&data->dev->lock);
12856 + if (data->dev->state == STATE_DEV_UNBOUND) {
12857 + value = -ENOENT;
12858 + goto gone;
12859 + } else if ((ep = data->ep) == NULL) {
12860 + value = -ENODEV;
12861 + goto gone;
12863 + switch (data->dev->gadget->speed) {
12864 + case USB_SPEED_LOW:
12865 + case USB_SPEED_FULL:
12866 + value = usb_ep_enable (ep, &data->desc);
12867 + if (value == 0)
12868 + data->state = STATE_EP_ENABLED;
12869 + break;
12870 +#ifdef HIGHSPEED
12871 + case USB_SPEED_HIGH:
12872 + /* fails if caller didn't provide that descriptor... */
12873 + value = usb_ep_enable (ep, &data->hs_desc);
12874 + if (value == 0)
12875 + data->state = STATE_EP_ENABLED;
12876 + break;
12877 +#endif
12878 + default:
12879 + DBG (data->dev, "unconnected, %s init deferred\n",
12880 + data->name);
12881 + data->state = STATE_EP_DEFER_ENABLE;
12883 + if (value == 0)
12884 + fd->f_op = &ep_io_operations;
12885 +gone:
12886 + spin_unlock_irq (&data->dev->lock);
12887 + if (value < 0) {
12888 +fail:
12889 + data->desc.bDescriptorType = 0;
12890 + data->hs_desc.bDescriptorType = 0;
12892 + up (&data->lock);
12893 + return value;
12894 +fail0:
12895 + value = -EINVAL;
12896 + goto fail;
12897 +fail1:
12898 + value = -EFAULT;
12899 + goto fail;
12902 +static int
12903 +ep_open (struct inode *inode, struct file *fd)
12905 + struct ep_data *data = inode->u.generic_ip;
12906 + int value = -EBUSY;
12908 + if (down_interruptible (&data->lock) != 0)
12909 + return -EINTR;
12910 + spin_lock_irq (&data->dev->lock);
12911 + if (data->dev->state == STATE_DEV_UNBOUND)
12912 + value = -ENOENT;
12913 + else if (data->state == STATE_EP_DISABLED) {
12914 + value = 0;
12915 + data->state = STATE_EP_READY;
12916 + get_ep (data);
12917 + fd->private_data = data;
12918 + VDEBUG (data->dev, "%s ready\n", data->name);
12919 + } else
12920 + DBG (data->dev, "%s state %d\n",
12921 + data->name, data->state);
12922 + spin_unlock_irq (&data->dev->lock);
12923 + up (&data->lock);
12924 + return value;
12927 +/* used before endpoint configuration */
12928 +static struct file_operations ep_config_operations = {
12929 + .owner = THIS_MODULE,
12930 + .open = ep_open,
12931 + .write = ep_config,
12932 + .release = ep_release,
12935 +/*----------------------------------------------------------------------*/
12937 +/* EP0 IMPLEMENTATION can be partly in userspace.
12939 + * Drivers that use this facility receive various events, including
12940 + * control requests the kernel doesn't handle. Drivers that don't
12941 + * use this facility may be too simple-minded for real applications.
12942 + */
12944 +static inline void ep0_readable (struct dev_data *dev)
12946 + wake_up (&dev->wait);
12947 + kill_fasync (&dev->fasync, SIGIO, POLL_IN);
12950 +static void clean_req (struct usb_ep *ep, struct usb_request *req)
12952 + struct dev_data *dev = ep->driver_data;
12954 + if (req->buf != dev->rbuf) {
12955 + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
12956 + req->buf = dev->rbuf;
12957 + req->dma = DMA_ADDR_INVALID;
12959 + req->complete = epio_complete;
12960 + dev->setup_out_ready = 0;
12963 +static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
12965 + struct dev_data *dev = ep->driver_data;
12966 + int free = 1;
12968 + /* for control OUT, data must still get to userspace */
12969 + if (!dev->setup_in) {
12970 + dev->setup_out_error = (req->status != 0);
12971 + if (!dev->setup_out_error)
12972 + free = 0;
12973 + dev->setup_out_ready = 1;
12974 + ep0_readable (dev);
12975 + } else if (dev->state == STATE_SETUP)
12976 + dev->state = STATE_CONNECTED;
12978 + /* clean up as appropriate */
12979 + if (free && req->buf != &dev->rbuf)
12980 + clean_req (ep, req);
12981 + req->complete = epio_complete;
12984 +static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
12986 + struct dev_data *dev = ep->driver_data;
12988 + if (dev->setup_out_ready) {
12989 + DBG (dev, "ep0 request busy!\n");
12990 + return -EBUSY;
12992 + if (len > sizeof (dev->rbuf))
12993 + req->buf = usb_ep_alloc_buffer (ep, len, &req->dma, GFP_ATOMIC);
12994 + if (req->buf == 0) {
12995 + req->buf = dev->rbuf;
12996 + return -ENOMEM;
12998 + req->complete = ep0_complete;
12999 + req->length = len;
13000 + return 0;
13003 +static ssize_t
13004 +ep0_read (struct file *fd, char *buf, size_t len, loff_t *ptr)
13006 + struct dev_data *dev = fd->private_data;
13007 + ssize_t retval;
13008 + enum ep0_state state;
13010 + spin_lock_irq (&dev->lock);
13012 + /* report fd mode change before acting on it */
13013 + if (dev->setup_abort) {
13014 + dev->setup_abort = 0;
13015 + retval = -EIDRM;
13016 + goto done;
13019 + /* control DATA stage */
13020 + if ((state = dev->state) == STATE_SETUP) {
13022 + if (dev->setup_in) { /* stall IN */
13023 + VDEBUG(dev, "ep0in stall\n");
13024 + (void) usb_ep_set_halt (dev->gadget->ep0);
13025 + retval = -EL2HLT;
13026 + dev->state = STATE_CONNECTED;
13028 + } else if (len == 0) { /* ack SET_CONFIGURATION etc */
13029 + struct usb_ep *ep = dev->gadget->ep0;
13030 + struct usb_request *req = dev->req;
13032 + if ((retval = setup_req (ep, req, 0)) == 0)
13033 + retval = usb_ep_queue (ep, req, GFP_ATOMIC);
13034 + dev->state = STATE_CONNECTED;
13036 + } else { /* collect OUT data */
13037 + if ((fd->f_flags & O_NONBLOCK) != 0
13038 + && !dev->setup_out_ready) {
13039 + retval = -EAGAIN;
13040 + goto done;
13042 + spin_unlock_irq (&dev->lock);
13043 + retval = wait_event_interruptible (dev->wait,
13044 + dev->setup_out_ready != 0);
13046 + /* FIXME state could change from under us */
13047 + spin_lock_irq (&dev->lock);
13048 + if (retval)
13049 + goto done;
13050 + if (dev->setup_out_error)
13051 + retval = -EIO;
13052 + else {
13053 + len = min (len, dev->req->actual);
13054 +// FIXME don't call this with the spinlock held ...
13055 + if (copy_to_user (buf, &dev->req->buf, len))
13056 + retval = -EFAULT;
13057 + clean_req (dev->gadget->ep0, dev->req);
13058 + /* NOTE userspace can't yet choose to stall */
13061 + goto done;
13064 + /* else normal: return event data */
13065 + if (len < sizeof dev->event [0]) {
13066 + retval = -EINVAL;
13067 + goto done;
13069 + len -= len % sizeof (struct usb_gadgetfs_event);
13070 + dev->usermode_setup = 1;
13072 +scan:
13073 + /* return queued events right away */
13074 + if (dev->ev_next != 0) {
13075 + unsigned i, n;
13076 + int tmp = dev->ev_next;
13078 + len = min (len, tmp * sizeof (struct usb_gadgetfs_event));
13079 + n = len / sizeof (struct usb_gadgetfs_event);
13081 + /* ep0 can't deliver events when STATE_SETUP */
13082 + for (i = 0; i < n; i++) {
13083 + if (dev->event [i].type == GADGETFS_SETUP) {
13084 + len = n = i + 1;
13085 + len *= sizeof (struct usb_gadgetfs_event);
13086 + n = 0;
13087 + break;
13090 + spin_unlock_irq (&dev->lock);
13091 + if (copy_to_user (buf, &dev->event, len))
13092 + retval = -EFAULT;
13093 + else
13094 + retval = len;
13095 + if (len > 0) {
13096 + len /= sizeof (struct usb_gadgetfs_event);
13098 + /* NOTE this doesn't guard against broken drivers;
13099 + * concurrent ep0 readers may lose events.
13100 + */
13101 + spin_lock_irq (&dev->lock);
13102 + dev->ev_next -= len;
13103 + if (dev->ev_next != 0)
13104 + memmove (&dev->event, &dev->event [len],
13105 + sizeof (struct usb_gadgetfs_event)
13106 + * (tmp - len));
13107 + if (n == 0)
13108 + dev->state = STATE_SETUP;
13109 + spin_unlock_irq (&dev->lock);
13111 + return retval;
13113 + if (fd->f_flags & O_NONBLOCK) {
13114 + retval = -EAGAIN;
13115 + goto done;
13118 + switch (state) {
13119 + default:
13120 + DBG (dev, "fail %s, state %d\n", __FUNCTION__, state);
13121 + retval = -ESRCH;
13122 + break;
13123 + case STATE_UNCONNECTED:
13124 + case STATE_CONNECTED:
13125 + spin_unlock_irq (&dev->lock);
13126 + DBG (dev, "%s wait\n", __FUNCTION__);
13128 + /* wait for events */
13129 + retval = wait_event_interruptible (dev->wait,
13130 + dev->ev_next != 0);
13131 + if (retval < 0)
13132 + return retval;
13133 + spin_lock_irq (&dev->lock);
13134 + goto scan;
13137 +done:
13138 + spin_unlock_irq (&dev->lock);
13139 + return retval;
13142 +static struct usb_gadgetfs_event *
13143 +next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
13145 + struct usb_gadgetfs_event *event;
13146 + unsigned i;
13148 + switch (type) {
13149 + /* these events purge the queue */
13150 + case GADGETFS_DISCONNECT:
13151 + if (dev->state == STATE_SETUP)
13152 + dev->setup_abort = 1;
13153 + // FALL THROUGH
13154 + case GADGETFS_CONNECT:
13155 + dev->ev_next = 0;
13156 + break;
13157 + case GADGETFS_SETUP: /* previous request timed out */
13158 + case GADGETFS_SUSPEND: /* same effect */
13159 + /* these events can't be repeated */
13160 + for (i = 0; i != dev->ev_next; i++) {
13161 + if (dev->event [i].type != type)
13162 + continue;
13163 + DBG (dev, "discard old event %d\n", type);
13164 + dev->ev_next--;
13165 + if (i == dev->ev_next)
13166 + break;
13167 + /* indices start at zero, for simplicity */
13168 + memmove (&dev->event [i], &dev->event [i + 1],
13169 + sizeof (struct usb_gadgetfs_event)
13170 + * (dev->ev_next - i));
13172 + break;
13173 + default:
13174 + BUG ();
13176 + event = &dev->event [dev->ev_next++];
13177 + BUG_ON (dev->ev_next > N_EVENT);
13178 + VDEBUG (dev, "ev %d, next %d\n", type, dev->ev_next);
13179 + memset (event, 0, sizeof *event);
13180 + event->type = type;
13181 + return event;
13184 +static ssize_t
13185 +ep0_write (struct file *fd, const char *buf, size_t len, loff_t *ptr)
13187 + struct dev_data *dev = fd->private_data;
13188 + ssize_t retval = -ESRCH;
13190 + spin_lock_irq (&dev->lock);
13192 + /* report fd mode change before acting on it */
13193 + if (dev->setup_abort) {
13194 + dev->setup_abort = 0;
13195 + retval = -EIDRM;
13197 + /* data and/or status stage for control request */
13198 + } else if (dev->state == STATE_SETUP) {
13200 + /* IN DATA+STATUS caller makes len <= wLength */
13201 + if (dev->setup_in) {
13202 + retval = setup_req (dev->gadget->ep0, dev->req, len);
13203 + if (retval == 0) {
13204 + spin_unlock_irq (&dev->lock);
13205 + if (copy_from_user (dev->req->buf, buf, len))
13206 + retval = -EFAULT;
13207 + else
13208 + retval = usb_ep_queue (
13209 + dev->gadget->ep0, dev->req,
13210 + GFP_KERNEL);
13211 + if (retval < 0) {
13212 + spin_lock_irq (&dev->lock);
13213 + clean_req (dev->gadget->ep0, dev->req);
13214 + spin_unlock_irq (&dev->lock);
13215 + } else
13216 + retval = len;
13218 + return retval;
13221 + /* can stall some OUT transfers */
13222 + } else if (dev->setup_can_stall) {
13223 + VDEBUG(dev, "ep0out stall\n");
13224 + (void) usb_ep_set_halt (dev->gadget->ep0);
13225 + retval = -EL2HLT;
13226 + dev->state = STATE_CONNECTED;
13227 + } else {
13228 + DBG(dev, "bogus ep0out stall!\n");
13230 + } else
13231 + DBG (dev, "fail %s, state %d\n", __FUNCTION__, dev->state);
13233 + spin_unlock_irq (&dev->lock);
13234 + return retval;
13237 +static int
13238 +ep0_fasync (int f, struct file *fd, int on)
13240 + struct dev_data *dev = fd->private_data;
13241 + // caller must F_SETOWN before signal delivery happens
13242 + VDEBUG (dev, "%s %s\n", __FUNCTION__, on ? "on" : "off");
13243 + return fasync_helper (f, fd, on, &dev->fasync);
13246 +static struct usb_gadget_driver gadgetfs_driver;
13248 +static int
13249 +dev_release (struct inode *inode, struct file *fd)
13251 + struct dev_data *dev = fd->private_data;
13253 + /* closing ep0 === shutdown all */
13255 + usb_gadget_unregister_driver (&gadgetfs_driver);
13257 + /* at this point "good" hardware has disconnected the
13258 + * device from USB; the host won't see it any more.
13259 + * alternatively, all host requests will time out.
13260 + */
13262 + fasync_helper (-1, fd, 0, &dev->fasync);
13263 + kfree (dev->buf);
13264 + dev->buf = 0;
13265 + put_dev (dev);
13267 + /* other endpoints were all decoupled from this device */
13268 + dev->state = STATE_DEV_DISABLED;
13269 + return 0;
13272 +static int dev_ioctl (struct inode *inode, struct file *fd,
13273 + unsigned code, unsigned long value)
13275 + struct dev_data *dev = fd->private_data;
13276 + struct usb_gadget *gadget = dev->gadget;
13278 + if (gadget->ops->ioctl)
13279 + return gadget->ops->ioctl (gadget, code, value);
13280 + return -ENOTTY;
13283 +/* used after device configuration */
13284 +static struct file_operations ep0_io_operations = {
13285 + .owner = THIS_MODULE,
13286 + .read = ep0_read,
13287 + .write = ep0_write,
13288 + .fasync = ep0_fasync,
13289 + // .poll = ep0_poll,
13290 + .ioctl = dev_ioctl,
13291 + .release = dev_release,
13294 +/*----------------------------------------------------------------------*/
13296 +/* The in-kernel gadget driver handles most ep0 issues, in particular
13297 + * enumerating the single configuration (as provided from user space).
13299 + * Unrecognized ep0 requests may be handled in user space.
13300 + */
13302 +#ifdef HIGHSPEED
13303 +static void make_qualifier (struct dev_data *dev)
13305 + struct usb_qualifier_descriptor qual;
13306 + struct usb_device_descriptor *desc;
13308 + qual.bLength = sizeof qual;
13309 + qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
13310 + qual.bcdUSB = __constant_cpu_to_le16 (0x0200);
13312 + desc = dev->dev;
13313 + qual.bDeviceClass = desc->bDeviceClass;
13314 + qual.bDeviceSubClass = desc->bDeviceSubClass;
13315 + qual.bDeviceProtocol = desc->bDeviceProtocol;
13317 + /* assumes ep0 uses the same value for both speeds ... */
13318 + qual.bMaxPacketSize0 = desc->bMaxPacketSize0;
13320 + qual.bNumConfigurations = 1;
13321 + qual.bRESERVED = 0;
13323 + memcpy (dev->rbuf, &qual, sizeof qual);
13325 +#endif
13327 +static int
13328 +config_buf (struct dev_data *dev, u8 type, unsigned index)
13330 + int len;
13331 +#ifdef HIGHSPEED
13332 + int hs;
13333 +#endif
13335 + /* only one configuration */
13336 + if (index > 0)
13337 + return -EINVAL;
13339 +#ifdef HIGHSPEED
13340 + hs = (dev->gadget->speed == USB_SPEED_HIGH);
13341 + if (type == USB_DT_OTHER_SPEED_CONFIG)
13342 + hs = !hs;
13343 + if (hs) {
13344 + dev->req->buf = dev->hs_config;
13345 + len = le16_to_cpup (&dev->hs_config->wTotalLength);
13346 + } else
13347 +#endif
13349 + dev->req->buf = dev->config;
13350 + len = le16_to_cpup (&dev->config->wTotalLength);
13352 + ((u8 *)dev->req->buf) [1] = type;
13353 + return len;
13356 +static int
13357 +gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
13359 + struct dev_data *dev = get_gadget_data (gadget);
13360 + struct usb_request *req = dev->req;
13361 + int value = -EOPNOTSUPP;
13362 + struct usb_gadgetfs_event *event;
13364 + spin_lock (&dev->lock);
13365 + dev->setup_abort = 0;
13366 + if (dev->state == STATE_UNCONNECTED) {
13367 + struct usb_ep *ep;
13368 + struct ep_data *data;
13370 + dev->state = STATE_CONNECTED;
13371 + dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
13373 +#ifdef HIGHSPEED
13374 + if (gadget->speed == USB_SPEED_HIGH && dev->hs_config == 0) {
13375 + ERROR (dev, "no high speed config??\n");
13376 + return -EINVAL;
13378 +#endif /* HIGHSPEED */
13380 + INFO (dev, "connected\n");
13381 + event = next_event (dev, GADGETFS_CONNECT);
13382 + event->u.speed = gadget->speed;
13383 + ep0_readable (dev);
13385 + list_for_each_entry (ep, &gadget->ep_list, ep_list) {
13386 + data = ep->driver_data;
13387 + /* ... down_trylock (&data->lock) ... */
13388 + if (data->state != STATE_EP_DEFER_ENABLE)
13389 + continue;
13390 +#ifdef HIGHSPEED
13391 + if (gadget->speed == USB_SPEED_HIGH)
13392 + value = usb_ep_enable (ep, &data->hs_desc);
13393 + else
13394 +#endif /* HIGHSPEED */
13395 + value = usb_ep_enable (ep, &data->desc);
13396 + if (value) {
13397 + ERROR (dev, "deferred %s enable --> %d\n",
13398 + data->name, value);
13399 + continue;
13401 + data->state = STATE_EP_ENABLED;
13402 + wake_up (&data->wait);
13403 + DBG (dev, "woke up %s waiters\n", data->name);
13406 + /* host may have given up waiting for response. we can miss control
13407 + * requests handled lower down (device/endpoint status and features);
13408 + * then ep0_{read,write} will report the wrong status. controller
13409 + * driver will have aborted pending i/o.
13410 + */
13411 + } else if (dev->state == STATE_SETUP)
13412 + dev->setup_abort = 1;
13414 + req->buf = dev->rbuf;
13415 + req->dma = DMA_ADDR_INVALID;
13416 + req->context = 0;
13417 + value = -EOPNOTSUPP;
13418 + switch (ctrl->bRequest) {
13420 + case USB_REQ_GET_DESCRIPTOR:
13421 + if (ctrl->bRequestType != USB_DIR_IN)
13422 + goto unrecognized;
13423 + switch (ctrl->wValue >> 8) {
13425 + case USB_DT_DEVICE:
13426 + value = min (ctrl->wLength, (u16) sizeof *dev->dev);
13427 + req->buf = dev->dev;
13428 + break;
13429 +#ifdef HIGHSPEED
13430 + case USB_DT_DEVICE_QUALIFIER:
13431 + if (!dev->hs_config)
13432 + break;
13433 + value = min (ctrl->wLength, (u16)
13434 + sizeof (struct usb_qualifier_descriptor));
13435 + make_qualifier (dev);
13436 + break;
13437 + case USB_DT_OTHER_SPEED_CONFIG:
13438 + // FALLTHROUGH
13439 +#endif
13440 + case USB_DT_CONFIG:
13441 + value = config_buf (dev,
13442 + ctrl->wValue >> 8,
13443 + ctrl->wValue & 0xff);
13444 + if (value >= 0)
13445 + value = min (ctrl->wLength, (u16) value);
13446 + break;
13447 + case USB_DT_STRING:
13448 + goto unrecognized;
13450 + default: // all others are errors
13451 + break;
13453 + break;
13455 + /* currently one config, two speeds */
13456 + case USB_REQ_SET_CONFIGURATION:
13457 + if (ctrl->bRequestType != 0)
13458 + break;
13459 + if (0 == (u8) ctrl->wValue) {
13460 + value = 0;
13461 + dev->current_config = 0;
13462 + // user mode expected to disable endpoints
13463 + } else {
13464 + u8 config;
13465 +#ifdef HIGHSPEED
13466 + if (gadget->speed == USB_SPEED_HIGH)
13467 + config = dev->hs_config->bConfigurationValue;
13468 + else
13469 +#endif
13470 + config = dev->config->bConfigurationValue;
13472 + if (config == (u8) ctrl->wValue) {
13473 + value = 0;
13474 + dev->current_config = config;
13478 + /* report SET_CONFIGURATION like any other control request,
13479 + * except that usermode may not stall this. the next
13480 + * request mustn't be allowed start until this finishes:
13481 + * endpoints and threads set up, etc.
13483 + * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
13484 + * has bad/racey automagic that prevents synchronizing here.
13485 + * even kernel mode drivers often miss them.
13486 + */
13487 + if (value == 0) {
13488 + INFO (dev, "configuration #%d\n", dev->current_config);
13489 + if (dev->usermode_setup) {
13490 + dev->setup_can_stall = 0;
13491 + goto delegate;
13494 + break;
13496 +#ifndef CONFIG_USB_GADGETFS_PXA2XX
13497 + /* PXA automagically handles this request too */
13498 + case USB_REQ_GET_CONFIGURATION:
13499 + if (ctrl->bRequestType != 0x80)
13500 + break;
13501 + *(u8 *)req->buf = dev->current_config;
13502 + value = min (ctrl->wLength, (u16) 1);
13503 + break;
13504 +#endif
13506 + default:
13507 +unrecognized:
13508 + VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
13509 + dev->usermode_setup ? "delegate" : "fail",
13510 + ctrl->bRequestType, ctrl->bRequest,
13511 + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
13513 + /* if there's an ep0 reader, don't stall */
13514 + if (dev->usermode_setup) {
13515 + dev->setup_can_stall = 1;
13516 +delegate:
13517 + dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
13518 + ? 1 : 0;
13519 + dev->setup_out_ready = 0;
13520 + dev->setup_out_error = 0;
13521 + value = 0;
13523 + /* read DATA stage for OUT right away */
13524 + if (unlikely (!dev->setup_in && ctrl->wLength)) {
13525 + value = setup_req (gadget->ep0, dev->req,
13526 + ctrl->wLength);
13527 + if (value < 0)
13528 + break;
13529 + value = usb_ep_queue (gadget->ep0, dev->req,
13530 + GFP_ATOMIC);
13531 + if (value < 0) {
13532 + clean_req (gadget->ep0, dev->req);
13533 + break;
13536 + /* we can't currently stall these */
13537 + dev->setup_can_stall = 0;
13540 + /* state changes when reader collects event */
13541 + event = next_event (dev, GADGETFS_SETUP);
13542 + event->u.setup = *ctrl;
13543 + ep0_readable (dev);
13544 + spin_unlock (&dev->lock);
13545 + return 0;
13549 + /* proceed with data transfer and status phases? */
13550 + if (value >= 0 && dev->state != STATE_SETUP) {
13551 + req->length = value;
13552 + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
13553 + if (value < 0) {
13554 + DBG (dev, "ep_queue --> %d\n", value);
13555 + req->status = 0;
13559 + /* device stalls when value < 0 */
13560 + spin_unlock (&dev->lock);
13561 + return value;
13564 +static void destroy_ep_files (struct dev_data *dev)
13566 + struct list_head *entry, *tmp;
13568 + DBG (dev, "%s %d\n", __FUNCTION__, dev->state);
13570 + /* dev->state must prevent interference */
13571 +restart:
13572 + spin_lock_irq (&dev->lock);
13573 + list_for_each_safe (entry, tmp, &dev->epfiles) {
13574 + struct ep_data *ep;
13576 + /* break link to FS */
13577 + ep = list_entry (entry, struct ep_data, epfiles);
13578 + list_del_init (&ep->epfiles);
13580 + /* break link to controller */
13581 + if (ep->state == STATE_EP_ENABLED)
13582 + (void) usb_ep_disable (ep->ep);
13583 + ep->state = STATE_EP_UNBOUND;
13584 + usb_ep_free_request (ep->ep, ep->req);
13585 + ep->ep = 0;
13586 + wake_up (&ep->wait);
13587 + put_ep (ep);
13589 + spin_unlock_irq (&dev->lock);
13591 + /* fds may still be open */
13592 + goto restart;
13594 + spin_unlock_irq (&dev->lock);
13598 +static int activate_ep_files (struct dev_data *dev)
13600 + struct usb_ep *ep;
13602 + gadget_for_each_ep (ep, dev->gadget) {
13603 + struct ep_data *data;
13605 + data = kmalloc (sizeof *data, GFP_KERNEL);
13606 + if (!data)
13607 + goto enomem;
13608 + memset (data, 0, sizeof data);
13609 + data->state = STATE_EP_DISABLED;
13610 + init_MUTEX (&data->lock);
13611 + init_waitqueue_head (&data->wait);
13613 + strncpy (data->name, ep->name, sizeof (data->name) - 1);
13614 + atomic_set (&data->count, 1);
13615 + data->dev = dev;
13616 + get_dev (dev);
13618 + data->ep = ep;
13619 + ep->driver_data = data;
13621 + data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
13622 + if (!data->req)
13623 + goto enomem;
13625 + list_add_tail (&data->epfiles, &dev->epfiles);
13627 + return 0;
13629 +enomem:
13630 + DBG (dev, "%s enomem\n", __FUNCTION__);
13631 + destroy_ep_files (dev);
13632 + return -ENOMEM;
13635 +static void
13636 +gadgetfs_unbind (struct usb_gadget *gadget)
13638 + struct dev_data *dev = get_gadget_data (gadget);
13640 + DBG (dev, "%s\n", __FUNCTION__);
13642 + spin_lock_irq (&dev->lock);
13643 + dev->state = STATE_DEV_UNBOUND;
13644 + spin_unlock_irq (&dev->lock);
13646 + destroy_ep_files (dev);
13647 + gadget->ep0->driver_data = 0;
13648 + set_gadget_data (gadget, 0);
13650 + /* we've already been disconnected ... no i/o is active */
13651 + if (dev->req)
13652 + usb_ep_free_request (gadget->ep0, dev->req);
13653 + DBG (dev, "%s done\n", __FUNCTION__);
13654 + put_dev (dev);
13657 +static struct dev_data *the_device;
13659 +static int
13660 +gadgetfs_bind (struct usb_gadget *gadget)
13662 + struct dev_data *dev = the_device;
13664 + if (!dev)
13665 + return -ESRCH;
13666 + if (0 != strcmp (CHIP, gadget->name)) {
13667 + printk (KERN_ERR "%s expected " CHIP " controller not %s\n",
13668 + shortname, gadget->name);
13669 + return -ENODEV;
13672 + set_gadget_data (gadget, dev);
13673 + dev->gadget = gadget;
13674 + gadget->ep0->driver_data = dev;
13675 + dev->dev->bMaxPacketSize0 = gadget->ep0->maxpacket;
13677 + /* preallocate control response and buffer */
13678 + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
13679 + if (!dev->req)
13680 + goto enomem;
13681 + dev->req->context = 0;
13682 + dev->req->complete = epio_complete;
13684 + if (activate_ep_files (dev) < 0)
13685 + goto enomem;
13687 + INFO (dev, "bound to %s driver\n", gadget->name);
13688 + dev->state = STATE_UNCONNECTED;
13689 + get_dev (dev);
13690 + return 0;
13692 +enomem:
13693 + gadgetfs_unbind (gadget);
13694 + return -ENOMEM;
13697 +static void
13698 +gadgetfs_disconnect (struct usb_gadget *gadget)
13700 + struct dev_data *dev = get_gadget_data (gadget);
13702 + if (dev->state == STATE_UNCONNECTED) {
13703 + DBG (dev, "already unconnected\n");
13704 + return;
13706 + dev->state = STATE_UNCONNECTED;
13708 + INFO (dev, "disconnected\n");
13709 + spin_lock (&dev->lock);
13710 + next_event (dev, GADGETFS_DISCONNECT);
13711 + ep0_readable (dev);
13712 + spin_unlock (&dev->lock);
13715 +static void
13716 +gadgetfs_suspend (struct usb_gadget *gadget)
13718 + struct dev_data *dev = get_gadget_data (gadget);
13720 + INFO (dev, "suspended from state %d\n", dev->state);
13721 + spin_lock (&dev->lock);
13722 + switch (dev->state) {
13723 + case STATE_SETUP: // VERY odd... host died??
13724 + case STATE_CONNECTED:
13725 + case STATE_UNCONNECTED:
13726 + next_event (dev, GADGETFS_SUSPEND);
13727 + ep0_readable (dev);
13728 + /* FALLTHROUGH */
13729 + default:
13730 + break;
13732 + spin_unlock (&dev->lock);
13735 +static struct usb_gadget_driver gadgetfs_driver = {
13736 +#ifdef HIGHSPEED
13737 + .speed = USB_SPEED_HIGH,
13738 +#else
13739 + .speed = USB_SPEED_FULL,
13740 +#endif
13741 + .function = (char *) driver_desc,
13742 + .bind = gadgetfs_bind,
13743 + .unbind = gadgetfs_unbind,
13744 + .setup = gadgetfs_setup,
13745 + .disconnect = gadgetfs_disconnect,
13746 + .suspend = gadgetfs_suspend,
13748 + .driver = {
13749 + .name = (char *) shortname,
13750 + // .shutdown = ...
13751 + // .suspend = ...
13752 + // .resume = ...
13753 + },
13756 +/*----------------------------------------------------------------------*/
13758 +/* DEVICE INITIALIZATION
13760 + * fd = open ("/dev/gadget/$CHIP", O_RDWR)
13761 + * status = write (fd, descriptors, sizeof descriptors)
13763 + * That write establishes the device configuration, so the kernel can
13764 + * bind to the controller ... guaranteeing it can handle enumeration
13765 + * at all necessary speeds. Descriptor order is:
13767 + * . message tag (u32, host order) ... for now, must be zero; it
13768 + * would change to support features like multi-config devices
13769 + * . full/low speed config ... all wTotalLength bytes (with interface,
13770 + * class, altsetting, endpoint, and other descriptors)
13771 + * . high speed config ... all descriptors, for high speed operation;
13772 + * this one's optional except for high-speed hardware
13773 + * . device descriptor
13775 + * Endpoints are not yet enabled. Drivers may want to immediately
13776 + * initialize them, using the /dev/gadget/ep* files that are available
13777 + * as soon as the kernel sees the configuration, or they can wait
13778 + * until device configuration and interface altsetting changes create
13779 + * the need to configure (or unconfigure) them.
13781 + * After initialization, the device stays active for as long as that
13782 + * $CHIP file is open. Events may then be read from that descriptor,
13783 + * such configuration notifications. More complex drivers will handle
13784 + * some control requests in user space.
13785 + */
13787 +static int is_valid_config (struct usb_config_descriptor *config)
13789 + return config->bDescriptorType == USB_DT_CONFIG
13790 + && config->bLength == USB_DT_CONFIG_SIZE
13791 + && config->bConfigurationValue != 0
13792 + && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
13793 + && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
13794 + /* FIXME check lengths: walk to end */
13797 +static ssize_t
13798 +dev_config (struct file *fd, const char *buf, size_t len, loff_t *ptr)
13800 + struct dev_data *dev = fd->private_data;
13801 + ssize_t value = len, length = len;
13802 + unsigned total;
13803 + u32 tag;
13804 + char *kbuf;
13806 + if (dev->state != STATE_OPENED)
13807 + return -EEXIST;
13809 + if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
13810 + return -EINVAL;
13812 + /* we might need to change message format someday */
13813 + if (copy_from_user (&tag, buf, 4))
13814 + return -EFAULT;
13815 + if (tag != 0)
13816 + return -EINVAL;
13817 + buf += 4;
13818 + length -= 4;
13820 + kbuf = kmalloc (length, SLAB_KERNEL);
13821 + if (!kbuf)
13822 + return -ENOMEM;
13823 + if (copy_from_user (kbuf, buf, length)) {
13824 + kfree (kbuf);
13825 + return -EFAULT;
13828 + spin_lock_irq (&dev->lock);
13829 + value = -EINVAL;
13830 + if (dev->buf)
13831 + goto fail;
13832 + dev->buf = kbuf;
13834 + /* full or low speed config */
13835 + dev->config = (void *) kbuf;
13836 + total = le16_to_cpup (&dev->config->wTotalLength);
13837 + if (!is_valid_config (dev->config) || total >= length)
13838 + goto fail;
13839 + kbuf += total;
13840 + length -= total;
13842 + /* optional high speed config */
13843 + if (kbuf [1] == USB_DT_CONFIG) {
13844 + dev->hs_config = (void *) kbuf;
13845 + total = le16_to_cpup (&dev->hs_config->wTotalLength);
13846 + if (!is_valid_config (dev->hs_config) || total >= length)
13847 + goto fail;
13848 + kbuf += total;
13849 + length -= total;
13852 + /* could support multiple configs, using another encoding! */
13854 + /* device descriptor (tweaked for paranoia) */
13855 + if (length != USB_DT_DEVICE_SIZE)
13856 + goto fail;
13857 + dev->dev = (void *)kbuf;
13858 + if (dev->dev->bLength != USB_DT_DEVICE_SIZE
13859 + || dev->dev->bDescriptorType != USB_DT_DEVICE
13860 + || dev->dev->bNumConfigurations != 1)
13861 + goto fail;
13862 + dev->dev->bNumConfigurations = 1;
13863 + dev->dev->bcdUSB = __constant_cpu_to_le16 (0x0200);
13865 + /* triggers gadgetfs_bind(); then we can enumerate. */
13866 + spin_unlock_irq (&dev->lock);
13867 + value = usb_gadget_register_driver (&gadgetfs_driver);
13868 + if (value != 0) {
13869 + kfree (dev->buf);
13870 + dev->buf = 0;
13871 + } else {
13872 + /* at this point "good" hardware has for the first time
13873 + * let the USB the host see us. alternatively, if users
13874 + * unplug/replug that will clear all the error state.
13876 + * note: everything running before here was guaranteed
13877 + * to choke driver model style diagnostics. from here
13878 + * on, they can work ... except in cleanup paths that
13879 + * kick in after the ep0 descriptor is closed.
13880 + */
13881 + fd->f_op = &ep0_io_operations;
13882 + value = len;
13884 + return value;
13886 +fail:
13887 + spin_unlock_irq (&dev->lock);
13888 + pr_debug ("%s: %s fail %d, %p\n", shortname, __FUNCTION__, value, dev);
13889 + kfree (dev->buf);
13890 + dev->buf = 0;
13891 + return value;
13894 +static int
13895 +dev_open (struct inode *inode, struct file *fd)
13897 + struct dev_data *dev = inode->u.generic_ip;
13898 + int value = -EBUSY;
13900 + if (dev->state == STATE_DEV_DISABLED) {
13901 + dev->ev_next = 0;
13902 + dev->state = STATE_OPENED;
13903 + fd->private_data = dev;
13904 + get_dev (dev);
13905 + value = 0;
13907 + return value;
13910 +static struct file_operations dev_init_operations = {
13911 + .owner = THIS_MODULE,
13912 + .open = dev_open,
13913 + .write = dev_config,
13914 + .fasync = ep0_fasync,
13915 + .ioctl = dev_ioctl,
13916 + .release = dev_release,
13919 +/*----------------------------------------------------------------------*/
13922 + * implementation for 2.4 uses character special files
13923 + * ep0/device file MKDEV (c_major, 0)
13924 + * first data ep MKDEV (c_major, 1)
13925 + * second data ep MKDEV (c_major, 2)
13926 + * ...
13928 + * FIXME can do it as a real filesystem on 2.4 too, without libfs
13929 + */
13930 +static int c_major = 240; /* 240 is local/experimental */
13931 +MODULE_PARM (c_major, "i");
13932 +MODULE_PARM_DESC (c_major, "major number for char special files");
13934 +static int gadget_open (struct inode *ino, struct file *fp)
13936 + int num = minor (ino->i_rdev);
13937 + struct dev_data *dev;
13938 + struct file_operations *ops;
13940 + /* ep0 file, "/dev/gadget/$CHIP" */
13941 + if (num == 0) {
13942 + int status;
13944 + if (the_device != 0)
13945 + return -EBUSY;
13946 + the_device = dev_new ();
13947 + if (the_device == 0)
13948 + return -ENOMEM;
13950 + dev = the_device;
13951 + ino->u.generic_ip = dev;
13952 + ops = &dev_init_operations;
13953 + fp->f_op = ops;
13955 + status = ops->open (ino, fp);
13956 + if (status < 0) {
13957 + put_dev (dev);
13958 + the_device = 0;
13960 + return status;
13962 + /* ep files, "/dev/gadget/$ENDPOINT" */
13963 + } else {
13964 + struct list_head *entry;
13965 + struct ep_data *data;
13967 + /* unavailable till device is initted */
13968 + dev = the_device;
13969 + if (dev == 0)
13970 + return -ENODEV;
13972 + /* order in controller's name listing matters! */
13973 + list_for_each (entry, &dev->epfiles) {
13974 + if (--num == 0)
13975 + goto found;
13977 + return -ENODEV;
13978 +found:
13979 + data = list_entry (entry, struct ep_data, epfiles);
13980 + ino->u.generic_ip = data;
13981 + ops = &ep_config_operations;
13982 + fp->f_op = ops;
13984 + return ops->open (ino, fp);
13988 +static struct file_operations gadget_fops = {
13989 + .owner = THIS_MODULE,
13990 + .open = gadget_open,
13993 +/*----------------------------------------------------------------------*/
13995 +static int __init init (void)
13997 + int status;
13999 + status = register_chrdev (c_major, shortname, &gadget_fops);
14000 + if (status < 0) {
14001 + printk (KERN_WARNING "%s: can't get major %d\n",
14002 + shortname, c_major);
14003 + return status;
14006 + /* dynamic assignment */
14007 + if (c_major == 0)
14008 + c_major = status;
14009 + status = 0;
14011 + pr_info ("%s: using char major %d\n", shortname, c_major);
14013 + if (status == 0)
14014 + pr_info ("%s: %s, version " DRIVER_VERSION "\n",
14015 + shortname, driver_desc);
14016 + return status;
14018 +module_init (init);
14020 +static void __exit cleanup (void)
14022 + pr_debug ("unregister %s\n", shortname);
14023 + unregister_chrdev (c_major, shortname);
14025 +module_exit (cleanup);
14027 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/n9604.c kernel/drivers/usb/gadget/n9604.c
14028 --- /tmp/kernel/drivers/usb/gadget/n9604.c 1970-01-01 01:00:00.000000000 +0100
14029 +++ kernel/drivers/usb/gadget/n9604.c 2005-04-22 17:53:19.461535120 +0200
14030 @@ -0,0 +1,1088 @@
14032 + * National 9603/4 USB Device Controller driver
14033 + * Copyright (C) 2004 Technical Solutions Inc. (support@techsol.ca)
14034 + * ported from : The Goku-S driver
14035 + * Copyright (C) 2003 MontaVista Software (source@mvista.com)
14037 + * This file is licensed under the terms of the GNU General Public
14038 + * License version 2. This program is licensed "as is" without any
14039 + * warranty of any kind, whether express or implied.
14040 + */
14043 + * This device has ep0 and six semi-configurable bulk/interrupt endpoints.
14045 + * - Endpoint numbering is fixed:
14046 + * Endpoint 0: ep0
14047 + * Endpoint 1: ep1in (tx)
14048 + * Endpoint 2: ep2out (rx)
14049 + * Endpoint 3: ep3in (tx)
14050 + * Endpoint 4: ep4out (rx)
14051 + * Endpoint 5: ep5in (tx)
14052 + * Endpoint 6: ep6out (rx)
14053 + */
14056 + * The ep->stage information refers to the state of a setup transaction
14058 + * state 0: no setup packet has been received
14059 + * state 1: setup packet has been received
14060 + * state 2: data has been sent/received
14061 + * state 3: ZLP has been received/sent
14062 + */
14064 +#include <linux/config.h>
14065 +#include <linux/kernel.h>
14066 +#include <linux/module.h>
14067 +#include <linux/delay.h>
14068 +#include <linux/ioport.h>
14069 +#include <linux/sched.h>
14070 +#include <linux/slab.h>
14071 +#include <linux/smp_lock.h>
14072 +#include <linux/errno.h>
14073 +#include <linux/init.h>
14074 +#include <linux/timer.h>
14075 +#include <linux/list.h>
14076 +#include <linux/interrupt.h>
14077 +#include <linux/usb_ch9.h>
14078 +#include <linux/usb_gadget.h>
14080 +#include <asm/byteorder.h>
14081 +#include <asm/io.h>
14082 +#include <asm/irq.h>
14083 +#include <asm/system.h>
14084 +#include <asm/unaligned.h>
14086 +#include "n9604.h"
14087 +#include "n9604regs.h"
14089 +inline void Flush_and_enable(u8 control_reg) {
14090 + write_9604(RXC_FLUSH, control_reg);
14091 + while (read_9604(control_reg) & RXC_FLUSH);
14092 + write_9604(RXC_RX_EN, control_reg);
14094 +inline void Flush(u8 control_reg) {
14095 + write_9604(RXC_FLUSH, control_reg);
14096 + while (read_9604(control_reg) & RXC_FLUSH);
14099 +#define DRIVER_DESC "N9604 USB Device Controller"
14100 +#define DRIVER_VERSION "29-Oct 2004"
14102 +static const char driver_name [] = "n9604_udc";
14103 +static const char driver_desc [] = DRIVER_DESC;
14105 +MODULE_AUTHOR("support@techsol.ca");
14106 +MODULE_DESCRIPTION(DRIVER_DESC);
14107 +MODULE_LICENSE("GPL");
14109 +static void nuke(struct n9604_ep *ep, int status);
14110 +inline void send_zero_length(int endpoint, struct n9604_udc *dev);
14112 +u8 * USBN9604_Offset; //device virtual address
14114 +/* FIXME all the IRQ stuff is board-specific
14115 + */
14117 +#define h7201_readl readl
14118 +#define h7201_writel writel
14120 +#define ETHER_IRQ_IP_OFFSET 0
14121 +#define ETHER_IRQ_BIT_POS 0
14122 +#define ETHER_IRQ_IM_OFFSET 0
14124 +#define IRQ_GPIOC -1
14126 +#define USBD_ENABLE_IRQ {h7201_writel( h7201_readl(ETHER_IRQ_IP_OFFSET) | (1 << ETHER_IRQ_BIT_POS), ETHER_IRQ_IP_OFFSET); h7201_writel( h7201_readl(ETHER_IRQ_IM_OFFSET) | (1 << ETHER_IRQ_BIT_POS), ETHER_IRQ_IM_OFFSET);}
14127 +#define USBD_DISABLE_IRQ h7201_writel( h7201_readl(ETHER_IRQ_IM_OFFSET) & ~(1 << ETHER_IRQ_BIT_POS), ETHER_IRQ_IM_OFFSET);
14130 +/*-------------------------------------------------------------------------*/
14132 +//enable an end point, of description desc
14133 +static int n9604_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc) {
14134 + struct n9604_udc *dev;
14135 + struct n9604_ep *ep;
14136 + u16 max;
14138 + ep = container_of(_ep, struct n9604_ep, ep);
14140 + if (!_ep || !desc || ep->desc || desc->bDescriptorType != USB_DT_ENDPOINT)
14141 + return -EINVAL;
14143 + dev = ep->dev;
14144 + if (!ep->num)
14145 + return -EINVAL;
14146 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
14147 + return -ESHUTDOWN;
14148 + if (ep->num && !(desc->bEndpointAddress & 0x0f))
14149 + return -EINVAL;
14151 + switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
14152 + case USB_ENDPOINT_XFER_BULK:
14153 + case USB_ENDPOINT_XFER_INT:
14154 + break;
14155 + default:
14156 + return -EINVAL;
14159 + write_9604((ep->numActual & EPC_EP_MASK) | EPC_EP_EN | (EPC_ISO * ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_ISOC)), ep->control);
14160 + if (ep->is_in)
14161 + Flush(ep->command);
14162 + else
14163 + Flush_and_enable(ep->command);
14165 + max = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
14166 + ep->ep.maxpacket = min_t(u16, max, MAX_FIFO_SIZE);
14167 + ep->desc = desc;
14169 + return 0;
14172 +static int n9604_ep_disable(struct usb_ep *_ep)//ep > 0
14174 + struct n9604_ep *ep;
14175 + struct n9604_udc *dev;
14176 + unsigned long flags;
14178 + ep = container_of(_ep, struct n9604_ep, ep);
14180 + if (!_ep || !ep->desc)
14181 + return -ENODEV;
14182 + dev = ep->dev;
14184 + spin_lock_irqsave(&dev->lock, flags);
14185 + nuke(ep, -ESHUTDOWN);
14186 + write_9604(0, ep->command);
14187 + ep->desc = NULL;
14188 + spin_unlock_irqrestore(&dev->lock, flags);
14190 + return 0;
14193 +/*-------------------------------------------------------------------------*/
14195 +static struct usb_request *
14196 +n9604_alloc_request(struct usb_ep *_ep, int gfp_flags)
14198 + struct n9604_request *req;
14200 + if (!_ep)
14201 + return 0;
14202 + req = kmalloc(sizeof *req, gfp_flags);
14203 + if (!req)
14204 + return 0;
14206 + memset(req, 0, sizeof *req);
14207 + INIT_LIST_HEAD(&req->queue);
14208 + return &req->req;
14211 +static void
14212 +n9604_free_request(struct usb_ep *_ep, struct usb_request *_req)
14214 + struct n9604_request *req;
14216 + if (!_ep || !_req)
14217 + return;
14219 + req = container_of(_req, struct n9604_request, req);
14220 + WARN_ON(!list_empty(&req->queue));
14221 + kfree(req);
14224 +/*-------------------------------------------------------------------------*/
14226 +static void done(struct n9604_ep *ep, struct n9604_request *req, int status);
14228 +static inline int
14229 +write_packet(struct n9604_ep *ep, u8 *buf, struct n9604_request *req)
14231 + unsigned written_length, desired_length, available_length, maximum_length, flags, loop_length;
14233 + u8 fifo = ep->fifo;
14234 + u8 command = ep->command;
14235 + u8 status = ep->status;
14236 + if (!ep->num) {
14237 + fifo = TXD0;
14238 + command = TXC0;
14239 + status = TXS0;
14242 + if (read_9604(command) & TXC_TX_EN)
14243 + return -EBUSY;
14244 + ep->packets++;
14246 + desired_length = req->req.length - req->req.actual;
14247 + available_length = read_9604(status) & TXS_TCOUNT_MASK;//might be greater
14248 + written_length = 0;
14249 + if (ep->num)
14250 + maximum_length = MAX_FIFO_SIZE;
14251 + else
14252 + maximum_length = MAX_EP0_SIZE;
14254 + while ((loop_length = min(min(available_length, desired_length), maximum_length))) {
14255 + int i = loop_length;
14256 + while (i) { write_9604(*buf++,fifo); i--; }
14257 + written_length += loop_length;
14258 + desired_length -= loop_length;
14259 + maximum_length -= loop_length;
14260 + if (desired_length && maximum_length)//check if really need to read the chip again
14261 + available_length = (read_9604(status) & TXS_TCOUNT_MASK);
14264 + req->req.actual += written_length;
14266 + flags = TXC_TX_EN;
14267 + if (ep->num)
14268 + flags |= TXC_LAST;
14269 + if (ep->toggle)
14270 + flags |= TXC_TOGGLE;
14271 + write_9604(flags, command);
14272 + ep->toggle = !(ep->toggle);
14273 + if (!written_length) req->req.zero = 0;//just wrote zero bytes, there is no more need for req.zero
14274 + return written_length;
14277 +// return: 0 = still running, 1 = completed, negative = errno
14278 +static int write_fifo(struct n9604_ep *ep, struct n9604_request *req)
14280 + struct n9604_udc *dev = ep->dev;
14281 + u8 *buf;
14282 + unsigned count;
14283 + int is_last;
14285 + buf = req->req.buf + req->req.actual;
14286 + prefetch(buf);
14288 + dev = ep->dev;
14290 + count = write_packet(ep, buf, req);
14291 + if (count < 0)
14292 + return count;
14294 + /* last packet often short (sometimes a zlp, especially on ep0) */
14295 + if ((req->req.length != req->req.actual) || req->req.zero)
14296 + is_last = 0;
14297 + else
14298 + is_last = 1;
14300 + /* requests complete when all IN data is in the FIFO,
14301 + * or sometimes later, if a zlp was needed.
14302 + */
14303 + if (is_last) {
14304 + done(ep, req, 0);
14305 + return 1;
14307 + return 0;
14310 +static inline void pio_irq_enable(struct n9604_ep *ep);
14312 +static int read_fifo(struct n9604_ep *ep, struct n9604_request *req)
14314 + u32 size;
14315 + u8 *buf;
14316 + int bufferspace_available, fifospace_left, num_bytes_read;
14317 + int fifo, status;
14318 + ep->packets++;
14319 + if (!ep->num) {
14320 + fifo = RXD0;
14321 + status = RXS0;
14322 + } else {
14323 + fifo = ep->fifo;
14324 + status = ep->status;
14326 + num_bytes_read = 0;
14327 + buf = req->req.buf + req->req.actual;
14328 + bufferspace_available = req->req.length - req->req.actual;
14329 + size = read_9604(status) & (RXS_RCOUNTMASK | RXS_RX_ERR);//number of bytes ready to be read (15 if greater than 15)
14330 + if (ep->num && (size & RXS_RX_ERR)) {
14331 + ERROR(ep->dev, "DATA ERROR!!!! on ep%d\nFlushing Fifo", ep->num);
14332 + Flush_and_enable(ep->command);
14333 + goto leave;
14335 + size = size & ~RXS_RX_ERR;//clear the bit
14336 + if (ep->num) fifospace_left = MAX_FIFO_SIZE;
14337 + else fifospace_left = MAX_EP0_SIZE;
14338 +loop:
14339 + /* read all bytes from this packet */
14340 + while (size-- != 0) {
14341 + u8 byte = read_9604(fifo);
14342 + if (unlikely(bufferspace_available == 0)) {
14343 + /* this happens when the driver's buffer
14344 + * is smaller than what the host sent.
14345 + * discard the extra data in this packet.
14346 + */
14347 + done(ep, req, -EOVERFLOW);
14348 + return 1;
14349 + } else {
14350 + *buf++ = byte;
14351 + bufferspace_available--;
14352 + fifospace_left--;
14353 + num_bytes_read++;
14356 + if ((size = (read_9604(status) & RXS_RCOUNTMASK))) {
14357 + goto loop;//since there is more data
14359 + /* completion */
14360 + req->req.actual = req->req.actual + num_bytes_read;
14361 + if (fifospace_left || req->req.actual == req->req.length) {
14362 + done(ep, req, 0);
14363 + return 1;
14365 +leave:
14366 + pio_irq_enable(ep);//turn the interrupt back on
14367 + return 0;
14371 +/*-------------------------------------------------------------------------*/
14373 +static inline void
14374 +pio_irq_enable(struct n9604_ep *ep)
14376 + if (ep->is_in)
14377 + write_9604(read_9604(TXMSK) | 1 << ep->fifoNum | 0x10 << ep->fifoNum, TXMSK);
14378 + else {
14379 + u8 command = ep->command;
14380 + if (!ep->num) command = RXC0;
14381 + write_9604(read_9604(RXMSK) | 1 << ep->fifoNum | 0x10 << ep->fifoNum, RXMSK);
14382 + write_9604(RXC_RX_EN | RXC_RFWL0 | RXC_RFWL1, command);
14386 +static inline void
14387 +pio_irq_disable(struct n9604_ep *ep)//epnum != 0
14389 + if (ep->is_in)
14390 + write_9604(read_9604(TXMSK) & ~(1 << ep->fifoNum) & ~(0x10 << ep->fifoNum), TXMSK);
14391 + else
14392 + write_9604(read_9604(RXMSK) & ~(1 << ep->fifoNum) & ~(0x10 << ep->fifoNum), RXMSK);
14395 +static int request_voodoo = 0;//number of bytes the host requested
14397 +static inline void
14398 +pio_advance(struct n9604_ep *ep)
14400 + struct n9604_request *req;
14402 + if (list_empty (&ep->queue)) {
14403 + if (!ep->num) {
14404 + if (ep->is_in && (ep->stage == 2)) {
14405 + ep->is_in = 0;//switch modes
14406 + Flush_and_enable(RXC0);//needed to receive a ZLP after tx
14407 + ep->stage++;//and bump the stage number
14408 + } else if (ep->stage == 3) {
14409 + ep->stage = 0;
14412 + return;
14414 + req = list_entry(ep->queue.next, struct n9604_request, queue);
14415 + (ep->is_in ? write_fifo : read_fifo)(ep, req);
14418 +/*-------------------------------------------------------------------------*/
14420 +static void * n9604_alloc_buffer(struct usb_ep *_ep, unsigned bytes, dma_addr_t *dma, int gfp_flags)
14422 + return kmalloc(bytes, gfp_flags);
14425 +static void n9604_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
14427 + kfree (buf);
14432 +/*-------------------------------------------------------------------------*/
14434 +static void
14435 +done(struct n9604_ep *ep, struct n9604_request *req, int status)
14437 + struct n9604_udc *dev;
14439 + list_del_init(&req->queue);
14440 + ep->queue_active--;
14442 + if (req->req.status == -EINPROGRESS)
14443 + req->req.status = status;
14444 + else
14445 + status = req->req.status;
14447 + dev = ep->dev;
14449 + /* don't modify queue heads during completion callback */
14450 + if (ep->num)
14451 + pio_irq_disable(ep);
14452 + else if (!ep->nuking) {
14453 + ep->stage++;
14454 + ep->toggle = 1;//other endpoints stay in their flipping mode between transactions
14455 + if (ep->stage == 2) {//we are in stage 2 now
14456 + if (!ep->is_in) {
14457 + ep->is_in = 1;//switch modes
14458 + request_voodoo = 1;//prevents n9604_queue from calling us again before doing anything
14459 + send_zero_length(0, dev);
14460 + } else {//we have to receive a ZLP
14461 + //this will happen when the tx is complete, the pio_advance fcn will activate it for us
14466 + req->req.complete(&ep->ep, &req->req);
14470 +/*-------------------------------------------------------------------------*/
14472 +static int
14473 +n9604_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
14475 + struct n9604_request *req;
14476 + struct n9604_ep *ep;
14477 + struct n9604_udc *dev;
14478 + unsigned long flags;
14479 + int status;
14481 + req = container_of(_req, struct n9604_request, req);
14482 + if (unlikely(!_req || !_req->complete
14483 + || !_req->buf || !list_empty(&req->queue)))
14484 + return -EINVAL;
14485 + ep = container_of(_ep, struct n9604_ep, ep);
14486 + if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
14487 + return -EINVAL;
14488 + dev = ep->dev;
14489 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
14490 + return -ESHUTDOWN;
14492 + if (ep->nuking)
14493 + return -ESHUTDOWN;
14495 + spin_lock_irqsave(&dev->lock, flags);
14497 + ep->queue_reqs++;
14498 + ep->queue_active++;
14500 + _req->status = -EINPROGRESS;
14501 + _req->actual = 0;
14503 + /* for ep0 IN without premature status, zlp is required and
14504 + * writing EOP starts the status stage (OUT).
14505 + */
14506 + if (ep->num == 0) {
14507 + if ((request_voodoo > _req->length) && !(_req->length % MAX_EP0_SIZE) && (_req->length != 0)) {
14508 + _req->zero = 1;
14510 + if (!request_voodoo && !ep->is_in) {//this is a zero length request
14511 + spin_unlock_irqrestore(&dev->lock, flags);//David
14512 + done(ep, req, 0);//this doesn't check if the list is empty (probably not an issue)
14513 + return 0; //shouldn't this be handled by the rx irq fcn, and passed to pio_advance
14514 + }//that may conflict with the voodoo stuff, maybe best to leave it
14517 + /* kickstart this i/o queue? */
14518 + status = 0;
14519 + if (list_empty(&ep->queue) && ep->is_in) {
14520 + status = write_fifo(ep, req);
14521 + if (status == -EBUSY)
14522 + ;//we should queue up the request then
14523 + else {
14524 + if (status != 0) {
14525 + if (status > 0)
14526 + status = 0;
14527 + req = 0;
14530 + } /* else pio or dma irq handler advances the queue. */
14532 + if (req != 0) {
14533 + list_add_tail(&req->queue, &ep->queue);
14534 + pio_irq_enable(ep);
14537 + spin_unlock_irqrestore(&dev->lock, flags);
14538 + return status;
14541 +/* dequeue ALL requests */
14542 +static void nuke(struct n9604_ep *ep, int status)
14544 + struct n9604_request *req;
14546 + if (list_empty(&ep->queue))
14547 + return;
14548 + ep->nuking = 1;
14549 + while (!list_empty(&ep->queue)) {
14550 + req = list_entry(ep->queue.next, struct n9604_request, queue);
14551 + done(ep, req, status);
14553 + ep->nuking = 0;
14556 +/* dequeue JUST ONE request */
14557 +static int n9604_dequeue(struct usb_ep *_ep, struct usb_request *_req)
14559 + struct n9604_request *req;
14560 + struct n9604_ep *ep;
14561 + struct n9604_udc *dev;
14562 + unsigned long flags;
14564 + ep = container_of(_ep, struct n9604_ep, ep);
14565 + if (!_ep || !_req || (!ep->desc && ep->num != 0))
14566 + return -EINVAL;
14567 + dev = ep->dev;
14569 + if (!dev->driver)
14570 + return -ESHUTDOWN;
14572 + spin_lock_irqsave(&dev->lock, flags);
14574 + /* make sure it's actually queued on this endpoint */
14575 + list_for_each_entry (req, &ep->queue, queue) {
14576 + if (&req->req == _req)
14577 + break;
14579 + if (&req->req != _req) {
14580 + spin_unlock_irqrestore (&dev->lock, flags);
14581 + return -EINVAL;
14584 + spin_unlock_irqrestore(&dev->lock, flags);
14586 + return req ? 0 : -EOPNOTSUPP;
14589 +static int n9604_clear_halt(struct usb_ep *_ep) {
14590 + struct n9604_ep *ep;
14591 + ep = container_of (_ep, struct n9604_ep, ep);
14593 + write_9604(read_9604(ep->control) & ~EPC_STALL, ep->control);
14594 + pio_advance(ep);
14595 + return 0;
14598 +static int n9604_set_halt(struct usb_ep *_ep, int value) {
14599 + struct n9604_ep *ep;
14600 + unsigned long flags;
14601 + int retval = 0;
14603 + if (!_ep) {
14604 + retval = -ENODEV; goto exit;
14606 + ep = container_of (_ep, struct n9604_ep, ep);
14608 + if (ep->num == 0) {//is this valid?
14609 + if (!value) {
14610 + retval = -EINVAL; goto exit; }
14612 + /* don't change EPxSTATUS_EP_INVALID to READY */
14613 + } else if (!ep->desc) {
14614 + retval = -EINVAL; goto exit;
14617 + spin_lock_irqsave(&ep->dev->lock, flags);
14618 + if (!list_empty(&ep->queue))
14619 + retval = -EAGAIN;
14620 + else if (!value)
14621 + n9604_clear_halt(_ep);
14622 + else {
14623 + write_9604(read_9604(ep->control) | EPC_STALL, ep->control);
14625 + spin_unlock_irqrestore(&ep->dev->lock, flags);
14626 +exit:
14627 + return retval;
14630 +static int n9604_fifo_status(struct usb_ep *_ep) {//not implemented
14631 + return -1;
14634 +static void n9604_fifo_flush(struct usb_ep *_ep) {//not implemented
14635 + struct n9604_ep *ep;
14636 + ep = container_of (_ep, struct n9604_ep, ep);
14639 +/*-------------------------------------------------------------------------*/
14641 +static struct usb_ep_ops n9604_ep_ops = {
14642 + .enable = n9604_ep_enable,
14643 + .disable = n9604_ep_disable,
14645 + .alloc_request = n9604_alloc_request,//io request objects called struct usb_request
14646 + .free_request = n9604_free_request,
14648 + .alloc_buffer = n9604_alloc_buffer,
14649 + .free_buffer = n9604_free_buffer,
14651 + .queue = n9604_queue,//submit a struct usb_request object to an endpoint
14652 + .dequeue = n9604_dequeue,
14654 + .set_halt = n9604_set_halt,//halts an endpoint
14655 + .fifo_status = n9604_fifo_status,//bytes in FIFO + data ready to go in FIFO
14656 + .fifo_flush = n9604_fifo_flush,//flush all the data, endpoint is probably been reconfigured
14659 +/*-------------------------------------------------------------------------*/
14661 +static int n9604_get_frame(struct usb_gadget *_gadget)
14663 + return -EOPNOTSUPP;
14666 +static const struct usb_gadget_ops n9604_ops = {
14667 + .get_frame = n9604_get_frame,
14670 +/*-------------------------------------------------------------------------*/
14672 +static void udc_reinit (struct n9604_udc *dev)
14674 + static char *names [] = { "ep0", "ep1in", "ep2out", "ep3in", "ep4out", "ep5in", "ep6out" };
14675 + unsigned i;
14677 + INIT_LIST_HEAD (&dev->gadget.ep_list);
14678 + dev->gadget.ep0 = &dev->ep [0].ep;
14679 + dev->gadget.speed = USB_SPEED_UNKNOWN;
14680 + dev->irqs = 0;
14681 + dev->configured = 0;
14683 + //for (i = 0; i < 7; i++) {
14684 + for (i = 0; i < ARRAY_SIZE(names); i++) {
14685 + struct n9604_ep *ep = &dev->ep[i];
14686 + ep->num = i;
14687 + ep->numActual = i;
14688 + ep->ep.name = names[i];
14689 + ep->irqs = 0;
14690 + if (i) {
14691 + ep->fifo = (i * 4) + RXD0; //each FIFO address is 4 bytes away. TXD0 is the first
14692 + ep->control = ep->fifo - 1;
14693 + ep->status = ep->fifo + 1;
14694 + ep->command = ep->fifo + 2;
14695 + Flush(ep->command);//flush any data in the fifo//we don't care about the previous state
14696 + read_9604(ep->status);
14697 + ep->ep.maxpacket = MAX_FIFO_SIZE;
14698 + } else {//were are endpoint 0
14699 + ep->fifo = ep->control = ep->status = ep->command = 0xff;//this should force an error
14700 + //we need to do this since we don't know if
14701 + //this is tx or rx
14702 + read_9604(TXS0);
14703 + Flush(TXC0);
14704 + Flush(RXC0);//we could potentially (probably) overwriting a pending setup packet
14705 + if (ep->stage)//if we get a setup packet before we have a chance to finish the reset we have a problem
14706 + read_9604(RXS0);//fix this by sending stalls or something
14707 + ep->stage = 0;
14708 + ep->ep.maxpacket = MAX_EP0_SIZE;
14710 + ep->is_in = i % 2;
14711 + ep->fifoNum = (i + ep->is_in) / 2;//ignored for endpoint 0
14712 + ep->ep.ops = &n9604_ep_ops;
14713 + list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
14714 + ep->dev = dev;
14715 + INIT_LIST_HEAD (&ep->queue);
14716 + ep->nuking=0;
14717 + ep->queue_reqs = 0;
14718 + ep->queue_active = 0;
14719 + ep->packets = 0;
14720 + ep->desc = 0;
14721 + ep->irqs = 0;
14724 + list_del_init (&dev->ep[0].ep.ep_list);
14726 + write_9604(~WKUP_PNDUSB & ~WKUP_PNDUC & read_9604(WKUP), WKUP);//clear the bits, we've done a reset
14727 + write_9604(FAR_AD_EN, FAR);//enable the chip to answer requests//address 0
14728 + dev->address = 0;
14729 + write_9604(0, EPC0);//clear the control register
14730 + write_9604(NFSR_NodeOperational, NFSR);//we're going for gold
14733 +static void udc_reset(struct n9604_udc *dev)
14735 + //USBD_DISABLE_IRQ; This disables all interrupts sharing that line
14736 + write_9604(MCNTRL_SRST,MCNTRL);//software reset -- this also prevents pullup
14737 + write_9604(0x00, MAMSK); //disable interrupts
14742 +static void udc_enable(struct n9604_udc *dev)
14744 + udc_reset(dev); //this is to prevent a pullup resistor
14745 + udc_reinit (dev);
14747 + dev->gadget.speed = USB_SPEED_FULL;
14749 + // enable ep0 interrupts
14750 + dev->ep[0].is_in = 0;
14752 + write_9604(MAMSK_WARN | MAMSK_ALT | MAMSK_TX_EV | MAMSK_RX_EV | MAMSK_INTR, MAMSK);//for now we turn it all on, except frames & ULD & NAK
14753 + write_9604(ALTMSK_RESET, ALTMSK);//just turn on reset
14754 + write_9604(0x11, TXMSK);
14755 + write_9604(0x11, RXMSK);
14756 + write_9604(0x0, NAKMSK);
14757 + write_9604(0x0, FWMSK);
14758 + write_9604(MCNTRL_NAT | MCNTRL_INTOC_ActHigh, MCNTRL);//this activates the pull-up and turns on interrupts
14759 + USBD_ENABLE_IRQ;
14762 +/*-------------------------------------------------------------------------*/
14764 +/* keeping it simple:
14765 + * - one bus driver, initted first;
14766 + * - one function driver, initted second
14767 + */
14769 +static struct n9604_udc *the_controller;
14771 +/* when a driver is successfully registered, it will receive
14772 + * control requests including set_configuration(), which enables
14773 + * non-control requests. then usb traffic follows until a
14774 + * disconnect is reported. then a host may connect again, or
14775 + * the driver might get unbound.
14776 + */
14777 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
14779 + struct n9604_udc *dev = the_controller;
14780 + int retval;
14782 + if (!driver
14783 + || driver->speed != USB_SPEED_FULL
14784 + || !driver->bind
14785 + || !driver->unbind
14786 + || !driver->disconnect
14787 + || !driver->setup)
14788 + return -EINVAL;
14789 + if (!dev)
14790 + return -ENODEV;
14791 + if (dev->driver)
14792 + return -EBUSY;
14794 + /* hook up the driver */
14795 + dev->driver = driver;
14796 + retval = driver->bind(&dev->gadget);
14797 + if (retval) {
14798 + dev->driver = 0;
14799 + return retval;
14802 + /* then enable host detection and ep0; and we're ready
14803 + * for set_configuration as well as eventual disconnect.
14804 + */
14805 + udc_enable(dev);
14807 + return 0;
14809 +EXPORT_SYMBOL(usb_gadget_register_driver);
14811 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
14813 + struct n9604_udc *dev = the_controller;
14814 + unsigned long flags;
14815 + int i;
14817 + if (!dev)
14818 + return -ENODEV;
14819 + if (!driver || driver != dev->driver)
14820 + return -EINVAL;
14822 + spin_lock_irqsave(&dev->lock, flags);
14823 + dev->driver = 0;
14825 + udc_reset(dev);//reset & diable irqs
14826 + for (i = 0; i < ARRAY_SIZE(dev->ep); i++)
14827 + nuke(&dev->ep [i], -ESHUTDOWN);
14828 + spin_unlock_irqrestore(&dev->lock, flags);
14830 + if (dev->gadget.speed != USB_SPEED_UNKNOWN)
14831 + driver->disconnect(&dev->gadget);
14832 + driver->unbind(&dev->gadget);
14834 + return 0;
14836 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
14839 +/*-------------------------------------------------------------------------*/
14841 +inline u8 tx_ev_irq(struct n9604_udc *dev) {
14842 + u8 mask;
14844 + mask = read_9604(TXEV) & read_9604(TXMSK);
14846 + if (mask & TXEV_FIFO0) {
14847 + write_9604(0, EPC0);//make sure we are not stalled, & not using the default address
14848 + read_9604(TXS0);//should really check for error conditions
14849 + dev->ep[0].irqs++;
14850 + pio_advance(&dev->ep[0]);
14852 + if (mask & TXEV_FIFO1) {
14853 + read_9604(TXS1);
14854 + dev->ep[1].irqs++;
14855 + pio_advance(&dev->ep[1]);
14857 + if (mask & TXEV_FIFO2) {
14858 + read_9604(TXS2);
14859 + dev->ep[3].irqs++;
14860 + pio_advance(&dev->ep[3]);
14862 + if (mask & TXEV_FIFO3) {
14863 + read_9604(TXS3);
14864 + dev->ep[5].irqs++;
14865 + pio_advance(&dev->ep[5]);
14867 + return mask;
14870 +static void my_req_complete(struct usb_ep *_ep, struct usb_request *req) {//this was for the setup packet, but I guess I could use it for anything
14871 + n9604_free_buffer(_ep, req->buf, req->dma, req->length);
14872 + n9604_free_request(_ep, req);
14875 +inline void send_dummy_packet(int endpoint, struct n9604_udc *dev, int length) {
14876 + struct usb_request *my_req;
14877 + my_req = n9604_alloc_request(&dev->ep[endpoint].ep, GFP_ATOMIC);
14878 + my_req->length = length;
14879 + my_req->buf = n9604_alloc_buffer(&dev->ep[endpoint].ep, length, &my_req->dma, GFP_ATOMIC);
14880 + my_req->complete = my_req_complete;
14881 + n9604_queue(&dev->ep[endpoint].ep, my_req, GFP_ATOMIC);
14884 +inline void send_zero_length(int endpoint, struct n9604_udc *dev) {
14885 + send_dummy_packet(endpoint, dev, 0);
14888 +inline void rx_ev_irq(struct n9604_udc *dev) {
14889 + u8 mask;
14890 + struct n9604_ep *ep;
14892 + mask = read_9604(RXEV) & read_9604(RXMSK);
14894 + if (mask & RXEV_FIFO0) {
14895 + static int read_mode = 0;
14896 + u8 rxs_mask = read_9604(RXS0);
14897 + ep = &dev->ep[0];
14898 + ep->irqs++;
14899 + if (rxs_mask & RXS_SETUP) {
14900 + struct usb_ctrlrequest ctrl;
14901 + ep->packets++;
14902 + write_9604(0x40, ALTMSK);//someone is talking to us. Make sure we can be reset if we lose this communication
14903 + ep->stage = 1;
14904 + rxs_mask = read_9604(RXS0);//2nd read (1st one is for zero length packet)
14905 + ctrl.bRequestType = read_9604(RXD0);
14906 + ctrl.bRequest = read_9604(RXD0);
14907 + ctrl.wValue = read_9604(RXD0) + (read_9604(RXD0) << 8);
14908 + ctrl.wIndex = read_9604(RXD0) + (read_9604(RXD0) << 8);
14909 + ctrl.wLength = read_9604(RXD0) + (read_9604(RXD0) << 8);
14910 + ep->toggle = 1;
14911 + request_voodoo = ctrl.wLength;
14912 + if (ctrl.bRequestType & 0x80) {//This is an IN transaction
14913 + ep->is_in = 1;//David: is this correct for both cases//check with n9604_queue
14914 + read_mode = 0;
14915 + if (ctrl.wLength) {//should be followed by ZLP out packet
14916 + } else {//host expects ZLP out packet
14917 + ep->stage = 2;
14919 + } else {//This is an out transaction
14920 + if (ctrl.wLength) {
14921 + ep->is_in = 0;
14922 + read_mode = 1;
14923 + } else {//host expects ZLP in packet
14924 + read_mode = 0;
14925 + ep->stage = 2;
14926 + ep->is_in = 1;
14929 + switch (ctrl.bRequest) {
14930 + case USB_REQ_SET_ADDRESS:
14931 + write_9604(EPC_DEF, EPC0);//we still want to respond to the default address
14932 + write_9604(((dev->address = (ctrl.wValue & FAR_AD_MASK))) | FAR_AD_EN, FAR);
14933 + send_zero_length(0, dev);
14934 + dev->configured = 1;//we can send longer packets now :)
14935 + read_9604(ALTEV);
14936 + write_9604(ALTMSK_RESET, ALTMSK);//we also listen to reset requests too
14937 + break;
14938 + case USB_REQ_CLEAR_FEATURE:
14939 + if (ctrl.wValue == 0 && ctrl.bRequestType == 2) {//endpoint halt
14940 + int i;
14941 + for (i = 0; i < ARRAY_SIZE(dev->ep); i++)
14942 + if ((ctrl.wIndex & 0xF) == dev->ep[i].numActual)
14943 + n9604_clear_halt(&dev->ep[i].ep);
14944 + send_zero_length(0, dev);
14945 + break;
14947 + case USB_REQ_SET_DESCRIPTOR:
14948 + case USB_REQ_SYNCH_FRAME:
14949 + case USB_REQ_GET_STATUS:
14950 + case USB_REQ_SET_FEATURE:
14951 + case USB_REQ_SET_CONFIGURATION:
14952 + case USB_REQ_GET_DESCRIPTOR:
14953 + case USB_REQ_GET_CONFIGURATION:
14954 + case USB_REQ_SET_INTERFACE:
14955 + case USB_REQ_GET_INTERFACE:
14956 + default:
14957 + if (dev->driver->setup(&dev->gadget, &ctrl) < 0)//there was an error
14958 + if (((ctrl.bRequestType & 0x80) && ctrl.wLength) || (!(ctrl.bRequestType & 0x80) && !ctrl.wLength))
14959 + send_zero_length(0, dev);
14960 + }//crtl.bRequest
14961 + }//setup
14962 + else if (read_mode)
14963 + pio_advance(ep);
14964 + else {
14965 + ep->stage = 0;
14966 + ep->packets++;
14968 + }//fifo 0
14969 + if (mask & RXEV_FIFO1) {
14970 + ep = &dev->ep[2];
14971 + pio_advance(ep);
14972 + ep->irqs++;
14974 + if (mask & RXEV_FIFO2) {
14975 + ep = &dev->ep[4];
14976 + pio_advance(ep);
14977 + ep->irqs++;
14979 + if (mask & RXEV_FIFO3) {
14980 + ep = &dev->ep[6];
14981 + pio_advance(ep);
14982 + ep->irqs++;
14986 +inline void alt_ev_irq(struct n9604_udc *dev) {
14987 + u8 mask;
14989 + mask = read_9604(ALTEV) & read_9604(ALTMSK);
14991 + if (mask & ALTEV_EOP);
14992 + if (mask & ALTEV_SD3);
14993 + if (mask & ALTEV_SD5);
14994 + if (mask & ALTEV_RESET) {
14995 + int i;
14996 + udelay(1200);//no idea why this is needed, but it makes things work
14997 + write_9604(0x0, FAR);//lets not respond to any packets until we are ready
14998 + write_9604(NFSR_NodeReset, NFSR);
14999 + dev->driver->disconnect(&dev->gadget);
15000 + for (i = 0; i < ARRAY_SIZE(dev->ep); i++)
15001 + nuke(&dev->ep [i], -ESHUTDOWN);//this should be handled above by disconnect
15002 + write_9604(0x00, ALTMSK);//make sure reset is turned off, or we will constantly be interrupted
15003 + write_9604(0x11, TXMSK);
15004 + write_9604(0x11, RXMSK);
15005 + udc_reinit(dev);
15006 + dev->gadget.speed = USB_SPEED_FULL;
15007 + dev->ep[0].is_in = 0;
15009 + if (mask & ALTEV_RESUME); //write_9604(NFSR_NodeOperational, NFSR);
15010 + if (mask & ALTEV_WKUP);//we don't really sleep
15011 + if (mask & ALTEV_DMA);
15014 +static void n9604_irq(int irq, void *_dev, struct pt_regs *r) {
15015 + struct n9604_udc *dev = _dev;
15016 + u8 mask;
15018 + mask = read_9604(MAEV) & read_9604(MAMSK);
15019 + if (!mask)
15020 + return;
15022 + if (mask & MAEV_ALT) {
15023 + alt_ev_irq(dev);
15024 + mask = read_9604(MAEV) & read_9604(MAMSK);//force a re-read of the current pending interrupts
15026 + if (mask & MAEV_TX_EV)
15027 + tx_ev_irq(dev);
15028 + if (mask & MAEV_RX_EV)
15029 + rx_ev_irq(dev);
15030 + dev->irqs++;
15031 + return;
15034 +/*-------------------------------------------------------------------------*/
15036 +static int __init init (void)
15038 + struct n9604_udc *dev;
15039 + int ret;
15040 + u8 * addr;
15042 + if (the_controller)
15043 + return -EBUSY;
15045 + addr = ioremap(USBN9604_PHYS, 0x2);//ioremap will bump this to 1 page size
15046 + if (!addr) {
15047 + ERROR(dev, KERN_ERR "Unable to remap address\n");
15048 + return -EINVAL;
15051 + USBN9604_Offset = addr;
15053 + if ((read_9604(RID) & 0xF) != 0x2) { //0x2 is the identifier for 9603/4
15054 + iounmap(addr);
15055 + return -ENODEV;
15058 + /* alloc, and start init */
15059 + dev = kmalloc(sizeof *dev, SLAB_KERNEL);
15060 + if (dev == NULL){
15061 + WARN(dev, "No memory");
15062 + iounmap(addr);
15063 + return -ENOMEM;
15065 + memset(dev, 0, sizeof *dev);
15066 + spin_lock_init(&dev->lock);
15067 + dev->gadget.ops = &n9604_ops;
15068 + dev->gadget.is_dualspeed = 0;
15070 + /* the "gadget" abstracts/virtualizes the controller */
15071 + dev->gadget.dev.bus_id = "gadget";
15072 + dev->gadget.name = driver_name;
15074 + /* initialize the hardware */
15076 + udc_reset(dev);
15078 + write_9604(CCONF_CODIS | 11, CCONF);
15080 + udc_reinit(dev);//this is necessary as it sets up the epx functions
15082 + the_controller = dev;
15084 + if ((ret=request_irq(IRQ_GPIOC, n9604_irq, SA_SHIRQ, driver_name,dev))) {
15085 + WARN(dev, "Can't get IRQ\n");
15086 + iounmap(addr);
15087 + return ret;
15090 + return 0;
15092 +module_init (init);
15094 +static void __exit cleanup (void)
15096 + struct n9604_udc *dev = the_controller;
15098 + //first kill the interrupts
15099 + udc_reset(dev);
15100 + free_irq(IRQ_GPIOC, dev);
15102 + /* start with the driver above us */
15103 + if (dev->driver) {
15104 + /* should have been done already by driver model core */
15105 + WARN(dev, "Warning: Driver '%s' is still registered\n",
15106 + dev->driver->driver.name);
15107 + usb_gadget_unregister_driver(dev->driver);
15109 + kfree(dev);
15110 + iounmap(USBN9604_Offset);
15111 + the_controller = 0;
15114 +module_exit (cleanup);
15116 +MODULE_PARM_DESC (delayTime, "Delays after reads and writes to the USB chip");
15117 +MODULE_PARM (delayTime, "i");
15119 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/n9604.h kernel/drivers/usb/gadget/n9604.h
15120 --- /tmp/kernel/drivers/usb/gadget/n9604.h 1970-01-01 01:00:00.000000000 +0100
15121 +++ kernel/drivers/usb/gadget/n9604.h 2005-04-22 17:53:19.463534794 +0200
15122 @@ -0,0 +1,112 @@
15124 + * National 9604 USB device controller driver
15126 + * Copyright 2003 Technical Solutions Inc.
15128 + * ported from:
15129 + *
15130 + * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
15132 + * Copyright (C) 2000-2002 Lineo
15133 + * by Stuart Lynne, Tom Rushworth, and Bruce Balden
15134 + * Copyright (C) 2002 Toshiba Corporation
15135 + * Copyright (C) 2003 MontaVista Software (source@mvista.com)
15137 + * This file is licensed under the terms of the GNU General Public
15138 + * License version 2. This program is licensed "as is" without any
15139 + * warranty of any kind, whether express or implied.
15140 + */
15142 +#define MAX_FIFO_SIZE 64
15143 +#define MAX_EP0_SIZE 8
15145 +struct n9604_ep {
15146 + struct usb_ep ep;
15147 + struct n9604_udc *dev;
15148 + unsigned long irqs;
15149 + int acct_req_lengths[4];
15150 + int acct_req_dir[4];//direction
15151 + unsigned long queue_reqs;//how many times has n9604_queue been called
15152 + unsigned long queue_active;//how many current requests
15153 + unsigned long packets;//counter of raw packets
15154 + unsigned num:4,
15155 + numActual:4,
15156 + fifoNum:2,
15157 + is_in:1,
15158 + stage:2,//for ep0, 0 = unused, 1 = got setup, 2 = done transfer/ready to send/receive ZLP
15159 + toggle:1,
15160 + nuking:1;//are we killing on off this endpoint//only used for ep0 to help with stages
15161 + /* analogous to a host-side qh */
15162 + struct list_head queue;
15163 + const struct usb_endpoint_descriptor *desc;
15165 + u8 control;
15166 + u8 fifo;
15167 + u8 status;
15168 + u8 command;
15171 +struct n9604_request {
15172 + struct usb_request req;
15173 + struct list_head queue;
15174 + int complete;//this is added for tx requests
15175 + //if set the entire request has been written to the fifo, just waiting for confirmation
15176 + //from the interrupt that it has been sent
15178 + unsigned mapped:1;
15181 +struct n9604_udc {
15182 + struct usb_gadget gadget;
15183 + spinlock_t lock;
15184 + struct n9604_ep ep[7];
15185 + struct usb_gadget_driver *driver;
15186 + int configured;
15188 + u8 address;
15190 + /* statistics... */
15191 + unsigned long irqs;
15195 +/*-------------------------------------------------------------------------*/
15197 +#define xprintk(dev,level,fmt,args...) \
15198 + printk(level "%s %s: " fmt , driver_name , \
15199 + "S2410 gadget" , ## args)
15201 +#define ERROR(dev,fmt,args...) \
15202 + xprintk(dev , KERN_ERR , fmt , ## args)
15203 +#define WARN(dev,fmt,args...) \
15204 + xprintk(dev , KERN_WARNING , fmt , ## args)
15205 +#define INFO(dev,fmt,args...) \
15206 + xprintk(dev , KERN_INFO , fmt , ## args)
15208 +/*-------------------------------------------------------------------------*/
15210 +/* 2.5 stuff that's sometimes missing in 2.4 */
15212 +#ifndef container_of
15213 +#define container_of list_entry
15214 +#endif
15216 +#ifndef likely
15217 +#define likely(x) (x)
15218 +#define unlikely(x) (x)
15219 +#endif
15221 +#ifndef BUG_ON
15222 +#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
15223 +#endif
15225 +#ifndef WARN_ON
15226 +#define WARN_ON(x) do { } while (0)
15227 +#endif
15229 +#ifndef IRQ_NONE
15230 +typedef void irqreturn_t;
15231 +#define IRQ_NONE
15232 +#define IRQ_HANDLED
15233 +#define IRQ_RETVAL(x)
15234 +#endif
15235 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/n9604regs.h kernel/drivers/usb/gadget/n9604regs.h
15236 --- /tmp/kernel/drivers/usb/gadget/n9604regs.h 1970-01-01 01:00:00.000000000 +0100
15237 +++ kernel/drivers/usb/gadget/n9604regs.h 2005-04-22 17:53:19.466534306 +0200
15238 @@ -0,0 +1,248 @@
15239 +/* National 9604 registers */
15241 +#define USBN9604_PHYS 0x08000000
15243 +extern u8 * USBN9604_Offset;
15245 +static u8 last_address = 255;//an invalid address
15247 +inline u8 read_9604(u8 addr) {
15248 + u8 tmp;
15249 + if (addr != last_address) {
15250 + outb(addr, USBN9604_Offset + 1);
15251 + last_address = addr;
15253 + tmp = inb(USBN9604_Offset);
15254 + return tmp;
15257 +inline void write_9604(u8 value, u8 addr) {
15258 + if (addr != last_address) {
15259 + outb(addr, USBN9604_Offset + 1);
15260 + last_address = addr;
15262 + outb(value, USBN9604_Offset);
15267 +#define MCNTRL 0x00
15268 +#define CCONF 0x01
15270 +#define RID 0x03
15271 +#define FAR 0x04
15272 +#define NFSR 0x05
15273 +#define MAEV 0x06
15274 +#define MAMSK 0x07
15275 +#define ALTEV 0x08
15276 +#define ALTMSK 0x09
15277 +#define TXEV 0x0A
15278 +#define TXMSK 0x0B
15279 +#define RXEV 0x0C
15280 +#define RXMSK 0x0D
15281 +#define NAKEV 0x0E
15282 +#define NAKMSK 0x0F
15283 +#define FWEV 0x10
15284 +#define FWMSK 0x11
15285 +#define FNH 0x12
15286 +#define FNL 0x13
15287 +#define DMACNTRL 0x14
15288 +#define DMAEV 0x15
15289 +#define DMAMSK 0x16
15290 +#define MIR 0x17
15291 +#define DMACNT 0x18
15292 +#define DMAERR 0x19
15294 +#define WKUP 0x1B
15299 +#define EPC0 0x20
15300 +#define TXD0 0x21
15301 +#define TXS0 0x22
15302 +#define TXC0 0x23
15304 +#define RXD0 0x25
15305 +#define RXS0 0x26
15306 +#define RXC0 0x27
15307 +#define EPC1 0x28
15308 +#define TXD1 0x29
15309 +#define TXS1 0x2A
15310 +#define TXC1 0x2B
15311 +#define EPC2 0x2C
15312 +#define RXD1 0x2D
15313 +#define RXS1 0x2E
15314 +#define RXC1 0x2F
15315 +#define EPC3 0x30
15316 +#define TXD2 0x31
15317 +#define TXS2 0x32
15318 +#define TXC2 0x33
15319 +#define EPC4 0x34
15320 +#define RXD2 0x35
15321 +#define RXS2 0x36
15322 +#define RXC2 0x37
15323 +#define EPC5 0x38
15324 +#define TXD3 0x39
15325 +#define TXS3 0x3A
15326 +#define TXC3 0x3B
15327 +#define EPC6 0x3C
15328 +#define RXD3 0x3D
15329 +#define RXS3 0x3E
15330 +#define RXC3 0x3F
15333 +/* MCNTRL values */
15334 +#define MCNTRL_SRST (1 << 0)
15335 +#define MCNTRL_VGE (1 << 2)
15336 +#define MCNTRL_NAT (1 << 3)
15337 +#define MCNTRL_INTOC_MASK (3 << 6)
15338 +#define MCNTRL_INTOC_DISABLE 0
15339 +#define MCNTRL_INTOC_ActLowOpen (1 << 6)
15340 +#define MCNTRL_INTOC_ActHigh (2 << 6)
15341 +#define MCNTRL_INTOC_ActLowPP (3 << 6)
15343 +/* CCONF values */
15344 +#define CCONF_CLKDIV_MASK 0x0F
15345 +#define CCONF_CODIS (1 << 7)
15347 +/* FAR values */
15348 +#define FAR_AD_MASK 0x7F
15349 +#define FAR_AD_EN 0x80
15351 +/* NFSR values */
15352 +#define NFSR_NodeReset 0x0
15353 +#define NFSR_NodeResume 0x1
15354 +#define NFSR_NodeOperational 0x2
15355 +#define NFSR_NodeSuspend 0x3
15357 +/* MAEV values */
15358 +#define MAEV_WARN (1 << 0)
15359 +#define MAEV_ALT (1 << 1)
15360 +#define MAEV_TX_EV (1 << 2)
15361 +#define MAEV_FRAME (1 << 3)
15362 +#define MAEV_NAK (1 << 4)
15363 +#define MAEV_ULD (1 << 5)
15364 +#define MAEV_RX_EV (1 << 6)
15365 +#define MAEV_INTR (1 << 7)
15367 +/* MAMSK values */
15368 +#define MAMSK_WARN (1 << 0)
15369 +#define MAMSK_ALT (1 << 1)
15370 +#define MAMSK_TX_EV (1 << 2)
15371 +#define MAMSK_FRAME (1 << 3)
15372 +#define MAMSK_NAK (1 << 4)
15373 +#define MAMSK_ULD (1 << 5)
15374 +#define MAMSK_RX_EV (1 << 6)
15375 +#define MAMSK_INTR (1 << 7)
15377 +/* ALTEV values */
15379 +#define ALTEV_WKUP (1 << 1)
15380 +#define ALTEV_DMA (1 << 2)
15381 +#define ALTEV_EOP (1 << 3)
15382 +#define ALTEV_SD3 (1 << 4)
15383 +#define ALTEV_SD5 (1 << 5)
15384 +#define ALTEV_RESET (1 << 6)
15385 +#define ALTEV_RESUME (1 << 7)
15387 +/* ALTMSK values */
15389 +#define ALTMSK_WKUP (1 << 1)
15390 +#define ALTMSK_DMA (1 << 2)
15391 +#define ALTMSK_EOP (1 << 3)
15392 +#define ALTMSK_SD3 (1 << 4)
15393 +#define ALTMSK_SD5 (1 << 5)
15394 +#define ALTMSK_RESET (1 << 6)
15395 +#define ALTMSK_RESUME (1 << 7)
15397 +/* NAKEV values */
15399 +#define NAKEV_TXFIFO0 (1 << 0)
15400 +#define NAKEV_TXFIFO1 (1 << 1)
15401 +#define NAKEV_TXFIFO2 (1 << 2)
15402 +#define NAKEV_TXFIFO3 (1 << 3)
15403 +#define NAKEV_RXFIFO0 (1 << 4)
15404 +#define NAKEV_RXFIFO1 (1 << 5)
15405 +#define NAKEV_RXFIFO2 (1 << 6)
15406 +#define NAKEV_RXFIFO3 (1 << 7)
15409 +/* WKUP values */
15410 +#define WKUP_PNDUSB (1 << 0)
15411 +#define WKUP_PNDUC (1 << 1)
15412 +#define WKUP_ENUSB (1 << 2)
15413 +#define WKUP_ENUC (1 << 3)
15414 +#define WKUP_WKMODE (1 << 5)
15415 +#define WKUP_HOS (1 << 6)
15416 +#define WKUP_FHT (1 << 7)
15418 +/* EPC values */
15420 +#define EPC_EP_MASK 0x0F //EP0 == 0
15421 +#define EPC_EP_EN (1 << 4)//not EP0
15422 +#define EPC_ISO (1 << 5)//not EP0
15423 +#define EPC_DEF (1 << 6)//EP0 only
15424 +#define EPC_STALL (1 << 7)
15426 +/* TXS values */
15428 +#define TXS_TCOUNT_MASK 0x1F
15429 +#define TXS_TX_DONE (1 << 5)
15430 +#define TXS_ACK_STAT (1 << 6)
15431 +#define TXS_TX_URUN (1 << 7)
15433 +/* TXC values */
15435 +#define TXC_TX_EN (1 << 0)
15436 +#define TXC_LAST (1 << 1)//not for endpoint 0
15437 +#define TXC_TOGGLE (1 << 2)//sets DATA1 when set
15438 +#define TXC_FLUSH (1 << 3)
15439 +#define TXC_IGN_IN (1 << 4)//only endpoint 0
15440 +#define TXC_RFF (1 << 4)//not for endpoint 0
15441 +#define TXC_TFWL0 (1 << 5)//"
15442 +#define TXC_TFWL1 (1 << 6)//"
15443 +#define TXC_IGN_ISOMSK (1 << 7)//"
15445 +/* TXEV values */
15447 +#define TXEV_FIFO0 (1 << 0)
15448 +#define TXEV_FIFO1 (1 << 1)
15449 +#define TXEV_FIFO2 (1 << 2)
15450 +#define TXEV_FIFO3 (1 << 3)
15451 +#define TXEV_UDRRN0 (1 << 4)
15452 +#define TXEV_UDRRN1 (1 << 5)
15453 +#define TXEV_UDRRN2 (1 << 6)
15454 +#define TXEV_UDRRN3 (1 << 7)
15457 +/* RXEV values */
15459 +#define RXEV_FIFO0 (1 << 0)
15460 +#define RXEV_FIFO1 (1 << 1)
15461 +#define RXEV_FIFO2 (1 << 2)
15462 +#define RXEV_FIFO3 (1 << 3)
15463 +#define RXEV_OVRRN0 (1 << 4)
15464 +#define RXEV_OVRRN1 (1 << 5)
15465 +#define RXEV_OVRRN2 (1 << 6)
15466 +#define RXEV_OVRRN3 (1 << 7)
15468 +/* RXC values */
15470 +#define RXC_RX_EN (1 << 0)
15471 +#define RXC_IGN_OUT (1 << 1)
15472 +#define RXC_IGN_SETUP (1 << 2)
15473 +#define RXC_FLUSH (1 << 3)
15474 +#define RXC_RFWL0 (1 << 5)
15475 +#define RXC_RFWL1 (1 << 6)
15477 +/* RXS values */
15479 +#define RXS_RCOUNTMASK 0xF
15480 +#define RXS_RX_LAST (1 << 4)
15481 +#define RXS_TOGGLE (1 << 5)
15482 +#define RXS_SETUP (1 << 6)
15483 +#define RXS_RX_ERR (1 << 7)
15487 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/ndis.h kernel/drivers/usb/gadget/ndis.h
15488 --- /tmp/kernel/drivers/usb/gadget/ndis.h 1970-01-01 01:00:00.000000000 +0100
15489 +++ kernel/drivers/usb/gadget/ndis.h 2005-04-22 17:53:19.469533817 +0200
15490 @@ -0,0 +1,217 @@
15492 + * ndis.h
15493 + *
15494 + * ntddndis.h modified by Benedikt Spranger <b.spranger@pengutronix.de>
15495 + *
15496 + * Thanks to the cygwin development team,
15497 + * espacially to Casper S. Hornstrup <chorns@users.sourceforge.net>
15498 + *
15499 + * THIS SOFTWARE IS NOT COPYRIGHTED
15501 + * This source code is offered for use in the public domain. You may
15502 + * use, modify or distribute it freely.
15504 + * This code is distributed in the hope that it will be useful but
15505 + * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
15506 + * DISCLAIMED. This includes but is not limited to warranties of
15507 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15509 + */
15511 +#ifndef _LINUX_NDIS_H
15512 +#define _LINUX_NDIS_H
15515 +#define NDIS_STATUS_MULTICAST_FULL 0xC0010009
15516 +#define NDIS_STATUS_MULTICAST_EXISTS 0xC001000A
15517 +#define NDIS_STATUS_MULTICAST_NOT_FOUND 0xC001000B
15519 +enum NDIS_DEVICE_POWER_STATE {
15520 + NdisDeviceStateUnspecified = 0,
15521 + NdisDeviceStateD0,
15522 + NdisDeviceStateD1,
15523 + NdisDeviceStateD2,
15524 + NdisDeviceStateD3,
15525 + NdisDeviceStateMaximum
15528 +struct NDIS_PM_WAKE_UP_CAPABILITIES {
15529 + enum NDIS_DEVICE_POWER_STATE MinMagicPacketWakeUp;
15530 + enum NDIS_DEVICE_POWER_STATE MinPatternWakeUp;
15531 + enum NDIS_DEVICE_POWER_STATE MinLinkChangeWakeUp;
15534 +/* NDIS_PNP_CAPABILITIES.Flags constants */
15535 +#define NDIS_DEVICE_WAKE_UP_ENABLE 0x00000001
15536 +#define NDIS_DEVICE_WAKE_ON_PATTERN_MATCH_ENABLE 0x00000002
15537 +#define NDIS_DEVICE_WAKE_ON_MAGIC_PACKET_ENABLE 0x00000004
15539 +struct NDIS_PNP_CAPABILITIES {
15540 + u32 Flags;
15541 + struct NDIS_PM_WAKE_UP_CAPABILITIES WakeUpCapabilities;
15544 +struct NDIS_PM_PACKET_PATTERN {
15545 + u32 Priority;
15546 + u32 Reserved;
15547 + u32 MaskSize;
15548 + u32 PatternOffset;
15549 + u32 PatternSize;
15550 + u32 PatternFlags;
15554 +/* Required Object IDs (OIDs) */
15555 +#define OID_GEN_SUPPORTED_LIST 0x00010101
15556 +#define OID_GEN_HARDWARE_STATUS 0x00010102
15557 +#define OID_GEN_MEDIA_SUPPORTED 0x00010103
15558 +#define OID_GEN_MEDIA_IN_USE 0x00010104
15559 +#define OID_GEN_MAXIMUM_LOOKAHEAD 0x00010105
15560 +#define OID_GEN_MAXIMUM_FRAME_SIZE 0x00010106
15561 +#define OID_GEN_LINK_SPEED 0x00010107
15562 +#define OID_GEN_TRANSMIT_BUFFER_SPACE 0x00010108
15563 +#define OID_GEN_RECEIVE_BUFFER_SPACE 0x00010109
15564 +#define OID_GEN_TRANSMIT_BLOCK_SIZE 0x0001010A
15565 +#define OID_GEN_RECEIVE_BLOCK_SIZE 0x0001010B
15566 +#define OID_GEN_VENDOR_ID 0x0001010C
15567 +#define OID_GEN_VENDOR_DESCRIPTION 0x0001010D
15568 +#define OID_GEN_CURRENT_PACKET_FILTER 0x0001010E
15569 +#define OID_GEN_CURRENT_LOOKAHEAD 0x0001010F
15570 +#define OID_GEN_DRIVER_VERSION 0x00010110
15571 +#define OID_GEN_MAXIMUM_TOTAL_SIZE 0x00010111
15572 +#define OID_GEN_PROTOCOL_OPTIONS 0x00010112
15573 +#define OID_GEN_MAC_OPTIONS 0x00010113
15574 +#define OID_GEN_MEDIA_CONNECT_STATUS 0x00010114
15575 +#define OID_GEN_MAXIMUM_SEND_PACKETS 0x00010115
15576 +#define OID_GEN_VENDOR_DRIVER_VERSION 0x00010116
15577 +#define OID_GEN_SUPPORTED_GUIDS 0x00010117
15578 +#define OID_GEN_NETWORK_LAYER_ADDRESSES 0x00010118
15579 +#define OID_GEN_TRANSPORT_HEADER_OFFSET 0x00010119
15580 +#define OID_GEN_MACHINE_NAME 0x0001021A
15581 +#define OID_GEN_RNDIS_CONFIG_PARAMETER 0x0001021B
15582 +#define OID_GEN_VLAN_ID 0x0001021C
15584 +/* Optional OIDs */
15585 +#define OID_GEN_MEDIA_CAPABILITIES 0x00010201
15586 +#define OID_GEN_PHYSICAL_MEDIUM 0x00010202
15588 +/* Required statistics OIDs */
15589 +#define OID_GEN_XMIT_OK 0x00020101
15590 +#define OID_GEN_RCV_OK 0x00020102
15591 +#define OID_GEN_XMIT_ERROR 0x00020103
15592 +#define OID_GEN_RCV_ERROR 0x00020104
15593 +#define OID_GEN_RCV_NO_BUFFER 0x00020105
15595 +/* Optional statistics OIDs */
15596 +#define OID_GEN_DIRECTED_BYTES_XMIT 0x00020201
15597 +#define OID_GEN_DIRECTED_FRAMES_XMIT 0x00020202
15598 +#define OID_GEN_MULTICAST_BYTES_XMIT 0x00020203
15599 +#define OID_GEN_MULTICAST_FRAMES_XMIT 0x00020204
15600 +#define OID_GEN_BROADCAST_BYTES_XMIT 0x00020205
15601 +#define OID_GEN_BROADCAST_FRAMES_XMIT 0x00020206
15602 +#define OID_GEN_DIRECTED_BYTES_RCV 0x00020207
15603 +#define OID_GEN_DIRECTED_FRAMES_RCV 0x00020208
15604 +#define OID_GEN_MULTICAST_BYTES_RCV 0x00020209
15605 +#define OID_GEN_MULTICAST_FRAMES_RCV 0x0002020A
15606 +#define OID_GEN_BROADCAST_BYTES_RCV 0x0002020B
15607 +#define OID_GEN_BROADCAST_FRAMES_RCV 0x0002020C
15608 +#define OID_GEN_RCV_CRC_ERROR 0x0002020D
15609 +#define OID_GEN_TRANSMIT_QUEUE_LENGTH 0x0002020E
15610 +#define OID_GEN_GET_TIME_CAPS 0x0002020F
15611 +#define OID_GEN_GET_NETCARD_TIME 0x00020210
15612 +#define OID_GEN_NETCARD_LOAD 0x00020211
15613 +#define OID_GEN_DEVICE_PROFILE 0x00020212
15614 +#define OID_GEN_INIT_TIME_MS 0x00020213
15615 +#define OID_GEN_RESET_COUNTS 0x00020214
15616 +#define OID_GEN_MEDIA_SENSE_COUNTS 0x00020215
15617 +#define OID_GEN_FRIENDLY_NAME 0x00020216
15618 +#define OID_GEN_MINIPORT_INFO 0x00020217
15619 +#define OID_GEN_RESET_VERIFY_PARAMETERS 0x00020218
15621 +/* IEEE 802.3 (Ethernet) OIDs */
15622 +#define NDIS_802_3_MAC_OPTION_PRIORITY 0x00000001
15624 +#define OID_802_3_PERMANENT_ADDRESS 0x01010101
15625 +#define OID_802_3_CURRENT_ADDRESS 0x01010102
15626 +#define OID_802_3_MULTICAST_LIST 0x01010103
15627 +#define OID_802_3_MAXIMUM_LIST_SIZE 0x01010104
15628 +#define OID_802_3_MAC_OPTIONS 0x01010105
15629 +#define OID_802_3_RCV_ERROR_ALIGNMENT 0x01020101
15630 +#define OID_802_3_XMIT_ONE_COLLISION 0x01020102
15631 +#define OID_802_3_XMIT_MORE_COLLISIONS 0x01020103
15632 +#define OID_802_3_XMIT_DEFERRED 0x01020201
15633 +#define OID_802_3_XMIT_MAX_COLLISIONS 0x01020202
15634 +#define OID_802_3_RCV_OVERRUN 0x01020203
15635 +#define OID_802_3_XMIT_UNDERRUN 0x01020204
15636 +#define OID_802_3_XMIT_HEARTBEAT_FAILURE 0x01020205
15637 +#define OID_802_3_XMIT_TIMES_CRS_LOST 0x01020206
15638 +#define OID_802_3_XMIT_LATE_COLLISIONS 0x01020207
15640 +/* OID_GEN_MINIPORT_INFO constants */
15641 +#define NDIS_MINIPORT_BUS_MASTER 0x00000001
15642 +#define NDIS_MINIPORT_WDM_DRIVER 0x00000002
15643 +#define NDIS_MINIPORT_SG_LIST 0x00000004
15644 +#define NDIS_MINIPORT_SUPPORTS_MEDIA_QUERY 0x00000008
15645 +#define NDIS_MINIPORT_INDICATES_PACKETS 0x00000010
15646 +#define NDIS_MINIPORT_IGNORE_PACKET_QUEUE 0x00000020
15647 +#define NDIS_MINIPORT_IGNORE_REQUEST_QUEUE 0x00000040
15648 +#define NDIS_MINIPORT_IGNORE_TOKEN_RING_ERRORS 0x00000080
15649 +#define NDIS_MINIPORT_INTERMEDIATE_DRIVER 0x00000100
15650 +#define NDIS_MINIPORT_IS_NDIS_5 0x00000200
15651 +#define NDIS_MINIPORT_IS_CO 0x00000400
15652 +#define NDIS_MINIPORT_DESERIALIZE 0x00000800
15653 +#define NDIS_MINIPORT_REQUIRES_MEDIA_POLLING 0x00001000
15654 +#define NDIS_MINIPORT_SUPPORTS_MEDIA_SENSE 0x00002000
15655 +#define NDIS_MINIPORT_NETBOOT_CARD 0x00004000
15656 +#define NDIS_MINIPORT_PM_SUPPORTED 0x00008000
15657 +#define NDIS_MINIPORT_SUPPORTS_MAC_ADDRESS_OVERWRITE 0x00010000
15658 +#define NDIS_MINIPORT_USES_SAFE_BUFFER_APIS 0x00020000
15659 +#define NDIS_MINIPORT_HIDDEN 0x00040000
15660 +#define NDIS_MINIPORT_SWENUM 0x00080000
15661 +#define NDIS_MINIPORT_SURPRISE_REMOVE_OK 0x00100000
15662 +#define NDIS_MINIPORT_NO_HALT_ON_SUSPEND 0x00200000
15663 +#define NDIS_MINIPORT_HARDWARE_DEVICE 0x00400000
15664 +#define NDIS_MINIPORT_SUPPORTS_CANCEL_SEND_PACKETS 0x00800000
15665 +#define NDIS_MINIPORT_64BITS_DMA 0x01000000
15667 +#define NDIS_MEDIUM_802_3 0x00000000
15668 +#define NDIS_MEDIUM_802_5 0x00000001
15669 +#define NDIS_MEDIUM_FDDI 0x00000002
15670 +#define NDIS_MEDIUM_WAN 0x00000003
15671 +#define NDIS_MEDIUM_LOCAL_TALK 0x00000004
15672 +#define NDIS_MEDIUM_DIX 0x00000005
15673 +#define NDIS_MEDIUM_ARCENT_RAW 0x00000006
15674 +#define NDIS_MEDIUM_ARCENT_878_2 0x00000007
15675 +#define NDIS_MEDIUM_ATM 0x00000008
15676 +#define NDIS_MEDIUM_WIRELESS_LAN 0x00000009
15677 +#define NDIS_MEDIUM_IRDA 0x0000000A
15678 +#define NDIS_MEDIUM_BPC 0x0000000B
15679 +#define NDIS_MEDIUM_CO_WAN 0x0000000C
15680 +#define NDIS_MEDIUM_1394 0x0000000D
15682 +#define NDIS_PACKET_TYPE_DIRECTED 0x00000001
15683 +#define NDIS_PACKET_TYPE_MULTICAST 0x00000002
15684 +#define NDIS_PACKET_TYPE_ALL_MULTICAST 0x00000004
15685 +#define NDIS_PACKET_TYPE_BROADCAST 0x00000008
15686 +#define NDIS_PACKET_TYPE_SOURCE_ROUTING 0x00000010
15687 +#define NDIS_PACKET_TYPE_PROMISCUOUS 0x00000020
15688 +#define NDIS_PACKET_TYPE_SMT 0x00000040
15689 +#define NDIS_PACKET_TYPE_ALL_LOCAL 0x00000080
15690 +#define NDIS_PACKET_TYPE_GROUP 0x00000100
15691 +#define NDIS_PACKET_TYPE_ALL_FUNCTIONAL 0x00000200
15692 +#define NDIS_PACKET_TYPE_FUNCTIONAL 0x00000400
15693 +#define NDIS_PACKET_TYPE_MAC_FRAME 0x00000800
15695 +#define NDIS_MEDIA_STATE_CONNECTED 0x00000000
15696 +#define NDIS_MEDIA_STATE_DISCONNECTED 0x00000001
15698 +#define NDIS_MAC_OPTION_COPY_LOOKAHEAD_DATA 0x00000001
15699 +#define NDIS_MAC_OPTION_RECEIVE_SERIALIZED 0x00000002
15700 +#define NDIS_MAC_OPTION_TRANSFERS_NOT_PEND 0x00000004
15701 +#define NDIS_MAC_OPTION_NO_LOOPBACK 0x00000008
15702 +#define NDIS_MAC_OPTION_FULL_DUPLEX 0x00000010
15703 +#define NDIS_MAC_OPTION_EOTX_INDICATION 0x00000020
15704 +#define NDIS_MAC_OPTION_8021P_PRIORITY 0x00000040
15705 +#define NDIS_MAC_OPTION_RESERVED 0x80000000
15707 +#endif /* _LINUX_NDIS_H */
15708 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/net2280.c kernel/drivers/usb/gadget/net2280.c
15709 --- /tmp/kernel/drivers/usb/gadget/net2280.c 1970-01-01 01:00:00.000000000 +0100
15710 +++ kernel/drivers/usb/gadget/net2280.c 2005-04-22 17:53:19.478532352 +0200
15711 @@ -0,0 +1,2918 @@
15713 + * Driver for the NetChip 2280 USB device controller.
15714 + * Specs and errata are available from <http://www.netchip.com>.
15716 + * NetChip Technology Inc. supported the development of this driver.
15719 + * CODE STATUS HIGHLIGHTS
15721 + * This driver should work well with most "gadget" drivers, including
15722 + * the File Storage, Serial, and Ethernet/RNDIS gadget drivers
15723 + * as well as Gadget Zero and Gadgetfs.
15725 + * DMA is enabled by default. Drivers using transfer queues might use
15726 + * DMA chaining to remove IRQ latencies between transfers. (Except when
15727 + * short OUT transfers happen.) Drivers can use the req->no_interrupt
15728 + * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
15729 + * and DMA chaining is enabled.
15731 + * Note that almost all the errata workarounds here are only needed for
15732 + * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
15733 + */
15736 + * Copyright (C) 2003 David Brownell
15737 + * Copyright (C) 2003 NetChip Technologies
15739 + * This program is free software; you can redistribute it and/or modify
15740 + * it under the terms of the GNU General Public License as published by
15741 + * the Free Software Foundation; either version 2 of the License, or
15742 + * (at your option) any later version.
15744 + * This program is distributed in the hope that it will be useful,
15745 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15746 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15747 + * GNU General Public License for more details.
15749 + * You should have received a copy of the GNU General Public License
15750 + * along with this program; if not, write to the Free Software
15751 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15752 + */
15754 +#undef DEBUG /* messages on error and most fault paths */
15755 +#undef VERBOSE /* extra debug messages (success too) */
15757 +#include <linux/config.h>
15758 +#include <linux/module.h>
15759 +#include <linux/pci.h>
15760 +#include <linux/kernel.h>
15761 +#include <linux/delay.h>
15762 +#include <linux/ioport.h>
15763 +#include <linux/sched.h>
15764 +#include <linux/slab.h>
15765 +#include <linux/smp_lock.h>
15766 +#include <linux/errno.h>
15767 +#include <linux/init.h>
15768 +#include <linux/timer.h>
15769 +#include <linux/list.h>
15770 +#include <linux/interrupt.h>
15772 +#include <linux/usb_ch9.h>
15773 +#include <linux/usb_gadget.h>
15775 +#include <asm/byteorder.h>
15776 +#include <asm/io.h>
15777 +#include <asm/irq.h>
15778 +#include <asm/system.h>
15779 +#include <asm/unaligned.h>
15782 +#define DRIVER_DESC "NetChip 2280 USB Peripheral Controller"
15783 +#define DRIVER_VERSION "2004 Jan 14"
15785 +#define DMA_ADDR_INVALID (~(dma_addr_t)0)
15786 +#define EP_DONTUSE 13 /* nonzero */
15788 +#define USE_RDK_LEDS /* GPIO pins control three LEDs */
15791 +static const char driver_name [] = "net2280";
15792 +static const char driver_desc [] = DRIVER_DESC;
15794 +static const char ep0name [] = "ep0";
15795 +static const char *ep_name [] = {
15796 + ep0name,
15797 + "ep-a", "ep-b", "ep-c", "ep-d",
15798 + "ep-e", "ep-f",
15801 +/* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
15802 + * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
15804 + * The net2280 DMA engines are not tightly integrated with their FIFOs;
15805 + * not all cases are (yet) handled well in this driver or the silicon.
15806 + * Some gadget drivers work better with the dma support here than others.
15807 + * These two parameters let you use PIO or more aggressive DMA.
15808 + */
15809 +static int use_dma = 1;
15810 +static int use_dma_chaining = 0;
15812 +MODULE_PARM (use_dma, "i");
15813 +MODULE_PARM_DESC (use_dma, "true to use dma controllers");
15815 +MODULE_PARM (use_dma_chaining, "i");
15816 +MODULE_PARM_DESC (use_dma_chaining, "true to use dma descriptor queues");
15819 +/* mode 0 == ep-{a,b,c,d} 1K fifo each
15820 + * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
15821 + * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
15822 + */
15823 +static ushort fifo_mode = 0;
15825 +MODULE_PARM (fifo_mode, "h");
15826 +MODULE_PARM_DESC (fifo_mode, "net2280 fifo mode");
15829 +#define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
15831 +#if defined(USE_SYSFS_DEBUG_FILES) || defined (DEBUG)
15832 +static char *type_string (u8 bmAttributes)
15834 + switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
15835 + case USB_ENDPOINT_XFER_BULK: return "bulk";
15836 + case USB_ENDPOINT_XFER_ISOC: return "iso";
15837 + case USB_ENDPOINT_XFER_INT: return "intr";
15838 + };
15839 + return "control";
15841 +#endif
15843 +#include "net2280.h"
15845 +#define valid_bit __constant_cpu_to_le32 (1 << VALID_BIT)
15846 +#define dma_done_ie __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
15848 +/*-------------------------------------------------------------------------*/
15850 +static int
15851 +net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
15853 + struct net2280 *dev;
15854 + struct net2280_ep *ep;
15855 + u32 max, tmp;
15856 + unsigned long flags;
15858 + ep = container_of (_ep, struct net2280_ep, ep);
15859 + if (!_ep || !desc || ep->desc || _ep->name == ep0name
15860 + || desc->bDescriptorType != USB_DT_ENDPOINT)
15861 + return -EINVAL;
15862 + dev = ep->dev;
15863 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
15864 + return -ESHUTDOWN;
15866 + /* erratum 0119 workaround ties up an endpoint number */
15867 + if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
15868 + return -EDOM;
15870 + /* sanity check ep-e/ep-f since their fifos are small */
15871 + max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
15872 + if (ep->num > 4 && max > 64)
15873 + return -ERANGE;
15875 + spin_lock_irqsave (&dev->lock, flags);
15876 + _ep->maxpacket = max & 0x7ff;
15877 + ep->desc = desc;
15879 + /* ep_reset() has already been called */
15880 + ep->stopped = 0;
15881 + ep->out_overflow = 0;
15883 + /* set speed-dependent max packet; may kick in high bandwidth */
15884 + set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
15886 + /* FIFO lines can't go to different packets. PIO is ok, so
15887 + * use it instead of troublesome (non-bulk) multi-packet DMA.
15888 + */
15889 + if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
15890 + DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
15891 + ep->ep.name, ep->ep.maxpacket);
15892 + ep->dma = NULL;
15895 + /* set type, direction, address; reset fifo counters */
15896 + writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
15897 + tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
15898 + if (tmp == USB_ENDPOINT_XFER_INT) {
15899 + /* erratum 0105 workaround prevents hs NYET */
15900 + if (dev->chiprev == 0100
15901 + && dev->gadget.speed == USB_SPEED_HIGH
15902 + && !(desc->bEndpointAddress & USB_DIR_IN))
15903 + writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
15904 + &ep->regs->ep_rsp);
15905 + } else if (tmp == USB_ENDPOINT_XFER_BULK) {
15906 + /* catch some particularly blatant driver bugs */
15907 + if ((dev->gadget.speed == USB_SPEED_HIGH
15908 + && max != 512)
15909 + || (dev->gadget.speed == USB_SPEED_FULL
15910 + && max > 64)) {
15911 + spin_unlock_irqrestore (&dev->lock, flags);
15912 + return -ERANGE;
15915 + ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
15916 + tmp <<= ENDPOINT_TYPE;
15917 + tmp |= desc->bEndpointAddress;
15918 + tmp |= (4 << ENDPOINT_BYTE_COUNT); /* default full fifo lines */
15919 + tmp |= 1 << ENDPOINT_ENABLE;
15920 + wmb ();
15922 + /* for OUT transfers, block the rx fifo until a read is posted */
15923 + ep->is_in = (tmp & USB_DIR_IN) != 0;
15924 + if (!ep->is_in)
15925 + writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
15927 + writel (tmp, &ep->regs->ep_cfg);
15929 + /* enable irqs */
15930 + if (!ep->dma) { /* pio, per-packet */
15931 + tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
15932 + writel (tmp, &dev->regs->pciirqenb0);
15934 + tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
15935 + | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
15936 + | readl (&ep->regs->ep_irqenb);
15937 + writel (tmp, &ep->regs->ep_irqenb);
15938 + } else { /* dma, per-request */
15939 + tmp = (1 << (8 + ep->num)); /* completion */
15940 + tmp |= readl (&dev->regs->pciirqenb1);
15941 + writel (tmp, &dev->regs->pciirqenb1);
15943 + /* for short OUT transfers, dma completions can't
15944 + * advance the queue; do it pio-style, by hand.
15945 + * NOTE erratum 0112 workaround #2
15946 + */
15947 + if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
15948 + tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
15949 + writel (tmp, &ep->regs->ep_irqenb);
15951 + tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
15952 + writel (tmp, &dev->regs->pciirqenb0);
15956 + tmp = desc->bEndpointAddress;
15957 + DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
15958 + _ep->name, tmp & 0x0f, DIR_STRING (tmp),
15959 + type_string (desc->bmAttributes),
15960 + ep->dma ? "dma" : "pio", max);
15962 + /* pci writes may still be posted */
15963 + spin_unlock_irqrestore (&dev->lock, flags);
15964 + return 0;
15967 +static int handshake (u32 *ptr, u32 mask, u32 done, int usec)
15969 + u32 result;
15971 + do {
15972 + result = readl (ptr);
15973 + if (result == ~(u32)0) /* "device unplugged" */
15974 + return -ENODEV;
15975 + result &= mask;
15976 + if (result == done)
15977 + return 0;
15978 + udelay (1);
15979 + usec--;
15980 + } while (usec > 0);
15981 + return -ETIMEDOUT;
15984 +static struct usb_ep_ops net2280_ep_ops;
15986 +static void ep_reset (struct net2280_regs *regs, struct net2280_ep *ep)
15988 + u32 tmp;
15990 + ep->desc = NULL;
15991 + INIT_LIST_HEAD (&ep->queue);
15993 + ep->ep.maxpacket = ~0;
15994 + ep->ep.ops = &net2280_ep_ops;
15996 + /* disable the dma, irqs, endpoint... */
15997 + if (ep->dma) {
15998 + writel (0, &ep->dma->dmactl);
15999 + writel ( (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
16000 + | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
16001 + | (1 << DMA_ABORT)
16002 + , &ep->dma->dmastat);
16004 + tmp = readl (&regs->pciirqenb0);
16005 + tmp &= ~(1 << ep->num);
16006 + writel (tmp, &regs->pciirqenb0);
16007 + } else {
16008 + tmp = readl (&regs->pciirqenb1);
16009 + tmp &= ~(1 << (8 + ep->num)); /* completion */
16010 + writel (tmp, &regs->pciirqenb1);
16012 + writel (0, &ep->regs->ep_irqenb);
16014 + /* init to our chosen defaults, notably so that we NAK OUT
16015 + * packets until the driver queues a read (+note erratum 0112)
16016 + */
16017 + writel ( (1 << SET_NAK_OUT_PACKETS_MODE)
16018 + | (1 << SET_NAK_OUT_PACKETS)
16019 + | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
16020 + | (1 << CLEAR_INTERRUPT_MODE)
16021 + | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
16022 + | (1 << CLEAR_ENDPOINT_TOGGLE)
16023 + | (1 << CLEAR_ENDPOINT_HALT)
16024 + , &ep->regs->ep_rsp);
16026 + /* scrub most status bits, and flush any fifo state */
16027 + writel ( (1 << TIMEOUT)
16028 + | (1 << USB_STALL_SENT)
16029 + | (1 << USB_IN_NAK_SENT)
16030 + | (1 << USB_IN_ACK_RCVD)
16031 + | (1 << USB_OUT_PING_NAK_SENT)
16032 + | (1 << USB_OUT_ACK_SENT)
16033 + | (1 << FIFO_OVERFLOW)
16034 + | (1 << FIFO_UNDERFLOW)
16035 + | (1 << FIFO_FLUSH)
16036 + | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
16037 + | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
16038 + | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
16039 + | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
16040 + | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
16041 + | (1 << DATA_IN_TOKEN_INTERRUPT)
16042 + , &ep->regs->ep_stat);
16044 + /* fifo size is handled separately */
16047 +static void nuke (struct net2280_ep *);
16049 +static int net2280_disable (struct usb_ep *_ep)
16051 + struct net2280_ep *ep;
16052 + unsigned long flags;
16054 + ep = container_of (_ep, struct net2280_ep, ep);
16055 + if (!_ep || !ep->desc || _ep->name == ep0name)
16056 + return -EINVAL;
16058 + spin_lock_irqsave (&ep->dev->lock, flags);
16059 + nuke (ep);
16060 + ep_reset (ep->dev->regs, ep);
16062 + VDEBUG (ep->dev, "disabled %s %s\n",
16063 + ep->dma ? "dma" : "pio", _ep->name);
16065 + /* synch memory views with the device */
16066 + (void) readl (&ep->regs->ep_cfg);
16068 + if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
16069 + ep->dma = &ep->dev->dma [ep->num - 1];
16071 + spin_unlock_irqrestore (&ep->dev->lock, flags);
16072 + return 0;
16075 +/*-------------------------------------------------------------------------*/
16077 +static struct usb_request *
16078 +net2280_alloc_request (struct usb_ep *_ep, int gfp_flags)
16080 + struct net2280_ep *ep;
16081 + struct net2280_request *req;
16083 + if (!_ep)
16084 + return NULL;
16085 + ep = container_of (_ep, struct net2280_ep, ep);
16087 + req = kmalloc (sizeof *req, gfp_flags);
16088 + if (!req)
16089 + return NULL;
16091 + memset (req, 0, sizeof *req);
16092 + req->req.dma = DMA_ADDR_INVALID;
16093 + INIT_LIST_HEAD (&req->queue);
16095 + /* this dma descriptor may be swapped with the previous dummy */
16096 + if (ep->dma) {
16097 + struct net2280_dma *td;
16099 + td = pci_pool_alloc (ep->dev->requests, gfp_flags,
16100 + &req->td_dma);
16101 + if (!td) {
16102 + kfree (req);
16103 + return NULL;
16105 + td->dmacount = 0; /* not VALID */
16106 + td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
16107 + td->dmadesc = td->dmaaddr;
16108 + req->td = td;
16110 + return &req->req;
16113 +static void
16114 +net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
16116 + struct net2280_ep *ep;
16117 + struct net2280_request *req;
16119 + ep = container_of (_ep, struct net2280_ep, ep);
16120 + if (!_ep || !_req)
16121 + return;
16123 + req = container_of (_req, struct net2280_request, req);
16124 + WARN_ON (!list_empty (&req->queue));
16125 + if (req->td)
16126 + pci_pool_free (ep->dev->requests, req->td, req->td_dma);
16127 + kfree (req);
16130 +/*-------------------------------------------------------------------------*/
16132 +#undef USE_KMALLOC
16134 +/* many common platforms have dma-coherent caches, which means that it's
16135 + * safe to use kmalloc() memory for all i/o buffers without using any
16136 + * cache flushing calls. (unless you're trying to share cache lines
16137 + * between dma and non-dma activities, which is a slow idea in any case.)
16139 + * other platforms need more care, with 2.5 having a moderately general
16140 + * solution (which falls down for allocations smaller than one page)
16141 + * that improves significantly on the 2.4 PCI allocators by removing
16142 + * the restriction that memory never be freed in_interrupt().
16143 + */
16144 +#if defined(CONFIG_X86)
16145 +#define USE_KMALLOC
16147 +#elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
16148 +#define USE_KMALLOC
16150 +#elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
16151 +#define USE_KMALLOC
16153 +/* FIXME there are other cases, including an x86-64 one ... */
16154 +#endif
16156 +/* allocating buffers this way eliminates dma mapping overhead, which
16157 + * on some platforms will mean eliminating a per-io buffer copy. with
16158 + * some kinds of system caches, further tweaks may still be needed.
16159 + */
16160 +static void *
16161 +net2280_alloc_buffer (
16162 + struct usb_ep *_ep,
16163 + unsigned bytes,
16164 + dma_addr_t *dma,
16165 + int gfp_flags
16168 + void *retval;
16169 + struct net2280_ep *ep;
16171 + ep = container_of (_ep, struct net2280_ep, ep);
16172 + if (!_ep)
16173 + return NULL;
16174 + *dma = DMA_ADDR_INVALID;
16176 +#if defined(USE_KMALLOC)
16177 + retval = kmalloc(bytes, gfp_flags);
16178 + if (retval)
16179 + *dma = virt_to_phys(retval);
16180 +#else
16181 + if (ep->dma) {
16182 + /* one problem with this call is that it wastes memory on
16183 + * typical 1/N page allocations: it allocates 1..N pages.
16184 + * another is that it always uses GFP_ATOMIC.
16185 + */
16186 +#warning Using pci_alloc_consistent even with buffers smaller than a page.
16187 + retval = pci_alloc_consistent(ep->dev->pdev, bytes, dma);
16188 + } else
16189 + retval = kmalloc(bytes, gfp_flags);
16190 +#endif
16191 + return retval;
16194 +static void
16195 +net2280_free_buffer (
16196 + struct usb_ep *_ep,
16197 + void *buf,
16198 + dma_addr_t dma,
16199 + unsigned bytes
16200 +) {
16201 + /* free memory into the right allocator */
16202 +#ifndef USE_KMALLOC
16203 + if (dma != DMA_ADDR_INVALID) {
16204 + struct net2280_ep *ep;
16206 + ep = container_of(_ep, struct net2280_ep, ep);
16207 + if (!_ep)
16208 + return;
16209 + /* one problem with this call is that some platforms
16210 + * don't allow it to be used in_irq().
16211 + */
16212 + pci_free_consistent(ep->dev->pdev, bytes, buf, dma);
16213 + } else
16214 +#endif
16215 + kfree (buf);
16218 +/*-------------------------------------------------------------------------*/
16220 +/* load a packet into the fifo we use for usb IN transfers.
16221 + * works for all endpoints.
16223 + * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
16224 + * at a time, but this code is simpler because it knows it only writes
16225 + * one packet. ep-a..ep-d should use dma instead.
16226 + */
16227 +static void
16228 +write_fifo (struct net2280_ep *ep, struct usb_request *req)
16230 + struct net2280_ep_regs *regs = ep->regs;
16231 + u8 *buf;
16232 + u32 tmp;
16233 + unsigned count, total;
16235 + /* INVARIANT: fifo is currently empty. (testable) */
16237 + if (req) {
16238 + buf = req->buf + req->actual;
16239 + prefetch (buf);
16240 + total = req->length - req->actual;
16241 + } else {
16242 + total = 0;
16243 + buf = NULL;
16246 + /* write just one packet at a time */
16247 + count = ep->ep.maxpacket;
16248 + if (count > total) /* min() cannot be used on a bitfield */
16249 + count = total;
16251 + VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
16252 + ep->ep.name, count,
16253 + (count != ep->ep.maxpacket) ? " (short)" : "",
16254 + req);
16255 + while (count >= 4) {
16256 + /* NOTE be careful if you try to align these. fifo lines
16257 + * should normally be full (4 bytes) and successive partial
16258 + * lines are ok only in certain cases.
16259 + */
16260 + tmp = get_unaligned ((u32 *)buf);
16261 + cpu_to_le32s (&tmp);
16262 + writel (tmp, &regs->ep_data);
16263 + buf += 4;
16264 + count -= 4;
16267 + /* last fifo entry is "short" unless we wrote a full packet.
16268 + * also explicitly validate last word in (periodic) transfers
16269 + * when maxpacket is not a multiple of 4 bytes.
16270 + */
16271 + if (count || total < ep->ep.maxpacket) {
16272 + tmp = count ? get_unaligned ((u32 *)buf) : count;
16273 + cpu_to_le32s (&tmp);
16274 + set_fifo_bytecount (ep, count & 0x03);
16275 + writel (tmp, &regs->ep_data);
16278 + /* pci writes may still be posted */
16281 +/* work around erratum 0106: PCI and USB race over the OUT fifo.
16282 + * caller guarantees chiprev 0100, out endpoint is NAKing, and
16283 + * there's no real data in the fifo.
16285 + * NOTE: also used in cases where that erratum doesn't apply:
16286 + * where the host wrote "too much" data to us.
16287 + */
16288 +static void out_flush (struct net2280_ep *ep)
16290 + u32 *statp, tmp;
16292 + ASSERT_OUT_NAKING (ep);
16294 + statp = &ep->regs->ep_stat;
16295 + writel ( (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
16296 + | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
16297 + , statp);
16298 + writel ((1 << FIFO_FLUSH), statp);
16299 + mb ();
16300 + tmp = readl (statp);
16301 + if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
16302 + /* high speed did bulk NYET; fifo isn't filling */
16303 + && ep->dev->gadget.speed == USB_SPEED_FULL) {
16304 + unsigned usec;
16306 + usec = 50; /* 64 byte bulk/interrupt */
16307 + handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
16308 + (1 << USB_OUT_PING_NAK_SENT), usec);
16309 + /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
16313 +/* unload packet(s) from the fifo we use for usb OUT transfers.
16314 + * returns true iff the request completed, because of short packet
16315 + * or the request buffer having filled with full packets.
16317 + * for ep-a..ep-d this will read multiple packets out when they
16318 + * have been accepted.
16319 + */
16320 +static int
16321 +read_fifo (struct net2280_ep *ep, struct net2280_request *req)
16323 + struct net2280_ep_regs *regs = ep->regs;
16324 + u8 *buf = req->req.buf + req->req.actual;
16325 + unsigned count, tmp, is_short;
16326 + unsigned cleanup = 0, prevent = 0;
16328 + /* erratum 0106 ... packets coming in during fifo reads might
16329 + * be incompletely rejected. not all cases have workarounds.
16330 + */
16331 + if (ep->dev->chiprev == 0x0100
16332 + && ep->dev->gadget.speed == USB_SPEED_FULL) {
16333 + udelay (1);
16334 + tmp = readl (&ep->regs->ep_stat);
16335 + if ((tmp & (1 << NAK_OUT_PACKETS)))
16336 + cleanup = 1;
16337 + else if ((tmp & (1 << FIFO_FULL))) {
16338 + start_out_naking (ep);
16339 + prevent = 1;
16341 + /* else: hope we don't see the problem */
16344 + /* never overflow the rx buffer. the fifo reads packets until
16345 + * it sees a short one; we might not be ready for them all.
16346 + */
16347 + prefetchw (buf);
16348 + count = readl (&regs->ep_avail);
16349 + if (unlikely (count == 0)) {
16350 + udelay (1);
16351 + tmp = readl (&ep->regs->ep_stat);
16352 + count = readl (&regs->ep_avail);
16353 + /* handled that data already? */
16354 + if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
16355 + return 0;
16358 + tmp = req->req.length - req->req.actual;
16359 + if (count > tmp) {
16360 + /* as with DMA, data overflow gets flushed */
16361 + if ((tmp % ep->ep.maxpacket) != 0) {
16362 + ERROR (ep->dev,
16363 + "%s out fifo %d bytes, expected %d\n",
16364 + ep->ep.name, count, tmp);
16365 + req->req.status = -EOVERFLOW;
16366 + cleanup = 1;
16367 + /* NAK_OUT_PACKETS will be set, so flushing is safe;
16368 + * the next read will start with the next packet
16369 + */
16370 + } /* else it's a ZLP, no worries */
16371 + count = tmp;
16373 + req->req.actual += count;
16375 + is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
16377 + VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
16378 + ep->ep.name, count, is_short ? " (short)" : "",
16379 + cleanup ? " flush" : "", prevent ? " nak" : "",
16380 + req, req->req.actual, req->req.length);
16382 + while (count >= 4) {
16383 + tmp = readl (&regs->ep_data);
16384 + cpu_to_le32s (&tmp);
16385 + put_unaligned (tmp, (u32 *)buf);
16386 + buf += 4;
16387 + count -= 4;
16389 + if (count) {
16390 + tmp = readl (&regs->ep_data);
16391 + /* LE conversion is implicit here: */
16392 + do {
16393 + *buf++ = (u8) tmp;
16394 + tmp >>= 8;
16395 + } while (--count);
16397 + if (cleanup)
16398 + out_flush (ep);
16399 + if (prevent) {
16400 + writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
16401 + (void) readl (&ep->regs->ep_rsp);
16404 + return is_short || ((req->req.actual == req->req.length)
16405 + && !req->req.zero);
16408 +/* fill out dma descriptor to match a given request */
16409 +static void
16410 +fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
16412 + struct net2280_dma *td = req->td;
16413 + u32 dmacount = req->req.length;
16415 + /* don't let DMA continue after a short OUT packet,
16416 + * so overruns can't affect the next transfer.
16417 + * in case of overruns on max-size packets, we can't
16418 + * stop the fifo from filling but we can flush it.
16419 + */
16420 + if (ep->is_in)
16421 + dmacount |= (1 << DMA_DIRECTION);
16422 + else if ((dmacount % ep->ep.maxpacket) != 0)
16423 + dmacount |= (1 << END_OF_CHAIN);
16425 + req->valid = valid;
16426 + if (valid)
16427 + dmacount |= (1 << VALID_BIT);
16428 + if (likely(!req->req.no_interrupt || !use_dma_chaining))
16429 + dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
16431 + /* td->dmadesc = previously set by caller */
16432 + td->dmaaddr = cpu_to_le32p (&req->req.dma);
16434 + /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
16435 + wmb ();
16436 + td->dmacount = cpu_to_le32p (&dmacount);
16439 +static const u32 dmactl_default =
16440 + (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
16441 + | (1 << DMA_CLEAR_COUNT_ENABLE)
16442 + /* erratum 0116 workaround part 1 (use POLLING) */
16443 + | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
16444 + | (1 << DMA_VALID_BIT_POLLING_ENABLE)
16445 + | (1 << DMA_VALID_BIT_ENABLE)
16446 + | (1 << DMA_SCATTER_GATHER_ENABLE)
16447 + /* erratum 0116 workaround part 2 (no AUTOSTART) */
16448 + | (1 << DMA_ENABLE);
16450 +static inline void spin_stop_dma (struct net2280_dma_regs *dma)
16452 + handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
16455 +static inline void stop_dma (struct net2280_dma_regs *dma)
16457 + writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
16458 + spin_stop_dma (dma);
16461 +static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
16463 + struct net2280_dma_regs *dma = ep->dma;
16465 + writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
16466 + &dma->dmacount);
16467 + writel (readl (&dma->dmastat), &dma->dmastat);
16469 + writel (td_dma, &dma->dmadesc);
16470 + writel (dmactl, &dma->dmactl);
16472 + /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
16473 + (void) readl (&ep->dev->pci->pcimstctl);
16475 + writel ((1 << DMA_START), &dma->dmastat);
16477 + if (!ep->is_in)
16478 + stop_out_naking (ep);
16481 +static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
16483 + u32 tmp;
16484 + struct net2280_dma_regs *dma = ep->dma;
16486 + /* FIXME can't use DMA for ZLPs */
16488 + /* on this path we "know" there's no dma active (yet) */
16489 + WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
16490 + writel (0, &ep->dma->dmactl);
16492 + /* previous OUT packet might have been short */
16493 + if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
16494 + & (1 << NAK_OUT_PACKETS)) != 0) {
16495 + writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
16496 + &ep->regs->ep_stat);
16498 + tmp = readl (&ep->regs->ep_avail);
16499 + if (tmp) {
16500 + writel (readl (&dma->dmastat), &dma->dmastat);
16502 + /* transfer all/some fifo data */
16503 + writel (req->req.dma, &dma->dmaaddr);
16504 + tmp = min (tmp, req->req.length);
16506 + /* dma irq, faking scatterlist status */
16507 + req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
16508 + writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
16509 + | tmp, &dma->dmacount);
16510 + req->td->dmadesc = 0;
16511 + req->valid = 1;
16513 + writel ((1 << DMA_ENABLE), &dma->dmactl);
16514 + writel ((1 << DMA_START), &dma->dmastat);
16515 + return;
16519 + tmp = dmactl_default;
16521 + /* force packet boundaries between dma requests, but prevent the
16522 + * controller from automagically writing a last "short" packet
16523 + * (zero length) unless the driver explicitly said to do that.
16524 + */
16525 + if (ep->is_in) {
16526 + if (likely ((req->req.length % ep->ep.maxpacket) != 0
16527 + || req->req.zero)) {
16528 + tmp |= (1 << DMA_FIFO_VALIDATE);
16529 + ep->in_fifo_validate = 1;
16530 + } else
16531 + ep->in_fifo_validate = 0;
16534 + /* init req->td, pointing to the current dummy */
16535 + req->td->dmadesc = cpu_to_le32 (ep->td_dma);
16536 + fill_dma_desc (ep, req, 1);
16538 + if (!use_dma_chaining)
16539 + req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
16541 + start_queue (ep, tmp, req->td_dma);
16544 +static inline void
16545 +queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
16547 + struct net2280_dma *end;
16548 + dma_addr_t tmp;
16550 + /* swap new dummy for old, link; fill and maybe activate */
16551 + end = ep->dummy;
16552 + ep->dummy = req->td;
16553 + req->td = end;
16555 + tmp = ep->td_dma;
16556 + ep->td_dma = req->td_dma;
16557 + req->td_dma = tmp;
16559 + end->dmadesc = cpu_to_le32 (ep->td_dma);
16561 + fill_dma_desc (ep, req, valid);
16564 +static void
16565 +done (struct net2280_ep *ep, struct net2280_request *req, int status)
16567 + struct net2280 *dev;
16568 + unsigned stopped = ep->stopped;
16570 + list_del_init (&req->queue);
16572 + if (req->req.status == -EINPROGRESS)
16573 + req->req.status = status;
16574 + else
16575 + status = req->req.status;
16577 + dev = ep->dev;
16578 + if (req->mapped) {
16579 + pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
16580 + ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
16581 + req->req.dma = DMA_ADDR_INVALID;
16582 + req->mapped = 0;
16585 + if (status && status != -ESHUTDOWN)
16586 + VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
16587 + ep->ep.name, &req->req, status,
16588 + req->req.actual, req->req.length);
16590 + /* don't modify queue heads during completion callback */
16591 + ep->stopped = 1;
16592 + spin_unlock (&dev->lock);
16593 + req->req.complete (&ep->ep, &req->req);
16594 + spin_lock (&dev->lock);
16595 + ep->stopped = stopped;
16598 +/*-------------------------------------------------------------------------*/
16600 +static int
16601 +net2280_queue (struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
16603 + struct net2280_request *req;
16604 + struct net2280_ep *ep;
16605 + struct net2280 *dev;
16606 + unsigned long flags;
16608 + /* we always require a cpu-view buffer, so that we can
16609 + * always use pio (as fallback or whatever).
16610 + */
16611 + req = container_of (_req, struct net2280_request, req);
16612 + if (!_req || !_req->complete || !_req->buf
16613 + || !list_empty (&req->queue))
16614 + return -EINVAL;
16615 + if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
16616 + return -EDOM;
16617 + ep = container_of (_ep, struct net2280_ep, ep);
16618 + if (!_ep || (!ep->desc && ep->num != 0))
16619 + return -EINVAL;
16620 + dev = ep->dev;
16621 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
16622 + return -ESHUTDOWN;
16624 + /* FIXME implement PIO fallback for ZLPs with DMA */
16625 + if (ep->dma && _req->length == 0)
16626 + return -EOPNOTSUPP;
16628 + /* set up dma mapping in case the caller didn't */
16629 + if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
16630 + _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
16631 + ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
16632 + req->mapped = 1;
16635 +#if 0
16636 + VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
16637 + _ep->name, _req, _req->length, _req->buf);
16638 +#endif
16640 + spin_lock_irqsave (&dev->lock, flags);
16642 + _req->status = -EINPROGRESS;
16643 + _req->actual = 0;
16645 + /* kickstart this i/o queue? */
16646 + if (list_empty (&ep->queue) && !ep->stopped) {
16647 + /* use DMA if the endpoint supports it, else pio */
16648 + if (ep->dma)
16649 + start_dma (ep, req);
16650 + else {
16651 + /* maybe there's no control data, just status ack */
16652 + if (ep->num == 0 && _req->length == 0) {
16653 + allow_status (ep);
16654 + done (ep, req, 0);
16655 + VDEBUG (dev, "%s status ack\n", ep->ep.name);
16656 + goto done;
16659 + /* PIO ... stuff the fifo, or unblock it. */
16660 + if (ep->is_in)
16661 + write_fifo (ep, _req);
16662 + else if (list_empty (&ep->queue)) {
16663 + u32 s;
16665 + /* OUT FIFO might have packet(s) buffered */
16666 + s = readl (&ep->regs->ep_stat);
16667 + if ((s & (1 << FIFO_EMPTY)) == 0) {
16668 + /* note: _req->short_not_ok is
16669 + * ignored here since PIO _always_
16670 + * stops queue advance here, and
16671 + * _req->status doesn't change for
16672 + * short reads (only _req->actual)
16673 + */
16674 + if (read_fifo (ep, req)) {
16675 + done (ep, req, 0);
16676 + if (ep->num == 0)
16677 + allow_status (ep);
16678 + /* don't queue it */
16679 + req = NULL;
16680 + } else
16681 + s = readl (&ep->regs->ep_stat);
16684 + /* don't NAK, let the fifo fill */
16685 + if (req && (s & (1 << NAK_OUT_PACKETS)))
16686 + writel ((1 << CLEAR_NAK_OUT_PACKETS),
16687 + &ep->regs->ep_rsp);
16691 + } else if (ep->dma) {
16692 + int valid = 1;
16694 + if (ep->is_in) {
16695 + int expect;
16697 + /* preventing magic zlps is per-engine state, not
16698 + * per-transfer; irq logic must recover hiccups.
16699 + */
16700 + expect = likely (req->req.zero
16701 + || (req->req.length % ep->ep.maxpacket) != 0);
16702 + if (expect != ep->in_fifo_validate)
16703 + valid = 0;
16705 + queue_dma (ep, req, valid);
16707 + } /* else the irq handler advances the queue. */
16709 + if (req)
16710 + list_add_tail (&req->queue, &ep->queue);
16711 +done:
16712 + spin_unlock_irqrestore (&dev->lock, flags);
16714 + /* pci writes may still be posted */
16715 + return 0;
16718 +static inline void
16719 +dma_done (
16720 + struct net2280_ep *ep,
16721 + struct net2280_request *req,
16722 + u32 dmacount,
16723 + int status
16726 + req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
16727 + done (ep, req, status);
16730 +static void restart_dma (struct net2280_ep *ep);
16732 +static void scan_dma_completions (struct net2280_ep *ep)
16734 + /* only look at descriptors that were "naturally" retired,
16735 + * so fifo and list head state won't matter
16736 + */
16737 + while (!list_empty (&ep->queue)) {
16738 + struct net2280_request *req;
16739 + u32 tmp;
16741 + req = list_entry (ep->queue.next,
16742 + struct net2280_request, queue);
16743 + if (!req->valid)
16744 + break;
16745 + rmb ();
16746 + tmp = le32_to_cpup (&req->td->dmacount);
16747 + if ((tmp & (1 << VALID_BIT)) != 0)
16748 + break;
16750 + /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
16751 + * cases where DMA must be aborted; this code handles
16752 + * all non-abort DMA completions.
16753 + */
16754 + if (unlikely (req->td->dmadesc == 0)) {
16755 + /* paranoia */
16756 + tmp = readl (&ep->dma->dmacount);
16757 + if (tmp & DMA_BYTE_COUNT_MASK)
16758 + break;
16759 + /* single transfer mode */
16760 + dma_done (ep, req, tmp, 0);
16761 + break;
16762 + } else if (!ep->is_in
16763 + && (req->req.length % ep->ep.maxpacket) != 0) {
16764 + tmp = readl (&ep->regs->ep_stat);
16766 + /* AVOID TROUBLE HERE by not issuing short reads from
16767 + * your gadget driver. That helps avoids errata 0121,
16768 + * 0122, and 0124; not all cases trigger the warning.
16769 + */
16770 + if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
16771 + WARN (ep->dev, "%s lost packet sync!\n",
16772 + ep->ep.name);
16773 + req->req.status = -EOVERFLOW;
16774 + } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
16775 + /* fifo gets flushed later */
16776 + ep->out_overflow = 1;
16777 + DEBUG (ep->dev, "%s dma, discard %d len %d\n",
16778 + ep->ep.name, tmp,
16779 + req->req.length);
16780 + req->req.status = -EOVERFLOW;
16783 + dma_done (ep, req, tmp, 0);
16787 +static void restart_dma (struct net2280_ep *ep)
16789 + struct net2280_request *req;
16790 + u32 dmactl = dmactl_default;
16792 + if (ep->stopped)
16793 + return;
16794 + req = list_entry (ep->queue.next, struct net2280_request, queue);
16796 + if (!use_dma_chaining) {
16797 + start_dma (ep, req);
16798 + return;
16801 + /* the 2280 will be processing the queue unless queue hiccups after
16802 + * the previous transfer:
16803 + * IN: wanted automagic zlp, head doesn't (or vice versa)
16804 + * DMA_FIFO_VALIDATE doesn't init from dma descriptors.
16805 + * OUT: was "usb-short", we must restart.
16806 + */
16807 + if (ep->is_in && !req->valid) {
16808 + struct net2280_request *entry, *prev = NULL;
16809 + int reqmode, done = 0;
16811 + DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
16812 + ep->in_fifo_validate = likely (req->req.zero
16813 + || (req->req.length % ep->ep.maxpacket) != 0);
16814 + if (ep->in_fifo_validate)
16815 + dmactl |= (1 << DMA_FIFO_VALIDATE);
16816 + list_for_each_entry (entry, &ep->queue, queue) {
16817 + u32 dmacount;
16819 + if (entry == req)
16820 + continue;
16821 + dmacount = entry->td->dmacount;
16822 + if (!done) {
16823 + reqmode = likely (entry->req.zero
16824 + || (entry->req.length
16825 + % ep->ep.maxpacket) != 0);
16826 + if (reqmode == ep->in_fifo_validate) {
16827 + entry->valid = 1;
16828 + dmacount |= valid_bit;
16829 + entry->td->dmacount = dmacount;
16830 + prev = entry;
16831 + continue;
16832 + } else {
16833 + /* force a hiccup */
16834 + prev->td->dmacount |= dma_done_ie;
16835 + done = 1;
16839 + /* walk the rest of the queue so unlinks behave */
16840 + entry->valid = 0;
16841 + dmacount &= ~valid_bit;
16842 + entry->td->dmacount = dmacount;
16843 + prev = entry;
16847 + writel (0, &ep->dma->dmactl);
16848 + start_queue (ep, dmactl, req->td_dma);
16851 +static void abort_dma (struct net2280_ep *ep)
16853 + /* abort the current transfer */
16854 + if (likely (!list_empty (&ep->queue))) {
16855 + /* FIXME work around errata 0121, 0122, 0124 */
16856 + writel ((1 << DMA_ABORT), &ep->dma->dmastat);
16857 + spin_stop_dma (ep->dma);
16858 + } else
16859 + stop_dma (ep->dma);
16860 + scan_dma_completions (ep);
16863 +/* dequeue ALL requests */
16864 +static void nuke (struct net2280_ep *ep)
16866 + struct net2280_request *req;
16868 + /* called with spinlock held */
16869 + ep->stopped = 1;
16870 + if (ep->dma)
16871 + abort_dma (ep);
16872 + while (!list_empty (&ep->queue)) {
16873 + req = list_entry (ep->queue.next,
16874 + struct net2280_request,
16875 + queue);
16876 + done (ep, req, -ESHUTDOWN);
16880 +/* dequeue JUST ONE request */
16881 +static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
16883 + struct net2280_ep *ep;
16884 + struct net2280_request *req;
16885 + unsigned long flags;
16886 + u32 dmactl;
16887 + int stopped;
16889 + ep = container_of (_ep, struct net2280_ep, ep);
16890 + if (!_ep || (!ep->desc && ep->num != 0) || !_req)
16891 + return -EINVAL;
16893 + spin_lock_irqsave (&ep->dev->lock, flags);
16894 + stopped = ep->stopped;
16896 + /* quiesce dma while we patch the queue */
16897 + dmactl = 0;
16898 + ep->stopped = 1;
16899 + if (ep->dma) {
16900 + dmactl = readl (&ep->dma->dmactl);
16901 + /* WARNING erratum 0127 may kick in ... */
16902 + stop_dma (ep->dma);
16903 + scan_dma_completions (ep);
16906 + /* make sure it's still queued on this endpoint */
16907 + list_for_each_entry (req, &ep->queue, queue) {
16908 + if (&req->req == _req)
16909 + break;
16911 + if (&req->req != _req) {
16912 + spin_unlock_irqrestore (&ep->dev->lock, flags);
16913 + return -EINVAL;
16916 + /* queue head may be partially complete. */
16917 + if (ep->queue.next == &req->queue) {
16918 + if (ep->dma) {
16919 + DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
16920 + _req->status = -ECONNRESET;
16921 + abort_dma (ep);
16922 + if (likely (ep->queue.next == &req->queue)) {
16923 + // NOTE: misreports single-transfer mode
16924 + req->td->dmacount = 0; /* invalidate */
16925 + dma_done (ep, req,
16926 + readl (&ep->dma->dmacount),
16927 + -ECONNRESET);
16929 + } else {
16930 + DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
16931 + done (ep, req, -ECONNRESET);
16933 + req = NULL;
16935 + /* patch up hardware chaining data */
16936 + } else if (ep->dma && use_dma_chaining) {
16937 + if (req->queue.prev == ep->queue.next) {
16938 + writel (le32_to_cpu (req->td->dmadesc),
16939 + &ep->dma->dmadesc);
16940 + if (req->td->dmacount & dma_done_ie)
16941 + writel (readl (&ep->dma->dmacount)
16942 + | dma_done_ie,
16943 + &ep->dma->dmacount);
16944 + } else {
16945 + struct net2280_request *prev;
16947 + prev = list_entry (req->queue.prev,
16948 + struct net2280_request, queue);
16949 + prev->td->dmadesc = req->td->dmadesc;
16950 + if (req->td->dmacount & dma_done_ie)
16951 + prev->td->dmacount |= dma_done_ie;
16955 + if (req)
16956 + done (ep, req, -ECONNRESET);
16957 + ep->stopped = stopped;
16959 + if (ep->dma) {
16960 + /* turn off dma on inactive queues */
16961 + if (list_empty (&ep->queue))
16962 + stop_dma (ep->dma);
16963 + else if (!ep->stopped) {
16964 + /* resume current request, or start new one */
16965 + if (req)
16966 + writel (dmactl, &ep->dma->dmactl);
16967 + else
16968 + start_dma (ep, list_entry (ep->queue.next,
16969 + struct net2280_request, queue));
16973 + spin_unlock_irqrestore (&ep->dev->lock, flags);
16974 + return req ? 0 : -EOPNOTSUPP;
16977 +/*-------------------------------------------------------------------------*/
16979 +static int net2280_fifo_status (struct usb_ep *_ep);
16981 +static int
16982 +net2280_set_halt (struct usb_ep *_ep, int value)
16984 + struct net2280_ep *ep;
16985 + unsigned long flags;
16986 + int retval = 0;
16988 + ep = container_of (_ep, struct net2280_ep, ep);
16989 + if (!_ep || (!ep->desc && ep->num != 0))
16990 + return -EINVAL;
16991 + if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
16992 + return -ESHUTDOWN;
16993 + if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
16994 + == USB_ENDPOINT_XFER_ISOC)
16995 + return -EINVAL;
16997 + spin_lock_irqsave (&ep->dev->lock, flags);
16998 + if (!list_empty (&ep->queue))
16999 + retval = -EAGAIN;
17000 + else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
17001 + retval = -EAGAIN;
17002 + else {
17003 + VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
17004 + value ? "set" : "clear");
17005 + /* set/clear, then synch memory views with the device */
17006 + if (value) {
17007 + if (ep->num == 0)
17008 + ep->dev->protocol_stall = 1;
17009 + else
17010 + set_halt (ep);
17011 + } else
17012 + clear_halt (ep);
17013 + (void) readl (&ep->regs->ep_rsp);
17015 + spin_unlock_irqrestore (&ep->dev->lock, flags);
17017 + return retval;
17020 +static int
17021 +net2280_fifo_status (struct usb_ep *_ep)
17023 + struct net2280_ep *ep;
17024 + u32 avail;
17026 + ep = container_of (_ep, struct net2280_ep, ep);
17027 + if (!_ep || (!ep->desc && ep->num != 0))
17028 + return -ENODEV;
17029 + if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
17030 + return -ESHUTDOWN;
17032 + avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
17033 + if (avail > ep->fifo_size)
17034 + return -EOVERFLOW;
17035 + if (ep->is_in)
17036 + avail = ep->fifo_size - avail;
17037 + return avail;
17040 +static void
17041 +net2280_fifo_flush (struct usb_ep *_ep)
17043 + struct net2280_ep *ep;
17045 + ep = container_of (_ep, struct net2280_ep, ep);
17046 + if (!_ep || (!ep->desc && ep->num != 0))
17047 + return;
17048 + if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
17049 + return;
17051 + writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
17052 + (void) readl (&ep->regs->ep_rsp);
17055 +static struct usb_ep_ops net2280_ep_ops = {
17056 + .enable = net2280_enable,
17057 + .disable = net2280_disable,
17059 + .alloc_request = net2280_alloc_request,
17060 + .free_request = net2280_free_request,
17062 + .alloc_buffer = net2280_alloc_buffer,
17063 + .free_buffer = net2280_free_buffer,
17065 + .queue = net2280_queue,
17066 + .dequeue = net2280_dequeue,
17068 + .set_halt = net2280_set_halt,
17069 + .fifo_status = net2280_fifo_status,
17070 + .fifo_flush = net2280_fifo_flush,
17073 +/*-------------------------------------------------------------------------*/
17075 +static int net2280_get_frame (struct usb_gadget *_gadget)
17077 + struct net2280 *dev;
17078 + unsigned long flags;
17079 + u16 retval;
17081 + if (!_gadget)
17082 + return -ENODEV;
17083 + dev = container_of (_gadget, struct net2280, gadget);
17084 + spin_lock_irqsave (&dev->lock, flags);
17085 + retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
17086 + spin_unlock_irqrestore (&dev->lock, flags);
17087 + return retval;
17090 +static int net2280_wakeup (struct usb_gadget *_gadget)
17092 + struct net2280 *dev;
17093 + u32 tmp;
17094 + unsigned long flags;
17096 + if (!_gadget)
17097 + return 0;
17098 + dev = container_of (_gadget, struct net2280, gadget);
17100 + spin_lock_irqsave (&dev->lock, flags);
17101 + tmp = readl (&dev->usb->usbctl);
17102 + if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
17103 + writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
17104 + spin_unlock_irqrestore (&dev->lock, flags);
17106 + /* pci writes may still be posted */
17107 + return 0;
17110 +static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
17112 + struct net2280 *dev;
17113 + u32 tmp;
17114 + unsigned long flags;
17116 + if (!_gadget)
17117 + return 0;
17118 + dev = container_of (_gadget, struct net2280, gadget);
17120 + spin_lock_irqsave (&dev->lock, flags);
17121 + tmp = readl (&dev->usb->usbctl);
17122 + if (value)
17123 + tmp |= (1 << SELF_POWERED_STATUS);
17124 + else
17125 + tmp &= ~(1 << SELF_POWERED_STATUS);
17126 + writel (tmp, &dev->usb->usbctl);
17127 + spin_unlock_irqrestore (&dev->lock, flags);
17129 + return 0;
17132 +static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
17134 + struct net2280 *dev;
17135 + u32 tmp;
17136 + unsigned long flags;
17138 + if (!_gadget)
17139 + return -ENODEV;
17140 + dev = container_of (_gadget, struct net2280, gadget);
17142 + spin_lock_irqsave (&dev->lock, flags);
17143 + tmp = readl (&dev->usb->usbctl);
17144 + dev->softconnect = (is_on != 0);
17145 + if (is_on)
17146 + tmp |= (1 << USB_DETECT_ENABLE);
17147 + else
17148 + tmp &= ~(1 << USB_DETECT_ENABLE);
17149 + writel (tmp, &dev->usb->usbctl);
17150 + spin_unlock_irqrestore (&dev->lock, flags);
17152 + return 0;
17155 +static const struct usb_gadget_ops net2280_ops = {
17156 + .get_frame = net2280_get_frame,
17157 + .wakeup = net2280_wakeup,
17158 + .set_selfpowered = net2280_set_selfpowered,
17159 + .pullup = net2280_pullup,
17162 +/*-------------------------------------------------------------------------*/
17164 +#ifdef USE_SYSFS_DEBUG_FILES
17166 +/* "function" sysfs attribute */
17167 +static ssize_t
17168 +show_function (struct device *_dev, char *buf)
17170 + struct net2280 *dev = dev_get_drvdata (_dev);
17172 + if (!dev->driver
17173 + || !dev->driver->function
17174 + || strlen (dev->driver->function) > PAGE_SIZE)
17175 + return 0;
17176 + return snprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
17178 +static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
17180 +static ssize_t
17181 +show_registers (struct device *_dev, char *buf)
17183 + struct net2280 *dev;
17184 + char *next;
17185 + unsigned size, t;
17186 + unsigned long flags;
17187 + int i;
17188 + u32 t1, t2;
17189 + char *s;
17191 + dev = dev_get_drvdata (_dev);
17192 + next = buf;
17193 + size = PAGE_SIZE;
17194 + spin_lock_irqsave (&dev->lock, flags);
17196 + if (dev->driver)
17197 + s = dev->driver->driver.name;
17198 + else
17199 + s = "(none)";
17201 + /* Main Control Registers */
17202 + t = snprintf (next, size, "%s version " DRIVER_VERSION
17203 + ", chiprev %04x, dma %s\n\n"
17204 + "devinit %03x fifoctl %08x gadget '%s'\n"
17205 + "pci irqenb0 %02x irqenb1 %08x "
17206 + "irqstat0 %04x irqstat1 %08x\n",
17207 + driver_name, dev->chiprev,
17208 + use_dma
17209 + ? (use_dma_chaining ? "chaining" : "enabled")
17210 + : "disabled",
17211 + readl (&dev->regs->devinit),
17212 + readl (&dev->regs->fifoctl),
17213 + s,
17214 + readl (&dev->regs->pciirqenb0),
17215 + readl (&dev->regs->pciirqenb1),
17216 + readl (&dev->regs->irqstat0),
17217 + readl (&dev->regs->irqstat1));
17218 + size -= t;
17219 + next += t;
17221 + /* USB Control Registers */
17222 + t1 = readl (&dev->usb->usbctl);
17223 + t2 = readl (&dev->usb->usbstat);
17224 + if (t1 & (1 << VBUS_PIN)) {
17225 + if (t2 & (1 << HIGH_SPEED))
17226 + s = "high speed";
17227 + else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
17228 + s = "powered";
17229 + else
17230 + s = "full speed";
17231 + /* full speed bit (6) not working?? */
17232 + } else
17233 + s = "not attached";
17234 + t = snprintf (next, size,
17235 + "stdrsp %08x usbctl %08x usbstat %08x "
17236 + "addr 0x%02x (%s)\n",
17237 + readl (&dev->usb->stdrsp), t1, t2,
17238 + readl (&dev->usb->ouraddr), s);
17239 + size -= t;
17240 + next += t;
17242 + /* PCI Master Control Registers */
17244 + /* DMA Control Registers */
17246 + /* Configurable EP Control Registers */
17247 + for (i = 0; i < 7; i++) {
17248 + struct net2280_ep *ep;
17250 + ep = &dev->ep [i];
17251 + if (i && !ep->desc)
17252 + continue;
17254 + t1 = readl (&ep->regs->ep_cfg);
17255 + t2 = readl (&ep->regs->ep_rsp) & 0xff;
17256 + t = snprintf (next, size,
17257 + "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
17258 + "irqenb %02x\n",
17259 + ep->ep.name, t1, t2,
17260 + (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
17261 + ? "NAK " : "",
17262 + (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
17263 + ? "hide " : "",
17264 + (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
17265 + ? "CRC " : "",
17266 + (t2 & (1 << CLEAR_INTERRUPT_MODE))
17267 + ? "interrupt " : "",
17268 + (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
17269 + ? "status " : "",
17270 + (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
17271 + ? "NAKmode " : "",
17272 + (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
17273 + ? "DATA1 " : "DATA0 ",
17274 + (t2 & (1 << CLEAR_ENDPOINT_HALT))
17275 + ? "HALT " : "",
17276 + readl (&ep->regs->ep_irqenb));
17277 + size -= t;
17278 + next += t;
17280 + t = snprintf (next, size,
17281 + "\tstat %08x avail %04x "
17282 + "(ep%d%s-%s)%s\n",
17283 + readl (&ep->regs->ep_stat),
17284 + readl (&ep->regs->ep_avail),
17285 + t1 & 0x0f, DIR_STRING (t1),
17286 + type_string (t1 >> 8),
17287 + ep->stopped ? "*" : "");
17288 + size -= t;
17289 + next += t;
17291 + if (!ep->dma)
17292 + continue;
17294 + t = snprintf (next, size,
17295 + " dma\tctl %08x stat %08x count %08x\n"
17296 + "\taddr %08x desc %08x\n",
17297 + readl (&ep->dma->dmactl),
17298 + readl (&ep->dma->dmastat),
17299 + readl (&ep->dma->dmacount),
17300 + readl (&ep->dma->dmaaddr),
17301 + readl (&ep->dma->dmadesc));
17302 + size -= t;
17303 + next += t;
17307 + /* Indexed Registers */
17308 + // none yet
17310 + /* Statistics */
17311 + t = snprintf (next, size, "\nirqs: ");
17312 + size -= t;
17313 + next += t;
17314 + for (i = 0; i < 7; i++) {
17315 + struct net2280_ep *ep;
17317 + ep = &dev->ep [i];
17318 + if (i && !ep->irqs)
17319 + continue;
17320 + t = snprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
17321 + size -= t;
17322 + next += t;
17325 + t = snprintf (next, size, "\n");
17326 + size -= t;
17327 + next += t;
17329 + spin_unlock_irqrestore (&dev->lock, flags);
17331 + return PAGE_SIZE - size;
17333 +static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
17335 +static ssize_t
17336 +show_queues (struct device *_dev, char *buf)
17338 + struct net2280 *dev;
17339 + char *next;
17340 + unsigned size;
17341 + unsigned long flags;
17342 + int i;
17344 + dev = dev_get_drvdata (_dev);
17345 + next = buf;
17346 + size = PAGE_SIZE;
17347 + spin_lock_irqsave (&dev->lock, flags);
17349 + for (i = 0; i < 7; i++) {
17350 + struct net2280_ep *ep = &dev->ep [i];
17351 + struct net2280_request *req;
17352 + int t;
17354 + if (i != 0) {
17355 + const struct usb_endpoint_descriptor *d;
17357 + d = ep->desc;
17358 + if (!d)
17359 + continue;
17360 + t = d->bEndpointAddress;
17361 + t = snprintf (next, size,
17362 + "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
17363 + ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
17364 + (t & USB_DIR_IN) ? "in" : "out",
17365 + ({ char *val;
17366 + switch (d->bmAttributes & 0x03) {
17367 + case USB_ENDPOINT_XFER_BULK:
17368 + val = "bulk"; break;
17369 + case USB_ENDPOINT_XFER_INT:
17370 + val = "intr"; break;
17371 + default:
17372 + val = "iso"; break;
17373 + }; val; }),
17374 + le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
17375 + ep->dma ? "dma" : "pio", ep->fifo_size
17376 + );
17377 + } else /* ep0 should only have one transfer queued */
17378 + t = snprintf (next, size, "ep0 max 64 pio %s\n",
17379 + ep->is_in ? "in" : "out");
17380 + if (t <= 0 || t > size)
17381 + goto done;
17382 + size -= t;
17383 + next += t;
17385 + if (list_empty (&ep->queue)) {
17386 + t = snprintf (next, size, "\t(nothing queued)\n");
17387 + if (t <= 0 || t > size)
17388 + goto done;
17389 + size -= t;
17390 + next += t;
17391 + continue;
17393 + list_for_each_entry (req, &ep->queue, queue) {
17394 + if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
17395 + t = snprintf (next, size,
17396 + "\treq %p len %d/%d "
17397 + "buf %p (dmacount %08x)\n",
17398 + &req->req, req->req.actual,
17399 + req->req.length, req->req.buf,
17400 + readl (&ep->dma->dmacount));
17401 + else
17402 + t = snprintf (next, size,
17403 + "\treq %p len %d/%d buf %p\n",
17404 + &req->req, req->req.actual,
17405 + req->req.length, req->req.buf);
17406 + if (t <= 0 || t > size)
17407 + goto done;
17408 + size -= t;
17409 + next += t;
17411 + if (ep->dma) {
17412 + struct net2280_dma *td;
17414 + td = req->td;
17415 + t = snprintf (next, size, "\t td %08x "
17416 + " count %08x buf %08x desc %08x\n",
17417 + req->td_dma, td->dmacount,
17418 + td->dmaaddr, td->dmadesc);
17419 + if (t <= 0 || t > size)
17420 + goto done;
17421 + size -= t;
17422 + next += t;
17427 +done:
17428 + spin_unlock_irqrestore (&dev->lock, flags);
17429 + return PAGE_SIZE - size;
17431 +static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
17434 +#else
17436 +#define device_create_file(a,b) do {} while (0)
17437 +#define device_remove_file device_create_file
17439 +#endif
17441 +/*-------------------------------------------------------------------------*/
17443 +/* another driver-specific mode might be a request type doing dma
17444 + * to/from another device fifo instead of to/from memory.
17445 + */
17447 +static void set_fifo_mode (struct net2280 *dev, int mode)
17449 + /* keeping high bits preserves BAR2 */
17450 + writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
17452 + /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
17453 + INIT_LIST_HEAD (&dev->gadget.ep_list);
17454 + list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
17455 + list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
17456 + switch (mode) {
17457 + case 0:
17458 + list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
17459 + list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
17460 + dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
17461 + break;
17462 + case 1:
17463 + dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
17464 + break;
17465 + case 2:
17466 + list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
17467 + dev->ep [1].fifo_size = 2048;
17468 + dev->ep [2].fifo_size = 1024;
17469 + break;
17471 + /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
17472 + list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
17473 + list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
17476 +/**
17477 + * net2280_set_fifo_mode - change allocation of fifo buffers
17478 + * @gadget: access to the net2280 device that will be updated
17479 + * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
17480 + * 1 for two 2kB buffers (ep-a and ep-b only);
17481 + * 2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
17483 + * returns zero on success, else negative errno. when this succeeds,
17484 + * the contents of gadget->ep_list may have changed.
17486 + * you may only call this function when endpoints a-d are all disabled.
17487 + * use it whenever extra hardware buffering can help performance, such
17488 + * as before enabling "high bandwidth" interrupt endpoints that use
17489 + * maxpacket bigger than 512 (when double buffering would otherwise
17490 + * be unavailable).
17491 + */
17492 +int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
17494 + int i;
17495 + struct net2280 *dev;
17496 + int status = 0;
17497 + unsigned long flags;
17499 + if (!gadget)
17500 + return -ENODEV;
17501 + dev = container_of (gadget, struct net2280, gadget);
17503 + spin_lock_irqsave (&dev->lock, flags);
17505 + for (i = 1; i <= 4; i++)
17506 + if (dev->ep [i].desc) {
17507 + status = -EINVAL;
17508 + break;
17510 + if (mode < 0 || mode > 2)
17511 + status = -EINVAL;
17512 + if (status == 0)
17513 + set_fifo_mode (dev, mode);
17514 + spin_unlock_irqrestore (&dev->lock, flags);
17516 + if (status == 0) {
17517 + if (mode == 1)
17518 + DEBUG (dev, "fifo: ep-a 2K, ep-b 2K\n");
17519 + else if (mode == 2)
17520 + DEBUG (dev, "fifo: ep-a 2K, ep-b 1K, ep-c 1K\n");
17521 + /* else all are 1K */
17523 + return status;
17525 +EXPORT_SYMBOL (net2280_set_fifo_mode);
17527 +/*-------------------------------------------------------------------------*/
17529 +/* keeping it simple:
17530 + * - one bus driver, initted first;
17531 + * - one function driver, initted second
17533 + * most of the work to support multiple net2280 controllers would
17534 + * be to associate this gadget driver (yes?) with all of them, or
17535 + * perhaps to bind specific drivers to specific devices.
17536 + */
17538 +static struct net2280 *the_controller;
17540 +static void usb_reset (struct net2280 *dev)
17542 + u32 tmp;
17544 + dev->gadget.speed = USB_SPEED_UNKNOWN;
17545 + (void) readl (&dev->usb->usbctl);
17547 + net2280_led_init (dev);
17549 + /* disable automatic responses, and irqs */
17550 + writel (0, &dev->usb->stdrsp);
17551 + writel (0, &dev->regs->pciirqenb0);
17552 + writel (0, &dev->regs->pciirqenb1);
17554 + /* clear old dma and irq state */
17555 + for (tmp = 0; tmp < 4; tmp++) {
17556 + struct net2280_ep *ep = &dev->ep [tmp + 1];
17558 + if (ep->dma)
17559 + abort_dma (ep);
17561 + writel (~0, &dev->regs->irqstat0),
17562 + writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
17564 + /* reset, and enable pci */
17565 + tmp = readl (&dev->regs->devinit)
17566 + | (1 << PCI_ENABLE)
17567 + | (1 << FIFO_SOFT_RESET)
17568 + | (1 << USB_SOFT_RESET)
17569 + | (1 << M8051_RESET);
17570 + writel (tmp, &dev->regs->devinit);
17572 + /* standard fifo and endpoint allocations */
17573 + set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
17576 +static void usb_reinit (struct net2280 *dev)
17578 + u32 tmp;
17579 + int init_dma;
17581 + /* use_dma changes are ignored till next device re-init */
17582 + init_dma = use_dma;
17584 + /* basic endpoint init */
17585 + for (tmp = 0; tmp < 7; tmp++) {
17586 + struct net2280_ep *ep = &dev->ep [tmp];
17588 + ep->ep.name = ep_name [tmp];
17589 + ep->dev = dev;
17590 + ep->num = tmp;
17592 + if (tmp > 0 && tmp <= 4) {
17593 + ep->fifo_size = 1024;
17594 + if (init_dma)
17595 + ep->dma = &dev->dma [tmp - 1];
17596 + } else
17597 + ep->fifo_size = 64;
17598 + ep->regs = &dev->epregs [tmp];
17599 + ep_reset (dev->regs, ep);
17601 + dev->ep [0].ep.maxpacket = 64;
17602 + dev->ep [5].ep.maxpacket = 64;
17603 + dev->ep [6].ep.maxpacket = 64;
17605 + dev->gadget.ep0 = &dev->ep [0].ep;
17606 + dev->ep [0].stopped = 0;
17607 + INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
17609 + /* we want to prevent lowlevel/insecure access from the USB host,
17610 + * but erratum 0119 means this enable bit is ignored
17611 + */
17612 + for (tmp = 0; tmp < 5; tmp++)
17613 + writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
17616 +static void ep0_start (struct net2280 *dev)
17618 + writel ( (1 << CLEAR_EP_HIDE_STATUS_PHASE)
17619 + | (1 << CLEAR_NAK_OUT_PACKETS)
17620 + | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
17621 + , &dev->epregs [0].ep_rsp);
17623 + /*
17624 + * hardware optionally handles a bunch of standard requests
17625 + * that the API hides from drivers anyway. have it do so.
17626 + * endpoint status/features are handled in software, to
17627 + * help pass tests for some dubious behavior.
17628 + */
17629 + writel ( (1 << SET_TEST_MODE)
17630 + | (1 << SET_ADDRESS)
17631 + | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
17632 + | (1 << GET_DEVICE_STATUS)
17633 + | (1 << GET_INTERFACE_STATUS)
17634 + , &dev->usb->stdrsp);
17635 + writel ( (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
17636 + | (1 << SELF_POWERED_USB_DEVICE)
17637 + /* erratum 0102 workaround */
17638 + | ((dev->chiprev == 0100) ? 0 : 1) << SUSPEND_IMMEDIATELY
17639 + | (1 << REMOTE_WAKEUP_SUPPORT)
17640 + | (dev->softconnect << USB_DETECT_ENABLE)
17641 + | (1 << SELF_POWERED_STATUS)
17642 + , &dev->usb->usbctl);
17644 + /* enable irqs so we can see ep0 and general operation */
17645 + writel ( (1 << SETUP_PACKET_INTERRUPT_ENABLE)
17646 + | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
17647 + , &dev->regs->pciirqenb0);
17648 + writel ( (1 << PCI_INTERRUPT_ENABLE)
17649 + | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
17650 + | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
17651 + | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
17652 + | (1 << VBUS_INTERRUPT_ENABLE)
17653 + | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
17654 + | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
17655 + , &dev->regs->pciirqenb1);
17657 + /* don't leave any writes posted */
17658 + (void) readl (&dev->usb->usbctl);
17661 +/* when a driver is successfully registered, it will receive
17662 + * control requests including set_configuration(), which enables
17663 + * non-control requests. then usb traffic follows until a
17664 + * disconnect is reported. then a host may connect again, or
17665 + * the driver might get unbound.
17666 + */
17667 +int usb_gadget_register_driver (struct usb_gadget_driver *driver)
17669 + struct net2280 *dev = the_controller;
17670 + int retval;
17671 + unsigned i;
17673 + /* insist on high speed support from the driver, since
17674 + * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
17675 + * "must not be used in normal operation"
17676 + */
17677 + if (!driver
17678 + || driver->speed != USB_SPEED_HIGH
17679 + || !driver->bind
17680 + || !driver->unbind
17681 + || !driver->setup)
17682 + return -EINVAL;
17683 + if (!dev)
17684 + return -ENODEV;
17685 + if (dev->driver)
17686 + return -EBUSY;
17688 + for (i = 0; i < 7; i++)
17689 + dev->ep [i].irqs = 0;
17691 + /* hook up the driver ... */
17692 + dev->softconnect = 1;
17693 + dev->driver = driver;
17694 + retval = driver->bind (&dev->gadget);
17695 + if (retval) {
17696 + DEBUG (dev, "bind to driver %s --> %d\n",
17697 + driver->driver.name, retval);
17698 + dev->driver = 0;
17699 + return retval;
17702 + /* ... then enable host detection and ep0; and we're ready
17703 + * for set_configuration as well as eventual disconnect.
17704 + */
17705 + net2280_led_active (dev, 1);
17706 + ep0_start (dev);
17708 + DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
17709 + driver->driver.name,
17710 + readl (&dev->usb->usbctl),
17711 + readl (&dev->usb->stdrsp));
17713 + /* pci writes may still be posted */
17714 + return 0;
17716 +EXPORT_SYMBOL (usb_gadget_register_driver);
17718 +static void
17719 +stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
17721 + int i;
17723 + /* don't disconnect if it's not connected */
17724 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
17725 + driver = NULL;
17727 + /* stop hardware; prevent new request submissions;
17728 + * and kill any outstanding requests.
17729 + */
17730 + usb_reset (dev);
17731 + for (i = 0; i < 7; i++)
17732 + nuke (&dev->ep [i]);
17734 + /* report disconnect; the driver is already quiesced */
17735 + if (driver) {
17736 + spin_unlock (&dev->lock);
17737 + driver->disconnect (&dev->gadget);
17738 + spin_lock (&dev->lock);
17741 + usb_reinit (dev);
17744 +int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
17746 + struct net2280 *dev = the_controller;
17747 + unsigned long flags;
17749 + if (!dev)
17750 + return -ENODEV;
17751 + if (!driver || driver != dev->driver)
17752 + return -EINVAL;
17754 + spin_lock_irqsave (&dev->lock, flags);
17755 + stop_activity (dev, driver);
17756 + spin_unlock_irqrestore (&dev->lock, flags);
17758 + driver->unbind (&dev->gadget);
17759 + dev->driver = 0;
17761 + net2280_led_active (dev, 0);
17763 + DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
17764 + return 0;
17766 +EXPORT_SYMBOL (usb_gadget_unregister_driver);
17769 +/*-------------------------------------------------------------------------*/
17771 +/* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
17772 + * also works for dma-capable endpoints, in pio mode or just
17773 + * to manually advance the queue after short OUT transfers.
17774 + */
17775 +static void handle_ep_small (struct net2280_ep *ep)
17777 + struct net2280_request *req;
17778 + u32 t;
17779 + /* 0 error, 1 mid-data, 2 done */
17780 + int mode = 1;
17782 + if (!list_empty (&ep->queue))
17783 + req = list_entry (ep->queue.next,
17784 + struct net2280_request, queue);
17785 + else
17786 + req = NULL;
17788 + /* ack all, and handle what we care about */
17789 + t = readl (&ep->regs->ep_stat);
17790 + ep->irqs++;
17791 +#if 0
17792 + VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
17793 + ep->ep.name, t, req ? &req->req : 0);
17794 +#endif
17795 + writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
17797 + /* for ep0, monitor token irqs to catch data stage length errors
17798 + * and to synchronize on status.
17800 + * also, to defer reporting of protocol stalls ... here's where
17801 + * data or status first appears, handling stalls here should never
17802 + * cause trouble on the host side..
17804 + * control requests could be slightly faster without token synch for
17805 + * status, but status can jam up that way.
17806 + */
17807 + if (unlikely (ep->num == 0)) {
17808 + if (ep->is_in) {
17809 + /* status; stop NAKing */
17810 + if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
17811 + if (ep->dev->protocol_stall) {
17812 + ep->stopped = 1;
17813 + set_halt (ep);
17815 + if (!req)
17816 + allow_status (ep);
17817 + mode = 2;
17818 + /* reply to extra IN data tokens with a zlp */
17819 + } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
17820 + if (ep->dev->protocol_stall) {
17821 + ep->stopped = 1;
17822 + set_halt (ep);
17823 + mode = 2;
17824 + } else if (!req && ep->stopped)
17825 + write_fifo (ep, NULL);
17827 + } else {
17828 + /* status; stop NAKing */
17829 + if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
17830 + if (ep->dev->protocol_stall) {
17831 + ep->stopped = 1;
17832 + set_halt (ep);
17834 + mode = 2;
17835 + /* an extra OUT token is an error */
17836 + } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
17837 + && req
17838 + && req->req.actual == req->req.length)
17839 + || !req) {
17840 + ep->dev->protocol_stall = 1;
17841 + set_halt (ep);
17842 + ep->stopped = 1;
17843 + if (req)
17844 + done (ep, req, -EOVERFLOW);
17845 + req = NULL;
17850 + if (unlikely (!req))
17851 + return;
17853 + /* manual DMA queue advance after short OUT */
17854 + if (likely (ep->dma != 0)) {
17855 + if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
17856 + u32 count;
17857 + int stopped = ep->stopped;
17859 + /* TRANSFERRED works around OUT_DONE erratum 0112.
17860 + * we expect (N <= maxpacket) bytes; host wrote M.
17861 + * iff (M < N) we won't ever see a DMA interrupt.
17862 + */
17863 + ep->stopped = 1;
17864 + for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
17866 + /* any preceding dma transfers must finish.
17867 + * dma handles (M >= N), may empty the queue
17868 + */
17869 + scan_dma_completions (ep);
17870 + if (unlikely (list_empty (&ep->queue)
17871 + || ep->out_overflow)) {
17872 + req = NULL;
17873 + break;
17875 + req = list_entry (ep->queue.next,
17876 + struct net2280_request, queue);
17878 + /* here either (M < N), a "real" short rx;
17879 + * or (M == N) and the queue didn't empty
17880 + */
17881 + if (likely (t & (1 << FIFO_EMPTY))) {
17882 + count = readl (&ep->dma->dmacount);
17883 + count &= DMA_BYTE_COUNT_MASK;
17884 + if (readl (&ep->dma->dmadesc)
17885 + != req->td_dma)
17886 + req = NULL;
17887 + break;
17889 + udelay(1);
17892 + /* stop DMA, leave ep NAKing */
17893 + writel ((1 << DMA_ABORT), &ep->dma->dmastat);
17894 + spin_stop_dma (ep->dma);
17896 + if (likely (req != 0)) {
17897 + req->td->dmacount = 0;
17898 + t = readl (&ep->regs->ep_avail);
17899 + dma_done (ep, req, count, t);
17902 + /* also flush to prevent erratum 0106 trouble */
17903 + if (unlikely (ep->out_overflow
17904 + || (ep->dev->chiprev == 0x0100
17905 + && ep->dev->gadget.speed
17906 + == USB_SPEED_FULL))) {
17907 + out_flush (ep);
17908 + ep->out_overflow = 0;
17911 + /* (re)start dma if needed, stop NAKing */
17912 + ep->stopped = stopped;
17913 + if (!list_empty (&ep->queue))
17914 + restart_dma (ep);
17915 + } else
17916 + DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
17917 + ep->ep.name, t);
17918 + return;
17920 + /* data packet(s) received (in the fifo, OUT) */
17921 + } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
17922 + if (read_fifo (ep, req) && ep->num != 0)
17923 + mode = 2;
17925 + /* data packet(s) transmitted (IN) */
17926 + } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
17927 + unsigned len;
17929 + len = req->req.length - req->req.actual;
17930 + if (len > ep->ep.maxpacket)
17931 + len = ep->ep.maxpacket;
17932 + req->req.actual += len;
17934 + /* if we wrote it all, we're usually done */
17935 + if (req->req.actual == req->req.length) {
17936 + if (ep->num == 0) {
17937 + /* wait for control status */
17938 + if (mode != 2)
17939 + req = NULL;
17940 + } else if (!req->req.zero || len != ep->ep.maxpacket)
17941 + mode = 2;
17944 + /* there was nothing to do ... */
17945 + } else if (mode == 1)
17946 + return;
17948 + /* done */
17949 + if (mode == 2) {
17950 + /* stream endpoints often resubmit/unlink in completion */
17951 + done (ep, req, 0);
17953 + /* maybe advance queue to next request */
17954 + if (ep->num == 0) {
17955 + /* NOTE: net2280 could let gadget driver start the
17956 + * status stage later. since not all controllers let
17957 + * them control that, the api doesn't (yet) allow it.
17958 + */
17959 + if (!ep->stopped)
17960 + allow_status (ep);
17961 + req = NULL;
17962 + } else {
17963 + if (!list_empty (&ep->queue) && !ep->stopped)
17964 + req = list_entry (ep->queue.next,
17965 + struct net2280_request, queue);
17966 + else
17967 + req = NULL;
17968 + if (req && !ep->is_in)
17969 + stop_out_naking (ep);
17973 + /* is there a buffer for the next packet?
17974 + * for best streaming performance, make sure there is one.
17975 + */
17976 + if (req && !ep->stopped) {
17978 + /* load IN fifo with next packet (may be zlp) */
17979 + if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
17980 + write_fifo (ep, &req->req);
17984 +static struct net2280_ep *
17985 +get_ep_by_addr (struct net2280 *dev, u16 wIndex)
17987 + struct net2280_ep *ep;
17989 + if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
17990 + return &dev->ep [0];
17991 + list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
17992 + u8 bEndpointAddress;
17994 + if (!ep->desc)
17995 + continue;
17996 + bEndpointAddress = ep->desc->bEndpointAddress;
17997 + if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
17998 + continue;
17999 + if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
18000 + return ep;
18002 + return NULL;
18005 +static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
18007 + struct net2280_ep *ep;
18008 + u32 num, scratch;
18010 + /* most of these don't need individual acks */
18011 + stat &= ~(1 << INTA_ASSERTED);
18012 + if (!stat)
18013 + return;
18014 + // DEBUG (dev, "irqstat0 %04x\n", stat);
18016 + /* starting a control request? */
18017 + if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
18018 + union {
18019 + u32 raw [2];
18020 + struct usb_ctrlrequest r;
18021 + } u;
18022 + int tmp = 0;
18023 + struct net2280_request *req;
18025 + if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
18026 + if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
18027 + dev->gadget.speed = USB_SPEED_HIGH;
18028 + else
18029 + dev->gadget.speed = USB_SPEED_FULL;
18030 + net2280_led_speed (dev, dev->gadget.speed);
18031 + DEBUG (dev, "%s speed\n",
18032 + (dev->gadget.speed == USB_SPEED_HIGH)
18033 + ? "high" : "full");
18036 + ep = &dev->ep [0];
18037 + ep->irqs++;
18039 + /* make sure any leftover request state is cleared */
18040 + stat &= ~(1 << ENDPOINT_0_INTERRUPT);
18041 + while (!list_empty (&ep->queue)) {
18042 + req = list_entry (ep->queue.next,
18043 + struct net2280_request, queue);
18044 + done (ep, req, (req->req.actual == req->req.length)
18045 + ? 0 : -EPROTO);
18047 + ep->stopped = 0;
18048 + dev->protocol_stall = 0;
18049 + writel ( (1 << TIMEOUT)
18050 + | (1 << USB_STALL_SENT)
18051 + | (1 << USB_IN_NAK_SENT)
18052 + | (1 << USB_IN_ACK_RCVD)
18053 + | (1 << USB_OUT_PING_NAK_SENT)
18054 + | (1 << USB_OUT_ACK_SENT)
18055 + | (1 << FIFO_OVERFLOW)
18056 + | (1 << FIFO_UNDERFLOW)
18057 + | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
18058 + | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
18059 + | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
18060 + | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
18061 + | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
18062 + | (1 << DATA_IN_TOKEN_INTERRUPT)
18063 + , &ep->regs->ep_stat);
18064 + u.raw [0] = readl (&dev->usb->setup0123);
18065 + u.raw [1] = readl (&dev->usb->setup4567);
18067 + cpu_to_le32s (&u.raw [0]);
18068 + cpu_to_le32s (&u.raw [1]);
18070 + le16_to_cpus (&u.r.wValue);
18071 + le16_to_cpus (&u.r.wIndex);
18072 + le16_to_cpus (&u.r.wLength);
18074 + /* ack the irq */
18075 + writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
18076 + stat ^= (1 << SETUP_PACKET_INTERRUPT);
18078 + /* watch control traffic at the token level, and force
18079 + * synchronization before letting the status stage happen.
18080 + * FIXME ignore tokens we'll NAK, until driver responds.
18081 + * that'll mean a lot less irqs for some drivers.
18082 + */
18083 + ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
18084 + if (ep->is_in) {
18085 + scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
18086 + | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
18087 + | (1 << DATA_IN_TOKEN_INTERRUPT);
18088 + stop_out_naking (ep);
18089 + } else
18090 + scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
18091 + | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
18092 + | (1 << DATA_IN_TOKEN_INTERRUPT);
18093 + writel (scratch, &dev->epregs [0].ep_irqenb);
18095 + /* we made the hardware handle most lowlevel requests;
18096 + * everything else goes uplevel to the gadget code.
18097 + */
18098 + switch (u.r.bRequest) {
18099 + case USB_REQ_GET_STATUS: {
18100 + struct net2280_ep *e;
18101 + u16 status;
18103 + /* hw handles device and interface status */
18104 + if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
18105 + goto delegate;
18106 + if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0
18107 + || u.r.wLength > 2)
18108 + goto do_stall;
18110 + if (readl (&e->regs->ep_rsp)
18111 + & (1 << SET_ENDPOINT_HALT))
18112 + status = __constant_cpu_to_le16 (1);
18113 + else
18114 + status = __constant_cpu_to_le16 (0);
18116 + /* don't bother with a request object! */
18117 + writel (0, &dev->epregs [0].ep_irqenb);
18118 + set_fifo_bytecount (ep, u.r.wLength);
18119 + writel (status, &dev->epregs [0].ep_data);
18120 + allow_status (ep);
18121 + VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
18122 + goto next_endpoints;
18124 + break;
18125 + case USB_REQ_CLEAR_FEATURE: {
18126 + struct net2280_ep *e;
18128 + /* hw handles device features */
18129 + if (u.r.bRequestType != USB_RECIP_ENDPOINT)
18130 + goto delegate;
18131 + if (u.r.wValue != USB_ENDPOINT_HALT
18132 + || u.r.wLength != 0)
18133 + goto do_stall;
18134 + if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
18135 + goto do_stall;
18136 + clear_halt (e);
18137 + allow_status (ep);
18138 + VDEBUG (dev, "%s clear halt\n", ep->ep.name);
18139 + goto next_endpoints;
18141 + break;
18142 + case USB_REQ_SET_FEATURE: {
18143 + struct net2280_ep *e;
18145 + /* hw handles device features */
18146 + if (u.r.bRequestType != USB_RECIP_ENDPOINT)
18147 + goto delegate;
18148 + if (u.r.wValue != USB_ENDPOINT_HALT
18149 + || u.r.wLength != 0)
18150 + goto do_stall;
18151 + if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
18152 + goto do_stall;
18153 + set_halt (e);
18154 + allow_status (ep);
18155 + VDEBUG (dev, "%s set halt\n", ep->ep.name);
18156 + goto next_endpoints;
18158 + break;
18159 + default:
18160 +delegate:
18161 + VDEBUG (dev, "setup %02x.%02x v%04x i%04x "
18162 + "ep_cfg %08x\n",
18163 + u.r.bRequestType, u.r.bRequest,
18164 + u.r.wValue, u.r.wIndex,
18165 + readl (&ep->regs->ep_cfg));
18166 + spin_unlock (&dev->lock);
18167 + tmp = dev->driver->setup (&dev->gadget, &u.r);
18168 + spin_lock (&dev->lock);
18171 + /* stall ep0 on error */
18172 + if (tmp < 0) {
18173 +do_stall:
18174 + VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
18175 + u.r.bRequestType, u.r.bRequest, tmp);
18176 + dev->protocol_stall = 1;
18179 + /* some in/out token irq should follow; maybe stall then.
18180 + * driver must queue a request (even zlp) or halt ep0
18181 + * before the host times out.
18182 + */
18185 +next_endpoints:
18186 + /* endpoint data irq ? */
18187 + scratch = stat & 0x7f;
18188 + stat &= ~0x7f;
18189 + for (num = 0; scratch; num++) {
18190 + u32 t;
18192 + /* do this endpoint's FIFO and queue need tending? */
18193 + t = 1 << num;
18194 + if ((scratch & t) == 0)
18195 + continue;
18196 + scratch ^= t;
18198 + ep = &dev->ep [num];
18199 + handle_ep_small (ep);
18202 + if (stat)
18203 + DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
18206 +#define DMA_INTERRUPTS ( \
18207 + (1 << DMA_D_INTERRUPT) \
18208 + | (1 << DMA_C_INTERRUPT) \
18209 + | (1 << DMA_B_INTERRUPT) \
18210 + | (1 << DMA_A_INTERRUPT))
18211 +#define PCI_ERROR_INTERRUPTS ( \
18212 + (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
18213 + | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
18214 + | (1 << PCI_RETRY_ABORT_INTERRUPT))
18216 +static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
18218 + struct net2280_ep *ep;
18219 + u32 tmp, num, scratch;
18221 + /* after disconnect there's nothing else to do! */
18222 + tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
18223 + if (stat & tmp) {
18224 + writel (tmp, &dev->regs->irqstat1);
18225 + if (((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) != 0
18226 + || (readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0
18227 + ) && dev->gadget.speed != USB_SPEED_UNKNOWN) {
18228 + DEBUG (dev, "disconnect %s\n",
18229 + dev->driver->driver.name);
18230 + stop_activity (dev, dev->driver);
18231 + ep0_start (dev);
18232 + return;
18234 + stat &= ~tmp;
18236 + /* vBUS can bounce ... one of many reasons to ignore the
18237 + * notion of hotplug events on bus connect/disconnect!
18238 + */
18239 + if (!stat)
18240 + return;
18243 + /* NOTE: chip stays in PCI D0 state for now, but it could
18244 + * enter D1 to save more power
18245 + */
18246 + tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
18247 + if (stat & tmp) {
18248 + writel (tmp, &dev->regs->irqstat1);
18249 + if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
18250 + if (dev->driver->suspend)
18251 + dev->driver->suspend (&dev->gadget);
18252 + /* we use SUSPEND_IMMEDIATELY */
18253 + stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
18254 + } else {
18255 + if (dev->driver->resume)
18256 + dev->driver->resume (&dev->gadget);
18257 + /* at high speed, note erratum 0133 */
18259 + stat &= ~tmp;
18262 + /* clear any other status/irqs */
18263 + if (stat)
18264 + writel (stat, &dev->regs->irqstat1);
18266 + /* some status we can just ignore */
18267 + stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
18268 + | (1 << SUSPEND_REQUEST_INTERRUPT)
18269 + | (1 << RESUME_INTERRUPT)
18270 + | (1 << SOF_INTERRUPT));
18271 + if (!stat)
18272 + return;
18273 + // DEBUG (dev, "irqstat1 %08x\n", stat);
18275 + /* DMA status, for ep-{a,b,c,d} */
18276 + scratch = stat & DMA_INTERRUPTS;
18277 + stat &= ~DMA_INTERRUPTS;
18278 + scratch >>= 9;
18279 + for (num = 0; scratch; num++) {
18280 + struct net2280_dma_regs *dma;
18282 + tmp = 1 << num;
18283 + if ((tmp & scratch) == 0)
18284 + continue;
18285 + scratch ^= tmp;
18287 + ep = &dev->ep [num + 1];
18288 + dma = ep->dma;
18290 + if (!dma)
18291 + continue;
18293 + /* clear ep's dma status */
18294 + tmp = readl (&dma->dmastat);
18295 + writel (tmp, &dma->dmastat);
18297 + /* chaining should stop on abort, short OUT from fifo,
18298 + * or (stat0 codepath) short OUT transfer.
18299 + */
18300 + if (!use_dma_chaining) {
18301 + if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
18302 + == 0) {
18303 + DEBUG (ep->dev, "%s no xact done? %08x\n",
18304 + ep->ep.name, tmp);
18305 + continue;
18307 + stop_dma (ep->dma);
18310 + /* OUT transfers terminate when the data from the
18311 + * host is in our memory. Process whatever's done.
18312 + * On this path, we know transfer's last packet wasn't
18313 + * less than req->length. NAK_OUT_PACKETS may be set,
18314 + * or the FIFO may already be holding new packets.
18316 + * IN transfers can linger in the FIFO for a very
18317 + * long time ... we ignore that for now, accounting
18318 + * precisely (like PIO does) needs per-packet irqs
18319 + */
18320 + scan_dma_completions (ep);
18322 + /* disable dma on inactive queues; else maybe restart */
18323 + if (list_empty (&ep->queue)) {
18324 + if (use_dma_chaining)
18325 + stop_dma (ep->dma);
18326 + } else {
18327 + tmp = readl (&dma->dmactl);
18328 + if (!use_dma_chaining
18329 + || (tmp & (1 << DMA_ENABLE)) == 0)
18330 + restart_dma (ep);
18331 + else if (ep->is_in && use_dma_chaining) {
18332 + struct net2280_request *req;
18333 + u32 dmacount;
18335 + /* the descriptor at the head of the chain
18336 + * may still have VALID_BIT clear; that's
18337 + * used to trigger changing DMA_FIFO_VALIDATE
18338 + * (affects automagic zlp writes).
18339 + */
18340 + req = list_entry (ep->queue.next,
18341 + struct net2280_request, queue);
18342 + dmacount = req->td->dmacount;
18343 + dmacount &= __constant_cpu_to_le32 (
18344 + (1 << VALID_BIT)
18345 + | DMA_BYTE_COUNT_MASK);
18346 + if (dmacount && (dmacount & valid_bit) == 0)
18347 + restart_dma (ep);
18350 + ep->irqs++;
18353 + /* NOTE: there are other PCI errors we might usefully notice.
18354 + * if they appear very often, here's where to try recovering.
18355 + */
18356 + if (stat & PCI_ERROR_INTERRUPTS) {
18357 + ERROR (dev, "pci dma error; stat %08x\n", stat);
18358 + stat &= ~PCI_ERROR_INTERRUPTS;
18359 + /* these are fatal errors, but "maybe" they won't
18360 + * happen again ...
18361 + */
18362 + stop_activity (dev, dev->driver);
18363 + ep0_start (dev);
18364 + stat = 0;
18367 + if (stat)
18368 + DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
18371 +static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
18373 + struct net2280 *dev = _dev;
18375 + spin_lock (&dev->lock);
18377 + /* handle disconnect, dma, and more */
18378 + handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
18380 + /* control requests and PIO */
18381 + handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
18383 + spin_unlock (&dev->lock);
18385 + return IRQ_HANDLED;
18388 +/*-------------------------------------------------------------------------*/
18390 +/* tear down the binding between this driver and the pci device */
18392 +static void net2280_remove (struct pci_dev *pdev)
18394 + struct net2280 *dev = pci_get_drvdata (pdev);
18396 + /* start with the driver above us */
18397 + if (dev->driver) {
18398 + /* should have been done already by driver model core */
18399 + WARN (dev, "pci remove, driver '%s' is still registered\n",
18400 + dev->driver->driver.name);
18401 + usb_gadget_unregister_driver (dev->driver);
18404 + /* then clean up the resources we allocated during probe() */
18405 + net2280_led_shutdown (dev);
18406 + if (dev->requests) {
18407 + int i;
18408 + for (i = 1; i < 5; i++) {
18409 + if (!dev->ep [i].dummy)
18410 + continue;
18411 + pci_pool_free (dev->requests, dev->ep [i].dummy,
18412 + dev->ep [i].td_dma);
18414 + pci_pool_destroy (dev->requests);
18416 + if (dev->got_irq)
18417 + free_irq (pdev->irq, dev);
18418 + if (dev->regs)
18419 + iounmap (dev->regs);
18420 + if (dev->region)
18421 + release_mem_region (pci_resource_start (pdev, 0),
18422 + pci_resource_len (pdev, 0));
18423 + if (dev->enabled)
18424 + pci_disable_device (pdev);
18425 + pci_set_drvdata (pdev, 0);
18427 + INFO (dev, "unbind from pci %s\n", pdev->slot_name);
18429 + kfree (dev);
18430 + the_controller = 0;
18433 +/* wrap this driver around the specified device, but
18434 + * don't respond over USB until a gadget driver binds to us.
18435 + */
18437 +static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
18439 + struct net2280 *dev;
18440 + unsigned long resource, len;
18441 + void *base = NULL;
18442 + int retval, i;
18443 + char buf [8], *bufp;
18445 + /* if you want to support more than one controller in a system,
18446 + * usb_gadget_driver_{register,unregister}() must change.
18447 + */
18448 + if (the_controller) {
18449 + WARN (the_controller, "ignoring %s\n", pdev->slot_name);
18450 + return -EBUSY;
18453 + /* alloc, and start init */
18454 + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
18455 + if (dev == NULL){
18456 + retval = -ENOMEM;
18457 + goto done;
18460 + memset (dev, 0, sizeof *dev);
18461 + spin_lock_init (&dev->lock);
18462 + dev->pdev = pdev;
18463 + dev->gadget.ops = &net2280_ops;
18464 + dev->gadget.is_dualspeed = 1;
18466 + dev->gadget.dev.bus_id = pdev->slot_name;
18467 + dev->gadget.name = driver_name;
18469 + /* now all the pci goodies ... */
18470 + if (pci_enable_device (pdev) < 0) {
18471 + retval = -ENODEV;
18472 + goto done;
18474 + dev->enabled = 1;
18476 + /* BAR 0 holds all the registers
18477 + * BAR 1 is 8051 memory; unused here (note erratum 0103)
18478 + * BAR 2 is fifo memory; unused here
18479 + */
18480 + resource = pci_resource_start (pdev, 0);
18481 + len = pci_resource_len (pdev, 0);
18482 + if (!request_mem_region (resource, len, driver_name)) {
18483 + DEBUG (dev, "controller already in use\n");
18484 + retval = -EBUSY;
18485 + goto done;
18487 + dev->region = 1;
18489 + base = ioremap_nocache (resource, len);
18490 + if (base == NULL) {
18491 + DEBUG (dev, "can't map memory\n");
18492 + retval = -EFAULT;
18493 + goto done;
18495 + dev->regs = (struct net2280_regs *) base;
18496 + dev->usb = (struct net2280_usb_regs *) (base + 0x0080);
18497 + dev->pci = (struct net2280_pci_regs *) (base + 0x0100);
18498 + dev->dma = (struct net2280_dma_regs *) (base + 0x0180);
18499 + dev->dep = (struct net2280_dep_regs *) (base + 0x0200);
18500 + dev->epregs = (struct net2280_ep_regs *) (base + 0x0300);
18502 + /* put into initial config, link up all endpoints */
18503 + writel (0, &dev->usb->usbctl);
18504 + usb_reset (dev);
18505 + usb_reinit (dev);
18507 + /* irq setup after old hardware is cleaned up */
18508 + if (!pdev->irq) {
18509 + ERROR (dev, "No IRQ. Check PCI setup!\n");
18510 + retval = -ENODEV;
18511 + goto done;
18513 +#ifndef __sparc__
18514 + snprintf (buf, sizeof buf, "%d", pdev->irq);
18515 + bufp = buf;
18516 +#else
18517 + bufp = __irq_itoa(pdev->irq);
18518 +#endif
18519 + if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
18520 + != 0) {
18521 + ERROR (dev, "request interrupt %s failed\n", bufp);
18522 + retval = -EBUSY;
18523 + goto done;
18525 + dev->got_irq = 1;
18527 + /* DMA setup */
18528 + dev->requests = pci_pool_create ("requests", pdev,
18529 + sizeof (struct net2280_dma),
18530 + 0 /* no alignment requirements */,
18531 + 0 /* or page-crossing issues */,
18532 + SLAB_KERNEL /* 2.4 only */ );
18533 + if (!dev->requests) {
18534 + DEBUG (dev, "can't get request pool\n");
18535 + retval = -ENOMEM;
18536 + goto done;
18538 + for (i = 1; i < 5; i++) {
18539 + struct net2280_dma *td;
18541 + td = pci_pool_alloc (dev->requests, GFP_KERNEL,
18542 + &dev->ep [i].td_dma);
18543 + if (!td) {
18544 + DEBUG (dev, "can't get dummy %d\n", i);
18545 + retval = -ENOMEM;
18546 + goto done;
18548 + td->dmacount = 0; /* not VALID */
18549 + td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
18550 + td->dmadesc = td->dmaaddr;
18551 + dev->ep [i].dummy = td;
18554 + /* enable lower-overhead pci memory bursts during DMA */
18555 + writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
18556 + // 256 write retries may not be enough...
18557 + // | (1 << PCI_RETRY_ABORT_ENABLE)
18558 + | (1 << DMA_READ_MULTIPLE_ENABLE)
18559 + | (1 << DMA_READ_LINE_ENABLE)
18560 + , &dev->pci->pcimstctl);
18561 + /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
18562 + pci_set_master (pdev);
18563 + pci_set_mwi (pdev);
18565 + /* ... also flushes any posted pci writes */
18566 + dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
18568 + /* done */
18569 + pci_set_drvdata (pdev, dev);
18570 + INFO (dev, "%s\n", driver_desc);
18571 + INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
18572 + bufp, base, dev->chiprev);
18573 + INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
18574 + use_dma
18575 + ? (use_dma_chaining ? "chaining" : "enabled")
18576 + : "disabled");
18577 + the_controller = dev;
18579 + return 0;
18581 +done:
18582 + if (dev)
18583 + net2280_remove (pdev);
18584 + return retval;
18588 +/*-------------------------------------------------------------------------*/
18590 +static struct pci_device_id pci_ids [] = { {
18591 + .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
18592 + .class_mask = ~0,
18593 + .vendor = 0x17cc,
18594 + .device = 0x2280,
18595 + .subvendor = PCI_ANY_ID,
18596 + .subdevice = PCI_ANY_ID,
18598 +}, { /* end: all zeroes */ }
18600 +MODULE_DEVICE_TABLE (pci, pci_ids);
18602 +/* pci driver glue; this is a "new style" PCI driver module */
18603 +static struct pci_driver net2280_pci_driver = {
18604 + .name = (char *) driver_name,
18605 + .id_table = pci_ids,
18607 + .probe = net2280_probe,
18608 + .remove = net2280_remove,
18610 + /* FIXME add power management support */
18613 +MODULE_DESCRIPTION (DRIVER_DESC);
18614 +MODULE_AUTHOR ("David Brownell");
18615 +MODULE_LICENSE ("GPL");
18617 +static int __init init (void)
18619 + if (!use_dma)
18620 + use_dma_chaining = 0;
18621 + return pci_module_init (&net2280_pci_driver);
18623 +module_init (init);
18625 +static void __exit cleanup (void)
18627 + pci_unregister_driver (&net2280_pci_driver);
18629 +module_exit (cleanup);
18630 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/net2280.h kernel/drivers/usb/gadget/net2280.h
18631 --- /tmp/kernel/drivers/usb/gadget/net2280.h 1970-01-01 01:00:00.000000000 +0100
18632 +++ kernel/drivers/usb/gadget/net2280.h 2005-04-22 17:53:19.483531538 +0200
18633 @@ -0,0 +1,756 @@
18635 + * NetChip 2280 high/full speed USB device controller.
18636 + * Unlike many such controllers, this one talks PCI.
18637 + */
18640 + * Copyright (C) 2002 NetChip Technology, Inc. (http://www.netchip.com)
18641 + * Copyright (C) 2003 David Brownell
18643 + * This program is free software; you can redistribute it and/or modify
18644 + * it under the terms of the GNU General Public License as published by
18645 + * the Free Software Foundation; either version 2 of the License, or
18646 + * (at your option) any later version.
18648 + * This program is distributed in the hope that it will be useful,
18649 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18650 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18651 + * GNU General Public License for more details.
18653 + * You should have received a copy of the GNU General Public License
18654 + * along with this program; if not, write to the Free Software
18655 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18656 + */
18658 +/*-------------------------------------------------------------------------*/
18660 +/* NET2280 MEMORY MAPPED REGISTERS
18662 + * The register layout came from the chip documentation, and the bit
18663 + * number definitions were extracted from chip specification.
18665 + * Use the shift operator ('<<') to build bit masks, with readl/writel
18666 + * to access the registers through PCI.
18667 + */
18669 +/* main registers, BAR0 + 0x0000 */
18670 +struct net2280_regs {
18671 + // offset 0x0000
18672 + u32 devinit;
18673 +#define LOCAL_CLOCK_FREQUENCY 8
18674 +#define FORCE_PCI_RESET 7
18675 +#define PCI_ID 6
18676 +#define PCI_ENABLE 5
18677 +#define FIFO_SOFT_RESET 4
18678 +#define CFG_SOFT_RESET 3
18679 +#define PCI_SOFT_RESET 2
18680 +#define USB_SOFT_RESET 1
18681 +#define M8051_RESET 0
18682 + u32 eectl;
18683 +#define EEPROM_ADDRESS_WIDTH 23
18684 +#define EEPROM_CHIP_SELECT_ACTIVE 22
18685 +#define EEPROM_PRESENT 21
18686 +#define EEPROM_VALID 20
18687 +#define EEPROM_BUSY 19
18688 +#define EEPROM_CHIP_SELECT_ENABLE 18
18689 +#define EEPROM_BYTE_READ_START 17
18690 +#define EEPROM_BYTE_WRITE_START 16
18691 +#define EEPROM_READ_DATA 8
18692 +#define EEPROM_WRITE_DATA 0
18693 + u32 eeclkfreq;
18694 + u32 _unused0;
18695 + // offset 0x0010
18697 + u32 pciirqenb0; /* interrupt PCI master ... */
18698 +#define SETUP_PACKET_INTERRUPT_ENABLE 7
18699 +#define ENDPOINT_F_INTERRUPT_ENABLE 6
18700 +#define ENDPOINT_E_INTERRUPT_ENABLE 5
18701 +#define ENDPOINT_D_INTERRUPT_ENABLE 4
18702 +#define ENDPOINT_C_INTERRUPT_ENABLE 3
18703 +#define ENDPOINT_B_INTERRUPT_ENABLE 2
18704 +#define ENDPOINT_A_INTERRUPT_ENABLE 1
18705 +#define ENDPOINT_0_INTERRUPT_ENABLE 0
18706 + u32 pciirqenb1;
18707 +#define PCI_INTERRUPT_ENABLE 31
18708 +#define POWER_STATE_CHANGE_INTERRUPT_ENABLE 27
18709 +#define PCI_ARBITER_TIMEOUT_INTERRUPT_ENABLE 26
18710 +#define PCI_PARITY_ERROR_INTERRUPT_ENABLE 25
18711 +#define PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE 20
18712 +#define PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE 19
18713 +#define PCI_TARGET_ABORT_ASSERTED_INTERRUPT_ENABLE 18
18714 +#define PCI_RETRY_ABORT_INTERRUPT_ENABLE 17
18715 +#define PCI_MASTER_CYCLE_DONE_INTERRUPT_ENABLE 16
18716 +#define GPIO_INTERRUPT_ENABLE 13
18717 +#define DMA_D_INTERRUPT_ENABLE 12
18718 +#define DMA_C_INTERRUPT_ENABLE 11
18719 +#define DMA_B_INTERRUPT_ENABLE 10
18720 +#define DMA_A_INTERRUPT_ENABLE 9
18721 +#define EEPROM_DONE_INTERRUPT_ENABLE 8
18722 +#define VBUS_INTERRUPT_ENABLE 7
18723 +#define CONTROL_STATUS_INTERRUPT_ENABLE 6
18724 +#define ROOT_PORT_RESET_INTERRUPT_ENABLE 4
18725 +#define SUSPEND_REQUEST_INTERRUPT_ENABLE 3
18726 +#define SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE 2
18727 +#define RESUME_INTERRUPT_ENABLE 1
18728 +#define SOF_INTERRUPT_ENABLE 0
18729 + u32 cpu_irqenb0; /* ... or onboard 8051 */
18730 +#define SETUP_PACKET_INTERRUPT_ENABLE 7
18731 +#define ENDPOINT_F_INTERRUPT_ENABLE 6
18732 +#define ENDPOINT_E_INTERRUPT_ENABLE 5
18733 +#define ENDPOINT_D_INTERRUPT_ENABLE 4
18734 +#define ENDPOINT_C_INTERRUPT_ENABLE 3
18735 +#define ENDPOINT_B_INTERRUPT_ENABLE 2
18736 +#define ENDPOINT_A_INTERRUPT_ENABLE 1
18737 +#define ENDPOINT_0_INTERRUPT_ENABLE 0
18738 + u32 cpu_irqenb1;
18739 +#define CPU_INTERRUPT_ENABLE 31
18740 +#define POWER_STATE_CHANGE_INTERRUPT_ENABLE 27
18741 +#define PCI_ARBITER_TIMEOUT_INTERRUPT_ENABLE 26
18742 +#define PCI_PARITY_ERROR_INTERRUPT_ENABLE 25
18743 +#define PCI_INTA_INTERRUPT_ENABLE 24
18744 +#define PCI_PME_INTERRUPT_ENABLE 23
18745 +#define PCI_SERR_INTERRUPT_ENABLE 22
18746 +#define PCI_PERR_INTERRUPT_ENABLE 21
18747 +#define PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE 20
18748 +#define PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE 19
18749 +#define PCI_RETRY_ABORT_INTERRUPT_ENABLE 17
18750 +#define PCI_MASTER_CYCLE_DONE_INTERRUPT_ENABLE 16
18751 +#define GPIO_INTERRUPT_ENABLE 13
18752 +#define DMA_D_INTERRUPT_ENABLE 12
18753 +#define DMA_C_INTERRUPT_ENABLE 11
18754 +#define DMA_B_INTERRUPT_ENABLE 10
18755 +#define DMA_A_INTERRUPT_ENABLE 9
18756 +#define EEPROM_DONE_INTERRUPT_ENABLE 8
18757 +#define VBUS_INTERRUPT_ENABLE 7
18758 +#define CONTROL_STATUS_INTERRUPT_ENABLE 6
18759 +#define ROOT_PORT_RESET_INTERRUPT_ENABLE 4
18760 +#define SUSPEND_REQUEST_INTERRUPT_ENABLE 3
18761 +#define SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE 2
18762 +#define RESUME_INTERRUPT_ENABLE 1
18763 +#define SOF_INTERRUPT_ENABLE 0
18765 + // offset 0x0020
18766 + u32 _unused1;
18767 + u32 usbirqenb1;
18768 +#define USB_INTERRUPT_ENABLE 31
18769 +#define POWER_STATE_CHANGE_INTERRUPT_ENABLE 27
18770 +#define PCI_ARBITER_TIMEOUT_INTERRUPT_ENABLE 26
18771 +#define PCI_PARITY_ERROR_INTERRUPT_ENABLE 25
18772 +#define PCI_INTA_INTERRUPT_ENABLE 24
18773 +#define PCI_PME_INTERRUPT_ENABLE 23
18774 +#define PCI_SERR_INTERRUPT_ENABLE 22
18775 +#define PCI_PERR_INTERRUPT_ENABLE 21
18776 +#define PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE 20
18777 +#define PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE 19
18778 +#define PCI_RETRY_ABORT_INTERRUPT_ENABLE 17
18779 +#define PCI_MASTER_CYCLE_DONE_INTERRUPT_ENABLE 16
18780 +#define GPIO_INTERRUPT_ENABLE 13
18781 +#define DMA_D_INTERRUPT_ENABLE 12
18782 +#define DMA_C_INTERRUPT_ENABLE 11
18783 +#define DMA_B_INTERRUPT_ENABLE 10
18784 +#define DMA_A_INTERRUPT_ENABLE 9
18785 +#define EEPROM_DONE_INTERRUPT_ENABLE 8
18786 +#define VBUS_INTERRUPT_ENABLE 7
18787 +#define CONTROL_STATUS_INTERRUPT_ENABLE 6
18788 +#define ROOT_PORT_RESET_INTERRUPT_ENABLE 4
18789 +#define SUSPEND_REQUEST_INTERRUPT_ENABLE 3
18790 +#define SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE 2
18791 +#define RESUME_INTERRUPT_ENABLE 1
18792 +#define SOF_INTERRUPT_ENABLE 0
18793 + u32 irqstat0;
18794 +#define INTA_ASSERTED 12
18795 +#define SETUP_PACKET_INTERRUPT 7
18796 +#define ENDPOINT_F_INTERRUPT 6
18797 +#define ENDPOINT_E_INTERRUPT 5
18798 +#define ENDPOINT_D_INTERRUPT 4
18799 +#define ENDPOINT_C_INTERRUPT 3
18800 +#define ENDPOINT_B_INTERRUPT 2
18801 +#define ENDPOINT_A_INTERRUPT 1
18802 +#define ENDPOINT_0_INTERRUPT 0
18803 + u32 irqstat1;
18804 +#define POWER_STATE_CHANGE_INTERRUPT 27
18805 +#define PCI_ARBITER_TIMEOUT_INTERRUPT 26
18806 +#define PCI_PARITY_ERROR_INTERRUPT 25
18807 +#define PCI_INTA_INTERRUPT 24
18808 +#define PCI_PME_INTERRUPT 23
18809 +#define PCI_SERR_INTERRUPT 22
18810 +#define PCI_PERR_INTERRUPT 21
18811 +#define PCI_MASTER_ABORT_RECEIVED_INTERRUPT 20
18812 +#define PCI_TARGET_ABORT_RECEIVED_INTERRUPT 19
18813 +#define PCI_RETRY_ABORT_INTERRUPT 17
18814 +#define PCI_MASTER_CYCLE_DONE_INTERRUPT 16
18815 +#define GPIO_INTERRUPT 13
18816 +#define DMA_D_INTERRUPT 12
18817 +#define DMA_C_INTERRUPT 11
18818 +#define DMA_B_INTERRUPT 10
18819 +#define DMA_A_INTERRUPT 9
18820 +#define EEPROM_DONE_INTERRUPT 8
18821 +#define VBUS_INTERRUPT 7
18822 +#define CONTROL_STATUS_INTERRUPT 6
18823 +#define ROOT_PORT_RESET_INTERRUPT 4
18824 +#define SUSPEND_REQUEST_INTERRUPT 3
18825 +#define SUSPEND_REQUEST_CHANGE_INTERRUPT 2
18826 +#define RESUME_INTERRUPT 1
18827 +#define SOF_INTERRUPT 0
18828 + // offset 0x0030
18829 + u32 idxaddr;
18830 + u32 idxdata;
18831 + u32 fifoctl;
18832 +#define PCI_BASE2_RANGE 16
18833 +#define IGNORE_FIFO_AVAILABILITY 3
18834 +#define PCI_BASE2_SELECT 2
18835 +#define FIFO_CONFIGURATION_SELECT 0
18836 + u32 _unused2;
18837 + // offset 0x0040
18838 + u32 memaddr;
18839 +#define START 28
18840 +#define DIRECTION 27
18841 +#define FIFO_DIAGNOSTIC_SELECT 24
18842 +#define MEMORY_ADDRESS 0
18843 + u32 memdata0;
18844 + u32 memdata1;
18845 + u32 _unused3;
18846 + // offset 0x0050
18847 + u32 gpioctl;
18848 +#define GPIO3_LED_SELECT 12
18849 +#define GPIO3_INTERRUPT_ENABLE 11
18850 +#define GPIO2_INTERRUPT_ENABLE 10
18851 +#define GPIO1_INTERRUPT_ENABLE 9
18852 +#define GPIO0_INTERRUPT_ENABLE 8
18853 +#define GPIO3_OUTPUT_ENABLE 7
18854 +#define GPIO2_OUTPUT_ENABLE 6
18855 +#define GPIO1_OUTPUT_ENABLE 5
18856 +#define GPIO0_OUTPUT_ENABLE 4
18857 +#define GPIO3_DATA 3
18858 +#define GPIO2_DATA 2
18859 +#define GPIO1_DATA 1
18860 +#define GPIO0_DATA 0
18861 + u32 gpiostat;
18862 +#define GPIO3_INTERRUPT 3
18863 +#define GPIO2_INTERRUPT 2
18864 +#define GPIO1_INTERRUPT 1
18865 +#define GPIO0_INTERRUPT 0
18866 +} __attribute__ ((packed));
18868 +/* usb control, BAR0 + 0x0080 */
18869 +struct net2280_usb_regs {
18870 + // offset 0x0080
18871 + u32 stdrsp;
18872 +#define STALL_UNSUPPORTED_REQUESTS 31
18873 +#define SET_TEST_MODE 16
18874 +#define GET_OTHER_SPEED_CONFIGURATION 15
18875 +#define GET_DEVICE_QUALIFIER 14
18876 +#define SET_ADDRESS 13
18877 +#define ENDPOINT_SET_CLEAR_HALT 12
18878 +#define DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP 11
18879 +#define GET_STRING_DESCRIPTOR_2 10
18880 +#define GET_STRING_DESCRIPTOR_1 9
18881 +#define GET_STRING_DESCRIPTOR_0 8
18882 +#define GET_SET_INTERFACE 6
18883 +#define GET_SET_CONFIGURATION 5
18884 +#define GET_CONFIGURATION_DESCRIPTOR 4
18885 +#define GET_DEVICE_DESCRIPTOR 3
18886 +#define GET_ENDPOINT_STATUS 2
18887 +#define GET_INTERFACE_STATUS 1
18888 +#define GET_DEVICE_STATUS 0
18889 + u32 prodvendid;
18890 +#define PRODUCT_ID 16
18891 +#define VENDOR_ID 0
18892 + u32 relnum;
18893 + u32 usbctl;
18894 +#define SERIAL_NUMBER_INDEX 16
18895 +#define PRODUCT_ID_STRING_ENABLE 13
18896 +#define VENDOR_ID_STRING_ENABLE 12
18897 +#define USB_ROOT_PORT_WAKEUP_ENABLE 11
18898 +#define VBUS_PIN 10
18899 +#define TIMED_DISCONNECT 9
18900 +#define SUSPEND_IMMEDIATELY 7
18901 +#define SELF_POWERED_USB_DEVICE 6
18902 +#define REMOTE_WAKEUP_SUPPORT 5
18903 +#define PME_POLARITY 4
18904 +#define USB_DETECT_ENABLE 3
18905 +#define PME_WAKEUP_ENABLE 2
18906 +#define DEVICE_REMOTE_WAKEUP_ENABLE 1
18907 +#define SELF_POWERED_STATUS 0
18908 + // offset 0x0090
18909 + u32 usbstat;
18910 +#define HIGH_SPEED 7
18911 +#define FULL_SPEED 6
18912 +#define GENERATE_RESUME 5
18913 +#define GENERATE_DEVICE_REMOTE_WAKEUP 4
18914 + u32 xcvrdiag;
18915 +#define FORCE_HIGH_SPEED_MODE 31
18916 +#define FORCE_FULL_SPEED_MODE 30
18917 +#define USB_TEST_MODE 24
18918 +#define LINE_STATE 16
18919 +#define TRANSCEIVER_OPERATION_MODE 2
18920 +#define TRANSCEIVER_SELECT 1
18921 +#define TERMINATION_SELECT 0
18922 + u32 setup0123;
18923 + u32 setup4567;
18924 + // offset 0x0090
18925 + u32 _unused0;
18926 + u32 ouraddr;
18927 +#define FORCE_IMMEDIATE 7
18928 +#define OUR_USB_ADDRESS 0
18929 + u32 ourconfig;
18930 +} __attribute__ ((packed));
18932 +/* pci control, BAR0 + 0x0100 */
18933 +struct net2280_pci_regs {
18934 + // offset 0x0100
18935 + u32 pcimstctl;
18936 +#define PCI_ARBITER_PARK_SELECT 13
18937 +#define PCI_MULTI LEVEL_ARBITER 12
18938 +#define PCI_RETRY_ABORT_ENABLE 11
18939 +#define DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE 10
18940 +#define DMA_READ_MULTIPLE_ENABLE 9
18941 +#define DMA_READ_LINE_ENABLE 8
18942 +#define PCI_MASTER_COMMAND_SELECT 6
18943 +#define MEM_READ_OR_WRITE 0
18944 +#define IO_READ_OR_WRITE 1
18945 +#define CFG_READ_OR_WRITE 2
18946 +#define PCI_MASTER_START 5
18947 +#define PCI_MASTER_READ_WRITE 4
18948 +#define PCI_MASTER_WRITE 0
18949 +#define PCI_MASTER_READ 1
18950 +#define PCI_MASTER_BYTE_WRITE_ENABLES 0
18951 + u32 pcimstaddr;
18952 + u32 pcimstdata;
18953 + u32 pcimststat;
18954 +#define PCI_ARBITER_CLEAR 2
18955 +#define PCI_EXTERNAL_ARBITER 1
18956 +#define PCI_HOST_MODE 0
18957 +} __attribute__ ((packed));
18959 +/* dma control, BAR0 + 0x0180 ... array of four structs like this,
18960 + * for channels 0..3. see also struct net2280_dma: descriptor
18961 + * that can be loaded into some of these registers.
18962 + */
18963 +struct net2280_dma_regs { /* [11.7] */
18964 + // offset 0x0180, 0x01a0, 0x01c0, 0x01e0,
18965 + u32 dmactl;
18966 +#define DMA_SCATTER_GATHER_DONE_INTERRUPT_ENABLE 25
18967 +#define DMA_CLEAR_COUNT_ENABLE 21
18968 +#define DESCRIPTOR_POLLING_RATE 19
18969 +#define POLL_CONTINUOUS 0
18970 +#define POLL_1_USEC 1
18971 +#define POLL_100_USEC 2
18972 +#define POLL_1_MSEC 3
18973 +#define DMA_VALID_BIT_POLLING_ENABLE 18
18974 +#define DMA_VALID_BIT_ENABLE 17
18975 +#define DMA_SCATTER_GATHER_ENABLE 16
18976 +#define DMA_OUT_AUTO_START_ENABLE 4
18977 +#define DMA_PREEMPT_ENABLE 3
18978 +#define DMA_FIFO_VALIDATE 2
18979 +#define DMA_ENABLE 1
18980 +#define DMA_ADDRESS_HOLD 0
18981 + u32 dmastat;
18982 +#define DMA_SCATTER_GATHER_DONE_INTERRUPT 25
18983 +#define DMA_TRANSACTION_DONE_INTERRUPT 24
18984 +#define DMA_ABORT 1
18985 +#define DMA_START 0
18986 + u32 _unused0 [2];
18987 + // offset 0x0190, 0x01b0, 0x01d0, 0x01f0,
18988 + u32 dmacount;
18989 +#define VALID_BIT 31
18990 +#define DMA_DIRECTION 30
18991 +#define DMA_DONE_INTERRUPT_ENABLE 29
18992 +#define END_OF_CHAIN 28
18993 +#define DMA_BYTE_COUNT_MASK ((1<<24)-1)
18994 +#define DMA_BYTE_COUNT 0
18995 + u32 dmaaddr;
18996 + u32 dmadesc;
18997 + u32 _unused1;
18998 +} __attribute__ ((packed));
19000 +/* dedicated endpoint registers, BAR0 + 0x0200 */
19002 +struct net2280_dep_regs { /* [11.8] */
19003 + // offset 0x0200, 0x0210, 0x220, 0x230, 0x240
19004 + u32 dep_cfg;
19005 + // offset 0x0204, 0x0214, 0x224, 0x234, 0x244
19006 + u32 dep_rsp;
19007 + u32 _unused [2];
19008 +} __attribute__ ((packed));
19010 +/* configurable endpoint registers, BAR0 + 0x0300 ... array of seven structs
19011 + * like this, for ep0 then the configurable endpoints A..F
19012 + * ep0 reserved for control; E and F have only 64 bytes of fifo
19013 + */
19014 +struct net2280_ep_regs { /* [11.9] */
19015 + // offset 0x0300, 0x0320, 0x0340, 0x0360, 0x0380, 0x03a0, 0x03c0
19016 + u32 ep_cfg;
19017 +#define ENDPOINT_BYTE_COUNT 16
19018 +#define ENDPOINT_ENABLE 10
19019 +#define ENDPOINT_TYPE 8
19020 +#define ENDPOINT_DIRECTION 7
19021 +#define ENDPOINT_NUMBER 0
19022 + u32 ep_rsp;
19023 +#define SET_NAK_OUT_PACKETS 15
19024 +#define SET_EP_HIDE_STATUS_PHASE 14
19025 +#define SET_EP_FORCE_CRC_ERROR 13
19026 +#define SET_INTERRUPT_MODE 12
19027 +#define SET_CONTROL_STATUS_PHASE_HANDSHAKE 11
19028 +#define SET_NAK_OUT_PACKETS_MODE 10
19029 +#define SET_ENDPOINT_TOGGLE 9
19030 +#define SET_ENDPOINT_HALT 8
19031 +#define CLEAR_NAK_OUT_PACKETS 7
19032 +#define CLEAR_EP_HIDE_STATUS_PHASE 6
19033 +#define CLEAR_EP_FORCE_CRC_ERROR 5
19034 +#define CLEAR_INTERRUPT_MODE 4
19035 +#define CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE 3
19036 +#define CLEAR_NAK_OUT_PACKETS_MODE 2
19037 +#define CLEAR_ENDPOINT_TOGGLE 1
19038 +#define CLEAR_ENDPOINT_HALT 0
19039 + u32 ep_irqenb;
19040 +#define SHORT_PACKET_OUT_DONE_INTERRUPT_ENABLE 6
19041 +#define SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE 5
19042 +#define DATA_PACKET_RECEIVED_INTERRUPT_ENABLE 3
19043 +#define DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE 2
19044 +#define DATA_OUT_PING_TOKEN_INTERRUPT_ENABLE 1
19045 +#define DATA_IN_TOKEN_INTERRUPT_ENABLE 0
19046 + u32 ep_stat;
19047 +#define FIFO_VALID_COUNT 24
19048 +#define HIGH_BANDWIDTH_OUT_TRANSACTION_PID 22
19049 +#define TIMEOUT 21
19050 +#define USB_STALL_SENT 20
19051 +#define USB_IN_NAK_SENT 19
19052 +#define USB_IN_ACK_RCVD 18
19053 +#define USB_OUT_PING_NAK_SENT 17
19054 +#define USB_OUT_ACK_SENT 16
19055 +#define FIFO_OVERFLOW 13
19056 +#define FIFO_UNDERFLOW 12
19057 +#define FIFO_FULL 11
19058 +#define FIFO_EMPTY 10
19059 +#define FIFO_FLUSH 9
19060 +#define SHORT_PACKET_OUT_DONE_INTERRUPT 6
19061 +#define SHORT_PACKET_TRANSFERRED_INTERRUPT 5
19062 +#define NAK_OUT_PACKETS 4
19063 +#define DATA_PACKET_RECEIVED_INTERRUPT 3
19064 +#define DATA_PACKET_TRANSMITTED_INTERRUPT 2
19065 +#define DATA_OUT_PING_TOKEN_INTERRUPT 1
19066 +#define DATA_IN_TOKEN_INTERRUPT 0
19067 + // offset 0x0310, 0x0330, 0x0350, 0x0370, 0x0390, 0x03b0, 0x03d0
19068 + u32 ep_avail;
19069 + u32 ep_data;
19070 + u32 _unused0 [2];
19071 +} __attribute__ ((packed));
19073 +/*-------------------------------------------------------------------------*/
19075 +#ifdef __KERNEL__
19077 +/* indexed registers [11.10] are accessed indirectly
19078 + * caller must own the device lock.
19079 + */
19081 +static inline u32
19082 +get_idx_reg (struct net2280_regs *regs, u32 index)
19084 + writel (index, &regs->idxaddr);
19085 + /* NOTE: synchs device/cpu memory views */
19086 + return readl (&regs->idxdata);
19089 +static inline void
19090 +set_idx_reg (struct net2280_regs *regs, u32 index, u32 value)
19092 + writel (index, &regs->idxaddr);
19093 + writel (value, &regs->idxdata);
19094 + /* posted, may not be visible yet */
19097 +#endif /* __KERNEL__ */
19100 +#define REG_DIAG 0x0
19101 +#define RETRY_COUNTER 16
19102 +#define FORCE_PCI_SERR 11
19103 +#define FORCE_PCI_INTERRUPT 10
19104 +#define FORCE_USB_INTERRUPT 9
19105 +#define FORCE_CPU_INTERRUPT 8
19106 +#define ILLEGAL_BYTE_ENABLES 5
19107 +#define FAST_TIMES 4
19108 +#define FORCE_RECEIVE_ERROR 2
19109 +#define FORCE_TRANSMIT_CRC_ERROR 0
19110 +#define REG_FRAME 0x02 /* from last sof */
19111 +#define REG_CHIPREV 0x03 /* in bcd */
19112 +#define REG_HS_NAK_RATE 0x0a /* NAK per N uframes */
19114 +#define CHIPREV_1 0x0100
19115 +#define CHIPREV_1A 0x0110
19117 +#ifdef __KERNEL__
19119 +/* ep a-f highspeed and fullspeed maxpacket, addresses
19120 + * computed from ep->num
19121 + */
19122 +#define REG_EP_MAXPKT(dev,num) (((num) + 1) * 0x10 + \
19123 + (((dev)->gadget.speed == USB_SPEED_HIGH) ? 0 : 1))
19125 +/*-------------------------------------------------------------------------*/
19127 +/* [8.3] for scatter/gather i/o
19128 + * use struct net2280_dma_regs bitfields
19129 + */
19130 +struct net2280_dma {
19131 + u32 dmacount;
19132 + u32 dmaaddr; /* the buffer */
19133 + u32 dmadesc; /* next dma descriptor */
19134 + u32 _reserved;
19135 +} __attribute__ ((aligned (16)));
19137 +/*-------------------------------------------------------------------------*/
19139 +/* DRIVER DATA STRUCTURES and UTILITIES */
19141 +struct net2280_ep {
19142 + struct usb_ep ep;
19143 + struct net2280_ep_regs *regs;
19144 + struct net2280_dma_regs *dma;
19145 + struct net2280_dma *dummy;
19146 + dma_addr_t td_dma; /* of dummy */
19147 + struct net2280 *dev;
19148 + unsigned long irqs;
19150 + /* analogous to a host-side qh */
19151 + struct list_head queue;
19152 + const struct usb_endpoint_descriptor *desc;
19153 + unsigned num : 8,
19154 + fifo_size : 12,
19155 + in_fifo_validate : 1,
19156 + out_overflow : 1,
19157 + stopped : 1,
19158 + is_in : 1,
19159 + is_iso : 1;
19162 +static inline void allow_status (struct net2280_ep *ep)
19164 + /* ep0 only */
19165 + writel ( (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
19166 + | (1 << CLEAR_NAK_OUT_PACKETS)
19167 + | (1 << CLEAR_NAK_OUT_PACKETS_MODE)
19168 + , &ep->regs->ep_rsp);
19169 + ep->stopped = 1;
19172 +/* count (<= 4) bytes in the next fifo write will be valid */
19173 +static inline void set_fifo_bytecount (struct net2280_ep *ep, unsigned count)
19175 + writeb (count, 2 + (u8 *) &ep->regs->ep_cfg);
19178 +struct net2280_request {
19179 + struct usb_request req;
19180 + struct net2280_dma *td;
19181 + dma_addr_t td_dma;
19182 + struct list_head queue;
19183 + unsigned mapped : 1,
19184 + valid : 1;
19187 +struct net2280 {
19188 + /* each pci device provides one gadget, several endpoints */
19189 + struct usb_gadget gadget;
19190 + spinlock_t lock;
19191 + struct net2280_ep ep [7];
19192 + struct usb_gadget_driver *driver;
19193 + unsigned enabled : 1,
19194 + protocol_stall : 1,
19195 + softconnect : 1,
19196 + got_irq : 1,
19197 + region : 1;
19198 + u16 chiprev;
19200 + /* pci state used to access those endpoints */
19201 + struct pci_dev *pdev;
19202 + struct net2280_regs *regs;
19203 + struct net2280_usb_regs *usb;
19204 + struct net2280_pci_regs *pci;
19205 + struct net2280_dma_regs *dma;
19206 + struct net2280_dep_regs *dep;
19207 + struct net2280_ep_regs *epregs;
19209 + struct pci_pool *requests;
19210 + // statistics...
19213 +static inline void set_halt (struct net2280_ep *ep)
19215 + /* ep0 and bulk/intr endpoints */
19216 + writel ( (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
19217 + /* set NAK_OUT for erratum 0114 */
19218 + | ((ep->dev->chiprev == CHIPREV_1) << SET_NAK_OUT_PACKETS)
19219 + | (1 << SET_ENDPOINT_HALT)
19220 + , &ep->regs->ep_rsp);
19223 +static inline void clear_halt (struct net2280_ep *ep)
19225 + /* ep0 and bulk/intr endpoints */
19226 + writel ( (1 << CLEAR_ENDPOINT_HALT)
19227 + | (1 << CLEAR_ENDPOINT_TOGGLE)
19228 + /* unless the gadget driver left a short packet in the
19229 + * fifo, this reverses the erratum 0114 workaround.
19230 + */
19231 + | ((ep->dev->chiprev == CHIPREV_1) << CLEAR_NAK_OUT_PACKETS)
19232 + , &ep->regs->ep_rsp);
19235 +#ifdef USE_RDK_LEDS
19237 +static inline void net2280_led_init (struct net2280 *dev)
19239 + /* LED3 (green) is on during USB activity. note erratum 0113. */
19240 + writel ((1 << GPIO3_LED_SELECT)
19241 + | (1 << GPIO3_OUTPUT_ENABLE)
19242 + | (1 << GPIO2_OUTPUT_ENABLE)
19243 + | (1 << GPIO1_OUTPUT_ENABLE)
19244 + | (1 << GPIO0_OUTPUT_ENABLE)
19245 + , &dev->regs->gpioctl);
19248 +/* indicate speed with bi-color LED 0/1 */
19249 +static inline
19250 +void net2280_led_speed (struct net2280 *dev, enum usb_device_speed speed)
19252 + u32 val = readl (&dev->regs->gpioctl);
19253 + switch (speed) {
19254 + case USB_SPEED_HIGH: /* green */
19255 + val &= ~(1 << GPIO0_DATA);
19256 + val |= (1 << GPIO1_DATA);
19257 + break;
19258 + case USB_SPEED_FULL: /* red */
19259 + val &= ~(1 << GPIO1_DATA);
19260 + val |= (1 << GPIO0_DATA);
19261 + break;
19262 + default: /* (off/black) */
19263 + val &= ~((1 << GPIO1_DATA) | (1 << GPIO0_DATA));
19264 + break;
19266 + writel (val, &dev->regs->gpioctl);
19269 +/* indicate power with LED 2 */
19270 +static inline void net2280_led_active (struct net2280 *dev, int is_active)
19272 + u32 val = readl (&dev->regs->gpioctl);
19274 + // FIXME this LED never seems to turn on.
19275 + if (is_active)
19276 + val |= GPIO2_DATA;
19277 + else
19278 + val &= ~GPIO2_DATA;
19279 + writel (val, &dev->regs->gpioctl);
19281 +static inline void net2280_led_shutdown (struct net2280 *dev)
19283 + /* turn off all four GPIO*_DATA bits */
19284 + writel (readl (&dev->regs->gpioctl) & ~0x0f,
19285 + &dev->regs->gpioctl);
19288 +#else
19290 +#define net2280_led_init(dev) do { } while (0)
19291 +#define net2280_led_speed(dev, speed) do { } while (0)
19292 +#define net2280_led_shutdown(dev) do { } while (0)
19294 +#endif
19296 +/*-------------------------------------------------------------------------*/
19298 +#define xprintk(dev,level,fmt,args...) \
19299 + printk(level "%s %s: " fmt , driver_name , \
19300 + dev->pdev->slot_name , ## args)
19302 +#ifdef DEBUG
19303 +#undef DEBUG
19304 +#define DEBUG(dev,fmt,args...) \
19305 + xprintk(dev , KERN_DEBUG , fmt , ## args)
19306 +#else
19307 +#define DEBUG(dev,fmt,args...) \
19308 + do { } while (0)
19309 +#endif /* DEBUG */
19311 +#ifdef VERBOSE
19312 +#define VDEBUG DEBUG
19313 +#else
19314 +#define VDEBUG(dev,fmt,args...) \
19315 + do { } while (0)
19316 +#endif /* VERBOSE */
19318 +#define ERROR(dev,fmt,args...) \
19319 + xprintk(dev , KERN_ERR , fmt , ## args)
19320 +#define WARN(dev,fmt,args...) \
19321 + xprintk(dev , KERN_WARNING , fmt , ## args)
19322 +#define INFO(dev,fmt,args...) \
19323 + xprintk(dev , KERN_INFO , fmt , ## args)
19325 +/*-------------------------------------------------------------------------*/
19327 +static inline void start_out_naking (struct net2280_ep *ep)
19329 + /* NOTE: hardware races lurk here, and PING protocol issues */
19330 + writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
19331 + /* synch with device */
19332 + readl (&ep->regs->ep_rsp);
19335 +#ifdef DEBUG
19336 +static inline void assert_out_naking (struct net2280_ep *ep, const char *where)
19338 + u32 tmp = readl (&ep->regs->ep_stat);
19340 + if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
19341 + DEBUG (ep->dev, "%s %s %08x !NAK\n",
19342 + ep->ep.name, where, tmp);
19343 + writel ((1 << SET_NAK_OUT_PACKETS),
19344 + &ep->regs->ep_rsp);
19347 +#define ASSERT_OUT_NAKING(ep) assert_out_naking(ep,__FUNCTION__)
19348 +#else
19349 +#define ASSERT_OUT_NAKING(ep) do {} while (0)
19350 +#endif
19352 +static inline void stop_out_naking (struct net2280_ep *ep)
19354 + u32 tmp;
19356 + tmp = readl (&ep->regs->ep_stat);
19357 + if ((tmp & (1 << NAK_OUT_PACKETS)) != 0)
19358 + writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
19361 +/*-------------------------------------------------------------------------*/
19363 +/* 2.5 and 2.4.older portability changes ... */
19365 +#ifndef container_of
19366 +#define container_of list_entry
19367 +#endif
19369 +#ifndef likely
19370 +#define likely(x) (x)
19371 +#define unlikely(x) (x)
19372 +#endif
19374 +#ifndef BUG_ON
19375 +#define BUG_ON(condition) do { if (unlikely((condition)!=0)) BUG(); } while(0)
19376 +#endif
19378 +#ifndef WARN_ON
19379 +#define WARN_ON(x) do { } while (0)
19380 +#endif
19382 +#ifndef IRQ_NONE
19383 +typedef void irqreturn_t;
19384 +#define IRQ_NONE
19385 +#define IRQ_HANDLED
19386 +#define IRQ_RETVAL(x)
19387 +#endif
19389 +#endif /* __KERNEL__ */
19390 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/pxa2xx_udc.c kernel/drivers/usb/gadget/pxa2xx_udc.c
19391 --- /tmp/kernel/drivers/usb/gadget/pxa2xx_udc.c 1970-01-01 01:00:00.000000000 +0100
19392 +++ kernel/drivers/usb/gadget/pxa2xx_udc.c 2005-04-22 17:53:19.492530073 +0200
19393 @@ -0,0 +1,2486 @@
19395 + * linux/drivers/usb/gadget/pxa2xx_udc.c
19396 + * Intel PXA2xx and IXP4xx on-chip full speed USB device controllers
19398 + * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
19399 + * Copyright (C) 2003 Robert Schwebel, Pengutronix
19400 + * Copyright (C) 2003 Benedikt Spranger, Pengutronix
19401 + * Copyright (C) 2003 David Brownell
19403 + * This program is free software; you can redistribute it and/or modify
19404 + * it under the terms of the GNU General Public License as published by
19405 + * the Free Software Foundation; either version 2 of the License, or
19406 + * (at your option) any later version.
19408 + * This program is distributed in the hope that it will be useful,
19409 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19410 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19411 + * GNU General Public License for more details.
19413 + * You should have received a copy of the GNU General Public License
19414 + * along with this program; if not, write to the Free Software
19415 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19417 + */
19419 +#define DEBUG 1
19420 +// #define VERBOSE DBG_VERBOSE
19422 +#include <linux/config.h>
19423 +#include <linux/module.h>
19424 +#include <linux/kernel.h>
19425 +#include <linux/ioport.h>
19426 +#include <linux/types.h>
19427 +#include <linux/version.h>
19428 +#include <linux/errno.h>
19429 +#include <linux/delay.h>
19430 +#include <linux/sched.h>
19431 +#include <linux/slab.h>
19432 +#include <linux/init.h>
19433 +#include <linux/timer.h>
19434 +#include <linux/list.h>
19435 +#include <linux/interrupt.h>
19436 +#include <linux/proc_fs.h>
19437 +#include <linux/mm.h>
19438 +// #include <linux/device.h>
19440 +#include <asm/byteorder.h>
19441 +#include <asm/dma.h>
19442 +#include <asm/io.h>
19443 +#include <asm/irq.h>
19444 +#include <asm/system.h>
19445 +#include <asm/unaligned.h>
19446 +#include <asm/proc/cache.h>
19448 +#include <linux/usb_ch9.h>
19449 +#include <linux/usb_gadget.h>
19453 + * This driver handles the USB Device Controller (UDC) in Intel's PXA 2xx
19454 + * series processors. The UDC for the IXP 4xx series is very similar.
19455 + * There are fifteen endpoints, in addition to ep0.
19457 + * Such controller drivers work with a gadget driver. The gadget driver
19458 + * returns descriptors, implements configuration and data protocols used
19459 + * by the host to interact with this device, and allocates endpoints to
19460 + * the different protocol interfaces. The controller driver virtualizes
19461 + * usb hardware so that the gadget drivers will be more portable.
19462 + *
19463 + * This UDC hardware wants to implement a bit too much USB protocol, so
19464 + * it constrains the sorts of USB configuration change events that work.
19465 + * The errata for these chips are misleading; some "fixed" bugs from
19466 + * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
19467 + */
19469 +/* NOTE: the 2.6 driver is probably the most current version */
19470 +#define DRIVER_VERSION "5-Jan-2004"
19471 +#define DRIVER_DESC "PXA 2xx USB Device Controller driver"
19473 +static const char driver_name [] = "pxa2xx_udc";
19475 +static const char ep0name [] = "ep0";
19478 +// #define USE_DMA
19479 +// #define USE_OUT_DMA
19480 +// #define DISABLE_TEST_MODE
19482 +#ifdef CONFIG_PROC_FS
19483 +#define UDC_PROC_FILE
19484 +#endif
19486 +#ifdef CONFIG_ARCH_IXP425
19487 +#undef USE_DMA
19489 +/* cpu-specific register addresses are compiled in to this code */
19490 +#ifdef CONFIG_ARCH_PXA
19491 +#error "Can't configure both IXP and PXA"
19492 +#endif
19494 +#endif
19496 +#ifdef CONFIG_EMBEDDED
19497 +/* few strings, and little code to use them */
19498 +#undef DEBUG
19499 +#undef UDC_PROC_FILE
19500 +#endif
19503 +#include "pxa2xx_udc.h"
19505 +#ifdef USE_DMA
19506 +static int use_dma = 1;
19507 +MODULE_PARM (use_dma, "i");
19508 +MODULE_PARM_DESC (use_dma, "true to use dma");
19510 +static void dma_nodesc_handler (int dmach, void *_ep, struct pt_regs *r);
19511 +static void kick_dma(struct pxa2xx_ep *ep, struct pxa2xx_request *req);
19513 +#ifdef USE_OUT_DMA
19514 +#define DMASTR " (dma support)"
19515 +#else
19516 +#define DMASTR " (dma in)"
19517 +#endif
19519 +#else /* !USE_DMA */
19520 +#define DMASTR " (pio only)"
19521 +#undef USE_OUT_DMA
19522 +#endif
19524 +#ifdef CONFIG_USB_PXA2XX_SMALL
19525 +#define SIZE_STR " (small)"
19526 +#else
19527 +#define SIZE_STR ""
19528 +#endif
19530 +#ifdef DISABLE_TEST_MODE
19531 +/* (mode == 0) == no undocumented chip tweaks
19532 + * (mode & 1) == double buffer bulk IN
19533 + * (mode & 2) == double buffer bulk OUT
19534 + * ... so mode = 3 (or 7, 15, etc) does it for both
19535 + */
19536 +static ushort fifo_mode = 0;
19537 +MODULE_PARM (fifo_mode, "h");
19538 +MODULE_PARM_DESC (fifo_mode, "pxa2xx udc fifo mode");
19539 +#endif
19541 +/* ---------------------------------------------------------------------------
19542 + * endpoint related parts of the api to the usb controller hardware,
19543 + * used by gadget driver; and the inner talker-to-hardware core.
19544 + * ---------------------------------------------------------------------------
19545 + */
19547 +static void pxa2xx_ep_fifo_flush (struct usb_ep *ep);
19548 +static void nuke (struct pxa2xx_ep *, int status);
19550 +static void pio_irq_enable(int bEndpointAddress)
19552 + bEndpointAddress &= 0xf;
19553 + if (bEndpointAddress < 8)
19554 + UICR0 &= ~(1 << bEndpointAddress);
19555 + else {
19556 + bEndpointAddress -= 8;
19557 + UICR1 &= ~(1 << bEndpointAddress);
19561 +static void pio_irq_disable(int bEndpointAddress)
19563 + bEndpointAddress &= 0xf;
19564 + if (bEndpointAddress < 8)
19565 + UICR0 |= 1 << bEndpointAddress;
19566 + else {
19567 + bEndpointAddress -= 8;
19568 + UICR1 |= 1 << bEndpointAddress;
19572 +/* The UDCCR reg contains mask and interrupt status bits,
19573 + * so using '|=' isn't safe as it may ack an interrupt.
19574 + */
19575 +#define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
19577 +static inline void udc_set_mask_UDCCR(int mask)
19579 + UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
19582 +static inline void udc_clear_mask_UDCCR(int mask)
19584 + UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
19587 +static inline void udc_ack_int_UDCCR(int mask)
19589 + /* udccr contains the bits we dont want to change */
19590 + __u32 udccr = UDCCR & UDCCR_MASK_BITS;
19592 + UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
19596 + * endpoint enable/disable
19598 + * we need to verify the descriptors used to enable endpoints. since pxa2xx
19599 + * endpoint configurations are fixed, and are pretty much always enabled,
19600 + * there's not a lot to manage here.
19602 + * because pxa2xx can't selectively initialize bulk (or interrupt) endpoints,
19603 + * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
19604 + * for a single interface (with only the default altsetting) and for gadget
19605 + * drivers that don't halt endpoints (not reset by set_interface). that also
19606 + * means that if you use ISO, you must violate the USB spec rule that all
19607 + * iso endpoints must be in non-default altsettings.
19608 + */
19609 +static int pxa2xx_ep_enable (struct usb_ep *_ep,
19610 + const struct usb_endpoint_descriptor *desc)
19612 + struct pxa2xx_ep *ep;
19613 + struct pxa2xx_udc *dev;
19615 + ep = container_of (_ep, struct pxa2xx_ep, ep);
19616 + if (!_ep || !desc || ep->desc || _ep->name == ep0name
19617 + || desc->bDescriptorType != USB_DT_ENDPOINT
19618 + || ep->bEndpointAddress != desc->bEndpointAddress
19619 + || ep->fifo_size < le16_to_cpu
19620 + (desc->wMaxPacketSize)) {
19621 + DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
19622 + return -EINVAL;
19625 + /* xfer types must match, except that interrupt ~= bulk */
19626 + if (ep->bmAttributes != desc->bmAttributes
19627 + && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
19628 + && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
19629 + DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
19630 + return -EINVAL;
19633 + /* hardware _could_ do smaller, but driver doesn't */
19634 + if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
19635 + && le16_to_cpu (desc->wMaxPacketSize)
19636 + != BULK_FIFO_SIZE)
19637 + || !desc->wMaxPacketSize) {
19638 + DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
19639 + return -ERANGE;
19642 + dev = ep->dev;
19643 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
19644 + DMSG("%s, bogus device state\n", __FUNCTION__);
19645 + return -ESHUTDOWN;
19648 + ep->desc = desc;
19649 + ep->dma = -1;
19650 + ep->stopped = 0;
19651 + ep->pio_irqs = ep->dma_irqs = 0;
19652 + ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
19654 + /* flush fifo (mostly for OUT buffers) */
19655 + pxa2xx_ep_fifo_flush (_ep);
19657 + /* ... reset halt state too, if we could ... */
19659 +#ifdef USE_DMA
19660 + /* for (some) bulk and ISO endpoints, try to get a DMA channel and
19661 + * bind it to the endpoint. otherwise use PIO.
19662 + */
19663 + switch (ep->bmAttributes) {
19664 + case USB_ENDPOINT_XFER_ISOC:
19665 + if (le16_to_cpu(desc->wMaxPacketSize) % 32)
19666 + break;
19667 + // fall through
19668 + case USB_ENDPOINT_XFER_BULK:
19669 + if (!use_dma || !ep->reg_drcmr)
19670 + break;
19671 + /* no bulk-out dma yet (pointless w/o descriptors) */
19672 + if ((ep->bmAttributes == USB_ENDPOINT_XFER_BULK)
19673 + && (ep->bEndpointAddress & USB_DIR_IN) == 0) {
19674 + DMSG("%s dma-out NYI\n", _ep->name);
19675 + break;
19677 + ep->dma = pxa_request_dma ((char *)_ep->name,
19678 + (le16_to_cpu(desc->wMaxPacketSize) > 64)
19679 + ? DMA_PRIO_MEDIUM /* some iso */
19680 + : DMA_PRIO_LOW,
19681 + // FIXME or ep_out_dma .. ..
19682 + dma_nodesc_handler, ep);
19683 + if (ep->dma >= 0) {
19684 + *ep->reg_drcmr = DRCMR_MAPVLD | ep->dma;
19685 + DMSG("%s using dma%d\n", _ep->name, ep->dma);
19688 +#endif
19690 + DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
19691 + return 0;
19694 +static int pxa2xx_ep_disable (struct usb_ep *_ep)
19696 + struct pxa2xx_ep *ep;
19698 + ep = container_of (_ep, struct pxa2xx_ep, ep);
19699 + if (!_ep || !ep->desc) {
19700 + DMSG("%s, %s not enabled\n", __FUNCTION__,
19701 + _ep ? ep->ep.name : NULL);
19702 + return -EINVAL;
19704 + nuke (ep, -ESHUTDOWN);
19706 +#ifdef USE_DMA
19707 + if (ep->dma >= 0) {
19708 + *ep->reg_drcmr = 0;
19709 + pxa_free_dma (ep->dma);
19710 + ep->dma = -1;
19712 +#endif
19714 + /* flush fifo (mostly for IN buffers) */
19715 + pxa2xx_ep_fifo_flush (_ep);
19717 + ep->desc = 0;
19718 + ep->stopped = 1;
19720 + DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
19721 + return 0;
19724 +/*-------------------------------------------------------------------------*/
19726 +/* for the pxa2xx, these can just wrap kmalloc/kfree. gadget drivers
19727 + * must still pass correctly initialized endpoints, since other controller
19728 + * drivers may care about how it's currently set up (dma issues etc).
19729 + */
19732 + * pxa2xx_ep_alloc_request - allocate a request data structure
19733 + */
19734 +static struct usb_request *
19735 +pxa2xx_ep_alloc_request (struct usb_ep *_ep, int gfp_flags)
19737 + struct pxa2xx_request *req;
19739 + /* FIXME for bulk out-dma endpoints, preallocate a frame's worth of
19740 + * (aligned) dma descriptors at the end of the request
19741 + */
19743 + req = kmalloc (sizeof *req, gfp_flags);
19744 + if (!req)
19745 + return 0;
19747 + memset (req, 0, sizeof *req);
19748 + INIT_LIST_HEAD (&req->queue);
19749 + return &req->req;
19754 + * pxa2xx_ep_free_request - deallocate a request data structure
19755 + */
19756 +static void
19757 +pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
19759 + struct pxa2xx_request *req;
19761 + req = container_of (_req, struct pxa2xx_request, req);
19762 + WARN_ON (!list_empty (&req->queue));
19763 + kfree(req);
19767 +/* PXA cache needs flushing with DMA I/O (it's dma-incoherent), but there's
19768 + * no device-affinity and the heap works perfectly well for i/o buffers.
19769 + */
19770 +static void *
19771 +pxa2xx_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
19772 + dma_addr_t *dma, int gfp_flags)
19774 + char *retval;
19776 + retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
19777 + if (retval)
19778 + *dma = virt_to_bus (retval);
19779 + return retval;
19782 +static void
19783 +pxa2xx_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma,
19784 + unsigned bytes)
19786 + kfree (buf);
19789 +/*-------------------------------------------------------------------------*/
19792 + * done - retire a request; caller blocked irqs
19793 + */
19794 +static void done(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int status)
19796 + unsigned stopped = ep->stopped;
19798 + list_del_init(&req->queue);
19800 + if (likely (req->req.status == -EINPROGRESS))
19801 + req->req.status = status;
19802 + else
19803 + status = req->req.status;
19805 + if (status && status != -ESHUTDOWN)
19806 + DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
19807 + ep->ep.name, &req->req, status,
19808 + req->req.actual, req->req.length);
19810 + /* don't modify queue heads during completion callback */
19811 + ep->stopped = 1;
19812 + req->req.complete(&ep->ep, &req->req);
19813 + ep->stopped = stopped;
19817 +static inline void ep0_idle (struct pxa2xx_udc *dev)
19819 + dev->ep0state = EP0_IDLE;
19820 + LED_EP0_OFF;
19823 +static int
19824 +write_packet(volatile u32 *uddr, struct pxa2xx_request *req, unsigned max)
19826 + u8 *buf;
19827 + unsigned length, count;
19829 + buf = req->req.buf + req->req.actual;
19830 + prefetch(buf);
19832 + /* how big will this packet be? */
19833 + length = min(req->req.length - req->req.actual, max);
19834 + req->req.actual += length;
19836 + count = length;
19837 + while (likely(count--))
19838 + *uddr = *buf++;
19840 + return length;
19844 + * write to an IN endpoint fifo, as many packets as possible.
19845 + * irqs will use this to write the rest later.
19846 + * caller guarantees at least one packet buffer is ready (or a zlp).
19847 + */
19848 +static int
19849 +write_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
19851 + unsigned max;
19853 + max = le16_to_cpu(ep->desc->wMaxPacketSize);
19854 + do {
19855 + unsigned count;
19856 + int is_last, is_short;
19858 + count = write_packet(ep->reg_uddr, req, max);
19860 + /* last packet is usually short (or a zlp) */
19861 + if (unlikely (count != max))
19862 + is_last = is_short = 1;
19863 + else {
19864 + if (likely(req->req.length != req->req.actual)
19865 + || req->req.zero)
19866 + is_last = 0;
19867 + else
19868 + is_last = 1;
19869 + /* interrupt/iso maxpacket may not fill the fifo */
19870 + is_short = unlikely (max < ep->fifo_size);
19873 + DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
19874 + ep->ep.name, count,
19875 + is_last ? "/L" : "", is_short ? "/S" : "",
19876 + req->req.length - req->req.actual, req);
19878 + /* let loose that packet. maybe try writing another one,
19879 + * double buffering might work. TSP, TPC, and TFS
19880 + * bit values are the same for all normal IN endpoints.
19881 + */
19882 + *ep->reg_udccs = UDCCS_BI_TPC;
19883 + if (is_short)
19884 + *ep->reg_udccs = UDCCS_BI_TSP;
19886 + /* requests complete when all IN data is in the FIFO */
19887 + if (is_last) {
19888 + done (ep, req, 0);
19889 + if (list_empty(&ep->queue) || unlikely(ep->dma >= 0))
19890 + pio_irq_disable (ep->bEndpointAddress);
19891 +#ifdef USE_DMA
19892 + if (unlikely(ep->dma >= 0) && !list_empty(&ep->queue)) {
19893 +DMSG("%s pio2dma\n", ep->ep.name);
19894 + req = list_entry(ep->queue.next,
19895 + struct pxa2xx_request, queue);
19896 + kick_dma(ep,req);
19897 + return 0;
19899 +#endif
19900 + return 1;
19903 + // TODO experiment: how robust can fifo mode tweaking be?
19904 + // the double buffering could speed up I/O a bunch.
19906 + } while (*ep->reg_udccs & UDCCS_BI_TFS);
19907 + return 0;
19910 +/* caller asserts req->pending (ep0 irq status nyet cleared); starts
19911 + * ep0 data stage. these chips want very simple state transitions.
19912 + */
19913 +static inline
19914 +void ep0start(struct pxa2xx_udc *dev, u32 flags, const char *tag)
19916 + UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
19917 + USIR0 = USIR0_IR0;
19918 + dev->req_pending = 0;
19919 + DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
19920 + __FUNCTION__, tag, UDCCS0, flags);
19923 +static int
19924 +write_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
19926 + unsigned count;
19927 + int is_short;
19929 + count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
19930 + ep->dev->stats.write.bytes += count;
19932 + /* last packet "must be" short (or a zlp) */
19933 + is_short = (count != EP0_FIFO_SIZE);
19935 + DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
19936 + req->req.length - req->req.actual, req);
19938 + if (unlikely (is_short)) {
19939 + if (ep->dev->req_pending)
19940 + ep0start(ep->dev, UDCCS0_IPR, "short IN");
19941 + else
19942 + UDCCS0 = UDCCS0_IPR;
19944 + count = req->req.length;
19945 + done (ep, req, 0);
19946 + ep0_idle(ep->dev);
19947 +#if 1
19948 + /* This seems to get rid of lost status irqs in some cases:
19949 + * host responds quickly, or next request involves config
19950 + * change automagic, or should have been hidden, or ...
19952 + * FIXME get rid of all udelays possible...
19953 + */
19954 + if (count >= EP0_FIFO_SIZE) {
19955 + count = 100;
19956 + do {
19957 + if ((UDCCS0 & UDCCS0_OPR) != 0) {
19958 + /* clear OPR, generate ack */
19959 + UDCCS0 = UDCCS0_OPR;
19960 + break;
19962 + count--;
19963 + udelay(1);
19964 + } while (count);
19966 +#endif
19967 + } else if (ep->dev->req_pending)
19968 + ep0start(ep->dev, 0, "IN");
19969 + return is_short;
19974 + * read_fifo - unload packet(s) from the fifo we use for usb OUT
19975 + * transfers and put them into the request. caller should have made
19976 + * sure there's at least one packet ready.
19978 + * returns true if the request completed because of short packet or the
19979 + * request buffer having filled (and maybe overran till end-of-packet).
19980 + */
19981 +static int
19982 +read_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
19984 + for (;;) {
19985 + u32 udccs;
19986 + u8 *buf;
19987 + unsigned bufferspace, count, is_short;
19989 + /* make sure there's a packet in the FIFO.
19990 + * UDCCS_{BO,IO}_RPC are all the same bit value.
19991 + * UDCCS_{BO,IO}_RNE are all the same bit value.
19992 + */
19993 + udccs = *ep->reg_udccs;
19994 + if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
19995 + break;
19996 + buf = req->req.buf + req->req.actual;
19997 + prefetchw(buf);
19998 + bufferspace = req->req.length - req->req.actual;
20000 + /* read all bytes from this packet */
20001 + if (likely (udccs & UDCCS_BO_RNE)) {
20002 + count = 1 + (0x0ff & *ep->reg_ubcr);
20003 + req->req.actual += min (count, bufferspace);
20004 + } else /* zlp */
20005 + count = 0;
20006 + is_short = (count < ep->ep.maxpacket);
20007 + DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
20008 + ep->ep.name, udccs, count,
20009 + is_short ? "/S" : "",
20010 + req, req->req.actual, req->req.length);
20011 + while (likely (count-- != 0)) {
20012 + u8 byte = (u8) *ep->reg_uddr;
20014 + if (unlikely (bufferspace == 0)) {
20015 + /* this happens when the driver's buffer
20016 + * is smaller than what the host sent.
20017 + * discard the extra data.
20018 + */
20019 + if (req->req.status != -EOVERFLOW)
20020 + DMSG("%s overflow %d\n",
20021 + ep->ep.name, count);
20022 + req->req.status = -EOVERFLOW;
20023 + } else {
20024 + *buf++ = byte;
20025 + bufferspace--;
20028 + *ep->reg_udccs = UDCCS_BO_RPC;
20029 + /* RPC/RSP/RNE could now reflect the other packet buffer */
20031 + /* iso is one request per packet */
20032 + if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
20033 + if (udccs & UDCCS_IO_ROF)
20034 + req->req.status = -EHOSTUNREACH;
20035 + /* more like "is_done" */
20036 + is_short = 1;
20039 + /* completion */
20040 + if (is_short || req->req.actual == req->req.length) {
20041 + done (ep, req, 0);
20042 + if (list_empty(&ep->queue))
20043 + pio_irq_disable (ep->bEndpointAddress);
20044 + return 1;
20047 + /* finished that packet. the next one may be waiting... */
20049 + return 0;
20053 + * special ep0 version of the above. no UBCR0 or double buffering; status
20054 + * handshaking is magic. most device protocols don't need control-OUT.
20055 + * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
20056 + * protocols do use them.
20057 + */
20058 +static int
20059 +read_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
20061 + u8 *buf, byte;
20062 + unsigned bufferspace;
20064 + buf = req->req.buf + req->req.actual;
20065 + bufferspace = req->req.length - req->req.actual;
20067 + while (UDCCS0 & UDCCS0_RNE) {
20068 + byte = (u8) UDDR0;
20070 + if (unlikely (bufferspace == 0)) {
20071 + /* this happens when the driver's buffer
20072 + * is smaller than what the host sent.
20073 + * discard the extra data.
20074 + */
20075 + if (req->req.status != -EOVERFLOW)
20076 + DMSG("%s overflow\n", ep->ep.name);
20077 + req->req.status = -EOVERFLOW;
20078 + } else {
20079 + *buf++ = byte;
20080 + req->req.actual++;
20081 + bufferspace--;
20085 + UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
20087 + /* completion */
20088 + if (req->req.actual >= req->req.length)
20089 + return 1;
20091 + /* finished that packet. the next one may be waiting... */
20092 + return 0;
20095 +#ifdef USE_DMA
20097 +static inline void
20098 +start_dma_nodesc(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int is_in)
20100 + u32 dcmd = req->req.length;
20101 + u32 buf = virt_to_bus (req->req.buf);
20102 + u32 fifo = io_v2p ((u32)ep->reg_uddr);
20104 + /* no-descriptor mode can be simple for bulk-in, iso-in, iso-out */
20105 + DCSR(ep->dma) = DCSR_NODESC;
20106 + dcmd |= DCMD_BURST32 | DCMD_ENDIRQEN | DCMD_WIDTH1;
20107 + if (is_in) {
20108 + DSADR(ep->dma) = buf;
20109 + DTADR(ep->dma) = fifo;
20110 + dcmd |= DCMD_FLOWTRG | DCMD_INCSRCADDR;
20111 + } else {
20112 + DSADR(ep->dma) = fifo;
20113 + DTADR(ep->dma) = buf;
20114 + dcmd |= DCMD_FLOWSRC | DCMD_INCTRGADDR;
20116 + DCMD(ep->dma) = dcmd;
20117 + DCSR(ep->dma) = DCSR_RUN | DCSR_STOPIRQEN | DCSR_NODESC;
20118 + /* and later the dma handler gets called */
20121 +static void kick_dma(struct pxa2xx_ep *ep, struct pxa2xx_request *req)
20123 + if (ep->bEndpointAddress & USB_DIR_IN) {
20124 + /* docs imply we can't preload with pio */
20125 + if ((((u32)req->req.buf) & 0x0f) != 0) {
20126 +// VERBOSE
20127 + DMSG("%s bad DMA align %p\n",
20128 + ep->ep.name, req->req.buf);
20129 +pio_in:
20130 +// FIXME PIO fallback doesn't work right yet (recovery?)
20131 +DMSG("%s dma2pio\n", ep->ep.name);
20132 + pio_irq_enable(ep->bEndpointAddress);
20133 + if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0)
20134 + (void) write_fifo(ep, req);
20135 + return;
20137 + /* dmacount 0 means end-of-transfer */
20138 + if (unlikely((req->req.length - req->req.actual) == 0)) {
20139 +// VERBOSE
20140 + DMSG("%s zlp dma write...\n", ep->ep.name);
20141 + goto pio_in;
20143 + start_dma_nodesc(ep, req, USB_DIR_IN);
20144 + } else {
20145 + // if ISO, use no-descriptor DMA
20146 + BUG();
20150 +static void cancel_dma(struct pxa2xx_ep *ep)
20152 + struct pxa2xx_request *req;
20153 + u32 tmp;
20155 + if (DCSR(ep->dma) == 0 || list_empty(&ep->queue))
20156 + return;
20158 + DCSR(ep->dma) = 0;
20159 + while ((DCSR(ep->dma) & DCSR_STOPSTATE) == 0)
20160 + cpu_relax();
20162 + req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
20163 + tmp = DCMD(ep->dma) & DCMD_LENGTH;
20164 + req->req.actual = req->req.length - (tmp & DCMD_LENGTH);
20166 + /* the last tx packet may be incomplete, so flush the fifo.
20167 + * FIXME correct req.actual if we can
20168 + */
20169 + if (ep->bEndpointAddress & USB_DIR_IN)
20170 + *ep->reg_udccs = UDCCS_BI_FTF;
20173 +static void dma_nodesc_handler(int dmach, void *_ep, struct pt_regs *r)
20175 + struct pxa2xx_ep *ep = _ep;
20176 + struct pxa2xx_request *req;
20177 + u32 tmp;
20179 + req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
20181 + ep->dev->stats.irqs++;
20182 + HEX_DISPLAY(ep->dev->stats.irqs);
20184 + /* ack/clear */
20185 + tmp = DCSR(ep->dma);
20186 + DCSR(ep->dma) = tmp;
20187 + if ((tmp & DCSR_STOPSTATE) == 0
20188 + || (DDADR(ep->dma) & DDADR_STOP) != 0) {
20189 + DBG(DBG_VERBOSE, "%s, dcsr %08x ddadr %08x\n",
20190 + ep->ep.name, DCSR(ep->dma), DDADR(ep->dma));
20191 + return;
20193 + DCSR(ep->dma) = 0; /* clear DCSR_STOPSTATE */
20195 + /* wrap up the transfer, and collect status */
20196 + if (unlikely(tmp & DCSR_BUSERR))
20197 + req->req.status = -EIO;
20198 + tmp = DCMD(ep->dma);
20199 + req->req.actual = req->req.length - (tmp & DCMD_LENGTH);
20200 + tmp = 1; /* normally this is the last packet */
20202 + if (ep->bEndpointAddress & USB_DIR_IN) {
20203 + /* maybe validate final short packet */
20204 + if ((ep->bmAttributes == USB_ENDPOINT_XFER_BULK
20205 + && req->req.actual % BULK_FIFO_SIZE)
20206 + || (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
20207 + && req->req.actual % ISO_FIFO_SIZE))
20208 + *ep->reg_udccs = UDCCS_BI_TSP /*|UDCCS_BI_TPC*/;
20210 + /* or force a zlp, with pio ... */
20211 + else if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK
20212 + && req->req.zero) {
20213 + tmp = 0;
20215 + // if iso, maybe report underrun (TUR)
20216 + } else {
20217 + BUG();
20220 + if (likely(tmp != 0))
20221 + done(ep, req, 0);
20223 + /* maybe re-activate after completion */
20224 + if (ep->stopped || list_empty(&ep->queue))
20225 + return;
20226 + req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
20227 + kick_dma(ep, req);
20230 +#endif
20232 +/*-------------------------------------------------------------------------*/
20234 +static int
20235 +pxa2xx_ep_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
20237 + struct pxa2xx_request *req;
20238 + struct pxa2xx_ep *ep;
20239 + struct pxa2xx_udc *dev;
20240 + unsigned long flags;
20242 + req = container_of(_req, struct pxa2xx_request, req);
20243 + if (unlikely (!_req || !_req->complete || !_req->buf
20244 + || !list_empty(&req->queue))) {
20245 + DMSG("%s, bad params\n", __FUNCTION__);
20246 + return -EINVAL;
20249 + ep = container_of(_ep, struct pxa2xx_ep, ep);
20250 + if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
20251 + DMSG("%s, bad ep\n", __FUNCTION__);
20252 + return -EINVAL;
20255 + dev = ep->dev;
20256 + if (unlikely (!dev->driver
20257 + || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
20258 + DMSG("%s, bogus device state\n", __FUNCTION__);
20259 + return -ESHUTDOWN;
20262 + /* iso is always one packet per request, that's the only way
20263 + * we can report per-packet status. that also helps with dma.
20264 + */
20265 + if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
20266 + && req->req.length > le16_to_cpu
20267 + (ep->desc->wMaxPacketSize)))
20268 + return -EMSGSIZE;
20270 +#ifdef USE_DMA
20271 + if (ep->dma >= 0) {
20272 + unsigned long start = (unsigned long) _req->buf;
20274 + clean_dcache_range(start, start + _req->length);
20275 + /* or for USB_DIR_OUT, invalidate_dcache_range (...) */
20277 +#endif
20279 + DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
20280 + _ep->name, _req, _req->length, _req->buf);
20282 + local_irq_save(flags);
20284 + _req->status = -EINPROGRESS;
20285 + _req->actual = 0;
20287 + /* kickstart this i/o queue? */
20288 + if (list_empty(&ep->queue) && !ep->stopped) {
20289 + if (ep->desc == 0 /* ep0 */) {
20290 + unsigned length = _req->length;
20292 + switch (dev->ep0state) {
20293 + case EP0_IN_DATA_PHASE:
20294 + dev->stats.write.ops++;
20295 + if (write_ep0_fifo(ep, req))
20296 + req = 0;
20297 + break;
20299 + case EP0_OUT_DATA_PHASE:
20300 + dev->stats.read.ops++;
20301 + /* messy ... */
20302 + if (dev->req_config) {
20303 + DBG(DBG_VERBOSE, "ep0 config ack%s\n",
20304 + dev->has_cfr ? "" : " raced");
20305 + if (dev->has_cfr)
20306 + UDCCFR = UDCCFR_AREN|UDCCFR_ACM;
20307 + done(ep, req, 0);
20308 + dev->ep0state = EP0_END_XFER;
20309 + local_irq_restore (flags);
20310 + return 0;
20312 + if (dev->req_pending)
20313 + ep0start(dev, UDCCS0_IPR, "OUT");
20314 + if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
20315 + && read_ep0_fifo(ep, req))) {
20316 + ep0_idle(dev);
20317 + done(ep, req, 0);
20318 + req = 0;
20320 + break;
20322 + default:
20323 + DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
20324 + local_irq_restore (flags);
20325 + return -EL2HLT;
20327 +#ifdef USE_DMA
20328 + /* either start dma or prime pio pump */
20329 + } else if (ep->dma >= 0) {
20330 + kick_dma(ep, req);
20331 +#endif
20332 + /* can the FIFO can satisfy the request immediately? */
20333 + } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
20334 + && (*ep->reg_udccs & UDCCS_BI_TFS) != 0
20335 + && write_fifo(ep, req)) {
20336 + req = 0;
20337 + } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
20338 + && read_fifo(ep, req)) {
20339 + req = 0;
20342 + if (likely (req && ep->desc) && ep->dma < 0)
20343 + pio_irq_enable(ep->bEndpointAddress);
20346 + /* pio or dma irq handler advances the queue. */
20347 + if (likely (req != 0))
20348 + list_add_tail(&req->queue, &ep->queue);
20349 + local_irq_restore(flags);
20351 + return 0;
20356 + * nuke - dequeue ALL requests
20357 + */
20358 +static void nuke(struct pxa2xx_ep *ep, int status)
20360 + struct pxa2xx_request *req;
20362 + /* called with irqs blocked */
20363 +#ifdef USE_DMA
20364 + if (ep->dma >= 0 && !ep->stopped)
20365 + cancel_dma(ep);
20366 +#endif
20367 + while (!list_empty(&ep->queue)) {
20368 + req = list_entry(ep->queue.next,
20369 + struct pxa2xx_request,
20370 + queue);
20371 + done(ep, req, status);
20373 + if (ep->desc)
20374 + pio_irq_disable (ep->bEndpointAddress);
20378 +/* dequeue JUST ONE request */
20379 +static int pxa2xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
20381 + struct pxa2xx_ep *ep;
20382 + struct pxa2xx_request *req;
20383 + unsigned long flags;
20385 + ep = container_of(_ep, struct pxa2xx_ep, ep);
20386 + if (!_ep || ep->ep.name == ep0name)
20387 + return -EINVAL;
20389 + local_irq_save(flags);
20391 + /* make sure it's actually queued on this endpoint */
20392 + list_for_each_entry (req, &ep->queue, queue) {
20393 + if (&req->req == _req)
20394 + break;
20396 + if (&req->req != _req) {
20397 + local_irq_restore(flags);
20398 + return -EINVAL;
20401 +#ifdef USE_DMA
20402 + if (ep->dma >= 0 && ep->queue.next == &req->queue && !ep->stopped) {
20403 + cancel_dma(ep);
20404 + done(ep, req, -ECONNRESET);
20405 + /* restart i/o */
20406 + if (!list_empty(&ep->queue)) {
20407 + req = list_entry(ep->queue.next,
20408 + struct pxa2xx_request, queue);
20409 + kick_dma(ep, req);
20411 + } else
20412 +#endif
20413 + done(ep, req, -ECONNRESET);
20415 + local_irq_restore(flags);
20416 + return 0;
20419 +/*-------------------------------------------------------------------------*/
20421 +static int pxa2xx_ep_set_halt(struct usb_ep *_ep, int value)
20423 + struct pxa2xx_ep *ep;
20424 + unsigned long flags;
20426 + ep = container_of(_ep, struct pxa2xx_ep, ep);
20427 + if (unlikely (!_ep
20428 + || (!ep->desc && ep->ep.name != ep0name))
20429 + || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
20430 + DMSG("%s, bad ep\n", __FUNCTION__);
20431 + return -EINVAL;
20433 + if (value == 0) {
20434 + /* this path (reset toggle+halt) is needed to implement
20435 + * SET_INTERFACE on normal hardware. but it can't be
20436 + * done from software on the PXA UDC, and the hardware
20437 + * forgets to do it as part of SET_INTERFACE automagic.
20438 + */
20439 + DMSG("only host can clear %s halt\n", _ep->name);
20440 + return -EROFS;
20443 + local_irq_save(flags);
20445 + if ((ep->bEndpointAddress & USB_DIR_IN) != 0
20446 + && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
20447 + || !list_empty(&ep->queue))) {
20448 + local_irq_restore(flags);
20449 + return -EAGAIN;
20452 + /* FST bit is the same for control, bulk in, bulk out, interrupt in */
20453 + *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
20455 + /* ep0 needs special care */
20456 + if (!ep->desc) {
20457 + start_watchdog(ep->dev);
20458 + ep->dev->req_pending = 0;
20459 + ep->dev->ep0state = EP0_STALL;
20460 + LED_EP0_OFF;
20462 + /* and bulk/intr endpoints like dropping stalls too */
20463 + } else {
20464 + unsigned i;
20465 + for (i = 0; i < 1000; i += 20) {
20466 + if (*ep->reg_udccs & UDCCS_BI_SST)
20467 + break;
20468 + udelay(20);
20471 + local_irq_restore(flags);
20473 + DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
20474 + return 0;
20477 +static int pxa2xx_ep_fifo_status(struct usb_ep *_ep)
20479 + struct pxa2xx_ep *ep;
20481 + ep = container_of(_ep, struct pxa2xx_ep, ep);
20482 + if (!_ep) {
20483 + DMSG("%s, bad ep\n", __FUNCTION__);
20484 + return -ENODEV;
20486 + /* pxa can't report unclaimed bytes from IN fifos */
20487 + if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
20488 + return -EOPNOTSUPP;
20489 + if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
20490 + || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
20491 + return 0;
20492 + else
20493 + return (*ep->reg_ubcr & 0xfff) + 1;
20496 +static void pxa2xx_ep_fifo_flush(struct usb_ep *_ep)
20498 + struct pxa2xx_ep *ep;
20500 + ep = container_of(_ep, struct pxa2xx_ep, ep);
20501 + if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
20502 + DMSG("%s, bad ep\n", __FUNCTION__);
20503 + return;
20506 + /* toggle and halt bits stay unchanged */
20508 + /* for OUT, just read and discard the FIFO contents. */
20509 + if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
20510 + while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
20511 + (void) *ep->reg_uddr;
20512 + return;
20515 + /* most IN status is the same, but ISO can't stall */
20516 + *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
20517 + | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
20518 + ? 0 : UDCCS_BI_SST;
20522 +static struct usb_ep_ops pxa2xx_ep_ops = {
20523 + .enable = pxa2xx_ep_enable,
20524 + .disable = pxa2xx_ep_disable,
20526 + .alloc_request = pxa2xx_ep_alloc_request,
20527 + .free_request = pxa2xx_ep_free_request,
20529 + .alloc_buffer = pxa2xx_ep_alloc_buffer,
20530 + .free_buffer = pxa2xx_ep_free_buffer,
20532 + .queue = pxa2xx_ep_queue,
20533 + .dequeue = pxa2xx_ep_dequeue,
20535 + .set_halt = pxa2xx_ep_set_halt,
20536 + .fifo_status = pxa2xx_ep_fifo_status,
20537 + .fifo_flush = pxa2xx_ep_fifo_flush,
20541 +/* ---------------------------------------------------------------------------
20542 + * device-scoped parts of the api to the usb controller hardware
20543 + * ---------------------------------------------------------------------------
20544 + */
20546 +static int pxa2xx_udc_get_frame(struct usb_gadget *_gadget)
20548 + return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
20551 +static int pxa2xx_udc_wakeup(struct usb_gadget *_gadget)
20553 + /* host may not have enabled remote wakeup */
20554 + if ((UDCCS0 & UDCCS0_DRWF) == 0)
20555 + return -EHOSTUNREACH;
20556 + udc_set_mask_UDCCR(UDCCR_RSM);
20557 + return 0;
20560 +static const struct usb_gadget_ops pxa2xx_udc_ops = {
20561 + .get_frame = pxa2xx_udc_get_frame,
20562 + .wakeup = pxa2xx_udc_wakeup,
20563 + // current versions must always be self-powered
20567 +/*-------------------------------------------------------------------------*/
20569 +#ifdef UDC_PROC_FILE
20571 +static const char proc_node_name [] = "driver/udc";
20573 +static int
20574 +udc_proc_read(char *page, char **start, off_t off, int count,
20575 + int *eof, void *_dev)
20577 + char *buf = page;
20578 + struct pxa2xx_udc *dev = _dev;
20579 + char *next = buf;
20580 + unsigned size = count;
20581 + unsigned long flags;
20582 + int i, t;
20583 + u32 tmp;
20585 + if (off != 0)
20586 + return 0;
20588 + local_irq_save(flags);
20590 + /* basic device status */
20591 + t = snprintf(next, size, DRIVER_DESC "\n"
20592 + "%s version: %s\nGadget driver: %s\nHost %s\n\n",
20593 + driver_name, DRIVER_VERSION SIZE_STR DMASTR,
20594 + dev->driver ? dev->driver->driver.name : "(none)",
20595 + is_usb_connected() ? "full speed" : "disconnected");
20596 + size -= t;
20597 + next += t;
20599 + /* registers for device and ep0 */
20600 + t = snprintf(next, size,
20601 + "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
20602 + UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
20603 + size -= t;
20604 + next += t;
20606 + tmp = UDCCR;
20607 + t = snprintf(next, size,
20608 + "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
20609 + (tmp & UDCCR_REM) ? " rem" : "",
20610 + (tmp & UDCCR_RSTIR) ? " rstir" : "",
20611 + (tmp & UDCCR_SRM) ? " srm" : "",
20612 + (tmp & UDCCR_SUSIR) ? " susir" : "",
20613 + (tmp & UDCCR_RESIR) ? " resir" : "",
20614 + (tmp & UDCCR_RSM) ? " rsm" : "",
20615 + (tmp & UDCCR_UDA) ? " uda" : "",
20616 + (tmp & UDCCR_UDE) ? " ude" : "");
20617 + size -= t;
20618 + next += t;
20620 + tmp = UDCCS0;
20621 + t = snprintf(next, size,
20622 + "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
20623 + (tmp & UDCCS0_SA) ? " sa" : "",
20624 + (tmp & UDCCS0_RNE) ? " rne" : "",
20625 + (tmp & UDCCS0_FST) ? " fst" : "",
20626 + (tmp & UDCCS0_SST) ? " sst" : "",
20627 + (tmp & UDCCS0_DRWF) ? " dwrf" : "",
20628 + (tmp & UDCCS0_FTF) ? " ftf" : "",
20629 + (tmp & UDCCS0_IPR) ? " ipr" : "",
20630 + (tmp & UDCCS0_OPR) ? " opr" : "");
20631 + size -= t;
20632 + next += t;
20634 + if (dev->has_cfr) {
20635 + tmp = UDCCFR;
20636 + t = snprintf(next, size,
20637 + "udccfr %02X =%s%s\n", tmp,
20638 + (tmp & UDCCFR_AREN) ? " aren" : "",
20639 + (tmp & UDCCFR_ACM) ? " acm" : "");
20640 + size -= t;
20641 + next += t;
20644 + if (!is_usb_connected() || !dev->driver)
20645 + goto done;
20647 + t = snprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
20648 + dev->stats.write.bytes, dev->stats.write.ops,
20649 + dev->stats.read.bytes, dev->stats.read.ops,
20650 + dev->stats.irqs);
20651 + size -= t;
20652 + next += t;
20654 + /* dump endpoint queues */
20655 + for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
20656 + struct pxa2xx_ep *ep = &dev->ep [i];
20657 + struct pxa2xx_request *req;
20658 + int t;
20660 + if (i != 0) {
20661 + const struct usb_endpoint_descriptor *d;
20663 + d = ep->desc;
20664 + if (!d)
20665 + continue;
20666 + tmp = *dev->ep [i].reg_udccs;
20667 + t = snprintf(next, size,
20668 + "%s max %d %s udccs %02x irqs %lu/%lu\n",
20669 + ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
20670 + (ep->dma >= 0) ? "dma" : "pio", tmp,
20671 + ep->pio_irqs, ep->dma_irqs);
20672 + /* TODO translate all five groups of udccs bits! */
20674 + } else /* ep0 should only have one transfer queued */
20675 + t = snprintf(next, size, "ep0 max 16 pio irqs %lu\n",
20676 + ep->pio_irqs);
20677 + if (t <= 0 || t > size)
20678 + goto done;
20679 + size -= t;
20680 + next += t;
20682 + if (list_empty(&ep->queue)) {
20683 + t = snprintf(next, size, "\t(nothing queued)\n");
20684 + if (t <= 0 || t > size)
20685 + goto done;
20686 + size -= t;
20687 + next += t;
20688 + continue;
20690 + list_for_each_entry(req, &ep->queue, queue) {
20691 +#ifdef USE_DMA
20692 + if (ep->dma >= 0 && req->queue.prev == &ep->queue)
20693 + t = snprintf(next, size,
20694 + "\treq %p len %d/%d "
20695 + "buf %p (dma%d dcmd %08x)\n",
20696 + &req->req, req->req.actual,
20697 + req->req.length, req->req.buf,
20698 + ep->dma, DCMD(ep->dma)
20699 + // low 13 bits == bytes-to-go
20700 + );
20701 + else
20702 +#endif
20703 + t = snprintf(next, size,
20704 + "\treq %p len %d/%d buf %p\n",
20705 + &req->req, req->req.actual,
20706 + req->req.length, req->req.buf);
20707 + if (t <= 0 || t > size)
20708 + goto done;
20709 + size -= t;
20710 + next += t;
20714 +done:
20715 + local_irq_restore(flags);
20716 + *eof = 1;
20717 + return count - size;
20720 +#define create_proc_files() \
20721 + create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
20722 +#define remove_proc_files() \
20723 + remove_proc_entry(proc_node_name, NULL)
20725 +#else /* !UDC_PROC_FILE */
20726 +#define create_proc_files() do {} while (0)
20727 +#define remove_proc_files() do {} while (0)
20729 +#endif /* UDC_PROC_FILE */
20731 +/*-------------------------------------------------------------------------*/
20734 + * udc_disable - disable USB device controller
20735 + */
20736 +static void udc_disable(struct pxa2xx_udc *dev)
20738 + /* block all irqs */
20739 + udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
20740 + UICR0 = UICR1 = 0xff;
20741 + UFNRH = UFNRH_SIM;
20743 + /* if hardware supports it, disconnect from usb */
20744 + make_usb_disappear();
20746 + udc_clear_mask_UDCCR(UDCCR_UDE);
20748 +#ifdef CONFIG_ARCH_PXA
20749 + /* Disable clock for USB device */
20750 + CKEN &= ~CKEN11_USB;
20751 +#endif
20753 + ep0_idle (dev);
20754 + dev->gadget.speed = USB_SPEED_UNKNOWN;
20755 + LED_CONNECTED_OFF;
20760 + * udc_reinit - initialize software state
20761 + */
20762 +static void udc_reinit(struct pxa2xx_udc *dev)
20764 + u32 i;
20766 + /* device/ep0 records init */
20767 + INIT_LIST_HEAD (&dev->gadget.ep_list);
20768 + INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
20769 + dev->ep0state = EP0_IDLE;
20771 + /* basic endpoint records init */
20772 + for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
20773 + struct pxa2xx_ep *ep = &dev->ep[i];
20775 + if (i != 0)
20776 + list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
20778 + ep->desc = 0;
20779 + ep->stopped = 0;
20780 + INIT_LIST_HEAD (&ep->queue);
20781 + ep->pio_irqs = ep->dma_irqs = 0;
20784 + /* the rest was statically initialized, and is read-only */
20787 +/* until it's enabled, this UDC should be completely invisible
20788 + * to any USB host.
20789 + */
20790 +static void udc_enable (struct pxa2xx_udc *dev)
20792 + udc_clear_mask_UDCCR(UDCCR_UDE);
20794 +#ifdef CONFIG_ARCH_PXA
20795 + /* Enable clock for USB device */
20796 + CKEN |= CKEN11_USB;
20797 +#endif
20799 + /* try to clear these bits before we enable the udc */
20800 + udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
20802 + ep0_idle(dev);
20803 + dev->gadget.speed = USB_SPEED_UNKNOWN;
20804 + dev->stats.irqs = 0;
20806 + /*
20807 + * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
20808 + * - enable UDC
20809 + * - if RESET is already in progress, ack interrupt
20810 + * - unmask reset interrupt
20811 + */
20812 + udc_set_mask_UDCCR(UDCCR_UDE);
20813 + if (!(UDCCR & UDCCR_UDA))
20814 + udc_ack_int_UDCCR(UDCCR_RSTIR);
20816 + if (dev->has_cfr /* UDC_RES2 is defined */) {
20817 + /* pxa255 (a0+) can avoid a set_config race that could
20818 + * prevent gadget drivers from configuring correctly
20819 + */
20820 + UDCCFR = UDCCFR_ACM;
20821 + } else {
20822 + /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
20823 + * which could result in missing packets and interrupts.
20824 + * supposedly one bit per endpoint, controlling whether it
20825 + * double buffers or not; ACM/AREN bits fit into the holes.
20826 + * zero bits (like USIR0_IRx) disable double buffering.
20827 + */
20828 + UDC_RES1 = 0x00;
20829 + UDC_RES2 = 0x00;
20832 +#ifdef DISABLE_TEST_MODE
20833 + /* "test mode" seems to have become the default in later chip
20834 + * revs, preventing double buffering (and invalidating docs).
20835 + * this EXPERIMENT enables it for bulk endpoints by tweaking
20836 + * undefined/reserved register bits (that other drivers clear).
20837 + * Belcarra code comments noted this usage.
20838 + */
20839 + if (fifo_mode & 1) { /* IN endpoints */
20840 + UDC_RES1 |= USIR0_IR1|USIR0_IR6;
20841 + UDC_RES2 |= USIR1_IR11;
20843 + if (fifo_mode & 2) { /* OUT endpoints */
20844 + UDC_RES1 |= USIR0_IR2|USIR0_IR7;
20845 + UDC_RES2 |= USIR1_IR12;
20847 +#endif
20849 + /* caller must be able to sleep in order to cope
20850 + * with startup transients.
20851 + */
20852 + schedule_timeout(HZ/10);
20854 + /* enable suspend/resume and reset irqs */
20855 + udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
20857 + /* enable ep0 irqs */
20858 + UICR0 &= ~UICR0_IM0;
20860 + /* if hardware supports it, connect to usb and wait for host */
20861 + let_usb_appear();
20865 +/* when a driver is successfully registered, it will receive
20866 + * control requests including set_configuration(), which enables
20867 + * non-control requests. then usb traffic follows until a
20868 + * disconnect is reported. then a host may connect again, or
20869 + * the driver might get unbound.
20870 + */
20871 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
20873 + struct pxa2xx_udc *dev = the_controller;
20874 + int retval;
20876 + if (!driver
20877 + || driver->speed != USB_SPEED_FULL
20878 + || !driver->bind
20879 + || !driver->unbind
20880 + || !driver->disconnect
20881 + || !driver->setup)
20882 + return -EINVAL;
20883 + if (!dev)
20884 + return -ENODEV;
20885 + if (dev->driver)
20886 + return -EBUSY;
20888 + /* first hook up the driver ... */
20889 + dev->driver = driver;
20891 + retval = driver->bind(&dev->gadget);
20892 + if (retval) {
20893 + DMSG("bind to driver %s --> error %d\n",
20894 + driver->driver.name, retval);
20895 + dev->driver = 0;
20896 + return retval;
20899 + /* ... then enable host detection and ep0; and we're ready
20900 + * for set_configuration as well as eventual disconnect.
20901 + * NOTE: this shouldn't power up until later.
20902 + */
20903 + DMSG("registered gadget driver '%s'\n", driver->driver.name);
20904 + udc_enable(dev);
20905 + dump_state(dev);
20906 + return 0;
20908 +EXPORT_SYMBOL(usb_gadget_register_driver);
20910 +static void
20911 +stop_activity(struct pxa2xx_udc *dev, struct usb_gadget_driver *driver)
20913 + int i;
20915 + /* don't disconnect drivers more than once */
20916 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
20917 + driver = 0;
20918 + dev->gadget.speed = USB_SPEED_UNKNOWN;
20920 + /* prevent new request submissions, kill any outstanding requests */
20921 + for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
20922 + struct pxa2xx_ep *ep = &dev->ep[i];
20924 + ep->stopped = 1;
20925 + nuke(ep, -ESHUTDOWN);
20927 + del_timer_sync(&dev->timer);
20929 + /* report disconnect; the driver is already quiesced */
20930 + LED_CONNECTED_OFF;
20931 + if (driver)
20932 + driver->disconnect(&dev->gadget);
20934 + /* re-init driver-visible data structures */
20935 + udc_reinit(dev);
20938 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
20940 + struct pxa2xx_udc *dev = the_controller;
20942 + if (!dev)
20943 + return -ENODEV;
20944 + if (!driver || driver != dev->driver)
20945 + return -EINVAL;
20947 + local_irq_disable();
20948 + udc_disable(dev);
20949 + stop_activity(dev, driver);
20950 + local_irq_enable();
20952 + driver->unbind(&dev->gadget);
20953 + dev->driver = 0;
20955 + DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
20956 + dump_state(dev);
20957 + return 0;
20959 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
20962 +/*-------------------------------------------------------------------------*/
20964 +#ifdef CONFIG_ARCH_LUBBOCK
20965 +#ifdef LUBBOCK_USB_DISC_IRQ
20967 +/* Lubbock can report connect or disconnect irqs. Likely more hardware
20968 + * could support it as a timer callback.
20970 + * FIXME for better power management, keep the hardware powered down
20971 + * until a host is powering the link. means scheduling work later
20972 + * in some task that can udc_enable().
20973 + */
20975 +#define enable_disconnect_irq() \
20976 + if (machine_is_lubbock()) { enable_irq(LUBBOCK_USB_DISC_IRQ); }
20977 +#define disable_disconnect_irq() \
20978 + if (machine_is_lubbock()) { disable_irq(LUBBOCK_USB_DISC_IRQ); }
20980 +static irqreturn_t
20981 +usb_connection_irq(int irq, void *_dev, struct pt_regs *r)
20983 + struct pxa2xx_udc *dev = _dev;
20985 + dev->stats.irqs++;
20986 + HEX_DISPLAY(dev->stats.irqs);
20988 + if (!is_usb_connected()) {
20989 + LED_CONNECTED_OFF;
20990 + disable_disconnect_irq();
20991 + /* report disconnect just once */
20992 + if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
20993 + DMSG("disconnect %s\n",
20994 + dev->driver ? dev->driver->driver.name : 0);
20995 + stop_activity(dev, dev->driver);
20997 + // udc_disable (dev);
20998 + // no more udc irqs
20999 + // maybe "ACTION=disconnect /sbin/hotplug gadget".
21001 + } else if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
21002 + LED_CONNECTED_ON;
21004 + DMSG("?? connect irq ??\n");
21006 + // if there's no driver bound, ignore; else
21007 + // udc_enable (dev);
21008 + // UDC irqs drive the rest.
21009 + // maybe "ACTION=connect /sbin/hotplug gadget".
21011 + return IRQ_HANDLED;
21014 +#endif
21015 +#endif
21017 +#ifndef enable_disconnect_irq
21018 +#warning USB disconnect() is not yet reported.
21019 +#define enable_disconnect_irq() do {} while (0)
21020 +#define disable_disconnect_irq() do {} while (0)
21021 +#endif
21024 +/*-------------------------------------------------------------------------*/
21026 +static inline void clear_ep_state (struct pxa2xx_udc *dev)
21028 + unsigned i;
21030 + /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
21031 + * fifos, and pending transactions mustn't be continued in any case.
21032 + */
21033 + for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
21034 + nuke(&dev->ep[i], -ECONNABORTED);
21037 +static void udc_watchdog(unsigned long _dev)
21039 + struct pxa2xx_udc *dev = (void *)_dev;
21041 + local_irq_disable();
21042 + if (dev->ep0state == EP0_STALL
21043 + && (UDCCS0 & UDCCS0_FST) == 0
21044 + && (UDCCS0 & UDCCS0_SST) == 0) {
21045 + UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
21046 + DBG(DBG_VERBOSE, "ep0 re-stall\n");
21047 + start_watchdog(dev);
21049 + local_irq_enable();
21052 +static void handle_ep0 (struct pxa2xx_udc *dev)
21054 + u32 udccs0 = UDCCS0;
21055 + struct pxa2xx_ep *ep = &dev->ep [0];
21056 + struct pxa2xx_request *req;
21057 + union {
21058 + struct usb_ctrlrequest r;
21059 + u8 raw [8];
21060 + u32 word [2];
21061 + } u;
21063 + if (list_empty(&ep->queue))
21064 + req = 0;
21065 + else
21066 + req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
21068 + /* clear stall status */
21069 + if (udccs0 & UDCCS0_SST) {
21070 + nuke(ep, -EPIPE);
21071 + UDCCS0 = UDCCS0_SST;
21072 + del_timer(&dev->timer);
21073 + ep0_idle(dev);
21076 + /* previous request unfinished? non-error iff back-to-back ... */
21077 + if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
21078 + nuke(ep, 0);
21079 + del_timer(&dev->timer);
21080 + ep0_idle(dev);
21083 + switch (dev->ep0state) {
21084 + case EP0_IDLE:
21085 + /* late-breaking status? */
21086 + udccs0 = UDCCS0;
21088 + /* start control request? */
21089 + if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
21090 + == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
21091 + int i;
21093 + nuke (ep, -EPROTO);
21095 + /* read SETUP packet */
21096 + for (i = 0; i < 8; i++) {
21097 + if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
21098 +bad_setup:
21099 + DMSG("SETUP %d!\n", i);
21100 + goto stall;
21102 + u.raw [i] = (u8) UDDR0;
21104 + if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
21105 + goto bad_setup;
21107 +got_setup:
21108 + le16_to_cpus (&u.r.wValue);
21109 + le16_to_cpus (&u.r.wIndex);
21110 + le16_to_cpus (&u.r.wLength);
21112 + LED_EP0_ON;
21113 + DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
21114 + u.r.bRequestType, u.r.bRequest,
21115 + u.r.wValue, u.r.wIndex, u.r.wLength);
21117 + /* cope with automagic for some standard requests. */
21118 + dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
21119 + == USB_TYPE_STANDARD;
21120 + dev->req_config = 0;
21121 + dev->req_pending = 1;
21122 + switch (u.r.bRequest) {
21123 + /* hardware restricts gadget drivers here! */
21124 + case USB_REQ_SET_CONFIGURATION:
21125 + if (u.r.bRequestType == USB_RECIP_DEVICE) {
21126 + /* reflect hardware's automagic
21127 + * up to the gadget driver.
21128 + */
21129 +config_change:
21130 + dev->req_config = 1;
21131 + clear_ep_state(dev);
21132 + /* if !has_cfr, there's no synch
21133 + * else use AREN (later) not SA|OPR
21134 + * USIR0_IR0 acts edge sensitive
21135 + */
21137 + break;
21138 + /* ... and here, even more ... */
21139 + case USB_REQ_SET_INTERFACE:
21140 + if (u.r.bRequestType == USB_RECIP_INTERFACE) {
21141 + /* udc hardware is broken by design:
21142 + * - altsetting may only be zero;
21143 + * - hw resets all interfaces' eps;
21144 + * - ep reset doesn't include halt(?).
21145 + */
21146 + DMSG("broken set_interface (%d/%d)\n",
21147 + u.r.wIndex, u.r.wValue);
21148 + goto config_change;
21150 + break;
21151 + /* hardware was supposed to hide this */
21152 + case USB_REQ_SET_ADDRESS:
21153 + if (u.r.bRequestType == USB_RECIP_DEVICE) {
21154 + ep0start(dev, 0, "address");
21155 + return;
21157 + break;
21160 + if (u.r.bRequestType & USB_DIR_IN)
21161 + dev->ep0state = EP0_IN_DATA_PHASE;
21162 + else
21163 + dev->ep0state = EP0_OUT_DATA_PHASE;
21165 + i = dev->driver->setup(&dev->gadget, &u.r);
21166 + if (i < 0) {
21167 + /* hardware automagic preventing STALL... */
21168 + if (dev->req_config) {
21169 + /* hardware sometimes neglects to tell
21170 + * tell us about config change events,
21171 + * so later ones may fail...
21172 + */
21173 + WARN("config change %02x fail %d?\n",
21174 + u.r.bRequest, i);
21175 + return;
21176 + /* TODO experiment: if has_cfr,
21177 + * hardware didn't ACK; maybe we
21178 + * could actually STALL!
21179 + */
21181 + DBG(DBG_VERBOSE, "protocol STALL, "
21182 + "%02x err %d\n", UDCCS0, i);
21183 +stall:
21184 + /* the watchdog timer helps deal with cases
21185 + * where udc seems to clear FST wrongly, and
21186 + * then NAKs instead of STALLing.
21187 + */
21188 + ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
21189 + start_watchdog(dev);
21190 + dev->ep0state = EP0_STALL;
21191 + LED_EP0_OFF;
21193 + /* deferred i/o == no response yet */
21194 + } else if (dev->req_pending) {
21195 + if (likely(dev->ep0state == EP0_IN_DATA_PHASE
21196 + || dev->req_std || u.r.wLength))
21197 + ep0start(dev, 0, "defer");
21198 + else
21199 + ep0start(dev, UDCCS0_IPR, "defer/IPR");
21202 + /* expect at least one data or status stage irq */
21203 + return;
21205 + } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
21206 + == (UDCCS0_OPR|UDCCS0_SA))) {
21207 + unsigned i;
21209 + /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
21210 + * still observed on a pxa255 a0.
21211 + */
21212 + DBG(DBG_VERBOSE, "e131\n");
21213 + nuke(ep, -EPROTO);
21215 + /* read SETUP data, but don't trust it too much */
21216 + for (i = 0; i < 8; i++)
21217 + u.raw [i] = (u8) UDDR0;
21218 + if ((u.r.bRequestType & USB_RECIP_MASK)
21219 + > USB_RECIP_OTHER)
21220 + goto stall;
21221 + if (u.word [0] == 0 && u.word [1] == 0)
21222 + goto stall;
21223 + goto got_setup;
21224 + } else {
21225 + /* some random early IRQ:
21226 + * - we acked FST
21227 + * - IPR cleared
21228 + * - OPR got set, without SA (likely status stage)
21229 + */
21230 + UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
21232 + break;
21233 + case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
21234 + if (udccs0 & UDCCS0_OPR) {
21235 + UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
21236 + DBG(DBG_VERBOSE, "ep0in premature status\n");
21237 + if (req)
21238 + done(ep, req, 0);
21239 + ep0_idle(dev);
21240 + } else /* irq was IPR clearing */ {
21241 + if (req) {
21242 + /* this IN packet might finish the request */
21243 + (void) write_ep0_fifo(ep, req);
21244 + } /* else IN token before response was written */
21246 + break;
21247 + case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
21248 + if (udccs0 & UDCCS0_OPR) {
21249 + if (req) {
21250 + /* this OUT packet might finish the request */
21251 + if (read_ep0_fifo(ep, req))
21252 + done(ep, req, 0);
21253 + /* else more OUT packets expected */
21254 + } /* else OUT token before read was issued */
21255 + } else /* irq was IPR clearing */ {
21256 + DBG(DBG_VERBOSE, "ep0out premature status\n");
21257 + if (req)
21258 + done(ep, req, 0);
21259 + ep0_idle(dev);
21261 + break;
21262 + case EP0_END_XFER:
21263 + if (req)
21264 + done(ep, req, 0);
21265 + /* ack control-IN status (maybe in-zlp was skipped)
21266 + * also appears after some config change events.
21267 + */
21268 + if (udccs0 & UDCCS0_OPR)
21269 + UDCCS0 = UDCCS0_OPR;
21270 + ep0_idle(dev);
21271 + break;
21272 + case EP0_STALL:
21273 + UDCCS0 = UDCCS0_FST;
21274 + break;
21276 + USIR0 = USIR0_IR0;
21279 +static void handle_ep(struct pxa2xx_ep *ep)
21281 + struct pxa2xx_request *req;
21282 + int is_in = ep->bEndpointAddress & USB_DIR_IN;
21283 + int completed;
21284 + u32 udccs, tmp;
21286 + do {
21287 + completed = 0;
21288 + if (likely (!list_empty(&ep->queue)))
21289 + req = list_entry(ep->queue.next,
21290 + struct pxa2xx_request, queue);
21291 + else
21292 + req = 0;
21294 + // TODO check FST handling
21296 + udccs = *ep->reg_udccs;
21297 + if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
21298 + tmp = UDCCS_BI_TUR;
21299 + if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
21300 + tmp |= UDCCS_BI_SST;
21301 + tmp &= udccs;
21302 + if (likely (tmp))
21303 + *ep->reg_udccs = tmp;
21304 + if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
21305 + completed = write_fifo(ep, req);
21307 + } else { /* irq from RPC (or for ISO, ROF) */
21308 + if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
21309 + tmp = UDCCS_BO_SST | UDCCS_BO_DME;
21310 + else
21311 + tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
21312 + tmp &= udccs;
21313 + if (likely(tmp))
21314 + *ep->reg_udccs = tmp;
21316 + /* fifos can hold packets, ready for reading... */
21317 + if (likely(req != 0)) {
21318 +#ifdef USE_OUT_DMA
21319 +// TODO didn't yet debug out-dma. this approach assumes
21320 +// the worst about short packets and RPC; it might be better.
21322 + if (likely(ep->dma >= 0)) {
21323 + if (!(udccs & UDCCS_BO_RSP)) {
21324 + *ep->reg_udccs = UDCCS_BO_RPC;
21325 + ep->dma_irqs++;
21326 + return;
21329 +#endif
21330 + completed = read_fifo(ep, req);
21331 + } else
21332 + pio_irq_disable (ep->bEndpointAddress);
21334 + ep->pio_irqs++;
21335 + } while (completed);
21339 + * pxa2xx_udc_irq - interrupt handler
21341 + * avoid delays in ep0 processing. the control handshaking isn't always
21342 + * under software control (pxa250c0 and the pxa255 are better), and delays
21343 + * could cause usb protocol errors.
21344 + */
21345 +static irqreturn_t
21346 +pxa2xx_udc_irq(int irq, void *_dev, struct pt_regs *r)
21348 + struct pxa2xx_udc *dev = _dev;
21349 + int handled;
21351 + dev->stats.irqs++;
21352 + HEX_DISPLAY(dev->stats.irqs);
21353 + do {
21354 + u32 udccr = UDCCR;
21356 + handled = 0;
21358 + /* SUSpend Interrupt Request */
21359 + if (unlikely(udccr & UDCCR_SUSIR)) {
21360 + udc_ack_int_UDCCR(UDCCR_SUSIR);
21361 + handled = 1;
21362 + DBG(DBG_VERBOSE, "USB suspend%s\n", is_usb_connected()
21363 + ? "" : "+disconnect");
21365 + if (!is_usb_connected())
21366 + stop_activity(dev, dev->driver);
21367 + else if (dev->gadget.speed != USB_SPEED_UNKNOWN
21368 + && dev->driver
21369 + && dev->driver->suspend)
21370 + dev->driver->suspend(&dev->gadget);
21371 + ep0_idle (dev);
21374 + /* RESume Interrupt Request */
21375 + if (unlikely(udccr & UDCCR_RESIR)) {
21376 + udc_ack_int_UDCCR(UDCCR_RESIR);
21377 + handled = 1;
21378 + DBG(DBG_VERBOSE, "USB resume\n");
21380 + if (dev->gadget.speed != USB_SPEED_UNKNOWN
21381 + && dev->driver
21382 + && dev->driver->resume
21383 + && is_usb_connected())
21384 + dev->driver->resume(&dev->gadget);
21387 + /* ReSeT Interrupt Request - USB reset */
21388 + if (unlikely(udccr & UDCCR_RSTIR)) {
21389 + udc_ack_int_UDCCR(UDCCR_RSTIR);
21390 + handled = 1;
21392 + if ((UDCCR & UDCCR_UDA) == 0) {
21393 + DBG(DBG_VERBOSE, "USB reset start\n");
21394 + if (dev->gadget.speed != USB_SPEED_UNKNOWN)
21395 + disable_disconnect_irq();
21397 + /* reset driver and endpoints,
21398 + * in case that's not yet done
21399 + */
21400 + stop_activity (dev, dev->driver);
21402 + } else {
21403 + INFO("USB reset\n");
21404 + dev->gadget.speed = USB_SPEED_FULL;
21405 + LED_CONNECTED_ON;
21406 + memset(&dev->stats, 0, sizeof dev->stats);
21407 + /* driver and endpoints are still reset */
21408 + enable_disconnect_irq();
21411 + } else {
21412 + u32 usir0 = USIR0 & ~UICR0;
21413 + u32 usir1 = USIR1 & ~UICR1;
21414 + int i;
21416 + if (unlikely (!usir0 && !usir1))
21417 + continue;
21419 + DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
21421 + /* control traffic */
21422 + if (usir0 & USIR0_IR0) {
21423 + dev->ep[0].pio_irqs++;
21424 + handle_ep0(dev);
21425 + handled = 1;
21428 + /* endpoint data transfers */
21429 + for (i = 0; i < 8; i++) {
21430 + u32 tmp = 1 << i;
21432 + if (i && (usir0 & tmp)) {
21433 + handle_ep(&dev->ep[i]);
21434 + USIR0 |= tmp;
21435 + handled = 1;
21437 + if (usir1 & tmp) {
21438 + handle_ep(&dev->ep[i+8]);
21439 + USIR1 |= tmp;
21440 + handled = 1;
21445 + /* we could also ask for 1 msec SOF (SIR) interrupts */
21447 + } while (handled);
21448 + return IRQ_HANDLED;
21451 +/*-------------------------------------------------------------------------*/
21454 + * cleanup - free resources allocated during init
21455 + */
21456 +static void /*__exit and */ __init cleanup(void)
21458 + struct pxa2xx_udc *dev = the_controller;
21460 + if (!dev)
21461 + return;
21463 + udc_disable(dev);
21464 + remove_proc_files();
21465 + usb_gadget_unregister_driver(dev->driver);
21466 + if (dev->got_irq) {
21467 + free_irq(IRQ_USB, dev);
21468 + dev->got_irq = 0;
21470 +#ifdef LUBBOCK_USB_DISC_IRQ
21471 + if (dev->got_disc) {
21472 + free_irq(LUBBOCK_USB_DISC_IRQ, dev);
21473 + dev->got_disc = 0;
21475 +#endif
21476 + the_controller = 0;
21477 + release_mem_region(REGISTER_FIRST, REGISTER_LENGTH);
21479 +module_exit (cleanup);
21481 +/* this uses load-time allocation and initialization (instead of
21482 + * doing it at run-time) to save code, eliminate fault paths, and
21483 + * be more obviously correct.
21484 + */
21485 +static struct pxa2xx_udc memory = {
21486 + .gadget = {
21487 + .ops = &pxa2xx_udc_ops,
21488 + .ep0 = &memory.ep[0].ep,
21489 + .name = driver_name,
21490 + .dev = {
21491 + .bus_id = "gadget",
21492 + },
21493 + },
21495 + /* control endpoint */
21496 + .ep[0] = {
21497 + .ep = {
21498 + .name = ep0name,
21499 + .ops = &pxa2xx_ep_ops,
21500 + .maxpacket = EP0_FIFO_SIZE,
21501 + },
21502 + .dev = &memory,
21503 + .reg_udccs = &UDCCS0,
21504 + .reg_uddr = &UDDR0,
21505 + },
21507 + /* first group of endpoints */
21508 + .ep[1] = {
21509 + .ep = {
21510 + .name = "ep1in-bulk",
21511 + .ops = &pxa2xx_ep_ops,
21512 + .maxpacket = BULK_FIFO_SIZE,
21513 + },
21514 + .dev = &memory,
21515 + .fifo_size = BULK_FIFO_SIZE,
21516 + .bEndpointAddress = USB_DIR_IN | 1,
21517 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
21518 + .reg_udccs = &UDCCS1,
21519 + .reg_uddr = &UDDR1,
21520 + drcmr (25)
21521 + },
21522 + .ep[2] = {
21523 + .ep = {
21524 + .name = "ep2out-bulk",
21525 + .ops = &pxa2xx_ep_ops,
21526 + .maxpacket = BULK_FIFO_SIZE,
21527 + },
21528 + .dev = &memory,
21529 + .fifo_size = BULK_FIFO_SIZE,
21530 + .bEndpointAddress = 2,
21531 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
21532 + .reg_udccs = &UDCCS2,
21533 + .reg_ubcr = &UBCR2,
21534 + .reg_uddr = &UDDR2,
21535 + drcmr (26)
21536 + },
21537 +#ifndef CONFIG_USB_PXA2XX_SMALL
21538 + .ep[3] = {
21539 + .ep = {
21540 + .name = "ep3in-iso",
21541 + .ops = &pxa2xx_ep_ops,
21542 + .maxpacket = ISO_FIFO_SIZE,
21543 + },
21544 + .dev = &memory,
21545 + .fifo_size = ISO_FIFO_SIZE,
21546 + .bEndpointAddress = USB_DIR_IN | 3,
21547 + .bmAttributes = USB_ENDPOINT_XFER_ISOC,
21548 + .reg_udccs = &UDCCS3,
21549 + .reg_uddr = &UDDR3,
21550 + drcmr (27)
21551 + },
21552 + .ep[4] = {
21553 + .ep = {
21554 + .name = "ep4out-iso",
21555 + .ops = &pxa2xx_ep_ops,
21556 + .maxpacket = ISO_FIFO_SIZE,
21557 + },
21558 + .dev = &memory,
21559 + .fifo_size = ISO_FIFO_SIZE,
21560 + .bEndpointAddress = 4,
21561 + .bmAttributes = USB_ENDPOINT_XFER_ISOC,
21562 + .reg_udccs = &UDCCS4,
21563 + .reg_ubcr = &UBCR4,
21564 + .reg_uddr = &UDDR4,
21565 + drcmr (28)
21566 + },
21567 + .ep[5] = {
21568 + .ep = {
21569 + .name = "ep5in-int",
21570 + .ops = &pxa2xx_ep_ops,
21571 + .maxpacket = INT_FIFO_SIZE,
21572 + },
21573 + .dev = &memory,
21574 + .fifo_size = INT_FIFO_SIZE,
21575 + .bEndpointAddress = USB_DIR_IN | 5,
21576 + .bmAttributes = USB_ENDPOINT_XFER_INT,
21577 + .reg_udccs = &UDCCS5,
21578 + .reg_uddr = &UDDR5,
21579 + },
21581 + /* second group of endpoints */
21582 + .ep[6] = {
21583 + .ep = {
21584 + .name = "ep6in-bulk",
21585 + .ops = &pxa2xx_ep_ops,
21586 + .maxpacket = BULK_FIFO_SIZE,
21587 + },
21588 + .dev = &memory,
21589 + .fifo_size = BULK_FIFO_SIZE,
21590 + .bEndpointAddress = USB_DIR_IN | 6,
21591 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
21592 + .reg_udccs = &UDCCS6,
21593 + .reg_uddr = &UDDR6,
21594 + drcmr (30)
21595 + },
21596 + .ep[7] = {
21597 + .ep = {
21598 + .name = "ep7out-bulk",
21599 + .ops = &pxa2xx_ep_ops,
21600 + .maxpacket = BULK_FIFO_SIZE,
21601 + },
21602 + .dev = &memory,
21603 + .fifo_size = BULK_FIFO_SIZE,
21604 + .bEndpointAddress = 7,
21605 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
21606 + .reg_udccs = &UDCCS7,
21607 + .reg_ubcr = &UBCR7,
21608 + .reg_uddr = &UDDR7,
21609 + drcmr (31)
21610 + },
21611 + .ep[8] = {
21612 + .ep = {
21613 + .name = "ep8in-iso",
21614 + .ops = &pxa2xx_ep_ops,
21615 + .maxpacket = ISO_FIFO_SIZE,
21616 + },
21617 + .dev = &memory,
21618 + .fifo_size = ISO_FIFO_SIZE,
21619 + .bEndpointAddress = USB_DIR_IN | 8,
21620 + .bmAttributes = USB_ENDPOINT_XFER_ISOC,
21621 + .reg_udccs = &UDCCS8,
21622 + .reg_uddr = &UDDR8,
21623 + drcmr (32)
21624 + },
21625 + .ep[9] = {
21626 + .ep = {
21627 + .name = "ep9out-iso",
21628 + .ops = &pxa2xx_ep_ops,
21629 + .maxpacket = ISO_FIFO_SIZE,
21630 + },
21631 + .dev = &memory,
21632 + .fifo_size = ISO_FIFO_SIZE,
21633 + .bEndpointAddress = 9,
21634 + .bmAttributes = USB_ENDPOINT_XFER_ISOC,
21635 + .reg_udccs = &UDCCS9,
21636 + .reg_ubcr = &UBCR9,
21637 + .reg_uddr = &UDDR9,
21638 + drcmr (33)
21639 + },
21640 + .ep[10] = {
21641 + .ep = {
21642 + .name = "ep10in-int",
21643 + .ops = &pxa2xx_ep_ops,
21644 + .maxpacket = INT_FIFO_SIZE,
21645 + },
21646 + .dev = &memory,
21647 + .fifo_size = INT_FIFO_SIZE,
21648 + .bEndpointAddress = USB_DIR_IN | 10,
21649 + .bmAttributes = USB_ENDPOINT_XFER_INT,
21650 + .reg_udccs = &UDCCS10,
21651 + .reg_uddr = &UDDR10,
21652 + },
21654 + /* third group of endpoints */
21655 + .ep[11] = {
21656 + .ep = {
21657 + .name = "ep11in-bulk",
21658 + .ops = &pxa2xx_ep_ops,
21659 + .maxpacket = BULK_FIFO_SIZE,
21660 + },
21661 + .dev = &memory,
21662 + .fifo_size = BULK_FIFO_SIZE,
21663 + .bEndpointAddress = USB_DIR_IN | 11,
21664 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
21665 + .reg_udccs = &UDCCS11,
21666 + .reg_uddr = &UDDR11,
21667 + drcmr (35)
21668 + },
21669 + .ep[12] = {
21670 + .ep = {
21671 + .name = "ep12out-bulk",
21672 + .ops = &pxa2xx_ep_ops,
21673 + .maxpacket = BULK_FIFO_SIZE,
21674 + },
21675 + .dev = &memory,
21676 + .fifo_size = BULK_FIFO_SIZE,
21677 + .bEndpointAddress = 12,
21678 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
21679 + .reg_udccs = &UDCCS12,
21680 + .reg_ubcr = &UBCR12,
21681 + .reg_uddr = &UDDR12,
21682 + drcmr (36)
21683 + },
21684 + .ep[13] = {
21685 + .ep = {
21686 + .name = "ep13in-iso",
21687 + .ops = &pxa2xx_ep_ops,
21688 + .maxpacket = ISO_FIFO_SIZE,
21689 + },
21690 + .dev = &memory,
21691 + .fifo_size = ISO_FIFO_SIZE,
21692 + .bEndpointAddress = USB_DIR_IN | 13,
21693 + .bmAttributes = USB_ENDPOINT_XFER_ISOC,
21694 + .reg_udccs = &UDCCS13,
21695 + .reg_uddr = &UDDR13,
21696 + drcmr (37)
21697 + },
21698 + .ep[14] = {
21699 + .ep = {
21700 + .name = "ep14out-iso",
21701 + .ops = &pxa2xx_ep_ops,
21702 + .maxpacket = ISO_FIFO_SIZE,
21703 + },
21704 + .dev = &memory,
21705 + .fifo_size = ISO_FIFO_SIZE,
21706 + .bEndpointAddress = 14,
21707 + .bmAttributes = USB_ENDPOINT_XFER_ISOC,
21708 + .reg_udccs = &UDCCS14,
21709 + .reg_ubcr = &UBCR14,
21710 + .reg_uddr = &UDDR14,
21711 + drcmr (38)
21712 + },
21713 + .ep[15] = {
21714 + .ep = {
21715 + .name = "ep15in-int",
21716 + .ops = &pxa2xx_ep_ops,
21717 + .maxpacket = INT_FIFO_SIZE,
21718 + },
21719 + .dev = &memory,
21720 + .fifo_size = INT_FIFO_SIZE,
21721 + .bEndpointAddress = USB_DIR_IN | 15,
21722 + .bmAttributes = USB_ENDPOINT_XFER_INT,
21723 + .reg_udccs = &UDCCS15,
21724 + .reg_uddr = &UDDR15,
21725 + },
21726 +#endif /* !CONFIG_USB_PXA2XX_SMALL */
21729 +#define CP15R0_VENDOR_MASK 0xffffe000
21731 +#if defined(CONFIG_ARCH_PXA)
21732 +#define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
21734 +#elif defined(CONFIG_ARCH_IXP425)
21735 +#define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp425 */
21737 +#endif
21739 +#define CP15R0_PROD_MASK 0x000003f0
21740 +#define PXA25x 0x00000100 /* and PXA26x */
21741 +#define PXA210 0x00000120
21743 +#define CP15R0_REV_MASK 0x0000000f
21745 +#define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
21747 +#define PXA255_A0 0x00000106 /* or PXA260_B1 */
21748 +#define PXA250_C0 0x00000105 /* or PXA26x_B0 */
21749 +#define PXA250_B2 0x00000104
21750 +#define PXA250_B1 0x00000103 /* or PXA260_A0 */
21751 +#define PXA250_B0 0x00000102
21752 +#define PXA250_A1 0x00000101
21753 +#define PXA250_A0 0x00000100
21755 +#define PXA210_C0 0x00000125
21756 +#define PXA210_B2 0x00000124
21757 +#define PXA210_B1 0x00000123
21758 +#define PXA210_B0 0x00000122
21760 +#define IXP425_A0 0x000001c1
21763 + * init - allocate resources
21764 + */
21765 +static int __init init(void)
21767 + struct pxa2xx_udc *dev;
21768 + int retval, out_dma = 1;
21769 + u32 chiprev;
21771 + printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
21773 + /* insist on Intel/ARM/XScale */
21774 + asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
21775 + if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
21776 + printk(KERN_ERR "%s: not XScale!\n", driver_name);
21777 + return -ENODEV;
21780 + /* allocate resources */
21781 + if (!request_mem_region(REGISTER_FIRST, REGISTER_LENGTH, driver_name))
21782 + return -EBUSY;
21784 + /* initialize data */
21785 + dev = &memory;
21787 + init_timer(&dev->timer);
21788 + dev->timer.function = udc_watchdog;
21789 + dev->timer.data = (unsigned long) dev;
21791 + /* trigger chiprev-specific logic */
21792 + switch (chiprev & CP15R0_PRODREV_MASK) {
21793 +#if defined(CONFIG_ARCH_PXA)
21794 + case PXA255_A0:
21795 + dev->has_cfr = 1;
21796 + break;
21797 + case PXA250_A0:
21798 + case PXA250_A1:
21799 + /* A0/A1 "not released"; ep 13, 15 unusable */
21800 + /* fall through */
21801 + case PXA250_B2: case PXA210_B2:
21802 + case PXA250_B1: case PXA210_B1:
21803 + case PXA250_B0: case PXA210_B0:
21804 + out_dma = 0;
21805 + /* fall through */
21806 + case PXA250_C0: case PXA210_C0:
21807 + break;
21808 +#elif defined(CONFIG_ARCH_IXP425)
21809 + case IXP425_A0:
21810 + out_dma = 0;
21811 + break;
21812 +#endif
21813 + default:
21814 + out_dma = 0;
21815 + printk(KERN_ERR "%s: unrecognized processor: %08x\n",
21816 + driver_name, chiprev);
21817 + return -ENODEV;
21820 + pr_debug("%s: IRQ %d%s%s%s\n", driver_name, IRQ_USB,
21821 + dev->has_cfr ? "" : " (!cfr)",
21822 + out_dma ? "" : " (broken dma-out)",
21823 + SIZE_STR DMASTR
21824 + );
21826 +#ifdef USE_DMA
21827 +#ifndef USE_OUT_DMA
21828 + out_dma = 0;
21829 +#endif
21830 + /* pxa 250 erratum 130 prevents using OUT dma (fixed C0) */
21831 + if (!out_dma) {
21832 + DMSG("disabled OUT dma\n");
21833 + dev->ep[ 2].reg_drcmr = dev->ep[ 4].reg_drcmr = 0;
21834 + dev->ep[ 7].reg_drcmr = dev->ep[ 9].reg_drcmr = 0;
21835 + dev->ep[12].reg_drcmr = dev->ep[14].reg_drcmr = 0;
21837 +#endif
21839 + the_controller = dev;
21840 + udc_disable(dev);
21841 + udc_reinit(dev);
21843 + /* irq setup after old hardware state is cleaned up */
21844 + retval = request_irq(IRQ_USB, pxa2xx_udc_irq,
21845 + SA_INTERRUPT, driver_name, dev);
21846 + if (retval != 0) {
21847 + printk(KERN_ERR "%s: can't get irq %i, err %d\n",
21848 + driver_name, IRQ_USB, retval);
21849 + return -EBUSY;
21851 + dev->got_irq = 1;
21853 +#ifdef LUBBOCK_USB_DISC_IRQ
21854 + if (machine_is_lubbock()) {
21855 + disable_irq(LUBBOCK_USB_DISC_IRQ);
21856 + retval = request_irq(LUBBOCK_USB_DISC_IRQ,
21857 + usb_connection_irq,
21858 + SA_INTERRUPT | SA_SAMPLE_RANDOM,
21859 + driver_name, dev);
21860 + if (retval != 0) {
21861 + enable_irq(LUBBOCK_USB_DISC_IRQ);
21862 + printk(KERN_ERR "%s: can't get irq %i, err %d\n",
21863 + driver_name, LUBBOCK_USB_DISC_IRQ, retval);
21864 + cleanup();
21865 + return retval;
21867 + dev->got_disc = 1;
21869 +#endif
21871 + create_proc_files();
21872 + return 0;
21874 +module_init (init);
21876 +MODULE_DESCRIPTION(DRIVER_DESC);
21877 +MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
21878 +MODULE_LICENSE("GPL");
21880 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/pxa2xx_udc.h kernel/drivers/usb/gadget/pxa2xx_udc.h
21881 --- /tmp/kernel/drivers/usb/gadget/pxa2xx_udc.h 1970-01-01 01:00:00.000000000 +0100
21882 +++ kernel/drivers/usb/gadget/pxa2xx_udc.h 2005-04-22 17:53:19.496529422 +0200
21883 @@ -0,0 +1,528 @@
21885 + * linux/drivers/usb/gadget/pxa2xx_udc.h
21886 + * Intel PXA2xx on-chip full speed USB device controller
21887 + *
21888 + * Copyright (C) 2003 Robert Schwebel <r.schwebel@pengutronix.de>, Pengutronix
21889 + * Copyright (C) 2003 David Brownell
21890 + * Copyright (C) 2003 Joshua Wise
21893 + * This program is free software; you can redistribute it and/or modify
21894 + * it under the terms of the GNU General Public License as published by
21895 + * the Free Software Foundation; either version 2 of the License, or
21896 + * (at your option) any later version.
21898 + * This program is distributed in the hope that it will be useful,
21899 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21900 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21901 + * GNU General Public License for more details.
21903 + * You should have received a copy of the GNU General Public License
21904 + * along with this program; if not, write to the Free Software
21905 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21906 + */
21908 +#ifndef __LINUX_USB_GADGET_PXA2XX_H
21909 +#define __LINUX_USB_GADGET_PXA2XX_H
21911 +#include <linux/types.h>
21913 +/*-------------------------------------------------------------------------*/
21915 +/* pxa2xx has this (move to include/asm-arm/arch-pxa/pxa-regs.h) */
21916 +#define UFNRH_SIR (1 << 7) /* SOF interrupt request */
21917 +#define UFNRH_SIM (1 << 6) /* SOF interrupt mask */
21918 +#define UFNRH_IPE14 (1 << 5) /* ISO packet error, ep14 */
21919 +#define UFNRH_IPE9 (1 << 4) /* ISO packet error, ep9 */
21920 +#define UFNRH_IPE4 (1 << 3) /* ISO packet error, ep4 */
21922 +/* pxa255 has this (move to include/asm-arm/arch-pxa/pxa-regs.h) */
21923 +#define UDCCFR UDC_RES2 /* UDC Control Function Register */
21924 +#define UDCCFR_AREN (1 << 7) /* ACK response enable (now) */
21925 +#define UDCCFR_ACM (1 << 2) /* ACK control mode (wait for AREN) */
21927 +/* for address space reservation */
21928 +#define REGISTER_FIRST ((unsigned long)(&UDCCR))
21929 +#define REGISTER_LAST ((unsigned long)(&UDDR14)) /* not UDDR15! */
21930 +#define REGISTER_LENGTH ((REGISTER_LAST - REGISTER_FIRST) + 4)
21932 +/*-------------------------------------------------------------------------*/
21934 +struct pxa2xx_udc;
21936 +struct pxa2xx_ep {
21937 + struct usb_ep ep;
21938 + struct pxa2xx_udc *dev;
21940 + const struct usb_endpoint_descriptor *desc;
21941 + struct list_head queue;
21942 + unsigned long pio_irqs;
21943 + unsigned long dma_irqs;
21944 + short dma;
21946 + unsigned short fifo_size;
21947 + u8 bEndpointAddress;
21948 + u8 bmAttributes;
21950 + unsigned stopped : 1;
21951 + unsigned dma_fixup : 1;
21953 + /* UDCCS = UDC Control/Status for this EP
21954 + * UBCR = UDC Byte Count Remaining (contents of OUT fifo)
21955 + * UDDR = UDC Endpoint Data Register (the fifo)
21956 + * DRCM = DMA Request Channel Map
21957 + */
21958 + volatile u32 *reg_udccs;
21959 + volatile u32 *reg_ubcr;
21960 + volatile u32 *reg_uddr;
21961 +#ifdef USE_DMA
21962 + volatile u32 *reg_drcmr;
21963 +#define drcmr(n) .reg_drcmr = & DRCMR ## n ,
21964 +#else
21965 +#define drcmr(n)
21966 +#endif
21969 +struct pxa2xx_request {
21970 + struct usb_request req;
21971 + struct list_head queue;
21974 +enum ep0_state {
21975 + EP0_IDLE,
21976 + EP0_IN_DATA_PHASE,
21977 + EP0_OUT_DATA_PHASE,
21978 + EP0_END_XFER,
21979 + EP0_STALL,
21982 +#define EP0_FIFO_SIZE ((unsigned)16)
21983 +#define BULK_FIFO_SIZE ((unsigned)64)
21984 +#define ISO_FIFO_SIZE ((unsigned)256)
21985 +#define INT_FIFO_SIZE ((unsigned)8)
21987 +struct udc_stats {
21988 + struct ep0stats {
21989 + unsigned long ops;
21990 + unsigned long bytes;
21991 + } read, write;
21992 + unsigned long irqs;
21995 +#ifdef CONFIG_USB_PXA2XX_SMALL
21996 +/* when memory's tight, SMALL config saves code+data. */
21997 +#undef USE_DMA
21998 +#define PXA_UDC_NUM_ENDPOINTS 3
21999 +#endif
22001 +#ifndef PXA_UDC_NUM_ENDPOINTS
22002 +#define PXA_UDC_NUM_ENDPOINTS 16
22003 +#endif
22005 +struct pxa2xx_udc {
22006 + struct usb_gadget gadget;
22007 + struct usb_gadget_driver *driver;
22009 + enum ep0_state ep0state;
22010 + struct udc_stats stats;
22011 + unsigned got_irq : 1,
22012 + got_disc : 1,
22013 + has_cfr : 1,
22014 + req_pending : 1,
22015 + req_std : 1,
22016 + req_config : 1;
22018 +#define start_watchdog(dev) mod_timer(&dev->timer, jiffies + (HZ/200))
22019 + struct timer_list timer;
22021 + struct pxa2xx_ep ep [PXA_UDC_NUM_ENDPOINTS];
22024 +/* 2.5 changes ... */
22026 +#ifndef container_of
22027 +#define container_of list_entry
22028 +#endif
22030 +#ifndef WARN_ON
22031 +#define WARN_ON BUG_ON
22032 +#endif
22034 +/*-------------------------------------------------------------------------*/
22036 +/* please keep machine-specific defines in alphabetical order. */
22038 +// CONFIG_ARCH_ADI_COYOTE behaves
22040 +#ifdef CONFIG_ARCH_E7XX
22041 +# include <asm/arch/e7xx-gpio.h>
22042 +#endif
22044 +#ifdef CONFIG_ARCH_H1900
22045 +# include <asm/arch/h1900-gpio.h>
22046 +#endif
22048 +#ifdef CONFIG_ARCH_H3900
22049 +# include <asm/arch/h3900-gpio.h>
22050 +#endif
22052 +#ifdef CONFIG_ARCH_H5400
22053 +# include <asm/arch/h5400-gpio.h>
22054 +#endif
22056 +#ifdef CONFIG_ARCH_INNOKOM
22057 +#include <asm/arch/innokom.h>
22058 +#endif
22060 +#ifdef CONFIG_ARCH_LUBBOCK
22061 +#include <asm/arch/lubbock.h>
22062 +/* lubbock can also report usb connect/disconnect irqs */
22064 +#ifdef DEBUG
22065 +#define HEX_DISPLAY(n) if (machine_is_lubbock()) { LUB_HEXLED = (n); }
22067 +#define LED_CONNECTED_ON if (machine_is_lubbock()) { \
22068 + DISCRETE_LED_ON(D26); }
22069 +#define LED_CONNECTED_OFF if(machine_is_lubbock()) { \
22070 + DISCRETE_LED_OFF(D26); LUB_HEXLED = 0; }
22071 +#define LED_EP0_ON if (machine_is_lubbock()) { DISCRETE_LED_ON(D25); }
22072 +#define LED_EP0_OFF if (machine_is_lubbock()) { DISCRETE_LED_OFF(D25); }
22073 +#endif /* DEBUG */
22075 +#endif
22077 +#ifdef CONFIG_ARCH_PXA_CORGI
22078 +/* Sharp Zaurus C-700, C-750, C-760, C-860 */
22079 +#define CORGI_CONNECT_GPIO 45
22080 +/* use the ARM-Linux registered symbol, not a Lineo-private one */
22081 +#define CONFIG_MACH_CORGI
22082 +#endif
22084 +#ifdef CONFIG_ARCH_PXA_POODLE
22085 +/* Sharp B-500, SL-5600 */
22086 +#define POODLE_CONNECT_GPIO 20
22087 +/* use the ARM-Linux registered symbol, not a Lineo-private one */
22088 +#define CONFIG_MACH_POODLE
22089 +#endif
22091 +/*-------------------------------------------------------------------------*/
22093 +/* LEDs are only for debug */
22094 +#ifndef HEX_DISPLAY
22095 +#define HEX_DISPLAY(n) do {} while(0)
22096 +#endif
22098 +#ifndef LED_CONNECTED_ON
22099 +#define LED_CONNECTED_ON do {} while(0)
22100 +#define LED_CONNECTED_OFF do {} while(0)
22101 +#endif
22102 +#ifndef LED_EP0_ON
22103 +#define LED_EP0_ON do {} while (0)
22104 +#define LED_EP0_OFF do {} while (0)
22105 +#endif
22107 +/*-------------------------------------------------------------------------*/
22109 +static struct pxa2xx_udc *the_controller;
22111 +/* one GPIO should be used to detect host disconnect */
22112 +static int is_usb_connected(void)
22114 + static int first = 0;
22116 + // CONFIG_ARCH_ADI_COYOTE cannot detect or force disconnect
22117 +#ifdef CONFIG_ARCH_E7XX
22118 + if (machine_is_e7xx())
22119 + return (GPLR(GPIO_E7XX_USB_DISC)
22120 + & GPIO_bit(GPIO_E7XX_USB_DISC));
22121 +#endif
22122 +#if 0
22123 +#ifdef CONFIG_ARCH_H1900
22124 + if (machine_is_h1900())
22125 + return (!(GPLR(GPIO_NR_H1900_USB_DETECT_N)
22126 + & GPIO_bit(GPIO_NR_H1900_USB_DETECT_N)));
22127 +#endif
22128 +#ifdef CONFIG_ARCH_H3900
22129 + if (machine_is_h3900())
22130 + return 1;
22131 +#endif
22132 +#ifdef CONFIG_ARCH_H5400
22133 + // h5400 ... ?
22134 +#endif
22135 +#endif
22136 +#ifdef CONFIG_ARCH_INNOKOM
22137 + if (machine_is_innokom())
22138 + return (GPLR(GPIO_INNOKOM_USB_DISC)
22139 + & GPIO_bit(GPIO_INNOKOM_USB_DISC));
22140 +#endif
22141 +#ifdef CONFIG_ARCH_LUBBOCK
22142 + if (machine_is_lubbock())
22143 + return ((LUB_MISC_RD & (1 << 9)) == 0);
22144 +#endif
22145 + // Sharp's sources didn't show a corgi or poodle hook
22147 + if (!first) {
22148 + pr_info("%s: can't check host connect\n", driver_name);
22149 + first++;
22151 + return 1;
22154 +static int disc_first = 0;
22156 +/* one GPIO should force the host to see this device (or not) */
22157 +static void make_usb_disappear(void)
22159 + // CONFIG_ARCH_ADI_COYOTE cannot detect or force disconnect
22160 +#ifdef CONFIG_ARCH_E7XX
22161 + if (machine_is_e7xx()) {
22162 + GPSR(GPIO_E7XX_USB_PULLUP) = GPIO_bit(GPIO_E7XX_USB_PULLUP);
22163 + return;
22165 +#endif
22166 + // h1900 ... ?
22167 +#ifdef CONFIG_ARCH_H3900
22168 + if (machine_is_h3900()) {
22169 + GPDR0 &= ~GPIO_H3900_USBP_PULLUP;
22170 + return;
22172 +#endif
22173 +#ifdef CONFIG_ARCH_H5400
22174 + if (machine_is_h5400()) {
22175 + GPDR(GPIO_NR_H5400_USB_PULLUP) &=
22176 + ~GPIO_bit(GPIO_NR_H5400_USB_PULLUP);
22177 + return;
22179 +#endif
22180 +#ifdef CONFIG_ARCH_INNOKOM
22181 + if (machine_is_innokom()) {
22182 + GPSR(GPIO_INNOKOM_USB_ONOFF) = GPIO_bit(GPIO_INNOKOM_USB_ONOFF);
22183 + printk("innokom: disappear\n");
22184 + udelay(5);
22185 + return;
22187 +#endif
22188 +#ifdef CONFIG_ARCH_CSB226
22189 + if (machine_is_csb226()) {
22190 + GPCR0 |= 0x00000080;
22191 + printk("csb226: disappear\n");
22192 + udelay(5);
22193 + return;
22195 +#endif
22196 + // lubbock has no D+ pullup
22197 +#ifdef CONFIG_MACH_CORGI
22198 + if (machine_is_corgi()) {
22199 + GPDR(CORGI_CONNECT_GPIO) |= GPIO_bit(CORGI_CONNECT_GPIO);
22200 + GPCR(CORGI_CONNECT_GPIO) = GPIO_bit(CORGI_CONNECT_GPIO);
22202 +#endif
22203 +#ifdef CONFIG_MACH_POODLE
22204 + if (machine_is_poodle()) {
22205 + GPDR(POODLE_CONNECT_GPIO) |= GPIO_bit(POODLE_CONNECT_GPIO);
22206 + GPCR(POODLE_CONNECT_GPIO) = GPIO_bit(POODLE_CONNECT_GPIO);
22208 +#endif
22210 + if (!disc_first) {
22211 + pr_info("%s: can't force usb disconnect\n", driver_name);
22212 + disc_first++;
22216 +static void let_usb_appear(void)
22218 + // CONFIG_ARCH_ADI_COYOTE cannot detect or force disconnect
22219 +#ifdef CONFIG_ARCH_E7XX
22220 + if (machine_is_e7xx()) {
22221 + GPCR(GPIO_E7XX_USB_PULLUP) = GPIO_bit(GPIO_E7XX_USB_PULLUP);
22222 + return;
22224 +#endif
22225 + // h1900 ... ?
22226 +#ifdef CONFIG_ARCH_H3900
22227 + if (machine_is_h3900()) {
22228 + GPDR0 |= GPIO_H3900_USBP_PULLUP;
22229 + GPSR0 |= GPIO_H3900_USBP_PULLUP;
22230 + return;
22232 +#endif
22233 +#ifdef CONFIG_ARCH_H5400
22234 + if (machine_is_h5400()) {
22235 + GPDR(GPIO_NR_H5400_USB_PULLUP) |=
22236 + GPIO_bit(GPIO_NR_H5400_USB_PULLUP);
22237 + return;
22239 +#endif
22240 +#ifdef CONFIG_ARCH_INNOKOM
22241 + if (machine_is_innokom()) {
22242 + GPCR(GPIO_INNOKOM_USB_ONOFF) = GPIO_bit(GPIO_INNOKOM_USB_ONOFF);
22243 + printk("innokom: appear\n");
22244 + udelay(5);
22245 + return;
22247 +#endif
22248 +#ifdef CONFIG_ARCH_CSB226
22249 + if (machine_is_csb226()) {
22250 + GPDR0 |= 0x00000080;
22251 + GPSR0 |= 0x00000080;
22252 + printk("csb226: appear\n");
22253 + udelay(5);
22254 + return;
22256 +#endif
22257 + // lubbock has no D+ pullup
22258 +#ifdef CONFIG_MACH_CORGI
22259 + if (machine_is_corgi()) {
22260 + GPDR(CORGI_CONNECT_GPIO) |= GPIO_bit(CORGI_CONNECT_GPIO);
22261 + GPSR(CORGI_CONNECT_GPIO) = GPIO_bit(CORGI_CONNECT_GPIO);
22263 +#endif
22264 +#ifdef CONFIG_MACH_POODLE
22265 + if (machine_is_poodle()) {
22266 + GPDR(POODLE_CONNECT_GPIO) |= GPIO_bit(POODLE_CONNECT_GPIO);
22267 + GPSR(POODLE_CONNECT_GPIO) = GPIO_bit(POODLE_CONNECT_GPIO);
22269 +#endif
22271 + if (!disc_first) {
22272 + pr_info("%s: can't force usb disconnect\n", driver_name);
22273 + disc_first++;
22277 +/*-------------------------------------------------------------------------*/
22279 +/* LEDs are only for debug */
22280 +#ifndef LED_CONNECTED_ON
22281 +#define LED_CONNECTED_ON do {} while(0)
22282 +#define LED_CONNECTED_OFF do {} while(0)
22283 +#endif
22284 +#ifndef LED_EP0_ON
22285 +#define LED_EP0_ON do {} while (0)
22286 +#define LED_EP0_OFF do {} while (0)
22287 +#endif
22289 +/*-------------------------------------------------------------------------*/
22292 + * Debugging support vanishes in non-debug builds. DBG_NORMAL should be
22293 + * mostly silent during normal use/testing, with no timing side-effects.
22294 + */
22295 +#define DBG_NORMAL 1 /* error paths, device state transitions */
22296 +#define DBG_VERBOSE 2 /* add some success path trace info */
22297 +#define DBG_NOISY 3 /* ... even more: request level */
22298 +#define DBG_VERY_NOISY 4 /* ... even more: packet level */
22300 +#ifdef DEBUG
22302 +static const char *state_name[] = {
22303 + "EP0_IDLE",
22304 + "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
22305 + "EP0_END_XFER", "EP0_STALL"
22308 +#define DMSG(stuff...) printk(KERN_DEBUG "udc: " stuff)
22310 +#ifdef VERBOSE
22311 +# define UDC_DEBUG DBG_VERBOSE
22312 +#else
22313 +# define UDC_DEBUG DBG_NORMAL
22314 +#endif
22316 +static void __attribute__ ((__unused__))
22317 +dump_udccr(const char *label)
22319 + u32 udccr = UDCCR;
22320 + DMSG("%s %02X =%s%s%s%s%s%s%s%s\n",
22321 + label, udccr,
22322 + (udccr & UDCCR_REM) ? " rem" : "",
22323 + (udccr & UDCCR_RSTIR) ? " rstir" : "",
22324 + (udccr & UDCCR_SRM) ? " srm" : "",
22325 + (udccr & UDCCR_SUSIR) ? " susir" : "",
22326 + (udccr & UDCCR_RESIR) ? " resir" : "",
22327 + (udccr & UDCCR_RSM) ? " rsm" : "",
22328 + (udccr & UDCCR_UDA) ? " uda" : "",
22329 + (udccr & UDCCR_UDE) ? " ude" : "");
22332 +static void __attribute__ ((__unused__))
22333 +dump_udccs0(const char *label)
22335 + u32 udccs0 = UDCCS0;
22337 + DMSG("%s %s %02X =%s%s%s%s%s%s%s%s\n",
22338 + label, state_name[the_controller->ep0state], udccs0,
22339 + (udccs0 & UDCCS0_SA) ? " sa" : "",
22340 + (udccs0 & UDCCS0_RNE) ? " rne" : "",
22341 + (udccs0 & UDCCS0_FST) ? " fst" : "",
22342 + (udccs0 & UDCCS0_SST) ? " sst" : "",
22343 + (udccs0 & UDCCS0_DRWF) ? " dwrf" : "",
22344 + (udccs0 & UDCCS0_FTF) ? " ftf" : "",
22345 + (udccs0 & UDCCS0_IPR) ? " ipr" : "",
22346 + (udccs0 & UDCCS0_OPR) ? " opr" : "");
22349 +static void __attribute__ ((__unused__))
22350 +dump_state(struct pxa2xx_udc *dev)
22352 + u32 tmp;
22353 + unsigned i;
22355 + DMSG("%s %s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
22356 + is_usb_connected() ? "host " : "disconnected",
22357 + state_name[dev->ep0state],
22358 + UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
22359 + dump_udccr("udccr");
22360 + if (dev->has_cfr) {
22361 + tmp = UDCCFR;
22362 + DMSG("udccfr %02X =%s%s\n", tmp,
22363 + (tmp & UDCCFR_AREN) ? " aren" : "",
22364 + (tmp & UDCCFR_ACM) ? " acm" : "");
22367 + if (!dev->driver) {
22368 + DMSG("no gadget driver bound\n");
22369 + return;
22370 + } else
22371 + DMSG("ep0 driver '%s'\n", dev->driver->driver.name);
22373 + if (!is_usb_connected())
22374 + return;
22376 + dump_udccs0 ("udccs0");
22377 + DMSG("ep0 IN %lu/%lu, OUT %lu/%lu\n",
22378 + dev->stats.write.bytes, dev->stats.write.ops,
22379 + dev->stats.read.bytes, dev->stats.read.ops);
22381 + for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++) {
22382 + if (dev->ep [i].desc == 0)
22383 + continue;
22384 + DMSG ("udccs%d = %02x\n", i, *dev->ep->reg_udccs);
22388 +#else
22390 +#define DMSG(stuff...) do{}while(0)
22392 +#define dump_udccr(x) do{}while(0)
22393 +#define dump_udccs0(x) do{}while(0)
22394 +#define dump_state(x) do{}while(0)
22396 +#define UDC_DEBUG ((unsigned)0)
22398 +#endif
22400 +#define DBG(lvl, stuff...) do{if ((lvl) <= UDC_DEBUG) DMSG(stuff);}while(0)
22402 +#define WARN(stuff...) printk(KERN_WARNING "udc: " stuff)
22403 +#define INFO(stuff...) printk(KERN_INFO "udc: " stuff)
22406 +/* 2.4 backport support */
22407 +#define irqreturn_t void
22408 +#define IRQ_HANDLED
22411 +#endif /* __LINUX_USB_GADGET_PXA2XX_H */
22412 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/rndis.c kernel/drivers/usb/gadget/rndis.c
22413 --- /tmp/kernel/drivers/usb/gadget/rndis.c 1970-01-01 01:00:00.000000000 +0100
22414 +++ kernel/drivers/usb/gadget/rndis.c 2005-04-22 17:53:19.501528608 +0200
22415 @@ -0,0 +1,1425 @@
22416 +/*
22417 + * RNDIS MSG parser
22418 + *
22419 + * Version: $Id: rndis.c,v 1.19 2004/03/25 21:33:46 robert Exp $
22420 + *
22421 + * Authors: Benedikt Spranger, Pengutronix
22422 + * Robert Schwebel, Pengutronix
22423 + *
22424 + * This program is free software; you can redistribute it and/or
22425 + * modify it under the terms of the GNU General Public License
22426 + * version 2, as published by the Free Software Foundation.
22427 + *
22428 + * This software was originally developed in conformance with
22429 + * Microsoft's Remote NDIS Specification License Agreement.
22430 + *
22431 + * 03/12/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
22432 + * Fixed message length bug in init_response
22433 + *
22434 + * 03/25/2004 Kai-Uwe Bloem <linux-development@auerswald.de>
22435 + * Fixed rndis_rm_hdr length bug.
22437 + * Copyright (C) 2004 by David Brownell
22438 + * updates to merge with Linux 2.6, better match RNDIS spec
22439 + */
22441 +#include <linux/config.h>
22442 +#include <linux/module.h>
22443 +#include <linux/moduleparam.h>
22444 +#include <linux/kernel.h>
22445 +#include <linux/errno.h>
22446 +#include <linux/version.h>
22447 +#include <linux/init.h>
22448 +#include <linux/list.h>
22449 +#include <linux/proc_fs.h>
22450 +#include <linux/netdevice.h>
22452 +#include <asm/io.h>
22453 +#include <asm/byteorder.h>
22454 +#include <asm/system.h>
22457 +#undef RNDIS_PM
22458 +#undef VERBOSE
22460 +#include "rndis.h"
22463 +/* The driver for your USB chip needs to support ep0 OUT to work with
22464 + * RNDIS, plus all three CDC Ethernet endpoints (interrupt not optional).
22466 + * Windows hosts need an INF file like Documentation/usb/linux.inf
22467 + * and will be happier if you provide the host_addr module parameter.
22468 + */
22470 +#if 0
22471 +#define DEBUG(str,args...) do { \
22472 + if (rndis_debug) \
22473 + printk(KERN_DEBUG str , ## args ); \
22474 + } while (0)
22475 +static int rndis_debug = 0;
22477 +module_param (rndis_debug, bool, 0);
22478 +MODULE_PARM_DESC (rndis_debug, "enable debugging");
22480 +#else
22482 +#define rndis_debug 0
22483 +#define DEBUG(str,args...) do{}while(0)
22484 +#endif
22486 +#define RNDIS_MAX_CONFIGS 1
22489 +static rndis_params rndis_per_dev_params [RNDIS_MAX_CONFIGS];
22491 +/* Driver Version */
22492 +static const u32 rndis_driver_version = __constant_cpu_to_le32 (1);
22494 +/* Function Prototypes */
22495 +static int rndis_init_response (int configNr, rndis_init_msg_type *buf);
22496 +static int rndis_query_response (int configNr, rndis_query_msg_type *buf);
22497 +static int rndis_set_response (int configNr, rndis_set_msg_type *buf);
22498 +static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf);
22499 +static int rndis_keepalive_response (int configNr,
22500 + rndis_keepalive_msg_type *buf);
22502 +static rndis_resp_t *rndis_add_response (int configNr, u32 length);
22505 +/* NDIS Functions */
22506 +static int gen_ndis_query_resp (int configNr, u32 OID, rndis_resp_t *r)
22508 + int retval = -ENOTSUPP;
22509 + u32 length = 0;
22510 + u32 *tmp;
22511 + int i, count;
22512 + rndis_query_cmplt_type *resp;
22514 + if (!r) return -ENOMEM;
22515 + resp = (rndis_query_cmplt_type *) r->buf;
22517 + if (!resp) return -ENOMEM;
22519 + switch (OID) {
22521 + /* general oids (table 4-1) */
22523 + /* mandatory */
22524 + case OID_GEN_SUPPORTED_LIST:
22525 + DEBUG ("%s: OID_GEN_SUPPORTED_LIST\n", __FUNCTION__);
22526 + length = sizeof (oid_supported_list);
22527 + count = length / sizeof (u32);
22528 + tmp = (u32 *) ((u8 *)resp + 24);
22529 + for (i = 0; i < count; i++)
22530 + tmp[i] = cpu_to_le32 (oid_supported_list[i]);
22531 + retval = 0;
22532 + break;
22534 + /* mandatory */
22535 + case OID_GEN_HARDWARE_STATUS:
22536 + DEBUG("%s: OID_GEN_HARDWARE_STATUS\n", __FUNCTION__);
22537 + length = 4;
22538 + /* Bogus question!
22539 + * Hardware must be ready to receive high level protocols.
22540 + * BTW:
22541 + * reddite ergo quae sunt Caesaris Caesari
22542 + * et quae sunt Dei Deo!
22543 + */
22544 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22545 + retval = 0;
22546 + break;
22548 + /* mandatory */
22549 + case OID_GEN_MEDIA_SUPPORTED:
22550 + DEBUG("%s: OID_GEN_MEDIA_SUPPORTED\n", __FUNCTION__);
22551 + length = 4;
22552 + *((u32 *) resp + 6) = cpu_to_le32 (
22553 + rndis_per_dev_params [configNr].medium);
22554 + retval = 0;
22555 + break;
22557 + /* mandatory */
22558 + case OID_GEN_MEDIA_IN_USE:
22559 + DEBUG("%s: OID_GEN_MEDIA_IN_USE\n", __FUNCTION__);
22560 + length = 4;
22561 + /* one medium, one transport... (maybe you do it better) */
22562 + *((u32 *) resp + 6) = cpu_to_le32 (
22563 + rndis_per_dev_params [configNr].medium);
22564 + retval = 0;
22565 + break;
22567 + /* mandatory */
22568 + case OID_GEN_MAXIMUM_FRAME_SIZE:
22569 + DEBUG("%s: OID_GEN_MAXIMUM_FRAME_SIZE\n", __FUNCTION__);
22570 + if (rndis_per_dev_params [configNr].dev) {
22571 + length = 4;
22572 + *((u32 *) resp + 6) = cpu_to_le32 (
22573 + rndis_per_dev_params [configNr].dev->mtu);
22574 + retval = 0;
22575 + } else {
22576 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22577 + retval = 0;
22579 + break;
22581 + /* mandatory */
22582 + case OID_GEN_LINK_SPEED:
22583 + DEBUG("%s: OID_GEN_LINK_SPEED\n", __FUNCTION__);
22584 + length = 4;
22585 + if (rndis_per_dev_params [configNr].media_state
22586 + == NDIS_MEDIA_STATE_DISCONNECTED)
22587 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22588 + else
22589 + *((u32 *) resp + 6) = cpu_to_le32 (
22590 + rndis_per_dev_params [configNr].speed);
22591 + retval = 0;
22592 + break;
22594 + /* mandatory */
22595 + case OID_GEN_TRANSMIT_BLOCK_SIZE:
22596 + DEBUG("%s: OID_GEN_TRANSMIT_BLOCK_SIZE\n", __FUNCTION__);
22597 + if (rndis_per_dev_params [configNr].dev) {
22598 + length = 4;
22599 + *((u32 *) resp + 6) = cpu_to_le32 (
22600 + rndis_per_dev_params [configNr].dev->mtu);
22601 + retval = 0;
22603 + break;
22605 + /* mandatory */
22606 + case OID_GEN_RECEIVE_BLOCK_SIZE:
22607 + DEBUG("%s: OID_GEN_RECEIVE_BLOCK_SIZE\n", __FUNCTION__);
22608 + if (rndis_per_dev_params [configNr].dev) {
22609 + length = 4;
22610 + *((u32 *) resp + 6) = cpu_to_le32 (
22611 + rndis_per_dev_params [configNr].dev->mtu);
22612 + retval = 0;
22614 + break;
22616 + /* mandatory */
22617 + case OID_GEN_VENDOR_ID:
22618 + DEBUG("%s: OID_GEN_VENDOR_ID\n", __FUNCTION__);
22619 + length = 4;
22620 + *((u32 *) resp + 6) = cpu_to_le32 (
22621 + rndis_per_dev_params [configNr].vendorID);
22622 + retval = 0;
22623 + break;
22625 + /* mandatory */
22626 + case OID_GEN_VENDOR_DESCRIPTION:
22627 + DEBUG("%s: OID_GEN_VENDOR_DESCRIPTION\n", __FUNCTION__);
22628 + length = strlen (rndis_per_dev_params [configNr].vendorDescr);
22629 + memcpy ((u8 *) resp + 24,
22630 + rndis_per_dev_params [configNr].vendorDescr, length);
22631 + retval = 0;
22632 + break;
22634 + case OID_GEN_VENDOR_DRIVER_VERSION:
22635 + DEBUG("%s: OID_GEN_VENDOR_DRIVER_VERSION\n", __FUNCTION__);
22636 + length = 4;
22637 + /* Created as LE */
22638 + *((u32 *) resp + 6) = rndis_driver_version;
22639 + retval = 0;
22640 + break;
22642 + /* mandatory */
22643 + case OID_GEN_CURRENT_PACKET_FILTER:
22644 + DEBUG("%s: OID_GEN_CURRENT_PACKET_FILTER\n", __FUNCTION__);
22645 + length = 4;
22646 + *((u32 *) resp + 6) = cpu_to_le32 (
22647 + rndis_per_dev_params[configNr].filter);
22648 + retval = 0;
22649 + break;
22651 + /* mandatory */
22652 + case OID_GEN_MAXIMUM_TOTAL_SIZE:
22653 + DEBUG("%s: OID_GEN_MAXIMUM_TOTAL_SIZE\n", __FUNCTION__);
22654 + length = 4;
22655 + *((u32 *) resp + 6) = __constant_cpu_to_le32(
22656 + RNDIS_MAX_TOTAL_SIZE);
22657 + retval = 0;
22658 + break;
22660 + /* mandatory */
22661 + case OID_GEN_MEDIA_CONNECT_STATUS:
22662 + DEBUG("%s: OID_GEN_MEDIA_CONNECT_STATUS\n", __FUNCTION__);
22663 + length = 4;
22664 + *((u32 *) resp + 6) = cpu_to_le32 (
22665 + rndis_per_dev_params [configNr]
22666 + .media_state);
22667 + retval = 0;
22668 + break;
22670 + case OID_GEN_PHYSICAL_MEDIUM:
22671 + DEBUG("%s: OID_GEN_PHYSICAL_MEDIUM\n", __FUNCTION__);
22672 + length = 4;
22673 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22674 + retval = 0;
22675 + break;
22677 + /* The RNDIS specification is incomplete/wrong. Some versions
22678 + * of MS-Windows expect OIDs that aren't specified there. Other
22679 + * versions emit undefined RNDIS messages. DOCUMENT ALL THESE!
22680 + */
22681 + case OID_GEN_MAC_OPTIONS: /* from WinME */
22682 + DEBUG("%s: OID_GEN_MAC_OPTIONS\n", __FUNCTION__);
22683 + length = 4;
22684 + *((u32 *) resp + 6) = __constant_cpu_to_le32(
22685 + NDIS_MAC_OPTION_RECEIVE_SERIALIZED
22686 + | NDIS_MAC_OPTION_FULL_DUPLEX);
22687 + retval = 0;
22688 + break;
22690 + /* statistics OIDs (table 4-2) */
22692 + /* mandatory */
22693 + case OID_GEN_XMIT_OK:
22694 + DEBUG("%s: OID_GEN_XMIT_OK\n", __FUNCTION__);
22695 + if (rndis_per_dev_params [configNr].stats) {
22696 + length = 4;
22697 + *((u32 *) resp + 6) = cpu_to_le32 (
22698 + rndis_per_dev_params [configNr].stats->tx_packets -
22699 + rndis_per_dev_params [configNr].stats->tx_errors -
22700 + rndis_per_dev_params [configNr].stats->tx_dropped);
22701 + retval = 0;
22702 + } else {
22703 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22704 + retval = 0;
22706 + break;
22708 + /* mandatory */
22709 + case OID_GEN_RCV_OK:
22710 + DEBUG("%s: OID_GEN_RCV_OK\n", __FUNCTION__);
22711 + if (rndis_per_dev_params [configNr].stats) {
22712 + length = 4;
22713 + *((u32 *) resp + 6) = cpu_to_le32 (
22714 + rndis_per_dev_params [configNr].stats->rx_packets -
22715 + rndis_per_dev_params [configNr].stats->rx_errors -
22716 + rndis_per_dev_params [configNr].stats->rx_dropped);
22717 + retval = 0;
22718 + } else {
22719 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22720 + retval = 0;
22722 + break;
22724 + /* mandatory */
22725 + case OID_GEN_XMIT_ERROR:
22726 + DEBUG("%s: OID_GEN_XMIT_ERROR\n", __FUNCTION__);
22727 + if (rndis_per_dev_params [configNr].stats) {
22728 + length = 4;
22729 + *((u32 *) resp + 6) = cpu_to_le32 (
22730 + rndis_per_dev_params [configNr]
22731 + .stats->tx_errors);
22732 + retval = 0;
22733 + } else {
22734 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22735 + retval = 0;
22737 + break;
22739 + /* mandatory */
22740 + case OID_GEN_RCV_ERROR:
22741 + DEBUG("%s: OID_GEN_RCV_ERROR\n", __FUNCTION__);
22742 + if (rndis_per_dev_params [configNr].stats) {
22743 + *((u32 *) resp + 6) = cpu_to_le32 (
22744 + rndis_per_dev_params [configNr]
22745 + .stats->rx_errors);
22746 + retval = 0;
22747 + } else {
22748 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22749 + retval = 0;
22751 + break;
22753 + /* mandatory */
22754 + case OID_GEN_RCV_NO_BUFFER:
22755 + DEBUG("%s: OID_GEN_RCV_NO_BUFFER\n", __FUNCTION__);
22756 + if (rndis_per_dev_params [configNr].stats) {
22757 + *((u32 *) resp + 6) = cpu_to_le32 (
22758 + rndis_per_dev_params [configNr]
22759 + .stats->rx_dropped);
22760 + retval = 0;
22761 + } else {
22762 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22763 + retval = 0;
22765 + break;
22767 +#ifdef RNDIS_OPTIONAL_STATS
22768 + case OID_GEN_DIRECTED_BYTES_XMIT:
22769 + DEBUG("%s: OID_GEN_DIRECTED_BYTES_XMIT\n", __FUNCTION__);
22770 + /*
22771 + * Aunt Tilly's size of shoes
22772 + * minus antarctica count of penguins
22773 + * divided by weight of Alpha Centauri
22774 + */
22775 + if (rndis_per_dev_params [configNr].stats) {
22776 + length = 4;
22777 + *((u32 *) resp + 6) = cpu_to_le32 (
22778 + (rndis_per_dev_params [configNr]
22779 + .stats->tx_packets -
22780 + rndis_per_dev_params [configNr]
22781 + .stats->tx_errors -
22782 + rndis_per_dev_params [configNr]
22783 + .stats->tx_dropped)
22784 + * 123);
22785 + retval = 0;
22786 + } else {
22787 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22788 + retval = 0;
22790 + break;
22792 + case OID_GEN_DIRECTED_FRAMES_XMIT:
22793 + DEBUG("%s: OID_GEN_DIRECTED_FRAMES_XMIT\n", __FUNCTION__);
22794 + /* dito */
22795 + if (rndis_per_dev_params [configNr].stats) {
22796 + length = 4;
22797 + *((u32 *) resp + 6) = cpu_to_le32 (
22798 + (rndis_per_dev_params [configNr]
22799 + .stats->tx_packets -
22800 + rndis_per_dev_params [configNr]
22801 + .stats->tx_errors -
22802 + rndis_per_dev_params [configNr]
22803 + .stats->tx_dropped)
22804 + / 123);
22805 + retval = 0;
22806 + } else {
22807 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22808 + retval = 0;
22810 + break;
22812 + case OID_GEN_MULTICAST_BYTES_XMIT:
22813 + DEBUG("%s: OID_GEN_MULTICAST_BYTES_XMIT\n", __FUNCTION__);
22814 + if (rndis_per_dev_params [configNr].stats) {
22815 + *((u32 *) resp + 6) = cpu_to_le32 (
22816 + rndis_per_dev_params [configNr]
22817 + .stats->multicast*1234);
22818 + retval = 0;
22819 + } else {
22820 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22821 + retval = 0;
22823 + break;
22825 + case OID_GEN_MULTICAST_FRAMES_XMIT:
22826 + DEBUG("%s: OID_GEN_MULTICAST_FRAMES_XMIT\n", __FUNCTION__);
22827 + if (rndis_per_dev_params [configNr].stats) {
22828 + *((u32 *) resp + 6) = cpu_to_le32 (
22829 + rndis_per_dev_params [configNr]
22830 + .stats->multicast);
22831 + retval = 0;
22832 + } else {
22833 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22834 + retval = 0;
22836 + break;
22838 + case OID_GEN_BROADCAST_BYTES_XMIT:
22839 + DEBUG("%s: OID_GEN_BROADCAST_BYTES_XMIT\n", __FUNCTION__);
22840 + if (rndis_per_dev_params [configNr].stats) {
22841 + *((u32 *) resp + 6) = cpu_to_le32 (
22842 + rndis_per_dev_params [configNr]
22843 + .stats->tx_packets/42*255);
22844 + retval = 0;
22845 + } else {
22846 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22847 + retval = 0;
22849 + break;
22851 + case OID_GEN_BROADCAST_FRAMES_XMIT:
22852 + DEBUG("%s: OID_GEN_BROADCAST_FRAMES_XMIT\n", __FUNCTION__);
22853 + if (rndis_per_dev_params [configNr].stats) {
22854 + *((u32 *) resp + 6) = cpu_to_le32 (
22855 + rndis_per_dev_params [configNr]
22856 + .stats->tx_packets/42);
22857 + retval = 0;
22858 + } else {
22859 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22860 + retval = 0;
22862 + break;
22864 + case OID_GEN_DIRECTED_BYTES_RCV:
22865 + DEBUG("%s: OID_GEN_DIRECTED_BYTES_RCV\n", __FUNCTION__);
22866 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22867 + retval = 0;
22868 + break;
22870 + case OID_GEN_DIRECTED_FRAMES_RCV:
22871 + DEBUG("%s: OID_GEN_DIRECTED_FRAMES_RCV\n", __FUNCTION__);
22872 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22873 + retval = 0;
22874 + break;
22876 + case OID_GEN_MULTICAST_BYTES_RCV:
22877 + DEBUG("%s: OID_GEN_MULTICAST_BYTES_RCV\n", __FUNCTION__);
22878 + if (rndis_per_dev_params [configNr].stats) {
22879 + *((u32 *) resp + 6) = cpu_to_le32 (
22880 + rndis_per_dev_params [configNr]
22881 + .stats->multicast * 1111);
22882 + retval = 0;
22883 + } else {
22884 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22885 + retval = 0;
22887 + break;
22889 + case OID_GEN_MULTICAST_FRAMES_RCV:
22890 + DEBUG("%s: OID_GEN_MULTICAST_FRAMES_RCV\n", __FUNCTION__);
22891 + if (rndis_per_dev_params [configNr].stats) {
22892 + *((u32 *) resp + 6) = cpu_to_le32 (
22893 + rndis_per_dev_params [configNr]
22894 + .stats->multicast);
22895 + retval = 0;
22896 + } else {
22897 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22898 + retval = 0;
22900 + break;
22902 + case OID_GEN_BROADCAST_BYTES_RCV:
22903 + DEBUG("%s: OID_GEN_BROADCAST_BYTES_RCV\n", __FUNCTION__);
22904 + if (rndis_per_dev_params [configNr].stats) {
22905 + *((u32 *) resp + 6) = cpu_to_le32 (
22906 + rndis_per_dev_params [configNr]
22907 + .stats->rx_packets/42*255);
22908 + retval = 0;
22909 + } else {
22910 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22911 + retval = 0;
22913 + break;
22915 + case OID_GEN_BROADCAST_FRAMES_RCV:
22916 + DEBUG("%s: OID_GEN_BROADCAST_FRAMES_RCV\n", __FUNCTION__);
22917 + if (rndis_per_dev_params [configNr].stats) {
22918 + *((u32 *) resp + 6) = cpu_to_le32 (
22919 + rndis_per_dev_params [configNr]
22920 + .stats->rx_packets/42);
22921 + retval = 0;
22922 + } else {
22923 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22924 + retval = 0;
22926 + break;
22928 + case OID_GEN_RCV_CRC_ERROR:
22929 + DEBUG("%s: OID_GEN_RCV_CRC_ERROR\n", __FUNCTION__);
22930 + if (rndis_per_dev_params [configNr].stats) {
22931 + *((u32 *) resp + 6) = cpu_to_le32 (
22932 + rndis_per_dev_params [configNr]
22933 + .stats->rx_crc_errors);
22934 + retval = 0;
22935 + } else {
22936 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22937 + retval = 0;
22939 + break;
22941 + case OID_GEN_TRANSMIT_QUEUE_LENGTH:
22942 + DEBUG("%s: OID_GEN_TRANSMIT_QUEUE_LENGTH\n", __FUNCTION__);
22943 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22944 + retval = 0;
22945 + break;
22946 +#endif /* RNDIS_OPTIONAL_STATS */
22948 + /* ieee802.3 OIDs (table 4-3) */
22950 + /* mandatory */
22951 + case OID_802_3_PERMANENT_ADDRESS:
22952 + DEBUG("%s: OID_802_3_PERMANENT_ADDRESS\n", __FUNCTION__);
22953 + if (rndis_per_dev_params [configNr].dev) {
22954 + length = ETH_ALEN;
22955 + memcpy ((u8 *) resp + 24,
22956 + rndis_per_dev_params [configNr].host_mac,
22957 + length);
22958 + retval = 0;
22959 + } else {
22960 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
22961 + retval = 0;
22963 + break;
22965 + /* mandatory */
22966 + case OID_802_3_CURRENT_ADDRESS:
22967 + DEBUG("%s: OID_802_3_CURRENT_ADDRESS\n", __FUNCTION__);
22968 + if (rndis_per_dev_params [configNr].dev) {
22969 + length = ETH_ALEN;
22970 + memcpy ((u8 *) resp + 24,
22971 + rndis_per_dev_params [configNr].host_mac,
22972 + length);
22973 + retval = 0;
22975 + break;
22977 + /* mandatory */
22978 + case OID_802_3_MULTICAST_LIST:
22979 + DEBUG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
22980 + length = 4;
22981 + /* Multicast base address only */
22982 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0xE0000000);
22983 + retval = 0;
22984 + break;
22986 + /* mandatory */
22987 + case OID_802_3_MAXIMUM_LIST_SIZE:
22988 + DEBUG("%s: OID_802_3_MAXIMUM_LIST_SIZE\n", __FUNCTION__);
22989 + length = 4;
22990 + /* Multicast base address only */
22991 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (1);
22992 + retval = 0;
22993 + break;
22995 + case OID_802_3_MAC_OPTIONS:
22996 + DEBUG("%s: OID_802_3_MAC_OPTIONS\n", __FUNCTION__);
22997 + break;
22999 + /* ieee802.3 statistics OIDs (table 4-4) */
23001 + /* mandatory */
23002 + case OID_802_3_RCV_ERROR_ALIGNMENT:
23003 + DEBUG("%s: OID_802_3_RCV_ERROR_ALIGNMENT\n", __FUNCTION__);
23004 + if (rndis_per_dev_params [configNr].stats)
23006 + length = 4;
23007 + *((u32 *) resp + 6) = cpu_to_le32 (
23008 + rndis_per_dev_params [configNr]
23009 + .stats->rx_frame_errors);
23010 + retval = 0;
23012 + break;
23014 + /* mandatory */
23015 + case OID_802_3_XMIT_ONE_COLLISION:
23016 + DEBUG("%s: OID_802_3_XMIT_ONE_COLLISION\n", __FUNCTION__);
23017 + length = 4;
23018 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
23019 + retval = 0;
23020 + break;
23022 + /* mandatory */
23023 + case OID_802_3_XMIT_MORE_COLLISIONS:
23024 + DEBUG("%s: OID_802_3_XMIT_MORE_COLLISIONS\n", __FUNCTION__);
23025 + length = 4;
23026 + *((u32 *) resp + 6) = __constant_cpu_to_le32 (0);
23027 + retval = 0;
23028 + break;
23030 +#ifdef RNDIS_OPTIONAL_STATS
23031 + case OID_802_3_XMIT_DEFERRED:
23032 + DEBUG("%s: OID_802_3_XMIT_DEFERRED\n", __FUNCTION__);
23033 + /* TODO */
23034 + break;
23036 + case OID_802_3_XMIT_MAX_COLLISIONS:
23037 + DEBUG("%s: OID_802_3_XMIT_MAX_COLLISIONS\n", __FUNCTION__);
23038 + /* TODO */
23039 + break;
23041 + case OID_802_3_RCV_OVERRUN:
23042 + DEBUG("%s: OID_802_3_RCV_OVERRUN\n", __FUNCTION__);
23043 + /* TODO */
23044 + break;
23046 + case OID_802_3_XMIT_UNDERRUN:
23047 + DEBUG("%s: OID_802_3_XMIT_UNDERRUN\n", __FUNCTION__);
23048 + /* TODO */
23049 + break;
23051 + case OID_802_3_XMIT_HEARTBEAT_FAILURE:
23052 + DEBUG("%s: OID_802_3_XMIT_HEARTBEAT_FAILURE\n", __FUNCTION__);
23053 + /* TODO */
23054 + break;
23056 + case OID_802_3_XMIT_TIMES_CRS_LOST:
23057 + DEBUG("%s: OID_802_3_XMIT_TIMES_CRS_LOST\n", __FUNCTION__);
23058 + /* TODO */
23059 + break;
23061 + case OID_802_3_XMIT_LATE_COLLISIONS:
23062 + DEBUG("%s: OID_802_3_XMIT_LATE_COLLISIONS\n", __FUNCTION__);
23063 + /* TODO */
23064 + break;
23065 +#endif /* RNDIS_OPTIONAL_STATS */
23067 +#ifdef RNDIS_PM
23068 + /* power management OIDs (table 4-5) */
23069 + case OID_PNP_CAPABILITIES:
23070 + DEBUG("%s: OID_PNP_CAPABILITIES\n", __FUNCTION__);
23072 + /* just PM, and remote wakeup on link status change
23073 + * (not magic packet or pattern match)
23074 + */
23075 + length = sizeof (struct NDIS_PNP_CAPABILITIES);
23076 + memset (resp, 0, length);
23078 + struct NDIS_PNP_CAPABILITIES *caps = (void *) resp;
23080 + caps->Flags = NDIS_DEVICE_WAKE_UP_ENABLE;
23081 + caps->WakeUpCapabilities.MinLinkChangeWakeUp
23082 + = NdisDeviceStateD3;
23084 + /* FIXME then use usb_gadget_wakeup(), and
23085 + * set USB_CONFIG_ATT_WAKEUP in config desc
23086 + */
23088 + retval = 0;
23089 + break;
23090 + case OID_PNP_QUERY_POWER:
23091 + DEBUG("%s: OID_PNP_QUERY_POWER\n", __FUNCTION__);
23092 + /* sure, handle any power state that maps to USB suspend */
23093 + retval = 0;
23094 + break;
23095 +#endif
23097 + default:
23098 + printk (KERN_WARNING "%s: query unknown OID 0x%08X\n",
23099 + __FUNCTION__, OID);
23102 + resp->InformationBufferOffset = __constant_cpu_to_le32 (16);
23103 + resp->InformationBufferLength = cpu_to_le32 (length);
23104 + resp->MessageLength = cpu_to_le32 (24 + length);
23105 + r->length = 24 + length;
23106 + return retval;
23109 +static int gen_ndis_set_resp (u8 configNr, u32 OID, u8 *buf, u32 buf_len,
23110 + rndis_resp_t *r)
23112 + rndis_set_cmplt_type *resp;
23113 + int i, retval = -ENOTSUPP;
23114 + struct rndis_params *params;
23116 + if (!r)
23117 + return -ENOMEM;
23118 + resp = (rndis_set_cmplt_type *) r->buf;
23119 + if (!resp)
23120 + return -ENOMEM;
23122 + DEBUG("set OID %08x value, len %d:\n", OID, buf_len);
23123 + for (i = 0; i < buf_len; i += 16) {
23124 + DEBUG ("%03d: "
23125 + " %02x %02x %02x %02x"
23126 + " %02x %02x %02x %02x"
23127 + " %02x %02x %02x %02x"
23128 + " %02x %02x %02x %02x"
23129 + "\n",
23130 + i,
23131 + buf[i], buf [i+1],
23132 + buf[i+2], buf[i+3],
23133 + buf[i+4], buf [i+5],
23134 + buf[i+6], buf[i+7],
23135 + buf[i+8], buf [i+9],
23136 + buf[i+10], buf[i+11],
23137 + buf[i+12], buf [i+13],
23138 + buf[i+14], buf[i+15]);
23141 + switch (OID) {
23142 + case OID_GEN_CURRENT_PACKET_FILTER:
23143 + params = &rndis_per_dev_params [configNr];
23144 + retval = 0;
23146 + /* FIXME use these NDIS_PACKET_TYPE_* bitflags to
23147 + * filter packets in hard_start_xmit()
23148 + * NDIS_PACKET_TYPE_x == USB_CDC_PACKET_TYPE_x for x in:
23149 + * PROMISCUOUS, DIRECTED,
23150 + * MULTICAST, ALL_MULTICAST, BROADCAST
23151 + */
23152 + params->filter = cpu_to_le32p((u32 *)buf);
23153 + DEBUG("%s: OID_GEN_CURRENT_PACKET_FILTER %08x\n",
23154 + __FUNCTION__, params->filter);
23156 + /* this call has a significant side effect: it's
23157 + * what makes the packet flow start and stop, like
23158 + * activating the CDC Ethernet altsetting.
23159 + */
23160 + if (params->filter) {
23161 + params->state = RNDIS_DATA_INITIALIZED;
23162 + netif_carrier_on(params->dev);
23163 + if (netif_running(params->dev))
23164 + netif_wake_queue (params->dev);
23165 + } else {
23166 + params->state = RNDIS_INITIALIZED;
23167 + netif_carrier_off (params->dev);
23168 + netif_stop_queue (params->dev);
23170 + break;
23172 + case OID_802_3_MULTICAST_LIST:
23173 + /* I think we can ignore this */
23174 + DEBUG("%s: OID_802_3_MULTICAST_LIST\n", __FUNCTION__);
23175 + retval = 0;
23176 + break;
23177 +#if 0
23178 + case OID_GEN_RNDIS_CONFIG_PARAMETER:
23180 + struct rndis_config_parameter *param;
23181 + param = (struct rndis_config_parameter *) buf;
23182 + DEBUG("%s: OID_GEN_RNDIS_CONFIG_PARAMETER '%*s'\n",
23183 + __FUNCTION__,
23184 + min(cpu_to_le32(param->ParameterNameLength),80),
23185 + buf + param->ParameterNameOffset);
23186 + retval = 0;
23188 + break;
23189 +#endif
23191 +#ifdef RNDIS_PM
23192 + case OID_PNP_SET_POWER:
23193 + DEBUG ("OID_PNP_SET_POWER\n");
23194 + /* sure, handle any power state that maps to USB suspend */
23195 + retval = 0;
23196 + break;
23198 + case OID_PNP_ENABLE_WAKE_UP:
23199 + /* always-connected ... */
23200 + DEBUG ("OID_PNP_ENABLE_WAKE_UP\n");
23201 + retval = 0;
23202 + break;
23204 + // no PM resume patterns supported (specified where?)
23205 + // so OID_PNP_{ADD,REMOVE}_WAKE_UP_PATTERN always fails
23206 +#endif
23208 + default:
23209 + printk (KERN_WARNING "%s: set unknown OID 0x%08X, size %d\n",
23210 + __FUNCTION__, OID, buf_len);
23213 + return retval;
23216 +/*
23217 + * Response Functions
23218 + */
23220 +static int rndis_init_response (int configNr, rndis_init_msg_type *buf)
23222 + rndis_init_cmplt_type *resp;
23223 + rndis_resp_t *r;
23225 + if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
23227 + r = rndis_add_response (configNr, sizeof (rndis_init_cmplt_type));
23229 + if (!r) return -ENOMEM;
23231 + resp = (rndis_init_cmplt_type *) r->buf;
23233 + if (!resp) return -ENOMEM;
23235 + resp->MessageType = __constant_cpu_to_le32 (
23236 + REMOTE_NDIS_INITIALIZE_CMPLT);
23237 + resp->MessageLength = __constant_cpu_to_le32 (52);
23238 + resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
23239 + resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
23240 + resp->MajorVersion = __constant_cpu_to_le32 (RNDIS_MAJOR_VERSION);
23241 + resp->MinorVersion = __constant_cpu_to_le32 (RNDIS_MINOR_VERSION);
23242 + resp->DeviceFlags = __constant_cpu_to_le32 (RNDIS_DF_CONNECTIONLESS);
23243 + resp->Medium = __constant_cpu_to_le32 (RNDIS_MEDIUM_802_3);
23244 + resp->MaxPacketsPerTransfer = __constant_cpu_to_le32 (1);
23245 + resp->MaxTransferSize = cpu_to_le32 (
23246 + rndis_per_dev_params [configNr].dev->mtu
23247 + + sizeof (struct ethhdr)
23248 + + sizeof (struct rndis_packet_msg_type)
23249 + + 22);
23250 + resp->PacketAlignmentFactor = __constant_cpu_to_le32 (0);
23251 + resp->AFListOffset = __constant_cpu_to_le32 (0);
23252 + resp->AFListSize = __constant_cpu_to_le32 (0);
23254 + if (rndis_per_dev_params [configNr].ack)
23255 + rndis_per_dev_params [configNr].ack (
23256 + rndis_per_dev_params [configNr].dev);
23258 + return 0;
23261 +static int rndis_query_response (int configNr, rndis_query_msg_type *buf)
23263 + rndis_query_cmplt_type *resp;
23264 + rndis_resp_t *r;
23266 + // DEBUG("%s: OID = %08X\n", __FUNCTION__, cpu_to_le32(buf->OID));
23267 + if (!rndis_per_dev_params [configNr].dev) return -ENOTSUPP;
23269 + /*
23270 + * we need more memory:
23271 + * oid_supported_list is the largest answer
23272 + */
23273 + r = rndis_add_response (configNr, sizeof (oid_supported_list));
23275 + if (!r) return -ENOMEM;
23276 + resp = (rndis_query_cmplt_type *) r->buf;
23278 + if (!resp) return -ENOMEM;
23280 + resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_QUERY_CMPLT);
23281 + resp->MessageLength = __constant_cpu_to_le32 (24);
23282 + resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
23284 + if (gen_ndis_query_resp (configNr, cpu_to_le32 (buf->OID), r)) {
23285 + /* OID not supported */
23286 + resp->Status = __constant_cpu_to_le32 (
23287 + RNDIS_STATUS_NOT_SUPPORTED);
23288 + resp->InformationBufferLength = __constant_cpu_to_le32 (0);
23289 + resp->InformationBufferOffset = __constant_cpu_to_le32 (0);
23290 + } else
23291 + resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
23293 + if (rndis_per_dev_params [configNr].ack)
23294 + rndis_per_dev_params [configNr].ack (
23295 + rndis_per_dev_params [configNr].dev);
23296 + return 0;
23299 +static int rndis_set_response (int configNr, rndis_set_msg_type *buf)
23301 + u32 BufLength, BufOffset;
23302 + rndis_set_cmplt_type *resp;
23303 + rndis_resp_t *r;
23305 + r = rndis_add_response (configNr, sizeof (rndis_set_cmplt_type));
23307 + if (!r) return -ENOMEM;
23308 + resp = (rndis_set_cmplt_type *) r->buf;
23309 + if (!resp) return -ENOMEM;
23311 + BufLength = cpu_to_le32 (buf->InformationBufferLength);
23312 + BufOffset = cpu_to_le32 (buf->InformationBufferOffset);
23314 +#ifdef VERBOSE
23315 + DEBUG("%s: Length: %d\n", __FUNCTION__, BufLength);
23316 + DEBUG("%s: Offset: %d\n", __FUNCTION__, BufOffset);
23317 + DEBUG("%s: InfoBuffer: ", __FUNCTION__);
23319 + for (i = 0; i < BufLength; i++) {
23320 + DEBUG ("%02x ", *(((u8 *) buf) + i + 8 + BufOffset));
23323 + DEBUG ("\n");
23324 +#endif
23326 + resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_SET_CMPLT);
23327 + resp->MessageLength = __constant_cpu_to_le32 (16);
23328 + resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
23329 + if (gen_ndis_set_resp (configNr, cpu_to_le32 (buf->OID),
23330 + ((u8 *) buf) + 8 + BufOffset, BufLength, r))
23331 + resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_NOT_SUPPORTED);
23332 + else resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
23334 + if (rndis_per_dev_params [configNr].ack)
23335 + rndis_per_dev_params [configNr].ack (
23336 + rndis_per_dev_params [configNr].dev);
23338 + return 0;
23341 +static int rndis_reset_response (int configNr, rndis_reset_msg_type *buf)
23343 + rndis_reset_cmplt_type *resp;
23344 + rndis_resp_t *r;
23346 + r = rndis_add_response (configNr, sizeof (rndis_reset_cmplt_type));
23348 + if (!r) return -ENOMEM;
23349 + resp = (rndis_reset_cmplt_type *) r->buf;
23350 + if (!resp) return -ENOMEM;
23352 + resp->MessageType = __constant_cpu_to_le32 (REMOTE_NDIS_RESET_CMPLT);
23353 + resp->MessageLength = __constant_cpu_to_le32 (16);
23354 + resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
23355 + /* resent information */
23356 + resp->AddressingReset = __constant_cpu_to_le32 (1);
23358 + if (rndis_per_dev_params [configNr].ack)
23359 + rndis_per_dev_params [configNr].ack (
23360 + rndis_per_dev_params [configNr].dev);
23362 + return 0;
23365 +static int rndis_keepalive_response (int configNr,
23366 + rndis_keepalive_msg_type *buf)
23368 + rndis_keepalive_cmplt_type *resp;
23369 + rndis_resp_t *r;
23371 + /* host "should" check only in RNDIS_DATA_INITIALIZED state */
23373 + r = rndis_add_response (configNr, sizeof (rndis_keepalive_cmplt_type));
23374 + resp = (rndis_keepalive_cmplt_type *) r->buf;
23375 + if (!resp) return -ENOMEM;
23377 + resp->MessageType = __constant_cpu_to_le32 (
23378 + REMOTE_NDIS_KEEPALIVE_CMPLT);
23379 + resp->MessageLength = __constant_cpu_to_le32 (16);
23380 + resp->RequestID = buf->RequestID; /* Still LE in msg buffer */
23381 + resp->Status = __constant_cpu_to_le32 (RNDIS_STATUS_SUCCESS);
23383 + if (rndis_per_dev_params [configNr].ack)
23384 + rndis_per_dev_params [configNr].ack (
23385 + rndis_per_dev_params [configNr].dev);
23387 + return 0;
23391 +/*
23392 + * Device to Host Comunication
23393 + */
23394 +static int rndis_indicate_status_msg (int configNr, u32 status)
23396 + rndis_indicate_status_msg_type *resp;
23397 + rndis_resp_t *r;
23399 + if (rndis_per_dev_params [configNr].state == RNDIS_UNINITIALIZED)
23400 + return -ENOTSUPP;
23402 + r = rndis_add_response (configNr,
23403 + sizeof (rndis_indicate_status_msg_type));
23404 + if (!r) return -ENOMEM;
23406 + resp = (rndis_indicate_status_msg_type *) r->buf;
23407 + if (!resp) return -ENOMEM;
23409 + resp->MessageType = __constant_cpu_to_le32 (
23410 + REMOTE_NDIS_INDICATE_STATUS_MSG);
23411 + resp->MessageLength = __constant_cpu_to_le32 (20);
23412 + resp->Status = cpu_to_le32 (status);
23413 + resp->StatusBufferLength = __constant_cpu_to_le32 (0);
23414 + resp->StatusBufferOffset = __constant_cpu_to_le32 (0);
23416 + if (rndis_per_dev_params [configNr].ack)
23417 + rndis_per_dev_params [configNr].ack (
23418 + rndis_per_dev_params [configNr].dev);
23419 + return 0;
23422 +int rndis_signal_connect (int configNr)
23424 + rndis_per_dev_params [configNr].media_state
23425 + = NDIS_MEDIA_STATE_CONNECTED;
23426 + return rndis_indicate_status_msg (configNr,
23427 + RNDIS_STATUS_MEDIA_CONNECT);
23430 +int rndis_signal_disconnect (int configNr)
23432 + rndis_per_dev_params [configNr].media_state
23433 + = NDIS_MEDIA_STATE_DISCONNECTED;
23434 + return rndis_indicate_status_msg (configNr,
23435 + RNDIS_STATUS_MEDIA_DISCONNECT);
23438 +void rndis_set_host_mac (int configNr, const u8 *addr)
23440 + rndis_per_dev_params [configNr].host_mac = addr;
23443 +/*
23444 + * Message Parser
23445 + */
23446 +int rndis_msg_parser (u8 configNr, u8 *buf)
23448 + u32 MsgType, MsgLength, *tmp;
23449 + struct rndis_params *params;
23451 + if (!buf)
23452 + return -ENOMEM;
23454 + tmp = (u32 *) buf;
23455 + MsgType = cpu_to_le32p(tmp++);
23456 + MsgLength = cpu_to_le32p(tmp++);
23458 + if (configNr >= RNDIS_MAX_CONFIGS)
23459 + return -ENOTSUPP;
23460 + params = &rndis_per_dev_params [configNr];
23462 + /* For USB: responses may take up to 10 seconds */
23463 + switch (MsgType)
23465 + case REMOTE_NDIS_INITIALIZE_MSG:
23466 + DEBUG("%s: REMOTE_NDIS_INITIALIZE_MSG\n",
23467 + __FUNCTION__ );
23468 + params->state = RNDIS_INITIALIZED;
23469 + return rndis_init_response (configNr,
23470 + (rndis_init_msg_type *) buf);
23472 + case REMOTE_NDIS_HALT_MSG:
23473 + DEBUG("%s: REMOTE_NDIS_HALT_MSG\n",
23474 + __FUNCTION__ );
23475 + params->state = RNDIS_UNINITIALIZED;
23476 + if (params->dev) {
23477 + netif_carrier_off (params->dev);
23478 + netif_stop_queue (params->dev);
23480 + return 0;
23482 + case REMOTE_NDIS_QUERY_MSG:
23483 + return rndis_query_response (configNr,
23484 + (rndis_query_msg_type *) buf);
23486 + case REMOTE_NDIS_SET_MSG:
23487 + return rndis_set_response (configNr,
23488 + (rndis_set_msg_type *) buf);
23490 + case REMOTE_NDIS_RESET_MSG:
23491 + DEBUG("%s: REMOTE_NDIS_RESET_MSG\n",
23492 + __FUNCTION__ );
23493 + return rndis_reset_response (configNr,
23494 + (rndis_reset_msg_type *) buf);
23496 + case REMOTE_NDIS_KEEPALIVE_MSG:
23497 + /* For USB: host does this every 5 seconds */
23498 +#ifdef VERBOSE
23499 + DEBUG("%s: REMOTE_NDIS_KEEPALIVE_MSG\n",
23500 + __FUNCTION__ );
23501 +#endif
23502 + return rndis_keepalive_response (configNr,
23503 + (rndis_keepalive_msg_type *)
23504 + buf);
23506 + default:
23507 + /* At least Windows XP emits some undefined RNDIS messages.
23508 + * In one case those messages seemed to relate to the host
23509 + * suspending itself.
23510 + */
23511 + printk (KERN_WARNING
23512 + "%s: unknown RNDIS message 0x%08X len %d\n",
23513 + __FUNCTION__ , MsgType, MsgLength);
23515 + unsigned i;
23516 + for (i = 0; i < MsgLength; i += 16) {
23517 + DEBUG ("%03d: "
23518 + " %02x %02x %02x %02x"
23519 + " %02x %02x %02x %02x"
23520 + " %02x %02x %02x %02x"
23521 + " %02x %02x %02x %02x"
23522 + "\n",
23523 + i,
23524 + buf[i], buf [i+1],
23525 + buf[i+2], buf[i+3],
23526 + buf[i+4], buf [i+5],
23527 + buf[i+6], buf[i+7],
23528 + buf[i+8], buf [i+9],
23529 + buf[i+10], buf[i+11],
23530 + buf[i+12], buf [i+13],
23531 + buf[i+14], buf[i+15]);
23534 + break;
23537 + return -ENOTSUPP;
23540 +int rndis_register (int (* rndis_control_ack) (struct net_device *))
23542 + u8 i;
23544 + for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
23545 + if (!rndis_per_dev_params [i].used) {
23546 + rndis_per_dev_params [i].used = 1;
23547 + rndis_per_dev_params [i].ack = rndis_control_ack;
23548 + DEBUG("%s: configNr = %d\n", __FUNCTION__, i);
23549 + return i;
23552 + DEBUG("failed\n");
23554 + return -1;
23557 +void rndis_deregister (int configNr)
23559 + DEBUG("%s: \n", __FUNCTION__ );
23561 + if (configNr >= RNDIS_MAX_CONFIGS) return;
23562 + rndis_per_dev_params [configNr].used = 0;
23564 + return;
23567 +int rndis_set_param_dev (u8 configNr, struct net_device *dev,
23568 + struct net_device_stats *stats)
23570 + DEBUG("%s:\n", __FUNCTION__ );
23571 + if (!dev || !stats) return -1;
23572 + if (configNr >= RNDIS_MAX_CONFIGS) return -1;
23574 + rndis_per_dev_params [configNr].dev = dev;
23575 + rndis_per_dev_params [configNr].stats = stats;
23577 + return 0;
23580 +int rndis_set_param_vendor (u8 configNr, u32 vendorID, const char *vendorDescr)
23582 + DEBUG("%s:\n", __FUNCTION__ );
23583 + if (!vendorDescr) return -1;
23584 + if (configNr >= RNDIS_MAX_CONFIGS) return -1;
23586 + rndis_per_dev_params [configNr].vendorID = vendorID;
23587 + rndis_per_dev_params [configNr].vendorDescr = vendorDescr;
23589 + return 0;
23592 +int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed)
23594 + DEBUG("%s:\n", __FUNCTION__ );
23595 + if (configNr >= RNDIS_MAX_CONFIGS) return -1;
23597 + rndis_per_dev_params [configNr].medium = medium;
23598 + rndis_per_dev_params [configNr].speed = speed;
23600 + return 0;
23603 +void rndis_add_hdr (struct sk_buff *skb)
23605 + if (!skb) return;
23606 + skb_push (skb, sizeof (struct rndis_packet_msg_type));
23607 + memset (skb->data, 0, sizeof (struct rndis_packet_msg_type));
23608 + *((u32 *) skb->data) = __constant_cpu_to_le32 (1);
23609 + *((u32 *) skb->data + 1) = cpu_to_le32(skb->len);
23610 + *((u32 *) skb->data + 2) = __constant_cpu_to_le32 (36);
23611 + *((u32 *) skb->data + 3) = cpu_to_le32(skb->len - 44);
23613 + return;
23616 +void rndis_free_response (int configNr, u8 *buf)
23618 + rndis_resp_t *r;
23619 + struct list_head *act, *tmp;
23621 + list_for_each_safe (act, tmp,
23622 + &(rndis_per_dev_params [configNr].resp_queue))
23624 + r = list_entry (act, rndis_resp_t, list);
23625 + if (r && r->buf == buf) {
23626 + list_del (&r->list);
23627 + kfree (r);
23632 +u8 *rndis_get_next_response (int configNr, u32 *length)
23634 + rndis_resp_t *r;
23635 + struct list_head *act, *tmp;
23637 + if (!length) return NULL;
23639 + list_for_each_safe (act, tmp,
23640 + &(rndis_per_dev_params [configNr].resp_queue))
23642 + r = list_entry (act, rndis_resp_t, list);
23643 + if (!r->send) {
23644 + r->send = 1;
23645 + *length = r->length;
23646 + return r->buf;
23650 + return NULL;
23653 +static rndis_resp_t *rndis_add_response (int configNr, u32 length)
23655 + rndis_resp_t *r;
23657 + r = kmalloc (sizeof (rndis_resp_t) + length, GFP_ATOMIC);
23658 + if (!r) return NULL;
23660 + r->buf = (u8 *) (r + 1);
23661 + r->length = length;
23662 + r->send = 0;
23664 + list_add_tail (&r->list,
23665 + &(rndis_per_dev_params [configNr].resp_queue));
23666 + return r;
23669 +int rndis_rm_hdr (u8 *buf, u32 *length)
23671 + u32 i, messageLen, dataOffset, *tmp;
23673 + tmp = (u32 *) buf;
23675 + if (!buf || !length) return -1;
23676 + if (cpu_to_le32p(tmp++) != 1) return -1;
23678 + messageLen = cpu_to_le32p(tmp++);
23679 + dataOffset = cpu_to_le32p(tmp++) + 8;
23681 + if (messageLen < dataOffset || messageLen > *length) return -1;
23683 + for (i = dataOffset; i < messageLen; i++)
23684 + buf [i - dataOffset] = buf [i];
23686 + *length = messageLen - dataOffset;
23688 + return 0;
23691 +#ifdef CONFIG_USB_GADGET_DEBUG_FILES
23693 +static int rndis_proc_read (char *page, char **start, off_t off, int count, int *eof,
23694 + void *data)
23696 + char *out = page;
23697 + int len;
23698 + rndis_params *param = (rndis_params *) data;
23700 + out += snprintf (out, count,
23701 + "Config Nr. %d\n"
23702 + "used : %s\n"
23703 + "state : %s\n"
23704 + "medium : 0x%08X\n"
23705 + "speed : %d\n"
23706 + "cable : %s\n"
23707 + "vendor ID : 0x%08X\n"
23708 + "vendor : %s\n",
23709 + param->confignr, (param->used) ? "y" : "n",
23710 + ({ char *s = "?";
23711 + switch (param->state) {
23712 + case RNDIS_UNINITIALIZED:
23713 + s = "RNDIS_UNINITIALIZED"; break;
23714 + case RNDIS_INITIALIZED:
23715 + s = "RNDIS_INITIALIZED"; break;
23716 + case RNDIS_DATA_INITIALIZED:
23717 + s = "RNDIS_DATA_INITIALIZED"; break;
23718 + }; s; }),
23719 + param->medium,
23720 + (param->media_state) ? 0 : param->speed*100,
23721 + (param->media_state) ? "disconnected" : "connected",
23722 + param->vendorID, param->vendorDescr);
23724 + len = out - page;
23725 + len -= off;
23727 + if (len < count) {
23728 + *eof = 1;
23729 + if (len <= 0)
23730 + return 0;
23731 + } else
23732 + len = count;
23734 + *start = page + off;
23735 + return len;
23738 +static int rndis_proc_write (struct file *file, const char __user *buffer,
23739 + unsigned long count, void *data)
23741 + rndis_params *p = data;
23742 + u32 speed = 0;
23743 + int i, fl_speed = 0;
23745 + for (i = 0; i < count; i++) {
23746 + char c;
23747 + if (get_user(c, buffer))
23748 + return -EFAULT;
23749 + switch (c) {
23750 + case '0':
23751 + case '1':
23752 + case '2':
23753 + case '3':
23754 + case '4':
23755 + case '5':
23756 + case '6':
23757 + case '7':
23758 + case '8':
23759 + case '9':
23760 + fl_speed = 1;
23761 + speed = speed*10 + c - '0';
23762 + break;
23763 + case 'C':
23764 + case 'c':
23765 + rndis_signal_connect (p->confignr);
23766 + break;
23767 + case 'D':
23768 + case 'd':
23769 + rndis_signal_disconnect(p->confignr);
23770 + break;
23771 + default:
23772 + if (fl_speed) p->speed = speed;
23773 + else DEBUG ("%c is not valid\n", c);
23774 + break;
23777 + buffer++;
23780 + return count;
23783 +#define NAME_TEMPLATE "driver/rndis-%03d"
23785 +static struct proc_dir_entry *rndis_connect_state [RNDIS_MAX_CONFIGS];
23787 +#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
23790 +int __init rndis_init (void)
23792 + u8 i;
23794 + for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
23795 +#ifdef CONFIG_USB_GADGET_DEBUG_FILES
23796 + char name [20];
23798 + sprintf (name, NAME_TEMPLATE, i);
23799 + if (!(rndis_connect_state [i]
23800 + = create_proc_entry (name, 0660, NULL)))
23802 + DEBUG ("%s :remove entries", __FUNCTION__);
23803 + while (i) {
23804 + sprintf (name, NAME_TEMPLATE, --i);
23805 + remove_proc_entry (name, NULL);
23807 + DEBUG ("\n");
23808 + return -EIO;
23811 + rndis_connect_state [i]->nlink = 1;
23812 + rndis_connect_state [i]->write_proc = rndis_proc_write;
23813 + rndis_connect_state [i]->read_proc = rndis_proc_read;
23814 + rndis_connect_state [i]->data = (void *)
23815 + (rndis_per_dev_params + i);
23816 +#endif
23817 + rndis_per_dev_params [i].confignr = i;
23818 + rndis_per_dev_params [i].used = 0;
23819 + rndis_per_dev_params [i].state = RNDIS_UNINITIALIZED;
23820 + rndis_per_dev_params [i].media_state
23821 + = NDIS_MEDIA_STATE_DISCONNECTED;
23822 + INIT_LIST_HEAD (&(rndis_per_dev_params [i].resp_queue));
23825 + return 0;
23828 +void rndis_exit (void)
23830 +#ifdef CONFIG_USB_GADGET_DEBUG_FILES
23831 + u8 i;
23832 + char name [20];
23834 + for (i = 0; i < RNDIS_MAX_CONFIGS; i++) {
23835 + sprintf (name, NAME_TEMPLATE, i);
23836 + remove_proc_entry (name, NULL);
23838 +#endif
23841 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/rndis.h kernel/drivers/usb/gadget/rndis.h
23842 --- /tmp/kernel/drivers/usb/gadget/rndis.h 1970-01-01 01:00:00.000000000 +0100
23843 +++ kernel/drivers/usb/gadget/rndis.h 2005-04-22 17:53:19.504528119 +0200
23844 @@ -0,0 +1,348 @@
23845 +/*
23846 + * RNDIS Definitions for Remote NDIS
23847 + *
23848 + * Version: $Id: rndis.h,v 1.15 2004/03/25 21:33:46 robert Exp $
23849 + *
23850 + * Authors: Benedikt Spranger, Pengutronix
23851 + * Robert Schwebel, Pengutronix
23852 + *
23853 + * This program is free software; you can redistribute it and/or
23854 + * modify it under the terms of the GNU General Public License
23855 + * version 2, as published by the Free Software Foundation.
23856 + *
23857 + * This software was originally developed in conformance with
23858 + * Microsoft's Remote NDIS Specification License Agreement.
23859 + */
23861 +#ifndef _LINUX_RNDIS_H
23862 +#define _LINUX_RNDIS_H
23864 +#include "ndis.h"
23866 +#define RNDIS_MAXIMUM_FRAME_SIZE 1518
23867 +#define RNDIS_MAX_TOTAL_SIZE 1558
23869 +/* Remote NDIS Versions */
23870 +#define RNDIS_MAJOR_VERSION 1
23871 +#define RNDIS_MINOR_VERSION 0
23873 +/* Status Values */
23874 +#define RNDIS_STATUS_SUCCESS 0x00000000U /* Success */
23875 +#define RNDIS_STATUS_FAILURE 0xC0000001U /* Unspecified error */
23876 +#define RNDIS_STATUS_INVALID_DATA 0xC0010015U /* Invalid data */
23877 +#define RNDIS_STATUS_NOT_SUPPORTED 0xC00000BBU /* Unsupported request */
23878 +#define RNDIS_STATUS_MEDIA_CONNECT 0x4001000BU /* Device connected */
23879 +#define RNDIS_STATUS_MEDIA_DISCONNECT 0x4001000CU /* Device disconnected */
23880 +/* For all not specified status messages:
23881 + * RNDIS_STATUS_Xxx -> NDIS_STATUS_Xxx
23882 + */
23884 +/* Message Set for Connectionless (802.3) Devices */
23885 +#define REMOTE_NDIS_INITIALIZE_MSG 0x00000002U /* Initialize device */
23886 +#define REMOTE_NDIS_HALT_MSG 0x00000003U
23887 +#define REMOTE_NDIS_QUERY_MSG 0x00000004U
23888 +#define REMOTE_NDIS_SET_MSG 0x00000005U
23889 +#define REMOTE_NDIS_RESET_MSG 0x00000006U
23890 +#define REMOTE_NDIS_INDICATE_STATUS_MSG 0x00000007U
23891 +#define REMOTE_NDIS_KEEPALIVE_MSG 0x00000008U
23893 +/* Message completion */
23894 +#define REMOTE_NDIS_INITIALIZE_CMPLT 0x80000002U
23895 +#define REMOTE_NDIS_QUERY_CMPLT 0x80000004U
23896 +#define REMOTE_NDIS_SET_CMPLT 0x80000005U
23897 +#define REMOTE_NDIS_RESET_CMPLT 0x80000006U
23898 +#define REMOTE_NDIS_KEEPALIVE_CMPLT 0x80000008U
23900 +/* Device Flags */
23901 +#define RNDIS_DF_CONNECTIONLESS 0x00000001U
23902 +#define RNDIS_DF_CONNECTION_ORIENTED 0x00000002U
23904 +#define RNDIS_MEDIUM_802_3 0x00000000U
23906 +/* from drivers/net/sk98lin/h/skgepnmi.h */
23907 +#define OID_PNP_CAPABILITIES 0xFD010100
23908 +#define OID_PNP_SET_POWER 0xFD010101
23909 +#define OID_PNP_QUERY_POWER 0xFD010102
23910 +#define OID_PNP_ADD_WAKE_UP_PATTERN 0xFD010103
23911 +#define OID_PNP_REMOVE_WAKE_UP_PATTERN 0xFD010104
23912 +#define OID_PNP_ENABLE_WAKE_UP 0xFD010106
23915 +/* supported OIDs */
23916 +static const u32 oid_supported_list [] =
23918 + /* the general stuff */
23919 + OID_GEN_SUPPORTED_LIST,
23920 + OID_GEN_HARDWARE_STATUS,
23921 + OID_GEN_MEDIA_SUPPORTED,
23922 + OID_GEN_MEDIA_IN_USE,
23923 + OID_GEN_MAXIMUM_FRAME_SIZE,
23924 + OID_GEN_LINK_SPEED,
23925 + OID_GEN_TRANSMIT_BLOCK_SIZE,
23926 + OID_GEN_RECEIVE_BLOCK_SIZE,
23927 + OID_GEN_VENDOR_ID,
23928 + OID_GEN_VENDOR_DESCRIPTION,
23929 + OID_GEN_VENDOR_DRIVER_VERSION,
23930 + OID_GEN_CURRENT_PACKET_FILTER,
23931 + OID_GEN_MAXIMUM_TOTAL_SIZE,
23932 + OID_GEN_MEDIA_CONNECT_STATUS,
23933 + OID_GEN_PHYSICAL_MEDIUM,
23934 +#if 0
23935 + OID_GEN_RNDIS_CONFIG_PARAMETER,
23936 +#endif
23938 + /* the statistical stuff */
23939 + OID_GEN_XMIT_OK,
23940 + OID_GEN_RCV_OK,
23941 + OID_GEN_XMIT_ERROR,
23942 + OID_GEN_RCV_ERROR,
23943 + OID_GEN_RCV_NO_BUFFER,
23944 +#ifdef RNDIS_OPTIONAL_STATS
23945 + OID_GEN_DIRECTED_BYTES_XMIT,
23946 + OID_GEN_DIRECTED_FRAMES_XMIT,
23947 + OID_GEN_MULTICAST_BYTES_XMIT,
23948 + OID_GEN_MULTICAST_FRAMES_XMIT,
23949 + OID_GEN_BROADCAST_BYTES_XMIT,
23950 + OID_GEN_BROADCAST_FRAMES_XMIT,
23951 + OID_GEN_DIRECTED_BYTES_RCV,
23952 + OID_GEN_DIRECTED_FRAMES_RCV,
23953 + OID_GEN_MULTICAST_BYTES_RCV,
23954 + OID_GEN_MULTICAST_FRAMES_RCV,
23955 + OID_GEN_BROADCAST_BYTES_RCV,
23956 + OID_GEN_BROADCAST_FRAMES_RCV,
23957 + OID_GEN_RCV_CRC_ERROR,
23958 + OID_GEN_TRANSMIT_QUEUE_LENGTH,
23959 +#endif /* RNDIS_OPTIONAL_STATS */
23961 + /* mandatory 802.3 */
23962 + /* the general stuff */
23963 + OID_802_3_PERMANENT_ADDRESS,
23964 + OID_802_3_CURRENT_ADDRESS,
23965 + OID_802_3_MULTICAST_LIST,
23966 + OID_802_3_MAC_OPTIONS,
23967 + OID_802_3_MAXIMUM_LIST_SIZE,
23969 + /* the statistical stuff */
23970 + OID_802_3_RCV_ERROR_ALIGNMENT,
23971 + OID_802_3_XMIT_ONE_COLLISION,
23972 + OID_802_3_XMIT_MORE_COLLISIONS,
23973 +#ifdef RNDIS_OPTIONAL_STATS
23974 + OID_802_3_XMIT_DEFERRED,
23975 + OID_802_3_XMIT_MAX_COLLISIONS,
23976 + OID_802_3_RCV_OVERRUN,
23977 + OID_802_3_XMIT_UNDERRUN,
23978 + OID_802_3_XMIT_HEARTBEAT_FAILURE,
23979 + OID_802_3_XMIT_TIMES_CRS_LOST,
23980 + OID_802_3_XMIT_LATE_COLLISIONS,
23981 +#endif /* RNDIS_OPTIONAL_STATS */
23983 +#ifdef RNDIS_PM
23984 + /* PM and wakeup are mandatory for USB: */
23986 + /* power management */
23987 + OID_PNP_CAPABILITIES,
23988 + OID_PNP_QUERY_POWER,
23989 + OID_PNP_SET_POWER,
23991 + /* wake up host */
23992 + OID_PNP_ENABLE_WAKE_UP,
23993 + OID_PNP_ADD_WAKE_UP_PATTERN,
23994 + OID_PNP_REMOVE_WAKE_UP_PATTERN,
23995 +#endif
23999 +typedef struct rndis_init_msg_type
24001 + u32 MessageType;
24002 + u32 MessageLength;
24003 + u32 RequestID;
24004 + u32 MajorVersion;
24005 + u32 MinorVersion;
24006 + u32 MaxTransferSize;
24007 +} rndis_init_msg_type;
24009 +typedef struct rndis_init_cmplt_type
24011 + u32 MessageType;
24012 + u32 MessageLength;
24013 + u32 RequestID;
24014 + u32 Status;
24015 + u32 MajorVersion;
24016 + u32 MinorVersion;
24017 + u32 DeviceFlags;
24018 + u32 Medium;
24019 + u32 MaxPacketsPerTransfer;
24020 + u32 MaxTransferSize;
24021 + u32 PacketAlignmentFactor;
24022 + u32 AFListOffset;
24023 + u32 AFListSize;
24024 +} rndis_init_cmplt_type;
24026 +typedef struct rndis_halt_msg_type
24028 + u32 MessageType;
24029 + u32 MessageLength;
24030 + u32 RequestID;
24031 +} rndis_halt_msg_type;
24033 +typedef struct rndis_query_msg_type
24035 + u32 MessageType;
24036 + u32 MessageLength;
24037 + u32 RequestID;
24038 + u32 OID;
24039 + u32 InformationBufferLength;
24040 + u32 InformationBufferOffset;
24041 + u32 DeviceVcHandle;
24042 +} rndis_query_msg_type;
24044 +typedef struct rndis_query_cmplt_type
24046 + u32 MessageType;
24047 + u32 MessageLength;
24048 + u32 RequestID;
24049 + u32 Status;
24050 + u32 InformationBufferLength;
24051 + u32 InformationBufferOffset;
24052 +} rndis_query_cmplt_type;
24054 +typedef struct rndis_set_msg_type
24056 + u32 MessageType;
24057 + u32 MessageLength;
24058 + u32 RequestID;
24059 + u32 OID;
24060 + u32 InformationBufferLength;
24061 + u32 InformationBufferOffset;
24062 + u32 DeviceVcHandle;
24063 +} rndis_set_msg_type;
24065 +typedef struct rndis_set_cmplt_type
24067 + u32 MessageType;
24068 + u32 MessageLength;
24069 + u32 RequestID;
24070 + u32 Status;
24071 +} rndis_set_cmplt_type;
24073 +typedef struct rndis_reset_msg_type
24075 + u32 MessageType;
24076 + u32 MessageLength;
24077 + u32 Reserved;
24078 +} rndis_reset_msg_type;
24080 +typedef struct rndis_reset_cmplt_type
24082 + u32 MessageType;
24083 + u32 MessageLength;
24084 + u32 Status;
24085 + u32 AddressingReset;
24086 +} rndis_reset_cmplt_type;
24088 +typedef struct rndis_indicate_status_msg_type
24090 + u32 MessageType;
24091 + u32 MessageLength;
24092 + u32 Status;
24093 + u32 StatusBufferLength;
24094 + u32 StatusBufferOffset;
24095 +} rndis_indicate_status_msg_type;
24097 +typedef struct rndis_keepalive_msg_type
24099 + u32 MessageType;
24100 + u32 MessageLength;
24101 + u32 RequestID;
24102 +} rndis_keepalive_msg_type;
24104 +typedef struct rndis_keepalive_cmplt_type
24106 + u32 MessageType;
24107 + u32 MessageLength;
24108 + u32 RequestID;
24109 + u32 Status;
24110 +} rndis_keepalive_cmplt_type;
24112 +struct rndis_packet_msg_type
24114 + u32 MessageType;
24115 + u32 MessageLength;
24116 + u32 DataOffset;
24117 + u32 DataLength;
24118 + u32 OOBDataOffset;
24119 + u32 OOBDataLength;
24120 + u32 NumOOBDataElements;
24121 + u32 PerPacketInfoOffset;
24122 + u32 PerPacketInfoLength;
24123 + u32 VcHandle;
24124 + u32 Reserved;
24127 +struct rndis_config_parameter
24129 + u32 ParameterNameOffset;
24130 + u32 ParameterNameLength;
24131 + u32 ParameterType;
24132 + u32 ParameterValueOffset;
24133 + u32 ParameterValueLength;
24136 +/* implementation specific */
24137 +enum rndis_state
24139 + RNDIS_UNINITIALIZED,
24140 + RNDIS_INITIALIZED,
24141 + RNDIS_DATA_INITIALIZED,
24144 +typedef struct rndis_resp_t
24146 + struct list_head list;
24147 + u8 *buf;
24148 + u32 length;
24149 + int send;
24150 +} rndis_resp_t;
24152 +typedef struct rndis_params
24154 + u8 confignr;
24155 + int used;
24156 + enum rndis_state state;
24157 + u32 filter;
24158 + u32 medium;
24159 + u32 speed;
24160 + u32 media_state;
24161 + const u8 *host_mac;
24162 + struct net_device *dev;
24163 + struct net_device_stats *stats;
24164 + u32 vendorID;
24165 + const char *vendorDescr;
24166 + int (*ack) (struct net_device *);
24167 + struct list_head resp_queue;
24168 +} rndis_params;
24170 +/* RNDIS Message parser and other useless functions */
24171 +int rndis_msg_parser (u8 configNr, u8 *buf);
24172 +int rndis_register (int (*rndis_control_ack) (struct net_device *));
24173 +void rndis_deregister (int configNr);
24174 +int rndis_set_param_dev (u8 configNr, struct net_device *dev,
24175 + struct net_device_stats *stats);
24176 +int rndis_set_param_vendor (u8 configNr, u32 vendorID,
24177 + const char *vendorDescr);
24178 +int rndis_set_param_medium (u8 configNr, u32 medium, u32 speed);
24179 +void rndis_add_hdr (struct sk_buff *skb);
24180 +int rndis_rm_hdr (u8 *buf, u32 *length);
24181 +u8 *rndis_get_next_response (int configNr, u32 *length);
24182 +void rndis_free_response (int configNr, u8 *buf);
24184 +int rndis_signal_connect (int configNr);
24185 +int rndis_signal_disconnect (int configNr);
24186 +int rndis_state (int configNr);
24187 +extern void rndis_set_host_mac (int configNr, const u8 *addr);
24189 +int __init rndis_init (void);
24190 +void rndis_exit (void);
24192 +#endif /* _LINUX_RNDIS_H */
24193 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/superh_udc.c kernel/drivers/usb/gadget/superh_udc.c
24194 --- /tmp/kernel/drivers/usb/gadget/superh_udc.c 1970-01-01 01:00:00.000000000 +0100
24195 +++ kernel/drivers/usb/gadget/superh_udc.c 2005-04-22 17:53:19.510527142 +0200
24196 @@ -0,0 +1,1819 @@
24198 + * Renesas SuperH USB 1.1 device controller (found on SH7705, SH7727...)
24200 + * Copyright (C) 2003 Renesas Technology Europe Limited
24201 + * Copyright (C) 2003 Julian Back (jback@mpc-data.co.uk), MPC Data Limited
24203 + * This program is free software; you can redistribute it and/or modify
24204 + * it under the terms of the GNU General Public License as published by
24205 + * the Free Software Foundation; either version 2 of the License, or
24206 + * (at your option) any later version.
24208 + * This program is distributed in the hope that it will be useful,
24209 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24210 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24211 + * GNU General Public License for more details.
24213 + * You should have received a copy of the GNU General Public License
24214 + * along with this program; if not, write to the Free Software
24215 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24216 + */
24219 + * This is a driver for the USB Device Controller found on Renesas SH
24220 + * processors. This is a full-speed controller which has four
24221 + * endpoints in a single fixed configuration.
24223 + * Limitations
24225 + * Only tested on SH7705. Mostly tested with Mass Storage gadget
24226 + * using Bulk-Only Transport. It has been tested with Linux 2.4,
24227 + * Linux 2.6, Windows 2000 and Windows XP hosts.
24228 + *
24229 + * DMA is not (yet) implemented.
24231 + * Handling of application stalls is tricky. We set a bit to stall an
24232 + * endpoint. When the host tries to access the ep it gets a stall and
24233 + * another stall bit is latched by the device. The host clears the
24234 + * stall with a clear feature but the hardware doesn't inform us, the
24235 + * latched bit is cleared but not the bit we have set, so the next
24236 + * time the host accesses the ep it will get another stall and the
24237 + * latch will be set again unless we have cleared our stall bit. The
24238 + * solution adopted in this driver is to use a timer to clear the
24239 + * application stall bit some time after setting the stall. This
24240 + * seems to work most of the time but is not 100% reliable. Because
24241 + * of this it is best to avoid USB protocols that require the USB
24242 + * device to stall the host. Unfortunately USB mass storage does
24243 + * require the device to stall when it gets unsupported commands,
24244 + * Linux hosts don't send any of these unsupported commands but
24245 + * Windows hosts do.
24247 + * Another place where the hardware is too clever is in the handling
24248 + * of setup packets. Many setup packets including SET_INTERFACE and
24249 + * SET_CONFIGURATION are handled by the hardware without informing the
24250 + * driver software. But we need to inform the gadget driver of at
24251 + * least one of these as it uses this to kick of it's data processing.
24252 + * The solution adopted is that after we have recieved N setup packets
24253 + * following a bus reset a fake SET_CONFIGURATION is sent to the
24254 + * gadget. We also have to arrange things so that the reply to the
24255 + * fake packet is not sent out.
24257 + */
24259 +#include <linux/config.h>
24260 +#include <linux/module.h>
24261 +#include <linux/kernel.h>
24262 +#include <linux/ioport.h>
24263 +#include <linux/types.h>
24264 +#include <linux/version.h>
24265 +#include <linux/errno.h>
24266 +#include <linux/delay.h>
24267 +#include <linux/sched.h>
24268 +#include <linux/slab.h>
24269 +#include <linux/init.h>
24270 +#include <linux/timer.h>
24271 +#include <linux/list.h>
24272 +#include <linux/interrupt.h>
24273 +#include <linux/proc_fs.h>
24274 +#include <linux/mm.h>
24276 +#include <asm/atomic.h>
24277 +#include <asm/byteorder.h>
24278 +#include <asm/dma.h>
24279 +#include <asm/io.h>
24280 +#include <asm/irq.h>
24281 +#include <asm/system.h>
24282 +#include <asm/unaligned.h>
24284 +#include <linux/usb_ch9.h>
24285 +#include <linux/usb_gadget.h>
24287 +#undef DEBUG
24288 +#undef VERY_NOISY
24290 +#define DRIVER_DESC "SuperH USB Peripheral Controller"
24291 +#define DRIVER_VERSION "alpha (11 November 2003)"
24293 +#ifdef USE_DMA
24294 +#error "DMA not supported"
24295 +#endif
24297 +static const char driver_name [] = "superh_udc";
24298 +static const char driver_desc [] = DRIVER_DESC;
24300 +static const char ep0name [] = "ep0";
24301 +static const char *ep_name [] = {
24302 + ep0name,
24303 + "ep1out-bulk",
24304 + "ep2in-bulk",
24305 + "ep3in-bulk",
24308 +static struct superh_udc *the_controller;
24310 +#include "superh_udc.h"
24312 +/* High priority interrupts */
24313 +#define F0_HIGH (EP1_FULL | EP2_TR | EP2_EMPTY )
24314 +#define F1_HIGH (0)
24316 +/* Low priority interrupts */
24317 +#define F0_LOW (BRST | SETUP_TS | EP0o_TS | EP0i_TR | EP0i_TS)
24318 +#define F1_LOW (EP3_TR | EP3_TS | VBUSF)
24320 +/* How long to leave the stall bit set - this value is quite critical
24321 + * to making stalls work. Unfortunately it doesn't seem possible to
24322 + * get a value that will work reliably with both fast and slow
24323 + * machines.
24324 + */
24325 +#define STALL_TIME (HZ/75)
24327 +/* Number of endpoints to check in the unstall timer. It should not
24328 + * be necessary to unstall bulk endpoints using the timer as long as
24329 + * the gadget code is aware that this device cannot stall properly
24330 + * (see the file backed storage gadget for an example). But if the
24331 + * UDC driver stalls ep0 due to a bad SETUP then the timer is still
24332 + * required otherwise the stall will never get cleared. If it is
24333 + * necessary to unstall all endpoints using the timer then set this to
24334 + * 4.
24335 + */
24336 +#define EP_TO_UNSTALL 1
24338 +/* Number of packets to wait for before sending a fake
24339 + * SET_CONFIGURATION to the gadget driver
24340 + */
24341 +#define DEFAULT_SETUP_COUNT 7
24342 +#define RESET_SETUP_COUNT 2
24344 +/* How long to wait for the number of packets specified above */
24345 +#define SETUP_TIME (HZ/10 )
24347 +static void superh_ep_fifo_flush(struct usb_ep *_ep);
24348 +static void stop_activity(struct superh_udc *dev, struct usb_gadget_driver *driver);
24349 +static int superh_ep_set_halt(struct usb_ep *_ep, int value);
24350 +static void udc_timer(unsigned long _dev);
24351 +static struct superh_request* process_ep_req(struct superh_ep *ep,
24352 + struct superh_request *req);
24353 +static void done(struct superh_ep *ep, struct superh_request *req, int status);
24356 + * IO
24357 + */
24359 +static inline void and_b(u8 mask, unsigned long addr)
24361 + ctrl_outb(ctrl_inb(addr) & mask, addr);
24365 +static inline void or_b(u8 mask, unsigned long addr)
24367 + ctrl_outb(ctrl_inb(addr) | mask, addr);
24371 +static inline void ep0_idle (struct superh_udc *dev)
24373 + DBG(DBG_VERY_NOISY, "ep0_idle\n");
24374 + dev->ep0state = EP0_IDLE;
24378 +static void init_udc_timer(struct superh_udc *dev)
24380 + init_timer(&dev->timer);
24381 + dev->timer.function = udc_timer;
24382 + dev->timer.data = (unsigned long) dev;
24383 + dev->timer.expires = jiffies + STALL_TIME;
24384 + add_timer(&dev->timer);
24387 +/* Send a fake SET_CONFIGURATION to the gadget to start it up.
24388 + * Needed because the hardware doesn't let us know when the real packet
24389 + * has arrived.
24390 + */
24391 +static void send_fake_config(struct superh_udc *dev)
24393 + struct usb_ctrlrequest r;
24394 + dev->fake_config = 1;
24395 + dev->setup_countdown = 0;
24396 + r.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD
24397 + | USB_RECIP_DEVICE;
24398 + r.bRequest = USB_REQ_SET_CONFIGURATION;
24399 + r.wValue = 1; /* configuration to select */
24400 + r.wIndex = 0;
24401 + r.wLength = 0;
24402 + if (dev->driver->setup(&dev->gadget, &r) < 0) {
24403 + DMSG("SET_CONFIGURATION failed.\n");
24408 + * Timer function. Clears stall from any stalled endpoints as we
24409 + * don't get informed when the host has sent a clear feature.
24410 + */
24411 +static void udc_timer(unsigned long _dev)
24413 + struct superh_udc *dev = (void *)_dev;
24414 + int i;
24415 + unsigned long flags;
24417 + local_irq_save(flags);
24419 + if (atomic_read(&dev->in_interrupt) == 0) {
24421 + /* Check if a bus reset has been done and we haven't faked a SET_CONFIGURATION */
24422 + if (dev->gadget.speed != USB_SPEED_UNKNOWN
24423 + && dev->setup_countdown > 0
24424 + && jiffies - dev->reset_time > SETUP_TIME
24425 + &&list_empty(&dev->ep[0].queue)) {
24426 + send_fake_config(dev);
24429 + /* Check if any end points are halted and restart them */
24430 + for (i = 0; i < EP_TO_UNSTALL; i++) {
24431 + struct superh_ep *ep = &dev->ep[i];
24432 + if (ep->halted) {
24433 + DBG(DBG_VERBOSE, "unstalling ep %d\n", i);
24434 + superh_ep_set_halt(&ep->ep, 0);
24435 + if (likely (!list_empty(&ep->queue))) {
24436 + struct superh_request *req
24437 + = list_entry(ep->queue.next,
24438 + struct superh_request, queue);
24439 + process_ep_req(ep, req);
24445 + init_udc_timer(dev);
24447 + local_irq_restore(flags);
24451 + * done - retire a request; caller blocked irqs
24452 + */
24453 +static void done(struct superh_ep *ep, struct superh_request *req, int status)
24455 + unsigned stopped = ep->stopped;
24457 + DBG(DBG_NOISY, "done: %s %p %d\n", ep->ep.name, req, status);
24459 + list_del_init(&req->queue);
24461 + if (likely (req->req.status == -EINPROGRESS))
24462 + req->req.status = status;
24463 + else
24464 + status = req->req.status;
24466 + if (status && status != -ESHUTDOWN)
24467 + DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
24468 + ep->ep.name, &req->req, status,
24469 + req->req.actual, req->req.length);
24471 + /* don't modify queue heads during completion callback */
24472 + ep->stopped = 1;
24473 + req->req.complete(&ep->ep, &req->req);
24474 + ep->stopped = stopped;
24478 + * Enable interrupts for the specified endpoint
24479 + */
24480 +static inline void pio_irq_enable(struct superh_ep *ep)
24482 + or_b(ep->interrupt_mask, ep->interrupt_reg);
24486 + * Disable interrupts for the specified endpoint
24487 + */
24488 +static inline void pio_irq_disable(struct superh_ep *ep)
24490 + and_b(~ep->interrupt_mask, ep->interrupt_reg);
24494 + * nuke - dequeue ALL requests
24495 + */
24496 +static void nuke(struct superh_ep *ep, int status)
24498 + struct superh_request *req;
24500 + DBG(DBG_NOISY, "nuke %s %d\n", ep->ep.name, status);
24502 + /* called with irqs blocked */
24503 +#ifdef USE_DMA
24504 + if (ep->dma >= 0 && !ep->stopped)
24505 + cancel_dma(ep);
24506 +#endif
24507 + while (!list_empty(&ep->queue)) {
24508 + req = list_entry(ep->queue.next,
24509 + struct superh_request,
24510 + queue);
24511 + done(ep, req, status);
24514 + if (ep->desc)
24515 + pio_irq_disable (ep);
24518 +static inline void clear_ep_state (struct superh_udc *dev)
24520 + unsigned i;
24522 + /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
24523 + * fifos, and pending transactions mustn't be continued in any case.
24524 + */
24525 + for (i = 1; i < 4; i++)
24526 + nuke(&dev->ep[i], -ECONNABORTED);
24530 + * write a packet to an endpoint data register
24531 + */
24532 +static int
24533 +write_packet(u32 epdr, struct superh_request *req, unsigned max)
24535 + u8 *buf;
24536 + unsigned length, count;
24538 + buf = req->req.buf + req->req.actual;
24539 + prefetch(buf);
24541 + /* how big will this packet be? */
24542 + length = min(req->req.length - req->req.actual, max);
24543 + req->req.actual += length;
24545 + count = length;
24546 + while (likely(count--))
24547 + ctrl_outb(*buf++, epdr);
24549 + return length;
24552 +static int
24553 +write_ep0_fifo (struct superh_ep *ep, struct superh_request *req)
24555 + unsigned count;
24556 + int is_short;
24558 + count = write_packet(USBEPDR0I, req, EP0_FIFO_SIZE);
24559 + ep->dev->stats.write.bytes += count;
24561 + /* last packet "must be" short (or a zlp) */
24562 + is_short = (count != EP0_FIFO_SIZE);
24564 + DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
24565 + req->req.length - req->req.actual, req);
24567 + ctrl_outb(EP0i_PKTE, USBTRG);
24569 + if (unlikely (is_short)) {
24570 + ep->dev->ep0state = EP0_END_XFER;
24572 + count = req->req.length;
24573 + done (ep, req, 0);
24574 + /*
24575 + * If we have received a specified number of setups
24576 + * after a bus reset or connect then fake a
24577 + * SET_CONFIGURATION to the driver (as we don't get
24578 + * them from the hardware).
24579 + */
24580 + if (ep->dev->setup_countdown >= 0)
24581 + ep->dev->setup_countdown--;
24582 + if (ep->dev->setup_countdown == 0) {
24583 + send_fake_config(ep->dev);
24587 + return is_short;
24591 + * handle_ep0_setup
24593 + * Handles a SETUP request on EP0
24594 + */
24595 +static void handle_ep0_setup(struct superh_udc* dev)
24597 + int i;
24598 + union { u8 raw [8]; struct usb_ctrlrequest r; } u;
24600 + for (i = 0; i < 8; i++) {
24601 + u.raw[i] = ctrl_inb(USBEPDR0S);
24604 + /* Send ACK */
24605 + ctrl_outb(EP0s_RDFN, USBTRG);
24607 + le16_to_cpus (&u.r.wValue);
24608 + le16_to_cpus (&u.r.wIndex);
24609 + le16_to_cpus (&u.r.wLength);
24611 + DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
24612 + u.r.bRequestType, u.r.bRequest,
24613 + u.r.wValue, u.r.wIndex, u.r.wLength);
24615 + if (u.r.bRequestType & USB_DIR_IN) {
24616 + DBG(DBG_VERY_NOISY, "handle_ep0_setup: EP0_IN_DATA_PHASE\n");
24617 + dev->ep0state = EP0_IN_DATA_PHASE;
24619 + else {
24620 + DBG(DBG_VERY_NOISY, "handle_ep0_setup: EP0_OUT_DATA_PHASE\n");
24621 + dev->ep0state = EP0_OUT_DATA_PHASE;
24624 + i = dev->driver->setup(&dev->gadget, &u.r);
24625 + if (i < 0) {
24626 + DMSG("SETUP %02x.%02x v%04x i%04x l%04x failed\n",
24627 + u.r.bRequestType, u.r.bRequest,
24628 + u.r.wValue, u.r.wIndex, u.r.wLength);
24629 + superh_ep_set_halt(&dev->ep[0].ep, 1);
24634 + * write to an IN endpoint fifo, as many packets as possible.
24635 + * irqs will use this to write the rest later.
24636 + * caller guarantees at least one packet buffer is ready.
24637 + */
24638 +static int
24639 +write_fifo (struct superh_ep *ep, struct superh_request *req)
24641 + unsigned max;
24643 + DBG(DBG_VERY_NOISY, "write_fifo\n");
24645 + if ((ep->bEndpointAddress & USB_DIR_IN) != USB_DIR_IN) {
24646 + DMSG("write_fifo from invalid EP (%s)\n", ep->ep.name);
24647 + return -EINVAL;
24650 + max = ep->desc->wMaxPacketSize;
24651 + do {
24652 + unsigned count;
24653 + int is_last, is_short;
24655 + count = write_packet(ep->fifo_reg, req, max);
24657 + /* last packet is usually short (or a zlp) */
24658 + if (unlikely (count != max))
24659 + is_last = is_short = 1;
24660 + else {
24661 + if (likely(req->req.length != req->req.actual)
24662 + || req->req.zero)
24663 + is_last = 0;
24664 + else
24665 + is_last = 1;
24666 + /* interrupt/iso maxpacket may not fill the fifo */
24667 + is_short = unlikely (max < ep->ep.maxpacket);
24669 + /* FIXME ep.maxpacket should be the current size,
24670 + * modified (for periodic endpoints) when the
24671 + * ep is enabled. do that, re-init as needed,
24672 + * and change maxpacket refs accordingly.
24673 + */
24676 + DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
24677 + ep->ep.name, count,
24678 + is_last ? "/L" : "", is_short ? "/S" : "",
24679 + req->req.length - req->req.actual, req);
24681 + /* let loose that packet. maybe try writing another one,
24682 + * double buffering might work.
24683 + */
24684 + or_b(ep->packet_enable_mask, USBTRG);
24686 + /* requests complete when all IN data is in the FIFO */
24687 + if (is_last) {
24688 + done (ep, req, 0);
24689 + if (list_empty(&ep->queue) || unlikely(ep->dma >= 0)) {
24690 + pio_irq_disable (ep);
24692 +#ifdef USE_DMA
24693 + /* TODO */
24694 + if (unlikely(ep->dma >= 0) && !list_empty(&ep->queue)) {
24695 + DMSG("%s pio2dma\n", ep->ep.name);
24696 + req = list_entry(ep->queue.next,
24697 + struct superh_request, queue);
24698 + kick_dma(ep,req);
24699 + return 0;
24701 +#endif
24702 + return 1;
24704 + /* Only loop if on EP2 as it is double buffered */
24705 + } while (ep->bEndpointAddress == (2|USB_DIR_IN)
24706 + && ctrl_inb(USBIFR0) & EP2_EMPTY);
24707 + return 0;
24711 + * read_ep0_fifo - unload packets from ep0 control-out fifo. caller
24712 + * should have made sure there's at least one packet ready.
24714 + * returns true if the request completed because of short packet or the
24715 + * request buffer having filled (and maybe overran till end-of-packet).
24716 + */
24717 +static int
24718 +read_ep0_fifo(struct superh_ep *ep, struct superh_request *req)
24720 + u8 *buf;
24721 + unsigned bufferspace, count;
24723 + DBG(DBG_VERY_NOISY, "read_ep0_fifo\n");
24725 + if (!ep) {
24726 + DMSG("read_ep0_fifo invalid ep\n");
24727 + return -EINVAL;
24730 + if (!req) {
24731 + DMSG("read_ep0_fifo invalid req\n");
24732 + return -EINVAL;
24735 + if (ep->desc != 0) {
24736 + DMSG("read_ep0_fifo from invalid EP (%s)\n", ep->ep.name);
24737 + return -EINVAL;
24740 + /* make sure there's a packet in the FIFO.
24741 + */
24742 + if (likely ((ctrl_inb(USBIFR0) & EP0o_TS) == 0)) {
24743 + buf = req->req.buf + req->req.actual;
24744 + bufferspace = req->req.length - req->req.actual;
24746 + /* read all bytes from this packet */
24747 + count = ctrl_inb(USBEPSZ0O);
24748 + req->req.actual += min (count, bufferspace);
24749 + DBG(DBG_VERY_NOISY, "read %s %d bytes req %p %d/%d\n",
24750 + ep->ep.name, count,
24751 + req, req->req.actual, req->req.length);
24752 + while (likely (count-- != 0)) {
24753 + u8 byte = ctrl_inb(USBEPDR0O);
24755 + if (unlikely (bufferspace == 0)) {
24756 + /* this happens when the driver's buffer
24757 + * is smaller than what the host sent.
24758 + * discard the extra data.
24759 + */
24760 + if (req->req.status != -EOVERFLOW)
24761 + DMSG("%s overflow %d\n",
24762 + ep->ep.name, count);
24763 + req->req.status = -EOVERFLOW;
24764 + } else {
24765 + *buf++ = byte;
24766 + bufferspace--;
24770 + /* Send ACK */
24771 + or_b(EP0o_RDFN, USBTRG);
24773 + /* completion */
24774 + if (req->req.actual >= req->req.length) {
24775 + done (ep, req, 0);
24776 + ep0_idle(ep->dev);
24777 + return 1;
24781 + return 0;
24785 + * read_fifo - unload packet(s) from the fifo we use for usb OUT
24786 + * transfers and put them into the request. caller should have made
24787 + * sure there's at least one packet ready.
24789 + * returns true if the request completed because of short packet or the
24790 + * request buffer having filled (and maybe overran till end-of-packet).
24791 + */
24792 +static int
24793 +read_fifo (struct superh_ep *ep, struct superh_request *req)
24795 + DBG(DBG_VERY_NOISY, "read_fifo\n");
24797 + if ((ep->bEndpointAddress & 0x0f) != 1) {
24798 + DMSG("read_fifo from invalid EP (%s)\n", ep->ep.name);
24799 + return -EINVAL;
24802 + for (;;) {
24803 + u8 *buf;
24804 + unsigned bufferspace, count, is_short;
24806 + /* make sure there's a packet in the FIFO.
24807 + */
24808 + if (unlikely ((ctrl_inb(USBIFR0) & EP1_FULL) == 0))
24809 + break;
24810 + buf = req->req.buf + req->req.actual;
24811 + bufferspace = req->req.length - req->req.actual;
24813 + /* read all bytes from this packet */
24814 + count = ctrl_inb(USBEPSZ1);
24815 + req->req.actual += min (count, bufferspace);
24816 + is_short = (count < ep->desc->wMaxPacketSize);
24817 + DBG(DBG_VERY_NOISY, "read %s %d bytes%s req %p %d/%d\n",
24818 + ep->ep.name, count,
24819 + is_short ? "/S" : "",
24820 + req, req->req.actual, req->req.length);
24821 + while (likely (count-- != 0)) {
24822 + u8 byte = ctrl_inb(USBEPDR1);
24824 + if (unlikely (bufferspace == 0)) {
24825 + /* this happens when the driver's buffer
24826 + * is smaller than what the host sent.
24827 + * discard the extra data.
24828 + */
24829 + if (req->req.status != -EOVERFLOW)
24830 + DMSG("%s overflow %d\n",
24831 + ep->ep.name, count);
24832 + req->req.status = -EOVERFLOW;
24833 + } else {
24834 + *buf++ = byte;
24835 + bufferspace--;
24839 + or_b(EP1_RDFN, USBTRG);
24840 + /* There could now be another packet because of dual buffer */
24842 + /* completion */
24843 + if (is_short || req->req.actual == req->req.length) {
24844 + done (ep, req, 0);
24845 + if (list_empty(&ep->queue))
24846 + pio_irq_disable (ep);
24847 + return 1;
24850 + /* finished that packet. the next one may be waiting... */
24852 + return 0;
24855 +/*--------------------------------------------------------------------------*/
24856 +/* Interrupt Handler(s)
24857 + */
24860 + * superh_udc_irq_f0 - high priority interrupt handler
24861 + * this deals with data to & from the bulk pipes
24862 + */
24863 +static void superh_udc_irq_f0(int irq, void *_dev, struct pt_regs *regs)
24865 + unsigned char f0_status;
24866 + struct superh_udc *dev = (struct superh_udc*) _dev;
24867 + struct superh_request *req;
24868 + struct superh_ep *ep;
24870 + DBG(DBG_NOISY, "superh_udc_irq_f0 %p\n", dev);
24872 + atomic_inc(&dev->in_interrupt);
24874 + dev->stats.irqs++;
24875 + dev->stats.irq0s++;
24876 + f0_status = ctrl_inb(USBIFR0);
24878 + /* Acknowledge interrupts */
24879 + ctrl_outb(~(f0_status & F0_HIGH), USBIFR0);
24881 + if (f0_status & EP1_FULL) {
24882 + DBG(DBG_NOISY, "superh_udc_irq_f0 %p: EP1 FULL\n", dev);
24883 + ep = &dev->ep[1];
24885 + if (likely (!list_empty(&ep->queue)))
24886 + req = list_entry(ep->queue.next,
24887 + struct superh_request, queue);
24888 + else
24889 + req = 0;
24891 + if (req)
24892 + read_fifo(ep, req);
24893 + else
24894 + pio_irq_disable(ep);
24897 + if ( f0_status & (EP2_TR | EP2_EMPTY) ) {
24898 + DBG(DBG_NOISY, "superh_udc_irq_f0 %p: EP2 TR | EP2_EMPTY\n", dev);
24899 + ep = &dev->ep[2];
24901 + if (likely (!list_empty(&ep->queue)))
24902 + req = list_entry(ep->queue.next,
24903 + struct superh_request, queue);
24904 + else
24905 + req = 0;
24907 + if (req) {
24908 + if ((f0_status & EP2_TR) && (f0_status & EP2_EMPTY))
24909 + write_fifo(ep, req);
24910 + else
24911 + and_b(~EP2_EMPTY, USBIER0);
24914 + else {
24915 + pio_irq_disable(ep);
24919 + atomic_dec(&dev->in_interrupt);
24922 +/**
24923 + * superh_udc_irq_f1 - low priority interrupt handler
24925 + */
24926 +static void superh_udc_irq_f1(int irq, void *_dev, struct pt_regs *regs)
24928 + unsigned char f0_status;
24929 + unsigned char f1_status;
24930 + struct superh_udc *dev = (struct superh_udc*) _dev;
24932 + atomic_inc(&dev->in_interrupt);;
24934 + dev->stats.irqs++;
24935 + dev->stats.irq1s++;
24937 + f0_status = ctrl_inb(USBIFR0);
24938 + f1_status = ctrl_inb(USBIFR1);
24940 + /* Acknowledge interrupts */
24941 + ctrl_outb(~(f0_status & F0_LOW), USBIFR0);
24942 + ctrl_outb(~(f1_status & F1_LOW), USBIFR1);
24944 + /* VBUSF indicates the USB being connected/disconnected */
24945 + if (f1_status & VBUSF) {
24946 + DBG(DBG_VERY_NOISY, "superh_udc_irq_f1[%lx] VBUSF\n", dev->stats.irqs);
24947 + if (!is_usb_connected) {
24948 + /* report disconnect just once */
24949 + if (dev->gadget.speed != USB_SPEED_UNKNOWN) {
24950 + DMSG("disconnect %s\n",
24951 + dev->driver ? dev->driver->driver.name : 0);
24952 + stop_activity(dev, dev->driver);
24955 + else if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
24956 + DMSG("connect\n");
24957 + dev->setup_countdown = DEFAULT_SETUP_COUNT;
24963 + /* Bus Reset */
24964 + if (f0_status & BRST) {
24965 + int i;
24966 + DBG(DBG_VERBOSE, "superh_udc_irq_f1[%lx]: BRST bus reset\n", dev->stats.irqs);
24967 + /* kill any outstanding requests */
24968 + for (i = 0; i < 4; i++) {
24969 + struct superh_ep *ep = &dev->ep[i];
24970 + nuke(ep, -ESHUTDOWN);
24971 + ep->halted = 0;
24972 + ep->stopped = 0;
24975 + /* reset fifo's and stall's */
24976 + ctrl_outb( EP3_CLEAR | EP1_CLEAR | EP2_CLEAR | EP0o_CLEAR | EP0i_CLEAR, USBFCLR );
24977 + ctrl_outb( 0, USBEPSTL );
24978 + DMSG("gadget driver '%s', address zero\n", dev->driver->driver.name);
24979 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
24980 + init_udc_timer(dev);
24981 + dev->gadget.speed = USB_SPEED_FULL;
24982 + memset(&dev->stats, 0, sizeof dev->stats);
24983 + if (dev->setup_countdown < 0)
24984 + dev->setup_countdown = RESET_SETUP_COUNT;
24985 + dev->reset_time = jiffies;
24986 + dev->fake_config = 0;
24987 + ep0_idle(dev);
24990 + /* EPOi Transmit Complete - data to host on EP0 ACKed
24991 + * EP0i Transfer Request - no data in FIFO to send on EP0
24992 + * either way we send next data if there is any and the FIFO is not busy
24993 + * it will interrupt again if we later if we don't send anything.
24994 + */
24995 + if ((f0_status & EP0i_TR || f0_status & EP0i_TS)
24996 + && (ctrl_inb(USBDASTS) & EP0i_DE) == 0) {
24997 + struct superh_ep *ep = &dev->ep[0];
24998 + struct superh_request *req;
24999 + DBG(DBG_VERY_NOISY, "superh_udc_irq_f1[%lx]: ep0i TR\n", dev->stats.irqs);
25000 + if (!list_empty(&ep->queue)) {
25001 + req = list_entry(ep->queue.next, struct superh_request, queue);
25002 + write_ep0_fifo(ep, req);
25004 + or_b(EP0i_PKTE, USBTRG);
25007 + /* Setup Command Receive Complete */
25008 + if (f0_status & SETUP_TS) {
25009 + DBG(DBG_NOISY, "superh_udc_irq_f1[%lx]: SETUP TS\n", dev->stats.irqs);
25010 + or_b( EP0o_CLEAR | EP0i_CLEAR, USBFCLR);
25011 + handle_ep0_setup(dev);
25014 + /* EPOo Receive Complete - EP0 has received data from host */
25015 + if (f0_status & EP0o_TS) {
25016 + struct superh_request *req;
25017 + struct superh_ep *ep;
25018 + DBG(DBG_VERY_NOISY, "superh_int_hndlr_f1[%lx]: ep0o TS\n", dev->stats.irqs);
25019 + ep = &dev->ep[0];
25021 + if (likely (!list_empty(&ep->queue)))
25022 + req = list_entry(ep->queue.next,
25023 + struct superh_request, queue);
25024 + else
25025 + req = 0;
25027 + if (req)
25028 + read_ep0_fifo(ep, req);
25031 + /* EP3 Transmit Request & Transmit Complete */
25032 + if ( f1_status & (EP3_TR | EP3_TS) ) {
25033 + struct superh_request *req;
25034 + struct superh_ep *ep;
25035 + DBG(DBG_VERY_NOISY, "superh_udc_irq_f1[%lx]: EP3 TR | EP3_TS (%x)\n", dev->stats.irqs, f1_status);
25036 + ep = &dev->ep[3];
25038 + if (likely (!list_empty(&ep->queue)))
25039 + req = list_entry(ep->queue.next,
25040 + struct superh_request, queue);
25041 + else
25042 + req = 0;
25044 + if (req) {
25045 + if ((f1_status & EP3_TR) && (ctrl_inb(USBDASTS) & EP3_DE) == 0)
25046 + write_fifo(ep, req);
25049 + else {
25050 + pio_irq_disable(ep);
25054 + atomic_dec(&dev->in_interrupt);;
25058 +/*--------------------------------------------------------------------------*/
25061 + * endpoint enable/disable
25063 + * we need to verify the descriptors used to enable endpoints. since superh
25064 + * endpoint configurations are fixed, and are pretty much always enabled,
25065 + * there's not a lot to manage here.
25067 + */
25068 +static int superh_ep_enable (struct usb_ep *_ep,
25069 + const struct usb_endpoint_descriptor *desc)
25071 + struct superh_ep *ep;
25072 + struct superh_udc *dev;
25074 + DBG(DBG_NOISY, "superh_ep_enable\n");
25076 + ep = container_of (_ep, struct superh_ep, ep);
25077 + if (!_ep || !desc || ep->desc || _ep->name == ep0name
25078 + || desc->bDescriptorType != USB_DT_ENDPOINT
25079 + || ep->bEndpointAddress != desc->bEndpointAddress
25080 + || ep->ep.maxpacket < desc->wMaxPacketSize) {
25081 + DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
25082 + return -EINVAL;
25085 + /* xfer types must match, except that interrupt ~= bulk */
25086 + if (ep->bmAttributes != desc->bmAttributes
25087 + && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
25088 + && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
25089 + DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
25090 + return -EINVAL;
25093 +#if 0
25094 + /* hardware _could_ do smaller, but driver doesn't */
25095 + if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
25096 + && desc->wMaxPacketSize != BULK_FIFO_SIZE)
25097 + || !desc->wMaxPacketSize) {
25098 + DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
25099 + return -ERANGE;
25101 +#endif
25103 + dev = ep->dev;
25104 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
25105 + DMSG("%s, bogus device state\n", __FUNCTION__);
25106 + return -ESHUTDOWN;
25109 + ep->desc = desc;
25110 + ep->dma = -1;
25111 + ep->stopped = 0;
25113 + /* flush fifo (mostly for OUT buffers), enable irq */
25114 + superh_ep_fifo_flush (_ep);
25116 + /* ... reset halt state too, if we could ... */
25118 +#ifdef USE_DMA
25120 +#endif
25122 + DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
25123 + return 0;
25126 +static int superh_ep_disable (struct usb_ep *_ep)
25128 + struct superh_ep *ep;
25130 + DBG(DBG_NOISY, "superh_ep_disable\n");
25132 + ep = container_of (_ep, struct superh_ep, ep);
25133 + if (!_ep || !ep->desc) {
25134 + DMSG("%s, %s not enabled\n", __FUNCTION__,
25135 + _ep ? ep->ep.name : NULL);
25136 + return -EINVAL;
25138 + nuke (ep, -ESHUTDOWN);
25140 +#ifdef USE_DMA
25141 + /* TODO */
25142 + if (ep->dma >= 0) {
25143 + *ep->reg_drcmr = 0;
25144 + pxa_free_dma (ep->dma);
25145 + ep->dma = -1;
25147 +#endif
25149 + /* flush fifo (mostly for IN buffers) */
25150 + superh_ep_fifo_flush (_ep);
25152 + ep->desc = 0;
25153 + ep->stopped = 1;
25155 + DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
25156 + return 0;
25159 +/* for the superh, these can just wrap kmalloc/kfree. gadget drivers
25160 + * must still pass correctly initialized endpoints, since other controller
25161 + * drivers may care about how it's currently set up (dma issues etc).
25162 + */
25165 + * superh_ep_alloc_request - allocate a request data structure
25166 + */
25167 +static struct usb_request *
25168 +superh_ep_alloc_request (struct usb_ep *_ep, int gfp_flags)
25170 + struct superh_request *req;
25172 + /* FIXME for bulk out-dma endpoints, preallocate a frame's worth of
25173 + * (aligned) dma descriptors at the end of the request
25174 + */
25176 + req = kmalloc (sizeof *req, gfp_flags);
25177 + if (!req)
25178 + return 0;
25180 + memset (req, 0, sizeof *req);
25181 + INIT_LIST_HEAD (&req->queue);
25182 + DBG(DBG_VERY_NOISY, "superh_ep_alloc_request: %p %d\n", req, list_empty(&req->queue));
25184 + return &req->req;
25188 + * superh_ep_free_request - deallocate a request data structure
25189 + */
25190 +static void
25191 +superh_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
25193 + struct superh_request *req;
25195 + req = container_of (_req, struct superh_request, req);
25196 + WARN_ON (!list_empty (&req->queue));
25197 + kfree(req);
25200 +/* SH cache needs flushing with DMA I/O (it's dma-incoherent), but there's
25201 + * no device-affinity and the heap works perfectly well for i/o buffers.
25202 + * TODO: check this
25203 + */
25204 +static void *
25205 +superh_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
25206 + dma_addr_t *dma, int gfp_flags)
25208 + char *retval;
25210 + retval = kmalloc (bytes, gfp_flags);
25211 + if (retval)
25212 + *dma = virt_to_bus (retval);
25213 + return retval;
25216 +static void
25217 +superh_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma,
25218 + unsigned bytes)
25220 + kfree (buf);
25223 +static struct superh_request*
25224 +process_ep_req(struct superh_ep *ep, struct superh_request *req)
25226 + struct superh_udc *dev = ep->dev;
25228 + if (ep->desc == 0 /* ep0 */) {
25229 + switch (dev->ep0state) {
25230 + case EP0_IN_DATA_PHASE:
25231 + DBG(DBG_VERY_NOISY, "superh_ep_queue: EP0_IN_DATA_PHASE\n");
25232 + dev->stats.write.ops++;
25233 + if (write_ep0_fifo(ep, req))
25234 + req = 0;
25235 + break;
25237 + case EP0_OUT_DATA_PHASE:
25238 + DBG(DBG_VERY_NOISY, "superh_ep_queue: EP0_OUT_DATA_PHASE\n");
25239 + dev->stats.read.ops++;
25240 + if (read_ep0_fifo(ep, req))
25241 + req = 0;
25242 + break;
25244 + default:
25245 + DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
25246 + return 0;
25248 +#ifdef USE_DMA
25249 + /* either start dma or prime pio pump */
25251 + else if (ep->dma >= 0) {
25252 + kick_dma(ep, req);
25253 +#endif
25254 + /* can the FIFO can satisfy the request immediately? */
25256 + else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
25257 + if ((ep->desc->bEndpointAddress & 0x0f) == 2
25258 + && (ctrl_inb(USBIFR0) & EP2_TR) != 0
25259 + && write_fifo(ep, req)) {
25260 + req = 0;
25262 + else if ((ep->desc->bEndpointAddress & 0x0f) == 3
25263 + && (ctrl_inb(USBIFR1) & EP3_TR) != 0
25264 + && write_fifo(ep, req)) {
25265 + req = 0;
25269 + if (likely (((req && ep->desc) && ep->dma < 0) || ep->desc == 0))
25270 + pio_irq_enable(ep);
25272 + return req;
25276 +static int
25277 +superh_ep_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
25279 + struct superh_request *req;
25280 + struct superh_ep *ep;
25281 + struct superh_udc *dev;
25282 + unsigned long flags;
25284 + req = container_of(_req, struct superh_request, req);
25285 + ep = container_of(_ep, struct superh_ep, ep);
25287 + DBG(DBG_VERY_NOISY, "superh_ep_queue\n");
25289 + /* If we have just sent a fake configuration request then
25290 + * this is the reply. We don't want to send it to the host
25291 + * so just ignore it.
25292 + */
25293 + if (ep->desc == 0 /* ep0 */ && ep->dev->fake_config) {
25294 + DBG(DBG_NOISY, "Ignoring bogus SET_CONFIGURATION response\n");
25295 + done(ep, req, 0);
25296 + ep->dev->fake_config = 0;
25297 + return 1;
25300 + if (unlikely (!_req || !_req->complete || !_req->buf
25301 + || !list_empty(&req->queue))) {
25302 + DMSG("%s, bad params %s, %p, %p, %p, %d\n", __FUNCTION__,
25303 + ep->ep.name, _req, _req->complete, _req->buf,
25304 + list_empty(&req->queue));
25305 + return -EINVAL;
25308 + if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
25309 + DMSG("%s, bad ep\n", __FUNCTION__);
25310 + return -EINVAL;
25313 + dev = ep->dev;
25314 + if (unlikely (!dev->driver
25315 + || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
25316 + DMSG("%s, bogus device state\n", __FUNCTION__);
25317 + return -ESHUTDOWN;
25320 +#ifdef USE_DMA
25321 + /* TODO */
25322 + if (ep->dma >= 0) {
25323 + unsigned long start = (unsigned long) _req->buf;
25325 + clean_dcache_range(start, start + _req->length);
25326 + /* or for USB_DIR_OUT, invalidate_dcache_range (...) */
25328 +#endif
25330 + DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
25331 + _ep->name, _req, _req->length, _req->buf);
25333 + local_irq_save(flags);
25335 + _req->status = -EINPROGRESS;
25336 + _req->actual = 0;
25338 + /* kickstart this i/o queue? */
25339 + if (list_empty(&ep->queue) && !ep->stopped && !ep->halted) {
25340 + req = process_ep_req(ep, req);
25343 + /* pio or dma irq handler advances the queue. */
25344 + if (likely (req != 0))
25345 + list_add_tail(&req->queue, &ep->queue);
25347 + local_irq_restore(flags);
25349 + return 0;
25352 +static int
25353 +superh_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
25355 + struct superh_ep *ep;
25356 + struct superh_request *req;
25357 + unsigned long flags;
25359 + DBG(DBG_NOISY, "superh_ep_dequeue %s\n", _ep->name);
25361 + ep = container_of(_ep, struct superh_ep, ep);
25362 + req = container_of(_req, struct superh_request, req);
25363 + if (!_ep || !_req || ep->ep.name == ep0name)
25364 + return -EINVAL;
25366 + local_irq_save(flags);
25367 +#ifdef USE_DMA
25368 + if (ep->dma >= 0 && ep->queue.next == &req->queue && !ep->stopped) {
25369 + cancel_dma(ep);
25370 + done(ep, req, -ECONNRESET);
25371 + /* restart i/o */
25372 + if (!list_empty(&ep->queue)) {
25373 + req = list_entry(ep->queue.next,
25374 + struct superh_request, queue);
25375 + kick_dma(ep, req);
25377 + } else
25378 +#endif
25379 + if (!list_empty(&req->queue))
25380 + done(ep, req, -ECONNRESET);
25381 + else
25382 + req = 0;
25383 + local_irq_restore(flags);
25385 + return req ? 0 : -EOPNOTSUPP;
25388 +/* stall/unstall an endpoint, 0 clears the stall, 1 sets it */
25389 +static int
25390 +superh_ep_set_halt(struct usb_ep *_ep, int value)
25392 + struct superh_ep *ep;
25393 + unsigned long flags;
25395 + ep = container_of(_ep, struct superh_ep, ep);
25396 + if (unlikely (!_ep
25397 + || (!ep->desc && ep->ep.name != ep0name))
25398 + || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
25399 + DMSG("%s, bad ep\n", __FUNCTION__);
25400 + return -EINVAL;
25403 + if (ep->halted == value)
25404 + return 0;
25406 + local_irq_save(flags);
25408 + if (value == 1 && (ep->bEndpointAddress & USB_DIR_IN) != 0
25409 + && ((ctrl_inb(USBDASTS) & ep->data_present_mask) != 0
25410 + || !list_empty(&ep->queue))) {
25411 + local_irq_restore(flags);
25412 + DBG(DBG_VERBOSE, "Can't %s on %s\n", value ? " halt" : "clear halt", _ep->name);
25413 + return -EAGAIN;
25416 + if (value) {
25417 + or_b(ep->stall_mask, USBEPSTL);
25418 + if (!ep->desc) {
25419 + ep->dev->ep0state = EP0_STALL;
25421 + /* disable ep interrupts and set a timer to clear the stall */
25422 + pio_irq_disable(ep);
25423 + mod_timer(&ep->dev->timer, jiffies + STALL_TIME);
25425 + else {
25426 + and_b(~ep->stall_mask, USBEPSTL);
25429 + ep->halted = value;
25431 + local_irq_restore(flags);
25433 + DBG(DBG_VERBOSE, "%s %s\n", _ep->name, value ? " halt" : "clear halt");
25435 + return 0;
25438 +static int superh_ep_fifo_status(struct usb_ep *_ep)
25440 + struct superh_ep *ep;
25442 + DBG(DBG_NOISY, "superh_ep_fifo_status\n");
25444 + ep = container_of(_ep, struct superh_ep, ep);
25445 + if (!_ep) {
25446 + DMSG("%s, bad ep\n", __FUNCTION__);
25447 + return -ENODEV;
25449 + if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
25450 + return -EOPNOTSUPP;
25451 + if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
25452 + return 0;
25453 + else {
25454 + switch (ep->desc->bEndpointAddress & 0x0f) {
25455 + case 0:
25456 + return ctrl_inb(USBEPSZ0O);
25457 + case 1:
25458 + return ctrl_inb(USBEPSZ1);
25462 + return 0;
25465 +static void superh_ep_fifo_flush(struct usb_ep *_ep)
25467 + struct superh_ep *ep;
25469 + DBG(DBG_NOISY, "superh_ep_fifo_flush\n");
25471 + ep = container_of(_ep, struct superh_ep, ep);
25472 + if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
25473 + DMSG("%s, bad ep\n", __FUNCTION__);
25474 + return;
25477 + or_b(ep->clear_mask, USBFCLR);
25480 +static struct usb_ep_ops superh_ep_ops = {
25481 + .enable = superh_ep_enable,
25482 + .disable = superh_ep_disable,
25484 + .alloc_request = superh_ep_alloc_request,
25485 + .free_request = superh_ep_free_request,
25487 + .alloc_buffer = superh_ep_alloc_buffer,
25488 + .free_buffer = superh_ep_free_buffer,
25490 + .queue = superh_ep_queue,
25491 + .dequeue = superh_ep_dequeue,
25493 + .set_halt = superh_ep_set_halt,
25494 + .fifo_status = superh_ep_fifo_status,
25495 + .fifo_flush = superh_ep_fifo_flush,
25498 +/* ---------------------------------------------------------------------------
25499 + * device-scoped parts of the api to the usb controller hardware
25500 + * ---------------------------------------------------------------------------
25501 + */
25503 +static int superh_udc_get_frame(struct usb_gadget *_gadget)
25505 + DBG(DBG_VERY_NOISY, "superh_udc_get_frame\n");
25507 + return -EOPNOTSUPP;
25510 +static const struct usb_gadget_ops superh_udc_ops = {
25511 + .get_frame = superh_udc_get_frame,
25512 + // no remote wakeup
25513 + // always selfpowered
25517 +/* if we're trying to save space, don't bother with this proc file */
25519 +#if defined(CONFIG_PROC_FS) && !defined(CONFIG_EMBEDDED)
25520 +# define UDC_PROC_FILE
25521 +#endif
25523 +#ifdef UDC_PROC_FILE
25525 +static const char proc_node_name [] = "driver/udc";
25527 +static int
25528 +udc_proc_read(char *page, char **start, off_t off, int count,
25529 + int *eof, void *_dev)
25531 + char *buf = page;
25532 + struct superh_udc *dev = _dev;
25533 + char *next = buf;
25534 + unsigned size = count;
25535 + unsigned long flags;
25536 + int t;
25537 + int i;
25539 + local_irq_save(flags);
25541 + /* basic device status */
25542 + t = snprintf(next, size,
25543 + "%s\n%s version: %s\nGadget driver: %s\nHost %s\n\n",
25544 + driver_desc,
25545 + driver_name, DRIVER_VERSION,
25546 + dev->driver ? dev->driver->driver.name : "(none)",
25547 + is_usb_connected ? "full speed" : "disconnected");
25548 + size -= t;
25549 + next += t;
25551 + /* device registers */
25552 + t = snprintf(next, size,
25553 + "ifr0 %02X, ifr1 %02X, isr0 %02X, isr1 %02X, ier0 %02X, ier1 %02X\n",
25554 + ctrl_inb(USBIFR0), ctrl_inb(USBIFR1),
25555 + ctrl_inb(USBISR0), ctrl_inb(USBISR1),
25556 + ctrl_inb(USBIER0), ctrl_inb(USBIER1));
25557 + size -= t;
25558 + next += t;
25560 + t = snprintf(next, size,
25561 + "epsz0o %02X, epsz1 %02X, dasts %02X, dmar %02X\n",
25562 + ctrl_inb(USBEPSZ0O), ctrl_inb(USBEPSZ1),
25563 + ctrl_inb(USBDASTS), ctrl_inb(USBDMA));
25564 + size -= t;
25565 + next += t;
25567 + t = snprintf(next, size,
25568 + "epstl %02X, xvercr %02X\n",
25569 + ctrl_inb(USBEPSTL), ctrl_inb(USBXVERCR));
25570 + size -= t;
25571 + next += t;
25573 + if (!is_usb_connected || !dev->driver)
25574 + goto done;
25576 + t = snprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu; irq0s %lu; irq1s %lu\n\n",
25577 + dev->stats.write.bytes, dev->stats.write.ops,
25578 + dev->stats.read.bytes, dev->stats.read.ops,
25579 + dev->stats.irq0s, dev->stats.irq1s);
25580 + size -= t;
25581 + next += t;
25583 + /* dump endpoint queues */
25584 + for (i = 0; i < 4; i++) {
25585 + struct superh_ep *ep = &dev->ep [i];
25586 + struct superh_request *req;
25587 + int t;
25589 + if (i != 0) {
25590 + const struct usb_endpoint_descriptor *d;
25592 + d = ep->desc;
25593 + if (!d)
25594 + continue;
25595 + t = snprintf(next, size,
25596 + "%s max %d %s\n",
25597 + ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
25598 + (ep->dma >= 0) ? "dma" : "pio");
25600 + } else /* ep0 should only have one transfer queued */
25601 + t = snprintf(next, size, "ep0 max 8 pio\n");
25602 + if (t <= 0 || t > size)
25603 + goto done;
25604 + size -= t;
25605 + next += t;
25607 + if (list_empty(&ep->queue)) {
25608 + t = snprintf(next, size, "\t(nothing queued)\n");
25609 + if (t <= 0 || t > size)
25610 + goto done;
25611 + size -= t;
25612 + next += t;
25613 + continue;
25615 + list_for_each_entry(req, &ep->queue, queue) {
25616 +#ifdef USE_DMA
25617 + if (ep->dma >= 0 && req->queue.prev == &ep->queue)
25618 + t = snprintf(next, size,
25619 + "\treq %p len %d/%d "
25620 + "buf %p (dma%d dcmd %08x)\n",
25621 + &req->req, req->req.actual,
25622 + req->req.length, req->req.buf,
25623 + ep->dma, DCMD(ep->dma)
25624 + // low 13 bits == bytes-to-go
25625 + );
25626 + else
25627 +#endif
25628 + t = snprintf(next, size,
25629 + "\treq %p len %d/%d buf %p\n",
25630 + &req->req, req->req.actual,
25631 + req->req.length, req->req.buf);
25632 + if (t <= 0 || t > size)
25633 + goto done;
25634 + size -= t;
25635 + next += t;
25639 +done:
25640 + local_irq_restore(flags);
25641 + return count - size;
25644 +#endif /* UDC_PROC_FILE */
25646 +/*-------------------------------------------------------------------------*/
25649 + * udc_disable - disable USB device controller
25650 + */
25651 +static void udc_disable(struct superh_udc *dev)
25653 + /* block all irqs */
25654 + ctrl_outb( 0, USBIER0);
25655 + ctrl_outb( 0, USBIER1);
25657 + /* Disable the USB module */
25658 + or_b(0x80, STBCR3);
25660 + /* Disable the USB clock */
25661 + ctrl_outw(0xA500, UCLKCR);
25663 + ep0_idle (dev);
25664 + dev->gadget.speed = USB_SPEED_UNKNOWN;
25668 + * udc_reinit - initialize software state
25669 + */
25670 +static void udc_reinit(struct superh_udc *dev)
25672 + u32 i;
25674 + /* device/ep0 records init */
25675 + INIT_LIST_HEAD (&dev->gadget.ep_list);
25676 + dev->gadget.ep0 = &dev->ep[0].ep;
25677 + INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
25678 + dev->ep0state = EP0_IDLE;
25680 + /* basic endpoint records init */
25681 + for (i = 0; i < 4; i++) {
25682 + struct superh_ep *ep = &dev->ep[i];
25684 + ep->ep.name = ep_name[i];
25685 + ep->ep.ops = &superh_ep_ops;
25686 + if (i != 0)
25687 + list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
25689 + ep->dev = dev;
25690 + ep->desc = 0;
25691 + ep->stopped = 0;
25692 + ep->halted = 0;
25693 + ep->dma = -1;
25694 + INIT_LIST_HEAD (&ep->queue);
25696 + /* address may need USB_DIR_IN, attributes likely wrong */
25697 + ep->bEndpointAddress = i;
25698 + ep->bmAttributes = USB_ENDPOINT_XFER_BULK;
25701 + /* TODO at least from here on, static initialization
25702 + * would work just as well and would need less code space
25703 + */
25705 + /* ep0 == control */
25706 + dev->ep[ 0].ep.maxpacket = EP0_FIFO_SIZE;
25707 + dev->ep[ 0].data_present_mask = EP0i_DE;
25708 + dev->ep[ 0].stall_mask = EP0_STL;
25709 + dev->ep[ 0].interrupt_mask = EP0o_TS | EP0i_TR | EP0i_TS;
25710 + dev->ep[ 0].interrupt_reg = USBIER0;
25711 + dev->ep[ 0].clear_mask = EP0i_CLEAR | EP0o_CLEAR;
25712 + dev->ep[ 0].fifo_reg = 0;
25713 + dev->ep[ 0].packet_enable_mask = 0;
25715 + dev->ep[ 1].ep.maxpacket = BULK_FIFO_SIZE;
25716 + dev->ep[ 1].bEndpointAddress |= USB_DIR_OUT;
25717 + dev->ep[ 1].data_present_mask = 0x00;
25718 + dev->ep[ 1].stall_mask = EP1_STL;
25719 + dev->ep[ 1].interrupt_mask = EP1_FULL;
25720 + dev->ep[ 1].interrupt_reg = USBIER0;
25721 + dev->ep[ 1].clear_mask = EP1_CLEAR;
25722 + dev->ep[ 1].fifo_reg = 0;
25723 + dev->ep[ 1].packet_enable_mask = 0;
25725 + dev->ep[ 2].ep.maxpacket = BULK_FIFO_SIZE;
25726 + dev->ep[ 2].bEndpointAddress |= USB_DIR_IN;
25727 + dev->ep[ 2].data_present_mask = EP2_DE;
25728 + dev->ep[ 2].stall_mask = EP2_STL;
25729 + dev->ep[ 2].interrupt_mask = EP2_TR | EP2_EMPTY;
25730 + dev->ep[ 2].interrupt_reg = USBIER0;
25731 + dev->ep[ 2].clear_mask = EP2_CLEAR;
25732 + dev->ep[ 2].fifo_reg = USBEPDR2;
25733 + dev->ep[ 2].packet_enable_mask = EP2_PKTE;
25735 + dev->ep[ 3].ep.maxpacket = INT_FIFO_SIZE;
25736 + dev->ep[ 3].bEndpointAddress |= USB_DIR_IN;
25737 + dev->ep[ 3].data_present_mask = EP3_DE;
25738 + dev->ep[ 3].stall_mask = EP3_STL;
25739 + dev->ep[ 3].interrupt_mask = EP3_TR | EP3_TS;
25740 + dev->ep[ 3].interrupt_reg = USBIER1;
25741 + dev->ep[ 3].clear_mask = EP3_CLEAR;
25742 + dev->ep[ 3].fifo_reg = USBEPDR3;
25743 + dev->ep[ 3].packet_enable_mask = EP3_PKTE;
25746 +/* until it's enabled, this UDC should be completely invisible
25747 + * to any USB host.
25748 + */
25749 +static void udc_enable (struct superh_udc *dev)
25751 +#if defined(CONFIG_CPU_SUBTYPE_SH7727)
25752 + // Reset and then Select Function USB1_pwr_en out (USB) c.f. Section 26, Table 26.1 PTE2
25753 + and_w(PN_PB2_MSK, PECR);
25754 + or_w(PN_PB2_OF, PECR);
25756 + // Reset and then Select Function UCLK c.f. Section 26, Table 26.1, PTD6
25757 + and_w(PN_PB6_MSK, PDCR);
25758 + or_w(PN_PB6_OF, PDCR);
25760 + // Stop USB module prior to setting clocks c.f. Section 9.2.3
25761 + and_b(~MSTP14, STBCR3);
25762 + or_b(MSTP14, STBCR3);
25764 + // Select external clock, 1/1 divisor c.f. Section 11.3.1
25765 + or_b(USBDIV_11|USBCKS_EC, EXCPGCR);
25767 + // Start USB c.f. Section 9.2.3
25768 + and_b(~MSTP14, STBCR3);
25770 + // Disable pullup c.f. Section 23.5.19
25771 + or_b(PULLUP_E, USBDMA);
25772 + //and_b(~PULLUP_E, USBDMA);
25774 + // Set port 1 to function, disabled c.f. Section 22.2.1
25775 + or_w(USB_TRANS_TRAN | USB_SEL_FUNC, EXPFC);
25777 + // Enable pullup c.f. Section 23.5.19a
25778 + and_b(~PULLUP_E, USBDMA);
25779 + //or_b(PULLUP_E, USBDMA);
25780 +#elif defined(CONFIG_CPU_SUBTYPE_SH7705)
25781 + /* Disable the USB module */
25782 + or_b(0x80, STBCR3);
25784 + /* Set the clock to external & enable */
25785 + ctrl_outw(0xA5E0, UCLKCR);
25787 + /* Enable the USB module */
25788 + and_b(0x7f, STBCR3);
25790 + /* Enable USB pins. */
25791 + ctrl_outw(0x01FD, PMCR); /* VBUS */
25792 + or_b(PULLUP_E, PMDR);
25793 +#endif
25794 + dev->gadget.speed = USB_SPEED_UNKNOWN;
25795 + dev->stats.irqs = 0;
25796 + dev->stats.irq0s = 0;
25797 + dev->stats.irq1s = 0;
25799 + // reset fifo's and stall's
25800 + or_b( EP3_CLEAR | EP1_CLEAR | EP2_CLEAR | EP0o_CLEAR | EP0i_CLEAR, USBFCLR);
25801 + or_b(0, USBEPSTL);
25803 + /* Setup interrupt priority by using the interrupt select registers */
25804 + ctrl_outb(F0_LOW, USBISR0);
25805 + ctrl_outb(F1_LOW, USBISR1);
25807 + /* Enable some interrupts */
25808 + or_b( BRST | SETUP_TS | EP0o_TS | EP0i_TR | EP0i_TS, USBIER0);
25809 + or_b( VBUSF, USBIER1);
25812 +/* when a driver is successfully registered, it will receive
25813 + * control requests including set_configuration(), which enables
25814 + * non-control requests. then usb traffic follows until a
25815 + * disconnect is reported. then a host may connect again, or
25816 + * the driver might get unbound.
25817 + */
25818 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
25820 + struct superh_udc *dev = the_controller;
25821 + int retval;
25823 + if (!driver
25824 + /*|| driver->speed != USB_SPEED_FULL
25825 + || !driver->bind
25826 + || !driver->unbind
25827 + || !driver->disconnect
25828 + || !driver->setup*/)
25829 + return -EINVAL;
25830 + if (!dev)
25831 + return -ENODEV;
25832 + if (dev->driver)
25833 + return -EBUSY;
25835 + /* first hook up the driver ... */
25836 + dev->driver = driver;
25838 + retval = driver->bind(&dev->gadget);
25839 + if (retval) {
25840 + DMSG("bind to driver %s --> error %d\n",
25841 + driver->driver.name, retval);
25842 + dev->driver = 0;
25843 + return retval;
25846 + /* ... then enable host detection and ep0; and we're ready
25847 + * for set_configuration as well as eventual disconnect.
25848 + * NOTE: this shouldn't power up until later.
25849 + */
25850 + udc_enable(dev);
25852 + DMSG("registered gadget driver '%s'\n", driver->driver.name);
25853 + dump_state(dev);
25854 + return 0;
25857 +EXPORT_SYMBOL(usb_gadget_register_driver);
25859 +static void
25860 +stop_activity(struct superh_udc *dev, struct usb_gadget_driver *driver)
25862 + int i;
25864 + /* don't disconnect drivers more than once */
25865 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
25866 + driver = 0;
25867 + dev->gadget.speed = USB_SPEED_UNKNOWN;
25869 + /* prevent new request submissions, kill any outstanding requests */
25870 + for (i = 0; i < 4; i++) {
25871 + struct superh_ep *ep = &dev->ep[i];
25873 + ep->stopped = 1;
25874 + nuke(ep, -ESHUTDOWN);
25877 + del_timer_sync(&dev->timer);
25879 + /* report disconnect; the driver is already quiesced */
25880 + if (driver)
25881 + driver->disconnect(&dev->gadget);
25883 + /* re-init driver-visible data structures */
25884 + udc_reinit(dev);
25887 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
25889 + struct superh_udc *dev = the_controller;
25891 + if (!dev)
25892 + return -ENODEV;
25893 + if (!driver || driver != dev->driver)
25894 + return -EINVAL;
25896 + local_irq_disable();
25897 + udc_disable(dev);
25898 + stop_activity(dev, driver);
25899 + driver->unbind(&dev->gadget);
25900 + dev->driver = 0;
25901 + local_irq_enable();
25903 + DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
25904 + dump_state(dev);
25905 + return 0;
25908 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
25911 +/*-------------------------------------------------------------------------*/
25913 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,40)
25914 +MODULE_DESCRIPTION(driver_desc);
25915 +#endif
25916 +MODULE_AUTHOR("Julian Back");
25917 +MODULE_LICENSE("GPL");
25920 + * cleanup - free resources allocated during init
25921 + */
25922 +static void /*__exit and */ __init cleanup(void)
25924 + struct superh_udc *dev = the_controller;
25926 + if (!dev)
25927 + return;
25929 + udc_disable(dev);
25930 +#ifdef UDC_PROC_FILE
25931 + remove_proc_entry(proc_node_name, NULL);
25932 +#endif
25933 + usb_gadget_unregister_driver(dev->driver);
25935 + if (dev->got_irq0) {
25936 + free_irq(USBF0_IRQ, dev);
25937 + dev->got_irq0 = 0;
25940 + if (dev->got_irq1) {
25941 + free_irq(USBF1_IRQ, dev);
25942 + dev->got_irq1 = 0;
25945 + the_controller = 0;
25947 +module_exit (cleanup);
25950 + * init - allocate resources
25951 + */
25952 +static int __init init(void)
25954 + static struct superh_udc memory;
25956 + struct superh_udc *dev;
25957 + int retval;
25959 + printk(KERN_DEBUG "%s: version %s\n", driver_name, DRIVER_VERSION);
25961 + /* initialize data */
25962 + dev = &memory;
25964 + memset(dev, 0, sizeof *dev);
25965 + dev->gadget.ops = &superh_udc_ops;
25966 + dev->gadget.name = driver_name;
25967 + dev->gadget.dev.bus_id = "udc";
25968 + dev->gadget.speed = USB_SPEED_UNKNOWN;
25970 + dev->vbusmn = 0;
25972 + atomic_set(&dev->in_interrupt, 0);
25974 + the_controller = dev;
25975 + udc_disable(dev);
25976 + udc_reinit(dev);
25978 + /* irq setup after old hardware state is cleaned up */
25979 + retval = request_irq(USBF0_IRQ, superh_udc_irq_f0,
25980 + 0/*SA_INTERRUPT | SA_SAMPLE_RANDOM*/,
25981 + driver_name, dev);
25982 + if (retval != 0) {
25983 + printk(KERN_ERR "%s: can't get irq %i, err %d\n",
25984 + driver_name, USBF0_IRQ, retval);
25985 + goto failed;
25987 + dev->got_irq0 = 1;
25989 + retval = request_irq(USBF1_IRQ, superh_udc_irq_f1,
25990 + 0/*SA_INTERRUPT | SA_SAMPLE_RANDOM*/,
25991 + driver_name, dev);
25992 + if (retval != 0) {
25993 + printk(KERN_ERR "%s: can't get irq %i, err %d\n",
25994 + driver_name, USBF1_IRQ, retval);
25995 + goto failed;
25997 + dev->got_irq1 = 1;
25999 + printk(KERN_INFO "%s, IRQs %d %d\n", driver_desc,
26000 + USBF0_IRQ, USBF1_IRQ);
26001 + dump_state(dev);
26003 + dev->setup_countdown = DEFAULT_SETUP_COUNT;
26005 +#ifdef UDC_PROC_FILE
26006 + create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
26007 +#endif
26009 + return 0;
26011 +failed:
26012 + cleanup();
26013 + return retval;
26015 +module_init (init);
26016 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/superh_udc.h kernel/drivers/usb/gadget/superh_udc.h
26017 --- /tmp/kernel/drivers/usb/gadget/superh_udc.h 1970-01-01 01:00:00.000000000 +0100
26018 +++ kernel/drivers/usb/gadget/superh_udc.h 2005-04-22 17:53:19.513526654 +0200
26019 @@ -0,0 +1,363 @@
26021 + * Renesas SuperH USB 1.1 device controller (found on SH7705, SH7727...)
26023 + * Copyright (C) 2003 Renesas Technology Europe Limited
26024 + * Copyright (C) 2003 Julian Back (jback@mpc-data.co.uk), MPC Data Limited
26026 + * This program is free software; you can redistribute it and/or modify
26027 + * it under the terms of the GNU General Public License as published by
26028 + * the Free Software Foundation; either version 2 of the License, or
26029 + * (at your option) any later version.
26031 + * This program is distributed in the hope that it will be useful,
26032 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26033 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26034 + * GNU General Public License for more details.
26036 + * You should have received a copy of the GNU General Public License
26037 + * along with this program; if not, write to the Free Software
26038 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26039 + */
26041 +#ifndef __LINUX_USB_GADGET_SUPERH_UDC_H
26042 +#define __LINUX_USB_GADGET_SUPERH_UDC_H
26044 +#include <linux/types.h>
26046 +struct superh_udc;
26048 +struct superh_ep {
26049 + struct usb_ep ep;
26050 + struct superh_udc *dev;
26052 + const struct usb_endpoint_descriptor *desc;
26053 + struct list_head queue;
26054 + int dma;
26056 + u8 bEndpointAddress;
26057 + u8 bmAttributes;
26059 + unsigned stopped : 1;
26060 + unsigned halted : 1;
26062 + u8 data_present_mask;
26063 + u8 stall_mask;
26064 + u8 interrupt_mask;
26065 + u8 clear_mask;
26066 + u8 packet_enable_mask;
26067 + unsigned interrupt_reg;
26068 + unsigned fifo_reg;
26071 +struct superh_request {
26072 + struct usb_request req;
26073 + struct list_head queue;
26076 +enum ep0_state {
26077 + EP0_IDLE,
26078 + EP0_IN_DATA_PHASE,
26079 + EP0_OUT_DATA_PHASE,
26080 + EP0_END_XFER,
26081 + EP0_STALL,
26084 +#define EP0_FIFO_SIZE ((unsigned)8)
26085 +#define BULK_FIFO_SIZE ((unsigned)64)
26086 +#define ISO_FIFO_SIZE ((unsigned)0)
26087 +#define INT_FIFO_SIZE ((unsigned)8)
26089 +struct udc_stats {
26090 + struct ep0stats {
26091 + unsigned long ops;
26092 + unsigned long bytes;
26093 + } read, write;
26094 + unsigned long irqs;
26095 + unsigned long irq0s;
26096 + unsigned long irq1s;
26099 +struct superh_udc {
26100 + struct usb_gadget gadget;
26101 + struct usb_gadget_driver *driver;
26102 + atomic_t in_interrupt;
26104 + enum ep0_state ep0state;
26105 + struct udc_stats stats;
26106 + unsigned int vbusmn;
26107 + unsigned long vbusf_time;
26108 + unsigned got_irq0 : 1,
26109 + got_irq1 : 1,
26110 + fake_config: 1;
26111 + int setup_countdown;
26112 + unsigned long reset_time;
26113 + struct timer_list timer;
26114 + struct superh_ep ep [4];
26117 +/* 2.5 changes ... */
26119 +#ifndef container_of
26120 +#define container_of list_entry
26121 +#endif
26123 +#ifndef WARN_ON
26124 +#define WARN_ON BUG_ON
26125 +#endif
26127 +/* one I/O pin should be used to detect disconnect */
26128 +#define is_usb_connected ((ctrl_inb(USBIFR1) & VBUSF) != 0)
26130 +/* Register addresses - should really be in include/asm-sh */
26132 +#ifdef CONFIG_CPU_SUBTYPE_SH7705
26134 +#define USBEPDR0I 0xA4480000
26135 +#define USBEPDR0O 0xA4480004
26136 +#define USBEPDR0S 0xA4480008
26137 +#define USBEPDR1 0xA448000C
26138 +#define USBEPDR2 0xA4480010
26139 +#define USBEPDR3 0xA4480014
26140 +#define USBIFR0 0xA4480018
26141 +#define USBIFR1 0xA448001C
26142 +#define USBTRG 0xA4480020
26143 +#define USBFCLR 0xA4480024
26144 +#define USBEPSZ0O 0xA4480028
26145 +#define USBDASTS 0xA448002C
26146 +#define USBEPSTL 0xA4480030
26147 +#define USBIER0 0xA4480034
26148 +#define USBIER1 0xA4480038
26149 +#define USBEPSZ1 0xA448003C
26150 +#define USBDMA 0xA4480040
26151 +#define USBISR0 0xA4480044
26152 +#define USBISR1 0xA4480048
26154 +#define USBXVERCR 0xA4480060
26156 +#define STBCR3 0xA40A0000
26157 +#define UCLKCR 0xA40A0008
26159 +#define PMCR 0xA4000118
26160 +#define PNCR 0xA400011A
26161 +#define PNCR2 0xA405015A
26163 +#define PMDR 0xA4000138
26165 +#endif
26168 + * Standby Control Register (STBCR3) c.f. 9.2.3
26169 + */
26171 +#define MSTP14 0x10
26173 +/*
26174 + * EXCPG Control Register (EXCPGCR) c.f. Section 11.3.1
26175 + */
26177 +#define USBDIVS_EL0 0x00
26178 +#define USBDIVS_EL1 0x01
26179 +#define USBDIVS_EL2 0x02
26181 +#define USBCKS_EL1 0x04
26182 +#define USBCKS_EL2 0x10
26183 +#define USBCKS_EL3 0x20
26185 +#define USBDIV_11 0x00
26186 +#define USBDIV_12 0x01
26187 +#define USBDIV_13 0x02
26189 +#define USBCKS_PC 0x00
26190 +#define USBCKS_IC 0x20
26191 +#define USBCKS_BC 0x24
26192 +#define USBCKS_EC 0x30
26196 + * Extra Pin Function Controller (EXPFC) c.f. Section 22.2.1
26197 + */
26199 +#define USB_TRANS_TRAN 0x00
26200 +#define USB_TRANS_DIG 0x02
26202 +#define USB_SEL_HOST 0x00
26203 +#define USB_SEL_FUNC 0x01
26207 + * USBDMA Setting Register (USBDMAR) c.f. Section 23.5.19
26208 + */
26210 +#define EP1_DMAE 0x01
26211 +#define EP2_DMAE 0x02
26213 +#if defined(CONFIG_CPU_SUBTYPE_SH7727)
26214 +#define PULLUP_E 0x04
26215 +#endif
26217 +#if defined(CONFIG_SH_EDOSK7705)
26218 +#define PULLUP_E 0x01
26219 +#endif
26222 + * USB Interrupt Flag Register 0 (USBIFR0) c.f. Section 23.5.7
26223 + */
26225 +#define BRST 0x80
26226 +#define EP1_FULL 0x40
26227 +#define EP2_TR 0x20
26228 +#define EP2_EMPTY 0x10
26229 +#define SETUP_TS 0x08
26230 +#define EP0o_TS 0x04
26231 +#define EP0i_TR 0x02
26232 +#define EP0i_TS 0x01
26236 + * USB Interrupt Flag Register 1 (USBIFR1) c.f. Section 23.5.8
26237 + */
26239 +#define VBUSMN 0x08
26240 +#define EP3_TR 0x04
26241 +#define EP3_TS 0x02
26242 +#define VBUSF 0x01
26245 + * USB Trigger Register (USBTRG) c.f. Section 23.5.9
26246 + */
26248 +#define EP0i_PKTE 0x01
26249 +#define EP0o_PKTE 0x02
26250 +#define EP0o_RDFN 0x02
26251 +#define EP0s_PKTE 0x04
26252 +#define EP0s_RDFN 0x04
26254 +#define EP2_PKTE 0x10
26255 +#define EP1_PKTE 0x20
26256 +#define EP1_RDFN 0x20
26257 +#define EP3_PKTE 0x40
26261 + * USBFIFO Clear Register (USBFCLR) c.f. Section 23.5.10
26262 + */
26264 +#define EP3_CLEAR 0x40
26265 +#define EP1_CLEAR 0x20
26266 +#define EP2_CLEAR 0x10
26267 +#define EP0o_CLEAR 0x02
26268 +#define EP0i_CLEAR 0x01
26272 + * USBEPSTL Endpoint Stall Register
26273 + */
26274 +#define EP3_STL 0x08
26275 +#define EP2_STL 0x04
26276 +#define EP1_STL 0x02
26277 +#define EP0_STL 0x01
26280 + * USBDASTS Data Status Register
26281 + */
26282 +#define EP3_DE 0x20
26283 +#define EP2_DE 0x10
26284 +#define EP0i_DE 0x01
26287 + * Port Control Registers (PNCR) c.f. Section 26.2
26288 + */
26289 +#define PN_PB0_OF 0x0000
26290 +#define PN_PB0_PO 0x0001
26291 +#define PN_PB0_PI_ON 0x0002
26292 +#define PN_PB0_PI_OFF 0x0003
26293 +#define PN_PB0_MSK ~0x0003
26295 +#define PN_PB1_OF 0x0000
26296 +#define PN_PB1_PO 0x0004
26297 +#define PN_PB1_PI_ON 0x0008
26298 +#define PN_PB1_PI_OFF 0x000c
26299 +#define PN_PB1_MSK ~0x000c
26301 +#define PN_PB2_OF 0x0000
26302 +#define PN_PB2_PO 0x0010
26303 +#define PN_PB2_PI_ON 0x0020
26304 +#define PN_PB2_PI_OFF 0x0030
26305 +#define PN_PB2_MSK ~0x0030
26307 +#define PN_PB3_OF 0x0000
26308 +#define PN_PB3_PO 0x0040
26309 +#define PN_PB3_PI_ON 0x0080
26310 +#define PN_PB3_PI_OFF 0x00c0
26311 +#define PN_PB3_MSK ~0x00c0
26313 +#define PN_PB4_OF 0x0000
26314 +#define PN_PB4_PO 0x0100
26315 +#define PN_PB4_PI_ON 0x0200
26316 +#define PN_PB4_PI_OFF 0x0300
26317 +#define PN_PB4_MSK ~0x0300
26319 +#define PN_PB5_OF 0x0000
26320 +#define PN_PB5_PO 0x0400
26321 +#define PN_PB5_PI_ON 0x0800
26322 +#define PN_PB5_PI_OFF 0x0c00
26323 +#define PN_PB5_MSK ~0x0c00
26325 +#define PN_PB6_OF 0x0000
26326 +#define PN_PB6_PO 0x1000
26327 +#define PN_PB6_PI_ON 0x2000
26328 +#define PN_PB6_PI_OFF 0x3000
26329 +#define PN_PB6_MSK ~0x3000
26331 +#define PN_PB7_OF 0x0000
26332 +#define PN_PB7_PO 0x4000
26333 +#define PN_PB7_PI_ON 0x8000
26334 +#define PN_PB7_PI_OFF 0xc000
26335 +#define PN_PB7_MSK ~0xc000
26338 + * Debugging support vanishes in non-debug builds. DBG_NORMAL should be
26339 + * mostly silent during normal use/testing, with no timing side-effects.
26340 + */
26341 +#define DBG_NORMAL 1 /* error paths, device state transitions */
26342 +#define DBG_VERBOSE 2 /* add some success path trace info */
26343 +#define DBG_NOISY 3 /* ... even more: request level */
26344 +#define DBG_VERY_NOISY 4 /* ... even more: packet level */
26346 +#ifdef DEBUG
26348 +#define DMSG(stuff...) printk(KERN_DEBUG "udc: " stuff)
26350 +#if defined(VERY_NOISY)
26351 +# define UDC_DEBUG DBG_VERY_NOISY
26352 +#elif defined(NOISY)
26353 +# define UDC_DEBUG DBG_NOISY
26354 +#elif defined(VERBOSE)
26355 +# define UDC_DEBUG DBG_VERBOSE
26356 +#else
26357 +# define UDC_DEBUG DBG_NORMAL
26358 +#endif
26360 +static void __attribute__ ((__unused__))
26361 +dump_state(struct superh_udc *dev)
26363 + if (!is_usb_connected)
26364 + return;
26368 +#else
26370 +#define DMSG(stuff...) do{}while(0)
26372 +#define UDC_DEBUG ((unsigned)0)
26374 +#define dump_state(x) do{}while(0)
26376 +#endif
26378 +#define DBG(lvl, stuff...) do{if ((lvl) <= UDC_DEBUG) DMSG(stuff);}while(0)
26380 +#define WARN(stuff...) printk(KERN_WARNING "udc: " stuff)
26382 +#endif /* __LINUX_USB_GADGET_SUPERH_UDC_H */
26383 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/usbstring.c kernel/drivers/usb/gadget/usbstring.c
26384 --- /tmp/kernel/drivers/usb/gadget/usbstring.c 1970-01-01 01:00:00.000000000 +0100
26385 +++ kernel/drivers/usb/gadget/usbstring.c 2005-04-22 17:53:19.516526166 +0200
26386 @@ -0,0 +1,136 @@
26388 + * Copyright (C) 2003 David Brownell
26390 + * This program is free software; you can redistribute it and/or modify
26391 + * it under the terms of the GNU Lesser General Public License as published
26392 + * by the Free Software Foundation; either version 2.1 of the License, or
26393 + * (at your option) any later version.
26394 + */
26396 +#include <linux/errno.h>
26397 +#include <linux/kernel.h>
26398 +#include <linux/list.h>
26399 +#include <linux/string.h>
26400 +#include <linux/init.h>
26402 +#include <linux/usb_ch9.h>
26403 +#include <linux/usb_gadget.h>
26405 +#include <asm/byteorder.h>
26406 +#include <asm/unaligned.h>
26409 +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
26411 + int count = 0;
26412 + u8 c;
26413 + u16 uchar;
26415 + /* this insists on correct encodings, though not minimal ones.
26416 + * BUT it currently rejects legit 4-byte UTF-8 code points,
26417 + * which need surrogate pairs. (Unicode 3.1 can use them.)
26418 + */
26419 + while (len != 0 && (c = (u8) *s++) != 0) {
26420 + if (unlikely(c & 0x80)) {
26421 + // 2-byte sequence:
26422 + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
26423 + if ((c & 0xe0) == 0xc0) {
26424 + uchar = (c & 0x1f) << 6;
26426 + c = (u8) *s++;
26427 + if ((c & 0xc0) != 0xc0)
26428 + goto fail;
26429 + c &= 0x3f;
26430 + uchar |= c;
26432 + // 3-byte sequence (most CJKV characters):
26433 + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
26434 + } else if ((c & 0xf0) == 0xe0) {
26435 + uchar = (c & 0x0f) << 12;
26437 + c = (u8) *s++;
26438 + if ((c & 0xc0) != 0xc0)
26439 + goto fail;
26440 + c &= 0x3f;
26441 + uchar |= c << 6;
26443 + c = (u8) *s++;
26444 + if ((c & 0xc0) != 0xc0)
26445 + goto fail;
26446 + c &= 0x3f;
26447 + uchar |= c;
26449 + /* no bogus surrogates */
26450 + if (0xd800 <= uchar && uchar <= 0xdfff)
26451 + goto fail;
26453 + // 4-byte sequence (surrogate pairs, currently rare):
26454 + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
26455 + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
26456 + // (uuuuu = wwww + 1)
26457 + // FIXME accept the surrogate code points (only)
26459 + } else
26460 + goto fail;
26461 + } else
26462 + uchar = c;
26463 + put_unaligned (cpu_to_le16 (uchar), cp++);
26464 + count++;
26465 + len--;
26467 + return count;
26468 +fail:
26469 + return -1;
26473 +/**
26474 + * usb_gadget_get_string - fill out a string descriptor
26475 + * @table: of c strings encoded using UTF-8
26476 + * @id: string id, from low byte of wValue in get string descriptor
26477 + * @buf: at least 256 bytes
26479 + * Finds the UTF-8 string matching the ID, and converts it into a
26480 + * string descriptor in utf16-le.
26481 + * Returns length of descriptor (always even) or negative errno
26483 + * If your driver needs stings in multiple languages, you'll probably
26484 + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
26485 + * using this routine after choosing which set of UTF-8 strings to use.
26486 + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
26487 + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
26488 + * characters (which are also widely used in C strings).
26489 + */
26490 +int
26491 +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
26493 + struct usb_string *s;
26494 + int len;
26496 + /* descriptor 0 has the language id */
26497 + if (id == 0) {
26498 + buf [0] = 4;
26499 + buf [1] = USB_DT_STRING;
26500 + buf [2] = (u8) table->language;
26501 + buf [3] = (u8) (table->language >> 8);
26502 + return 4;
26504 + for (s = table->strings; s && s->s; s++)
26505 + if (s->id == id)
26506 + break;
26508 + /* unrecognized: stall. */
26509 + if (!s || !s->s)
26510 + return -EINVAL;
26512 + /* string descriptors have length, tag, then UTF16-LE text */
26513 + len = min ((size_t) 126, strlen (s->s));
26514 + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
26515 + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
26516 + if (len < 0)
26517 + return -EINVAL;
26518 + buf [0] = (len + 1) * 2;
26519 + buf [1] = USB_DT_STRING;
26520 + return buf [0];
26523 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/drivers/usb/gadget/zero.c kernel/drivers/usb/gadget/zero.c
26524 --- /tmp/kernel/drivers/usb/gadget/zero.c 1970-01-01 01:00:00.000000000 +0100
26525 +++ kernel/drivers/usb/gadget/zero.c 2005-04-22 17:53:19.521525352 +0200
26526 @@ -0,0 +1,1363 @@
26528 + * zero.c -- Gadget Zero, for USB development
26530 + * Copyright (C) 2003-2004 David Brownell
26531 + * All rights reserved.
26533 + * Redistribution and use in source and binary forms, with or without
26534 + * modification, are permitted provided that the following conditions
26535 + * are met:
26536 + * 1. Redistributions of source code must retain the above copyright
26537 + * notice, this list of conditions, and the following disclaimer,
26538 + * without modification.
26539 + * 2. Redistributions in binary form must reproduce the above copyright
26540 + * notice, this list of conditions and the following disclaimer in the
26541 + * documentation and/or other materials provided with the distribution.
26542 + * 3. The names of the above-listed copyright holders may not be used
26543 + * to endorse or promote products derived from this software without
26544 + * specific prior written permission.
26546 + * ALTERNATIVELY, this software may be distributed under the terms of the
26547 + * GNU General Public License ("GPL") as published by the Free Software
26548 + * Foundation, either version 2 of that License or (at your option) any
26549 + * later version.
26551 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26552 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26553 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26554 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26555 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26556 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26557 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26558 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26559 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26560 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26561 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26562 + */
26566 + * Gadget Zero only needs two bulk endpoints, and is an example of how you
26567 + * can write a hardware-agnostic gadget driver running inside a USB device.
26569 + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
26570 + * affect most of the driver.
26572 + * Use it with the Linux host/master side "usbtest" driver to get a basic
26573 + * functional test of your device-side usb stack, or with "usb-skeleton".
26575 + * It supports two similar configurations. One sinks whatever the usb host
26576 + * writes, and in return sources zeroes. The other loops whatever the host
26577 + * writes back, so the host can read it. Module options include:
26579 + * buflen=N default N=4096, buffer size used
26580 + * qlen=N default N=32, how many buffers in the loopback queue
26581 + * loopdefault default false, list loopback config first
26583 + * Many drivers will only have one configuration, letting them be much
26584 + * simpler if they also don't support high speed operation (like this
26585 + * driver does).
26586 + */
26588 +#define DEBUG 1
26589 +// #define VERBOSE
26591 +#include <linux/config.h>
26592 +#include <linux/kernel.h>
26593 +#include <linux/module.h>
26594 +#include <linux/delay.h>
26595 +#include <linux/ioport.h>
26596 +#include <linux/sched.h>
26597 +#include <linux/slab.h>
26598 +#include <linux/smp_lock.h>
26599 +#include <linux/errno.h>
26600 +#include <linux/init.h>
26601 +#include <linux/timer.h>
26602 +#include <linux/list.h>
26603 +#include <linux/interrupt.h>
26604 +#include <linux/uts.h>
26605 +#include <linux/version.h>
26607 +#include <asm/byteorder.h>
26608 +#include <asm/io.h>
26609 +#include <asm/irq.h>
26610 +#include <asm/system.h>
26611 +#include <asm/unaligned.h>
26613 +#include <linux/usb_ch9.h>
26614 +#include <linux/usb_gadget.h>
26616 +#include "gadget_chips.h"
26619 +/*-------------------------------------------------------------------------*/
26621 +#define DRIVER_VERSION "St Patrick's Day 2004"
26623 +static const char shortname [] = "zero";
26624 +static const char longname [] = "Gadget Zero";
26626 +static const char source_sink [] = "source and sink data";
26627 +static const char loopback [] = "loop input to output";
26629 +/*-------------------------------------------------------------------------*/
26632 + * driver assumes self-powered hardware, and
26633 + * has no way for users to trigger remote wakeup.
26635 + * this version autoconfigures as much as possible,
26636 + * which is reasonable for most "bulk-only" drivers.
26637 + */
26638 +static const char *EP_IN_NAME; /* source */
26639 +static const char *EP_OUT_NAME; /* sink */
26641 +/*-------------------------------------------------------------------------*/
26643 +/* big enough to hold our biggest descriptor */
26644 +#define USB_BUFSIZ 256
26646 +struct zero_dev {
26647 + spinlock_t lock;
26648 + struct usb_gadget *gadget;
26649 + struct usb_request *req; /* for control responses */
26651 + /* when configured, we have one of two configs:
26652 + * - source data (in to host) and sink it (out from host)
26653 + * - or loop it back (out from host back in to host)
26654 + */
26655 + u8 config;
26656 + struct usb_ep *in_ep, *out_ep;
26658 + /* autoresume timer */
26659 + struct timer_list resume;
26662 +#define xprintk(d,level,fmt,args...) \
26663 + printk(level "%s %s: " fmt , shortname , (d)->gadget->dev.bus_id , \
26664 + ## args)
26666 +#ifdef DEBUG
26667 +#define DBG(dev,fmt,args...) \
26668 + xprintk(dev , KERN_DEBUG , fmt , ## args)
26669 +#else
26670 +#define DBG(dev,fmt,args...) \
26671 + do { } while (0)
26672 +#endif /* DEBUG */
26674 +#ifdef VERBOSE
26675 +#define VDBG DBG
26676 +#else
26677 +#define VDBG(dev,fmt,args...) \
26678 + do { } while (0)
26679 +#endif /* VERBOSE */
26681 +#define ERROR(dev,fmt,args...) \
26682 + xprintk(dev , KERN_ERR , fmt , ## args)
26683 +#define WARN(dev,fmt,args...) \
26684 + xprintk(dev , KERN_WARNING , fmt , ## args)
26685 +#define INFO(dev,fmt,args...) \
26686 + xprintk(dev , KERN_INFO , fmt , ## args)
26688 +/*-------------------------------------------------------------------------*/
26690 +static unsigned buflen = 4096;
26691 +static unsigned qlen = 32;
26692 +static unsigned pattern = 0;
26695 + * Normally the "loopback" configuration is second (index 1) so
26696 + * it's not the default. Here's where to change that order, to
26697 + * work better with hosts where config changes are problematic.
26698 + * Or controllers (like superh) that only support one config.
26699 + */
26700 +static int loopdefault = 0;
26703 +MODULE_PARM (buflen, "i");
26704 +MODULE_PARM_DESC (buflen, "size of i/o buffers");
26706 +MODULE_PARM (qlen, "i");
26707 +MODULE_PARM_DESC (qlen, "depth of loopback buffering");
26709 +MODULE_PARM (pattern, "i");
26710 +MODULE_PARM_DESC (pattern, "0 for default all-zeroes, 1 for mod63");
26712 +MODULE_PARM (loopdefault, "b");
26713 +MODULE_PARM_DESC (loopdefault, "true to have default config be loopback");
26716 + * if it's nonzero, autoresume says how many seconds to wait
26717 + * before trying to wake up the host after suspend.
26718 + */
26719 +static unsigned autoresume = 0;
26720 +MODULE_PARM (autoresume, "i");
26722 +/*-------------------------------------------------------------------------*/
26724 +/* Thanks to NetChip Technologies for donating this product ID.
26726 + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
26727 + * Instead: allocate your own, using normal USB-IF procedures.
26728 + */
26729 +#ifndef CONFIG_USB_ZERO_HNPTEST
26730 +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
26731 +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
26732 +#else
26733 +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
26734 +#define DRIVER_PRODUCT_NUM 0xbadd
26735 +#endif
26737 +/*-------------------------------------------------------------------------*/
26740 + * DESCRIPTORS ... most are static, but strings and (full)
26741 + * configuration descriptors are built on demand.
26742 + */
26744 +#define STRING_MANUFACTURER 25
26745 +#define STRING_PRODUCT 42
26746 +#define STRING_SERIAL 101
26747 +#define STRING_SOURCE_SINK 250
26748 +#define STRING_LOOPBACK 251
26751 + * This device advertises two configurations; these numbers work
26752 + * on a pxa250 as well as more flexible hardware.
26753 + */
26754 +#define CONFIG_SOURCE_SINK 3
26755 +#define CONFIG_LOOPBACK 2
26757 +static struct usb_device_descriptor
26758 +device_desc = {
26759 + .bLength = sizeof device_desc,
26760 + .bDescriptorType = USB_DT_DEVICE,
26762 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
26763 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
26765 + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
26766 + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
26767 + .iManufacturer = STRING_MANUFACTURER,
26768 + .iProduct = STRING_PRODUCT,
26769 + .iSerialNumber = STRING_SERIAL,
26770 + .bNumConfigurations = 2,
26773 +static struct usb_config_descriptor
26774 +source_sink_config = {
26775 + .bLength = sizeof source_sink_config,
26776 + .bDescriptorType = USB_DT_CONFIG,
26778 + /* compute wTotalLength on the fly */
26779 + .bNumInterfaces = 1,
26780 + .bConfigurationValue = CONFIG_SOURCE_SINK,
26781 + .iConfiguration = STRING_SOURCE_SINK,
26782 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
26783 + .bMaxPower = 1, /* self-powered */
26786 +static struct usb_config_descriptor
26787 +loopback_config = {
26788 + .bLength = sizeof loopback_config,
26789 + .bDescriptorType = USB_DT_CONFIG,
26791 + /* compute wTotalLength on the fly */
26792 + .bNumInterfaces = 1,
26793 + .bConfigurationValue = CONFIG_LOOPBACK,
26794 + .iConfiguration = STRING_LOOPBACK,
26795 + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
26796 + .bMaxPower = 1, /* self-powered */
26799 +static struct usb_otg_descriptor
26800 +otg_descriptor = {
26801 + .bLength = sizeof otg_descriptor,
26802 + .bDescriptorType = USB_DT_OTG,
26804 + .bmAttributes = USB_OTG_SRP,
26807 +/* one interface in each configuration */
26809 +static const struct usb_interface_descriptor
26810 +source_sink_intf = {
26811 + .bLength = sizeof source_sink_intf,
26812 + .bDescriptorType = USB_DT_INTERFACE,
26814 + .bNumEndpoints = 2,
26815 + .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
26816 + .iInterface = STRING_SOURCE_SINK,
26819 +static const struct usb_interface_descriptor
26820 +loopback_intf = {
26821 + .bLength = sizeof loopback_intf,
26822 + .bDescriptorType = USB_DT_INTERFACE,
26824 + .bNumEndpoints = 2,
26825 + .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
26826 + .iInterface = STRING_LOOPBACK,
26829 +/* two full speed bulk endpoints; their use is config-dependent */
26831 +static struct usb_endpoint_descriptor
26832 +fs_source_desc = {
26833 + .bLength = USB_DT_ENDPOINT_SIZE,
26834 + .bDescriptorType = USB_DT_ENDPOINT,
26836 + .bEndpointAddress = USB_DIR_IN,
26837 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
26840 +static struct usb_endpoint_descriptor
26841 +fs_sink_desc = {
26842 + .bLength = USB_DT_ENDPOINT_SIZE,
26843 + .bDescriptorType = USB_DT_ENDPOINT,
26845 + .bEndpointAddress = USB_DIR_OUT,
26846 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
26849 +static const struct usb_descriptor_header *fs_source_sink_function [] = {
26850 + (struct usb_descriptor_header *) &otg_descriptor,
26851 + (struct usb_descriptor_header *) &source_sink_intf,
26852 + (struct usb_descriptor_header *) &fs_sink_desc,
26853 + (struct usb_descriptor_header *) &fs_source_desc,
26854 + NULL,
26857 +static const struct usb_descriptor_header *fs_loopback_function [] = {
26858 + (struct usb_descriptor_header *) &otg_descriptor,
26859 + (struct usb_descriptor_header *) &loopback_intf,
26860 + (struct usb_descriptor_header *) &fs_sink_desc,
26861 + (struct usb_descriptor_header *) &fs_source_desc,
26862 + NULL,
26865 +#ifdef CONFIG_USB_GADGET_DUALSPEED
26868 + * usb 2.0 devices need to expose both high speed and full speed
26869 + * descriptors, unless they only run at full speed.
26871 + * that means alternate endpoint descriptors (bigger packets)
26872 + * and a "device qualifier" ... plus more construction options
26873 + * for the config descriptor.
26874 + */
26876 +static struct usb_endpoint_descriptor
26877 +hs_source_desc = {
26878 + .bLength = USB_DT_ENDPOINT_SIZE,
26879 + .bDescriptorType = USB_DT_ENDPOINT,
26881 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
26882 + .wMaxPacketSize = __constant_cpu_to_le16 (512),
26885 +static struct usb_endpoint_descriptor
26886 +hs_sink_desc = {
26887 + .bLength = USB_DT_ENDPOINT_SIZE,
26888 + .bDescriptorType = USB_DT_ENDPOINT,
26890 + .bmAttributes = USB_ENDPOINT_XFER_BULK,
26891 + .wMaxPacketSize = __constant_cpu_to_le16 (512),
26894 +static struct usb_qualifier_descriptor
26895 +dev_qualifier = {
26896 + .bLength = sizeof dev_qualifier,
26897 + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
26899 + .bcdUSB = __constant_cpu_to_le16 (0x0200),
26900 + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
26902 + .bNumConfigurations = 2,
26905 +static const struct usb_descriptor_header *hs_source_sink_function [] = {
26906 + (struct usb_descriptor_header *) &otg_descriptor,
26907 + (struct usb_descriptor_header *) &source_sink_intf,
26908 + (struct usb_descriptor_header *) &hs_source_desc,
26909 + (struct usb_descriptor_header *) &hs_sink_desc,
26910 + NULL,
26913 +static const struct usb_descriptor_header *hs_loopback_function [] = {
26914 + (struct usb_descriptor_header *) &otg_descriptor,
26915 + (struct usb_descriptor_header *) &loopback_intf,
26916 + (struct usb_descriptor_header *) &hs_source_desc,
26917 + (struct usb_descriptor_header *) &hs_sink_desc,
26918 + NULL,
26921 +/* maxpacket and other transfer characteristics vary by speed. */
26922 +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
26924 +#else
26926 +/* if there's no high speed support, maxpacket doesn't change. */
26927 +#define ep_desc(g,hs,fs) fs
26929 +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
26931 +static char manufacturer [50];
26932 +static char serial [40];
26934 +/* static strings, in UTF-8 */
26935 +static struct usb_string strings [] = {
26936 + { STRING_MANUFACTURER, manufacturer, },
26937 + { STRING_PRODUCT, longname, },
26938 + { STRING_SERIAL, serial, },
26939 + { STRING_LOOPBACK, loopback, },
26940 + { STRING_SOURCE_SINK, source_sink, },
26941 + { } /* end of list */
26944 +static struct usb_gadget_strings stringtab = {
26945 + .language = 0x0409, /* en-us */
26946 + .strings = strings,
26950 + * config descriptors are also handcrafted. these must agree with code
26951 + * that sets configurations, and with code managing interfaces and their
26952 + * altsettings. other complexity may come from:
26954 + * - high speed support, including "other speed config" rules
26955 + * - multiple configurations
26956 + * - interfaces with alternate settings
26957 + * - embedded class or vendor-specific descriptors
26959 + * this handles high speed, and has a second config that could as easily
26960 + * have been an alternate interface setting (on most hardware).
26962 + * NOTE: to demonstrate (and test) more USB capabilities, this driver
26963 + * should include an altsetting to test interrupt transfers, including
26964 + * high bandwidth modes at high speed. (Maybe work like Intel's test
26965 + * device?)
26966 + */
26967 +static int
26968 +config_buf (struct usb_gadget *gadget,
26969 + u8 *buf, u8 type, unsigned index)
26971 + int is_source_sink;
26972 + int len;
26973 + const struct usb_descriptor_header **function;
26974 +#ifdef CONFIG_USB_GADGET_DUALSPEED
26975 + int hs = (gadget->speed == USB_SPEED_HIGH);
26976 +#endif
26978 + /* two configurations will always be index 0 and index 1 */
26979 + if (index > 1)
26980 + return -EINVAL;
26981 + is_source_sink = loopdefault ? (index == 1) : (index == 0);
26983 +#ifdef CONFIG_USB_GADGET_DUALSPEED
26984 + if (type == USB_DT_OTHER_SPEED_CONFIG)
26985 + hs = !hs;
26986 + if (hs)
26987 + function = is_source_sink
26988 + ? hs_source_sink_function
26989 + : hs_loopback_function;
26990 + else
26991 +#endif
26992 + function = is_source_sink
26993 + ? fs_source_sink_function
26994 + : fs_loopback_function;
26996 + /* for now, don't advertise srp-only devices */
26997 + if (!gadget->is_otg)
26998 + function++;
27000 + len = usb_gadget_config_buf (is_source_sink
27001 + ? &source_sink_config
27002 + : &loopback_config,
27003 + buf, USB_BUFSIZ, function);
27004 + if (len < 0)
27005 + return len;
27006 + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
27007 + return len;
27010 +/*-------------------------------------------------------------------------*/
27012 +static struct usb_request *
27013 +alloc_ep_req (struct usb_ep *ep, unsigned length)
27015 + struct usb_request *req;
27017 + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
27018 + if (req) {
27019 + req->length = length;
27020 + req->buf = usb_ep_alloc_buffer (ep, length,
27021 + &req->dma, GFP_ATOMIC);
27022 + if (!req->buf) {
27023 + usb_ep_free_request (ep, req);
27024 + req = NULL;
27027 + return req;
27030 +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
27032 + if (req->buf)
27033 + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
27034 + usb_ep_free_request (ep, req);
27037 +/*-------------------------------------------------------------------------*/
27039 +/* optionally require specific source/sink data patterns */
27041 +static int
27042 +check_read_data (
27043 + struct zero_dev *dev,
27044 + struct usb_ep *ep,
27045 + struct usb_request *req
27048 + unsigned i;
27049 + u8 *buf = req->buf;
27051 + for (i = 0; i < req->actual; i++, buf++) {
27052 + switch (pattern) {
27053 + /* all-zeroes has no synchronization issues */
27054 + case 0:
27055 + if (*buf == 0)
27056 + continue;
27057 + break;
27058 + /* mod63 stays in sync with short-terminated transfers,
27059 + * or otherwise when host and gadget agree on how large
27060 + * each usb transfer request should be. resync is done
27061 + * with set_interface or set_config.
27062 + */
27063 + case 1:
27064 + if (*buf == (u8)(i % 63))
27065 + continue;
27066 + break;
27068 + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
27069 + usb_ep_set_halt (ep);
27070 + return -EINVAL;
27072 + return 0;
27075 +static void
27076 +reinit_write_data (
27077 + struct zero_dev *dev,
27078 + struct usb_ep *ep,
27079 + struct usb_request *req
27082 + unsigned i;
27083 + u8 *buf = req->buf;
27085 + switch (pattern) {
27086 + case 0:
27087 + memset (req->buf, 0, req->length);
27088 + break;
27089 + case 1:
27090 + for (i = 0; i < req->length; i++)
27091 + *buf++ = (u8) (i % 63);
27092 + break;
27096 +/* if there is only one request in the queue, there'll always be an
27097 + * irq delay between end of one request and start of the next.
27098 + * that prevents using hardware dma queues.
27099 + */
27100 +static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
27102 + struct zero_dev *dev = ep->driver_data;
27103 + int status = req->status;
27105 + switch (status) {
27107 + case 0: /* normal completion? */
27108 + if (ep == dev->out_ep)
27109 + check_read_data (dev, ep, req);
27110 + else
27111 + reinit_write_data (dev, ep, req);
27112 + break;
27114 + /* this endpoint is normally active while we're configured */
27115 + case -ECONNABORTED: /* hardware forced ep reset */
27116 + case -ECONNRESET: /* request dequeued */
27117 + case -ESHUTDOWN: /* disconnect from host */
27118 + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
27119 + req->actual, req->length);
27120 + if (ep == dev->out_ep)
27121 + check_read_data (dev, ep, req);
27122 + free_ep_req (ep, req);
27123 + return;
27125 + case -EOVERFLOW: /* buffer overrun on read means that
27126 + * we didn't provide a big enough
27127 + * buffer.
27128 + */
27129 + default:
27130 +#if 1
27131 + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
27132 + status, req->actual, req->length);
27133 +#endif
27134 + case -EREMOTEIO: /* short read */
27135 + break;
27138 + status = usb_ep_queue (ep, req, GFP_ATOMIC);
27139 + if (status) {
27140 + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
27141 + ep->name, req->length, status);
27142 + usb_ep_set_halt (ep);
27143 + /* FIXME recover later ... somehow */
27147 +static struct usb_request *
27148 +source_sink_start_ep (struct usb_ep *ep, int gfp_flags)
27150 + struct usb_request *req;
27151 + int status;
27153 + req = alloc_ep_req (ep, buflen);
27154 + if (!req)
27155 + return NULL;
27157 + memset (req->buf, 0, req->length);
27158 + req->complete = source_sink_complete;
27160 + if (strcmp (ep->name, EP_IN_NAME) == 0)
27161 + reinit_write_data (ep->driver_data, ep, req);
27163 + status = usb_ep_queue (ep, req, gfp_flags);
27164 + if (status) {
27165 + struct zero_dev *dev = ep->driver_data;
27167 + ERROR (dev, "start %s --> %d\n", ep->name, status);
27168 + free_ep_req (ep, req);
27169 + req = NULL;
27172 + return req;
27175 +static int
27176 +set_source_sink_config (struct zero_dev *dev, int gfp_flags)
27178 + int result = 0;
27179 + struct usb_ep *ep;
27180 + struct usb_gadget *gadget = dev->gadget;
27182 + gadget_for_each_ep (ep, gadget) {
27183 + const struct usb_endpoint_descriptor *d;
27185 + /* one endpoint writes (sources) zeroes in (to the host) */
27186 + if (strcmp (ep->name, EP_IN_NAME) == 0) {
27187 + d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
27188 + result = usb_ep_enable (ep, d);
27189 + if (result == 0) {
27190 + ep->driver_data = dev;
27191 + if (source_sink_start_ep (ep, gfp_flags) != 0) {
27192 + dev->in_ep = ep;
27193 + continue;
27195 + usb_ep_disable (ep);
27196 + result = -EIO;
27199 + /* one endpoint reads (sinks) anything out (from the host) */
27200 + } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
27201 + d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
27202 + result = usb_ep_enable (ep, d);
27203 + if (result == 0) {
27204 + ep->driver_data = dev;
27205 + if (source_sink_start_ep (ep, gfp_flags) != 0) {
27206 + dev->out_ep = ep;
27207 + continue;
27209 + usb_ep_disable (ep);
27210 + result = -EIO;
27213 + /* ignore any other endpoints */
27214 + } else
27215 + continue;
27217 + /* stop on error */
27218 + ERROR (dev, "can't start %s, result %d\n", ep->name, result);
27219 + break;
27221 + if (result == 0)
27222 + DBG (dev, "buflen %d\n", buflen);
27224 + /* caller is responsible for cleanup on error */
27225 + return result;
27228 +/*-------------------------------------------------------------------------*/
27230 +static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
27232 + struct zero_dev *dev = ep->driver_data;
27233 + int status = req->status;
27235 + switch (status) {
27237 + case 0: /* normal completion? */
27238 + if (ep == dev->out_ep) {
27239 + /* loop this OUT packet back IN to the host */
27240 + req->zero = (req->actual < req->length);
27241 + req->length = req->actual;
27242 + status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
27243 + if (status == 0)
27244 + return;
27246 + /* "should never get here" */
27247 + ERROR (dev, "can't loop %s to %s: %d\n",
27248 + ep->name, dev->in_ep->name,
27249 + status);
27252 + /* queue the buffer for some later OUT packet */
27253 + req->length = buflen;
27254 + status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
27255 + if (status == 0)
27256 + return;
27258 + /* "should never get here" */
27259 + /* FALLTHROUGH */
27261 + default:
27262 + ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
27263 + status, req->actual, req->length);
27264 + /* FALLTHROUGH */
27266 + /* NOTE: since this driver doesn't maintain an explicit record
27267 + * of requests it submitted (just maintains qlen count), we
27268 + * rely on the hardware driver to clean up on disconnect or
27269 + * endpoint disable.
27270 + */
27271 + case -ECONNABORTED: /* hardware forced ep reset */
27272 + case -ECONNRESET: /* request dequeued */
27273 + case -ESHUTDOWN: /* disconnect from host */
27274 + free_ep_req (ep, req);
27275 + return;
27279 +static int
27280 +set_loopback_config (struct zero_dev *dev, int gfp_flags)
27282 + int result = 0;
27283 + struct usb_ep *ep;
27284 + struct usb_gadget *gadget = dev->gadget;
27286 + gadget_for_each_ep (ep, gadget) {
27287 + const struct usb_endpoint_descriptor *d;
27289 + /* one endpoint writes data back IN to the host */
27290 + if (strcmp (ep->name, EP_IN_NAME) == 0) {
27291 + d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
27292 + result = usb_ep_enable (ep, d);
27293 + if (result == 0) {
27294 + ep->driver_data = dev;
27295 + dev->in_ep = ep;
27296 + continue;
27299 + /* one endpoint just reads OUT packets */
27300 + } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
27301 + d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
27302 + result = usb_ep_enable (ep, d);
27303 + if (result == 0) {
27304 + ep->driver_data = dev;
27305 + dev->out_ep = ep;
27306 + continue;
27309 + /* ignore any other endpoints */
27310 + } else
27311 + continue;
27313 + /* stop on error */
27314 + ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
27315 + break;
27318 + /* allocate a bunch of read buffers and queue them all at once.
27319 + * we buffer at most 'qlen' transfers; fewer if any need more
27320 + * than 'buflen' bytes each.
27321 + */
27322 + if (result == 0) {
27323 + struct usb_request *req;
27324 + unsigned i;
27326 + ep = dev->out_ep;
27327 + for (i = 0; i < qlen && result == 0; i++) {
27328 + req = alloc_ep_req (ep, buflen);
27329 + if (req) {
27330 + req->complete = loopback_complete;
27331 + result = usb_ep_queue (ep, req, GFP_ATOMIC);
27332 + if (result)
27333 + DBG (dev, "%s queue req --> %d\n",
27334 + ep->name, result);
27335 + } else
27336 + result = -ENOMEM;
27339 + if (result == 0)
27340 + DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
27342 + /* caller is responsible for cleanup on error */
27343 + return result;
27346 +/*-------------------------------------------------------------------------*/
27348 +static void zero_reset_config (struct zero_dev *dev)
27350 + if (dev->config == 0)
27351 + return;
27353 + DBG (dev, "reset config\n");
27355 + /* just disable endpoints, forcing completion of pending i/o.
27356 + * all our completion handlers free their requests in this case.
27357 + */
27358 + if (dev->in_ep) {
27359 + usb_ep_disable (dev->in_ep);
27360 + dev->in_ep = NULL;
27362 + if (dev->out_ep) {
27363 + usb_ep_disable (dev->out_ep);
27364 + dev->out_ep = NULL;
27366 + dev->config = 0;
27367 + del_timer (&dev->resume);
27370 +/* change our operational config. this code must agree with the code
27371 + * that returns config descriptors, and altsetting code.
27373 + * it's also responsible for power management interactions. some
27374 + * configurations might not work with our current power sources.
27376 + * note that some device controller hardware will constrain what this
27377 + * code can do, perhaps by disallowing more than one configuration or
27378 + * by limiting configuration choices (like the pxa2xx).
27379 + */
27380 +static int
27381 +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
27383 + int result = 0;
27384 + struct usb_gadget *gadget = dev->gadget;
27386 + if (number == dev->config)
27387 + return 0;
27389 + if (gadget_is_sa1100 (gadget) && dev->config) {
27390 + /* tx fifo is full, but we can't clear it...*/
27391 + INFO (dev, "can't change configurations\n");
27392 + return -ESPIPE;
27394 + zero_reset_config (dev);
27396 + switch (number) {
27397 + case CONFIG_SOURCE_SINK:
27398 + result = set_source_sink_config (dev, gfp_flags);
27399 + break;
27400 + case CONFIG_LOOPBACK:
27401 + result = set_loopback_config (dev, gfp_flags);
27402 + break;
27403 + default:
27404 + result = -EINVAL;
27405 + /* FALL THROUGH */
27406 + case 0:
27407 + return result;
27410 + if (!result && (!dev->in_ep || !dev->out_ep))
27411 + result = -ENODEV;
27412 + if (result)
27413 + zero_reset_config (dev);
27414 + else {
27415 + char *speed;
27417 + switch (gadget->speed) {
27418 + case USB_SPEED_LOW: speed = "low"; break;
27419 + case USB_SPEED_FULL: speed = "full"; break;
27420 + case USB_SPEED_HIGH: speed = "high"; break;
27421 + default: speed = "?"; break;
27424 + dev->config = number;
27425 + INFO (dev, "%s speed config #%d: %s\n", speed, number,
27426 + (number == CONFIG_SOURCE_SINK)
27427 + ? source_sink : loopback);
27429 + return result;
27432 +/*-------------------------------------------------------------------------*/
27434 +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
27436 + if (req->status || req->actual != req->length)
27437 + DBG ((struct zero_dev *) ep->driver_data,
27438 + "setup complete --> %d, %d/%d\n",
27439 + req->status, req->actual, req->length);
27443 + * The setup() callback implements all the ep0 functionality that's
27444 + * not handled lower down, in hardware or the hardware driver (like
27445 + * device and endpoint feature flags, and their status). It's all
27446 + * housekeeping for the gadget function we're implementing. Most of
27447 + * the work is in config-specific setup.
27448 + */
27449 +static int
27450 +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
27452 + struct zero_dev *dev = get_gadget_data (gadget);
27453 + struct usb_request *req = dev->req;
27454 + int value = -EOPNOTSUPP;
27456 + /* usually this stores reply data in the pre-allocated ep0 buffer,
27457 + * but config change events will reconfigure hardware.
27458 + */
27459 + req->zero = 0;
27460 + switch (ctrl->bRequest) {
27462 + case USB_REQ_GET_DESCRIPTOR:
27463 + if (ctrl->bRequestType != USB_DIR_IN)
27464 + goto unknown;
27465 + switch (ctrl->wValue >> 8) {
27467 + case USB_DT_DEVICE:
27468 + value = min (ctrl->wLength, (u16) sizeof device_desc);
27469 + memcpy (req->buf, &device_desc, value);
27470 + break;
27471 +#ifdef CONFIG_USB_GADGET_DUALSPEED
27472 + case USB_DT_DEVICE_QUALIFIER:
27473 + if (!gadget->is_dualspeed)
27474 + break;
27475 + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
27476 + memcpy (req->buf, &dev_qualifier, value);
27477 + break;
27479 + case USB_DT_OTHER_SPEED_CONFIG:
27480 + if (!gadget->is_dualspeed)
27481 + break;
27482 + // FALLTHROUGH
27483 +#endif /* CONFIG_USB_GADGET_DUALSPEED */
27484 + case USB_DT_CONFIG:
27485 + value = config_buf (gadget, req->buf,
27486 + ctrl->wValue >> 8,
27487 + ctrl->wValue & 0xff);
27488 + if (value >= 0)
27489 + value = min (ctrl->wLength, (u16) value);
27490 + break;
27492 + case USB_DT_STRING:
27493 + /* wIndex == language code.
27494 + * this driver only handles one language, you can
27495 + * add string tables for other languages, using
27496 + * any UTF-8 characters
27497 + */
27498 + value = usb_gadget_get_string (&stringtab,
27499 + ctrl->wValue & 0xff, req->buf);
27500 + if (value >= 0)
27501 + value = min (ctrl->wLength, (u16) value);
27502 + break;
27504 + break;
27506 + /* currently two configs, two speeds */
27507 + case USB_REQ_SET_CONFIGURATION:
27508 + if (ctrl->bRequestType != 0)
27509 + goto unknown;
27510 + if (gadget->a_hnp_support)
27511 + DBG (dev, "HNP available\n");
27512 + else if (gadget->a_alt_hnp_support)
27513 + DBG (dev, "HNP needs a different root port\n");
27514 + else
27515 + VDBG (dev, "HNP inactive\n");
27516 + spin_lock (&dev->lock);
27517 + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
27518 + spin_unlock (&dev->lock);
27519 + break;
27520 + case USB_REQ_GET_CONFIGURATION:
27521 + if (ctrl->bRequestType != USB_DIR_IN)
27522 + goto unknown;
27523 + *(u8 *)req->buf = dev->config;
27524 + value = min (ctrl->wLength, (u16) 1);
27525 + break;
27527 + /* until we add altsetting support, or other interfaces,
27528 + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
27529 + * and already killed pending endpoint I/O.
27530 + */
27531 + case USB_REQ_SET_INTERFACE:
27532 + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
27533 + goto unknown;
27534 + spin_lock (&dev->lock);
27535 + if (dev->config && ctrl->wIndex == 0 && ctrl->wValue == 0) {
27536 + u8 config = dev->config;
27538 + /* resets interface configuration, forgets about
27539 + * previous transaction state (queued bufs, etc)
27540 + * and re-inits endpoint state (toggle etc)
27541 + * no response queued, just zero status == success.
27542 + * if we had more than one interface we couldn't
27543 + * use this "reset the config" shortcut.
27544 + */
27545 + zero_reset_config (dev);
27546 + zero_set_config (dev, config, GFP_ATOMIC);
27547 + value = 0;
27549 + spin_unlock (&dev->lock);
27550 + break;
27551 + case USB_REQ_GET_INTERFACE:
27552 + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
27553 + goto unknown;
27554 + if (!dev->config)
27555 + break;
27556 + if (ctrl->wIndex != 0) {
27557 + value = -EDOM;
27558 + break;
27560 + *(u8 *)req->buf = 0;
27561 + value = min (ctrl->wLength, (u16) 1);
27562 + break;
27564 + /*
27565 + * These are the same vendor-specific requests supported by
27566 + * Intel's USB 2.0 compliance test devices. We exceed that
27567 + * device spec by allowing multiple-packet requests.
27568 + */
27569 + case 0x5b: /* control WRITE test -- fill the buffer */
27570 + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
27571 + goto unknown;
27572 + if (ctrl->wValue || ctrl->wIndex)
27573 + break;
27574 + /* just read that many bytes into the buffer */
27575 + if (ctrl->wLength > USB_BUFSIZ)
27576 + break;
27577 + value = ctrl->wLength;
27578 + break;
27579 + case 0x5c: /* control READ test -- return the buffer */
27580 + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
27581 + goto unknown;
27582 + if (ctrl->wValue || ctrl->wIndex)
27583 + break;
27584 + /* expect those bytes are still in the buffer; send back */
27585 + if (ctrl->wLength > USB_BUFSIZ
27586 + || ctrl->wLength != req->length)
27587 + break;
27588 + value = ctrl->wLength;
27589 + break;
27591 + default:
27592 +unknown:
27593 + VDBG (dev,
27594 + "unknown control req%02x.%02x v%04x i%04x l%d\n",
27595 + ctrl->bRequestType, ctrl->bRequest,
27596 + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
27599 + /* respond with data transfer before status phase? */
27600 + if (value >= 0) {
27601 + req->length = value;
27602 + req->zero = value < ctrl->wLength
27603 + && (value % gadget->ep0->maxpacket) == 0;
27604 + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
27605 + if (value < 0) {
27606 + DBG (dev, "ep_queue --> %d\n", value);
27607 + req->status = 0;
27608 + zero_setup_complete (gadget->ep0, req);
27612 + /* device either stalls (value < 0) or reports success */
27613 + return value;
27616 +static void
27617 +zero_disconnect (struct usb_gadget *gadget)
27619 + struct zero_dev *dev = get_gadget_data (gadget);
27620 + unsigned long flags;
27622 + spin_lock_irqsave (&dev->lock, flags);
27623 + zero_reset_config (dev);
27625 + /* a more significant application might have some non-usb
27626 + * activities to quiesce here, saving resources like power
27627 + * or pushing the notification up a network stack.
27628 + */
27629 + spin_unlock_irqrestore (&dev->lock, flags);
27631 + /* next we may get setup() calls to enumerate new connections;
27632 + * or an unbind() during shutdown (including removing module).
27633 + */
27636 +static void
27637 +zero_autoresume (unsigned long _dev)
27639 + struct zero_dev *dev = (struct zero_dev *) _dev;
27640 + int status;
27642 + /* normally the host would be woken up for something
27643 + * more significant than just a timer firing...
27644 + */
27645 + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
27646 + status = usb_gadget_wakeup (dev->gadget);
27647 + DBG (dev, "wakeup --> %d\n", status);
27651 +/*-------------------------------------------------------------------------*/
27653 +static void
27654 +zero_unbind (struct usb_gadget *gadget)
27656 + struct zero_dev *dev = get_gadget_data (gadget);
27658 + DBG (dev, "unbind\n");
27660 + /* we've already been disconnected ... no i/o is active */
27661 + if (dev->req)
27662 + free_ep_req (gadget->ep0, dev->req);
27663 + del_timer_sync (&dev->resume);
27664 + kfree (dev);
27665 + set_gadget_data (gadget, NULL);
27668 +static int
27669 +zero_bind (struct usb_gadget *gadget)
27671 + struct zero_dev *dev;
27672 + struct usb_ep *ep;
27674 + /* Bulk-only drivers like this one SHOULD be able to
27675 + * autoconfigure on any sane usb controller driver,
27676 + * but there may also be important quirks to address.
27677 + */
27678 + usb_ep_autoconfig_reset (gadget);
27679 + ep = usb_ep_autoconfig (gadget, &fs_source_desc);
27680 + if (!ep) {
27681 +autoconf_fail:
27682 + printk (KERN_ERR "%s: can't autoconfigure on %s\n",
27683 + shortname, gadget->name);
27684 + return -ENODEV;
27686 + EP_IN_NAME = ep->name;
27687 + ep->driver_data = ep; /* claim */
27689 + ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
27690 + if (!ep)
27691 + goto autoconf_fail;
27692 + EP_OUT_NAME = ep->name;
27693 + ep->driver_data = ep; /* claim */
27696 + /*
27697 + * DRIVER POLICY CHOICE: you may want to do this differently.
27698 + * One thing to avoid is reusing a bcdDevice revision code
27699 + * with different host-visible configurations or behavior
27700 + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
27701 + */
27702 + if (gadget_is_net2280 (gadget)) {
27703 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
27704 + } else if (gadget_is_pxa (gadget)) {
27705 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0203);
27706 +#if 0
27707 + } else if (gadget_is_sh(gadget)) {
27708 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0204);
27709 + /* SH has only one configuration; see "loopdefault" */
27710 + device_desc.bNumConfigurations = 1;
27711 + /* FIXME make 1 == default.bConfigurationValue */
27712 +#endif
27713 + } else if (gadget_is_sa1100 (gadget)) {
27714 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0205);
27715 + } else if (gadget_is_goku (gadget)) {
27716 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0206);
27717 + } else if (gadget_is_mq11xx (gadget)) {
27718 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0207);
27719 + } else if (gadget_is_omap (gadget)) {
27720 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0208);
27721 + } else if (gadget_is_lh7a40x(gadget)) {
27722 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0209);
27723 + } else if (gadget_is_n9604(gadget)) {
27724 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0210);
27725 + } else if (gadget_is_pxa27x(gadget)) {
27726 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0211);
27727 + } else if (gadget_is_s3c2410(gadget)) {
27728 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0212);
27729 + } else if (gadget_is_at91(gadget)) {
27730 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x0213);
27731 + } else {
27732 + /* gadget zero is so simple (for now, no altsettings) that
27733 + * it SHOULD NOT have problems with bulk-capable hardware.
27734 + * so warn about unrcognized controllers, don't panic.
27736 + * things like configuration and altsetting numbering
27737 + * can need hardware-specific attention though.
27738 + */
27739 + printk (KERN_WARNING "%s: controller '%s' not recognized\n",
27740 + shortname, gadget->name);
27741 + device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
27745 + /* ok, we made sense of the hardware ... */
27746 + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
27747 + if (!dev)
27748 + return -ENOMEM;
27749 + memset (dev, 0, sizeof *dev);
27750 + spin_lock_init (&dev->lock);
27751 + dev->gadget = gadget;
27752 + set_gadget_data (gadget, dev);
27754 + /* preallocate control response and buffer */
27755 + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
27756 + if (!dev->req)
27757 + goto enomem;
27758 + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
27759 + &dev->req->dma, GFP_KERNEL);
27760 + if (!dev->req->buf)
27761 + goto enomem;
27763 + dev->req->complete = zero_setup_complete;
27765 + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
27767 +#ifdef CONFIG_USB_GADGET_DUALSPEED
27768 + /* assume ep0 uses the same value for both speeds ... */
27769 + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
27771 + /* and that all endpoints are dual-speed */
27772 + hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
27773 + hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
27774 +#endif
27776 + if (gadget->is_otg) {
27777 + otg_descriptor.bmAttributes |= USB_OTG_HNP,
27778 + source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
27779 + loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
27782 + if (gadget->is_otg) {
27783 + otg_descriptor.bmAttributes |= USB_OTG_HNP,
27784 + source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
27785 + loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
27788 + usb_gadget_set_selfpowered (gadget);
27790 + init_timer (&dev->resume);
27791 + dev->resume.function = zero_autoresume;
27792 + dev->resume.data = (unsigned long) dev;
27793 + if (autoresume) {
27794 + source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
27795 + loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
27798 + gadget->ep0->driver_data = dev;
27800 + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
27801 + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
27802 + EP_OUT_NAME, EP_IN_NAME);
27804 + snprintf (manufacturer, sizeof manufacturer,
27805 + UTS_SYSNAME " " UTS_RELEASE " with %s",
27806 + gadget->name);
27808 + return 0;
27810 +enomem:
27811 + zero_unbind (gadget);
27812 + return -ENOMEM;
27815 +/*-------------------------------------------------------------------------*/
27817 +static void
27818 +zero_suspend (struct usb_gadget *gadget)
27820 + struct zero_dev *dev = get_gadget_data (gadget);
27822 + if (gadget->speed == USB_SPEED_UNKNOWN)
27823 + return;
27825 + if (autoresume) {
27826 + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
27827 + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
27828 + } else
27829 + DBG (dev, "suspend\n");
27832 +static void
27833 +zero_resume (struct usb_gadget *gadget)
27835 + struct zero_dev *dev = get_gadget_data (gadget);
27837 + DBG (dev, "resume\n");
27838 + del_timer (&dev->resume);
27842 +/*-------------------------------------------------------------------------*/
27844 +static struct usb_gadget_driver zero_driver = {
27845 +#ifdef CONFIG_USB_GADGET_DUALSPEED
27846 + .speed = USB_SPEED_HIGH,
27847 +#else
27848 + .speed = USB_SPEED_FULL,
27849 +#endif
27850 + .function = (char *) longname,
27851 + .bind = zero_bind,
27852 + .unbind = zero_unbind,
27854 + .setup = zero_setup,
27855 + .disconnect = zero_disconnect,
27857 + .suspend = zero_suspend,
27858 + .resume = zero_resume,
27860 + .driver = {
27861 + .name = (char *) shortname,
27862 + // .shutdown = ...
27863 + // .suspend = ...
27864 + // .resume = ...
27865 + },
27868 +MODULE_AUTHOR ("David Brownell");
27869 +MODULE_LICENSE ("Dual BSD/GPL");
27872 +static int __init init (void)
27874 + /* a real value would likely come through some id prom
27875 + * or module option. this one takes at least two packets.
27876 + */
27877 + strncpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
27878 + serial [sizeof serial - 1] = 0;
27880 + return usb_gadget_register_driver (&zero_driver);
27882 +module_init (init);
27884 +static void __exit cleanup (void)
27886 + usb_gadget_unregister_driver (&zero_driver);
27888 +module_exit (cleanup);
27890 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/include/linux/moduleparam.h kernel/include/linux/moduleparam.h
27891 --- /tmp/kernel/include/linux/moduleparam.h 1970-01-01 01:00:00.000000000 +0100
27892 +++ kernel/include/linux/moduleparam.h 2005-04-22 17:53:19.357552052 +0200
27893 @@ -0,0 +1,25 @@
27894 +#ifndef _LINUX_MODULE_PARAMS_H
27895 +#define _LINUX_MODULE_PARAMS_H
27896 +/* Macros for (very simple) module parameter compatibility with 2.6. */
27897 +#include <linux/module.h>
27899 +/* type is byte, short, ushort, int, uint, long, ulong, bool. (2.6
27900 + has more, but they are not supported). perm is permissions when
27901 + it appears in sysfs: 0 means doens't appear, 0444 means read-only
27902 + by everyone, 0644 means changable dynamically by root, etc. name
27903 + must be in scope (unlike MODULE_PARM).
27905 +#define module_param(name, type, perm) \
27906 + static inline void *__check_existence_##name(void) { return &name; } \
27907 + MODULE_PARM(name, _MODULE_PARM_STRING_ ## type)
27909 +#define _MODULE_PARM_STRING_byte "b"
27910 +#define _MODULE_PARM_STRING_short "h"
27911 +#define _MODULE_PARM_STRING_ushort "h"
27912 +#define _MODULE_PARM_STRING_int "i"
27913 +#define _MODULE_PARM_STRING_uint "i"
27914 +#define _MODULE_PARM_STRING_long "l"
27915 +#define _MODULE_PARM_STRING_ulong "l"
27916 +#define _MODULE_PARM_STRING_bool "i"
27918 +#endif /* _LINUX_MODULE_PARAM_TYPES_H */
27919 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/include/linux/usb_cdc.h kernel/include/linux/usb_cdc.h
27920 --- /tmp/kernel/include/linux/usb_cdc.h 1970-01-01 01:00:00.000000000 +0100
27921 +++ kernel/include/linux/usb_cdc.h 2005-04-22 17:53:19.360551563 +0200
27922 @@ -0,0 +1,204 @@
27924 + * USB Communications Device Class (CDC) definitions
27926 + * CDC says how to talk to lots of different types of network adapters,
27927 + * notably ethernet adapters and various modems. It's used mostly with
27928 + * firmware based USB peripherals.
27930 + * (C) Copyright 2005 by David Brownell
27931 + * All Rights Reserved.
27933 + * This software is licensed under the GNU GPL version 2.
27934 + */
27936 +#define USB_CDC_SUBCLASS_ACM 0x02
27937 +#define USB_CDC_SUBCLASS_ETHERNET 0x06
27938 +#define USB_CDC_SUBCLASS_WHCM 0x08
27939 +#define USB_CDC_SUBCLASS_DMM 0x09
27940 +#define USB_CDC_SUBCLASS_MDLM 0x0a
27941 +#define USB_CDC_SUBCLASS_OBEX 0x0b
27943 +#define USB_CDC_PROTO_NONE 0
27945 +#define USB_CDC_ACM_PROTO_AT_V25TER 1
27946 +#define USB_CDC_ACM_PROTO_AT_PCCA101 2
27947 +#define USB_CDC_ACM_PROTO_AT_PCCA101_WAKE 3
27948 +#define USB_CDC_ACM_PROTO_AT_GSM 4
27949 +#define USB_CDC_ACM_PROTO_AT_3G 5
27950 +#define USB_CDC_ACM_PROTO_AT_CDMA 6
27951 +#define USB_CDC_ACM_PROTO_VENDOR 0xff
27953 +/*-------------------------------------------------------------------------*/
27955 +/* 2.6 "sparse" support for checking beyond what GCC does */
27957 +#define __le16 u16
27958 +#define __le32 u32
27960 +/*-------------------------------------------------------------------------*/
27963 + * Class-Specific descriptors ... there are a couple dozen of them
27964 + */
27966 +#define USB_CDC_HEADER_TYPE 0x00 /* header_desc */
27967 +#define USB_CDC_CALL_MANAGEMENT_TYPE 0x01 /* call_mgmt_descriptor */
27968 +#define USB_CDC_ACM_TYPE 0x02 /* acm_descriptor */
27969 +#define USB_CDC_UNION_TYPE 0x06 /* union_desc */
27970 +#define USB_CDC_COUNTRY_TYPE 0x07
27971 +#define USB_CDC_ETHERNET_TYPE 0x0f /* ether_desc */
27972 +#define USB_CDC_WHCM_TYPE 0x11
27973 +#define USB_CDC_MDLM_TYPE 0x12 /* mdlm_desc */
27974 +#define USB_CDC_MDLM_DETAIL_TYPE 0x13 /* mdlm_detail_desc */
27975 +#define USB_CDC_DMM_TYPE 0x14
27976 +#define USB_CDC_OBEX_TYPE 0x15
27978 +/* "Header Functional Descriptor" from CDC spec 5.2.3.1 */
27979 +struct usb_cdc_header_desc {
27980 + __u8 bLength;
27981 + __u8 bDescriptorType;
27982 + __u8 bDescriptorSubType;
27984 + __le16 bcdCDC;
27985 +} __attribute__ ((packed));
27987 +/* "Call Management Descriptor" from CDC spec 5.2.3.2 */
27988 +struct usb_cdc_call_mgmt_descriptor {
27989 + __u8 bLength;
27990 + __u8 bDescriptorType;
27991 + __u8 bDescriptorSubType;
27993 + __u8 bmCapabilities;
27994 +#define USB_CDC_CALL_MGMT_CAP_CALL_MGMT 0x01
27995 +#define USB_CDC_CALL_MGMT_CAP_DATA_INTF 0x02
27997 + __u8 bDataInterface;
27998 +} __attribute__ ((packed));
28000 +/* "Abstract Control Management Descriptor" from CDC spec 5.2.3.3 */
28001 +struct usb_cdc_acm_descriptor {
28002 + __u8 bLength;
28003 + __u8 bDescriptorType;
28004 + __u8 bDescriptorSubType;
28006 + __u8 bmCapabilities;
28007 +} __attribute__ ((packed));
28009 +/* "Union Functional Descriptor" from CDC spec 5.2.3.8 */
28010 +struct usb_cdc_union_desc {
28011 + __u8 bLength;
28012 + __u8 bDescriptorType;
28013 + __u8 bDescriptorSubType;
28015 + __u8 bMasterInterface0;
28016 + __u8 bSlaveInterface0;
28017 + /* ... and there could be other slave interfaces */
28018 +} __attribute__ ((packed));
28020 +/* "Ethernet Networking Functional Descriptor" from CDC spec 5.2.3.16 */
28021 +struct usb_cdc_ether_desc {
28022 + __u8 bLength;
28023 + __u8 bDescriptorType;
28024 + __u8 bDescriptorSubType;
28026 + __u8 iMACAddress;
28027 + __le32 bmEthernetStatistics;
28028 + __le16 wMaxSegmentSize;
28029 + __le16 wNumberMCFilters;
28030 + __u8 bNumberPowerFilters;
28031 +} __attribute__ ((packed));
28033 +/* "MDLM Functional Descriptor" from CDC WMC spec 6.7.2.3 */
28034 +struct usb_cdc_mdlm_desc {
28035 + __u8 bLength;
28036 + __u8 bDescriptorType;
28037 + __u8 bDescriptorSubType;
28039 + __le16 bcdVersion;
28040 + __u8 bGUID[16];
28041 +} __attribute__ ((packed));
28043 +/* "MDLM Detail Functional Descriptor" from CDC WMC spec 6.7.2.4 */
28044 +struct usb_cdc_mdlm_detail_desc {
28045 + __u8 bLength;
28046 + __u8 bDescriptorType;
28047 + __u8 bDescriptorSubType;
28049 + /* type is associated with mdlm_desc.bGUID */
28050 + __u8 bGuidDescriptorType;
28051 + __u8 bDetailData[];
28052 +} __attribute__ ((packed));
28054 +/*-------------------------------------------------------------------------*/
28057 + * Class-Specific Control Requests (6.2)
28059 + * section 3.6.2.1 table 4 has the ACM profile, for modems.
28060 + * section 3.8.2 table 10 has the ethernet profile.
28062 + * Microsoft's RNDIS stack for Ethernet is a vendor-specific CDC ACM variant,
28063 + * heavily dependent on the encapsulated (proprietary) command mechanism.
28064 + */
28066 +#define USB_CDC_SEND_ENCAPSULATED_COMMAND 0x00
28067 +#define USB_CDC_GET_ENCAPSULATED_RESPONSE 0x01
28068 +#define USB_CDC_REQ_SET_LINE_CODING 0x20
28069 +#define USB_CDC_REQ_GET_LINE_CODING 0x21
28070 +#define USB_CDC_REQ_SET_CONTROL_LINE_STATE 0x22
28071 +#define USB_CDC_REQ_SEND_BREAK 0x23
28072 +#define USB_CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40
28073 +#define USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER 0x41
28074 +#define USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER 0x42
28075 +#define USB_CDC_SET_ETHERNET_PACKET_FILTER 0x43
28076 +#define USB_CDC_GET_ETHERNET_STATISTIC 0x44
28078 +/* Line Coding Structure from CDC spec 6.2.13 */
28079 +struct usb_cdc_line_coding {
28080 + __le32 dwDTERate;
28081 + __u8 bCharFormat;
28082 +#define USB_CDC_1_STOP_BITS 0
28083 +#define USB_CDC_1_5_STOP_BITS 1
28084 +#define USB_CDC_2_STOP_BITS 2
28086 + __u8 bParityType;
28087 +#define USB_CDC_NO_PARITY 0
28088 +#define USB_CDC_ODD_PARITY 1
28089 +#define USB_CDC_EVEN_PARITY 2
28090 +#define USB_CDC_MARK_PARITY 3
28091 +#define USB_CDC_SPACE_PARITY 4
28093 + __u8 bDataBits;
28094 +} __attribute__ ((packed));
28096 +/* table 62; bits in multicast filter */
28097 +#define USB_CDC_PACKET_TYPE_PROMISCUOUS (1 << 0)
28098 +#define USB_CDC_PACKET_TYPE_ALL_MULTICAST (1 << 1) /* no filter */
28099 +#define USB_CDC_PACKET_TYPE_DIRECTED (1 << 2)
28100 +#define USB_CDC_PACKET_TYPE_BROADCAST (1 << 3)
28101 +#define USB_CDC_PACKET_TYPE_MULTICAST (1 << 4) /* filtered */
28104 +/*-------------------------------------------------------------------------*/
28107 + * Class-Specific Notifications (6.3) sent by interrupt transfers
28109 + * section 3.8.2 table 11 of the CDC spec lists Ethernet notifications
28110 + * section 3.6.2.1 table 5 specifies ACM notifications, accepted by RNDIS
28111 + * RNDIS also defines its own bit-incompatible notifications
28112 + */
28114 +#define USB_CDC_NOTIFY_NETWORK_CONNECTION 0x00
28115 +#define USB_CDC_NOTIFY_RESPONSE_AVAILABLE 0x01
28116 +#define USB_CDC_NOTIFY_SERIAL_STATE 0x20
28117 +#define USB_CDC_NOTIFY_SPEED_CHANGE 0x2a
28119 +struct usb_cdc_notification {
28120 + __u8 bmRequestType;
28121 + __u8 bNotificationType;
28122 + __le16 wValue;
28123 + __le16 wIndex;
28124 + __le16 wLength;
28125 +} __attribute__ ((packed));
28127 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/include/linux/usb_ch9.h kernel/include/linux/usb_ch9.h
28128 --- /tmp/kernel/include/linux/usb_ch9.h 1970-01-01 01:00:00.000000000 +0100
28129 +++ kernel/include/linux/usb_ch9.h 2005-04-22 17:53:19.363551075 +0200
28130 @@ -0,0 +1,384 @@
28132 + * This file holds USB constants and structures that are needed for USB
28133 + * device APIs. These are used by the USB device model, which is defined
28134 + * in chapter 9 of the USB 2.0 specification. Linux has several APIs in C
28135 + * that need these:
28137 + * - the master/host side Linux-USB kernel driver API;
28138 + * - the "usbfs" user space API; and
28139 + * - (eventually) a Linux "gadget" slave/device side driver API.
28141 + * USB 2.0 adds an additional "On The Go" (OTG) mode, which lets systems
28142 + * act either as a USB master/host or as a USB slave/device. That means
28143 + * the master and slave side APIs will benefit from working well together.
28144 + */
28146 +#ifndef __LINUX_USB_CH9_H
28147 +#define __LINUX_USB_CH9_H
28149 +#include <asm/types.h> /* __u8 etc */
28151 +/*-------------------------------------------------------------------------*/
28153 +/* CONTROL REQUEST SUPPORT */
28156 + * USB directions
28158 + * This bit flag is used in endpoint descriptors' bEndpointAddress field.
28159 + * It's also one of three fields in control requests bRequestType.
28160 + */
28161 +#define USB_DIR_OUT 0 /* to device */
28162 +#define USB_DIR_IN 0x80 /* to host */
28165 + * USB types, the second of three bRequestType fields
28166 + */
28167 +#define USB_TYPE_MASK (0x03 << 5)
28168 +#define USB_TYPE_STANDARD (0x00 << 5)
28169 +#define USB_TYPE_CLASS (0x01 << 5)
28170 +#define USB_TYPE_VENDOR (0x02 << 5)
28171 +#define USB_TYPE_RESERVED (0x03 << 5)
28174 + * USB recipients, the third of three bRequestType fields
28175 + */
28176 +#define USB_RECIP_MASK 0x1f
28177 +#define USB_RECIP_DEVICE 0x00
28178 +#define USB_RECIP_INTERFACE 0x01
28179 +#define USB_RECIP_ENDPOINT 0x02
28180 +#define USB_RECIP_OTHER 0x03
28183 + * Standard requests, for the bRequest field of a SETUP packet.
28185 + * These are qualified by the bRequestType field, so that for example
28186 + * TYPE_CLASS or TYPE_VENDOR specific feature flags could be retrieved
28187 + * by a GET_STATUS request.
28188 + */
28189 +#define USB_REQ_GET_STATUS 0x00
28190 +#define USB_REQ_CLEAR_FEATURE 0x01
28191 +#define USB_REQ_SET_FEATURE 0x03
28192 +#define USB_REQ_SET_ADDRESS 0x05
28193 +#define USB_REQ_GET_DESCRIPTOR 0x06
28194 +#define USB_REQ_SET_DESCRIPTOR 0x07
28195 +#define USB_REQ_GET_CONFIGURATION 0x08
28196 +#define USB_REQ_SET_CONFIGURATION 0x09
28197 +#define USB_REQ_GET_INTERFACE 0x0A
28198 +#define USB_REQ_SET_INTERFACE 0x0B
28199 +#define USB_REQ_SYNCH_FRAME 0x0C
28202 + * USB feature flags are written using USB_REQ_{CLEAR,SET}_FEATURE, and
28203 + * are read as a bit array returned by USB_REQ_GET_STATUS. (So there
28204 + * are at most sixteen features of each type.)
28205 + */
28206 +#define USB_DEVICE_SELF_POWERED 0 /* (read only) */
28207 +#define USB_DEVICE_REMOTE_WAKEUP 1 /* dev may initiate wakeup */
28208 +#define USB_DEVICE_TEST_MODE 2 /* (high speed only) */
28209 +#define USB_DEVICE_B_HNP_ENABLE 3 /* dev may initiate HNP */
28210 +#define USB_DEVICE_A_HNP_SUPPORT 4 /* RH port supports HNP */
28211 +#define USB_DEVICE_A_ALT_HNP_SUPPORT 5 /* other RH port does */
28212 +#define USB_DEVICE_DEBUG_MODE 6 /* (special devices only) */
28214 +#define USB_ENDPOINT_HALT 0 /* IN/OUT will STALL */
28217 +/**
28218 + * struct usb_ctrlrequest - SETUP data for a USB device control request
28219 + * @bRequestType: matches the USB bmRequestType field
28220 + * @bRequest: matches the USB bRequest field
28221 + * @wValue: matches the USB wValue field (le16 byte order)
28222 + * @wIndex: matches the USB wIndex field (le16 byte order)
28223 + * @wLength: matches the USB wLength field (le16 byte order)
28225 + * This structure is used to send control requests to a USB device. It matches
28226 + * the different fields of the USB 2.0 Spec section 9.3, table 9-2. See the
28227 + * USB spec for a fuller description of the different fields, and what they are
28228 + * used for.
28230 + * Note that the driver for any interface can issue control requests.
28231 + * For most devices, interfaces don't coordinate with each other, so
28232 + * such requests may be made at any time.
28233 + */
28234 +struct usb_ctrlrequest {
28235 + __u8 bRequestType;
28236 + __u8 bRequest;
28237 + __u16 wValue;
28238 + __u16 wIndex;
28239 + __u16 wLength;
28240 +} __attribute__ ((packed));
28242 +/*-------------------------------------------------------------------------*/
28245 + * STANDARD DESCRIPTORS ... as returned by GET_DESCRIPTOR, or
28246 + * (rarely) accepted by SET_DESCRIPTOR.
28248 + * Note that all multi-byte values here are encoded in little endian
28249 + * byte order "on the wire". But when exposed through Linux-USB APIs,
28250 + * they've been converted to cpu byte order.
28251 + */
28254 + * Descriptor types ... USB 2.0 spec table 9.5
28255 + */
28256 +#define USB_DT_DEVICE 0x01
28257 +#define USB_DT_CONFIG 0x02
28258 +#define USB_DT_STRING 0x03
28259 +#define USB_DT_INTERFACE 0x04
28260 +#define USB_DT_ENDPOINT 0x05
28261 +#define USB_DT_DEVICE_QUALIFIER 0x06
28262 +#define USB_DT_OTHER_SPEED_CONFIG 0x07
28263 +#define USB_DT_INTERFACE_POWER 0x08
28264 +/* these are from a minor usb 2.0 revision (ECN) */
28265 +#define USB_DT_OTG 0x09
28266 +#define USB_DT_DEBUG 0x0a
28267 +#define USB_DT_INTERFACE_ASSOCIATION 0x0b
28269 +/* conventional codes for class-specific descriptors */
28270 +#define USB_DT_CS_DEVICE 0x21
28271 +#define USB_DT_CS_CONFIG 0x22
28272 +#define USB_DT_CS_STRING 0x23
28273 +#define USB_DT_CS_INTERFACE 0x24
28274 +#define USB_DT_CS_ENDPOINT 0x25
28276 +/* All standard descriptors have these 2 fields at the beginning */
28277 +struct usb_descriptor_header {
28278 + __u8 bLength;
28279 + __u8 bDescriptorType;
28280 +} __attribute__ ((packed));
28283 +/*-------------------------------------------------------------------------*/
28285 +/* USB_DT_DEVICE: Device descriptor */
28286 +struct usb_device_descriptor {
28287 + __u8 bLength;
28288 + __u8 bDescriptorType;
28290 + __u16 bcdUSB;
28291 + __u8 bDeviceClass;
28292 + __u8 bDeviceSubClass;
28293 + __u8 bDeviceProtocol;
28294 + __u8 bMaxPacketSize0;
28295 + __u16 idVendor;
28296 + __u16 idProduct;
28297 + __u16 bcdDevice;
28298 + __u8 iManufacturer;
28299 + __u8 iProduct;
28300 + __u8 iSerialNumber;
28301 + __u8 bNumConfigurations;
28302 +} __attribute__ ((packed));
28304 +#define USB_DT_DEVICE_SIZE 18
28308 + * Device and/or Interface Class codes
28309 + * as found in bDeviceClass or bInterfaceClass
28310 + * and defined by www.usb.org documents
28311 + */
28312 +#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
28313 +#define USB_CLASS_AUDIO 1
28314 +#define USB_CLASS_COMM 2
28315 +#define USB_CLASS_HID 3
28316 +#define USB_CLASS_PHYSICAL 5
28317 +#define USB_CLASS_STILL_IMAGE 6
28318 +#define USB_CLASS_PRINTER 7
28319 +#define USB_CLASS_MASS_STORAGE 8
28320 +#define USB_CLASS_HUB 9
28321 +#define USB_CLASS_CDC_DATA 0x0a
28322 +#define USB_CLASS_CSCID 0x0b /* chip+ smart card */
28323 +#define USB_CLASS_CONTENT_SEC 0x0d /* content security */
28324 +#define USB_CLASS_VIDEO 0x0e
28325 +#define USB_CLASS_APP_SPEC 0xfe
28326 +#define USB_CLASS_VENDOR_SPEC 0xff
28328 +/*-------------------------------------------------------------------------*/
28330 +/* USB_DT_CONFIG: Configuration descriptor information.
28332 + * USB_DT_OTHER_SPEED_CONFIG is the same descriptor, except that the
28333 + * descriptor type is different. Highspeed-capable devices can look
28334 + * different depending on what speed they're currently running. Only
28335 + * devices with a USB_DT_DEVICE_QUALIFIER have any OTHER_SPEED_CONFIG
28336 + * descriptors.
28337 + */
28338 +struct usb_config_descriptor {
28339 + __u8 bLength;
28340 + __u8 bDescriptorType;
28342 + __u16 wTotalLength;
28343 + __u8 bNumInterfaces;
28344 + __u8 bConfigurationValue;
28345 + __u8 iConfiguration;
28346 + __u8 bmAttributes;
28347 + __u8 bMaxPower;
28348 +} __attribute__ ((packed));
28350 +#define USB_DT_CONFIG_SIZE 9
28352 +/* from config descriptor bmAttributes */
28353 +#define USB_CONFIG_ATT_ONE (1 << 7) /* must be set */
28354 +#define USB_CONFIG_ATT_SELFPOWER (1 << 6) /* self powered */
28355 +#define USB_CONFIG_ATT_WAKEUP (1 << 5) /* can wakeup */
28357 +/*-------------------------------------------------------------------------*/
28359 +/* USB_DT_STRING: String descriptor */
28360 +struct usb_string_descriptor {
28361 + __u8 bLength;
28362 + __u8 bDescriptorType;
28364 + __u16 wData[1]; /* UTF-16LE encoded */
28365 +} __attribute__ ((packed));
28367 +/* note that "string" zero is special, it holds language codes that
28368 + * the device supports, not Unicode characters.
28369 + */
28371 +/*-------------------------------------------------------------------------*/
28373 +/* USB_DT_INTERFACE: Interface descriptor */
28374 +struct usb_interface_descriptor {
28375 + __u8 bLength;
28376 + __u8 bDescriptorType;
28378 + __u8 bInterfaceNumber;
28379 + __u8 bAlternateSetting;
28380 + __u8 bNumEndpoints;
28381 + __u8 bInterfaceClass;
28382 + __u8 bInterfaceSubClass;
28383 + __u8 bInterfaceProtocol;
28384 + __u8 iInterface;
28385 +} __attribute__ ((packed));
28387 +#define USB_DT_INTERFACE_SIZE 9
28389 +/*-------------------------------------------------------------------------*/
28391 +/* USB_DT_ENDPOINT: Endpoint descriptor */
28392 +struct usb_endpoint_descriptor {
28393 + __u8 bLength;
28394 + __u8 bDescriptorType;
28396 + __u8 bEndpointAddress;
28397 + __u8 bmAttributes;
28398 + __u16 wMaxPacketSize;
28399 + __u8 bInterval;
28401 + // NOTE: these two are _only_ in audio endpoints.
28402 + // use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof.
28403 + __u8 bRefresh;
28404 + __u8 bSynchAddress;
28405 +} __attribute__ ((packed));
28407 +#define USB_DT_ENDPOINT_SIZE 7
28408 +#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
28412 + * Endpoints
28413 + */
28414 +#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
28415 +#define USB_ENDPOINT_DIR_MASK 0x80
28417 +#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
28418 +#define USB_ENDPOINT_XFER_CONTROL 0
28419 +#define USB_ENDPOINT_XFER_ISOC 1
28420 +#define USB_ENDPOINT_XFER_BULK 2
28421 +#define USB_ENDPOINT_XFER_INT 3
28424 +/*-------------------------------------------------------------------------*/
28426 +/* USB_DT_DEVICE_QUALIFIER: Device Qualifier descriptor */
28427 +struct usb_qualifier_descriptor {
28428 + __u8 bLength;
28429 + __u8 bDescriptorType;
28431 + __u16 bcdUSB;
28432 + __u8 bDeviceClass;
28433 + __u8 bDeviceSubClass;
28434 + __u8 bDeviceProtocol;
28435 + __u8 bMaxPacketSize0;
28436 + __u8 bNumConfigurations;
28437 + __u8 bRESERVED;
28438 +} __attribute__ ((packed));
28441 +/*-------------------------------------------------------------------------*/
28443 +/* USB_DT_OTG (from OTG 1.0a supplement) */
28444 +struct usb_otg_descriptor {
28445 + __u8 bLength;
28446 + __u8 bDescriptorType;
28448 + __u8 bmAttributes; /* support for HNP, SRP, etc */
28449 +} __attribute__ ((packed));
28451 +/* from usb_otg_descriptor.bmAttributes */
28452 +#define USB_OTG_SRP (1 << 0)
28453 +#define USB_OTG_HNP (1 << 1) /* swap host/device roles */
28455 +/*-------------------------------------------------------------------------*/
28457 +/* USB_DT_DEBUG: for special highspeed devices, replacing serial console */
28458 +struct usb_debug_descriptor {
28459 + __u8 bLength;
28460 + __u8 bDescriptorType;
28462 + /* bulk endpoints with 8 byte maxpacket */
28463 + __u8 bDebugInEndpoint;
28464 + __u8 bDebugOutEndpoint;
28467 +/*-------------------------------------------------------------------------*/
28469 +/* USB_DT_INTERFACE_ASSOCIATION: groups interfaces */
28470 +struct usb_interface_assoc_descriptor {
28471 + __u8 bLength;
28472 + __u8 bDescriptorType;
28474 + __u8 bFirstInterface;
28475 + __u8 bInterfaceCount;
28476 + __u8 bFunctionClass;
28477 + __u8 bFunctionSubClass;
28478 + __u8 bFunctionProtocol;
28479 + __u8 iFunction;
28480 +} __attribute__ ((packed));
28483 +/*-------------------------------------------------------------------------*/
28485 +/* USB 2.0 defines three speeds, here's how Linux identifies them */
28487 +enum usb_device_speed {
28488 + USB_SPEED_UNKNOWN = 0, /* enumerating */
28489 + USB_SPEED_LOW, USB_SPEED_FULL, /* usb 1.1 */
28490 + USB_SPEED_HIGH /* usb 2.0 */
28493 +enum usb_device_state {
28494 + /* NOTATTACHED isn't in the USB spec, and this state acts
28495 + * the same as ATTACHED ... but it's clearer this way.
28496 + */
28497 + USB_STATE_NOTATTACHED = 0,
28499 + /* the chapter 9 device states */
28500 + USB_STATE_ATTACHED,
28501 + USB_STATE_POWERED,
28502 + USB_STATE_DEFAULT, /* limited function */
28503 + USB_STATE_ADDRESS,
28504 + USB_STATE_CONFIGURED, /* most functions */
28506 + USB_STATE_SUSPENDED
28508 + /* NOTE: there are actually four different SUSPENDED
28509 + * states, returning to POWERED, DEFAULT, ADDRESS, or
28510 + * CONFIGURED respectively when SOF tokens flow again.
28511 + */
28514 +#endif /* __LINUX_USB_CH9_H */
28515 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/include/linux/usb_gadget.h kernel/include/linux/usb_gadget.h
28516 --- /tmp/kernel/include/linux/usb_gadget.h 1970-01-01 01:00:00.000000000 +0100
28517 +++ kernel/include/linux/usb_gadget.h 2005-04-22 17:53:19.367550424 +0200
28518 @@ -0,0 +1,896 @@
28520 + * <linux/usb_gadget.h>
28522 + * We call the USB code inside a Linux-based peripheral device a "gadget"
28523 + * driver, except for the hardware-specific bus glue. One USB host can
28524 + * master many USB gadgets, but the gadgets are only slaved to one host.
28527 + * (C) Copyright 2002-2004 by David Brownell
28528 + * All Rights Reserved.
28530 + * This software is licensed under the GNU GPL version 2.
28531 + */
28533 +#ifndef __LINUX_USB_GADGET_H
28534 +#define __LINUX_USB_GADGET_H
28536 +#ifdef __KERNEL__
28538 +struct usb_ep;
28540 +/**
28541 + * struct usb_request - describes one i/o request
28542 + * @buf: Buffer used for data. Always provide this; some controllers
28543 + * only use PIO, or don't use DMA for some endpoints.
28544 + * @dma: DMA address corresponding to 'buf'. If you don't set this
28545 + * field, and the usb controller needs one, it is responsible
28546 + * for mapping and unmapping the buffer.
28547 + * @length: Length of that data
28548 + * @no_interrupt: If true, hints that no completion irq is needed.
28549 + * Helpful sometimes with deep request queues that are handled
28550 + * directly by DMA controllers.
28551 + * @zero: If true, when writing data, makes the last packet be "short"
28552 + * by adding a zero length packet as needed;
28553 + * @short_not_ok: When reading data, makes short packets be
28554 + * treated as errors (queue stops advancing till cleanup).
28555 + * @complete: Function called when request completes, so this request and
28556 + * its buffer may be re-used.
28557 + * Reads terminate with a short packet, or when the buffer fills,
28558 + * whichever comes first. When writes terminate, some data bytes
28559 + * will usually still be in flight (often in a hardware fifo).
28560 + * Errors (for reads or writes) stop the queue from advancing
28561 + * until the completion function returns, so that any transfers
28562 + * invalidated by the error may first be dequeued.
28563 + * @context: For use by the completion callback
28564 + * @list: For use by the gadget driver.
28565 + * @status: Reports completion code, zero or a negative errno.
28566 + * Normally, faults block the transfer queue from advancing until
28567 + * the completion callback returns.
28568 + * Code "-ESHUTDOWN" indicates completion caused by device disconnect,
28569 + * or when the driver disabled the endpoint.
28570 + * @actual: Reports bytes transferred to/from the buffer. For reads (OUT
28571 + * transfers) this may be less than the requested length. If the
28572 + * short_not_ok flag is set, short reads are treated as errors
28573 + * even when status otherwise indicates successful completion.
28574 + * Note that for writes (IN transfers) some data bytes may still
28575 + * reside in a device-side FIFO when the request is reported as
28576 + * complete.
28578 + * These are allocated/freed through the endpoint they're used with. The
28579 + * hardware's driver can add extra per-request data to the memory it returns,
28580 + * which often avoids separate memory allocations (potential failures),
28581 + * later when the request is queued.
28583 + * Request flags affect request handling, such as whether a zero length
28584 + * packet is written (the "zero" flag), whether a short read should be
28585 + * treated as an error (blocking request queue advance, the "short_not_ok"
28586 + * flag), or hinting that an interrupt is not required (the "no_interrupt"
28587 + * flag, for use with deep request queues).
28589 + * Bulk endpoints can use any size buffers, and can also be used for interrupt
28590 + * transfers. interrupt-only endpoints can be much less functional.
28591 + */
28592 + // NOTE this is analagous to 'struct urb' on the host side,
28593 + // except that it's thinner and promotes more pre-allocation.
28595 +struct usb_request {
28596 + void *buf;
28597 + unsigned length;
28598 + dma_addr_t dma;
28600 + unsigned no_interrupt:1;
28601 + unsigned zero:1;
28602 + unsigned short_not_ok:1;
28604 + void (*complete)(struct usb_ep *ep,
28605 + struct usb_request *req);
28606 + void *context;
28607 + struct list_head list;
28609 + int status;
28610 + unsigned actual;
28613 +/*-------------------------------------------------------------------------*/
28615 +/* endpoint-specific parts of the api to the usb controller hardware.
28616 + * unlike the urb model, (de)multiplexing layers are not required.
28617 + * (so this api could slash overhead if used on the host side...)
28619 + * note that device side usb controllers commonly differ in how many
28620 + * endpoints they support, as well as their capabilities.
28621 + */
28622 +struct usb_ep_ops {
28623 + int (*enable) (struct usb_ep *ep,
28624 + const struct usb_endpoint_descriptor *desc);
28625 + int (*disable) (struct usb_ep *ep);
28627 + struct usb_request *(*alloc_request) (struct usb_ep *ep,
28628 + int gfp_flags);
28629 + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
28631 + void *(*alloc_buffer) (struct usb_ep *ep, unsigned bytes,
28632 + dma_addr_t *dma, int gfp_flags);
28633 + void (*free_buffer) (struct usb_ep *ep, void *buf, dma_addr_t dma,
28634 + unsigned bytes);
28635 + // NOTE: on 2.6, drivers may also use dma_map() and
28636 + // dma_sync_single_*() to directly manage dma overhead.
28638 + int (*queue) (struct usb_ep *ep, struct usb_request *req,
28639 + int gfp_flags);
28640 + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
28642 + int (*set_halt) (struct usb_ep *ep, int value);
28643 + int (*fifo_status) (struct usb_ep *ep);
28644 + void (*fifo_flush) (struct usb_ep *ep);
28647 +/**
28648 + * struct usb_ep - device side representation of USB endpoint
28649 + * @name:identifier for the endpoint, such as "ep-a" or "ep9in-bulk"
28650 + * @ops: Function pointers used to access hardware-specific operations.
28651 + * @ep_list:the gadget's ep_list holds all of its endpoints
28652 + * @maxpacket:The maximum packet size used on this endpoint. The initial
28653 + * value can sometimes be reduced (hardware allowing), according to
28654 + * the endpoint descriptor used to configure the endpoint.
28655 + * @driver_data:for use by the gadget driver. all other fields are
28656 + * read-only to gadget drivers.
28658 + * the bus controller driver lists all the general purpose endpoints in
28659 + * gadget->ep_list. the control endpoint (gadget->ep0) is not in that list,
28660 + * and is accessed only in response to a driver setup() callback.
28661 + */
28662 +struct usb_ep {
28663 + void *driver_data;
28665 + const char *name;
28666 + const struct usb_ep_ops *ops;
28667 + struct list_head ep_list;
28668 + unsigned maxpacket:16;
28671 +/*-------------------------------------------------------------------------*/
28673 +/**
28674 + * usb_ep_enable - configure endpoint, making it usable
28675 + * @ep:the endpoint being configured. may not be the endpoint named "ep0".
28676 + * drivers discover endpoints through the ep_list of a usb_gadget.
28677 + * @desc:descriptor for desired behavior. caller guarantees this pointer
28678 + * remains valid until the endpoint is disabled; the data byte order
28679 + * is little-endian (usb-standard).
28681 + * when configurations are set, or when interface settings change, the driver
28682 + * will enable or disable the relevant endpoints. while it is enabled, an
28683 + * endpoint may be used for i/o until the driver receives a disconnect() from
28684 + * the host or until the endpoint is disabled.
28686 + * the ep0 implementation (which calls this routine) must ensure that the
28687 + * hardware capabilities of each endpoint match the descriptor provided
28688 + * for it. for example, an endpoint named "ep2in-bulk" would be usable
28689 + * for interrupt transfers as well as bulk, but it likely couldn't be used
28690 + * for iso transfers or for endpoint 14. some endpoints are fully
28691 + * configurable, with more generic names like "ep-a". (remember that for
28692 + * USB, "in" means "towards the USB master".)
28694 + * returns zero, or a negative error code.
28695 + */
28696 +static inline int
28697 +usb_ep_enable (struct usb_ep *ep, const struct usb_endpoint_descriptor *desc)
28699 + return ep->ops->enable (ep, desc);
28702 +/**
28703 + * usb_ep_disable - endpoint is no longer usable
28704 + * @ep:the endpoint being unconfigured. may not be the endpoint named "ep0".
28706 + * no other task may be using this endpoint when this is called.
28707 + * any pending and uncompleted requests will complete with status
28708 + * indicating disconnect (-ESHUTDOWN) before this call returns.
28709 + * gadget drivers must call usb_ep_enable() again before queueing
28710 + * requests to the endpoint.
28712 + * returns zero, or a negative error code.
28713 + */
28714 +static inline int
28715 +usb_ep_disable (struct usb_ep *ep)
28717 + return ep->ops->disable (ep);
28720 +/**
28721 + * usb_ep_alloc_request - allocate a request object to use with this endpoint
28722 + * @ep:the endpoint to be used with with the request
28723 + * @gfp_flags:GFP_* flags to use
28725 + * Request objects must be allocated with this call, since they normally
28726 + * need controller-specific setup and may even need endpoint-specific
28727 + * resources such as allocation of DMA descriptors.
28728 + * Requests may be submitted with usb_ep_queue(), and receive a single
28729 + * completion callback. Free requests with usb_ep_free_request(), when
28730 + * they are no longer needed.
28732 + * Returns the request, or null if one could not be allocated.
28733 + */
28734 +static inline struct usb_request *
28735 +usb_ep_alloc_request (struct usb_ep *ep, int gfp_flags)
28737 + return ep->ops->alloc_request (ep, gfp_flags);
28740 +/**
28741 + * usb_ep_free_request - frees a request object
28742 + * @ep:the endpoint associated with the request
28743 + * @req:the request being freed
28745 + * Reverses the effect of usb_ep_alloc_request().
28746 + * Caller guarantees the request is not queued, and that it will
28747 + * no longer be requeued (or otherwise used).
28748 + */
28749 +static inline void
28750 +usb_ep_free_request (struct usb_ep *ep, struct usb_request *req)
28752 + ep->ops->free_request (ep, req);
28755 +/**
28756 + * usb_ep_alloc_buffer - allocate an I/O buffer
28757 + * @ep:the endpoint associated with the buffer
28758 + * @len:length of the desired buffer
28759 + * @dma:pointer to the buffer's DMA address; must be valid
28760 + * @gfp_flags:GFP_* flags to use
28762 + * Returns a new buffer, or null if one could not be allocated.
28763 + * The buffer is suitably aligned for dma, if that endpoint uses DMA,
28764 + * and the caller won't have to care about dma-inconsistency
28765 + * or any hidden "bounce buffer" mechanism. No additional per-request
28766 + * DMA mapping will be required for such buffers.
28767 + * Free it later with usb_ep_free_buffer().
28769 + * You don't need to use this call to allocate I/O buffers unless you
28770 + * want to make sure drivers don't incur costs for such "bounce buffer"
28771 + * copies or per-request DMA mappings.
28772 + */
28773 +static inline void *
28774 +usb_ep_alloc_buffer (struct usb_ep *ep, unsigned len, dma_addr_t *dma,
28775 + int gfp_flags)
28777 + return ep->ops->alloc_buffer (ep, len, dma, gfp_flags);
28780 +/**
28781 + * usb_ep_free_buffer - frees an i/o buffer
28782 + * @ep:the endpoint associated with the buffer
28783 + * @buf:CPU view address of the buffer
28784 + * @dma:the buffer's DMA address
28785 + * @len:length of the buffer
28787 + * reverses the effect of usb_ep_alloc_buffer().
28788 + * caller guarantees the buffer will no longer be accessed
28789 + */
28790 +static inline void
28791 +usb_ep_free_buffer (struct usb_ep *ep, void *buf, dma_addr_t dma, unsigned len)
28793 + ep->ops->free_buffer (ep, buf, dma, len);
28796 +/**
28797 + * usb_ep_queue - queues (submits) an I/O request to an endpoint.
28798 + * @ep:the endpoint associated with the request
28799 + * @req:the request being submitted
28800 + * @gfp_flags: GFP_* flags to use in case the lower level driver couldn't
28801 + * pre-allocate all necessary memory with the request.
28803 + * This tells the device controller to perform the specified request through
28804 + * that endpoint (reading or writing a buffer). When the request completes,
28805 + * including being canceled by usb_ep_dequeue(), the request's completion
28806 + * routine is called to return the request to the driver. Any endpoint
28807 + * (except control endpoints like ep0) may have more than one transfer
28808 + * request queued; they complete in FIFO order. Once a gadget driver
28809 + * submits a request, that request may not be examined or modified until it
28810 + * is given back to that driver through the completion callback.
28812 + * Each request is turned into one or more packets. The controller driver
28813 + * never merges adjacent requests into the same packet. OUT transfers
28814 + * will sometimes use data that's already buffered in the hardware.
28815 + * Drivers can rely on the fact that the first byte of the request's buffer
28816 + * always corresponds to the first byte of some USB packet, for both
28817 + * IN and OUT transfers.
28819 + * Bulk endpoints can queue any amount of data; the transfer is packetized
28820 + * automatically. The last packet will be short if the request doesn't fill it
28821 + * out completely. Zero length packets (ZLPs) should be avoided in portable
28822 + * protocols since not all usb hardware can successfully handle zero length
28823 + * packets. (ZLPs may be explicitly written, and may be implicitly written if
28824 + * the request 'zero' flag is set.) Bulk endpoints may also be used
28825 + * for interrupt transfers; but the reverse is not true, and some endpoints
28826 + * won't support every interrupt transfer. (Such as 768 byte packets.)
28828 + * Interrupt-only endpoints are less functional than bulk endpoints, for
28829 + * example by not supporting queueing or not handling buffers that are
28830 + * larger than the endpoint's maxpacket size. They may also treat data
28831 + * toggle differently.
28833 + * Control endpoints ... after getting a setup() callback, the driver queues
28834 + * one response (even if it would be zero length). That enables the
28835 + * status ack, after transfering data as specified in the response. Setup
28836 + * functions may return negative error codes to generate protocol stalls.
28837 + * (Note that some USB device controllers disallow protocol stall responses
28838 + * in some cases.) When control responses are deferred (the response is
28839 + * written after the setup callback returns), then usb_ep_set_halt() may be
28840 + * used on ep0 to trigger protocol stalls.
28842 + * For periodic endpoints, like interrupt or isochronous ones, the usb host
28843 + * arranges to poll once per interval, and the gadget driver usually will
28844 + * have queued some data to transfer at that time.
28846 + * Returns zero, or a negative error code. Endpoints that are not enabled
28847 + * report errors; errors will also be
28848 + * reported when the usb peripheral is disconnected.
28849 + */
28850 +static inline int
28851 +usb_ep_queue (struct usb_ep *ep, struct usb_request *req, int gfp_flags)
28853 + return ep->ops->queue (ep, req, gfp_flags);
28856 +/**
28857 + * usb_ep_dequeue - dequeues (cancels, unlinks) an I/O request from an endpoint
28858 + * @ep:the endpoint associated with the request
28859 + * @req:the request being canceled
28861 + * if the request is still active on the endpoint, it is dequeued and its
28862 + * completion routine is called (with status -ECONNRESET); else a negative
28863 + * error code is returned.
28865 + * note that some hardware can't clear out write fifos (to unlink the request
28866 + * at the head of the queue) except as part of disconnecting from usb. such
28867 + * restrictions prevent drivers from supporting configuration changes,
28868 + * even to configuration zero (a "chapter 9" requirement).
28869 + */
28870 +static inline int usb_ep_dequeue (struct usb_ep *ep, struct usb_request *req)
28872 + return ep->ops->dequeue (ep, req);
28875 +/**
28876 + * usb_ep_set_halt - sets the endpoint halt feature.
28877 + * @ep: the non-isochronous endpoint being stalled
28879 + * Use this to stall an endpoint, perhaps as an error report.
28880 + * Except for control endpoints,
28881 + * the endpoint stays halted (will not stream any data) until the host
28882 + * clears this feature; drivers may need to empty the endpoint's request
28883 + * queue first, to make sure no inappropriate transfers happen.
28885 + * Note that while an endpoint CLEAR_FEATURE will be invisible to the
28886 + * gadget driver, a SET_INTERFACE will not be. To reset endpoints for the
28887 + * current altsetting, see usb_ep_clear_halt(). When switching altsettings,
28888 + * it's simplest to use usb_ep_enable() or usb_ep_disable() for the endpoints.
28890 + * Returns zero, or a negative error code. On success, this call sets
28891 + * underlying hardware state that blocks data transfers.
28892 + * Attempts to halt IN endpoints will fail (returning -EAGAIN) if any
28893 + * transfer requests are still queued, or if the controller hardware
28894 + * (usually a FIFO) still holds bytes that the host hasn't collected.
28895 + */
28896 +static inline int
28897 +usb_ep_set_halt (struct usb_ep *ep)
28899 + return ep->ops->set_halt (ep, 1);
28902 +/**
28903 + * usb_ep_clear_halt - clears endpoint halt, and resets toggle
28904 + * @ep:the bulk or interrupt endpoint being reset
28906 + * Use this when responding to the standard usb "set interface" request,
28907 + * for endpoints that aren't reconfigured, after clearing any other state
28908 + * in the endpoint's i/o queue.
28910 + * Returns zero, or a negative error code. On success, this call clears
28911 + * the underlying hardware state reflecting endpoint halt and data toggle.
28912 + * Note that some hardware can't support this request (like pxa2xx_udc),
28913 + * and accordingly can't correctly implement interface altsettings.
28914 + */
28915 +static inline int
28916 +usb_ep_clear_halt (struct usb_ep *ep)
28918 + return ep->ops->set_halt (ep, 0);
28921 +/**
28922 + * usb_ep_fifo_status - returns number of bytes in fifo, or error
28923 + * @ep: the endpoint whose fifo status is being checked.
28925 + * FIFO endpoints may have "unclaimed data" in them in certain cases,
28926 + * such as after aborted transfers. Hosts may not have collected all
28927 + * the IN data written by the gadget driver (and reported by a request
28928 + * completion). The gadget driver may not have collected all the data
28929 + * written OUT to it by the host. Drivers that need precise handling for
28930 + * fault reporting or recovery may need to use this call.
28932 + * This returns the number of such bytes in the fifo, or a negative
28933 + * errno if the endpoint doesn't use a FIFO or doesn't support such
28934 + * precise handling.
28935 + */
28936 +static inline int
28937 +usb_ep_fifo_status (struct usb_ep *ep)
28939 + if (ep->ops->fifo_status)
28940 + return ep->ops->fifo_status (ep);
28941 + else
28942 + return -EOPNOTSUPP;
28945 +/**
28946 + * usb_ep_fifo_flush - flushes contents of a fifo
28947 + * @ep: the endpoint whose fifo is being flushed.
28949 + * This call may be used to flush the "unclaimed data" that may exist in
28950 + * an endpoint fifo after abnormal transaction terminations. The call
28951 + * must never be used except when endpoint is not being used for any
28952 + * protocol translation.
28953 + */
28954 +static inline void
28955 +usb_ep_fifo_flush (struct usb_ep *ep)
28957 + if (ep->ops->fifo_flush)
28958 + ep->ops->fifo_flush (ep);
28962 +/*-------------------------------------------------------------------------*/
28964 +struct usb_gadget;
28966 +/* the rest of the api to the controller hardware: device operations,
28967 + * which don't involve endpoints (or i/o).
28968 + */
28969 +struct usb_gadget_ops {
28970 + int (*get_frame)(struct usb_gadget *);
28971 + int (*wakeup)(struct usb_gadget *);
28972 + int (*set_selfpowered) (struct usb_gadget *, int is_selfpowered);
28973 + int (*vbus_session) (struct usb_gadget *, int is_active);
28974 + int (*vbus_draw) (struct usb_gadget *, unsigned mA);
28975 + int (*pullup) (struct usb_gadget *, int is_on);
28976 + int (*ioctl)(struct usb_gadget *,
28977 + unsigned code, unsigned long param);
28980 +/**
28981 + * struct usb_gadget - represents a usb slave device
28982 + * @ops: Function pointers used to access hardware-specific operations.
28983 + * @ep0: Endpoint zero, used when reading or writing responses to
28984 + * driver setup() requests
28985 + * @ep_list: List of other endpoints supported by the device.
28986 + * @speed: Speed of current connection to USB host.
28987 + * @is_dualspeed: True if the controller supports both high and full speed
28988 + * operation. If it does, the gadget driver must also support both.
28989 + * @is_otg: True if the USB device port uses a Mini-AB jack, so that the
28990 + * gadget driver must provide a USB OTG descriptor.
28991 + * @is_a_peripheral: False unless is_otg, the "A" end of a USB cable
28992 + * is in the Mini-AB jack, and HNP has been used to switch roles
28993 + * so that the "A" device currently acts as A-Peripheral, not A-Host.
28994 + * @a_hnp_support: OTG device feature flag, indicating that the A-Host
28995 + * supports HNP at this port.
28996 + * @a_alt_hnp_support: OTG device feature flag, indicating that the A-Host
28997 + * only supports HNP on a different root port.
28998 + * @b_hnp_enable: OTG device feature flag, indicating that the A-Host
28999 + * enabled HNP support.
29000 + * @name: Identifies the controller hardware type. Used in diagnostics
29001 + * and sometimes configuration.
29002 + * @dev: Driver model state for this abstract device.
29004 + * Gadgets have a mostly-portable "gadget driver" implementing device
29005 + * functions, handling all usb configurations and interfaces. Gadget
29006 + * drivers talk to hardware-specific code indirectly, through ops vectors.
29007 + * That insulates the gadget driver from hardware details, and packages
29008 + * the hardware endpoints through generic i/o queues. The "usb_gadget"
29009 + * and "usb_ep" interfaces provide that insulation from the hardware.
29011 + * Except for the driver data, all fields in this structure are
29012 + * read-only to the gadget driver. That driver data is part of the
29013 + * "driver model" infrastructure in 2.6 (and later) kernels, and for
29014 + * earlier systems is grouped in a similar structure that's not known
29015 + * to the rest of the kernel.
29017 + * Values of the three OTG device feature flags are updated before the
29018 + * setup() call corresponding to USB_REQ_SET_CONFIGURATION, and before
29019 + * driver suspend() calls. They are valid only when is_otg, and when the
29020 + * device is acting as a B-Peripheral (so is_a_peripheral is false).
29021 + */
29022 +struct usb_gadget {
29023 + /* readonly to gadget driver */
29024 + const struct usb_gadget_ops *ops;
29025 + struct usb_ep *ep0;
29026 + struct list_head ep_list; /* of usb_ep */
29027 + enum usb_device_speed speed;
29028 + unsigned is_dualspeed:1;
29029 + unsigned is_otg:1;
29030 + unsigned is_a_peripheral:1;
29031 + unsigned b_hnp_enable:1;
29032 + unsigned a_hnp_support:1;
29033 + unsigned a_alt_hnp_support:1;
29034 + const char *name;
29036 + struct __gadget_device {
29037 + const char *bus_id;
29038 + void *driver_data;
29039 + } dev;
29042 +static inline void set_gadget_data (struct usb_gadget *gadget, void *data)
29043 + { gadget->dev.driver_data = data; }
29044 +static inline void *get_gadget_data (struct usb_gadget *gadget)
29045 + { return gadget->dev.driver_data; }
29048 +/* iterates the non-control endpoints; 'tmp' is a struct usb_ep pointer */
29049 +#define gadget_for_each_ep(tmp,gadget) \
29050 + list_for_each_entry(tmp, &(gadget)->ep_list, ep_list)
29052 +#ifndef list_for_each_entry
29053 +/* not available in 2.4.18 */
29054 +#define list_for_each_entry(pos, head, member) \
29055 + for (pos = list_entry((head)->next, typeof(*pos), member), \
29056 + prefetch(pos->member.next); \
29057 + &pos->member != (head); \
29058 + pos = list_entry(pos->member.next, typeof(*pos), member), \
29059 + prefetch(pos->member.next))
29060 +#endif
29063 +/**
29064 + * usb_gadget_frame_number - returns the current frame number
29065 + * @gadget: controller that reports the frame number
29067 + * Returns the usb frame number, normally eleven bits from a SOF packet,
29068 + * or negative errno if this device doesn't support this capability.
29069 + */
29070 +static inline int usb_gadget_frame_number (struct usb_gadget *gadget)
29072 + return gadget->ops->get_frame (gadget);
29075 +/**
29076 + * usb_gadget_wakeup - tries to wake up the host connected to this gadget
29077 + * @gadget: controller used to wake up the host
29079 + * Returns zero on success, else negative error code if the hardware
29080 + * doesn't support such attempts, or its support has not been enabled
29081 + * by the usb host. Drivers must return device descriptors that report
29082 + * their ability to support this, or hosts won't enable it.
29084 + * This may also try to use SRP to wake the host and start enumeration,
29085 + * even if OTG isn't otherwise in use. OTG devices may also start
29086 + * remote wakeup even when hosts don't explicitly enable it.
29087 + */
29088 +static inline int usb_gadget_wakeup (struct usb_gadget *gadget)
29090 + if (!gadget->ops->wakeup)
29091 + return -EOPNOTSUPP;
29092 + return gadget->ops->wakeup (gadget);
29095 +/**
29096 + * usb_gadget_set_selfpowered - sets the device selfpowered feature.
29097 + * @gadget:the device being declared as self-powered
29099 + * this affects the device status reported by the hardware driver
29100 + * to reflect that it now has a local power supply.
29102 + * returns zero on success, else negative errno.
29103 + */
29104 +static inline int
29105 +usb_gadget_set_selfpowered (struct usb_gadget *gadget)
29107 + if (!gadget->ops->set_selfpowered)
29108 + return -EOPNOTSUPP;
29109 + return gadget->ops->set_selfpowered (gadget, 1);
29112 +/**
29113 + * usb_gadget_clear_selfpowered - clear the device selfpowered feature.
29114 + * @gadget:the device being declared as bus-powered
29116 + * this affects the device status reported by the hardware driver.
29117 + * some hardware may not support bus-powered operation, in which
29118 + * case this feature's value can never change.
29120 + * returns zero on success, else negative errno.
29121 + */
29122 +static inline int
29123 +usb_gadget_clear_selfpowered (struct usb_gadget *gadget)
29125 + if (!gadget->ops->set_selfpowered)
29126 + return -EOPNOTSUPP;
29127 + return gadget->ops->set_selfpowered (gadget, 0);
29130 +/**
29131 + * usb_gadget_vbus_connect - Notify controller that VBUS is powered
29132 + * @gadget:The device which now has VBUS power.
29134 + * This call is used by a driver for an external transceiver (or GPIO)
29135 + * that detects a VBUS power session starting. Common responses include
29136 + * resuming the controller, activating the D+ (or D-) pullup to let the
29137 + * host detect that a USB device is attached, and starting to draw power
29138 + * (8mA or possibly more, especially after SET_CONFIGURATION).
29140 + * Returns zero on success, else negative errno.
29141 + */
29142 +static inline int
29143 +usb_gadget_vbus_connect(struct usb_gadget *gadget)
29145 + if (!gadget->ops->vbus_session)
29146 + return -EOPNOTSUPP;
29147 + return gadget->ops->vbus_session (gadget, 1);
29150 +/**
29151 + * usb_gadget_vbus_draw - constrain controller's VBUS power usage
29152 + * @gadget:The device whose VBUS usage is being described
29153 + * @mA:How much current to draw, in milliAmperes. This should be twice
29154 + * the value listed in the configuration descriptor bMaxPower field.
29156 + * This call is used by gadget drivers during SET_CONFIGURATION calls,
29157 + * reporting how much power the device may consume. For example, this
29158 + * could affect how quickly batteries are recharged.
29160 + * Returns zero on success, else negative errno.
29161 + */
29162 +static inline int
29163 +usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
29165 + if (!gadget->ops->vbus_draw)
29166 + return -EOPNOTSUPP;
29167 + return gadget->ops->vbus_draw (gadget, mA);
29170 +/**
29171 + * usb_gadget_vbus_disconnect - notify controller about VBUS session end
29172 + * @gadget:the device whose VBUS supply is being described
29174 + * This call is used by a driver for an external transceiver (or GPIO)
29175 + * that detects a VBUS power session ending. Common responses include
29176 + * reversing everything done in usb_gadget_vbus_connect().
29178 + * Returns zero on success, else negative errno.
29179 + */
29180 +static inline int
29181 +usb_gadget_vbus_disconnect(struct usb_gadget *gadget)
29183 + if (!gadget->ops->vbus_session)
29184 + return -EOPNOTSUPP;
29185 + return gadget->ops->vbus_session (gadget, 0);
29188 +/**
29189 + * usb_gadget_connect - software-controlled connect to USB host
29190 + * @gadget:the peripheral being connected
29192 + * Enables the D+ (or potentially D-) pullup. The host will start
29193 + * enumerating this gadget when the pullup is active and a VBUS session
29194 + * is active (the link is powered). This pullup is always enabled unless
29195 + * usb_gadget_disconnect() has been used to disable it.
29197 + * Returns zero on success, else negative errno.
29198 + */
29199 +static inline int
29200 +usb_gadget_connect (struct usb_gadget *gadget)
29202 + if (!gadget->ops->pullup)
29203 + return -EOPNOTSUPP;
29204 + return gadget->ops->pullup (gadget, 1);
29207 +/**
29208 + * usb_gadget_disconnect - software-controlled disconnect from USB host
29209 + * @gadget:the peripheral being disconnected
29211 + * Disables the D+ (or potentially D-) pullup, which the host may see
29212 + * as a disconnect (when a VBUS session is active). Not all systems
29213 + * support software pullup controls.
29215 + * This routine may be used during the gadget driver bind() call to prevent
29216 + * the peripheral from ever being visible to the USB host, unless later
29217 + * usb_gadget_connect() is called. For example, user mode components may
29218 + * need to be activated before the system can talk to hosts.
29220 + * Returns zero on success, else negative errno.
29221 + */
29222 +static inline int
29223 +usb_gadget_disconnect (struct usb_gadget *gadget)
29225 + if (!gadget->ops->pullup)
29226 + return -EOPNOTSUPP;
29227 + return gadget->ops->pullup (gadget, 0);
29232 +/*-------------------------------------------------------------------------*/
29234 +/**
29235 + * struct usb_gadget_driver - driver for usb 'slave' devices
29236 + * @function: String describing the gadget's function
29237 + * @speed: Highest speed the driver handles.
29238 + * @bind: Invoked when the driver is bound to a gadget, usually
29239 + * after registering the driver.
29240 + * At that point, ep0 is fully initialized, and ep_list holds
29241 + * the currently-available endpoints.
29242 + * Called in a context that permits sleeping.
29243 + * @setup: Invoked for ep0 control requests that aren't handled by
29244 + * the hardware level driver. Most calls must be handled by
29245 + * the gadget driver, including descriptor and configuration
29246 + * management. The 16 bit members of the setup data are in
29247 + * cpu order. Called in_interrupt; this may not sleep. Driver
29248 + * queues a response to ep0, or returns negative to stall.
29249 + * @disconnect: Invoked after all transfers have been stopped,
29250 + * when the host is disconnected. May be called in_interrupt; this
29251 + * may not sleep. Some devices can't detect disconnect, so this might
29252 + * not be called except as part of controller shutdown.
29253 + * @unbind: Invoked when the driver is unbound from a gadget,
29254 + * usually from rmmod (after a disconnect is reported).
29255 + * Called in a context that permits sleeping.
29256 + * @suspend: Invoked on USB suspend. May be called in_interrupt.
29257 + * @resume: Invoked on USB resume. May be called in_interrupt.
29258 + * @driver: Driver model state for this driver.
29260 + * Devices are disabled till a gadget driver successfully bind()s, which
29261 + * means the driver will handle setup() requests needed to enumerate (and
29262 + * meet "chapter 9" requirements) then do some useful work.
29264 + * If gadget->is_otg is true, the gadget driver must provide an OTG
29265 + * descriptor during enumeration, or else fail the bind() call. In such
29266 + * cases, no USB traffic may flow until both bind() returns without
29267 + * having called usb_gadget_disconnect(), and the USB host stack has
29268 + * initialized.
29270 + * Drivers use hardware-specific knowledge to configure the usb hardware.
29271 + * endpoint addressing is only one of several hardware characteristics that
29272 + * are in descriptors the ep0 implementation returns from setup() calls.
29274 + * Except for ep0 implementation, most driver code shouldn't need change to
29275 + * run on top of different usb controllers. It'll use endpoints set up by
29276 + * that ep0 implementation.
29278 + * The usb controller driver handles a few standard usb requests. Those
29279 + * include set_address, and feature flags for devices, interfaces, and
29280 + * endpoints (the get_status, set_feature, and clear_feature requests).
29282 + * Accordingly, the driver's setup() callback must always implement all
29283 + * get_descriptor requests, returning at least a device descriptor and
29284 + * a configuration descriptor. Drivers must make sure the endpoint
29285 + * descriptors match any hardware constraints. Some hardware also constrains
29286 + * other descriptors. (The pxa250 allows only configurations 1, 2, or 3).
29288 + * The driver's setup() callback must also implement set_configuration,
29289 + * and should also implement set_interface, get_configuration, and
29290 + * get_interface. Setting a configuration (or interface) is where
29291 + * endpoints should be activated or (config 0) shut down.
29293 + * (Note that only the default control endpoint is supported. Neither
29294 + * hosts nor devices generally support control traffic except to ep0.)
29296 + * Most devices will ignore USB suspend/resume operations, and so will
29297 + * not provide those callbacks. However, some may need to change modes
29298 + * when the host is not longer directing those activities. For example,
29299 + * local controls (buttons, dials, etc) may need to be re-enabled since
29300 + * the (remote) host can't do that any longer; or an error state might
29301 + * be cleared, to make the device behave identically whether or not
29302 + * power is maintained.
29303 + */
29304 +struct usb_gadget_driver {
29305 + char *function;
29306 + enum usb_device_speed speed;
29307 + int (*bind)(struct usb_gadget *);
29308 + void (*unbind)(struct usb_gadget *);
29309 + int (*setup)(struct usb_gadget *,
29310 + const struct usb_ctrlrequest *);
29311 + void (*disconnect)(struct usb_gadget *);
29312 + void (*suspend)(struct usb_gadget *);
29313 + void (*resume)(struct usb_gadget *);
29315 + // FIXME support safe rmmod
29316 + struct __gadget_driver {
29317 + const char *name;
29318 + void *driver_data;
29319 + } driver;
29324 +/*-------------------------------------------------------------------------*/
29326 +/* driver modules register and unregister, as usual.
29327 + * these calls must be made in a context that can sleep.
29329 + * these will usually be implemented directly by the hardware-dependent
29330 + * usb bus interface driver, which will only support a single driver.
29331 + */
29333 +/**
29334 + * usb_gadget_register_driver - register a gadget driver
29335 + * @driver:the driver being registered
29337 + * Call this in your gadget driver's module initialization function,
29338 + * to tell the underlying usb controller driver about your driver.
29339 + * The driver's bind() function will be called to bind it to a
29340 + * gadget. This function must be called in a context that can sleep.
29341 + */
29342 +int usb_gadget_register_driver (struct usb_gadget_driver *driver);
29344 +/**
29345 + * usb_gadget_unregister_driver - unregister a gadget driver
29346 + * @driver:the driver being unregistered
29348 + * Call this in your gadget driver's module cleanup function,
29349 + * to tell the underlying usb controller that your driver is
29350 + * going away. If the controller is connected to a USB host,
29351 + * it will first disconnect(). The driver is also requested
29352 + * to unbind() and clean up any device state, before this procedure
29353 + * finally returns.
29354 + * This function must be called in a context that can sleep.
29355 + */
29356 +int usb_gadget_unregister_driver (struct usb_gadget_driver *driver);
29358 +/*-------------------------------------------------------------------------*/
29360 +/* utility to simplify dealing with string descriptors */
29362 +/**
29363 + * struct usb_string - wraps a C string and its USB id
29364 + * @id:the (nonzero) ID for this string
29365 + * @s:the string, in UTF-8 encoding
29367 + * If you're using usb_gadget_get_string(), use this to wrap a string
29368 + * together with its ID.
29369 + */
29370 +struct usb_string {
29371 + u8 id;
29372 + const char *s;
29375 +/**
29376 + * struct usb_gadget_strings - a set of USB strings in a given language
29377 + * @language:identifies the strings' language (0x0409 for en-us)
29378 + * @strings:array of strings with their ids
29380 + * If you're using usb_gadget_get_string(), use this to wrap all the
29381 + * strings for a given language.
29382 + */
29383 +struct usb_gadget_strings {
29384 + u16 language; /* 0x0409 for en-us */
29385 + struct usb_string *strings;
29388 +/* put descriptor for string with that id into buf (buflen >= 256) */
29389 +int usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf);
29391 +/*-------------------------------------------------------------------------*/
29393 +/* utility to simplify managing config descriptors */
29395 +/* write vector of descriptors into buffer */
29396 +int usb_descriptor_fillbuf(void *, unsigned,
29397 + const struct usb_descriptor_header **);
29399 +/* build config descriptor from single descriptor vector */
29400 +int usb_gadget_config_buf(const struct usb_config_descriptor *config,
29401 + void *buf, unsigned buflen, const struct usb_descriptor_header **desc);
29403 +/*-------------------------------------------------------------------------*/
29405 +/* utility wrapping a simple endpoint selection policy */
29407 +extern struct usb_ep *usb_ep_autoconfig (struct usb_gadget *,
29408 + struct usb_endpoint_descriptor *) __init;
29410 +extern void usb_ep_autoconfig_reset (struct usb_gadget *) __init;
29412 +#endif /* __KERNEL__ */
29414 +#endif /* __LINUX_USB_GADGET_H */
29415 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/include/linux/usb_gadgetfs.h kernel/include/linux/usb_gadgetfs.h
29416 --- /tmp/kernel/include/linux/usb_gadgetfs.h 1970-01-01 01:00:00.000000000 +0100
29417 +++ kernel/include/linux/usb_gadgetfs.h 2005-04-22 17:53:19.370549935 +0200
29418 @@ -0,0 +1,75 @@
29420 +#include <asm/types.h>
29421 +#include <asm/ioctl.h>
29423 +#include <linux/usb_ch9.h>
29426 + * Filesystem based user-mode API to USB Gadget controller hardware
29428 + * Almost everything can be done with only read and write operations,
29429 + * on endpoint files found in one directory. They are configured by
29430 + * writing descriptors, and then may be used for normal stream style
29431 + * i/o requests. When ep0 is configured, the device can enumerate;
29432 + * when it's closed, the device disconnects from usb.
29434 + * Configuration and device descriptors get written to /dev/gadget/$CHIP,
29435 + * which may then be used to read usb_gadgetfs_event structs. The driver
29436 + * may activate endpoints as it handles SET_CONFIGURATION setup events,
29437 + * or earlier; writing endpoint descriptors to /dev/gadget/$ENDPOINT
29438 + * then performing data transfers by reading or writing.
29439 + */
29442 + * Events are delivered on the ep0 file descriptor, if the user mode driver
29443 + * reads from this file descriptor after writing the descriptors. Don't
29444 + * stop polling this descriptor, if you write that kind of driver.
29445 + */
29447 +enum usb_gadgetfs_event_type {
29448 + GADGETFS_NOP = 0,
29450 + GADGETFS_CONNECT,
29451 + GADGETFS_DISCONNECT,
29452 + GADGETFS_SETUP,
29453 + GADGETFS_SUSPEND,
29454 + // and likely more !
29457 +struct usb_gadgetfs_event {
29458 + enum usb_gadgetfs_event_type type;
29459 + union {
29460 + // NOP, DISCONNECT, SUSPEND: nothing
29461 + // ... some hardware can't report disconnection
29463 + // CONNECT: just the speed
29464 + enum usb_device_speed speed;
29466 + // SETUP: packet; DATA phase i/o precedes next event
29467 + // (setup.bmRequestType & USB_DIR_IN) flags direction
29468 + // ... includes SET_CONFIGURATION, SET_INTERFACE
29469 + struct usb_ctrlrequest setup;
29470 + } u;
29474 +/* endpoint ioctls */
29476 +/* IN transfers may be reported to the gadget driver as complete
29477 + * when the fifo is loaded, before the host reads the data;
29478 + * OUT transfers may be reported to the host's "client" driver as
29479 + * complete when they're sitting in the FIFO unread.
29480 + * THIS returns how many bytes are "unclaimed" in the endpoint fifo
29481 + * (needed for precise fault handling, when the hardware allows it)
29482 + */
29483 +#define GADGETFS_FIFO_STATUS _IO('g',1)
29485 +/* discards any unclaimed data in the fifo. */
29486 +#define GADGETFS_FIFO_FLUSH _IO('g',2)
29488 +/* resets endpoint halt+toggle; used to implement set_interface.
29489 + * some hardware (like pxa2xx) can't support this.
29490 + */
29491 +#define GADGETFS_CLEAR_HALT _IO('g',3)
29494 diff -x '*~' -x '.*' -r -N -u /tmp/kernel/include/linux/usb_scanner_ioctl.h kernel/include/linux/usb_scanner_ioctl.h
29495 --- /tmp/kernel/include/linux/usb_scanner_ioctl.h 1970-01-01 01:00:00.000000000 +0100
29496 +++ kernel/include/linux/usb_scanner_ioctl.h 2005-04-22 17:53:19.372549610 +0200
29497 @@ -0,0 +1,9 @@
29498 +/* USB Scanner IOCTLS */
29500 +/* read vendor and product IDs from the scanner */
29501 +#define SCANNER_IOCTL_VENDOR _IOR('U', 0x20, int)
29502 +#define SCANNER_IOCTL_PRODUCT _IOR('U', 0x21, int)
29503 +/* send/recv a control message to the scanner */
29504 +#define SCANNER_IOCTL_CTRLMSG _IOWR('U', 0x22, struct usb_ctrlrequest )