vserver 1.9.5.x5
[linux-2.6.git] / drivers / usb / gadget / net2280.c
1 /*
2  * Driver for the NetChip 2280 USB device controller.
3  * Specs and errata are available from <http://www.netchip.com>.
4  *
5  * NetChip Technology Inc. supported the development of this driver.
6  *
7  *
8  * CODE STATUS HIGHLIGHTS
9  *
10  * This driver should work well with most "gadget" drivers, including
11  * the File Storage, Serial, and Ethernet/RNDIS gadget drivers
12  * as well as Gadget Zero and Gadgetfs.
13  *
14  * DMA is enabled by default.  Drivers using transfer queues might use
15  * DMA chaining to remove IRQ latencies between transfers.  (Except when
16  * short OUT transfers happen.)  Drivers can use the req->no_interrupt
17  * hint to completely eliminate some IRQs, if a later IRQ is guaranteed
18  * and DMA chaining is enabled.
19  *
20  * Note that almost all the errata workarounds here are only needed for
21  * rev1 chips.  Rev1a silicon (0110) fixes almost all of them.
22  */
23
24 /*
25  * Copyright (C) 2003 David Brownell
26  * Copyright (C) 2003 NetChip Technologies
27  *
28  * This program is free software; you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation; either version 2 of the License, or
31  * (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
41  */
42
43 #undef  DEBUG           /* messages on error and most fault paths */
44 #undef  VERBOSE         /* extra debug messages (success too) */
45
46 #include <linux/config.h>
47 #include <linux/module.h>
48 #include <linux/pci.h>
49 #include <linux/kernel.h>
50 #include <linux/delay.h>
51 #include <linux/ioport.h>
52 #include <linux/sched.h>
53 #include <linux/slab.h>
54 #include <linux/smp_lock.h>
55 #include <linux/errno.h>
56 #include <linux/init.h>
57 #include <linux/timer.h>
58 #include <linux/list.h>
59 #include <linux/interrupt.h>
60 #include <linux/moduleparam.h>
61 #include <linux/device.h>
62 #include <linux/usb_ch9.h>
63 #include <linux/usb_gadget.h>
64
65 #include <asm/byteorder.h>
66 #include <asm/io.h>
67 #include <asm/irq.h>
68 #include <asm/system.h>
69 #include <asm/unaligned.h>
70
71
72 #define DRIVER_DESC             "NetChip 2280 USB Peripheral Controller"
73 #define DRIVER_VERSION          "2004 Jan 14"
74
75 #define DMA_ADDR_INVALID        (~(dma_addr_t)0)
76 #define EP_DONTUSE              13      /* nonzero */
77
78 #define USE_RDK_LEDS            /* GPIO pins control three LEDs */
79
80
81 static const char driver_name [] = "net2280";
82 static const char driver_desc [] = DRIVER_DESC;
83
84 static const char ep0name [] = "ep0";
85 static const char *ep_name [] = {
86         ep0name,
87         "ep-a", "ep-b", "ep-c", "ep-d",
88         "ep-e", "ep-f",
89 };
90
91 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
92  * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
93  *
94  * The net2280 DMA engines are not tightly integrated with their FIFOs;
95  * not all cases are (yet) handled well in this driver or the silicon.
96  * Some gadget drivers work better with the dma support here than others.
97  * These two parameters let you use PIO or more aggressive DMA.
98  */
99 static int use_dma = 1;
100 static int use_dma_chaining = 0;
101
102 /* "modprobe net2280 use_dma=n" etc */
103 module_param (use_dma, bool, S_IRUGO);
104 module_param (use_dma_chaining, bool, S_IRUGO);
105
106
107 /* mode 0 == ep-{a,b,c,d} 1K fifo each
108  * mode 1 == ep-{a,b} 2K fifo each, ep-{c,d} unavailable
109  * mode 2 == ep-a 2K fifo, ep-{b,c} 1K each, ep-d unavailable
110  */
111 static ushort fifo_mode = 0;
112
113 /* "modprobe net2280 fifo_mode=1" etc */
114 module_param (fifo_mode, ushort, 0644);
115
116
117 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
118
119 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
120 static char *type_string (u8 bmAttributes)
121 {
122         switch ((bmAttributes) & USB_ENDPOINT_XFERTYPE_MASK) {
123         case USB_ENDPOINT_XFER_BULK:    return "bulk";
124         case USB_ENDPOINT_XFER_ISOC:    return "iso";
125         case USB_ENDPOINT_XFER_INT:     return "intr";
126         };
127         return "control";
128 }
129 #endif
130
131 #include "net2280.h"
132
133 #define valid_bit       __constant_cpu_to_le32 (1 << VALID_BIT)
134 #define dma_done_ie     __constant_cpu_to_le32 (1 << DMA_DONE_INTERRUPT_ENABLE)
135
136 /*-------------------------------------------------------------------------*/
137
138 static int
139 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
140 {
141         struct net2280          *dev;
142         struct net2280_ep       *ep;
143         u32                     max, tmp;
144         unsigned long           flags;
145
146         ep = container_of (_ep, struct net2280_ep, ep);
147         if (!_ep || !desc || ep->desc || _ep->name == ep0name
148                         || desc->bDescriptorType != USB_DT_ENDPOINT)
149                 return -EINVAL;
150         dev = ep->dev;
151         if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
152                 return -ESHUTDOWN;
153
154         /* erratum 0119 workaround ties up an endpoint number */
155         if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
156                 return -EDOM;
157
158         /* sanity check ep-e/ep-f since their fifos are small */
159         max = le16_to_cpu (desc->wMaxPacketSize) & 0x1fff;
160         if (ep->num > 4 && max > 64)
161                 return -ERANGE;
162
163         spin_lock_irqsave (&dev->lock, flags);
164         _ep->maxpacket = max & 0x7ff;
165         ep->desc = desc;
166
167         /* ep_reset() has already been called */
168         ep->stopped = 0;
169         ep->out_overflow = 0;
170
171         /* set speed-dependent max packet; may kick in high bandwidth */
172         set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
173
174         /* FIFO lines can't go to different packets.  PIO is ok, so
175          * use it instead of troublesome (non-bulk) multi-packet DMA.
176          */
177         if (ep->dma && (max % 4) != 0 && use_dma_chaining) {
178                 DEBUG (ep->dev, "%s, no dma for maxpacket %d\n",
179                         ep->ep.name, ep->ep.maxpacket);
180                 ep->dma = NULL;
181         }
182
183         /* set type, direction, address; reset fifo counters */
184         writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
185         tmp = (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
186         if (tmp == USB_ENDPOINT_XFER_INT) {
187                 /* erratum 0105 workaround prevents hs NYET */
188                 if (dev->chiprev == 0100
189                                 && dev->gadget.speed == USB_SPEED_HIGH
190                                 && !(desc->bEndpointAddress & USB_DIR_IN))
191                         writel ((1 << CLEAR_NAK_OUT_PACKETS_MODE),
192                                 &ep->regs->ep_rsp);
193         } else if (tmp == USB_ENDPOINT_XFER_BULK) {
194                 /* catch some particularly blatant driver bugs */
195                 if ((dev->gadget.speed == USB_SPEED_HIGH
196                                         && max != 512)
197                                 || (dev->gadget.speed == USB_SPEED_FULL
198                                         && max > 64)) {
199                         spin_unlock_irqrestore (&dev->lock, flags);
200                         return -ERANGE;
201                 }
202         }
203         ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC) ? 1 : 0;
204         tmp <<= ENDPOINT_TYPE;
205         tmp |= desc->bEndpointAddress;
206         tmp |= (4 << ENDPOINT_BYTE_COUNT);      /* default full fifo lines */
207         tmp |= 1 << ENDPOINT_ENABLE;
208         wmb ();
209
210         /* for OUT transfers, block the rx fifo until a read is posted */
211         ep->is_in = (tmp & USB_DIR_IN) != 0;
212         if (!ep->is_in)
213                 writel ((1 << SET_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
214
215         writel (tmp, &ep->regs->ep_cfg);
216
217         /* enable irqs */
218         if (!ep->dma) {                         /* pio, per-packet */
219                 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
220                 writel (tmp, &dev->regs->pciirqenb0);
221
222                 tmp = (1 << DATA_PACKET_RECEIVED_INTERRUPT_ENABLE)
223                         | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT_ENABLE)
224                         | readl (&ep->regs->ep_irqenb);
225                 writel (tmp, &ep->regs->ep_irqenb);
226         } else {                                /* dma, per-request */
227                 tmp = (1 << (8 + ep->num));     /* completion */
228                 tmp |= readl (&dev->regs->pciirqenb1);
229                 writel (tmp, &dev->regs->pciirqenb1);
230
231                 /* for short OUT transfers, dma completions can't
232                  * advance the queue; do it pio-style, by hand.
233                  * NOTE erratum 0112 workaround #2
234                  */
235                 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
236                         tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
237                         writel (tmp, &ep->regs->ep_irqenb);
238
239                         tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
240                         writel (tmp, &dev->regs->pciirqenb0);
241                 }
242         }
243
244         tmp = desc->bEndpointAddress;
245         DEBUG (dev, "enabled %s (ep%d%s-%s) %s max %04x\n",
246                 _ep->name, tmp & 0x0f, DIR_STRING (tmp),
247                 type_string (desc->bmAttributes),
248                 ep->dma ? "dma" : "pio", max);
249
250         /* pci writes may still be posted */
251         spin_unlock_irqrestore (&dev->lock, flags);
252         return 0;
253 }
254
255 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
256 {
257         u32     result;
258
259         do {
260                 result = readl (ptr);
261                 if (result == ~(u32)0)          /* "device unplugged" */
262                         return -ENODEV;
263                 result &= mask;
264                 if (result == done)
265                         return 0;
266                 udelay (1);
267                 usec--;
268         } while (usec > 0);
269         return -ETIMEDOUT;
270 }
271
272 static struct usb_ep_ops net2280_ep_ops;
273
274 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
275 {
276         u32             tmp;
277
278         ep->desc = NULL;
279         INIT_LIST_HEAD (&ep->queue);
280
281         ep->ep.maxpacket = ~0;
282         ep->ep.ops = &net2280_ep_ops;
283
284         /* disable the dma, irqs, endpoint... */
285         if (ep->dma) {
286                 writel (0, &ep->dma->dmactl);
287                 writel (  (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
288                         | (1 << DMA_TRANSACTION_DONE_INTERRUPT)
289                         | (1 << DMA_ABORT)
290                         , &ep->dma->dmastat);
291
292                 tmp = readl (&regs->pciirqenb0);
293                 tmp &= ~(1 << ep->num);
294                 writel (tmp, &regs->pciirqenb0);
295         } else {
296                 tmp = readl (&regs->pciirqenb1);
297                 tmp &= ~(1 << (8 + ep->num));   /* completion */
298                 writel (tmp, &regs->pciirqenb1);
299         }
300         writel (0, &ep->regs->ep_irqenb);
301
302         /* init to our chosen defaults, notably so that we NAK OUT
303          * packets until the driver queues a read (+note erratum 0112)
304          */
305         tmp = (1 << SET_NAK_OUT_PACKETS_MODE)
306                 | (1 << SET_NAK_OUT_PACKETS)
307                 | (1 << CLEAR_EP_HIDE_STATUS_PHASE)
308                 | (1 << CLEAR_INTERRUPT_MODE);
309
310         if (ep->num != 0) {
311                 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
312                         | (1 << CLEAR_ENDPOINT_HALT);
313         }
314         writel (tmp, &ep->regs->ep_rsp);
315
316         /* scrub most status bits, and flush any fifo state */
317         writel (  (1 << TIMEOUT)
318                 | (1 << USB_STALL_SENT)
319                 | (1 << USB_IN_NAK_SENT)
320                 | (1 << USB_IN_ACK_RCVD)
321                 | (1 << USB_OUT_PING_NAK_SENT)
322                 | (1 << USB_OUT_ACK_SENT)
323                 | (1 << FIFO_OVERFLOW)
324                 | (1 << FIFO_UNDERFLOW)
325                 | (1 << FIFO_FLUSH)
326                 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
327                 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
328                 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
329                 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
330                 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
331                 | (1 << DATA_IN_TOKEN_INTERRUPT)
332                 , &ep->regs->ep_stat);
333
334         /* fifo size is handled separately */
335 }
336
337 static void nuke (struct net2280_ep *);
338
339 static int net2280_disable (struct usb_ep *_ep)
340 {
341         struct net2280_ep       *ep;
342         unsigned long           flags;
343
344         ep = container_of (_ep, struct net2280_ep, ep);
345         if (!_ep || !ep->desc || _ep->name == ep0name)
346                 return -EINVAL;
347
348         spin_lock_irqsave (&ep->dev->lock, flags);
349         nuke (ep);
350         ep_reset (ep->dev->regs, ep);
351
352         VDEBUG (ep->dev, "disabled %s %s\n",
353                         ep->dma ? "dma" : "pio", _ep->name);
354
355         /* synch memory views with the device */
356         (void) readl (&ep->regs->ep_cfg);
357
358         if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
359                 ep->dma = &ep->dev->dma [ep->num - 1];
360
361         spin_unlock_irqrestore (&ep->dev->lock, flags);
362         return 0;
363 }
364
365 /*-------------------------------------------------------------------------*/
366
367 static struct usb_request *
368 net2280_alloc_request (struct usb_ep *_ep, int gfp_flags)
369 {
370         struct net2280_ep       *ep;
371         struct net2280_request  *req;
372
373         if (!_ep)
374                 return NULL;
375         ep = container_of (_ep, struct net2280_ep, ep);
376
377         req = kmalloc (sizeof *req, gfp_flags);
378         if (!req)
379                 return NULL;
380
381         memset (req, 0, sizeof *req);
382         req->req.dma = DMA_ADDR_INVALID;
383         INIT_LIST_HEAD (&req->queue);
384
385         /* this dma descriptor may be swapped with the previous dummy */
386         if (ep->dma) {
387                 struct net2280_dma      *td;
388
389                 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
390                                 &req->td_dma);
391                 if (!td) {
392                         kfree (req);
393                         return NULL;
394                 }
395                 td->dmacount = 0;       /* not VALID */
396                 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
397                 td->dmadesc = td->dmaaddr;
398                 req->td = td;
399         }
400         return &req->req;
401 }
402
403 static void
404 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
405 {
406         struct net2280_ep       *ep;
407         struct net2280_request  *req;
408
409         ep = container_of (_ep, struct net2280_ep, ep);
410         if (!_ep || !_req)
411                 return;
412
413         req = container_of (_req, struct net2280_request, req);
414         WARN_ON (!list_empty (&req->queue));
415         if (req->td)
416                 pci_pool_free (ep->dev->requests, req->td, req->td_dma);
417         kfree (req);
418 }
419
420 /*-------------------------------------------------------------------------*/
421
422 #undef USE_KMALLOC
423
424 /* many common platforms have dma-coherent caches, which means that it's
425  * safe to use kmalloc() memory for all i/o buffers without using any
426  * cache flushing calls.  (unless you're trying to share cache lines
427  * between dma and non-dma activities, which is a slow idea in any case.)
428  *
429  * other platforms need more care, with 2.5 having a moderately general
430  * solution (which falls down for allocations smaller than one page)
431  * that improves significantly on the 2.4 PCI allocators by removing
432  * the restriction that memory never be freed in_interrupt().
433  */
434 #if     defined(CONFIG_X86)
435 #define USE_KMALLOC
436
437 #elif   defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
438 #define USE_KMALLOC
439
440 #elif   defined(CONFIG_MIPS) && !defined(CONFIG_NONCOHERENT_IO)
441 #define USE_KMALLOC
442
443 /* FIXME there are other cases, including an x86-64 one ...  */
444 #endif
445
446 /* allocating buffers this way eliminates dma mapping overhead, which
447  * on some platforms will mean eliminating a per-io buffer copy.  with
448  * some kinds of system caches, further tweaks may still be needed.
449  */
450 static void *
451 net2280_alloc_buffer (
452         struct usb_ep           *_ep,
453         unsigned                bytes,
454         dma_addr_t              *dma,
455         int                     gfp_flags
456 )
457 {
458         void                    *retval;
459         struct net2280_ep       *ep;
460
461         ep = container_of (_ep, struct net2280_ep, ep);
462         if (!_ep)
463                 return NULL;
464         *dma = DMA_ADDR_INVALID;
465
466 #if     defined(USE_KMALLOC)
467         retval = kmalloc(bytes, gfp_flags);
468         if (retval)
469                 *dma = virt_to_phys(retval);
470 #else
471         if (ep->dma) {
472                 /* the main problem with this call is that it wastes memory
473                  * on typical 1/N page allocations: it allocates 1-N pages.
474                  */
475 #warning Using dma_alloc_coherent even with buffers smaller than a page.
476                 retval = dma_alloc_coherent(&ep->dev->pdev->dev,
477                                 bytes, dma, gfp_flags);
478         } else
479                 retval = kmalloc(bytes, gfp_flags);
480 #endif
481         return retval;
482 }
483
484 static void
485 net2280_free_buffer (
486         struct usb_ep *_ep,
487         void *buf,
488         dma_addr_t dma,
489         unsigned bytes
490 ) {
491         /* free memory into the right allocator */
492 #ifndef USE_KMALLOC
493         if (dma != DMA_ADDR_INVALID) {
494                 struct net2280_ep       *ep;
495
496                 ep = container_of(_ep, struct net2280_ep, ep);
497                 if (!_ep)
498                         return;
499                 dma_free_coherent(&ep->dev->pdev->dev, bytes, buf, dma);
500         } else
501 #endif
502                 kfree (buf);
503 }
504
505 /*-------------------------------------------------------------------------*/
506
507 /* load a packet into the fifo we use for usb IN transfers.
508  * works for all endpoints.
509  *
510  * NOTE: pio with ep-a..ep-d could stuff multiple packets into the fifo
511  * at a time, but this code is simpler because it knows it only writes
512  * one packet.  ep-a..ep-d should use dma instead.
513  */
514 static void
515 write_fifo (struct net2280_ep *ep, struct usb_request *req)
516 {
517         struct net2280_ep_regs  __iomem *regs = ep->regs;
518         u8                      *buf;
519         u32                     tmp;
520         unsigned                count, total;
521
522         /* INVARIANT:  fifo is currently empty. (testable) */
523
524         if (req) {
525                 buf = req->buf + req->actual;
526                 prefetch (buf);
527                 total = req->length - req->actual;
528         } else {
529                 total = 0;
530                 buf = NULL;
531         }
532
533         /* write just one packet at a time */
534         count = ep->ep.maxpacket;
535         if (count > total)      /* min() cannot be used on a bitfield */
536                 count = total;
537
538         VDEBUG (ep->dev, "write %s fifo (IN) %d bytes%s req %p\n",
539                         ep->ep.name, count,
540                         (count != ep->ep.maxpacket) ? " (short)" : "",
541                         req);
542         while (count >= 4) {
543                 /* NOTE be careful if you try to align these. fifo lines
544                  * should normally be full (4 bytes) and successive partial
545                  * lines are ok only in certain cases.
546                  */
547                 tmp = get_unaligned ((u32 *)buf);
548                 cpu_to_le32s (&tmp);
549                 writel (tmp, &regs->ep_data);
550                 buf += 4;
551                 count -= 4;
552         }
553
554         /* last fifo entry is "short" unless we wrote a full packet.
555          * also explicitly validate last word in (periodic) transfers
556          * when maxpacket is not a multiple of 4 bytes.
557          */
558         if (count || total < ep->ep.maxpacket) {
559                 tmp = count ? get_unaligned ((u32 *)buf) : count;
560                 cpu_to_le32s (&tmp);
561                 set_fifo_bytecount (ep, count & 0x03);
562                 writel (tmp, &regs->ep_data);
563         }
564
565         /* pci writes may still be posted */
566 }
567
568 /* work around erratum 0106: PCI and USB race over the OUT fifo.
569  * caller guarantees chiprev 0100, out endpoint is NAKing, and
570  * there's no real data in the fifo.
571  *
572  * NOTE:  also used in cases where that erratum doesn't apply:
573  * where the host wrote "too much" data to us.
574  */
575 static void out_flush (struct net2280_ep *ep)
576 {
577         u32     __iomem *statp;
578         u32     tmp;
579
580         ASSERT_OUT_NAKING (ep);
581
582         statp = &ep->regs->ep_stat;
583         writel (  (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
584                 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
585                 , statp);
586         writel ((1 << FIFO_FLUSH), statp);
587         mb ();
588         tmp = readl (statp);
589         if (tmp & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
590                         /* high speed did bulk NYET; fifo isn't filling */
591                         && ep->dev->gadget.speed == USB_SPEED_FULL) {
592                 unsigned        usec;
593
594                 usec = 50;              /* 64 byte bulk/interrupt */
595                 handshake (statp, (1 << USB_OUT_PING_NAK_SENT),
596                                 (1 << USB_OUT_PING_NAK_SENT), usec);
597                 /* NAK done; now CLEAR_NAK_OUT_PACKETS is safe */
598         }
599 }
600
601 /* unload packet(s) from the fifo we use for usb OUT transfers.
602  * returns true iff the request completed, because of short packet
603  * or the request buffer having filled with full packets.
604  *
605  * for ep-a..ep-d this will read multiple packets out when they
606  * have been accepted.
607  */
608 static int
609 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
610 {
611         struct net2280_ep_regs  __iomem *regs = ep->regs;
612         u8                      *buf = req->req.buf + req->req.actual;
613         unsigned                count, tmp, is_short;
614         unsigned                cleanup = 0, prevent = 0;
615
616         /* erratum 0106 ... packets coming in during fifo reads might
617          * be incompletely rejected.  not all cases have workarounds.
618          */
619         if (ep->dev->chiprev == 0x0100
620                         && ep->dev->gadget.speed == USB_SPEED_FULL) {
621                 udelay (1);
622                 tmp = readl (&ep->regs->ep_stat);
623                 if ((tmp & (1 << NAK_OUT_PACKETS)))
624                         cleanup = 1;
625                 else if ((tmp & (1 << FIFO_FULL))) {
626                         start_out_naking (ep);
627                         prevent = 1;
628                 }
629                 /* else: hope we don't see the problem */
630         }
631
632         /* never overflow the rx buffer. the fifo reads packets until
633          * it sees a short one; we might not be ready for them all.
634          */
635         prefetchw (buf);
636         count = readl (&regs->ep_avail);
637         if (unlikely (count == 0)) {
638                 udelay (1);
639                 tmp = readl (&ep->regs->ep_stat);
640                 count = readl (&regs->ep_avail);
641                 /* handled that data already? */
642                 if (count == 0 && (tmp & (1 << NAK_OUT_PACKETS)) == 0)
643                         return 0;
644         }
645
646         tmp = req->req.length - req->req.actual;
647         if (count > tmp) {
648                 /* as with DMA, data overflow gets flushed */
649                 if ((tmp % ep->ep.maxpacket) != 0) {
650                         ERROR (ep->dev,
651                                 "%s out fifo %d bytes, expected %d\n",
652                                 ep->ep.name, count, tmp);
653                         req->req.status = -EOVERFLOW;
654                         cleanup = 1;
655                         /* NAK_OUT_PACKETS will be set, so flushing is safe;
656                          * the next read will start with the next packet
657                          */
658                 } /* else it's a ZLP, no worries */
659                 count = tmp;
660         }
661         req->req.actual += count;
662
663         is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
664
665         VDEBUG (ep->dev, "read %s fifo (OUT) %d bytes%s%s%s req %p %d/%d\n",
666                         ep->ep.name, count, is_short ? " (short)" : "",
667                         cleanup ? " flush" : "", prevent ? " nak" : "",
668                         req, req->req.actual, req->req.length);
669
670         while (count >= 4) {
671                 tmp = readl (&regs->ep_data);
672                 cpu_to_le32s (&tmp);
673                 put_unaligned (tmp, (u32 *)buf);
674                 buf += 4;
675                 count -= 4;
676         }
677         if (count) {
678                 tmp = readl (&regs->ep_data);
679                 /* LE conversion is implicit here: */
680                 do {
681                         *buf++ = (u8) tmp;
682                         tmp >>= 8;
683                 } while (--count);
684         }
685         if (cleanup)
686                 out_flush (ep);
687         if (prevent) {
688                 writel ((1 << CLEAR_NAK_OUT_PACKETS), &ep->regs->ep_rsp);
689                 (void) readl (&ep->regs->ep_rsp);
690         }
691
692         return is_short || ((req->req.actual == req->req.length)
693                                 && !req->req.zero);
694 }
695
696 /* fill out dma descriptor to match a given request */
697 static void
698 fill_dma_desc (struct net2280_ep *ep, struct net2280_request *req, int valid)
699 {
700         struct net2280_dma      *td = req->td;
701         u32                     dmacount = req->req.length;
702
703         /* don't let DMA continue after a short OUT packet,
704          * so overruns can't affect the next transfer.
705          * in case of overruns on max-size packets, we can't
706          * stop the fifo from filling but we can flush it.
707          */
708         if (ep->is_in)
709                 dmacount |= (1 << DMA_DIRECTION);
710         else if ((dmacount % ep->ep.maxpacket) != 0)
711                 dmacount |= (1 << END_OF_CHAIN);
712
713         req->valid = valid;
714         if (valid)
715                 dmacount |= (1 << VALID_BIT);
716         if (likely(!req->req.no_interrupt || !use_dma_chaining))
717                 dmacount |= (1 << DMA_DONE_INTERRUPT_ENABLE);
718
719         /* td->dmadesc = previously set by caller */
720         td->dmaaddr = cpu_to_le32 (req->req.dma);
721
722         /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
723         wmb ();
724         td->dmacount = cpu_to_le32p (&dmacount);
725 }
726
727 static const u32 dmactl_default =
728                   (1 << DMA_SCATTER_GATHER_DONE_INTERRUPT)
729                 | (1 << DMA_CLEAR_COUNT_ENABLE)
730                 /* erratum 0116 workaround part 1 (use POLLING) */
731                 | (POLL_100_USEC << DESCRIPTOR_POLLING_RATE)
732                 | (1 << DMA_VALID_BIT_POLLING_ENABLE)
733                 | (1 << DMA_VALID_BIT_ENABLE)
734                 | (1 << DMA_SCATTER_GATHER_ENABLE)
735                 /* erratum 0116 workaround part 2 (no AUTOSTART) */
736                 | (1 << DMA_ENABLE);
737
738 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
739 {
740         handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
741 }
742
743 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
744 {
745         writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
746         spin_stop_dma (dma);
747 }
748
749 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
750 {
751         struct net2280_dma_regs __iomem *dma = ep->dma;
752
753         writel ((1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION),
754                         &dma->dmacount);
755         writel (readl (&dma->dmastat), &dma->dmastat);
756
757         writel (td_dma, &dma->dmadesc);
758         writel (dmactl, &dma->dmactl);
759
760         /* erratum 0116 workaround part 3:  pci arbiter away from net2280 */
761         (void) readl (&ep->dev->pci->pcimstctl);
762
763         writel ((1 << DMA_START), &dma->dmastat);
764
765         if (!ep->is_in)
766                 stop_out_naking (ep);
767 }
768
769 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
770 {
771         u32                     tmp;
772         struct net2280_dma_regs __iomem *dma = ep->dma;
773
774         /* FIXME can't use DMA for ZLPs */
775
776         /* on this path we "know" there's no dma active (yet) */
777         WARN_ON (readl (&dma->dmactl) & (1 << DMA_ENABLE));
778         writel (0, &ep->dma->dmactl);
779
780         /* previous OUT packet might have been short */
781         if (!ep->is_in && ((tmp = readl (&ep->regs->ep_stat))
782                                 & (1 << NAK_OUT_PACKETS)) != 0) {
783                 writel ((1 << SHORT_PACKET_TRANSFERRED_INTERRUPT),
784                         &ep->regs->ep_stat);
785
786                 tmp = readl (&ep->regs->ep_avail);
787                 if (tmp) {
788                         writel (readl (&dma->dmastat), &dma->dmastat);
789
790                         /* transfer all/some fifo data */
791                         writel (req->req.dma, &dma->dmaaddr);
792                         tmp = min (tmp, req->req.length);
793
794                         /* dma irq, faking scatterlist status */
795                         req->td->dmacount = cpu_to_le32 (req->req.length - tmp);
796                         writel ((1 << DMA_DONE_INTERRUPT_ENABLE)
797                                 | tmp, &dma->dmacount);
798                         req->td->dmadesc = 0;
799                         req->valid = 1;
800
801                         writel ((1 << DMA_ENABLE), &dma->dmactl);
802                         writel ((1 << DMA_START), &dma->dmastat);
803                         return;
804                 }
805         }
806
807         tmp = dmactl_default;
808
809         /* force packet boundaries between dma requests, but prevent the
810          * controller from automagically writing a last "short" packet
811          * (zero length) unless the driver explicitly said to do that.
812          */
813         if (ep->is_in) {
814                 if (likely ((req->req.length % ep->ep.maxpacket) != 0
815                                 || req->req.zero)) {
816                         tmp |= (1 << DMA_FIFO_VALIDATE);
817                         ep->in_fifo_validate = 1;
818                 } else
819                         ep->in_fifo_validate = 0;
820         }
821
822         /* init req->td, pointing to the current dummy */
823         req->td->dmadesc = cpu_to_le32 (ep->td_dma);
824         fill_dma_desc (ep, req, 1);
825
826         if (!use_dma_chaining)
827                 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
828
829         start_queue (ep, tmp, req->td_dma);
830 }
831
832 static inline void
833 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
834 {
835         struct net2280_dma      *end;
836         dma_addr_t              tmp;
837
838         /* swap new dummy for old, link; fill and maybe activate */
839         end = ep->dummy;
840         ep->dummy = req->td;
841         req->td = end;
842
843         tmp = ep->td_dma;
844         ep->td_dma = req->td_dma;
845         req->td_dma = tmp;
846
847         end->dmadesc = cpu_to_le32 (ep->td_dma);
848
849         fill_dma_desc (ep, req, valid);
850 }
851
852 static void
853 done (struct net2280_ep *ep, struct net2280_request *req, int status)
854 {
855         struct net2280          *dev;
856         unsigned                stopped = ep->stopped;
857
858         list_del_init (&req->queue);
859
860         if (req->req.status == -EINPROGRESS)
861                 req->req.status = status;
862         else
863                 status = req->req.status;
864
865         dev = ep->dev;
866         if (req->mapped) {
867                 pci_unmap_single (dev->pdev, req->req.dma, req->req.length,
868                         ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
869                 req->req.dma = DMA_ADDR_INVALID;
870                 req->mapped = 0;
871         }
872
873         if (status && status != -ESHUTDOWN)
874                 VDEBUG (dev, "complete %s req %p stat %d len %u/%u\n",
875                         ep->ep.name, &req->req, status,
876                         req->req.actual, req->req.length);
877
878         /* don't modify queue heads during completion callback */
879         ep->stopped = 1;
880         spin_unlock (&dev->lock);
881         req->req.complete (&ep->ep, &req->req);
882         spin_lock (&dev->lock);
883         ep->stopped = stopped;
884 }
885
886 /*-------------------------------------------------------------------------*/
887
888 static int
889 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
890 {
891         struct net2280_request  *req;
892         struct net2280_ep       *ep;
893         struct net2280          *dev;
894         unsigned long           flags;
895
896         /* we always require a cpu-view buffer, so that we can
897          * always use pio (as fallback or whatever).
898          */
899         req = container_of (_req, struct net2280_request, req);
900         if (!_req || !_req->complete || !_req->buf
901                         || !list_empty (&req->queue))
902                 return -EINVAL;
903         if (_req->length > (~0 & DMA_BYTE_COUNT_MASK))
904                 return -EDOM;
905         ep = container_of (_ep, struct net2280_ep, ep);
906         if (!_ep || (!ep->desc && ep->num != 0))
907                 return -EINVAL;
908         dev = ep->dev;
909         if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
910                 return -ESHUTDOWN;
911
912         /* FIXME implement PIO fallback for ZLPs with DMA */
913         if (ep->dma && _req->length == 0)
914                 return -EOPNOTSUPP;
915
916         /* set up dma mapping in case the caller didn't */
917         if (ep->dma && _req->dma == DMA_ADDR_INVALID) {
918                 _req->dma = pci_map_single (dev->pdev, _req->buf, _req->length,
919                         ep->is_in ? PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
920                 req->mapped = 1;
921         }
922
923 #if 0
924         VDEBUG (dev, "%s queue req %p, len %d buf %p\n",
925                         _ep->name, _req, _req->length, _req->buf);
926 #endif
927
928         spin_lock_irqsave (&dev->lock, flags);
929
930         _req->status = -EINPROGRESS;
931         _req->actual = 0;
932
933         /* kickstart this i/o queue? */
934         if (list_empty (&ep->queue) && !ep->stopped) {
935                 /* use DMA if the endpoint supports it, else pio */
936                 if (ep->dma)
937                         start_dma (ep, req);
938                 else {
939                         /* maybe there's no control data, just status ack */
940                         if (ep->num == 0 && _req->length == 0) {
941                                 allow_status (ep);
942                                 done (ep, req, 0);
943                                 VDEBUG (dev, "%s status ack\n", ep->ep.name);
944                                 goto done;
945                         }
946
947                         /* PIO ... stuff the fifo, or unblock it.  */
948                         if (ep->is_in)
949                                 write_fifo (ep, _req);
950                         else if (list_empty (&ep->queue)) {
951                                 u32     s;
952
953                                 /* OUT FIFO might have packet(s) buffered */
954                                 s = readl (&ep->regs->ep_stat);
955                                 if ((s & (1 << FIFO_EMPTY)) == 0) {
956                                         /* note:  _req->short_not_ok is
957                                          * ignored here since PIO _always_
958                                          * stops queue advance here, and
959                                          * _req->status doesn't change for
960                                          * short reads (only _req->actual)
961                                          */
962                                         if (read_fifo (ep, req)) {
963                                                 done (ep, req, 0);
964                                                 if (ep->num == 0)
965                                                         allow_status (ep);
966                                                 /* don't queue it */
967                                                 req = NULL;
968                                         } else
969                                                 s = readl (&ep->regs->ep_stat);
970                                 }
971
972                                 /* don't NAK, let the fifo fill */
973                                 if (req && (s & (1 << NAK_OUT_PACKETS)))
974                                         writel ((1 << CLEAR_NAK_OUT_PACKETS),
975                                                         &ep->regs->ep_rsp);
976                         }
977                 }
978
979         } else if (ep->dma) {
980                 int     valid = 1;
981
982                 if (ep->is_in) {
983                         int     expect;
984
985                         /* preventing magic zlps is per-engine state, not
986                          * per-transfer; irq logic must recover hiccups.
987                          */
988                         expect = likely (req->req.zero
989                                 || (req->req.length % ep->ep.maxpacket) != 0);
990                         if (expect != ep->in_fifo_validate)
991                                 valid = 0;
992                 }
993                 queue_dma (ep, req, valid);
994
995         } /* else the irq handler advances the queue. */
996
997         if (req)
998                 list_add_tail (&req->queue, &ep->queue);
999 done:
1000         spin_unlock_irqrestore (&dev->lock, flags);
1001
1002         /* pci writes may still be posted */
1003         return 0;
1004 }
1005
1006 static inline void
1007 dma_done (
1008         struct net2280_ep *ep,
1009         struct net2280_request *req,
1010         u32 dmacount,
1011         int status
1012 )
1013 {
1014         req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1015         done (ep, req, status);
1016 }
1017
1018 static void restart_dma (struct net2280_ep *ep);
1019
1020 static void scan_dma_completions (struct net2280_ep *ep)
1021 {
1022         /* only look at descriptors that were "naturally" retired,
1023          * so fifo and list head state won't matter
1024          */
1025         while (!list_empty (&ep->queue)) {
1026                 struct net2280_request  *req;
1027                 u32                     tmp;
1028
1029                 req = list_entry (ep->queue.next,
1030                                 struct net2280_request, queue);
1031                 if (!req->valid)
1032                         break;
1033                 rmb ();
1034                 tmp = le32_to_cpup (&req->td->dmacount);
1035                 if ((tmp & (1 << VALID_BIT)) != 0)
1036                         break;
1037
1038                 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1039                  * cases where DMA must be aborted; this code handles
1040                  * all non-abort DMA completions.
1041                  */
1042                 if (unlikely (req->td->dmadesc == 0)) {
1043                         /* paranoia */
1044                         tmp = readl (&ep->dma->dmacount);
1045                         if (tmp & DMA_BYTE_COUNT_MASK)
1046                                 break;
1047                         /* single transfer mode */
1048                         dma_done (ep, req, tmp, 0);
1049                         break;
1050                 } else if (!ep->is_in
1051                                 && (req->req.length % ep->ep.maxpacket) != 0) {
1052                         tmp = readl (&ep->regs->ep_stat);
1053
1054                         /* AVOID TROUBLE HERE by not issuing short reads from
1055                          * your gadget driver.  That helps avoids errata 0121,
1056                          * 0122, and 0124; not all cases trigger the warning.
1057                          */
1058                         if ((tmp & (1 << NAK_OUT_PACKETS)) == 0) {
1059                                 WARN (ep->dev, "%s lost packet sync!\n",
1060                                                 ep->ep.name);
1061                                 req->req.status = -EOVERFLOW;
1062                         } else if ((tmp = readl (&ep->regs->ep_avail)) != 0) {
1063                                 /* fifo gets flushed later */
1064                                 ep->out_overflow = 1;
1065                                 DEBUG (ep->dev, "%s dma, discard %d len %d\n",
1066                                                 ep->ep.name, tmp,
1067                                                 req->req.length);
1068                                 req->req.status = -EOVERFLOW;
1069                         }
1070                 }
1071                 dma_done (ep, req, tmp, 0);
1072         }
1073 }
1074
1075 static void restart_dma (struct net2280_ep *ep)
1076 {
1077         struct net2280_request  *req;
1078         u32                     dmactl = dmactl_default;
1079
1080         if (ep->stopped)
1081                 return;
1082         req = list_entry (ep->queue.next, struct net2280_request, queue);
1083
1084         if (!use_dma_chaining) {
1085                 start_dma (ep, req);
1086                 return;
1087         }
1088
1089         /* the 2280 will be processing the queue unless queue hiccups after
1090          * the previous transfer:
1091          *  IN:   wanted automagic zlp, head doesn't (or vice versa)
1092          *        DMA_FIFO_VALIDATE doesn't init from dma descriptors.
1093          *  OUT:  was "usb-short", we must restart.
1094          */
1095         if (ep->is_in && !req->valid) {
1096                 struct net2280_request  *entry, *prev = NULL;
1097                 int                     reqmode, done = 0;
1098
1099                 DEBUG (ep->dev, "%s dma hiccup td %p\n", ep->ep.name, req->td);
1100                 ep->in_fifo_validate = likely (req->req.zero
1101                         || (req->req.length % ep->ep.maxpacket) != 0);
1102                 if (ep->in_fifo_validate)
1103                         dmactl |= (1 << DMA_FIFO_VALIDATE);
1104                 list_for_each_entry (entry, &ep->queue, queue) {
1105                         u32             dmacount;
1106
1107                         if (entry == req)
1108                                 continue;
1109                         dmacount = entry->td->dmacount;
1110                         if (!done) {
1111                                 reqmode = likely (entry->req.zero
1112                                         || (entry->req.length
1113                                                 % ep->ep.maxpacket) != 0);
1114                                 if (reqmode == ep->in_fifo_validate) {
1115                                         entry->valid = 1;
1116                                         dmacount |= valid_bit;
1117                                         entry->td->dmacount = dmacount;
1118                                         prev = entry;
1119                                         continue;
1120                                 } else {
1121                                         /* force a hiccup */
1122                                         prev->td->dmacount |= dma_done_ie;
1123                                         done = 1;
1124                                 }
1125                         }
1126
1127                         /* walk the rest of the queue so unlinks behave */
1128                         entry->valid = 0;
1129                         dmacount &= ~valid_bit;
1130                         entry->td->dmacount = dmacount;
1131                         prev = entry;
1132                 }
1133         }
1134
1135         writel (0, &ep->dma->dmactl);
1136         start_queue (ep, dmactl, req->td_dma);
1137 }
1138
1139 static void abort_dma (struct net2280_ep *ep)
1140 {
1141         /* abort the current transfer */
1142         if (likely (!list_empty (&ep->queue))) {
1143                 /* FIXME work around errata 0121, 0122, 0124 */
1144                 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
1145                 spin_stop_dma (ep->dma);
1146         } else
1147                 stop_dma (ep->dma);
1148         scan_dma_completions (ep);
1149 }
1150
1151 /* dequeue ALL requests */
1152 static void nuke (struct net2280_ep *ep)
1153 {
1154         struct net2280_request  *req;
1155
1156         /* called with spinlock held */
1157         ep->stopped = 1;
1158         if (ep->dma)
1159                 abort_dma (ep);
1160         while (!list_empty (&ep->queue)) {
1161                 req = list_entry (ep->queue.next,
1162                                 struct net2280_request,
1163                                 queue);
1164                 done (ep, req, -ESHUTDOWN);
1165         }
1166 }
1167
1168 /* dequeue JUST ONE request */
1169 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1170 {
1171         struct net2280_ep       *ep;
1172         struct net2280_request  *req;
1173         unsigned long           flags;
1174         u32                     dmactl;
1175         int                     stopped;
1176
1177         ep = container_of (_ep, struct net2280_ep, ep);
1178         if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1179                 return -EINVAL;
1180
1181         spin_lock_irqsave (&ep->dev->lock, flags);
1182         stopped = ep->stopped;
1183
1184         /* quiesce dma while we patch the queue */
1185         dmactl = 0;
1186         ep->stopped = 1;
1187         if (ep->dma) {
1188                 dmactl = readl (&ep->dma->dmactl);
1189                 /* WARNING erratum 0127 may kick in ... */
1190                 stop_dma (ep->dma);
1191                 scan_dma_completions (ep);
1192         }
1193
1194         /* make sure it's still queued on this endpoint */
1195         list_for_each_entry (req, &ep->queue, queue) {
1196                 if (&req->req == _req)
1197                         break;
1198         }
1199         if (&req->req != _req) {
1200                 spin_unlock_irqrestore (&ep->dev->lock, flags);
1201                 return -EINVAL;
1202         }
1203
1204         /* queue head may be partially complete. */
1205         if (ep->queue.next == &req->queue) {
1206                 if (ep->dma) {
1207                         DEBUG (ep->dev, "unlink (%s) dma\n", _ep->name);
1208                         _req->status = -ECONNRESET;
1209                         abort_dma (ep);
1210                         if (likely (ep->queue.next == &req->queue)) {
1211                                 // NOTE: misreports single-transfer mode
1212                                 req->td->dmacount = 0;  /* invalidate */
1213                                 dma_done (ep, req,
1214                                         readl (&ep->dma->dmacount),
1215                                         -ECONNRESET);
1216                         }
1217                 } else {
1218                         DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1219                         done (ep, req, -ECONNRESET);
1220                 }
1221                 req = NULL;
1222
1223         /* patch up hardware chaining data */
1224         } else if (ep->dma && use_dma_chaining) {
1225                 if (req->queue.prev == ep->queue.next) {
1226                         writel (le32_to_cpu (req->td->dmadesc),
1227                                 &ep->dma->dmadesc);
1228                         if (req->td->dmacount & dma_done_ie)
1229                                 writel (readl (&ep->dma->dmacount)
1230                                                 | dma_done_ie,
1231                                         &ep->dma->dmacount);
1232                 } else {
1233                         struct net2280_request  *prev;
1234
1235                         prev = list_entry (req->queue.prev,
1236                                 struct net2280_request, queue);
1237                         prev->td->dmadesc = req->td->dmadesc;
1238                         if (req->td->dmacount & dma_done_ie)
1239                                 prev->td->dmacount |= dma_done_ie;
1240                 }
1241         }
1242
1243         if (req)
1244                 done (ep, req, -ECONNRESET);
1245         ep->stopped = stopped;
1246
1247         if (ep->dma) {
1248                 /* turn off dma on inactive queues */
1249                 if (list_empty (&ep->queue))
1250                         stop_dma (ep->dma);
1251                 else if (!ep->stopped) {
1252                         /* resume current request, or start new one */
1253                         if (req)
1254                                 writel (dmactl, &ep->dma->dmactl);
1255                         else
1256                                 start_dma (ep, list_entry (ep->queue.next,
1257                                         struct net2280_request, queue));
1258                 }
1259         }
1260
1261         spin_unlock_irqrestore (&ep->dev->lock, flags);
1262         return req ? 0 : -EOPNOTSUPP;
1263 }
1264
1265 /*-------------------------------------------------------------------------*/
1266
1267 static int net2280_fifo_status (struct usb_ep *_ep);
1268
1269 static int
1270 net2280_set_halt (struct usb_ep *_ep, int value)
1271 {
1272         struct net2280_ep       *ep;
1273         unsigned long           flags;
1274         int                     retval = 0;
1275
1276         ep = container_of (_ep, struct net2280_ep, ep);
1277         if (!_ep || (!ep->desc && ep->num != 0))
1278                 return -EINVAL;
1279         if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1280                 return -ESHUTDOWN;
1281         if (ep->desc /* not ep0 */ && (ep->desc->bmAttributes & 0x03)
1282                                                 == USB_ENDPOINT_XFER_ISOC)
1283                 return -EINVAL;
1284
1285         spin_lock_irqsave (&ep->dev->lock, flags);
1286         if (!list_empty (&ep->queue))
1287                 retval = -EAGAIN;
1288         else if (ep->is_in && value && net2280_fifo_status (_ep) != 0)
1289                 retval = -EAGAIN;
1290         else {
1291                 VDEBUG (ep->dev, "%s %s halt\n", _ep->name,
1292                                 value ? "set" : "clear");
1293                 /* set/clear, then synch memory views with the device */
1294                 if (value) {
1295                         if (ep->num == 0)
1296                                 ep->dev->protocol_stall = 1;
1297                         else
1298                                 set_halt (ep);
1299                 } else
1300                         clear_halt (ep);
1301                 (void) readl (&ep->regs->ep_rsp);
1302         }
1303         spin_unlock_irqrestore (&ep->dev->lock, flags);
1304
1305         return retval;
1306 }
1307
1308 static int
1309 net2280_fifo_status (struct usb_ep *_ep)
1310 {
1311         struct net2280_ep       *ep;
1312         u32                     avail;
1313
1314         ep = container_of (_ep, struct net2280_ep, ep);
1315         if (!_ep || (!ep->desc && ep->num != 0))
1316                 return -ENODEV;
1317         if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1318                 return -ESHUTDOWN;
1319
1320         avail = readl (&ep->regs->ep_avail) & ((1 << 12) - 1);
1321         if (avail > ep->fifo_size)
1322                 return -EOVERFLOW;
1323         if (ep->is_in)
1324                 avail = ep->fifo_size - avail;
1325         return avail;
1326 }
1327
1328 static void
1329 net2280_fifo_flush (struct usb_ep *_ep)
1330 {
1331         struct net2280_ep       *ep;
1332
1333         ep = container_of (_ep, struct net2280_ep, ep);
1334         if (!_ep || (!ep->desc && ep->num != 0))
1335                 return;
1336         if (!ep->dev->driver || ep->dev->gadget.speed == USB_SPEED_UNKNOWN)
1337                 return;
1338
1339         writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1340         (void) readl (&ep->regs->ep_rsp);
1341 }
1342
1343 static struct usb_ep_ops net2280_ep_ops = {
1344         .enable         = net2280_enable,
1345         .disable        = net2280_disable,
1346
1347         .alloc_request  = net2280_alloc_request,
1348         .free_request   = net2280_free_request,
1349
1350         .alloc_buffer   = net2280_alloc_buffer,
1351         .free_buffer    = net2280_free_buffer,
1352
1353         .queue          = net2280_queue,
1354         .dequeue        = net2280_dequeue,
1355
1356         .set_halt       = net2280_set_halt,
1357         .fifo_status    = net2280_fifo_status,
1358         .fifo_flush     = net2280_fifo_flush,
1359 };
1360
1361 /*-------------------------------------------------------------------------*/
1362
1363 static int net2280_get_frame (struct usb_gadget *_gadget)
1364 {
1365         struct net2280          *dev;
1366         unsigned long           flags;
1367         u16                     retval;
1368
1369         if (!_gadget)
1370                 return -ENODEV;
1371         dev = container_of (_gadget, struct net2280, gadget);
1372         spin_lock_irqsave (&dev->lock, flags);
1373         retval = get_idx_reg (dev->regs, REG_FRAME) & 0x03ff;
1374         spin_unlock_irqrestore (&dev->lock, flags);
1375         return retval;
1376 }
1377
1378 static int net2280_wakeup (struct usb_gadget *_gadget)
1379 {
1380         struct net2280          *dev;
1381         u32                     tmp;
1382         unsigned long           flags;
1383
1384         if (!_gadget)
1385                 return 0;
1386         dev = container_of (_gadget, struct net2280, gadget);
1387
1388         spin_lock_irqsave (&dev->lock, flags);
1389         tmp = readl (&dev->usb->usbctl);
1390         if (tmp & (1 << DEVICE_REMOTE_WAKEUP_ENABLE))
1391                 writel (1 << GENERATE_RESUME, &dev->usb->usbstat);
1392         spin_unlock_irqrestore (&dev->lock, flags);
1393
1394         /* pci writes may still be posted */
1395         return 0;
1396 }
1397
1398 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1399 {
1400         struct net2280          *dev;
1401         u32                     tmp;
1402         unsigned long           flags;
1403
1404         if (!_gadget)
1405                 return 0;
1406         dev = container_of (_gadget, struct net2280, gadget);
1407
1408         spin_lock_irqsave (&dev->lock, flags);
1409         tmp = readl (&dev->usb->usbctl);
1410         if (value)
1411                 tmp |= (1 << SELF_POWERED_STATUS);
1412         else
1413                 tmp &= ~(1 << SELF_POWERED_STATUS);
1414         writel (tmp, &dev->usb->usbctl);
1415         spin_unlock_irqrestore (&dev->lock, flags);
1416
1417         return 0;
1418 }
1419
1420 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1421 {
1422         struct net2280  *dev;
1423         u32             tmp;
1424         unsigned long   flags;
1425
1426         if (!_gadget)
1427                 return -ENODEV;
1428         dev = container_of (_gadget, struct net2280, gadget);
1429
1430         spin_lock_irqsave (&dev->lock, flags);
1431         tmp = readl (&dev->usb->usbctl);
1432         dev->softconnect = (is_on != 0);
1433         if (is_on)
1434                 tmp |= (1 << USB_DETECT_ENABLE);
1435         else
1436                 tmp &= ~(1 << USB_DETECT_ENABLE);
1437         writel (tmp, &dev->usb->usbctl);
1438         spin_unlock_irqrestore (&dev->lock, flags);
1439
1440         return 0;
1441 }
1442
1443 static const struct usb_gadget_ops net2280_ops = {
1444         .get_frame      = net2280_get_frame,
1445         .wakeup         = net2280_wakeup,
1446         .set_selfpowered = net2280_set_selfpowered,
1447         .pullup         = net2280_pullup,
1448 };
1449
1450 /*-------------------------------------------------------------------------*/
1451
1452 #ifdef  CONFIG_USB_GADGET_DEBUG_FILES
1453
1454 /* FIXME move these into procfs, and use seq_file.
1455  * Sysfs _still_ doesn't behave for arbitrarily sized files,
1456  * and also doesn't help products using this with 2.4 kernels.
1457  */
1458
1459 /* "function" sysfs attribute */
1460 static ssize_t
1461 show_function (struct device *_dev, char *buf)
1462 {
1463         struct net2280  *dev = dev_get_drvdata (_dev);
1464
1465         if (!dev->driver
1466                         || !dev->driver->function
1467                         || strlen (dev->driver->function) > PAGE_SIZE)
1468                 return 0;
1469         return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1470 }
1471 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1472
1473 static ssize_t
1474 show_registers (struct device *_dev, char *buf)
1475 {
1476         struct net2280          *dev;
1477         char                    *next;
1478         unsigned                size, t;
1479         unsigned long           flags;
1480         int                     i;
1481         u32                     t1, t2;
1482         char                    *s;
1483
1484         dev = dev_get_drvdata (_dev);
1485         next = buf;
1486         size = PAGE_SIZE;
1487         spin_lock_irqsave (&dev->lock, flags);
1488
1489         if (dev->driver)
1490                 s = dev->driver->driver.name;
1491         else
1492                 s = "(none)";
1493
1494         /* Main Control Registers */
1495         t = scnprintf (next, size, "%s version " DRIVER_VERSION
1496                         ", chiprev %04x, dma %s\n\n"
1497                         "devinit %03x fifoctl %08x gadget '%s'\n"
1498                         "pci irqenb0 %02x irqenb1 %08x "
1499                         "irqstat0 %04x irqstat1 %08x\n",
1500                         driver_name, dev->chiprev,
1501                         use_dma
1502                                 ? (use_dma_chaining ? "chaining" : "enabled")
1503                                 : "disabled",
1504                         readl (&dev->regs->devinit),
1505                         readl (&dev->regs->fifoctl),
1506                         s,
1507                         readl (&dev->regs->pciirqenb0),
1508                         readl (&dev->regs->pciirqenb1),
1509                         readl (&dev->regs->irqstat0),
1510                         readl (&dev->regs->irqstat1));
1511         size -= t;
1512         next += t;
1513
1514         /* USB Control Registers */
1515         t1 = readl (&dev->usb->usbctl);
1516         t2 = readl (&dev->usb->usbstat);
1517         if (t1 & (1 << VBUS_PIN)) {
1518                 if (t2 & (1 << HIGH_SPEED))
1519                         s = "high speed";
1520                 else if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1521                         s = "powered";
1522                 else
1523                         s = "full speed";
1524                 /* full speed bit (6) not working?? */
1525         } else
1526                         s = "not attached";
1527         t = scnprintf (next, size,
1528                         "stdrsp %08x usbctl %08x usbstat %08x "
1529                                 "addr 0x%02x (%s)\n",
1530                         readl (&dev->usb->stdrsp), t1, t2,
1531                         readl (&dev->usb->ouraddr), s);
1532         size -= t;
1533         next += t;
1534
1535         /* PCI Master Control Registers */
1536
1537         /* DMA Control Registers */
1538
1539         /* Configurable EP Control Registers */
1540         for (i = 0; i < 7; i++) {
1541                 struct net2280_ep       *ep;
1542
1543                 ep = &dev->ep [i];
1544                 if (i && !ep->desc)
1545                         continue;
1546
1547                 t1 = readl (&ep->regs->ep_cfg);
1548                 t2 = readl (&ep->regs->ep_rsp) & 0xff;
1549                 t = scnprintf (next, size,
1550                                 "\n%s\tcfg %05x rsp (%02x) %s%s%s%s%s%s%s%s"
1551                                         "irqenb %02x\n",
1552                                 ep->ep.name, t1, t2,
1553                                 (t2 & (1 << CLEAR_NAK_OUT_PACKETS))
1554                                         ? "NAK " : "",
1555                                 (t2 & (1 << CLEAR_EP_HIDE_STATUS_PHASE))
1556                                         ? "hide " : "",
1557                                 (t2 & (1 << CLEAR_EP_FORCE_CRC_ERROR))
1558                                         ? "CRC " : "",
1559                                 (t2 & (1 << CLEAR_INTERRUPT_MODE))
1560                                         ? "interrupt " : "",
1561                                 (t2 & (1<<CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE))
1562                                         ? "status " : "",
1563                                 (t2 & (1 << CLEAR_NAK_OUT_PACKETS_MODE))
1564                                         ? "NAKmode " : "",
1565                                 (t2 & (1 << CLEAR_ENDPOINT_TOGGLE))
1566                                         ? "DATA1 " : "DATA0 ",
1567                                 (t2 & (1 << CLEAR_ENDPOINT_HALT))
1568                                         ? "HALT " : "",
1569                                 readl (&ep->regs->ep_irqenb));
1570                 size -= t;
1571                 next += t;
1572
1573                 t = scnprintf (next, size,
1574                                 "\tstat %08x avail %04x "
1575                                 "(ep%d%s-%s)%s\n",
1576                                 readl (&ep->regs->ep_stat),
1577                                 readl (&ep->regs->ep_avail),
1578                                 t1 & 0x0f, DIR_STRING (t1),
1579                                 type_string (t1 >> 8),
1580                                 ep->stopped ? "*" : "");
1581                 size -= t;
1582                 next += t;
1583
1584                 if (!ep->dma)
1585                         continue;
1586
1587                 t = scnprintf (next, size,
1588                                 "  dma\tctl %08x stat %08x count %08x\n"
1589                                 "\taddr %08x desc %08x\n",
1590                                 readl (&ep->dma->dmactl),
1591                                 readl (&ep->dma->dmastat),
1592                                 readl (&ep->dma->dmacount),
1593                                 readl (&ep->dma->dmaaddr),
1594                                 readl (&ep->dma->dmadesc));
1595                 size -= t;
1596                 next += t;
1597
1598         }
1599
1600         /* Indexed Registers */
1601                 // none yet 
1602
1603         /* Statistics */
1604         t = scnprintf (next, size, "\nirqs:  ");
1605         size -= t;
1606         next += t;
1607         for (i = 0; i < 7; i++) {
1608                 struct net2280_ep       *ep;
1609
1610                 ep = &dev->ep [i];
1611                 if (i && !ep->irqs)
1612                         continue;
1613                 t = scnprintf (next, size, " %s/%lu", ep->ep.name, ep->irqs);
1614                 size -= t;
1615                 next += t;
1616
1617         }
1618         t = scnprintf (next, size, "\n");
1619         size -= t;
1620         next += t;
1621
1622         spin_unlock_irqrestore (&dev->lock, flags);
1623
1624         return PAGE_SIZE - size;
1625 }
1626 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1627
1628 static ssize_t
1629 show_queues (struct device *_dev, char *buf)
1630 {
1631         struct net2280          *dev;
1632         char                    *next;
1633         unsigned                size;
1634         unsigned long           flags;
1635         int                     i;
1636
1637         dev = dev_get_drvdata (_dev);
1638         next = buf;
1639         size = PAGE_SIZE;
1640         spin_lock_irqsave (&dev->lock, flags);
1641
1642         for (i = 0; i < 7; i++) {
1643                 struct net2280_ep               *ep = &dev->ep [i];
1644                 struct net2280_request          *req;
1645                 int                             t;
1646
1647                 if (i != 0) {
1648                         const struct usb_endpoint_descriptor    *d;
1649
1650                         d = ep->desc;
1651                         if (!d)
1652                                 continue;
1653                         t = d->bEndpointAddress;
1654                         t = scnprintf (next, size,
1655                                 "\n%s (ep%d%s-%s) max %04x %s fifo %d\n",
1656                                 ep->ep.name, t & USB_ENDPOINT_NUMBER_MASK,
1657                                 (t & USB_DIR_IN) ? "in" : "out",
1658                                 ({ char *val;
1659                                  switch (d->bmAttributes & 0x03) {
1660                                  case USB_ENDPOINT_XFER_BULK:
1661                                         val = "bulk"; break;
1662                                  case USB_ENDPOINT_XFER_INT:
1663                                         val = "intr"; break;
1664                                  default:
1665                                         val = "iso"; break;
1666                                  }; val; }),
1667                                 le16_to_cpu (d->wMaxPacketSize) & 0x1fff,
1668                                 ep->dma ? "dma" : "pio", ep->fifo_size
1669                                 );
1670                 } else /* ep0 should only have one transfer queued */
1671                         t = scnprintf (next, size, "ep0 max 64 pio %s\n",
1672                                         ep->is_in ? "in" : "out");
1673                 if (t <= 0 || t > size)
1674                         goto done;
1675                 size -= t;
1676                 next += t;
1677
1678                 if (list_empty (&ep->queue)) {
1679                         t = scnprintf (next, size, "\t(nothing queued)\n");
1680                         if (t <= 0 || t > size)
1681                                 goto done;
1682                         size -= t;
1683                         next += t;
1684                         continue;
1685                 }
1686                 list_for_each_entry (req, &ep->queue, queue) {
1687                         if (ep->dma && req->td_dma == readl (&ep->dma->dmadesc))
1688                                 t = scnprintf (next, size,
1689                                         "\treq %p len %d/%d "
1690                                         "buf %p (dmacount %08x)\n",
1691                                         &req->req, req->req.actual,
1692                                         req->req.length, req->req.buf,
1693                                         readl (&ep->dma->dmacount));
1694                         else
1695                                 t = scnprintf (next, size,
1696                                         "\treq %p len %d/%d buf %p\n",
1697                                         &req->req, req->req.actual,
1698                                         req->req.length, req->req.buf);
1699                         if (t <= 0 || t > size)
1700                                 goto done;
1701                         size -= t;
1702                         next += t;
1703
1704                         if (ep->dma) {
1705                                 struct net2280_dma      *td;
1706
1707                                 td = req->td;
1708                                 t = scnprintf (next, size, "\t    td %08x "
1709                                         " count %08x buf %08x desc %08x\n",
1710                                         (u32) req->td_dma,
1711                                         le32_to_cpu (td->dmacount),
1712                                         le32_to_cpu (td->dmaaddr),
1713                                         le32_to_cpu (td->dmadesc));
1714                                 if (t <= 0 || t > size)
1715                                         goto done;
1716                                 size -= t;
1717                                 next += t;
1718                         }
1719                 }
1720         }
1721
1722 done:
1723         spin_unlock_irqrestore (&dev->lock, flags);
1724         return PAGE_SIZE - size;
1725 }
1726 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1727
1728
1729 #else
1730
1731 #define device_create_file(a,b) do {} while (0)
1732 #define device_remove_file      device_create_file
1733
1734 #endif
1735
1736 /*-------------------------------------------------------------------------*/
1737
1738 /* another driver-specific mode might be a request type doing dma
1739  * to/from another device fifo instead of to/from memory.
1740  */
1741
1742 static void set_fifo_mode (struct net2280 *dev, int mode)
1743 {
1744         /* keeping high bits preserves BAR2 */
1745         writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
1746
1747         /* always ep-{a,b,e,f} ... maybe not ep-c or ep-d */
1748         INIT_LIST_HEAD (&dev->gadget.ep_list);
1749         list_add_tail (&dev->ep [1].ep.ep_list, &dev->gadget.ep_list);
1750         list_add_tail (&dev->ep [2].ep.ep_list, &dev->gadget.ep_list);
1751         switch (mode) {
1752         case 0:
1753                 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1754                 list_add_tail (&dev->ep [4].ep.ep_list, &dev->gadget.ep_list);
1755                 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 1024;
1756                 break;
1757         case 1:
1758                 dev->ep [1].fifo_size = dev->ep [2].fifo_size = 2048;
1759                 break;
1760         case 2:
1761                 list_add_tail (&dev->ep [3].ep.ep_list, &dev->gadget.ep_list);
1762                 dev->ep [1].fifo_size = 2048;
1763                 dev->ep [2].fifo_size = 1024;
1764                 break;
1765         }
1766         /* fifo sizes for ep0, ep-c, ep-d, ep-e, and ep-f never change */
1767         list_add_tail (&dev->ep [5].ep.ep_list, &dev->gadget.ep_list);
1768         list_add_tail (&dev->ep [6].ep.ep_list, &dev->gadget.ep_list);
1769 }
1770
1771 /**
1772  * net2280_set_fifo_mode - change allocation of fifo buffers
1773  * @gadget: access to the net2280 device that will be updated
1774  * @mode: 0 for default, four 1kB buffers (ep-a through ep-d);
1775  *      1 for two 2kB buffers (ep-a and ep-b only);
1776  *      2 for one 2kB buffer (ep-a) and two 1kB ones (ep-b, ep-c).
1777  *
1778  * returns zero on success, else negative errno.  when this succeeds,
1779  * the contents of gadget->ep_list may have changed.
1780  *
1781  * you may only call this function when endpoints a-d are all disabled.
1782  * use it whenever extra hardware buffering can help performance, such
1783  * as before enabling "high bandwidth" interrupt endpoints that use
1784  * maxpacket bigger than 512 (when double buffering would otherwise
1785  * be unavailable).
1786  */
1787 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1788 {
1789         int                     i;
1790         struct net2280          *dev;
1791         int                     status = 0;
1792         unsigned long           flags;
1793
1794         if (!gadget)
1795                 return -ENODEV;
1796         dev = container_of (gadget, struct net2280, gadget);
1797
1798         spin_lock_irqsave (&dev->lock, flags);
1799
1800         for (i = 1; i <= 4; i++)
1801                 if (dev->ep [i].desc) {
1802                         status = -EINVAL;
1803                         break;
1804                 }
1805         if (mode < 0 || mode > 2)
1806                 status = -EINVAL;
1807         if (status == 0)
1808                 set_fifo_mode (dev, mode);
1809         spin_unlock_irqrestore (&dev->lock, flags);
1810
1811         if (status == 0) {
1812                 if (mode == 1)
1813                         DEBUG (dev, "fifo:  ep-a 2K, ep-b 2K\n");
1814                 else if (mode == 2)
1815                         DEBUG (dev, "fifo:  ep-a 2K, ep-b 1K, ep-c 1K\n");
1816                 /* else all are 1K */
1817         }
1818         return status;
1819 }
1820 EXPORT_SYMBOL (net2280_set_fifo_mode);
1821
1822 /*-------------------------------------------------------------------------*/
1823
1824 /* keeping it simple:
1825  * - one bus driver, initted first;
1826  * - one function driver, initted second
1827  *
1828  * most of the work to support multiple net2280 controllers would
1829  * be to associate this gadget driver (yes?) with all of them, or
1830  * perhaps to bind specific drivers to specific devices.
1831  */
1832
1833 static struct net2280   *the_controller;
1834
1835 static void usb_reset (struct net2280 *dev)
1836 {
1837         u32     tmp;
1838
1839         dev->gadget.speed = USB_SPEED_UNKNOWN;
1840         (void) readl (&dev->usb->usbctl);
1841
1842         net2280_led_init (dev);
1843
1844         /* disable automatic responses, and irqs */
1845         writel (0, &dev->usb->stdrsp);
1846         writel (0, &dev->regs->pciirqenb0);
1847         writel (0, &dev->regs->pciirqenb1);
1848
1849         /* clear old dma and irq state */
1850         for (tmp = 0; tmp < 4; tmp++) {
1851                 struct net2280_ep       *ep = &dev->ep [tmp + 1];
1852
1853                 if (ep->dma)
1854                         abort_dma (ep);
1855         }
1856         writel (~0, &dev->regs->irqstat0),
1857         writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
1858
1859         /* reset, and enable pci */
1860         tmp = readl (&dev->regs->devinit)
1861                 | (1 << PCI_ENABLE)
1862                 | (1 << FIFO_SOFT_RESET)
1863                 | (1 << USB_SOFT_RESET)
1864                 | (1 << M8051_RESET);
1865         writel (tmp, &dev->regs->devinit);
1866
1867         /* standard fifo and endpoint allocations */
1868         set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1869 }
1870
1871 static void usb_reinit (struct net2280 *dev)
1872 {
1873         u32     tmp;
1874         int     init_dma;
1875
1876         /* use_dma changes are ignored till next device re-init */
1877         init_dma = use_dma;
1878
1879         /* basic endpoint init */
1880         for (tmp = 0; tmp < 7; tmp++) {
1881                 struct net2280_ep       *ep = &dev->ep [tmp];
1882
1883                 ep->ep.name = ep_name [tmp];
1884                 ep->dev = dev;
1885                 ep->num = tmp;
1886
1887                 if (tmp > 0 && tmp <= 4) {
1888                         ep->fifo_size = 1024;
1889                         if (init_dma)
1890                                 ep->dma = &dev->dma [tmp - 1];
1891                 } else
1892                         ep->fifo_size = 64;
1893                 ep->regs = &dev->epregs [tmp];
1894                 ep_reset (dev->regs, ep);
1895         }
1896         dev->ep [0].ep.maxpacket = 64;
1897         dev->ep [5].ep.maxpacket = 64;
1898         dev->ep [6].ep.maxpacket = 64;
1899
1900         dev->gadget.ep0 = &dev->ep [0].ep;
1901         dev->ep [0].stopped = 0;
1902         INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1903
1904         /* we want to prevent lowlevel/insecure access from the USB host,
1905          * but erratum 0119 means this enable bit is ignored
1906          */
1907         for (tmp = 0; tmp < 5; tmp++)
1908                 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1909 }
1910
1911 static void ep0_start (struct net2280 *dev)
1912 {
1913         writel (  (1 << CLEAR_EP_HIDE_STATUS_PHASE)
1914                 | (1 << CLEAR_NAK_OUT_PACKETS)
1915                 | (1 << CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE)
1916                 , &dev->epregs [0].ep_rsp);
1917
1918         /*
1919          * hardware optionally handles a bunch of standard requests
1920          * that the API hides from drivers anyway.  have it do so.
1921          * endpoint status/features are handled in software, to
1922          * help pass tests for some dubious behavior.
1923          */
1924         writel (  (1 << SET_TEST_MODE)
1925                 | (1 << SET_ADDRESS)
1926                 | (1 << DEVICE_SET_CLEAR_DEVICE_REMOTE_WAKEUP)
1927                 | (1 << GET_DEVICE_STATUS)
1928                 | (1 << GET_INTERFACE_STATUS)
1929                 , &dev->usb->stdrsp);
1930         writel (  (1 << USB_ROOT_PORT_WAKEUP_ENABLE)
1931                 | (1 << SELF_POWERED_USB_DEVICE)
1932                 | (1 << REMOTE_WAKEUP_SUPPORT)
1933                 | (dev->softconnect << USB_DETECT_ENABLE)
1934                 | (1 << SELF_POWERED_STATUS)
1935                 , &dev->usb->usbctl);
1936
1937         /* enable irqs so we can see ep0 and general operation  */
1938         writel (  (1 << SETUP_PACKET_INTERRUPT_ENABLE)
1939                 | (1 << ENDPOINT_0_INTERRUPT_ENABLE)
1940                 , &dev->regs->pciirqenb0);
1941         writel (  (1 << PCI_INTERRUPT_ENABLE)
1942                 | (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT_ENABLE)
1943                 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT_ENABLE)
1944                 | (1 << PCI_RETRY_ABORT_INTERRUPT_ENABLE)
1945                 | (1 << VBUS_INTERRUPT_ENABLE)
1946                 | (1 << ROOT_PORT_RESET_INTERRUPT_ENABLE)
1947                 | (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT_ENABLE)
1948                 , &dev->regs->pciirqenb1);
1949
1950         /* don't leave any writes posted */
1951         (void) readl (&dev->usb->usbctl);
1952 }
1953
1954 /* when a driver is successfully registered, it will receive
1955  * control requests including set_configuration(), which enables
1956  * non-control requests.  then usb traffic follows until a
1957  * disconnect is reported.  then a host may connect again, or
1958  * the driver might get unbound.
1959  */
1960 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1961 {
1962         struct net2280          *dev = the_controller;
1963         int                     retval;
1964         unsigned                i;
1965
1966         /* insist on high speed support from the driver, since
1967          * (dev->usb->xcvrdiag & FORCE_FULL_SPEED_MODE)
1968          * "must not be used in normal operation"
1969          */
1970         if (!driver
1971                         || driver->speed != USB_SPEED_HIGH
1972                         || !driver->bind
1973                         || !driver->unbind
1974                         || !driver->setup)
1975                 return -EINVAL;
1976         if (!dev)
1977                 return -ENODEV;
1978         if (dev->driver)
1979                 return -EBUSY;
1980
1981         for (i = 0; i < 7; i++)
1982                 dev->ep [i].irqs = 0;
1983
1984         /* hook up the driver ... */
1985         dev->softconnect = 1;
1986         driver->driver.bus = NULL;
1987         dev->driver = driver;
1988         dev->gadget.dev.driver = &driver->driver;
1989         retval = driver->bind (&dev->gadget);
1990         if (retval) {
1991                 DEBUG (dev, "bind to driver %s --> %d\n",
1992                                 driver->driver.name, retval);
1993                 dev->driver = NULL;
1994                 dev->gadget.dev.driver = NULL;
1995                 return retval;
1996         }
1997
1998         device_create_file (&dev->pdev->dev, &dev_attr_function);
1999         device_create_file (&dev->pdev->dev, &dev_attr_queues);
2000
2001         /* ... then enable host detection and ep0; and we're ready
2002          * for set_configuration as well as eventual disconnect.
2003          */
2004         net2280_led_active (dev, 1);
2005         ep0_start (dev);
2006
2007         DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2008                         driver->driver.name,
2009                         readl (&dev->usb->usbctl),
2010                         readl (&dev->usb->stdrsp));
2011
2012         /* pci writes may still be posted */
2013         return 0;
2014 }
2015 EXPORT_SYMBOL (usb_gadget_register_driver);
2016
2017 static void
2018 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2019 {
2020         int                     i;
2021
2022         /* don't disconnect if it's not connected */
2023         if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2024                 driver = NULL;
2025
2026         /* stop hardware; prevent new request submissions;
2027          * and kill any outstanding requests.
2028          */
2029         usb_reset (dev);
2030         for (i = 0; i < 7; i++)
2031                 nuke (&dev->ep [i]);
2032
2033         /* report disconnect; the driver is already quiesced */
2034         if (driver) {
2035                 spin_unlock (&dev->lock);
2036                 driver->disconnect (&dev->gadget);
2037                 spin_lock (&dev->lock);
2038         }
2039
2040         usb_reinit (dev);
2041 }
2042
2043 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2044 {
2045         struct net2280  *dev = the_controller;
2046         unsigned long   flags;
2047
2048         if (!dev)
2049                 return -ENODEV;
2050         if (!driver || driver != dev->driver)
2051                 return -EINVAL;
2052
2053         spin_lock_irqsave (&dev->lock, flags);
2054         stop_activity (dev, driver);
2055         spin_unlock_irqrestore (&dev->lock, flags);
2056
2057         net2280_pullup (&dev->gadget, 0);
2058
2059         driver->unbind (&dev->gadget);
2060         dev->gadget.dev.driver = NULL;
2061         dev->driver = NULL;
2062
2063         net2280_led_active (dev, 0);
2064         device_remove_file (&dev->pdev->dev, &dev_attr_function);
2065         device_remove_file (&dev->pdev->dev, &dev_attr_queues);
2066
2067         DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2068         return 0;
2069 }
2070 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2071
2072
2073 /*-------------------------------------------------------------------------*/
2074
2075 /* handle ep0, ep-e, ep-f with 64 byte packets: packet per irq.
2076  * also works for dma-capable endpoints, in pio mode or just
2077  * to manually advance the queue after short OUT transfers.
2078  */
2079 static void handle_ep_small (struct net2280_ep *ep)
2080 {
2081         struct net2280_request  *req;
2082         u32                     t;
2083         /* 0 error, 1 mid-data, 2 done */
2084         int                     mode = 1;
2085
2086         if (!list_empty (&ep->queue))
2087                 req = list_entry (ep->queue.next,
2088                         struct net2280_request, queue);
2089         else
2090                 req = NULL;
2091
2092         /* ack all, and handle what we care about */
2093         t = readl (&ep->regs->ep_stat);
2094         ep->irqs++;
2095 #if 0
2096         VDEBUG (ep->dev, "%s ack ep_stat %08x, req %p\n",
2097                         ep->ep.name, t, req ? &req->req : 0);
2098 #endif
2099         writel (t & ~(1 << NAK_OUT_PACKETS), &ep->regs->ep_stat);
2100
2101         /* for ep0, monitor token irqs to catch data stage length errors
2102          * and to synchronize on status.
2103          *
2104          * also, to defer reporting of protocol stalls ... here's where
2105          * data or status first appears, handling stalls here should never
2106          * cause trouble on the host side..
2107          *
2108          * control requests could be slightly faster without token synch for
2109          * status, but status can jam up that way.
2110          */
2111         if (unlikely (ep->num == 0)) {
2112                 if (ep->is_in) {
2113                         /* status; stop NAKing */
2114                         if (t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT)) {
2115                                 if (ep->dev->protocol_stall) {
2116                                         ep->stopped = 1;
2117                                         set_halt (ep);
2118                                 }
2119                                 if (!req)
2120                                         allow_status (ep);
2121                                 mode = 2;
2122                         /* reply to extra IN data tokens with a zlp */
2123                         } else if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2124                                 if (ep->dev->protocol_stall) {
2125                                         ep->stopped = 1;
2126                                         set_halt (ep);
2127                                         mode = 2;
2128                                 } else if (!req && ep->stopped)
2129                                         write_fifo (ep, NULL);
2130                         }
2131                 } else {
2132                         /* status; stop NAKing */
2133                         if (t & (1 << DATA_IN_TOKEN_INTERRUPT)) {
2134                                 if (ep->dev->protocol_stall) {
2135                                         ep->stopped = 1;
2136                                         set_halt (ep);
2137                                 }
2138                                 mode = 2;
2139                         /* an extra OUT token is an error */
2140                         } else if (((t & (1 << DATA_OUT_PING_TOKEN_INTERRUPT))
2141                                         && req
2142                                         && req->req.actual == req->req.length)
2143                                         || !req) {
2144                                 ep->dev->protocol_stall = 1;
2145                                 set_halt (ep);
2146                                 ep->stopped = 1;
2147                                 if (req)
2148                                         done (ep, req, -EOVERFLOW);
2149                                 req = NULL;
2150                         }
2151                 }
2152         }
2153
2154         if (unlikely (!req))
2155                 return;
2156
2157         /* manual DMA queue advance after short OUT */
2158         if (likely (ep->dma != 0)) {
2159                 if (t & (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)) {
2160                         u32     count;
2161                         int     stopped = ep->stopped;
2162
2163                         /* TRANSFERRED works around OUT_DONE erratum 0112.
2164                          * we expect (N <= maxpacket) bytes; host wrote M.
2165                          * iff (M < N) we won't ever see a DMA interrupt.
2166                          */
2167                         ep->stopped = 1;
2168                         for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2169
2170                                 /* any preceding dma transfers must finish.
2171                                  * dma handles (M >= N), may empty the queue
2172                                  */
2173                                 scan_dma_completions (ep);
2174                                 if (unlikely (list_empty (&ep->queue)
2175                                                 || ep->out_overflow)) {
2176                                         req = NULL;
2177                                         break;
2178                                 }
2179                                 req = list_entry (ep->queue.next,
2180                                         struct net2280_request, queue);
2181
2182                                 /* here either (M < N), a "real" short rx;
2183                                  * or (M == N) and the queue didn't empty
2184                                  */
2185                                 if (likely (t & (1 << FIFO_EMPTY))) {
2186                                         count = readl (&ep->dma->dmacount);
2187                                         count &= DMA_BYTE_COUNT_MASK;
2188                                         if (readl (&ep->dma->dmadesc)
2189                                                         != req->td_dma)
2190                                                 req = NULL;
2191                                         break;
2192                                 }
2193                                 udelay(1);
2194                         }
2195
2196                         /* stop DMA, leave ep NAKing */
2197                         writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2198                         spin_stop_dma (ep->dma);
2199
2200                         if (likely (req)) {
2201                                 req->td->dmacount = 0;
2202                                 t = readl (&ep->regs->ep_avail);
2203                                 dma_done (ep, req, count, t);
2204                         }
2205
2206                         /* also flush to prevent erratum 0106 trouble */
2207                         if (unlikely (ep->out_overflow
2208                                         || (ep->dev->chiprev == 0x0100
2209                                                 && ep->dev->gadget.speed
2210                                                         == USB_SPEED_FULL))) {
2211                                 out_flush (ep);
2212                                 ep->out_overflow = 0;
2213                         }
2214
2215                         /* (re)start dma if needed, stop NAKing */
2216                         ep->stopped = stopped;
2217                         if (!list_empty (&ep->queue))
2218                                 restart_dma (ep);
2219                 } else
2220                         DEBUG (ep->dev, "%s dma ep_stat %08x ??\n",
2221                                         ep->ep.name, t);
2222                 return;
2223
2224         /* data packet(s) received (in the fifo, OUT) */
2225         } else if (t & (1 << DATA_PACKET_RECEIVED_INTERRUPT)) {
2226                 if (read_fifo (ep, req) && ep->num != 0)
2227                         mode = 2;
2228
2229         /* data packet(s) transmitted (IN) */
2230         } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2231                 unsigned        len;
2232
2233                 len = req->req.length - req->req.actual;
2234                 if (len > ep->ep.maxpacket)
2235                         len = ep->ep.maxpacket;
2236                 req->req.actual += len;
2237
2238                 /* if we wrote it all, we're usually done */
2239                 if (req->req.actual == req->req.length) {
2240                         if (ep->num == 0) {
2241                                 /* wait for control status */
2242                                 if (mode != 2)
2243                                         req = NULL;
2244                         } else if (!req->req.zero || len != ep->ep.maxpacket)
2245                                 mode = 2;
2246                 }
2247
2248         /* there was nothing to do ...  */
2249         } else if (mode == 1)
2250                 return;
2251
2252         /* done */
2253         if (mode == 2) {
2254                 /* stream endpoints often resubmit/unlink in completion */
2255                 done (ep, req, 0);
2256
2257                 /* maybe advance queue to next request */
2258                 if (ep->num == 0) {
2259                         /* NOTE:  net2280 could let gadget driver start the
2260                          * status stage later. since not all controllers let
2261                          * them control that, the api doesn't (yet) allow it.
2262                          */
2263                         if (!ep->stopped)
2264                                 allow_status (ep);
2265                         req = NULL;
2266                 } else {
2267                         if (!list_empty (&ep->queue) && !ep->stopped)
2268                                 req = list_entry (ep->queue.next,
2269                                         struct net2280_request, queue);
2270                         else
2271                                 req = NULL;
2272                         if (req && !ep->is_in)
2273                                 stop_out_naking (ep);
2274                 }
2275         }
2276
2277         /* is there a buffer for the next packet?
2278          * for best streaming performance, make sure there is one.
2279          */
2280         if (req && !ep->stopped) {
2281
2282                 /* load IN fifo with next packet (may be zlp) */
2283                 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2284                         write_fifo (ep, &req->req);
2285         }
2286 }
2287
2288 static struct net2280_ep *
2289 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2290 {
2291         struct net2280_ep       *ep;
2292
2293         if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2294                 return &dev->ep [0];
2295         list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2296                 u8      bEndpointAddress;
2297
2298                 if (!ep->desc)
2299                         continue;
2300                 bEndpointAddress = ep->desc->bEndpointAddress;
2301                 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2302                         continue;
2303                 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2304                         return ep;
2305         }
2306         return NULL;
2307 }
2308
2309 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2310 {
2311         struct net2280_ep       *ep;
2312         u32                     num, scratch;
2313
2314         /* most of these don't need individual acks */
2315         stat &= ~(1 << INTA_ASSERTED);
2316         if (!stat)
2317                 return;
2318         // DEBUG (dev, "irqstat0 %04x\n", stat);
2319
2320         /* starting a control request? */
2321         if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2322                 union {
2323                         u32                     raw [2];
2324                         struct usb_ctrlrequest  r;
2325                 } u;
2326                 int                             tmp = 0;
2327                 struct net2280_request          *req;
2328
2329                 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2330                         if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2331                                 dev->gadget.speed = USB_SPEED_HIGH;
2332                         else
2333                                 dev->gadget.speed = USB_SPEED_FULL;
2334                         net2280_led_speed (dev, dev->gadget.speed);
2335                         DEBUG (dev, "%s speed\n",
2336                                 (dev->gadget.speed == USB_SPEED_HIGH)
2337                                         ? "high" : "full");
2338                 }
2339
2340                 ep = &dev->ep [0];
2341                 ep->irqs++;
2342
2343                 /* make sure any leftover request state is cleared */
2344                 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2345                 while (!list_empty (&ep->queue)) {
2346                         req = list_entry (ep->queue.next,
2347                                         struct net2280_request, queue);
2348                         done (ep, req, (req->req.actual == req->req.length)
2349                                                 ? 0 : -EPROTO);
2350                 }
2351                 ep->stopped = 0;
2352                 dev->protocol_stall = 0;
2353                 writel (  (1 << TIMEOUT)
2354                         | (1 << USB_STALL_SENT)
2355                         | (1 << USB_IN_NAK_SENT)
2356                         | (1 << USB_IN_ACK_RCVD)
2357                         | (1 << USB_OUT_PING_NAK_SENT)
2358                         | (1 << USB_OUT_ACK_SENT)
2359                         | (1 << FIFO_OVERFLOW)
2360                         | (1 << FIFO_UNDERFLOW)
2361                         | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2362                         | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2363                         | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2364                         | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2365                         | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2366                         | (1 << DATA_IN_TOKEN_INTERRUPT)
2367                         , &ep->regs->ep_stat);
2368                 u.raw [0] = readl (&dev->usb->setup0123);
2369                 u.raw [1] = readl (&dev->usb->setup4567);
2370                 
2371                 cpu_to_le32s (&u.raw [0]);
2372                 cpu_to_le32s (&u.raw [1]);
2373
2374                 le16_to_cpus (&u.r.wValue);
2375                 le16_to_cpus (&u.r.wIndex);
2376                 le16_to_cpus (&u.r.wLength);
2377
2378                 /* ack the irq */
2379                 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2380                 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2381
2382                 /* watch control traffic at the token level, and force
2383                  * synchronization before letting the status stage happen.
2384                  * FIXME ignore tokens we'll NAK, until driver responds.
2385                  * that'll mean a lot less irqs for some drivers.
2386                  */
2387                 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2388                 if (ep->is_in) {
2389                         scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2390                                 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2391                                 | (1 << DATA_IN_TOKEN_INTERRUPT);
2392                         stop_out_naking (ep);
2393                 } else
2394                         scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2395                                 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2396                                 | (1 << DATA_IN_TOKEN_INTERRUPT);
2397                 writel (scratch, &dev->epregs [0].ep_irqenb);
2398
2399                 /* we made the hardware handle most lowlevel requests;
2400                  * everything else goes uplevel to the gadget code.
2401                  */
2402                 switch (u.r.bRequest) {
2403                 case USB_REQ_GET_STATUS: {
2404                         struct net2280_ep       *e;
2405                         u16                     status;
2406
2407                         /* hw handles device and interface status */
2408                         if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2409                                 goto delegate;
2410                         if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0
2411                                         || u.r.wLength > 2)
2412                                 goto do_stall;
2413
2414                         if (readl (&e->regs->ep_rsp)
2415                                         & (1 << SET_ENDPOINT_HALT))
2416                                 status = __constant_cpu_to_le16 (1);
2417                         else
2418                                 status = __constant_cpu_to_le16 (0);
2419
2420                         /* don't bother with a request object! */
2421                         writel (0, &dev->epregs [0].ep_irqenb);
2422                         set_fifo_bytecount (ep, u.r.wLength);
2423                         writel (status, &dev->epregs [0].ep_data);
2424                         allow_status (ep);
2425                         VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2426                         goto next_endpoints;
2427                         }
2428                         break;
2429                 case USB_REQ_CLEAR_FEATURE: {
2430                         struct net2280_ep       *e;
2431
2432                         /* hw handles device features */
2433                         if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2434                                 goto delegate;
2435                         if (u.r.wValue != USB_ENDPOINT_HALT
2436                                         || u.r.wLength != 0)
2437                                 goto do_stall;
2438                         if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2439                                 goto do_stall;
2440                         clear_halt (e);
2441                         allow_status (ep);
2442                         VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2443                         goto next_endpoints;
2444                         }
2445                         break;
2446                 case USB_REQ_SET_FEATURE: {
2447                         struct net2280_ep       *e;
2448
2449                         /* hw handles device features */
2450                         if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2451                                 goto delegate;
2452                         if (u.r.wValue != USB_ENDPOINT_HALT
2453                                         || u.r.wLength != 0)
2454                                 goto do_stall;
2455                         if ((e = get_ep_by_addr (dev, u.r.wIndex)) == 0)
2456                                 goto do_stall;
2457                         set_halt (e);
2458                         allow_status (ep);
2459                         VDEBUG (dev, "%s set halt\n", ep->ep.name);
2460                         goto next_endpoints;
2461                         }
2462                         break;
2463                 default:
2464 delegate:
2465                         VDEBUG (dev, "setup %02x.%02x v%04x i%04x "
2466                                 "ep_cfg %08x\n",
2467                                 u.r.bRequestType, u.r.bRequest,
2468                                 u.r.wValue, u.r.wIndex,
2469                                 readl (&ep->regs->ep_cfg));
2470                         spin_unlock (&dev->lock);
2471                         tmp = dev->driver->setup (&dev->gadget, &u.r);
2472                         spin_lock (&dev->lock);
2473                 }
2474
2475                 /* stall ep0 on error */
2476                 if (tmp < 0) {
2477 do_stall:
2478                         VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2479                                         u.r.bRequestType, u.r.bRequest, tmp);
2480                         dev->protocol_stall = 1;
2481                 }
2482
2483                 /* some in/out token irq should follow; maybe stall then.
2484                  * driver must queue a request (even zlp) or halt ep0
2485                  * before the host times out.
2486                  */
2487         }
2488
2489 next_endpoints:
2490         /* endpoint data irq ? */
2491         scratch = stat & 0x7f;
2492         stat &= ~0x7f;
2493         for (num = 0; scratch; num++) {
2494                 u32             t;
2495
2496                 /* do this endpoint's FIFO and queue need tending? */
2497                 t = 1 << num;
2498                 if ((scratch & t) == 0)
2499                         continue;
2500                 scratch ^= t;
2501
2502                 ep = &dev->ep [num];
2503                 handle_ep_small (ep);
2504         }
2505
2506         if (stat)
2507                 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2508 }
2509
2510 #define DMA_INTERRUPTS ( \
2511                   (1 << DMA_D_INTERRUPT) \
2512                 | (1 << DMA_C_INTERRUPT) \
2513                 | (1 << DMA_B_INTERRUPT) \
2514                 | (1 << DMA_A_INTERRUPT))
2515 #define PCI_ERROR_INTERRUPTS ( \
2516                   (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2517                 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2518                 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2519
2520 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2521 {
2522         struct net2280_ep       *ep;
2523         u32                     tmp, num, mask, scratch;
2524
2525         /* after disconnect there's nothing else to do! */
2526         tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2527         mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2528
2529         /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2530          * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2531          * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT 
2532          * only indicates a change in the reset state).
2533          */
2534         if (stat & tmp) {
2535                 writel (tmp, &dev->regs->irqstat1);
2536                 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) && 
2537                                 ((readl (&dev->usb->usbstat) & mask) == 0))
2538                                 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0) 
2539                             ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2540                         DEBUG (dev, "disconnect %s\n",
2541                                         dev->driver->driver.name);
2542                         stop_activity (dev, dev->driver);
2543                         ep0_start (dev);
2544                         return;
2545                 }
2546                 stat &= ~tmp;
2547
2548                 /* vBUS can bounce ... one of many reasons to ignore the
2549                  * notion of hotplug events on bus connect/disconnect!
2550                  */
2551                 if (!stat)
2552                         return;
2553         }
2554
2555         /* NOTE: chip stays in PCI D0 state for now, but it could
2556          * enter D1 to save more power
2557          */
2558         tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2559         if (stat & tmp) {
2560                 writel (tmp, &dev->regs->irqstat1);
2561                 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2562                         if (dev->driver->suspend)
2563                                 dev->driver->suspend (&dev->gadget);
2564                 } else {
2565                         if (dev->driver->resume)
2566                                 dev->driver->resume (&dev->gadget);
2567                         /* at high speed, note erratum 0133 */
2568                 }
2569                 stat &= ~tmp;
2570         }
2571
2572         /* clear any other status/irqs */
2573         if (stat)
2574                 writel (stat, &dev->regs->irqstat1);
2575
2576         /* some status we can just ignore */
2577         stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2578                         | (1 << SUSPEND_REQUEST_INTERRUPT)
2579                         | (1 << RESUME_INTERRUPT)
2580                         | (1 << SOF_INTERRUPT));
2581         if (!stat)
2582                 return;
2583         // DEBUG (dev, "irqstat1 %08x\n", stat);
2584
2585         /* DMA status, for ep-{a,b,c,d} */
2586         scratch = stat & DMA_INTERRUPTS;
2587         stat &= ~DMA_INTERRUPTS;
2588         scratch >>= 9;
2589         for (num = 0; scratch; num++) {
2590                 struct net2280_dma_regs __iomem *dma;
2591
2592                 tmp = 1 << num;
2593                 if ((tmp & scratch) == 0)
2594                         continue;
2595                 scratch ^= tmp;
2596
2597                 ep = &dev->ep [num + 1];
2598                 dma = ep->dma;
2599
2600                 if (!dma)
2601                         continue;
2602
2603                 /* clear ep's dma status */
2604                 tmp = readl (&dma->dmastat);
2605                 writel (tmp, &dma->dmastat);
2606
2607                 /* chaining should stop on abort, short OUT from fifo,
2608                  * or (stat0 codepath) short OUT transfer.
2609                  */
2610                 if (!use_dma_chaining) {
2611                         if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2612                                         == 0) {
2613                                 DEBUG (ep->dev, "%s no xact done? %08x\n",
2614                                         ep->ep.name, tmp);
2615                                 continue;
2616                         }
2617                         stop_dma (ep->dma);
2618                 }
2619
2620                 /* OUT transfers terminate when the data from the
2621                  * host is in our memory.  Process whatever's done.
2622                  * On this path, we know transfer's last packet wasn't
2623                  * less than req->length. NAK_OUT_PACKETS may be set,
2624                  * or the FIFO may already be holding new packets.
2625                  *
2626                  * IN transfers can linger in the FIFO for a very
2627                  * long time ... we ignore that for now, accounting
2628                  * precisely (like PIO does) needs per-packet irqs
2629                  */
2630                 scan_dma_completions (ep);
2631
2632                 /* disable dma on inactive queues; else maybe restart */
2633                 if (list_empty (&ep->queue)) {
2634                         if (use_dma_chaining)
2635                                 stop_dma (ep->dma);
2636                 } else {
2637                         tmp = readl (&dma->dmactl);
2638                         if (!use_dma_chaining
2639                                         || (tmp & (1 << DMA_ENABLE)) == 0)
2640                                 restart_dma (ep);
2641                         else if (ep->is_in && use_dma_chaining) {
2642                                 struct net2280_request  *req;
2643                                 u32                     dmacount;
2644
2645                                 /* the descriptor at the head of the chain
2646                                  * may still have VALID_BIT clear; that's
2647                                  * used to trigger changing DMA_FIFO_VALIDATE
2648                                  * (affects automagic zlp writes).
2649                                  */
2650                                 req = list_entry (ep->queue.next,
2651                                                 struct net2280_request, queue);
2652                                 dmacount = req->td->dmacount;
2653                                 dmacount &= __constant_cpu_to_le32 (
2654                                                 (1 << VALID_BIT)
2655                                                 | DMA_BYTE_COUNT_MASK);
2656                                 if (dmacount && (dmacount & valid_bit) == 0)
2657                                         restart_dma (ep);
2658                         }
2659                 }
2660                 ep->irqs++;
2661         }
2662
2663         /* NOTE:  there are other PCI errors we might usefully notice.
2664          * if they appear very often, here's where to try recovering.
2665          */
2666         if (stat & PCI_ERROR_INTERRUPTS) {
2667                 ERROR (dev, "pci dma error; stat %08x\n", stat);
2668                 stat &= ~PCI_ERROR_INTERRUPTS;
2669                 /* these are fatal errors, but "maybe" they won't
2670                  * happen again ...
2671                  */
2672                 stop_activity (dev, dev->driver);
2673                 ep0_start (dev);
2674                 stat = 0;
2675         }
2676
2677         if (stat)
2678                 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2679 }
2680
2681 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2682 {
2683         struct net2280          *dev = _dev;
2684
2685         spin_lock (&dev->lock);
2686
2687         /* handle disconnect, dma, and more */
2688         handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2689
2690         /* control requests and PIO */
2691         handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2692
2693         spin_unlock (&dev->lock);
2694
2695         return IRQ_HANDLED;
2696 }
2697
2698 /*-------------------------------------------------------------------------*/
2699
2700 static void gadget_release (struct device *_dev)
2701 {
2702         struct net2280  *dev = dev_get_drvdata (_dev);
2703
2704         kfree (dev);
2705 }
2706
2707 /* tear down the binding between this driver and the pci device */
2708
2709 static void net2280_remove (struct pci_dev *pdev)
2710 {
2711         struct net2280          *dev = pci_get_drvdata (pdev);
2712
2713         /* start with the driver above us */
2714         if (dev->driver) {
2715                 /* should have been done already by driver model core */
2716                 WARN (dev, "pci remove, driver '%s' is still registered\n",
2717                                 dev->driver->driver.name);
2718                 usb_gadget_unregister_driver (dev->driver);
2719         }
2720
2721         /* then clean up the resources we allocated during probe() */
2722         net2280_led_shutdown (dev);
2723         if (dev->requests) {
2724                 int             i;
2725                 for (i = 1; i < 5; i++) {
2726                         if (!dev->ep [i].dummy)
2727                                 continue;
2728                         pci_pool_free (dev->requests, dev->ep [i].dummy,
2729                                         dev->ep [i].td_dma);
2730                 }
2731                 pci_pool_destroy (dev->requests);
2732         }
2733         if (dev->got_irq)
2734                 free_irq (pdev->irq, dev);
2735         if (dev->regs)
2736                 iounmap (dev->regs);
2737         if (dev->region)
2738                 release_mem_region (pci_resource_start (pdev, 0),
2739                                 pci_resource_len (pdev, 0));
2740         if (dev->enabled)
2741                 pci_disable_device (pdev);
2742         device_unregister (&dev->gadget.dev);
2743         device_remove_file (&pdev->dev, &dev_attr_registers);
2744         pci_set_drvdata (pdev, NULL);
2745
2746         INFO (dev, "unbind\n");
2747
2748         the_controller = NULL;
2749 }
2750
2751 /* wrap this driver around the specified device, but
2752  * don't respond over USB until a gadget driver binds to us.
2753  */
2754
2755 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2756 {
2757         struct net2280          *dev;
2758         unsigned long           resource, len;
2759         void                    __iomem *base = NULL;
2760         int                     retval, i;
2761         char                    buf [8], *bufp;
2762
2763         /* if you want to support more than one controller in a system,
2764          * usb_gadget_driver_{register,unregister}() must change.
2765          */
2766         if (the_controller) {
2767                 dev_warn (&pdev->dev, "ignoring\n");
2768                 return -EBUSY;
2769         }
2770
2771         /* alloc, and start init */
2772         dev = kmalloc (sizeof *dev, SLAB_KERNEL);
2773         if (dev == NULL){
2774                 retval = -ENOMEM;
2775                 goto done;
2776         }
2777
2778         memset (dev, 0, sizeof *dev);
2779         spin_lock_init (&dev->lock);
2780         dev->pdev = pdev;
2781         dev->gadget.ops = &net2280_ops;
2782         dev->gadget.is_dualspeed = 1;
2783
2784         /* the "gadget" abstracts/virtualizes the controller */
2785         strcpy (dev->gadget.dev.bus_id, "gadget");
2786         dev->gadget.dev.parent = &pdev->dev;
2787         dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2788         dev->gadget.dev.release = gadget_release;
2789         dev->gadget.name = driver_name;
2790
2791         /* now all the pci goodies ... */
2792         if (pci_enable_device (pdev) < 0) {
2793                 retval = -ENODEV;
2794                 goto done;
2795         }
2796         dev->enabled = 1;
2797
2798         /* BAR 0 holds all the registers
2799          * BAR 1 is 8051 memory; unused here (note erratum 0103)
2800          * BAR 2 is fifo memory; unused here
2801          */
2802         resource = pci_resource_start (pdev, 0);
2803         len = pci_resource_len (pdev, 0);
2804         if (!request_mem_region (resource, len, driver_name)) {
2805                 DEBUG (dev, "controller already in use\n");
2806                 retval = -EBUSY;
2807                 goto done;
2808         }
2809         dev->region = 1;
2810
2811         base = ioremap_nocache (resource, len);
2812         if (base == NULL) {
2813                 DEBUG (dev, "can't map memory\n");
2814                 retval = -EFAULT;
2815                 goto done;
2816         }
2817         dev->regs = (struct net2280_regs __iomem *) base;
2818         dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2819         dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2820         dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2821         dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2822         dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2823
2824         /* put into initial config, link up all endpoints */
2825         writel (0, &dev->usb->usbctl);
2826         usb_reset (dev);
2827         usb_reinit (dev);
2828
2829         /* irq setup after old hardware is cleaned up */
2830         if (!pdev->irq) {
2831                 ERROR (dev, "No IRQ.  Check PCI setup!\n");
2832                 retval = -ENODEV;
2833                 goto done;
2834         }
2835 #ifndef __sparc__
2836         scnprintf (buf, sizeof buf, "%d", pdev->irq);
2837         bufp = buf;
2838 #else
2839         bufp = __irq_itoa(pdev->irq);
2840 #endif
2841         if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2842                         != 0) {
2843                 ERROR (dev, "request interrupt %s failed\n", bufp);
2844                 retval = -EBUSY;
2845                 goto done;
2846         }
2847         dev->got_irq = 1;
2848
2849         /* DMA setup */
2850         /* NOTE:  we know only the 32 LSBs of dma addresses may be nonzero */
2851         dev->requests = pci_pool_create ("requests", pdev,
2852                 sizeof (struct net2280_dma),
2853                 0 /* no alignment requirements */,
2854                 0 /* or page-crossing issues */);
2855         if (!dev->requests) {
2856                 DEBUG (dev, "can't get request pool\n");
2857                 retval = -ENOMEM;
2858                 goto done;
2859         }
2860         for (i = 1; i < 5; i++) {
2861                 struct net2280_dma      *td;
2862
2863                 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2864                                 &dev->ep [i].td_dma);
2865                 if (!td) {
2866                         DEBUG (dev, "can't get dummy %d\n", i);
2867                         retval = -ENOMEM;
2868                         goto done;
2869                 }
2870                 td->dmacount = 0;       /* not VALID */
2871                 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2872                 td->dmadesc = td->dmaaddr;
2873                 dev->ep [i].dummy = td;
2874         }
2875
2876         /* enable lower-overhead pci memory bursts during DMA */
2877         writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2878                         // 256 write retries may not be enough...
2879                         // | (1 << PCI_RETRY_ABORT_ENABLE)
2880                         | (1 << DMA_READ_MULTIPLE_ENABLE)
2881                         | (1 << DMA_READ_LINE_ENABLE)
2882                         , &dev->pci->pcimstctl);
2883         /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2884         pci_set_master (pdev);
2885         pci_set_mwi (pdev);
2886
2887         /* ... also flushes any posted pci writes */
2888         dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2889
2890         /* done */
2891         pci_set_drvdata (pdev, dev);
2892         INFO (dev, "%s\n", driver_desc);
2893         INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2894                         bufp, base, dev->chiprev);
2895         INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2896                         use_dma
2897                                 ? (use_dma_chaining ? "chaining" : "enabled")
2898                                 : "disabled");
2899         the_controller = dev;
2900
2901         device_register (&dev->gadget.dev);
2902         device_create_file (&pdev->dev, &dev_attr_registers);
2903
2904         return 0;
2905
2906 done:
2907         if (dev)
2908                 net2280_remove (pdev);
2909         return retval;
2910 }
2911
2912
2913 /*-------------------------------------------------------------------------*/
2914
2915 static struct pci_device_id pci_ids [] = { {
2916         .class =        ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2917         .class_mask =   ~0,
2918         .vendor =       0x17cc,
2919         .device =       0x2280,
2920         .subvendor =    PCI_ANY_ID,
2921         .subdevice =    PCI_ANY_ID,
2922
2923 }, { /* end: all zeroes */ }
2924 };
2925 MODULE_DEVICE_TABLE (pci, pci_ids);
2926
2927 /* pci driver glue; this is a "new style" PCI driver module */
2928 static struct pci_driver net2280_pci_driver = {
2929         .name =         (char *) driver_name,
2930         .id_table =     pci_ids,
2931
2932         .probe =        net2280_probe,
2933         .remove =       net2280_remove,
2934
2935         /* FIXME add power management support */
2936 };
2937
2938 MODULE_DESCRIPTION (DRIVER_DESC);
2939 MODULE_AUTHOR ("David Brownell");
2940 MODULE_LICENSE ("GPL");
2941
2942 static int __init init (void)
2943 {
2944         if (!use_dma)
2945                 use_dma_chaining = 0;
2946         return pci_register_driver (&net2280_pci_driver);
2947 }
2948 module_init (init);
2949
2950 static void __exit cleanup (void)
2951 {
2952         pci_unregister_driver (&net2280_pci_driver);
2953 }
2954 module_exit (cleanup);