1.linux內核字符設備驅動之發送命令(ioctl)接口
int ioctl(int fd, int request, ...);
函數功能:
1.應用程序可以向設備發送控制命令;2.應用程序還可以和設備進行讀/寫操作;
參數:
fd:設備文件描述符request:向設備發送的命令字,命令定義;
#define LED_ON 0x100001 //開燈命令
#define LED_OFF 0x100002 //關燈命令
...: 可變參數, 如果傳遞第三個參數,第三個參數爲用戶緩衝區的首地址
例如:
用戶需求:向設備發送開燈命令:
ioctl(fd, LED_ON);
用戶需求:向設備發送開燈命令,並且寫一個數據給設備;
int udata = 0x5555;
ioctl(fd, LED_ON, &udata);
對應的底層驅動的ioctl接口:
struct file_operations {long (*unlocked_ioctl)(struct file *file,
unsigned int cmd,
unsigned long arg);
};
unlocked_ioctl接口作用:
1.處理用戶的命令請求2.完成和用戶空間的數據交互(讀或者寫)
調用關係:
應用程序調用ioctl->....->驅動的unlocked_ioctl
參數:
file:文件指針
cmd:用戶發送來的命令字
arg:如果用戶傳遞三個參數,保存着用戶發送來的用戶緩衝區的首地址,內核不能直接訪問操作,必須利用copy_to_user/copy_from_user完成數據的拷貝;
注意在使用的時候要進行數據類型的轉換
案例:利用ioctl實現開關某個燈;
int uindex = 1;
ioctl(fd, LED_ON, &uindex);
int uindex = 2;
ioctl(fd, LED_ON, &uindex);
int uindex = 1;
ioctl(fd, LED_OFF, &uindex);
int uindex = 2;
ioctl(fd, LED_OFF, &uindex);、
底層驅動代碼:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h> //struct file_operations
#include <linux/cdev.h> //struct cdev + 設備號
#include <asm/gpio.h>
#include <plat/gpio-cfg.h>
#include <linux/uaccess.h> //copy_to_user
//聲明LED操作的數據結構
struct led_cmd {
int index;
int cmd;
};
//聲明描述LED硬件相關的數據結構
struct led_resource {
char *name;
int gpio;
};
//定義初始化LED硬件信息
static struct led_resource led_info[] = {
[0] = {
.name = "LED1",
.gpio = S5PV210_GPC0(3)
},
[1] = {
.name = "LED2",
.gpio = S5PV210_GPC0(4)
}
};
//定義設備號
static dev_t dev;
//定義字符設備對象
static struct cdev led_cdev;
//調用關係:應用程序open->....->led_open
static int led_open(struct inode *inode, struct file *file)
{
int i;
for(i = 0; i < ARRAY_SIZE(led_info); i++)
gpio_set_value(led_info[i].gpio, 1);
printk("%s\n", __func__);
return 0; //執行成功返回0,執行失敗返回負值
}
//調用關係:應用程序close->...->led_close
static int led_close(struct inode *inode, struct file *file)
{
int i;
for(i = 0; i < ARRAY_SIZE(led_info); i++)
gpio_set_value(led_info[i].gpio, 0);
printk("%s\n", __func__);
return 0; //執行成功返回0,執行失敗返回負值
}
//調用關係:應用程序read->...->led_read
static ssize_t led_read(struct file *file,
char __user *buf,
size_t count,
loff_t *ppos)
{
//定義初始化內核緩衝區(存儲空間再後1G虛擬內存中)
int kdata = 0x5555;
//將內核數據上報給用戶
//切記:buf雖然保存的用戶緩衝區的首地址,但不能直接訪問
//*(int *)buf = kdata;錯誤
copy_to_user(buf, &kdata, sizeof(kdata));
printk("%s\n", __func__);
return sizeof(kdata); //失敗返回負值,成功返回實際讀取的字節數
}
//調用關係:應用程序write->...->最終調用led_write
static ssize_t led_write(struct file *file,
const char __user *buf,
size_t count,
loff_t *ppos)
{
//定義內核緩衝區
struct led_cmd kdata;
//拷貝用戶數據到內核
copy_from_user(&kdata, buf, sizeof(kdata));
//開或者關燈
gpio_set_value(led_info[kdata.index - 1].gpio, kdata.cmd);
printk("%s:第%d燈被%s\n",
__func__, kdata.index, kdata.cmd?"打開":"關閉");
return count; //失敗返回負值,成功返回寫入的字節數
}
#define LED_ON 0x100001 //開燈命令
#define LED_OFF 0x100002 //關燈命令
static long led_ioctl(struct file *file,
unsigned int cmd,
unsigned long arg)
{
//定義內核緩衝區,保存用戶緩衝區的數據
int kindex;
//拷貝用戶數據到內核
copy_from_user(&kindex, (int *)arg, sizeof(kindex));
//解析處理用戶命令
switch(cmd) {
case LED_ON:
gpio_set_value(led_info[kindex-1].gpio, 1);
printk("%s:第%d個燈被打開!\n", __func__, kindex);
break;
case LED_OFF:
gpio_set_value(led_info[kindex-1].gpio, 0);
printk("%s:第%d個燈被關閉!\n", __func__, kindex);
break;
default:
return -1;
}
return 0; //成功返回0,失敗返回-1
}
//定義初始化硬件操作方法
static struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open, //打開設備
.release = led_close, //關閉設備
.read = led_read, //讀取設備
.write = led_write, //寫設備
.unlocked_ioctl = led_ioctl //向設備發送命令和完成讀寫
};
static int led_init(void)
{
int i;
//申請設備號
alloc_chrdev_region(&dev, 0, 1, "tarena");
//初始化字符設備對象
cdev_init(&led_cdev, &led_fops);
//註冊字符設備對象到內核
cdev_add(&led_cdev, dev, 1);
//申請GPIO資源和配置GPIO爲輸出口,輸出0(省電)
for (i = 0; i < ARRAY_SIZE(led_info); i++) {
gpio_request(led_info[i].gpio, led_info[i].name);
gpio_direction_output(led_info[i].gpio, 0);
}
return 0;
}
static void led_exit(void)
{
int i;
//輸出0,釋放GPIO資源
for (i = 0; i < ARRAY_SIZE(led_info); i++) {
gpio_set_value(led_info[i].gpio, 0);
gpio_free(led_info[i].gpio);
}`
//卸載字符設備對象
cdev_del(&led_cdev);
//釋放設備號
unregister_chrdev_region(dev, 1);
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
應用層代碼:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define LED_ON 0x100001
#define LED_OFF 0x100002
int main(void)
{
int fd;
int uindex; //用戶緩衝區
//打開設備
//open->....->調用led_open
fd = open("/dev/myled", O_RDWR);
if (fd < 0) {
printf("打開設備失敗!\n");
return -1;
}
//write->...->調用led_write
while (1) {
uindex = 1;
ioctl(fd, LED_ON, &uindex);
sleep(1);
uindex = 2;
ioctl(fd, LED_ON, &uindex);
sleep(1);
uindex = 1;
ioctl(fd, LED_OFF, &uindex);
sleep(1);
uindex = 2;
ioctl(fd, LED_OFF, &uindex);
sleep(1);
}
//關閉設備
//close->...->調用led_close
close(fd);
return 0;
}