Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
goku_udc.c
Go to the documentation of this file.
1 /*
2  * Toshiba TC86C001 ("Goku-S") USB Device Controller driver
3  *
4  * Copyright (C) 2000-2002 Lineo
5  * by Stuart Lynne, Tom Rushworth, and Bruce Balden
6  * Copyright (C) 2002 Toshiba Corporation
7  * Copyright (C) 2003 MontaVista Software ([email protected])
8  *
9  * This file is licensed under the terms of the GNU General Public
10  * License version 2. This program is licensed "as is" without any
11  * warranty of any kind, whether express or implied.
12  */
13 
14 /*
15  * This device has ep0 and three semi-configurable bulk/interrupt endpoints.
16  *
17  * - Endpoint numbering is fixed: ep{1,2,3}-bulk
18  * - Gadget drivers can choose ep maxpacket (8/16/32/64)
19  * - Gadget drivers can choose direction (IN, OUT)
20  * - DMA works with ep1 (OUT transfers) and ep2 (IN transfers).
21  */
22 
23 // #define VERBOSE /* extra debug messages (success too) */
24 // #define USB_TRACE /* packet-level success messages */
25 
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/init.h>
34 #include <linux/timer.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
38 #include <linux/device.h>
39 #include <linux/usb/ch9.h>
40 #include <linux/usb/gadget.h>
41 #include <linux/prefetch.h>
42 
43 #include <asm/byteorder.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/unaligned.h>
47 
48 
49 #include "goku_udc.h"
50 
51 #define DRIVER_DESC "TC86C001 USB Device Controller"
52 #define DRIVER_VERSION "30-Oct 2003"
53 
54 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
55 
56 static const char driver_name [] = "goku_udc";
57 static const char driver_desc [] = DRIVER_DESC;
58 
61 MODULE_LICENSE("GPL");
62 
63 
64 /*
65  * IN dma behaves ok under testing, though the IN-dma abort paths don't
66  * seem to behave quite as expected. Used by default.
67  *
68  * OUT dma documents design problems handling the common "short packet"
69  * transfer termination policy; it couldn't be enabled by default, even
70  * if the OUT-dma abort problems had a resolution.
71  */
72 static unsigned use_dma = 1;
73 
74 #if 0
75 //#include <linux/moduleparam.h>
76 /* "modprobe goku_udc use_dma=1" etc
77  * 0 to disable dma
78  * 1 to use IN dma only (normal operation)
79  * 2 to use IN and OUT dma
80  */
82 #endif
83 
84 /*-------------------------------------------------------------------------*/
85 
86 static void nuke(struct goku_ep *, int status);
87 
88 static inline void
89 command(struct goku_udc_regs __iomem *regs, int command, unsigned epnum)
90 {
91  writel(COMMAND_EP(epnum) | command, &regs->Command);
92  udelay(300);
93 }
94 
95 static int
96 goku_ep_enable(struct usb_ep *_ep, const struct usb_endpoint_descriptor *desc)
97 {
98  struct goku_udc *dev;
99  struct goku_ep *ep;
100  u32 mode;
101  u16 max;
102  unsigned long flags;
103 
104  ep = container_of(_ep, struct goku_ep, ep);
105  if (!_ep || !desc
106  || desc->bDescriptorType != USB_DT_ENDPOINT)
107  return -EINVAL;
108  dev = ep->dev;
109  if (ep == &dev->ep[0])
110  return -EINVAL;
111  if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)
112  return -ESHUTDOWN;
113  if (ep->num != usb_endpoint_num(desc))
114  return -EINVAL;
115 
116  switch (usb_endpoint_type(desc)) {
119  break;
120  default:
121  return -EINVAL;
122  }
123 
124  if ((readl(ep->reg_status) & EPxSTATUS_EP_MASK)
126  return -EBUSY;
127 
128  /* enabling the no-toggle interrupt mode would need an api hook */
129  mode = 0;
130  max = get_unaligned_le16(&desc->wMaxPacketSize);
131  switch (max) {
132  case 64: mode++;
133  case 32: mode++;
134  case 16: mode++;
135  case 8: mode <<= 3;
136  break;
137  default:
138  return -EINVAL;
139  }
140  mode |= 2 << 1; /* bulk, or intr-with-toggle */
141 
142  /* ep1/ep2 dma direction is chosen early; it works in the other
143  * direction, with pio. be cautious with out-dma.
144  */
145  ep->is_in = usb_endpoint_dir_in(desc);
146  if (ep->is_in) {
147  mode |= 1;
148  ep->dma = (use_dma != 0) && (ep->num == UDC_MSTRD_ENDPOINT);
149  } else {
150  ep->dma = (use_dma == 2) && (ep->num == UDC_MSTWR_ENDPOINT);
151  if (ep->dma)
152  DBG(dev, "%s out-dma hides short packets\n",
153  ep->ep.name);
154  }
155 
156  spin_lock_irqsave(&ep->dev->lock, flags);
157 
158  /* ep1 and ep2 can do double buffering and/or dma */
159  if (ep->num < 3) {
160  struct goku_udc_regs __iomem *regs = ep->dev->regs;
161  u32 tmp;
162 
163  /* double buffer except (for now) with pio in */
164  tmp = ((ep->dma || !ep->is_in)
165  ? 0x10 /* double buffered */
166  : 0x11 /* single buffer */
167  ) << ep->num;
168  tmp |= readl(&regs->EPxSingle);
169  writel(tmp, &regs->EPxSingle);
170 
171  tmp = (ep->dma ? 0x10/*dma*/ : 0x11/*pio*/) << ep->num;
172  tmp |= readl(&regs->EPxBCS);
173  writel(tmp, &regs->EPxBCS);
174  }
175  writel(mode, ep->reg_mode);
176  command(ep->dev->regs, COMMAND_RESET, ep->num);
177  ep->ep.maxpacket = max;
178  ep->stopped = 0;
179  ep->ep.desc = desc;
180  spin_unlock_irqrestore(&ep->dev->lock, flags);
181 
182  DBG(dev, "enable %s %s %s maxpacket %u\n", ep->ep.name,
183  ep->is_in ? "IN" : "OUT",
184  ep->dma ? "dma" : "pio",
185  max);
186 
187  return 0;
188 }
189 
190 static void ep_reset(struct goku_udc_regs __iomem *regs, struct goku_ep *ep)
191 {
192  struct goku_udc *dev = ep->dev;
193 
194  if (regs) {
195  command(regs, COMMAND_INVALID, ep->num);
196  if (ep->num) {
197  if (ep->num == UDC_MSTWR_ENDPOINT)
198  dev->int_enable &= ~(INT_MSTWREND
199  |INT_MSTWRTMOUT);
200  else if (ep->num == UDC_MSTRD_ENDPOINT)
201  dev->int_enable &= ~INT_MSTRDEND;
202  dev->int_enable &= ~INT_EPxDATASET (ep->num);
203  } else
204  dev->int_enable &= ~INT_EP0;
205  writel(dev->int_enable, &regs->int_enable);
206  readl(&regs->int_enable);
207  if (ep->num < 3) {
208  struct goku_udc_regs __iomem *r = ep->dev->regs;
209  u32 tmp;
210 
211  tmp = readl(&r->EPxSingle);
212  tmp &= ~(0x11 << ep->num);
213  writel(tmp, &r->EPxSingle);
214 
215  tmp = readl(&r->EPxBCS);
216  tmp &= ~(0x11 << ep->num);
217  writel(tmp, &r->EPxBCS);
218  }
219  /* reset dma in case we're still using it */
220  if (ep->dma) {
221  u32 master;
222 
223  master = readl(&regs->dma_master) & MST_RW_BITS;
224  if (ep->num == UDC_MSTWR_ENDPOINT) {
225  master &= ~MST_W_BITS;
226  master |= MST_WR_RESET;
227  } else {
228  master &= ~MST_R_BITS;
229  master |= MST_RD_RESET;
230  }
231  writel(master, &regs->dma_master);
232  }
233  }
234 
235  ep->ep.maxpacket = MAX_FIFO_SIZE;
236  ep->ep.desc = NULL;
237  ep->stopped = 1;
238  ep->irqs = 0;
239  ep->dma = 0;
240 }
241 
242 static int goku_ep_disable(struct usb_ep *_ep)
243 {
244  struct goku_ep *ep;
245  struct goku_udc *dev;
246  unsigned long flags;
247 
248  ep = container_of(_ep, struct goku_ep, ep);
249  if (!_ep || !ep->ep.desc)
250  return -ENODEV;
251  dev = ep->dev;
252  if (dev->ep0state == EP0_SUSPEND)
253  return -EBUSY;
254 
255  VDBG(dev, "disable %s\n", _ep->name);
256 
257  spin_lock_irqsave(&dev->lock, flags);
258  nuke(ep, -ESHUTDOWN);
259  ep_reset(dev->regs, ep);
260  spin_unlock_irqrestore(&dev->lock, flags);
261 
262  return 0;
263 }
264 
265 /*-------------------------------------------------------------------------*/
266 
267 static struct usb_request *
268 goku_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
269 {
270  struct goku_request *req;
271 
272  if (!_ep)
273  return NULL;
274  req = kzalloc(sizeof *req, gfp_flags);
275  if (!req)
276  return NULL;
277 
278  req->req.dma = DMA_ADDR_INVALID;
279  INIT_LIST_HEAD(&req->queue);
280  return &req->req;
281 }
282 
283 static void
284 goku_free_request(struct usb_ep *_ep, struct usb_request *_req)
285 {
286  struct goku_request *req;
287 
288  if (!_ep || !_req)
289  return;
290 
291  req = container_of(_req, struct goku_request, req);
292  WARN_ON(!list_empty(&req->queue));
293  kfree(req);
294 }
295 
296 /*-------------------------------------------------------------------------*/
297 
298 static void
299 done(struct goku_ep *ep, struct goku_request *req, int status)
300 {
301  struct goku_udc *dev;
302  unsigned stopped = ep->stopped;
303 
304  list_del_init(&req->queue);
305 
306  if (likely(req->req.status == -EINPROGRESS))
307  req->req.status = status;
308  else
309  status = req->req.status;
310 
311  dev = ep->dev;
312 
313  if (ep->dma)
314  usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in);
315 
316 #ifndef USB_TRACE
317  if (status && status != -ESHUTDOWN)
318 #endif
319  VDBG(dev, "complete %s req %p stat %d len %u/%u\n",
320  ep->ep.name, &req->req, status,
321  req->req.actual, req->req.length);
322 
323  /* don't modify queue heads during completion callback */
324  ep->stopped = 1;
325  spin_unlock(&dev->lock);
326  req->req.complete(&ep->ep, &req->req);
327  spin_lock(&dev->lock);
328  ep->stopped = stopped;
329 }
330 
331 /*-------------------------------------------------------------------------*/
332 
333 static inline int
334 write_packet(u32 __iomem *fifo, u8 *buf, struct goku_request *req, unsigned max)
335 {
336  unsigned length, count;
337 
338  length = min(req->req.length - req->req.actual, max);
339  req->req.actual += length;
340 
341  count = length;
342  while (likely(count--))
343  writel(*buf++, fifo);
344  return length;
345 }
346 
347 // return: 0 = still running, 1 = completed, negative = errno
348 static int write_fifo(struct goku_ep *ep, struct goku_request *req)
349 {
350  struct goku_udc *dev = ep->dev;
351  u32 tmp;
352  u8 *buf;
353  unsigned count;
354  int is_last;
355 
356  tmp = readl(&dev->regs->DataSet);
357  buf = req->req.buf + req->req.actual;
358  prefetch(buf);
359 
360  dev = ep->dev;
361  if (unlikely(ep->num == 0 && dev->ep0state != EP0_IN))
362  return -EL2HLT;
363 
364  /* NOTE: just single-buffered PIO-IN for now. */
365  if (unlikely((tmp & DATASET_A(ep->num)) != 0))
366  return 0;
367 
368  /* clear our "packet available" irq */
369  if (ep->num != 0)
370  writel(~INT_EPxDATASET(ep->num), &dev->regs->int_status);
371 
372  count = write_packet(ep->reg_fifo, buf, req, ep->ep.maxpacket);
373 
374  /* last packet often short (sometimes a zlp, especially on ep0) */
375  if (unlikely(count != ep->ep.maxpacket)) {
376  writel(~(1<<ep->num), &dev->regs->EOP);
377  if (ep->num == 0) {
378  dev->ep[0].stopped = 1;
379  dev->ep0state = EP0_STATUS;
380  }
381  is_last = 1;
382  } else {
383  if (likely(req->req.length != req->req.actual)
384  || req->req.zero)
385  is_last = 0;
386  else
387  is_last = 1;
388  }
389 #if 0 /* printk seemed to trash is_last...*/
390 //#ifdef USB_TRACE
391  VDBG(dev, "wrote %s %u bytes%s IN %u left %p\n",
392  ep->ep.name, count, is_last ? "/last" : "",
393  req->req.length - req->req.actual, req);
394 #endif
395 
396  /* requests complete when all IN data is in the FIFO,
397  * or sometimes later, if a zlp was needed.
398  */
399  if (is_last) {
400  done(ep, req, 0);
401  return 1;
402  }
403 
404  return 0;
405 }
406 
407 static int read_fifo(struct goku_ep *ep, struct goku_request *req)
408 {
409  struct goku_udc_regs __iomem *regs;
410  u32 size, set;
411  u8 *buf;
412  unsigned bufferspace, is_short, dbuff;
413 
414  regs = ep->dev->regs;
415 top:
416  buf = req->req.buf + req->req.actual;
417  prefetchw(buf);
418 
419  if (unlikely(ep->num == 0 && ep->dev->ep0state != EP0_OUT))
420  return -EL2HLT;
421 
422  dbuff = (ep->num == 1 || ep->num == 2);
423  do {
424  /* ack dataset irq matching the status we'll handle */
425  if (ep->num != 0)
426  writel(~INT_EPxDATASET(ep->num), &regs->int_status);
427 
428  set = readl(&regs->DataSet) & DATASET_AB(ep->num);
429  size = readl(&regs->EPxSizeLA[ep->num]);
430  bufferspace = req->req.length - req->req.actual;
431 
432  /* usually do nothing without an OUT packet */
433  if (likely(ep->num != 0 || bufferspace != 0)) {
434  if (unlikely(set == 0))
435  break;
436  /* use ep1/ep2 double-buffering for OUT */
437  if (!(size & PACKET_ACTIVE))
438  size = readl(&regs->EPxSizeLB[ep->num]);
439  if (!(size & PACKET_ACTIVE)) /* "can't happen" */
440  break;
441  size &= DATASIZE; /* EPxSizeH == 0 */
442 
443  /* ep0out no-out-data case for set_config, etc */
444  } else
445  size = 0;
446 
447  /* read all bytes from this packet */
448  req->req.actual += size;
449  is_short = (size < ep->ep.maxpacket);
450 #ifdef USB_TRACE
451  VDBG(ep->dev, "read %s %u bytes%s OUT req %p %u/%u\n",
452  ep->ep.name, size, is_short ? "/S" : "",
453  req, req->req.actual, req->req.length);
454 #endif
455  while (likely(size-- != 0)) {
456  u8 byte = (u8) readl(ep->reg_fifo);
457 
458  if (unlikely(bufferspace == 0)) {
459  /* this happens when the driver's buffer
460  * is smaller than what the host sent.
461  * discard the extra data in this packet.
462  */
463  if (req->req.status != -EOVERFLOW)
464  DBG(ep->dev, "%s overflow %u\n",
465  ep->ep.name, size);
466  req->req.status = -EOVERFLOW;
467  } else {
468  *buf++ = byte;
469  bufferspace--;
470  }
471  }
472 
473  /* completion */
474  if (unlikely(is_short || req->req.actual == req->req.length)) {
475  if (unlikely(ep->num == 0)) {
476  /* non-control endpoints now usable? */
477  if (ep->dev->req_config)
478  writel(ep->dev->configured
480  : 0,
481  &regs->UsbState);
482  /* ep0out status stage */
483  writel(~(1<<0), &regs->EOP);
484  ep->stopped = 1;
485  ep->dev->ep0state = EP0_STATUS;
486  }
487  done(ep, req, 0);
488 
489  /* empty the second buffer asap */
490  if (dbuff && !list_empty(&ep->queue)) {
491  req = list_entry(ep->queue.next,
492  struct goku_request, queue);
493  goto top;
494  }
495  return 1;
496  }
497  } while (dbuff);
498  return 0;
499 }
500 
501 static inline void
502 pio_irq_enable(struct goku_udc *dev,
503  struct goku_udc_regs __iomem *regs, int epnum)
504 {
505  dev->int_enable |= INT_EPxDATASET (epnum);
506  writel(dev->int_enable, &regs->int_enable);
507  /* write may still be posted */
508 }
509 
510 static inline void
511 pio_irq_disable(struct goku_udc *dev,
512  struct goku_udc_regs __iomem *regs, int epnum)
513 {
514  dev->int_enable &= ~INT_EPxDATASET (epnum);
515  writel(dev->int_enable, &regs->int_enable);
516  /* write may still be posted */
517 }
518 
519 static inline void
520 pio_advance(struct goku_ep *ep)
521 {
522  struct goku_request *req;
523 
524  if (unlikely(list_empty (&ep->queue)))
525  return;
526  req = list_entry(ep->queue.next, struct goku_request, queue);
527  (ep->is_in ? write_fifo : read_fifo)(ep, req);
528 }
529 
530 
531 /*-------------------------------------------------------------------------*/
532 
533 // return: 0 = q running, 1 = q stopped, negative = errno
534 static int start_dma(struct goku_ep *ep, struct goku_request *req)
535 {
536  struct goku_udc_regs __iomem *regs = ep->dev->regs;
537  u32 master;
538  u32 start = req->req.dma;
539  u32 end = start + req->req.length - 1;
540 
541  master = readl(&regs->dma_master) & MST_RW_BITS;
542 
543  /* re-init the bits affecting IN dma; careful with zlps */
544  if (likely(ep->is_in)) {
545  if (unlikely(master & MST_RD_ENA)) {
546  DBG (ep->dev, "start, IN active dma %03x!!\n",
547  master);
548 // return -EL2HLT;
549  }
550  writel(end, &regs->in_dma_end);
551  writel(start, &regs->in_dma_start);
552 
553  master &= ~MST_R_BITS;
554  if (unlikely(req->req.length == 0))
555  master = MST_RD_ENA | MST_RD_EOPB;
556  else if ((req->req.length % ep->ep.maxpacket) != 0
557  || req->req.zero)
558  master = MST_RD_ENA | MST_EOPB_ENA;
559  else
560  master = MST_RD_ENA | MST_EOPB_DIS;
561 
562  ep->dev->int_enable |= INT_MSTRDEND;
563 
564  /* Goku DMA-OUT merges short packets, which plays poorly with
565  * protocols where short packets mark the transfer boundaries.
566  * The chip supports a nonstandard policy with INT_MSTWRTMOUT,
567  * ending transfers after 3 SOFs; we don't turn it on.
568  */
569  } else {
570  if (unlikely(master & MST_WR_ENA)) {
571  DBG (ep->dev, "start, OUT active dma %03x!!\n",
572  master);
573 // return -EL2HLT;
574  }
575  writel(end, &regs->out_dma_end);
576  writel(start, &regs->out_dma_start);
577 
578  master &= ~MST_W_BITS;
579  master |= MST_WR_ENA | MST_TIMEOUT_DIS;
580 
581  ep->dev->int_enable |= INT_MSTWREND|INT_MSTWRTMOUT;
582  }
583 
584  writel(master, &regs->dma_master);
585  writel(ep->dev->int_enable, &regs->int_enable);
586  return 0;
587 }
588 
589 static void dma_advance(struct goku_udc *dev, struct goku_ep *ep)
590 {
591  struct goku_request *req;
592  struct goku_udc_regs __iomem *regs = ep->dev->regs;
593  u32 master;
594 
595  master = readl(&regs->dma_master);
596 
597  if (unlikely(list_empty(&ep->queue))) {
598 stop:
599  if (ep->is_in)
600  dev->int_enable &= ~INT_MSTRDEND;
601  else
603  writel(dev->int_enable, &regs->int_enable);
604  return;
605  }
606  req = list_entry(ep->queue.next, struct goku_request, queue);
607 
608  /* normal hw dma completion (not abort) */
609  if (likely(ep->is_in)) {
610  if (unlikely(master & MST_RD_ENA))
611  return;
612  req->req.actual = readl(&regs->in_dma_current);
613  } else {
614  if (unlikely(master & MST_WR_ENA))
615  return;
616 
617  /* hardware merges short packets, and also hides packet
618  * overruns. a partial packet MAY be in the fifo here.
619  */
620  req->req.actual = readl(&regs->out_dma_current);
621  }
622  req->req.actual -= req->req.dma;
623  req->req.actual++;
624 
625 #ifdef USB_TRACE
626  VDBG(dev, "done %s %s dma, %u/%u bytes, req %p\n",
627  ep->ep.name, ep->is_in ? "IN" : "OUT",
628  req->req.actual, req->req.length, req);
629 #endif
630  done(ep, req, 0);
631  if (list_empty(&ep->queue))
632  goto stop;
633  req = list_entry(ep->queue.next, struct goku_request, queue);
634  (void) start_dma(ep, req);
635 }
636 
637 static void abort_dma(struct goku_ep *ep, int status)
638 {
639  struct goku_udc_regs __iomem *regs = ep->dev->regs;
640  struct goku_request *req;
641  u32 curr, master;
642 
643  /* NAK future host requests, hoping the implicit delay lets the
644  * dma engine finish reading (or writing) its latest packet and
645  * empty the dma buffer (up to 16 bytes).
646  *
647  * This avoids needing to clean up a partial packet in the fifo;
648  * we can't do that for IN without side effects to HALT and TOGGLE.
649  */
650  command(regs, COMMAND_FIFO_DISABLE, ep->num);
651  req = list_entry(ep->queue.next, struct goku_request, queue);
652  master = readl(&regs->dma_master) & MST_RW_BITS;
653 
654  /* FIXME using these resets isn't usably documented. this may
655  * not work unless it's followed by disabling the endpoint.
656  *
657  * FIXME the OUT reset path doesn't even behave consistently.
658  */
659  if (ep->is_in) {
660  if (unlikely((readl(&regs->dma_master) & MST_RD_ENA) == 0))
661  goto finished;
662  curr = readl(&regs->in_dma_current);
663 
664  writel(curr, &regs->in_dma_end);
665  writel(curr, &regs->in_dma_start);
666 
667  master &= ~MST_R_BITS;
668  master |= MST_RD_RESET;
669  writel(master, &regs->dma_master);
670 
671  if (readl(&regs->dma_master) & MST_RD_ENA)
672  DBG(ep->dev, "IN dma active after reset!\n");
673 
674  } else {
675  if (unlikely((readl(&regs->dma_master) & MST_WR_ENA) == 0))
676  goto finished;
677  curr = readl(&regs->out_dma_current);
678 
679  writel(curr, &regs->out_dma_end);
680  writel(curr, &regs->out_dma_start);
681 
682  master &= ~MST_W_BITS;
683  master |= MST_WR_RESET;
684  writel(master, &regs->dma_master);
685 
686  if (readl(&regs->dma_master) & MST_WR_ENA)
687  DBG(ep->dev, "OUT dma active after reset!\n");
688  }
689  req->req.actual = (curr - req->req.dma) + 1;
690  req->req.status = status;
691 
692  VDBG(ep->dev, "%s %s %s %d/%d\n", __func__, ep->ep.name,
693  ep->is_in ? "IN" : "OUT",
694  req->req.actual, req->req.length);
695 
696  command(regs, COMMAND_FIFO_ENABLE, ep->num);
697 
698  return;
699 
700 finished:
701  /* dma already completed; no abort needed */
702  command(regs, COMMAND_FIFO_ENABLE, ep->num);
703  req->req.actual = req->req.length;
704  req->req.status = 0;
705 }
706 
707 /*-------------------------------------------------------------------------*/
708 
709 static int
710 goku_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
711 {
712  struct goku_request *req;
713  struct goku_ep *ep;
714  struct goku_udc *dev;
715  unsigned long flags;
716  int status;
717 
718  /* always require a cpu-view buffer so pio works */
719  req = container_of(_req, struct goku_request, req);
720  if (unlikely(!_req || !_req->complete
721  || !_req->buf || !list_empty(&req->queue)))
722  return -EINVAL;
723  ep = container_of(_ep, struct goku_ep, ep);
724  if (unlikely(!_ep || (!ep->ep.desc && ep->num != 0)))
725  return -EINVAL;
726  dev = ep->dev;
727  if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN))
728  return -ESHUTDOWN;
729 
730  /* can't touch registers when suspended */
731  if (dev->ep0state == EP0_SUSPEND)
732  return -EBUSY;
733 
734  /* set up dma mapping in case the caller didn't */
735  if (ep->dma) {
736  status = usb_gadget_map_request(&dev->gadget, &req->req,
737  ep->is_in);
738  if (status)
739  return status;
740  }
741 
742 #ifdef USB_TRACE
743  VDBG(dev, "%s queue req %p, len %u buf %p\n",
744  _ep->name, _req, _req->length, _req->buf);
745 #endif
746 
747  spin_lock_irqsave(&dev->lock, flags);
748 
749  _req->status = -EINPROGRESS;
750  _req->actual = 0;
751 
752  /* for ep0 IN without premature status, zlp is required and
753  * writing EOP starts the status stage (OUT).
754  */
755  if (unlikely(ep->num == 0 && ep->is_in))
756  _req->zero = 1;
757 
758  /* kickstart this i/o queue? */
759  status = 0;
760  if (list_empty(&ep->queue) && likely(!ep->stopped)) {
761  /* dma: done after dma completion IRQ (or error)
762  * pio: done after last fifo operation
763  */
764  if (ep->dma)
765  status = start_dma(ep, req);
766  else
767  status = (ep->is_in ? write_fifo : read_fifo)(ep, req);
768 
769  if (unlikely(status != 0)) {
770  if (status > 0)
771  status = 0;
772  req = NULL;
773  }
774 
775  } /* else pio or dma irq handler advances the queue. */
776 
777  if (likely(req != 0))
778  list_add_tail(&req->queue, &ep->queue);
779 
780  if (likely(!list_empty(&ep->queue))
781  && likely(ep->num != 0)
782  && !ep->dma
783  && !(dev->int_enable & INT_EPxDATASET (ep->num)))
784  pio_irq_enable(dev, dev->regs, ep->num);
785 
786  spin_unlock_irqrestore(&dev->lock, flags);
787 
788  /* pci writes may still be posted */
789  return status;
790 }
791 
792 /* dequeue ALL requests */
793 static void nuke(struct goku_ep *ep, int status)
794 {
795  struct goku_request *req;
796 
797  ep->stopped = 1;
798  if (list_empty(&ep->queue))
799  return;
800  if (ep->dma)
801  abort_dma(ep, status);
802  while (!list_empty(&ep->queue)) {
803  req = list_entry(ep->queue.next, struct goku_request, queue);
804  done(ep, req, status);
805  }
806 }
807 
808 /* dequeue JUST ONE request */
809 static int goku_dequeue(struct usb_ep *_ep, struct usb_request *_req)
810 {
811  struct goku_request *req;
812  struct goku_ep *ep;
813  struct goku_udc *dev;
814  unsigned long flags;
815 
816  ep = container_of(_ep, struct goku_ep, ep);
817  if (!_ep || !_req || (!ep->ep.desc && ep->num != 0))
818  return -EINVAL;
819  dev = ep->dev;
820  if (!dev->driver)
821  return -ESHUTDOWN;
822 
823  /* we can't touch (dma) registers when suspended */
824  if (dev->ep0state == EP0_SUSPEND)
825  return -EBUSY;
826 
827  VDBG(dev, "%s %s %s %s %p\n", __func__, _ep->name,
828  ep->is_in ? "IN" : "OUT",
829  ep->dma ? "dma" : "pio",
830  _req);
831 
832  spin_lock_irqsave(&dev->lock, flags);
833 
834  /* make sure it's actually queued on this endpoint */
835  list_for_each_entry (req, &ep->queue, queue) {
836  if (&req->req == _req)
837  break;
838  }
839  if (&req->req != _req) {
840  spin_unlock_irqrestore (&dev->lock, flags);
841  return -EINVAL;
842  }
843 
844  if (ep->dma && ep->queue.next == &req->queue && !ep->stopped) {
845  abort_dma(ep, -ECONNRESET);
846  done(ep, req, -ECONNRESET);
847  dma_advance(dev, ep);
848  } else if (!list_empty(&req->queue))
849  done(ep, req, -ECONNRESET);
850  else
851  req = NULL;
852  spin_unlock_irqrestore(&dev->lock, flags);
853 
854  return req ? 0 : -EOPNOTSUPP;
855 }
856 
857 /*-------------------------------------------------------------------------*/
858 
859 static void goku_clear_halt(struct goku_ep *ep)
860 {
861  // assert (ep->num !=0)
862  VDBG(ep->dev, "%s clear halt\n", ep->ep.name);
863  command(ep->dev->regs, COMMAND_SETDATA0, ep->num);
864  command(ep->dev->regs, COMMAND_STALL_CLEAR, ep->num);
865  if (ep->stopped) {
866  ep->stopped = 0;
867  if (ep->dma) {
868  struct goku_request *req;
869 
870  if (list_empty(&ep->queue))
871  return;
872  req = list_entry(ep->queue.next, struct goku_request,
873  queue);
874  (void) start_dma(ep, req);
875  } else
876  pio_advance(ep);
877  }
878 }
879 
880 static int goku_set_halt(struct usb_ep *_ep, int value)
881 {
882  struct goku_ep *ep;
883  unsigned long flags;
884  int retval = 0;
885 
886  if (!_ep)
887  return -ENODEV;
888  ep = container_of (_ep, struct goku_ep, ep);
889 
890  if (ep->num == 0) {
891  if (value) {
892  ep->dev->ep0state = EP0_STALL;
893  ep->dev->ep[0].stopped = 1;
894  } else
895  return -EINVAL;
896 
897  /* don't change EPxSTATUS_EP_INVALID to READY */
898  } else if (!ep->ep.desc) {
899  DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
900  return -EINVAL;
901  }
902 
903  spin_lock_irqsave(&ep->dev->lock, flags);
904  if (!list_empty(&ep->queue))
905  retval = -EAGAIN;
906  else if (ep->is_in && value
907  /* data in (either) packet buffer? */
908  && (readl(&ep->dev->regs->DataSet)
909  & DATASET_AB(ep->num)))
910  retval = -EAGAIN;
911  else if (!value)
912  goku_clear_halt(ep);
913  else {
914  ep->stopped = 1;
915  VDBG(ep->dev, "%s set halt\n", ep->ep.name);
916  command(ep->dev->regs, COMMAND_STALL, ep->num);
917  readl(ep->reg_status);
918  }
919  spin_unlock_irqrestore(&ep->dev->lock, flags);
920  return retval;
921 }
922 
923 static int goku_fifo_status(struct usb_ep *_ep)
924 {
925  struct goku_ep *ep;
926  struct goku_udc_regs __iomem *regs;
927  u32 size;
928 
929  if (!_ep)
930  return -ENODEV;
931  ep = container_of(_ep, struct goku_ep, ep);
932 
933  /* size is only reported sanely for OUT */
934  if (ep->is_in)
935  return -EOPNOTSUPP;
936 
937  /* ignores 16-byte dma buffer; SizeH == 0 */
938  regs = ep->dev->regs;
939  size = readl(&regs->EPxSizeLA[ep->num]) & DATASIZE;
940  size += readl(&regs->EPxSizeLB[ep->num]) & DATASIZE;
941  VDBG(ep->dev, "%s %s %u\n", __func__, ep->ep.name, size);
942  return size;
943 }
944 
945 static void goku_fifo_flush(struct usb_ep *_ep)
946 {
947  struct goku_ep *ep;
948  struct goku_udc_regs __iomem *regs;
949  u32 size;
950 
951  if (!_ep)
952  return;
953  ep = container_of(_ep, struct goku_ep, ep);
954  VDBG(ep->dev, "%s %s\n", __func__, ep->ep.name);
955 
956  /* don't change EPxSTATUS_EP_INVALID to READY */
957  if (!ep->ep.desc && ep->num != 0) {
958  DBG(ep->dev, "%s %s inactive?\n", __func__, ep->ep.name);
959  return;
960  }
961 
962  regs = ep->dev->regs;
963  size = readl(&regs->EPxSizeLA[ep->num]);
964  size &= DATASIZE;
965 
966  /* Non-desirable behavior: FIFO_CLEAR also clears the
967  * endpoint halt feature. For OUT, we _could_ just read
968  * the bytes out (PIO, if !ep->dma); for in, no choice.
969  */
970  if (size)
971  command(regs, COMMAND_FIFO_CLEAR, ep->num);
972 }
973 
974 static struct usb_ep_ops goku_ep_ops = {
975  .enable = goku_ep_enable,
976  .disable = goku_ep_disable,
977 
978  .alloc_request = goku_alloc_request,
979  .free_request = goku_free_request,
980 
981  .queue = goku_queue,
982  .dequeue = goku_dequeue,
983 
984  .set_halt = goku_set_halt,
985  .fifo_status = goku_fifo_status,
986  .fifo_flush = goku_fifo_flush,
987 };
988 
989 /*-------------------------------------------------------------------------*/
990 
991 static int goku_get_frame(struct usb_gadget *_gadget)
992 {
993  return -EOPNOTSUPP;
994 }
995 
996 static int goku_start(struct usb_gadget_driver *driver,
997  int (*bind)(struct usb_gadget *, struct usb_gadget_driver *));
998 static int goku_stop(struct usb_gadget_driver *driver);
999 
1000 static const struct usb_gadget_ops goku_ops = {
1001  .get_frame = goku_get_frame,
1002  .start = goku_start,
1003  .stop = goku_stop,
1004  // no remote wakeup
1005  // not selfpowered
1006 };
1007 
1008 /*-------------------------------------------------------------------------*/
1009 
1010 static inline char *dmastr(void)
1011 {
1012  if (use_dma == 0)
1013  return "(dma disabled)";
1014  else if (use_dma == 2)
1015  return "(dma IN and OUT)";
1016  else
1017  return "(dma IN)";
1018 }
1019 
1020 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1021 
1022 static const char proc_node_name [] = "driver/udc";
1023 
1024 #define FOURBITS "%s%s%s%s"
1025 #define EIGHTBITS FOURBITS FOURBITS
1026 
1027 static void
1028 dump_intmask(const char *label, u32 mask, char **next, unsigned *size)
1029 {
1030  int t;
1031 
1032  /* int_status is the same format ... */
1033  t = scnprintf(*next, *size,
1034  "%s %05X =" FOURBITS EIGHTBITS EIGHTBITS "\n",
1035  label, mask,
1036  (mask & INT_PWRDETECT) ? " power" : "",
1037  (mask & INT_SYSERROR) ? " sys" : "",
1038  (mask & INT_MSTRDEND) ? " in-dma" : "",
1039  (mask & INT_MSTWRTMOUT) ? " wrtmo" : "",
1040 
1041  (mask & INT_MSTWREND) ? " out-dma" : "",
1042  (mask & INT_MSTWRSET) ? " wrset" : "",
1043  (mask & INT_ERR) ? " err" : "",
1044  (mask & INT_SOF) ? " sof" : "",
1045 
1046  (mask & INT_EP3NAK) ? " ep3nak" : "",
1047  (mask & INT_EP2NAK) ? " ep2nak" : "",
1048  (mask & INT_EP1NAK) ? " ep1nak" : "",
1049  (mask & INT_EP3DATASET) ? " ep3" : "",
1050 
1051  (mask & INT_EP2DATASET) ? " ep2" : "",
1052  (mask & INT_EP1DATASET) ? " ep1" : "",
1053  (mask & INT_STATUSNAK) ? " ep0snak" : "",
1054  (mask & INT_STATUS) ? " ep0status" : "",
1055 
1056  (mask & INT_SETUP) ? " setup" : "",
1057  (mask & INT_ENDPOINT0) ? " ep0" : "",
1058  (mask & INT_USBRESET) ? " reset" : "",
1059  (mask & INT_SUSPEND) ? " suspend" : "");
1060  *size -= t;
1061  *next += t;
1062 }
1063 
1064 
1065 static int
1066 udc_proc_read(char *buffer, char **start, off_t off, int count,
1067  int *eof, void *_dev)
1068 {
1069  char *buf = buffer;
1070  struct goku_udc *dev = _dev;
1071  struct goku_udc_regs __iomem *regs = dev->regs;
1072  char *next = buf;
1073  unsigned size = count;
1074  unsigned long flags;
1075  int i, t, is_usb_connected;
1076  u32 tmp;
1077 
1078  if (off != 0)
1079  return 0;
1080 
1081  local_irq_save(flags);
1082 
1083  /* basic device status */
1084  tmp = readl(&regs->power_detect);
1085  is_usb_connected = tmp & PW_DETECT;
1086  t = scnprintf(next, size,
1087  "%s - %s\n"
1088  "%s version: %s %s\n"
1089  "Gadget driver: %s\n"
1090  "Host %s, %s\n"
1091  "\n",
1092  pci_name(dev->pdev), driver_desc,
1093  driver_name, DRIVER_VERSION, dmastr(),
1094  dev->driver ? dev->driver->driver.name : "(none)",
1095  is_usb_connected
1096  ? ((tmp & PW_PULLUP) ? "full speed" : "powered")
1097  : "disconnected",
1098  ({char *state;
1099  switch(dev->ep0state){
1100  case EP0_DISCONNECT: state = "ep0_disconnect"; break;
1101  case EP0_IDLE: state = "ep0_idle"; break;
1102  case EP0_IN: state = "ep0_in"; break;
1103  case EP0_OUT: state = "ep0_out"; break;
1104  case EP0_STATUS: state = "ep0_status"; break;
1105  case EP0_STALL: state = "ep0_stall"; break;
1106  case EP0_SUSPEND: state = "ep0_suspend"; break;
1107  default: state = "ep0_?"; break;
1108  } state; })
1109  );
1110  size -= t;
1111  next += t;
1112 
1113  dump_intmask("int_status", readl(&regs->int_status), &next, &size);
1114  dump_intmask("int_enable", readl(&regs->int_enable), &next, &size);
1115 
1116  if (!is_usb_connected || !dev->driver || (tmp & PW_PULLUP) == 0)
1117  goto done;
1118 
1119  /* registers for (active) device and ep0 */
1120  t = scnprintf(next, size, "\nirqs %lu\ndataset %02x "
1121  "single.bcs %02x.%02x state %x addr %u\n",
1122  dev->irqs, readl(&regs->DataSet),
1123  readl(&regs->EPxSingle), readl(&regs->EPxBCS),
1124  readl(&regs->UsbState),
1125  readl(&regs->address));
1126  size -= t;
1127  next += t;
1128 
1129  tmp = readl(&regs->dma_master);
1130  t = scnprintf(next, size,
1131  "dma %03X =" EIGHTBITS "%s %s\n", tmp,
1132  (tmp & MST_EOPB_DIS) ? " eopb-" : "",
1133  (tmp & MST_EOPB_ENA) ? " eopb+" : "",
1134  (tmp & MST_TIMEOUT_DIS) ? " tmo-" : "",
1135  (tmp & MST_TIMEOUT_ENA) ? " tmo+" : "",
1136 
1137  (tmp & MST_RD_EOPB) ? " eopb" : "",
1138  (tmp & MST_RD_RESET) ? " in_reset" : "",
1139  (tmp & MST_WR_RESET) ? " out_reset" : "",
1140  (tmp & MST_RD_ENA) ? " IN" : "",
1141 
1142  (tmp & MST_WR_ENA) ? " OUT" : "",
1143  (tmp & MST_CONNECTION)
1144  ? "ep1in/ep2out"
1145  : "ep1out/ep2in");
1146  size -= t;
1147  next += t;
1148 
1149  /* dump endpoint queues */
1150  for (i = 0; i < 4; i++) {
1151  struct goku_ep *ep = &dev->ep [i];
1152  struct goku_request *req;
1153 
1154  if (i && !ep->ep.desc)
1155  continue;
1156 
1157  tmp = readl(ep->reg_status);
1158  t = scnprintf(next, size,
1159  "%s %s max %u %s, irqs %lu, "
1160  "status %02x (%s) " FOURBITS "\n",
1161  ep->ep.name,
1162  ep->is_in ? "in" : "out",
1163  ep->ep.maxpacket,
1164  ep->dma ? "dma" : "pio",
1165  ep->irqs,
1166  tmp, ({ char *s;
1167  switch (tmp & EPxSTATUS_EP_MASK) {
1168  case EPxSTATUS_EP_READY:
1169  s = "ready"; break;
1170  case EPxSTATUS_EP_DATAIN:
1171  s = "packet"; break;
1172  case EPxSTATUS_EP_FULL:
1173  s = "full"; break;
1174  case EPxSTATUS_EP_TX_ERR: // host will retry
1175  s = "tx_err"; break;
1176  case EPxSTATUS_EP_RX_ERR:
1177  s = "rx_err"; break;
1178  case EPxSTATUS_EP_BUSY: /* ep0 only */
1179  s = "busy"; break;
1180  case EPxSTATUS_EP_STALL:
1181  s = "stall"; break;
1182  case EPxSTATUS_EP_INVALID: // these "can't happen"
1183  s = "invalid"; break;
1184  default:
1185  s = "?"; break;
1186  }; s; }),
1187  (tmp & EPxSTATUS_TOGGLE) ? "data1" : "data0",
1188  (tmp & EPxSTATUS_SUSPEND) ? " suspend" : "",
1189  (tmp & EPxSTATUS_FIFO_DISABLE) ? " disable" : "",
1190  (tmp & EPxSTATUS_STAGE_ERROR) ? " ep0stat" : ""
1191  );
1192  if (t <= 0 || t > size)
1193  goto done;
1194  size -= t;
1195  next += t;
1196 
1197  if (list_empty(&ep->queue)) {
1198  t = scnprintf(next, size, "\t(nothing queued)\n");
1199  if (t <= 0 || t > size)
1200  goto done;
1201  size -= t;
1202  next += t;
1203  continue;
1204  }
1205  list_for_each_entry(req, &ep->queue, queue) {
1206  if (ep->dma && req->queue.prev == &ep->queue) {
1207  if (i == UDC_MSTRD_ENDPOINT)
1208  tmp = readl(&regs->in_dma_current);
1209  else
1210  tmp = readl(&regs->out_dma_current);
1211  tmp -= req->req.dma;
1212  tmp++;
1213  } else
1214  tmp = req->req.actual;
1215 
1216  t = scnprintf(next, size,
1217  "\treq %p len %u/%u buf %p\n",
1218  &req->req, tmp, req->req.length,
1219  req->req.buf);
1220  if (t <= 0 || t > size)
1221  goto done;
1222  size -= t;
1223  next += t;
1224  }
1225  }
1226 
1227 done:
1228  local_irq_restore(flags);
1229  *eof = 1;
1230  return count - size;
1231 }
1232 
1233 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1234 
1235 /*-------------------------------------------------------------------------*/
1236 
1237 static void udc_reinit (struct goku_udc *dev)
1238 {
1239  static char *names [] = { "ep0", "ep1-bulk", "ep2-bulk", "ep3-bulk" };
1240 
1241  unsigned i;
1242 
1243  INIT_LIST_HEAD (&dev->gadget.ep_list);
1244  dev->gadget.ep0 = &dev->ep [0].ep;
1245  dev->gadget.speed = USB_SPEED_UNKNOWN;
1246  dev->ep0state = EP0_DISCONNECT;
1247  dev->irqs = 0;
1248 
1249  for (i = 0; i < 4; i++) {
1250  struct goku_ep *ep = &dev->ep[i];
1251 
1252  ep->num = i;
1253  ep->ep.name = names[i];
1254  ep->reg_fifo = &dev->regs->ep_fifo [i];
1255  ep->reg_status = &dev->regs->ep_status [i];
1256  ep->reg_mode = &dev->regs->ep_mode[i];
1257 
1258  ep->ep.ops = &goku_ep_ops;
1259  list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1260  ep->dev = dev;
1261  INIT_LIST_HEAD (&ep->queue);
1262 
1263  ep_reset(NULL, ep);
1264  }
1265 
1266  dev->ep[0].reg_mode = NULL;
1267  dev->ep[0].ep.maxpacket = MAX_EP0_SIZE;
1268  list_del_init (&dev->ep[0].ep.ep_list);
1269 }
1270 
1271 static void udc_reset(struct goku_udc *dev)
1272 {
1273  struct goku_udc_regs __iomem *regs = dev->regs;
1274 
1275  writel(0, &regs->power_detect);
1276  writel(0, &regs->int_enable);
1277  readl(&regs->int_enable);
1278  dev->int_enable = 0;
1279 
1280  /* deassert reset, leave USB D+ at hi-Z (no pullup)
1281  * don't let INT_PWRDETECT sequence begin
1282  */
1283  udelay(250);
1284  writel(PW_RESETB, &regs->power_detect);
1285  readl(&regs->int_enable);
1286 }
1287 
1288 static void ep0_start(struct goku_udc *dev)
1289 {
1290  struct goku_udc_regs __iomem *regs = dev->regs;
1291  unsigned i;
1292 
1293  VDBG(dev, "%s\n", __func__);
1294 
1295  udc_reset(dev);
1296  udc_reinit (dev);
1297  //writel(MST_EOPB_ENA | MST_TIMEOUT_ENA, &regs->dma_master);
1298 
1299  /* hw handles set_address, set_feature, get_status; maybe more */
1304  , &regs->reqmode);
1305 
1306  for (i = 0; i < 4; i++)
1307  dev->ep[i].irqs = 0;
1308 
1309  /* can't modify descriptors after writing UsbReady */
1310  for (i = 0; i < DESC_LEN; i++)
1311  writel(0, &regs->descriptors[i]);
1312  writel(0, &regs->UsbReady);
1313 
1314  /* expect ep0 requests when the host drops reset */
1315  writel(PW_RESETB | PW_PULLUP, &regs->power_detect);
1316  dev->int_enable = INT_DEVWIDE | INT_EP0;
1317  writel(dev->int_enable, &dev->regs->int_enable);
1318  readl(&regs->int_enable);
1319  dev->gadget.speed = USB_SPEED_FULL;
1320  dev->ep0state = EP0_IDLE;
1321 }
1322 
1323 static void udc_enable(struct goku_udc *dev)
1324 {
1325  /* start enumeration now, or after power detect irq */
1326  if (readl(&dev->regs->power_detect) & PW_DETECT)
1327  ep0_start(dev);
1328  else {
1329  DBG(dev, "%s\n", __func__);
1330  dev->int_enable = INT_PWRDETECT;
1331  writel(dev->int_enable, &dev->regs->int_enable);
1332  }
1333 }
1334 
1335 /*-------------------------------------------------------------------------*/
1336 
1337 /* keeping it simple:
1338  * - one bus driver, initted first;
1339  * - one function driver, initted second
1340  */
1341 
1342 static struct goku_udc *the_controller;
1343 
1344 /* when a driver is successfully registered, it will receive
1345  * control requests including set_configuration(), which enables
1346  * non-control requests. then usb traffic follows until a
1347  * disconnect is reported. then a host may connect again, or
1348  * the driver might get unbound.
1349  */
1350 static int goku_start(struct usb_gadget_driver *driver,
1351  int (*bind)(struct usb_gadget *, struct usb_gadget_driver *))
1352 {
1353  struct goku_udc *dev = the_controller;
1354  int retval;
1355 
1356  if (!driver
1357  || driver->max_speed < USB_SPEED_FULL
1358  || !bind
1359  || !driver->disconnect
1360  || !driver->setup)
1361  return -EINVAL;
1362  if (!dev)
1363  return -ENODEV;
1364  if (dev->driver)
1365  return -EBUSY;
1366 
1367  /* hook up the driver */
1368  driver->driver.bus = NULL;
1369  dev->driver = driver;
1370  dev->gadget.dev.driver = &driver->driver;
1371  retval = bind(&dev->gadget, driver);
1372  if (retval) {
1373  DBG(dev, "bind to driver %s --> error %d\n",
1374  driver->driver.name, retval);
1375  dev->driver = NULL;
1376  dev->gadget.dev.driver = NULL;
1377  return retval;
1378  }
1379 
1380  /* then enable host detection and ep0; and we're ready
1381  * for set_configuration as well as eventual disconnect.
1382  */
1383  udc_enable(dev);
1384 
1385  DBG(dev, "registered gadget driver '%s'\n", driver->driver.name);
1386  return 0;
1387 }
1388 
1389 static void
1390 stop_activity(struct goku_udc *dev, struct usb_gadget_driver *driver)
1391 {
1392  unsigned i;
1393 
1394  DBG (dev, "%s\n", __func__);
1395 
1396  if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1397  driver = NULL;
1398 
1399  /* disconnect gadget driver after quiesceing hw and the driver */
1400  udc_reset (dev);
1401  for (i = 0; i < 4; i++)
1402  nuke(&dev->ep [i], -ESHUTDOWN);
1403  if (driver) {
1404  spin_unlock(&dev->lock);
1405  driver->disconnect(&dev->gadget);
1406  spin_lock(&dev->lock);
1407  }
1408 
1409  if (dev->driver)
1410  udc_enable(dev);
1411 }
1412 
1413 static int goku_stop(struct usb_gadget_driver *driver)
1414 {
1415  struct goku_udc *dev = the_controller;
1416  unsigned long flags;
1417 
1418  if (!dev)
1419  return -ENODEV;
1420  if (!driver || driver != dev->driver || !driver->unbind)
1421  return -EINVAL;
1422 
1423  spin_lock_irqsave(&dev->lock, flags);
1424  dev->driver = NULL;
1425  stop_activity(dev, driver);
1426  spin_unlock_irqrestore(&dev->lock, flags);
1427 
1428  driver->unbind(&dev->gadget);
1429  dev->gadget.dev.driver = NULL;
1430 
1431  DBG(dev, "unregistered driver '%s'\n", driver->driver.name);
1432  return 0;
1433 }
1434 
1435 /*-------------------------------------------------------------------------*/
1436 
1437 static void ep0_setup(struct goku_udc *dev)
1438 {
1439  struct goku_udc_regs __iomem *regs = dev->regs;
1440  struct usb_ctrlrequest ctrl;
1441  int tmp;
1442 
1443  /* read SETUP packet and enter DATA stage */
1444  ctrl.bRequestType = readl(&regs->bRequestType);
1445  ctrl.bRequest = readl(&regs->bRequest);
1446  ctrl.wValue = cpu_to_le16((readl(&regs->wValueH) << 8)
1447  | readl(&regs->wValueL));
1448  ctrl.wIndex = cpu_to_le16((readl(&regs->wIndexH) << 8)
1449  | readl(&regs->wIndexL));
1450  ctrl.wLength = cpu_to_le16((readl(&regs->wLengthH) << 8)
1451  | readl(&regs->wLengthL));
1452  writel(0, &regs->SetupRecv);
1453 
1454  nuke(&dev->ep[0], 0);
1455  dev->ep[0].stopped = 0;
1456  if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1457  dev->ep[0].is_in = 1;
1458  dev->ep0state = EP0_IN;
1459  /* detect early status stages */
1460  writel(ICONTROL_STATUSNAK, &dev->regs->IntControl);
1461  } else {
1462  dev->ep[0].is_in = 0;
1463  dev->ep0state = EP0_OUT;
1464 
1465  /* NOTE: CLEAR_FEATURE is done in software so that we can
1466  * synchronize transfer restarts after bulk IN stalls. data
1467  * won't even enter the fifo until the halt is cleared.
1468  */
1469  switch (ctrl.bRequest) {
1470  case USB_REQ_CLEAR_FEATURE:
1471  switch (ctrl.bRequestType) {
1472  case USB_RECIP_ENDPOINT:
1473  tmp = le16_to_cpu(ctrl.wIndex) & 0x0f;
1474  /* active endpoint */
1475  if (tmp > 3 ||
1476  (!dev->ep[tmp].ep.desc && tmp != 0))
1477  goto stall;
1478  if (ctrl.wIndex & cpu_to_le16(
1479  USB_DIR_IN)) {
1480  if (!dev->ep[tmp].is_in)
1481  goto stall;
1482  } else {
1483  if (dev->ep[tmp].is_in)
1484  goto stall;
1485  }
1486  if (ctrl.wValue != cpu_to_le16(
1488  goto stall;
1489  if (tmp)
1490  goku_clear_halt(&dev->ep[tmp]);
1491 succeed:
1492  /* start ep0out status stage */
1493  writel(~(1<<0), &regs->EOP);
1494  dev->ep[0].stopped = 1;
1495  dev->ep0state = EP0_STATUS;
1496  return;
1497  case USB_RECIP_DEVICE:
1498  /* device remote wakeup: always clear */
1499  if (ctrl.wValue != cpu_to_le16(1))
1500  goto stall;
1501  VDBG(dev, "clear dev remote wakeup\n");
1502  goto succeed;
1503  case USB_RECIP_INTERFACE:
1504  goto stall;
1505  default: /* pass to gadget driver */
1506  break;
1507  }
1508  break;
1509  default:
1510  break;
1511  }
1512  }
1513 
1514 #ifdef USB_TRACE
1515  VDBG(dev, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1516  ctrl.bRequestType, ctrl.bRequest,
1517  le16_to_cpu(ctrl.wValue), le16_to_cpu(ctrl.wIndex),
1518  le16_to_cpu(ctrl.wLength));
1519 #endif
1520 
1521  /* hw wants to know when we're configured (or not) */
1522  dev->req_config = (ctrl.bRequest == USB_REQ_SET_CONFIGURATION
1523  && ctrl.bRequestType == USB_RECIP_DEVICE);
1524  if (unlikely(dev->req_config))
1525  dev->configured = (ctrl.wValue != cpu_to_le16(0));
1526 
1527  /* delegate everything to the gadget driver.
1528  * it may respond after this irq handler returns.
1529  */
1530  spin_unlock (&dev->lock);
1531  tmp = dev->driver->setup(&dev->gadget, &ctrl);
1532  spin_lock (&dev->lock);
1533  if (unlikely(tmp < 0)) {
1534 stall:
1535 #ifdef USB_TRACE
1536  VDBG(dev, "req %02x.%02x protocol STALL; err %d\n",
1537  ctrl.bRequestType, ctrl.bRequest, tmp);
1538 #endif
1539  command(regs, COMMAND_STALL, 0);
1540  dev->ep[0].stopped = 1;
1541  dev->ep0state = EP0_STALL;
1542  }
1543 
1544  /* expect at least one data or status stage irq */
1545 }
1546 
1547 #define ACK(irqbit) { \
1548  stat &= ~irqbit; \
1549  writel(~irqbit, &regs->int_status); \
1550  handled = 1; \
1551  }
1552 
1553 static irqreturn_t goku_irq(int irq, void *_dev)
1554 {
1555  struct goku_udc *dev = _dev;
1556  struct goku_udc_regs __iomem *regs = dev->regs;
1557  struct goku_ep *ep;
1558  u32 stat, handled = 0;
1559  unsigned i, rescans = 5;
1560 
1561  spin_lock(&dev->lock);
1562 
1563 rescan:
1564  stat = readl(&regs->int_status) & dev->int_enable;
1565  if (!stat)
1566  goto done;
1567  dev->irqs++;
1568 
1569  /* device-wide irqs */
1570  if (unlikely(stat & INT_DEVWIDE)) {
1571  if (stat & INT_SYSERROR) {
1572  ERROR(dev, "system error\n");
1573  stop_activity(dev, dev->driver);
1574  stat = 0;
1575  handled = 1;
1576  // FIXME have a neater way to prevent re-enumeration
1577  dev->driver = NULL;
1578  goto done;
1579  }
1580  if (stat & INT_PWRDETECT) {
1581  writel(~stat, &regs->int_status);
1582  if (readl(&dev->regs->power_detect) & PW_DETECT) {
1583  VDBG(dev, "connect\n");
1584  ep0_start(dev);
1585  } else {
1586  DBG(dev, "disconnect\n");
1587  if (dev->gadget.speed == USB_SPEED_FULL)
1588  stop_activity(dev, dev->driver);
1589  dev->ep0state = EP0_DISCONNECT;
1590  dev->int_enable = INT_DEVWIDE;
1591  writel(dev->int_enable, &dev->regs->int_enable);
1592  }
1593  stat = 0;
1594  handled = 1;
1595  goto done;
1596  }
1597  if (stat & INT_SUSPEND) {
1598  ACK(INT_SUSPEND);
1599  if (readl(&regs->ep_status[0]) & EPxSTATUS_SUSPEND) {
1600  switch (dev->ep0state) {
1601  case EP0_DISCONNECT:
1602  case EP0_SUSPEND:
1603  goto pm_next;
1604  default:
1605  break;
1606  }
1607  DBG(dev, "USB suspend\n");
1608  dev->ep0state = EP0_SUSPEND;
1609  if (dev->gadget.speed != USB_SPEED_UNKNOWN
1610  && dev->driver
1611  && dev->driver->suspend) {
1612  spin_unlock(&dev->lock);
1613  dev->driver->suspend(&dev->gadget);
1614  spin_lock(&dev->lock);
1615  }
1616  } else {
1617  if (dev->ep0state != EP0_SUSPEND) {
1618  DBG(dev, "bogus USB resume %d\n",
1619  dev->ep0state);
1620  goto pm_next;
1621  }
1622  DBG(dev, "USB resume\n");
1623  dev->ep0state = EP0_IDLE;
1624  if (dev->gadget.speed != USB_SPEED_UNKNOWN
1625  && dev->driver
1626  && dev->driver->resume) {
1627  spin_unlock(&dev->lock);
1628  dev->driver->resume(&dev->gadget);
1629  spin_lock(&dev->lock);
1630  }
1631  }
1632  }
1633 pm_next:
1634  if (stat & INT_USBRESET) { /* hub reset done */
1635  ACK(INT_USBRESET);
1636  INFO(dev, "USB reset done, gadget %s\n",
1637  dev->driver->driver.name);
1638  }
1639  // and INT_ERR on some endpoint's crc/bitstuff/... problem
1640  }
1641 
1642  /* progress ep0 setup, data, or status stages.
1643  * no transition {EP0_STATUS, EP0_STALL} --> EP0_IDLE; saves irqs
1644  */
1645  if (stat & INT_SETUP) {
1646  ACK(INT_SETUP);
1647  dev->ep[0].irqs++;
1648  ep0_setup(dev);
1649  }
1650  if (stat & INT_STATUSNAK) {
1651  ACK(INT_STATUSNAK|INT_ENDPOINT0);
1652  if (dev->ep0state == EP0_IN) {
1653  ep = &dev->ep[0];
1654  ep->irqs++;
1655  nuke(ep, 0);
1656  writel(~(1<<0), &regs->EOP);
1657  dev->ep0state = EP0_STATUS;
1658  }
1659  }
1660  if (stat & INT_ENDPOINT0) {
1661  ACK(INT_ENDPOINT0);
1662  ep = &dev->ep[0];
1663  ep->irqs++;
1664  pio_advance(ep);
1665  }
1666 
1667  /* dma completion */
1668  if (stat & INT_MSTRDEND) { /* IN */
1669  ACK(INT_MSTRDEND);
1670  ep = &dev->ep[UDC_MSTRD_ENDPOINT];
1671  ep->irqs++;
1672  dma_advance(dev, ep);
1673  }
1674  if (stat & INT_MSTWREND) { /* OUT */
1675  ACK(INT_MSTWREND);
1676  ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1677  ep->irqs++;
1678  dma_advance(dev, ep);
1679  }
1680  if (stat & INT_MSTWRTMOUT) { /* OUT */
1681  ACK(INT_MSTWRTMOUT);
1682  ep = &dev->ep[UDC_MSTWR_ENDPOINT];
1683  ep->irqs++;
1684  ERROR(dev, "%s write timeout ?\n", ep->ep.name);
1685  // reset dma? then dma_advance()
1686  }
1687 
1688  /* pio */
1689  for (i = 1; i < 4; i++) {
1690  u32 tmp = INT_EPxDATASET(i);
1691 
1692  if (!(stat & tmp))
1693  continue;
1694  ep = &dev->ep[i];
1695  pio_advance(ep);
1696  if (list_empty (&ep->queue))
1697  pio_irq_disable(dev, regs, i);
1698  stat &= ~tmp;
1699  handled = 1;
1700  ep->irqs++;
1701  }
1702 
1703  if (rescans--)
1704  goto rescan;
1705 
1706 done:
1707  (void)readl(&regs->int_enable);
1708  spin_unlock(&dev->lock);
1709  if (stat)
1710  DBG(dev, "unhandled irq status: %05x (%05x, %05x)\n", stat,
1711  readl(&regs->int_status), dev->int_enable);
1712  return IRQ_RETVAL(handled);
1713 }
1714 
1715 #undef ACK
1716 
1717 /*-------------------------------------------------------------------------*/
1718 
1719 static void gadget_release(struct device *_dev)
1720 {
1721  struct goku_udc *dev = dev_get_drvdata(_dev);
1722 
1723  kfree(dev);
1724 }
1725 
1726 /* tear down the binding between this driver and the pci device */
1727 
1728 static void goku_remove(struct pci_dev *pdev)
1729 {
1730  struct goku_udc *dev = pci_get_drvdata(pdev);
1731 
1732  DBG(dev, "%s\n", __func__);
1733 
1734  usb_del_gadget_udc(&dev->gadget);
1735 
1736  BUG_ON(dev->driver);
1737 
1738 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1739  remove_proc_entry(proc_node_name, NULL);
1740 #endif
1741  if (dev->regs)
1742  udc_reset(dev);
1743  if (dev->got_irq)
1744  free_irq(pdev->irq, dev);
1745  if (dev->regs)
1746  iounmap(dev->regs);
1747  if (dev->got_region)
1749  pci_resource_len (pdev, 0));
1750  if (dev->enabled)
1751  pci_disable_device(pdev);
1752  if (dev->registered)
1753  device_unregister(&dev->gadget.dev);
1754 
1755  pci_set_drvdata(pdev, NULL);
1756  dev->regs = NULL;
1757  the_controller = NULL;
1758 
1759  INFO(dev, "unbind\n");
1760 }
1761 
1762 /* wrap this driver around the specified pci device, but
1763  * don't respond over USB until a gadget driver binds to us.
1764  */
1765 
1766 static int goku_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1767 {
1768  struct goku_udc *dev = NULL;
1769  unsigned long resource, len;
1770  void __iomem *base = NULL;
1771  int retval;
1772 
1773  /* if you want to support more than one controller in a system,
1774  * usb_gadget_driver_{register,unregister}() must change.
1775  */
1776  if (the_controller) {
1777  pr_warning("ignoring %s\n", pci_name(pdev));
1778  return -EBUSY;
1779  }
1780  if (!pdev->irq) {
1781  printk(KERN_ERR "Check PCI %s IRQ setup!\n", pci_name(pdev));
1782  retval = -ENODEV;
1783  goto err;
1784  }
1785 
1786  /* alloc, and start init */
1787  dev = kzalloc (sizeof *dev, GFP_KERNEL);
1788  if (dev == NULL){
1789  pr_debug("enomem %s\n", pci_name(pdev));
1790  retval = -ENOMEM;
1791  goto err;
1792  }
1793 
1794  spin_lock_init(&dev->lock);
1795  dev->pdev = pdev;
1796  dev->gadget.ops = &goku_ops;
1797  dev->gadget.max_speed = USB_SPEED_FULL;
1798 
1799  /* the "gadget" abstracts/virtualizes the controller */
1800  dev_set_name(&dev->gadget.dev, "gadget");
1801  dev->gadget.dev.parent = &pdev->dev;
1802  dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
1803  dev->gadget.dev.release = gadget_release;
1804  dev->gadget.name = driver_name;
1805 
1806  /* now all the pci goodies ... */
1807  retval = pci_enable_device(pdev);
1808  if (retval < 0) {
1809  DBG(dev, "can't enable, %d\n", retval);
1810  goto err;
1811  }
1812  dev->enabled = 1;
1813 
1814  resource = pci_resource_start(pdev, 0);
1815  len = pci_resource_len(pdev, 0);
1816  if (!request_mem_region(resource, len, driver_name)) {
1817  DBG(dev, "controller already in use\n");
1818  retval = -EBUSY;
1819  goto err;
1820  }
1821  dev->got_region = 1;
1822 
1823  base = ioremap_nocache(resource, len);
1824  if (base == NULL) {
1825  DBG(dev, "can't map memory\n");
1826  retval = -EFAULT;
1827  goto err;
1828  }
1829  dev->regs = (struct goku_udc_regs __iomem *) base;
1830 
1831  pci_set_drvdata(pdev, dev);
1832  INFO(dev, "%s\n", driver_desc);
1833  INFO(dev, "version: " DRIVER_VERSION " %s\n", dmastr());
1834  INFO(dev, "irq %d, pci mem %p\n", pdev->irq, base);
1835 
1836  /* init to known state, then setup irqs */
1837  udc_reset(dev);
1838  udc_reinit (dev);
1839  if (request_irq(pdev->irq, goku_irq, IRQF_SHARED,
1840  driver_name, dev) != 0) {
1841  DBG(dev, "request interrupt %d failed\n", pdev->irq);
1842  retval = -EBUSY;
1843  goto err;
1844  }
1845  dev->got_irq = 1;
1846  if (use_dma)
1847  pci_set_master(pdev);
1848 
1849 
1850 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1851  create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev);
1852 #endif
1853 
1854  the_controller = dev;
1855  retval = device_register(&dev->gadget.dev);
1856  if (retval) {
1857  put_device(&dev->gadget.dev);
1858  goto err;
1859  }
1860  dev->registered = 1;
1861  retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
1862  if (retval)
1863  goto err;
1864 
1865  return 0;
1866 
1867 err:
1868  if (dev)
1869  goku_remove (pdev);
1870  return retval;
1871 }
1872 
1873 
1874 /*-------------------------------------------------------------------------*/
1875 
1876 static const struct pci_device_id pci_ids[] = { {
1877  .class = ((PCI_CLASS_SERIAL_USB << 8) | 0xfe),
1878  .class_mask = ~0,
1879  .vendor = 0x102f, /* Toshiba */
1880  .device = 0x0107, /* this UDC */
1881  .subvendor = PCI_ANY_ID,
1882  .subdevice = PCI_ANY_ID,
1883 
1884 }, { /* end: all zeroes */ }
1885 };
1886 MODULE_DEVICE_TABLE (pci, pci_ids);
1887 
1888 static struct pci_driver goku_pci_driver = {
1889  .name = (char *) driver_name,
1890  .id_table = pci_ids,
1891 
1892  .probe = goku_probe,
1893  .remove = goku_remove,
1894 
1895  /* FIXME add power management support */
1896 };
1897 
1898 module_pci_driver(goku_pci_driver);