xref: /openbmc/linux/drivers/usb/musb/musb_gadget.c (revision b240b419db5d624ce7a5a397d6f62a1a686009ec)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MUSB OTG driver peripheral support
4  *
5  * Copyright 2005 Mentor Graphics Corporation
6  * Copyright (C) 2005-2006 by Texas Instruments
7  * Copyright (C) 2006-2007 Nokia Corporation
8  * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/timer.h>
14 #include <linux/module.h>
15 #include <linux/smp.h>
16 #include <linux/spinlock.h>
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 
21 #include "musb_core.h"
22 #include "musb_trace.h"
23 
24 
25 /* ----------------------------------------------------------------------- */
26 
27 #define is_buffer_mapped(req) (is_dma_capable() && \
28 					(req->map_state != UN_MAPPED))
29 
30 /* Maps the buffer to dma  */
31 
32 static inline void map_dma_buffer(struct musb_request *request,
33 			struct musb *musb, struct musb_ep *musb_ep)
34 {
35 	int compatible = true;
36 	struct dma_controller *dma = musb->dma_controller;
37 
38 	request->map_state = UN_MAPPED;
39 
40 	if (!is_dma_capable() || !musb_ep->dma)
41 		return;
42 
43 	/* Check if DMA engine can handle this request.
44 	 * DMA code must reject the USB request explicitly.
45 	 * Default behaviour is to map the request.
46 	 */
47 	if (dma->is_compatible)
48 		compatible = dma->is_compatible(musb_ep->dma,
49 				musb_ep->packet_sz, request->request.buf,
50 				request->request.length);
51 	if (!compatible)
52 		return;
53 
54 	if (request->request.dma == DMA_ADDR_INVALID) {
55 		dma_addr_t dma_addr;
56 		int ret;
57 
58 		dma_addr = dma_map_single(
59 				musb->controller,
60 				request->request.buf,
61 				request->request.length,
62 				request->tx
63 					? DMA_TO_DEVICE
64 					: DMA_FROM_DEVICE);
65 		ret = dma_mapping_error(musb->controller, dma_addr);
66 		if (ret)
67 			return;
68 
69 		request->request.dma = dma_addr;
70 		request->map_state = MUSB_MAPPED;
71 	} else {
72 		dma_sync_single_for_device(musb->controller,
73 			request->request.dma,
74 			request->request.length,
75 			request->tx
76 				? DMA_TO_DEVICE
77 				: DMA_FROM_DEVICE);
78 		request->map_state = PRE_MAPPED;
79 	}
80 }
81 
82 /* Unmap the buffer from dma and maps it back to cpu */
83 static inline void unmap_dma_buffer(struct musb_request *request,
84 				struct musb *musb)
85 {
86 	struct musb_ep *musb_ep = request->ep;
87 
88 	if (!is_buffer_mapped(request) || !musb_ep->dma)
89 		return;
90 
91 	if (request->request.dma == DMA_ADDR_INVALID) {
92 		dev_vdbg(musb->controller,
93 				"not unmapping a never mapped buffer\n");
94 		return;
95 	}
96 	if (request->map_state == MUSB_MAPPED) {
97 		dma_unmap_single(musb->controller,
98 			request->request.dma,
99 			request->request.length,
100 			request->tx
101 				? DMA_TO_DEVICE
102 				: DMA_FROM_DEVICE);
103 		request->request.dma = DMA_ADDR_INVALID;
104 	} else { /* PRE_MAPPED */
105 		dma_sync_single_for_cpu(musb->controller,
106 			request->request.dma,
107 			request->request.length,
108 			request->tx
109 				? DMA_TO_DEVICE
110 				: DMA_FROM_DEVICE);
111 	}
112 	request->map_state = UN_MAPPED;
113 }
114 
115 /*
116  * Immediately complete a request.
117  *
118  * @param request the request to complete
119  * @param status the status to complete the request with
120  * Context: controller locked, IRQs blocked.
121  */
122 void musb_g_giveback(
123 	struct musb_ep		*ep,
124 	struct usb_request	*request,
125 	int			status)
126 __releases(ep->musb->lock)
127 __acquires(ep->musb->lock)
128 {
129 	struct musb_request	*req;
130 	struct musb		*musb;
131 	int			busy = ep->busy;
132 
133 	req = to_musb_request(request);
134 
135 	list_del(&req->list);
136 	if (req->request.status == -EINPROGRESS)
137 		req->request.status = status;
138 	musb = req->musb;
139 
140 	ep->busy = 1;
141 	spin_unlock(&musb->lock);
142 
143 	if (!dma_mapping_error(&musb->g.dev, request->dma))
144 		unmap_dma_buffer(req, musb);
145 
146 	trace_musb_req_gb(req);
147 	usb_gadget_giveback_request(&req->ep->end_point, &req->request);
148 	spin_lock(&musb->lock);
149 	ep->busy = busy;
150 }
151 
152 /* ----------------------------------------------------------------------- */
153 
154 /*
155  * Abort requests queued to an endpoint using the status. Synchronous.
156  * caller locked controller and blocked irqs, and selected this ep.
157  */
158 static void nuke(struct musb_ep *ep, const int status)
159 {
160 	struct musb		*musb = ep->musb;
161 	struct musb_request	*req = NULL;
162 	void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
163 
164 	ep->busy = 1;
165 
166 	if (is_dma_capable() && ep->dma) {
167 		struct dma_controller	*c = ep->musb->dma_controller;
168 		int value;
169 
170 		if (ep->is_in) {
171 			/*
172 			 * The programming guide says that we must not clear
173 			 * the DMAMODE bit before DMAENAB, so we only
174 			 * clear it in the second write...
175 			 */
176 			musb_writew(epio, MUSB_TXCSR,
177 				    MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
178 			musb_writew(epio, MUSB_TXCSR,
179 					0 | MUSB_TXCSR_FLUSHFIFO);
180 		} else {
181 			musb_writew(epio, MUSB_RXCSR,
182 					0 | MUSB_RXCSR_FLUSHFIFO);
183 			musb_writew(epio, MUSB_RXCSR,
184 					0 | MUSB_RXCSR_FLUSHFIFO);
185 		}
186 
187 		value = c->channel_abort(ep->dma);
188 		musb_dbg(musb, "%s: abort DMA --> %d", ep->name, value);
189 		c->channel_release(ep->dma);
190 		ep->dma = NULL;
191 	}
192 
193 	while (!list_empty(&ep->req_list)) {
194 		req = list_first_entry(&ep->req_list, struct musb_request, list);
195 		musb_g_giveback(ep, &req->request, status);
196 	}
197 }
198 
199 /* ----------------------------------------------------------------------- */
200 
201 /* Data transfers - pure PIO, pure DMA, or mixed mode */
202 
203 /*
204  * This assumes the separate CPPI engine is responding to DMA requests
205  * from the usb core ... sequenced a bit differently from mentor dma.
206  */
207 
208 static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
209 {
210 	if (can_bulk_split(musb, ep->type))
211 		return ep->hw_ep->max_packet_sz_tx;
212 	else
213 		return ep->packet_sz;
214 }
215 
216 /*
217  * An endpoint is transmitting data. This can be called either from
218  * the IRQ routine or from ep.queue() to kickstart a request on an
219  * endpoint.
220  *
221  * Context: controller locked, IRQs blocked, endpoint selected
222  */
223 static void txstate(struct musb *musb, struct musb_request *req)
224 {
225 	u8			epnum = req->epnum;
226 	struct musb_ep		*musb_ep;
227 	void __iomem		*epio = musb->endpoints[epnum].regs;
228 	struct usb_request	*request;
229 	u16			fifo_count = 0, csr;
230 	int			use_dma = 0;
231 
232 	musb_ep = req->ep;
233 
234 	/* Check if EP is disabled */
235 	if (!musb_ep->desc) {
236 		musb_dbg(musb, "ep:%s disabled - ignore request",
237 						musb_ep->end_point.name);
238 		return;
239 	}
240 
241 	/* we shouldn't get here while DMA is active ... but we do ... */
242 	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
243 		musb_dbg(musb, "dma pending...");
244 		return;
245 	}
246 
247 	/* read TXCSR before */
248 	csr = musb_readw(epio, MUSB_TXCSR);
249 
250 	request = &req->request;
251 	fifo_count = min(max_ep_writesize(musb, musb_ep),
252 			(int)(request->length - request->actual));
253 
254 	if (csr & MUSB_TXCSR_TXPKTRDY) {
255 		musb_dbg(musb, "%s old packet still ready , txcsr %03x",
256 				musb_ep->end_point.name, csr);
257 		return;
258 	}
259 
260 	if (csr & MUSB_TXCSR_P_SENDSTALL) {
261 		musb_dbg(musb, "%s stalling, txcsr %03x",
262 				musb_ep->end_point.name, csr);
263 		return;
264 	}
265 
266 	musb_dbg(musb, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x",
267 			epnum, musb_ep->packet_sz, fifo_count,
268 			csr);
269 
270 #ifndef	CONFIG_MUSB_PIO_ONLY
271 	if (is_buffer_mapped(req)) {
272 		struct dma_controller	*c = musb->dma_controller;
273 		size_t request_size;
274 
275 		/* setup DMA, then program endpoint CSR */
276 		request_size = min_t(size_t, request->length - request->actual,
277 					musb_ep->dma->max_len);
278 
279 		use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
280 
281 		/* MUSB_TXCSR_P_ISO is still set correctly */
282 
283 		if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) {
284 			if (request_size < musb_ep->packet_sz)
285 				musb_ep->dma->desired_mode = 0;
286 			else
287 				musb_ep->dma->desired_mode = 1;
288 
289 			use_dma = use_dma && c->channel_program(
290 					musb_ep->dma, musb_ep->packet_sz,
291 					musb_ep->dma->desired_mode,
292 					request->dma + request->actual, request_size);
293 			if (use_dma) {
294 				if (musb_ep->dma->desired_mode == 0) {
295 					/*
296 					 * We must not clear the DMAMODE bit
297 					 * before the DMAENAB bit -- and the
298 					 * latter doesn't always get cleared
299 					 * before we get here...
300 					 */
301 					csr &= ~(MUSB_TXCSR_AUTOSET
302 						| MUSB_TXCSR_DMAENAB);
303 					musb_writew(epio, MUSB_TXCSR, csr
304 						| MUSB_TXCSR_P_WZC_BITS);
305 					csr &= ~MUSB_TXCSR_DMAMODE;
306 					csr |= (MUSB_TXCSR_DMAENAB |
307 							MUSB_TXCSR_MODE);
308 					/* against programming guide */
309 				} else {
310 					csr |= (MUSB_TXCSR_DMAENAB
311 							| MUSB_TXCSR_DMAMODE
312 							| MUSB_TXCSR_MODE);
313 					/*
314 					 * Enable Autoset according to table
315 					 * below
316 					 * bulk_split hb_mult	Autoset_Enable
317 					 *	0	0	Yes(Normal)
318 					 *	0	>0	No(High BW ISO)
319 					 *	1	0	Yes(HS bulk)
320 					 *	1	>0	Yes(FS bulk)
321 					 */
322 					if (!musb_ep->hb_mult ||
323 					    can_bulk_split(musb,
324 							   musb_ep->type))
325 						csr |= MUSB_TXCSR_AUTOSET;
326 				}
327 				csr &= ~MUSB_TXCSR_P_UNDERRUN;
328 
329 				musb_writew(epio, MUSB_TXCSR, csr);
330 			}
331 		}
332 
333 		if (is_cppi_enabled(musb)) {
334 			/* program endpoint CSR first, then setup DMA */
335 			csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
336 			csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
337 				MUSB_TXCSR_MODE;
338 			musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS &
339 						~MUSB_TXCSR_P_UNDERRUN) | csr);
340 
341 			/* ensure writebuffer is empty */
342 			csr = musb_readw(epio, MUSB_TXCSR);
343 
344 			/*
345 			 * NOTE host side sets DMAENAB later than this; both are
346 			 * OK since the transfer dma glue (between CPPI and
347 			 * Mentor fifos) just tells CPPI it could start. Data
348 			 * only moves to the USB TX fifo when both fifos are
349 			 * ready.
350 			 */
351 			/*
352 			 * "mode" is irrelevant here; handle terminating ZLPs
353 			 * like PIO does, since the hardware RNDIS mode seems
354 			 * unreliable except for the
355 			 * last-packet-is-already-short case.
356 			 */
357 			use_dma = use_dma && c->channel_program(
358 					musb_ep->dma, musb_ep->packet_sz,
359 					0,
360 					request->dma + request->actual,
361 					request_size);
362 			if (!use_dma) {
363 				c->channel_release(musb_ep->dma);
364 				musb_ep->dma = NULL;
365 				csr &= ~MUSB_TXCSR_DMAENAB;
366 				musb_writew(epio, MUSB_TXCSR, csr);
367 				/* invariant: prequest->buf is non-null */
368 			}
369 		} else if (tusb_dma_omap(musb))
370 			use_dma = use_dma && c->channel_program(
371 					musb_ep->dma, musb_ep->packet_sz,
372 					request->zero,
373 					request->dma + request->actual,
374 					request_size);
375 	}
376 #endif
377 
378 	if (!use_dma) {
379 		/*
380 		 * Unmap the dma buffer back to cpu if dma channel
381 		 * programming fails
382 		 */
383 		unmap_dma_buffer(req, musb);
384 
385 		musb_write_fifo(musb_ep->hw_ep, fifo_count,
386 				(u8 *) (request->buf + request->actual));
387 		request->actual += fifo_count;
388 		csr |= MUSB_TXCSR_TXPKTRDY;
389 		csr &= ~MUSB_TXCSR_P_UNDERRUN;
390 		musb_writew(epio, MUSB_TXCSR, csr);
391 	}
392 
393 	/* host may already have the data when this message shows... */
394 	musb_dbg(musb, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d",
395 			musb_ep->end_point.name, use_dma ? "dma" : "pio",
396 			request->actual, request->length,
397 			musb_readw(epio, MUSB_TXCSR),
398 			fifo_count,
399 			musb_readw(epio, MUSB_TXMAXP));
400 }
401 
402 /*
403  * FIFO state update (e.g. data ready).
404  * Called from IRQ,  with controller locked.
405  */
406 void musb_g_tx(struct musb *musb, u8 epnum)
407 {
408 	u16			csr;
409 	struct musb_request	*req;
410 	struct usb_request	*request;
411 	u8 __iomem		*mbase = musb->mregs;
412 	struct musb_ep		*musb_ep = &musb->endpoints[epnum].ep_in;
413 	void __iomem		*epio = musb->endpoints[epnum].regs;
414 	struct dma_channel	*dma;
415 
416 	musb_ep_select(mbase, epnum);
417 	req = next_request(musb_ep);
418 	request = &req->request;
419 
420 	trace_musb_req_tx(req);
421 	csr = musb_readw(epio, MUSB_TXCSR);
422 	musb_dbg(musb, "<== %s, txcsr %04x", musb_ep->end_point.name, csr);
423 
424 	dma = is_dma_capable() ? musb_ep->dma : NULL;
425 
426 	/*
427 	 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
428 	 * probably rates reporting as a host error.
429 	 */
430 	if (csr & MUSB_TXCSR_P_SENTSTALL) {
431 		csr |=	MUSB_TXCSR_P_WZC_BITS;
432 		csr &= ~MUSB_TXCSR_P_SENTSTALL;
433 		musb_writew(epio, MUSB_TXCSR, csr);
434 		return;
435 	}
436 
437 	if (csr & MUSB_TXCSR_P_UNDERRUN) {
438 		/* We NAKed, no big deal... little reason to care. */
439 		csr |=	 MUSB_TXCSR_P_WZC_BITS;
440 		csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
441 		musb_writew(epio, MUSB_TXCSR, csr);
442 		dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
443 				epnum, request);
444 	}
445 
446 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
447 		/*
448 		 * SHOULD NOT HAPPEN... has with CPPI though, after
449 		 * changing SENDSTALL (and other cases); harmless?
450 		 */
451 		musb_dbg(musb, "%s dma still busy?", musb_ep->end_point.name);
452 		return;
453 	}
454 
455 	if (request) {
456 		u8	is_dma = 0;
457 		bool	short_packet = false;
458 
459 		if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
460 			is_dma = 1;
461 			csr |= MUSB_TXCSR_P_WZC_BITS;
462 			csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
463 				 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
464 			musb_writew(epio, MUSB_TXCSR, csr);
465 			/* Ensure writebuffer is empty. */
466 			csr = musb_readw(epio, MUSB_TXCSR);
467 			request->actual += musb_ep->dma->actual_len;
468 			musb_dbg(musb, "TXCSR%d %04x, DMA off, len %zu, req %p",
469 				epnum, csr, musb_ep->dma->actual_len, request);
470 		}
471 
472 		/*
473 		 * First, maybe a terminating short packet. Some DMA
474 		 * engines might handle this by themselves.
475 		 */
476 		if ((request->zero && request->length)
477 			&& (request->length % musb_ep->packet_sz == 0)
478 			&& (request->actual == request->length))
479 				short_packet = true;
480 
481 		if ((musb_dma_inventra(musb) || musb_dma_ux500(musb)) &&
482 			(is_dma && (!dma->desired_mode ||
483 				(request->actual &
484 					(musb_ep->packet_sz - 1)))))
485 				short_packet = true;
486 
487 		if (short_packet) {
488 			/*
489 			 * On DMA completion, FIFO may not be
490 			 * available yet...
491 			 */
492 			if (csr & MUSB_TXCSR_TXPKTRDY)
493 				return;
494 
495 			musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
496 					| MUSB_TXCSR_TXPKTRDY);
497 			request->zero = 0;
498 		}
499 
500 		if (request->actual == request->length) {
501 			musb_g_giveback(musb_ep, request, 0);
502 			/*
503 			 * In the giveback function the MUSB lock is
504 			 * released and acquired after sometime. During
505 			 * this time period the INDEX register could get
506 			 * changed by the gadget_queue function especially
507 			 * on SMP systems. Reselect the INDEX to be sure
508 			 * we are reading/modifying the right registers
509 			 */
510 			musb_ep_select(mbase, epnum);
511 			req = musb_ep->desc ? next_request(musb_ep) : NULL;
512 			if (!req) {
513 				musb_dbg(musb, "%s idle now",
514 					musb_ep->end_point.name);
515 				return;
516 			}
517 		}
518 
519 		txstate(musb, req);
520 	}
521 }
522 
523 /* ------------------------------------------------------------ */
524 
525 /*
526  * Context: controller locked, IRQs blocked, endpoint selected
527  */
528 static void rxstate(struct musb *musb, struct musb_request *req)
529 {
530 	const u8		epnum = req->epnum;
531 	struct usb_request	*request = &req->request;
532 	struct musb_ep		*musb_ep;
533 	void __iomem		*epio = musb->endpoints[epnum].regs;
534 	unsigned		len = 0;
535 	u16			fifo_count;
536 	u16			csr = musb_readw(epio, MUSB_RXCSR);
537 	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
538 	u8			use_mode_1;
539 
540 	if (hw_ep->is_shared_fifo)
541 		musb_ep = &hw_ep->ep_in;
542 	else
543 		musb_ep = &hw_ep->ep_out;
544 
545 	fifo_count = musb_ep->packet_sz;
546 
547 	/* Check if EP is disabled */
548 	if (!musb_ep->desc) {
549 		musb_dbg(musb, "ep:%s disabled - ignore request",
550 						musb_ep->end_point.name);
551 		return;
552 	}
553 
554 	/* We shouldn't get here while DMA is active, but we do... */
555 	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
556 		musb_dbg(musb, "DMA pending...");
557 		return;
558 	}
559 
560 	if (csr & MUSB_RXCSR_P_SENDSTALL) {
561 		musb_dbg(musb, "%s stalling, RXCSR %04x",
562 		    musb_ep->end_point.name, csr);
563 		return;
564 	}
565 
566 	if (is_cppi_enabled(musb) && is_buffer_mapped(req)) {
567 		struct dma_controller	*c = musb->dma_controller;
568 		struct dma_channel	*channel = musb_ep->dma;
569 
570 		/* NOTE:  CPPI won't actually stop advancing the DMA
571 		 * queue after short packet transfers, so this is almost
572 		 * always going to run as IRQ-per-packet DMA so that
573 		 * faults will be handled correctly.
574 		 */
575 		if (c->channel_program(channel,
576 				musb_ep->packet_sz,
577 				!request->short_not_ok,
578 				request->dma + request->actual,
579 				request->length - request->actual)) {
580 
581 			/* make sure that if an rxpkt arrived after the irq,
582 			 * the cppi engine will be ready to take it as soon
583 			 * as DMA is enabled
584 			 */
585 			csr &= ~(MUSB_RXCSR_AUTOCLEAR
586 					| MUSB_RXCSR_DMAMODE);
587 			csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
588 			musb_writew(epio, MUSB_RXCSR, csr);
589 			return;
590 		}
591 	}
592 
593 	if (csr & MUSB_RXCSR_RXPKTRDY) {
594 		fifo_count = musb_readw(epio, MUSB_RXCOUNT);
595 
596 		/*
597 		 * Enable Mode 1 on RX transfers only when short_not_ok flag
598 		 * is set. Currently short_not_ok flag is set only from
599 		 * file_storage and f_mass_storage drivers
600 		 */
601 
602 		if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
603 			use_mode_1 = 1;
604 		else
605 			use_mode_1 = 0;
606 
607 		if (request->actual < request->length) {
608 			if (!is_buffer_mapped(req))
609 				goto buffer_aint_mapped;
610 
611 			if (musb_dma_inventra(musb)) {
612 				struct dma_controller	*c;
613 				struct dma_channel	*channel;
614 				int			use_dma = 0;
615 				unsigned int transfer_size;
616 
617 				c = musb->dma_controller;
618 				channel = musb_ep->dma;
619 
620 	/* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
621 	 * mode 0 only. So we do not get endpoint interrupts due to DMA
622 	 * completion. We only get interrupts from DMA controller.
623 	 *
624 	 * We could operate in DMA mode 1 if we knew the size of the tranfer
625 	 * in advance. For mass storage class, request->length = what the host
626 	 * sends, so that'd work.  But for pretty much everything else,
627 	 * request->length is routinely more than what the host sends. For
628 	 * most these gadgets, end of is signified either by a short packet,
629 	 * or filling the last byte of the buffer.  (Sending extra data in
630 	 * that last pckate should trigger an overflow fault.)  But in mode 1,
631 	 * we don't get DMA completion interrupt for short packets.
632 	 *
633 	 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
634 	 * to get endpoint interrupt on every DMA req, but that didn't seem
635 	 * to work reliably.
636 	 *
637 	 * REVISIT an updated g_file_storage can set req->short_not_ok, which
638 	 * then becomes usable as a runtime "use mode 1" hint...
639 	 */
640 
641 				/* Experimental: Mode1 works with mass storage use cases */
642 				if (use_mode_1) {
643 					csr |= MUSB_RXCSR_AUTOCLEAR;
644 					musb_writew(epio, MUSB_RXCSR, csr);
645 					csr |= MUSB_RXCSR_DMAENAB;
646 					musb_writew(epio, MUSB_RXCSR, csr);
647 
648 					/*
649 					 * this special sequence (enabling and then
650 					 * disabling MUSB_RXCSR_DMAMODE) is required
651 					 * to get DMAReq to activate
652 					 */
653 					musb_writew(epio, MUSB_RXCSR,
654 						csr | MUSB_RXCSR_DMAMODE);
655 					musb_writew(epio, MUSB_RXCSR, csr);
656 
657 					transfer_size = min_t(unsigned int,
658 							request->length -
659 							request->actual,
660 							channel->max_len);
661 					musb_ep->dma->desired_mode = 1;
662 				} else {
663 					if (!musb_ep->hb_mult &&
664 						musb_ep->hw_ep->rx_double_buffered)
665 						csr |= MUSB_RXCSR_AUTOCLEAR;
666 					csr |= MUSB_RXCSR_DMAENAB;
667 					musb_writew(epio, MUSB_RXCSR, csr);
668 
669 					transfer_size = min(request->length - request->actual,
670 							(unsigned)fifo_count);
671 					musb_ep->dma->desired_mode = 0;
672 				}
673 
674 				use_dma = c->channel_program(
675 						channel,
676 						musb_ep->packet_sz,
677 						channel->desired_mode,
678 						request->dma
679 						+ request->actual,
680 						transfer_size);
681 
682 				if (use_dma)
683 					return;
684 			}
685 
686 			if ((musb_dma_ux500(musb)) &&
687 				(request->actual < request->length)) {
688 
689 				struct dma_controller *c;
690 				struct dma_channel *channel;
691 				unsigned int transfer_size = 0;
692 
693 				c = musb->dma_controller;
694 				channel = musb_ep->dma;
695 
696 				/* In case first packet is short */
697 				if (fifo_count < musb_ep->packet_sz)
698 					transfer_size = fifo_count;
699 				else if (request->short_not_ok)
700 					transfer_size =	min_t(unsigned int,
701 							request->length -
702 							request->actual,
703 							channel->max_len);
704 				else
705 					transfer_size = min_t(unsigned int,
706 							request->length -
707 							request->actual,
708 							(unsigned)fifo_count);
709 
710 				csr &= ~MUSB_RXCSR_DMAMODE;
711 				csr |= (MUSB_RXCSR_DMAENAB |
712 					MUSB_RXCSR_AUTOCLEAR);
713 
714 				musb_writew(epio, MUSB_RXCSR, csr);
715 
716 				if (transfer_size <= musb_ep->packet_sz) {
717 					musb_ep->dma->desired_mode = 0;
718 				} else {
719 					musb_ep->dma->desired_mode = 1;
720 					/* Mode must be set after DMAENAB */
721 					csr |= MUSB_RXCSR_DMAMODE;
722 					musb_writew(epio, MUSB_RXCSR, csr);
723 				}
724 
725 				if (c->channel_program(channel,
726 							musb_ep->packet_sz,
727 							channel->desired_mode,
728 							request->dma
729 							+ request->actual,
730 							transfer_size))
731 
732 					return;
733 			}
734 
735 			len = request->length - request->actual;
736 			musb_dbg(musb, "%s OUT/RX pio fifo %d/%d, maxpacket %d",
737 					musb_ep->end_point.name,
738 					fifo_count, len,
739 					musb_ep->packet_sz);
740 
741 			fifo_count = min_t(unsigned, len, fifo_count);
742 
743 			if (tusb_dma_omap(musb)) {
744 				struct dma_controller *c = musb->dma_controller;
745 				struct dma_channel *channel = musb_ep->dma;
746 				u32 dma_addr = request->dma + request->actual;
747 				int ret;
748 
749 				ret = c->channel_program(channel,
750 						musb_ep->packet_sz,
751 						channel->desired_mode,
752 						dma_addr,
753 						fifo_count);
754 				if (ret)
755 					return;
756 			}
757 
758 			/*
759 			 * Unmap the dma buffer back to cpu if dma channel
760 			 * programming fails. This buffer is mapped if the
761 			 * channel allocation is successful
762 			 */
763 			unmap_dma_buffer(req, musb);
764 
765 			/*
766 			 * Clear DMAENAB and AUTOCLEAR for the
767 			 * PIO mode transfer
768 			 */
769 			csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
770 			musb_writew(epio, MUSB_RXCSR, csr);
771 
772 buffer_aint_mapped:
773 			musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
774 					(request->buf + request->actual));
775 			request->actual += fifo_count;
776 
777 			/* REVISIT if we left anything in the fifo, flush
778 			 * it and report -EOVERFLOW
779 			 */
780 
781 			/* ack the read! */
782 			csr |= MUSB_RXCSR_P_WZC_BITS;
783 			csr &= ~MUSB_RXCSR_RXPKTRDY;
784 			musb_writew(epio, MUSB_RXCSR, csr);
785 		}
786 	}
787 
788 	/* reach the end or short packet detected */
789 	if (request->actual == request->length ||
790 	    fifo_count < musb_ep->packet_sz)
791 		musb_g_giveback(musb_ep, request, 0);
792 }
793 
794 /*
795  * Data ready for a request; called from IRQ
796  */
797 void musb_g_rx(struct musb *musb, u8 epnum)
798 {
799 	u16			csr;
800 	struct musb_request	*req;
801 	struct usb_request	*request;
802 	void __iomem		*mbase = musb->mregs;
803 	struct musb_ep		*musb_ep;
804 	void __iomem		*epio = musb->endpoints[epnum].regs;
805 	struct dma_channel	*dma;
806 	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
807 
808 	if (hw_ep->is_shared_fifo)
809 		musb_ep = &hw_ep->ep_in;
810 	else
811 		musb_ep = &hw_ep->ep_out;
812 
813 	musb_ep_select(mbase, epnum);
814 
815 	req = next_request(musb_ep);
816 	if (!req)
817 		return;
818 
819 	trace_musb_req_rx(req);
820 	request = &req->request;
821 
822 	csr = musb_readw(epio, MUSB_RXCSR);
823 	dma = is_dma_capable() ? musb_ep->dma : NULL;
824 
825 	musb_dbg(musb, "<== %s, rxcsr %04x%s %p", musb_ep->end_point.name,
826 			csr, dma ? " (dma)" : "", request);
827 
828 	if (csr & MUSB_RXCSR_P_SENTSTALL) {
829 		csr |= MUSB_RXCSR_P_WZC_BITS;
830 		csr &= ~MUSB_RXCSR_P_SENTSTALL;
831 		musb_writew(epio, MUSB_RXCSR, csr);
832 		return;
833 	}
834 
835 	if (csr & MUSB_RXCSR_P_OVERRUN) {
836 		/* csr |= MUSB_RXCSR_P_WZC_BITS; */
837 		csr &= ~MUSB_RXCSR_P_OVERRUN;
838 		musb_writew(epio, MUSB_RXCSR, csr);
839 
840 		musb_dbg(musb, "%s iso overrun on %p", musb_ep->name, request);
841 		if (request->status == -EINPROGRESS)
842 			request->status = -EOVERFLOW;
843 	}
844 	if (csr & MUSB_RXCSR_INCOMPRX) {
845 		/* REVISIT not necessarily an error */
846 		musb_dbg(musb, "%s, incomprx", musb_ep->end_point.name);
847 	}
848 
849 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
850 		/* "should not happen"; likely RXPKTRDY pending for DMA */
851 		musb_dbg(musb, "%s busy, csr %04x",
852 			musb_ep->end_point.name, csr);
853 		return;
854 	}
855 
856 	if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
857 		csr &= ~(MUSB_RXCSR_AUTOCLEAR
858 				| MUSB_RXCSR_DMAENAB
859 				| MUSB_RXCSR_DMAMODE);
860 		musb_writew(epio, MUSB_RXCSR,
861 			MUSB_RXCSR_P_WZC_BITS | csr);
862 
863 		request->actual += musb_ep->dma->actual_len;
864 
865 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
866 	defined(CONFIG_USB_UX500_DMA)
867 		/* Autoclear doesn't clear RxPktRdy for short packets */
868 		if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
869 				|| (dma->actual_len
870 					& (musb_ep->packet_sz - 1))) {
871 			/* ack the read! */
872 			csr &= ~MUSB_RXCSR_RXPKTRDY;
873 			musb_writew(epio, MUSB_RXCSR, csr);
874 		}
875 
876 		/* incomplete, and not short? wait for next IN packet */
877 		if ((request->actual < request->length)
878 				&& (musb_ep->dma->actual_len
879 					== musb_ep->packet_sz)) {
880 			/* In double buffer case, continue to unload fifo if
881  			 * there is Rx packet in FIFO.
882  			 **/
883 			csr = musb_readw(epio, MUSB_RXCSR);
884 			if ((csr & MUSB_RXCSR_RXPKTRDY) &&
885 				hw_ep->rx_double_buffered)
886 				goto exit;
887 			return;
888 		}
889 #endif
890 		musb_g_giveback(musb_ep, request, 0);
891 		/*
892 		 * In the giveback function the MUSB lock is
893 		 * released and acquired after sometime. During
894 		 * this time period the INDEX register could get
895 		 * changed by the gadget_queue function especially
896 		 * on SMP systems. Reselect the INDEX to be sure
897 		 * we are reading/modifying the right registers
898 		 */
899 		musb_ep_select(mbase, epnum);
900 
901 		req = next_request(musb_ep);
902 		if (!req)
903 			return;
904 	}
905 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
906 	defined(CONFIG_USB_UX500_DMA)
907 exit:
908 #endif
909 	/* Analyze request */
910 	rxstate(musb, req);
911 }
912 
913 /* ------------------------------------------------------------ */
914 
915 static int musb_gadget_enable(struct usb_ep *ep,
916 			const struct usb_endpoint_descriptor *desc)
917 {
918 	unsigned long		flags;
919 	struct musb_ep		*musb_ep;
920 	struct musb_hw_ep	*hw_ep;
921 	void __iomem		*regs;
922 	struct musb		*musb;
923 	void __iomem	*mbase;
924 	u8		epnum;
925 	u16		csr;
926 	unsigned	tmp;
927 	int		status = -EINVAL;
928 
929 	if (!ep || !desc)
930 		return -EINVAL;
931 
932 	musb_ep = to_musb_ep(ep);
933 	hw_ep = musb_ep->hw_ep;
934 	regs = hw_ep->regs;
935 	musb = musb_ep->musb;
936 	mbase = musb->mregs;
937 	epnum = musb_ep->current_epnum;
938 
939 	spin_lock_irqsave(&musb->lock, flags);
940 
941 	if (musb_ep->desc) {
942 		status = -EBUSY;
943 		goto fail;
944 	}
945 	musb_ep->type = usb_endpoint_type(desc);
946 
947 	/* check direction and (later) maxpacket size against endpoint */
948 	if (usb_endpoint_num(desc) != epnum)
949 		goto fail;
950 
951 	/* REVISIT this rules out high bandwidth periodic transfers */
952 	tmp = usb_endpoint_maxp_mult(desc) - 1;
953 	if (tmp) {
954 		int ok;
955 
956 		if (usb_endpoint_dir_in(desc))
957 			ok = musb->hb_iso_tx;
958 		else
959 			ok = musb->hb_iso_rx;
960 
961 		if (!ok) {
962 			musb_dbg(musb, "no support for high bandwidth ISO");
963 			goto fail;
964 		}
965 		musb_ep->hb_mult = tmp;
966 	} else {
967 		musb_ep->hb_mult = 0;
968 	}
969 
970 	musb_ep->packet_sz = usb_endpoint_maxp(desc);
971 	tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
972 
973 	/* enable the interrupts for the endpoint, set the endpoint
974 	 * packet size (or fail), set the mode, clear the fifo
975 	 */
976 	musb_ep_select(mbase, epnum);
977 	if (usb_endpoint_dir_in(desc)) {
978 
979 		if (hw_ep->is_shared_fifo)
980 			musb_ep->is_in = 1;
981 		if (!musb_ep->is_in)
982 			goto fail;
983 
984 		if (tmp > hw_ep->max_packet_sz_tx) {
985 			musb_dbg(musb, "packet size beyond hardware FIFO size");
986 			goto fail;
987 		}
988 
989 		musb->intrtxe |= (1 << epnum);
990 		musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
991 
992 		/* REVISIT if can_bulk_split(), use by updating "tmp";
993 		 * likewise high bandwidth periodic tx
994 		 */
995 		/* Set TXMAXP with the FIFO size of the endpoint
996 		 * to disable double buffering mode.
997 		 */
998 		if (can_bulk_split(musb, musb_ep->type))
999 			musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
1000 						musb_ep->packet_sz) - 1;
1001 		musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1002 				| (musb_ep->hb_mult << 11));
1003 
1004 		csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1005 		if (musb_readw(regs, MUSB_TXCSR)
1006 				& MUSB_TXCSR_FIFONOTEMPTY)
1007 			csr |= MUSB_TXCSR_FLUSHFIFO;
1008 		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1009 			csr |= MUSB_TXCSR_P_ISO;
1010 
1011 		/* set twice in case of double buffering */
1012 		musb_writew(regs, MUSB_TXCSR, csr);
1013 		/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1014 		musb_writew(regs, MUSB_TXCSR, csr);
1015 
1016 	} else {
1017 
1018 		if (hw_ep->is_shared_fifo)
1019 			musb_ep->is_in = 0;
1020 		if (musb_ep->is_in)
1021 			goto fail;
1022 
1023 		if (tmp > hw_ep->max_packet_sz_rx) {
1024 			musb_dbg(musb, "packet size beyond hardware FIFO size");
1025 			goto fail;
1026 		}
1027 
1028 		musb->intrrxe |= (1 << epnum);
1029 		musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
1030 
1031 		/* REVISIT if can_bulk_combine() use by updating "tmp"
1032 		 * likewise high bandwidth periodic rx
1033 		 */
1034 		/* Set RXMAXP with the FIFO size of the endpoint
1035 		 * to disable double buffering mode.
1036 		 */
1037 		musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1038 				| (musb_ep->hb_mult << 11));
1039 
1040 		/* force shared fifo to OUT-only mode */
1041 		if (hw_ep->is_shared_fifo) {
1042 			csr = musb_readw(regs, MUSB_TXCSR);
1043 			csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1044 			musb_writew(regs, MUSB_TXCSR, csr);
1045 		}
1046 
1047 		csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1048 		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1049 			csr |= MUSB_RXCSR_P_ISO;
1050 		else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1051 			csr |= MUSB_RXCSR_DISNYET;
1052 
1053 		/* set twice in case of double buffering */
1054 		musb_writew(regs, MUSB_RXCSR, csr);
1055 		musb_writew(regs, MUSB_RXCSR, csr);
1056 	}
1057 
1058 	/* NOTE:  all the I/O code _should_ work fine without DMA, in case
1059 	 * for some reason you run out of channels here.
1060 	 */
1061 	if (is_dma_capable() && musb->dma_controller) {
1062 		struct dma_controller	*c = musb->dma_controller;
1063 
1064 		musb_ep->dma = c->channel_alloc(c, hw_ep,
1065 				(desc->bEndpointAddress & USB_DIR_IN));
1066 	} else
1067 		musb_ep->dma = NULL;
1068 
1069 	musb_ep->desc = desc;
1070 	musb_ep->busy = 0;
1071 	musb_ep->wedged = 0;
1072 	status = 0;
1073 
1074 	pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1075 			musb_driver_name, musb_ep->end_point.name,
1076 			musb_ep_xfertype_string(musb_ep->type),
1077 			musb_ep->is_in ? "IN" : "OUT",
1078 			musb_ep->dma ? "dma, " : "",
1079 			musb_ep->packet_sz);
1080 
1081 	schedule_delayed_work(&musb->irq_work, 0);
1082 
1083 fail:
1084 	spin_unlock_irqrestore(&musb->lock, flags);
1085 	return status;
1086 }
1087 
1088 /*
1089  * Disable an endpoint flushing all requests queued.
1090  */
1091 static int musb_gadget_disable(struct usb_ep *ep)
1092 {
1093 	unsigned long	flags;
1094 	struct musb	*musb;
1095 	u8		epnum;
1096 	struct musb_ep	*musb_ep;
1097 	void __iomem	*epio;
1098 	int		status = 0;
1099 
1100 	musb_ep = to_musb_ep(ep);
1101 	musb = musb_ep->musb;
1102 	epnum = musb_ep->current_epnum;
1103 	epio = musb->endpoints[epnum].regs;
1104 
1105 	spin_lock_irqsave(&musb->lock, flags);
1106 	musb_ep_select(musb->mregs, epnum);
1107 
1108 	/* zero the endpoint sizes */
1109 	if (musb_ep->is_in) {
1110 		musb->intrtxe &= ~(1 << epnum);
1111 		musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
1112 		musb_writew(epio, MUSB_TXMAXP, 0);
1113 	} else {
1114 		musb->intrrxe &= ~(1 << epnum);
1115 		musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
1116 		musb_writew(epio, MUSB_RXMAXP, 0);
1117 	}
1118 
1119 	/* abort all pending DMA and requests */
1120 	nuke(musb_ep, -ESHUTDOWN);
1121 
1122 	musb_ep->desc = NULL;
1123 	musb_ep->end_point.desc = NULL;
1124 
1125 	schedule_delayed_work(&musb->irq_work, 0);
1126 
1127 	spin_unlock_irqrestore(&(musb->lock), flags);
1128 
1129 	musb_dbg(musb, "%s", musb_ep->end_point.name);
1130 
1131 	return status;
1132 }
1133 
1134 /*
1135  * Allocate a request for an endpoint.
1136  * Reused by ep0 code.
1137  */
1138 struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1139 {
1140 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1141 	struct musb_request	*request = NULL;
1142 
1143 	request = kzalloc(sizeof *request, gfp_flags);
1144 	if (!request)
1145 		return NULL;
1146 
1147 	request->request.dma = DMA_ADDR_INVALID;
1148 	request->epnum = musb_ep->current_epnum;
1149 	request->ep = musb_ep;
1150 
1151 	trace_musb_req_alloc(request);
1152 	return &request->request;
1153 }
1154 
1155 /*
1156  * Free a request
1157  * Reused by ep0 code.
1158  */
1159 void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1160 {
1161 	struct musb_request *request = to_musb_request(req);
1162 
1163 	trace_musb_req_free(request);
1164 	kfree(request);
1165 }
1166 
1167 static LIST_HEAD(buffers);
1168 
1169 struct free_record {
1170 	struct list_head	list;
1171 	struct device		*dev;
1172 	unsigned		bytes;
1173 	dma_addr_t		dma;
1174 };
1175 
1176 /*
1177  * Context: controller locked, IRQs blocked.
1178  */
1179 void musb_ep_restart(struct musb *musb, struct musb_request *req)
1180 {
1181 	trace_musb_req_start(req);
1182 	musb_ep_select(musb->mregs, req->epnum);
1183 	if (req->tx)
1184 		txstate(musb, req);
1185 	else
1186 		rxstate(musb, req);
1187 }
1188 
1189 static int musb_ep_restart_resume_work(struct musb *musb, void *data)
1190 {
1191 	struct musb_request *req = data;
1192 
1193 	musb_ep_restart(musb, req);
1194 
1195 	return 0;
1196 }
1197 
1198 static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1199 			gfp_t gfp_flags)
1200 {
1201 	struct musb_ep		*musb_ep;
1202 	struct musb_request	*request;
1203 	struct musb		*musb;
1204 	int			status;
1205 	unsigned long		lockflags;
1206 
1207 	if (!ep || !req)
1208 		return -EINVAL;
1209 	if (!req->buf)
1210 		return -ENODATA;
1211 
1212 	musb_ep = to_musb_ep(ep);
1213 	musb = musb_ep->musb;
1214 
1215 	request = to_musb_request(req);
1216 	request->musb = musb;
1217 
1218 	if (request->ep != musb_ep)
1219 		return -EINVAL;
1220 
1221 	status = pm_runtime_get(musb->controller);
1222 	if ((status != -EINPROGRESS) && status < 0) {
1223 		dev_err(musb->controller,
1224 			"pm runtime get failed in %s\n",
1225 			__func__);
1226 		pm_runtime_put_noidle(musb->controller);
1227 
1228 		return status;
1229 	}
1230 	status = 0;
1231 
1232 	trace_musb_req_enq(request);
1233 
1234 	/* request is mine now... */
1235 	request->request.actual = 0;
1236 	request->request.status = -EINPROGRESS;
1237 	request->epnum = musb_ep->current_epnum;
1238 	request->tx = musb_ep->is_in;
1239 
1240 	map_dma_buffer(request, musb, musb_ep);
1241 
1242 	spin_lock_irqsave(&musb->lock, lockflags);
1243 
1244 	/* don't queue if the ep is down */
1245 	if (!musb_ep->desc) {
1246 		musb_dbg(musb, "req %p queued to %s while ep %s",
1247 				req, ep->name, "disabled");
1248 		status = -ESHUTDOWN;
1249 		unmap_dma_buffer(request, musb);
1250 		goto unlock;
1251 	}
1252 
1253 	/* add request to the list */
1254 	list_add_tail(&request->list, &musb_ep->req_list);
1255 
1256 	/* it this is the head of the queue, start i/o ... */
1257 	if (!musb_ep->busy && &request->list == musb_ep->req_list.next) {
1258 		status = musb_queue_resume_work(musb,
1259 						musb_ep_restart_resume_work,
1260 						request);
1261 		if (status < 0)
1262 			dev_err(musb->controller, "%s resume work: %i\n",
1263 				__func__, status);
1264 	}
1265 
1266 unlock:
1267 	spin_unlock_irqrestore(&musb->lock, lockflags);
1268 	pm_runtime_mark_last_busy(musb->controller);
1269 	pm_runtime_put_autosuspend(musb->controller);
1270 
1271 	return status;
1272 }
1273 
1274 static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1275 {
1276 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1277 	struct musb_request	*req = to_musb_request(request);
1278 	struct musb_request	*r;
1279 	unsigned long		flags;
1280 	int			status = 0;
1281 	struct musb		*musb = musb_ep->musb;
1282 
1283 	if (!ep || !request || req->ep != musb_ep)
1284 		return -EINVAL;
1285 
1286 	trace_musb_req_deq(req);
1287 
1288 	spin_lock_irqsave(&musb->lock, flags);
1289 
1290 	list_for_each_entry(r, &musb_ep->req_list, list) {
1291 		if (r == req)
1292 			break;
1293 	}
1294 	if (r != req) {
1295 		dev_err(musb->controller, "request %p not queued to %s\n",
1296 				request, ep->name);
1297 		status = -EINVAL;
1298 		goto done;
1299 	}
1300 
1301 	/* if the hardware doesn't have the request, easy ... */
1302 	if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1303 		musb_g_giveback(musb_ep, request, -ECONNRESET);
1304 
1305 	/* ... else abort the dma transfer ... */
1306 	else if (is_dma_capable() && musb_ep->dma) {
1307 		struct dma_controller	*c = musb->dma_controller;
1308 
1309 		musb_ep_select(musb->mregs, musb_ep->current_epnum);
1310 		if (c->channel_abort)
1311 			status = c->channel_abort(musb_ep->dma);
1312 		else
1313 			status = -EBUSY;
1314 		if (status == 0)
1315 			musb_g_giveback(musb_ep, request, -ECONNRESET);
1316 	} else {
1317 		/* NOTE: by sticking to easily tested hardware/driver states,
1318 		 * we leave counting of in-flight packets imprecise.
1319 		 */
1320 		musb_g_giveback(musb_ep, request, -ECONNRESET);
1321 	}
1322 
1323 done:
1324 	spin_unlock_irqrestore(&musb->lock, flags);
1325 	return status;
1326 }
1327 
1328 /*
1329  * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1330  * data but will queue requests.
1331  *
1332  * exported to ep0 code
1333  */
1334 static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1335 {
1336 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1337 	u8			epnum = musb_ep->current_epnum;
1338 	struct musb		*musb = musb_ep->musb;
1339 	void __iomem		*epio = musb->endpoints[epnum].regs;
1340 	void __iomem		*mbase;
1341 	unsigned long		flags;
1342 	u16			csr;
1343 	struct musb_request	*request;
1344 	int			status = 0;
1345 
1346 	if (!ep)
1347 		return -EINVAL;
1348 	mbase = musb->mregs;
1349 
1350 	spin_lock_irqsave(&musb->lock, flags);
1351 
1352 	if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1353 		status = -EINVAL;
1354 		goto done;
1355 	}
1356 
1357 	musb_ep_select(mbase, epnum);
1358 
1359 	request = next_request(musb_ep);
1360 	if (value) {
1361 		if (request) {
1362 			musb_dbg(musb, "request in progress, cannot halt %s",
1363 			    ep->name);
1364 			status = -EAGAIN;
1365 			goto done;
1366 		}
1367 		/* Cannot portably stall with non-empty FIFO */
1368 		if (musb_ep->is_in) {
1369 			csr = musb_readw(epio, MUSB_TXCSR);
1370 			if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1371 				musb_dbg(musb, "FIFO busy, cannot halt %s",
1372 						ep->name);
1373 				status = -EAGAIN;
1374 				goto done;
1375 			}
1376 		}
1377 	} else
1378 		musb_ep->wedged = 0;
1379 
1380 	/* set/clear the stall and toggle bits */
1381 	musb_dbg(musb, "%s: %s stall", ep->name, value ? "set" : "clear");
1382 	if (musb_ep->is_in) {
1383 		csr = musb_readw(epio, MUSB_TXCSR);
1384 		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 
1406 	/* maybe start the first request in the queue */
1407 	if (!musb_ep->busy && !value && request) {
1408 		musb_dbg(musb, "restarting the request");
1409 		musb_ep_restart(musb, request);
1410 	}
1411 
1412 done:
1413 	spin_unlock_irqrestore(&musb->lock, flags);
1414 	return status;
1415 }
1416 
1417 /*
1418  * Sets the halt feature with the clear requests ignored
1419  */
1420 static int musb_gadget_set_wedge(struct usb_ep *ep)
1421 {
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 
1432 static 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 
1455 static 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;
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 	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
1472 
1473 	if (musb_ep->is_in) {
1474 		csr = musb_readw(epio, MUSB_TXCSR);
1475 		if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1476 			csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1477 			/*
1478 			 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1479 			 * to interrupt current FIFO loading, but not flushing
1480 			 * the already loaded ones.
1481 			 */
1482 			csr &= ~MUSB_TXCSR_TXPKTRDY;
1483 			musb_writew(epio, MUSB_TXCSR, csr);
1484 			/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1485 			musb_writew(epio, MUSB_TXCSR, csr);
1486 		}
1487 	} else {
1488 		csr = musb_readw(epio, MUSB_RXCSR);
1489 		csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1490 		musb_writew(epio, MUSB_RXCSR, csr);
1491 		musb_writew(epio, MUSB_RXCSR, csr);
1492 	}
1493 
1494 	/* re-enable interrupt */
1495 	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
1496 	spin_unlock_irqrestore(&musb->lock, flags);
1497 }
1498 
1499 static const struct usb_ep_ops musb_ep_ops = {
1500 	.enable		= musb_gadget_enable,
1501 	.disable	= musb_gadget_disable,
1502 	.alloc_request	= musb_alloc_request,
1503 	.free_request	= musb_free_request,
1504 	.queue		= musb_gadget_queue,
1505 	.dequeue	= musb_gadget_dequeue,
1506 	.set_halt	= musb_gadget_set_halt,
1507 	.set_wedge	= musb_gadget_set_wedge,
1508 	.fifo_status	= musb_gadget_fifo_status,
1509 	.fifo_flush	= musb_gadget_fifo_flush
1510 };
1511 
1512 /* ----------------------------------------------------------------------- */
1513 
1514 static int musb_gadget_get_frame(struct usb_gadget *gadget)
1515 {
1516 	struct musb	*musb = gadget_to_musb(gadget);
1517 
1518 	return (int)musb_readw(musb->mregs, MUSB_FRAME);
1519 }
1520 
1521 static int musb_gadget_wakeup(struct usb_gadget *gadget)
1522 {
1523 	struct musb	*musb = gadget_to_musb(gadget);
1524 	void __iomem	*mregs = musb->mregs;
1525 	unsigned long	flags;
1526 	int		status = -EINVAL;
1527 	u8		power, devctl;
1528 	int		retries;
1529 
1530 	spin_lock_irqsave(&musb->lock, flags);
1531 
1532 	switch (musb->xceiv->otg->state) {
1533 	case OTG_STATE_B_PERIPHERAL:
1534 		/* NOTE:  OTG state machine doesn't include B_SUSPENDED;
1535 		 * that's part of the standard usb 1.1 state machine, and
1536 		 * doesn't affect OTG transitions.
1537 		 */
1538 		if (musb->may_wakeup && musb->is_suspended)
1539 			break;
1540 		goto done;
1541 	case OTG_STATE_B_IDLE:
1542 		/* Start SRP ... OTG not required. */
1543 		devctl = musb_readb(mregs, MUSB_DEVCTL);
1544 		musb_dbg(musb, "Sending SRP: devctl: %02x", devctl);
1545 		devctl |= MUSB_DEVCTL_SESSION;
1546 		musb_writeb(mregs, MUSB_DEVCTL, devctl);
1547 		devctl = musb_readb(mregs, MUSB_DEVCTL);
1548 		retries = 100;
1549 		while (!(devctl & MUSB_DEVCTL_SESSION)) {
1550 			devctl = musb_readb(mregs, MUSB_DEVCTL);
1551 			if (retries-- < 1)
1552 				break;
1553 		}
1554 		retries = 10000;
1555 		while (devctl & MUSB_DEVCTL_SESSION) {
1556 			devctl = musb_readb(mregs, MUSB_DEVCTL);
1557 			if (retries-- < 1)
1558 				break;
1559 		}
1560 
1561 		spin_unlock_irqrestore(&musb->lock, flags);
1562 		otg_start_srp(musb->xceiv->otg);
1563 		spin_lock_irqsave(&musb->lock, flags);
1564 
1565 		/* Block idling for at least 1s */
1566 		musb_platform_try_idle(musb,
1567 			jiffies + msecs_to_jiffies(1 * HZ));
1568 
1569 		status = 0;
1570 		goto done;
1571 	default:
1572 		musb_dbg(musb, "Unhandled wake: %s",
1573 			usb_otg_state_string(musb->xceiv->otg->state));
1574 		goto done;
1575 	}
1576 
1577 	status = 0;
1578 
1579 	power = musb_readb(mregs, MUSB_POWER);
1580 	power |= MUSB_POWER_RESUME;
1581 	musb_writeb(mregs, MUSB_POWER, power);
1582 	musb_dbg(musb, "issue wakeup");
1583 
1584 	/* FIXME do this next chunk in a timer callback, no udelay */
1585 	mdelay(2);
1586 
1587 	power = musb_readb(mregs, MUSB_POWER);
1588 	power &= ~MUSB_POWER_RESUME;
1589 	musb_writeb(mregs, MUSB_POWER, power);
1590 done:
1591 	spin_unlock_irqrestore(&musb->lock, flags);
1592 	return status;
1593 }
1594 
1595 static int
1596 musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1597 {
1598 	gadget->is_selfpowered = !!is_selfpowered;
1599 	return 0;
1600 }
1601 
1602 static void musb_pullup(struct musb *musb, int is_on)
1603 {
1604 	u8 power;
1605 
1606 	power = musb_readb(musb->mregs, MUSB_POWER);
1607 	if (is_on)
1608 		power |= MUSB_POWER_SOFTCONN;
1609 	else
1610 		power &= ~MUSB_POWER_SOFTCONN;
1611 
1612 	/* FIXME if on, HdrcStart; if off, HdrcStop */
1613 
1614 	musb_dbg(musb, "gadget D+ pullup %s",
1615 		is_on ? "on" : "off");
1616 	musb_writeb(musb->mregs, MUSB_POWER, power);
1617 }
1618 
1619 #if 0
1620 static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1621 {
1622 	musb_dbg(musb, "<= %s =>\n", __func__);
1623 
1624 	/*
1625 	 * FIXME iff driver's softconnect flag is set (as it is during probe,
1626 	 * though that can clear it), just musb_pullup().
1627 	 */
1628 
1629 	return -EINVAL;
1630 }
1631 #endif
1632 
1633 static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1634 {
1635 	struct musb	*musb = gadget_to_musb(gadget);
1636 
1637 	if (!musb->xceiv->set_power)
1638 		return -EOPNOTSUPP;
1639 	return usb_phy_set_power(musb->xceiv, mA);
1640 }
1641 
1642 static void musb_gadget_work(struct work_struct *work)
1643 {
1644 	struct musb *musb;
1645 	unsigned long flags;
1646 
1647 	musb = container_of(work, struct musb, gadget_work.work);
1648 	pm_runtime_get_sync(musb->controller);
1649 	spin_lock_irqsave(&musb->lock, flags);
1650 	musb_pullup(musb, musb->softconnect);
1651 	spin_unlock_irqrestore(&musb->lock, flags);
1652 	pm_runtime_mark_last_busy(musb->controller);
1653 	pm_runtime_put_autosuspend(musb->controller);
1654 }
1655 
1656 static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1657 {
1658 	struct musb	*musb = gadget_to_musb(gadget);
1659 	unsigned long	flags;
1660 
1661 	is_on = !!is_on;
1662 
1663 	/* NOTE: this assumes we are sensing vbus; we'd rather
1664 	 * not pullup unless the B-session is active.
1665 	 */
1666 	spin_lock_irqsave(&musb->lock, flags);
1667 	if (is_on != musb->softconnect) {
1668 		musb->softconnect = is_on;
1669 		schedule_delayed_work(&musb->gadget_work, 0);
1670 	}
1671 	spin_unlock_irqrestore(&musb->lock, flags);
1672 
1673 	return 0;
1674 }
1675 
1676 static int musb_gadget_start(struct usb_gadget *g,
1677 		struct usb_gadget_driver *driver);
1678 static int musb_gadget_stop(struct usb_gadget *g);
1679 
1680 static const struct usb_gadget_ops musb_gadget_operations = {
1681 	.get_frame		= musb_gadget_get_frame,
1682 	.wakeup			= musb_gadget_wakeup,
1683 	.set_selfpowered	= musb_gadget_set_self_powered,
1684 	/* .vbus_session		= musb_gadget_vbus_session, */
1685 	.vbus_draw		= musb_gadget_vbus_draw,
1686 	.pullup			= musb_gadget_pullup,
1687 	.udc_start		= musb_gadget_start,
1688 	.udc_stop		= musb_gadget_stop,
1689 };
1690 
1691 /* ----------------------------------------------------------------------- */
1692 
1693 /* Registration */
1694 
1695 /* Only this registration code "knows" the rule (from USB standards)
1696  * about there being only one external upstream port.  It assumes
1697  * all peripheral ports are external...
1698  */
1699 
1700 static void
1701 init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1702 {
1703 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1704 
1705 	memset(ep, 0, sizeof *ep);
1706 
1707 	ep->current_epnum = epnum;
1708 	ep->musb = musb;
1709 	ep->hw_ep = hw_ep;
1710 	ep->is_in = is_in;
1711 
1712 	INIT_LIST_HEAD(&ep->req_list);
1713 
1714 	sprintf(ep->name, "ep%d%s", epnum,
1715 			(!epnum || hw_ep->is_shared_fifo) ? "" : (
1716 				is_in ? "in" : "out"));
1717 	ep->end_point.name = ep->name;
1718 	INIT_LIST_HEAD(&ep->end_point.ep_list);
1719 	if (!epnum) {
1720 		usb_ep_set_maxpacket_limit(&ep->end_point, 64);
1721 		ep->end_point.caps.type_control = true;
1722 		ep->end_point.ops = &musb_g_ep0_ops;
1723 		musb->g.ep0 = &ep->end_point;
1724 	} else {
1725 		if (is_in)
1726 			usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx);
1727 		else
1728 			usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx);
1729 		ep->end_point.caps.type_iso = true;
1730 		ep->end_point.caps.type_bulk = true;
1731 		ep->end_point.caps.type_int = true;
1732 		ep->end_point.ops = &musb_ep_ops;
1733 		list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1734 	}
1735 
1736 	if (!epnum || hw_ep->is_shared_fifo) {
1737 		ep->end_point.caps.dir_in = true;
1738 		ep->end_point.caps.dir_out = true;
1739 	} else if (is_in)
1740 		ep->end_point.caps.dir_in = true;
1741 	else
1742 		ep->end_point.caps.dir_out = true;
1743 }
1744 
1745 /*
1746  * Initialize the endpoints exposed to peripheral drivers, with backlinks
1747  * to the rest of the driver state.
1748  */
1749 static inline void musb_g_init_endpoints(struct musb *musb)
1750 {
1751 	u8			epnum;
1752 	struct musb_hw_ep	*hw_ep;
1753 	unsigned		count = 0;
1754 
1755 	/* initialize endpoint list just once */
1756 	INIT_LIST_HEAD(&(musb->g.ep_list));
1757 
1758 	for (epnum = 0, hw_ep = musb->endpoints;
1759 			epnum < musb->nr_endpoints;
1760 			epnum++, hw_ep++) {
1761 		if (hw_ep->is_shared_fifo /* || !epnum */) {
1762 			init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1763 			count++;
1764 		} else {
1765 			if (hw_ep->max_packet_sz_tx) {
1766 				init_peripheral_ep(musb, &hw_ep->ep_in,
1767 							epnum, 1);
1768 				count++;
1769 			}
1770 			if (hw_ep->max_packet_sz_rx) {
1771 				init_peripheral_ep(musb, &hw_ep->ep_out,
1772 							epnum, 0);
1773 				count++;
1774 			}
1775 		}
1776 	}
1777 }
1778 
1779 /* called once during driver setup to initialize and link into
1780  * the driver model; memory is zeroed.
1781  */
1782 int musb_gadget_setup(struct musb *musb)
1783 {
1784 	int status;
1785 
1786 	/* REVISIT minor race:  if (erroneously) setting up two
1787 	 * musb peripherals at the same time, only the bus lock
1788 	 * is probably held.
1789 	 */
1790 
1791 	musb->g.ops = &musb_gadget_operations;
1792 	musb->g.max_speed = USB_SPEED_HIGH;
1793 	musb->g.speed = USB_SPEED_UNKNOWN;
1794 
1795 	MUSB_DEV_MODE(musb);
1796 	musb->xceiv->otg->default_a = 0;
1797 	musb->xceiv->otg->state = OTG_STATE_B_IDLE;
1798 
1799 	/* this "gadget" abstracts/virtualizes the controller */
1800 	musb->g.name = musb_driver_name;
1801 #if IS_ENABLED(CONFIG_USB_MUSB_DUAL_ROLE)
1802 	musb->g.is_otg = 1;
1803 #elif IS_ENABLED(CONFIG_USB_MUSB_GADGET)
1804 	musb->g.is_otg = 0;
1805 #endif
1806 	INIT_DELAYED_WORK(&musb->gadget_work, musb_gadget_work);
1807 	musb_g_init_endpoints(musb);
1808 
1809 	musb->is_active = 0;
1810 	musb_platform_try_idle(musb, 0);
1811 
1812 	status = usb_add_gadget_udc(musb->controller, &musb->g);
1813 	if (status)
1814 		goto err;
1815 
1816 	return 0;
1817 err:
1818 	musb->g.dev.parent = NULL;
1819 	device_unregister(&musb->g.dev);
1820 	return status;
1821 }
1822 
1823 void musb_gadget_cleanup(struct musb *musb)
1824 {
1825 	if (musb->port_mode == MUSB_PORT_MODE_HOST)
1826 		return;
1827 
1828 	cancel_delayed_work_sync(&musb->gadget_work);
1829 	usb_del_gadget_udc(&musb->g);
1830 }
1831 
1832 /*
1833  * Register the gadget driver. Used by gadget drivers when
1834  * registering themselves with the controller.
1835  *
1836  * -EINVAL something went wrong (not driver)
1837  * -EBUSY another gadget is already using the controller
1838  * -ENOMEM no memory to perform the operation
1839  *
1840  * @param driver the gadget driver
1841  * @return <0 if error, 0 if everything is fine
1842  */
1843 static int musb_gadget_start(struct usb_gadget *g,
1844 		struct usb_gadget_driver *driver)
1845 {
1846 	struct musb		*musb = gadget_to_musb(g);
1847 	struct usb_otg		*otg = musb->xceiv->otg;
1848 	unsigned long		flags;
1849 	int			retval = 0;
1850 
1851 	if (driver->max_speed < USB_SPEED_HIGH) {
1852 		retval = -EINVAL;
1853 		goto err;
1854 	}
1855 
1856 	pm_runtime_get_sync(musb->controller);
1857 
1858 	musb->softconnect = 0;
1859 	musb->gadget_driver = driver;
1860 
1861 	spin_lock_irqsave(&musb->lock, flags);
1862 	musb->is_active = 1;
1863 
1864 	otg_set_peripheral(otg, &musb->g);
1865 	musb->xceiv->otg->state = OTG_STATE_B_IDLE;
1866 	spin_unlock_irqrestore(&musb->lock, flags);
1867 
1868 	musb_start(musb);
1869 
1870 	/* REVISIT:  funcall to other code, which also
1871 	 * handles power budgeting ... this way also
1872 	 * ensures HdrcStart is indirectly called.
1873 	 */
1874 	if (musb->xceiv->last_event == USB_EVENT_ID)
1875 		musb_platform_set_vbus(musb, 1);
1876 
1877 	pm_runtime_mark_last_busy(musb->controller);
1878 	pm_runtime_put_autosuspend(musb->controller);
1879 
1880 	return 0;
1881 
1882 err:
1883 	return retval;
1884 }
1885 
1886 /*
1887  * Unregister the gadget driver. Used by gadget drivers when
1888  * unregistering themselves from the controller.
1889  *
1890  * @param driver the gadget driver to unregister
1891  */
1892 static int musb_gadget_stop(struct usb_gadget *g)
1893 {
1894 	struct musb	*musb = gadget_to_musb(g);
1895 	unsigned long	flags;
1896 
1897 	pm_runtime_get_sync(musb->controller);
1898 
1899 	/*
1900 	 * REVISIT always use otg_set_peripheral() here too;
1901 	 * this needs to shut down the OTG engine.
1902 	 */
1903 
1904 	spin_lock_irqsave(&musb->lock, flags);
1905 
1906 	musb_hnp_stop(musb);
1907 
1908 	(void) musb_gadget_vbus_draw(&musb->g, 0);
1909 
1910 	musb->xceiv->otg->state = OTG_STATE_UNDEFINED;
1911 	musb_stop(musb);
1912 	otg_set_peripheral(musb->xceiv->otg, NULL);
1913 
1914 	musb->is_active = 0;
1915 	musb->gadget_driver = NULL;
1916 	musb_platform_try_idle(musb, 0);
1917 	spin_unlock_irqrestore(&musb->lock, flags);
1918 
1919 	/*
1920 	 * FIXME we need to be able to register another
1921 	 * gadget driver here and have everything work;
1922 	 * that currently misbehaves.
1923 	 */
1924 
1925 	/* Force check of devctl register for PM runtime */
1926 	schedule_delayed_work(&musb->irq_work, 0);
1927 
1928 	pm_runtime_mark_last_busy(musb->controller);
1929 	pm_runtime_put_autosuspend(musb->controller);
1930 
1931 	return 0;
1932 }
1933 
1934 /* ----------------------------------------------------------------------- */
1935 
1936 /* lifecycle operations called through plat_uds.c */
1937 
1938 void musb_g_resume(struct musb *musb)
1939 {
1940 	musb->is_suspended = 0;
1941 	switch (musb->xceiv->otg->state) {
1942 	case OTG_STATE_B_IDLE:
1943 		break;
1944 	case OTG_STATE_B_WAIT_ACON:
1945 	case OTG_STATE_B_PERIPHERAL:
1946 		musb->is_active = 1;
1947 		if (musb->gadget_driver && musb->gadget_driver->resume) {
1948 			spin_unlock(&musb->lock);
1949 			musb->gadget_driver->resume(&musb->g);
1950 			spin_lock(&musb->lock);
1951 		}
1952 		break;
1953 	default:
1954 		WARNING("unhandled RESUME transition (%s)\n",
1955 				usb_otg_state_string(musb->xceiv->otg->state));
1956 	}
1957 }
1958 
1959 /* called when SOF packets stop for 3+ msec */
1960 void musb_g_suspend(struct musb *musb)
1961 {
1962 	u8	devctl;
1963 
1964 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1965 	musb_dbg(musb, "musb_g_suspend: devctl %02x", devctl);
1966 
1967 	switch (musb->xceiv->otg->state) {
1968 	case OTG_STATE_B_IDLE:
1969 		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
1970 			musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
1971 		break;
1972 	case OTG_STATE_B_PERIPHERAL:
1973 		musb->is_suspended = 1;
1974 		if (musb->gadget_driver && musb->gadget_driver->suspend) {
1975 			spin_unlock(&musb->lock);
1976 			musb->gadget_driver->suspend(&musb->g);
1977 			spin_lock(&musb->lock);
1978 		}
1979 		break;
1980 	default:
1981 		/* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
1982 		 * A_PERIPHERAL may need care too
1983 		 */
1984 		WARNING("unhandled SUSPEND transition (%s)",
1985 				usb_otg_state_string(musb->xceiv->otg->state));
1986 	}
1987 }
1988 
1989 /* Called during SRP */
1990 void musb_g_wakeup(struct musb *musb)
1991 {
1992 	musb_gadget_wakeup(&musb->g);
1993 }
1994 
1995 /* called when VBUS drops below session threshold, and in other cases */
1996 void musb_g_disconnect(struct musb *musb)
1997 {
1998 	void __iomem	*mregs = musb->mregs;
1999 	u8	devctl = musb_readb(mregs, MUSB_DEVCTL);
2000 
2001 	musb_dbg(musb, "musb_g_disconnect: devctl %02x", devctl);
2002 
2003 	/* clear HR */
2004 	musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2005 
2006 	/* don't draw vbus until new b-default session */
2007 	(void) musb_gadget_vbus_draw(&musb->g, 0);
2008 
2009 	musb->g.speed = USB_SPEED_UNKNOWN;
2010 	if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2011 		spin_unlock(&musb->lock);
2012 		musb->gadget_driver->disconnect(&musb->g);
2013 		spin_lock(&musb->lock);
2014 	}
2015 
2016 	switch (musb->xceiv->otg->state) {
2017 	default:
2018 		musb_dbg(musb, "Unhandled disconnect %s, setting a_idle",
2019 			usb_otg_state_string(musb->xceiv->otg->state));
2020 		musb->xceiv->otg->state = OTG_STATE_A_IDLE;
2021 		MUSB_HST_MODE(musb);
2022 		break;
2023 	case OTG_STATE_A_PERIPHERAL:
2024 		musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
2025 		MUSB_HST_MODE(musb);
2026 		break;
2027 	case OTG_STATE_B_WAIT_ACON:
2028 	case OTG_STATE_B_HOST:
2029 	case OTG_STATE_B_PERIPHERAL:
2030 	case OTG_STATE_B_IDLE:
2031 		musb->xceiv->otg->state = OTG_STATE_B_IDLE;
2032 		break;
2033 	case OTG_STATE_B_SRP_INIT:
2034 		break;
2035 	}
2036 
2037 	musb->is_active = 0;
2038 }
2039 
2040 void musb_g_reset(struct musb *musb)
2041 __releases(musb->lock)
2042 __acquires(musb->lock)
2043 {
2044 	void __iomem	*mbase = musb->mregs;
2045 	u8		devctl = musb_readb(mbase, MUSB_DEVCTL);
2046 	u8		power;
2047 
2048 	musb_dbg(musb, "<== %s driver '%s'",
2049 			(devctl & MUSB_DEVCTL_BDEVICE)
2050 				? "B-Device" : "A-Device",
2051 			musb->gadget_driver
2052 				? musb->gadget_driver->driver.name
2053 				: NULL
2054 			);
2055 
2056 	/* report reset, if we didn't already (flushing EP state) */
2057 	if (musb->gadget_driver && musb->g.speed != USB_SPEED_UNKNOWN) {
2058 		spin_unlock(&musb->lock);
2059 		usb_gadget_udc_reset(&musb->g, musb->gadget_driver);
2060 		spin_lock(&musb->lock);
2061 	}
2062 
2063 	/* clear HR */
2064 	else if (devctl & MUSB_DEVCTL_HR)
2065 		musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2066 
2067 
2068 	/* what speed did we negotiate? */
2069 	power = musb_readb(mbase, MUSB_POWER);
2070 	musb->g.speed = (power & MUSB_POWER_HSMODE)
2071 			? USB_SPEED_HIGH : USB_SPEED_FULL;
2072 
2073 	/* start in USB_STATE_DEFAULT */
2074 	musb->is_active = 1;
2075 	musb->is_suspended = 0;
2076 	MUSB_DEV_MODE(musb);
2077 	musb->address = 0;
2078 	musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2079 
2080 	musb->may_wakeup = 0;
2081 	musb->g.b_hnp_enable = 0;
2082 	musb->g.a_alt_hnp_support = 0;
2083 	musb->g.a_hnp_support = 0;
2084 	musb->g.quirk_zlp_not_supp = 1;
2085 
2086 	/* Normal reset, as B-Device;
2087 	 * or else after HNP, as A-Device
2088 	 */
2089 	if (!musb->g.is_otg) {
2090 		/* USB device controllers that are not OTG compatible
2091 		 * may not have DEVCTL register in silicon.
2092 		 * In that case, do not rely on devctl for setting
2093 		 * peripheral mode.
2094 		 */
2095 		musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
2096 		musb->g.is_a_peripheral = 0;
2097 	} else if (devctl & MUSB_DEVCTL_BDEVICE) {
2098 		musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
2099 		musb->g.is_a_peripheral = 0;
2100 	} else {
2101 		musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL;
2102 		musb->g.is_a_peripheral = 1;
2103 	}
2104 
2105 	/* start with default limits on VBUS power draw */
2106 	(void) musb_gadget_vbus_draw(&musb->g, 8);
2107 }
2108