實現乙個基本框架
#define notice(fmt,args...) printk(kern_notice "scull-->" fmt,##args)
#define error(fmt,args...) printk(kern_err "scull-->" fmt,##args)
static __init int scull_init(void)
static __exit void scull_exit(void)
module_init(scull_init);
module_exit(scull_exit);
module_author("******");
module_description("share buffer");
module_alias("scull");
module_version("v1.0");
分配裝置號註冊字元裝置
int register_chrdev_region(dev_t,unsigned int,const char*);表示靜態的申請和註冊裝置號int alloc_chrdev_region(dev_t,unsigned int,const char*);表示動態的申請和註冊裝置號
int register_chrdev(unsigned int,const char*,struct file_operations*);表示int為0時動態註冊,非零靜態註冊
dev_t dev_num=mkdev(major_no,0);將裝置號轉換為dev_t型別變數,第乙個引數是主裝置號,第二個是次裝置號
此時,使用lsmod,可以發現你的裝置就在其中為裝置分配記憶體
1.靜態記憶體初始化
2.動態記憶體初始化struct cdev my_cdev;
cdev_init(&my_cdev, &fops);//裝置操作結構體
my_cdev.owner = this_module;
struct cdev *my_cdev = cdev_alloc();my_cdev->ops = &fops;
my_cdev->owner = this_module;
新增裝置到系統
int cdev_add(struct cdev *p, dev_t num, unsigned count);num 是這個裝置響應的第乙個裝置號, count 是關聯到設從系統去除乙個字元裝置備的裝置號的數目. 常常是 1
void cdev_del(struct cdev *dev);釋放裝置號
void unregister_chrdev_region(dev_t first, unsigned int count);例項
#define notice(fmt,args...) printk(kern_notice "scull-->" fmt,##args)
#define error(fmt,args...) printk(kern_err "scull-->" fmt,##args)
#define dev_name "scull"
struct scull_dev
;static struct file_operations fops =
;static int major = 0;
static int setup_scull(struct scull_dev *p_dev)
return 0;
}static __init int scull_init(void)
else
struct scull_dev *pscull_dev = kmalloc(sizeof(struct scull_dev),gfp_kernel);
if (null == pscull_dev)
ret = setup_scull(pscull_dev);
if (ret < 0)
notice("scull init\n");
return 0;
err_kmalloc:
unregister_chrdev_region(mkdev(major,0), 1);
return -1;
}static __exit void scull_exit(void)
module_init(scull_init);
module_exit(scull_exit);
module_author("******");
module_description("share buffer");
module_alias("scull");
module_version("v1.0");
簡單的乙個字元裝置驅動
include include include include include include include include include include include include 包含記憶體管理兩個核心函式 include memdev.h static int mem major me...
c c ,輸入乙個字元
getch getche 和getchar 函式 1 getch 和getche 函式 這兩個函式都是從鍵盤上讀入乙個字元。其呼叫格式為 getch getche 兩者的區別是 getch 函式不將讀入的字元回顯在顯示螢幕上,而getche 函式卻將讀入的字元回顯到顯示螢幕上。例1 include ...
等等是乙個字元
在使用字串的過程中,我們經常會用到轉義字元,如 等等。對字串中的轉義字元進行操作時,有可能會誤認為這是兩個字元,需要我們根據轉義的定義,將這兩個字元轉換為乙個字元。其實完全不需要,因為對字串進行操作時,計算機自動地將它們識別為乙個字元。include stdio.h int my print str...