blob: de0ca90ceb9b55365e8973464bdb0d3f28f9fd15 [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;
303
304 use_dma = (request->dma != DMA_ADDR_INVALID);
305
306 /* MUSB_TXCSR_P_ISO is still set correctly */
307
308#ifdef CONFIG_USB_INVENTRA_DMA
309 {
310 size_t request_size;
311
312 /* setup DMA, then program endpoint CSR */
Cliff Caif95c4c02009-12-15 11:08:44 +0200313 request_size = min_t(size_t, request->length,
Felipe Balbi550a7372008-07-24 12:27:36 +0300314 musb_ep->dma->max_len);
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 */
340 } else
341 csr |= (MUSB_TXCSR_AUTOSET
342 | MUSB_TXCSR_DMAENAB
343 | MUSB_TXCSR_DMAMODE
344 | MUSB_TXCSR_MODE);
345
346 csr &= ~MUSB_TXCSR_P_UNDERRUN;
347 musb_writew(epio, MUSB_TXCSR, csr);
348 }
349 }
350
351#elif defined(CONFIG_USB_TI_CPPI_DMA)
352 /* program endpoint CSR first, then setup DMA */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700353 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
Sergei Shtylyov37e3ee92009-03-27 12:53:32 -0700354 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
355 MUSB_TXCSR_MODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300356 musb_writew(epio, MUSB_TXCSR,
357 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
358 | csr);
359
360 /* ensure writebuffer is empty */
361 csr = musb_readw(epio, MUSB_TXCSR);
362
363 /* NOTE host side sets DMAENAB later than this; both are
364 * OK since the transfer dma glue (between CPPI and Mentor
365 * fifos) just tells CPPI it could start. Data only moves
366 * to the USB TX fifo when both fifos are ready.
367 */
368
369 /* "mode" is irrelevant here; handle terminating ZLPs like
370 * PIO does, since the hardware RNDIS mode seems unreliable
371 * except for the last-packet-is-already-short case.
372 */
373 use_dma = use_dma && c->channel_program(
374 musb_ep->dma, musb_ep->packet_sz,
375 0,
376 request->dma,
377 request->length);
378 if (!use_dma) {
379 c->channel_release(musb_ep->dma);
380 musb_ep->dma = NULL;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700381 csr &= ~MUSB_TXCSR_DMAENAB;
382 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300383 /* invariant: prequest->buf is non-null */
384 }
385#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
386 use_dma = use_dma && c->channel_program(
387 musb_ep->dma, musb_ep->packet_sz,
388 request->zero,
389 request->dma,
390 request->length);
391#endif
392 }
393#endif
394
395 if (!use_dma) {
396 musb_write_fifo(musb_ep->hw_ep, fifo_count,
397 (u8 *) (request->buf + request->actual));
398 request->actual += fifo_count;
399 csr |= MUSB_TXCSR_TXPKTRDY;
400 csr &= ~MUSB_TXCSR_P_UNDERRUN;
401 musb_writew(epio, MUSB_TXCSR, csr);
402 }
403
404 /* host may already have the data when this message shows... */
405 DBG(3, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
406 musb_ep->end_point.name, use_dma ? "dma" : "pio",
407 request->actual, request->length,
408 musb_readw(epio, MUSB_TXCSR),
409 fifo_count,
410 musb_readw(epio, MUSB_TXMAXP));
411}
412
413/*
414 * FIFO state update (e.g. data ready).
415 * Called from IRQ, with controller locked.
416 */
417void musb_g_tx(struct musb *musb, u8 epnum)
418{
419 u16 csr;
420 struct usb_request *request;
421 u8 __iomem *mbase = musb->mregs;
422 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
423 void __iomem *epio = musb->endpoints[epnum].regs;
424 struct dma_channel *dma;
425
426 musb_ep_select(mbase, epnum);
427 request = next_request(musb_ep);
428
429 csr = musb_readw(epio, MUSB_TXCSR);
430 DBG(4, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
431
432 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300433
434 /*
435 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
436 * probably rates reporting as a host error.
437 */
438 if (csr & MUSB_TXCSR_P_SENTSTALL) {
439 csr |= MUSB_TXCSR_P_WZC_BITS;
440 csr &= ~MUSB_TXCSR_P_SENTSTALL;
441 musb_writew(epio, MUSB_TXCSR, csr);
442 return;
443 }
444
445 if (csr & MUSB_TXCSR_P_UNDERRUN) {
446 /* We NAKed, no big deal... little reason to care. */
447 csr |= MUSB_TXCSR_P_WZC_BITS;
448 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
449 musb_writew(epio, MUSB_TXCSR, csr);
450 DBG(20, "underrun on ep%d, req %p\n", epnum, request);
451 }
452
453 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
454 /*
455 * SHOULD NOT HAPPEN... has with CPPI though, after
456 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300457 */
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300458 DBG(5, "%s dma still busy?\n", musb_ep->end_point.name);
459 return;
460 }
461
462 if (request) {
463 u8 is_dma = 0;
464
465 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
466 is_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300467 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300468 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
469 MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +0300470 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300471 /* Ensure writebuffer is empty. */
472 csr = musb_readw(epio, MUSB_TXCSR);
473 request->actual += musb_ep->dma->actual_len;
474 DBG(4, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
475 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300476 }
477
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300478 if (is_dma || request->actual == request->length) {
479 /*
480 * First, maybe a terminating short packet. Some DMA
481 * engines might handle this by themselves.
Felipe Balbi550a7372008-07-24 12:27:36 +0300482 */
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300483 if ((request->zero && request->length
484 && request->length % musb_ep->packet_sz == 0)
Felipe Balbi550a7372008-07-24 12:27:36 +0300485#ifdef CONFIG_USB_INVENTRA_DMA
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300486 || (is_dma && (!dma->desired_mode ||
487 (request->actual &
488 (musb_ep->packet_sz - 1))))
Felipe Balbi550a7372008-07-24 12:27:36 +0300489#endif
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300490 ) {
491 /*
492 * On DMA completion, FIFO may not be
493 * available yet...
Felipe Balbi550a7372008-07-24 12:27:36 +0300494 */
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300495 if (csr & MUSB_TXCSR_TXPKTRDY)
496 return;
497
498 DBG(4, "sending zero pkt\n");
499 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
500 | MUSB_TXCSR_TXPKTRDY);
501 request->zero = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300502 }
503
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300504 /* ... or if not, then complete it. */
505 musb_g_giveback(musb_ep, request, 0);
506
507 /*
508 * Kickstart next transfer if appropriate;
509 * the packet that just completed might not
510 * be transmitted for hours or days.
511 * REVISIT for double buffering...
512 * FIXME revisit for stalls too...
513 */
514 musb_ep_select(mbase, epnum);
515 csr = musb_readw(epio, MUSB_TXCSR);
516 if (csr & MUSB_TXCSR_FIFONOTEMPTY)
517 return;
518
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300519 request = musb_ep->desc ? next_request(musb_ep) : NULL;
520 if (!request) {
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300521 DBG(4, "%s idle now\n",
522 musb_ep->end_point.name);
523 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300524 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300525 }
526
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300527 txstate(musb, to_musb_request(request));
528 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300529}
530
531/* ------------------------------------------------------------ */
532
533#ifdef CONFIG_USB_INVENTRA_DMA
534
535/* Peripheral rx (OUT) using Mentor DMA works as follows:
536 - Only mode 0 is used.
537
538 - Request is queued by the gadget class driver.
539 -> if queue was previously empty, rxstate()
540
541 - Host sends OUT token which causes an endpoint interrupt
542 /\ -> RxReady
543 | -> if request queued, call rxstate
544 | /\ -> setup DMA
545 | | -> DMA interrupt on completion
546 | | -> RxReady
547 | | -> stop DMA
548 | | -> ack the read
549 | | -> if data recd = max expected
550 | | by the request, or host
551 | | sent a short packet,
552 | | complete the request,
553 | | and start the next one.
554 | |_____________________________________|
555 | else just wait for the host
556 | to send the next OUT token.
557 |__________________________________________________|
558
559 * Non-Mentor DMA engines can of course work differently.
560 */
561
562#endif
563
564/*
565 * Context: controller locked, IRQs blocked, endpoint selected
566 */
567static void rxstate(struct musb *musb, struct musb_request *req)
568{
Felipe Balbi550a7372008-07-24 12:27:36 +0300569 const u8 epnum = req->epnum;
570 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300571 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300572 void __iomem *epio = musb->endpoints[epnum].regs;
Felipe Balbic2c96322009-02-21 15:29:42 -0800573 unsigned fifo_count = 0;
Ming Leibd2e74d2010-09-20 10:32:01 +0300574 u16 len;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300575 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300576 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
577
578 if (hw_ep->is_shared_fifo)
579 musb_ep = &hw_ep->ep_in;
580 else
581 musb_ep = &hw_ep->ep_out;
582
583 len = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300584
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300585 /* We shouldn't get here while DMA is active, but we do... */
586 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
587 DBG(4, "DMA pending...\n");
588 return;
589 }
590
591 if (csr & MUSB_RXCSR_P_SENDSTALL) {
592 DBG(5, "%s stalling, RXCSR %04x\n",
593 musb_ep->end_point.name, csr);
594 return;
595 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300596
597 if (is_cppi_enabled() && musb_ep->dma) {
598 struct dma_controller *c = musb->dma_controller;
599 struct dma_channel *channel = musb_ep->dma;
600
601 /* NOTE: CPPI won't actually stop advancing the DMA
602 * queue after short packet transfers, so this is almost
603 * always going to run as IRQ-per-packet DMA so that
604 * faults will be handled correctly.
605 */
606 if (c->channel_program(channel,
607 musb_ep->packet_sz,
608 !request->short_not_ok,
609 request->dma + request->actual,
610 request->length - request->actual)) {
611
612 /* make sure that if an rxpkt arrived after the irq,
613 * the cppi engine will be ready to take it as soon
614 * as DMA is enabled
615 */
616 csr &= ~(MUSB_RXCSR_AUTOCLEAR
617 | MUSB_RXCSR_DMAMODE);
618 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
619 musb_writew(epio, MUSB_RXCSR, csr);
620 return;
621 }
622 }
623
624 if (csr & MUSB_RXCSR_RXPKTRDY) {
625 len = musb_readw(epio, MUSB_RXCOUNT);
626 if (request->actual < request->length) {
627#ifdef CONFIG_USB_INVENTRA_DMA
628 if (is_dma_capable() && musb_ep->dma) {
629 struct dma_controller *c;
630 struct dma_channel *channel;
631 int use_dma = 0;
632
633 c = musb->dma_controller;
634 channel = musb_ep->dma;
635
636 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
637 * mode 0 only. So we do not get endpoint interrupts due to DMA
638 * completion. We only get interrupts from DMA controller.
639 *
640 * We could operate in DMA mode 1 if we knew the size of the tranfer
641 * in advance. For mass storage class, request->length = what the host
642 * sends, so that'd work. But for pretty much everything else,
643 * request->length is routinely more than what the host sends. For
644 * most these gadgets, end of is signified either by a short packet,
645 * or filling the last byte of the buffer. (Sending extra data in
646 * that last pckate should trigger an overflow fault.) But in mode 1,
647 * we don't get DMA completion interrrupt for short packets.
648 *
649 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
650 * to get endpoint interrupt on every DMA req, but that didn't seem
651 * to work reliably.
652 *
653 * REVISIT an updated g_file_storage can set req->short_not_ok, which
654 * then becomes usable as a runtime "use mode 1" hint...
655 */
656
657 csr |= MUSB_RXCSR_DMAENAB;
658#ifdef USE_MODE1
659 csr |= MUSB_RXCSR_AUTOCLEAR;
660 /* csr |= MUSB_RXCSR_DMAMODE; */
661
662 /* this special sequence (enabling and then
663 * disabling MUSB_RXCSR_DMAMODE) is required
664 * to get DMAReq to activate
665 */
666 musb_writew(epio, MUSB_RXCSR,
667 csr | MUSB_RXCSR_DMAMODE);
668#endif
669 musb_writew(epio, MUSB_RXCSR, csr);
670
671 if (request->actual < request->length) {
672 int transfer_size = 0;
673#ifdef USE_MODE1
674 transfer_size = min(request->length,
675 channel->max_len);
676#else
677 transfer_size = len;
678#endif
679 if (transfer_size <= musb_ep->packet_sz)
680 musb_ep->dma->desired_mode = 0;
681 else
682 musb_ep->dma->desired_mode = 1;
683
684 use_dma = c->channel_program(
685 channel,
686 musb_ep->packet_sz,
687 channel->desired_mode,
688 request->dma
689 + request->actual,
690 transfer_size);
691 }
692
693 if (use_dma)
694 return;
695 }
696#endif /* Mentor's DMA */
697
698 fifo_count = request->length - request->actual;
699 DBG(3, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
700 musb_ep->end_point.name,
701 len, fifo_count,
702 musb_ep->packet_sz);
703
Felipe Balbic2c96322009-02-21 15:29:42 -0800704 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300705
706#ifdef CONFIG_USB_TUSB_OMAP_DMA
707 if (tusb_dma_omap() && musb_ep->dma) {
708 struct dma_controller *c = musb->dma_controller;
709 struct dma_channel *channel = musb_ep->dma;
710 u32 dma_addr = request->dma + request->actual;
711 int ret;
712
713 ret = c->channel_program(channel,
714 musb_ep->packet_sz,
715 channel->desired_mode,
716 dma_addr,
717 fifo_count);
718 if (ret)
719 return;
720 }
721#endif
722
723 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
724 (request->buf + request->actual));
725 request->actual += fifo_count;
726
727 /* REVISIT if we left anything in the fifo, flush
728 * it and report -EOVERFLOW
729 */
730
731 /* ack the read! */
732 csr |= MUSB_RXCSR_P_WZC_BITS;
733 csr &= ~MUSB_RXCSR_RXPKTRDY;
734 musb_writew(epio, MUSB_RXCSR, csr);
735 }
736 }
737
738 /* reach the end or short packet detected */
739 if (request->actual == request->length || len < musb_ep->packet_sz)
740 musb_g_giveback(musb_ep, request, 0);
741}
742
743/*
744 * Data ready for a request; called from IRQ
745 */
746void musb_g_rx(struct musb *musb, u8 epnum)
747{
748 u16 csr;
749 struct usb_request *request;
750 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300751 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300752 void __iomem *epio = musb->endpoints[epnum].regs;
753 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300754 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
755
756 if (hw_ep->is_shared_fifo)
757 musb_ep = &hw_ep->ep_in;
758 else
759 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300760
761 musb_ep_select(mbase, epnum);
762
763 request = next_request(musb_ep);
Maulik Mankad0abdc362009-12-22 16:18:19 +0530764 if (!request)
765 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300766
767 csr = musb_readw(epio, MUSB_RXCSR);
768 dma = is_dma_capable() ? musb_ep->dma : NULL;
769
770 DBG(4, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
771 csr, dma ? " (dma)" : "", request);
772
773 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300774 csr |= MUSB_RXCSR_P_WZC_BITS;
775 csr &= ~MUSB_RXCSR_P_SENTSTALL;
776 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300777 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300778 }
779
780 if (csr & MUSB_RXCSR_P_OVERRUN) {
781 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
782 csr &= ~MUSB_RXCSR_P_OVERRUN;
783 musb_writew(epio, MUSB_RXCSR, csr);
784
785 DBG(3, "%s iso overrun on %p\n", musb_ep->name, request);
786 if (request && request->status == -EINPROGRESS)
787 request->status = -EOVERFLOW;
788 }
789 if (csr & MUSB_RXCSR_INCOMPRX) {
790 /* REVISIT not necessarily an error */
791 DBG(4, "%s, incomprx\n", musb_ep->end_point.name);
792 }
793
794 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
795 /* "should not happen"; likely RXPKTRDY pending for DMA */
796 DBG((csr & MUSB_RXCSR_DMAENAB) ? 4 : 1,
797 "%s busy, csr %04x\n",
798 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300799 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300800 }
801
802 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
803 csr &= ~(MUSB_RXCSR_AUTOCLEAR
804 | MUSB_RXCSR_DMAENAB
805 | MUSB_RXCSR_DMAMODE);
806 musb_writew(epio, MUSB_RXCSR,
807 MUSB_RXCSR_P_WZC_BITS | csr);
808
809 request->actual += musb_ep->dma->actual_len;
810
811 DBG(4, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
812 epnum, csr,
813 musb_readw(epio, MUSB_RXCSR),
814 musb_ep->dma->actual_len, request);
815
816#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
817 /* Autoclear doesn't clear RxPktRdy for short packets */
818 if ((dma->desired_mode == 0)
819 || (dma->actual_len
820 & (musb_ep->packet_sz - 1))) {
821 /* ack the read! */
822 csr &= ~MUSB_RXCSR_RXPKTRDY;
823 musb_writew(epio, MUSB_RXCSR, csr);
824 }
825
826 /* incomplete, and not short? wait for next IN packet */
827 if ((request->actual < request->length)
828 && (musb_ep->dma->actual_len
829 == musb_ep->packet_sz))
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300830 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300831#endif
832 musb_g_giveback(musb_ep, request, 0);
833
834 request = next_request(musb_ep);
835 if (!request)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300836 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300837 }
838
Felipe Balbi550a7372008-07-24 12:27:36 +0300839 /* analyze request if the ep is hot */
840 if (request)
841 rxstate(musb, to_musb_request(request));
842 else
843 DBG(3, "packet waiting for %s%s request\n",
844 musb_ep->desc ? "" : "inactive ",
845 musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300846 return;
847}
848
849/* ------------------------------------------------------------ */
850
851static int musb_gadget_enable(struct usb_ep *ep,
852 const struct usb_endpoint_descriptor *desc)
853{
854 unsigned long flags;
855 struct musb_ep *musb_ep;
856 struct musb_hw_ep *hw_ep;
857 void __iomem *regs;
858 struct musb *musb;
859 void __iomem *mbase;
860 u8 epnum;
861 u16 csr;
862 unsigned tmp;
863 int status = -EINVAL;
864
865 if (!ep || !desc)
866 return -EINVAL;
867
868 musb_ep = to_musb_ep(ep);
869 hw_ep = musb_ep->hw_ep;
870 regs = hw_ep->regs;
871 musb = musb_ep->musb;
872 mbase = musb->mregs;
873 epnum = musb_ep->current_epnum;
874
875 spin_lock_irqsave(&musb->lock, flags);
876
877 if (musb_ep->desc) {
878 status = -EBUSY;
879 goto fail;
880 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800881 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300882
883 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800884 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300885 goto fail;
886
887 /* REVISIT this rules out high bandwidth periodic transfers */
888 tmp = le16_to_cpu(desc->wMaxPacketSize);
889 if (tmp & ~0x07ff)
890 goto fail;
891 musb_ep->packet_sz = tmp;
892
893 /* enable the interrupts for the endpoint, set the endpoint
894 * packet size (or fail), set the mode, clear the fifo
895 */
896 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -0800897 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300898 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
899
900 if (hw_ep->is_shared_fifo)
901 musb_ep->is_in = 1;
902 if (!musb_ep->is_in)
903 goto fail;
904 if (tmp > hw_ep->max_packet_sz_tx)
905 goto fail;
906
907 int_txe |= (1 << epnum);
908 musb_writew(mbase, MUSB_INTRTXE, int_txe);
909
910 /* REVISIT if can_bulk_split(), use by updating "tmp";
911 * likewise high bandwidth periodic tx
912 */
Cliff Cai9f445cb2010-01-28 20:44:18 -0500913 /* Set TXMAXP with the FIFO size of the endpoint
914 * to disable double buffering mode. Currently, It seems that double
915 * buffering has problem if musb RTL revision number < 2.0.
916 */
917 if (musb->hwvers < MUSB_HWVERS_2000)
918 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
919 else
920 musb_writew(regs, MUSB_TXMAXP, tmp);
Felipe Balbi550a7372008-07-24 12:27:36 +0300921
922 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
923 if (musb_readw(regs, MUSB_TXCSR)
924 & MUSB_TXCSR_FIFONOTEMPTY)
925 csr |= MUSB_TXCSR_FLUSHFIFO;
926 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
927 csr |= MUSB_TXCSR_P_ISO;
928
929 /* set twice in case of double buffering */
930 musb_writew(regs, MUSB_TXCSR, csr);
931 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
932 musb_writew(regs, MUSB_TXCSR, csr);
933
934 } else {
935 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
936
937 if (hw_ep->is_shared_fifo)
938 musb_ep->is_in = 0;
939 if (musb_ep->is_in)
940 goto fail;
941 if (tmp > hw_ep->max_packet_sz_rx)
942 goto fail;
943
944 int_rxe |= (1 << epnum);
945 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
946
947 /* REVISIT if can_bulk_combine() use by updating "tmp"
948 * likewise high bandwidth periodic rx
949 */
Cliff Cai9f445cb2010-01-28 20:44:18 -0500950 /* Set RXMAXP with the FIFO size of the endpoint
951 * to disable double buffering mode.
952 */
953 if (musb->hwvers < MUSB_HWVERS_2000)
954 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_rx);
955 else
956 musb_writew(regs, MUSB_RXMAXP, tmp);
Felipe Balbi550a7372008-07-24 12:27:36 +0300957
958 /* force shared fifo to OUT-only mode */
959 if (hw_ep->is_shared_fifo) {
960 csr = musb_readw(regs, MUSB_TXCSR);
961 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
962 musb_writew(regs, MUSB_TXCSR, csr);
963 }
964
965 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
966 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
967 csr |= MUSB_RXCSR_P_ISO;
968 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
969 csr |= MUSB_RXCSR_DISNYET;
970
971 /* set twice in case of double buffering */
972 musb_writew(regs, MUSB_RXCSR, csr);
973 musb_writew(regs, MUSB_RXCSR, csr);
974 }
975
976 /* NOTE: all the I/O code _should_ work fine without DMA, in case
977 * for some reason you run out of channels here.
978 */
979 if (is_dma_capable() && musb->dma_controller) {
980 struct dma_controller *c = musb->dma_controller;
981
982 musb_ep->dma = c->channel_alloc(c, hw_ep,
983 (desc->bEndpointAddress & USB_DIR_IN));
984 } else
985 musb_ep->dma = NULL;
986
987 musb_ep->desc = desc;
988 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +0300989 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300990 status = 0;
991
992 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
993 musb_driver_name, musb_ep->end_point.name,
994 ({ char *s; switch (musb_ep->type) {
995 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
996 case USB_ENDPOINT_XFER_INT: s = "int"; break;
997 default: s = "iso"; break;
998 }; s; }),
999 musb_ep->is_in ? "IN" : "OUT",
1000 musb_ep->dma ? "dma, " : "",
1001 musb_ep->packet_sz);
1002
1003 schedule_work(&musb->irq_work);
1004
1005fail:
1006 spin_unlock_irqrestore(&musb->lock, flags);
1007 return status;
1008}
1009
1010/*
1011 * Disable an endpoint flushing all requests queued.
1012 */
1013static int musb_gadget_disable(struct usb_ep *ep)
1014{
1015 unsigned long flags;
1016 struct musb *musb;
1017 u8 epnum;
1018 struct musb_ep *musb_ep;
1019 void __iomem *epio;
1020 int status = 0;
1021
1022 musb_ep = to_musb_ep(ep);
1023 musb = musb_ep->musb;
1024 epnum = musb_ep->current_epnum;
1025 epio = musb->endpoints[epnum].regs;
1026
1027 spin_lock_irqsave(&musb->lock, flags);
1028 musb_ep_select(musb->mregs, epnum);
1029
1030 /* zero the endpoint sizes */
1031 if (musb_ep->is_in) {
1032 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1033 int_txe &= ~(1 << epnum);
1034 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1035 musb_writew(epio, MUSB_TXMAXP, 0);
1036 } else {
1037 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1038 int_rxe &= ~(1 << epnum);
1039 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1040 musb_writew(epio, MUSB_RXMAXP, 0);
1041 }
1042
1043 musb_ep->desc = NULL;
1044
1045 /* abort all pending DMA and requests */
1046 nuke(musb_ep, -ESHUTDOWN);
1047
1048 schedule_work(&musb->irq_work);
1049
1050 spin_unlock_irqrestore(&(musb->lock), flags);
1051
1052 DBG(2, "%s\n", musb_ep->end_point.name);
1053
1054 return status;
1055}
1056
1057/*
1058 * Allocate a request for an endpoint.
1059 * Reused by ep0 code.
1060 */
1061struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1062{
1063 struct musb_ep *musb_ep = to_musb_ep(ep);
1064 struct musb_request *request = NULL;
1065
1066 request = kzalloc(sizeof *request, gfp_flags);
1067 if (request) {
1068 INIT_LIST_HEAD(&request->request.list);
1069 request->request.dma = DMA_ADDR_INVALID;
1070 request->epnum = musb_ep->current_epnum;
1071 request->ep = musb_ep;
1072 }
1073
1074 return &request->request;
1075}
1076
1077/*
1078 * Free a request
1079 * Reused by ep0 code.
1080 */
1081void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1082{
1083 kfree(to_musb_request(req));
1084}
1085
1086static LIST_HEAD(buffers);
1087
1088struct free_record {
1089 struct list_head list;
1090 struct device *dev;
1091 unsigned bytes;
1092 dma_addr_t dma;
1093};
1094
1095/*
1096 * Context: controller locked, IRQs blocked.
1097 */
1098static void musb_ep_restart(struct musb *musb, struct musb_request *req)
1099{
1100 DBG(3, "<== %s request %p len %u on hw_ep%d\n",
1101 req->tx ? "TX/IN" : "RX/OUT",
1102 &req->request, req->request.length, req->epnum);
1103
1104 musb_ep_select(musb->mregs, req->epnum);
1105 if (req->tx)
1106 txstate(musb, req);
1107 else
1108 rxstate(musb, req);
1109}
1110
1111static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1112 gfp_t gfp_flags)
1113{
1114 struct musb_ep *musb_ep;
1115 struct musb_request *request;
1116 struct musb *musb;
1117 int status = 0;
1118 unsigned long lockflags;
1119
1120 if (!ep || !req)
1121 return -EINVAL;
1122 if (!req->buf)
1123 return -ENODATA;
1124
1125 musb_ep = to_musb_ep(ep);
1126 musb = musb_ep->musb;
1127
1128 request = to_musb_request(req);
1129 request->musb = musb;
1130
1131 if (request->ep != musb_ep)
1132 return -EINVAL;
1133
1134 DBG(4, "<== to %s request=%p\n", ep->name, req);
1135
1136 /* request is mine now... */
1137 request->request.actual = 0;
1138 request->request.status = -EINPROGRESS;
1139 request->epnum = musb_ep->current_epnum;
1140 request->tx = musb_ep->is_in;
1141
1142 if (is_dma_capable() && musb_ep->dma) {
1143 if (request->request.dma == DMA_ADDR_INVALID) {
1144 request->request.dma = dma_map_single(
1145 musb->controller,
1146 request->request.buf,
1147 request->request.length,
1148 request->tx
1149 ? DMA_TO_DEVICE
1150 : DMA_FROM_DEVICE);
1151 request->mapped = 1;
1152 } else {
1153 dma_sync_single_for_device(musb->controller,
1154 request->request.dma,
1155 request->request.length,
1156 request->tx
1157 ? DMA_TO_DEVICE
1158 : DMA_FROM_DEVICE);
1159 request->mapped = 0;
1160 }
1161 } else if (!req->buf) {
1162 return -ENODATA;
1163 } else
1164 request->mapped = 0;
1165
1166 spin_lock_irqsave(&musb->lock, lockflags);
1167
1168 /* don't queue if the ep is down */
1169 if (!musb_ep->desc) {
1170 DBG(4, "req %p queued to %s while ep %s\n",
1171 req, ep->name, "disabled");
1172 status = -ESHUTDOWN;
1173 goto cleanup;
1174 }
1175
1176 /* add request to the list */
1177 list_add_tail(&(request->request.list), &(musb_ep->req_list));
1178
1179 /* it this is the head of the queue, start i/o ... */
1180 if (!musb_ep->busy && &request->request.list == musb_ep->req_list.next)
1181 musb_ep_restart(musb, request);
1182
1183cleanup:
1184 spin_unlock_irqrestore(&musb->lock, lockflags);
1185 return status;
1186}
1187
1188static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1189{
1190 struct musb_ep *musb_ep = to_musb_ep(ep);
1191 struct usb_request *r;
1192 unsigned long flags;
1193 int status = 0;
1194 struct musb *musb = musb_ep->musb;
1195
1196 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1197 return -EINVAL;
1198
1199 spin_lock_irqsave(&musb->lock, flags);
1200
1201 list_for_each_entry(r, &musb_ep->req_list, list) {
1202 if (r == request)
1203 break;
1204 }
1205 if (r != request) {
1206 DBG(3, "request %p not queued to %s\n", request, ep->name);
1207 status = -EINVAL;
1208 goto done;
1209 }
1210
1211 /* if the hardware doesn't have the request, easy ... */
1212 if (musb_ep->req_list.next != &request->list || musb_ep->busy)
1213 musb_g_giveback(musb_ep, request, -ECONNRESET);
1214
1215 /* ... else abort the dma transfer ... */
1216 else if (is_dma_capable() && musb_ep->dma) {
1217 struct dma_controller *c = musb->dma_controller;
1218
1219 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1220 if (c->channel_abort)
1221 status = c->channel_abort(musb_ep->dma);
1222 else
1223 status = -EBUSY;
1224 if (status == 0)
1225 musb_g_giveback(musb_ep, request, -ECONNRESET);
1226 } else {
1227 /* NOTE: by sticking to easily tested hardware/driver states,
1228 * we leave counting of in-flight packets imprecise.
1229 */
1230 musb_g_giveback(musb_ep, request, -ECONNRESET);
1231 }
1232
1233done:
1234 spin_unlock_irqrestore(&musb->lock, flags);
1235 return status;
1236}
1237
1238/*
1239 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1240 * data but will queue requests.
1241 *
1242 * exported to ep0 code
1243 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001244static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001245{
1246 struct musb_ep *musb_ep = to_musb_ep(ep);
1247 u8 epnum = musb_ep->current_epnum;
1248 struct musb *musb = musb_ep->musb;
1249 void __iomem *epio = musb->endpoints[epnum].regs;
1250 void __iomem *mbase;
1251 unsigned long flags;
1252 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001253 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001254 int status = 0;
1255
1256 if (!ep)
1257 return -EINVAL;
1258 mbase = musb->mregs;
1259
1260 spin_lock_irqsave(&musb->lock, flags);
1261
1262 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1263 status = -EINVAL;
1264 goto done;
1265 }
1266
1267 musb_ep_select(mbase, epnum);
1268
Felipe Balbi550a7372008-07-24 12:27:36 +03001269 request = to_musb_request(next_request(musb_ep));
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001270 if (value) {
1271 if (request) {
1272 DBG(3, "request in progress, cannot halt %s\n",
1273 ep->name);
1274 status = -EAGAIN;
1275 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001276 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001277 /* Cannot portably stall with non-empty FIFO */
1278 if (musb_ep->is_in) {
1279 csr = musb_readw(epio, MUSB_TXCSR);
1280 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1281 DBG(3, "FIFO busy, cannot halt %s\n", ep->name);
1282 status = -EAGAIN;
1283 goto done;
1284 }
1285 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001286 } else
1287 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001288
1289 /* set/clear the stall and toggle bits */
1290 DBG(2, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1291 if (musb_ep->is_in) {
1292 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001293 csr |= MUSB_TXCSR_P_WZC_BITS
1294 | MUSB_TXCSR_CLRDATATOG;
1295 if (value)
1296 csr |= MUSB_TXCSR_P_SENDSTALL;
1297 else
1298 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1299 | MUSB_TXCSR_P_SENTSTALL);
1300 csr &= ~MUSB_TXCSR_TXPKTRDY;
1301 musb_writew(epio, MUSB_TXCSR, csr);
1302 } else {
1303 csr = musb_readw(epio, MUSB_RXCSR);
1304 csr |= MUSB_RXCSR_P_WZC_BITS
1305 | MUSB_RXCSR_FLUSHFIFO
1306 | MUSB_RXCSR_CLRDATATOG;
1307 if (value)
1308 csr |= MUSB_RXCSR_P_SENDSTALL;
1309 else
1310 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1311 | MUSB_RXCSR_P_SENTSTALL);
1312 musb_writew(epio, MUSB_RXCSR, csr);
1313 }
1314
Felipe Balbi550a7372008-07-24 12:27:36 +03001315 /* maybe start the first request in the queue */
1316 if (!musb_ep->busy && !value && request) {
1317 DBG(3, "restarting the request\n");
1318 musb_ep_restart(musb, request);
1319 }
1320
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001321done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001322 spin_unlock_irqrestore(&musb->lock, flags);
1323 return status;
1324}
1325
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001326/*
1327 * Sets the halt feature with the clear requests ignored
1328 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001329static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001330{
1331 struct musb_ep *musb_ep = to_musb_ep(ep);
1332
1333 if (!ep)
1334 return -EINVAL;
1335
1336 musb_ep->wedged = 1;
1337
1338 return usb_ep_set_halt(ep);
1339}
1340
Felipe Balbi550a7372008-07-24 12:27:36 +03001341static int musb_gadget_fifo_status(struct usb_ep *ep)
1342{
1343 struct musb_ep *musb_ep = to_musb_ep(ep);
1344 void __iomem *epio = musb_ep->hw_ep->regs;
1345 int retval = -EINVAL;
1346
1347 if (musb_ep->desc && !musb_ep->is_in) {
1348 struct musb *musb = musb_ep->musb;
1349 int epnum = musb_ep->current_epnum;
1350 void __iomem *mbase = musb->mregs;
1351 unsigned long flags;
1352
1353 spin_lock_irqsave(&musb->lock, flags);
1354
1355 musb_ep_select(mbase, epnum);
1356 /* FIXME return zero unless RXPKTRDY is set */
1357 retval = musb_readw(epio, MUSB_RXCOUNT);
1358
1359 spin_unlock_irqrestore(&musb->lock, flags);
1360 }
1361 return retval;
1362}
1363
1364static void musb_gadget_fifo_flush(struct usb_ep *ep)
1365{
1366 struct musb_ep *musb_ep = to_musb_ep(ep);
1367 struct musb *musb = musb_ep->musb;
1368 u8 epnum = musb_ep->current_epnum;
1369 void __iomem *epio = musb->endpoints[epnum].regs;
1370 void __iomem *mbase;
1371 unsigned long flags;
1372 u16 csr, int_txe;
1373
1374 mbase = musb->mregs;
1375
1376 spin_lock_irqsave(&musb->lock, flags);
1377 musb_ep_select(mbase, (u8) epnum);
1378
1379 /* disable interrupts */
1380 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1381 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1382
1383 if (musb_ep->is_in) {
1384 csr = musb_readw(epio, MUSB_TXCSR);
1385 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1386 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1387 musb_writew(epio, MUSB_TXCSR, csr);
1388 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1389 musb_writew(epio, MUSB_TXCSR, csr);
1390 }
1391 } else {
1392 csr = musb_readw(epio, MUSB_RXCSR);
1393 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1394 musb_writew(epio, MUSB_RXCSR, csr);
1395 musb_writew(epio, MUSB_RXCSR, csr);
1396 }
1397
1398 /* re-enable interrupt */
1399 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1400 spin_unlock_irqrestore(&musb->lock, flags);
1401}
1402
1403static const struct usb_ep_ops musb_ep_ops = {
1404 .enable = musb_gadget_enable,
1405 .disable = musb_gadget_disable,
1406 .alloc_request = musb_alloc_request,
1407 .free_request = musb_free_request,
1408 .queue = musb_gadget_queue,
1409 .dequeue = musb_gadget_dequeue,
1410 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001411 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001412 .fifo_status = musb_gadget_fifo_status,
1413 .fifo_flush = musb_gadget_fifo_flush
1414};
1415
1416/* ----------------------------------------------------------------------- */
1417
1418static int musb_gadget_get_frame(struct usb_gadget *gadget)
1419{
1420 struct musb *musb = gadget_to_musb(gadget);
1421
1422 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1423}
1424
1425static int musb_gadget_wakeup(struct usb_gadget *gadget)
1426{
1427 struct musb *musb = gadget_to_musb(gadget);
1428 void __iomem *mregs = musb->mregs;
1429 unsigned long flags;
1430 int status = -EINVAL;
1431 u8 power, devctl;
1432 int retries;
1433
1434 spin_lock_irqsave(&musb->lock, flags);
1435
David Brownell84e250f2009-03-31 12:30:04 -07001436 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001437 case OTG_STATE_B_PERIPHERAL:
1438 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1439 * that's part of the standard usb 1.1 state machine, and
1440 * doesn't affect OTG transitions.
1441 */
1442 if (musb->may_wakeup && musb->is_suspended)
1443 break;
1444 goto done;
1445 case OTG_STATE_B_IDLE:
1446 /* Start SRP ... OTG not required. */
1447 devctl = musb_readb(mregs, MUSB_DEVCTL);
1448 DBG(2, "Sending SRP: devctl: %02x\n", devctl);
1449 devctl |= MUSB_DEVCTL_SESSION;
1450 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1451 devctl = musb_readb(mregs, MUSB_DEVCTL);
1452 retries = 100;
1453 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1454 devctl = musb_readb(mregs, MUSB_DEVCTL);
1455 if (retries-- < 1)
1456 break;
1457 }
1458 retries = 10000;
1459 while (devctl & MUSB_DEVCTL_SESSION) {
1460 devctl = musb_readb(mregs, MUSB_DEVCTL);
1461 if (retries-- < 1)
1462 break;
1463 }
1464
1465 /* Block idling for at least 1s */
1466 musb_platform_try_idle(musb,
1467 jiffies + msecs_to_jiffies(1 * HZ));
1468
1469 status = 0;
1470 goto done;
1471 default:
1472 DBG(2, "Unhandled wake: %s\n", otg_state_string(musb));
1473 goto done;
1474 }
1475
1476 status = 0;
1477
1478 power = musb_readb(mregs, MUSB_POWER);
1479 power |= MUSB_POWER_RESUME;
1480 musb_writeb(mregs, MUSB_POWER, power);
1481 DBG(2, "issue wakeup\n");
1482
1483 /* FIXME do this next chunk in a timer callback, no udelay */
1484 mdelay(2);
1485
1486 power = musb_readb(mregs, MUSB_POWER);
1487 power &= ~MUSB_POWER_RESUME;
1488 musb_writeb(mregs, MUSB_POWER, power);
1489done:
1490 spin_unlock_irqrestore(&musb->lock, flags);
1491 return status;
1492}
1493
1494static int
1495musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1496{
1497 struct musb *musb = gadget_to_musb(gadget);
1498
1499 musb->is_self_powered = !!is_selfpowered;
1500 return 0;
1501}
1502
1503static void musb_pullup(struct musb *musb, int is_on)
1504{
1505 u8 power;
1506
1507 power = musb_readb(musb->mregs, MUSB_POWER);
1508 if (is_on)
1509 power |= MUSB_POWER_SOFTCONN;
1510 else
1511 power &= ~MUSB_POWER_SOFTCONN;
1512
1513 /* FIXME if on, HdrcStart; if off, HdrcStop */
1514
1515 DBG(3, "gadget %s D+ pullup %s\n",
1516 musb->gadget_driver->function, is_on ? "on" : "off");
1517 musb_writeb(musb->mregs, MUSB_POWER, power);
1518}
1519
1520#if 0
1521static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1522{
1523 DBG(2, "<= %s =>\n", __func__);
1524
1525 /*
1526 * FIXME iff driver's softconnect flag is set (as it is during probe,
1527 * though that can clear it), just musb_pullup().
1528 */
1529
1530 return -EINVAL;
1531}
1532#endif
1533
1534static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1535{
1536 struct musb *musb = gadget_to_musb(gadget);
1537
David Brownell84e250f2009-03-31 12:30:04 -07001538 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001539 return -EOPNOTSUPP;
David Brownell84e250f2009-03-31 12:30:04 -07001540 return otg_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001541}
1542
1543static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1544{
1545 struct musb *musb = gadget_to_musb(gadget);
1546 unsigned long flags;
1547
1548 is_on = !!is_on;
1549
1550 /* NOTE: this assumes we are sensing vbus; we'd rather
1551 * not pullup unless the B-session is active.
1552 */
1553 spin_lock_irqsave(&musb->lock, flags);
1554 if (is_on != musb->softconnect) {
1555 musb->softconnect = is_on;
1556 musb_pullup(musb, is_on);
1557 }
1558 spin_unlock_irqrestore(&musb->lock, flags);
1559 return 0;
1560}
1561
1562static const struct usb_gadget_ops musb_gadget_operations = {
1563 .get_frame = musb_gadget_get_frame,
1564 .wakeup = musb_gadget_wakeup,
1565 .set_selfpowered = musb_gadget_set_self_powered,
1566 /* .vbus_session = musb_gadget_vbus_session, */
1567 .vbus_draw = musb_gadget_vbus_draw,
1568 .pullup = musb_gadget_pullup,
1569};
1570
1571/* ----------------------------------------------------------------------- */
1572
1573/* Registration */
1574
1575/* Only this registration code "knows" the rule (from USB standards)
1576 * about there being only one external upstream port. It assumes
1577 * all peripheral ports are external...
1578 */
1579static struct musb *the_gadget;
1580
1581static void musb_gadget_release(struct device *dev)
1582{
1583 /* kref_put(WHAT) */
1584 dev_dbg(dev, "%s\n", __func__);
1585}
1586
1587
1588static void __init
1589init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1590{
1591 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1592
1593 memset(ep, 0, sizeof *ep);
1594
1595 ep->current_epnum = epnum;
1596 ep->musb = musb;
1597 ep->hw_ep = hw_ep;
1598 ep->is_in = is_in;
1599
1600 INIT_LIST_HEAD(&ep->req_list);
1601
1602 sprintf(ep->name, "ep%d%s", epnum,
1603 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1604 is_in ? "in" : "out"));
1605 ep->end_point.name = ep->name;
1606 INIT_LIST_HEAD(&ep->end_point.ep_list);
1607 if (!epnum) {
1608 ep->end_point.maxpacket = 64;
1609 ep->end_point.ops = &musb_g_ep0_ops;
1610 musb->g.ep0 = &ep->end_point;
1611 } else {
1612 if (is_in)
1613 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1614 else
1615 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1616 ep->end_point.ops = &musb_ep_ops;
1617 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1618 }
1619}
1620
1621/*
1622 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1623 * to the rest of the driver state.
1624 */
1625static inline void __init musb_g_init_endpoints(struct musb *musb)
1626{
1627 u8 epnum;
1628 struct musb_hw_ep *hw_ep;
1629 unsigned count = 0;
1630
1631 /* intialize endpoint list just once */
1632 INIT_LIST_HEAD(&(musb->g.ep_list));
1633
1634 for (epnum = 0, hw_ep = musb->endpoints;
1635 epnum < musb->nr_endpoints;
1636 epnum++, hw_ep++) {
1637 if (hw_ep->is_shared_fifo /* || !epnum */) {
1638 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1639 count++;
1640 } else {
1641 if (hw_ep->max_packet_sz_tx) {
1642 init_peripheral_ep(musb, &hw_ep->ep_in,
1643 epnum, 1);
1644 count++;
1645 }
1646 if (hw_ep->max_packet_sz_rx) {
1647 init_peripheral_ep(musb, &hw_ep->ep_out,
1648 epnum, 0);
1649 count++;
1650 }
1651 }
1652 }
1653}
1654
1655/* called once during driver setup to initialize and link into
1656 * the driver model; memory is zeroed.
1657 */
1658int __init musb_gadget_setup(struct musb *musb)
1659{
1660 int status;
1661
1662 /* REVISIT minor race: if (erroneously) setting up two
1663 * musb peripherals at the same time, only the bus lock
1664 * is probably held.
1665 */
1666 if (the_gadget)
1667 return -EBUSY;
1668 the_gadget = musb;
1669
1670 musb->g.ops = &musb_gadget_operations;
1671 musb->g.is_dualspeed = 1;
1672 musb->g.speed = USB_SPEED_UNKNOWN;
1673
1674 /* this "gadget" abstracts/virtualizes the controller */
Kay Sievers427c4f32008-11-07 01:52:53 +01001675 dev_set_name(&musb->g.dev, "gadget");
Felipe Balbi550a7372008-07-24 12:27:36 +03001676 musb->g.dev.parent = musb->controller;
1677 musb->g.dev.dma_mask = musb->controller->dma_mask;
1678 musb->g.dev.release = musb_gadget_release;
1679 musb->g.name = musb_driver_name;
1680
1681 if (is_otg_enabled(musb))
1682 musb->g.is_otg = 1;
1683
1684 musb_g_init_endpoints(musb);
1685
1686 musb->is_active = 0;
1687 musb_platform_try_idle(musb, 0);
1688
1689 status = device_register(&musb->g.dev);
1690 if (status != 0)
1691 the_gadget = NULL;
1692 return status;
1693}
1694
1695void musb_gadget_cleanup(struct musb *musb)
1696{
1697 if (musb != the_gadget)
1698 return;
1699
1700 device_unregister(&musb->g.dev);
1701 the_gadget = NULL;
1702}
1703
1704/*
1705 * Register the gadget driver. Used by gadget drivers when
1706 * registering themselves with the controller.
1707 *
1708 * -EINVAL something went wrong (not driver)
1709 * -EBUSY another gadget is already using the controller
1710 * -ENOMEM no memeory to perform the operation
1711 *
1712 * @param driver the gadget driver
1713 * @return <0 if error, 0 if everything is fine
1714 */
1715int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1716{
1717 int retval;
1718 unsigned long flags;
1719 struct musb *musb = the_gadget;
1720
1721 if (!driver
1722 || driver->speed != USB_SPEED_HIGH
1723 || !driver->bind
1724 || !driver->setup)
1725 return -EINVAL;
1726
1727 /* driver must be initialized to support peripheral mode */
Roel Kluin08e6c972010-02-02 14:47:17 -08001728 if (!musb) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001729 DBG(1, "%s, no dev??\n", __func__);
1730 return -ENODEV;
1731 }
1732
1733 DBG(3, "registering driver %s\n", driver->function);
1734 spin_lock_irqsave(&musb->lock, flags);
1735
1736 if (musb->gadget_driver) {
1737 DBG(1, "%s is already bound to %s\n",
1738 musb_driver_name,
1739 musb->gadget_driver->driver.name);
1740 retval = -EBUSY;
1741 } else {
1742 musb->gadget_driver = driver;
1743 musb->g.dev.driver = &driver->driver;
1744 driver->driver.bus = NULL;
1745 musb->softconnect = 1;
1746 retval = 0;
1747 }
1748
1749 spin_unlock_irqrestore(&musb->lock, flags);
1750
Felipe Balbi550a7372008-07-24 12:27:36 +03001751 if (retval == 0) {
Felipe Balbif362a472008-08-04 13:53:52 +03001752 retval = driver->bind(&musb->g);
1753 if (retval != 0) {
1754 DBG(3, "bind to driver %s failed --> %d\n",
1755 driver->driver.name, retval);
1756 musb->gadget_driver = NULL;
1757 musb->g.dev.driver = NULL;
1758 }
1759
Felipe Balbi550a7372008-07-24 12:27:36 +03001760 spin_lock_irqsave(&musb->lock, flags);
1761
David Brownell84e250f2009-03-31 12:30:04 -07001762 otg_set_peripheral(musb->xceiv, &musb->g);
Arnaud Mandyd4c433f2009-12-15 13:29:58 +02001763 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001764 musb->is_active = 1;
1765
1766 /* FIXME this ignores the softconnect flag. Drivers are
1767 * allowed hold the peripheral inactive until for example
1768 * userspace hooks up printer hardware or DSP codecs, so
1769 * hosts only see fully functional devices.
1770 */
1771
1772 if (!is_otg_enabled(musb))
1773 musb_start(musb);
1774
David Brownell84e250f2009-03-31 12:30:04 -07001775 otg_set_peripheral(musb->xceiv, &musb->g);
1776
Felipe Balbi550a7372008-07-24 12:27:36 +03001777 spin_unlock_irqrestore(&musb->lock, flags);
1778
1779 if (is_otg_enabled(musb)) {
1780 DBG(3, "OTG startup...\n");
1781
1782 /* REVISIT: funcall to other code, which also
1783 * handles power budgeting ... this way also
1784 * ensures HdrcStart is indirectly called.
1785 */
1786 retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
1787 if (retval < 0) {
1788 DBG(1, "add_hcd failed, %d\n", retval);
1789 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -07001790 otg_set_peripheral(musb->xceiv, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001791 musb->gadget_driver = NULL;
1792 musb->g.dev.driver = NULL;
1793 spin_unlock_irqrestore(&musb->lock, flags);
1794 }
1795 }
1796 }
1797
1798 return retval;
1799}
1800EXPORT_SYMBOL(usb_gadget_register_driver);
1801
1802static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1803{
1804 int i;
1805 struct musb_hw_ep *hw_ep;
1806
1807 /* don't disconnect if it's not connected */
1808 if (musb->g.speed == USB_SPEED_UNKNOWN)
1809 driver = NULL;
1810 else
1811 musb->g.speed = USB_SPEED_UNKNOWN;
1812
1813 /* deactivate the hardware */
1814 if (musb->softconnect) {
1815 musb->softconnect = 0;
1816 musb_pullup(musb, 0);
1817 }
1818 musb_stop(musb);
1819
1820 /* killing any outstanding requests will quiesce the driver;
1821 * then report disconnect
1822 */
1823 if (driver) {
1824 for (i = 0, hw_ep = musb->endpoints;
1825 i < musb->nr_endpoints;
1826 i++, hw_ep++) {
1827 musb_ep_select(musb->mregs, i);
1828 if (hw_ep->is_shared_fifo /* || !epnum */) {
1829 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1830 } else {
1831 if (hw_ep->max_packet_sz_tx)
1832 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1833 if (hw_ep->max_packet_sz_rx)
1834 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1835 }
1836 }
1837
1838 spin_unlock(&musb->lock);
1839 driver->disconnect(&musb->g);
1840 spin_lock(&musb->lock);
1841 }
1842}
1843
1844/*
1845 * Unregister the gadget driver. Used by gadget drivers when
1846 * unregistering themselves from the controller.
1847 *
1848 * @param driver the gadget driver to unregister
1849 */
1850int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1851{
1852 unsigned long flags;
1853 int retval = 0;
1854 struct musb *musb = the_gadget;
1855
1856 if (!driver || !driver->unbind || !musb)
1857 return -EINVAL;
1858
1859 /* REVISIT always use otg_set_peripheral() here too;
1860 * this needs to shut down the OTG engine.
1861 */
1862
1863 spin_lock_irqsave(&musb->lock, flags);
1864
1865#ifdef CONFIG_USB_MUSB_OTG
1866 musb_hnp_stop(musb);
1867#endif
1868
1869 if (musb->gadget_driver == driver) {
1870
1871 (void) musb_gadget_vbus_draw(&musb->g, 0);
1872
David Brownell84e250f2009-03-31 12:30:04 -07001873 musb->xceiv->state = OTG_STATE_UNDEFINED;
Felipe Balbi550a7372008-07-24 12:27:36 +03001874 stop_activity(musb, driver);
David Brownell84e250f2009-03-31 12:30:04 -07001875 otg_set_peripheral(musb->xceiv, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001876
1877 DBG(3, "unregistering driver %s\n", driver->function);
1878 spin_unlock_irqrestore(&musb->lock, flags);
1879 driver->unbind(&musb->g);
1880 spin_lock_irqsave(&musb->lock, flags);
1881
1882 musb->gadget_driver = NULL;
1883 musb->g.dev.driver = NULL;
1884
1885 musb->is_active = 0;
1886 musb_platform_try_idle(musb, 0);
1887 } else
1888 retval = -EINVAL;
1889 spin_unlock_irqrestore(&musb->lock, flags);
1890
1891 if (is_otg_enabled(musb) && retval == 0) {
1892 usb_remove_hcd(musb_to_hcd(musb));
1893 /* FIXME we need to be able to register another
1894 * gadget driver here and have everything work;
1895 * that currently misbehaves.
1896 */
1897 }
1898
1899 return retval;
1900}
1901EXPORT_SYMBOL(usb_gadget_unregister_driver);
1902
1903
1904/* ----------------------------------------------------------------------- */
1905
1906/* lifecycle operations called through plat_uds.c */
1907
1908void musb_g_resume(struct musb *musb)
1909{
1910 musb->is_suspended = 0;
David Brownell84e250f2009-03-31 12:30:04 -07001911 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001912 case OTG_STATE_B_IDLE:
1913 break;
1914 case OTG_STATE_B_WAIT_ACON:
1915 case OTG_STATE_B_PERIPHERAL:
1916 musb->is_active = 1;
1917 if (musb->gadget_driver && musb->gadget_driver->resume) {
1918 spin_unlock(&musb->lock);
1919 musb->gadget_driver->resume(&musb->g);
1920 spin_lock(&musb->lock);
1921 }
1922 break;
1923 default:
1924 WARNING("unhandled RESUME transition (%s)\n",
1925 otg_state_string(musb));
1926 }
1927}
1928
1929/* called when SOF packets stop for 3+ msec */
1930void musb_g_suspend(struct musb *musb)
1931{
1932 u8 devctl;
1933
1934 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1935 DBG(3, "devctl %02x\n", devctl);
1936
David Brownell84e250f2009-03-31 12:30:04 -07001937 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001938 case OTG_STATE_B_IDLE:
1939 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
David Brownell84e250f2009-03-31 12:30:04 -07001940 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001941 break;
1942 case OTG_STATE_B_PERIPHERAL:
1943 musb->is_suspended = 1;
1944 if (musb->gadget_driver && musb->gadget_driver->suspend) {
1945 spin_unlock(&musb->lock);
1946 musb->gadget_driver->suspend(&musb->g);
1947 spin_lock(&musb->lock);
1948 }
1949 break;
1950 default:
1951 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
1952 * A_PERIPHERAL may need care too
1953 */
1954 WARNING("unhandled SUSPEND transition (%s)\n",
1955 otg_state_string(musb));
1956 }
1957}
1958
1959/* Called during SRP */
1960void musb_g_wakeup(struct musb *musb)
1961{
1962 musb_gadget_wakeup(&musb->g);
1963}
1964
1965/* called when VBUS drops below session threshold, and in other cases */
1966void musb_g_disconnect(struct musb *musb)
1967{
1968 void __iomem *mregs = musb->mregs;
1969 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
1970
1971 DBG(3, "devctl %02x\n", devctl);
1972
1973 /* clear HR */
1974 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
1975
1976 /* don't draw vbus until new b-default session */
1977 (void) musb_gadget_vbus_draw(&musb->g, 0);
1978
1979 musb->g.speed = USB_SPEED_UNKNOWN;
1980 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
1981 spin_unlock(&musb->lock);
1982 musb->gadget_driver->disconnect(&musb->g);
1983 spin_lock(&musb->lock);
1984 }
1985
David Brownell84e250f2009-03-31 12:30:04 -07001986 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001987 default:
1988#ifdef CONFIG_USB_MUSB_OTG
1989 DBG(2, "Unhandled disconnect %s, setting a_idle\n",
1990 otg_state_string(musb));
David Brownell84e250f2009-03-31 12:30:04 -07001991 musb->xceiv->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07001992 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001993 break;
1994 case OTG_STATE_A_PERIPHERAL:
David Brownell1de00da2009-04-02 10:16:11 -07001995 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07001996 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001997 break;
1998 case OTG_STATE_B_WAIT_ACON:
1999 case OTG_STATE_B_HOST:
2000#endif
2001 case OTG_STATE_B_PERIPHERAL:
2002 case OTG_STATE_B_IDLE:
David Brownell84e250f2009-03-31 12:30:04 -07002003 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002004 break;
2005 case OTG_STATE_B_SRP_INIT:
2006 break;
2007 }
2008
2009 musb->is_active = 0;
2010}
2011
2012void musb_g_reset(struct musb *musb)
2013__releases(musb->lock)
2014__acquires(musb->lock)
2015{
2016 void __iomem *mbase = musb->mregs;
2017 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2018 u8 power;
2019
2020 DBG(3, "<== %s addr=%x driver '%s'\n",
2021 (devctl & MUSB_DEVCTL_BDEVICE)
2022 ? "B-Device" : "A-Device",
2023 musb_readb(mbase, MUSB_FADDR),
2024 musb->gadget_driver
2025 ? musb->gadget_driver->driver.name
2026 : NULL
2027 );
2028
2029 /* report disconnect, if we didn't already (flushing EP state) */
2030 if (musb->g.speed != USB_SPEED_UNKNOWN)
2031 musb_g_disconnect(musb);
2032
2033 /* clear HR */
2034 else if (devctl & MUSB_DEVCTL_HR)
2035 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2036
2037
2038 /* what speed did we negotiate? */
2039 power = musb_readb(mbase, MUSB_POWER);
2040 musb->g.speed = (power & MUSB_POWER_HSMODE)
2041 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2042
2043 /* start in USB_STATE_DEFAULT */
2044 musb->is_active = 1;
2045 musb->is_suspended = 0;
2046 MUSB_DEV_MODE(musb);
2047 musb->address = 0;
2048 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2049
2050 musb->may_wakeup = 0;
2051 musb->g.b_hnp_enable = 0;
2052 musb->g.a_alt_hnp_support = 0;
2053 musb->g.a_hnp_support = 0;
2054
2055 /* Normal reset, as B-Device;
2056 * or else after HNP, as A-Device
2057 */
2058 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -07002059 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002060 musb->g.is_a_peripheral = 0;
2061 } else if (is_otg_enabled(musb)) {
David Brownell84e250f2009-03-31 12:30:04 -07002062 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002063 musb->g.is_a_peripheral = 1;
2064 } else
2065 WARN_ON(1);
2066
2067 /* start with default limits on VBUS power draw */
2068 (void) musb_gadget_vbus_draw(&musb->g,
2069 is_otg_enabled(musb) ? 8 : 100);
2070}