liaoliao123
驱动牛犊
驱动牛犊
  • 注册日期2002-07-02
  • 最后登录2005-01-11
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1407回复:3

请高手帮忙看一下这个usb驱动,为何打不开设备?

楼主#
更多 发布于:2002-11-28 09:43
我的usb 驱动源码:
********************************************
card.h:
********************************************

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/ioctl.h>
#include <linux/sched.h>
#include <linux/devfs_fs_kernel.h>
#include <linux/usb.h>

#define DRIVER_VERSION \"v1.0\"
#define DRIVER_AUTHOR \"lily\"
#define DRIVER_DESC \"linux usb card driver\"
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE(\"GPL\");
static struct usb_device_id my_usb_id =
{idVendor       :0x0400,
 idProduct      :0xc35b
};

extern devfs_handle_t usb_devfs_handle;
devfs_handle_t    myusb_devfs_handle;

**********************************************
card.c:
**********************************************

#include \"card.h\"

int device_plug_in = 0;

static int usb_open(struct inode *inode,struct file *file)
{
int err = 0;
printk(\"<5>app call driver open.\\n\");
MOD_INC_USE_COUNT;
if(device_plug_in)
{
return 0;
}
else
{
MOD_DEC_USE_COUNT;
return -EBUSY;
}
}

static int usb_close (struct inode * inode,struct file * file)
{
int err = 0;
printk(\"<5>app call driver close\\n\");
MOD_DEC_USE_COUNT;
return 0;
}

static int usb_ioctl (struct inode *inode,struct file * file, unsigned int cmd,unsigned long arg)
{
return 0;
}

static struct file_operations usb_fops =
{
open            :usb_open,
release         :usb_close,
ioctl           :usb_ioctl,
};

static void * card_usb_probe(struct usb_device *dev)
{
printk(\"<5>usb device probe.\\n\");
if (dev->descriptor.idVendor == 0x0400 && dev->descriptor.idProduct == 0xc35b)
{
printk(\"<5>My usb device pluged in.\\n\");
myusb_devfs_handle = devfs_register(
usb_devfs_handle,
\"cd\",
DEVFS_FL_DEFAULT,
USB_MAJOR,
100,
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH,
&usb_fops,
NULL);

device_plug_in = 1;

}
}
static void * card_usb_disconnect(struct usb_device *dev)
{
printk(\"<5>usb device disconnected.\\n\");
if (dev->descriptor.idVendor == 0x0400 && dev->descriptor.idProduct == 0xc35b)
{
printk(\"<5>My usb device pluged out.\\n\");
devfs_unregister (myusb_devfs_handle);
device_plug_in = 0;
}
}



static struct usb_driver card_usb_driver = {
name :\"cd\",
probe   :card_usb_probe,
disconnect :card_usb_disconnect,
fops :&usb_fops,
id_table :&my_usb_id
};

int init_module(void)
{
printk(\"<5>Module is loaded!\\n\");
usb_register(&card_usb_driver);
return 0;
}
void cleanup_module(void)
{
printk(\"<5>Module is unloaded!\\n\");
usb_deregister(&card_usb_driver);
}


**********************************************
ap.c
************************************************
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/types.h>
int main(int argc,char * argv[])
{
int fd = open(\"/dev/usb/cd\",O_RDWR);
if (fd == -1)
{
printf(\"open failed!\\n\");
return -1;
}
close(fd);
}

加载card.o后,成功探测,但调用ap时, fd = -1, 打不开设备
找不着原因。

最新喜欢:

jeanyejeanye
tigerzd
驱动老牛
驱动老牛
  • 注册日期2001-08-25
  • 最后登录2004-12-13
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2002-11-29 11:17
版主能在此解释一下给大家听吗?
 

根据我与liaoliao123通讯的过程来看,不能成功加载的原因是他在devfs_register使用了非0的mirnor number(100),但是没有在usb_driver的定义中加入MIRNOR : 100,因此无法打开。我原来的例子没有加入这项也能打开是因为这个值缺省是0。
犯强汉者,虽远必诛! [img]http://www.driverdevelop.com/forum/upload/tigerzd/2002-12-13_sf10.JPG[/img]
bearbrother
驱动牛犊
驱动牛犊
  • 注册日期2002-08-23
  • 最后登录2002-12-26
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2002-11-29 10:01
版主能在此解释一下给大家听吗?
tigerzd
驱动老牛
驱动老牛
  • 注册日期2001-08-25
  • 最后登录2004-12-13
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地板#
发布于:2002-11-28 11:17
我已经给你发了邮件了,里面有说明。
犯强汉者,虽远必诛! [img]http://www.driverdevelop.com/forum/upload/tigerzd/2002-12-13_sf10.JPG[/img]
游客

返回顶部