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 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 28 | static void construct_urb(struct urb *urb, struct usb_host_endpoint *hep, |
| 29 | struct usb_device *dev, int endpoint_type, |
| 30 | unsigned long pipe, void *buffer, int len, |
| 31 | struct devrequest *setup, int interval) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 32 | { |
| 33 | int epnum = usb_pipeendpoint(pipe); |
| 34 | int is_in = usb_pipein(pipe); |
| 35 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 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); |
| 40 | urb->ep = hep; |
| 41 | urb->complete = musb_host_complete_urb; |
| 42 | urb->status = -EINPROGRESS; |
| 43 | urb->dev = dev; |
| 44 | urb->pipe = pipe; |
| 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; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 49 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 50 | urb->ep->desc.wMaxPacketSize = |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 51 | __cpu_to_le16(is_in ? dev->epmaxpacketin[epnum] : |
| 52 | dev->epmaxpacketout[epnum]); |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 53 | urb->ep->desc.bmAttributes = endpoint_type; |
| 54 | urb->ep->desc.bEndpointAddress = |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 55 | (is_in ? USB_DIR_IN : USB_DIR_OUT) | epnum; |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 56 | urb->ep->desc.bInterval = interval; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 59 | static int submit_urb(struct usb_hcd *hcd, struct urb *urb) |
| 60 | { |
| 61 | struct musb *host = hcd->hcd_priv; |
| 62 | int ret; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 63 | unsigned long timeout; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 64 | |
| 65 | ret = musb_urb_enqueue(hcd, urb, 0); |
| 66 | if (ret < 0) { |
| 67 | printf("Failed to enqueue URB to controller\n"); |
| 68 | return ret; |
| 69 | } |
| 70 | |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 71 | timeout = get_timer(0) + USB_TIMEOUT_MS(urb->pipe); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 72 | do { |
| 73 | if (ctrlc()) |
| 74 | return -EIO; |
| 75 | host->isr(0, host); |
Hans de Goede | e8672e3 | 2015-01-11 20:34:50 +0100 | [diff] [blame] | 76 | } while (urb->status == -EINPROGRESS && |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 77 | get_timer(0) < timeout); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 78 | |
Hans de Goede | b918a0c | 2015-01-11 20:34:52 +0100 | [diff] [blame] | 79 | if (urb->status == -EINPROGRESS) |
| 80 | musb_urb_dequeue(hcd, urb, -ETIME); |
| 81 | |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 82 | return urb->status; |
| 83 | } |
| 84 | |
| 85 | int submit_control_msg(struct usb_device *dev, unsigned long pipe, |
| 86 | void *buffer, int len, struct devrequest *setup) |
| 87 | { |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 88 | construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_CONTROL, pipe, |
| 89 | buffer, len, setup, 0); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 90 | |
| 91 | /* Fix speed for non hub-attached devices */ |
| 92 | if (!dev->parent) |
| 93 | dev->speed = host_speed; |
| 94 | |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 95 | return submit_urb(&hcd, &urb); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 100 | void *buffer, int len) |
| 101 | { |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 102 | construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_BULK, pipe, |
| 103 | buffer, len, NULL, 0); |
| 104 | return submit_urb(&hcd, &urb); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | int submit_int_msg(struct usb_device *dev, unsigned long pipe, |
| 108 | void *buffer, int len, int interval) |
| 109 | { |
Hans de Goede | accf04c | 2015-01-11 20:34:53 +0100 | [diff] [blame^] | 110 | construct_urb(&urb, &hep, dev, USB_ENDPOINT_XFER_INT, pipe, |
| 111 | buffer, len, NULL, interval); |
| 112 | return submit_urb(&hcd, &urb); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 115 | void usb_reset_root_port(void) |
| 116 | { |
| 117 | void *mbase = host->mregs; |
| 118 | u8 power; |
| 119 | |
| 120 | power = musb_readb(mbase, MUSB_POWER); |
| 121 | power &= 0xf0; |
| 122 | musb_writeb(mbase, MUSB_POWER, MUSB_POWER_RESET | power); |
| 123 | mdelay(50); |
| 124 | power = musb_readb(mbase, MUSB_POWER); |
| 125 | musb_writeb(mbase, MUSB_POWER, ~MUSB_POWER_RESET & power); |
| 126 | host->isr(0, host); |
| 127 | host_speed = (musb_readb(mbase, MUSB_POWER) & MUSB_POWER_HSMODE) ? |
| 128 | USB_SPEED_HIGH : |
| 129 | (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_FSDEV) ? |
| 130 | USB_SPEED_FULL : USB_SPEED_LOW; |
| 131 | mdelay((host_speed == USB_SPEED_LOW) ? 200 : 50); |
| 132 | } |
| 133 | |
Troy Kisky | 06d513e | 2013-10-10 15:27:56 -0700 | [diff] [blame] | 134 | 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] | 135 | { |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 136 | void *mbase; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 137 | /* USB spec says it may take up to 1 second for a device to connect */ |
| 138 | unsigned long timeout = get_timer(0) + 1000; |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 139 | |
| 140 | if (!host) { |
| 141 | printf("MUSB host is not registered\n"); |
| 142 | return -ENODEV; |
| 143 | } |
| 144 | |
| 145 | musb_start(host); |
| 146 | mbase = host->mregs; |
| 147 | do { |
| 148 | if (musb_readb(mbase, MUSB_DEVCTL) & MUSB_DEVCTL_HM) |
| 149 | break; |
Hans de Goede | dc9a391 | 2015-01-11 20:34:49 +0100 | [diff] [blame] | 150 | } while (get_timer(0) < timeout); |
| 151 | if (get_timer(0) >= timeout) |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 152 | return -ENODEV; |
| 153 | |
Hans de Goede | 90cdc10 | 2015-01-11 20:34:51 +0100 | [diff] [blame] | 154 | usb_reset_root_port(); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 155 | host->is_active = 1; |
| 156 | hcd.hcd_priv = host; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | int usb_lowlevel_stop(int index) |
| 162 | { |
| 163 | if (!host) { |
| 164 | printf("MUSB host is not registered\n"); |
| 165 | return -ENODEV; |
| 166 | } |
| 167 | |
| 168 | musb_stop(host); |
| 169 | return 0; |
| 170 | } |
| 171 | #endif /* CONFIG_MUSB_HOST */ |
| 172 | |
| 173 | #ifdef CONFIG_MUSB_GADGET |
| 174 | static struct musb *gadget; |
| 175 | |
| 176 | int usb_gadget_handle_interrupts(void) |
| 177 | { |
Heiko Schocher | 7a34278 | 2013-06-04 11:10:01 +0200 | [diff] [blame] | 178 | WATCHDOG_RESET(); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 179 | if (!gadget || !gadget->isr) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | return gadget->isr(0, gadget); |
| 183 | } |
| 184 | |
| 185 | int usb_gadget_register_driver(struct usb_gadget_driver *driver) |
| 186 | { |
| 187 | int ret; |
| 188 | |
Bin Liu | 76b09b8 | 2013-03-21 05:27:49 +0000 | [diff] [blame] | 189 | if (!driver || driver->speed < USB_SPEED_FULL || !driver->bind || |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 190 | !driver->setup) { |
| 191 | printf("bad parameter.\n"); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
| 195 | if (!gadget) { |
| 196 | printf("Controller uninitialized\n"); |
| 197 | return -ENXIO; |
| 198 | } |
| 199 | |
| 200 | ret = musb_gadget_start(&gadget->g, driver); |
| 201 | if (ret < 0) { |
| 202 | printf("gadget_start failed with %d\n", ret); |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | ret = driver->bind(&gadget->g); |
| 207 | if (ret < 0) { |
| 208 | printf("bind failed with %d\n", ret); |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | int usb_gadget_unregister_driver(struct usb_gadget_driver *driver) |
| 216 | { |
Rob Herring | 078d730 | 2014-04-18 08:54:30 -0500 | [diff] [blame] | 217 | if (driver->disconnect) |
| 218 | driver->disconnect(&gadget->g); |
| 219 | if (driver->unbind) |
| 220 | driver->unbind(&gadget->g); |
Ilya Yanok | eb81955 | 2012-11-06 13:48:21 +0000 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | #endif /* CONFIG_MUSB_GADGET */ |
| 224 | |
| 225 | int musb_register(struct musb_hdrc_platform_data *plat, void *bdata, |
| 226 | void *ctl_regs) |
| 227 | { |
| 228 | struct musb **musbp; |
| 229 | |
| 230 | switch (plat->mode) { |
| 231 | #ifdef CONFIG_MUSB_HOST |
| 232 | case MUSB_HOST: |
| 233 | musbp = &host; |
| 234 | break; |
| 235 | #endif |
| 236 | #ifdef CONFIG_MUSB_GADGET |
| 237 | case MUSB_PERIPHERAL: |
| 238 | musbp = &gadget; |
| 239 | break; |
| 240 | #endif |
| 241 | default: |
| 242 | return -EINVAL; |
| 243 | } |
| 244 | |
| 245 | *musbp = musb_init_controller(plat, (struct device *)bdata, ctl_regs); |
| 246 | if (!musbp) { |
| 247 | printf("Failed to init the controller\n"); |
| 248 | return -EIO; |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |