1 Index: linux-2.6.21.7/drivers/usb/gadget/Kconfig
2 ===================================================================
3 --- linux-2.6.21.7.orig/drivers/usb/gadget/Kconfig
4 +++ linux-2.6.21.7/drivers/usb/gadget/Kconfig
5 @@ -129,6 +129,28 @@ config USB_PXA2XX_SMALL
7 default y if USB_G_SERIAL
9 +config USB_GADGET_PXA27X
11 + depends on ARCH_PXA && PXA27x
13 + Intel's PXA 27x series XScale ARM-5TE processors include
14 + an integrated full speed USB 1.1 device controller.
16 + Say "y" to link the driver statically, or "m" to build a
17 + dynamically linked module called "pxa27x_udc" and force all
18 + gadget drivers to also be dynamically linked.
22 + depends on USB_GADGET_PXA27X
24 + select USB_GADGET_SELECTED
26 +config USB_PXA27X_DMA
27 + bool # "Use DMA support"
28 + depends on USB_GADGET_PXA27X
31 config USB_GADGET_GOKU
32 boolean "Toshiba TC86C001 'Goku-S'"
34 Index: linux-2.6.21.7/drivers/usb/gadget/Makefile
35 ===================================================================
36 --- linux-2.6.21.7.orig/drivers/usb/gadget/Makefile
37 +++ linux-2.6.21.7/drivers/usb/gadget/Makefile
38 @@ -7,6 +7,7 @@ obj-$(CONFIG_USB_PXA2XX) += pxa2xx_udc.o
39 obj-$(CONFIG_USB_GOKU) += goku_udc.o
40 obj-$(CONFIG_USB_OMAP) += omap_udc.o
41 obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o
42 +obj-$(CONFIG_USB_PXA27X) += pxa27x_udc.o
43 obj-$(CONFIG_USB_AT91) += at91_udc.o
44 obj-$(CONFIG_USB_GADGET_GUMSTIX) += gumstix_gadget.o
46 Index: linux-2.6.21.7/drivers/usb/gadget/pxa27x_udc.c
47 ===================================================================
49 +++ linux-2.6.21.7/drivers/usb/gadget/pxa27x_udc.c
52 + * linux/drivers/usb/gadget/pxa27x_udc.c
53 + * Intel PXA2xx and IXP4xx on-chip full speed USB device controllers
55 + * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
56 + * Copyright (C) 2003 Robert Schwebel, Pengutronix
57 + * Copyright (C) 2003 Benedikt Spranger, Pengutronix
58 + * Copyright (C) 2003 David Brownell
59 + * Copyright (C) 2003 Joshua Wise
60 + * Copyright (C) 2004 Intel Corporation
61 + * Copyright (C) 2005 SDG Systems, LLC (Aric Blumer)
63 + * This program is free software; you can redistribute it and/or modify
64 + * it under the terms of the GNU General Public License as published by
65 + * the Free Software Foundation; either version 2 of the License, or
66 + * (at your option) any later version.
68 + * This program is distributed in the hope that it will be useful,
69 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
70 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71 + * GNU General Public License for more details.
73 + * You should have received a copy of the GNU General Public License
74 + * along with this program; if not, write to the Free Software
75 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
81 + //#define VERBOSE DBG_VERBOSE
83 +#include <linux/module.h>
84 +#include <linux/kernel.h>
85 +#include <linux/ioport.h>
86 +#include <linux/types.h>
87 +#include <linux/version.h>
88 +#include <linux/errno.h>
89 +#include <linux/delay.h>
90 +#include <linux/sched.h>
91 +#include <linux/slab.h>
92 +#include <linux/init.h>
93 +#include <linux/timer.h>
94 +#include <linux/list.h>
95 +#include <linux/interrupt.h>
96 +#include <linux/proc_fs.h>
97 +#include <linux/mm.h>
98 +#include <linux/platform_device.h>
99 +#include <linux/dma-mapping.h>
100 +#include <linux/irq.h>
102 +#include <asm/byteorder.h>
103 +#include <asm/dma.h>
105 +#include <asm/system.h>
106 +#include <asm/mach-types.h>
107 +#include <asm/unaligned.h>
108 +#include <asm/hardware.h>
109 +#include <asm/mach/irq.h>
110 +#include <asm/arch/pxa-regs.h>
112 +#include <linux/usb/ch9.h>
113 +#include <linux/usb_gadget.h>
115 +#include <asm/arch/udc.h>
119 + * This driver handles the USB Device Controller (UDC) in Intel's PXA 27777777x
120 + * series processors.
121 + * Such controller drivers work with a gadget driver. The gadget driver
122 + * returns descriptors, implements configuration and data protocols used
123 + * by the host to interact with this device, and allocates endpoints to
124 + * the different protocol interfaces. The controller driver virtualizes
125 + * usb hardware so that the gadget drivers will be more portable.
127 + * This UDC hardware wants to implement a bit too much USB protocol, so
128 + * it constrains the sorts of USB configuration change events that work.
129 + * The errata for these chips are misleading; some "fixed" bugs from
130 + * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
133 +#define DRIVER_VERSION "21-Jul-2005"
134 +#define DRIVER_DESC "PXA 27x USB Device Controller driver"
137 +static const char driver_name [] = "pxa27x_udc";
139 +static const char ep0name [] = "ep0";
143 +//#define DISABLE_TEST_MODE
145 +#ifdef CONFIG_PROC_FS
146 +#define UDC_PROC_FILE
149 +#include "pxa27x_udc.h"
152 +#ifdef CONFIG_EMBEDDED
153 +/* few strings, and little code to use them */
155 +#undef UDC_PROC_FILE
160 +static int use_dma = 1;
161 +module_param(use_dma, bool, 0);
162 +MODULE_PARM_DESC (use_dma, "true to use dma");
164 +static void dma_nodesc_handler (int dmach, void *_ep);
165 +static void kick_dma(struct pxa27x_ep *ep, struct pxa27x_request *req);
167 +#define DMASTR " (dma support)"
169 +#else /* !USE_DMA */
170 +#define DMASTR " (pio only)"
173 +#ifdef CONFIG_USB_PXA27X_SMALL
174 +#define SIZE_STR " (small)"
179 +#ifdef DISABLE_TEST_MODE
180 +/* (mode == 0) == no undocumented chip tweaks
181 + * (mode & 1) == double buffer bulk IN
182 + * (mode & 2) == double buffer bulk OUT
183 + * ... so mode = 3 (or 7, 15, etc) does it for both
185 +static ushort fifo_mode = 0;
186 +module_param(fifo_mode, ushort, 0);
187 +MODULE_PARM_DESC (fifo_mode, "pxa27x udc fifo mode");
190 +#define UDCISR0_IR0 0x3
191 +#define UDCISR_INT_MASK (UDC_INT_FIFOERROR | UDC_INT_PACKETCMP)
192 +#define UDCICR_INT_MASK UDCISR_INT_MASK
194 +#define UDCCSR_MASK (UDCCSR_FST | UDCCSR_DME)
195 +/* ---------------------------------------------------------------------------
196 + * endpoint related parts of the api to the usb controller hardware,
197 + * used by gadget driver; and the inner talker-to-hardware core.
198 + * ---------------------------------------------------------------------------
201 +static void pxa27x_ep_fifo_flush (struct usb_ep *ep);
202 +static void nuke (struct pxa27x_ep *, int status);
204 +static void pio_irq_enable(int ep_num)
207 + UDCICR0 |= 3 << (ep_num * 2);
210 + UDCICR1 |= 3 << (ep_num * 2);
214 +static void pio_irq_disable(int ep_num)
218 + UDCICR0 &= ~(3 << (ep_num * 2));
221 + UDCICR1 &= ~(3 << (ep_num * 2));
225 +/* The UDCCR reg contains mask and interrupt status bits,
226 + * so using '|=' isn't safe as it may ack an interrupt.
228 +#define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_UDE)
230 +static inline void udc_set_mask_UDCCR(int mask)
232 + UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
235 +static inline void udc_clear_mask_UDCCR(int mask)
237 + UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
240 +static inline void udc_ack_int_UDCCR(int mask)
242 + /* udccr contains the bits we dont want to change */
243 + __u32 udccr = UDCCR & UDCCR_MASK_BITS;
245 + UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
249 + * endpoint enable/disable
251 + * we need to verify the descriptors used to enable endpoints. since pxa27x
252 + * endpoint configurations are fixed, and are pretty much always enabled,
253 + * there's not a lot to manage here.
255 + * because pxa27x can't selectively initialize bulk (or interrupt) endpoints,
256 + * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
257 + * for a single interface (with only the default altsetting) and for gadget
258 + * drivers that don't halt endpoints (not reset by set_interface). that also
259 + * means that if you use ISO, you must violate the USB spec rule that all
260 + * iso endpoints must be in non-default altsettings.
262 +static int pxa27x_ep_enable (struct usb_ep *_ep,
263 + const struct usb_endpoint_descriptor *desc)
265 + struct pxa27x_ep *ep;
266 + struct pxa27x_udc *dev;
268 + ep = container_of (_ep, struct pxa27x_ep, ep);
269 + if (!_ep || !desc || _ep->name == ep0name
270 + || desc->bDescriptorType != USB_DT_ENDPOINT
271 + || ep->fifo_size < le16_to_cpu(desc->wMaxPacketSize)) {
272 + DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
276 + /* xfer types must match, except that interrupt ~= bulk */
277 + if( ep->ep_type != USB_ENDPOINT_XFER_BULK
278 + && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
279 + DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
283 + /* hardware _could_ do smaller, but driver doesn't */
284 + if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
285 + && le16_to_cpu (desc->wMaxPacketSize)
287 + || !desc->wMaxPacketSize) {
288 + DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
293 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
294 + DMSG("%s, bogus device state\n", __FUNCTION__);
301 + ep->pio_irqs = ep->dma_irqs = 0;
302 + ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
304 + /* flush fifo (mostly for OUT buffers) */
305 + pxa27x_ep_fifo_flush (_ep);
307 + /* ... reset halt state too, if we could ... */
310 + /* for (some) bulk and ISO endpoints, try to get a DMA channel and
311 + * bind it to the endpoint. otherwise use PIO.
313 + DMSG("%s: called attributes=%d\n", __FUNCTION__, ep->ep_type);
314 + switch (ep->ep_type) {
315 + case USB_ENDPOINT_XFER_ISOC:
316 + if (le16_to_cpu(desc->wMaxPacketSize) % 32)
319 + case USB_ENDPOINT_XFER_BULK:
320 + if (!use_dma || !ep->reg_drcmr)
322 + ep->dma = pxa_request_dma ((char *)_ep->name,
323 + (le16_to_cpu (desc->wMaxPacketSize) > 64)
324 + ? DMA_PRIO_MEDIUM /* some iso */
326 + dma_nodesc_handler, ep);
327 + if (ep->dma >= 0) {
328 + *ep->reg_drcmr = DRCMR_MAPVLD | ep->dma;
329 + DMSG("%s using dma%d\n", _ep->name, ep->dma);
335 + DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
339 +static int pxa27x_ep_disable (struct usb_ep *_ep)
341 + struct pxa27x_ep *ep;
343 + ep = container_of (_ep, struct pxa27x_ep, ep);
344 + if (!_ep || !ep->desc) {
345 + DMSG("%s, %s not enabled\n", __FUNCTION__,
346 + _ep ? ep->ep.name : NULL);
349 + nuke (ep, -ESHUTDOWN);
352 + if (ep->dma >= 0) {
353 + *ep->reg_drcmr = 0;
354 + pxa_free_dma (ep->dma);
359 + /* flush fifo (mostly for IN buffers) */
360 + pxa27x_ep_fifo_flush (_ep);
365 + DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
369 +/*-------------------------------------------------------------------------*/
371 +/* for the pxa27x, these can just wrap kmalloc/kfree. gadget drivers
372 + * must still pass correctly initialized endpoints, since other controller
373 + * drivers may care about how it's currently set up (dma issues etc).
377 + * pxa27x_ep_alloc_request - allocate a request data structure
379 +static struct usb_request *
380 +pxa27x_ep_alloc_request (struct usb_ep *_ep, unsigned gfp_flags)
382 + struct pxa27x_request *req;
384 + req = kmalloc (sizeof *req, gfp_flags);
388 + memset (req, 0, sizeof *req);
389 + INIT_LIST_HEAD (&req->queue);
395 + * pxa27x_ep_free_request - deallocate a request data structure
398 +pxa27x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
400 + struct pxa27x_request *req;
402 + req = container_of(_req, struct pxa27x_request, req);
403 + WARN_ON (!list_empty (&req->queue));
408 +/* PXA cache needs flushing with DMA I/O (it's dma-incoherent), but there's
409 + * no device-affinity and the heap works perfectly well for i/o buffers.
410 + * It wastes much less memory than dma_alloc_coherent() would, and even
411 + * prevents cacheline (32 bytes wide) sharing problems.
414 +pxa27x_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
415 + dma_addr_t *dma, unsigned gfp_flags)
419 + retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
421 + *dma = virt_to_bus (retval);
426 +pxa27x_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma,
432 +/*-------------------------------------------------------------------------*/
435 + * done - retire a request; caller blocked irqs
437 +static void done(struct pxa27x_ep *ep, struct pxa27x_request *req, int status)
439 + list_del_init(&req->queue);
440 + if (likely (req->req.status == -EINPROGRESS))
441 + req->req.status = status;
443 + status = req->req.status;
445 + if (status && status != -ESHUTDOWN)
446 + DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
447 + ep->ep.name, &req->req, status,
448 + req->req.actual, req->req.length);
450 + /* don't modify queue heads during completion callback */
451 + req->req.complete(&ep->ep, &req->req);
455 +static inline void ep0_idle (struct pxa27x_udc *dev)
457 + dev->ep0state = EP0_IDLE;
462 +write_packet(volatile u32 *uddr, struct pxa27x_request *req, unsigned max)
465 + int length, count, remain;
467 + buf = (u32*)(req->req.buf + req->req.actual);
470 + /* how big will this packet be? */
471 + length = min(req->req.length - req->req.actual, max);
472 + req->req.actual += length;
474 + remain = length & 0x3;
475 + count = length & ~(0x3);
477 + while (likely(count)) {
483 + volatile u8* reg=(u8*)uddr;
484 + char *rd =(u8*)buf;
495 + * write to an IN endpoint fifo, as many packets as possible.
496 + * irqs will use this to write the rest later.
497 + * caller guarantees at least one packet buffer is ready (or a zlp).
500 +write_fifo (struct pxa27x_ep *ep, struct pxa27x_request *req)
504 + max = le16_to_cpu(ep->desc->wMaxPacketSize);
507 + int is_last, is_short;
509 + count = write_packet(ep->reg_udcdr, req, max);
511 + /* last packet is usually short (or a zlp) */
512 + if (unlikely (count != max))
513 + is_last = is_short = 1;
515 + if (likely(req->req.length != req->req.actual)
520 + /* interrupt/iso maxpacket may not fill the fifo */
521 + is_short = unlikely (max < ep->fifo_size);
524 + DMSG("wrote %s count:%d bytes%s%s %d left %p\n",
525 + ep->ep.name, count,
526 + is_last ? "/L" : "", is_short ? "/S" : "",
527 + req->req.length - req->req.actual, &req->req);
529 + /* let loose that packet. maybe try writing another one,
530 + * double buffering might work. TSP, TPC, and TFS
531 + * bit values are the same for all normal IN endpoints.
533 + *ep->reg_udccsr = UDCCSR_PC;
535 + *ep->reg_udccsr = UDCCSR_SP;
537 + /* requests complete when all IN data is in the FIFO */
540 + if (list_empty(&ep->queue) || unlikely(ep->dma >= 0)) {
541 + pio_irq_disable (ep->ep_num);
543 + /* unaligned data and zlps couldn't use dma */
544 + if (unlikely(!list_empty(&ep->queue))) {
545 + req = list_entry(ep->queue.next,
546 + struct pxa27x_request, queue);
555 + // TODO experiment: how robust can fifo mode tweaking be?
556 + // double buffering is off in the default fifo mode, which
557 + // prevents TFS from being set here.
559 + } while (*ep->reg_udccsr & UDCCSR_FS);
563 +/* caller asserts req->pending (ep0 irq status nyet cleared); starts
564 + * ep0 data stage. these chips want very simple state transitions.
567 +void ep0start(struct pxa27x_udc *dev, u32 flags, const char *tag)
569 + UDCCSR0 = flags|UDCCSR0_SA|UDCCSR0_OPC;
570 + UDCISR0 = UDCICR_INT(0, UDC_INT_FIFOERROR | UDC_INT_PACKETCMP);
571 + dev->req_pending = 0;
572 + DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
573 + __FUNCTION__, tag, UDCCSR0, flags);
577 +write_ep0_fifo (struct pxa27x_ep *ep, struct pxa27x_request *req)
582 + count = write_packet(&UDCDR0, req, EP0_FIFO_SIZE);
583 + ep->dev->stats.write.bytes += count;
585 + /* last packet "must be" short (or a zlp) */
586 + is_short = (count != EP0_FIFO_SIZE);
588 + DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
589 + req->req.length - req->req.actual, &req->req);
591 + if (unlikely (is_short)) {
592 + if (ep->dev->req_pending)
593 + ep0start(ep->dev, UDCCSR0_IPR, "short IN");
595 + UDCCSR0 = UDCCSR0_IPR;
597 + count = req->req.length;
601 + /* This seems to get rid of lost status irqs in some cases:
602 + * host responds quickly, or next request involves config
603 + * change automagic, or should have been hidden, or ...
605 + * FIXME get rid of all udelays possible...
607 + if (count >= EP0_FIFO_SIZE) {
610 + if ((UDCCSR0 & UDCCSR0_OPC) != 0) {
611 + /* clear OPC, generate ack */
612 + UDCCSR0 = UDCCSR0_OPC;
620 + } else if (ep->dev->req_pending)
621 + ep0start(ep->dev, 0, "IN");
627 + * read_fifo - unload packet(s) from the fifo we use for usb OUT
628 + * transfers and put them into the request. caller should have made
629 + * sure there's at least one packet ready.
631 + * returns true if the request completed because of short packet or the
632 + * request buffer having filled (and maybe overran till end-of-packet).
635 +read_fifo (struct pxa27x_ep *ep, struct pxa27x_request *req)
639 + int bufferspace, count, is_short;
641 + /* make sure there's a packet in the FIFO.*/
642 + if (unlikely ((*ep->reg_udccsr & UDCCSR_PC) == 0))
644 + buf =(u32*) (req->req.buf + req->req.actual);
646 + bufferspace = req->req.length - req->req.actual;
648 + /* read all bytes from this packet */
649 + if (likely (*ep->reg_udccsr & UDCCSR_BNE)) {
650 + count = 0x3ff & *ep->reg_udcbcr;
651 + req->req.actual += min (count, bufferspace);
655 + is_short = (count < ep->ep.maxpacket);
656 + DMSG("read %s udccsr:%02x, count:%d bytes%s req %p %d/%d\n",
657 + ep->ep.name, *ep->reg_udccsr, count,
658 + is_short ? "/S" : "",
659 + &req->req, req->req.actual, req->req.length);
661 +// dump_regs(ep->ep_num );
662 + count = min(count, bufferspace);
663 + while (likely (count > 0)) {
664 + *buf++ = *ep->reg_udcdr;
667 + DMSG("Buf:0x%p\n", req->req.buf);
669 + *ep->reg_udccsr = UDCCSR_PC;
670 + /* RPC/RSP/RNE could now reflect the other packet buffer */
673 + if (is_short || req->req.actual == req->req.length) {
675 + if (list_empty(&ep->queue))
676 + pio_irq_disable (ep->ep_num);
680 + /* finished that packet. the next one may be waiting... */
686 + * special ep0 version of the above. no UBCR0 or double buffering; status
687 + * handshaking is magic. most device protocols don't need control-OUT.
688 + * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
689 + * protocols do use them.
692 +read_ep0_fifo (struct pxa27x_ep *ep, struct pxa27x_request *req)
695 + unsigned bufferspace;
697 + buf = (u32*) (req->req.buf + req->req.actual);
698 + bufferspace = req->req.length - req->req.actual;
700 + while (UDCCSR0 & UDCCSR0_RNE) {
703 + if (unlikely (bufferspace == 0)) {
704 + /* this happens when the driver's buffer
705 + * is smaller than what the host sent.
706 + * discard the extra data.
708 + if (req->req.status != -EOVERFLOW)
709 + DMSG("%s overflow\n", ep->ep.name);
710 + req->req.status = -EOVERFLOW;
713 + req->req.actual += 4;
718 + UDCCSR0 = UDCCSR0_OPC ;
721 + if (req->req.actual >= req->req.length)
724 + /* finished that packet. the next one may be waiting... */
730 +#define MAX_IN_DMA ((DCMD_LENGTH + 1) - BULK_FIFO_SIZE)
731 +static void kick_dma(struct pxa27x_ep *ep, struct pxa27x_request *req)
734 + u32 len = req->req.length;
735 + u32 buf = req->req.dma;
736 + u32 fifo = io_v2p ((u32)ep->reg_udcdr);
738 + buf += req->req.actual;
739 + len -= req->req.actual;
742 + DMSG("%s: req:0x%p length:%d, actual:%d dma:%d\n",
743 + __FUNCTION__, &req->req, req->req.length,
744 + req->req.actual,ep->dma);
746 + /* no-descriptor mode can be simple for bulk-in, iso-in, iso-out */
747 + DCSR(ep->dma) = DCSR_NODESC;
749 + DALGN |= 1 << ep->dma;
751 + DALGN &= ~(1 << ep->dma);
754 + DSADR(ep->dma) = buf;
755 + DTADR(ep->dma) = fifo;
756 + if (len > MAX_IN_DMA) {
759 + } else if (len >= ep->ep.maxpacket) {
760 + if ((ep->dma_con = (len % ep->ep.maxpacket) != 0))
761 + len = ep->ep.maxpacket;
763 + dcmd = len | DCMD_BURST32 | DCMD_WIDTH4 | DCMD_ENDIRQEN
764 + | DCMD_FLOWTRG | DCMD_INCSRCADDR;
766 + DSADR(ep->dma) = fifo;
767 + DTADR(ep->dma) = buf;
768 + dcmd = len | DCMD_BURST32 | DCMD_WIDTH4 | DCMD_ENDIRQEN
769 + | DCMD_FLOWSRC | DCMD_INCTRGADDR;
771 + *ep->reg_udccsr = UDCCSR_DME;
772 + DCMD(ep->dma) = dcmd;
773 + DCSR(ep->dma) = DCSR_NODESC | DCSR_EORIRQEN \
774 + | ((ep->dir_in) ? DCSR_STOPIRQEN : 0);
775 + *ep->reg_drcmr = ep->dma | DRCMR_MAPVLD;
776 + DCSR(ep->dma) |= DCSR_RUN;
779 +static void cancel_dma(struct pxa27x_ep *ep)
781 + struct pxa27x_request *req;
784 + if (DCSR(ep->dma) == 0 || list_empty(&ep->queue))
787 + DMSG("hehe dma:%d,dcsr:0x%x\n", ep->dma, DCSR(ep->dma));
789 + while ((DCSR(ep->dma) & DCSR_STOPSTATE) == 0)
792 + req = list_entry(ep->queue.next, struct pxa27x_request, queue);
793 + tmp = DCMD(ep->dma) & DCMD_LENGTH;
794 + req->req.actual = req->req.length - tmp;
796 + /* the last tx packet may be incomplete, so flush the fifo.
797 + * FIXME correct req.actual if we can
799 + *ep->reg_udccsr = UDCCSR_FEF;
802 +static void dma_nodesc_handler(int dmach, void *_ep)
804 + struct pxa27x_ep *ep = _ep;
805 + struct pxa27x_request *req, *req_next;
806 + u32 dcsr, tmp, completed;
808 + local_irq_disable();
810 + req = list_entry(ep->queue.next, struct pxa27x_request, queue);
812 + DMSG("%s, buf:0x%p\n",__FUNCTION__, req->req.buf);
815 + ep->dev->stats.irqs++;
816 + HEX_DISPLAY(ep->dev->stats.irqs);
820 + dcsr = DCSR(dmach);
821 + DCSR(ep->dma) &= ~DCSR_RUN;
823 + if (dcsr & DCSR_BUSERR) {
824 + DCSR(dmach) = DCSR_BUSERR;
825 + printk(KERN_ERR " Buss Error\n");
826 + req->req.status = -EIO;
828 + } else if (dcsr & DCSR_ENDINTR) {
829 + DCSR(dmach) = DCSR_ENDINTR;
831 + tmp = req->req.length - req->req.actual;
832 + /* Last packet is a short one*/
833 + if ( tmp < ep->ep.maxpacket) {
836 + *ep->reg_udccsr = UDCCSR_SP | \
837 + (*ep->reg_udccsr & UDCCSR_MASK);
838 + /*Wait for packet out */
839 + while( (count++ < 10000) && \
840 + !(*ep->reg_udccsr & UDCCSR_FS));
841 + if (count >= 10000)
842 + DMSG("Failed to send packet\n");
844 + DMSG("%s: short packet sent len:%d,"
845 + "length:%d,actual:%d\n", __FUNCTION__,
846 + tmp, req->req.length, req->req.actual);
847 + req->req.actual = req->req.length;
849 + /* There are still packets to transfer */
850 + } else if ( ep->dma_con) {
851 + DMSG("%s: more packets,length:%d,actual:%d\n",
852 + __FUNCTION__,req->req.length,
854 + req->req.actual += ep->ep.maxpacket;
857 + DMSG("%s: no more packets,length:%d,"
858 + "actual:%d\n", __FUNCTION__,
859 + req->req.length, req->req.actual);
860 + req->req.actual = req->req.length;
864 + req->req.actual = req->req.length;
867 + } else if (dcsr & DCSR_EORINTR) { //Only happened in OUT DMA
868 + int remain,udccsr ;
870 + DCSR(dmach) = DCSR_EORINTR;
871 + remain = DCMD(dmach) & DCMD_LENGTH;
872 + req->req.actual = req->req.length - remain;
874 + udccsr = *ep->reg_udccsr;
875 + if (udccsr & UDCCSR_SP) {
876 + *ep->reg_udccsr = UDCCSR_PC | (udccsr & UDCCSR_MASK);
879 + DMSG("%s: length:%d actual:%d\n",
880 + __FUNCTION__, req->req.length, req->req.actual);
882 + DMSG("%s: Others dma:%d DCSR:0x%x DCMD:0x%x\n",
883 + __FUNCTION__, dmach, DCSR(dmach), DCMD(dmach));
885 + if (likely(completed)) {
886 + if (req->queue.next != &ep->queue) {
887 + req_next = list_entry(req->queue.next,
888 + struct pxa27x_request, queue);
889 + kick_dma(ep, req_next);
896 + local_irq_enable();
900 +/*-------------------------------------------------------------------------*/
903 +pxa27x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, unsigned gfp_flags)
905 + struct pxa27x_ep *ep;
906 + struct pxa27x_request *req;
907 + struct pxa27x_udc *dev;
908 + unsigned long flags;
910 + req = container_of(_req, struct pxa27x_request, req);
911 + if (unlikely (!_req || !_req->complete || !_req->buf||
912 + !list_empty(&req->queue))) {
913 + DMSG("%s, bad params\n", __FUNCTION__);
917 + ep = container_of(_ep, struct pxa27x_ep, ep);
918 + if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
919 + DMSG("%s, bad ep\n", __FUNCTION__);
923 + DMSG("%s, ep point %d is queue\n", __FUNCTION__, ep->ep_num);
926 + if (unlikely (!dev->driver
927 + || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
928 + DMSG("%s, bogus device state\n", __FUNCTION__);
932 + /* iso is always one packet per request, that's the only way
933 + * we can report per-packet status. that also helps with dma.
935 + if (unlikely (ep->ep_type == USB_ENDPOINT_XFER_ISOC
936 + && req->req.length > le16_to_cpu
937 + (ep->desc->wMaxPacketSize)))
941 + // FIXME caller may already have done the dma mapping
942 + if (ep->dma >= 0) {
943 + _req->dma = dma_map_single(dev->dev, _req->buf, _req->length,
944 + (ep->dir_in) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
948 + DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
949 + _ep->name, _req, _req->length, _req->buf);
951 + local_irq_save(flags);
953 + _req->status = -EINPROGRESS;
956 + /* kickstart this i/o queue? */
957 + if (list_empty(&ep->queue) && !ep->stopped) {
958 + if (ep->desc == 0 /* ep0 */) {
959 + unsigned length = _req->length;
961 + switch (dev->ep0state) {
962 + case EP0_IN_DATA_PHASE:
963 + dev->stats.write.ops++;
964 + if (write_ep0_fifo(ep, req))
968 + case EP0_OUT_DATA_PHASE:
969 + dev->stats.read.ops++;
970 + if (dev->req_pending)
971 + ep0start(dev, UDCCSR0_IPR, "OUT");
972 + if (length == 0 || ((UDCCSR0 & UDCCSR0_RNE) != 0
973 + && read_ep0_fifo(ep, req))) {
979 + case EP0_NO_ACTION:
984 + DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
985 + local_irq_restore (flags);
989 + /* either start dma or prime pio pump */
990 + } else if (ep->dma >= 0) {
993 + /* can the FIFO can satisfy the request immediately? */
994 + } else if (ep->dir_in
995 + && (*ep->reg_udccsr & UDCCSR_FS) != 0
996 + && write_fifo(ep, req)) {
998 + } else if ((*ep->reg_udccsr & UDCCSR_FS) != 0
999 + && read_fifo(ep, req)) {
1002 + DMSG("req:%p,ep->desc:%p,ep->dma:%d\n", req, ep->desc, ep->dma);
1003 + if (likely (req && ep->desc) && ep->dma < 0)
1004 + pio_irq_enable(ep->ep_num);
1007 + /* pio or dma irq handler advances the queue. */
1008 + if (likely (req != 0))
1009 + list_add_tail(&req->queue, &ep->queue);
1010 + local_irq_restore(flags);
1017 + * nuke - dequeue ALL requests
1019 +static void nuke(struct pxa27x_ep *ep, int status)
1021 + struct pxa27x_request *req;
1023 + /* called with irqs blocked */
1025 + if (ep->dma >= 0 && !ep->stopped)
1028 + while (!list_empty(&ep->queue)) {
1029 + req = list_entry(ep->queue.next, struct pxa27x_request, queue);
1030 + done(ep, req, status);
1033 + pio_irq_disable (ep->ep_num);
1037 +/* dequeue JUST ONE request */
1038 +static int pxa27x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1040 + struct pxa27x_ep *ep;
1041 + struct pxa27x_request *req;
1042 + unsigned long flags;
1044 + ep = container_of(_ep, struct pxa27x_ep, ep);
1045 + if (!_ep || ep->ep.name == ep0name)
1048 + local_irq_save(flags);
1050 + /* make sure it's actually queued on this endpoint */
1051 + list_for_each_entry (req, &ep->queue, queue) {
1052 + if (&req->req == _req)
1055 + if (&req->req != _req) {
1056 + local_irq_restore(flags);
1061 + if (ep->dma >= 0 && ep->queue.next == &req->queue && !ep->stopped) {
1063 + done(ep, req, -ECONNRESET);
1065 + if (!list_empty(&ep->queue)) {
1066 + req = list_entry(ep->queue.next,
1067 + struct pxa27x_request, queue);
1068 + kick_dma(ep, req);
1072 + done(ep, req, -ECONNRESET);
1074 + local_irq_restore(flags);
1078 +/*-------------------------------------------------------------------------*/
1080 +static int pxa27x_ep_set_halt(struct usb_ep *_ep, int value)
1082 + struct pxa27x_ep *ep;
1083 + unsigned long flags;
1085 + DMSG("%s is called\n", __FUNCTION__);
1086 + ep = container_of(_ep, struct pxa27x_ep, ep);
1087 + if (unlikely (!_ep
1088 + || (!ep->desc && ep->ep.name != ep0name))
1089 + || ep->ep_type == USB_ENDPOINT_XFER_ISOC) {
1090 + DMSG("%s, bad ep\n", __FUNCTION__);
1094 + /* this path (reset toggle+halt) is needed to implement
1095 + * SET_INTERFACE on normal hardware. but it can't be
1096 + * done from software on the PXA UDC, and the hardware
1097 + * forgets to do it as part of SET_INTERFACE automagic.
1099 + DMSG("only host can clear %s halt\n", _ep->name);
1103 + local_irq_save(flags);
1105 + if (ep->dir_in && ((*ep->reg_udccsr & UDCCSR_FS) == 0
1106 + || !list_empty(&ep->queue))) {
1107 + local_irq_restore(flags);
1111 + /* FST bit is the same for control, bulk in, bulk out, interrupt in */
1112 + *ep->reg_udccsr = UDCCSR_FST|UDCCSR_FEF;
1114 + /* ep0 needs special care */
1116 + start_watchdog(ep->dev);
1117 + ep->dev->req_pending = 0;
1118 + ep->dev->ep0state = EP0_STALL;
1121 + /* and bulk/intr endpoints like dropping stalls too */
1124 + for (i = 0; i < 1000; i += 20) {
1125 + if (*ep->reg_udccsr & UDCCSR_SST)
1130 + local_irq_restore(flags);
1132 + DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
1136 +static int pxa27x_ep_fifo_status(struct usb_ep *_ep)
1138 + struct pxa27x_ep *ep;
1140 + ep = container_of(_ep, struct pxa27x_ep, ep);
1142 + DMSG("%s, bad ep\n", __FUNCTION__);
1145 + /* pxa can't report unclaimed bytes from IN fifos */
1147 + return -EOPNOTSUPP;
1148 + if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
1149 + || (*ep->reg_udccsr & UDCCSR_FS) == 0)
1152 + return (*ep->reg_udcbcr & 0xfff) + 1;
1155 +static void pxa27x_ep_fifo_flush(struct usb_ep *_ep)
1157 + struct pxa27x_ep *ep;
1159 + ep = container_of(_ep, struct pxa27x_ep, ep);
1160 + if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
1161 + DMSG("%s, bad ep\n", __FUNCTION__);
1165 + /* toggle and halt bits stay unchanged */
1167 + /* for OUT, just read and discard the FIFO contents. */
1168 + if (!ep->dir_in) {
1169 + while (((*ep->reg_udccsr) & UDCCSR_BNE) != 0)
1170 + (void) *ep->reg_udcdr;
1174 + /* most IN status is the same, but ISO can't stall */
1175 + *ep->reg_udccsr = UDCCSR_PC|UDCCSR_FST|UDCCSR_TRN
1176 + | (ep->ep_type == USB_ENDPOINT_XFER_ISOC)
1181 +static struct usb_ep_ops pxa27x_ep_ops = {
1182 + .enable = pxa27x_ep_enable,
1183 + .disable = pxa27x_ep_disable,
1185 + .alloc_request = pxa27x_ep_alloc_request,
1186 + .free_request = pxa27x_ep_free_request,
1188 + .alloc_buffer = pxa27x_ep_alloc_buffer,
1189 + .free_buffer = pxa27x_ep_free_buffer,
1191 + .queue = pxa27x_ep_queue,
1192 + .dequeue = pxa27x_ep_dequeue,
1194 + .set_halt = pxa27x_ep_set_halt,
1195 + .fifo_status = pxa27x_ep_fifo_status,
1196 + .fifo_flush = pxa27x_ep_fifo_flush,
1200 +/* ---------------------------------------------------------------------------
1201 + * device-scoped parts of the api to the usb controller hardware
1202 + * ---------------------------------------------------------------------------
1205 +static int pxa27x_udc_get_frame(struct usb_gadget *_gadget)
1207 + return (UDCFNR & 0x3FF);
1210 +static int pxa27x_udc_wakeup(struct usb_gadget *_gadget)
1212 + /* host may not have enabled remote wakeup */
1213 + if ((UDCCR & UDCCR_DWRE) == 0)
1214 + return -EHOSTUNREACH;
1215 + udc_set_mask_UDCCR(UDCCR_UDR);
1219 +static const struct usb_gadget_ops pxa27x_udc_ops = {
1220 + .get_frame = pxa27x_udc_get_frame,
1221 + .wakeup = pxa27x_udc_wakeup,
1222 + // current versions must always be self-powered
1226 +/*-------------------------------------------------------------------------*/
1228 +#ifdef UDC_PROC_FILE
1230 +static const char proc_node_name [] = "driver/udc";
1233 +udc_proc_read(char *page, char **start, off_t off, int count,
1234 + int *eof, void *_dev)
1237 + struct pxa27x_udc *dev = _dev;
1239 + unsigned size = count;
1240 + unsigned long flags;
1247 + local_irq_save(flags);
1249 + /* basic device status */
1250 + t = scnprintf(next, size, DRIVER_DESC "\n"
1251 + "%s version: %s\nGadget driver: %s\n",
1252 + driver_name, DRIVER_VERSION SIZE_STR DMASTR,
1253 + dev->driver ? dev->driver->driver.name : "(none)");
1257 + /* registers for device and ep0 */
1258 + t = scnprintf(next, size,
1259 + "uicr %02X.%02X, usir %02X.%02x, ufnr %02X\n",
1260 + UDCICR1, UDCICR0, UDCISR1, UDCISR0, UDCFNR);
1265 + t = scnprintf(next, size,"udccr %02X =%s%s%s%s%s%s%s%s%s%s, con=%d,inter=%d,altinter=%d\n", tmp,
1266 + (tmp & UDCCR_OEN) ? " oen":"",
1267 + (tmp & UDCCR_AALTHNP) ? " aalthnp":"",
1268 + (tmp & UDCCR_AHNP) ? " rem" : "",
1269 + (tmp & UDCCR_BHNP) ? " rstir" : "",
1270 + (tmp & UDCCR_DWRE) ? " dwre" : "",
1271 + (tmp & UDCCR_SMAC) ? " smac" : "",
1272 + (tmp & UDCCR_EMCE) ? " emce" : "",
1273 + (tmp & UDCCR_UDR) ? " udr" : "",
1274 + (tmp & UDCCR_UDA) ? " uda" : "",
1275 + (tmp & UDCCR_UDE) ? " ude" : "",
1276 + (tmp & UDCCR_ACN) >> UDCCR_ACN_S,
1277 + (tmp & UDCCR_AIN) >> UDCCR_AIN_S,
1278 + (tmp & UDCCR_AAISN)>> UDCCR_AAISN_S );
1284 + t = scnprintf(next, size,
1285 + "udccsr0 %02X =%s%s%s%s%s%s%s\n", tmp,
1286 + (tmp & UDCCSR0_SA) ? " sa" : "",
1287 + (tmp & UDCCSR0_RNE) ? " rne" : "",
1288 + (tmp & UDCCSR0_FST) ? " fst" : "",
1289 + (tmp & UDCCSR0_SST) ? " sst" : "",
1290 + (tmp & UDCCSR0_DME) ? " dme" : "",
1291 + (tmp & UDCCSR0_IPR) ? " ipr" : "",
1292 + (tmp & UDCCSR0_OPC) ? " opc" : "");
1299 + t = scnprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1300 + dev->stats.write.bytes, dev->stats.write.ops,
1301 + dev->stats.read.bytes, dev->stats.read.ops,
1306 + /* dump endpoint queues */
1307 + for (i = 0; i < UDC_EP_NUM; i++) {
1308 + struct pxa27x_ep *ep = &dev->ep [i];
1309 + struct pxa27x_request *req;
1313 + const struct usb_endpoint_descriptor *d;
1318 + tmp = *dev->ep [i].reg_udccsr;
1319 + t = scnprintf(next, size,
1320 + "%s max %d %s udccs %02x udccr:0x%x\n",
1321 + ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
1322 + (ep->dma >= 0) ? "dma" : "pio", tmp,
1323 + *dev->ep[i].reg_udccr);
1324 + /* TODO translate all five groups of udccs bits! */
1326 + } else /* ep0 should only have one transfer queued */
1327 + t = scnprintf(next, size, "ep0 max 16 pio irqs %lu\n",
1329 + if (t <= 0 || t > size)
1334 + if (list_empty(&ep->queue)) {
1335 + t = scnprintf(next, size, "\t(nothing queued)\n");
1336 + if (t <= 0 || t > size)
1342 + list_for_each_entry(req, &ep->queue, queue) {
1344 + if (ep->dma >= 0 && req->queue.prev == &ep->queue)
1345 + t = scnprintf(next, size,
1346 + "\treq %p len %d/%d "
1347 + "buf %p (dma%d dcmd %08x)\n",
1348 + &req->req, req->req.actual,
1349 + req->req.length, req->req.buf,
1350 + ep->dma, DCMD(ep->dma)
1351 + // low 13 bits == bytes-to-go
1355 + t = scnprintf(next, size,
1356 + "\treq %p len %d/%d buf %p\n",
1357 + &req->req, req->req.actual,
1358 + req->req.length, req->req.buf);
1359 + if (t <= 0 || t > size)
1367 + local_irq_restore(flags);
1369 + return count - size;
1372 +#define create_proc_files() \
1373 + create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
1374 +#define remove_proc_files() \
1375 + remove_proc_entry(proc_node_name, NULL)
1377 +#else /* !UDC_PROC_FILE */
1378 +#define create_proc_files() do {} while (0)
1379 +#define remove_proc_files() do {} while (0)
1381 +#endif /* UDC_PROC_FILE */
1383 +/* "function" sysfs attribute */
1385 +show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1387 + struct pxa27x_udc *dev = dev_get_drvdata (_dev);
1390 + || !dev->driver->function
1391 + || strlen (dev->driver->function) > PAGE_SIZE)
1393 + return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1395 +static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1397 +/*-------------------------------------------------------------------------*/
1400 + * udc_disable - disable USB device controller
1402 +static void udc_disable(struct pxa27x_udc *dev)
1404 + UDCICR0 = UDCICR1 = 0x00000000;
1406 + udc_clear_mask_UDCCR(UDCCR_UDE);
1408 + /* Disable clock for USB device */
1409 + pxa_set_cken(CKEN11_USB, 0);
1412 + dev->gadget.speed = USB_SPEED_UNKNOWN;
1413 + LED_CONNECTED_OFF;
1414 + if (dev->mach->udc_command)
1415 + dev->mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
1420 + * udc_reinit - initialize software state
1422 +static void udc_reinit(struct pxa27x_udc *dev)
1426 + dev->ep0state = EP0_IDLE;
1428 + /* basic endpoint records init */
1429 + for (i = 0; i < UDC_EP_NUM; i++) {
1430 + struct pxa27x_ep *ep = &dev->ep[i];
1433 + ep->pio_irqs = ep->dma_irqs = 0;
1435 + dev->configuration = 0;
1436 + dev->interface = 0;
1437 + dev->alternate = 0;
1438 + /* the rest was statically initialized, and is read-only */
1441 +/* until it's enabled, this UDC should be completely invisible
1442 + * to any USB host.
1444 +static void udc_enable (struct pxa27x_udc *dev)
1446 + udc_clear_mask_UDCCR(UDCCR_UDE);
1448 + // MST_MSCWR2 &= ~(MST_MSCWR2_nUSBC_SC);
1450 + /* Enable clock for USB device */
1451 + pxa_set_cken(CKEN11_USB, 1);
1453 + UDCICR0 = UDCICR1 = 0;
1456 + dev->gadget.speed = USB_SPEED_FULL;
1457 + dev->stats.irqs = 0;
1459 + udc_set_mask_UDCCR(UDCCR_UDE);
1461 + if (UDCCR & UDCCR_EMCE)
1463 + printk(KERN_ERR ": There are error in configuration, udc disabled\n");
1466 + /* caller must be able to sleep in order to cope
1467 + * with startup transients.
1471 + /* enable suspend/resume and reset irqs */
1472 + UDCICR1 = UDCICR1_IECC | UDCICR1_IERU | UDCICR1_IESU | UDCICR1_IERS;
1474 + /* enable ep0 irqs */
1475 + UDCICR0 = UDCICR_INT(0,UDCICR_INT_MASK);
1477 + for(i=1; i < UDC_EP_NUM; i++) {
1478 + if (dev->ep[i].assigned)
1479 + pio_irq_enable(i);
1482 + if (dev->mach->udc_command)
1483 + dev->mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
1487 +/* when a driver is successfully registered, it will receive
1488 + * control requests including set_configuration(), which enables
1489 + * non-control requests. then usb traffic follows until a
1490 + * disconnect is reported. then a host may connect again, or
1491 + * the driver might get unbound.
1493 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1495 + struct pxa27x_udc *dev = the_controller;
1498 + DMSG("dev=0x%x, driver=0x%x, speed=%d,"
1499 + "bind=0x%x, unbind=0x%x, disconnect=0x%x, setup=0x%x\n",
1500 + (unsigned)dev, (unsigned)driver, driver->speed,
1501 + (unsigned)driver->bind, (unsigned)driver->unbind,
1502 + (unsigned)driver->disconnect, (unsigned)driver->setup);
1504 + if (!driver || driver->speed != USB_SPEED_FULL
1506 + || !driver->unbind
1507 + || !driver->disconnect
1508 + || !driver->setup)
1515 + /* first hook up the driver ... */
1516 + dev->driver = driver;
1517 + dev->gadget.dev.driver = &driver->driver;
1519 + device_add (&dev->gadget.dev);
1520 + retval = driver->bind(&dev->gadget);
1522 + DMSG("bind to driver %s --> error %d\n",
1523 + driver->driver.name, retval);
1524 + device_del (&dev->gadget.dev);
1527 + dev->gadget.dev.driver = 0;
1530 + device_create_file(dev->dev, &dev_attr_function);
1532 + /* ... then enable host detection and ep0; and we're ready
1533 + * for set_configuration as well as eventual disconnect.
1534 + * NOTE: this shouldn't power up until later.
1536 + DMSG("registered gadget driver '%s'\n", driver->driver.name);
1541 +EXPORT_SYMBOL(usb_gadget_register_driver);
1544 +stop_activity(struct pxa27x_udc *dev, struct usb_gadget_driver *driver)
1548 + DMSG("Trace path 1\n");
1549 + /* don't disconnect drivers more than once */
1550 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1552 + dev->gadget.speed = USB_SPEED_UNKNOWN;
1554 + /* prevent new request submissions, kill any outstanding requests */
1555 + for (i = 0; i < UDC_EP_NUM; i++) {
1556 + struct pxa27x_ep *ep = &dev->ep[i];
1559 + nuke(ep, -ESHUTDOWN);
1561 + del_timer_sync(&dev->timer);
1563 + /* report disconnect; the driver is already quiesced */
1565 + driver->disconnect(&dev->gadget);
1567 + /* re-init driver-visible data structures */
1571 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1573 + struct pxa27x_udc *dev = the_controller;
1577 + if (!driver || driver != dev->driver)
1580 + local_irq_disable();
1582 + stop_activity(dev, driver);
1583 + local_irq_enable();
1585 + driver->unbind(&dev->gadget);
1588 + device_del (&dev->gadget.dev);
1589 + device_remove_file(dev->dev, &dev_attr_function);
1591 + DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1595 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
1597 +#ifndef enable_disconnect_irq
1598 +#define enable_disconnect_irq() do {} while (0)
1599 +#define disable_disconnect_irq() do {} while (0)
1603 +/*-------------------------------------------------------------------------*/
1605 +static inline void clear_ep_state (struct pxa27x_udc *dev)
1609 + /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1610 + * fifos, and pending transactions mustn't be continued in any case.
1612 + for (i = 1; i < UDC_EP_NUM; i++)
1613 + nuke(&dev->ep[i], -ECONNABORTED);
1616 +static void udc_watchdog(unsigned long _dev)
1618 + struct pxa27x_udc *dev = (void *)_dev;
1620 + local_irq_disable();
1621 + if (dev->ep0state == EP0_STALL
1622 + && (UDCCSR0 & UDCCSR0_FST) == 0
1623 + && (UDCCSR0 & UDCCSR0_SST) == 0) {
1624 + UDCCSR0 = UDCCSR0_FST|UDCCSR0_FTF;
1625 + DBG(DBG_VERBOSE, "ep0 re-stall\n");
1626 + start_watchdog(dev);
1628 + local_irq_enable();
1631 +static void handle_ep0 (struct pxa27x_udc *dev)
1633 + u32 udccsr0 = UDCCSR0;
1634 + struct pxa27x_ep *ep = &dev->ep [0];
1635 + struct pxa27x_request *req;
1637 + struct usb_ctrlrequest r;
1642 + if (list_empty(&ep->queue))
1645 + req = list_entry(ep->queue.next, struct pxa27x_request, queue);
1647 + /* clear stall status */
1648 + if (udccsr0 & UDCCSR0_SST) {
1650 + UDCCSR0 = UDCCSR0_SST;
1651 + del_timer(&dev->timer);
1655 + /* previous request unfinished? non-error iff back-to-back ... */
1656 + if ((udccsr0 & UDCCSR0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1658 + del_timer(&dev->timer);
1662 + switch (dev->ep0state) {
1663 + case EP0_NO_ACTION:
1664 + printk(KERN_INFO"%s: Busy\n", __FUNCTION__);
1667 + /* late-breaking status? */
1668 + udccsr0 = UDCCSR0;
1670 + /* start control request? */
1671 + if (likely((udccsr0 & (UDCCSR0_OPC|UDCCSR0_SA|UDCCSR0_RNE))
1672 + == (UDCCSR0_OPC|UDCCSR0_SA|UDCCSR0_RNE))) {
1675 + nuke (ep, -EPROTO);
1676 + /* read SETUP packet */
1677 + for (i = 0; i < 2; i++) {
1678 + if (unlikely(!(UDCCSR0 & UDCCSR0_RNE))) {
1680 + DMSG("SETUP %d!\n", i);
1683 + u.word [i] = UDCDR0;
1685 + if (unlikely((UDCCSR0 & UDCCSR0_RNE) != 0))
1688 + le16_to_cpus (&u.r.wValue);
1689 + le16_to_cpus (&u.r.wIndex);
1690 + le16_to_cpus (&u.r.wLength);
1694 + DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1695 + u.r.bRequestType, u.r.bRequest,
1696 + u.r.wValue, u.r.wIndex, u.r.wLength);
1697 + /* cope with automagic for some standard requests. */
1698 + dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1699 + == USB_TYPE_STANDARD;
1700 + dev->req_config = 0;
1701 + dev->req_pending = 1;
1703 + switch (u.r.bRequest) {
1704 + /* hardware was supposed to hide this */
1705 + case USB_REQ_SET_CONFIGURATION:
1706 + case USB_REQ_SET_INTERFACE:
1707 + case USB_REQ_SET_ADDRESS:
1708 + printk(KERN_ERR "Should not come here\n");
1713 + if (u.r.bRequestType & USB_DIR_IN)
1714 + dev->ep0state = EP0_IN_DATA_PHASE;
1716 + dev->ep0state = EP0_OUT_DATA_PHASE;
1717 + i = dev->driver->setup(&dev->gadget, &u.r);
1720 + /* hardware automagic preventing STALL... */
1721 + if (dev->req_config) {
1722 + /* hardware sometimes neglects to tell
1723 + * tell us about config change events,
1724 + * so later ones may fail...
1726 + WARN("config change %02x fail %d?\n",
1729 + /* TODO experiment: if has_cfr,
1730 + * hardware didn't ACK; maybe we
1731 + * could actually STALL!
1734 + DBG(DBG_VERBOSE, "protocol STALL, "
1735 + "%02x err %d\n", UDCCSR0, i);
1737 + /* the watchdog timer helps deal with cases
1738 + * where udc seems to clear FST wrongly, and
1739 + * then NAKs instead of STALLing.
1741 + ep0start(dev, UDCCSR0_FST|UDCCSR0_FTF, "stall");
1742 + start_watchdog(dev);
1743 + dev->ep0state = EP0_STALL;
1746 + /* deferred i/o == no response yet */
1747 + } else if (dev->req_pending) {
1748 + if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1749 + || dev->req_std || u.r.wLength))
1750 + ep0start(dev, 0, "defer");
1752 + ep0start(dev, UDCCSR0_IPR, "defer/IPR");
1755 + /* expect at least one data or status stage irq */
1759 + /* some random early IRQ:
1762 + * - OPC got set, without SA (likely status stage)
1764 + UDCCSR0 = udccsr0 & (UDCCSR0_SA|UDCCSR0_OPC);
1767 + case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1768 + if (udccsr0 & UDCCSR0_OPC) {
1769 + UDCCSR0 = UDCCSR0_OPC|UDCCSR0_FTF;
1770 + DBG(DBG_VERBOSE, "ep0in premature status\n");
1774 + } else /* irq was IPR clearing */ {
1776 + /* this IN packet might finish the request */
1777 + (void) write_ep0_fifo(ep, req);
1778 + } /* else IN token before response was written */
1781 + case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1782 + if (udccsr0 & UDCCSR0_OPC) {
1784 + /* this OUT packet might finish the request */
1785 + if (read_ep0_fifo(ep, req))
1787 + /* else more OUT packets expected */
1788 + } /* else OUT token before read was issued */
1789 + } else /* irq was IPR clearing */ {
1790 + DBG(DBG_VERBOSE, "ep0out premature status\n");
1797 + UDCCSR0 = UDCCSR0_FST;
1800 + UDCISR0 = UDCISR_INT(0, UDCISR_INT_MASK);
1804 +static void handle_ep(struct pxa27x_ep *ep)
1806 + struct pxa27x_request *req;
1810 + DMSG("%s is called\n", __FUNCTION__);
1813 + if (likely (!list_empty(&ep->queue))) {
1814 + req = list_entry(ep->queue.next,
1815 + struct pxa27x_request, queue);
1819 +// udccsr = *ep->reg_udccsr;
1820 + DMSG("%s: req:%p, udcisr0:0x%x udccsr %p:0x%x\n", __FUNCTION__,
1821 + req, UDCISR0, ep->reg_udccsr, *ep->reg_udccsr);
1822 + if (unlikely(ep->dir_in)) {
1823 + udccsr = (UDCCSR_SST | UDCCSR_TRN) & *ep->reg_udccsr;
1824 + if (unlikely (udccsr))
1825 + *ep->reg_udccsr = udccsr;
1827 + if (req && likely ((*ep->reg_udccsr & UDCCSR_FS) != 0))
1828 + completed = write_fifo(ep, req);
1831 + udccsr = (UDCCSR_SST | UDCCSR_TRN) & *ep->reg_udccsr;
1832 + if (unlikely(udccsr))
1833 + *ep->reg_udccsr = udccsr;
1835 + /* fifos can hold packets, ready for reading... */
1836 + if (likely(req)) {
1837 + completed = read_fifo(ep, req);
1839 + pio_irq_disable (ep->ep_num);
1840 + *ep->reg_udccsr = UDCCSR_FEF;
1841 + DMSG("%s: no req for out data\n",
1846 + } while (completed);
1849 +static void pxa27x_change_configuration (struct pxa27x_udc *dev)
1851 + struct usb_ctrlrequest req ;
1853 + req.bRequestType = 0;
1854 + req.bRequest = USB_REQ_SET_CONFIGURATION;
1855 + req.wValue = dev->configuration;
1859 + dev->ep0state = EP0_NO_ACTION;
1860 + dev->driver->setup(&dev->gadget, &req);
1864 +static void pxa27x_change_interface (struct pxa27x_udc *dev)
1866 + struct usb_ctrlrequest req;
1868 + req.bRequestType = USB_RECIP_INTERFACE;
1869 + req.bRequest = USB_REQ_SET_INTERFACE;
1870 + req.wValue = dev->alternate;
1871 + req.wIndex = dev->interface;
1874 + dev->ep0state = EP0_NO_ACTION;
1875 + dev->driver->setup(&dev->gadget, &req);
1879 + * pxa27x_udc_irq - interrupt handler
1881 + * avoid delays in ep0 processing. the control handshaking isn't always
1882 + * under software control (pxa250c0 and the pxa255 are better), and delays
1883 + * could cause usb protocol errors.
1886 +pxa27x_udc_irq(int irq, void *_dev)
1888 + struct pxa27x_udc *dev = _dev;
1891 + dev->stats.irqs++;
1892 + HEX_DISPLAY(dev->stats.irqs);
1895 + DBG(DBG_VERBOSE, "Interrupt, UDCISR0:0x%08x, UDCISR1:0x%08x, "
1896 + "UDCCR:0x%08x\n", UDCISR0, UDCISR1, UDCCR);
1898 + u32 udcir = UDCISR1 & 0xF8000000;
1902 + /* SUSpend Interrupt Request */
1903 + if (unlikely(udcir & UDCISR1_IRSU)) {
1904 + UDCISR1 = UDCISR1_IRSU;
1906 + DBG(DBG_VERBOSE, "USB suspend\n");
1907 + if (dev->gadget.speed != USB_SPEED_UNKNOWN
1909 + && dev->driver->suspend)
1910 + dev->driver->suspend(&dev->gadget);
1914 + /* RESume Interrupt Request */
1915 + if (unlikely(udcir & UDCISR1_IRRU)) {
1916 + UDCISR1 = UDCISR1_IRRU;
1918 + DBG(DBG_VERBOSE, "USB resume\n");
1920 + if (dev->gadget.speed != USB_SPEED_UNKNOWN
1922 + && dev->driver->resume)
1923 + dev->driver->resume(&dev->gadget);
1926 + if (unlikely(udcir & UDCISR1_IRCC)) {
1927 + unsigned config, interface, alternate;
1930 + DBG(DBG_VERBOSE, "USB SET_CONFIGURATION or "
1931 + "SET_INTERFACE command received\n");
1933 + UDCCR |= UDCCR_SMAC;
1935 + config = (UDCCR & UDCCR_ACN) >> UDCCR_ACN_S;
1937 + if (dev->configuration != config) {
1938 + dev->configuration = config;
1939 + pxa27x_change_configuration(dev) ;
1942 + interface = (UDCCR & UDCCR_AIN) >> UDCCR_AIN_S;
1943 + alternate = (UDCCR & UDCCR_AAISN) >> UDCCR_AAISN_S;
1945 + if ( (dev->configuration != interface) || \
1946 + (dev->alternate != alternate)){
1947 + dev->interface = config;
1948 + dev->alternate = alternate;
1949 + pxa27x_change_interface(dev);
1952 + UDCISR1 = UDCISR1_IRCC;
1953 + DMSG("%s: con:%d,inter:%d,alt:%d\n",
1954 + __FUNCTION__, config,interface, alternate);
1957 + /* ReSeT Interrupt Request - USB reset */
1958 + if (unlikely(udcir & UDCISR1_IRRS)) {
1959 + UDCISR1 = UDCISR1_IRRS;
1962 + if ((UDCCR & UDCCR_UDA) == 0) {
1963 + DBG(DBG_VERBOSE, "USB reset start\n");
1965 + /* reset driver and endpoints,
1966 + * in case that's not yet done
1968 + stop_activity (dev, dev->driver);
1971 + INFO("USB reset\n");
1972 + dev->gadget.speed = USB_SPEED_FULL;
1973 + memset(&dev->stats, 0, sizeof dev->stats);
1976 + u32 udcisr0 = UDCISR0 ;
1977 + u32 udcisr1 = UDCISR1 & 0xFFFF;
1980 + if (unlikely (!udcisr0 && !udcisr1))
1983 + DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", udcisr1,udcisr0);
1985 + /* control traffic */
1986 + if (udcisr0 & UDCISR0_IR0) {
1987 + dev->ep[0].pio_irqs++;
1993 + /* endpoint data transfers */
1994 + for (i = 1; udcisr0!=0 && i < 16; udcisr0>>=2,i++) {
1995 + UDCISR0 = UDCISR_INT(i, UDCISR_INT_MASK);
1997 + if (udcisr0 & UDC_INT_FIFOERROR)
1998 + printk(KERN_ERR" Endpoint %d Fifo error\n", i);
1999 + if (udcisr0 & UDC_INT_PACKETCMP) {
2000 + handle_ep(&dev->ep[i]);
2006 + for (i = 0; udcisr1!=0 && i < 8; udcisr1 >>= 2, i++) {
2007 + UDCISR1 = UDCISR_INT(i, UDCISR_INT_MASK);
2009 + if (udcisr1 & UDC_INT_FIFOERROR) {
2010 + printk(KERN_ERR" Endpoint %d fifo error\n", (i+16));
2013 + if (udcisr1 & UDC_INT_PACKETCMP) {
2014 + handle_ep(&dev->ep[i+16]);
2020 + /* we could also ask for 1 msec SOF (SIR) interrupts */
2022 + } while (handled);
2023 + return IRQ_HANDLED;
2026 +static inline void validate_fifo_size(struct pxa27x_ep *pxa_ep, u8 bmAttributes)
2028 + switch (bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2029 + case USB_ENDPOINT_XFER_CONTROL:
2030 + pxa_ep->fifo_size = EP0_FIFO_SIZE;
2032 + case USB_ENDPOINT_XFER_ISOC:
2033 + pxa_ep->fifo_size = ISO_FIFO_SIZE;
2035 + case USB_ENDPOINT_XFER_BULK:
2036 + pxa_ep->fifo_size = BULK_FIFO_SIZE;
2038 + case USB_ENDPOINT_XFER_INT:
2039 + pxa_ep->fifo_size = INT_FIFO_SIZE;
2046 +static void udc_init_ep(struct pxa27x_udc *dev)
2050 + INIT_LIST_HEAD (&dev->gadget.ep_list);
2051 + INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
2053 + for (i = 0; i < UDC_EP_NUM; i++) {
2054 + struct pxa27x_ep *ep = &dev->ep[i];
2058 + memset(ep, 0, sizeof(*ep));
2060 + INIT_LIST_HEAD (&ep->queue);
2063 +#define NAME_SIZE 18
2065 +struct usb_ep* pxa27x_ep_config(
2066 + struct usb_gadget *gadget,
2067 + struct usb_endpoint_descriptor *desc,
2068 + int config, int interface, int alt
2074 + struct usb_ep * ep = NULL;
2075 + struct pxa27x_ep *pxa_ep = NULL;
2076 + struct pxa27x_udc *dev = the_controller;
2078 + DMSG("pxa27x_config_ep is called\n");
2079 + DMSG(" usb endpoint descriptor is:\n"
2081 + " bDescriptorType:%x\n"
2082 + " bEndpointAddress:%x\n"
2083 + " bmAttributes:%x\n"
2084 + " wMaxPacketSize:%d\n",
2086 + desc->bDescriptorType,desc->bEndpointAddress,
2087 + desc->bmAttributes,desc->wMaxPacketSize);
2089 + for (i = 1; i < UDC_EP_NUM; i++) {
2090 + if(!dev->ep[i].assigned) {
2091 + pxa_ep = &dev->ep[i];
2092 + pxa_ep->assigned = 1;
2093 + pxa_ep->ep_num = i;
2097 + if (unlikely(i == UDC_EP_NUM)) {
2098 + printk(KERN_ERR __FILE__ ": Failed to find a spare endpoint\n");
2105 + pxa_ep->dev = dev;
2106 + pxa_ep->desc = desc;
2107 + pxa_ep->pio_irqs = pxa_ep->dma_irqs = 0;
2110 + if (!(desc->bEndpointAddress & 0xF))
2111 + desc->bEndpointAddress |= i;
2113 + if (!(desc->wMaxPacketSize)) {
2114 + validate_fifo_size(pxa_ep, desc->bmAttributes);
2115 + desc->wMaxPacketSize = pxa_ep->fifo_size;
2117 + pxa_ep->fifo_size = desc->wMaxPacketSize;
2119 + pxa_ep->dir_in = (desc->bEndpointAddress & USB_DIR_IN) ? 1 : 0;
2120 + pxa_ep->ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2121 + pxa_ep->stopped = 1;
2122 + pxa_ep->dma_con = 0;
2123 + pxa_ep->config = config;
2124 + pxa_ep->interface = interface;
2125 + pxa_ep->aisn = alt;
2127 + pxa_ep->reg_udccsr = &UDCCSR0 + i;
2128 + pxa_ep->reg_udcbcr = &UDCBCR0 + i;
2129 + pxa_ep->reg_udcdr = &UDCDR0 + i ;
2130 + pxa_ep->reg_udccr = &UDCCRA - 1 + i;
2132 + pxa_ep->reg_drcmr = &DRCMR24 + i;
2136 + DMSG("udccsr=0x%8x, udcbcr=0x%8x, udcdr=0x%8x,"
2138 + (unsigned)pxa_ep->reg_udccsr,
2139 + (unsigned)pxa_ep->reg_udcbcr,
2140 + (unsigned)pxa_ep->reg_udcdr,
2141 + (unsigned)pxa_ep->reg_udccr);
2143 + /* Configure UDCCR */
2145 + tmp |= (pxa_ep->config << UDCCONR_CN_S) & UDCCONR_CN;
2146 + tmp |= (pxa_ep->interface << UDCCONR_IN_S) & UDCCONR_IN;
2147 + tmp |= (pxa_ep->aisn << UDCCONR_AISN_S) & UDCCONR_AISN;
2148 + tmp |= (desc->bEndpointAddress << UDCCONR_EN_S) & UDCCONR_EN;
2149 + tmp |= (pxa_ep->ep_type << UDCCONR_ET_S) & UDCCONR_ET;
2150 + tmp |= (pxa_ep->dir_in) ? UDCCONR_ED : 0;
2151 + tmp |= (min(pxa_ep->fifo_size, (unsigned)desc->wMaxPacketSize) \
2152 + << UDCCONR_MPS_S ) & UDCCONR_MPS;
2153 + tmp |= UDCCONR_DE | UDCCONR_EE;
2154 +// tmp |= UDCCONR_EE;
2156 + *pxa_ep->reg_udccr = tmp;
2159 + /* Only BULK use DMA */
2160 + if ((pxa_ep->ep_type & USB_ENDPOINT_XFERTYPE_MASK)\
2161 + == USB_ENDPOINT_XFER_BULK)
2162 + *pxa_ep->reg_udccsr = UDCCSR_DME;
2165 + DMSG("UDCCR: 0x%p is 0x%x\n", pxa_ep->reg_udccr,*pxa_ep->reg_udccr);
2168 + name = kmalloc(NAME_SIZE, GFP_KERNEL);
2170 + printk(KERN_ERR "%s: Error\n", __FUNCTION__);
2174 + switch (pxa_ep->ep_type) {
2175 + case USB_ENDPOINT_XFER_BULK:
2176 + sprintf(name, "Bulk-%s-%d", (pxa_ep->dir_in ? "in":"out"), i);
2178 + case USB_ENDPOINT_XFER_INT:
2179 + sprintf(name, "Interrupt-%s-%d", (pxa_ep->dir_in ? \
2183 + sprintf(name, "endpoint-%s-%d", (pxa_ep->dir_in ? \
2189 + ep->ops = &pxa27x_ep_ops;
2190 + ep->maxpacket = min((ushort)pxa_ep->fifo_size, desc->wMaxPacketSize);
2192 + list_add_tail (&ep->ep_list, &gadget->ep_list);
2196 +EXPORT_SYMBOL(pxa27x_ep_config);
2198 +/*-------------------------------------------------------------------------*/
2200 +static void nop_release (struct device *dev)
2202 + DMSG("%s %s\n", __FUNCTION__, dev->bus_id);
2205 +/* this uses load-time allocation and initialization (instead of
2206 + * doing it at run-time) to save code, eliminate fault paths, and
2207 + * be more obviously correct.
2209 +static struct pxa27x_udc memory = {
2211 + .ops = &pxa27x_udc_ops,
2212 + .ep0 = &memory.ep[0].ep,
2213 + .name = driver_name,
2215 + .bus_id = "gadget",
2216 + .release = nop_release,
2220 + /* control endpoint */
2224 + .ops = &pxa27x_ep_ops,
2225 + .maxpacket = EP0_FIFO_SIZE,
2228 + .reg_udccsr = &UDCCSR0,
2229 + .reg_udcdr = &UDCDR0,
2233 +#define CP15R0_VENDOR_MASK 0xffffe000
2235 +#define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/xscale */
2238 + * probe - binds to the platform device
2240 +static int __init pxa27x_udc_probe(struct platform_device *_dev)
2242 + struct pxa27x_udc *dev = &memory;
2246 + /* insist on Intel/ARM/XScale */
2247 + asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2248 + if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2249 + printk(KERN_ERR "%s: not XScale!\n", driver_name);
2252 + /* other non-static parts of init */
2253 + dev->dev = &_dev->dev;
2254 + dev->mach = _dev->dev.platform_data;
2256 + init_timer(&dev->timer);
2257 + dev->timer.function = udc_watchdog;
2258 + dev->timer.data = (unsigned long) dev;
2260 + device_initialize(&dev->gadget.dev);
2261 + dev->gadget.dev.parent = &_dev->dev;
2262 + dev->gadget.dev.dma_mask = _dev->dev.dma_mask;
2264 + the_controller = dev;
2265 + platform_set_drvdata(_dev, dev);
2271 + /* irq setup after old hardware state is cleaned up */
2272 + retval = request_irq(IRQ_USB, pxa27x_udc_irq,
2273 + SA_INTERRUPT, driver_name, dev);
2274 + if (retval != 0) {
2275 + printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2276 + driver_name, IRQ_USB, retval);
2281 + create_proc_files();
2286 +static int __exit pxa27x_udc_remove(struct platform_device *_dev)
2288 + struct pxa27x_udc *dev = (struct pxa27x_udc*)platform_get_drvdata(_dev);
2291 + remove_proc_files();
2292 + usb_gadget_unregister_driver(dev->driver);
2294 + if (dev->got_irq) {
2295 + free_irq(IRQ_USB, dev);
2298 + if (machine_is_lubbock() && dev->got_disc) {
2299 + free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2300 + dev->got_disc = 0;
2302 + platform_set_drvdata(_dev, 0);
2303 + the_controller = 0;
2308 +static void pxa27x_udc_shutdown(struct platform_device *_dev)
2310 + struct pxa27x_udc *dev = (struct pxa27x_udc*)platform_get_drvdata(_dev);
2315 +static int pxa27x_udc_suspend(struct platform_device *_dev, pm_message_t state)
2318 + struct pxa27x_udc *dev = (struct pxa27x_udc*)platform_get_drvdata(_dev);
2320 + DMSG("%s is called\n", __FUNCTION__);
2321 + dev->udccsr0 = UDCCSR0;
2322 + for(i=1; (i<UDC_EP_NUM); i++) {
2323 + if (dev->ep[i].assigned) {
2324 + struct pxa27x_ep *ep = &dev->ep[i];
2326 + ep->udccsr_value = *ep->reg_udccsr;
2327 + ep->udccr_value = *ep->reg_udccr;
2328 + DMSG("EP%d, udccsr:0x%x, udccr:0x%x\n",
2329 + i, *ep->reg_udccsr, *ep->reg_udccr);
2333 + udc_clear_mask_UDCCR(UDCCR_UDE);
2334 + pxa_set_cken(CKEN11_USB, 0);
2335 + // MST_MSCWR2 |= MST_MSCWR2_nUSBC_SC;
2340 +static int pxa27x_udc_resume(struct platform_device *_dev)
2343 + struct pxa27x_udc *dev = (struct pxa27x_udc*)platform_get_drvdata(_dev);
2345 + DMSG("%s is called\n", __FUNCTION__);
2347 + UDCCSR0 = dev->udccsr0 & (UDCCSR0_FST | UDCCSR0_DME);
2348 + for (i=1; i < UDC_EP_NUM; i++) {
2349 + if (dev->ep[i].assigned) {
2350 + struct pxa27x_ep *ep = &dev->ep[i];
2352 + *ep->reg_udccsr = ep->udccsr_value;
2353 + *ep->reg_udccr = ep->udccr_value;
2354 + DMSG("EP%d, udccsr:0x%x, udccr:0x%x\n",
2355 + i, *ep->reg_udccsr, *ep->reg_udccr);
2359 + /* OTGPH bit is set when sleep mode is entered.
2360 + * it indicates that OTG pad is retaining its state.
2361 + * Upon exit from sleep mode and before clearing OTGPH,
2362 + * Software must configure the USB OTG pad, UDC, and UHC
2363 + * to the state they were in before entering sleep mode.*/
2364 + PSSR |= PSSR_OTGPH;
2369 +/*-------------------------------------------------------------------------*/
2371 +static struct platform_driver udc_driver = {
2373 + .name = "pxa2xx-udc",
2375 + .probe = pxa27x_udc_probe,
2376 + .remove = __exit_p(pxa27x_udc_remove),
2379 + // FIXME power management support
2380 + .shutdown = pxa27x_udc_shutdown,
2381 + .suspend = pxa27x_udc_suspend,
2382 + .resume = pxa27x_udc_resume
2386 +static int __init udc_init(void)
2388 + printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2389 + return platform_driver_register(&udc_driver);
2391 +module_init(udc_init);
2393 +static void __exit udc_exit(void)
2395 + platform_driver_unregister(&udc_driver);
2397 +module_exit(udc_exit);
2399 +MODULE_DESCRIPTION(DRIVER_DESC);
2400 +MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2401 +MODULE_LICENSE("GPL");
2403 Index: linux-2.6.21.7/drivers/usb/gadget/pxa27x_udc.h
2404 ===================================================================
2406 +++ linux-2.6.21.7/drivers/usb/gadget/pxa27x_udc.h
2409 + * linux/drivers/usb/gadget/pxa27x_udc.h
2410 + * Intel PXA27x on-chip full speed USB device controller
2412 + * Copyright (C) 2003 Robert Schwebel <r.schwebel@pengutronix.de>, Pengutronix
2413 + * Copyright (C) 2003 David Brownell
2414 + * Copyright (C) 2004 Intel Corporation
2416 + * This program is free software; you can redistribute it and/or modify
2417 + * it under the terms of the GNU General Public License as published by
2418 + * the Free Software Foundation; either version 2 of the License, or
2419 + * (at your option) any later version.
2421 + * This program is distributed in the hope that it will be useful,
2422 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2423 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2424 + * GNU General Public License for more details.
2426 + * You should have received a copy of the GNU General Public License
2427 + * along with this program; if not, write to the Free Software
2428 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2431 +#ifndef __LINUX_USB_GADGET_PXA27X_H
2432 +#define __LINUX_USB_GADGET_PXA27X_H
2434 +#include <linux/types.h>
2440 + struct pxa27x_udc *dev;
2442 + const struct usb_endpoint_descriptor *desc;
2443 + struct list_head queue;
2444 + unsigned long pio_irqs;
2445 + unsigned long dma_irqs;
2448 + unsigned fifo_size;
2452 + unsigned stopped : 1;
2453 + unsigned dma_con : 1;
2454 + unsigned dir_in : 1;
2455 + unsigned assigned : 1;
2458 + unsigned interface;
2460 + /* UDCCSR = UDC Control/Status Register for this EP
2461 + * UBCR = UDC Byte Count Remaining (contents of OUT fifo)
2462 + * UDCDR = UDC Endpoint Data Register (the fifo)
2463 + * UDCCR = UDC Endpoint Configuration Registers
2464 + * DRCM = DMA Request Channel Map
2466 + volatile u32 *reg_udccsr;
2467 + volatile u32 *reg_udcbcr;
2468 + volatile u32 *reg_udcdr;
2469 + volatile u32 *reg_udccr;
2471 + volatile u32 *reg_drcmr;
2472 +#define drcmr(n) .reg_drcmr = & DRCMR ## n ,
2478 + unsigned udccsr_value;
2479 + unsigned udccr_value;
2483 +struct pxa27x_request {
2484 + struct usb_request req;
2485 + struct list_head queue;
2490 + EP0_IN_DATA_PHASE,
2491 + EP0_OUT_DATA_PHASE,
2497 +#define EP0_FIFO_SIZE ((unsigned)16)
2498 +#define BULK_FIFO_SIZE ((unsigned)64)
2499 +#define ISO_FIFO_SIZE ((unsigned)256)
2500 +#define INT_FIFO_SIZE ((unsigned)8)
2504 + unsigned long ops;
2505 + unsigned long bytes;
2507 + unsigned long irqs;
2510 +#ifdef CONFIG_USB_PXA27X_SMALL
2511 +/* when memory's tight, SMALL config saves code+data. */
2513 +//#define UDC_EP_NUM 3
2517 +#define UDC_EP_NUM 24
2520 +struct pxa27x_udc {
2521 + struct usb_gadget gadget;
2522 + struct usb_gadget_driver *driver;
2524 + enum ep0_state ep0state;
2525 + struct udc_stats stats;
2526 + unsigned got_irq : 1,
2533 +#define start_watchdog(dev) mod_timer(&dev->timer, jiffies + (HZ/200))
2534 + struct timer_list timer;
2536 + struct device *dev;
2537 + struct pxa2xx_udc_mach_info *mach;
2539 + struct pxa27x_ep ep [UDC_EP_NUM];
2541 + unsigned configuration,
2549 +/*-------------------------------------------------------------------------*/
2552 +#define HEX_DISPLAY(n) do { \
2553 + if (machine_is_mainstone())\
2554 + { MST_LEDDAT1 = (n); } \
2557 +#define HEX_DISPLAY1(n) HEX_DISPLAY(n)
2559 +#define HEX_DISPLAY2(n) do { \
2560 + if (machine_is_mainstone()) \
2561 + { MST_LEDDAT2 = (n); } \
2566 +/*-------------------------------------------------------------------------*/
2568 +/* LEDs are only for debug */
2569 +#ifndef HEX_DISPLAY
2570 +#define HEX_DISPLAY(n) do {} while(0)
2573 +#ifndef LED_CONNECTED_ON
2574 +#define LED_CONNECTED_ON do {} while(0)
2575 +#define LED_CONNECTED_OFF do {} while(0)
2578 +#define LED_EP0_ON do {} while (0)
2579 +#define LED_EP0_OFF do {} while (0)
2582 +static struct pxa27x_udc *the_controller;
2585 +/*-------------------------------------------------------------------------*/
2588 +/* one GPIO should be used to detect host disconnect */
2589 +static inline int is_usb_connected(void)
2591 + if (!the_controller->mach->udc_is_connected)
2593 + return the_controller->mach->udc_is_connected();
2596 +/* one GPIO should force the host to see this device (or not) */
2597 +static inline void make_usb_disappear(void)
2599 + if (!the_controller->mach->udc_command)
2601 + the_controller->mach->udc_command(PXA27X_UDC_CMD_DISCONNECT);
2604 +static inline void let_usb_appear(void)
2606 + if (!the_controller->mach->udc_command)
2608 + the_controller->mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
2612 +/*-------------------------------------------------------------------------*/
2615 + * Debugging support vanishes in non-debug builds. DBG_NORMAL should be
2616 + * mostly silent during normal use/testing, with no timing side-effects.
2618 +#define DBG_NORMAL 1 /* error paths, device state transitions */
2619 +#define DBG_VERBOSE 2 /* add some success path trace info */
2620 +#define DBG_NOISY 3 /* ... even more: request level */
2621 +#define DBG_VERY_NOISY 4 /* ... even more: packet level */
2625 +static const char *state_name[] = {
2627 + "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
2628 + "EP0_END_XFER", "EP0_STALL"
2631 +#define DMSG(stuff...) printk(KERN_ERR "udc: " stuff)
2634 +# define UDC_DEBUG DBG_VERBOSE
2636 +# define UDC_DEBUG DBG_NORMAL
2639 +static void __attribute__ ((__unused__))
2640 +dump_udccr(const char *label)
2642 + u32 udccr = UDCCR;
2643 + DMSG("%s 0x%08x =%s%s%s%s%s%s%s%s%s%s, con=%d,inter=%d,altinter=%d\n",
2645 + (udccr & UDCCR_OEN) ? " oen":"",
2646 + (udccr & UDCCR_AALTHNP) ? " aalthnp":"",
2647 + (udccr & UDCCR_AHNP) ? " rem" : "",
2648 + (udccr & UDCCR_BHNP) ? " rstir" : "",
2649 + (udccr & UDCCR_DWRE) ? " dwre" : "",
2650 + (udccr & UDCCR_SMAC) ? " smac" : "",
2651 + (udccr & UDCCR_EMCE) ? " emce" : "",
2652 + (udccr & UDCCR_UDR) ? " udr" : "",
2653 + (udccr & UDCCR_UDA) ? " uda" : "",
2654 + (udccr & UDCCR_UDE) ? " ude" : "",
2655 + (udccr & UDCCR_ACN) >> UDCCR_ACN_S,
2656 + (udccr & UDCCR_AIN) >> UDCCR_AIN_S,
2657 + (udccr & UDCCR_AAISN)>> UDCCR_AAISN_S );
2660 +static void __attribute__ ((__unused__))
2661 +dump_udccsr0(const char *label)
2663 + u32 udccsr0 = UDCCSR0;
2665 + DMSG("%s %s 0x%08x =%s%s%s%s%s%s%s\n",
2666 + label, state_name[the_controller->ep0state], udccsr0,
2667 + (udccsr0 & UDCCSR0_SA) ? " sa" : "",
2668 + (udccsr0 & UDCCSR0_RNE) ? " rne" : "",
2669 + (udccsr0 & UDCCSR0_FST) ? " fst" : "",
2670 + (udccsr0 & UDCCSR0_SST) ? " sst" : "",
2671 + (udccsr0 & UDCCSR0_DME) ? " dme" : "",
2672 + (udccsr0 & UDCCSR0_IPR) ? " ipr" : "",
2673 + (udccsr0 & UDCCSR0_OPC) ? " opr" : "");
2676 +static void __attribute__ ((__unused__))
2677 +dump_state(struct pxa27x_udc *dev)
2681 + DMSG("%s, udcicr %02X.%02X, udcsir %02X.%02x, udcfnr %02X\n",
2682 + state_name[dev->ep0state],
2683 + UDCICR1, UDCICR0, UDCISR1, UDCISR0, UDCFNR);
2684 + dump_udccr("udccr");
2686 + if (!dev->driver) {
2687 + DMSG("no gadget driver bound\n");
2690 + DMSG("ep0 driver '%s'\n", dev->driver->driver.name);
2693 + dump_udccsr0 ("udccsr0");
2694 + DMSG("ep0 IN %lu/%lu, OUT %lu/%lu\n",
2695 + dev->stats.write.bytes, dev->stats.write.ops,
2696 + dev->stats.read.bytes, dev->stats.read.ops);
2698 + for (i = 1; i < UDC_EP_NUM; i++) {
2699 + if (dev->ep [i].desc == 0)
2701 + DMSG ("udccs%d = %02x\n", i, *dev->ep->reg_udccsr);
2706 +static void dump_regs(u8 ep)
2708 + DMSG("EP:%d UDCCSR:0x%08x UDCBCR:0x%08x\n UDCCR:0x%08x\n",
2709 + ep,UDCCSN(ep), UDCBCN(ep), UDCCN(ep));
2711 +static void dump_req (struct pxa27x_request *req)
2713 + struct usb_request *r = &req->req;
2715 + DMSG("%s: buf:0x%08x length:%d dma:0x%08x actual:%d\n",
2716 + __FUNCTION__, (unsigned)r->buf, r->length,
2717 + r->dma, r->actual);
2723 +#define DMSG(stuff...) do{}while(0)
2725 +#define dump_udccr(x) do{}while(0)
2726 +#define dump_udccsr0(x) do{}while(0)
2727 +#define dump_state(x) do{}while(0)
2729 +#define UDC_DEBUG ((unsigned)0)
2733 +#define DBG(lvl, stuff...) do{if ((lvl) <= UDC_DEBUG) DMSG(stuff);}while(0)
2735 +#define WARN(stuff...) printk(KERN_WARNING "udc: " stuff)
2736 +#define INFO(stuff...) printk(KERN_INFO "udc: " stuff)
2739 +#endif /* __LINUX_USB_GADGET_PXA27X_H */