⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbd.c

📁 linux嵌入式课程实践中的一个关于声卡驱动程序 。
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * linux/drivers/usbd/usbd.c.c - USB Device Core Layer * * 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. * *//* * Notes... * * 1. The order of loading must be: * *        usb-device *        usb-function(s) *        usb-bus(s) * * 2. Loading usb-function modules allows them to register with the usb-device * layer. This makes their usb configuration descriptors available. Currently * function modules cannot be loaded after a bus module has been loaded. * * 3. Loading usb-bus modules causes them to register with the usb-device * layer and then enable their upstream port. This in turn will cause the * upstream host to enumerate this device. Note that bus modules can create * multiple devices, one per physical interface. * * 4. The usb-device layer provides a default configuration for endpoint 0 for * each device.  * * 5. Additional configurations are added to support the configuration * function driver when entering the configured state. * * 6. The usb-device maintains a list of configurations from the loaded * function drivers. It will provide this to the USB HOST as part of the * enumeration process and will add someof them as the active configuration as * per the USB HOST instructions.   * * 6. Two types of bus interface modules can be implemented: simple and compound. * * 7. Simple bus interface modules can only support a single function module. The * first function module is used. * * 8. Compound bus interface modules can support multiple functions. When implemented * these will use all available function modules. * */#include <linux/config.h>#include <linux/module.h>#include "usbd-export.h"#include "usbd-build.h"//#include "usbd-module.h"MODULE_AUTHOR("sl@lineo.com, tbr@lineo.com");MODULE_DESCRIPTION("USB Device Core Support");//USBD_MODULE_INFO("usbdcore 0.1");#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"#include "hotplug.h"#define MAX_INTERFACES 2/* Module Parameters ************************************************************************* */MODULE_PARM(maxstrings, "i");MODULE_PARM(dbg, "s");MODULE_PARM_DESC(maxstrings, "Maximum number of strings to allow (0..255)");MODULE_PARM_DESC(dbg, "debug parameters");int maxstrings = 20;static char *dbg = "init=0:ep0=0:rx=0:tx=0:usbe=0:mem=0";/* Debug switches (module parameter "dbg=...") *********************************************** */int dbgflg_usbdcore_init;int dbgflg_usbdcore_ep0;int dbgflg_usbdcore_rx;int dbgflg_usbdcore_tx;int dbgflg_usbdcore_usbe;int dbgflg_usbdcore_urbmem;static debug_option dbg_table[] = {    {&dbgflg_usbdcore_init,NULL,"init","initialization and termination"},    {&dbgflg_usbdcore_ep0,NULL,"ep0","End Point 0 (setup) packet handling"},    {&dbgflg_usbdcore_rx,NULL,"rx","USB RX (host->device) handling"},    {&dbgflg_usbdcore_tx,NULL,"tx","USB TX (device->host) handling"},    {&dbgflg_usbdcore_usbe,NULL,"usbe","USB events"},    {&dbgflg_usbdcore_urbmem,NULL,"mem","URB memory allocation"},#if 0    {NULL,NULL,"hot","hotplug actions"},    {NULL,NULL,"mon","USB conection monitor"},#endif    {NULL,NULL,NULL,NULL}};#define dbg_init(lvl,fmt,args...) dbgPRINT(dbgflg_usbdcore_init,lvl,fmt,##args)#define dbg_ep0(lvl,fmt,args...) dbgPRINT(dbgflg_usbdcore_ep0,lvl,fmt,##args)#define dbg_rx(lvl,fmt,args...) dbgPRINT(dbgflg_usbdcore_rx,lvl,fmt,##args)#define dbg_tx(lvl,fmt,args...) dbgPRINT(dbgflg_usbdcore_tx,lvl,fmt,##args)#define dbg_usbe(lvl,fmt,args...) dbgPRINT(dbgflg_usbdcore_usbe,lvl,fmt,##args)#define dbg_urbmem(lvl,fmt,args...) dbgPRINT(dbgflg_usbdcore_urbmem,lvl,fmt,##args)/* Global variables ************************************************************************** */struct usb_string_descriptor **usb_strings;int usb_devices;extern struct usb_function_driver ep0_driver;int registered_functions;int registered_devices;char *usbd_device_events[] = {    "DEVICE_UNKNOWN",    "DEVICE_INIT",    "DEVICE_CREATE",    "DEVICE_HUB_CONFIGURED",    "DEVICE_RESET",    "DEVICE_ADDRESS_ASSIGNED",    "DEVICE_CONFIGURED",    "DEVICE_SET_INTERFACE",    "DEVICE_SET_FEATURE",    "DEVICE_CLEAR_FEATURE",    "DEVICE_DE_CONFIGURED",    "DEVICE_BUS_INACTIVE",    "DEVICE_BUS_ACTIVITY",    "DEVICE_POWER_INTERRUPTION",    "DEVICE_HUB_RESET",    "DEVICE_DESTROY",    "DEVICE_FUNCTION_PRIVATE",};char *usbd_device_states[] = {    "STATE_INIT",     "STATE_CREATED",     "STATE_ATTACHED",     "STATE_POWERED",     "STATE_DEFAULT",     "STATE_ADDRESSED",     "STATE_CONFIGURED",     "STATE_UNKNOWN", };/* List support functions ******************************************************************** */LIST_HEAD(function_drivers);    // list of all registered function modulesLIST_HEAD(devices);             // list of all registered devicesstatic inline struct usb_function_driver *list_entry_func(const struct list_head *le){    return list_entry(le, struct usb_function_driver, drivers);}static inline struct usb_device_instance *list_entry_device(const struct list_head *le){    return list_entry(le, struct usb_device_instance, devices);}/* Support Functions ************************************************************************* *//** * usbd_hotplug - call a hotplug script * @action: hotplug action */int usbd_hotplug(char *interface, char *action){#ifdef CONFIG_HOTPLUG    dbg_init(1, "agent: usbd interface: %s action: %s", interface, action);    return hotplug("usbd", interface, action);#else    return(0);#endif}void usbd_request_bus(struct usb_device_instance *device, int request){#ifdef JON_CHECK_THIS_OUT    if(device->bus->driver->ops->bi_event)    {        if(request)        {            device->bus->driver->ops->bi_event(device, DEVICE_BUS_REQUEST, NULL);        }        else        {            device->bus->driver->ops->bi_event(device, DEVICE_BUS_RELEASE, NULL);        }    }#endif}/* Descriptor support functions ************************************************************** *//** * usbd_get_string - find and return a string descriptor * @index: string index to return * * Find an indexed string and return a pointer to a it. */struct usb_string_descriptor * usbd_get_string(__u8 index){    if (index >= maxstrings) {        return NULL;    }    return usb_strings[index];}/** * usbd_cancel_urb - cancel an urb being sent * @urb: pointer to an urb structure * * Used by a USB Function driver to cancel an urb that is being * sent. */int usbd_cancel_urb(struct usb_device_instance *device, struct usbd_urb *urb){    dbg_tx(3,"%p", urb);    if (!device->bus->driver->ops->cancel_urb) {        // XXX should we do usbd_dealloc_urb(urb);        return 0;    }    //urb->device->urbs_queued++;    return device->bus->driver->ops->cancel_urb(urb);}/* Access to device descriptor functions ***************************************************** *//* * * usbd_device_function_instance - find a function instance for this device * @device:  * @configuration: index to configuration, 0 - N-1  * * Get specifed device configuration. Index should be bConfigurationValue-1.  */struct usb_function_instance *usbd_device_function_instance(struct usb_device_instance *device, unsigned int port){    //dbg_init(2,"device: %p port: %d functions: %d", device, port, device->functions);    if (port >= device->functions) {        dbg_init(0, "configuration out of range: %d %d", port, device->functions);        return NULL;    }    return device->function_instance_array+port;}/* * * usbd_device_configuration_instance - find a configuration instance for this device * @device:  * @configuration: index to configuration, 0 - N-1  * * Get specifed device configuration. Index should be bConfigurationValue-1.  */static struct usb_configuration_instance *usbd_device_configuration_instance(struct usb_device_instance *device, unsigned int port, unsigned int configuration){    struct usb_function_instance *function_instance;    struct usb_function_driver *function_driver;    if (!(function_instance = usbd_device_function_instance(device, port))) {        dbg_init(0, "function_instance NULL");        return NULL;    }    if (!(function_driver = function_instance->function_driver)) {        dbg_init(0, "function_driver NULL");        return NULL;    }    if (configuration >= function_driver->configurations) {        dbg_init(0, "configuration out of range: %d %d %d", port, configuration, function_driver->configurations);        return NULL;    }    return function_driver->configuration_instance_array+configuration;}/* * * usbd_device_interface_instance * @device:  * @configuration: index to configuration, 0 - N-1  * @interface: index to interface * * Return the specified interface descriptor for the specified device. */struct usb_interface_instance *usbd_device_interface_instance(struct usb_device_instance *device, int port, int configuration, int interface){    struct usb_configuration_instance *configuration_instance;    if ((configuration_instance = usbd_device_configuration_instance(device, port, configuration)) == NULL) {        return NULL;    }    if (interface >= configuration_instance->interfaces) {        return NULL;    }    return configuration_instance->interface_instance_array+interface;}/* * * usbd_device_alternate_descriptor_list * @device:  * @configuration: index to configuration, 0 - N-1  * @interface: index to interface * @alternate: alternate setting * * Return the specified alternate descriptor for the specified device. */struct usb_alternate_instance *usbd_device_alternate_instance(struct usb_device_instance *device, int port, int configuration, int interface, int alternate){    struct usb_interface_instance *interface_instance;    if ((interface_instance = usbd_device_interface_instance(device, port, configuration, interface)) == NULL) {        return NULL;    }    if (alternate >= interface_instance->alternates) {        return NULL;    }    return interface_instance->alternates_instance_array+alternate;}/* * * usbd_device_device_descriptor * @device: which device * @configuration: index to configuration, 0 - N-1  * @port: which port * * Return the specified configuration descriptor for the specified device. */struct usb_device_descriptor *usbd_device_device_descriptor(struct usb_device_instance *device, int port){    struct usb_function_instance *function_instance;    if (!(function_instance = usbd_device_function_instance(device, port))) {        return NULL;    }    if (!function_instance->function_driver || !function_instance->function_driver->device_descriptor) {        return NULL;    }    return (function_instance->function_driver->device_descriptor);}/** * usbd_device_configuration_descriptor * @device: which device * @port: which port * @configuration: index to configuration, 0 - N-1  * * Return the specified configuration descriptor for the specified device. */struct usb_configuration_descriptor *usbd_device_configuration_descriptor(struct usb_device_instance *device, int port, int configuration){    struct usb_configuration_instance *configuration_instance;    if (!(configuration_instance = usbd_device_configuration_instance(device, port, configuration))) {        return NULL;    }    return (configuration_instance->configuration_descriptor);}/** * usbd_device_interface_descriptor * @device: which device * @port: which port * @configuration: index to configuration, 0 - N-1  * @interface: index to interface * @alternate: alternate setting * * Return the specified interface descriptor for the specified device. */struct usb_interface_descriptor *usbd_device_interface_descriptor(struct usb_device_instance *device, int port, int configuration, int interface, int alternate){    struct usb_interface_instance *interface_instance;    if (!(interface_instance = usbd_device_interface_instance(device, port, configuration, interface))) {        return NULL;    }    if ((alternate < 0) || (alternate >= interface_instance->alternates)) {        return NULL;    }    return (interface_instance->alternates_instance_array[alternate].interface_descriptor);}/** * usbd_device_class_descriptor_index * @device: which device * @port: which port * @configuration: index to configuration, 0 - N-1  * @interface: index to interface * @index: which index * * Return the specified class descriptor for the specified device. */struct usb_class_descriptor *usbd_device_class_descriptor_index(        struct usb_device_instance *device,         int port,         int configuration,         int interface,         int alternate,         int index){    struct usb_alternate_instance *alternate_instance;    if (!(alternate_instance = usbd_device_alternate_instance(device, port, configuration, interface, alternate))) {        return NULL;

⌨️ 快捷键说明

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