blob: 66b81bbf6bee2d695f4ffb637dc489e737be0d17 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
3 *
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)
8 *
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.
12 */
13
14/*
15 * This device has ep0 and three semi-configurable bulk/interrupt endpoints.
16 *
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).
21 */
22
23#undef DEBUG
24// #define VERBOSE /* extra debug messages (success too) */
25// #define USB_TRACE /* packet-level success messages */
26
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>
45
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>
51
52
53#include "goku_udc.h"
54
55#define DRIVER_DESC "TC86C001 USB Device Controller"
56#define DRIVER_VERSION "30-Oct 2003"
57
58#define DMA_ADDR_INVALID (~(dma_addr_t)0)
59
60static const char driver_name [] = "goku_udc";
61static const char driver_desc [] = DRIVER_DESC;
62
63MODULE_AUTHOR("source@mvista.com");
64MODULE_DESCRIPTION(DRIVER_DESC);
65MODULE_LICENSE("GPL");
66
67
68/*
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.
71 *
72 * OUT dma documents design problems handling the common "short packet"
David Brownell988199f2005-05-07 13:05:52 -070073 * transfer termination policy; it couldn't be enabled by default, even
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * if the OUT-dma abort problems had a resolution.
75 */
76static unsigned use_dma = 1;
77
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
84 */
85module_param(use_dma, uint, S_IRUGO);
86#endif
87
88/*-------------------------------------------------------------------------*/
89
90static void nuke(struct goku_ep *, int status);
91
92static inline void
93command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
94{
95 writel(COMMAND_EP(epnum) | command, &regs->Command);
96 udelay(300);
97}
98
99static int
100goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
101{
102 struct goku_udc *dev;
103 struct goku_ep *ep;
104 u32 mode;
105 u16 max;
106 unsigned long flags;
107
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;
119
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;
126 }
127
128 if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
129 != EPxSTATUS_EP_INVALID)
130 return -EBUSY;
131
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;
143 }
144 mode |= 2 << 1; /* bulk, or intr-with-toggle */
145
146 /* ep1/ep2 dma direction is chosen early; it works in the other
147 * direction, with pio. be cautious with out-dma.
148 */
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);
158 }
159
160 spin_lock_irqsave(&ep->dev->lock, flags);
161
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;
166
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);
174
175 tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
176 tmp |= readl(&regs->EPxBCS);
177 writel(tmp, &regs->EPxBCS);
178 }
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);
185
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);
190
191 return 0;
192}
193
194static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
195{
196 struct goku_udc *dev = ep->dev;
197
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;
214
215 tmp = readl(&r->EPxSingle);
216 tmp &= ~(0x11 << ep->num);
217 writel(tmp, &r->EPxSingle);
218
219 tmp = readl(&r->EPxBCS);
220 tmp &= ~(0x11 << ep->num);
221 writel(tmp, &r->EPxBCS);
222 }
223 /* reset dma in case we're still using it */
224 if (ep->dma) {
225 u32 master;
226
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;
234 }
235 writel(master, &regs->dma_master);
236 }
237 }
238
239 ep->ep.maxpacket = MAX_FIFO_SIZE;
240 ep->desc = NULL;
241 ep->stopped = 1;
242 ep->irqs = 0;
243 ep->dma = 0;
244}
245
246static int goku_ep_disable(struct usb_ep *_ep)
247{
248 struct goku_ep *ep;
249 struct goku_udc *dev;
250 unsigned long flags;
251
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;
258
259 VDBG(dev, "disable %s\n", _ep->name);
260
261 spin_lock_irqsave(&dev->lock, flags);
262 nuke(ep, -ESHUTDOWN);
263 ep_reset(dev->regs, ep);
264 spin_unlock_irqrestore(&dev->lock, flags);
265
266 return 0;
267}
268
269/*-------------------------------------------------------------------------*/
270
271static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400272goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct goku_request *req;
275
276 if (!_ep)
277 return NULL;
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800278 req = kzalloc(sizeof *req, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!req)
280 return NULL;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 req->req.dma = DMA_ADDR_INVALID;
283 INIT_LIST_HEAD(&req->queue);
284 return &req->req;
285}
286
287static void
288goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
289{
290 struct goku_request *req;
291
292 if (!_ep || !_req)
293 return;
294
295 req = container_of(_req, struct goku_request, req);
296 WARN_ON(!list_empty(&req->queue));
297 kfree(req);
298}
299
300/*-------------------------------------------------------------------------*/
301
302#undef USE_KMALLOC
303
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.)
308 *
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.
311 */
312#if defined(CONFIG_X86)
313#define USE_KMALLOC
314
David Brownell988199f2005-05-07 13:05:52 -0700315#elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#define USE_KMALLOC
317
318#elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
319#define USE_KMALLOC
320
321#endif
322
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.
326 */
327static void *
328goku_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
Al Viro55016f12005-10-21 03:21:58 -0400329 dma_addr_t *dma, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 void *retval;
332 struct goku_ep *ep;
333
334 ep = container_of(_ep, struct goku_ep, ep);
335 if (!_ep)
336 return NULL;
337 *dma = DMA_ADDR_INVALID;
338
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.
347 */
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;
355}
356
357static void
358goku_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma, unsigned bytes)
359{
360 /* free memory into the right allocator */
361#ifndef USE_KMALLOC
362 if (dma != DMA_ADDR_INVALID) {
363 struct goku_ep *ep;
364
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);
372}
373
374/*-------------------------------------------------------------------------*/
375
376static void
377done(struct goku_ep *ep, struct goku_request *req, int status)
378{
379 struct goku_udc *dev;
380 unsigned stopped = ep->stopped;
381
382 list_del_init(&req->queue);
383
384 if (likely(req->req.status == -EINPROGRESS))
385 req->req.status = status;
386 else
387 status = req->req.status;
388
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;
395 }
396
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);
403
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;
410}
411
412/*-------------------------------------------------------------------------*/
413
414static inline int
415write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
416{
417 unsigned length, count;
418
419 length = min(req->req.length - req->req.actual, max);
420 req->req.actual += length;
421
422 count = length;
423 while (likely(count--))
424 writel(*buf++, fifo);
425 return length;
426}
427
428// return: 0 = still running, 1 = completed, negative = errno
429static int write_fifo(struct goku_ep *ep, struct goku_request *req)
430{
431 struct goku_udc *dev = ep->dev;
432 u32 tmp;
433 u8 *buf;
434 unsigned count;
435 int is_last;
436
437 tmp = readl(&dev->regs->DataSet);
438 buf = req->req.buf + req->req.actual;
439 prefetch(buf);
440
441 dev = ep->dev;
442 if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
443 return -EL2HLT;
444
445 /* NOTE: just single-buffered PIO-IN for now. */
446 if (unlikely((tmp & DATASET_A(ep->num)) != 0))
447 return 0;
448
449 /* clear our "packet available" irq */
450 if (ep->num != 0)
451 writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
452
453 count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
454
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;
461 }
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;
469 }
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
476
477 /* requests complete when all IN data is in the FIFO,
478 * or sometimes later, if a zlp was needed.
479 */
480 if (is_last) {
481 done(ep, req, 0);
482 return 1;
483 }
484
485 return 0;
486}
487
488static int read_fifo(struct goku_ep *ep, struct goku_request *req)
489{
490 struct goku_udc_regs __iomem *regs;
491 u32 size, set;
492 u8 *buf;
493 unsigned bufferspace, is_short, dbuff;
494
495 regs = ep->dev->regs;
496top:
497 buf = req->req.buf + req->req.actual;
498 prefetchw(buf);
499
500 if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
501 return -EL2HLT;
502
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);
508
509 set = readl(&regs->DataSet) & DATASET_AB(ep->num);
510 size = readl(&regs->EPxSizeLA[ep->num]);
511 bufferspace = req->req.length - req->req.actual;
512
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 */
523
524 /* ep0out no-out-data case for set_config, etc */
525 } else
526 size = 0;
527
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);
538
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.
543 */
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--;
551 }
552 }
553
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;
567 }
568 done(ep, req, 0);
569
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;
575 }
576 return 1;
577 }
578 } while (dbuff);
579 return 0;
580}
581
582static inline void
583pio_irq_enable(struct goku_udc *dev,
584 struct goku_udc_regs __iomem *regs, int epnum)
585{
586 dev->int_enable |= INT_EPxDATASET (epnum);
587 writel(dev->int_enable, &regs->int_enable);
588 /* write may still be posted */
589}
590
591static inline void
592pio_irq_disable(struct goku_udc *dev,
593 struct goku_udc_regs __iomem *regs, int epnum)
594{
595 dev->int_enable &= ~INT_EPxDATASET (epnum);
596 writel(dev->int_enable, &regs->int_enable);
597 /* write may still be posted */
598}
599
600static inline void
601pio_advance(struct goku_ep *ep)
602{
603 struct goku_request *req;
604
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);
609}
610
611
612/*-------------------------------------------------------------------------*/
613
614// return: 0 = q running, 1 = q stopped, negative = errno
615static int start_dma(struct goku_ep *ep, struct goku_request *req)
616{
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;
621
622 master = readl(&regs->dma_master) & MST_RW_BITS;
623
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;
630 }
631 writel(end, &regs->in_dma_end);
632 writel(start, &regs->in_dma_start);
633
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;
642
643 ep->dev->int_enable |= INT_MSTRDEND;
644
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.
649 */
650 } else {
651 if (unlikely(master & MST_WR_ENA)) {
652 DBG (ep->dev, "start, OUT active dma %03x!!\n",
653 master);
654// return -EL2HLT;
655 }
656 writel(end, &regs->out_dma_end);
657 writel(start, &regs->out_dma_start);
658
659 master &= ~MST_W_BITS;
660 master |= MST_WR_ENA | MST_TIMEOUT_DIS;
661
662 ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
663 }
664
665 writel(master, &regs->dma_master);
666 writel(ep->dev->int_enable, &regs->int_enable);
667 return 0;
668}
669
670static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
671{
672 struct goku_request *req;
673 struct goku_udc_regs __iomem *regs = ep->dev->regs;
674 u32 master;
675
676 master = readl(&regs->dma_master);
677
678 if (unlikely(list_empty(&ep->queue))) {
679stop:
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;
686 }
687 req = list_entry(ep->queue.next, struct goku_request, queue);
688
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;
697
698 /* hardware merges short packets, and also hides packet
699 * overruns. a partial packet MAY be in the fifo here.
700 */
701 req->req.actual = readl(&regs->out_dma_current);
702 }
703 req->req.actual -= req->req.dma;
704 req->req.actual++;
705
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);
716}
717
718static void abort_dma(struct goku_ep *ep, int status)
719{
720 struct goku_udc_regs __iomem *regs = ep->dev->regs;
721 struct goku_request *req;
722 u32 curr, master;
723
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).
727 *
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.
730 */
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;
734
735 /* FIXME using these resets isn't usably documented. this may
736 * not work unless it's followed by disabling the endpoint.
737 *
738 * FIXME the OUT reset path doesn't even behave consistently.
739 */
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);
744
745 writel(curr, &regs->in_dma_end);
746 writel(curr, &regs->in_dma_start);
747
748 master &= ~MST_R_BITS;
749 master |= MST_RD_RESET;
750 writel(master, &regs->dma_master);
751
752 if (readl(&regs->dma_master) & MST_RD_ENA)
753 DBG(ep->dev, "IN dma active after reset!\n");
754
755 } else {
756 if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
757 goto finished;
758 curr = readl(&regs->out_dma_current);
759
760 writel(curr, &regs->out_dma_end);
761 writel(curr, &regs->out_dma_start);
762
763 master &= ~MST_W_BITS;
764 master |= MST_WR_RESET;
765 writel(master, &regs->dma_master);
766
767 if (readl(&regs->dma_master) & MST_WR_ENA)
768 DBG(ep->dev, "OUT dma active after reset!\n");
769 }
770 req->req.actual = (curr - req->req.dma) + 1;
771 req->req.status = status;
772
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);
776
777 command(regs, COMMAND_FIFO_ENABLE, ep->num);
778
779 return;
780
781finished:
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;
786}
787
788/*-------------------------------------------------------------------------*/
789
790static int
Al Viro55016f12005-10-21 03:21:58 -0400791goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct goku_request *req;
794 struct goku_ep *ep;
795 struct goku_udc *dev;
796 unsigned long flags;
797 int status;
798
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;
810
811 /* can't touch registers when suspended */
812 if (dev->ep0state == EP0_SUSPEND)
813 return -EBUSY;
814
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;
820 }
821
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
826
827 spin_lock_irqsave(&dev->lock, flags);
828
829 _req->status = -EINPROGRESS;
830 _req->actual = 0;
831
832 /* for ep0 IN without premature status, zlp is required and
833 * writing EOP starts the status stage (OUT).
834 */
835 if (unlikely(ep->num == 0 && ep->is_in))
836 _req->zero = 1;
837
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
843 */
844 if (ep->dma)
845 status = start_dma(ep, req);
846 else
847 status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
848
849 if (unlikely(status != 0)) {
850 if (status > 0)
851 status = 0;
852 req = NULL;
853 }
854
855 } /* else pio or dma irq handler advances the queue. */
856
857 if (likely(req != 0))
858 list_add_tail(&req->queue, &ep->queue);
859
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);
865
866 spin_unlock_irqrestore(&dev->lock, flags);
867
868 /* pci writes may still be posted */
869 return status;
870}
871
872/* dequeue ALL requests */
873static void nuke(struct goku_ep *ep, int status)
874{
875 struct goku_request *req;
876
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);
885 }
886}
887
888/* dequeue JUST ONE request */
889static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
890{
891 struct goku_request *req;
892 struct goku_ep *ep;
893 struct goku_udc *dev;
894 unsigned long flags;
895
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;
902
903 /* we can't touch (dma) registers when suspended */
904 if (dev->ep0state == EP0_SUSPEND)
905 return -EBUSY;
906
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);
911
912 spin_lock_irqsave(&dev->lock, flags);
913
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;
918 }
919 if (&req->req != _req) {
920 spin_unlock_irqrestore (&dev->lock, flags);
921 return -EINVAL;
922 }
923
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);
933
934 return req ? 0 : -EOPNOTSUPP;
935}
936
937/*-------------------------------------------------------------------------*/
938
939static void goku_clear_halt(struct goku_ep *ep)
940{
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;
949
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);
957 }
958}
959
960static int goku_set_halt(struct usb_ep *_ep, int value)
961{
962 struct goku_ep *ep;
963 unsigned long flags;
964 int retval = 0;
965
966 if (!_ep)
967 return -ENODEV;
968 ep = container_of (_ep, struct goku_ep, ep);
969
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;
976
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;
981 }
982
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);
998 }
999 spin_unlock_irqrestore(&ep->dev->lock, flags);
1000 return retval;
1001}
1002
1003static int goku_fifo_status(struct usb_ep *_ep)
1004{
1005 struct goku_ep *ep;
1006 struct goku_udc_regs __iomem *regs;
1007 u32 size;
1008
1009 if (!_ep)
1010 return -ENODEV;
1011 ep = container_of(_ep, struct goku_ep, ep);
1012
1013 /* size is only reported sanely for OUT */
1014 if (ep->is_in)
1015 return -EOPNOTSUPP;
1016
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;
1023}
1024
1025static void goku_fifo_flush(struct usb_ep *_ep)
1026{
1027 struct goku_ep *ep;
1028 struct goku_udc_regs __iomem *regs;
1029 u32 size;
1030
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);
1035
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;
1040 }
1041
1042 regs = ep->dev->regs;
1043 size = readl(&regs->EPxSizeLA[ep->num]);
1044 size &= DATASIZE;
1045
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.
1049 */
1050 if (size)
1051 command(regs, COMMAND_FIFO_CLEAR, ep->num);
1052}
1053
1054static struct usb_ep_ops goku_ep_ops = {
1055 .enable = goku_ep_enable,
1056 .disable = goku_ep_disable,
1057
1058 .alloc_request = goku_alloc_request,
1059 .free_request = goku_free_request,
1060
1061 .alloc_buffer = goku_alloc_buffer,
1062 .free_buffer = goku_free_buffer,
1063
1064 .queue = goku_queue,
1065 .dequeue = goku_dequeue,
1066
1067 .set_halt = goku_set_halt,
1068 .fifo_status = goku_fifo_status,
1069 .fifo_flush = goku_fifo_flush,
1070};
1071
1072/*-------------------------------------------------------------------------*/
1073
1074static int goku_get_frame(struct usb_gadget *_gadget)
1075{
1076 return -EOPNOTSUPP;
1077}
1078
1079static const struct usb_gadget_ops goku_ops = {
1080 .get_frame = goku_get_frame,
1081 // no remote wakeup
1082 // not selfpowered
1083};
1084
1085/*-------------------------------------------------------------------------*/
1086
1087static inline char *dmastr(void)
1088{
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)";
1095}
1096
1097#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1098
1099static const char proc_node_name [] = "driver/udc";
1100
1101#define FOURBITS "%s%s%s%s"
1102#define EIGHTBITS FOURBITS FOURBITS
1103
1104static void
1105dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1106{
1107 int t;
1108
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" : "",
1117
1118 (mask & INT_MSTWREND) ? " out-dma" : "",
1119 (mask & INT_MSTWRSET) ? " wrset" : "",
1120 (mask & INT_ERR) ? " err" : "",
1121 (mask & INT_SOF) ? " sof" : "",
1122
1123 (mask & INT_EP3NAK) ? " ep3nak" : "",
1124 (mask & INT_EP2NAK) ? " ep2nak" : "",
1125 (mask & INT_EP1NAK) ? " ep1nak" : "",
1126 (mask & INT_EP3DATASET) ? " ep3" : "",
1127
1128 (mask & INT_EP2DATASET) ? " ep2" : "",
1129 (mask & INT_EP1DATASET) ? " ep1" : "",
1130 (mask & INT_STATUSNAK) ? " ep0snak" : "",
1131 (mask & INT_STATUS) ? " ep0status" : "",
1132
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;
1139}
1140
1141
1142static int
1143udc_proc_read(char *buffer, char **start, off_t off, int count,
1144 int *eof, void *_dev)
1145{
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;
1154
1155 if (off != 0)
1156 return 0;
1157
1158 local_irq_save(flags);
1159
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; })
1186 );
1187 size -= t;
1188 next += t;
1189
1190 dump_intmask("int_status", readl(&regs->int_status), &next, &size);
1191 dump_intmask("int_enable", readl(&regs->int_enable), &next, &size);
1192
1193 if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1194 goto done;
1195
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;
1205
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+" : "",
1213
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" : "",
1218
1219 (tmp & MST_WR_ENA) ? " OUT" : "",
1220 (tmp & MST_CONNECTION)
1221 ? "ep1in/ep2out"
1222 : "ep1out/ep2in");
1223 size -= t;
1224 next += t;
1225
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;
1231
1232 if (i && !ep->desc)
1233 continue;
1234
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" : ""
1269 );
1270 if (t <= 0 || t > size)
1271 goto done;
1272 size -= t;
1273 next += t;
1274
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;
1282 }
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;
1293
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;
1302 }
1303 }
1304
1305done:
1306 local_irq_restore(flags);
1307 *eof = 1;
1308 return count - size;
1309}
1310
1311#endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1312
1313/*-------------------------------------------------------------------------*/
1314
1315static void udc_reinit (struct goku_udc *dev)
1316{
1317 static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1318
1319 unsigned i;
1320
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;
1326
1327 for (i = 0; i < 4; i++) {
1328 struct goku_ep *ep = &dev->ep[i];
1329
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];
1335
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);
1340
1341 ep_reset(NULL, ep);
1342 }
1343
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);
1347}
1348
1349static void udc_reset(struct goku_udc *dev)
1350{
1351 struct goku_udc_regs __iomem *regs = dev->regs;
1352
1353 writel(0, &regs->power_detect);
1354 writel(0, &regs->int_enable);
1355 readl(&regs->int_enable);
1356 dev->int_enable = 0;
1357
1358 /* deassert reset, leave USB D+ at hi-Z (no pullup)
1359 * don't let INT_PWRDETECT sequence begin
1360 */
1361 udelay(250);
1362 writel(PW_RESETB, &regs->power_detect);
1363 readl(&regs->int_enable);
1364}
1365
1366static void ep0_start(struct goku_udc *dev)
1367{
1368 struct goku_udc_regs __iomem *regs = dev->regs;
1369 unsigned i;
1370
1371 VDBG(dev, "%s\n", __FUNCTION__);
1372
1373 udc_reset(dev);
1374 udc_reinit (dev);
1375 //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
1376
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);
1383
1384 for (i = 0; i < 4; i++)
1385 dev->ep[i].irqs = 0;
1386
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);
1391
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;
1399}
1400
1401static void udc_enable(struct goku_udc *dev)
1402{
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);
1410 }
1411}
1412
1413/*-------------------------------------------------------------------------*/
1414
1415/* keeping it simple:
1416 * - one bus driver, initted first;
1417 * - one function driver, initted second
1418 */
1419
1420static struct goku_udc *the_controller;
1421
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.
1427 */
1428int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1429{
1430 struct goku_udc *dev = the_controller;
1431 int retval;
1432
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;
1444
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;
1456 }
1457
1458 /* then enable host detection and ep0; and we're ready
1459 * for set_configuration as well as eventual disconnect.
1460 */
1461 udc_enable(dev);
1462
1463 DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1464 return 0;
1465}
1466EXPORT_SYMBOL(usb_gadget_register_driver);
1467
1468static void
1469stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1470{
1471 unsigned i;
1472
1473 DBG (dev, "%s\n", __FUNCTION__);
1474
1475 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1476 driver = NULL;
1477
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);
1486 }
1487
1488 if (dev->driver)
1489 udc_enable(dev);
1490}
1491
1492int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1493{
1494 struct goku_udc *dev = the_controller;
1495 unsigned long flags;
1496
1497 if (!dev)
1498 return -ENODEV;
1499 if (!driver || driver != dev->driver)
1500 return -EINVAL;
1501
1502 spin_lock_irqsave(&dev->lock, flags);
1503 dev->driver = NULL;
1504 stop_activity(dev, driver);
1505 spin_unlock_irqrestore(&dev->lock, flags);
1506
1507 driver->unbind(&dev->gadget);
1508
1509 DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1510 return 0;
1511}
1512EXPORT_SYMBOL(usb_gadget_unregister_driver);
1513
1514
1515/*-------------------------------------------------------------------------*/
1516
1517static void ep0_setup(struct goku_udc *dev)
1518{
1519 struct goku_udc_regs __iomem *regs = dev->regs;
1520 struct usb_ctrlrequest ctrl;
1521 int tmp;
1522
1523 /* read SETUP packet and enter DATA stage */
1524 ctrl.bRequestType = readl(&regs->bRequestType);
1525 ctrl.bRequest = readl(&regs->bRequest);
David Brownell988199f2005-05-07 13:05:52 -07001526 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 writel(0, &regs->SetupRecv);
1533
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;
1544
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.
1548 */
1549 switch (ctrl.bRequest) {
1550 case USB_REQ_CLEAR_FEATURE:
1551 switch (ctrl.bRequestType) {
1552 case USB_RECIP_ENDPOINT:
David Brownell988199f2005-05-07 13:05:52 -07001553 tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* active endpoint */
1555 if (tmp > 3 || (!dev->ep[tmp].desc && tmp != 0))
1556 goto stall;
David Brownell988199f2005-05-07 13:05:52 -07001557 if (ctrl.wIndex & __constant_cpu_to_le16(
1558 USB_DIR_IN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!dev->ep[tmp].is_in)
1560 goto stall;
1561 } else {
1562 if (dev->ep[tmp].is_in)
1563 goto stall;
1564 }
David Brownell988199f2005-05-07 13:05:52 -07001565 if (ctrl.wValue != __constant_cpu_to_le16(
1566 USB_ENDPOINT_HALT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 goto stall;
1568 if (tmp)
1569 goku_clear_halt(&dev->ep[tmp]);
1570succeed:
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 */
David Brownell988199f2005-05-07 13:05:52 -07001578 if (ctrl.wValue != __constant_cpu_to_le16(1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 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;
1586 }
1587 break;
1588 default:
1589 break;
1590 }
1591 }
1592
1593#ifdef USB_TRACE
1594 VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1595 ctrl.bRequestType, ctrl.bRequest,
David Brownell988199f2005-05-07 13:05:52 -07001596 le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1597 le16_to_cpu(ctrl.wLength));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598#endif
1599
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))
David Brownell988199f2005-05-07 13:05:52 -07001604 dev->configured = (ctrl.wValue != __constant_cpu_to_le16(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 /* delegate everything to the gadget driver.
1607 * it may respond after this irq handler returns.
1608 */
1609 spin_unlock (&dev->lock);
1610 tmp = dev->driver->setup(&dev->gadget, &ctrl);
1611 spin_lock (&dev->lock);
1612 if (unlikely(tmp < 0)) {
1613stall:
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;
1621 }
1622
1623 /* expect at least one data or status stage irq */
1624}
1625
1626#define ACK(irqbit) { \
1627 stat &= ~irqbit; \
1628 writel(~irqbit, &regs->int_status); \
1629 handled = 1; \
1630 }
1631
1632static irqreturn_t goku_irq(int irq, void *_dev, struct pt_regs *r)
1633{
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;
1639
1640 spin_lock(&dev->lock);
1641
1642rescan:
1643 stat = readl(&regs->int_status) & dev->int_enable;
1644 if (!stat)
1645 goto done;
1646 dev->irqs++;
1647
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;
1658 }
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);
1671 }
1672 stat = 0;
1673 handled = 1;
1674 goto done;
1675 }
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;
1685 }
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);
1694 }
1695 } else {
1696 if (dev->ep0state != EP0_SUSPEND) {
1697 DBG(dev, "bogus USB resume %d\n",
1698 dev->ep0state);
1699 goto pm_next;
1700 }
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);
1709 }
1710 }
1711 }
1712pm_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);
1717 }
1718 // and INT_ERR on some endpoint's crc/bitstuff/... problem
1719 }
1720
1721 /* progress ep0 setup, data, or status stages.
1722 * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1723 */
1724 if (stat & INT_SETUP) {
1725 ACK(INT_SETUP);
1726 dev->ep[0].irqs++;
1727 ep0_setup(dev);
1728 }
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;
1737 }
1738 }
1739 if (stat & INT_ENDPOINT0) {
1740 ACK(INT_ENDPOINT0);
1741 ep = &dev->ep[0];
1742 ep->irqs++;
1743 pio_advance(ep);
1744 }
1745
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);
1752 }
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);
1758 }
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()
1765 }
1766
1767 /* pio */
1768 for (i = 1; i < 4; i++) {
1769 u32 tmp = INT_EPxDATASET(i);
1770
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++;
1780 }
1781
1782 if (rescans--)
1783 goto rescan;
1784
1785done:
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);
1792}
1793
1794#undef ACK
1795
1796/*-------------------------------------------------------------------------*/
1797
1798static void gadget_release(struct device *_dev)
1799{
1800 struct goku_udc *dev = dev_get_drvdata(_dev);
1801
1802 kfree(dev);
1803}
1804
1805/* tear down the binding between this driver and the pci device */
1806
1807static void goku_remove(struct pci_dev *pdev)
1808{
1809 struct goku_udc *dev = pci_get_drvdata(pdev);
1810
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);
1818 }
1819
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);
1835
1836 pci_set_drvdata(pdev, NULL);
1837 dev->regs = NULL;
1838 the_controller = NULL;
1839
1840 INFO(dev, "unbind\n");
1841}
1842
1843/* wrap this driver around the specified pci device, but
1844 * don't respond over USB until a gadget driver binds to us.
1845 */
1846
1847static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1848{
1849 struct goku_udc *dev = NULL;
1850 unsigned long resource, len;
1851 void __iomem *base = NULL;
1852 int retval;
1853 char buf [8], *bufp;
1854
1855 /* if you want to support more than one controller in a system,
1856 * usb_gadget_driver_{register,unregister}() must change.
1857 */
1858 if (the_controller) {
1859 WARN(dev, "ignoring %s\n", pci_name(pdev));
1860 return -EBUSY;
1861 }
1862 if (!pdev->irq) {
1863 printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1864 retval = -ENODEV;
1865 goto done;
1866 }
1867
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;
1874 }
1875
1876 memset(dev, 0, sizeof *dev);
1877 spin_lock_init(&dev->lock);
1878 dev->pdev = pdev;
1879 dev->gadget.ops = &goku_ops;
1880
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;
1887
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;
1893 }
1894 dev->enabled = 1;
1895
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;
1902 }
1903 dev->got_region = 1;
1904
1905 base = ioremap_nocache(resource, len);
1906 if (base == NULL) {
1907 DBG(dev, "can't map memory\n");
1908 retval = -EFAULT;
1909 goto done;
1910 }
1911 dev->regs = (struct goku_udc_regs __iomem *) base;
1912
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);
1923
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;
1932 }
1933 dev->got_irq = 1;
1934 if (use_dma)
1935 pci_set_master(pdev);
1936
1937
1938#ifdef CONFIG_USB_GADGET_DEBUG_FILES
1939 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1940#endif
1941
1942 /* done */
1943 the_controller = dev;
1944 device_register(&dev->gadget.dev);
1945
1946 return 0;
1947
1948done:
1949 if (dev)
1950 goku_remove (pdev);
1951 return retval;
1952}
1953
1954
1955/*-------------------------------------------------------------------------*/
1956
1957static 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,
1964
1965}, { /* end: all zeroes */ }
1966};
1967MODULE_DEVICE_TABLE (pci, pci_ids);
1968
1969static struct pci_driver goku_pci_driver = {
1970 .name = (char *) driver_name,
1971 .id_table = pci_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 .probe = goku_probe,
1974 .remove = goku_remove,
1975
1976 /* FIXME add power management support */
1977};
1978
1979static int __init init (void)
1980{
1981 return pci_register_driver (&goku_pci_driver);
1982}
1983module_init (init);
1984
1985static void __exit cleanup (void)
1986{
1987 pci_unregister_driver (&goku_pci_driver);
1988}
1989module_exit (cleanup);