標籤:c a int get com 檔案
Linux驅動:
角色:應用程式
API
作業系統
驅動
實際硬體
功能:
1.對裝置進行初始化和釋放
2.把資料從核心傳送到硬體和從硬體讀取資料
3.檢測和處理裝置出現的錯誤
Linux驅動程式類型:
字元裝置:由檔案系統管理 (通過裝置檔案訪問)
塊裝置:由檔案系統管理
網路裝置:由協議棧管理 (通過socket訪問)
查看系統裝置檔案
ls -l /dev
cat /proc/devices
cat /sys/power/state
屬性:檔案類型 主裝置號 次裝置號
裝置編號:<linux/types.h>
dev_t 32位,12為主裝置號,20位為次裝置號
實際使用:<linux/kdev_t.h>
dev_t --> 主裝置號,次裝置號
MAJOR(dev_t dev);
MINOR(dev_t dev);
主裝置號,次裝置號-->(dev_t)
MKDEV(int major,int minor)
裝置驅動組成:
1.配置初始化,註冊裝置,檢測硬體裝置是否正常工作。
2.服務於I/O請求的子程式
3.中斷服務子程式
1.系統運行時編譯並載入,卸載模組:insmod,rmmod
2.Linux核心原始碼中編譯成模組,啟動後用insmod,rmmod
或者直接在原始碼中載入進核心。
輸出:核心編程不能使用使用者態C庫函數中的printf()
只能用printk().為日誌機制,記錄日誌資訊或者給出警告提示。(var/log/messages)
記錄層級:預設:DEFAULT_MESSAGE_LOGLEVEL 4
<linux/kernel.h>
#define KERN_EMERG "<0>" /* system is unusable */
#define KERN_ALERT "<1>" /* action must be taken immediately */
#define KERN_CRIT "<2>" /* critical conditions */
#define KERN_ERR "<3>" /* error conditions */
#define KERN_WARNING "<4>" /* warning conditions */
#define KERN_NOTICE "<5>" /* normal but significant */
#define KERN_INFO "<6>" /* informational */
#define KERN_DEBUG "<7>" /* debug-level messages */
當優先順序小於int console_loglevel變數的時候,訊息列印到控制台上。
如果syslogd和klogd守護進程在啟動並執行話,訊息只能加進var/log/messages檔案
klogd只處理核心資訊,syslogd處理其他系統資訊,比如應用程式
驅動程式與應用程式的區別:
1.驅動沒有main,以模組初始化函數為入口
2.驅動完成初始化後不再運行,等待系統調用
3.驅動不能使用標準C庫
*編譯驅動到核心模板:
1.而配置Kconfig,開啟linux-2.6.32.2/drivers/char/Kconfig,
找到MINI2440_HELLO_MODULE,複製一段下來,修改成:
config MY_HELLO
tristate "my hello module"
depends on MACH_MINI2440
default m if MACH_MINI2440
help
Mini2440 module samples
2.在make menuconfig,可以在Device Drivers-> Character devices中看到my hello module
選擇為M
3.開啟linux-2.6.32.2/drivers/char/Makefile
在有CONFIG_MINI2440_HELLO_MODULE下增加:
obj-S(CONFIG_MY_HELLO) +=hello.o
4.回到根目錄,執行:make modules
產生:hello.ko
系統運行後編譯模板基本步驟:
1.編譯
make
即等於:make modules
Makefile中:
先看KERNELRELEASE是在linux核心源碼中主Makefile中是否定義,如果定義,則編譯產生目標檔案
若未定義:定義:主機:KERNELDIR ?= /lib/modules/$(shell uname -r)/build
開發板:KERNELDIR ?=/mnt/hgfs/LinuxShared/linux-2.6.32/linux-2.6.32.2
即KERNELDIR是本Makefile 依賴的linux核心源碼路徑 ,如是交叉編譯時間就取開發板上啟動並執行源碼路徑
make: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
到linux源碼所在的目錄執行主Makefile 併當前路徑傳給主Makefile,告訴主Makefile執行完後返回到目前的目錄,執行Makefile
2.安裝模組
insmod hello.ko
modprobe -r filename 載入驅動時會同時載入該模組所依賴的其他模組。
insmod 模組名 參數名=參數值
定義參數:
module_param(參數名、參數類型、參數讀寫屬性)
static char *string_test = “this is a test”;
static num_test = 1000;
module_param (num_test,int,S_IRUGO);
module_param (steing_test,charp,S_ITUGO);
3.顯示模組
lsmod |grep hello
4.刪除模組
rmmod hello
5.擷取模組資訊
modinfo hello.ko
6.清除
make clean
---字元驅動---
操作步驟:
//裝置的探測和初始化
************************************************************
static __init int myinit(void)
{
//裝置探測相關的
//1.裝置的常用屬性分配 -- 裝置名稱,裝置號
//2.裝置的註冊
//裝置初始化
//1.驅動中使用的一系列空間分配
//2.和裝置相關的資源的初始化 -- GPIO口的定義(屬性,引腳)
return 0;
}
static __exit void myexit(void)
{
//登出裝置
//釋放資源
}
//IO介面的實現
//中斷的處理
***********************************************************
<linux/fs.h>
struct file_operations
{
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
};
struct file
{
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union
{
struct list_head fu_list;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
#define f_dentry f_path.dentry
#define f_vfsmnt f_path.mnt
const struct file_operations *f_op;
spinlock_t f_lock; /* f_ep_links, f_flags, no IRQ */
atomic_long_t f_count;
unsigned int f_flags;
fmode_t f_mode;
loff_t f_pos;
struct fown_struct f_owner;
const struct cred *f_cred;
struct file_ra_state f_ra;
u64 f_version;
#ifdef CONFIG_SECURITY
void *f_security;
#endif
/* needed for tty driver, and maybe others */
void *private_data;
#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head f_ep_links;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};
FILE *fp;
<asm/uaccess.h>
unsigned long copy_from_user(void *to, const void *from, unsigned long n);
unsigned long copy_to_user (void * to, void * from, unsigned long len);
globvar_driver.c編譯後產生的ko檔案載入後,通過lsmod或者cat /proc/devices都可以查看到該裝置已經在系統中載入,但是在/dev下面有產生裝置檔案,需要手動建立節點
[[email protected] driver_2]# mknod /dev/globalvar c 230 0
[[email protected] driver_2]# ls /dev
---Linux2.6核心字元驅動---
<linux/cdev.h>
struct cdev {
struct kobject kobj; // 每個 cdev 都是一個 kobject
struct module *owner; // 指向實現驅動的模組
const struct file_operations *ops; // 操縱這個字元裝置檔案的方法
struct list_head list; // 與 cdev 對應的字元裝置檔案的 inode->i_devices 的鏈表頭
dev_t dev; // 起始裝置編號
unsigned int count; // 裝置範圍號大小
};
定義一個struct cdev類型的對象來表示當前的裝置驅動
給cdev對象分配空間
初始化cdev: cdev_init
分配裝置號:regist_chrdev_region或alloc_chrdev_region
裝置驅動添加到系統:cdev_add
介面實現
登出: cdev_del
unregister_chrdev_region