Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / usb / gadget / net2280.c
blob0b92934939577bffd17cd9939c3f93976fd19584
1 /*
2 * Driver for the PLX NET2280 USB device controller.
3 * Specs and errata are available from <http://www.plxtech.com>.
5 * PLX Technology Inc. (formerly NetChip Technology) supported the
6 * development of this driver.
9 * CODE STATUS HIGHLIGHTS
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.
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.
21 * Note that almost all the errata workarounds here are only needed for
22 * rev1 chips. Rev1a silicon (0110) fixes almost all of them.
26 * Copyright (C) 2003 David Brownell
27 * Copyright (C) 2003-2005 PLX Technology, Inc.
29 * Modified Seth Levy 2005 PLX Technology, Inc. to provide compatibility with 2282 chip
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.
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.
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
46 #undef DEBUG /* messages on error and most fault paths */
47 #undef VERBOSE /* extra debug messages (success too) */
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>
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>
76 #define DRIVER_DESC "PLX NET228x USB Peripheral Controller"
77 #define DRIVER_VERSION "2005 Sept 27"
79 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
80 #define EP_DONTUSE 13 /* nonzero */
82 #define USE_RDK_LEDS /* GPIO pins control three LEDs */
85 static const char driver_name [] = "net2280";
86 static const char driver_desc [] = DRIVER_DESC;
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",
95 /* use_dma -- general goodness, fewer interrupts, less cpu load (vs PIO)
96 * use_dma_chaining -- dma descriptor queueing gives even more irq reduction
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.
103 static int use_dma = 1;
104 static int use_dma_chaining = 0;
106 /* "modprobe net2280 use_dma=n" etc */
107 module_param (use_dma, bool, S_IRUGO);
108 module_param (use_dma_chaining, bool, S_IRUGO);
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
115 static ushort fifo_mode = 0;
117 /* "modprobe net2280 fifo_mode=1" etc */
118 module_param (fifo_mode, ushort, 0644);
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
125 static int enable_suspend = 0;
127 /* "modprobe net2280 enable_suspend=1" etc */
128 module_param (enable_suspend, bool, S_IRUGO);
131 #define DIR_STRING(bAddress) (((bAddress) & USB_DIR_IN) ? "in" : "out")
133 #if defined(CONFIG_USB_GADGET_DEBUG_FILES) || defined (DEBUG)
134 static char *type_string (u8 bmAttributes)
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";
141 return "control";
143 #endif
145 #include "net2280.h"
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)
150 /*-------------------------------------------------------------------------*/
152 static int
153 net2280_enable (struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
155 struct net2280 *dev;
156 struct net2280_ep *ep;
157 u32 max, tmp;
158 unsigned long flags;
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;
168 /* erratum 0119 workaround ties up an endpoint number */
169 if ((desc->bEndpointAddress & 0x0f) == EP_DONTUSE)
170 return -EDOM;
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;
177 spin_lock_irqsave (&dev->lock, flags);
178 _ep->maxpacket = max & 0x7ff;
179 ep->desc = desc;
181 /* ep_reset() has already been called */
182 ep->stopped = 0;
183 ep->out_overflow = 0;
185 /* set speed-dependent max packet; may kick in high bandwidth */
186 set_idx_reg (dev->regs, REG_EP_MAXPKT (dev, ep->num), max);
188 /* FIFO lines can't go to different packets. PIO is ok, so
189 * use it instead of troublesome (non-bulk) multi-packet DMA.
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;
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;
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 ();
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);
234 writel (tmp, &ep->regs->ep_cfg);
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);
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);
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
255 if ((desc->bEndpointAddress & USB_DIR_IN) == 0) {
256 tmp = (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT_ENABLE);
257 writel (tmp, &ep->regs->ep_irqenb);
259 tmp = (1 << ep->num) | readl (&dev->regs->pciirqenb0);
260 writel (tmp, &dev->regs->pciirqenb0);
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);
270 /* pci writes may still be posted */
271 spin_unlock_irqrestore (&dev->lock, flags);
272 return 0;
275 static int handshake (u32 __iomem *ptr, u32 mask, u32 done, int usec)
277 u32 result;
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;
292 static struct usb_ep_ops net2280_ep_ops;
294 static void ep_reset (struct net2280_regs __iomem *regs, struct net2280_ep *ep)
296 u32 tmp;
298 ep->desc = NULL;
299 INIT_LIST_HEAD (&ep->queue);
301 ep->ep.maxpacket = ~0;
302 ep->ep.ops = &net2280_ep_ops;
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);
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);
320 writel (0, &ep->regs->ep_irqenb);
322 /* init to our chosen defaults, notably so that we NAK OUT
323 * packets until the driver queues a read (+note erratum 0112)
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);
338 if (ep->num != 0) {
339 tmp |= (1 << CLEAR_ENDPOINT_TOGGLE)
340 | (1 << CLEAR_ENDPOINT_HALT);
342 writel (tmp, &ep->regs->ep_rsp);
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;
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);
366 /* fifo size is handled separately */
369 static void nuke (struct net2280_ep *);
371 static int net2280_disable (struct usb_ep *_ep)
373 struct net2280_ep *ep;
374 unsigned long flags;
376 ep = container_of (_ep, struct net2280_ep, ep);
377 if (!_ep || !ep->desc || _ep->name == ep0name)
378 return -EINVAL;
380 spin_lock_irqsave (&ep->dev->lock, flags);
381 nuke (ep);
382 ep_reset (ep->dev->regs, ep);
384 VDEBUG (ep->dev, "disabled %s %s\n",
385 ep->dma ? "dma" : "pio", _ep->name);
387 /* synch memory views with the device */
388 (void) readl (&ep->regs->ep_cfg);
390 if (use_dma && !ep->dma && ep->num >= 1 && ep->num <= 4)
391 ep->dma = &ep->dev->dma [ep->num - 1];
393 spin_unlock_irqrestore (&ep->dev->lock, flags);
394 return 0;
397 /*-------------------------------------------------------------------------*/
399 static struct usb_request *
400 net2280_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
402 struct net2280_ep *ep;
403 struct net2280_request *req;
405 if (!_ep)
406 return NULL;
407 ep = container_of (_ep, struct net2280_ep, ep);
409 req = kzalloc(sizeof(*req), gfp_flags);
410 if (!req)
411 return NULL;
413 req->req.dma = DMA_ADDR_INVALID;
414 INIT_LIST_HEAD (&req->queue);
416 /* this dma descriptor may be swapped with the previous dummy */
417 if (ep->dma) {
418 struct net2280_dma *td;
420 td = pci_pool_alloc (ep->dev->requests, gfp_flags,
421 &req->td_dma);
422 if (!td) {
423 kfree (req);
424 return NULL;
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;
431 return &req->req;
434 static void
435 net2280_free_request (struct usb_ep *_ep, struct usb_request *_req)
437 struct net2280_ep *ep;
438 struct net2280_request *req;
440 ep = container_of (_ep, struct net2280_ep, ep);
441 if (!_ep || !_req)
442 return;
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);
451 /*-------------------------------------------------------------------------*/
453 #undef USE_KMALLOC
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.)
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().
465 #if defined(CONFIG_X86)
466 #define USE_KMALLOC
468 #elif defined(CONFIG_PPC) && !defined(CONFIG_NOT_COHERENT_CACHE)
469 #define USE_KMALLOC
471 #elif defined(CONFIG_MIPS) && !defined(CONFIG_DMA_NONCOHERENT)
472 #define USE_KMALLOC
474 /* FIXME there are other cases, including an x86-64 one ... */
475 #endif
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.
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
489 void *retval;
490 struct net2280_ep *ep;
492 ep = container_of (_ep, struct net2280_ep, ep);
493 if (!_ep)
494 return NULL;
495 *dma = DMA_ADDR_INVALID;
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.
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;
515 static void
516 net2280_free_buffer (
517 struct usb_ep *_ep,
518 void *buf,
519 dma_addr_t dma,
520 unsigned bytes
522 /* free memory into the right allocator */
523 #ifndef USE_KMALLOC
524 if (dma != DMA_ADDR_INVALID) {
525 struct net2280_ep *ep;
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);
536 /*-------------------------------------------------------------------------*/
538 /* load a packet into the fifo we use for usb IN transfers.
539 * works for all endpoints.
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.
545 static void
546 write_fifo (struct net2280_ep *ep, struct usb_request *req)
548 struct net2280_ep_regs __iomem *regs = ep->regs;
549 u8 *buf;
550 u32 tmp;
551 unsigned count, total;
553 /* INVARIANT: fifo is currently empty. (testable) */
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;
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;
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.
578 tmp = get_unaligned ((u32 *)buf);
579 cpu_to_le32s (&tmp);
580 writel (tmp, &regs->ep_data);
581 buf += 4;
582 count -= 4;
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.
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);
596 /* pci writes may still be posted */
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.
603 * NOTE: also used in cases where that erratum doesn't apply:
604 * where the host wrote "too much" data to us.
606 static void out_flush (struct net2280_ep *ep)
608 u32 __iomem *statp;
609 u32 tmp;
611 ASSERT_OUT_NAKING (ep);
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;
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 */
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.
636 * for ep-a..ep-d this will read multiple packets out when they
637 * have been accepted.
639 static int
640 read_fifo (struct net2280_ep *ep, struct net2280_request *req)
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;
647 /* erratum 0106 ... packets coming in during fifo reads might
648 * be incompletely rejected. not all cases have workarounds.
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;
660 /* else: hope we don't see the problem */
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.
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;
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
689 } /* else it's a ZLP, no worries */
690 count = tmp;
692 req->req.actual += count;
694 is_short = (count == 0) || ((count % ep->ep.maxpacket) != 0);
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);
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;
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);
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);
723 return is_short || ((req->req.actual == req->req.length)
724 && !req->req.zero);
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)
731 struct net2280_dma *td = req->td;
732 u32 dmacount = req->req.length;
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.
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);
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);
750 /* td->dmadesc = previously set by caller */
751 td->dmaaddr = cpu_to_le32 (req->req.dma);
753 /* 2280 may be polling VALID_BIT through ep->dma->dmadesc */
754 wmb ();
755 td->dmacount = cpu_to_le32p (&dmacount);
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);
769 static inline void spin_stop_dma (struct net2280_dma_regs __iomem *dma)
771 handshake (&dma->dmactl, (1 << DMA_ENABLE), 0, 50);
774 static inline void stop_dma (struct net2280_dma_regs __iomem *dma)
776 writel (readl (&dma->dmactl) & ~(1 << DMA_ENABLE), &dma->dmactl);
777 spin_stop_dma (dma);
780 static void start_queue (struct net2280_ep *ep, u32 dmactl, u32 td_dma)
782 struct net2280_dma_regs __iomem *dma = ep->dma;
783 unsigned int tmp = (1 << VALID_BIT) | (ep->is_in << DMA_DIRECTION);
785 if (ep->dev->pdev->device != 0x2280)
786 tmp |= (1 << END_OF_CHAIN);
788 writel (tmp, &dma->dmacount);
789 writel (readl (&dma->dmastat), &dma->dmastat);
791 writel (td_dma, &dma->dmadesc);
792 writel (dmactl, &dma->dmactl);
794 /* erratum 0116 workaround part 3: pci arbiter away from net2280 */
795 (void) readl (&ep->dev->pci->pcimstctl);
797 writel ((1 << DMA_START), &dma->dmastat);
799 if (!ep->is_in)
800 stop_out_naking (ep);
803 static void start_dma (struct net2280_ep *ep, struct net2280_request *req)
805 u32 tmp;
806 struct net2280_dma_regs __iomem *dma = ep->dma;
808 /* FIXME can't use DMA for ZLPs */
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);
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);
820 tmp = readl (&ep->regs->ep_avail);
821 if (tmp) {
822 writel (readl (&dma->dmastat), &dma->dmastat);
824 /* transfer all/some fifo data */
825 writel (req->req.dma, &dma->dmaaddr);
826 tmp = min (tmp, req->req.length);
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;
835 writel ((1 << DMA_ENABLE), &dma->dmactl);
836 writel ((1 << DMA_START), &dma->dmastat);
837 return;
841 tmp = dmactl_default;
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.
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;
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);
860 if (!use_dma_chaining)
861 req->td->dmacount |= __constant_cpu_to_le32 (1 << END_OF_CHAIN);
863 start_queue (ep, tmp, req->td_dma);
866 static inline void
867 queue_dma (struct net2280_ep *ep, struct net2280_request *req, int valid)
869 struct net2280_dma *end;
870 dma_addr_t tmp;
872 /* swap new dummy for old, link; fill and maybe activate */
873 end = ep->dummy;
874 ep->dummy = req->td;
875 req->td = end;
877 tmp = ep->td_dma;
878 ep->td_dma = req->td_dma;
879 req->td_dma = tmp;
881 end->dmadesc = cpu_to_le32 (ep->td_dma);
883 fill_dma_desc (ep, req, valid);
886 static void
887 done (struct net2280_ep *ep, struct net2280_request *req, int status)
889 struct net2280 *dev;
890 unsigned stopped = ep->stopped;
892 list_del_init (&req->queue);
894 if (req->req.status == -EINPROGRESS)
895 req->req.status = status;
896 else
897 status = req->req.status;
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;
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);
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;
920 /*-------------------------------------------------------------------------*/
922 static int
923 net2280_queue (struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
925 struct net2280_request *req;
926 struct net2280_ep *ep;
927 struct net2280 *dev;
928 unsigned long flags;
930 /* we always require a cpu-view buffer, so that we can
931 * always use pio (as fallback or whatever).
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;
946 /* FIXME implement PIO fallback for ZLPs with DMA */
947 if (ep->dma && _req->length == 0)
948 return -EOPNOTSUPP;
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;
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
962 spin_lock_irqsave (&dev->lock, flags);
964 _req->status = -EINPROGRESS;
965 _req->actual = 0;
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;
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;
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)
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);
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);
1013 } else if (ep->dma) {
1014 int valid = 1;
1016 if (ep->is_in) {
1017 int expect;
1019 /* preventing magic zlps is per-engine state, not
1020 * per-transfer; irq logic must recover hiccups.
1022 expect = likely (req->req.zero
1023 || (req->req.length % ep->ep.maxpacket) != 0);
1024 if (expect != ep->in_fifo_validate)
1025 valid = 0;
1027 queue_dma (ep, req, valid);
1029 } /* else the irq handler advances the queue. */
1031 if (req)
1032 list_add_tail (&req->queue, &ep->queue);
1033 done:
1034 spin_unlock_irqrestore (&dev->lock, flags);
1036 /* pci writes may still be posted */
1037 return 0;
1040 static inline void
1041 dma_done (
1042 struct net2280_ep *ep,
1043 struct net2280_request *req,
1044 u32 dmacount,
1045 int status
1048 req->req.actual = req->req.length - (DMA_BYTE_COUNT_MASK & dmacount);
1049 done (ep, req, status);
1052 static void restart_dma (struct net2280_ep *ep);
1054 static void scan_dma_completions (struct net2280_ep *ep)
1056 /* only look at descriptors that were "naturally" retired,
1057 * so fifo and list head state won't matter
1059 while (!list_empty (&ep->queue)) {
1060 struct net2280_request *req;
1061 u32 tmp;
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;
1072 /* SHORT_PACKET_TRANSFERRED_INTERRUPT handles "usb-short"
1073 * cases where DMA must be aborted; this code handles
1074 * all non-abort DMA completions.
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);
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.
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;
1105 dma_done (ep, req, tmp, 0);
1109 static void restart_dma (struct net2280_ep *ep)
1111 struct net2280_request *req;
1112 u32 dmactl = dmactl_default;
1114 if (ep->stopped)
1115 return;
1116 req = list_entry (ep->queue.next, struct net2280_request, queue);
1118 if (!use_dma_chaining) {
1119 start_dma (ep, req);
1120 return;
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.
1129 if (ep->is_in && !req->valid) {
1130 struct net2280_request *entry, *prev = NULL;
1131 int reqmode, done = 0;
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;
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;
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;
1169 writel (0, &ep->dma->dmactl);
1170 start_queue (ep, dmactl, req->td_dma);
1173 static void abort_dma (struct net2280_ep *ep)
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);
1185 /* dequeue ALL requests */
1186 static void nuke (struct net2280_ep *ep)
1188 struct net2280_request *req;
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);
1202 /* dequeue JUST ONE request */
1203 static int net2280_dequeue (struct usb_ep *_ep, struct usb_request *_req)
1205 struct net2280_ep *ep;
1206 struct net2280_request *req;
1207 unsigned long flags;
1208 u32 dmactl;
1209 int stopped;
1211 ep = container_of (_ep, struct net2280_ep, ep);
1212 if (!_ep || (!ep->desc && ep->num != 0) || !_req)
1213 return -EINVAL;
1215 spin_lock_irqsave (&ep->dev->lock, flags);
1216 stopped = ep->stopped;
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);
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;
1233 if (&req->req != _req) {
1234 spin_unlock_irqrestore (&ep->dev->lock, flags);
1235 return -EINVAL;
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);
1251 } else {
1252 DEBUG (ep->dev, "unlink (%s) pio\n", _ep->name);
1253 done (ep, req, -ECONNRESET);
1255 req = NULL;
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;
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;
1277 if (req)
1278 done (ep, req, -ECONNRESET);
1279 ep->stopped = stopped;
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));
1295 spin_unlock_irqrestore (&ep->dev->lock, flags);
1296 return 0;
1299 /*-------------------------------------------------------------------------*/
1301 static int net2280_fifo_status (struct usb_ep *_ep);
1303 static int
1304 net2280_set_halt (struct usb_ep *_ep, int value)
1306 struct net2280_ep *ep;
1307 unsigned long flags;
1308 int retval = 0;
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;
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);
1337 spin_unlock_irqrestore (&ep->dev->lock, flags);
1339 return retval;
1342 static int
1343 net2280_fifo_status (struct usb_ep *_ep)
1345 struct net2280_ep *ep;
1346 u32 avail;
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;
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;
1362 static void
1363 net2280_fifo_flush (struct usb_ep *_ep)
1365 struct net2280_ep *ep;
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;
1373 writel ((1 << FIFO_FLUSH), &ep->regs->ep_stat);
1374 (void) readl (&ep->regs->ep_rsp);
1377 static struct usb_ep_ops net2280_ep_ops = {
1378 .enable = net2280_enable,
1379 .disable = net2280_disable,
1381 .alloc_request = net2280_alloc_request,
1382 .free_request = net2280_free_request,
1384 .alloc_buffer = net2280_alloc_buffer,
1385 .free_buffer = net2280_free_buffer,
1387 .queue = net2280_queue,
1388 .dequeue = net2280_dequeue,
1390 .set_halt = net2280_set_halt,
1391 .fifo_status = net2280_fifo_status,
1392 .fifo_flush = net2280_fifo_flush,
1395 /*-------------------------------------------------------------------------*/
1397 static int net2280_get_frame (struct usb_gadget *_gadget)
1399 struct net2280 *dev;
1400 unsigned long flags;
1401 u16 retval;
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;
1412 static int net2280_wakeup (struct usb_gadget *_gadget)
1414 struct net2280 *dev;
1415 u32 tmp;
1416 unsigned long flags;
1418 if (!_gadget)
1419 return 0;
1420 dev = container_of (_gadget, struct net2280, gadget);
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);
1428 /* pci writes may still be posted */
1429 return 0;
1432 static int net2280_set_selfpowered (struct usb_gadget *_gadget, int value)
1434 struct net2280 *dev;
1435 u32 tmp;
1436 unsigned long flags;
1438 if (!_gadget)
1439 return 0;
1440 dev = container_of (_gadget, struct net2280, gadget);
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);
1451 return 0;
1454 static int net2280_pullup(struct usb_gadget *_gadget, int is_on)
1456 struct net2280 *dev;
1457 u32 tmp;
1458 unsigned long flags;
1460 if (!_gadget)
1461 return -ENODEV;
1462 dev = container_of (_gadget, struct net2280, gadget);
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);
1474 return 0;
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,
1484 /*-------------------------------------------------------------------------*/
1486 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
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.
1493 /* "function" sysfs attribute */
1494 static ssize_t
1495 show_function (struct device *_dev, struct device_attribute *attr, char *buf)
1497 struct net2280 *dev = dev_get_drvdata (_dev);
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);
1505 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1507 static ssize_t
1508 show_registers (struct device *_dev, struct device_attribute *attr, char *buf)
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;
1518 dev = dev_get_drvdata (_dev);
1519 next = buf;
1520 size = PAGE_SIZE;
1521 spin_lock_irqsave (&dev->lock, flags);
1523 if (dev->driver)
1524 s = dev->driver->driver.name;
1525 else
1526 s = "(none)";
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),
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;
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;
1569 /* PCI Master Control Registers */
1571 /* DMA Control Registers */
1573 /* Configurable EP Control Registers */
1574 for (i = 0; i < 7; i++) {
1575 struct net2280_ep *ep;
1577 ep = &dev->ep [i];
1578 if (i && !ep->desc)
1579 continue;
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;
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;
1618 if (!ep->dma)
1619 continue;
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;
1634 /* Indexed Registers */
1635 // none yet
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;
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;
1652 t = scnprintf (next, size, "\n");
1653 size -= t;
1654 next += t;
1656 spin_unlock_irqrestore (&dev->lock, flags);
1658 return PAGE_SIZE - size;
1660 static DEVICE_ATTR (registers, S_IRUGO, show_registers, NULL);
1662 static ssize_t
1663 show_queues (struct device *_dev, struct device_attribute *attr, char *buf)
1665 struct net2280 *dev;
1666 char *next;
1667 unsigned size;
1668 unsigned long flags;
1669 int i;
1671 dev = dev_get_drvdata (_dev);
1672 next = buf;
1673 size = PAGE_SIZE;
1674 spin_lock_irqsave (&dev->lock, flags);
1676 for (i = 0; i < 7; i++) {
1677 struct net2280_ep *ep = &dev->ep [i];
1678 struct net2280_request *req;
1679 int t;
1681 if (i != 0) {
1682 const struct usb_endpoint_descriptor *d;
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
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;
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;
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;
1738 if (ep->dma) {
1739 struct net2280_dma *td;
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;
1756 done:
1757 spin_unlock_irqrestore (&dev->lock, flags);
1758 return PAGE_SIZE - size;
1760 static DEVICE_ATTR (queues, S_IRUGO, show_queues, NULL);
1763 #else
1765 #define device_create_file(a,b) do {} while (0)
1766 #define device_remove_file device_create_file
1768 #endif
1770 /*-------------------------------------------------------------------------*/
1772 /* another driver-specific mode might be a request type doing dma
1773 * to/from another device fifo instead of to/from memory.
1776 static void set_fifo_mode (struct net2280 *dev, int mode)
1778 /* keeping high bits preserves BAR2 */
1779 writel ((0xffff << PCI_BASE2_RANGE) | mode, &dev->regs->fifoctl);
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;
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);
1805 /* just declare this in any driver that really need it */
1806 extern int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode);
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).
1815 * returns zero on success, else negative errno. when this succeeds,
1816 * the contents of gadget->ep_list may have changed.
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).
1824 int net2280_set_fifo_mode (struct usb_gadget *gadget, int mode)
1826 int i;
1827 struct net2280 *dev;
1828 int status = 0;
1829 unsigned long flags;
1831 if (!gadget)
1832 return -ENODEV;
1833 dev = container_of (gadget, struct net2280, gadget);
1835 spin_lock_irqsave (&dev->lock, flags);
1837 for (i = 1; i <= 4; i++)
1838 if (dev->ep [i].desc) {
1839 status = -EINVAL;
1840 break;
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);
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 */
1855 return status;
1857 EXPORT_SYMBOL (net2280_set_fifo_mode);
1859 /*-------------------------------------------------------------------------*/
1861 /* keeping it simple:
1862 * - one bus driver, initted first;
1863 * - one function driver, initted second
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.
1870 static struct net2280 *the_controller;
1872 static void usb_reset (struct net2280 *dev)
1874 u32 tmp;
1876 dev->gadget.speed = USB_SPEED_UNKNOWN;
1877 (void) readl (&dev->usb->usbctl);
1879 net2280_led_init (dev);
1881 /* disable automatic responses, and irqs */
1882 writel (0, &dev->usb->stdrsp);
1883 writel (0, &dev->regs->pciirqenb0);
1884 writel (0, &dev->regs->pciirqenb1);
1886 /* clear old dma and irq state */
1887 for (tmp = 0; tmp < 4; tmp++) {
1888 struct net2280_ep *ep = &dev->ep [tmp + 1];
1890 if (ep->dma)
1891 abort_dma (ep);
1893 writel (~0, &dev->regs->irqstat0),
1894 writel (~(1 << SUSPEND_REQUEST_INTERRUPT), &dev->regs->irqstat1),
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);
1904 /* standard fifo and endpoint allocations */
1905 set_fifo_mode (dev, (fifo_mode <= 2) ? fifo_mode : 0);
1908 static void usb_reinit (struct net2280 *dev)
1910 u32 tmp;
1911 int init_dma;
1913 /* use_dma changes are ignored till next device re-init */
1914 init_dma = use_dma;
1916 /* basic endpoint init */
1917 for (tmp = 0; tmp < 7; tmp++) {
1918 struct net2280_ep *ep = &dev->ep [tmp];
1920 ep->ep.name = ep_name [tmp];
1921 ep->dev = dev;
1922 ep->num = tmp;
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);
1933 dev->ep [0].ep.maxpacket = 64;
1934 dev->ep [5].ep.maxpacket = 64;
1935 dev->ep [6].ep.maxpacket = 64;
1937 dev->gadget.ep0 = &dev->ep [0].ep;
1938 dev->ep [0].stopped = 0;
1939 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1941 /* we want to prevent lowlevel/insecure access from the USB host,
1942 * but erratum 0119 means this enable bit is ignored
1944 for (tmp = 0; tmp < 5; tmp++)
1945 writel (EP_DONTUSE, &dev->dep [tmp].dep_cfg);
1948 static void ep0_start (struct net2280 *dev)
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);
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.
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);
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);
1987 /* don't leave any writes posted */
1988 (void) readl (&dev->usb->usbctl);
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.
1997 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1999 struct net2280 *dev = the_controller;
2000 int retval;
2001 unsigned i;
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"
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;
2018 for (i = 0; i < 7; i++)
2019 dev->ep [i].irqs = 0;
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;
2035 device_create_file (&dev->pdev->dev, &dev_attr_function);
2036 device_create_file (&dev->pdev->dev, &dev_attr_queues);
2038 /* ... then enable host detection and ep0; and we're ready
2039 * for set_configuration as well as eventual disconnect.
2041 net2280_led_active (dev, 1);
2042 ep0_start (dev);
2044 DEBUG (dev, "%s ready, usbctl %08x stdrsp %08x\n",
2045 driver->driver.name,
2046 readl (&dev->usb->usbctl),
2047 readl (&dev->usb->stdrsp));
2049 /* pci writes may still be posted */
2050 return 0;
2052 EXPORT_SYMBOL (usb_gadget_register_driver);
2054 static void
2055 stop_activity (struct net2280 *dev, struct usb_gadget_driver *driver)
2057 int i;
2059 /* don't disconnect if it's not connected */
2060 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
2061 driver = NULL;
2063 /* stop hardware; prevent new request submissions;
2064 * and kill any outstanding requests.
2066 usb_reset (dev);
2067 for (i = 0; i < 7; i++)
2068 nuke (&dev->ep [i]);
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);
2077 usb_reinit (dev);
2080 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2082 struct net2280 *dev = the_controller;
2083 unsigned long flags;
2085 if (!dev)
2086 return -ENODEV;
2087 if (!driver || driver != dev->driver)
2088 return -EINVAL;
2090 spin_lock_irqsave (&dev->lock, flags);
2091 stop_activity (dev, driver);
2092 spin_unlock_irqrestore (&dev->lock, flags);
2094 net2280_pullup (&dev->gadget, 0);
2096 driver->unbind (&dev->gadget);
2097 dev->gadget.dev.driver = NULL;
2098 dev->driver = NULL;
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);
2104 DEBUG (dev, "unregistered driver '%s'\n", driver->driver.name);
2105 return 0;
2107 EXPORT_SYMBOL (usb_gadget_unregister_driver);
2110 /*-------------------------------------------------------------------------*/
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.
2116 static void handle_ep_small (struct net2280_ep *ep)
2118 struct net2280_request *req;
2119 u32 t;
2120 /* 0 error, 1 mid-data, 2 done */
2121 int mode = 1;
2123 if (!list_empty (&ep->queue))
2124 req = list_entry (ep->queue.next,
2125 struct net2280_request, queue);
2126 else
2127 req = NULL;
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);
2142 /* for ep0, monitor token irqs to catch data stage length errors
2143 * and to synchronize on status.
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..
2149 * control requests could be slightly faster without token synch for
2150 * status, but status can jam up that way.
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);
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);
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);
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;
2195 if (unlikely (!req))
2196 return;
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;
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.
2208 ep->stopped = 1;
2209 for (count = 0; ; t = readl (&ep->regs->ep_stat)) {
2211 /* any preceding dma transfers must finish.
2212 * dma handles (M >= N), may empty the queue
2214 scan_dma_completions (ep);
2215 if (unlikely (list_empty (&ep->queue)
2216 || ep->out_overflow)) {
2217 req = NULL;
2218 break;
2220 req = list_entry (ep->queue.next,
2221 struct net2280_request, queue);
2223 /* here either (M < N), a "real" short rx;
2224 * or (M == N) and the queue didn't empty
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;
2234 udelay(1);
2237 /* stop DMA, leave ep NAKing */
2238 writel ((1 << DMA_ABORT), &ep->dma->dmastat);
2239 spin_stop_dma (ep->dma);
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);
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;
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;
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;
2271 /* data packet(s) transmitted (IN) */
2272 } else if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)) {
2273 unsigned len;
2275 len = req->req.length - req->req.actual;
2276 if (len > ep->ep.maxpacket)
2277 len = ep->ep.maxpacket;
2278 req->req.actual += len;
2280 /* if we wrote it all, we're usually done */
2281 if (req->req.actual == req->req.length) {
2282 if (ep->num == 0) {
2283 /* send zlps until the status stage */
2284 } else if (!req->req.zero || len != ep->ep.maxpacket)
2285 mode = 2;
2288 /* there was nothing to do ... */
2289 } else if (mode == 1)
2290 return;
2292 /* done */
2293 if (mode == 2) {
2294 /* stream endpoints often resubmit/unlink in completion */
2295 done (ep, req, 0);
2297 /* maybe advance queue to next request */
2298 if (ep->num == 0) {
2299 /* NOTE: net2280 could let gadget driver start the
2300 * status stage later. since not all controllers let
2301 * them control that, the api doesn't (yet) allow it.
2303 if (!ep->stopped)
2304 allow_status (ep);
2305 req = NULL;
2306 } else {
2307 if (!list_empty (&ep->queue) && !ep->stopped)
2308 req = list_entry (ep->queue.next,
2309 struct net2280_request, queue);
2310 else
2311 req = NULL;
2312 if (req && !ep->is_in)
2313 stop_out_naking (ep);
2317 /* is there a buffer for the next packet?
2318 * for best streaming performance, make sure there is one.
2320 if (req && !ep->stopped) {
2322 /* load IN fifo with next packet (may be zlp) */
2323 if (t & (1 << DATA_PACKET_TRANSMITTED_INTERRUPT))
2324 write_fifo (ep, &req->req);
2328 static struct net2280_ep *
2329 get_ep_by_addr (struct net2280 *dev, u16 wIndex)
2331 struct net2280_ep *ep;
2333 if ((wIndex & USB_ENDPOINT_NUMBER_MASK) == 0)
2334 return &dev->ep [0];
2335 list_for_each_entry (ep, &dev->gadget.ep_list, ep.ep_list) {
2336 u8 bEndpointAddress;
2338 if (!ep->desc)
2339 continue;
2340 bEndpointAddress = ep->desc->bEndpointAddress;
2341 if ((wIndex ^ bEndpointAddress) & USB_DIR_IN)
2342 continue;
2343 if ((wIndex & 0x0f) == (bEndpointAddress & 0x0f))
2344 return ep;
2346 return NULL;
2349 static void handle_stat0_irqs (struct net2280 *dev, u32 stat)
2351 struct net2280_ep *ep;
2352 u32 num, scratch;
2354 /* most of these don't need individual acks */
2355 stat &= ~(1 << INTA_ASSERTED);
2356 if (!stat)
2357 return;
2358 // DEBUG (dev, "irqstat0 %04x\n", stat);
2360 /* starting a control request? */
2361 if (unlikely (stat & (1 << SETUP_PACKET_INTERRUPT))) {
2362 union {
2363 u32 raw [2];
2364 struct usb_ctrlrequest r;
2365 } u;
2366 int tmp;
2367 struct net2280_request *req;
2369 if (dev->gadget.speed == USB_SPEED_UNKNOWN) {
2370 if (readl (&dev->usb->usbstat) & (1 << HIGH_SPEED))
2371 dev->gadget.speed = USB_SPEED_HIGH;
2372 else
2373 dev->gadget.speed = USB_SPEED_FULL;
2374 net2280_led_speed (dev, dev->gadget.speed);
2375 DEBUG (dev, "%s speed\n",
2376 (dev->gadget.speed == USB_SPEED_HIGH)
2377 ? "high" : "full");
2380 ep = &dev->ep [0];
2381 ep->irqs++;
2383 /* make sure any leftover request state is cleared */
2384 stat &= ~(1 << ENDPOINT_0_INTERRUPT);
2385 while (!list_empty (&ep->queue)) {
2386 req = list_entry (ep->queue.next,
2387 struct net2280_request, queue);
2388 done (ep, req, (req->req.actual == req->req.length)
2389 ? 0 : -EPROTO);
2391 ep->stopped = 0;
2392 dev->protocol_stall = 0;
2394 if (ep->dev->pdev->device == 0x2280)
2395 tmp = (1 << FIFO_OVERFLOW)
2396 | (1 << FIFO_UNDERFLOW);
2397 else
2398 tmp = 0;
2400 writel (tmp | (1 << TIMEOUT)
2401 | (1 << USB_STALL_SENT)
2402 | (1 << USB_IN_NAK_SENT)
2403 | (1 << USB_IN_ACK_RCVD)
2404 | (1 << USB_OUT_PING_NAK_SENT)
2405 | (1 << USB_OUT_ACK_SENT)
2406 | (1 << SHORT_PACKET_OUT_DONE_INTERRUPT)
2407 | (1 << SHORT_PACKET_TRANSFERRED_INTERRUPT)
2408 | (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2409 | (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2410 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2411 | (1 << DATA_IN_TOKEN_INTERRUPT)
2412 , &ep->regs->ep_stat);
2413 u.raw [0] = readl (&dev->usb->setup0123);
2414 u.raw [1] = readl (&dev->usb->setup4567);
2416 cpu_to_le32s (&u.raw [0]);
2417 cpu_to_le32s (&u.raw [1]);
2419 tmp = 0;
2421 #define w_value le16_to_cpup (&u.r.wValue)
2422 #define w_index le16_to_cpup (&u.r.wIndex)
2423 #define w_length le16_to_cpup (&u.r.wLength)
2425 /* ack the irq */
2426 writel (1 << SETUP_PACKET_INTERRUPT, &dev->regs->irqstat0);
2427 stat ^= (1 << SETUP_PACKET_INTERRUPT);
2429 /* watch control traffic at the token level, and force
2430 * synchronization before letting the status stage happen.
2431 * FIXME ignore tokens we'll NAK, until driver responds.
2432 * that'll mean a lot less irqs for some drivers.
2434 ep->is_in = (u.r.bRequestType & USB_DIR_IN) != 0;
2435 if (ep->is_in) {
2436 scratch = (1 << DATA_PACKET_TRANSMITTED_INTERRUPT)
2437 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2438 | (1 << DATA_IN_TOKEN_INTERRUPT);
2439 stop_out_naking (ep);
2440 } else
2441 scratch = (1 << DATA_PACKET_RECEIVED_INTERRUPT)
2442 | (1 << DATA_OUT_PING_TOKEN_INTERRUPT)
2443 | (1 << DATA_IN_TOKEN_INTERRUPT);
2444 writel (scratch, &dev->epregs [0].ep_irqenb);
2446 /* we made the hardware handle most lowlevel requests;
2447 * everything else goes uplevel to the gadget code.
2449 switch (u.r.bRequest) {
2450 case USB_REQ_GET_STATUS: {
2451 struct net2280_ep *e;
2452 __le32 status;
2454 /* hw handles device and interface status */
2455 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
2456 goto delegate;
2457 if ((e = get_ep_by_addr (dev, w_index)) == 0
2458 || w_length > 2)
2459 goto do_stall;
2461 if (readl (&e->regs->ep_rsp)
2462 & (1 << SET_ENDPOINT_HALT))
2463 status = __constant_cpu_to_le32 (1);
2464 else
2465 status = __constant_cpu_to_le32 (0);
2467 /* don't bother with a request object! */
2468 writel (0, &dev->epregs [0].ep_irqenb);
2469 set_fifo_bytecount (ep, w_length);
2470 writel ((__force u32)status, &dev->epregs [0].ep_data);
2471 allow_status (ep);
2472 VDEBUG (dev, "%s stat %02x\n", ep->ep.name, status);
2473 goto next_endpoints;
2475 break;
2476 case USB_REQ_CLEAR_FEATURE: {
2477 struct net2280_ep *e;
2479 /* hw handles device features */
2480 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2481 goto delegate;
2482 if (w_value != USB_ENDPOINT_HALT
2483 || w_length != 0)
2484 goto do_stall;
2485 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2486 goto do_stall;
2487 clear_halt (e);
2488 allow_status (ep);
2489 VDEBUG (dev, "%s clear halt\n", ep->ep.name);
2490 goto next_endpoints;
2492 break;
2493 case USB_REQ_SET_FEATURE: {
2494 struct net2280_ep *e;
2496 /* hw handles device features */
2497 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
2498 goto delegate;
2499 if (w_value != USB_ENDPOINT_HALT
2500 || w_length != 0)
2501 goto do_stall;
2502 if ((e = get_ep_by_addr (dev, w_index)) == 0)
2503 goto do_stall;
2504 set_halt (e);
2505 allow_status (ep);
2506 VDEBUG (dev, "%s set halt\n", ep->ep.name);
2507 goto next_endpoints;
2509 break;
2510 default:
2511 delegate:
2512 VDEBUG (dev, "setup %02x.%02x v%04x i%04x l%04x"
2513 "ep_cfg %08x\n",
2514 u.r.bRequestType, u.r.bRequest,
2515 w_value, w_index, w_length,
2516 readl (&ep->regs->ep_cfg));
2517 spin_unlock (&dev->lock);
2518 tmp = dev->driver->setup (&dev->gadget, &u.r);
2519 spin_lock (&dev->lock);
2522 /* stall ep0 on error */
2523 if (tmp < 0) {
2524 do_stall:
2525 VDEBUG (dev, "req %02x.%02x protocol STALL; stat %d\n",
2526 u.r.bRequestType, u.r.bRequest, tmp);
2527 dev->protocol_stall = 1;
2530 /* some in/out token irq should follow; maybe stall then.
2531 * driver must queue a request (even zlp) or halt ep0
2532 * before the host times out.
2536 #undef w_value
2537 #undef w_index
2538 #undef w_length
2540 next_endpoints:
2541 /* endpoint data irq ? */
2542 scratch = stat & 0x7f;
2543 stat &= ~0x7f;
2544 for (num = 0; scratch; num++) {
2545 u32 t;
2547 /* do this endpoint's FIFO and queue need tending? */
2548 t = 1 << num;
2549 if ((scratch & t) == 0)
2550 continue;
2551 scratch ^= t;
2553 ep = &dev->ep [num];
2554 handle_ep_small (ep);
2557 if (stat)
2558 DEBUG (dev, "unhandled irqstat0 %08x\n", stat);
2561 #define DMA_INTERRUPTS ( \
2562 (1 << DMA_D_INTERRUPT) \
2563 | (1 << DMA_C_INTERRUPT) \
2564 | (1 << DMA_B_INTERRUPT) \
2565 | (1 << DMA_A_INTERRUPT))
2566 #define PCI_ERROR_INTERRUPTS ( \
2567 (1 << PCI_MASTER_ABORT_RECEIVED_INTERRUPT) \
2568 | (1 << PCI_TARGET_ABORT_RECEIVED_INTERRUPT) \
2569 | (1 << PCI_RETRY_ABORT_INTERRUPT))
2571 static void handle_stat1_irqs (struct net2280 *dev, u32 stat)
2573 struct net2280_ep *ep;
2574 u32 tmp, num, mask, scratch;
2576 /* after disconnect there's nothing else to do! */
2577 tmp = (1 << VBUS_INTERRUPT) | (1 << ROOT_PORT_RESET_INTERRUPT);
2578 mask = (1 << HIGH_SPEED) | (1 << FULL_SPEED);
2580 /* VBUS disconnect is indicated by VBUS_PIN and VBUS_INTERRUPT set.
2581 * Root Port Reset is indicated by ROOT_PORT_RESET_INTERRRUPT set and
2582 * both HIGH_SPEED and FULL_SPEED clear (as ROOT_PORT_RESET_INTERRUPT
2583 * only indicates a change in the reset state).
2585 if (stat & tmp) {
2586 writel (tmp, &dev->regs->irqstat1);
2587 if ((((stat & (1 << ROOT_PORT_RESET_INTERRUPT)) &&
2588 ((readl (&dev->usb->usbstat) & mask) == 0))
2589 || ((readl (&dev->usb->usbctl) & (1 << VBUS_PIN)) == 0)
2590 ) && ( dev->gadget.speed != USB_SPEED_UNKNOWN)) {
2591 DEBUG (dev, "disconnect %s\n",
2592 dev->driver->driver.name);
2593 stop_activity (dev, dev->driver);
2594 ep0_start (dev);
2595 return;
2597 stat &= ~tmp;
2599 /* vBUS can bounce ... one of many reasons to ignore the
2600 * notion of hotplug events on bus connect/disconnect!
2602 if (!stat)
2603 return;
2606 /* NOTE: chip stays in PCI D0 state for now, but it could
2607 * enter D1 to save more power
2609 tmp = (1 << SUSPEND_REQUEST_CHANGE_INTERRUPT);
2610 if (stat & tmp) {
2611 writel (tmp, &dev->regs->irqstat1);
2612 if (stat & (1 << SUSPEND_REQUEST_INTERRUPT)) {
2613 if (dev->driver->suspend)
2614 dev->driver->suspend (&dev->gadget);
2615 if (!enable_suspend)
2616 stat &= ~(1 << SUSPEND_REQUEST_INTERRUPT);
2617 } else {
2618 if (dev->driver->resume)
2619 dev->driver->resume (&dev->gadget);
2620 /* at high speed, note erratum 0133 */
2622 stat &= ~tmp;
2625 /* clear any other status/irqs */
2626 if (stat)
2627 writel (stat, &dev->regs->irqstat1);
2629 /* some status we can just ignore */
2630 if (dev->pdev->device == 0x2280)
2631 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2632 | (1 << SUSPEND_REQUEST_INTERRUPT)
2633 | (1 << RESUME_INTERRUPT)
2634 | (1 << SOF_INTERRUPT));
2635 else
2636 stat &= ~((1 << CONTROL_STATUS_INTERRUPT)
2637 | (1 << RESUME_INTERRUPT)
2638 | (1 << SOF_DOWN_INTERRUPT)
2639 | (1 << SOF_INTERRUPT));
2641 if (!stat)
2642 return;
2643 // DEBUG (dev, "irqstat1 %08x\n", stat);
2645 /* DMA status, for ep-{a,b,c,d} */
2646 scratch = stat & DMA_INTERRUPTS;
2647 stat &= ~DMA_INTERRUPTS;
2648 scratch >>= 9;
2649 for (num = 0; scratch; num++) {
2650 struct net2280_dma_regs __iomem *dma;
2652 tmp = 1 << num;
2653 if ((tmp & scratch) == 0)
2654 continue;
2655 scratch ^= tmp;
2657 ep = &dev->ep [num + 1];
2658 dma = ep->dma;
2660 if (!dma)
2661 continue;
2663 /* clear ep's dma status */
2664 tmp = readl (&dma->dmastat);
2665 writel (tmp, &dma->dmastat);
2667 /* chaining should stop on abort, short OUT from fifo,
2668 * or (stat0 codepath) short OUT transfer.
2670 if (!use_dma_chaining) {
2671 if ((tmp & (1 << DMA_TRANSACTION_DONE_INTERRUPT))
2672 == 0) {
2673 DEBUG (ep->dev, "%s no xact done? %08x\n",
2674 ep->ep.name, tmp);
2675 continue;
2677 stop_dma (ep->dma);
2680 /* OUT transfers terminate when the data from the
2681 * host is in our memory. Process whatever's done.
2682 * On this path, we know transfer's last packet wasn't
2683 * less than req->length. NAK_OUT_PACKETS may be set,
2684 * or the FIFO may already be holding new packets.
2686 * IN transfers can linger in the FIFO for a very
2687 * long time ... we ignore that for now, accounting
2688 * precisely (like PIO does) needs per-packet irqs
2690 scan_dma_completions (ep);
2692 /* disable dma on inactive queues; else maybe restart */
2693 if (list_empty (&ep->queue)) {
2694 if (use_dma_chaining)
2695 stop_dma (ep->dma);
2696 } else {
2697 tmp = readl (&dma->dmactl);
2698 if (!use_dma_chaining
2699 || (tmp & (1 << DMA_ENABLE)) == 0)
2700 restart_dma (ep);
2701 else if (ep->is_in && use_dma_chaining) {
2702 struct net2280_request *req;
2703 __le32 dmacount;
2705 /* the descriptor at the head of the chain
2706 * may still have VALID_BIT clear; that's
2707 * used to trigger changing DMA_FIFO_VALIDATE
2708 * (affects automagic zlp writes).
2710 req = list_entry (ep->queue.next,
2711 struct net2280_request, queue);
2712 dmacount = req->td->dmacount;
2713 dmacount &= __constant_cpu_to_le32 (
2714 (1 << VALID_BIT)
2715 | DMA_BYTE_COUNT_MASK);
2716 if (dmacount && (dmacount & valid_bit) == 0)
2717 restart_dma (ep);
2720 ep->irqs++;
2723 /* NOTE: there are other PCI errors we might usefully notice.
2724 * if they appear very often, here's where to try recovering.
2726 if (stat & PCI_ERROR_INTERRUPTS) {
2727 ERROR (dev, "pci dma error; stat %08x\n", stat);
2728 stat &= ~PCI_ERROR_INTERRUPTS;
2729 /* these are fatal errors, but "maybe" they won't
2730 * happen again ...
2732 stop_activity (dev, dev->driver);
2733 ep0_start (dev);
2734 stat = 0;
2737 if (stat)
2738 DEBUG (dev, "unhandled irqstat1 %08x\n", stat);
2741 static irqreturn_t net2280_irq (int irq, void *_dev, struct pt_regs * r)
2743 struct net2280 *dev = _dev;
2745 /* shared interrupt, not ours */
2746 if (!(readl(&dev->regs->irqstat0) & (1 << INTA_ASSERTED)))
2747 return IRQ_NONE;
2749 spin_lock (&dev->lock);
2751 /* handle disconnect, dma, and more */
2752 handle_stat1_irqs (dev, readl (&dev->regs->irqstat1));
2754 /* control requests and PIO */
2755 handle_stat0_irqs (dev, readl (&dev->regs->irqstat0));
2757 spin_unlock (&dev->lock);
2759 return IRQ_HANDLED;
2762 /*-------------------------------------------------------------------------*/
2764 static void gadget_release (struct device *_dev)
2766 struct net2280 *dev = dev_get_drvdata (_dev);
2768 kfree (dev);
2771 /* tear down the binding between this driver and the pci device */
2773 static void net2280_remove (struct pci_dev *pdev)
2775 struct net2280 *dev = pci_get_drvdata (pdev);
2777 /* start with the driver above us */
2778 if (dev->driver) {
2779 /* should have been done already by driver model core */
2780 WARN (dev, "pci remove, driver '%s' is still registered\n",
2781 dev->driver->driver.name);
2782 usb_gadget_unregister_driver (dev->driver);
2785 /* then clean up the resources we allocated during probe() */
2786 net2280_led_shutdown (dev);
2787 if (dev->requests) {
2788 int i;
2789 for (i = 1; i < 5; i++) {
2790 if (!dev->ep [i].dummy)
2791 continue;
2792 pci_pool_free (dev->requests, dev->ep [i].dummy,
2793 dev->ep [i].td_dma);
2795 pci_pool_destroy (dev->requests);
2797 if (dev->got_irq)
2798 free_irq (pdev->irq, dev);
2799 if (dev->regs)
2800 iounmap (dev->regs);
2801 if (dev->region)
2802 release_mem_region (pci_resource_start (pdev, 0),
2803 pci_resource_len (pdev, 0));
2804 if (dev->enabled)
2805 pci_disable_device (pdev);
2806 device_unregister (&dev->gadget.dev);
2807 device_remove_file (&pdev->dev, &dev_attr_registers);
2808 pci_set_drvdata (pdev, NULL);
2810 INFO (dev, "unbind\n");
2812 the_controller = NULL;
2815 /* wrap this driver around the specified device, but
2816 * don't respond over USB until a gadget driver binds to us.
2819 static int net2280_probe (struct pci_dev *pdev, const struct pci_device_id *id)
2821 struct net2280 *dev;
2822 unsigned long resource, len;
2823 void __iomem *base = NULL;
2824 int retval, i;
2825 char buf [8], *bufp;
2827 /* if you want to support more than one controller in a system,
2828 * usb_gadget_driver_{register,unregister}() must change.
2830 if (the_controller) {
2831 dev_warn (&pdev->dev, "ignoring\n");
2832 return -EBUSY;
2835 /* alloc, and start init */
2836 dev = kzalloc (sizeof *dev, SLAB_KERNEL);
2837 if (dev == NULL){
2838 retval = -ENOMEM;
2839 goto done;
2842 pci_set_drvdata (pdev, dev);
2843 spin_lock_init (&dev->lock);
2844 dev->pdev = pdev;
2845 dev->gadget.ops = &net2280_ops;
2846 dev->gadget.is_dualspeed = 1;
2848 /* the "gadget" abstracts/virtualizes the controller */
2849 strcpy (dev->gadget.dev.bus_id, "gadget");
2850 dev->gadget.dev.parent = &pdev->dev;
2851 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2852 dev->gadget.dev.release = gadget_release;
2853 dev->gadget.name = driver_name;
2855 /* now all the pci goodies ... */
2856 if (pci_enable_device (pdev) < 0) {
2857 retval = -ENODEV;
2858 goto done;
2860 dev->enabled = 1;
2862 /* BAR 0 holds all the registers
2863 * BAR 1 is 8051 memory; unused here (note erratum 0103)
2864 * BAR 2 is fifo memory; unused here
2866 resource = pci_resource_start (pdev, 0);
2867 len = pci_resource_len (pdev, 0);
2868 if (!request_mem_region (resource, len, driver_name)) {
2869 DEBUG (dev, "controller already in use\n");
2870 retval = -EBUSY;
2871 goto done;
2873 dev->region = 1;
2875 base = ioremap_nocache (resource, len);
2876 if (base == NULL) {
2877 DEBUG (dev, "can't map memory\n");
2878 retval = -EFAULT;
2879 goto done;
2881 dev->regs = (struct net2280_regs __iomem *) base;
2882 dev->usb = (struct net2280_usb_regs __iomem *) (base + 0x0080);
2883 dev->pci = (struct net2280_pci_regs __iomem *) (base + 0x0100);
2884 dev->dma = (struct net2280_dma_regs __iomem *) (base + 0x0180);
2885 dev->dep = (struct net2280_dep_regs __iomem *) (base + 0x0200);
2886 dev->epregs = (struct net2280_ep_regs __iomem *) (base + 0x0300);
2888 /* put into initial config, link up all endpoints */
2889 writel (0, &dev->usb->usbctl);
2890 usb_reset (dev);
2891 usb_reinit (dev);
2893 /* irq setup after old hardware is cleaned up */
2894 if (!pdev->irq) {
2895 ERROR (dev, "No IRQ. Check PCI setup!\n");
2896 retval = -ENODEV;
2897 goto done;
2899 #ifndef __sparc__
2900 scnprintf (buf, sizeof buf, "%d", pdev->irq);
2901 bufp = buf;
2902 #else
2903 bufp = __irq_itoa(pdev->irq);
2904 #endif
2905 if (request_irq (pdev->irq, net2280_irq, SA_SHIRQ, driver_name, dev)
2906 != 0) {
2907 ERROR (dev, "request interrupt %s failed\n", bufp);
2908 retval = -EBUSY;
2909 goto done;
2911 dev->got_irq = 1;
2913 /* DMA setup */
2914 /* NOTE: we know only the 32 LSBs of dma addresses may be nonzero */
2915 dev->requests = pci_pool_create ("requests", pdev,
2916 sizeof (struct net2280_dma),
2917 0 /* no alignment requirements */,
2918 0 /* or page-crossing issues */);
2919 if (!dev->requests) {
2920 DEBUG (dev, "can't get request pool\n");
2921 retval = -ENOMEM;
2922 goto done;
2924 for (i = 1; i < 5; i++) {
2925 struct net2280_dma *td;
2927 td = pci_pool_alloc (dev->requests, GFP_KERNEL,
2928 &dev->ep [i].td_dma);
2929 if (!td) {
2930 DEBUG (dev, "can't get dummy %d\n", i);
2931 retval = -ENOMEM;
2932 goto done;
2934 td->dmacount = 0; /* not VALID */
2935 td->dmaaddr = __constant_cpu_to_le32 (DMA_ADDR_INVALID);
2936 td->dmadesc = td->dmaaddr;
2937 dev->ep [i].dummy = td;
2940 /* enable lower-overhead pci memory bursts during DMA */
2941 writel ( (1 << DMA_MEMORY_WRITE_AND_INVALIDATE_ENABLE)
2942 // 256 write retries may not be enough...
2943 // | (1 << PCI_RETRY_ABORT_ENABLE)
2944 | (1 << DMA_READ_MULTIPLE_ENABLE)
2945 | (1 << DMA_READ_LINE_ENABLE)
2946 , &dev->pci->pcimstctl);
2947 /* erratum 0115 shouldn't appear: Linux inits PCI_LATENCY_TIMER */
2948 pci_set_master (pdev);
2949 pci_set_mwi (pdev);
2951 /* ... also flushes any posted pci writes */
2952 dev->chiprev = get_idx_reg (dev->regs, REG_CHIPREV) & 0xffff;
2954 /* done */
2955 INFO (dev, "%s\n", driver_desc);
2956 INFO (dev, "irq %s, pci mem %p, chip rev %04x\n",
2957 bufp, base, dev->chiprev);
2958 INFO (dev, "version: " DRIVER_VERSION "; dma %s\n",
2959 use_dma
2960 ? (use_dma_chaining ? "chaining" : "enabled")
2961 : "disabled");
2962 the_controller = dev;
2964 device_register (&dev->gadget.dev);
2965 device_create_file (&pdev->dev, &dev_attr_registers);
2967 return 0;
2969 done:
2970 if (dev)
2971 net2280_remove (pdev);
2972 return retval;
2976 /*-------------------------------------------------------------------------*/
2978 static struct pci_device_id pci_ids [] = { {
2979 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2980 .class_mask = ~0,
2981 .vendor = 0x17cc,
2982 .device = 0x2280,
2983 .subvendor = PCI_ANY_ID,
2984 .subdevice = PCI_ANY_ID,
2985 }, {
2986 .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
2987 .class_mask = ~0,
2988 .vendor = 0x17cc,
2989 .device = 0x2282,
2990 .subvendor = PCI_ANY_ID,
2991 .subdevice = PCI_ANY_ID,
2993 }, { /* end: all zeroes */ }
2995 MODULE_DEVICE_TABLE (pci, pci_ids);
2997 /* pci driver glue; this is a "new style" PCI driver module */
2998 static struct pci_driver net2280_pci_driver = {
2999 .name = (char *) driver_name,
3000 .id_table = pci_ids,
3002 .probe = net2280_probe,
3003 .remove = net2280_remove,
3005 /* FIXME add power management support */
3008 MODULE_DESCRIPTION (DRIVER_DESC);
3009 MODULE_AUTHOR ("David Brownell");
3010 MODULE_LICENSE ("GPL");
3012 static int __init init (void)
3014 if (!use_dma)
3015 use_dma_chaining = 0;
3016 return pci_register_driver (&net2280_pci_driver);
3018 module_init (init);
3020 static void __exit cleanup (void)
3022 pci_unregister_driver (&net2280_pci_driver);
3024 module_exit (cleanup);