3 * Gerry Hamel, geh@ti.com, Texas Instruments
6 * linux/drivers/usbd/ep0.c
8 * Copyright (c) 2000, 2001, 2002 Lineo
9 * Copyright (c) 2001 Hewlett Packard
12 * Stuart Lynne <sl@lineo.com>,
13 * Tom Rushworth <tbr@lineo.com>,
14 * Bruce Balden <balden@lineo.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 * This is the builtin ep0 control function. It implements all required functionality
34 * for responding to control requests (SETUP packets).
38 * Currently we do not pass any SETUP packets (or other) to the configured
39 * function driver. This may need to change.
46 #if defined(CONFIG_OMAP1510) && defined(CONFIG_USB_DEVICE)
50 #define dbg_ep0(lvl,fmt,args...) serial_printf("[%s] %s:%d: "fmt"\n",__FILE__,__FUNCTION__,__LINE__,##args)
52 #define dbg_ep0(lvl,fmt,args...)
55 /* EP0 Configuration Set ********************************************************************* */
59 * ep0_get_status - fill in URB data with appropriate status
66 static int ep0_get_status (struct usb_device_instance *device,
67 struct urb *urb, int index, int requesttype)
71 urb->actual_length = 2;
75 switch (requesttype) {
76 case USB_REQ_RECIPIENT_DEVICE:
77 cp[0] = USB_STATUS_SELFPOWERED;
79 case USB_REQ_RECIPIENT_INTERFACE:
81 case USB_REQ_RECIPIENT_ENDPOINT:
82 cp[0] = usbd_endpoint_halted (device, index);
84 case USB_REQ_RECIPIENT_OTHER:
85 urb->actual_length = 0;
89 dbg_ep0 (2, "%02x %02x", cp[0], cp[1]);
99 * Set a single byte value in the urb send buffer. Return non-zero to signal
102 static int ep0_get_one (struct usb_device_instance *device, struct urb *urb,
105 urb->actual_length = 1; /* XXX 2? */
106 ((char *) urb->buffer)[0] = result;
112 * @urb: pointer to urb
113 * @data: pointer to configuration data
114 * @length: length of data
116 * Copy configuration data to urb transfer buffer if there is room for it.
118 static void copy_config (struct urb *urb, void *data, int max_length,
124 /*dbg_ep0(3, "-> actual: %d buf: %d max_buf: %d max_length: %d data: %p", */
125 /* urb->actual_length, urb->buffer_length, max_buf, max_length, data); */
128 dbg_ep0 (1, "data is NULL");
131 if (!(length = *(unsigned char *) data)) {
132 dbg_ep0 (1, "length is zero");
136 if (length > max_length) {
137 dbg_ep0 (1, "length: %d >= max_length: %d", length,
141 /*dbg_ep0(1, " actual: %d buf: %d max_buf: %d max_length: %d length: %d", */
142 /* urb->actual_length, urb->buffer_length, max_buf, max_length, length); */
145 /*urb->buffer_length */ max_buf - urb->actual_length) <= 0) {
148 /*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */
149 /* urb->actual_length, urb->buffer_length, max_buf, length, available); */
151 if (length > available) {
154 /*dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d", */
155 /* urb->actual_length, urb->buffer_length, max_buf, length, available); */
157 memcpy (urb->buffer + urb->actual_length, data, length);
158 urb->actual_length += length;
161 "copy_config: <- actual: %d buf: %d max_buf: %d max_length: %d available: %d",
162 urb->actual_length, urb->buffer_length, max_buf, max_length,
174 * Called by ep0_rx_process for a get descriptor device command. Determine what
175 * descriptor is being requested, copy to send buffer. Return zero if ok to send,
176 * return non-zero to signal a request error.
178 static int ep0_get_descriptor (struct usb_device_instance *device,
179 struct urb *urb, int max, int descriptor_type,
182 int port = 0; /* XXX compound device */
185 /*dbg_ep0(3, "max: %x type: %x index: %x", max, descriptor_type, index); */
187 if (!urb || !urb->buffer || !urb->buffer_length
188 || (urb->buffer_length < 255)) {
189 dbg_ep0 (2, "invalid urb %p", urb);
194 urb->actual_length = 0;
197 dbg_ep0 (2, "%s", USBD_DEVICE_DESCRIPTORS (descriptor_type));
199 switch (descriptor_type) {
200 case USB_DESCRIPTOR_TYPE_DEVICE:
202 struct usb_device_descriptor *device_descriptor;
206 usbd_device_device_descriptor (device, port))) {
209 /* copy descriptor for this device */
210 copy_config (urb, device_descriptor,
211 sizeof (struct usb_device_descriptor),
214 /* correct the correct control endpoint 0 max packet size into the descriptor */
216 (struct usb_device_descriptor *) urb->buffer;
217 device_descriptor->bMaxPacketSize0 =
218 urb->device->bus->maxpacketsize;
221 /*dbg_ep0(3, "copied device configuration, actual_length: %x", urb->actual_length); */
224 case USB_DESCRIPTOR_TYPE_CONFIGURATION:
227 struct usb_configuration_descriptor
228 *configuration_descriptor;
229 struct usb_device_descriptor *device_descriptor;
233 usbd_device_device_descriptor (device, port))) {
236 /*dbg_ep0(2, "%d %d", index, device_descriptor->bNumConfigurations); */
237 if (index > device_descriptor->bNumConfigurations) {
238 dbg_ep0 (0, "index too large: %d > %d", index,
245 (configuration_descriptor =
246 usbd_device_configuration_descriptor (device,
250 "usbd_device_configuration_descriptor failed: %d",
254 copy_config (urb, configuration_descriptor,
256 usb_configuration_descriptor),
260 /* iterate across interfaces for specified configuration */
261 dbg_ep0 (0, "bNumInterfaces: %d",
262 configuration_descriptor->bNumInterfaces);
263 for (bNumInterface = 0;
265 configuration_descriptor->bNumInterfaces;
268 int bAlternateSetting;
269 struct usb_interface_instance
272 dbg_ep0 (3, "[%d] bNumInterfaces: %d",
274 configuration_descriptor->bNumInterfaces);
276 if (! (interface_instance = usbd_device_interface_instance (device,
277 port, index, bNumInterface)))
279 dbg_ep0 (3, "[%d] interface_instance NULL",
283 /* iterate across interface alternates */
284 for (bAlternateSetting = 0;
285 bAlternateSetting < interface_instance->alternates;
286 bAlternateSetting++) {
289 struct usb_interface_descriptor *interface_descriptor;
291 struct usb_alternate_instance *alternate_instance;
293 dbg_ep0 (3, "[%d:%d] alternates: %d",
296 interface_instance->alternates);
298 if (! (alternate_instance = usbd_device_alternate_instance (device, port, index, bNumInterface, bAlternateSetting))) {
299 dbg_ep0 (3, "[%d] alternate_instance NULL",
303 /* copy descriptor for this interface */
304 copy_config (urb, alternate_instance->interface_descriptor,
305 sizeof (struct usb_interface_descriptor),
308 /*dbg_ep0(3, "[%d:%d] classes: %d endpoints: %d", bNumInterface, bAlternateSetting, */
309 /* alternate_instance->classes, alternate_instance->endpoints); */
311 /* iterate across classes for this alternate interface */
314 class < alternate_instance->classes;
316 struct usb_class_descriptor *class_descriptor;
317 /*dbg_ep0(3, "[%d:%d:%d] classes: %d", bNumInterface, bAlternateSetting, */
318 /* class, alternate_instance->classes); */
319 if (!(class_descriptor = usbd_device_class_descriptor_index (device, port, index, bNumInterface, bAlternateSetting, class))) {
320 dbg_ep0 (3, "[%d] class NULL",
324 /* copy descriptor for this class */
325 copy_config (urb, class_descriptor,
326 sizeof (struct usb_class_descriptor),
331 /* iterate across endpoints for this alternate interface */
332 interface_descriptor = alternate_instance->interface_descriptor;
333 for (bNumEndpoint = 0;
334 bNumEndpoint < alternate_instance->endpoints;
336 struct usb_endpoint_descriptor *endpoint_descriptor;
337 dbg_ep0 (3, "[%d:%d:%d] endpoint: %d",
341 interface_descriptor->
343 if (!(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, index, bNumInterface, bAlternateSetting, bNumEndpoint))) {
344 dbg_ep0 (3, "[%d] endpoint NULL",
348 /* copy descriptor for this endpoint */
349 copy_config (urb, endpoint_descriptor,
350 sizeof (struct usb_endpoint_descriptor),
355 dbg_ep0 (3, "lengths: %d %d",
356 le16_to_cpu (configuration_descriptor->wTotalLength),
361 case USB_DESCRIPTOR_TYPE_STRING:
363 struct usb_string_descriptor *string_descriptor;
365 if (!(string_descriptor = usbd_get_string (index))) {
368 /*dbg_ep0(3, "string_descriptor: %p", string_descriptor); */
369 copy_config (urb, string_descriptor, string_descriptor->bLength, max);
372 case USB_DESCRIPTOR_TYPE_INTERFACE:
374 case USB_DESCRIPTOR_TYPE_ENDPOINT:
376 case USB_DESCRIPTOR_TYPE_HID:
378 return -1; /* unsupported at this time */
381 le16_to_cpu (urb->device_request.wIndex);
382 int bAlternateSetting = 0;
384 struct usb_class_descriptor *class_descriptor;
386 if (!(class_descriptor =
387 usbd_device_class_descriptor_index (device,
392 || class_descriptor->descriptor.hid.bDescriptorType != USB_DT_HID) {
393 dbg_ep0 (3, "[%d] interface is not HID",
397 /* copy descriptor for this class */
398 copy_config (urb, class_descriptor,
399 class_descriptor->descriptor.hid.bLength,
404 case USB_DESCRIPTOR_TYPE_REPORT:
406 return -1; /* unsupported at this time */
409 le16_to_cpu (urb->device_request.wIndex);
410 int bAlternateSetting = 0;
412 struct usb_class_report_descriptor *report_descriptor;
414 if (!(report_descriptor =
415 usbd_device_class_report_descriptor_index
416 (device, port, 0, bNumInterface,
417 bAlternateSetting, class))
418 || report_descriptor->bDescriptorType !=
420 dbg_ep0 (3, "[%d] descriptor is not REPORT",
424 /* copy report descriptor for this class */
425 /*copy_config(urb, &report_descriptor->bData[0], report_descriptor->wLength, max); */
426 if (max - urb->actual_length > 0) {
428 MIN (report_descriptor->wLength,
429 max - urb->actual_length);
430 memcpy (urb->buffer + urb->actual_length,
431 &report_descriptor->bData[0], length);
432 urb->actual_length += length;
442 dbg_ep0 (1, "urb: buffer: %p buffer_length: %2d actual_length: %2d packet size: %2d",
443 urb->buffer, urb->buffer_length, urb->actual_length,
444 device->bus->endpoint_array[0].tx_packetSize);
446 if ((urb->actual_length < max) && !(urb->actual_length % device->bus->endpoint_array[0].tx_packetSize)) {
447 dbg_ep0(0, "adding null byte");
448 urb->buffer[urb->actual_length++] = 0;
449 dbg_ep0(0, "urb: buffer_length: %2d actual_length: %2d packet size: %2d",
450 urb->buffer_length, urb->actual_length device->bus->endpoint_array[0].tx_packetSize);
458 * ep0_recv_setup - called to indicate URB has been received
459 * @urb: pointer to struct urb
461 * Check if this is a setup packet, process the device request, put results
462 * back into the urb and return zero or non-zero to indicate success (DATA)
463 * or failure (STALL).
466 int ep0_recv_setup (struct urb *urb)
468 /*struct usb_device_request *request = urb->buffer; */
469 /*struct usb_device_instance *device = urb->device; */
471 struct usb_device_request *request;
472 struct usb_device_instance *device;
475 dbg_ep0 (0, "entering ep0_recv_setup()");
476 if (!urb || !urb->device) {
477 dbg_ep0 (3, "invalid URB %p", urb);
481 request = &urb->device_request;
482 device = urb->device;
484 dbg_ep0 (3, "urb: %p device: %p", urb, urb->device);
487 /*dbg_ep0(2, "- - - - - - - - - -"); */
490 "bmRequestType:%02x bRequest:%02x wValue:%04x wIndex:%04x wLength:%04x %s",
491 request->bmRequestType, request->bRequest,
492 le16_to_cpu (request->wValue), le16_to_cpu (request->wIndex),
493 le16_to_cpu (request->wLength),
494 USBD_DEVICE_REQUESTS (request->bRequest));
496 /* handle USB Standard Request (c.f. USB Spec table 9-2) */
497 if ((request->bmRequestType & USB_REQ_TYPE_MASK) != 0) {
498 dbg_ep0 (1, "non standard request: %x",
499 request->bmRequestType & USB_REQ_TYPE_MASK);
500 return -1; /* Stall here */
503 switch (device->device_state) {
507 /* It actually is important to allow requests in these states,
508 * Windows will request descriptors before assigning an
509 * address to the client.
512 /*dbg_ep0 (1, "request %s not allowed in this state: %s", */
513 /* USBD_DEVICE_REQUESTS(request->bRequest), */
514 /* usbd_device_states[device->device_state]); */
520 switch (request->bRequest) {
521 case USB_REQ_GET_STATUS:
522 case USB_REQ_GET_INTERFACE:
523 case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
524 case USB_REQ_CLEAR_FEATURE:
525 case USB_REQ_SET_FEATURE:
526 case USB_REQ_SET_DESCRIPTOR:
527 /* case USB_REQ_SET_CONFIGURATION: */
528 case USB_REQ_SET_INTERFACE:
530 "request %s not allowed in DEFAULT state: %s",
531 USBD_DEVICE_REQUESTS (request->bRequest),
532 usbd_device_states[device->device_state]);
535 case USB_REQ_SET_CONFIGURATION:
536 case USB_REQ_SET_ADDRESS:
537 case USB_REQ_GET_DESCRIPTOR:
538 case USB_REQ_GET_CONFIGURATION:
541 case STATE_ADDRESSED:
542 case STATE_CONFIGURED:
545 dbg_ep0 (1, "request %s not allowed in UNKNOWN state: %s",
546 USBD_DEVICE_REQUESTS (request->bRequest),
547 usbd_device_states[device->device_state]);
551 /* handle all requests that return data (direction bit set on bm RequestType) */
552 if ((request->bmRequestType & USB_REQ_DIRECTION_MASK)) {
554 dbg_ep0 (3, "Device-to-Host");
556 switch (request->bRequest) {
558 case USB_REQ_GET_STATUS:
559 return ep0_get_status (device, urb, request->wIndex,
560 request->bmRequestType &
561 USB_REQ_RECIPIENT_MASK);
563 case USB_REQ_GET_DESCRIPTOR:
564 return ep0_get_descriptor (device, urb,
565 le16_to_cpu (request->wLength),
566 le16_to_cpu (request->wValue) >> 8,
567 le16_to_cpu (request->wValue) & 0xff);
569 case USB_REQ_GET_CONFIGURATION:
570 return ep0_get_one (device, urb,
571 device->configuration);
573 case USB_REQ_GET_INTERFACE:
574 return ep0_get_one (device, urb, device->alternate);
576 case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */
579 case USB_REQ_CLEAR_FEATURE:
580 case USB_REQ_SET_FEATURE:
581 case USB_REQ_SET_ADDRESS:
582 case USB_REQ_SET_DESCRIPTOR:
583 case USB_REQ_SET_CONFIGURATION:
584 case USB_REQ_SET_INTERFACE:
588 /* handle the requests that do not return data */
592 /*dbg_ep0(3, "Host-to-Device"); */
593 switch (request->bRequest) {
595 case USB_REQ_CLEAR_FEATURE:
596 case USB_REQ_SET_FEATURE:
597 dbg_ep0 (0, "Host-to-Device");
599 bmRequestType & USB_REQ_RECIPIENT_MASK) {
600 case USB_REQ_RECIPIENT_DEVICE:
601 /* XXX DEVICE_REMOTE_WAKEUP or TEST_MODE would be added here */
602 /* XXX fall through for now as we do not support either */
603 case USB_REQ_RECIPIENT_INTERFACE:
604 case USB_REQ_RECIPIENT_OTHER:
605 dbg_ep0 (0, "request %s not",
606 USBD_DEVICE_REQUESTS (request->bRequest));
610 case USB_REQ_RECIPIENT_ENDPOINT:
611 dbg_ep0 (0, "ENDPOINT: %x", le16_to_cpu (request->wValue));
612 if (le16_to_cpu (request->wValue) == USB_ENDPOINT_HALT) {
613 /*return usbd_device_feature (device, le16_to_cpu (request->wIndex), */
614 /* request->bRequest == USB_REQ_SET_FEATURE); */
615 /* NEED TO IMPLEMENT THIS!!! */
618 dbg_ep0 (1, "request %s bad wValue: %04x",
621 le16_to_cpu (request->wValue));
626 case USB_REQ_SET_ADDRESS:
627 /* check if this is a re-address, reset first if it is (this shouldn't be possible) */
628 if (device->device_state != STATE_DEFAULT) {
629 dbg_ep0 (1, "set_address: %02x state: %s",
630 le16_to_cpu (request->wValue),
631 usbd_device_states[device->device_state]);
634 address = le16_to_cpu (request->wValue);
635 if ((address & 0x7f) != address) {
636 dbg_ep0 (1, "invalid address %04x %04x",
637 address, address & 0x7f);
640 device->address = address;
642 /*dbg_ep0(2, "address: %d %d %d", */
643 /* request->wValue, le16_to_cpu(request->wValue), device->address); */
645 serial_printf ("DEVICE_ADDRESS_ASSIGNED.. event?\n");
648 case USB_REQ_SET_DESCRIPTOR: /* XXX should we support this? */
649 dbg_ep0 (0, "set descriptor: NOT SUPPORTED");
652 case USB_REQ_SET_CONFIGURATION:
653 /* c.f. 9.4.7 - the top half of wValue is reserved */
655 if ((device->configuration =
656 le16_to_cpu (request->wValue) & 0x7f) != 0) {
657 /* c.f. 9.4.7 - zero is the default or addressed state, in our case this */
658 /* is the same is configuration zero */
659 device->configuration = 0; /* TBR - ?????? */
661 /* reset interface and alternate settings */
662 device->interface = device->alternate = 0;
664 /*dbg_ep0(2, "set configuration: %d", device->configuration); */
665 /*serial_printf("DEVICE_CONFIGURED.. event?\n"); */
668 case USB_REQ_SET_INTERFACE:
669 device->interface = le16_to_cpu (request->wIndex);
670 device->alternate = le16_to_cpu (request->wValue);
671 /*dbg_ep0(2, "set interface: %d alternate: %d", device->interface, device->alternate); */
672 serial_printf ("DEVICE_SET_INTERFACE.. event?\n");
675 case USB_REQ_GET_STATUS:
676 case USB_REQ_GET_DESCRIPTOR:
677 case USB_REQ_GET_CONFIGURATION:
678 case USB_REQ_GET_INTERFACE:
679 case USB_REQ_SYNCH_FRAME: /* XXX should never see this (?) */