This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / usb / gadget / lh7a40x_udc.c
1 /*
2  * linux/drivers/usb/gadget/lh7a40x_udc.c
3  * Sharp LH7A40x on-chip full speed USB device controllers
4  *
5  * Copyright (C) 2004 Mikko Lahteenmaki, Nordic ID
6  * Copyright (C) 2004 Bo Henriksen, Nordic ID
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "lh7a40x_udc.h"
25
26 //#define DEBUG printk
27 //#define DEBUG_EP0 printk
28 //#define DEBUG_SETUP printk
29
30 #ifndef DEBUG_EP0
31 # define DEBUG_EP0(fmt,args...)
32 #endif
33 #ifndef DEBUG_SETUP
34 # define DEBUG_SETUP(fmt,args...)
35 #endif
36 #ifndef DEBUG
37 # define NO_STATES
38 # define DEBUG(fmt,args...)
39 #endif
40
41 #define DRIVER_DESC                     "LH7A40x USB Device Controller"
42 #define DRIVER_VERSION          __DATE__
43
44 #ifndef _BIT                    /* FIXME - what happended to _BIT in 2.6.7bk18? */
45 #define _BIT(x) (1<<(x))
46 #endif
47
48 struct lh7a40x_udc *the_controller;
49
50 static const char driver_name[] = "lh7a40x_udc";
51 static const char driver_desc[] = DRIVER_DESC;
52 static const char ep0name[] = "ep0-control";
53
54 /*
55   Local definintions.
56 */
57 #define UDC_PROC_FILE
58
59 #ifndef NO_STATES
60 static char *state_names[] = {
61         "WAIT_FOR_SETUP",
62         "DATA_STATE_XMIT",
63         "DATA_STATE_NEED_ZLP",
64         "WAIT_FOR_OUT_STATUS",
65         "DATA_STATE_RECV"
66 };
67 #endif
68
69 /*
70   Local declarations.
71 */
72 static int lh7a40x_ep_enable(struct usb_ep *ep,
73                              const struct usb_endpoint_descriptor *);
74 static int lh7a40x_ep_disable(struct usb_ep *ep);
75 static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep, int);
76 static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *);
77 static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned, dma_addr_t *,
78                                   int);
79 static void lh7a40x_free_buffer(struct usb_ep *ep, void *, dma_addr_t,
80                                 unsigned);
81 static int lh7a40x_queue(struct usb_ep *ep, struct usb_request *, int);
82 static int lh7a40x_dequeue(struct usb_ep *ep, struct usb_request *);
83 static int lh7a40x_set_halt(struct usb_ep *ep, int);
84 static int lh7a40x_fifo_status(struct usb_ep *ep);
85 static int lh7a40x_fifo_status(struct usb_ep *ep);
86 static void lh7a40x_fifo_flush(struct usb_ep *ep);
87 static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep);
88 static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr);
89
90 static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req,
91                  int status);
92 static void pio_irq_enable(int bEndpointAddress);
93 static void pio_irq_disable(int bEndpointAddress);
94 static void stop_activity(struct lh7a40x_udc *dev,
95                           struct usb_gadget_driver *driver);
96 static void flush(struct lh7a40x_ep *ep);
97 static void udc_enable(struct lh7a40x_udc *dev);
98 static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address);
99
100 static struct usb_ep_ops lh7a40x_ep_ops = {
101         .enable = lh7a40x_ep_enable,
102         .disable = lh7a40x_ep_disable,
103
104         .alloc_request = lh7a40x_alloc_request,
105         .free_request = lh7a40x_free_request,
106
107         .alloc_buffer = lh7a40x_alloc_buffer,
108         .free_buffer = lh7a40x_free_buffer,
109
110         .queue = lh7a40x_queue,
111         .dequeue = lh7a40x_dequeue,
112
113         .set_halt = lh7a40x_set_halt,
114         .fifo_status = lh7a40x_fifo_status,
115         .fifo_flush = lh7a40x_fifo_flush,
116 };
117
118 /* Inline code */
119
120 static __inline__ int write_packet(struct lh7a40x_ep *ep,
121                                    struct lh7a40x_request *req, int max)
122 {
123         u8 *buf;
124         int length, count;
125         volatile u32 *fifo = (volatile u32 *)ep->fifo;
126
127         buf = req->req.buf + req->req.actual;
128         prefetch(buf);
129
130         length = req->req.length - req->req.actual;
131         length = min(length, max);
132         req->req.actual += length;
133
134         DEBUG("Write %d (max %d), fifo %p\n", length, max, fifo);
135
136         count = length;
137         while (count--) {
138                 *fifo = *buf++;
139         }
140
141         return length;
142 }
143
144 static __inline__ void usb_set_index(u32 ep)
145 {
146         *(volatile u32 *)io_p2v(USB_INDEX) = ep;
147 }
148
149 static __inline__ u32 usb_read(u32 port)
150 {
151         return *(volatile u32 *)io_p2v(port);
152 }
153
154 static __inline__ void usb_write(u32 val, u32 port)
155 {
156         *(volatile u32 *)io_p2v(port) = val;
157 }
158
159 static __inline__ void usb_set(u32 val, u32 port)
160 {
161         volatile u32 *ioport = (volatile u32 *)io_p2v(port);
162         u32 after = (*ioport) | val;
163         *ioport = after;
164 }
165
166 static __inline__ void usb_clear(u32 val, u32 port)
167 {
168         volatile u32 *ioport = (volatile u32 *)io_p2v(port);
169         u32 after = (*ioport) & ~val;
170         *ioport = after;
171 }
172
173 /*-------------------------------------------------------------------------*/
174
175 #define GPIO_PORTC_DR   (0x80000E08)
176 #define GPIO_PORTC_DDR  (0x80000E18)
177 #define GPIO_PORTC_PDR  (0x80000E70)
178
179 /* get port C pin data register */
180 #define get_portc_pdr(bit)              ((usb_read(GPIO_PORTC_PDR) & _BIT(bit)) != 0)
181 /* get port C data direction register */
182 #define get_portc_ddr(bit)              ((usb_read(GPIO_PORTC_DDR) & _BIT(bit)) != 0)
183 /* set port C data register */
184 #define set_portc_dr(bit, val)  (val ? usb_set(_BIT(bit), GPIO_PORTC_DR) : usb_clear(_BIT(bit), GPIO_PORTC_DR))
185 /* set port C data direction register */
186 #define set_portc_ddr(bit, val) (val ? usb_set(_BIT(bit), GPIO_PORTC_DDR) : usb_clear(_BIT(bit), GPIO_PORTC_DDR))
187
188 /*
189  * LPD7A404 GPIO's:
190  * Port C bit 1 = USB Port 1 Power Enable
191  * Port C bit 2 = USB Port 1 Data Carrier Detect
192  */
193 #define is_usb_connected()              get_portc_pdr(2)
194
195 #ifdef UDC_PROC_FILE
196
197 static const char proc_node_name[] = "driver/udc";
198
199 static int
200 udc_proc_read(char *page, char **start, off_t off, int count,
201               int *eof, void *_dev)
202 {
203         char *buf = page;
204         struct lh7a40x_udc *dev = _dev;
205         char *next = buf;
206         unsigned size = count;
207         unsigned long flags;
208         int t;
209
210         if (off != 0)
211                 return 0;
212
213         local_irq_save(flags);
214
215         /* basic device status */
216         t = scnprintf(next, size,
217                       DRIVER_DESC "\n"
218                       "%s version: %s\n"
219                       "Gadget driver: %s\n"
220                       "Host: %s\n\n",
221                       driver_name, DRIVER_VERSION,
222                       dev->driver ? dev->driver->driver.name : "(none)",
223                       is_usb_connected()? "full speed" : "disconnected");
224         size -= t;
225         next += t;
226
227         t = scnprintf(next, size,
228                       "GPIO:\n"
229                       " Port C bit 1: %d, dir %d\n"
230                       " Port C bit 2: %d, dir %d\n\n",
231                       get_portc_pdr(1), get_portc_ddr(1),
232                       get_portc_pdr(2), get_portc_ddr(2)
233             );
234         size -= t;
235         next += t;
236
237         t = scnprintf(next, size,
238                       "DCP pullup: %d\n\n",
239                       (usb_read(USB_PM) & PM_USB_DCP) != 0);
240         size -= t;
241         next += t;
242
243         local_irq_restore(flags);
244         *eof = 1;
245         return count - size;
246 }
247
248 #define create_proc_files()     create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
249 #define remove_proc_files()     remove_proc_entry(proc_node_name, NULL)
250
251 #else                           /* !UDC_PROC_FILE */
252
253 #define create_proc_files() do {} while (0)
254 #define remove_proc_files() do {} while (0)
255
256 #endif                          /* UDC_PROC_FILE */
257
258 /*
259  *      udc_disable - disable USB device controller
260  */
261 static void udc_disable(struct lh7a40x_udc *dev)
262 {
263         DEBUG("%s, %p\n", __FUNCTION__, dev);
264
265         udc_set_address(dev, 0);
266
267         /* Disable interrupts */
268         usb_write(0, USB_IN_INT_EN);
269         usb_write(0, USB_OUT_INT_EN);
270         usb_write(0, USB_INT_EN);
271
272         /* Disable the USB */
273         usb_write(0, USB_PM);
274
275 #ifdef CONFIG_ARCH_LH7A404
276         /* Disable USB power */
277         set_portc_dr(1, 0);
278 #endif
279
280         /* if hardware supports it, disconnect from usb */
281         /* make_usb_disappear(); */
282
283         dev->ep0state = WAIT_FOR_SETUP;
284         dev->gadget.speed = USB_SPEED_UNKNOWN;
285         dev->usb_address = 0;
286 }
287
288 /*
289  *      udc_reinit - initialize software state
290  */
291 static void udc_reinit(struct lh7a40x_udc *dev)
292 {
293         u32 i;
294
295         DEBUG("%s, %p\n", __FUNCTION__, dev);
296
297         /* device/ep0 records init */
298         INIT_LIST_HEAD(&dev->gadget.ep_list);
299         INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
300         dev->ep0state = WAIT_FOR_SETUP;
301
302         /* basic endpoint records init */
303         for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
304                 struct lh7a40x_ep *ep = &dev->ep[i];
305
306                 if (i != 0)
307                         list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
308
309                 ep->desc = 0;
310                 ep->stopped = 0;
311                 INIT_LIST_HEAD(&ep->queue);
312                 ep->pio_irqs = 0;
313         }
314
315         /* the rest was statically initialized, and is read-only */
316 }
317
318 #define BYTES2MAXP(x)   (x / 8)
319 #define MAXP2BYTES(x)   (x * 8)
320
321 /* until it's enabled, this UDC should be completely invisible
322  * to any USB host.
323  */
324 static void udc_enable(struct lh7a40x_udc *dev)
325 {
326         int ep;
327
328         DEBUG("%s, %p\n", __FUNCTION__, dev);
329
330         dev->gadget.speed = USB_SPEED_UNKNOWN;
331
332 #ifdef CONFIG_ARCH_LH7A404
333         /* Set Port C bit 1 & 2 as output */
334         set_portc_ddr(1, 1);
335         set_portc_ddr(2, 1);
336
337         /* Enable USB power */
338         set_portc_dr(1, 0);
339 #endif
340
341         /*
342          * C.f Chapter 18.1.3.1 Initializing the USB
343          */
344
345         /* Disable the USB */
346         usb_clear(PM_USB_ENABLE, USB_PM);
347
348         /* Reset APB & I/O sides of the USB */
349         usb_set(USB_RESET_APB | USB_RESET_IO, USB_RESET);
350         mdelay(5);
351         usb_clear(USB_RESET_APB | USB_RESET_IO, USB_RESET);
352
353         /* Set MAXP values for each */
354         for (ep = 0; ep < UDC_MAX_ENDPOINTS; ep++) {
355                 struct lh7a40x_ep *ep_reg = &dev->ep[ep];
356                 u32 csr;
357
358                 usb_set_index(ep);
359
360                 switch (ep_reg->ep_type) {
361                 case ep_bulk_in:
362                 case ep_interrupt:
363                         usb_clear(USB_IN_CSR2_USB_DMA_EN | USB_IN_CSR2_AUTO_SET,
364                                   ep_reg->csr2);
365                         /* Fall through */
366                 case ep_control:
367                         usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
368                                   USB_IN_MAXP);
369                         break;
370                 case ep_bulk_out:
371                         usb_clear(USB_OUT_CSR2_USB_DMA_EN |
372                                   USB_OUT_CSR2_AUTO_CLR, ep_reg->csr2);
373                         usb_write(BYTES2MAXP(ep_maxpacket(ep_reg)),
374                                   USB_OUT_MAXP);
375                         break;
376                 }
377
378                 /* Read & Write CSR1, just in case */
379                 csr = usb_read(ep_reg->csr1);
380                 usb_write(csr, ep_reg->csr1);
381
382                 flush(ep_reg);
383         }
384
385         /* Disable interrupts */
386         usb_write(0, USB_IN_INT_EN);
387         usb_write(0, USB_OUT_INT_EN);
388         usb_write(0, USB_INT_EN);
389
390         /* Enable interrupts */
391         usb_set(USB_IN_INT_EP0, USB_IN_INT_EN);
392         usb_set(USB_INT_RESET_INT | USB_INT_RESUME_INT, USB_INT_EN);
393         /* Dont enable rest of the interrupts */
394         /* usb_set(USB_IN_INT_EP3 | USB_IN_INT_EP1 | USB_IN_INT_EP0, USB_IN_INT_EN);
395            usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN); */
396
397         /* Enable SUSPEND */
398         usb_set(PM_ENABLE_SUSPEND, USB_PM);
399
400         /* Enable the USB */
401         usb_set(PM_USB_ENABLE, USB_PM);
402
403 #ifdef CONFIG_ARCH_LH7A404
404         /* NOTE: DOES NOT WORK! */
405         /* Let host detect UDC:
406          * Software must write a 0 to the PMR:DCP_CTRL bit to turn this
407          * transistor on and pull the USBDP pin HIGH.
408          */
409         /* usb_clear(PM_USB_DCP, USB_PM);
410            usb_set(PM_USB_DCP, USB_PM); */
411 #endif
412 }
413
414 /*
415   Register entry point for the peripheral controller driver.
416 */
417 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
418 {
419         struct lh7a40x_udc *dev = the_controller;
420         int retval;
421
422         DEBUG("%s: %s\n", __FUNCTION__, driver->driver.name);
423
424         if (!driver
425             || driver->speed != USB_SPEED_FULL
426             || !driver->bind
427             || !driver->unbind || !driver->disconnect || !driver->setup)
428                 return -EINVAL;
429         if (!dev)
430                 return -ENODEV;
431         if (dev->driver)
432                 return -EBUSY;
433
434         /* first hook up the driver ... */
435         dev->driver = driver;
436         dev->gadget.dev.driver = &driver->driver;
437
438         device_add(&dev->gadget.dev);
439         retval = driver->bind(&dev->gadget);
440         if (retval) {
441                 printk("%s: bind to driver %s --> error %d\n", dev->gadget.name,
442                        driver->driver.name, retval);
443                 device_del(&dev->gadget.dev);
444
445                 dev->driver = 0;
446                 dev->gadget.dev.driver = 0;
447                 return retval;
448         }
449
450         /* ... then enable host detection and ep0; and we're ready
451          * for set_configuration as well as eventual disconnect.
452          * NOTE:  this shouldn't power up until later.
453          */
454         printk("%s: registered gadget driver '%s'\n", dev->gadget.name,
455                driver->driver.name);
456
457         udc_enable(dev);
458
459         return 0;
460 }
461
462 EXPORT_SYMBOL(usb_gadget_register_driver);
463
464 /*
465   Unregister entry point for the peripheral controller driver.
466 */
467 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
468 {
469         struct lh7a40x_udc *dev = the_controller;
470         unsigned long flags;
471
472         if (!dev)
473                 return -ENODEV;
474         if (!driver || driver != dev->driver)
475                 return -EINVAL;
476
477         spin_lock_irqsave(&dev->lock, flags);
478         dev->driver = 0;
479         stop_activity(dev, driver);
480         spin_unlock_irqrestore(&dev->lock, flags);
481
482         driver->unbind(&dev->gadget);
483         device_del(&dev->gadget.dev);
484
485         udc_disable(dev);
486
487         DEBUG("unregistered gadget driver '%s'\n", driver->driver.name);
488         return 0;
489 }
490
491 EXPORT_SYMBOL(usb_gadget_unregister_driver);
492
493 /*-------------------------------------------------------------------------*/
494
495 /** Write request to FIFO (max write == maxp size)
496  *  Return:  0 = still running, 1 = completed, negative = errno
497  *  NOTE: INDEX register must be set for EP
498  */
499 static int write_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
500 {
501         u32 max;
502         u32 csr;
503
504         max = le16_to_cpu(ep->desc->wMaxPacketSize);
505
506         csr = usb_read(ep->csr1);
507         DEBUG("CSR: %x %d\n", csr, csr & USB_IN_CSR1_FIFO_NOT_EMPTY);
508
509         if (!(csr & USB_IN_CSR1_FIFO_NOT_EMPTY)) {
510                 unsigned count;
511                 int is_last, is_short;
512
513                 count = write_packet(ep, req, max);
514                 usb_set(USB_IN_CSR1_IN_PKT_RDY, ep->csr1);
515
516                 /* last packet is usually short (or a zlp) */
517                 if (unlikely(count != max))
518                         is_last = is_short = 1;
519                 else {
520                         if (likely(req->req.length != req->req.actual)
521                             || req->req.zero)
522                                 is_last = 0;
523                         else
524                                 is_last = 1;
525                         /* interrupt/iso maxpacket may not fill the fifo */
526                         is_short = unlikely(max < ep_maxpacket(ep));
527                 }
528
529                 DEBUG("%s: wrote %s %d bytes%s%s %d left %p\n", __FUNCTION__,
530                       ep->ep.name, count,
531                       is_last ? "/L" : "", is_short ? "/S" : "",
532                       req->req.length - req->req.actual, req);
533
534                 /* requests complete when all IN data is in the FIFO */
535                 if (is_last) {
536                         done(ep, req, 0);
537                         if (list_empty(&ep->queue)) {
538                                 pio_irq_disable(ep_index(ep));
539                         }
540                         return 1;
541                 }
542         } else {
543                 DEBUG("Hmm.. %d ep FIFO is not empty!\n", ep_index(ep));
544         }
545
546         return 0;
547 }
548
549 /** Read to request from FIFO (max read == bytes in fifo)
550  *  Return:  0 = still running, 1 = completed, negative = errno
551  *  NOTE: INDEX register must be set for EP
552  */
553 static int read_fifo(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
554 {
555         u32 csr;
556         u8 *buf;
557         unsigned bufferspace, count, is_short;
558         volatile u32 *fifo = (volatile u32 *)ep->fifo;
559
560         /* make sure there's a packet in the FIFO. */
561         csr = usb_read(ep->csr1);
562         if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY)) {
563                 DEBUG("%s: Packet NOT ready!\n", __FUNCTION__);
564                 return -EINVAL;
565         }
566
567         buf = req->req.buf + req->req.actual;
568         prefetchw(buf);
569         bufferspace = req->req.length - req->req.actual;
570
571         /* read all bytes from this packet */
572         count = usb_read(USB_OUT_FIFO_WC1);
573         req->req.actual += min(count, bufferspace);
574
575         is_short = (count < ep->ep.maxpacket);
576         DEBUG("read %s %02x, %d bytes%s req %p %d/%d\n",
577               ep->ep.name, csr, count,
578               is_short ? "/S" : "", req, req->req.actual, req->req.length);
579
580         while (likely(count-- != 0)) {
581                 u8 byte = (u8) (*fifo & 0xff);
582
583                 if (unlikely(bufferspace == 0)) {
584                         /* this happens when the driver's buffer
585                          * is smaller than what the host sent.
586                          * discard the extra data.
587                          */
588                         if (req->req.status != -EOVERFLOW)
589                                 printk("%s overflow %d\n", ep->ep.name, count);
590                         req->req.status = -EOVERFLOW;
591                 } else {
592                         *buf++ = byte;
593                         bufferspace--;
594                 }
595         }
596
597         usb_clear(USB_OUT_CSR1_OUT_PKT_RDY, ep->csr1);
598
599         /* completion */
600         if (is_short || req->req.actual == req->req.length) {
601                 done(ep, req, 0);
602                 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
603
604                 if (list_empty(&ep->queue))
605                         pio_irq_disable(ep_index(ep));
606                 return 1;
607         }
608
609         /* finished that packet.  the next one may be waiting... */
610         return 0;
611 }
612
613 /*
614  *      done - retire a request; caller blocked irqs
615  *  INDEX register is preserved to keep same
616  */
617 static void done(struct lh7a40x_ep *ep, struct lh7a40x_request *req, int status)
618 {
619         unsigned int stopped = ep->stopped;
620         u32 index;
621
622         DEBUG("%s, %p\n", __FUNCTION__, ep);
623         list_del_init(&req->queue);
624
625         if (likely(req->req.status == -EINPROGRESS))
626                 req->req.status = status;
627         else
628                 status = req->req.status;
629
630         if (status && status != -ESHUTDOWN)
631                 DEBUG("complete %s req %p stat %d len %u/%u\n",
632                       ep->ep.name, &req->req, status,
633                       req->req.actual, req->req.length);
634
635         /* don't modify queue heads during completion callback */
636         ep->stopped = 1;
637         /* Read current index (completion may modify it) */
638         index = usb_read(USB_INDEX);
639
640         spin_unlock(&ep->dev->lock);
641         req->req.complete(&ep->ep, &req->req);
642         spin_lock(&ep->dev->lock);
643
644         /* Restore index */
645         usb_set_index(index);
646         ep->stopped = stopped;
647 }
648
649 /** Enable EP interrupt */
650 static void pio_irq_enable(int ep)
651 {
652         DEBUG("%s: %d\n", __FUNCTION__, ep);
653
654         switch (ep) {
655         case 1:
656                 usb_set(USB_IN_INT_EP1, USB_IN_INT_EN);
657                 break;
658         case 2:
659                 usb_set(USB_OUT_INT_EP2, USB_OUT_INT_EN);
660                 break;
661         case 3:
662                 usb_set(USB_IN_INT_EP3, USB_IN_INT_EN);
663                 break;
664         default:
665                 DEBUG("Unknown endpoint: %d\n", ep);
666                 break;
667         }
668 }
669
670 /** Disable EP interrupt */
671 static void pio_irq_disable(int ep)
672 {
673         DEBUG("%s: %d\n", __FUNCTION__, ep);
674
675         switch (ep) {
676         case 1:
677                 usb_clear(USB_IN_INT_EP1, USB_IN_INT_EN);
678                 break;
679         case 2:
680                 usb_clear(USB_OUT_INT_EP2, USB_OUT_INT_EN);
681                 break;
682         case 3:
683                 usb_clear(USB_IN_INT_EP3, USB_IN_INT_EN);
684                 break;
685         default:
686                 DEBUG("Unknown endpoint: %d\n", ep);
687                 break;
688         }
689 }
690
691 /*
692  *      nuke - dequeue ALL requests
693  */
694 void nuke(struct lh7a40x_ep *ep, int status)
695 {
696         struct lh7a40x_request *req;
697
698         DEBUG("%s, %p\n", __FUNCTION__, ep);
699
700         /* Flush FIFO */
701         flush(ep);
702
703         /* called with irqs blocked */
704         while (!list_empty(&ep->queue)) {
705                 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
706                 done(ep, req, status);
707         }
708
709         /* Disable IRQ if EP is enabled (has decriptor) */
710         if (ep->desc)
711                 pio_irq_disable(ep_index(ep));
712 }
713
714 /*
715 void nuke_all(struct lh7a40x_udc *dev)
716 {
717         int n;
718         for(n=0; n<UDC_MAX_ENDPOINTS; n++) {
719                 struct lh7a40x_ep *ep = &dev->ep[n];
720                 usb_set_index(n);
721                 nuke(ep, 0);
722         }
723 }*/
724
725 /*
726 static void flush_all(struct lh7a40x_udc *dev)
727 {
728         int n;
729     for (n = 0; n < UDC_MAX_ENDPOINTS; n++)
730     {
731                 struct lh7a40x_ep *ep = &dev->ep[n];
732                 flush(ep);
733     }
734 }
735 */
736
737 /** Flush EP
738  * NOTE: INDEX register must be set before this call
739  */
740 static void flush(struct lh7a40x_ep *ep)
741 {
742         DEBUG("%s, %p\n", __FUNCTION__, ep);
743
744         switch (ep->ep_type) {
745         case ep_control:
746                 /* check, by implication c.f. 15.1.2.11 */
747                 break;
748
749         case ep_bulk_in:
750         case ep_interrupt:
751                 /* if(csr & USB_IN_CSR1_IN_PKT_RDY) */
752                 usb_set(USB_IN_CSR1_FIFO_FLUSH, ep->csr1);
753                 break;
754
755         case ep_bulk_out:
756                 /* if(csr & USB_OUT_CSR1_OUT_PKT_RDY) */
757                 usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1);
758                 break;
759         }
760 }
761
762 /**
763  * lh7a40x_in_epn - handle IN interrupt
764  */
765 static void lh7a40x_in_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
766 {
767         u32 csr;
768         struct lh7a40x_ep *ep = &dev->ep[ep_idx];
769         struct lh7a40x_request *req;
770
771         usb_set_index(ep_idx);
772
773         csr = usb_read(ep->csr1);
774         DEBUG("%s: %d, csr %x\n", __FUNCTION__, ep_idx, csr);
775
776         if (csr & USB_IN_CSR1_SENT_STALL) {
777                 DEBUG("USB_IN_CSR1_SENT_STALL\n");
778                 usb_set(USB_IN_CSR1_SENT_STALL /*|USB_IN_CSR1_SEND_STALL */ ,
779                         ep->csr1);
780                 return;
781         }
782
783         if (!ep->desc) {
784                 DEBUG("%s: NO EP DESC\n", __FUNCTION__);
785                 return;
786         }
787
788         if (list_empty(&ep->queue))
789                 req = 0;
790         else
791                 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
792
793         DEBUG("req: %p\n", req);
794
795         if (!req)
796                 return;
797
798         write_fifo(ep, req);
799 }
800
801 /* ********************************************************************************************* */
802 /* Bulk OUT (recv)
803  */
804
805 static void lh7a40x_out_epn(struct lh7a40x_udc *dev, u32 ep_idx, u32 intr)
806 {
807         struct lh7a40x_ep *ep = &dev->ep[ep_idx];
808         struct lh7a40x_request *req;
809
810         DEBUG("%s: %d\n", __FUNCTION__, ep_idx);
811
812         usb_set_index(ep_idx);
813
814         if (ep->desc) {
815                 u32 csr;
816                 csr = usb_read(ep->csr1);
817
818                 while ((csr =
819                         usb_read(ep->
820                                  csr1)) & (USB_OUT_CSR1_OUT_PKT_RDY |
821                                            USB_OUT_CSR1_SENT_STALL)) {
822                         DEBUG("%s: %x\n", __FUNCTION__, csr);
823
824                         if (csr & USB_OUT_CSR1_SENT_STALL) {
825                                 DEBUG("%s: stall sent, flush fifo\n",
826                                       __FUNCTION__);
827                                 /* usb_set(USB_OUT_CSR1_FIFO_FLUSH, ep->csr1); */
828                                 flush(ep);
829                         } else if (csr & USB_OUT_CSR1_OUT_PKT_RDY) {
830                                 if (list_empty(&ep->queue))
831                                         req = 0;
832                                 else
833                                         req =
834                                             list_entry(ep->queue.next,
835                                                        struct lh7a40x_request,
836                                                        queue);
837
838                                 if (!req) {
839                                         printk("%s: NULL REQ %d\n",
840                                                __FUNCTION__, ep_idx);
841                                         flush(ep);
842                                         break;
843                                 } else {
844                                         read_fifo(ep, req);
845                                 }
846                         }
847
848                 }
849
850         } else {
851                 /* Throw packet away.. */
852                 printk("%s: No descriptor?!?\n", __FUNCTION__);
853                 flush(ep);
854         }
855 }
856
857 static void stop_activity(struct lh7a40x_udc *dev,
858                           struct usb_gadget_driver *driver)
859 {
860         int i;
861
862         /* don't disconnect drivers more than once */
863         if (dev->gadget.speed == USB_SPEED_UNKNOWN)
864                 driver = 0;
865         dev->gadget.speed = USB_SPEED_UNKNOWN;
866
867         /* prevent new request submissions, kill any outstanding requests  */
868         for (i = 0; i < UDC_MAX_ENDPOINTS; i++) {
869                 struct lh7a40x_ep *ep = &dev->ep[i];
870                 ep->stopped = 1;
871
872                 usb_set_index(i);
873                 nuke(ep, -ESHUTDOWN);
874         }
875
876         /* report disconnect; the driver is already quiesced */
877         if (driver) {
878                 spin_unlock(&dev->lock);
879                 driver->disconnect(&dev->gadget);
880                 spin_lock(&dev->lock);
881         }
882
883         /* re-init driver-visible data structures */
884         udc_reinit(dev);
885 }
886
887 /** Handle USB RESET interrupt
888  */
889 static void lh7a40x_reset_intr(struct lh7a40x_udc *dev)
890 {
891 #if 0                           /* def CONFIG_ARCH_LH7A404 */
892         /* Does not work always... */
893
894         DEBUG("%s: %d\n", __FUNCTION__, dev->usb_address);
895
896         if (!dev->usb_address) {
897                 /*usb_set(USB_RESET_IO, USB_RESET);
898                    mdelay(5);
899                    usb_clear(USB_RESET_IO, USB_RESET); */
900                 return;
901         }
902         /* Put the USB controller into reset. */
903         usb_set(USB_RESET_IO, USB_RESET);
904
905         /* Set Device ID to 0 */
906         udc_set_address(dev, 0);
907
908         /* Let PLL2 settle down */
909         mdelay(5);
910
911         /* Release the USB controller from reset */
912         usb_clear(USB_RESET_IO, USB_RESET);
913
914         /* Re-enable UDC */
915         udc_enable(dev);
916
917 #endif
918         dev->gadget.speed = USB_SPEED_FULL;
919 }
920
921 /*
922  *      lh7a40x usb client interrupt handler.
923  */
924 static irqreturn_t lh7a40x_udc_irq(int irq, void *_dev, struct pt_regs *r)
925 {
926         struct lh7a40x_udc *dev = _dev;
927
928         DEBUG("\n\n");
929
930         spin_lock(&dev->lock);
931
932         for (;;) {
933                 u32 intr_in = usb_read(USB_IN_INT);
934                 u32 intr_out = usb_read(USB_OUT_INT);
935                 u32 intr_int = usb_read(USB_INT);
936
937                 /* Test also against enable bits.. (lh7a40x errata).. Sigh.. */
938                 u32 in_en = usb_read(USB_IN_INT_EN);
939                 u32 out_en = usb_read(USB_OUT_INT_EN);
940
941                 if (!intr_out && !intr_in && !intr_int)
942                         break;
943
944                 DEBUG("%s (on state %s)\n", __FUNCTION__,
945                       state_names[dev->ep0state]);
946                 DEBUG("intr_out = %x\n", intr_out);
947                 DEBUG("intr_in  = %x\n", intr_in);
948                 DEBUG("intr_int = %x\n", intr_int);
949
950                 if (intr_in) {
951                         usb_write(intr_in, USB_IN_INT);
952
953                         if ((intr_in & USB_IN_INT_EP1)
954                             && (in_en & USB_IN_INT_EP1)) {
955                                 DEBUG("USB_IN_INT_EP1\n");
956                                 lh7a40x_in_epn(dev, 1, intr_in);
957                         }
958                         if ((intr_in & USB_IN_INT_EP3)
959                             && (in_en & USB_IN_INT_EP3)) {
960                                 DEBUG("USB_IN_INT_EP3\n");
961                                 lh7a40x_in_epn(dev, 3, intr_in);
962                         }
963                         if (intr_in & USB_IN_INT_EP0) {
964                                 DEBUG("USB_IN_INT_EP0 (control)\n");
965                                 lh7a40x_handle_ep0(dev, intr_in);
966                         }
967                 }
968
969                 if (intr_out) {
970                         usb_write(intr_out, USB_OUT_INT);
971
972                         if ((intr_out & USB_OUT_INT_EP2)
973                             && (out_en & USB_OUT_INT_EP2)) {
974                                 DEBUG("USB_OUT_INT_EP2\n");
975                                 lh7a40x_out_epn(dev, 2, intr_out);
976                         }
977                 }
978
979                 if (intr_int) {
980                         usb_write(intr_int, USB_INT);
981
982                         if (intr_int & USB_INT_RESET_INT) {
983                                 lh7a40x_reset_intr(dev);
984                         }
985
986                         if (intr_int & USB_INT_RESUME_INT) {
987                                 DEBUG("USB resume\n");
988
989                                 if (dev->gadget.speed != USB_SPEED_UNKNOWN
990                                     && dev->driver
991                                     && dev->driver->resume
992                                     && is_usb_connected()) {
993                                         dev->driver->resume(&dev->gadget);
994                                 }
995                         }
996
997                         if (intr_int & USB_INT_SUSPEND_INT) {
998                                 DEBUG("USB suspend%s\n",
999                                       is_usb_connected()? "" : "+disconnect");
1000                                 if (!is_usb_connected()) {
1001                                         stop_activity(dev, dev->driver);
1002                                 } else if (dev->gadget.speed !=
1003                                            USB_SPEED_UNKNOWN && dev->driver
1004                                            && dev->driver->suspend) {
1005                                         dev->driver->suspend(&dev->gadget);
1006                                 }
1007                         }
1008
1009                 }
1010         }
1011
1012         spin_unlock(&dev->lock);
1013
1014         return IRQ_HANDLED;
1015 }
1016
1017 static int lh7a40x_ep_enable(struct usb_ep *_ep,
1018                              const struct usb_endpoint_descriptor *desc)
1019 {
1020         struct lh7a40x_ep *ep;
1021         struct lh7a40x_udc *dev;
1022         unsigned long flags;
1023
1024         DEBUG("%s, %p\n", __FUNCTION__, _ep);
1025
1026         ep = container_of(_ep, struct lh7a40x_ep, ep);
1027         if (!_ep || !desc || ep->desc || _ep->name == ep0name
1028             || desc->bDescriptorType != USB_DT_ENDPOINT
1029             || ep->bEndpointAddress != desc->bEndpointAddress
1030             || ep_maxpacket(ep) < le16_to_cpu(desc->wMaxPacketSize)) {
1031                 DEBUG("%s, bad ep or descriptor\n", __FUNCTION__);
1032                 return -EINVAL;
1033         }
1034
1035         /* xfer types must match, except that interrupt ~= bulk */
1036         if (ep->bmAttributes != desc->bmAttributes
1037             && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
1038             && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
1039                 DEBUG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
1040                 return -EINVAL;
1041         }
1042
1043         /* hardware _could_ do smaller, but driver doesn't */
1044         if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
1045              && le16_to_cpu(desc->wMaxPacketSize) != ep_maxpacket(ep))
1046             || !desc->wMaxPacketSize) {
1047                 DEBUG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
1048                 return -ERANGE;
1049         }
1050
1051         dev = ep->dev;
1052         if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
1053                 DEBUG("%s, bogus device state\n", __FUNCTION__);
1054                 return -ESHUTDOWN;
1055         }
1056
1057         spin_lock_irqsave(&ep->dev->lock, flags);
1058
1059         ep->stopped = 0;
1060         ep->desc = desc;
1061         ep->pio_irqs = 0;
1062         ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
1063
1064         /* Reset halt state (does flush) */
1065         lh7a40x_set_halt(_ep, 0);
1066
1067         spin_unlock_irqrestore(&ep->dev->lock, flags);
1068
1069         DEBUG("%s: enabled %s\n", __FUNCTION__, _ep->name);
1070         return 0;
1071 }
1072
1073 /** Disable EP
1074  *  NOTE: Sets INDEX register
1075  */
1076 static int lh7a40x_ep_disable(struct usb_ep *_ep)
1077 {
1078         struct lh7a40x_ep *ep;
1079         unsigned long flags;
1080
1081         DEBUG("%s, %p\n", __FUNCTION__, _ep);
1082
1083         ep = container_of(_ep, struct lh7a40x_ep, ep);
1084         if (!_ep || !ep->desc) {
1085                 DEBUG("%s, %s not enabled\n", __FUNCTION__,
1086                       _ep ? ep->ep.name : NULL);
1087                 return -EINVAL;
1088         }
1089
1090         spin_lock_irqsave(&ep->dev->lock, flags);
1091
1092         usb_set_index(ep_index(ep));
1093
1094         /* Nuke all pending requests (does flush) */
1095         nuke(ep, -ESHUTDOWN);
1096
1097         /* Disable ep IRQ */
1098         pio_irq_disable(ep_index(ep));
1099
1100         ep->desc = 0;
1101         ep->stopped = 1;
1102
1103         spin_unlock_irqrestore(&ep->dev->lock, flags);
1104
1105         DEBUG("%s: disabled %s\n", __FUNCTION__, _ep->name);
1106         return 0;
1107 }
1108
1109 static struct usb_request *lh7a40x_alloc_request(struct usb_ep *ep,
1110                                                  int gfp_flags)
1111 {
1112         struct lh7a40x_request *req;
1113
1114         DEBUG("%s, %p\n", __FUNCTION__, ep);
1115
1116         req = kmalloc(sizeof *req, gfp_flags);
1117         if (!req)
1118                 return 0;
1119
1120         memset(req, 0, sizeof *req);
1121         INIT_LIST_HEAD(&req->queue);
1122
1123         return &req->req;
1124 }
1125
1126 static void lh7a40x_free_request(struct usb_ep *ep, struct usb_request *_req)
1127 {
1128         struct lh7a40x_request *req;
1129
1130         DEBUG("%s, %p\n", __FUNCTION__, ep);
1131
1132         req = container_of(_req, struct lh7a40x_request, req);
1133         WARN_ON(!list_empty(&req->queue));
1134         kfree(req);
1135 }
1136
1137 static void *lh7a40x_alloc_buffer(struct usb_ep *ep, unsigned bytes,
1138                                   dma_addr_t * dma, int gfp_flags)
1139 {
1140         char *retval;
1141
1142         DEBUG("%s (%p, %d, %d)\n", __FUNCTION__, ep, bytes, gfp_flags);
1143
1144         retval = kmalloc(bytes, gfp_flags & ~(__GFP_DMA | __GFP_HIGHMEM));
1145         if (retval)
1146                 *dma = virt_to_bus(retval);
1147         return retval;
1148 }
1149
1150 static void lh7a40x_free_buffer(struct usb_ep *ep, void *buf, dma_addr_t dma,
1151                                 unsigned bytes)
1152 {
1153         DEBUG("%s, %p\n", __FUNCTION__, ep);
1154         kfree(buf);
1155 }
1156
1157 /** Queue one request
1158  *  Kickstart transfer if needed
1159  *  NOTE: Sets INDEX register
1160  */
1161 static int lh7a40x_queue(struct usb_ep *_ep, struct usb_request *_req,
1162                          int gfp_flags)
1163 {
1164         struct lh7a40x_request *req;
1165         struct lh7a40x_ep *ep;
1166         struct lh7a40x_udc *dev;
1167         unsigned long flags;
1168
1169         DEBUG("\n\n\n%s, %p\n", __FUNCTION__, _ep);
1170
1171         req = container_of(_req, struct lh7a40x_request, req);
1172         if (unlikely
1173             (!_req || !_req->complete || !_req->buf
1174              || !list_empty(&req->queue))) {
1175                 DEBUG("%s, bad params\n", __FUNCTION__);
1176                 return -EINVAL;
1177         }
1178
1179         ep = container_of(_ep, struct lh7a40x_ep, ep);
1180         if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1181                 DEBUG("%s, bad ep\n", __FUNCTION__);
1182                 return -EINVAL;
1183         }
1184
1185         dev = ep->dev;
1186         if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
1187                 DEBUG("%s, bogus device state %p\n", __FUNCTION__, dev->driver);
1188                 return -ESHUTDOWN;
1189         }
1190
1191         DEBUG("%s queue req %p, len %d buf %p\n", _ep->name, _req, _req->length,
1192               _req->buf);
1193
1194         spin_lock_irqsave(&dev->lock, flags);
1195
1196         _req->status = -EINPROGRESS;
1197         _req->actual = 0;
1198
1199         /* kickstart this i/o queue? */
1200         DEBUG("Add to %d Q %d %d\n", ep_index(ep), list_empty(&ep->queue),
1201               ep->stopped);
1202         if (list_empty(&ep->queue) && likely(!ep->stopped)) {
1203                 u32 csr;
1204
1205                 if (unlikely(ep_index(ep) == 0)) {
1206                         /* EP0 */
1207                         list_add_tail(&req->queue, &ep->queue);
1208                         lh7a40x_ep0_kick(dev, ep);
1209                         req = 0;
1210                 } else if (ep_is_in(ep)) {
1211                         /* EP1 & EP3 */
1212                         usb_set_index(ep_index(ep));
1213                         csr = usb_read(ep->csr1);
1214                         pio_irq_enable(ep_index(ep));
1215                         if ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY) == 0) {
1216                                 if (write_fifo(ep, req) == 1)
1217                                         req = 0;
1218                         }
1219                 } else {
1220                         /* EP2 */
1221                         usb_set_index(ep_index(ep));
1222                         csr = usb_read(ep->csr1);
1223                         pio_irq_enable(ep_index(ep));
1224                         if (!(csr & USB_OUT_CSR1_FIFO_FULL)) {
1225                                 if (read_fifo(ep, req) == 1)
1226                                         req = 0;
1227                         }
1228                 }
1229         }
1230
1231         /* pio or dma irq handler advances the queue. */
1232         if (likely(req != 0))
1233                 list_add_tail(&req->queue, &ep->queue);
1234
1235         spin_unlock_irqrestore(&dev->lock, flags);
1236
1237         return 0;
1238 }
1239
1240 /* dequeue JUST ONE request */
1241 static int lh7a40x_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1242 {
1243         struct lh7a40x_ep *ep;
1244         struct lh7a40x_request *req;
1245         unsigned long flags;
1246
1247         DEBUG("%s, %p\n", __FUNCTION__, _ep);
1248
1249         ep = container_of(_ep, struct lh7a40x_ep, ep);
1250         if (!_ep || ep->ep.name == ep0name)
1251                 return -EINVAL;
1252
1253         spin_lock_irqsave(&ep->dev->lock, flags);
1254
1255         /* make sure it's actually queued on this endpoint */
1256         list_for_each_entry(req, &ep->queue, queue) {
1257                 if (&req->req == _req)
1258                         break;
1259         }
1260         if (&req->req != _req) {
1261                 spin_unlock_irqrestore(&ep->dev->lock, flags);
1262                 return -EINVAL;
1263         }
1264
1265         done(ep, req, -ECONNRESET);
1266
1267         spin_unlock_irqrestore(&ep->dev->lock, flags);
1268         return 0;
1269 }
1270
1271 /** Halt specific EP
1272  *  Return 0 if success
1273  *  NOTE: Sets INDEX register to EP !
1274  */
1275 static int lh7a40x_set_halt(struct usb_ep *_ep, int value)
1276 {
1277         struct lh7a40x_ep *ep;
1278         unsigned long flags;
1279
1280         ep = container_of(_ep, struct lh7a40x_ep, ep);
1281         if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1282                 DEBUG("%s, bad ep\n", __FUNCTION__);
1283                 return -EINVAL;
1284         }
1285
1286         usb_set_index(ep_index(ep));
1287
1288         DEBUG("%s, ep %d, val %d\n", __FUNCTION__, ep_index(ep), value);
1289
1290         spin_lock_irqsave(&ep->dev->lock, flags);
1291
1292         if (ep_index(ep) == 0) {
1293                 /* EP0 */
1294                 usb_set(EP0_SEND_STALL, ep->csr1);
1295         } else if (ep_is_in(ep)) {
1296                 u32 csr = usb_read(ep->csr1);
1297                 if (value && ((csr & USB_IN_CSR1_FIFO_NOT_EMPTY)
1298                               || !list_empty(&ep->queue))) {
1299                         /*
1300                          * Attempts to halt IN endpoints will fail (returning -EAGAIN)
1301                          * if any transfer requests are still queued, or if the controller
1302                          * FIFO still holds bytes that the host hasn\92t collected.
1303                          */
1304                         spin_unlock_irqrestore(&ep->dev->lock, flags);
1305                         DEBUG
1306                             ("Attempt to halt IN endpoint failed (returning -EAGAIN) %d %d\n",
1307                              (csr & USB_IN_CSR1_FIFO_NOT_EMPTY),
1308                              !list_empty(&ep->queue));
1309                         return -EAGAIN;
1310                 }
1311                 flush(ep);
1312                 if (value)
1313                         usb_set(USB_IN_CSR1_SEND_STALL, ep->csr1);
1314                 else {
1315                         usb_clear(USB_IN_CSR1_SEND_STALL, ep->csr1);
1316                         usb_set(USB_IN_CSR1_CLR_DATA_TOGGLE, ep->csr1);
1317                 }
1318
1319         } else {
1320
1321                 flush(ep);
1322                 if (value)
1323                         usb_set(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1324                 else {
1325                         usb_clear(USB_OUT_CSR1_SEND_STALL, ep->csr1);
1326                         usb_set(USB_OUT_CSR1_CLR_DATA_REG, ep->csr1);
1327                 }
1328         }
1329
1330         if (value) {
1331                 ep->stopped = 1;
1332         } else {
1333                 ep->stopped = 0;
1334         }
1335
1336         spin_unlock_irqrestore(&ep->dev->lock, flags);
1337
1338         DEBUG("%s %s halted\n", _ep->name, value == 0 ? "NOT" : "IS");
1339
1340         return 0;
1341 }
1342
1343 /** Return bytes in EP FIFO
1344  *  NOTE: Sets INDEX register to EP
1345  */
1346 static int lh7a40x_fifo_status(struct usb_ep *_ep)
1347 {
1348         u32 csr;
1349         int count = 0;
1350         struct lh7a40x_ep *ep;
1351
1352         ep = container_of(_ep, struct lh7a40x_ep, ep);
1353         if (!_ep) {
1354                 DEBUG("%s, bad ep\n", __FUNCTION__);
1355                 return -ENODEV;
1356         }
1357
1358         DEBUG("%s, %d\n", __FUNCTION__, ep_index(ep));
1359
1360         /* LPD can't report unclaimed bytes from IN fifos */
1361         if (ep_is_in(ep))
1362                 return -EOPNOTSUPP;
1363
1364         usb_set_index(ep_index(ep));
1365
1366         csr = usb_read(ep->csr1);
1367         if (ep->dev->gadget.speed != USB_SPEED_UNKNOWN ||
1368             csr & USB_OUT_CSR1_OUT_PKT_RDY) {
1369                 count = usb_read(USB_OUT_FIFO_WC1);
1370         }
1371
1372         return count;
1373 }
1374
1375 /** Flush EP FIFO
1376  *  NOTE: Sets INDEX register to EP
1377  */
1378 static void lh7a40x_fifo_flush(struct usb_ep *_ep)
1379 {
1380         struct lh7a40x_ep *ep;
1381
1382         ep = container_of(_ep, struct lh7a40x_ep, ep);
1383         if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
1384                 DEBUG("%s, bad ep\n", __FUNCTION__);
1385                 return;
1386         }
1387
1388         usb_set_index(ep_index(ep));
1389         flush(ep);
1390 }
1391
1392 /****************************************************************/
1393 /* End Point 0 related functions                                */
1394 /****************************************************************/
1395
1396 /* return:  0 = still running, 1 = completed, negative = errno */
1397 static int write_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1398 {
1399         u32 max;
1400         unsigned count;
1401         int is_last;
1402
1403         max = ep_maxpacket(ep);
1404
1405         DEBUG_EP0("%s\n", __FUNCTION__);
1406
1407         count = write_packet(ep, req, max);
1408
1409         /* last packet is usually short (or a zlp) */
1410         if (unlikely(count != max))
1411                 is_last = 1;
1412         else {
1413                 if (likely(req->req.length != req->req.actual) || req->req.zero)
1414                         is_last = 0;
1415                 else
1416                         is_last = 1;
1417         }
1418
1419         DEBUG_EP0("%s: wrote %s %d bytes%s %d left %p\n", __FUNCTION__,
1420                   ep->ep.name, count,
1421                   is_last ? "/L" : "", req->req.length - req->req.actual, req);
1422
1423         /* requests complete when all IN data is in the FIFO */
1424         if (is_last) {
1425                 done(ep, req, 0);
1426                 return 1;
1427         }
1428
1429         return 0;
1430 }
1431
1432 static __inline__ int lh7a40x_fifo_read(struct lh7a40x_ep *ep,
1433                                         unsigned char *cp, int max)
1434 {
1435         int bytes;
1436         int count = usb_read(USB_OUT_FIFO_WC1);
1437         volatile u32 *fifo = (volatile u32 *)ep->fifo;
1438
1439         if (count > max)
1440                 count = max;
1441         bytes = count;
1442         while (count--)
1443                 *cp++ = *fifo & 0xFF;
1444         return bytes;
1445 }
1446
1447 static __inline__ void lh7a40x_fifo_write(struct lh7a40x_ep *ep,
1448                                           unsigned char *cp, int count)
1449 {
1450         volatile u32 *fifo = (volatile u32 *)ep->fifo;
1451         DEBUG_EP0("fifo_write: %d %d\n", ep_index(ep), count);
1452         while (count--)
1453                 *fifo = *cp++;
1454 }
1455
1456 static int read_fifo_ep0(struct lh7a40x_ep *ep, struct lh7a40x_request *req)
1457 {
1458         u32 csr;
1459         u8 *buf;
1460         unsigned bufferspace, count, is_short;
1461         volatile u32 *fifo = (volatile u32 *)ep->fifo;
1462
1463         DEBUG_EP0("%s\n", __FUNCTION__);
1464
1465         csr = usb_read(USB_EP0_CSR);
1466         if (!(csr & USB_OUT_CSR1_OUT_PKT_RDY))
1467                 return 0;
1468
1469         buf = req->req.buf + req->req.actual;
1470         prefetchw(buf);
1471         bufferspace = req->req.length - req->req.actual;
1472
1473         /* read all bytes from this packet */
1474         if (likely(csr & EP0_OUT_PKT_RDY)) {
1475                 count = usb_read(USB_OUT_FIFO_WC1);
1476                 req->req.actual += min(count, bufferspace);
1477         } else                  /* zlp */
1478                 count = 0;
1479
1480         is_short = (count < ep->ep.maxpacket);
1481         DEBUG_EP0("read %s %02x, %d bytes%s req %p %d/%d\n",
1482                   ep->ep.name, csr, count,
1483                   is_short ? "/S" : "", req, req->req.actual, req->req.length);
1484
1485         while (likely(count-- != 0)) {
1486                 u8 byte = (u8) (*fifo & 0xff);
1487
1488                 if (unlikely(bufferspace == 0)) {
1489                         /* this happens when the driver's buffer
1490                          * is smaller than what the host sent.
1491                          * discard the extra data.
1492                          */
1493                         if (req->req.status != -EOVERFLOW)
1494                                 DEBUG_EP0("%s overflow %d\n", ep->ep.name,
1495                                           count);
1496                         req->req.status = -EOVERFLOW;
1497                 } else {
1498                         *buf++ = byte;
1499                         bufferspace--;
1500                 }
1501         }
1502
1503         /* completion */
1504         if (is_short || req->req.actual == req->req.length) {
1505                 done(ep, req, 0);
1506                 return 1;
1507         }
1508
1509         /* finished that packet.  the next one may be waiting... */
1510         return 0;
1511 }
1512
1513 /**
1514  * udc_set_address - set the USB address for this device
1515  * @address:
1516  *
1517  * Called from control endpoint function after it decodes a set address setup packet.
1518  */
1519 static void udc_set_address(struct lh7a40x_udc *dev, unsigned char address)
1520 {
1521         DEBUG_EP0("%s: %d\n", __FUNCTION__, address);
1522         /* c.f. 15.1.2.2 Table 15-4 address will be used after DATA_END is set */
1523         dev->usb_address = address;
1524         usb_set((address & USB_FA_FUNCTION_ADDR), USB_FA);
1525         usb_set(USB_FA_ADDR_UPDATE | (address & USB_FA_FUNCTION_ADDR), USB_FA);
1526         /* usb_read(USB_FA); */
1527 }
1528
1529 /*
1530  * DATA_STATE_RECV (OUT_PKT_RDY)
1531  *      - if error
1532  *              set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1533  *      - else
1534  *              set EP0_CLR_OUT bit
1535                                 if last set EP0_DATA_END bit
1536  */
1537 static void lh7a40x_ep0_out(struct lh7a40x_udc *dev, u32 csr)
1538 {
1539         struct lh7a40x_request *req;
1540         struct lh7a40x_ep *ep = &dev->ep[0];
1541         int ret;
1542
1543         DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1544
1545         if (list_empty(&ep->queue))
1546                 req = 0;
1547         else
1548                 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1549
1550         if (req) {
1551
1552                 if (req->req.length == 0) {
1553                         DEBUG_EP0("ZERO LENGTH OUT!\n");
1554                         usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1555                         dev->ep0state = WAIT_FOR_SETUP;
1556                         return;
1557                 }
1558                 ret = read_fifo_ep0(ep, req);
1559                 if (ret) {
1560                         /* Done! */
1561                         DEBUG_EP0("%s: finished, waiting for status\n",
1562                                   __FUNCTION__);
1563
1564                         usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1565                         dev->ep0state = WAIT_FOR_SETUP;
1566                 } else {
1567                         /* Not done yet.. */
1568                         DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1569                         usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1570                 }
1571         } else {
1572                 DEBUG_EP0("NO REQ??!\n");
1573         }
1574 }
1575
1576 /*
1577  * DATA_STATE_XMIT
1578  */
1579 static int lh7a40x_ep0_in(struct lh7a40x_udc *dev, u32 csr)
1580 {
1581         struct lh7a40x_request *req;
1582         struct lh7a40x_ep *ep = &dev->ep[0];
1583         int ret, need_zlp = 0;
1584
1585         DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1586
1587         if (list_empty(&ep->queue))
1588                 req = 0;
1589         else
1590                 req = list_entry(ep->queue.next, struct lh7a40x_request, queue);
1591
1592         if (!req) {
1593                 DEBUG_EP0("%s: NULL REQ\n", __FUNCTION__);
1594                 return 0;
1595         }
1596
1597         if (req->req.length == 0) {
1598
1599                 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1600                 dev->ep0state = WAIT_FOR_SETUP;
1601                 return 1;
1602         }
1603
1604         if (req->req.length - req->req.actual == EP0_PACKETSIZE) {
1605                 /* Next write will end with the packet size, */
1606                 /* so we need Zero-length-packet */
1607                 need_zlp = 1;
1608         }
1609
1610         ret = write_fifo_ep0(ep, req);
1611
1612         if (ret == 1 && !need_zlp) {
1613                 /* Last packet */
1614                 DEBUG_EP0("%s: finished, waiting for status\n", __FUNCTION__);
1615
1616                 usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1617                 dev->ep0state = WAIT_FOR_SETUP;
1618         } else {
1619                 DEBUG_EP0("%s: not finished\n", __FUNCTION__);
1620                 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1621         }
1622
1623         if (need_zlp) {
1624                 DEBUG_EP0("%s: Need ZLP!\n", __FUNCTION__);
1625                 usb_set(EP0_IN_PKT_RDY, USB_EP0_CSR);
1626                 dev->ep0state = DATA_STATE_NEED_ZLP;
1627         }
1628
1629         return 1;
1630 }
1631
1632 static int lh7a40x_handle_get_status(struct lh7a40x_udc *dev,
1633                                      struct usb_ctrlrequest *ctrl)
1634 {
1635         struct lh7a40x_ep *ep0 = &dev->ep[0];
1636         struct lh7a40x_ep *qep;
1637         int reqtype = (ctrl->bRequestType & USB_RECIP_MASK);
1638         u16 val = 0;
1639
1640         if (reqtype == USB_RECIP_INTERFACE) {
1641                 /* This is not supported.
1642                  * And according to the USB spec, this one does nothing..
1643                  * Just return 0
1644                  */
1645                 DEBUG_SETUP("GET_STATUS: USB_RECIP_INTERFACE\n");
1646         } else if (reqtype == USB_RECIP_DEVICE) {
1647                 DEBUG_SETUP("GET_STATUS: USB_RECIP_DEVICE\n");
1648                 val |= (1 << 0);        /* Self powered */
1649                 /*val |= (1<<1); *//* Remote wakeup */
1650         } else if (reqtype == USB_RECIP_ENDPOINT) {
1651                 int ep_num = (ctrl->wIndex & ~USB_DIR_IN);
1652
1653                 DEBUG_SETUP
1654                     ("GET_STATUS: USB_RECIP_ENDPOINT (%d), ctrl->wLength = %d\n",
1655                      ep_num, ctrl->wLength);
1656
1657                 if (ctrl->wLength > 2 || ep_num > 3)
1658                         return -EOPNOTSUPP;
1659
1660                 qep = &dev->ep[ep_num];
1661                 if (ep_is_in(qep) != ((ctrl->wIndex & USB_DIR_IN) ? 1 : 0)
1662                     && ep_index(qep) != 0) {
1663                         return -EOPNOTSUPP;
1664                 }
1665
1666                 usb_set_index(ep_index(qep));
1667
1668                 /* Return status on next IN token */
1669                 switch (qep->ep_type) {
1670                 case ep_control:
1671                         val =
1672                             (usb_read(qep->csr1) & EP0_SEND_STALL) ==
1673                             EP0_SEND_STALL;
1674                         break;
1675                 case ep_bulk_in:
1676                 case ep_interrupt:
1677                         val =
1678                             (usb_read(qep->csr1) & USB_IN_CSR1_SEND_STALL) ==
1679                             USB_IN_CSR1_SEND_STALL;
1680                         break;
1681                 case ep_bulk_out:
1682                         val =
1683                             (usb_read(qep->csr1) & USB_OUT_CSR1_SEND_STALL) ==
1684                             USB_OUT_CSR1_SEND_STALL;
1685                         break;
1686                 }
1687
1688                 /* Back to EP0 index */
1689                 usb_set_index(0);
1690
1691                 DEBUG_SETUP("GET_STATUS, ep: %d (%x), val = %d\n", ep_num,
1692                             ctrl->wIndex, val);
1693         } else {
1694                 DEBUG_SETUP("Unknown REQ TYPE: %d\n", reqtype);
1695                 return -EOPNOTSUPP;
1696         }
1697
1698         /* Clear "out packet ready" */
1699         usb_set((EP0_CLR_OUT), USB_EP0_CSR);
1700         /* Put status to FIFO */
1701         lh7a40x_fifo_write(ep0, (u8 *) & val, sizeof(val));
1702         /* Issue "In packet ready" */
1703         usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1704
1705         return 0;
1706 }
1707
1708 /*
1709  * WAIT_FOR_SETUP (OUT_PKT_RDY)
1710  *      - read data packet from EP0 FIFO
1711  *      - decode command
1712  *      - if error
1713  *              set EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL bits
1714  *      - else
1715  *              set EP0_CLR_OUT | EP0_DATA_END bits
1716  */
1717 static void lh7a40x_ep0_setup(struct lh7a40x_udc *dev, u32 csr)
1718 {
1719         struct lh7a40x_ep *ep = &dev->ep[0];
1720         struct usb_ctrlrequest ctrl;
1721         int i, bytes, is_in;
1722
1723         DEBUG_SETUP("%s: %x\n", __FUNCTION__, csr);
1724
1725         /* Nuke all previous transfers */
1726         nuke(ep, -EPROTO);
1727
1728         /* read control req from fifo (8 bytes) */
1729         bytes = lh7a40x_fifo_read(ep, (unsigned char *)&ctrl, 8);
1730
1731         DEBUG_SETUP("Read CTRL REQ %d bytes\n", bytes);
1732         DEBUG_SETUP("CTRL.bRequestType = %d (is_in %d)\n", ctrl.bRequestType,
1733                     ctrl.bRequestType == USB_DIR_IN);
1734         DEBUG_SETUP("CTRL.bRequest = %d\n", ctrl.bRequest);
1735         DEBUG_SETUP("CTRL.wLength = %d\n", ctrl.wLength);
1736         DEBUG_SETUP("CTRL.wValue = %d (%d)\n", ctrl.wValue, ctrl.wValue >> 8);
1737         DEBUG_SETUP("CTRL.wIndex = %d\n", ctrl.wIndex);
1738
1739         /* Set direction of EP0 */
1740         if (likely(ctrl.bRequestType & USB_DIR_IN)) {
1741                 ep->bEndpointAddress |= USB_DIR_IN;
1742                 is_in = 1;
1743         } else {
1744                 ep->bEndpointAddress &= ~USB_DIR_IN;
1745                 is_in = 0;
1746         }
1747
1748         dev->req_pending = 1;
1749
1750         /* Handle some SETUP packets ourselves */
1751         switch (ctrl.bRequest) {
1752         case USB_REQ_SET_ADDRESS:
1753                 if (ctrl.bRequestType != (USB_TYPE_STANDARD | USB_RECIP_DEVICE))
1754                         break;
1755
1756                 DEBUG_SETUP("USB_REQ_SET_ADDRESS (%d)\n", ctrl.wValue);
1757                 udc_set_address(dev, ctrl.wValue);
1758                 usb_set((EP0_CLR_OUT | EP0_DATA_END), USB_EP0_CSR);
1759                 return;
1760
1761         case USB_REQ_GET_STATUS:{
1762                         if (lh7a40x_handle_get_status(dev, &ctrl) == 0)
1763                                 return;
1764
1765         case USB_REQ_CLEAR_FEATURE:
1766         case USB_REQ_SET_FEATURE:
1767                         if (ctrl.bRequestType == USB_RECIP_ENDPOINT) {
1768                                 struct lh7a40x_ep *qep;
1769                                 int ep_num = (ctrl.wIndex & 0x0f);
1770
1771                                 /* Support only HALT feature */
1772                                 if (ctrl.wValue != 0 || ctrl.wLength != 0
1773                                     || ep_num > 3 || ep_num < 1)
1774                                         break;
1775
1776                                 qep = &dev->ep[ep_num];
1777                                 if (ctrl.bRequest == USB_REQ_SET_FEATURE) {
1778                                         DEBUG_SETUP("SET_FEATURE (%d)\n",
1779                                                     ep_num);
1780                                         lh7a40x_set_halt(&qep->ep, 1);
1781                                 } else {
1782                                         DEBUG_SETUP("CLR_FEATURE (%d)\n",
1783                                                     ep_num);
1784                                         lh7a40x_set_halt(&qep->ep, 0);
1785                                 }
1786                                 usb_set_index(0);
1787
1788                                 /* Reply with a ZLP on next IN token */
1789                                 usb_set((EP0_CLR_OUT | EP0_DATA_END),
1790                                         USB_EP0_CSR);
1791                                 return;
1792                         }
1793                         break;
1794                 }
1795
1796         default:
1797                 break;
1798         }
1799
1800         if (likely(dev->driver)) {
1801                 /* device-2-host (IN) or no data setup command, process immediately */
1802                 spin_unlock(&dev->lock);
1803                 i = dev->driver->setup(&dev->gadget, &ctrl);
1804                 spin_lock(&dev->lock);
1805
1806                 if (i < 0) {
1807                         /* setup processing failed, force stall */
1808                         DEBUG_SETUP
1809                             ("  --> ERROR: gadget setup FAILED (stalling), setup returned %d\n",
1810                              i);
1811                         usb_set_index(0);
1812                         usb_set((EP0_CLR_OUT | EP0_DATA_END | EP0_SEND_STALL),
1813                                 USB_EP0_CSR);
1814
1815                         /* ep->stopped = 1; */
1816                         dev->ep0state = WAIT_FOR_SETUP;
1817                 }
1818         }
1819 }
1820
1821 /*
1822  * DATA_STATE_NEED_ZLP
1823  */
1824 static void lh7a40x_ep0_in_zlp(struct lh7a40x_udc *dev, u32 csr)
1825 {
1826         DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1827
1828         /* c.f. Table 15-14 */
1829         usb_set((EP0_IN_PKT_RDY | EP0_DATA_END), USB_EP0_CSR);
1830         dev->ep0state = WAIT_FOR_SETUP;
1831 }
1832
1833 /*
1834  * handle ep0 interrupt
1835  */
1836 static void lh7a40x_handle_ep0(struct lh7a40x_udc *dev, u32 intr)
1837 {
1838         struct lh7a40x_ep *ep = &dev->ep[0];
1839         u32 csr;
1840
1841         /* Set index 0 */
1842         usb_set_index(0);
1843         csr = usb_read(USB_EP0_CSR);
1844
1845         DEBUG_EP0("%s: csr = %x\n", __FUNCTION__, csr);
1846
1847         /*
1848          * For overview of what we should be doing see c.f. Chapter 18.1.2.4
1849          * We will follow that outline here modified by our own global state
1850          * indication which provides hints as to what we think should be
1851          * happening..
1852          */
1853
1854         /*
1855          * if SENT_STALL is set
1856          *      - clear the SENT_STALL bit
1857          */
1858         if (csr & EP0_SENT_STALL) {
1859                 DEBUG_EP0("%s: EP0_SENT_STALL is set: %x\n", __FUNCTION__, csr);
1860                 usb_clear((EP0_SENT_STALL | EP0_SEND_STALL), USB_EP0_CSR);
1861                 nuke(ep, -ECONNABORTED);
1862                 dev->ep0state = WAIT_FOR_SETUP;
1863                 return;
1864         }
1865
1866         /*
1867          * if a transfer is in progress && IN_PKT_RDY and OUT_PKT_RDY are clear
1868          *      - fill EP0 FIFO
1869          *      - if last packet
1870          *      -       set IN_PKT_RDY | DATA_END
1871          *      - else
1872          *              set IN_PKT_RDY
1873          */
1874         if (!(csr & (EP0_IN_PKT_RDY | EP0_OUT_PKT_RDY))) {
1875                 DEBUG_EP0("%s: IN_PKT_RDY and OUT_PKT_RDY are clear\n",
1876                           __FUNCTION__);
1877
1878                 switch (dev->ep0state) {
1879                 case DATA_STATE_XMIT:
1880                         DEBUG_EP0("continue with DATA_STATE_XMIT\n");
1881                         lh7a40x_ep0_in(dev, csr);
1882                         return;
1883                 case DATA_STATE_NEED_ZLP:
1884                         DEBUG_EP0("continue with DATA_STATE_NEED_ZLP\n");
1885                         lh7a40x_ep0_in_zlp(dev, csr);
1886                         return;
1887                 default:
1888                         /* Stall? */
1889                         DEBUG_EP0("Odd state!! state = %s\n",
1890                                   state_names[dev->ep0state]);
1891                         dev->ep0state = WAIT_FOR_SETUP;
1892                         /* nuke(ep, 0); */
1893                         /* usb_set(EP0_SEND_STALL, ep->csr1); */
1894                         break;
1895                 }
1896         }
1897
1898         /*
1899          * if SETUP_END is set
1900          *      - abort the last transfer
1901          *      - set SERVICED_SETUP_END_BIT
1902          */
1903         if (csr & EP0_SETUP_END) {
1904                 DEBUG_EP0("%s: EP0_SETUP_END is set: %x\n", __FUNCTION__, csr);
1905
1906                 usb_set(EP0_CLR_SETUP_END, USB_EP0_CSR);
1907
1908                 nuke(ep, 0);
1909                 dev->ep0state = WAIT_FOR_SETUP;
1910         }
1911
1912         /*
1913          * if EP0_OUT_PKT_RDY is set
1914          *      - read data packet from EP0 FIFO
1915          *      - decode command
1916          *      - if error
1917          *              set SERVICED_OUT_PKT_RDY | DATA_END bits | SEND_STALL
1918          *      - else
1919          *              set SERVICED_OUT_PKT_RDY | DATA_END bits
1920          */
1921         if (csr & EP0_OUT_PKT_RDY) {
1922
1923                 DEBUG_EP0("%s: EP0_OUT_PKT_RDY is set: %x\n", __FUNCTION__,
1924                           csr);
1925
1926                 switch (dev->ep0state) {
1927                 case WAIT_FOR_SETUP:
1928                         DEBUG_EP0("WAIT_FOR_SETUP\n");
1929                         lh7a40x_ep0_setup(dev, csr);
1930                         break;
1931
1932                 case DATA_STATE_RECV:
1933                         DEBUG_EP0("DATA_STATE_RECV\n");
1934                         lh7a40x_ep0_out(dev, csr);
1935                         break;
1936
1937                 default:
1938                         /* send stall? */
1939                         DEBUG_EP0("strange state!! 2. send stall? state = %d\n",
1940                                   dev->ep0state);
1941                         break;
1942                 }
1943         }
1944 }
1945
1946 static void lh7a40x_ep0_kick(struct lh7a40x_udc *dev, struct lh7a40x_ep *ep)
1947 {
1948         u32 csr;
1949
1950         usb_set_index(0);
1951         csr = usb_read(USB_EP0_CSR);
1952
1953         DEBUG_EP0("%s: %x\n", __FUNCTION__, csr);
1954
1955         /* Clear "out packet ready" */
1956         usb_set(EP0_CLR_OUT, USB_EP0_CSR);
1957
1958         if (ep_is_in(ep)) {
1959                 dev->ep0state = DATA_STATE_XMIT;
1960                 lh7a40x_ep0_in(dev, csr);
1961         } else {
1962                 dev->ep0state = DATA_STATE_RECV;
1963                 lh7a40x_ep0_out(dev, csr);
1964         }
1965 }
1966
1967 /* ---------------------------------------------------------------------------
1968  *      device-scoped parts of the api to the usb controller hardware
1969  * ---------------------------------------------------------------------------
1970  */
1971
1972 static int lh7a40x_udc_get_frame(struct usb_gadget *_gadget)
1973 {
1974         u32 frame1 = usb_read(USB_FRM_NUM1);    /* Least significant 8 bits */
1975         u32 frame2 = usb_read(USB_FRM_NUM2);    /* Most significant 3 bits */
1976         DEBUG("%s, %p\n", __FUNCTION__, _gadget);
1977         return ((frame2 & 0x07) << 8) | (frame1 & 0xff);
1978 }
1979
1980 static int lh7a40x_udc_wakeup(struct usb_gadget *_gadget)
1981 {
1982         /* host may not have enabled remote wakeup */
1983         /*if ((UDCCS0 & UDCCS0_DRWF) == 0)
1984            return -EHOSTUNREACH;
1985            udc_set_mask_UDCCR(UDCCR_RSM); */
1986         return -ENOTSUPP;
1987 }
1988
1989 static const struct usb_gadget_ops lh7a40x_udc_ops = {
1990         .get_frame = lh7a40x_udc_get_frame,
1991         .wakeup = lh7a40x_udc_wakeup,
1992         /* current versions must always be self-powered */
1993 };
1994
1995 static void nop_release(struct device *dev)
1996 {
1997         DEBUG("%s %s\n", __FUNCTION__, dev->bus_id);
1998 }
1999
2000 static struct lh7a40x_udc memory = {
2001         .usb_address = 0,
2002
2003         .gadget = {
2004                    .ops = &lh7a40x_udc_ops,
2005                    .ep0 = &memory.ep[0].ep,
2006                    .name = driver_name,
2007                    .dev = {
2008                            .bus_id = "gadget",
2009                            .release = nop_release,
2010                            },
2011                    },
2012
2013         /* control endpoint */
2014         .ep[0] = {
2015                   .ep = {
2016                          .name = ep0name,
2017                          .ops = &lh7a40x_ep_ops,
2018                          .maxpacket = EP0_PACKETSIZE,
2019                          },
2020                   .dev = &memory,
2021
2022                   .bEndpointAddress = 0,
2023                   .bmAttributes = 0,
2024
2025                   .ep_type = ep_control,
2026                   .fifo = io_p2v(USB_EP0_FIFO),
2027                   .csr1 = USB_EP0_CSR,
2028                   .csr2 = USB_EP0_CSR,
2029                   },
2030
2031         /* first group of endpoints */
2032         .ep[1] = {
2033                   .ep = {
2034                          .name = "ep1in-bulk",
2035                          .ops = &lh7a40x_ep_ops,
2036                          .maxpacket = 64,
2037                          },
2038                   .dev = &memory,
2039
2040                   .bEndpointAddress = USB_DIR_IN | 1,
2041                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
2042
2043                   .ep_type = ep_bulk_in,
2044                   .fifo = io_p2v(USB_EP1_FIFO),
2045                   .csr1 = USB_IN_CSR1,
2046                   .csr2 = USB_IN_CSR2,
2047                   },
2048
2049         .ep[2] = {
2050                   .ep = {
2051                          .name = "ep2out-bulk",
2052                          .ops = &lh7a40x_ep_ops,
2053                          .maxpacket = 64,
2054                          },
2055                   .dev = &memory,
2056
2057                   .bEndpointAddress = 2,
2058                   .bmAttributes = USB_ENDPOINT_XFER_BULK,
2059
2060                   .ep_type = ep_bulk_out,
2061                   .fifo = io_p2v(USB_EP2_FIFO),
2062                   .csr1 = USB_OUT_CSR1,
2063                   .csr2 = USB_OUT_CSR2,
2064                   },
2065
2066         .ep[3] = {
2067                   .ep = {
2068                          .name = "ep3in-int",
2069                          .ops = &lh7a40x_ep_ops,
2070                          .maxpacket = 64,
2071                          },
2072                   .dev = &memory,
2073
2074                   .bEndpointAddress = USB_DIR_IN | 3,
2075                   .bmAttributes = USB_ENDPOINT_XFER_INT,
2076
2077                   .ep_type = ep_interrupt,
2078                   .fifo = io_p2v(USB_EP3_FIFO),
2079                   .csr1 = USB_IN_CSR1,
2080                   .csr2 = USB_IN_CSR2,
2081                   },
2082 };
2083
2084 /*
2085  *      probe - binds to the platform device
2086  */
2087 static int lh7a40x_udc_probe(struct device *_dev)
2088 {
2089         struct lh7a40x_udc *dev = &memory;
2090         int retval;
2091
2092         DEBUG("%s: %p\n", __FUNCTION__, _dev);
2093
2094         spin_lock_init(&dev->lock);
2095         dev->dev = _dev;
2096
2097         device_initialize(&dev->gadget.dev);
2098         dev->gadget.dev.parent = _dev;
2099
2100         the_controller = dev;
2101         dev_set_drvdata(_dev, dev);
2102
2103         udc_disable(dev);
2104         udc_reinit(dev);
2105
2106         /* irq setup after old hardware state is cleaned up */
2107         retval =
2108             request_irq(IRQ_USBINTR, lh7a40x_udc_irq, SA_INTERRUPT, driver_name,
2109                         dev);
2110         if (retval != 0) {
2111                 DEBUG(KERN_ERR "%s: can't get irq %i, err %d\n", driver_name,
2112                       IRQ_USBINTR, retval);
2113                 return -EBUSY;
2114         }
2115
2116         create_proc_files();
2117
2118         return retval;
2119 }
2120
2121 static int lh7a40x_udc_remove(struct device *_dev)
2122 {
2123         struct lh7a40x_udc *dev = _dev->driver_data;
2124
2125         DEBUG("%s: %p\n", __FUNCTION__, dev);
2126
2127         udc_disable(dev);
2128         remove_proc_files();
2129         usb_gadget_unregister_driver(dev->driver);
2130
2131         free_irq(IRQ_USBINTR, dev);
2132
2133         dev_set_drvdata(_dev, 0);
2134
2135         the_controller = 0;
2136
2137         return 0;
2138 }
2139
2140 /*-------------------------------------------------------------------------*/
2141
2142 static struct device_driver udc_driver = {
2143         .name = (char *)driver_name,
2144         .bus = &platform_bus_type,
2145         .probe = lh7a40x_udc_probe,
2146         .remove = lh7a40x_udc_remove
2147             /* FIXME power management support */
2148             /* .suspend = ... disable UDC */
2149             /* .resume = ... re-enable UDC */
2150 };
2151
2152 static int __init udc_init(void)
2153 {
2154         DEBUG("%s: %s version %s\n", __FUNCTION__, driver_name, DRIVER_VERSION);
2155         return driver_register(&udc_driver);
2156 }
2157
2158 static void __exit udc_exit(void)
2159 {
2160         driver_unregister(&udc_driver);
2161 }
2162
2163 module_init(udc_init);
2164 module_exit(udc_exit);
2165
2166 MODULE_DESCRIPTION(DRIVER_DESC);
2167 MODULE_AUTHOR("Mikko Lahteenmaki, Bo Henriksen");
2168 MODULE_LICENSE("GPL");