blob: d34ff408c81571bef89944925a7c542ac378bf0c [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
1556 /* Block idling for at least 1s */
1557 musb_platform_try_idle(musb,
1558 jiffies + msecs_to_jiffies(1 * HZ));
1559
1560 status = 0;
1561 goto done;
1562 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001563 dev_dbg(musb->controller, "Unhandled wake: %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02001564 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001565 goto done;
1566 }
1567
1568 status = 0;
1569
1570 power = musb_readb(mregs, MUSB_POWER);
1571 power |= MUSB_POWER_RESUME;
1572 musb_writeb(mregs, MUSB_POWER, power);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001573 dev_dbg(musb->controller, "issue wakeup\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001574
1575 /* FIXME do this next chunk in a timer callback, no udelay */
1576 mdelay(2);
1577
1578 power = musb_readb(mregs, MUSB_POWER);
1579 power &= ~MUSB_POWER_RESUME;
1580 musb_writeb(mregs, MUSB_POWER, power);
1581done:
1582 spin_unlock_irqrestore(&musb->lock, flags);
1583 return status;
1584}
1585
1586static int
1587musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1588{
1589 struct musb *musb = gadget_to_musb(gadget);
1590
1591 musb->is_self_powered = !!is_selfpowered;
1592 return 0;
1593}
1594
1595static void musb_pullup(struct musb *musb, int is_on)
1596{
1597 u8 power;
1598
1599 power = musb_readb(musb->mregs, MUSB_POWER);
1600 if (is_on)
1601 power |= MUSB_POWER_SOFTCONN;
1602 else
1603 power &= ~MUSB_POWER_SOFTCONN;
1604
1605 /* FIXME if on, HdrcStart; if off, HdrcStop */
1606
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001607 dev_dbg(musb->controller, "gadget %s D+ pullup %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001608 musb->gadget_driver->function, is_on ? "on" : "off");
1609 musb_writeb(musb->mregs, MUSB_POWER, power);
1610}
1611
1612#if 0
1613static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1614{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001615 dev_dbg(musb->controller, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001616
1617 /*
1618 * FIXME iff driver's softconnect flag is set (as it is during probe,
1619 * though that can clear it), just musb_pullup().
1620 */
1621
1622 return -EINVAL;
1623}
1624#endif
1625
1626static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1627{
1628 struct musb *musb = gadget_to_musb(gadget);
1629
David Brownell84e250f2009-03-31 12:30:04 -07001630 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001631 return -EOPNOTSUPP;
David Brownell84e250f2009-03-31 12:30:04 -07001632 return otg_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001633}
1634
1635static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1636{
1637 struct musb *musb = gadget_to_musb(gadget);
1638 unsigned long flags;
1639
1640 is_on = !!is_on;
1641
1642 /* NOTE: this assumes we are sensing vbus; we'd rather
1643 * not pullup unless the B-session is active.
1644 */
1645 spin_lock_irqsave(&musb->lock, flags);
1646 if (is_on != musb->softconnect) {
1647 musb->softconnect = is_on;
1648 musb_pullup(musb, is_on);
1649 }
1650 spin_unlock_irqrestore(&musb->lock, flags);
1651 return 0;
1652}
1653
1654static const struct usb_gadget_ops musb_gadget_operations = {
1655 .get_frame = musb_gadget_get_frame,
1656 .wakeup = musb_gadget_wakeup,
1657 .set_selfpowered = musb_gadget_set_self_powered,
1658 /* .vbus_session = musb_gadget_vbus_session, */
1659 .vbus_draw = musb_gadget_vbus_draw,
1660 .pullup = musb_gadget_pullup,
1661};
1662
1663/* ----------------------------------------------------------------------- */
1664
1665/* Registration */
1666
1667/* Only this registration code "knows" the rule (from USB standards)
1668 * about there being only one external upstream port. It assumes
1669 * all peripheral ports are external...
1670 */
1671static struct musb *the_gadget;
1672
1673static void musb_gadget_release(struct device *dev)
1674{
1675 /* kref_put(WHAT) */
1676 dev_dbg(dev, "%s\n", __func__);
1677}
1678
1679
1680static void __init
1681init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1682{
1683 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1684
1685 memset(ep, 0, sizeof *ep);
1686
1687 ep->current_epnum = epnum;
1688 ep->musb = musb;
1689 ep->hw_ep = hw_ep;
1690 ep->is_in = is_in;
1691
1692 INIT_LIST_HEAD(&ep->req_list);
1693
1694 sprintf(ep->name, "ep%d%s", epnum,
1695 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1696 is_in ? "in" : "out"));
1697 ep->end_point.name = ep->name;
1698 INIT_LIST_HEAD(&ep->end_point.ep_list);
1699 if (!epnum) {
1700 ep->end_point.maxpacket = 64;
1701 ep->end_point.ops = &musb_g_ep0_ops;
1702 musb->g.ep0 = &ep->end_point;
1703 } else {
1704 if (is_in)
1705 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1706 else
1707 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1708 ep->end_point.ops = &musb_ep_ops;
1709 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1710 }
1711}
1712
1713/*
1714 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1715 * to the rest of the driver state.
1716 */
1717static inline void __init musb_g_init_endpoints(struct musb *musb)
1718{
1719 u8 epnum;
1720 struct musb_hw_ep *hw_ep;
1721 unsigned count = 0;
1722
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001723 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001724 INIT_LIST_HEAD(&(musb->g.ep_list));
1725
1726 for (epnum = 0, hw_ep = musb->endpoints;
1727 epnum < musb->nr_endpoints;
1728 epnum++, hw_ep++) {
1729 if (hw_ep->is_shared_fifo /* || !epnum */) {
1730 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1731 count++;
1732 } else {
1733 if (hw_ep->max_packet_sz_tx) {
1734 init_peripheral_ep(musb, &hw_ep->ep_in,
1735 epnum, 1);
1736 count++;
1737 }
1738 if (hw_ep->max_packet_sz_rx) {
1739 init_peripheral_ep(musb, &hw_ep->ep_out,
1740 epnum, 0);
1741 count++;
1742 }
1743 }
1744 }
1745}
1746
1747/* called once during driver setup to initialize and link into
1748 * the driver model; memory is zeroed.
1749 */
1750int __init musb_gadget_setup(struct musb *musb)
1751{
1752 int status;
1753
1754 /* REVISIT minor race: if (erroneously) setting up two
1755 * musb peripherals at the same time, only the bus lock
1756 * is probably held.
1757 */
1758 if (the_gadget)
1759 return -EBUSY;
1760 the_gadget = musb;
1761
1762 musb->g.ops = &musb_gadget_operations;
1763 musb->g.is_dualspeed = 1;
1764 musb->g.speed = USB_SPEED_UNKNOWN;
1765
1766 /* this "gadget" abstracts/virtualizes the controller */
Kay Sievers427c4f32008-11-07 01:52:53 +01001767 dev_set_name(&musb->g.dev, "gadget");
Felipe Balbi550a7372008-07-24 12:27:36 +03001768 musb->g.dev.parent = musb->controller;
1769 musb->g.dev.dma_mask = musb->controller->dma_mask;
1770 musb->g.dev.release = musb_gadget_release;
1771 musb->g.name = musb_driver_name;
1772
1773 if (is_otg_enabled(musb))
1774 musb->g.is_otg = 1;
1775
1776 musb_g_init_endpoints(musb);
1777
1778 musb->is_active = 0;
1779 musb_platform_try_idle(musb, 0);
1780
1781 status = device_register(&musb->g.dev);
Rahul Ruikare2c34042010-10-02 01:35:48 -05001782 if (status != 0) {
1783 put_device(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001784 the_gadget = NULL;
Rahul Ruikare2c34042010-10-02 01:35:48 -05001785 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001786 return status;
1787}
1788
1789void musb_gadget_cleanup(struct musb *musb)
1790{
1791 if (musb != the_gadget)
1792 return;
1793
1794 device_unregister(&musb->g.dev);
1795 the_gadget = NULL;
1796}
1797
1798/*
1799 * Register the gadget driver. Used by gadget drivers when
1800 * registering themselves with the controller.
1801 *
1802 * -EINVAL something went wrong (not driver)
1803 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001804 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001805 *
1806 * @param driver the gadget driver
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001807 * @param bind the driver's bind function
Felipe Balbi550a7372008-07-24 12:27:36 +03001808 * @return <0 if error, 0 if everything is fine
1809 */
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001810int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1811 int (*bind)(struct usb_gadget *))
Felipe Balbi550a7372008-07-24 12:27:36 +03001812{
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001813 struct musb *musb = the_gadget;
1814 unsigned long flags;
1815 int retval = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001816
1817 if (!driver
1818 || driver->speed != USB_SPEED_HIGH
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001819 || !bind || !driver->setup)
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001820 goto err0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001821
1822 /* driver must be initialized to support peripheral mode */
Roel Kluin08e6c972010-02-02 14:47:17 -08001823 if (!musb) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001824 dev_dbg(musb->controller, "no dev??\n");
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001825 retval = -ENODEV;
1826 goto err0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001827 }
1828
Hema HK7acc6192011-02-28 14:19:34 +05301829 pm_runtime_get_sync(musb->controller);
1830
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001831 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001832
1833 if (musb->gadget_driver) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001834 dev_dbg(musb->controller, "%s is already bound to %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001835 musb_driver_name,
1836 musb->gadget_driver->driver.name);
1837 retval = -EBUSY;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001838 goto err0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001839 }
1840
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001841 spin_lock_irqsave(&musb->lock, flags);
1842 musb->gadget_driver = driver;
1843 musb->g.dev.driver = &driver->driver;
1844 driver->driver.bus = NULL;
1845 musb->softconnect = 1;
1846 spin_unlock_irqrestore(&musb->lock, flags);
1847
1848 retval = bind(&musb->g);
1849 if (retval) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001850 dev_dbg(musb->controller, "bind to driver %s failed --> %d\n",
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001851 driver->driver.name, retval);
1852 goto err1;
1853 }
1854
1855 spin_lock_irqsave(&musb->lock, flags);
1856
1857 otg_set_peripheral(musb->xceiv, &musb->g);
1858 musb->xceiv->state = OTG_STATE_B_IDLE;
1859 musb->is_active = 1;
1860
1861 /*
1862 * FIXME this ignores the softconnect flag. Drivers are
1863 * allowed hold the peripheral inactive until for example
1864 * userspace hooks up printer hardware or DSP codecs, so
1865 * hosts only see fully functional devices.
1866 */
1867
1868 if (!is_otg_enabled(musb))
1869 musb_start(musb);
1870
1871 otg_set_peripheral(musb->xceiv, &musb->g);
1872
Felipe Balbi550a7372008-07-24 12:27:36 +03001873 spin_unlock_irqrestore(&musb->lock, flags);
1874
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001875 if (is_otg_enabled(musb)) {
1876 struct usb_hcd *hcd = musb_to_hcd(musb);
Felipe Balbif362a472008-08-04 13:53:52 +03001877
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001878 dev_dbg(musb->controller, "OTG startup...\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001879
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001880 /* REVISIT: funcall to other code, which also
1881 * handles power budgeting ... this way also
1882 * ensures HdrcStart is indirectly called.
Felipe Balbi550a7372008-07-24 12:27:36 +03001883 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001884 retval = usb_add_hcd(musb_to_hcd(musb), -1, 0);
1885 if (retval < 0) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001886 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001887 goto err2;
Felipe Balbi550a7372008-07-24 12:27:36 +03001888 }
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001889
Hema HK5f1e8ce2011-03-17 16:11:58 +05301890 if ((musb->xceiv->last_event == USB_EVENT_ID)
1891 && musb->xceiv->set_vbus)
1892 otg_set_vbus(musb->xceiv, 1);
1893
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001894 hcd->self.uses_pio_for_control = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001895 }
Jarkko Nikulacdefce12011-04-29 16:17:35 +03001896 if (musb->xceiv->last_event == USB_EVENT_NONE)
1897 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001898
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001899 return 0;
1900
1901err2:
1902 if (!is_otg_enabled(musb))
1903 musb_stop(musb);
1904
1905err1:
1906 musb->gadget_driver = NULL;
1907 musb->g.dev.driver = NULL;
1908
1909err0:
Felipe Balbi550a7372008-07-24 12:27:36 +03001910 return retval;
1911}
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02001912EXPORT_SYMBOL(usb_gadget_probe_driver);
Felipe Balbi550a7372008-07-24 12:27:36 +03001913
1914static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1915{
1916 int i;
1917 struct musb_hw_ep *hw_ep;
1918
1919 /* don't disconnect if it's not connected */
1920 if (musb->g.speed == USB_SPEED_UNKNOWN)
1921 driver = NULL;
1922 else
1923 musb->g.speed = USB_SPEED_UNKNOWN;
1924
1925 /* deactivate the hardware */
1926 if (musb->softconnect) {
1927 musb->softconnect = 0;
1928 musb_pullup(musb, 0);
1929 }
1930 musb_stop(musb);
1931
1932 /* killing any outstanding requests will quiesce the driver;
1933 * then report disconnect
1934 */
1935 if (driver) {
1936 for (i = 0, hw_ep = musb->endpoints;
1937 i < musb->nr_endpoints;
1938 i++, hw_ep++) {
1939 musb_ep_select(musb->mregs, i);
1940 if (hw_ep->is_shared_fifo /* || !epnum */) {
1941 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1942 } else {
1943 if (hw_ep->max_packet_sz_tx)
1944 nuke(&hw_ep->ep_in, -ESHUTDOWN);
1945 if (hw_ep->max_packet_sz_rx)
1946 nuke(&hw_ep->ep_out, -ESHUTDOWN);
1947 }
1948 }
1949
1950 spin_unlock(&musb->lock);
1951 driver->disconnect(&musb->g);
1952 spin_lock(&musb->lock);
1953 }
1954}
1955
1956/*
1957 * Unregister the gadget driver. Used by gadget drivers when
1958 * unregistering themselves from the controller.
1959 *
1960 * @param driver the gadget driver to unregister
1961 */
1962int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1963{
Felipe Balbi550a7372008-07-24 12:27:36 +03001964 struct musb *musb = the_gadget;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001965 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03001966
1967 if (!driver || !driver->unbind || !musb)
1968 return -EINVAL;
1969
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001970 if (!musb->gadget_driver)
1971 return -EINVAL;
1972
Hema HK7acc6192011-02-28 14:19:34 +05301973 if (musb->xceiv->last_event == USB_EVENT_NONE)
1974 pm_runtime_get_sync(musb->controller);
1975
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001976 /*
1977 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03001978 * this needs to shut down the OTG engine.
1979 */
1980
1981 spin_lock_irqsave(&musb->lock, flags);
1982
1983#ifdef CONFIG_USB_MUSB_OTG
1984 musb_hnp_stop(musb);
1985#endif
1986
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001987 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001988
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001989 musb->xceiv->state = OTG_STATE_UNDEFINED;
1990 stop_activity(musb, driver);
1991 otg_set_peripheral(musb->xceiv, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001992
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001993 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001994
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001995 spin_unlock_irqrestore(&musb->lock, flags);
1996 driver->unbind(&musb->g);
1997 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03001998
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001999 musb->gadget_driver = NULL;
2000 musb->g.dev.driver = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002001
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002002 musb->is_active = 0;
2003 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002004 spin_unlock_irqrestore(&musb->lock, flags);
2005
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002006 if (is_otg_enabled(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002007 usb_remove_hcd(musb_to_hcd(musb));
2008 /* FIXME we need to be able to register another
2009 * gadget driver here and have everything work;
2010 * that currently misbehaves.
2011 */
2012 }
2013
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002014 if (!is_otg_enabled(musb))
2015 musb_stop(musb);
2016
Hema HK7acc6192011-02-28 14:19:34 +05302017 pm_runtime_put(musb->controller);
2018
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002019 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002020}
2021EXPORT_SYMBOL(usb_gadget_unregister_driver);
2022
2023
2024/* ----------------------------------------------------------------------- */
2025
2026/* lifecycle operations called through plat_uds.c */
2027
2028void musb_g_resume(struct musb *musb)
2029{
2030 musb->is_suspended = 0;
David Brownell84e250f2009-03-31 12:30:04 -07002031 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002032 case OTG_STATE_B_IDLE:
2033 break;
2034 case OTG_STATE_B_WAIT_ACON:
2035 case OTG_STATE_B_PERIPHERAL:
2036 musb->is_active = 1;
2037 if (musb->gadget_driver && musb->gadget_driver->resume) {
2038 spin_unlock(&musb->lock);
2039 musb->gadget_driver->resume(&musb->g);
2040 spin_lock(&musb->lock);
2041 }
2042 break;
2043 default:
2044 WARNING("unhandled RESUME transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002045 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002046 }
2047}
2048
2049/* called when SOF packets stop for 3+ msec */
2050void musb_g_suspend(struct musb *musb)
2051{
2052 u8 devctl;
2053
2054 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002055 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002056
David Brownell84e250f2009-03-31 12:30:04 -07002057 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002058 case OTG_STATE_B_IDLE:
2059 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
David Brownell84e250f2009-03-31 12:30:04 -07002060 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002061 break;
2062 case OTG_STATE_B_PERIPHERAL:
2063 musb->is_suspended = 1;
2064 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2065 spin_unlock(&musb->lock);
2066 musb->gadget_driver->suspend(&musb->g);
2067 spin_lock(&musb->lock);
2068 }
2069 break;
2070 default:
2071 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2072 * A_PERIPHERAL may need care too
2073 */
2074 WARNING("unhandled SUSPEND transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002075 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002076 }
2077}
2078
2079/* Called during SRP */
2080void musb_g_wakeup(struct musb *musb)
2081{
2082 musb_gadget_wakeup(&musb->g);
2083}
2084
2085/* called when VBUS drops below session threshold, and in other cases */
2086void musb_g_disconnect(struct musb *musb)
2087{
2088 void __iomem *mregs = musb->mregs;
2089 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2090
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002091 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002092
2093 /* clear HR */
2094 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2095
2096 /* don't draw vbus until new b-default session */
2097 (void) musb_gadget_vbus_draw(&musb->g, 0);
2098
2099 musb->g.speed = USB_SPEED_UNKNOWN;
2100 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2101 spin_unlock(&musb->lock);
2102 musb->gadget_driver->disconnect(&musb->g);
2103 spin_lock(&musb->lock);
2104 }
2105
David Brownell84e250f2009-03-31 12:30:04 -07002106 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002107 default:
2108#ifdef CONFIG_USB_MUSB_OTG
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002109 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002110 otg_state_string(musb->xceiv->state));
David Brownell84e250f2009-03-31 12:30:04 -07002111 musb->xceiv->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002112 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002113 break;
2114 case OTG_STATE_A_PERIPHERAL:
David Brownell1de00da2009-04-02 10:16:11 -07002115 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
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_B_WAIT_ACON:
2119 case OTG_STATE_B_HOST:
2120#endif
2121 case OTG_STATE_B_PERIPHERAL:
2122 case OTG_STATE_B_IDLE:
David Brownell84e250f2009-03-31 12:30:04 -07002123 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002124 break;
2125 case OTG_STATE_B_SRP_INIT:
2126 break;
2127 }
2128
2129 musb->is_active = 0;
2130}
2131
2132void musb_g_reset(struct musb *musb)
2133__releases(musb->lock)
2134__acquires(musb->lock)
2135{
2136 void __iomem *mbase = musb->mregs;
2137 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2138 u8 power;
2139
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002140 dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002141 (devctl & MUSB_DEVCTL_BDEVICE)
2142 ? "B-Device" : "A-Device",
2143 musb_readb(mbase, MUSB_FADDR),
2144 musb->gadget_driver
2145 ? musb->gadget_driver->driver.name
2146 : NULL
2147 );
2148
2149 /* report disconnect, if we didn't already (flushing EP state) */
2150 if (musb->g.speed != USB_SPEED_UNKNOWN)
2151 musb_g_disconnect(musb);
2152
2153 /* clear HR */
2154 else if (devctl & MUSB_DEVCTL_HR)
2155 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2156
2157
2158 /* what speed did we negotiate? */
2159 power = musb_readb(mbase, MUSB_POWER);
2160 musb->g.speed = (power & MUSB_POWER_HSMODE)
2161 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2162
2163 /* start in USB_STATE_DEFAULT */
2164 musb->is_active = 1;
2165 musb->is_suspended = 0;
2166 MUSB_DEV_MODE(musb);
2167 musb->address = 0;
2168 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2169
2170 musb->may_wakeup = 0;
2171 musb->g.b_hnp_enable = 0;
2172 musb->g.a_alt_hnp_support = 0;
2173 musb->g.a_hnp_support = 0;
2174
2175 /* Normal reset, as B-Device;
2176 * or else after HNP, as A-Device
2177 */
2178 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -07002179 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002180 musb->g.is_a_peripheral = 0;
2181 } else if (is_otg_enabled(musb)) {
David Brownell84e250f2009-03-31 12:30:04 -07002182 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002183 musb->g.is_a_peripheral = 1;
2184 } else
2185 WARN_ON(1);
2186
2187 /* start with default limits on VBUS power draw */
2188 (void) musb_gadget_vbus_draw(&musb->g,
2189 is_otg_enabled(musb) ? 8 : 100);
2190}