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