创建字符设备
来源:互联网 发布:怎么恢复卡通农场数据 编辑:程序博客网 时间:2024/06/07 03:01
将创建字符设备的三种方法记录一下,以便以后参考.
1.使用早期的register_chardev()方法
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<asm/uaccess.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<asm/uaccess.h>
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
static int major;
static int Device_open = 0;
static char msg[BUF_LEN];
static char *msg_ptr;
static int Device_open = 0;
static char msg[BUF_LEN];
static char *msg_ptr;
static struct file_operations fops =
{
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release,
};
{
};
int init_module(void)
{
major = register_chrdev(0, DEVICE_NAME, &fops);
if (major < 0)
{
printk(KERN_ALERT "Registering char device failed with %d\n", major);
return major;
}
printk(KERN_INFO "I was assigned major number %d.\n", major);
return SUCCESS;
}
{
}
void cleanup_module(void)
{
int ret = unregister_chrdev(major, DEVICE_NAME);
if (ret < 0)
{
printk(KERN_ALERT "Error in unregister chrdev %d\n", major);
}
}
{
}
static int device_open(struct inode* inode, struct file* file)
{
static int counter = 0;
if (Device_open)
{
return -EBUSY;
}
Device_open++;
sprintf(msg, "I already told you %d times hello world\nDevice_open=%d\n" , counter++, Device_open);
msg_ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
{
}
static int device_release(struct inode* inode, struct file* file)
{
Device_open--;
module_put(THIS_MODULE);
return 0;
}
{
}
static ssize_t device_read(struct file* filp, char *buffer, size_t length, loff_t *offset)
{
int bytes_read = 0;
if (*msg_ptr == '\0')
return 0;
printk(KERN_ALERT "length=%d\n", length);
while (length && *msg_ptr)
{
put_user(*(msg_ptr++), buffer++);
length--;
bytes_read++;
}
return bytes_read;
}
{
}
static ssize_t device_write(struct file* filp, const char *buff, size_t len, loff_t *off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported\n");
return -EINVAL;
}
{
}
2.使用cdev的方法
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/fs.h>
#include<linux/types.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<asm/uaccess.h>
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
static int major;
static int Device_open = 0;
static char msg[BUF_LEN];
static char *msg_ptr;
static struct cdev *my_cdev;
static struct file_operations fops =
{
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release,
};
{
};
int init_module(void)
{
int err;
dev_t devid ;
alloc_chrdev_region(&devid, 0, 1, "chardev");
major = MAJOR(devid);
my_cdev = cdev_alloc();
cdev_init(my_cdev, &fops);
err = cdev_add(my_cdev, devid, 1);
if (err)
{
printk(KERN_INFO "I was assigned major number %d.\n", major);
return -1;
}
printk("major number is %d\n", MAJOR(devid));
return SUCCESS;
}
{
}
void cleanup_module(void)
{
cdev_del(my_cdev);
printk("cleanup done\n");
}
{
}
static int device_open(struct inode* inode, struct file* file)
{
static int counter = 0;
if (Device_open)
{
return -EBUSY;
}
Device_open++;
sprintf(msg, "I already told you %d times hello world\nDevice_open=%d\n", counter++, Device_open);
msg_ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
{
}
static int device_release(struct inode* inode, struct file* file)
{
Device_open--;
module_put(THIS_MODULE);
return 0;
}
{
}
static ssize_t device_read(struct file* filp, char *buffer, size_t length, loff_t *offset)
{
int bytes_read = 0;
if (*msg_ptr == '\0')
return 0;
printk(KERN_ALERT "length=%d\n", length);
while (length && *msg_ptr)
{
put_user(*(msg_ptr++), buffer++);
length--;
bytes_read++;
}
return bytes_read;
}
{
}
static ssize_t device_write(struct file* filp, const char *buff, size_t len, loff_t *off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported\n");
return -EINVAL;
}
{
}
3.使用udev在/dev/下动态生成设备文件的方式
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/types.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<linux/pci.h>
#include<linux/moduleparam.h>
#include<linux/init.h>
#include<linux/string.h>
#include<asm/uaccess.h>
#include<asm/unistd.h>
#include<asm/uaccess.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/types.h>
#include<linux/fs.h>
#include<linux/cdev.h>
#include<linux/pci.h>
#include<linux/moduleparam.h>
#include<linux/init.h>
#include<linux/string.h>
#include<asm/uaccess.h>
#include<asm/unistd.h>
#include<asm/uaccess.h>
MODULE_LICENSE("GPL");
int init_module(void);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
void cleanup_module(void);
static int device_open(struct inode*, struct file*);
static int device_release(struct inode*, struct file*);
static ssize_t device_read(struct file*, char *, size_t, loff_t*);
static ssize_t device_write(struct file*, const char*, size_t, loff_t*);
#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
static int major;
static int Device_open = 0;
static char msg[BUF_LEN];
static char *msg_ptr;
static struct cdev *my_cdev;
static struct class *my_class;
dev_t devid ;
static struct file_operations fops =
{
.read = device_read,
.write = device_write,
.open = device_open,
.release = device_release,
};
{
};
int init_module(void)
{
int err;
alloc_chrdev_region(&devid, 0, 1, "chardev");
major = MAJOR(devid);
my_cdev = cdev_alloc();
cdev_init(my_cdev, &fops);
my_cdev->owner = THIS_MODULE;
err = cdev_add(my_cdev, devid, 1);
if (err)
{
printk(KERN_INFO "I was assigned major number %d.\n", major);
return -1;
}
my_class = class_create(THIS_MODULE, "chardev_class1");
if (IS_ERR(my_class))
{
printk(KERN_INFO "create class error\n");
return -1;
}
class_device_create(my_class, NULL, devid, NULL, "chardev" "%d", MINOR(devid));
printk("major number is %d\n", MAJOR(devid));
return SUCCESS;
}
{
}
void cleanup_module(void)
{
cdev_del(my_cdev);
class_device_destroy(my_class, devid);
class_destroy(my_class);
printk("cleanup done\n");
}
{
}
static int device_open(struct inode* inode, struct file* file)
{
static int counter = 0;
if (Device_open)
{
return -EBUSY;
}
Device_open++;
sprintf(msg, "I already told you %d times hello world\nDevice_open=%d\n", counter++, Device_open);
msg_ptr = msg;
try_module_get(THIS_MODULE);
return SUCCESS;
}
{
}
static int device_release(struct inode* inode, struct file* file)
{
Device_open--;
module_put(THIS_MODULE);
return 0;
}
{
}
static ssize_t device_read(struct file* filp, char *buffer, size_t length, loff_t *offset)
{
int bytes_read = 0;
if (*msg_ptr == '\0')
return 0;
printk(KERN_ALERT "length=%d\n", length);
while (length && *msg_ptr)
{
put_user(*(msg_ptr++), buffer++);
length--;
bytes_read++;
}
return bytes_read;
}
{
}
static ssize_t device_write(struct file* filp, const char *buff, size_t len, loff_t *off)
{
printk(KERN_ALERT "Sorry, this operation isn't supported\n");
return -EINVAL;
}
{
}
0 0
- 创建字符设备
- 字符设备文件的创建
- 创建一个字符设备驱动
- 创建字符设备与生成设备节点
- 字符设备自动创建设备节点
- 创建字符设备的三种方法
- 如何一次创建多个字符设备
- 创建字符设备的三种方法
- 创建字符设备的三种方法
- LINUX 字符设备自动创建文件节点
- 使用ioctl方法创建字符设备驱动
- 《Linux驱动》创建字符类设备节点
- 创建一个杂项字符设备模版
- Linux字符设备节点创建讲解
- linux 驱动程序 设备模块 设备号 设备文件创建 设备注册 字符驱动设备分析
- 高级字符设备驱动-自动创建设备文件笔记
- 简单字符设备驱动和自动创建设备文件
- 简单字符设备驱动和自动创建设备文件
- 从网上查到的Android开发常见错误及技巧
- OpenglES2.0 for Android:来画个三角形吧
- 整理有关javaScript的知识1
- KTV 点歌单
- linux_2day
- 创建字符设备
- 第一天的javascript-简单的计算器和笔记
- 判断mvn命令是否成功
- PHP开源管理后台 OSAdmin
- win8.1中安装rabbitmq
- Masonry介绍与使用实践
- Reservior Sampling(蓄水池采样) in Data Streams
- 安卓程序如何获取另一个apk的签名
- AAA在Spring下集成ActiveMQ