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