drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / drivers / usb / cdns3 / cdnsp-ep0.c
blobf317d3c84781082d00d27f13481a5f759e7d1515
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Cadence CDNSP DRD Driver.
5 * Copyright (C) 2020 Cadence.
7 * Author: Pawel Laszczak <pawell@cadence.com>
9 */
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;
21 struct cdnsp_ep *pep;
23 pep = &pdev->eps[0];
24 preq = next_request(&pep->pending_list);
26 if (pdev->three_stage_setup) {
27 cdnsp_halt_endpoint(pdev, pep, true);
29 if (preq)
30 cdnsp_gadget_giveback(pep, preq, -ECONNRESET);
31 } else {
32 pep->ep_state |= EP0_HALTED_STATUS;
34 if (preq)
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)
44 int ret;
46 spin_unlock(&pdev->lock);
47 ret = pdev->gadget_driver->setup(&pdev->gadget, ctrl);
48 spin_lock(&pdev->lock);
50 return ret;
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;
57 u32 cfg;
58 int ret;
60 cfg = le16_to_cpu(ctrl->wValue);
62 switch (state) {
63 case USB_STATE_ADDRESS:
64 trace_cdnsp_ep0_set_config("from Address state");
65 break;
66 case USB_STATE_CONFIGURED:
67 trace_cdnsp_ep0_set_config("from Configured state");
68 break;
69 default:
70 dev_err(pdev->dev, "Set Configuration - bad device state\n");
71 return -EINVAL;
74 ret = cdnsp_ep0_delegate_req(pdev, ctrl);
75 if (ret)
76 return ret;
78 if (!cfg)
79 usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
81 return 0;
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;
90 int ret;
91 u32 addr;
93 addr = le16_to_cpu(ctrl->wValue);
95 if (addr > 127) {
96 dev_err(pdev->dev, "Invalid device address %d\n", addr);
97 return -EINVAL;
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");
104 return -EINVAL;
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);
116 if (ret)
117 return ret;
119 if (addr)
120 usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
121 else
122 usb_gadget_set_state(&pdev->gadget, USB_STATE_DEFAULT);
124 return 0;
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))
138 return 0;
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;
148 __le16 *response;
149 int ep_sts = 0;
150 u16 status = 0;
151 u32 recipient;
153 recipient = ctrl->bRequestType & USB_RECIP_MASK;
155 switch (recipient) {
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;
164 break;
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);
179 break;
180 default:
181 return -EINVAL;
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)
195 u32 temp;
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,
204 int set)
206 enum usb_device_state state;
207 enum usb_device_speed speed;
208 u16 tmode;
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);
217 break;
218 case USB_DEVICE_U1_ENABLE:
219 if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER)
220 return -EINVAL;
222 pdev->u1_allowed = !!set;
223 trace_cdnsp_u1(set);
224 break;
225 case USB_DEVICE_U2_ENABLE:
226 if (state != USB_STATE_CONFIGURED || speed < USB_SPEED_SUPER)
227 return -EINVAL;
229 pdev->u2_allowed = !!set;
230 trace_cdnsp_u2(set);
231 break;
232 case USB_DEVICE_LTM_ENABLE:
233 return -EINVAL;
234 case USB_DEVICE_TEST_MODE:
235 if (state != USB_STATE_CONFIGURED || speed > USB_SPEED_HIGH)
236 return -EINVAL;
238 tmode = le16_to_cpu(ctrl->wIndex);
240 if (!set || (tmode & 0xff) != 0)
241 return -EINVAL;
243 tmode = tmode >> 8;
245 if (tmode > USB_TEST_FORCE_ENABLE || tmode < USB_TEST_J)
246 return -EINVAL;
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);
255 break;
256 default:
257 return -EINVAL;
260 return 0;
263 static int cdnsp_ep0_handle_feature_intf(struct cdnsp_device *pdev,
264 struct usb_ctrlrequest *ctrl,
265 int set)
267 u16 wValue, wIndex;
268 int ret;
270 wValue = le16_to_cpu(ctrl->wValue);
271 wIndex = le16_to_cpu(ctrl->wIndex);
273 switch (wValue) {
274 case USB_INTRF_FUNC_SUSPEND:
275 ret = cdnsp_ep0_delegate_req(pdev, ctrl);
276 if (ret)
277 return ret;
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)
284 pdev->may_wakeup++;
285 else
286 if (pdev->may_wakeup > 0)
287 pdev->may_wakeup--;
289 return 0;
290 default:
291 return -EINVAL;
294 return 0;
297 static int cdnsp_ep0_handle_feature_endpoint(struct cdnsp_device *pdev,
298 struct usb_ctrlrequest *ctrl,
299 int set)
301 struct cdnsp_ep *pep;
302 u16 wValue;
304 wValue = le16_to_cpu(ctrl->wValue);
305 pep = &pdev->eps[cdnsp_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex))];
307 switch (wValue) {
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);
313 break;
316 return cdnsp_halt_endpoint(pdev, pep, set);
317 default:
318 dev_warn(pdev->dev, "WARN Incorrect wValue %04x\n", wValue);
319 return -EINVAL;
322 return 0;
325 static int cdnsp_ep0_handle_feature(struct cdnsp_device *pdev,
326 struct usb_ctrlrequest *ctrl,
327 int set)
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);
336 default:
337 return -EINVAL;
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;
345 u16 wLength;
347 if (state == USB_STATE_DEFAULT)
348 return -EINVAL;
350 wLength = le16_to_cpu(ctrl->wLength);
352 if (wLength != 6) {
353 dev_err(pdev->dev, "Set SEL should be 6 bytes, got %d\n",
354 wLength);
355 return -EINVAL;
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))
372 return -EINVAL;
374 pdev->gadget.isoch_delay = le16_to_cpu(ctrl->wValue);
376 return 0;
379 static int cdnsp_ep0_std_request(struct cdnsp_device *pdev,
380 struct usb_ctrlrequest *ctrl)
382 int ret;
384 switch (ctrl->bRequest) {
385 case USB_REQ_GET_STATUS:
386 ret = cdnsp_ep0_handle_status(pdev, ctrl);
387 break;
388 case USB_REQ_CLEAR_FEATURE:
389 ret = cdnsp_ep0_handle_feature(pdev, ctrl, 0);
390 break;
391 case USB_REQ_SET_FEATURE:
392 ret = cdnsp_ep0_handle_feature(pdev, ctrl, 1);
393 break;
394 case USB_REQ_SET_ADDRESS:
395 ret = cdnsp_ep0_set_address(pdev, ctrl);
396 break;
397 case USB_REQ_SET_CONFIGURATION:
398 ret = cdnsp_ep0_set_config(pdev, ctrl);
399 break;
400 case USB_REQ_SET_SEL:
401 ret = cdnsp_ep0_set_sel(pdev, ctrl);
402 break;
403 case USB_REQ_SET_ISOCH_DELAY:
404 ret = cdnsp_ep0_set_isoch_delay(pdev, ctrl);
405 break;
406 default:
407 ret = cdnsp_ep0_delegate_req(pdev, ctrl);
408 break;
411 return ret;
414 void cdnsp_setup_analyze(struct cdnsp_device *pdev)
416 struct usb_ctrlrequest *ctrl = &pdev->setup;
417 int ret = -EINVAL;
418 u16 len;
420 trace_cdnsp_ctrl_req(ctrl);
422 if (!pdev->gadget_driver)
423 goto out;
425 if (pdev->gadget.state == USB_STATE_NOTATTACHED) {
426 dev_err(pdev->dev, "ERR: Setup detected in unattached state\n");
427 goto out;
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);
449 if (!len) {
450 pdev->three_stage_setup = false;
451 pdev->ep0_expect_in = false;
452 } else {
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);
459 else
460 ret = cdnsp_ep0_delegate_req(pdev, ctrl);
462 if (ret == USB_GADGET_DELAYED_STATUS) {
463 trace_cdnsp_ep0_status_stage("delayed");
464 return;
466 out:
467 if (ret < 0)
468 cdnsp_ep0_stall(pdev);
469 else if (!len && pdev->ep0_stage != CDNSP_STATUS_STAGE)
470 cdnsp_status_stage(pdev);