WIP FPC-III support
[linux/fpc-iii.git] / drivers / usb / cdns3 / ep0.c
blobd3121a32cc68cfc25bec49668285ef03d3458b7d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Cadence USBSS DRD Driver - gadget side.
5 * Copyright (C) 2018 Cadence Design Systems.
6 * Copyright (C) 2017-2018 NXP
8 * Authors: Pawel Jez <pjez@cadence.com>,
9 * Pawel Laszczak <pawell@cadence.com>
10 * Peter Chen <peter.chen@nxp.com>
13 #include <linux/usb/composite.h>
14 #include <linux/iopoll.h>
16 #include "gadget.h"
17 #include "trace.h"
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,
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 * @zlp: add zero length packet
34 static void cdns3_ep0_run_transfer(struct cdns3_device *priv_dev,
35 dma_addr_t dma_addr,
36 unsigned int length, int erdy, int zlp)
38 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
39 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
41 priv_ep->trb_pool[0].buffer = cpu_to_le32(TRB_BUFFER(dma_addr));
42 priv_ep->trb_pool[0].length = cpu_to_le32(TRB_LEN(length));
44 if (zlp) {
45 priv_ep->trb_pool[0].control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_NORMAL));
46 priv_ep->trb_pool[1].buffer = cpu_to_le32(TRB_BUFFER(dma_addr));
47 priv_ep->trb_pool[1].length = cpu_to_le32(TRB_LEN(0));
48 priv_ep->trb_pool[1].control = cpu_to_le32(TRB_CYCLE | TRB_IOC |
49 TRB_TYPE(TRB_NORMAL));
50 } else {
51 priv_ep->trb_pool[0].control = cpu_to_le32(TRB_CYCLE | TRB_IOC |
52 TRB_TYPE(TRB_NORMAL));
53 priv_ep->trb_pool[1].control = 0;
56 trace_cdns3_prepare_trb(priv_ep, priv_ep->trb_pool);
58 cdns3_select_ep(priv_dev, priv_dev->ep0_data_dir);
60 writel(EP_STS_TRBERR, &regs->ep_sts);
61 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma), &regs->ep_traddr);
62 trace_cdns3_doorbell_ep0(priv_dev->ep0_data_dir ? "ep0in" : "ep0out",
63 readl(&regs->ep_traddr));
65 /* TRB should be prepared before starting transfer. */
66 writel(EP_CMD_DRDY, &regs->ep_cmd);
68 /* Resume controller before arming transfer. */
69 __cdns3_gadget_wakeup(priv_dev);
71 if (erdy)
72 writel(EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
75 /**
76 * cdns3_ep0_delegate_req - Returns status of handling setup packet
77 * Setup is handled by gadget driver
78 * @priv_dev: extended gadget object
79 * @ctrl_req: pointer to received setup packet
81 * Returns zero on success or negative value on failure
83 static int cdns3_ep0_delegate_req(struct cdns3_device *priv_dev,
84 struct usb_ctrlrequest *ctrl_req)
86 int ret;
88 spin_unlock(&priv_dev->lock);
89 priv_dev->setup_pending = 1;
90 ret = priv_dev->gadget_driver->setup(&priv_dev->gadget, ctrl_req);
91 priv_dev->setup_pending = 0;
92 spin_lock(&priv_dev->lock);
93 return ret;
96 static void cdns3_prepare_setup_packet(struct cdns3_device *priv_dev)
98 priv_dev->ep0_data_dir = 0;
99 priv_dev->ep0_stage = CDNS3_SETUP_STAGE;
100 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma,
101 sizeof(struct usb_ctrlrequest), 0, 0);
104 static void cdns3_ep0_complete_setup(struct cdns3_device *priv_dev,
105 u8 send_stall, u8 send_erdy)
107 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
108 struct usb_request *request;
110 request = cdns3_next_request(&priv_ep->pending_req_list);
111 if (request)
112 list_del_init(&request->list);
114 if (send_stall) {
115 trace_cdns3_halt(priv_ep, send_stall, 0);
116 /* set_stall on ep0 */
117 cdns3_select_ep(priv_dev, 0x00);
118 writel(EP_CMD_SSTALL, &priv_dev->regs->ep_cmd);
119 } else {
120 cdns3_prepare_setup_packet(priv_dev);
123 priv_dev->ep0_stage = CDNS3_SETUP_STAGE;
124 writel((send_erdy ? EP_CMD_ERDY : 0) | EP_CMD_REQ_CMPL,
125 &priv_dev->regs->ep_cmd);
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
133 * Returns 0 if success, USB_GADGET_DELAYED_STATUS on deferred status stage,
134 * error code on error
136 static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev,
137 struct usb_ctrlrequest *ctrl_req)
139 enum usb_device_state device_state = priv_dev->gadget.state;
140 u32 config = le16_to_cpu(ctrl_req->wValue);
141 int result = 0;
143 switch (device_state) {
144 case USB_STATE_ADDRESS:
145 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
147 if (result || !config)
148 goto reset_config;
150 break;
151 case USB_STATE_CONFIGURED:
152 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
153 if (!config && !result)
154 goto reset_config;
156 break;
157 default:
158 return -EINVAL;
161 return 0;
163 reset_config:
164 if (result != USB_GADGET_DELAYED_STATUS)
165 cdns3_hw_reset_eps_config(priv_dev);
167 usb_gadget_set_state(&priv_dev->gadget,
168 USB_STATE_ADDRESS);
170 return result;
174 * cdns3_req_ep0_set_address - Handling of SET_ADDRESS standard USB request
175 * @priv_dev: extended gadget object
176 * @ctrl_req: pointer to received setup packet
178 * Returns 0 if success, error code on error
180 static int cdns3_req_ep0_set_address(struct cdns3_device *priv_dev,
181 struct usb_ctrlrequest *ctrl_req)
183 enum usb_device_state device_state = priv_dev->gadget.state;
184 u32 reg;
185 u32 addr;
187 addr = le16_to_cpu(ctrl_req->wValue);
189 if (addr > USB_DEVICE_MAX_ADDRESS) {
190 dev_err(priv_dev->dev,
191 "Device address (%d) cannot be greater than %d\n",
192 addr, USB_DEVICE_MAX_ADDRESS);
193 return -EINVAL;
196 if (device_state == USB_STATE_CONFIGURED) {
197 dev_err(priv_dev->dev,
198 "can't set_address from configured state\n");
199 return -EINVAL;
202 reg = readl(&priv_dev->regs->usb_cmd);
204 writel(reg | USB_CMD_FADDR(addr) | USB_CMD_SET_ADDR,
205 &priv_dev->regs->usb_cmd);
207 usb_gadget_set_state(&priv_dev->gadget,
208 (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT));
210 return 0;
214 * cdns3_req_ep0_get_status - Handling of GET_STATUS standard USB request
215 * @priv_dev: extended gadget object
216 * @ctrl: pointer to received setup packet
218 * Returns 0 if success, error code on error
220 static int cdns3_req_ep0_get_status(struct cdns3_device *priv_dev,
221 struct usb_ctrlrequest *ctrl)
223 struct cdns3_endpoint *priv_ep;
224 __le16 *response_pkt;
225 u16 usb_status = 0;
226 u32 recip;
227 u8 index;
229 recip = ctrl->bRequestType & USB_RECIP_MASK;
231 switch (recip) {
232 case USB_RECIP_DEVICE:
233 /* self powered */
234 if (priv_dev->is_selfpowered)
235 usb_status = BIT(USB_DEVICE_SELF_POWERED);
237 if (priv_dev->wake_up_flag)
238 usb_status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
240 if (priv_dev->gadget.speed != USB_SPEED_SUPER)
241 break;
243 if (priv_dev->u1_allowed)
244 usb_status |= BIT(USB_DEV_STAT_U1_ENABLED);
246 if (priv_dev->u2_allowed)
247 usb_status |= BIT(USB_DEV_STAT_U2_ENABLED);
249 break;
250 case USB_RECIP_INTERFACE:
251 return cdns3_ep0_delegate_req(priv_dev, ctrl);
252 case USB_RECIP_ENDPOINT:
253 index = cdns3_ep_addr_to_index(le16_to_cpu(ctrl->wIndex));
254 priv_ep = priv_dev->eps[index];
256 /* check if endpoint is stalled or stall is pending */
257 cdns3_select_ep(priv_dev, le16_to_cpu(ctrl->wIndex));
258 if (EP_STS_STALL(readl(&priv_dev->regs->ep_sts)) ||
259 (priv_ep->flags & EP_STALL_PENDING))
260 usb_status = BIT(USB_ENDPOINT_HALT);
261 break;
262 default:
263 return -EINVAL;
266 response_pkt = (__le16 *)priv_dev->setup_buf;
267 *response_pkt = cpu_to_le16(usb_status);
269 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma,
270 sizeof(*response_pkt), 1, 0);
271 return 0;
274 static int cdns3_ep0_feature_handle_device(struct cdns3_device *priv_dev,
275 struct usb_ctrlrequest *ctrl,
276 int set)
278 enum usb_device_state state;
279 enum usb_device_speed speed;
280 int ret = 0;
281 u32 wValue;
282 u16 tmode;
284 wValue = le16_to_cpu(ctrl->wValue);
285 state = priv_dev->gadget.state;
286 speed = priv_dev->gadget.speed;
288 switch (wValue) {
289 case USB_DEVICE_REMOTE_WAKEUP:
290 priv_dev->wake_up_flag = !!set;
291 break;
292 case USB_DEVICE_U1_ENABLE:
293 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER)
294 return -EINVAL;
296 priv_dev->u1_allowed = !!set;
297 break;
298 case USB_DEVICE_U2_ENABLE:
299 if (state != USB_STATE_CONFIGURED || speed != USB_SPEED_SUPER)
300 return -EINVAL;
302 priv_dev->u2_allowed = !!set;
303 break;
304 case USB_DEVICE_LTM_ENABLE:
305 ret = -EINVAL;
306 break;
307 case USB_DEVICE_TEST_MODE:
308 if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
309 return -EINVAL;
311 tmode = le16_to_cpu(ctrl->wIndex);
313 if (!set || (tmode & 0xff) != 0)
314 return -EINVAL;
316 tmode >>= 8;
317 switch (tmode) {
318 case USB_TEST_J:
319 case USB_TEST_K:
320 case USB_TEST_SE0_NAK:
321 case USB_TEST_PACKET:
322 cdns3_set_register_bit(&priv_dev->regs->usb_cmd,
323 USB_CMD_STMODE |
324 USB_STS_TMODE_SEL(tmode - 1));
325 break;
326 default:
327 ret = -EINVAL;
329 break;
330 default:
331 ret = -EINVAL;
334 return ret;
337 static int cdns3_ep0_feature_handle_intf(struct cdns3_device *priv_dev,
338 struct usb_ctrlrequest *ctrl,
339 int set)
341 u32 wValue;
342 int ret = 0;
344 wValue = le16_to_cpu(ctrl->wValue);
346 switch (wValue) {
347 case USB_INTRF_FUNC_SUSPEND:
348 break;
349 default:
350 ret = -EINVAL;
353 return ret;
356 static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev,
357 struct usb_ctrlrequest *ctrl,
358 int set)
360 struct cdns3_endpoint *priv_ep;
361 int ret = 0;
362 u8 index;
364 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
365 return -EINVAL;
367 if (!(ctrl->wIndex & ~USB_DIR_IN))
368 return 0;
370 index = cdns3_ep_addr_to_index(le16_to_cpu(ctrl->wIndex));
371 priv_ep = priv_dev->eps[index];
373 cdns3_select_ep(priv_dev, le16_to_cpu(ctrl->wIndex));
375 if (set)
376 __cdns3_gadget_ep_set_halt(priv_ep);
377 else if (!(priv_ep->flags & EP_WEDGE))
378 ret = __cdns3_gadget_ep_clear_halt(priv_ep);
380 cdns3_select_ep(priv_dev, 0x00);
382 return ret;
386 * cdns3_req_ep0_handle_feature -
387 * Handling of GET/SET_FEATURE standard USB request
389 * @priv_dev: extended gadget object
390 * @ctrl: pointer to received setup packet
391 * @set: must be set to 1 for SET_FEATURE request
393 * Returns 0 if success, error code on error
395 static int cdns3_req_ep0_handle_feature(struct cdns3_device *priv_dev,
396 struct usb_ctrlrequest *ctrl,
397 int set)
399 int ret = 0;
400 u32 recip;
402 recip = ctrl->bRequestType & USB_RECIP_MASK;
404 switch (recip) {
405 case USB_RECIP_DEVICE:
406 ret = cdns3_ep0_feature_handle_device(priv_dev, ctrl, set);
407 break;
408 case USB_RECIP_INTERFACE:
409 ret = cdns3_ep0_feature_handle_intf(priv_dev, ctrl, set);
410 break;
411 case USB_RECIP_ENDPOINT:
412 ret = cdns3_ep0_feature_handle_endpoint(priv_dev, ctrl, set);
413 break;
414 default:
415 return -EINVAL;
418 return ret;
422 * cdns3_req_ep0_set_sel - Handling of SET_SEL standard USB request
423 * @priv_dev: extended gadget object
424 * @ctrl_req: pointer to received setup packet
426 * Returns 0 if success, error code on error
428 static int cdns3_req_ep0_set_sel(struct cdns3_device *priv_dev,
429 struct usb_ctrlrequest *ctrl_req)
431 if (priv_dev->gadget.state < USB_STATE_ADDRESS)
432 return -EINVAL;
434 if (le16_to_cpu(ctrl_req->wLength) != 6) {
435 dev_err(priv_dev->dev, "Set SEL should be 6 bytes, got %d\n",
436 ctrl_req->wLength);
437 return -EINVAL;
440 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 6, 1, 0);
441 return 0;
445 * cdns3_req_ep0_set_isoch_delay -
446 * Handling of GET_ISOCH_DELAY standard USB request
447 * @priv_dev: extended gadget object
448 * @ctrl_req: pointer to received setup packet
450 * Returns 0 if success, error code on error
452 static int cdns3_req_ep0_set_isoch_delay(struct cdns3_device *priv_dev,
453 struct usb_ctrlrequest *ctrl_req)
455 if (ctrl_req->wIndex || ctrl_req->wLength)
456 return -EINVAL;
458 priv_dev->isoch_delay = le16_to_cpu(ctrl_req->wValue);
460 return 0;
464 * cdns3_ep0_standard_request - Handling standard USB requests
465 * @priv_dev: extended gadget object
466 * @ctrl_req: pointer to received setup packet
468 * Returns 0 if success, error code on error
470 static int cdns3_ep0_standard_request(struct cdns3_device *priv_dev,
471 struct usb_ctrlrequest *ctrl_req)
473 int ret;
475 switch (ctrl_req->bRequest) {
476 case USB_REQ_SET_ADDRESS:
477 ret = cdns3_req_ep0_set_address(priv_dev, ctrl_req);
478 break;
479 case USB_REQ_SET_CONFIGURATION:
480 ret = cdns3_req_ep0_set_configuration(priv_dev, ctrl_req);
481 break;
482 case USB_REQ_GET_STATUS:
483 ret = cdns3_req_ep0_get_status(priv_dev, ctrl_req);
484 break;
485 case USB_REQ_CLEAR_FEATURE:
486 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 0);
487 break;
488 case USB_REQ_SET_FEATURE:
489 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 1);
490 break;
491 case USB_REQ_SET_SEL:
492 ret = cdns3_req_ep0_set_sel(priv_dev, ctrl_req);
493 break;
494 case USB_REQ_SET_ISOCH_DELAY:
495 ret = cdns3_req_ep0_set_isoch_delay(priv_dev, ctrl_req);
496 break;
497 default:
498 ret = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
499 break;
502 return ret;
505 static void __pending_setup_status_handler(struct cdns3_device *priv_dev)
507 struct usb_request *request = priv_dev->pending_status_request;
509 if (priv_dev->status_completion_no_call && request &&
510 request->complete) {
511 request->complete(&priv_dev->eps[0]->endpoint, request);
512 priv_dev->status_completion_no_call = 0;
516 void cdns3_pending_setup_status_handler(struct work_struct *work)
518 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
519 pending_status_wq);
520 unsigned long flags;
522 spin_lock_irqsave(&priv_dev->lock, flags);
523 __pending_setup_status_handler(priv_dev);
524 spin_unlock_irqrestore(&priv_dev->lock, flags);
528 * cdns3_ep0_setup_phase - Handling setup USB requests
529 * @priv_dev: extended gadget object
531 static void cdns3_ep0_setup_phase(struct cdns3_device *priv_dev)
533 struct usb_ctrlrequest *ctrl = priv_dev->setup_buf;
534 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
535 int result;
537 priv_dev->ep0_data_dir = ctrl->bRequestType & USB_DIR_IN;
539 trace_cdns3_ctrl_req(ctrl);
541 if (!list_empty(&priv_ep->pending_req_list)) {
542 struct usb_request *request;
544 request = cdns3_next_request(&priv_ep->pending_req_list);
545 priv_ep->dir = priv_dev->ep0_data_dir;
546 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
547 -ECONNRESET);
550 if (le16_to_cpu(ctrl->wLength))
551 priv_dev->ep0_stage = CDNS3_DATA_STAGE;
552 else
553 priv_dev->ep0_stage = CDNS3_STATUS_STAGE;
555 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
556 result = cdns3_ep0_standard_request(priv_dev, ctrl);
557 else
558 result = cdns3_ep0_delegate_req(priv_dev, ctrl);
560 if (result == USB_GADGET_DELAYED_STATUS)
561 return;
563 if (result < 0)
564 cdns3_ep0_complete_setup(priv_dev, 1, 1);
565 else if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE)
566 cdns3_ep0_complete_setup(priv_dev, 0, 1);
569 static void cdns3_transfer_completed(struct cdns3_device *priv_dev)
571 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
573 if (!list_empty(&priv_ep->pending_req_list)) {
574 struct usb_request *request;
576 trace_cdns3_complete_trb(priv_ep, priv_ep->trb_pool);
577 request = cdns3_next_request(&priv_ep->pending_req_list);
579 request->actual =
580 TRB_LEN(le32_to_cpu(priv_ep->trb_pool->length));
582 priv_ep->dir = priv_dev->ep0_data_dir;
583 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 0);
586 cdns3_ep0_complete_setup(priv_dev, 0, 0);
590 * cdns3_check_new_setup - Check if controller receive new SETUP packet.
591 * @priv_dev: extended gadget object
593 * The SETUP packet can be kept in on-chip memory or in system memory.
595 static bool cdns3_check_new_setup(struct cdns3_device *priv_dev)
597 u32 ep_sts_reg;
599 cdns3_select_ep(priv_dev, USB_DIR_OUT);
600 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
602 return !!(ep_sts_reg & (EP_STS_SETUP | EP_STS_STPWAIT));
606 * cdns3_check_ep0_interrupt_proceed - Processes interrupt related to endpoint 0
607 * @priv_dev: extended gadget object
608 * @dir: USB_DIR_IN for IN direction, USB_DIR_OUT for OUT direction
610 void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir)
612 u32 ep_sts_reg;
614 cdns3_select_ep(priv_dev, dir);
616 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
617 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
619 trace_cdns3_ep0_irq(priv_dev, ep_sts_reg);
621 __pending_setup_status_handler(priv_dev);
623 if (ep_sts_reg & EP_STS_SETUP)
624 priv_dev->wait_for_setup = 1;
626 if (priv_dev->wait_for_setup && ep_sts_reg & EP_STS_IOC) {
627 priv_dev->wait_for_setup = 0;
628 cdns3_ep0_setup_phase(priv_dev);
629 } else if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP)) {
630 priv_dev->ep0_data_dir = dir;
631 cdns3_transfer_completed(priv_dev);
634 if (ep_sts_reg & EP_STS_DESCMIS) {
635 if (dir == 0 && !priv_dev->setup_pending)
636 cdns3_prepare_setup_packet(priv_dev);
641 * cdns3_gadget_ep0_enable
642 * @ep: pointer to endpoint zero object
643 * @desc: pointer to usb endpoint descriptor
645 * Function shouldn't be called by gadget driver,
646 * endpoint 0 is allways active
648 static int cdns3_gadget_ep0_enable(struct usb_ep *ep,
649 const struct usb_endpoint_descriptor *desc)
651 return -EINVAL;
655 * cdns3_gadget_ep0_disable
656 * @ep: pointer to endpoint zero object
658 * Function shouldn't be called by gadget driver,
659 * endpoint 0 is allways active
661 static int cdns3_gadget_ep0_disable(struct usb_ep *ep)
663 return -EINVAL;
667 * cdns3_gadget_ep0_set_halt
668 * @ep: pointer to endpoint zero object
669 * @value: 1 for set stall, 0 for clear stall
671 * Returns 0
673 static int cdns3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
675 /* TODO */
676 return 0;
680 * cdns3_gadget_ep0_queue Transfer data on endpoint zero
681 * @ep: pointer to endpoint zero object
682 * @request: pointer to request object
683 * @gfp_flags: gfp flags
685 * Returns 0 on success, error code elsewhere
687 static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
688 struct usb_request *request,
689 gfp_t gfp_flags)
691 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
692 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
693 unsigned long flags;
694 int ret = 0;
695 u8 zlp = 0;
696 int i;
698 spin_lock_irqsave(&priv_dev->lock, flags);
699 trace_cdns3_ep0_queue(priv_dev, request);
701 /* cancel the request if controller receive new SETUP packet. */
702 if (cdns3_check_new_setup(priv_dev)) {
703 spin_unlock_irqrestore(&priv_dev->lock, flags);
704 return -ECONNRESET;
707 /* send STATUS stage. Should be called only for SET_CONFIGURATION */
708 if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) {
709 u32 val;
711 cdns3_select_ep(priv_dev, 0x00);
714 * Configure all non-control EPs which are not enabled by class driver
716 for (i = 0; i < CDNS3_ENDPOINTS_MAX_COUNT; i++) {
717 priv_ep = priv_dev->eps[i];
718 if (priv_ep && priv_ep->flags & EP_CLAIMED &&
719 !(priv_ep->flags & EP_ENABLED))
720 cdns3_ep_config(priv_ep, 0);
723 cdns3_set_hw_configuration(priv_dev);
724 cdns3_ep0_complete_setup(priv_dev, 0, 1);
725 /* wait until configuration set */
726 ret = readl_poll_timeout_atomic(&priv_dev->regs->usb_sts, val,
727 val & USB_STS_CFGSTS_MASK, 1, 100);
728 if (ret == -ETIMEDOUT)
729 dev_warn(priv_dev->dev, "timeout for waiting configuration set\n");
731 request->actual = 0;
732 priv_dev->status_completion_no_call = true;
733 priv_dev->pending_status_request = request;
734 spin_unlock_irqrestore(&priv_dev->lock, flags);
737 * Since there is no completion interrupt for status stage,
738 * it needs to call ->completion in software after
739 * ep0_queue is back.
741 queue_work(system_freezable_wq, &priv_dev->pending_status_wq);
742 return ret;
745 if (!list_empty(&priv_ep->pending_req_list)) {
746 dev_err(priv_dev->dev,
747 "can't handle multiple requests for ep0\n");
748 spin_unlock_irqrestore(&priv_dev->lock, flags);
749 return -EBUSY;
752 ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request,
753 priv_dev->ep0_data_dir);
754 if (ret) {
755 spin_unlock_irqrestore(&priv_dev->lock, flags);
756 dev_err(priv_dev->dev, "failed to map request\n");
757 return -EINVAL;
760 request->status = -EINPROGRESS;
761 list_add_tail(&request->list, &priv_ep->pending_req_list);
763 if (request->zero && request->length &&
764 (request->length % ep->maxpacket == 0))
765 zlp = 1;
767 cdns3_ep0_run_transfer(priv_dev, request->dma, request->length, 1, zlp);
769 spin_unlock_irqrestore(&priv_dev->lock, flags);
771 return ret;
775 * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint
776 * @ep: endpoint object
778 * Returns 0
780 int cdns3_gadget_ep_set_wedge(struct usb_ep *ep)
782 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
783 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
785 dev_dbg(priv_dev->dev, "Wedge for %s\n", ep->name);
786 cdns3_gadget_ep_set_halt(ep, 1);
787 priv_ep->flags |= EP_WEDGE;
789 return 0;
792 const struct usb_ep_ops cdns3_gadget_ep0_ops = {
793 .enable = cdns3_gadget_ep0_enable,
794 .disable = cdns3_gadget_ep0_disable,
795 .alloc_request = cdns3_gadget_ep_alloc_request,
796 .free_request = cdns3_gadget_ep_free_request,
797 .queue = cdns3_gadget_ep0_queue,
798 .dequeue = cdns3_gadget_ep_dequeue,
799 .set_halt = cdns3_gadget_ep0_set_halt,
800 .set_wedge = cdns3_gadget_ep_set_wedge,
804 * cdns3_ep0_config - Configures default endpoint
805 * @priv_dev: extended gadget object
807 * Functions sets parameters: maximal packet size and enables interrupts
809 void cdns3_ep0_config(struct cdns3_device *priv_dev)
811 struct cdns3_usb_regs __iomem *regs;
812 struct cdns3_endpoint *priv_ep;
813 u32 max_packet_size = 64;
814 u32 ep_cfg;
816 regs = priv_dev->regs;
818 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
819 max_packet_size = 512;
821 priv_ep = priv_dev->eps[0];
823 if (!list_empty(&priv_ep->pending_req_list)) {
824 struct usb_request *request;
826 request = cdns3_next_request(&priv_ep->pending_req_list);
827 list_del_init(&request->list);
830 priv_dev->u1_allowed = 0;
831 priv_dev->u2_allowed = 0;
833 priv_dev->gadget.ep0->maxpacket = max_packet_size;
834 cdns3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(max_packet_size);
836 /* init ep out */
837 cdns3_select_ep(priv_dev, USB_DIR_OUT);
839 if (priv_dev->dev_ver >= DEV_VER_V3) {
840 cdns3_set_register_bit(&priv_dev->regs->dtrans,
841 BIT(0) | BIT(16));
842 cdns3_set_register_bit(&priv_dev->regs->tdl_from_trb,
843 BIT(0) | BIT(16));
846 ep_cfg = EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size);
848 if (!(priv_ep->flags & EP_CONFIGURED))
849 writel(ep_cfg, &regs->ep_cfg);
851 writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN,
852 &regs->ep_sts_en);
854 /* init ep in */
855 cdns3_select_ep(priv_dev, USB_DIR_IN);
857 if (!(priv_ep->flags & EP_CONFIGURED))
858 writel(ep_cfg, &regs->ep_cfg);
860 priv_ep->flags |= EP_CONFIGURED;
862 writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, &regs->ep_sts_en);
864 cdns3_set_register_bit(&regs->usb_conf, USB_CONF_U1DS | USB_CONF_U2DS);
868 * cdns3_init_ep0 Initializes software endpoint 0 of gadget
869 * @priv_dev: extended gadget object
870 * @priv_ep: extended endpoint object
872 * Returns 0 on success else error code.
874 int cdns3_init_ep0(struct cdns3_device *priv_dev,
875 struct cdns3_endpoint *priv_ep)
877 sprintf(priv_ep->name, "ep0");
879 /* fill linux fields */
880 priv_ep->endpoint.ops = &cdns3_gadget_ep0_ops;
881 priv_ep->endpoint.maxburst = 1;
882 usb_ep_set_maxpacket_limit(&priv_ep->endpoint,
883 CDNS3_EP0_MAX_PACKET_LIMIT);
884 priv_ep->endpoint.address = 0;
885 priv_ep->endpoint.caps.type_control = 1;
886 priv_ep->endpoint.caps.dir_in = 1;
887 priv_ep->endpoint.caps.dir_out = 1;
888 priv_ep->endpoint.name = priv_ep->name;
889 priv_ep->endpoint.desc = &cdns3_gadget_ep0_desc;
890 priv_dev->gadget.ep0 = &priv_ep->endpoint;
891 priv_ep->type = USB_ENDPOINT_XFER_CONTROL;
893 return cdns3_allocate_trb_pool(priv_ep);