MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / gadget / goku_udc.c
blob61ce5b2eea41f6e8abe4a028c29fa8560cd5a9f9
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 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 *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 *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 *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 *regs = ep->dev->regs;
213 u32 tmp;
215 tmp = readl(&regs->EPxSingle);
216 tmp &= ~(0x11 << ep->num);
217 writel(tmp, &regs->EPxSingle);
219 tmp = readl(&regs->EPxBCS);
220 tmp &= ~(0x11 << ep->num);
221 writel(tmp, &regs->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 = 0;
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, int gfp_flags)
274 struct goku_request *req;
276 if (!_ep)
277 return 0;
278 req = kmalloc(sizeof *req, gfp_flags);
279 if (!req)
280 return 0;
282 memset(req, 0, sizeof *req);
283 req->req.dma = DMA_ADDR_INVALID;
284 INIT_LIST_HEAD(&req->queue);
285 return &req->req;
288 static void
289 goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
291 struct goku_request *req;
293 if (!_ep || !_req)
294 return;
296 req = container_of(_req, struct goku_request, req);
297 WARN_ON(!list_empty(&req->queue));
298 kfree(req);
301 /*-------------------------------------------------------------------------*/
303 #undef USE_KMALLOC
305 /* many common platforms have dma-coherent caches, which means that it's
306 * safe to use kmalloc() memory for all i/o buffers without using any
307 * cache flushing calls. (unless you're trying to share cache lines
308 * between dma and non-dma activities, which is a slow idea in any case.)
310 * other platforms need more care, with 2.6 having a moderately general
311 * solution except for the common "buffer is smaller than a page" case.
313 #if defined(CONFIG_X86)
314 #define USE_KMALLOC
316 #elif defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
317 #define USE_KMALLOC
319 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
320 #define USE_KMALLOC
322 #endif
324 /* allocating buffers this way eliminates dma mapping overhead, which
325 * on some platforms will mean eliminating a per-io buffer copy. with
326 * some kinds of system caches, further tweaks may still be needed.
328 static void *
329 goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
330 dma_addr_t *dma, int gfp_flags)
332 void *retval;
333 struct goku_ep *ep;
335 ep = container_of(_ep, struct goku_ep, ep);
336 if (!_ep)
337 return 0;
338 *dma = DMA_ADDR_INVALID;
340 #if defined(USE_KMALLOC)
341 retval = kmalloc(bytes, gfp_flags);
342 if (retval)
343 *dma = virt_to_phys(retval);
344 #else
345 if (ep->dma) {
346 /* the main problem with this call is that it wastes memory
347 * on typical 1/N page allocations: it allocates 1-N pages.
349 #warning Using dma_alloc_coherent even with buffers smaller than a page.
350 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
351 bytes, dma, gfp_flags);
352 } else
353 retval = kmalloc(bytes, gfp_flags);
354 #endif
355 return retval;
358 static void
359 goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
361 /* free memory into the right allocator */
362 #ifndef USE_KMALLOC
363 if (dma != DMA_ADDR_INVALID) {
364 struct goku_ep *ep;
366 ep = container_of(_ep, struct goku_ep, ep);
367 if (!_ep)
368 return;
369 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
370 } else
371 #endif
372 kfree (buf);
375 /*-------------------------------------------------------------------------*/
377 static void
378 done(struct goku_ep *ep, struct goku_request *req, int status)
380 struct goku_udc *dev;
381 unsigned stopped = ep->stopped;
383 list_del_init(&req->queue);
385 if (likely(req->req.status == -EINPROGRESS))
386 req->req.status = status;
387 else
388 status = req->req.status;
390 dev = ep->dev;
391 if (req->mapped) {
392 pci_unmap_single(dev->pdev, req->req.dma, req->req.length,
393 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
394 req->req.dma = DMA_ADDR_INVALID;
395 req->mapped = 0;
398 #ifndef USB_TRACE
399 if (status && status != -ESHUTDOWN)
400 #endif
401 VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
402 ep->ep.name, &req->req, status,
403 req->req.actual, req->req.length);
405 /* don't modify queue heads during completion callback */
406 ep->stopped = 1;
407 spin_unlock(&dev->lock);
408 req->req.complete(&ep->ep, &req->req);
409 spin_lock(&dev->lock);
410 ep->stopped = stopped;
413 /*-------------------------------------------------------------------------*/
415 static inline int
416 write_packet(u32 *fifo, u8 *buf, struct goku_request *req, unsigned max)
418 unsigned length, count;
420 length = min(req->req.length - req->req.actual, max);
421 req->req.actual += length;
423 count = length;
424 while (likely(count--))
425 writel(*buf++, fifo);
426 return length;
429 // return: 0 = still running, 1 = completed, negative = errno
430 static int write_fifo(struct goku_ep *ep, struct goku_request *req)
432 struct goku_udc *dev = ep->dev;
433 u32 tmp;
434 u8 *buf;
435 unsigned count;
436 int is_last;
438 tmp = readl(&dev->regs->DataSet);
439 buf = req->req.buf + req->req.actual;
440 prefetch(buf);
442 dev = ep->dev;
443 if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
444 return -EL2HLT;
446 /* NOTE: just single-buffered PIO-IN for now. */
447 if (unlikely((tmp & DATASET_A(ep->num)) != 0))
448 return 0;
450 /* clear our "packet available" irq */
451 if (ep->num != 0)
452 writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
454 count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
456 /* last packet often short (sometimes a zlp, especially on ep0) */
457 if (unlikely(count != ep->ep.maxpacket)) {
458 writel(~(1<<ep->num), &dev->regs->EOP);
459 if (ep->num == 0) {
460 dev->ep[0].stopped = 1;
461 dev->ep0state = EP0_STATUS;
463 is_last = 1;
464 } else {
465 if (likely(req->req.length != req->req.actual)
466 || req->req.zero)
467 is_last = 0;
468 else
469 is_last = 1;
471 #if 0 /* printk seemed to trash is_last...*/
472 //#ifdef USB_TRACE
473 VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
474 ep->ep.name, count, is_last ? "/last" : "",
475 req->req.length - req->req.actual, req);
476 #endif
478 /* requests complete when all IN data is in the FIFO,
479 * or sometimes later, if a zlp was needed.
481 if (is_last) {
482 done(ep, req, 0);
483 return 1;
486 return 0;
489 static int read_fifo(struct goku_ep *ep, struct goku_request *req)
491 struct goku_udc_regs *regs;
492 u32 size, set;
493 u8 *buf;
494 unsigned bufferspace, is_short, dbuff;
496 regs = ep->dev->regs;
497 top:
498 buf = req->req.buf + req->req.actual;
499 prefetchw(buf);
501 if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
502 return -EL2HLT;
504 dbuff = (ep->num == 1 || ep->num == 2);
505 do {
506 /* ack dataset irq matching the status we'll handle */
507 if (ep->num != 0)
508 writel(~INT_EPxDATASET(ep->num), &regs->int_status);
510 set = readl(&regs->DataSet) & DATASET_AB(ep->num);
511 size = readl(&regs->EPxSizeLA[ep->num]);
512 bufferspace = req->req.length - req->req.actual;
514 /* usually do nothing without an OUT packet */
515 if (likely(ep->num != 0 || bufferspace != 0)) {
516 if (unlikely(set == 0))
517 break;
518 /* use ep1/ep2 double-buffering for OUT */
519 if (!(size & PACKET_ACTIVE))
520 size = readl(&regs->EPxSizeLB[ep->num]);
521 if (!(size & PACKET_ACTIVE)) // "can't happen"
522 break;
523 size &= DATASIZE; /* EPxSizeH == 0 */
525 /* ep0out no-out-data case for set_config, etc */
526 } else
527 size = 0;
529 /* read all bytes from this packet */
530 req->req.actual += size;
531 is_short = (size < ep->ep.maxpacket);
532 #ifdef USB_TRACE
533 VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
534 ep->ep.name, size, is_short ? "/S" : "",
535 req, req->req.actual, req->req.length);
536 #endif
537 while (likely(size-- != 0)) {
538 u8 byte = (u8) readl(ep->reg_fifo);
540 if (unlikely(bufferspace == 0)) {
541 /* this happens when the driver's buffer
542 * is smaller than what the host sent.
543 * discard the extra data in this packet.
545 if (req->req.status != -EOVERFLOW)
546 DBG(ep->dev, "%s overflow %u\n",
547 ep->ep.name, size);
548 req->req.status = -EOVERFLOW;
549 } else {
550 *buf++ = byte;
551 bufferspace--;
555 /* completion */
556 if (unlikely(is_short || req->req.actual == req->req.length)) {
557 if (unlikely(ep->num == 0)) {
558 /* non-control endpoints now usable? */
559 if (ep->dev->req_config)
560 writel(ep->dev->configured
561 ? USBSTATE_CONFIGURED
562 : 0,
563 &regs->UsbState);
564 /* ep0out status stage */
565 writel(~(1<<0), &regs->EOP);
566 ep->stopped = 1;
567 ep->dev->ep0state = EP0_STATUS;
569 done(ep, req, 0);
571 /* empty the second buffer asap */
572 if (dbuff && !list_empty(&ep->queue)) {
573 req = list_entry(ep->queue.next,
574 struct goku_request, queue);
575 goto top;
577 return 1;
579 } while (dbuff);
580 return 0;
583 static inline void
584 pio_irq_enable(struct goku_udc *dev, struct goku_udc_regs *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, struct goku_udc_regs *regs, int epnum)
594 dev->int_enable &= ~INT_EPxDATASET (epnum);
595 writel(dev->int_enable, &regs->int_enable);
596 /* write may still be posted */
599 static inline void
600 pio_advance(struct goku_ep *ep)
602 struct goku_request *req;
604 if (unlikely(list_empty (&ep->queue)))
605 return;
606 req = list_entry(ep->queue.next, struct goku_request, queue);
607 (ep->is_in ? write_fifo : read_fifo)(ep, req);
611 /*-------------------------------------------------------------------------*/
613 // return: 0 = q running, 1 = q stopped, negative = errno
614 static int start_dma(struct goku_ep *ep, struct goku_request *req)
616 struct goku_udc_regs *regs = ep->dev->regs;
617 u32 master;
618 u32 start = req->req.dma;
619 u32 end = start + req->req.length - 1;
621 master = readl(&regs->dma_master) & MST_RW_BITS;
623 /* re-init the bits affecting IN dma; careful with zlps */
624 if (likely(ep->is_in)) {
625 if (unlikely(master & MST_RD_ENA)) {
626 DBG (ep->dev, "start, IN active dma %03x!!\n",
627 master);
628 // return -EL2HLT;
630 writel(end, &regs->in_dma_end);
631 writel(start, &regs->in_dma_start);
633 master &= ~MST_R_BITS;
634 if (unlikely(req->req.length == 0))
635 master = MST_RD_ENA | MST_RD_EOPB;
636 else if ((req->req.length % ep->ep.maxpacket) != 0
637 || req->req.zero)
638 master = MST_RD_ENA | MST_EOPB_ENA;
639 else
640 master = MST_RD_ENA | MST_EOPB_DIS;
642 ep->dev->int_enable |= INT_MSTRDEND;
644 /* Goku DMA-OUT merges short packets, which plays poorly with
645 * protocols where short packets mark the transfer boundaries.
646 * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
647 * ending transfers after 3 SOFs; we don't turn it on.
649 } else {
650 if (unlikely(master & MST_WR_ENA)) {
651 DBG (ep->dev, "start, OUT active dma %03x!!\n",
652 master);
653 // return -EL2HLT;
655 writel(end, &regs->out_dma_end);
656 writel(start, &regs->out_dma_start);
658 master &= ~MST_W_BITS;
659 master |= MST_WR_ENA | MST_TIMEOUT_DIS;
661 ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
664 writel(master, &regs->dma_master);
665 writel(ep->dev->int_enable, &regs->int_enable);
666 return 0;
669 static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
671 struct goku_request *req;
672 struct goku_udc_regs *regs = ep->dev->regs;
673 u32 master;
675 master = readl(&regs->dma_master);
677 if (unlikely(list_empty(&ep->queue))) {
678 stop:
679 if (ep->is_in)
680 dev->int_enable &= ~INT_MSTRDEND;
681 else
682 dev->int_enable &= ~(INT_MSTWREND|INT_MSTWRTMOUT);
683 writel(dev->int_enable, &regs->int_enable);
684 return;
686 req = list_entry(ep->queue.next, struct goku_request, queue);
688 /* normal hw dma completion (not abort) */
689 if (likely(ep->is_in)) {
690 if (unlikely(master & MST_RD_ENA))
691 return;
692 req->req.actual = readl(&regs->in_dma_current);
693 } else {
694 if (unlikely(master & MST_WR_ENA))
695 return;
697 /* hardware merges short packets, and also hides packet
698 * overruns. a partial packet MAY be in the fifo here.
700 req->req.actual = readl(&regs->out_dma_current);
702 req->req.actual -= req->req.dma;
703 req->req.actual++;
705 #ifdef USB_TRACE
706 VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
707 ep->ep.name, ep->is_in ? "IN" : "OUT",
708 req->req.actual, req->req.length, req);
709 #endif
710 done(ep, req, 0);
711 if (list_empty(&ep->queue))
712 goto stop;
713 req = list_entry(ep->queue.next, struct goku_request, queue);
714 (void) start_dma(ep, req);
717 static void abort_dma(struct goku_ep *ep, int status)
719 struct goku_udc_regs *regs = ep->dev->regs;
720 struct goku_request *req;
721 u32 curr, master;
723 /* NAK future host requests, hoping the implicit delay lets the
724 * dma engine finish reading (or writing) its latest packet and
725 * empty the dma buffer (up to 16 bytes).
727 * This avoids needing to clean up a partial packet in the fifo;
728 * we can't do that for IN without side effects to HALT and TOGGLE.
730 command(regs, COMMAND_FIFO_DISABLE, ep->num);
731 req = list_entry(ep->queue.next, struct goku_request, queue);
732 master = readl(&regs->dma_master) & MST_RW_BITS;
734 /* FIXME using these resets isn't usably documented. this may
735 * not work unless it's followed by disabling the endpoint.
737 * FIXME the OUT reset path doesn't even behave consistently.
739 if (ep->is_in) {
740 if (unlikely((readl(&regs->dma_master) & MST_RD_ENA) == 0))
741 goto finished;
742 curr = readl(&regs->in_dma_current);
744 writel(curr, &regs->in_dma_end);
745 writel(curr, &regs->in_dma_start);
747 master &= ~MST_R_BITS;
748 master |= MST_RD_RESET;
749 writel(master, &regs->dma_master);
751 if (readl(&regs->dma_master) & MST_RD_ENA)
752 DBG(ep->dev, "IN dma active after reset!\n");
754 } else {
755 if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
756 goto finished;
757 curr = readl(&regs->out_dma_current);
759 writel(curr, &regs->out_dma_end);
760 writel(curr, &regs->out_dma_start);
762 master &= ~MST_W_BITS;
763 master |= MST_WR_RESET;
764 writel(master, &regs->dma_master);
766 if (readl(&regs->dma_master) & MST_WR_ENA)
767 DBG(ep->dev, "OUT dma active after reset!\n");
769 req->req.actual = (curr - req->req.dma) + 1;
770 req->req.status = status;
772 VDBG(ep->dev, "%s %s %s %d/%d\n", __FUNCTION__, ep->ep.name,
773 ep->is_in ? "IN" : "OUT",
774 req->req.actual, req->req.length);
776 command(regs, COMMAND_FIFO_ENABLE, ep->num);
778 return;
780 finished:
781 /* dma already completed; no abort needed */
782 command(regs, COMMAND_FIFO_ENABLE, ep->num);
783 req->req.actual = req->req.length;
784 req->req.status = 0;
787 /*-------------------------------------------------------------------------*/
789 static int
790 goku_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
792 struct goku_request *req;
793 struct goku_ep *ep;
794 struct goku_udc *dev;
795 unsigned long flags;
796 int status;
798 /* always require a cpu-view buffer so pio works */
799 req = container_of(_req, struct goku_request, req);
800 if (unlikely(!_req || !_req->complete
801 || !_req->buf || !list_empty(&req->queue)))
802 return -EINVAL;
803 ep = container_of(_ep, struct goku_ep, ep);
804 if (unlikely(!_ep || (!ep->desc && ep->num != 0)))
805 return -EINVAL;
806 dev = ep->dev;
807 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
808 return -ESHUTDOWN;
810 /* can't touch registers when suspended */
811 if (dev->ep0state == EP0_SUSPEND)
812 return -EBUSY;
814 /* set up dma mapping in case the caller didn't */
815 if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
816 _req->dma = pci_map_single(dev->pdev, _req->buf, _req->length,
817 ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
818 req->mapped = 1;
821 #ifdef USB_TRACE
822 VDBG(dev, "%s queue req %p, len %u buf %p\n",
823 _ep->name, _req, _req->length, _req->buf);
824 #endif
826 spin_lock_irqsave(&dev->lock, flags);
828 _req->status = -EINPROGRESS;
829 _req->actual = 0;
831 /* for ep0 IN without premature status, zlp is required and
832 * writing EOP starts the status stage (OUT).
834 if (unlikely(ep->num == 0 && ep->is_in))
835 _req->zero = 1;
837 /* kickstart this i/o queue? */
838 status = 0;
839 if (list_empty(&ep->queue) && likely(!ep->stopped)) {
840 /* dma: done after dma completion IRQ (or error)
841 * pio: done after last fifo operation
843 if (ep->dma)
844 status = start_dma(ep, req);
845 else
846 status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
848 if (unlikely(status != 0)) {
849 if (status > 0)
850 status = 0;
851 req = 0;
854 } /* else pio or dma irq handler advances the queue. */
856 if (likely(req != 0))
857 list_add_tail(&req->queue, &ep->queue);
859 if (likely(!list_empty(&ep->queue))
860 && likely(ep->num != 0)
861 && !ep->dma
862 && !(dev->int_enable & INT_EPxDATASET (ep->num)))
863 pio_irq_enable(dev, dev->regs, ep->num);
865 spin_unlock_irqrestore(&dev->lock, flags);
867 /* pci writes may still be posted */
868 return status;
871 /* dequeue ALL requests */
872 static void nuke(struct goku_ep *ep, int status)
874 struct goku_request *req;
876 ep->stopped = 1;
877 if (list_empty(&ep->queue))
878 return;
879 if (ep->dma)
880 abort_dma(ep, status);
881 while (!list_empty(&ep->queue)) {
882 req = list_entry(ep->queue.next, struct goku_request, queue);
883 done(ep, req, status);
887 /* dequeue JUST ONE request */
888 static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
890 struct goku_request *req;
891 struct goku_ep *ep;
892 struct goku_udc *dev;
893 unsigned long flags;
895 ep = container_of(_ep, struct goku_ep, ep);
896 if (!_ep || !_req || (!ep->desc && ep->num != 0))
897 return -EINVAL;
898 dev = ep->dev;
899 if (!dev->driver)
900 return -ESHUTDOWN;
902 /* we can't touch (dma) registers when suspended */
903 if (dev->ep0state == EP0_SUSPEND)
904 return -EBUSY;
906 VDBG(dev, "%s %s %s %s %p\n", __FUNCTION__, _ep->name,
907 ep->is_in ? "IN" : "OUT",
908 ep->dma ? "dma" : "pio",
909 _req);
911 spin_lock_irqsave(&dev->lock, flags);
913 /* make sure it's actually queued on this endpoint */
914 list_for_each_entry (req, &ep->queue, queue) {
915 if (&req->req == _req)
916 break;
918 if (&req->req != _req) {
919 spin_unlock_irqrestore (&dev->lock, flags);
920 return -EINVAL;
923 if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
924 abort_dma(ep, -ECONNRESET);
925 done(ep, req, -ECONNRESET);
926 dma_advance(dev, ep);
927 } else if (!list_empty(&req->queue))
928 done(ep, req, -ECONNRESET);
929 else
930 req = 0;
931 spin_unlock_irqrestore(&dev->lock, flags);
933 return req ? 0 : -EOPNOTSUPP;
936 /*-------------------------------------------------------------------------*/
938 static void goku_clear_halt(struct goku_ep *ep)
940 // assert (ep->num !=0)
941 VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
942 command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
943 command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
944 if (ep->stopped) {
945 ep->stopped = 0;
946 if (ep->dma) {
947 struct goku_request *req;
949 if (list_empty(&ep->queue))
950 return;
951 req = list_entry(ep->queue.next, struct goku_request,
952 queue);
953 (void) start_dma(ep, req);
954 } else
955 pio_advance(ep);
959 static int goku_set_halt(struct usb_ep *_ep, int value)
961 struct goku_ep *ep;
962 unsigned long flags;
963 int retval = 0;
965 if (!_ep)
966 return -ENODEV;
967 ep = container_of (_ep, struct goku_ep, ep);
969 if (ep->num == 0) {
970 if (value) {
971 ep->dev->ep0state = EP0_STALL;
972 ep->dev->ep[0].stopped = 1;
973 } else
974 return -EINVAL;
976 /* don't change EPxSTATUS_EP_INVALID to READY */
977 } else if (!ep->desc) {
978 DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
979 return -EINVAL;
982 spin_lock_irqsave(&ep->dev->lock, flags);
983 if (!list_empty(&ep->queue))
984 retval = -EAGAIN;
985 else if (ep->is_in && value
986 /* data in (either) packet buffer? */
987 && (ep->dev->regs->DataSet & DATASET_AB(ep->num)))
988 retval = -EAGAIN;
989 else if (!value)
990 goku_clear_halt(ep);
991 else {
992 ep->stopped = 1;
993 VDBG(ep->dev, "%s set halt\n", ep->ep.name);
994 command(ep->dev->regs, COMMAND_STALL, ep->num);
995 readl(ep->reg_status);
997 spin_unlock_irqrestore(&ep->dev->lock, flags);
998 return retval;
1001 static int goku_fifo_status(struct usb_ep *_ep)
1003 struct goku_ep *ep;
1004 struct goku_udc_regs *regs;
1005 u32 size;
1007 if (!_ep)
1008 return -ENODEV;
1009 ep = container_of(_ep, struct goku_ep, ep);
1011 /* size is only reported sanely for OUT */
1012 if (ep->is_in)
1013 return -EOPNOTSUPP;
1015 /* ignores 16-byte dma buffer; SizeH == 0 */
1016 regs = ep->dev->regs;
1017 size = readl(&regs->EPxSizeLA[ep->num]) & DATASIZE;
1018 size += readl(&regs->EPxSizeLB[ep->num]) & DATASIZE;
1019 VDBG(ep->dev, "%s %s %u\n", __FUNCTION__, ep->ep.name, size);
1020 return size;
1023 static void goku_fifo_flush(struct usb_ep *_ep)
1025 struct goku_ep *ep;
1026 struct goku_udc_regs *regs;
1027 u32 size;
1029 if (!_ep)
1030 return;
1031 ep = container_of(_ep, struct goku_ep, ep);
1032 VDBG(ep->dev, "%s %s\n", __FUNCTION__, ep->ep.name);
1034 /* don't change EPxSTATUS_EP_INVALID to READY */
1035 if (!ep->desc && ep->num != 0) {
1036 DBG(ep->dev, "%s %s inactive?\n", __FUNCTION__, ep->ep.name);
1037 return;
1040 regs = ep->dev->regs;
1041 size = readl(&regs->EPxSizeLA[ep->num]);
1042 size &= DATASIZE;
1044 /* Non-desirable behavior: FIFO_CLEAR also clears the
1045 * endpoint halt feature. For OUT, we _could_ just read
1046 * the bytes out (PIO, if !ep->dma); for in, no choice.
1048 if (size)
1049 command(regs, COMMAND_FIFO_CLEAR, ep->num);
1052 static struct usb_ep_ops goku_ep_ops = {
1053 .enable = goku_ep_enable,
1054 .disable = goku_ep_disable,
1056 .alloc_request = goku_alloc_request,
1057 .free_request = goku_free_request,
1059 .alloc_buffer = goku_alloc_buffer,
1060 .free_buffer = goku_free_buffer,
1062 .queue = goku_queue,
1063 .dequeue = goku_dequeue,
1065 .set_halt = goku_set_halt,
1066 .fifo_status = goku_fifo_status,
1067 .fifo_flush = goku_fifo_flush,
1070 /*-------------------------------------------------------------------------*/
1072 static int goku_get_frame(struct usb_gadget *_gadget)
1074 return -EOPNOTSUPP;
1077 static const struct usb_gadget_ops goku_ops = {
1078 .get_frame = goku_get_frame,
1079 // no remote wakeup
1080 // not selfpowered
1083 /*-------------------------------------------------------------------------*/
1085 static inline char *dmastr(void)
1087 if (use_dma == 0)
1088 return "(dma disabled)";
1089 else if (use_dma == 2)
1090 return "(dma IN and OUT)";
1091 else
1092 return "(dma IN)";
1095 /* if we're trying to save space, don't bother with this proc file */
1097 #if defined(CONFIG_PROC_FS) && !defined(CONFIG_EMBEDDED)
1098 # define UDC_PROC_FILE
1099 #endif
1101 #ifdef UDC_PROC_FILE
1103 static const char proc_node_name [] = "driver/udc";
1105 #define FOURBITS "%s%s%s%s"
1106 #define EIGHTBITS FOURBITS FOURBITS
1108 static void
1109 dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1111 int t;
1113 /* int_status is the same format ... */
1114 t = scnprintf(*next, *size,
1115 "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1116 label, mask,
1117 (mask & INT_PWRDETECT) ? " power" : "",
1118 (mask & INT_SYSERROR) ? " sys" : "",
1119 (mask & INT_MSTRDEND) ? " in-dma" : "",
1120 (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1122 (mask & INT_MSTWREND) ? " out-dma" : "",
1123 (mask & INT_MSTWRSET) ? " wrset" : "",
1124 (mask & INT_ERR) ? " err" : "",
1125 (mask & INT_SOF) ? " sof" : "",
1127 (mask & INT_EP3NAK) ? " ep3nak" : "",
1128 (mask & INT_EP2NAK) ? " ep2nak" : "",
1129 (mask & INT_EP1NAK) ? " ep1nak" : "",
1130 (mask & INT_EP3DATASET) ? " ep3" : "",
1132 (mask & INT_EP2DATASET) ? " ep2" : "",
1133 (mask & INT_EP1DATASET) ? " ep1" : "",
1134 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1135 (mask & INT_STATUS) ? " ep0status" : "",
1137 (mask & INT_SETUP) ? " setup" : "",
1138 (mask & INT_ENDPOINT0) ? " ep0" : "",
1139 (mask & INT_USBRESET) ? " reset" : "",
1140 (mask & INT_SUSPEND) ? " suspend" : "");
1141 *size -= t;
1142 *next += t;
1146 static int
1147 udc_proc_read(char *buffer, char **start, off_t off, int count,
1148 int *eof, void *_dev)
1150 char *buf = buffer;
1151 struct goku_udc *dev = _dev;
1152 struct goku_udc_regs *regs = dev->regs;
1153 char *next = buf;
1154 unsigned size = count;
1155 unsigned long flags;
1156 int i, t, is_usb_connected;
1157 u32 tmp;
1159 if (off != 0)
1160 return 0;
1162 local_irq_save(flags);
1164 /* basic device status */
1165 tmp = readl(&regs->power_detect);
1166 is_usb_connected = tmp & PW_DETECT;
1167 t = scnprintf(next, size,
1168 "%s - %s\n"
1169 "%s version: %s %s\n"
1170 "Gadget driver: %s\n"
1171 "Host %s, %s\n"
1172 "\n",
1173 pci_name(dev->pdev), driver_desc,
1174 driver_name, DRIVER_VERSION, dmastr(),
1175 dev->driver ? dev->driver->driver.name : "(none)",
1176 is_usb_connected
1177 ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1178 : "disconnected",
1179 ({char *tmp;
1180 switch(dev->ep0state){
1181 case EP0_DISCONNECT: tmp = "ep0_disconnect"; break;
1182 case EP0_IDLE: tmp = "ep0_idle"; break;
1183 case EP0_IN: tmp = "ep0_in"; break;
1184 case EP0_OUT: tmp = "ep0_out"; break;
1185 case EP0_STATUS: tmp = "ep0_status"; break;
1186 case EP0_STALL: tmp = "ep0_stall"; break;
1187 case EP0_SUSPEND: tmp = "ep0_suspend"; break;
1188 default: tmp = "ep0_?"; break;
1189 } tmp; })
1191 size -= t;
1192 next += t;
1194 dump_intmask("int_status", readl(&regs->int_status), &next, &size);
1195 dump_intmask("int_enable", readl(&regs->int_enable), &next, &size);
1197 if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1198 goto done;
1200 /* registers for (active) device and ep0 */
1201 t = scnprintf(next, size, "\nirqs %lu\ndataset %02x "
1202 "single.bcs %02x.%02x state %x addr %u\n",
1203 dev->irqs, readl(&regs->DataSet),
1204 readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1205 readl(&regs->UsbState),
1206 readl(&regs->address));
1207 size -= t;
1208 next += t;
1210 tmp = readl(&regs->dma_master);
1211 t = scnprintf(next, size,
1212 "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1213 (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1214 (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1215 (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1216 (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1218 (tmp & MST_RD_EOPB) ? " eopb" : "",
1219 (tmp & MST_RD_RESET) ? " in_reset" : "",
1220 (tmp & MST_WR_RESET) ? " out_reset" : "",
1221 (tmp & MST_RD_ENA) ? " IN" : "",
1223 (tmp & MST_WR_ENA) ? " OUT" : "",
1224 (tmp & MST_CONNECTION)
1225 ? "ep1in/ep2out"
1226 : "ep1out/ep2in");
1227 size -= t;
1228 next += t;
1230 /* dump endpoint queues */
1231 for (i = 0; i < 4; i++) {
1232 struct goku_ep *ep = &dev->ep [i];
1233 struct goku_request *req;
1234 int t;
1236 if (i && !ep->desc)
1237 continue;
1239 tmp = readl(ep->reg_status);
1240 t = scnprintf(next, size,
1241 "%s %s max %u %s, irqs %lu, "
1242 "status %02x (%s) " FOURBITS "\n",
1243 ep->ep.name,
1244 ep->is_in ? "in" : "out",
1245 ep->ep.maxpacket,
1246 ep->dma ? "dma" : "pio",
1247 ep->irqs,
1248 tmp, ({ char *s;
1249 switch (tmp & EPxSTATUS_EP_MASK) {
1250 case EPxSTATUS_EP_READY:
1251 s = "ready"; break;
1252 case EPxSTATUS_EP_DATAIN:
1253 s = "packet"; break;
1254 case EPxSTATUS_EP_FULL:
1255 s = "full"; break;
1256 case EPxSTATUS_EP_TX_ERR: // host will retry
1257 s = "tx_err"; break;
1258 case EPxSTATUS_EP_RX_ERR:
1259 s = "rx_err"; break;
1260 case EPxSTATUS_EP_BUSY: /* ep0 only */
1261 s = "busy"; break;
1262 case EPxSTATUS_EP_STALL:
1263 s = "stall"; break;
1264 case EPxSTATUS_EP_INVALID: // these "can't happen"
1265 s = "invalid"; break;
1266 default:
1267 s = "?"; break;
1268 }; s; }),
1269 (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1270 (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1271 (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1272 (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1274 if (t <= 0 || t > size)
1275 goto done;
1276 size -= t;
1277 next += t;
1279 if (list_empty(&ep->queue)) {
1280 t = scnprintf(next, size, "\t(nothing queued)\n");
1281 if (t <= 0 || t > size)
1282 goto done;
1283 size -= t;
1284 next += t;
1285 continue;
1287 list_for_each_entry(req, &ep->queue, queue) {
1288 if (ep->dma && req->queue.prev == &ep->queue) {
1289 if (i == UDC_MSTRD_ENDPOINT)
1290 tmp = readl(&regs->in_dma_current);
1291 else
1292 tmp = readl(&regs->out_dma_current);
1293 tmp -= req->req.dma;
1294 tmp++;
1295 } else
1296 tmp = req->req.actual;
1298 t = scnprintf(next, size,
1299 "\treq %p len %u/%u buf %p\n",
1300 &req->req, tmp, req->req.length,
1301 req->req.buf);
1302 if (t <= 0 || t > size)
1303 goto done;
1304 size -= t;
1305 next += t;
1309 done:
1310 local_irq_restore(flags);
1311 *eof = 1;
1312 return count - size;
1315 #endif /* UDC_PROC_FILE */
1317 /*-------------------------------------------------------------------------*/
1319 static void udc_reinit (struct goku_udc *dev)
1321 static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1323 unsigned i;
1325 INIT_LIST_HEAD (&dev->gadget.ep_list);
1326 dev->gadget.ep0 = &dev->ep [0].ep;
1327 dev->gadget.speed = USB_SPEED_UNKNOWN;
1328 dev->ep0state = EP0_DISCONNECT;
1329 dev->irqs = 0;
1331 for (i = 0; i < 4; i++) {
1332 struct goku_ep *ep = &dev->ep[i];
1334 ep->num = i;
1335 ep->ep.name = names[i];
1336 ep->reg_fifo = &dev->regs->ep_fifo [i];
1337 ep->reg_status = &dev->regs->ep_status [i];
1338 ep->reg_mode = &dev->regs->ep_mode[i];
1340 ep->ep.ops = &goku_ep_ops;
1341 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1342 ep->dev = dev;
1343 INIT_LIST_HEAD (&ep->queue);
1345 ep_reset(0, ep);
1348 dev->ep[0].reg_mode = 0;
1349 dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1350 list_del_init (&dev->ep[0].ep.ep_list);
1353 static void udc_reset(struct goku_udc *dev)
1355 struct goku_udc_regs *regs = dev->regs;
1357 writel(0, &regs->power_detect);
1358 writel(0, &regs->int_enable);
1359 readl(&regs->int_enable);
1360 dev->int_enable = 0;
1362 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1363 * don't let INT_PWRDETECT sequence begin
1365 udelay(250);
1366 writel(PW_RESETB, &regs->power_detect);
1367 readl(&regs->int_enable);
1370 static void ep0_start(struct goku_udc *dev)
1372 struct goku_udc_regs *regs = dev->regs;
1373 unsigned i;
1375 VDBG(dev, "%s\n", __FUNCTION__);
1377 udc_reset(dev);
1378 udc_reinit (dev);
1379 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
1381 /* hw handles set_address, set_feature, get_status; maybe more */
1382 writel( G_REQMODE_SET_INTF | G_REQMODE_GET_INTF
1383 | G_REQMODE_SET_CONF | G_REQMODE_GET_CONF
1384 | G_REQMODE_GET_DESC
1385 | G_REQMODE_CLEAR_FEAT
1386 , &regs->reqmode);
1388 for (i = 0; i < 4; i++)
1389 dev->ep[i].irqs = 0;
1391 /* can't modify descriptors after writing UsbReady */
1392 for (i = 0; i < DESC_LEN; i++)
1393 writel(0, &regs->descriptors[i]);
1394 writel(0, &regs->UsbReady);
1396 /* expect ep0 requests when the host drops reset */
1397 writel(PW_RESETB | PW_PULLUP, &regs->power_detect);
1398 dev->int_enable = INT_DEVWIDE | INT_EP0;
1399 writel(dev->int_enable, &dev->regs->int_enable);
1400 readl(&regs->int_enable);
1401 dev->gadget.speed = USB_SPEED_FULL;
1402 dev->ep0state = EP0_IDLE;
1405 static void udc_enable(struct goku_udc *dev)
1407 /* start enumeration now, or after power detect irq */
1408 if (readl(&dev->regs->power_detect) & PW_DETECT)
1409 ep0_start(dev);
1410 else {
1411 DBG(dev, "%s\n", __FUNCTION__);
1412 dev->int_enable = INT_PWRDETECT;
1413 writel(dev->int_enable, &dev->regs->int_enable);
1417 /*-------------------------------------------------------------------------*/
1419 /* keeping it simple:
1420 * - one bus driver, initted first;
1421 * - one function driver, initted second
1424 static struct goku_udc *the_controller;
1426 /* when a driver is successfully registered, it will receive
1427 * control requests including set_configuration(), which enables
1428 * non-control requests. then usb traffic follows until a
1429 * disconnect is reported. then a host may connect again, or
1430 * the driver might get unbound.
1432 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1434 struct goku_udc *dev = the_controller;
1435 int retval;
1437 if (!driver
1438 || driver->speed != USB_SPEED_FULL
1439 || !driver->bind
1440 || !driver->unbind
1441 || !driver->disconnect
1442 || !driver->setup)
1443 return -EINVAL;
1444 if (!dev)
1445 return -ENODEV;
1446 if (dev->driver)
1447 return -EBUSY;
1449 /* hook up the driver */
1450 driver->driver.bus = 0;
1451 dev->driver = driver;
1452 dev->gadget.dev.driver = &driver->driver;
1453 retval = driver->bind(&dev->gadget);
1454 if (retval) {
1455 DBG(dev, "bind to driver %s --> error %d\n",
1456 driver->driver.name, retval);
1457 dev->driver = 0;
1458 dev->gadget.dev.driver = 0;
1459 return retval;
1462 /* then enable host detection and ep0; and we're ready
1463 * for set_configuration as well as eventual disconnect.
1465 udc_enable(dev);
1467 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1468 return 0;
1470 EXPORT_SYMBOL(usb_gadget_register_driver);
1472 static void
1473 stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1475 unsigned i;
1477 DBG (dev, "%s\n", __FUNCTION__);
1479 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1480 driver = 0;
1482 /* disconnect gadget driver after quiesceing hw and the driver */
1483 udc_reset (dev);
1484 for (i = 0; i < 4; i++)
1485 nuke(&dev->ep [i], -ESHUTDOWN);
1486 if (driver) {
1487 spin_unlock(&dev->lock);
1488 driver->disconnect(&dev->gadget);
1489 spin_lock(&dev->lock);
1492 if (dev->driver)
1493 udc_enable(dev);
1496 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1498 struct goku_udc *dev = the_controller;
1499 unsigned long flags;
1501 if (!dev)
1502 return -ENODEV;
1503 if (!driver || driver != dev->driver)
1504 return -EINVAL;
1506 spin_lock_irqsave(&dev->lock, flags);
1507 dev->driver = 0;
1508 stop_activity(dev, driver);
1509 spin_unlock_irqrestore(&dev->lock, flags);
1511 driver->unbind(&dev->gadget);
1513 DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1514 return 0;
1516 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1519 /*-------------------------------------------------------------------------*/
1521 static void ep0_setup(struct goku_udc *dev)
1523 struct goku_udc_regs *regs = dev->regs;
1524 struct usb_ctrlrequest ctrl;
1525 int tmp;
1527 /* read SETUP packet and enter DATA stage */
1528 ctrl.bRequestType = readl(&regs->bRequestType);
1529 ctrl.bRequest = readl(&regs->bRequest);
1530 ctrl.wValue = (readl(&regs->wValueH) << 8) | readl(&regs->wValueL);
1531 ctrl.wIndex = (readl(&regs->wIndexH) << 8) | readl(&regs->wIndexL);
1532 ctrl.wLength = (readl(&regs->wLengthH) << 8) | readl(&regs->wLengthL);
1533 writel(0, &regs->SetupRecv);
1535 nuke(&dev->ep[0], 0);
1536 dev->ep[0].stopped = 0;
1537 if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1538 dev->ep[0].is_in = 1;
1539 dev->ep0state = EP0_IN;
1540 /* detect early status stages */
1541 writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1542 } else {
1543 dev->ep[0].is_in = 0;
1544 dev->ep0state = EP0_OUT;
1546 /* NOTE: CLEAR_FEATURE is done in software so that we can
1547 * synchronize transfer restarts after bulk IN stalls. data
1548 * won't even enter the fifo until the halt is cleared.
1550 switch (ctrl.bRequest) {
1551 case USB_REQ_CLEAR_FEATURE:
1552 switch (ctrl.bRequestType) {
1553 case USB_RECIP_ENDPOINT:
1554 tmp = ctrl.wIndex & 0x0f;
1555 /* active endpoint */
1556 if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
1557 goto stall;
1558 if (ctrl.wIndex & 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 != USB_ENDPOINT_HALT)
1566 goto stall;
1567 if (tmp)
1568 goku_clear_halt(&dev->ep[tmp]);
1569 succeed:
1570 /* start ep0out status stage */
1571 writel(~(1<<0), &regs->EOP);
1572 dev->ep[0].stopped = 1;
1573 dev->ep0state = EP0_STATUS;
1574 return;
1575 case USB_RECIP_DEVICE:
1576 /* device remote wakeup: always clear */
1577 if (ctrl.wValue != 1)
1578 goto stall;
1579 VDBG(dev, "clear dev remote wakeup\n");
1580 goto succeed;
1581 case USB_RECIP_INTERFACE:
1582 goto stall;
1583 default: /* pass to gadget driver */
1584 break;
1586 break;
1587 default:
1588 break;
1592 #ifdef USB_TRACE
1593 VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1594 ctrl.bRequestType, ctrl.bRequest,
1595 ctrl.wValue, ctrl.wIndex, ctrl.wLength);
1596 #endif
1598 /* hw wants to know when we're configured (or not) */
1599 dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1600 && ctrl.bRequestType == USB_RECIP_DEVICE);
1601 if (unlikely(dev->req_config))
1602 dev->configured = (ctrl.wValue != 0);
1604 /* delegate everything to the gadget driver.
1605 * it may respond after this irq handler returns.
1607 spin_unlock (&dev->lock);
1608 tmp = dev->driver->setup(&dev->gadget, &ctrl);
1609 spin_lock (&dev->lock);
1610 if (unlikely(tmp < 0)) {
1611 stall:
1612 #ifdef USB_TRACE
1613 VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1614 ctrl.bRequestType, ctrl.bRequest, tmp);
1615 #endif
1616 command(regs, COMMAND_STALL, 0);
1617 dev->ep[0].stopped = 1;
1618 dev->ep0state = EP0_STALL;
1621 /* expect at least one data or status stage irq */
1624 #define ACK(irqbit) { \
1625 stat &= ~irqbit; \
1626 writel(~irqbit, &regs->int_status); \
1627 handled = 1; \
1630 static irqreturn_t goku_irq(int irq, void *_dev, struct pt_regs *r)
1632 struct goku_udc *dev = _dev;
1633 struct goku_udc_regs *regs = dev->regs;
1634 struct goku_ep *ep;
1635 u32 stat, handled = 0;
1636 unsigned i, rescans = 5;
1638 spin_lock(&dev->lock);
1640 rescan:
1641 stat = readl(&regs->int_status) & dev->int_enable;
1642 if (!stat)
1643 goto done;
1644 dev->irqs++;
1646 /* device-wide irqs */
1647 if (unlikely(stat & INT_DEVWIDE)) {
1648 if (stat & INT_SYSERROR) {
1649 ERROR(dev, "system error\n");
1650 stop_activity(dev, dev->driver);
1651 stat = 0;
1652 handled = 1;
1653 // FIXME have a neater way to prevent re-enumeration
1654 dev->driver = 0;
1655 goto done;
1657 if (stat & INT_PWRDETECT) {
1658 writel(~stat, &regs->int_status);
1659 if (readl(&dev->regs->power_detect) & PW_DETECT) {
1660 VDBG(dev, "connect\n");
1661 ep0_start(dev);
1662 } else {
1663 DBG(dev, "disconnect\n");
1664 if (dev->gadget.speed == USB_SPEED_FULL)
1665 stop_activity(dev, dev->driver);
1666 dev->ep0state = EP0_DISCONNECT;
1667 dev->int_enable = INT_DEVWIDE;
1668 writel(dev->int_enable, &dev->regs->int_enable);
1670 stat = 0;
1671 handled = 1;
1672 goto done;
1674 if (stat & INT_SUSPEND) {
1675 ACK(INT_SUSPEND);
1676 if (readl(&regs->ep_status[0]) & EPxSTATUS_SUSPEND) {
1677 switch (dev->ep0state) {
1678 case EP0_DISCONNECT:
1679 case EP0_SUSPEND:
1680 goto pm_next;
1681 default:
1682 break;
1684 DBG(dev, "USB suspend\n");
1685 dev->ep0state = EP0_SUSPEND;
1686 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1687 && dev->driver
1688 && dev->driver->suspend) {
1689 spin_unlock(&dev->lock);
1690 dev->driver->suspend(&dev->gadget);
1691 spin_lock(&dev->lock);
1693 } else {
1694 if (dev->ep0state != EP0_SUSPEND) {
1695 DBG(dev, "bogus USB resume %d\n",
1696 dev->ep0state);
1697 goto pm_next;
1699 DBG(dev, "USB resume\n");
1700 dev->ep0state = EP0_IDLE;
1701 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1702 && dev->driver
1703 && dev->driver->resume) {
1704 spin_unlock(&dev->lock);
1705 dev->driver->resume(&dev->gadget);
1706 spin_lock(&dev->lock);
1710 pm_next:
1711 if (stat & INT_USBRESET) { /* hub reset done */
1712 ACK(INT_USBRESET);
1713 INFO(dev, "USB reset done, gadget %s\n",
1714 dev->driver->driver.name);
1716 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1719 /* progress ep0 setup, data, or status stages.
1720 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1722 if (stat & INT_SETUP) {
1723 ACK(INT_SETUP);
1724 dev->ep[0].irqs++;
1725 ep0_setup(dev);
1727 if (stat & INT_STATUSNAK) {
1728 ACK(INT_STATUSNAK|INT_ENDPOINT0);
1729 if (dev->ep0state == EP0_IN) {
1730 ep = &dev->ep[0];
1731 ep->irqs++;
1732 nuke(ep, 0);
1733 writel(~(1<<0), &regs->EOP);
1734 dev->ep0state = EP0_STATUS;
1737 if (stat & INT_ENDPOINT0) {
1738 ACK(INT_ENDPOINT0);
1739 ep = &dev->ep[0];
1740 ep->irqs++;
1741 pio_advance(ep);
1744 /* dma completion */
1745 if (stat & INT_MSTRDEND) { /* IN */
1746 ACK(INT_MSTRDEND);
1747 ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1748 ep->irqs++;
1749 dma_advance(dev, ep);
1751 if (stat & INT_MSTWREND) { /* OUT */
1752 ACK(INT_MSTWREND);
1753 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1754 ep->irqs++;
1755 dma_advance(dev, ep);
1757 if (stat & INT_MSTWRTMOUT) { /* OUT */
1758 ACK(INT_MSTWRTMOUT);
1759 ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1760 ep->irqs++;
1761 ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1762 // reset dma? then dma_advance()
1765 /* pio */
1766 for (i = 1; i < 4; i++) {
1767 u32 tmp = INT_EPxDATASET(i);
1769 if (!(stat & tmp))
1770 continue;
1771 ep = &dev->ep[i];
1772 pio_advance(ep);
1773 if (list_empty (&ep->queue))
1774 pio_irq_disable(dev, regs, i);
1775 stat &= ~tmp;
1776 handled = 1;
1777 ep->irqs++;
1780 if (rescans--)
1781 goto rescan;
1783 done:
1784 (void)readl(&regs->int_enable);
1785 spin_unlock(&dev->lock);
1786 if (stat)
1787 DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1788 readl(&regs->int_status), dev->int_enable);
1789 return IRQ_RETVAL(handled);
1792 #undef ACK
1794 /*-------------------------------------------------------------------------*/
1796 static void gadget_release(struct device *_dev)
1798 struct goku_udc *dev = dev_get_drvdata(_dev);
1800 kfree(dev);
1803 /* tear down the binding between this driver and the pci device */
1805 static void goku_remove(struct pci_dev *pdev)
1807 struct goku_udc *dev = pci_get_drvdata(pdev);
1809 DBG(dev, "%s\n", __FUNCTION__);
1810 /* start with the driver above us */
1811 if (dev->driver) {
1812 /* should have been done already by driver model core */
1813 WARN(dev, "pci remove, driver '%s' is still registered\n",
1814 dev->driver->driver.name);
1815 usb_gadget_unregister_driver(dev->driver);
1818 #ifdef UDC_PROC_FILE
1819 remove_proc_entry(proc_node_name, NULL);
1820 #endif
1821 if (dev->regs)
1822 udc_reset(dev);
1823 if (dev->got_irq)
1824 free_irq(pdev->irq, dev);
1825 if (dev->regs)
1826 iounmap(dev->regs);
1827 if (dev->got_region)
1828 release_mem_region(pci_resource_start (pdev, 0),
1829 pci_resource_len (pdev, 0));
1830 if (dev->enabled)
1831 pci_disable_device(pdev);
1832 device_unregister(&dev->gadget.dev);
1834 pci_set_drvdata(pdev, 0);
1835 dev->regs = 0;
1836 the_controller = 0;
1838 INFO(dev, "unbind\n");
1841 /* wrap this driver around the specified pci device, but
1842 * don't respond over USB until a gadget driver binds to us.
1845 static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1847 struct goku_udc *dev = 0;
1848 unsigned long resource, len;
1849 void *base = 0;
1850 int retval;
1851 char buf [8], *bufp;
1853 /* if you want to support more than one controller in a system,
1854 * usb_gadget_driver_{register,unregister}() must change.
1856 if (the_controller) {
1857 WARN(dev, "ignoring %s\n", pci_name(pdev));
1858 return -EBUSY;
1860 if (!pdev->irq) {
1861 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1862 retval = -ENODEV;
1863 goto done;
1866 /* alloc, and start init */
1867 dev = kmalloc (sizeof *dev, SLAB_KERNEL);
1868 if (dev == NULL){
1869 pr_debug("enomem %s\n", pci_name(pdev));
1870 retval = -ENOMEM;
1871 goto done;
1874 memset(dev, 0, sizeof *dev);
1875 spin_lock_init(&dev->lock);
1876 dev->pdev = pdev;
1877 dev->gadget.ops = &goku_ops;
1879 /* the "gadget" abstracts/virtualizes the controller */
1880 strcpy(dev->gadget.dev.bus_id, "gadget");
1881 dev->gadget.dev.parent = &pdev->dev;
1882 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1883 dev->gadget.dev.release = gadget_release;
1884 dev->gadget.name = driver_name;
1886 /* now all the pci goodies ... */
1887 retval = pci_enable_device(pdev);
1888 if (retval < 0) {
1889 DBG(dev, "can't enable, %d\n", retval);
1890 goto done;
1892 dev->enabled = 1;
1894 resource = pci_resource_start(pdev, 0);
1895 len = pci_resource_len(pdev, 0);
1896 if (!request_mem_region(resource, len, driver_name)) {
1897 DBG(dev, "controller already in use\n");
1898 retval = -EBUSY;
1899 goto done;
1901 dev->got_region = 1;
1903 base = ioremap_nocache(resource, len);
1904 if (base == NULL) {
1905 DBG(dev, "can't map memory\n");
1906 retval = -EFAULT;
1907 goto done;
1909 dev->regs = (struct goku_udc_regs *) base;
1911 pci_set_drvdata(pdev, dev);
1912 INFO(dev, "%s\n", driver_desc);
1913 INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1914 #ifndef __sparc__
1915 scnprintf(buf, sizeof buf, "%d", pdev->irq);
1916 bufp = buf;
1917 #else
1918 bufp = __irq_itoa(pdev->irq);
1919 #endif
1920 INFO(dev, "irq %s, pci mem %p\n", bufp, base);
1922 /* init to known state, then setup irqs */
1923 udc_reset(dev);
1924 udc_reinit (dev);
1925 if (request_irq(pdev->irq, goku_irq, SA_SHIRQ/*|SA_SAMPLE_RANDOM*/,
1926 driver_name, dev) != 0) {
1927 DBG(dev, "request interrupt %s failed\n", bufp);
1928 retval = -EBUSY;
1929 goto done;
1931 dev->got_irq = 1;
1932 if (use_dma)
1933 pci_set_master(pdev);
1936 #ifdef UDC_PROC_FILE
1937 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1938 #endif
1940 /* done */
1941 the_controller = dev;
1942 device_register(&dev->gadget.dev);
1944 return 0;
1946 done:
1947 if (dev)
1948 goku_remove (pdev);
1949 return retval;
1953 /*-------------------------------------------------------------------------*/
1955 static struct pci_device_id pci_ids [] = { {
1956 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1957 .class_mask = ~0,
1958 .vendor = 0x102f, /* Toshiba */
1959 .device = 0x0107, /* this UDC */
1960 .subvendor = PCI_ANY_ID,
1961 .subdevice = PCI_ANY_ID,
1963 }, { /* end: all zeroes */ }
1965 MODULE_DEVICE_TABLE (pci, pci_ids);
1967 static struct pci_driver goku_pci_driver = {
1968 .name = (char *) driver_name,
1969 .id_table = pci_ids,
1971 .probe = goku_probe,
1972 .remove = goku_remove,
1974 /* FIXME add power management support */
1977 static int __init init (void)
1979 return pci_module_init (&goku_pci_driver);
1981 module_init (init);
1983 static void __exit cleanup (void)
1985 pci_unregister_driver (&goku_pci_driver);
1987 module_exit (cleanup);