blob: ea65326ab6264492ea668ddd047b360965a5ad19 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Ilya Yanok06bb9202012-11-06 13:48:21 +00002/*
3 * MUSB OTG peripheral driver ep0 handling
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) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
Ilya Yanok06bb9202012-11-06 13:48:21 +00009 */
10
Ilya Yanok06bb9202012-11-06 13:48:21 +000011#ifndef __UBOOT__
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +000014#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/timer.h>
17#include <linux/spinlock.h>
18#include <linux/device.h>
19#include <linux/interrupt.h>
20#else
Sean Anderson3e464862020-10-04 21:39:54 -040021#include <dm.h>
22#include <dm/device_compat.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060023#include <linux/printk.h>
Masahiro Yamada34d20f82015-07-13 13:17:05 +090024#include <asm/processor.h>
Sean Anderson3e464862020-10-04 21:39:54 -040025#include "linux-compat.h"
Ilya Yanok06bb9202012-11-06 13:48:21 +000026#endif
27
28#include "musb_core.h"
29
30/* ep0 is always musb->endpoints[0].ep_in */
31#define next_ep0_request(musb) next_in_request(&(musb)->endpoints[0])
32
33/*
34 * locking note: we use only the controller lock, for simpler correctness.
35 * It's always held with IRQs blocked.
36 *
37 * It protects the ep0 request queue as well as ep0_state, not just the
38 * controller and indexed registers. And that lock stays held unless it
39 * needs to be dropped to allow reentering this driver ... like upcalls to
40 * the gadget driver, or adjusting endpoint halt status.
41 */
42
43static char *decode_ep0stage(u8 stage)
44{
45 switch (stage) {
46 case MUSB_EP0_STAGE_IDLE: return "idle";
47 case MUSB_EP0_STAGE_SETUP: return "setup";
48 case MUSB_EP0_STAGE_TX: return "in";
49 case MUSB_EP0_STAGE_RX: return "out";
50 case MUSB_EP0_STAGE_ACKWAIT: return "wait";
51 case MUSB_EP0_STAGE_STATUSIN: return "in/status";
52 case MUSB_EP0_STAGE_STATUSOUT: return "out/status";
53 default: return "?";
54 }
55}
56
57/* handle a standard GET_STATUS request
58 * Context: caller holds controller lock
59 */
60static int service_tx_status_request(
61 struct musb *musb,
62 const struct usb_ctrlrequest *ctrlrequest)
63{
64 void __iomem *mbase = musb->mregs;
65 int handled = 1;
66 u8 result[2], epnum = 0;
67 const u8 recip = ctrlrequest->bRequestType & USB_RECIP_MASK;
68
69 result[1] = 0;
70
71 switch (recip) {
72 case USB_RECIP_DEVICE:
73 result[0] = musb->is_self_powered << USB_DEVICE_SELF_POWERED;
74 result[0] |= musb->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
75 if (musb->g.is_otg) {
76 result[0] |= musb->g.b_hnp_enable
77 << USB_DEVICE_B_HNP_ENABLE;
78 result[0] |= musb->g.a_alt_hnp_support
79 << USB_DEVICE_A_ALT_HNP_SUPPORT;
80 result[0] |= musb->g.a_hnp_support
81 << USB_DEVICE_A_HNP_SUPPORT;
82 }
83 break;
84
85 case USB_RECIP_INTERFACE:
86 result[0] = 0;
87 break;
88
89 case USB_RECIP_ENDPOINT: {
90 int is_in;
91 struct musb_ep *ep;
92 u16 tmp;
93 void __iomem *regs;
94
95 epnum = (u8) ctrlrequest->wIndex;
96 if (!epnum) {
97 result[0] = 0;
98 break;
99 }
100
101 is_in = epnum & USB_DIR_IN;
102 if (is_in) {
103 epnum &= 0x0f;
104 ep = &musb->endpoints[epnum].ep_in;
105 } else {
106 ep = &musb->endpoints[epnum].ep_out;
107 }
108 regs = musb->endpoints[epnum].regs;
109
110 if (epnum >= MUSB_C_NUM_EPS || !ep->desc) {
111 handled = -EINVAL;
112 break;
113 }
114
115 musb_ep_select(mbase, epnum);
116 if (is_in)
117 tmp = musb_readw(regs, MUSB_TXCSR)
118 & MUSB_TXCSR_P_SENDSTALL;
119 else
120 tmp = musb_readw(regs, MUSB_RXCSR)
121 & MUSB_RXCSR_P_SENDSTALL;
122 musb_ep_select(mbase, 0);
123
124 result[0] = tmp ? 1 : 0;
125 } break;
126
127 default:
128 /* class, vendor, etc ... delegate */
129 handled = 0;
130 break;
131 }
132
133 /* fill up the fifo; caller updates csr0 */
134 if (handled > 0) {
135 u16 len = le16_to_cpu(ctrlrequest->wLength);
136
137 if (len > 2)
138 len = 2;
139 musb_write_fifo(&musb->endpoints[0], len, result);
140 }
141
142 return handled;
143}
144
145/*
146 * handle a control-IN request, the end0 buffer contains the current request
147 * that is supposed to be a standard control request. Assumes the fifo to
148 * be at least 2 bytes long.
149 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100150 * Return: 0 if the request was NOT HANDLED,
Ilya Yanok06bb9202012-11-06 13:48:21 +0000151 * < 0 when error
152 * > 0 when the request is processed
153 *
154 * Context: caller holds controller lock
155 */
156static int
157service_in_request(struct musb *musb, const struct usb_ctrlrequest *ctrlrequest)
158{
159 int handled = 0; /* not handled */
160
161 if ((ctrlrequest->bRequestType & USB_TYPE_MASK)
162 == USB_TYPE_STANDARD) {
163 switch (ctrlrequest->bRequest) {
164 case USB_REQ_GET_STATUS:
165 handled = service_tx_status_request(musb,
166 ctrlrequest);
167 break;
168
169 /* case USB_REQ_SYNC_FRAME: */
170
171 default:
172 break;
173 }
174 }
175 return handled;
176}
177
178/*
179 * Context: caller holds controller lock
180 */
181static void musb_g_ep0_giveback(struct musb *musb, struct usb_request *req)
182{
183 musb_g_giveback(&musb->endpoints[0].ep_in, req, 0);
184}
185
186/*
187 * Tries to start B-device HNP negotiation if enabled via sysfs
188 */
189static inline void musb_try_b_hnp_enable(struct musb *musb)
190{
191 void __iomem *mbase = musb->mregs;
192 u8 devctl;
193
194 dev_dbg(musb->controller, "HNP: Setting HR\n");
195 devctl = musb_readb(mbase, MUSB_DEVCTL);
196 musb_writeb(mbase, MUSB_DEVCTL, devctl | MUSB_DEVCTL_HR);
197}
198
199/*
200 * Handle all control requests with no DATA stage, including standard
201 * requests such as:
202 * USB_REQ_SET_CONFIGURATION, USB_REQ_SET_INTERFACE, unrecognized
203 * always delegated to the gadget driver
204 * USB_REQ_SET_ADDRESS, USB_REQ_CLEAR_FEATURE, USB_REQ_SET_FEATURE
205 * always handled here, except for class/vendor/... features
206 *
207 * Context: caller holds controller lock
208 */
209static int
210service_zero_data_request(struct musb *musb,
211 struct usb_ctrlrequest *ctrlrequest)
212__releases(musb->lock)
213__acquires(musb->lock)
214{
215 int handled = -EINVAL;
216 void __iomem *mbase = musb->mregs;
217 const u8 recip = ctrlrequest->bRequestType & USB_RECIP_MASK;
218
219 /* the gadget driver handles everything except what we MUST handle */
220 if ((ctrlrequest->bRequestType & USB_TYPE_MASK)
221 == USB_TYPE_STANDARD) {
222 switch (ctrlrequest->bRequest) {
223 case USB_REQ_SET_ADDRESS:
224 /* change it after the status stage */
225 musb->set_address = true;
226 musb->address = (u8) (ctrlrequest->wValue & 0x7f);
227 handled = 1;
228 break;
229
230 case USB_REQ_CLEAR_FEATURE:
231 switch (recip) {
232 case USB_RECIP_DEVICE:
233 if (ctrlrequest->wValue
234 != USB_DEVICE_REMOTE_WAKEUP)
235 break;
236 musb->may_wakeup = 0;
237 handled = 1;
238 break;
239 case USB_RECIP_INTERFACE:
240 break;
241 case USB_RECIP_ENDPOINT:{
242 const u8 epnum =
243 ctrlrequest->wIndex & 0x0f;
244 struct musb_ep *musb_ep;
245 struct musb_hw_ep *ep;
246 struct musb_request *request;
247 void __iomem *regs;
248 int is_in;
249 u16 csr;
250
251 if (epnum == 0 || epnum >= MUSB_C_NUM_EPS ||
252 ctrlrequest->wValue != USB_ENDPOINT_HALT)
253 break;
254
255 ep = musb->endpoints + epnum;
256 regs = ep->regs;
257 is_in = ctrlrequest->wIndex & USB_DIR_IN;
258 if (is_in)
259 musb_ep = &ep->ep_in;
260 else
261 musb_ep = &ep->ep_out;
262 if (!musb_ep->desc)
263 break;
264
265 handled = 1;
266 /* Ignore request if endpoint is wedged */
267 if (musb_ep->wedged)
268 break;
269
270 musb_ep_select(mbase, epnum);
271 if (is_in) {
272 csr = musb_readw(regs, MUSB_TXCSR);
273 csr |= MUSB_TXCSR_CLRDATATOG |
274 MUSB_TXCSR_P_WZC_BITS;
275 csr &= ~(MUSB_TXCSR_P_SENDSTALL |
276 MUSB_TXCSR_P_SENTSTALL |
277 MUSB_TXCSR_TXPKTRDY);
278 musb_writew(regs, MUSB_TXCSR, csr);
279 } else {
280 csr = musb_readw(regs, MUSB_RXCSR);
281 csr |= MUSB_RXCSR_CLRDATATOG |
282 MUSB_RXCSR_P_WZC_BITS;
283 csr &= ~(MUSB_RXCSR_P_SENDSTALL |
284 MUSB_RXCSR_P_SENTSTALL);
285 musb_writew(regs, MUSB_RXCSR, csr);
286 }
287
288 /* Maybe start the first request in the queue */
289 request = next_request(musb_ep);
290 if (!musb_ep->busy && request) {
291 dev_dbg(musb->controller, "restarting the request\n");
292 musb_ep_restart(musb, request);
293 }
294
295 /* select ep0 again */
296 musb_ep_select(mbase, 0);
297 } break;
298 default:
299 /* class, vendor, etc ... delegate */
300 handled = 0;
301 break;
302 }
303 break;
304
305 case USB_REQ_SET_FEATURE:
306 switch (recip) {
307 case USB_RECIP_DEVICE:
308 handled = 1;
309 switch (ctrlrequest->wValue) {
310 case USB_DEVICE_REMOTE_WAKEUP:
311 musb->may_wakeup = 1;
312 break;
313 case USB_DEVICE_TEST_MODE:
314 if (musb->g.speed != USB_SPEED_HIGH)
315 goto stall;
316 if (ctrlrequest->wIndex & 0xff)
317 goto stall;
318
319 switch (ctrlrequest->wIndex >> 8) {
320 case 1:
321 pr_debug("TEST_J\n");
322 /* TEST_J */
323 musb->test_mode_nr =
324 MUSB_TEST_J;
325 break;
326 case 2:
327 /* TEST_K */
328 pr_debug("TEST_K\n");
329 musb->test_mode_nr =
330 MUSB_TEST_K;
331 break;
332 case 3:
333 /* TEST_SE0_NAK */
334 pr_debug("TEST_SE0_NAK\n");
335 musb->test_mode_nr =
336 MUSB_TEST_SE0_NAK;
337 break;
338 case 4:
339 /* TEST_PACKET */
340 pr_debug("TEST_PACKET\n");
341 musb->test_mode_nr =
342 MUSB_TEST_PACKET;
343 break;
344
345 case 0xc0:
346 /* TEST_FORCE_HS */
347 pr_debug("TEST_FORCE_HS\n");
348 musb->test_mode_nr =
349 MUSB_TEST_FORCE_HS;
350 break;
351 case 0xc1:
352 /* TEST_FORCE_FS */
353 pr_debug("TEST_FORCE_FS\n");
354 musb->test_mode_nr =
355 MUSB_TEST_FORCE_FS;
356 break;
357 case 0xc2:
358 /* TEST_FIFO_ACCESS */
359 pr_debug("TEST_FIFO_ACCESS\n");
360 musb->test_mode_nr =
361 MUSB_TEST_FIFO_ACCESS;
362 break;
363 case 0xc3:
364 /* TEST_FORCE_HOST */
365 pr_debug("TEST_FORCE_HOST\n");
366 musb->test_mode_nr =
367 MUSB_TEST_FORCE_HOST;
368 break;
369 default:
370 goto stall;
371 }
372
373 /* enter test mode after irq */
374 if (handled > 0)
375 musb->test_mode = true;
376 break;
377 case USB_DEVICE_B_HNP_ENABLE:
378 if (!musb->g.is_otg)
379 goto stall;
380 musb->g.b_hnp_enable = 1;
381 musb_try_b_hnp_enable(musb);
382 break;
383 case USB_DEVICE_A_HNP_SUPPORT:
384 if (!musb->g.is_otg)
385 goto stall;
386 musb->g.a_hnp_support = 1;
387 break;
388 case USB_DEVICE_A_ALT_HNP_SUPPORT:
389 if (!musb->g.is_otg)
390 goto stall;
391 musb->g.a_alt_hnp_support = 1;
392 break;
393 case USB_DEVICE_DEBUG_MODE:
394 handled = 0;
395 break;
396stall:
397 default:
398 handled = -EINVAL;
399 break;
400 }
401 break;
402
403 case USB_RECIP_INTERFACE:
404 break;
405
406 case USB_RECIP_ENDPOINT:{
407 const u8 epnum =
408 ctrlrequest->wIndex & 0x0f;
409 struct musb_ep *musb_ep;
410 struct musb_hw_ep *ep;
411 void __iomem *regs;
412 int is_in;
413 u16 csr;
414
415 if (epnum == 0 || epnum >= MUSB_C_NUM_EPS ||
416 ctrlrequest->wValue != USB_ENDPOINT_HALT)
417 break;
418
419 ep = musb->endpoints + epnum;
420 regs = ep->regs;
421 is_in = ctrlrequest->wIndex & USB_DIR_IN;
422 if (is_in)
423 musb_ep = &ep->ep_in;
424 else
425 musb_ep = &ep->ep_out;
426 if (!musb_ep->desc)
427 break;
428
429 musb_ep_select(mbase, epnum);
430 if (is_in) {
431 csr = musb_readw(regs, MUSB_TXCSR);
432 if (csr & MUSB_TXCSR_FIFONOTEMPTY)
433 csr |= MUSB_TXCSR_FLUSHFIFO;
434 csr |= MUSB_TXCSR_P_SENDSTALL
435 | MUSB_TXCSR_CLRDATATOG
436 | MUSB_TXCSR_P_WZC_BITS;
437 musb_writew(regs, MUSB_TXCSR, csr);
438 } else {
439 csr = musb_readw(regs, MUSB_RXCSR);
440 csr |= MUSB_RXCSR_P_SENDSTALL
441 | MUSB_RXCSR_FLUSHFIFO
442 | MUSB_RXCSR_CLRDATATOG
443 | MUSB_RXCSR_P_WZC_BITS;
444 musb_writew(regs, MUSB_RXCSR, csr);
445 }
446
447 /* select ep0 again */
448 musb_ep_select(mbase, 0);
449 handled = 1;
450 } break;
451
452 default:
453 /* class, vendor, etc ... delegate */
454 handled = 0;
455 break;
456 }
457 break;
458 default:
459 /* delegate SET_CONFIGURATION, etc */
460 handled = 0;
461 }
462 } else
463 handled = 0;
464 return handled;
465}
466
467/* we have an ep0out data packet
468 * Context: caller holds controller lock
469 */
470static void ep0_rxstate(struct musb *musb)
471{
472 void __iomem *regs = musb->control_ep->regs;
473 struct musb_request *request;
474 struct usb_request *req;
475 u16 count, csr;
476
477 request = next_ep0_request(musb);
478 req = &request->request;
479
480 /* read packet and ack; or stall because of gadget driver bug:
481 * should have provided the rx buffer before setup() returned.
482 */
483 if (req) {
484 void *buf = req->buf + req->actual;
485 unsigned len = req->length - req->actual;
486
487 /* read the buffer */
488 count = musb_readb(regs, MUSB_COUNT0);
489 if (count > len) {
490 req->status = -EOVERFLOW;
491 count = len;
492 }
493 musb_read_fifo(&musb->endpoints[0], count, buf);
494 req->actual += count;
495 csr = MUSB_CSR0_P_SVDRXPKTRDY;
496 if (count < 64 || req->actual == req->length) {
497 musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
498 csr |= MUSB_CSR0_P_DATAEND;
499 } else
500 req = NULL;
501 } else
502 csr = MUSB_CSR0_P_SVDRXPKTRDY | MUSB_CSR0_P_SENDSTALL;
503
Ilya Yanok06bb9202012-11-06 13:48:21 +0000504 /* Completion handler may choose to stall, e.g. because the
505 * message just received holds invalid data.
506 */
507 if (req) {
508 musb->ackpend = csr;
509 musb_g_ep0_giveback(musb, req);
510 if (!musb->ackpend)
511 return;
512 musb->ackpend = 0;
513 }
514 musb_ep_select(musb->mregs, 0);
515 musb_writew(regs, MUSB_CSR0, csr);
516}
517
518/*
519 * transmitting to the host (IN), this code might be called from IRQ
520 * and from kernel thread.
521 *
522 * Context: caller holds controller lock
523 */
524static void ep0_txstate(struct musb *musb)
525{
526 void __iomem *regs = musb->control_ep->regs;
527 struct musb_request *req = next_ep0_request(musb);
528 struct usb_request *request;
529 u16 csr = MUSB_CSR0_TXPKTRDY;
530 u8 *fifo_src;
531 u8 fifo_count;
532
533 if (!req) {
534 /* WARN_ON(1); */
535 dev_dbg(musb->controller, "odd; csr0 %04x\n", musb_readw(regs, MUSB_CSR0));
536 return;
537 }
538
539 request = &req->request;
540
541 /* load the data */
542 fifo_src = (u8 *) request->buf + request->actual;
543 fifo_count = min((unsigned) MUSB_EP0_FIFOSIZE,
544 request->length - request->actual);
545 musb_write_fifo(&musb->endpoints[0], fifo_count, fifo_src);
546 request->actual += fifo_count;
547
548 /* update the flags */
549 if (fifo_count < MUSB_MAX_END0_PACKET
550 || (request->actual == request->length
551 && !request->zero)) {
552 musb->ep0_state = MUSB_EP0_STAGE_STATUSOUT;
553 csr |= MUSB_CSR0_P_DATAEND;
554 } else
555 request = NULL;
556
Heiko Schocher0c9fe562014-04-10 07:08:05 +0200557 /* send it out, triggering a "txpktrdy cleared" irq */
558 musb_ep_select(musb->mregs, 0);
559 musb_writew(regs, MUSB_CSR0, csr);
560
Ilya Yanok06bb9202012-11-06 13:48:21 +0000561 /* report completions as soon as the fifo's loaded; there's no
562 * win in waiting till this last packet gets acked. (other than
563 * very precise fault reporting, needed by USB TMC; possible with
564 * this hardware, but not usable from portable gadget drivers.)
565 */
566 if (request) {
567 musb->ackpend = csr;
568 musb_g_ep0_giveback(musb, request);
569 if (!musb->ackpend)
570 return;
571 musb->ackpend = 0;
572 }
Ilya Yanok06bb9202012-11-06 13:48:21 +0000573}
574
575/*
576 * Read a SETUP packet (struct usb_ctrlrequest) from the hardware.
577 * Fields are left in USB byte-order.
578 *
579 * Context: caller holds controller lock.
580 */
581static void
582musb_read_setup(struct musb *musb, struct usb_ctrlrequest *req)
583{
584 struct musb_request *r;
585 void __iomem *regs = musb->control_ep->regs;
586
587 musb_read_fifo(&musb->endpoints[0], sizeof *req, (u8 *)req);
588
589 /* NOTE: earlier 2.6 versions changed setup packets to host
590 * order, but now USB packets always stay in USB byte order.
591 */
592 dev_dbg(musb->controller, "SETUP req%02x.%02x v%04x i%04x l%d\n",
593 req->bRequestType,
594 req->bRequest,
595 le16_to_cpu(req->wValue),
596 le16_to_cpu(req->wIndex),
597 le16_to_cpu(req->wLength));
598
599 /* clean up any leftover transfers */
600 r = next_ep0_request(musb);
601 if (r)
602 musb_g_ep0_giveback(musb, &r->request);
603
604 /* For zero-data requests we want to delay the STATUS stage to
605 * avoid SETUPEND errors. If we read data (OUT), delay accepting
606 * packets until there's a buffer to store them in.
607 *
608 * If we write data, the controller acts happier if we enable
609 * the TX FIFO right away, and give the controller a moment
610 * to switch modes...
611 */
612 musb->set_address = false;
613 musb->ackpend = MUSB_CSR0_P_SVDRXPKTRDY;
614 if (req->wLength == 0) {
615 if (req->bRequestType & USB_DIR_IN)
616 musb->ackpend |= MUSB_CSR0_TXPKTRDY;
617 musb->ep0_state = MUSB_EP0_STAGE_ACKWAIT;
618 } else if (req->bRequestType & USB_DIR_IN) {
619 musb->ep0_state = MUSB_EP0_STAGE_TX;
620 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SVDRXPKTRDY);
621 while ((musb_readw(regs, MUSB_CSR0)
622 & MUSB_CSR0_RXPKTRDY) != 0)
623 cpu_relax();
624 musb->ackpend = 0;
625 } else
626 musb->ep0_state = MUSB_EP0_STAGE_RX;
627}
628
629static int
630forward_to_driver(struct musb *musb, const struct usb_ctrlrequest *ctrlrequest)
631__releases(musb->lock)
632__acquires(musb->lock)
633{
634 int retval;
635 if (!musb->gadget_driver)
636 return -EOPNOTSUPP;
637 spin_unlock(&musb->lock);
638 retval = musb->gadget_driver->setup(&musb->g, ctrlrequest);
639 spin_lock(&musb->lock);
640 return retval;
641}
642
643/*
644 * Handle peripheral ep0 interrupt
645 *
646 * Context: irq handler; we won't re-enter the driver that way.
647 */
648irqreturn_t musb_g_ep0_irq(struct musb *musb)
649{
650 u16 csr;
651 u16 len;
652 void __iomem *mbase = musb->mregs;
653 void __iomem *regs = musb->endpoints[0].regs;
654 irqreturn_t retval = IRQ_NONE;
655
656 musb_ep_select(mbase, 0); /* select ep0 */
657 csr = musb_readw(regs, MUSB_CSR0);
658 len = musb_readb(regs, MUSB_COUNT0);
659
660 dev_dbg(musb->controller, "csr %04x, count %d, myaddr %d, ep0stage %s\n",
661 csr, len,
662 musb_readb(mbase, MUSB_FADDR),
663 decode_ep0stage(musb->ep0_state));
664
665 if (csr & MUSB_CSR0_P_DATAEND) {
666 /*
667 * If DATAEND is set we should not call the callback,
668 * hence the status stage is not complete.
669 */
670 return IRQ_HANDLED;
671 }
672
673 /* I sent a stall.. need to acknowledge it now.. */
674 if (csr & MUSB_CSR0_P_SENTSTALL) {
675 musb_writew(regs, MUSB_CSR0,
676 csr & ~MUSB_CSR0_P_SENTSTALL);
677 retval = IRQ_HANDLED;
678 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
679 csr = musb_readw(regs, MUSB_CSR0);
680 }
681
682 /* request ended "early" */
683 if (csr & MUSB_CSR0_P_SETUPEND) {
684 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SVDSETUPEND);
685 retval = IRQ_HANDLED;
686 /* Transition into the early status phase */
687 switch (musb->ep0_state) {
688 case MUSB_EP0_STAGE_TX:
689 musb->ep0_state = MUSB_EP0_STAGE_STATUSOUT;
690 break;
691 case MUSB_EP0_STAGE_RX:
692 musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
693 break;
694 default:
695 ERR("SetupEnd came in a wrong ep0stage %s\n",
696 decode_ep0stage(musb->ep0_state));
697 }
698 csr = musb_readw(regs, MUSB_CSR0);
699 /* NOTE: request may need completion */
700 }
701
702 /* docs from Mentor only describe tx, rx, and idle/setup states.
703 * we need to handle nuances around status stages, and also the
704 * case where status and setup stages come back-to-back ...
705 */
706 switch (musb->ep0_state) {
707
708 case MUSB_EP0_STAGE_TX:
709 /* irq on clearing txpktrdy */
710 if ((csr & MUSB_CSR0_TXPKTRDY) == 0) {
711 ep0_txstate(musb);
712 retval = IRQ_HANDLED;
713 }
714 break;
715
716 case MUSB_EP0_STAGE_RX:
717 /* irq on set rxpktrdy */
718 if (csr & MUSB_CSR0_RXPKTRDY) {
719 ep0_rxstate(musb);
720 retval = IRQ_HANDLED;
721 }
722 break;
723
724 case MUSB_EP0_STAGE_STATUSIN:
725 /* end of sequence #2 (OUT/RX state) or #3 (no data) */
726
727 /* update address (if needed) only @ the end of the
728 * status phase per usb spec, which also guarantees
729 * we get 10 msec to receive this irq... until this
730 * is done we won't see the next packet.
731 */
732 if (musb->set_address) {
733 musb->set_address = false;
734 musb_writeb(mbase, MUSB_FADDR, musb->address);
735 }
736
737 /* enter test mode if needed (exit by reset) */
738 else if (musb->test_mode) {
739 dev_dbg(musb->controller, "entering TESTMODE\n");
740
741 if (MUSB_TEST_PACKET == musb->test_mode_nr)
742 musb_load_testpacket(musb);
743
744 musb_writeb(mbase, MUSB_TESTMODE,
745 musb->test_mode_nr);
746 }
747 /* FALLTHROUGH */
748
749 case MUSB_EP0_STAGE_STATUSOUT:
750 /* end of sequence #1: write to host (TX state) */
751 {
752 struct musb_request *req;
753
754 req = next_ep0_request(musb);
755 if (req)
756 musb_g_ep0_giveback(musb, &req->request);
757 }
758
759 /*
760 * In case when several interrupts can get coalesced,
761 * check to see if we've already received a SETUP packet...
762 */
763 if (csr & MUSB_CSR0_RXPKTRDY)
764 goto setup;
765
766 retval = IRQ_HANDLED;
767 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
768 break;
769
770 case MUSB_EP0_STAGE_IDLE:
771 /*
772 * This state is typically (but not always) indiscernible
773 * from the status states since the corresponding interrupts
774 * tend to happen within too little period of time (with only
775 * a zero-length packet in between) and so get coalesced...
776 */
777 retval = IRQ_HANDLED;
778 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
779 /* FALLTHROUGH */
780
781 case MUSB_EP0_STAGE_SETUP:
782setup:
783 if (csr & MUSB_CSR0_RXPKTRDY) {
784 struct usb_ctrlrequest setup;
785 int handled = 0;
786
787 if (len != 8) {
788 ERR("SETUP packet len %d != 8 ?\n", len);
789 break;
790 }
791 musb_read_setup(musb, &setup);
792 retval = IRQ_HANDLED;
793
794 /* sometimes the RESET won't be reported */
795 if (unlikely(musb->g.speed == USB_SPEED_UNKNOWN)) {
796 u8 power;
797
798 printk(KERN_NOTICE "%s: peripheral reset "
799 "irq lost!\n",
800 musb_driver_name);
801 power = musb_readb(mbase, MUSB_POWER);
802 musb->g.speed = (power & MUSB_POWER_HSMODE)
803 ? USB_SPEED_HIGH : USB_SPEED_FULL;
804
805 }
806
807 switch (musb->ep0_state) {
808
809 /* sequence #3 (no data stage), includes requests
810 * we can't forward (notably SET_ADDRESS and the
811 * device/endpoint feature set/clear operations)
812 * plus SET_CONFIGURATION and others we must
813 */
814 case MUSB_EP0_STAGE_ACKWAIT:
815 handled = service_zero_data_request(
816 musb, &setup);
817
818 /*
819 * We're expecting no data in any case, so
820 * always set the DATAEND bit -- doing this
821 * here helps avoid SetupEnd interrupt coming
822 * in the idle stage when we're stalling...
823 */
824 musb->ackpend |= MUSB_CSR0_P_DATAEND;
825
826 /* status stage might be immediate */
827 if (handled > 0)
828 musb->ep0_state =
829 MUSB_EP0_STAGE_STATUSIN;
830 break;
831
832 /* sequence #1 (IN to host), includes GET_STATUS
833 * requests that we can't forward, GET_DESCRIPTOR
834 * and others that we must
835 */
836 case MUSB_EP0_STAGE_TX:
837 handled = service_in_request(musb, &setup);
838 if (handled > 0) {
839 musb->ackpend = MUSB_CSR0_TXPKTRDY
840 | MUSB_CSR0_P_DATAEND;
841 musb->ep0_state =
842 MUSB_EP0_STAGE_STATUSOUT;
843 }
844 break;
845
846 /* sequence #2 (OUT from host), always forward */
847 default: /* MUSB_EP0_STAGE_RX */
848 break;
849 }
850
851 dev_dbg(musb->controller, "handled %d, csr %04x, ep0stage %s\n",
852 handled, csr,
853 decode_ep0stage(musb->ep0_state));
854
855 /* unless we need to delegate this to the gadget
856 * driver, we know how to wrap this up: csr0 has
857 * not yet been written.
858 */
859 if (handled < 0)
860 goto stall;
861 else if (handled > 0)
862 goto finish;
863
864 handled = forward_to_driver(musb, &setup);
865 if (handled < 0) {
866 musb_ep_select(mbase, 0);
867stall:
868 dev_dbg(musb->controller, "stall (%d)\n", handled);
869 musb->ackpend |= MUSB_CSR0_P_SENDSTALL;
870 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
871finish:
872 musb_writew(regs, MUSB_CSR0,
873 musb->ackpend);
874 musb->ackpend = 0;
875 }
876 }
877 break;
878
879 case MUSB_EP0_STAGE_ACKWAIT:
880 /* This should not happen. But happens with tusb6010 with
881 * g_file_storage and high speed. Do nothing.
882 */
883 retval = IRQ_HANDLED;
884 break;
885
886 default:
887 /* "can't happen" */
Simon Glassb18a9602019-12-29 21:19:12 -0700888 assert_noisy(false);
Ilya Yanok06bb9202012-11-06 13:48:21 +0000889 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_P_SENDSTALL);
890 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
891 break;
892 }
893
894 return retval;
895}
896
Ilya Yanok06bb9202012-11-06 13:48:21 +0000897static int
898musb_g_ep0_enable(struct usb_ep *ep, const struct usb_endpoint_descriptor *desc)
899{
900 /* always enabled */
901 return -EINVAL;
902}
903
904static int musb_g_ep0_disable(struct usb_ep *e)
905{
906 /* always enabled */
907 return -EINVAL;
908}
909
910static int
911musb_g_ep0_queue(struct usb_ep *e, struct usb_request *r, gfp_t gfp_flags)
912{
913 struct musb_ep *ep;
914 struct musb_request *req;
915 struct musb *musb;
916 int status;
917 unsigned long lockflags;
918 void __iomem *regs;
919
920 if (!e || !r)
921 return -EINVAL;
922
923 ep = to_musb_ep(e);
924 musb = ep->musb;
925 regs = musb->control_ep->regs;
926
927 req = to_musb_request(r);
928 req->musb = musb;
929 req->request.actual = 0;
930 req->request.status = -EINPROGRESS;
931 req->tx = ep->is_in;
932
933 spin_lock_irqsave(&musb->lock, lockflags);
934
935 if (!list_empty(&ep->req_list)) {
936 status = -EBUSY;
937 goto cleanup;
938 }
939
940 switch (musb->ep0_state) {
941 case MUSB_EP0_STAGE_RX: /* control-OUT data */
942 case MUSB_EP0_STAGE_TX: /* control-IN data */
943 case MUSB_EP0_STAGE_ACKWAIT: /* zero-length data */
944 status = 0;
945 break;
946 default:
947 dev_dbg(musb->controller, "ep0 request queued in state %d\n",
948 musb->ep0_state);
949 status = -EINVAL;
950 goto cleanup;
951 }
952
953 /* add request to the list */
954 list_add_tail(&req->list, &ep->req_list);
955
956 dev_dbg(musb->controller, "queue to %s (%s), length=%d\n",
957 ep->name, ep->is_in ? "IN/TX" : "OUT/RX",
958 req->request.length);
959
960 musb_ep_select(musb->mregs, 0);
961
962 /* sequence #1, IN ... start writing the data */
963 if (musb->ep0_state == MUSB_EP0_STAGE_TX)
964 ep0_txstate(musb);
965
966 /* sequence #3, no-data ... issue IN status */
967 else if (musb->ep0_state == MUSB_EP0_STAGE_ACKWAIT) {
968 if (req->request.length)
969 status = -EINVAL;
970 else {
971 musb->ep0_state = MUSB_EP0_STAGE_STATUSIN;
972 musb_writew(regs, MUSB_CSR0,
973 musb->ackpend | MUSB_CSR0_P_DATAEND);
974 musb->ackpend = 0;
975 musb_g_ep0_giveback(ep->musb, r);
976 }
977
978 /* else for sequence #2 (OUT), caller provides a buffer
979 * before the next packet arrives. deferred responses
980 * (after SETUP is acked) are racey.
981 */
982 } else if (musb->ackpend) {
983 musb_writew(regs, MUSB_CSR0, musb->ackpend);
984 musb->ackpend = 0;
985 }
986
987cleanup:
988 spin_unlock_irqrestore(&musb->lock, lockflags);
989 return status;
990}
991
992static int musb_g_ep0_dequeue(struct usb_ep *ep, struct usb_request *req)
993{
994 /* we just won't support this */
995 return -EINVAL;
996}
997
998static int musb_g_ep0_halt(struct usb_ep *e, int value)
999{
1000 struct musb_ep *ep;
1001 struct musb *musb;
1002 void __iomem *base, *regs;
1003 unsigned long flags;
1004 int status;
1005 u16 csr;
1006
1007 if (!e || !value)
1008 return -EINVAL;
1009
1010 ep = to_musb_ep(e);
1011 musb = ep->musb;
1012 base = musb->mregs;
1013 regs = musb->control_ep->regs;
1014 status = 0;
1015
1016 spin_lock_irqsave(&musb->lock, flags);
1017
1018 if (!list_empty(&ep->req_list)) {
1019 status = -EBUSY;
1020 goto cleanup;
1021 }
1022
1023 musb_ep_select(base, 0);
1024 csr = musb->ackpend;
1025
1026 switch (musb->ep0_state) {
1027
1028 /* Stalls are usually issued after parsing SETUP packet, either
1029 * directly in irq context from setup() or else later.
1030 */
1031 case MUSB_EP0_STAGE_TX: /* control-IN data */
1032 case MUSB_EP0_STAGE_ACKWAIT: /* STALL for zero-length data */
1033 case MUSB_EP0_STAGE_RX: /* control-OUT data */
1034 csr = musb_readw(regs, MUSB_CSR0);
1035 /* FALLTHROUGH */
1036
1037 /* It's also OK to issue stalls during callbacks when a non-empty
1038 * DATA stage buffer has been read (or even written).
1039 */
1040 case MUSB_EP0_STAGE_STATUSIN: /* control-OUT status */
1041 case MUSB_EP0_STAGE_STATUSOUT: /* control-IN status */
1042
1043 csr |= MUSB_CSR0_P_SENDSTALL;
1044 musb_writew(regs, MUSB_CSR0, csr);
1045 musb->ep0_state = MUSB_EP0_STAGE_IDLE;
1046 musb->ackpend = 0;
1047 break;
1048 default:
1049 dev_dbg(musb->controller, "ep0 can't halt in state %d\n", musb->ep0_state);
1050 status = -EINVAL;
1051 }
1052
1053cleanup:
1054 spin_unlock_irqrestore(&musb->lock, flags);
1055 return status;
1056}
1057
1058const struct usb_ep_ops musb_g_ep0_ops = {
1059 .enable = musb_g_ep0_enable,
1060 .disable = musb_g_ep0_disable,
1061 .alloc_request = musb_alloc_request,
1062 .free_request = musb_free_request,
1063 .queue = musb_g_ep0_queue,
1064 .dequeue = musb_g_ep0_dequeue,
1065 .set_halt = musb_g_ep0_halt,
1066};