usb: musb: Calling VBUS pulsing API when SRP is initiated.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / musb / musb_gadget.c
blob6f3cf4ce8bd6e374970640a007ee8dd0839b3638
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>
46 #include <linux/slab.h>
48 #include "musb_core.h"
51 /* MUSB PERIPHERAL status 3-mar-2006:
53 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
54 * Minor glitches:
56 * + remote wakeup to Linux hosts work, but saw USBCV failures;
57 * in one test run (operator error?)
58 * + endpoint halt tests -- in both usbtest and usbcv -- seem
59 * to break when dma is enabled ... is something wrongly
60 * clearing SENDSTALL?
62 * - Mass storage behaved ok when last tested. Network traffic patterns
63 * (with lots of short transfers etc) need retesting; they turn up the
64 * worst cases of the DMA, since short packets are typical but are not
65 * required.
67 * - TX/IN
68 * + both pio and dma behave in with network and g_zero tests
69 * + no cppi throughput issues other than no-hw-queueing
70 * + failed with FLAT_REG (DaVinci)
71 * + seems to behave with double buffering, PIO -and- CPPI
72 * + with gadgetfs + AIO, requests got lost?
74 * - RX/OUT
75 * + both pio and dma behave in with network and g_zero tests
76 * + dma is slow in typical case (short_not_ok is clear)
77 * + double buffering ok with PIO
78 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
79 * + request lossage observed with gadgetfs
81 * - ISO not tested ... might work, but only weakly isochronous
83 * - Gadget driver disabling of softconnect during bind() is ignored; so
84 * drivers can't hold off host requests until userspace is ready.
85 * (Workaround: they can turn it off later.)
87 * - PORTABILITY (assumes PIO works):
88 * + DaVinci, basically works with cppi dma
89 * + OMAP 2430, ditto with mentor dma
90 * + TUSB 6010, platform-specific dma in the works
93 /* ----------------------------------------------------------------------- */
95 #define is_buffer_mapped(req) (is_dma_capable() && \
96 (req->map_state != UN_MAPPED))
98 /* Maps the buffer to dma */
100 static inline void map_dma_buffer(struct musb_request *request,
101 struct musb *musb, struct musb_ep *musb_ep)
103 int compatible = true;
104 struct dma_controller *dma = musb->dma_controller;
106 request->map_state = UN_MAPPED;
108 if (!is_dma_capable() || !musb_ep->dma)
109 return;
111 /* Check if DMA engine can handle this request.
112 * DMA code must reject the USB request explicitly.
113 * Default behaviour is to map the request.
115 if (dma->is_compatible)
116 compatible = dma->is_compatible(musb_ep->dma,
117 musb_ep->packet_sz, request->request.buf,
118 request->request.length);
119 if (!compatible)
120 return;
122 if (request->request.dma == DMA_ADDR_INVALID) {
123 request->request.dma = dma_map_single(
124 musb->controller,
125 request->request.buf,
126 request->request.length,
127 request->tx
128 ? DMA_TO_DEVICE
129 : DMA_FROM_DEVICE);
130 request->map_state = MUSB_MAPPED;
131 } else {
132 dma_sync_single_for_device(musb->controller,
133 request->request.dma,
134 request->request.length,
135 request->tx
136 ? DMA_TO_DEVICE
137 : DMA_FROM_DEVICE);
138 request->map_state = PRE_MAPPED;
142 /* Unmap the buffer from dma and maps it back to cpu */
143 static inline void unmap_dma_buffer(struct musb_request *request,
144 struct musb *musb)
146 if (!is_buffer_mapped(request))
147 return;
149 if (request->request.dma == DMA_ADDR_INVALID) {
150 dev_vdbg(musb->controller,
151 "not unmapping a never mapped buffer\n");
152 return;
154 if (request->map_state == MUSB_MAPPED) {
155 dma_unmap_single(musb->controller,
156 request->request.dma,
157 request->request.length,
158 request->tx
159 ? DMA_TO_DEVICE
160 : DMA_FROM_DEVICE);
161 request->request.dma = DMA_ADDR_INVALID;
162 } else { /* PRE_MAPPED */
163 dma_sync_single_for_cpu(musb->controller,
164 request->request.dma,
165 request->request.length,
166 request->tx
167 ? DMA_TO_DEVICE
168 : DMA_FROM_DEVICE);
170 request->map_state = UN_MAPPED;
174 * Immediately complete a request.
176 * @param request the request to complete
177 * @param status the status to complete the request with
178 * Context: controller locked, IRQs blocked.
180 void musb_g_giveback(
181 struct musb_ep *ep,
182 struct usb_request *request,
183 int status)
184 __releases(ep->musb->lock)
185 __acquires(ep->musb->lock)
187 struct musb_request *req;
188 struct musb *musb;
189 int busy = ep->busy;
191 req = to_musb_request(request);
193 list_del(&req->list);
194 if (req->request.status == -EINPROGRESS)
195 req->request.status = status;
196 musb = req->musb;
198 ep->busy = 1;
199 spin_unlock(&musb->lock);
200 unmap_dma_buffer(req, musb);
201 if (request->status == 0)
202 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
203 ep->end_point.name, request,
204 req->request.actual, req->request.length);
205 else
206 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
207 ep->end_point.name, request,
208 req->request.actual, req->request.length,
209 request->status);
210 req->request.complete(&req->ep->end_point, &req->request);
211 spin_lock(&musb->lock);
212 ep->busy = busy;
215 /* ----------------------------------------------------------------------- */
218 * Abort requests queued to an endpoint using the status. Synchronous.
219 * caller locked controller and blocked irqs, and selected this ep.
221 static void nuke(struct musb_ep *ep, const int status)
223 struct musb *musb = ep->musb;
224 struct musb_request *req = NULL;
225 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
227 ep->busy = 1;
229 if (is_dma_capable() && ep->dma) {
230 struct dma_controller *c = ep->musb->dma_controller;
231 int value;
233 if (ep->is_in) {
235 * The programming guide says that we must not clear
236 * the DMAMODE bit before DMAENAB, so we only
237 * clear it in the second write...
239 musb_writew(epio, MUSB_TXCSR,
240 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
241 musb_writew(epio, MUSB_TXCSR,
242 0 | MUSB_TXCSR_FLUSHFIFO);
243 } else {
244 musb_writew(epio, MUSB_RXCSR,
245 0 | MUSB_RXCSR_FLUSHFIFO);
246 musb_writew(epio, MUSB_RXCSR,
247 0 | MUSB_RXCSR_FLUSHFIFO);
250 value = c->channel_abort(ep->dma);
251 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
252 ep->name, value);
253 c->channel_release(ep->dma);
254 ep->dma = NULL;
257 while (!list_empty(&ep->req_list)) {
258 req = list_first_entry(&ep->req_list, struct musb_request, list);
259 musb_g_giveback(ep, &req->request, status);
263 /* ----------------------------------------------------------------------- */
265 /* Data transfers - pure PIO, pure DMA, or mixed mode */
268 * This assumes the separate CPPI engine is responding to DMA requests
269 * from the usb core ... sequenced a bit differently from mentor dma.
272 static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
274 if (can_bulk_split(musb, ep->type))
275 return ep->hw_ep->max_packet_sz_tx;
276 else
277 return ep->packet_sz;
281 #ifdef CONFIG_USB_INVENTRA_DMA
283 /* Peripheral tx (IN) using Mentor DMA works as follows:
284 Only mode 0 is used for transfers <= wPktSize,
285 mode 1 is used for larger transfers,
287 One of the following happens:
288 - Host sends IN token which causes an endpoint interrupt
289 -> TxAvail
290 -> if DMA is currently busy, exit.
291 -> if queue is non-empty, txstate().
293 - Request is queued by the gadget driver.
294 -> if queue was previously empty, txstate()
296 txstate()
297 -> start
298 /\ -> setup DMA
299 | (data is transferred to the FIFO, then sent out when
300 | IN token(s) are recd from Host.
301 | -> DMA interrupt on completion
302 | calls TxAvail.
303 | -> stop DMA, ~DMAENAB,
304 | -> set TxPktRdy for last short pkt or zlp
305 | -> Complete Request
306 | -> Continue next request (call txstate)
307 |___________________________________|
309 * Non-Mentor DMA engines can of course work differently, such as by
310 * upleveling from irq-per-packet to irq-per-buffer.
313 #endif
316 * An endpoint is transmitting data. This can be called either from
317 * the IRQ routine or from ep.queue() to kickstart a request on an
318 * endpoint.
320 * Context: controller locked, IRQs blocked, endpoint selected
322 static void txstate(struct musb *musb, struct musb_request *req)
324 u8 epnum = req->epnum;
325 struct musb_ep *musb_ep;
326 void __iomem *epio = musb->endpoints[epnum].regs;
327 struct usb_request *request;
328 u16 fifo_count = 0, csr;
329 int use_dma = 0;
331 musb_ep = req->ep;
333 /* we shouldn't get here while DMA is active ... but we do ... */
334 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
335 dev_dbg(musb->controller, "dma pending...\n");
336 return;
339 /* read TXCSR before */
340 csr = musb_readw(epio, MUSB_TXCSR);
342 request = &req->request;
343 fifo_count = min(max_ep_writesize(musb, musb_ep),
344 (int)(request->length - request->actual));
346 if (csr & MUSB_TXCSR_TXPKTRDY) {
347 dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
348 musb_ep->end_point.name, csr);
349 return;
352 if (csr & MUSB_TXCSR_P_SENDSTALL) {
353 dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
354 musb_ep->end_point.name, csr);
355 return;
358 dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
359 epnum, musb_ep->packet_sz, fifo_count,
360 csr);
362 #ifndef CONFIG_MUSB_PIO_ONLY
363 if (is_buffer_mapped(req)) {
364 struct dma_controller *c = musb->dma_controller;
365 size_t request_size;
367 /* setup DMA, then program endpoint CSR */
368 request_size = min_t(size_t, request->length - request->actual,
369 musb_ep->dma->max_len);
371 use_dma = (request->dma != DMA_ADDR_INVALID);
373 /* MUSB_TXCSR_P_ISO is still set correctly */
375 #ifdef CONFIG_USB_INVENTRA_DMA
377 if (request_size < musb_ep->packet_sz)
378 musb_ep->dma->desired_mode = 0;
379 else
380 musb_ep->dma->desired_mode = 1;
382 use_dma = use_dma && c->channel_program(
383 musb_ep->dma, musb_ep->packet_sz,
384 musb_ep->dma->desired_mode,
385 request->dma + request->actual, request_size);
386 if (use_dma) {
387 if (musb_ep->dma->desired_mode == 0) {
389 * We must not clear the DMAMODE bit
390 * before the DMAENAB bit -- and the
391 * latter doesn't always get cleared
392 * before we get here...
394 csr &= ~(MUSB_TXCSR_AUTOSET
395 | MUSB_TXCSR_DMAENAB);
396 musb_writew(epio, MUSB_TXCSR, csr
397 | MUSB_TXCSR_P_WZC_BITS);
398 csr &= ~MUSB_TXCSR_DMAMODE;
399 csr |= (MUSB_TXCSR_DMAENAB |
400 MUSB_TXCSR_MODE);
401 /* against programming guide */
402 } else {
403 csr |= (MUSB_TXCSR_DMAENAB
404 | MUSB_TXCSR_DMAMODE
405 | MUSB_TXCSR_MODE);
406 if (!musb_ep->hb_mult)
407 csr |= MUSB_TXCSR_AUTOSET;
409 csr &= ~MUSB_TXCSR_P_UNDERRUN;
411 musb_writew(epio, MUSB_TXCSR, csr);
415 #elif defined(CONFIG_USB_TI_CPPI_DMA)
416 /* program endpoint CSR first, then setup DMA */
417 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
418 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
419 MUSB_TXCSR_MODE;
420 musb_writew(epio, MUSB_TXCSR,
421 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
422 | csr);
424 /* ensure writebuffer is empty */
425 csr = musb_readw(epio, MUSB_TXCSR);
427 /* NOTE host side sets DMAENAB later than this; both are
428 * OK since the transfer dma glue (between CPPI and Mentor
429 * fifos) just tells CPPI it could start. Data only moves
430 * to the USB TX fifo when both fifos are ready.
433 /* "mode" is irrelevant here; handle terminating ZLPs like
434 * PIO does, since the hardware RNDIS mode seems unreliable
435 * except for the last-packet-is-already-short case.
437 use_dma = use_dma && c->channel_program(
438 musb_ep->dma, musb_ep->packet_sz,
440 request->dma + request->actual,
441 request_size);
442 if (!use_dma) {
443 c->channel_release(musb_ep->dma);
444 musb_ep->dma = NULL;
445 csr &= ~MUSB_TXCSR_DMAENAB;
446 musb_writew(epio, MUSB_TXCSR, csr);
447 /* invariant: prequest->buf is non-null */
449 #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
450 use_dma = use_dma && c->channel_program(
451 musb_ep->dma, musb_ep->packet_sz,
452 request->zero,
453 request->dma + request->actual,
454 request_size);
455 #endif
457 #endif
459 if (!use_dma) {
461 * Unmap the dma buffer back to cpu if dma channel
462 * programming fails
464 unmap_dma_buffer(req, musb);
466 musb_write_fifo(musb_ep->hw_ep, fifo_count,
467 (u8 *) (request->buf + request->actual));
468 request->actual += fifo_count;
469 csr |= MUSB_TXCSR_TXPKTRDY;
470 csr &= ~MUSB_TXCSR_P_UNDERRUN;
471 musb_writew(epio, MUSB_TXCSR, csr);
474 /* host may already have the data when this message shows... */
475 dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
476 musb_ep->end_point.name, use_dma ? "dma" : "pio",
477 request->actual, request->length,
478 musb_readw(epio, MUSB_TXCSR),
479 fifo_count,
480 musb_readw(epio, MUSB_TXMAXP));
484 * FIFO state update (e.g. data ready).
485 * Called from IRQ, with controller locked.
487 void musb_g_tx(struct musb *musb, u8 epnum)
489 u16 csr;
490 struct musb_request *req;
491 struct usb_request *request;
492 u8 __iomem *mbase = musb->mregs;
493 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
494 void __iomem *epio = musb->endpoints[epnum].regs;
495 struct dma_channel *dma;
497 musb_ep_select(mbase, epnum);
498 req = next_request(musb_ep);
499 request = &req->request;
501 csr = musb_readw(epio, MUSB_TXCSR);
502 dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
504 dma = is_dma_capable() ? musb_ep->dma : NULL;
507 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
508 * probably rates reporting as a host error.
510 if (csr & MUSB_TXCSR_P_SENTSTALL) {
511 csr |= MUSB_TXCSR_P_WZC_BITS;
512 csr &= ~MUSB_TXCSR_P_SENTSTALL;
513 musb_writew(epio, MUSB_TXCSR, csr);
514 return;
517 if (csr & MUSB_TXCSR_P_UNDERRUN) {
518 /* We NAKed, no big deal... little reason to care. */
519 csr |= MUSB_TXCSR_P_WZC_BITS;
520 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
521 musb_writew(epio, MUSB_TXCSR, csr);
522 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
523 epnum, request);
526 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
528 * SHOULD NOT HAPPEN... has with CPPI though, after
529 * changing SENDSTALL (and other cases); harmless?
531 dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
532 return;
535 if (request) {
536 u8 is_dma = 0;
538 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
539 is_dma = 1;
540 csr |= MUSB_TXCSR_P_WZC_BITS;
541 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
542 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
543 musb_writew(epio, MUSB_TXCSR, csr);
544 /* Ensure writebuffer is empty. */
545 csr = musb_readw(epio, MUSB_TXCSR);
546 request->actual += musb_ep->dma->actual_len;
547 dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
548 epnum, csr, musb_ep->dma->actual_len, request);
552 * First, maybe a terminating short packet. Some DMA
553 * engines might handle this by themselves.
555 if ((request->zero && request->length
556 && (request->length % musb_ep->packet_sz == 0)
557 && (request->actual == request->length))
558 #ifdef CONFIG_USB_INVENTRA_DMA
559 || (is_dma && (!dma->desired_mode ||
560 (request->actual &
561 (musb_ep->packet_sz - 1))))
562 #endif
565 * On DMA completion, FIFO may not be
566 * available yet...
568 if (csr & MUSB_TXCSR_TXPKTRDY)
569 return;
571 dev_dbg(musb->controller, "sending zero pkt\n");
572 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
573 | MUSB_TXCSR_TXPKTRDY);
574 request->zero = 0;
577 if (request->actual == request->length) {
578 musb_g_giveback(musb_ep, request, 0);
579 req = musb_ep->desc ? next_request(musb_ep) : NULL;
580 if (!req) {
581 dev_dbg(musb->controller, "%s idle now\n",
582 musb_ep->end_point.name);
583 return;
587 txstate(musb, req);
591 /* ------------------------------------------------------------ */
593 #ifdef CONFIG_USB_INVENTRA_DMA
595 /* Peripheral rx (OUT) using Mentor DMA works as follows:
596 - Only mode 0 is used.
598 - Request is queued by the gadget class driver.
599 -> if queue was previously empty, rxstate()
601 - Host sends OUT token which causes an endpoint interrupt
602 /\ -> RxReady
603 | -> if request queued, call rxstate
604 | /\ -> setup DMA
605 | | -> DMA interrupt on completion
606 | | -> RxReady
607 | | -> stop DMA
608 | | -> ack the read
609 | | -> if data recd = max expected
610 | | by the request, or host
611 | | sent a short packet,
612 | | complete the request,
613 | | and start the next one.
614 | |_____________________________________|
615 | else just wait for the host
616 | to send the next OUT token.
617 |__________________________________________________|
619 * Non-Mentor DMA engines can of course work differently.
622 #endif
625 * Context: controller locked, IRQs blocked, endpoint selected
627 static void rxstate(struct musb *musb, struct musb_request *req)
629 const u8 epnum = req->epnum;
630 struct usb_request *request = &req->request;
631 struct musb_ep *musb_ep;
632 void __iomem *epio = musb->endpoints[epnum].regs;
633 unsigned fifo_count = 0;
634 u16 len;
635 u16 csr = musb_readw(epio, MUSB_RXCSR);
636 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
638 if (hw_ep->is_shared_fifo)
639 musb_ep = &hw_ep->ep_in;
640 else
641 musb_ep = &hw_ep->ep_out;
643 len = musb_ep->packet_sz;
645 /* We shouldn't get here while DMA is active, but we do... */
646 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
647 dev_dbg(musb->controller, "DMA pending...\n");
648 return;
651 if (csr & MUSB_RXCSR_P_SENDSTALL) {
652 dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
653 musb_ep->end_point.name, csr);
654 return;
657 if (is_cppi_enabled() && is_buffer_mapped(req)) {
658 struct dma_controller *c = musb->dma_controller;
659 struct dma_channel *channel = musb_ep->dma;
661 /* NOTE: CPPI won't actually stop advancing the DMA
662 * queue after short packet transfers, so this is almost
663 * always going to run as IRQ-per-packet DMA so that
664 * faults will be handled correctly.
666 if (c->channel_program(channel,
667 musb_ep->packet_sz,
668 !request->short_not_ok,
669 request->dma + request->actual,
670 request->length - request->actual)) {
672 /* make sure that if an rxpkt arrived after the irq,
673 * the cppi engine will be ready to take it as soon
674 * as DMA is enabled
676 csr &= ~(MUSB_RXCSR_AUTOCLEAR
677 | MUSB_RXCSR_DMAMODE);
678 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
679 musb_writew(epio, MUSB_RXCSR, csr);
680 return;
684 if (csr & MUSB_RXCSR_RXPKTRDY) {
685 len = musb_readw(epio, MUSB_RXCOUNT);
686 if (request->actual < request->length) {
687 #ifdef CONFIG_USB_INVENTRA_DMA
688 if (is_buffer_mapped(req)) {
689 struct dma_controller *c;
690 struct dma_channel *channel;
691 int use_dma = 0;
693 c = musb->dma_controller;
694 channel = musb_ep->dma;
696 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
697 * mode 0 only. So we do not get endpoint interrupts due to DMA
698 * completion. We only get interrupts from DMA controller.
700 * We could operate in DMA mode 1 if we knew the size of the tranfer
701 * in advance. For mass storage class, request->length = what the host
702 * sends, so that'd work. But for pretty much everything else,
703 * request->length is routinely more than what the host sends. For
704 * most these gadgets, end of is signified either by a short packet,
705 * or filling the last byte of the buffer. (Sending extra data in
706 * that last pckate should trigger an overflow fault.) But in mode 1,
707 * we don't get DMA completion interrrupt for short packets.
709 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
710 * to get endpoint interrupt on every DMA req, but that didn't seem
711 * to work reliably.
713 * REVISIT an updated g_file_storage can set req->short_not_ok, which
714 * then becomes usable as a runtime "use mode 1" hint...
717 csr |= MUSB_RXCSR_DMAENAB;
718 #ifdef USE_MODE1
719 csr |= MUSB_RXCSR_AUTOCLEAR;
720 /* csr |= MUSB_RXCSR_DMAMODE; */
722 /* this special sequence (enabling and then
723 * disabling MUSB_RXCSR_DMAMODE) is required
724 * to get DMAReq to activate
726 musb_writew(epio, MUSB_RXCSR,
727 csr | MUSB_RXCSR_DMAMODE);
728 #else
729 if (!musb_ep->hb_mult &&
730 musb_ep->hw_ep->rx_double_buffered)
731 csr |= MUSB_RXCSR_AUTOCLEAR;
732 #endif
733 musb_writew(epio, MUSB_RXCSR, csr);
735 if (request->actual < request->length) {
736 int transfer_size = 0;
737 #ifdef USE_MODE1
738 transfer_size = min(request->length - request->actual,
739 channel->max_len);
740 #else
741 transfer_size = min(request->length - request->actual,
742 (unsigned)len);
743 #endif
744 if (transfer_size <= musb_ep->packet_sz)
745 musb_ep->dma->desired_mode = 0;
746 else
747 musb_ep->dma->desired_mode = 1;
749 use_dma = c->channel_program(
750 channel,
751 musb_ep->packet_sz,
752 channel->desired_mode,
753 request->dma
754 + request->actual,
755 transfer_size);
758 if (use_dma)
759 return;
761 #endif /* Mentor's DMA */
763 fifo_count = request->length - request->actual;
764 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
765 musb_ep->end_point.name,
766 len, fifo_count,
767 musb_ep->packet_sz);
769 fifo_count = min_t(unsigned, len, fifo_count);
771 #ifdef CONFIG_USB_TUSB_OMAP_DMA
772 if (tusb_dma_omap() && is_buffer_mapped(req)) {
773 struct dma_controller *c = musb->dma_controller;
774 struct dma_channel *channel = musb_ep->dma;
775 u32 dma_addr = request->dma + request->actual;
776 int ret;
778 ret = c->channel_program(channel,
779 musb_ep->packet_sz,
780 channel->desired_mode,
781 dma_addr,
782 fifo_count);
783 if (ret)
784 return;
786 #endif
788 * Unmap the dma buffer back to cpu if dma channel
789 * programming fails. This buffer is mapped if the
790 * channel allocation is successful
792 if (is_buffer_mapped(req)) {
793 unmap_dma_buffer(req, musb);
796 * Clear DMAENAB and AUTOCLEAR for the
797 * PIO mode transfer
799 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
800 musb_writew(epio, MUSB_RXCSR, csr);
803 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
804 (request->buf + request->actual));
805 request->actual += fifo_count;
807 /* REVISIT if we left anything in the fifo, flush
808 * it and report -EOVERFLOW
811 /* ack the read! */
812 csr |= MUSB_RXCSR_P_WZC_BITS;
813 csr &= ~MUSB_RXCSR_RXPKTRDY;
814 musb_writew(epio, MUSB_RXCSR, csr);
818 /* reach the end or short packet detected */
819 if (request->actual == request->length || len < musb_ep->packet_sz)
820 musb_g_giveback(musb_ep, request, 0);
824 * Data ready for a request; called from IRQ
826 void musb_g_rx(struct musb *musb, u8 epnum)
828 u16 csr;
829 struct musb_request *req;
830 struct usb_request *request;
831 void __iomem *mbase = musb->mregs;
832 struct musb_ep *musb_ep;
833 void __iomem *epio = musb->endpoints[epnum].regs;
834 struct dma_channel *dma;
835 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
837 if (hw_ep->is_shared_fifo)
838 musb_ep = &hw_ep->ep_in;
839 else
840 musb_ep = &hw_ep->ep_out;
842 musb_ep_select(mbase, epnum);
844 req = next_request(musb_ep);
845 if (!req)
846 return;
848 request = &req->request;
850 csr = musb_readw(epio, MUSB_RXCSR);
851 dma = is_dma_capable() ? musb_ep->dma : NULL;
853 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
854 csr, dma ? " (dma)" : "", request);
856 if (csr & MUSB_RXCSR_P_SENTSTALL) {
857 csr |= MUSB_RXCSR_P_WZC_BITS;
858 csr &= ~MUSB_RXCSR_P_SENTSTALL;
859 musb_writew(epio, MUSB_RXCSR, csr);
860 return;
863 if (csr & MUSB_RXCSR_P_OVERRUN) {
864 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
865 csr &= ~MUSB_RXCSR_P_OVERRUN;
866 musb_writew(epio, MUSB_RXCSR, csr);
868 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
869 if (request->status == -EINPROGRESS)
870 request->status = -EOVERFLOW;
872 if (csr & MUSB_RXCSR_INCOMPRX) {
873 /* REVISIT not necessarily an error */
874 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
877 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
878 /* "should not happen"; likely RXPKTRDY pending for DMA */
879 dev_dbg(musb->controller, "%s busy, csr %04x\n",
880 musb_ep->end_point.name, csr);
881 return;
884 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
885 csr &= ~(MUSB_RXCSR_AUTOCLEAR
886 | MUSB_RXCSR_DMAENAB
887 | MUSB_RXCSR_DMAMODE);
888 musb_writew(epio, MUSB_RXCSR,
889 MUSB_RXCSR_P_WZC_BITS | csr);
891 request->actual += musb_ep->dma->actual_len;
893 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
894 epnum, csr,
895 musb_readw(epio, MUSB_RXCSR),
896 musb_ep->dma->actual_len, request);
898 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
899 /* Autoclear doesn't clear RxPktRdy for short packets */
900 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
901 || (dma->actual_len
902 & (musb_ep->packet_sz - 1))) {
903 /* ack the read! */
904 csr &= ~MUSB_RXCSR_RXPKTRDY;
905 musb_writew(epio, MUSB_RXCSR, csr);
908 /* incomplete, and not short? wait for next IN packet */
909 if ((request->actual < request->length)
910 && (musb_ep->dma->actual_len
911 == musb_ep->packet_sz)) {
912 /* In double buffer case, continue to unload fifo if
913 * there is Rx packet in FIFO.
915 csr = musb_readw(epio, MUSB_RXCSR);
916 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
917 hw_ep->rx_double_buffered)
918 goto exit;
919 return;
921 #endif
922 musb_g_giveback(musb_ep, request, 0);
924 req = next_request(musb_ep);
925 if (!req)
926 return;
928 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
929 exit:
930 #endif
931 /* Analyze request */
932 rxstate(musb, req);
935 /* ------------------------------------------------------------ */
937 static int musb_gadget_enable(struct usb_ep *ep,
938 const struct usb_endpoint_descriptor *desc)
940 unsigned long flags;
941 struct musb_ep *musb_ep;
942 struct musb_hw_ep *hw_ep;
943 void __iomem *regs;
944 struct musb *musb;
945 void __iomem *mbase;
946 u8 epnum;
947 u16 csr;
948 unsigned tmp;
949 int status = -EINVAL;
951 if (!ep || !desc)
952 return -EINVAL;
954 musb_ep = to_musb_ep(ep);
955 hw_ep = musb_ep->hw_ep;
956 regs = hw_ep->regs;
957 musb = musb_ep->musb;
958 mbase = musb->mregs;
959 epnum = musb_ep->current_epnum;
961 spin_lock_irqsave(&musb->lock, flags);
963 if (musb_ep->desc) {
964 status = -EBUSY;
965 goto fail;
967 musb_ep->type = usb_endpoint_type(desc);
969 /* check direction and (later) maxpacket size against endpoint */
970 if (usb_endpoint_num(desc) != epnum)
971 goto fail;
973 /* REVISIT this rules out high bandwidth periodic transfers */
974 tmp = le16_to_cpu(desc->wMaxPacketSize);
975 if (tmp & ~0x07ff) {
976 int ok;
978 if (usb_endpoint_dir_in(desc))
979 ok = musb->hb_iso_tx;
980 else
981 ok = musb->hb_iso_rx;
983 if (!ok) {
984 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
985 goto fail;
987 musb_ep->hb_mult = (tmp >> 11) & 3;
988 } else {
989 musb_ep->hb_mult = 0;
992 musb_ep->packet_sz = tmp & 0x7ff;
993 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
995 /* enable the interrupts for the endpoint, set the endpoint
996 * packet size (or fail), set the mode, clear the fifo
998 musb_ep_select(mbase, epnum);
999 if (usb_endpoint_dir_in(desc)) {
1000 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1002 if (hw_ep->is_shared_fifo)
1003 musb_ep->is_in = 1;
1004 if (!musb_ep->is_in)
1005 goto fail;
1007 if (tmp > hw_ep->max_packet_sz_tx) {
1008 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1009 goto fail;
1012 int_txe |= (1 << epnum);
1013 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1015 /* REVISIT if can_bulk_split(), use by updating "tmp";
1016 * likewise high bandwidth periodic tx
1018 /* Set TXMAXP with the FIFO size of the endpoint
1019 * to disable double buffering mode.
1021 if (musb->double_buffer_not_ok)
1022 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
1023 else
1024 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1025 | (musb_ep->hb_mult << 11));
1027 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1028 if (musb_readw(regs, MUSB_TXCSR)
1029 & MUSB_TXCSR_FIFONOTEMPTY)
1030 csr |= MUSB_TXCSR_FLUSHFIFO;
1031 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1032 csr |= MUSB_TXCSR_P_ISO;
1034 /* set twice in case of double buffering */
1035 musb_writew(regs, MUSB_TXCSR, csr);
1036 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1037 musb_writew(regs, MUSB_TXCSR, csr);
1039 } else {
1040 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
1042 if (hw_ep->is_shared_fifo)
1043 musb_ep->is_in = 0;
1044 if (musb_ep->is_in)
1045 goto fail;
1047 if (tmp > hw_ep->max_packet_sz_rx) {
1048 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1049 goto fail;
1052 int_rxe |= (1 << epnum);
1053 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
1055 /* REVISIT if can_bulk_combine() use by updating "tmp"
1056 * likewise high bandwidth periodic rx
1058 /* Set RXMAXP with the FIFO size of the endpoint
1059 * to disable double buffering mode.
1061 if (musb->double_buffer_not_ok)
1062 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1063 else
1064 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1065 | (musb_ep->hb_mult << 11));
1067 /* force shared fifo to OUT-only mode */
1068 if (hw_ep->is_shared_fifo) {
1069 csr = musb_readw(regs, MUSB_TXCSR);
1070 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1071 musb_writew(regs, MUSB_TXCSR, csr);
1074 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1075 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1076 csr |= MUSB_RXCSR_P_ISO;
1077 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1078 csr |= MUSB_RXCSR_DISNYET;
1080 /* set twice in case of double buffering */
1081 musb_writew(regs, MUSB_RXCSR, csr);
1082 musb_writew(regs, MUSB_RXCSR, csr);
1085 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1086 * for some reason you run out of channels here.
1088 if (is_dma_capable() && musb->dma_controller) {
1089 struct dma_controller *c = musb->dma_controller;
1091 musb_ep->dma = c->channel_alloc(c, hw_ep,
1092 (desc->bEndpointAddress & USB_DIR_IN));
1093 } else
1094 musb_ep->dma = NULL;
1096 musb_ep->desc = desc;
1097 musb_ep->busy = 0;
1098 musb_ep->wedged = 0;
1099 status = 0;
1101 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1102 musb_driver_name, musb_ep->end_point.name,
1103 ({ char *s; switch (musb_ep->type) {
1104 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1105 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1106 default: s = "iso"; break;
1107 }; s; }),
1108 musb_ep->is_in ? "IN" : "OUT",
1109 musb_ep->dma ? "dma, " : "",
1110 musb_ep->packet_sz);
1112 schedule_work(&musb->irq_work);
1114 fail:
1115 spin_unlock_irqrestore(&musb->lock, flags);
1116 return status;
1120 * Disable an endpoint flushing all requests queued.
1122 static int musb_gadget_disable(struct usb_ep *ep)
1124 unsigned long flags;
1125 struct musb *musb;
1126 u8 epnum;
1127 struct musb_ep *musb_ep;
1128 void __iomem *epio;
1129 int status = 0;
1131 musb_ep = to_musb_ep(ep);
1132 musb = musb_ep->musb;
1133 epnum = musb_ep->current_epnum;
1134 epio = musb->endpoints[epnum].regs;
1136 spin_lock_irqsave(&musb->lock, flags);
1137 musb_ep_select(musb->mregs, epnum);
1139 /* zero the endpoint sizes */
1140 if (musb_ep->is_in) {
1141 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1142 int_txe &= ~(1 << epnum);
1143 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1144 musb_writew(epio, MUSB_TXMAXP, 0);
1145 } else {
1146 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1147 int_rxe &= ~(1 << epnum);
1148 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1149 musb_writew(epio, MUSB_RXMAXP, 0);
1152 musb_ep->desc = NULL;
1154 /* abort all pending DMA and requests */
1155 nuke(musb_ep, -ESHUTDOWN);
1157 schedule_work(&musb->irq_work);
1159 spin_unlock_irqrestore(&(musb->lock), flags);
1161 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
1163 return status;
1167 * Allocate a request for an endpoint.
1168 * Reused by ep0 code.
1170 struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1172 struct musb_ep *musb_ep = to_musb_ep(ep);
1173 struct musb *musb = musb_ep->musb;
1174 struct musb_request *request = NULL;
1176 request = kzalloc(sizeof *request, gfp_flags);
1177 if (!request) {
1178 dev_dbg(musb->controller, "not enough memory\n");
1179 return NULL;
1182 request->request.dma = DMA_ADDR_INVALID;
1183 request->epnum = musb_ep->current_epnum;
1184 request->ep = musb_ep;
1186 return &request->request;
1190 * Free a request
1191 * Reused by ep0 code.
1193 void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1195 kfree(to_musb_request(req));
1198 static LIST_HEAD(buffers);
1200 struct free_record {
1201 struct list_head list;
1202 struct device *dev;
1203 unsigned bytes;
1204 dma_addr_t dma;
1208 * Context: controller locked, IRQs blocked.
1210 void musb_ep_restart(struct musb *musb, struct musb_request *req)
1212 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
1213 req->tx ? "TX/IN" : "RX/OUT",
1214 &req->request, req->request.length, req->epnum);
1216 musb_ep_select(musb->mregs, req->epnum);
1217 if (req->tx)
1218 txstate(musb, req);
1219 else
1220 rxstate(musb, req);
1223 static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1224 gfp_t gfp_flags)
1226 struct musb_ep *musb_ep;
1227 struct musb_request *request;
1228 struct musb *musb;
1229 int status = 0;
1230 unsigned long lockflags;
1232 if (!ep || !req)
1233 return -EINVAL;
1234 if (!req->buf)
1235 return -ENODATA;
1237 musb_ep = to_musb_ep(ep);
1238 musb = musb_ep->musb;
1240 request = to_musb_request(req);
1241 request->musb = musb;
1243 if (request->ep != musb_ep)
1244 return -EINVAL;
1246 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
1248 /* request is mine now... */
1249 request->request.actual = 0;
1250 request->request.status = -EINPROGRESS;
1251 request->epnum = musb_ep->current_epnum;
1252 request->tx = musb_ep->is_in;
1254 map_dma_buffer(request, musb, musb_ep);
1256 spin_lock_irqsave(&musb->lock, lockflags);
1258 /* don't queue if the ep is down */
1259 if (!musb_ep->desc) {
1260 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
1261 req, ep->name, "disabled");
1262 status = -ESHUTDOWN;
1263 goto cleanup;
1266 /* add request to the list */
1267 list_add_tail(&request->list, &musb_ep->req_list);
1269 /* it this is the head of the queue, start i/o ... */
1270 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
1271 musb_ep_restart(musb, request);
1273 cleanup:
1274 spin_unlock_irqrestore(&musb->lock, lockflags);
1275 return status;
1278 static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1280 struct musb_ep *musb_ep = to_musb_ep(ep);
1281 struct musb_request *req = to_musb_request(request);
1282 struct musb_request *r;
1283 unsigned long flags;
1284 int status = 0;
1285 struct musb *musb = musb_ep->musb;
1287 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1288 return -EINVAL;
1290 spin_lock_irqsave(&musb->lock, flags);
1292 list_for_each_entry(r, &musb_ep->req_list, list) {
1293 if (r == req)
1294 break;
1296 if (r != req) {
1297 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
1298 status = -EINVAL;
1299 goto done;
1302 /* if the hardware doesn't have the request, easy ... */
1303 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1304 musb_g_giveback(musb_ep, request, -ECONNRESET);
1306 /* ... else abort the dma transfer ... */
1307 else if (is_dma_capable() && musb_ep->dma) {
1308 struct dma_controller *c = musb->dma_controller;
1310 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1311 if (c->channel_abort)
1312 status = c->channel_abort(musb_ep->dma);
1313 else
1314 status = -EBUSY;
1315 if (status == 0)
1316 musb_g_giveback(musb_ep, request, -ECONNRESET);
1317 } else {
1318 /* NOTE: by sticking to easily tested hardware/driver states,
1319 * we leave counting of in-flight packets imprecise.
1321 musb_g_giveback(musb_ep, request, -ECONNRESET);
1324 done:
1325 spin_unlock_irqrestore(&musb->lock, flags);
1326 return status;
1330 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1331 * data but will queue requests.
1333 * exported to ep0 code
1335 static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1337 struct musb_ep *musb_ep = to_musb_ep(ep);
1338 u8 epnum = musb_ep->current_epnum;
1339 struct musb *musb = musb_ep->musb;
1340 void __iomem *epio = musb->endpoints[epnum].regs;
1341 void __iomem *mbase;
1342 unsigned long flags;
1343 u16 csr;
1344 struct musb_request *request;
1345 int status = 0;
1347 if (!ep)
1348 return -EINVAL;
1349 mbase = musb->mregs;
1351 spin_lock_irqsave(&musb->lock, flags);
1353 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1354 status = -EINVAL;
1355 goto done;
1358 musb_ep_select(mbase, epnum);
1360 request = next_request(musb_ep);
1361 if (value) {
1362 if (request) {
1363 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
1364 ep->name);
1365 status = -EAGAIN;
1366 goto done;
1368 /* Cannot portably stall with non-empty FIFO */
1369 if (musb_ep->is_in) {
1370 csr = musb_readw(epio, MUSB_TXCSR);
1371 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1372 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
1373 status = -EAGAIN;
1374 goto done;
1377 } else
1378 musb_ep->wedged = 0;
1380 /* set/clear the stall and toggle bits */
1381 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1382 if (musb_ep->is_in) {
1383 csr = musb_readw(epio, MUSB_TXCSR);
1384 csr |= MUSB_TXCSR_P_WZC_BITS
1385 | MUSB_TXCSR_CLRDATATOG;
1386 if (value)
1387 csr |= MUSB_TXCSR_P_SENDSTALL;
1388 else
1389 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1390 | MUSB_TXCSR_P_SENTSTALL);
1391 csr &= ~MUSB_TXCSR_TXPKTRDY;
1392 musb_writew(epio, MUSB_TXCSR, csr);
1393 } else {
1394 csr = musb_readw(epio, MUSB_RXCSR);
1395 csr |= MUSB_RXCSR_P_WZC_BITS
1396 | MUSB_RXCSR_FLUSHFIFO
1397 | MUSB_RXCSR_CLRDATATOG;
1398 if (value)
1399 csr |= MUSB_RXCSR_P_SENDSTALL;
1400 else
1401 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1402 | MUSB_RXCSR_P_SENTSTALL);
1403 musb_writew(epio, MUSB_RXCSR, csr);
1406 /* maybe start the first request in the queue */
1407 if (!musb_ep->busy && !value && request) {
1408 dev_dbg(musb->controller, "restarting the request\n");
1409 musb_ep_restart(musb, request);
1412 done:
1413 spin_unlock_irqrestore(&musb->lock, flags);
1414 return status;
1418 * Sets the halt feature with the clear requests ignored
1420 static int musb_gadget_set_wedge(struct usb_ep *ep)
1422 struct musb_ep *musb_ep = to_musb_ep(ep);
1424 if (!ep)
1425 return -EINVAL;
1427 musb_ep->wedged = 1;
1429 return usb_ep_set_halt(ep);
1432 static int musb_gadget_fifo_status(struct usb_ep *ep)
1434 struct musb_ep *musb_ep = to_musb_ep(ep);
1435 void __iomem *epio = musb_ep->hw_ep->regs;
1436 int retval = -EINVAL;
1438 if (musb_ep->desc && !musb_ep->is_in) {
1439 struct musb *musb = musb_ep->musb;
1440 int epnum = musb_ep->current_epnum;
1441 void __iomem *mbase = musb->mregs;
1442 unsigned long flags;
1444 spin_lock_irqsave(&musb->lock, flags);
1446 musb_ep_select(mbase, epnum);
1447 /* FIXME return zero unless RXPKTRDY is set */
1448 retval = musb_readw(epio, MUSB_RXCOUNT);
1450 spin_unlock_irqrestore(&musb->lock, flags);
1452 return retval;
1455 static void musb_gadget_fifo_flush(struct usb_ep *ep)
1457 struct musb_ep *musb_ep = to_musb_ep(ep);
1458 struct musb *musb = musb_ep->musb;
1459 u8 epnum = musb_ep->current_epnum;
1460 void __iomem *epio = musb->endpoints[epnum].regs;
1461 void __iomem *mbase;
1462 unsigned long flags;
1463 u16 csr, int_txe;
1465 mbase = musb->mregs;
1467 spin_lock_irqsave(&musb->lock, flags);
1468 musb_ep_select(mbase, (u8) epnum);
1470 /* disable interrupts */
1471 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1472 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1474 if (musb_ep->is_in) {
1475 csr = musb_readw(epio, MUSB_TXCSR);
1476 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1477 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1478 musb_writew(epio, MUSB_TXCSR, csr);
1479 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1480 musb_writew(epio, MUSB_TXCSR, csr);
1482 } else {
1483 csr = musb_readw(epio, MUSB_RXCSR);
1484 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1485 musb_writew(epio, MUSB_RXCSR, csr);
1486 musb_writew(epio, MUSB_RXCSR, csr);
1489 /* re-enable interrupt */
1490 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1491 spin_unlock_irqrestore(&musb->lock, flags);
1494 static const struct usb_ep_ops musb_ep_ops = {
1495 .enable = musb_gadget_enable,
1496 .disable = musb_gadget_disable,
1497 .alloc_request = musb_alloc_request,
1498 .free_request = musb_free_request,
1499 .queue = musb_gadget_queue,
1500 .dequeue = musb_gadget_dequeue,
1501 .set_halt = musb_gadget_set_halt,
1502 .set_wedge = musb_gadget_set_wedge,
1503 .fifo_status = musb_gadget_fifo_status,
1504 .fifo_flush = musb_gadget_fifo_flush
1507 /* ----------------------------------------------------------------------- */
1509 static int musb_gadget_get_frame(struct usb_gadget *gadget)
1511 struct musb *musb = gadget_to_musb(gadget);
1513 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1516 static int musb_gadget_wakeup(struct usb_gadget *gadget)
1518 struct musb *musb = gadget_to_musb(gadget);
1519 void __iomem *mregs = musb->mregs;
1520 unsigned long flags;
1521 int status = -EINVAL;
1522 u8 power, devctl;
1523 int retries;
1525 spin_lock_irqsave(&musb->lock, flags);
1527 switch (musb->xceiv->state) {
1528 case OTG_STATE_B_PERIPHERAL:
1529 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1530 * that's part of the standard usb 1.1 state machine, and
1531 * doesn't affect OTG transitions.
1533 if (musb->may_wakeup && musb->is_suspended)
1534 break;
1535 goto done;
1536 case OTG_STATE_B_IDLE:
1537 /* Start SRP ... OTG not required. */
1538 devctl = musb_readb(mregs, MUSB_DEVCTL);
1539 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
1540 devctl |= MUSB_DEVCTL_SESSION;
1541 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1542 devctl = musb_readb(mregs, MUSB_DEVCTL);
1543 retries = 100;
1544 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1545 devctl = musb_readb(mregs, MUSB_DEVCTL);
1546 if (retries-- < 1)
1547 break;
1549 retries = 10000;
1550 while (devctl & MUSB_DEVCTL_SESSION) {
1551 devctl = musb_readb(mregs, MUSB_DEVCTL);
1552 if (retries-- < 1)
1553 break;
1556 spin_unlock_irqrestore(&musb->lock, flags);
1557 otg_start_srp(musb->xceiv);
1558 spin_lock_irqsave(&musb->lock, flags);
1560 /* Block idling for at least 1s */
1561 musb_platform_try_idle(musb,
1562 jiffies + msecs_to_jiffies(1 * HZ));
1564 status = 0;
1565 goto done;
1566 default:
1567 dev_dbg(musb->controller, "Unhandled wake: %s\n",
1568 otg_state_string(musb->xceiv->state));
1569 goto done;
1572 status = 0;
1574 power = musb_readb(mregs, MUSB_POWER);
1575 power |= MUSB_POWER_RESUME;
1576 musb_writeb(mregs, MUSB_POWER, power);
1577 dev_dbg(musb->controller, "issue wakeup\n");
1579 /* FIXME do this next chunk in a timer callback, no udelay */
1580 mdelay(2);
1582 power = musb_readb(mregs, MUSB_POWER);
1583 power &= ~MUSB_POWER_RESUME;
1584 musb_writeb(mregs, MUSB_POWER, power);
1585 done:
1586 spin_unlock_irqrestore(&musb->lock, flags);
1587 return status;
1590 static int
1591 musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1593 struct musb *musb = gadget_to_musb(gadget);
1595 musb->is_self_powered = !!is_selfpowered;
1596 return 0;
1599 static void musb_pullup(struct musb *musb, int is_on)
1601 u8 power;
1603 power = musb_readb(musb->mregs, MUSB_POWER);
1604 if (is_on)
1605 power |= MUSB_POWER_SOFTCONN;
1606 else
1607 power &= ~MUSB_POWER_SOFTCONN;
1609 /* FIXME if on, HdrcStart; if off, HdrcStop */
1611 dev_dbg(musb->controller, "gadget %s D+ pullup %s\n",
1612 musb->gadget_driver->function, is_on ? "on" : "off");
1613 musb_writeb(musb->mregs, MUSB_POWER, power);
1616 #if 0
1617 static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1619 dev_dbg(musb->controller, "<= %s =>\n", __func__);
1622 * FIXME iff driver's softconnect flag is set (as it is during probe,
1623 * though that can clear it), just musb_pullup().
1626 return -EINVAL;
1628 #endif
1630 static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1632 struct musb *musb = gadget_to_musb(gadget);
1634 if (!musb->xceiv->set_power)
1635 return -EOPNOTSUPP;
1636 return otg_set_power(musb->xceiv, mA);
1639 static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1641 struct musb *musb = gadget_to_musb(gadget);
1642 unsigned long flags;
1644 is_on = !!is_on;
1646 /* NOTE: this assumes we are sensing vbus; we'd rather
1647 * not pullup unless the B-session is active.
1649 spin_lock_irqsave(&musb->lock, flags);
1650 if (is_on != musb->softconnect) {
1651 musb->softconnect = is_on;
1652 musb_pullup(musb, is_on);
1654 spin_unlock_irqrestore(&musb->lock, flags);
1655 return 0;
1658 static const struct usb_gadget_ops musb_gadget_operations = {
1659 .get_frame = musb_gadget_get_frame,
1660 .wakeup = musb_gadget_wakeup,
1661 .set_selfpowered = musb_gadget_set_self_powered,
1662 /* .vbus_session = musb_gadget_vbus_session, */
1663 .vbus_draw = musb_gadget_vbus_draw,
1664 .pullup = musb_gadget_pullup,
1667 /* ----------------------------------------------------------------------- */
1669 /* Registration */
1671 /* Only this registration code "knows" the rule (from USB standards)
1672 * about there being only one external upstream port. It assumes
1673 * all peripheral ports are external...
1675 static struct musb *the_gadget;
1677 static void musb_gadget_release(struct device *dev)
1679 /* kref_put(WHAT) */
1680 dev_dbg(dev, "%s\n", __func__);
1684 static void __init
1685 init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1687 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1689 memset(ep, 0, sizeof *ep);
1691 ep->current_epnum = epnum;
1692 ep->musb = musb;
1693 ep->hw_ep = hw_ep;
1694 ep->is_in = is_in;
1696 INIT_LIST_HEAD(&ep->req_list);
1698 sprintf(ep->name, "ep%d%s", epnum,
1699 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1700 is_in ? "in" : "out"));
1701 ep->end_point.name = ep->name;
1702 INIT_LIST_HEAD(&ep->end_point.ep_list);
1703 if (!epnum) {
1704 ep->end_point.maxpacket = 64;
1705 ep->end_point.ops = &musb_g_ep0_ops;
1706 musb->g.ep0 = &ep->end_point;
1707 } else {
1708 if (is_in)
1709 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1710 else
1711 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1712 ep->end_point.ops = &musb_ep_ops;
1713 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1718 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1719 * to the rest of the driver state.
1721 static inline void __init musb_g_init_endpoints(struct musb *musb)
1723 u8 epnum;
1724 struct musb_hw_ep *hw_ep;
1725 unsigned count = 0;
1727 /* initialize endpoint list just once */
1728 INIT_LIST_HEAD(&(musb->g.ep_list));
1730 for (epnum = 0, hw_ep = musb->endpoints;
1731 epnum < musb->nr_endpoints;
1732 epnum++, hw_ep++) {
1733 if (hw_ep->is_shared_fifo /* || !epnum */) {
1734 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1735 count++;
1736 } else {
1737 if (hw_ep->max_packet_sz_tx) {
1738 init_peripheral_ep(musb, &hw_ep->ep_in,
1739 epnum, 1);
1740 count++;
1742 if (hw_ep->max_packet_sz_rx) {
1743 init_peripheral_ep(musb, &hw_ep->ep_out,
1744 epnum, 0);
1745 count++;
1751 /* called once during driver setup to initialize and link into
1752 * the driver model; memory is zeroed.
1754 int __init musb_gadget_setup(struct musb *musb)
1756 int status;
1758 /* REVISIT minor race: if (erroneously) setting up two
1759 * musb peripherals at the same time, only the bus lock
1760 * is probably held.
1762 if (the_gadget)
1763 return -EBUSY;
1764 the_gadget = musb;
1766 musb->g.ops = &musb_gadget_operations;
1767 musb->g.is_dualspeed = 1;
1768 musb->g.speed = USB_SPEED_UNKNOWN;
1770 /* this "gadget" abstracts/virtualizes the controller */
1771 dev_set_name(&musb->g.dev, "gadget");
1772 musb->g.dev.parent = musb->controller;
1773 musb->g.dev.dma_mask = musb->controller->dma_mask;
1774 musb->g.dev.release = musb_gadget_release;
1775 musb->g.name = musb_driver_name;
1777 if (is_otg_enabled(musb))
1778 musb->g.is_otg = 1;
1780 musb_g_init_endpoints(musb);
1782 musb->is_active = 0;
1783 musb_platform_try_idle(musb, 0);
1785 status = device_register(&musb->g.dev);
1786 if (status != 0) {
1787 put_device(&musb->g.dev);
1788 the_gadget = NULL;
1790 return status;
1793 void musb_gadget_cleanup(struct musb *musb)
1795 if (musb != the_gadget)
1796 return;
1798 device_unregister(&musb->g.dev);
1799 the_gadget = NULL;
1803 * Register the gadget driver. Used by gadget drivers when
1804 * registering themselves with the controller.
1806 * -EINVAL something went wrong (not driver)
1807 * -EBUSY another gadget is already using the controller
1808 * -ENOMEM no memory to perform the operation
1810 * @param driver the gadget driver
1811 * @param bind the driver's bind function
1812 * @return <0 if error, 0 if everything is fine
1814 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1815 int (*bind)(struct usb_gadget *))
1817 struct musb *musb = the_gadget;
1818 unsigned long flags;
1819 int retval = -EINVAL;
1821 if (!driver
1822 || driver->speed != USB_SPEED_HIGH
1823 || !bind || !driver->setup)
1824 goto err0;
1826 /* driver must be initialized to support peripheral mode */
1827 if (!musb) {
1828 dev_dbg(musb->controller, "no dev??\n");
1829 retval = -ENODEV;
1830 goto err0;
1833 pm_runtime_get_sync(musb->controller);
1835 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
1837 if (musb->gadget_driver) {
1838 dev_dbg(musb->controller, "%s is already bound to %s\n",
1839 musb_driver_name,
1840 musb->gadget_driver->driver.name);
1841 retval = -EBUSY;
1842 goto err0;
1845 spin_lock_irqsave(&musb->lock, flags);
1846 musb->gadget_driver = driver;
1847 musb->g.dev.driver = &driver->driver;
1848 driver->driver.bus = NULL;
1849 musb->softconnect = 1;
1850 spin_unlock_irqrestore(&musb->lock, flags);
1852 retval = bind(&musb->g);
1853 if (retval) {
1854 dev_dbg(musb->controller, "bind to driver %s failed --> %d\n",
1855 driver->driver.name, retval);
1856 goto err1;
1859 spin_lock_irqsave(&musb->lock, flags);
1861 otg_set_peripheral(musb->xceiv, &musb->g);
1862 musb->xceiv->state = OTG_STATE_B_IDLE;
1863 musb->is_active = 1;
1866 * FIXME this ignores the softconnect flag. Drivers are
1867 * allowed hold the peripheral inactive until for example
1868 * userspace hooks up printer hardware or DSP codecs, so
1869 * hosts only see fully functional devices.
1872 if (!is_otg_enabled(musb))
1873 musb_start(musb);
1875 otg_set_peripheral(musb->xceiv, &musb->g);
1877 spin_unlock_irqrestore(&musb->lock, flags);
1879 if (is_otg_enabled(musb)) {
1880 struct usb_hcd *hcd = musb_to_hcd(musb);
1882 dev_dbg(musb->controller, "OTG startup...\n");
1884 /* REVISIT: funcall to other code, which also
1885 * handles power budgeting ... this way also
1886 * ensures HdrcStart is indirectly called.
1888 retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
1889 if (retval < 0) {
1890 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
1891 goto err2;
1894 if ((musb->xceiv->last_event == USB_EVENT_ID)
1895 && musb->xceiv->set_vbus)
1896 otg_set_vbus(musb->xceiv, 1);
1898 hcd->self.uses_pio_for_control = 1;
1900 if (musb->xceiv->last_event == USB_EVENT_NONE)
1901 pm_runtime_put(musb->controller);
1903 return 0;
1905 err2:
1906 if (!is_otg_enabled(musb))
1907 musb_stop(musb);
1909 err1:
1910 musb->gadget_driver = NULL;
1911 musb->g.dev.driver = NULL;
1913 err0:
1914 return retval;
1916 EXPORT_SYMBOL(usb_gadget_probe_driver);
1918 static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1920 int i;
1921 struct musb_hw_ep *hw_ep;
1923 /* don't disconnect if it's not connected */
1924 if (musb->g.speed == USB_SPEED_UNKNOWN)
1925 driver = NULL;
1926 else
1927 musb->g.speed = USB_SPEED_UNKNOWN;
1929 /* deactivate the hardware */
1930 if (musb->softconnect) {
1931 musb->softconnect = 0;
1932 musb_pullup(musb, 0);
1934 musb_stop(musb);
1936 /* killing any outstanding requests will quiesce the driver;
1937 * then report disconnect
1939 if (driver) {
1940 for (i = 0, hw_ep = musb->endpoints;
1941 i < musb->nr_endpoints;
1942 i++, hw_ep++) {
1943 musb_ep_select(musb->mregs, i);
1944 if (hw_ep->is_shared_fifo /* || !epnum */) {
1945 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1946 } else {
1947 if (hw_ep->max_packet_sz_tx)
1948 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1949 if (hw_ep->max_packet_sz_rx)
1950 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1954 spin_unlock(&musb->lock);
1955 driver->disconnect(&musb->g);
1956 spin_lock(&musb->lock);
1961 * Unregister the gadget driver. Used by gadget drivers when
1962 * unregistering themselves from the controller.
1964 * @param driver the gadget driver to unregister
1966 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1968 struct musb *musb = the_gadget;
1969 unsigned long flags;
1971 if (!driver || !driver->unbind || !musb)
1972 return -EINVAL;
1974 if (!musb->gadget_driver)
1975 return -EINVAL;
1977 if (musb->xceiv->last_event == USB_EVENT_NONE)
1978 pm_runtime_get_sync(musb->controller);
1981 * REVISIT always use otg_set_peripheral() here too;
1982 * this needs to shut down the OTG engine.
1985 spin_lock_irqsave(&musb->lock, flags);
1987 #ifdef CONFIG_USB_MUSB_OTG
1988 musb_hnp_stop(musb);
1989 #endif
1991 (void) musb_gadget_vbus_draw(&musb->g, 0);
1993 musb->xceiv->state = OTG_STATE_UNDEFINED;
1994 stop_activity(musb, driver);
1995 otg_set_peripheral(musb->xceiv, NULL);
1997 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
1999 spin_unlock_irqrestore(&musb->lock, flags);
2000 driver->unbind(&musb->g);
2001 spin_lock_irqsave(&musb->lock, flags);
2003 musb->gadget_driver = NULL;
2004 musb->g.dev.driver = NULL;
2006 musb->is_active = 0;
2007 musb_platform_try_idle(musb, 0);
2008 spin_unlock_irqrestore(&musb->lock, flags);
2010 if (is_otg_enabled(musb)) {
2011 usb_remove_hcd(musb_to_hcd(musb));
2012 /* FIXME we need to be able to register another
2013 * gadget driver here and have everything work;
2014 * that currently misbehaves.
2018 if (!is_otg_enabled(musb))
2019 musb_stop(musb);
2021 pm_runtime_put(musb->controller);
2023 return 0;
2025 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2028 /* ----------------------------------------------------------------------- */
2030 /* lifecycle operations called through plat_uds.c */
2032 void musb_g_resume(struct musb *musb)
2034 musb->is_suspended = 0;
2035 switch (musb->xceiv->state) {
2036 case OTG_STATE_B_IDLE:
2037 break;
2038 case OTG_STATE_B_WAIT_ACON:
2039 case OTG_STATE_B_PERIPHERAL:
2040 musb->is_active = 1;
2041 if (musb->gadget_driver && musb->gadget_driver->resume) {
2042 spin_unlock(&musb->lock);
2043 musb->gadget_driver->resume(&musb->g);
2044 spin_lock(&musb->lock);
2046 break;
2047 default:
2048 WARNING("unhandled RESUME transition (%s)\n",
2049 otg_state_string(musb->xceiv->state));
2053 /* called when SOF packets stop for 3+ msec */
2054 void musb_g_suspend(struct musb *musb)
2056 u8 devctl;
2058 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2059 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2061 switch (musb->xceiv->state) {
2062 case OTG_STATE_B_IDLE:
2063 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2064 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2065 break;
2066 case OTG_STATE_B_PERIPHERAL:
2067 musb->is_suspended = 1;
2068 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2069 spin_unlock(&musb->lock);
2070 musb->gadget_driver->suspend(&musb->g);
2071 spin_lock(&musb->lock);
2073 break;
2074 default:
2075 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2076 * A_PERIPHERAL may need care too
2078 WARNING("unhandled SUSPEND transition (%s)\n",
2079 otg_state_string(musb->xceiv->state));
2083 /* Called during SRP */
2084 void musb_g_wakeup(struct musb *musb)
2086 musb_gadget_wakeup(&musb->g);
2089 /* called when VBUS drops below session threshold, and in other cases */
2090 void musb_g_disconnect(struct musb *musb)
2092 void __iomem *mregs = musb->mregs;
2093 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2095 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2097 /* clear HR */
2098 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2100 /* don't draw vbus until new b-default session */
2101 (void) musb_gadget_vbus_draw(&musb->g, 0);
2103 musb->g.speed = USB_SPEED_UNKNOWN;
2104 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2105 spin_unlock(&musb->lock);
2106 musb->gadget_driver->disconnect(&musb->g);
2107 spin_lock(&musb->lock);
2110 switch (musb->xceiv->state) {
2111 default:
2112 #ifdef CONFIG_USB_MUSB_OTG
2113 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
2114 otg_state_string(musb->xceiv->state));
2115 musb->xceiv->state = OTG_STATE_A_IDLE;
2116 MUSB_HST_MODE(musb);
2117 break;
2118 case OTG_STATE_A_PERIPHERAL:
2119 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2120 MUSB_HST_MODE(musb);
2121 break;
2122 case OTG_STATE_B_WAIT_ACON:
2123 case OTG_STATE_B_HOST:
2124 #endif
2125 case OTG_STATE_B_PERIPHERAL:
2126 case OTG_STATE_B_IDLE:
2127 musb->xceiv->state = OTG_STATE_B_IDLE;
2128 break;
2129 case OTG_STATE_B_SRP_INIT:
2130 break;
2133 musb->is_active = 0;
2136 void musb_g_reset(struct musb *musb)
2137 __releases(musb->lock)
2138 __acquires(musb->lock)
2140 void __iomem *mbase = musb->mregs;
2141 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2142 u8 power;
2144 dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
2145 (devctl & MUSB_DEVCTL_BDEVICE)
2146 ? "B-Device" : "A-Device",
2147 musb_readb(mbase, MUSB_FADDR),
2148 musb->gadget_driver
2149 ? musb->gadget_driver->driver.name
2150 : NULL
2153 /* report disconnect, if we didn't already (flushing EP state) */
2154 if (musb->g.speed != USB_SPEED_UNKNOWN)
2155 musb_g_disconnect(musb);
2157 /* clear HR */
2158 else if (devctl & MUSB_DEVCTL_HR)
2159 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2162 /* what speed did we negotiate? */
2163 power = musb_readb(mbase, MUSB_POWER);
2164 musb->g.speed = (power & MUSB_POWER_HSMODE)
2165 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2167 /* start in USB_STATE_DEFAULT */
2168 musb->is_active = 1;
2169 musb->is_suspended = 0;
2170 MUSB_DEV_MODE(musb);
2171 musb->address = 0;
2172 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2174 musb->may_wakeup = 0;
2175 musb->g.b_hnp_enable = 0;
2176 musb->g.a_alt_hnp_support = 0;
2177 musb->g.a_hnp_support = 0;
2179 /* Normal reset, as B-Device;
2180 * or else after HNP, as A-Device
2182 if (devctl & MUSB_DEVCTL_BDEVICE) {
2183 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2184 musb->g.is_a_peripheral = 0;
2185 } else if (is_otg_enabled(musb)) {
2186 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2187 musb->g.is_a_peripheral = 1;
2188 } else
2189 WARN_ON(1);
2191 /* start with default limits on VBUS power draw */
2192 (void) musb_gadget_vbus_draw(&musb->g,
2193 is_otg_enabled(musb) ? 8 : 100);