LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 842|回复: 7

模块加载问题

[复制链接]
发表于 2005-1-23 15:19:52 | 显示全部楼层 |阅读模式
请问高手,
向内核加载一个驱动程序,出错如下
Warning: loading myusb.o will taint the kernel: no license
  See http://www.tux.org/lkml/#export-tainted for information about tainted modules
Module myusb loaded, with warnings

上网查查,还是没结果,
急待解决.
请高手帮忙啊!
发表于 2005-1-24 09:29:27 | 显示全部楼层
加上这句试试看
MODULE_LICENSE("GPL");
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-24 10:48:29 | 显示全部楼层

谢谢

非常感谢高手,不过,测试时,首先mknod /dev/?? c 180 160  
但open出错,是什么问题呢,是否要指定端口号,但端口怎么找,怎样加呢?
恳请高手指点.
回复 支持 反对

使用道具 举报

发表于 2005-1-24 13:04:10 | 显示全部楼层
什么端口号?
设备只有主设备号和次设备号,没有什么端口号。
其中主设备号是核心用的,而次设备号是可以被你的驱动使用的(也可以不用)!
为什么open出错!没有代码我不能猜啊!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-25 16:42:26 | 显示全部楼层

高手帮忙!

高手,可以把QQ号码,或邮箱告诉我吗?
这样比较好联系.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-25 20:23:15 | 显示全部楼层

程序如下

#define MODULE
#define __NO_VERSION__
#define __KERNEL__
#include <linux/version.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/usb.h>


#define DRIVER_VERSION "MYUSB Driver"

#define USB_MINOR                16

#define USB_VENDOR_ID        0x1043
#define USB_PRODUCT_ID        0x8006


#define NAK_TIMEOUT        (10*HZ)

#define IBUF_SIZE        0x1000
#define OBUF_SIZE        0x10000

struct my_usb_data {
        struct usb_device *my_dev;       
        unsigned int ifnum;               
        int isopen;                       
        int present;                       
        char *obuf, *ibuf;               
        wait_queue_head_t wait_q;       
};

static struct my_usb_data my_instance;

static int open_my(struct inode *inode, struct file *file)
{
        struct my_usb_data *my = &my_instance;
        if (my->isopen || !my->present) {
                return -EBUSY;
        }
        my->isopen = 1;
        init_waitqueue_head(&my->wait_q);
        info("MYUSB opened.");
        return 0;
}

static int close_my(struct inode *inode, struct file *file)
{
        struct my_usb_data *my = &my_instance;
        my->isopen = 0;
        info("MYUSB closed.");
        return 0;
}

static ssize_t
write_my(struct file *file, const char *buffer,
          size_t count, loff_t * ppos)
{
        struct my_usb_data *my = &my_instance;

        unsigned long copy_size;
        unsigned long bytes_written = 0;
        unsigned int partial;

        int result = 0;
        int maxretry;

        if (my == NULL ||    my->present == 0 ||    my->my_dev == NULL)
                return -1;

        do {
                unsigned long thistime;
                char *obuf = my->obuf;

                thistime = copy_size =
                    (count >= OBUF_SIZE) ? OBUF_SIZE : count;
                if (copy_from_user(my->obuf, buffer, copy_size))
                        return -EFAULT;
                maxretry = 5;
                while (thistime) {
                        if (!my->my_dev)
                                return -ENODEV;
                        if (signal_pending(current)) {
                                return bytes_written ? bytes_written : -EINTR;
                        }

                        result = usb_bulk_msg(my->my_dev,
                                         usb_sndbulkpipe(my->my_dev, 0),
                                         obuf, thistime, &partial, 10 * HZ);

        if (result == USB_ST_TIMEOUT) {       
                                if (!maxretry--) {
                                        return -ETIME;
                                }
                                interruptible_sleep_on_timeout(&my-> wait_q, NAK_TIMEOUT);
                                continue;
                        } else if (!result & partial) {
                                obuf += partial;
                                thistime -= partial;
                        } else
                                break;
                };
                if (result) {
                        err("Write Whoops - %x", result);
                        return -EIO;
                }
                bytes_written += copy_size;
                count -= copy_size;
                buffer += copy_size;
        } while (count > 0);

        return bytes_written ? bytes_written : -EIO;
}

static ssize_t
read_my(struct file *file, char *buffer, size_t count, loff_t * ppos)
{
        struct my_usb_data *my = &my_instance;
        ssize_t read_count;
        unsigned int partial;
        int this_read;
        int result;
        int maxretry = 10;
        char *ibuf = my->ibuf;

        if (my == NULL ||    my->present == 0 ||    my->my_dev == NULL)
                return -1;

        read_count = 0;

        while (count > 0) {
                if (signal_pending(current)) {
                        return read_count ? read_count : -EINTR;
                }
                if (!my->my_dev)
                        return -ENODEV;
                this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;

                result = usb_bulk_msg(my->my_dev,
                                      usb_rcvbulkpipe(my->my_dev, 1),
                                      ibuf, this_read, &partial,
                                      (int) (HZ * 8));

                if (partial) {
                        count = this_read = partial;
                } else if (result == USB_ST_TIMEOUT || result == 15) {       
                        if (!maxretry--) {
                                err("read_lcd: maxretry timeout");
                                return -ETIME;
                        }
                        interruptible_sleep_on_timeout(&my->wait_q,
                                                       NAK_TIMEOUT);
                        continue;
                } else if (result != USB_ST_DATAUNDERRUN) {
                        err("Read Whoops - result:%u partial:%u this_read:%u",
                             result, partial, this_read);
                        return -EIO;
                } else {
                        return (0);
                }

                if (this_read) {
                        if (copy_to_user(buffer, ibuf, this_read))
                                return -EFAULT;
                        count -= this_read;
                        read_count += this_read;
                        buffer += this_read;
                }
        }
        return read_count;
}

static void *probe_my(struct usb_device *dev, unsigned int ifnum)
{
        struct my_usb_data *my = &my_instance;
        int i;
       
        if (dev->descriptor.idProduct != 0x8006||dev->descriptor.idVendor!=0x1043 ) {
                warn(KERN_INFO "MYUSB model not supported.");
                return NULL;
        }

        if (my->present == 1) {
                warn(KERN_INFO "Multiple MYUSBs are not supported!");
                return NULL;
        }

        my->present = 1;
        my->my_dev = dev;

        if (!(my->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
                err("probe_lcd: Not enough memory for the output buffer");
                return NULL;
        }

        if (!(my->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
                err("probe_my: Not enough memory for the input buffer");
                kfree(my->obuf);
                return NULL;
        }
        my_instance=*my;
        return my;
}

static void disconnect_my(struct usb_device *dev, void *ptr)
{
        struct my_usb_data *my = (struct my_usb_data *) ptr;

        if (my->isopen) {
                my->isopen = 0;
                my->my_dev = NULL;
                return;
        }
        kfree(my->ibuf);
        kfree(my->obuf);

        info("MYUSB disconnected.");

        my->present = 0;
}

static struct usb_device_id id_table [] = {
        {USB_DEVICE(USB_VENDOR_ID, USB_PRODUCT_ID) },
        {},
};

MODULE_DEVICE_TABLE (usb, id_table);


static struct
file_operations usb_my_fops = {
        read:                read_my,
        write:                write_my,
        open:                 open_my,
        release:        close_my,
};

static struct
usb_driver my_driver = {
        name:                "myusb",
        probe:                (void *)probe_my,
        disconnect:        disconnect_my,
        fops:                &usb_my_fops,
        minor:                USB_MINOR,
};

int usb_my_init(void)
{
       
        if (usb_register(&my_driver) < 0)
                return -1;

        info("MYUSB support registered.");
        return 0;
}


void usb_my_cleanup(void)
{
        struct my_usb_data *my = &my_instance;
        my->present = 0;
        usb_deregister(&my_driver);
}

module_init(usb_my_init);
module_exit(usb_my_cleanup);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-1-25 20:24:58 | 显示全部楼层

问题

不知怎样测试程序是否能打开U盘.
回复 支持 反对

使用道具 举报

发表于 2005-1-26 13:02:39 | 显示全部楼层
没有做过usb,不过我问你个问题你也许就明白了!

usb应该是块设备吧?
如果是:那你怎么能用struct file_operations 呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表