教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 专业资料 >

驱动程序编写实验

来源:网络收集 时间:2026-09-11
导读: 1、编写pc机下的驱动程序,并安装驱动2、编写应用程序调用驱动程序。 实验六 驱动程序编写实验 一、实验目的 1、掌握Linux下驱动程序开发过程 2、了解驱动程序框架 3、熟练使用gcc编译器 4、理解makefile文件作用 5、学会安装驱动程序,熟悉指令insmod,mknod

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字,全部文档内容请下载后查看。喜欢就下载吧 ……

驱动程序编写实验.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/1759923.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)