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