1 // SPDX-License-Identifier: GPL-2.0+
4 * Gerry Hamel, geh@ti.com, Texas Instruments
7 * Bryan O'Donoghue, deckard@CodeHermit.ie
10 * linux/drivers/usbd/ep0.c
12 * Copyright (c) 2000, 2001, 2002 Lineo
13 * Copyright (c) 2001 Hewlett Packard
16 * Stuart Lynne <sl@lineo.com>,
17 * Tom Rushworth <tbr@lineo.com>,
18 * Bruce Balden <balden@lineo.com>
22 * This is the builtin ep0 control function. It implements all required functionality
23 * for responding to control requests (SETUP packets).
27 * Currently we do not pass any SETUP packets (or other) to the configured
28 * function driver. This may need to change.
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.
40 #include <usbdevice.h>
43 #define dbg_ep0(lvl,fmt,args...) serial_printf("[%s] %s:%d: "fmt"\n",__FILE__,__FUNCTION__,__LINE__,##args)
45 #define dbg_ep0(lvl,fmt,args...)
48 /* EP0 Configuration Set ********************************************************************* */
52 * ep0_get_status - fill in URB data with appropriate status
59 static int ep0_get_status (struct usb_device_instance *device,
60 struct urb *urb, int index, int requesttype)
64 urb->actual_length = 2;
65 cp = (char*)urb->buffer;
68 switch (requesttype) {
69 case USB_REQ_RECIPIENT_DEVICE:
70 cp[0] = USB_STATUS_SELFPOWERED;
72 case USB_REQ_RECIPIENT_INTERFACE:
74 case USB_REQ_RECIPIENT_ENDPOINT:
75 cp[0] = usbd_endpoint_halted (device, index);
77 case USB_REQ_RECIPIENT_OTHER:
78 urb->actual_length = 0;
82 dbg_ep0 (2, "%02x %02x", cp[0], cp[1]);
92 * Set a single byte value in the urb send buffer. Return non-zero to signal
95 static int ep0_get_one (struct usb_device_instance *device, struct urb *urb,
98 urb->actual_length = 1; /* XXX 2? */
99 ((char *) urb->buffer)[0] = result;
105 * @urb: pointer to urb
106 * @data: pointer to configuration data
107 * @length: length of data
109 * Copy configuration data to urb transfer buffer if there is room for it.
111 void copy_config (struct urb *urb, void *data, int max_length,
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); */
121 dbg_ep0 (1, "data is NULL");
126 if (length > max_length) {
127 dbg_ep0 (1, "length: %d >= max_length: %d", length,
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); */
135 /*urb->buffer_length */ max_buf - urb->actual_length) <= 0) {
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); */
141 if (length > available) {
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); */
147 memcpy (urb->buffer + urb->actual_length, data, length);
148 urb->actual_length += length;
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,
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.
168 static int ep0_get_descriptor (struct usb_device_instance *device,
169 struct urb *urb, int max, int descriptor_type,
172 int port = 0; /* XXX compound device */
174 /*dbg_ep0(3, "max: %x type: %x index: %x", max, descriptor_type, index); */
176 if (!urb || !urb->buffer || !urb->buffer_length
177 || (urb->buffer_length < 255)) {
178 dbg_ep0 (2, "invalid urb %p", urb);
183 urb->actual_length = 0;
185 dbg_ep0 (2, "%s", USBD_DEVICE_DESCRIPTORS (descriptor_type));
187 switch (descriptor_type) {
188 case USB_DESCRIPTOR_TYPE_DEVICE:
190 struct usb_device_descriptor *device_descriptor;
193 usbd_device_device_descriptor (device, port))) {
196 /* copy descriptor for this device */
197 copy_config (urb, device_descriptor,
198 sizeof (struct usb_device_descriptor),
201 /* correct the correct control endpoint 0 max packet size into the descriptor */
203 (struct usb_device_descriptor *) urb->buffer;
206 dbg_ep0(3, "copied device configuration, actual_length: 0x%x", urb->actual_length);
209 case USB_DESCRIPTOR_TYPE_CONFIGURATION:
211 struct usb_configuration_descriptor
212 *configuration_descriptor;
213 struct usb_device_descriptor *device_descriptor;
216 usbd_device_device_descriptor (device, port))) {
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,
228 (configuration_descriptor =
229 usbd_device_configuration_descriptor (device,
233 "usbd_device_configuration_descriptor failed: %d",
237 dbg_ep0(0, "attempt to copy %d bytes to urb\n",cpu_to_le16(configuration_descriptor->wTotalLength));
238 copy_config (urb, configuration_descriptor,
240 cpu_to_le16(configuration_descriptor->wTotalLength),
246 case USB_DESCRIPTOR_TYPE_STRING:
248 struct usb_string_descriptor *string_descriptor;
249 if (!(string_descriptor = usbd_get_string (index))) {
250 serial_printf("Invalid string index %d\n", index);
253 dbg_ep0(3, "string_descriptor: %p length %d", string_descriptor, string_descriptor->bLength);
254 copy_config (urb, string_descriptor, string_descriptor->bLength, max);
257 case USB_DESCRIPTOR_TYPE_INTERFACE:
258 serial_printf("USB_DESCRIPTOR_TYPE_INTERFACE - error not implemented\n");
260 case USB_DESCRIPTOR_TYPE_ENDPOINT:
261 serial_printf("USB_DESCRIPTOR_TYPE_ENDPOINT - error not implemented\n");
263 case USB_DESCRIPTOR_TYPE_HID:
265 serial_printf("USB_DESCRIPTOR_TYPE_HID - error not implemented\n");
266 return -1; /* unsupported at this time */
269 le16_to_cpu (urb->device_request.wIndex);
270 int bAlternateSetting = 0;
272 struct usb_class_descriptor *class_descriptor;
274 if (!(class_descriptor =
275 usbd_device_class_descriptor_index (device,
280 || class_descriptor->descriptor.hid.bDescriptorType != USB_DT_HID) {
281 dbg_ep0 (3, "[%d] interface is not HID",
285 /* copy descriptor for this class */
286 copy_config (urb, class_descriptor,
287 class_descriptor->descriptor.hid.bLength,
292 case USB_DESCRIPTOR_TYPE_REPORT:
294 serial_printf("USB_DESCRIPTOR_TYPE_REPORT - error not implemented\n");
295 return -1; /* unsupported at this time */
298 le16_to_cpu (urb->device_request.wIndex);
299 int bAlternateSetting = 0;
301 struct usb_class_report_descriptor *report_descriptor;
303 if (!(report_descriptor =
304 usbd_device_class_report_descriptor_index
305 (device, port, 0, bNumInterface,
306 bAlternateSetting, class))
307 || report_descriptor->bDescriptorType !=
309 dbg_ep0 (3, "[%d] descriptor is not REPORT",
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) {
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;
326 case USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER:
327 #if defined(CONFIG_USBD_HS)
329 struct usb_qualifier_descriptor *qualifier_descriptor =
330 device->qualifier_descriptor;
332 if (!qualifier_descriptor)
335 /* copy descriptor for this device */
336 copy_config(urb, qualifier_descriptor,
337 sizeof(struct usb_qualifier_descriptor),
341 dbg_ep0(3, "copied qualifier descriptor, actual_length: 0x%x",
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);
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);
369 * ep0_recv_setup - called to indicate URB has been received
370 * @urb: pointer to struct urb
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).
377 int ep0_recv_setup (struct urb *urb)
379 /*struct usb_device_request *request = urb->buffer; */
380 /*struct usb_device_instance *device = urb->device; */
382 struct usb_device_request *request;
383 struct usb_device_instance *device;
386 dbg_ep0 (0, "entering ep0_recv_setup()");
387 if (!urb || !urb->device) {
388 dbg_ep0 (3, "invalid URB %p", urb);
392 request = &urb->device_request;
393 device = urb->device;
395 dbg_ep0 (3, "urb: %p device: %p", urb, urb->device);
398 /*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));
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.
413 return device->cdc_recv_setup(request,urb);
415 dbg_ep0 (1, "non standard request: %x",
416 request->bmRequestType & USB_REQ_TYPE_MASK);
417 return -1; /* Stall here */
420 switch (device->device_state) {
424 /* It actually is important to allow requests in these states,
425 * Windows will request descriptors before assigning an
426 * address to the client.
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]); */
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:
447 "request %s not allowed in DEFAULT state: %s",
448 USBD_DEVICE_REQUESTS (request->bRequest),
449 usbd_device_states[device->device_state]);
452 case USB_REQ_SET_CONFIGURATION:
453 case USB_REQ_SET_ADDRESS:
454 case USB_REQ_GET_DESCRIPTOR:
455 case USB_REQ_GET_CONFIGURATION:
458 case STATE_ADDRESSED:
459 case STATE_CONFIGURED:
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]);
468 /* handle all requests that return data (direction bit set on bm RequestType) */
469 if ((request->bmRequestType & USB_REQ_DIRECTION_MASK)) {
471 dbg_ep0 (3, "Device-to-Host");
473 switch (request->bRequest) {
475 case USB_REQ_GET_STATUS:
476 return ep0_get_status (device, urb, request->wIndex,
477 request->bmRequestType &
478 USB_REQ_RECIPIENT_MASK);
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);
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);
491 case USB_REQ_GET_INTERFACE:
492 return ep0_get_one (device, urb, device->alternate);
494 case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
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:
506 /* handle the requests that do not return data */
510 /*dbg_ep0(3, "Host-to-Device"); */
511 switch (request->bRequest) {
513 case USB_REQ_CLEAR_FEATURE:
514 case USB_REQ_SET_FEATURE:
515 dbg_ep0 (0, "Host-to-Device");
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));
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!!! */
536 dbg_ep0 (1, "request %s bad wValue: %04x",
539 le16_to_cpu (request->wValue));
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]);
552 address = le16_to_cpu (request->wValue);
553 if ((address & 0x7f) != address) {
554 dbg_ep0 (1, "invalid address %04x %04x",
555 address, address & 0x7f);
558 device->address = address;
560 /*dbg_ep0(2, "address: %d %d %d", */
561 /* request->wValue, le16_to_cpu(request->wValue), device->address); */
565 case USB_REQ_SET_DESCRIPTOR: /* XXX should we support this? */
566 dbg_ep0 (0, "set descriptor: NOT SUPPORTED");
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;
573 /* reset interface and alternate settings */
574 device->interface = device->alternate = 0;
576 /*dbg_ep0(2, "set configuration: %d", device->configuration); */
577 /*serial_printf("DEVICE_CONFIGURED.. event?\n"); */
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");
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 (?) */