xxxx18一60岁hd中国/日韩女同互慰一区二区/西西人体扒开双腿无遮挡/日韩欧美黄色一级片 - 色护士精品影院www

資源簡介

linux下采用模塊方法,添加一個(gè)新的設(shè)備驅(qū)動(dòng)程序。 要求添加字符設(shè)備的驅(qū)動(dòng)。 另附一個(gè)應(yīng)用程序,測試添加的驅(qū)動(dòng)程序

資源截圖

代碼片段和文件信息

#include?“l(fā)inux/kernel.h“
#include?“l(fā)inux/module.h“
#include?“l(fā)inux/fs.h“
#include?“l(fā)inux/init.h“
#include?“l(fā)inux/types.h“
#include?“l(fā)inux/errno.h“
#include?“l(fā)inux/uaccess.h“
#include?“l(fā)inux/kdev_t.h“
#define?MAX_SIZE?1024

static?int?my_open(struct?inode?*inode?struct?file?*file);
static?int?my_release(struct?inode?*inode?struct?file?*file);
static?ssize_t?my_read(struct?file?*file?char?__user?*user?size_t?t?loff_t?*f);
static?ssize_t?my_write(struct?file?*file?const?char?__user?*user?size_t?t?loff_t?*f);

static?char?message[MAX_SIZE]?=?“-------congratulations--------!“;
static?int?device_num?=?0;//設(shè)備號(hào)
static?int?counter?=?0;//計(jì)數(shù)用
static?int?mutex?=?0;//互斥用
static?char*?devName?=?“myDevice“;//設(shè)備名

struct?file_operations?pStruct?=
{?open:my_open?release:my_release?read:my_read?write:my_write?};

/*?注冊(cè)模塊?*/
int?init_module()
{
int?ret;
/*?函數(shù)中第一個(gè)參數(shù)是告訴系統(tǒng),新注冊(cè)的設(shè)備的主設(shè)備號(hào)由系統(tǒng)分配,
?*?第二個(gè)參數(shù)是新設(shè)備注冊(cè)時(shí)的設(shè)備名字,
?*?第三個(gè)參數(shù)是指向file_operations的指針,
?*?當(dāng)用設(shè)備號(hào)為0創(chuàng)建時(shí),系統(tǒng)一個(gè)可以用的設(shè)備號(hào)創(chuàng)建模塊?*/
ret?=?register_chrdev(0?devName?&pStruct);
if?(ret? {
printk(“regist?failure!\n“);
return?-1;
}
else
{
printk(“the?device?has?been?registered!\n“);
device_num?=?ret;
printk(“<1>the?virtual?device‘s?major?number?%d.\n“?device_num);
printk(“<1>Or?you?can?see?it?by?using\n“);
printk(“<1>------more?/proc/devices-------\n“);
printk(“<1>To?talk?to?the?drivercreate?a?dev?file?with\n“);
printk(“<1>------‘mknod?/dev/myDevice?c?%d?0‘-------\n“?device_num);
printk(“<1>Use?\“rmmode\“?to?remove?the?module\n“);

return?0;
}
}
/*?注銷模塊,函數(shù)名很特殊?*/
void?cleanup_module()
{
unregister_chrdev(device_num?devName);
printk(“unregister?it?success!\n“);
}

static?int?my_open(struct?inode?*inode?struct?file?*file)
{
????????if(mutex)
????????????????return?-EBUSY;
????????mutex?=?1;//上鎖
printk(“<1>main??device?:?%d\n“?MAJOR(inode->i_rdev));
printk(“<1>slave?device?:?%d\n“?MINOR(inode->i_rdev));
printk(“<1>%d?times?to?call?the?device\n“?++counter);
try_module_get(THIS_MODULE);
return?0;
}
/*?每次使用完后會(huì)release?*/
static?int?my_release(struct?inode?*inode?struct?file?*file)
{
printk(“Device?released!\n“);
module_put(THIS_MODULE);
????????mutex?=?0;//開鎖
return?0;
}

static?ssize_t?my_read(struct?file?*file?char?__user?*user?size_t?t?loff_t?*f)
{
if(copy_to_user(usermessagesizeof(message)))
{
return?-EFAULT;
}
return?sizeof(message);
}

static?ssize_t?my_write(struct?file?*file?const?char?__user?*user?size_t?t?loff_t?*f)
{
if(copy_from_user(messageusersizeof(message)))
{
return?-EFAULT;
}
return?sizeof(message);
}


評(píng)論

共有 條評(píng)論

相關(guān)資源