i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / usb / gadget / goku_udc.c
blob59777490a19ae481294487f16b963b6d4affa834
1 /*
2 * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
4 * Copyright (C) 2000-2002 Lineo
5 * by Stuart Lynne, Tom Rushworth, and Bruce Balden
6 * Copyright (C) 2002 Toshiba Corporation
7 * Copyright (C) 2003 MontaVista Software (source@mvista.com)
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
15 * This device has ep0 and three semi-configurable bulk/interrupt endpoints.
17 * - Endpoint numbering is fixed: ep{1,2,3}-bulk
18 * - Gadget drivers can choose ep maxpacket (8/16/32/64)
19 * - Gadget drivers can choose direction (IN, OUT)
20 * - DMA works with ep1 (OUT transfers) and ep2 (IN transfers).
23 // #define VERBOSE /* extra debug messages (success too) */
24 // #define USB_TRACE /* packet-level success messages */
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/timer.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
38 #include <linux/device.h>
39 #include <linux/usb/ch9.h>
40 #include <linux/usb/gadget.h>
41 #include <linux/prefetch.h>
43 #include <asm/byteorder.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/system.h>
47 #include <asm/unaligned.h>
50 #include "goku_udc.h"
52 #define DRIVER_DESC "TC86C001 USB Device Controller"
53 #define DRIVER_VERSION "30-Oct 2003"
55 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
57 static const char driver_name [] = "goku_udc";
58 static const char driver_desc [] = DRIVER_DESC;
60 MODULE_AUTHOR("source@mvista.com");
61 MODULE_DESCRIPTION(DRIVER_DESC);
62 MODULE_LICENSE("GPL");
66 * IN dma behaves ok under testing, though the IN-dma abort paths don't
67 * seem to behave quite as expected. Used by default.
69 * OUT dma documents design problems handling the common "short packet"
70 * transfer termination policy; it couldn't be enabled by default, even
71 * if the OUT-dma abort problems had a resolution.
73 static unsigned use_dma = 1;
75 #if 0
76 //#include <linux/moduleparam.h>
77 /* "modprobe goku_udc use_dma=1" etc
78 * 0 to disable dma
79 * 1 to use IN dma only (normal operation)
80 * 2 to use IN and OUT dma
82 module_param(use_dma, uint, S_IRUGO);
83 #endif
85 /*-------------------------------------------------------------------------*/
87 static void nuke(struct goku_ep *, int status);
89 static inline void
90 command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
92 writel(COMMAND_EP(epnum) | command, &regs->Command);
93 udelay(300);
96 static int
97 goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
99 struct goku_udc *dev;
100 struct goku_ep *ep;
101 u32 mode;
102 u16 max;
103 unsigned long flags;
105 ep = container_of(_ep, struct goku_ep, ep);
106 if (!_ep || !desc || ep->desc
107 || desc->bDescriptorType != USB_DT_ENDPOINT)
108 return -EINVAL;
109 dev = ep->dev;
110 if (ep == &dev->ep[0])
111 return -EINVAL;
112 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
113 return -ESHUTDOWN;
114 if (ep->num != usb_endpoint_num(desc))
115 return -EINVAL;
117 switch (usb_endpoint_type(desc)) {
118 case USB_ENDPOINT_XFER_BULK:
119 case USB_ENDPOINT_XFER_INT:
120 break;
121 default:
122 return -EINVAL;
125 if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
126 != EPxSTATUS_EP_INVALID)
127 return -EBUSY;
129 /* enabling the no-toggle interrupt mode would need an api hook */
130 mode = 0;
131 max = get_unaligned_le16(&desc->wMaxPacketSize);
132 switch (max) {
133 case 64: mode++;
134 case 32: mode++;
135 case 16: mode++;
136 case 8: mode <<= 3;
137 break;
138 default:
139 return -EINVAL;
141 mode |= 2 << 1; /* bulk, or intr-with-toggle */
143 /* ep1/ep2 dma direction is chosen early; it works in the other
144 * direction, with pio. be cautious with out-dma.
146 ep->is_in = usb_endpoint_dir_in(desc);
147 if (ep->is_in) {
148 mode |= 1;
149 ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
150 } else {
151 ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
152 if (ep->dma)
153 DBG(dev, "%s out-dma hides short packets\n",
154 ep->ep.name);
157 spin_lock_irqsave(&ep->dev->lock, flags);
159 /* ep1 and ep2 can do double buffering and/or dma */
160 if (ep->num < 3) {
161 struct goku_udc_regs __iomem *regs = ep->dev->regs;
162 u32 tmp;
164 /* double buffer except (for now) with pio in */
165 tmp = ((ep->dma || !ep->is_in)
166 ? 0x10 /* double buffered */
167 : 0x11 /* single buffer */
168 ) << ep->num;
169 tmp |= readl(&regs->EPxSingle);
170 writel(tmp, &regs->EPxSingle);
172 tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
173 tmp |= readl(&regs->EPxBCS);
174 writel(tmp, &regs->EPxBCS);
176 writel(mode, ep->reg_mode);
177 command(ep->dev->regs, COMMAND_RESET, ep->num);
178 ep->ep.maxpacket = max;
179 ep->stopped = 0;
180 ep->desc = desc;
181 spin_unlock_irqrestore(&ep->dev->lock, flags);
183 DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
184 ep->is_in ? "IN" : "OUT",
185 ep->dma ? "dma" : "pio",
186 max);
188 return 0;
191 static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
193 struct goku_udc *dev = ep->dev;
195 if (regs) {
196 command(regs, COMMAND_INVALID, ep->num);
197 if (ep->num) {
198 if (ep->num == UDC_MSTWR_ENDPOINT)
199 dev->int_enable &= ~(INT_MSTWREND
200 |INT_MSTWRTMOUT);
201 else if (ep->num == UDC_MSTRD_ENDPOINT)
202 dev->int_enable &= ~INT_MSTRDEND;
203 dev->int_enable &= ~INT_EPxDATASET (ep->num);
204 } else
205 dev->int_enable &= ~INT_EP0;
206 writel(dev->int_enable, &regs->int_enable);
207 readl(&regs->int_enable);
208 if (ep->num < 3) {
209 struct goku_udc_regs __iomem *r = ep->dev->regs;
210 u32 tmp;
212 tmp = readl(&r->EPxSingle);
213 tmp &= ~(0x11 << ep->num);
214 writel(tmp, &r->EPxSingle);
216 tmp = readl(&r->EPxBCS);
217 tmp &= ~(0x11 << ep->num);
218 writel(tmp, &r->EPxBCS);
220 /* reset dma in case we're still using it */
221 if (ep->dma) {
222 u32 master;
224 master = readl(&regs->dma_master) & MST_RW_BITS;
225 if (ep->num == UDC_MSTWR_ENDPOINT) {
226 master &= ~MST_W_BITS;
227 master |= MST_WR_RESET;
228 } else {
229 master &= ~MST_R_BITS;
230 master |= MST_RD_RESET;
232 writel(master, &regs->dma_master);
236 ep->ep.maxpacket = MAX_FIFO_SIZE;
237 ep->desc = NULL;
238 ep->ep.desc = NULL;
239 ep->stopped = 1;
240 ep->irqs = 0;
241 ep->dma = 0;
244 static int goku_ep_disable(struct usb_ep *_ep)
246 struct goku_ep *ep;
247 struct goku_udc *dev;
248 unsigned long flags;
250 ep = container_of(_ep, struct goku_ep, ep);
251 if (!_ep || !ep->desc)
252 return -ENODEV;
253 dev = ep->dev;
254 if (dev->ep0state == EP0_SUSPEND)
255 return -EBUSY;
257 VDBG(dev, "disable %s\n", _ep->name);
259 spin_lock_irqsave(&dev->lock, flags);
260 nuke(ep, -ESHUTDOWN);
261 ep_reset(dev->regs, ep);
262 spin_unlock_irqrestore(&dev->lock, flags);
264 return 0;
267 /*-------------------------------------------------------------------------*/
269 static struct usb_request *
270 goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
272 struct goku_request *req;
274 if (!_ep)
275 return NULL;
276 req = kzalloc(sizeof *req, gfp_flags);
277 if (!req)
278 return NULL;
280 req->req.dma = DMA_ADDR_INVALID;
281 INIT_LIST_HEAD(&req->queue);
282 return &req->req;
285 static void
286 goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
288 struct goku_request *req;
290 if (!_ep || !_req)
291 return;
293 req = container_of(_req, struct goku_request, req);
294 WARN_ON(!list_empty(&req->queue));
295 kfree(req);
298 /*-------------------------------------------------------------------------*/
300 static void
301 done(struct goku_ep *ep, struct goku_request *req, int status)
303 struct goku_udc *dev;
304 unsigned stopped = ep->stopped;
306 list_del_init(&req->queue);
308 if (likely(req->req.status == -EINPROGRESS))
309 req->req.status = status;
310 else
311 status = req->req.status;
313 dev = ep->dev;
314 if (req->mapped) {
315 pci_unmap_single(dev->pdev, req->req.dma, req->req.length,
316 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
317 req->req.dma = DMA_ADDR_INVALID;
318 req->mapped = 0;
321 #ifndef USB_TRACE
322 if (status && status != -ESHUTDOWN)
323 #endif
324 VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
325 ep->ep.name, &req->req, status,
326 req->req.actual, req->req.length);
328 /* don't modify queue heads during completion callback */
329 ep->stopped = 1;
330 spin_unlock(&dev->lock);
331 req->req.complete(&ep->ep, &req->req);
332 spin_lock(&dev->lock);
333 ep->stopped = stopped;
336 /*-------------------------------------------------------------------------*/
338 static inline int
339 write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
341 unsigned length, count;
343 length = min(req->req.length - req->req.actual, max);
344 req->req.actual += length;
346 count = length;
347 while (likely(count--))
348 writel(*buf++, fifo);
349 return length;
352 // return: 0 = still running, 1 = completed, negative = errno
353 static int write_fifo(struct goku_ep *ep, struct goku_request *req)
355 struct goku_udc *dev = ep->dev;
356 u32 tmp;
357 u8 *buf;
358 unsigned count;
359 int is_last;
361 tmp = readl(&dev->regs->DataSet);
362 buf = req->req.buf + req->req.actual;
363 prefetch(buf);
365 dev = ep->dev;
366 if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
367 return -EL2HLT;
369 /* NOTE: just single-buffered PIO-IN for now. */
370 if (unlikely((tmp & DATASET_A(ep->num)) != 0))
371 return 0;
373 /* clear our "packet available" irq */
374 if (ep->num != 0)
375 writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
377 count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
379 /* last packet often short (sometimes a zlp, especially on ep0) */
380 if (unlikely(count != ep->ep.maxpacket)) {
381 writel(~(1<<ep->num), &dev->regs->EOP);
382 if (ep->num == 0) {
383 dev->ep[0].stopped = 1;
384 dev->ep0state = EP0_STATUS;
386 is_last = 1;
387 } else {
388 if (likely(req->req.length != req->req.actual)
389 || req->req.zero)
390 is_last = 0;
391 else
392 is_last = 1;
394 #if 0 /* printk seemed to trash is_last...*/
395 //#ifdef USB_TRACE
396 VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
397 ep->ep.name, count, is_last ? "/last" : "",
398 req->req.length - req->req.actual, req);
399 #endif
401 /* requests complete when all IN data is in the FIFO,
402 * or sometimes later, if a zlp was needed.
404 if (is_last) {
405 done(ep, req, 0);
406 return 1;
409 return 0;
412 static int read_fifo(struct goku_ep *ep, struct goku_request *req)
414 struct goku_udc_regs __iomem *regs;
415 u32 size, set;
416 u8 *buf;
417 unsigned bufferspace, is_short, dbuff;
419 regs = ep->dev->regs;
420 top:
421 buf = req->req.buf + req->req.actual;
422 prefetchw(buf);
424 if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
425 return -EL2HLT;
427 dbuff = (ep->num == 1 || ep->num == 2);
428 do {
429 /* ack dataset irq matching the status we'll handle */
430 if (ep->num != 0)
431 writel(~INT_EPxDATASET(ep->num), &regs->int_status);
433 set = readl(&regs->DataSet) & DATASET_AB(ep->num);
434 size = readl(&regs->EPxSizeLA[ep->num]);
435 bufferspace = req->req.length - req->req.actual;
437 /* usually do nothing without an OUT packet */
438 if (likely(ep->num != 0 || bufferspace != 0)) {
439 if (unlikely(set == 0))
440 break;
441 /* use ep1/ep2 double-buffering for OUT */
442 if (!(size & PACKET_ACTIVE))
443 size = readl(&regs->EPxSizeLB[ep->num]);
444 if (!(size & PACKET_ACTIVE)) /* "can't happen" */
445 break;
446 size &= DATASIZE; /* EPxSizeH == 0 */
448 /* ep0out no-out-data case for set_config, etc */
449 } else
450 size = 0;
452 /* read all bytes from this packet */
453 req->req.actual += size;
454 is_short = (size < ep->ep.maxpacket);
455 #ifdef USB_TRACE
456 VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
457 ep->ep.name, size, is_short ? "/S" : "",
458 req, req->req.actual, req->req.length);
459 #endif
460 while (likely(size-- != 0)) {
461 u8 byte = (u8) readl(ep->reg_fifo);
463 if (unlikely(bufferspace == 0)) {
464 /* this happens when the driver's buffer
465 * is smaller than what the host sent.
466 * discard the extra data in this packet.
468 if (req->req.status != -EOVERFLOW)
469 DBG(ep->dev, "%s overflow %u\n",
470 ep->ep.name, size);
471 req->req.status = -EOVERFLOW;
472 } else {
473 *buf++ = byte;
474 bufferspace--;
478 /* completion */
479 if (unlikely(is_short || req->req.actual == req->req.length)) {
480 if (unlikely(ep->num == 0)) {
481 /* non-control endpoints now usable? */
482 if (ep->dev->req_config)
483 writel(ep->dev->configured
484 ? USBSTATE_CONFIGURED
485 : 0,
486 &regs->UsbState);
487 /* ep0out status stage */
488 writel(~(1<<0), &regs->EOP);
489 ep->stopped = 1;
490 ep->dev->ep0state = EP0_STATUS;
492 done(ep, req, 0);
494 /* empty the second buffer asap */
495 if (dbuff && !list_empty(&ep->queue)) {
496 req = list_entry(ep->queue.next,
497 struct goku_request, queue);
498 goto top;
500 return 1;
502 } while (dbuff);
503 return 0;
506 static inline void
507 pio_irq_enable(struct goku_udc *dev,
508 struct goku_udc_regs __iomem *regs, int epnum)
510 dev->int_enable |= INT_EPxDATASET (epnum);
511 writel(dev->int_enable, &regs->int_enable);
512 /* write may still be posted */
515 static inline void
516 pio_irq_disable(struct goku_udc *dev,
517 struct goku_udc_regs __iomem *regs, int epnum)
519 dev->int_enable &= ~INT_EPxDATASET (epnum);
520 writel(dev->int_enable, &regs->int_enable);
521 /* write may still be posted */
524 static inline void
525 pio_advance(struct goku_ep *ep)
527 struct goku_request *req;
529 if (unlikely(list_empty (&ep->queue)))
530 return;
531 req = list_entry(ep->queue.next, struct goku_request, queue);
532 (ep->is_in ? write_fifo : read_fifo)(ep, req);
536 /*-------------------------------------------------------------------------*/
538 // return: 0 = q running, 1 = q stopped, negative = errno
539 static int start_dma(struct goku_ep *ep, struct goku_request *req)
541 struct goku_udc_regs __iomem *regs = ep->dev->regs;
542 u32 master;
543 u32 start = req->req.dma;
544 u32 end = start + req->req.length - 1;
546 master = readl(&regs->dma_master) & MST_RW_BITS;
548 /* re-init the bits affecting IN dma; careful with zlps */
549 if (likely(ep->is_in)) {
550 if (unlikely(master & MST_RD_ENA)) {
551 DBG (ep->dev, "start, IN active dma %03x!!\n",
552 master);
553 // return -EL2HLT;
555 writel(end, &regs->in_dma_end);
556 writel(start, &regs->in_dma_start);
558 master &= ~MST_R_BITS;
559 if (unlikely(req->req.length == 0))
560 master = MST_RD_ENA | MST_RD_EOPB;
561 else if ((req->req.length % ep->ep.maxpacket) != 0
562 || req->req.zero)
563 master = MST_RD_ENA | MST_EOPB_ENA;
564 else
565 master = MST_RD_ENA | MST_EOPB_DIS;
567 ep->dev->int_enable |= INT_MSTRDEND;
569 /* Goku DMA-OUT merges short packets, which plays poorly with
570 * protocols where short packets mark the transfer boundaries.
571 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
572 * ending transfers after 3 SOFs; we don't turn it on.
574 } else {
575 if (unlikely(master & MST_WR_ENA)) {
576 DBG (ep->dev, "start, OUT active dma %03x!!\n",
577 master);
578 // return -EL2HLT;
580 writel(end, &regs->out_dma_end);
581 writel(start, &regs->out_dma_start);
583 master &= ~MST_W_BITS;
584 master |= MST_WR_ENA | MST_TIMEOUT_DIS;
586 ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
589 writel(master, &regs->dma_master);
590 writel(ep->dev->int_enable, &regs->int_enable);
591 return 0;
594 static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
596 struct goku_request *req;
597 struct goku_udc_regs __iomem *regs = ep->dev->regs;
598 u32 master;
600 master = readl(&regs->dma_master);
602 if (unlikely(list_empty(&ep->queue))) {
603 stop:
604 if (ep->is_in)
605 dev->int_enable &= ~INT_MSTRDEND;
606 else
607 dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
608 writel(dev->int_enable, &regs->int_enable);
609 return;
611 req = list_entry(ep->queue.next, struct goku_request, queue);
613 /* normal hw dma completion (not abort) */
614 if (likely(ep->is_in)) {
615 if (unlikely(master & MST_RD_ENA))
616 return;
617 req->req.actual = readl(&regs->in_dma_current);
618 } else {
619 if (unlikely(master & MST_WR_ENA))
620 return;
622 /* hardware merges short packets, and also hides packet
623 * overruns. a partial packet MAY be in the fifo here.
625 req->req.actual = readl(&regs->out_dma_current);
627 req->req.actual -= req->req.dma;
628 req->req.actual++;
630 #ifdef USB_TRACE
631 VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
632 ep->ep.name, ep->is_in ? "IN" : "OUT",
633 req->req.actual, req->req.length, req);
634 #endif
635 done(ep, req, 0);
636 if (list_empty(&ep->queue))
637 goto stop;
638 req = list_entry(ep->queue.next, struct goku_request, queue);
639 (void) start_dma(ep, req);
642 static void abort_dma(struct goku_ep *ep, int status)
644 struct goku_udc_regs __iomem *regs = ep->dev->regs;
645 struct goku_request *req;
646 u32 curr, master;
648 /* NAK future host requests, hoping the implicit delay lets the
649 * dma engine finish reading (or writing) its latest packet and
650 * empty the dma buffer (up to 16 bytes).
652 * This avoids needing to clean up a partial packet in the fifo;
653 * we can't do that for IN without side effects to HALT and TOGGLE.
655 command(regs, COMMAND_FIFO_DISABLE, ep->num);
656 req = list_entry(ep->queue.next, struct goku_request, queue);
657 master = readl(&regs->dma_master) & MST_RW_BITS;
659 /* FIXME using these resets isn't usably documented. this may
660 * not work unless it's followed by disabling the endpoint.
662 * FIXME the OUT reset path doesn't even behave consistently.
664 if (ep->is_in) {
665 if (unlikely((readl(&regs->dma_master) & MST_RD_ENA) == 0))
666 goto finished;
667 curr = readl(&regs->in_dma_current);
669 writel(curr, &regs->in_dma_end);
670 writel(curr, &regs->in_dma_start);
672 master &= ~MST_R_BITS;
673 master |= MST_RD_RESET;
674 writel(master, &regs->dma_master);
676 if (readl(&regs->dma_master) & MST_RD_ENA)
677 DBG(ep->dev, "IN dma active after reset!\n");
679 } else {
680 if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
681 goto finished;
682 curr = readl(&regs->out_dma_current);
684 writel(curr, &regs->out_dma_end);
685 writel(curr, &regs->out_dma_start);
687 master &= ~MST_W_BITS;
688 master |= MST_WR_RESET;
689 writel(master, &regs->dma_master);
691 if (readl(&regs->dma_master) & MST_WR_ENA)
692 DBG(ep->dev, "OUT dma active after reset!\n");
694 req->req.actual = (curr - req->req.dma) + 1;
695 req->req.status = status;
697 VDBG(ep->dev, "%s %s %s %d/%d\n", __func__, ep->ep.name,
698 ep->is_in ? "IN" : "OUT",
699 req->req.actual, req->req.length);
701 command(regs, COMMAND_FIFO_ENABLE, ep->num);
703 return;
705 finished:
706 /* dma already completed; no abort needed */
707 command(regs, COMMAND_FIFO_ENABLE, ep->num);
708 req->req.actual = req->req.length;
709 req->req.status = 0;
712 /*-------------------------------------------------------------------------*/
714 static int
715 goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
717 struct goku_request *req;
718 struct goku_ep *ep;
719 struct goku_udc *dev;
720 unsigned long flags;
721 int status;
723 /* always require a cpu-view buffer so pio works */
724 req = container_of(_req, struct goku_request, req);
725 if (unlikely(!_req || !_req->complete
726 || !_req->buf || !list_empty(&req->queue)))
727 return -EINVAL;
728 ep = container_of(_ep, struct goku_ep, ep);
729 if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
730 return -EINVAL;
731 dev = ep->dev;
732 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
733 return -ESHUTDOWN;
735 /* can't touch registers when suspended */
736 if (dev->ep0state == EP0_SUSPEND)
737 return -EBUSY;
739 /* set up dma mapping in case the caller didn't */
740 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
741 _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length,
742 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
743 req->mapped = 1;
746 #ifdef USB_TRACE
747 VDBG(dev, "%s queue req %p, len %u buf %p\n",
748 _ep->name, _req, _req->length, _req->buf);
749 #endif
751 spin_lock_irqsave(&dev->lock, flags);
753 _req->status = -EINPROGRESS;
754 _req->actual = 0;
756 /* for ep0 IN without premature status, zlp is required and
757 * writing EOP starts the status stage (OUT).
759 if (unlikely(ep->num == 0 && ep->is_in))
760 _req->zero = 1;
762 /* kickstart this i/o queue? */
763 status = 0;
764 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
765 /* dma: done after dma completion IRQ (or error)
766 * pio: done after last fifo operation
768 if (ep->dma)
769 status = start_dma(ep, req);
770 else
771 status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
773 if (unlikely(status != 0)) {
774 if (status > 0)
775 status = 0;
776 req = NULL;
779 } /* else pio or dma irq handler advances the queue. */
781 if (likely(req != 0))
782 list_add_tail(&req->queue, &ep->queue);
784 if (likely(!list_empty(&ep->queue))
785 && likely(ep->num != 0)
786 && !ep->dma
787 && !(dev->int_enable & INT_EPxDATASET (ep->num)))
788 pio_irq_enable(dev, dev->regs, ep->num);
790 spin_unlock_irqrestore(&dev->lock, flags);
792 /* pci writes may still be posted */
793 return status;
796 /* dequeue ALL requests */
797 static void nuke(struct goku_ep *ep, int status)
799 struct goku_request *req;
801 ep->stopped = 1;
802 if (list_empty(&ep->queue))
803 return;
804 if (ep->dma)
805 abort_dma(ep, status);
806 while (!list_empty(&ep->queue)) {
807 req = list_entry(ep->queue.next, struct goku_request, queue);
808 done(ep, req, status);
812 /* dequeue JUST ONE request */
813 static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
815 struct goku_request *req;
816 struct goku_ep *ep;
817 struct goku_udc *dev;
818 unsigned long flags;
820 ep = container_of(_ep, struct goku_ep, ep);
821 if (!_ep || !_req || (!ep->desc && ep->num != 0))
822 return -EINVAL;
823 dev = ep->dev;
824 if (!dev->driver)
825 return -ESHUTDOWN;
827 /* we can't touch (dma) registers when suspended */
828 if (dev->ep0state == EP0_SUSPEND)
829 return -EBUSY;
831 VDBG(dev, "%s %s %s %s %p\n", __func__, _ep->name,
832 ep->is_in ? "IN" : "OUT",
833 ep->dma ? "dma" : "pio",
834 _req);
836 spin_lock_irqsave(&dev->lock, flags);
838 /* make sure it's actually queued on this endpoint */
839 list_for_each_entry (req, &ep->queue, queue) {
840 if (&req->req == _req)
841 break;
843 if (&req->req != _req) {
844 spin_unlock_irqrestore (&dev->lock, flags);
845 return -EINVAL;
848 if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
849 abort_dma(ep, -ECONNRESET);
850 done(ep, req, -ECONNRESET);
851 dma_advance(dev, ep);
852 } else if (!list_empty(&req->queue))
853 done(ep, req, -ECONNRESET);
854 else
855 req = NULL;
856 spin_unlock_irqrestore(&dev->lock, flags);
858 return req ? 0 : -EOPNOTSUPP;
861 /*-------------------------------------------------------------------------*/
863 static void goku_clear_halt(struct goku_ep *ep)
865 // assert (ep->num !=0)
866 VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
867 command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
868 command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
869 if (ep->stopped) {
870 ep->stopped = 0;
871 if (ep->dma) {
872 struct goku_request *req;
874 if (list_empty(&ep->queue))
875 return;
876 req = list_entry(ep->queue.next, struct goku_request,
877 queue);
878 (void) start_dma(ep, req);
879 } else
880 pio_advance(ep);
884 static int goku_set_halt(struct usb_ep *_ep, int value)
886 struct goku_ep *ep;
887 unsigned long flags;
888 int retval = 0;
890 if (!_ep)
891 return -ENODEV;
892 ep = container_of (_ep, struct goku_ep, ep);
894 if (ep->num == 0) {
895 if (value) {
896 ep->dev->ep0state = EP0_STALL;
897 ep->dev->ep[0].stopped = 1;
898 } else
899 return -EINVAL;
901 /* don't change EPxSTATUS_EP_INVALID to READY */
902 } else if (!ep->desc) {
903 DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
904 return -EINVAL;
907 spin_lock_irqsave(&ep->dev->lock, flags);
908 if (!list_empty(&ep->queue))
909 retval = -EAGAIN;
910 else if (ep->is_in && value
911 /* data in (either) packet buffer? */
912 && (readl(&ep->dev->regs->DataSet)
913 & DATASET_AB(ep->num)))
914 retval = -EAGAIN;
915 else if (!value)
916 goku_clear_halt(ep);
917 else {
918 ep->stopped = 1;
919 VDBG(ep->dev, "%s set halt\n", ep->ep.name);
920 command(ep->dev->regs, COMMAND_STALL, ep->num);
921 readl(ep->reg_status);
923 spin_unlock_irqrestore(&ep->dev->lock, flags);
924 return retval;
927 static int goku_fifo_status(struct usb_ep *_ep)
929 struct goku_ep *ep;
930 struct goku_udc_regs __iomem *regs;
931 u32 size;
933 if (!_ep)
934 return -ENODEV;
935 ep = container_of(_ep, struct goku_ep, ep);
937 /* size is only reported sanely for OUT */
938 if (ep->is_in)
939 return -EOPNOTSUPP;
941 /* ignores 16-byte dma buffer; SizeH == 0 */
942 regs = ep->dev->regs;
943 size = readl(&regs->EPxSizeLA[ep->num]) & DATASIZE;
944 size += readl(&regs->EPxSizeLB[ep->num]) & DATASIZE;
945 VDBG(ep->dev, "%s %s %u\n", __func__, ep->ep.name, size);
946 return size;
949 static void goku_fifo_flush(struct usb_ep *_ep)
951 struct goku_ep *ep;
952 struct goku_udc_regs __iomem *regs;
953 u32 size;
955 if (!_ep)
956 return;
957 ep = container_of(_ep, struct goku_ep, ep);
958 VDBG(ep->dev, "%s %s\n", __func__, ep->ep.name);
960 /* don't change EPxSTATUS_EP_INVALID to READY */
961 if (!ep->desc && ep->num != 0) {
962 DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
963 return;
966 regs = ep->dev->regs;
967 size = readl(&regs->EPxSizeLA[ep->num]);
968 size &= DATASIZE;
970 /* Non-desirable behavior: FIFO_CLEAR also clears the
971 * endpoint halt feature. For OUT, we _could_ just read
972 * the bytes out (PIO, if !ep->dma); for in, no choice.
974 if (size)
975 command(regs, COMMAND_FIFO_CLEAR, ep->num);
978 static struct usb_ep_ops goku_ep_ops = {
979 .enable = goku_ep_enable,
980 .disable = goku_ep_disable,
982 .alloc_request = goku_alloc_request,
983 .free_request = goku_free_request,
985 .queue = goku_queue,
986 .dequeue = goku_dequeue,
988 .set_halt = goku_set_halt,
989 .fifo_status = goku_fifo_status,
990 .fifo_flush = goku_fifo_flush,
993 /*-------------------------------------------------------------------------*/
995 static int goku_get_frame(struct usb_gadget *_gadget)
997 return -EOPNOTSUPP;
1000 static int goku_start(struct usb_gadget_driver *driver,
1001 int (*bind)(struct usb_gadget *));
1002 static int goku_stop(struct usb_gadget_driver *driver);
1004 static const struct usb_gadget_ops goku_ops = {
1005 .get_frame = goku_get_frame,
1006 .start = goku_start,
1007 .stop = goku_stop,
1008 // no remote wakeup
1009 // not selfpowered
1012 /*-------------------------------------------------------------------------*/
1014 static inline char *dmastr(void)
1016 if (use_dma == 0)
1017 return "(dma disabled)";
1018 else if (use_dma == 2)
1019 return "(dma IN and OUT)";
1020 else
1021 return "(dma IN)";
1024 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1026 static const char proc_node_name [] = "driver/udc";
1028 #define FOURBITS "%s%s%s%s"
1029 #define EIGHTBITS FOURBITS FOURBITS
1031 static void
1032 dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1034 int t;
1036 /* int_status is the same format ... */
1037 t = scnprintf(*next, *size,
1038 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1039 label, mask,
1040 (mask & INT_PWRDETECT) ? " power" : "",
1041 (mask & INT_SYSERROR) ? " sys" : "",
1042 (mask & INT_MSTRDEND) ? " in-dma" : "",
1043 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1045 (mask & INT_MSTWREND) ? " out-dma" : "",
1046 (mask & INT_MSTWRSET) ? " wrset" : "",
1047 (mask & INT_ERR) ? " err" : "",
1048 (mask & INT_SOF) ? " sof" : "",
1050 (mask & INT_EP3NAK) ? " ep3nak" : "",
1051 (mask & INT_EP2NAK) ? " ep2nak" : "",
1052 (mask & INT_EP1NAK) ? " ep1nak" : "",
1053 (mask & INT_EP3DATASET) ? " ep3" : "",
1055 (mask & INT_EP2DATASET) ? " ep2" : "",
1056 (mask & INT_EP1DATASET) ? " ep1" : "",
1057 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1058 (mask & INT_STATUS) ? " ep0status" : "",
1060 (mask & INT_SETUP) ? " setup" : "",
1061 (mask & INT_ENDPOINT0) ? " ep0" : "",
1062 (mask & INT_USBRESET) ? " reset" : "",
1063 (mask & INT_SUSPEND) ? " suspend" : "");
1064 *size -= t;
1065 *next += t;
1069 static int
1070 udc_proc_read(char *buffer, char **start, off_t off, int count,
1071 int *eof, void *_dev)
1073 char *buf = buffer;
1074 struct goku_udc *dev = _dev;
1075 struct goku_udc_regs __iomem *regs = dev->regs;
1076 char *next = buf;
1077 unsigned size = count;
1078 unsigned long flags;
1079 int i, t, is_usb_connected;
1080 u32 tmp;
1082 if (off != 0)
1083 return 0;
1085 local_irq_save(flags);
1087 /* basic device status */
1088 tmp = readl(&regs->power_detect);
1089 is_usb_connected = tmp & PW_DETECT;
1090 t = scnprintf(next, size,
1091 "%s - %s\n"
1092 "%s version: %s %s\n"
1093 "Gadget driver: %s\n"
1094 "Host %s, %s\n"
1095 "\n",
1096 pci_name(dev->pdev), driver_desc,
1097 driver_name, DRIVER_VERSION, dmastr(),
1098 dev->driver ? dev->driver->driver.name : "(none)",
1099 is_usb_connected
1100 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1101 : "disconnected",
1102 ({char *state;
1103 switch(dev->ep0state){
1104 case EP0_DISCONNECT: state = "ep0_disconnect"; break;
1105 case EP0_IDLE: state = "ep0_idle"; break;
1106 case EP0_IN: state = "ep0_in"; break;
1107 case EP0_OUT: state = "ep0_out"; break;
1108 case EP0_STATUS: state = "ep0_status"; break;
1109 case EP0_STALL: state = "ep0_stall"; break;
1110 case EP0_SUSPEND: state = "ep0_suspend"; break;
1111 default: state = "ep0_?"; break;
1112 } state; })
1114 size -= t;
1115 next += t;
1117 dump_intmask("int_status", readl(&regs->int_status), &next, &size);
1118 dump_intmask("int_enable", readl(&regs->int_enable), &next, &size);
1120 if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1121 goto done;
1123 /* registers for (active) device and ep0 */
1124 t = scnprintf(next, size, "\nirqs %lu\ndataset %02x "
1125 "single.bcs %02x.%02x state %x addr %u\n",
1126 dev->irqs, readl(&regs->DataSet),
1127 readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1128 readl(&regs->UsbState),
1129 readl(&regs->address));
1130 size -= t;
1131 next += t;
1133 tmp = readl(&regs->dma_master);
1134 t = scnprintf(next, size,
1135 "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1136 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1137 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1138 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1139 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1141 (tmp & MST_RD_EOPB) ? " eopb" : "",
1142 (tmp & MST_RD_RESET) ? " in_reset" : "",
1143 (tmp & MST_WR_RESET) ? " out_reset" : "",
1144 (tmp & MST_RD_ENA) ? " IN" : "",
1146 (tmp & MST_WR_ENA) ? " OUT" : "",
1147 (tmp & MST_CONNECTION)
1148 ? "ep1in/ep2out"
1149 : "ep1out/ep2in");
1150 size -= t;
1151 next += t;
1153 /* dump endpoint queues */
1154 for (i = 0; i < 4; i++) {
1155 struct goku_ep *ep = &dev->ep [i];
1156 struct goku_request *req;
1158 if (i && !ep->desc)
1159 continue;
1161 tmp = readl(ep->reg_status);
1162 t = scnprintf(next, size,
1163 "%s %s max %u %s, irqs %lu, "
1164 "status %02x (%s) " FOURBITS "\n",
1165 ep->ep.name,
1166 ep->is_in ? "in" : "out",
1167 ep->ep.maxpacket,
1168 ep->dma ? "dma" : "pio",
1169 ep->irqs,
1170 tmp, ({ char *s;
1171 switch (tmp & EPxSTATUS_EP_MASK) {
1172 case EPxSTATUS_EP_READY:
1173 s = "ready"; break;
1174 case EPxSTATUS_EP_DATAIN:
1175 s = "packet"; break;
1176 case EPxSTATUS_EP_FULL:
1177 s = "full"; break;
1178 case EPxSTATUS_EP_TX_ERR: // host will retry
1179 s = "tx_err"; break;
1180 case EPxSTATUS_EP_RX_ERR:
1181 s = "rx_err"; break;
1182 case EPxSTATUS_EP_BUSY: /* ep0 only */
1183 s = "busy"; break;
1184 case EPxSTATUS_EP_STALL:
1185 s = "stall"; break;
1186 case EPxSTATUS_EP_INVALID: // these "can't happen"
1187 s = "invalid"; break;
1188 default:
1189 s = "?"; break;
1190 }; s; }),
1191 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1192 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1193 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1194 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1196 if (t <= 0 || t > size)
1197 goto done;
1198 size -= t;
1199 next += t;
1201 if (list_empty(&ep->queue)) {
1202 t = scnprintf(next, size, "\t(nothing queued)\n");
1203 if (t <= 0 || t > size)
1204 goto done;
1205 size -= t;
1206 next += t;
1207 continue;
1209 list_for_each_entry(req, &ep->queue, queue) {
1210 if (ep->dma && req->queue.prev == &ep->queue) {
1211 if (i == UDC_MSTRD_ENDPOINT)
1212 tmp = readl(&regs->in_dma_current);
1213 else
1214 tmp = readl(&regs->out_dma_current);
1215 tmp -= req->req.dma;
1216 tmp++;
1217 } else
1218 tmp = req->req.actual;
1220 t = scnprintf(next, size,
1221 "\treq %p len %u/%u buf %p\n",
1222 &req->req, tmp, req->req.length,
1223 req->req.buf);
1224 if (t <= 0 || t > size)
1225 goto done;
1226 size -= t;
1227 next += t;
1231 done:
1232 local_irq_restore(flags);
1233 *eof = 1;
1234 return count - size;
1237 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1239 /*-------------------------------------------------------------------------*/
1241 static void udc_reinit (struct goku_udc *dev)
1243 static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1245 unsigned i;
1247 INIT_LIST_HEAD (&dev->gadget.ep_list);
1248 dev->gadget.ep0 = &dev->ep [0].ep;
1249 dev->gadget.speed = USB_SPEED_UNKNOWN;
1250 dev->ep0state = EP0_DISCONNECT;
1251 dev->irqs = 0;
1253 for (i = 0; i < 4; i++) {
1254 struct goku_ep *ep = &dev->ep[i];
1256 ep->num = i;
1257 ep->ep.name = names[i];
1258 ep->reg_fifo = &dev->regs->ep_fifo [i];
1259 ep->reg_status = &dev->regs->ep_status [i];
1260 ep->reg_mode = &dev->regs->ep_mode[i];
1262 ep->ep.ops = &goku_ep_ops;
1263 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1264 ep->dev = dev;
1265 INIT_LIST_HEAD (&ep->queue);
1267 ep_reset(NULL, ep);
1270 dev->ep[0].reg_mode = NULL;
1271 dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1272 list_del_init (&dev->ep[0].ep.ep_list);
1275 static void udc_reset(struct goku_udc *dev)
1277 struct goku_udc_regs __iomem *regs = dev->regs;
1279 writel(0, &regs->power_detect);
1280 writel(0, &regs->int_enable);
1281 readl(&regs->int_enable);
1282 dev->int_enable = 0;
1284 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1285 * don't let INT_PWRDETECT sequence begin
1287 udelay(250);
1288 writel(PW_RESETB, &regs->power_detect);
1289 readl(&regs->int_enable);
1292 static void ep0_start(struct goku_udc *dev)
1294 struct goku_udc_regs __iomem *regs = dev->regs;
1295 unsigned i;
1297 VDBG(dev, "%s\n", __func__);
1299 udc_reset(dev);
1300 udc_reinit (dev);
1301 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
1303 /* hw handles set_address, set_feature, get_status; maybe more */
1304 writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
1305 | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
1306 | G_REQMODE_GET_DESC
1307 | G_REQMODE_CLEAR_FEAT
1308 , &regs->reqmode);
1310 for (i = 0; i < 4; i++)
1311 dev->ep[i].irqs = 0;
1313 /* can't modify descriptors after writing UsbReady */
1314 for (i = 0; i < DESC_LEN; i++)
1315 writel(0, &regs->descriptors[i]);
1316 writel(0, &regs->UsbReady);
1318 /* expect ep0 requests when the host drops reset */
1319 writel(PW_RESETB | PW_PULLUP, &regs->power_detect);
1320 dev->int_enable = INT_DEVWIDE | INT_EP0;
1321 writel(dev->int_enable, &dev->regs->int_enable);
1322 readl(&regs->int_enable);
1323 dev->gadget.speed = USB_SPEED_FULL;
1324 dev->ep0state = EP0_IDLE;
1327 static void udc_enable(struct goku_udc *dev)
1329 /* start enumeration now, or after power detect irq */
1330 if (readl(&dev->regs->power_detect) & PW_DETECT)
1331 ep0_start(dev);
1332 else {
1333 DBG(dev, "%s\n", __func__);
1334 dev->int_enable = INT_PWRDETECT;
1335 writel(dev->int_enable, &dev->regs->int_enable);
1339 /*-------------------------------------------------------------------------*/
1341 /* keeping it simple:
1342 * - one bus driver, initted first;
1343 * - one function driver, initted second
1346 static struct goku_udc *the_controller;
1348 /* when a driver is successfully registered, it will receive
1349 * control requests including set_configuration(), which enables
1350 * non-control requests. then usb traffic follows until a
1351 * disconnect is reported. then a host may connect again, or
1352 * the driver might get unbound.
1354 static int goku_start(struct usb_gadget_driver *driver,
1355 int (*bind)(struct usb_gadget *))
1357 struct goku_udc *dev = the_controller;
1358 int retval;
1360 if (!driver
1361 || driver->max_speed < USB_SPEED_FULL
1362 || !bind
1363 || !driver->disconnect
1364 || !driver->setup)
1365 return -EINVAL;
1366 if (!dev)
1367 return -ENODEV;
1368 if (dev->driver)
1369 return -EBUSY;
1371 /* hook up the driver */
1372 driver->driver.bus = NULL;
1373 dev->driver = driver;
1374 dev->gadget.dev.driver = &driver->driver;
1375 retval = bind(&dev->gadget);
1376 if (retval) {
1377 DBG(dev, "bind to driver %s --> error %d\n",
1378 driver->driver.name, retval);
1379 dev->driver = NULL;
1380 dev->gadget.dev.driver = NULL;
1381 return retval;
1384 /* then enable host detection and ep0; and we're ready
1385 * for set_configuration as well as eventual disconnect.
1387 udc_enable(dev);
1389 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1390 return 0;
1393 static void
1394 stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1396 unsigned i;
1398 DBG (dev, "%s\n", __func__);
1400 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1401 driver = NULL;
1403 /* disconnect gadget driver after quiesceing hw and the driver */
1404 udc_reset (dev);
1405 for (i = 0; i < 4; i++)
1406 nuke(&dev->ep [i], -ESHUTDOWN);
1407 if (driver) {
1408 spin_unlock(&dev->lock);
1409 driver->disconnect(&dev->gadget);
1410 spin_lock(&dev->lock);
1413 if (dev->driver)
1414 udc_enable(dev);
1417 static int goku_stop(struct usb_gadget_driver *driver)
1419 struct goku_udc *dev = the_controller;
1420 unsigned long flags;
1422 if (!dev)
1423 return -ENODEV;
1424 if (!driver || driver != dev->driver || !driver->unbind)
1425 return -EINVAL;
1427 spin_lock_irqsave(&dev->lock, flags);
1428 dev->driver = NULL;
1429 stop_activity(dev, driver);
1430 spin_unlock_irqrestore(&dev->lock, flags);
1432 driver->unbind(&dev->gadget);
1433 dev->gadget.dev.driver = NULL;
1435 DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1436 return 0;
1439 /*-------------------------------------------------------------------------*/
1441 static void ep0_setup(struct goku_udc *dev)
1443 struct goku_udc_regs __iomem *regs = dev->regs;
1444 struct usb_ctrlrequest ctrl;
1445 int tmp;
1447 /* read SETUP packet and enter DATA stage */
1448 ctrl.bRequestType = readl(&regs->bRequestType);
1449 ctrl.bRequest = readl(&regs->bRequest);
1450 ctrl.wValue = cpu_to_le16((readl(&regs->wValueH) << 8)
1451 | readl(&regs->wValueL));
1452 ctrl.wIndex = cpu_to_le16((readl(&regs->wIndexH) << 8)
1453 | readl(&regs->wIndexL));
1454 ctrl.wLength = cpu_to_le16((readl(&regs->wLengthH) << 8)
1455 | readl(&regs->wLengthL));
1456 writel(0, &regs->SetupRecv);
1458 nuke(&dev->ep[0], 0);
1459 dev->ep[0].stopped = 0;
1460 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1461 dev->ep[0].is_in = 1;
1462 dev->ep0state = EP0_IN;
1463 /* detect early status stages */
1464 writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1465 } else {
1466 dev->ep[0].is_in = 0;
1467 dev->ep0state = EP0_OUT;
1469 /* NOTE: CLEAR_FEATURE is done in software so that we can
1470 * synchronize transfer restarts after bulk IN stalls. data
1471 * won't even enter the fifo until the halt is cleared.
1473 switch (ctrl.bRequest) {
1474 case USB_REQ_CLEAR_FEATURE:
1475 switch (ctrl.bRequestType) {
1476 case USB_RECIP_ENDPOINT:
1477 tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
1478 /* active endpoint */
1479 if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
1480 goto stall;
1481 if (ctrl.wIndex & cpu_to_le16(
1482 USB_DIR_IN)) {
1483 if (!dev->ep[tmp].is_in)
1484 goto stall;
1485 } else {
1486 if (dev->ep[tmp].is_in)
1487 goto stall;
1489 if (ctrl.wValue != cpu_to_le16(
1490 USB_ENDPOINT_HALT))
1491 goto stall;
1492 if (tmp)
1493 goku_clear_halt(&dev->ep[tmp]);
1494 succeed:
1495 /* start ep0out status stage */
1496 writel(~(1<<0), &regs->EOP);
1497 dev->ep[0].stopped = 1;
1498 dev->ep0state = EP0_STATUS;
1499 return;
1500 case USB_RECIP_DEVICE:
1501 /* device remote wakeup: always clear */
1502 if (ctrl.wValue != cpu_to_le16(1))
1503 goto stall;
1504 VDBG(dev, "clear dev remote wakeup\n");
1505 goto succeed;
1506 case USB_RECIP_INTERFACE:
1507 goto stall;
1508 default: /* pass to gadget driver */
1509 break;
1511 break;
1512 default:
1513 break;
1517 #ifdef USB_TRACE
1518 VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1519 ctrl.bRequestType, ctrl.bRequest,
1520 le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1521 le16_to_cpu(ctrl.wLength));
1522 #endif
1524 /* hw wants to know when we're configured (or not) */
1525 dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1526 && ctrl.bRequestType == USB_RECIP_DEVICE);
1527 if (unlikely(dev->req_config))
1528 dev->configured = (ctrl.wValue != cpu_to_le16(0));
1530 /* delegate everything to the gadget driver.
1531 * it may respond after this irq handler returns.
1533 spin_unlock (&dev->lock);
1534 tmp = dev->driver->setup(&dev->gadget, &ctrl);
1535 spin_lock (&dev->lock);
1536 if (unlikely(tmp < 0)) {
1537 stall:
1538 #ifdef USB_TRACE
1539 VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1540 ctrl.bRequestType, ctrl.bRequest, tmp);
1541 #endif
1542 command(regs, COMMAND_STALL, 0);
1543 dev->ep[0].stopped = 1;
1544 dev->ep0state = EP0_STALL;
1547 /* expect at least one data or status stage irq */
1550 #define ACK(irqbit) { \
1551 stat &= ~irqbit; \
1552 writel(~irqbit, &regs->int_status); \
1553 handled = 1; \
1556 static irqreturn_t goku_irq(int irq, void *_dev)
1558 struct goku_udc *dev = _dev;
1559 struct goku_udc_regs __iomem *regs = dev->regs;
1560 struct goku_ep *ep;
1561 u32 stat, handled = 0;
1562 unsigned i, rescans = 5;
1564 spin_lock(&dev->lock);
1566 rescan:
1567 stat = readl(&regs->int_status) & dev->int_enable;
1568 if (!stat)
1569 goto done;
1570 dev->irqs++;
1572 /* device-wide irqs */
1573 if (unlikely(stat & INT_DEVWIDE)) {
1574 if (stat & INT_SYSERROR) {
1575 ERROR(dev, "system error\n");
1576 stop_activity(dev, dev->driver);
1577 stat = 0;
1578 handled = 1;
1579 // FIXME have a neater way to prevent re-enumeration
1580 dev->driver = NULL;
1581 goto done;
1583 if (stat & INT_PWRDETECT) {
1584 writel(~stat, &regs->int_status);
1585 if (readl(&dev->regs->power_detect) & PW_DETECT) {
1586 VDBG(dev, "connect\n");
1587 ep0_start(dev);
1588 } else {
1589 DBG(dev, "disconnect\n");
1590 if (dev->gadget.speed == USB_SPEED_FULL)
1591 stop_activity(dev, dev->driver);
1592 dev->ep0state = EP0_DISCONNECT;
1593 dev->int_enable = INT_DEVWIDE;
1594 writel(dev->int_enable, &dev->regs->int_enable);
1596 stat = 0;
1597 handled = 1;
1598 goto done;
1600 if (stat & INT_SUSPEND) {
1601 ACK(INT_SUSPEND);
1602 if (readl(&regs->ep_status[0]) & EPxSTATUS_SUSPEND) {
1603 switch (dev->ep0state) {
1604 case EP0_DISCONNECT:
1605 case EP0_SUSPEND:
1606 goto pm_next;
1607 default:
1608 break;
1610 DBG(dev, "USB suspend\n");
1611 dev->ep0state = EP0_SUSPEND;
1612 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1613 && dev->driver
1614 && dev->driver->suspend) {
1615 spin_unlock(&dev->lock);
1616 dev->driver->suspend(&dev->gadget);
1617 spin_lock(&dev->lock);
1619 } else {
1620 if (dev->ep0state != EP0_SUSPEND) {
1621 DBG(dev, "bogus USB resume %d\n",
1622 dev->ep0state);
1623 goto pm_next;
1625 DBG(dev, "USB resume\n");
1626 dev->ep0state = EP0_IDLE;
1627 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1628 && dev->driver
1629 && dev->driver->resume) {
1630 spin_unlock(&dev->lock);
1631 dev->driver->resume(&dev->gadget);
1632 spin_lock(&dev->lock);
1636 pm_next:
1637 if (stat & INT_USBRESET) { /* hub reset done */
1638 ACK(INT_USBRESET);
1639 INFO(dev, "USB reset done, gadget %s\n",
1640 dev->driver->driver.name);
1642 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1645 /* progress ep0 setup, data, or status stages.
1646 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1648 if (stat & INT_SETUP) {
1649 ACK(INT_SETUP);
1650 dev->ep[0].irqs++;
1651 ep0_setup(dev);
1653 if (stat & INT_STATUSNAK) {
1654 ACK(INT_STATUSNAK|INT_ENDPOINT0);
1655 if (dev->ep0state == EP0_IN) {
1656 ep = &dev->ep[0];
1657 ep->irqs++;
1658 nuke(ep, 0);
1659 writel(~(1<<0), &regs->EOP);
1660 dev->ep0state = EP0_STATUS;
1663 if (stat & INT_ENDPOINT0) {
1664 ACK(INT_ENDPOINT0);
1665 ep = &dev->ep[0];
1666 ep->irqs++;
1667 pio_advance(ep);
1670 /* dma completion */
1671 if (stat & INT_MSTRDEND) { /* IN */
1672 ACK(INT_MSTRDEND);
1673 ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1674 ep->irqs++;
1675 dma_advance(dev, ep);
1677 if (stat & INT_MSTWREND) { /* OUT */
1678 ACK(INT_MSTWREND);
1679 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1680 ep->irqs++;
1681 dma_advance(dev, ep);
1683 if (stat & INT_MSTWRTMOUT) { /* OUT */
1684 ACK(INT_MSTWRTMOUT);
1685 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1686 ep->irqs++;
1687 ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1688 // reset dma? then dma_advance()
1691 /* pio */
1692 for (i = 1; i < 4; i++) {
1693 u32 tmp = INT_EPxDATASET(i);
1695 if (!(stat & tmp))
1696 continue;
1697 ep = &dev->ep[i];
1698 pio_advance(ep);
1699 if (list_empty (&ep->queue))
1700 pio_irq_disable(dev, regs, i);
1701 stat &= ~tmp;
1702 handled = 1;
1703 ep->irqs++;
1706 if (rescans--)
1707 goto rescan;
1709 done:
1710 (void)readl(&regs->int_enable);
1711 spin_unlock(&dev->lock);
1712 if (stat)
1713 DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1714 readl(&regs->int_status), dev->int_enable);
1715 return IRQ_RETVAL(handled);
1718 #undef ACK
1720 /*-------------------------------------------------------------------------*/
1722 static void gadget_release(struct device *_dev)
1724 struct goku_udc *dev = dev_get_drvdata(_dev);
1726 kfree(dev);
1729 /* tear down the binding between this driver and the pci device */
1731 static void goku_remove(struct pci_dev *pdev)
1733 struct goku_udc *dev = pci_get_drvdata(pdev);
1735 DBG(dev, "%s\n", __func__);
1737 usb_del_gadget_udc(&dev->gadget);
1739 BUG_ON(dev->driver);
1741 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1742 remove_proc_entry(proc_node_name, NULL);
1743 #endif
1744 if (dev->regs)
1745 udc_reset(dev);
1746 if (dev->got_irq)
1747 free_irq(pdev->irq, dev);
1748 if (dev->regs)
1749 iounmap(dev->regs);
1750 if (dev->got_region)
1751 release_mem_region(pci_resource_start (pdev, 0),
1752 pci_resource_len (pdev, 0));
1753 if (dev->enabled)
1754 pci_disable_device(pdev);
1755 if (dev->registered)
1756 device_unregister(&dev->gadget.dev);
1758 pci_set_drvdata(pdev, NULL);
1759 dev->regs = NULL;
1760 the_controller = NULL;
1762 INFO(dev, "unbind\n");
1765 /* wrap this driver around the specified pci device, but
1766 * don't respond over USB until a gadget driver binds to us.
1769 static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1771 struct goku_udc *dev = NULL;
1772 unsigned long resource, len;
1773 void __iomem *base = NULL;
1774 int retval;
1776 /* if you want to support more than one controller in a system,
1777 * usb_gadget_driver_{register,unregister}() must change.
1779 if (the_controller) {
1780 pr_warning("ignoring %s\n", pci_name(pdev));
1781 return -EBUSY;
1783 if (!pdev->irq) {
1784 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1785 retval = -ENODEV;
1786 goto err;
1789 /* alloc, and start init */
1790 dev = kzalloc (sizeof *dev, GFP_KERNEL);
1791 if (dev == NULL){
1792 pr_debug("enomem %s\n", pci_name(pdev));
1793 retval = -ENOMEM;
1794 goto err;
1797 spin_lock_init(&dev->lock);
1798 dev->pdev = pdev;
1799 dev->gadget.ops = &goku_ops;
1800 dev->gadget.max_speed = USB_SPEED_FULL;
1802 /* the "gadget" abstracts/virtualizes the controller */
1803 dev_set_name(&dev->gadget.dev, "gadget");
1804 dev->gadget.dev.parent = &pdev->dev;
1805 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1806 dev->gadget.dev.release = gadget_release;
1807 dev->gadget.name = driver_name;
1809 /* now all the pci goodies ... */
1810 retval = pci_enable_device(pdev);
1811 if (retval < 0) {
1812 DBG(dev, "can't enable, %d\n", retval);
1813 goto err;
1815 dev->enabled = 1;
1817 resource = pci_resource_start(pdev, 0);
1818 len = pci_resource_len(pdev, 0);
1819 if (!request_mem_region(resource, len, driver_name)) {
1820 DBG(dev, "controller already in use\n");
1821 retval = -EBUSY;
1822 goto err;
1824 dev->got_region = 1;
1826 base = ioremap_nocache(resource, len);
1827 if (base == NULL) {
1828 DBG(dev, "can't map memory\n");
1829 retval = -EFAULT;
1830 goto err;
1832 dev->regs = (struct goku_udc_regs __iomem *) base;
1834 pci_set_drvdata(pdev, dev);
1835 INFO(dev, "%s\n", driver_desc);
1836 INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1837 INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
1839 /* init to known state, then setup irqs */
1840 udc_reset(dev);
1841 udc_reinit (dev);
1842 if (request_irq(pdev->irq, goku_irq, IRQF_SHARED/*|IRQF_SAMPLE_RANDOM*/,
1843 driver_name, dev) != 0) {
1844 DBG(dev, "request interrupt %d failed\n", pdev->irq);
1845 retval = -EBUSY;
1846 goto err;
1848 dev->got_irq = 1;
1849 if (use_dma)
1850 pci_set_master(pdev);
1853 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1854 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1855 #endif
1857 the_controller = dev;
1858 retval = device_register(&dev->gadget.dev);
1859 if (retval) {
1860 put_device(&dev->gadget.dev);
1861 goto err;
1863 dev->registered = 1;
1864 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
1865 if (retval)
1866 goto err;
1868 return 0;
1870 err:
1871 if (dev)
1872 goku_remove (pdev);
1873 return retval;
1877 /*-------------------------------------------------------------------------*/
1879 static const struct pci_device_id pci_ids[] = { {
1880 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1881 .class_mask = ~0,
1882 .vendor = 0x102f, /* Toshiba */
1883 .device = 0x0107, /* this UDC */
1884 .subvendor = PCI_ANY_ID,
1885 .subdevice = PCI_ANY_ID,
1887 }, { /* end: all zeroes */ }
1889 MODULE_DEVICE_TABLE (pci, pci_ids);
1891 static struct pci_driver goku_pci_driver = {
1892 .name = (char *) driver_name,
1893 .id_table = pci_ids,
1895 .probe = goku_probe,
1896 .remove = goku_remove,
1898 /* FIXME add power management support */
1901 static int __init init (void)
1903 return pci_register_driver (&goku_pci_driver);
1905 module_init (init);
1907 static void __exit cleanup (void)
1909 pci_unregister_driver (&goku_pci_driver);
1911 module_exit (cleanup);