usb-skeleton.c 之 05-- struct usb-skel 赏析

来源:互联网 发布:item 可靠性分析软件 编辑:程序博客网 时间:2024/04/30 14:38
 

/* Structure to hold all of our device specific stuff */

这个结构体的名字有开发人员自定义,它描述的是该驱动拥有的所有资源及状态: 
struct usb_skel {

    struct usb_device    *udev;            /* the usb device for this device */

                                                      该设备的usb_device 指针  制造商 ID  产品ID等等
    struct usb_interface    *interface;        /* the interface for this device */

                                                                 接口
    struct semaphore    limit_sem;        /* limiting the number of writes in progress */

                                                数据共享 ->竞态/并发    信号量  防止竞态发生
    struct usb_anchor    submitted;        /* in case we need to retract our submissions */


    unsigned char           *bulk_in_buffer;    /* the buffer to receive data */

                                                                 接送数据缓冲
    size_t            bulk_in_size;        /* the size of the receive buffer */

                                                                            接受缓冲大小

    __u8            bulk_in_endpointAddr;    /* the address of the bulk in endpoint */
    __u8            bulk_out_endpointAddr;    /* the address of the bulk out endpoint */

                                                               批量输入、输出  8位 端点 地址

    int            errors;            /* the last request tanked */
    int            open_count;        /* count the number of openers */
    spinlock_t        err_lock;        /* lock for errors */
    struct kref        kref;

                                                   内核引用的 计数器
     struct mutex        io_mutex;        /* synchronize I/O with disconnect */

                                                                 同步 IO
};

 

 

**struct usb_device *udev   该设备的usb_device 指针  制造商 ID  产品ID等等

            那struct usb_device定义在 哪里呢? 寻寻觅觅。。 在usb.h 中 定义了她。

 

struct usb_device {
    int        devnum;
    char        devpath [16];
    enum usb_device_state    state;
    enum usb_device_speed    speed;

    struct usb_tt    *tt;
    int        ttport;

    unsigned int toggle[2];

    struct usb_device *parent;  定义了双亲  向上寻找
    struct usb_bus *bus;               usb 总线
    struct usb_host_endpoint ep0;       usb 端点  

    struct device dev;         使用usb_devce结构体描述整个usb设备



    struct usb_device_descriptor descriptor;        设备描述符
    struct usb_host_config *config;                     主机配置

    struct usb_host_config *actconfig;
    struct usb_host_endpoint *ep_in[16];         usb 端口 in
    struct usb_host_endpoint *ep_out[16];        usb  端口  out

    char **rawdescriptors;

    unsigned short bus_mA;
    u8 portnum;
    u8 level;

    unsigned can_submit:1;
    unsigned discon_suspended:1;
    unsigned persist_enabled:1;
    unsigned have_langid:1;
    unsigned authorized:1;
     unsigned authenticated:1;
    unsigned wusb:1;
    int string_langid;

    /* static strings from the device */
    char *product;
    char *manufacturer;
    char *serial;

    struct list_head filelist;
#ifdef CONFIG_USB_DEVICE_CLASS
    struct device *usb_classdev;
#endif
#ifdef CONFIG_USB_DEVICEFS
    struct dentry *usbfs_dentry;
#endif

    int maxchild;
    struct usb_device *children[USB_MAXCHILDREN];

    int pm_usage_cnt;
    u32 quirks;
    atomic_t urbnum;

    unsigned long active_duration;

#ifdef CONFIG_PM
    struct delayed_work autosuspend;
    struct work_struct autoresume;
    struct mutex pm_mutex;

    unsigned long last_busy;
    int autosuspend_delay;
    unsigned long connect_time;

    unsigned auto_pm:1;
    unsigned do_remote_wakeup:1;
    unsigned reset_resume:1;
    unsigned autosuspend_disabled:1;
    unsigned autoresume_disabled:1;
    unsigned skip_sys_resume:1;
#endif
    struct wusb_dev *wusb_dev;
};

 

 

** struct usb_interface    *interface;      设备接口  一个接口相对于一个功能

 

 

    这里我们得补充说明一下一些USB的协议规范细节。USB能够自动 监测设备,并调用相应得驱动程序处理设备, 所以其规 范实际上是相当复杂的,幸好,我们不必理会大部分细节问题,因为Linux已经提供相应的解决 方案 。就我现在的理解来说,USB的驱动分为两块,一块 是USB的bus 总线 驱动, 这个东西,Linux内核已经做好了,我们可以不管,但我们至少要了解他的功能。形象得说,USB的bus驱动相当于铺出一条路 来,让所有的信息都可以通过这条USB通道到达该到的地方,这部分工作由usb_core来完成。当USB设备接到USB控制器接口时,usb_core 就检测该设备的一些信息,例如生产厂商ID和产品的ID,或者是设备所属的class、subclass跟protocol,以便确定应该调用哪一个驱动 处理该设备。里面复杂细节我们不用管,我们要做的是另一块工作——usb的设备驱动。也就是说,我们就等着usb_core告诉我们要工作了,我们才工 作。