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