blob: 0169dcf3a6ff62956188d30d19818533b9f8f8a3 [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
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
Sergei Shtylyovcea83242009-11-18 22:51:18 +03007 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
Felipe Balbi550a7372008-07-24 12:27:36 +03008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36#include <linux/kernel.h>
37#include <linux/list.h>
38#include <linux/timer.h>
39#include <linux/module.h>
40#include <linux/smp.h>
41#include <linux/spinlock.h>
42#include <linux/delay.h>
43#include <linux/moduleparam.h>
44#include <linux/stat.h>
45#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030047
48#include "musb_core.h"
49
50
51/* MUSB PERIPHERAL status 3-mar-2006:
52 *
53 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
54 * Minor glitches:
55 *
56 * + remote wakeup to Linux hosts work, but saw USBCV failures;
57 * in one test run (operator error?)
58 * + endpoint halt tests -- in both usbtest and usbcv -- seem
59 * to break when dma is enabled ... is something wrongly
60 * clearing SENDSTALL?
61 *
62 * - Mass storage behaved ok when last tested. Network traffic patterns
63 * (with lots of short transfers etc) need retesting; they turn up the
64 * worst cases of the DMA, since short packets are typical but are not
65 * required.
66 *
67 * - TX/IN
68 * + both pio and dma behave in with network and g_zero tests
69 * + no cppi throughput issues other than no-hw-queueing
70 * + failed with FLAT_REG (DaVinci)
71 * + seems to behave with double buffering, PIO -and- CPPI
72 * + with gadgetfs + AIO, requests got lost?
73 *
74 * - RX/OUT
75 * + both pio and dma behave in with network and g_zero tests
76 * + dma is slow in typical case (short_not_ok is clear)
77 * + double buffering ok with PIO
78 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
79 * + request lossage observed with gadgetfs
80 *
81 * - ISO not tested ... might work, but only weakly isochronous
82 *
83 * - Gadget driver disabling of softconnect during bind() is ignored; so
84 * drivers can't hold off host requests until userspace is ready.
85 * (Workaround: they can turn it off later.)
86 *
87 * - PORTABILITY (assumes PIO works):
88 * + DaVinci, basically works with cppi dma
89 * + OMAP 2430, ditto with mentor dma
90 * + TUSB 6010, platform-specific dma in the works
91 */
92
93/* ----------------------------------------------------------------------- */
94
Hema Kalliguddi92d27112010-11-15 04:24:01 -060095/* Maps the buffer to dma */
96
97static inline void map_dma_buffer(struct musb_request *request,
98 struct musb *musb)
99{
100 if (request->request.dma == DMA_ADDR_INVALID) {
101 request->request.dma = dma_map_single(
102 musb->controller,
103 request->request.buf,
104 request->request.length,
105 request->tx
106 ? DMA_TO_DEVICE
107 : DMA_FROM_DEVICE);
108 request->mapped = 1;
109 } else {
110 dma_sync_single_for_device(musb->controller,
111 request->request.dma,
112 request->request.length,
113 request->tx
114 ? DMA_TO_DEVICE
115 : DMA_FROM_DEVICE);
116 request->mapped = 0;
117 }
118}
119
120/* Unmap the buffer from dma and maps it back to cpu */
121static inline void unmap_dma_buffer(struct musb_request *request,
122 struct musb *musb)
123{
124 if (request->request.dma == DMA_ADDR_INVALID) {
125 DBG(20, "not unmapping a never mapped buffer\n");
126 return;
127 }
128 if (request->mapped) {
129 dma_unmap_single(musb->controller,
130 request->request.dma,
131 request->request.length,
132 request->tx
133 ? DMA_TO_DEVICE
134 : DMA_FROM_DEVICE);
135 request->request.dma = DMA_ADDR_INVALID;
136 request->mapped = 0;
137 } else {
138 dma_sync_single_for_cpu(musb->controller,
139 request->request.dma,
140 request->request.length,
141 request->tx
142 ? DMA_TO_DEVICE
143 : DMA_FROM_DEVICE);
144
145 }
146}
147
Felipe Balbi550a7372008-07-24 12:27:36 +0300148/*
149 * Immediately complete a request.
150 *
151 * @param request the request to complete
152 * @param status the status to complete the request with
153 * Context: controller locked, IRQs blocked.
154 */
155void musb_g_giveback(
156 struct musb_ep *ep,
157 struct usb_request *request,
158 int status)
159__releases(ep->musb->lock)
160__acquires(ep->musb->lock)
161{
162 struct musb_request *req;
163 struct musb *musb;
164 int busy = ep->busy;
165
166 req = to_musb_request(request);
167
168 list_del(&request->list);
169 if (req->request.status == -EINPROGRESS)
170 req->request.status = status;
171 musb = req->musb;
172
173 ep->busy = 1;
174 spin_unlock(&musb->lock);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600175 if (is_dma_capable() && ep->dma)
176 unmap_dma_buffer(req, musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300177 if (request->status == 0)
178 DBG(5, "%s done request %p, %d/%d\n",
179 ep->end_point.name, request,
180 req->request.actual, req->request.length);
181 else
182 DBG(2, "%s request %p, %d/%d fault %d\n",
183 ep->end_point.name, request,
184 req->request.actual, req->request.length,
185 request->status);
186 req->request.complete(&req->ep->end_point, &req->request);
187 spin_lock(&musb->lock);
188 ep->busy = busy;
189}
190
191/* ----------------------------------------------------------------------- */
192
193/*
194 * Abort requests queued to an endpoint using the status. Synchronous.
195 * caller locked controller and blocked irqs, and selected this ep.
196 */
197static void nuke(struct musb_ep *ep, const int status)
198{
199 struct musb_request *req = NULL;
200 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
201
202 ep->busy = 1;
203
204 if (is_dma_capable() && ep->dma) {
205 struct dma_controller *c = ep->musb->dma_controller;
206 int value;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700207
Felipe Balbi550a7372008-07-24 12:27:36 +0300208 if (ep->is_in) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700209 /*
210 * The programming guide says that we must not clear
211 * the DMAMODE bit before DMAENAB, so we only
212 * clear it in the second write...
213 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300214 musb_writew(epio, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700215 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
Felipe Balbi550a7372008-07-24 12:27:36 +0300216 musb_writew(epio, MUSB_TXCSR,
217 0 | MUSB_TXCSR_FLUSHFIFO);
218 } else {
219 musb_writew(epio, MUSB_RXCSR,
220 0 | MUSB_RXCSR_FLUSHFIFO);
221 musb_writew(epio, MUSB_RXCSR,
222 0 | MUSB_RXCSR_FLUSHFIFO);
223 }
224
225 value = c->channel_abort(ep->dma);
226 DBG(value ? 1 : 6, "%s: abort DMA --> %d\n", ep->name, value);
227 c->channel_release(ep->dma);
228 ep->dma = NULL;
229 }
230
231 while (!list_empty(&(ep->req_list))) {
232 req = container_of(ep->req_list.next, struct musb_request,
233 request.list);
234 musb_g_giveback(ep, &req->request, status);
235 }
236}
237
238/* ----------------------------------------------------------------------- */
239
240/* Data transfers - pure PIO, pure DMA, or mixed mode */
241
242/*
243 * This assumes the separate CPPI engine is responding to DMA requests
244 * from the usb core ... sequenced a bit differently from mentor dma.
245 */
246
247static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
248{
249 if (can_bulk_split(musb, ep->type))
250 return ep->hw_ep->max_packet_sz_tx;
251 else
252 return ep->packet_sz;
253}
254
255
256#ifdef CONFIG_USB_INVENTRA_DMA
257
258/* Peripheral tx (IN) using Mentor DMA works as follows:
259 Only mode 0 is used for transfers <= wPktSize,
260 mode 1 is used for larger transfers,
261
262 One of the following happens:
263 - Host sends IN token which causes an endpoint interrupt
264 -> TxAvail
265 -> if DMA is currently busy, exit.
266 -> if queue is non-empty, txstate().
267
268 - Request is queued by the gadget driver.
269 -> if queue was previously empty, txstate()
270
271 txstate()
272 -> start
273 /\ -> setup DMA
274 | (data is transferred to the FIFO, then sent out when
275 | IN token(s) are recd from Host.
276 | -> DMA interrupt on completion
277 | calls TxAvail.
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700278 | -> stop DMA, ~DMAENAB,
Felipe Balbi550a7372008-07-24 12:27:36 +0300279 | -> set TxPktRdy for last short pkt or zlp
280 | -> Complete Request
281 | -> Continue next request (call txstate)
282 |___________________________________|
283
284 * Non-Mentor DMA engines can of course work differently, such as by
285 * upleveling from irq-per-packet to irq-per-buffer.
286 */
287
288#endif
289
290/*
291 * An endpoint is transmitting data. This can be called either from
292 * the IRQ routine or from ep.queue() to kickstart a request on an
293 * endpoint.
294 *
295 * Context: controller locked, IRQs blocked, endpoint selected
296 */
297static void txstate(struct musb *musb, struct musb_request *req)
298{
299 u8 epnum = req->epnum;
300 struct musb_ep *musb_ep;
301 void __iomem *epio = musb->endpoints[epnum].regs;
302 struct usb_request *request;
303 u16 fifo_count = 0, csr;
304 int use_dma = 0;
305
306 musb_ep = req->ep;
307
308 /* we shouldn't get here while DMA is active ... but we do ... */
309 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
310 DBG(4, "dma pending...\n");
311 return;
312 }
313
314 /* read TXCSR before */
315 csr = musb_readw(epio, MUSB_TXCSR);
316
317 request = &req->request;
318 fifo_count = min(max_ep_writesize(musb, musb_ep),
319 (int)(request->length - request->actual));
320
321 if (csr & MUSB_TXCSR_TXPKTRDY) {
322 DBG(5, "%s old packet still ready , txcsr %03x\n",
323 musb_ep->end_point.name, csr);
324 return;
325 }
326
327 if (csr & MUSB_TXCSR_P_SENDSTALL) {
328 DBG(5, "%s stalling, txcsr %03x\n",
329 musb_ep->end_point.name, csr);
330 return;
331 }
332
333 DBG(4, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
334 epnum, musb_ep->packet_sz, fifo_count,
335 csr);
336
337#ifndef CONFIG_MUSB_PIO_ONLY
338 if (is_dma_capable() && musb_ep->dma) {
339 struct dma_controller *c = musb->dma_controller;
Ming Lei66af83d2010-09-20 10:32:06 +0300340 size_t request_size;
341
342 /* setup DMA, then program endpoint CSR */
343 request_size = min_t(size_t, request->length - request->actual,
344 musb_ep->dma->max_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300345
346 use_dma = (request->dma != DMA_ADDR_INVALID);
347
348 /* MUSB_TXCSR_P_ISO is still set correctly */
349
350#ifdef CONFIG_USB_INVENTRA_DMA
351 {
Anand Gadiyard1043a22009-04-02 12:07:08 -0700352 if (request_size < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300353 musb_ep->dma->desired_mode = 0;
354 else
355 musb_ep->dma->desired_mode = 1;
356
357 use_dma = use_dma && c->channel_program(
358 musb_ep->dma, musb_ep->packet_sz,
359 musb_ep->dma->desired_mode,
Cliff Cai796a83f2009-12-21 21:18:02 -0500360 request->dma + request->actual, request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300361 if (use_dma) {
362 if (musb_ep->dma->desired_mode == 0) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700363 /*
364 * We must not clear the DMAMODE bit
365 * before the DMAENAB bit -- and the
366 * latter doesn't always get cleared
367 * before we get here...
368 */
369 csr &= ~(MUSB_TXCSR_AUTOSET
370 | MUSB_TXCSR_DMAENAB);
371 musb_writew(epio, MUSB_TXCSR, csr
372 | MUSB_TXCSR_P_WZC_BITS);
373 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300374 csr |= (MUSB_TXCSR_DMAENAB |
375 MUSB_TXCSR_MODE);
376 /* against programming guide */
Ming Leif11d8932010-09-24 13:44:04 +0300377 } else {
378 csr |= (MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300379 | MUSB_TXCSR_DMAMODE
380 | MUSB_TXCSR_MODE);
Ming Leif11d8932010-09-24 13:44:04 +0300381 if (!musb_ep->hb_mult)
382 csr |= MUSB_TXCSR_AUTOSET;
383 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300384 csr &= ~MUSB_TXCSR_P_UNDERRUN;
Ming Leif11d8932010-09-24 13:44:04 +0300385
Felipe Balbi550a7372008-07-24 12:27:36 +0300386 musb_writew(epio, MUSB_TXCSR, csr);
387 }
388 }
389
390#elif defined(CONFIG_USB_TI_CPPI_DMA)
391 /* program endpoint CSR first, then setup DMA */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700392 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
Sergei Shtylyov37e3ee92009-03-27 12:53:32 -0700393 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
394 MUSB_TXCSR_MODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300395 musb_writew(epio, MUSB_TXCSR,
396 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
397 | csr);
398
399 /* ensure writebuffer is empty */
400 csr = musb_readw(epio, MUSB_TXCSR);
401
402 /* NOTE host side sets DMAENAB later than this; both are
403 * OK since the transfer dma glue (between CPPI and Mentor
404 * fifos) just tells CPPI it could start. Data only moves
405 * to the USB TX fifo when both fifos are ready.
406 */
407
408 /* "mode" is irrelevant here; handle terminating ZLPs like
409 * PIO does, since the hardware RNDIS mode seems unreliable
410 * except for the last-packet-is-already-short case.
411 */
412 use_dma = use_dma && c->channel_program(
413 musb_ep->dma, musb_ep->packet_sz,
414 0,
Ming Lei66af83d2010-09-20 10:32:06 +0300415 request->dma + request->actual,
416 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300417 if (!use_dma) {
418 c->channel_release(musb_ep->dma);
419 musb_ep->dma = NULL;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700420 csr &= ~MUSB_TXCSR_DMAENAB;
421 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300422 /* invariant: prequest->buf is non-null */
423 }
424#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
425 use_dma = use_dma && c->channel_program(
426 musb_ep->dma, musb_ep->packet_sz,
427 request->zero,
Ming Lei66af83d2010-09-20 10:32:06 +0300428 request->dma + request->actual,
429 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300430#endif
431 }
432#endif
433
434 if (!use_dma) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600435 /*
436 * Unmap the dma buffer back to cpu if dma channel
437 * programming fails
438 */
439 if (is_dma_capable() && musb_ep->dma)
440 unmap_dma_buffer(req, musb);
441
Felipe Balbi550a7372008-07-24 12:27:36 +0300442 musb_write_fifo(musb_ep->hw_ep, fifo_count,
443 (u8 *) (request->buf + request->actual));
444 request->actual += fifo_count;
445 csr |= MUSB_TXCSR_TXPKTRDY;
446 csr &= ~MUSB_TXCSR_P_UNDERRUN;
447 musb_writew(epio, MUSB_TXCSR, csr);
448 }
449
450 /* host may already have the data when this message shows... */
451 DBG(3, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
452 musb_ep->end_point.name, use_dma ? "dma" : "pio",
453 request->actual, request->length,
454 musb_readw(epio, MUSB_TXCSR),
455 fifo_count,
456 musb_readw(epio, MUSB_TXMAXP));
457}
458
459/*
460 * FIFO state update (e.g. data ready).
461 * Called from IRQ, with controller locked.
462 */
463void musb_g_tx(struct musb *musb, u8 epnum)
464{
465 u16 csr;
466 struct usb_request *request;
467 u8 __iomem *mbase = musb->mregs;
468 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
469 void __iomem *epio = musb->endpoints[epnum].regs;
470 struct dma_channel *dma;
471
472 musb_ep_select(mbase, epnum);
473 request = next_request(musb_ep);
474
475 csr = musb_readw(epio, MUSB_TXCSR);
476 DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
477
478 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300479
480 /*
481 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
482 * probably rates reporting as a host error.
483 */
484 if (csr & MUSB_TXCSR_P_SENTSTALL) {
485 csr |= MUSB_TXCSR_P_WZC_BITS;
486 csr &= ~MUSB_TXCSR_P_SENTSTALL;
487 musb_writew(epio, MUSB_TXCSR, csr);
488 return;
489 }
490
491 if (csr & MUSB_TXCSR_P_UNDERRUN) {
492 /* We NAKed, no big deal... little reason to care. */
493 csr |= MUSB_TXCSR_P_WZC_BITS;
494 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
495 musb_writew(epio, MUSB_TXCSR, csr);
496 DBG(20, "underrun on ep%d, req %p\n", epnum, request);
497 }
498
499 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
500 /*
501 * SHOULD NOT HAPPEN... has with CPPI though, after
502 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300503 */
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300504 DBG(5, "%s dma still busy?\n", musb_ep->end_point.name);
505 return;
506 }
507
508 if (request) {
509 u8 is_dma = 0;
510
511 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
512 is_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300513 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300514 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
515 MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +0300516 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300517 /* Ensure writebuffer is empty. */
518 csr = musb_readw(epio, MUSB_TXCSR);
519 request->actual += musb_ep->dma->actual_len;
520 DBG(4, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
521 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300522 }
523
Ming Leie7379aa2010-09-24 13:44:14 +0300524 /*
525 * First, maybe a terminating short packet. Some DMA
526 * engines might handle this by themselves.
527 */
528 if ((request->zero && request->length
529 && (request->length % musb_ep->packet_sz == 0)
530 && (request->actual == request->length))
Felipe Balbi550a7372008-07-24 12:27:36 +0300531#ifdef CONFIG_USB_INVENTRA_DMA
Ming Leie7379aa2010-09-24 13:44:14 +0300532 || (is_dma && (!dma->desired_mode ||
533 (request->actual &
534 (musb_ep->packet_sz - 1))))
Felipe Balbi550a7372008-07-24 12:27:36 +0300535#endif
Ming Leie7379aa2010-09-24 13:44:14 +0300536 ) {
537 /*
538 * On DMA completion, FIFO may not be
539 * available yet...
540 */
541 if (csr & MUSB_TXCSR_TXPKTRDY)
542 return;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300543
Ming Leie7379aa2010-09-24 13:44:14 +0300544 DBG(4, "sending zero pkt\n");
545 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
546 | MUSB_TXCSR_TXPKTRDY);
547 request->zero = 0;
548 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300549
Ming Leie7379aa2010-09-24 13:44:14 +0300550 if (request->actual == request->length) {
551 musb_g_giveback(musb_ep, request, 0);
552 request = musb_ep->desc ? next_request(musb_ep) : NULL;
553 if (!request) {
554 DBG(4, "%s idle now\n",
555 musb_ep->end_point.name);
556 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300557 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300558 }
559
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300560 txstate(musb, to_musb_request(request));
561 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300562}
563
564/* ------------------------------------------------------------ */
565
566#ifdef CONFIG_USB_INVENTRA_DMA
567
568/* Peripheral rx (OUT) using Mentor DMA works as follows:
569 - Only mode 0 is used.
570
571 - Request is queued by the gadget class driver.
572 -> if queue was previously empty, rxstate()
573
574 - Host sends OUT token which causes an endpoint interrupt
575 /\ -> RxReady
576 | -> if request queued, call rxstate
577 | /\ -> setup DMA
578 | | -> DMA interrupt on completion
579 | | -> RxReady
580 | | -> stop DMA
581 | | -> ack the read
582 | | -> if data recd = max expected
583 | | by the request, or host
584 | | sent a short packet,
585 | | complete the request,
586 | | and start the next one.
587 | |_____________________________________|
588 | else just wait for the host
589 | to send the next OUT token.
590 |__________________________________________________|
591
592 * Non-Mentor DMA engines can of course work differently.
593 */
594
595#endif
596
597/*
598 * Context: controller locked, IRQs blocked, endpoint selected
599 */
600static void rxstate(struct musb *musb, struct musb_request *req)
601{
Felipe Balbi550a7372008-07-24 12:27:36 +0300602 const u8 epnum = req->epnum;
603 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300604 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300605 void __iomem *epio = musb->endpoints[epnum].regs;
Felipe Balbic2c96322009-02-21 15:29:42 -0800606 unsigned fifo_count = 0;
Ming Leibd2e74d2010-09-20 10:32:01 +0300607 u16 len;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300608 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300609 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
610
611 if (hw_ep->is_shared_fifo)
612 musb_ep = &hw_ep->ep_in;
613 else
614 musb_ep = &hw_ep->ep_out;
615
616 len = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300617
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300618 /* We shouldn't get here while DMA is active, but we do... */
619 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
620 DBG(4, "DMA pending...\n");
621 return;
622 }
623
624 if (csr & MUSB_RXCSR_P_SENDSTALL) {
625 DBG(5, "%s stalling, RXCSR %04x\n",
626 musb_ep->end_point.name, csr);
627 return;
628 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300629
630 if (is_cppi_enabled() && musb_ep->dma) {
631 struct dma_controller *c = musb->dma_controller;
632 struct dma_channel *channel = musb_ep->dma;
633
634 /* NOTE: CPPI won't actually stop advancing the DMA
635 * queue after short packet transfers, so this is almost
636 * always going to run as IRQ-per-packet DMA so that
637 * faults will be handled correctly.
638 */
639 if (c->channel_program(channel,
640 musb_ep->packet_sz,
641 !request->short_not_ok,
642 request->dma + request->actual,
643 request->length - request->actual)) {
644
645 /* make sure that if an rxpkt arrived after the irq,
646 * the cppi engine will be ready to take it as soon
647 * as DMA is enabled
648 */
649 csr &= ~(MUSB_RXCSR_AUTOCLEAR
650 | MUSB_RXCSR_DMAMODE);
651 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
652 musb_writew(epio, MUSB_RXCSR, csr);
653 return;
654 }
655 }
656
657 if (csr & MUSB_RXCSR_RXPKTRDY) {
658 len = musb_readw(epio, MUSB_RXCOUNT);
659 if (request->actual < request->length) {
660#ifdef CONFIG_USB_INVENTRA_DMA
661 if (is_dma_capable() && musb_ep->dma) {
662 struct dma_controller *c;
663 struct dma_channel *channel;
664 int use_dma = 0;
665
666 c = musb->dma_controller;
667 channel = musb_ep->dma;
668
669 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
670 * mode 0 only. So we do not get endpoint interrupts due to DMA
671 * completion. We only get interrupts from DMA controller.
672 *
673 * We could operate in DMA mode 1 if we knew the size of the tranfer
674 * in advance. For mass storage class, request->length = what the host
675 * sends, so that'd work. But for pretty much everything else,
676 * request->length is routinely more than what the host sends. For
677 * most these gadgets, end of is signified either by a short packet,
678 * or filling the last byte of the buffer. (Sending extra data in
679 * that last pckate should trigger an overflow fault.) But in mode 1,
680 * we don't get DMA completion interrrupt for short packets.
681 *
682 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
683 * to get endpoint interrupt on every DMA req, but that didn't seem
684 * to work reliably.
685 *
686 * REVISIT an updated g_file_storage can set req->short_not_ok, which
687 * then becomes usable as a runtime "use mode 1" hint...
688 */
689
690 csr |= MUSB_RXCSR_DMAENAB;
Ming Lei490e5fb2010-09-20 10:32:03 +0300691#ifdef USE_MODE1
Ming Lei9001d802010-09-25 05:50:43 -0500692 csr |= MUSB_RXCSR_AUTOCLEAR;
Felipe Balbi550a7372008-07-24 12:27:36 +0300693 /* csr |= MUSB_RXCSR_DMAMODE; */
694
695 /* this special sequence (enabling and then
696 * disabling MUSB_RXCSR_DMAMODE) is required
697 * to get DMAReq to activate
698 */
699 musb_writew(epio, MUSB_RXCSR,
700 csr | MUSB_RXCSR_DMAMODE);
Ming Lei9001d802010-09-25 05:50:43 -0500701#else
702 if (!musb_ep->hb_mult &&
703 musb_ep->hw_ep->rx_double_buffered)
704 csr |= MUSB_RXCSR_AUTOCLEAR;
Felipe Balbi550a7372008-07-24 12:27:36 +0300705#endif
706 musb_writew(epio, MUSB_RXCSR, csr);
707
708 if (request->actual < request->length) {
709 int transfer_size = 0;
710#ifdef USE_MODE1
Ming Lei1018b4e2010-09-20 10:32:04 +0300711 transfer_size = min(request->length - request->actual,
Felipe Balbi550a7372008-07-24 12:27:36 +0300712 channel->max_len);
713#else
Ming Lei1018b4e2010-09-20 10:32:04 +0300714 transfer_size = min(request->length - request->actual,
715 (unsigned)len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300716#endif
717 if (transfer_size <= musb_ep->packet_sz)
718 musb_ep->dma->desired_mode = 0;
719 else
720 musb_ep->dma->desired_mode = 1;
721
722 use_dma = c->channel_program(
723 channel,
724 musb_ep->packet_sz,
725 channel->desired_mode,
726 request->dma
727 + request->actual,
728 transfer_size);
729 }
730
731 if (use_dma)
732 return;
733 }
734#endif /* Mentor's DMA */
735
736 fifo_count = request->length - request->actual;
737 DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
738 musb_ep->end_point.name,
739 len, fifo_count,
740 musb_ep->packet_sz);
741
Felipe Balbic2c96322009-02-21 15:29:42 -0800742 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300743
744#ifdef CONFIG_USB_TUSB_OMAP_DMA
745 if (tusb_dma_omap() && musb_ep->dma) {
746 struct dma_controller *c = musb->dma_controller;
747 struct dma_channel *channel = musb_ep->dma;
748 u32 dma_addr = request->dma + request->actual;
749 int ret;
750
751 ret = c->channel_program(channel,
752 musb_ep->packet_sz,
753 channel->desired_mode,
754 dma_addr,
755 fifo_count);
756 if (ret)
757 return;
758 }
759#endif
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600760 /*
761 * Unmap the dma buffer back to cpu if dma channel
762 * programming fails. This buffer is mapped if the
763 * channel allocation is successful
764 */
765 if (is_dma_capable() && musb_ep->dma) {
766 unmap_dma_buffer(req, musb);
767
768 /* Clear DMAENAB for the
769 * PIO mode transfer
770 */
771 csr &= ~MUSB_RXCSR_DMAENAB;
772 musb_writew(epio, MUSB_RXCSR, csr);
773 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300774
775 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
776 (request->buf + request->actual));
777 request->actual += fifo_count;
778
779 /* REVISIT if we left anything in the fifo, flush
780 * it and report -EOVERFLOW
781 */
782
783 /* ack the read! */
784 csr |= MUSB_RXCSR_P_WZC_BITS;
785 csr &= ~MUSB_RXCSR_RXPKTRDY;
786 musb_writew(epio, MUSB_RXCSR, csr);
787 }
788 }
789
790 /* reach the end or short packet detected */
791 if (request->actual == request->length || len < musb_ep->packet_sz)
792 musb_g_giveback(musb_ep, request, 0);
793}
794
795/*
796 * Data ready for a request; called from IRQ
797 */
798void musb_g_rx(struct musb *musb, u8 epnum)
799{
800 u16 csr;
801 struct usb_request *request;
802 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300803 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300804 void __iomem *epio = musb->endpoints[epnum].regs;
805 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300806 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
807
808 if (hw_ep->is_shared_fifo)
809 musb_ep = &hw_ep->ep_in;
810 else
811 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300812
813 musb_ep_select(mbase, epnum);
814
815 request = next_request(musb_ep);
Maulik Mankad0abdc362009-12-22 16:18:19 +0530816 if (!request)
817 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300818
819 csr = musb_readw(epio, MUSB_RXCSR);
820 dma = is_dma_capable() ? musb_ep->dma : NULL;
821
822 DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
823 csr, dma ? " (dma)" : "", request);
824
825 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300826 csr |= MUSB_RXCSR_P_WZC_BITS;
827 csr &= ~MUSB_RXCSR_P_SENTSTALL;
828 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300829 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300830 }
831
832 if (csr & MUSB_RXCSR_P_OVERRUN) {
833 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
834 csr &= ~MUSB_RXCSR_P_OVERRUN;
835 musb_writew(epio, MUSB_RXCSR, csr);
836
837 DBG(3, "%s iso overrun on %p\n", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300838 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300839 request->status = -EOVERFLOW;
840 }
841 if (csr & MUSB_RXCSR_INCOMPRX) {
842 /* REVISIT not necessarily an error */
843 DBG(4, "%s, incomprx\n", musb_ep->end_point.name);
844 }
845
846 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
847 /* "should not happen"; likely RXPKTRDY pending for DMA */
848 DBG((csr & MUSB_RXCSR_DMAENAB) ? 4 : 1,
849 "%s busy, csr %04x\n",
850 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300851 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300852 }
853
854 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
855 csr &= ~(MUSB_RXCSR_AUTOCLEAR
856 | MUSB_RXCSR_DMAENAB
857 | MUSB_RXCSR_DMAMODE);
858 musb_writew(epio, MUSB_RXCSR,
859 MUSB_RXCSR_P_WZC_BITS | csr);
860
861 request->actual += musb_ep->dma->actual_len;
862
863 DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
864 epnum, csr,
865 musb_readw(epio, MUSB_RXCSR),
866 musb_ep->dma->actual_len, request);
867
868#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
869 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -0500870 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +0300871 || (dma->actual_len
872 & (musb_ep->packet_sz - 1))) {
873 /* ack the read! */
874 csr &= ~MUSB_RXCSR_RXPKTRDY;
875 musb_writew(epio, MUSB_RXCSR, csr);
876 }
877
878 /* incomplete, and not short? wait for next IN packet */
879 if ((request->actual < request->length)
880 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -0500881 == musb_ep->packet_sz)) {
882 /* In double buffer case, continue to unload fifo if
883 * there is Rx packet in FIFO.
884 **/
885 csr = musb_readw(epio, MUSB_RXCSR);
886 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
887 hw_ep->rx_double_buffered)
888 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300889 return;
Ming Lei9001d802010-09-25 05:50:43 -0500890 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300891#endif
892 musb_g_giveback(musb_ep, request, 0);
893
894 request = next_request(musb_ep);
895 if (!request)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300896 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300897 }
Ming Lei9001d802010-09-25 05:50:43 -0500898exit:
Sergei Shtylyov43467862010-09-24 13:44:12 +0300899 /* Analyze request */
900 rxstate(musb, to_musb_request(request));
Felipe Balbi550a7372008-07-24 12:27:36 +0300901}
902
903/* ------------------------------------------------------------ */
904
905static int musb_gadget_enable(struct usb_ep *ep,
906 const struct usb_endpoint_descriptor *desc)
907{
908 unsigned long flags;
909 struct musb_ep *musb_ep;
910 struct musb_hw_ep *hw_ep;
911 void __iomem *regs;
912 struct musb *musb;
913 void __iomem *mbase;
914 u8 epnum;
915 u16 csr;
916 unsigned tmp;
917 int status = -EINVAL;
918
919 if (!ep || !desc)
920 return -EINVAL;
921
922 musb_ep = to_musb_ep(ep);
923 hw_ep = musb_ep->hw_ep;
924 regs = hw_ep->regs;
925 musb = musb_ep->musb;
926 mbase = musb->mregs;
927 epnum = musb_ep->current_epnum;
928
929 spin_lock_irqsave(&musb->lock, flags);
930
931 if (musb_ep->desc) {
932 status = -EBUSY;
933 goto fail;
934 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800935 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300936
937 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800938 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300939 goto fail;
940
941 /* REVISIT this rules out high bandwidth periodic transfers */
942 tmp = le16_to_cpu(desc->wMaxPacketSize);
Ming Leif11d8932010-09-24 13:44:04 +0300943 if (tmp & ~0x07ff) {
944 int ok;
945
946 if (usb_endpoint_dir_in(desc))
947 ok = musb->hb_iso_tx;
948 else
949 ok = musb->hb_iso_rx;
950
951 if (!ok) {
952 DBG(4, "%s: not support ISO high bandwidth\n", __func__);
953 goto fail;
954 }
955 musb_ep->hb_mult = (tmp >> 11) & 3;
956 } else {
957 musb_ep->hb_mult = 0;
958 }
959
960 musb_ep->packet_sz = tmp & 0x7ff;
961 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300962
963 /* enable the interrupts for the endpoint, set the endpoint
964 * packet size (or fail), set the mode, clear the fifo
965 */
966 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -0800967 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300968 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
969
970 if (hw_ep->is_shared_fifo)
971 musb_ep->is_in = 1;
972 if (!musb_ep->is_in)
973 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +0300974
975 if (tmp > hw_ep->max_packet_sz_tx) {
976 DBG(4, "%s: packet size beyond hw fifo size\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +0300977 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +0300978 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300979
980 int_txe |= (1 << epnum);
981 musb_writew(mbase, MUSB_INTRTXE, int_txe);
982
983 /* REVISIT if can_bulk_split(), use by updating "tmp";
984 * likewise high bandwidth periodic tx
985 */
Cliff Cai9f445cb2010-01-28 20:44:18 -0500986 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -0500987 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -0500988 */
Ming Lei31c99092010-10-19 19:08:25 -0500989 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +0300990
991 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
992 if (musb_readw(regs, MUSB_TXCSR)
993 & MUSB_TXCSR_FIFONOTEMPTY)
994 csr |= MUSB_TXCSR_FLUSHFIFO;
995 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
996 csr |= MUSB_TXCSR_P_ISO;
997
998 /* set twice in case of double buffering */
999 musb_writew(regs, MUSB_TXCSR, csr);
1000 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1001 musb_writew(regs, MUSB_TXCSR, csr);
1002
1003 } else {
1004 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
1005
1006 if (hw_ep->is_shared_fifo)
1007 musb_ep->is_in = 0;
1008 if (musb_ep->is_in)
1009 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001010
1011 if (tmp > hw_ep->max_packet_sz_rx) {
1012 DBG(4, "%s: packet size beyond hw fifo size\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001013 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001014 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001015
1016 int_rxe |= (1 << epnum);
1017 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
1018
1019 /* REVISIT if can_bulk_combine() use by updating "tmp"
1020 * likewise high bandwidth periodic rx
1021 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001022 /* Set RXMAXP with the FIFO size of the endpoint
1023 * to disable double buffering mode.
1024 */
Ming Lei31c99092010-10-19 19:08:25 -05001025 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001026
1027 /* force shared fifo to OUT-only mode */
1028 if (hw_ep->is_shared_fifo) {
1029 csr = musb_readw(regs, MUSB_TXCSR);
1030 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1031 musb_writew(regs, MUSB_TXCSR, csr);
1032 }
1033
1034 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1035 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1036 csr |= MUSB_RXCSR_P_ISO;
1037 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1038 csr |= MUSB_RXCSR_DISNYET;
1039
1040 /* set twice in case of double buffering */
1041 musb_writew(regs, MUSB_RXCSR, csr);
1042 musb_writew(regs, MUSB_RXCSR, csr);
1043 }
1044
1045 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1046 * for some reason you run out of channels here.
1047 */
1048 if (is_dma_capable() && musb->dma_controller) {
1049 struct dma_controller *c = musb->dma_controller;
1050
1051 musb_ep->dma = c->channel_alloc(c, hw_ep,
1052 (desc->bEndpointAddress & USB_DIR_IN));
1053 } else
1054 musb_ep->dma = NULL;
1055
1056 musb_ep->desc = desc;
1057 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001058 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001059 status = 0;
1060
1061 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1062 musb_driver_name, musb_ep->end_point.name,
1063 ({ char *s; switch (musb_ep->type) {
1064 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1065 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1066 default: s = "iso"; break;
1067 }; s; }),
1068 musb_ep->is_in ? "IN" : "OUT",
1069 musb_ep->dma ? "dma, " : "",
1070 musb_ep->packet_sz);
1071
1072 schedule_work(&musb->irq_work);
1073
1074fail:
1075 spin_unlock_irqrestore(&musb->lock, flags);
1076 return status;
1077}
1078
1079/*
1080 * Disable an endpoint flushing all requests queued.
1081 */
1082static int musb_gadget_disable(struct usb_ep *ep)
1083{
1084 unsigned long flags;
1085 struct musb *musb;
1086 u8 epnum;
1087 struct musb_ep *musb_ep;
1088 void __iomem *epio;
1089 int status = 0;
1090
1091 musb_ep = to_musb_ep(ep);
1092 musb = musb_ep->musb;
1093 epnum = musb_ep->current_epnum;
1094 epio = musb->endpoints[epnum].regs;
1095
1096 spin_lock_irqsave(&musb->lock, flags);
1097 musb_ep_select(musb->mregs, epnum);
1098
1099 /* zero the endpoint sizes */
1100 if (musb_ep->is_in) {
1101 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1102 int_txe &= ~(1 << epnum);
1103 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1104 musb_writew(epio, MUSB_TXMAXP, 0);
1105 } else {
1106 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1107 int_rxe &= ~(1 << epnum);
1108 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1109 musb_writew(epio, MUSB_RXMAXP, 0);
1110 }
1111
1112 musb_ep->desc = NULL;
1113
1114 /* abort all pending DMA and requests */
1115 nuke(musb_ep, -ESHUTDOWN);
1116
1117 schedule_work(&musb->irq_work);
1118
1119 spin_unlock_irqrestore(&(musb->lock), flags);
1120
1121 DBG(2, "%s\n", musb_ep->end_point.name);
1122
1123 return status;
1124}
1125
1126/*
1127 * Allocate a request for an endpoint.
1128 * Reused by ep0 code.
1129 */
1130struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1131{
1132 struct musb_ep *musb_ep = to_musb_ep(ep);
1133 struct musb_request *request = NULL;
1134
1135 request = kzalloc(sizeof *request, gfp_flags);
1136 if (request) {
1137 INIT_LIST_HEAD(&request->request.list);
1138 request->request.dma = DMA_ADDR_INVALID;
1139 request->epnum = musb_ep->current_epnum;
1140 request->ep = musb_ep;
1141 }
1142
1143 return &request->request;
1144}
1145
1146/*
1147 * Free a request
1148 * Reused by ep0 code.
1149 */
1150void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1151{
1152 kfree(to_musb_request(req));
1153}
1154
1155static LIST_HEAD(buffers);
1156
1157struct free_record {
1158 struct list_head list;
1159 struct device *dev;
1160 unsigned bytes;
1161 dma_addr_t dma;
1162};
1163
1164/*
1165 * Context: controller locked, IRQs blocked.
1166 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001167void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001168{
1169 DBG(3, "<== %s request %p len %u on hw_ep%d\n",
1170 req->tx ? "TX/IN" : "RX/OUT",
1171 &req->request, req->request.length, req->epnum);
1172
1173 musb_ep_select(musb->mregs, req->epnum);
1174 if (req->tx)
1175 txstate(musb, req);
1176 else
1177 rxstate(musb, req);
1178}
1179
1180static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1181 gfp_t gfp_flags)
1182{
1183 struct musb_ep *musb_ep;
1184 struct musb_request *request;
1185 struct musb *musb;
1186 int status = 0;
1187 unsigned long lockflags;
1188
1189 if (!ep || !req)
1190 return -EINVAL;
1191 if (!req->buf)
1192 return -ENODATA;
1193
1194 musb_ep = to_musb_ep(ep);
1195 musb = musb_ep->musb;
1196
1197 request = to_musb_request(req);
1198 request->musb = musb;
1199
1200 if (request->ep != musb_ep)
1201 return -EINVAL;
1202
1203 DBG(4, "<== to %s request=%p\n", ep->name, req);
1204
1205 /* request is mine now... */
1206 request->request.actual = 0;
1207 request->request.status = -EINPROGRESS;
1208 request->epnum = musb_ep->current_epnum;
1209 request->tx = musb_ep->is_in;
1210
Hema Kalliguddi92d27112010-11-15 04:24:01 -06001211 if (is_dma_capable() && musb_ep->dma)
1212 map_dma_buffer(request, musb);
1213 else
Felipe Balbi550a7372008-07-24 12:27:36 +03001214 request->mapped = 0;
1215
1216 spin_lock_irqsave(&musb->lock, lockflags);
1217
1218 /* don't queue if the ep is down */
1219 if (!musb_ep->desc) {
1220 DBG(4, "req %p queued to %s while ep %s\n",
1221 req, ep->name, "disabled");
1222 status = -ESHUTDOWN;
1223 goto cleanup;
1224 }
1225
1226 /* add request to the list */
1227 list_add_tail(&(request->request.list), &(musb_ep->req_list));
1228
1229 /* it this is the head of the queue, start i/o ... */
1230 if (!musb_ep->busy && &request->request.list == musb_ep->req_list.next)
1231 musb_ep_restart(musb, request);
1232
1233cleanup:
1234 spin_unlock_irqrestore(&musb->lock, lockflags);
1235 return status;
1236}
1237
1238static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1239{
1240 struct musb_ep *musb_ep = to_musb_ep(ep);
1241 struct usb_request *r;
1242 unsigned long flags;
1243 int status = 0;
1244 struct musb *musb = musb_ep->musb;
1245
1246 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1247 return -EINVAL;
1248
1249 spin_lock_irqsave(&musb->lock, flags);
1250
1251 list_for_each_entry(r, &musb_ep->req_list, list) {
1252 if (r == request)
1253 break;
1254 }
1255 if (r != request) {
1256 DBG(3, "request %p not queued to %s\n", request, ep->name);
1257 status = -EINVAL;
1258 goto done;
1259 }
1260
1261 /* if the hardware doesn't have the request, easy ... */
1262 if (musb_ep->req_list.next != &request->list || musb_ep->busy)
1263 musb_g_giveback(musb_ep, request, -ECONNRESET);
1264
1265 /* ... else abort the dma transfer ... */
1266 else if (is_dma_capable() && musb_ep->dma) {
1267 struct dma_controller *c = musb->dma_controller;
1268
1269 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1270 if (c->channel_abort)
1271 status = c->channel_abort(musb_ep->dma);
1272 else
1273 status = -EBUSY;
1274 if (status == 0)
1275 musb_g_giveback(musb_ep, request, -ECONNRESET);
1276 } else {
1277 /* NOTE: by sticking to easily tested hardware/driver states,
1278 * we leave counting of in-flight packets imprecise.
1279 */
1280 musb_g_giveback(musb_ep, request, -ECONNRESET);
1281 }
1282
1283done:
1284 spin_unlock_irqrestore(&musb->lock, flags);
1285 return status;
1286}
1287
1288/*
1289 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1290 * data but will queue requests.
1291 *
1292 * exported to ep0 code
1293 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001294static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001295{
1296 struct musb_ep *musb_ep = to_musb_ep(ep);
1297 u8 epnum = musb_ep->current_epnum;
1298 struct musb *musb = musb_ep->musb;
1299 void __iomem *epio = musb->endpoints[epnum].regs;
1300 void __iomem *mbase;
1301 unsigned long flags;
1302 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001303 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001304 int status = 0;
1305
1306 if (!ep)
1307 return -EINVAL;
1308 mbase = musb->mregs;
1309
1310 spin_lock_irqsave(&musb->lock, flags);
1311
1312 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1313 status = -EINVAL;
1314 goto done;
1315 }
1316
1317 musb_ep_select(mbase, epnum);
1318
Felipe Balbi550a7372008-07-24 12:27:36 +03001319 request = to_musb_request(next_request(musb_ep));
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001320 if (value) {
1321 if (request) {
1322 DBG(3, "request in progress, cannot halt %s\n",
1323 ep->name);
1324 status = -EAGAIN;
1325 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001326 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001327 /* Cannot portably stall with non-empty FIFO */
1328 if (musb_ep->is_in) {
1329 csr = musb_readw(epio, MUSB_TXCSR);
1330 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1331 DBG(3, "FIFO busy, cannot halt %s\n", ep->name);
1332 status = -EAGAIN;
1333 goto done;
1334 }
1335 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001336 } else
1337 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001338
1339 /* set/clear the stall and toggle bits */
1340 DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1341 if (musb_ep->is_in) {
1342 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001343 csr |= MUSB_TXCSR_P_WZC_BITS
1344 | MUSB_TXCSR_CLRDATATOG;
1345 if (value)
1346 csr |= MUSB_TXCSR_P_SENDSTALL;
1347 else
1348 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1349 | MUSB_TXCSR_P_SENTSTALL);
1350 csr &= ~MUSB_TXCSR_TXPKTRDY;
1351 musb_writew(epio, MUSB_TXCSR, csr);
1352 } else {
1353 csr = musb_readw(epio, MUSB_RXCSR);
1354 csr |= MUSB_RXCSR_P_WZC_BITS
1355 | MUSB_RXCSR_FLUSHFIFO
1356 | MUSB_RXCSR_CLRDATATOG;
1357 if (value)
1358 csr |= MUSB_RXCSR_P_SENDSTALL;
1359 else
1360 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1361 | MUSB_RXCSR_P_SENTSTALL);
1362 musb_writew(epio, MUSB_RXCSR, csr);
1363 }
1364
Felipe Balbi550a7372008-07-24 12:27:36 +03001365 /* maybe start the first request in the queue */
1366 if (!musb_ep->busy && !value && request) {
1367 DBG(3, "restarting the request\n");
1368 musb_ep_restart(musb, request);
1369 }
1370
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001371done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001372 spin_unlock_irqrestore(&musb->lock, flags);
1373 return status;
1374}
1375
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001376/*
1377 * Sets the halt feature with the clear requests ignored
1378 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001379static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001380{
1381 struct musb_ep *musb_ep = to_musb_ep(ep);
1382
1383 if (!ep)
1384 return -EINVAL;
1385
1386 musb_ep->wedged = 1;
1387
1388 return usb_ep_set_halt(ep);
1389}
1390
Felipe Balbi550a7372008-07-24 12:27:36 +03001391static int musb_gadget_fifo_status(struct usb_ep *ep)
1392{
1393 struct musb_ep *musb_ep = to_musb_ep(ep);
1394 void __iomem *epio = musb_ep->hw_ep->regs;
1395 int retval = -EINVAL;
1396
1397 if (musb_ep->desc && !musb_ep->is_in) {
1398 struct musb *musb = musb_ep->musb;
1399 int epnum = musb_ep->current_epnum;
1400 void __iomem *mbase = musb->mregs;
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(&musb->lock, flags);
1404
1405 musb_ep_select(mbase, epnum);
1406 /* FIXME return zero unless RXPKTRDY is set */
1407 retval = musb_readw(epio, MUSB_RXCOUNT);
1408
1409 spin_unlock_irqrestore(&musb->lock, flags);
1410 }
1411 return retval;
1412}
1413
1414static void musb_gadget_fifo_flush(struct usb_ep *ep)
1415{
1416 struct musb_ep *musb_ep = to_musb_ep(ep);
1417 struct musb *musb = musb_ep->musb;
1418 u8 epnum = musb_ep->current_epnum;
1419 void __iomem *epio = musb->endpoints[epnum].regs;
1420 void __iomem *mbase;
1421 unsigned long flags;
1422 u16 csr, int_txe;
1423
1424 mbase = musb->mregs;
1425
1426 spin_lock_irqsave(&musb->lock, flags);
1427 musb_ep_select(mbase, (u8) epnum);
1428
1429 /* disable interrupts */
1430 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1431 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1432
1433 if (musb_ep->is_in) {
1434 csr = musb_readw(epio, MUSB_TXCSR);
1435 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1436 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1437 musb_writew(epio, MUSB_TXCSR, csr);
1438 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1439 musb_writew(epio, MUSB_TXCSR, csr);
1440 }
1441 } else {
1442 csr = musb_readw(epio, MUSB_RXCSR);
1443 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1444 musb_writew(epio, MUSB_RXCSR, csr);
1445 musb_writew(epio, MUSB_RXCSR, csr);
1446 }
1447
1448 /* re-enable interrupt */
1449 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1450 spin_unlock_irqrestore(&musb->lock, flags);
1451}
1452
1453static const struct usb_ep_ops musb_ep_ops = {
1454 .enable = musb_gadget_enable,
1455 .disable = musb_gadget_disable,
1456 .alloc_request = musb_alloc_request,
1457 .free_request = musb_free_request,
1458 .queue = musb_gadget_queue,
1459 .dequeue = musb_gadget_dequeue,
1460 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001461 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001462 .fifo_status = musb_gadget_fifo_status,
1463 .fifo_flush = musb_gadget_fifo_flush
1464};
1465
1466/* ----------------------------------------------------------------------- */
1467
1468static int musb_gadget_get_frame(struct usb_gadget *gadget)
1469{
1470 struct musb *musb = gadget_to_musb(gadget);
1471
1472 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1473}
1474
1475static int musb_gadget_wakeup(struct usb_gadget *gadget)
1476{
1477 struct musb *musb = gadget_to_musb(gadget);
1478 void __iomem *mregs = musb->mregs;
1479 unsigned long flags;
1480 int status = -EINVAL;
1481 u8 power, devctl;
1482 int retries;
1483
1484 spin_lock_irqsave(&musb->lock, flags);
1485
David Brownell84e250f2009-03-31 12:30:04 -07001486 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001487 case OTG_STATE_B_PERIPHERAL:
1488 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1489 * that's part of the standard usb 1.1 state machine, and
1490 * doesn't affect OTG transitions.
1491 */
1492 if (musb->may_wakeup && musb->is_suspended)
1493 break;
1494 goto done;
1495 case OTG_STATE_B_IDLE:
1496 /* Start SRP ... OTG not required. */
1497 devctl = musb_readb(mregs, MUSB_DEVCTL);
1498 DBG(2, "Sending SRP: devctl: %02x\n", devctl);
1499 devctl |= MUSB_DEVCTL_SESSION;
1500 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1501 devctl = musb_readb(mregs, MUSB_DEVCTL);
1502 retries = 100;
1503 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1504 devctl = musb_readb(mregs, MUSB_DEVCTL);
1505 if (retries-- < 1)
1506 break;
1507 }
1508 retries = 10000;
1509 while (devctl & MUSB_DEVCTL_SESSION) {
1510 devctl = musb_readb(mregs, MUSB_DEVCTL);
1511 if (retries-- < 1)
1512 break;
1513 }
1514
1515 /* Block idling for at least 1s */
1516 musb_platform_try_idle(musb,
1517 jiffies + msecs_to_jiffies(1 * HZ));
1518
1519 status = 0;
1520 goto done;
1521 default:
1522 DBG(2, "Unhandled wake: %s\n", otg_state_string(musb));
1523 goto done;
1524 }
1525
1526 status = 0;
1527
1528 power = musb_readb(mregs, MUSB_POWER);
1529 power |= MUSB_POWER_RESUME;
1530 musb_writeb(mregs, MUSB_POWER, power);
1531 DBG(2, "issue wakeup\n");
1532
1533 /* FIXME do this next chunk in a timer callback, no udelay */
1534 mdelay(2);
1535
1536 power = musb_readb(mregs, MUSB_POWER);
1537 power &= ~MUSB_POWER_RESUME;
1538 musb_writeb(mregs, MUSB_POWER, power);
1539done:
1540 spin_unlock_irqrestore(&musb->lock, flags);
1541 return status;
1542}
1543
1544static int
1545musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1546{
1547 struct musb *musb = gadget_to_musb(gadget);
1548
1549 musb->is_self_powered = !!is_selfpowered;
1550 return 0;
1551}
1552
1553static void musb_pullup(struct musb *musb, int is_on)
1554{
1555 u8 power;
1556
1557 power = musb_readb(musb->mregs, MUSB_POWER);
1558 if (is_on)
1559 power |= MUSB_POWER_SOFTCONN;
1560 else
1561 power &= ~MUSB_POWER_SOFTCONN;
1562
1563 /* FIXME if on, HdrcStart; if off, HdrcStop */
1564
1565 DBG(3, "gadget %s D+ pullup %s\n",
1566 musb->gadget_driver->function, is_on ? "on" : "off");
1567 musb_writeb(musb->mregs, MUSB_POWER, power);
1568}
1569
1570#if 0
1571static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1572{
1573 DBG(2, "<= %s =>\n", __func__);
1574
1575 /*
1576 * FIXME iff driver's softconnect flag is set (as it is during probe,
1577 * though that can clear it), just musb_pullup().
1578 */
1579
1580 return -EINVAL;
1581}
1582#endif
1583
1584static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1585{
1586 struct musb *musb = gadget_to_musb(gadget);
1587
David Brownell84e250f2009-03-31 12:30:04 -07001588 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001589 return -EOPNOTSUPP;
David Brownell84e250f2009-03-31 12:30:04 -07001590 return otg_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001591}
1592
1593static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1594{
1595 struct musb *musb = gadget_to_musb(gadget);
1596 unsigned long flags;
1597
1598 is_on = !!is_on;
1599
1600 /* NOTE: this assumes we are sensing vbus; we'd rather
1601 * not pullup unless the B-session is active.
1602 */
1603 spin_lock_irqsave(&musb->lock, flags);
1604 if (is_on != musb->softconnect) {
1605 musb->softconnect = is_on;
1606 musb_pullup(musb, is_on);
1607 }
1608 spin_unlock_irqrestore(&musb->lock, flags);
1609 return 0;
1610}
1611
1612static const struct usb_gadget_ops musb_gadget_operations = {
1613 .get_frame = musb_gadget_get_frame,
1614 .wakeup = musb_gadget_wakeup,
1615 .set_selfpowered = musb_gadget_set_self_powered,
1616 /* .vbus_session = musb_gadget_vbus_session, */
1617 .vbus_draw = musb_gadget_vbus_draw,
1618 .pullup = musb_gadget_pullup,
1619};
1620
1621/* ----------------------------------------------------------------------- */
1622
1623/* Registration */
1624
1625/* Only this registration code "knows" the rule (from USB standards)
1626 * about there being only one external upstream port. It assumes
1627 * all peripheral ports are external...
1628 */
1629static struct musb *the_gadget;
1630
1631static void musb_gadget_release(struct device *dev)
1632{
1633 /* kref_put(WHAT) */
1634 dev_dbg(dev, "%s\n", __func__);
1635}
1636
1637
1638static void __init
1639init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1640{
1641 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1642
1643 memset(ep, 0, sizeof *ep);
1644
1645 ep->current_epnum = epnum;
1646 ep->musb = musb;
1647 ep->hw_ep = hw_ep;
1648 ep->is_in = is_in;
1649
1650 INIT_LIST_HEAD(&ep->req_list);
1651
1652 sprintf(ep->name, "ep%d%s", epnum,
1653 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1654 is_in ? "in" : "out"));
1655 ep->end_point.name = ep->name;
1656 INIT_LIST_HEAD(&ep->end_point.ep_list);
1657 if (!epnum) {
1658 ep->end_point.maxpacket = 64;
1659 ep->end_point.ops = &musb_g_ep0_ops;
1660 musb->g.ep0 = &ep->end_point;
1661 } else {
1662 if (is_in)
1663 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1664 else
1665 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1666 ep->end_point.ops = &musb_ep_ops;
1667 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1668 }
1669}
1670
1671/*
1672 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1673 * to the rest of the driver state.
1674 */
1675static inline void __init musb_g_init_endpoints(struct musb *musb)
1676{
1677 u8 epnum;
1678 struct musb_hw_ep *hw_ep;
1679 unsigned count = 0;
1680
1681 /* intialize endpoint list just once */
1682 INIT_LIST_HEAD(&(musb->g.ep_list));
1683
1684 for (epnum = 0, hw_ep = musb->endpoints;
1685 epnum < musb->nr_endpoints;
1686 epnum++, hw_ep++) {
1687 if (hw_ep->is_shared_fifo /* || !epnum */) {
1688 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1689 count++;
1690 } else {
1691 if (hw_ep->max_packet_sz_tx) {
1692 init_peripheral_ep(musb, &hw_ep->ep_in,
1693 epnum, 1);
1694 count++;
1695 }
1696 if (hw_ep->max_packet_sz_rx) {
1697 init_peripheral_ep(musb, &hw_ep->ep_out,
1698 epnum, 0);
1699 count++;
1700 }
1701 }
1702 }
1703}
1704
1705/* called once during driver setup to initialize and link into
1706 * the driver model; memory is zeroed.
1707 */
1708int __init musb_gadget_setup(struct musb *musb)
1709{
1710 int status;
1711
1712 /* REVISIT minor race: if (erroneously) setting up two
1713 * musb peripherals at the same time, only the bus lock
1714 * is probably held.
1715 */
1716 if (the_gadget)
1717 return -EBUSY;
1718 the_gadget = musb;
1719
1720 musb->g.ops = &musb_gadget_operations;
1721 musb->g.is_dualspeed = 1;
1722 musb->g.speed = USB_SPEED_UNKNOWN;
1723
1724 /* this "gadget" abstracts/virtualizes the controller */
Kay Sievers427c4f32008-11-07 01:52:53 +01001725 dev_set_name(&musb->g.dev, "gadget");
Felipe Balbi550a7372008-07-24 12:27:36 +03001726 musb->g.dev.parent = musb->controller;
1727 musb->g.dev.dma_mask = musb->controller->dma_mask;
1728 musb->g.dev.release = musb_gadget_release;
1729 musb->g.name = musb_driver_name;
1730
1731 if (is_otg_enabled(musb))
1732 musb->g.is_otg = 1;
1733
1734 musb_g_init_endpoints(musb);
1735
1736 musb->is_active = 0;
1737 musb_platform_try_idle(musb, 0);
1738
1739 status = device_register(&musb->g.dev);
Rahul Ruikare2c34042010-10-02 01:35:48 -05001740 if (status != 0) {
1741 put_device(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001742 the_gadget = NULL;
Rahul Ruikare2c34042010-10-02 01:35:48 -05001743 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001744 return status;
1745}
1746
1747void musb_gadget_cleanup(struct musb *musb)
1748{
1749 if (musb != the_gadget)
1750 return;
1751
1752 device_unregister(&musb->g.dev);
1753 the_gadget = NULL;
1754}
1755
1756/*
1757 * Register the gadget driver. Used by gadget drivers when
1758 * registering themselves with the controller.
1759 *
1760 * -EINVAL something went wrong (not driver)
1761 * -EBUSY another gadget is already using the controller
1762 * -ENOMEM no memeory to perform the operation
1763 *
1764 * @param driver the gadget driver
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001765 * @param bind the driver's bind function
Felipe Balbi550a7372008-07-24 12:27:36 +03001766 * @return <0 if error, 0 if everything is fine
1767 */
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001768int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1769 int (*bind)(struct usb_gadget *))
Felipe Balbi550a7372008-07-24 12:27:36 +03001770{
1771 int retval;
1772 unsigned long flags;
1773 struct musb *musb = the_gadget;
1774
1775 if (!driver
1776 || driver->speed != USB_SPEED_HIGH
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001777 || !bind || !driver->setup)
Felipe Balbi550a7372008-07-24 12:27:36 +03001778 return -EINVAL;
1779
1780 /* driver must be initialized to support peripheral mode */
Roel Kluin08e6c972010-02-02 14:47:17 -08001781 if (!musb) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001782 DBG(1, "%s, no dev??\n", __func__);
1783 return -ENODEV;
1784 }
1785
1786 DBG(3, "registering driver %s\n", driver->function);
1787 spin_lock_irqsave(&musb->lock, flags);
1788
1789 if (musb->gadget_driver) {
1790 DBG(1, "%s is already bound to %s\n",
1791 musb_driver_name,
1792 musb->gadget_driver->driver.name);
1793 retval = -EBUSY;
1794 } else {
1795 musb->gadget_driver = driver;
1796 musb->g.dev.driver = &driver->driver;
1797 driver->driver.bus = NULL;
1798 musb->softconnect = 1;
1799 retval = 0;
1800 }
1801
1802 spin_unlock_irqrestore(&musb->lock, flags);
1803
Felipe Balbi550a7372008-07-24 12:27:36 +03001804 if (retval == 0) {
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001805 retval = bind(&musb->g);
Felipe Balbif362a472008-08-04 13:53:52 +03001806 if (retval != 0) {
1807 DBG(3, "bind to driver %s failed --> %d\n",
1808 driver->driver.name, retval);
1809 musb->gadget_driver = NULL;
1810 musb->g.dev.driver = NULL;
1811 }
1812
Felipe Balbi550a7372008-07-24 12:27:36 +03001813 spin_lock_irqsave(&musb->lock, flags);
1814
David Brownell84e250f2009-03-31 12:30:04 -07001815 otg_set_peripheral(musb->xceiv, &musb->g);
Arnaud Mandyd4c433f2009-12-15 13:29:58 +02001816 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001817 musb->is_active = 1;
1818
1819 /* FIXME this ignores the softconnect flag. Drivers are
1820 * allowed hold the peripheral inactive until for example
1821 * userspace hooks up printer hardware or DSP codecs, so
1822 * hosts only see fully functional devices.
1823 */
1824
1825 if (!is_otg_enabled(musb))
1826 musb_start(musb);
1827
David Brownell84e250f2009-03-31 12:30:04 -07001828 otg_set_peripheral(musb->xceiv, &musb->g);
1829
Felipe Balbi550a7372008-07-24 12:27:36 +03001830 spin_unlock_irqrestore(&musb->lock, flags);
1831
1832 if (is_otg_enabled(musb)) {
1833 DBG(3, "OTG startup...\n");
1834
1835 /* REVISIT: funcall to other code, which also
1836 * handles power budgeting ... this way also
1837 * ensures HdrcStart is indirectly called.
1838 */
1839 retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
1840 if (retval < 0) {
1841 DBG(1, "add_hcd failed, %d\n", retval);
1842 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -07001843 otg_set_peripheral(musb->xceiv, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001844 musb->gadget_driver = NULL;
1845 musb->g.dev.driver = NULL;
1846 spin_unlock_irqrestore(&musb->lock, flags);
1847 }
1848 }
1849 }
1850
1851 return retval;
1852}
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001853EXPORT_SYMBOL(usb_gadget_probe_driver);
Felipe Balbi550a7372008-07-24 12:27:36 +03001854
1855static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1856{
1857 int i;
1858 struct musb_hw_ep *hw_ep;
1859
1860 /* don't disconnect if it's not connected */
1861 if (musb->g.speed == USB_SPEED_UNKNOWN)
1862 driver = NULL;
1863 else
1864 musb->g.speed = USB_SPEED_UNKNOWN;
1865
1866 /* deactivate the hardware */
1867 if (musb->softconnect) {
1868 musb->softconnect = 0;
1869 musb_pullup(musb, 0);
1870 }
1871 musb_stop(musb);
1872
1873 /* killing any outstanding requests will quiesce the driver;
1874 * then report disconnect
1875 */
1876 if (driver) {
1877 for (i = 0, hw_ep = musb->endpoints;
1878 i < musb->nr_endpoints;
1879 i++, hw_ep++) {
1880 musb_ep_select(musb->mregs, i);
1881 if (hw_ep->is_shared_fifo /* || !epnum */) {
1882 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1883 } else {
1884 if (hw_ep->max_packet_sz_tx)
1885 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1886 if (hw_ep->max_packet_sz_rx)
1887 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1888 }
1889 }
1890
1891 spin_unlock(&musb->lock);
1892 driver->disconnect(&musb->g);
1893 spin_lock(&musb->lock);
1894 }
1895}
1896
1897/*
1898 * Unregister the gadget driver. Used by gadget drivers when
1899 * unregistering themselves from the controller.
1900 *
1901 * @param driver the gadget driver to unregister
1902 */
1903int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1904{
1905 unsigned long flags;
1906 int retval = 0;
1907 struct musb *musb = the_gadget;
1908
1909 if (!driver || !driver->unbind || !musb)
1910 return -EINVAL;
1911
1912 /* REVISIT always use otg_set_peripheral() here too;
1913 * this needs to shut down the OTG engine.
1914 */
1915
1916 spin_lock_irqsave(&musb->lock, flags);
1917
1918#ifdef CONFIG_USB_MUSB_OTG
1919 musb_hnp_stop(musb);
1920#endif
1921
1922 if (musb->gadget_driver == driver) {
1923
1924 (void) musb_gadget_vbus_draw(&musb->g, 0);
1925
David Brownell84e250f2009-03-31 12:30:04 -07001926 musb->xceiv->state = OTG_STATE_UNDEFINED;
Felipe Balbi550a7372008-07-24 12:27:36 +03001927 stop_activity(musb, driver);
David Brownell84e250f2009-03-31 12:30:04 -07001928 otg_set_peripheral(musb->xceiv, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001929
1930 DBG(3, "unregistering driver %s\n", driver->function);
1931 spin_unlock_irqrestore(&musb->lock, flags);
1932 driver->unbind(&musb->g);
1933 spin_lock_irqsave(&musb->lock, flags);
1934
1935 musb->gadget_driver = NULL;
1936 musb->g.dev.driver = NULL;
1937
1938 musb->is_active = 0;
1939 musb_platform_try_idle(musb, 0);
1940 } else
1941 retval = -EINVAL;
1942 spin_unlock_irqrestore(&musb->lock, flags);
1943
1944 if (is_otg_enabled(musb) && retval == 0) {
1945 usb_remove_hcd(musb_to_hcd(musb));
1946 /* FIXME we need to be able to register another
1947 * gadget driver here and have everything work;
1948 * that currently misbehaves.
1949 */
1950 }
1951
1952 return retval;
1953}
1954EXPORT_SYMBOL(usb_gadget_unregister_driver);
1955
1956
1957/* ----------------------------------------------------------------------- */
1958
1959/* lifecycle operations called through plat_uds.c */
1960
1961void musb_g_resume(struct musb *musb)
1962{
1963 musb->is_suspended = 0;
David Brownell84e250f2009-03-31 12:30:04 -07001964 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001965 case OTG_STATE_B_IDLE:
1966 break;
1967 case OTG_STATE_B_WAIT_ACON:
1968 case OTG_STATE_B_PERIPHERAL:
1969 musb->is_active = 1;
1970 if (musb->gadget_driver && musb->gadget_driver->resume) {
1971 spin_unlock(&musb->lock);
1972 musb->gadget_driver->resume(&musb->g);
1973 spin_lock(&musb->lock);
1974 }
1975 break;
1976 default:
1977 WARNING("unhandled RESUME transition (%s)\n",
1978 otg_state_string(musb));
1979 }
1980}
1981
1982/* called when SOF packets stop for 3+ msec */
1983void musb_g_suspend(struct musb *musb)
1984{
1985 u8 devctl;
1986
1987 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1988 DBG(3, "devctl %02x\n", devctl);
1989
David Brownell84e250f2009-03-31 12:30:04 -07001990 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001991 case OTG_STATE_B_IDLE:
1992 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
David Brownell84e250f2009-03-31 12:30:04 -07001993 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001994 break;
1995 case OTG_STATE_B_PERIPHERAL:
1996 musb->is_suspended = 1;
1997 if (musb->gadget_driver && musb->gadget_driver->suspend) {
1998 spin_unlock(&musb->lock);
1999 musb->gadget_driver->suspend(&musb->g);
2000 spin_lock(&musb->lock);
2001 }
2002 break;
2003 default:
2004 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2005 * A_PERIPHERAL may need care too
2006 */
2007 WARNING("unhandled SUSPEND transition (%s)\n",
2008 otg_state_string(musb));
2009 }
2010}
2011
2012/* Called during SRP */
2013void musb_g_wakeup(struct musb *musb)
2014{
2015 musb_gadget_wakeup(&musb->g);
2016}
2017
2018/* called when VBUS drops below session threshold, and in other cases */
2019void musb_g_disconnect(struct musb *musb)
2020{
2021 void __iomem *mregs = musb->mregs;
2022 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2023
2024 DBG(3, "devctl %02x\n", devctl);
2025
2026 /* clear HR */
2027 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2028
2029 /* don't draw vbus until new b-default session */
2030 (void) musb_gadget_vbus_draw(&musb->g, 0);
2031
2032 musb->g.speed = USB_SPEED_UNKNOWN;
2033 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2034 spin_unlock(&musb->lock);
2035 musb->gadget_driver->disconnect(&musb->g);
2036 spin_lock(&musb->lock);
2037 }
2038
David Brownell84e250f2009-03-31 12:30:04 -07002039 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002040 default:
2041#ifdef CONFIG_USB_MUSB_OTG
2042 DBG(2, "Unhandled disconnect %s, setting a_idle\n",
2043 otg_state_string(musb));
David Brownell84e250f2009-03-31 12:30:04 -07002044 musb->xceiv->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002045 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002046 break;
2047 case OTG_STATE_A_PERIPHERAL:
David Brownell1de00da2009-04-02 10:16:11 -07002048 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07002049 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002050 break;
2051 case OTG_STATE_B_WAIT_ACON:
2052 case OTG_STATE_B_HOST:
2053#endif
2054 case OTG_STATE_B_PERIPHERAL:
2055 case OTG_STATE_B_IDLE:
David Brownell84e250f2009-03-31 12:30:04 -07002056 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002057 break;
2058 case OTG_STATE_B_SRP_INIT:
2059 break;
2060 }
2061
2062 musb->is_active = 0;
2063}
2064
2065void musb_g_reset(struct musb *musb)
2066__releases(musb->lock)
2067__acquires(musb->lock)
2068{
2069 void __iomem *mbase = musb->mregs;
2070 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2071 u8 power;
2072
2073 DBG(3, "<== %s addr=%x driver '%s'\n",
2074 (devctl & MUSB_DEVCTL_BDEVICE)
2075 ? "B-Device" : "A-Device",
2076 musb_readb(mbase, MUSB_FADDR),
2077 musb->gadget_driver
2078 ? musb->gadget_driver->driver.name
2079 : NULL
2080 );
2081
2082 /* report disconnect, if we didn't already (flushing EP state) */
2083 if (musb->g.speed != USB_SPEED_UNKNOWN)
2084 musb_g_disconnect(musb);
2085
2086 /* clear HR */
2087 else if (devctl & MUSB_DEVCTL_HR)
2088 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2089
2090
2091 /* what speed did we negotiate? */
2092 power = musb_readb(mbase, MUSB_POWER);
2093 musb->g.speed = (power & MUSB_POWER_HSMODE)
2094 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2095
2096 /* start in USB_STATE_DEFAULT */
2097 musb->is_active = 1;
2098 musb->is_suspended = 0;
2099 MUSB_DEV_MODE(musb);
2100 musb->address = 0;
2101 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2102
2103 musb->may_wakeup = 0;
2104 musb->g.b_hnp_enable = 0;
2105 musb->g.a_alt_hnp_support = 0;
2106 musb->g.a_hnp_support = 0;
2107
2108 /* Normal reset, as B-Device;
2109 * or else after HNP, as A-Device
2110 */
2111 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -07002112 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002113 musb->g.is_a_peripheral = 0;
2114 } else if (is_otg_enabled(musb)) {
David Brownell84e250f2009-03-31 12:30:04 -07002115 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002116 musb->g.is_a_peripheral = 1;
2117 } else
2118 WARN_ON(1);
2119
2120 /* start with default limits on VBUS power draw */
2121 (void) musb_gadget_vbus_draw(&musb->g,
2122 is_otg_enabled(musb) ? 8 : 100);
2123}