1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Cadence USBSS DRD Driver - gadget side. 4 * 5 * Copyright (C) 2018 Cadence Design Systems. 6 * Copyright (C) 2017-2018 NXP 7 * 8 * Authors: Pawel Jez <pjez@cadence.com>, 9 * Pawel Laszczak <pawell@cadence.com> 10 * Peter Chen <peter.chen@nxp.com> 11 */ 12 13 #include <linux/usb/composite.h> 14 #include <linux/iopoll.h> 15 16 #include "gadget.h" 17 #include "trace.h" 18 19 static struct usb_endpoint_descriptor cdns3_gadget_ep0_desc = { 20 .bLength = USB_DT_ENDPOINT_SIZE, 21 .bDescriptorType = USB_DT_ENDPOINT, 22 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 23 }; 24 25 /** 26 * cdns3_ep0_run_transfer - Do transfer on default endpoint hardware 27 * @priv_dev: extended gadget object 28 * @dma_addr: physical address where data is/will be stored 29 * @length: data length 30 * @erdy: set it to 1 when ERDY packet should be sent - 31 * exit from flow control state 32 */ cdns3_ep0_run_transfer(struct cdns3_device * priv_dev,dma_addr_t dma_addr,unsigned int length,int erdy,int zlp)33 static void cdns3_ep0_run_transfer(struct cdns3_device *priv_dev, 34 dma_addr_t dma_addr, 35 unsigned int length, int erdy, int zlp) 36 { 37 struct cdns3_usb_regs __iomem *regs = priv_dev->regs; 38 struct cdns3_endpoint *priv_ep = priv_dev->eps[0]; 39 40 priv_ep->trb_pool[0].buffer = TRB_BUFFER(dma_addr); 41 priv_ep->trb_pool[0].length = TRB_LEN(length); 42 43 if (zlp) { 44 priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_TYPE(TRB_NORMAL); 45 priv_ep->trb_pool[1].buffer = TRB_BUFFER(dma_addr); 46 priv_ep->trb_pool[1].length = TRB_LEN(0); 47 priv_ep->trb_pool[1].control = TRB_CYCLE | TRB_IOC | 48 TRB_TYPE(TRB_NORMAL); 49 } else { 50 priv_ep->trb_pool[0].control = TRB_CYCLE | TRB_IOC | 51 TRB_TYPE(TRB_NORMAL); 52 priv_ep->trb_pool[1].control = 0; 53 } 54 55 trace_cdns3_prepare_trb(priv_ep, priv_ep->trb_pool); 56 57 cdns3_select_ep(priv_dev, priv_dev->ep0_data_dir); 58 59 writel(EP_STS_TRBERR, ®s->ep_sts); 60 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma), ®s->ep_traddr); 61 trace_cdns3_doorbell_ep0(priv_dev->ep0_data_dir ? "ep0in" : "ep0out", 62 readl(®s->ep_traddr)); 63 64 /* TRB should be prepared before starting transfer. */ 65 writel(EP_CMD_DRDY, ®s->ep_cmd); 66 67 /* Resume controller before arming transfer. */ 68 __cdns3_gadget_wakeup(priv_dev); 69 70 if (erdy) 71 writel(EP_CMD_ERDY, &priv_dev->regs->ep_cmd); 72 } 73 74 /** 75 * cdns3_ep0_delegate_req - Returns status of handling setup packet 76 * Setup is handled by gadget driver 77 * @priv_dev: extended gadget object 78 * @ctrl_req: pointer to received setup packet 79 * 80 * Returns zero on success or negative value on failure 81 */ cdns3_ep0_delegate_req(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl_req)82 static int cdns3_ep0_delegate_req(struct cdns3_device *priv_dev, 83 struct usb_ctrlrequest *ctrl_req) 84 { 85 int ret; 86 87 spin_unlock(&priv_dev->lock); 88 priv_dev->setup_pending = 1; 89 ret = priv_dev->gadget_driver->setup(&priv_dev->gadget, ctrl_req); 90 priv_dev->setup_pending = 0; 91 spin_lock(&priv_dev->lock); 92 return ret; 93 } 94 cdns3_prepare_setup_packet(struct cdns3_device * priv_dev)95 static void cdns3_prepare_setup_packet(struct cdns3_device *priv_dev) 96 { 97 priv_dev->ep0_data_dir = 0; 98 priv_dev->ep0_stage = CDNS3_SETUP_STAGE; 99 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 100 sizeof(struct usb_ctrlrequest), 0, 0); 101 } 102 cdns3_ep0_complete_setup(struct cdns3_device * priv_dev,u8 send_stall,u8 send_erdy)103 static void cdns3_ep0_complete_setup(struct cdns3_device *priv_dev, 104 u8 send_stall, u8 send_erdy) 105 { 106 struct cdns3_endpoint *priv_ep = priv_dev->eps[0]; 107 struct usb_request *request; 108 109 request = cdns3_next_request(&priv_ep->pending_req_list); 110 if (request) 111 list_del_init(&request->list); 112 113 if (send_stall) { 114 trace_cdns3_halt(priv_ep, send_stall, 0); 115 /* set_stall on ep0 */ 116 cdns3_select_ep(priv_dev, 0x00); 117 writel(EP_CMD_SSTALL, &priv_dev->regs->ep_cmd); 118 } else { 119 cdns3_prepare_setup_packet(priv_dev); 120 } 121 122 priv_dev->ep0_stage = CDNS3_SETUP_STAGE; 123 writel((send_erdy ? EP_CMD_ERDY : 0) | EP_CMD_REQ_CMPL, 124 &priv_dev->regs->ep_cmd); 125 126 cdns3_allow_enable_l1(priv_dev, 1); 127 } 128 129 /** 130 * cdns3_req_ep0_set_configuration - Handling of SET_CONFIG standard USB request 131 * @priv_dev: extended gadget object 132 * @ctrl_req: pointer to received setup packet 133 * 134 * Returns 0 if success, USB_GADGET_DELAYED_STATUS on deferred status stage, 135 * error code on error 136 */ cdns3_req_ep0_set_configuration(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl_req)137 static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev, 138 struct usb_ctrlrequest *ctrl_req) 139 { 140 enum usb_device_state device_state = priv_dev->gadget.state; 141 struct cdns3_endpoint *priv_ep; 142 u32 config = le16_to_cpu(ctrl_req->wValue); 143 int result = 0; 144 int i; 145 146 switch (device_state) { 147 case USB_STATE_ADDRESS: 148 /* Configure non-control EPs */ 149 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) { 150 priv_ep = priv_dev->eps[i]; 151 if (!priv_ep) 152 continue; 153 154 if (priv_ep->flags & EP_CLAIMED) 155 cdns3_ep_config(priv_ep); 156 } 157 158 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req); 159 160 if (result) 161 return result; 162 163 if (config) { 164 cdns3_set_hw_configuration(priv_dev); 165 } else { 166 cdns3_hw_reset_eps_config(priv_dev); 167 usb_gadget_set_state(&priv_dev->gadget, 168 USB_STATE_ADDRESS); 169 } 170 break; 171 case USB_STATE_CONFIGURED: 172 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req); 173 174 if (!config && !result) { 175 cdns3_hw_reset_eps_config(priv_dev); 176 usb_gadget_set_state(&priv_dev->gadget, 177 USB_STATE_ADDRESS); 178 } 179 break; 180 default: 181 result = -EINVAL; 182 } 183 184 return result; 185 } 186 187 /** 188 * cdns3_req_ep0_set_address - Handling of SET_ADDRESS standard USB request 189 * @priv_dev: extended gadget object 190 * @ctrl_req: pointer to received setup packet 191 * 192 * Returns 0 if success, error code on error 193 */ cdns3_req_ep0_set_address(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl_req)194 static int cdns3_req_ep0_set_address(struct cdns3_device *priv_dev, 195 struct usb_ctrlrequest *ctrl_req) 196 { 197 enum usb_device_state device_state = priv_dev->gadget.state; 198 u32 reg; 199 u32 addr; 200 201 addr = le16_to_cpu(ctrl_req->wValue); 202 203 if (addr > USB_DEVICE_MAX_ADDRESS) { 204 dev_err(priv_dev->dev, 205 "Device address (%d) cannot be greater than %d\n", 206 addr, USB_DEVICE_MAX_ADDRESS); 207 return -EINVAL; 208 } 209 210 if (device_state == USB_STATE_CONFIGURED) { 211 dev_err(priv_dev->dev, 212 "can't set_address from configured state\n"); 213 return -EINVAL; 214 } 215 216 reg = readl(&priv_dev->regs->usb_cmd); 217 218 writel(reg | USB_CMD_FADDR(addr) | USB_CMD_SET_ADDR, 219 &priv_dev->regs->usb_cmd); 220 221 usb_gadget_set_state(&priv_dev->gadget, 222 (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT)); 223 224 return 0; 225 } 226 227 /** 228 * cdns3_req_ep0_get_status - Handling of GET_STATUS standard USB request 229 * @priv_dev: extended gadget object 230 * @ctrl_req: pointer to received setup packet 231 * 232 * Returns 0 if success, error code on error 233 */ cdns3_req_ep0_get_status(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl)234 static int cdns3_req_ep0_get_status(struct cdns3_device *priv_dev, 235 struct usb_ctrlrequest *ctrl) 236 { 237 struct cdns3_endpoint *priv_ep; 238 __le16 *response_pkt; 239 u16 usb_status = 0; 240 u32 recip; 241 u8 index; 242 243 recip = ctrl->bRequestType & USB_RECIP_MASK; 244 245 switch (recip) { 246 case USB_RECIP_DEVICE: 247 /* self powered */ 248 if (priv_dev->is_selfpowered) 249 usb_status = BIT(USB_DEVICE_SELF_POWERED); 250 251 if (priv_dev->wake_up_flag) 252 usb_status |= BIT(USB_DEVICE_REMOTE_WAKEUP); 253 254 if (priv_dev->gadget.speed != USB_SPEED_SUPER) 255 break; 256 257 if (priv_dev->u1_allowed) 258 usb_status |= BIT(USB_DEV_STAT_U1_ENABLED); 259 260 if (priv_dev->u2_allowed) 261 usb_status |= BIT(USB_DEV_STAT_U2_ENABLED); 262 263 break; 264 case USB_RECIP_INTERFACE: 265 return cdns3_ep0_delegate_req(priv_dev, ctrl); 266 case USB_RECIP_ENDPOINT: 267 index = cdns3_ep_addr_to_index(ctrl->wIndex); 268 priv_ep = priv_dev->eps[index]; 269 270 /* check if endpoint is stalled or stall is pending */ 271 cdns3_select_ep(priv_dev, ctrl->wIndex); 272 if (EP_STS_STALL(readl(&priv_dev->regs->ep_sts)) || 273 (priv_ep->flags & EP_STALL_PENDING)) 274 usb_status = BIT(USB_ENDPOINT_HALT); 275 break; 276 default: 277 return -EINVAL; 278 } 279 280 response_pkt = (__le16 *)priv_dev->setup_buf; 281 *response_pkt = cpu_to_le16(usb_status); 282 283 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 284 sizeof(*response_pkt), 1, 0); 285 return 0; 286 } 287 cdns3_ep0_feature_handle_device(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl,int set)288 static int cdns3_ep0_feature_handle_device(struct cdns3_device *priv_dev, 289 struct usb_ctrlrequest *ctrl, 290 int set) 291 { 292 enum usb_device_state state; 293 enum usb_device_speed speed; 294 int ret = 0; 295 u32 wValue; 296 u16 tmode; 297 298 wValue = le16_to_cpu(ctrl->wValue); 299 state = priv_dev->gadget.state; 300 speed = priv_dev->gadget.speed; 301 302 switch (wValue) { 303 case USB_DEVICE_REMOTE_WAKEUP: 304 priv_dev->wake_up_flag = !!set; 305 break; 306 case USB_DEVICE_U1_ENABLE: 307 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER) 308 return -EINVAL; 309 310 priv_dev->u1_allowed = !!set; 311 break; 312 case USB_DEVICE_U2_ENABLE: 313 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER) 314 return -EINVAL; 315 316 priv_dev->u2_allowed = !!set; 317 break; 318 case USB_DEVICE_LTM_ENABLE: 319 ret = -EINVAL; 320 break; 321 case USB_DEVICE_TEST_MODE: 322 if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH) 323 return -EINVAL; 324 325 tmode = le16_to_cpu(ctrl->wIndex); 326 327 if (!set || (tmode & 0xff) != 0) 328 return -EINVAL; 329 330 switch (tmode >> 8) { 331 case TEST_J: 332 case TEST_K: 333 case TEST_SE0_NAK: 334 case TEST_PACKET: 335 cdns3_ep0_complete_setup(priv_dev, 0, 1); 336 /** 337 * Little delay to give the controller some time 338 * for sending status stage. 339 * This time should be less then 3ms. 340 */ 341 mdelay(1); 342 cdns3_set_register_bit(&priv_dev->regs->usb_cmd, 343 USB_CMD_STMODE | 344 USB_STS_TMODE_SEL(tmode - 1)); 345 break; 346 default: 347 ret = -EINVAL; 348 } 349 break; 350 default: 351 ret = -EINVAL; 352 } 353 354 return ret; 355 } 356 cdns3_ep0_feature_handle_intf(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl,int set)357 static int cdns3_ep0_feature_handle_intf(struct cdns3_device *priv_dev, 358 struct usb_ctrlrequest *ctrl, 359 int set) 360 { 361 u32 wValue; 362 int ret = 0; 363 364 wValue = le16_to_cpu(ctrl->wValue); 365 366 switch (wValue) { 367 case USB_INTRF_FUNC_SUSPEND: 368 break; 369 default: 370 ret = -EINVAL; 371 } 372 373 return ret; 374 } 375 cdns3_ep0_feature_handle_endpoint(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl,int set)376 static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev, 377 struct usb_ctrlrequest *ctrl, 378 int set) 379 { 380 struct cdns3_endpoint *priv_ep; 381 int ret = 0; 382 u8 index; 383 384 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT) 385 return -EINVAL; 386 387 if (!(ctrl->wIndex & ~USB_DIR_IN)) 388 return 0; 389 390 index = cdns3_ep_addr_to_index(ctrl->wIndex); 391 priv_ep = priv_dev->eps[index]; 392 393 cdns3_select_ep(priv_dev, ctrl->wIndex); 394 395 if (set) 396 __cdns3_gadget_ep_set_halt(priv_ep); 397 else if (!(priv_ep->flags & EP_WEDGE)) 398 ret = __cdns3_gadget_ep_clear_halt(priv_ep); 399 400 cdns3_select_ep(priv_dev, 0x00); 401 402 return ret; 403 } 404 405 /** 406 * cdns3_req_ep0_handle_feature - 407 * Handling of GET/SET_FEATURE standard USB request 408 * 409 * @priv_dev: extended gadget object 410 * @ctrl_req: pointer to received setup packet 411 * @set: must be set to 1 for SET_FEATURE request 412 * 413 * Returns 0 if success, error code on error 414 */ cdns3_req_ep0_handle_feature(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl,int set)415 static int cdns3_req_ep0_handle_feature(struct cdns3_device *priv_dev, 416 struct usb_ctrlrequest *ctrl, 417 int set) 418 { 419 int ret = 0; 420 u32 recip; 421 422 recip = ctrl->bRequestType & USB_RECIP_MASK; 423 424 switch (recip) { 425 case USB_RECIP_DEVICE: 426 ret = cdns3_ep0_feature_handle_device(priv_dev, ctrl, set); 427 break; 428 case USB_RECIP_INTERFACE: 429 ret = cdns3_ep0_feature_handle_intf(priv_dev, ctrl, set); 430 break; 431 case USB_RECIP_ENDPOINT: 432 ret = cdns3_ep0_feature_handle_endpoint(priv_dev, ctrl, set); 433 break; 434 default: 435 return -EINVAL; 436 } 437 438 return ret; 439 } 440 441 /** 442 * cdns3_req_ep0_set_sel - Handling of SET_SEL standard USB request 443 * @priv_dev: extended gadget object 444 * @ctrl_req: pointer to received setup packet 445 * 446 * Returns 0 if success, error code on error 447 */ cdns3_req_ep0_set_sel(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl_req)448 static int cdns3_req_ep0_set_sel(struct cdns3_device *priv_dev, 449 struct usb_ctrlrequest *ctrl_req) 450 { 451 if (priv_dev->gadget.state < USB_STATE_ADDRESS) 452 return -EINVAL; 453 454 if (ctrl_req->wLength != 6) { 455 dev_err(priv_dev->dev, "Set SEL should be 6 bytes, got %d\n", 456 ctrl_req->wLength); 457 return -EINVAL; 458 } 459 460 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 6, 1, 0); 461 return 0; 462 } 463 464 /** 465 * cdns3_req_ep0_set_isoch_delay - 466 * Handling of GET_ISOCH_DELAY standard USB request 467 * @priv_dev: extended gadget object 468 * @ctrl_req: pointer to received setup packet 469 * 470 * Returns 0 if success, error code on error 471 */ cdns3_req_ep0_set_isoch_delay(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl_req)472 static int cdns3_req_ep0_set_isoch_delay(struct cdns3_device *priv_dev, 473 struct usb_ctrlrequest *ctrl_req) 474 { 475 if (ctrl_req->wIndex || ctrl_req->wLength) 476 return -EINVAL; 477 478 priv_dev->isoch_delay = ctrl_req->wValue; 479 480 return 0; 481 } 482 483 /** 484 * cdns3_ep0_standard_request - Handling standard USB requests 485 * @priv_dev: extended gadget object 486 * @ctrl_req: pointer to received setup packet 487 * 488 * Returns 0 if success, error code on error 489 */ cdns3_ep0_standard_request(struct cdns3_device * priv_dev,struct usb_ctrlrequest * ctrl_req)490 static int cdns3_ep0_standard_request(struct cdns3_device *priv_dev, 491 struct usb_ctrlrequest *ctrl_req) 492 { 493 int ret; 494 495 switch (ctrl_req->bRequest) { 496 case USB_REQ_SET_ADDRESS: 497 ret = cdns3_req_ep0_set_address(priv_dev, ctrl_req); 498 break; 499 case USB_REQ_SET_CONFIGURATION: 500 ret = cdns3_req_ep0_set_configuration(priv_dev, ctrl_req); 501 break; 502 case USB_REQ_GET_STATUS: 503 ret = cdns3_req_ep0_get_status(priv_dev, ctrl_req); 504 break; 505 case USB_REQ_CLEAR_FEATURE: 506 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 0); 507 break; 508 case USB_REQ_SET_FEATURE: 509 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 1); 510 break; 511 case USB_REQ_SET_SEL: 512 ret = cdns3_req_ep0_set_sel(priv_dev, ctrl_req); 513 break; 514 case USB_REQ_SET_ISOCH_DELAY: 515 ret = cdns3_req_ep0_set_isoch_delay(priv_dev, ctrl_req); 516 break; 517 default: 518 ret = cdns3_ep0_delegate_req(priv_dev, ctrl_req); 519 break; 520 } 521 522 return ret; 523 } 524 __pending_setup_status_handler(struct cdns3_device * priv_dev)525 static void __pending_setup_status_handler(struct cdns3_device *priv_dev) 526 { 527 struct usb_request *request = priv_dev->pending_status_request; 528 529 if (priv_dev->status_completion_no_call && request && 530 request->complete) { 531 request->complete(&priv_dev->eps[0]->endpoint, request); 532 priv_dev->status_completion_no_call = 0; 533 } 534 } 535 cdns3_pending_setup_status_handler(struct work_struct * work)536 void cdns3_pending_setup_status_handler(struct work_struct *work) 537 { 538 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device, 539 pending_status_wq); 540 unsigned long flags; 541 542 spin_lock_irqsave(&priv_dev->lock, flags); 543 __pending_setup_status_handler(priv_dev); 544 spin_unlock_irqrestore(&priv_dev->lock, flags); 545 } 546 547 /** 548 * cdns3_ep0_setup_phase - Handling setup USB requests 549 * @priv_dev: extended gadget object 550 */ cdns3_ep0_setup_phase(struct cdns3_device * priv_dev)551 static void cdns3_ep0_setup_phase(struct cdns3_device *priv_dev) 552 { 553 struct usb_ctrlrequest *ctrl = priv_dev->setup_buf; 554 struct cdns3_endpoint *priv_ep = priv_dev->eps[0]; 555 int result; 556 557 priv_dev->ep0_data_dir = ctrl->bRequestType & USB_DIR_IN; 558 559 trace_cdns3_ctrl_req(ctrl); 560 561 if (!list_empty(&priv_ep->pending_req_list)) { 562 struct usb_request *request; 563 564 request = cdns3_next_request(&priv_ep->pending_req_list); 565 priv_ep->dir = priv_dev->ep0_data_dir; 566 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 567 -ECONNRESET); 568 } 569 570 if (le16_to_cpu(ctrl->wLength)) 571 priv_dev->ep0_stage = CDNS3_DATA_STAGE; 572 else 573 priv_dev->ep0_stage = CDNS3_STATUS_STAGE; 574 575 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) 576 result = cdns3_ep0_standard_request(priv_dev, ctrl); 577 else 578 result = cdns3_ep0_delegate_req(priv_dev, ctrl); 579 580 if (result == USB_GADGET_DELAYED_STATUS) 581 return; 582 583 if (result < 0) 584 cdns3_ep0_complete_setup(priv_dev, 1, 1); 585 else if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) 586 cdns3_ep0_complete_setup(priv_dev, 0, 1); 587 } 588 cdns3_transfer_completed(struct cdns3_device * priv_dev)589 static void cdns3_transfer_completed(struct cdns3_device *priv_dev) 590 { 591 struct cdns3_endpoint *priv_ep = priv_dev->eps[0]; 592 593 if (!list_empty(&priv_ep->pending_req_list)) { 594 struct usb_request *request; 595 596 trace_cdns3_complete_trb(priv_ep, priv_ep->trb_pool); 597 request = cdns3_next_request(&priv_ep->pending_req_list); 598 599 request->actual = 600 TRB_LEN(le32_to_cpu(priv_ep->trb_pool->length)); 601 602 priv_ep->dir = priv_dev->ep0_data_dir; 603 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 0); 604 } 605 606 cdns3_ep0_complete_setup(priv_dev, 0, 0); 607 } 608 609 /** 610 * cdns3_check_new_setup - Check if controller receive new SETUP packet. 611 * @priv_dev: extended gadget object 612 * 613 * The SETUP packet can be kept in on-chip memory or in system memory. 614 */ cdns3_check_new_setup(struct cdns3_device * priv_dev)615 static bool cdns3_check_new_setup(struct cdns3_device *priv_dev) 616 { 617 u32 ep_sts_reg; 618 619 cdns3_select_ep(priv_dev, 0 | USB_DIR_OUT); 620 ep_sts_reg = readl(&priv_dev->regs->ep_sts); 621 622 return !!(ep_sts_reg & (EP_STS_SETUP | EP_STS_STPWAIT)); 623 } 624 625 /** 626 * cdns3_check_ep0_interrupt_proceed - Processes interrupt related to endpoint 0 627 * @priv_dev: extended gadget object 628 * @dir: USB_DIR_IN for IN direction, USB_DIR_OUT for OUT direction 629 */ cdns3_check_ep0_interrupt_proceed(struct cdns3_device * priv_dev,int dir)630 void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir) 631 { 632 u32 ep_sts_reg; 633 634 cdns3_select_ep(priv_dev, dir); 635 636 ep_sts_reg = readl(&priv_dev->regs->ep_sts); 637 writel(ep_sts_reg, &priv_dev->regs->ep_sts); 638 639 trace_cdns3_ep0_irq(priv_dev, ep_sts_reg); 640 641 __pending_setup_status_handler(priv_dev); 642 643 if (ep_sts_reg & EP_STS_SETUP) 644 priv_dev->wait_for_setup = 1; 645 646 if (priv_dev->wait_for_setup && ep_sts_reg & EP_STS_IOC) { 647 priv_dev->wait_for_setup = 0; 648 cdns3_allow_enable_l1(priv_dev, 0); 649 cdns3_ep0_setup_phase(priv_dev); 650 } else if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP)) { 651 priv_dev->ep0_data_dir = dir; 652 cdns3_transfer_completed(priv_dev); 653 } 654 655 if (ep_sts_reg & EP_STS_DESCMIS) { 656 if (dir == 0 && !priv_dev->setup_pending) 657 cdns3_prepare_setup_packet(priv_dev); 658 } 659 } 660 661 /** 662 * cdns3_gadget_ep0_enable 663 * Function shouldn't be called by gadget driver, 664 * endpoint 0 is allways active 665 */ cdns3_gadget_ep0_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)666 static int cdns3_gadget_ep0_enable(struct usb_ep *ep, 667 const struct usb_endpoint_descriptor *desc) 668 { 669 return -EINVAL; 670 } 671 672 /** 673 * cdns3_gadget_ep0_disable 674 * Function shouldn't be called by gadget driver, 675 * endpoint 0 is allways active 676 */ cdns3_gadget_ep0_disable(struct usb_ep * ep)677 static int cdns3_gadget_ep0_disable(struct usb_ep *ep) 678 { 679 return -EINVAL; 680 } 681 682 /** 683 * cdns3_gadget_ep0_set_halt 684 * @ep: pointer to endpoint zero object 685 * @value: 1 for set stall, 0 for clear stall 686 * 687 * Returns 0 688 */ cdns3_gadget_ep0_set_halt(struct usb_ep * ep,int value)689 static int cdns3_gadget_ep0_set_halt(struct usb_ep *ep, int value) 690 { 691 /* TODO */ 692 return 0; 693 } 694 695 /** 696 * cdns3_gadget_ep0_queue Transfer data on endpoint zero 697 * @ep: pointer to endpoint zero object 698 * @request: pointer to request object 699 * @gfp_flags: gfp flags 700 * 701 * Returns 0 on success, error code elsewhere 702 */ cdns3_gadget_ep0_queue(struct usb_ep * ep,struct usb_request * request,gfp_t gfp_flags)703 static int cdns3_gadget_ep0_queue(struct usb_ep *ep, 704 struct usb_request *request, 705 gfp_t gfp_flags) 706 { 707 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep); 708 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 709 unsigned long flags; 710 int erdy_sent = 0; 711 int ret = 0; 712 u8 zlp = 0; 713 714 trace_cdns3_ep0_queue(priv_dev, request); 715 716 /* cancel the request if controller receive new SETUP packet. */ 717 if (cdns3_check_new_setup(priv_dev)) 718 return -ECONNRESET; 719 720 /* send STATUS stage. Should be called only for SET_CONFIGURATION */ 721 if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) { 722 spin_lock_irqsave(&priv_dev->lock, flags); 723 cdns3_select_ep(priv_dev, 0x00); 724 725 erdy_sent = !priv_dev->hw_configured_flag; 726 cdns3_set_hw_configuration(priv_dev); 727 728 if (!erdy_sent) 729 cdns3_ep0_complete_setup(priv_dev, 0, 1); 730 731 cdns3_allow_enable_l1(priv_dev, 1); 732 733 request->actual = 0; 734 priv_dev->status_completion_no_call = true; 735 priv_dev->pending_status_request = request; 736 spin_unlock_irqrestore(&priv_dev->lock, flags); 737 738 /* 739 * Since there is no completion interrupt for status stage, 740 * it needs to call ->completion in software after 741 * ep0_queue is back. 742 */ 743 queue_work(system_freezable_wq, &priv_dev->pending_status_wq); 744 return 0; 745 } 746 747 spin_lock_irqsave(&priv_dev->lock, flags); 748 if (!list_empty(&priv_ep->pending_req_list)) { 749 dev_err(priv_dev->dev, 750 "can't handle multiple requests for ep0\n"); 751 spin_unlock_irqrestore(&priv_dev->lock, flags); 752 return -EBUSY; 753 } 754 755 ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request, 756 priv_dev->ep0_data_dir); 757 if (ret) { 758 spin_unlock_irqrestore(&priv_dev->lock, flags); 759 dev_err(priv_dev->dev, "failed to map request\n"); 760 return -EINVAL; 761 } 762 763 request->status = -EINPROGRESS; 764 list_add_tail(&request->list, &priv_ep->pending_req_list); 765 766 if (request->zero && request->length && 767 (request->length % ep->maxpacket == 0)) 768 zlp = 1; 769 770 cdns3_ep0_run_transfer(priv_dev, request->dma, request->length, 1, zlp); 771 772 spin_unlock_irqrestore(&priv_dev->lock, flags); 773 774 return ret; 775 } 776 777 /** 778 * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint 779 * @ep: endpoint object 780 * 781 * Returns 0 782 */ cdns3_gadget_ep_set_wedge(struct usb_ep * ep)783 int cdns3_gadget_ep_set_wedge(struct usb_ep *ep) 784 { 785 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep); 786 struct cdns3_device *priv_dev = priv_ep->cdns3_dev; 787 788 dev_dbg(priv_dev->dev, "Wedge for %s\n", ep->name); 789 cdns3_gadget_ep_set_halt(ep, 1); 790 priv_ep->flags |= EP_WEDGE; 791 792 return 0; 793 } 794 795 const struct usb_ep_ops cdns3_gadget_ep0_ops = { 796 .enable = cdns3_gadget_ep0_enable, 797 .disable = cdns3_gadget_ep0_disable, 798 .alloc_request = cdns3_gadget_ep_alloc_request, 799 .free_request = cdns3_gadget_ep_free_request, 800 .queue = cdns3_gadget_ep0_queue, 801 .dequeue = cdns3_gadget_ep_dequeue, 802 .set_halt = cdns3_gadget_ep0_set_halt, 803 .set_wedge = cdns3_gadget_ep_set_wedge, 804 }; 805 806 /** 807 * cdns3_ep0_config - Configures default endpoint 808 * @priv_dev: extended gadget object 809 * 810 * Functions sets parameters: maximal packet size and enables interrupts 811 */ cdns3_ep0_config(struct cdns3_device * priv_dev)812 void cdns3_ep0_config(struct cdns3_device *priv_dev) 813 { 814 struct cdns3_usb_regs __iomem *regs; 815 struct cdns3_endpoint *priv_ep; 816 u32 max_packet_size = 64; 817 818 regs = priv_dev->regs; 819 820 if (priv_dev->gadget.speed == USB_SPEED_SUPER) 821 max_packet_size = 512; 822 823 priv_ep = priv_dev->eps[0]; 824 825 if (!list_empty(&priv_ep->pending_req_list)) { 826 struct usb_request *request; 827 828 request = cdns3_next_request(&priv_ep->pending_req_list); 829 list_del_init(&request->list); 830 } 831 832 priv_dev->u1_allowed = 0; 833 priv_dev->u2_allowed = 0; 834 835 priv_dev->gadget.ep0->maxpacket = max_packet_size; 836 cdns3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(max_packet_size); 837 838 /* init ep out */ 839 cdns3_select_ep(priv_dev, USB_DIR_OUT); 840 841 if (priv_dev->dev_ver >= DEV_VER_V3) { 842 cdns3_set_register_bit(&priv_dev->regs->dtrans, 843 BIT(0) | BIT(16)); 844 cdns3_set_register_bit(&priv_dev->regs->tdl_from_trb, 845 BIT(0) | BIT(16)); 846 } 847 848 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size), 849 ®s->ep_cfg); 850 851 writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN, 852 ®s->ep_sts_en); 853 854 /* init ep in */ 855 cdns3_select_ep(priv_dev, USB_DIR_IN); 856 857 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size), 858 ®s->ep_cfg); 859 860 writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, ®s->ep_sts_en); 861 862 cdns3_set_register_bit(®s->usb_conf, USB_CONF_U1DS | USB_CONF_U2DS); 863 } 864 865 /** 866 * cdns3_init_ep0 Initializes software endpoint 0 of gadget 867 * @priv_dev: extended gadget object 868 * @ep_priv: extended endpoint object 869 * 870 * Returns 0 on success else error code. 871 */ cdns3_init_ep0(struct cdns3_device * priv_dev,struct cdns3_endpoint * priv_ep)872 int cdns3_init_ep0(struct cdns3_device *priv_dev, 873 struct cdns3_endpoint *priv_ep) 874 { 875 sprintf(priv_ep->name, "ep0"); 876 877 /* fill linux fields */ 878 priv_ep->endpoint.ops = &cdns3_gadget_ep0_ops; 879 priv_ep->endpoint.maxburst = 1; 880 usb_ep_set_maxpacket_limit(&priv_ep->endpoint, 881 CDNS3_EP0_MAX_PACKET_LIMIT); 882 priv_ep->endpoint.address = 0; 883 priv_ep->endpoint.caps.type_control = 1; 884 priv_ep->endpoint.caps.dir_in = 1; 885 priv_ep->endpoint.caps.dir_out = 1; 886 priv_ep->endpoint.name = priv_ep->name; 887 priv_ep->endpoint.desc = &cdns3_gadget_ep0_desc; 888 priv_dev->gadget.ep0 = &priv_ep->endpoint; 889 priv_ep->type = USB_ENDPOINT_XFER_CONTROL; 890 891 return cdns3_allocate_trb_pool(priv_ep); 892 } 893