驱动程序编写实验
1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。
实验六 驱动程序编写实验
一、实验目的
1、掌握Linux下驱动程序开发过程
2、了解驱动程序框架
3、熟练使用gcc编译器
4、理解makefile文件作用
5、学会安装驱动程序,熟悉指令insmod,mknod,rmmod。
二、实验设备
1、硬件:PC机。
2、软件:vmware虚拟机,win7/XP
三、实验内容
1、编写pc机下的驱动程序,并安装驱动
2、编写应用程序调用驱动程序。
四、程序代码
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/semaphore.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/string.h>
#include "hello_mod_ioctl.h"
#define MAJOR_NUM 250
1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。
#define MINOR_NUM 0
#define IN_BUF_LEN 256
#define OUT_BUF_LEN 512
MODULE_AUTHOR("Tishion");
MODULE_DESCRIPTION("Hello_mod driver by tishion");
static struct class * hello_class;
static struct cdev hello_cdev;
static dev_t devnum = 0;
static char * modname = "hello_mod";
static char * devicename = "hello";
static char * classname = "hello_class";
static int open_count = 0;
static struct semaphore sem;
static spinlock_t spin = SPIN_LOCK_UNLOCKED;
static char * inbuffer = NULL;
static char * outbuffer = NULL;
static lang_t langtype;
static int hello_mod_open(struct inode *, struct file *);
1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。
static int hello_mod_release(struct inode *, struct file *);
static ssize_t hello_mod_read(struct file *, char *, size_t, loff_t *);
static ssize_t hello_mod_write(struct file *, const char *, size_t, loff_t *);
static int hello_mod_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
struct file_operations hello_mod_fops =
{
.owner = THIS_MODULE,
.open = hello_mod_open,
.read = hello_mod_read,
.write = hello_mod_write,
.ioctl = hello_mod_ioctl,
.release = hello_mod_release,
};
static int hello_mod_open(struct inode *inode, struct file *pfile)
{
printk("+hello_mod_open()!/n");
spin_lock(&spin);
if(open_count)
{
spin_unlock(&spin);
1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。
return -EBUSY;
}
open_count++;
spin_unlock(&spin);
printk("-hello_mod_open()!/n");
return 0;
}
static int hello_mod_release(struct inode *inode, struct file *pfile)
{
printk("+hello_mod_release()!/n");
open_count--;
printk("-hello_mod_release()!/n");
return 0;
}
static ssize_t hello_mod_read(struct file *pfile, char *user_buf, size_t len, loff_t *off)
{
printk("+hello_mod_read()!/n");
if(down_interruptible(&sem))
{
return -ERESTARTSYS;
1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。
}
memset(outbuffer, 0, OUT_BUF_LEN);
printk(" +switch()/n");
switch(langtype)
{
case english:
printk(" >in case: english/n");
sprintf(outbuffer, "Hello! %s.", inbuffer);
break;
case chinese:
printk(" >in case: chinese/n");
sprintf(outbuffer, "你好! %s.", inbuffer);
break;
case pinyin:
printk(" >in case: pinyin/n");
sprintf(outbuffer, "ni hao! %s.", inbuffer);
break;
default:
printk(" >in case: default/n");
break;
}
printk(" -switch()/n");
1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。
{
up(&sem);
return -EFAULT;
}
up(&sem);
printk("-hello_mod_read()!/n");
return 0;
}
static ssize_t hello_mod_write(struct file *pfile, const char *user_buf, size_t len, loff_t *off)
{
printk("+hello_mod_write()!/n");
if(down_interruptible(&sem))
{
return -ERESTARTSYS;
}
if(len > IN_BUF_LEN)
{
printk("Out of input buffer/n");
return -ERESTARTSYS;
}
1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。
{
up(&sem);
return -EFAULT;
}
up(&sem);
printk("-hello_mod_write()!/n");
return 0;
}
static int hello_mod_ioctl(struct inode *inode, struct file *pfile, unsigned int cmd, unsigned long arg)
{
int err = 0;
printk("+hello_mod_ioctl()!/n");
printk(" +switch()/n");
switch(cmd)
{
case HELLO_IOCTL_RESETLANG:
printk(" >in case: HELLO_IOCTL_RESETLANG/n");
langtype = english;
break;
case HELLO_IOCTL_GETLANG:
1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。
printk(" >in case: HELLO_IOCTL_GETLANG/n");
err = copy_to_user((int *)arg, &langtype, sizeof(int));
break;
case HELLO_IOCTL_SETLANG:
printk(" >in case: HELLO_IOCTL_SETLANG/n");
err = copy_from_user(&langtype,(int *)arg, sizeof(int));
break;
default:
printk(" >in case: default/n"); …… 此处隐藏:6057字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [专业资料]《蜜蜂之家》教学反思
- [专业资料]过去分词作定语和表语1
- [专业资料]苏州工业园区住房公积金贷款申请表
- [专业资料]保安管理制度及处罚条例细则
- [专业资料]2018年中国工程咨询市场发展现状调研及
- [专业资料]2015年电大本科《学前教育科研方法》期
- [专业资料]数字信号处理实验 matlab版 离散傅里叶
- [专业资料]“十三五”重点项目-虎杖白藜芦醇及功
- [专业资料]2015-2020年中国竹木工艺市场需求及投
- [专业资料]国际贸易理论与实务作业五:理论案例分
- [专业资料]财政部修订发布事业单位会计制度
- [专业资料]BCA蛋白浓度测定试剂盒(增强型)
- [专业资料]工程进度总计划横道图模板(通用版)
- [专业资料]七年级地理同步练习(天气与气候)
- [专业资料]X光安检机介绍火灾自动报警系统的组成
- [专业资料]衢州市人民政府办公室关于印发衢州市区
- [专业资料]经济全球化及其影响[1]
- [专业资料]质粒DNA限制性酶切图谱分析
- [专业资料]国家安全人民防线工作“六项”制度
- [专业资料]劳动力投入计划及保证措施
- 电子账册联网监管培训手册
- 人教版语文七年级上第1课《在山的那边
- 对我区担保行业发展现状的思考与建议
- 平面四边形网格自动生成方法研究
- 2016年党课学习心得体会范文
- 如何设置电脑定时关机
- 全球最美人妖排行榜新鲜出炉
- 社会实践调查报告及问卷
- Visual Basic习题集
- 《鱼我所欲也》课件2
- 浙江省会计从业资格考试试卷
- 全遥控数字音量控制的D 类功率放大器资
- 鞍钢宪法与后福特主义
- 电表的改装与校准实验报告(1)
- 2014年高考理科数学真题解析分类汇编:
- Windows 7 AIK 的使用
- 风电场全场停电事故应急处置方案
- 化工原理选填题题库(下)
- 关于产学研合作教育模式的学习与思考
- 西安先锋公馆项目前期定位报告




