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