net: DCB: Validate DCB_ATTR_DCB_BUFFER argument
[linux/fpc-iii.git] / drivers / usb / cdns3 / ep0.c
blob666cebd9c5f295f0e79298d158e9a5401fdae96b
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 = cpu_to_le32(TRB_BUFFER(dma_addr));
41 priv_ep->trb_pool[0].length = cpu_to_le32(TRB_LEN(length));
43 if (zlp) {
44 priv_ep->trb_pool[0].control = cpu_to_le32(TRB_CYCLE | TRB_TYPE(TRB_NORMAL));
45 priv_ep->trb_pool[1].buffer = cpu_to_le32(TRB_BUFFER(dma_addr));
46 priv_ep->trb_pool[1].length = cpu_to_le32(TRB_LEN(0));
47 priv_ep->trb_pool[1].control = cpu_to_le32(TRB_CYCLE | TRB_IOC |
48 TRB_TYPE(TRB_NORMAL));
49 } else {
50 priv_ep->trb_pool[0].control = cpu_to_le32(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(le16_to_cpu(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, le16_to_cpu(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 tmode >>= 8;
331 switch (tmode) {
332 case TEST_J:
333 case TEST_K:
334 case TEST_SE0_NAK:
335 case TEST_PACKET:
336 cdns3_ep0_complete_setup(priv_dev, 0, 1);
338 * Little delay to give the controller some time
339 * for sending status stage.
340 * This time should be less then 3ms.
342 mdelay(1);
343 cdns3_set_register_bit(&priv_dev->regs->usb_cmd,
344 USB_CMD_STMODE |
345 USB_STS_TMODE_SEL(tmode - 1));
346 break;
347 default:
348 ret = -EINVAL;
350 break;
351 default:
352 ret = -EINVAL;
355 return ret;
358 static int cdns3_ep0_feature_handle_intf(struct cdns3_device *priv_dev,
359 struct usb_ctrlrequest *ctrl,
360 int set)
362 u32 wValue;
363 int ret = 0;
365 wValue = le16_to_cpu(ctrl->wValue);
367 switch (wValue) {
368 case USB_INTRF_FUNC_SUSPEND:
369 break;
370 default:
371 ret = -EINVAL;
374 return ret;
377 static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device *priv_dev,
378 struct usb_ctrlrequest *ctrl,
379 int set)
381 struct cdns3_endpoint *priv_ep;
382 int ret = 0;
383 u8 index;
385 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT)
386 return -EINVAL;
388 if (!(ctrl->wIndex & ~USB_DIR_IN))
389 return 0;
391 index = cdns3_ep_addr_to_index(le16_to_cpu(ctrl->wIndex));
392 priv_ep = priv_dev->eps[index];
394 cdns3_select_ep(priv_dev, le16_to_cpu(ctrl->wIndex));
396 if (set)
397 __cdns3_gadget_ep_set_halt(priv_ep);
398 else if (!(priv_ep->flags & EP_WEDGE))
399 ret = __cdns3_gadget_ep_clear_halt(priv_ep);
401 cdns3_select_ep(priv_dev, 0x00);
403 return ret;
407 * cdns3_req_ep0_handle_feature -
408 * Handling of GET/SET_FEATURE standard USB request
410 * @priv_dev: extended gadget object
411 * @ctrl_req: pointer to received setup packet
412 * @set: must be set to 1 for SET_FEATURE request
414 * Returns 0 if success, error code on error
416 static int cdns3_req_ep0_handle_feature(struct cdns3_device *priv_dev,
417 struct usb_ctrlrequest *ctrl,
418 int set)
420 int ret = 0;
421 u32 recip;
423 recip = ctrl->bRequestType & USB_RECIP_MASK;
425 switch (recip) {
426 case USB_RECIP_DEVICE:
427 ret = cdns3_ep0_feature_handle_device(priv_dev, ctrl, set);
428 break;
429 case USB_RECIP_INTERFACE:
430 ret = cdns3_ep0_feature_handle_intf(priv_dev, ctrl, set);
431 break;
432 case USB_RECIP_ENDPOINT:
433 ret = cdns3_ep0_feature_handle_endpoint(priv_dev, ctrl, set);
434 break;
435 default:
436 return -EINVAL;
439 return ret;
443 * cdns3_req_ep0_set_sel - Handling of SET_SEL standard USB request
444 * @priv_dev: extended gadget object
445 * @ctrl_req: pointer to received setup packet
447 * Returns 0 if success, error code on error
449 static int cdns3_req_ep0_set_sel(struct cdns3_device *priv_dev,
450 struct usb_ctrlrequest *ctrl_req)
452 if (priv_dev->gadget.state < USB_STATE_ADDRESS)
453 return -EINVAL;
455 if (le16_to_cpu(ctrl_req->wLength) != 6) {
456 dev_err(priv_dev->dev, "Set SEL should be 6 bytes, got %d\n",
457 ctrl_req->wLength);
458 return -EINVAL;
461 cdns3_ep0_run_transfer(priv_dev, priv_dev->setup_dma, 6, 1, 0);
462 return 0;
466 * cdns3_req_ep0_set_isoch_delay -
467 * Handling of GET_ISOCH_DELAY standard USB request
468 * @priv_dev: extended gadget object
469 * @ctrl_req: pointer to received setup packet
471 * Returns 0 if success, error code on error
473 static int cdns3_req_ep0_set_isoch_delay(struct cdns3_device *priv_dev,
474 struct usb_ctrlrequest *ctrl_req)
476 if (ctrl_req->wIndex || ctrl_req->wLength)
477 return -EINVAL;
479 priv_dev->isoch_delay = le16_to_cpu(ctrl_req->wValue);
481 return 0;
485 * cdns3_ep0_standard_request - Handling standard USB requests
486 * @priv_dev: extended gadget object
487 * @ctrl_req: pointer to received setup packet
489 * Returns 0 if success, error code on error
491 static int cdns3_ep0_standard_request(struct cdns3_device *priv_dev,
492 struct usb_ctrlrequest *ctrl_req)
494 int ret;
496 switch (ctrl_req->bRequest) {
497 case USB_REQ_SET_ADDRESS:
498 ret = cdns3_req_ep0_set_address(priv_dev, ctrl_req);
499 break;
500 case USB_REQ_SET_CONFIGURATION:
501 ret = cdns3_req_ep0_set_configuration(priv_dev, ctrl_req);
502 break;
503 case USB_REQ_GET_STATUS:
504 ret = cdns3_req_ep0_get_status(priv_dev, ctrl_req);
505 break;
506 case USB_REQ_CLEAR_FEATURE:
507 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 0);
508 break;
509 case USB_REQ_SET_FEATURE:
510 ret = cdns3_req_ep0_handle_feature(priv_dev, ctrl_req, 1);
511 break;
512 case USB_REQ_SET_SEL:
513 ret = cdns3_req_ep0_set_sel(priv_dev, ctrl_req);
514 break;
515 case USB_REQ_SET_ISOCH_DELAY:
516 ret = cdns3_req_ep0_set_isoch_delay(priv_dev, ctrl_req);
517 break;
518 default:
519 ret = cdns3_ep0_delegate_req(priv_dev, ctrl_req);
520 break;
523 return ret;
526 static void __pending_setup_status_handler(struct cdns3_device *priv_dev)
528 struct usb_request *request = priv_dev->pending_status_request;
530 if (priv_dev->status_completion_no_call && request &&
531 request->complete) {
532 request->complete(&priv_dev->eps[0]->endpoint, request);
533 priv_dev->status_completion_no_call = 0;
537 void cdns3_pending_setup_status_handler(struct work_struct *work)
539 struct cdns3_device *priv_dev = container_of(work, struct cdns3_device,
540 pending_status_wq);
541 unsigned long flags;
543 spin_lock_irqsave(&priv_dev->lock, flags);
544 __pending_setup_status_handler(priv_dev);
545 spin_unlock_irqrestore(&priv_dev->lock, flags);
549 * cdns3_ep0_setup_phase - Handling setup USB requests
550 * @priv_dev: extended gadget object
552 static void cdns3_ep0_setup_phase(struct cdns3_device *priv_dev)
554 struct usb_ctrlrequest *ctrl = priv_dev->setup_buf;
555 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
556 int result;
558 priv_dev->ep0_data_dir = ctrl->bRequestType & USB_DIR_IN;
560 trace_cdns3_ctrl_req(ctrl);
562 if (!list_empty(&priv_ep->pending_req_list)) {
563 struct usb_request *request;
565 request = cdns3_next_request(&priv_ep->pending_req_list);
566 priv_ep->dir = priv_dev->ep0_data_dir;
567 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request),
568 -ECONNRESET);
571 if (le16_to_cpu(ctrl->wLength))
572 priv_dev->ep0_stage = CDNS3_DATA_STAGE;
573 else
574 priv_dev->ep0_stage = CDNS3_STATUS_STAGE;
576 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD)
577 result = cdns3_ep0_standard_request(priv_dev, ctrl);
578 else
579 result = cdns3_ep0_delegate_req(priv_dev, ctrl);
581 if (result == USB_GADGET_DELAYED_STATUS)
582 return;
584 if (result < 0)
585 cdns3_ep0_complete_setup(priv_dev, 1, 1);
586 else if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE)
587 cdns3_ep0_complete_setup(priv_dev, 0, 1);
590 static void cdns3_transfer_completed(struct cdns3_device *priv_dev)
592 struct cdns3_endpoint *priv_ep = priv_dev->eps[0];
594 if (!list_empty(&priv_ep->pending_req_list)) {
595 struct usb_request *request;
597 trace_cdns3_complete_trb(priv_ep, priv_ep->trb_pool);
598 request = cdns3_next_request(&priv_ep->pending_req_list);
600 request->actual =
601 TRB_LEN(le32_to_cpu(priv_ep->trb_pool->length));
603 priv_ep->dir = priv_dev->ep0_data_dir;
604 cdns3_gadget_giveback(priv_ep, to_cdns3_request(request), 0);
607 cdns3_ep0_complete_setup(priv_dev, 0, 0);
611 * cdns3_check_new_setup - Check if controller receive new SETUP packet.
612 * @priv_dev: extended gadget object
614 * The SETUP packet can be kept in on-chip memory or in system memory.
616 static bool cdns3_check_new_setup(struct cdns3_device *priv_dev)
618 u32 ep_sts_reg;
620 cdns3_select_ep(priv_dev, 0 | USB_DIR_OUT);
621 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
623 return !!(ep_sts_reg & (EP_STS_SETUP | EP_STS_STPWAIT));
627 * cdns3_check_ep0_interrupt_proceed - Processes interrupt related to endpoint 0
628 * @priv_dev: extended gadget object
629 * @dir: USB_DIR_IN for IN direction, USB_DIR_OUT for OUT direction
631 void cdns3_check_ep0_interrupt_proceed(struct cdns3_device *priv_dev, int dir)
633 u32 ep_sts_reg;
635 cdns3_select_ep(priv_dev, dir);
637 ep_sts_reg = readl(&priv_dev->regs->ep_sts);
638 writel(ep_sts_reg, &priv_dev->regs->ep_sts);
640 trace_cdns3_ep0_irq(priv_dev, ep_sts_reg);
642 __pending_setup_status_handler(priv_dev);
644 if (ep_sts_reg & EP_STS_SETUP)
645 priv_dev->wait_for_setup = 1;
647 if (priv_dev->wait_for_setup && ep_sts_reg & EP_STS_IOC) {
648 priv_dev->wait_for_setup = 0;
649 cdns3_allow_enable_l1(priv_dev, 0);
650 cdns3_ep0_setup_phase(priv_dev);
651 } else if ((ep_sts_reg & EP_STS_IOC) || (ep_sts_reg & EP_STS_ISP)) {
652 priv_dev->ep0_data_dir = dir;
653 cdns3_transfer_completed(priv_dev);
656 if (ep_sts_reg & EP_STS_DESCMIS) {
657 if (dir == 0 && !priv_dev->setup_pending)
658 cdns3_prepare_setup_packet(priv_dev);
663 * cdns3_gadget_ep0_enable
664 * Function shouldn't be called by gadget driver,
665 * endpoint 0 is allways active
667 static int cdns3_gadget_ep0_enable(struct usb_ep *ep,
668 const struct usb_endpoint_descriptor *desc)
670 return -EINVAL;
674 * cdns3_gadget_ep0_disable
675 * Function shouldn't be called by gadget driver,
676 * endpoint 0 is allways active
678 static int cdns3_gadget_ep0_disable(struct usb_ep *ep)
680 return -EINVAL;
684 * cdns3_gadget_ep0_set_halt
685 * @ep: pointer to endpoint zero object
686 * @value: 1 for set stall, 0 for clear stall
688 * Returns 0
690 static int cdns3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
692 /* TODO */
693 return 0;
697 * cdns3_gadget_ep0_queue Transfer data on endpoint zero
698 * @ep: pointer to endpoint zero object
699 * @request: pointer to request object
700 * @gfp_flags: gfp flags
702 * Returns 0 on success, error code elsewhere
704 static int cdns3_gadget_ep0_queue(struct usb_ep *ep,
705 struct usb_request *request,
706 gfp_t gfp_flags)
708 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
709 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
710 unsigned long flags;
711 int erdy_sent = 0;
712 int ret = 0;
713 u8 zlp = 0;
715 spin_lock_irqsave(&priv_dev->lock, flags);
716 trace_cdns3_ep0_queue(priv_dev, request);
718 /* cancel the request if controller receive new SETUP packet. */
719 if (cdns3_check_new_setup(priv_dev)) {
720 spin_unlock_irqrestore(&priv_dev->lock, flags);
721 return -ECONNRESET;
724 /* send STATUS stage. Should be called only for SET_CONFIGURATION */
725 if (priv_dev->ep0_stage == CDNS3_STATUS_STAGE) {
726 cdns3_select_ep(priv_dev, 0x00);
728 erdy_sent = !priv_dev->hw_configured_flag;
729 cdns3_set_hw_configuration(priv_dev);
731 if (!erdy_sent)
732 cdns3_ep0_complete_setup(priv_dev, 0, 1);
734 cdns3_allow_enable_l1(priv_dev, 1);
736 request->actual = 0;
737 priv_dev->status_completion_no_call = true;
738 priv_dev->pending_status_request = request;
739 spin_unlock_irqrestore(&priv_dev->lock, flags);
742 * Since there is no completion interrupt for status stage,
743 * it needs to call ->completion in software after
744 * ep0_queue is back.
746 queue_work(system_freezable_wq, &priv_dev->pending_status_wq);
747 return 0;
750 if (!list_empty(&priv_ep->pending_req_list)) {
751 dev_err(priv_dev->dev,
752 "can't handle multiple requests for ep0\n");
753 spin_unlock_irqrestore(&priv_dev->lock, flags);
754 return -EBUSY;
757 ret = usb_gadget_map_request_by_dev(priv_dev->sysdev, request,
758 priv_dev->ep0_data_dir);
759 if (ret) {
760 spin_unlock_irqrestore(&priv_dev->lock, flags);
761 dev_err(priv_dev->dev, "failed to map request\n");
762 return -EINVAL;
765 request->status = -EINPROGRESS;
766 list_add_tail(&request->list, &priv_ep->pending_req_list);
768 if (request->zero && request->length &&
769 (request->length % ep->maxpacket == 0))
770 zlp = 1;
772 cdns3_ep0_run_transfer(priv_dev, request->dma, request->length, 1, zlp);
774 spin_unlock_irqrestore(&priv_dev->lock, flags);
776 return ret;
780 * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint
781 * @ep: endpoint object
783 * Returns 0
785 int cdns3_gadget_ep_set_wedge(struct usb_ep *ep)
787 struct cdns3_endpoint *priv_ep = ep_to_cdns3_ep(ep);
788 struct cdns3_device *priv_dev = priv_ep->cdns3_dev;
790 dev_dbg(priv_dev->dev, "Wedge for %s\n", ep->name);
791 cdns3_gadget_ep_set_halt(ep, 1);
792 priv_ep->flags |= EP_WEDGE;
794 return 0;
797 const struct usb_ep_ops cdns3_gadget_ep0_ops = {
798 .enable = cdns3_gadget_ep0_enable,
799 .disable = cdns3_gadget_ep0_disable,
800 .alloc_request = cdns3_gadget_ep_alloc_request,
801 .free_request = cdns3_gadget_ep_free_request,
802 .queue = cdns3_gadget_ep0_queue,
803 .dequeue = cdns3_gadget_ep_dequeue,
804 .set_halt = cdns3_gadget_ep0_set_halt,
805 .set_wedge = cdns3_gadget_ep_set_wedge,
809 * cdns3_ep0_config - Configures default endpoint
810 * @priv_dev: extended gadget object
812 * Functions sets parameters: maximal packet size and enables interrupts
814 void cdns3_ep0_config(struct cdns3_device *priv_dev)
816 struct cdns3_usb_regs __iomem *regs;
817 struct cdns3_endpoint *priv_ep;
818 u32 max_packet_size = 64;
820 regs = priv_dev->regs;
822 if (priv_dev->gadget.speed == USB_SPEED_SUPER)
823 max_packet_size = 512;
825 priv_ep = priv_dev->eps[0];
827 if (!list_empty(&priv_ep->pending_req_list)) {
828 struct usb_request *request;
830 request = cdns3_next_request(&priv_ep->pending_req_list);
831 list_del_init(&request->list);
834 priv_dev->u1_allowed = 0;
835 priv_dev->u2_allowed = 0;
837 priv_dev->gadget.ep0->maxpacket = max_packet_size;
838 cdns3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(max_packet_size);
840 /* init ep out */
841 cdns3_select_ep(priv_dev, USB_DIR_OUT);
843 if (priv_dev->dev_ver >= DEV_VER_V3) {
844 cdns3_set_register_bit(&priv_dev->regs->dtrans,
845 BIT(0) | BIT(16));
846 cdns3_set_register_bit(&priv_dev->regs->tdl_from_trb,
847 BIT(0) | BIT(16));
850 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
851 &regs->ep_cfg);
853 writel(EP_STS_EN_SETUPEN | EP_STS_EN_DESCMISEN | EP_STS_EN_TRBERREN,
854 &regs->ep_sts_en);
856 /* init ep in */
857 cdns3_select_ep(priv_dev, USB_DIR_IN);
859 writel(EP_CFG_ENABLE | EP_CFG_MAXPKTSIZE(max_packet_size),
860 &regs->ep_cfg);
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 * @ep_priv: 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);