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