ep0.c

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

C
659
字号
/* * linux/drivers/usbd/ep0.c * * Copyright (c) 2000, 2001, 2002 Lineo * Copyright (c) 2001 Hewlett Packard * * By:  *      Stuart Lynne <sl@lineo.com>,  *      Tom Rushworth <tbr@lineo.com>,  *      Bruce Balden <balden@lineo.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *//* * This is the builtin ep0 control function. It implements all required functionality * for responding to control requests (SETUP packets). * * XXX  * * Currently we do not pass any SETUP packets (or other) to the configured * function driver. This may need to change. * * XXX */#include <linux/config.h>#include <linux/module.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/list.h>#include <asm/uaccess.h>#include <linux/netdevice.h>#include <linux/skbuff.h>#include <linux/etherdevice.h>#include <net/arp.h>#include <linux/rtnetlink.h>#include <linux/smp_lock.h>#include <linux/ctype.h>#include <linux/timer.h>#include <linux/string.h>#include <linux/atmdev.h>#include <linux/pkt_sched.h>#include "usbd.h"#include "usbd-debug.h"#include "usbd-func.h"#include "usbd-bus.h"#include "usbd-inline.h"extern int      dbgflg_usbdcore_ep0;#define dbg_ep0(lvl,fmt,args...) dbgPRINT(dbgflg_usbdcore_ep0,lvl,fmt,##args)/* EP0 Configuration Set ********************************************************************* */#if 0// XXX ep0 does not have or need any configurationstatic struct usb_endpoint_description ep0_endpoints[] = {    { bEndpointAddress: 0x00, attributes: 0, max_size: 8, polling_interval: 0 }};static struct usb_interface_description ep0_interfaces[] = {    {   class: 0, sub_class: 0, protocol: 0,         endpoints: sizeof(ep0_endpoints)/sizeof(struct usb_endpoint_description),        endpoint_list: ep0_endpoints },};struct usb_configuration_description ep0_description[] = {    { name: "EP0", attributes: 0, max_power: 0,         interfaces: sizeof(ep0_interfaces)/sizeof(struct usb_interface_description),        interface_list: ep0_interfaces },};#endif/* * * ep0_event - respond to USB event * @device: * @event: * * Called by lower layers to indicate some event has taken place. Typically * this is reset, suspend, resume and close. */static void ep0_event(        struct usb_device_instance *device,        usb_device_event_t event,        int dummy /* to make fn signature correct */){    //dbg_ep0(1, "%s %d", device->name, event);    switch (event) {    case DEVICE_INIT:    case DEVICE_CREATE:    case DEVICE_DESTROY:    case DEVICE_HUB_CONFIGURED:    case DEVICE_HUB_RESET:    case DEVICE_RESET:    case DEVICE_POWER_INTERRUPTION:    case DEVICE_ADDRESS_ASSIGNED:    case DEVICE_CONFIGURED:    case DEVICE_DE_CONFIGURED:    case DEVICE_BUS_INACTIVE:    case DEVICE_BUS_ACTIVITY:    case DEVICE_SET_INTERFACE:    case DEVICE_SET_FEATURE:    case DEVICE_CLEAR_FEATURE:    case DEVICE_BUS_REQUEST:    case DEVICE_BUS_RELEASE:    case DEVICE_ACCEPT_HNP:    case DEVICE_REQUEST_SRP:    case DEVICE_UNKNOWN:    case DEVICE_FUNCTION_PRIVATE:        break;    }}/* * * ep0_get_status - fill in URB data with appropriate status * @device: * @urb: * @requesttype: * */static int ep0_get_status(struct usb_device_instance *device, struct usbd_urb *urb, int requesttype){    char                *cp;    //dbg_ep0(2, "urb: %p requesttype: %x", urb, requesttype);    // XXX     return 0;    //usbd_alloc_urb_data(urb, 2);    urb->actual_length = 2;    cp = urb->buffer;    switch(requesttype) {    case USB_REQ_RECIPIENT_DEVICE:        cp[0] = USB_STATUS_SELFPOWERED;        break;    case USB_REQ_RECIPIENT_INTERFACE:        break;    case USB_REQ_RECIPIENT_ENDPOINT:        cp[0] = usbd_endpoint_halted(device, urb->endpoint->endpoint_address);        break;    case USB_REQ_RECIPIENT_OTHER:        urb->actual_length = 0;    }    //dbg_ep0(2, "%2x %2x", cp[0], cp[1]);    return 0;}/* * * ep0_get_one * @device: * @urb: * @result: * * Set a single byte value in the urb send buffer. Return non-zero to signal * a request error. */static int ep0_get_one(struct usb_device_instance *device, struct usbd_urb *urb, __u8 result){    //if ((usbd_alloc_urb_data(urb, 1))) {    //    return -EINVAL;    //}    urb->actual_length = 1;                     // XXX 2?    ((char *)urb->buffer)[0] = result;    return 0;}/* * * copy_config * @urb - pointer to urb * @data - pointer to configuration data * @length - length of data * * Copy configuration data to urb transfer buffer if there is room for it. */static void copy_config(struct usbd_urb *urb, void *data, int max_length, int max_buf){    int available;    int length;    //dbg_ep0(3, "-> actual: %d buf: %d max_buf: %d max_length: %d data: %p",     //        urb->actual_length, urb->buffer_length, max_buf, max_length, data);    if (!data) {        dbg_ep0(1, "data is NULL");        return;    }    if (!(length = *(unsigned char *)data)) {        dbg_ep0(1, "length is zero");        return;    }    if (length > max_length) {        dbg_ep0(1, "length: %d >= max_length: %d", length, max_length);        return;    }    //dbg_ep0(1, "   actual: %d buf: %d max_buf: %d max_length: %d length: %d",     //        urb->actual_length, urb->buffer_length, max_buf, max_length, length);    if ((available = /*urb->buffer_length*/ max_buf-urb->actual_length) <= 0) {        return;    }    //dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d",     //        urb->actual_length, urb->buffer_length, max_buf, length, available);    if (length > available) {        length = available;    }    //dbg_ep0(1, "actual: %d buf: %d max_buf: %d length: %d available: %d",     //        urb->actual_length, urb->buffer_length, max_buf, length, available);    memcpy(urb->buffer+urb->actual_length, data, length);    urb->actual_length += length;    //dbg_ep0(3, "<- actual: %d buf: %d max_buf: %d max_length: %d available: %d",     //        urb->actual_length, urb->buffer_length, max_buf, max_length, available);}/* * * ep0_get_descriptor * @device: * @urb: * @max: * @descriptor_type: * @index: * * Called by ep0_rx_process for a get descriptor device command. Determine what * descriptor is being requested, copy to send buffer. Return zero if ok to send, * return non-zero to signal a request error. */static int ep0_get_descriptor(struct usb_device_instance *device, struct usbd_urb *urb, int max, int descriptor_type, int index){    int port = 0; // XXX compound device    char                *cp;    int i = 0;    //dbg_ep0(3, "max: %x type: %x index: %x", max, descriptor_type, index);    if (!urb || !urb->buffer || !urb->buffer_length || (urb->buffer_length < 255)) {        dbg_ep0(2, "invalid urb %p", urb);        return -EINVAL;    }    //usbd_alloc_urb_data(urb, max);    // setup tx urb    urb->actual_length = 0;    cp = urb->buffer;    switch(descriptor_type) {    case USB_DESCRIPTOR_TYPE_DEVICE:        dbg_ep0(2, "USB_DESCRIPTOR_TYPE_DEVICE");        {            struct usb_device_descriptor *device_descriptor;            if (!(device_descriptor = usbd_device_device_descriptor(device, port))) {                return -EINVAL;            }            // copy descriptor for this device            copy_config(urb, device_descriptor, sizeof(struct usb_device_descriptor), max);            // correct the correct control endpoint 0 max packet size into the descriptor            device_descriptor = (struct usb_device_descriptor *)urb->buffer;            device_descriptor->bMaxPacketSize0 = urb->device->bus->driver->maxpacketsize;        }        //dbg_ep0(3, "copied device configuration, actual_length: %x", urb->actual_length);        break;    case USB_DESCRIPTOR_TYPE_CONFIGURATION:        //dbg_ep0(2, "USB_DESCRIPTOR_TYPE_CONFIGURATION");        {            int bNumInterface;            struct usb_configuration_descriptor *configuration_descriptor;            struct usb_device_descriptor *device_descriptor;            if (!(device_descriptor = usbd_device_device_descriptor(device, port))) {                return -EINVAL;            }            //dbg_ep0(2, "%d %d", index, device_descriptor->bNumConfigurations);            if (index > device_descriptor->bNumConfigurations) {                dbg_ep0(0, "index too large: %d > %d", index, device_descriptor->bNumConfigurations);                return -EINVAL;            }            if (!(configuration_descriptor = usbd_device_configuration_descriptor(device, port, index))) {                dbg_ep0(0, "usbd_device_configuration_descriptor failed: %d", index);                return -EINVAL;            }            copy_config(urb, configuration_descriptor, sizeof(struct usb_configuration_descriptor), max);            // iterate across interfaces for specified configuration            for (bNumInterface = 0; bNumInterface < configuration_descriptor->bNumInterfaces ; bNumInterface++) {                int bAlternateSetting;                struct usb_interface_instance *interface_instance;                //dbg_ep0(3, "[%d] bNumInterfaces: %d", bNumInterface, configuration_descriptor->bNumInterfaces);                if (!(interface_instance = usbd_device_interface_instance(device, port, index, bNumInterface))) {                    dbg_ep0(3, "[%d] interface_instance NULL", bNumInterface);                    return -EINVAL;                }

⌨️ 快捷键说明

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