usb-skeleton.c 之 08---skel_open

来源:互联网 发布:华为沧州云计算 编辑:程序博客网 时间:2024/05/16 15:13
 

static int skel_open(struct inode *inode, struct file *file)
{
    struct usb_skel *dev;    骨架 结构体
    struct usb_interface *interface;   接口结构体  接口类、子类、和适用的协议,接口备用的数目和端点数目
    int subminor;
    int retval = 0;

    subminor = iminor(inode);  
               **   static inline unsigned iminor (const struct inode *inode )

               **      return MINOR (inode ->i_rdev );  若是设备文件,此i_rdev记录设备的设备号

               **   获得 次设备号

    interface = usb_find_interface(&skel_driver, subminor);**想想也是 通过子次设备号 在driver 中找 接口
    if (!interface) {                                  ** 如果失败 退出
        err ("%s - error, can't find device for minor %d",
             __func__, subminor);
        retval = -ENODEV;
        goto exit;
    }

    dev = usb_get_intfdata(interface);**void *usb_get_intfdata(struct usb_interface *intf);
    if (!dev) {                                        **用于得到usb_interface的私有数据      
        retval = -ENODEV;
        goto exit;
    }

    /* increment our usage count for the device */
    kref_get(&dev->kref);       **增加 我们适用设备的次数

    /* lock the device to allow correctly handling errors
     * in resumption */
    mutex_lock(&dev->io_mutex);  **获得互斥体  和spinlock  semaphore  差不多

    if (!dev->open_count++) {    ** 打开的次数=0  没有被打开
        retval = usb_autopm_get_interface(interface);

                               **static inline int usb_autopm_get_interface (struct usb_interface *intf )

                               **{ return 0; }  再次 赋值  usb_interface  接口   打开接口

            if (retval) {        赋值 失败/打开失败
                dev->open_count--;   打开次数 减 一
                mutex_unlock(&dev->io_mutex);  解锁
                kref_put(&dev->kref, skel_delete);
                goto exit;
            }
    }

/* else { //uncomment this block if you want exclusive open
        retval = -EBUSY;
        dev->open_count--;
        mutex_unlock(&dev->io_mutex);
        kref_put(&dev->kref, skel_delete);
        goto exit;
    } */
    /* prevent the device from being autosuspended */

    /* save our object in the file's private structure */
    file->private_data = dev;     **保存到 私有 数据中
    mutex_unlock(&dev->io_mutex);    **解锁

exit:
    return retval;
}

 

open 函数 主要 是打开 接口设备

原创粉丝点击