1 /* 2 * USB Peripheral Controller driver for Aeroflex Gaisler GRUSBDC. 3 * 4 * 2013 (c) Aeroflex Gaisler AB 5 * 6 * This driver supports GRUSBDC USB Device Controller cores available in the 7 * GRLIB VHDL IP core library. 8 * 9 * Full documentation of the GRUSBDC core can be found here: 10 * http://www.gaisler.com/products/grlib/grip.pdf 11 * 12 * This program is free software; you can redistribute it and/or modify it 13 * under the terms of the GNU General Public License as published by the 14 * Free Software Foundation; either version 2 of the License, or (at your 15 * option) any later version. 16 * 17 * Contributors: 18 * - Andreas Larsson <andreas@gaisler.com> 19 * - Marko Isomaki 20 */ 21 22 /* 23 * A GRUSBDC core can have up to 16 IN endpoints and 16 OUT endpoints each 24 * individually configurable to any of the four USB transfer types. This driver 25 * only supports cores in DMA mode. 26 */ 27 28 #include <linux/kernel.h> 29 #include <linux/module.h> 30 #include <linux/slab.h> 31 #include <linux/spinlock.h> 32 #include <linux/errno.h> 33 #include <linux/list.h> 34 #include <linux/interrupt.h> 35 #include <linux/device.h> 36 #include <linux/usb/ch9.h> 37 #include <linux/usb/gadget.h> 38 #include <linux/dma-mapping.h> 39 #include <linux/dmapool.h> 40 #include <linux/debugfs.h> 41 #include <linux/seq_file.h> 42 #include <linux/of_platform.h> 43 #include <linux/of_irq.h> 44 #include <linux/of_address.h> 45 46 #include <asm/byteorder.h> 47 48 #include "gr_udc.h" 49 50 #define DRIVER_NAME "gr_udc" 51 #define DRIVER_DESC "Aeroflex Gaisler GRUSBDC USB Peripheral Controller" 52 53 static const char driver_name[] = DRIVER_NAME; 54 static const char driver_desc[] = DRIVER_DESC; 55 56 #define gr_read32(x) (ioread32be((x))) 57 #define gr_write32(x, v) (iowrite32be((v), (x))) 58 59 /* USB speed and corresponding string calculated from status register value */ 60 #define GR_SPEED(status) \ 61 ((status & GR_STATUS_SP) ? USB_SPEED_FULL : USB_SPEED_HIGH) 62 #define GR_SPEED_STR(status) usb_speed_string(GR_SPEED(status)) 63 64 /* Size of hardware buffer calculated from epctrl register value */ 65 #define GR_BUFFER_SIZE(epctrl) \ 66 ((((epctrl) & GR_EPCTRL_BUFSZ_MASK) >> GR_EPCTRL_BUFSZ_POS) * \ 67 GR_EPCTRL_BUFSZ_SCALER) 68 69 /* ---------------------------------------------------------------------- */ 70 /* Debug printout functionality */ 71 72 static const char * const gr_modestring[] = {"control", "iso", "bulk", "int"}; 73 74 static const char *gr_ep0state_string(enum gr_ep0state state) 75 { 76 static const char *const names[] = { 77 [GR_EP0_DISCONNECT] = "disconnect", 78 [GR_EP0_SETUP] = "setup", 79 [GR_EP0_IDATA] = "idata", 80 [GR_EP0_ODATA] = "odata", 81 [GR_EP0_ISTATUS] = "istatus", 82 [GR_EP0_OSTATUS] = "ostatus", 83 [GR_EP0_STALL] = "stall", 84 [GR_EP0_SUSPEND] = "suspend", 85 }; 86 87 if (state < 0 || state >= ARRAY_SIZE(names)) 88 return "UNKNOWN"; 89 90 return names[state]; 91 } 92 93 #ifdef VERBOSE_DEBUG 94 95 static void gr_dbgprint_request(const char *str, struct gr_ep *ep, 96 struct gr_request *req) 97 { 98 int buflen = ep->is_in ? req->req.length : req->req.actual; 99 int rowlen = 32; 100 int plen = min(rowlen, buflen); 101 102 dev_dbg(ep->dev->dev, "%s: 0x%p, %d bytes data%s:\n", str, req, buflen, 103 (buflen > plen ? " (truncated)" : "")); 104 print_hex_dump_debug(" ", DUMP_PREFIX_NONE, 105 rowlen, 4, req->req.buf, plen, false); 106 } 107 108 static void gr_dbgprint_devreq(struct gr_udc *dev, u8 type, u8 request, 109 u16 value, u16 index, u16 length) 110 { 111 dev_vdbg(dev->dev, "REQ: %02x.%02x v%04x i%04x l%04x\n", 112 type, request, value, index, length); 113 } 114 #else /* !VERBOSE_DEBUG */ 115 116 static void gr_dbgprint_request(const char *str, struct gr_ep *ep, 117 struct gr_request *req) {} 118 119 static void gr_dbgprint_devreq(struct gr_udc *dev, u8 type, u8 request, 120 u16 value, u16 index, u16 length) {} 121 122 #endif /* VERBOSE_DEBUG */ 123 124 /* ---------------------------------------------------------------------- */ 125 /* Debugfs functionality */ 126 127 #ifdef CONFIG_USB_GADGET_DEBUG_FS 128 129 static void gr_seq_ep_show(struct seq_file *seq, struct gr_ep *ep) 130 { 131 u32 epctrl = gr_read32(&ep->regs->epctrl); 132 u32 epstat = gr_read32(&ep->regs->epstat); 133 int mode = (epctrl & GR_EPCTRL_TT_MASK) >> GR_EPCTRL_TT_POS; 134 struct gr_request *req; 135 136 seq_printf(seq, "%s:\n", ep->ep.name); 137 seq_printf(seq, " mode = %s\n", gr_modestring[mode]); 138 seq_printf(seq, " halted: %d\n", !!(epctrl & GR_EPCTRL_EH)); 139 seq_printf(seq, " disabled: %d\n", !!(epctrl & GR_EPCTRL_ED)); 140 seq_printf(seq, " valid: %d\n", !!(epctrl & GR_EPCTRL_EV)); 141 seq_printf(seq, " dma_start = %d\n", ep->dma_start); 142 seq_printf(seq, " stopped = %d\n", ep->stopped); 143 seq_printf(seq, " wedged = %d\n", ep->wedged); 144 seq_printf(seq, " callback = %d\n", ep->callback); 145 seq_printf(seq, " maxpacket = %d\n", ep->ep.maxpacket); 146 seq_printf(seq, " maxpacket_limit = %d\n", ep->ep.maxpacket_limit); 147 seq_printf(seq, " bytes_per_buffer = %d\n", ep->bytes_per_buffer); 148 if (mode == 1 || mode == 3) 149 seq_printf(seq, " nt = %d\n", 150 (epctrl & GR_EPCTRL_NT_MASK) >> GR_EPCTRL_NT_POS); 151 152 seq_printf(seq, " Buffer 0: %s %s%d\n", 153 epstat & GR_EPSTAT_B0 ? "valid" : "invalid", 154 epstat & GR_EPSTAT_BS ? " " : "selected ", 155 (epstat & GR_EPSTAT_B0CNT_MASK) >> GR_EPSTAT_B0CNT_POS); 156 seq_printf(seq, " Buffer 1: %s %s%d\n", 157 epstat & GR_EPSTAT_B1 ? "valid" : "invalid", 158 epstat & GR_EPSTAT_BS ? "selected " : " ", 159 (epstat & GR_EPSTAT_B1CNT_MASK) >> GR_EPSTAT_B1CNT_POS); 160 161 if (list_empty(&ep->queue)) { 162 seq_puts(seq, " Queue: empty\n\n"); 163 return; 164 } 165 166 seq_puts(seq, " Queue:\n"); 167 list_for_each_entry(req, &ep->queue, queue) { 168 struct gr_dma_desc *desc; 169 struct gr_dma_desc *next; 170 171 seq_printf(seq, " 0x%p: 0x%p %d %d\n", req, 172 &req->req.buf, req->req.actual, req->req.length); 173 174 next = req->first_desc; 175 do { 176 desc = next; 177 next = desc->next_desc; 178 seq_printf(seq, " %c 0x%p (0x%08x): 0x%05x 0x%08x\n", 179 desc == req->curr_desc ? 'c' : ' ', 180 desc, desc->paddr, desc->ctrl, desc->data); 181 } while (desc != req->last_desc); 182 } 183 seq_puts(seq, "\n"); 184 } 185 186 187 static int gr_seq_show(struct seq_file *seq, void *v) 188 { 189 struct gr_udc *dev = seq->private; 190 u32 control = gr_read32(&dev->regs->control); 191 u32 status = gr_read32(&dev->regs->status); 192 struct gr_ep *ep; 193 194 seq_printf(seq, "usb state = %s\n", 195 usb_state_string(dev->gadget.state)); 196 seq_printf(seq, "address = %d\n", 197 (control & GR_CONTROL_UA_MASK) >> GR_CONTROL_UA_POS); 198 seq_printf(seq, "speed = %s\n", GR_SPEED_STR(status)); 199 seq_printf(seq, "ep0state = %s\n", gr_ep0state_string(dev->ep0state)); 200 seq_printf(seq, "irq_enabled = %d\n", dev->irq_enabled); 201 seq_printf(seq, "remote_wakeup = %d\n", dev->remote_wakeup); 202 seq_printf(seq, "test_mode = %d\n", dev->test_mode); 203 seq_puts(seq, "\n"); 204 205 list_for_each_entry(ep, &dev->ep_list, ep_list) 206 gr_seq_ep_show(seq, ep); 207 208 return 0; 209 } 210 211 static int gr_dfs_open(struct inode *inode, struct file *file) 212 { 213 return single_open(file, gr_seq_show, inode->i_private); 214 } 215 216 static const struct file_operations gr_dfs_fops = { 217 .owner = THIS_MODULE, 218 .open = gr_dfs_open, 219 .read = seq_read, 220 .llseek = seq_lseek, 221 .release = single_release, 222 }; 223 224 static void gr_dfs_create(struct gr_udc *dev) 225 { 226 const char *name = "gr_udc_state"; 227 228 dev->dfs_root = debugfs_create_dir(dev_name(dev->dev), NULL); 229 dev->dfs_state = debugfs_create_file(name, 0444, dev->dfs_root, dev, 230 &gr_dfs_fops); 231 } 232 233 static void gr_dfs_delete(struct gr_udc *dev) 234 { 235 /* Handles NULL and ERR pointers internally */ 236 debugfs_remove(dev->dfs_state); 237 debugfs_remove(dev->dfs_root); 238 } 239 240 #else /* !CONFIG_USB_GADGET_DEBUG_FS */ 241 242 static void gr_dfs_create(struct gr_udc *dev) {} 243 static void gr_dfs_delete(struct gr_udc *dev) {} 244 245 #endif /* CONFIG_USB_GADGET_DEBUG_FS */ 246 247 /* ---------------------------------------------------------------------- */ 248 /* DMA and request handling */ 249 250 /* Allocates a new struct gr_dma_desc, sets paddr and zeroes the rest */ 251 static struct gr_dma_desc *gr_alloc_dma_desc(struct gr_ep *ep, gfp_t gfp_flags) 252 { 253 dma_addr_t paddr; 254 struct gr_dma_desc *dma_desc; 255 256 dma_desc = dma_pool_alloc(ep->dev->desc_pool, gfp_flags, &paddr); 257 if (!dma_desc) { 258 dev_err(ep->dev->dev, "Could not allocate from DMA pool\n"); 259 return NULL; 260 } 261 262 memset(dma_desc, 0, sizeof(*dma_desc)); 263 dma_desc->paddr = paddr; 264 265 return dma_desc; 266 } 267 268 static inline void gr_free_dma_desc(struct gr_udc *dev, 269 struct gr_dma_desc *desc) 270 { 271 dma_pool_free(dev->desc_pool, desc, (dma_addr_t)desc->paddr); 272 } 273 274 /* Frees the chain of struct gr_dma_desc for the given request */ 275 static void gr_free_dma_desc_chain(struct gr_udc *dev, struct gr_request *req) 276 { 277 struct gr_dma_desc *desc; 278 struct gr_dma_desc *next; 279 280 next = req->first_desc; 281 if (!next) 282 return; 283 284 do { 285 desc = next; 286 next = desc->next_desc; 287 gr_free_dma_desc(dev, desc); 288 } while (desc != req->last_desc); 289 290 req->first_desc = NULL; 291 req->curr_desc = NULL; 292 req->last_desc = NULL; 293 } 294 295 static void gr_ep0_setup(struct gr_udc *dev, struct gr_request *req); 296 297 /* 298 * Frees allocated resources and calls the appropriate completion function/setup 299 * package handler for a finished request. 300 * 301 * Must be called with dev->lock held and irqs disabled. 302 */ 303 static void gr_finish_request(struct gr_ep *ep, struct gr_request *req, 304 int status) 305 __releases(&dev->lock) 306 __acquires(&dev->lock) 307 { 308 struct gr_udc *dev; 309 310 list_del_init(&req->queue); 311 312 if (likely(req->req.status == -EINPROGRESS)) 313 req->req.status = status; 314 else 315 status = req->req.status; 316 317 dev = ep->dev; 318 usb_gadget_unmap_request(&dev->gadget, &req->req, ep->is_in); 319 gr_free_dma_desc_chain(dev, req); 320 321 if (ep->is_in) /* For OUT, actual gets updated bit by bit */ 322 req->req.actual = req->req.length; 323 324 if (!status) { 325 if (ep->is_in) 326 gr_dbgprint_request("SENT", ep, req); 327 else 328 gr_dbgprint_request("RECV", ep, req); 329 } 330 331 /* Prevent changes to ep->queue during callback */ 332 ep->callback = 1; 333 if (req == dev->ep0reqo && !status) { 334 if (req->setup) 335 gr_ep0_setup(dev, req); 336 else 337 dev_err(dev->dev, 338 "Unexpected non setup packet on ep0in\n"); 339 } else if (req->req.complete) { 340 spin_unlock(&dev->lock); 341 342 req->req.complete(&ep->ep, &req->req); 343 344 spin_lock(&dev->lock); 345 } 346 ep->callback = 0; 347 } 348 349 static struct usb_request *gr_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags) 350 { 351 struct gr_request *req; 352 353 req = kzalloc(sizeof(*req), gfp_flags); 354 if (!req) 355 return NULL; 356 357 INIT_LIST_HEAD(&req->queue); 358 359 return &req->req; 360 } 361 362 /* 363 * Starts DMA for endpoint ep if there are requests in the queue. 364 * 365 * Must be called with dev->lock held and with !ep->stopped. 366 */ 367 static void gr_start_dma(struct gr_ep *ep) 368 { 369 struct gr_request *req; 370 u32 dmactrl; 371 372 if (list_empty(&ep->queue)) { 373 ep->dma_start = 0; 374 return; 375 } 376 377 req = list_first_entry(&ep->queue, struct gr_request, queue); 378 379 /* A descriptor should already have been allocated */ 380 BUG_ON(!req->curr_desc); 381 382 wmb(); /* Make sure all is settled before handing it over to DMA */ 383 384 /* Set the descriptor pointer in the hardware */ 385 gr_write32(&ep->regs->dmaaddr, req->curr_desc->paddr); 386 387 /* Announce available descriptors */ 388 dmactrl = gr_read32(&ep->regs->dmactrl); 389 gr_write32(&ep->regs->dmactrl, dmactrl | GR_DMACTRL_DA); 390 391 ep->dma_start = 1; 392 } 393 394 /* 395 * Finishes the first request in the ep's queue and, if available, starts the 396 * next request in queue. 397 * 398 * Must be called with dev->lock held, irqs disabled and with !ep->stopped. 399 */ 400 static void gr_dma_advance(struct gr_ep *ep, int status) 401 { 402 struct gr_request *req; 403 404 req = list_first_entry(&ep->queue, struct gr_request, queue); 405 gr_finish_request(ep, req, status); 406 gr_start_dma(ep); /* Regardless of ep->dma_start */ 407 } 408 409 /* 410 * Abort DMA for an endpoint. Sets the abort DMA bit which causes an ongoing DMA 411 * transfer to be canceled and clears GR_DMACTRL_DA. 412 * 413 * Must be called with dev->lock held. 414 */ 415 static void gr_abort_dma(struct gr_ep *ep) 416 { 417 u32 dmactrl; 418 419 dmactrl = gr_read32(&ep->regs->dmactrl); 420 gr_write32(&ep->regs->dmactrl, dmactrl | GR_DMACTRL_AD); 421 } 422 423 /* 424 * Allocates and sets up a struct gr_dma_desc and putting it on the descriptor 425 * chain. 426 * 427 * Size is not used for OUT endpoints. Hardware can not be instructed to handle 428 * smaller buffer than MAXPL in the OUT direction. 429 */ 430 static int gr_add_dma_desc(struct gr_ep *ep, struct gr_request *req, 431 dma_addr_t data, unsigned size, gfp_t gfp_flags) 432 { 433 struct gr_dma_desc *desc; 434 435 desc = gr_alloc_dma_desc(ep, gfp_flags); 436 if (!desc) 437 return -ENOMEM; 438 439 desc->data = data; 440 if (ep->is_in) 441 desc->ctrl = 442 (GR_DESC_IN_CTRL_LEN_MASK & size) | GR_DESC_IN_CTRL_EN; 443 else 444 desc->ctrl = GR_DESC_OUT_CTRL_IE; 445 446 if (!req->first_desc) { 447 req->first_desc = desc; 448 req->curr_desc = desc; 449 } else { 450 req->last_desc->next_desc = desc; 451 req->last_desc->next = desc->paddr; 452 req->last_desc->ctrl |= GR_DESC_OUT_CTRL_NX; 453 } 454 req->last_desc = desc; 455 456 return 0; 457 } 458 459 /* 460 * Sets up a chain of struct gr_dma_descriptors pointing to buffers that 461 * together covers req->req.length bytes of the buffer at DMA address 462 * req->req.dma for the OUT direction. 463 * 464 * The first descriptor in the chain is enabled, the rest disabled. The 465 * interrupt handler will later enable them one by one when needed so we can 466 * find out when the transfer is finished. For OUT endpoints, all descriptors 467 * therefore generate interrutps. 468 */ 469 static int gr_setup_out_desc_list(struct gr_ep *ep, struct gr_request *req, 470 gfp_t gfp_flags) 471 { 472 u16 bytes_left; /* Bytes left to provide descriptors for */ 473 u16 bytes_used; /* Bytes accommodated for */ 474 int ret = 0; 475 476 req->first_desc = NULL; /* Signals that no allocation is done yet */ 477 bytes_left = req->req.length; 478 bytes_used = 0; 479 while (bytes_left > 0) { 480 dma_addr_t start = req->req.dma + bytes_used; 481 u16 size = min(bytes_left, ep->bytes_per_buffer); 482 483 /* Should not happen however - gr_queue stops such lengths */ 484 if (size < ep->bytes_per_buffer) 485 dev_warn(ep->dev->dev, 486 "Buffer overrun risk: %u < %u bytes/buffer\n", 487 size, ep->bytes_per_buffer); 488 489 ret = gr_add_dma_desc(ep, req, start, size, gfp_flags); 490 if (ret) 491 goto alloc_err; 492 493 bytes_left -= size; 494 bytes_used += size; 495 } 496 497 req->first_desc->ctrl |= GR_DESC_OUT_CTRL_EN; 498 499 return 0; 500 501 alloc_err: 502 gr_free_dma_desc_chain(ep->dev, req); 503 504 return ret; 505 } 506 507 /* 508 * Sets up a chain of struct gr_dma_descriptors pointing to buffers that 509 * together covers req->req.length bytes of the buffer at DMA address 510 * req->req.dma for the IN direction. 511 * 512 * When more data is provided than the maximum payload size, the hardware splits 513 * this up into several payloads automatically. Moreover, ep->bytes_per_buffer 514 * is always set to a multiple of the maximum payload (restricted to the valid 515 * number of maximum payloads during high bandwidth isochronous or interrupt 516 * transfers) 517 * 518 * All descriptors are enabled from the beginning and we only generate an 519 * interrupt for the last one indicating that the entire request has been pushed 520 * to hardware. 521 */ 522 static int gr_setup_in_desc_list(struct gr_ep *ep, struct gr_request *req, 523 gfp_t gfp_flags) 524 { 525 u16 bytes_left; /* Bytes left in req to provide descriptors for */ 526 u16 bytes_used; /* Bytes in req accommodated for */ 527 int ret = 0; 528 529 req->first_desc = NULL; /* Signals that no allocation is done yet */ 530 bytes_left = req->req.length; 531 bytes_used = 0; 532 do { /* Allow for zero length packets */ 533 dma_addr_t start = req->req.dma + bytes_used; 534 u16 size = min(bytes_left, ep->bytes_per_buffer); 535 536 ret = gr_add_dma_desc(ep, req, start, size, gfp_flags); 537 if (ret) 538 goto alloc_err; 539 540 bytes_left -= size; 541 bytes_used += size; 542 } while (bytes_left > 0); 543 544 /* 545 * Send an extra zero length packet to indicate that no more data is 546 * available when req->req.zero is set and the data length is even 547 * multiples of ep->ep.maxpacket. 548 */ 549 if (req->req.zero && (req->req.length % ep->ep.maxpacket == 0)) { 550 ret = gr_add_dma_desc(ep, req, 0, 0, gfp_flags); 551 if (ret) 552 goto alloc_err; 553 } 554 555 /* 556 * For IN packets we only want to know when the last packet has been 557 * transmitted (not just put into internal buffers). 558 */ 559 req->last_desc->ctrl |= GR_DESC_IN_CTRL_PI; 560 561 return 0; 562 563 alloc_err: 564 gr_free_dma_desc_chain(ep->dev, req); 565 566 return ret; 567 } 568 569 /* Must be called with dev->lock held */ 570 static int gr_queue(struct gr_ep *ep, struct gr_request *req, gfp_t gfp_flags) 571 { 572 struct gr_udc *dev = ep->dev; 573 int ret; 574 575 if (unlikely(!ep->ep.desc && ep->num != 0)) { 576 dev_err(dev->dev, "No ep descriptor for %s\n", ep->ep.name); 577 return -EINVAL; 578 } 579 580 if (unlikely(!req->req.buf || !list_empty(&req->queue))) { 581 dev_err(dev->dev, 582 "Invalid request for %s: buf=%p list_empty=%d\n", 583 ep->ep.name, req->req.buf, list_empty(&req->queue)); 584 return -EINVAL; 585 } 586 587 /* 588 * The DMA controller can not handle smaller OUT buffers than 589 * maxpacket. It could lead to buffer overruns if unexpectedly long 590 * packet are received. 591 */ 592 if (!ep->is_in && (req->req.length % ep->ep.maxpacket) != 0) { 593 dev_err(dev->dev, 594 "OUT request length %d is not multiple of maxpacket\n", 595 req->req.length); 596 return -EMSGSIZE; 597 } 598 599 if (unlikely(!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN)) { 600 dev_err(dev->dev, "-ESHUTDOWN"); 601 return -ESHUTDOWN; 602 } 603 604 /* Can't touch registers when suspended */ 605 if (dev->ep0state == GR_EP0_SUSPEND) { 606 dev_err(dev->dev, "-EBUSY"); 607 return -EBUSY; 608 } 609 610 /* Set up DMA mapping in case the caller didn't */ 611 ret = usb_gadget_map_request(&dev->gadget, &req->req, ep->is_in); 612 if (ret) { 613 dev_err(dev->dev, "usb_gadget_map_request"); 614 return ret; 615 } 616 617 if (ep->is_in) 618 ret = gr_setup_in_desc_list(ep, req, gfp_flags); 619 else 620 ret = gr_setup_out_desc_list(ep, req, gfp_flags); 621 if (ret) 622 return ret; 623 624 req->req.status = -EINPROGRESS; 625 req->req.actual = 0; 626 list_add_tail(&req->queue, &ep->queue); 627 628 /* Start DMA if not started, otherwise interrupt handler handles it */ 629 if (!ep->dma_start && likely(!ep->stopped)) 630 gr_start_dma(ep); 631 632 return 0; 633 } 634 635 /* 636 * Queue a request from within the driver. 637 * 638 * Must be called with dev->lock held. 639 */ 640 static inline int gr_queue_int(struct gr_ep *ep, struct gr_request *req, 641 gfp_t gfp_flags) 642 { 643 if (ep->is_in) 644 gr_dbgprint_request("RESP", ep, req); 645 646 return gr_queue(ep, req, gfp_flags); 647 } 648 649 /* ---------------------------------------------------------------------- */ 650 /* General helper functions */ 651 652 /* 653 * Dequeue ALL requests. 654 * 655 * Must be called with dev->lock held and irqs disabled. 656 */ 657 static void gr_ep_nuke(struct gr_ep *ep) 658 { 659 struct gr_request *req; 660 661 ep->stopped = 1; 662 ep->dma_start = 0; 663 gr_abort_dma(ep); 664 665 while (!list_empty(&ep->queue)) { 666 req = list_first_entry(&ep->queue, struct gr_request, queue); 667 gr_finish_request(ep, req, -ESHUTDOWN); 668 } 669 } 670 671 /* 672 * Reset the hardware state of this endpoint. 673 * 674 * Must be called with dev->lock held. 675 */ 676 static void gr_ep_reset(struct gr_ep *ep) 677 { 678 gr_write32(&ep->regs->epctrl, 0); 679 gr_write32(&ep->regs->dmactrl, 0); 680 681 ep->ep.maxpacket = MAX_CTRL_PL_SIZE; 682 ep->ep.desc = NULL; 683 ep->stopped = 1; 684 ep->dma_start = 0; 685 } 686 687 /* 688 * Generate STALL on ep0in/out. 689 * 690 * Must be called with dev->lock held. 691 */ 692 static void gr_control_stall(struct gr_udc *dev) 693 { 694 u32 epctrl; 695 696 epctrl = gr_read32(&dev->epo[0].regs->epctrl); 697 gr_write32(&dev->epo[0].regs->epctrl, epctrl | GR_EPCTRL_CS); 698 epctrl = gr_read32(&dev->epi[0].regs->epctrl); 699 gr_write32(&dev->epi[0].regs->epctrl, epctrl | GR_EPCTRL_CS); 700 701 dev->ep0state = GR_EP0_STALL; 702 } 703 704 /* 705 * Halts, halts and wedges, or clears halt for an endpoint. 706 * 707 * Must be called with dev->lock held. 708 */ 709 static int gr_ep_halt_wedge(struct gr_ep *ep, int halt, int wedge, int fromhost) 710 { 711 u32 epctrl; 712 int retval = 0; 713 714 if (ep->num && !ep->ep.desc) 715 return -EINVAL; 716 717 if (ep->num && ep->ep.desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) 718 return -EOPNOTSUPP; 719 720 /* Never actually halt ep0, and therefore never clear halt for ep0 */ 721 if (!ep->num) { 722 if (halt && !fromhost) { 723 /* ep0 halt from gadget - generate protocol stall */ 724 gr_control_stall(ep->dev); 725 dev_dbg(ep->dev->dev, "EP: stall ep0\n"); 726 return 0; 727 } 728 return -EINVAL; 729 } 730 731 dev_dbg(ep->dev->dev, "EP: %s halt %s\n", 732 (halt ? (wedge ? "wedge" : "set") : "clear"), ep->ep.name); 733 734 epctrl = gr_read32(&ep->regs->epctrl); 735 if (halt) { 736 /* Set HALT */ 737 gr_write32(&ep->regs->epctrl, epctrl | GR_EPCTRL_EH); 738 ep->stopped = 1; 739 if (wedge) 740 ep->wedged = 1; 741 } else { 742 gr_write32(&ep->regs->epctrl, epctrl & ~GR_EPCTRL_EH); 743 ep->stopped = 0; 744 ep->wedged = 0; 745 746 /* Things might have been queued up in the meantime */ 747 if (!ep->dma_start) 748 gr_start_dma(ep); 749 } 750 751 return retval; 752 } 753 754 /* Must be called with dev->lock held */ 755 static inline void gr_set_ep0state(struct gr_udc *dev, enum gr_ep0state value) 756 { 757 if (dev->ep0state != value) 758 dev_vdbg(dev->dev, "STATE: ep0state=%s\n", 759 gr_ep0state_string(value)); 760 dev->ep0state = value; 761 } 762 763 /* 764 * Should only be called when endpoints can not generate interrupts. 765 * 766 * Must be called with dev->lock held. 767 */ 768 static void gr_disable_interrupts_and_pullup(struct gr_udc *dev) 769 { 770 gr_write32(&dev->regs->control, 0); 771 wmb(); /* Make sure that we do not deny one of our interrupts */ 772 dev->irq_enabled = 0; 773 } 774 775 /* 776 * Stop all device activity and disable data line pullup. 777 * 778 * Must be called with dev->lock held and irqs disabled. 779 */ 780 static void gr_stop_activity(struct gr_udc *dev) 781 { 782 struct gr_ep *ep; 783 784 list_for_each_entry(ep, &dev->ep_list, ep_list) 785 gr_ep_nuke(ep); 786 787 gr_disable_interrupts_and_pullup(dev); 788 789 gr_set_ep0state(dev, GR_EP0_DISCONNECT); 790 usb_gadget_set_state(&dev->gadget, USB_STATE_NOTATTACHED); 791 } 792 793 /* ---------------------------------------------------------------------- */ 794 /* ep0 setup packet handling */ 795 796 static void gr_ep0_testmode_complete(struct usb_ep *_ep, 797 struct usb_request *_req) 798 { 799 struct gr_ep *ep; 800 struct gr_udc *dev; 801 u32 control; 802 803 ep = container_of(_ep, struct gr_ep, ep); 804 dev = ep->dev; 805 806 spin_lock(&dev->lock); 807 808 control = gr_read32(&dev->regs->control); 809 control |= GR_CONTROL_TM | (dev->test_mode << GR_CONTROL_TS_POS); 810 gr_write32(&dev->regs->control, control); 811 812 spin_unlock(&dev->lock); 813 } 814 815 static void gr_ep0_dummy_complete(struct usb_ep *_ep, struct usb_request *_req) 816 { 817 /* Nothing needs to be done here */ 818 } 819 820 /* 821 * Queue a response on ep0in. 822 * 823 * Must be called with dev->lock held. 824 */ 825 static int gr_ep0_respond(struct gr_udc *dev, u8 *buf, int length, 826 void (*complete)(struct usb_ep *ep, 827 struct usb_request *req)) 828 { 829 u8 *reqbuf = dev->ep0reqi->req.buf; 830 int status; 831 int i; 832 833 for (i = 0; i < length; i++) 834 reqbuf[i] = buf[i]; 835 dev->ep0reqi->req.length = length; 836 dev->ep0reqi->req.complete = complete; 837 838 status = gr_queue_int(&dev->epi[0], dev->ep0reqi, GFP_ATOMIC); 839 if (status < 0) 840 dev_err(dev->dev, 841 "Could not queue ep0in setup response: %d\n", status); 842 843 return status; 844 } 845 846 /* 847 * Queue a 2 byte response on ep0in. 848 * 849 * Must be called with dev->lock held. 850 */ 851 static inline int gr_ep0_respond_u16(struct gr_udc *dev, u16 response) 852 { 853 __le16 le_response = cpu_to_le16(response); 854 855 return gr_ep0_respond(dev, (u8 *)&le_response, 2, 856 gr_ep0_dummy_complete); 857 } 858 859 /* 860 * Queue a ZLP response on ep0in. 861 * 862 * Must be called with dev->lock held. 863 */ 864 static inline int gr_ep0_respond_empty(struct gr_udc *dev) 865 { 866 return gr_ep0_respond(dev, NULL, 0, gr_ep0_dummy_complete); 867 } 868 869 /* 870 * This is run when a SET_ADDRESS request is received. First writes 871 * the new address to the control register which is updated internally 872 * when the next IN packet is ACKED. 873 * 874 * Must be called with dev->lock held. 875 */ 876 static void gr_set_address(struct gr_udc *dev, u8 address) 877 { 878 u32 control; 879 880 control = gr_read32(&dev->regs->control) & ~GR_CONTROL_UA_MASK; 881 control |= (address << GR_CONTROL_UA_POS) & GR_CONTROL_UA_MASK; 882 control |= GR_CONTROL_SU; 883 gr_write32(&dev->regs->control, control); 884 } 885 886 /* 887 * Returns negative for STALL, 0 for successful handling and positive for 888 * delegation. 889 * 890 * Must be called with dev->lock held. 891 */ 892 static int gr_device_request(struct gr_udc *dev, u8 type, u8 request, 893 u16 value, u16 index) 894 { 895 u16 response; 896 u8 test; 897 898 switch (request) { 899 case USB_REQ_SET_ADDRESS: 900 dev_dbg(dev->dev, "STATUS: address %d\n", value & 0xff); 901 gr_set_address(dev, value & 0xff); 902 if (value) 903 usb_gadget_set_state(&dev->gadget, USB_STATE_ADDRESS); 904 else 905 usb_gadget_set_state(&dev->gadget, USB_STATE_DEFAULT); 906 return gr_ep0_respond_empty(dev); 907 908 case USB_REQ_GET_STATUS: 909 /* Self powered | remote wakeup */ 910 response = 0x0001 | (dev->remote_wakeup ? 0x0002 : 0); 911 return gr_ep0_respond_u16(dev, response); 912 913 case USB_REQ_SET_FEATURE: 914 switch (value) { 915 case USB_DEVICE_REMOTE_WAKEUP: 916 /* Allow remote wakeup */ 917 dev->remote_wakeup = 1; 918 return gr_ep0_respond_empty(dev); 919 920 case USB_DEVICE_TEST_MODE: 921 /* The hardware does not support TEST_FORCE_EN */ 922 test = index >> 8; 923 if (test >= TEST_J && test <= TEST_PACKET) { 924 dev->test_mode = test; 925 return gr_ep0_respond(dev, NULL, 0, 926 gr_ep0_testmode_complete); 927 } 928 } 929 break; 930 931 case USB_REQ_CLEAR_FEATURE: 932 switch (value) { 933 case USB_DEVICE_REMOTE_WAKEUP: 934 /* Disallow remote wakeup */ 935 dev->remote_wakeup = 0; 936 return gr_ep0_respond_empty(dev); 937 } 938 break; 939 } 940 941 return 1; /* Delegate the rest */ 942 } 943 944 /* 945 * Returns negative for STALL, 0 for successful handling and positive for 946 * delegation. 947 * 948 * Must be called with dev->lock held. 949 */ 950 static int gr_interface_request(struct gr_udc *dev, u8 type, u8 request, 951 u16 value, u16 index) 952 { 953 if (dev->gadget.state != USB_STATE_CONFIGURED) 954 return -1; 955 956 /* 957 * Should return STALL for invalid interfaces, but udc driver does not 958 * know anything about that. However, many gadget drivers do not handle 959 * GET_STATUS so we need to take care of that. 960 */ 961 962 switch (request) { 963 case USB_REQ_GET_STATUS: 964 return gr_ep0_respond_u16(dev, 0x0000); 965 966 case USB_REQ_SET_FEATURE: 967 case USB_REQ_CLEAR_FEATURE: 968 /* 969 * No possible valid standard requests. Still let gadget drivers 970 * have a go at it. 971 */ 972 break; 973 } 974 975 return 1; /* Delegate the rest */ 976 } 977 978 /* 979 * Returns negative for STALL, 0 for successful handling and positive for 980 * delegation. 981 * 982 * Must be called with dev->lock held. 983 */ 984 static int gr_endpoint_request(struct gr_udc *dev, u8 type, u8 request, 985 u16 value, u16 index) 986 { 987 struct gr_ep *ep; 988 int status; 989 int halted; 990 u8 epnum = index & USB_ENDPOINT_NUMBER_MASK; 991 u8 is_in = index & USB_ENDPOINT_DIR_MASK; 992 993 if ((is_in && epnum >= dev->nepi) || (!is_in && epnum >= dev->nepo)) 994 return -1; 995 996 if (dev->gadget.state != USB_STATE_CONFIGURED && epnum != 0) 997 return -1; 998 999 ep = (is_in ? &dev->epi[epnum] : &dev->epo[epnum]); 1000 1001 switch (request) { 1002 case USB_REQ_GET_STATUS: 1003 halted = gr_read32(&ep->regs->epctrl) & GR_EPCTRL_EH; 1004 return gr_ep0_respond_u16(dev, halted ? 0x0001 : 0); 1005 1006 case USB_REQ_SET_FEATURE: 1007 switch (value) { 1008 case USB_ENDPOINT_HALT: 1009 status = gr_ep_halt_wedge(ep, 1, 0, 1); 1010 if (status >= 0) 1011 status = gr_ep0_respond_empty(dev); 1012 return status; 1013 } 1014 break; 1015 1016 case USB_REQ_CLEAR_FEATURE: 1017 switch (value) { 1018 case USB_ENDPOINT_HALT: 1019 if (ep->wedged) 1020 return -1; 1021 status = gr_ep_halt_wedge(ep, 0, 0, 1); 1022 if (status >= 0) 1023 status = gr_ep0_respond_empty(dev); 1024 return status; 1025 } 1026 break; 1027 } 1028 1029 return 1; /* Delegate the rest */ 1030 } 1031 1032 /* Must be called with dev->lock held */ 1033 static void gr_ep0out_requeue(struct gr_udc *dev) 1034 { 1035 int ret = gr_queue_int(&dev->epo[0], dev->ep0reqo, GFP_ATOMIC); 1036 1037 if (ret) 1038 dev_err(dev->dev, "Could not queue ep0out setup request: %d\n", 1039 ret); 1040 } 1041 1042 /* 1043 * The main function dealing with setup requests on ep0. 1044 * 1045 * Must be called with dev->lock held and irqs disabled 1046 */ 1047 static void gr_ep0_setup(struct gr_udc *dev, struct gr_request *req) 1048 __releases(&dev->lock) 1049 __acquires(&dev->lock) 1050 { 1051 union { 1052 struct usb_ctrlrequest ctrl; 1053 u8 raw[8]; 1054 u32 word[2]; 1055 } u; 1056 u8 type; 1057 u8 request; 1058 u16 value; 1059 u16 index; 1060 u16 length; 1061 int i; 1062 int status; 1063 1064 /* Restore from ep0 halt */ 1065 if (dev->ep0state == GR_EP0_STALL) { 1066 gr_set_ep0state(dev, GR_EP0_SETUP); 1067 if (!req->req.actual) 1068 goto out; 1069 } 1070 1071 if (dev->ep0state == GR_EP0_ISTATUS) { 1072 gr_set_ep0state(dev, GR_EP0_SETUP); 1073 if (req->req.actual > 0) 1074 dev_dbg(dev->dev, 1075 "Unexpected setup packet at state %s\n", 1076 gr_ep0state_string(GR_EP0_ISTATUS)); 1077 else 1078 goto out; /* Got expected ZLP */ 1079 } else if (dev->ep0state != GR_EP0_SETUP) { 1080 dev_info(dev->dev, 1081 "Unexpected ep0out request at state %s - stalling\n", 1082 gr_ep0state_string(dev->ep0state)); 1083 gr_control_stall(dev); 1084 gr_set_ep0state(dev, GR_EP0_SETUP); 1085 goto out; 1086 } else if (!req->req.actual) { 1087 dev_dbg(dev->dev, "Unexpected ZLP at state %s\n", 1088 gr_ep0state_string(dev->ep0state)); 1089 goto out; 1090 } 1091 1092 /* Handle SETUP packet */ 1093 for (i = 0; i < req->req.actual; i++) 1094 u.raw[i] = ((u8 *)req->req.buf)[i]; 1095 1096 type = u.ctrl.bRequestType; 1097 request = u.ctrl.bRequest; 1098 value = le16_to_cpu(u.ctrl.wValue); 1099 index = le16_to_cpu(u.ctrl.wIndex); 1100 length = le16_to_cpu(u.ctrl.wLength); 1101 1102 gr_dbgprint_devreq(dev, type, request, value, index, length); 1103 1104 /* Check for data stage */ 1105 if (length) { 1106 if (type & USB_DIR_IN) 1107 gr_set_ep0state(dev, GR_EP0_IDATA); 1108 else 1109 gr_set_ep0state(dev, GR_EP0_ODATA); 1110 } 1111 1112 status = 1; /* Positive status flags delegation */ 1113 if ((type & USB_TYPE_MASK) == USB_TYPE_STANDARD) { 1114 switch (type & USB_RECIP_MASK) { 1115 case USB_RECIP_DEVICE: 1116 status = gr_device_request(dev, type, request, 1117 value, index); 1118 break; 1119 case USB_RECIP_ENDPOINT: 1120 status = gr_endpoint_request(dev, type, request, 1121 value, index); 1122 break; 1123 case USB_RECIP_INTERFACE: 1124 status = gr_interface_request(dev, type, request, 1125 value, index); 1126 break; 1127 } 1128 } 1129 1130 if (status > 0) { 1131 spin_unlock(&dev->lock); 1132 1133 dev_vdbg(dev->dev, "DELEGATE\n"); 1134 status = dev->driver->setup(&dev->gadget, &u.ctrl); 1135 1136 spin_lock(&dev->lock); 1137 } 1138 1139 /* Generate STALL on both ep0out and ep0in if requested */ 1140 if (unlikely(status < 0)) { 1141 dev_vdbg(dev->dev, "STALL\n"); 1142 gr_control_stall(dev); 1143 } 1144 1145 if ((type & USB_TYPE_MASK) == USB_TYPE_STANDARD && 1146 request == USB_REQ_SET_CONFIGURATION) { 1147 if (!value) { 1148 dev_dbg(dev->dev, "STATUS: deconfigured\n"); 1149 usb_gadget_set_state(&dev->gadget, USB_STATE_ADDRESS); 1150 } else if (status >= 0) { 1151 /* Not configured unless gadget OK:s it */ 1152 dev_dbg(dev->dev, "STATUS: configured: %d\n", value); 1153 usb_gadget_set_state(&dev->gadget, 1154 USB_STATE_CONFIGURED); 1155 } 1156 } 1157 1158 /* Get ready for next stage */ 1159 if (dev->ep0state == GR_EP0_ODATA) 1160 gr_set_ep0state(dev, GR_EP0_OSTATUS); 1161 else if (dev->ep0state == GR_EP0_IDATA) 1162 gr_set_ep0state(dev, GR_EP0_ISTATUS); 1163 else 1164 gr_set_ep0state(dev, GR_EP0_SETUP); 1165 1166 out: 1167 gr_ep0out_requeue(dev); 1168 } 1169 1170 /* ---------------------------------------------------------------------- */ 1171 /* VBUS and USB reset handling */ 1172 1173 /* Must be called with dev->lock held and irqs disabled */ 1174 static void gr_vbus_connected(struct gr_udc *dev, u32 status) 1175 { 1176 u32 control; 1177 1178 dev->gadget.speed = GR_SPEED(status); 1179 usb_gadget_set_state(&dev->gadget, USB_STATE_POWERED); 1180 1181 /* Turn on full interrupts and pullup */ 1182 control = (GR_CONTROL_SI | GR_CONTROL_UI | GR_CONTROL_VI | 1183 GR_CONTROL_SP | GR_CONTROL_EP); 1184 gr_write32(&dev->regs->control, control); 1185 } 1186 1187 /* Must be called with dev->lock held */ 1188 static void gr_enable_vbus_detect(struct gr_udc *dev) 1189 { 1190 u32 status; 1191 1192 dev->irq_enabled = 1; 1193 wmb(); /* Make sure we do not ignore an interrupt */ 1194 gr_write32(&dev->regs->control, GR_CONTROL_VI); 1195 1196 /* Take care of the case we are already plugged in at this point */ 1197 status = gr_read32(&dev->regs->status); 1198 if (status & GR_STATUS_VB) 1199 gr_vbus_connected(dev, status); 1200 } 1201 1202 /* Must be called with dev->lock held and irqs disabled */ 1203 static void gr_vbus_disconnected(struct gr_udc *dev) 1204 { 1205 gr_stop_activity(dev); 1206 1207 /* Report disconnect */ 1208 if (dev->driver && dev->driver->disconnect) { 1209 spin_unlock(&dev->lock); 1210 1211 dev->driver->disconnect(&dev->gadget); 1212 1213 spin_lock(&dev->lock); 1214 } 1215 1216 gr_enable_vbus_detect(dev); 1217 } 1218 1219 /* Must be called with dev->lock held and irqs disabled */ 1220 static void gr_udc_usbreset(struct gr_udc *dev, u32 status) 1221 { 1222 gr_set_address(dev, 0); 1223 gr_set_ep0state(dev, GR_EP0_SETUP); 1224 usb_gadget_set_state(&dev->gadget, USB_STATE_DEFAULT); 1225 dev->gadget.speed = GR_SPEED(status); 1226 1227 gr_ep_nuke(&dev->epo[0]); 1228 gr_ep_nuke(&dev->epi[0]); 1229 dev->epo[0].stopped = 0; 1230 dev->epi[0].stopped = 0; 1231 gr_ep0out_requeue(dev); 1232 } 1233 1234 /* ---------------------------------------------------------------------- */ 1235 /* Irq handling */ 1236 1237 /* 1238 * Handles interrupts from in endpoints. Returns whether something was handled. 1239 * 1240 * Must be called with dev->lock held, irqs disabled and with !ep->stopped. 1241 */ 1242 static int gr_handle_in_ep(struct gr_ep *ep) 1243 { 1244 struct gr_request *req; 1245 1246 req = list_first_entry(&ep->queue, struct gr_request, queue); 1247 if (!req->last_desc) 1248 return 0; 1249 1250 if (ACCESS_ONCE(req->last_desc->ctrl) & GR_DESC_IN_CTRL_EN) 1251 return 0; /* Not put in hardware buffers yet */ 1252 1253 if (gr_read32(&ep->regs->epstat) & (GR_EPSTAT_B1 | GR_EPSTAT_B0)) 1254 return 0; /* Not transmitted yet, still in hardware buffers */ 1255 1256 /* Write complete */ 1257 gr_dma_advance(ep, 0); 1258 1259 return 1; 1260 } 1261 1262 /* 1263 * Handles interrupts from out endpoints. Returns whether something was handled. 1264 * 1265 * Must be called with dev->lock held, irqs disabled and with !ep->stopped. 1266 */ 1267 static int gr_handle_out_ep(struct gr_ep *ep) 1268 { 1269 u32 ep_dmactrl; 1270 u32 ctrl; 1271 u16 len; 1272 struct gr_request *req; 1273 struct gr_udc *dev = ep->dev; 1274 1275 req = list_first_entry(&ep->queue, struct gr_request, queue); 1276 if (!req->curr_desc) 1277 return 0; 1278 1279 ctrl = ACCESS_ONCE(req->curr_desc->ctrl); 1280 if (ctrl & GR_DESC_OUT_CTRL_EN) 1281 return 0; /* Not received yet */ 1282 1283 /* Read complete */ 1284 len = ctrl & GR_DESC_OUT_CTRL_LEN_MASK; 1285 req->req.actual += len; 1286 if (ctrl & GR_DESC_OUT_CTRL_SE) 1287 req->setup = 1; 1288 1289 if (len < ep->ep.maxpacket || req->req.actual == req->req.length) { 1290 /* Short packet or the expected size - we are done */ 1291 1292 if ((ep == &dev->epo[0]) && (dev->ep0state == GR_EP0_OSTATUS)) { 1293 /* 1294 * Send a status stage ZLP to ack the DATA stage in the 1295 * OUT direction. This needs to be done before 1296 * gr_dma_advance as that can lead to a call to 1297 * ep0_setup that can change dev->ep0state. 1298 */ 1299 gr_ep0_respond_empty(dev); 1300 gr_set_ep0state(dev, GR_EP0_SETUP); 1301 } 1302 1303 gr_dma_advance(ep, 0); 1304 } else { 1305 /* Not done yet. Enable the next descriptor to receive more. */ 1306 req->curr_desc = req->curr_desc->next_desc; 1307 req->curr_desc->ctrl |= GR_DESC_OUT_CTRL_EN; 1308 1309 ep_dmactrl = gr_read32(&ep->regs->dmactrl); 1310 gr_write32(&ep->regs->dmactrl, ep_dmactrl | GR_DMACTRL_DA); 1311 } 1312 1313 return 1; 1314 } 1315 1316 /* 1317 * Handle state changes. Returns whether something was handled. 1318 * 1319 * Must be called with dev->lock held and irqs disabled. 1320 */ 1321 static int gr_handle_state_changes(struct gr_udc *dev) 1322 { 1323 u32 status = gr_read32(&dev->regs->status); 1324 int handled = 0; 1325 int powstate = !(dev->gadget.state == USB_STATE_NOTATTACHED || 1326 dev->gadget.state == USB_STATE_ATTACHED); 1327 1328 /* VBUS valid detected */ 1329 if (!powstate && (status & GR_STATUS_VB)) { 1330 dev_dbg(dev->dev, "STATUS: vbus valid detected\n"); 1331 gr_vbus_connected(dev, status); 1332 handled = 1; 1333 } 1334 1335 /* Disconnect */ 1336 if (powstate && !(status & GR_STATUS_VB)) { 1337 dev_dbg(dev->dev, "STATUS: vbus invalid detected\n"); 1338 gr_vbus_disconnected(dev); 1339 handled = 1; 1340 } 1341 1342 /* USB reset detected */ 1343 if (status & GR_STATUS_UR) { 1344 dev_dbg(dev->dev, "STATUS: USB reset - speed is %s\n", 1345 GR_SPEED_STR(status)); 1346 gr_write32(&dev->regs->status, GR_STATUS_UR); 1347 gr_udc_usbreset(dev, status); 1348 handled = 1; 1349 } 1350 1351 /* Speed change */ 1352 if (dev->gadget.speed != GR_SPEED(status)) { 1353 dev_dbg(dev->dev, "STATUS: USB Speed change to %s\n", 1354 GR_SPEED_STR(status)); 1355 dev->gadget.speed = GR_SPEED(status); 1356 handled = 1; 1357 } 1358 1359 /* Going into suspend */ 1360 if ((dev->ep0state != GR_EP0_SUSPEND) && !(status & GR_STATUS_SU)) { 1361 dev_dbg(dev->dev, "STATUS: USB suspend\n"); 1362 gr_set_ep0state(dev, GR_EP0_SUSPEND); 1363 dev->suspended_from = dev->gadget.state; 1364 usb_gadget_set_state(&dev->gadget, USB_STATE_SUSPENDED); 1365 1366 if ((dev->gadget.speed != USB_SPEED_UNKNOWN) && 1367 dev->driver && dev->driver->suspend) { 1368 spin_unlock(&dev->lock); 1369 1370 dev->driver->suspend(&dev->gadget); 1371 1372 spin_lock(&dev->lock); 1373 } 1374 handled = 1; 1375 } 1376 1377 /* Coming out of suspend */ 1378 if ((dev->ep0state == GR_EP0_SUSPEND) && (status & GR_STATUS_SU)) { 1379 dev_dbg(dev->dev, "STATUS: USB resume\n"); 1380 if (dev->suspended_from == USB_STATE_POWERED) 1381 gr_set_ep0state(dev, GR_EP0_DISCONNECT); 1382 else 1383 gr_set_ep0state(dev, GR_EP0_SETUP); 1384 usb_gadget_set_state(&dev->gadget, dev->suspended_from); 1385 1386 if ((dev->gadget.speed != USB_SPEED_UNKNOWN) && 1387 dev->driver && dev->driver->resume) { 1388 spin_unlock(&dev->lock); 1389 1390 dev->driver->resume(&dev->gadget); 1391 1392 spin_lock(&dev->lock); 1393 } 1394 handled = 1; 1395 } 1396 1397 return handled; 1398 } 1399 1400 /* Non-interrupt context irq handler */ 1401 static irqreturn_t gr_irq_handler(int irq, void *_dev) 1402 { 1403 struct gr_udc *dev = _dev; 1404 struct gr_ep *ep; 1405 int handled = 0; 1406 int i; 1407 unsigned long flags; 1408 1409 spin_lock_irqsave(&dev->lock, flags); 1410 1411 if (!dev->irq_enabled) 1412 goto out; 1413 1414 /* 1415 * Check IN ep interrupts. We check these before the OUT eps because 1416 * some gadgets reuse the request that might already be currently 1417 * outstanding and needs to be completed (mainly setup requests). 1418 */ 1419 for (i = 0; i < dev->nepi; i++) { 1420 ep = &dev->epi[i]; 1421 if (!ep->stopped && !ep->callback && !list_empty(&ep->queue)) 1422 handled = gr_handle_in_ep(ep) || handled; 1423 } 1424 1425 /* Check OUT ep interrupts */ 1426 for (i = 0; i < dev->nepo; i++) { 1427 ep = &dev->epo[i]; 1428 if (!ep->stopped && !ep->callback && !list_empty(&ep->queue)) 1429 handled = gr_handle_out_ep(ep) || handled; 1430 } 1431 1432 /* Check status interrupts */ 1433 handled = gr_handle_state_changes(dev) || handled; 1434 1435 /* 1436 * Check AMBA DMA errors. Only check if we didn't find anything else to 1437 * handle because this shouldn't happen if we did everything right. 1438 */ 1439 if (!handled) { 1440 list_for_each_entry(ep, &dev->ep_list, ep_list) { 1441 if (gr_read32(&ep->regs->dmactrl) & GR_DMACTRL_AE) { 1442 dev_err(dev->dev, 1443 "AMBA Error occurred for %s\n", 1444 ep->ep.name); 1445 handled = 1; 1446 } 1447 } 1448 } 1449 1450 out: 1451 spin_unlock_irqrestore(&dev->lock, flags); 1452 1453 return handled ? IRQ_HANDLED : IRQ_NONE; 1454 } 1455 1456 /* Interrupt context irq handler */ 1457 static irqreturn_t gr_irq(int irq, void *_dev) 1458 { 1459 struct gr_udc *dev = _dev; 1460 1461 if (!dev->irq_enabled) 1462 return IRQ_NONE; 1463 1464 return IRQ_WAKE_THREAD; 1465 } 1466 1467 /* ---------------------------------------------------------------------- */ 1468 /* USB ep ops */ 1469 1470 /* Enable endpoint. Not for ep0in and ep0out that are handled separately. */ 1471 static int gr_ep_enable(struct usb_ep *_ep, 1472 const struct usb_endpoint_descriptor *desc) 1473 { 1474 struct gr_udc *dev; 1475 struct gr_ep *ep; 1476 u8 mode; 1477 u8 nt; 1478 u16 max; 1479 u16 buffer_size = 0; 1480 u32 epctrl; 1481 1482 ep = container_of(_ep, struct gr_ep, ep); 1483 if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) 1484 return -EINVAL; 1485 1486 dev = ep->dev; 1487 1488 /* 'ep0' IN and OUT are reserved */ 1489 if (ep == &dev->epo[0] || ep == &dev->epi[0]) 1490 return -EINVAL; 1491 1492 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) 1493 return -ESHUTDOWN; 1494 1495 /* Make sure we are clear for enabling */ 1496 epctrl = gr_read32(&ep->regs->epctrl); 1497 if (epctrl & GR_EPCTRL_EV) 1498 return -EBUSY; 1499 1500 /* Check that directions match */ 1501 if (!ep->is_in != !usb_endpoint_dir_in(desc)) 1502 return -EINVAL; 1503 1504 /* Check ep num */ 1505 if ((!ep->is_in && ep->num >= dev->nepo) || 1506 (ep->is_in && ep->num >= dev->nepi)) 1507 return -EINVAL; 1508 1509 if (usb_endpoint_xfer_control(desc)) { 1510 mode = 0; 1511 } else if (usb_endpoint_xfer_isoc(desc)) { 1512 mode = 1; 1513 } else if (usb_endpoint_xfer_bulk(desc)) { 1514 mode = 2; 1515 } else if (usb_endpoint_xfer_int(desc)) { 1516 mode = 3; 1517 } else { 1518 dev_err(dev->dev, "Unknown transfer type for %s\n", 1519 ep->ep.name); 1520 return -EINVAL; 1521 } 1522 1523 /* 1524 * Bits 10-0 set the max payload. 12-11 set the number of 1525 * additional transactions. 1526 */ 1527 max = 0x7ff & usb_endpoint_maxp(desc); 1528 nt = 0x3 & (usb_endpoint_maxp(desc) >> 11); 1529 buffer_size = GR_BUFFER_SIZE(epctrl); 1530 if (nt && (mode == 0 || mode == 2)) { 1531 dev_err(dev->dev, 1532 "%s mode: multiple trans./microframe not valid\n", 1533 (mode == 2 ? "Bulk" : "Control")); 1534 return -EINVAL; 1535 } else if (nt == 0x3) { 1536 dev_err(dev->dev, 1537 "Invalid value 0x3 for additional trans./microframe\n"); 1538 return -EINVAL; 1539 } else if ((nt + 1) * max > buffer_size) { 1540 dev_err(dev->dev, "Hw buffer size %d < max payload %d * %d\n", 1541 buffer_size, (nt + 1), max); 1542 return -EINVAL; 1543 } else if (max == 0) { 1544 dev_err(dev->dev, "Max payload cannot be set to 0\n"); 1545 return -EINVAL; 1546 } else if (max > ep->ep.maxpacket_limit) { 1547 dev_err(dev->dev, "Requested max payload %d > limit %d\n", 1548 max, ep->ep.maxpacket_limit); 1549 return -EINVAL; 1550 } 1551 1552 spin_lock(&ep->dev->lock); 1553 1554 if (!ep->stopped) { 1555 spin_unlock(&ep->dev->lock); 1556 return -EBUSY; 1557 } 1558 1559 ep->stopped = 0; 1560 ep->wedged = 0; 1561 ep->ep.desc = desc; 1562 ep->ep.maxpacket = max; 1563 ep->dma_start = 0; 1564 1565 1566 if (nt) { 1567 /* 1568 * Maximum possible size of all payloads in one microframe 1569 * regardless of direction when using high-bandwidth mode. 1570 */ 1571 ep->bytes_per_buffer = (nt + 1) * max; 1572 } else if (ep->is_in) { 1573 /* 1574 * The biggest multiple of maximum packet size that fits into 1575 * the buffer. The hardware will split up into many packets in 1576 * the IN direction. 1577 */ 1578 ep->bytes_per_buffer = (buffer_size / max) * max; 1579 } else { 1580 /* 1581 * Only single packets will be placed the buffers in the OUT 1582 * direction. 1583 */ 1584 ep->bytes_per_buffer = max; 1585 } 1586 1587 epctrl = (max << GR_EPCTRL_MAXPL_POS) 1588 | (nt << GR_EPCTRL_NT_POS) 1589 | (mode << GR_EPCTRL_TT_POS) 1590 | GR_EPCTRL_EV; 1591 if (ep->is_in) 1592 epctrl |= GR_EPCTRL_PI; 1593 gr_write32(&ep->regs->epctrl, epctrl); 1594 1595 gr_write32(&ep->regs->dmactrl, GR_DMACTRL_IE | GR_DMACTRL_AI); 1596 1597 spin_unlock(&ep->dev->lock); 1598 1599 dev_dbg(ep->dev->dev, "EP: %s enabled - %s with %d bytes/buffer\n", 1600 ep->ep.name, gr_modestring[mode], ep->bytes_per_buffer); 1601 return 0; 1602 } 1603 1604 /* Disable endpoint. Not for ep0in and ep0out that are handled separately. */ 1605 static int gr_ep_disable(struct usb_ep *_ep) 1606 { 1607 struct gr_ep *ep; 1608 struct gr_udc *dev; 1609 unsigned long flags; 1610 1611 ep = container_of(_ep, struct gr_ep, ep); 1612 if (!_ep || !ep->ep.desc) 1613 return -ENODEV; 1614 1615 dev = ep->dev; 1616 1617 /* 'ep0' IN and OUT are reserved */ 1618 if (ep == &dev->epo[0] || ep == &dev->epi[0]) 1619 return -EINVAL; 1620 1621 if (dev->ep0state == GR_EP0_SUSPEND) 1622 return -EBUSY; 1623 1624 dev_dbg(ep->dev->dev, "EP: disable %s\n", ep->ep.name); 1625 1626 spin_lock_irqsave(&dev->lock, flags); 1627 1628 gr_ep_nuke(ep); 1629 gr_ep_reset(ep); 1630 ep->ep.desc = NULL; 1631 1632 spin_unlock_irqrestore(&dev->lock, flags); 1633 1634 return 0; 1635 } 1636 1637 /* 1638 * Frees a request, but not any DMA buffers associated with it 1639 * (gr_finish_request should already have taken care of that). 1640 */ 1641 static void gr_free_request(struct usb_ep *_ep, struct usb_request *_req) 1642 { 1643 struct gr_request *req; 1644 1645 if (!_ep || !_req) 1646 return; 1647 req = container_of(_req, struct gr_request, req); 1648 1649 /* Leads to memory leak */ 1650 WARN(!list_empty(&req->queue), 1651 "request not dequeued properly before freeing\n"); 1652 1653 kfree(req); 1654 } 1655 1656 /* Queue a request from the gadget */ 1657 static int gr_queue_ext(struct usb_ep *_ep, struct usb_request *_req, 1658 gfp_t gfp_flags) 1659 { 1660 struct gr_ep *ep; 1661 struct gr_request *req; 1662 struct gr_udc *dev; 1663 int ret; 1664 1665 if (unlikely(!_ep || !_req)) 1666 return -EINVAL; 1667 1668 ep = container_of(_ep, struct gr_ep, ep); 1669 req = container_of(_req, struct gr_request, req); 1670 dev = ep->dev; 1671 1672 spin_lock(&ep->dev->lock); 1673 1674 /* 1675 * The ep0 pointer in the gadget struct is used both for ep0in and 1676 * ep0out. In a data stage in the out direction ep0out needs to be used 1677 * instead of the default ep0in. Completion functions might use 1678 * driver_data, so that needs to be copied as well. 1679 */ 1680 if ((ep == &dev->epi[0]) && (dev->ep0state == GR_EP0_ODATA)) { 1681 ep = &dev->epo[0]; 1682 ep->ep.driver_data = dev->epi[0].ep.driver_data; 1683 } 1684 1685 if (ep->is_in) 1686 gr_dbgprint_request("EXTERN", ep, req); 1687 1688 ret = gr_queue(ep, req, GFP_ATOMIC); 1689 1690 spin_unlock(&ep->dev->lock); 1691 1692 return ret; 1693 } 1694 1695 /* Dequeue JUST ONE request */ 1696 static int gr_dequeue(struct usb_ep *_ep, struct usb_request *_req) 1697 { 1698 struct gr_request *req; 1699 struct gr_ep *ep; 1700 struct gr_udc *dev; 1701 int ret = 0; 1702 unsigned long flags; 1703 1704 ep = container_of(_ep, struct gr_ep, ep); 1705 if (!_ep || !_req || (!ep->ep.desc && ep->num != 0)) 1706 return -EINVAL; 1707 dev = ep->dev; 1708 if (!dev->driver) 1709 return -ESHUTDOWN; 1710 1711 /* We can't touch (DMA) registers when suspended */ 1712 if (dev->ep0state == GR_EP0_SUSPEND) 1713 return -EBUSY; 1714 1715 spin_lock_irqsave(&dev->lock, flags); 1716 1717 /* Make sure it's actually queued on this endpoint */ 1718 list_for_each_entry(req, &ep->queue, queue) { 1719 if (&req->req == _req) 1720 break; 1721 } 1722 if (&req->req != _req) { 1723 ret = -EINVAL; 1724 goto out; 1725 } 1726 1727 if (list_first_entry(&ep->queue, struct gr_request, queue) == req) { 1728 /* This request is currently being processed */ 1729 gr_abort_dma(ep); 1730 if (ep->stopped) 1731 gr_finish_request(ep, req, -ECONNRESET); 1732 else 1733 gr_dma_advance(ep, -ECONNRESET); 1734 } else if (!list_empty(&req->queue)) { 1735 /* Not being processed - gr_finish_request dequeues it */ 1736 gr_finish_request(ep, req, -ECONNRESET); 1737 } else { 1738 ret = -EOPNOTSUPP; 1739 } 1740 1741 out: 1742 spin_unlock_irqrestore(&dev->lock, flags); 1743 1744 return ret; 1745 } 1746 1747 /* Helper for gr_set_halt and gr_set_wedge */ 1748 static int gr_set_halt_wedge(struct usb_ep *_ep, int halt, int wedge) 1749 { 1750 int ret; 1751 struct gr_ep *ep; 1752 1753 if (!_ep) 1754 return -ENODEV; 1755 ep = container_of(_ep, struct gr_ep, ep); 1756 1757 spin_lock(&ep->dev->lock); 1758 1759 /* Halting an IN endpoint should fail if queue is not empty */ 1760 if (halt && ep->is_in && !list_empty(&ep->queue)) { 1761 ret = -EAGAIN; 1762 goto out; 1763 } 1764 1765 ret = gr_ep_halt_wedge(ep, halt, wedge, 0); 1766 1767 out: 1768 spin_unlock(&ep->dev->lock); 1769 1770 return ret; 1771 } 1772 1773 /* Halt endpoint */ 1774 static int gr_set_halt(struct usb_ep *_ep, int halt) 1775 { 1776 return gr_set_halt_wedge(_ep, halt, 0); 1777 } 1778 1779 /* Halt and wedge endpoint */ 1780 static int gr_set_wedge(struct usb_ep *_ep) 1781 { 1782 return gr_set_halt_wedge(_ep, 1, 1); 1783 } 1784 1785 /* 1786 * Return the total number of bytes currently stored in the internal buffers of 1787 * the endpoint. 1788 */ 1789 static int gr_fifo_status(struct usb_ep *_ep) 1790 { 1791 struct gr_ep *ep; 1792 u32 epstat; 1793 u32 bytes = 0; 1794 1795 if (!_ep) 1796 return -ENODEV; 1797 ep = container_of(_ep, struct gr_ep, ep); 1798 1799 epstat = gr_read32(&ep->regs->epstat); 1800 1801 if (epstat & GR_EPSTAT_B0) 1802 bytes += (epstat & GR_EPSTAT_B0CNT_MASK) >> GR_EPSTAT_B0CNT_POS; 1803 if (epstat & GR_EPSTAT_B1) 1804 bytes += (epstat & GR_EPSTAT_B1CNT_MASK) >> GR_EPSTAT_B1CNT_POS; 1805 1806 return bytes; 1807 } 1808 1809 1810 /* Empty data from internal buffers of an endpoint. */ 1811 static void gr_fifo_flush(struct usb_ep *_ep) 1812 { 1813 struct gr_ep *ep; 1814 u32 epctrl; 1815 1816 if (!_ep) 1817 return; 1818 ep = container_of(_ep, struct gr_ep, ep); 1819 dev_vdbg(ep->dev->dev, "EP: flush fifo %s\n", ep->ep.name); 1820 1821 spin_lock(&ep->dev->lock); 1822 1823 epctrl = gr_read32(&ep->regs->epctrl); 1824 epctrl |= GR_EPCTRL_CB; 1825 gr_write32(&ep->regs->epctrl, epctrl); 1826 1827 spin_unlock(&ep->dev->lock); 1828 } 1829 1830 static struct usb_ep_ops gr_ep_ops = { 1831 .enable = gr_ep_enable, 1832 .disable = gr_ep_disable, 1833 1834 .alloc_request = gr_alloc_request, 1835 .free_request = gr_free_request, 1836 1837 .queue = gr_queue_ext, 1838 .dequeue = gr_dequeue, 1839 1840 .set_halt = gr_set_halt, 1841 .set_wedge = gr_set_wedge, 1842 .fifo_status = gr_fifo_status, 1843 .fifo_flush = gr_fifo_flush, 1844 }; 1845 1846 /* ---------------------------------------------------------------------- */ 1847 /* USB Gadget ops */ 1848 1849 static int gr_get_frame(struct usb_gadget *_gadget) 1850 { 1851 struct gr_udc *dev; 1852 1853 if (!_gadget) 1854 return -ENODEV; 1855 dev = container_of(_gadget, struct gr_udc, gadget); 1856 return gr_read32(&dev->regs->status) & GR_STATUS_FN_MASK; 1857 } 1858 1859 static int gr_wakeup(struct usb_gadget *_gadget) 1860 { 1861 struct gr_udc *dev; 1862 1863 if (!_gadget) 1864 return -ENODEV; 1865 dev = container_of(_gadget, struct gr_udc, gadget); 1866 1867 /* Remote wakeup feature not enabled by host*/ 1868 if (!dev->remote_wakeup) 1869 return -EINVAL; 1870 1871 spin_lock(&dev->lock); 1872 1873 gr_write32(&dev->regs->control, 1874 gr_read32(&dev->regs->control) | GR_CONTROL_RW); 1875 1876 spin_unlock(&dev->lock); 1877 1878 return 0; 1879 } 1880 1881 static int gr_pullup(struct usb_gadget *_gadget, int is_on) 1882 { 1883 struct gr_udc *dev; 1884 u32 control; 1885 1886 if (!_gadget) 1887 return -ENODEV; 1888 dev = container_of(_gadget, struct gr_udc, gadget); 1889 1890 spin_lock(&dev->lock); 1891 1892 control = gr_read32(&dev->regs->control); 1893 if (is_on) 1894 control |= GR_CONTROL_EP; 1895 else 1896 control &= ~GR_CONTROL_EP; 1897 gr_write32(&dev->regs->control, control); 1898 1899 spin_unlock(&dev->lock); 1900 1901 return 0; 1902 } 1903 1904 static int gr_udc_start(struct usb_gadget *gadget, 1905 struct usb_gadget_driver *driver) 1906 { 1907 struct gr_udc *dev = to_gr_udc(gadget); 1908 1909 spin_lock(&dev->lock); 1910 1911 /* Hook up the driver */ 1912 driver->driver.bus = NULL; 1913 dev->driver = driver; 1914 1915 /* Get ready for host detection */ 1916 gr_enable_vbus_detect(dev); 1917 1918 spin_unlock(&dev->lock); 1919 1920 dev_info(dev->dev, "Started with gadget driver '%s'\n", 1921 driver->driver.name); 1922 1923 return 0; 1924 } 1925 1926 static int gr_udc_stop(struct usb_gadget *gadget, 1927 struct usb_gadget_driver *driver) 1928 { 1929 struct gr_udc *dev = to_gr_udc(gadget); 1930 unsigned long flags; 1931 1932 spin_lock_irqsave(&dev->lock, flags); 1933 1934 dev->driver = NULL; 1935 gr_stop_activity(dev); 1936 1937 spin_unlock_irqrestore(&dev->lock, flags); 1938 1939 dev_info(dev->dev, "Stopped\n"); 1940 1941 return 0; 1942 } 1943 1944 static const struct usb_gadget_ops gr_ops = { 1945 .get_frame = gr_get_frame, 1946 .wakeup = gr_wakeup, 1947 .pullup = gr_pullup, 1948 .udc_start = gr_udc_start, 1949 .udc_stop = gr_udc_stop, 1950 /* Other operations not supported */ 1951 }; 1952 1953 /* ---------------------------------------------------------------------- */ 1954 /* Module probe, removal and of-matching */ 1955 1956 static const char * const onames[] = { 1957 "ep0out", "ep1out", "ep2out", "ep3out", "ep4out", "ep5out", 1958 "ep6out", "ep7out", "ep8out", "ep9out", "ep10out", "ep11out", 1959 "ep12out", "ep13out", "ep14out", "ep15out" 1960 }; 1961 1962 static const char * const inames[] = { 1963 "ep0in", "ep1in", "ep2in", "ep3in", "ep4in", "ep5in", 1964 "ep6in", "ep7in", "ep8in", "ep9in", "ep10in", "ep11in", 1965 "ep12in", "ep13in", "ep14in", "ep15in" 1966 }; 1967 1968 /* Must be called with dev->lock held */ 1969 static int gr_ep_init(struct gr_udc *dev, int num, int is_in, u32 maxplimit) 1970 { 1971 struct gr_ep *ep; 1972 struct gr_request *req; 1973 struct usb_request *_req; 1974 void *buf; 1975 1976 if (is_in) { 1977 ep = &dev->epi[num]; 1978 ep->ep.name = inames[num]; 1979 ep->regs = &dev->regs->epi[num]; 1980 } else { 1981 ep = &dev->epo[num]; 1982 ep->ep.name = onames[num]; 1983 ep->regs = &dev->regs->epo[num]; 1984 } 1985 1986 gr_ep_reset(ep); 1987 ep->num = num; 1988 ep->is_in = is_in; 1989 ep->dev = dev; 1990 ep->ep.ops = &gr_ep_ops; 1991 INIT_LIST_HEAD(&ep->queue); 1992 1993 if (num == 0) { 1994 _req = gr_alloc_request(&ep->ep, GFP_ATOMIC); 1995 buf = devm_kzalloc(dev->dev, PAGE_SIZE, GFP_DMA | GFP_ATOMIC); 1996 if (!_req || !buf) { 1997 /* possible _req freed by gr_probe via gr_remove */ 1998 return -ENOMEM; 1999 } 2000 2001 req = container_of(_req, struct gr_request, req); 2002 req->req.buf = buf; 2003 req->req.length = MAX_CTRL_PL_SIZE; 2004 2005 if (is_in) 2006 dev->ep0reqi = req; /* Complete gets set as used */ 2007 else 2008 dev->ep0reqo = req; /* Completion treated separately */ 2009 2010 usb_ep_set_maxpacket_limit(&ep->ep, MAX_CTRL_PL_SIZE); 2011 ep->bytes_per_buffer = MAX_CTRL_PL_SIZE; 2012 } else { 2013 usb_ep_set_maxpacket_limit(&ep->ep, (u16)maxplimit); 2014 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list); 2015 } 2016 list_add_tail(&ep->ep_list, &dev->ep_list); 2017 2018 return 0; 2019 } 2020 2021 /* Must be called with dev->lock held */ 2022 static int gr_udc_init(struct gr_udc *dev) 2023 { 2024 struct device_node *np = dev->dev->of_node; 2025 u32 epctrl_val; 2026 u32 dmactrl_val; 2027 int i; 2028 int ret = 0; 2029 u32 bufsize; 2030 2031 gr_set_address(dev, 0); 2032 2033 INIT_LIST_HEAD(&dev->gadget.ep_list); 2034 dev->gadget.speed = USB_SPEED_UNKNOWN; 2035 dev->gadget.ep0 = &dev->epi[0].ep; 2036 2037 INIT_LIST_HEAD(&dev->ep_list); 2038 gr_set_ep0state(dev, GR_EP0_DISCONNECT); 2039 2040 for (i = 0; i < dev->nepo; i++) { 2041 if (of_property_read_u32_index(np, "epobufsizes", i, &bufsize)) 2042 bufsize = 1024; 2043 ret = gr_ep_init(dev, i, 0, bufsize); 2044 if (ret) 2045 return ret; 2046 } 2047 2048 for (i = 0; i < dev->nepi; i++) { 2049 if (of_property_read_u32_index(np, "epibufsizes", i, &bufsize)) 2050 bufsize = 1024; 2051 ret = gr_ep_init(dev, i, 1, bufsize); 2052 if (ret) 2053 return ret; 2054 } 2055 2056 /* Must be disabled by default */ 2057 dev->remote_wakeup = 0; 2058 2059 /* Enable ep0out and ep0in */ 2060 epctrl_val = (MAX_CTRL_PL_SIZE << GR_EPCTRL_MAXPL_POS) | GR_EPCTRL_EV; 2061 dmactrl_val = GR_DMACTRL_IE | GR_DMACTRL_AI; 2062 gr_write32(&dev->epo[0].regs->epctrl, epctrl_val); 2063 gr_write32(&dev->epi[0].regs->epctrl, epctrl_val | GR_EPCTRL_PI); 2064 gr_write32(&dev->epo[0].regs->dmactrl, dmactrl_val); 2065 gr_write32(&dev->epi[0].regs->dmactrl, dmactrl_val); 2066 2067 return 0; 2068 } 2069 2070 static int gr_remove(struct platform_device *pdev) 2071 { 2072 struct gr_udc *dev = platform_get_drvdata(pdev); 2073 2074 if (dev->added) 2075 usb_del_gadget_udc(&dev->gadget); /* Shuts everything down */ 2076 if (dev->driver) 2077 return -EBUSY; 2078 2079 gr_dfs_delete(dev); 2080 if (dev->desc_pool) 2081 dma_pool_destroy(dev->desc_pool); 2082 platform_set_drvdata(pdev, NULL); 2083 2084 gr_free_request(&dev->epi[0].ep, &dev->ep0reqi->req); 2085 gr_free_request(&dev->epo[0].ep, &dev->ep0reqo->req); 2086 2087 return 0; 2088 } 2089 static int gr_request_irq(struct gr_udc *dev, int irq) 2090 { 2091 return devm_request_threaded_irq(dev->dev, irq, gr_irq, gr_irq_handler, 2092 IRQF_SHARED, driver_name, dev); 2093 } 2094 2095 static int gr_probe(struct platform_device *pdev) 2096 { 2097 struct gr_udc *dev; 2098 struct resource *res; 2099 struct gr_regs __iomem *regs; 2100 int retval; 2101 u32 status; 2102 2103 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 2104 if (!dev) 2105 return -ENOMEM; 2106 dev->dev = &pdev->dev; 2107 2108 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2109 regs = devm_ioremap_resource(dev->dev, res); 2110 if (IS_ERR(regs)) 2111 return PTR_ERR(regs); 2112 2113 dev->irq = platform_get_irq(pdev, 0); 2114 if (dev->irq <= 0) { 2115 dev_err(dev->dev, "No irq found\n"); 2116 return -ENODEV; 2117 } 2118 2119 /* Some core configurations has separate irqs for IN and OUT events */ 2120 dev->irqi = platform_get_irq(pdev, 1); 2121 if (dev->irqi > 0) { 2122 dev->irqo = platform_get_irq(pdev, 2); 2123 if (dev->irqo <= 0) { 2124 dev_err(dev->dev, "Found irqi but not irqo\n"); 2125 return -ENODEV; 2126 } 2127 } else { 2128 dev->irqi = 0; 2129 } 2130 2131 dev->gadget.name = driver_name; 2132 dev->gadget.max_speed = USB_SPEED_HIGH; 2133 dev->gadget.ops = &gr_ops; 2134 dev->gadget.quirk_ep_out_aligned_size = true; 2135 2136 spin_lock_init(&dev->lock); 2137 dev->regs = regs; 2138 2139 platform_set_drvdata(pdev, dev); 2140 2141 /* Determine number of endpoints and data interface mode */ 2142 status = gr_read32(&dev->regs->status); 2143 dev->nepi = ((status & GR_STATUS_NEPI_MASK) >> GR_STATUS_NEPI_POS) + 1; 2144 dev->nepo = ((status & GR_STATUS_NEPO_MASK) >> GR_STATUS_NEPO_POS) + 1; 2145 2146 if (!(status & GR_STATUS_DM)) { 2147 dev_err(dev->dev, "Slave mode cores are not supported\n"); 2148 return -ENODEV; 2149 } 2150 2151 /* --- Effects of the following calls might need explicit cleanup --- */ 2152 2153 /* Create DMA pool for descriptors */ 2154 dev->desc_pool = dma_pool_create("desc_pool", dev->dev, 2155 sizeof(struct gr_dma_desc), 4, 0); 2156 if (!dev->desc_pool) { 2157 dev_err(dev->dev, "Could not allocate DMA pool"); 2158 return -ENOMEM; 2159 } 2160 2161 spin_lock(&dev->lock); 2162 2163 /* Inside lock so that no gadget can use this udc until probe is done */ 2164 retval = usb_add_gadget_udc(dev->dev, &dev->gadget); 2165 if (retval) { 2166 dev_err(dev->dev, "Could not add gadget udc"); 2167 goto out; 2168 } 2169 dev->added = 1; 2170 2171 retval = gr_udc_init(dev); 2172 if (retval) 2173 goto out; 2174 2175 gr_dfs_create(dev); 2176 2177 /* Clear all interrupt enables that might be left on since last boot */ 2178 gr_disable_interrupts_and_pullup(dev); 2179 2180 retval = gr_request_irq(dev, dev->irq); 2181 if (retval) { 2182 dev_err(dev->dev, "Failed to request irq %d\n", dev->irq); 2183 goto out; 2184 } 2185 2186 if (dev->irqi) { 2187 retval = gr_request_irq(dev, dev->irqi); 2188 if (retval) { 2189 dev_err(dev->dev, "Failed to request irqi %d\n", 2190 dev->irqi); 2191 goto out; 2192 } 2193 retval = gr_request_irq(dev, dev->irqo); 2194 if (retval) { 2195 dev_err(dev->dev, "Failed to request irqo %d\n", 2196 dev->irqo); 2197 goto out; 2198 } 2199 } 2200 2201 if (dev->irqi) 2202 dev_info(dev->dev, "regs: %p, irqs %d, %d, %d\n", dev->regs, 2203 dev->irq, dev->irqi, dev->irqo); 2204 else 2205 dev_info(dev->dev, "regs: %p, irq %d\n", dev->regs, dev->irq); 2206 2207 out: 2208 spin_unlock(&dev->lock); 2209 2210 if (retval) 2211 gr_remove(pdev); 2212 2213 return retval; 2214 } 2215 2216 static const struct of_device_id gr_match[] = { 2217 {.name = "GAISLER_USBDC"}, 2218 {.name = "01_021"}, 2219 {}, 2220 }; 2221 MODULE_DEVICE_TABLE(of, gr_match); 2222 2223 static struct platform_driver gr_driver = { 2224 .driver = { 2225 .name = DRIVER_NAME, 2226 .owner = THIS_MODULE, 2227 .of_match_table = gr_match, 2228 }, 2229 .probe = gr_probe, 2230 .remove = gr_remove, 2231 }; 2232 module_platform_driver(gr_driver); 2233 2234 MODULE_AUTHOR("Aeroflex Gaisler AB."); 2235 MODULE_DESCRIPTION(DRIVER_DESC); 2236 MODULE_LICENSE("GPL"); 2237