1 // SPDX-License-Identifier: GPL-2.0
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>
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
,
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
,
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
));
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
));
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
, ®s
->ep_sts
);
60 writel(EP_TRADDR_TRADDR(priv_ep
->trb_pool_dma
), ®s
->ep_traddr
);
61 trace_cdns3_doorbell_ep0(priv_dev
->ep0_data_dir
? "ep0in" : "ep0out",
62 readl(®s
->ep_traddr
));
64 /* TRB should be prepared before starting transfer. */
65 writel(EP_CMD_DRDY
, ®s
->ep_cmd
);
67 /* Resume controller before arming transfer. */
68 __cdns3_gadget_wakeup(priv_dev
);
71 writel(EP_CMD_ERDY
, &priv_dev
->regs
->ep_cmd
);
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
)
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
);
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
);
111 list_del_init(&request
->list
);
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
);
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
);
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
];
154 if (priv_ep
->flags
& EP_CLAIMED
)
155 cdns3_ep_config(priv_ep
);
158 result
= cdns3_ep0_delegate_req(priv_dev
, ctrl_req
);
164 cdns3_set_hw_configuration(priv_dev
);
166 cdns3_hw_reset_eps_config(priv_dev
);
167 usb_gadget_set_state(&priv_dev
->gadget
,
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
,
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
;
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
);
210 if (device_state
== USB_STATE_CONFIGURED
) {
211 dev_err(priv_dev
->dev
,
212 "can't set_address from configured state\n");
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
));
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
;
243 recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
246 case USB_RECIP_DEVICE
:
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
)
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
);
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
);
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);
288 static int cdns3_ep0_feature_handle_device(struct cdns3_device
*priv_dev
,
289 struct usb_ctrlrequest
*ctrl
,
292 enum usb_device_state state
;
293 enum usb_device_speed speed
;
298 wValue
= le16_to_cpu(ctrl
->wValue
);
299 state
= priv_dev
->gadget
.state
;
300 speed
= priv_dev
->gadget
.speed
;
303 case USB_DEVICE_REMOTE_WAKEUP
:
304 priv_dev
->wake_up_flag
= !!set
;
306 case USB_DEVICE_U1_ENABLE
:
307 if (state
!= USB_STATE_CONFIGURED
|| speed
!= USB_SPEED_SUPER
)
310 priv_dev
->u1_allowed
= !!set
;
312 case USB_DEVICE_U2_ENABLE
:
313 if (state
!= USB_STATE_CONFIGURED
|| speed
!= USB_SPEED_SUPER
)
316 priv_dev
->u2_allowed
= !!set
;
318 case USB_DEVICE_LTM_ENABLE
:
321 case USB_DEVICE_TEST_MODE
:
322 if (state
!= USB_STATE_CONFIGURED
|| speed
> USB_SPEED_HIGH
)
325 tmode
= le16_to_cpu(ctrl
->wIndex
);
327 if (!set
|| (tmode
& 0xff) != 0)
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.
343 cdns3_set_register_bit(&priv_dev
->regs
->usb_cmd
,
345 USB_STS_TMODE_SEL(tmode
- 1));
358 static int cdns3_ep0_feature_handle_intf(struct cdns3_device
*priv_dev
,
359 struct usb_ctrlrequest
*ctrl
,
365 wValue
= le16_to_cpu(ctrl
->wValue
);
368 case USB_INTRF_FUNC_SUSPEND
:
377 static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device
*priv_dev
,
378 struct usb_ctrlrequest
*ctrl
,
381 struct cdns3_endpoint
*priv_ep
;
385 if (le16_to_cpu(ctrl
->wValue
) != USB_ENDPOINT_HALT
)
388 if (!(ctrl
->wIndex
& ~USB_DIR_IN
))
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
));
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);
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
,
423 recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
426 case USB_RECIP_DEVICE
:
427 ret
= cdns3_ep0_feature_handle_device(priv_dev
, ctrl
, set
);
429 case USB_RECIP_INTERFACE
:
430 ret
= cdns3_ep0_feature_handle_intf(priv_dev
, ctrl
, set
);
432 case USB_RECIP_ENDPOINT
:
433 ret
= cdns3_ep0_feature_handle_endpoint(priv_dev
, ctrl
, set
);
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
)
455 if (le16_to_cpu(ctrl_req
->wLength
) != 6) {
456 dev_err(priv_dev
->dev
, "Set SEL should be 6 bytes, got %d\n",
461 cdns3_ep0_run_transfer(priv_dev
, priv_dev
->setup_dma
, 6, 1, 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
)
479 priv_dev
->isoch_delay
= le16_to_cpu(ctrl_req
->wValue
);
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
)
496 switch (ctrl_req
->bRequest
) {
497 case USB_REQ_SET_ADDRESS
:
498 ret
= cdns3_req_ep0_set_address(priv_dev
, ctrl_req
);
500 case USB_REQ_SET_CONFIGURATION
:
501 ret
= cdns3_req_ep0_set_configuration(priv_dev
, ctrl_req
);
503 case USB_REQ_GET_STATUS
:
504 ret
= cdns3_req_ep0_get_status(priv_dev
, ctrl_req
);
506 case USB_REQ_CLEAR_FEATURE
:
507 ret
= cdns3_req_ep0_handle_feature(priv_dev
, ctrl_req
, 0);
509 case USB_REQ_SET_FEATURE
:
510 ret
= cdns3_req_ep0_handle_feature(priv_dev
, ctrl_req
, 1);
512 case USB_REQ_SET_SEL
:
513 ret
= cdns3_req_ep0_set_sel(priv_dev
, ctrl_req
);
515 case USB_REQ_SET_ISOCH_DELAY
:
516 ret
= cdns3_req_ep0_set_isoch_delay(priv_dev
, ctrl_req
);
519 ret
= cdns3_ep0_delegate_req(priv_dev
, ctrl_req
);
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
&&
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
,
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];
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
),
571 if (le16_to_cpu(ctrl
->wLength
))
572 priv_dev
->ep0_stage
= CDNS3_DATA_STAGE
;
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
);
579 result
= cdns3_ep0_delegate_req(priv_dev
, ctrl
);
581 if (result
== USB_GADGET_DELAYED_STATUS
)
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
);
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
)
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
)
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
)
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
)
684 * cdns3_gadget_ep0_set_halt
685 * @ep: pointer to endpoint zero object
686 * @value: 1 for set stall, 0 for clear stall
690 static int cdns3_gadget_ep0_set_halt(struct usb_ep
*ep
, int value
)
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
,
708 struct cdns3_endpoint
*priv_ep
= ep_to_cdns3_ep(ep
);
709 struct cdns3_device
*priv_dev
= priv_ep
->cdns3_dev
;
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
);
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
);
732 cdns3_ep0_complete_setup(priv_dev
, 0, 1);
734 cdns3_allow_enable_l1(priv_dev
, 1);
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
746 queue_work(system_freezable_wq
, &priv_dev
->pending_status_wq
);
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
);
757 ret
= usb_gadget_map_request_by_dev(priv_dev
->sysdev
, request
,
758 priv_dev
->ep0_data_dir
);
760 spin_unlock_irqrestore(&priv_dev
->lock
, flags
);
761 dev_err(priv_dev
->dev
, "failed to map request\n");
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))
772 cdns3_ep0_run_transfer(priv_dev
, request
->dma
, request
->length
, 1, zlp
);
774 spin_unlock_irqrestore(&priv_dev
->lock
, flags
);
780 * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint
781 * @ep: endpoint object
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
;
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
);
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
,
846 cdns3_set_register_bit(&priv_dev
->regs
->tdl_from_trb
,
850 writel(EP_CFG_ENABLE
| EP_CFG_MAXPKTSIZE(max_packet_size
),
853 writel(EP_STS_EN_SETUPEN
| EP_STS_EN_DESCMISEN
| EP_STS_EN_TRBERREN
,
857 cdns3_select_ep(priv_dev
, USB_DIR_IN
);
859 writel(EP_CFG_ENABLE
| EP_CFG_MAXPKTSIZE(max_packet_size
),
862 writel(EP_STS_EN_SETUPEN
| EP_STS_EN_TRBERREN
, ®s
->ep_sts_en
);
864 cdns3_set_register_bit(®s
->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
);