Ilya Yanok | 06bb920 | 2012-11-06 13:48:21 +0000 | [diff] [blame^] | 1 | #include <common.h> |
| 2 | #include <asm/errno.h> |
| 3 | #include <linux/usb/ch9.h> |
| 4 | #include <linux/usb/gadget.h> |
| 5 | |
| 6 | #define __UBOOT__ |
| 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 |
| 15 | static struct musb *host; |
| 16 | static struct usb_hcd hcd; |
| 17 | static enum usb_device_speed host_speed; |
| 18 | |
| 19 | static 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 | |
| 25 | static struct usb_host_endpoint hep; |
| 26 | static struct urb urb; |
| 27 | |
| 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) |
| 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 | |
| 62 | static 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 | |
| 84 | int 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 | /* Fix speed for non hub-attached devices */ |
| 91 | if (!dev->parent) |
| 92 | dev->speed = host_speed; |
| 93 | |
| 94 | return submit_urb(&hcd, urb); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | int 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 | |
| 106 | int 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 | |
| 114 | int usb_lowlevel_init(int index, 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 | |
| 150 | int 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 /* CONFIG_MUSB_HOST */ |
| 161 | |
| 162 | #ifdef CONFIG_MUSB_GADGET |
| 163 | static struct musb *gadget; |
| 164 | |
| 165 | int usb_gadget_handle_interrupts(void) |
| 166 | { |
| 167 | if (!gadget || !gadget->isr) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | return gadget->isr(0, gadget); |
| 171 | } |
| 172 | |
| 173 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 174 | { |
| 175 | int ret; |
| 176 | |
| 177 | if (!driver || driver->speed < USB_SPEED_HIGH || !driver->bind || |
| 178 | !driver->setup) { |
| 179 | printf("bad parameter.\n"); |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | if (!gadget) { |
| 184 | printf("Controller uninitialized\n"); |
| 185 | return -ENXIO; |
| 186 | } |
| 187 | |
| 188 | ret = musb_gadget_start(&gadget->g, driver); |
| 189 | if (ret < 0) { |
| 190 | printf("gadget_start failed with %d\n", ret); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | ret = driver->bind(&gadget->g); |
| 195 | if (ret < 0) { |
| 196 | printf("bind failed with %d\n", ret); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 204 | { |
| 205 | /* TODO: implement me */ |
| 206 | return 0; |
| 207 | } |
| 208 | #endif /* CONFIG_MUSB_GADGET */ |
| 209 | |
| 210 | int musb_register(struct musb_hdrc_platform_data *plat, void *bdata, |
| 211 | void *ctl_regs) |
| 212 | { |
| 213 | struct musb **musbp; |
| 214 | |
| 215 | switch (plat->mode) { |
| 216 | #ifdef CONFIG_MUSB_HOST |
| 217 | case MUSB_HOST: |
| 218 | musbp = &host; |
| 219 | break; |
| 220 | #endif |
| 221 | #ifdef CONFIG_MUSB_GADGET |
| 222 | case MUSB_PERIPHERAL: |
| 223 | musbp = &gadget; |
| 224 | break; |
| 225 | #endif |
| 226 | default: |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs); |
| 231 | if (!musbp) { |
| 232 | printf("Failed to init the controller\n"); |
| 233 | return -EIO; |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |