musb_gadget.c 58.2 KB
Newer Older
F
Felipe Balbi 已提交
1 2 3 4 5 6
/*
 * MUSB OTG driver peripheral support
 *
 * Copyright 2005 Mentor Graphics Corporation
 * Copyright (C) 2005-2006 by Texas Instruments
 * Copyright (C) 2006-2007 Nokia Corporation
7
 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
F
Felipe Balbi 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/module.h>
#include <linux/smp.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
44
#include <linux/slab.h>
F
Felipe Balbi 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

#include "musb_core.h"


/* MUSB PERIPHERAL status 3-mar-2006:
 *
 * - EP0 seems solid.  It passes both USBCV and usbtest control cases.
 *   Minor glitches:
 *
 *     + remote wakeup to Linux hosts work, but saw USBCV failures;
 *       in one test run (operator error?)
 *     + endpoint halt tests -- in both usbtest and usbcv -- seem
 *       to break when dma is enabled ... is something wrongly
 *       clearing SENDSTALL?
 *
 * - Mass storage behaved ok when last tested.  Network traffic patterns
 *   (with lots of short transfers etc) need retesting; they turn up the
 *   worst cases of the DMA, since short packets are typical but are not
 *   required.
 *
 * - TX/IN
 *     + both pio and dma behave in with network and g_zero tests
 *     + no cppi throughput issues other than no-hw-queueing
 *     + failed with FLAT_REG (DaVinci)
 *     + seems to behave with double buffering, PIO -and- CPPI
 *     + with gadgetfs + AIO, requests got lost?
 *
 * - RX/OUT
 *     + both pio and dma behave in with network and g_zero tests
 *     + dma is slow in typical case (short_not_ok is clear)
 *     + double buffering ok with PIO
 *     + double buffering *FAILS* with CPPI, wrong data bytes sometimes
 *     + request lossage observed with gadgetfs
 *
 * - ISO not tested ... might work, but only weakly isochronous
 *
 * - Gadget driver disabling of softconnect during bind() is ignored; so
 *   drivers can't hold off host requests until userspace is ready.
 *   (Workaround:  they can turn it off later.)
 *
 * - PORTABILITY (assumes PIO works):
 *     + DaVinci, basically works with cppi dma
 *     + OMAP 2430, ditto with mentor dma
 *     + TUSB 6010, platform-specific dma in the works
 */

/* ----------------------------------------------------------------------- */

93 94 95
#define is_buffer_mapped(req) (is_dma_capable() && \
					(req->map_state != UN_MAPPED))

96 97 98
/* Maps the buffer to dma  */

static inline void map_dma_buffer(struct musb_request *request,
99
			struct musb *musb, struct musb_ep *musb_ep)
100
{
101 102 103
	int compatible = true;
	struct dma_controller *dma = musb->dma_controller;

104 105 106 107 108
	request->map_state = UN_MAPPED;

	if (!is_dma_capable() || !musb_ep->dma)
		return;

109 110 111 112 113 114 115 116 117 118 119
	/* Check if DMA engine can handle this request.
	 * DMA code must reject the USB request explicitly.
	 * Default behaviour is to map the request.
	 */
	if (dma->is_compatible)
		compatible = dma->is_compatible(musb_ep->dma,
				musb_ep->packet_sz, request->request.buf,
				request->request.length);
	if (!compatible)
		return;

120 121 122 123 124 125 126 127
	if (request->request.dma == DMA_ADDR_INVALID) {
		request->request.dma = dma_map_single(
				musb->controller,
				request->request.buf,
				request->request.length,
				request->tx
					? DMA_TO_DEVICE
					: DMA_FROM_DEVICE);
128
		request->map_state = MUSB_MAPPED;
129 130 131 132 133 134 135
	} else {
		dma_sync_single_for_device(musb->controller,
			request->request.dma,
			request->request.length,
			request->tx
				? DMA_TO_DEVICE
				: DMA_FROM_DEVICE);
136
		request->map_state = PRE_MAPPED;
137 138 139 140 141 142 143
	}
}

/* Unmap the buffer from dma and maps it back to cpu */
static inline void unmap_dma_buffer(struct musb_request *request,
				struct musb *musb)
{
144 145 146
	if (!is_buffer_mapped(request))
		return;

147
	if (request->request.dma == DMA_ADDR_INVALID) {
148 149
		dev_vdbg(musb->controller,
				"not unmapping a never mapped buffer\n");
150 151
		return;
	}
152
	if (request->map_state == MUSB_MAPPED) {
153 154 155 156 157 158 159
		dma_unmap_single(musb->controller,
			request->request.dma,
			request->request.length,
			request->tx
				? DMA_TO_DEVICE
				: DMA_FROM_DEVICE);
		request->request.dma = DMA_ADDR_INVALID;
160
	} else { /* PRE_MAPPED */
161 162 163 164 165 166 167
		dma_sync_single_for_cpu(musb->controller,
			request->request.dma,
			request->request.length,
			request->tx
				? DMA_TO_DEVICE
				: DMA_FROM_DEVICE);
	}
168
	request->map_state = UN_MAPPED;
169 170
}

F
Felipe Balbi 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/*
 * Immediately complete a request.
 *
 * @param request the request to complete
 * @param status the status to complete the request with
 * Context: controller locked, IRQs blocked.
 */
void musb_g_giveback(
	struct musb_ep		*ep,
	struct usb_request	*request,
	int			status)
__releases(ep->musb->lock)
__acquires(ep->musb->lock)
{
	struct musb_request	*req;
	struct musb		*musb;
	int			busy = ep->busy;

	req = to_musb_request(request);

191
	list_del(&req->list);
F
Felipe Balbi 已提交
192 193 194 195 196 197
	if (req->request.status == -EINPROGRESS)
		req->request.status = status;
	musb = req->musb;

	ep->busy = 1;
	spin_unlock(&musb->lock);
198
	unmap_dma_buffer(req, musb);
F
Felipe Balbi 已提交
199
	if (request->status == 0)
200
		dev_dbg(musb->controller, "%s done request %p,  %d/%d\n",
F
Felipe Balbi 已提交
201 202 203
				ep->end_point.name, request,
				req->request.actual, req->request.length);
	else
204
		dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
F
Felipe Balbi 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
				ep->end_point.name, request,
				req->request.actual, req->request.length,
				request->status);
	req->request.complete(&req->ep->end_point, &req->request);
	spin_lock(&musb->lock);
	ep->busy = busy;
}

/* ----------------------------------------------------------------------- */

/*
 * Abort requests queued to an endpoint using the status. Synchronous.
 * caller locked controller and blocked irqs, and selected this ep.
 */
static void nuke(struct musb_ep *ep, const int status)
{
221
	struct musb		*musb = ep->musb;
F
Felipe Balbi 已提交
222 223 224 225 226 227 228 229
	struct musb_request	*req = NULL;
	void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;

	ep->busy = 1;

	if (is_dma_capable() && ep->dma) {
		struct dma_controller	*c = ep->musb->dma_controller;
		int value;
230

F
Felipe Balbi 已提交
231
		if (ep->is_in) {
232 233 234 235 236
			/*
			 * The programming guide says that we must not clear
			 * the DMAMODE bit before DMAENAB, so we only
			 * clear it in the second write...
			 */
F
Felipe Balbi 已提交
237
			musb_writew(epio, MUSB_TXCSR,
238
				    MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
F
Felipe Balbi 已提交
239 240 241 242 243 244 245 246 247 248
			musb_writew(epio, MUSB_TXCSR,
					0 | MUSB_TXCSR_FLUSHFIFO);
		} else {
			musb_writew(epio, MUSB_RXCSR,
					0 | MUSB_RXCSR_FLUSHFIFO);
			musb_writew(epio, MUSB_RXCSR,
					0 | MUSB_RXCSR_FLUSHFIFO);
		}

		value = c->channel_abort(ep->dma);
249 250
		dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
				ep->name, value);
F
Felipe Balbi 已提交
251 252 253 254
		c->channel_release(ep->dma);
		ep->dma = NULL;
	}

255 256
	while (!list_empty(&ep->req_list)) {
		req = list_first_entry(&ep->req_list, struct musb_request, list);
F
Felipe Balbi 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
		musb_g_giveback(ep, &req->request, status);
	}
}

/* ----------------------------------------------------------------------- */

/* Data transfers - pure PIO, pure DMA, or mixed mode */

/*
 * This assumes the separate CPPI engine is responding to DMA requests
 * from the usb core ... sequenced a bit differently from mentor dma.
 */

static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
{
	if (can_bulk_split(musb, ep->type))
		return ep->hw_ep->max_packet_sz_tx;
	else
		return ep->packet_sz;
}


#ifdef CONFIG_USB_INVENTRA_DMA

/* Peripheral tx (IN) using Mentor DMA works as follows:
	Only mode 0 is used for transfers <= wPktSize,
	mode 1 is used for larger transfers,

	One of the following happens:
	- Host sends IN token which causes an endpoint interrupt
		-> TxAvail
			-> if DMA is currently busy, exit.
			-> if queue is non-empty, txstate().

	- Request is queued by the gadget driver.
		-> if queue was previously empty, txstate()

	txstate()
		-> start
		  /\	-> setup DMA
		  |     (data is transferred to the FIFO, then sent out when
		  |	IN token(s) are recd from Host.
		  |		-> DMA interrupt on completion
		  |		   calls TxAvail.
301
		  |		      -> stop DMA, ~DMAENAB,
F
Felipe Balbi 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		  |		      -> set TxPktRdy for last short pkt or zlp
		  |		      -> Complete Request
		  |		      -> Continue next request (call txstate)
		  |___________________________________|

 * Non-Mentor DMA engines can of course work differently, such as by
 * upleveling from irq-per-packet to irq-per-buffer.
 */

#endif

/*
 * An endpoint is transmitting data. This can be called either from
 * the IRQ routine or from ep.queue() to kickstart a request on an
 * endpoint.
 *
 * Context: controller locked, IRQs blocked, endpoint selected
 */
static void txstate(struct musb *musb, struct musb_request *req)
{
	u8			epnum = req->epnum;
	struct musb_ep		*musb_ep;
	void __iomem		*epio = musb->endpoints[epnum].regs;
	struct usb_request	*request;
	u16			fifo_count = 0, csr;
	int			use_dma = 0;

	musb_ep = req->ep;

331 332 333 334 335 336 337
	/* Check if EP is disabled */
	if (!musb_ep->desc) {
		dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
						musb_ep->end_point.name);
		return;
	}

F
Felipe Balbi 已提交
338 339
	/* we shouldn't get here while DMA is active ... but we do ... */
	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
340
		dev_dbg(musb->controller, "dma pending...\n");
F
Felipe Balbi 已提交
341 342 343 344 345 346 347 348 349 350 351
		return;
	}

	/* read TXCSR before */
	csr = musb_readw(epio, MUSB_TXCSR);

	request = &req->request;
	fifo_count = min(max_ep_writesize(musb, musb_ep),
			(int)(request->length - request->actual));

	if (csr & MUSB_TXCSR_TXPKTRDY) {
352
		dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
F
Felipe Balbi 已提交
353 354 355 356 357
				musb_ep->end_point.name, csr);
		return;
	}

	if (csr & MUSB_TXCSR_P_SENDSTALL) {
358
		dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
F
Felipe Balbi 已提交
359 360 361 362
				musb_ep->end_point.name, csr);
		return;
	}

363
	dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
F
Felipe Balbi 已提交
364 365 366 367
			epnum, musb_ep->packet_sz, fifo_count,
			csr);

#ifndef	CONFIG_MUSB_PIO_ONLY
368
	if (is_buffer_mapped(req)) {
F
Felipe Balbi 已提交
369
		struct dma_controller	*c = musb->dma_controller;
370 371 372 373 374
		size_t request_size;

		/* setup DMA, then program endpoint CSR */
		request_size = min_t(size_t, request->length - request->actual,
					musb_ep->dma->max_len);
F
Felipe Balbi 已提交
375

376
		use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
F
Felipe Balbi 已提交
377 378 379

		/* MUSB_TXCSR_P_ISO is still set correctly */

380
#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
F
Felipe Balbi 已提交
381
		{
382
			if (request_size < musb_ep->packet_sz)
F
Felipe Balbi 已提交
383 384 385 386 387 388 389
				musb_ep->dma->desired_mode = 0;
			else
				musb_ep->dma->desired_mode = 1;

			use_dma = use_dma && c->channel_program(
					musb_ep->dma, musb_ep->packet_sz,
					musb_ep->dma->desired_mode,
C
Cliff Cai 已提交
390
					request->dma + request->actual, request_size);
F
Felipe Balbi 已提交
391 392
			if (use_dma) {
				if (musb_ep->dma->desired_mode == 0) {
393 394 395 396 397 398 399 400 401 402 403
					/*
					 * We must not clear the DMAMODE bit
					 * before the DMAENAB bit -- and the
					 * latter doesn't always get cleared
					 * before we get here...
					 */
					csr &= ~(MUSB_TXCSR_AUTOSET
						| MUSB_TXCSR_DMAENAB);
					musb_writew(epio, MUSB_TXCSR, csr
						| MUSB_TXCSR_P_WZC_BITS);
					csr &= ~MUSB_TXCSR_DMAMODE;
F
Felipe Balbi 已提交
404 405 406
					csr |= (MUSB_TXCSR_DMAENAB |
							MUSB_TXCSR_MODE);
					/* against programming guide */
407 408
				} else {
					csr |= (MUSB_TXCSR_DMAENAB
F
Felipe Balbi 已提交
409 410
							| MUSB_TXCSR_DMAMODE
							| MUSB_TXCSR_MODE);
411 412 413
					if (!musb_ep->hb_mult)
						csr |= MUSB_TXCSR_AUTOSET;
				}
F
Felipe Balbi 已提交
414
				csr &= ~MUSB_TXCSR_P_UNDERRUN;
415

F
Felipe Balbi 已提交
416 417 418 419 420 421
				musb_writew(epio, MUSB_TXCSR, csr);
			}
		}

#elif defined(CONFIG_USB_TI_CPPI_DMA)
		/* program endpoint CSR first, then setup DMA */
422
		csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
423 424
		csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
		       MUSB_TXCSR_MODE;
F
Felipe Balbi 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
		musb_writew(epio, MUSB_TXCSR,
			(MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
				| csr);

		/* ensure writebuffer is empty */
		csr = musb_readw(epio, MUSB_TXCSR);

		/* NOTE host side sets DMAENAB later than this; both are
		 * OK since the transfer dma glue (between CPPI and Mentor
		 * fifos) just tells CPPI it could start.  Data only moves
		 * to the USB TX fifo when both fifos are ready.
		 */

		/* "mode" is irrelevant here; handle terminating ZLPs like
		 * PIO does, since the hardware RNDIS mode seems unreliable
		 * except for the last-packet-is-already-short case.
		 */
		use_dma = use_dma && c->channel_program(
				musb_ep->dma, musb_ep->packet_sz,
				0,
445 446
				request->dma + request->actual,
				request_size);
F
Felipe Balbi 已提交
447 448 449
		if (!use_dma) {
			c->channel_release(musb_ep->dma);
			musb_ep->dma = NULL;
450 451
			csr &= ~MUSB_TXCSR_DMAENAB;
			musb_writew(epio, MUSB_TXCSR, csr);
F
Felipe Balbi 已提交
452 453 454 455 456 457
			/* invariant: prequest->buf is non-null */
		}
#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
		use_dma = use_dma && c->channel_program(
				musb_ep->dma, musb_ep->packet_sz,
				request->zero,
458 459
				request->dma + request->actual,
				request_size);
F
Felipe Balbi 已提交
460 461 462 463 464
#endif
	}
#endif

	if (!use_dma) {
465 466 467 468
		/*
		 * Unmap the dma buffer back to cpu if dma channel
		 * programming fails
		 */
469
		unmap_dma_buffer(req, musb);
470

F
Felipe Balbi 已提交
471 472 473 474 475 476 477 478 479
		musb_write_fifo(musb_ep->hw_ep, fifo_count,
				(u8 *) (request->buf + request->actual));
		request->actual += fifo_count;
		csr |= MUSB_TXCSR_TXPKTRDY;
		csr &= ~MUSB_TXCSR_P_UNDERRUN;
		musb_writew(epio, MUSB_TXCSR, csr);
	}

	/* host may already have the data when this message shows... */
480
	dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
F
Felipe Balbi 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494
			musb_ep->end_point.name, use_dma ? "dma" : "pio",
			request->actual, request->length,
			musb_readw(epio, MUSB_TXCSR),
			fifo_count,
			musb_readw(epio, MUSB_TXMAXP));
}

/*
 * FIFO state update (e.g. data ready).
 * Called from IRQ,  with controller locked.
 */
void musb_g_tx(struct musb *musb, u8 epnum)
{
	u16			csr;
495
	struct musb_request	*req;
F
Felipe Balbi 已提交
496 497 498 499 500 501 502
	struct usb_request	*request;
	u8 __iomem		*mbase = musb->mregs;
	struct musb_ep		*musb_ep = &musb->endpoints[epnum].ep_in;
	void __iomem		*epio = musb->endpoints[epnum].regs;
	struct dma_channel	*dma;

	musb_ep_select(mbase, epnum);
503 504
	req = next_request(musb_ep);
	request = &req->request;
F
Felipe Balbi 已提交
505 506

	csr = musb_readw(epio, MUSB_TXCSR);
507
	dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
F
Felipe Balbi 已提交
508 509

	dma = is_dma_capable() ? musb_ep->dma : NULL;
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

	/*
	 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
	 * probably rates reporting as a host error.
	 */
	if (csr & MUSB_TXCSR_P_SENTSTALL) {
		csr |=	MUSB_TXCSR_P_WZC_BITS;
		csr &= ~MUSB_TXCSR_P_SENTSTALL;
		musb_writew(epio, MUSB_TXCSR, csr);
		return;
	}

	if (csr & MUSB_TXCSR_P_UNDERRUN) {
		/* We NAKed, no big deal... little reason to care. */
		csr |=	 MUSB_TXCSR_P_WZC_BITS;
		csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
		musb_writew(epio, MUSB_TXCSR, csr);
527 528
		dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
				epnum, request);
529 530 531 532 533 534
	}

	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
		/*
		 * SHOULD NOT HAPPEN... has with CPPI though, after
		 * changing SENDSTALL (and other cases); harmless?
F
Felipe Balbi 已提交
535
		 */
536
		dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
537 538
		return;
	}
F
Felipe Balbi 已提交
539

540 541 542 543 544
	if (request) {
		u8	is_dma = 0;

		if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
			is_dma = 1;
F
Felipe Balbi 已提交
545
			csr |= MUSB_TXCSR_P_WZC_BITS;
546
			csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
547
				 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
F
Felipe Balbi 已提交
548
			musb_writew(epio, MUSB_TXCSR, csr);
549 550 551
			/* Ensure writebuffer is empty. */
			csr = musb_readw(epio, MUSB_TXCSR);
			request->actual += musb_ep->dma->actual_len;
552
			dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
553
				epnum, csr, musb_ep->dma->actual_len, request);
F
Felipe Balbi 已提交
554 555
		}

556 557 558 559 560 561 562
		/*
		 * First, maybe a terminating short packet. Some DMA
		 * engines might handle this by themselves.
		 */
		if ((request->zero && request->length
			&& (request->length % musb_ep->packet_sz == 0)
			&& (request->actual == request->length))
563
#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
564 565 566
			|| (is_dma && (!dma->desired_mode ||
				(request->actual &
					(musb_ep->packet_sz - 1))))
F
Felipe Balbi 已提交
567
#endif
568 569 570 571 572 573 574
		) {
			/*
			 * On DMA completion, FIFO may not be
			 * available yet...
			 */
			if (csr & MUSB_TXCSR_TXPKTRDY)
				return;
F
Felipe Balbi 已提交
575

576
			dev_dbg(musb->controller, "sending zero pkt\n");
577 578 579 580 581 582 583
			musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
					| MUSB_TXCSR_TXPKTRDY);
			request->zero = 0;
		}

		if (request->actual == request->length) {
			musb_g_giveback(musb_ep, request, 0);
584 585 586 587 588 589 590 591 592
			/*
			 * In the giveback function the MUSB lock is
			 * released and acquired after sometime. During
			 * this time period the INDEX register could get
			 * changed by the gadget_queue function especially
			 * on SMP systems. Reselect the INDEX to be sure
			 * we are reading/modifying the right registers
			 */
			musb_ep_select(mbase, epnum);
593 594
			req = musb_ep->desc ? next_request(musb_ep) : NULL;
			if (!req) {
595
				dev_dbg(musb->controller, "%s idle now\n",
596 597
					musb_ep->end_point.name);
				return;
598
			}
F
Felipe Balbi 已提交
599 600
		}

601
		txstate(musb, req);
602
	}
F
Felipe Balbi 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
}

/* ------------------------------------------------------------ */

#ifdef CONFIG_USB_INVENTRA_DMA

/* Peripheral rx (OUT) using Mentor DMA works as follows:
	- Only mode 0 is used.

	- Request is queued by the gadget class driver.
		-> if queue was previously empty, rxstate()

	- Host sends OUT token which causes an endpoint interrupt
	  /\      -> RxReady
	  |	      -> if request queued, call rxstate
	  |		/\	-> setup DMA
	  |		|	     -> DMA interrupt on completion
	  |		|		-> RxReady
	  |		|		      -> stop DMA
	  |		|		      -> ack the read
	  |		|		      -> if data recd = max expected
	  |		|				by the request, or host
	  |		|				sent a short packet,
	  |		|				complete the request,
	  |		|				and start the next one.
	  |		|_____________________________________|
	  |					 else just wait for the host
	  |					    to send the next OUT token.
	  |__________________________________________________|

 * Non-Mentor DMA engines can of course work differently.
 */

#endif

/*
 * Context: controller locked, IRQs blocked, endpoint selected
 */
static void rxstate(struct musb *musb, struct musb_request *req)
{
	const u8		epnum = req->epnum;
	struct usb_request	*request = &req->request;
645
	struct musb_ep		*musb_ep;
F
Felipe Balbi 已提交
646
	void __iomem		*epio = musb->endpoints[epnum].regs;
647 648
	unsigned		len = 0;
	u16			fifo_count;
649
	u16			csr = musb_readw(epio, MUSB_RXCSR);
650
	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
651
	u8			use_mode_1;
652 653 654 655 656 657

	if (hw_ep->is_shared_fifo)
		musb_ep = &hw_ep->ep_in;
	else
		musb_ep = &hw_ep->ep_out;

658
	fifo_count = musb_ep->packet_sz;
F
Felipe Balbi 已提交
659

660 661 662 663 664 665 666
	/* Check if EP is disabled */
	if (!musb_ep->desc) {
		dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
						musb_ep->end_point.name);
		return;
	}

667 668
	/* We shouldn't get here while DMA is active, but we do... */
	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
669
		dev_dbg(musb->controller, "DMA pending...\n");
670 671 672 673
		return;
	}

	if (csr & MUSB_RXCSR_P_SENDSTALL) {
674
		dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
675 676 677
		    musb_ep->end_point.name, csr);
		return;
	}
F
Felipe Balbi 已提交
678

679
	if (is_cppi_enabled() && is_buffer_mapped(req)) {
F
Felipe Balbi 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
		struct dma_controller	*c = musb->dma_controller;
		struct dma_channel	*channel = musb_ep->dma;

		/* NOTE:  CPPI won't actually stop advancing the DMA
		 * queue after short packet transfers, so this is almost
		 * always going to run as IRQ-per-packet DMA so that
		 * faults will be handled correctly.
		 */
		if (c->channel_program(channel,
				musb_ep->packet_sz,
				!request->short_not_ok,
				request->dma + request->actual,
				request->length - request->actual)) {

			/* make sure that if an rxpkt arrived after the irq,
			 * the cppi engine will be ready to take it as soon
			 * as DMA is enabled
			 */
			csr &= ~(MUSB_RXCSR_AUTOCLEAR
					| MUSB_RXCSR_DMAMODE);
			csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
			musb_writew(epio, MUSB_RXCSR, csr);
			return;
		}
	}

	if (csr & MUSB_RXCSR_RXPKTRDY) {
707
		fifo_count = musb_readw(epio, MUSB_RXCOUNT);
708 709

		/*
710 711 712
		 * Enable Mode 1 on RX transfers only when short_not_ok flag
		 * is set. Currently short_not_ok flag is set only from
		 * file_storage and f_mass_storage drivers
713
		 */
714 715

		if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
716 717 718 719
			use_mode_1 = 1;
		else
			use_mode_1 = 0;

F
Felipe Balbi 已提交
720 721
		if (request->actual < request->length) {
#ifdef CONFIG_USB_INVENTRA_DMA
722
			if (is_buffer_mapped(req)) {
F
Felipe Balbi 已提交
723 724 725
				struct dma_controller	*c;
				struct dma_channel	*channel;
				int			use_dma = 0;
726
				int transfer_size;
F
Felipe Balbi 已提交
727 728 729 730

				c = musb->dma_controller;
				channel = musb_ep->dma;

731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
	/* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
	 * mode 0 only. So we do not get endpoint interrupts due to DMA
	 * completion. We only get interrupts from DMA controller.
	 *
	 * We could operate in DMA mode 1 if we knew the size of the tranfer
	 * in advance. For mass storage class, request->length = what the host
	 * sends, so that'd work.  But for pretty much everything else,
	 * request->length is routinely more than what the host sends. For
	 * most these gadgets, end of is signified either by a short packet,
	 * or filling the last byte of the buffer.  (Sending extra data in
	 * that last pckate should trigger an overflow fault.)  But in mode 1,
	 * we don't get DMA completion interrupt for short packets.
	 *
	 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
	 * to get endpoint interrupt on every DMA req, but that didn't seem
	 * to work reliably.
	 *
	 * REVISIT an updated g_file_storage can set req->short_not_ok, which
	 * then becomes usable as a runtime "use mode 1" hint...
	 */

752 753
				/* Experimental: Mode1 works with mass storage use cases */
				if (use_mode_1) {
754
					csr |= MUSB_RXCSR_AUTOCLEAR;
755 756 757 758 759 760 761 762 763 764 765 766 767
					musb_writew(epio, MUSB_RXCSR, csr);
					csr |= MUSB_RXCSR_DMAENAB;
					musb_writew(epio, MUSB_RXCSR, csr);

					/*
					 * this special sequence (enabling and then
					 * disabling MUSB_RXCSR_DMAMODE) is required
					 * to get DMAReq to activate
					 */
					musb_writew(epio, MUSB_RXCSR,
						csr | MUSB_RXCSR_DMAMODE);
					musb_writew(epio, MUSB_RXCSR, csr);

768 769 770 771
					transfer_size = min(request->length - request->actual,
							channel->max_len);
					musb_ep->dma->desired_mode = 1;

772 773 774 775 776 777
				} else {
					if (!musb_ep->hb_mult &&
						musb_ep->hw_ep->rx_double_buffered)
						csr |= MUSB_RXCSR_AUTOCLEAR;
					csr |= MUSB_RXCSR_DMAENAB;
					musb_writew(epio, MUSB_RXCSR, csr);
F
Felipe Balbi 已提交
778

779
					transfer_size = min(request->length - request->actual,
780
							(unsigned)fifo_count);
781
					musb_ep->dma->desired_mode = 0;
F
Felipe Balbi 已提交
782 783
				}

784 785 786 787 788 789 790 791
				use_dma = c->channel_program(
						channel,
						musb_ep->packet_sz,
						channel->desired_mode,
						request->dma
						+ request->actual,
						transfer_size);

F
Felipe Balbi 已提交
792
				if (use_dma)
793 794 795 796 797 798 799 800 801 802 803 804 805 806
					return;
			}
#elif defined(CONFIG_USB_UX500_DMA)
			if ((is_buffer_mapped(req)) &&
				(request->actual < request->length)) {

				struct dma_controller *c;
				struct dma_channel *channel;
				int transfer_size = 0;

				c = musb->dma_controller;
				channel = musb_ep->dma;

				/* In case first packet is short */
807 808
				if (fifo_count < musb_ep->packet_sz)
					transfer_size = fifo_count;
809 810 811 812 813 814 815
				else if (request->short_not_ok)
					transfer_size =	min(request->length -
							request->actual,
							channel->max_len);
				else
					transfer_size = min(request->length -
							request->actual,
816
							(unsigned)fifo_count);
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

				csr &= ~MUSB_RXCSR_DMAMODE;
				csr |= (MUSB_RXCSR_DMAENAB |
					MUSB_RXCSR_AUTOCLEAR);

				musb_writew(epio, MUSB_RXCSR, csr);

				if (transfer_size <= musb_ep->packet_sz) {
					musb_ep->dma->desired_mode = 0;
				} else {
					musb_ep->dma->desired_mode = 1;
					/* Mode must be set after DMAENAB */
					csr |= MUSB_RXCSR_DMAMODE;
					musb_writew(epio, MUSB_RXCSR, csr);
				}

				if (c->channel_program(channel,
							musb_ep->packet_sz,
							channel->desired_mode,
							request->dma
							+ request->actual,
							transfer_size))

F
Felipe Balbi 已提交
840 841 842 843
					return;
			}
#endif	/* Mentor's DMA */

844
			len = request->length - request->actual;
845
			dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
F
Felipe Balbi 已提交
846
					musb_ep->end_point.name,
847
					fifo_count, len,
F
Felipe Balbi 已提交
848 849
					musb_ep->packet_sz);

850
			fifo_count = min_t(unsigned, len, fifo_count);
F
Felipe Balbi 已提交
851 852

#ifdef	CONFIG_USB_TUSB_OMAP_DMA
853
			if (tusb_dma_omap() && is_buffer_mapped(req)) {
F
Felipe Balbi 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867
				struct dma_controller *c = musb->dma_controller;
				struct dma_channel *channel = musb_ep->dma;
				u32 dma_addr = request->dma + request->actual;
				int ret;

				ret = c->channel_program(channel,
						musb_ep->packet_sz,
						channel->desired_mode,
						dma_addr,
						fifo_count);
				if (ret)
					return;
			}
#endif
868 869 870 871 872
			/*
			 * Unmap the dma buffer back to cpu if dma channel
			 * programming fails. This buffer is mapped if the
			 * channel allocation is successful
			 */
873
			 if (is_buffer_mapped(req)) {
874 875
				unmap_dma_buffer(req, musb);

876 877
				/*
				 * Clear DMAENAB and AUTOCLEAR for the
878 879
				 * PIO mode transfer
				 */
880
				csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
881 882
				musb_writew(epio, MUSB_RXCSR, csr);
			}
F
Felipe Balbi 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899

			musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
					(request->buf + request->actual));
			request->actual += fifo_count;

			/* REVISIT if we left anything in the fifo, flush
			 * it and report -EOVERFLOW
			 */

			/* ack the read! */
			csr |= MUSB_RXCSR_P_WZC_BITS;
			csr &= ~MUSB_RXCSR_RXPKTRDY;
			musb_writew(epio, MUSB_RXCSR, csr);
		}
	}

	/* reach the end or short packet detected */
900 901
	if (request->actual == request->length ||
	    fifo_count < musb_ep->packet_sz)
F
Felipe Balbi 已提交
902 903 904 905 906 907 908 909 910
		musb_g_giveback(musb_ep, request, 0);
}

/*
 * Data ready for a request; called from IRQ
 */
void musb_g_rx(struct musb *musb, u8 epnum)
{
	u16			csr;
911
	struct musb_request	*req;
F
Felipe Balbi 已提交
912 913
	struct usb_request	*request;
	void __iomem		*mbase = musb->mregs;
914
	struct musb_ep		*musb_ep;
F
Felipe Balbi 已提交
915 916
	void __iomem		*epio = musb->endpoints[epnum].regs;
	struct dma_channel	*dma;
917 918 919 920 921 922
	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];

	if (hw_ep->is_shared_fifo)
		musb_ep = &hw_ep->ep_in;
	else
		musb_ep = &hw_ep->ep_out;
F
Felipe Balbi 已提交
923 924 925

	musb_ep_select(mbase, epnum);

926 927
	req = next_request(musb_ep);
	if (!req)
928
		return;
F
Felipe Balbi 已提交
929

930 931
	request = &req->request;

F
Felipe Balbi 已提交
932 933 934
	csr = musb_readw(epio, MUSB_RXCSR);
	dma = is_dma_capable() ? musb_ep->dma : NULL;

935
	dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
F
Felipe Balbi 已提交
936 937 938 939 940 941
			csr, dma ? " (dma)" : "", request);

	if (csr & MUSB_RXCSR_P_SENTSTALL) {
		csr |= MUSB_RXCSR_P_WZC_BITS;
		csr &= ~MUSB_RXCSR_P_SENTSTALL;
		musb_writew(epio, MUSB_RXCSR, csr);
942
		return;
F
Felipe Balbi 已提交
943 944 945 946 947 948 949
	}

	if (csr & MUSB_RXCSR_P_OVERRUN) {
		/* csr |= MUSB_RXCSR_P_WZC_BITS; */
		csr &= ~MUSB_RXCSR_P_OVERRUN;
		musb_writew(epio, MUSB_RXCSR, csr);

950
		dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
951
		if (request->status == -EINPROGRESS)
F
Felipe Balbi 已提交
952 953 954 955
			request->status = -EOVERFLOW;
	}
	if (csr & MUSB_RXCSR_INCOMPRX) {
		/* REVISIT not necessarily an error */
956
		dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
F
Felipe Balbi 已提交
957 958 959 960
	}

	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
		/* "should not happen"; likely RXPKTRDY pending for DMA */
961
		dev_dbg(musb->controller, "%s busy, csr %04x\n",
F
Felipe Balbi 已提交
962
			musb_ep->end_point.name, csr);
963
		return;
F
Felipe Balbi 已提交
964 965 966 967 968 969 970 971 972 973 974
	}

	if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
		csr &= ~(MUSB_RXCSR_AUTOCLEAR
				| MUSB_RXCSR_DMAENAB
				| MUSB_RXCSR_DMAMODE);
		musb_writew(epio, MUSB_RXCSR,
			MUSB_RXCSR_P_WZC_BITS | csr);

		request->actual += musb_ep->dma->actual_len;

975
		dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
F
Felipe Balbi 已提交
976 977 978 979
			epnum, csr,
			musb_readw(epio, MUSB_RXCSR),
			musb_ep->dma->actual_len, request);

980 981
#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
	defined(CONFIG_USB_UX500_DMA)
F
Felipe Balbi 已提交
982
		/* Autoclear doesn't clear RxPktRdy for short packets */
983
		if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
F
Felipe Balbi 已提交
984 985 986 987 988 989 990 991 992 993
				|| (dma->actual_len
					& (musb_ep->packet_sz - 1))) {
			/* ack the read! */
			csr &= ~MUSB_RXCSR_RXPKTRDY;
			musb_writew(epio, MUSB_RXCSR, csr);
		}

		/* incomplete, and not short? wait for next IN packet */
		if ((request->actual < request->length)
				&& (musb_ep->dma->actual_len
994 995 996 997 998 999 1000 1001
					== musb_ep->packet_sz)) {
			/* In double buffer case, continue to unload fifo if
 			 * there is Rx packet in FIFO.
 			 **/
			csr = musb_readw(epio, MUSB_RXCSR);
			if ((csr & MUSB_RXCSR_RXPKTRDY) &&
				hw_ep->rx_double_buffered)
				goto exit;
1002
			return;
1003
		}
F
Felipe Balbi 已提交
1004 1005
#endif
		musb_g_giveback(musb_ep, request, 0);
1006 1007 1008 1009 1010 1011 1012 1013 1014
		/*
		 * In the giveback function the MUSB lock is
		 * released and acquired after sometime. During
		 * this time period the INDEX register could get
		 * changed by the gadget_queue function especially
		 * on SMP systems. Reselect the INDEX to be sure
		 * we are reading/modifying the right registers
		 */
		musb_ep_select(mbase, epnum);
F
Felipe Balbi 已提交
1015

1016 1017
		req = next_request(musb_ep);
		if (!req)
1018
			return;
F
Felipe Balbi 已提交
1019
	}
1020 1021
#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
	defined(CONFIG_USB_UX500_DMA)
1022
exit:
1023
#endif
1024
	/* Analyze request */
1025
	rxstate(musb, req);
F
Felipe Balbi 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
}

/* ------------------------------------------------------------ */

static int musb_gadget_enable(struct usb_ep *ep,
			const struct usb_endpoint_descriptor *desc)
{
	unsigned long		flags;
	struct musb_ep		*musb_ep;
	struct musb_hw_ep	*hw_ep;
	void __iomem		*regs;
	struct musb		*musb;
	void __iomem	*mbase;
	u8		epnum;
	u16		csr;
	unsigned	tmp;
	int		status = -EINVAL;

	if (!ep || !desc)
		return -EINVAL;

	musb_ep = to_musb_ep(ep);
	hw_ep = musb_ep->hw_ep;
	regs = hw_ep->regs;
	musb = musb_ep->musb;
	mbase = musb->mregs;
	epnum = musb_ep->current_epnum;

	spin_lock_irqsave(&musb->lock, flags);

	if (musb_ep->desc) {
		status = -EBUSY;
		goto fail;
	}
J
Julia Lawall 已提交
1060
	musb_ep->type = usb_endpoint_type(desc);
F
Felipe Balbi 已提交
1061 1062

	/* check direction and (later) maxpacket size against endpoint */
J
Julia Lawall 已提交
1063
	if (usb_endpoint_num(desc) != epnum)
F
Felipe Balbi 已提交
1064 1065 1066
		goto fail;

	/* REVISIT this rules out high bandwidth periodic transfers */
1067
	tmp = usb_endpoint_maxp(desc);
1068 1069 1070 1071 1072 1073 1074 1075 1076
	if (tmp & ~0x07ff) {
		int ok;

		if (usb_endpoint_dir_in(desc))
			ok = musb->hb_iso_tx;
		else
			ok = musb->hb_iso_rx;

		if (!ok) {
1077
			dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
1078 1079 1080 1081 1082 1083 1084 1085 1086
			goto fail;
		}
		musb_ep->hb_mult = (tmp >> 11) & 3;
	} else {
		musb_ep->hb_mult = 0;
	}

	musb_ep->packet_sz = tmp & 0x7ff;
	tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
F
Felipe Balbi 已提交
1087 1088 1089 1090 1091

	/* enable the interrupts for the endpoint, set the endpoint
	 * packet size (or fail), set the mode, clear the fifo
	 */
	musb_ep_select(mbase, epnum);
J
Julia Lawall 已提交
1092
	if (usb_endpoint_dir_in(desc)) {
F
Felipe Balbi 已提交
1093 1094 1095 1096 1097

		if (hw_ep->is_shared_fifo)
			musb_ep->is_in = 1;
		if (!musb_ep->is_in)
			goto fail;
1098 1099

		if (tmp > hw_ep->max_packet_sz_tx) {
1100
			dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
F
Felipe Balbi 已提交
1101
			goto fail;
1102
		}
F
Felipe Balbi 已提交
1103

1104 1105
		musb->intrtxe |= (1 << epnum);
		musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
F
Felipe Balbi 已提交
1106 1107 1108 1109

		/* REVISIT if can_bulk_split(), use by updating "tmp";
		 * likewise high bandwidth periodic tx
		 */
1110
		/* Set TXMAXP with the FIFO size of the endpoint
1111
		 * to disable double buffering mode.
1112
		 */
1113 1114 1115 1116 1117
		if (musb->double_buffer_not_ok)
			musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
		else
			musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
					| (musb_ep->hb_mult << 11));
F
Felipe Balbi 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

		csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
		if (musb_readw(regs, MUSB_TXCSR)
				& MUSB_TXCSR_FIFONOTEMPTY)
			csr |= MUSB_TXCSR_FLUSHFIFO;
		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
			csr |= MUSB_TXCSR_P_ISO;

		/* set twice in case of double buffering */
		musb_writew(regs, MUSB_TXCSR, csr);
		/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
		musb_writew(regs, MUSB_TXCSR, csr);

	} else {

		if (hw_ep->is_shared_fifo)
			musb_ep->is_in = 0;
		if (musb_ep->is_in)
			goto fail;
1137 1138

		if (tmp > hw_ep->max_packet_sz_rx) {
1139
			dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
F
Felipe Balbi 已提交
1140
			goto fail;
1141
		}
F
Felipe Balbi 已提交
1142

1143 1144
		musb->intrrxe |= (1 << epnum);
		musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
F
Felipe Balbi 已提交
1145 1146 1147 1148

		/* REVISIT if can_bulk_combine() use by updating "tmp"
		 * likewise high bandwidth periodic rx
		 */
1149 1150 1151
		/* Set RXMAXP with the FIFO size of the endpoint
		 * to disable double buffering mode.
		 */
1152 1153 1154 1155 1156
		if (musb->double_buffer_not_ok)
			musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
		else
			musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
					| (musb_ep->hb_mult << 11));
F
Felipe Balbi 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

		/* force shared fifo to OUT-only mode */
		if (hw_ep->is_shared_fifo) {
			csr = musb_readw(regs, MUSB_TXCSR);
			csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
			musb_writew(regs, MUSB_TXCSR, csr);
		}

		csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
			csr |= MUSB_RXCSR_P_ISO;
		else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
			csr |= MUSB_RXCSR_DISNYET;

		/* set twice in case of double buffering */
		musb_writew(regs, MUSB_RXCSR, csr);
		musb_writew(regs, MUSB_RXCSR, csr);
	}

	/* NOTE:  all the I/O code _should_ work fine without DMA, in case
	 * for some reason you run out of channels here.
	 */
	if (is_dma_capable() && musb->dma_controller) {
		struct dma_controller	*c = musb->dma_controller;

		musb_ep->dma = c->channel_alloc(c, hw_ep,
				(desc->bEndpointAddress & USB_DIR_IN));
	} else
		musb_ep->dma = NULL;

	musb_ep->desc = desc;
	musb_ep->busy = 0;
1189
	musb_ep->wedged = 0;
F
Felipe Balbi 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	status = 0;

	pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
			musb_driver_name, musb_ep->end_point.name,
			({ char *s; switch (musb_ep->type) {
			case USB_ENDPOINT_XFER_BULK:	s = "bulk"; break;
			case USB_ENDPOINT_XFER_INT:	s = "int"; break;
			default:			s = "iso"; break;
			}; s; }),
			musb_ep->is_in ? "IN" : "OUT",
			musb_ep->dma ? "dma, " : "",
			musb_ep->packet_sz);

	schedule_work(&musb->irq_work);

fail:
	spin_unlock_irqrestore(&musb->lock, flags);
	return status;
}

/*
 * Disable an endpoint flushing all requests queued.
 */
static int musb_gadget_disable(struct usb_ep *ep)
{
	unsigned long	flags;
	struct musb	*musb;
	u8		epnum;
	struct musb_ep	*musb_ep;
	void __iomem	*epio;
	int		status = 0;

	musb_ep = to_musb_ep(ep);
	musb = musb_ep->musb;
	epnum = musb_ep->current_epnum;
	epio = musb->endpoints[epnum].regs;

	spin_lock_irqsave(&musb->lock, flags);
	musb_ep_select(musb->mregs, epnum);

	/* zero the endpoint sizes */
	if (musb_ep->is_in) {
1232 1233
		musb->intrtxe &= ~(1 << epnum);
		musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
F
Felipe Balbi 已提交
1234 1235
		musb_writew(epio, MUSB_TXMAXP, 0);
	} else {
1236 1237
		musb->intrrxe &= ~(1 << epnum);
		musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
F
Felipe Balbi 已提交
1238 1239 1240 1241
		musb_writew(epio, MUSB_RXMAXP, 0);
	}

	musb_ep->desc = NULL;
1242
	musb_ep->end_point.desc = NULL;
F
Felipe Balbi 已提交
1243 1244 1245 1246 1247 1248 1249 1250

	/* abort all pending DMA and requests */
	nuke(musb_ep, -ESHUTDOWN);

	schedule_work(&musb->irq_work);

	spin_unlock_irqrestore(&(musb->lock), flags);

1251
	dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
F
Felipe Balbi 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

	return status;
}

/*
 * Allocate a request for an endpoint.
 * Reused by ep0 code.
 */
struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
{
	struct musb_ep		*musb_ep = to_musb_ep(ep);
1263
	struct musb		*musb = musb_ep->musb;
F
Felipe Balbi 已提交
1264 1265 1266
	struct musb_request	*request = NULL;

	request = kzalloc(sizeof *request, gfp_flags);
1267
	if (!request) {
1268
		dev_dbg(musb->controller, "not enough memory\n");
1269
		return NULL;
F
Felipe Balbi 已提交
1270 1271
	}

1272 1273 1274 1275
	request->request.dma = DMA_ADDR_INVALID;
	request->epnum = musb_ep->current_epnum;
	request->ep = musb_ep;

F
Felipe Balbi 已提交
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
	return &request->request;
}

/*
 * Free a request
 * Reused by ep0 code.
 */
void musb_free_request(struct usb_ep *ep, struct usb_request *req)
{
	kfree(to_musb_request(req));
}

static LIST_HEAD(buffers);

struct free_record {
	struct list_head	list;
	struct device		*dev;
	unsigned		bytes;
	dma_addr_t		dma;
};

/*
 * Context: controller locked, IRQs blocked.
 */
1300
void musb_ep_restart(struct musb *musb, struct musb_request *req)
F
Felipe Balbi 已提交
1301
{
1302
	dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
F
Felipe Balbi 已提交
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
		req->tx ? "TX/IN" : "RX/OUT",
		&req->request, req->request.length, req->epnum);

	musb_ep_select(musb->mregs, req->epnum);
	if (req->tx)
		txstate(musb, req);
	else
		rxstate(musb, req);
}

static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
			gfp_t gfp_flags)
{
	struct musb_ep		*musb_ep;
	struct musb_request	*request;
	struct musb		*musb;
	int			status = 0;
	unsigned long		lockflags;

	if (!ep || !req)
		return -EINVAL;
	if (!req->buf)
		return -ENODATA;

	musb_ep = to_musb_ep(ep);
	musb = musb_ep->musb;

	request = to_musb_request(req);
	request->musb = musb;

	if (request->ep != musb_ep)
		return -EINVAL;

1336
	dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
F
Felipe Balbi 已提交
1337 1338 1339 1340 1341 1342 1343

	/* request is mine now... */
	request->request.actual = 0;
	request->request.status = -EINPROGRESS;
	request->epnum = musb_ep->current_epnum;
	request->tx = musb_ep->is_in;

1344
	map_dma_buffer(request, musb, musb_ep);
F
Felipe Balbi 已提交
1345 1346 1347 1348 1349

	spin_lock_irqsave(&musb->lock, lockflags);

	/* don't queue if the ep is down */
	if (!musb_ep->desc) {
1350
		dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
F
Felipe Balbi 已提交
1351 1352 1353 1354 1355 1356
				req, ep->name, "disabled");
		status = -ESHUTDOWN;
		goto cleanup;
	}

	/* add request to the list */
1357
	list_add_tail(&request->list, &musb_ep->req_list);
F
Felipe Balbi 已提交
1358 1359

	/* it this is the head of the queue, start i/o ... */
1360
	if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
F
Felipe Balbi 已提交
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
		musb_ep_restart(musb, request);

cleanup:
	spin_unlock_irqrestore(&musb->lock, lockflags);
	return status;
}

static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
{
	struct musb_ep		*musb_ep = to_musb_ep(ep);
1371 1372
	struct musb_request	*req = to_musb_request(request);
	struct musb_request	*r;
F
Felipe Balbi 已提交
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
	unsigned long		flags;
	int			status = 0;
	struct musb		*musb = musb_ep->musb;

	if (!ep || !request || to_musb_request(request)->ep != musb_ep)
		return -EINVAL;

	spin_lock_irqsave(&musb->lock, flags);

	list_for_each_entry(r, &musb_ep->req_list, list) {
1383
		if (r == req)
F
Felipe Balbi 已提交
1384 1385
			break;
	}
1386
	if (r != req) {
1387
		dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
F
Felipe Balbi 已提交
1388 1389 1390 1391 1392
		status = -EINVAL;
		goto done;
	}

	/* if the hardware doesn't have the request, easy ... */
1393
	if (musb_ep->req_list.next != &req->list || musb_ep->busy)
F
Felipe Balbi 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
		musb_g_giveback(musb_ep, request, -ECONNRESET);

	/* ... else abort the dma transfer ... */
	else if (is_dma_capable() && musb_ep->dma) {
		struct dma_controller	*c = musb->dma_controller;

		musb_ep_select(musb->mregs, musb_ep->current_epnum);
		if (c->channel_abort)
			status = c->channel_abort(musb_ep->dma);
		else
			status = -EBUSY;
		if (status == 0)
			musb_g_giveback(musb_ep, request, -ECONNRESET);
	} else {
		/* NOTE: by sticking to easily tested hardware/driver states,
		 * we leave counting of in-flight packets imprecise.
		 */
		musb_g_giveback(musb_ep, request, -ECONNRESET);
	}

done:
	spin_unlock_irqrestore(&musb->lock, flags);
	return status;
}

/*
 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
 * data but will queue requests.
 *
 * exported to ep0 code
 */
1425
static int musb_gadget_set_halt(struct usb_ep *ep, int value)
F
Felipe Balbi 已提交
1426 1427 1428 1429 1430 1431 1432 1433
{
	struct musb_ep		*musb_ep = to_musb_ep(ep);
	u8			epnum = musb_ep->current_epnum;
	struct musb		*musb = musb_ep->musb;
	void __iomem		*epio = musb->endpoints[epnum].regs;
	void __iomem		*mbase;
	unsigned long		flags;
	u16			csr;
1434
	struct musb_request	*request;
F
Felipe Balbi 已提交
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
	int			status = 0;

	if (!ep)
		return -EINVAL;
	mbase = musb->mregs;

	spin_lock_irqsave(&musb->lock, flags);

	if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
		status = -EINVAL;
		goto done;
	}

	musb_ep_select(mbase, epnum);

1450
	request = next_request(musb_ep);
1451 1452
	if (value) {
		if (request) {
1453
			dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
1454 1455 1456 1457 1458 1459 1460 1461
			    ep->name);
			status = -EAGAIN;
			goto done;
		}
		/* Cannot portably stall with non-empty FIFO */
		if (musb_ep->is_in) {
			csr = musb_readw(epio, MUSB_TXCSR);
			if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1462
				dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
1463 1464 1465
				status = -EAGAIN;
				goto done;
			}
F
Felipe Balbi 已提交
1466
		}
1467 1468
	} else
		musb_ep->wedged = 0;
F
Felipe Balbi 已提交
1469 1470

	/* set/clear the stall and toggle bits */
1471
	dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
F
Felipe Balbi 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
	if (musb_ep->is_in) {
		csr = musb_readw(epio, MUSB_TXCSR);
		csr |= MUSB_TXCSR_P_WZC_BITS
			| MUSB_TXCSR_CLRDATATOG;
		if (value)
			csr |= MUSB_TXCSR_P_SENDSTALL;
		else
			csr &= ~(MUSB_TXCSR_P_SENDSTALL
				| MUSB_TXCSR_P_SENTSTALL);
		csr &= ~MUSB_TXCSR_TXPKTRDY;
		musb_writew(epio, MUSB_TXCSR, csr);
	} else {
		csr = musb_readw(epio, MUSB_RXCSR);
		csr |= MUSB_RXCSR_P_WZC_BITS
			| MUSB_RXCSR_FLUSHFIFO
			| MUSB_RXCSR_CLRDATATOG;
		if (value)
			csr |= MUSB_RXCSR_P_SENDSTALL;
		else
			csr &= ~(MUSB_RXCSR_P_SENDSTALL
				| MUSB_RXCSR_P_SENTSTALL);
		musb_writew(epio, MUSB_RXCSR, csr);
	}

	/* maybe start the first request in the queue */
	if (!musb_ep->busy && !value && request) {
1498
		dev_dbg(musb->controller, "restarting the request\n");
F
Felipe Balbi 已提交
1499 1500 1501
		musb_ep_restart(musb, request);
	}

1502
done:
F
Felipe Balbi 已提交
1503 1504 1505 1506
	spin_unlock_irqrestore(&musb->lock, flags);
	return status;
}

1507 1508 1509
/*
 * Sets the halt feature with the clear requests ignored
 */
1510
static int musb_gadget_set_wedge(struct usb_ep *ep)
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
{
	struct musb_ep		*musb_ep = to_musb_ep(ep);

	if (!ep)
		return -EINVAL;

	musb_ep->wedged = 1;

	return usb_ep_set_halt(ep);
}

F
Felipe Balbi 已提交
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
static int musb_gadget_fifo_status(struct usb_ep *ep)
{
	struct musb_ep		*musb_ep = to_musb_ep(ep);
	void __iomem		*epio = musb_ep->hw_ep->regs;
	int			retval = -EINVAL;

	if (musb_ep->desc && !musb_ep->is_in) {
		struct musb		*musb = musb_ep->musb;
		int			epnum = musb_ep->current_epnum;
		void __iomem		*mbase = musb->mregs;
		unsigned long		flags;

		spin_lock_irqsave(&musb->lock, flags);

		musb_ep_select(mbase, epnum);
		/* FIXME return zero unless RXPKTRDY is set */
		retval = musb_readw(epio, MUSB_RXCOUNT);

		spin_unlock_irqrestore(&musb->lock, flags);
	}
	return retval;
}

static void musb_gadget_fifo_flush(struct usb_ep *ep)
{
	struct musb_ep	*musb_ep = to_musb_ep(ep);
	struct musb	*musb = musb_ep->musb;
	u8		epnum = musb_ep->current_epnum;
	void __iomem	*epio = musb->endpoints[epnum].regs;
	void __iomem	*mbase;
	unsigned long	flags;
1553
	u16		csr;
F
Felipe Balbi 已提交
1554 1555 1556 1557 1558 1559 1560

	mbase = musb->mregs;

	spin_lock_irqsave(&musb->lock, flags);
	musb_ep_select(mbase, (u8) epnum);

	/* disable interrupts */
1561
	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
F
Felipe Balbi 已提交
1562 1563 1564 1565 1566

	if (musb_ep->is_in) {
		csr = musb_readw(epio, MUSB_TXCSR);
		if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
			csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1567 1568 1569 1570 1571 1572
			/*
			 * Setting both TXPKTRDY and FLUSHFIFO makes controller
			 * to interrupt current FIFO loading, but not flushing
			 * the already loaded ones.
			 */
			csr &= ~MUSB_TXCSR_TXPKTRDY;
F
Felipe Balbi 已提交
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
			musb_writew(epio, MUSB_TXCSR, csr);
			/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
			musb_writew(epio, MUSB_TXCSR, csr);
		}
	} else {
		csr = musb_readw(epio, MUSB_RXCSR);
		csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
		musb_writew(epio, MUSB_RXCSR, csr);
		musb_writew(epio, MUSB_RXCSR, csr);
	}

	/* re-enable interrupt */
1585
	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
F
Felipe Balbi 已提交
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	spin_unlock_irqrestore(&musb->lock, flags);
}

static const struct usb_ep_ops musb_ep_ops = {
	.enable		= musb_gadget_enable,
	.disable	= musb_gadget_disable,
	.alloc_request	= musb_alloc_request,
	.free_request	= musb_free_request,
	.queue		= musb_gadget_queue,
	.dequeue	= musb_gadget_dequeue,
	.set_halt	= musb_gadget_set_halt,
1597
	.set_wedge	= musb_gadget_set_wedge,
F
Felipe Balbi 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
	.fifo_status	= musb_gadget_fifo_status,
	.fifo_flush	= musb_gadget_fifo_flush
};

/* ----------------------------------------------------------------------- */

static int musb_gadget_get_frame(struct usb_gadget *gadget)
{
	struct musb	*musb = gadget_to_musb(gadget);

	return (int)musb_readw(musb->mregs, MUSB_FRAME);
}

static int musb_gadget_wakeup(struct usb_gadget *gadget)
{
	struct musb	*musb = gadget_to_musb(gadget);
	void __iomem	*mregs = musb->mregs;
	unsigned long	flags;
	int		status = -EINVAL;
	u8		power, devctl;
	int		retries;

	spin_lock_irqsave(&musb->lock, flags);

1622
	switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
	case OTG_STATE_B_PERIPHERAL:
		/* NOTE:  OTG state machine doesn't include B_SUSPENDED;
		 * that's part of the standard usb 1.1 state machine, and
		 * doesn't affect OTG transitions.
		 */
		if (musb->may_wakeup && musb->is_suspended)
			break;
		goto done;
	case OTG_STATE_B_IDLE:
		/* Start SRP ... OTG not required. */
		devctl = musb_readb(mregs, MUSB_DEVCTL);
1634
		dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
F
Felipe Balbi 已提交
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
		devctl |= MUSB_DEVCTL_SESSION;
		musb_writeb(mregs, MUSB_DEVCTL, devctl);
		devctl = musb_readb(mregs, MUSB_DEVCTL);
		retries = 100;
		while (!(devctl & MUSB_DEVCTL_SESSION)) {
			devctl = musb_readb(mregs, MUSB_DEVCTL);
			if (retries-- < 1)
				break;
		}
		retries = 10000;
		while (devctl & MUSB_DEVCTL_SESSION) {
			devctl = musb_readb(mregs, MUSB_DEVCTL);
			if (retries-- < 1)
				break;
		}

1651
		spin_unlock_irqrestore(&musb->lock, flags);
1652
		otg_start_srp(musb->xceiv->otg);
1653 1654
		spin_lock_irqsave(&musb->lock, flags);

F
Felipe Balbi 已提交
1655 1656 1657 1658 1659 1660 1661
		/* Block idling for at least 1s */
		musb_platform_try_idle(musb,
			jiffies + msecs_to_jiffies(1 * HZ));

		status = 0;
		goto done;
	default:
1662
		dev_dbg(musb->controller, "Unhandled wake: %s\n",
1663
			otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
1664 1665 1666 1667 1668 1669 1670 1671
		goto done;
	}

	status = 0;

	power = musb_readb(mregs, MUSB_POWER);
	power |= MUSB_POWER_RESUME;
	musb_writeb(mregs, MUSB_POWER, power);
1672
	dev_dbg(musb->controller, "issue wakeup\n");
F
Felipe Balbi 已提交
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705

	/* FIXME do this next chunk in a timer callback, no udelay */
	mdelay(2);

	power = musb_readb(mregs, MUSB_POWER);
	power &= ~MUSB_POWER_RESUME;
	musb_writeb(mregs, MUSB_POWER, power);
done:
	spin_unlock_irqrestore(&musb->lock, flags);
	return status;
}

static int
musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
{
	struct musb	*musb = gadget_to_musb(gadget);

	musb->is_self_powered = !!is_selfpowered;
	return 0;
}

static void musb_pullup(struct musb *musb, int is_on)
{
	u8 power;

	power = musb_readb(musb->mregs, MUSB_POWER);
	if (is_on)
		power |= MUSB_POWER_SOFTCONN;
	else
		power &= ~MUSB_POWER_SOFTCONN;

	/* FIXME if on, HdrcStart; if off, HdrcStop */

1706 1707
	dev_dbg(musb->controller, "gadget D+ pullup %s\n",
		is_on ? "on" : "off");
F
Felipe Balbi 已提交
1708 1709 1710 1711 1712 1713
	musb_writeb(musb->mregs, MUSB_POWER, power);
}

#if 0
static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
{
1714
	dev_dbg(musb->controller, "<= %s =>\n", __func__);
F
Felipe Balbi 已提交
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728

	/*
	 * FIXME iff driver's softconnect flag is set (as it is during probe,
	 * though that can clear it), just musb_pullup().
	 */

	return -EINVAL;
}
#endif

static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
{
	struct musb	*musb = gadget_to_musb(gadget);

1729
	if (!musb->xceiv->set_power)
F
Felipe Balbi 已提交
1730
		return -EOPNOTSUPP;
1731
	return usb_phy_set_power(musb->xceiv, mA);
F
Felipe Balbi 已提交
1732 1733 1734 1735 1736 1737 1738 1739 1740
}

static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
{
	struct musb	*musb = gadget_to_musb(gadget);
	unsigned long	flags;

	is_on = !!is_on;

1741 1742
	pm_runtime_get_sync(musb->controller);

F
Felipe Balbi 已提交
1743 1744 1745 1746 1747 1748 1749 1750 1751
	/* NOTE: this assumes we are sensing vbus; we'd rather
	 * not pullup unless the B-session is active.
	 */
	spin_lock_irqsave(&musb->lock, flags);
	if (is_on != musb->softconnect) {
		musb->softconnect = is_on;
		musb_pullup(musb, is_on);
	}
	spin_unlock_irqrestore(&musb->lock, flags);
1752 1753 1754

	pm_runtime_put(musb->controller);

F
Felipe Balbi 已提交
1755 1756 1757
	return 0;
}

1758 1759 1760 1761
static int musb_gadget_start(struct usb_gadget *g,
		struct usb_gadget_driver *driver);
static int musb_gadget_stop(struct usb_gadget *g,
		struct usb_gadget_driver *driver);
1762

F
Felipe Balbi 已提交
1763 1764 1765 1766 1767 1768 1769
static const struct usb_gadget_ops musb_gadget_operations = {
	.get_frame		= musb_gadget_get_frame,
	.wakeup			= musb_gadget_wakeup,
	.set_selfpowered	= musb_gadget_set_self_powered,
	/* .vbus_session		= musb_gadget_vbus_session, */
	.vbus_draw		= musb_gadget_vbus_draw,
	.pullup			= musb_gadget_pullup,
1770 1771
	.udc_start		= musb_gadget_start,
	.udc_stop		= musb_gadget_stop,
F
Felipe Balbi 已提交
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789
};

/* ----------------------------------------------------------------------- */

/* Registration */

/* Only this registration code "knows" the rule (from USB standards)
 * about there being only one external upstream port.  It assumes
 * all peripheral ports are external...
 */

static void musb_gadget_release(struct device *dev)
{
	/* kref_put(WHAT) */
	dev_dbg(dev, "%s\n", __func__);
}


1790
static void __devinit
F
Felipe Balbi 已提交
1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
{
	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;

	memset(ep, 0, sizeof *ep);

	ep->current_epnum = epnum;
	ep->musb = musb;
	ep->hw_ep = hw_ep;
	ep->is_in = is_in;

	INIT_LIST_HEAD(&ep->req_list);

	sprintf(ep->name, "ep%d%s", epnum,
			(!epnum || hw_ep->is_shared_fifo) ? "" : (
				is_in ? "in" : "out"));
	ep->end_point.name = ep->name;
	INIT_LIST_HEAD(&ep->end_point.ep_list);
	if (!epnum) {
		ep->end_point.maxpacket = 64;
		ep->end_point.ops = &musb_g_ep0_ops;
		musb->g.ep0 = &ep->end_point;
	} else {
		if (is_in)
			ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
		else
			ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
		ep->end_point.ops = &musb_ep_ops;
		list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
	}
}

/*
 * Initialize the endpoints exposed to peripheral drivers, with backlinks
 * to the rest of the driver state.
 */
1827
static inline void __devinit musb_g_init_endpoints(struct musb *musb)
F
Felipe Balbi 已提交
1828 1829 1830 1831 1832
{
	u8			epnum;
	struct musb_hw_ep	*hw_ep;
	unsigned		count = 0;

1833
	/* initialize endpoint list just once */
F
Felipe Balbi 已提交
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859
	INIT_LIST_HEAD(&(musb->g.ep_list));

	for (epnum = 0, hw_ep = musb->endpoints;
			epnum < musb->nr_endpoints;
			epnum++, hw_ep++) {
		if (hw_ep->is_shared_fifo /* || !epnum */) {
			init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
			count++;
		} else {
			if (hw_ep->max_packet_sz_tx) {
				init_peripheral_ep(musb, &hw_ep->ep_in,
							epnum, 1);
				count++;
			}
			if (hw_ep->max_packet_sz_rx) {
				init_peripheral_ep(musb, &hw_ep->ep_out,
							epnum, 0);
				count++;
			}
		}
	}
}

/* called once during driver setup to initialize and link into
 * the driver model; memory is zeroed.
 */
1860
int __devinit musb_gadget_setup(struct musb *musb)
F
Felipe Balbi 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869
{
	int status;

	/* REVISIT minor race:  if (erroneously) setting up two
	 * musb peripherals at the same time, only the bus lock
	 * is probably held.
	 */

	musb->g.ops = &musb_gadget_operations;
1870
	musb->g.max_speed = USB_SPEED_HIGH;
F
Felipe Balbi 已提交
1871 1872 1873
	musb->g.speed = USB_SPEED_UNKNOWN;

	/* this "gadget" abstracts/virtualizes the controller */
1874
	dev_set_name(&musb->g.dev, "gadget");
F
Felipe Balbi 已提交
1875 1876 1877 1878 1879
	musb->g.dev.parent = musb->controller;
	musb->g.dev.dma_mask = musb->controller->dma_mask;
	musb->g.dev.release = musb_gadget_release;
	musb->g.name = musb_driver_name;

1880
	musb->g.is_otg = 1;
F
Felipe Balbi 已提交
1881 1882 1883 1884 1885 1886 1887

	musb_g_init_endpoints(musb);

	musb->is_active = 0;
	musb_platform_try_idle(musb, 0);

	status = device_register(&musb->g.dev);
1888 1889
	if (status != 0) {
		put_device(&musb->g.dev);
1890
		return status;
1891
	}
1892 1893 1894 1895 1896 1897
	status = usb_add_gadget_udc(musb->controller, &musb->g);
	if (status)
		goto err;

	return 0;
err:
1898
	musb->g.dev.parent = NULL;
1899
	device_unregister(&musb->g.dev);
F
Felipe Balbi 已提交
1900 1901 1902 1903 1904
	return status;
}

void musb_gadget_cleanup(struct musb *musb)
{
1905
	usb_del_gadget_udc(&musb->g);
1906 1907
	if (musb->g.dev.parent)
		device_unregister(&musb->g.dev);
F
Felipe Balbi 已提交
1908 1909 1910 1911 1912 1913 1914 1915
}

/*
 * Register the gadget driver. Used by gadget drivers when
 * registering themselves with the controller.
 *
 * -EINVAL something went wrong (not driver)
 * -EBUSY another gadget is already using the controller
1916
 * -ENOMEM no memory to perform the operation
F
Felipe Balbi 已提交
1917 1918 1919 1920
 *
 * @param driver the gadget driver
 * @return <0 if error, 0 if everything is fine
 */
1921 1922
static int musb_gadget_start(struct usb_gadget *g,
		struct usb_gadget_driver *driver)
F
Felipe Balbi 已提交
1923
{
1924
	struct musb		*musb = gadget_to_musb(g);
1925
	struct usb_otg		*otg = musb->xceiv->otg;
1926
	struct usb_hcd		*hcd = musb_to_hcd(musb);
1927
	unsigned long		flags;
1928
	int			retval = 0;
F
Felipe Balbi 已提交
1929

1930 1931 1932 1933
	if (driver->max_speed < USB_SPEED_HIGH) {
		retval = -EINVAL;
		goto err;
	}
F
Felipe Balbi 已提交
1934

1935 1936
	pm_runtime_get_sync(musb->controller);

1937
	dev_dbg(musb->controller, "registering driver %s\n", driver->function);
F
Felipe Balbi 已提交
1938

1939
	musb->softconnect = 0;
1940
	musb->gadget_driver = driver;
F
Felipe Balbi 已提交
1941

1942
	spin_lock_irqsave(&musb->lock, flags);
1943
	musb->is_active = 1;
F
Felipe Balbi 已提交
1944

1945
	otg_set_peripheral(otg, &musb->g);
1946 1947
	musb->xceiv->state = OTG_STATE_B_IDLE;
	spin_unlock_irqrestore(&musb->lock, flags);
F
Felipe Balbi 已提交
1948

1949 1950 1951 1952 1953 1954 1955 1956 1957
	/* REVISIT:  funcall to other code, which also
	 * handles power budgeting ... this way also
	 * ensures HdrcStart is indirectly called.
	 */
	retval = usb_add_hcd(hcd, 0, 0);
	if (retval < 0) {
		dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
		goto err;
	}
F
Felipe Balbi 已提交
1958

1959 1960 1961
	if ((musb->xceiv->last_event == USB_EVENT_ID)
				&& otg->set_vbus)
		otg_set_vbus(otg, 1);
1962

1963
	hcd->self.uses_pio_for_control = 1;
H
Hema HK 已提交
1964

1965 1966
	if (musb->xceiv->last_event == USB_EVENT_NONE)
		pm_runtime_put(musb->controller);
F
Felipe Balbi 已提交
1967

1968 1969
	return 0;

1970
err:
F
Felipe Balbi 已提交
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
	return retval;
}

static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
{
	int			i;
	struct musb_hw_ep	*hw_ep;

	/* don't disconnect if it's not connected */
	if (musb->g.speed == USB_SPEED_UNKNOWN)
		driver = NULL;
	else
		musb->g.speed = USB_SPEED_UNKNOWN;

	/* deactivate the hardware */
	if (musb->softconnect) {
		musb->softconnect = 0;
		musb_pullup(musb, 0);
	}
	musb_stop(musb);

	/* killing any outstanding requests will quiesce the driver;
	 * then report disconnect
	 */
	if (driver) {
		for (i = 0, hw_ep = musb->endpoints;
				i < musb->nr_endpoints;
				i++, hw_ep++) {
			musb_ep_select(musb->mregs, i);
			if (hw_ep->is_shared_fifo /* || !epnum */) {
				nuke(&hw_ep->ep_in, -ESHUTDOWN);
			} else {
				if (hw_ep->max_packet_sz_tx)
					nuke(&hw_ep->ep_in, -ESHUTDOWN);
				if (hw_ep->max_packet_sz_rx)
					nuke(&hw_ep->ep_out, -ESHUTDOWN);
			}
		}
	}
}

/*
 * Unregister the gadget driver. Used by gadget drivers when
 * unregistering themselves from the controller.
 *
 * @param driver the gadget driver to unregister
 */
2018 2019
static int musb_gadget_stop(struct usb_gadget *g,
		struct usb_gadget_driver *driver)
F
Felipe Balbi 已提交
2020
{
2021
	struct musb	*musb = gadget_to_musb(g);
2022
	unsigned long	flags;
F
Felipe Balbi 已提交
2023

2024 2025 2026
	if (musb->xceiv->last_event == USB_EVENT_NONE)
		pm_runtime_get_sync(musb->controller);

2027 2028
	/*
	 * REVISIT always use otg_set_peripheral() here too;
F
Felipe Balbi 已提交
2029 2030 2031 2032 2033 2034 2035
	 * this needs to shut down the OTG engine.
	 */

	spin_lock_irqsave(&musb->lock, flags);

	musb_hnp_stop(musb);

2036
	(void) musb_gadget_vbus_draw(&musb->g, 0);
F
Felipe Balbi 已提交
2037

2038 2039
	musb->xceiv->state = OTG_STATE_UNDEFINED;
	stop_activity(musb, driver);
2040
	otg_set_peripheral(musb->xceiv->otg, NULL);
F
Felipe Balbi 已提交
2041

2042
	dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
F
Felipe Balbi 已提交
2043

2044 2045
	musb->is_active = 0;
	musb_platform_try_idle(musb, 0);
F
Felipe Balbi 已提交
2046 2047
	spin_unlock_irqrestore(&musb->lock, flags);

2048 2049 2050 2051 2052 2053
	usb_remove_hcd(musb_to_hcd(musb));
	/*
	 * FIXME we need to be able to register another
	 * gadget driver here and have everything work;
	 * that currently misbehaves.
	 */
2054

2055 2056
	pm_runtime_put(musb->controller);

2057
	return 0;
F
Felipe Balbi 已提交
2058 2059 2060 2061 2062 2063 2064 2065 2066
}

/* ----------------------------------------------------------------------- */

/* lifecycle operations called through plat_uds.c */

void musb_g_resume(struct musb *musb)
{
	musb->is_suspended = 0;
2067
	switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
	case OTG_STATE_B_IDLE:
		break;
	case OTG_STATE_B_WAIT_ACON:
	case OTG_STATE_B_PERIPHERAL:
		musb->is_active = 1;
		if (musb->gadget_driver && musb->gadget_driver->resume) {
			spin_unlock(&musb->lock);
			musb->gadget_driver->resume(&musb->g);
			spin_lock(&musb->lock);
		}
		break;
	default:
		WARNING("unhandled RESUME transition (%s)\n",
2081
				otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090
	}
}

/* called when SOF packets stop for 3+ msec */
void musb_g_suspend(struct musb *musb)
{
	u8	devctl;

	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2091
	dev_dbg(musb->controller, "devctl %02x\n", devctl);
F
Felipe Balbi 已提交
2092

2093
	switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
2094 2095
	case OTG_STATE_B_IDLE:
		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2096
			musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
		break;
	case OTG_STATE_B_PERIPHERAL:
		musb->is_suspended = 1;
		if (musb->gadget_driver && musb->gadget_driver->suspend) {
			spin_unlock(&musb->lock);
			musb->gadget_driver->suspend(&musb->g);
			spin_lock(&musb->lock);
		}
		break;
	default:
		/* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
		 * A_PERIPHERAL may need care too
		 */
		WARNING("unhandled SUSPEND transition (%s)\n",
2111
				otg_state_string(musb->xceiv->state));
F
Felipe Balbi 已提交
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
	}
}

/* Called during SRP */
void musb_g_wakeup(struct musb *musb)
{
	musb_gadget_wakeup(&musb->g);
}

/* called when VBUS drops below session threshold, and in other cases */
void musb_g_disconnect(struct musb *musb)
{
	void __iomem	*mregs = musb->mregs;
	u8	devctl = musb_readb(mregs, MUSB_DEVCTL);

2127
	dev_dbg(musb->controller, "devctl %02x\n", devctl);
F
Felipe Balbi 已提交
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141

	/* clear HR */
	musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);

	/* don't draw vbus until new b-default session */
	(void) musb_gadget_vbus_draw(&musb->g, 0);

	musb->g.speed = USB_SPEED_UNKNOWN;
	if (musb->gadget_driver && musb->gadget_driver->disconnect) {
		spin_unlock(&musb->lock);
		musb->gadget_driver->disconnect(&musb->g);
		spin_lock(&musb->lock);
	}

2142
	switch (musb->xceiv->state) {
F
Felipe Balbi 已提交
2143
	default:
2144
		dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
2145
			otg_state_string(musb->xceiv->state));
2146
		musb->xceiv->state = OTG_STATE_A_IDLE;
2147
		MUSB_HST_MODE(musb);
F
Felipe Balbi 已提交
2148 2149
		break;
	case OTG_STATE_A_PERIPHERAL:
2150
		musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2151
		MUSB_HST_MODE(musb);
F
Felipe Balbi 已提交
2152 2153 2154 2155 2156
		break;
	case OTG_STATE_B_WAIT_ACON:
	case OTG_STATE_B_HOST:
	case OTG_STATE_B_PERIPHERAL:
	case OTG_STATE_B_IDLE:
2157
		musb->xceiv->state = OTG_STATE_B_IDLE;
F
Felipe Balbi 已提交
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
		break;
	case OTG_STATE_B_SRP_INIT:
		break;
	}

	musb->is_active = 0;
}

void musb_g_reset(struct musb *musb)
__releases(musb->lock)
__acquires(musb->lock)
{
	void __iomem	*mbase = musb->mregs;
	u8		devctl = musb_readb(mbase, MUSB_DEVCTL);
	u8		power;

2174
	dev_dbg(musb->controller, "<== %s driver '%s'\n",
F
Felipe Balbi 已提交
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
			(devctl & MUSB_DEVCTL_BDEVICE)
				? "B-Device" : "A-Device",
			musb->gadget_driver
				? musb->gadget_driver->driver.name
				: NULL
			);

	/* report disconnect, if we didn't already (flushing EP state) */
	if (musb->g.speed != USB_SPEED_UNKNOWN)
		musb_g_disconnect(musb);

	/* clear HR */
	else if (devctl & MUSB_DEVCTL_HR)
		musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);


	/* what speed did we negotiate? */
	power = musb_readb(mbase, MUSB_POWER);
	musb->g.speed = (power & MUSB_POWER_HSMODE)
			? USB_SPEED_HIGH : USB_SPEED_FULL;

	/* start in USB_STATE_DEFAULT */
	musb->is_active = 1;
	musb->is_suspended = 0;
	MUSB_DEV_MODE(musb);
	musb->address = 0;
	musb->ep0_state = MUSB_EP0_STAGE_SETUP;

	musb->may_wakeup = 0;
	musb->g.b_hnp_enable = 0;
	musb->g.a_alt_hnp_support = 0;
	musb->g.a_hnp_support = 0;

	/* Normal reset, as B-Device;
	 * or else after HNP, as A-Device
	 */
	if (devctl & MUSB_DEVCTL_BDEVICE) {
2212
		musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
F
Felipe Balbi 已提交
2213
		musb->g.is_a_peripheral = 0;
2214
	} else {
2215
		musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
F
Felipe Balbi 已提交
2216
		musb->g.is_a_peripheral = 1;
2217
	}
F
Felipe Balbi 已提交
2218 2219

	/* start with default limits on VBUS power draw */
2220
	(void) musb_gadget_vbus_draw(&musb->g, 8);
F
Felipe Balbi 已提交
2221
}