OSDN Git Service

a83a139da5bcef906563b1f4226b933a44de23fc
[android-x86/kernel.git] / drivers / usb / musb / musb_gadget.c
1 /*
2  * MUSB OTG driver peripheral support
3  *
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>
8  *
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.
12  *
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.
17  *
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
22  *
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.
33  *
34  */
35
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>
47
48 #include "musb_core.h"
49
50
51 /* MUSB PERIPHERAL status 3-mar-2006:
52  *
53  * - EP0 seems solid.  It passes both USBCV and usbtest control cases.
54  *   Minor glitches:
55  *
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?
61  *
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.
66  *
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?
73  *
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
80  *
81  * - ISO not tested ... might work, but only weakly isochronous
82  *
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.)
86  *
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
91  */
92
93 /* ----------------------------------------------------------------------- */
94
95 /*
96  * Immediately complete a request.
97  *
98  * @param request the request to complete
99  * @param status the status to complete the request with
100  * Context: controller locked, IRQs blocked.
101  */
102 void musb_g_giveback(
103         struct musb_ep          *ep,
104         struct usb_request      *request,
105         int                     status)
106 __releases(ep->musb->lock)
107 __acquires(ep->musb->lock)
108 {
109         struct musb_request     *req;
110         struct musb             *musb;
111         int                     busy = ep->busy;
112
113         req = to_musb_request(request);
114
115         list_del(&request->list);
116         if (req->request.status == -EINPROGRESS)
117                 req->request.status = status;
118         musb = req->musb;
119
120         ep->busy = 1;
121         spin_unlock(&musb->lock);
122         if (is_dma_capable()) {
123                 if (req->mapped) {
124                         dma_unmap_single(musb->controller,
125                                         req->request.dma,
126                                         req->request.length,
127                                         req->tx
128                                                 ? DMA_TO_DEVICE
129                                                 : DMA_FROM_DEVICE);
130                         req->request.dma = DMA_ADDR_INVALID;
131                         req->mapped = 0;
132                 } else if (req->request.dma != DMA_ADDR_INVALID)
133                         dma_sync_single_for_cpu(musb->controller,
134                                         req->request.dma,
135                                         req->request.length,
136                                         req->tx
137                                                 ? DMA_TO_DEVICE
138                                                 : DMA_FROM_DEVICE);
139         }
140         if (request->status == 0)
141                 DBG(5, "%s done request %p,  %d/%d\n",
142                                 ep->end_point.name, request,
143                                 req->request.actual, req->request.length);
144         else
145                 DBG(2, "%s request %p, %d/%d fault %d\n",
146                                 ep->end_point.name, request,
147                                 req->request.actual, req->request.length,
148                                 request->status);
149         req->request.complete(&req->ep->end_point, &req->request);
150         spin_lock(&musb->lock);
151         ep->busy = busy;
152 }
153
154 /* ----------------------------------------------------------------------- */
155
156 /*
157  * Abort requests queued to an endpoint using the status. Synchronous.
158  * caller locked controller and blocked irqs, and selected this ep.
159  */
160 static void nuke(struct musb_ep *ep, const int status)
161 {
162         struct musb_request     *req = NULL;
163         void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
164
165         ep->busy = 1;
166
167         if (is_dma_capable() && ep->dma) {
168                 struct dma_controller   *c = ep->musb->dma_controller;
169                 int value;
170
171                 if (ep->is_in) {
172                         /*
173                          * The programming guide says that we must not clear
174                          * the DMAMODE bit before DMAENAB, so we only
175                          * clear it in the second write...
176                          */
177                         musb_writew(epio, MUSB_TXCSR,
178                                     MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
179                         musb_writew(epio, MUSB_TXCSR,
180                                         0 | MUSB_TXCSR_FLUSHFIFO);
181                 } else {
182                         musb_writew(epio, MUSB_RXCSR,
183                                         0 | MUSB_RXCSR_FLUSHFIFO);
184                         musb_writew(epio, MUSB_RXCSR,
185                                         0 | MUSB_RXCSR_FLUSHFIFO);
186                 }
187
188                 value = c->channel_abort(ep->dma);
189                 DBG(value ? 1 : 6, "%s: abort DMA --> %d\n", ep->name, value);
190                 c->channel_release(ep->dma);
191                 ep->dma = NULL;
192         }
193
194         while (!list_empty(&(ep->req_list))) {
195                 req = container_of(ep->req_list.next, struct musb_request,
196                                 request.list);
197                 musb_g_giveback(ep, &req->request, status);
198         }
199 }
200
201 /* ----------------------------------------------------------------------- */
202
203 /* Data transfers - pure PIO, pure DMA, or mixed mode */
204
205 /*
206  * This assumes the separate CPPI engine is responding to DMA requests
207  * from the usb core ... sequenced a bit differently from mentor dma.
208  */
209
210 static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
211 {
212         if (can_bulk_split(musb, ep->type))
213                 return ep->hw_ep->max_packet_sz_tx;
214         else
215                 return ep->packet_sz;
216 }
217
218
219 #ifdef CONFIG_USB_INVENTRA_DMA
220
221 /* Peripheral tx (IN) using Mentor DMA works as follows:
222         Only mode 0 is used for transfers <= wPktSize,
223         mode 1 is used for larger transfers,
224
225         One of the following happens:
226         - Host sends IN token which causes an endpoint interrupt
227                 -> TxAvail
228                         -> if DMA is currently busy, exit.
229                         -> if queue is non-empty, txstate().
230
231         - Request is queued by the gadget driver.
232                 -> if queue was previously empty, txstate()
233
234         txstate()
235                 -> start
236                   /\    -> setup DMA
237                   |     (data is transferred to the FIFO, then sent out when
238                   |     IN token(s) are recd from Host.
239                   |             -> DMA interrupt on completion
240                   |                calls TxAvail.
241                   |                   -> stop DMA, ~DMAENAB,
242                   |                   -> set TxPktRdy for last short pkt or zlp
243                   |                   -> Complete Request
244                   |                   -> Continue next request (call txstate)
245                   |___________________________________|
246
247  * Non-Mentor DMA engines can of course work differently, such as by
248  * upleveling from irq-per-packet to irq-per-buffer.
249  */
250
251 #endif
252
253 /*
254  * An endpoint is transmitting data. This can be called either from
255  * the IRQ routine or from ep.queue() to kickstart a request on an
256  * endpoint.
257  *
258  * Context: controller locked, IRQs blocked, endpoint selected
259  */
260 static void txstate(struct musb *musb, struct musb_request *req)
261 {
262         u8                      epnum = req->epnum;
263         struct musb_ep          *musb_ep;
264         void __iomem            *epio = musb->endpoints[epnum].regs;
265         struct usb_request      *request;
266         u16                     fifo_count = 0, csr;
267         int                     use_dma = 0;
268
269         musb_ep = req->ep;
270
271         /* we shouldn't get here while DMA is active ... but we do ... */
272         if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
273                 DBG(4, "dma pending...\n");
274                 return;
275         }
276
277         /* read TXCSR before */
278         csr = musb_readw(epio, MUSB_TXCSR);
279
280         request = &req->request;
281         fifo_count = min(max_ep_writesize(musb, musb_ep),
282                         (int)(request->length - request->actual));
283
284         if (csr & MUSB_TXCSR_TXPKTRDY) {
285                 DBG(5, "%s old packet still ready , txcsr %03x\n",
286                                 musb_ep->end_point.name, csr);
287                 return;
288         }
289
290         if (csr & MUSB_TXCSR_P_SENDSTALL) {
291                 DBG(5, "%s stalling, txcsr %03x\n",
292                                 musb_ep->end_point.name, csr);
293                 return;
294         }
295
296         DBG(4, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
297                         epnum, musb_ep->packet_sz, fifo_count,
298                         csr);
299
300 #ifndef CONFIG_MUSB_PIO_ONLY
301         if (is_dma_capable() && musb_ep->dma) {
302                 struct dma_controller   *c = musb->dma_controller;
303
304                 use_dma = (request->dma != DMA_ADDR_INVALID);
305
306                 /* MUSB_TXCSR_P_ISO is still set correctly */
307
308 #ifdef CONFIG_USB_INVENTRA_DMA
309                 {
310                         size_t request_size;
311
312                         /* setup DMA, then program endpoint CSR */
313                         request_size = min_t(size_t, request->length,
314                                                 musb_ep->dma->max_len);
315                         if (request_size < musb_ep->packet_sz)
316                                 musb_ep->dma->desired_mode = 0;
317                         else
318                                 musb_ep->dma->desired_mode = 1;
319
320                         use_dma = use_dma && c->channel_program(
321                                         musb_ep->dma, musb_ep->packet_sz,
322                                         musb_ep->dma->desired_mode,
323                                         request->dma + request->actual, request_size);
324                         if (use_dma) {
325                                 if (musb_ep->dma->desired_mode == 0) {
326                                         /*
327                                          * We must not clear the DMAMODE bit
328                                          * before the DMAENAB bit -- and the
329                                          * latter doesn't always get cleared
330                                          * before we get here...
331                                          */
332                                         csr &= ~(MUSB_TXCSR_AUTOSET
333                                                 | MUSB_TXCSR_DMAENAB);
334                                         musb_writew(epio, MUSB_TXCSR, csr
335                                                 | MUSB_TXCSR_P_WZC_BITS);
336                                         csr &= ~MUSB_TXCSR_DMAMODE;
337                                         csr |= (MUSB_TXCSR_DMAENAB |
338                                                         MUSB_TXCSR_MODE);
339                                         /* against programming guide */
340                                 } else
341                                         csr |= (MUSB_TXCSR_AUTOSET
342                                                         | MUSB_TXCSR_DMAENAB
343                                                         | MUSB_TXCSR_DMAMODE
344                                                         | MUSB_TXCSR_MODE);
345
346                                 csr &= ~MUSB_TXCSR_P_UNDERRUN;
347                                 musb_writew(epio, MUSB_TXCSR, csr);
348                         }
349                 }
350
351 #elif defined(CONFIG_USB_TI_CPPI_DMA)
352                 /* program endpoint CSR first, then setup DMA */
353                 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
354                 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
355                        MUSB_TXCSR_MODE;
356                 musb_writew(epio, MUSB_TXCSR,
357                         (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
358                                 | csr);
359
360                 /* ensure writebuffer is empty */
361                 csr = musb_readw(epio, MUSB_TXCSR);
362
363                 /* NOTE host side sets DMAENAB later than this; both are
364                  * OK since the transfer dma glue (between CPPI and Mentor
365                  * fifos) just tells CPPI it could start.  Data only moves
366                  * to the USB TX fifo when both fifos are ready.
367                  */
368
369                 /* "mode" is irrelevant here; handle terminating ZLPs like
370                  * PIO does, since the hardware RNDIS mode seems unreliable
371                  * except for the last-packet-is-already-short case.
372                  */
373                 use_dma = use_dma && c->channel_program(
374                                 musb_ep->dma, musb_ep->packet_sz,
375                                 0,
376                                 request->dma,
377                                 request->length);
378                 if (!use_dma) {
379                         c->channel_release(musb_ep->dma);
380                         musb_ep->dma = NULL;
381                         csr &= ~MUSB_TXCSR_DMAENAB;
382                         musb_writew(epio, MUSB_TXCSR, csr);
383                         /* invariant: prequest->buf is non-null */
384                 }
385 #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
386                 use_dma = use_dma && c->channel_program(
387                                 musb_ep->dma, musb_ep->packet_sz,
388                                 request->zero,
389                                 request->dma,
390                                 request->length);
391 #endif
392         }
393 #endif
394
395         if (!use_dma) {
396                 musb_write_fifo(musb_ep->hw_ep, fifo_count,
397                                 (u8 *) (request->buf + request->actual));
398                 request->actual += fifo_count;
399                 csr |= MUSB_TXCSR_TXPKTRDY;
400                 csr &= ~MUSB_TXCSR_P_UNDERRUN;
401                 musb_writew(epio, MUSB_TXCSR, csr);
402         }
403
404         /* host may already have the data when this message shows... */
405         DBG(3, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
406                         musb_ep->end_point.name, use_dma ? "dma" : "pio",
407                         request->actual, request->length,
408                         musb_readw(epio, MUSB_TXCSR),
409                         fifo_count,
410                         musb_readw(epio, MUSB_TXMAXP));
411 }
412
413 /*
414  * FIFO state update (e.g. data ready).
415  * Called from IRQ,  with controller locked.
416  */
417 void musb_g_tx(struct musb *musb, u8 epnum)
418 {
419         u16                     csr;
420         struct usb_request      *request;
421         u8 __iomem              *mbase = musb->mregs;
422         struct musb_ep          *musb_ep = &musb->endpoints[epnum].ep_in;
423         void __iomem            *epio = musb->endpoints[epnum].regs;
424         struct dma_channel      *dma;
425
426         musb_ep_select(mbase, epnum);
427         request = next_request(musb_ep);
428
429         csr = musb_readw(epio, MUSB_TXCSR);
430         DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
431
432         dma = is_dma_capable() ? musb_ep->dma : NULL;
433
434         /*
435          * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
436          * probably rates reporting as a host error.
437          */
438         if (csr & MUSB_TXCSR_P_SENTSTALL) {
439                 csr |=  MUSB_TXCSR_P_WZC_BITS;
440                 csr &= ~MUSB_TXCSR_P_SENTSTALL;
441                 musb_writew(epio, MUSB_TXCSR, csr);
442                 return;
443         }
444
445         if (csr & MUSB_TXCSR_P_UNDERRUN) {
446                 /* We NAKed, no big deal... little reason to care. */
447                 csr |=   MUSB_TXCSR_P_WZC_BITS;
448                 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
449                 musb_writew(epio, MUSB_TXCSR, csr);
450                 DBG(20, "underrun on ep%d, req %p\n", epnum, request);
451         }
452
453         if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
454                 /*
455                  * SHOULD NOT HAPPEN... has with CPPI though, after
456                  * changing SENDSTALL (and other cases); harmless?
457                  */
458                 DBG(5, "%s dma still busy?\n", musb_ep->end_point.name);
459                 return;
460         }
461
462         if (request) {
463                 u8      is_dma = 0;
464
465                 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
466                         is_dma = 1;
467                         csr |= MUSB_TXCSR_P_WZC_BITS;
468                         csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
469                                  MUSB_TXCSR_TXPKTRDY);
470                         musb_writew(epio, MUSB_TXCSR, csr);
471                         /* Ensure writebuffer is empty. */
472                         csr = musb_readw(epio, MUSB_TXCSR);
473                         request->actual += musb_ep->dma->actual_len;
474                         DBG(4, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
475                                 epnum, csr, musb_ep->dma->actual_len, request);
476                 }
477
478                 if (is_dma || request->actual == request->length) {
479                         /*
480                          * First, maybe a terminating short packet. Some DMA
481                          * engines might handle this by themselves.
482                          */
483                         if ((request->zero && request->length
484                                 && request->length % musb_ep->packet_sz == 0)
485 #ifdef CONFIG_USB_INVENTRA_DMA
486                                 || (is_dma && (!dma->desired_mode ||
487                                         (request->actual &
488                                                 (musb_ep->packet_sz - 1))))
489 #endif
490                         ) {
491                                 /*
492                                  * On DMA completion, FIFO may not be
493                                  * available yet...
494                                  */
495                                 if (csr & MUSB_TXCSR_TXPKTRDY)
496                                         return;
497
498                                 DBG(4, "sending zero pkt\n");
499                                 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
500                                                 | MUSB_TXCSR_TXPKTRDY);
501                                 request->zero = 0;
502                         }
503
504                         /* ... or if not, then complete it. */
505                         musb_g_giveback(musb_ep, request, 0);
506
507                         /*
508                          * Kickstart next transfer if appropriate;
509                          * the packet that just completed might not
510                          * be transmitted for hours or days.
511                          * REVISIT for double buffering...
512                          * FIXME revisit for stalls too...
513                          */
514                         musb_ep_select(mbase, epnum);
515                         csr = musb_readw(epio, MUSB_TXCSR);
516                         if (csr & MUSB_TXCSR_FIFONOTEMPTY)
517                                 return;
518
519                         request = musb_ep->desc ? next_request(musb_ep) : NULL;
520                         if (!request) {
521                                 DBG(4, "%s idle now\n",
522                                         musb_ep->end_point.name);
523                                 return;
524                         }
525                 }
526
527                 txstate(musb, to_musb_request(request));
528         }
529 }
530
531 /* ------------------------------------------------------------ */
532
533 #ifdef CONFIG_USB_INVENTRA_DMA
534
535 /* Peripheral rx (OUT) using Mentor DMA works as follows:
536         - Only mode 0 is used.
537
538         - Request is queued by the gadget class driver.
539                 -> if queue was previously empty, rxstate()
540
541         - Host sends OUT token which causes an endpoint interrupt
542           /\      -> RxReady
543           |           -> if request queued, call rxstate
544           |             /\      -> setup DMA
545           |             |            -> DMA interrupt on completion
546           |             |               -> RxReady
547           |             |                     -> stop DMA
548           |             |                     -> ack the read
549           |             |                     -> if data recd = max expected
550           |             |                               by the request, or host
551           |             |                               sent a short packet,
552           |             |                               complete the request,
553           |             |                               and start the next one.
554           |             |_____________________________________|
555           |                                      else just wait for the host
556           |                                         to send the next OUT token.
557           |__________________________________________________|
558
559  * Non-Mentor DMA engines can of course work differently.
560  */
561
562 #endif
563
564 /*
565  * Context: controller locked, IRQs blocked, endpoint selected
566  */
567 static void rxstate(struct musb *musb, struct musb_request *req)
568 {
569         const u8                epnum = req->epnum;
570         struct usb_request      *request = &req->request;
571         struct musb_ep          *musb_ep = &musb->endpoints[epnum].ep_out;
572         void __iomem            *epio = musb->endpoints[epnum].regs;
573         unsigned                fifo_count = 0;
574         u16                     len = musb_ep->packet_sz;
575         u16                     csr = musb_readw(epio, MUSB_RXCSR);
576
577         /* We shouldn't get here while DMA is active, but we do... */
578         if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
579                 DBG(4, "DMA pending...\n");
580                 return;
581         }
582
583         if (csr & MUSB_RXCSR_P_SENDSTALL) {
584                 DBG(5, "%s stalling, RXCSR %04x\n",
585                     musb_ep->end_point.name, csr);
586                 return;
587         }
588
589         if (is_cppi_enabled() && musb_ep->dma) {
590                 struct dma_controller   *c = musb->dma_controller;
591                 struct dma_channel      *channel = musb_ep->dma;
592
593                 /* NOTE:  CPPI won't actually stop advancing the DMA
594                  * queue after short packet transfers, so this is almost
595                  * always going to run as IRQ-per-packet DMA so that
596                  * faults will be handled correctly.
597                  */
598                 if (c->channel_program(channel,
599                                 musb_ep->packet_sz,
600                                 !request->short_not_ok,
601                                 request->dma + request->actual,
602                                 request->length - request->actual)) {
603
604                         /* make sure that if an rxpkt arrived after the irq,
605                          * the cppi engine will be ready to take it as soon
606                          * as DMA is enabled
607                          */
608                         csr &= ~(MUSB_RXCSR_AUTOCLEAR
609                                         | MUSB_RXCSR_DMAMODE);
610                         csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
611                         musb_writew(epio, MUSB_RXCSR, csr);
612                         return;
613                 }
614         }
615
616         if (csr & MUSB_RXCSR_RXPKTRDY) {
617                 len = musb_readw(epio, MUSB_RXCOUNT);
618                 if (request->actual < request->length) {
619 #ifdef CONFIG_USB_INVENTRA_DMA
620                         if (is_dma_capable() && musb_ep->dma) {
621                                 struct dma_controller   *c;
622                                 struct dma_channel      *channel;
623                                 int                     use_dma = 0;
624
625                                 c = musb->dma_controller;
626                                 channel = musb_ep->dma;
627
628         /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
629          * mode 0 only. So we do not get endpoint interrupts due to DMA
630          * completion. We only get interrupts from DMA controller.
631          *
632          * We could operate in DMA mode 1 if we knew the size of the tranfer
633          * in advance. For mass storage class, request->length = what the host
634          * sends, so that'd work.  But for pretty much everything else,
635          * request->length is routinely more than what the host sends. For
636          * most these gadgets, end of is signified either by a short packet,
637          * or filling the last byte of the buffer.  (Sending extra data in
638          * that last pckate should trigger an overflow fault.)  But in mode 1,
639          * we don't get DMA completion interrrupt for short packets.
640          *
641          * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
642          * to get endpoint interrupt on every DMA req, but that didn't seem
643          * to work reliably.
644          *
645          * REVISIT an updated g_file_storage can set req->short_not_ok, which
646          * then becomes usable as a runtime "use mode 1" hint...
647          */
648
649                                 csr |= MUSB_RXCSR_DMAENAB;
650 #ifdef USE_MODE1
651                                 csr |= MUSB_RXCSR_AUTOCLEAR;
652                                 /* csr |= MUSB_RXCSR_DMAMODE; */
653
654                                 /* this special sequence (enabling and then
655                                  * disabling MUSB_RXCSR_DMAMODE) is required
656                                  * to get DMAReq to activate
657                                  */
658                                 musb_writew(epio, MUSB_RXCSR,
659                                         csr | MUSB_RXCSR_DMAMODE);
660 #endif
661                                 musb_writew(epio, MUSB_RXCSR, csr);
662
663                                 if (request->actual < request->length) {
664                                         int transfer_size = 0;
665 #ifdef USE_MODE1
666                                         transfer_size = min(request->length,
667                                                         channel->max_len);
668 #else
669                                         transfer_size = len;
670 #endif
671                                         if (transfer_size <= musb_ep->packet_sz)
672                                                 musb_ep->dma->desired_mode = 0;
673                                         else
674                                                 musb_ep->dma->desired_mode = 1;
675
676                                         use_dma = c->channel_program(
677                                                         channel,
678                                                         musb_ep->packet_sz,
679                                                         channel->desired_mode,
680                                                         request->dma
681                                                         + request->actual,
682                                                         transfer_size);
683                                 }
684
685                                 if (use_dma)
686                                         return;
687                         }
688 #endif  /* Mentor's DMA */
689
690                         fifo_count = request->length - request->actual;
691                         DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
692                                         musb_ep->end_point.name,
693                                         len, fifo_count,
694                                         musb_ep->packet_sz);
695
696                         fifo_count = min_t(unsigned, len, fifo_count);
697
698 #ifdef  CONFIG_USB_TUSB_OMAP_DMA
699                         if (tusb_dma_omap() && musb_ep->dma) {
700                                 struct dma_controller *c = musb->dma_controller;
701                                 struct dma_channel *channel = musb_ep->dma;
702                                 u32 dma_addr = request->dma + request->actual;
703                                 int ret;
704
705                                 ret = c->channel_program(channel,
706                                                 musb_ep->packet_sz,
707                                                 channel->desired_mode,
708                                                 dma_addr,
709                                                 fifo_count);
710                                 if (ret)
711                                         return;
712                         }
713 #endif
714
715                         musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
716                                         (request->buf + request->actual));
717                         request->actual += fifo_count;
718
719                         /* REVISIT if we left anything in the fifo, flush
720                          * it and report -EOVERFLOW
721                          */
722
723                         /* ack the read! */
724                         csr |= MUSB_RXCSR_P_WZC_BITS;
725                         csr &= ~MUSB_RXCSR_RXPKTRDY;
726                         musb_writew(epio, MUSB_RXCSR, csr);
727                 }
728         }
729
730         /* reach the end or short packet detected */
731         if (request->actual == request->length || len < musb_ep->packet_sz)
732                 musb_g_giveback(musb_ep, request, 0);
733 }
734
735 /*
736  * Data ready for a request; called from IRQ
737  */
738 void musb_g_rx(struct musb *musb, u8 epnum)
739 {
740         u16                     csr;
741         struct usb_request      *request;
742         void __iomem            *mbase = musb->mregs;
743         struct musb_ep          *musb_ep = &musb->endpoints[epnum].ep_out;
744         void __iomem            *epio = musb->endpoints[epnum].regs;
745         struct dma_channel      *dma;
746
747         musb_ep_select(mbase, epnum);
748
749         request = next_request(musb_ep);
750         if (!request)
751                 return;
752
753         csr = musb_readw(epio, MUSB_RXCSR);
754         dma = is_dma_capable() ? musb_ep->dma : NULL;
755
756         DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
757                         csr, dma ? " (dma)" : "", request);
758
759         if (csr & MUSB_RXCSR_P_SENTSTALL) {
760                 csr |= MUSB_RXCSR_P_WZC_BITS;
761                 csr &= ~MUSB_RXCSR_P_SENTSTALL;
762                 musb_writew(epio, MUSB_RXCSR, csr);
763                 return;
764         }
765
766         if (csr & MUSB_RXCSR_P_OVERRUN) {
767                 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
768                 csr &= ~MUSB_RXCSR_P_OVERRUN;
769                 musb_writew(epio, MUSB_RXCSR, csr);
770
771                 DBG(3, "%s iso overrun on %p\n", musb_ep->name, request);
772                 if (request && request->status == -EINPROGRESS)
773                         request->status = -EOVERFLOW;
774         }
775         if (csr & MUSB_RXCSR_INCOMPRX) {
776                 /* REVISIT not necessarily an error */
777                 DBG(4, "%s, incomprx\n", musb_ep->end_point.name);
778         }
779
780         if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
781                 /* "should not happen"; likely RXPKTRDY pending for DMA */
782                 DBG((csr & MUSB_RXCSR_DMAENAB) ? 4 : 1,
783                         "%s busy, csr %04x\n",
784                         musb_ep->end_point.name, csr);
785                 return;
786         }
787
788         if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
789                 csr &= ~(MUSB_RXCSR_AUTOCLEAR
790                                 | MUSB_RXCSR_DMAENAB
791                                 | MUSB_RXCSR_DMAMODE);
792                 musb_writew(epio, MUSB_RXCSR,
793                         MUSB_RXCSR_P_WZC_BITS | csr);
794
795                 request->actual += musb_ep->dma->actual_len;
796
797                 DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
798                         epnum, csr,
799                         musb_readw(epio, MUSB_RXCSR),
800                         musb_ep->dma->actual_len, request);
801
802 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
803                 /* Autoclear doesn't clear RxPktRdy for short packets */
804                 if ((dma->desired_mode == 0)
805                                 || (dma->actual_len
806                                         & (musb_ep->packet_sz - 1))) {
807                         /* ack the read! */
808                         csr &= ~MUSB_RXCSR_RXPKTRDY;
809                         musb_writew(epio, MUSB_RXCSR, csr);
810                 }
811
812                 /* incomplete, and not short? wait for next IN packet */
813                 if ((request->actual < request->length)
814                                 && (musb_ep->dma->actual_len
815                                         == musb_ep->packet_sz))
816                         return;
817 #endif
818                 musb_g_giveback(musb_ep, request, 0);
819
820                 request = next_request(musb_ep);
821                 if (!request)
822                         return;
823         }
824
825         /* analyze request if the ep is hot */
826         if (request)
827                 rxstate(musb, to_musb_request(request));
828         else
829                 DBG(3, "packet waiting for %s%s request\n",
830                                 musb_ep->desc ? "" : "inactive ",
831                                 musb_ep->end_point.name);
832 }
833
834 /* ------------------------------------------------------------ */
835
836 static int musb_gadget_enable(struct usb_ep *ep,
837                         const struct usb_endpoint_descriptor *desc)
838 {
839         unsigned long           flags;
840         struct musb_ep          *musb_ep;
841         struct musb_hw_ep       *hw_ep;
842         void __iomem            *regs;
843         struct musb             *musb;
844         void __iomem    *mbase;
845         u8              epnum;
846         u16             csr;
847         unsigned        tmp;
848         int             status = -EINVAL;
849
850         if (!ep || !desc)
851                 return -EINVAL;
852
853         musb_ep = to_musb_ep(ep);
854         hw_ep = musb_ep->hw_ep;
855         regs = hw_ep->regs;
856         musb = musb_ep->musb;
857         mbase = musb->mregs;
858         epnum = musb_ep->current_epnum;
859
860         spin_lock_irqsave(&musb->lock, flags);
861
862         if (musb_ep->desc) {
863                 status = -EBUSY;
864                 goto fail;
865         }
866         musb_ep->type = usb_endpoint_type(desc);
867
868         /* check direction and (later) maxpacket size against endpoint */
869         if (usb_endpoint_num(desc) != epnum)
870                 goto fail;
871
872         /* REVISIT this rules out high bandwidth periodic transfers */
873         tmp = le16_to_cpu(desc->wMaxPacketSize);
874         if (tmp & ~0x07ff)
875                 goto fail;
876         musb_ep->packet_sz = tmp;
877
878         /* enable the interrupts for the endpoint, set the endpoint
879          * packet size (or fail), set the mode, clear the fifo
880          */
881         musb_ep_select(mbase, epnum);
882         if (usb_endpoint_dir_in(desc)) {
883                 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
884
885                 if (hw_ep->is_shared_fifo)
886                         musb_ep->is_in = 1;
887                 if (!musb_ep->is_in)
888                         goto fail;
889                 if (tmp > hw_ep->max_packet_sz_tx)
890                         goto fail;
891
892                 int_txe |= (1 << epnum);
893                 musb_writew(mbase, MUSB_INTRTXE, int_txe);
894
895                 /* REVISIT if can_bulk_split(), use by updating "tmp";
896                  * likewise high bandwidth periodic tx
897                  */
898                 /* Set TXMAXP with the FIFO size of the endpoint
899                  * to disable double buffering mode. Currently, It seems that double
900                  * buffering has problem if musb RTL revision number < 2.0.
901                  */
902                 if (musb->hwvers < MUSB_HWVERS_2000)
903                         musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
904                 else
905                         musb_writew(regs, MUSB_TXMAXP, tmp);
906
907                 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
908                 if (musb_readw(regs, MUSB_TXCSR)
909                                 & MUSB_TXCSR_FIFONOTEMPTY)
910                         csr |= MUSB_TXCSR_FLUSHFIFO;
911                 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
912                         csr |= MUSB_TXCSR_P_ISO;
913
914                 /* set twice in case of double buffering */
915                 musb_writew(regs, MUSB_TXCSR, csr);
916                 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
917                 musb_writew(regs, MUSB_TXCSR, csr);
918
919         } else {
920                 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
921
922                 if (hw_ep->is_shared_fifo)
923                         musb_ep->is_in = 0;
924                 if (musb_ep->is_in)
925                         goto fail;
926                 if (tmp > hw_ep->max_packet_sz_rx)
927                         goto fail;
928
929                 int_rxe |= (1 << epnum);
930                 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
931
932                 /* REVISIT if can_bulk_combine() use by updating "tmp"
933                  * likewise high bandwidth periodic rx
934                  */
935                 /* Set RXMAXP with the FIFO size of the endpoint
936                  * to disable double buffering mode.
937                  */
938                 if (musb->hwvers < MUSB_HWVERS_2000)
939                         musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_rx);
940                 else
941                         musb_writew(regs, MUSB_RXMAXP, tmp);
942
943                 /* force shared fifo to OUT-only mode */
944                 if (hw_ep->is_shared_fifo) {
945                         csr = musb_readw(regs, MUSB_TXCSR);
946                         csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
947                         musb_writew(regs, MUSB_TXCSR, csr);
948                 }
949
950                 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
951                 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
952                         csr |= MUSB_RXCSR_P_ISO;
953                 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
954                         csr |= MUSB_RXCSR_DISNYET;
955
956                 /* set twice in case of double buffering */
957                 musb_writew(regs, MUSB_RXCSR, csr);
958                 musb_writew(regs, MUSB_RXCSR, csr);
959         }
960
961         /* NOTE:  all the I/O code _should_ work fine without DMA, in case
962          * for some reason you run out of channels here.
963          */
964         if (is_dma_capable() && musb->dma_controller) {
965                 struct dma_controller   *c = musb->dma_controller;
966
967                 musb_ep->dma = c->channel_alloc(c, hw_ep,
968                                 (desc->bEndpointAddress & USB_DIR_IN));
969         } else
970                 musb_ep->dma = NULL;
971
972         musb_ep->desc = desc;
973         musb_ep->busy = 0;
974         musb_ep->wedged = 0;
975         status = 0;
976
977         pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
978                         musb_driver_name, musb_ep->end_point.name,
979                         ({ char *s; switch (musb_ep->type) {
980                         case USB_ENDPOINT_XFER_BULK:    s = "bulk"; break;
981                         case USB_ENDPOINT_XFER_INT:     s = "int"; break;
982                         default:                        s = "iso"; break;
983                         }; s; }),
984                         musb_ep->is_in ? "IN" : "OUT",
985                         musb_ep->dma ? "dma, " : "",
986                         musb_ep->packet_sz);
987
988         schedule_work(&musb->irq_work);
989
990 fail:
991         spin_unlock_irqrestore(&musb->lock, flags);
992         return status;
993 }
994
995 /*
996  * Disable an endpoint flushing all requests queued.
997  */
998 static int musb_gadget_disable(struct usb_ep *ep)
999 {
1000         unsigned long   flags;
1001         struct musb     *musb;
1002         u8              epnum;
1003         struct musb_ep  *musb_ep;
1004         void __iomem    *epio;
1005         int             status = 0;
1006
1007         musb_ep = to_musb_ep(ep);
1008         musb = musb_ep->musb;
1009         epnum = musb_ep->current_epnum;
1010         epio = musb->endpoints[epnum].regs;
1011
1012         spin_lock_irqsave(&musb->lock, flags);
1013         musb_ep_select(musb->mregs, epnum);
1014
1015         /* zero the endpoint sizes */
1016         if (musb_ep->is_in) {
1017                 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1018                 int_txe &= ~(1 << epnum);
1019                 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1020                 musb_writew(epio, MUSB_TXMAXP, 0);
1021         } else {
1022                 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1023                 int_rxe &= ~(1 << epnum);
1024                 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1025                 musb_writew(epio, MUSB_RXMAXP, 0);
1026         }
1027
1028         musb_ep->desc = NULL;
1029
1030         /* abort all pending DMA and requests */
1031         nuke(musb_ep, -ESHUTDOWN);
1032
1033         schedule_work(&musb->irq_work);
1034
1035         spin_unlock_irqrestore(&(musb->lock), flags);
1036
1037         DBG(2, "%s\n", musb_ep->end_point.name);
1038
1039         return status;
1040 }
1041
1042 /*
1043  * Allocate a request for an endpoint.
1044  * Reused by ep0 code.
1045  */
1046 struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1047 {
1048         struct musb_ep          *musb_ep = to_musb_ep(ep);
1049         struct musb_request     *request = NULL;
1050
1051         request = kzalloc(sizeof *request, gfp_flags);
1052         if (request) {
1053                 INIT_LIST_HEAD(&request->request.list);
1054                 request->request.dma = DMA_ADDR_INVALID;
1055                 request->epnum = musb_ep->current_epnum;
1056                 request->ep = musb_ep;
1057         }
1058
1059         return &request->request;
1060 }
1061
1062 /*
1063  * Free a request
1064  * Reused by ep0 code.
1065  */
1066 void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1067 {
1068         kfree(to_musb_request(req));
1069 }
1070
1071 static LIST_HEAD(buffers);
1072
1073 struct free_record {
1074         struct list_head        list;
1075         struct device           *dev;
1076         unsigned                bytes;
1077         dma_addr_t              dma;
1078 };
1079
1080 /*
1081  * Context: controller locked, IRQs blocked.
1082  */
1083 static void musb_ep_restart(struct musb *musb, struct musb_request *req)
1084 {
1085         DBG(3, "<== %s request %p len %u on hw_ep%d\n",
1086                 req->tx ? "TX/IN" : "RX/OUT",
1087                 &req->request, req->request.length, req->epnum);
1088
1089         musb_ep_select(musb->mregs, req->epnum);
1090         if (req->tx)
1091                 txstate(musb, req);
1092         else
1093                 rxstate(musb, req);
1094 }
1095
1096 static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1097                         gfp_t gfp_flags)
1098 {
1099         struct musb_ep          *musb_ep;
1100         struct musb_request     *request;
1101         struct musb             *musb;
1102         int                     status = 0;
1103         unsigned long           lockflags;
1104
1105         if (!ep || !req)
1106                 return -EINVAL;
1107         if (!req->buf)
1108                 return -ENODATA;
1109
1110         musb_ep = to_musb_ep(ep);
1111         musb = musb_ep->musb;
1112
1113         request = to_musb_request(req);
1114         request->musb = musb;
1115
1116         if (request->ep != musb_ep)
1117                 return -EINVAL;
1118
1119         DBG(4, "<== to %s request=%p\n", ep->name, req);
1120
1121         /* request is mine now... */
1122         request->request.actual = 0;
1123         request->request.status = -EINPROGRESS;
1124         request->epnum = musb_ep->current_epnum;
1125         request->tx = musb_ep->is_in;
1126
1127         if (is_dma_capable() && musb_ep->dma) {
1128                 if (request->request.dma == DMA_ADDR_INVALID) {
1129                         request->request.dma = dma_map_single(
1130                                         musb->controller,
1131                                         request->request.buf,
1132                                         request->request.length,
1133                                         request->tx
1134                                                 ? DMA_TO_DEVICE
1135                                                 : DMA_FROM_DEVICE);
1136                         request->mapped = 1;
1137                 } else {
1138                         dma_sync_single_for_device(musb->controller,
1139                                         request->request.dma,
1140                                         request->request.length,
1141                                         request->tx
1142                                                 ? DMA_TO_DEVICE
1143                                                 : DMA_FROM_DEVICE);
1144                         request->mapped = 0;
1145                 }
1146         } else if (!req->buf) {
1147                 return -ENODATA;
1148         } else
1149                 request->mapped = 0;
1150
1151         spin_lock_irqsave(&musb->lock, lockflags);
1152
1153         /* don't queue if the ep is down */
1154         if (!musb_ep->desc) {
1155                 DBG(4, "req %p queued to %s while ep %s\n",
1156                                 req, ep->name, "disabled");
1157                 status = -ESHUTDOWN;
1158                 goto cleanup;
1159         }
1160
1161         /* add request to the list */
1162         list_add_tail(&(request->request.list), &(musb_ep->req_list));
1163
1164         /* it this is the head of the queue, start i/o ... */
1165         if (!musb_ep->busy && &request->request.list == musb_ep->req_list.next)
1166                 musb_ep_restart(musb, request);
1167
1168 cleanup:
1169         spin_unlock_irqrestore(&musb->lock, lockflags);
1170         return status;
1171 }
1172
1173 static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1174 {
1175         struct musb_ep          *musb_ep = to_musb_ep(ep);
1176         struct usb_request      *r;
1177         unsigned long           flags;
1178         int                     status = 0;
1179         struct musb             *musb = musb_ep->musb;
1180
1181         if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1182                 return -EINVAL;
1183
1184         spin_lock_irqsave(&musb->lock, flags);
1185
1186         list_for_each_entry(r, &musb_ep->req_list, list) {
1187                 if (r == request)
1188                         break;
1189         }
1190         if (r != request) {
1191                 DBG(3, "request %p not queued to %s\n", request, ep->name);
1192                 status = -EINVAL;
1193                 goto done;
1194         }
1195
1196         /* if the hardware doesn't have the request, easy ... */
1197         if (musb_ep->req_list.next != &request->list || musb_ep->busy)
1198                 musb_g_giveback(musb_ep, request, -ECONNRESET);
1199
1200         /* ... else abort the dma transfer ... */
1201         else if (is_dma_capable() && musb_ep->dma) {
1202                 struct dma_controller   *c = musb->dma_controller;
1203
1204                 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1205                 if (c->channel_abort)
1206                         status = c->channel_abort(musb_ep->dma);
1207                 else
1208                         status = -EBUSY;
1209                 if (status == 0)
1210                         musb_g_giveback(musb_ep, request, -ECONNRESET);
1211         } else {
1212                 /* NOTE: by sticking to easily tested hardware/driver states,
1213                  * we leave counting of in-flight packets imprecise.
1214                  */
1215                 musb_g_giveback(musb_ep, request, -ECONNRESET);
1216         }
1217
1218 done:
1219         spin_unlock_irqrestore(&musb->lock, flags);
1220         return status;
1221 }
1222
1223 /*
1224  * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1225  * data but will queue requests.
1226  *
1227  * exported to ep0 code
1228  */
1229 static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1230 {
1231         struct musb_ep          *musb_ep = to_musb_ep(ep);
1232         u8                      epnum = musb_ep->current_epnum;
1233         struct musb             *musb = musb_ep->musb;
1234         void __iomem            *epio = musb->endpoints[epnum].regs;
1235         void __iomem            *mbase;
1236         unsigned long           flags;
1237         u16                     csr;
1238         struct musb_request     *request;
1239         int                     status = 0;
1240
1241         if (!ep)
1242                 return -EINVAL;
1243         mbase = musb->mregs;
1244
1245         spin_lock_irqsave(&musb->lock, flags);
1246
1247         if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1248                 status = -EINVAL;
1249                 goto done;
1250         }
1251
1252         musb_ep_select(mbase, epnum);
1253
1254         request = to_musb_request(next_request(musb_ep));
1255         if (value) {
1256                 if (request) {
1257                         DBG(3, "request in progress, cannot halt %s\n",
1258                             ep->name);
1259                         status = -EAGAIN;
1260                         goto done;
1261                 }
1262                 /* Cannot portably stall with non-empty FIFO */
1263                 if (musb_ep->is_in) {
1264                         csr = musb_readw(epio, MUSB_TXCSR);
1265                         if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1266                                 DBG(3, "FIFO busy, cannot halt %s\n", ep->name);
1267                                 status = -EAGAIN;
1268                                 goto done;
1269                         }
1270                 }
1271         } else
1272                 musb_ep->wedged = 0;
1273
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                 csr |= MUSB_TXCSR_P_WZC_BITS
1279                         | MUSB_TXCSR_CLRDATATOG;
1280                 if (value)
1281                         csr |= MUSB_TXCSR_P_SENDSTALL;
1282                 else
1283                         csr &= ~(MUSB_TXCSR_P_SENDSTALL
1284                                 | MUSB_TXCSR_P_SENTSTALL);
1285                 csr &= ~MUSB_TXCSR_TXPKTRDY;
1286                 musb_writew(epio, MUSB_TXCSR, csr);
1287         } else {
1288                 csr = musb_readw(epio, MUSB_RXCSR);
1289                 csr |= MUSB_RXCSR_P_WZC_BITS
1290                         | MUSB_RXCSR_FLUSHFIFO
1291                         | MUSB_RXCSR_CLRDATATOG;
1292                 if (value)
1293                         csr |= MUSB_RXCSR_P_SENDSTALL;
1294                 else
1295                         csr &= ~(MUSB_RXCSR_P_SENDSTALL
1296                                 | MUSB_RXCSR_P_SENTSTALL);
1297                 musb_writew(epio, MUSB_RXCSR, csr);
1298         }
1299
1300         /* maybe start the first request in the queue */
1301         if (!musb_ep->busy && !value && request) {
1302                 DBG(3, "restarting the request\n");
1303                 musb_ep_restart(musb, request);
1304         }
1305
1306 done:
1307         spin_unlock_irqrestore(&musb->lock, flags);
1308         return status;
1309 }
1310
1311 /*
1312  * Sets the halt feature with the clear requests ignored
1313  */
1314 static int musb_gadget_set_wedge(struct usb_ep *ep)
1315 {
1316         struct musb_ep          *musb_ep = to_musb_ep(ep);
1317
1318         if (!ep)
1319                 return -EINVAL;
1320
1321         musb_ep->wedged = 1;
1322
1323         return usb_ep_set_halt(ep);
1324 }
1325
1326 static int musb_gadget_fifo_status(struct usb_ep *ep)
1327 {
1328         struct musb_ep          *musb_ep = to_musb_ep(ep);
1329         void __iomem            *epio = musb_ep->hw_ep->regs;
1330         int                     retval = -EINVAL;
1331
1332         if (musb_ep->desc && !musb_ep->is_in) {
1333                 struct musb             *musb = musb_ep->musb;
1334                 int                     epnum = musb_ep->current_epnum;
1335                 void __iomem            *mbase = musb->mregs;
1336                 unsigned long           flags;
1337
1338                 spin_lock_irqsave(&musb->lock, flags);
1339
1340                 musb_ep_select(mbase, epnum);
1341                 /* FIXME return zero unless RXPKTRDY is set */
1342                 retval = musb_readw(epio, MUSB_RXCOUNT);
1343
1344                 spin_unlock_irqrestore(&musb->lock, flags);
1345         }
1346         return retval;
1347 }
1348
1349 static void musb_gadget_fifo_flush(struct usb_ep *ep)
1350 {
1351         struct musb_ep  *musb_ep = to_musb_ep(ep);
1352         struct musb     *musb = musb_ep->musb;
1353         u8              epnum = musb_ep->current_epnum;
1354         void __iomem    *epio = musb->endpoints[epnum].regs;
1355         void __iomem    *mbase;
1356         unsigned long   flags;
1357         u16             csr, int_txe;
1358
1359         mbase = musb->mregs;
1360
1361         spin_lock_irqsave(&musb->lock, flags);
1362         musb_ep_select(mbase, (u8) epnum);
1363
1364         /* disable interrupts */
1365         int_txe = musb_readw(mbase, MUSB_INTRTXE);
1366         musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1367
1368         if (musb_ep->is_in) {
1369                 csr = musb_readw(epio, MUSB_TXCSR);
1370                 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1371                         csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1372                         musb_writew(epio, MUSB_TXCSR, csr);
1373                         /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1374                         musb_writew(epio, MUSB_TXCSR, csr);
1375                 }
1376         } else {
1377                 csr = musb_readw(epio, MUSB_RXCSR);
1378                 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1379                 musb_writew(epio, MUSB_RXCSR, csr);
1380                 musb_writew(epio, MUSB_RXCSR, csr);
1381         }
1382
1383         /* re-enable interrupt */
1384         musb_writew(mbase, MUSB_INTRTXE, int_txe);
1385         spin_unlock_irqrestore(&musb->lock, flags);
1386 }
1387
1388 static const struct usb_ep_ops musb_ep_ops = {
1389         .enable         = musb_gadget_enable,
1390         .disable        = musb_gadget_disable,
1391         .alloc_request  = musb_alloc_request,
1392         .free_request   = musb_free_request,
1393         .queue          = musb_gadget_queue,
1394         .dequeue        = musb_gadget_dequeue,
1395         .set_halt       = musb_gadget_set_halt,
1396         .set_wedge      = musb_gadget_set_wedge,
1397         .fifo_status    = musb_gadget_fifo_status,
1398         .fifo_flush     = musb_gadget_fifo_flush
1399 };
1400
1401 /* ----------------------------------------------------------------------- */
1402
1403 static int musb_gadget_get_frame(struct usb_gadget *gadget)
1404 {
1405         struct musb     *musb = gadget_to_musb(gadget);
1406
1407         return (int)musb_readw(musb->mregs, MUSB_FRAME);
1408 }
1409
1410 static int musb_gadget_wakeup(struct usb_gadget *gadget)
1411 {
1412         struct musb     *musb = gadget_to_musb(gadget);
1413         void __iomem    *mregs = musb->mregs;
1414         unsigned long   flags;
1415         int             status = -EINVAL;
1416         u8              power, devctl;
1417         int             retries;
1418
1419         spin_lock_irqsave(&musb->lock, flags);
1420
1421         switch (musb->xceiv->state) {
1422         case OTG_STATE_B_PERIPHERAL:
1423                 /* NOTE:  OTG state machine doesn't include B_SUSPENDED;
1424                  * that's part of the standard usb 1.1 state machine, and
1425                  * doesn't affect OTG transitions.
1426                  */
1427                 if (musb->may_wakeup && musb->is_suspended)
1428                         break;
1429                 goto done;
1430         case OTG_STATE_B_IDLE:
1431                 /* Start SRP ... OTG not required. */
1432                 devctl = musb_readb(mregs, MUSB_DEVCTL);
1433                 DBG(2, "Sending SRP: devctl: %02x\n", devctl);
1434                 devctl |= MUSB_DEVCTL_SESSION;
1435                 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1436                 devctl = musb_readb(mregs, MUSB_DEVCTL);
1437                 retries = 100;
1438                 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1439                         devctl = musb_readb(mregs, MUSB_DEVCTL);
1440                         if (retries-- < 1)
1441                                 break;
1442                 }
1443                 retries = 10000;
1444                 while (devctl & MUSB_DEVCTL_SESSION) {
1445                         devctl = musb_readb(mregs, MUSB_DEVCTL);
1446                         if (retries-- < 1)
1447                                 break;
1448                 }
1449
1450                 /* Block idling for at least 1s */
1451                 musb_platform_try_idle(musb,
1452                         jiffies + msecs_to_jiffies(1 * HZ));
1453
1454                 status = 0;
1455                 goto done;
1456         default:
1457                 DBG(2, "Unhandled wake: %s\n", otg_state_string(musb));
1458                 goto done;
1459         }
1460
1461         status = 0;
1462
1463         power = musb_readb(mregs, MUSB_POWER);
1464         power |= MUSB_POWER_RESUME;
1465         musb_writeb(mregs, MUSB_POWER, power);
1466         DBG(2, "issue wakeup\n");
1467
1468         /* FIXME do this next chunk in a timer callback, no udelay */
1469         mdelay(2);
1470
1471         power = musb_readb(mregs, MUSB_POWER);
1472         power &= ~MUSB_POWER_RESUME;
1473         musb_writeb(mregs, MUSB_POWER, power);
1474 done:
1475         spin_unlock_irqrestore(&musb->lock, flags);
1476         return status;
1477 }
1478
1479 static int
1480 musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1481 {
1482         struct musb     *musb = gadget_to_musb(gadget);
1483
1484         musb->is_self_powered = !!is_selfpowered;
1485         return 0;
1486 }
1487
1488 static void musb_pullup(struct musb *musb, int is_on)
1489 {
1490         u8 power;
1491
1492         power = musb_readb(musb->mregs, MUSB_POWER);
1493         if (is_on)
1494                 power |= MUSB_POWER_SOFTCONN;
1495         else
1496                 power &= ~MUSB_POWER_SOFTCONN;
1497
1498         /* FIXME if on, HdrcStart; if off, HdrcStop */
1499
1500         DBG(3, "gadget %s D+ pullup %s\n",
1501                 musb->gadget_driver->function, is_on ? "on" : "off");
1502         musb_writeb(musb->mregs, MUSB_POWER, power);
1503 }
1504
1505 #if 0
1506 static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1507 {
1508         DBG(2, "<= %s =>\n", __func__);
1509
1510         /*
1511          * FIXME iff driver's softconnect flag is set (as it is during probe,
1512          * though that can clear it), just musb_pullup().
1513          */
1514
1515         return -EINVAL;
1516 }
1517 #endif
1518
1519 static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1520 {
1521         struct musb     *musb = gadget_to_musb(gadget);
1522
1523         if (!musb->xceiv->set_power)
1524                 return -EOPNOTSUPP;
1525         return otg_set_power(musb->xceiv, mA);
1526 }
1527
1528 static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1529 {
1530         struct musb     *musb = gadget_to_musb(gadget);
1531         unsigned long   flags;
1532
1533         is_on = !!is_on;
1534
1535         /* NOTE: this assumes we are sensing vbus; we'd rather
1536          * not pullup unless the B-session is active.
1537          */
1538         spin_lock_irqsave(&musb->lock, flags);
1539         if (is_on != musb->softconnect) {
1540                 musb->softconnect = is_on;
1541                 musb_pullup(musb, is_on);
1542         }
1543         spin_unlock_irqrestore(&musb->lock, flags);
1544         return 0;
1545 }
1546
1547 static const struct usb_gadget_ops musb_gadget_operations = {
1548         .get_frame              = musb_gadget_get_frame,
1549         .wakeup                 = musb_gadget_wakeup,
1550         .set_selfpowered        = musb_gadget_set_self_powered,
1551         /* .vbus_session                = musb_gadget_vbus_session, */
1552         .vbus_draw              = musb_gadget_vbus_draw,
1553         .pullup                 = musb_gadget_pullup,
1554 };
1555
1556 /* ----------------------------------------------------------------------- */
1557
1558 /* Registration */
1559
1560 /* Only this registration code "knows" the rule (from USB standards)
1561  * about there being only one external upstream port.  It assumes
1562  * all peripheral ports are external...
1563  */
1564 static struct musb *the_gadget;
1565
1566 static void musb_gadget_release(struct device *dev)
1567 {
1568         /* kref_put(WHAT) */
1569         dev_dbg(dev, "%s\n", __func__);
1570 }
1571
1572
1573 static void __init
1574 init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1575 {
1576         struct musb_hw_ep       *hw_ep = musb->endpoints + epnum;
1577
1578         memset(ep, 0, sizeof *ep);
1579
1580         ep->current_epnum = epnum;
1581         ep->musb = musb;
1582         ep->hw_ep = hw_ep;
1583         ep->is_in = is_in;
1584
1585         INIT_LIST_HEAD(&ep->req_list);
1586
1587         sprintf(ep->name, "ep%d%s", epnum,
1588                         (!epnum || hw_ep->is_shared_fifo) ? "" : (
1589                                 is_in ? "in" : "out"));
1590         ep->end_point.name = ep->name;
1591         INIT_LIST_HEAD(&ep->end_point.ep_list);
1592         if (!epnum) {
1593                 ep->end_point.maxpacket = 64;
1594                 ep->end_point.ops = &musb_g_ep0_ops;
1595                 musb->g.ep0 = &ep->end_point;
1596         } else {
1597                 if (is_in)
1598                         ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1599                 else
1600                         ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1601                 ep->end_point.ops = &musb_ep_ops;
1602                 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1603         }
1604 }
1605
1606 /*
1607  * Initialize the endpoints exposed to peripheral drivers, with backlinks
1608  * to the rest of the driver state.
1609  */
1610 static inline void __init musb_g_init_endpoints(struct musb *musb)
1611 {
1612         u8                      epnum;
1613         struct musb_hw_ep       *hw_ep;
1614         unsigned                count = 0;
1615
1616         /* intialize endpoint list just once */
1617         INIT_LIST_HEAD(&(musb->g.ep_list));
1618
1619         for (epnum = 0, hw_ep = musb->endpoints;
1620                         epnum < musb->nr_endpoints;
1621                         epnum++, hw_ep++) {
1622                 if (hw_ep->is_shared_fifo /* || !epnum */) {
1623                         init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1624                         count++;
1625                 } else {
1626                         if (hw_ep->max_packet_sz_tx) {
1627                                 init_peripheral_ep(musb, &hw_ep->ep_in,
1628                                                         epnum, 1);
1629                                 count++;
1630                         }
1631                         if (hw_ep->max_packet_sz_rx) {
1632                                 init_peripheral_ep(musb, &hw_ep->ep_out,
1633                                                         epnum, 0);
1634                                 count++;
1635                         }
1636                 }
1637         }
1638 }
1639
1640 /* called once during driver setup to initialize and link into
1641  * the driver model; memory is zeroed.
1642  */
1643 int __init musb_gadget_setup(struct musb *musb)
1644 {
1645         int status;
1646
1647         /* REVISIT minor race:  if (erroneously) setting up two
1648          * musb peripherals at the same time, only the bus lock
1649          * is probably held.
1650          */
1651         if (the_gadget)
1652                 return -EBUSY;
1653         the_gadget = musb;
1654
1655         musb->g.ops = &musb_gadget_operations;
1656         musb->g.is_dualspeed = 1;
1657         musb->g.speed = USB_SPEED_UNKNOWN;
1658
1659         /* this "gadget" abstracts/virtualizes the controller */
1660         dev_set_name(&musb->g.dev, "gadget");
1661         musb->g.dev.parent = musb->controller;
1662         musb->g.dev.dma_mask = musb->controller->dma_mask;
1663         musb->g.dev.release = musb_gadget_release;
1664         musb->g.name = musb_driver_name;
1665
1666         if (is_otg_enabled(musb))
1667                 musb->g.is_otg = 1;
1668
1669         musb_g_init_endpoints(musb);
1670
1671         musb->is_active = 0;
1672         musb_platform_try_idle(musb, 0);
1673
1674         status = device_register(&musb->g.dev);
1675         if (status != 0)
1676                 the_gadget = NULL;
1677         return status;
1678 }
1679
1680 void musb_gadget_cleanup(struct musb *musb)
1681 {
1682         if (musb != the_gadget)
1683                 return;
1684
1685         device_unregister(&musb->g.dev);
1686         the_gadget = NULL;
1687 }
1688
1689 /*
1690  * Register the gadget driver. Used by gadget drivers when
1691  * registering themselves with the controller.
1692  *
1693  * -EINVAL something went wrong (not driver)
1694  * -EBUSY another gadget is already using the controller
1695  * -ENOMEM no memeory to perform the operation
1696  *
1697  * @param driver the gadget driver
1698  * @return <0 if error, 0 if everything is fine
1699  */
1700 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1701 {
1702         int retval;
1703         unsigned long flags;
1704         struct musb *musb = the_gadget;
1705
1706         if (!driver
1707                         || driver->speed != USB_SPEED_HIGH
1708                         || !driver->bind
1709                         || !driver->setup)
1710                 return -EINVAL;
1711
1712         /* driver must be initialized to support peripheral mode */
1713         if (!musb) {
1714                 DBG(1, "%s, no dev??\n", __func__);
1715                 return -ENODEV;
1716         }
1717
1718         DBG(3, "registering driver %s\n", driver->function);
1719         spin_lock_irqsave(&musb->lock, flags);
1720
1721         if (musb->gadget_driver) {
1722                 DBG(1, "%s is already bound to %s\n",
1723                                 musb_driver_name,
1724                                 musb->gadget_driver->driver.name);
1725                 retval = -EBUSY;
1726         } else {
1727                 musb->gadget_driver = driver;
1728                 musb->g.dev.driver = &driver->driver;
1729                 driver->driver.bus = NULL;
1730                 musb->softconnect = 1;
1731                 retval = 0;
1732         }
1733
1734         spin_unlock_irqrestore(&musb->lock, flags);
1735
1736         if (retval == 0) {
1737                 retval = driver->bind(&musb->g);
1738                 if (retval != 0) {
1739                         DBG(3, "bind to driver %s failed --> %d\n",
1740                                         driver->driver.name, retval);
1741                         musb->gadget_driver = NULL;
1742                         musb->g.dev.driver = NULL;
1743                 }
1744
1745                 spin_lock_irqsave(&musb->lock, flags);
1746
1747                 otg_set_peripheral(musb->xceiv, &musb->g);
1748                 musb->xceiv->state = OTG_STATE_B_IDLE;
1749                 musb->is_active = 1;
1750
1751                 /* FIXME this ignores the softconnect flag.  Drivers are
1752                  * allowed hold the peripheral inactive until for example
1753                  * userspace hooks up printer hardware or DSP codecs, so
1754                  * hosts only see fully functional devices.
1755                  */
1756
1757                 if (!is_otg_enabled(musb))
1758                         musb_start(musb);
1759
1760                 otg_set_peripheral(musb->xceiv, &musb->g);
1761
1762                 spin_unlock_irqrestore(&musb->lock, flags);
1763
1764                 if (is_otg_enabled(musb)) {
1765                         DBG(3, "OTG startup...\n");
1766
1767                         /* REVISIT:  funcall to other code, which also
1768                          * handles power budgeting ... this way also
1769                          * ensures HdrcStart is indirectly called.
1770                          */
1771                         retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
1772                         if (retval < 0) {
1773                                 DBG(1, "add_hcd failed, %d\n", retval);
1774                                 spin_lock_irqsave(&musb->lock, flags);
1775                                 otg_set_peripheral(musb->xceiv, NULL);
1776                                 musb->gadget_driver = NULL;
1777                                 musb->g.dev.driver = NULL;
1778                                 spin_unlock_irqrestore(&musb->lock, flags);
1779                         }
1780                 }
1781         }
1782
1783         return retval;
1784 }
1785 EXPORT_SYMBOL(usb_gadget_register_driver);
1786
1787 static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1788 {
1789         int                     i;
1790         struct musb_hw_ep       *hw_ep;
1791
1792         /* don't disconnect if it's not connected */
1793         if (musb->g.speed == USB_SPEED_UNKNOWN)
1794                 driver = NULL;
1795         else
1796                 musb->g.speed = USB_SPEED_UNKNOWN;
1797
1798         /* deactivate the hardware */
1799         if (musb->softconnect) {
1800                 musb->softconnect = 0;
1801                 musb_pullup(musb, 0);
1802         }
1803         musb_stop(musb);
1804
1805         /* killing any outstanding requests will quiesce the driver;
1806          * then report disconnect
1807          */
1808         if (driver) {
1809                 for (i = 0, hw_ep = musb->endpoints;
1810                                 i < musb->nr_endpoints;
1811                                 i++, hw_ep++) {
1812                         musb_ep_select(musb->mregs, i);
1813                         if (hw_ep->is_shared_fifo /* || !epnum */) {
1814                                 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1815                         } else {
1816                                 if (hw_ep->max_packet_sz_tx)
1817                                         nuke(&hw_ep->ep_in, -ESHUTDOWN);
1818                                 if (hw_ep->max_packet_sz_rx)
1819                                         nuke(&hw_ep->ep_out, -ESHUTDOWN);
1820                         }
1821                 }
1822
1823                 spin_unlock(&musb->lock);
1824                 driver->disconnect(&musb->g);
1825                 spin_lock(&musb->lock);
1826         }
1827 }
1828
1829 /*
1830  * Unregister the gadget driver. Used by gadget drivers when
1831  * unregistering themselves from the controller.
1832  *
1833  * @param driver the gadget driver to unregister
1834  */
1835 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1836 {
1837         unsigned long   flags;
1838         int             retval = 0;
1839         struct musb     *musb = the_gadget;
1840
1841         if (!driver || !driver->unbind || !musb)
1842                 return -EINVAL;
1843
1844         /* REVISIT always use otg_set_peripheral() here too;
1845          * this needs to shut down the OTG engine.
1846          */
1847
1848         spin_lock_irqsave(&musb->lock, flags);
1849
1850 #ifdef  CONFIG_USB_MUSB_OTG
1851         musb_hnp_stop(musb);
1852 #endif
1853
1854         if (musb->gadget_driver == driver) {
1855
1856                 (void) musb_gadget_vbus_draw(&musb->g, 0);
1857
1858                 musb->xceiv->state = OTG_STATE_UNDEFINED;
1859                 stop_activity(musb, driver);
1860                 otg_set_peripheral(musb->xceiv, NULL);
1861
1862                 DBG(3, "unregistering driver %s\n", driver->function);
1863                 spin_unlock_irqrestore(&musb->lock, flags);
1864                 driver->unbind(&musb->g);
1865                 spin_lock_irqsave(&musb->lock, flags);
1866
1867                 musb->gadget_driver = NULL;
1868                 musb->g.dev.driver = NULL;
1869
1870                 musb->is_active = 0;
1871                 musb_platform_try_idle(musb, 0);
1872         } else
1873                 retval = -EINVAL;
1874         spin_unlock_irqrestore(&musb->lock, flags);
1875
1876         if (is_otg_enabled(musb) && retval == 0) {
1877                 usb_remove_hcd(musb_to_hcd(musb));
1878                 /* FIXME we need to be able to register another
1879                  * gadget driver here and have everything work;
1880                  * that currently misbehaves.
1881                  */
1882         }
1883
1884         return retval;
1885 }
1886 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1887
1888
1889 /* ----------------------------------------------------------------------- */
1890
1891 /* lifecycle operations called through plat_uds.c */
1892
1893 void musb_g_resume(struct musb *musb)
1894 {
1895         musb->is_suspended = 0;
1896         switch (musb->xceiv->state) {
1897         case OTG_STATE_B_IDLE:
1898                 break;
1899         case OTG_STATE_B_WAIT_ACON:
1900         case OTG_STATE_B_PERIPHERAL:
1901                 musb->is_active = 1;
1902                 if (musb->gadget_driver && musb->gadget_driver->resume) {
1903                         spin_unlock(&musb->lock);
1904                         musb->gadget_driver->resume(&musb->g);
1905                         spin_lock(&musb->lock);
1906                 }
1907                 break;
1908         default:
1909                 WARNING("unhandled RESUME transition (%s)\n",
1910                                 otg_state_string(musb));
1911         }
1912 }
1913
1914 /* called when SOF packets stop for 3+ msec */
1915 void musb_g_suspend(struct musb *musb)
1916 {
1917         u8      devctl;
1918
1919         devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1920         DBG(3, "devctl %02x\n", devctl);
1921
1922         switch (musb->xceiv->state) {
1923         case OTG_STATE_B_IDLE:
1924                 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
1925                         musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
1926                 break;
1927         case OTG_STATE_B_PERIPHERAL:
1928                 musb->is_suspended = 1;
1929                 if (musb->gadget_driver && musb->gadget_driver->suspend) {
1930                         spin_unlock(&musb->lock);
1931                         musb->gadget_driver->suspend(&musb->g);
1932                         spin_lock(&musb->lock);
1933                 }
1934                 break;
1935         default:
1936                 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
1937                  * A_PERIPHERAL may need care too
1938                  */
1939                 WARNING("unhandled SUSPEND transition (%s)\n",
1940                                 otg_state_string(musb));
1941         }
1942 }
1943
1944 /* Called during SRP */
1945 void musb_g_wakeup(struct musb *musb)
1946 {
1947         musb_gadget_wakeup(&musb->g);
1948 }
1949
1950 /* called when VBUS drops below session threshold, and in other cases */
1951 void musb_g_disconnect(struct musb *musb)
1952 {
1953         void __iomem    *mregs = musb->mregs;
1954         u8      devctl = musb_readb(mregs, MUSB_DEVCTL);
1955
1956         DBG(3, "devctl %02x\n", devctl);
1957
1958         /* clear HR */
1959         musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
1960
1961         /* don't draw vbus until new b-default session */
1962         (void) musb_gadget_vbus_draw(&musb->g, 0);
1963
1964         musb->g.speed = USB_SPEED_UNKNOWN;
1965         if (musb->gadget_driver && musb->gadget_driver->disconnect) {
1966                 spin_unlock(&musb->lock);
1967                 musb->gadget_driver->disconnect(&musb->g);
1968                 spin_lock(&musb->lock);
1969         }
1970
1971         switch (musb->xceiv->state) {
1972         default:
1973 #ifdef  CONFIG_USB_MUSB_OTG
1974                 DBG(2, "Unhandled disconnect %s, setting a_idle\n",
1975                         otg_state_string(musb));
1976                 musb->xceiv->state = OTG_STATE_A_IDLE;
1977                 MUSB_HST_MODE(musb);
1978                 break;
1979         case OTG_STATE_A_PERIPHERAL:
1980                 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
1981                 MUSB_HST_MODE(musb);
1982                 break;
1983         case OTG_STATE_B_WAIT_ACON:
1984         case OTG_STATE_B_HOST:
1985 #endif
1986         case OTG_STATE_B_PERIPHERAL:
1987         case OTG_STATE_B_IDLE:
1988                 musb->xceiv->state = OTG_STATE_B_IDLE;
1989                 break;
1990         case OTG_STATE_B_SRP_INIT:
1991                 break;
1992         }
1993
1994         musb->is_active = 0;
1995 }
1996
1997 void musb_g_reset(struct musb *musb)
1998 __releases(musb->lock)
1999 __acquires(musb->lock)
2000 {
2001         void __iomem    *mbase = musb->mregs;
2002         u8              devctl = musb_readb(mbase, MUSB_DEVCTL);
2003         u8              power;
2004
2005         DBG(3, "<== %s addr=%x driver '%s'\n",
2006                         (devctl & MUSB_DEVCTL_BDEVICE)
2007                                 ? "B-Device" : "A-Device",
2008                         musb_readb(mbase, MUSB_FADDR),
2009                         musb->gadget_driver
2010                                 ? musb->gadget_driver->driver.name
2011                                 : NULL
2012                         );
2013
2014         /* report disconnect, if we didn't already (flushing EP state) */
2015         if (musb->g.speed != USB_SPEED_UNKNOWN)
2016                 musb_g_disconnect(musb);
2017
2018         /* clear HR */
2019         else if (devctl & MUSB_DEVCTL_HR)
2020                 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2021
2022
2023         /* what speed did we negotiate? */
2024         power = musb_readb(mbase, MUSB_POWER);
2025         musb->g.speed = (power & MUSB_POWER_HSMODE)
2026                         ? USB_SPEED_HIGH : USB_SPEED_FULL;
2027
2028         /* start in USB_STATE_DEFAULT */
2029         musb->is_active = 1;
2030         musb->is_suspended = 0;
2031         MUSB_DEV_MODE(musb);
2032         musb->address = 0;
2033         musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2034
2035         musb->may_wakeup = 0;
2036         musb->g.b_hnp_enable = 0;
2037         musb->g.a_alt_hnp_support = 0;
2038         musb->g.a_hnp_support = 0;
2039
2040         /* Normal reset, as B-Device;
2041          * or else after HNP, as A-Device
2042          */
2043         if (devctl & MUSB_DEVCTL_BDEVICE) {
2044                 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2045                 musb->g.is_a_peripheral = 0;
2046         } else if (is_otg_enabled(musb)) {
2047                 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2048                 musb->g.is_a_peripheral = 1;
2049         } else
2050                 WARN_ON(1);
2051
2052         /* start with default limits on VBUS power draw */
2053         (void) musb_gadget_vbus_draw(&musb->g,
2054                         is_otg_enabled(musb) ? 8 : 100);
2055 }