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
= TRB_BUFFER(dma_addr
);
41 priv_ep
->trb_pool
[0].length
= TRB_LEN(length
);
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
|
50 priv_ep
->trb_pool
[0].control
= TRB_CYCLE
| TRB_IOC
|
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(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
);
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)
330 switch (tmode
>> 8) {
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.
342 cdns3_set_register_bit(&priv_dev
->regs
->usb_cmd
,
344 USB_STS_TMODE_SEL(tmode
- 1));
357 static int cdns3_ep0_feature_handle_intf(struct cdns3_device
*priv_dev
,
358 struct usb_ctrlrequest
*ctrl
,
364 wValue
= le16_to_cpu(ctrl
->wValue
);
367 case USB_INTRF_FUNC_SUSPEND
:
376 static int cdns3_ep0_feature_handle_endpoint(struct cdns3_device
*priv_dev
,
377 struct usb_ctrlrequest
*ctrl
,
380 struct cdns3_endpoint
*priv_ep
;
384 if (le16_to_cpu(ctrl
->wValue
) != USB_ENDPOINT_HALT
)
387 if (!(ctrl
->wIndex
& ~USB_DIR_IN
))
390 index
= cdns3_ep_addr_to_index(ctrl
->wIndex
);
391 priv_ep
= priv_dev
->eps
[index
];
393 cdns3_select_ep(priv_dev
, ctrl
->wIndex
);
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);
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
,
422 recip
= ctrl
->bRequestType
& USB_RECIP_MASK
;
425 case USB_RECIP_DEVICE
:
426 ret
= cdns3_ep0_feature_handle_device(priv_dev
, ctrl
, set
);
428 case USB_RECIP_INTERFACE
:
429 ret
= cdns3_ep0_feature_handle_intf(priv_dev
, ctrl
, set
);
431 case USB_RECIP_ENDPOINT
:
432 ret
= cdns3_ep0_feature_handle_endpoint(priv_dev
, ctrl
, set
);
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
)
454 if (ctrl_req
->wLength
!= 6) {
455 dev_err(priv_dev
->dev
, "Set SEL should be 6 bytes, got %d\n",
460 cdns3_ep0_run_transfer(priv_dev
, priv_dev
->setup_dma
, 6, 1, 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
)
478 priv_dev
->isoch_delay
= ctrl_req
->wValue
;
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
)
495 switch (ctrl_req
->bRequest
) {
496 case USB_REQ_SET_ADDRESS
:
497 ret
= cdns3_req_ep0_set_address(priv_dev
, ctrl_req
);
499 case USB_REQ_SET_CONFIGURATION
:
500 ret
= cdns3_req_ep0_set_configuration(priv_dev
, ctrl_req
);
502 case USB_REQ_GET_STATUS
:
503 ret
= cdns3_req_ep0_get_status(priv_dev
, ctrl_req
);
505 case USB_REQ_CLEAR_FEATURE
:
506 ret
= cdns3_req_ep0_handle_feature(priv_dev
, ctrl_req
, 0);
508 case USB_REQ_SET_FEATURE
:
509 ret
= cdns3_req_ep0_handle_feature(priv_dev
, ctrl_req
, 1);
511 case USB_REQ_SET_SEL
:
512 ret
= cdns3_req_ep0_set_sel(priv_dev
, ctrl_req
);
514 case USB_REQ_SET_ISOCH_DELAY
:
515 ret
= cdns3_req_ep0_set_isoch_delay(priv_dev
, ctrl_req
);
518 ret
= cdns3_ep0_delegate_req(priv_dev
, ctrl_req
);
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
&&
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
,
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];
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
),
570 if (le16_to_cpu(ctrl
->wLength
))
571 priv_dev
->ep0_stage
= CDNS3_DATA_STAGE
;
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
);
578 result
= cdns3_ep0_delegate_req(priv_dev
, ctrl
);
580 if (result
== USB_GADGET_DELAYED_STATUS
)
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
);
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
)
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
)
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
)
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
)
683 * cdns3_gadget_ep0_set_halt
684 * @ep: pointer to endpoint zero object
685 * @value: 1 for set stall, 0 for clear stall
689 static int cdns3_gadget_ep0_set_halt(struct usb_ep
*ep
, int value
)
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
,
707 struct cdns3_endpoint
*priv_ep
= ep_to_cdns3_ep(ep
);
708 struct cdns3_device
*priv_dev
= priv_ep
->cdns3_dev
;
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
))
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
);
729 cdns3_ep0_complete_setup(priv_dev
, 0, 1);
731 cdns3_allow_enable_l1(priv_dev
, 1);
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
743 queue_work(system_freezable_wq
, &priv_dev
->pending_status_wq
);
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
);
755 ret
= usb_gadget_map_request_by_dev(priv_dev
->sysdev
, request
,
756 priv_dev
->ep0_data_dir
);
758 spin_unlock_irqrestore(&priv_dev
->lock
, flags
);
759 dev_err(priv_dev
->dev
, "failed to map request\n");
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))
770 cdns3_ep0_run_transfer(priv_dev
, request
->dma
, request
->length
, 1, zlp
);
772 spin_unlock_irqrestore(&priv_dev
->lock
, flags
);
778 * cdns3_gadget_ep_set_wedge Set wedge on selected endpoint
779 * @ep: endpoint object
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
;
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
);
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
,
844 cdns3_set_register_bit(&priv_dev
->regs
->tdl_from_trb
,
848 writel(EP_CFG_ENABLE
| EP_CFG_MAXPKTSIZE(max_packet_size
),
851 writel(EP_STS_EN_SETUPEN
| EP_STS_EN_DESCMISEN
| EP_STS_EN_TRBERREN
,
855 cdns3_select_ep(priv_dev
, USB_DIR_IN
);
857 writel(EP_CFG_ENABLE
| EP_CFG_MAXPKTSIZE(max_packet_size
),
860 writel(EP_STS_EN_SETUPEN
| EP_STS_EN_TRBERREN
, ®s
->ep_sts_en
);
862 cdns3_set_register_bit(®s
->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
);