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