ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / au1000 / common / usbdev.c
1 /*
2  * BRIEF MODULE DESCRIPTION
3  *      Au1000 USB Device-Side (device layer)
4  *
5  * Copyright 2001-2002 MontaVista Software Inc.
6  * Author: MontaVista Software, Inc.
7  *              stevel@mvista.com or source@mvista.com
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  *
14  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
15  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
16  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
17  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
18  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
20  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
22  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *  You should have received a copy of the  GNU General Public License along
26  *  with this program; if not, write  to the Free Software Foundation, Inc.,
27  *  675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29 #include <linux/kernel.h>
30 #include <linux/ioport.h>
31 #include <linux/sched.h>
32 #include <linux/signal.h>
33 #include <linux/errno.h>
34 #include <linux/poll.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/fcntl.h>
38 #include <linux/module.h>
39 #include <linux/spinlock.h>
40 #include <linux/list.h>
41 #include <linux/smp_lock.h>
42 #define DEBUG
43 #include <linux/usb.h>
44
45 #include <asm/io.h>
46 #include <asm/uaccess.h>
47 #include <asm/irq.h>
48 #include <asm/mipsregs.h>
49 #include <asm/au1000.h>
50 #include <asm/au1000_dma.h>
51 #include <asm/au1000_usbdev.h>
52
53 #ifdef DEBUG
54 #undef VDEBUG
55 #ifdef VDEBUG
56 #define vdbg(fmt, arg...) printk(KERN_DEBUG __FILE__ ": " fmt "\n" , ## arg)
57 #else
58 #define vdbg(fmt, arg...) do {} while (0)
59 #endif
60 #else
61 #define vdbg(fmt, arg...) do {} while (0)
62 #endif
63
64 #define MAX(a,b)        (((a)>(b))?(a):(b))
65
66 #define ALLOC_FLAGS (in_interrupt () ? GFP_ATOMIC : GFP_KERNEL)
67
68 #define EP_FIFO_DEPTH 8
69
70 typedef enum {
71         SETUP_STAGE = 0,
72         DATA_STAGE,
73         STATUS_STAGE
74 } ep0_stage_t;
75
76 typedef struct {
77         int read_fifo;
78         int write_fifo;
79         int ctrl_stat;
80         int read_fifo_status;
81         int write_fifo_status;
82 } endpoint_reg_t;
83
84 typedef struct {
85         usbdev_pkt_t *head;
86         usbdev_pkt_t *tail;
87         int count;
88 } pkt_list_t;
89
90 typedef struct {
91         int active;
92         struct usb_endpoint_descriptor *desc;
93         endpoint_reg_t *reg;
94         /* Only one of these are used, unless this is the control ep */
95         pkt_list_t inlist;
96         pkt_list_t outlist;
97         unsigned int indma, outdma; /* DMA channel numbers for IN, OUT */
98         /* following are extracted from endpoint descriptor for easy access */
99         int max_pkt_size;
100         int type;
101         int direction;
102         /* WE assign endpoint addresses! */
103         int address;
104         spinlock_t lock;
105 } endpoint_t;
106
107
108 static struct usb_dev {
109         endpoint_t ep[6];
110         ep0_stage_t ep0_stage;
111
112         struct usb_device_descriptor *   dev_desc;
113         struct usb_interface_descriptor* if_desc;
114         struct usb_config_descriptor *   conf_desc;
115         u8 *                             full_conf_desc;
116         struct usb_string_descriptor *   str_desc[6];
117
118         /* callback to function layer */
119         void (*func_cb)(usbdev_cb_type_t type, unsigned long arg,
120                         void *cb_data);
121         void* cb_data;
122
123         usbdev_state_t state;   // device state
124         int suspended;          // suspended flag
125         int address;            // device address
126         int interface;
127         int num_ep;
128         u8 alternate_setting;
129         u8 configuration;       // configuration value
130         int remote_wakeup_en;
131 } usbdev;
132
133
134 static endpoint_reg_t ep_reg[] = {
135         // FIFO's 0 and 1 are EP0 default control
136         {USBD_EP0RD, USBD_EP0WR, USBD_EP0CS, USBD_EP0RDSTAT, USBD_EP0WRSTAT },
137         {0},
138         // FIFO 2 is EP2, IN
139         { -1, USBD_EP2WR, USBD_EP2CS, -1, USBD_EP2WRSTAT },
140         // FIFO 3 is EP3, IN
141         {    -1,     USBD_EP3WR, USBD_EP3CS,     -1,         USBD_EP3WRSTAT },
142         // FIFO 4 is EP4, OUT
143         {USBD_EP4RD,     -1,     USBD_EP4CS, USBD_EP4RDSTAT,     -1         },
144         // FIFO 5 is EP5, OUT
145         {USBD_EP5RD,     -1,     USBD_EP5CS, USBD_EP5RDSTAT,     -1         }
146 };
147
148 static struct {
149         unsigned int id;
150         const char *str;
151 } ep_dma_id[] = {
152         { DMA_ID_USBDEV_EP0_TX, "USBDev EP0 IN" },
153         { DMA_ID_USBDEV_EP0_RX, "USBDev EP0 OUT" },
154         { DMA_ID_USBDEV_EP2_TX, "USBDev EP2 IN" },
155         { DMA_ID_USBDEV_EP3_TX, "USBDev EP3 IN" },
156         { DMA_ID_USBDEV_EP4_RX, "USBDev EP4 OUT" },
157         { DMA_ID_USBDEV_EP5_RX, "USBDev EP5 OUT" }
158 };
159
160 #define DIR_OUT 0
161 #define DIR_IN  (1<<3)
162
163 #define CONTROL_EP USB_ENDPOINT_XFER_CONTROL
164 #define BULK_EP    USB_ENDPOINT_XFER_BULK
165
166 static inline endpoint_t *
167 epaddr_to_ep(struct usb_dev* dev, int ep_addr)
168 {
169         if (ep_addr >= 0 && ep_addr < 2)
170                 return &dev->ep[0];
171         if (ep_addr < 6)
172                 return &dev->ep[ep_addr];
173         return NULL;
174 }
175
176 static const char* std_req_name[] = {
177         "GET_STATUS",
178         "CLEAR_FEATURE",
179         "RESERVED",
180         "SET_FEATURE",
181         "RESERVED",
182         "SET_ADDRESS",
183         "GET_DESCRIPTOR",
184         "SET_DESCRIPTOR",
185         "GET_CONFIGURATION",
186         "SET_CONFIGURATION",
187         "GET_INTERFACE",
188         "SET_INTERFACE",
189         "SYNCH_FRAME"
190 };
191
192 static inline const char*
193 get_std_req_name(int req)
194 {
195         return (req >= 0 && req <= 12) ? std_req_name[req] : "UNKNOWN";
196 }
197
198 #if 0
199 static void
200 dump_setup(struct usb_ctrlrequest* s)
201 {
202         dbg("%s: requesttype=%d", __FUNCTION__, s->requesttype);
203         dbg("%s: request=%d %s", __FUNCTION__, s->request,
204             get_std_req_name(s->request));
205         dbg("%s: value=0x%04x", __FUNCTION__, s->wValue);
206         dbg("%s: index=%d", __FUNCTION__, s->index);
207         dbg("%s: length=%d", __FUNCTION__, s->length);
208 }
209 #endif
210
211 static inline usbdev_pkt_t *
212 alloc_packet(endpoint_t * ep, int data_size, void* data)
213 {
214         usbdev_pkt_t* pkt =
215                 (usbdev_pkt_t *)kmalloc(sizeof(usbdev_pkt_t) + data_size,
216                                         ALLOC_FLAGS);
217         if (!pkt)
218                 return NULL;
219         pkt->ep_addr = ep->address;
220         pkt->size = data_size;
221         pkt->status = 0;
222         pkt->next = NULL;
223         if (data)
224                 memcpy(pkt->payload, data, data_size);
225
226         return pkt;
227 }
228
229
230 /*
231  * Link a packet to the tail of the enpoint's packet list.
232  * EP spinlock must be held when calling.
233  */
234 static void
235 link_tail(endpoint_t * ep, pkt_list_t * list, usbdev_pkt_t * pkt)
236 {
237         if (!list->tail) {
238                 list->head = list->tail = pkt;
239                 list->count = 1;
240         } else {
241                 list->tail->next = pkt;
242                 list->tail = pkt;
243                 list->count++;
244         }
245 }
246
247 /*
248  * Unlink and return a packet from the head of the given packet
249  * list. It is the responsibility of the caller to free the packet.
250  * EP spinlock must be held when calling.
251  */
252 static usbdev_pkt_t *
253 unlink_head(pkt_list_t * list)
254 {
255         usbdev_pkt_t *pkt;
256
257         pkt = list->head;
258         if (!pkt || !list->count) {
259                 return NULL;
260         }
261
262         list->head = pkt->next;
263         if (!list->head) {
264                 list->head = list->tail = NULL;
265                 list->count = 0;
266         } else
267                 list->count--;
268
269         return pkt;
270 }
271
272 /*
273  * Create and attach a new packet to the tail of the enpoint's
274  * packet list. EP spinlock must be held when calling.
275  */
276 static usbdev_pkt_t *
277 add_packet(endpoint_t * ep, pkt_list_t * list, int size)
278 {
279         usbdev_pkt_t *pkt = alloc_packet(ep, size, NULL);
280         if (!pkt)
281                 return NULL;
282
283         link_tail(ep, list, pkt);
284         return pkt;
285 }
286
287
288 /*
289  * Unlink and free a packet from the head of the enpoint's
290  * packet list. EP spinlock must be held when calling.
291  */
292 static inline void
293 free_packet(pkt_list_t * list)
294 {
295         kfree(unlink_head(list));
296 }
297
298 /* EP spinlock must be held when calling. */
299 static inline void
300 flush_pkt_list(pkt_list_t * list)
301 {
302         while (list->count)
303                 free_packet(list);
304 }
305
306 /* EP spinlock must be held when calling */
307 static inline void
308 flush_write_fifo(endpoint_t * ep)
309 {
310         if (ep->reg->write_fifo_status >= 0) {
311                 au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF |
312                           USBDEV_FSTAT_OF,
313                           ep->reg->write_fifo_status);
314                 //udelay(100);
315                 //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF,
316                 //        ep->reg->write_fifo_status);
317         }
318 }
319
320 /* EP spinlock must be held when calling */
321 static inline void
322 flush_read_fifo(endpoint_t * ep)
323 {
324         if (ep->reg->read_fifo_status >= 0) {
325                 au_writel(USBDEV_FSTAT_FLUSH | USBDEV_FSTAT_UF |
326                           USBDEV_FSTAT_OF,
327                           ep->reg->read_fifo_status);
328                 //udelay(100);
329                 //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF,
330                 //        ep->reg->read_fifo_status);
331         }
332 }
333
334
335 /* EP spinlock must be held when calling. */
336 static void
337 endpoint_flush(endpoint_t * ep)
338 {
339         // First, flush all packets
340         flush_pkt_list(&ep->inlist);
341         flush_pkt_list(&ep->outlist);
342
343         // Now flush the endpoint's h/w FIFO(s)
344         flush_write_fifo(ep);
345         flush_read_fifo(ep);
346 }
347
348 /* EP spinlock must be held when calling. */
349 static void
350 endpoint_stall(endpoint_t * ep)
351 {
352         u32 cs;
353
354         warn(__FUNCTION__);
355
356         cs = au_readl(ep->reg->ctrl_stat) | USBDEV_CS_STALL;
357         au_writel(cs, ep->reg->ctrl_stat);
358 }
359
360 /* EP spinlock must be held when calling. */
361 static void
362 endpoint_unstall(endpoint_t * ep)
363 {
364         u32 cs;
365
366         warn(__FUNCTION__);
367
368         cs = au_readl(ep->reg->ctrl_stat) & ~USBDEV_CS_STALL;
369         au_writel(cs, ep->reg->ctrl_stat);
370 }
371
372 static void
373 endpoint_reset_datatoggle(endpoint_t * ep)
374 {
375         // FIXME: is this possible?
376 }
377
378
379 /* EP spinlock must be held when calling. */
380 static int
381 endpoint_fifo_read(endpoint_t * ep)
382 {
383         int read_count = 0;
384         u8 *bufptr;
385         usbdev_pkt_t *pkt = ep->outlist.tail;
386
387         if (!pkt)
388                 return -EINVAL;
389
390         bufptr = &pkt->payload[pkt->size];
391         while (au_readl(ep->reg->read_fifo_status) & USBDEV_FSTAT_FCNT_MASK) {
392                 *bufptr++ = au_readl(ep->reg->read_fifo) & 0xff;
393                 read_count++;
394                 pkt->size++;
395         }
396
397         return read_count;
398 }
399
400 #if 0
401 /* EP spinlock must be held when calling. */
402 static int
403 endpoint_fifo_write(endpoint_t * ep, int index)
404 {
405         int write_count = 0;
406         u8 *bufptr;
407         usbdev_pkt_t *pkt = ep->inlist.head;
408
409         if (!pkt)
410                 return -EINVAL;
411
412         bufptr = &pkt->payload[index];
413         while ((au_readl(ep->reg->write_fifo_status) &
414                 USBDEV_FSTAT_FCNT_MASK) < EP_FIFO_DEPTH) {
415                 if (bufptr < pkt->payload + pkt->size) {
416                         au_writel(*bufptr++, ep->reg->write_fifo);
417                         write_count++;
418                 } else {
419                         break;
420                 }
421         }
422
423         return write_count;
424 }
425 #endif
426
427 /*
428  * This routine is called to restart transmission of a packet.
429  * The endpoint's TSIZE must be set to the new packet's size,
430  * and DMA to the write FIFO needs to be restarted.
431  * EP spinlock must be held when calling.
432  */
433 static void
434 kickstart_send_packet(endpoint_t * ep)
435 {
436         u32 cs;
437         usbdev_pkt_t *pkt = ep->inlist.head;
438
439         vdbg("%s: ep%d, pkt=%p", __FUNCTION__, ep->address, pkt);
440
441         if (!pkt) {
442                 err("%s: head=NULL! list->count=%d", __FUNCTION__,
443                     ep->inlist.count);
444                 return;
445         }
446
447         dma_cache_wback_inv((unsigned long)pkt->payload, pkt->size);
448
449         /*
450          * make sure FIFO is empty
451          */
452         flush_write_fifo(ep);
453
454         cs = au_readl(ep->reg->ctrl_stat) & USBDEV_CS_STALL;
455         cs |= (pkt->size << USBDEV_CS_TSIZE_BIT);
456         au_writel(cs, ep->reg->ctrl_stat);
457
458         if (get_dma_active_buffer(ep->indma) == 1) {
459                 set_dma_count1(ep->indma, pkt->size);
460                 set_dma_addr1(ep->indma, virt_to_phys(pkt->payload));
461                 enable_dma_buffer1(ep->indma);  // reenable
462         } else {
463                 set_dma_count0(ep->indma, pkt->size);
464                 set_dma_addr0(ep->indma, virt_to_phys(pkt->payload));
465                 enable_dma_buffer0(ep->indma);  // reenable
466         }
467         if (dma_halted(ep->indma))
468                 start_dma(ep->indma);
469 }
470
471
472 /*
473  * This routine is called when a packet in the inlist has been
474  * completed. Frees the completed packet and starts sending the
475  * next. EP spinlock must be held when calling.
476  */
477 static usbdev_pkt_t *
478 send_packet_complete(endpoint_t * ep)
479 {
480         usbdev_pkt_t *pkt = unlink_head(&ep->inlist);
481
482         if (pkt) {
483                 pkt->status =
484                         (au_readl(ep->reg->ctrl_stat) & USBDEV_CS_NAK) ?
485                         PKT_STATUS_NAK : PKT_STATUS_ACK;
486
487                 vdbg("%s: ep%d, %s pkt=%p, list count=%d", __FUNCTION__,
488                      ep->address, (pkt->status & PKT_STATUS_NAK) ?
489                      "NAK" : "ACK", pkt, ep->inlist.count);
490         }
491
492         /*
493          * The write fifo should already be drained if things are
494          * working right, but flush it anyway just in case.
495          */
496         flush_write_fifo(ep);
497
498         // begin transmitting next packet in the inlist
499         if (ep->inlist.count) {
500                 kickstart_send_packet(ep);
501         }
502
503         return pkt;
504 }
505
506 /*
507  * Add a new packet to the tail of the given ep's packet
508  * inlist. The transmit complete interrupt frees packets from
509  * the head of this list. EP spinlock must be held when calling.
510  */
511 static int
512 send_packet(struct usb_dev* dev, usbdev_pkt_t *pkt, int async)
513 {
514         pkt_list_t *list;
515         endpoint_t* ep;
516
517         if (!pkt || !(ep = epaddr_to_ep(dev, pkt->ep_addr)))
518                 return -EINVAL;
519
520         if (!pkt->size)
521                 return 0;
522
523         list = &ep->inlist;
524
525         if (!async && list->count) {
526                 halt_dma(ep->indma);
527                 flush_pkt_list(list);
528         }
529
530         link_tail(ep, list, pkt);
531
532         vdbg("%s: ep%d, pkt=%p, size=%d, list count=%d", __FUNCTION__,
533              ep->address, pkt, pkt->size, list->count);
534
535         if (list->count == 1) {
536                 /*
537                  * if the packet count is one, it means the list was empty,
538                  * and no more data will go out this ep until we kick-start
539                  * it again.
540                  */
541                 kickstart_send_packet(ep);
542         }
543
544         return pkt->size;
545 }
546
547 /*
548  * This routine is called to restart reception of a packet.
549  * EP spinlock must be held when calling.
550  */
551 static void
552 kickstart_receive_packet(endpoint_t * ep)
553 {
554         usbdev_pkt_t *pkt;
555
556         // get and link a new packet for next reception
557         if (!(pkt = add_packet(ep, &ep->outlist, ep->max_pkt_size))) {
558                 err("%s: could not alloc new packet", __FUNCTION__);
559                 return;
560         }
561
562         if (get_dma_active_buffer(ep->outdma) == 1) {
563                 clear_dma_done1(ep->outdma);
564                 set_dma_count1(ep->outdma, ep->max_pkt_size);
565                 set_dma_count0(ep->outdma, 0);
566                 set_dma_addr1(ep->outdma, virt_to_phys(pkt->payload));
567                 enable_dma_buffer1(ep->outdma); // reenable
568         } else {
569                 clear_dma_done0(ep->outdma);
570                 set_dma_count0(ep->outdma, ep->max_pkt_size);
571                 set_dma_count1(ep->outdma, 0);
572                 set_dma_addr0(ep->outdma, virt_to_phys(pkt->payload));
573                 enable_dma_buffer0(ep->outdma); // reenable
574         }
575         if (dma_halted(ep->outdma))
576                 start_dma(ep->outdma);
577 }
578
579
580 /*
581  * This routine is called when a packet in the outlist has been
582  * completed (received) and we need to prepare for a new packet
583  * to be received. Halts DMA and computes the packet size from the
584  * remaining DMA counter. Then prepares a new packet for reception
585  * and restarts DMA. FIXME: what if another packet comes in
586  * on top of the completed packet? Counter would be wrong.
587  * EP spinlock must be held when calling.
588  */
589 static usbdev_pkt_t *
590 receive_packet_complete(endpoint_t * ep)
591 {
592         usbdev_pkt_t *pkt = ep->outlist.tail;
593         u32 cs;
594
595         halt_dma(ep->outdma);
596
597         cs = au_readl(ep->reg->ctrl_stat);
598
599         if (!pkt)
600                 return NULL;
601
602         pkt->size = ep->max_pkt_size - get_dma_residue(ep->outdma);
603         if (pkt->size)
604                 dma_cache_inv((unsigned long)pkt->payload, pkt->size);
605         /*
606          * need to pull out any remaining bytes in the FIFO.
607          */
608         endpoint_fifo_read(ep);
609         /*
610          * should be drained now, but flush anyway just in case.
611          */
612         flush_read_fifo(ep);
613
614         pkt->status = (cs & USBDEV_CS_NAK) ? PKT_STATUS_NAK : PKT_STATUS_ACK;
615         if (ep->address == 0 && (cs & USBDEV_CS_SU))
616                 pkt->status |= PKT_STATUS_SU;
617
618         vdbg("%s: ep%d, %s pkt=%p, size=%d", __FUNCTION__,
619              ep->address, (pkt->status & PKT_STATUS_NAK) ?
620              "NAK" : "ACK", pkt, pkt->size);
621
622         kickstart_receive_packet(ep);
623
624         return pkt;
625 }
626
627
628 /*
629  ****************************************************************************
630  * Here starts the standard device request handlers. They are
631  * all called by do_setup() via a table of function pointers.
632  ****************************************************************************
633  */
634
635 static ep0_stage_t
636 do_get_status(struct usb_dev* dev, struct usb_ctrlrequest* setup)
637 {
638         switch (setup->bRequestType) {
639         case 0x80:      // Device
640                 // FIXME: send device status
641                 break;
642         case 0x81:      // Interface
643                 // FIXME: send interface status
644                 break;
645         case 0x82:      // End Point
646                 // FIXME: send endpoint status
647                 break;
648         default:
649                 // Invalid Command
650                 endpoint_stall(&dev->ep[0]); // Stall End Point 0
651                 break;
652         }
653
654         return STATUS_STAGE;
655 }
656
657 static ep0_stage_t
658 do_clear_feature(struct usb_dev* dev, struct usb_ctrlrequest* setup)
659 {
660         switch (setup->bRequestType) {
661         case 0x00:      // Device
662                 if ((le16_to_cpu(setup->wValue) & 0xff) == 1)
663                         dev->remote_wakeup_en = 0;
664         else
665                         endpoint_stall(&dev->ep[0]);
666                 break;
667         case 0x02:      // End Point
668                 if ((le16_to_cpu(setup->wValue) & 0xff) == 0) {
669                         endpoint_t *ep =
670                                 epaddr_to_ep(dev,
671                                              le16_to_cpu(setup->wIndex) & 0xff);
672
673                         endpoint_unstall(ep);
674                         endpoint_reset_datatoggle(ep);
675                 } else
676                         endpoint_stall(&dev->ep[0]);
677                 break;
678         }
679
680         return SETUP_STAGE;
681 }
682
683 static ep0_stage_t
684 do_reserved(struct usb_dev* dev, struct usb_ctrlrequest* setup)
685 {
686         // Invalid request, stall End Point 0
687         endpoint_stall(&dev->ep[0]);
688         return SETUP_STAGE;
689 }
690
691 static ep0_stage_t
692 do_set_feature(struct usb_dev* dev, struct usb_ctrlrequest* setup)
693 {
694         switch (setup->bRequestType) {
695         case 0x00:      // Device
696                 if ((le16_to_cpu(setup->wValue) & 0xff) == 1)
697                         dev->remote_wakeup_en = 1;
698                 else
699                         endpoint_stall(&dev->ep[0]);
700                 break;
701         case 0x02:      // End Point
702                 if ((le16_to_cpu(setup->wValue) & 0xff) == 0) {
703                         endpoint_t *ep =
704                                 epaddr_to_ep(dev,
705                                              le16_to_cpu(setup->wIndex) & 0xff);
706
707                         endpoint_stall(ep);
708                 } else
709                         endpoint_stall(&dev->ep[0]);
710                 break;
711         }
712
713         return SETUP_STAGE;
714 }
715
716 static ep0_stage_t
717 do_set_address(struct usb_dev* dev, struct usb_ctrlrequest* setup)
718 {
719         int new_state = dev->state;
720         int new_addr = le16_to_cpu(setup->wValue);
721
722         dbg("%s: our address=%d", __FUNCTION__, new_addr);
723
724         if (new_addr > 127) {
725                         // usb spec doesn't tell us what to do, so just go to
726                         // default state
727                 new_state = DEFAULT;
728                 dev->address = 0;
729         } else if (dev->address != new_addr) {
730                 dev->address = new_addr;
731                 new_state = ADDRESS;
732         }
733
734         if (dev->state != new_state) {
735                 dev->state = new_state;
736                 /* inform function layer of usbdev state change */
737                 dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data);
738         }
739
740         return SETUP_STAGE;
741 }
742
743 static ep0_stage_t
744 do_get_descriptor(struct usb_dev* dev, struct usb_ctrlrequest* setup)
745 {
746         int strnum, desc_len = le16_to_cpu(setup->wLength);
747
748                 switch (le16_to_cpu(setup->wValue) >> 8) {
749                 case USB_DT_DEVICE:
750                         // send device descriptor!
751                 desc_len = desc_len > dev->dev_desc->bLength ?
752                         dev->dev_desc->bLength : desc_len;
753                         dbg("sending device desc, size=%d", desc_len);
754                 send_packet(dev, alloc_packet(&dev->ep[0], desc_len,
755                                               dev->dev_desc), 0);
756                         break;
757                 case USB_DT_CONFIG:
758                         // If the config descr index in low-byte of
759                         // setup->wValue        is valid, send config descr,
760                         // otherwise stall ep0.
761                         if ((le16_to_cpu(setup->wValue) & 0xff) == 0) {
762                                 // send config descriptor!
763                                 if (desc_len <= USB_DT_CONFIG_SIZE) {
764                                         dbg("sending partial config desc, size=%d",
765                                              desc_len);
766                                 send_packet(dev,
767                                             alloc_packet(&dev->ep[0],
768                                                          desc_len,
769                                                          dev->conf_desc),
770                                             0);
771                                 } else {
772                                 int len = dev->conf_desc->wTotalLength;
773                                 dbg("sending whole config desc,"
774                                     " size=%d, our size=%d", desc_len, len);
775                                 desc_len = desc_len > len ? len : desc_len;
776                                 send_packet(dev,
777                                             alloc_packet(&dev->ep[0],
778                                                          desc_len,
779                                                          dev->full_conf_desc),
780                                             0);
781                                 }
782                         } else
783                         endpoint_stall(&dev->ep[0]);
784                         break;
785                 case USB_DT_STRING:
786                         // If the string descr index in low-byte of setup->wValue
787                         // is valid, send string descr, otherwise stall ep0.
788                         strnum = le16_to_cpu(setup->wValue) & 0xff;
789                         if (strnum >= 0 && strnum < 6) {
790                                 struct usb_string_descriptor *desc =
791                                 dev->str_desc[strnum];
792                                 desc_len = desc_len > desc->bLength ?
793                                         desc->bLength : desc_len;
794                                 dbg("sending string desc %d", strnum);
795                         send_packet(dev,
796                                     alloc_packet(&dev->ep[0], desc_len,
797                                                  desc), 0);
798                         } else
799                         endpoint_stall(&dev->ep[0]);
800                         break;
801         default:
802                 // Invalid request
803                 err("invalid get desc=%d, stalled",
804                             le16_to_cpu(setup->wValue) >> 8);
805                 endpoint_stall(&dev->ep[0]);    // Stall endpoint 0
806                         break;
807                 }
808
809         return STATUS_STAGE;
810 }
811
812 static ep0_stage_t
813 do_set_descriptor(struct usb_dev* dev, struct usb_ctrlrequest* setup)
814 {
815         // TODO: implement
816         // there will be an OUT data stage (the descriptor to set)
817         return DATA_STAGE;
818 }
819
820 static ep0_stage_t
821 do_get_configuration(struct usb_dev* dev, struct usb_ctrlrequest* setup)
822 {
823         // send dev->configuration
824         dbg("sending config");
825         send_packet(dev, alloc_packet(&dev->ep[0], 1, &dev->configuration),
826                     0);
827         return STATUS_STAGE;
828 }
829
830 static ep0_stage_t
831 do_set_configuration(struct usb_dev* dev, struct usb_ctrlrequest* setup)
832 {
833         // set active config to low-byte of setup->wValue
834         dev->configuration = le16_to_cpu(setup->wValue) & 0xff;
835         dbg("set config, config=%d", dev->configuration);
836         if (!dev->configuration && dev->state > DEFAULT) {
837                 dev->state = ADDRESS;
838                 /* inform function layer of usbdev state change */
839                 dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data);
840         } else if (dev->configuration == 1) {
841                 dev->state = CONFIGURED;
842                 /* inform function layer of usbdev state change */
843                 dev->func_cb(CB_NEW_STATE, dev->state, dev->cb_data);
844         } else {
845                 // FIXME: "respond with request error" - how?
846         }
847
848         return SETUP_STAGE;
849 }
850
851 static ep0_stage_t
852 do_get_interface(struct usb_dev* dev, struct usb_ctrlrequest* setup)
853 {
854                 // interface must be zero.
855         if ((le16_to_cpu(setup->wIndex) & 0xff) || dev->state == ADDRESS) {
856                         // FIXME: respond with "request error". how?
857         } else if (dev->state == CONFIGURED) {
858                 // send dev->alternate_setting
859                         dbg("sending alt setting");
860                 send_packet(dev, alloc_packet(&dev->ep[0], 1,
861                                               &dev->alternate_setting), 0);
862                 }
863
864         return STATUS_STAGE;
865
866 }
867
868 static ep0_stage_t
869 do_set_interface(struct usb_dev* dev, struct usb_ctrlrequest* setup)
870 {
871         if (dev->state == ADDRESS) {
872                         // FIXME: respond with "request error". how?
873         } else if (dev->state == CONFIGURED) {
874                 dev->interface = le16_to_cpu(setup->wIndex) & 0xff;
875                 dev->alternate_setting =
876                             le16_to_cpu(setup->wValue) & 0xff;
877                         // interface and alternate_setting must be zero
878                 if (dev->interface || dev->alternate_setting) {
879                                 // FIXME: respond with "request error". how?
880                         }
881                 }
882
883         return SETUP_STAGE;
884 }
885
886 static ep0_stage_t
887 do_synch_frame(struct usb_dev* dev, struct usb_ctrlrequest* setup)
888 {
889         // TODO
890         return SETUP_STAGE;
891 }
892
893 typedef ep0_stage_t (*req_method_t)(struct usb_dev* dev,
894                                     struct usb_ctrlrequest* setup);
895
896
897 /* Table of the standard device request handlers */
898 static const req_method_t req_method[] = {
899         do_get_status,
900         do_clear_feature,
901         do_reserved,
902         do_set_feature,
903         do_reserved,
904         do_set_address,
905         do_get_descriptor,
906         do_set_descriptor,
907         do_get_configuration,
908         do_set_configuration,
909         do_get_interface,
910         do_set_interface,
911         do_synch_frame
912 };
913
914
915 // SETUP packet request dispatcher
916 static void
917 do_setup (struct usb_dev* dev, struct usb_ctrlrequest* setup)
918 {
919         req_method_t m;
920
921         dbg("%s: req %d %s", __FUNCTION__, setup->bRequestType,
922             get_std_req_name(setup->bRequestType));
923
924         if ((setup->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD ||
925             (setup->bRequestType & USB_RECIP_MASK) != USB_RECIP_DEVICE) {
926                 err("%s: invalid requesttype 0x%02x", __FUNCTION__,
927                     setup->bRequestType);
928                 return;
929                 }
930
931         if ((setup->bRequestType & 0x80) == USB_DIR_OUT && setup->wLength)
932                 dbg("%s: OUT phase! length=%d", __FUNCTION__, setup->wLength);
933
934         if (setup->bRequestType < sizeof(req_method)/sizeof(req_method_t))
935                 m = req_method[setup->bRequestType];
936                         else
937                 m = do_reserved;
938
939         dev->ep0_stage = (*m)(dev, setup);
940 }
941
942 /*
943  * A SETUP, DATA0, or DATA1 packet has been received
944  * on the default control endpoint's fifo.
945  */
946 static void
947 process_ep0_receive (struct usb_dev* dev)
948 {
949         endpoint_t *ep0 = &dev->ep[0];
950         usbdev_pkt_t *pkt;
951
952         spin_lock(&ep0->lock);
953
954                 // complete packet and prepare a new packet
955         pkt = receive_packet_complete(ep0);
956         if (!pkt) {
957                 // FIXME: should  put a warn/err here.
958                 spin_unlock(&ep0->lock);
959                         return;
960                 }
961
962         // unlink immediately from endpoint.
963         unlink_head(&ep0->outlist);
964
965         // override current stage if h/w says it's a setup packet
966         if (pkt->status & PKT_STATUS_SU)
967                 dev->ep0_stage = SETUP_STAGE;
968
969         switch (dev->ep0_stage) {
970         case SETUP_STAGE:
971                 vdbg("SU bit is %s in setup stage",
972                      (pkt->status & PKT_STATUS_SU) ? "set" : "not set");
973
974                         if (pkt->size == sizeof(struct usb_ctrlrequest)) {
975 #ifdef VDEBUG
976                         if (pkt->status & PKT_STATUS_ACK)
977                                 vdbg("received SETUP");
978                                 else
979                                 vdbg("received NAK SETUP");
980 #endif
981                         do_setup(dev, (struct usb_ctrlrequest*)pkt->payload);
982                 } else
983                         err("%s: wrong size SETUP received", __FUNCTION__);
984                 break;
985         case DATA_STAGE:
986                 /*
987                  * this setup has an OUT data stage. Of the standard
988                  * device requests, only set_descriptor has this stage,
989                  * so this packet is that descriptor. TODO: drop it for
990                  * now, set_descriptor not implemented.
991                  *
992                  * Need to place a byte in the write FIFO here, to prepare
993                  * to send a zero-length DATA ack packet to the host in the
994                  * STATUS stage.
995                  */
996                 au_writel(0, ep0->reg->write_fifo);
997                 dbg("received OUT stage DATAx on EP0, size=%d", pkt->size);
998                 dev->ep0_stage = SETUP_STAGE;
999                 break;
1000         case STATUS_STAGE:
1001                 // this setup had an IN data stage, and host is ACK'ing
1002                 // the packet we sent during that stage.
1003                 if (pkt->size != 0)
1004                         warn("received non-zero ACK on EP0??");
1005 #ifdef VDEBUG
1006                 else
1007                         vdbg("received ACK on EP0");
1008 #endif
1009                 dev->ep0_stage = SETUP_STAGE;
1010                 break;
1011                 }
1012
1013         spin_unlock(&ep0->lock);
1014                 // we're done processing the packet, free it
1015                 kfree(pkt);
1016 }
1017
1018
1019 /*
1020  * A DATA0/1 packet has been received on one of the OUT endpoints (4 or 5)
1021  */
1022 static void
1023 process_ep_receive (struct usb_dev* dev, endpoint_t *ep)
1024 {
1025         usbdev_pkt_t *pkt;
1026
1027                 spin_lock(&ep->lock);
1028         pkt = receive_packet_complete(ep);
1029                 spin_unlock(&ep->lock);
1030
1031         dev->func_cb(CB_PKT_COMPLETE, (unsigned long)pkt, dev->cb_data);
1032 }
1033
1034
1035
1036 /* This ISR handles the receive complete and suspend events */
1037 static void
1038 req_sus_intr (int irq, void *dev_id, struct pt_regs *regs)
1039 {
1040         struct usb_dev *dev = (struct usb_dev *) dev_id;
1041         u32 status;
1042
1043         status = au_readl(USBD_INTSTAT);
1044         au_writel(status, USBD_INTSTAT);        // ack'em
1045
1046         if (status & (1<<0))
1047                 process_ep0_receive(dev);
1048         if (status & (1<<4))
1049                 process_ep_receive(dev, &dev->ep[4]);
1050         if (status & (1<<5))
1051                 process_ep_receive(dev, &dev->ep[5]);
1052 }
1053
1054
1055 /* This ISR handles the DMA done events on EP0 */
1056 static void
1057 dma_done_ep0_intr(int irq, void *dev_id, struct pt_regs *regs)
1058 {
1059         struct usb_dev *dev = (struct usb_dev *) dev_id;
1060         usbdev_pkt_t* pkt;
1061         endpoint_t *ep0 = &dev->ep[0];
1062         u32 cs0, buff_done;
1063
1064         spin_lock(&ep0->lock);
1065         cs0 = au_readl(ep0->reg->ctrl_stat);
1066
1067         // first check packet transmit done
1068         if ((buff_done = get_dma_buffer_done(ep0->indma)) != 0) {
1069                 // transmitted a DATAx packet during DATA stage
1070                 // on control endpoint 0
1071                 // clear DMA done bit
1072                 if (buff_done & DMA_D0)
1073                         clear_dma_done0(ep0->indma);
1074                 if (buff_done & DMA_D1)
1075                         clear_dma_done1(ep0->indma);
1076
1077                 pkt = send_packet_complete(ep0);
1078                 if (pkt)
1079                         kfree(pkt);
1080         }
1081
1082         /*
1083          * Now check packet receive done. Shouldn't get these,
1084          * the receive packet complete intr should happen
1085          * before the DMA done intr occurs.
1086          */
1087         if ((buff_done = get_dma_buffer_done(ep0->outdma)) != 0) {
1088                 // clear DMA done bit
1089                 if (buff_done & DMA_D0)
1090                         clear_dma_done0(ep0->outdma);
1091                 if (buff_done & DMA_D1)
1092                         clear_dma_done1(ep0->outdma);
1093
1094                 //process_ep0_receive(dev);
1095         }
1096
1097         spin_unlock(&ep0->lock);
1098 }
1099
1100 /* This ISR handles the DMA done events on endpoints 2,3,4,5 */
1101 static void
1102 dma_done_ep_intr(int irq, void *dev_id, struct pt_regs *regs)
1103 {
1104         struct usb_dev *dev = (struct usb_dev *) dev_id;
1105         int i;
1106
1107         for (i = 2; i < 6; i++) {
1108         u32 buff_done;
1109                 usbdev_pkt_t* pkt;
1110                 endpoint_t *ep = &dev->ep[i];
1111
1112                 if (!ep->active) continue;
1113
1114         spin_lock(&ep->lock);
1115
1116                 if (ep->direction == USB_DIR_IN) {
1117                         buff_done = get_dma_buffer_done(ep->indma);
1118                         if (buff_done != 0) {
1119                                 // transmitted a DATAx pkt on the IN ep
1120                 // clear DMA done bit
1121                 if (buff_done & DMA_D0)
1122                         clear_dma_done0(ep->indma);
1123                 if (buff_done & DMA_D1)
1124                         clear_dma_done1(ep->indma);
1125
1126                                 pkt = send_packet_complete(ep);
1127
1128                                 spin_unlock(&ep->lock);
1129                                 dev->func_cb(CB_PKT_COMPLETE,
1130                                              (unsigned long)pkt,
1131                                              dev->cb_data);
1132                                 spin_lock(&ep->lock);
1133                         }
1134                 } else {
1135         /*
1136                          * Check packet receive done (OUT ep). Shouldn't get
1137                          * these, the rx packet complete intr should happen
1138          * before the DMA done intr occurs.
1139          */
1140                         buff_done = get_dma_buffer_done(ep->outdma);
1141                         if (buff_done != 0) {
1142                                 // received a DATAx pkt on the OUT ep
1143                 // clear DMA done bit
1144                 if (buff_done & DMA_D0)
1145                         clear_dma_done0(ep->outdma);
1146                 if (buff_done & DMA_D1)
1147                         clear_dma_done1(ep->outdma);
1148
1149                                 //process_ep_receive(dev, ep);
1150         }
1151         }
1152
1153                 spin_unlock(&ep->lock);
1154         }
1155 }
1156
1157
1158 /***************************************************************************
1159  * Here begins the external interface functions
1160  ***************************************************************************
1161  */
1162
1163 /*
1164  * allocate a new packet
1165  */
1166 int
1167 usbdev_alloc_packet(int ep_addr, int data_size, usbdev_pkt_t** pkt)
1168 {
1169         endpoint_t * ep = epaddr_to_ep(&usbdev, ep_addr);
1170         usbdev_pkt_t* lpkt = NULL;
1171
1172         if (!ep || !ep->active || ep->address < 2)
1173                 return -ENODEV;
1174         if (data_size > ep->max_pkt_size)
1175                 return -EINVAL;
1176
1177         lpkt = *pkt = alloc_packet(ep, data_size, NULL);
1178         if (!lpkt)
1179                 return -ENOMEM;
1180         return 0;
1181 }
1182
1183
1184 /*
1185  * packet send
1186  */
1187 int
1188 usbdev_send_packet(int ep_addr, usbdev_pkt_t * pkt)
1189 {
1190         unsigned long flags;
1191         int count;
1192         endpoint_t * ep;
1193
1194         if (!pkt || !(ep = epaddr_to_ep(&usbdev, pkt->ep_addr)) ||
1195             !ep->active || ep->address < 2)
1196                 return -ENODEV;
1197         if (ep->direction != USB_DIR_IN)
1198                 return -EINVAL;
1199
1200         spin_lock_irqsave(&ep->lock, flags);
1201         count = send_packet(&usbdev, pkt, 1);
1202         spin_unlock_irqrestore(&ep->lock, flags);
1203
1204         return count;
1205 }
1206
1207 /*
1208  * packet receive
1209  */
1210 int
1211 usbdev_receive_packet(int ep_addr, usbdev_pkt_t** pkt)
1212 {
1213         unsigned long flags;
1214         usbdev_pkt_t* lpkt = NULL;
1215         endpoint_t *ep = epaddr_to_ep(&usbdev, ep_addr);
1216
1217         if (!ep || !ep->active || ep->address < 2)
1218                 return -ENODEV;
1219         if (ep->direction != USB_DIR_OUT)
1220                 return -EINVAL;
1221
1222         spin_lock_irqsave(&ep->lock, flags);
1223         if (ep->outlist.count > 1)
1224                 lpkt = unlink_head(&ep->outlist);
1225         spin_unlock_irqrestore(&ep->lock, flags);
1226
1227         if (!lpkt) {
1228                 /* no packet available */
1229                 *pkt = NULL;
1230                 return -ENODATA;
1231         }
1232
1233         *pkt = lpkt;
1234
1235         return lpkt->size;
1236 }
1237
1238
1239 /*
1240  * return total queued byte count on the endpoint.
1241  */
1242 int
1243 usbdev_get_byte_count(int ep_addr)
1244 {
1245         unsigned long flags;
1246         pkt_list_t *list;
1247         usbdev_pkt_t *scan;
1248         int count = 0;
1249         endpoint_t * ep = epaddr_to_ep(&usbdev, ep_addr);
1250
1251         if (!ep || !ep->active || ep->address < 2)
1252                 return -ENODEV;
1253
1254         if (ep->direction == USB_DIR_IN) {
1255                 list = &ep->inlist;
1256
1257                 spin_lock_irqsave(&ep->lock, flags);
1258                 for (scan = list->head; scan; scan = scan->next)
1259                         count += scan->size;
1260                 spin_unlock_irqrestore(&ep->lock, flags);
1261         } else {
1262                 list = &ep->outlist;
1263
1264                 spin_lock_irqsave(&ep->lock, flags);
1265                 if (list->count > 1) {
1266                         for (scan = list->head; scan != list->tail;
1267                              scan = scan->next)
1268                                 count += scan->size;
1269         }
1270                 spin_unlock_irqrestore(&ep->lock, flags);
1271         }
1272
1273         return count;
1274 }
1275
1276
1277 void
1278 usbdev_exit(void)
1279 {
1280         endpoint_t *ep;
1281         int i;
1282
1283         au_writel(0, USBD_INTEN);       // disable usb dev ints
1284         au_writel(0, USBD_ENABLE);      // disable usb dev
1285
1286         free_irq(AU1000_USB_DEV_REQ_INT, &usbdev);
1287         free_irq(AU1000_USB_DEV_SUS_INT, &usbdev);
1288
1289         // free all control endpoint resources
1290         ep = &usbdev.ep[0];
1291         free_au1000_dma(ep->indma);
1292         free_au1000_dma(ep->outdma);
1293         endpoint_flush(ep);
1294
1295         // free ep resources
1296         for (i = 2; i < 6; i++) {
1297                 ep = &usbdev.ep[i];
1298                 if (!ep->active) continue;
1299
1300                 if (ep->direction == USB_DIR_IN) {
1301                         free_au1000_dma(ep->indma);
1302                 } else {
1303                 free_au1000_dma(ep->outdma);
1304                 }
1305                 endpoint_flush(ep);
1306         }
1307
1308         if (usbdev.full_conf_desc)
1309                 kfree(usbdev.full_conf_desc);
1310 }
1311
1312 int
1313 usbdev_init(struct usb_device_descriptor* dev_desc,
1314             struct usb_config_descriptor* config_desc,
1315             struct usb_interface_descriptor* if_desc,
1316             struct usb_endpoint_descriptor* ep_desc,
1317             struct usb_string_descriptor* str_desc[],
1318             void (*cb)(usbdev_cb_type_t, unsigned long, void *),
1319             void* cb_data)
1320 {
1321         endpoint_t *ep0;
1322         int i, ret=0;
1323         u8* fcd;
1324
1325         if (dev_desc->bNumConfigurations > 1 ||
1326             config_desc->bNumInterfaces > 1 ||
1327             if_desc->bNumEndpoints > 4) {
1328                 err("Only one config, one i/f, and no more "
1329                     "than 4 ep's allowed");
1330                 ret = -EINVAL;
1331                 goto out;
1332         }
1333
1334         if (!cb) {
1335                 err("Function-layer callback required");
1336                 ret = -EINVAL;
1337                 goto out;
1338         }
1339
1340         if (dev_desc->bMaxPacketSize0 != USBDEV_EP0_MAX_PACKET_SIZE) {
1341                 warn("EP0 Max Packet size must be %d",
1342                      USBDEV_EP0_MAX_PACKET_SIZE);
1343                 dev_desc->bMaxPacketSize0 = USBDEV_EP0_MAX_PACKET_SIZE;
1344         }
1345
1346         memset(&usbdev, 0, sizeof(struct usb_dev));
1347
1348         usbdev.state = DEFAULT;
1349         usbdev.dev_desc = dev_desc;
1350         usbdev.if_desc = if_desc;
1351         usbdev.conf_desc = config_desc;
1352         for (i=0; i<6; i++)
1353                 usbdev.str_desc[i] = str_desc[i];
1354         usbdev.func_cb = cb;
1355         usbdev.cb_data = cb_data;
1356
1357         /* Initialize default control endpoint */
1358         ep0 = &usbdev.ep[0];
1359         ep0->active = 1;
1360         ep0->type = CONTROL_EP;
1361         ep0->max_pkt_size = USBDEV_EP0_MAX_PACKET_SIZE;
1362         spin_lock_init(&ep0->lock);
1363         ep0->desc = NULL;       // ep0 has no descriptor
1364         ep0->address = 0;
1365         ep0->direction = 0;
1366         ep0->reg = &ep_reg[0];
1367
1368         /* Initialize the other requested endpoints */
1369         for (i = 0; i < if_desc->bNumEndpoints; i++) {
1370                 struct usb_endpoint_descriptor* epd = &ep_desc[i];
1371         endpoint_t *ep;
1372
1373                 if ((epd->bEndpointAddress & 0x80) == USB_DIR_IN) {
1374                         ep = &usbdev.ep[2];
1375                         ep->address = 2;
1376                         if (ep->active) {
1377                                 ep = &usbdev.ep[3];
1378                                 ep->address = 3;
1379                                 if (ep->active) {
1380                                         err("too many IN ep's requested");
1381                                         ret = -ENODEV;
1382                                         goto out;
1383         }
1384         }
1385                 } else {
1386                         ep = &usbdev.ep[4];
1387                         ep->address = 4;
1388                         if (ep->active) {
1389                                 ep = &usbdev.ep[5];
1390                                 ep->address = 5;
1391                                 if (ep->active) {
1392                                         err("too many OUT ep's requested");
1393                                         ret = -ENODEV;
1394                                         goto out;
1395         }
1396         }
1397                 }
1398
1399                 ep->active = 1;
1400                 epd->bEndpointAddress &= ~0x0f;
1401                 epd->bEndpointAddress |= (u8)ep->address;
1402                 ep->direction = epd->bEndpointAddress & 0x80;
1403                 ep->type = epd->bmAttributes & 0x03;
1404                 ep->max_pkt_size = epd->wMaxPacketSize;
1405                 spin_lock_init(&ep->lock);
1406                 ep->desc = epd;
1407                 ep->reg = &ep_reg[ep->address];
1408                 }
1409
1410         /*
1411          * initialize the full config descriptor
1412          */
1413         usbdev.full_conf_desc = fcd = kmalloc(config_desc->wTotalLength,
1414                                               ALLOC_FLAGS);
1415         if (!fcd) {
1416                 err("failed to alloc full config descriptor");
1417                 ret = -ENOMEM;
1418                 goto out;
1419         }
1420
1421         memcpy(fcd, config_desc, USB_DT_CONFIG_SIZE);
1422         fcd += USB_DT_CONFIG_SIZE;
1423         memcpy(fcd, if_desc, USB_DT_INTERFACE_SIZE);
1424         fcd += USB_DT_INTERFACE_SIZE;
1425         for (i = 0; i < if_desc->bNumEndpoints; i++) {
1426                 memcpy(fcd, &ep_desc[i], USB_DT_ENDPOINT_SIZE);
1427                 fcd += USB_DT_ENDPOINT_SIZE;
1428         }
1429
1430         /* Now we're ready to enable the controller */
1431         au_writel(0x0002, USBD_ENABLE);
1432         udelay(100);
1433         au_writel(0x0003, USBD_ENABLE);
1434         udelay(100);
1435
1436         /* build and send config table based on ep descriptors */
1437         for (i = 0; i < 6; i++) {
1438                 endpoint_t *ep;
1439                 if (i == 1)
1440                         continue; // skip dummy ep
1441                 ep = &usbdev.ep[i];
1442                 if (ep->active) {
1443                         au_writel((ep->address << 4) | 0x04, USBD_CONFIG);
1444                         au_writel(((ep->max_pkt_size & 0x380) >> 7) |
1445                                   (ep->direction >> 4) | (ep->type << 4),
1446                                   USBD_CONFIG);
1447                         au_writel((ep->max_pkt_size & 0x7f) << 1, USBD_CONFIG);
1448                         au_writel(0x00, USBD_CONFIG);
1449                         au_writel(ep->address, USBD_CONFIG);
1450                 } else {
1451                         u8 dir = (i==2 || i==3) ? DIR_IN : DIR_OUT;
1452                         au_writel((i << 4) | 0x04, USBD_CONFIG);
1453                         au_writel(((16 & 0x380) >> 7) | dir |
1454                                   (BULK_EP << 4), USBD_CONFIG);
1455                         au_writel((16 & 0x7f) << 1, USBD_CONFIG);
1456                         au_writel(0x00, USBD_CONFIG);
1457                         au_writel(i, USBD_CONFIG);
1458                 }
1459         }
1460
1461         /*
1462          * Enable Receive FIFO Complete interrupts only. Transmit
1463          * complete is being handled by the DMA done interrupts.
1464          */
1465         au_writel(0x31, USBD_INTEN);
1466
1467         /*
1468          * Controller is now enabled, request DMA and IRQ
1469          * resources.
1470          */
1471
1472         /* request the USB device transfer complete interrupt */
1473         if (request_irq(AU1000_USB_DEV_REQ_INT, req_sus_intr, SA_INTERRUPT,
1474                         "USBdev req", &usbdev)) {
1475                 err("Can't get device request intr");
1476                 ret = -ENXIO;
1477                 goto out;
1478         }
1479         /* request the USB device suspend interrupt */
1480         if (request_irq(AU1000_USB_DEV_SUS_INT, req_sus_intr, SA_INTERRUPT,
1481                         "USBdev sus", &usbdev)) {
1482                 err("Can't get device suspend intr");
1483                 ret = -ENXIO;
1484                 goto out;
1485         }
1486
1487         /* Request EP0 DMA and IRQ */
1488         if ((ep0->indma = request_au1000_dma(ep_dma_id[0].id,
1489                                              ep_dma_id[0].str,
1490                                              dma_done_ep0_intr,
1491                                              SA_INTERRUPT,
1492                                              &usbdev)) < 0) {
1493                 err("Can't get %s DMA", ep_dma_id[0].str);
1494                 ret = -ENXIO;
1495                 goto out;
1496         }
1497         if ((ep0->outdma = request_au1000_dma(ep_dma_id[1].id,
1498                                               ep_dma_id[1].str,
1499                                               NULL, 0, NULL)) < 0) {
1500                 err("Can't get %s DMA", ep_dma_id[1].str);
1501                 ret = -ENXIO;
1502                 goto out;
1503         }
1504
1505         // Flush the ep0 buffers and FIFOs
1506         endpoint_flush(ep0);
1507         // start packet reception on ep0
1508         kickstart_receive_packet(ep0);
1509
1510         /* Request DMA and IRQ for the other endpoints */
1511         for (i = 2; i < 6; i++) {
1512                 endpoint_t *ep = &usbdev.ep[i];
1513                 if (!ep->active)
1514                         continue;
1515
1516                 // Flush the endpoint buffers and FIFOs
1517                 endpoint_flush(ep);
1518
1519                 if (ep->direction == USB_DIR_IN) {
1520                         ep->indma =
1521                                 request_au1000_dma(ep_dma_id[ep->address].id,
1522                                                    ep_dma_id[ep->address].str,
1523                                                    dma_done_ep_intr,
1524                                                    SA_INTERRUPT,
1525                                                    &usbdev);
1526                         if (ep->indma < 0) {
1527                                 err("Can't get %s DMA",
1528                                     ep_dma_id[ep->address].str);
1529                                 ret = -ENXIO;
1530                                 goto out;
1531                         }
1532                 } else {
1533                         ep->outdma =
1534                                 request_au1000_dma(ep_dma_id[ep->address].id,
1535                                                    ep_dma_id[ep->address].str,
1536                                                    NULL, 0, NULL);
1537                         if (ep->outdma < 0) {
1538                                 err("Can't get %s DMA",
1539                                     ep_dma_id[ep->address].str);
1540                                 ret = -ENXIO;
1541                                 goto out;
1542                         }
1543
1544                         // start packet reception on OUT endpoint
1545                         kickstart_receive_packet(ep);
1546                 }
1547         }
1548
1549  out:
1550         if (ret)
1551                 usbdev_exit();
1552         return ret;
1553 }
1554
1555 EXPORT_SYMBOL(usbdev_init);
1556 EXPORT_SYMBOL(usbdev_exit);
1557 EXPORT_SYMBOL(usbdev_alloc_packet);
1558 EXPORT_SYMBOL(usbdev_receive_packet);
1559 EXPORT_SYMBOL(usbdev_send_packet);
1560 EXPORT_SYMBOL(usbdev_get_byte_count);