4 #include <linux/usb/ch9.h>
5 #include <linux/usb/gadget.h>
8 #include "linux-compat.h"
9 #include "usb-compat.h"
10 #include "musb_core.h"
11 #include "musb_host.h"
12 #include "musb_gadget.h"
14 #ifdef CONFIG_MUSB_HOST
15 static struct musb *host;
16 static struct usb_hcd hcd;
17 static enum usb_device_speed host_speed;
19 static void musb_host_complete_urb(struct urb *urb)
21 urb->dev->status &= ~USB_ST_NOT_PROC;
22 urb->dev->act_len = urb->actual_length;
25 static struct usb_host_endpoint hep;
26 static struct urb urb;
28 static struct urb *construct_urb(struct usb_device *dev, int endpoint_type,
29 unsigned long pipe, void *buffer, int len,
30 struct devrequest *setup, int interval)
32 int epnum = usb_pipeendpoint(pipe);
33 int is_in = usb_pipein(pipe);
35 memset(&urb, 0, sizeof(struct urb));
36 memset(&hep, 0, sizeof(struct usb_host_endpoint));
37 INIT_LIST_HEAD(&hep.urb_list);
38 INIT_LIST_HEAD(&urb.urb_list);
40 urb.complete = musb_host_complete_urb;
41 urb.status = -EINPROGRESS;
44 urb.transfer_buffer = buffer;
45 urb.transfer_dma = (unsigned long)buffer;
46 urb.transfer_buffer_length = len;
47 urb.setup_packet = (unsigned char *)setup;
49 urb.ep->desc.wMaxPacketSize =
50 __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] :
51 dev->epmaxpacketout[epnum]);
52 urb.ep->desc.bmAttributes = endpoint_type;
53 urb.ep->desc.bEndpointAddress =
54 (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum;
55 urb.ep->desc.bInterval = interval;
60 static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
62 struct musb *host = hcd->hcd_priv;
64 unsigned long timeout;
66 ret = musb_urb_enqueue(hcd, urb, 0);
68 printf("Failed to enqueue URB to controller\n");
72 timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe);
77 } while (urb->status == -EINPROGRESS &&
78 get_timer(0) < timeout);
80 if (urb->status == -EINPROGRESS)
81 musb_urb_dequeue(hcd, urb, -ETIME);
86 int submit_control_msg(struct usb_device *dev, unsigned long pipe,
87 void *buffer, int len, struct devrequest *setup)
89 struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_CONTROL, pipe,
90 buffer, len, setup, 0);
92 /* Fix speed for non hub-attached devices */
94 dev->speed = host_speed;
96 return submit_urb(&hcd, urb);
100 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
101 void *buffer, int len)
103 struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_BULK, pipe,
104 buffer, len, NULL, 0);
105 return submit_urb(&hcd, urb);
108 int submit_int_msg(struct usb_device *dev, unsigned long pipe,
109 void *buffer, int len, int interval)
111 struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_INT, pipe,
112 buffer, len, NULL, interval);
113 return submit_urb(&hcd, urb);
116 void usb_reset_root_port(void)
118 void *mbase = host->mregs;
121 power = musb_readb(mbase, MUSB_POWER);
123 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
125 power = musb_readb(mbase, MUSB_POWER);
126 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
128 host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
130 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
131 USB_SPEED_FULL : USB_SPEED_LOW;
132 mdelay((host_speed == USB_SPEED_LOW) ? 200 : 50);
135 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
138 /* USB spec says it may take up to 1 second for a device to connect */
139 unsigned long timeout = get_timer(0) + 1000;
142 printf("MUSB host is not registered\n");
149 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
151 } while (get_timer(0) < timeout);
152 if (get_timer(0) >= timeout)
155 usb_reset_root_port();
162 int usb_lowlevel_stop(int index)
165 printf("MUSB host is not registered\n");
172 #endif /* CONFIG_MUSB_HOST */
174 #ifdef CONFIG_MUSB_GADGET
175 static struct musb *gadget;
177 int usb_gadget_handle_interrupts(void)
180 if (!gadget || !gadget->isr)
183 return gadget->isr(0, gadget);
186 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
190 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
192 printf("bad parameter.\n");
197 printf("Controller uninitialized\n");
201 ret = musb_gadget_start(&gadget->g, driver);
203 printf("gadget_start failed with %d\n", ret);
207 ret = driver->bind(&gadget->g);
209 printf("bind failed with %d\n", ret);
216 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
218 if (driver->disconnect)
219 driver->disconnect(&gadget->g);
221 driver->unbind(&gadget->g);
224 #endif /* CONFIG_MUSB_GADGET */
226 int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
231 switch (plat->mode) {
232 #ifdef CONFIG_MUSB_HOST
237 #ifdef CONFIG_MUSB_GADGET
238 case MUSB_PERIPHERAL:
246 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
248 printf("Failed to init the controller\n");