1#include <common.h>
2#include <watchdog.h>
3#include <asm/errno.h>
4#include <linux/usb/ch9.h>
5#include <linux/usb/gadget.h>
6
7#include <usb.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"
13
14#ifdef CONFIG_MUSB_HOST
15static struct musb *host;
16static struct usb_hcd hcd;
17static enum usb_device_speed host_speed;
18
19static void musb_host_complete_urb(struct urb *urb)
20{
21 urb->dev->status &= ~USB_ST_NOT_PROC;
22 urb->dev->act_len = urb->actual_length;
23}
24
25static struct usb_host_endpoint hep;
26static struct urb urb;
27
28static 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)
31{
32 int epnum = usb_pipeendpoint(pipe);
33 int is_in = usb_pipein(pipe);
34
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);
39 urb.ep = &hep;
40 urb.complete = musb_host_complete_urb;
41 urb.status = -EINPROGRESS;
42 urb.dev = dev;
43 urb.pipe = pipe;
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;
48
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;
56
57 return &urb;
58}
59
60#define MUSB_HOST_TIMEOUT 0x3ffffff
61
62static int submit_urb(struct usb_hcd *hcd, struct urb *urb)
63{
64 struct musb *host = hcd->hcd_priv;
65 int ret;
66 int timeout;
67
68 ret = musb_urb_enqueue(hcd, urb, 0);
69 if (ret < 0) {
70 printf("Failed to enqueue URB to controller\n");
71 return ret;
72 }
73
74 timeout = MUSB_HOST_TIMEOUT;
75 do {
76 if (ctrlc())
77 return -EIO;
78 host->isr(0, host);
79 } while ((urb->dev->status & USB_ST_NOT_PROC) && --timeout);
80
81 return urb->status;
82}
83
84int submit_control_msg(struct usb_device *dev, unsigned long pipe,
85 void *buffer, int len, struct devrequest *setup)
86{
87 struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_CONTROL, pipe,
88 buffer, len, setup, 0);
89
90
91 if (!dev->parent)
92 dev->speed = host_speed;
93
94 return submit_urb(&hcd, urb);
95}
96
97
98int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
99 void *buffer, int len)
100{
101 struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_BULK, pipe,
102 buffer, len, NULL, 0);
103 return submit_urb(&hcd, urb);
104}
105
106int submit_int_msg(struct usb_device *dev, unsigned long pipe,
107 void *buffer, int len, int interval)
108{
109 struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_INT, pipe,
110 buffer, len, NULL, interval);
111 return submit_urb(&hcd, urb);
112}
113
114int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
115{
116 u8 power;
117 void *mbase;
118 int timeout = MUSB_HOST_TIMEOUT;
119
120 if (!host) {
121 printf("MUSB host is not registered\n");
122 return -ENODEV;
123 }
124
125 musb_start(host);
126 mbase = host->mregs;
127 do {
128 if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM)
129 break;
130 } while (--timeout);
131 if (!timeout)
132 return -ENODEV;
133
134 power = musb_readb(mbase, MUSB_POWER);
135 musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power);
136 udelay(30000);
137 power = musb_readb(mbase, MUSB_POWER);
138 musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power);
139 host->isr(0, host);
140 host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ?
141 USB_SPEED_HIGH :
142 (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ?
143 USB_SPEED_FULL : USB_SPEED_LOW;
144 host->is_active = 1;
145 hcd.hcd_priv = host;
146
147 return 0;
148}
149
150int usb_lowlevel_stop(int index)
151{
152 if (!host) {
153 printf("MUSB host is not registered\n");
154 return -ENODEV;
155 }
156
157 musb_stop(host);
158 return 0;
159}
160#endif
161
162#ifdef CONFIG_MUSB_GADGET
163static struct musb *gadget;
164
165int usb_gadget_handle_interrupts(void)
166{
167 WATCHDOG_RESET();
168 if (!gadget || !gadget->isr)
169 return -EINVAL;
170
171 return gadget->isr(0, gadget);
172}
173
174int usb_gadget_register_driver(struct usb_gadget_driver *driver)
175{
176 int ret;
177
178 if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind ||
179 !driver->setup) {
180 printf("bad parameter.\n");
181 return -EINVAL;
182 }
183
184 if (!gadget) {
185 printf("Controller uninitialized\n");
186 return -ENXIO;
187 }
188
189 ret = musb_gadget_start(&gadget->g, driver);
190 if (ret < 0) {
191 printf("gadget_start failed with %d\n", ret);
192 return ret;
193 }
194
195 ret = driver->bind(&gadget->g);
196 if (ret < 0) {
197 printf("bind failed with %d\n", ret);
198 return ret;
199 }
200
201 return 0;
202}
203
204int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
205{
206 if (driver->disconnect)
207 driver->disconnect(&gadget->g);
208 if (driver->unbind)
209 driver->unbind(&gadget->g);
210 return 0;
211}
212#endif
213
214int musb_register(struct musb_hdrc_platform_data *plat, void *bdata,
215 void *ctl_regs)
216{
217 struct musb **musbp;
218
219 switch (plat->mode) {
220#ifdef CONFIG_MUSB_HOST
221 case MUSB_HOST:
222 musbp = &host;
223 break;
224#endif
225#ifdef CONFIG_MUSB_GADGET
226 case MUSB_PERIPHERAL:
227 musbp = &gadget;
228 break;
229#endif
230 default:
231 return -EINVAL;
232 }
233
234 *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs);
235 if (!musbp) {
236 printf("Failed to init the controller\n");
237 return -EIO;
238 }
239
240 return 0;
241}
242