/*设备结构体*/
struct xxx_dev_t {
struct cdev cdev;
...
}
/*设备驱动模块加载函数*/
static int __init xxx_init(void) {
...
/*初始化 cdev*/
cdev_init (&xxx_dev.cdev, &xxx_fops);
xxx_dev.cdev.owner = THIS_MODULE;
/*获取字符设备号*/
if (xxx_major) {
register_chrdev_region (xxx_dev_no, 1, DEV_NAME);
} else {
alloc_chrdev_region (&xxx_dev_no, 0, 1, DEV_NAME);
}
/*注册设备*/
ret = cdev_add (&xxx_dev.cdev, xxx_dev_no, 1);
...
}
/*设备驱动模块卸载函数*/
static void __exit xxx_exit (void)
{
/*释放占用的设备号*/
unregister_chrdev_region (xxx_dev_no, 1);
/*注销设备*/
cdev_del (&xxx_dev.cdev);
...
}
/*读设备*/
ssize_t xxx_read (struct file *filp, char __user *buf, size_t count, loff_t *f_ops)
{
...
copy_to_user (buf, ..., ...);
...
}
/*写设备*/
ssize_t xxx_write (struct file *filp, const char __user *buf, size_t count, loff_t *f_ops)
{
...
copy_from_user (..., buf, ...);
...
}
/* ioctl 函数 */
int xxx_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
...
switch (cmd) {
case XXX_CMD1:
...
break;
case XXX_CMD2:
...
break;
default:
/* 不能支持的命令 */
return -ENOTTY;
}
return 0;
}
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |