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