proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / usb / gadget / goku_udc.c
blob48a760220baf992c48541f2cae822943690bae5c
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>
42 #include <asm/byteorder.h>
43 #include <asm/io.h>
44 #include <asm/irq.h>
45 #include <asm/system.h>
46 #include <asm/unaligned.h>
49 #include "goku_udc.h"
51 #define DRIVER_DESC "TC86C001 USB Device Controller"
52 #define DRIVER_VERSION "30-Oct 2003"
54 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
56 static const char driver_name [] = "goku_udc";
57 static const char driver_desc [] = DRIVER_DESC;
59 MODULE_AUTHOR("source@mvista.com");
60 MODULE_DESCRIPTION(DRIVER_DESC);
61 MODULE_LICENSE("GPL");
65 * IN dma behaves ok under testing, though the IN-dma abort paths don't
66 * seem to behave quite as expected. Used by default.
68 * OUT dma documents design problems handling the common "short packet"
69 * transfer termination policy; it couldn't be enabled by default, even
70 * if the OUT-dma abort problems had a resolution.
72 static unsigned use_dma = 1;
74 #if 0
75 //#include <linux/moduleparam.h>
76 /* "modprobe goku_udc use_dma=1" etc
77 * 0 to disable dma
78 * 1 to use IN dma only (normal operation)
79 * 2 to use IN and OUT dma
81 module_param(use_dma, uint, S_IRUGO);
82 #endif
84 /*-------------------------------------------------------------------------*/
86 static void nuke(struct goku_ep *, int status);
88 static inline void
89 command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
91 writel(COMMAND_EP(epnum) | command, &regs->Command);
92 udelay(300);
95 static int
96 goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
98 struct goku_udc *dev;
99 struct goku_ep *ep;
100 u32 mode;
101 u16 max;
102 unsigned long flags;
104 ep = container_of(_ep, struct goku_ep, ep);
105 if (!_ep || !desc || ep->desc
106 || desc->bDescriptorType != USB_DT_ENDPOINT)
107 return -EINVAL;
108 dev = ep->dev;
109 if (ep == &dev->ep[0])
110 return -EINVAL;
111 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
112 return -ESHUTDOWN;
113 if (ep->num != usb_endpoint_num(desc))
114 return -EINVAL;
116 switch (usb_endpoint_type(desc)) {
117 case USB_ENDPOINT_XFER_BULK:
118 case USB_ENDPOINT_XFER_INT:
119 break;
120 default:
121 return -EINVAL;
124 if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
125 != EPxSTATUS_EP_INVALID)
126 return -EBUSY;
128 /* enabling the no-toggle interrupt mode would need an api hook */
129 mode = 0;
130 max = get_unaligned_le16(&desc->wMaxPacketSize);
131 switch (max) {
132 case 64: mode++;
133 case 32: mode++;
134 case 16: mode++;
135 case 8: mode <<= 3;
136 break;
137 default:
138 return -EINVAL;
140 mode |= 2 << 1; /* bulk, or intr-with-toggle */
142 /* ep1/ep2 dma direction is chosen early; it works in the other
143 * direction, with pio. be cautious with out-dma.
145 ep->is_in = usb_endpoint_dir_in(desc);
146 if (ep->is_in) {
147 mode |= 1;
148 ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
149 } else {
150 ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
151 if (ep->dma)
152 DBG(dev, "%s out-dma hides short packets\n",
153 ep->ep.name);
156 spin_lock_irqsave(&ep->dev->lock, flags);
158 /* ep1 and ep2 can do double buffering and/or dma */
159 if (ep->num < 3) {
160 struct goku_udc_regs __iomem *regs = ep->dev->regs;
161 u32 tmp;
163 /* double buffer except (for now) with pio in */
164 tmp = ((ep->dma || !ep->is_in)
165 ? 0x10 /* double buffered */
166 : 0x11 /* single buffer */
167 ) << ep->num;
168 tmp |= readl(&regs->EPxSingle);
169 writel(tmp, &regs->EPxSingle);
171 tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
172 tmp |= readl(&regs->EPxBCS);
173 writel(tmp, &regs->EPxBCS);
175 writel(mode, ep->reg_mode);
176 command(ep->dev->regs, COMMAND_RESET, ep->num);
177 ep->ep.maxpacket = max;
178 ep->stopped = 0;
179 ep->desc = desc;
180 spin_unlock_irqrestore(&ep->dev->lock, flags);
182 DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
183 ep->is_in ? "IN" : "OUT",
184 ep->dma ? "dma" : "pio",
185 max);
187 return 0;
190 static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
192 struct goku_udc *dev = ep->dev;
194 if (regs) {
195 command(regs, COMMAND_INVALID, ep->num);
196 if (ep->num) {
197 if (ep->num == UDC_MSTWR_ENDPOINT)
198 dev->int_enable &= ~(INT_MSTWREND
199 |INT_MSTWRTMOUT);
200 else if (ep->num == UDC_MSTRD_ENDPOINT)
201 dev->int_enable &= ~INT_MSTRDEND;
202 dev->int_enable &= ~INT_EPxDATASET (ep->num);
203 } else
204 dev->int_enable &= ~INT_EP0;
205 writel(dev->int_enable, &regs->int_enable);
206 readl(&regs->int_enable);
207 if (ep->num < 3) {
208 struct goku_udc_regs __iomem *r = ep->dev->regs;
209 u32 tmp;
211 tmp = readl(&r->EPxSingle);
212 tmp &= ~(0x11 << ep->num);
213 writel(tmp, &r->EPxSingle);
215 tmp = readl(&r->EPxBCS);
216 tmp &= ~(0x11 << ep->num);
217 writel(tmp, &r->EPxBCS);
219 /* reset dma in case we're still using it */
220 if (ep->dma) {
221 u32 master;
223 master = readl(&regs->dma_master) & MST_RW_BITS;
224 if (ep->num == UDC_MSTWR_ENDPOINT) {
225 master &= ~MST_W_BITS;
226 master |= MST_WR_RESET;
227 } else {
228 master &= ~MST_R_BITS;
229 master |= MST_RD_RESET;
231 writel(master, &regs->dma_master);
235 ep->ep.maxpacket = MAX_FIFO_SIZE;
236 ep->desc = NULL;
237 ep->stopped = 1;
238 ep->irqs = 0;
239 ep->dma = 0;
242 static int goku_ep_disable(struct usb_ep *_ep)
244 struct goku_ep *ep;
245 struct goku_udc *dev;
246 unsigned long flags;
248 ep = container_of(_ep, struct goku_ep, ep);
249 if (!_ep || !ep->desc)
250 return -ENODEV;
251 dev = ep->dev;
252 if (dev->ep0state == EP0_SUSPEND)
253 return -EBUSY;
255 VDBG(dev, "disable %s\n", _ep->name);
257 spin_lock_irqsave(&dev->lock, flags);
258 nuke(ep, -ESHUTDOWN);
259 ep_reset(dev->regs, ep);
260 spin_unlock_irqrestore(&dev->lock, flags);
262 return 0;
265 /*-------------------------------------------------------------------------*/
267 static struct usb_request *
268 goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
270 struct goku_request *req;
272 if (!_ep)
273 return NULL;
274 req = kzalloc(sizeof *req, gfp_flags);
275 if (!req)
276 return NULL;
278 req->req.dma = DMA_ADDR_INVALID;
279 INIT_LIST_HEAD(&req->queue);
280 return &req->req;
283 static void
284 goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
286 struct goku_request *req;
288 if (!_ep || !_req)
289 return;
291 req = container_of(_req, struct goku_request, req);
292 WARN_ON(!list_empty(&req->queue));
293 kfree(req);
296 /*-------------------------------------------------------------------------*/
298 static void
299 done(struct goku_ep *ep, struct goku_request *req, int status)
301 struct goku_udc *dev;
302 unsigned stopped = ep->stopped;
304 list_del_init(&req->queue);
306 if (likely(req->req.status == -EINPROGRESS))
307 req->req.status = status;
308 else
309 status = req->req.status;
311 dev = ep->dev;
312 if (req->mapped) {
313 pci_unmap_single(dev->pdev, req->req.dma, req->req.length,
314 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
315 req->req.dma = DMA_ADDR_INVALID;
316 req->mapped = 0;
319 #ifndef USB_TRACE
320 if (status && status != -ESHUTDOWN)
321 #endif
322 VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
323 ep->ep.name, &req->req, status,
324 req->req.actual, req->req.length);
326 /* don't modify queue heads during completion callback */
327 ep->stopped = 1;
328 spin_unlock(&dev->lock);
329 req->req.complete(&ep->ep, &req->req);
330 spin_lock(&dev->lock);
331 ep->stopped = stopped;
334 /*-------------------------------------------------------------------------*/
336 static inline int
337 write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
339 unsigned length, count;
341 length = min(req->req.length - req->req.actual, max);
342 req->req.actual += length;
344 count = length;
345 while (likely(count--))
346 writel(*buf++, fifo);
347 return length;
350 // return: 0 = still running, 1 = completed, negative = errno
351 static int write_fifo(struct goku_ep *ep, struct goku_request *req)
353 struct goku_udc *dev = ep->dev;
354 u32 tmp;
355 u8 *buf;
356 unsigned count;
357 int is_last;
359 tmp = readl(&dev->regs->DataSet);
360 buf = req->req.buf + req->req.actual;
361 prefetch(buf);
363 dev = ep->dev;
364 if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
365 return -EL2HLT;
367 /* NOTE: just single-buffered PIO-IN for now. */
368 if (unlikely((tmp & DATASET_A(ep->num)) != 0))
369 return 0;
371 /* clear our "packet available" irq */
372 if (ep->num != 0)
373 writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
375 count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
377 /* last packet often short (sometimes a zlp, especially on ep0) */
378 if (unlikely(count != ep->ep.maxpacket)) {
379 writel(~(1<<ep->num), &dev->regs->EOP);
380 if (ep->num == 0) {
381 dev->ep[0].stopped = 1;
382 dev->ep0state = EP0_STATUS;
384 is_last = 1;
385 } else {
386 if (likely(req->req.length != req->req.actual)
387 || req->req.zero)
388 is_last = 0;
389 else
390 is_last = 1;
392 #if 0 /* printk seemed to trash is_last...*/
393 //#ifdef USB_TRACE
394 VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
395 ep->ep.name, count, is_last ? "/last" : "",
396 req->req.length - req->req.actual, req);
397 #endif
399 /* requests complete when all IN data is in the FIFO,
400 * or sometimes later, if a zlp was needed.
402 if (is_last) {
403 done(ep, req, 0);
404 return 1;
407 return 0;
410 static int read_fifo(struct goku_ep *ep, struct goku_request *req)
412 struct goku_udc_regs __iomem *regs;
413 u32 size, set;
414 u8 *buf;
415 unsigned bufferspace, is_short, dbuff;
417 regs = ep->dev->regs;
418 top:
419 buf = req->req.buf + req->req.actual;
420 prefetchw(buf);
422 if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
423 return -EL2HLT;
425 dbuff = (ep->num == 1 || ep->num == 2);
426 do {
427 /* ack dataset irq matching the status we'll handle */
428 if (ep->num != 0)
429 writel(~INT_EPxDATASET(ep->num), &regs->int_status);
431 set = readl(&regs->DataSet) & DATASET_AB(ep->num);
432 size = readl(&regs->EPxSizeLA[ep->num]);
433 bufferspace = req->req.length - req->req.actual;
435 /* usually do nothing without an OUT packet */
436 if (likely(ep->num != 0 || bufferspace != 0)) {
437 if (unlikely(set == 0))
438 break;
439 /* use ep1/ep2 double-buffering for OUT */
440 if (!(size & PACKET_ACTIVE))
441 size = readl(&regs->EPxSizeLB[ep->num]);
442 if (!(size & PACKET_ACTIVE)) /* "can't happen" */
443 break;
444 size &= DATASIZE; /* EPxSizeH == 0 */
446 /* ep0out no-out-data case for set_config, etc */
447 } else
448 size = 0;
450 /* read all bytes from this packet */
451 req->req.actual += size;
452 is_short = (size < ep->ep.maxpacket);
453 #ifdef USB_TRACE
454 VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
455 ep->ep.name, size, is_short ? "/S" : "",
456 req, req->req.actual, req->req.length);
457 #endif
458 while (likely(size-- != 0)) {
459 u8 byte = (u8) readl(ep->reg_fifo);
461 if (unlikely(bufferspace == 0)) {
462 /* this happens when the driver's buffer
463 * is smaller than what the host sent.
464 * discard the extra data in this packet.
466 if (req->req.status != -EOVERFLOW)
467 DBG(ep->dev, "%s overflow %u\n",
468 ep->ep.name, size);
469 req->req.status = -EOVERFLOW;
470 } else {
471 *buf++ = byte;
472 bufferspace--;
476 /* completion */
477 if (unlikely(is_short || req->req.actual == req->req.length)) {
478 if (unlikely(ep->num == 0)) {
479 /* non-control endpoints now usable? */
480 if (ep->dev->req_config)
481 writel(ep->dev->configured
482 ? USBSTATE_CONFIGURED
483 : 0,
484 &regs->UsbState);
485 /* ep0out status stage */
486 writel(~(1<<0), &regs->EOP);
487 ep->stopped = 1;
488 ep->dev->ep0state = EP0_STATUS;
490 done(ep, req, 0);
492 /* empty the second buffer asap */
493 if (dbuff && !list_empty(&ep->queue)) {
494 req = list_entry(ep->queue.next,
495 struct goku_request, queue);
496 goto top;
498 return 1;
500 } while (dbuff);
501 return 0;
504 static inline void
505 pio_irq_enable(struct goku_udc *dev,
506 struct goku_udc_regs __iomem *regs, int epnum)
508 dev->int_enable |= INT_EPxDATASET (epnum);
509 writel(dev->int_enable, &regs->int_enable);
510 /* write may still be posted */
513 static inline void
514 pio_irq_disable(struct goku_udc *dev,
515 struct goku_udc_regs __iomem *regs, int epnum)
517 dev->int_enable &= ~INT_EPxDATASET (epnum);
518 writel(dev->int_enable, &regs->int_enable);
519 /* write may still be posted */
522 static inline void
523 pio_advance(struct goku_ep *ep)
525 struct goku_request *req;
527 if (unlikely(list_empty (&ep->queue)))
528 return;
529 req = list_entry(ep->queue.next, struct goku_request, queue);
530 (ep->is_in ? write_fifo : read_fifo)(ep, req);
534 /*-------------------------------------------------------------------------*/
536 // return: 0 = q running, 1 = q stopped, negative = errno
537 static int start_dma(struct goku_ep *ep, struct goku_request *req)
539 struct goku_udc_regs __iomem *regs = ep->dev->regs;
540 u32 master;
541 u32 start = req->req.dma;
542 u32 end = start + req->req.length - 1;
544 master = readl(&regs->dma_master) & MST_RW_BITS;
546 /* re-init the bits affecting IN dma; careful with zlps */
547 if (likely(ep->is_in)) {
548 if (unlikely(master & MST_RD_ENA)) {
549 DBG (ep->dev, "start, IN active dma %03x!!\n",
550 master);
551 // return -EL2HLT;
553 writel(end, &regs->in_dma_end);
554 writel(start, &regs->in_dma_start);
556 master &= ~MST_R_BITS;
557 if (unlikely(req->req.length == 0))
558 master = MST_RD_ENA | MST_RD_EOPB;
559 else if ((req->req.length % ep->ep.maxpacket) != 0
560 || req->req.zero)
561 master = MST_RD_ENA | MST_EOPB_ENA;
562 else
563 master = MST_RD_ENA | MST_EOPB_DIS;
565 ep->dev->int_enable |= INT_MSTRDEND;
567 /* Goku DMA-OUT merges short packets, which plays poorly with
568 * protocols where short packets mark the transfer boundaries.
569 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
570 * ending transfers after 3 SOFs; we don't turn it on.
572 } else {
573 if (unlikely(master & MST_WR_ENA)) {
574 DBG (ep->dev, "start, OUT active dma %03x!!\n",
575 master);
576 // return -EL2HLT;
578 writel(end, &regs->out_dma_end);
579 writel(start, &regs->out_dma_start);
581 master &= ~MST_W_BITS;
582 master |= MST_WR_ENA | MST_TIMEOUT_DIS;
584 ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
587 writel(master, &regs->dma_master);
588 writel(ep->dev->int_enable, &regs->int_enable);
589 return 0;
592 static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
594 struct goku_request *req;
595 struct goku_udc_regs __iomem *regs = ep->dev->regs;
596 u32 master;
598 master = readl(&regs->dma_master);
600 if (unlikely(list_empty(&ep->queue))) {
601 stop:
602 if (ep->is_in)
603 dev->int_enable &= ~INT_MSTRDEND;
604 else
605 dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
606 writel(dev->int_enable, &regs->int_enable);
607 return;
609 req = list_entry(ep->queue.next, struct goku_request, queue);
611 /* normal hw dma completion (not abort) */
612 if (likely(ep->is_in)) {
613 if (unlikely(master & MST_RD_ENA))
614 return;
615 req->req.actual = readl(&regs->in_dma_current);
616 } else {
617 if (unlikely(master & MST_WR_ENA))
618 return;
620 /* hardware merges short packets, and also hides packet
621 * overruns. a partial packet MAY be in the fifo here.
623 req->req.actual = readl(&regs->out_dma_current);
625 req->req.actual -= req->req.dma;
626 req->req.actual++;
628 #ifdef USB_TRACE
629 VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
630 ep->ep.name, ep->is_in ? "IN" : "OUT",
631 req->req.actual, req->req.length, req);
632 #endif
633 done(ep, req, 0);
634 if (list_empty(&ep->queue))
635 goto stop;
636 req = list_entry(ep->queue.next, struct goku_request, queue);
637 (void) start_dma(ep, req);
640 static void abort_dma(struct goku_ep *ep, int status)
642 struct goku_udc_regs __iomem *regs = ep->dev->regs;
643 struct goku_request *req;
644 u32 curr, master;
646 /* NAK future host requests, hoping the implicit delay lets the
647 * dma engine finish reading (or writing) its latest packet and
648 * empty the dma buffer (up to 16 bytes).
650 * This avoids needing to clean up a partial packet in the fifo;
651 * we can't do that for IN without side effects to HALT and TOGGLE.
653 command(regs, COMMAND_FIFO_DISABLE, ep->num);
654 req = list_entry(ep->queue.next, struct goku_request, queue);
655 master = readl(&regs->dma_master) & MST_RW_BITS;
657 /* FIXME using these resets isn't usably documented. this may
658 * not work unless it's followed by disabling the endpoint.
660 * FIXME the OUT reset path doesn't even behave consistently.
662 if (ep->is_in) {
663 if (unlikely((readl(&regs->dma_master) & MST_RD_ENA) == 0))
664 goto finished;
665 curr = readl(&regs->in_dma_current);
667 writel(curr, &regs->in_dma_end);
668 writel(curr, &regs->in_dma_start);
670 master &= ~MST_R_BITS;
671 master |= MST_RD_RESET;
672 writel(master, &regs->dma_master);
674 if (readl(&regs->dma_master) & MST_RD_ENA)
675 DBG(ep->dev, "IN dma active after reset!\n");
677 } else {
678 if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
679 goto finished;
680 curr = readl(&regs->out_dma_current);
682 writel(curr, &regs->out_dma_end);
683 writel(curr, &regs->out_dma_start);
685 master &= ~MST_W_BITS;
686 master |= MST_WR_RESET;
687 writel(master, &regs->dma_master);
689 if (readl(&regs->dma_master) & MST_WR_ENA)
690 DBG(ep->dev, "OUT dma active after reset!\n");
692 req->req.actual = (curr - req->req.dma) + 1;
693 req->req.status = status;
695 VDBG(ep->dev, "%s %s %s %d/%d\n", __func__, ep->ep.name,
696 ep->is_in ? "IN" : "OUT",
697 req->req.actual, req->req.length);
699 command(regs, COMMAND_FIFO_ENABLE, ep->num);
701 return;
703 finished:
704 /* dma already completed; no abort needed */
705 command(regs, COMMAND_FIFO_ENABLE, ep->num);
706 req->req.actual = req->req.length;
707 req->req.status = 0;
710 /*-------------------------------------------------------------------------*/
712 static int
713 goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
715 struct goku_request *req;
716 struct goku_ep *ep;
717 struct goku_udc *dev;
718 unsigned long flags;
719 int status;
721 /* always require a cpu-view buffer so pio works */
722 req = container_of(_req, struct goku_request, req);
723 if (unlikely(!_req || !_req->complete
724 || !_req->buf || !list_empty(&req->queue)))
725 return -EINVAL;
726 ep = container_of(_ep, struct goku_ep, ep);
727 if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
728 return -EINVAL;
729 dev = ep->dev;
730 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
731 return -ESHUTDOWN;
733 /* can't touch registers when suspended */
734 if (dev->ep0state == EP0_SUSPEND)
735 return -EBUSY;
737 /* set up dma mapping in case the caller didn't */
738 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
739 _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length,
740 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
741 req->mapped = 1;
744 #ifdef USB_TRACE
745 VDBG(dev, "%s queue req %p, len %u buf %p\n",
746 _ep->name, _req, _req->length, _req->buf);
747 #endif
749 spin_lock_irqsave(&dev->lock, flags);
751 _req->status = -EINPROGRESS;
752 _req->actual = 0;
754 /* for ep0 IN without premature status, zlp is required and
755 * writing EOP starts the status stage (OUT).
757 if (unlikely(ep->num == 0 && ep->is_in))
758 _req->zero = 1;
760 /* kickstart this i/o queue? */
761 status = 0;
762 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
763 /* dma: done after dma completion IRQ (or error)
764 * pio: done after last fifo operation
766 if (ep->dma)
767 status = start_dma(ep, req);
768 else
769 status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
771 if (unlikely(status != 0)) {
772 if (status > 0)
773 status = 0;
774 req = NULL;
777 } /* else pio or dma irq handler advances the queue. */
779 if (likely(req != 0))
780 list_add_tail(&req->queue, &ep->queue);
782 if (likely(!list_empty(&ep->queue))
783 && likely(ep->num != 0)
784 && !ep->dma
785 && !(dev->int_enable & INT_EPxDATASET (ep->num)))
786 pio_irq_enable(dev, dev->regs, ep->num);
788 spin_unlock_irqrestore(&dev->lock, flags);
790 /* pci writes may still be posted */
791 return status;
794 /* dequeue ALL requests */
795 static void nuke(struct goku_ep *ep, int status)
797 struct goku_request *req;
799 ep->stopped = 1;
800 if (list_empty(&ep->queue))
801 return;
802 if (ep->dma)
803 abort_dma(ep, status);
804 while (!list_empty(&ep->queue)) {
805 req = list_entry(ep->queue.next, struct goku_request, queue);
806 done(ep, req, status);
810 /* dequeue JUST ONE request */
811 static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
813 struct goku_request *req;
814 struct goku_ep *ep;
815 struct goku_udc *dev;
816 unsigned long flags;
818 ep = container_of(_ep, struct goku_ep, ep);
819 if (!_ep || !_req || (!ep->desc && ep->num != 0))
820 return -EINVAL;
821 dev = ep->dev;
822 if (!dev->driver)
823 return -ESHUTDOWN;
825 /* we can't touch (dma) registers when suspended */
826 if (dev->ep0state == EP0_SUSPEND)
827 return -EBUSY;
829 VDBG(dev, "%s %s %s %s %p\n", __func__, _ep->name,
830 ep->is_in ? "IN" : "OUT",
831 ep->dma ? "dma" : "pio",
832 _req);
834 spin_lock_irqsave(&dev->lock, flags);
836 /* make sure it's actually queued on this endpoint */
837 list_for_each_entry (req, &ep->queue, queue) {
838 if (&req->req == _req)
839 break;
841 if (&req->req != _req) {
842 spin_unlock_irqrestore (&dev->lock, flags);
843 return -EINVAL;
846 if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
847 abort_dma(ep, -ECONNRESET);
848 done(ep, req, -ECONNRESET);
849 dma_advance(dev, ep);
850 } else if (!list_empty(&req->queue))
851 done(ep, req, -ECONNRESET);
852 else
853 req = NULL;
854 spin_unlock_irqrestore(&dev->lock, flags);
856 return req ? 0 : -EOPNOTSUPP;
859 /*-------------------------------------------------------------------------*/
861 static void goku_clear_halt(struct goku_ep *ep)
863 // assert (ep->num !=0)
864 VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
865 command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
866 command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
867 if (ep->stopped) {
868 ep->stopped = 0;
869 if (ep->dma) {
870 struct goku_request *req;
872 if (list_empty(&ep->queue))
873 return;
874 req = list_entry(ep->queue.next, struct goku_request,
875 queue);
876 (void) start_dma(ep, req);
877 } else
878 pio_advance(ep);
882 static int goku_set_halt(struct usb_ep *_ep, int value)
884 struct goku_ep *ep;
885 unsigned long flags;
886 int retval = 0;
888 if (!_ep)
889 return -ENODEV;
890 ep = container_of (_ep, struct goku_ep, ep);
892 if (ep->num == 0) {
893 if (value) {
894 ep->dev->ep0state = EP0_STALL;
895 ep->dev->ep[0].stopped = 1;
896 } else
897 return -EINVAL;
899 /* don't change EPxSTATUS_EP_INVALID to READY */
900 } else if (!ep->desc) {
901 DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
902 return -EINVAL;
905 spin_lock_irqsave(&ep->dev->lock, flags);
906 if (!list_empty(&ep->queue))
907 retval = -EAGAIN;
908 else if (ep->is_in && value
909 /* data in (either) packet buffer? */
910 && (readl(&ep->dev->regs->DataSet)
911 & DATASET_AB(ep->num)))
912 retval = -EAGAIN;
913 else if (!value)
914 goku_clear_halt(ep);
915 else {
916 ep->stopped = 1;
917 VDBG(ep->dev, "%s set halt\n", ep->ep.name);
918 command(ep->dev->regs, COMMAND_STALL, ep->num);
919 readl(ep->reg_status);
921 spin_unlock_irqrestore(&ep->dev->lock, flags);
922 return retval;
925 static int goku_fifo_status(struct usb_ep *_ep)
927 struct goku_ep *ep;
928 struct goku_udc_regs __iomem *regs;
929 u32 size;
931 if (!_ep)
932 return -ENODEV;
933 ep = container_of(_ep, struct goku_ep, ep);
935 /* size is only reported sanely for OUT */
936 if (ep->is_in)
937 return -EOPNOTSUPP;
939 /* ignores 16-byte dma buffer; SizeH == 0 */
940 regs = ep->dev->regs;
941 size = readl(&regs->EPxSizeLA[ep->num]) & DATASIZE;
942 size += readl(&regs->EPxSizeLB[ep->num]) & DATASIZE;
943 VDBG(ep->dev, "%s %s %u\n", __func__, ep->ep.name, size);
944 return size;
947 static void goku_fifo_flush(struct usb_ep *_ep)
949 struct goku_ep *ep;
950 struct goku_udc_regs __iomem *regs;
951 u32 size;
953 if (!_ep)
954 return;
955 ep = container_of(_ep, struct goku_ep, ep);
956 VDBG(ep->dev, "%s %s\n", __func__, ep->ep.name);
958 /* don't change EPxSTATUS_EP_INVALID to READY */
959 if (!ep->desc && ep->num != 0) {
960 DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
961 return;
964 regs = ep->dev->regs;
965 size = readl(&regs->EPxSizeLA[ep->num]);
966 size &= DATASIZE;
968 /* Non-desirable behavior: FIFO_CLEAR also clears the
969 * endpoint halt feature. For OUT, we _could_ just read
970 * the bytes out (PIO, if !ep->dma); for in, no choice.
972 if (size)
973 command(regs, COMMAND_FIFO_CLEAR, ep->num);
976 static struct usb_ep_ops goku_ep_ops = {
977 .enable = goku_ep_enable,
978 .disable = goku_ep_disable,
980 .alloc_request = goku_alloc_request,
981 .free_request = goku_free_request,
983 .queue = goku_queue,
984 .dequeue = goku_dequeue,
986 .set_halt = goku_set_halt,
987 .fifo_status = goku_fifo_status,
988 .fifo_flush = goku_fifo_flush,
991 /*-------------------------------------------------------------------------*/
993 static int goku_get_frame(struct usb_gadget *_gadget)
995 return -EOPNOTSUPP;
998 static const struct usb_gadget_ops goku_ops = {
999 .get_frame = goku_get_frame,
1000 // no remote wakeup
1001 // not selfpowered
1004 /*-------------------------------------------------------------------------*/
1006 static inline char *dmastr(void)
1008 if (use_dma == 0)
1009 return "(dma disabled)";
1010 else if (use_dma == 2)
1011 return "(dma IN and OUT)";
1012 else
1013 return "(dma IN)";
1016 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1018 static const char proc_node_name [] = "driver/udc";
1020 #define FOURBITS "%s%s%s%s"
1021 #define EIGHTBITS FOURBITS FOURBITS
1023 static void
1024 dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1026 int t;
1028 /* int_status is the same format ... */
1029 t = scnprintf(*next, *size,
1030 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1031 label, mask,
1032 (mask & INT_PWRDETECT) ? " power" : "",
1033 (mask & INT_SYSERROR) ? " sys" : "",
1034 (mask & INT_MSTRDEND) ? " in-dma" : "",
1035 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1037 (mask & INT_MSTWREND) ? " out-dma" : "",
1038 (mask & INT_MSTWRSET) ? " wrset" : "",
1039 (mask & INT_ERR) ? " err" : "",
1040 (mask & INT_SOF) ? " sof" : "",
1042 (mask & INT_EP3NAK) ? " ep3nak" : "",
1043 (mask & INT_EP2NAK) ? " ep2nak" : "",
1044 (mask & INT_EP1NAK) ? " ep1nak" : "",
1045 (mask & INT_EP3DATASET) ? " ep3" : "",
1047 (mask & INT_EP2DATASET) ? " ep2" : "",
1048 (mask & INT_EP1DATASET) ? " ep1" : "",
1049 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1050 (mask & INT_STATUS) ? " ep0status" : "",
1052 (mask & INT_SETUP) ? " setup" : "",
1053 (mask & INT_ENDPOINT0) ? " ep0" : "",
1054 (mask & INT_USBRESET) ? " reset" : "",
1055 (mask & INT_SUSPEND) ? " suspend" : "");
1056 *size -= t;
1057 *next += t;
1061 static int
1062 udc_proc_read(char *buffer, char **start, off_t off, int count,
1063 int *eof, void *_dev)
1065 char *buf = buffer;
1066 struct goku_udc *dev = _dev;
1067 struct goku_udc_regs __iomem *regs = dev->regs;
1068 char *next = buf;
1069 unsigned size = count;
1070 unsigned long flags;
1071 int i, t, is_usb_connected;
1072 u32 tmp;
1074 if (off != 0)
1075 return 0;
1077 local_irq_save(flags);
1079 /* basic device status */
1080 tmp = readl(&regs->power_detect);
1081 is_usb_connected = tmp & PW_DETECT;
1082 t = scnprintf(next, size,
1083 "%s - %s\n"
1084 "%s version: %s %s\n"
1085 "Gadget driver: %s\n"
1086 "Host %s, %s\n"
1087 "\n",
1088 pci_name(dev->pdev), driver_desc,
1089 driver_name, DRIVER_VERSION, dmastr(),
1090 dev->driver ? dev->driver->driver.name : "(none)",
1091 is_usb_connected
1092 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1093 : "disconnected",
1094 ({char *state;
1095 switch(dev->ep0state){
1096 case EP0_DISCONNECT: state = "ep0_disconnect"; break;
1097 case EP0_IDLE: state = "ep0_idle"; break;
1098 case EP0_IN: state = "ep0_in"; break;
1099 case EP0_OUT: state = "ep0_out"; break;
1100 case EP0_STATUS: state = "ep0_status"; break;
1101 case EP0_STALL: state = "ep0_stall"; break;
1102 case EP0_SUSPEND: state = "ep0_suspend"; break;
1103 default: state = "ep0_?"; break;
1104 } state; })
1106 size -= t;
1107 next += t;
1109 dump_intmask("int_status", readl(&regs->int_status), &next, &size);
1110 dump_intmask("int_enable", readl(&regs->int_enable), &next, &size);
1112 if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1113 goto done;
1115 /* registers for (active) device and ep0 */
1116 t = scnprintf(next, size, "\nirqs %lu\ndataset %02x "
1117 "single.bcs %02x.%02x state %x addr %u\n",
1118 dev->irqs, readl(&regs->DataSet),
1119 readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1120 readl(&regs->UsbState),
1121 readl(&regs->address));
1122 size -= t;
1123 next += t;
1125 tmp = readl(&regs->dma_master);
1126 t = scnprintf(next, size,
1127 "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1128 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1129 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1130 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1131 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1133 (tmp & MST_RD_EOPB) ? " eopb" : "",
1134 (tmp & MST_RD_RESET) ? " in_reset" : "",
1135 (tmp & MST_WR_RESET) ? " out_reset" : "",
1136 (tmp & MST_RD_ENA) ? " IN" : "",
1138 (tmp & MST_WR_ENA) ? " OUT" : "",
1139 (tmp & MST_CONNECTION)
1140 ? "ep1in/ep2out"
1141 : "ep1out/ep2in");
1142 size -= t;
1143 next += t;
1145 /* dump endpoint queues */
1146 for (i = 0; i < 4; i++) {
1147 struct goku_ep *ep = &dev->ep [i];
1148 struct goku_request *req;
1150 if (i && !ep->desc)
1151 continue;
1153 tmp = readl(ep->reg_status);
1154 t = scnprintf(next, size,
1155 "%s %s max %u %s, irqs %lu, "
1156 "status %02x (%s) " FOURBITS "\n",
1157 ep->ep.name,
1158 ep->is_in ? "in" : "out",
1159 ep->ep.maxpacket,
1160 ep->dma ? "dma" : "pio",
1161 ep->irqs,
1162 tmp, ({ char *s;
1163 switch (tmp & EPxSTATUS_EP_MASK) {
1164 case EPxSTATUS_EP_READY:
1165 s = "ready"; break;
1166 case EPxSTATUS_EP_DATAIN:
1167 s = "packet"; break;
1168 case EPxSTATUS_EP_FULL:
1169 s = "full"; break;
1170 case EPxSTATUS_EP_TX_ERR: // host will retry
1171 s = "tx_err"; break;
1172 case EPxSTATUS_EP_RX_ERR:
1173 s = "rx_err"; break;
1174 case EPxSTATUS_EP_BUSY: /* ep0 only */
1175 s = "busy"; break;
1176 case EPxSTATUS_EP_STALL:
1177 s = "stall"; break;
1178 case EPxSTATUS_EP_INVALID: // these "can't happen"
1179 s = "invalid"; break;
1180 default:
1181 s = "?"; break;
1182 }; s; }),
1183 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1184 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1185 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1186 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1188 if (t <= 0 || t > size)
1189 goto done;
1190 size -= t;
1191 next += t;
1193 if (list_empty(&ep->queue)) {
1194 t = scnprintf(next, size, "\t(nothing queued)\n");
1195 if (t <= 0 || t > size)
1196 goto done;
1197 size -= t;
1198 next += t;
1199 continue;
1201 list_for_each_entry(req, &ep->queue, queue) {
1202 if (ep->dma && req->queue.prev == &ep->queue) {
1203 if (i == UDC_MSTRD_ENDPOINT)
1204 tmp = readl(&regs->in_dma_current);
1205 else
1206 tmp = readl(&regs->out_dma_current);
1207 tmp -= req->req.dma;
1208 tmp++;
1209 } else
1210 tmp = req->req.actual;
1212 t = scnprintf(next, size,
1213 "\treq %p len %u/%u buf %p\n",
1214 &req->req, tmp, req->req.length,
1215 req->req.buf);
1216 if (t <= 0 || t > size)
1217 goto done;
1218 size -= t;
1219 next += t;
1223 done:
1224 local_irq_restore(flags);
1225 *eof = 1;
1226 return count - size;
1229 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1231 /*-------------------------------------------------------------------------*/
1233 static void udc_reinit (struct goku_udc *dev)
1235 static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1237 unsigned i;
1239 INIT_LIST_HEAD (&dev->gadget.ep_list);
1240 dev->gadget.ep0 = &dev->ep [0].ep;
1241 dev->gadget.speed = USB_SPEED_UNKNOWN;
1242 dev->ep0state = EP0_DISCONNECT;
1243 dev->irqs = 0;
1245 for (i = 0; i < 4; i++) {
1246 struct goku_ep *ep = &dev->ep[i];
1248 ep->num = i;
1249 ep->ep.name = names[i];
1250 ep->reg_fifo = &dev->regs->ep_fifo [i];
1251 ep->reg_status = &dev->regs->ep_status [i];
1252 ep->reg_mode = &dev->regs->ep_mode[i];
1254 ep->ep.ops = &goku_ep_ops;
1255 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1256 ep->dev = dev;
1257 INIT_LIST_HEAD (&ep->queue);
1259 ep_reset(NULL, ep);
1262 dev->ep[0].reg_mode = NULL;
1263 dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1264 list_del_init (&dev->ep[0].ep.ep_list);
1267 static void udc_reset(struct goku_udc *dev)
1269 struct goku_udc_regs __iomem *regs = dev->regs;
1271 writel(0, &regs->power_detect);
1272 writel(0, &regs->int_enable);
1273 readl(&regs->int_enable);
1274 dev->int_enable = 0;
1276 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1277 * don't let INT_PWRDETECT sequence begin
1279 udelay(250);
1280 writel(PW_RESETB, &regs->power_detect);
1281 readl(&regs->int_enable);
1284 static void ep0_start(struct goku_udc *dev)
1286 struct goku_udc_regs __iomem *regs = dev->regs;
1287 unsigned i;
1289 VDBG(dev, "%s\n", __func__);
1291 udc_reset(dev);
1292 udc_reinit (dev);
1293 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
1295 /* hw handles set_address, set_feature, get_status; maybe more */
1296 writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
1297 | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
1298 | G_REQMODE_GET_DESC
1299 | G_REQMODE_CLEAR_FEAT
1300 , &regs->reqmode);
1302 for (i = 0; i < 4; i++)
1303 dev->ep[i].irqs = 0;
1305 /* can't modify descriptors after writing UsbReady */
1306 for (i = 0; i < DESC_LEN; i++)
1307 writel(0, &regs->descriptors[i]);
1308 writel(0, &regs->UsbReady);
1310 /* expect ep0 requests when the host drops reset */
1311 writel(PW_RESETB | PW_PULLUP, &regs->power_detect);
1312 dev->int_enable = INT_DEVWIDE | INT_EP0;
1313 writel(dev->int_enable, &dev->regs->int_enable);
1314 readl(&regs->int_enable);
1315 dev->gadget.speed = USB_SPEED_FULL;
1316 dev->ep0state = EP0_IDLE;
1319 static void udc_enable(struct goku_udc *dev)
1321 /* start enumeration now, or after power detect irq */
1322 if (readl(&dev->regs->power_detect) & PW_DETECT)
1323 ep0_start(dev);
1324 else {
1325 DBG(dev, "%s\n", __func__);
1326 dev->int_enable = INT_PWRDETECT;
1327 writel(dev->int_enable, &dev->regs->int_enable);
1331 /*-------------------------------------------------------------------------*/
1333 /* keeping it simple:
1334 * - one bus driver, initted first;
1335 * - one function driver, initted second
1338 static struct goku_udc *the_controller;
1340 /* when a driver is successfully registered, it will receive
1341 * control requests including set_configuration(), which enables
1342 * non-control requests. then usb traffic follows until a
1343 * disconnect is reported. then a host may connect again, or
1344 * the driver might get unbound.
1346 int usb_gadget_probe_driver(struct usb_gadget_driver *driver,
1347 int (*bind)(struct usb_gadget *))
1349 struct goku_udc *dev = the_controller;
1350 int retval;
1352 if (!driver
1353 || driver->speed < USB_SPEED_FULL
1354 || !bind
1355 || !driver->disconnect
1356 || !driver->setup)
1357 return -EINVAL;
1358 if (!dev)
1359 return -ENODEV;
1360 if (dev->driver)
1361 return -EBUSY;
1363 /* hook up the driver */
1364 driver->driver.bus = NULL;
1365 dev->driver = driver;
1366 dev->gadget.dev.driver = &driver->driver;
1367 retval = bind(&dev->gadget);
1368 if (retval) {
1369 DBG(dev, "bind to driver %s --> error %d\n",
1370 driver->driver.name, retval);
1371 dev->driver = NULL;
1372 dev->gadget.dev.driver = NULL;
1373 return retval;
1376 /* then enable host detection and ep0; and we're ready
1377 * for set_configuration as well as eventual disconnect.
1379 udc_enable(dev);
1381 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1382 return 0;
1384 EXPORT_SYMBOL(usb_gadget_probe_driver);
1386 static void
1387 stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1389 unsigned i;
1391 DBG (dev, "%s\n", __func__);
1393 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1394 driver = NULL;
1396 /* disconnect gadget driver after quiesceing hw and the driver */
1397 udc_reset (dev);
1398 for (i = 0; i < 4; i++)
1399 nuke(&dev->ep [i], -ESHUTDOWN);
1400 if (driver) {
1401 spin_unlock(&dev->lock);
1402 driver->disconnect(&dev->gadget);
1403 spin_lock(&dev->lock);
1406 if (dev->driver)
1407 udc_enable(dev);
1410 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1412 struct goku_udc *dev = the_controller;
1413 unsigned long flags;
1415 if (!dev)
1416 return -ENODEV;
1417 if (!driver || driver != dev->driver || !driver->unbind)
1418 return -EINVAL;
1420 spin_lock_irqsave(&dev->lock, flags);
1421 dev->driver = NULL;
1422 stop_activity(dev, driver);
1423 spin_unlock_irqrestore(&dev->lock, flags);
1425 driver->unbind(&dev->gadget);
1426 dev->gadget.dev.driver = NULL;
1428 DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1429 return 0;
1431 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1434 /*-------------------------------------------------------------------------*/
1436 static void ep0_setup(struct goku_udc *dev)
1438 struct goku_udc_regs __iomem *regs = dev->regs;
1439 struct usb_ctrlrequest ctrl;
1440 int tmp;
1442 /* read SETUP packet and enter DATA stage */
1443 ctrl.bRequestType = readl(&regs->bRequestType);
1444 ctrl.bRequest = readl(&regs->bRequest);
1445 ctrl.wValue = cpu_to_le16((readl(&regs->wValueH) << 8)
1446 | readl(&regs->wValueL));
1447 ctrl.wIndex = cpu_to_le16((readl(&regs->wIndexH) << 8)
1448 | readl(&regs->wIndexL));
1449 ctrl.wLength = cpu_to_le16((readl(&regs->wLengthH) << 8)
1450 | readl(&regs->wLengthL));
1451 writel(0, &regs->SetupRecv);
1453 nuke(&dev->ep[0], 0);
1454 dev->ep[0].stopped = 0;
1455 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1456 dev->ep[0].is_in = 1;
1457 dev->ep0state = EP0_IN;
1458 /* detect early status stages */
1459 writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1460 } else {
1461 dev->ep[0].is_in = 0;
1462 dev->ep0state = EP0_OUT;
1464 /* NOTE: CLEAR_FEATURE is done in software so that we can
1465 * synchronize transfer restarts after bulk IN stalls. data
1466 * won't even enter the fifo until the halt is cleared.
1468 switch (ctrl.bRequest) {
1469 case USB_REQ_CLEAR_FEATURE:
1470 switch (ctrl.bRequestType) {
1471 case USB_RECIP_ENDPOINT:
1472 tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
1473 /* active endpoint */
1474 if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
1475 goto stall;
1476 if (ctrl.wIndex & cpu_to_le16(
1477 USB_DIR_IN)) {
1478 if (!dev->ep[tmp].is_in)
1479 goto stall;
1480 } else {
1481 if (dev->ep[tmp].is_in)
1482 goto stall;
1484 if (ctrl.wValue != cpu_to_le16(
1485 USB_ENDPOINT_HALT))
1486 goto stall;
1487 if (tmp)
1488 goku_clear_halt(&dev->ep[tmp]);
1489 succeed:
1490 /* start ep0out status stage */
1491 writel(~(1<<0), &regs->EOP);
1492 dev->ep[0].stopped = 1;
1493 dev->ep0state = EP0_STATUS;
1494 return;
1495 case USB_RECIP_DEVICE:
1496 /* device remote wakeup: always clear */
1497 if (ctrl.wValue != cpu_to_le16(1))
1498 goto stall;
1499 VDBG(dev, "clear dev remote wakeup\n");
1500 goto succeed;
1501 case USB_RECIP_INTERFACE:
1502 goto stall;
1503 default: /* pass to gadget driver */
1504 break;
1506 break;
1507 default:
1508 break;
1512 #ifdef USB_TRACE
1513 VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1514 ctrl.bRequestType, ctrl.bRequest,
1515 le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1516 le16_to_cpu(ctrl.wLength));
1517 #endif
1519 /* hw wants to know when we're configured (or not) */
1520 dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1521 && ctrl.bRequestType == USB_RECIP_DEVICE);
1522 if (unlikely(dev->req_config))
1523 dev->configured = (ctrl.wValue != cpu_to_le16(0));
1525 /* delegate everything to the gadget driver.
1526 * it may respond after this irq handler returns.
1528 spin_unlock (&dev->lock);
1529 tmp = dev->driver->setup(&dev->gadget, &ctrl);
1530 spin_lock (&dev->lock);
1531 if (unlikely(tmp < 0)) {
1532 stall:
1533 #ifdef USB_TRACE
1534 VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1535 ctrl.bRequestType, ctrl.bRequest, tmp);
1536 #endif
1537 command(regs, COMMAND_STALL, 0);
1538 dev->ep[0].stopped = 1;
1539 dev->ep0state = EP0_STALL;
1542 /* expect at least one data or status stage irq */
1545 #define ACK(irqbit) { \
1546 stat &= ~irqbit; \
1547 writel(~irqbit, &regs->int_status); \
1548 handled = 1; \
1551 static irqreturn_t goku_irq(int irq, void *_dev)
1553 struct goku_udc *dev = _dev;
1554 struct goku_udc_regs __iomem *regs = dev->regs;
1555 struct goku_ep *ep;
1556 u32 stat, handled = 0;
1557 unsigned i, rescans = 5;
1559 spin_lock(&dev->lock);
1561 rescan:
1562 stat = readl(&regs->int_status) & dev->int_enable;
1563 if (!stat)
1564 goto done;
1565 dev->irqs++;
1567 /* device-wide irqs */
1568 if (unlikely(stat & INT_DEVWIDE)) {
1569 if (stat & INT_SYSERROR) {
1570 ERROR(dev, "system error\n");
1571 stop_activity(dev, dev->driver);
1572 stat = 0;
1573 handled = 1;
1574 // FIXME have a neater way to prevent re-enumeration
1575 dev->driver = NULL;
1576 goto done;
1578 if (stat & INT_PWRDETECT) {
1579 writel(~stat, &regs->int_status);
1580 if (readl(&dev->regs->power_detect) & PW_DETECT) {
1581 VDBG(dev, "connect\n");
1582 ep0_start(dev);
1583 } else {
1584 DBG(dev, "disconnect\n");
1585 if (dev->gadget.speed == USB_SPEED_FULL)
1586 stop_activity(dev, dev->driver);
1587 dev->ep0state = EP0_DISCONNECT;
1588 dev->int_enable = INT_DEVWIDE;
1589 writel(dev->int_enable, &dev->regs->int_enable);
1591 stat = 0;
1592 handled = 1;
1593 goto done;
1595 if (stat & INT_SUSPEND) {
1596 ACK(INT_SUSPEND);
1597 if (readl(&regs->ep_status[0]) & EPxSTATUS_SUSPEND) {
1598 switch (dev->ep0state) {
1599 case EP0_DISCONNECT:
1600 case EP0_SUSPEND:
1601 goto pm_next;
1602 default:
1603 break;
1605 DBG(dev, "USB suspend\n");
1606 dev->ep0state = EP0_SUSPEND;
1607 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1608 && dev->driver
1609 && dev->driver->suspend) {
1610 spin_unlock(&dev->lock);
1611 dev->driver->suspend(&dev->gadget);
1612 spin_lock(&dev->lock);
1614 } else {
1615 if (dev->ep0state != EP0_SUSPEND) {
1616 DBG(dev, "bogus USB resume %d\n",
1617 dev->ep0state);
1618 goto pm_next;
1620 DBG(dev, "USB resume\n");
1621 dev->ep0state = EP0_IDLE;
1622 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1623 && dev->driver
1624 && dev->driver->resume) {
1625 spin_unlock(&dev->lock);
1626 dev->driver->resume(&dev->gadget);
1627 spin_lock(&dev->lock);
1631 pm_next:
1632 if (stat & INT_USBRESET) { /* hub reset done */
1633 ACK(INT_USBRESET);
1634 INFO(dev, "USB reset done, gadget %s\n",
1635 dev->driver->driver.name);
1637 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1640 /* progress ep0 setup, data, or status stages.
1641 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1643 if (stat & INT_SETUP) {
1644 ACK(INT_SETUP);
1645 dev->ep[0].irqs++;
1646 ep0_setup(dev);
1648 if (stat & INT_STATUSNAK) {
1649 ACK(INT_STATUSNAK|INT_ENDPOINT0);
1650 if (dev->ep0state == EP0_IN) {
1651 ep = &dev->ep[0];
1652 ep->irqs++;
1653 nuke(ep, 0);
1654 writel(~(1<<0), &regs->EOP);
1655 dev->ep0state = EP0_STATUS;
1658 if (stat & INT_ENDPOINT0) {
1659 ACK(INT_ENDPOINT0);
1660 ep = &dev->ep[0];
1661 ep->irqs++;
1662 pio_advance(ep);
1665 /* dma completion */
1666 if (stat & INT_MSTRDEND) { /* IN */
1667 ACK(INT_MSTRDEND);
1668 ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1669 ep->irqs++;
1670 dma_advance(dev, ep);
1672 if (stat & INT_MSTWREND) { /* OUT */
1673 ACK(INT_MSTWREND);
1674 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1675 ep->irqs++;
1676 dma_advance(dev, ep);
1678 if (stat & INT_MSTWRTMOUT) { /* OUT */
1679 ACK(INT_MSTWRTMOUT);
1680 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1681 ep->irqs++;
1682 ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1683 // reset dma? then dma_advance()
1686 /* pio */
1687 for (i = 1; i < 4; i++) {
1688 u32 tmp = INT_EPxDATASET(i);
1690 if (!(stat & tmp))
1691 continue;
1692 ep = &dev->ep[i];
1693 pio_advance(ep);
1694 if (list_empty (&ep->queue))
1695 pio_irq_disable(dev, regs, i);
1696 stat &= ~tmp;
1697 handled = 1;
1698 ep->irqs++;
1701 if (rescans--)
1702 goto rescan;
1704 done:
1705 (void)readl(&regs->int_enable);
1706 spin_unlock(&dev->lock);
1707 if (stat)
1708 DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1709 readl(&regs->int_status), dev->int_enable);
1710 return IRQ_RETVAL(handled);
1713 #undef ACK
1715 /*-------------------------------------------------------------------------*/
1717 static void gadget_release(struct device *_dev)
1719 struct goku_udc *dev = dev_get_drvdata(_dev);
1721 kfree(dev);
1724 /* tear down the binding between this driver and the pci device */
1726 static void goku_remove(struct pci_dev *pdev)
1728 struct goku_udc *dev = pci_get_drvdata(pdev);
1730 DBG(dev, "%s\n", __func__);
1732 BUG_ON(dev->driver);
1734 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1735 remove_proc_entry(proc_node_name, NULL);
1736 #endif
1737 if (dev->regs)
1738 udc_reset(dev);
1739 if (dev->got_irq)
1740 free_irq(pdev->irq, dev);
1741 if (dev->regs)
1742 iounmap(dev->regs);
1743 if (dev->got_region)
1744 release_mem_region(pci_resource_start (pdev, 0),
1745 pci_resource_len (pdev, 0));
1746 if (dev->enabled)
1747 pci_disable_device(pdev);
1748 if (dev->registered)
1749 device_unregister(&dev->gadget.dev);
1751 pci_set_drvdata(pdev, NULL);
1752 dev->regs = NULL;
1753 the_controller = NULL;
1755 INFO(dev, "unbind\n");
1758 /* wrap this driver around the specified pci device, but
1759 * don't respond over USB until a gadget driver binds to us.
1762 static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1764 struct goku_udc *dev = NULL;
1765 unsigned long resource, len;
1766 void __iomem *base = NULL;
1767 int retval;
1769 /* if you want to support more than one controller in a system,
1770 * usb_gadget_driver_{register,unregister}() must change.
1772 if (the_controller) {
1773 pr_warning("ignoring %s\n", pci_name(pdev));
1774 return -EBUSY;
1776 if (!pdev->irq) {
1777 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1778 retval = -ENODEV;
1779 goto err;
1782 /* alloc, and start init */
1783 dev = kzalloc (sizeof *dev, GFP_KERNEL);
1784 if (dev == NULL){
1785 pr_debug("enomem %s\n", pci_name(pdev));
1786 retval = -ENOMEM;
1787 goto err;
1790 spin_lock_init(&dev->lock);
1791 dev->pdev = pdev;
1792 dev->gadget.ops = &goku_ops;
1794 /* the "gadget" abstracts/virtualizes the controller */
1795 dev_set_name(&dev->gadget.dev, "gadget");
1796 dev->gadget.dev.parent = &pdev->dev;
1797 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1798 dev->gadget.dev.release = gadget_release;
1799 dev->gadget.name = driver_name;
1801 /* now all the pci goodies ... */
1802 retval = pci_enable_device(pdev);
1803 if (retval < 0) {
1804 DBG(dev, "can't enable, %d\n", retval);
1805 goto err;
1807 dev->enabled = 1;
1809 resource = pci_resource_start(pdev, 0);
1810 len = pci_resource_len(pdev, 0);
1811 if (!request_mem_region(resource, len, driver_name)) {
1812 DBG(dev, "controller already in use\n");
1813 retval = -EBUSY;
1814 goto err;
1816 dev->got_region = 1;
1818 base = ioremap_nocache(resource, len);
1819 if (base == NULL) {
1820 DBG(dev, "can't map memory\n");
1821 retval = -EFAULT;
1822 goto err;
1824 dev->regs = (struct goku_udc_regs __iomem *) base;
1826 pci_set_drvdata(pdev, dev);
1827 INFO(dev, "%s\n", driver_desc);
1828 INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1829 INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
1831 /* init to known state, then setup irqs */
1832 udc_reset(dev);
1833 udc_reinit (dev);
1834 if (request_irq(pdev->irq, goku_irq, IRQF_SHARED/*|IRQF_SAMPLE_RANDOM*/,
1835 driver_name, dev) != 0) {
1836 DBG(dev, "request interrupt %d failed\n", pdev->irq);
1837 retval = -EBUSY;
1838 goto err;
1840 dev->got_irq = 1;
1841 if (use_dma)
1842 pci_set_master(pdev);
1845 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1846 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1847 #endif
1849 the_controller = dev;
1850 retval = device_register(&dev->gadget.dev);
1851 if (retval) {
1852 put_device(&dev->gadget.dev);
1853 goto err;
1855 dev->registered = 1;
1856 return 0;
1858 err:
1859 if (dev)
1860 goku_remove (pdev);
1861 return retval;
1865 /*-------------------------------------------------------------------------*/
1867 static const struct pci_device_id pci_ids[] = { {
1868 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1869 .class_mask = ~0,
1870 .vendor = 0x102f, /* Toshiba */
1871 .device = 0x0107, /* this UDC */
1872 .subvendor = PCI_ANY_ID,
1873 .subdevice = PCI_ANY_ID,
1875 }, { /* end: all zeroes */ }
1877 MODULE_DEVICE_TABLE (pci, pci_ids);
1879 static struct pci_driver goku_pci_driver = {
1880 .name = (char *) driver_name,
1881 .id_table = pci_ids,
1883 .probe = goku_probe,
1884 .remove = goku_remove,
1886 /* FIXME add power management support */
1889 static int __init init (void)
1891 return pci_register_driver (&goku_pci_driver);
1893 module_init (init);
1895 static void __exit cleanup (void)
1897 pci_unregister_driver (&goku_pci_driver);
1899 module_exit (cleanup);