init from v2.6.32.60
[mach-moxart.git] / drivers / usb / musb / musb_gadget.c
blobc6f5ee4575cf26ce8ab8d5fcdcee8aa04648c905
1 /*
2 * MUSB OTG driver peripheral support
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
7 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
23 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <linux/kernel.h>
37 #include <linux/list.h>
38 #include <linux/timer.h>
39 #include <linux/module.h>
40 #include <linux/smp.h>
41 #include <linux/spinlock.h>
42 #include <linux/delay.h>
43 #include <linux/moduleparam.h>
44 #include <linux/stat.h>
45 #include <linux/dma-mapping.h>
47 #include "musb_core.h"
50 /* MUSB PERIPHERAL status 3-mar-2006:
52 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
53 * Minor glitches:
55 * + remote wakeup to Linux hosts work, but saw USBCV failures;
56 * in one test run (operator error?)
57 * + endpoint halt tests -- in both usbtest and usbcv -- seem
58 * to break when dma is enabled ... is something wrongly
59 * clearing SENDSTALL?
61 * - Mass storage behaved ok when last tested. Network traffic patterns
62 * (with lots of short transfers etc) need retesting; they turn up the
63 * worst cases of the DMA, since short packets are typical but are not
64 * required.
66 * - TX/IN
67 * + both pio and dma behave in with network and g_zero tests
68 * + no cppi throughput issues other than no-hw-queueing
69 * + failed with FLAT_REG (DaVinci)
70 * + seems to behave with double buffering, PIO -and- CPPI
71 * + with gadgetfs + AIO, requests got lost?
73 * - RX/OUT
74 * + both pio and dma behave in with network and g_zero tests
75 * + dma is slow in typical case (short_not_ok is clear)
76 * + double buffering ok with PIO
77 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
78 * + request lossage observed with gadgetfs
80 * - ISO not tested ... might work, but only weakly isochronous
82 * - Gadget driver disabling of softconnect during bind() is ignored; so
83 * drivers can't hold off host requests until userspace is ready.
84 * (Workaround: they can turn it off later.)
86 * - PORTABILITY (assumes PIO works):
87 * + DaVinci, basically works with cppi dma
88 * + OMAP 2430, ditto with mentor dma
89 * + TUSB 6010, platform-specific dma in the works
92 /* ----------------------------------------------------------------------- */
95 * Immediately complete a request.
97 * @param request the request to complete
98 * @param status the status to complete the request with
99 * Context: controller locked, IRQs blocked.
101 void musb_g_giveback(
102 struct musb_ep *ep,
103 struct usb_request *request,
104 int status)
105 __releases(ep->musb->lock)
106 __acquires(ep->musb->lock)
108 struct musb_request *req;
109 struct musb *musb;
110 int busy = ep->busy;
112 req = to_musb_request(request);
114 list_del(&request->list);
115 if (req->request.status == -EINPROGRESS)
116 req->request.status = status;
117 musb = req->musb;
119 ep->busy = 1;
120 spin_unlock(&musb->lock);
121 if (is_dma_capable()) {
122 if (req->mapped) {
123 dma_unmap_single(musb->controller,
124 req->request.dma,
125 req->request.length,
126 req->tx
127 ? DMA_TO_DEVICE
128 : DMA_FROM_DEVICE);
129 req->request.dma = DMA_ADDR_INVALID;
130 req->mapped = 0;
131 } else if (req->request.dma != DMA_ADDR_INVALID)
132 dma_sync_single_for_cpu(musb->controller,
133 req->request.dma,
134 req->request.length,
135 req->tx
136 ? DMA_TO_DEVICE
137 : DMA_FROM_DEVICE);
139 if (request->status == 0)
140 DBG(5, "%s done request %p, %d/%d\n",
141 ep->end_point.name, request,
142 req->request.actual, req->request.length);
143 else
144 DBG(2, "%s request %p, %d/%d fault %d\n",
145 ep->end_point.name, request,
146 req->request.actual, req->request.length,
147 request->status);
148 req->request.complete(&req->ep->end_point, &req->request);
149 spin_lock(&musb->lock);
150 ep->busy = busy;
153 /* ----------------------------------------------------------------------- */
156 * Abort requests queued to an endpoint using the status. Synchronous.
157 * caller locked controller and blocked irqs, and selected this ep.
159 static void nuke(struct musb_ep *ep, const int status)
161 struct musb_request *req = NULL;
162 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
164 ep->busy = 1;
166 if (is_dma_capable() && ep->dma) {
167 struct dma_controller *c = ep->musb->dma_controller;
168 int value;
170 if (ep->is_in) {
172 * The programming guide says that we must not clear
173 * the DMAMODE bit before DMAENAB, so we only
174 * clear it in the second write...
176 musb_writew(epio, MUSB_TXCSR,
177 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
178 musb_writew(epio, MUSB_TXCSR,
179 0 | MUSB_TXCSR_FLUSHFIFO);
180 } else {
181 musb_writew(epio, MUSB_RXCSR,
182 0 | MUSB_RXCSR_FLUSHFIFO);
183 musb_writew(epio, MUSB_RXCSR,
184 0 | MUSB_RXCSR_FLUSHFIFO);
187 value = c->channel_abort(ep->dma);
188 DBG(value ? 1 : 6, "%s: abort DMA --> %d\n", ep->name, value);
189 c->channel_release(ep->dma);
190 ep->dma = NULL;
193 while (!list_empty(&(ep->req_list))) {
194 req = container_of(ep->req_list.next, struct musb_request,
195 request.list);
196 musb_g_giveback(ep, &req->request, status);
200 /* ----------------------------------------------------------------------- */
202 /* Data transfers - pure PIO, pure DMA, or mixed mode */
205 * This assumes the separate CPPI engine is responding to DMA requests
206 * from the usb core ... sequenced a bit differently from mentor dma.
209 static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
211 if (can_bulk_split(musb, ep->type))
212 return ep->hw_ep->max_packet_sz_tx;
213 else
214 return ep->packet_sz;
218 #ifdef CONFIG_USB_INVENTRA_DMA
220 /* Peripheral tx (IN) using Mentor DMA works as follows:
221 Only mode 0 is used for transfers <= wPktSize,
222 mode 1 is used for larger transfers,
224 One of the following happens:
225 - Host sends IN token which causes an endpoint interrupt
226 -> TxAvail
227 -> if DMA is currently busy, exit.
228 -> if queue is non-empty, txstate().
230 - Request is queued by the gadget driver.
231 -> if queue was previously empty, txstate()
233 txstate()
234 -> start
235 /\ -> setup DMA
236 | (data is transferred to the FIFO, then sent out when
237 | IN token(s) are recd from Host.
238 | -> DMA interrupt on completion
239 | calls TxAvail.
240 | -> stop DMA, ~DMAENAB,
241 | -> set TxPktRdy for last short pkt or zlp
242 | -> Complete Request
243 | -> Continue next request (call txstate)
244 |___________________________________|
246 * Non-Mentor DMA engines can of course work differently, such as by
247 * upleveling from irq-per-packet to irq-per-buffer.
250 #endif
253 * An endpoint is transmitting data. This can be called either from
254 * the IRQ routine or from ep.queue() to kickstart a request on an
255 * endpoint.
257 * Context: controller locked, IRQs blocked, endpoint selected
259 static void txstate(struct musb *musb, struct musb_request *req)
261 u8 epnum = req->epnum;
262 struct musb_ep *musb_ep;
263 void __iomem *epio = musb->endpoints[epnum].regs;
264 struct usb_request *request;
265 u16 fifo_count = 0, csr;
266 int use_dma = 0;
268 musb_ep = req->ep;
270 /* we shouldn't get here while DMA is active ... but we do ... */
271 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
272 DBG(4, "dma pending...\n");
273 return;
276 /* read TXCSR before */
277 csr = musb_readw(epio, MUSB_TXCSR);
279 request = &req->request;
280 fifo_count = min(max_ep_writesize(musb, musb_ep),
281 (int)(request->length - request->actual));
283 if (csr & MUSB_TXCSR_TXPKTRDY) {
284 DBG(5, "%s old packet still ready , txcsr %03x\n",
285 musb_ep->end_point.name, csr);
286 return;
289 if (csr & MUSB_TXCSR_P_SENDSTALL) {
290 DBG(5, "%s stalling, txcsr %03x\n",
291 musb_ep->end_point.name, csr);
292 return;
295 DBG(4, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
296 epnum, musb_ep->packet_sz, fifo_count,
297 csr);
299 #ifndef CONFIG_MUSB_PIO_ONLY
300 if (is_dma_capable() && musb_ep->dma) {
301 struct dma_controller *c = musb->dma_controller;
303 use_dma = (request->dma != DMA_ADDR_INVALID);
305 /* MUSB_TXCSR_P_ISO is still set correctly */
307 #ifdef CONFIG_USB_INVENTRA_DMA
309 size_t request_size;
311 /* setup DMA, then program endpoint CSR */
312 request_size = min(request->length,
313 musb_ep->dma->max_len);
314 if (request_size < musb_ep->packet_sz)
315 musb_ep->dma->desired_mode = 0;
316 else
317 musb_ep->dma->desired_mode = 1;
319 use_dma = use_dma && c->channel_program(
320 musb_ep->dma, musb_ep->packet_sz,
321 musb_ep->dma->desired_mode,
322 request->dma, request_size);
323 if (use_dma) {
324 if (musb_ep->dma->desired_mode == 0) {
326 * We must not clear the DMAMODE bit
327 * before the DMAENAB bit -- and the
328 * latter doesn't always get cleared
329 * before we get here...
331 csr &= ~(MUSB_TXCSR_AUTOSET
332 | MUSB_TXCSR_DMAENAB);
333 musb_writew(epio, MUSB_TXCSR, csr
334 | MUSB_TXCSR_P_WZC_BITS);
335 csr &= ~MUSB_TXCSR_DMAMODE;
336 csr |= (MUSB_TXCSR_DMAENAB |
337 MUSB_TXCSR_MODE);
338 /* against programming guide */
339 } else
340 csr |= (MUSB_TXCSR_AUTOSET
341 | MUSB_TXCSR_DMAENAB
342 | MUSB_TXCSR_DMAMODE
343 | MUSB_TXCSR_MODE);
345 csr &= ~MUSB_TXCSR_P_UNDERRUN;
346 musb_writew(epio, MUSB_TXCSR, csr);
350 #elif defined(CONFIG_USB_TI_CPPI_DMA)
351 /* program endpoint CSR first, then setup DMA */
352 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
353 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
354 MUSB_TXCSR_MODE;
355 musb_writew(epio, MUSB_TXCSR,
356 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
357 | csr);
359 /* ensure writebuffer is empty */
360 csr = musb_readw(epio, MUSB_TXCSR);
362 /* NOTE host side sets DMAENAB later than this; both are
363 * OK since the transfer dma glue (between CPPI and Mentor
364 * fifos) just tells CPPI it could start. Data only moves
365 * to the USB TX fifo when both fifos are ready.
368 /* "mode" is irrelevant here; handle terminating ZLPs like
369 * PIO does, since the hardware RNDIS mode seems unreliable
370 * except for the last-packet-is-already-short case.
372 use_dma = use_dma && c->channel_program(
373 musb_ep->dma, musb_ep->packet_sz,
375 request->dma,
376 request->length);
377 if (!use_dma) {
378 c->channel_release(musb_ep->dma);
379 musb_ep->dma = NULL;
380 csr &= ~MUSB_TXCSR_DMAENAB;
381 musb_writew(epio, MUSB_TXCSR, csr);
382 /* invariant: prequest->buf is non-null */
384 #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
385 use_dma = use_dma && c->channel_program(
386 musb_ep->dma, musb_ep->packet_sz,
387 request->zero,
388 request->dma,
389 request->length);
390 #endif
392 #endif
394 if (!use_dma) {
395 musb_write_fifo(musb_ep->hw_ep, fifo_count,
396 (u8 *) (request->buf + request->actual));
397 request->actual += fifo_count;
398 csr |= MUSB_TXCSR_TXPKTRDY;
399 csr &= ~MUSB_TXCSR_P_UNDERRUN;
400 musb_writew(epio, MUSB_TXCSR, csr);
403 /* host may already have the data when this message shows... */
404 DBG(3, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
405 musb_ep->end_point.name, use_dma ? "dma" : "pio",
406 request->actual, request->length,
407 musb_readw(epio, MUSB_TXCSR),
408 fifo_count,
409 musb_readw(epio, MUSB_TXMAXP));
413 * FIFO state update (e.g. data ready).
414 * Called from IRQ, with controller locked.
416 void musb_g_tx(struct musb *musb, u8 epnum)
418 u16 csr;
419 struct usb_request *request;
420 u8 __iomem *mbase = musb->mregs;
421 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
422 void __iomem *epio = musb->endpoints[epnum].regs;
423 struct dma_channel *dma;
425 musb_ep_select(mbase, epnum);
426 request = next_request(musb_ep);
428 csr = musb_readw(epio, MUSB_TXCSR);
429 DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
431 dma = is_dma_capable() ? musb_ep->dma : NULL;
432 do {
433 /* REVISIT for high bandwidth, MUSB_TXCSR_P_INCOMPTX
434 * probably rates reporting as a host error
436 if (csr & MUSB_TXCSR_P_SENTSTALL) {
437 csr |= MUSB_TXCSR_P_WZC_BITS;
438 csr &= ~MUSB_TXCSR_P_SENTSTALL;
439 musb_writew(epio, MUSB_TXCSR, csr);
440 break;
443 if (csr & MUSB_TXCSR_P_UNDERRUN) {
444 /* we NAKed, no big deal ... little reason to care */
445 csr |= MUSB_TXCSR_P_WZC_BITS;
446 csr &= ~(MUSB_TXCSR_P_UNDERRUN
447 | MUSB_TXCSR_TXPKTRDY);
448 musb_writew(epio, MUSB_TXCSR, csr);
449 DBG(20, "underrun on ep%d, req %p\n", epnum, request);
452 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
453 /* SHOULD NOT HAPPEN ... has with cppi though, after
454 * changing SENDSTALL (and other cases); harmless?
456 DBG(5, "%s dma still busy?\n", musb_ep->end_point.name);
457 break;
460 if (request) {
461 u8 is_dma = 0;
463 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
464 is_dma = 1;
465 csr |= MUSB_TXCSR_P_WZC_BITS;
466 csr &= ~(MUSB_TXCSR_DMAENAB
467 | MUSB_TXCSR_P_UNDERRUN
468 | MUSB_TXCSR_TXPKTRDY);
469 musb_writew(epio, MUSB_TXCSR, csr);
470 /* ensure writebuffer is empty */
471 csr = musb_readw(epio, MUSB_TXCSR);
472 request->actual += musb_ep->dma->actual_len;
473 DBG(4, "TXCSR%d %04x, dma off, "
474 "len %zu, req %p\n",
475 epnum, csr,
476 musb_ep->dma->actual_len,
477 request);
480 if (is_dma || request->actual == request->length) {
482 /* First, maybe a terminating short packet.
483 * Some DMA engines might handle this by
484 * themselves.
486 if ((request->zero
487 && request->length
488 && (request->length
489 % musb_ep->packet_sz)
490 == 0)
491 #ifdef CONFIG_USB_INVENTRA_DMA
492 || (is_dma &&
493 ((!dma->desired_mode) ||
494 (request->actual &
495 (musb_ep->packet_sz - 1))))
496 #endif
498 /* on dma completion, fifo may not
499 * be available yet ...
501 if (csr & MUSB_TXCSR_TXPKTRDY)
502 break;
504 DBG(4, "sending zero pkt\n");
505 musb_writew(epio, MUSB_TXCSR,
506 MUSB_TXCSR_MODE
507 | MUSB_TXCSR_TXPKTRDY);
508 request->zero = 0;
511 /* ... or if not, then complete it */
512 musb_g_giveback(musb_ep, request, 0);
514 /* kickstart next transfer if appropriate;
515 * the packet that just completed might not
516 * be transmitted for hours or days.
517 * REVISIT for double buffering...
518 * FIXME revisit for stalls too...
520 musb_ep_select(mbase, epnum);
521 csr = musb_readw(epio, MUSB_TXCSR);
522 if (csr & MUSB_TXCSR_FIFONOTEMPTY)
523 break;
524 request = musb_ep->desc
525 ? next_request(musb_ep)
526 : NULL;
527 if (!request) {
528 DBG(4, "%s idle now\n",
529 musb_ep->end_point.name);
530 break;
534 txstate(musb, to_musb_request(request));
537 } while (0);
540 /* ------------------------------------------------------------ */
542 #ifdef CONFIG_USB_INVENTRA_DMA
544 /* Peripheral rx (OUT) using Mentor DMA works as follows:
545 - Only mode 0 is used.
547 - Request is queued by the gadget class driver.
548 -> if queue was previously empty, rxstate()
550 - Host sends OUT token which causes an endpoint interrupt
551 /\ -> RxReady
552 | -> if request queued, call rxstate
553 | /\ -> setup DMA
554 | | -> DMA interrupt on completion
555 | | -> RxReady
556 | | -> stop DMA
557 | | -> ack the read
558 | | -> if data recd = max expected
559 | | by the request, or host
560 | | sent a short packet,
561 | | complete the request,
562 | | and start the next one.
563 | |_____________________________________|
564 | else just wait for the host
565 | to send the next OUT token.
566 |__________________________________________________|
568 * Non-Mentor DMA engines can of course work differently.
571 #endif
574 * Context: controller locked, IRQs blocked, endpoint selected
576 static void rxstate(struct musb *musb, struct musb_request *req)
578 const u8 epnum = req->epnum;
579 struct usb_request *request = &req->request;
580 struct musb_ep *musb_ep;
581 void __iomem *epio = musb->endpoints[epnum].regs;
582 unsigned fifo_count = 0;
583 u16 len;
584 u16 csr = musb_readw(epio, MUSB_RXCSR);
585 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
587 if (hw_ep->is_shared_fifo)
588 musb_ep = &hw_ep->ep_in;
589 else
590 musb_ep = &hw_ep->ep_out;
592 len = musb_ep->packet_sz;
594 /* We shouldn't get here while DMA is active, but we do... */
595 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
596 DBG(4, "DMA pending...\n");
597 return;
600 if (csr & MUSB_RXCSR_P_SENDSTALL) {
601 DBG(5, "%s stalling, RXCSR %04x\n",
602 musb_ep->end_point.name, csr);
603 return;
606 if (is_cppi_enabled() && musb_ep->dma) {
607 struct dma_controller *c = musb->dma_controller;
608 struct dma_channel *channel = musb_ep->dma;
610 /* NOTE: CPPI won't actually stop advancing the DMA
611 * queue after short packet transfers, so this is almost
612 * always going to run as IRQ-per-packet DMA so that
613 * faults will be handled correctly.
615 if (c->channel_program(channel,
616 musb_ep->packet_sz,
617 !request->short_not_ok,
618 request->dma + request->actual,
619 request->length - request->actual)) {
621 /* make sure that if an rxpkt arrived after the irq,
622 * the cppi engine will be ready to take it as soon
623 * as DMA is enabled
625 csr &= ~(MUSB_RXCSR_AUTOCLEAR
626 | MUSB_RXCSR_DMAMODE);
627 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
628 musb_writew(epio, MUSB_RXCSR, csr);
629 return;
633 if (csr & MUSB_RXCSR_RXPKTRDY) {
634 len = musb_readw(epio, MUSB_RXCOUNT);
635 if (request->actual < request->length) {
636 #ifdef CONFIG_USB_INVENTRA_DMA
637 if (is_dma_capable() && musb_ep->dma) {
638 struct dma_controller *c;
639 struct dma_channel *channel;
640 int use_dma = 0;
642 c = musb->dma_controller;
643 channel = musb_ep->dma;
645 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
646 * mode 0 only. So we do not get endpoint interrupts due to DMA
647 * completion. We only get interrupts from DMA controller.
649 * We could operate in DMA mode 1 if we knew the size of the tranfer
650 * in advance. For mass storage class, request->length = what the host
651 * sends, so that'd work. But for pretty much everything else,
652 * request->length is routinely more than what the host sends. For
653 * most these gadgets, end of is signified either by a short packet,
654 * or filling the last byte of the buffer. (Sending extra data in
655 * that last pckate should trigger an overflow fault.) But in mode 1,
656 * we don't get DMA completion interrrupt for short packets.
658 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
659 * to get endpoint interrupt on every DMA req, but that didn't seem
660 * to work reliably.
662 * REVISIT an updated g_file_storage can set req->short_not_ok, which
663 * then becomes usable as a runtime "use mode 1" hint...
666 csr |= MUSB_RXCSR_DMAENAB;
667 #ifdef USE_MODE1
668 csr |= MUSB_RXCSR_AUTOCLEAR;
669 /* csr |= MUSB_RXCSR_DMAMODE; */
671 /* this special sequence (enabling and then
672 * disabling MUSB_RXCSR_DMAMODE) is required
673 * to get DMAReq to activate
675 musb_writew(epio, MUSB_RXCSR,
676 csr | MUSB_RXCSR_DMAMODE);
677 #endif
678 musb_writew(epio, MUSB_RXCSR, csr);
680 if (request->actual < request->length) {
681 int transfer_size = 0;
682 #ifdef USE_MODE1
683 transfer_size = min(request->length,
684 channel->max_len);
685 #else
686 transfer_size = len;
687 #endif
688 if (transfer_size <= musb_ep->packet_sz)
689 musb_ep->dma->desired_mode = 0;
690 else
691 musb_ep->dma->desired_mode = 1;
693 use_dma = c->channel_program(
694 channel,
695 musb_ep->packet_sz,
696 channel->desired_mode,
697 request->dma
698 + request->actual,
699 transfer_size);
702 if (use_dma)
703 return;
705 #endif /* Mentor's DMA */
707 fifo_count = request->length - request->actual;
708 DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
709 musb_ep->end_point.name,
710 len, fifo_count,
711 musb_ep->packet_sz);
713 fifo_count = min_t(unsigned, len, fifo_count);
715 #ifdef CONFIG_USB_TUSB_OMAP_DMA
716 if (tusb_dma_omap() && musb_ep->dma) {
717 struct dma_controller *c = musb->dma_controller;
718 struct dma_channel *channel = musb_ep->dma;
719 u32 dma_addr = request->dma + request->actual;
720 int ret;
722 ret = c->channel_program(channel,
723 musb_ep->packet_sz,
724 channel->desired_mode,
725 dma_addr,
726 fifo_count);
727 if (ret)
728 return;
730 #endif
732 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
733 (request->buf + request->actual));
734 request->actual += fifo_count;
736 /* REVISIT if we left anything in the fifo, flush
737 * it and report -EOVERFLOW
740 /* ack the read! */
741 csr |= MUSB_RXCSR_P_WZC_BITS;
742 csr &= ~MUSB_RXCSR_RXPKTRDY;
743 musb_writew(epio, MUSB_RXCSR, csr);
747 /* reach the end or short packet detected */
748 if (request->actual == request->length || len < musb_ep->packet_sz)
749 musb_g_giveback(musb_ep, request, 0);
753 * Data ready for a request; called from IRQ
755 void musb_g_rx(struct musb *musb, u8 epnum)
757 u16 csr;
758 struct usb_request *request;
759 void __iomem *mbase = musb->mregs;
760 struct musb_ep *musb_ep;
761 void __iomem *epio = musb->endpoints[epnum].regs;
762 struct dma_channel *dma;
763 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
765 if (hw_ep->is_shared_fifo)
766 musb_ep = &hw_ep->ep_in;
767 else
768 musb_ep = &hw_ep->ep_out;
770 musb_ep_select(mbase, epnum);
772 request = next_request(musb_ep);
774 csr = musb_readw(epio, MUSB_RXCSR);
775 dma = is_dma_capable() ? musb_ep->dma : NULL;
777 DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
778 csr, dma ? " (dma)" : "", request);
780 if (csr & MUSB_RXCSR_P_SENTSTALL) {
781 csr |= MUSB_RXCSR_P_WZC_BITS;
782 csr &= ~MUSB_RXCSR_P_SENTSTALL;
783 musb_writew(epio, MUSB_RXCSR, csr);
784 return;
787 if (csr & MUSB_RXCSR_P_OVERRUN) {
788 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
789 csr &= ~MUSB_RXCSR_P_OVERRUN;
790 musb_writew(epio, MUSB_RXCSR, csr);
792 DBG(3, "%s iso overrun on %p\n", musb_ep->name, request);
793 if (request && request->status == -EINPROGRESS)
794 request->status = -EOVERFLOW;
796 if (csr & MUSB_RXCSR_INCOMPRX) {
797 /* REVISIT not necessarily an error */
798 DBG(4, "%s, incomprx\n", musb_ep->end_point.name);
801 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
802 /* "should not happen"; likely RXPKTRDY pending for DMA */
803 DBG((csr & MUSB_RXCSR_DMAENAB) ? 4 : 1,
804 "%s busy, csr %04x\n",
805 musb_ep->end_point.name, csr);
806 return;
809 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
810 csr &= ~(MUSB_RXCSR_AUTOCLEAR
811 | MUSB_RXCSR_DMAENAB
812 | MUSB_RXCSR_DMAMODE);
813 musb_writew(epio, MUSB_RXCSR,
814 MUSB_RXCSR_P_WZC_BITS | csr);
816 request->actual += musb_ep->dma->actual_len;
818 DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
819 epnum, csr,
820 musb_readw(epio, MUSB_RXCSR),
821 musb_ep->dma->actual_len, request);
823 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
824 /* Autoclear doesn't clear RxPktRdy for short packets */
825 if ((dma->desired_mode == 0)
826 || (dma->actual_len
827 & (musb_ep->packet_sz - 1))) {
828 /* ack the read! */
829 csr &= ~MUSB_RXCSR_RXPKTRDY;
830 musb_writew(epio, MUSB_RXCSR, csr);
833 /* incomplete, and not short? wait for next IN packet */
834 if ((request->actual < request->length)
835 && (musb_ep->dma->actual_len
836 == musb_ep->packet_sz))
837 return;
838 #endif
839 musb_g_giveback(musb_ep, request, 0);
841 request = next_request(musb_ep);
842 if (!request)
843 return;
846 /* analyze request if the ep is hot */
847 if (request)
848 rxstate(musb, to_musb_request(request));
849 else
850 DBG(3, "packet waiting for %s%s request\n",
851 musb_ep->desc ? "" : "inactive ",
852 musb_ep->end_point.name);
853 return;
856 /* ------------------------------------------------------------ */
858 static int musb_gadget_enable(struct usb_ep *ep,
859 const struct usb_endpoint_descriptor *desc)
861 unsigned long flags;
862 struct musb_ep *musb_ep;
863 struct musb_hw_ep *hw_ep;
864 void __iomem *regs;
865 struct musb *musb;
866 void __iomem *mbase;
867 u8 epnum;
868 u16 csr;
869 unsigned tmp;
870 int status = -EINVAL;
872 if (!ep || !desc)
873 return -EINVAL;
875 musb_ep = to_musb_ep(ep);
876 hw_ep = musb_ep->hw_ep;
877 regs = hw_ep->regs;
878 musb = musb_ep->musb;
879 mbase = musb->mregs;
880 epnum = musb_ep->current_epnum;
882 spin_lock_irqsave(&musb->lock, flags);
884 if (musb_ep->desc) {
885 status = -EBUSY;
886 goto fail;
888 musb_ep->type = usb_endpoint_type(desc);
890 /* check direction and (later) maxpacket size against endpoint */
891 if (usb_endpoint_num(desc) != epnum)
892 goto fail;
894 /* REVISIT this rules out high bandwidth periodic transfers */
895 tmp = le16_to_cpu(desc->wMaxPacketSize);
896 if (tmp & ~0x07ff)
897 goto fail;
898 musb_ep->packet_sz = tmp;
900 /* enable the interrupts for the endpoint, set the endpoint
901 * packet size (or fail), set the mode, clear the fifo
903 musb_ep_select(mbase, epnum);
904 if (usb_endpoint_dir_in(desc)) {
905 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
907 if (hw_ep->is_shared_fifo)
908 musb_ep->is_in = 1;
909 if (!musb_ep->is_in)
910 goto fail;
911 if (tmp > hw_ep->max_packet_sz_tx)
912 goto fail;
914 int_txe |= (1 << epnum);
915 musb_writew(mbase, MUSB_INTRTXE, int_txe);
917 /* REVISIT if can_bulk_split(), use by updating "tmp";
918 * likewise high bandwidth periodic tx
920 musb_writew(regs, MUSB_TXMAXP, tmp);
922 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
923 if (musb_readw(regs, MUSB_TXCSR)
924 & MUSB_TXCSR_FIFONOTEMPTY)
925 csr |= MUSB_TXCSR_FLUSHFIFO;
926 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
927 csr |= MUSB_TXCSR_P_ISO;
929 /* set twice in case of double buffering */
930 musb_writew(regs, MUSB_TXCSR, csr);
931 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
932 musb_writew(regs, MUSB_TXCSR, csr);
934 } else {
935 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
937 if (hw_ep->is_shared_fifo)
938 musb_ep->is_in = 0;
939 if (musb_ep->is_in)
940 goto fail;
941 if (tmp > hw_ep->max_packet_sz_rx)
942 goto fail;
944 int_rxe |= (1 << epnum);
945 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
947 /* REVISIT if can_bulk_combine() use by updating "tmp"
948 * likewise high bandwidth periodic rx
950 musb_writew(regs, MUSB_RXMAXP, tmp);
952 /* force shared fifo to OUT-only mode */
953 if (hw_ep->is_shared_fifo) {
954 csr = musb_readw(regs, MUSB_TXCSR);
955 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
956 musb_writew(regs, MUSB_TXCSR, csr);
959 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
960 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
961 csr |= MUSB_RXCSR_P_ISO;
962 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
963 csr |= MUSB_RXCSR_DISNYET;
965 /* set twice in case of double buffering */
966 musb_writew(regs, MUSB_RXCSR, csr);
967 musb_writew(regs, MUSB_RXCSR, csr);
970 /* NOTE: all the I/O code _should_ work fine without DMA, in case
971 * for some reason you run out of channels here.
973 if (is_dma_capable() && musb->dma_controller) {
974 struct dma_controller *c = musb->dma_controller;
976 musb_ep->dma = c->channel_alloc(c, hw_ep,
977 (desc->bEndpointAddress & USB_DIR_IN));
978 } else
979 musb_ep->dma = NULL;
981 musb_ep->desc = desc;
982 musb_ep->busy = 0;
983 status = 0;
985 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
986 musb_driver_name, musb_ep->end_point.name,
987 ({ char *s; switch (musb_ep->type) {
988 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
989 case USB_ENDPOINT_XFER_INT: s = "int"; break;
990 default: s = "iso"; break;
991 }; s; }),
992 musb_ep->is_in ? "IN" : "OUT",
993 musb_ep->dma ? "dma, " : "",
994 musb_ep->packet_sz);
996 schedule_work(&musb->irq_work);
998 fail:
999 spin_unlock_irqrestore(&musb->lock, flags);
1000 return status;
1004 * Disable an endpoint flushing all requests queued.
1006 static int musb_gadget_disable(struct usb_ep *ep)
1008 unsigned long flags;
1009 struct musb *musb;
1010 u8 epnum;
1011 struct musb_ep *musb_ep;
1012 void __iomem *epio;
1013 int status = 0;
1015 musb_ep = to_musb_ep(ep);
1016 musb = musb_ep->musb;
1017 epnum = musb_ep->current_epnum;
1018 epio = musb->endpoints[epnum].regs;
1020 spin_lock_irqsave(&musb->lock, flags);
1021 musb_ep_select(musb->mregs, epnum);
1023 /* zero the endpoint sizes */
1024 if (musb_ep->is_in) {
1025 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1026 int_txe &= ~(1 << epnum);
1027 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1028 musb_writew(epio, MUSB_TXMAXP, 0);
1029 } else {
1030 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1031 int_rxe &= ~(1 << epnum);
1032 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1033 musb_writew(epio, MUSB_RXMAXP, 0);
1036 musb_ep->desc = NULL;
1038 /* abort all pending DMA and requests */
1039 nuke(musb_ep, -ESHUTDOWN);
1041 schedule_work(&musb->irq_work);
1043 spin_unlock_irqrestore(&(musb->lock), flags);
1045 DBG(2, "%s\n", musb_ep->end_point.name);
1047 return status;
1051 * Allocate a request for an endpoint.
1052 * Reused by ep0 code.
1054 struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1056 struct musb_ep *musb_ep = to_musb_ep(ep);
1057 struct musb_request *request = NULL;
1059 request = kzalloc(sizeof *request, gfp_flags);
1060 if (request) {
1061 INIT_LIST_HEAD(&request->request.list);
1062 request->request.dma = DMA_ADDR_INVALID;
1063 request->epnum = musb_ep->current_epnum;
1064 request->ep = musb_ep;
1067 return &request->request;
1071 * Free a request
1072 * Reused by ep0 code.
1074 void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1076 kfree(to_musb_request(req));
1079 static LIST_HEAD(buffers);
1081 struct free_record {
1082 struct list_head list;
1083 struct device *dev;
1084 unsigned bytes;
1085 dma_addr_t dma;
1089 * Context: controller locked, IRQs blocked.
1091 void musb_ep_restart(struct musb *musb, struct musb_request *req)
1093 DBG(3, "<== %s request %p len %u on hw_ep%d\n",
1094 req->tx ? "TX/IN" : "RX/OUT",
1095 &req->request, req->request.length, req->epnum);
1097 musb_ep_select(musb->mregs, req->epnum);
1098 if (req->tx)
1099 txstate(musb, req);
1100 else
1101 rxstate(musb, req);
1104 static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1105 gfp_t gfp_flags)
1107 struct musb_ep *musb_ep;
1108 struct musb_request *request;
1109 struct musb *musb;
1110 int status = 0;
1111 unsigned long lockflags;
1113 if (!ep || !req)
1114 return -EINVAL;
1115 if (!req->buf)
1116 return -ENODATA;
1118 musb_ep = to_musb_ep(ep);
1119 musb = musb_ep->musb;
1121 request = to_musb_request(req);
1122 request->musb = musb;
1124 if (request->ep != musb_ep)
1125 return -EINVAL;
1127 DBG(4, "<== to %s request=%p\n", ep->name, req);
1129 /* request is mine now... */
1130 request->request.actual = 0;
1131 request->request.status = -EINPROGRESS;
1132 request->epnum = musb_ep->current_epnum;
1133 request->tx = musb_ep->is_in;
1135 if (is_dma_capable() && musb_ep->dma) {
1136 if (request->request.dma == DMA_ADDR_INVALID) {
1137 request->request.dma = dma_map_single(
1138 musb->controller,
1139 request->request.buf,
1140 request->request.length,
1141 request->tx
1142 ? DMA_TO_DEVICE
1143 : DMA_FROM_DEVICE);
1144 request->mapped = 1;
1145 } else {
1146 dma_sync_single_for_device(musb->controller,
1147 request->request.dma,
1148 request->request.length,
1149 request->tx
1150 ? DMA_TO_DEVICE
1151 : DMA_FROM_DEVICE);
1152 request->mapped = 0;
1154 } else if (!req->buf) {
1155 return -ENODATA;
1156 } else
1157 request->mapped = 0;
1159 spin_lock_irqsave(&musb->lock, lockflags);
1161 /* don't queue if the ep is down */
1162 if (!musb_ep->desc) {
1163 DBG(4, "req %p queued to %s while ep %s\n",
1164 req, ep->name, "disabled");
1165 status = -ESHUTDOWN;
1166 goto cleanup;
1169 /* add request to the list */
1170 list_add_tail(&(request->request.list), &(musb_ep->req_list));
1172 /* it this is the head of the queue, start i/o ... */
1173 if (!musb_ep->busy && &request->request.list == musb_ep->req_list.next)
1174 musb_ep_restart(musb, request);
1176 cleanup:
1177 spin_unlock_irqrestore(&musb->lock, lockflags);
1178 return status;
1181 static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1183 struct musb_ep *musb_ep = to_musb_ep(ep);
1184 struct usb_request *r;
1185 unsigned long flags;
1186 int status = 0;
1187 struct musb *musb = musb_ep->musb;
1189 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1190 return -EINVAL;
1192 spin_lock_irqsave(&musb->lock, flags);
1194 list_for_each_entry(r, &musb_ep->req_list, list) {
1195 if (r == request)
1196 break;
1198 if (r != request) {
1199 DBG(3, "request %p not queued to %s\n", request, ep->name);
1200 status = -EINVAL;
1201 goto done;
1204 /* if the hardware doesn't have the request, easy ... */
1205 if (musb_ep->req_list.next != &request->list || musb_ep->busy)
1206 musb_g_giveback(musb_ep, request, -ECONNRESET);
1208 /* ... else abort the dma transfer ... */
1209 else if (is_dma_capable() && musb_ep->dma) {
1210 struct dma_controller *c = musb->dma_controller;
1212 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1213 if (c->channel_abort)
1214 status = c->channel_abort(musb_ep->dma);
1215 else
1216 status = -EBUSY;
1217 if (status == 0)
1218 musb_g_giveback(musb_ep, request, -ECONNRESET);
1219 } else {
1220 /* NOTE: by sticking to easily tested hardware/driver states,
1221 * we leave counting of in-flight packets imprecise.
1223 musb_g_giveback(musb_ep, request, -ECONNRESET);
1226 done:
1227 spin_unlock_irqrestore(&musb->lock, flags);
1228 return status;
1232 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1233 * data but will queue requests.
1235 * exported to ep0 code
1237 int musb_gadget_set_halt(struct usb_ep *ep, int value)
1239 struct musb_ep *musb_ep = to_musb_ep(ep);
1240 u8 epnum = musb_ep->current_epnum;
1241 struct musb *musb = musb_ep->musb;
1242 void __iomem *epio = musb->endpoints[epnum].regs;
1243 void __iomem *mbase;
1244 unsigned long flags;
1245 u16 csr;
1246 struct musb_request *request;
1247 int status = 0;
1249 if (!ep)
1250 return -EINVAL;
1251 mbase = musb->mregs;
1253 spin_lock_irqsave(&musb->lock, flags);
1255 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1256 status = -EINVAL;
1257 goto done;
1260 musb_ep_select(mbase, epnum);
1262 request = to_musb_request(next_request(musb_ep));
1263 if (value) {
1264 if (request) {
1265 DBG(3, "request in progress, cannot halt %s\n",
1266 ep->name);
1267 status = -EAGAIN;
1268 goto done;
1270 /* Cannot portably stall with non-empty FIFO */
1271 if (musb_ep->is_in) {
1272 csr = musb_readw(epio, MUSB_TXCSR);
1273 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1274 DBG(3, "FIFO busy, cannot halt %s\n", ep->name);
1275 status = -EAGAIN;
1276 goto done;
1281 /* set/clear the stall and toggle bits */
1282 DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1283 if (musb_ep->is_in) {
1284 csr = musb_readw(epio, MUSB_TXCSR);
1285 csr |= MUSB_TXCSR_P_WZC_BITS
1286 | MUSB_TXCSR_CLRDATATOG;
1287 if (value)
1288 csr |= MUSB_TXCSR_P_SENDSTALL;
1289 else
1290 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1291 | MUSB_TXCSR_P_SENTSTALL);
1292 csr &= ~MUSB_TXCSR_TXPKTRDY;
1293 musb_writew(epio, MUSB_TXCSR, csr);
1294 } else {
1295 csr = musb_readw(epio, MUSB_RXCSR);
1296 csr |= MUSB_RXCSR_P_WZC_BITS
1297 | MUSB_RXCSR_FLUSHFIFO
1298 | MUSB_RXCSR_CLRDATATOG;
1299 if (value)
1300 csr |= MUSB_RXCSR_P_SENDSTALL;
1301 else
1302 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1303 | MUSB_RXCSR_P_SENTSTALL);
1304 musb_writew(epio, MUSB_RXCSR, csr);
1307 /* maybe start the first request in the queue */
1308 if (!musb_ep->busy && !value && request) {
1309 DBG(3, "restarting the request\n");
1310 musb_ep_restart(musb, request);
1313 done:
1314 spin_unlock_irqrestore(&musb->lock, flags);
1315 return status;
1318 static int musb_gadget_fifo_status(struct usb_ep *ep)
1320 struct musb_ep *musb_ep = to_musb_ep(ep);
1321 void __iomem *epio = musb_ep->hw_ep->regs;
1322 int retval = -EINVAL;
1324 if (musb_ep->desc && !musb_ep->is_in) {
1325 struct musb *musb = musb_ep->musb;
1326 int epnum = musb_ep->current_epnum;
1327 void __iomem *mbase = musb->mregs;
1328 unsigned long flags;
1330 spin_lock_irqsave(&musb->lock, flags);
1332 musb_ep_select(mbase, epnum);
1333 /* FIXME return zero unless RXPKTRDY is set */
1334 retval = musb_readw(epio, MUSB_RXCOUNT);
1336 spin_unlock_irqrestore(&musb->lock, flags);
1338 return retval;
1341 static void musb_gadget_fifo_flush(struct usb_ep *ep)
1343 struct musb_ep *musb_ep = to_musb_ep(ep);
1344 struct musb *musb = musb_ep->musb;
1345 u8 epnum = musb_ep->current_epnum;
1346 void __iomem *epio = musb->endpoints[epnum].regs;
1347 void __iomem *mbase;
1348 unsigned long flags;
1349 u16 csr, int_txe;
1351 mbase = musb->mregs;
1353 spin_lock_irqsave(&musb->lock, flags);
1354 musb_ep_select(mbase, (u8) epnum);
1356 /* disable interrupts */
1357 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1358 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1360 if (musb_ep->is_in) {
1361 csr = musb_readw(epio, MUSB_TXCSR);
1362 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1363 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1364 musb_writew(epio, MUSB_TXCSR, csr);
1365 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1366 musb_writew(epio, MUSB_TXCSR, csr);
1368 } else {
1369 csr = musb_readw(epio, MUSB_RXCSR);
1370 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1371 musb_writew(epio, MUSB_RXCSR, csr);
1372 musb_writew(epio, MUSB_RXCSR, csr);
1375 /* re-enable interrupt */
1376 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1377 spin_unlock_irqrestore(&musb->lock, flags);
1380 static const struct usb_ep_ops musb_ep_ops = {
1381 .enable = musb_gadget_enable,
1382 .disable = musb_gadget_disable,
1383 .alloc_request = musb_alloc_request,
1384 .free_request = musb_free_request,
1385 .queue = musb_gadget_queue,
1386 .dequeue = musb_gadget_dequeue,
1387 .set_halt = musb_gadget_set_halt,
1388 .fifo_status = musb_gadget_fifo_status,
1389 .fifo_flush = musb_gadget_fifo_flush
1392 /* ----------------------------------------------------------------------- */
1394 static int musb_gadget_get_frame(struct usb_gadget *gadget)
1396 struct musb *musb = gadget_to_musb(gadget);
1398 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1401 static int musb_gadget_wakeup(struct usb_gadget *gadget)
1403 struct musb *musb = gadget_to_musb(gadget);
1404 void __iomem *mregs = musb->mregs;
1405 unsigned long flags;
1406 int status = -EINVAL;
1407 u8 power, devctl;
1408 int retries;
1410 spin_lock_irqsave(&musb->lock, flags);
1412 switch (musb->xceiv->state) {
1413 case OTG_STATE_B_PERIPHERAL:
1414 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1415 * that's part of the standard usb 1.1 state machine, and
1416 * doesn't affect OTG transitions.
1418 if (musb->may_wakeup && musb->is_suspended)
1419 break;
1420 goto done;
1421 case OTG_STATE_B_IDLE:
1422 /* Start SRP ... OTG not required. */
1423 devctl = musb_readb(mregs, MUSB_DEVCTL);
1424 DBG(2, "Sending SRP: devctl: %02x\n", devctl);
1425 devctl |= MUSB_DEVCTL_SESSION;
1426 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1427 devctl = musb_readb(mregs, MUSB_DEVCTL);
1428 retries = 100;
1429 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1430 devctl = musb_readb(mregs, MUSB_DEVCTL);
1431 if (retries-- < 1)
1432 break;
1434 retries = 10000;
1435 while (devctl & MUSB_DEVCTL_SESSION) {
1436 devctl = musb_readb(mregs, MUSB_DEVCTL);
1437 if (retries-- < 1)
1438 break;
1441 /* Block idling for at least 1s */
1442 musb_platform_try_idle(musb,
1443 jiffies + msecs_to_jiffies(1 * HZ));
1445 status = 0;
1446 goto done;
1447 default:
1448 DBG(2, "Unhandled wake: %s\n", otg_state_string(musb));
1449 goto done;
1452 status = 0;
1454 power = musb_readb(mregs, MUSB_POWER);
1455 power |= MUSB_POWER_RESUME;
1456 musb_writeb(mregs, MUSB_POWER, power);
1457 DBG(2, "issue wakeup\n");
1459 /* FIXME do this next chunk in a timer callback, no udelay */
1460 mdelay(2);
1462 power = musb_readb(mregs, MUSB_POWER);
1463 power &= ~MUSB_POWER_RESUME;
1464 musb_writeb(mregs, MUSB_POWER, power);
1465 done:
1466 spin_unlock_irqrestore(&musb->lock, flags);
1467 return status;
1470 static int
1471 musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1473 struct musb *musb = gadget_to_musb(gadget);
1475 musb->is_self_powered = !!is_selfpowered;
1476 return 0;
1479 static void musb_pullup(struct musb *musb, int is_on)
1481 u8 power;
1483 power = musb_readb(musb->mregs, MUSB_POWER);
1484 if (is_on)
1485 power |= MUSB_POWER_SOFTCONN;
1486 else
1487 power &= ~MUSB_POWER_SOFTCONN;
1489 /* FIXME if on, HdrcStart; if off, HdrcStop */
1491 DBG(3, "gadget %s D+ pullup %s\n",
1492 musb->gadget_driver->function, is_on ? "on" : "off");
1493 musb_writeb(musb->mregs, MUSB_POWER, power);
1496 #if 0
1497 static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1499 DBG(2, "<= %s =>\n", __func__);
1502 * FIXME iff driver's softconnect flag is set (as it is during probe,
1503 * though that can clear it), just musb_pullup().
1506 return -EINVAL;
1508 #endif
1510 static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1512 struct musb *musb = gadget_to_musb(gadget);
1514 if (!musb->xceiv->set_power)
1515 return -EOPNOTSUPP;
1516 return otg_set_power(musb->xceiv, mA);
1519 static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1521 struct musb *musb = gadget_to_musb(gadget);
1522 unsigned long flags;
1524 is_on = !!is_on;
1526 /* NOTE: this assumes we are sensing vbus; we'd rather
1527 * not pullup unless the B-session is active.
1529 spin_lock_irqsave(&musb->lock, flags);
1530 if (is_on != musb->softconnect) {
1531 musb->softconnect = is_on;
1532 musb_pullup(musb, is_on);
1534 spin_unlock_irqrestore(&musb->lock, flags);
1535 return 0;
1538 static const struct usb_gadget_ops musb_gadget_operations = {
1539 .get_frame = musb_gadget_get_frame,
1540 .wakeup = musb_gadget_wakeup,
1541 .set_selfpowered = musb_gadget_set_self_powered,
1542 /* .vbus_session = musb_gadget_vbus_session, */
1543 .vbus_draw = musb_gadget_vbus_draw,
1544 .pullup = musb_gadget_pullup,
1547 /* ----------------------------------------------------------------------- */
1549 /* Registration */
1551 /* Only this registration code "knows" the rule (from USB standards)
1552 * about there being only one external upstream port. It assumes
1553 * all peripheral ports are external...
1555 static struct musb *the_gadget;
1557 static void musb_gadget_release(struct device *dev)
1559 /* kref_put(WHAT) */
1560 dev_dbg(dev, "%s\n", __func__);
1564 static void __init
1565 init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1567 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1569 memset(ep, 0, sizeof *ep);
1571 ep->current_epnum = epnum;
1572 ep->musb = musb;
1573 ep->hw_ep = hw_ep;
1574 ep->is_in = is_in;
1576 INIT_LIST_HEAD(&ep->req_list);
1578 sprintf(ep->name, "ep%d%s", epnum,
1579 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1580 is_in ? "in" : "out"));
1581 ep->end_point.name = ep->name;
1582 INIT_LIST_HEAD(&ep->end_point.ep_list);
1583 if (!epnum) {
1584 ep->end_point.maxpacket = 64;
1585 ep->end_point.ops = &musb_g_ep0_ops;
1586 musb->g.ep0 = &ep->end_point;
1587 } else {
1588 if (is_in)
1589 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1590 else
1591 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1592 ep->end_point.ops = &musb_ep_ops;
1593 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1598 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1599 * to the rest of the driver state.
1601 static inline void __init musb_g_init_endpoints(struct musb *musb)
1603 u8 epnum;
1604 struct musb_hw_ep *hw_ep;
1605 unsigned count = 0;
1607 /* intialize endpoint list just once */
1608 INIT_LIST_HEAD(&(musb->g.ep_list));
1610 for (epnum = 0, hw_ep = musb->endpoints;
1611 epnum < musb->nr_endpoints;
1612 epnum++, hw_ep++) {
1613 if (hw_ep->is_shared_fifo /* || !epnum */) {
1614 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1615 count++;
1616 } else {
1617 if (hw_ep->max_packet_sz_tx) {
1618 init_peripheral_ep(musb, &hw_ep->ep_in,
1619 epnum, 1);
1620 count++;
1622 if (hw_ep->max_packet_sz_rx) {
1623 init_peripheral_ep(musb, &hw_ep->ep_out,
1624 epnum, 0);
1625 count++;
1631 /* called once during driver setup to initialize and link into
1632 * the driver model; memory is zeroed.
1634 int __init musb_gadget_setup(struct musb *musb)
1636 int status;
1638 /* REVISIT minor race: if (erroneously) setting up two
1639 * musb peripherals at the same time, only the bus lock
1640 * is probably held.
1642 if (the_gadget)
1643 return -EBUSY;
1644 the_gadget = musb;
1646 musb->g.ops = &musb_gadget_operations;
1647 musb->g.is_dualspeed = 1;
1648 musb->g.speed = USB_SPEED_UNKNOWN;
1650 /* this "gadget" abstracts/virtualizes the controller */
1651 dev_set_name(&musb->g.dev, "gadget");
1652 musb->g.dev.parent = musb->controller;
1653 musb->g.dev.dma_mask = musb->controller->dma_mask;
1654 musb->g.dev.release = musb_gadget_release;
1655 musb->g.name = musb_driver_name;
1657 if (is_otg_enabled(musb))
1658 musb->g.is_otg = 1;
1660 musb_g_init_endpoints(musb);
1662 musb->is_active = 0;
1663 musb_platform_try_idle(musb, 0);
1665 status = device_register(&musb->g.dev);
1666 if (status != 0)
1667 the_gadget = NULL;
1668 return status;
1671 void musb_gadget_cleanup(struct musb *musb)
1673 if (musb != the_gadget)
1674 return;
1676 device_unregister(&musb->g.dev);
1677 the_gadget = NULL;
1681 * Register the gadget driver. Used by gadget drivers when
1682 * registering themselves with the controller.
1684 * -EINVAL something went wrong (not driver)
1685 * -EBUSY another gadget is already using the controller
1686 * -ENOMEM no memeory to perform the operation
1688 * @param driver the gadget driver
1689 * @return <0 if error, 0 if everything is fine
1691 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1693 int retval;
1694 unsigned long flags;
1695 struct musb *musb = the_gadget;
1697 if (!driver
1698 || driver->speed != USB_SPEED_HIGH
1699 || !driver->bind
1700 || !driver->setup)
1701 return -EINVAL;
1703 /* driver must be initialized to support peripheral mode */
1704 if (!musb || !(musb->board_mode == MUSB_OTG
1705 || musb->board_mode != MUSB_OTG)) {
1706 DBG(1, "%s, no dev??\n", __func__);
1707 return -ENODEV;
1710 DBG(3, "registering driver %s\n", driver->function);
1711 spin_lock_irqsave(&musb->lock, flags);
1713 if (musb->gadget_driver) {
1714 DBG(1, "%s is already bound to %s\n",
1715 musb_driver_name,
1716 musb->gadget_driver->driver.name);
1717 retval = -EBUSY;
1718 } else {
1719 musb->gadget_driver = driver;
1720 musb->g.dev.driver = &driver->driver;
1721 driver->driver.bus = NULL;
1722 musb->softconnect = 1;
1723 retval = 0;
1726 spin_unlock_irqrestore(&musb->lock, flags);
1728 if (retval == 0) {
1729 retval = driver->bind(&musb->g);
1730 if (retval != 0) {
1731 DBG(3, "bind to driver %s failed --> %d\n",
1732 driver->driver.name, retval);
1733 musb->gadget_driver = NULL;
1734 musb->g.dev.driver = NULL;
1737 spin_lock_irqsave(&musb->lock, flags);
1739 otg_set_peripheral(musb->xceiv, &musb->g);
1740 musb->is_active = 1;
1742 /* FIXME this ignores the softconnect flag. Drivers are
1743 * allowed hold the peripheral inactive until for example
1744 * userspace hooks up printer hardware or DSP codecs, so
1745 * hosts only see fully functional devices.
1748 if (!is_otg_enabled(musb))
1749 musb_start(musb);
1751 otg_set_peripheral(musb->xceiv, &musb->g);
1753 spin_unlock_irqrestore(&musb->lock, flags);
1755 if (is_otg_enabled(musb)) {
1756 DBG(3, "OTG startup...\n");
1758 /* REVISIT: funcall to other code, which also
1759 * handles power budgeting ... this way also
1760 * ensures HdrcStart is indirectly called.
1762 retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
1763 if (retval < 0) {
1764 DBG(1, "add_hcd failed, %d\n", retval);
1765 spin_lock_irqsave(&musb->lock, flags);
1766 otg_set_peripheral(musb->xceiv, NULL);
1767 musb->gadget_driver = NULL;
1768 musb->g.dev.driver = NULL;
1769 spin_unlock_irqrestore(&musb->lock, flags);
1774 return retval;
1776 EXPORT_SYMBOL(usb_gadget_register_driver);
1778 static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1780 int i;
1781 struct musb_hw_ep *hw_ep;
1783 /* don't disconnect if it's not connected */
1784 if (musb->g.speed == USB_SPEED_UNKNOWN)
1785 driver = NULL;
1786 else
1787 musb->g.speed = USB_SPEED_UNKNOWN;
1789 /* deactivate the hardware */
1790 if (musb->softconnect) {
1791 musb->softconnect = 0;
1792 musb_pullup(musb, 0);
1794 musb_stop(musb);
1796 /* killing any outstanding requests will quiesce the driver;
1797 * then report disconnect
1799 if (driver) {
1800 for (i = 0, hw_ep = musb->endpoints;
1801 i < musb->nr_endpoints;
1802 i++, hw_ep++) {
1803 musb_ep_select(musb->mregs, i);
1804 if (hw_ep->is_shared_fifo /* || !epnum */) {
1805 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1806 } else {
1807 if (hw_ep->max_packet_sz_tx)
1808 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1809 if (hw_ep->max_packet_sz_rx)
1810 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1814 spin_unlock(&musb->lock);
1815 driver->disconnect(&musb->g);
1816 spin_lock(&musb->lock);
1821 * Unregister the gadget driver. Used by gadget drivers when
1822 * unregistering themselves from the controller.
1824 * @param driver the gadget driver to unregister
1826 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1828 unsigned long flags;
1829 int retval = 0;
1830 struct musb *musb = the_gadget;
1832 if (!driver || !driver->unbind || !musb)
1833 return -EINVAL;
1835 /* REVISIT always use otg_set_peripheral() here too;
1836 * this needs to shut down the OTG engine.
1839 spin_lock_irqsave(&musb->lock, flags);
1841 #ifdef CONFIG_USB_MUSB_OTG
1842 musb_hnp_stop(musb);
1843 #endif
1845 if (musb->gadget_driver == driver) {
1847 (void) musb_gadget_vbus_draw(&musb->g, 0);
1849 musb->xceiv->state = OTG_STATE_UNDEFINED;
1850 stop_activity(musb, driver);
1851 otg_set_peripheral(musb->xceiv, NULL);
1853 DBG(3, "unregistering driver %s\n", driver->function);
1854 spin_unlock_irqrestore(&musb->lock, flags);
1855 driver->unbind(&musb->g);
1856 spin_lock_irqsave(&musb->lock, flags);
1858 musb->gadget_driver = NULL;
1859 musb->g.dev.driver = NULL;
1861 musb->is_active = 0;
1862 musb_platform_try_idle(musb, 0);
1863 } else
1864 retval = -EINVAL;
1865 spin_unlock_irqrestore(&musb->lock, flags);
1867 if (is_otg_enabled(musb) && retval == 0) {
1868 usb_remove_hcd(musb_to_hcd(musb));
1869 /* FIXME we need to be able to register another
1870 * gadget driver here and have everything work;
1871 * that currently misbehaves.
1875 return retval;
1877 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1880 /* ----------------------------------------------------------------------- */
1882 /* lifecycle operations called through plat_uds.c */
1884 void musb_g_resume(struct musb *musb)
1886 musb->is_suspended = 0;
1887 switch (musb->xceiv->state) {
1888 case OTG_STATE_B_IDLE:
1889 break;
1890 case OTG_STATE_B_WAIT_ACON:
1891 case OTG_STATE_B_PERIPHERAL:
1892 musb->is_active = 1;
1893 if (musb->gadget_driver && musb->gadget_driver->resume) {
1894 spin_unlock(&musb->lock);
1895 musb->gadget_driver->resume(&musb->g);
1896 spin_lock(&musb->lock);
1898 break;
1899 default:
1900 WARNING("unhandled RESUME transition (%s)\n",
1901 otg_state_string(musb));
1905 /* called when SOF packets stop for 3+ msec */
1906 void musb_g_suspend(struct musb *musb)
1908 u8 devctl;
1910 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1911 DBG(3, "devctl %02x\n", devctl);
1913 switch (musb->xceiv->state) {
1914 case OTG_STATE_B_IDLE:
1915 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
1916 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
1917 break;
1918 case OTG_STATE_B_PERIPHERAL:
1919 musb->is_suspended = 1;
1920 if (musb->gadget_driver && musb->gadget_driver->suspend) {
1921 spin_unlock(&musb->lock);
1922 musb->gadget_driver->suspend(&musb->g);
1923 spin_lock(&musb->lock);
1925 break;
1926 default:
1927 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
1928 * A_PERIPHERAL may need care too
1930 WARNING("unhandled SUSPEND transition (%s)\n",
1931 otg_state_string(musb));
1935 /* Called during SRP */
1936 void musb_g_wakeup(struct musb *musb)
1938 musb_gadget_wakeup(&musb->g);
1941 /* called when VBUS drops below session threshold, and in other cases */
1942 void musb_g_disconnect(struct musb *musb)
1944 void __iomem *mregs = musb->mregs;
1945 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
1947 DBG(3, "devctl %02x\n", devctl);
1949 /* clear HR */
1950 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
1952 /* don't draw vbus until new b-default session */
1953 (void) musb_gadget_vbus_draw(&musb->g, 0);
1955 musb->g.speed = USB_SPEED_UNKNOWN;
1956 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
1957 spin_unlock(&musb->lock);
1958 musb->gadget_driver->disconnect(&musb->g);
1959 spin_lock(&musb->lock);
1962 switch (musb->xceiv->state) {
1963 default:
1964 #ifdef CONFIG_USB_MUSB_OTG
1965 DBG(2, "Unhandled disconnect %s, setting a_idle\n",
1966 otg_state_string(musb));
1967 musb->xceiv->state = OTG_STATE_A_IDLE;
1968 MUSB_HST_MODE(musb);
1969 break;
1970 case OTG_STATE_A_PERIPHERAL:
1971 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
1972 MUSB_HST_MODE(musb);
1973 break;
1974 case OTG_STATE_B_WAIT_ACON:
1975 case OTG_STATE_B_HOST:
1976 #endif
1977 case OTG_STATE_B_PERIPHERAL:
1978 case OTG_STATE_B_IDLE:
1979 musb->xceiv->state = OTG_STATE_B_IDLE;
1980 break;
1981 case OTG_STATE_B_SRP_INIT:
1982 break;
1985 musb->is_active = 0;
1988 void musb_g_reset(struct musb *musb)
1989 __releases(musb->lock)
1990 __acquires(musb->lock)
1992 void __iomem *mbase = musb->mregs;
1993 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
1994 u8 power;
1996 DBG(3, "<== %s addr=%x driver '%s'\n",
1997 (devctl & MUSB_DEVCTL_BDEVICE)
1998 ? "B-Device" : "A-Device",
1999 musb_readb(mbase, MUSB_FADDR),
2000 musb->gadget_driver
2001 ? musb->gadget_driver->driver.name
2002 : NULL
2005 /* report disconnect, if we didn't already (flushing EP state) */
2006 if (musb->g.speed != USB_SPEED_UNKNOWN)
2007 musb_g_disconnect(musb);
2009 /* clear HR */
2010 else if (devctl & MUSB_DEVCTL_HR)
2011 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2014 /* what speed did we negotiate? */
2015 power = musb_readb(mbase, MUSB_POWER);
2016 musb->g.speed = (power & MUSB_POWER_HSMODE)
2017 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2019 /* start in USB_STATE_DEFAULT */
2020 musb->is_active = 1;
2021 musb->is_suspended = 0;
2022 MUSB_DEV_MODE(musb);
2023 musb->address = 0;
2024 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2026 musb->may_wakeup = 0;
2027 musb->g.b_hnp_enable = 0;
2028 musb->g.a_alt_hnp_support = 0;
2029 musb->g.a_hnp_support = 0;
2031 /* Normal reset, as B-Device;
2032 * or else after HNP, as A-Device
2034 if (devctl & MUSB_DEVCTL_BDEVICE) {
2035 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2036 musb->g.is_a_peripheral = 0;
2037 } else if (is_otg_enabled(musb)) {
2038 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2039 musb->g.is_a_peripheral = 1;
2040 } else
2041 WARN_ON(1);
2043 /* start with default limits on VBUS power draw */
2044 (void) musb_gadget_vbus_draw(&musb->g,
2045 is_otg_enabled(musb) ? 8 : 100);