2012-04-20 27 views
4

私は、USBデバイス用のLinuxデバイスドライバを書く方法を学ぶ初心者です。コードをコンパイルする際にエラーが発生します。コメント行に問題があります。次のように私は、USBドライブのためのモジュールを作っています:シンプルなUSBドライバ

#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/usb.h> 

static int pen_probe(struct usb_interface *intf,const struct usb_device_id *id) 
{ 
    printk(KERN_ALERT"\nthe probe is successful"); 
    return 0; 
} 

static void pen_disconnect(struct usb_interface *intf) 
{ 
    printk(KERN_ALERT"pen drive removed"); 
} 

const struct usb_device_id pen_table = { 
    USB_DEVICE(0x058f,0x6387), 
}; 

MODULE_DEVICE_TABLE(usb,pen_table); 

static struct usb_driver pen_driver = { 
    .name = "pen_driver", 
    .id_table = pen_table, // error coming at this line 
    .probe = pen_probe, 
    .disconnect = pen_disconnect, 
}; 

static int __init pen_init(void) 
{ 
    int ret; 
    ret = usb_register(&pen_driver); 
    printk(KERN_ALERT"THE RET::%d\n",ret); 
    return 0; 
} 

static void __exit pen_exit(void) 
{ 
    usb_deregister(&pen_driver); 
} 

module_init(pen_init); 
module_exit(pen_exit); 

MODULE_LICENSE("GPL"); 

次のようにそれは私にエラーを与えている:構造の

:26:5: error: initializer element is not constant 

    /home/karan/practice/usb/usb1.c:26:5: error: (near initialization for ‘pen_driver.id_table’) 

答えて

3

id_tableメンバーは、タイプconst struct usb_device_id *のですが、const struct usb_device_idを割り当てています。構造体の初期化でpen_table&pen_tableに変更してみてください。
これが役立つことを願っています!
編集:実際にはpen_tableの宣言が間違っているようです。

const struct usb_device_id pen_table[] = { 
    {USB_DEVICE(0x058f,0x6387)}, 
    {} 
}; 

と(以前に示唆されているようにしていない&pen_table)あなたのコードで行ったように初期化がpen_tableする必要があります:それはおそらくする必要があります。

+0

ドライバー/ USB/USB-skeleton.cグレッグ・クロー=ハートマンによって書かれたことができます詳細については参照してください。 – Jayzcode

-1
obj-m +=usbDemo1.o 

KVERSION = $(shell uname -r) 
all: 
     make -C/lib/modules/$(KVERSION)/build M=$(PWD) modules 
clean: 
     make -C/lib/modules/$(KVERSION)/build M=$(PWD) clean 
+3

いくつかの説明が役に立ちます – PaulG

0
#include <linux/module.h> 
#include <linux/kernel.h> 
#include <linux/usb.h> 

static int pen_probe(struct usb_interface *interface, const struct usb_device_id *id) 
{ 
printk(KERN_INFO "Pen drive (%04X:%04X) plugged\n", id->idVendor, id->idProduct); 
return 0; 
} 

static void pen_disconnect(struct usb_interface *interface) 
{ 
printk(KERN_INFO "Pen drive removed\n"); 
} 

static struct usb_device_id pen_table[] = 
{ 
{ USB_DEVICE(0x058F, 0x6387) }, 
{} /* Terminating entry */ 
}; 
MODULE_DEVICE_TABLE (usb, pen_table); 

    static struct usb_driver pen_driver = 
    { 
.name = "pen_driver", 
.id_table = pen_table, 
.probe = pen_probe, 
.disconnect = pen_disconnect, 
}; 

    static int __init pen_init(void) 
{ 
return usb_register(&pen_driver); 
    } 

static void __exit pen_exit(void) 
    { 
usb_deregister(&pen_driver); 
    } 

    module_init(pen_init); 
    module_exit(pen_exit); 
+0

ようこそ、ここでは、ソリューションの使い方を説明するのは良い習慣です。それはあなたの答えをより価値のあるものにし、読者があなたのやり方をより良く理解できるようにします。また、私たちのよくある質問をご覧ください:http://stackoverflow.com/faq。 – ForceMagic

-2

OBJ-M + = usbDemo1.o

KVERSION = $(シェルのuname -rの) すべて: -C/libに/モジュール/ $(KVERSION)を作成/ Mを構築= $(PWD)モジュール クリーン:クリーン メイク-C/libに/モジュール/ $(KVERSION)/ M = $構築(PWD)

関連する問題