1 --- a/drivers/usb/gadget/Kconfig
2 +++ b/drivers/usb/gadget/Kconfig
3 @@ -129,6 +129,28 @@ config USB_PXA2XX_SMALL
5 default y if USB_G_SERIAL
7 +config USB_GADGET_PXA27X
9 + depends on ARCH_PXA && PXA27x
11 + Intel's PXA 27x series XScale ARM-5TE processors include
12 + an integrated full speed USB 1.1 device controller.
14 + Say "y" to link the driver statically, or "m" to build a
15 + dynamically linked module called "pxa27x_udc" and force all
16 + gadget drivers to also be dynamically linked.
20 + depends on USB_GADGET_PXA27X
22 + select USB_GADGET_SELECTED
24 +config USB_PXA27X_DMA
25 + bool # "Use DMA support"
26 + depends on USB_GADGET_PXA27X
29 config USB_GADGET_GOKU
30 boolean "Toshiba TC86C001 'Goku-S'"
32 --- a/drivers/usb/gadget/Makefile
33 +++ b/drivers/usb/gadget/Makefile
34 @@ -7,6 +7,7 @@ obj-$(CONFIG_USB_PXA2XX) += pxa2xx_udc.o
35 obj-$(CONFIG_USB_GOKU) += goku_udc.o
36 obj-$(CONFIG_USB_OMAP) += omap_udc.o
37 obj-$(CONFIG_USB_LH7A40X) += lh7a40x_udc.o
38 +obj-$(CONFIG_USB_PXA27X) += pxa27x_udc.o
39 obj-$(CONFIG_USB_AT91) += at91_udc.o
40 obj-$(CONFIG_USB_GADGET_GUMSTIX) += gumstix_gadget.o
43 +++ b/drivers/usb/gadget/pxa27x_udc.c
46 + * linux/drivers/usb/gadget/pxa27x_udc.c
47 + * Intel PXA2xx and IXP4xx on-chip full speed USB device controllers
49 + * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
50 + * Copyright (C) 2003 Robert Schwebel, Pengutronix
51 + * Copyright (C) 2003 Benedikt Spranger, Pengutronix
52 + * Copyright (C) 2003 David Brownell
53 + * Copyright (C) 2003 Joshua Wise
54 + * Copyright (C) 2004 Intel Corporation
55 + * Copyright (C) 2005 SDG Systems, LLC (Aric Blumer)
57 + * This program is free software; you can redistribute it and/or modify
58 + * it under the terms of the GNU General Public License as published by
59 + * the Free Software Foundation; either version 2 of the License, or
60 + * (at your option) any later version.
62 + * This program is distributed in the hope that it will be useful,
63 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
64 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
65 + * GNU General Public License for more details.
67 + * You should have received a copy of the GNU General Public License
68 + * along with this program; if not, write to the Free Software
69 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
75 + //#define VERBOSE DBG_VERBOSE
77 +#include <linux/module.h>
78 +#include <linux/kernel.h>
79 +#include <linux/ioport.h>
80 +#include <linux/types.h>
81 +#include <linux/version.h>
82 +#include <linux/errno.h>
83 +#include <linux/delay.h>
84 +#include <linux/sched.h>
85 +#include <linux/slab.h>
86 +#include <linux/init.h>
87 +#include <linux/timer.h>
88 +#include <linux/list.h>
89 +#include <linux/interrupt.h>
90 +#include <linux/proc_fs.h>
91 +#include <linux/mm.h>
92 +#include <linux/platform_device.h>
93 +#include <linux/dma-mapping.h>
94 +#include <linux/irq.h>
96 +#include <asm/byteorder.h>
99 +#include <asm/system.h>
100 +#include <asm/mach-types.h>
101 +#include <asm/unaligned.h>
102 +#include <asm/hardware.h>
103 +#include <asm/mach/irq.h>
104 +#include <asm/arch/pxa-regs.h>
106 +#include <linux/usb/ch9.h>
107 +#include <linux/usb_gadget.h>
109 +#include <asm/arch/udc.h>
113 + * This driver handles the USB Device Controller (UDC) in Intel's PXA 27777777x
114 + * series processors.
115 + * Such controller drivers work with a gadget driver. The gadget driver
116 + * returns descriptors, implements configuration and data protocols used
117 + * by the host to interact with this device, and allocates endpoints to
118 + * the different protocol interfaces. The controller driver virtualizes
119 + * usb hardware so that the gadget drivers will be more portable.
121 + * This UDC hardware wants to implement a bit too much USB protocol, so
122 + * it constrains the sorts of USB configuration change events that work.
123 + * The errata for these chips are misleading; some "fixed" bugs from
124 + * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
127 +#define DRIVER_VERSION "21-Jul-2005"
128 +#define DRIVER_DESC "PXA 27x USB Device Controller driver"
131 +static const char driver_name [] = "pxa27x_udc";
133 +static const char ep0name [] = "ep0";
137 +//#define DISABLE_TEST_MODE
139 +#ifdef CONFIG_PROC_FS
140 +#define UDC_PROC_FILE
143 +#include "pxa27x_udc.h"
146 +#ifdef CONFIG_EMBEDDED
147 +/* few strings, and little code to use them */
149 +#undef UDC_PROC_FILE
154 +static int use_dma = 1;
155 +module_param(use_dma, bool, 0);
156 +MODULE_PARM_DESC (use_dma, "true to use dma");
158 +static void dma_nodesc_handler (int dmach, void *_ep);
159 +static void kick_dma(struct pxa27x_ep *ep, struct pxa27x_request *req);
161 +#define DMASTR " (dma support)"
163 +#else /* !USE_DMA */
164 +#define DMASTR " (pio only)"
167 +#ifdef CONFIG_USB_PXA27X_SMALL
168 +#define SIZE_STR " (small)"
173 +#ifdef DISABLE_TEST_MODE
174 +/* (mode == 0) == no undocumented chip tweaks
175 + * (mode & 1) == double buffer bulk IN
176 + * (mode & 2) == double buffer bulk OUT
177 + * ... so mode = 3 (or 7, 15, etc) does it for both
179 +static ushort fifo_mode = 0;
180 +module_param(fifo_mode, ushort, 0);
181 +MODULE_PARM_DESC (fifo_mode, "pxa27x udc fifo mode");
184 +#define UDCISR0_IR0 0x3
185 +#define UDCISR_INT_MASK (UDC_INT_FIFOERROR | UDC_INT_PACKETCMP)
186 +#define UDCICR_INT_MASK UDCISR_INT_MASK
188 +#define UDCCSR_MASK (UDCCSR_FST | UDCCSR_DME)
189 +/* ---------------------------------------------------------------------------
190 + * endpoint related parts of the api to the usb controller hardware,
191 + * used by gadget driver; and the inner talker-to-hardware core.
192 + * ---------------------------------------------------------------------------
195 +static void pxa27x_ep_fifo_flush (struct usb_ep *ep);
196 +static void nuke (struct pxa27x_ep *, int status);
198 +static void pio_irq_enable(int ep_num)
201 + UDCICR0 |= 3 << (ep_num * 2);
204 + UDCICR1 |= 3 << (ep_num * 2);
208 +static void pio_irq_disable(int ep_num)
212 + UDCICR0 &= ~(3 << (ep_num * 2));
215 + UDCICR1 &= ~(3 << (ep_num * 2));
219 +/* The UDCCR reg contains mask and interrupt status bits,
220 + * so using '|=' isn't safe as it may ack an interrupt.
222 +#define UDCCR_MASK_BITS (UDCCR_OEN | UDCCR_UDE)
224 +static inline void udc_set_mask_UDCCR(int mask)
226 + UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
229 +static inline void udc_clear_mask_UDCCR(int mask)
231 + UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
234 +static inline void udc_ack_int_UDCCR(int mask)
236 + /* udccr contains the bits we dont want to change */
237 + __u32 udccr = UDCCR & UDCCR_MASK_BITS;
239 + UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
243 + * endpoint enable/disable
245 + * we need to verify the descriptors used to enable endpoints. since pxa27x
246 + * endpoint configurations are fixed, and are pretty much always enabled,
247 + * there's not a lot to manage here.
249 + * because pxa27x can't selectively initialize bulk (or interrupt) endpoints,
250 + * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
251 + * for a single interface (with only the default altsetting) and for gadget
252 + * drivers that don't halt endpoints (not reset by set_interface). that also
253 + * means that if you use ISO, you must violate the USB spec rule that all
254 + * iso endpoints must be in non-default altsettings.
256 +static int pxa27x_ep_enable (struct usb_ep *_ep,
257 + const struct usb_endpoint_descriptor *desc)
259 + struct pxa27x_ep *ep;
260 + struct pxa27x_udc *dev;
262 + ep = container_of (_ep, struct pxa27x_ep, ep);
263 + if (!_ep || !desc || _ep->name == ep0name
264 + || desc->bDescriptorType != USB_DT_ENDPOINT
265 + || ep->fifo_size < le16_to_cpu(desc->wMaxPacketSize)) {
266 + DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
270 + /* xfer types must match, except that interrupt ~= bulk */
271 + if( ep->ep_type != USB_ENDPOINT_XFER_BULK
272 + && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
273 + DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
277 + /* hardware _could_ do smaller, but driver doesn't */
278 + if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
279 + && le16_to_cpu (desc->wMaxPacketSize)
281 + || !desc->wMaxPacketSize) {
282 + DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
287 + if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
288 + DMSG("%s, bogus device state\n", __FUNCTION__);
295 + ep->pio_irqs = ep->dma_irqs = 0;
296 + ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
298 + /* flush fifo (mostly for OUT buffers) */
299 + pxa27x_ep_fifo_flush (_ep);
301 + /* ... reset halt state too, if we could ... */
304 + /* for (some) bulk and ISO endpoints, try to get a DMA channel and
305 + * bind it to the endpoint. otherwise use PIO.
307 + DMSG("%s: called attributes=%d\n", __FUNCTION__, ep->ep_type);
308 + switch (ep->ep_type) {
309 + case USB_ENDPOINT_XFER_ISOC:
310 + if (le16_to_cpu(desc->wMaxPacketSize) % 32)
313 + case USB_ENDPOINT_XFER_BULK:
314 + if (!use_dma || !ep->reg_drcmr)
316 + ep->dma = pxa_request_dma ((char *)_ep->name,
317 + (le16_to_cpu (desc->wMaxPacketSize) > 64)
318 + ? DMA_PRIO_MEDIUM /* some iso */
320 + dma_nodesc_handler, ep);
321 + if (ep->dma >= 0) {
322 + *ep->reg_drcmr = DRCMR_MAPVLD | ep->dma;
323 + DMSG("%s using dma%d\n", _ep->name, ep->dma);
329 + DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
333 +static int pxa27x_ep_disable (struct usb_ep *_ep)
335 + struct pxa27x_ep *ep;
337 + ep = container_of (_ep, struct pxa27x_ep, ep);
338 + if (!_ep || !ep->desc) {
339 + DMSG("%s, %s not enabled\n", __FUNCTION__,
340 + _ep ? ep->ep.name : NULL);
343 + nuke (ep, -ESHUTDOWN);
346 + if (ep->dma >= 0) {
347 + *ep->reg_drcmr = 0;
348 + pxa_free_dma (ep->dma);
353 + /* flush fifo (mostly for IN buffers) */
354 + pxa27x_ep_fifo_flush (_ep);
359 + DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
363 +/*-------------------------------------------------------------------------*/
365 +/* for the pxa27x, these can just wrap kmalloc/kfree. gadget drivers
366 + * must still pass correctly initialized endpoints, since other controller
367 + * drivers may care about how it's currently set up (dma issues etc).
371 + * pxa27x_ep_alloc_request - allocate a request data structure
373 +static struct usb_request *
374 +pxa27x_ep_alloc_request (struct usb_ep *_ep, unsigned gfp_flags)
376 + struct pxa27x_request *req;
378 + req = kmalloc (sizeof *req, gfp_flags);
382 + memset (req, 0, sizeof *req);
383 + INIT_LIST_HEAD (&req->queue);
389 + * pxa27x_ep_free_request - deallocate a request data structure
392 +pxa27x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
394 + struct pxa27x_request *req;
396 + req = container_of(_req, struct pxa27x_request, req);
397 + WARN_ON (!list_empty (&req->queue));
402 +/* PXA cache needs flushing with DMA I/O (it's dma-incoherent), but there's
403 + * no device-affinity and the heap works perfectly well for i/o buffers.
404 + * It wastes much less memory than dma_alloc_coherent() would, and even
405 + * prevents cacheline (32 bytes wide) sharing problems.
408 +pxa27x_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
409 + dma_addr_t *dma, unsigned gfp_flags)
413 + retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
415 + *dma = virt_to_bus (retval);
420 +pxa27x_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma,
426 +/*-------------------------------------------------------------------------*/
429 + * done - retire a request; caller blocked irqs
431 +static void done(struct pxa27x_ep *ep, struct pxa27x_request *req, int status)
433 + list_del_init(&req->queue);
434 + if (likely (req->req.status == -EINPROGRESS))
435 + req->req.status = status;
437 + status = req->req.status;
439 + if (status && status != -ESHUTDOWN)
440 + DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
441 + ep->ep.name, &req->req, status,
442 + req->req.actual, req->req.length);
444 + /* don't modify queue heads during completion callback */
445 + req->req.complete(&ep->ep, &req->req);
449 +static inline void ep0_idle (struct pxa27x_udc *dev)
451 + dev->ep0state = EP0_IDLE;
456 +write_packet(volatile u32 *uddr, struct pxa27x_request *req, unsigned max)
459 + int length, count, remain;
461 + buf = (u32*)(req->req.buf + req->req.actual);
464 + /* how big will this packet be? */
465 + length = min(req->req.length - req->req.actual, max);
466 + req->req.actual += length;
468 + remain = length & 0x3;
469 + count = length & ~(0x3);
471 + while (likely(count)) {
477 + volatile u8* reg=(u8*)uddr;
478 + char *rd =(u8*)buf;
489 + * write to an IN endpoint fifo, as many packets as possible.
490 + * irqs will use this to write the rest later.
491 + * caller guarantees at least one packet buffer is ready (or a zlp).
494 +write_fifo (struct pxa27x_ep *ep, struct pxa27x_request *req)
498 + max = le16_to_cpu(ep->desc->wMaxPacketSize);
501 + int is_last, is_short;
503 + count = write_packet(ep->reg_udcdr, req, max);
505 + /* last packet is usually short (or a zlp) */
506 + if (unlikely (count != max))
507 + is_last = is_short = 1;
509 + if (likely(req->req.length != req->req.actual)
514 + /* interrupt/iso maxpacket may not fill the fifo */
515 + is_short = unlikely (max < ep->fifo_size);
518 + DMSG("wrote %s count:%d bytes%s%s %d left %p\n",
519 + ep->ep.name, count,
520 + is_last ? "/L" : "", is_short ? "/S" : "",
521 + req->req.length - req->req.actual, &req->req);
523 + /* let loose that packet. maybe try writing another one,
524 + * double buffering might work. TSP, TPC, and TFS
525 + * bit values are the same for all normal IN endpoints.
527 + *ep->reg_udccsr = UDCCSR_PC;
529 + *ep->reg_udccsr = UDCCSR_SP;
531 + /* requests complete when all IN data is in the FIFO */
534 + if (list_empty(&ep->queue) || unlikely(ep->dma >= 0)) {
535 + pio_irq_disable (ep->ep_num);
537 + /* unaligned data and zlps couldn't use dma */
538 + if (unlikely(!list_empty(&ep->queue))) {
539 + req = list_entry(ep->queue.next,
540 + struct pxa27x_request, queue);
549 + // TODO experiment: how robust can fifo mode tweaking be?
550 + // double buffering is off in the default fifo mode, which
551 + // prevents TFS from being set here.
553 + } while (*ep->reg_udccsr & UDCCSR_FS);
557 +/* caller asserts req->pending (ep0 irq status nyet cleared); starts
558 + * ep0 data stage. these chips want very simple state transitions.
561 +void ep0start(struct pxa27x_udc *dev, u32 flags, const char *tag)
563 + UDCCSR0 = flags|UDCCSR0_SA|UDCCSR0_OPC;
564 + UDCISR0 = UDCICR_INT(0, UDC_INT_FIFOERROR | UDC_INT_PACKETCMP);
565 + dev->req_pending = 0;
566 + DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
567 + __FUNCTION__, tag, UDCCSR0, flags);
571 +write_ep0_fifo (struct pxa27x_ep *ep, struct pxa27x_request *req)
576 + count = write_packet(&UDCDR0, req, EP0_FIFO_SIZE);
577 + ep->dev->stats.write.bytes += count;
579 + /* last packet "must be" short (or a zlp) */
580 + is_short = (count != EP0_FIFO_SIZE);
582 + DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
583 + req->req.length - req->req.actual, &req->req);
585 + if (unlikely (is_short)) {
586 + if (ep->dev->req_pending)
587 + ep0start(ep->dev, UDCCSR0_IPR, "short IN");
589 + UDCCSR0 = UDCCSR0_IPR;
591 + count = req->req.length;
595 + /* This seems to get rid of lost status irqs in some cases:
596 + * host responds quickly, or next request involves config
597 + * change automagic, or should have been hidden, or ...
599 + * FIXME get rid of all udelays possible...
601 + if (count >= EP0_FIFO_SIZE) {
604 + if ((UDCCSR0 & UDCCSR0_OPC) != 0) {
605 + /* clear OPC, generate ack */
606 + UDCCSR0 = UDCCSR0_OPC;
614 + } else if (ep->dev->req_pending)
615 + ep0start(ep->dev, 0, "IN");
621 + * read_fifo - unload packet(s) from the fifo we use for usb OUT
622 + * transfers and put them into the request. caller should have made
623 + * sure there's at least one packet ready.
625 + * returns true if the request completed because of short packet or the
626 + * request buffer having filled (and maybe overran till end-of-packet).
629 +read_fifo (struct pxa27x_ep *ep, struct pxa27x_request *req)
633 + int bufferspace, count, is_short;
635 + /* make sure there's a packet in the FIFO.*/
636 + if (unlikely ((*ep->reg_udccsr & UDCCSR_PC) == 0))
638 + buf =(u32*) (req->req.buf + req->req.actual);
640 + bufferspace = req->req.length - req->req.actual;
642 + /* read all bytes from this packet */
643 + if (likely (*ep->reg_udccsr & UDCCSR_BNE)) {
644 + count = 0x3ff & *ep->reg_udcbcr;
645 + req->req.actual += min (count, bufferspace);
649 + is_short = (count < ep->ep.maxpacket);
650 + DMSG("read %s udccsr:%02x, count:%d bytes%s req %p %d/%d\n",
651 + ep->ep.name, *ep->reg_udccsr, count,
652 + is_short ? "/S" : "",
653 + &req->req, req->req.actual, req->req.length);
655 +// dump_regs(ep->ep_num );
656 + count = min(count, bufferspace);
657 + while (likely (count > 0)) {
658 + *buf++ = *ep->reg_udcdr;
661 + DMSG("Buf:0x%p\n", req->req.buf);
663 + *ep->reg_udccsr = UDCCSR_PC;
664 + /* RPC/RSP/RNE could now reflect the other packet buffer */
667 + if (is_short || req->req.actual == req->req.length) {
669 + if (list_empty(&ep->queue))
670 + pio_irq_disable (ep->ep_num);
674 + /* finished that packet. the next one may be waiting... */
680 + * special ep0 version of the above. no UBCR0 or double buffering; status
681 + * handshaking is magic. most device protocols don't need control-OUT.
682 + * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
683 + * protocols do use them.
686 +read_ep0_fifo (struct pxa27x_ep *ep, struct pxa27x_request *req)
689 + unsigned bufferspace;
691 + buf = (u32*) (req->req.buf + req->req.actual);
692 + bufferspace = req->req.length - req->req.actual;
694 + while (UDCCSR0 & UDCCSR0_RNE) {
697 + if (unlikely (bufferspace == 0)) {
698 + /* this happens when the driver's buffer
699 + * is smaller than what the host sent.
700 + * discard the extra data.
702 + if (req->req.status != -EOVERFLOW)
703 + DMSG("%s overflow\n", ep->ep.name);
704 + req->req.status = -EOVERFLOW;
707 + req->req.actual += 4;
712 + UDCCSR0 = UDCCSR0_OPC ;
715 + if (req->req.actual >= req->req.length)
718 + /* finished that packet. the next one may be waiting... */
724 +#define MAX_IN_DMA ((DCMD_LENGTH + 1) - BULK_FIFO_SIZE)
725 +static void kick_dma(struct pxa27x_ep *ep, struct pxa27x_request *req)
728 + u32 len = req->req.length;
729 + u32 buf = req->req.dma;
730 + u32 fifo = io_v2p ((u32)ep->reg_udcdr);
732 + buf += req->req.actual;
733 + len -= req->req.actual;
736 + DMSG("%s: req:0x%p length:%d, actual:%d dma:%d\n",
737 + __FUNCTION__, &req->req, req->req.length,
738 + req->req.actual,ep->dma);
740 + /* no-descriptor mode can be simple for bulk-in, iso-in, iso-out */
741 + DCSR(ep->dma) = DCSR_NODESC;
743 + DALGN |= 1 << ep->dma;
745 + DALGN &= ~(1 << ep->dma);
748 + DSADR(ep->dma) = buf;
749 + DTADR(ep->dma) = fifo;
750 + if (len > MAX_IN_DMA) {
753 + } else if (len >= ep->ep.maxpacket) {
754 + if ((ep->dma_con = (len % ep->ep.maxpacket) != 0))
755 + len = ep->ep.maxpacket;
757 + dcmd = len | DCMD_BURST32 | DCMD_WIDTH4 | DCMD_ENDIRQEN
758 + | DCMD_FLOWTRG | DCMD_INCSRCADDR;
760 + DSADR(ep->dma) = fifo;
761 + DTADR(ep->dma) = buf;
762 + dcmd = len | DCMD_BURST32 | DCMD_WIDTH4 | DCMD_ENDIRQEN
763 + | DCMD_FLOWSRC | DCMD_INCTRGADDR;
765 + *ep->reg_udccsr = UDCCSR_DME;
766 + DCMD(ep->dma) = dcmd;
767 + DCSR(ep->dma) = DCSR_NODESC | DCSR_EORIRQEN \
768 + | ((ep->dir_in) ? DCSR_STOPIRQEN : 0);
769 + *ep->reg_drcmr = ep->dma | DRCMR_MAPVLD;
770 + DCSR(ep->dma) |= DCSR_RUN;
773 +static void cancel_dma(struct pxa27x_ep *ep)
775 + struct pxa27x_request *req;
778 + if (DCSR(ep->dma) == 0 || list_empty(&ep->queue))
781 + DMSG("hehe dma:%d,dcsr:0x%x\n", ep->dma, DCSR(ep->dma));
783 + while ((DCSR(ep->dma) & DCSR_STOPSTATE) == 0)
786 + req = list_entry(ep->queue.next, struct pxa27x_request, queue);
787 + tmp = DCMD(ep->dma) & DCMD_LENGTH;
788 + req->req.actual = req->req.length - tmp;
790 + /* the last tx packet may be incomplete, so flush the fifo.
791 + * FIXME correct req.actual if we can
793 + *ep->reg_udccsr = UDCCSR_FEF;
796 +static void dma_nodesc_handler(int dmach, void *_ep)
798 + struct pxa27x_ep *ep = _ep;
799 + struct pxa27x_request *req, *req_next;
800 + u32 dcsr, tmp, completed;
802 + local_irq_disable();
804 + req = list_entry(ep->queue.next, struct pxa27x_request, queue);
806 + DMSG("%s, buf:0x%p\n",__FUNCTION__, req->req.buf);
809 + ep->dev->stats.irqs++;
810 + HEX_DISPLAY(ep->dev->stats.irqs);
814 + dcsr = DCSR(dmach);
815 + DCSR(ep->dma) &= ~DCSR_RUN;
817 + if (dcsr & DCSR_BUSERR) {
818 + DCSR(dmach) = DCSR_BUSERR;
819 + printk(KERN_ERR " Buss Error\n");
820 + req->req.status = -EIO;
822 + } else if (dcsr & DCSR_ENDINTR) {
823 + DCSR(dmach) = DCSR_ENDINTR;
825 + tmp = req->req.length - req->req.actual;
826 + /* Last packet is a short one*/
827 + if ( tmp < ep->ep.maxpacket) {
830 + *ep->reg_udccsr = UDCCSR_SP | \
831 + (*ep->reg_udccsr & UDCCSR_MASK);
832 + /*Wait for packet out */
833 + while( (count++ < 10000) && \
834 + !(*ep->reg_udccsr & UDCCSR_FS));
835 + if (count >= 10000)
836 + DMSG("Failed to send packet\n");
838 + DMSG("%s: short packet sent len:%d,"
839 + "length:%d,actual:%d\n", __FUNCTION__,
840 + tmp, req->req.length, req->req.actual);
841 + req->req.actual = req->req.length;
843 + /* There are still packets to transfer */
844 + } else if ( ep->dma_con) {
845 + DMSG("%s: more packets,length:%d,actual:%d\n",
846 + __FUNCTION__,req->req.length,
848 + req->req.actual += ep->ep.maxpacket;
851 + DMSG("%s: no more packets,length:%d,"
852 + "actual:%d\n", __FUNCTION__,
853 + req->req.length, req->req.actual);
854 + req->req.actual = req->req.length;
858 + req->req.actual = req->req.length;
861 + } else if (dcsr & DCSR_EORINTR) { //Only happened in OUT DMA
862 + int remain,udccsr ;
864 + DCSR(dmach) = DCSR_EORINTR;
865 + remain = DCMD(dmach) & DCMD_LENGTH;
866 + req->req.actual = req->req.length - remain;
868 + udccsr = *ep->reg_udccsr;
869 + if (udccsr & UDCCSR_SP) {
870 + *ep->reg_udccsr = UDCCSR_PC | (udccsr & UDCCSR_MASK);
873 + DMSG("%s: length:%d actual:%d\n",
874 + __FUNCTION__, req->req.length, req->req.actual);
876 + DMSG("%s: Others dma:%d DCSR:0x%x DCMD:0x%x\n",
877 + __FUNCTION__, dmach, DCSR(dmach), DCMD(dmach));
879 + if (likely(completed)) {
880 + if (req->queue.next != &ep->queue) {
881 + req_next = list_entry(req->queue.next,
882 + struct pxa27x_request, queue);
883 + kick_dma(ep, req_next);
890 + local_irq_enable();
894 +/*-------------------------------------------------------------------------*/
897 +pxa27x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, unsigned gfp_flags)
899 + struct pxa27x_ep *ep;
900 + struct pxa27x_request *req;
901 + struct pxa27x_udc *dev;
902 + unsigned long flags;
904 + req = container_of(_req, struct pxa27x_request, req);
905 + if (unlikely (!_req || !_req->complete || !_req->buf||
906 + !list_empty(&req->queue))) {
907 + DMSG("%s, bad params\n", __FUNCTION__);
911 + ep = container_of(_ep, struct pxa27x_ep, ep);
912 + if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
913 + DMSG("%s, bad ep\n", __FUNCTION__);
917 + DMSG("%s, ep point %d is queue\n", __FUNCTION__, ep->ep_num);
920 + if (unlikely (!dev->driver
921 + || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
922 + DMSG("%s, bogus device state\n", __FUNCTION__);
926 + /* iso is always one packet per request, that's the only way
927 + * we can report per-packet status. that also helps with dma.
929 + if (unlikely (ep->ep_type == USB_ENDPOINT_XFER_ISOC
930 + && req->req.length > le16_to_cpu
931 + (ep->desc->wMaxPacketSize)))
935 + // FIXME caller may already have done the dma mapping
936 + if (ep->dma >= 0) {
937 + _req->dma = dma_map_single(dev->dev, _req->buf, _req->length,
938 + (ep->dir_in) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
942 + DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
943 + _ep->name, _req, _req->length, _req->buf);
945 + local_irq_save(flags);
947 + _req->status = -EINPROGRESS;
950 + /* kickstart this i/o queue? */
951 + if (list_empty(&ep->queue) && !ep->stopped) {
952 + if (ep->desc == 0 /* ep0 */) {
953 + unsigned length = _req->length;
955 + switch (dev->ep0state) {
956 + case EP0_IN_DATA_PHASE:
957 + dev->stats.write.ops++;
958 + if (write_ep0_fifo(ep, req))
962 + case EP0_OUT_DATA_PHASE:
963 + dev->stats.read.ops++;
964 + if (dev->req_pending)
965 + ep0start(dev, UDCCSR0_IPR, "OUT");
966 + if (length == 0 || ((UDCCSR0 & UDCCSR0_RNE) != 0
967 + && read_ep0_fifo(ep, req))) {
973 + case EP0_NO_ACTION:
978 + DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
979 + local_irq_restore (flags);
983 + /* either start dma or prime pio pump */
984 + } else if (ep->dma >= 0) {
987 + /* can the FIFO can satisfy the request immediately? */
988 + } else if (ep->dir_in
989 + && (*ep->reg_udccsr & UDCCSR_FS) != 0
990 + && write_fifo(ep, req)) {
992 + } else if ((*ep->reg_udccsr & UDCCSR_FS) != 0
993 + && read_fifo(ep, req)) {
996 + DMSG("req:%p,ep->desc:%p,ep->dma:%d\n", req, ep->desc, ep->dma);
997 + if (likely (req && ep->desc) && ep->dma < 0)
998 + pio_irq_enable(ep->ep_num);
1001 + /* pio or dma irq handler advances the queue. */
1002 + if (likely (req != 0))
1003 + list_add_tail(&req->queue, &ep->queue);
1004 + local_irq_restore(flags);
1011 + * nuke - dequeue ALL requests
1013 +static void nuke(struct pxa27x_ep *ep, int status)
1015 + struct pxa27x_request *req;
1017 + /* called with irqs blocked */
1019 + if (ep->dma >= 0 && !ep->stopped)
1022 + while (!list_empty(&ep->queue)) {
1023 + req = list_entry(ep->queue.next, struct pxa27x_request, queue);
1024 + done(ep, req, status);
1027 + pio_irq_disable (ep->ep_num);
1031 +/* dequeue JUST ONE request */
1032 +static int pxa27x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1034 + struct pxa27x_ep *ep;
1035 + struct pxa27x_request *req;
1036 + unsigned long flags;
1038 + ep = container_of(_ep, struct pxa27x_ep, ep);
1039 + if (!_ep || ep->ep.name == ep0name)
1042 + local_irq_save(flags);
1044 + /* make sure it's actually queued on this endpoint */
1045 + list_for_each_entry (req, &ep->queue, queue) {
1046 + if (&req->req == _req)
1049 + if (&req->req != _req) {
1050 + local_irq_restore(flags);
1055 + if (ep->dma >= 0 && ep->queue.next == &req->queue && !ep->stopped) {
1057 + done(ep, req, -ECONNRESET);
1059 + if (!list_empty(&ep->queue)) {
1060 + req = list_entry(ep->queue.next,
1061 + struct pxa27x_request, queue);
1062 + kick_dma(ep, req);
1066 + done(ep, req, -ECONNRESET);
1068 + local_irq_restore(flags);
1072 +/*-------------------------------------------------------------------------*/
1074 +static int pxa27x_ep_set_halt(struct usb_ep *_ep, int value)
1076 + struct pxa27x_ep *ep;
1077 + unsigned long flags;
1079 + DMSG("%s is called\n", __FUNCTION__);
1080 + ep = container_of(_ep, struct pxa27x_ep, ep);
1081 + if (unlikely (!_ep
1082 + || (!ep->desc && ep->ep.name != ep0name))
1083 + || ep->ep_type == USB_ENDPOINT_XFER_ISOC) {
1084 + DMSG("%s, bad ep\n", __FUNCTION__);
1088 + /* this path (reset toggle+halt) is needed to implement
1089 + * SET_INTERFACE on normal hardware. but it can't be
1090 + * done from software on the PXA UDC, and the hardware
1091 + * forgets to do it as part of SET_INTERFACE automagic.
1093 + DMSG("only host can clear %s halt\n", _ep->name);
1097 + local_irq_save(flags);
1099 + if (ep->dir_in && ((*ep->reg_udccsr & UDCCSR_FS) == 0
1100 + || !list_empty(&ep->queue))) {
1101 + local_irq_restore(flags);
1105 + /* FST bit is the same for control, bulk in, bulk out, interrupt in */
1106 + *ep->reg_udccsr = UDCCSR_FST|UDCCSR_FEF;
1108 + /* ep0 needs special care */
1110 + start_watchdog(ep->dev);
1111 + ep->dev->req_pending = 0;
1112 + ep->dev->ep0state = EP0_STALL;
1115 + /* and bulk/intr endpoints like dropping stalls too */
1118 + for (i = 0; i < 1000; i += 20) {
1119 + if (*ep->reg_udccsr & UDCCSR_SST)
1124 + local_irq_restore(flags);
1126 + DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
1130 +static int pxa27x_ep_fifo_status(struct usb_ep *_ep)
1132 + struct pxa27x_ep *ep;
1134 + ep = container_of(_ep, struct pxa27x_ep, ep);
1136 + DMSG("%s, bad ep\n", __FUNCTION__);
1139 + /* pxa can't report unclaimed bytes from IN fifos */
1141 + return -EOPNOTSUPP;
1142 + if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
1143 + || (*ep->reg_udccsr & UDCCSR_FS) == 0)
1146 + return (*ep->reg_udcbcr & 0xfff) + 1;
1149 +static void pxa27x_ep_fifo_flush(struct usb_ep *_ep)
1151 + struct pxa27x_ep *ep;
1153 + ep = container_of(_ep, struct pxa27x_ep, ep);
1154 + if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
1155 + DMSG("%s, bad ep\n", __FUNCTION__);
1159 + /* toggle and halt bits stay unchanged */
1161 + /* for OUT, just read and discard the FIFO contents. */
1162 + if (!ep->dir_in) {
1163 + while (((*ep->reg_udccsr) & UDCCSR_BNE) != 0)
1164 + (void) *ep->reg_udcdr;
1168 + /* most IN status is the same, but ISO can't stall */
1169 + *ep->reg_udccsr = UDCCSR_PC|UDCCSR_FST|UDCCSR_TRN
1170 + | (ep->ep_type == USB_ENDPOINT_XFER_ISOC)
1175 +static struct usb_ep_ops pxa27x_ep_ops = {
1176 + .enable = pxa27x_ep_enable,
1177 + .disable = pxa27x_ep_disable,
1179 + .alloc_request = pxa27x_ep_alloc_request,
1180 + .free_request = pxa27x_ep_free_request,
1182 + .alloc_buffer = pxa27x_ep_alloc_buffer,
1183 + .free_buffer = pxa27x_ep_free_buffer,
1185 + .queue = pxa27x_ep_queue,
1186 + .dequeue = pxa27x_ep_dequeue,
1188 + .set_halt = pxa27x_ep_set_halt,
1189 + .fifo_status = pxa27x_ep_fifo_status,
1190 + .fifo_flush = pxa27x_ep_fifo_flush,
1194 +/* ---------------------------------------------------------------------------
1195 + * device-scoped parts of the api to the usb controller hardware
1196 + * ---------------------------------------------------------------------------
1199 +static int pxa27x_udc_get_frame(struct usb_gadget *_gadget)
1201 + return (UDCFNR & 0x3FF);
1204 +static int pxa27x_udc_wakeup(struct usb_gadget *_gadget)
1206 + /* host may not have enabled remote wakeup */
1207 + if ((UDCCR & UDCCR_DWRE) == 0)
1208 + return -EHOSTUNREACH;
1209 + udc_set_mask_UDCCR(UDCCR_UDR);
1213 +static const struct usb_gadget_ops pxa27x_udc_ops = {
1214 + .get_frame = pxa27x_udc_get_frame,
1215 + .wakeup = pxa27x_udc_wakeup,
1216 + // current versions must always be self-powered
1220 +/*-------------------------------------------------------------------------*/
1222 +#ifdef UDC_PROC_FILE
1224 +static const char proc_node_name [] = "driver/udc";
1227 +udc_proc_read(char *page, char **start, off_t off, int count,
1228 + int *eof, void *_dev)
1231 + struct pxa27x_udc *dev = _dev;
1233 + unsigned size = count;
1234 + unsigned long flags;
1241 + local_irq_save(flags);
1243 + /* basic device status */
1244 + t = scnprintf(next, size, DRIVER_DESC "\n"
1245 + "%s version: %s\nGadget driver: %s\n",
1246 + driver_name, DRIVER_VERSION SIZE_STR DMASTR,
1247 + dev->driver ? dev->driver->driver.name : "(none)");
1251 + /* registers for device and ep0 */
1252 + t = scnprintf(next, size,
1253 + "uicr %02X.%02X, usir %02X.%02x, ufnr %02X\n",
1254 + UDCICR1, UDCICR0, UDCISR1, UDCISR0, UDCFNR);
1259 + t = scnprintf(next, size,"udccr %02X =%s%s%s%s%s%s%s%s%s%s, con=%d,inter=%d,altinter=%d\n", tmp,
1260 + (tmp & UDCCR_OEN) ? " oen":"",
1261 + (tmp & UDCCR_AALTHNP) ? " aalthnp":"",
1262 + (tmp & UDCCR_AHNP) ? " rem" : "",
1263 + (tmp & UDCCR_BHNP) ? " rstir" : "",
1264 + (tmp & UDCCR_DWRE) ? " dwre" : "",
1265 + (tmp & UDCCR_SMAC) ? " smac" : "",
1266 + (tmp & UDCCR_EMCE) ? " emce" : "",
1267 + (tmp & UDCCR_UDR) ? " udr" : "",
1268 + (tmp & UDCCR_UDA) ? " uda" : "",
1269 + (tmp & UDCCR_UDE) ? " ude" : "",
1270 + (tmp & UDCCR_ACN) >> UDCCR_ACN_S,
1271 + (tmp & UDCCR_AIN) >> UDCCR_AIN_S,
1272 + (tmp & UDCCR_AAISN)>> UDCCR_AAISN_S );
1278 + t = scnprintf(next, size,
1279 + "udccsr0 %02X =%s%s%s%s%s%s%s\n", tmp,
1280 + (tmp & UDCCSR0_SA) ? " sa" : "",
1281 + (tmp & UDCCSR0_RNE) ? " rne" : "",
1282 + (tmp & UDCCSR0_FST) ? " fst" : "",
1283 + (tmp & UDCCSR0_SST) ? " sst" : "",
1284 + (tmp & UDCCSR0_DME) ? " dme" : "",
1285 + (tmp & UDCCSR0_IPR) ? " ipr" : "",
1286 + (tmp & UDCCSR0_OPC) ? " opc" : "");
1293 + t = scnprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1294 + dev->stats.write.bytes, dev->stats.write.ops,
1295 + dev->stats.read.bytes, dev->stats.read.ops,
1300 + /* dump endpoint queues */
1301 + for (i = 0; i < UDC_EP_NUM; i++) {
1302 + struct pxa27x_ep *ep = &dev->ep [i];
1303 + struct pxa27x_request *req;
1307 + const struct usb_endpoint_descriptor *d;
1312 + tmp = *dev->ep [i].reg_udccsr;
1313 + t = scnprintf(next, size,
1314 + "%s max %d %s udccs %02x udccr:0x%x\n",
1315 + ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
1316 + (ep->dma >= 0) ? "dma" : "pio", tmp,
1317 + *dev->ep[i].reg_udccr);
1318 + /* TODO translate all five groups of udccs bits! */
1320 + } else /* ep0 should only have one transfer queued */
1321 + t = scnprintf(next, size, "ep0 max 16 pio irqs %lu\n",
1323 + if (t <= 0 || t > size)
1328 + if (list_empty(&ep->queue)) {
1329 + t = scnprintf(next, size, "\t(nothing queued)\n");
1330 + if (t <= 0 || t > size)
1336 + list_for_each_entry(req, &ep->queue, queue) {
1338 + if (ep->dma >= 0 && req->queue.prev == &ep->queue)
1339 + t = scnprintf(next, size,
1340 + "\treq %p len %d/%d "
1341 + "buf %p (dma%d dcmd %08x)\n",
1342 + &req->req, req->req.actual,
1343 + req->req.length, req->req.buf,
1344 + ep->dma, DCMD(ep->dma)
1345 + // low 13 bits == bytes-to-go
1349 + t = scnprintf(next, size,
1350 + "\treq %p len %d/%d buf %p\n",
1351 + &req->req, req->req.actual,
1352 + req->req.length, req->req.buf);
1353 + if (t <= 0 || t > size)
1361 + local_irq_restore(flags);
1363 + return count - size;
1366 +#define create_proc_files() \
1367 + create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
1368 +#define remove_proc_files() \
1369 + remove_proc_entry(proc_node_name, NULL)
1371 +#else /* !UDC_PROC_FILE */
1372 +#define create_proc_files() do {} while (0)
1373 +#define remove_proc_files() do {} while (0)
1375 +#endif /* UDC_PROC_FILE */
1377 +/* "function" sysfs attribute */
1379 +show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1381 + struct pxa27x_udc *dev = dev_get_drvdata (_dev);
1384 + || !dev->driver->function
1385 + || strlen (dev->driver->function) > PAGE_SIZE)
1387 + return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1389 +static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1391 +/*-------------------------------------------------------------------------*/
1394 + * udc_disable - disable USB device controller
1396 +static void udc_disable(struct pxa27x_udc *dev)
1398 + UDCICR0 = UDCICR1 = 0x00000000;
1400 + udc_clear_mask_UDCCR(UDCCR_UDE);
1402 + /* Disable clock for USB device */
1403 + pxa_set_cken(CKEN11_USB, 0);
1406 + dev->gadget.speed = USB_SPEED_UNKNOWN;
1407 + LED_CONNECTED_OFF;
1408 + if (dev->mach->udc_command)
1409 + dev->mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
1414 + * udc_reinit - initialize software state
1416 +static void udc_reinit(struct pxa27x_udc *dev)
1420 + dev->ep0state = EP0_IDLE;
1422 + /* basic endpoint records init */
1423 + for (i = 0; i < UDC_EP_NUM; i++) {
1424 + struct pxa27x_ep *ep = &dev->ep[i];
1427 + ep->pio_irqs = ep->dma_irqs = 0;
1429 + dev->configuration = 0;
1430 + dev->interface = 0;
1431 + dev->alternate = 0;
1432 + /* the rest was statically initialized, and is read-only */
1435 +/* until it's enabled, this UDC should be completely invisible
1436 + * to any USB host.
1438 +static void udc_enable (struct pxa27x_udc *dev)
1440 + udc_clear_mask_UDCCR(UDCCR_UDE);
1442 + // MST_MSCWR2 &= ~(MST_MSCWR2_nUSBC_SC);
1444 + /* Enable clock for USB device */
1445 + pxa_set_cken(CKEN11_USB, 1);
1447 + UDCICR0 = UDCICR1 = 0;
1450 + dev->gadget.speed = USB_SPEED_FULL;
1451 + dev->stats.irqs = 0;
1453 + udc_set_mask_UDCCR(UDCCR_UDE);
1455 + if (UDCCR & UDCCR_EMCE)
1457 + printk(KERN_ERR ": There are error in configuration, udc disabled\n");
1460 + /* caller must be able to sleep in order to cope
1461 + * with startup transients.
1465 + /* enable suspend/resume and reset irqs */
1466 + UDCICR1 = UDCICR1_IECC | UDCICR1_IERU | UDCICR1_IESU | UDCICR1_IERS;
1468 + /* enable ep0 irqs */
1469 + UDCICR0 = UDCICR_INT(0,UDCICR_INT_MASK);
1471 + for(i=1; i < UDC_EP_NUM; i++) {
1472 + if (dev->ep[i].assigned)
1473 + pio_irq_enable(i);
1476 + if (dev->mach->udc_command)
1477 + dev->mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
1481 +/* when a driver is successfully registered, it will receive
1482 + * control requests including set_configuration(), which enables
1483 + * non-control requests. then usb traffic follows until a
1484 + * disconnect is reported. then a host may connect again, or
1485 + * the driver might get unbound.
1487 +int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1489 + struct pxa27x_udc *dev = the_controller;
1492 + DMSG("dev=0x%x, driver=0x%x, speed=%d,"
1493 + "bind=0x%x, unbind=0x%x, disconnect=0x%x, setup=0x%x\n",
1494 + (unsigned)dev, (unsigned)driver, driver->speed,
1495 + (unsigned)driver->bind, (unsigned)driver->unbind,
1496 + (unsigned)driver->disconnect, (unsigned)driver->setup);
1498 + if (!driver || driver->speed != USB_SPEED_FULL
1500 + || !driver->unbind
1501 + || !driver->disconnect
1502 + || !driver->setup)
1509 + /* first hook up the driver ... */
1510 + dev->driver = driver;
1511 + dev->gadget.dev.driver = &driver->driver;
1513 + device_add (&dev->gadget.dev);
1514 + retval = driver->bind(&dev->gadget);
1516 + DMSG("bind to driver %s --> error %d\n",
1517 + driver->driver.name, retval);
1518 + device_del (&dev->gadget.dev);
1521 + dev->gadget.dev.driver = 0;
1524 + device_create_file(dev->dev, &dev_attr_function);
1526 + /* ... then enable host detection and ep0; and we're ready
1527 + * for set_configuration as well as eventual disconnect.
1528 + * NOTE: this shouldn't power up until later.
1530 + DMSG("registered gadget driver '%s'\n", driver->driver.name);
1535 +EXPORT_SYMBOL(usb_gadget_register_driver);
1538 +stop_activity(struct pxa27x_udc *dev, struct usb_gadget_driver *driver)
1542 + DMSG("Trace path 1\n");
1543 + /* don't disconnect drivers more than once */
1544 + if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1546 + dev->gadget.speed = USB_SPEED_UNKNOWN;
1548 + /* prevent new request submissions, kill any outstanding requests */
1549 + for (i = 0; i < UDC_EP_NUM; i++) {
1550 + struct pxa27x_ep *ep = &dev->ep[i];
1553 + nuke(ep, -ESHUTDOWN);
1555 + del_timer_sync(&dev->timer);
1557 + /* report disconnect; the driver is already quiesced */
1559 + driver->disconnect(&dev->gadget);
1561 + /* re-init driver-visible data structures */
1565 +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1567 + struct pxa27x_udc *dev = the_controller;
1571 + if (!driver || driver != dev->driver)
1574 + local_irq_disable();
1576 + stop_activity(dev, driver);
1577 + local_irq_enable();
1579 + driver->unbind(&dev->gadget);
1582 + device_del (&dev->gadget.dev);
1583 + device_remove_file(dev->dev, &dev_attr_function);
1585 + DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1589 +EXPORT_SYMBOL(usb_gadget_unregister_driver);
1591 +#ifndef enable_disconnect_irq
1592 +#define enable_disconnect_irq() do {} while (0)
1593 +#define disable_disconnect_irq() do {} while (0)
1597 +/*-------------------------------------------------------------------------*/
1599 +static inline void clear_ep_state (struct pxa27x_udc *dev)
1603 + /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1604 + * fifos, and pending transactions mustn't be continued in any case.
1606 + for (i = 1; i < UDC_EP_NUM; i++)
1607 + nuke(&dev->ep[i], -ECONNABORTED);
1610 +static void udc_watchdog(unsigned long _dev)
1612 + struct pxa27x_udc *dev = (void *)_dev;
1614 + local_irq_disable();
1615 + if (dev->ep0state == EP0_STALL
1616 + && (UDCCSR0 & UDCCSR0_FST) == 0
1617 + && (UDCCSR0 & UDCCSR0_SST) == 0) {
1618 + UDCCSR0 = UDCCSR0_FST|UDCCSR0_FTF;
1619 + DBG(DBG_VERBOSE, "ep0 re-stall\n");
1620 + start_watchdog(dev);
1622 + local_irq_enable();
1625 +static void handle_ep0 (struct pxa27x_udc *dev)
1627 + u32 udccsr0 = UDCCSR0;
1628 + struct pxa27x_ep *ep = &dev->ep [0];
1629 + struct pxa27x_request *req;
1631 + struct usb_ctrlrequest r;
1636 + if (list_empty(&ep->queue))
1639 + req = list_entry(ep->queue.next, struct pxa27x_request, queue);
1641 + /* clear stall status */
1642 + if (udccsr0 & UDCCSR0_SST) {
1644 + UDCCSR0 = UDCCSR0_SST;
1645 + del_timer(&dev->timer);
1649 + /* previous request unfinished? non-error iff back-to-back ... */
1650 + if ((udccsr0 & UDCCSR0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1652 + del_timer(&dev->timer);
1656 + switch (dev->ep0state) {
1657 + case EP0_NO_ACTION:
1658 + printk(KERN_INFO"%s: Busy\n", __FUNCTION__);
1661 + /* late-breaking status? */
1662 + udccsr0 = UDCCSR0;
1664 + /* start control request? */
1665 + if (likely((udccsr0 & (UDCCSR0_OPC|UDCCSR0_SA|UDCCSR0_RNE))
1666 + == (UDCCSR0_OPC|UDCCSR0_SA|UDCCSR0_RNE))) {
1669 + nuke (ep, -EPROTO);
1670 + /* read SETUP packet */
1671 + for (i = 0; i < 2; i++) {
1672 + if (unlikely(!(UDCCSR0 & UDCCSR0_RNE))) {
1674 + DMSG("SETUP %d!\n", i);
1677 + u.word [i] = UDCDR0;
1679 + if (unlikely((UDCCSR0 & UDCCSR0_RNE) != 0))
1682 + le16_to_cpus (&u.r.wValue);
1683 + le16_to_cpus (&u.r.wIndex);
1684 + le16_to_cpus (&u.r.wLength);
1688 + DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1689 + u.r.bRequestType, u.r.bRequest,
1690 + u.r.wValue, u.r.wIndex, u.r.wLength);
1691 + /* cope with automagic for some standard requests. */
1692 + dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1693 + == USB_TYPE_STANDARD;
1694 + dev->req_config = 0;
1695 + dev->req_pending = 1;
1697 + switch (u.r.bRequest) {
1698 + /* hardware was supposed to hide this */
1699 + case USB_REQ_SET_CONFIGURATION:
1700 + case USB_REQ_SET_INTERFACE:
1701 + case USB_REQ_SET_ADDRESS:
1702 + printk(KERN_ERR "Should not come here\n");
1707 + if (u.r.bRequestType & USB_DIR_IN)
1708 + dev->ep0state = EP0_IN_DATA_PHASE;
1710 + dev->ep0state = EP0_OUT_DATA_PHASE;
1711 + i = dev->driver->setup(&dev->gadget, &u.r);
1714 + /* hardware automagic preventing STALL... */
1715 + if (dev->req_config) {
1716 + /* hardware sometimes neglects to tell
1717 + * tell us about config change events,
1718 + * so later ones may fail...
1720 + WARN("config change %02x fail %d?\n",
1723 + /* TODO experiment: if has_cfr,
1724 + * hardware didn't ACK; maybe we
1725 + * could actually STALL!
1728 + DBG(DBG_VERBOSE, "protocol STALL, "
1729 + "%02x err %d\n", UDCCSR0, i);
1731 + /* the watchdog timer helps deal with cases
1732 + * where udc seems to clear FST wrongly, and
1733 + * then NAKs instead of STALLing.
1735 + ep0start(dev, UDCCSR0_FST|UDCCSR0_FTF, "stall");
1736 + start_watchdog(dev);
1737 + dev->ep0state = EP0_STALL;
1740 + /* deferred i/o == no response yet */
1741 + } else if (dev->req_pending) {
1742 + if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1743 + || dev->req_std || u.r.wLength))
1744 + ep0start(dev, 0, "defer");
1746 + ep0start(dev, UDCCSR0_IPR, "defer/IPR");
1749 + /* expect at least one data or status stage irq */
1753 + /* some random early IRQ:
1756 + * - OPC got set, without SA (likely status stage)
1758 + UDCCSR0 = udccsr0 & (UDCCSR0_SA|UDCCSR0_OPC);
1761 + case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1762 + if (udccsr0 & UDCCSR0_OPC) {
1763 + UDCCSR0 = UDCCSR0_OPC|UDCCSR0_FTF;
1764 + DBG(DBG_VERBOSE, "ep0in premature status\n");
1768 + } else /* irq was IPR clearing */ {
1770 + /* this IN packet might finish the request */
1771 + (void) write_ep0_fifo(ep, req);
1772 + } /* else IN token before response was written */
1775 + case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1776 + if (udccsr0 & UDCCSR0_OPC) {
1778 + /* this OUT packet might finish the request */
1779 + if (read_ep0_fifo(ep, req))
1781 + /* else more OUT packets expected */
1782 + } /* else OUT token before read was issued */
1783 + } else /* irq was IPR clearing */ {
1784 + DBG(DBG_VERBOSE, "ep0out premature status\n");
1791 + UDCCSR0 = UDCCSR0_FST;
1794 + UDCISR0 = UDCISR_INT(0, UDCISR_INT_MASK);
1798 +static void handle_ep(struct pxa27x_ep *ep)
1800 + struct pxa27x_request *req;
1804 + DMSG("%s is called\n", __FUNCTION__);
1807 + if (likely (!list_empty(&ep->queue))) {
1808 + req = list_entry(ep->queue.next,
1809 + struct pxa27x_request, queue);
1813 +// udccsr = *ep->reg_udccsr;
1814 + DMSG("%s: req:%p, udcisr0:0x%x udccsr %p:0x%x\n", __FUNCTION__,
1815 + req, UDCISR0, ep->reg_udccsr, *ep->reg_udccsr);
1816 + if (unlikely(ep->dir_in)) {
1817 + udccsr = (UDCCSR_SST | UDCCSR_TRN) & *ep->reg_udccsr;
1818 + if (unlikely (udccsr))
1819 + *ep->reg_udccsr = udccsr;
1821 + if (req && likely ((*ep->reg_udccsr & UDCCSR_FS) != 0))
1822 + completed = write_fifo(ep, req);
1825 + udccsr = (UDCCSR_SST | UDCCSR_TRN) & *ep->reg_udccsr;
1826 + if (unlikely(udccsr))
1827 + *ep->reg_udccsr = udccsr;
1829 + /* fifos can hold packets, ready for reading... */
1830 + if (likely(req)) {
1831 + completed = read_fifo(ep, req);
1833 + pio_irq_disable (ep->ep_num);
1834 + *ep->reg_udccsr = UDCCSR_FEF;
1835 + DMSG("%s: no req for out data\n",
1840 + } while (completed);
1843 +static void pxa27x_change_configuration (struct pxa27x_udc *dev)
1845 + struct usb_ctrlrequest req ;
1847 + req.bRequestType = 0;
1848 + req.bRequest = USB_REQ_SET_CONFIGURATION;
1849 + req.wValue = dev->configuration;
1853 + dev->ep0state = EP0_NO_ACTION;
1854 + dev->driver->setup(&dev->gadget, &req);
1858 +static void pxa27x_change_interface (struct pxa27x_udc *dev)
1860 + struct usb_ctrlrequest req;
1862 + req.bRequestType = USB_RECIP_INTERFACE;
1863 + req.bRequest = USB_REQ_SET_INTERFACE;
1864 + req.wValue = dev->alternate;
1865 + req.wIndex = dev->interface;
1868 + dev->ep0state = EP0_NO_ACTION;
1869 + dev->driver->setup(&dev->gadget, &req);
1873 + * pxa27x_udc_irq - interrupt handler
1875 + * avoid delays in ep0 processing. the control handshaking isn't always
1876 + * under software control (pxa250c0 and the pxa255 are better), and delays
1877 + * could cause usb protocol errors.
1880 +pxa27x_udc_irq(int irq, void *_dev)
1882 + struct pxa27x_udc *dev = _dev;
1885 + dev->stats.irqs++;
1886 + HEX_DISPLAY(dev->stats.irqs);
1889 + DBG(DBG_VERBOSE, "Interrupt, UDCISR0:0x%08x, UDCISR1:0x%08x, "
1890 + "UDCCR:0x%08x\n", UDCISR0, UDCISR1, UDCCR);
1892 + u32 udcir = UDCISR1 & 0xF8000000;
1896 + /* SUSpend Interrupt Request */
1897 + if (unlikely(udcir & UDCISR1_IRSU)) {
1898 + UDCISR1 = UDCISR1_IRSU;
1900 + DBG(DBG_VERBOSE, "USB suspend\n");
1901 + if (dev->gadget.speed != USB_SPEED_UNKNOWN
1903 + && dev->driver->suspend)
1904 + dev->driver->suspend(&dev->gadget);
1908 + /* RESume Interrupt Request */
1909 + if (unlikely(udcir & UDCISR1_IRRU)) {
1910 + UDCISR1 = UDCISR1_IRRU;
1912 + DBG(DBG_VERBOSE, "USB resume\n");
1914 + if (dev->gadget.speed != USB_SPEED_UNKNOWN
1916 + && dev->driver->resume)
1917 + dev->driver->resume(&dev->gadget);
1920 + if (unlikely(udcir & UDCISR1_IRCC)) {
1921 + unsigned config, interface, alternate;
1924 + DBG(DBG_VERBOSE, "USB SET_CONFIGURATION or "
1925 + "SET_INTERFACE command received\n");
1927 + UDCCR |= UDCCR_SMAC;
1929 + config = (UDCCR & UDCCR_ACN) >> UDCCR_ACN_S;
1931 + if (dev->configuration != config) {
1932 + dev->configuration = config;
1933 + pxa27x_change_configuration(dev) ;
1936 + interface = (UDCCR & UDCCR_AIN) >> UDCCR_AIN_S;
1937 + alternate = (UDCCR & UDCCR_AAISN) >> UDCCR_AAISN_S;
1939 + if ( (dev->configuration != interface) || \
1940 + (dev->alternate != alternate)){
1941 + dev->interface = config;
1942 + dev->alternate = alternate;
1943 + pxa27x_change_interface(dev);
1946 + UDCISR1 = UDCISR1_IRCC;
1947 + DMSG("%s: con:%d,inter:%d,alt:%d\n",
1948 + __FUNCTION__, config,interface, alternate);
1951 + /* ReSeT Interrupt Request - USB reset */
1952 + if (unlikely(udcir & UDCISR1_IRRS)) {
1953 + UDCISR1 = UDCISR1_IRRS;
1956 + if ((UDCCR & UDCCR_UDA) == 0) {
1957 + DBG(DBG_VERBOSE, "USB reset start\n");
1959 + /* reset driver and endpoints,
1960 + * in case that's not yet done
1962 + stop_activity (dev, dev->driver);
1965 + INFO("USB reset\n");
1966 + dev->gadget.speed = USB_SPEED_FULL;
1967 + memset(&dev->stats, 0, sizeof dev->stats);
1970 + u32 udcisr0 = UDCISR0 ;
1971 + u32 udcisr1 = UDCISR1 & 0xFFFF;
1974 + if (unlikely (!udcisr0 && !udcisr1))
1977 + DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", udcisr1,udcisr0);
1979 + /* control traffic */
1980 + if (udcisr0 & UDCISR0_IR0) {
1981 + dev->ep[0].pio_irqs++;
1987 + /* endpoint data transfers */
1988 + for (i = 1; udcisr0!=0 && i < 16; udcisr0>>=2,i++) {
1989 + UDCISR0 = UDCISR_INT(i, UDCISR_INT_MASK);
1991 + if (udcisr0 & UDC_INT_FIFOERROR)
1992 + printk(KERN_ERR" Endpoint %d Fifo error\n", i);
1993 + if (udcisr0 & UDC_INT_PACKETCMP) {
1994 + handle_ep(&dev->ep[i]);
2000 + for (i = 0; udcisr1!=0 && i < 8; udcisr1 >>= 2, i++) {
2001 + UDCISR1 = UDCISR_INT(i, UDCISR_INT_MASK);
2003 + if (udcisr1 & UDC_INT_FIFOERROR) {
2004 + printk(KERN_ERR" Endpoint %d fifo error\n", (i+16));
2007 + if (udcisr1 & UDC_INT_PACKETCMP) {
2008 + handle_ep(&dev->ep[i+16]);
2014 + /* we could also ask for 1 msec SOF (SIR) interrupts */
2016 + } while (handled);
2017 + return IRQ_HANDLED;
2020 +static inline void validate_fifo_size(struct pxa27x_ep *pxa_ep, u8 bmAttributes)
2022 + switch (bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2023 + case USB_ENDPOINT_XFER_CONTROL:
2024 + pxa_ep->fifo_size = EP0_FIFO_SIZE;
2026 + case USB_ENDPOINT_XFER_ISOC:
2027 + pxa_ep->fifo_size = ISO_FIFO_SIZE;
2029 + case USB_ENDPOINT_XFER_BULK:
2030 + pxa_ep->fifo_size = BULK_FIFO_SIZE;
2032 + case USB_ENDPOINT_XFER_INT:
2033 + pxa_ep->fifo_size = INT_FIFO_SIZE;
2040 +static void udc_init_ep(struct pxa27x_udc *dev)
2044 + INIT_LIST_HEAD (&dev->gadget.ep_list);
2045 + INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
2047 + for (i = 0; i < UDC_EP_NUM; i++) {
2048 + struct pxa27x_ep *ep = &dev->ep[i];
2052 + memset(ep, 0, sizeof(*ep));
2054 + INIT_LIST_HEAD (&ep->queue);
2057 +#define NAME_SIZE 18
2059 +struct usb_ep* pxa27x_ep_config(
2060 + struct usb_gadget *gadget,
2061 + struct usb_endpoint_descriptor *desc,
2062 + int config, int interface, int alt
2068 + struct usb_ep * ep = NULL;
2069 + struct pxa27x_ep *pxa_ep = NULL;
2070 + struct pxa27x_udc *dev = the_controller;
2072 + DMSG("pxa27x_config_ep is called\n");
2073 + DMSG(" usb endpoint descriptor is:\n"
2075 + " bDescriptorType:%x\n"
2076 + " bEndpointAddress:%x\n"
2077 + " bmAttributes:%x\n"
2078 + " wMaxPacketSize:%d\n",
2080 + desc->bDescriptorType,desc->bEndpointAddress,
2081 + desc->bmAttributes,desc->wMaxPacketSize);
2083 + for (i = 1; i < UDC_EP_NUM; i++) {
2084 + if(!dev->ep[i].assigned) {
2085 + pxa_ep = &dev->ep[i];
2086 + pxa_ep->assigned = 1;
2087 + pxa_ep->ep_num = i;
2091 + if (unlikely(i == UDC_EP_NUM)) {
2092 + printk(KERN_ERR __FILE__ ": Failed to find a spare endpoint\n");
2099 + pxa_ep->dev = dev;
2100 + pxa_ep->desc = desc;
2101 + pxa_ep->pio_irqs = pxa_ep->dma_irqs = 0;
2104 + if (!(desc->bEndpointAddress & 0xF))
2105 + desc->bEndpointAddress |= i;
2107 + if (!(desc->wMaxPacketSize)) {
2108 + validate_fifo_size(pxa_ep, desc->bmAttributes);
2109 + desc->wMaxPacketSize = pxa_ep->fifo_size;
2111 + pxa_ep->fifo_size = desc->wMaxPacketSize;
2113 + pxa_ep->dir_in = (desc->bEndpointAddress & USB_DIR_IN) ? 1 : 0;
2114 + pxa_ep->ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
2115 + pxa_ep->stopped = 1;
2116 + pxa_ep->dma_con = 0;
2117 + pxa_ep->config = config;
2118 + pxa_ep->interface = interface;
2119 + pxa_ep->aisn = alt;
2121 + pxa_ep->reg_udccsr = &UDCCSR0 + i;
2122 + pxa_ep->reg_udcbcr = &UDCBCR0 + i;
2123 + pxa_ep->reg_udcdr = &UDCDR0 + i ;
2124 + pxa_ep->reg_udccr = &UDCCRA - 1 + i;
2126 + pxa_ep->reg_drcmr = &DRCMR24 + i;
2130 + DMSG("udccsr=0x%8x, udcbcr=0x%8x, udcdr=0x%8x,"
2132 + (unsigned)pxa_ep->reg_udccsr,
2133 + (unsigned)pxa_ep->reg_udcbcr,
2134 + (unsigned)pxa_ep->reg_udcdr,
2135 + (unsigned)pxa_ep->reg_udccr);
2137 + /* Configure UDCCR */
2139 + tmp |= (pxa_ep->config << UDCCONR_CN_S) & UDCCONR_CN;
2140 + tmp |= (pxa_ep->interface << UDCCONR_IN_S) & UDCCONR_IN;
2141 + tmp |= (pxa_ep->aisn << UDCCONR_AISN_S) & UDCCONR_AISN;
2142 + tmp |= (desc->bEndpointAddress << UDCCONR_EN_S) & UDCCONR_EN;
2143 + tmp |= (pxa_ep->ep_type << UDCCONR_ET_S) & UDCCONR_ET;
2144 + tmp |= (pxa_ep->dir_in) ? UDCCONR_ED : 0;
2145 + tmp |= (min(pxa_ep->fifo_size, (unsigned)desc->wMaxPacketSize) \
2146 + << UDCCONR_MPS_S ) & UDCCONR_MPS;
2147 + tmp |= UDCCONR_DE | UDCCONR_EE;
2148 +// tmp |= UDCCONR_EE;
2150 + *pxa_ep->reg_udccr = tmp;
2153 + /* Only BULK use DMA */
2154 + if ((pxa_ep->ep_type & USB_ENDPOINT_XFERTYPE_MASK)\
2155 + == USB_ENDPOINT_XFER_BULK)
2156 + *pxa_ep->reg_udccsr = UDCCSR_DME;
2159 + DMSG("UDCCR: 0x%p is 0x%x\n", pxa_ep->reg_udccr,*pxa_ep->reg_udccr);
2162 + name = kmalloc(NAME_SIZE, GFP_KERNEL);
2164 + printk(KERN_ERR "%s: Error\n", __FUNCTION__);
2168 + switch (pxa_ep->ep_type) {
2169 + case USB_ENDPOINT_XFER_BULK:
2170 + sprintf(name, "Bulk-%s-%d", (pxa_ep->dir_in ? "in":"out"), i);
2172 + case USB_ENDPOINT_XFER_INT:
2173 + sprintf(name, "Interrupt-%s-%d", (pxa_ep->dir_in ? \
2177 + sprintf(name, "endpoint-%s-%d", (pxa_ep->dir_in ? \
2183 + ep->ops = &pxa27x_ep_ops;
2184 + ep->maxpacket = min((ushort)pxa_ep->fifo_size, desc->wMaxPacketSize);
2186 + list_add_tail (&ep->ep_list, &gadget->ep_list);
2190 +EXPORT_SYMBOL(pxa27x_ep_config);
2192 +/*-------------------------------------------------------------------------*/
2194 +static void nop_release (struct device *dev)
2196 + DMSG("%s %s\n", __FUNCTION__, dev->bus_id);
2199 +/* this uses load-time allocation and initialization (instead of
2200 + * doing it at run-time) to save code, eliminate fault paths, and
2201 + * be more obviously correct.
2203 +static struct pxa27x_udc memory = {
2205 + .ops = &pxa27x_udc_ops,
2206 + .ep0 = &memory.ep[0].ep,
2207 + .name = driver_name,
2209 + .bus_id = "gadget",
2210 + .release = nop_release,
2214 + /* control endpoint */
2218 + .ops = &pxa27x_ep_ops,
2219 + .maxpacket = EP0_FIFO_SIZE,
2222 + .reg_udccsr = &UDCCSR0,
2223 + .reg_udcdr = &UDCDR0,
2227 +#define CP15R0_VENDOR_MASK 0xffffe000
2229 +#define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/xscale */
2232 + * probe - binds to the platform device
2234 +static int __init pxa27x_udc_probe(struct platform_device *_dev)
2236 + struct pxa27x_udc *dev = &memory;
2240 + /* insist on Intel/ARM/XScale */
2241 + asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2242 + if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2243 + printk(KERN_ERR "%s: not XScale!\n", driver_name);
2246 + /* other non-static parts of init */
2247 + dev->dev = &_dev->dev;
2248 + dev->mach = _dev->dev.platform_data;
2250 + init_timer(&dev->timer);
2251 + dev->timer.function = udc_watchdog;
2252 + dev->timer.data = (unsigned long) dev;
2254 + device_initialize(&dev->gadget.dev);
2255 + dev->gadget.dev.parent = &_dev->dev;
2256 + dev->gadget.dev.dma_mask = _dev->dev.dma_mask;
2258 + the_controller = dev;
2259 + platform_set_drvdata(_dev, dev);
2265 + /* irq setup after old hardware state is cleaned up */
2266 + retval = request_irq(IRQ_USB, pxa27x_udc_irq,
2267 + SA_INTERRUPT, driver_name, dev);
2268 + if (retval != 0) {
2269 + printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2270 + driver_name, IRQ_USB, retval);
2275 + create_proc_files();
2280 +static int __exit pxa27x_udc_remove(struct platform_device *_dev)
2282 + struct pxa27x_udc *dev = (struct pxa27x_udc*)platform_get_drvdata(_dev);
2285 + remove_proc_files();
2286 + usb_gadget_unregister_driver(dev->driver);
2288 + if (dev->got_irq) {
2289 + free_irq(IRQ_USB, dev);
2292 + if (machine_is_lubbock() && dev->got_disc) {
2293 + free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2294 + dev->got_disc = 0;
2296 + platform_set_drvdata(_dev, 0);
2297 + the_controller = 0;
2302 +static void pxa27x_udc_shutdown(struct platform_device *_dev)
2304 + struct pxa27x_udc *dev = (struct pxa27x_udc*)platform_get_drvdata(_dev);
2309 +static int pxa27x_udc_suspend(struct platform_device *_dev, pm_message_t state)
2312 + struct pxa27x_udc *dev = (struct pxa27x_udc*)platform_get_drvdata(_dev);
2314 + DMSG("%s is called\n", __FUNCTION__);
2315 + dev->udccsr0 = UDCCSR0;
2316 + for(i=1; (i<UDC_EP_NUM); i++) {
2317 + if (dev->ep[i].assigned) {
2318 + struct pxa27x_ep *ep = &dev->ep[i];
2320 + ep->udccsr_value = *ep->reg_udccsr;
2321 + ep->udccr_value = *ep->reg_udccr;
2322 + DMSG("EP%d, udccsr:0x%x, udccr:0x%x\n",
2323 + i, *ep->reg_udccsr, *ep->reg_udccr);
2327 + udc_clear_mask_UDCCR(UDCCR_UDE);
2328 + pxa_set_cken(CKEN11_USB, 0);
2329 + // MST_MSCWR2 |= MST_MSCWR2_nUSBC_SC;
2334 +static int pxa27x_udc_resume(struct platform_device *_dev)
2337 + struct pxa27x_udc *dev = (struct pxa27x_udc*)platform_get_drvdata(_dev);
2339 + DMSG("%s is called\n", __FUNCTION__);
2341 + UDCCSR0 = dev->udccsr0 & (UDCCSR0_FST | UDCCSR0_DME);
2342 + for (i=1; i < UDC_EP_NUM; i++) {
2343 + if (dev->ep[i].assigned) {
2344 + struct pxa27x_ep *ep = &dev->ep[i];
2346 + *ep->reg_udccsr = ep->udccsr_value;
2347 + *ep->reg_udccr = ep->udccr_value;
2348 + DMSG("EP%d, udccsr:0x%x, udccr:0x%x\n",
2349 + i, *ep->reg_udccsr, *ep->reg_udccr);
2353 + /* OTGPH bit is set when sleep mode is entered.
2354 + * it indicates that OTG pad is retaining its state.
2355 + * Upon exit from sleep mode and before clearing OTGPH,
2356 + * Software must configure the USB OTG pad, UDC, and UHC
2357 + * to the state they were in before entering sleep mode.*/
2358 + PSSR |= PSSR_OTGPH;
2363 +/*-------------------------------------------------------------------------*/
2365 +static struct platform_driver udc_driver = {
2367 + .name = "pxa2xx-udc",
2369 + .probe = pxa27x_udc_probe,
2370 + .remove = __exit_p(pxa27x_udc_remove),
2373 + // FIXME power management support
2374 + .shutdown = pxa27x_udc_shutdown,
2375 + .suspend = pxa27x_udc_suspend,
2376 + .resume = pxa27x_udc_resume
2380 +static int __init udc_init(void)
2382 + printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2383 + return platform_driver_register(&udc_driver);
2385 +module_init(udc_init);
2387 +static void __exit udc_exit(void)
2389 + platform_driver_unregister(&udc_driver);
2391 +module_exit(udc_exit);
2393 +MODULE_DESCRIPTION(DRIVER_DESC);
2394 +MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2395 +MODULE_LICENSE("GPL");
2398 +++ b/drivers/usb/gadget/pxa27x_udc.h
2401 + * linux/drivers/usb/gadget/pxa27x_udc.h
2402 + * Intel PXA27x on-chip full speed USB device controller
2404 + * Copyright (C) 2003 Robert Schwebel <r.schwebel@pengutronix.de>, Pengutronix
2405 + * Copyright (C) 2003 David Brownell
2406 + * Copyright (C) 2004 Intel Corporation
2408 + * This program is free software; you can redistribute it and/or modify
2409 + * it under the terms of the GNU General Public License as published by
2410 + * the Free Software Foundation; either version 2 of the License, or
2411 + * (at your option) any later version.
2413 + * This program is distributed in the hope that it will be useful,
2414 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2415 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2416 + * GNU General Public License for more details.
2418 + * You should have received a copy of the GNU General Public License
2419 + * along with this program; if not, write to the Free Software
2420 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2423 +#ifndef __LINUX_USB_GADGET_PXA27X_H
2424 +#define __LINUX_USB_GADGET_PXA27X_H
2426 +#include <linux/types.h>
2432 + struct pxa27x_udc *dev;
2434 + const struct usb_endpoint_descriptor *desc;
2435 + struct list_head queue;
2436 + unsigned long pio_irqs;
2437 + unsigned long dma_irqs;
2440 + unsigned fifo_size;
2444 + unsigned stopped : 1;
2445 + unsigned dma_con : 1;
2446 + unsigned dir_in : 1;
2447 + unsigned assigned : 1;
2450 + unsigned interface;
2452 + /* UDCCSR = UDC Control/Status Register for this EP
2453 + * UBCR = UDC Byte Count Remaining (contents of OUT fifo)
2454 + * UDCDR = UDC Endpoint Data Register (the fifo)
2455 + * UDCCR = UDC Endpoint Configuration Registers
2456 + * DRCM = DMA Request Channel Map
2458 + volatile u32 *reg_udccsr;
2459 + volatile u32 *reg_udcbcr;
2460 + volatile u32 *reg_udcdr;
2461 + volatile u32 *reg_udccr;
2463 + volatile u32 *reg_drcmr;
2464 +#define drcmr(n) .reg_drcmr = & DRCMR ## n ,
2470 + unsigned udccsr_value;
2471 + unsigned udccr_value;
2475 +struct pxa27x_request {
2476 + struct usb_request req;
2477 + struct list_head queue;
2482 + EP0_IN_DATA_PHASE,
2483 + EP0_OUT_DATA_PHASE,
2489 +#define EP0_FIFO_SIZE ((unsigned)16)
2490 +#define BULK_FIFO_SIZE ((unsigned)64)
2491 +#define ISO_FIFO_SIZE ((unsigned)256)
2492 +#define INT_FIFO_SIZE ((unsigned)8)
2496 + unsigned long ops;
2497 + unsigned long bytes;
2499 + unsigned long irqs;
2502 +#ifdef CONFIG_USB_PXA27X_SMALL
2503 +/* when memory's tight, SMALL config saves code+data. */
2505 +//#define UDC_EP_NUM 3
2509 +#define UDC_EP_NUM 24
2512 +struct pxa27x_udc {
2513 + struct usb_gadget gadget;
2514 + struct usb_gadget_driver *driver;
2516 + enum ep0_state ep0state;
2517 + struct udc_stats stats;
2518 + unsigned got_irq : 1,
2525 +#define start_watchdog(dev) mod_timer(&dev->timer, jiffies + (HZ/200))
2526 + struct timer_list timer;
2528 + struct device *dev;
2529 + struct pxa2xx_udc_mach_info *mach;
2531 + struct pxa27x_ep ep [UDC_EP_NUM];
2533 + unsigned configuration,
2541 +/*-------------------------------------------------------------------------*/
2544 +#define HEX_DISPLAY(n) do { \
2545 + if (machine_is_mainstone())\
2546 + { MST_LEDDAT1 = (n); } \
2549 +#define HEX_DISPLAY1(n) HEX_DISPLAY(n)
2551 +#define HEX_DISPLAY2(n) do { \
2552 + if (machine_is_mainstone()) \
2553 + { MST_LEDDAT2 = (n); } \
2558 +/*-------------------------------------------------------------------------*/
2560 +/* LEDs are only for debug */
2561 +#ifndef HEX_DISPLAY
2562 +#define HEX_DISPLAY(n) do {} while(0)
2565 +#ifndef LED_CONNECTED_ON
2566 +#define LED_CONNECTED_ON do {} while(0)
2567 +#define LED_CONNECTED_OFF do {} while(0)
2570 +#define LED_EP0_ON do {} while (0)
2571 +#define LED_EP0_OFF do {} while (0)
2574 +static struct pxa27x_udc *the_controller;
2577 +/*-------------------------------------------------------------------------*/
2580 +/* one GPIO should be used to detect host disconnect */
2581 +static inline int is_usb_connected(void)
2583 + if (!the_controller->mach->udc_is_connected)
2585 + return the_controller->mach->udc_is_connected();
2588 +/* one GPIO should force the host to see this device (or not) */
2589 +static inline void make_usb_disappear(void)
2591 + if (!the_controller->mach->udc_command)
2593 + the_controller->mach->udc_command(PXA27X_UDC_CMD_DISCONNECT);
2596 +static inline void let_usb_appear(void)
2598 + if (!the_controller->mach->udc_command)
2600 + the_controller->mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
2604 +/*-------------------------------------------------------------------------*/
2607 + * Debugging support vanishes in non-debug builds. DBG_NORMAL should be
2608 + * mostly silent during normal use/testing, with no timing side-effects.
2610 +#define DBG_NORMAL 1 /* error paths, device state transitions */
2611 +#define DBG_VERBOSE 2 /* add some success path trace info */
2612 +#define DBG_NOISY 3 /* ... even more: request level */
2613 +#define DBG_VERY_NOISY 4 /* ... even more: packet level */
2617 +static const char *state_name[] = {
2619 + "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
2620 + "EP0_END_XFER", "EP0_STALL"
2623 +#define DMSG(stuff...) printk(KERN_ERR "udc: " stuff)
2626 +# define UDC_DEBUG DBG_VERBOSE
2628 +# define UDC_DEBUG DBG_NORMAL
2631 +static void __attribute__ ((__unused__))
2632 +dump_udccr(const char *label)
2634 + u32 udccr = UDCCR;
2635 + DMSG("%s 0x%08x =%s%s%s%s%s%s%s%s%s%s, con=%d,inter=%d,altinter=%d\n",
2637 + (udccr & UDCCR_OEN) ? " oen":"",
2638 + (udccr & UDCCR_AALTHNP) ? " aalthnp":"",
2639 + (udccr & UDCCR_AHNP) ? " rem" : "",
2640 + (udccr & UDCCR_BHNP) ? " rstir" : "",
2641 + (udccr & UDCCR_DWRE) ? " dwre" : "",
2642 + (udccr & UDCCR_SMAC) ? " smac" : "",
2643 + (udccr & UDCCR_EMCE) ? " emce" : "",
2644 + (udccr & UDCCR_UDR) ? " udr" : "",
2645 + (udccr & UDCCR_UDA) ? " uda" : "",
2646 + (udccr & UDCCR_UDE) ? " ude" : "",
2647 + (udccr & UDCCR_ACN) >> UDCCR_ACN_S,
2648 + (udccr & UDCCR_AIN) >> UDCCR_AIN_S,
2649 + (udccr & UDCCR_AAISN)>> UDCCR_AAISN_S );
2652 +static void __attribute__ ((__unused__))
2653 +dump_udccsr0(const char *label)
2655 + u32 udccsr0 = UDCCSR0;
2657 + DMSG("%s %s 0x%08x =%s%s%s%s%s%s%s\n",
2658 + label, state_name[the_controller->ep0state], udccsr0,
2659 + (udccsr0 & UDCCSR0_SA) ? " sa" : "",
2660 + (udccsr0 & UDCCSR0_RNE) ? " rne" : "",
2661 + (udccsr0 & UDCCSR0_FST) ? " fst" : "",
2662 + (udccsr0 & UDCCSR0_SST) ? " sst" : "",
2663 + (udccsr0 & UDCCSR0_DME) ? " dme" : "",
2664 + (udccsr0 & UDCCSR0_IPR) ? " ipr" : "",
2665 + (udccsr0 & UDCCSR0_OPC) ? " opr" : "");
2668 +static void __attribute__ ((__unused__))
2669 +dump_state(struct pxa27x_udc *dev)
2673 + DMSG("%s, udcicr %02X.%02X, udcsir %02X.%02x, udcfnr %02X\n",
2674 + state_name[dev->ep0state],
2675 + UDCICR1, UDCICR0, UDCISR1, UDCISR0, UDCFNR);
2676 + dump_udccr("udccr");
2678 + if (!dev->driver) {
2679 + DMSG("no gadget driver bound\n");
2682 + DMSG("ep0 driver '%s'\n", dev->driver->driver.name);
2685 + dump_udccsr0 ("udccsr0");
2686 + DMSG("ep0 IN %lu/%lu, OUT %lu/%lu\n",
2687 + dev->stats.write.bytes, dev->stats.write.ops,
2688 + dev->stats.read.bytes, dev->stats.read.ops);
2690 + for (i = 1; i < UDC_EP_NUM; i++) {
2691 + if (dev->ep [i].desc == 0)
2693 + DMSG ("udccs%d = %02x\n", i, *dev->ep->reg_udccsr);
2698 +static void dump_regs(u8 ep)
2700 + DMSG("EP:%d UDCCSR:0x%08x UDCBCR:0x%08x\n UDCCR:0x%08x\n",
2701 + ep,UDCCSN(ep), UDCBCN(ep), UDCCN(ep));
2703 +static void dump_req (struct pxa27x_request *req)
2705 + struct usb_request *r = &req->req;
2707 + DMSG("%s: buf:0x%08x length:%d dma:0x%08x actual:%d\n",
2708 + __FUNCTION__, (unsigned)r->buf, r->length,
2709 + r->dma, r->actual);
2715 +#define DMSG(stuff...) do{}while(0)
2717 +#define dump_udccr(x) do{}while(0)
2718 +#define dump_udccsr0(x) do{}while(0)
2719 +#define dump_state(x) do{}while(0)
2721 +#define UDC_DEBUG ((unsigned)0)
2725 +#define DBG(lvl, stuff...) do{if ((lvl) <= UDC_DEBUG) DMSG(stuff);}while(0)
2727 +#define WARN(stuff...) printk(KERN_WARNING "udc: " stuff)
2728 +#define INFO(stuff...) printk(KERN_INFO "udc: " stuff)
2731 +#endif /* __LINUX_USB_GADGET_PXA27X_H */