Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / usb / gadget / goku_udc.c
blob66b81bbf6bee2d695f4ffb637dc489e737be0d17
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 #undef DEBUG
24 // #define VERBOSE /* extra debug messages (success too) */
25 // #define USB_TRACE /* packet-level success messages */
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/pci.h>
31 #include <linux/delay.h>
32 #include <linux/ioport.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/smp_lock.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/timer.h>
39 #include <linux/list.h>
40 #include <linux/interrupt.h>
41 #include <linux/proc_fs.h>
42 #include <linux/device.h>
43 #include <linux/usb_ch9.h>
44 #include <linux/usb_gadget.h>
46 #include <asm/byteorder.h>
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/system.h>
50 #include <asm/unaligned.h>
53 #include "goku_udc.h"
55 #define DRIVER_DESC "TC86C001 USB Device Controller"
56 #define DRIVER_VERSION "30-Oct 2003"
58 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
60 static const char driver_name [] = "goku_udc";
61 static const char driver_desc [] = DRIVER_DESC;
63 MODULE_AUTHOR("source@mvista.com");
64 MODULE_DESCRIPTION(DRIVER_DESC);
65 MODULE_LICENSE("GPL");
69 * IN dma behaves ok under testing, though the IN-dma abort paths don't
70 * seem to behave quite as expected. Used by default.
72 * OUT dma documents design problems handling the common "short packet"
73 * transfer termination policy; it couldn't be enabled by default, even
74 * if the OUT-dma abort problems had a resolution.
76 static unsigned use_dma = 1;
78 #if 0
79 //#include <linux/moduleparam.h>
80 /* "modprobe goku_udc use_dma=1" etc
81 * 0 to disable dma
82 * 1 to use IN dma only (normal operation)
83 * 2 to use IN and OUT dma
85 module_param(use_dma, uint, S_IRUGO);
86 #endif
88 /*-------------------------------------------------------------------------*/
90 static void nuke(struct goku_ep *, int status);
92 static inline void
93 command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
95 writel(COMMAND_EP(epnum) | command, &regs->Command);
96 udelay(300);
99 static int
100 goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
102 struct goku_udc *dev;
103 struct goku_ep *ep;
104 u32 mode;
105 u16 max;
106 unsigned long flags;
108 ep = container_of(_ep, struct goku_ep, ep);
109 if (!_ep || !desc || ep->desc
110 || desc->bDescriptorType != USB_DT_ENDPOINT)
111 return -EINVAL;
112 dev = ep->dev;
113 if (ep == &dev->ep[0])
114 return -EINVAL;
115 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
116 return -ESHUTDOWN;
117 if (ep->num != (desc->bEndpointAddress & 0x0f))
118 return -EINVAL;
120 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
121 case USB_ENDPOINT_XFER_BULK:
122 case USB_ENDPOINT_XFER_INT:
123 break;
124 default:
125 return -EINVAL;
128 if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
129 != EPxSTATUS_EP_INVALID)
130 return -EBUSY;
132 /* enabling the no-toggle interrupt mode would need an api hook */
133 mode = 0;
134 max = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
135 switch (max) {
136 case 64: mode++;
137 case 32: mode++;
138 case 16: mode++;
139 case 8: mode <<= 3;
140 break;
141 default:
142 return -EINVAL;
144 mode |= 2 << 1; /* bulk, or intr-with-toggle */
146 /* ep1/ep2 dma direction is chosen early; it works in the other
147 * direction, with pio. be cautious with out-dma.
149 ep->is_in = (USB_DIR_IN & desc->bEndpointAddress) != 0;
150 if (ep->is_in) {
151 mode |= 1;
152 ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
153 } else {
154 ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
155 if (ep->dma)
156 DBG(dev, "%s out-dma hides short packets\n",
157 ep->ep.name);
160 spin_lock_irqsave(&ep->dev->lock, flags);
162 /* ep1 and ep2 can do double buffering and/or dma */
163 if (ep->num < 3) {
164 struct goku_udc_regs __iomem *regs = ep->dev->regs;
165 u32 tmp;
167 /* double buffer except (for now) with pio in */
168 tmp = ((ep->dma || !ep->is_in)
169 ? 0x10 /* double buffered */
170 : 0x11 /* single buffer */
171 ) << ep->num;
172 tmp |= readl(&regs->EPxSingle);
173 writel(tmp, &regs->EPxSingle);
175 tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
176 tmp |= readl(&regs->EPxBCS);
177 writel(tmp, &regs->EPxBCS);
179 writel(mode, ep->reg_mode);
180 command(ep->dev->regs, COMMAND_RESET, ep->num);
181 ep->ep.maxpacket = max;
182 ep->stopped = 0;
183 ep->desc = desc;
184 spin_unlock_irqrestore(&ep->dev->lock, flags);
186 DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
187 ep->is_in ? "IN" : "OUT",
188 ep->dma ? "dma" : "pio",
189 max);
191 return 0;
194 static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
196 struct goku_udc *dev = ep->dev;
198 if (regs) {
199 command(regs, COMMAND_INVALID, ep->num);
200 if (ep->num) {
201 if (ep->num == UDC_MSTWR_ENDPOINT)
202 dev->int_enable &= ~(INT_MSTWREND
203 |INT_MSTWRTMOUT);
204 else if (ep->num == UDC_MSTRD_ENDPOINT)
205 dev->int_enable &= ~INT_MSTRDEND;
206 dev->int_enable &= ~INT_EPxDATASET (ep->num);
207 } else
208 dev->int_enable &= ~INT_EP0;
209 writel(dev->int_enable, &regs->int_enable);
210 readl(&regs->int_enable);
211 if (ep->num < 3) {
212 struct goku_udc_regs __iomem *r = ep->dev->regs;
213 u32 tmp;
215 tmp = readl(&r->EPxSingle);
216 tmp &= ~(0x11 << ep->num);
217 writel(tmp, &r->EPxSingle);
219 tmp = readl(&r->EPxBCS);
220 tmp &= ~(0x11 << ep->num);
221 writel(tmp, &r->EPxBCS);
223 /* reset dma in case we're still using it */
224 if (ep->dma) {
225 u32 master;
227 master = readl(&regs->dma_master) & MST_RW_BITS;
228 if (ep->num == UDC_MSTWR_ENDPOINT) {
229 master &= ~MST_W_BITS;
230 master |= MST_WR_RESET;
231 } else {
232 master &= ~MST_R_BITS;
233 master |= MST_RD_RESET;
235 writel(master, &regs->dma_master);
239 ep->ep.maxpacket = MAX_FIFO_SIZE;
240 ep->desc = NULL;
241 ep->stopped = 1;
242 ep->irqs = 0;
243 ep->dma = 0;
246 static int goku_ep_disable(struct usb_ep *_ep)
248 struct goku_ep *ep;
249 struct goku_udc *dev;
250 unsigned long flags;
252 ep = container_of(_ep, struct goku_ep, ep);
253 if (!_ep || !ep->desc)
254 return -ENODEV;
255 dev = ep->dev;
256 if (dev->ep0state == EP0_SUSPEND)
257 return -EBUSY;
259 VDBG(dev, "disable %s\n", _ep->name);
261 spin_lock_irqsave(&dev->lock, flags);
262 nuke(ep, -ESHUTDOWN);
263 ep_reset(dev->regs, ep);
264 spin_unlock_irqrestore(&dev->lock, flags);
266 return 0;
269 /*-------------------------------------------------------------------------*/
271 static struct usb_request *
272 goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
274 struct goku_request *req;
276 if (!_ep)
277 return NULL;
278 req = kzalloc(sizeof *req, gfp_flags);
279 if (!req)
280 return NULL;
282 req->req.dma = DMA_ADDR_INVALID;
283 INIT_LIST_HEAD(&req->queue);
284 return &req->req;
287 static void
288 goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
290 struct goku_request *req;
292 if (!_ep || !_req)
293 return;
295 req = container_of(_req, struct goku_request, req);
296 WARN_ON(!list_empty(&req->queue));
297 kfree(req);
300 /*-------------------------------------------------------------------------*/
302 #undef USE_KMALLOC
304 /* many common platforms have dma-coherent caches, which means that it's
305 * safe to use kmalloc() memory for all i/o buffers without using any
306 * cache flushing calls. (unless you're trying to share cache lines
307 * between dma and non-dma activities, which is a slow idea in any case.)
309 * other platforms need more care, with 2.6 having a moderately general
310 * solution except for the common "buffer is smaller than a page" case.
312 #if defined(CONFIG_X86)
313 #define USE_KMALLOC
315 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
316 #define USE_KMALLOC
318 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
319 #define USE_KMALLOC
321 #endif
323 /* allocating buffers this way eliminates dma mapping overhead, which
324 * on some platforms will mean eliminating a per-io buffer copy. with
325 * some kinds of system caches, further tweaks may still be needed.
327 static void *
328 goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
329 dma_addr_t *dma, gfp_t gfp_flags)
331 void *retval;
332 struct goku_ep *ep;
334 ep = container_of(_ep, struct goku_ep, ep);
335 if (!_ep)
336 return NULL;
337 *dma = DMA_ADDR_INVALID;
339 #if defined(USE_KMALLOC)
340 retval = kmalloc(bytes, gfp_flags);
341 if (retval)
342 *dma = virt_to_phys(retval);
343 #else
344 if (ep->dma) {
345 /* the main problem with this call is that it wastes memory
346 * on typical 1/N page allocations: it allocates 1-N pages.
348 #warning Using dma_alloc_coherent even with buffers smaller than a page.
349 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
350 bytes, dma, gfp_flags);
351 } else
352 retval = kmalloc(bytes, gfp_flags);
353 #endif
354 return retval;
357 static void
358 goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
360 /* free memory into the right allocator */
361 #ifndef USE_KMALLOC
362 if (dma != DMA_ADDR_INVALID) {
363 struct goku_ep *ep;
365 ep = container_of(_ep, struct goku_ep, ep);
366 if (!_ep)
367 return;
368 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
369 } else
370 #endif
371 kfree (buf);
374 /*-------------------------------------------------------------------------*/
376 static void
377 done(struct goku_ep *ep, struct goku_request *req, int status)
379 struct goku_udc *dev;
380 unsigned stopped = ep->stopped;
382 list_del_init(&req->queue);
384 if (likely(req->req.status == -EINPROGRESS))
385 req->req.status = status;
386 else
387 status = req->req.status;
389 dev = ep->dev;
390 if (req->mapped) {
391 pci_unmap_single(dev->pdev, req->req.dma, req->req.length,
392 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
393 req->req.dma = DMA_ADDR_INVALID;
394 req->mapped = 0;
397 #ifndef USB_TRACE
398 if (status && status != -ESHUTDOWN)
399 #endif
400 VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
401 ep->ep.name, &req->req, status,
402 req->req.actual, req->req.length);
404 /* don't modify queue heads during completion callback */
405 ep->stopped = 1;
406 spin_unlock(&dev->lock);
407 req->req.complete(&ep->ep, &req->req);
408 spin_lock(&dev->lock);
409 ep->stopped = stopped;
412 /*-------------------------------------------------------------------------*/
414 static inline int
415 write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
417 unsigned length, count;
419 length = min(req->req.length - req->req.actual, max);
420 req->req.actual += length;
422 count = length;
423 while (likely(count--))
424 writel(*buf++, fifo);
425 return length;
428 // return: 0 = still running, 1 = completed, negative = errno
429 static int write_fifo(struct goku_ep *ep, struct goku_request *req)
431 struct goku_udc *dev = ep->dev;
432 u32 tmp;
433 u8 *buf;
434 unsigned count;
435 int is_last;
437 tmp = readl(&dev->regs->DataSet);
438 buf = req->req.buf + req->req.actual;
439 prefetch(buf);
441 dev = ep->dev;
442 if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
443 return -EL2HLT;
445 /* NOTE: just single-buffered PIO-IN for now. */
446 if (unlikely((tmp & DATASET_A(ep->num)) != 0))
447 return 0;
449 /* clear our "packet available" irq */
450 if (ep->num != 0)
451 writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
453 count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
455 /* last packet often short (sometimes a zlp, especially on ep0) */
456 if (unlikely(count != ep->ep.maxpacket)) {
457 writel(~(1<<ep->num), &dev->regs->EOP);
458 if (ep->num == 0) {
459 dev->ep[0].stopped = 1;
460 dev->ep0state = EP0_STATUS;
462 is_last = 1;
463 } else {
464 if (likely(req->req.length != req->req.actual)
465 || req->req.zero)
466 is_last = 0;
467 else
468 is_last = 1;
470 #if 0 /* printk seemed to trash is_last...*/
471 //#ifdef USB_TRACE
472 VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
473 ep->ep.name, count, is_last ? "/last" : "",
474 req->req.length - req->req.actual, req);
475 #endif
477 /* requests complete when all IN data is in the FIFO,
478 * or sometimes later, if a zlp was needed.
480 if (is_last) {
481 done(ep, req, 0);
482 return 1;
485 return 0;
488 static int read_fifo(struct goku_ep *ep, struct goku_request *req)
490 struct goku_udc_regs __iomem *regs;
491 u32 size, set;
492 u8 *buf;
493 unsigned bufferspace, is_short, dbuff;
495 regs = ep->dev->regs;
496 top:
497 buf = req->req.buf + req->req.actual;
498 prefetchw(buf);
500 if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
501 return -EL2HLT;
503 dbuff = (ep->num == 1 || ep->num == 2);
504 do {
505 /* ack dataset irq matching the status we'll handle */
506 if (ep->num != 0)
507 writel(~INT_EPxDATASET(ep->num), &regs->int_status);
509 set = readl(&regs->DataSet) & DATASET_AB(ep->num);
510 size = readl(&regs->EPxSizeLA[ep->num]);
511 bufferspace = req->req.length - req->req.actual;
513 /* usually do nothing without an OUT packet */
514 if (likely(ep->num != 0 || bufferspace != 0)) {
515 if (unlikely(set == 0))
516 break;
517 /* use ep1/ep2 double-buffering for OUT */
518 if (!(size & PACKET_ACTIVE))
519 size = readl(&regs->EPxSizeLB[ep->num]);
520 if (!(size & PACKET_ACTIVE)) // "can't happen"
521 break;
522 size &= DATASIZE; /* EPxSizeH == 0 */
524 /* ep0out no-out-data case for set_config, etc */
525 } else
526 size = 0;
528 /* read all bytes from this packet */
529 req->req.actual += size;
530 is_short = (size < ep->ep.maxpacket);
531 #ifdef USB_TRACE
532 VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
533 ep->ep.name, size, is_short ? "/S" : "",
534 req, req->req.actual, req->req.length);
535 #endif
536 while (likely(size-- != 0)) {
537 u8 byte = (u8) readl(ep->reg_fifo);
539 if (unlikely(bufferspace == 0)) {
540 /* this happens when the driver's buffer
541 * is smaller than what the host sent.
542 * discard the extra data in this packet.
544 if (req->req.status != -EOVERFLOW)
545 DBG(ep->dev, "%s overflow %u\n",
546 ep->ep.name, size);
547 req->req.status = -EOVERFLOW;
548 } else {
549 *buf++ = byte;
550 bufferspace--;
554 /* completion */
555 if (unlikely(is_short || req->req.actual == req->req.length)) {
556 if (unlikely(ep->num == 0)) {
557 /* non-control endpoints now usable? */
558 if (ep->dev->req_config)
559 writel(ep->dev->configured
560 ? USBSTATE_CONFIGURED
561 : 0,
562 &regs->UsbState);
563 /* ep0out status stage */
564 writel(~(1<<0), &regs->EOP);
565 ep->stopped = 1;
566 ep->dev->ep0state = EP0_STATUS;
568 done(ep, req, 0);
570 /* empty the second buffer asap */
571 if (dbuff && !list_empty(&ep->queue)) {
572 req = list_entry(ep->queue.next,
573 struct goku_request, queue);
574 goto top;
576 return 1;
578 } while (dbuff);
579 return 0;
582 static inline void
583 pio_irq_enable(struct goku_udc *dev,
584 struct goku_udc_regs __iomem *regs, int epnum)
586 dev->int_enable |= INT_EPxDATASET (epnum);
587 writel(dev->int_enable, &regs->int_enable);
588 /* write may still be posted */
591 static inline void
592 pio_irq_disable(struct goku_udc *dev,
593 struct goku_udc_regs __iomem *regs, int epnum)
595 dev->int_enable &= ~INT_EPxDATASET (epnum);
596 writel(dev->int_enable, &regs->int_enable);
597 /* write may still be posted */
600 static inline void
601 pio_advance(struct goku_ep *ep)
603 struct goku_request *req;
605 if (unlikely(list_empty (&ep->queue)))
606 return;
607 req = list_entry(ep->queue.next, struct goku_request, queue);
608 (ep->is_in ? write_fifo : read_fifo)(ep, req);
612 /*-------------------------------------------------------------------------*/
614 // return: 0 = q running, 1 = q stopped, negative = errno
615 static int start_dma(struct goku_ep *ep, struct goku_request *req)
617 struct goku_udc_regs __iomem *regs = ep->dev->regs;
618 u32 master;
619 u32 start = req->req.dma;
620 u32 end = start + req->req.length - 1;
622 master = readl(&regs->dma_master) & MST_RW_BITS;
624 /* re-init the bits affecting IN dma; careful with zlps */
625 if (likely(ep->is_in)) {
626 if (unlikely(master & MST_RD_ENA)) {
627 DBG (ep->dev, "start, IN active dma %03x!!\n",
628 master);
629 // return -EL2HLT;
631 writel(end, &regs->in_dma_end);
632 writel(start, &regs->in_dma_start);
634 master &= ~MST_R_BITS;
635 if (unlikely(req->req.length == 0))
636 master = MST_RD_ENA | MST_RD_EOPB;
637 else if ((req->req.length % ep->ep.maxpacket) != 0
638 || req->req.zero)
639 master = MST_RD_ENA | MST_EOPB_ENA;
640 else
641 master = MST_RD_ENA | MST_EOPB_DIS;
643 ep->dev->int_enable |= INT_MSTRDEND;
645 /* Goku DMA-OUT merges short packets, which plays poorly with
646 * protocols where short packets mark the transfer boundaries.
647 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
648 * ending transfers after 3 SOFs; we don't turn it on.
650 } else {
651 if (unlikely(master & MST_WR_ENA)) {
652 DBG (ep->dev, "start, OUT active dma %03x!!\n",
653 master);
654 // return -EL2HLT;
656 writel(end, &regs->out_dma_end);
657 writel(start, &regs->out_dma_start);
659 master &= ~MST_W_BITS;
660 master |= MST_WR_ENA | MST_TIMEOUT_DIS;
662 ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
665 writel(master, &regs->dma_master);
666 writel(ep->dev->int_enable, &regs->int_enable);
667 return 0;
670 static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
672 struct goku_request *req;
673 struct goku_udc_regs __iomem *regs = ep->dev->regs;
674 u32 master;
676 master = readl(&regs->dma_master);
678 if (unlikely(list_empty(&ep->queue))) {
679 stop:
680 if (ep->is_in)
681 dev->int_enable &= ~INT_MSTRDEND;
682 else
683 dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
684 writel(dev->int_enable, &regs->int_enable);
685 return;
687 req = list_entry(ep->queue.next, struct goku_request, queue);
689 /* normal hw dma completion (not abort) */
690 if (likely(ep->is_in)) {
691 if (unlikely(master & MST_RD_ENA))
692 return;
693 req->req.actual = readl(&regs->in_dma_current);
694 } else {
695 if (unlikely(master & MST_WR_ENA))
696 return;
698 /* hardware merges short packets, and also hides packet
699 * overruns. a partial packet MAY be in the fifo here.
701 req->req.actual = readl(&regs->out_dma_current);
703 req->req.actual -= req->req.dma;
704 req->req.actual++;
706 #ifdef USB_TRACE
707 VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
708 ep->ep.name, ep->is_in ? "IN" : "OUT",
709 req->req.actual, req->req.length, req);
710 #endif
711 done(ep, req, 0);
712 if (list_empty(&ep->queue))
713 goto stop;
714 req = list_entry(ep->queue.next, struct goku_request, queue);
715 (void) start_dma(ep, req);
718 static void abort_dma(struct goku_ep *ep, int status)
720 struct goku_udc_regs __iomem *regs = ep->dev->regs;
721 struct goku_request *req;
722 u32 curr, master;
724 /* NAK future host requests, hoping the implicit delay lets the
725 * dma engine finish reading (or writing) its latest packet and
726 * empty the dma buffer (up to 16 bytes).
728 * This avoids needing to clean up a partial packet in the fifo;
729 * we can't do that for IN without side effects to HALT and TOGGLE.
731 command(regs, COMMAND_FIFO_DISABLE, ep->num);
732 req = list_entry(ep->queue.next, struct goku_request, queue);
733 master = readl(&regs->dma_master) & MST_RW_BITS;
735 /* FIXME using these resets isn't usably documented. this may
736 * not work unless it's followed by disabling the endpoint.
738 * FIXME the OUT reset path doesn't even behave consistently.
740 if (ep->is_in) {
741 if (unlikely((readl(&regs->dma_master) & MST_RD_ENA) == 0))
742 goto finished;
743 curr = readl(&regs->in_dma_current);
745 writel(curr, &regs->in_dma_end);
746 writel(curr, &regs->in_dma_start);
748 master &= ~MST_R_BITS;
749 master |= MST_RD_RESET;
750 writel(master, &regs->dma_master);
752 if (readl(&regs->dma_master) & MST_RD_ENA)
753 DBG(ep->dev, "IN dma active after reset!\n");
755 } else {
756 if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
757 goto finished;
758 curr = readl(&regs->out_dma_current);
760 writel(curr, &regs->out_dma_end);
761 writel(curr, &regs->out_dma_start);
763 master &= ~MST_W_BITS;
764 master |= MST_WR_RESET;
765 writel(master, &regs->dma_master);
767 if (readl(&regs->dma_master) & MST_WR_ENA)
768 DBG(ep->dev, "OUT dma active after reset!\n");
770 req->req.actual = (curr - req->req.dma) + 1;
771 req->req.status = status;
773 VDBG(ep->dev, "%s %s %s %d/%d\n", __FUNCTION__, ep->ep.name,
774 ep->is_in ? "IN" : "OUT",
775 req->req.actual, req->req.length);
777 command(regs, COMMAND_FIFO_ENABLE, ep->num);
779 return;
781 finished:
782 /* dma already completed; no abort needed */
783 command(regs, COMMAND_FIFO_ENABLE, ep->num);
784 req->req.actual = req->req.length;
785 req->req.status = 0;
788 /*-------------------------------------------------------------------------*/
790 static int
791 goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
793 struct goku_request *req;
794 struct goku_ep *ep;
795 struct goku_udc *dev;
796 unsigned long flags;
797 int status;
799 /* always require a cpu-view buffer so pio works */
800 req = container_of(_req, struct goku_request, req);
801 if (unlikely(!_req || !_req->complete
802 || !_req->buf || !list_empty(&req->queue)))
803 return -EINVAL;
804 ep = container_of(_ep, struct goku_ep, ep);
805 if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
806 return -EINVAL;
807 dev = ep->dev;
808 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
809 return -ESHUTDOWN;
811 /* can't touch registers when suspended */
812 if (dev->ep0state == EP0_SUSPEND)
813 return -EBUSY;
815 /* set up dma mapping in case the caller didn't */
816 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
817 _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length,
818 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
819 req->mapped = 1;
822 #ifdef USB_TRACE
823 VDBG(dev, "%s queue req %p, len %u buf %p\n",
824 _ep->name, _req, _req->length, _req->buf);
825 #endif
827 spin_lock_irqsave(&dev->lock, flags);
829 _req->status = -EINPROGRESS;
830 _req->actual = 0;
832 /* for ep0 IN without premature status, zlp is required and
833 * writing EOP starts the status stage (OUT).
835 if (unlikely(ep->num == 0 && ep->is_in))
836 _req->zero = 1;
838 /* kickstart this i/o queue? */
839 status = 0;
840 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
841 /* dma: done after dma completion IRQ (or error)
842 * pio: done after last fifo operation
844 if (ep->dma)
845 status = start_dma(ep, req);
846 else
847 status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
849 if (unlikely(status != 0)) {
850 if (status > 0)
851 status = 0;
852 req = NULL;
855 } /* else pio or dma irq handler advances the queue. */
857 if (likely(req != 0))
858 list_add_tail(&req->queue, &ep->queue);
860 if (likely(!list_empty(&ep->queue))
861 && likely(ep->num != 0)
862 && !ep->dma
863 && !(dev->int_enable & INT_EPxDATASET (ep->num)))
864 pio_irq_enable(dev, dev->regs, ep->num);
866 spin_unlock_irqrestore(&dev->lock, flags);
868 /* pci writes may still be posted */
869 return status;
872 /* dequeue ALL requests */
873 static void nuke(struct goku_ep *ep, int status)
875 struct goku_request *req;
877 ep->stopped = 1;
878 if (list_empty(&ep->queue))
879 return;
880 if (ep->dma)
881 abort_dma(ep, status);
882 while (!list_empty(&ep->queue)) {
883 req = list_entry(ep->queue.next, struct goku_request, queue);
884 done(ep, req, status);
888 /* dequeue JUST ONE request */
889 static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
891 struct goku_request *req;
892 struct goku_ep *ep;
893 struct goku_udc *dev;
894 unsigned long flags;
896 ep = container_of(_ep, struct goku_ep, ep);
897 if (!_ep || !_req || (!ep->desc && ep->num != 0))
898 return -EINVAL;
899 dev = ep->dev;
900 if (!dev->driver)
901 return -ESHUTDOWN;
903 /* we can't touch (dma) registers when suspended */
904 if (dev->ep0state == EP0_SUSPEND)
905 return -EBUSY;
907 VDBG(dev, "%s %s %s %s %p\n", __FUNCTION__, _ep->name,
908 ep->is_in ? "IN" : "OUT",
909 ep->dma ? "dma" : "pio",
910 _req);
912 spin_lock_irqsave(&dev->lock, flags);
914 /* make sure it's actually queued on this endpoint */
915 list_for_each_entry (req, &ep->queue, queue) {
916 if (&req->req == _req)
917 break;
919 if (&req->req != _req) {
920 spin_unlock_irqrestore (&dev->lock, flags);
921 return -EINVAL;
924 if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
925 abort_dma(ep, -ECONNRESET);
926 done(ep, req, -ECONNRESET);
927 dma_advance(dev, ep);
928 } else if (!list_empty(&req->queue))
929 done(ep, req, -ECONNRESET);
930 else
931 req = NULL;
932 spin_unlock_irqrestore(&dev->lock, flags);
934 return req ? 0 : -EOPNOTSUPP;
937 /*-------------------------------------------------------------------------*/
939 static void goku_clear_halt(struct goku_ep *ep)
941 // assert (ep->num !=0)
942 VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
943 command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
944 command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
945 if (ep->stopped) {
946 ep->stopped = 0;
947 if (ep->dma) {
948 struct goku_request *req;
950 if (list_empty(&ep->queue))
951 return;
952 req = list_entry(ep->queue.next, struct goku_request,
953 queue);
954 (void) start_dma(ep, req);
955 } else
956 pio_advance(ep);
960 static int goku_set_halt(struct usb_ep *_ep, int value)
962 struct goku_ep *ep;
963 unsigned long flags;
964 int retval = 0;
966 if (!_ep)
967 return -ENODEV;
968 ep = container_of (_ep, struct goku_ep, ep);
970 if (ep->num == 0) {
971 if (value) {
972 ep->dev->ep0state = EP0_STALL;
973 ep->dev->ep[0].stopped = 1;
974 } else
975 return -EINVAL;
977 /* don't change EPxSTATUS_EP_INVALID to READY */
978 } else if (!ep->desc) {
979 DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
980 return -EINVAL;
983 spin_lock_irqsave(&ep->dev->lock, flags);
984 if (!list_empty(&ep->queue))
985 retval = -EAGAIN;
986 else if (ep->is_in && value
987 /* data in (either) packet buffer? */
988 && (readl(&ep->dev->regs->DataSet)
989 & DATASET_AB(ep->num)))
990 retval = -EAGAIN;
991 else if (!value)
992 goku_clear_halt(ep);
993 else {
994 ep->stopped = 1;
995 VDBG(ep->dev, "%s set halt\n", ep->ep.name);
996 command(ep->dev->regs, COMMAND_STALL, ep->num);
997 readl(ep->reg_status);
999 spin_unlock_irqrestore(&ep->dev->lock, flags);
1000 return retval;
1003 static int goku_fifo_status(struct usb_ep *_ep)
1005 struct goku_ep *ep;
1006 struct goku_udc_regs __iomem *regs;
1007 u32 size;
1009 if (!_ep)
1010 return -ENODEV;
1011 ep = container_of(_ep, struct goku_ep, ep);
1013 /* size is only reported sanely for OUT */
1014 if (ep->is_in)
1015 return -EOPNOTSUPP;
1017 /* ignores 16-byte dma buffer; SizeH == 0 */
1018 regs = ep->dev->regs;
1019 size = readl(&regs->EPxSizeLA[ep->num]) & DATASIZE;
1020 size += readl(&regs->EPxSizeLB[ep->num]) & DATASIZE;
1021 VDBG(ep->dev, "%s %s %u\n", __FUNCTION__, ep->ep.name, size);
1022 return size;
1025 static void goku_fifo_flush(struct usb_ep *_ep)
1027 struct goku_ep *ep;
1028 struct goku_udc_regs __iomem *regs;
1029 u32 size;
1031 if (!_ep)
1032 return;
1033 ep = container_of(_ep, struct goku_ep, ep);
1034 VDBG(ep->dev, "%s %s\n", __FUNCTION__, ep->ep.name);
1036 /* don't change EPxSTATUS_EP_INVALID to READY */
1037 if (!ep->desc && ep->num != 0) {
1038 DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
1039 return;
1042 regs = ep->dev->regs;
1043 size = readl(&regs->EPxSizeLA[ep->num]);
1044 size &= DATASIZE;
1046 /* Non-desirable behavior: FIFO_CLEAR also clears the
1047 * endpoint halt feature. For OUT, we _could_ just read
1048 * the bytes out (PIO, if !ep->dma); for in, no choice.
1050 if (size)
1051 command(regs, COMMAND_FIFO_CLEAR, ep->num);
1054 static struct usb_ep_ops goku_ep_ops = {
1055 .enable = goku_ep_enable,
1056 .disable = goku_ep_disable,
1058 .alloc_request = goku_alloc_request,
1059 .free_request = goku_free_request,
1061 .alloc_buffer = goku_alloc_buffer,
1062 .free_buffer = goku_free_buffer,
1064 .queue = goku_queue,
1065 .dequeue = goku_dequeue,
1067 .set_halt = goku_set_halt,
1068 .fifo_status = goku_fifo_status,
1069 .fifo_flush = goku_fifo_flush,
1072 /*-------------------------------------------------------------------------*/
1074 static int goku_get_frame(struct usb_gadget *_gadget)
1076 return -EOPNOTSUPP;
1079 static const struct usb_gadget_ops goku_ops = {
1080 .get_frame = goku_get_frame,
1081 // no remote wakeup
1082 // not selfpowered
1085 /*-------------------------------------------------------------------------*/
1087 static inline char *dmastr(void)
1089 if (use_dma == 0)
1090 return "(dma disabled)";
1091 else if (use_dma == 2)
1092 return "(dma IN and OUT)";
1093 else
1094 return "(dma IN)";
1097 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1099 static const char proc_node_name [] = "driver/udc";
1101 #define FOURBITS "%s%s%s%s"
1102 #define EIGHTBITS FOURBITS FOURBITS
1104 static void
1105 dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1107 int t;
1109 /* int_status is the same format ... */
1110 t = scnprintf(*next, *size,
1111 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1112 label, mask,
1113 (mask & INT_PWRDETECT) ? " power" : "",
1114 (mask & INT_SYSERROR) ? " sys" : "",
1115 (mask & INT_MSTRDEND) ? " in-dma" : "",
1116 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1118 (mask & INT_MSTWREND) ? " out-dma" : "",
1119 (mask & INT_MSTWRSET) ? " wrset" : "",
1120 (mask & INT_ERR) ? " err" : "",
1121 (mask & INT_SOF) ? " sof" : "",
1123 (mask & INT_EP3NAK) ? " ep3nak" : "",
1124 (mask & INT_EP2NAK) ? " ep2nak" : "",
1125 (mask & INT_EP1NAK) ? " ep1nak" : "",
1126 (mask & INT_EP3DATASET) ? " ep3" : "",
1128 (mask & INT_EP2DATASET) ? " ep2" : "",
1129 (mask & INT_EP1DATASET) ? " ep1" : "",
1130 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1131 (mask & INT_STATUS) ? " ep0status" : "",
1133 (mask & INT_SETUP) ? " setup" : "",
1134 (mask & INT_ENDPOINT0) ? " ep0" : "",
1135 (mask & INT_USBRESET) ? " reset" : "",
1136 (mask & INT_SUSPEND) ? " suspend" : "");
1137 *size -= t;
1138 *next += t;
1142 static int
1143 udc_proc_read(char *buffer, char **start, off_t off, int count,
1144 int *eof, void *_dev)
1146 char *buf = buffer;
1147 struct goku_udc *dev = _dev;
1148 struct goku_udc_regs __iomem *regs = dev->regs;
1149 char *next = buf;
1150 unsigned size = count;
1151 unsigned long flags;
1152 int i, t, is_usb_connected;
1153 u32 tmp;
1155 if (off != 0)
1156 return 0;
1158 local_irq_save(flags);
1160 /* basic device status */
1161 tmp = readl(&regs->power_detect);
1162 is_usb_connected = tmp & PW_DETECT;
1163 t = scnprintf(next, size,
1164 "%s - %s\n"
1165 "%s version: %s %s\n"
1166 "Gadget driver: %s\n"
1167 "Host %s, %s\n"
1168 "\n",
1169 pci_name(dev->pdev), driver_desc,
1170 driver_name, DRIVER_VERSION, dmastr(),
1171 dev->driver ? dev->driver->driver.name : "(none)",
1172 is_usb_connected
1173 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1174 : "disconnected",
1175 ({char *tmp;
1176 switch(dev->ep0state){
1177 case EP0_DISCONNECT: tmp = "ep0_disconnect"; break;
1178 case EP0_IDLE: tmp = "ep0_idle"; break;
1179 case EP0_IN: tmp = "ep0_in"; break;
1180 case EP0_OUT: tmp = "ep0_out"; break;
1181 case EP0_STATUS: tmp = "ep0_status"; break;
1182 case EP0_STALL: tmp = "ep0_stall"; break;
1183 case EP0_SUSPEND: tmp = "ep0_suspend"; break;
1184 default: tmp = "ep0_?"; break;
1185 } tmp; })
1187 size -= t;
1188 next += t;
1190 dump_intmask("int_status", readl(&regs->int_status), &next, &size);
1191 dump_intmask("int_enable", readl(&regs->int_enable), &next, &size);
1193 if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1194 goto done;
1196 /* registers for (active) device and ep0 */
1197 t = scnprintf(next, size, "\nirqs %lu\ndataset %02x "
1198 "single.bcs %02x.%02x state %x addr %u\n",
1199 dev->irqs, readl(&regs->DataSet),
1200 readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1201 readl(&regs->UsbState),
1202 readl(&regs->address));
1203 size -= t;
1204 next += t;
1206 tmp = readl(&regs->dma_master);
1207 t = scnprintf(next, size,
1208 "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1209 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1210 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1211 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1212 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1214 (tmp & MST_RD_EOPB) ? " eopb" : "",
1215 (tmp & MST_RD_RESET) ? " in_reset" : "",
1216 (tmp & MST_WR_RESET) ? " out_reset" : "",
1217 (tmp & MST_RD_ENA) ? " IN" : "",
1219 (tmp & MST_WR_ENA) ? " OUT" : "",
1220 (tmp & MST_CONNECTION)
1221 ? "ep1in/ep2out"
1222 : "ep1out/ep2in");
1223 size -= t;
1224 next += t;
1226 /* dump endpoint queues */
1227 for (i = 0; i < 4; i++) {
1228 struct goku_ep *ep = &dev->ep [i];
1229 struct goku_request *req;
1230 int t;
1232 if (i && !ep->desc)
1233 continue;
1235 tmp = readl(ep->reg_status);
1236 t = scnprintf(next, size,
1237 "%s %s max %u %s, irqs %lu, "
1238 "status %02x (%s) " FOURBITS "\n",
1239 ep->ep.name,
1240 ep->is_in ? "in" : "out",
1241 ep->ep.maxpacket,
1242 ep->dma ? "dma" : "pio",
1243 ep->irqs,
1244 tmp, ({ char *s;
1245 switch (tmp & EPxSTATUS_EP_MASK) {
1246 case EPxSTATUS_EP_READY:
1247 s = "ready"; break;
1248 case EPxSTATUS_EP_DATAIN:
1249 s = "packet"; break;
1250 case EPxSTATUS_EP_FULL:
1251 s = "full"; break;
1252 case EPxSTATUS_EP_TX_ERR: // host will retry
1253 s = "tx_err"; break;
1254 case EPxSTATUS_EP_RX_ERR:
1255 s = "rx_err"; break;
1256 case EPxSTATUS_EP_BUSY: /* ep0 only */
1257 s = "busy"; break;
1258 case EPxSTATUS_EP_STALL:
1259 s = "stall"; break;
1260 case EPxSTATUS_EP_INVALID: // these "can't happen"
1261 s = "invalid"; break;
1262 default:
1263 s = "?"; break;
1264 }; s; }),
1265 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1266 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1267 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1268 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1270 if (t <= 0 || t > size)
1271 goto done;
1272 size -= t;
1273 next += t;
1275 if (list_empty(&ep->queue)) {
1276 t = scnprintf(next, size, "\t(nothing queued)\n");
1277 if (t <= 0 || t > size)
1278 goto done;
1279 size -= t;
1280 next += t;
1281 continue;
1283 list_for_each_entry(req, &ep->queue, queue) {
1284 if (ep->dma && req->queue.prev == &ep->queue) {
1285 if (i == UDC_MSTRD_ENDPOINT)
1286 tmp = readl(&regs->in_dma_current);
1287 else
1288 tmp = readl(&regs->out_dma_current);
1289 tmp -= req->req.dma;
1290 tmp++;
1291 } else
1292 tmp = req->req.actual;
1294 t = scnprintf(next, size,
1295 "\treq %p len %u/%u buf %p\n",
1296 &req->req, tmp, req->req.length,
1297 req->req.buf);
1298 if (t <= 0 || t > size)
1299 goto done;
1300 size -= t;
1301 next += t;
1305 done:
1306 local_irq_restore(flags);
1307 *eof = 1;
1308 return count - size;
1311 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1313 /*-------------------------------------------------------------------------*/
1315 static void udc_reinit (struct goku_udc *dev)
1317 static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1319 unsigned i;
1321 INIT_LIST_HEAD (&dev->gadget.ep_list);
1322 dev->gadget.ep0 = &dev->ep [0].ep;
1323 dev->gadget.speed = USB_SPEED_UNKNOWN;
1324 dev->ep0state = EP0_DISCONNECT;
1325 dev->irqs = 0;
1327 for (i = 0; i < 4; i++) {
1328 struct goku_ep *ep = &dev->ep[i];
1330 ep->num = i;
1331 ep->ep.name = names[i];
1332 ep->reg_fifo = &dev->regs->ep_fifo [i];
1333 ep->reg_status = &dev->regs->ep_status [i];
1334 ep->reg_mode = &dev->regs->ep_mode[i];
1336 ep->ep.ops = &goku_ep_ops;
1337 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1338 ep->dev = dev;
1339 INIT_LIST_HEAD (&ep->queue);
1341 ep_reset(NULL, ep);
1344 dev->ep[0].reg_mode = NULL;
1345 dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1346 list_del_init (&dev->ep[0].ep.ep_list);
1349 static void udc_reset(struct goku_udc *dev)
1351 struct goku_udc_regs __iomem *regs = dev->regs;
1353 writel(0, &regs->power_detect);
1354 writel(0, &regs->int_enable);
1355 readl(&regs->int_enable);
1356 dev->int_enable = 0;
1358 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1359 * don't let INT_PWRDETECT sequence begin
1361 udelay(250);
1362 writel(PW_RESETB, &regs->power_detect);
1363 readl(&regs->int_enable);
1366 static void ep0_start(struct goku_udc *dev)
1368 struct goku_udc_regs __iomem *regs = dev->regs;
1369 unsigned i;
1371 VDBG(dev, "%s\n", __FUNCTION__);
1373 udc_reset(dev);
1374 udc_reinit (dev);
1375 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
1377 /* hw handles set_address, set_feature, get_status; maybe more */
1378 writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
1379 | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
1380 | G_REQMODE_GET_DESC
1381 | G_REQMODE_CLEAR_FEAT
1382 , &regs->reqmode);
1384 for (i = 0; i < 4; i++)
1385 dev->ep[i].irqs = 0;
1387 /* can't modify descriptors after writing UsbReady */
1388 for (i = 0; i < DESC_LEN; i++)
1389 writel(0, &regs->descriptors[i]);
1390 writel(0, &regs->UsbReady);
1392 /* expect ep0 requests when the host drops reset */
1393 writel(PW_RESETB | PW_PULLUP, &regs->power_detect);
1394 dev->int_enable = INT_DEVWIDE | INT_EP0;
1395 writel(dev->int_enable, &dev->regs->int_enable);
1396 readl(&regs->int_enable);
1397 dev->gadget.speed = USB_SPEED_FULL;
1398 dev->ep0state = EP0_IDLE;
1401 static void udc_enable(struct goku_udc *dev)
1403 /* start enumeration now, or after power detect irq */
1404 if (readl(&dev->regs->power_detect) & PW_DETECT)
1405 ep0_start(dev);
1406 else {
1407 DBG(dev, "%s\n", __FUNCTION__);
1408 dev->int_enable = INT_PWRDETECT;
1409 writel(dev->int_enable, &dev->regs->int_enable);
1413 /*-------------------------------------------------------------------------*/
1415 /* keeping it simple:
1416 * - one bus driver, initted first;
1417 * - one function driver, initted second
1420 static struct goku_udc *the_controller;
1422 /* when a driver is successfully registered, it will receive
1423 * control requests including set_configuration(), which enables
1424 * non-control requests. then usb traffic follows until a
1425 * disconnect is reported. then a host may connect again, or
1426 * the driver might get unbound.
1428 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1430 struct goku_udc *dev = the_controller;
1431 int retval;
1433 if (!driver
1434 || driver->speed != USB_SPEED_FULL
1435 || !driver->bind
1436 || !driver->unbind
1437 || !driver->disconnect
1438 || !driver->setup)
1439 return -EINVAL;
1440 if (!dev)
1441 return -ENODEV;
1442 if (dev->driver)
1443 return -EBUSY;
1445 /* hook up the driver */
1446 driver->driver.bus = NULL;
1447 dev->driver = driver;
1448 dev->gadget.dev.driver = &driver->driver;
1449 retval = driver->bind(&dev->gadget);
1450 if (retval) {
1451 DBG(dev, "bind to driver %s --> error %d\n",
1452 driver->driver.name, retval);
1453 dev->driver = NULL;
1454 dev->gadget.dev.driver = NULL;
1455 return retval;
1458 /* then enable host detection and ep0; and we're ready
1459 * for set_configuration as well as eventual disconnect.
1461 udc_enable(dev);
1463 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1464 return 0;
1466 EXPORT_SYMBOL(usb_gadget_register_driver);
1468 static void
1469 stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1471 unsigned i;
1473 DBG (dev, "%s\n", __FUNCTION__);
1475 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1476 driver = NULL;
1478 /* disconnect gadget driver after quiesceing hw and the driver */
1479 udc_reset (dev);
1480 for (i = 0; i < 4; i++)
1481 nuke(&dev->ep [i], -ESHUTDOWN);
1482 if (driver) {
1483 spin_unlock(&dev->lock);
1484 driver->disconnect(&dev->gadget);
1485 spin_lock(&dev->lock);
1488 if (dev->driver)
1489 udc_enable(dev);
1492 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1494 struct goku_udc *dev = the_controller;
1495 unsigned long flags;
1497 if (!dev)
1498 return -ENODEV;
1499 if (!driver || driver != dev->driver)
1500 return -EINVAL;
1502 spin_lock_irqsave(&dev->lock, flags);
1503 dev->driver = NULL;
1504 stop_activity(dev, driver);
1505 spin_unlock_irqrestore(&dev->lock, flags);
1507 driver->unbind(&dev->gadget);
1509 DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1510 return 0;
1512 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1515 /*-------------------------------------------------------------------------*/
1517 static void ep0_setup(struct goku_udc *dev)
1519 struct goku_udc_regs __iomem *regs = dev->regs;
1520 struct usb_ctrlrequest ctrl;
1521 int tmp;
1523 /* read SETUP packet and enter DATA stage */
1524 ctrl.bRequestType = readl(&regs->bRequestType);
1525 ctrl.bRequest = readl(&regs->bRequest);
1526 ctrl.wValue = cpu_to_le16((readl(&regs->wValueH) << 8)
1527 | readl(&regs->wValueL));
1528 ctrl.wIndex = cpu_to_le16((readl(&regs->wIndexH) << 8)
1529 | readl(&regs->wIndexL));
1530 ctrl.wLength = cpu_to_le16((readl(&regs->wLengthH) << 8)
1531 | readl(&regs->wLengthL));
1532 writel(0, &regs->SetupRecv);
1534 nuke(&dev->ep[0], 0);
1535 dev->ep[0].stopped = 0;
1536 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1537 dev->ep[0].is_in = 1;
1538 dev->ep0state = EP0_IN;
1539 /* detect early status stages */
1540 writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1541 } else {
1542 dev->ep[0].is_in = 0;
1543 dev->ep0state = EP0_OUT;
1545 /* NOTE: CLEAR_FEATURE is done in software so that we can
1546 * synchronize transfer restarts after bulk IN stalls. data
1547 * won't even enter the fifo until the halt is cleared.
1549 switch (ctrl.bRequest) {
1550 case USB_REQ_CLEAR_FEATURE:
1551 switch (ctrl.bRequestType) {
1552 case USB_RECIP_ENDPOINT:
1553 tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
1554 /* active endpoint */
1555 if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
1556 goto stall;
1557 if (ctrl.wIndex & __constant_cpu_to_le16(
1558 USB_DIR_IN)) {
1559 if (!dev->ep[tmp].is_in)
1560 goto stall;
1561 } else {
1562 if (dev->ep[tmp].is_in)
1563 goto stall;
1565 if (ctrl.wValue != __constant_cpu_to_le16(
1566 USB_ENDPOINT_HALT))
1567 goto stall;
1568 if (tmp)
1569 goku_clear_halt(&dev->ep[tmp]);
1570 succeed:
1571 /* start ep0out status stage */
1572 writel(~(1<<0), &regs->EOP);
1573 dev->ep[0].stopped = 1;
1574 dev->ep0state = EP0_STATUS;
1575 return;
1576 case USB_RECIP_DEVICE:
1577 /* device remote wakeup: always clear */
1578 if (ctrl.wValue != __constant_cpu_to_le16(1))
1579 goto stall;
1580 VDBG(dev, "clear dev remote wakeup\n");
1581 goto succeed;
1582 case USB_RECIP_INTERFACE:
1583 goto stall;
1584 default: /* pass to gadget driver */
1585 break;
1587 break;
1588 default:
1589 break;
1593 #ifdef USB_TRACE
1594 VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1595 ctrl.bRequestType, ctrl.bRequest,
1596 le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1597 le16_to_cpu(ctrl.wLength));
1598 #endif
1600 /* hw wants to know when we're configured (or not) */
1601 dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1602 && ctrl.bRequestType == USB_RECIP_DEVICE);
1603 if (unlikely(dev->req_config))
1604 dev->configured = (ctrl.wValue != __constant_cpu_to_le16(0));
1606 /* delegate everything to the gadget driver.
1607 * it may respond after this irq handler returns.
1609 spin_unlock (&dev->lock);
1610 tmp = dev->driver->setup(&dev->gadget, &ctrl);
1611 spin_lock (&dev->lock);
1612 if (unlikely(tmp < 0)) {
1613 stall:
1614 #ifdef USB_TRACE
1615 VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1616 ctrl.bRequestType, ctrl.bRequest, tmp);
1617 #endif
1618 command(regs, COMMAND_STALL, 0);
1619 dev->ep[0].stopped = 1;
1620 dev->ep0state = EP0_STALL;
1623 /* expect at least one data or status stage irq */
1626 #define ACK(irqbit) { \
1627 stat &= ~irqbit; \
1628 writel(~irqbit, &regs->int_status); \
1629 handled = 1; \
1632 static irqreturn_t goku_irq(int irq, void *_dev, struct pt_regs *r)
1634 struct goku_udc *dev = _dev;
1635 struct goku_udc_regs __iomem *regs = dev->regs;
1636 struct goku_ep *ep;
1637 u32 stat, handled = 0;
1638 unsigned i, rescans = 5;
1640 spin_lock(&dev->lock);
1642 rescan:
1643 stat = readl(&regs->int_status) & dev->int_enable;
1644 if (!stat)
1645 goto done;
1646 dev->irqs++;
1648 /* device-wide irqs */
1649 if (unlikely(stat & INT_DEVWIDE)) {
1650 if (stat & INT_SYSERROR) {
1651 ERROR(dev, "system error\n");
1652 stop_activity(dev, dev->driver);
1653 stat = 0;
1654 handled = 1;
1655 // FIXME have a neater way to prevent re-enumeration
1656 dev->driver = NULL;
1657 goto done;
1659 if (stat & INT_PWRDETECT) {
1660 writel(~stat, &regs->int_status);
1661 if (readl(&dev->regs->power_detect) & PW_DETECT) {
1662 VDBG(dev, "connect\n");
1663 ep0_start(dev);
1664 } else {
1665 DBG(dev, "disconnect\n");
1666 if (dev->gadget.speed == USB_SPEED_FULL)
1667 stop_activity(dev, dev->driver);
1668 dev->ep0state = EP0_DISCONNECT;
1669 dev->int_enable = INT_DEVWIDE;
1670 writel(dev->int_enable, &dev->regs->int_enable);
1672 stat = 0;
1673 handled = 1;
1674 goto done;
1676 if (stat & INT_SUSPEND) {
1677 ACK(INT_SUSPEND);
1678 if (readl(&regs->ep_status[0]) & EPxSTATUS_SUSPEND) {
1679 switch (dev->ep0state) {
1680 case EP0_DISCONNECT:
1681 case EP0_SUSPEND:
1682 goto pm_next;
1683 default:
1684 break;
1686 DBG(dev, "USB suspend\n");
1687 dev->ep0state = EP0_SUSPEND;
1688 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1689 && dev->driver
1690 && dev->driver->suspend) {
1691 spin_unlock(&dev->lock);
1692 dev->driver->suspend(&dev->gadget);
1693 spin_lock(&dev->lock);
1695 } else {
1696 if (dev->ep0state != EP0_SUSPEND) {
1697 DBG(dev, "bogus USB resume %d\n",
1698 dev->ep0state);
1699 goto pm_next;
1701 DBG(dev, "USB resume\n");
1702 dev->ep0state = EP0_IDLE;
1703 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1704 && dev->driver
1705 && dev->driver->resume) {
1706 spin_unlock(&dev->lock);
1707 dev->driver->resume(&dev->gadget);
1708 spin_lock(&dev->lock);
1712 pm_next:
1713 if (stat & INT_USBRESET) { /* hub reset done */
1714 ACK(INT_USBRESET);
1715 INFO(dev, "USB reset done, gadget %s\n",
1716 dev->driver->driver.name);
1718 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1721 /* progress ep0 setup, data, or status stages.
1722 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1724 if (stat & INT_SETUP) {
1725 ACK(INT_SETUP);
1726 dev->ep[0].irqs++;
1727 ep0_setup(dev);
1729 if (stat & INT_STATUSNAK) {
1730 ACK(INT_STATUSNAK|INT_ENDPOINT0);
1731 if (dev->ep0state == EP0_IN) {
1732 ep = &dev->ep[0];
1733 ep->irqs++;
1734 nuke(ep, 0);
1735 writel(~(1<<0), &regs->EOP);
1736 dev->ep0state = EP0_STATUS;
1739 if (stat & INT_ENDPOINT0) {
1740 ACK(INT_ENDPOINT0);
1741 ep = &dev->ep[0];
1742 ep->irqs++;
1743 pio_advance(ep);
1746 /* dma completion */
1747 if (stat & INT_MSTRDEND) { /* IN */
1748 ACK(INT_MSTRDEND);
1749 ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1750 ep->irqs++;
1751 dma_advance(dev, ep);
1753 if (stat & INT_MSTWREND) { /* OUT */
1754 ACK(INT_MSTWREND);
1755 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1756 ep->irqs++;
1757 dma_advance(dev, ep);
1759 if (stat & INT_MSTWRTMOUT) { /* OUT */
1760 ACK(INT_MSTWRTMOUT);
1761 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1762 ep->irqs++;
1763 ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1764 // reset dma? then dma_advance()
1767 /* pio */
1768 for (i = 1; i < 4; i++) {
1769 u32 tmp = INT_EPxDATASET(i);
1771 if (!(stat & tmp))
1772 continue;
1773 ep = &dev->ep[i];
1774 pio_advance(ep);
1775 if (list_empty (&ep->queue))
1776 pio_irq_disable(dev, regs, i);
1777 stat &= ~tmp;
1778 handled = 1;
1779 ep->irqs++;
1782 if (rescans--)
1783 goto rescan;
1785 done:
1786 (void)readl(&regs->int_enable);
1787 spin_unlock(&dev->lock);
1788 if (stat)
1789 DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1790 readl(&regs->int_status), dev->int_enable);
1791 return IRQ_RETVAL(handled);
1794 #undef ACK
1796 /*-------------------------------------------------------------------------*/
1798 static void gadget_release(struct device *_dev)
1800 struct goku_udc *dev = dev_get_drvdata(_dev);
1802 kfree(dev);
1805 /* tear down the binding between this driver and the pci device */
1807 static void goku_remove(struct pci_dev *pdev)
1809 struct goku_udc *dev = pci_get_drvdata(pdev);
1811 DBG(dev, "%s\n", __FUNCTION__);
1812 /* start with the driver above us */
1813 if (dev->driver) {
1814 /* should have been done already by driver model core */
1815 WARN(dev, "pci remove, driver '%s' is still registered\n",
1816 dev->driver->driver.name);
1817 usb_gadget_unregister_driver(dev->driver);
1820 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1821 remove_proc_entry(proc_node_name, NULL);
1822 #endif
1823 if (dev->regs)
1824 udc_reset(dev);
1825 if (dev->got_irq)
1826 free_irq(pdev->irq, dev);
1827 if (dev->regs)
1828 iounmap(dev->regs);
1829 if (dev->got_region)
1830 release_mem_region(pci_resource_start (pdev, 0),
1831 pci_resource_len (pdev, 0));
1832 if (dev->enabled)
1833 pci_disable_device(pdev);
1834 device_unregister(&dev->gadget.dev);
1836 pci_set_drvdata(pdev, NULL);
1837 dev->regs = NULL;
1838 the_controller = NULL;
1840 INFO(dev, "unbind\n");
1843 /* wrap this driver around the specified pci device, but
1844 * don't respond over USB until a gadget driver binds to us.
1847 static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1849 struct goku_udc *dev = NULL;
1850 unsigned long resource, len;
1851 void __iomem *base = NULL;
1852 int retval;
1853 char buf [8], *bufp;
1855 /* if you want to support more than one controller in a system,
1856 * usb_gadget_driver_{register,unregister}() must change.
1858 if (the_controller) {
1859 WARN(dev, "ignoring %s\n", pci_name(pdev));
1860 return -EBUSY;
1862 if (!pdev->irq) {
1863 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1864 retval = -ENODEV;
1865 goto done;
1868 /* alloc, and start init */
1869 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1870 if (dev == NULL){
1871 pr_debug("enomem %s\n", pci_name(pdev));
1872 retval = -ENOMEM;
1873 goto done;
1876 memset(dev, 0, sizeof *dev);
1877 spin_lock_init(&dev->lock);
1878 dev->pdev = pdev;
1879 dev->gadget.ops = &goku_ops;
1881 /* the "gadget" abstracts/virtualizes the controller */
1882 strcpy(dev->gadget.dev.bus_id, "gadget");
1883 dev->gadget.dev.parent = &pdev->dev;
1884 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1885 dev->gadget.dev.release = gadget_release;
1886 dev->gadget.name = driver_name;
1888 /* now all the pci goodies ... */
1889 retval = pci_enable_device(pdev);
1890 if (retval < 0) {
1891 DBG(dev, "can't enable, %d\n", retval);
1892 goto done;
1894 dev->enabled = 1;
1896 resource = pci_resource_start(pdev, 0);
1897 len = pci_resource_len(pdev, 0);
1898 if (!request_mem_region(resource, len, driver_name)) {
1899 DBG(dev, "controller already in use\n");
1900 retval = -EBUSY;
1901 goto done;
1903 dev->got_region = 1;
1905 base = ioremap_nocache(resource, len);
1906 if (base == NULL) {
1907 DBG(dev, "can't map memory\n");
1908 retval = -EFAULT;
1909 goto done;
1911 dev->regs = (struct goku_udc_regs __iomem *) base;
1913 pci_set_drvdata(pdev, dev);
1914 INFO(dev, "%s\n", driver_desc);
1915 INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1916 #ifndef __sparc__
1917 scnprintf(buf, sizeof buf, "%d", pdev->irq);
1918 bufp = buf;
1919 #else
1920 bufp = __irq_itoa(pdev->irq);
1921 #endif
1922 INFO(dev, "irq %s, pci mem %p\n", bufp, base);
1924 /* init to known state, then setup irqs */
1925 udc_reset(dev);
1926 udc_reinit (dev);
1927 if (request_irq(pdev->irq, goku_irq, SA_SHIRQ/*|SA_SAMPLE_RANDOM*/,
1928 driver_name, dev) != 0) {
1929 DBG(dev, "request interrupt %s failed\n", bufp);
1930 retval = -EBUSY;
1931 goto done;
1933 dev->got_irq = 1;
1934 if (use_dma)
1935 pci_set_master(pdev);
1938 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1939 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1940 #endif
1942 /* done */
1943 the_controller = dev;
1944 device_register(&dev->gadget.dev);
1946 return 0;
1948 done:
1949 if (dev)
1950 goku_remove (pdev);
1951 return retval;
1955 /*-------------------------------------------------------------------------*/
1957 static struct pci_device_id pci_ids [] = { {
1958 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1959 .class_mask = ~0,
1960 .vendor = 0x102f, /* Toshiba */
1961 .device = 0x0107, /* this UDC */
1962 .subvendor = PCI_ANY_ID,
1963 .subdevice = PCI_ANY_ID,
1965 }, { /* end: all zeroes */ }
1967 MODULE_DEVICE_TABLE (pci, pci_ids);
1969 static struct pci_driver goku_pci_driver = {
1970 .name = (char *) driver_name,
1971 .id_table = pci_ids,
1973 .probe = goku_probe,
1974 .remove = goku_remove,
1976 /* FIXME add power management support */
1979 static int __init init (void)
1981 return pci_register_driver (&goku_pci_driver);
1983 module_init (init);
1985 static void __exit cleanup (void)
1987 pci_unregister_driver (&goku_pci_driver);
1989 module_exit (cleanup);