blob: 6f3cf4ce8bd6e374970640a007ee8dd0839b3638 [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
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010095#define is_buffer_mapped(req) (is_dma_capable() && \
96 (req->map_state != UN_MAPPED))
97
Hema Kalliguddi92d27112010-11-15 04:24:01 -060098/* Maps the buffer to dma */
99
100static inline void map_dma_buffer(struct musb_request *request,
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100101 struct musb *musb, struct musb_ep *musb_ep)
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600102{
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +0100103 int compatible = true;
104 struct dma_controller *dma = musb->dma_controller;
105
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100106 request->map_state = UN_MAPPED;
107
108 if (!is_dma_capable() || !musb_ep->dma)
109 return;
110
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +0100111 /* Check if DMA engine can handle this request.
112 * DMA code must reject the USB request explicitly.
113 * Default behaviour is to map the request.
114 */
115 if (dma->is_compatible)
116 compatible = dma->is_compatible(musb_ep->dma,
117 musb_ep->packet_sz, request->request.buf,
118 request->request.length);
119 if (!compatible)
120 return;
121
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600122 if (request->request.dma == DMA_ADDR_INVALID) {
123 request->request.dma = dma_map_single(
124 musb->controller,
125 request->request.buf,
126 request->request.length,
127 request->tx
128 ? DMA_TO_DEVICE
129 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100130 request->map_state = MUSB_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600131 } else {
132 dma_sync_single_for_device(musb->controller,
133 request->request.dma,
134 request->request.length,
135 request->tx
136 ? DMA_TO_DEVICE
137 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100138 request->map_state = PRE_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600139 }
140}
141
142/* Unmap the buffer from dma and maps it back to cpu */
143static inline void unmap_dma_buffer(struct musb_request *request,
144 struct musb *musb)
145{
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100146 if (!is_buffer_mapped(request))
147 return;
148
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600149 if (request->request.dma == DMA_ADDR_INVALID) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300150 dev_vdbg(musb->controller,
151 "not unmapping a never mapped buffer\n");
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600152 return;
153 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100154 if (request->map_state == MUSB_MAPPED) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600155 dma_unmap_single(musb->controller,
156 request->request.dma,
157 request->request.length,
158 request->tx
159 ? DMA_TO_DEVICE
160 : DMA_FROM_DEVICE);
161 request->request.dma = DMA_ADDR_INVALID;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100162 } else { /* PRE_MAPPED */
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600163 dma_sync_single_for_cpu(musb->controller,
164 request->request.dma,
165 request->request.length,
166 request->tx
167 ? DMA_TO_DEVICE
168 : DMA_FROM_DEVICE);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600169 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100170 request->map_state = UN_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600171}
172
Felipe Balbi550a7372008-07-24 12:27:36 +0300173/*
174 * Immediately complete a request.
175 *
176 * @param request the request to complete
177 * @param status the status to complete the request with
178 * Context: controller locked, IRQs blocked.
179 */
180void musb_g_giveback(
181 struct musb_ep *ep,
182 struct usb_request *request,
183 int status)
184__releases(ep->musb->lock)
185__acquires(ep->musb->lock)
186{
187 struct musb_request *req;
188 struct musb *musb;
189 int busy = ep->busy;
190
191 req = to_musb_request(request);
192
Felipe Balbiad1adb82011-02-16 12:40:05 +0200193 list_del(&req->list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300194 if (req->request.status == -EINPROGRESS)
195 req->request.status = status;
196 musb = req->musb;
197
198 ep->busy = 1;
199 spin_unlock(&musb->lock);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100200 unmap_dma_buffer(req, musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300201 if (request->status == 0)
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300202 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300203 ep->end_point.name, request,
204 req->request.actual, req->request.length);
205 else
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300206 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300207 ep->end_point.name, request,
208 req->request.actual, req->request.length,
209 request->status);
210 req->request.complete(&req->ep->end_point, &req->request);
211 spin_lock(&musb->lock);
212 ep->busy = busy;
213}
214
215/* ----------------------------------------------------------------------- */
216
217/*
218 * Abort requests queued to an endpoint using the status. Synchronous.
219 * caller locked controller and blocked irqs, and selected this ep.
220 */
221static void nuke(struct musb_ep *ep, const int status)
222{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300223 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300224 struct musb_request *req = NULL;
225 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
226
227 ep->busy = 1;
228
229 if (is_dma_capable() && ep->dma) {
230 struct dma_controller *c = ep->musb->dma_controller;
231 int value;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700232
Felipe Balbi550a7372008-07-24 12:27:36 +0300233 if (ep->is_in) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700234 /*
235 * The programming guide says that we must not clear
236 * the DMAMODE bit before DMAENAB, so we only
237 * clear it in the second write...
238 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300239 musb_writew(epio, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700240 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
Felipe Balbi550a7372008-07-24 12:27:36 +0300241 musb_writew(epio, MUSB_TXCSR,
242 0 | MUSB_TXCSR_FLUSHFIFO);
243 } else {
244 musb_writew(epio, MUSB_RXCSR,
245 0 | MUSB_RXCSR_FLUSHFIFO);
246 musb_writew(epio, MUSB_RXCSR,
247 0 | MUSB_RXCSR_FLUSHFIFO);
248 }
249
250 value = c->channel_abort(ep->dma);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300251 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
252 ep->name, value);
Felipe Balbi550a7372008-07-24 12:27:36 +0300253 c->channel_release(ep->dma);
254 ep->dma = NULL;
255 }
256
Felipe Balbiad1adb82011-02-16 12:40:05 +0200257 while (!list_empty(&ep->req_list)) {
258 req = list_first_entry(&ep->req_list, struct musb_request, list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300259 musb_g_giveback(ep, &req->request, status);
260 }
261}
262
263/* ----------------------------------------------------------------------- */
264
265/* Data transfers - pure PIO, pure DMA, or mixed mode */
266
267/*
268 * This assumes the separate CPPI engine is responding to DMA requests
269 * from the usb core ... sequenced a bit differently from mentor dma.
270 */
271
272static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
273{
274 if (can_bulk_split(musb, ep->type))
275 return ep->hw_ep->max_packet_sz_tx;
276 else
277 return ep->packet_sz;
278}
279
280
281#ifdef CONFIG_USB_INVENTRA_DMA
282
283/* Peripheral tx (IN) using Mentor DMA works as follows:
284 Only mode 0 is used for transfers <= wPktSize,
285 mode 1 is used for larger transfers,
286
287 One of the following happens:
288 - Host sends IN token which causes an endpoint interrupt
289 -> TxAvail
290 -> if DMA is currently busy, exit.
291 -> if queue is non-empty, txstate().
292
293 - Request is queued by the gadget driver.
294 -> if queue was previously empty, txstate()
295
296 txstate()
297 -> start
298 /\ -> setup DMA
299 | (data is transferred to the FIFO, then sent out when
300 | IN token(s) are recd from Host.
301 | -> DMA interrupt on completion
302 | calls TxAvail.
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700303 | -> stop DMA, ~DMAENAB,
Felipe Balbi550a7372008-07-24 12:27:36 +0300304 | -> set TxPktRdy for last short pkt or zlp
305 | -> Complete Request
306 | -> Continue next request (call txstate)
307 |___________________________________|
308
309 * Non-Mentor DMA engines can of course work differently, such as by
310 * upleveling from irq-per-packet to irq-per-buffer.
311 */
312
313#endif
314
315/*
316 * An endpoint is transmitting data. This can be called either from
317 * the IRQ routine or from ep.queue() to kickstart a request on an
318 * endpoint.
319 *
320 * Context: controller locked, IRQs blocked, endpoint selected
321 */
322static void txstate(struct musb *musb, struct musb_request *req)
323{
324 u8 epnum = req->epnum;
325 struct musb_ep *musb_ep;
326 void __iomem *epio = musb->endpoints[epnum].regs;
327 struct usb_request *request;
328 u16 fifo_count = 0, csr;
329 int use_dma = 0;
330
331 musb_ep = req->ep;
332
333 /* we shouldn't get here while DMA is active ... but we do ... */
334 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300335 dev_dbg(musb->controller, "dma pending...\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300336 return;
337 }
338
339 /* read TXCSR before */
340 csr = musb_readw(epio, MUSB_TXCSR);
341
342 request = &req->request;
343 fifo_count = min(max_ep_writesize(musb, musb_ep),
344 (int)(request->length - request->actual));
345
346 if (csr & MUSB_TXCSR_TXPKTRDY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300347 dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300348 musb_ep->end_point.name, csr);
349 return;
350 }
351
352 if (csr & MUSB_TXCSR_P_SENDSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300353 dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300354 musb_ep->end_point.name, csr);
355 return;
356 }
357
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300358 dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300359 epnum, musb_ep->packet_sz, fifo_count,
360 csr);
361
362#ifndef CONFIG_MUSB_PIO_ONLY
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100363 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300364 struct dma_controller *c = musb->dma_controller;
Ming Lei66af83d2010-09-20 10:32:06 +0300365 size_t request_size;
366
367 /* setup DMA, then program endpoint CSR */
368 request_size = min_t(size_t, request->length - request->actual,
369 musb_ep->dma->max_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300370
371 use_dma = (request->dma != DMA_ADDR_INVALID);
372
373 /* MUSB_TXCSR_P_ISO is still set correctly */
374
375#ifdef CONFIG_USB_INVENTRA_DMA
376 {
Anand Gadiyard1043a22009-04-02 12:07:08 -0700377 if (request_size < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300378 musb_ep->dma->desired_mode = 0;
379 else
380 musb_ep->dma->desired_mode = 1;
381
382 use_dma = use_dma && c->channel_program(
383 musb_ep->dma, musb_ep->packet_sz,
384 musb_ep->dma->desired_mode,
Cliff Cai796a83f2009-12-21 21:18:02 -0500385 request->dma + request->actual, request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300386 if (use_dma) {
387 if (musb_ep->dma->desired_mode == 0) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700388 /*
389 * We must not clear the DMAMODE bit
390 * before the DMAENAB bit -- and the
391 * latter doesn't always get cleared
392 * before we get here...
393 */
394 csr &= ~(MUSB_TXCSR_AUTOSET
395 | MUSB_TXCSR_DMAENAB);
396 musb_writew(epio, MUSB_TXCSR, csr
397 | MUSB_TXCSR_P_WZC_BITS);
398 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300399 csr |= (MUSB_TXCSR_DMAENAB |
400 MUSB_TXCSR_MODE);
401 /* against programming guide */
Ming Leif11d8932010-09-24 13:44:04 +0300402 } else {
403 csr |= (MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300404 | MUSB_TXCSR_DMAMODE
405 | MUSB_TXCSR_MODE);
Ming Leif11d8932010-09-24 13:44:04 +0300406 if (!musb_ep->hb_mult)
407 csr |= MUSB_TXCSR_AUTOSET;
408 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300409 csr &= ~MUSB_TXCSR_P_UNDERRUN;
Ming Leif11d8932010-09-24 13:44:04 +0300410
Felipe Balbi550a7372008-07-24 12:27:36 +0300411 musb_writew(epio, MUSB_TXCSR, csr);
412 }
413 }
414
415#elif defined(CONFIG_USB_TI_CPPI_DMA)
416 /* program endpoint CSR first, then setup DMA */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700417 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
Sergei Shtylyov37e3ee92009-03-27 12:53:32 -0700418 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
419 MUSB_TXCSR_MODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300420 musb_writew(epio, MUSB_TXCSR,
421 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
422 | csr);
423
424 /* ensure writebuffer is empty */
425 csr = musb_readw(epio, MUSB_TXCSR);
426
427 /* NOTE host side sets DMAENAB later than this; both are
428 * OK since the transfer dma glue (between CPPI and Mentor
429 * fifos) just tells CPPI it could start. Data only moves
430 * to the USB TX fifo when both fifos are ready.
431 */
432
433 /* "mode" is irrelevant here; handle terminating ZLPs like
434 * PIO does, since the hardware RNDIS mode seems unreliable
435 * except for the last-packet-is-already-short case.
436 */
437 use_dma = use_dma && c->channel_program(
438 musb_ep->dma, musb_ep->packet_sz,
439 0,
Ming Lei66af83d2010-09-20 10:32:06 +0300440 request->dma + request->actual,
441 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300442 if (!use_dma) {
443 c->channel_release(musb_ep->dma);
444 musb_ep->dma = NULL;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700445 csr &= ~MUSB_TXCSR_DMAENAB;
446 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300447 /* invariant: prequest->buf is non-null */
448 }
449#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
450 use_dma = use_dma && c->channel_program(
451 musb_ep->dma, musb_ep->packet_sz,
452 request->zero,
Ming Lei66af83d2010-09-20 10:32:06 +0300453 request->dma + request->actual,
454 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300455#endif
456 }
457#endif
458
459 if (!use_dma) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600460 /*
461 * Unmap the dma buffer back to cpu if dma channel
462 * programming fails
463 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100464 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600465
Felipe Balbi550a7372008-07-24 12:27:36 +0300466 musb_write_fifo(musb_ep->hw_ep, fifo_count,
467 (u8 *) (request->buf + request->actual));
468 request->actual += fifo_count;
469 csr |= MUSB_TXCSR_TXPKTRDY;
470 csr &= ~MUSB_TXCSR_P_UNDERRUN;
471 musb_writew(epio, MUSB_TXCSR, csr);
472 }
473
474 /* host may already have the data when this message shows... */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300475 dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300476 musb_ep->end_point.name, use_dma ? "dma" : "pio",
477 request->actual, request->length,
478 musb_readw(epio, MUSB_TXCSR),
479 fifo_count,
480 musb_readw(epio, MUSB_TXMAXP));
481}
482
483/*
484 * FIFO state update (e.g. data ready).
485 * Called from IRQ, with controller locked.
486 */
487void musb_g_tx(struct musb *musb, u8 epnum)
488{
489 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200490 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300491 struct usb_request *request;
492 u8 __iomem *mbase = musb->mregs;
493 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
494 void __iomem *epio = musb->endpoints[epnum].regs;
495 struct dma_channel *dma;
496
497 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200498 req = next_request(musb_ep);
499 request = &req->request;
Felipe Balbi550a7372008-07-24 12:27:36 +0300500
501 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300502 dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300503
504 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300505
506 /*
507 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
508 * probably rates reporting as a host error.
509 */
510 if (csr & MUSB_TXCSR_P_SENTSTALL) {
511 csr |= MUSB_TXCSR_P_WZC_BITS;
512 csr &= ~MUSB_TXCSR_P_SENTSTALL;
513 musb_writew(epio, MUSB_TXCSR, csr);
514 return;
515 }
516
517 if (csr & MUSB_TXCSR_P_UNDERRUN) {
518 /* We NAKed, no big deal... little reason to care. */
519 csr |= MUSB_TXCSR_P_WZC_BITS;
520 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
521 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300522 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
523 epnum, request);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300524 }
525
526 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
527 /*
528 * SHOULD NOT HAPPEN... has with CPPI though, after
529 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300530 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300531 dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300532 return;
533 }
534
535 if (request) {
536 u8 is_dma = 0;
537
538 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
539 is_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300540 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300541 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
Mian Yousaf Kaukab100d4a92011-03-15 16:24:24 +0100542 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
Felipe Balbi550a7372008-07-24 12:27:36 +0300543 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300544 /* Ensure writebuffer is empty. */
545 csr = musb_readw(epio, MUSB_TXCSR);
546 request->actual += musb_ep->dma->actual_len;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300547 dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300548 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300549 }
550
Ming Leie7379aa2010-09-24 13:44:14 +0300551 /*
552 * First, maybe a terminating short packet. Some DMA
553 * engines might handle this by themselves.
554 */
555 if ((request->zero && request->length
556 && (request->length % musb_ep->packet_sz == 0)
557 && (request->actual == request->length))
Felipe Balbi550a7372008-07-24 12:27:36 +0300558#ifdef CONFIG_USB_INVENTRA_DMA
Ming Leie7379aa2010-09-24 13:44:14 +0300559 || (is_dma && (!dma->desired_mode ||
560 (request->actual &
561 (musb_ep->packet_sz - 1))))
Felipe Balbi550a7372008-07-24 12:27:36 +0300562#endif
Ming Leie7379aa2010-09-24 13:44:14 +0300563 ) {
564 /*
565 * On DMA completion, FIFO may not be
566 * available yet...
567 */
568 if (csr & MUSB_TXCSR_TXPKTRDY)
569 return;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300570
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300571 dev_dbg(musb->controller, "sending zero pkt\n");
Ming Leie7379aa2010-09-24 13:44:14 +0300572 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
573 | MUSB_TXCSR_TXPKTRDY);
574 request->zero = 0;
575 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300576
Ming Leie7379aa2010-09-24 13:44:14 +0300577 if (request->actual == request->length) {
578 musb_g_giveback(musb_ep, request, 0);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200579 req = musb_ep->desc ? next_request(musb_ep) : NULL;
580 if (!req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300581 dev_dbg(musb->controller, "%s idle now\n",
Ming Leie7379aa2010-09-24 13:44:14 +0300582 musb_ep->end_point.name);
583 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300584 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300585 }
586
Felipe Balbiad1adb82011-02-16 12:40:05 +0200587 txstate(musb, req);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300588 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300589}
590
591/* ------------------------------------------------------------ */
592
593#ifdef CONFIG_USB_INVENTRA_DMA
594
595/* Peripheral rx (OUT) using Mentor DMA works as follows:
596 - Only mode 0 is used.
597
598 - Request is queued by the gadget class driver.
599 -> if queue was previously empty, rxstate()
600
601 - Host sends OUT token which causes an endpoint interrupt
602 /\ -> RxReady
603 | -> if request queued, call rxstate
604 | /\ -> setup DMA
605 | | -> DMA interrupt on completion
606 | | -> RxReady
607 | | -> stop DMA
608 | | -> ack the read
609 | | -> if data recd = max expected
610 | | by the request, or host
611 | | sent a short packet,
612 | | complete the request,
613 | | and start the next one.
614 | |_____________________________________|
615 | else just wait for the host
616 | to send the next OUT token.
617 |__________________________________________________|
618
619 * Non-Mentor DMA engines can of course work differently.
620 */
621
622#endif
623
624/*
625 * Context: controller locked, IRQs blocked, endpoint selected
626 */
627static void rxstate(struct musb *musb, struct musb_request *req)
628{
Felipe Balbi550a7372008-07-24 12:27:36 +0300629 const u8 epnum = req->epnum;
630 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300631 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300632 void __iomem *epio = musb->endpoints[epnum].regs;
Felipe Balbic2c96322009-02-21 15:29:42 -0800633 unsigned fifo_count = 0;
Ming Leibd2e74d2010-09-20 10:32:01 +0300634 u16 len;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300635 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300636 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
637
638 if (hw_ep->is_shared_fifo)
639 musb_ep = &hw_ep->ep_in;
640 else
641 musb_ep = &hw_ep->ep_out;
642
643 len = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300644
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300645 /* We shouldn't get here while DMA is active, but we do... */
646 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300647 dev_dbg(musb->controller, "DMA pending...\n");
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300648 return;
649 }
650
651 if (csr & MUSB_RXCSR_P_SENDSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300652 dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300653 musb_ep->end_point.name, csr);
654 return;
655 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300656
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100657 if (is_cppi_enabled() && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300658 struct dma_controller *c = musb->dma_controller;
659 struct dma_channel *channel = musb_ep->dma;
660
661 /* NOTE: CPPI won't actually stop advancing the DMA
662 * queue after short packet transfers, so this is almost
663 * always going to run as IRQ-per-packet DMA so that
664 * faults will be handled correctly.
665 */
666 if (c->channel_program(channel,
667 musb_ep->packet_sz,
668 !request->short_not_ok,
669 request->dma + request->actual,
670 request->length - request->actual)) {
671
672 /* make sure that if an rxpkt arrived after the irq,
673 * the cppi engine will be ready to take it as soon
674 * as DMA is enabled
675 */
676 csr &= ~(MUSB_RXCSR_AUTOCLEAR
677 | MUSB_RXCSR_DMAMODE);
678 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
679 musb_writew(epio, MUSB_RXCSR, csr);
680 return;
681 }
682 }
683
684 if (csr & MUSB_RXCSR_RXPKTRDY) {
685 len = musb_readw(epio, MUSB_RXCOUNT);
686 if (request->actual < request->length) {
687#ifdef CONFIG_USB_INVENTRA_DMA
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100688 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300689 struct dma_controller *c;
690 struct dma_channel *channel;
691 int use_dma = 0;
692
693 c = musb->dma_controller;
694 channel = musb_ep->dma;
695
696 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
697 * mode 0 only. So we do not get endpoint interrupts due to DMA
698 * completion. We only get interrupts from DMA controller.
699 *
700 * We could operate in DMA mode 1 if we knew the size of the tranfer
701 * in advance. For mass storage class, request->length = what the host
702 * sends, so that'd work. But for pretty much everything else,
703 * request->length is routinely more than what the host sends. For
704 * most these gadgets, end of is signified either by a short packet,
705 * or filling the last byte of the buffer. (Sending extra data in
706 * that last pckate should trigger an overflow fault.) But in mode 1,
707 * we don't get DMA completion interrrupt for short packets.
708 *
709 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
710 * to get endpoint interrupt on every DMA req, but that didn't seem
711 * to work reliably.
712 *
713 * REVISIT an updated g_file_storage can set req->short_not_ok, which
714 * then becomes usable as a runtime "use mode 1" hint...
715 */
716
717 csr |= MUSB_RXCSR_DMAENAB;
Ming Lei490e5fb2010-09-20 10:32:03 +0300718#ifdef USE_MODE1
Ming Lei9001d802010-09-25 05:50:43 -0500719 csr |= MUSB_RXCSR_AUTOCLEAR;
Felipe Balbi550a7372008-07-24 12:27:36 +0300720 /* csr |= MUSB_RXCSR_DMAMODE; */
721
722 /* this special sequence (enabling and then
723 * disabling MUSB_RXCSR_DMAMODE) is required
724 * to get DMAReq to activate
725 */
726 musb_writew(epio, MUSB_RXCSR,
727 csr | MUSB_RXCSR_DMAMODE);
Ming Lei9001d802010-09-25 05:50:43 -0500728#else
729 if (!musb_ep->hb_mult &&
730 musb_ep->hw_ep->rx_double_buffered)
731 csr |= MUSB_RXCSR_AUTOCLEAR;
Felipe Balbi550a7372008-07-24 12:27:36 +0300732#endif
733 musb_writew(epio, MUSB_RXCSR, csr);
734
735 if (request->actual < request->length) {
736 int transfer_size = 0;
737#ifdef USE_MODE1
Ming Lei1018b4e2010-09-20 10:32:04 +0300738 transfer_size = min(request->length - request->actual,
Felipe Balbi550a7372008-07-24 12:27:36 +0300739 channel->max_len);
740#else
Ming Lei1018b4e2010-09-20 10:32:04 +0300741 transfer_size = min(request->length - request->actual,
742 (unsigned)len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300743#endif
744 if (transfer_size <= musb_ep->packet_sz)
745 musb_ep->dma->desired_mode = 0;
746 else
747 musb_ep->dma->desired_mode = 1;
748
749 use_dma = c->channel_program(
750 channel,
751 musb_ep->packet_sz,
752 channel->desired_mode,
753 request->dma
754 + request->actual,
755 transfer_size);
756 }
757
758 if (use_dma)
759 return;
760 }
761#endif /* Mentor's DMA */
762
763 fifo_count = request->length - request->actual;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300764 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300765 musb_ep->end_point.name,
766 len, fifo_count,
767 musb_ep->packet_sz);
768
Felipe Balbic2c96322009-02-21 15:29:42 -0800769 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300770
771#ifdef CONFIG_USB_TUSB_OMAP_DMA
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100772 if (tusb_dma_omap() && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300773 struct dma_controller *c = musb->dma_controller;
774 struct dma_channel *channel = musb_ep->dma;
775 u32 dma_addr = request->dma + request->actual;
776 int ret;
777
778 ret = c->channel_program(channel,
779 musb_ep->packet_sz,
780 channel->desired_mode,
781 dma_addr,
782 fifo_count);
783 if (ret)
784 return;
785 }
786#endif
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600787 /*
788 * Unmap the dma buffer back to cpu if dma channel
789 * programming fails. This buffer is mapped if the
790 * channel allocation is successful
791 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100792 if (is_buffer_mapped(req)) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600793 unmap_dma_buffer(req, musb);
794
Ming Leie75df372010-11-16 23:37:37 +0800795 /*
796 * Clear DMAENAB and AUTOCLEAR for the
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600797 * PIO mode transfer
798 */
Ming Leie75df372010-11-16 23:37:37 +0800799 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600800 musb_writew(epio, MUSB_RXCSR, csr);
801 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300802
803 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
804 (request->buf + request->actual));
805 request->actual += fifo_count;
806
807 /* REVISIT if we left anything in the fifo, flush
808 * it and report -EOVERFLOW
809 */
810
811 /* ack the read! */
812 csr |= MUSB_RXCSR_P_WZC_BITS;
813 csr &= ~MUSB_RXCSR_RXPKTRDY;
814 musb_writew(epio, MUSB_RXCSR, csr);
815 }
816 }
817
818 /* reach the end or short packet detected */
819 if (request->actual == request->length || len < musb_ep->packet_sz)
820 musb_g_giveback(musb_ep, request, 0);
821}
822
823/*
824 * Data ready for a request; called from IRQ
825 */
826void musb_g_rx(struct musb *musb, u8 epnum)
827{
828 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200829 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300830 struct usb_request *request;
831 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300832 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300833 void __iomem *epio = musb->endpoints[epnum].regs;
834 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300835 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
836
837 if (hw_ep->is_shared_fifo)
838 musb_ep = &hw_ep->ep_in;
839 else
840 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300841
842 musb_ep_select(mbase, epnum);
843
Felipe Balbiad1adb82011-02-16 12:40:05 +0200844 req = next_request(musb_ep);
845 if (!req)
Maulik Mankad0abdc362009-12-22 16:18:19 +0530846 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300847
Felipe Balbiad1adb82011-02-16 12:40:05 +0200848 request = &req->request;
849
Felipe Balbi550a7372008-07-24 12:27:36 +0300850 csr = musb_readw(epio, MUSB_RXCSR);
851 dma = is_dma_capable() ? musb_ep->dma : NULL;
852
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300853 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
Felipe Balbi550a7372008-07-24 12:27:36 +0300854 csr, dma ? " (dma)" : "", request);
855
856 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300857 csr |= MUSB_RXCSR_P_WZC_BITS;
858 csr &= ~MUSB_RXCSR_P_SENTSTALL;
859 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300860 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300861 }
862
863 if (csr & MUSB_RXCSR_P_OVERRUN) {
864 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
865 csr &= ~MUSB_RXCSR_P_OVERRUN;
866 musb_writew(epio, MUSB_RXCSR, csr);
867
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300868 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300869 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300870 request->status = -EOVERFLOW;
871 }
872 if (csr & MUSB_RXCSR_INCOMPRX) {
873 /* REVISIT not necessarily an error */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300874 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300875 }
876
877 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
878 /* "should not happen"; likely RXPKTRDY pending for DMA */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300879 dev_dbg(musb->controller, "%s busy, csr %04x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300880 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300881 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300882 }
883
884 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
885 csr &= ~(MUSB_RXCSR_AUTOCLEAR
886 | MUSB_RXCSR_DMAENAB
887 | MUSB_RXCSR_DMAMODE);
888 musb_writew(epio, MUSB_RXCSR,
889 MUSB_RXCSR_P_WZC_BITS | csr);
890
891 request->actual += musb_ep->dma->actual_len;
892
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300893 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300894 epnum, csr,
895 musb_readw(epio, MUSB_RXCSR),
896 musb_ep->dma->actual_len, request);
897
898#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
899 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -0500900 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +0300901 || (dma->actual_len
902 & (musb_ep->packet_sz - 1))) {
903 /* ack the read! */
904 csr &= ~MUSB_RXCSR_RXPKTRDY;
905 musb_writew(epio, MUSB_RXCSR, csr);
906 }
907
908 /* incomplete, and not short? wait for next IN packet */
909 if ((request->actual < request->length)
910 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -0500911 == musb_ep->packet_sz)) {
912 /* In double buffer case, continue to unload fifo if
913 * there is Rx packet in FIFO.
914 **/
915 csr = musb_readw(epio, MUSB_RXCSR);
916 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
917 hw_ep->rx_double_buffered)
918 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300919 return;
Ming Lei9001d802010-09-25 05:50:43 -0500920 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300921#endif
922 musb_g_giveback(musb_ep, request, 0);
923
Felipe Balbiad1adb82011-02-16 12:40:05 +0200924 req = next_request(musb_ep);
925 if (!req)
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300926 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300927 }
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +0530928#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA)
Ming Lei9001d802010-09-25 05:50:43 -0500929exit:
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +0530930#endif
Sergei Shtylyov43467862010-09-24 13:44:12 +0300931 /* Analyze request */
Felipe Balbiad1adb82011-02-16 12:40:05 +0200932 rxstate(musb, req);
Felipe Balbi550a7372008-07-24 12:27:36 +0300933}
934
935/* ------------------------------------------------------------ */
936
937static int musb_gadget_enable(struct usb_ep *ep,
938 const struct usb_endpoint_descriptor *desc)
939{
940 unsigned long flags;
941 struct musb_ep *musb_ep;
942 struct musb_hw_ep *hw_ep;
943 void __iomem *regs;
944 struct musb *musb;
945 void __iomem *mbase;
946 u8 epnum;
947 u16 csr;
948 unsigned tmp;
949 int status = -EINVAL;
950
951 if (!ep || !desc)
952 return -EINVAL;
953
954 musb_ep = to_musb_ep(ep);
955 hw_ep = musb_ep->hw_ep;
956 regs = hw_ep->regs;
957 musb = musb_ep->musb;
958 mbase = musb->mregs;
959 epnum = musb_ep->current_epnum;
960
961 spin_lock_irqsave(&musb->lock, flags);
962
963 if (musb_ep->desc) {
964 status = -EBUSY;
965 goto fail;
966 }
Julia Lawall96bcd092009-01-24 17:57:24 -0800967 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +0300968
969 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -0800970 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300971 goto fail;
972
973 /* REVISIT this rules out high bandwidth periodic transfers */
974 tmp = le16_to_cpu(desc->wMaxPacketSize);
Ming Leif11d8932010-09-24 13:44:04 +0300975 if (tmp & ~0x07ff) {
976 int ok;
977
978 if (usb_endpoint_dir_in(desc))
979 ok = musb->hb_iso_tx;
980 else
981 ok = musb->hb_iso_rx;
982
983 if (!ok) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300984 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
Ming Leif11d8932010-09-24 13:44:04 +0300985 goto fail;
986 }
987 musb_ep->hb_mult = (tmp >> 11) & 3;
988 } else {
989 musb_ep->hb_mult = 0;
990 }
991
992 musb_ep->packet_sz = tmp & 0x7ff;
993 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300994
995 /* enable the interrupts for the endpoint, set the endpoint
996 * packet size (or fail), set the mode, clear the fifo
997 */
998 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -0800999 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001000 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1001
1002 if (hw_ep->is_shared_fifo)
1003 musb_ep->is_in = 1;
1004 if (!musb_ep->is_in)
1005 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001006
1007 if (tmp > hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001008 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001009 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001010 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001011
1012 int_txe |= (1 << epnum);
1013 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1014
1015 /* REVISIT if can_bulk_split(), use by updating "tmp";
1016 * likewise high bandwidth periodic tx
1017 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001018 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -05001019 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -05001020 */
Felipe Balbi06624812011-01-21 13:39:20 +08001021 if (musb->double_buffer_not_ok)
1022 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
1023 else
1024 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1025 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001026
1027 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1028 if (musb_readw(regs, MUSB_TXCSR)
1029 & MUSB_TXCSR_FIFONOTEMPTY)
1030 csr |= MUSB_TXCSR_FLUSHFIFO;
1031 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1032 csr |= MUSB_TXCSR_P_ISO;
1033
1034 /* set twice in case of double buffering */
1035 musb_writew(regs, MUSB_TXCSR, csr);
1036 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1037 musb_writew(regs, MUSB_TXCSR, csr);
1038
1039 } else {
1040 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
1041
1042 if (hw_ep->is_shared_fifo)
1043 musb_ep->is_in = 0;
1044 if (musb_ep->is_in)
1045 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001046
1047 if (tmp > hw_ep->max_packet_sz_rx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001048 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001049 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001050 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001051
1052 int_rxe |= (1 << epnum);
1053 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
1054
1055 /* REVISIT if can_bulk_combine() use by updating "tmp"
1056 * likewise high bandwidth periodic rx
1057 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001058 /* Set RXMAXP with the FIFO size of the endpoint
1059 * to disable double buffering mode.
1060 */
Felipe Balbi06624812011-01-21 13:39:20 +08001061 if (musb->double_buffer_not_ok)
1062 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1063 else
1064 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1065 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001066
1067 /* force shared fifo to OUT-only mode */
1068 if (hw_ep->is_shared_fifo) {
1069 csr = musb_readw(regs, MUSB_TXCSR);
1070 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1071 musb_writew(regs, MUSB_TXCSR, csr);
1072 }
1073
1074 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1075 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1076 csr |= MUSB_RXCSR_P_ISO;
1077 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1078 csr |= MUSB_RXCSR_DISNYET;
1079
1080 /* set twice in case of double buffering */
1081 musb_writew(regs, MUSB_RXCSR, csr);
1082 musb_writew(regs, MUSB_RXCSR, csr);
1083 }
1084
1085 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1086 * for some reason you run out of channels here.
1087 */
1088 if (is_dma_capable() && musb->dma_controller) {
1089 struct dma_controller *c = musb->dma_controller;
1090
1091 musb_ep->dma = c->channel_alloc(c, hw_ep,
1092 (desc->bEndpointAddress & USB_DIR_IN));
1093 } else
1094 musb_ep->dma = NULL;
1095
1096 musb_ep->desc = desc;
1097 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001098 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001099 status = 0;
1100
1101 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1102 musb_driver_name, musb_ep->end_point.name,
1103 ({ char *s; switch (musb_ep->type) {
1104 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1105 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1106 default: s = "iso"; break;
1107 }; s; }),
1108 musb_ep->is_in ? "IN" : "OUT",
1109 musb_ep->dma ? "dma, " : "",
1110 musb_ep->packet_sz);
1111
1112 schedule_work(&musb->irq_work);
1113
1114fail:
1115 spin_unlock_irqrestore(&musb->lock, flags);
1116 return status;
1117}
1118
1119/*
1120 * Disable an endpoint flushing all requests queued.
1121 */
1122static int musb_gadget_disable(struct usb_ep *ep)
1123{
1124 unsigned long flags;
1125 struct musb *musb;
1126 u8 epnum;
1127 struct musb_ep *musb_ep;
1128 void __iomem *epio;
1129 int status = 0;
1130
1131 musb_ep = to_musb_ep(ep);
1132 musb = musb_ep->musb;
1133 epnum = musb_ep->current_epnum;
1134 epio = musb->endpoints[epnum].regs;
1135
1136 spin_lock_irqsave(&musb->lock, flags);
1137 musb_ep_select(musb->mregs, epnum);
1138
1139 /* zero the endpoint sizes */
1140 if (musb_ep->is_in) {
1141 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1142 int_txe &= ~(1 << epnum);
1143 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1144 musb_writew(epio, MUSB_TXMAXP, 0);
1145 } else {
1146 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1147 int_rxe &= ~(1 << epnum);
1148 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1149 musb_writew(epio, MUSB_RXMAXP, 0);
1150 }
1151
1152 musb_ep->desc = NULL;
1153
1154 /* abort all pending DMA and requests */
1155 nuke(musb_ep, -ESHUTDOWN);
1156
1157 schedule_work(&musb->irq_work);
1158
1159 spin_unlock_irqrestore(&(musb->lock), flags);
1160
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001161 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001162
1163 return status;
1164}
1165
1166/*
1167 * Allocate a request for an endpoint.
1168 * Reused by ep0 code.
1169 */
1170struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1171{
1172 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001173 struct musb *musb = musb_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03001174 struct musb_request *request = NULL;
1175
1176 request = kzalloc(sizeof *request, gfp_flags);
Felipe Balbi0607f862010-12-01 11:03:54 +02001177 if (!request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001178 dev_dbg(musb->controller, "not enough memory\n");
Felipe Balbi0607f862010-12-01 11:03:54 +02001179 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001180 }
1181
Felipe Balbi0607f862010-12-01 11:03:54 +02001182 request->request.dma = DMA_ADDR_INVALID;
1183 request->epnum = musb_ep->current_epnum;
1184 request->ep = musb_ep;
1185
Felipe Balbi550a7372008-07-24 12:27:36 +03001186 return &request->request;
1187}
1188
1189/*
1190 * Free a request
1191 * Reused by ep0 code.
1192 */
1193void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1194{
1195 kfree(to_musb_request(req));
1196}
1197
1198static LIST_HEAD(buffers);
1199
1200struct free_record {
1201 struct list_head list;
1202 struct device *dev;
1203 unsigned bytes;
1204 dma_addr_t dma;
1205};
1206
1207/*
1208 * Context: controller locked, IRQs blocked.
1209 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001210void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001211{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001212 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001213 req->tx ? "TX/IN" : "RX/OUT",
1214 &req->request, req->request.length, req->epnum);
1215
1216 musb_ep_select(musb->mregs, req->epnum);
1217 if (req->tx)
1218 txstate(musb, req);
1219 else
1220 rxstate(musb, req);
1221}
1222
1223static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1224 gfp_t gfp_flags)
1225{
1226 struct musb_ep *musb_ep;
1227 struct musb_request *request;
1228 struct musb *musb;
1229 int status = 0;
1230 unsigned long lockflags;
1231
1232 if (!ep || !req)
1233 return -EINVAL;
1234 if (!req->buf)
1235 return -ENODATA;
1236
1237 musb_ep = to_musb_ep(ep);
1238 musb = musb_ep->musb;
1239
1240 request = to_musb_request(req);
1241 request->musb = musb;
1242
1243 if (request->ep != musb_ep)
1244 return -EINVAL;
1245
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001246 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001247
1248 /* request is mine now... */
1249 request->request.actual = 0;
1250 request->request.status = -EINPROGRESS;
1251 request->epnum = musb_ep->current_epnum;
1252 request->tx = musb_ep->is_in;
1253
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +01001254 map_dma_buffer(request, musb, musb_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001255
1256 spin_lock_irqsave(&musb->lock, lockflags);
1257
1258 /* don't queue if the ep is down */
1259 if (!musb_ep->desc) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001260 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001261 req, ep->name, "disabled");
1262 status = -ESHUTDOWN;
1263 goto cleanup;
1264 }
1265
1266 /* add request to the list */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001267 list_add_tail(&request->list, &musb_ep->req_list);
Felipe Balbi550a7372008-07-24 12:27:36 +03001268
1269 /* it this is the head of the queue, start i/o ... */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001270 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
Felipe Balbi550a7372008-07-24 12:27:36 +03001271 musb_ep_restart(musb, request);
1272
1273cleanup:
1274 spin_unlock_irqrestore(&musb->lock, lockflags);
1275 return status;
1276}
1277
1278static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1279{
1280 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001281 struct musb_request *req = to_musb_request(request);
1282 struct musb_request *r;
Felipe Balbi550a7372008-07-24 12:27:36 +03001283 unsigned long flags;
1284 int status = 0;
1285 struct musb *musb = musb_ep->musb;
1286
1287 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1288 return -EINVAL;
1289
1290 spin_lock_irqsave(&musb->lock, flags);
1291
1292 list_for_each_entry(r, &musb_ep->req_list, list) {
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001293 if (r == req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001294 break;
1295 }
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001296 if (r != req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001297 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001298 status = -EINVAL;
1299 goto done;
1300 }
1301
1302 /* if the hardware doesn't have the request, easy ... */
Felipe Balbi3d5ad132011-03-22 11:38:49 +02001303 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
Felipe Balbi550a7372008-07-24 12:27:36 +03001304 musb_g_giveback(musb_ep, request, -ECONNRESET);
1305
1306 /* ... else abort the dma transfer ... */
1307 else if (is_dma_capable() && musb_ep->dma) {
1308 struct dma_controller *c = musb->dma_controller;
1309
1310 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1311 if (c->channel_abort)
1312 status = c->channel_abort(musb_ep->dma);
1313 else
1314 status = -EBUSY;
1315 if (status == 0)
1316 musb_g_giveback(musb_ep, request, -ECONNRESET);
1317 } else {
1318 /* NOTE: by sticking to easily tested hardware/driver states,
1319 * we leave counting of in-flight packets imprecise.
1320 */
1321 musb_g_giveback(musb_ep, request, -ECONNRESET);
1322 }
1323
1324done:
1325 spin_unlock_irqrestore(&musb->lock, flags);
1326 return status;
1327}
1328
1329/*
1330 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1331 * data but will queue requests.
1332 *
1333 * exported to ep0 code
1334 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001335static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001336{
1337 struct musb_ep *musb_ep = to_musb_ep(ep);
1338 u8 epnum = musb_ep->current_epnum;
1339 struct musb *musb = musb_ep->musb;
1340 void __iomem *epio = musb->endpoints[epnum].regs;
1341 void __iomem *mbase;
1342 unsigned long flags;
1343 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001344 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001345 int status = 0;
1346
1347 if (!ep)
1348 return -EINVAL;
1349 mbase = musb->mregs;
1350
1351 spin_lock_irqsave(&musb->lock, flags);
1352
1353 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1354 status = -EINVAL;
1355 goto done;
1356 }
1357
1358 musb_ep_select(mbase, epnum);
1359
Felipe Balbiad1adb82011-02-16 12:40:05 +02001360 request = next_request(musb_ep);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001361 if (value) {
1362 if (request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001363 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001364 ep->name);
1365 status = -EAGAIN;
1366 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001367 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001368 /* Cannot portably stall with non-empty FIFO */
1369 if (musb_ep->is_in) {
1370 csr = musb_readw(epio, MUSB_TXCSR);
1371 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001372 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001373 status = -EAGAIN;
1374 goto done;
1375 }
1376 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001377 } else
1378 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001379
1380 /* set/clear the stall and toggle bits */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001381 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
Felipe Balbi550a7372008-07-24 12:27:36 +03001382 if (musb_ep->is_in) {
1383 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001384 csr |= MUSB_TXCSR_P_WZC_BITS
1385 | MUSB_TXCSR_CLRDATATOG;
1386 if (value)
1387 csr |= MUSB_TXCSR_P_SENDSTALL;
1388 else
1389 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1390 | MUSB_TXCSR_P_SENTSTALL);
1391 csr &= ~MUSB_TXCSR_TXPKTRDY;
1392 musb_writew(epio, MUSB_TXCSR, csr);
1393 } else {
1394 csr = musb_readw(epio, MUSB_RXCSR);
1395 csr |= MUSB_RXCSR_P_WZC_BITS
1396 | MUSB_RXCSR_FLUSHFIFO
1397 | MUSB_RXCSR_CLRDATATOG;
1398 if (value)
1399 csr |= MUSB_RXCSR_P_SENDSTALL;
1400 else
1401 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1402 | MUSB_RXCSR_P_SENTSTALL);
1403 musb_writew(epio, MUSB_RXCSR, csr);
1404 }
1405
Felipe Balbi550a7372008-07-24 12:27:36 +03001406 /* maybe start the first request in the queue */
1407 if (!musb_ep->busy && !value && request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001408 dev_dbg(musb->controller, "restarting the request\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001409 musb_ep_restart(musb, request);
1410 }
1411
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001412done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001413 spin_unlock_irqrestore(&musb->lock, flags);
1414 return status;
1415}
1416
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001417/*
1418 * Sets the halt feature with the clear requests ignored
1419 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001420static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001421{
1422 struct musb_ep *musb_ep = to_musb_ep(ep);
1423
1424 if (!ep)
1425 return -EINVAL;
1426
1427 musb_ep->wedged = 1;
1428
1429 return usb_ep_set_halt(ep);
1430}
1431
Felipe Balbi550a7372008-07-24 12:27:36 +03001432static int musb_gadget_fifo_status(struct usb_ep *ep)
1433{
1434 struct musb_ep *musb_ep = to_musb_ep(ep);
1435 void __iomem *epio = musb_ep->hw_ep->regs;
1436 int retval = -EINVAL;
1437
1438 if (musb_ep->desc && !musb_ep->is_in) {
1439 struct musb *musb = musb_ep->musb;
1440 int epnum = musb_ep->current_epnum;
1441 void __iomem *mbase = musb->mregs;
1442 unsigned long flags;
1443
1444 spin_lock_irqsave(&musb->lock, flags);
1445
1446 musb_ep_select(mbase, epnum);
1447 /* FIXME return zero unless RXPKTRDY is set */
1448 retval = musb_readw(epio, MUSB_RXCOUNT);
1449
1450 spin_unlock_irqrestore(&musb->lock, flags);
1451 }
1452 return retval;
1453}
1454
1455static void musb_gadget_fifo_flush(struct usb_ep *ep)
1456{
1457 struct musb_ep *musb_ep = to_musb_ep(ep);
1458 struct musb *musb = musb_ep->musb;
1459 u8 epnum = musb_ep->current_epnum;
1460 void __iomem *epio = musb->endpoints[epnum].regs;
1461 void __iomem *mbase;
1462 unsigned long flags;
1463 u16 csr, int_txe;
1464
1465 mbase = musb->mregs;
1466
1467 spin_lock_irqsave(&musb->lock, flags);
1468 musb_ep_select(mbase, (u8) epnum);
1469
1470 /* disable interrupts */
1471 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1472 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1473
1474 if (musb_ep->is_in) {
1475 csr = musb_readw(epio, MUSB_TXCSR);
1476 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1477 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1478 musb_writew(epio, MUSB_TXCSR, csr);
1479 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1480 musb_writew(epio, MUSB_TXCSR, csr);
1481 }
1482 } else {
1483 csr = musb_readw(epio, MUSB_RXCSR);
1484 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1485 musb_writew(epio, MUSB_RXCSR, csr);
1486 musb_writew(epio, MUSB_RXCSR, csr);
1487 }
1488
1489 /* re-enable interrupt */
1490 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1491 spin_unlock_irqrestore(&musb->lock, flags);
1492}
1493
1494static const struct usb_ep_ops musb_ep_ops = {
1495 .enable = musb_gadget_enable,
1496 .disable = musb_gadget_disable,
1497 .alloc_request = musb_alloc_request,
1498 .free_request = musb_free_request,
1499 .queue = musb_gadget_queue,
1500 .dequeue = musb_gadget_dequeue,
1501 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001502 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001503 .fifo_status = musb_gadget_fifo_status,
1504 .fifo_flush = musb_gadget_fifo_flush
1505};
1506
1507/* ----------------------------------------------------------------------- */
1508
1509static int musb_gadget_get_frame(struct usb_gadget *gadget)
1510{
1511 struct musb *musb = gadget_to_musb(gadget);
1512
1513 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1514}
1515
1516static int musb_gadget_wakeup(struct usb_gadget *gadget)
1517{
1518 struct musb *musb = gadget_to_musb(gadget);
1519 void __iomem *mregs = musb->mregs;
1520 unsigned long flags;
1521 int status = -EINVAL;
1522 u8 power, devctl;
1523 int retries;
1524
1525 spin_lock_irqsave(&musb->lock, flags);
1526
David Brownell84e250f2009-03-31 12:30:04 -07001527 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001528 case OTG_STATE_B_PERIPHERAL:
1529 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1530 * that's part of the standard usb 1.1 state machine, and
1531 * doesn't affect OTG transitions.
1532 */
1533 if (musb->may_wakeup && musb->is_suspended)
1534 break;
1535 goto done;
1536 case OTG_STATE_B_IDLE:
1537 /* Start SRP ... OTG not required. */
1538 devctl = musb_readb(mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001539 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001540 devctl |= MUSB_DEVCTL_SESSION;
1541 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1542 devctl = musb_readb(mregs, MUSB_DEVCTL);
1543 retries = 100;
1544 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1545 devctl = musb_readb(mregs, MUSB_DEVCTL);
1546 if (retries-- < 1)
1547 break;
1548 }
1549 retries = 10000;
1550 while (devctl & MUSB_DEVCTL_SESSION) {
1551 devctl = musb_readb(mregs, MUSB_DEVCTL);
1552 if (retries-- < 1)
1553 break;
1554 }
1555
Hema HK86205432011-03-22 16:54:22 +05301556 spin_unlock_irqrestore(&musb->lock, flags);
1557 otg_start_srp(musb->xceiv);
1558 spin_lock_irqsave(&musb->lock, flags);
1559
Felipe Balbi550a7372008-07-24 12:27:36 +03001560 /* Block idling for at least 1s */
1561 musb_platform_try_idle(musb,
1562 jiffies + msecs_to_jiffies(1 * HZ));
1563
1564 status = 0;
1565 goto done;
1566 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001567 dev_dbg(musb->controller, "Unhandled wake: %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02001568 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001569 goto done;
1570 }
1571
1572 status = 0;
1573
1574 power = musb_readb(mregs, MUSB_POWER);
1575 power |= MUSB_POWER_RESUME;
1576 musb_writeb(mregs, MUSB_POWER, power);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001577 dev_dbg(musb->controller, "issue wakeup\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001578
1579 /* FIXME do this next chunk in a timer callback, no udelay */
1580 mdelay(2);
1581
1582 power = musb_readb(mregs, MUSB_POWER);
1583 power &= ~MUSB_POWER_RESUME;
1584 musb_writeb(mregs, MUSB_POWER, power);
1585done:
1586 spin_unlock_irqrestore(&musb->lock, flags);
1587 return status;
1588}
1589
1590static int
1591musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1592{
1593 struct musb *musb = gadget_to_musb(gadget);
1594
1595 musb->is_self_powered = !!is_selfpowered;
1596 return 0;
1597}
1598
1599static void musb_pullup(struct musb *musb, int is_on)
1600{
1601 u8 power;
1602
1603 power = musb_readb(musb->mregs, MUSB_POWER);
1604 if (is_on)
1605 power |= MUSB_POWER_SOFTCONN;
1606 else
1607 power &= ~MUSB_POWER_SOFTCONN;
1608
1609 /* FIXME if on, HdrcStart; if off, HdrcStop */
1610
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001611 dev_dbg(musb->controller, "gadget %s D+ pullup %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001612 musb->gadget_driver->function, is_on ? "on" : "off");
1613 musb_writeb(musb->mregs, MUSB_POWER, power);
1614}
1615
1616#if 0
1617static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1618{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001619 dev_dbg(musb->controller, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001620
1621 /*
1622 * FIXME iff driver's softconnect flag is set (as it is during probe,
1623 * though that can clear it), just musb_pullup().
1624 */
1625
1626 return -EINVAL;
1627}
1628#endif
1629
1630static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1631{
1632 struct musb *musb = gadget_to_musb(gadget);
1633
David Brownell84e250f2009-03-31 12:30:04 -07001634 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001635 return -EOPNOTSUPP;
David Brownell84e250f2009-03-31 12:30:04 -07001636 return otg_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001637}
1638
1639static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1640{
1641 struct musb *musb = gadget_to_musb(gadget);
1642 unsigned long flags;
1643
1644 is_on = !!is_on;
1645
1646 /* NOTE: this assumes we are sensing vbus; we'd rather
1647 * not pullup unless the B-session is active.
1648 */
1649 spin_lock_irqsave(&musb->lock, flags);
1650 if (is_on != musb->softconnect) {
1651 musb->softconnect = is_on;
1652 musb_pullup(musb, is_on);
1653 }
1654 spin_unlock_irqrestore(&musb->lock, flags);
1655 return 0;
1656}
1657
1658static const struct usb_gadget_ops musb_gadget_operations = {
1659 .get_frame = musb_gadget_get_frame,
1660 .wakeup = musb_gadget_wakeup,
1661 .set_selfpowered = musb_gadget_set_self_powered,
1662 /* .vbus_session = musb_gadget_vbus_session, */
1663 .vbus_draw = musb_gadget_vbus_draw,
1664 .pullup = musb_gadget_pullup,
1665};
1666
1667/* ----------------------------------------------------------------------- */
1668
1669/* Registration */
1670
1671/* Only this registration code "knows" the rule (from USB standards)
1672 * about there being only one external upstream port. It assumes
1673 * all peripheral ports are external...
1674 */
1675static struct musb *the_gadget;
1676
1677static void musb_gadget_release(struct device *dev)
1678{
1679 /* kref_put(WHAT) */
1680 dev_dbg(dev, "%s\n", __func__);
1681}
1682
1683
1684static void __init
1685init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1686{
1687 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1688
1689 memset(ep, 0, sizeof *ep);
1690
1691 ep->current_epnum = epnum;
1692 ep->musb = musb;
1693 ep->hw_ep = hw_ep;
1694 ep->is_in = is_in;
1695
1696 INIT_LIST_HEAD(&ep->req_list);
1697
1698 sprintf(ep->name, "ep%d%s", epnum,
1699 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1700 is_in ? "in" : "out"));
1701 ep->end_point.name = ep->name;
1702 INIT_LIST_HEAD(&ep->end_point.ep_list);
1703 if (!epnum) {
1704 ep->end_point.maxpacket = 64;
1705 ep->end_point.ops = &musb_g_ep0_ops;
1706 musb->g.ep0 = &ep->end_point;
1707 } else {
1708 if (is_in)
1709 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1710 else
1711 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1712 ep->end_point.ops = &musb_ep_ops;
1713 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1714 }
1715}
1716
1717/*
1718 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1719 * to the rest of the driver state.
1720 */
1721static inline void __init musb_g_init_endpoints(struct musb *musb)
1722{
1723 u8 epnum;
1724 struct musb_hw_ep *hw_ep;
1725 unsigned count = 0;
1726
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001727 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001728 INIT_LIST_HEAD(&(musb->g.ep_list));
1729
1730 for (epnum = 0, hw_ep = musb->endpoints;
1731 epnum < musb->nr_endpoints;
1732 epnum++, hw_ep++) {
1733 if (hw_ep->is_shared_fifo /* || !epnum */) {
1734 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1735 count++;
1736 } else {
1737 if (hw_ep->max_packet_sz_tx) {
1738 init_peripheral_ep(musb, &hw_ep->ep_in,
1739 epnum, 1);
1740 count++;
1741 }
1742 if (hw_ep->max_packet_sz_rx) {
1743 init_peripheral_ep(musb, &hw_ep->ep_out,
1744 epnum, 0);
1745 count++;
1746 }
1747 }
1748 }
1749}
1750
1751/* called once during driver setup to initialize and link into
1752 * the driver model; memory is zeroed.
1753 */
1754int __init musb_gadget_setup(struct musb *musb)
1755{
1756 int status;
1757
1758 /* REVISIT minor race: if (erroneously) setting up two
1759 * musb peripherals at the same time, only the bus lock
1760 * is probably held.
1761 */
1762 if (the_gadget)
1763 return -EBUSY;
1764 the_gadget = musb;
1765
1766 musb->g.ops = &musb_gadget_operations;
1767 musb->g.is_dualspeed = 1;
1768 musb->g.speed = USB_SPEED_UNKNOWN;
1769
1770 /* this "gadget" abstracts/virtualizes the controller */
Kay Sievers427c4f32008-11-07 01:52:53 +01001771 dev_set_name(&musb->g.dev, "gadget");
Felipe Balbi550a7372008-07-24 12:27:36 +03001772 musb->g.dev.parent = musb->controller;
1773 musb->g.dev.dma_mask = musb->controller->dma_mask;
1774 musb->g.dev.release = musb_gadget_release;
1775 musb->g.name = musb_driver_name;
1776
1777 if (is_otg_enabled(musb))
1778 musb->g.is_otg = 1;
1779
1780 musb_g_init_endpoints(musb);
1781
1782 musb->is_active = 0;
1783 musb_platform_try_idle(musb, 0);
1784
1785 status = device_register(&musb->g.dev);
Rahul Ruikare2c34042010-10-02 01:35:48 -05001786 if (status != 0) {
1787 put_device(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001788 the_gadget = NULL;
Rahul Ruikare2c34042010-10-02 01:35:48 -05001789 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001790 return status;
1791}
1792
1793void musb_gadget_cleanup(struct musb *musb)
1794{
1795 if (musb != the_gadget)
1796 return;
1797
1798 device_unregister(&musb->g.dev);
1799 the_gadget = NULL;
1800}
1801
1802/*
1803 * Register the gadget driver. Used by gadget drivers when
1804 * registering themselves with the controller.
1805 *
1806 * -EINVAL something went wrong (not driver)
1807 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001808 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001809 *
1810 * @param driver the gadget driver
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001811 * @param bind the driver's bind function
Felipe Balbi550a7372008-07-24 12:27:36 +03001812 * @return <0 if error, 0 if everything is fine
1813 */
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001814int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1815 int (*bind)(struct usb_gadget *))
Felipe Balbi550a7372008-07-24 12:27:36 +03001816{
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001817 struct musb *musb = the_gadget;
1818 unsigned long flags;
1819 int retval = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001820
1821 if (!driver
1822 || driver->speed != USB_SPEED_HIGH
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001823 || !bind || !driver->setup)
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001824 goto err0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001825
1826 /* driver must be initialized to support peripheral mode */
Roel Kluin08e6c972010-02-02 14:47:17 -08001827 if (!musb) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001828 dev_dbg(musb->controller, "no dev??\n");
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001829 retval = -ENODEV;
1830 goto err0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001831 }
1832
Hema HK7acc6192011-02-28 14:19:34 +05301833 pm_runtime_get_sync(musb->controller);
1834
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001835 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001836
1837 if (musb->gadget_driver) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001838 dev_dbg(musb->controller, "%s is already bound to %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001839 musb_driver_name,
1840 musb->gadget_driver->driver.name);
1841 retval = -EBUSY;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001842 goto err0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001843 }
1844
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001845 spin_lock_irqsave(&musb->lock, flags);
1846 musb->gadget_driver = driver;
1847 musb->g.dev.driver = &driver->driver;
1848 driver->driver.bus = NULL;
1849 musb->softconnect = 1;
1850 spin_unlock_irqrestore(&musb->lock, flags);
1851
1852 retval = bind(&musb->g);
1853 if (retval) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001854 dev_dbg(musb->controller, "bind to driver %s failed --> %d\n",
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001855 driver->driver.name, retval);
1856 goto err1;
1857 }
1858
1859 spin_lock_irqsave(&musb->lock, flags);
1860
1861 otg_set_peripheral(musb->xceiv, &musb->g);
1862 musb->xceiv->state = OTG_STATE_B_IDLE;
1863 musb->is_active = 1;
1864
1865 /*
1866 * FIXME this ignores the softconnect flag. Drivers are
1867 * allowed hold the peripheral inactive until for example
1868 * userspace hooks up printer hardware or DSP codecs, so
1869 * hosts only see fully functional devices.
1870 */
1871
1872 if (!is_otg_enabled(musb))
1873 musb_start(musb);
1874
1875 otg_set_peripheral(musb->xceiv, &musb->g);
1876
Felipe Balbi550a7372008-07-24 12:27:36 +03001877 spin_unlock_irqrestore(&musb->lock, flags);
1878
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001879 if (is_otg_enabled(musb)) {
1880 struct usb_hcd *hcd = musb_to_hcd(musb);
Felipe Balbif362a472008-08-04 13:53:52 +03001881
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001882 dev_dbg(musb->controller, "OTG startup...\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001883
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001884 /* REVISIT: funcall to other code, which also
1885 * handles power budgeting ... this way also
1886 * ensures HdrcStart is indirectly called.
Felipe Balbi550a7372008-07-24 12:27:36 +03001887 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001888 retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
1889 if (retval < 0) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001890 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001891 goto err2;
Felipe Balbi550a7372008-07-24 12:27:36 +03001892 }
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001893
Hema HK5f1e8ce2011-03-17 16:11:58 +05301894 if ((musb->xceiv->last_event == USB_EVENT_ID)
1895 && musb->xceiv->set_vbus)
1896 otg_set_vbus(musb->xceiv, 1);
1897
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001898 hcd->self.uses_pio_for_control = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001899 }
Jarkko Nikulacdefce12011-04-29 16:17:35 +03001900 if (musb->xceiv->last_event == USB_EVENT_NONE)
1901 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001902
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001903 return 0;
1904
1905err2:
1906 if (!is_otg_enabled(musb))
1907 musb_stop(musb);
1908
1909err1:
1910 musb->gadget_driver = NULL;
1911 musb->g.dev.driver = NULL;
1912
1913err0:
Felipe Balbi550a7372008-07-24 12:27:36 +03001914 return retval;
1915}
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001916EXPORT_SYMBOL(usb_gadget_probe_driver);
Felipe Balbi550a7372008-07-24 12:27:36 +03001917
1918static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1919{
1920 int i;
1921 struct musb_hw_ep *hw_ep;
1922
1923 /* don't disconnect if it's not connected */
1924 if (musb->g.speed == USB_SPEED_UNKNOWN)
1925 driver = NULL;
1926 else
1927 musb->g.speed = USB_SPEED_UNKNOWN;
1928
1929 /* deactivate the hardware */
1930 if (musb->softconnect) {
1931 musb->softconnect = 0;
1932 musb_pullup(musb, 0);
1933 }
1934 musb_stop(musb);
1935
1936 /* killing any outstanding requests will quiesce the driver;
1937 * then report disconnect
1938 */
1939 if (driver) {
1940 for (i = 0, hw_ep = musb->endpoints;
1941 i < musb->nr_endpoints;
1942 i++, hw_ep++) {
1943 musb_ep_select(musb->mregs, i);
1944 if (hw_ep->is_shared_fifo /* || !epnum */) {
1945 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1946 } else {
1947 if (hw_ep->max_packet_sz_tx)
1948 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1949 if (hw_ep->max_packet_sz_rx)
1950 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1951 }
1952 }
1953
1954 spin_unlock(&musb->lock);
1955 driver->disconnect(&musb->g);
1956 spin_lock(&musb->lock);
1957 }
1958}
1959
1960/*
1961 * Unregister the gadget driver. Used by gadget drivers when
1962 * unregistering themselves from the controller.
1963 *
1964 * @param driver the gadget driver to unregister
1965 */
1966int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1967{
Felipe Balbi550a7372008-07-24 12:27:36 +03001968 struct musb *musb = the_gadget;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001969 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03001970
1971 if (!driver || !driver->unbind || !musb)
1972 return -EINVAL;
1973
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001974 if (!musb->gadget_driver)
1975 return -EINVAL;
1976
Hema HK7acc6192011-02-28 14:19:34 +05301977 if (musb->xceiv->last_event == USB_EVENT_NONE)
1978 pm_runtime_get_sync(musb->controller);
1979
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001980 /*
1981 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03001982 * this needs to shut down the OTG engine.
1983 */
1984
1985 spin_lock_irqsave(&musb->lock, flags);
1986
1987#ifdef CONFIG_USB_MUSB_OTG
1988 musb_hnp_stop(musb);
1989#endif
1990
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001991 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001992
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001993 musb->xceiv->state = OTG_STATE_UNDEFINED;
1994 stop_activity(musb, driver);
1995 otg_set_peripheral(musb->xceiv, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001996
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001997 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001998
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001999 spin_unlock_irqrestore(&musb->lock, flags);
2000 driver->unbind(&musb->g);
2001 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002002
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002003 musb->gadget_driver = NULL;
2004 musb->g.dev.driver = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002005
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002006 musb->is_active = 0;
2007 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002008 spin_unlock_irqrestore(&musb->lock, flags);
2009
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002010 if (is_otg_enabled(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002011 usb_remove_hcd(musb_to_hcd(musb));
2012 /* FIXME we need to be able to register another
2013 * gadget driver here and have everything work;
2014 * that currently misbehaves.
2015 */
2016 }
2017
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002018 if (!is_otg_enabled(musb))
2019 musb_stop(musb);
2020
Hema HK7acc6192011-02-28 14:19:34 +05302021 pm_runtime_put(musb->controller);
2022
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002023 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002024}
2025EXPORT_SYMBOL(usb_gadget_unregister_driver);
2026
2027
2028/* ----------------------------------------------------------------------- */
2029
2030/* lifecycle operations called through plat_uds.c */
2031
2032void musb_g_resume(struct musb *musb)
2033{
2034 musb->is_suspended = 0;
David Brownell84e250f2009-03-31 12:30:04 -07002035 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002036 case OTG_STATE_B_IDLE:
2037 break;
2038 case OTG_STATE_B_WAIT_ACON:
2039 case OTG_STATE_B_PERIPHERAL:
2040 musb->is_active = 1;
2041 if (musb->gadget_driver && musb->gadget_driver->resume) {
2042 spin_unlock(&musb->lock);
2043 musb->gadget_driver->resume(&musb->g);
2044 spin_lock(&musb->lock);
2045 }
2046 break;
2047 default:
2048 WARNING("unhandled RESUME transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002049 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002050 }
2051}
2052
2053/* called when SOF packets stop for 3+ msec */
2054void musb_g_suspend(struct musb *musb)
2055{
2056 u8 devctl;
2057
2058 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002059 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002060
David Brownell84e250f2009-03-31 12:30:04 -07002061 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002062 case OTG_STATE_B_IDLE:
2063 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
David Brownell84e250f2009-03-31 12:30:04 -07002064 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002065 break;
2066 case OTG_STATE_B_PERIPHERAL:
2067 musb->is_suspended = 1;
2068 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2069 spin_unlock(&musb->lock);
2070 musb->gadget_driver->suspend(&musb->g);
2071 spin_lock(&musb->lock);
2072 }
2073 break;
2074 default:
2075 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2076 * A_PERIPHERAL may need care too
2077 */
2078 WARNING("unhandled SUSPEND transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002079 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002080 }
2081}
2082
2083/* Called during SRP */
2084void musb_g_wakeup(struct musb *musb)
2085{
2086 musb_gadget_wakeup(&musb->g);
2087}
2088
2089/* called when VBUS drops below session threshold, and in other cases */
2090void musb_g_disconnect(struct musb *musb)
2091{
2092 void __iomem *mregs = musb->mregs;
2093 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2094
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002095 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002096
2097 /* clear HR */
2098 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2099
2100 /* don't draw vbus until new b-default session */
2101 (void) musb_gadget_vbus_draw(&musb->g, 0);
2102
2103 musb->g.speed = USB_SPEED_UNKNOWN;
2104 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2105 spin_unlock(&musb->lock);
2106 musb->gadget_driver->disconnect(&musb->g);
2107 spin_lock(&musb->lock);
2108 }
2109
David Brownell84e250f2009-03-31 12:30:04 -07002110 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002111 default:
2112#ifdef CONFIG_USB_MUSB_OTG
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002113 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002114 otg_state_string(musb->xceiv->state));
David Brownell84e250f2009-03-31 12:30:04 -07002115 musb->xceiv->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002116 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002117 break;
2118 case OTG_STATE_A_PERIPHERAL:
David Brownell1de00da2009-04-02 10:16:11 -07002119 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07002120 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002121 break;
2122 case OTG_STATE_B_WAIT_ACON:
2123 case OTG_STATE_B_HOST:
2124#endif
2125 case OTG_STATE_B_PERIPHERAL:
2126 case OTG_STATE_B_IDLE:
David Brownell84e250f2009-03-31 12:30:04 -07002127 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002128 break;
2129 case OTG_STATE_B_SRP_INIT:
2130 break;
2131 }
2132
2133 musb->is_active = 0;
2134}
2135
2136void musb_g_reset(struct musb *musb)
2137__releases(musb->lock)
2138__acquires(musb->lock)
2139{
2140 void __iomem *mbase = musb->mregs;
2141 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2142 u8 power;
2143
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002144 dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002145 (devctl & MUSB_DEVCTL_BDEVICE)
2146 ? "B-Device" : "A-Device",
2147 musb_readb(mbase, MUSB_FADDR),
2148 musb->gadget_driver
2149 ? musb->gadget_driver->driver.name
2150 : NULL
2151 );
2152
2153 /* report disconnect, if we didn't already (flushing EP state) */
2154 if (musb->g.speed != USB_SPEED_UNKNOWN)
2155 musb_g_disconnect(musb);
2156
2157 /* clear HR */
2158 else if (devctl & MUSB_DEVCTL_HR)
2159 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2160
2161
2162 /* what speed did we negotiate? */
2163 power = musb_readb(mbase, MUSB_POWER);
2164 musb->g.speed = (power & MUSB_POWER_HSMODE)
2165 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2166
2167 /* start in USB_STATE_DEFAULT */
2168 musb->is_active = 1;
2169 musb->is_suspended = 0;
2170 MUSB_DEV_MODE(musb);
2171 musb->address = 0;
2172 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2173
2174 musb->may_wakeup = 0;
2175 musb->g.b_hnp_enable = 0;
2176 musb->g.a_alt_hnp_support = 0;
2177 musb->g.a_hnp_support = 0;
2178
2179 /* Normal reset, as B-Device;
2180 * or else after HNP, as A-Device
2181 */
2182 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -07002183 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002184 musb->g.is_a_peripheral = 0;
2185 } else if (is_otg_enabled(musb)) {
David Brownell84e250f2009-03-31 12:30:04 -07002186 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002187 musb->g.is_a_peripheral = 1;
2188 } else
2189 WARN_ON(1);
2190
2191 /* start with default limits on VBUS power draw */
2192 (void) musb_gadget_vbus_draw(&musb->g,
2193 is_otg_enabled(musb) ? 8 : 100);
2194}