Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame^] | 5 | * Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved. |
Shashank Mittal | 23b8f42 | 2010-04-16 19:27:21 -0700 | [diff] [blame] | 6 | * |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 14 | * the documentation and/or other materials provided with the |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 15 | * distribution. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 24 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 25 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 27 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <debug.h> |
| 34 | #include <platform/iomap.h> |
| 35 | #include <platform/irqs.h> |
| 36 | #include <platform/interrupts.h> |
Greg Grisco | d2471ef | 2011-07-14 13:00:42 -0700 | [diff] [blame] | 37 | #include <platform/timer.h> |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 38 | #include <kernel/thread.h> |
| 39 | #include <reg.h> |
| 40 | |
| 41 | #include <dev/udc.h> |
| 42 | |
| 43 | #include "hsusb.h" |
| 44 | |
| 45 | /* common code - factor out into a shared file */ |
| 46 | |
| 47 | struct udc_descriptor { |
| 48 | struct udc_descriptor *next; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 49 | unsigned short tag; /* ((TYPE << 8) | NUM) */ |
| 50 | unsigned short len; /* total length */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 51 | unsigned char data[0]; |
| 52 | }; |
| 53 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 54 | struct udc_descriptor *udc_descriptor_alloc(unsigned type, unsigned num, |
| 55 | unsigned len) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 56 | { |
| 57 | struct udc_descriptor *desc; |
| 58 | if ((len > 255) || (len < 2) || (num > 255) || (type > 255)) |
| 59 | return 0; |
| 60 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 61 | if (!(desc = malloc(sizeof(struct udc_descriptor) + len))) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 62 | return 0; |
| 63 | |
| 64 | desc->next = 0; |
| 65 | desc->tag = (type << 8) | num; |
| 66 | desc->len = len; |
| 67 | desc->data[0] = len; |
| 68 | desc->data[1] = type; |
| 69 | |
| 70 | return desc; |
| 71 | } |
| 72 | |
| 73 | static struct udc_descriptor *desc_list = 0; |
| 74 | static unsigned next_string_id = 1; |
| 75 | |
| 76 | void udc_descriptor_register(struct udc_descriptor *desc) |
| 77 | { |
| 78 | desc->next = desc_list; |
| 79 | desc_list = desc; |
| 80 | } |
| 81 | |
| 82 | unsigned udc_string_desc_alloc(const char *str) |
| 83 | { |
| 84 | unsigned len; |
| 85 | struct udc_descriptor *desc; |
| 86 | unsigned char *data; |
| 87 | |
| 88 | if (next_string_id > 255) |
| 89 | return 0; |
| 90 | |
| 91 | if (!str) |
| 92 | return 0; |
| 93 | |
| 94 | len = strlen(str); |
| 95 | desc = udc_descriptor_alloc(TYPE_STRING, next_string_id, len * 2 + 2); |
| 96 | if (!desc) |
| 97 | return 0; |
| 98 | next_string_id++; |
| 99 | |
| 100 | /* expand ascii string to utf16 */ |
| 101 | data = desc->data + 2; |
| 102 | while (len-- > 0) { |
| 103 | *data++ = *str++; |
| 104 | *data++ = 0; |
| 105 | } |
| 106 | |
| 107 | udc_descriptor_register(desc); |
| 108 | return desc->tag & 0xff; |
| 109 | } |
| 110 | |
| 111 | /* end of common code */ |
| 112 | |
Ajay Dudani | 7d60552 | 2010-10-01 19:52:37 -0700 | [diff] [blame] | 113 | __WEAK void hsusb_clock_init(void) |
| 114 | { |
Greg Grisco | d625055 | 2011-06-29 14:40:23 -0700 | [diff] [blame] | 115 | return; |
Ajay Dudani | 7d60552 | 2010-10-01 19:52:37 -0700 | [diff] [blame] | 116 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 117 | |
| 118 | #if 1 |
| 119 | #define DBG(x...) do {} while(0) |
| 120 | #else |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame^] | 121 | #define DBG(x...) dprintf(ALWAYS, x) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 122 | #endif |
| 123 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 124 | #define usb_status(a,b) |
| 125 | |
| 126 | struct usb_request { |
| 127 | struct udc_request req; |
| 128 | struct ept_queue_item *item; |
| 129 | }; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 130 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 131 | struct udc_endpoint { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 132 | struct udc_endpoint *next; |
| 133 | unsigned bit; |
| 134 | struct ept_queue_head *head; |
| 135 | struct usb_request *req; |
| 136 | unsigned char num; |
| 137 | unsigned char in; |
| 138 | unsigned short maxpkt; |
| 139 | }; |
| 140 | |
| 141 | struct udc_endpoint *ept_list = 0; |
| 142 | struct ept_queue_head *epts = 0; |
| 143 | |
| 144 | static int usb_online = 0; |
| 145 | static int usb_highspeed = 0; |
| 146 | |
| 147 | static struct udc_device *the_device; |
| 148 | static struct udc_gadget *the_gadget; |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 149 | static unsigned test_mode = 0; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 150 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 151 | struct udc_endpoint *_udc_endpoint_alloc(unsigned num, unsigned in, |
| 152 | unsigned max_pkt) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 153 | { |
| 154 | struct udc_endpoint *ept; |
| 155 | unsigned cfg; |
| 156 | |
| 157 | ept = malloc(sizeof(*ept)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 158 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 159 | ept->maxpkt = max_pkt; |
| 160 | ept->num = num; |
| 161 | ept->in = !!in; |
| 162 | ept->req = 0; |
| 163 | |
| 164 | cfg = CONFIG_MAX_PKT(max_pkt) | CONFIG_ZLT; |
| 165 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 166 | if (ept->in) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 167 | ept->bit = EPT_TX(ept->num); |
| 168 | } else { |
| 169 | ept->bit = EPT_RX(ept->num); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 170 | if (num == 0) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 171 | cfg |= CONFIG_IOS; |
| 172 | } |
| 173 | |
| 174 | ept->head = epts + (num * 2) + (ept->in); |
| 175 | ept->head->config = cfg; |
| 176 | |
| 177 | ept->next = ept_list; |
| 178 | ept_list = ept; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 179 | |
| 180 | DBG("ept%d %s @%p/%p max=%d bit=%x\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 181 | num, in ? "in" : "out", ept, ept->head, max_pkt, ept->bit); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 182 | |
| 183 | return ept; |
| 184 | } |
| 185 | |
| 186 | static unsigned ept_alloc_table = EPT_TX(0) | EPT_RX(0); |
| 187 | |
| 188 | struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt) |
| 189 | { |
| 190 | struct udc_endpoint *ept; |
| 191 | unsigned n; |
| 192 | unsigned in; |
| 193 | |
| 194 | if (type == UDC_TYPE_BULK_IN) { |
| 195 | in = 1; |
| 196 | } else if (type == UDC_TYPE_BULK_OUT) { |
| 197 | in = 0; |
| 198 | } else { |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | for (n = 1; n < 16; n++) { |
| 203 | unsigned bit = in ? EPT_TX(n) : EPT_RX(n); |
| 204 | if (ept_alloc_table & bit) |
| 205 | continue; |
| 206 | ept = _udc_endpoint_alloc(n, in, maxpkt); |
| 207 | if (ept) |
| 208 | ept_alloc_table |= bit; |
| 209 | return ept; |
| 210 | } |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | void udc_endpoint_free(struct udc_endpoint *ept) |
| 215 | { |
| 216 | /* todo */ |
| 217 | } |
| 218 | |
| 219 | static void endpoint_enable(struct udc_endpoint *ept, unsigned yes) |
| 220 | { |
| 221 | unsigned n = readl(USB_ENDPTCTRL(ept->num)); |
| 222 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 223 | if (yes) { |
| 224 | if (ept->in) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 225 | n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK); |
| 226 | } else { |
| 227 | n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK); |
| 228 | } |
| 229 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 230 | if (ept->num != 0) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 231 | /* XXX should be more dynamic... */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 232 | if (usb_highspeed) { |
| 233 | ept->head->config = |
| 234 | CONFIG_MAX_PKT(512) | CONFIG_ZLT; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 235 | } else { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 236 | ept->head->config = |
| 237 | CONFIG_MAX_PKT(64) | CONFIG_ZLT; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | writel(n, USB_ENDPTCTRL(ept->num)); |
| 242 | } |
| 243 | |
| 244 | struct udc_request *udc_request_alloc(void) |
| 245 | { |
| 246 | struct usb_request *req; |
| 247 | req = malloc(sizeof(*req)); |
| 248 | req->req.buf = 0; |
| 249 | req->req.length = 0; |
| 250 | req->item = memalign(32, 32); |
| 251 | return &req->req; |
| 252 | } |
| 253 | |
| 254 | void udc_request_free(struct udc_request *req) |
| 255 | { |
| 256 | free(req); |
| 257 | } |
| 258 | |
| 259 | int udc_request_queue(struct udc_endpoint *ept, struct udc_request *_req) |
| 260 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 261 | struct usb_request *req = (struct usb_request *)_req; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 262 | struct ept_queue_item *item = req->item; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 263 | unsigned phys = (unsigned)req->req.buf; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 264 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 265 | item->next = TERMINATE; |
| 266 | item->info = INFO_BYTES(req->req.length) | INFO_IOC | INFO_ACTIVE; |
| 267 | item->page0 = phys; |
| 268 | item->page1 = (phys & 0xfffff000) + 0x1000; |
Shashank Mittal | 1cc65b0 | 2011-12-20 15:30:17 -0800 | [diff] [blame] | 269 | item->page2 = (phys & 0xfffff000) + 0x2000; |
| 270 | item->page3 = (phys & 0xfffff000) + 0x3000; |
| 271 | item->page4 = (phys & 0xfffff000) + 0x4000; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 272 | |
| 273 | enter_critical_section(); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 274 | ept->head->next = (unsigned)item; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 275 | ept->head->info = 0; |
| 276 | ept->req = req; |
| 277 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 278 | arch_clean_invalidate_cache_range((addr_t) ept, |
| 279 | sizeof(struct udc_endpoint)); |
| 280 | arch_clean_invalidate_cache_range((addr_t) ept->head, |
| 281 | sizeof(struct ept_queue_head)); |
| 282 | arch_clean_invalidate_cache_range((addr_t) ept->req, |
| 283 | sizeof(struct usb_request)); |
| 284 | arch_clean_invalidate_cache_range((addr_t) req->req.buf, |
| 285 | req->req.length); |
| 286 | arch_clean_invalidate_cache_range((addr_t) ept->req->item, |
| 287 | sizeof(struct ept_queue_item)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 288 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 289 | DBG("ept%d %s queue req=%p\n", ept->num, ept->in ? "in" : "out", req); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 290 | |
| 291 | writel(ept->bit, USB_ENDPTPRIME); |
| 292 | exit_critical_section(); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static void handle_ept_complete(struct udc_endpoint *ept) |
| 297 | { |
| 298 | struct ept_queue_item *item; |
| 299 | unsigned actual; |
| 300 | int status; |
| 301 | struct usb_request *req; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 302 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 303 | DBG("ept%d %s complete req=%p\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 304 | ept->num, ept->in ? "in" : "out", ept->req); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 305 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 306 | arch_clean_invalidate_cache_range((addr_t) ept, |
| 307 | sizeof(struct udc_endpoint)); |
| 308 | arch_clean_invalidate_cache_range((addr_t) ept->req, |
| 309 | sizeof(struct usb_request)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 310 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 311 | req = ept->req; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 312 | if (req) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 313 | ept->req = 0; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 314 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 315 | item = req->item; |
| 316 | |
| 317 | /* For some reason we are getting the notification for |
| 318 | * transfer completion before the active bit has cleared. |
| 319 | * HACK: wait for the ACTIVE bit to clear: |
| 320 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 321 | do { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 322 | /* Must clean/invalidate cached item data before checking |
| 323 | * the status every time. |
| 324 | */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 325 | arch_clean_invalidate_cache_range((addr_t) item, |
| 326 | sizeof(struct |
| 327 | ept_queue_item)); |
| 328 | } |
| 329 | while (readl(&(item->info)) & INFO_ACTIVE); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 330 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 331 | arch_clean_invalidate_cache_range((addr_t) req->req.buf, |
| 332 | req->req.length); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 333 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 334 | if (item->info & 0xff) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 335 | actual = 0; |
| 336 | status = -1; |
| 337 | dprintf(INFO, "EP%d/%s FAIL nfo=%x pg0=%x\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 338 | ept->num, ept->in ? "in" : "out", item->info, |
| 339 | item->page0); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 340 | } else { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 341 | actual = |
| 342 | req->req.length - ((item->info >> 16) & 0x7fff); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 343 | status = 0; |
| 344 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 345 | if (req->req.complete) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 346 | req->req.complete(&req->req, actual, status); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static const char *reqname(unsigned r) |
| 351 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 352 | switch (r) { |
| 353 | case GET_STATUS: |
| 354 | return "GET_STATUS"; |
| 355 | case CLEAR_FEATURE: |
| 356 | return "CLEAR_FEATURE"; |
| 357 | case SET_FEATURE: |
| 358 | return "SET_FEATURE"; |
| 359 | case SET_ADDRESS: |
| 360 | return "SET_ADDRESS"; |
| 361 | case GET_DESCRIPTOR: |
| 362 | return "GET_DESCRIPTOR"; |
| 363 | case SET_DESCRIPTOR: |
| 364 | return "SET_DESCRIPTOR"; |
| 365 | case GET_CONFIGURATION: |
| 366 | return "GET_CONFIGURATION"; |
| 367 | case SET_CONFIGURATION: |
| 368 | return "SET_CONFIGURATION"; |
| 369 | case GET_INTERFACE: |
| 370 | return "GET_INTERFACE"; |
| 371 | case SET_INTERFACE: |
| 372 | return "SET_INTERFACE"; |
| 373 | default: |
| 374 | return "*UNKNOWN*"; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | static struct udc_endpoint *ep0in, *ep0out; |
| 379 | static struct udc_request *ep0req; |
| 380 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 381 | static void |
| 382 | ep0_setup_ack_complete(struct udc_endpoint *ep, struct usb_request *req) |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 383 | { |
| 384 | uint32_t mode; |
| 385 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 386 | if (!test_mode) |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 387 | return; |
| 388 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 389 | switch (test_mode) { |
| 390 | case TEST_PACKET: |
| 391 | dprintf(INFO, "Entering test mode for TST_PKT\n"); |
| 392 | mode = readl(USB_PORTSC) & (~PORTSC_PTC); |
| 393 | writel(mode | PORTSC_PTC_TST_PKT, USB_PORTSC); |
| 394 | break; |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 395 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 396 | case TEST_SE0_NAK: |
| 397 | dprintf(INFO, "Entering test mode for SE0-NAK\n"); |
| 398 | mode = readl(USB_PORTSC) & (~PORTSC_PTC); |
| 399 | writel(mode | PORTSC_PTC_SE0_NAK, USB_PORTSC); |
| 400 | break; |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | } |
| 404 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 405 | static void setup_ack(void) |
| 406 | { |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 407 | ep0req->complete = ep0_setup_ack_complete; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 408 | ep0req->length = 0; |
| 409 | udc_request_queue(ep0in, ep0req); |
| 410 | } |
| 411 | |
| 412 | static void ep0in_complete(struct udc_request *req, unsigned actual, int status) |
| 413 | { |
| 414 | DBG("ep0in_complete %p %d %d\n", req, actual, status); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 415 | if (status == 0) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 416 | req->length = 0; |
| 417 | req->complete = 0; |
| 418 | udc_request_queue(ep0out, req); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | static void setup_tx(void *buf, unsigned len) |
| 423 | { |
| 424 | DBG("setup_tx %p %d\n", buf, len); |
| 425 | memcpy(ep0req->buf, buf, len); |
| 426 | ep0req->complete = ep0in_complete; |
| 427 | ep0req->length = len; |
| 428 | udc_request_queue(ep0in, ep0req); |
| 429 | } |
| 430 | |
| 431 | static unsigned char usb_config_value = 0; |
| 432 | |
| 433 | #define SETUP(type,request) (((type) << 8) | (request)) |
| 434 | |
| 435 | static void handle_setup(struct udc_endpoint *ept) |
| 436 | { |
| 437 | struct setup_packet s; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 438 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 439 | arch_clean_invalidate_cache_range((addr_t) ept->head->setup_data, |
| 440 | sizeof(struct ept_queue_head)); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 441 | memcpy(&s, ept->head->setup_data, sizeof(s)); |
| 442 | writel(ept->bit, USB_ENDPTSETUPSTAT); |
| 443 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 444 | DBG("handle_setup type=0x%02x req=0x%02x val=%d idx=%d len=%d (%s)\n", |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 445 | s.type, s.request, s.value, s.index, s.length, reqname(s.request)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 446 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 447 | switch (SETUP(s.type, s.request)) { |
| 448 | case SETUP(DEVICE_READ, GET_STATUS): |
| 449 | { |
| 450 | unsigned zero = 0; |
| 451 | if (s.length == 2) { |
| 452 | setup_tx(&zero, 2); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 453 | return; |
| 454 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 455 | break; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 456 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 457 | case SETUP(DEVICE_READ, GET_DESCRIPTOR): |
| 458 | { |
| 459 | struct udc_descriptor *desc; |
| 460 | /* usb_highspeed? */ |
| 461 | for (desc = desc_list; desc; desc = desc->next) { |
| 462 | if (desc->tag == s.value) { |
| 463 | unsigned len = desc->len; |
| 464 | if (len > s.length) |
| 465 | len = s.length; |
| 466 | setup_tx(desc->data, len); |
| 467 | return; |
| 468 | } |
| 469 | } |
| 470 | break; |
| 471 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 472 | case SETUP(DEVICE_READ, GET_CONFIGURATION): |
| 473 | /* disabling this causes data transaction failures on OSX. Why? */ |
| 474 | if ((s.value == 0) && (s.index == 0) && (s.length == 1)) { |
| 475 | setup_tx(&usb_config_value, 1); |
| 476 | return; |
| 477 | } |
| 478 | break; |
| 479 | case SETUP(DEVICE_WRITE, SET_CONFIGURATION): |
| 480 | if (s.value == 1) { |
| 481 | struct udc_endpoint *ept; |
| 482 | /* enable endpoints */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 483 | for (ept = ept_list; ept; ept = ept->next) { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 484 | if (ept->num == 0) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 485 | continue; |
| 486 | endpoint_enable(ept, s.value); |
| 487 | } |
| 488 | usb_config_value = 1; |
| 489 | the_gadget->notify(the_gadget, UDC_EVENT_ONLINE); |
| 490 | } else { |
| 491 | writel(0, USB_ENDPTCTRL(1)); |
| 492 | usb_config_value = 0; |
| 493 | the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE); |
| 494 | } |
| 495 | setup_ack(); |
| 496 | usb_online = s.value ? 1 : 0; |
| 497 | usb_status(s.value ? 1 : 0, usb_highspeed); |
| 498 | return; |
| 499 | case SETUP(DEVICE_WRITE, SET_ADDRESS): |
| 500 | /* write address delayed (will take effect |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 501 | ** after the next IN txn) |
| 502 | */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 503 | writel((s.value << 25) | (1 << 24), USB_DEVICEADDR); |
| 504 | setup_ack(); |
| 505 | return; |
| 506 | case SETUP(INTERFACE_WRITE, SET_INTERFACE): |
| 507 | /* if we ack this everything hangs */ |
| 508 | /* per spec, STALL is valid if there is not alt func */ |
| 509 | goto stall; |
Subbaraman Narayanamurthy | d8b7afc | 2011-06-30 15:42:41 -0700 | [diff] [blame] | 510 | case SETUP(DEVICE_WRITE, SET_FEATURE): |
Shashank Mittal | 2523e0b | 2011-10-14 17:32:46 -0700 | [diff] [blame] | 511 | test_mode = s.index; |
Subbaraman Narayanamurthy | d8b7afc | 2011-06-30 15:42:41 -0700 | [diff] [blame] | 512 | setup_ack(); |
Subbaraman Narayanamurthy | d8b7afc | 2011-06-30 15:42:41 -0700 | [diff] [blame] | 513 | return; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 514 | case SETUP(ENDPOINT_WRITE, CLEAR_FEATURE): |
| 515 | { |
| 516 | struct udc_endpoint *ept; |
| 517 | unsigned num = s.index & 15; |
| 518 | unsigned in = !!(s.index & 0x80); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 519 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 520 | if ((s.value == 0) && (s.length == 0)) { |
| 521 | DBG("clr feat %d %d\n", num, in); |
| 522 | for (ept = ept_list; ept; ept = ept->next) { |
| 523 | if ((ept->num == num) |
| 524 | && (ept->in == in)) { |
| 525 | endpoint_enable(ept, 1); |
| 526 | setup_ack(); |
| 527 | return; |
| 528 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 531 | break; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 532 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | dprintf(INFO, "STALL %s %d %d %d %d %d\n", |
| 536 | reqname(s.request), |
| 537 | s.type, s.request, s.value, s.index, s.length); |
| 538 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 539 | stall: |
| 540 | writel((1 << 16) | (1 << 0), USB_ENDPTCTRL(ept->num)); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | unsigned ulpi_read(unsigned reg) |
| 544 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 545 | /* initiate read operation */ |
| 546 | writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg), USB_ULPI_VIEWPORT); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 547 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 548 | /* wait for completion */ |
| 549 | while (readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 550 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 551 | return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT)); |
| 552 | } |
| 553 | |
| 554 | void ulpi_write(unsigned val, unsigned reg) |
| 555 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 556 | /* initiate write operation */ |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 557 | writel(ULPI_RUN | ULPI_WRITE | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 558 | ULPI_ADDR(reg) | ULPI_DATA(val), USB_ULPI_VIEWPORT); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 559 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 560 | /* wait for completion */ |
| 561 | while (readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ; |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 562 | } |
Ajay Dudani | 5a1e330 | 2009-12-05 13:19:17 -0800 | [diff] [blame] | 563 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 564 | |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 565 | int udc_init(struct udc_device *dev) |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 566 | { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 567 | DBG("udc_init():\n"); |
| 568 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 569 | hsusb_clock_init(); |
| 570 | |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame^] | 571 | /* Do any target specific intialization like GPIO settings, |
| 572 | * LDO, PHY configuration etc. needed before USB port can be used. |
| 573 | */ |
| 574 | target_usb_init(); |
| 575 | |
| 576 | /* select ULPI phy */ |
| 577 | writel(0x81000000, USB_PORTSC); |
| 578 | |
| 579 | /* RESET */ |
| 580 | writel(0x00080002, USB_USBCMD); |
| 581 | |
| 582 | thread_sleep(20); |
Subbaraman Narayanamurthy | b0111a1 | 2011-02-03 14:24:16 -0800 | [diff] [blame] | 583 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 584 | epts = memalign(4096, 4096); |
| 585 | |
| 586 | dprintf(INFO, "USB init ept @ %p\n", epts); |
| 587 | memset(epts, 0, 32 * sizeof(struct ept_queue_head)); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 588 | arch_clean_invalidate_cache_range((addr_t) epts, |
| 589 | 32 * sizeof(struct ept_queue_head)); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 590 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 591 | writel((unsigned)epts, USB_ENDPOINTLISTADDR); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 592 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 593 | /* select DEVICE mode */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 594 | writel(0x02, USB_USBMODE); |
| 595 | |
| 596 | writel(0xffffffff, USB_ENDPTFLUSH); |
| 597 | thread_sleep(20); |
| 598 | |
| 599 | ep0out = _udc_endpoint_alloc(0, 0, 64); |
| 600 | ep0in = _udc_endpoint_alloc(0, 1, 64); |
| 601 | ep0req = udc_request_alloc(); |
| 602 | ep0req->buf = malloc(4096); |
| 603 | |
| 604 | { |
| 605 | /* create and register a language table descriptor */ |
| 606 | /* language 0x0409 is US English */ |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 607 | struct udc_descriptor *desc = |
| 608 | udc_descriptor_alloc(TYPE_STRING, 0, 4); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 609 | desc->data[2] = 0x09; |
| 610 | desc->data[3] = 0x04; |
| 611 | udc_descriptor_register(desc); |
| 612 | } |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 613 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 614 | the_device = dev; |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | enum handler_return udc_interrupt(void *arg) |
| 619 | { |
| 620 | struct udc_endpoint *ept; |
| 621 | unsigned ret = INT_NO_RESCHEDULE; |
| 622 | unsigned n = readl(USB_USBSTS); |
| 623 | writel(n, USB_USBSTS); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 624 | |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame^] | 625 | DBG("\nudc_interrupt(): status = 0x%x\n", n); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 626 | |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame^] | 627 | n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 628 | |
| 629 | if (n == 0) { |
| 630 | DBG("n = 0\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 631 | return ret; |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 632 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 633 | |
| 634 | if (n & STS_URI) { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 635 | DBG("STS_URI\n"); |
| 636 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 637 | writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE); |
| 638 | writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT); |
| 639 | writel(0xffffffff, USB_ENDPTFLUSH); |
| 640 | writel(0, USB_ENDPTCTRL(1)); |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 641 | dprintf(INFO, "-- reset --\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 642 | usb_online = 0; |
| 643 | usb_config_value = 0; |
| 644 | the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE); |
| 645 | |
| 646 | /* error out any pending reqs */ |
| 647 | for (ept = ept_list; ept; ept = ept->next) { |
| 648 | /* ensure that ept_complete considers |
| 649 | * this to be an error state |
| 650 | */ |
| 651 | if (ept->req) { |
| 652 | ept->req->item->info = INFO_HALTED; |
| 653 | handle_ept_complete(ept); |
| 654 | } |
| 655 | } |
| 656 | usb_status(0, usb_highspeed); |
| 657 | } |
| 658 | if (n & STS_SLI) { |
Ajay Dudani | 35f686f | 2011-07-22 13:12:09 -0700 | [diff] [blame] | 659 | DBG("-- suspend --\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 660 | } |
| 661 | if (n & STS_PCI) { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 662 | dprintf(INFO, "-- portchange --\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 663 | unsigned spd = (readl(USB_PORTSC) >> 26) & 3; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 664 | if (spd == 2) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 665 | usb_highspeed = 1; |
| 666 | } else { |
| 667 | usb_highspeed = 0; |
| 668 | } |
| 669 | } |
| 670 | if (n & STS_UEI) { |
Amol Jadi | 4421e65 | 2011-06-16 15:00:48 -0700 | [diff] [blame] | 671 | DBG("STS_UEI\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 672 | dprintf(INFO, "<UEI %x>\n", readl(USB_ENDPTCOMPLETE)); |
| 673 | } |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 674 | if ((n & STS_UI) || (n & STS_UEI)) { |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame^] | 675 | |
| 676 | if (n & STS_UEI) |
| 677 | DBG("ERROR "); |
| 678 | if (n & STS_UI) |
| 679 | DBG("USB "); |
| 680 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 681 | n = readl(USB_ENDPTSETUPSTAT); |
| 682 | if (n & EPT_RX(0)) { |
| 683 | handle_setup(ep0out); |
| 684 | ret = INT_RESCHEDULE; |
| 685 | } |
| 686 | |
| 687 | n = readl(USB_ENDPTCOMPLETE); |
| 688 | if (n != 0) { |
| 689 | writel(n, USB_ENDPTCOMPLETE); |
| 690 | } |
| 691 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 692 | for (ept = ept_list; ept; ept = ept->next) { |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 693 | if (n & ept->bit) { |
| 694 | handle_ept_complete(ept); |
| 695 | ret = INT_RESCHEDULE; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | int udc_register_gadget(struct udc_gadget *gadget) |
| 703 | { |
| 704 | if (the_gadget) { |
| 705 | dprintf(CRITICAL, "only one gadget supported\n"); |
| 706 | return -1; |
| 707 | } |
| 708 | the_gadget = gadget; |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static void udc_ept_desc_fill(struct udc_endpoint *ept, unsigned char *data) |
| 713 | { |
| 714 | data[0] = 7; |
| 715 | data[1] = TYPE_ENDPOINT; |
| 716 | data[2] = ept->num | (ept->in ? 0x80 : 0x00); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 717 | data[3] = 0x02; /* bulk -- the only kind we support */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 718 | data[4] = ept->maxpkt; |
| 719 | data[5] = ept->maxpkt >> 8; |
| 720 | data[6] = ept->in ? 0x00 : 0x01; |
| 721 | } |
| 722 | |
| 723 | static unsigned udc_ifc_desc_size(struct udc_gadget *g) |
| 724 | { |
| 725 | return 9 + g->ifc_endpoints * 7; |
| 726 | } |
| 727 | |
| 728 | static void udc_ifc_desc_fill(struct udc_gadget *g, unsigned char *data) |
| 729 | { |
| 730 | unsigned n; |
| 731 | |
| 732 | data[0] = 0x09; |
| 733 | data[1] = TYPE_INTERFACE; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 734 | data[2] = 0x00; /* ifc number */ |
| 735 | data[3] = 0x00; /* alt number */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 736 | data[4] = g->ifc_endpoints; |
| 737 | data[5] = g->ifc_class; |
| 738 | data[6] = g->ifc_subclass; |
| 739 | data[7] = g->ifc_protocol; |
| 740 | data[8] = udc_string_desc_alloc(g->ifc_string); |
| 741 | |
| 742 | data += 9; |
| 743 | for (n = 0; n < g->ifc_endpoints; n++) { |
| 744 | udc_ept_desc_fill(g->ept[n], data); |
| 745 | data += 7; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | int udc_start(void) |
| 750 | { |
| 751 | struct udc_descriptor *desc; |
| 752 | unsigned char *data; |
| 753 | unsigned size; |
| 754 | |
Chandan Uddaraju | 40b227d | 2010-08-03 19:25:41 -0700 | [diff] [blame] | 755 | dprintf(ALWAYS, "udc_start()\n"); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 756 | |
| 757 | if (!the_device) { |
| 758 | dprintf(CRITICAL, "udc cannot start before init\n"); |
| 759 | return -1; |
| 760 | } |
| 761 | if (!the_gadget) { |
| 762 | dprintf(CRITICAL, "udc has no gadget registered\n"); |
| 763 | return -1; |
| 764 | } |
| 765 | |
| 766 | /* create our device descriptor */ |
| 767 | desc = udc_descriptor_alloc(TYPE_DEVICE, 0, 18); |
| 768 | data = desc->data; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 769 | data[2] = 0x00; /* usb spec minor rev */ |
| 770 | data[3] = 0x02; /* usb spec major rev */ |
| 771 | data[4] = 0x00; /* class */ |
| 772 | data[5] = 0x00; /* subclass */ |
| 773 | data[6] = 0x00; /* protocol */ |
| 774 | data[7] = 0x40; /* max packet size on ept 0 */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 775 | memcpy(data + 8, &the_device->vendor_id, sizeof(short)); |
| 776 | memcpy(data + 10, &the_device->product_id, sizeof(short)); |
| 777 | memcpy(data + 12, &the_device->version_id, sizeof(short)); |
| 778 | data[14] = udc_string_desc_alloc(the_device->manufacturer); |
| 779 | data[15] = udc_string_desc_alloc(the_device->product); |
| 780 | data[16] = udc_string_desc_alloc(the_device->serialno); |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 781 | data[17] = 1; /* number of configurations */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 782 | udc_descriptor_register(desc); |
| 783 | |
| 784 | /* create our configuration descriptor */ |
| 785 | size = 9 + udc_ifc_desc_size(the_gadget); |
| 786 | desc = udc_descriptor_alloc(TYPE_CONFIGURATION, 0, size); |
| 787 | data = desc->data; |
| 788 | data[0] = 0x09; |
| 789 | data[2] = size; |
| 790 | data[3] = size >> 8; |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 791 | data[4] = 0x01; /* number of interfaces */ |
| 792 | data[5] = 0x01; /* configuration value */ |
| 793 | data[6] = 0x00; /* configuration string */ |
| 794 | data[7] = 0x80; /* attributes */ |
| 795 | data[8] = 0x80; /* max power (250ma) -- todo fix this */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 796 | udc_ifc_desc_fill(the_gadget, data + 9); |
| 797 | udc_descriptor_register(desc); |
| 798 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 799 | register_int_handler(INT_USB_HS, udc_interrupt, (void *)0); |
Amol Jadi | ca4f4c9 | 2011-01-13 20:19:34 -0800 | [diff] [blame] | 800 | writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR); |
| 801 | unmask_interrupt(INT_USB_HS); |
| 802 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 803 | /* go to RUN mode (D+ pullup enable) */ |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 804 | writel(0x00080001, USB_USBCMD); |
Amol Jadi | ca4f4c9 | 2011-01-13 20:19:34 -0800 | [diff] [blame] | 805 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | int udc_stop(void) |
| 810 | { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 811 | writel(0, USB_USBINTR); |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 812 | mask_interrupt(INT_USB_HS); |
| 813 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame] | 814 | /* disable pullup */ |
Shashank Mittal | d8c42bf | 2010-06-09 15:44:28 -0700 | [diff] [blame] | 815 | writel(0x00080000, USB_USBCMD); |
Amol Jadi | da118b9 | 2012-07-06 19:53:18 -0700 | [diff] [blame^] | 816 | |
| 817 | target_usb_stop(); |
| 818 | |
Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame] | 819 | thread_sleep(10); |
| 820 | |
| 821 | return 0; |
| 822 | } |