usb: musb: drop is_host_capable() and is_peripheral_capable()
[deliverable/linux.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 375
d17d535f 376 use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
550a7375
FB
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;
f0443afd
SS
647 unsigned len = 0;
648 u16 fifo_count;
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
f0443afd 658 fifo_count = 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) {
f0443afd 707 fifo_count = musb_readw(epio, MUSB_RXCOUNT);
0ae52d54
AG
708
709 /*
4f3e8d26
RQ
710 * use mode 1 only if we expect data of at least ep packet_sz
711 * and have not yet received a short packet
0ae52d54 712 */
4f3e8d26
RQ
713 if ((request->length - request->actual >= musb_ep->packet_sz) &&
714 (fifo_count >= musb_ep->packet_sz))
0ae52d54
AG
715 use_mode_1 = 1;
716 else
717 use_mode_1 = 0;
718
550a7375
FB
719 if (request->actual < request->length) {
720#ifdef CONFIG_USB_INVENTRA_DMA
c65bfa62 721 if (is_buffer_mapped(req)) {
550a7375
FB
722 struct dma_controller *c;
723 struct dma_channel *channel;
724 int use_dma = 0;
725
726 c = musb->dma_controller;
727 channel = musb_ep->dma;
728
0ae52d54
AG
729 /* Experimental: Mode1 works with mass storage use cases */
730 if (use_mode_1) {
9001d80d 731 csr |= MUSB_RXCSR_AUTOCLEAR;
0ae52d54
AG
732 musb_writew(epio, MUSB_RXCSR, csr);
733 csr |= MUSB_RXCSR_DMAENAB;
734 musb_writew(epio, MUSB_RXCSR, csr);
735
736 /*
737 * this special sequence (enabling and then
738 * disabling MUSB_RXCSR_DMAMODE) is required
739 * to get DMAReq to activate
740 */
741 musb_writew(epio, MUSB_RXCSR,
742 csr | MUSB_RXCSR_DMAMODE);
743 musb_writew(epio, MUSB_RXCSR, csr);
744
745 } else {
746 if (!musb_ep->hb_mult &&
747 musb_ep->hw_ep->rx_double_buffered)
748 csr |= MUSB_RXCSR_AUTOCLEAR;
749 csr |= MUSB_RXCSR_DMAENAB;
750 musb_writew(epio, MUSB_RXCSR, csr);
751 }
550a7375
FB
752
753 if (request->actual < request->length) {
754 int transfer_size = 0;
0ae52d54
AG
755 if (use_mode_1) {
756 transfer_size = min(request->length - request->actual,
757 channel->max_len);
550a7375 758 musb_ep->dma->desired_mode = 1;
0ae52d54
AG
759 } else {
760 transfer_size = min(request->length - request->actual,
f0443afd 761 (unsigned)fifo_count);
0ae52d54
AG
762 musb_ep->dma->desired_mode = 0;
763 }
550a7375
FB
764
765 use_dma = c->channel_program(
766 channel,
767 musb_ep->packet_sz,
768 channel->desired_mode,
769 request->dma
770 + request->actual,
771 transfer_size);
772 }
773
774 if (use_dma)
a48ff906
MYK
775 return;
776 }
777#elif defined(CONFIG_USB_UX500_DMA)
778 if ((is_buffer_mapped(req)) &&
779 (request->actual < request->length)) {
780
781 struct dma_controller *c;
782 struct dma_channel *channel;
783 int transfer_size = 0;
784
785 c = musb->dma_controller;
786 channel = musb_ep->dma;
787
788 /* In case first packet is short */
f0443afd
SS
789 if (fifo_count < musb_ep->packet_sz)
790 transfer_size = fifo_count;
a48ff906
MYK
791 else if (request->short_not_ok)
792 transfer_size = min(request->length -
793 request->actual,
794 channel->max_len);
795 else
796 transfer_size = min(request->length -
797 request->actual,
f0443afd 798 (unsigned)fifo_count);
a48ff906
MYK
799
800 csr &= ~MUSB_RXCSR_DMAMODE;
801 csr |= (MUSB_RXCSR_DMAENAB |
802 MUSB_RXCSR_AUTOCLEAR);
803
804 musb_writew(epio, MUSB_RXCSR, csr);
805
806 if (transfer_size <= musb_ep->packet_sz) {
807 musb_ep->dma->desired_mode = 0;
808 } else {
809 musb_ep->dma->desired_mode = 1;
810 /* Mode must be set after DMAENAB */
811 csr |= MUSB_RXCSR_DMAMODE;
812 musb_writew(epio, MUSB_RXCSR, csr);
813 }
814
815 if (c->channel_program(channel,
816 musb_ep->packet_sz,
817 channel->desired_mode,
818 request->dma
819 + request->actual,
820 transfer_size))
821
550a7375
FB
822 return;
823 }
824#endif /* Mentor's DMA */
825
f0443afd 826 len = request->length - request->actual;
5c8a86e1 827 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
550a7375 828 musb_ep->end_point.name,
f0443afd 829 fifo_count, len,
550a7375
FB
830 musb_ep->packet_sz);
831
c2c96321 832 fifo_count = min_t(unsigned, len, fifo_count);
550a7375
FB
833
834#ifdef CONFIG_USB_TUSB_OMAP_DMA
c65bfa62 835 if (tusb_dma_omap() && is_buffer_mapped(req)) {
550a7375
FB
836 struct dma_controller *c = musb->dma_controller;
837 struct dma_channel *channel = musb_ep->dma;
838 u32 dma_addr = request->dma + request->actual;
839 int ret;
840
841 ret = c->channel_program(channel,
842 musb_ep->packet_sz,
843 channel->desired_mode,
844 dma_addr,
845 fifo_count);
846 if (ret)
847 return;
848 }
849#endif
92d2711f
HK
850 /*
851 * Unmap the dma buffer back to cpu if dma channel
852 * programming fails. This buffer is mapped if the
853 * channel allocation is successful
854 */
c65bfa62 855 if (is_buffer_mapped(req)) {
92d2711f
HK
856 unmap_dma_buffer(req, musb);
857
e75df371
ML
858 /*
859 * Clear DMAENAB and AUTOCLEAR for the
92d2711f
HK
860 * PIO mode transfer
861 */
e75df371 862 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
92d2711f
HK
863 musb_writew(epio, MUSB_RXCSR, csr);
864 }
550a7375
FB
865
866 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
867 (request->buf + request->actual));
868 request->actual += fifo_count;
869
870 /* REVISIT if we left anything in the fifo, flush
871 * it and report -EOVERFLOW
872 */
873
874 /* ack the read! */
875 csr |= MUSB_RXCSR_P_WZC_BITS;
876 csr &= ~MUSB_RXCSR_RXPKTRDY;
877 musb_writew(epio, MUSB_RXCSR, csr);
878 }
879 }
880
881 /* reach the end or short packet detected */
f0443afd
SS
882 if (request->actual == request->length ||
883 fifo_count < musb_ep->packet_sz)
550a7375
FB
884 musb_g_giveback(musb_ep, request, 0);
885}
886
887/*
888 * Data ready for a request; called from IRQ
889 */
890void musb_g_rx(struct musb *musb, u8 epnum)
891{
892 u16 csr;
ad1adb89 893 struct musb_request *req;
550a7375
FB
894 struct usb_request *request;
895 void __iomem *mbase = musb->mregs;
bd2e74d6 896 struct musb_ep *musb_ep;
550a7375
FB
897 void __iomem *epio = musb->endpoints[epnum].regs;
898 struct dma_channel *dma;
bd2e74d6
ML
899 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
900
901 if (hw_ep->is_shared_fifo)
902 musb_ep = &hw_ep->ep_in;
903 else
904 musb_ep = &hw_ep->ep_out;
550a7375
FB
905
906 musb_ep_select(mbase, epnum);
907
ad1adb89
FB
908 req = next_request(musb_ep);
909 if (!req)
0abdc36f 910 return;
550a7375 911
ad1adb89
FB
912 request = &req->request;
913
550a7375
FB
914 csr = musb_readw(epio, MUSB_RXCSR);
915 dma = is_dma_capable() ? musb_ep->dma : NULL;
916
5c8a86e1 917 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
550a7375
FB
918 csr, dma ? " (dma)" : "", request);
919
920 if (csr & MUSB_RXCSR_P_SENTSTALL) {
550a7375
FB
921 csr |= MUSB_RXCSR_P_WZC_BITS;
922 csr &= ~MUSB_RXCSR_P_SENTSTALL;
923 musb_writew(epio, MUSB_RXCSR, csr);
cea83241 924 return;
550a7375
FB
925 }
926
927 if (csr & MUSB_RXCSR_P_OVERRUN) {
928 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
929 csr &= ~MUSB_RXCSR_P_OVERRUN;
930 musb_writew(epio, MUSB_RXCSR, csr);
931
5c8a86e1 932 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
43467868 933 if (request->status == -EINPROGRESS)
550a7375
FB
934 request->status = -EOVERFLOW;
935 }
936 if (csr & MUSB_RXCSR_INCOMPRX) {
937 /* REVISIT not necessarily an error */
5c8a86e1 938 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
550a7375
FB
939 }
940
941 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
942 /* "should not happen"; likely RXPKTRDY pending for DMA */
5c8a86e1 943 dev_dbg(musb->controller, "%s busy, csr %04x\n",
550a7375 944 musb_ep->end_point.name, csr);
cea83241 945 return;
550a7375
FB
946 }
947
948 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
949 csr &= ~(MUSB_RXCSR_AUTOCLEAR
950 | MUSB_RXCSR_DMAENAB
951 | MUSB_RXCSR_DMAMODE);
952 musb_writew(epio, MUSB_RXCSR,
953 MUSB_RXCSR_P_WZC_BITS | csr);
954
955 request->actual += musb_ep->dma->actual_len;
956
5c8a86e1 957 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
550a7375
FB
958 epnum, csr,
959 musb_readw(epio, MUSB_RXCSR),
960 musb_ep->dma->actual_len, request);
961
a48ff906
MYK
962#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
963 defined(CONFIG_USB_UX500_DMA)
550a7375 964 /* Autoclear doesn't clear RxPktRdy for short packets */
9001d80d 965 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
550a7375
FB
966 || (dma->actual_len
967 & (musb_ep->packet_sz - 1))) {
968 /* ack the read! */
969 csr &= ~MUSB_RXCSR_RXPKTRDY;
970 musb_writew(epio, MUSB_RXCSR, csr);
971 }
972
973 /* incomplete, and not short? wait for next IN packet */
974 if ((request->actual < request->length)
975 && (musb_ep->dma->actual_len
9001d80d
ML
976 == musb_ep->packet_sz)) {
977 /* In double buffer case, continue to unload fifo if
978 * there is Rx packet in FIFO.
979 **/
980 csr = musb_readw(epio, MUSB_RXCSR);
981 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
982 hw_ep->rx_double_buffered)
983 goto exit;
cea83241 984 return;
9001d80d 985 }
550a7375
FB
986#endif
987 musb_g_giveback(musb_ep, request, 0);
39287076
SK
988 /*
989 * In the giveback function the MUSB lock is
990 * released and acquired after sometime. During
991 * this time period the INDEX register could get
992 * changed by the gadget_queue function especially
993 * on SMP systems. Reselect the INDEX to be sure
994 * we are reading/modifying the right registers
995 */
996 musb_ep_select(mbase, epnum);
550a7375 997
ad1adb89
FB
998 req = next_request(musb_ep);
999 if (!req)
cea83241 1000 return;
550a7375 1001 }
a48ff906
MYK
1002#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
1003 defined(CONFIG_USB_UX500_DMA)
9001d80d 1004exit:
bb324b08 1005#endif
43467868 1006 /* Analyze request */
ad1adb89 1007 rxstate(musb, req);
550a7375
FB
1008}
1009
1010/* ------------------------------------------------------------ */
1011
1012static int musb_gadget_enable(struct usb_ep *ep,
1013 const struct usb_endpoint_descriptor *desc)
1014{
1015 unsigned long flags;
1016 struct musb_ep *musb_ep;
1017 struct musb_hw_ep *hw_ep;
1018 void __iomem *regs;
1019 struct musb *musb;
1020 void __iomem *mbase;
1021 u8 epnum;
1022 u16 csr;
1023 unsigned tmp;
1024 int status = -EINVAL;
1025
1026 if (!ep || !desc)
1027 return -EINVAL;
1028
1029 musb_ep = to_musb_ep(ep);
1030 hw_ep = musb_ep->hw_ep;
1031 regs = hw_ep->regs;
1032 musb = musb_ep->musb;
1033 mbase = musb->mregs;
1034 epnum = musb_ep->current_epnum;
1035
1036 spin_lock_irqsave(&musb->lock, flags);
1037
1038 if (musb_ep->desc) {
1039 status = -EBUSY;
1040 goto fail;
1041 }
96bcd090 1042 musb_ep->type = usb_endpoint_type(desc);
550a7375
FB
1043
1044 /* check direction and (later) maxpacket size against endpoint */
96bcd090 1045 if (usb_endpoint_num(desc) != epnum)
550a7375
FB
1046 goto fail;
1047
1048 /* REVISIT this rules out high bandwidth periodic transfers */
29cc8897 1049 tmp = usb_endpoint_maxp(desc);
f11d893d
ML
1050 if (tmp & ~0x07ff) {
1051 int ok;
1052
1053 if (usb_endpoint_dir_in(desc))
1054 ok = musb->hb_iso_tx;
1055 else
1056 ok = musb->hb_iso_rx;
1057
1058 if (!ok) {
5c8a86e1 1059 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
f11d893d
ML
1060 goto fail;
1061 }
1062 musb_ep->hb_mult = (tmp >> 11) & 3;
1063 } else {
1064 musb_ep->hb_mult = 0;
1065 }
1066
1067 musb_ep->packet_sz = tmp & 0x7ff;
1068 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
550a7375
FB
1069
1070 /* enable the interrupts for the endpoint, set the endpoint
1071 * packet size (or fail), set the mode, clear the fifo
1072 */
1073 musb_ep_select(mbase, epnum);
96bcd090 1074 if (usb_endpoint_dir_in(desc)) {
550a7375
FB
1075 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1076
1077 if (hw_ep->is_shared_fifo)
1078 musb_ep->is_in = 1;
1079 if (!musb_ep->is_in)
1080 goto fail;
f11d893d
ML
1081
1082 if (tmp > hw_ep->max_packet_sz_tx) {
5c8a86e1 1083 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
550a7375 1084 goto fail;
f11d893d 1085 }
550a7375
FB
1086
1087 int_txe |= (1 << epnum);
1088 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1089
1090 /* REVISIT if can_bulk_split(), use by updating "tmp";
1091 * likewise high bandwidth periodic tx
1092 */
9f445cb2 1093 /* Set TXMAXP with the FIFO size of the endpoint
31c9909b 1094 * to disable double buffering mode.
9f445cb2 1095 */
06624818
FB
1096 if (musb->double_buffer_not_ok)
1097 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
1098 else
1099 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1100 | (musb_ep->hb_mult << 11));
550a7375
FB
1101
1102 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1103 if (musb_readw(regs, MUSB_TXCSR)
1104 & MUSB_TXCSR_FIFONOTEMPTY)
1105 csr |= MUSB_TXCSR_FLUSHFIFO;
1106 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1107 csr |= MUSB_TXCSR_P_ISO;
1108
1109 /* set twice in case of double buffering */
1110 musb_writew(regs, MUSB_TXCSR, csr);
1111 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1112 musb_writew(regs, MUSB_TXCSR, csr);
1113
1114 } else {
1115 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
1116
1117 if (hw_ep->is_shared_fifo)
1118 musb_ep->is_in = 0;
1119 if (musb_ep->is_in)
1120 goto fail;
f11d893d
ML
1121
1122 if (tmp > hw_ep->max_packet_sz_rx) {
5c8a86e1 1123 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
550a7375 1124 goto fail;
f11d893d 1125 }
550a7375
FB
1126
1127 int_rxe |= (1 << epnum);
1128 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
1129
1130 /* REVISIT if can_bulk_combine() use by updating "tmp"
1131 * likewise high bandwidth periodic rx
1132 */
9f445cb2
CC
1133 /* Set RXMAXP with the FIFO size of the endpoint
1134 * to disable double buffering mode.
1135 */
06624818
FB
1136 if (musb->double_buffer_not_ok)
1137 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1138 else
1139 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1140 | (musb_ep->hb_mult << 11));
550a7375
FB
1141
1142 /* force shared fifo to OUT-only mode */
1143 if (hw_ep->is_shared_fifo) {
1144 csr = musb_readw(regs, MUSB_TXCSR);
1145 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1146 musb_writew(regs, MUSB_TXCSR, csr);
1147 }
1148
1149 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1150 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1151 csr |= MUSB_RXCSR_P_ISO;
1152 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1153 csr |= MUSB_RXCSR_DISNYET;
1154
1155 /* set twice in case of double buffering */
1156 musb_writew(regs, MUSB_RXCSR, csr);
1157 musb_writew(regs, MUSB_RXCSR, csr);
1158 }
1159
1160 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1161 * for some reason you run out of channels here.
1162 */
1163 if (is_dma_capable() && musb->dma_controller) {
1164 struct dma_controller *c = musb->dma_controller;
1165
1166 musb_ep->dma = c->channel_alloc(c, hw_ep,
1167 (desc->bEndpointAddress & USB_DIR_IN));
1168 } else
1169 musb_ep->dma = NULL;
1170
1171 musb_ep->desc = desc;
1172 musb_ep->busy = 0;
47e97605 1173 musb_ep->wedged = 0;
550a7375
FB
1174 status = 0;
1175
1176 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1177 musb_driver_name, musb_ep->end_point.name,
1178 ({ char *s; switch (musb_ep->type) {
1179 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1180 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1181 default: s = "iso"; break;
1182 }; s; }),
1183 musb_ep->is_in ? "IN" : "OUT",
1184 musb_ep->dma ? "dma, " : "",
1185 musb_ep->packet_sz);
1186
1187 schedule_work(&musb->irq_work);
1188
1189fail:
1190 spin_unlock_irqrestore(&musb->lock, flags);
1191 return status;
1192}
1193
1194/*
1195 * Disable an endpoint flushing all requests queued.
1196 */
1197static int musb_gadget_disable(struct usb_ep *ep)
1198{
1199 unsigned long flags;
1200 struct musb *musb;
1201 u8 epnum;
1202 struct musb_ep *musb_ep;
1203 void __iomem *epio;
1204 int status = 0;
1205
1206 musb_ep = to_musb_ep(ep);
1207 musb = musb_ep->musb;
1208 epnum = musb_ep->current_epnum;
1209 epio = musb->endpoints[epnum].regs;
1210
1211 spin_lock_irqsave(&musb->lock, flags);
1212 musb_ep_select(musb->mregs, epnum);
1213
1214 /* zero the endpoint sizes */
1215 if (musb_ep->is_in) {
1216 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1217 int_txe &= ~(1 << epnum);
1218 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1219 musb_writew(epio, MUSB_TXMAXP, 0);
1220 } else {
1221 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1222 int_rxe &= ~(1 << epnum);
1223 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1224 musb_writew(epio, MUSB_RXMAXP, 0);
1225 }
1226
1227 musb_ep->desc = NULL;
08f75bf1 1228 musb_ep->end_point.desc = NULL;
550a7375
FB
1229
1230 /* abort all pending DMA and requests */
1231 nuke(musb_ep, -ESHUTDOWN);
1232
1233 schedule_work(&musb->irq_work);
1234
1235 spin_unlock_irqrestore(&(musb->lock), flags);
1236
5c8a86e1 1237 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
550a7375
FB
1238
1239 return status;
1240}
1241
1242/*
1243 * Allocate a request for an endpoint.
1244 * Reused by ep0 code.
1245 */
1246struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1247{
1248 struct musb_ep *musb_ep = to_musb_ep(ep);
5c8a86e1 1249 struct musb *musb = musb_ep->musb;
550a7375
FB
1250 struct musb_request *request = NULL;
1251
1252 request = kzalloc(sizeof *request, gfp_flags);
0607f862 1253 if (!request) {
5c8a86e1 1254 dev_dbg(musb->controller, "not enough memory\n");
0607f862 1255 return NULL;
550a7375
FB
1256 }
1257
0607f862
FB
1258 request->request.dma = DMA_ADDR_INVALID;
1259 request->epnum = musb_ep->current_epnum;
1260 request->ep = musb_ep;
1261
550a7375
FB
1262 return &request->request;
1263}
1264
1265/*
1266 * Free a request
1267 * Reused by ep0 code.
1268 */
1269void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1270{
1271 kfree(to_musb_request(req));
1272}
1273
1274static LIST_HEAD(buffers);
1275
1276struct free_record {
1277 struct list_head list;
1278 struct device *dev;
1279 unsigned bytes;
1280 dma_addr_t dma;
1281};
1282
1283/*
1284 * Context: controller locked, IRQs blocked.
1285 */
a666e3e6 1286void musb_ep_restart(struct musb *musb, struct musb_request *req)
550a7375 1287{
5c8a86e1 1288 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
550a7375
FB
1289 req->tx ? "TX/IN" : "RX/OUT",
1290 &req->request, req->request.length, req->epnum);
1291
1292 musb_ep_select(musb->mregs, req->epnum);
1293 if (req->tx)
1294 txstate(musb, req);
1295 else
1296 rxstate(musb, req);
1297}
1298
1299static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1300 gfp_t gfp_flags)
1301{
1302 struct musb_ep *musb_ep;
1303 struct musb_request *request;
1304 struct musb *musb;
1305 int status = 0;
1306 unsigned long lockflags;
1307
1308 if (!ep || !req)
1309 return -EINVAL;
1310 if (!req->buf)
1311 return -ENODATA;
1312
1313 musb_ep = to_musb_ep(ep);
1314 musb = musb_ep->musb;
1315
1316 request = to_musb_request(req);
1317 request->musb = musb;
1318
1319 if (request->ep != musb_ep)
1320 return -EINVAL;
1321
5c8a86e1 1322 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
550a7375
FB
1323
1324 /* request is mine now... */
1325 request->request.actual = 0;
1326 request->request.status = -EINPROGRESS;
1327 request->epnum = musb_ep->current_epnum;
1328 request->tx = musb_ep->is_in;
1329
c65bfa62 1330 map_dma_buffer(request, musb, musb_ep);
550a7375
FB
1331
1332 spin_lock_irqsave(&musb->lock, lockflags);
1333
1334 /* don't queue if the ep is down */
1335 if (!musb_ep->desc) {
5c8a86e1 1336 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
550a7375
FB
1337 req, ep->name, "disabled");
1338 status = -ESHUTDOWN;
1339 goto cleanup;
1340 }
1341
1342 /* add request to the list */
ad1adb89 1343 list_add_tail(&request->list, &musb_ep->req_list);
550a7375
FB
1344
1345 /* it this is the head of the queue, start i/o ... */
ad1adb89 1346 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
550a7375
FB
1347 musb_ep_restart(musb, request);
1348
1349cleanup:
1350 spin_unlock_irqrestore(&musb->lock, lockflags);
1351 return status;
1352}
1353
1354static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1355{
1356 struct musb_ep *musb_ep = to_musb_ep(ep);
4cbbf084
FB
1357 struct musb_request *req = to_musb_request(request);
1358 struct musb_request *r;
550a7375
FB
1359 unsigned long flags;
1360 int status = 0;
1361 struct musb *musb = musb_ep->musb;
1362
1363 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1364 return -EINVAL;
1365
1366 spin_lock_irqsave(&musb->lock, flags);
1367
1368 list_for_each_entry(r, &musb_ep->req_list, list) {
4cbbf084 1369 if (r == req)
550a7375
FB
1370 break;
1371 }
4cbbf084 1372 if (r != req) {
5c8a86e1 1373 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
550a7375
FB
1374 status = -EINVAL;
1375 goto done;
1376 }
1377
1378 /* if the hardware doesn't have the request, easy ... */
3d5ad13e 1379 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
550a7375
FB
1380 musb_g_giveback(musb_ep, request, -ECONNRESET);
1381
1382 /* ... else abort the dma transfer ... */
1383 else if (is_dma_capable() && musb_ep->dma) {
1384 struct dma_controller *c = musb->dma_controller;
1385
1386 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1387 if (c->channel_abort)
1388 status = c->channel_abort(musb_ep->dma);
1389 else
1390 status = -EBUSY;
1391 if (status == 0)
1392 musb_g_giveback(musb_ep, request, -ECONNRESET);
1393 } else {
1394 /* NOTE: by sticking to easily tested hardware/driver states,
1395 * we leave counting of in-flight packets imprecise.
1396 */
1397 musb_g_giveback(musb_ep, request, -ECONNRESET);
1398 }
1399
1400done:
1401 spin_unlock_irqrestore(&musb->lock, flags);
1402 return status;
1403}
1404
1405/*
1406 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1407 * data but will queue requests.
1408 *
1409 * exported to ep0 code
1410 */
1b6c3b0f 1411static int musb_gadget_set_halt(struct usb_ep *ep, int value)
550a7375
FB
1412{
1413 struct musb_ep *musb_ep = to_musb_ep(ep);
1414 u8 epnum = musb_ep->current_epnum;
1415 struct musb *musb = musb_ep->musb;
1416 void __iomem *epio = musb->endpoints[epnum].regs;
1417 void __iomem *mbase;
1418 unsigned long flags;
1419 u16 csr;
cea83241 1420 struct musb_request *request;
550a7375
FB
1421 int status = 0;
1422
1423 if (!ep)
1424 return -EINVAL;
1425 mbase = musb->mregs;
1426
1427 spin_lock_irqsave(&musb->lock, flags);
1428
1429 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1430 status = -EINVAL;
1431 goto done;
1432 }
1433
1434 musb_ep_select(mbase, epnum);
1435
ad1adb89 1436 request = next_request(musb_ep);
cea83241
SS
1437 if (value) {
1438 if (request) {
5c8a86e1 1439 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
cea83241
SS
1440 ep->name);
1441 status = -EAGAIN;
1442 goto done;
1443 }
1444 /* Cannot portably stall with non-empty FIFO */
1445 if (musb_ep->is_in) {
1446 csr = musb_readw(epio, MUSB_TXCSR);
1447 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
5c8a86e1 1448 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
cea83241
SS
1449 status = -EAGAIN;
1450 goto done;
1451 }
550a7375 1452 }
47e97605
SS
1453 } else
1454 musb_ep->wedged = 0;
550a7375
FB
1455
1456 /* set/clear the stall and toggle bits */
5c8a86e1 1457 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
550a7375
FB
1458 if (musb_ep->is_in) {
1459 csr = musb_readw(epio, MUSB_TXCSR);
550a7375
FB
1460 csr |= MUSB_TXCSR_P_WZC_BITS
1461 | MUSB_TXCSR_CLRDATATOG;
1462 if (value)
1463 csr |= MUSB_TXCSR_P_SENDSTALL;
1464 else
1465 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1466 | MUSB_TXCSR_P_SENTSTALL);
1467 csr &= ~MUSB_TXCSR_TXPKTRDY;
1468 musb_writew(epio, MUSB_TXCSR, csr);
1469 } else {
1470 csr = musb_readw(epio, MUSB_RXCSR);
1471 csr |= MUSB_RXCSR_P_WZC_BITS
1472 | MUSB_RXCSR_FLUSHFIFO
1473 | MUSB_RXCSR_CLRDATATOG;
1474 if (value)
1475 csr |= MUSB_RXCSR_P_SENDSTALL;
1476 else
1477 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1478 | MUSB_RXCSR_P_SENTSTALL);
1479 musb_writew(epio, MUSB_RXCSR, csr);
1480 }
1481
550a7375
FB
1482 /* maybe start the first request in the queue */
1483 if (!musb_ep->busy && !value && request) {
5c8a86e1 1484 dev_dbg(musb->controller, "restarting the request\n");
550a7375
FB
1485 musb_ep_restart(musb, request);
1486 }
1487
cea83241 1488done:
550a7375
FB
1489 spin_unlock_irqrestore(&musb->lock, flags);
1490 return status;
1491}
1492
47e97605
SS
1493/*
1494 * Sets the halt feature with the clear requests ignored
1495 */
1b6c3b0f 1496static int musb_gadget_set_wedge(struct usb_ep *ep)
47e97605
SS
1497{
1498 struct musb_ep *musb_ep = to_musb_ep(ep);
1499
1500 if (!ep)
1501 return -EINVAL;
1502
1503 musb_ep->wedged = 1;
1504
1505 return usb_ep_set_halt(ep);
1506}
1507
550a7375
FB
1508static int musb_gadget_fifo_status(struct usb_ep *ep)
1509{
1510 struct musb_ep *musb_ep = to_musb_ep(ep);
1511 void __iomem *epio = musb_ep->hw_ep->regs;
1512 int retval = -EINVAL;
1513
1514 if (musb_ep->desc && !musb_ep->is_in) {
1515 struct musb *musb = musb_ep->musb;
1516 int epnum = musb_ep->current_epnum;
1517 void __iomem *mbase = musb->mregs;
1518 unsigned long flags;
1519
1520 spin_lock_irqsave(&musb->lock, flags);
1521
1522 musb_ep_select(mbase, epnum);
1523 /* FIXME return zero unless RXPKTRDY is set */
1524 retval = musb_readw(epio, MUSB_RXCOUNT);
1525
1526 spin_unlock_irqrestore(&musb->lock, flags);
1527 }
1528 return retval;
1529}
1530
1531static void musb_gadget_fifo_flush(struct usb_ep *ep)
1532{
1533 struct musb_ep *musb_ep = to_musb_ep(ep);
1534 struct musb *musb = musb_ep->musb;
1535 u8 epnum = musb_ep->current_epnum;
1536 void __iomem *epio = musb->endpoints[epnum].regs;
1537 void __iomem *mbase;
1538 unsigned long flags;
1539 u16 csr, int_txe;
1540
1541 mbase = musb->mregs;
1542
1543 spin_lock_irqsave(&musb->lock, flags);
1544 musb_ep_select(mbase, (u8) epnum);
1545
1546 /* disable interrupts */
1547 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1548 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1549
1550 if (musb_ep->is_in) {
1551 csr = musb_readw(epio, MUSB_TXCSR);
1552 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1553 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
4858f06e
YK
1554 /*
1555 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1556 * to interrupt current FIFO loading, but not flushing
1557 * the already loaded ones.
1558 */
1559 csr &= ~MUSB_TXCSR_TXPKTRDY;
550a7375
FB
1560 musb_writew(epio, MUSB_TXCSR, csr);
1561 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1562 musb_writew(epio, MUSB_TXCSR, csr);
1563 }
1564 } else {
1565 csr = musb_readw(epio, MUSB_RXCSR);
1566 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1567 musb_writew(epio, MUSB_RXCSR, csr);
1568 musb_writew(epio, MUSB_RXCSR, csr);
1569 }
1570
1571 /* re-enable interrupt */
1572 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1573 spin_unlock_irqrestore(&musb->lock, flags);
1574}
1575
1576static const struct usb_ep_ops musb_ep_ops = {
1577 .enable = musb_gadget_enable,
1578 .disable = musb_gadget_disable,
1579 .alloc_request = musb_alloc_request,
1580 .free_request = musb_free_request,
1581 .queue = musb_gadget_queue,
1582 .dequeue = musb_gadget_dequeue,
1583 .set_halt = musb_gadget_set_halt,
47e97605 1584 .set_wedge = musb_gadget_set_wedge,
550a7375
FB
1585 .fifo_status = musb_gadget_fifo_status,
1586 .fifo_flush = musb_gadget_fifo_flush
1587};
1588
1589/* ----------------------------------------------------------------------- */
1590
1591static int musb_gadget_get_frame(struct usb_gadget *gadget)
1592{
1593 struct musb *musb = gadget_to_musb(gadget);
1594
1595 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1596}
1597
1598static int musb_gadget_wakeup(struct usb_gadget *gadget)
1599{
1600 struct musb *musb = gadget_to_musb(gadget);
1601 void __iomem *mregs = musb->mregs;
1602 unsigned long flags;
1603 int status = -EINVAL;
1604 u8 power, devctl;
1605 int retries;
1606
1607 spin_lock_irqsave(&musb->lock, flags);
1608
84e250ff 1609 switch (musb->xceiv->state) {
550a7375
FB
1610 case OTG_STATE_B_PERIPHERAL:
1611 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1612 * that's part of the standard usb 1.1 state machine, and
1613 * doesn't affect OTG transitions.
1614 */
1615 if (musb->may_wakeup && musb->is_suspended)
1616 break;
1617 goto done;
1618 case OTG_STATE_B_IDLE:
1619 /* Start SRP ... OTG not required. */
1620 devctl = musb_readb(mregs, MUSB_DEVCTL);
5c8a86e1 1621 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
550a7375
FB
1622 devctl |= MUSB_DEVCTL_SESSION;
1623 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1624 devctl = musb_readb(mregs, MUSB_DEVCTL);
1625 retries = 100;
1626 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1627 devctl = musb_readb(mregs, MUSB_DEVCTL);
1628 if (retries-- < 1)
1629 break;
1630 }
1631 retries = 10000;
1632 while (devctl & MUSB_DEVCTL_SESSION) {
1633 devctl = musb_readb(mregs, MUSB_DEVCTL);
1634 if (retries-- < 1)
1635 break;
1636 }
1637
8620543e 1638 spin_unlock_irqrestore(&musb->lock, flags);
6e13c650 1639 otg_start_srp(musb->xceiv->otg);
8620543e
HH
1640 spin_lock_irqsave(&musb->lock, flags);
1641
550a7375
FB
1642 /* Block idling for at least 1s */
1643 musb_platform_try_idle(musb,
1644 jiffies + msecs_to_jiffies(1 * HZ));
1645
1646 status = 0;
1647 goto done;
1648 default:
5c8a86e1 1649 dev_dbg(musb->controller, "Unhandled wake: %s\n",
3df00453 1650 otg_state_string(musb->xceiv->state));
550a7375
FB
1651 goto done;
1652 }
1653
1654 status = 0;
1655
1656 power = musb_readb(mregs, MUSB_POWER);
1657 power |= MUSB_POWER_RESUME;
1658 musb_writeb(mregs, MUSB_POWER, power);
5c8a86e1 1659 dev_dbg(musb->controller, "issue wakeup\n");
550a7375
FB
1660
1661 /* FIXME do this next chunk in a timer callback, no udelay */
1662 mdelay(2);
1663
1664 power = musb_readb(mregs, MUSB_POWER);
1665 power &= ~MUSB_POWER_RESUME;
1666 musb_writeb(mregs, MUSB_POWER, power);
1667done:
1668 spin_unlock_irqrestore(&musb->lock, flags);
1669 return status;
1670}
1671
1672static int
1673musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1674{
1675 struct musb *musb = gadget_to_musb(gadget);
1676
1677 musb->is_self_powered = !!is_selfpowered;
1678 return 0;
1679}
1680
1681static void musb_pullup(struct musb *musb, int is_on)
1682{
1683 u8 power;
1684
1685 power = musb_readb(musb->mregs, MUSB_POWER);
1686 if (is_on)
1687 power |= MUSB_POWER_SOFTCONN;
1688 else
1689 power &= ~MUSB_POWER_SOFTCONN;
1690
1691 /* FIXME if on, HdrcStart; if off, HdrcStop */
1692
e71eb392
SAS
1693 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1694 is_on ? "on" : "off");
550a7375
FB
1695 musb_writeb(musb->mregs, MUSB_POWER, power);
1696}
1697
1698#if 0
1699static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1700{
5c8a86e1 1701 dev_dbg(musb->controller, "<= %s =>\n", __func__);
550a7375
FB
1702
1703 /*
1704 * FIXME iff driver's softconnect flag is set (as it is during probe,
1705 * though that can clear it), just musb_pullup().
1706 */
1707
1708 return -EINVAL;
1709}
1710#endif
1711
1712static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1713{
1714 struct musb *musb = gadget_to_musb(gadget);
1715
84e250ff 1716 if (!musb->xceiv->set_power)
550a7375 1717 return -EOPNOTSUPP;
b96d3b08 1718 return usb_phy_set_power(musb->xceiv, mA);
550a7375
FB
1719}
1720
1721static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1722{
1723 struct musb *musb = gadget_to_musb(gadget);
1724 unsigned long flags;
1725
1726 is_on = !!is_on;
1727
93e098a8
JS
1728 pm_runtime_get_sync(musb->controller);
1729
550a7375
FB
1730 /* NOTE: this assumes we are sensing vbus; we'd rather
1731 * not pullup unless the B-session is active.
1732 */
1733 spin_lock_irqsave(&musb->lock, flags);
1734 if (is_on != musb->softconnect) {
1735 musb->softconnect = is_on;
1736 musb_pullup(musb, is_on);
1737 }
1738 spin_unlock_irqrestore(&musb->lock, flags);
93e098a8
JS
1739
1740 pm_runtime_put(musb->controller);
1741
550a7375
FB
1742 return 0;
1743}
1744
e71eb392
SAS
1745static int musb_gadget_start(struct usb_gadget *g,
1746 struct usb_gadget_driver *driver);
1747static int musb_gadget_stop(struct usb_gadget *g,
1748 struct usb_gadget_driver *driver);
0f91349b 1749
550a7375
FB
1750static const struct usb_gadget_ops musb_gadget_operations = {
1751 .get_frame = musb_gadget_get_frame,
1752 .wakeup = musb_gadget_wakeup,
1753 .set_selfpowered = musb_gadget_set_self_powered,
1754 /* .vbus_session = musb_gadget_vbus_session, */
1755 .vbus_draw = musb_gadget_vbus_draw,
1756 .pullup = musb_gadget_pullup,
e71eb392
SAS
1757 .udc_start = musb_gadget_start,
1758 .udc_stop = musb_gadget_stop,
550a7375
FB
1759};
1760
1761/* ----------------------------------------------------------------------- */
1762
1763/* Registration */
1764
1765/* Only this registration code "knows" the rule (from USB standards)
1766 * about there being only one external upstream port. It assumes
1767 * all peripheral ports are external...
1768 */
550a7375
FB
1769
1770static void musb_gadget_release(struct device *dev)
1771{
1772 /* kref_put(WHAT) */
1773 dev_dbg(dev, "%s\n", __func__);
1774}
1775
1776
e9e8c85e 1777static void __devinit
550a7375
FB
1778init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1779{
1780 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1781
1782 memset(ep, 0, sizeof *ep);
1783
1784 ep->current_epnum = epnum;
1785 ep->musb = musb;
1786 ep->hw_ep = hw_ep;
1787 ep->is_in = is_in;
1788
1789 INIT_LIST_HEAD(&ep->req_list);
1790
1791 sprintf(ep->name, "ep%d%s", epnum,
1792 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1793 is_in ? "in" : "out"));
1794 ep->end_point.name = ep->name;
1795 INIT_LIST_HEAD(&ep->end_point.ep_list);
1796 if (!epnum) {
1797 ep->end_point.maxpacket = 64;
1798 ep->end_point.ops = &musb_g_ep0_ops;
1799 musb->g.ep0 = &ep->end_point;
1800 } else {
1801 if (is_in)
1802 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1803 else
1804 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1805 ep->end_point.ops = &musb_ep_ops;
1806 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1807 }
1808}
1809
1810/*
1811 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1812 * to the rest of the driver state.
1813 */
e9e8c85e 1814static inline void __devinit musb_g_init_endpoints(struct musb *musb)
550a7375
FB
1815{
1816 u8 epnum;
1817 struct musb_hw_ep *hw_ep;
1818 unsigned count = 0;
1819
b595076a 1820 /* initialize endpoint list just once */
550a7375
FB
1821 INIT_LIST_HEAD(&(musb->g.ep_list));
1822
1823 for (epnum = 0, hw_ep = musb->endpoints;
1824 epnum < musb->nr_endpoints;
1825 epnum++, hw_ep++) {
1826 if (hw_ep->is_shared_fifo /* || !epnum */) {
1827 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1828 count++;
1829 } else {
1830 if (hw_ep->max_packet_sz_tx) {
1831 init_peripheral_ep(musb, &hw_ep->ep_in,
1832 epnum, 1);
1833 count++;
1834 }
1835 if (hw_ep->max_packet_sz_rx) {
1836 init_peripheral_ep(musb, &hw_ep->ep_out,
1837 epnum, 0);
1838 count++;
1839 }
1840 }
1841 }
1842}
1843
1844/* called once during driver setup to initialize and link into
1845 * the driver model; memory is zeroed.
1846 */
e9e8c85e 1847int __devinit musb_gadget_setup(struct musb *musb)
550a7375
FB
1848{
1849 int status;
1850
1851 /* REVISIT minor race: if (erroneously) setting up two
1852 * musb peripherals at the same time, only the bus lock
1853 * is probably held.
1854 */
550a7375
FB
1855
1856 musb->g.ops = &musb_gadget_operations;
d327ab5b 1857 musb->g.max_speed = USB_SPEED_HIGH;
550a7375
FB
1858 musb->g.speed = USB_SPEED_UNKNOWN;
1859
1860 /* this "gadget" abstracts/virtualizes the controller */
427c4f33 1861 dev_set_name(&musb->g.dev, "gadget");
550a7375
FB
1862 musb->g.dev.parent = musb->controller;
1863 musb->g.dev.dma_mask = musb->controller->dma_mask;
1864 musb->g.dev.release = musb_gadget_release;
1865 musb->g.name = musb_driver_name;
1866
032ec49f 1867 musb->g.is_otg = 1;
550a7375
FB
1868
1869 musb_g_init_endpoints(musb);
1870
1871 musb->is_active = 0;
1872 musb_platform_try_idle(musb, 0);
1873
1874 status = device_register(&musb->g.dev);
e2c34045
RR
1875 if (status != 0) {
1876 put_device(&musb->g.dev);
0f91349b 1877 return status;
e2c34045 1878 }
0f91349b
SAS
1879 status = usb_add_gadget_udc(musb->controller, &musb->g);
1880 if (status)
1881 goto err;
1882
1883 return 0;
1884err:
6193d699 1885 musb->g.dev.parent = NULL;
0f91349b 1886 device_unregister(&musb->g.dev);
550a7375
FB
1887 return status;
1888}
1889
1890void musb_gadget_cleanup(struct musb *musb)
1891{
0f91349b 1892 usb_del_gadget_udc(&musb->g);
6193d699
SAS
1893 if (musb->g.dev.parent)
1894 device_unregister(&musb->g.dev);
550a7375
FB
1895}
1896
1897/*
1898 * Register the gadget driver. Used by gadget drivers when
1899 * registering themselves with the controller.
1900 *
1901 * -EINVAL something went wrong (not driver)
1902 * -EBUSY another gadget is already using the controller
b595076a 1903 * -ENOMEM no memory to perform the operation
550a7375
FB
1904 *
1905 * @param driver the gadget driver
1906 * @return <0 if error, 0 if everything is fine
1907 */
e71eb392
SAS
1908static int musb_gadget_start(struct usb_gadget *g,
1909 struct usb_gadget_driver *driver)
550a7375 1910{
e71eb392 1911 struct musb *musb = gadget_to_musb(g);
d445b6da 1912 struct usb_otg *otg = musb->xceiv->otg;
032ec49f 1913 struct usb_hcd *hcd = musb_to_hcd(musb);
63eed2b5 1914 unsigned long flags;
032ec49f 1915 int retval = 0;
550a7375 1916
032ec49f
FB
1917 if (driver->max_speed < USB_SPEED_HIGH) {
1918 retval = -EINVAL;
1919 goto err;
1920 }
550a7375 1921
7acc6197
HH
1922 pm_runtime_get_sync(musb->controller);
1923
5c8a86e1 1924 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
550a7375 1925
e71eb392 1926 musb->softconnect = 0;
63eed2b5 1927 musb->gadget_driver = driver;
550a7375 1928
63eed2b5 1929 spin_lock_irqsave(&musb->lock, flags);
e71eb392 1930 musb->is_active = 1;
550a7375 1931
6e13c650 1932 otg_set_peripheral(otg, &musb->g);
63eed2b5 1933 musb->xceiv->state = OTG_STATE_B_IDLE;
63eed2b5 1934 spin_unlock_irqrestore(&musb->lock, flags);
550a7375 1935
032ec49f
FB
1936 /* REVISIT: funcall to other code, which also
1937 * handles power budgeting ... this way also
1938 * ensures HdrcStart is indirectly called.
1939 */
1940 retval = usb_add_hcd(hcd, 0, 0);
1941 if (retval < 0) {
1942 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
1943 goto err;
1944 }
550a7375 1945
032ec49f
FB
1946 if ((musb->xceiv->last_event == USB_EVENT_ID)
1947 && otg->set_vbus)
1948 otg_set_vbus(otg, 1);
63eed2b5 1949
032ec49f 1950 hcd->self.uses_pio_for_control = 1;
5f1e8ce7 1951
cdefce16
JN
1952 if (musb->xceiv->last_event == USB_EVENT_NONE)
1953 pm_runtime_put(musb->controller);
550a7375 1954
63eed2b5
FB
1955 return 0;
1956
032ec49f 1957err:
550a7375
FB
1958 return retval;
1959}
550a7375
FB
1960
1961static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1962{
1963 int i;
1964 struct musb_hw_ep *hw_ep;
1965
1966 /* don't disconnect if it's not connected */
1967 if (musb->g.speed == USB_SPEED_UNKNOWN)
1968 driver = NULL;
1969 else
1970 musb->g.speed = USB_SPEED_UNKNOWN;
1971
1972 /* deactivate the hardware */
1973 if (musb->softconnect) {
1974 musb->softconnect = 0;
1975 musb_pullup(musb, 0);
1976 }
1977 musb_stop(musb);
1978
1979 /* killing any outstanding requests will quiesce the driver;
1980 * then report disconnect
1981 */
1982 if (driver) {
1983 for (i = 0, hw_ep = musb->endpoints;
1984 i < musb->nr_endpoints;
1985 i++, hw_ep++) {
1986 musb_ep_select(musb->mregs, i);
1987 if (hw_ep->is_shared_fifo /* || !epnum */) {
1988 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1989 } else {
1990 if (hw_ep->max_packet_sz_tx)
1991 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1992 if (hw_ep->max_packet_sz_rx)
1993 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1994 }
1995 }
550a7375
FB
1996 }
1997}
1998
1999/*
2000 * Unregister the gadget driver. Used by gadget drivers when
2001 * unregistering themselves from the controller.
2002 *
2003 * @param driver the gadget driver to unregister
2004 */
e71eb392
SAS
2005static int musb_gadget_stop(struct usb_gadget *g,
2006 struct usb_gadget_driver *driver)
550a7375 2007{
e71eb392 2008 struct musb *musb = gadget_to_musb(g);
63eed2b5 2009 unsigned long flags;
550a7375 2010
7acc6197
HH
2011 if (musb->xceiv->last_event == USB_EVENT_NONE)
2012 pm_runtime_get_sync(musb->controller);
2013
63eed2b5
FB
2014 /*
2015 * REVISIT always use otg_set_peripheral() here too;
550a7375
FB
2016 * this needs to shut down the OTG engine.
2017 */
2018
2019 spin_lock_irqsave(&musb->lock, flags);
2020
550a7375 2021 musb_hnp_stop(musb);
550a7375 2022
63eed2b5 2023 (void) musb_gadget_vbus_draw(&musb->g, 0);
550a7375 2024
63eed2b5
FB
2025 musb->xceiv->state = OTG_STATE_UNDEFINED;
2026 stop_activity(musb, driver);
6e13c650 2027 otg_set_peripheral(musb->xceiv->otg, NULL);
550a7375 2028
5c8a86e1 2029 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
550a7375 2030
63eed2b5
FB
2031 musb->is_active = 0;
2032 musb_platform_try_idle(musb, 0);
550a7375
FB
2033 spin_unlock_irqrestore(&musb->lock, flags);
2034
032ec49f
FB
2035 usb_remove_hcd(musb_to_hcd(musb));
2036 /*
2037 * FIXME we need to be able to register another
2038 * gadget driver here and have everything work;
2039 * that currently misbehaves.
2040 */
63eed2b5 2041
7acc6197
HH
2042 pm_runtime_put(musb->controller);
2043
63eed2b5 2044 return 0;
550a7375 2045}
550a7375
FB
2046
2047/* ----------------------------------------------------------------------- */
2048
2049/* lifecycle operations called through plat_uds.c */
2050
2051void musb_g_resume(struct musb *musb)
2052{
2053 musb->is_suspended = 0;
84e250ff 2054 switch (musb->xceiv->state) {
550a7375
FB
2055 case OTG_STATE_B_IDLE:
2056 break;
2057 case OTG_STATE_B_WAIT_ACON:
2058 case OTG_STATE_B_PERIPHERAL:
2059 musb->is_active = 1;
2060 if (musb->gadget_driver && musb->gadget_driver->resume) {
2061 spin_unlock(&musb->lock);
2062 musb->gadget_driver->resume(&musb->g);
2063 spin_lock(&musb->lock);
2064 }
2065 break;
2066 default:
2067 WARNING("unhandled RESUME transition (%s)\n",
3df00453 2068 otg_state_string(musb->xceiv->state));
550a7375
FB
2069 }
2070}
2071
2072/* called when SOF packets stop for 3+ msec */
2073void musb_g_suspend(struct musb *musb)
2074{
2075 u8 devctl;
2076
2077 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
5c8a86e1 2078 dev_dbg(musb->controller, "devctl %02x\n", devctl);
550a7375 2079
84e250ff 2080 switch (musb->xceiv->state) {
550a7375
FB
2081 case OTG_STATE_B_IDLE:
2082 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
84e250ff 2083 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
550a7375
FB
2084 break;
2085 case OTG_STATE_B_PERIPHERAL:
2086 musb->is_suspended = 1;
2087 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2088 spin_unlock(&musb->lock);
2089 musb->gadget_driver->suspend(&musb->g);
2090 spin_lock(&musb->lock);
2091 }
2092 break;
2093 default:
2094 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2095 * A_PERIPHERAL may need care too
2096 */
2097 WARNING("unhandled SUSPEND transition (%s)\n",
3df00453 2098 otg_state_string(musb->xceiv->state));
550a7375
FB
2099 }
2100}
2101
2102/* Called during SRP */
2103void musb_g_wakeup(struct musb *musb)
2104{
2105 musb_gadget_wakeup(&musb->g);
2106}
2107
2108/* called when VBUS drops below session threshold, and in other cases */
2109void musb_g_disconnect(struct musb *musb)
2110{
2111 void __iomem *mregs = musb->mregs;
2112 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2113
5c8a86e1 2114 dev_dbg(musb->controller, "devctl %02x\n", devctl);
550a7375
FB
2115
2116 /* clear HR */
2117 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2118
2119 /* don't draw vbus until new b-default session */
2120 (void) musb_gadget_vbus_draw(&musb->g, 0);
2121
2122 musb->g.speed = USB_SPEED_UNKNOWN;
2123 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2124 spin_unlock(&musb->lock);
2125 musb->gadget_driver->disconnect(&musb->g);
2126 spin_lock(&musb->lock);
2127 }
2128
84e250ff 2129 switch (musb->xceiv->state) {
550a7375 2130 default:
5c8a86e1 2131 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
3df00453 2132 otg_state_string(musb->xceiv->state));
84e250ff 2133 musb->xceiv->state = OTG_STATE_A_IDLE;
ab983f2a 2134 MUSB_HST_MODE(musb);
550a7375
FB
2135 break;
2136 case OTG_STATE_A_PERIPHERAL:
1de00dae 2137 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
ab983f2a 2138 MUSB_HST_MODE(musb);
550a7375
FB
2139 break;
2140 case OTG_STATE_B_WAIT_ACON:
2141 case OTG_STATE_B_HOST:
550a7375
FB
2142 case OTG_STATE_B_PERIPHERAL:
2143 case OTG_STATE_B_IDLE:
84e250ff 2144 musb->xceiv->state = OTG_STATE_B_IDLE;
550a7375
FB
2145 break;
2146 case OTG_STATE_B_SRP_INIT:
2147 break;
2148 }
2149
2150 musb->is_active = 0;
2151}
2152
2153void musb_g_reset(struct musb *musb)
2154__releases(musb->lock)
2155__acquires(musb->lock)
2156{
2157 void __iomem *mbase = musb->mregs;
2158 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2159 u8 power;
2160
5c8a86e1 2161 dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
550a7375
FB
2162 (devctl & MUSB_DEVCTL_BDEVICE)
2163 ? "B-Device" : "A-Device",
2164 musb_readb(mbase, MUSB_FADDR),
2165 musb->gadget_driver
2166 ? musb->gadget_driver->driver.name
2167 : NULL
2168 );
2169
2170 /* report disconnect, if we didn't already (flushing EP state) */
2171 if (musb->g.speed != USB_SPEED_UNKNOWN)
2172 musb_g_disconnect(musb);
2173
2174 /* clear HR */
2175 else if (devctl & MUSB_DEVCTL_HR)
2176 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2177
2178
2179 /* what speed did we negotiate? */
2180 power = musb_readb(mbase, MUSB_POWER);
2181 musb->g.speed = (power & MUSB_POWER_HSMODE)
2182 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2183
2184 /* start in USB_STATE_DEFAULT */
2185 musb->is_active = 1;
2186 musb->is_suspended = 0;
2187 MUSB_DEV_MODE(musb);
2188 musb->address = 0;
2189 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2190
2191 musb->may_wakeup = 0;
2192 musb->g.b_hnp_enable = 0;
2193 musb->g.a_alt_hnp_support = 0;
2194 musb->g.a_hnp_support = 0;
2195
2196 /* Normal reset, as B-Device;
2197 * or else after HNP, as A-Device
2198 */
2199 if (devctl & MUSB_DEVCTL_BDEVICE) {
84e250ff 2200 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
550a7375 2201 musb->g.is_a_peripheral = 0;
032ec49f 2202 } else {
84e250ff 2203 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
550a7375 2204 musb->g.is_a_peripheral = 1;
032ec49f 2205 }
550a7375
FB
2206
2207 /* start with default limits on VBUS power draw */
032ec49f 2208 (void) musb_gadget_vbus_draw(&musb->g, 8);
550a7375 2209}
This page took 0.439771 seconds and 5 git commands to generate.