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
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
25 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/timer.h>
38 #include <linux/module.h>
39 #include <linux/smp.h>
40 #include <linux/spinlock.h>
41 #include <linux/delay.h>
42 #include <linux/moduleparam.h>
43 #include <linux/stat.h>
44 #include <linux/dma-mapping.h>
46 #include "musb_core.h"
49 /* MUSB PERIPHERAL status 3-mar-2006:
51 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
54 * + remote wakeup to Linux hosts work, but saw USBCV failures;
55 * in one test run (operator error?)
56 * + endpoint halt tests -- in both usbtest and usbcv -- seem
57 * to break when dma is enabled ... is something wrongly
60 * - Mass storage behaved ok when last tested. Network traffic patterns
61 * (with lots of short transfers etc) need retesting; they turn up the
62 * worst cases of the DMA, since short packets are typical but are not
66 * + both pio and dma behave in with network and g_zero tests
67 * + no cppi throughput issues other than no-hw-queueing
68 * + failed with FLAT_REG (DaVinci)
69 * + seems to behave with double buffering, PIO -and- CPPI
70 * + with gadgetfs + AIO, requests got lost?
73 * + both pio and dma behave in with network and g_zero tests
74 * + dma is slow in typical case (short_not_ok is clear)
75 * + double buffering ok with PIO
76 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
77 * + request lossage observed with gadgetfs
79 * - ISO not tested ... might work, but only weakly isochronous
81 * - Gadget driver disabling of softconnect during bind() is ignored; so
82 * drivers can't hold off host requests until userspace is ready.
83 * (Workaround: they can turn it off later.)
85 * - PORTABILITY (assumes PIO works):
86 * + DaVinci, basically works with cppi dma
87 * + OMAP 2430, ditto with mentor dma
88 * + TUSB 6010, platform-specific dma in the works
91 /* ----------------------------------------------------------------------- */
94 * Immediately complete a request.
96 * @param request the request to complete
97 * @param status the status to complete the request with
98 * Context: controller locked, IRQs blocked.
100 void musb_g_giveback(
102 struct usb_request
*request
,
104 __releases(ep
->musb
->lock
)
105 __acquires(ep
->musb
->lock
)
107 struct musb_request
*req
;
111 req
= to_musb_request(request
);
113 list_del(&request
->list
);
114 if (req
->request
.status
== -EINPROGRESS
)
115 req
->request
.status
= status
;
119 spin_unlock(&musb
->lock
);
120 if (is_dma_capable()) {
122 dma_unmap_single(musb
->controller
,
128 req
->request
.dma
= DMA_ADDR_INVALID
;
130 } else if (req
->request
.dma
!= DMA_ADDR_INVALID
)
131 dma_sync_single_for_cpu(musb
->controller
,
138 if (request
->status
== 0)
139 DBG(5, "%s done request %p, %d/%d\n",
140 ep
->end_point
.name
, request
,
141 req
->request
.actual
, req
->request
.length
);
143 DBG(2, "%s request %p, %d/%d fault %d\n",
144 ep
->end_point
.name
, request
,
145 req
->request
.actual
, req
->request
.length
,
147 req
->request
.complete(&req
->ep
->end_point
, &req
->request
);
148 spin_lock(&musb
->lock
);
152 /* ----------------------------------------------------------------------- */
155 * Abort requests queued to an endpoint using the status. Synchronous.
156 * caller locked controller and blocked irqs, and selected this ep.
158 static void nuke(struct musb_ep
*ep
, const int status
)
160 struct musb_request
*req
= NULL
;
161 void __iomem
*epio
= ep
->musb
->endpoints
[ep
->current_epnum
].regs
;
165 if (is_dma_capable() && ep
->dma
) {
166 struct dma_controller
*c
= ep
->musb
->dma_controller
;
171 * The programming guide says that we must not clear
172 * the DMAMODE bit before DMAENAB, so we only
173 * clear it in the second write...
175 musb_writew(epio
, MUSB_TXCSR
,
176 MUSB_TXCSR_DMAMODE
| MUSB_TXCSR_FLUSHFIFO
);
177 musb_writew(epio
, MUSB_TXCSR
,
178 0 | MUSB_TXCSR_FLUSHFIFO
);
180 musb_writew(epio
, MUSB_RXCSR
,
181 0 | MUSB_RXCSR_FLUSHFIFO
);
182 musb_writew(epio
, MUSB_RXCSR
,
183 0 | MUSB_RXCSR_FLUSHFIFO
);
186 value
= c
->channel_abort(ep
->dma
);
187 DBG(value
? 1 : 6, "%s: abort DMA --> %d\n", ep
->name
, value
);
188 c
->channel_release(ep
->dma
);
192 while (!list_empty(&(ep
->req_list
))) {
193 req
= container_of(ep
->req_list
.next
, struct musb_request
,
195 musb_g_giveback(ep
, &req
->request
, status
);
199 /* ----------------------------------------------------------------------- */
201 /* Data transfers - pure PIO, pure DMA, or mixed mode */
204 * This assumes the separate CPPI engine is responding to DMA requests
205 * from the usb core ... sequenced a bit differently from mentor dma.
208 static inline int max_ep_writesize(struct musb
*musb
, struct musb_ep
*ep
)
210 if (can_bulk_split(musb
, ep
->type
))
211 return ep
->hw_ep
->max_packet_sz_tx
;
213 return ep
->packet_sz
;
217 #ifdef CONFIG_USB_INVENTRA_DMA
219 /* Peripheral tx (IN) using Mentor DMA works as follows:
220 Only mode 0 is used for transfers <= wPktSize,
221 mode 1 is used for larger transfers,
223 One of the following happens:
224 - Host sends IN token which causes an endpoint interrupt
226 -> if DMA is currently busy, exit.
227 -> if queue is non-empty, txstate().
229 - Request is queued by the gadget driver.
230 -> if queue was previously empty, txstate()
235 | (data is transferred to the FIFO, then sent out when
236 | IN token(s) are recd from Host.
237 | -> DMA interrupt on completion
239 | -> stop DMA, ~DMAENAB,
240 | -> set TxPktRdy for last short pkt or zlp
241 | -> Complete Request
242 | -> Continue next request (call txstate)
243 |___________________________________|
245 * Non-Mentor DMA engines can of course work differently, such as by
246 * upleveling from irq-per-packet to irq-per-buffer.
252 * An endpoint is transmitting data. This can be called either from
253 * the IRQ routine or from ep.queue() to kickstart a request on an
256 * Context: controller locked, IRQs blocked, endpoint selected
258 static void txstate(struct musb
*musb
, struct musb_request
*req
)
260 u8 epnum
= req
->epnum
;
261 struct musb_ep
*musb_ep
;
262 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
263 struct usb_request
*request
;
264 u16 fifo_count
= 0, csr
;
269 /* we shouldn't get here while DMA is active ... but we do ... */
270 if (dma_channel_status(musb_ep
->dma
) == MUSB_DMA_STATUS_BUSY
) {
271 DBG(4, "dma pending...\n");
275 /* read TXCSR before */
276 csr
= musb_readw(epio
, MUSB_TXCSR
);
278 request
= &req
->request
;
279 fifo_count
= min(max_ep_writesize(musb
, musb_ep
),
280 (int)(request
->length
- request
->actual
));
282 if (csr
& MUSB_TXCSR_TXPKTRDY
) {
283 DBG(5, "%s old packet still ready , txcsr %03x\n",
284 musb_ep
->end_point
.name
, csr
);
288 if (csr
& MUSB_TXCSR_P_SENDSTALL
) {
289 DBG(5, "%s stalling, txcsr %03x\n",
290 musb_ep
->end_point
.name
, csr
);
294 DBG(4, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
295 epnum
, musb_ep
->packet_sz
, fifo_count
,
298 #ifndef CONFIG_MUSB_PIO_ONLY
299 if (is_dma_capable() && musb_ep
->dma
) {
300 struct dma_controller
*c
= musb
->dma_controller
;
302 use_dma
= (request
->dma
!= DMA_ADDR_INVALID
);
304 /* MUSB_TXCSR_P_ISO is still set correctly */
306 #ifdef CONFIG_USB_INVENTRA_DMA
310 /* setup DMA, then program endpoint CSR */
311 request_size
= min(request
->length
,
312 musb_ep
->dma
->max_len
);
313 if (request_size
<= musb_ep
->packet_sz
)
314 musb_ep
->dma
->desired_mode
= 0;
316 musb_ep
->dma
->desired_mode
= 1;
318 use_dma
= use_dma
&& c
->channel_program(
319 musb_ep
->dma
, musb_ep
->packet_sz
,
320 musb_ep
->dma
->desired_mode
,
321 request
->dma
, request_size
);
323 if (musb_ep
->dma
->desired_mode
== 0) {
325 * We must not clear the DMAMODE bit
326 * before the DMAENAB bit -- and the
327 * latter doesn't always get cleared
328 * before we get here...
330 csr
&= ~(MUSB_TXCSR_AUTOSET
331 | MUSB_TXCSR_DMAENAB
);
332 musb_writew(epio
, MUSB_TXCSR
, csr
333 | MUSB_TXCSR_P_WZC_BITS
);
334 csr
&= ~MUSB_TXCSR_DMAMODE
;
335 csr
|= (MUSB_TXCSR_DMAENAB
|
337 /* against programming guide */
339 csr
|= (MUSB_TXCSR_AUTOSET
344 csr
&= ~MUSB_TXCSR_P_UNDERRUN
;
345 musb_writew(epio
, MUSB_TXCSR
, csr
);
349 #elif defined(CONFIG_USB_TI_CPPI_DMA)
350 /* program endpoint CSR first, then setup DMA */
351 csr
&= ~(MUSB_TXCSR_P_UNDERRUN
| MUSB_TXCSR_TXPKTRDY
);
352 csr
|= MUSB_TXCSR_MODE
| MUSB_TXCSR_DMAENAB
;
353 musb_writew(epio
, MUSB_TXCSR
,
354 (MUSB_TXCSR_P_WZC_BITS
& ~MUSB_TXCSR_P_UNDERRUN
)
357 /* ensure writebuffer is empty */
358 csr
= musb_readw(epio
, MUSB_TXCSR
);
360 /* NOTE host side sets DMAENAB later than this; both are
361 * OK since the transfer dma glue (between CPPI and Mentor
362 * fifos) just tells CPPI it could start. Data only moves
363 * to the USB TX fifo when both fifos are ready.
366 /* "mode" is irrelevant here; handle terminating ZLPs like
367 * PIO does, since the hardware RNDIS mode seems unreliable
368 * except for the last-packet-is-already-short case.
370 use_dma
= use_dma
&& c
->channel_program(
371 musb_ep
->dma
, musb_ep
->packet_sz
,
376 c
->channel_release(musb_ep
->dma
);
378 csr
&= ~MUSB_TXCSR_DMAENAB
;
379 musb_writew(epio
, MUSB_TXCSR
, csr
);
380 /* invariant: prequest->buf is non-null */
382 #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
383 use_dma
= use_dma
&& c
->channel_program(
384 musb_ep
->dma
, musb_ep
->packet_sz
,
393 musb_write_fifo(musb_ep
->hw_ep
, fifo_count
,
394 (u8
*) (request
->buf
+ request
->actual
));
395 request
->actual
+= fifo_count
;
396 csr
|= MUSB_TXCSR_TXPKTRDY
;
397 csr
&= ~MUSB_TXCSR_P_UNDERRUN
;
398 musb_writew(epio
, MUSB_TXCSR
, csr
);
401 /* host may already have the data when this message shows... */
402 DBG(3, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
403 musb_ep
->end_point
.name
, use_dma
? "dma" : "pio",
404 request
->actual
, request
->length
,
405 musb_readw(epio
, MUSB_TXCSR
),
407 musb_readw(epio
, MUSB_TXMAXP
));
411 * FIFO state update (e.g. data ready).
412 * Called from IRQ, with controller locked.
414 void musb_g_tx(struct musb
*musb
, u8 epnum
)
417 struct usb_request
*request
;
418 u8 __iomem
*mbase
= musb
->mregs
;
419 struct musb_ep
*musb_ep
= &musb
->endpoints
[epnum
].ep_in
;
420 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
421 struct dma_channel
*dma
;
423 musb_ep_select(mbase
, epnum
);
424 request
= next_request(musb_ep
);
426 csr
= musb_readw(epio
, MUSB_TXCSR
);
427 DBG(4, "<== %s, txcsr %04x\n", musb_ep
->end_point
.name
, csr
);
429 dma
= is_dma_capable() ? musb_ep
->dma
: NULL
;
431 /* REVISIT for high bandwidth, MUSB_TXCSR_P_INCOMPTX
432 * probably rates reporting as a host error
434 if (csr
& MUSB_TXCSR_P_SENTSTALL
) {
435 csr
|= MUSB_TXCSR_P_WZC_BITS
;
436 csr
&= ~MUSB_TXCSR_P_SENTSTALL
;
437 musb_writew(epio
, MUSB_TXCSR
, csr
);
438 if (dma_channel_status(dma
) == MUSB_DMA_STATUS_BUSY
) {
439 dma
->status
= MUSB_DMA_STATUS_CORE_ABORT
;
440 musb
->dma_controller
->channel_abort(dma
);
444 musb_g_giveback(musb_ep
, request
, -EPIPE
);
449 if (csr
& MUSB_TXCSR_P_UNDERRUN
) {
450 /* we NAKed, no big deal ... little reason to care */
451 csr
|= MUSB_TXCSR_P_WZC_BITS
;
452 csr
&= ~(MUSB_TXCSR_P_UNDERRUN
453 | MUSB_TXCSR_TXPKTRDY
);
454 musb_writew(epio
, MUSB_TXCSR
, csr
);
455 DBG(20, "underrun on ep%d, req %p\n", epnum
, request
);
458 if (dma_channel_status(dma
) == MUSB_DMA_STATUS_BUSY
) {
459 /* SHOULD NOT HAPPEN ... has with cppi though, after
460 * changing SENDSTALL (and other cases); harmless?
462 DBG(5, "%s dma still busy?\n", musb_ep
->end_point
.name
);
469 if (dma
&& (csr
& MUSB_TXCSR_DMAENAB
)) {
471 csr
|= MUSB_TXCSR_P_WZC_BITS
;
472 csr
&= ~(MUSB_TXCSR_DMAENAB
473 | MUSB_TXCSR_P_UNDERRUN
474 | MUSB_TXCSR_TXPKTRDY
);
475 musb_writew(epio
, MUSB_TXCSR
, csr
);
476 /* ensure writebuffer is empty */
477 csr
= musb_readw(epio
, MUSB_TXCSR
);
478 request
->actual
+= musb_ep
->dma
->actual_len
;
479 DBG(4, "TXCSR%d %04x, dma off, "
482 musb_ep
->dma
->actual_len
,
486 if (is_dma
|| request
->actual
== request
->length
) {
488 /* First, maybe a terminating short packet.
489 * Some DMA engines might handle this by
495 % musb_ep
->packet_sz
)
497 #ifdef CONFIG_USB_INVENTRA_DMA
499 ((!dma
->desired_mode
) ||
501 (musb_ep
->packet_sz
- 1))))
504 /* on dma completion, fifo may not
505 * be available yet ...
507 if (csr
& MUSB_TXCSR_TXPKTRDY
)
510 DBG(4, "sending zero pkt\n");
511 musb_writew(epio
, MUSB_TXCSR
,
513 | MUSB_TXCSR_TXPKTRDY
);
517 /* ... or if not, then complete it */
518 musb_g_giveback(musb_ep
, request
, 0);
520 /* kickstart next transfer if appropriate;
521 * the packet that just completed might not
522 * be transmitted for hours or days.
523 * REVISIT for double buffering...
524 * FIXME revisit for stalls too...
526 musb_ep_select(mbase
, epnum
);
527 csr
= musb_readw(epio
, MUSB_TXCSR
);
528 if (csr
& MUSB_TXCSR_FIFONOTEMPTY
)
530 request
= musb_ep
->desc
531 ? next_request(musb_ep
)
534 DBG(4, "%s idle now\n",
535 musb_ep
->end_point
.name
);
540 txstate(musb
, to_musb_request(request
));
546 /* ------------------------------------------------------------ */
548 #ifdef CONFIG_USB_INVENTRA_DMA
550 /* Peripheral rx (OUT) using Mentor DMA works as follows:
551 - Only mode 0 is used.
553 - Request is queued by the gadget class driver.
554 -> if queue was previously empty, rxstate()
556 - Host sends OUT token which causes an endpoint interrupt
558 | -> if request queued, call rxstate
560 | | -> DMA interrupt on completion
564 | | -> if data recd = max expected
565 | | by the request, or host
566 | | sent a short packet,
567 | | complete the request,
568 | | and start the next one.
569 | |_____________________________________|
570 | else just wait for the host
571 | to send the next OUT token.
572 |__________________________________________________|
574 * Non-Mentor DMA engines can of course work differently.
580 * Context: controller locked, IRQs blocked, endpoint selected
582 static void rxstate(struct musb
*musb
, struct musb_request
*req
)
585 const u8 epnum
= req
->epnum
;
586 struct usb_request
*request
= &req
->request
;
587 struct musb_ep
*musb_ep
= &musb
->endpoints
[epnum
].ep_out
;
588 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
589 unsigned fifo_count
= 0;
590 u16 len
= musb_ep
->packet_sz
;
592 csr
= musb_readw(epio
, MUSB_RXCSR
);
594 if (is_cppi_enabled() && musb_ep
->dma
) {
595 struct dma_controller
*c
= musb
->dma_controller
;
596 struct dma_channel
*channel
= musb_ep
->dma
;
598 /* NOTE: CPPI won't actually stop advancing the DMA
599 * queue after short packet transfers, so this is almost
600 * always going to run as IRQ-per-packet DMA so that
601 * faults will be handled correctly.
603 if (c
->channel_program(channel
,
605 !request
->short_not_ok
,
606 request
->dma
+ request
->actual
,
607 request
->length
- request
->actual
)) {
609 /* make sure that if an rxpkt arrived after the irq,
610 * the cppi engine will be ready to take it as soon
613 csr
&= ~(MUSB_RXCSR_AUTOCLEAR
614 | MUSB_RXCSR_DMAMODE
);
615 csr
|= MUSB_RXCSR_DMAENAB
| MUSB_RXCSR_P_WZC_BITS
;
616 musb_writew(epio
, MUSB_RXCSR
, csr
);
621 if (csr
& MUSB_RXCSR_RXPKTRDY
) {
622 len
= musb_readw(epio
, MUSB_RXCOUNT
);
623 if (request
->actual
< request
->length
) {
624 #ifdef CONFIG_USB_INVENTRA_DMA
625 if (is_dma_capable() && musb_ep
->dma
) {
626 struct dma_controller
*c
;
627 struct dma_channel
*channel
;
630 c
= musb
->dma_controller
;
631 channel
= musb_ep
->dma
;
633 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
634 * mode 0 only. So we do not get endpoint interrupts due to DMA
635 * completion. We only get interrupts from DMA controller.
637 * We could operate in DMA mode 1 if we knew the size of the tranfer
638 * in advance. For mass storage class, request->length = what the host
639 * sends, so that'd work. But for pretty much everything else,
640 * request->length is routinely more than what the host sends. For
641 * most these gadgets, end of is signified either by a short packet,
642 * or filling the last byte of the buffer. (Sending extra data in
643 * that last pckate should trigger an overflow fault.) But in mode 1,
644 * we don't get DMA completion interrrupt for short packets.
646 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
647 * to get endpoint interrupt on every DMA req, but that didn't seem
650 * REVISIT an updated g_file_storage can set req->short_not_ok, which
651 * then becomes usable as a runtime "use mode 1" hint...
654 csr
|= MUSB_RXCSR_DMAENAB
;
656 csr
|= MUSB_RXCSR_AUTOCLEAR
;
657 /* csr |= MUSB_RXCSR_DMAMODE; */
659 /* this special sequence (enabling and then
660 * disabling MUSB_RXCSR_DMAMODE) is required
661 * to get DMAReq to activate
663 musb_writew(epio
, MUSB_RXCSR
,
664 csr
| MUSB_RXCSR_DMAMODE
);
666 musb_writew(epio
, MUSB_RXCSR
, csr
);
668 if (request
->actual
< request
->length
) {
669 int transfer_size
= 0;
671 transfer_size
= min(request
->length
,
676 if (transfer_size
<= musb_ep
->packet_sz
)
677 musb_ep
->dma
->desired_mode
= 0;
679 musb_ep
->dma
->desired_mode
= 1;
681 use_dma
= c
->channel_program(
684 channel
->desired_mode
,
693 #endif /* Mentor's DMA */
695 fifo_count
= request
->length
- request
->actual
;
696 DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
697 musb_ep
->end_point
.name
,
701 fifo_count
= min_t(unsigned, len
, fifo_count
);
703 #ifdef CONFIG_USB_TUSB_OMAP_DMA
704 if (tusb_dma_omap() && musb_ep
->dma
) {
705 struct dma_controller
*c
= musb
->dma_controller
;
706 struct dma_channel
*channel
= musb_ep
->dma
;
707 u32 dma_addr
= request
->dma
+ request
->actual
;
710 ret
= c
->channel_program(channel
,
712 channel
->desired_mode
,
720 musb_read_fifo(musb_ep
->hw_ep
, fifo_count
, (u8
*)
721 (request
->buf
+ request
->actual
));
722 request
->actual
+= fifo_count
;
724 /* REVISIT if we left anything in the fifo, flush
725 * it and report -EOVERFLOW
729 csr
|= MUSB_RXCSR_P_WZC_BITS
;
730 csr
&= ~MUSB_RXCSR_RXPKTRDY
;
731 musb_writew(epio
, MUSB_RXCSR
, csr
);
735 /* reach the end or short packet detected */
736 if (request
->actual
== request
->length
|| len
< musb_ep
->packet_sz
)
737 musb_g_giveback(musb_ep
, request
, 0);
741 * Data ready for a request; called from IRQ
743 void musb_g_rx(struct musb
*musb
, u8 epnum
)
746 struct usb_request
*request
;
747 void __iomem
*mbase
= musb
->mregs
;
748 struct musb_ep
*musb_ep
= &musb
->endpoints
[epnum
].ep_out
;
749 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
750 struct dma_channel
*dma
;
752 musb_ep_select(mbase
, epnum
);
754 request
= next_request(musb_ep
);
756 csr
= musb_readw(epio
, MUSB_RXCSR
);
757 dma
= is_dma_capable() ? musb_ep
->dma
: NULL
;
759 DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep
->end_point
.name
,
760 csr
, dma
? " (dma)" : "", request
);
762 if (csr
& MUSB_RXCSR_P_SENTSTALL
) {
763 if (dma_channel_status(dma
) == MUSB_DMA_STATUS_BUSY
) {
764 dma
->status
= MUSB_DMA_STATUS_CORE_ABORT
;
765 (void) musb
->dma_controller
->channel_abort(dma
);
766 request
->actual
+= musb_ep
->dma
->actual_len
;
769 csr
|= MUSB_RXCSR_P_WZC_BITS
;
770 csr
&= ~MUSB_RXCSR_P_SENTSTALL
;
771 musb_writew(epio
, MUSB_RXCSR
, csr
);
774 musb_g_giveback(musb_ep
, request
, -EPIPE
);
778 if (csr
& MUSB_RXCSR_P_OVERRUN
) {
779 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
780 csr
&= ~MUSB_RXCSR_P_OVERRUN
;
781 musb_writew(epio
, MUSB_RXCSR
, csr
);
783 DBG(3, "%s iso overrun on %p\n", musb_ep
->name
, request
);
784 if (request
&& request
->status
== -EINPROGRESS
)
785 request
->status
= -EOVERFLOW
;
787 if (csr
& MUSB_RXCSR_INCOMPRX
) {
788 /* REVISIT not necessarily an error */
789 DBG(4, "%s, incomprx\n", musb_ep
->end_point
.name
);
792 if (dma_channel_status(dma
) == MUSB_DMA_STATUS_BUSY
) {
793 /* "should not happen"; likely RXPKTRDY pending for DMA */
794 DBG((csr
& MUSB_RXCSR_DMAENAB
) ? 4 : 1,
795 "%s busy, csr %04x\n",
796 musb_ep
->end_point
.name
, csr
);
800 if (dma
&& (csr
& MUSB_RXCSR_DMAENAB
)) {
801 csr
&= ~(MUSB_RXCSR_AUTOCLEAR
803 | MUSB_RXCSR_DMAMODE
);
804 musb_writew(epio
, MUSB_RXCSR
,
805 MUSB_RXCSR_P_WZC_BITS
| csr
);
807 request
->actual
+= musb_ep
->dma
->actual_len
;
809 DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
811 musb_readw(epio
, MUSB_RXCSR
),
812 musb_ep
->dma
->actual_len
, request
);
814 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
815 /* Autoclear doesn't clear RxPktRdy for short packets */
816 if ((dma
->desired_mode
== 0)
818 & (musb_ep
->packet_sz
- 1))) {
820 csr
&= ~MUSB_RXCSR_RXPKTRDY
;
821 musb_writew(epio
, MUSB_RXCSR
, csr
);
824 /* incomplete, and not short? wait for next IN packet */
825 if ((request
->actual
< request
->length
)
826 && (musb_ep
->dma
->actual_len
827 == musb_ep
->packet_sz
))
830 musb_g_giveback(musb_ep
, request
, 0);
832 request
= next_request(musb_ep
);
836 /* don't start more i/o till the stall clears */
837 musb_ep_select(mbase
, epnum
);
838 csr
= musb_readw(epio
, MUSB_RXCSR
);
839 if (csr
& MUSB_RXCSR_P_SENDSTALL
)
844 /* analyze request if the ep is hot */
846 rxstate(musb
, to_musb_request(request
));
848 DBG(3, "packet waiting for %s%s request\n",
849 musb_ep
->desc
? "" : "inactive ",
850 musb_ep
->end_point
.name
);
856 /* ------------------------------------------------------------ */
858 static int musb_gadget_enable(struct usb_ep
*ep
,
859 const struct usb_endpoint_descriptor
*desc
)
862 struct musb_ep
*musb_ep
;
863 struct musb_hw_ep
*hw_ep
;
870 int status
= -EINVAL
;
875 musb_ep
= to_musb_ep(ep
);
876 hw_ep
= musb_ep
->hw_ep
;
878 musb
= musb_ep
->musb
;
880 epnum
= musb_ep
->current_epnum
;
882 spin_lock_irqsave(&musb
->lock
, flags
);
888 musb_ep
->type
= usb_endpoint_type(desc
);
890 /* check direction and (later) maxpacket size against endpoint */
891 if (usb_endpoint_num(desc
) != epnum
)
894 /* REVISIT this rules out high bandwidth periodic transfers */
895 tmp
= le16_to_cpu(desc
->wMaxPacketSize
);
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
)
911 if (tmp
> hw_ep
->max_packet_sz_tx
)
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
);
935 u16 int_rxe
= musb_readw(mbase
, MUSB_INTRRXE
);
937 if (hw_ep
->is_shared_fifo
)
941 if (tmp
> hw_ep
->max_packet_sz_rx
)
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
));
981 musb_ep
->desc
= desc
;
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;
992 musb_ep
->is_in
? "IN" : "OUT",
993 musb_ep
->dma
? "dma, " : "",
996 schedule_work(&musb
->irq_work
);
999 spin_unlock_irqrestore(&musb
->lock
, flags
);
1004 * Disable an endpoint flushing all requests queued.
1006 static int musb_gadget_disable(struct usb_ep
*ep
)
1008 unsigned long flags
;
1011 struct musb_ep
*musb_ep
;
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);
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
);
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
);
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
;
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
;
1089 * Context: controller locked, IRQs blocked.
1091 static 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
);
1104 static int musb_gadget_queue(struct usb_ep
*ep
, struct usb_request
*req
,
1107 struct musb_ep
*musb_ep
;
1108 struct musb_request
*request
;
1111 unsigned long lockflags
;
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
)
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(
1139 request
->request
.buf
,
1140 request
->request
.length
,
1144 request
->mapped
= 1;
1146 dma_sync_single_for_device(musb
->controller
,
1147 request
->request
.dma
,
1148 request
->request
.length
,
1152 request
->mapped
= 0;
1154 } else if (!req
->buf
) {
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
;
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
);
1177 spin_unlock_irqrestore(&musb
->lock
, lockflags
);
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
;
1187 struct musb
*musb
= musb_ep
->musb
;
1189 if (!ep
|| !request
|| to_musb_request(request
)->ep
!= musb_ep
)
1192 spin_lock_irqsave(&musb
->lock
, flags
);
1194 list_for_each_entry(r
, &musb_ep
->req_list
, list
) {
1199 DBG(3, "request %p not queued to %s\n", request
, ep
->name
);
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
);
1218 musb_g_giveback(musb_ep
, request
, -ECONNRESET
);
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
);
1227 spin_unlock_irqrestore(&musb
->lock
, flags
);
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
;
1246 struct musb_request
*request
= NULL
;
1251 mbase
= musb
->mregs
;
1253 spin_lock_irqsave(&musb
->lock
, flags
);
1255 if ((USB_ENDPOINT_XFER_ISOC
== musb_ep
->type
)) {
1260 musb_ep_select(mbase
, epnum
);
1262 /* cannot portably stall with non-empty FIFO */
1263 request
= to_musb_request(next_request(musb_ep
));
1264 if (value
&& musb_ep
->is_in
) {
1265 csr
= musb_readw(epio
, MUSB_TXCSR
);
1266 if (csr
& MUSB_TXCSR_FIFONOTEMPTY
) {
1267 DBG(3, "%s fifo busy, cannot halt\n", ep
->name
);
1268 spin_unlock_irqrestore(&musb
->lock
, flags
);
1274 /* set/clear the stall and toggle bits */
1275 DBG(2, "%s: %s stall\n", ep
->name
, value
? "set" : "clear");
1276 if (musb_ep
->is_in
) {
1277 csr
= musb_readw(epio
, MUSB_TXCSR
);
1278 if (csr
& MUSB_TXCSR_FIFONOTEMPTY
)
1279 csr
|= MUSB_TXCSR_FLUSHFIFO
;
1280 csr
|= MUSB_TXCSR_P_WZC_BITS
1281 | MUSB_TXCSR_CLRDATATOG
;
1283 csr
|= MUSB_TXCSR_P_SENDSTALL
;
1285 csr
&= ~(MUSB_TXCSR_P_SENDSTALL
1286 | MUSB_TXCSR_P_SENTSTALL
);
1287 csr
&= ~MUSB_TXCSR_TXPKTRDY
;
1288 musb_writew(epio
, MUSB_TXCSR
, csr
);
1290 csr
= musb_readw(epio
, MUSB_RXCSR
);
1291 csr
|= MUSB_RXCSR_P_WZC_BITS
1292 | MUSB_RXCSR_FLUSHFIFO
1293 | MUSB_RXCSR_CLRDATATOG
;
1295 csr
|= MUSB_RXCSR_P_SENDSTALL
;
1297 csr
&= ~(MUSB_RXCSR_P_SENDSTALL
1298 | MUSB_RXCSR_P_SENTSTALL
);
1299 musb_writew(epio
, MUSB_RXCSR
, csr
);
1304 /* maybe start the first request in the queue */
1305 if (!musb_ep
->busy
&& !value
&& request
) {
1306 DBG(3, "restarting the request\n");
1307 musb_ep_restart(musb
, request
);
1310 spin_unlock_irqrestore(&musb
->lock
, flags
);
1314 static int musb_gadget_fifo_status(struct usb_ep
*ep
)
1316 struct musb_ep
*musb_ep
= to_musb_ep(ep
);
1317 void __iomem
*epio
= musb_ep
->hw_ep
->regs
;
1318 int retval
= -EINVAL
;
1320 if (musb_ep
->desc
&& !musb_ep
->is_in
) {
1321 struct musb
*musb
= musb_ep
->musb
;
1322 int epnum
= musb_ep
->current_epnum
;
1323 void __iomem
*mbase
= musb
->mregs
;
1324 unsigned long flags
;
1326 spin_lock_irqsave(&musb
->lock
, flags
);
1328 musb_ep_select(mbase
, epnum
);
1329 /* FIXME return zero unless RXPKTRDY is set */
1330 retval
= musb_readw(epio
, MUSB_RXCOUNT
);
1332 spin_unlock_irqrestore(&musb
->lock
, flags
);
1337 static void musb_gadget_fifo_flush(struct usb_ep
*ep
)
1339 struct musb_ep
*musb_ep
= to_musb_ep(ep
);
1340 struct musb
*musb
= musb_ep
->musb
;
1341 u8 epnum
= musb_ep
->current_epnum
;
1342 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
1343 void __iomem
*mbase
;
1344 unsigned long flags
;
1347 mbase
= musb
->mregs
;
1349 spin_lock_irqsave(&musb
->lock
, flags
);
1350 musb_ep_select(mbase
, (u8
) epnum
);
1352 /* disable interrupts */
1353 int_txe
= musb_readw(mbase
, MUSB_INTRTXE
);
1354 musb_writew(mbase
, MUSB_INTRTXE
, int_txe
& ~(1 << epnum
));
1356 if (musb_ep
->is_in
) {
1357 csr
= musb_readw(epio
, MUSB_TXCSR
);
1358 if (csr
& MUSB_TXCSR_FIFONOTEMPTY
) {
1359 csr
|= MUSB_TXCSR_FLUSHFIFO
| MUSB_TXCSR_P_WZC_BITS
;
1360 musb_writew(epio
, MUSB_TXCSR
, csr
);
1361 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1362 musb_writew(epio
, MUSB_TXCSR
, csr
);
1365 csr
= musb_readw(epio
, MUSB_RXCSR
);
1366 csr
|= MUSB_RXCSR_FLUSHFIFO
| MUSB_RXCSR_P_WZC_BITS
;
1367 musb_writew(epio
, MUSB_RXCSR
, csr
);
1368 musb_writew(epio
, MUSB_RXCSR
, csr
);
1371 /* re-enable interrupt */
1372 musb_writew(mbase
, MUSB_INTRTXE
, int_txe
);
1373 spin_unlock_irqrestore(&musb
->lock
, flags
);
1376 static const struct usb_ep_ops musb_ep_ops
= {
1377 .enable
= musb_gadget_enable
,
1378 .disable
= musb_gadget_disable
,
1379 .alloc_request
= musb_alloc_request
,
1380 .free_request
= musb_free_request
,
1381 .queue
= musb_gadget_queue
,
1382 .dequeue
= musb_gadget_dequeue
,
1383 .set_halt
= musb_gadget_set_halt
,
1384 .fifo_status
= musb_gadget_fifo_status
,
1385 .fifo_flush
= musb_gadget_fifo_flush
1388 /* ----------------------------------------------------------------------- */
1390 static int musb_gadget_get_frame(struct usb_gadget
*gadget
)
1392 struct musb
*musb
= gadget_to_musb(gadget
);
1394 return (int)musb_readw(musb
->mregs
, MUSB_FRAME
);
1397 static int musb_gadget_wakeup(struct usb_gadget
*gadget
)
1399 struct musb
*musb
= gadget_to_musb(gadget
);
1400 void __iomem
*mregs
= musb
->mregs
;
1401 unsigned long flags
;
1402 int status
= -EINVAL
;
1406 spin_lock_irqsave(&musb
->lock
, flags
);
1408 switch (musb
->xceiv
.state
) {
1409 case OTG_STATE_B_PERIPHERAL
:
1410 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1411 * that's part of the standard usb 1.1 state machine, and
1412 * doesn't affect OTG transitions.
1414 if (musb
->may_wakeup
&& musb
->is_suspended
)
1417 case OTG_STATE_B_IDLE
:
1418 /* Start SRP ... OTG not required. */
1419 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1420 DBG(2, "Sending SRP: devctl: %02x\n", devctl
);
1421 devctl
|= MUSB_DEVCTL_SESSION
;
1422 musb_writeb(mregs
, MUSB_DEVCTL
, devctl
);
1423 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1425 while (!(devctl
& MUSB_DEVCTL_SESSION
)) {
1426 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1431 while (devctl
& MUSB_DEVCTL_SESSION
) {
1432 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1437 /* Block idling for at least 1s */
1438 musb_platform_try_idle(musb
,
1439 jiffies
+ msecs_to_jiffies(1 * HZ
));
1444 DBG(2, "Unhandled wake: %s\n", otg_state_string(musb
));
1450 power
= musb_readb(mregs
, MUSB_POWER
);
1451 power
|= MUSB_POWER_RESUME
;
1452 musb_writeb(mregs
, MUSB_POWER
, power
);
1453 DBG(2, "issue wakeup\n");
1455 /* FIXME do this next chunk in a timer callback, no udelay */
1458 power
= musb_readb(mregs
, MUSB_POWER
);
1459 power
&= ~MUSB_POWER_RESUME
;
1460 musb_writeb(mregs
, MUSB_POWER
, power
);
1462 spin_unlock_irqrestore(&musb
->lock
, flags
);
1467 musb_gadget_set_self_powered(struct usb_gadget
*gadget
, int is_selfpowered
)
1469 struct musb
*musb
= gadget_to_musb(gadget
);
1471 musb
->is_self_powered
= !!is_selfpowered
;
1475 static void musb_pullup(struct musb
*musb
, int is_on
)
1479 power
= musb_readb(musb
->mregs
, MUSB_POWER
);
1481 power
|= MUSB_POWER_SOFTCONN
;
1483 power
&= ~MUSB_POWER_SOFTCONN
;
1485 /* FIXME if on, HdrcStart; if off, HdrcStop */
1487 DBG(3, "gadget %s D+ pullup %s\n",
1488 musb
->gadget_driver
->function
, is_on
? "on" : "off");
1489 musb_writeb(musb
->mregs
, MUSB_POWER
, power
);
1493 static int musb_gadget_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1495 DBG(2, "<= %s =>\n", __func__
);
1498 * FIXME iff driver's softconnect flag is set (as it is during probe,
1499 * though that can clear it), just musb_pullup().
1506 static int musb_gadget_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1508 struct musb
*musb
= gadget_to_musb(gadget
);
1510 if (!musb
->xceiv
.set_power
)
1512 return otg_set_power(&musb
->xceiv
, mA
);
1515 static int musb_gadget_pullup(struct usb_gadget
*gadget
, int is_on
)
1517 struct musb
*musb
= gadget_to_musb(gadget
);
1518 unsigned long flags
;
1522 /* NOTE: this assumes we are sensing vbus; we'd rather
1523 * not pullup unless the B-session is active.
1525 spin_lock_irqsave(&musb
->lock
, flags
);
1526 if (is_on
!= musb
->softconnect
) {
1527 musb
->softconnect
= is_on
;
1528 musb_pullup(musb
, is_on
);
1530 spin_unlock_irqrestore(&musb
->lock
, flags
);
1534 static const struct usb_gadget_ops musb_gadget_operations
= {
1535 .get_frame
= musb_gadget_get_frame
,
1536 .wakeup
= musb_gadget_wakeup
,
1537 .set_selfpowered
= musb_gadget_set_self_powered
,
1538 /* .vbus_session = musb_gadget_vbus_session, */
1539 .vbus_draw
= musb_gadget_vbus_draw
,
1540 .pullup
= musb_gadget_pullup
,
1543 /* ----------------------------------------------------------------------- */
1547 /* Only this registration code "knows" the rule (from USB standards)
1548 * about there being only one external upstream port. It assumes
1549 * all peripheral ports are external...
1551 static struct musb
*the_gadget
;
1553 static void musb_gadget_release(struct device
*dev
)
1555 /* kref_put(WHAT) */
1556 dev_dbg(dev
, "%s\n", __func__
);
1561 init_peripheral_ep(struct musb
*musb
, struct musb_ep
*ep
, u8 epnum
, int is_in
)
1563 struct musb_hw_ep
*hw_ep
= musb
->endpoints
+ epnum
;
1565 memset(ep
, 0, sizeof *ep
);
1567 ep
->current_epnum
= epnum
;
1572 INIT_LIST_HEAD(&ep
->req_list
);
1574 sprintf(ep
->name
, "ep%d%s", epnum
,
1575 (!epnum
|| hw_ep
->is_shared_fifo
) ? "" : (
1576 is_in
? "in" : "out"));
1577 ep
->end_point
.name
= ep
->name
;
1578 INIT_LIST_HEAD(&ep
->end_point
.ep_list
);
1580 ep
->end_point
.maxpacket
= 64;
1581 ep
->end_point
.ops
= &musb_g_ep0_ops
;
1582 musb
->g
.ep0
= &ep
->end_point
;
1585 ep
->end_point
.maxpacket
= hw_ep
->max_packet_sz_tx
;
1587 ep
->end_point
.maxpacket
= hw_ep
->max_packet_sz_rx
;
1588 ep
->end_point
.ops
= &musb_ep_ops
;
1589 list_add_tail(&ep
->end_point
.ep_list
, &musb
->g
.ep_list
);
1594 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1595 * to the rest of the driver state.
1597 static inline void __init
musb_g_init_endpoints(struct musb
*musb
)
1600 struct musb_hw_ep
*hw_ep
;
1603 /* intialize endpoint list just once */
1604 INIT_LIST_HEAD(&(musb
->g
.ep_list
));
1606 for (epnum
= 0, hw_ep
= musb
->endpoints
;
1607 epnum
< musb
->nr_endpoints
;
1609 if (hw_ep
->is_shared_fifo
/* || !epnum */) {
1610 init_peripheral_ep(musb
, &hw_ep
->ep_in
, epnum
, 0);
1613 if (hw_ep
->max_packet_sz_tx
) {
1614 init_peripheral_ep(musb
, &hw_ep
->ep_in
,
1618 if (hw_ep
->max_packet_sz_rx
) {
1619 init_peripheral_ep(musb
, &hw_ep
->ep_out
,
1627 /* called once during driver setup to initialize and link into
1628 * the driver model; memory is zeroed.
1630 int __init
musb_gadget_setup(struct musb
*musb
)
1634 /* REVISIT minor race: if (erroneously) setting up two
1635 * musb peripherals at the same time, only the bus lock
1642 musb
->g
.ops
= &musb_gadget_operations
;
1643 musb
->g
.is_dualspeed
= 1;
1644 musb
->g
.speed
= USB_SPEED_UNKNOWN
;
1646 /* this "gadget" abstracts/virtualizes the controller */
1647 dev_set_name(&musb
->g
.dev
, "gadget");
1648 musb
->g
.dev
.parent
= musb
->controller
;
1649 musb
->g
.dev
.dma_mask
= musb
->controller
->dma_mask
;
1650 musb
->g
.dev
.release
= musb_gadget_release
;
1651 musb
->g
.name
= musb_driver_name
;
1653 if (is_otg_enabled(musb
))
1656 musb_g_init_endpoints(musb
);
1658 musb
->is_active
= 0;
1659 musb_platform_try_idle(musb
, 0);
1661 status
= device_register(&musb
->g
.dev
);
1667 void musb_gadget_cleanup(struct musb
*musb
)
1669 if (musb
!= the_gadget
)
1672 device_unregister(&musb
->g
.dev
);
1677 * Register the gadget driver. Used by gadget drivers when
1678 * registering themselves with the controller.
1680 * -EINVAL something went wrong (not driver)
1681 * -EBUSY another gadget is already using the controller
1682 * -ENOMEM no memeory to perform the operation
1684 * @param driver the gadget driver
1685 * @return <0 if error, 0 if everything is fine
1687 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1690 unsigned long flags
;
1691 struct musb
*musb
= the_gadget
;
1694 || driver
->speed
!= USB_SPEED_HIGH
1699 /* driver must be initialized to support peripheral mode */
1700 if (!musb
|| !(musb
->board_mode
== MUSB_OTG
1701 || musb
->board_mode
!= MUSB_OTG
)) {
1702 DBG(1, "%s, no dev??\n", __func__
);
1706 DBG(3, "registering driver %s\n", driver
->function
);
1707 spin_lock_irqsave(&musb
->lock
, flags
);
1709 if (musb
->gadget_driver
) {
1710 DBG(1, "%s is already bound to %s\n",
1712 musb
->gadget_driver
->driver
.name
);
1715 musb
->gadget_driver
= driver
;
1716 musb
->g
.dev
.driver
= &driver
->driver
;
1717 driver
->driver
.bus
= NULL
;
1718 musb
->softconnect
= 1;
1722 spin_unlock_irqrestore(&musb
->lock
, flags
);
1725 retval
= driver
->bind(&musb
->g
);
1727 DBG(3, "bind to driver %s failed --> %d\n",
1728 driver
->driver
.name
, retval
);
1729 musb
->gadget_driver
= NULL
;
1730 musb
->g
.dev
.driver
= NULL
;
1733 spin_lock_irqsave(&musb
->lock
, flags
);
1735 /* REVISIT always use otg_set_peripheral(), handling
1736 * issues including the root hub one below ...
1738 musb
->xceiv
.gadget
= &musb
->g
;
1739 musb
->xceiv
.state
= OTG_STATE_B_IDLE
;
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
))
1751 spin_unlock_irqrestore(&musb
->lock
, flags
);
1753 if (is_otg_enabled(musb
)) {
1754 DBG(3, "OTG startup...\n");
1756 /* REVISIT: funcall to other code, which also
1757 * handles power budgeting ... this way also
1758 * ensures HdrcStart is indirectly called.
1760 retval
= usb_add_hcd(musb_to_hcd(musb
), -1, 0);
1762 DBG(1, "add_hcd failed, %d\n", retval
);
1763 spin_lock_irqsave(&musb
->lock
, flags
);
1764 musb
->xceiv
.gadget
= NULL
;
1765 musb
->xceiv
.state
= OTG_STATE_UNDEFINED
;
1766 musb
->gadget_driver
= NULL
;
1767 musb
->g
.dev
.driver
= NULL
;
1768 spin_unlock_irqrestore(&musb
->lock
, flags
);
1775 EXPORT_SYMBOL(usb_gadget_register_driver
);
1777 static void stop_activity(struct musb
*musb
, struct usb_gadget_driver
*driver
)
1780 struct musb_hw_ep
*hw_ep
;
1782 /* don't disconnect if it's not connected */
1783 if (musb
->g
.speed
== USB_SPEED_UNKNOWN
)
1786 musb
->g
.speed
= USB_SPEED_UNKNOWN
;
1788 /* deactivate the hardware */
1789 if (musb
->softconnect
) {
1790 musb
->softconnect
= 0;
1791 musb_pullup(musb
, 0);
1795 /* killing any outstanding requests will quiesce the driver;
1796 * then report disconnect
1799 for (i
= 0, hw_ep
= musb
->endpoints
;
1800 i
< musb
->nr_endpoints
;
1802 musb_ep_select(musb
->mregs
, i
);
1803 if (hw_ep
->is_shared_fifo
/* || !epnum */) {
1804 nuke(&hw_ep
->ep_in
, -ESHUTDOWN
);
1806 if (hw_ep
->max_packet_sz_tx
)
1807 nuke(&hw_ep
->ep_in
, -ESHUTDOWN
);
1808 if (hw_ep
->max_packet_sz_rx
)
1809 nuke(&hw_ep
->ep_out
, -ESHUTDOWN
);
1813 spin_unlock(&musb
->lock
);
1814 driver
->disconnect(&musb
->g
);
1815 spin_lock(&musb
->lock
);
1820 * Unregister the gadget driver. Used by gadget drivers when
1821 * unregistering themselves from the controller.
1823 * @param driver the gadget driver to unregister
1825 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1827 unsigned long flags
;
1829 struct musb
*musb
= the_gadget
;
1831 if (!driver
|| !driver
->unbind
|| !musb
)
1834 /* REVISIT always use otg_set_peripheral() here too;
1835 * this needs to shut down the OTG engine.
1838 spin_lock_irqsave(&musb
->lock
, flags
);
1840 #ifdef CONFIG_USB_MUSB_OTG
1841 musb_hnp_stop(musb
);
1844 if (musb
->gadget_driver
== driver
) {
1846 (void) musb_gadget_vbus_draw(&musb
->g
, 0);
1848 musb
->xceiv
.state
= OTG_STATE_UNDEFINED
;
1849 stop_activity(musb
, driver
);
1851 DBG(3, "unregistering driver %s\n", driver
->function
);
1852 spin_unlock_irqrestore(&musb
->lock
, flags
);
1853 driver
->unbind(&musb
->g
);
1854 spin_lock_irqsave(&musb
->lock
, flags
);
1856 musb
->gadget_driver
= NULL
;
1857 musb
->g
.dev
.driver
= NULL
;
1859 musb
->is_active
= 0;
1860 musb_platform_try_idle(musb
, 0);
1863 spin_unlock_irqrestore(&musb
->lock
, flags
);
1865 if (is_otg_enabled(musb
) && retval
== 0) {
1866 usb_remove_hcd(musb_to_hcd(musb
));
1867 /* FIXME we need to be able to register another
1868 * gadget driver here and have everything work;
1869 * that currently misbehaves.
1875 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1878 /* ----------------------------------------------------------------------- */
1880 /* lifecycle operations called through plat_uds.c */
1882 void musb_g_resume(struct musb
*musb
)
1884 musb
->is_suspended
= 0;
1885 switch (musb
->xceiv
.state
) {
1886 case OTG_STATE_B_IDLE
:
1888 case OTG_STATE_B_WAIT_ACON
:
1889 case OTG_STATE_B_PERIPHERAL
:
1890 musb
->is_active
= 1;
1891 if (musb
->gadget_driver
&& musb
->gadget_driver
->resume
) {
1892 spin_unlock(&musb
->lock
);
1893 musb
->gadget_driver
->resume(&musb
->g
);
1894 spin_lock(&musb
->lock
);
1898 WARNING("unhandled RESUME transition (%s)\n",
1899 otg_state_string(musb
));
1903 /* called when SOF packets stop for 3+ msec */
1904 void musb_g_suspend(struct musb
*musb
)
1908 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
1909 DBG(3, "devctl %02x\n", devctl
);
1911 switch (musb
->xceiv
.state
) {
1912 case OTG_STATE_B_IDLE
:
1913 if ((devctl
& MUSB_DEVCTL_VBUS
) == MUSB_DEVCTL_VBUS
)
1914 musb
->xceiv
.state
= OTG_STATE_B_PERIPHERAL
;
1916 case OTG_STATE_B_PERIPHERAL
:
1917 musb
->is_suspended
= 1;
1918 if (musb
->gadget_driver
&& musb
->gadget_driver
->suspend
) {
1919 spin_unlock(&musb
->lock
);
1920 musb
->gadget_driver
->suspend(&musb
->g
);
1921 spin_lock(&musb
->lock
);
1925 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
1926 * A_PERIPHERAL may need care too
1928 WARNING("unhandled SUSPEND transition (%s)\n",
1929 otg_state_string(musb
));
1933 /* Called during SRP */
1934 void musb_g_wakeup(struct musb
*musb
)
1936 musb_gadget_wakeup(&musb
->g
);
1939 /* called when VBUS drops below session threshold, and in other cases */
1940 void musb_g_disconnect(struct musb
*musb
)
1942 void __iomem
*mregs
= musb
->mregs
;
1943 u8 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1945 DBG(3, "devctl %02x\n", devctl
);
1948 musb_writeb(mregs
, MUSB_DEVCTL
, devctl
& MUSB_DEVCTL_SESSION
);
1950 /* don't draw vbus until new b-default session */
1951 (void) musb_gadget_vbus_draw(&musb
->g
, 0);
1953 musb
->g
.speed
= USB_SPEED_UNKNOWN
;
1954 if (musb
->gadget_driver
&& musb
->gadget_driver
->disconnect
) {
1955 spin_unlock(&musb
->lock
);
1956 musb
->gadget_driver
->disconnect(&musb
->g
);
1957 spin_lock(&musb
->lock
);
1960 switch (musb
->xceiv
.state
) {
1962 #ifdef CONFIG_USB_MUSB_OTG
1963 DBG(2, "Unhandled disconnect %s, setting a_idle\n",
1964 otg_state_string(musb
));
1965 musb
->xceiv
.state
= OTG_STATE_A_IDLE
;
1967 case OTG_STATE_A_PERIPHERAL
:
1968 musb
->xceiv
.state
= OTG_STATE_A_WAIT_VFALL
;
1970 case OTG_STATE_B_WAIT_ACON
:
1971 case OTG_STATE_B_HOST
:
1973 case OTG_STATE_B_PERIPHERAL
:
1974 case OTG_STATE_B_IDLE
:
1975 musb
->xceiv
.state
= OTG_STATE_B_IDLE
;
1977 case OTG_STATE_B_SRP_INIT
:
1981 musb
->is_active
= 0;
1984 void musb_g_reset(struct musb
*musb
)
1985 __releases(musb
->lock
)
1986 __acquires(musb
->lock
)
1988 void __iomem
*mbase
= musb
->mregs
;
1989 u8 devctl
= musb_readb(mbase
, MUSB_DEVCTL
);
1992 DBG(3, "<== %s addr=%x driver '%s'\n",
1993 (devctl
& MUSB_DEVCTL_BDEVICE
)
1994 ? "B-Device" : "A-Device",
1995 musb_readb(mbase
, MUSB_FADDR
),
1997 ? musb
->gadget_driver
->driver
.name
2001 /* report disconnect, if we didn't already (flushing EP state) */
2002 if (musb
->g
.speed
!= USB_SPEED_UNKNOWN
)
2003 musb_g_disconnect(musb
);
2006 else if (devctl
& MUSB_DEVCTL_HR
)
2007 musb_writeb(mbase
, MUSB_DEVCTL
, MUSB_DEVCTL_SESSION
);
2010 /* what speed did we negotiate? */
2011 power
= musb_readb(mbase
, MUSB_POWER
);
2012 musb
->g
.speed
= (power
& MUSB_POWER_HSMODE
)
2013 ? USB_SPEED_HIGH
: USB_SPEED_FULL
;
2015 /* start in USB_STATE_DEFAULT */
2016 musb
->is_active
= 1;
2017 musb
->is_suspended
= 0;
2018 MUSB_DEV_MODE(musb
);
2020 musb
->ep0_state
= MUSB_EP0_STAGE_SETUP
;
2022 musb
->may_wakeup
= 0;
2023 musb
->g
.b_hnp_enable
= 0;
2024 musb
->g
.a_alt_hnp_support
= 0;
2025 musb
->g
.a_hnp_support
= 0;
2027 /* Normal reset, as B-Device;
2028 * or else after HNP, as A-Device
2030 if (devctl
& MUSB_DEVCTL_BDEVICE
) {
2031 musb
->xceiv
.state
= OTG_STATE_B_PERIPHERAL
;
2032 musb
->g
.is_a_peripheral
= 0;
2033 } else if (is_otg_enabled(musb
)) {
2034 musb
->xceiv
.state
= OTG_STATE_A_PERIPHERAL
;
2035 musb
->g
.is_a_peripheral
= 1;
2039 /* start with default limits on VBUS power draw */
2040 (void) musb_gadget_vbus_draw(&musb
->g
,
2041 is_otg_enabled(musb
) ? 8 : 100);