網(wǎng)站如何做標(biāo)題優(yōu)化網(wǎng)站備案查詢官網(wǎng)
1、新字符設(shè)備驅(qū)動原理
使用 register_chrdev 函數(shù)注冊字符設(shè)備的時候只需要給定一個主設(shè)備號即可,但是這樣會
帶來兩個問題
- 需要我們事先確定好哪些主設(shè)備號沒有使用
-
會將一個主設(shè)備號下的所有次設(shè)備號都使用掉,比如現(xiàn)在設(shè)置 LED 這個主設(shè)備號為200,那么 0~1048575(2^20-1)?這個區(qū)間的次設(shè)備號就全部都被 LED 一個設(shè)備分走了。這樣太浪費次設(shè)備號了!一個 LED 設(shè)備肯定只能有一個主設(shè)備號,一個次設(shè)
舊字符驅(qū)動模式,如下所示:
新字符驅(qū)動模型,如下所示:
2、分配和釋放設(shè)備號
解決這兩個問題最好的方法就是要使用設(shè)備號的時候向 Linux 內(nèi)核申請,需要幾個就申請
幾個,由 Linux 內(nèi)核分配設(shè)備可以使用的設(shè)備號。
如果沒有指定設(shè)備號的話就使用如下函數(shù)來申請設(shè)備號:
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
如果給定了設(shè)備的主設(shè)備號和次設(shè)備號就使用如下所示函數(shù)來注冊設(shè)備號即可
int register_chrdev_region(dev_t from, unsigned count, const char *name)
注銷字符設(shè)備之后要釋放掉設(shè)備號 ,不管是通過 alloc_chrdev_region 函數(shù)還是
register_chrdev_region 函數(shù)申請的設(shè)備號,統(tǒng)一使用如下釋放函數(shù):
void unregister_chrdev_region(dev_t from, unsigned count)
綜上所述,新字符設(shè)備驅(qū)動下,設(shè)備號分配示例代碼如下:
int major; /* 主設(shè)備號 */int minor; /* 次設(shè)備號 */dev_t devid; /* 設(shè)備號 */if (major) { /* 定義了主設(shè)備號 */devid = MKDEV(major, 0); /* 大部分驅(qū)動次設(shè)備號都選擇 0*/register_chrdev_region(devid, 1, "test");} else { /* 沒有定義設(shè)備號 */alloc_chrdev_region(&devid, 0, 1, "test"); /* 申請設(shè)備號 */major = MAJOR(devid); /* 獲取分配號的主設(shè)備號 */minor = MINOR(devid); /* 獲取分配號的次設(shè)備號 */
}
注銷設(shè)備很簡單,因為我們的CNT是1嘛,如下所示:
unregister_chrdev_region(devid, 1);
3、新的字符設(shè)備注冊方法(cdev)
其中我們要涉及用到cdev,在 Linux 中使用 cdev 結(jié)構(gòu)體表示一個字符設(shè)備,cdev 結(jié)構(gòu)體在 include/linux/cdev.h 文件中的定義如下:
struct cdev {struct kobject kobj;struct module *owner;const struct file_operations *ops;struct list_head list;dev_t dev;unsigned int count;
};
在 cdev 中有兩個重要的成員變量: ops 和 dev ,這兩個就是字符設(shè)備文件操作函數(shù)集合
file_operations 以及設(shè)備號 dev_t 。編寫字符設(shè)備驅(qū)動之前需要定義一個 cdev 結(jié)構(gòu)體變量,這個
變量就表示一個字符設(shè)備,如下所示:
struct cdev test_cdev;
參數(shù) cdev 就是要初始化的 cdev 結(jié)構(gòu)體變量,參數(shù) fops 就是字符設(shè)備文件操作函數(shù)集合。
使用 cdev_init 函數(shù)初始化 cdev 變量和向 Linux 系統(tǒng)添加字符設(shè)備 (cdev 結(jié)構(gòu)體變量)的示例代碼如下:
struct cdev testcdev;/* 設(shè)備操作函數(shù) */static struct file_operations test_fops = {.owner = THIS_MODULE,/* 其他具體的初始項 */
};testcdev.owner = THIS_MODULE;cdev_init(&testcdev, &test_fops); /* 初始化 cdev 結(jié)構(gòu)體變量 */cdev_add(&testcdev, devid, 1); /* 添加字符設(shè)備 */
卸載刪除驅(qū)動時如下所示:
cdev_del(&testcdev); /* 刪除 cdev */
4、創(chuàng)建以及刪除類和設(shè)備(class、device)
struct class *class; /* 類 */ struct device *device; /* 設(shè)備 */dev_t devid; /* 設(shè)備號 */ /* 驅(qū)動入口函數(shù) */static int __init led_init(void){/* 創(chuàng)建類 */class = class_create(THIS_MODULE, "xxx");/* 創(chuàng)建設(shè)備 */device = device_create(class, NULL, devid, NULL, "xxx");return 0;}/* 驅(qū)動出口函數(shù) */static void __exit led_exit(void){/* 刪除設(shè)備 */device_destroy(newchrled.class, newchrled.devid);/* 刪除類 */class_destroy(newchrled.class);}module_init(led_init);module_exit(led_exit);
整體案例代碼如下所示:
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>#define NEWCHRLED_CHT 1 /* 設(shè)備號個數(shù) */
#define NEWCHRLED_NAME "newchrled" /* 設(shè)備名字 */
#define LEDOFF 0 /* 關(guān)燈 */
#define LEDON 1 /* 開燈 */#define CCM_CCGR1_BASE (0X020C406C)
#define SW_MUX_GPIO1_IO03_BASE (0X020E0068)
#define SW_PAD_GPIO1_IO03_BASE (0X020E02F4)
#define GPIO1_DR_BASE (0X0209C000)
#define GPIO1_GDIR_BASE (0X0209C004)static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;struct newchrled_dev{dev_t devid; /* 設(shè)備號 */struct cdev cdev; /* cdev */struct class *class; /* 類 */struct device *device; /* 設(shè)備 */int major; /* 主設(shè)備號*/int minor; /* 次設(shè)備號 */
}struct newchrled_dev newchrled; /* led設(shè)備 *//** @description : LED 打開/關(guān)閉* @param - sta : LEDON(0) 打開 LED,LEDOFF(1) 關(guān)閉 LED* @return : 無*/
void led_switch(u8 sta)
{u32 val = 0;if(sta == LEDON) {val = readl(GPIO1_DR);val &= ~(1 << 3); writel(val, GPIO1_DR);}else if(sta == LEDOFF) {val = readl(GPIO1_DR);val |= (1 << 3);writel(val, GPIO1_DR);}
}static int led_open (struct inode *inodp, struct file *filp)
{filp->private_data = &newchrled; return 0;
}static ssize_t led_read (struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{return 0;
}/*
* @description : 向設(shè)備寫數(shù)據(jù)
* @param - filp : 設(shè)備文件,表示打開的文件描述符
* @param - buf : 要寫給設(shè)備寫入的數(shù)據(jù)
* @param - cnt : 要寫入的數(shù)據(jù)長度
* @param - offt : 相對于文件首地址的偏移
* @return : 寫入的字節(jié)數(shù),如果為負(fù)值,表示寫入失敗
*/
static ssize_t led_write (struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{int retvalue = 0;unsigned char databuf[1];unsigned char ledstat;retvalue = copy_from_user(databuf,buf,cnt);if(retvalue < 0){printk("kernel write failed\r\n");return -EFAULT;}ledstat = databuf[0];if(ledstat == LEDON){led_switch(LEDON);}else if(ledstat == LEDOFF){led_switch(LEDOFF);}return 0;
}/** @description : 關(guān)閉/釋放設(shè)備* @param - filp : 要關(guān)閉的設(shè)備文件(文件描述符)* @return : 0 成功;其他 失敗*/
static int led_release (struct inode *inodp, struct file *filp)
{return 0;
}static struct file_operations newchrled_fops = {.owner = THIS_MODULE,.open = led_open,.read = led_read,.write = led_write,.release = led_release,
};static int __init led_init(void){int retvalue = 0;u32 val = 0;IMX6U_CCM_CCGR1 = ioremap(CCM_CCGR1_BASE,4);SW_MUX_GPIO1_IO03 = ioremap(SW_MUX_GPIO1_IO03_BASE,4);SW_PAD_GPIO1_IO03 = ioremap(SW_PAD_GPIO1_IO03_BASE,4);GPIO1_DR = ioremap(GPIO1_DR_BASE,4);GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE,4);/* 2、使能 GPIO1 時鐘 */val = readl(IMX6U_CCM_CCGR1);val &= ~(3 << 26); /* 清除以前的設(shè)置 */val |= (3 << 26); /* 設(shè)置新值 */writel(val, IMX6U_CCM_CCGR1);/* 3、設(shè)置 GPIO1_IO03 的復(fù)用功能,將其復(fù)用為* GPIO1_IO03,最后設(shè)置 IO 屬性。*/writel(5, SW_MUX_GPIO1_IO03);/* 寄存器 SW_PAD_GPIO1_IO03 設(shè)置 IO 屬性 */writel(0x10B0, SW_PAD_GPIO1_IO03);/* 4、設(shè)置 GPIO1_IO03 為輸出功能 */val = readl(GPIO1_GDIR);val &= ~(1 << 3); /* 清除以前的設(shè)置 */val |= (1 << 3); /* 設(shè)置為輸出 */writel(val, GPIO1_GDIR);/* 5、默認(rèn)關(guān)閉 LED */val = readl(GPIO1_DR);val |= (1 << 3); writel(val, GPIO1_DR);/* //設(shè)備號 名字 字符模型驅(qū)動的一個結(jié)構(gòu)體retvalue = register_chrdev(LED_MAJOR,LED_NAME,&newchrled_fops);if(retvalue < 0){// exit} *//* 注冊字符設(shè)備驅(qū)動 *//* 1、創(chuàng)建設(shè)備號 */if(newchrled.major){newchrled.devid = MKDEV(newchrled.major, 0);register_chrdev_region(newchrled.devid, NEWCHRLED_CHT, NEWCHRLED_NAME);} else {alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_CHT, NEWCHRLED_NAME);newchrled.major = MAJOR(newchrled.devid);newchrled.minor = MINOR(newchrled.devid);}printk("newchrled major = %d,minor = %d \r\n",newchrled.major,newchrled.minor);/* 2、初始化 cdev */newchrled.cdev.owner = THIS_MODULE;cdev_init(&newchrled.cdev,&newchrled_fops);/* 3、添加一個 cdev */cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_CHT);/* 4、創(chuàng)建類 */newchrled.class = class_create(THIS_MODULE, NEWCHRLED_NAME);if(IS_ERR(newchrled.class)){return PTR_ERR(newchrled.class);}/* 5、創(chuàng)建設(shè)備 */newchrled.device = device_create(newchrled.class, NULL, newchrled.devid, NULL, NEWCHRLED_NAME);if(IS_ERR(newchrled.device)){return PTR_ERR(newchrled.device);}return 0;
}static void __exit led_exit(void){iounmap(IMX6U_CCM_CCGR1);iounmap(SW_MUX_GPIO1_IO03);iounmap(SW_PAD_GPIO1_IO03);iounmap(GPIO1_DR);iounmap(GPIO1_GDIR);/* unregister_chrdev(LED_MAJOR,LED_NAME); *//* 注銷字符設(shè)備 */cdev_del(&newchrled.cdev);unregister_chrdev_region(newchrled.devid, NEWCHRLED_CHT);device_destroy(newchrled.class,newchrled.devid);class_destroy(newchrled.class);
}module_init(led_init);
module_exit(led_exit);MODULE_LICENSE("GPL");
MODULE_AUTHOR("7yewh");