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