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