歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux2.6字符設備驅動開發模板

linux2.6字符設備驅動開發模板

日期:2017/3/3 12:13:49   编辑:Linux技術

linux2.6字符設備驅動開發模板

[cpp] view
plain copy





#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
//=======================字符設備驅動模板開始 ===========================//
#define CHAR_DEV_DEVICE_NAME "char_dev" // 是應當連接到這個編號范圍的設備的名字,出現在/proc/devices和sysfs中
#define CHAR_DEV_NODE_NAME "char_dev" // 節點名,出現在/dev中
#define CHAR_DEV_CLASS_NAME "char_dev_class" //出現在/sys/devices/virtual/和/sys/class/中
struct class *char_dev_class; // class結構用於自動創建設備結點
static int major = 0;<span > </span> // 0表示動態分配主設備號,可以設置成未被系統分配的具體的數字。
static struct cdev char_dev_cdev;// 定義一個cdev結構
// 進行初始化設置,打開設備,對應應用空間的open 系統調用
int char_dev_open(struct inode *inode, struct file *filp)
{
// 這裡可以進行一些初始化
printk("char_dev device open.\n");
return 0;
}
// 釋放設備,關閉設備,對應應用空間的close 系統調用
static int char_dev_release (struct inode *node, struct file *file)
{
// 這裡可以進行一些資源的釋放
printk("char_dev device release.\n");
return 0;
}
// 實現讀功能,讀設備,對應應用空間的read 系統調用
/*__user. 這種注解是一種文檔形式, 注意, 一個指針是一個不能被直接解引用的
用戶空間地址. 對於正常的編譯, __user 沒有效果, 但是它可被外部檢查軟件使
用來找出對用戶空間地址的錯誤使用.*/
ssize_t char_dev_read(struct file *file,char __user *buff,size_t count,loff_t *offp)
{
printk("char_dev device read.\n");
return 0;
}
// 實現寫功能,寫設備,對應應用空間的write 系統調用
ssize_t char_dev_write(struct file *file,const char __user *buff,size_t count,loff_t *offp)
{
printk("char_dev device write.\n");
return 0;
}
// 實現主要控制功能,控制設備,對應應用空間的ioctl系統調用
static int char_dev_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)
{
printk("char_dev device ioctl.\n");
return 0;
}
// file_operations 結構體設置,該設備的所有對外接口在這裡明確,此處只寫出了幾常用的
static struct file_operations char_dev_fops =
{
.owner = THIS_MODULE,
.open = char_dev_open, // 打開設備
.release = char_dev_release, // 關閉設備
.read = char_dev_read, // 實現設備讀功能
.write = char_dev_write, // 實現設備寫功能
.ioctl = char_dev_ioctl, // 實現設備控制功能
};
// 設備建立子函數,被char_dev_init函數調用
static void char_dev_setup_cdev(struct cdev *dev, int minor, struct file_operations *fops)
{
int err, devno = MKDEV(major, minor);
cdev_init(dev, fops);//對cdev結構體進行初始化
dev->owner = THIS_MODULE;
dev->ops = fops;
err = cdev_add(dev, devno, 1);//參數1是應當關聯到設備的設備號的數目. 常常是1
if(err)
{
printk(KERN_NOTICE "Error %d adding char_dev %d.\n", err, minor);
}
printk("char_dev device setup.\n");
}
// 設備初始化
static int char_dev_init(void)
{
int result;
dev_t dev = MKDEV(major, 0);//將主次編號轉換為一個dev_t類型
if(major)
{
// 給定設備號注冊
result = register_chrdev_region(dev, 1, CHAR_DEV_DEVICE_NAME);//1是你請求的連續設備編號的總數
printk("char_dev register_chrdev_region.\n");
}
else
{
// 動態分配設備號
result = alloc_chrdev_region(&dev, 0, 1, CHAR_DEV_DEVICE_NAME);//0是請求的第一個要用的次編號,它常常是 0
printk("char_dev alloc_chrdev_region.\n");
major = MAJOR(dev);
}
if(result < 0)//獲取設備號失敗返回
{
printk(KERN_WARNING "char_dev region fail.\n");
return result;
}
char_dev_setup_cdev(&char_dev_cdev, 0, &char_dev_fops);
printk("The major of the char_dev device is %d.\n", major);
//==== 有中斷的可以在此注冊中斷:request_irq,並要實現中斷服務程序 ===//
// 創建設備節點
char_dev_class = class_create(THIS_MODULE,CHAR_DEV_CLASS_NAME);
if (IS_ERR(char_dev_class))
{
printk("Err: failed in creating char_dev class.\n");
return 0;
}
device_create(char_dev_class, NULL, dev, NULL, CHAR_DEV_NODE_NAME);
printk("char_dev device installed.\n");
return 0;
}
// 設備注銷
static void char_dev_cleanup(void)
{
device_destroy(char_dev_class,MKDEV(major, 0));
class_destroy(char_dev_class);
cdev_del(&char_dev_cdev);//字符設備的注銷
unregister_chrdev_region(MKDEV(major, 0), 1);//設備號的注銷
//======== 有中斷的可以在此注銷中斷:free_irq ======//
printk("char_dev device uninstalled.\n");
}
module_init(char_dev_init);//模塊初始化接口
module_exit(char_dev_cleanup);//模塊注銷接口
//所有模塊代碼都應該指定所使用的許可證,該句不能省略,否則模塊加載會報錯
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Author");
MODULE_DESCRIPTION("Driver Description");

Makefile

[cpp] view
plain copy





#
# Makefile for kernel helloworld drivers
#
# If KERNELRELEASE is defined, we've been invoked from the
# kernel build system and can use its language.
ifneq ($(KERNELRELEASE),)
obj-m += chardev.o
# Otherwise we were called directly from the command
# line; invoke the kernel build system.
else
KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build
ANDROID_KERNEL_DIR ?= /home/wzf/amlogic_m1_0427/kernel/
PWD := $(shell pwd)
default:
$(MAKE) -C $(ANDROID_KERNEL_DIR) M=$(PWD) modules
modules:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean
endif
Copyright © Linux教程網 All Rights Reserved