freebsd給我們提供開發設備驅動的例子,這些例子確實很有用。但是我發現下面這個東西有點小問題。
在freebsd 6.1 release的/usr/share/example/kld/cdev/module下的cdevmod.c有一個結構:
QUOTE:static struct cdevsw my_devsw = {
/* version */ .d_version = D_VERSION,
/* open */ .d_open = mydev_open,
/* close */ .d_close = mydev_close,
/* read */ .d_read = mydev_read,
/* write */ .d_write = mydev_write,
/* ioctl */ .d_ioctl = mydev_ioctl,
/* name */ .d_name = "cdev",
/* maj */ .d_maj = CDEV_MAJOR
};在make時會發現,這個結構的d_maj沒有定義。這個結構的定義在/usr/include/sys/conf.h中:
QUOTE:struct cdevsw {
int d_version;
u_int d_flags;
const char *d_name;
d_open_t *d_open;
d_fdopen_t *d_fdopen;
d_close_t *d_close;
d_read_t *d_read;
d_write_t *d_write;
d_ioctl_t *d_ioctl;
d_poll_t *d_poll;
d_mmap_t *d_mmap;
d_strategy_t *d_strategy;
dumper_t *d_dump;
d_kqfilter_t *d_kqfilter;
d_purge_t *d_purge;
d_spare2_t *d_spare2;
uid_t d_uid;
gid_t d_gid;
mode_t d_mode;
const char *d_kind;
/* These fields should not be messed with by drivers */
LIST_ENTRY(cdevsw) d_list;
LIST_HEAD(, cdev) d_devs;
int d_spare3;
struct cdevsw *d_gianttrick;
};顯然,找不到d_maj的定義。如果想make通過,簡單的方法就是去掉d_maj的賦值。修改cdevmod.c中的代碼為這樣:
QUOTE:static struct cdevsw my_devsw = {
/* version */ .d_version = D_VERSION,
/* open */ .d_open = mydev_open,
/* close */ .d_close = mydev_close,
/* read */ .d_read = mydev_read,
/* write */ .d_write = mydev_write,
/* ioctl */ .d_ioctl = mydev_ioctl,
/* name */ .d_name = "cdev",
/* maj */ //.d_maj = CDEV_MAJOR
};然後,make load來load這個module,make unload來unload這個module。也可以在../test目錄下make並運行測試程序試試。注意所有的module裡面的輸出都得用dmesg命令查看。