xref: /openbmc/linux/drivers/usb/cdns3/cdnsp-ep0.c (revision 1edf4899)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cadence CDNSP DRD Driver.
4  *
5  * Copyright (C) 2020 Cadence.
6  *
7  * Author: Pawel Laszczak <pawell@cadence.com>
8  *
9  */
10 
11 #include <linux/usb/composite.h>
12 #include <linux/usb/gadget.h>
13 #include <linux/list.h>
14 
15 #include "cdnsp-gadget.h"
16 #include "cdnsp-trace.h"
17 
cdnsp_ep0_stall(struct cdnsp_device * pdev)18 static void cdnsp_ep0_stall(struct cdnsp_device *pdev)
19 {
20 	struct cdnsp_request *preq;
21 	struct cdnsp_ep *pep;
22 
23 	pep = &pdev->eps[0];
24 	preq = next_request(&pep->pending_list);
25 
26 	if (pdev->three_stage_setup) {
27 		cdnsp_halt_endpoint(pdev, pep, true);
28 
29 		if (preq)
30 			cdnsp_gadget_giveback(pep, preq, -ECONNRESET);
31 	} else {
32 		pep->ep_state |= EP0_HALTED_STATUS;
33 
34 		if (preq)
35 			list_del(&preq->list);
36 
37 		cdnsp_status_stage(pdev);
38 	}
39 }
40 
cdnsp_ep0_delegate_req(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl)41 static int cdnsp_ep0_delegate_req(struct cdnsp_device *pdev,
42 				  struct usb_ctrlrequest *ctrl)
43 {
44 	int ret;
45 
46 	spin_unlock(&pdev->lock);
47 	ret = pdev->gadget_driver->setup(&pdev->gadget, ctrl);
48 	spin_lock(&pdev->lock);
49 
50 	return ret;
51 }
52 
cdnsp_ep0_set_config(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl)53 static int cdnsp_ep0_set_config(struct cdnsp_device *pdev,
54 				struct usb_ctrlrequest *ctrl)
55 {
56 	enum usb_device_state state = pdev->gadget.state;
57 	u32 cfg;
58 	int ret;
59 
60 	cfg = le16_to_cpu(ctrl->wValue);
61 
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;
72 	}
73 
74 	ret = cdnsp_ep0_delegate_req(pdev, ctrl);
75 	if (ret)
76 		return ret;
77 
78 	if (!cfg)
79 		usb_gadget_set_state(&pdev->gadget, USB_STATE_ADDRESS);
80 
81 	return 0;
82 }
83 
cdnsp_ep0_set_address(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl)84 static int cdnsp_ep0_set_address(struct cdnsp_device *pdev,
85 				 struct usb_ctrlrequest *ctrl)
86 {
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;
92 
93 	addr = le16_to_cpu(ctrl->wValue);
94 
95 	if (addr > 127) {
96 		dev_err(pdev->dev, "Invalid device address %d\n", addr);
97 		return -EINVAL;
98 	}
99 
100 	slot_ctx = cdnsp_get_slot_ctx(&pdev->out_ctx);
101 
102 	if (state == USB_STATE_CONFIGURED) {
103 		dev_err(pdev->dev, "Can't Set Address from Configured State\n");
104 		return -EINVAL;
105 	}
106 
107 	pdev->device_address = le16_to_cpu(ctrl->wValue);
108 
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);
113 
114 	/*set device address*/
115 	ret = cdnsp_setup_device(pdev, SETUP_CONTEXT_ADDRESS);
116 	if (ret)
117 		return ret;
118 
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);
123 
124 	return 0;
125 }
126 
cdnsp_status_stage(struct cdnsp_device * pdev)127 int cdnsp_status_stage(struct cdnsp_device *pdev)
128 {
129 	pdev->ep0_stage = CDNSP_STATUS_STAGE;
130 	pdev->ep0_preq.request.length = 0;
131 
132 	return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
133 }
134 
cdnsp_w_index_to_ep_index(u16 wIndex)135 static int cdnsp_w_index_to_ep_index(u16 wIndex)
136 {
137 	if (!(wIndex & USB_ENDPOINT_NUMBER_MASK))
138 		return 0;
139 
140 	return ((wIndex & USB_ENDPOINT_NUMBER_MASK) * 2) +
141 		(wIndex & USB_ENDPOINT_DIR_MASK ? 1 : 0) - 1;
142 }
143 
cdnsp_ep0_handle_status(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl)144 static int cdnsp_ep0_handle_status(struct cdnsp_device *pdev,
145 				   struct usb_ctrlrequest *ctrl)
146 {
147 	struct cdnsp_ep *pep;
148 	__le16 *response;
149 	int ep_sts = 0;
150 	u16 status = 0;
151 	u32 recipient;
152 
153 	recipient = ctrl->bRequestType & USB_RECIP_MASK;
154 
155 	switch (recipient) {
156 	case USB_RECIP_DEVICE:
157 		status = pdev->gadget.is_selfpowered;
158 		status |= pdev->may_wakeup << USB_DEVICE_REMOTE_WAKEUP;
159 
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;
163 		}
164 		break;
165 	case USB_RECIP_INTERFACE:
166 		/*
167 		 * Function Remote Wake Capable	D0
168 		 * Function Remote Wakeup	D1
169 		 */
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);
175 
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;
182 	}
183 
184 	response = (__le16 *)pdev->setup_buf;
185 	*response = cpu_to_le16(status);
186 
187 	pdev->ep0_preq.request.length = sizeof(*response);
188 	pdev->ep0_preq.request.buf = pdev->setup_buf;
189 
190 	return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
191 }
192 
cdnsp_enter_test_mode(struct cdnsp_device * pdev)193 static void cdnsp_enter_test_mode(struct cdnsp_device *pdev)
194 {
195 	u32 temp;
196 
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);
200 }
201 
cdnsp_ep0_handle_feature_device(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl,int set)202 static int cdnsp_ep0_handle_feature_device(struct cdnsp_device *pdev,
203 					   struct usb_ctrlrequest *ctrl,
204 					   int set)
205 {
206 	enum usb_device_state state;
207 	enum usb_device_speed speed;
208 	u16 tmode;
209 
210 	state = pdev->gadget.state;
211 	speed = pdev->gadget.speed;
212 
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;
221 
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;
228 
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;
237 
238 		tmode = le16_to_cpu(ctrl->wIndex);
239 
240 		if (!set || (tmode & 0xff) != 0)
241 			return -EINVAL;
242 
243 		tmode = tmode >> 8;
244 
245 		if (tmode > USB_TEST_FORCE_ENABLE || tmode < USB_TEST_J)
246 			return -EINVAL;
247 
248 		pdev->test_mode = tmode;
249 
250 		/*
251 		 * Test mode must be set before Status Stage but controller
252 		 * will start testing sequence after Status Stage.
253 		 */
254 		cdnsp_enter_test_mode(pdev);
255 		break;
256 	default:
257 		return -EINVAL;
258 	}
259 
260 	return 0;
261 }
262 
cdnsp_ep0_handle_feature_intf(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl,int set)263 static int cdnsp_ep0_handle_feature_intf(struct cdnsp_device *pdev,
264 					 struct usb_ctrlrequest *ctrl,
265 					 int set)
266 {
267 	u16 wValue, wIndex;
268 	int ret;
269 
270 	wValue = le16_to_cpu(ctrl->wValue);
271 	wIndex = le16_to_cpu(ctrl->wIndex);
272 
273 	switch (wValue) {
274 	case USB_INTRF_FUNC_SUSPEND:
275 		ret = cdnsp_ep0_delegate_req(pdev, ctrl);
276 		if (ret)
277 			return ret;
278 
279 		/*
280 		 * Remote wakeup is enabled when any function within a device
281 		 * is enabled for function remote wakeup.
282 		 */
283 		if (wIndex & USB_INTRF_FUNC_SUSPEND_RW)
284 			pdev->may_wakeup++;
285 		else
286 			if (pdev->may_wakeup > 0)
287 				pdev->may_wakeup--;
288 
289 		return 0;
290 	default:
291 		return -EINVAL;
292 	}
293 
294 	return 0;
295 }
296 
cdnsp_ep0_handle_feature_endpoint(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl,int set)297 static int cdnsp_ep0_handle_feature_endpoint(struct cdnsp_device *pdev,
298 					     struct usb_ctrlrequest *ctrl,
299 					     int set)
300 {
301 	struct cdnsp_ep *pep;
302 	u16 wValue;
303 
304 	wValue = le16_to_cpu(ctrl->wValue);
305 	pep = &pdev->eps[cdnsp_w_index_to_ep_index(le16_to_cpu(ctrl->wIndex))];
306 
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;
314 		}
315 
316 		return cdnsp_halt_endpoint(pdev, pep, set);
317 	default:
318 		dev_warn(pdev->dev, "WARN Incorrect wValue %04x\n", wValue);
319 		return -EINVAL;
320 	}
321 
322 	return 0;
323 }
324 
cdnsp_ep0_handle_feature(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl,int set)325 static int cdnsp_ep0_handle_feature(struct cdnsp_device *pdev,
326 				    struct usb_ctrlrequest *ctrl,
327 				    int set)
328 {
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;
338 	}
339 }
340 
cdnsp_ep0_set_sel(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl)341 static int cdnsp_ep0_set_sel(struct cdnsp_device *pdev,
342 			     struct usb_ctrlrequest *ctrl)
343 {
344 	enum usb_device_state state = pdev->gadget.state;
345 	u16 wLength;
346 
347 	if (state == USB_STATE_DEFAULT)
348 		return -EINVAL;
349 
350 	wLength = le16_to_cpu(ctrl->wLength);
351 
352 	if (wLength != 6) {
353 		dev_err(pdev->dev, "Set SEL should be 6 bytes, got %d\n",
354 			wLength);
355 		return -EINVAL;
356 	}
357 
358 	/*
359 	 * To handle Set SEL we need to receive 6 bytes from Host. So let's
360 	 * queue a usb_request for 6 bytes.
361 	 */
362 	pdev->ep0_preq.request.length = 6;
363 	pdev->ep0_preq.request.buf = pdev->setup_buf;
364 
365 	return cdnsp_ep_enqueue(pdev->ep0_preq.pep, &pdev->ep0_preq);
366 }
367 
cdnsp_ep0_set_isoch_delay(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl)368 static int cdnsp_ep0_set_isoch_delay(struct cdnsp_device *pdev,
369 				     struct usb_ctrlrequest *ctrl)
370 {
371 	if (le16_to_cpu(ctrl->wIndex) || le16_to_cpu(ctrl->wLength))
372 		return -EINVAL;
373 
374 	pdev->gadget.isoch_delay = le16_to_cpu(ctrl->wValue);
375 
376 	return 0;
377 }
378 
cdnsp_ep0_std_request(struct cdnsp_device * pdev,struct usb_ctrlrequest * ctrl)379 static int cdnsp_ep0_std_request(struct cdnsp_device *pdev,
380 				 struct usb_ctrlrequest *ctrl)
381 {
382 	int ret;
383 
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;
409 	}
410 
411 	return ret;
412 }
413 
cdnsp_setup_analyze(struct cdnsp_device * pdev)414 void cdnsp_setup_analyze(struct cdnsp_device *pdev)
415 {
416 	struct usb_ctrlrequest *ctrl = &pdev->setup;
417 	int ret = -EINVAL;
418 	u16 len;
419 
420 	trace_cdnsp_ctrl_req(ctrl);
421 
422 	if (!pdev->gadget_driver)
423 		goto out;
424 
425 	if (pdev->gadget.state == USB_STATE_NOTATTACHED) {
426 		dev_err(pdev->dev, "ERR: Setup detected in unattached state\n");
427 		goto out;
428 	}
429 
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);
434 	}
435 
436 	/*
437 	 * Finishing previous SETUP transfer by removing request from
438 	 * list and informing upper layer
439 	 */
440 	if (!list_empty(&pdev->eps[0].pending_list)) {
441 		struct cdnsp_request	*req;
442 
443 		trace_cdnsp_ep0_request("Remove previous");
444 		req = next_request(&pdev->eps[0].pending_list);
445 		cdnsp_ep_dequeue(&pdev->eps[0], req);
446 	}
447 
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);
455 	}
456 
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);
461 
462 	if (ret == USB_GADGET_DELAYED_STATUS) {
463 		trace_cdnsp_ep0_status_stage("delayed");
464 		return;
465 	}
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);
471 }
472