//字元裝置的結構體struct xxx_dev_t{struct cdev cdev;...........}xxx_dev;//字元裝置模組的載入和卸載函數static int __init xxx_init(void){......./*初始化cdev*/cdev_init(&xxx_dev.cdev,&xxx_fops);xxx_dev.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);............}//字元裝置驅動的file_operations結構體中的成員函數/*讀裝置*/ssize_t xxx_read(struct file *filp,char __usr *buf,size_t count,loff_t *f_pos){............copy_to_user(buf,...,...);.............}/*寫裝置*/ssize_t xxx_write(struct file *filp,const char __usr *buf,size_f count,loff_t *f_pos){..............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;}return0;}//核心空間和使用者空間互動,經常用到的兩個函數unsigned long copy_from_user(void *to,const void __user *from,unsigned long count);unsigned long copy_to_usr(void __user *to,const void *from,unsigned long count);/*上述函數均返回不能被複製的位元組數,如果完全複製成功,則傳回值為0。*/