【发布时间】:2012-03-29 09:54:03
【问题描述】:
我正在制作一个简单的字符驱动程序,假设写入我的字符设备“/dev/coffee_bean”,当读取时,它应该显示字符串“Hi There!”在控制台中。我通过“cat /dev/coffee_bean”从设备读取,而不是我的系统崩溃并重置。贝娄是我的源代码。感谢您的帮助。
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/types.h>
#include <linux/completion.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include <linux/semaphore.h>
MODULE_LICENSE("Dual BSD/GPL");
#define DEVICE_NAME "coffee_grinds"
#define COUNT 4
#define FIRST_MINOR 0
#define CONST_QUANTUM 4000
#define CONST_QSET 4000
int test;
module_param(test, int, S_IRUGO);
struct my_char_structure{
struct cdev my_cdev;
struct semaphore sem;
unsigned int access_key;
unsigned long size;
};
static dev_t dev_num;
int dev_open(struct inode *in_node, struct file *filp){
struct my_char_structure *my_dev;
my_dev = container_of(in_node->i_cdev, struct my_char_structure, my_cdev);
filp->private_data = my_dev;
return 0;
}
int dev_release(struct inode *inode, struct file *filp){
return 0;
}
ssize_t dev_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp){
struct my_char_structure *my_dev = filp->private_data;
ssize_t retval = -ENOMEM; /* value used in "goto out" statements */
char *my_string;
int counting;
printk(KERN_ALERT "Write was accessed, Lol");
if (down_interruptible(&my_dev->sem))
return -ERESTARTSYS;
my_string = kmalloc(count,GFP_KERNEL);
counting = copy_from_user(my_string,buff,count);
printk(KERN_ALERT "You wrote %s",my_string);
kfree(my_string);
up(&my_dev->sem);
printk(KERN_ALERT "We wrote %d bytes",counting);
return retval;
// Here is some experimental code
}
ssize_t dev_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){
struct my_char_structure *my_dev = filp->private_data;
ssize_t retval = 0;
char *my_string;
printk(KERN_ALERT "Read was accessed Lol");
if (down_interruptible(&my_dev->sem))
return -ERESTARTSYS;
my_string = "Hi there!";
copy_to_user(buff,my_string,10);
up(&my_dev->sem);
return retval;
}
struct file_operations fops = {
.owner = THIS_MODULE,
.read = dev_read,
.write = dev_write,
.open = dev_open,
.release= dev_release,
};
int start_mod(void){
//Because we are dealing with a fictitious device, I want
//the driver to create my two devices with arbitrarly
//assigned major numbers.
static struct my_char_structure Dev;
static struct my_char_structure *my_dev = &Dev;
int err;
alloc_chrdev_region(&dev_num, FIRST_MINOR, COUNT, DEVICE_NAME);
sema_init(&(my_dev->sem),1);
cdev_init(&(my_dev->my_cdev), &fops);
my_dev->my_cdev.owner = THIS_MODULE;
my_dev->my_cdev.ops = &fops;// fops is my file operations struct
err = cdev_add(&my_dev->my_cdev, dev_num, COUNT);
if(err)
printk(KERN_ALERT "There was an error %d.",err);
printk(KERN_ALERT " insmod to major number %d",MAJOR(dev_num));
return 0;
}
void end_mod(void){
unregister_chrdev_region(dev_num, COUNT);
}
module_init(start_mod);
module_exit(end_mod);
【问题讨论】:
标签: c linux kernel driver device