4 #include <linux/usb/ch9.h>
5 #include <linux/usb/gadget.h>
9 #include "linux-compat.h"
10 #include "usb-compat.h"
11 #include "musb_core.h"
12 #include "musb_host.h"
13 #include "musb_gadget.h"
15 #ifdef CONFIG_MUSB_HOST
16 static struct musb
*host
;
17 static struct usb_hcd hcd
;
18 static enum usb_device_speed host_speed
;
20 static void musb_host_complete_urb(struct urb
*urb
)
22 urb
->dev
->status
&= ~USB_ST_NOT_PROC
;
23 urb
->dev
->act_len
= urb
->actual_length
;
26 static struct usb_host_endpoint hep
;
27 static struct urb urb
;
29 static struct urb
*construct_urb(struct usb_device
*dev
, int endpoint_type
,
30 unsigned long pipe
, void *buffer
, int len
,
31 struct devrequest
*setup
, int interval
)
33 int epnum
= usb_pipeendpoint(pipe
);
34 int is_in
= usb_pipein(pipe
);
36 memset(&urb
, 0, sizeof(struct urb
));
37 memset(&hep
, 0, sizeof(struct usb_host_endpoint
));
38 INIT_LIST_HEAD(&hep
.urb_list
);
39 INIT_LIST_HEAD(&urb
.urb_list
);
41 urb
.complete
= musb_host_complete_urb
;
42 urb
.status
= -EINPROGRESS
;
45 urb
.transfer_buffer
= buffer
;
46 urb
.transfer_dma
= (unsigned long)buffer
;
47 urb
.transfer_buffer_length
= len
;
48 urb
.setup_packet
= (unsigned char *)setup
;
50 urb
.ep
->desc
.wMaxPacketSize
=
51 __cpu_to_le16(is_in
? dev
->epmaxpacketin
[epnum
] :
52 dev
->epmaxpacketout
[epnum
]);
53 urb
.ep
->desc
.bmAttributes
= endpoint_type
;
54 urb
.ep
->desc
.bEndpointAddress
=
55 (is_in
? USB_DIR_IN
: USB_DIR_OUT
) | epnum
;
56 urb
.ep
->desc
.bInterval
= interval
;
61 #define MUSB_HOST_TIMEOUT 0x3ffffff
63 static int submit_urb(struct usb_hcd
*hcd
, struct urb
*urb
)
65 struct musb
*host
= hcd
->hcd_priv
;
69 ret
= musb_urb_enqueue(hcd
, urb
, 0);
71 printf("Failed to enqueue URB to controller\n");
75 timeout
= MUSB_HOST_TIMEOUT
;
80 } while ((urb
->dev
->status
& USB_ST_NOT_PROC
) && --timeout
);
85 int submit_control_msg(struct usb_device
*dev
, unsigned long pipe
,
86 void *buffer
, int len
, struct devrequest
*setup
)
88 struct urb
*urb
= construct_urb(dev
, USB_ENDPOINT_XFER_CONTROL
, pipe
,
89 buffer
, len
, setup
, 0);
91 /* Fix speed for non hub-attached devices */
93 dev
->speed
= host_speed
;
95 return submit_urb(&hcd
, urb
);
99 int submit_bulk_msg(struct usb_device
*dev
, unsigned long pipe
,
100 void *buffer
, int len
)
102 struct urb
*urb
= construct_urb(dev
, USB_ENDPOINT_XFER_BULK
, pipe
,
103 buffer
, len
, NULL
, 0);
104 return submit_urb(&hcd
, urb
);
107 int submit_int_msg(struct usb_device
*dev
, unsigned long pipe
,
108 void *buffer
, int len
, int interval
)
110 struct urb
*urb
= construct_urb(dev
, USB_ENDPOINT_XFER_INT
, pipe
,
111 buffer
, len
, NULL
, interval
);
112 return submit_urb(&hcd
, urb
);
115 int usb_lowlevel_init(int index
, enum usb_init_type init
, void **controller
)
119 int timeout
= MUSB_HOST_TIMEOUT
;
122 printf("MUSB host is not registered\n");
129 if (musb_readb(mbase
, MUSB_DEVCTL
) & MUSB_DEVCTL_HM
)
135 power
= musb_readb(mbase
, MUSB_POWER
);
136 musb_writeb(mbase
, MUSB_POWER
, MUSB_POWER_RESET
| power
);
138 power
= musb_readb(mbase
, MUSB_POWER
);
139 musb_writeb(mbase
, MUSB_POWER
, ~MUSB_POWER_RESET
& power
);
141 host_speed
= (musb_readb(mbase
, MUSB_POWER
) & MUSB_POWER_HSMODE
) ?
143 (musb_readb(mbase
, MUSB_DEVCTL
) & MUSB_DEVCTL_FSDEV
) ?
144 USB_SPEED_FULL
: USB_SPEED_LOW
;
151 int usb_lowlevel_stop(int index
)
154 printf("MUSB host is not registered\n");
161 #endif /* CONFIG_MUSB_HOST */
163 #ifdef CONFIG_MUSB_GADGET
164 static struct musb
*gadget
;
166 int usb_gadget_handle_interrupts(void)
169 if (!gadget
|| !gadget
->isr
)
172 return gadget
->isr(0, gadget
);
175 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
179 if (!driver
|| driver
->speed
< USB_SPEED_FULL
|| !driver
->bind
||
181 printf("bad parameter.\n");
186 printf("Controller uninitialized\n");
190 ret
= musb_gadget_start(&gadget
->g
, driver
);
192 printf("gadget_start failed with %d\n", ret
);
196 ret
= driver
->bind(&gadget
->g
);
198 printf("bind failed with %d\n", ret
);
205 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
207 /* TODO: implement me */
210 #endif /* CONFIG_MUSB_GADGET */
212 int musb_register(struct musb_hdrc_platform_data
*plat
, void *bdata
,
217 switch (plat
->mode
) {
218 #ifdef CONFIG_MUSB_HOST
223 #ifdef CONFIG_MUSB_GADGET
224 case MUSB_PERIPHERAL
:
232 *musbp
= musb_init_controller(plat
, (struct device
*)bdata
, ctl_regs
);
234 printf("Failed to init the controller\n");