Chunfeng Yun | e09b88c | 2020-10-16 11:38:39 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * mtu3_gadget.c - MediaTek usb3 DRD peripheral support |
| 4 | * |
| 5 | * Copyright (C) 2016 MediaTek Inc. |
| 6 | * |
| 7 | * Author: Chunfeng Yun <chunfeng.yun@mediatek.com> |
| 8 | */ |
| 9 | |
| 10 | #include "mtu3.h" |
| 11 | |
| 12 | void mtu3_req_complete(struct mtu3_ep *mep, |
| 13 | struct usb_request *req, int status) |
| 14 | __releases(mep->mtu->lock) |
| 15 | __acquires(mep->mtu->lock) |
| 16 | { |
| 17 | struct mtu3_request *mreq = to_mtu3_request(req); |
| 18 | struct mtu3 *mtu = mreq->mtu; |
| 19 | |
| 20 | list_del(&mreq->list); |
| 21 | if (req->status == -EINPROGRESS) |
| 22 | req->status = status; |
| 23 | |
| 24 | spin_unlock(&mtu->lock); |
| 25 | |
| 26 | /* ep0 makes use of PIO, needn't unmap it */ |
| 27 | if (mep->epnum) |
| 28 | usb_gadget_unmap_request(&mtu->g, req, mep->is_in); |
| 29 | |
| 30 | dev_dbg(mtu->dev, "%s complete req: %p, sts %d, %d/%d\n", |
| 31 | mep->name, req, req->status, req->actual, req->length); |
| 32 | |
| 33 | usb_gadget_giveback_request(&mep->ep, req); |
| 34 | spin_lock(&mtu->lock); |
| 35 | } |
| 36 | |
| 37 | static void nuke(struct mtu3_ep *mep, const int status) |
| 38 | { |
| 39 | struct mtu3_request *mreq = NULL; |
| 40 | |
| 41 | if (list_empty(&mep->req_list)) |
| 42 | return; |
| 43 | |
| 44 | dev_dbg(mep->mtu->dev, "abort %s's req: sts %d\n", mep->name, status); |
| 45 | |
| 46 | /* exclude EP0 */ |
| 47 | if (mep->epnum) |
| 48 | mtu3_qmu_flush(mep); |
| 49 | |
| 50 | while (!list_empty(&mep->req_list)) { |
| 51 | mreq = list_first_entry(&mep->req_list, |
| 52 | struct mtu3_request, list); |
| 53 | mtu3_req_complete(mep, &mreq->request, status); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static int mtu3_ep_enable(struct mtu3_ep *mep) |
| 58 | { |
| 59 | const struct usb_endpoint_descriptor *desc; |
| 60 | const struct usb_ss_ep_comp_descriptor *comp_desc; |
| 61 | struct mtu3 *mtu = mep->mtu; |
| 62 | u32 interval = 0; |
| 63 | u32 mult = 0; |
| 64 | u32 burst = 0; |
| 65 | int max_packet; |
| 66 | int ret; |
| 67 | |
| 68 | desc = mep->desc; |
| 69 | comp_desc = mep->comp_desc; |
| 70 | mep->type = usb_endpoint_type(desc); |
| 71 | max_packet = usb_endpoint_maxp(desc); |
| 72 | mep->maxp = max_packet & GENMASK(10, 0); |
| 73 | |
| 74 | switch (mtu->g.speed) { |
| 75 | case USB_SPEED_SUPER: |
| 76 | case USB_SPEED_SUPER_PLUS: |
| 77 | if (usb_endpoint_xfer_int(desc) || |
| 78 | usb_endpoint_xfer_isoc(desc)) { |
| 79 | interval = desc->bInterval; |
| 80 | interval = clamp_val(interval, 1, 16) - 1; |
| 81 | if (usb_endpoint_xfer_isoc(desc) && comp_desc) |
| 82 | mult = comp_desc->bmAttributes; |
| 83 | } |
| 84 | if (comp_desc) |
| 85 | burst = comp_desc->bMaxBurst; |
| 86 | |
| 87 | break; |
| 88 | case USB_SPEED_HIGH: |
| 89 | if (usb_endpoint_xfer_isoc(desc) || |
| 90 | usb_endpoint_xfer_int(desc)) { |
| 91 | interval = desc->bInterval; |
| 92 | interval = clamp_val(interval, 1, 16) - 1; |
| 93 | burst = (max_packet & GENMASK(12, 11)) >> 11; |
| 94 | } |
| 95 | break; |
| 96 | default: |
| 97 | break; /*others are ignored */ |
| 98 | } |
| 99 | |
| 100 | dev_dbg(mtu->dev, "%s maxp:%d, interval:%d, burst:%d, mult:%d\n", |
| 101 | __func__, mep->maxp, interval, burst, mult); |
| 102 | |
| 103 | mep->ep.maxpacket = mep->maxp; |
| 104 | mep->ep.desc = desc; |
| 105 | mep->ep.comp_desc = comp_desc; |
| 106 | mep->slot = mtu->slot; |
| 107 | |
| 108 | ret = mtu3_config_ep(mtu, mep, interval, burst, mult); |
| 109 | if (ret < 0) |
| 110 | return ret; |
| 111 | |
| 112 | ret = mtu3_gpd_ring_alloc(mep); |
| 113 | if (ret < 0) { |
| 114 | mtu3_deconfig_ep(mtu, mep); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | mtu3_qmu_start(mep); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int mtu3_ep_disable(struct mtu3_ep *mep) |
| 124 | { |
| 125 | struct mtu3 *mtu = mep->mtu; |
| 126 | |
| 127 | mtu3_qmu_stop(mep); |
| 128 | |
| 129 | /* abort all pending requests */ |
| 130 | nuke(mep, -ESHUTDOWN); |
| 131 | mtu3_deconfig_ep(mtu, mep); |
| 132 | mtu3_gpd_ring_free(mep); |
| 133 | |
| 134 | mep->desc = NULL; |
| 135 | mep->ep.desc = NULL; |
| 136 | mep->comp_desc = NULL; |
| 137 | mep->type = 0; |
| 138 | mep->flags = 0; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int mtu3_gadget_ep_enable(struct usb_ep *ep, |
| 144 | const struct usb_endpoint_descriptor *desc) |
| 145 | { |
| 146 | struct mtu3_ep *mep; |
| 147 | struct mtu3 *mtu; |
| 148 | unsigned long flags; |
| 149 | int ret = -EINVAL; |
| 150 | |
| 151 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { |
| 152 | pr_debug("%s invalid parameters\n", __func__); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | |
| 156 | if (!desc->wMaxPacketSize) { |
| 157 | pr_debug("%s missing wMaxPacketSize\n", __func__); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | mep = to_mtu3_ep(ep); |
| 161 | mtu = mep->mtu; |
| 162 | |
| 163 | /* check ep number and direction against endpoint */ |
| 164 | if (usb_endpoint_num(desc) != mep->epnum) |
| 165 | return -EINVAL; |
| 166 | |
| 167 | if (!!usb_endpoint_dir_in(desc) ^ !!mep->is_in) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | dev_dbg(mtu->dev, "%s %s\n", __func__, ep->name); |
| 171 | |
| 172 | if (mep->flags & MTU3_EP_ENABLED) { |
| 173 | dev_warn(mtu->dev, "%s is already enabled\n", mep->name); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | spin_lock_irqsave(&mtu->lock, flags); |
| 178 | mep->desc = desc; |
| 179 | mep->comp_desc = ep->comp_desc; |
| 180 | |
| 181 | ret = mtu3_ep_enable(mep); |
| 182 | if (ret) |
| 183 | goto error; |
| 184 | |
| 185 | mep->flags = MTU3_EP_ENABLED; |
| 186 | mtu->active_ep++; |
| 187 | |
| 188 | error: |
| 189 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 190 | |
| 191 | dev_dbg(mtu->dev, "%s active_ep=%d\n", __func__, mtu->active_ep); |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static int mtu3_gadget_ep_disable(struct usb_ep *ep) |
| 197 | { |
| 198 | struct mtu3_ep *mep = to_mtu3_ep(ep); |
| 199 | struct mtu3 *mtu = mep->mtu; |
| 200 | unsigned long flags; |
| 201 | |
| 202 | dev_dbg(mtu->dev, "%s %s\n", __func__, mep->name); |
| 203 | |
| 204 | if (!(mep->flags & MTU3_EP_ENABLED)) { |
| 205 | dev_warn(mtu->dev, "%s is already disabled\n", mep->name); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | spin_lock_irqsave(&mtu->lock, flags); |
| 210 | mtu3_ep_disable(mep); |
| 211 | mep->flags = 0; |
| 212 | mtu->active_ep--; |
| 213 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 214 | |
| 215 | dev_dbg(mtu->dev, "%s active_ep=%d, mtu3 is_active=%d\n", |
| 216 | __func__, mtu->active_ep, mtu->is_active); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | struct usb_request *mtu3_alloc_request(struct usb_ep *ep, gfp_t gfp_flags) |
| 222 | { |
| 223 | struct mtu3_ep *mep = to_mtu3_ep(ep); |
| 224 | struct mtu3_request *mreq; |
| 225 | |
| 226 | mreq = kzalloc(sizeof(*mreq), gfp_flags); |
| 227 | if (!mreq) |
| 228 | return NULL; |
| 229 | |
| 230 | mreq->request.dma = DMA_ADDR_INVALID; |
| 231 | mreq->epnum = mep->epnum; |
| 232 | mreq->mep = mep; |
| 233 | |
| 234 | return &mreq->request; |
| 235 | } |
| 236 | |
| 237 | void mtu3_free_request(struct usb_ep *ep, struct usb_request *req) |
| 238 | { |
| 239 | struct mtu3_request *mreq = to_mtu3_request(req); |
| 240 | |
| 241 | kfree(mreq); |
| 242 | } |
| 243 | |
| 244 | static int mtu3_gadget_queue(struct usb_ep *ep, |
| 245 | struct usb_request *req, gfp_t gfp_flags) |
| 246 | { |
| 247 | struct mtu3_ep *mep = to_mtu3_ep(ep); |
| 248 | struct mtu3_request *mreq = to_mtu3_request(req); |
| 249 | struct mtu3 *mtu = mep->mtu; |
| 250 | unsigned long flags; |
| 251 | int ret = 0; |
| 252 | |
| 253 | if (!req->buf) |
| 254 | return -ENODATA; |
| 255 | |
| 256 | if (mreq->mep != mep) |
| 257 | return -EINVAL; |
| 258 | |
| 259 | dev_dbg(mtu->dev, "%s %s EP%d(%s), req=%p, maxp=%d, len#%d\n", |
| 260 | __func__, mep->is_in ? "TX" : "RX", mreq->epnum, ep->name, |
| 261 | mreq, ep->maxpacket, mreq->request.length); |
| 262 | |
| 263 | if (req->length > GPD_BUF_SIZE) { |
| 264 | dev_warn(mtu->dev, |
| 265 | "req length > supported MAX:%d requested:%d\n", |
| 266 | GPD_BUF_SIZE, req->length); |
| 267 | return -EOPNOTSUPP; |
| 268 | } |
| 269 | |
| 270 | /* don't queue if the ep is down */ |
| 271 | if (!mep->desc) { |
| 272 | dev_dbg(mtu->dev, "req=%p queued to %s while it's disabled\n", |
| 273 | req, ep->name); |
| 274 | return -ESHUTDOWN; |
| 275 | } |
| 276 | |
| 277 | mreq->mtu = mtu; |
| 278 | mreq->request.actual = 0; |
| 279 | mreq->request.status = -EINPROGRESS; |
| 280 | |
| 281 | ret = usb_gadget_map_request(&mtu->g, req, mep->is_in); |
| 282 | if (ret) { |
| 283 | dev_err(mtu->dev, "dma mapping failed\n"); |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | spin_lock_irqsave(&mtu->lock, flags); |
| 288 | |
| 289 | if (mtu3_prepare_transfer(mep)) { |
| 290 | ret = -EAGAIN; |
| 291 | goto error; |
| 292 | } |
| 293 | |
| 294 | list_add_tail(&mreq->list, &mep->req_list); |
| 295 | mtu3_insert_gpd(mep, mreq); |
| 296 | mtu3_qmu_resume(mep); |
| 297 | |
| 298 | error: |
| 299 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | static int mtu3_gadget_dequeue(struct usb_ep *ep, struct usb_request *req) |
| 305 | { |
| 306 | struct mtu3_ep *mep = to_mtu3_ep(ep); |
| 307 | struct mtu3_request *mreq = to_mtu3_request(req); |
| 308 | struct mtu3_request *r; |
| 309 | struct mtu3 *mtu = mep->mtu; |
| 310 | unsigned long flags; |
| 311 | int ret = 0; |
| 312 | |
| 313 | if (mreq->mep != mep) |
| 314 | return -EINVAL; |
| 315 | |
| 316 | dev_dbg(mtu->dev, "%s : req=%p\n", __func__, req); |
| 317 | |
| 318 | spin_lock_irqsave(&mtu->lock, flags); |
| 319 | |
| 320 | list_for_each_entry(r, &mep->req_list, list) { |
| 321 | if (r == mreq) |
| 322 | break; |
| 323 | } |
| 324 | if (r != mreq) { |
| 325 | dev_dbg(mtu->dev, "req=%p not queued to %s\n", req, ep->name); |
| 326 | ret = -EINVAL; |
| 327 | goto done; |
| 328 | } |
| 329 | |
| 330 | mtu3_qmu_flush(mep); /* REVISIT: set BPS ?? */ |
| 331 | mtu3_req_complete(mep, req, -ECONNRESET); |
| 332 | mtu3_qmu_start(mep); |
| 333 | |
| 334 | done: |
| 335 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Set or clear the halt bit of an EP. |
| 342 | * A halted EP won't TX/RX any data but will queue requests. |
| 343 | */ |
| 344 | static int mtu3_gadget_ep_set_halt(struct usb_ep *ep, int value) |
| 345 | { |
| 346 | struct mtu3_ep *mep = to_mtu3_ep(ep); |
| 347 | struct mtu3 *mtu = mep->mtu; |
| 348 | struct mtu3_request *mreq; |
| 349 | unsigned long flags = 0; |
| 350 | int ret = 0; |
| 351 | |
| 352 | dev_dbg(mtu->dev, "%s : %s...", __func__, ep->name); |
| 353 | |
| 354 | spin_lock_irqsave(&mtu->lock, flags); |
| 355 | |
| 356 | if (mep->type == USB_ENDPOINT_XFER_ISOC) { |
| 357 | ret = -EINVAL; |
| 358 | goto done; |
| 359 | } |
| 360 | |
| 361 | mreq = next_request(mep); |
| 362 | if (value) { |
| 363 | /* |
| 364 | * If there is not request for TX-EP, QMU will not transfer |
| 365 | * data to TX-FIFO, so no need check whether TX-FIFO |
| 366 | * holds bytes or not here |
| 367 | */ |
| 368 | if (mreq) { |
| 369 | dev_dbg(mtu->dev, "req in progress, cannot halt %s\n", |
| 370 | ep->name); |
| 371 | ret = -EAGAIN; |
| 372 | goto done; |
| 373 | } |
| 374 | } else { |
| 375 | mep->flags &= ~MTU3_EP_WEDGE; |
| 376 | } |
| 377 | |
| 378 | dev_dbg(mtu->dev, "%s %s stall\n", ep->name, value ? "set" : "clear"); |
| 379 | |
| 380 | mtu3_ep_stall_set(mep, value); |
| 381 | |
| 382 | done: |
| 383 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 384 | |
| 385 | return ret; |
| 386 | } |
| 387 | |
| 388 | /* Sets the halt feature with the clear requests ignored */ |
| 389 | static int mtu3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 390 | { |
| 391 | struct mtu3_ep *mep = to_mtu3_ep(ep); |
| 392 | |
| 393 | mep->flags |= MTU3_EP_WEDGE; |
| 394 | |
| 395 | return usb_ep_set_halt(ep); |
| 396 | } |
| 397 | |
| 398 | static const struct usb_ep_ops mtu3_ep_ops = { |
| 399 | .enable = mtu3_gadget_ep_enable, |
| 400 | .disable = mtu3_gadget_ep_disable, |
| 401 | .alloc_request = mtu3_alloc_request, |
| 402 | .free_request = mtu3_free_request, |
| 403 | .queue = mtu3_gadget_queue, |
| 404 | .dequeue = mtu3_gadget_dequeue, |
| 405 | .set_halt = mtu3_gadget_ep_set_halt, |
| 406 | .set_wedge = mtu3_gadget_ep_set_wedge, |
| 407 | }; |
| 408 | |
| 409 | static int mtu3_gadget_get_frame(struct usb_gadget *gadget) |
| 410 | { |
| 411 | struct mtu3 *mtu = gadget_to_mtu3(gadget); |
| 412 | |
| 413 | return (int)mtu3_readl(mtu->mac_base, U3D_USB20_FRAME_NUM); |
| 414 | } |
| 415 | |
| 416 | static int mtu3_gadget_wakeup(struct usb_gadget *gadget) |
| 417 | { |
| 418 | struct mtu3 *mtu = gadget_to_mtu3(gadget); |
| 419 | unsigned long flags; |
| 420 | |
| 421 | dev_dbg(mtu->dev, "%s\n", __func__); |
| 422 | |
| 423 | /* remote wakeup feature is not enabled by host */ |
| 424 | if (!mtu->may_wakeup) |
| 425 | return -EOPNOTSUPP; |
| 426 | |
| 427 | spin_lock_irqsave(&mtu->lock, flags); |
| 428 | if (mtu->g.speed >= USB_SPEED_SUPER) { |
| 429 | mtu3_setbits(mtu->mac_base, U3D_LINK_POWER_CONTROL, UX_EXIT); |
| 430 | } else { |
| 431 | mtu3_setbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME); |
| 432 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 433 | mdelay(10); |
| 434 | spin_lock_irqsave(&mtu->lock, flags); |
| 435 | mtu3_clrbits(mtu->mac_base, U3D_POWER_MANAGEMENT, RESUME); |
| 436 | } |
| 437 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static int mtu3_gadget_set_self_powered(struct usb_gadget *gadget, |
| 442 | int is_selfpowered) |
| 443 | { |
| 444 | struct mtu3 *mtu = gadget_to_mtu3(gadget); |
| 445 | |
| 446 | mtu->is_self_powered = !!is_selfpowered; |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int mtu3_gadget_pullup(struct usb_gadget *gadget, int is_on) |
| 451 | { |
| 452 | struct mtu3 *mtu = gadget_to_mtu3(gadget); |
| 453 | unsigned long flags; |
| 454 | |
| 455 | dev_dbg(mtu->dev, "%s (%s) for %sactive device\n", __func__, |
| 456 | is_on ? "on" : "off", mtu->is_active ? "" : "in"); |
| 457 | |
| 458 | /* we'd rather not pullup unless the device is active. */ |
| 459 | spin_lock_irqsave(&mtu->lock, flags); |
| 460 | |
| 461 | is_on = !!is_on; |
| 462 | if (!mtu->is_active) { |
| 463 | /* save it for mtu3_start() to process the request */ |
| 464 | mtu->softconnect = is_on; |
| 465 | } else if (is_on != mtu->softconnect) { |
| 466 | mtu->softconnect = is_on; |
| 467 | mtu3_dev_on_off(mtu, is_on); |
| 468 | } |
| 469 | |
| 470 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static int mtu3_gadget_start(struct usb_gadget *gadget, |
| 476 | struct usb_gadget_driver *driver) |
| 477 | { |
| 478 | struct mtu3 *mtu = gadget_to_mtu3(gadget); |
| 479 | unsigned long flags; |
| 480 | |
| 481 | if (mtu->gadget_driver) { |
| 482 | dev_err(mtu->dev, "%s is already bound to %s\n", |
| 483 | mtu->g.name, mtu->gadget_driver->function); |
| 484 | return -EBUSY; |
| 485 | } |
| 486 | |
| 487 | dev_dbg(mtu->dev, "bind driver %s\n", driver->function); |
| 488 | |
| 489 | spin_lock_irqsave(&mtu->lock, flags); |
| 490 | |
| 491 | mtu->softconnect = 0; |
| 492 | mtu->gadget_driver = driver; |
| 493 | mtu3_start(mtu); |
| 494 | |
| 495 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | static void stop_activity(struct mtu3 *mtu) |
| 501 | { |
| 502 | int i; |
| 503 | |
| 504 | mtu->g.speed = USB_SPEED_UNKNOWN; |
| 505 | |
| 506 | /* deactivate the hardware */ |
| 507 | if (mtu->softconnect) { |
| 508 | mtu->softconnect = 0; |
| 509 | mtu3_dev_on_off(mtu, 0); |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * killing any outstanding requests will quiesce the driver; |
| 514 | * then report disconnect |
| 515 | */ |
| 516 | nuke(mtu->ep0, -ESHUTDOWN); |
| 517 | for (i = 1; i < mtu->num_eps; i++) { |
| 518 | nuke(mtu->in_eps + i, -ESHUTDOWN); |
| 519 | nuke(mtu->out_eps + i, -ESHUTDOWN); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static int mtu3_gadget_stop(struct usb_gadget *g) |
| 524 | { |
| 525 | struct mtu3 *mtu = gadget_to_mtu3(g); |
| 526 | unsigned long flags; |
| 527 | |
| 528 | dev_dbg(mtu->dev, "%s\n", __func__); |
| 529 | |
| 530 | spin_lock_irqsave(&mtu->lock, flags); |
| 531 | |
| 532 | stop_activity(mtu); |
| 533 | mtu->gadget_driver = NULL; |
| 534 | mtu3_stop(mtu); |
| 535 | |
| 536 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static void |
| 542 | mtu3_gadget_set_speed(struct usb_gadget *g, enum usb_device_speed speed) |
| 543 | { |
| 544 | struct mtu3 *mtu = gadget_to_mtu3(g); |
| 545 | unsigned long flags; |
| 546 | |
| 547 | dev_dbg(mtu->dev, "%s %d\n", __func__, speed); |
| 548 | |
| 549 | spin_lock_irqsave(&mtu->lock, flags); |
| 550 | mtu3_set_speed(mtu, speed); |
| 551 | spin_unlock_irqrestore(&mtu->lock, flags); |
| 552 | } |
| 553 | |
| 554 | static const struct usb_gadget_ops mtu3_gadget_ops = { |
| 555 | .get_frame = mtu3_gadget_get_frame, |
| 556 | .wakeup = mtu3_gadget_wakeup, |
| 557 | .set_selfpowered = mtu3_gadget_set_self_powered, |
| 558 | .pullup = mtu3_gadget_pullup, |
| 559 | .udc_start = mtu3_gadget_start, |
| 560 | .udc_stop = mtu3_gadget_stop, |
| 561 | .udc_set_speed = mtu3_gadget_set_speed, |
| 562 | }; |
| 563 | |
| 564 | static void mtu3_state_reset(struct mtu3 *mtu) |
| 565 | { |
| 566 | mtu->address = 0; |
| 567 | mtu->ep0_state = MU3D_EP0_STATE_SETUP; |
| 568 | mtu->may_wakeup = 0; |
| 569 | mtu->u1_enable = 0; |
| 570 | mtu->u2_enable = 0; |
| 571 | mtu->delayed_status = false; |
| 572 | mtu->test_mode = false; |
| 573 | } |
| 574 | |
| 575 | static void init_hw_ep(struct mtu3 *mtu, struct mtu3_ep *mep, |
| 576 | u32 epnum, u32 is_in) |
| 577 | { |
| 578 | mep->epnum = epnum; |
| 579 | mep->mtu = mtu; |
| 580 | mep->is_in = is_in; |
| 581 | |
| 582 | INIT_LIST_HEAD(&mep->req_list); |
| 583 | |
| 584 | sprintf(mep->name, "ep%d%s", epnum, |
| 585 | !epnum ? "" : (is_in ? "in" : "out")); |
| 586 | |
| 587 | mep->ep.name = mep->name; |
| 588 | INIT_LIST_HEAD(&mep->ep.ep_list); |
| 589 | |
| 590 | /* initialize maxpacket as SS */ |
| 591 | if (!epnum) { |
| 592 | usb_ep_set_maxpacket_limit(&mep->ep, USB_HS_MAXP); |
| 593 | mep->ep.ops = &mtu3_ep0_ops; |
| 594 | mtu->g.ep0 = &mep->ep; |
| 595 | } else { |
| 596 | usb_ep_set_maxpacket_limit(&mep->ep, USB_SS_MAXP); |
| 597 | mep->ep.ops = &mtu3_ep_ops; |
| 598 | list_add_tail(&mep->ep.ep_list, &mtu->g.ep_list); |
| 599 | } |
| 600 | |
| 601 | dev_dbg(mtu->dev, "%s, name=%s, maxp=%d\n", __func__, mep->ep.name, |
| 602 | mep->ep.maxpacket); |
| 603 | } |
| 604 | |
| 605 | static void mtu3_gadget_init_eps(struct mtu3 *mtu) |
| 606 | { |
| 607 | u8 epnum; |
| 608 | |
| 609 | /* initialize endpoint list just once */ |
| 610 | INIT_LIST_HEAD(&mtu->g.ep_list); |
| 611 | |
| 612 | dev_dbg(mtu->dev, "%s num_eps(1 for a pair of tx&rx ep)=%d\n", |
| 613 | __func__, mtu->num_eps); |
| 614 | |
| 615 | init_hw_ep(mtu, mtu->ep0, 0, 0); |
| 616 | for (epnum = 1; epnum < mtu->num_eps; epnum++) { |
| 617 | init_hw_ep(mtu, mtu->in_eps + epnum, epnum, 1); |
| 618 | init_hw_ep(mtu, mtu->out_eps + epnum, epnum, 0); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | int mtu3_gadget_setup(struct mtu3 *mtu) |
| 623 | { |
| 624 | mtu->g.ops = &mtu3_gadget_ops; |
| 625 | mtu->g.max_speed = mtu->max_speed; |
| 626 | mtu->g.speed = USB_SPEED_UNKNOWN; |
| 627 | mtu->g.is_dualspeed = 1; |
| 628 | mtu->g.name = MTU3_DRIVER_NAME; |
| 629 | mtu->is_active = 0; |
| 630 | mtu->delayed_status = false; |
| 631 | |
| 632 | mtu3_gadget_init_eps(mtu); |
| 633 | |
| 634 | return usb_add_gadget_udc((struct device *)mtu->dev, &mtu->g); |
| 635 | } |
| 636 | |
| 637 | void mtu3_gadget_cleanup(struct mtu3 *mtu) |
| 638 | { |
| 639 | usb_del_gadget_udc(&mtu->g); |
| 640 | } |
| 641 | |
| 642 | void mtu3_gadget_resume(struct mtu3 *mtu) |
| 643 | { |
| 644 | dev_dbg(mtu->dev, "gadget RESUME\n"); |
| 645 | if (mtu->gadget_driver && mtu->gadget_driver->resume) { |
| 646 | spin_unlock(&mtu->lock); |
| 647 | mtu->gadget_driver->resume(&mtu->g); |
| 648 | spin_lock(&mtu->lock); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /* called when SOF packets stop for 3+ msec or enters U3 */ |
| 653 | void mtu3_gadget_suspend(struct mtu3 *mtu) |
| 654 | { |
| 655 | dev_dbg(mtu->dev, "gadget SUSPEND\n"); |
| 656 | if (mtu->gadget_driver && mtu->gadget_driver->suspend) { |
| 657 | spin_unlock(&mtu->lock); |
| 658 | mtu->gadget_driver->suspend(&mtu->g); |
| 659 | spin_lock(&mtu->lock); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /* called when VBUS drops below session threshold, and in other cases */ |
| 664 | void mtu3_gadget_disconnect(struct mtu3 *mtu) |
| 665 | { |
| 666 | dev_dbg(mtu->dev, "gadget DISCONNECT\n"); |
| 667 | if (mtu->gadget_driver && mtu->gadget_driver->disconnect) { |
| 668 | spin_unlock(&mtu->lock); |
| 669 | mtu->gadget_driver->disconnect(&mtu->g); |
| 670 | spin_lock(&mtu->lock); |
| 671 | } |
| 672 | |
| 673 | mtu3_state_reset(mtu); |
| 674 | usb_gadget_set_state(&mtu->g, USB_STATE_NOTATTACHED); |
| 675 | } |
| 676 | |
| 677 | void mtu3_gadget_reset(struct mtu3 *mtu) |
| 678 | { |
| 679 | dev_dbg(mtu->dev, "gadget RESET\n"); |
| 680 | |
| 681 | /* report disconnect, if we didn't flush EP state */ |
| 682 | if (mtu->g.speed != USB_SPEED_UNKNOWN) |
| 683 | mtu3_gadget_disconnect(mtu); |
| 684 | else |
| 685 | mtu3_state_reset(mtu); |
| 686 | } |