1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence CDNSP DRD Driver.
5 * Copyright (C) 2020 Cadence.
7 * Author: Pawel Laszczak <pawell@cadence.com>
11 #include <linux/usb/composite.h>
12 #include <linux/usb/gadget.h>
13 #include <linux/list.h>
15 #include "cdnsp-gadget.h"
16 #include "cdnsp-trace.h"
18 static void cdnsp_ep0_stall(struct cdnsp_device
*pdev
)
20 struct cdnsp_request
*preq
;
24 preq
= next_request(&pep
->pending_list
);
26 if (pdev
->three_stage_setup
) {
27 cdnsp_halt_endpoint(pdev
, pep
, true);
30 cdnsp_gadget_giveback(pep
, preq
, -ECONNRESET
);
32 pep
->ep_state
|= EP0_HALTED_STATUS
;
35 list_del(&preq
->list
);
37 cdnsp_status_stage(pdev
);
41 static int cdnsp_ep0_delegate_req(struct cdnsp_device
*pdev
,
42 struct usb_ctrlrequest
*ctrl
)
46 spin_unlock(&pdev
->lock
);
47 ret
= pdev
->gadget_driver
->setup(&pdev
->gadget
, ctrl
);
48 spin_lock(&pdev
->lock
);
53 static int cdnsp_ep0_set_config(struct cdnsp_device
*pdev
,
54 struct usb_ctrlrequest
*ctrl
)
56 enum usb_device_state state
= pdev
->gadget
.state
;
60 cfg
= le16_to_cpu(ctrl
->wValue
);
63 case USB_STATE_ADDRESS
:
64 trace_cdnsp_ep0_set_config("from Address state");
66 case USB_STATE_CONFIGURED
:
67 trace_cdnsp_ep0_set_config("from Configured state");
70 dev_err(pdev
->dev
, "Set Configuration - bad device state\n");
74 ret
= cdnsp_ep0_delegate_req(pdev
, ctrl
);
79 usb_gadget_set_state(&pdev
->gadget
, USB_STATE_ADDRESS
);
84 static int cdnsp_ep0_set_address(struct cdnsp_device
*pdev
,
85 struct usb_ctrlrequest
*ctrl
)
87 enum usb_device_state state
= pdev
->gadget
.state
;
88 struct cdnsp_slot_ctx
*slot_ctx
;
89 unsigned int slot_state
;
93 addr
= le16_to_cpu(ctrl
->wValue
);
96 dev_err(pdev
->dev
, "Invalid device address %d\n", addr
);
100 slot_ctx
= cdnsp_get_slot_ctx(&pdev
->out_ctx
);
102 if (state
== USB_STATE_CONFIGURED
) {
103 dev_err(pdev
->dev
, "Can't Set Address from Configured State\n");
107 pdev
->device_address
= le16_to_cpu(ctrl
->wValue
);
109 slot_ctx
= cdnsp_get_slot_ctx(&pdev
->out_ctx
);
110 slot_state
= GET_SLOT_STATE(le32_to_cpu(slot_ctx
->dev_state
));
111 if (slot_state
== SLOT_STATE_ADDRESSED
)
112 cdnsp_reset_device(pdev
);
114 /*set device address*/
115 ret
= cdnsp_setup_device(pdev
, SETUP_CONTEXT_ADDRESS
);
120 usb_gadget_set_state(&pdev
->gadget
, USB_STATE_ADDRESS
);
122 usb_gadget_set_state(&pdev
->gadget
, USB_STATE_DEFAULT
);
127 int cdnsp_status_stage(struct cdnsp_device
*pdev
)
129 pdev
->ep0_stage
= CDNSP_STATUS_STAGE
;
130 pdev
->ep0_preq
.request
.length
= 0;
132 return cdnsp_ep_enqueue(pdev
->ep0_preq
.pep
, &pdev
->ep0_preq
);
135 static int cdnsp_w_index_to_ep_index(u16 wIndex
)
137 if (!(wIndex
& USB_ENDPOINT_NUMBER_MASK
))
140 return ((wIndex
& USB_ENDPOINT_NUMBER_MASK
) * 2) +
141 (wIndex
& USB_ENDPOINT_DIR_MASK
? 1 : 0) - 1;
144 static int cdnsp_ep0_handle_status(struct cdnsp_device
*pdev
,
145 struct usb_ctrlrequest
*ctrl
)
147 struct cdnsp_ep
*pep
;
153 recipient
= ctrl
->bRequestType
& USB_RECIP_MASK
;
156 case USB_RECIP_DEVICE
:
157 status
= pdev
->gadget
.is_selfpowered
;
158 status
|= pdev
->may_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
;
160 if (pdev
->gadget
.speed
>= USB_SPEED_SUPER
) {
161 status
|= pdev
->u1_allowed
<< USB_DEV_STAT_U1_ENABLED
;
162 status
|= pdev
->u2_allowed
<< USB_DEV_STAT_U2_ENABLED
;
165 case USB_RECIP_INTERFACE
:
167 * Function Remote Wake Capable D0
168 * Function Remote Wakeup D1
170 return cdnsp_ep0_delegate_req(pdev
, ctrl
);
171 case USB_RECIP_ENDPOINT
:
172 ep_sts
= cdnsp_w_index_to_ep_index(le16_to_cpu(ctrl
->wIndex
));
173 pep
= &pdev
->eps
[ep_sts
];
174 ep_sts
= GET_EP_CTX_STATE(pep
->out_ctx
);
176 /* check if endpoint is stalled */
177 if (ep_sts
== EP_STATE_HALTED
)
178 status
= BIT(USB_ENDPOINT_HALT
);
184 response
= (__le16
*)pdev
->setup_buf
;
185 *response
= cpu_to_le16(status
);
187 pdev
->ep0_preq
.request
.length
= sizeof(*response
);
188 pdev
->ep0_preq
.request
.buf
= pdev
->setup_buf
;
190 return cdnsp_ep_enqueue(pdev
->ep0_preq
.pep
, &pdev
->ep0_preq
);
193 static void cdnsp_enter_test_mode(struct cdnsp_device
*pdev
)
197 temp
= readl(&pdev
->active_port
->regs
->portpmsc
) & ~GENMASK(31, 28);
198 temp
|= PORT_TEST_MODE(pdev
->test_mode
);
199 writel(temp
, &pdev
->active_port
->regs
->portpmsc
);
202 static int cdnsp_ep0_handle_feature_device(struct cdnsp_device
*pdev
,
203 struct usb_ctrlrequest
*ctrl
,
206 enum usb_device_state state
;
207 enum usb_device_speed speed
;
210 state
= pdev
->gadget
.state
;
211 speed
= pdev
->gadget
.speed
;
213 switch (le16_to_cpu(ctrl
->wValue
)) {
214 case USB_DEVICE_REMOTE_WAKEUP
:
215 pdev
->may_wakeup
= !!set
;
216 trace_cdnsp_may_wakeup(set
);
218 case USB_DEVICE_U1_ENABLE
:
219 if (state
!= USB_STATE_CONFIGURED
|| speed
< USB_SPEED_SUPER
)
222 pdev
->u1_allowed
= !!set
;
225 case USB_DEVICE_U2_ENABLE
:
226 if (state
!= USB_STATE_CONFIGURED
|| speed
< USB_SPEED_SUPER
)
229 pdev
->u2_allowed
= !!set
;
232 case USB_DEVICE_LTM_ENABLE
:
234 case USB_DEVICE_TEST_MODE
:
235 if (state
!= USB_STATE_CONFIGURED
|| speed
> USB_SPEED_HIGH
)
238 tmode
= le16_to_cpu(ctrl
->wIndex
);
240 if (!set
|| (tmode
& 0xff) != 0)
245 if (tmode
> USB_TEST_FORCE_ENABLE
|| tmode
< USB_TEST_J
)
248 pdev
->test_mode
= tmode
;
251 * Test mode must be set before Status Stage but controller
252 * will start testing sequence after Status Stage.
254 cdnsp_enter_test_mode(pdev
);
263 static int cdnsp_ep0_handle_feature_intf(struct cdnsp_device
*pdev
,
264 struct usb_ctrlrequest
*ctrl
,
270 wValue
= le16_to_cpu(ctrl
->wValue
);
271 wIndex
= le16_to_cpu(ctrl
->wIndex
);
274 case USB_INTRF_FUNC_SUSPEND
:
275 ret
= cdnsp_ep0_delegate_req(pdev
, ctrl
);
280 * Remote wakeup is enabled when any function within a device
281 * is enabled for function remote wakeup.
283 if (wIndex
& USB_INTRF_FUNC_SUSPEND_RW
)
286 if (pdev
->may_wakeup
> 0)
297 static int cdnsp_ep0_handle_feature_endpoint(struct cdnsp_device
*pdev
,
298 struct usb_ctrlrequest
*ctrl
,
301 struct cdnsp_ep
*pep
;
304 wValue
= le16_to_cpu(ctrl
->wValue
);
305 pep
= &pdev
->eps
[cdnsp_w_index_to_ep_index(le16_to_cpu(ctrl
->wIndex
))];
308 case USB_ENDPOINT_HALT
:
309 if (!set
&& (pep
->ep_state
& EP_WEDGE
)) {
310 /* Resets Sequence Number */
311 cdnsp_halt_endpoint(pdev
, pep
, 0);
312 cdnsp_halt_endpoint(pdev
, pep
, 1);
316 return cdnsp_halt_endpoint(pdev
, pep
, set
);
318 dev_warn(pdev
->dev
, "WARN Incorrect wValue %04x\n", wValue
);
325 static int cdnsp_ep0_handle_feature(struct cdnsp_device
*pdev
,
326 struct usb_ctrlrequest
*ctrl
,
329 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
330 case USB_RECIP_DEVICE
:
331 return cdnsp_ep0_handle_feature_device(pdev
, ctrl
, set
);
332 case USB_RECIP_INTERFACE
:
333 return cdnsp_ep0_handle_feature_intf(pdev
, ctrl
, set
);
334 case USB_RECIP_ENDPOINT
:
335 return cdnsp_ep0_handle_feature_endpoint(pdev
, ctrl
, set
);
341 static int cdnsp_ep0_set_sel(struct cdnsp_device
*pdev
,
342 struct usb_ctrlrequest
*ctrl
)
344 enum usb_device_state state
= pdev
->gadget
.state
;
347 if (state
== USB_STATE_DEFAULT
)
350 wLength
= le16_to_cpu(ctrl
->wLength
);
353 dev_err(pdev
->dev
, "Set SEL should be 6 bytes, got %d\n",
359 * To handle Set SEL we need to receive 6 bytes from Host. So let's
360 * queue a usb_request for 6 bytes.
362 pdev
->ep0_preq
.request
.length
= 6;
363 pdev
->ep0_preq
.request
.buf
= pdev
->setup_buf
;
365 return cdnsp_ep_enqueue(pdev
->ep0_preq
.pep
, &pdev
->ep0_preq
);
368 static int cdnsp_ep0_set_isoch_delay(struct cdnsp_device
*pdev
,
369 struct usb_ctrlrequest
*ctrl
)
371 if (le16_to_cpu(ctrl
->wIndex
) || le16_to_cpu(ctrl
->wLength
))
374 pdev
->gadget
.isoch_delay
= le16_to_cpu(ctrl
->wValue
);
379 static int cdnsp_ep0_std_request(struct cdnsp_device
*pdev
,
380 struct usb_ctrlrequest
*ctrl
)
384 switch (ctrl
->bRequest
) {
385 case USB_REQ_GET_STATUS
:
386 ret
= cdnsp_ep0_handle_status(pdev
, ctrl
);
388 case USB_REQ_CLEAR_FEATURE
:
389 ret
= cdnsp_ep0_handle_feature(pdev
, ctrl
, 0);
391 case USB_REQ_SET_FEATURE
:
392 ret
= cdnsp_ep0_handle_feature(pdev
, ctrl
, 1);
394 case USB_REQ_SET_ADDRESS
:
395 ret
= cdnsp_ep0_set_address(pdev
, ctrl
);
397 case USB_REQ_SET_CONFIGURATION
:
398 ret
= cdnsp_ep0_set_config(pdev
, ctrl
);
400 case USB_REQ_SET_SEL
:
401 ret
= cdnsp_ep0_set_sel(pdev
, ctrl
);
403 case USB_REQ_SET_ISOCH_DELAY
:
404 ret
= cdnsp_ep0_set_isoch_delay(pdev
, ctrl
);
407 ret
= cdnsp_ep0_delegate_req(pdev
, ctrl
);
414 void cdnsp_setup_analyze(struct cdnsp_device
*pdev
)
416 struct usb_ctrlrequest
*ctrl
= &pdev
->setup
;
420 trace_cdnsp_ctrl_req(ctrl
);
422 if (!pdev
->gadget_driver
)
425 if (pdev
->gadget
.state
== USB_STATE_NOTATTACHED
) {
426 dev_err(pdev
->dev
, "ERR: Setup detected in unattached state\n");
430 /* Restore the ep0 to Stopped/Running state. */
431 if (pdev
->eps
[0].ep_state
& EP_HALTED
) {
432 trace_cdnsp_ep0_halted("Restore to normal state");
433 cdnsp_halt_endpoint(pdev
, &pdev
->eps
[0], 0);
437 * Finishing previous SETUP transfer by removing request from
438 * list and informing upper layer
440 if (!list_empty(&pdev
->eps
[0].pending_list
)) {
441 struct cdnsp_request
*req
;
443 trace_cdnsp_ep0_request("Remove previous");
444 req
= next_request(&pdev
->eps
[0].pending_list
);
445 cdnsp_ep_dequeue(&pdev
->eps
[0], req
);
448 len
= le16_to_cpu(ctrl
->wLength
);
450 pdev
->three_stage_setup
= false;
451 pdev
->ep0_expect_in
= false;
453 pdev
->three_stage_setup
= true;
454 pdev
->ep0_expect_in
= !!(ctrl
->bRequestType
& USB_DIR_IN
);
457 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
)
458 ret
= cdnsp_ep0_std_request(pdev
, ctrl
);
460 ret
= cdnsp_ep0_delegate_req(pdev
, ctrl
);
462 if (ret
== USB_GADGET_DELAYED_STATUS
) {
463 trace_cdnsp_ep0_status_stage("delayed");
468 cdnsp_ep0_stall(pdev
);
469 else if (!len
&& pdev
->ep0_stage
!= CDNSP_STATUS_STAGE
)
470 cdnsp_status_stage(pdev
);