ep0.c

来自「linux嵌入式课程实践中的一个关于声卡驱动程序 。」· C语言 代码 · 共 659 行 · 第 1/2 页

C
659
字号
                // iterate across interface alternates                for (bAlternateSetting = 0; bAlternateSetting < interface_instance->alternates; bAlternateSetting++) {                    int class;                    int bNumEndpoint;                    struct usb_interface_descriptor *interface_descriptor;                    struct usb_alternate_instance *alternate_instance;                    //dbg_ep0(3, "[%d:%d] alternates: %d", bNumInterface, bAlternateSetting, interface_instance->alternates);                    if (!(alternate_instance = usbd_device_alternate_instance(device, port, index,                                     bNumInterface, bAlternateSetting)))                     {                        dbg_ep0(3, "[%d] alternate_instance NULL", bNumInterface);                        return -EINVAL;                    }                    // copy descriptor for this interface                    copy_config(urb, alternate_instance->interface_descriptor, sizeof(struct usb_interface_descriptor), max);                    //dbg_ep0(3, "[%d:%d] classes: %d endpoints: %d", bNumInterface, bAlternateSetting,                     //        alternate_instance->classes, alternate_instance->endpoints);                    // iterate across classes for this alternate interface                    for (class = 0; class < alternate_instance->classes ; class++) {                        struct usb_class_descriptor *class_descriptor;                        //dbg_ep0(3, "[%d:%d:%d] classes: %d", bNumInterface, bAlternateSetting,                         //        class, alternate_instance->classes);                        if (!(class_descriptor =                                     usbd_device_class_descriptor_index(device, port, index,                                         bNumInterface, bAlternateSetting, class)))                         {                            dbg_ep0(3, "[%d] class NULL", class);                            return -EINVAL;                        }                        // copy descriptor for this class                        copy_config(urb, class_descriptor, sizeof(struct usb_class_descriptor), max);                    }                                        // iterate across endpoints for this alternate interface                    interface_descriptor = alternate_instance->interface_descriptor;                    for (bNumEndpoint = 0; bNumEndpoint < alternate_instance->endpoints ; bNumEndpoint++) {                        struct usb_endpoint_descriptor *endpoint_descriptor;                        //dbg_ep0(3, "[%d:%d:%d] endpoint: %d", bNumInterface, bAlternateSetting,                         //        bNumEndpoint, interface_descriptor->bNumEndpoints);                        if (!(endpoint_descriptor =                                     usbd_device_endpoint_descriptor_index(device, port, index,                                         bNumInterface, bAlternateSetting, bNumEndpoint)))                         {                            dbg_ep0(3, "[%d] endpoint NULL", bNumEndpoint);                            return -EINVAL;                        }                        // copy descriptor for this endpoint                        copy_config(urb, endpoint_descriptor, sizeof(struct usb_endpoint_descriptor), max);                    }                }            }            //dbg_ep0(3, "lengths: %d %d", le16_to_cpu(configuration_descriptor->wTotalLength), urb->actual_length);        }        break;    case USB_DESCRIPTOR_TYPE_STRING:        // dbg_ep0(2, "USB_DESCRIPTOR_TYPE_STRING");        {            struct usb_string_descriptor *string_descriptor;            if (!(string_descriptor = usbd_get_string(index))) {                printk ("get_descriptor failed\n");                return -EINVAL;            }            //dbg_ep0(3, "string_descriptor: %p", string_descriptor);            copy_config(urb, string_descriptor, string_descriptor->bLength, max);            printk ("ep0_get_descriptor: string: index = %d, length %d, data = ", index, string_descriptor->bLength);            for (i = 0; i < urb->actual_length; i++)            {                printk ("%2x ", *(char *) (urb->buffer + i));            }        }        break;    case USB_DESCRIPTOR_TYPE_INTERFACE:        //dbg_ep0(2, "USB_DESCRIPTOR_TYPE_INTERFACE");        return -EINVAL;    case USB_DESCRIPTOR_TYPE_ENDPOINT:        //dbg_ep0(2, "USB_DESCRIPTOR_TYPE_ENDPOINT");        return -EINVAL;    default:        //dbg_ep0(2, "USB_DESCRIPTOR_TYPE_ UNKNOWN");        return -EINVAL;    }    //dbg_ep0(1, "urb: buffer: %p buffer_length: %2d actual_length: %2d packet size: %2d",     //        urb->buffer, urb->buffer_length, urb->actual_length, device->bus->endpoint_array[0].tx_packetSize);/*    if ((urb->actual_length < max) && !(urb->actual_length % device->bus->endpoint_array[0].tx_packetSize)) {        dbg_ep0(0, "adding null byte");        urb->buffer[urb->actual_length++] = 0;        dbg_ep0(0, "urb: buffer_length: %2d actual_length: %2d packet size: %2d",                 urb->buffer_length, urb->actual_length device->bus->endpoint_array[0].tx_packetSize);    }*/    return 0;}/* * * ep0_recv_setup - called to indicate URB has been received * @urb - pointer to struct usbd_urb * * Check if this is a setup packet, process the device request, put results * back into the urb and return zero or non-zero to indicate success (DATA) * or failure (STALL). * */static int ep0_recv_setup (struct usbd_urb *urb){    //struct usb_device_request *request = urb->buffer;    //struct usb_device_instance *device = urb->device;    struct usb_device_request *request;    struct usb_device_instance *device;    int address;    if (!urb || !urb->device) {        dbg_ep0(3, "invalid URB %p", urb);        return -EINVAL;    }    request = &urb->device_request;    device = urb->device;    //dbg_ep0(3, "urb: %p device: %p", urb, urb->device);    //dbg_ep0(2, "-       -       -       -       -       -       -       -       -       -");   //  dbg_ep0(2, "bmRequestType:%02x bRequest:%02x wValue:%04x wIndex:%04x wLength:%04x\n",   //         request->bmRequestType, request->bRequest,    //         le16_to_cpu(request->wValue),    //         le16_to_cpu(request->wIndex),    //         le16_to_cpu(request->wLength));    // handle USB Standard Request (c.f. USB Spec table 9-2)    if ((request->bmRequestType&USB_REQ_TYPE_MASK)!=0) {        dbg_ep0(1, "non standard request: %x", request->bmRequestType&USB_REQ_TYPE_MASK);        return 0; // XXX    }#if 0    // check that this is for endpoint zero and is a SETUP packet    if (urb->endpoint->endpoint_address != 0) {        dbg_ep0(1, "endpoint non zero: %d or not setup packet: %x", urb->endpoint->endpoint_address, urb->pid);        return -EINVAL;    }#endif    switch(device->device_state) {    case STATE_CREATED:    case STATE_ATTACHED:    case STATE_POWERED:        dbg_ep0(1, "request %x not allowed in this state: %x", request->bmRequestType, device->device_state);        return -EINVAL;    case STATE_INIT:    case STATE_DEFAULT:        switch (request->bRequest) {        case USB_REQ_GET_STATUS:        case USB_REQ_GET_INTERFACE:        case USB_REQ_SYNCH_FRAME:           // XXX should never see this (?)        case USB_REQ_CLEAR_FEATURE:                 case USB_REQ_SET_FEATURE:        case USB_REQ_SET_DESCRIPTOR:        case USB_REQ_SET_CONFIGURATION:        case USB_REQ_SET_INTERFACE:            dbg_ep0(1, "request %x not allowed in DEFAULT state: %x", request->bmRequestType, device->device_state);            return -EINVAL;        case USB_REQ_SET_ADDRESS:        case USB_REQ_GET_DESCRIPTOR:        case USB_REQ_GET_CONFIGURATION:            break;        }    case STATE_ADDRESSED:    case STATE_CONFIGURED:        break;    case STATE_UNKNOWN:        dbg_ep0(1, "request %x not allowed in UNKNOWN state: %x", request->bmRequestType, device->device_state);        return -EINVAL;    }    // handle all requests that return data (direction bit set on bm RequestType)    if ((request->bmRequestType&USB_REQ_DIRECTION_MASK)) {        //dbg_ep0(3, "Device-to-Host");        switch (request->bRequest) {        case USB_REQ_GET_STATUS:            return ep0_get_status(device, urb, request->bmRequestType&USB_REQ_RECIPIENT_MASK);        case USB_REQ_GET_DESCRIPTOR:            return ep0_get_descriptor(device, urb,                     le16_to_cpu(request->wLength),                     le16_to_cpu(request->wValue>>8),                     le16_to_cpu(request->wValue)&0xff);        case USB_REQ_GET_CONFIGURATION:            return ep0_get_one(device, urb, device->configuration);        case USB_REQ_GET_INTERFACE:            return ep0_get_one(device, urb, device->interface);        case USB_REQ_SYNCH_FRAME:           // XXX should never see this (?)            return -EINVAL;        case USB_REQ_CLEAR_FEATURE:                 case USB_REQ_SET_FEATURE:        case USB_REQ_SET_ADDRESS:        case USB_REQ_SET_DESCRIPTOR:        case USB_REQ_SET_CONFIGURATION:        case USB_REQ_SET_INTERFACE:            return -EINVAL;        }    }    // handle the requests that do not return data    else {        //dbg_ep0(3, "Host-to-Device");                switch (request->bRequest) {        case USB_REQ_CLEAR_FEATURE:              case USB_REQ_SET_FEATURE:                     switch(request->bmRequestType&USB_REQ_RECIPIENT_MASK) {            case USB_REQ_RECIPIENT_DEVICE:            case USB_REQ_RECIPIENT_INTERFACE:            case USB_REQ_RECIPIENT_OTHER:                return -EINVAL;            case USB_REQ_RECIPIENT_ENDPOINT:                return usbd_device_feature(device, le16_to_cpu(request->wIndex)&0x7f, request->bRequest==USB_REQ_SET_FEATURE);            }            // XXX need to save requestype and values            usbd_device_event(device, DEVICE_SET_FEATURE, 0);            return 0;        case USB_REQ_SET_ADDRESS:            // check if this is a re-address, reset first if it is (this shouldn't be possible)            if (device->device_state != STATE_DEFAULT) {                dbg_ep0(1, "set_address: %02x state: %d",                         le16_to_cpu(request->wValue), device->device_state);                return -EINVAL;            }            address = le16_to_cpu(request->wValue);            if ((address&0x7f) != address) {                dbg_ep0(1, "invalid address %04x %04x", address, address&0x7f);                return -EINVAL;            }            device->address = address;            //dbg_ep0(2, "address: %d %d %d",             //        request->wValue, le16_to_cpu(request->wValue), device->address);            usbd_device_event(device, DEVICE_ADDRESS_ASSIGNED, 0);            return 0;        case USB_REQ_SET_DESCRIPTOR:        // XXX should we support this?            dbg_ep0(0, "set descriptor: NOT SUPPORTED");            return -EINVAL;        case USB_REQ_SET_CONFIGURATION:            // c.f. 9.4.7 - the top half of wValue is reserved            //            if ((device->configuration = le16_to_cpu(request->wValue)&0x7f) != 0) {                // c.f. 9.4.7 - zero is the default or addressed state, in our case this                // is the same is configuration zero                device->configuration--;            }            // reset interface and alternate settings            device->interface = device->alternate = 0;            //dbg_ep0(2, "set configuration: %d", device->configuration);            usbd_device_event(device, DEVICE_CONFIGURED, 0);            return 0;        case USB_REQ_SET_INTERFACE:            device->interface = le16_to_cpu(request->wIndex);             device->alternate = le16_to_cpu(request->wValue);            //dbg_ep0(2, "set interface: %d alternate: %d", device->interface, device->alternate);            usbd_device_event(device, DEVICE_SET_INTERFACE, 0);            return 0;        case USB_REQ_GET_STATUS:        case USB_REQ_GET_DESCRIPTOR:        case USB_REQ_GET_CONFIGURATION:        case USB_REQ_GET_INTERFACE:        case USB_REQ_SYNCH_FRAME:           // XXX should never see this (?)            return -EINVAL;        }    }    return -EINVAL;}/* * * ep0_urb_sent - called to indicate URB transmit finished * @urb: pointer to struct usbd_urb * @rc: result */static int ep0_urb_sent(struct usbd_urb *urb, int rc){    //dbg_ep0(2, "%s rc: %d", urb->device->name, rc);    //usbd_dealloc_urb(urb);    return 0;}static struct usb_function_operations ep0_ops = {    event: ep0_event,    recv_setup: ep0_recv_setup,    urb_sent: ep0_urb_sent,};struct usb_function_driver ep0_driver = {    name: "EP0",    ops: &ep0_ops,};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?