xref: /openbmc/u-boot/drivers/usb/gadget/ep0.c (revision b9553986)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003
4  * Gerry Hamel, geh@ti.com, Texas Instruments
5  *
6  * (C) Copyright 2006
7  * Bryan O'Donoghue, deckard@CodeHermit.ie
8  *
9  * Based on
10  * linux/drivers/usbd/ep0.c
11  *
12  * Copyright (c) 2000, 2001, 2002 Lineo
13  * Copyright (c) 2001 Hewlett Packard
14  *
15  * By:
16  *	Stuart Lynne <sl@lineo.com>,
17  *	Tom Rushworth <tbr@lineo.com>,
18  *	Bruce Balden <balden@lineo.com>
19  */
20 
21 /*
22  * This is the builtin ep0 control function. It implements all required functionality
23  * for responding to control requests (SETUP packets).
24  *
25  * XXX
26  *
27  * Currently we do not pass any SETUP packets (or other) to the configured
28  * function driver. This may need to change.
29  *
30  * XXX
31  *
32  * As alluded to above, a simple callback cdc_recv_setup has been implemented
33  * in the usb_device data structure to facilicate passing
34  * Common Device Class packets to a function driver.
35  *
36  * XXX
37  */
38 
39 #include <common.h>
40 #include <usbdevice.h>
41 
42 #if 0
43 #define dbg_ep0(lvl,fmt,args...) serial_printf("[%s] %s:%d: "fmt"\n",__FILE__,__FUNCTION__,__LINE__,##args)
44 #else
45 #define dbg_ep0(lvl,fmt,args...)
46 #endif
47 
48 /* EP0 Configuration Set ********************************************************************* */
49 
50 
51 /**
52  * ep0_get_status - fill in URB data with appropriate status
53  * @device:
54  * @urb:
55  * @index:
56  * @requesttype:
57  *
58  */
59 static int ep0_get_status (struct usb_device_instance *device,
60 			   struct urb *urb, int index, int requesttype)
61 {
62 	char *cp;
63 
64 	urb->actual_length = 2;
65 	cp = (char*)urb->buffer;
66 	cp[0] = cp[1] = 0;
67 
68 	switch (requesttype) {
69 	case USB_REQ_RECIPIENT_DEVICE:
70 		cp[0] = USB_STATUS_SELFPOWERED;
71 		break;
72 	case USB_REQ_RECIPIENT_INTERFACE:
73 		break;
74 	case USB_REQ_RECIPIENT_ENDPOINT:
75 		cp[0] = usbd_endpoint_halted (device, index);
76 		break;
77 	case USB_REQ_RECIPIENT_OTHER:
78 		urb->actual_length = 0;
79 	default:
80 		break;
81 	}
82 	dbg_ep0 (2, "%02x %02x", cp[0], cp[1]);
83 	return 0;
84 }
85 
86 /**
87  * ep0_get_one
88  * @device:
89  * @urb:
90  * @result:
91  *
92  * Set a single byte value in the urb send buffer. Return non-zero to signal
93  * a request error.
94  */
95 static int ep0_get_one (struct usb_device_instance *device, struct urb *urb,
96 			__u8 result)
97 {
98 	urb->actual_length = 1;	/* XXX 2? */
99 	((char *) urb->buffer)[0] = result;
100 	return 0;
101 }
102 
103 /**
104  * copy_config
105  * @urb: pointer to urb
106  * @data: pointer to configuration data
107  * @length: length of data
108  *
109  * Copy configuration data to urb transfer buffer if there is room for it.
110  */
111 void copy_config (struct urb *urb, void *data, int max_length,
112 			 int max_buf)
113 {
114 	int available;
115 	int length;
116 
117 	/*dbg_ep0(3, "-> actual: %d buf: %d max_buf: %d max_length: %d data: %p", */
118 	/*        urb->actual_length, urb->buffer_length, max_buf, max_length, data); */
119 
120 	if (!data) {
121 		dbg_ep0 (1, "data is NULL");
122 		return;
123 	}
124 	length = max_length;
125 
126 	if (length > max_length) {
127 		dbg_ep0 (1, "length: %d >= max_length: %d", length,
128 			 max_length);
129 		return;
130 	}
131 	/*dbg_ep0(1, "   actual: %d buf: %d max_buf: %d max_length: %d length: %d", */
132 	/*        urb->actual_length, urb->buffer_length, max_buf, max_length, length); */
133 
134 	if ((available =
135 	     /*urb->buffer_length */ max_buf - urb->actual_length) <= 0) {
136 		return;
137 	}
138 	/*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */
139 	/*        urb->actual_length, urb->buffer_length, max_buf, length, available); */
140 
141 	if (length > available) {
142 		length = available;
143 	}
144 	/*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */
145 	/*        urb->actual_length, urb->buffer_length, max_buf, length, available); */
146 
147 	memcpy (urb->buffer + urb->actual_length, data, length);
148 	urb->actual_length += length;
149 
150 	dbg_ep0 (3,
151 		 "copy_config: <- actual: %d buf: %d max_buf: %d max_length: %d available: %d",
152 		 urb->actual_length, urb->buffer_length, max_buf, max_length,
153 		 available);
154 }
155 
156 /**
157  * ep0_get_descriptor
158  * @device:
159  * @urb:
160  * @max:
161  * @descriptor_type:
162  * @index:
163  *
164  * Called by ep0_rx_process for a get descriptor device command. Determine what
165  * descriptor is being requested, copy to send buffer. Return zero if ok to send,
166  * return non-zero to signal a request error.
167  */
168 static int ep0_get_descriptor (struct usb_device_instance *device,
169 			       struct urb *urb, int max, int descriptor_type,
170 			       int index)
171 {
172 	int port = 0;		/* XXX compound device */
173 
174 	/*dbg_ep0(3, "max: %x type: %x index: %x", max, descriptor_type, index); */
175 
176 	if (!urb || !urb->buffer || !urb->buffer_length
177 	    || (urb->buffer_length < 255)) {
178 		dbg_ep0 (2, "invalid urb %p", urb);
179 		return -1L;
180 	}
181 
182 	/* setup tx urb */
183 	urb->actual_length = 0;
184 
185 	dbg_ep0 (2, "%s", USBD_DEVICE_DESCRIPTORS (descriptor_type));
186 
187 	switch (descriptor_type) {
188 	case USB_DESCRIPTOR_TYPE_DEVICE:
189 		{
190 			struct usb_device_descriptor *device_descriptor;
191 			if (!
192 			    (device_descriptor =
193 			     usbd_device_device_descriptor (device, port))) {
194 				return -1;
195 			}
196 			/* copy descriptor for this device */
197 			copy_config (urb, device_descriptor,
198 				     sizeof (struct usb_device_descriptor),
199 				     max);
200 
201 			/* correct the correct control endpoint 0 max packet size into the descriptor */
202 			device_descriptor =
203 				(struct usb_device_descriptor *) urb->buffer;
204 
205 		}
206 		dbg_ep0(3, "copied device configuration, actual_length: 0x%x", urb->actual_length);
207 		break;
208 
209 	case USB_DESCRIPTOR_TYPE_CONFIGURATION:
210 		{
211 			struct usb_configuration_descriptor
212 				*configuration_descriptor;
213 			struct usb_device_descriptor *device_descriptor;
214 			if (!
215 			    (device_descriptor =
216 			     usbd_device_device_descriptor (device, port))) {
217 				return -1;
218 			}
219 			/*dbg_ep0(2, "%d %d", index, device_descriptor->bNumConfigurations); */
220 			if (index >= device_descriptor->bNumConfigurations) {
221 				dbg_ep0 (0, "index too large: %d >= %d", index,
222 					 device_descriptor->
223 					 bNumConfigurations);
224 				return -1;
225 			}
226 
227 			if (!
228 			    (configuration_descriptor =
229 			     usbd_device_configuration_descriptor (device,
230 								   port,
231 								   index))) {
232 				dbg_ep0 (0,
233 					 "usbd_device_configuration_descriptor failed: %d",
234 					 index);
235 				return -1;
236 			}
237 			dbg_ep0(0, "attempt to copy %d bytes to urb\n",cpu_to_le16(configuration_descriptor->wTotalLength));
238 			copy_config (urb, configuration_descriptor,
239 
240 					cpu_to_le16(configuration_descriptor->wTotalLength),
241 				     max);
242 		}
243 
244 		break;
245 
246 	case USB_DESCRIPTOR_TYPE_STRING:
247 		{
248 			struct usb_string_descriptor *string_descriptor;
249 			if (!(string_descriptor = usbd_get_string (index))) {
250 				serial_printf("Invalid string index %d\n", index);
251 				return -1;
252 			}
253 			dbg_ep0(3, "string_descriptor: %p length %d", string_descriptor, string_descriptor->bLength);
254 			copy_config (urb, string_descriptor, string_descriptor->bLength, max);
255 		}
256 		break;
257 	case USB_DESCRIPTOR_TYPE_INTERFACE:
258 	serial_printf("USB_DESCRIPTOR_TYPE_INTERFACE - error not implemented\n");
259 		return -1;
260 	case USB_DESCRIPTOR_TYPE_ENDPOINT:
261 		serial_printf("USB_DESCRIPTOR_TYPE_ENDPOINT - error not implemented\n");
262 		return -1;
263 	case USB_DESCRIPTOR_TYPE_HID:
264 		{
265 			serial_printf("USB_DESCRIPTOR_TYPE_HID - error not implemented\n");
266 			return -1;	/* unsupported at this time */
267 #if 0
268 			int bNumInterface =
269 				le16_to_cpu (urb->device_request.wIndex);
270 			int bAlternateSetting = 0;
271 			int class = 0;
272 			struct usb_class_descriptor *class_descriptor;
273 
274 			if (!(class_descriptor =
275 			      usbd_device_class_descriptor_index (device,
276 								  port, 0,
277 								  bNumInterface,
278 								  bAlternateSetting,
279 								  class))
280 			    || class_descriptor->descriptor.hid.bDescriptorType != USB_DT_HID) {
281 				dbg_ep0 (3, "[%d] interface is not HID",
282 					 bNumInterface);
283 				return -1;
284 			}
285 			/* copy descriptor for this class */
286 			copy_config (urb, class_descriptor,
287 				     class_descriptor->descriptor.hid.bLength,
288 				     max);
289 #endif
290 		}
291 		break;
292 	case USB_DESCRIPTOR_TYPE_REPORT:
293 		{
294 			serial_printf("USB_DESCRIPTOR_TYPE_REPORT - error not implemented\n");
295 			return -1;	/* unsupported at this time */
296 #if 0
297 			int bNumInterface =
298 				le16_to_cpu (urb->device_request.wIndex);
299 			int bAlternateSetting = 0;
300 			int class = 0;
301 			struct usb_class_report_descriptor *report_descriptor;
302 
303 			if (!(report_descriptor =
304 			      usbd_device_class_report_descriptor_index
305 			      (device, port, 0, bNumInterface,
306 			       bAlternateSetting, class))
307 			    || report_descriptor->bDescriptorType !=
308 			    USB_DT_REPORT) {
309 				dbg_ep0 (3, "[%d] descriptor is not REPORT",
310 					 bNumInterface);
311 				return -1;
312 			}
313 			/* copy report descriptor for this class */
314 			/*copy_config(urb, &report_descriptor->bData[0], report_descriptor->wLength, max); */
315 			if (max - urb->actual_length > 0) {
316 				int length =
317 					min(report_descriptor->wLength,
318 					     max - urb->actual_length);
319 				memcpy (urb->buffer + urb->actual_length,
320 					&report_descriptor->bData[0], length);
321 				urb->actual_length += length;
322 			}
323 #endif
324 		}
325 		break;
326 	case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
327 #if defined(CONFIG_USBD_HS)
328 		{
329 			struct usb_qualifier_descriptor *qualifier_descriptor =
330 				device->qualifier_descriptor;
331 
332 			if (!qualifier_descriptor)
333 				return -1;
334 
335 			/* copy descriptor for this device */
336 			copy_config(urb, qualifier_descriptor,
337 					sizeof(struct usb_qualifier_descriptor),
338 					max);
339 
340 		}
341 		dbg_ep0(3, "copied qualifier descriptor, actual_length: 0x%x",
342 				urb->actual_length);
343 #else
344 		return -1;
345 #endif
346 		break;
347 
348 	default:
349 		return -1;
350 	}
351 
352 
353 	dbg_ep0 (1, "urb: buffer: %p buffer_length: %2d actual_length: %2d tx_packetSize: %2d",
354 		 urb->buffer, urb->buffer_length, urb->actual_length,
355 		 device->bus->endpoint_array[0].tx_packetSize);
356 /*
357     if ((urb->actual_length < max) && !(urb->actual_length % device->bus->endpoint_array[0].tx_packetSize)) {
358 	dbg_ep0(0, "adding null byte");
359 	urb->buffer[urb->actual_length++] = 0;
360 	dbg_ep0(0, "urb: buffer_length: %2d actual_length: %2d packet size: %2d",
361 		urb->buffer_length, urb->actual_length device->bus->endpoint_array[0].tx_packetSize);
362     }
363 */
364 	return 0;
365 
366 }
367 
368 /**
369  * ep0_recv_setup - called to indicate URB has been received
370  * @urb: pointer to struct urb
371  *
372  * Check if this is a setup packet, process the device request, put results
373  * back into the urb and return zero or non-zero to indicate success (DATA)
374  * or failure (STALL).
375  *
376  */
377 int ep0_recv_setup (struct urb *urb)
378 {
379 	/*struct usb_device_request *request = urb->buffer; */
380 	/*struct usb_device_instance *device = urb->device; */
381 
382 	struct usb_device_request *request;
383 	struct usb_device_instance *device;
384 	int address;
385 
386 	dbg_ep0 (0, "entering ep0_recv_setup()");
387 	if (!urb || !urb->device) {
388 		dbg_ep0 (3, "invalid URB %p", urb);
389 		return -1;
390 	}
391 
392 	request = &urb->device_request;
393 	device = urb->device;
394 
395 	dbg_ep0 (3, "urb: %p device: %p", urb, urb->device);
396 
397 
398 	/*dbg_ep0(2, "-       -       -       -       -       -       -       -       -       -"); */
399 
400 	dbg_ep0 (2,
401 		 "bmRequestType:%02x bRequest:%02x wValue:%04x wIndex:%04x wLength:%04x %s",
402 		 request->bmRequestType, request->bRequest,
403 		 le16_to_cpu (request->wValue), le16_to_cpu (request->wIndex),
404 		 le16_to_cpu (request->wLength),
405 		 USBD_DEVICE_REQUESTS (request->bRequest));
406 
407 	/* handle USB Standard Request (c.f. USB Spec table 9-2) */
408 	if ((request->bmRequestType & USB_REQ_TYPE_MASK) != 0) {
409 		if(device->device_state <= STATE_CONFIGURED){
410 			/*	Attempt to handle a CDC specific request if we are
411 			 *	in the configured state.
412 			 */
413 			return device->cdc_recv_setup(request,urb);
414 		}
415 		dbg_ep0 (1, "non standard request: %x",
416 			 request->bmRequestType & USB_REQ_TYPE_MASK);
417 		return -1;	/* Stall here */
418 	}
419 
420 	switch (device->device_state) {
421 	case STATE_CREATED:
422 	case STATE_ATTACHED:
423 	case STATE_POWERED:
424 		/* It actually is important to allow requests in these states,
425 		 * Windows will request descriptors before assigning an
426 		 * address to the client.
427 		 */
428 
429 		/*dbg_ep0 (1, "request %s not allowed in this state: %s", */
430 		/*                USBD_DEVICE_REQUESTS(request->bRequest), */
431 		/*                usbd_device_states[device->device_state]); */
432 		/*return -1; */
433 		break;
434 
435 	case STATE_INIT:
436 	case STATE_DEFAULT:
437 		switch (request->bRequest) {
438 		case USB_REQ_GET_STATUS:
439 		case USB_REQ_GET_INTERFACE:
440 		case USB_REQ_SYNCH_FRAME:	/* XXX should never see this (?) */
441 		case USB_REQ_CLEAR_FEATURE:
442 		case USB_REQ_SET_FEATURE:
443 		case USB_REQ_SET_DESCRIPTOR:
444 			/* case USB_REQ_SET_CONFIGURATION: */
445 		case USB_REQ_SET_INTERFACE:
446 			dbg_ep0 (1,
447 				 "request %s not allowed in DEFAULT state: %s",
448 				 USBD_DEVICE_REQUESTS (request->bRequest),
449 				 usbd_device_states[device->device_state]);
450 			return -1;
451 
452 		case USB_REQ_SET_CONFIGURATION:
453 		case USB_REQ_SET_ADDRESS:
454 		case USB_REQ_GET_DESCRIPTOR:
455 		case USB_REQ_GET_CONFIGURATION:
456 			break;
457 		}
458 	case STATE_ADDRESSED:
459 	case STATE_CONFIGURED:
460 		break;
461 	case STATE_UNKNOWN:
462 		dbg_ep0 (1, "request %s not allowed in UNKNOWN state: %s",
463 			 USBD_DEVICE_REQUESTS (request->bRequest),
464 			 usbd_device_states[device->device_state]);
465 		return -1;
466 	}
467 
468 	/* handle all requests that return data (direction bit set on bm RequestType) */
469 	if ((request->bmRequestType & USB_REQ_DIRECTION_MASK)) {
470 
471 		dbg_ep0 (3, "Device-to-Host");
472 
473 		switch (request->bRequest) {
474 
475 		case USB_REQ_GET_STATUS:
476 			return ep0_get_status (device, urb, request->wIndex,
477 					       request->bmRequestType &
478 					       USB_REQ_RECIPIENT_MASK);
479 
480 		case USB_REQ_GET_DESCRIPTOR:
481 			return ep0_get_descriptor (device, urb,
482 						   le16_to_cpu (request->wLength),
483 						   le16_to_cpu (request->wValue) >> 8,
484 						   le16_to_cpu (request->wValue) & 0xff);
485 
486 		case USB_REQ_GET_CONFIGURATION:
487 			serial_printf("get config %d\n", device->configuration);
488 			return ep0_get_one (device, urb,
489 					    device->configuration);
490 
491 		case USB_REQ_GET_INTERFACE:
492 			return ep0_get_one (device, urb, device->alternate);
493 
494 		case USB_REQ_SYNCH_FRAME:	/* XXX should never see this (?) */
495 			return -1;
496 
497 		case USB_REQ_CLEAR_FEATURE:
498 		case USB_REQ_SET_FEATURE:
499 		case USB_REQ_SET_ADDRESS:
500 		case USB_REQ_SET_DESCRIPTOR:
501 		case USB_REQ_SET_CONFIGURATION:
502 		case USB_REQ_SET_INTERFACE:
503 			return -1;
504 		}
505 	}
506 	/* handle the requests that do not return data */
507 	else {
508 
509 
510 		/*dbg_ep0(3, "Host-to-Device"); */
511 		switch (request->bRequest) {
512 
513 		case USB_REQ_CLEAR_FEATURE:
514 		case USB_REQ_SET_FEATURE:
515 			dbg_ep0 (0, "Host-to-Device");
516 			switch (request->
517 				bmRequestType & USB_REQ_RECIPIENT_MASK) {
518 			case USB_REQ_RECIPIENT_DEVICE:
519 				/* XXX DEVICE_REMOTE_WAKEUP or TEST_MODE would be added here */
520 				/* XXX fall through for now as we do not support either */
521 			case USB_REQ_RECIPIENT_INTERFACE:
522 			case USB_REQ_RECIPIENT_OTHER:
523 				dbg_ep0 (0, "request %s not",
524 					 USBD_DEVICE_REQUESTS (request->bRequest));
525 			default:
526 				return -1;
527 
528 			case USB_REQ_RECIPIENT_ENDPOINT:
529 				dbg_ep0 (0, "ENDPOINT: %x", le16_to_cpu (request->wValue));
530 				if (le16_to_cpu (request->wValue) == USB_ENDPOINT_HALT) {
531 					/*return usbd_device_feature (device, le16_to_cpu (request->wIndex), */
532 					/*                    request->bRequest == USB_REQ_SET_FEATURE); */
533 					/* NEED TO IMPLEMENT THIS!!! */
534 					return -1;
535 				} else {
536 					dbg_ep0 (1, "request %s bad wValue: %04x",
537 						 USBD_DEVICE_REQUESTS
538 						 (request->bRequest),
539 						 le16_to_cpu (request->wValue));
540 					return -1;
541 				}
542 			}
543 
544 		case USB_REQ_SET_ADDRESS:
545 			/* check if this is a re-address, reset first if it is (this shouldn't be possible) */
546 			if (device->device_state != STATE_DEFAULT) {
547 				dbg_ep0 (1, "set_address: %02x state: %s",
548 					 le16_to_cpu (request->wValue),
549 					 usbd_device_states[device->device_state]);
550 				return -1;
551 			}
552 			address = le16_to_cpu (request->wValue);
553 			if ((address & 0x7f) != address) {
554 				dbg_ep0 (1, "invalid address %04x %04x",
555 					 address, address & 0x7f);
556 				return -1;
557 			}
558 			device->address = address;
559 
560 			/*dbg_ep0(2, "address: %d %d %d", */
561 			/*        request->wValue, le16_to_cpu(request->wValue), device->address); */
562 
563 			return 0;
564 
565 		case USB_REQ_SET_DESCRIPTOR:	/* XXX should we support this? */
566 			dbg_ep0 (0, "set descriptor: NOT SUPPORTED");
567 			return -1;
568 
569 		case USB_REQ_SET_CONFIGURATION:
570 			/* c.f. 9.4.7 - the top half of wValue is reserved */
571 			device->configuration = le16_to_cpu(request->wValue) & 0xff;
572 
573 			/* reset interface and alternate settings */
574 			device->interface = device->alternate = 0;
575 
576 			/*dbg_ep0(2, "set configuration: %d", device->configuration); */
577 			/*serial_printf("DEVICE_CONFIGURED.. event?\n"); */
578 			return 0;
579 
580 		case USB_REQ_SET_INTERFACE:
581 			device->interface = le16_to_cpu (request->wIndex);
582 			device->alternate = le16_to_cpu (request->wValue);
583 			/*dbg_ep0(2, "set interface: %d alternate: %d", device->interface, device->alternate); */
584 			serial_printf ("DEVICE_SET_INTERFACE.. event?\n");
585 			return 0;
586 
587 		case USB_REQ_GET_STATUS:
588 		case USB_REQ_GET_DESCRIPTOR:
589 		case USB_REQ_GET_CONFIGURATION:
590 		case USB_REQ_GET_INTERFACE:
591 		case USB_REQ_SYNCH_FRAME:	/* XXX should never see this (?) */
592 			return -1;
593 		}
594 	}
595 	return -1;
596 }
597