Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 1 | #include <common.h> |
Heiko Schocher | 7a34278 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 2 | #include <watchdog.h> |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 3 | #include <asm/errno.h> |
| 4 | #include <linux/usb/ch9.h> |
| 5 | #include <linux/usb/gadget.h> |
| 6 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 60 | static int submit_urb(struct usb_hcd *hcd, struct urb *urb) |
| 61 | { |
| 62 | struct musb *host = hcd->hcd_priv; |
| 63 | int ret; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame^] | 64 | unsigned long timeout; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 65 | |
| 66 | ret = musb_urb_enqueue(hcd, urb, 0); |
| 67 | if (ret < 0) { |
| 68 | printf("Failed to enqueue URB to controller\n"); |
| 69 | return ret; |
| 70 | } |
| 71 | |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame^] | 72 | timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 73 | do { |
| 74 | if (ctrlc()) |
| 75 | return -EIO; |
| 76 | host->isr(0, host); |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame^] | 77 | } while ((urb->dev->status & USB_ST_NOT_PROC) && |
| 78 | get_timer(0) < timeout); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 79 | |
| 80 | return urb->status; |
| 81 | } |
| 82 | |
| 83 | int submit_control_msg(struct usb_device *dev, unsigned long pipe, |
| 84 | void *buffer, int len, struct devrequest *setup) |
| 85 | { |
| 86 | struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_CONTROL, pipe, |
| 87 | buffer, len, setup, 0); |
| 88 | |
| 89 | /* Fix speed for non hub-attached devices */ |
| 90 | if (!dev->parent) |
| 91 | dev->speed = host_speed; |
| 92 | |
| 93 | return submit_urb(&hcd, urb); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 98 | void *buffer, int len) |
| 99 | { |
| 100 | struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_BULK, pipe, |
| 101 | buffer, len, NULL, 0); |
| 102 | return submit_urb(&hcd, urb); |
| 103 | } |
| 104 | |
| 105 | int submit_int_msg(struct usb_device *dev, unsigned long pipe, |
| 106 | void *buffer, int len, int interval) |
| 107 | { |
| 108 | struct urb *urb = construct_urb(dev, USB_ENDPOINT_XFER_INT, pipe, |
| 109 | buffer, len, NULL, interval); |
| 110 | return submit_urb(&hcd, urb); |
| 111 | } |
| 112 | |
Troy Kisky | 06d513e | 2013-10-10 15:27:56 -0700 | [diff] [blame] | 113 | int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 114 | { |
| 115 | u8 power; |
| 116 | void *mbase; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame^] | 117 | /* USB spec says it may take up to 1 second for a device to connect */ |
| 118 | unsigned long timeout = get_timer(0) + 1000; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame^] | 130 | } while (get_timer(0) < timeout); |
| 131 | if (get_timer(0) >= timeout) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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 | { |
Heiko Schocher | 7a34278 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 167 | WATCHDOG_RESET(); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 168 | if (!gadget || !gadget->isr) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | return gadget->isr(0, gadget); |
| 172 | } |
| 173 | |
| 174 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 175 | { |
| 176 | int ret; |
| 177 | |
Bin Liu | 76b09b8 | 2013-03-21 05:27:49 +0000 | [diff] [blame] | 178 | if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind || |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 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 | |
| 204 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 205 | { |
Rob Herring | 078d730 | 2014-04-18 08:54:30 -0500 | [diff] [blame] | 206 | if (driver->disconnect) |
| 207 | driver->disconnect(&gadget->g); |
| 208 | if (driver->unbind) |
| 209 | driver->unbind(&gadget->g); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | #endif /* CONFIG_MUSB_GADGET */ |
| 213 | |
| 214 | int 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 | } |