2 * BRIEF MODULE DESCRIPTION
3 * Au1000 USB Device-Side (device layer)
5 * Copyright 2001-2002 MontaVista Software Inc.
6 * Author: MontaVista Software, Inc.
7 * stevel@mvista.com or source@mvista.com
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.
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.
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.
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>
43 #include <linux/usb.h>
46 #include <asm/uaccess.h>
48 #include <asm/mipsregs.h>
49 #include <asm/au1000.h>
50 #include <asm/au1000_dma.h>
51 #include <asm/au1000_usbdev.h>
56 #define vdbg(fmt, arg...) printk(KERN_DEBUG __FILE__ ": " fmt "\n" , ## arg)
58 #define vdbg(fmt, arg...) do {} while (0)
61 #define vdbg(fmt, arg...) do {} while (0)
64 #define ALLOC_FLAGS (in_interrupt () ? GFP_ATOMIC : GFP_KERNEL)
66 #define EP_FIFO_DEPTH 8
79 int write_fifo_status
;
90 struct usb_endpoint_descriptor
*desc
;
92 /* Only one of these are used, unless this is the control ep */
95 unsigned int indma
, outdma
; /* DMA channel numbers for IN, OUT */
96 /* following are extracted from endpoint descriptor for easy access */
100 /* WE assign endpoint addresses! */
106 static struct usb_dev
{
108 ep0_stage_t ep0_stage
;
110 struct usb_device_descriptor
* dev_desc
;
111 struct usb_interface_descriptor
* if_desc
;
112 struct usb_config_descriptor
* conf_desc
;
114 struct usb_string_descriptor
* str_desc
[6];
116 /* callback to function layer */
117 void (*func_cb
)(usbdev_cb_type_t type
, unsigned long arg
,
121 usbdev_state_t state
; // device state
122 int suspended
; // suspended flag
123 int address
; // device address
126 u8 alternate_setting
;
127 u8 configuration
; // configuration value
128 int remote_wakeup_en
;
132 static endpoint_reg_t ep_reg
[] = {
133 // FIFO's 0 and 1 are EP0 default control
134 {USBD_EP0RD
, USBD_EP0WR
, USBD_EP0CS
, USBD_EP0RDSTAT
, USBD_EP0WRSTAT
},
137 { -1, USBD_EP2WR
, USBD_EP2CS
, -1, USBD_EP2WRSTAT
},
139 { -1, USBD_EP3WR
, USBD_EP3CS
, -1, USBD_EP3WRSTAT
},
140 // FIFO 4 is EP4, OUT
141 {USBD_EP4RD
, -1, USBD_EP4CS
, USBD_EP4RDSTAT
, -1 },
142 // FIFO 5 is EP5, OUT
143 {USBD_EP5RD
, -1, USBD_EP5CS
, USBD_EP5RDSTAT
, -1 }
150 { DMA_ID_USBDEV_EP0_TX
, "USBDev EP0 IN" },
151 { DMA_ID_USBDEV_EP0_RX
, "USBDev EP0 OUT" },
152 { DMA_ID_USBDEV_EP2_TX
, "USBDev EP2 IN" },
153 { DMA_ID_USBDEV_EP3_TX
, "USBDev EP3 IN" },
154 { DMA_ID_USBDEV_EP4_RX
, "USBDev EP4 OUT" },
155 { DMA_ID_USBDEV_EP5_RX
, "USBDev EP5 OUT" }
159 #define DIR_IN (1<<3)
161 #define CONTROL_EP USB_ENDPOINT_XFER_CONTROL
162 #define BULK_EP USB_ENDPOINT_XFER_BULK
164 static inline endpoint_t
*
165 epaddr_to_ep(struct usb_dev
* dev
, int ep_addr
)
167 if (ep_addr
>= 0 && ep_addr
< 2)
170 return &dev
->ep
[ep_addr
];
174 static const char* std_req_name
[] = {
190 static inline const char*
191 get_std_req_name(int req
)
193 return (req
>= 0 && req
<= 12) ? std_req_name
[req
] : "UNKNOWN";
198 dump_setup(struct usb_ctrlrequest
* s
)
200 dbg("%s: requesttype=%d", __FUNCTION__
, s
->requesttype
);
201 dbg("%s: request=%d %s", __FUNCTION__
, s
->request
,
202 get_std_req_name(s
->request
));
203 dbg("%s: value=0x%04x", __FUNCTION__
, s
->wValue
);
204 dbg("%s: index=%d", __FUNCTION__
, s
->index
);
205 dbg("%s: length=%d", __FUNCTION__
, s
->length
);
209 static inline usbdev_pkt_t
*
210 alloc_packet(endpoint_t
* ep
, int data_size
, void* data
)
212 usbdev_pkt_t
* pkt
= kmalloc(sizeof(usbdev_pkt_t
) + data_size
,
216 pkt
->ep_addr
= ep
->address
;
217 pkt
->size
= data_size
;
221 memcpy(pkt
->payload
, data
, data_size
);
228 * Link a packet to the tail of the enpoint's packet list.
229 * EP spinlock must be held when calling.
232 link_tail(endpoint_t
* ep
, pkt_list_t
* list
, usbdev_pkt_t
* pkt
)
235 list
->head
= list
->tail
= pkt
;
238 list
->tail
->next
= pkt
;
245 * Unlink and return a packet from the head of the given packet
246 * list. It is the responsibility of the caller to free the packet.
247 * EP spinlock must be held when calling.
249 static usbdev_pkt_t
*
250 unlink_head(pkt_list_t
* list
)
255 if (!pkt
|| !list
->count
) {
259 list
->head
= pkt
->next
;
261 list
->head
= list
->tail
= NULL
;
270 * Create and attach a new packet to the tail of the enpoint's
271 * packet list. EP spinlock must be held when calling.
273 static usbdev_pkt_t
*
274 add_packet(endpoint_t
* ep
, pkt_list_t
* list
, int size
)
276 usbdev_pkt_t
*pkt
= alloc_packet(ep
, size
, NULL
);
280 link_tail(ep
, list
, pkt
);
286 * Unlink and free a packet from the head of the enpoint's
287 * packet list. EP spinlock must be held when calling.
290 free_packet(pkt_list_t
* list
)
292 kfree(unlink_head(list
));
295 /* EP spinlock must be held when calling. */
297 flush_pkt_list(pkt_list_t
* list
)
303 /* EP spinlock must be held when calling */
305 flush_write_fifo(endpoint_t
* ep
)
307 if (ep
->reg
->write_fifo_status
>= 0) {
308 au_writel(USBDEV_FSTAT_FLUSH
| USBDEV_FSTAT_UF
|
310 ep
->reg
->write_fifo_status
);
312 //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF,
313 // ep->reg->write_fifo_status);
317 /* EP spinlock must be held when calling */
319 flush_read_fifo(endpoint_t
* ep
)
321 if (ep
->reg
->read_fifo_status
>= 0) {
322 au_writel(USBDEV_FSTAT_FLUSH
| USBDEV_FSTAT_UF
|
324 ep
->reg
->read_fifo_status
);
326 //au_writel(USBDEV_FSTAT_UF | USBDEV_FSTAT_OF,
327 // ep->reg->read_fifo_status);
332 /* EP spinlock must be held when calling. */
334 endpoint_flush(endpoint_t
* ep
)
336 // First, flush all packets
337 flush_pkt_list(&ep
->inlist
);
338 flush_pkt_list(&ep
->outlist
);
340 // Now flush the endpoint's h/w FIFO(s)
341 flush_write_fifo(ep
);
345 /* EP spinlock must be held when calling. */
347 endpoint_stall(endpoint_t
* ep
)
353 cs
= au_readl(ep
->reg
->ctrl_stat
) | USBDEV_CS_STALL
;
354 au_writel(cs
, ep
->reg
->ctrl_stat
);
357 /* EP spinlock must be held when calling. */
359 endpoint_unstall(endpoint_t
* ep
)
365 cs
= au_readl(ep
->reg
->ctrl_stat
) & ~USBDEV_CS_STALL
;
366 au_writel(cs
, ep
->reg
->ctrl_stat
);
370 endpoint_reset_datatoggle(endpoint_t
* ep
)
372 // FIXME: is this possible?
376 /* EP spinlock must be held when calling. */
378 endpoint_fifo_read(endpoint_t
* ep
)
382 usbdev_pkt_t
*pkt
= ep
->outlist
.tail
;
387 bufptr
= &pkt
->payload
[pkt
->size
];
388 while (au_readl(ep
->reg
->read_fifo_status
) & USBDEV_FSTAT_FCNT_MASK
) {
389 *bufptr
++ = au_readl(ep
->reg
->read_fifo
) & 0xff;
398 /* EP spinlock must be held when calling. */
400 endpoint_fifo_write(endpoint_t
* ep
, int index
)
404 usbdev_pkt_t
*pkt
= ep
->inlist
.head
;
409 bufptr
= &pkt
->payload
[index
];
410 while ((au_readl(ep
->reg
->write_fifo_status
) &
411 USBDEV_FSTAT_FCNT_MASK
) < EP_FIFO_DEPTH
) {
412 if (bufptr
< pkt
->payload
+ pkt
->size
) {
413 au_writel(*bufptr
++, ep
->reg
->write_fifo
);
425 * This routine is called to restart transmission of a packet.
426 * The endpoint's TSIZE must be set to the new packet's size,
427 * and DMA to the write FIFO needs to be restarted.
428 * EP spinlock must be held when calling.
431 kickstart_send_packet(endpoint_t
* ep
)
434 usbdev_pkt_t
*pkt
= ep
->inlist
.head
;
436 vdbg("%s: ep%d, pkt=%p", __FUNCTION__
, ep
->address
, pkt
);
439 err("%s: head=NULL! list->count=%d", __FUNCTION__
,
444 dma_cache_wback_inv((unsigned long)pkt
->payload
, pkt
->size
);
447 * make sure FIFO is empty
449 flush_write_fifo(ep
);
451 cs
= au_readl(ep
->reg
->ctrl_stat
) & USBDEV_CS_STALL
;
452 cs
|= (pkt
->size
<< USBDEV_CS_TSIZE_BIT
);
453 au_writel(cs
, ep
->reg
->ctrl_stat
);
455 if (get_dma_active_buffer(ep
->indma
) == 1) {
456 set_dma_count1(ep
->indma
, pkt
->size
);
457 set_dma_addr1(ep
->indma
, virt_to_phys(pkt
->payload
));
458 enable_dma_buffer1(ep
->indma
); // reenable
460 set_dma_count0(ep
->indma
, pkt
->size
);
461 set_dma_addr0(ep
->indma
, virt_to_phys(pkt
->payload
));
462 enable_dma_buffer0(ep
->indma
); // reenable
464 if (dma_halted(ep
->indma
))
465 start_dma(ep
->indma
);
470 * This routine is called when a packet in the inlist has been
471 * completed. Frees the completed packet and starts sending the
472 * next. EP spinlock must be held when calling.
474 static usbdev_pkt_t
*
475 send_packet_complete(endpoint_t
* ep
)
477 usbdev_pkt_t
*pkt
= unlink_head(&ep
->inlist
);
481 (au_readl(ep
->reg
->ctrl_stat
) & USBDEV_CS_NAK
) ?
482 PKT_STATUS_NAK
: PKT_STATUS_ACK
;
484 vdbg("%s: ep%d, %s pkt=%p, list count=%d", __FUNCTION__
,
485 ep
->address
, (pkt
->status
& PKT_STATUS_NAK
) ?
486 "NAK" : "ACK", pkt
, ep
->inlist
.count
);
490 * The write fifo should already be drained if things are
491 * working right, but flush it anyway just in case.
493 flush_write_fifo(ep
);
495 // begin transmitting next packet in the inlist
496 if (ep
->inlist
.count
) {
497 kickstart_send_packet(ep
);
504 * Add a new packet to the tail of the given ep's packet
505 * inlist. The transmit complete interrupt frees packets from
506 * the head of this list. EP spinlock must be held when calling.
509 send_packet(struct usb_dev
* dev
, usbdev_pkt_t
*pkt
, int async
)
514 if (!pkt
|| !(ep
= epaddr_to_ep(dev
, pkt
->ep_addr
)))
522 if (!async
&& list
->count
) {
524 flush_pkt_list(list
);
527 link_tail(ep
, list
, pkt
);
529 vdbg("%s: ep%d, pkt=%p, size=%d, list count=%d", __FUNCTION__
,
530 ep
->address
, pkt
, pkt
->size
, list
->count
);
532 if (list
->count
== 1) {
534 * if the packet count is one, it means the list was empty,
535 * and no more data will go out this ep until we kick-start
538 kickstart_send_packet(ep
);
545 * This routine is called to restart reception of a packet.
546 * EP spinlock must be held when calling.
549 kickstart_receive_packet(endpoint_t
* ep
)
553 // get and link a new packet for next reception
554 if (!(pkt
= add_packet(ep
, &ep
->outlist
, ep
->max_pkt_size
))) {
555 err("%s: could not alloc new packet", __FUNCTION__
);
559 if (get_dma_active_buffer(ep
->outdma
) == 1) {
560 clear_dma_done1(ep
->outdma
);
561 set_dma_count1(ep
->outdma
, ep
->max_pkt_size
);
562 set_dma_count0(ep
->outdma
, 0);
563 set_dma_addr1(ep
->outdma
, virt_to_phys(pkt
->payload
));
564 enable_dma_buffer1(ep
->outdma
); // reenable
566 clear_dma_done0(ep
->outdma
);
567 set_dma_count0(ep
->outdma
, ep
->max_pkt_size
);
568 set_dma_count1(ep
->outdma
, 0);
569 set_dma_addr0(ep
->outdma
, virt_to_phys(pkt
->payload
));
570 enable_dma_buffer0(ep
->outdma
); // reenable
572 if (dma_halted(ep
->outdma
))
573 start_dma(ep
->outdma
);
578 * This routine is called when a packet in the outlist has been
579 * completed (received) and we need to prepare for a new packet
580 * to be received. Halts DMA and computes the packet size from the
581 * remaining DMA counter. Then prepares a new packet for reception
582 * and restarts DMA. FIXME: what if another packet comes in
583 * on top of the completed packet? Counter would be wrong.
584 * EP spinlock must be held when calling.
586 static usbdev_pkt_t
*
587 receive_packet_complete(endpoint_t
* ep
)
589 usbdev_pkt_t
*pkt
= ep
->outlist
.tail
;
592 halt_dma(ep
->outdma
);
594 cs
= au_readl(ep
->reg
->ctrl_stat
);
599 pkt
->size
= ep
->max_pkt_size
- get_dma_residue(ep
->outdma
);
601 dma_cache_inv((unsigned long)pkt
->payload
, pkt
->size
);
603 * need to pull out any remaining bytes in the FIFO.
605 endpoint_fifo_read(ep
);
607 * should be drained now, but flush anyway just in case.
611 pkt
->status
= (cs
& USBDEV_CS_NAK
) ? PKT_STATUS_NAK
: PKT_STATUS_ACK
;
612 if (ep
->address
== 0 && (cs
& USBDEV_CS_SU
))
613 pkt
->status
|= PKT_STATUS_SU
;
615 vdbg("%s: ep%d, %s pkt=%p, size=%d", __FUNCTION__
,
616 ep
->address
, (pkt
->status
& PKT_STATUS_NAK
) ?
617 "NAK" : "ACK", pkt
, pkt
->size
);
619 kickstart_receive_packet(ep
);
626 ****************************************************************************
627 * Here starts the standard device request handlers. They are
628 * all called by do_setup() via a table of function pointers.
629 ****************************************************************************
633 do_get_status(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
635 switch (setup
->bRequestType
) {
637 // FIXME: send device status
639 case 0x81: // Interface
640 // FIXME: send interface status
642 case 0x82: // End Point
643 // FIXME: send endpoint status
647 endpoint_stall(&dev
->ep
[0]); // Stall End Point 0
655 do_clear_feature(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
657 switch (setup
->bRequestType
) {
659 if ((le16_to_cpu(setup
->wValue
) & 0xff) == 1)
660 dev
->remote_wakeup_en
= 0;
662 endpoint_stall(&dev
->ep
[0]);
664 case 0x02: // End Point
665 if ((le16_to_cpu(setup
->wValue
) & 0xff) == 0) {
668 le16_to_cpu(setup
->wIndex
) & 0xff);
670 endpoint_unstall(ep
);
671 endpoint_reset_datatoggle(ep
);
673 endpoint_stall(&dev
->ep
[0]);
681 do_reserved(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
683 // Invalid request, stall End Point 0
684 endpoint_stall(&dev
->ep
[0]);
689 do_set_feature(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
691 switch (setup
->bRequestType
) {
693 if ((le16_to_cpu(setup
->wValue
) & 0xff) == 1)
694 dev
->remote_wakeup_en
= 1;
696 endpoint_stall(&dev
->ep
[0]);
698 case 0x02: // End Point
699 if ((le16_to_cpu(setup
->wValue
) & 0xff) == 0) {
702 le16_to_cpu(setup
->wIndex
) & 0xff);
706 endpoint_stall(&dev
->ep
[0]);
714 do_set_address(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
716 int new_state
= dev
->state
;
717 int new_addr
= le16_to_cpu(setup
->wValue
);
719 dbg("%s: our address=%d", __FUNCTION__
, new_addr
);
721 if (new_addr
> 127) {
722 // usb spec doesn't tell us what to do, so just go to
726 } else if (dev
->address
!= new_addr
) {
727 dev
->address
= new_addr
;
731 if (dev
->state
!= new_state
) {
732 dev
->state
= new_state
;
733 /* inform function layer of usbdev state change */
734 dev
->func_cb(CB_NEW_STATE
, dev
->state
, dev
->cb_data
);
741 do_get_descriptor(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
743 int strnum
, desc_len
= le16_to_cpu(setup
->wLength
);
745 switch (le16_to_cpu(setup
->wValue
) >> 8) {
747 // send device descriptor!
748 desc_len
= desc_len
> dev
->dev_desc
->bLength
?
749 dev
->dev_desc
->bLength
: desc_len
;
750 dbg("sending device desc, size=%d", desc_len
);
751 send_packet(dev
, alloc_packet(&dev
->ep
[0], desc_len
,
755 // If the config descr index in low-byte of
756 // setup->wValue is valid, send config descr,
757 // otherwise stall ep0.
758 if ((le16_to_cpu(setup
->wValue
) & 0xff) == 0) {
759 // send config descriptor!
760 if (desc_len
<= USB_DT_CONFIG_SIZE
) {
761 dbg("sending partial config desc, size=%d",
764 alloc_packet(&dev
->ep
[0],
769 int len
= le16_to_cpu(dev
->conf_desc
->wTotalLength
);
770 dbg("sending whole config desc,"
771 " size=%d, our size=%d", desc_len
, len
);
772 desc_len
= desc_len
> len
? len
: desc_len
;
774 alloc_packet(&dev
->ep
[0],
776 dev
->full_conf_desc
),
780 endpoint_stall(&dev
->ep
[0]);
783 // If the string descr index in low-byte of setup->wValue
784 // is valid, send string descr, otherwise stall ep0.
785 strnum
= le16_to_cpu(setup
->wValue
) & 0xff;
786 if (strnum
>= 0 && strnum
< 6) {
787 struct usb_string_descriptor
*desc
=
788 dev
->str_desc
[strnum
];
789 desc_len
= desc_len
> desc
->bLength
?
790 desc
->bLength
: desc_len
;
791 dbg("sending string desc %d", strnum
);
793 alloc_packet(&dev
->ep
[0], desc_len
,
796 endpoint_stall(&dev
->ep
[0]);
800 err("invalid get desc=%d, stalled",
801 le16_to_cpu(setup
->wValue
) >> 8);
802 endpoint_stall(&dev
->ep
[0]); // Stall endpoint 0
810 do_set_descriptor(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
813 // there will be an OUT data stage (the descriptor to set)
818 do_get_configuration(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
820 // send dev->configuration
821 dbg("sending config");
822 send_packet(dev
, alloc_packet(&dev
->ep
[0], 1, &dev
->configuration
),
828 do_set_configuration(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
830 // set active config to low-byte of setup->wValue
831 dev
->configuration
= le16_to_cpu(setup
->wValue
) & 0xff;
832 dbg("set config, config=%d", dev
->configuration
);
833 if (!dev
->configuration
&& dev
->state
> DEFAULT
) {
834 dev
->state
= ADDRESS
;
835 /* inform function layer of usbdev state change */
836 dev
->func_cb(CB_NEW_STATE
, dev
->state
, dev
->cb_data
);
837 } else if (dev
->configuration
== 1) {
838 dev
->state
= CONFIGURED
;
839 /* inform function layer of usbdev state change */
840 dev
->func_cb(CB_NEW_STATE
, dev
->state
, dev
->cb_data
);
842 // FIXME: "respond with request error" - how?
849 do_get_interface(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
851 // interface must be zero.
852 if ((le16_to_cpu(setup
->wIndex
) & 0xff) || dev
->state
== ADDRESS
) {
853 // FIXME: respond with "request error". how?
854 } else if (dev
->state
== CONFIGURED
) {
855 // send dev->alternate_setting
856 dbg("sending alt setting");
857 send_packet(dev
, alloc_packet(&dev
->ep
[0], 1,
858 &dev
->alternate_setting
), 0);
866 do_set_interface(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
868 if (dev
->state
== ADDRESS
) {
869 // FIXME: respond with "request error". how?
870 } else if (dev
->state
== CONFIGURED
) {
871 dev
->interface
= le16_to_cpu(setup
->wIndex
) & 0xff;
872 dev
->alternate_setting
=
873 le16_to_cpu(setup
->wValue
) & 0xff;
874 // interface and alternate_setting must be zero
875 if (dev
->interface
|| dev
->alternate_setting
) {
876 // FIXME: respond with "request error". how?
884 do_synch_frame(struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
890 typedef ep0_stage_t (*req_method_t
)(struct usb_dev
* dev
,
891 struct usb_ctrlrequest
* setup
);
894 /* Table of the standard device request handlers */
895 static const req_method_t req_method
[] = {
904 do_get_configuration
,
905 do_set_configuration
,
912 // SETUP packet request dispatcher
914 do_setup (struct usb_dev
* dev
, struct usb_ctrlrequest
* setup
)
918 dbg("%s: req %d %s", __FUNCTION__
, setup
->bRequestType
,
919 get_std_req_name(setup
->bRequestType
));
921 if ((setup
->bRequestType
& USB_TYPE_MASK
) != USB_TYPE_STANDARD
||
922 (setup
->bRequestType
& USB_RECIP_MASK
) != USB_RECIP_DEVICE
) {
923 err("%s: invalid requesttype 0x%02x", __FUNCTION__
,
924 setup
->bRequestType
);
928 if ((setup
->bRequestType
& 0x80) == USB_DIR_OUT
&& setup
->wLength
)
929 dbg("%s: OUT phase! length=%d", __FUNCTION__
, setup
->wLength
);
931 if (setup
->bRequestType
< sizeof(req_method
)/sizeof(req_method_t
))
932 m
= req_method
[setup
->bRequestType
];
936 dev
->ep0_stage
= (*m
)(dev
, setup
);
940 * A SETUP, DATA0, or DATA1 packet has been received
941 * on the default control endpoint's fifo.
944 process_ep0_receive (struct usb_dev
* dev
)
946 endpoint_t
*ep0
= &dev
->ep
[0];
949 spin_lock(&ep0
->lock
);
951 // complete packet and prepare a new packet
952 pkt
= receive_packet_complete(ep0
);
954 // FIXME: should put a warn/err here.
955 spin_unlock(&ep0
->lock
);
959 // unlink immediately from endpoint.
960 unlink_head(&ep0
->outlist
);
962 // override current stage if h/w says it's a setup packet
963 if (pkt
->status
& PKT_STATUS_SU
)
964 dev
->ep0_stage
= SETUP_STAGE
;
966 switch (dev
->ep0_stage
) {
968 vdbg("SU bit is %s in setup stage",
969 (pkt
->status
& PKT_STATUS_SU
) ? "set" : "not set");
971 if (pkt
->size
== sizeof(struct usb_ctrlrequest
)) {
973 if (pkt
->status
& PKT_STATUS_ACK
)
974 vdbg("received SETUP");
976 vdbg("received NAK SETUP");
978 do_setup(dev
, (struct usb_ctrlrequest
*)pkt
->payload
);
980 err("%s: wrong size SETUP received", __FUNCTION__
);
984 * this setup has an OUT data stage. Of the standard
985 * device requests, only set_descriptor has this stage,
986 * so this packet is that descriptor. TODO: drop it for
987 * now, set_descriptor not implemented.
989 * Need to place a byte in the write FIFO here, to prepare
990 * to send a zero-length DATA ack packet to the host in the
993 au_writel(0, ep0
->reg
->write_fifo
);
994 dbg("received OUT stage DATAx on EP0, size=%d", pkt
->size
);
995 dev
->ep0_stage
= SETUP_STAGE
;
998 // this setup had an IN data stage, and host is ACK'ing
999 // the packet we sent during that stage.
1001 warn("received non-zero ACK on EP0??");
1004 vdbg("received ACK on EP0");
1006 dev
->ep0_stage
= SETUP_STAGE
;
1010 spin_unlock(&ep0
->lock
);
1011 // we're done processing the packet, free it
1017 * A DATA0/1 packet has been received on one of the OUT endpoints (4 or 5)
1020 process_ep_receive (struct usb_dev
* dev
, endpoint_t
*ep
)
1024 spin_lock(&ep
->lock
);
1025 pkt
= receive_packet_complete(ep
);
1026 spin_unlock(&ep
->lock
);
1028 dev
->func_cb(CB_PKT_COMPLETE
, (unsigned long)pkt
, dev
->cb_data
);
1033 /* This ISR handles the receive complete and suspend events */
1035 req_sus_intr (int irq
, void *dev_id
, struct pt_regs
*regs
)
1037 struct usb_dev
*dev
= (struct usb_dev
*) dev_id
;
1040 status
= au_readl(USBD_INTSTAT
);
1041 au_writel(status
, USBD_INTSTAT
); // ack'em
1043 if (status
& (1<<0))
1044 process_ep0_receive(dev
);
1045 if (status
& (1<<4))
1046 process_ep_receive(dev
, &dev
->ep
[4]);
1047 if (status
& (1<<5))
1048 process_ep_receive(dev
, &dev
->ep
[5]);
1052 /* This ISR handles the DMA done events on EP0 */
1054 dma_done_ep0_intr(int irq
, void *dev_id
, struct pt_regs
*regs
)
1056 struct usb_dev
*dev
= (struct usb_dev
*) dev_id
;
1058 endpoint_t
*ep0
= &dev
->ep
[0];
1061 spin_lock(&ep0
->lock
);
1062 cs0
= au_readl(ep0
->reg
->ctrl_stat
);
1064 // first check packet transmit done
1065 if ((buff_done
= get_dma_buffer_done(ep0
->indma
)) != 0) {
1066 // transmitted a DATAx packet during DATA stage
1067 // on control endpoint 0
1068 // clear DMA done bit
1069 if (buff_done
& DMA_D0
)
1070 clear_dma_done0(ep0
->indma
);
1071 if (buff_done
& DMA_D1
)
1072 clear_dma_done1(ep0
->indma
);
1074 pkt
= send_packet_complete(ep0
);
1080 * Now check packet receive done. Shouldn't get these,
1081 * the receive packet complete intr should happen
1082 * before the DMA done intr occurs.
1084 if ((buff_done
= get_dma_buffer_done(ep0
->outdma
)) != 0) {
1085 // clear DMA done bit
1086 if (buff_done
& DMA_D0
)
1087 clear_dma_done0(ep0
->outdma
);
1088 if (buff_done
& DMA_D1
)
1089 clear_dma_done1(ep0
->outdma
);
1091 //process_ep0_receive(dev);
1094 spin_unlock(&ep0
->lock
);
1097 /* This ISR handles the DMA done events on endpoints 2,3,4,5 */
1099 dma_done_ep_intr(int irq
, void *dev_id
, struct pt_regs
*regs
)
1101 struct usb_dev
*dev
= (struct usb_dev
*) dev_id
;
1104 for (i
= 2; i
< 6; i
++) {
1107 endpoint_t
*ep
= &dev
->ep
[i
];
1109 if (!ep
->active
) continue;
1111 spin_lock(&ep
->lock
);
1113 if (ep
->direction
== USB_DIR_IN
) {
1114 buff_done
= get_dma_buffer_done(ep
->indma
);
1115 if (buff_done
!= 0) {
1116 // transmitted a DATAx pkt on the IN ep
1117 // clear DMA done bit
1118 if (buff_done
& DMA_D0
)
1119 clear_dma_done0(ep
->indma
);
1120 if (buff_done
& DMA_D1
)
1121 clear_dma_done1(ep
->indma
);
1123 pkt
= send_packet_complete(ep
);
1125 spin_unlock(&ep
->lock
);
1126 dev
->func_cb(CB_PKT_COMPLETE
,
1129 spin_lock(&ep
->lock
);
1133 * Check packet receive done (OUT ep). Shouldn't get
1134 * these, the rx packet complete intr should happen
1135 * before the DMA done intr occurs.
1137 buff_done
= get_dma_buffer_done(ep
->outdma
);
1138 if (buff_done
!= 0) {
1139 // received a DATAx pkt on the OUT ep
1140 // clear DMA done bit
1141 if (buff_done
& DMA_D0
)
1142 clear_dma_done0(ep
->outdma
);
1143 if (buff_done
& DMA_D1
)
1144 clear_dma_done1(ep
->outdma
);
1146 //process_ep_receive(dev, ep);
1150 spin_unlock(&ep
->lock
);
1155 /***************************************************************************
1156 * Here begins the external interface functions
1157 ***************************************************************************
1161 * allocate a new packet
1164 usbdev_alloc_packet(int ep_addr
, int data_size
, usbdev_pkt_t
** pkt
)
1166 endpoint_t
* ep
= epaddr_to_ep(&usbdev
, ep_addr
);
1167 usbdev_pkt_t
* lpkt
= NULL
;
1169 if (!ep
|| !ep
->active
|| ep
->address
< 2)
1171 if (data_size
> ep
->max_pkt_size
)
1174 lpkt
= *pkt
= alloc_packet(ep
, data_size
, NULL
);
1185 usbdev_send_packet(int ep_addr
, usbdev_pkt_t
* pkt
)
1187 unsigned long flags
;
1191 if (!pkt
|| !(ep
= epaddr_to_ep(&usbdev
, pkt
->ep_addr
)) ||
1192 !ep
->active
|| ep
->address
< 2)
1194 if (ep
->direction
!= USB_DIR_IN
)
1197 spin_lock_irqsave(&ep
->lock
, flags
);
1198 count
= send_packet(&usbdev
, pkt
, 1);
1199 spin_unlock_irqrestore(&ep
->lock
, flags
);
1208 usbdev_receive_packet(int ep_addr
, usbdev_pkt_t
** pkt
)
1210 unsigned long flags
;
1211 usbdev_pkt_t
* lpkt
= NULL
;
1212 endpoint_t
*ep
= epaddr_to_ep(&usbdev
, ep_addr
);
1214 if (!ep
|| !ep
->active
|| ep
->address
< 2)
1216 if (ep
->direction
!= USB_DIR_OUT
)
1219 spin_lock_irqsave(&ep
->lock
, flags
);
1220 if (ep
->outlist
.count
> 1)
1221 lpkt
= unlink_head(&ep
->outlist
);
1222 spin_unlock_irqrestore(&ep
->lock
, flags
);
1225 /* no packet available */
1237 * return total queued byte count on the endpoint.
1240 usbdev_get_byte_count(int ep_addr
)
1242 unsigned long flags
;
1246 endpoint_t
* ep
= epaddr_to_ep(&usbdev
, ep_addr
);
1248 if (!ep
|| !ep
->active
|| ep
->address
< 2)
1251 if (ep
->direction
== USB_DIR_IN
) {
1254 spin_lock_irqsave(&ep
->lock
, flags
);
1255 for (scan
= list
->head
; scan
; scan
= scan
->next
)
1256 count
+= scan
->size
;
1257 spin_unlock_irqrestore(&ep
->lock
, flags
);
1259 list
= &ep
->outlist
;
1261 spin_lock_irqsave(&ep
->lock
, flags
);
1262 if (list
->count
> 1) {
1263 for (scan
= list
->head
; scan
!= list
->tail
;
1265 count
+= scan
->size
;
1267 spin_unlock_irqrestore(&ep
->lock
, flags
);
1280 au_writel(0, USBD_INTEN
); // disable usb dev ints
1281 au_writel(0, USBD_ENABLE
); // disable usb dev
1283 free_irq(AU1000_USB_DEV_REQ_INT
, &usbdev
);
1284 free_irq(AU1000_USB_DEV_SUS_INT
, &usbdev
);
1286 // free all control endpoint resources
1288 free_au1000_dma(ep
->indma
);
1289 free_au1000_dma(ep
->outdma
);
1292 // free ep resources
1293 for (i
= 2; i
< 6; i
++) {
1295 if (!ep
->active
) continue;
1297 if (ep
->direction
== USB_DIR_IN
) {
1298 free_au1000_dma(ep
->indma
);
1300 free_au1000_dma(ep
->outdma
);
1305 if (usbdev
.full_conf_desc
)
1306 kfree(usbdev
.full_conf_desc
);
1310 usbdev_init(struct usb_device_descriptor
* dev_desc
,
1311 struct usb_config_descriptor
* config_desc
,
1312 struct usb_interface_descriptor
* if_desc
,
1313 struct usb_endpoint_descriptor
* ep_desc
,
1314 struct usb_string_descriptor
* str_desc
[],
1315 void (*cb
)(usbdev_cb_type_t
, unsigned long, void *),
1322 if (dev_desc
->bNumConfigurations
> 1 ||
1323 config_desc
->bNumInterfaces
> 1 ||
1324 if_desc
->bNumEndpoints
> 4) {
1325 err("Only one config, one i/f, and no more "
1326 "than 4 ep's allowed");
1332 err("Function-layer callback required");
1337 if (dev_desc
->bMaxPacketSize0
!= USBDEV_EP0_MAX_PACKET_SIZE
) {
1338 warn("EP0 Max Packet size must be %d",
1339 USBDEV_EP0_MAX_PACKET_SIZE
);
1340 dev_desc
->bMaxPacketSize0
= USBDEV_EP0_MAX_PACKET_SIZE
;
1343 memset(&usbdev
, 0, sizeof(struct usb_dev
));
1345 usbdev
.state
= DEFAULT
;
1346 usbdev
.dev_desc
= dev_desc
;
1347 usbdev
.if_desc
= if_desc
;
1348 usbdev
.conf_desc
= config_desc
;
1350 usbdev
.str_desc
[i
] = str_desc
[i
];
1351 usbdev
.func_cb
= cb
;
1352 usbdev
.cb_data
= cb_data
;
1354 /* Initialize default control endpoint */
1355 ep0
= &usbdev
.ep
[0];
1357 ep0
->type
= CONTROL_EP
;
1358 ep0
->max_pkt_size
= USBDEV_EP0_MAX_PACKET_SIZE
;
1359 spin_lock_init(&ep0
->lock
);
1360 ep0
->desc
= NULL
; // ep0 has no descriptor
1363 ep0
->reg
= &ep_reg
[0];
1365 /* Initialize the other requested endpoints */
1366 for (i
= 0; i
< if_desc
->bNumEndpoints
; i
++) {
1367 struct usb_endpoint_descriptor
* epd
= &ep_desc
[i
];
1370 if ((epd
->bEndpointAddress
& 0x80) == USB_DIR_IN
) {
1377 err("too many IN ep's requested");
1389 err("too many OUT ep's requested");
1397 epd
->bEndpointAddress
&= ~0x0f;
1398 epd
->bEndpointAddress
|= (u8
)ep
->address
;
1399 ep
->direction
= epd
->bEndpointAddress
& 0x80;
1400 ep
->type
= epd
->bmAttributes
& 0x03;
1401 ep
->max_pkt_size
= le16_to_cpu(epd
->wMaxPacketSize
);
1402 spin_lock_init(&ep
->lock
);
1404 ep
->reg
= &ep_reg
[ep
->address
];
1408 * initialize the full config descriptor
1410 usbdev
.full_conf_desc
= fcd
= kmalloc(le16_to_cpu(config_desc
->wTotalLength
),
1413 err("failed to alloc full config descriptor");
1418 memcpy(fcd
, config_desc
, USB_DT_CONFIG_SIZE
);
1419 fcd
+= USB_DT_CONFIG_SIZE
;
1420 memcpy(fcd
, if_desc
, USB_DT_INTERFACE_SIZE
);
1421 fcd
+= USB_DT_INTERFACE_SIZE
;
1422 for (i
= 0; i
< if_desc
->bNumEndpoints
; i
++) {
1423 memcpy(fcd
, &ep_desc
[i
], USB_DT_ENDPOINT_SIZE
);
1424 fcd
+= USB_DT_ENDPOINT_SIZE
;
1427 /* Now we're ready to enable the controller */
1428 au_writel(0x0002, USBD_ENABLE
);
1430 au_writel(0x0003, USBD_ENABLE
);
1433 /* build and send config table based on ep descriptors */
1434 for (i
= 0; i
< 6; i
++) {
1437 continue; // skip dummy ep
1440 au_writel((ep
->address
<< 4) | 0x04, USBD_CONFIG
);
1441 au_writel(((ep
->max_pkt_size
& 0x380) >> 7) |
1442 (ep
->direction
>> 4) | (ep
->type
<< 4),
1444 au_writel((ep
->max_pkt_size
& 0x7f) << 1, USBD_CONFIG
);
1445 au_writel(0x00, USBD_CONFIG
);
1446 au_writel(ep
->address
, USBD_CONFIG
);
1448 u8 dir
= (i
==2 || i
==3) ? DIR_IN
: DIR_OUT
;
1449 au_writel((i
<< 4) | 0x04, USBD_CONFIG
);
1450 au_writel(((16 & 0x380) >> 7) | dir
|
1451 (BULK_EP
<< 4), USBD_CONFIG
);
1452 au_writel((16 & 0x7f) << 1, USBD_CONFIG
);
1453 au_writel(0x00, USBD_CONFIG
);
1454 au_writel(i
, USBD_CONFIG
);
1459 * Enable Receive FIFO Complete interrupts only. Transmit
1460 * complete is being handled by the DMA done interrupts.
1462 au_writel(0x31, USBD_INTEN
);
1465 * Controller is now enabled, request DMA and IRQ
1469 /* request the USB device transfer complete interrupt */
1470 if (request_irq(AU1000_USB_DEV_REQ_INT
, req_sus_intr
, SA_INTERRUPT
,
1471 "USBdev req", &usbdev
)) {
1472 err("Can't get device request intr");
1476 /* request the USB device suspend interrupt */
1477 if (request_irq(AU1000_USB_DEV_SUS_INT
, req_sus_intr
, SA_INTERRUPT
,
1478 "USBdev sus", &usbdev
)) {
1479 err("Can't get device suspend intr");
1484 /* Request EP0 DMA and IRQ */
1485 if ((ep0
->indma
= request_au1000_dma(ep_dma_id
[0].id
,
1490 err("Can't get %s DMA", ep_dma_id
[0].str
);
1494 if ((ep0
->outdma
= request_au1000_dma(ep_dma_id
[1].id
,
1496 NULL
, 0, NULL
)) < 0) {
1497 err("Can't get %s DMA", ep_dma_id
[1].str
);
1502 // Flush the ep0 buffers and FIFOs
1503 endpoint_flush(ep0
);
1504 // start packet reception on ep0
1505 kickstart_receive_packet(ep0
);
1507 /* Request DMA and IRQ for the other endpoints */
1508 for (i
= 2; i
< 6; i
++) {
1509 endpoint_t
*ep
= &usbdev
.ep
[i
];
1513 // Flush the endpoint buffers and FIFOs
1516 if (ep
->direction
== USB_DIR_IN
) {
1518 request_au1000_dma(ep_dma_id
[ep
->address
].id
,
1519 ep_dma_id
[ep
->address
].str
,
1523 if (ep
->indma
< 0) {
1524 err("Can't get %s DMA",
1525 ep_dma_id
[ep
->address
].str
);
1531 request_au1000_dma(ep_dma_id
[ep
->address
].id
,
1532 ep_dma_id
[ep
->address
].str
,
1534 if (ep
->outdma
< 0) {
1535 err("Can't get %s DMA",
1536 ep_dma_id
[ep
->address
].str
);
1541 // start packet reception on OUT endpoint
1542 kickstart_receive_packet(ep
);
1552 EXPORT_SYMBOL(usbdev_init
);
1553 EXPORT_SYMBOL(usbdev_exit
);
1554 EXPORT_SYMBOL(usbdev_alloc_packet
);
1555 EXPORT_SYMBOL(usbdev_receive_packet
);
1556 EXPORT_SYMBOL(usbdev_send_packet
);
1557 EXPORT_SYMBOL(usbdev_get_byte_count
);