treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / cdns3 / ep0.c
blobe71240b386b4965fc332c5def2e7b4ffa26f9a1b
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
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)
37 struct cdns3_usb_regs __iomem *regs = priv_dev->regs;
38 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
40 priv_ep->trb_pool[0].buffer = TRB_BUFFER(dma_addr);
41 priv_ep->trb_pool[0].length = TRB_LEN(length);
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;
55 trace_cdns3_prepare_trb(priv_ep, priv_ep->trb_pool);
57 cdns3_select_ep(priv_dev, priv_dev->ep0_data_dir);
59 writel(EP_STS_TRBERR, &regs->ep_sts);
60 writel(EP_TRADDR_TRADDR(priv_ep->trb_pool_dma), &regs->ep_traddr);
61 trace_cdns3_doorbell_ep0(priv_dev->ep0_data_dir ? "ep0in" : "ep0out",
62 readl(&regs->ep_traddr));
64 /* TRB should be prepared before starting transfer. */
65 writel(EP_CMD_DRDY, &regs->ep_cmd);
67 /* Resume controller before arming transfer. */
68 __cdns3_gadget_wakeup(priv_dev);
70 if (erdy)
71 writel(EP_CMD_ERDY, &priv_dev->regs->ep_cmd);
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
80 * Returns zero on success or negative value on failure
82 static int cdns3_ep0_delegate_req(struct cdns3_device *priv_dev,
83 struct usb_ctrlrequest *ctrl_req)
85 int ret;
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;
95 static void cdns3_prepare_setup_packet(struct cdns3_device *priv_dev)
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);
103 static void cdns3_ep0_complete_setup(struct cdns3_device *priv_dev,
104 u8 send_stall, u8 send_erdy)
106 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
107 struct usb_request *request;
109 request = cdns3_next_request(&priv_ep->pending_req_list);
110 if (request)
111 list_del_init(&request->list);
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);
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);
126 cdns3_allow_enable_l1(priv_dev, 1);
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
134 * Returns 0 if success, USB_GADGET_DELAYED_STATUS on deferred status stage,
135 * error code on error
137 static int cdns3_req_ep0_set_configuration(struct cdns3_device *priv_dev,
138 struct usb_ctrlrequest *ctrl_req)
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;
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;
154 if (priv_ep->flags & EP_CLAIMED)
155 cdns3_ep_config(priv_ep);
158 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
160 if (result)
161 return result;
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);
170 break;
171 case USB_STATE_CONFIGURED:
172 result = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
174 if (!config && !result) {
175 cdns3_hw_reset_eps_config(priv_dev);
176 usb_gadget_set_state(&priv_dev->gadget,
177 USB_STATE_ADDRESS);
179 break;
180 default:
181 result = -EINVAL;
184 return result;
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
192 * Returns 0 if success, error code on error
194 static int cdns3_req_ep0_set_address(struct cdns3_device *priv_dev,
195 struct usb_ctrlrequest *ctrl_req)
197 enum usb_device_state device_state = priv_dev->gadget.state;
198 u32 reg;
199 u32 addr;
201 addr = le16_to_cpu(ctrl_req->wValue);
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;
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;
216 reg = readl(&priv_dev->regs->usb_cmd);
218 writel(reg | USB_CMD_FADDR(addr) | USB_CMD_SET_ADDR,
219 &priv_dev->regs->usb_cmd);
221 usb_gadget_set_state(&priv_dev->gadget,
222 (addr ? USB_STATE_ADDRESS : USB_STATE_DEFAULT));
224 return 0;
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
232 * Returns 0 if success, error code on error
234 static int cdns3_req_ep0_get_status(struct cdns3_device *priv_dev,
235 struct usb_ctrlrequest *ctrl)
237 struct cdns3_endpoint *priv_ep;
238 __le16 *response_pkt;
239 u16 usb_status = 0;
240 u32 recip;
241 u8 index;
243 recip = ctrl->bRequestType & USB_RECIP_MASK;
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);
251 if (priv_dev->wake_up_flag)
252 usb_status |= BIT(USB_DEVICE_REMOTE_WAKEUP);
254 if (priv_dev->gadget.speed != USB_SPEED_SUPER)
255 break;
257 if (priv_dev->u1_allowed)
258 usb_status |= BIT(USB_DEV_STAT_U1_ENABLED);
260 if (priv_dev->u2_allowed)
261 usb_status |= BIT(USB_DEV_STAT_U2_ENABLED);
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];
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;
280 response_pkt = (__le16 *)priv_dev->setup_buf;
281 *response_pkt = cpu_to_le16(usb_status);
283 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma,
284 sizeof(*response_pkt), 1, 0);
285 return 0;
288 static int cdns3_ep0_feature_handle_device(struct cdns3_device *priv_dev,
289 struct usb_ctrlrequest *ctrl,
290 int set)
292 enum usb_device_state state;
293 enum usb_device_speed speed;
294 int ret = 0;
295 u32 wValue;
296 u16 tmode;
298 wValue = le16_to_cpu(ctrl->wValue);
299 state = priv_dev->gadget.state;
300 speed = priv_dev->gadget.speed;
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;
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;
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;
325 tmode = le16_to_cpu(ctrl->wIndex);
327 if (!set || (tmode & 0xff) != 0)
328 return -EINVAL;
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);
337 * Little delay to give the controller some time
338 * for sending status stage.
339 * This time should be less then 3ms.
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;
349 break;
350 default:
351 ret = -EINVAL;
354 return ret;
357 static int cdns3_ep0_feature_handle_intf(struct cdns3_device *priv_dev,
358 struct usb_ctrlrequest *ctrl,
359 int set)
361 u32 wValue;
362 int ret = 0;
364 wValue = le16_to_cpu(ctrl->wValue);
366 switch (wValue) {
367 case USB_INTRF_FUNC_SUSPEND:
368 break;
369 default:
370 ret = -EINVAL;
373 return ret;
376 static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev,
377 struct usb_ctrlrequest *ctrl,
378 int set)
380 struct cdns3_endpoint *priv_ep;
381 int ret = 0;
382 u8 index;
384 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
385 return -EINVAL;
387 if (!(ctrl->wIndex & ~USB_DIR_IN))
388 return 0;
390 index = cdns3_ep_addr_to_index(ctrl->wIndex);
391 priv_ep = priv_dev->eps[index];
393 cdns3_select_ep(priv_dev, ctrl->wIndex);
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);
400 cdns3_select_ep(priv_dev, 0x00);
402 return ret;
406 * cdns3_req_ep0_handle_feature -
407 * Handling of GET/SET_FEATURE standard USB request
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
413 * Returns 0 if success, error code on error
415 static int cdns3_req_ep0_handle_feature(struct cdns3_device *priv_dev,
416 struct usb_ctrlrequest *ctrl,
417 int set)
419 int ret = 0;
420 u32 recip;
422 recip = ctrl->bRequestType & USB_RECIP_MASK;
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;
438 return ret;
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
446 * Returns 0 if success, error code on error
448 static int cdns3_req_ep0_set_sel(struct cdns3_device *priv_dev,
449 struct usb_ctrlrequest *ctrl_req)
451 if (priv_dev->gadget.state < USB_STATE_ADDRESS)
452 return -EINVAL;
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;
460 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 6, 1, 0);
461 return 0;
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
470 * Returns 0 if success, error code on error
472 static int cdns3_req_ep0_set_isoch_delay(struct cdns3_device *priv_dev,
473 struct usb_ctrlrequest *ctrl_req)
475 if (ctrl_req->wIndex || ctrl_req->wLength)
476 return -EINVAL;
478 priv_dev->isoch_delay = ctrl_req->wValue;
480 return 0;
484 * cdns3_ep0_standard_request - Handling standard USB requests
485 * @priv_dev: extended gadget object
486 * @ctrl_req: pointer to received setup packet
488 * Returns 0 if success, error code on error
490 static int cdns3_ep0_standard_request(struct cdns3_device *priv_dev,
491 struct usb_ctrlrequest *ctrl_req)
493 int ret;
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;
522 return ret;
525 static void __pending_setup_status_handler(struct cdns3_device *priv_dev)
527 struct usb_request *request = priv_dev->pending_status_request;
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;
536 void cdns3_pending_setup_status_handler(struct work_struct *work)
538 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
539 pending_status_wq);
540 unsigned long flags;
542 spin_lock_irqsave(&priv_dev->lock, flags);
543 __pending_setup_status_handler(priv_dev);
544 spin_unlock_irqrestore(&priv_dev->lock, flags);
548 * cdns3_ep0_setup_phase - Handling setup USB requests
549 * @priv_dev: extended gadget object
551 static void cdns3_ep0_setup_phase(struct cdns3_device *priv_dev)
553 struct usb_ctrlrequest *ctrl = priv_dev->setup_buf;
554 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
555 int result;
557 priv_dev->ep0_data_dir = ctrl->bRequestType & USB_DIR_IN;
559 trace_cdns3_ctrl_req(ctrl);
561 if (!list_empty(&priv_ep->pending_req_list)) {
562 struct usb_request *request;
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);
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;
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);
580 if (result == USB_GADGET_DELAYED_STATUS)
581 return;
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);
589 static void cdns3_transfer_completed(struct cdns3_device *priv_dev)
591 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
593 if (!list_empty(&priv_ep->pending_req_list)) {
594 struct usb_request *request;
596 trace_cdns3_complete_trb(priv_ep, priv_ep->trb_pool);
597 request = cdns3_next_request(&priv_ep->pending_req_list);
599 request->actual =
600 TRB_LEN(le32_to_cpu(priv_ep->trb_pool->length));
602 priv_ep->dir = priv_dev->ep0_data_dir;
603 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 0);
606 cdns3_ep0_complete_setup(priv_dev, 0, 0);
610 * cdns3_check_new_setup - Check if controller receive new SETUP packet.
611 * @priv_dev: extended gadget object
613 * The SETUP packet can be kept in on-chip memory or in system memory.
615 static bool cdns3_check_new_setup(struct cdns3_device *priv_dev)
617 u32 ep_sts_reg;
619 cdns3_select_ep(priv_dev, 0 | USB_DIR_OUT);
620 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
622 return !!(ep_sts_reg & (EP_STS_SETUP | EP_STS_STPWAIT));
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
630 void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir)
632 u32 ep_sts_reg;
634 cdns3_select_ep(priv_dev, dir);
636 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
637 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
639 trace_cdns3_ep0_irq(priv_dev, ep_sts_reg);
641 __pending_setup_status_handler(priv_dev);
643 if (ep_sts_reg & EP_STS_SETUP)
644 priv_dev->wait_for_setup = 1;
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);
655 if (ep_sts_reg & EP_STS_DESCMIS) {
656 if (dir == 0 && !priv_dev->setup_pending)
657 cdns3_prepare_setup_packet(priv_dev);
662 * cdns3_gadget_ep0_enable
663 * Function shouldn't be called by gadget driver,
664 * endpoint 0 is allways active
666 static int cdns3_gadget_ep0_enable(struct usb_ep *ep,
667 const struct usb_endpoint_descriptor *desc)
669 return -EINVAL;
673 * cdns3_gadget_ep0_disable
674 * Function shouldn't be called by gadget driver,
675 * endpoint 0 is allways active
677 static int cdns3_gadget_ep0_disable(struct usb_ep *ep)
679 return -EINVAL;
683 * cdns3_gadget_ep0_set_halt
684 * @ep: pointer to endpoint zero object
685 * @value: 1 for set stall, 0 for clear stall
687 * Returns 0
689 static int cdns3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
691 /* TODO */
692 return 0;
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
701 * Returns 0 on success, error code elsewhere
703 static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
704 struct usb_request *request,
705 gfp_t gfp_flags)
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;
714 trace_cdns3_ep0_queue(priv_dev, request);
716 /* cancel the request if controller receive new SETUP packet. */
717 if (cdns3_check_new_setup(priv_dev))
718 return -ECONNRESET;
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);
725 erdy_sent = !priv_dev->hw_configured_flag;
726 cdns3_set_hw_configuration(priv_dev);
728 if (!erdy_sent)
729 cdns3_ep0_complete_setup(priv_dev, 0, 1);
731 cdns3_allow_enable_l1(priv_dev, 1);
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);
739 * Since there is no completion interrupt for status stage,
740 * it needs to call ->completion in software after
741 * ep0_queue is back.
743 queue_work(system_freezable_wq, &priv_dev->pending_status_wq);
744 return 0;
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;
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;
763 request->status = -EINPROGRESS;
764 list_add_tail(&request->list, &priv_ep->pending_req_list);
766 if (request->zero && request->length &&
767 (request->length % ep->maxpacket == 0))
768 zlp = 1;
770 cdns3_ep0_run_transfer(priv_dev, request->dma, request->length, 1, zlp);
772 spin_unlock_irqrestore(&priv_dev->lock, flags);
774 return ret;
778 * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint
779 * @ep: endpoint object
781 * Returns 0
783 int cdns3_gadget_ep_set_wedge(struct usb_ep *ep)
785 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
786 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
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;
792 return 0;
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,
807 * cdns3_ep0_config - Configures default endpoint
808 * @priv_dev: extended gadget object
810 * Functions sets parameters: maximal packet size and enables interrupts
812 void cdns3_ep0_config(struct cdns3_device *priv_dev)
814 struct cdns3_usb_regs __iomem *regs;
815 struct cdns3_endpoint *priv_ep;
816 u32 max_packet_size = 64;
818 regs = priv_dev->regs;
820 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
821 max_packet_size = 512;
823 priv_ep = priv_dev->eps[0];
825 if (!list_empty(&priv_ep->pending_req_list)) {
826 struct usb_request *request;
828 request = cdns3_next_request(&priv_ep->pending_req_list);
829 list_del_init(&request->list);
832 priv_dev->u1_allowed = 0;
833 priv_dev->u2_allowed = 0;
835 priv_dev->gadget.ep0->maxpacket = max_packet_size;
836 cdns3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(max_packet_size);
838 /* init ep out */
839 cdns3_select_ep(priv_dev, USB_DIR_OUT);
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));
848 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
849 &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 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
858 &regs->ep_cfg);
860 writel(EP_STS_EN_SETUPEN | EP_STS_EN_TRBERREN, &regs->ep_sts_en);
862 cdns3_set_register_bit(&regs->usb_conf, USB_CONF_U1DS | USB_CONF_U2DS);
866 * cdns3_init_ep0 Initializes software endpoint 0 of gadget
867 * @priv_dev: extended gadget object
868 * @ep_priv: extended endpoint object
870 * Returns 0 on success else error code.
872 int cdns3_init_ep0(struct cdns3_device *priv_dev,
873 struct cdns3_endpoint *priv_ep)
875 sprintf(priv_ep->name, "ep0");
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;
891 return cdns3_allocate_trb_pool(priv_ep);