Linux下的第一个驱动程序
2013-11-15 15:32
337 查看
makefile文件:
[cpp] view
plaincopy
KERN_DIR = /home/youshan/linux-2.6.32.2
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += first_drv.o
驱动程序:
[cpp] view
plaincopy
/*************************************************
* File name : first_drv.c(可当做模板使用)
* Description : 参考韦东山视频,改写了部分代码
* Author : sg131971@qq.com
* Version : V1.0
* Date :
* Compiler : arm-linux-gcc-4.4.3
* Target : mini2440(Linux-2.6.32)
* History :
* <author> <time> <version > <desc>
*************************************************/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/device.h> /* device_create() */
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;
static int major;
static int first_drv_open(struct inode *inode, struct file *file)
{
printk("First_drv_open\n");
return 0;
}
static ssize_t first_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk("First_drv_read\n");
return 0;
}
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk("First_drv_write\n");
return 0;
}
static int first_drv_release(struct inode *inode, struct file *file)
{
printk("First_drv_release\n");
return 0;
}
static struct file_operations first_drv_fops = {
.owner = THIS_MODULE,
.open = first_drv_open,
.read = first_drv_read,
.write = first_drv_write,
.release = first_drv_release,
};
static int __init first_drv_init(void)
{
/* cat /proc/devices */
major = register_chrdev(0, "first_drv_proc", &first_drv_fops);
/* ls /sys/class/first_drv */
firstdrv_class = class_create(THIS_MODULE, "first_drv_sys");
if (IS_ERR(firstdrv_class))
return PTR_ERR(firstdrv_class);
/* ls /dev/first_drv */
firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "first_drv_dev");
if (unlikely(IS_ERR(firstdrv_class_dev)))
return PTR_ERR(firstdrv_class_dev);
return 0;
}
static void __exit first_drv_exit(void)
{
unregister_chrdev(major, "first_drv_proc");
device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
}
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_AUTHOR("WHUT-ShiGuang");
MODULE_DESCRIPTION("Mini2440 Test Driver");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");
测试程序:
[cpp] view
plaincopy
/*************************************************
* File name :
* Description :
* Author : sg131971@qq.com
* Version : V1.0
* Date :
* Compiler : arm-linux-gcc-4.4.3
* Target : mini2440(Linux-2.6.32)
* History :
* <author> <time> <version > <desc>
*************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int fd;
int val = 1;
fd = open("/dev/first_drv_dev", O_RDWR);
printf("fd = %d\n", fd);
write(fd, &val, 4);
read(fd, &val, 4);
close(fd);
return 0;
}
测试结果:
[root@FriendlyARM /home]#
[root@FriendlyARM /home]# ls /dev/first_drv_dev -l
crw-rw---- 1 root root 253, 0 Jan 1 08:31 /dev/first_drv_dev
[root@FriendlyARM /home]#
[root@FriendlyARM /home]# cat /proc/devices | grep first_drv
253 first_drv_proc
[root@FriendlyARM /home]#
[root@FriendlyARM /home]# ls /sys/class/first_drv_sys/
first_drv_dev
[root@FriendlyARM /home]#
[root@FriendlyARM /home]#
[root@FriendlyARM /home]# ./app
First_drv_open
fd = 3
First_drv_write
First_drv_read
First_drv_release
[root@FriendlyARM /home]#
[root@FriendlyARM /home]#
OVER! THANK YOU !
[cpp] view
plaincopy
KERN_DIR = /home/youshan/linux-2.6.32.2
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += first_drv.o
驱动程序:
[cpp] view
plaincopy
/*************************************************
* File name : first_drv.c(可当做模板使用)
* Description : 参考韦东山视频,改写了部分代码
* Author : sg131971@qq.com
* Version : V1.0
* Date :
* Compiler : arm-linux-gcc-4.4.3
* Target : mini2440(Linux-2.6.32)
* History :
* <author> <time> <version > <desc>
*************************************************/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/device.h> /* device_create() */
static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;
static int major;
static int first_drv_open(struct inode *inode, struct file *file)
{
printk("First_drv_open\n");
return 0;
}
static ssize_t first_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk("First_drv_read\n");
return 0;
}
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk("First_drv_write\n");
return 0;
}
static int first_drv_release(struct inode *inode, struct file *file)
{
printk("First_drv_release\n");
return 0;
}
static struct file_operations first_drv_fops = {
.owner = THIS_MODULE,
.open = first_drv_open,
.read = first_drv_read,
.write = first_drv_write,
.release = first_drv_release,
};
static int __init first_drv_init(void)
{
/* cat /proc/devices */
major = register_chrdev(0, "first_drv_proc", &first_drv_fops);
/* ls /sys/class/first_drv */
firstdrv_class = class_create(THIS_MODULE, "first_drv_sys");
if (IS_ERR(firstdrv_class))
return PTR_ERR(firstdrv_class);
/* ls /dev/first_drv */
firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "first_drv_dev");
if (unlikely(IS_ERR(firstdrv_class_dev)))
return PTR_ERR(firstdrv_class_dev);
return 0;
}
static void __exit first_drv_exit(void)
{
unregister_chrdev(major, "first_drv_proc");
device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
}
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_AUTHOR("WHUT-ShiGuang");
MODULE_DESCRIPTION("Mini2440 Test Driver");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");
测试程序:
[cpp] view
plaincopy
/*************************************************
* File name :
* Description :
* Author : sg131971@qq.com
* Version : V1.0
* Date :
* Compiler : arm-linux-gcc-4.4.3
* Target : mini2440(Linux-2.6.32)
* History :
* <author> <time> <version > <desc>
*************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int fd;
int val = 1;
fd = open("/dev/first_drv_dev", O_RDWR);
printf("fd = %d\n", fd);
write(fd, &val, 4);
read(fd, &val, 4);
close(fd);
return 0;
}
测试结果:
[root@FriendlyARM /home]#
[root@FriendlyARM /home]# ls /dev/first_drv_dev -l
crw-rw---- 1 root root 253, 0 Jan 1 08:31 /dev/first_drv_dev
[root@FriendlyARM /home]#
[root@FriendlyARM /home]# cat /proc/devices | grep first_drv
253 first_drv_proc
[root@FriendlyARM /home]#
[root@FriendlyARM /home]# ls /sys/class/first_drv_sys/
first_drv_dev
[root@FriendlyARM /home]#
[root@FriendlyARM /home]#
[root@FriendlyARM /home]# ./app
First_drv_open
fd = 3
First_drv_write
First_drv_read
First_drv_release
[root@FriendlyARM /home]#
[root@FriendlyARM /home]#
OVER! THANK YOU !
相关文章推荐
- 第一个Linux驱动程序
- 第二章 第二节 Linux设备驱动程序之--我的第一个程序 Hello World
- 【Linux驱动】第一个驱动程序
- 第一个嵌入式Linux的驱动程序
- Linux驱动开发学习 第一个驱动程序 hello world
- 第一个Linux内核驱动程序
- 第一个linux驱动程序
- Linux下第一个驱动程序
- LINUX2.6.26.6内核下的第一个LED驱动程序测试成功!
- 手把手教你写第一个Linux驱动程序
- 手把手教你写第一个Linux驱动程序
- 第一个LINUX下的驱动程序
- Linux行走(1)——第一个驱动程序
- 我的第一个Linux开发板字符驱动程序
- linux驱动由浅入深系列:驱动程序的基本结构概览之一(第一个驱动程序)
- Tiny6410下的第一个Linux驱动程序
- linux驱动学习(2)-第一个驱动程序hello world
- 嵌入式Linux驱动学习笔记(一)------第一个LED驱动程序
- 开发板上使用GPIO向电磁继电器发送高低电平控制台灯亮灭(我的第一个linux驱动程序开发)
- linux下的第一个驱动程序 hello (for 2.4)