1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * udc.c - ChipIdea UDC driver 4 * 5 * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved. 6 * 7 * Author: David Lopo 8 */ 9 10 #include <linux/delay.h> 11 #include <linux/device.h> 12 #include <linux/dmapool.h> 13 #include <linux/err.h> 14 #include <linux/irqreturn.h> 15 #include <linux/kernel.h> 16 #include <linux/slab.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/pinctrl/consumer.h> 19 #include <linux/usb/ch9.h> 20 #include <linux/usb/gadget.h> 21 #include <linux/usb/otg-fsm.h> 22 #include <linux/usb/chipidea.h> 23 24 #include "ci.h" 25 #include "udc.h" 26 #include "bits.h" 27 #include "otg.h" 28 #include "otg_fsm.h" 29 30 /* control endpoint description */ 31 static const struct usb_endpoint_descriptor 32 ctrl_endpt_out_desc = { 33 .bLength = USB_DT_ENDPOINT_SIZE, 34 .bDescriptorType = USB_DT_ENDPOINT, 35 36 .bEndpointAddress = USB_DIR_OUT, 37 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 38 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), 39 }; 40 41 static const struct usb_endpoint_descriptor 42 ctrl_endpt_in_desc = { 43 .bLength = USB_DT_ENDPOINT_SIZE, 44 .bDescriptorType = USB_DT_ENDPOINT, 45 46 .bEndpointAddress = USB_DIR_IN, 47 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 48 .wMaxPacketSize = cpu_to_le16(CTRL_PAYLOAD_MAX), 49 }; 50 51 /** 52 * hw_ep_bit: calculates the bit number 53 * @num: endpoint number 54 * @dir: endpoint direction 55 * 56 * This function returns bit number 57 */ 58 static inline int hw_ep_bit(int num, int dir) 59 { 60 return num + ((dir == TX) ? 16 : 0); 61 } 62 63 static inline int ep_to_bit(struct ci_hdrc *ci, int n) 64 { 65 int fill = 16 - ci->hw_ep_max / 2; 66 67 if (n >= ci->hw_ep_max / 2) 68 n += fill; 69 70 return n; 71 } 72 73 /** 74 * hw_device_state: enables/disables interrupts (execute without interruption) 75 * @ci: the controller 76 * @dma: 0 => disable, !0 => enable and set dma engine 77 * 78 * This function returns an error code 79 */ 80 static int hw_device_state(struct ci_hdrc *ci, u32 dma) 81 { 82 if (dma) { 83 hw_write(ci, OP_ENDPTLISTADDR, ~0, dma); 84 /* interrupt, error, port change, reset, sleep/suspend */ 85 hw_write(ci, OP_USBINTR, ~0, 86 USBi_UI|USBi_UEI|USBi_PCI|USBi_URI|USBi_SLI); 87 } else { 88 hw_write(ci, OP_USBINTR, ~0, 0); 89 } 90 return 0; 91 } 92 93 /** 94 * hw_ep_flush: flush endpoint fifo (execute without interruption) 95 * @ci: the controller 96 * @num: endpoint number 97 * @dir: endpoint direction 98 * 99 * This function returns an error code 100 */ 101 static int hw_ep_flush(struct ci_hdrc *ci, int num, int dir) 102 { 103 int n = hw_ep_bit(num, dir); 104 105 do { 106 /* flush any pending transfer */ 107 hw_write(ci, OP_ENDPTFLUSH, ~0, BIT(n)); 108 while (hw_read(ci, OP_ENDPTFLUSH, BIT(n))) 109 cpu_relax(); 110 } while (hw_read(ci, OP_ENDPTSTAT, BIT(n))); 111 112 return 0; 113 } 114 115 /** 116 * hw_ep_disable: disables endpoint (execute without interruption) 117 * @ci: the controller 118 * @num: endpoint number 119 * @dir: endpoint direction 120 * 121 * This function returns an error code 122 */ 123 static int hw_ep_disable(struct ci_hdrc *ci, int num, int dir) 124 { 125 hw_write(ci, OP_ENDPTCTRL + num, 126 (dir == TX) ? ENDPTCTRL_TXE : ENDPTCTRL_RXE, 0); 127 return 0; 128 } 129 130 /** 131 * hw_ep_enable: enables endpoint (execute without interruption) 132 * @ci: the controller 133 * @num: endpoint number 134 * @dir: endpoint direction 135 * @type: endpoint type 136 * 137 * This function returns an error code 138 */ 139 static int hw_ep_enable(struct ci_hdrc *ci, int num, int dir, int type) 140 { 141 u32 mask, data; 142 143 if (dir == TX) { 144 mask = ENDPTCTRL_TXT; /* type */ 145 data = type << __ffs(mask); 146 147 mask |= ENDPTCTRL_TXS; /* unstall */ 148 mask |= ENDPTCTRL_TXR; /* reset data toggle */ 149 data |= ENDPTCTRL_TXR; 150 mask |= ENDPTCTRL_TXE; /* enable */ 151 data |= ENDPTCTRL_TXE; 152 } else { 153 mask = ENDPTCTRL_RXT; /* type */ 154 data = type << __ffs(mask); 155 156 mask |= ENDPTCTRL_RXS; /* unstall */ 157 mask |= ENDPTCTRL_RXR; /* reset data toggle */ 158 data |= ENDPTCTRL_RXR; 159 mask |= ENDPTCTRL_RXE; /* enable */ 160 data |= ENDPTCTRL_RXE; 161 } 162 hw_write(ci, OP_ENDPTCTRL + num, mask, data); 163 return 0; 164 } 165 166 /** 167 * hw_ep_get_halt: return endpoint halt status 168 * @ci: the controller 169 * @num: endpoint number 170 * @dir: endpoint direction 171 * 172 * This function returns 1 if endpoint halted 173 */ 174 static int hw_ep_get_halt(struct ci_hdrc *ci, int num, int dir) 175 { 176 u32 mask = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS; 177 178 return hw_read(ci, OP_ENDPTCTRL + num, mask) ? 1 : 0; 179 } 180 181 /** 182 * hw_ep_prime: primes endpoint (execute without interruption) 183 * @ci: the controller 184 * @num: endpoint number 185 * @dir: endpoint direction 186 * @is_ctrl: true if control endpoint 187 * 188 * This function returns an error code 189 */ 190 static int hw_ep_prime(struct ci_hdrc *ci, int num, int dir, int is_ctrl) 191 { 192 int n = hw_ep_bit(num, dir); 193 194 /* Synchronize before ep prime */ 195 wmb(); 196 197 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num))) 198 return -EAGAIN; 199 200 hw_write(ci, OP_ENDPTPRIME, ~0, BIT(n)); 201 202 while (hw_read(ci, OP_ENDPTPRIME, BIT(n))) 203 cpu_relax(); 204 if (is_ctrl && dir == RX && hw_read(ci, OP_ENDPTSETUPSTAT, BIT(num))) 205 return -EAGAIN; 206 207 /* status shoult be tested according with manual but it doesn't work */ 208 return 0; 209 } 210 211 /** 212 * hw_ep_set_halt: configures ep halt & resets data toggle after clear (execute 213 * without interruption) 214 * @ci: the controller 215 * @num: endpoint number 216 * @dir: endpoint direction 217 * @value: true => stall, false => unstall 218 * 219 * This function returns an error code 220 */ 221 static int hw_ep_set_halt(struct ci_hdrc *ci, int num, int dir, int value) 222 { 223 if (value != 0 && value != 1) 224 return -EINVAL; 225 226 do { 227 enum ci_hw_regs reg = OP_ENDPTCTRL + num; 228 u32 mask_xs = (dir == TX) ? ENDPTCTRL_TXS : ENDPTCTRL_RXS; 229 u32 mask_xr = (dir == TX) ? ENDPTCTRL_TXR : ENDPTCTRL_RXR; 230 231 /* data toggle - reserved for EP0 but it's in ESS */ 232 hw_write(ci, reg, mask_xs|mask_xr, 233 value ? mask_xs : mask_xr); 234 } while (value != hw_ep_get_halt(ci, num, dir)); 235 236 return 0; 237 } 238 239 /** 240 * hw_is_port_high_speed: test if port is high speed 241 * 242 * This function returns true if high speed port 243 */ 244 static int hw_port_is_high_speed(struct ci_hdrc *ci) 245 { 246 return ci->hw_bank.lpm ? hw_read(ci, OP_DEVLC, DEVLC_PSPD) : 247 hw_read(ci, OP_PORTSC, PORTSC_HSP); 248 } 249 250 /** 251 * hw_test_and_clear_complete: test & clear complete status (execute without 252 * interruption) 253 * @ci: the controller 254 * @n: endpoint number 255 * 256 * This function returns complete status 257 */ 258 static int hw_test_and_clear_complete(struct ci_hdrc *ci, int n) 259 { 260 n = ep_to_bit(ci, n); 261 return hw_test_and_clear(ci, OP_ENDPTCOMPLETE, BIT(n)); 262 } 263 264 /** 265 * hw_test_and_clear_intr_active: test & clear active interrupts (execute 266 * without interruption) 267 * 268 * This function returns active interrutps 269 */ 270 static u32 hw_test_and_clear_intr_active(struct ci_hdrc *ci) 271 { 272 u32 reg = hw_read_intr_status(ci) & hw_read_intr_enable(ci); 273 274 hw_write(ci, OP_USBSTS, ~0, reg); 275 return reg; 276 } 277 278 /** 279 * hw_test_and_clear_setup_guard: test & clear setup guard (execute without 280 * interruption) 281 * 282 * This function returns guard value 283 */ 284 static int hw_test_and_clear_setup_guard(struct ci_hdrc *ci) 285 { 286 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, 0); 287 } 288 289 /** 290 * hw_test_and_set_setup_guard: test & set setup guard (execute without 291 * interruption) 292 * 293 * This function returns guard value 294 */ 295 static int hw_test_and_set_setup_guard(struct ci_hdrc *ci) 296 { 297 return hw_test_and_write(ci, OP_USBCMD, USBCMD_SUTW, USBCMD_SUTW); 298 } 299 300 /** 301 * hw_usb_set_address: configures USB address (execute without interruption) 302 * @ci: the controller 303 * @value: new USB address 304 * 305 * This function explicitly sets the address, without the "USBADRA" (advance) 306 * feature, which is not supported by older versions of the controller. 307 */ 308 static void hw_usb_set_address(struct ci_hdrc *ci, u8 value) 309 { 310 hw_write(ci, OP_DEVICEADDR, DEVICEADDR_USBADR, 311 value << __ffs(DEVICEADDR_USBADR)); 312 } 313 314 /** 315 * hw_usb_reset: restart device after a bus reset (execute without 316 * interruption) 317 * 318 * This function returns an error code 319 */ 320 static int hw_usb_reset(struct ci_hdrc *ci) 321 { 322 hw_usb_set_address(ci, 0); 323 324 /* ESS flushes only at end?!? */ 325 hw_write(ci, OP_ENDPTFLUSH, ~0, ~0); 326 327 /* clear setup token semaphores */ 328 hw_write(ci, OP_ENDPTSETUPSTAT, 0, 0); 329 330 /* clear complete status */ 331 hw_write(ci, OP_ENDPTCOMPLETE, 0, 0); 332 333 /* wait until all bits cleared */ 334 while (hw_read(ci, OP_ENDPTPRIME, ~0)) 335 udelay(10); /* not RTOS friendly */ 336 337 /* reset all endpoints ? */ 338 339 /* reset internal status and wait for further instructions 340 no need to verify the port reset status (ESS does it) */ 341 342 return 0; 343 } 344 345 /****************************************************************************** 346 * UTIL block 347 *****************************************************************************/ 348 349 static int add_td_to_list(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq, 350 unsigned int length, struct scatterlist *s) 351 { 352 int i; 353 u32 temp; 354 struct td_node *lastnode, *node = kzalloc(sizeof(struct td_node), 355 GFP_ATOMIC); 356 357 if (node == NULL) 358 return -ENOMEM; 359 360 node->ptr = dma_pool_zalloc(hwep->td_pool, GFP_ATOMIC, &node->dma); 361 if (node->ptr == NULL) { 362 kfree(node); 363 return -ENOMEM; 364 } 365 366 node->ptr->token = cpu_to_le32(length << __ffs(TD_TOTAL_BYTES)); 367 node->ptr->token &= cpu_to_le32(TD_TOTAL_BYTES); 368 node->ptr->token |= cpu_to_le32(TD_STATUS_ACTIVE); 369 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) { 370 u32 mul = hwreq->req.length / hwep->ep.maxpacket; 371 372 if (hwreq->req.length == 0 373 || hwreq->req.length % hwep->ep.maxpacket) 374 mul++; 375 node->ptr->token |= cpu_to_le32(mul << __ffs(TD_MULTO)); 376 } 377 378 if (s) { 379 temp = (u32) (sg_dma_address(s) + hwreq->req.actual); 380 node->td_remaining_size = CI_MAX_BUF_SIZE - length; 381 } else { 382 temp = (u32) (hwreq->req.dma + hwreq->req.actual); 383 } 384 385 if (length) { 386 node->ptr->page[0] = cpu_to_le32(temp); 387 for (i = 1; i < TD_PAGE_COUNT; i++) { 388 u32 page = temp + i * CI_HDRC_PAGE_SIZE; 389 page &= ~TD_RESERVED_MASK; 390 node->ptr->page[i] = cpu_to_le32(page); 391 } 392 } 393 394 hwreq->req.actual += length; 395 396 if (!list_empty(&hwreq->tds)) { 397 /* get the last entry */ 398 lastnode = list_entry(hwreq->tds.prev, 399 struct td_node, td); 400 lastnode->ptr->next = cpu_to_le32(node->dma); 401 } 402 403 INIT_LIST_HEAD(&node->td); 404 list_add_tail(&node->td, &hwreq->tds); 405 406 return 0; 407 } 408 409 /** 410 * _usb_addr: calculates endpoint address from direction & number 411 * @ep: endpoint 412 */ 413 static inline u8 _usb_addr(struct ci_hw_ep *ep) 414 { 415 return ((ep->dir == TX) ? USB_ENDPOINT_DIR_MASK : 0) | ep->num; 416 } 417 418 static int prepare_td_for_non_sg(struct ci_hw_ep *hwep, 419 struct ci_hw_req *hwreq) 420 { 421 unsigned int rest = hwreq->req.length; 422 int pages = TD_PAGE_COUNT; 423 int ret = 0; 424 425 if (rest == 0) { 426 ret = add_td_to_list(hwep, hwreq, 0, NULL); 427 if (ret < 0) 428 return ret; 429 } 430 431 /* 432 * The first buffer could be not page aligned. 433 * In that case we have to span into one extra td. 434 */ 435 if (hwreq->req.dma % PAGE_SIZE) 436 pages--; 437 438 while (rest > 0) { 439 unsigned int count = min(hwreq->req.length - hwreq->req.actual, 440 (unsigned int)(pages * CI_HDRC_PAGE_SIZE)); 441 442 ret = add_td_to_list(hwep, hwreq, count, NULL); 443 if (ret < 0) 444 return ret; 445 446 rest -= count; 447 } 448 449 if (hwreq->req.zero && hwreq->req.length && hwep->dir == TX 450 && (hwreq->req.length % hwep->ep.maxpacket == 0)) { 451 ret = add_td_to_list(hwep, hwreq, 0, NULL); 452 if (ret < 0) 453 return ret; 454 } 455 456 return ret; 457 } 458 459 static int prepare_td_per_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq, 460 struct scatterlist *s) 461 { 462 unsigned int rest = sg_dma_len(s); 463 int ret = 0; 464 465 hwreq->req.actual = 0; 466 while (rest > 0) { 467 unsigned int count = min_t(unsigned int, rest, 468 CI_MAX_BUF_SIZE); 469 470 ret = add_td_to_list(hwep, hwreq, count, s); 471 if (ret < 0) 472 return ret; 473 474 rest -= count; 475 } 476 477 return ret; 478 } 479 480 static void ci_add_buffer_entry(struct td_node *node, struct scatterlist *s) 481 { 482 int empty_td_slot_index = (CI_MAX_BUF_SIZE - node->td_remaining_size) 483 / CI_HDRC_PAGE_SIZE; 484 int i; 485 486 node->ptr->token += 487 cpu_to_le32(sg_dma_len(s) << __ffs(TD_TOTAL_BYTES)); 488 489 for (i = empty_td_slot_index; i < TD_PAGE_COUNT; i++) { 490 u32 page = (u32) sg_dma_address(s) + 491 (i - empty_td_slot_index) * CI_HDRC_PAGE_SIZE; 492 493 page &= ~TD_RESERVED_MASK; 494 node->ptr->page[i] = cpu_to_le32(page); 495 } 496 } 497 498 static int prepare_td_for_sg(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq) 499 { 500 struct usb_request *req = &hwreq->req; 501 struct scatterlist *s = req->sg; 502 int ret = 0, i = 0; 503 struct td_node *node = NULL; 504 505 if (!s || req->zero || req->length == 0) { 506 dev_err(hwep->ci->dev, "not supported operation for sg\n"); 507 return -EINVAL; 508 } 509 510 while (i++ < req->num_mapped_sgs) { 511 if (sg_dma_address(s) % PAGE_SIZE) { 512 dev_err(hwep->ci->dev, "not page aligned sg buffer\n"); 513 return -EINVAL; 514 } 515 516 if (node && (node->td_remaining_size >= sg_dma_len(s))) { 517 ci_add_buffer_entry(node, s); 518 node->td_remaining_size -= sg_dma_len(s); 519 } else { 520 ret = prepare_td_per_sg(hwep, hwreq, s); 521 if (ret) 522 return ret; 523 524 node = list_entry(hwreq->tds.prev, 525 struct td_node, td); 526 } 527 528 s = sg_next(s); 529 } 530 531 return ret; 532 } 533 534 /** 535 * _hardware_enqueue: configures a request at hardware level 536 * @hwep: endpoint 537 * @hwreq: request 538 * 539 * This function returns an error code 540 */ 541 static int _hardware_enqueue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq) 542 { 543 struct ci_hdrc *ci = hwep->ci; 544 int ret = 0; 545 struct td_node *firstnode, *lastnode; 546 547 /* don't queue twice */ 548 if (hwreq->req.status == -EALREADY) 549 return -EALREADY; 550 551 hwreq->req.status = -EALREADY; 552 553 ret = usb_gadget_map_request_by_dev(ci->dev->parent, 554 &hwreq->req, hwep->dir); 555 if (ret) 556 return ret; 557 558 if (hwreq->req.num_mapped_sgs) 559 ret = prepare_td_for_sg(hwep, hwreq); 560 else 561 ret = prepare_td_for_non_sg(hwep, hwreq); 562 563 if (ret) 564 return ret; 565 566 firstnode = list_first_entry(&hwreq->tds, struct td_node, td); 567 568 lastnode = list_entry(hwreq->tds.prev, 569 struct td_node, td); 570 571 lastnode->ptr->next = cpu_to_le32(TD_TERMINATE); 572 if (!hwreq->req.no_interrupt) 573 lastnode->ptr->token |= cpu_to_le32(TD_IOC); 574 wmb(); 575 576 hwreq->req.actual = 0; 577 if (!list_empty(&hwep->qh.queue)) { 578 struct ci_hw_req *hwreqprev; 579 int n = hw_ep_bit(hwep->num, hwep->dir); 580 int tmp_stat; 581 struct td_node *prevlastnode; 582 u32 next = firstnode->dma & TD_ADDR_MASK; 583 584 hwreqprev = list_entry(hwep->qh.queue.prev, 585 struct ci_hw_req, queue); 586 prevlastnode = list_entry(hwreqprev->tds.prev, 587 struct td_node, td); 588 589 prevlastnode->ptr->next = cpu_to_le32(next); 590 wmb(); 591 if (hw_read(ci, OP_ENDPTPRIME, BIT(n))) 592 goto done; 593 do { 594 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, USBCMD_ATDTW); 595 tmp_stat = hw_read(ci, OP_ENDPTSTAT, BIT(n)); 596 } while (!hw_read(ci, OP_USBCMD, USBCMD_ATDTW)); 597 hw_write(ci, OP_USBCMD, USBCMD_ATDTW, 0); 598 if (tmp_stat) 599 goto done; 600 } 601 602 /* QH configuration */ 603 hwep->qh.ptr->td.next = cpu_to_le32(firstnode->dma); 604 hwep->qh.ptr->td.token &= 605 cpu_to_le32(~(TD_STATUS_HALTED|TD_STATUS_ACTIVE)); 606 607 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == RX) { 608 u32 mul = hwreq->req.length / hwep->ep.maxpacket; 609 610 if (hwreq->req.length == 0 611 || hwreq->req.length % hwep->ep.maxpacket) 612 mul++; 613 hwep->qh.ptr->cap |= cpu_to_le32(mul << __ffs(QH_MULT)); 614 } 615 616 ret = hw_ep_prime(ci, hwep->num, hwep->dir, 617 hwep->type == USB_ENDPOINT_XFER_CONTROL); 618 done: 619 return ret; 620 } 621 622 /** 623 * free_pending_td: remove a pending request for the endpoint 624 * @ci: the controller 625 * @hwep: endpoint 626 */ 627 static void free_pending_td(struct ci_hw_ep *hwep) 628 { 629 struct td_node *pending = hwep->pending_td; 630 631 dma_pool_free(hwep->td_pool, pending->ptr, pending->dma); 632 hwep->pending_td = NULL; 633 kfree(pending); 634 } 635 636 static int reprime_dtd(struct ci_hdrc *ci, struct ci_hw_ep *hwep, 637 struct td_node *node) 638 { 639 hwep->qh.ptr->td.next = cpu_to_le32(node->dma); 640 hwep->qh.ptr->td.token &= 641 cpu_to_le32(~(TD_STATUS_HALTED | TD_STATUS_ACTIVE)); 642 643 return hw_ep_prime(ci, hwep->num, hwep->dir, 644 hwep->type == USB_ENDPOINT_XFER_CONTROL); 645 } 646 647 /** 648 * _hardware_dequeue: handles a request at hardware level 649 * @gadget: gadget 650 * @hwep: endpoint 651 * 652 * This function returns an error code 653 */ 654 static int _hardware_dequeue(struct ci_hw_ep *hwep, struct ci_hw_req *hwreq) 655 { 656 u32 tmptoken; 657 struct td_node *node, *tmpnode; 658 unsigned remaining_length; 659 unsigned actual = hwreq->req.length; 660 struct ci_hdrc *ci = hwep->ci; 661 662 if (hwreq->req.status != -EALREADY) 663 return -EINVAL; 664 665 hwreq->req.status = 0; 666 667 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) { 668 tmptoken = le32_to_cpu(node->ptr->token); 669 if ((TD_STATUS_ACTIVE & tmptoken) != 0) { 670 int n = hw_ep_bit(hwep->num, hwep->dir); 671 672 if (ci->rev == CI_REVISION_24) 673 if (!hw_read(ci, OP_ENDPTSTAT, BIT(n))) 674 reprime_dtd(ci, hwep, node); 675 hwreq->req.status = -EALREADY; 676 return -EBUSY; 677 } 678 679 remaining_length = (tmptoken & TD_TOTAL_BYTES); 680 remaining_length >>= __ffs(TD_TOTAL_BYTES); 681 actual -= remaining_length; 682 683 hwreq->req.status = tmptoken & TD_STATUS; 684 if ((TD_STATUS_HALTED & hwreq->req.status)) { 685 hwreq->req.status = -EPIPE; 686 break; 687 } else if ((TD_STATUS_DT_ERR & hwreq->req.status)) { 688 hwreq->req.status = -EPROTO; 689 break; 690 } else if ((TD_STATUS_TR_ERR & hwreq->req.status)) { 691 hwreq->req.status = -EILSEQ; 692 break; 693 } 694 695 if (remaining_length) { 696 if (hwep->dir == TX) { 697 hwreq->req.status = -EPROTO; 698 break; 699 } 700 } 701 /* 702 * As the hardware could still address the freed td 703 * which will run the udc unusable, the cleanup of the 704 * td has to be delayed by one. 705 */ 706 if (hwep->pending_td) 707 free_pending_td(hwep); 708 709 hwep->pending_td = node; 710 list_del_init(&node->td); 711 } 712 713 usb_gadget_unmap_request_by_dev(hwep->ci->dev->parent, 714 &hwreq->req, hwep->dir); 715 716 hwreq->req.actual += actual; 717 718 if (hwreq->req.status) 719 return hwreq->req.status; 720 721 return hwreq->req.actual; 722 } 723 724 /** 725 * _ep_nuke: dequeues all endpoint requests 726 * @hwep: endpoint 727 * 728 * This function returns an error code 729 * Caller must hold lock 730 */ 731 static int _ep_nuke(struct ci_hw_ep *hwep) 732 __releases(hwep->lock) 733 __acquires(hwep->lock) 734 { 735 struct td_node *node, *tmpnode; 736 if (hwep == NULL) 737 return -EINVAL; 738 739 hw_ep_flush(hwep->ci, hwep->num, hwep->dir); 740 741 while (!list_empty(&hwep->qh.queue)) { 742 743 /* pop oldest request */ 744 struct ci_hw_req *hwreq = list_entry(hwep->qh.queue.next, 745 struct ci_hw_req, queue); 746 747 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) { 748 dma_pool_free(hwep->td_pool, node->ptr, node->dma); 749 list_del_init(&node->td); 750 node->ptr = NULL; 751 kfree(node); 752 } 753 754 list_del_init(&hwreq->queue); 755 hwreq->req.status = -ESHUTDOWN; 756 757 if (hwreq->req.complete != NULL) { 758 spin_unlock(hwep->lock); 759 usb_gadget_giveback_request(&hwep->ep, &hwreq->req); 760 spin_lock(hwep->lock); 761 } 762 } 763 764 if (hwep->pending_td) 765 free_pending_td(hwep); 766 767 return 0; 768 } 769 770 static int _ep_set_halt(struct usb_ep *ep, int value, bool check_transfer) 771 { 772 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 773 int direction, retval = 0; 774 unsigned long flags; 775 776 if (ep == NULL || hwep->ep.desc == NULL) 777 return -EINVAL; 778 779 if (usb_endpoint_xfer_isoc(hwep->ep.desc)) 780 return -EOPNOTSUPP; 781 782 spin_lock_irqsave(hwep->lock, flags); 783 784 if (value && hwep->dir == TX && check_transfer && 785 !list_empty(&hwep->qh.queue) && 786 !usb_endpoint_xfer_control(hwep->ep.desc)) { 787 spin_unlock_irqrestore(hwep->lock, flags); 788 return -EAGAIN; 789 } 790 791 direction = hwep->dir; 792 do { 793 retval |= hw_ep_set_halt(hwep->ci, hwep->num, hwep->dir, value); 794 795 if (!value) 796 hwep->wedge = 0; 797 798 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) 799 hwep->dir = (hwep->dir == TX) ? RX : TX; 800 801 } while (hwep->dir != direction); 802 803 spin_unlock_irqrestore(hwep->lock, flags); 804 return retval; 805 } 806 807 808 /** 809 * _gadget_stop_activity: stops all USB activity, flushes & disables all endpts 810 * @gadget: gadget 811 * 812 * This function returns an error code 813 */ 814 static int _gadget_stop_activity(struct usb_gadget *gadget) 815 { 816 struct usb_ep *ep; 817 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget); 818 unsigned long flags; 819 820 /* flush all endpoints */ 821 gadget_for_each_ep(ep, gadget) { 822 usb_ep_fifo_flush(ep); 823 } 824 usb_ep_fifo_flush(&ci->ep0out->ep); 825 usb_ep_fifo_flush(&ci->ep0in->ep); 826 827 /* make sure to disable all endpoints */ 828 gadget_for_each_ep(ep, gadget) { 829 usb_ep_disable(ep); 830 } 831 832 if (ci->status != NULL) { 833 usb_ep_free_request(&ci->ep0in->ep, ci->status); 834 ci->status = NULL; 835 } 836 837 spin_lock_irqsave(&ci->lock, flags); 838 ci->gadget.speed = USB_SPEED_UNKNOWN; 839 ci->remote_wakeup = 0; 840 ci->suspended = 0; 841 spin_unlock_irqrestore(&ci->lock, flags); 842 843 return 0; 844 } 845 846 /****************************************************************************** 847 * ISR block 848 *****************************************************************************/ 849 /** 850 * isr_reset_handler: USB reset interrupt handler 851 * @ci: UDC device 852 * 853 * This function resets USB engine after a bus reset occurred 854 */ 855 static void isr_reset_handler(struct ci_hdrc *ci) 856 __releases(ci->lock) 857 __acquires(ci->lock) 858 { 859 int retval; 860 861 spin_unlock(&ci->lock); 862 if (ci->gadget.speed != USB_SPEED_UNKNOWN) 863 usb_gadget_udc_reset(&ci->gadget, ci->driver); 864 865 retval = _gadget_stop_activity(&ci->gadget); 866 if (retval) 867 goto done; 868 869 retval = hw_usb_reset(ci); 870 if (retval) 871 goto done; 872 873 ci->status = usb_ep_alloc_request(&ci->ep0in->ep, GFP_ATOMIC); 874 if (ci->status == NULL) 875 retval = -ENOMEM; 876 877 done: 878 spin_lock(&ci->lock); 879 880 if (retval) 881 dev_err(ci->dev, "error: %i\n", retval); 882 } 883 884 /** 885 * isr_get_status_complete: get_status request complete function 886 * @ep: endpoint 887 * @req: request handled 888 * 889 * Caller must release lock 890 */ 891 static void isr_get_status_complete(struct usb_ep *ep, struct usb_request *req) 892 { 893 if (ep == NULL || req == NULL) 894 return; 895 896 kfree(req->buf); 897 usb_ep_free_request(ep, req); 898 } 899 900 /** 901 * _ep_queue: queues (submits) an I/O request to an endpoint 902 * @ep: endpoint 903 * @req: request 904 * @gfp_flags: GFP flags (not used) 905 * 906 * Caller must hold lock 907 * This function returns an error code 908 */ 909 static int _ep_queue(struct usb_ep *ep, struct usb_request *req, 910 gfp_t __maybe_unused gfp_flags) 911 { 912 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 913 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req); 914 struct ci_hdrc *ci = hwep->ci; 915 int retval = 0; 916 917 if (ep == NULL || req == NULL || hwep->ep.desc == NULL) 918 return -EINVAL; 919 920 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) { 921 if (req->length) 922 hwep = (ci->ep0_dir == RX) ? 923 ci->ep0out : ci->ep0in; 924 if (!list_empty(&hwep->qh.queue)) { 925 _ep_nuke(hwep); 926 dev_warn(hwep->ci->dev, "endpoint ctrl %X nuked\n", 927 _usb_addr(hwep)); 928 } 929 } 930 931 if (usb_endpoint_xfer_isoc(hwep->ep.desc) && 932 hwreq->req.length > hwep->ep.mult * hwep->ep.maxpacket) { 933 dev_err(hwep->ci->dev, "request length too big for isochronous\n"); 934 return -EMSGSIZE; 935 } 936 937 /* first nuke then test link, e.g. previous status has not sent */ 938 if (!list_empty(&hwreq->queue)) { 939 dev_err(hwep->ci->dev, "request already in queue\n"); 940 return -EBUSY; 941 } 942 943 /* push request */ 944 hwreq->req.status = -EINPROGRESS; 945 hwreq->req.actual = 0; 946 947 retval = _hardware_enqueue(hwep, hwreq); 948 949 if (retval == -EALREADY) 950 retval = 0; 951 if (!retval) 952 list_add_tail(&hwreq->queue, &hwep->qh.queue); 953 954 return retval; 955 } 956 957 /** 958 * isr_get_status_response: get_status request response 959 * @ci: ci struct 960 * @setup: setup request packet 961 * 962 * This function returns an error code 963 */ 964 static int isr_get_status_response(struct ci_hdrc *ci, 965 struct usb_ctrlrequest *setup) 966 __releases(hwep->lock) 967 __acquires(hwep->lock) 968 { 969 struct ci_hw_ep *hwep = ci->ep0in; 970 struct usb_request *req = NULL; 971 gfp_t gfp_flags = GFP_ATOMIC; 972 int dir, num, retval; 973 974 if (hwep == NULL || setup == NULL) 975 return -EINVAL; 976 977 spin_unlock(hwep->lock); 978 req = usb_ep_alloc_request(&hwep->ep, gfp_flags); 979 spin_lock(hwep->lock); 980 if (req == NULL) 981 return -ENOMEM; 982 983 req->complete = isr_get_status_complete; 984 req->length = 2; 985 req->buf = kzalloc(req->length, gfp_flags); 986 if (req->buf == NULL) { 987 retval = -ENOMEM; 988 goto err_free_req; 989 } 990 991 if ((setup->bRequestType & USB_RECIP_MASK) == USB_RECIP_DEVICE) { 992 *(u16 *)req->buf = (ci->remote_wakeup << 1) | 993 ci->gadget.is_selfpowered; 994 } else if ((setup->bRequestType & USB_RECIP_MASK) \ 995 == USB_RECIP_ENDPOINT) { 996 dir = (le16_to_cpu(setup->wIndex) & USB_ENDPOINT_DIR_MASK) ? 997 TX : RX; 998 num = le16_to_cpu(setup->wIndex) & USB_ENDPOINT_NUMBER_MASK; 999 *(u16 *)req->buf = hw_ep_get_halt(ci, num, dir); 1000 } 1001 /* else do nothing; reserved for future use */ 1002 1003 retval = _ep_queue(&hwep->ep, req, gfp_flags); 1004 if (retval) 1005 goto err_free_buf; 1006 1007 return 0; 1008 1009 err_free_buf: 1010 kfree(req->buf); 1011 err_free_req: 1012 spin_unlock(hwep->lock); 1013 usb_ep_free_request(&hwep->ep, req); 1014 spin_lock(hwep->lock); 1015 return retval; 1016 } 1017 1018 /** 1019 * isr_setup_status_complete: setup_status request complete function 1020 * @ep: endpoint 1021 * @req: request handled 1022 * 1023 * Caller must release lock. Put the port in test mode if test mode 1024 * feature is selected. 1025 */ 1026 static void 1027 isr_setup_status_complete(struct usb_ep *ep, struct usb_request *req) 1028 { 1029 struct ci_hdrc *ci = req->context; 1030 unsigned long flags; 1031 1032 if (ci->setaddr) { 1033 hw_usb_set_address(ci, ci->address); 1034 ci->setaddr = false; 1035 if (ci->address) 1036 usb_gadget_set_state(&ci->gadget, USB_STATE_ADDRESS); 1037 } 1038 1039 spin_lock_irqsave(&ci->lock, flags); 1040 if (ci->test_mode) 1041 hw_port_test_set(ci, ci->test_mode); 1042 spin_unlock_irqrestore(&ci->lock, flags); 1043 } 1044 1045 /** 1046 * isr_setup_status_phase: queues the status phase of a setup transation 1047 * @ci: ci struct 1048 * 1049 * This function returns an error code 1050 */ 1051 static int isr_setup_status_phase(struct ci_hdrc *ci) 1052 { 1053 struct ci_hw_ep *hwep; 1054 1055 /* 1056 * Unexpected USB controller behavior, caused by bad signal integrity 1057 * or ground reference problems, can lead to isr_setup_status_phase 1058 * being called with ci->status equal to NULL. 1059 * If this situation occurs, you should review your USB hardware design. 1060 */ 1061 if (WARN_ON_ONCE(!ci->status)) 1062 return -EPIPE; 1063 1064 hwep = (ci->ep0_dir == TX) ? ci->ep0out : ci->ep0in; 1065 ci->status->context = ci; 1066 ci->status->complete = isr_setup_status_complete; 1067 1068 return _ep_queue(&hwep->ep, ci->status, GFP_ATOMIC); 1069 } 1070 1071 /** 1072 * isr_tr_complete_low: transaction complete low level handler 1073 * @hwep: endpoint 1074 * 1075 * This function returns an error code 1076 * Caller must hold lock 1077 */ 1078 static int isr_tr_complete_low(struct ci_hw_ep *hwep) 1079 __releases(hwep->lock) 1080 __acquires(hwep->lock) 1081 { 1082 struct ci_hw_req *hwreq, *hwreqtemp; 1083 struct ci_hw_ep *hweptemp = hwep; 1084 int retval = 0; 1085 1086 list_for_each_entry_safe(hwreq, hwreqtemp, &hwep->qh.queue, 1087 queue) { 1088 retval = _hardware_dequeue(hwep, hwreq); 1089 if (retval < 0) 1090 break; 1091 list_del_init(&hwreq->queue); 1092 if (hwreq->req.complete != NULL) { 1093 spin_unlock(hwep->lock); 1094 if ((hwep->type == USB_ENDPOINT_XFER_CONTROL) && 1095 hwreq->req.length) 1096 hweptemp = hwep->ci->ep0in; 1097 usb_gadget_giveback_request(&hweptemp->ep, &hwreq->req); 1098 spin_lock(hwep->lock); 1099 } 1100 } 1101 1102 if (retval == -EBUSY) 1103 retval = 0; 1104 1105 return retval; 1106 } 1107 1108 static int otg_a_alt_hnp_support(struct ci_hdrc *ci) 1109 { 1110 dev_warn(&ci->gadget.dev, 1111 "connect the device to an alternate port if you want HNP\n"); 1112 return isr_setup_status_phase(ci); 1113 } 1114 1115 /** 1116 * isr_setup_packet_handler: setup packet handler 1117 * @ci: UDC descriptor 1118 * 1119 * This function handles setup packet 1120 */ 1121 static void isr_setup_packet_handler(struct ci_hdrc *ci) 1122 __releases(ci->lock) 1123 __acquires(ci->lock) 1124 { 1125 struct ci_hw_ep *hwep = &ci->ci_hw_ep[0]; 1126 struct usb_ctrlrequest req; 1127 int type, num, dir, err = -EINVAL; 1128 u8 tmode = 0; 1129 1130 /* 1131 * Flush data and handshake transactions of previous 1132 * setup packet. 1133 */ 1134 _ep_nuke(ci->ep0out); 1135 _ep_nuke(ci->ep0in); 1136 1137 /* read_setup_packet */ 1138 do { 1139 hw_test_and_set_setup_guard(ci); 1140 memcpy(&req, &hwep->qh.ptr->setup, sizeof(req)); 1141 } while (!hw_test_and_clear_setup_guard(ci)); 1142 1143 type = req.bRequestType; 1144 1145 ci->ep0_dir = (type & USB_DIR_IN) ? TX : RX; 1146 1147 switch (req.bRequest) { 1148 case USB_REQ_CLEAR_FEATURE: 1149 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) && 1150 le16_to_cpu(req.wValue) == 1151 USB_ENDPOINT_HALT) { 1152 if (req.wLength != 0) 1153 break; 1154 num = le16_to_cpu(req.wIndex); 1155 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX; 1156 num &= USB_ENDPOINT_NUMBER_MASK; 1157 if (dir == TX) 1158 num += ci->hw_ep_max / 2; 1159 if (!ci->ci_hw_ep[num].wedge) { 1160 spin_unlock(&ci->lock); 1161 err = usb_ep_clear_halt( 1162 &ci->ci_hw_ep[num].ep); 1163 spin_lock(&ci->lock); 1164 if (err) 1165 break; 1166 } 1167 err = isr_setup_status_phase(ci); 1168 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE) && 1169 le16_to_cpu(req.wValue) == 1170 USB_DEVICE_REMOTE_WAKEUP) { 1171 if (req.wLength != 0) 1172 break; 1173 ci->remote_wakeup = 0; 1174 err = isr_setup_status_phase(ci); 1175 } else { 1176 goto delegate; 1177 } 1178 break; 1179 case USB_REQ_GET_STATUS: 1180 if ((type != (USB_DIR_IN|USB_RECIP_DEVICE) || 1181 le16_to_cpu(req.wIndex) == OTG_STS_SELECTOR) && 1182 type != (USB_DIR_IN|USB_RECIP_ENDPOINT) && 1183 type != (USB_DIR_IN|USB_RECIP_INTERFACE)) 1184 goto delegate; 1185 if (le16_to_cpu(req.wLength) != 2 || 1186 le16_to_cpu(req.wValue) != 0) 1187 break; 1188 err = isr_get_status_response(ci, &req); 1189 break; 1190 case USB_REQ_SET_ADDRESS: 1191 if (type != (USB_DIR_OUT|USB_RECIP_DEVICE)) 1192 goto delegate; 1193 if (le16_to_cpu(req.wLength) != 0 || 1194 le16_to_cpu(req.wIndex) != 0) 1195 break; 1196 ci->address = (u8)le16_to_cpu(req.wValue); 1197 ci->setaddr = true; 1198 err = isr_setup_status_phase(ci); 1199 break; 1200 case USB_REQ_SET_FEATURE: 1201 if (type == (USB_DIR_OUT|USB_RECIP_ENDPOINT) && 1202 le16_to_cpu(req.wValue) == 1203 USB_ENDPOINT_HALT) { 1204 if (req.wLength != 0) 1205 break; 1206 num = le16_to_cpu(req.wIndex); 1207 dir = (num & USB_ENDPOINT_DIR_MASK) ? TX : RX; 1208 num &= USB_ENDPOINT_NUMBER_MASK; 1209 if (dir == TX) 1210 num += ci->hw_ep_max / 2; 1211 1212 spin_unlock(&ci->lock); 1213 err = _ep_set_halt(&ci->ci_hw_ep[num].ep, 1, false); 1214 spin_lock(&ci->lock); 1215 if (!err) 1216 isr_setup_status_phase(ci); 1217 } else if (type == (USB_DIR_OUT|USB_RECIP_DEVICE)) { 1218 if (req.wLength != 0) 1219 break; 1220 switch (le16_to_cpu(req.wValue)) { 1221 case USB_DEVICE_REMOTE_WAKEUP: 1222 ci->remote_wakeup = 1; 1223 err = isr_setup_status_phase(ci); 1224 break; 1225 case USB_DEVICE_TEST_MODE: 1226 tmode = le16_to_cpu(req.wIndex) >> 8; 1227 switch (tmode) { 1228 case USB_TEST_J: 1229 case USB_TEST_K: 1230 case USB_TEST_SE0_NAK: 1231 case USB_TEST_PACKET: 1232 case USB_TEST_FORCE_ENABLE: 1233 ci->test_mode = tmode; 1234 err = isr_setup_status_phase( 1235 ci); 1236 break; 1237 default: 1238 break; 1239 } 1240 break; 1241 case USB_DEVICE_B_HNP_ENABLE: 1242 if (ci_otg_is_fsm_mode(ci)) { 1243 ci->gadget.b_hnp_enable = 1; 1244 err = isr_setup_status_phase( 1245 ci); 1246 } 1247 break; 1248 case USB_DEVICE_A_ALT_HNP_SUPPORT: 1249 if (ci_otg_is_fsm_mode(ci)) 1250 err = otg_a_alt_hnp_support(ci); 1251 break; 1252 case USB_DEVICE_A_HNP_SUPPORT: 1253 if (ci_otg_is_fsm_mode(ci)) { 1254 ci->gadget.a_hnp_support = 1; 1255 err = isr_setup_status_phase( 1256 ci); 1257 } 1258 break; 1259 default: 1260 goto delegate; 1261 } 1262 } else { 1263 goto delegate; 1264 } 1265 break; 1266 default: 1267 delegate: 1268 if (req.wLength == 0) /* no data phase */ 1269 ci->ep0_dir = TX; 1270 1271 spin_unlock(&ci->lock); 1272 err = ci->driver->setup(&ci->gadget, &req); 1273 spin_lock(&ci->lock); 1274 break; 1275 } 1276 1277 if (err < 0) { 1278 spin_unlock(&ci->lock); 1279 if (_ep_set_halt(&hwep->ep, 1, false)) 1280 dev_err(ci->dev, "error: _ep_set_halt\n"); 1281 spin_lock(&ci->lock); 1282 } 1283 } 1284 1285 /** 1286 * isr_tr_complete_handler: transaction complete interrupt handler 1287 * @ci: UDC descriptor 1288 * 1289 * This function handles traffic events 1290 */ 1291 static void isr_tr_complete_handler(struct ci_hdrc *ci) 1292 __releases(ci->lock) 1293 __acquires(ci->lock) 1294 { 1295 unsigned i; 1296 int err; 1297 1298 for (i = 0; i < ci->hw_ep_max; i++) { 1299 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i]; 1300 1301 if (hwep->ep.desc == NULL) 1302 continue; /* not configured */ 1303 1304 if (hw_test_and_clear_complete(ci, i)) { 1305 err = isr_tr_complete_low(hwep); 1306 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) { 1307 if (err > 0) /* needs status phase */ 1308 err = isr_setup_status_phase(ci); 1309 if (err < 0) { 1310 spin_unlock(&ci->lock); 1311 if (_ep_set_halt(&hwep->ep, 1, false)) 1312 dev_err(ci->dev, 1313 "error: _ep_set_halt\n"); 1314 spin_lock(&ci->lock); 1315 } 1316 } 1317 } 1318 1319 /* Only handle setup packet below */ 1320 if (i == 0 && 1321 hw_test_and_clear(ci, OP_ENDPTSETUPSTAT, BIT(0))) 1322 isr_setup_packet_handler(ci); 1323 } 1324 } 1325 1326 /****************************************************************************** 1327 * ENDPT block 1328 *****************************************************************************/ 1329 /** 1330 * ep_enable: configure endpoint, making it usable 1331 * 1332 * Check usb_ep_enable() at "usb_gadget.h" for details 1333 */ 1334 static int ep_enable(struct usb_ep *ep, 1335 const struct usb_endpoint_descriptor *desc) 1336 { 1337 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 1338 int retval = 0; 1339 unsigned long flags; 1340 u32 cap = 0; 1341 1342 if (ep == NULL || desc == NULL) 1343 return -EINVAL; 1344 1345 spin_lock_irqsave(hwep->lock, flags); 1346 1347 /* only internal SW should enable ctrl endpts */ 1348 1349 if (!list_empty(&hwep->qh.queue)) { 1350 dev_warn(hwep->ci->dev, "enabling a non-empty endpoint!\n"); 1351 spin_unlock_irqrestore(hwep->lock, flags); 1352 return -EBUSY; 1353 } 1354 1355 hwep->ep.desc = desc; 1356 1357 hwep->dir = usb_endpoint_dir_in(desc) ? TX : RX; 1358 hwep->num = usb_endpoint_num(desc); 1359 hwep->type = usb_endpoint_type(desc); 1360 1361 hwep->ep.maxpacket = usb_endpoint_maxp(desc); 1362 hwep->ep.mult = usb_endpoint_maxp_mult(desc); 1363 1364 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) 1365 cap |= QH_IOS; 1366 1367 cap |= QH_ZLT; 1368 cap |= (hwep->ep.maxpacket << __ffs(QH_MAX_PKT)) & QH_MAX_PKT; 1369 /* 1370 * For ISO-TX, we set mult at QH as the largest value, and use 1371 * MultO at TD as real mult value. 1372 */ 1373 if (hwep->type == USB_ENDPOINT_XFER_ISOC && hwep->dir == TX) 1374 cap |= 3 << __ffs(QH_MULT); 1375 1376 hwep->qh.ptr->cap = cpu_to_le32(cap); 1377 1378 hwep->qh.ptr->td.next |= cpu_to_le32(TD_TERMINATE); /* needed? */ 1379 1380 if (hwep->num != 0 && hwep->type == USB_ENDPOINT_XFER_CONTROL) { 1381 dev_err(hwep->ci->dev, "Set control xfer at non-ep0\n"); 1382 retval = -EINVAL; 1383 } 1384 1385 /* 1386 * Enable endpoints in the HW other than ep0 as ep0 1387 * is always enabled 1388 */ 1389 if (hwep->num) 1390 retval |= hw_ep_enable(hwep->ci, hwep->num, hwep->dir, 1391 hwep->type); 1392 1393 spin_unlock_irqrestore(hwep->lock, flags); 1394 return retval; 1395 } 1396 1397 /** 1398 * ep_disable: endpoint is no longer usable 1399 * 1400 * Check usb_ep_disable() at "usb_gadget.h" for details 1401 */ 1402 static int ep_disable(struct usb_ep *ep) 1403 { 1404 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 1405 int direction, retval = 0; 1406 unsigned long flags; 1407 1408 if (ep == NULL) 1409 return -EINVAL; 1410 else if (hwep->ep.desc == NULL) 1411 return -EBUSY; 1412 1413 spin_lock_irqsave(hwep->lock, flags); 1414 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) { 1415 spin_unlock_irqrestore(hwep->lock, flags); 1416 return 0; 1417 } 1418 1419 /* only internal SW should disable ctrl endpts */ 1420 1421 direction = hwep->dir; 1422 do { 1423 retval |= _ep_nuke(hwep); 1424 retval |= hw_ep_disable(hwep->ci, hwep->num, hwep->dir); 1425 1426 if (hwep->type == USB_ENDPOINT_XFER_CONTROL) 1427 hwep->dir = (hwep->dir == TX) ? RX : TX; 1428 1429 } while (hwep->dir != direction); 1430 1431 hwep->ep.desc = NULL; 1432 1433 spin_unlock_irqrestore(hwep->lock, flags); 1434 return retval; 1435 } 1436 1437 /** 1438 * ep_alloc_request: allocate a request object to use with this endpoint 1439 * 1440 * Check usb_ep_alloc_request() at "usb_gadget.h" for details 1441 */ 1442 static struct usb_request *ep_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) 1443 { 1444 struct ci_hw_req *hwreq = NULL; 1445 1446 if (ep == NULL) 1447 return NULL; 1448 1449 hwreq = kzalloc(sizeof(struct ci_hw_req), gfp_flags); 1450 if (hwreq != NULL) { 1451 INIT_LIST_HEAD(&hwreq->queue); 1452 INIT_LIST_HEAD(&hwreq->tds); 1453 } 1454 1455 return (hwreq == NULL) ? NULL : &hwreq->req; 1456 } 1457 1458 /** 1459 * ep_free_request: frees a request object 1460 * 1461 * Check usb_ep_free_request() at "usb_gadget.h" for details 1462 */ 1463 static void ep_free_request(struct usb_ep *ep, struct usb_request *req) 1464 { 1465 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 1466 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req); 1467 struct td_node *node, *tmpnode; 1468 unsigned long flags; 1469 1470 if (ep == NULL || req == NULL) { 1471 return; 1472 } else if (!list_empty(&hwreq->queue)) { 1473 dev_err(hwep->ci->dev, "freeing queued request\n"); 1474 return; 1475 } 1476 1477 spin_lock_irqsave(hwep->lock, flags); 1478 1479 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) { 1480 dma_pool_free(hwep->td_pool, node->ptr, node->dma); 1481 list_del_init(&node->td); 1482 node->ptr = NULL; 1483 kfree(node); 1484 } 1485 1486 kfree(hwreq); 1487 1488 spin_unlock_irqrestore(hwep->lock, flags); 1489 } 1490 1491 /** 1492 * ep_queue: queues (submits) an I/O request to an endpoint 1493 * 1494 * Check usb_ep_queue()* at usb_gadget.h" for details 1495 */ 1496 static int ep_queue(struct usb_ep *ep, struct usb_request *req, 1497 gfp_t __maybe_unused gfp_flags) 1498 { 1499 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 1500 int retval = 0; 1501 unsigned long flags; 1502 1503 if (ep == NULL || req == NULL || hwep->ep.desc == NULL) 1504 return -EINVAL; 1505 1506 spin_lock_irqsave(hwep->lock, flags); 1507 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) { 1508 spin_unlock_irqrestore(hwep->lock, flags); 1509 return 0; 1510 } 1511 retval = _ep_queue(ep, req, gfp_flags); 1512 spin_unlock_irqrestore(hwep->lock, flags); 1513 return retval; 1514 } 1515 1516 /** 1517 * ep_dequeue: dequeues (cancels, unlinks) an I/O request from an endpoint 1518 * 1519 * Check usb_ep_dequeue() at "usb_gadget.h" for details 1520 */ 1521 static int ep_dequeue(struct usb_ep *ep, struct usb_request *req) 1522 { 1523 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 1524 struct ci_hw_req *hwreq = container_of(req, struct ci_hw_req, req); 1525 unsigned long flags; 1526 struct td_node *node, *tmpnode; 1527 1528 if (ep == NULL || req == NULL || hwreq->req.status != -EALREADY || 1529 hwep->ep.desc == NULL || list_empty(&hwreq->queue) || 1530 list_empty(&hwep->qh.queue)) 1531 return -EINVAL; 1532 1533 spin_lock_irqsave(hwep->lock, flags); 1534 if (hwep->ci->gadget.speed != USB_SPEED_UNKNOWN) 1535 hw_ep_flush(hwep->ci, hwep->num, hwep->dir); 1536 1537 list_for_each_entry_safe(node, tmpnode, &hwreq->tds, td) { 1538 dma_pool_free(hwep->td_pool, node->ptr, node->dma); 1539 list_del(&node->td); 1540 kfree(node); 1541 } 1542 1543 /* pop request */ 1544 list_del_init(&hwreq->queue); 1545 1546 usb_gadget_unmap_request(&hwep->ci->gadget, req, hwep->dir); 1547 1548 req->status = -ECONNRESET; 1549 1550 if (hwreq->req.complete != NULL) { 1551 spin_unlock(hwep->lock); 1552 usb_gadget_giveback_request(&hwep->ep, &hwreq->req); 1553 spin_lock(hwep->lock); 1554 } 1555 1556 spin_unlock_irqrestore(hwep->lock, flags); 1557 return 0; 1558 } 1559 1560 /** 1561 * ep_set_halt: sets the endpoint halt feature 1562 * 1563 * Check usb_ep_set_halt() at "usb_gadget.h" for details 1564 */ 1565 static int ep_set_halt(struct usb_ep *ep, int value) 1566 { 1567 return _ep_set_halt(ep, value, true); 1568 } 1569 1570 /** 1571 * ep_set_wedge: sets the halt feature and ignores clear requests 1572 * 1573 * Check usb_ep_set_wedge() at "usb_gadget.h" for details 1574 */ 1575 static int ep_set_wedge(struct usb_ep *ep) 1576 { 1577 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 1578 unsigned long flags; 1579 1580 if (ep == NULL || hwep->ep.desc == NULL) 1581 return -EINVAL; 1582 1583 spin_lock_irqsave(hwep->lock, flags); 1584 hwep->wedge = 1; 1585 spin_unlock_irqrestore(hwep->lock, flags); 1586 1587 return usb_ep_set_halt(ep); 1588 } 1589 1590 /** 1591 * ep_fifo_flush: flushes contents of a fifo 1592 * 1593 * Check usb_ep_fifo_flush() at "usb_gadget.h" for details 1594 */ 1595 static void ep_fifo_flush(struct usb_ep *ep) 1596 { 1597 struct ci_hw_ep *hwep = container_of(ep, struct ci_hw_ep, ep); 1598 unsigned long flags; 1599 1600 if (ep == NULL) { 1601 dev_err(hwep->ci->dev, "%02X: -EINVAL\n", _usb_addr(hwep)); 1602 return; 1603 } 1604 1605 spin_lock_irqsave(hwep->lock, flags); 1606 if (hwep->ci->gadget.speed == USB_SPEED_UNKNOWN) { 1607 spin_unlock_irqrestore(hwep->lock, flags); 1608 return; 1609 } 1610 1611 hw_ep_flush(hwep->ci, hwep->num, hwep->dir); 1612 1613 spin_unlock_irqrestore(hwep->lock, flags); 1614 } 1615 1616 /** 1617 * Endpoint-specific part of the API to the USB controller hardware 1618 * Check "usb_gadget.h" for details 1619 */ 1620 static const struct usb_ep_ops usb_ep_ops = { 1621 .enable = ep_enable, 1622 .disable = ep_disable, 1623 .alloc_request = ep_alloc_request, 1624 .free_request = ep_free_request, 1625 .queue = ep_queue, 1626 .dequeue = ep_dequeue, 1627 .set_halt = ep_set_halt, 1628 .set_wedge = ep_set_wedge, 1629 .fifo_flush = ep_fifo_flush, 1630 }; 1631 1632 /****************************************************************************** 1633 * GADGET block 1634 *****************************************************************************/ 1635 /** 1636 * ci_hdrc_gadget_connect: caller makes sure gadget driver is binded 1637 */ 1638 static void ci_hdrc_gadget_connect(struct usb_gadget *_gadget, int is_active) 1639 { 1640 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget); 1641 1642 if (is_active) { 1643 pm_runtime_get_sync(ci->dev); 1644 hw_device_reset(ci); 1645 spin_lock_irq(&ci->lock); 1646 if (ci->driver) { 1647 hw_device_state(ci, ci->ep0out->qh.dma); 1648 usb_gadget_set_state(_gadget, USB_STATE_POWERED); 1649 spin_unlock_irq(&ci->lock); 1650 usb_udc_vbus_handler(_gadget, true); 1651 } else { 1652 spin_unlock_irq(&ci->lock); 1653 } 1654 } else { 1655 usb_udc_vbus_handler(_gadget, false); 1656 if (ci->driver) 1657 ci->driver->disconnect(&ci->gadget); 1658 hw_device_state(ci, 0); 1659 if (ci->platdata->notify_event) 1660 ci->platdata->notify_event(ci, 1661 CI_HDRC_CONTROLLER_STOPPED_EVENT); 1662 _gadget_stop_activity(&ci->gadget); 1663 pm_runtime_put_sync(ci->dev); 1664 usb_gadget_set_state(_gadget, USB_STATE_NOTATTACHED); 1665 } 1666 } 1667 1668 static int ci_udc_vbus_session(struct usb_gadget *_gadget, int is_active) 1669 { 1670 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget); 1671 unsigned long flags; 1672 int ret = 0; 1673 1674 spin_lock_irqsave(&ci->lock, flags); 1675 ci->vbus_active = is_active; 1676 spin_unlock_irqrestore(&ci->lock, flags); 1677 1678 if (ci->usb_phy) 1679 usb_phy_set_charger_state(ci->usb_phy, is_active ? 1680 USB_CHARGER_PRESENT : USB_CHARGER_ABSENT); 1681 1682 if (ci->platdata->notify_event) 1683 ret = ci->platdata->notify_event(ci, 1684 CI_HDRC_CONTROLLER_VBUS_EVENT); 1685 1686 if (ci->driver) 1687 ci_hdrc_gadget_connect(_gadget, is_active); 1688 1689 return ret; 1690 } 1691 1692 static int ci_udc_wakeup(struct usb_gadget *_gadget) 1693 { 1694 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget); 1695 unsigned long flags; 1696 int ret = 0; 1697 1698 spin_lock_irqsave(&ci->lock, flags); 1699 if (ci->gadget.speed == USB_SPEED_UNKNOWN) { 1700 spin_unlock_irqrestore(&ci->lock, flags); 1701 return 0; 1702 } 1703 if (!ci->remote_wakeup) { 1704 ret = -EOPNOTSUPP; 1705 goto out; 1706 } 1707 if (!hw_read(ci, OP_PORTSC, PORTSC_SUSP)) { 1708 ret = -EINVAL; 1709 goto out; 1710 } 1711 hw_write(ci, OP_PORTSC, PORTSC_FPR, PORTSC_FPR); 1712 out: 1713 spin_unlock_irqrestore(&ci->lock, flags); 1714 return ret; 1715 } 1716 1717 static int ci_udc_vbus_draw(struct usb_gadget *_gadget, unsigned ma) 1718 { 1719 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget); 1720 1721 if (ci->usb_phy) 1722 return usb_phy_set_power(ci->usb_phy, ma); 1723 return -ENOTSUPP; 1724 } 1725 1726 static int ci_udc_selfpowered(struct usb_gadget *_gadget, int is_on) 1727 { 1728 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget); 1729 struct ci_hw_ep *hwep = ci->ep0in; 1730 unsigned long flags; 1731 1732 spin_lock_irqsave(hwep->lock, flags); 1733 _gadget->is_selfpowered = (is_on != 0); 1734 spin_unlock_irqrestore(hwep->lock, flags); 1735 1736 return 0; 1737 } 1738 1739 /* Change Data+ pullup status 1740 * this func is used by usb_gadget_connect/disconnect 1741 */ 1742 static int ci_udc_pullup(struct usb_gadget *_gadget, int is_on) 1743 { 1744 struct ci_hdrc *ci = container_of(_gadget, struct ci_hdrc, gadget); 1745 1746 /* 1747 * Data+ pullup controlled by OTG state machine in OTG fsm mode; 1748 * and don't touch Data+ in host mode for dual role config. 1749 */ 1750 if (ci_otg_is_fsm_mode(ci) || ci->role == CI_ROLE_HOST) 1751 return 0; 1752 1753 pm_runtime_get_sync(ci->dev); 1754 if (is_on) 1755 hw_write(ci, OP_USBCMD, USBCMD_RS, USBCMD_RS); 1756 else 1757 hw_write(ci, OP_USBCMD, USBCMD_RS, 0); 1758 pm_runtime_put_sync(ci->dev); 1759 1760 return 0; 1761 } 1762 1763 static int ci_udc_start(struct usb_gadget *gadget, 1764 struct usb_gadget_driver *driver); 1765 static int ci_udc_stop(struct usb_gadget *gadget); 1766 1767 /* Match ISOC IN from the highest endpoint */ 1768 static struct usb_ep *ci_udc_match_ep(struct usb_gadget *gadget, 1769 struct usb_endpoint_descriptor *desc, 1770 struct usb_ss_ep_comp_descriptor *comp_desc) 1771 { 1772 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget); 1773 struct usb_ep *ep; 1774 1775 if (usb_endpoint_xfer_isoc(desc) && usb_endpoint_dir_in(desc)) { 1776 list_for_each_entry_reverse(ep, &ci->gadget.ep_list, ep_list) { 1777 if (ep->caps.dir_in && !ep->claimed) 1778 return ep; 1779 } 1780 } 1781 1782 return NULL; 1783 } 1784 1785 /** 1786 * Device operations part of the API to the USB controller hardware, 1787 * which don't involve endpoints (or i/o) 1788 * Check "usb_gadget.h" for details 1789 */ 1790 static const struct usb_gadget_ops usb_gadget_ops = { 1791 .vbus_session = ci_udc_vbus_session, 1792 .wakeup = ci_udc_wakeup, 1793 .set_selfpowered = ci_udc_selfpowered, 1794 .pullup = ci_udc_pullup, 1795 .vbus_draw = ci_udc_vbus_draw, 1796 .udc_start = ci_udc_start, 1797 .udc_stop = ci_udc_stop, 1798 .match_ep = ci_udc_match_ep, 1799 }; 1800 1801 static int init_eps(struct ci_hdrc *ci) 1802 { 1803 int retval = 0, i, j; 1804 1805 for (i = 0; i < ci->hw_ep_max/2; i++) 1806 for (j = RX; j <= TX; j++) { 1807 int k = i + j * ci->hw_ep_max/2; 1808 struct ci_hw_ep *hwep = &ci->ci_hw_ep[k]; 1809 1810 scnprintf(hwep->name, sizeof(hwep->name), "ep%i%s", i, 1811 (j == TX) ? "in" : "out"); 1812 1813 hwep->ci = ci; 1814 hwep->lock = &ci->lock; 1815 hwep->td_pool = ci->td_pool; 1816 1817 hwep->ep.name = hwep->name; 1818 hwep->ep.ops = &usb_ep_ops; 1819 1820 if (i == 0) { 1821 hwep->ep.caps.type_control = true; 1822 } else { 1823 hwep->ep.caps.type_iso = true; 1824 hwep->ep.caps.type_bulk = true; 1825 hwep->ep.caps.type_int = true; 1826 } 1827 1828 if (j == TX) 1829 hwep->ep.caps.dir_in = true; 1830 else 1831 hwep->ep.caps.dir_out = true; 1832 1833 /* 1834 * for ep0: maxP defined in desc, for other 1835 * eps, maxP is set by epautoconfig() called 1836 * by gadget layer 1837 */ 1838 usb_ep_set_maxpacket_limit(&hwep->ep, (unsigned short)~0); 1839 1840 INIT_LIST_HEAD(&hwep->qh.queue); 1841 hwep->qh.ptr = dma_pool_zalloc(ci->qh_pool, GFP_KERNEL, 1842 &hwep->qh.dma); 1843 if (hwep->qh.ptr == NULL) 1844 retval = -ENOMEM; 1845 1846 /* 1847 * set up shorthands for ep0 out and in endpoints, 1848 * don't add to gadget's ep_list 1849 */ 1850 if (i == 0) { 1851 if (j == RX) 1852 ci->ep0out = hwep; 1853 else 1854 ci->ep0in = hwep; 1855 1856 usb_ep_set_maxpacket_limit(&hwep->ep, CTRL_PAYLOAD_MAX); 1857 continue; 1858 } 1859 1860 list_add_tail(&hwep->ep.ep_list, &ci->gadget.ep_list); 1861 } 1862 1863 return retval; 1864 } 1865 1866 static void destroy_eps(struct ci_hdrc *ci) 1867 { 1868 int i; 1869 1870 for (i = 0; i < ci->hw_ep_max; i++) { 1871 struct ci_hw_ep *hwep = &ci->ci_hw_ep[i]; 1872 1873 if (hwep->pending_td) 1874 free_pending_td(hwep); 1875 dma_pool_free(ci->qh_pool, hwep->qh.ptr, hwep->qh.dma); 1876 } 1877 } 1878 1879 /** 1880 * ci_udc_start: register a gadget driver 1881 * @gadget: our gadget 1882 * @driver: the driver being registered 1883 * 1884 * Interrupts are enabled here. 1885 */ 1886 static int ci_udc_start(struct usb_gadget *gadget, 1887 struct usb_gadget_driver *driver) 1888 { 1889 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget); 1890 int retval; 1891 1892 if (driver->disconnect == NULL) 1893 return -EINVAL; 1894 1895 ci->ep0out->ep.desc = &ctrl_endpt_out_desc; 1896 retval = usb_ep_enable(&ci->ep0out->ep); 1897 if (retval) 1898 return retval; 1899 1900 ci->ep0in->ep.desc = &ctrl_endpt_in_desc; 1901 retval = usb_ep_enable(&ci->ep0in->ep); 1902 if (retval) 1903 return retval; 1904 1905 ci->driver = driver; 1906 1907 /* Start otg fsm for B-device */ 1908 if (ci_otg_is_fsm_mode(ci) && ci->fsm.id) { 1909 ci_hdrc_otg_fsm_start(ci); 1910 return retval; 1911 } 1912 1913 if (ci->vbus_active) 1914 ci_hdrc_gadget_connect(gadget, 1); 1915 else 1916 usb_udc_vbus_handler(&ci->gadget, false); 1917 1918 return retval; 1919 } 1920 1921 static void ci_udc_stop_for_otg_fsm(struct ci_hdrc *ci) 1922 { 1923 if (!ci_otg_is_fsm_mode(ci)) 1924 return; 1925 1926 mutex_lock(&ci->fsm.lock); 1927 if (ci->fsm.otg->state == OTG_STATE_A_PERIPHERAL) { 1928 ci->fsm.a_bidl_adis_tmout = 1; 1929 ci_hdrc_otg_fsm_start(ci); 1930 } else if (ci->fsm.otg->state == OTG_STATE_B_PERIPHERAL) { 1931 ci->fsm.protocol = PROTO_UNDEF; 1932 ci->fsm.otg->state = OTG_STATE_UNDEFINED; 1933 } 1934 mutex_unlock(&ci->fsm.lock); 1935 } 1936 1937 /** 1938 * ci_udc_stop: unregister a gadget driver 1939 */ 1940 static int ci_udc_stop(struct usb_gadget *gadget) 1941 { 1942 struct ci_hdrc *ci = container_of(gadget, struct ci_hdrc, gadget); 1943 unsigned long flags; 1944 1945 spin_lock_irqsave(&ci->lock, flags); 1946 ci->driver = NULL; 1947 1948 if (ci->vbus_active) { 1949 hw_device_state(ci, 0); 1950 spin_unlock_irqrestore(&ci->lock, flags); 1951 if (ci->platdata->notify_event) 1952 ci->platdata->notify_event(ci, 1953 CI_HDRC_CONTROLLER_STOPPED_EVENT); 1954 _gadget_stop_activity(&ci->gadget); 1955 spin_lock_irqsave(&ci->lock, flags); 1956 pm_runtime_put(ci->dev); 1957 } 1958 1959 spin_unlock_irqrestore(&ci->lock, flags); 1960 1961 ci_udc_stop_for_otg_fsm(ci); 1962 return 0; 1963 } 1964 1965 /****************************************************************************** 1966 * BUS block 1967 *****************************************************************************/ 1968 /** 1969 * udc_irq: ci interrupt handler 1970 * 1971 * This function returns IRQ_HANDLED if the IRQ has been handled 1972 * It locks access to registers 1973 */ 1974 static irqreturn_t udc_irq(struct ci_hdrc *ci) 1975 { 1976 irqreturn_t retval; 1977 u32 intr; 1978 1979 if (ci == NULL) 1980 return IRQ_HANDLED; 1981 1982 spin_lock(&ci->lock); 1983 1984 if (ci->platdata->flags & CI_HDRC_REGS_SHARED) { 1985 if (hw_read(ci, OP_USBMODE, USBMODE_CM) != 1986 USBMODE_CM_DC) { 1987 spin_unlock(&ci->lock); 1988 return IRQ_NONE; 1989 } 1990 } 1991 intr = hw_test_and_clear_intr_active(ci); 1992 1993 if (intr) { 1994 /* order defines priority - do NOT change it */ 1995 if (USBi_URI & intr) 1996 isr_reset_handler(ci); 1997 1998 if (USBi_PCI & intr) { 1999 ci->gadget.speed = hw_port_is_high_speed(ci) ? 2000 USB_SPEED_HIGH : USB_SPEED_FULL; 2001 if (ci->suspended) { 2002 if (ci->driver->resume) { 2003 spin_unlock(&ci->lock); 2004 ci->driver->resume(&ci->gadget); 2005 spin_lock(&ci->lock); 2006 } 2007 ci->suspended = 0; 2008 usb_gadget_set_state(&ci->gadget, 2009 ci->resume_state); 2010 } 2011 } 2012 2013 if (USBi_UI & intr) 2014 isr_tr_complete_handler(ci); 2015 2016 if ((USBi_SLI & intr) && !(ci->suspended)) { 2017 ci->suspended = 1; 2018 ci->resume_state = ci->gadget.state; 2019 if (ci->gadget.speed != USB_SPEED_UNKNOWN && 2020 ci->driver->suspend) { 2021 spin_unlock(&ci->lock); 2022 ci->driver->suspend(&ci->gadget); 2023 spin_lock(&ci->lock); 2024 } 2025 usb_gadget_set_state(&ci->gadget, 2026 USB_STATE_SUSPENDED); 2027 } 2028 retval = IRQ_HANDLED; 2029 } else { 2030 retval = IRQ_NONE; 2031 } 2032 spin_unlock(&ci->lock); 2033 2034 return retval; 2035 } 2036 2037 /** 2038 * udc_start: initialize gadget role 2039 * @ci: chipidea controller 2040 */ 2041 static int udc_start(struct ci_hdrc *ci) 2042 { 2043 struct device *dev = ci->dev; 2044 struct usb_otg_caps *otg_caps = &ci->platdata->ci_otg_caps; 2045 int retval = 0; 2046 2047 ci->gadget.ops = &usb_gadget_ops; 2048 ci->gadget.speed = USB_SPEED_UNKNOWN; 2049 ci->gadget.max_speed = USB_SPEED_HIGH; 2050 ci->gadget.name = ci->platdata->name; 2051 ci->gadget.otg_caps = otg_caps; 2052 ci->gadget.sg_supported = 1; 2053 2054 if (ci->platdata->flags & CI_HDRC_REQUIRES_ALIGNED_DMA) 2055 ci->gadget.quirk_avoids_skb_reserve = 1; 2056 2057 if (ci->is_otg && (otg_caps->hnp_support || otg_caps->srp_support || 2058 otg_caps->adp_support)) 2059 ci->gadget.is_otg = 1; 2060 2061 INIT_LIST_HEAD(&ci->gadget.ep_list); 2062 2063 /* alloc resources */ 2064 ci->qh_pool = dma_pool_create("ci_hw_qh", dev->parent, 2065 sizeof(struct ci_hw_qh), 2066 64, CI_HDRC_PAGE_SIZE); 2067 if (ci->qh_pool == NULL) 2068 return -ENOMEM; 2069 2070 ci->td_pool = dma_pool_create("ci_hw_td", dev->parent, 2071 sizeof(struct ci_hw_td), 2072 64, CI_HDRC_PAGE_SIZE); 2073 if (ci->td_pool == NULL) { 2074 retval = -ENOMEM; 2075 goto free_qh_pool; 2076 } 2077 2078 retval = init_eps(ci); 2079 if (retval) 2080 goto free_pools; 2081 2082 ci->gadget.ep0 = &ci->ep0in->ep; 2083 2084 retval = usb_add_gadget_udc(dev, &ci->gadget); 2085 if (retval) 2086 goto destroy_eps; 2087 2088 return retval; 2089 2090 destroy_eps: 2091 destroy_eps(ci); 2092 free_pools: 2093 dma_pool_destroy(ci->td_pool); 2094 free_qh_pool: 2095 dma_pool_destroy(ci->qh_pool); 2096 return retval; 2097 } 2098 2099 /** 2100 * ci_hdrc_gadget_destroy: parent remove must call this to remove UDC 2101 * 2102 * No interrupts active, the IRQ has been released 2103 */ 2104 void ci_hdrc_gadget_destroy(struct ci_hdrc *ci) 2105 { 2106 if (!ci->roles[CI_ROLE_GADGET]) 2107 return; 2108 2109 usb_del_gadget_udc(&ci->gadget); 2110 2111 destroy_eps(ci); 2112 2113 dma_pool_destroy(ci->td_pool); 2114 dma_pool_destroy(ci->qh_pool); 2115 } 2116 2117 static int udc_id_switch_for_device(struct ci_hdrc *ci) 2118 { 2119 if (ci->platdata->pins_device) 2120 pinctrl_select_state(ci->platdata->pctl, 2121 ci->platdata->pins_device); 2122 2123 if (ci->is_otg) 2124 /* Clear and enable BSV irq */ 2125 hw_write_otgsc(ci, OTGSC_BSVIS | OTGSC_BSVIE, 2126 OTGSC_BSVIS | OTGSC_BSVIE); 2127 2128 return 0; 2129 } 2130 2131 static void udc_id_switch_for_host(struct ci_hdrc *ci) 2132 { 2133 /* 2134 * host doesn't care B_SESSION_VALID event 2135 * so clear and disbale BSV irq 2136 */ 2137 if (ci->is_otg) 2138 hw_write_otgsc(ci, OTGSC_BSVIE | OTGSC_BSVIS, OTGSC_BSVIS); 2139 2140 ci->vbus_active = 0; 2141 2142 if (ci->platdata->pins_device && ci->platdata->pins_default) 2143 pinctrl_select_state(ci->platdata->pctl, 2144 ci->platdata->pins_default); 2145 } 2146 2147 /** 2148 * ci_hdrc_gadget_init - initialize device related bits 2149 * ci: the controller 2150 * 2151 * This function initializes the gadget, if the device is "device capable". 2152 */ 2153 int ci_hdrc_gadget_init(struct ci_hdrc *ci) 2154 { 2155 struct ci_role_driver *rdrv; 2156 int ret; 2157 2158 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DC)) 2159 return -ENXIO; 2160 2161 rdrv = devm_kzalloc(ci->dev, sizeof(*rdrv), GFP_KERNEL); 2162 if (!rdrv) 2163 return -ENOMEM; 2164 2165 rdrv->start = udc_id_switch_for_device; 2166 rdrv->stop = udc_id_switch_for_host; 2167 rdrv->irq = udc_irq; 2168 rdrv->name = "gadget"; 2169 2170 ret = udc_start(ci); 2171 if (!ret) 2172 ci->roles[CI_ROLE_GADGET] = rdrv; 2173 2174 return ret; 2175 } 2176