2 * linux/drivers/usb/gadget/pxa2xx_udc.c
3 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
5 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
6 * Copyright (C) 2003 Robert Schwebel, Pengutronix
7 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
8 * Copyright (C) 2003 David Brownell
9 * Copyright (C) 2003 Joshua Wise
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 // #define VERBOSE DBG_VERBOSE
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/ioport.h>
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/delay.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/init.h>
40 #include <linux/timer.h>
41 #include <linux/list.h>
42 #include <linux/interrupt.h>
43 #include <linux/proc_fs.h>
45 #include <linux/platform_device.h>
46 #include <linux/dma-mapping.h>
48 #include <asm/byteorder.h>
52 #include <asm/system.h>
53 #include <asm/mach-types.h>
54 #include <asm/unaligned.h>
55 #include <asm/hardware.h>
56 #ifdef CONFIG_ARCH_PXA
57 #include <asm/arch/pxa-regs.h>
60 #include <linux/usb_ch9.h>
61 #include <linux/usb_gadget.h>
63 #include <asm/arch/hardware/intel_udc.h>
67 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
68 * series processors. The UDC for the IXP 4xx series is very similar.
69 * There are fifteen endpoints, in addition to ep0.
71 * Such controller drivers work with a gadget driver. The gadget driver
72 * returns descriptors, implements configuration and data protocols used
73 * by the host to interact with this device, and allocates endpoints to
74 * the different protocol interfaces. The controller driver virtualizes
75 * usb hardware so that the gadget drivers will be more portable.
77 * This UDC hardware wants to implement a bit too much USB protocol, so
78 * it constrains the sorts of USB configuration change events that work.
79 * The errata for these chips are misleading; some "fixed" bugs from
80 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
83 #define DRIVER_VERSION "4-May-2005"
84 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
87 static const char driver_name [] = "pxa2xx_udc";
89 static const char ep0name [] = "ep0";
93 // #define USE_OUT_DMA
94 // #define DISABLE_TEST_MODE
96 #ifdef CONFIG_ARCH_IXP4XX
99 /* cpu-specific register addresses are compiled in to this code */
100 #ifdef CONFIG_ARCH_PXA
101 #error "Can't configure both IXP and PXA"
106 #include "pxa2xx_udc.h"
110 static int use_dma = 1;
111 module_param(use_dma, bool, 0);
112 MODULE_PARM_DESC (use_dma, "true to use dma");
114 static void dma_nodesc_handler (int dmach, void *_ep, struct pt_regs *r);
115 static void kick_dma(struct pxa2xx_ep *ep, struct pxa2xx_request *req);
118 #define DMASTR " (dma support)"
120 #define DMASTR " (dma in)"
124 #define DMASTR " (pio only)"
128 #ifdef CONFIG_USB_PXA2XX_SMALL
129 #define SIZE_STR " (small)"
134 #ifdef DISABLE_TEST_MODE
135 /* (mode == 0) == no undocumented chip tweaks
136 * (mode & 1) == double buffer bulk IN
137 * (mode & 2) == double buffer bulk OUT
138 * ... so mode = 3 (or 7, 15, etc) does it for both
140 static ushort fifo_mode = 0;
141 module_param(fifo_mode, ushort, 0);
142 MODULE_PARM_DESC (fifo_mode, "pxa2xx udc fifo mode");
145 /* ---------------------------------------------------------------------------
146 * endpoint related parts of the api to the usb controller hardware,
147 * used by gadget driver; and the inner talker-to-hardware core.
148 * ---------------------------------------------------------------------------
151 static void pxa2xx_ep_fifo_flush (struct usb_ep *ep);
152 static void nuke (struct pxa2xx_ep *, int status);
154 static void pio_irq_enable(int bEndpointAddress)
156 bEndpointAddress &= 0xf;
157 if (bEndpointAddress < 8)
158 UICR0 &= ~(1 << bEndpointAddress);
160 bEndpointAddress -= 8;
161 UICR1 &= ~(1 << bEndpointAddress);
165 static void pio_irq_disable(int bEndpointAddress)
167 bEndpointAddress &= 0xf;
168 if (bEndpointAddress < 8)
169 UICR0 |= 1 << bEndpointAddress;
171 bEndpointAddress -= 8;
172 UICR1 |= 1 << bEndpointAddress;
176 /* The UDCCR reg contains mask and interrupt status bits,
177 * so using '|=' isn't safe as it may ack an interrupt.
179 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
181 static inline void udc_set_mask_UDCCR(int mask)
183 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
186 static inline void udc_clear_mask_UDCCR(int mask)
188 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
191 static inline void udc_ack_int_UDCCR(int mask)
193 /* udccr contains the bits we dont want to change */
194 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
196 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
200 * endpoint enable/disable
202 * we need to verify the descriptors used to enable endpoints. since pxa2xx
203 * endpoint configurations are fixed, and are pretty much always enabled,
204 * there's not a lot to manage here.
206 * because pxa2xx can't selectively initialize bulk (or interrupt) endpoints,
207 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
208 * for a single interface (with only the default altsetting) and for gadget
209 * drivers that don't halt endpoints (not reset by set_interface). that also
210 * means that if you use ISO, you must violate the USB spec rule that all
211 * iso endpoints must be in non-default altsettings.
213 static int pxa2xx_ep_enable (struct usb_ep *_ep,
214 const struct usb_endpoint_descriptor *desc)
216 struct pxa2xx_ep *ep;
217 struct pxa2xx_udc *dev;
219 ep = container_of (_ep, struct pxa2xx_ep, ep);
220 if (!_ep || !desc || ep->desc || _ep->name == ep0name
221 || desc->bDescriptorType != USB_DT_ENDPOINT
222 || ep->bEndpointAddress != desc->bEndpointAddress
223 || ep->fifo_size < le16_to_cpu
224 (desc->wMaxPacketSize)) {
225 DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
229 /* xfer types must match, except that interrupt ~= bulk */
230 if (ep->bmAttributes != desc->bmAttributes
231 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
232 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
233 DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
237 /* hardware _could_ do smaller, but driver doesn't */
238 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
239 && le16_to_cpu (desc->wMaxPacketSize)
241 || !desc->wMaxPacketSize) {
242 DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
247 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
248 DMSG("%s, bogus device state\n", __FUNCTION__);
255 ep->pio_irqs = ep->dma_irqs = 0;
256 ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
258 /* flush fifo (mostly for OUT buffers) */
259 pxa2xx_ep_fifo_flush (_ep);
261 /* ... reset halt state too, if we could ... */
264 /* for (some) bulk and ISO endpoints, try to get a DMA channel and
265 * bind it to the endpoint. otherwise use PIO.
267 switch (ep->bmAttributes) {
268 case USB_ENDPOINT_XFER_ISOC:
269 if (le16_to_cpu(desc->wMaxPacketSize) % 32)
272 case USB_ENDPOINT_XFER_BULK:
273 if (!use_dma || !ep->reg_drcmr)
275 ep->dma = pxa_request_dma ((char *)_ep->name,
276 (le16_to_cpu (desc->wMaxPacketSize) > 64)
277 ? DMA_PRIO_MEDIUM /* some iso */
279 dma_nodesc_handler, ep);
281 *ep->reg_drcmr = DRCMR_MAPVLD | ep->dma;
282 DMSG("%s using dma%d\n", _ep->name, ep->dma);
287 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
291 static int pxa2xx_ep_disable (struct usb_ep *_ep)
293 struct pxa2xx_ep *ep;
296 ep = container_of (_ep, struct pxa2xx_ep, ep);
297 if (!_ep || !ep->desc) {
298 DMSG("%s, %s not enabled\n", __FUNCTION__,
299 _ep ? ep->ep.name : NULL);
302 local_irq_save(flags);
304 nuke (ep, -ESHUTDOWN);
309 pxa_free_dma (ep->dma);
314 /* flush fifo (mostly for IN buffers) */
315 pxa2xx_ep_fifo_flush (_ep);
320 local_irq_restore(flags);
321 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
325 /*-------------------------------------------------------------------------*/
327 /* for the pxa2xx, these can just wrap kmalloc/kfree. gadget drivers
328 * must still pass correctly initialized endpoints, since other controller
329 * drivers may care about how it's currently set up (dma issues etc).
333 * pxa2xx_ep_alloc_request - allocate a request data structure
335 static struct usb_request *
336 pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
338 struct pxa2xx_request *req;
340 req = kzalloc(sizeof(*req), gfp_flags);
344 INIT_LIST_HEAD (&req->queue);
350 * pxa2xx_ep_free_request - deallocate a request data structure
353 pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
355 struct pxa2xx_request *req;
357 req = container_of (_req, struct pxa2xx_request, req);
358 WARN_ON (!list_empty (&req->queue));
363 /* PXA cache needs flushing with DMA I/O (it's dma-incoherent), but there's
364 * no device-affinity and the heap works perfectly well for i/o buffers.
365 * It wastes much less memory than dma_alloc_coherent() would, and even
366 * prevents cacheline (32 bytes wide) sharing problems.
369 pxa2xx_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
370 dma_addr_t *dma, gfp_t gfp_flags)
374 retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
377 *dma = virt_to_bus (retval);
379 *dma = (dma_addr_t)~0;
385 pxa2xx_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma,
391 /*-------------------------------------------------------------------------*/
394 * done - retire a request; caller blocked irqs
396 static void done(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int status)
398 unsigned stopped = ep->stopped;
400 list_del_init(&req->queue);
402 if (likely (req->req.status == -EINPROGRESS))
403 req->req.status = status;
405 status = req->req.status;
407 if (status && status != -ESHUTDOWN)
408 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
409 ep->ep.name, &req->req, status,
410 req->req.actual, req->req.length);
412 /* don't modify queue heads during completion callback */
414 req->req.complete(&ep->ep, &req->req);
415 ep->stopped = stopped;
419 static inline void ep0_idle (struct pxa2xx_udc *dev)
421 dev->ep0state = EP0_IDLE;
425 write_packet(volatile u32 *uddr, struct pxa2xx_request *req, unsigned max)
428 unsigned length, count;
430 buf = req->req.buf + req->req.actual;
433 /* how big will this packet be? */
434 length = min(req->req.length - req->req.actual, max);
435 req->req.actual += length;
438 while (likely(count--))
445 * write to an IN endpoint fifo, as many packets as possible.
446 * irqs will use this to write the rest later.
447 * caller guarantees at least one packet buffer is ready (or a zlp).
450 write_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
454 max = le16_to_cpu(ep->desc->wMaxPacketSize);
457 int is_last, is_short;
459 count = write_packet(ep->reg_uddr, req, max);
461 /* last packet is usually short (or a zlp) */
462 if (unlikely (count != max))
463 is_last = is_short = 1;
465 if (likely(req->req.length != req->req.actual)
470 /* interrupt/iso maxpacket may not fill the fifo */
471 is_short = unlikely (max < ep->fifo_size);
474 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
476 is_last ? "/L" : "", is_short ? "/S" : "",
477 req->req.length - req->req.actual, req);
479 /* let loose that packet. maybe try writing another one,
480 * double buffering might work. TSP, TPC, and TFS
481 * bit values are the same for all normal IN endpoints.
483 *ep->reg_udccs = UDCCS_BI_TPC;
485 *ep->reg_udccs = UDCCS_BI_TSP;
487 /* requests complete when all IN data is in the FIFO */
490 if (list_empty(&ep->queue) || unlikely(ep->dma >= 0)) {
491 pio_irq_disable (ep->bEndpointAddress);
493 /* unaligned data and zlps couldn't use dma */
494 if (unlikely(!list_empty(&ep->queue))) {
495 req = list_entry(ep->queue.next,
496 struct pxa2xx_request, queue);
505 // TODO experiment: how robust can fifo mode tweaking be?
506 // double buffering is off in the default fifo mode, which
507 // prevents TFS from being set here.
509 } while (*ep->reg_udccs & UDCCS_BI_TFS);
513 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
514 * ep0 data stage. these chips want very simple state transitions.
517 void ep0start(struct pxa2xx_udc *dev, u32 flags, const char *tag)
519 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
521 dev->req_pending = 0;
522 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
523 __FUNCTION__, tag, UDCCS0, flags);
527 write_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
532 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
533 ep->dev->stats.write.bytes += count;
535 /* last packet "must be" short (or a zlp) */
536 is_short = (count != EP0_FIFO_SIZE);
538 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
539 req->req.length - req->req.actual, req);
541 if (unlikely (is_short)) {
542 if (ep->dev->req_pending)
543 ep0start(ep->dev, UDCCS0_IPR, "short IN");
547 count = req->req.length;
550 #ifndef CONFIG_ARCH_IXP4XX
552 /* This seems to get rid of lost status irqs in some cases:
553 * host responds quickly, or next request involves config
554 * change automagic, or should have been hidden, or ...
556 * FIXME get rid of all udelays possible...
558 if (count >= EP0_FIFO_SIZE) {
561 if ((UDCCS0 & UDCCS0_OPR) != 0) {
562 /* clear OPR, generate ack */
572 } else if (ep->dev->req_pending)
573 ep0start(ep->dev, 0, "IN");
579 * read_fifo - unload packet(s) from the fifo we use for usb OUT
580 * transfers and put them into the request. caller should have made
581 * sure there's at least one packet ready.
583 * returns true if the request completed because of short packet or the
584 * request buffer having filled (and maybe overran till end-of-packet).
587 read_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
592 unsigned bufferspace, count, is_short;
594 /* make sure there's a packet in the FIFO.
595 * UDCCS_{BO,IO}_RPC are all the same bit value.
596 * UDCCS_{BO,IO}_RNE are all the same bit value.
598 udccs = *ep->reg_udccs;
599 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
601 buf = req->req.buf + req->req.actual;
603 bufferspace = req->req.length - req->req.actual;
605 /* read all bytes from this packet */
606 if (likely (udccs & UDCCS_BO_RNE)) {
607 count = 1 + (0x0ff & *ep->reg_ubcr);
608 req->req.actual += min (count, bufferspace);
611 is_short = (count < ep->ep.maxpacket);
612 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
613 ep->ep.name, udccs, count,
614 is_short ? "/S" : "",
615 req, req->req.actual, req->req.length);
616 while (likely (count-- != 0)) {
617 u8 byte = (u8) *ep->reg_uddr;
619 if (unlikely (bufferspace == 0)) {
620 /* this happens when the driver's buffer
621 * is smaller than what the host sent.
622 * discard the extra data.
624 if (req->req.status != -EOVERFLOW)
625 DMSG("%s overflow %d\n",
627 req->req.status = -EOVERFLOW;
633 *ep->reg_udccs = UDCCS_BO_RPC;
634 /* RPC/RSP/RNE could now reflect the other packet buffer */
636 /* iso is one request per packet */
637 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
638 if (udccs & UDCCS_IO_ROF)
639 req->req.status = -EHOSTUNREACH;
640 /* more like "is_done" */
645 if (is_short || req->req.actual == req->req.length) {
647 if (list_empty(&ep->queue))
648 pio_irq_disable (ep->bEndpointAddress);
652 /* finished that packet. the next one may be waiting... */
658 * special ep0 version of the above. no UBCR0 or double buffering; status
659 * handshaking is magic. most device protocols don't need control-OUT.
660 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
661 * protocols do use them.
664 read_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
667 unsigned bufferspace;
669 buf = req->req.buf + req->req.actual;
670 bufferspace = req->req.length - req->req.actual;
672 while (UDCCS0 & UDCCS0_RNE) {
675 if (unlikely (bufferspace == 0)) {
676 /* this happens when the driver's buffer
677 * is smaller than what the host sent.
678 * discard the extra data.
680 if (req->req.status != -EOVERFLOW)
681 DMSG("%s overflow\n", ep->ep.name);
682 req->req.status = -EOVERFLOW;
690 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
693 if (req->req.actual >= req->req.length)
696 /* finished that packet. the next one may be waiting... */
702 #define MAX_IN_DMA ((DCMD_LENGTH + 1) - BULK_FIFO_SIZE)
705 start_dma_nodesc(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int is_in)
707 u32 dcmd = req->req.length;
708 u32 buf = req->req.dma;
709 u32 fifo = io_v2p ((u32)ep->reg_uddr);
711 /* caller guarantees there's a packet or more remaining
712 * - IN may end with a short packet (TSP set separately),
713 * - OUT is always full length
715 buf += req->req.actual;
716 dcmd -= req->req.actual;
719 /* no-descriptor mode can be simple for bulk-in, iso-in, iso-out */
720 DCSR(ep->dma) = DCSR_NODESC;
722 DSADR(ep->dma) = buf;
723 DTADR(ep->dma) = fifo;
724 if (dcmd > MAX_IN_DMA)
727 ep->dma_fixup = (dcmd % ep->ep.maxpacket) != 0;
728 dcmd |= DCMD_BURST32 | DCMD_WIDTH1
729 | DCMD_FLOWTRG | DCMD_INCSRCADDR;
732 DSADR(ep->dma) = fifo;
733 DTADR(ep->dma) = buf;
734 if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
735 dcmd = ep->ep.maxpacket;
736 dcmd |= DCMD_BURST32 | DCMD_WIDTH1
737 | DCMD_FLOWSRC | DCMD_INCTRGADDR;
740 DCMD(ep->dma) = dcmd;
741 DCSR(ep->dma) = DCSR_RUN | DCSR_NODESC
743 ? DCSR_STOPIRQEN /* use dma_nodesc_handler() */
744 : 0); /* use handle_ep() */
747 static void kick_dma(struct pxa2xx_ep *ep, struct pxa2xx_request *req)
749 int is_in = ep->bEndpointAddress & USB_DIR_IN;
752 /* unaligned tx buffers and zlps only work with PIO */
753 if ((req->req.dma & 0x0f) != 0
754 || unlikely((req->req.length - req->req.actual)
756 pio_irq_enable(ep->bEndpointAddress);
757 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0)
758 (void) write_fifo(ep, req);
760 start_dma_nodesc(ep, req, USB_DIR_IN);
763 if ((req->req.length - req->req.actual) < ep->ep.maxpacket) {
764 DMSG("%s short dma read...\n", ep->ep.name);
765 /* we're always set up for pio out */
768 *ep->reg_udccs = UDCCS_BO_DME
769 | (*ep->reg_udccs & UDCCS_BO_FST);
770 start_dma_nodesc(ep, req, USB_DIR_OUT);
775 static void cancel_dma(struct pxa2xx_ep *ep)
777 struct pxa2xx_request *req;
780 if (DCSR(ep->dma) == 0 || list_empty(&ep->queue))
784 while ((DCSR(ep->dma) & DCSR_STOPSTATE) == 0)
787 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
788 tmp = DCMD(ep->dma) & DCMD_LENGTH;
789 req->req.actual = req->req.length - (tmp & DCMD_LENGTH);
791 /* the last tx packet may be incomplete, so flush the fifo.
792 * FIXME correct req.actual if we can
794 if (ep->bEndpointAddress & USB_DIR_IN)
795 *ep->reg_udccs = UDCCS_BI_FTF;
798 /* dma channel stopped ... normal tx end (IN), or on error (IN/OUT) */
799 static void dma_nodesc_handler(int dmach, void *_ep, struct pt_regs *r)
801 struct pxa2xx_ep *ep = _ep;
802 struct pxa2xx_request *req;
807 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
810 ep->dev->stats.irqs++;
811 HEX_DISPLAY(ep->dev->stats.irqs);
816 if ((tmp & DCSR_STOPSTATE) == 0
817 || (DDADR(ep->dma) & DDADR_STOP) != 0) {
818 DBG(DBG_VERBOSE, "%s, dcsr %08x ddadr %08x\n",
819 ep->ep.name, DCSR(ep->dma), DDADR(ep->dma));
822 DCSR(ep->dma) = 0; /* clear DCSR_STOPSTATE */
824 /* update transfer status */
825 completed = tmp & DCSR_BUSERR;
826 if (ep->bEndpointAddress & USB_DIR_IN)
827 tmp = DSADR(ep->dma);
829 tmp = DTADR(ep->dma);
830 req->req.actual = tmp - req->req.dma;
832 /* FIXME seems we sometimes see partial transfers... */
834 if (unlikely(completed != 0))
835 req->req.status = -EIO;
836 else if (req->req.actual) {
837 /* these registers have zeroes in low bits; they miscount
838 * some (end-of-transfer) short packets: tx 14 as tx 12
841 req->req.actual = min(req->req.actual + 3,
844 tmp = (req->req.length - req->req.actual);
845 completed = (tmp == 0);
846 if (completed && (ep->bEndpointAddress & USB_DIR_IN)) {
848 /* maybe validate final short packet ... */
849 if ((req->req.actual % ep->ep.maxpacket) != 0)
850 *ep->reg_udccs = UDCCS_BI_TSP/*|UDCCS_BI_TPC*/;
852 /* ... or zlp, using pio fallback */
853 else if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK
855 DMSG("%s zlp terminate ...\n", ep->ep.name);
861 if (likely(completed)) {
864 /* maybe re-activate after completion */
865 if (ep->stopped || list_empty(&ep->queue))
867 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
876 /*-------------------------------------------------------------------------*/
879 pxa2xx_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
881 struct pxa2xx_request *req;
882 struct pxa2xx_ep *ep;
883 struct pxa2xx_udc *dev;
886 req = container_of(_req, struct pxa2xx_request, req);
887 if (unlikely (!_req || !_req->complete || !_req->buf
888 || !list_empty(&req->queue))) {
889 DMSG("%s, bad params\n", __FUNCTION__);
893 ep = container_of(_ep, struct pxa2xx_ep, ep);
894 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
895 DMSG("%s, bad ep\n", __FUNCTION__);
900 if (unlikely (!dev->driver
901 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
902 DMSG("%s, bogus device state\n", __FUNCTION__);
906 /* iso is always one packet per request, that's the only way
907 * we can report per-packet status. that also helps with dma.
909 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
910 && req->req.length > le16_to_cpu
911 (ep->desc->wMaxPacketSize)))
915 // FIXME caller may already have done the dma mapping
917 _req->dma = dma_map_single(dev->dev,
918 _req->buf, _req->length,
919 ((ep->bEndpointAddress & USB_DIR_IN) != 0)
925 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
926 _ep->name, _req, _req->length, _req->buf);
928 local_irq_save(flags);
930 _req->status = -EINPROGRESS;
933 /* kickstart this i/o queue? */
934 if (list_empty(&ep->queue) && !ep->stopped) {
935 if (ep->desc == 0 /* ep0 */) {
936 unsigned length = _req->length;
938 switch (dev->ep0state) {
939 case EP0_IN_DATA_PHASE:
940 dev->stats.write.ops++;
941 if (write_ep0_fifo(ep, req))
945 case EP0_OUT_DATA_PHASE:
946 dev->stats.read.ops++;
948 if (dev->req_config) {
949 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
950 dev->has_cfr ? "" : " raced");
952 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
955 dev->ep0state = EP0_END_XFER;
956 local_irq_restore (flags);
959 if (dev->req_pending)
960 ep0start(dev, UDCCS0_IPR, "OUT");
961 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
962 && read_ep0_fifo(ep, req))) {
970 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
971 local_irq_restore (flags);
975 /* either start dma or prime pio pump */
976 } else if (ep->dma >= 0) {
979 /* can the FIFO can satisfy the request immediately? */
980 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
981 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
982 && write_fifo(ep, req))
984 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
985 && read_fifo(ep, req)) {
989 if (likely (req && ep->desc) && ep->dma < 0)
990 pio_irq_enable(ep->bEndpointAddress);
993 /* pio or dma irq handler advances the queue. */
994 if (likely (req != 0))
995 list_add_tail(&req->queue, &ep->queue);
996 local_irq_restore(flags);
1003 * nuke - dequeue ALL requests
1005 static void nuke(struct pxa2xx_ep *ep, int status)
1007 struct pxa2xx_request *req;
1009 /* called with irqs blocked */
1011 if (ep->dma >= 0 && !ep->stopped)
1014 while (!list_empty(&ep->queue)) {
1015 req = list_entry(ep->queue.next,
1016 struct pxa2xx_request,
1018 done(ep, req, status);
1021 pio_irq_disable (ep->bEndpointAddress);
1025 /* dequeue JUST ONE request */
1026 static int pxa2xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1028 struct pxa2xx_ep *ep;
1029 struct pxa2xx_request *req;
1030 unsigned long flags;
1032 ep = container_of(_ep, struct pxa2xx_ep, ep);
1033 if (!_ep || ep->ep.name == ep0name)
1036 local_irq_save(flags);
1038 /* make sure it's actually queued on this endpoint */
1039 list_for_each_entry (req, &ep->queue, queue) {
1040 if (&req->req == _req)
1043 if (&req->req != _req) {
1044 local_irq_restore(flags);
1049 if (ep->dma >= 0 && ep->queue.next == &req->queue && !ep->stopped) {
1051 done(ep, req, -ECONNRESET);
1053 if (!list_empty(&ep->queue)) {
1054 req = list_entry(ep->queue.next,
1055 struct pxa2xx_request, queue);
1060 done(ep, req, -ECONNRESET);
1062 local_irq_restore(flags);
1066 /*-------------------------------------------------------------------------*/
1068 static int pxa2xx_ep_set_halt(struct usb_ep *_ep, int value)
1070 struct pxa2xx_ep *ep;
1071 unsigned long flags;
1073 ep = container_of(_ep, struct pxa2xx_ep, ep);
1075 || (!ep->desc && ep->ep.name != ep0name))
1076 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
1077 DMSG("%s, bad ep\n", __FUNCTION__);
1081 /* this path (reset toggle+halt) is needed to implement
1082 * SET_INTERFACE on normal hardware. but it can't be
1083 * done from software on the PXA UDC, and the hardware
1084 * forgets to do it as part of SET_INTERFACE automagic.
1086 DMSG("only host can clear %s halt\n", _ep->name);
1090 local_irq_save(flags);
1092 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1093 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
1094 || !list_empty(&ep->queue))) {
1095 local_irq_restore(flags);
1099 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
1100 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
1102 /* ep0 needs special care */
1104 start_watchdog(ep->dev);
1105 ep->dev->req_pending = 0;
1106 ep->dev->ep0state = EP0_STALL;
1108 /* and bulk/intr endpoints like dropping stalls too */
1111 for (i = 0; i < 1000; i += 20) {
1112 if (*ep->reg_udccs & UDCCS_BI_SST)
1117 local_irq_restore(flags);
1119 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
1123 static int pxa2xx_ep_fifo_status(struct usb_ep *_ep)
1125 struct pxa2xx_ep *ep;
1127 ep = container_of(_ep, struct pxa2xx_ep, ep);
1129 DMSG("%s, bad ep\n", __FUNCTION__);
1132 /* pxa can't report unclaimed bytes from IN fifos */
1133 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
1135 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
1136 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
1139 return (*ep->reg_ubcr & 0xfff) + 1;
1142 static void pxa2xx_ep_fifo_flush(struct usb_ep *_ep)
1144 struct pxa2xx_ep *ep;
1146 ep = container_of(_ep, struct pxa2xx_ep, ep);
1147 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
1148 DMSG("%s, bad ep\n", __FUNCTION__);
1152 /* toggle and halt bits stay unchanged */
1154 /* for OUT, just read and discard the FIFO contents. */
1155 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
1156 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
1157 (void) *ep->reg_uddr;
1161 /* most IN status is the same, but ISO can't stall */
1162 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
1163 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1168 static struct usb_ep_ops pxa2xx_ep_ops = {
1169 .enable = pxa2xx_ep_enable,
1170 .disable = pxa2xx_ep_disable,
1172 .alloc_request = pxa2xx_ep_alloc_request,
1173 .free_request = pxa2xx_ep_free_request,
1175 .alloc_buffer = pxa2xx_ep_alloc_buffer,
1176 .free_buffer = pxa2xx_ep_free_buffer,
1178 .queue = pxa2xx_ep_queue,
1179 .dequeue = pxa2xx_ep_dequeue,
1181 .set_halt = pxa2xx_ep_set_halt,
1182 .fifo_status = pxa2xx_ep_fifo_status,
1183 .fifo_flush = pxa2xx_ep_fifo_flush,
1187 /* ---------------------------------------------------------------------------
1188 * device-scoped parts of the api to the usb controller hardware
1189 * ---------------------------------------------------------------------------
1192 static int pxa2xx_udc_get_frame(struct usb_gadget *_gadget)
1194 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
1197 static int pxa2xx_udc_wakeup(struct usb_gadget *_gadget)
1199 /* host may not have enabled remote wakeup */
1200 if ((UDCCS0 & UDCCS0_DRWF) == 0)
1201 return -EHOSTUNREACH;
1202 udc_set_mask_UDCCR(UDCCR_RSM);
1206 static void stop_activity(struct pxa2xx_udc *, struct usb_gadget_driver *);
1207 static void udc_enable (struct pxa2xx_udc *);
1208 static void udc_disable(struct pxa2xx_udc *);
1210 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
1213 static int pullup(struct pxa2xx_udc *udc, int is_active)
1215 is_active = is_active && udc->vbus && udc->pullup;
1216 DMSG("%s\n", is_active ? "active" : "inactive");
1220 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1221 DMSG("disconnect %s\n", udc->driver
1222 ? udc->driver->driver.name
1224 stop_activity(udc, udc->driver);
1231 /* VBUS reporting logically comes from a transceiver */
1232 static int pxa2xx_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1234 struct pxa2xx_udc *udc;
1236 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
1237 udc->vbus = is_active = (is_active != 0);
1238 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
1239 pullup(udc, is_active);
1243 /* drivers may have software control over D+ pullup */
1244 static int pxa2xx_udc_pullup(struct usb_gadget *_gadget, int is_active)
1246 struct pxa2xx_udc *udc;
1248 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
1250 /* not all boards support pullup control */
1251 if (!udc->mach->udc_command)
1254 is_active = (is_active != 0);
1255 udc->pullup = is_active;
1256 pullup(udc, is_active);
1260 static const struct usb_gadget_ops pxa2xx_udc_ops = {
1261 .get_frame = pxa2xx_udc_get_frame,
1262 .wakeup = pxa2xx_udc_wakeup,
1263 .vbus_session = pxa2xx_udc_vbus_session,
1264 .pullup = pxa2xx_udc_pullup,
1266 // .vbus_draw ... boards may consume current from VBUS, up to
1267 // 100-500mA based on config. the 500uA suspend ceiling means
1268 // that exclusively vbus-powered PXA designs violate USB specs.
1271 /*-------------------------------------------------------------------------*/
1273 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1275 static const char proc_node_name [] = "driver/udc";
1278 udc_proc_read(char *page, char **start, off_t off, int count,
1279 int *eof, void *_dev)
1282 struct pxa2xx_udc *dev = _dev;
1284 unsigned size = count;
1285 unsigned long flags;
1292 local_irq_save(flags);
1294 /* basic device status */
1295 t = scnprintf(next, size, DRIVER_DESC "\n"
1296 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1297 driver_name, DRIVER_VERSION SIZE_STR DMASTR,
1298 dev->driver ? dev->driver->driver.name : "(none)",
1299 is_vbus_present() ? "full speed" : "disconnected");
1303 /* registers for device and ep0 */
1304 t = scnprintf(next, size,
1305 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1306 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1311 t = scnprintf(next, size,
1312 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1313 (tmp & UDCCR_REM) ? " rem" : "",
1314 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1315 (tmp & UDCCR_SRM) ? " srm" : "",
1316 (tmp & UDCCR_SUSIR) ? " susir" : "",
1317 (tmp & UDCCR_RESIR) ? " resir" : "",
1318 (tmp & UDCCR_RSM) ? " rsm" : "",
1319 (tmp & UDCCR_UDA) ? " uda" : "",
1320 (tmp & UDCCR_UDE) ? " ude" : "");
1325 t = scnprintf(next, size,
1326 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1327 (tmp & UDCCS0_SA) ? " sa" : "",
1328 (tmp & UDCCS0_RNE) ? " rne" : "",
1329 (tmp & UDCCS0_FST) ? " fst" : "",
1330 (tmp & UDCCS0_SST) ? " sst" : "",
1331 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1332 (tmp & UDCCS0_FTF) ? " ftf" : "",
1333 (tmp & UDCCS0_IPR) ? " ipr" : "",
1334 (tmp & UDCCS0_OPR) ? " opr" : "");
1340 t = scnprintf(next, size,
1341 "udccfr %02X =%s%s\n", tmp,
1342 (tmp & UDCCFR_AREN) ? " aren" : "",
1343 (tmp & UDCCFR_ACM) ? " acm" : "");
1348 if (!is_vbus_present() || !dev->driver)
1351 t = scnprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1352 dev->stats.write.bytes, dev->stats.write.ops,
1353 dev->stats.read.bytes, dev->stats.read.ops,
1358 /* dump endpoint queues */
1359 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1360 struct pxa2xx_ep *ep = &dev->ep [i];
1361 struct pxa2xx_request *req;
1365 const struct usb_endpoint_descriptor *d;
1370 tmp = *dev->ep [i].reg_udccs;
1371 t = scnprintf(next, size,
1372 "%s max %d %s udccs %02x irqs %lu/%lu\n",
1373 ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
1374 (ep->dma >= 0) ? "dma" : "pio", tmp,
1375 ep->pio_irqs, ep->dma_irqs);
1376 /* TODO translate all five groups of udccs bits! */
1378 } else /* ep0 should only have one transfer queued */
1379 t = scnprintf(next, size, "ep0 max 16 pio irqs %lu\n",
1381 if (t <= 0 || t > size)
1386 if (list_empty(&ep->queue)) {
1387 t = scnprintf(next, size, "\t(nothing queued)\n");
1388 if (t <= 0 || t > size)
1394 list_for_each_entry(req, &ep->queue, queue) {
1396 if (ep->dma >= 0 && req->queue.prev == &ep->queue)
1397 t = scnprintf(next, size,
1398 "\treq %p len %d/%d "
1399 "buf %p (dma%d dcmd %08x)\n",
1400 &req->req, req->req.actual,
1401 req->req.length, req->req.buf,
1402 ep->dma, DCMD(ep->dma)
1403 // low 13 bits == bytes-to-go
1407 t = scnprintf(next, size,
1408 "\treq %p len %d/%d buf %p\n",
1409 &req->req, req->req.actual,
1410 req->req.length, req->req.buf);
1411 if (t <= 0 || t > size)
1419 local_irq_restore(flags);
1421 return count - size;
1424 #define create_proc_files() \
1425 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
1426 #define remove_proc_files() \
1427 remove_proc_entry(proc_node_name, NULL)
1429 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1431 #define create_proc_files() do {} while (0)
1432 #define remove_proc_files() do {} while (0)
1434 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1436 /* "function" sysfs attribute */
1438 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1440 struct pxa2xx_udc *dev = dev_get_drvdata (_dev);
1443 || !dev->driver->function
1444 || strlen (dev->driver->function) > PAGE_SIZE)
1446 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1448 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1450 /*-------------------------------------------------------------------------*/
1453 * udc_disable - disable USB device controller
1455 static void udc_disable(struct pxa2xx_udc *dev)
1457 /* block all irqs */
1458 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1459 UICR0 = UICR1 = 0xff;
1462 /* if hardware supports it, disconnect from usb */
1465 udc_clear_mask_UDCCR(UDCCR_UDE);
1467 #ifdef CONFIG_ARCH_PXA
1468 /* Disable clock for USB device */
1469 pxa_set_cken(CKEN11_USB, 0);
1473 dev->gadget.speed = USB_SPEED_UNKNOWN;
1479 * udc_reinit - initialize software state
1481 static void udc_reinit(struct pxa2xx_udc *dev)
1485 /* device/ep0 records init */
1486 INIT_LIST_HEAD (&dev->gadget.ep_list);
1487 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1488 dev->ep0state = EP0_IDLE;
1490 /* basic endpoint records init */
1491 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1492 struct pxa2xx_ep *ep = &dev->ep[i];
1495 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1499 INIT_LIST_HEAD (&ep->queue);
1500 ep->pio_irqs = ep->dma_irqs = 0;
1503 /* the rest was statically initialized, and is read-only */
1506 /* until it's enabled, this UDC should be completely invisible
1509 static void udc_enable (struct pxa2xx_udc *dev)
1511 udc_clear_mask_UDCCR(UDCCR_UDE);
1513 #ifdef CONFIG_ARCH_PXA
1514 /* Enable clock for USB device */
1515 pxa_set_cken(CKEN11_USB, 1);
1519 /* try to clear these bits before we enable the udc */
1520 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1523 dev->gadget.speed = USB_SPEED_UNKNOWN;
1524 dev->stats.irqs = 0;
1527 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1529 * - if RESET is already in progress, ack interrupt
1530 * - unmask reset interrupt
1532 udc_set_mask_UDCCR(UDCCR_UDE);
1533 if (!(UDCCR & UDCCR_UDA))
1534 udc_ack_int_UDCCR(UDCCR_RSTIR);
1536 if (dev->has_cfr /* UDC_RES2 is defined */) {
1537 /* pxa255 (a0+) can avoid a set_config race that could
1538 * prevent gadget drivers from configuring correctly
1540 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1542 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1543 * which could result in missing packets and interrupts.
1544 * supposedly one bit per endpoint, controlling whether it
1545 * double buffers or not; ACM/AREN bits fit into the holes.
1546 * zero bits (like USIR0_IRx) disable double buffering.
1552 #ifdef DISABLE_TEST_MODE
1553 /* "test mode" seems to have become the default in later chip
1554 * revs, preventing double buffering (and invalidating docs).
1555 * this EXPERIMENT enables it for bulk endpoints by tweaking
1556 * undefined/reserved register bits (that other drivers clear).
1557 * Belcarra code comments noted this usage.
1559 if (fifo_mode & 1) { /* IN endpoints */
1560 UDC_RES1 |= USIR0_IR1|USIR0_IR6;
1561 UDC_RES2 |= USIR1_IR11;
1563 if (fifo_mode & 2) { /* OUT endpoints */
1564 UDC_RES1 |= USIR0_IR2|USIR0_IR7;
1565 UDC_RES2 |= USIR1_IR12;
1569 /* enable suspend/resume and reset irqs */
1570 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1572 /* enable ep0 irqs */
1573 UICR0 &= ~UICR0_IM0;
1575 /* if hardware supports it, pullup D+ and wait for reset */
1580 /* when a driver is successfully registered, it will receive
1581 * control requests including set_configuration(), which enables
1582 * non-control requests. then usb traffic follows until a
1583 * disconnect is reported. then a host may connect again, or
1584 * the driver might get unbound.
1586 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1588 struct pxa2xx_udc *dev = the_controller;
1592 || driver->speed < USB_SPEED_FULL
1595 || !driver->disconnect
1603 /* first hook up the driver ... */
1604 dev->driver = driver;
1605 dev->gadget.dev.driver = &driver->driver;
1608 device_add (&dev->gadget.dev);
1609 retval = driver->bind(&dev->gadget);
1611 DMSG("bind to driver %s --> error %d\n",
1612 driver->driver.name, retval);
1613 device_del (&dev->gadget.dev);
1616 dev->gadget.dev.driver = NULL;
1619 device_create_file(dev->dev, &dev_attr_function);
1621 /* ... then enable host detection and ep0; and we're ready
1622 * for set_configuration as well as eventual disconnect.
1624 DMSG("registered gadget driver '%s'\n", driver->driver.name);
1629 EXPORT_SYMBOL(usb_gadget_register_driver);
1632 stop_activity(struct pxa2xx_udc *dev, struct usb_gadget_driver *driver)
1636 /* don't disconnect drivers more than once */
1637 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1639 dev->gadget.speed = USB_SPEED_UNKNOWN;
1641 /* prevent new request submissions, kill any outstanding requests */
1642 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1643 struct pxa2xx_ep *ep = &dev->ep[i];
1646 nuke(ep, -ESHUTDOWN);
1648 del_timer_sync(&dev->timer);
1650 /* report disconnect; the driver is already quiesced */
1653 driver->disconnect(&dev->gadget);
1655 /* re-init driver-visible data structures */
1659 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1661 struct pxa2xx_udc *dev = the_controller;
1665 if (!driver || driver != dev->driver)
1668 local_irq_disable();
1670 stop_activity(dev, driver);
1673 driver->unbind(&dev->gadget);
1676 device_del (&dev->gadget.dev);
1677 device_remove_file(dev->dev, &dev_attr_function);
1679 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1683 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1686 /*-------------------------------------------------------------------------*/
1688 #ifdef CONFIG_ARCH_LUBBOCK
1690 /* Lubbock has separate connect and disconnect irqs. More typical designs
1691 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1695 lubbock_vbus_irq(int irq, void *_dev, struct pt_regs *r)
1697 struct pxa2xx_udc *dev = _dev;
1701 HEX_DISPLAY(dev->stats.irqs);
1703 case LUBBOCK_USB_IRQ:
1706 disable_irq(LUBBOCK_USB_IRQ);
1707 enable_irq(LUBBOCK_USB_DISC_IRQ);
1709 case LUBBOCK_USB_DISC_IRQ:
1712 disable_irq(LUBBOCK_USB_DISC_IRQ);
1713 enable_irq(LUBBOCK_USB_IRQ);
1719 pxa2xx_udc_vbus_session(&dev->gadget, vbus);
1726 /*-------------------------------------------------------------------------*/
1728 static inline void clear_ep_state (struct pxa2xx_udc *dev)
1732 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1733 * fifos, and pending transactions mustn't be continued in any case.
1735 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1736 nuke(&dev->ep[i], -ECONNABORTED);
1739 static void udc_watchdog(unsigned long _dev)
1741 struct pxa2xx_udc *dev = (void *)_dev;
1743 local_irq_disable();
1744 if (dev->ep0state == EP0_STALL
1745 && (UDCCS0 & UDCCS0_FST) == 0
1746 && (UDCCS0 & UDCCS0_SST) == 0) {
1747 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1748 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1749 start_watchdog(dev);
1754 static void handle_ep0 (struct pxa2xx_udc *dev)
1756 u32 udccs0 = UDCCS0;
1757 struct pxa2xx_ep *ep = &dev->ep [0];
1758 struct pxa2xx_request *req;
1760 struct usb_ctrlrequest r;
1765 if (list_empty(&ep->queue))
1768 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
1770 /* clear stall status */
1771 if (udccs0 & UDCCS0_SST) {
1773 UDCCS0 = UDCCS0_SST;
1774 del_timer(&dev->timer);
1778 /* previous request unfinished? non-error iff back-to-back ... */
1779 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1781 del_timer(&dev->timer);
1785 switch (dev->ep0state) {
1787 /* late-breaking status? */
1790 /* start control request? */
1791 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1792 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1797 /* read SETUP packet */
1798 for (i = 0; i < 8; i++) {
1799 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1801 DMSG("SETUP %d!\n", i);
1804 u.raw [i] = (u8) UDDR0;
1806 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1810 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1811 u.r.bRequestType, u.r.bRequest,
1812 le16_to_cpu(u.r.wValue),
1813 le16_to_cpu(u.r.wIndex),
1814 le16_to_cpu(u.r.wLength));
1816 /* cope with automagic for some standard requests. */
1817 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1818 == USB_TYPE_STANDARD;
1819 dev->req_config = 0;
1820 dev->req_pending = 1;
1821 switch (u.r.bRequest) {
1822 /* hardware restricts gadget drivers here! */
1823 case USB_REQ_SET_CONFIGURATION:
1824 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1825 /* reflect hardware's automagic
1826 * up to the gadget driver.
1829 dev->req_config = 1;
1830 clear_ep_state(dev);
1831 /* if !has_cfr, there's no synch
1832 * else use AREN (later) not SA|OPR
1833 * USIR0_IR0 acts edge sensitive
1837 /* ... and here, even more ... */
1838 case USB_REQ_SET_INTERFACE:
1839 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1840 /* udc hardware is broken by design:
1841 * - altsetting may only be zero;
1842 * - hw resets all interfaces' eps;
1843 * - ep reset doesn't include halt(?).
1845 DMSG("broken set_interface (%d/%d)\n",
1846 le16_to_cpu(u.r.wIndex),
1847 le16_to_cpu(u.r.wValue));
1851 /* hardware was supposed to hide this */
1852 case USB_REQ_SET_ADDRESS:
1853 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1854 ep0start(dev, 0, "address");
1860 if (u.r.bRequestType & USB_DIR_IN)
1861 dev->ep0state = EP0_IN_DATA_PHASE;
1863 dev->ep0state = EP0_OUT_DATA_PHASE;
1865 i = dev->driver->setup(&dev->gadget, &u.r);
1867 /* hardware automagic preventing STALL... */
1868 if (dev->req_config) {
1869 /* hardware sometimes neglects to tell
1870 * tell us about config change events,
1871 * so later ones may fail...
1873 WARN("config change %02x fail %d?\n",
1876 /* TODO experiment: if has_cfr,
1877 * hardware didn't ACK; maybe we
1878 * could actually STALL!
1881 DBG(DBG_VERBOSE, "protocol STALL, "
1882 "%02x err %d\n", UDCCS0, i);
1884 /* the watchdog timer helps deal with cases
1885 * where udc seems to clear FST wrongly, and
1886 * then NAKs instead of STALLing.
1888 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1889 start_watchdog(dev);
1890 dev->ep0state = EP0_STALL;
1892 /* deferred i/o == no response yet */
1893 } else if (dev->req_pending) {
1894 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1895 || dev->req_std || u.r.wLength))
1896 ep0start(dev, 0, "defer");
1898 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1901 /* expect at least one data or status stage irq */
1904 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1905 == (UDCCS0_OPR|UDCCS0_SA))) {
1908 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1909 * still observed on a pxa255 a0.
1911 DBG(DBG_VERBOSE, "e131\n");
1914 /* read SETUP data, but don't trust it too much */
1915 for (i = 0; i < 8; i++)
1916 u.raw [i] = (u8) UDDR0;
1917 if ((u.r.bRequestType & USB_RECIP_MASK)
1920 if (u.word [0] == 0 && u.word [1] == 0)
1924 /* some random early IRQ:
1927 * - OPR got set, without SA (likely status stage)
1929 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1932 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1933 if (udccs0 & UDCCS0_OPR) {
1934 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1935 DBG(DBG_VERBOSE, "ep0in premature status\n");
1939 } else /* irq was IPR clearing */ {
1941 /* this IN packet might finish the request */
1942 (void) write_ep0_fifo(ep, req);
1943 } /* else IN token before response was written */
1946 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1947 if (udccs0 & UDCCS0_OPR) {
1949 /* this OUT packet might finish the request */
1950 if (read_ep0_fifo(ep, req))
1952 /* else more OUT packets expected */
1953 } /* else OUT token before read was issued */
1954 } else /* irq was IPR clearing */ {
1955 DBG(DBG_VERBOSE, "ep0out premature status\n");
1964 /* ack control-IN status (maybe in-zlp was skipped)
1965 * also appears after some config change events.
1967 if (udccs0 & UDCCS0_OPR)
1968 UDCCS0 = UDCCS0_OPR;
1972 UDCCS0 = UDCCS0_FST;
1978 static void handle_ep(struct pxa2xx_ep *ep)
1980 struct pxa2xx_request *req;
1981 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1987 if (likely (!list_empty(&ep->queue)))
1988 req = list_entry(ep->queue.next,
1989 struct pxa2xx_request, queue);
1993 // TODO check FST handling
1995 udccs = *ep->reg_udccs;
1996 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1998 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1999 tmp |= UDCCS_BI_SST;
2002 *ep->reg_udccs = tmp;
2003 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
2004 completed = write_fifo(ep, req);
2006 } else { /* irq from RPC (or for ISO, ROF) */
2007 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
2008 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
2010 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
2013 *ep->reg_udccs = tmp;
2015 /* fifos can hold packets, ready for reading... */
2018 // TODO didn't yet debug out-dma. this approach assumes
2019 // the worst about short packets and RPC; it might be better.
2021 if (likely(ep->dma >= 0)) {
2022 if (!(udccs & UDCCS_BO_RSP)) {
2023 *ep->reg_udccs = UDCCS_BO_RPC;
2029 completed = read_fifo(ep, req);
2031 pio_irq_disable (ep->bEndpointAddress);
2034 } while (completed);
2038 * pxa2xx_udc_irq - interrupt handler
2040 * avoid delays in ep0 processing. the control handshaking isn't always
2041 * under software control (pxa250c0 and the pxa255 are better), and delays
2042 * could cause usb protocol errors.
2045 pxa2xx_udc_irq(int irq, void *_dev, struct pt_regs *r)
2047 struct pxa2xx_udc *dev = _dev;
2051 HEX_DISPLAY(dev->stats.irqs);
2057 /* SUSpend Interrupt Request */
2058 if (unlikely(udccr & UDCCR_SUSIR)) {
2059 udc_ack_int_UDCCR(UDCCR_SUSIR);
2061 DBG(DBG_VERBOSE, "USB suspend%s\n", is_vbus_present()
2062 ? "" : "+disconnect");
2064 if (!is_vbus_present())
2065 stop_activity(dev, dev->driver);
2066 else if (dev->gadget.speed != USB_SPEED_UNKNOWN
2068 && dev->driver->suspend)
2069 dev->driver->suspend(&dev->gadget);
2073 /* RESume Interrupt Request */
2074 if (unlikely(udccr & UDCCR_RESIR)) {
2075 udc_ack_int_UDCCR(UDCCR_RESIR);
2077 DBG(DBG_VERBOSE, "USB resume\n");
2079 if (dev->gadget.speed != USB_SPEED_UNKNOWN
2081 && dev->driver->resume
2082 && is_vbus_present())
2083 dev->driver->resume(&dev->gadget);
2086 /* ReSeT Interrupt Request - USB reset */
2087 if (unlikely(udccr & UDCCR_RSTIR)) {
2088 udc_ack_int_UDCCR(UDCCR_RSTIR);
2091 if ((UDCCR & UDCCR_UDA) == 0) {
2092 DBG(DBG_VERBOSE, "USB reset start\n");
2094 /* reset driver and endpoints,
2095 * in case that's not yet done
2097 stop_activity (dev, dev->driver);
2100 DBG(DBG_VERBOSE, "USB reset end\n");
2101 dev->gadget.speed = USB_SPEED_FULL;
2103 memset(&dev->stats, 0, sizeof dev->stats);
2104 /* driver and endpoints are still reset */
2108 u32 usir0 = USIR0 & ~UICR0;
2109 u32 usir1 = USIR1 & ~UICR1;
2112 if (unlikely (!usir0 && !usir1))
2115 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
2117 /* control traffic */
2118 if (usir0 & USIR0_IR0) {
2119 dev->ep[0].pio_irqs++;
2124 /* endpoint data transfers */
2125 for (i = 0; i < 8; i++) {
2128 if (i && (usir0 & tmp)) {
2129 handle_ep(&dev->ep[i]);
2134 handle_ep(&dev->ep[i+8]);
2141 /* we could also ask for 1 msec SOF (SIR) interrupts */
2147 /*-------------------------------------------------------------------------*/
2149 static void nop_release (struct device *dev)
2151 DMSG("%s %s\n", __FUNCTION__, dev->bus_id);
2154 /* this uses load-time allocation and initialization (instead of
2155 * doing it at run-time) to save code, eliminate fault paths, and
2156 * be more obviously correct.
2158 static struct pxa2xx_udc memory = {
2160 .ops = &pxa2xx_udc_ops,
2161 .ep0 = &memory.ep[0].ep,
2162 .name = driver_name,
2165 .release = nop_release,
2169 /* control endpoint */
2173 .ops = &pxa2xx_ep_ops,
2174 .maxpacket = EP0_FIFO_SIZE,
2177 .reg_udccs = &UDCCS0,
2181 /* first group of endpoints */
2184 .name = "ep1in-bulk",
2185 .ops = &pxa2xx_ep_ops,
2186 .maxpacket = BULK_FIFO_SIZE,
2189 .fifo_size = BULK_FIFO_SIZE,
2190 .bEndpointAddress = USB_DIR_IN | 1,
2191 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2192 .reg_udccs = &UDCCS1,
2198 .name = "ep2out-bulk",
2199 .ops = &pxa2xx_ep_ops,
2200 .maxpacket = BULK_FIFO_SIZE,
2203 .fifo_size = BULK_FIFO_SIZE,
2204 .bEndpointAddress = 2,
2205 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2206 .reg_udccs = &UDCCS2,
2211 #ifndef CONFIG_USB_PXA2XX_SMALL
2214 .name = "ep3in-iso",
2215 .ops = &pxa2xx_ep_ops,
2216 .maxpacket = ISO_FIFO_SIZE,
2219 .fifo_size = ISO_FIFO_SIZE,
2220 .bEndpointAddress = USB_DIR_IN | 3,
2221 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2222 .reg_udccs = &UDCCS3,
2228 .name = "ep4out-iso",
2229 .ops = &pxa2xx_ep_ops,
2230 .maxpacket = ISO_FIFO_SIZE,
2233 .fifo_size = ISO_FIFO_SIZE,
2234 .bEndpointAddress = 4,
2235 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2236 .reg_udccs = &UDCCS4,
2243 .name = "ep5in-int",
2244 .ops = &pxa2xx_ep_ops,
2245 .maxpacket = INT_FIFO_SIZE,
2248 .fifo_size = INT_FIFO_SIZE,
2249 .bEndpointAddress = USB_DIR_IN | 5,
2250 .bmAttributes = USB_ENDPOINT_XFER_INT,
2251 .reg_udccs = &UDCCS5,
2255 /* second group of endpoints */
2258 .name = "ep6in-bulk",
2259 .ops = &pxa2xx_ep_ops,
2260 .maxpacket = BULK_FIFO_SIZE,
2263 .fifo_size = BULK_FIFO_SIZE,
2264 .bEndpointAddress = USB_DIR_IN | 6,
2265 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2266 .reg_udccs = &UDCCS6,
2272 .name = "ep7out-bulk",
2273 .ops = &pxa2xx_ep_ops,
2274 .maxpacket = BULK_FIFO_SIZE,
2277 .fifo_size = BULK_FIFO_SIZE,
2278 .bEndpointAddress = 7,
2279 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2280 .reg_udccs = &UDCCS7,
2287 .name = "ep8in-iso",
2288 .ops = &pxa2xx_ep_ops,
2289 .maxpacket = ISO_FIFO_SIZE,
2292 .fifo_size = ISO_FIFO_SIZE,
2293 .bEndpointAddress = USB_DIR_IN | 8,
2294 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2295 .reg_udccs = &UDCCS8,
2301 .name = "ep9out-iso",
2302 .ops = &pxa2xx_ep_ops,
2303 .maxpacket = ISO_FIFO_SIZE,
2306 .fifo_size = ISO_FIFO_SIZE,
2307 .bEndpointAddress = 9,
2308 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2309 .reg_udccs = &UDCCS9,
2316 .name = "ep10in-int",
2317 .ops = &pxa2xx_ep_ops,
2318 .maxpacket = INT_FIFO_SIZE,
2321 .fifo_size = INT_FIFO_SIZE,
2322 .bEndpointAddress = USB_DIR_IN | 10,
2323 .bmAttributes = USB_ENDPOINT_XFER_INT,
2324 .reg_udccs = &UDCCS10,
2325 .reg_uddr = &UDDR10,
2328 /* third group of endpoints */
2331 .name = "ep11in-bulk",
2332 .ops = &pxa2xx_ep_ops,
2333 .maxpacket = BULK_FIFO_SIZE,
2336 .fifo_size = BULK_FIFO_SIZE,
2337 .bEndpointAddress = USB_DIR_IN | 11,
2338 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2339 .reg_udccs = &UDCCS11,
2340 .reg_uddr = &UDDR11,
2345 .name = "ep12out-bulk",
2346 .ops = &pxa2xx_ep_ops,
2347 .maxpacket = BULK_FIFO_SIZE,
2350 .fifo_size = BULK_FIFO_SIZE,
2351 .bEndpointAddress = 12,
2352 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2353 .reg_udccs = &UDCCS12,
2354 .reg_ubcr = &UBCR12,
2355 .reg_uddr = &UDDR12,
2360 .name = "ep13in-iso",
2361 .ops = &pxa2xx_ep_ops,
2362 .maxpacket = ISO_FIFO_SIZE,
2365 .fifo_size = ISO_FIFO_SIZE,
2366 .bEndpointAddress = USB_DIR_IN | 13,
2367 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2368 .reg_udccs = &UDCCS13,
2369 .reg_uddr = &UDDR13,
2374 .name = "ep14out-iso",
2375 .ops = &pxa2xx_ep_ops,
2376 .maxpacket = ISO_FIFO_SIZE,
2379 .fifo_size = ISO_FIFO_SIZE,
2380 .bEndpointAddress = 14,
2381 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2382 .reg_udccs = &UDCCS14,
2383 .reg_ubcr = &UBCR14,
2384 .reg_uddr = &UDDR14,
2389 .name = "ep15in-int",
2390 .ops = &pxa2xx_ep_ops,
2391 .maxpacket = INT_FIFO_SIZE,
2394 .fifo_size = INT_FIFO_SIZE,
2395 .bEndpointAddress = USB_DIR_IN | 15,
2396 .bmAttributes = USB_ENDPOINT_XFER_INT,
2397 .reg_udccs = &UDCCS15,
2398 .reg_uddr = &UDDR15,
2400 #endif /* !CONFIG_USB_PXA2XX_SMALL */
2403 #define CP15R0_VENDOR_MASK 0xffffe000
2405 #if defined(CONFIG_ARCH_PXA)
2406 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2408 #elif defined(CONFIG_ARCH_IXP4XX)
2409 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2413 #define CP15R0_PROD_MASK 0x000003f0
2414 #define PXA25x 0x00000100 /* and PXA26x */
2415 #define PXA210 0x00000120
2417 #define CP15R0_REV_MASK 0x0000000f
2419 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2421 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2422 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2423 #define PXA250_B2 0x00000104
2424 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2425 #define PXA250_B0 0x00000102
2426 #define PXA250_A1 0x00000101
2427 #define PXA250_A0 0x00000100
2429 #define PXA210_C0 0x00000125
2430 #define PXA210_B2 0x00000124
2431 #define PXA210_B1 0x00000123
2432 #define PXA210_B0 0x00000122
2433 #define IXP425_A0 0x000001c1
2434 #define IXP465_AD 0x00000200
2437 * probe - binds to the platform device
2439 static int __init pxa2xx_udc_probe(struct platform_device *pdev)
2441 struct pxa2xx_udc *dev = &memory;
2442 int retval, out_dma = 1;
2445 /* insist on Intel/ARM/XScale */
2446 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2447 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2448 printk(KERN_ERR "%s: not XScale!\n", driver_name);
2452 /* trigger chiprev-specific logic */
2453 switch (chiprev & CP15R0_PRODREV_MASK) {
2454 #if defined(CONFIG_ARCH_PXA)
2460 /* A0/A1 "not released"; ep 13, 15 unusable */
2462 case PXA250_B2: case PXA210_B2:
2463 case PXA250_B1: case PXA210_B1:
2464 case PXA250_B0: case PXA210_B0:
2467 case PXA250_C0: case PXA210_C0:
2469 #elif defined(CONFIG_ARCH_IXP4XX)
2478 printk(KERN_ERR "%s: unrecognized processor: %08x\n",
2479 driver_name, chiprev);
2480 /* iop3xx, ixp4xx, ... */
2484 pr_debug("%s: IRQ %d%s%s%s\n", driver_name, IRQ_USB,
2485 dev->has_cfr ? "" : " (!cfr)",
2486 out_dma ? "" : " (broken dma-out)",
2494 /* pxa 250 erratum 130 prevents using OUT dma (fixed C0) */
2496 DMSG("disabled OUT dma\n");
2497 dev->ep[ 2].reg_drcmr = dev->ep[ 4].reg_drcmr = 0;
2498 dev->ep[ 7].reg_drcmr = dev->ep[ 9].reg_drcmr = 0;
2499 dev->ep[12].reg_drcmr = dev->ep[14].reg_drcmr = 0;
2503 /* other non-static parts of init */
2504 dev->dev = &pdev->dev;
2505 dev->mach = pdev->dev.platform_data;
2507 init_timer(&dev->timer);
2508 dev->timer.function = udc_watchdog;
2509 dev->timer.data = (unsigned long) dev;
2511 device_initialize(&dev->gadget.dev);
2512 dev->gadget.dev.parent = &pdev->dev;
2513 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2515 the_controller = dev;
2516 platform_set_drvdata(pdev, dev);
2521 dev->vbus = is_vbus_present();
2523 /* irq setup after old hardware state is cleaned up */
2524 retval = request_irq(IRQ_USB, pxa2xx_udc_irq,
2525 SA_INTERRUPT, driver_name, dev);
2527 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2528 driver_name, IRQ_USB, retval);
2533 #ifdef CONFIG_ARCH_LUBBOCK
2534 if (machine_is_lubbock()) {
2535 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2537 SA_INTERRUPT | SA_SAMPLE_RANDOM,
2540 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2541 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2543 free_irq(IRQ_USB, dev);
2546 retval = request_irq(LUBBOCK_USB_IRQ,
2548 SA_INTERRUPT | SA_SAMPLE_RANDOM,
2551 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2552 driver_name, LUBBOCK_USB_IRQ, retval);
2553 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2557 /* with U-Boot (but not BLOB), hex is off by default */
2558 HEX_DISPLAY(dev->stats.irqs);
2559 LUB_DISC_BLNK_LED &= 0xff;
2563 create_proc_files();
2568 static void pxa2xx_udc_shutdown(struct platform_device *_dev)
2573 static int __exit pxa2xx_udc_remove(struct platform_device *pdev)
2575 struct pxa2xx_udc *dev = platform_get_drvdata(pdev);
2578 remove_proc_files();
2579 usb_gadget_unregister_driver(dev->driver);
2582 free_irq(IRQ_USB, dev);
2585 #ifdef CONFIG_ARCH_LUBBOCK
2586 if (machine_is_lubbock()) {
2587 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2588 free_irq(LUBBOCK_USB_IRQ, dev);
2591 platform_set_drvdata(pdev, NULL);
2592 the_controller = NULL;
2596 /*-------------------------------------------------------------------------*/
2600 /* USB suspend (controlled by the host) and system suspend (controlled
2601 * by the PXA) don't necessarily work well together. If USB is active,
2602 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2603 * mode, or any deeper PM saving state.
2605 * For now, we punt and forcibly disconnect from the USB host when PXA
2606 * enters any suspend state. While we're disconnected, we always disable
2607 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2608 * Boards without software pullup control shouldn't use those states.
2609 * VBUS IRQs should probably be ignored so that the PXA device just acts
2610 * "dead" to USB hosts until system resume.
2612 static int pxa2xx_udc_suspend(struct platform_device *dev, pm_message_t state)
2614 struct pxa2xx_udc *udc = platform_get_drvdata(dev);
2616 if (!udc->mach->udc_command)
2617 WARN("USB host won't detect disconnect!\n");
2623 static int pxa2xx_udc_resume(struct platform_device *dev)
2625 struct pxa2xx_udc *udc = platform_get_drvdata(dev);
2633 #define pxa2xx_udc_suspend NULL
2634 #define pxa2xx_udc_resume NULL
2637 /*-------------------------------------------------------------------------*/
2639 static struct platform_driver udc_driver = {
2640 .probe = pxa2xx_udc_probe,
2641 .shutdown = pxa2xx_udc_shutdown,
2642 .remove = __exit_p(pxa2xx_udc_remove),
2643 .suspend = pxa2xx_udc_suspend,
2644 .resume = pxa2xx_udc_resume,
2646 .owner = THIS_MODULE,
2647 .name = "pxa2xx-udc",
2651 static int __init udc_init(void)
2653 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2654 return platform_driver_register(&udc_driver);
2656 module_init(udc_init);
2658 static void __exit udc_exit(void)
2660 platform_driver_unregister(&udc_driver);
2662 module_exit(udc_exit);
2664 MODULE_DESCRIPTION(DRIVER_DESC);
2665 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2666 MODULE_LICENSE("GPL");