104 lines
3.3 KiB
C
104 lines
3.3 KiB
C
|
#include <linux/types.h>
|
||
|
#include <linux/kernel.h>
|
||
|
#include <linux/delay.h>
|
||
|
#include <linux/ide.h>
|
||
|
#include <linux/init.h>
|
||
|
#include <linux/module.h>
|
||
|
#include <linux/errno.h>
|
||
|
#include <linux/gpio.h>
|
||
|
#include <linux/cdev.h>
|
||
|
#include <linux/device.h>
|
||
|
#include <asm/mach/map.h>
|
||
|
#include <asm/uaccess.h>
|
||
|
#include <asm/io.h>
|
||
|
|
||
|
#define NEWCHRLED_CNT 1
|
||
|
#define NEWCHRLED_NAME "test"
|
||
|
|
||
|
/* newchrled设备结构体 */
|
||
|
struct newchrled_dev{
|
||
|
dev_t devid; /* 设备号 */
|
||
|
struct cdev cdev; /* cdev */
|
||
|
struct class *class; /* 类 */
|
||
|
struct device *device; /* 设备 */
|
||
|
int major; /* 主设备号 */
|
||
|
int minor; /* 次设备号 */
|
||
|
};
|
||
|
|
||
|
struct newchrled_dev newchrled; /* led设备 */
|
||
|
|
||
|
static int led_open(struct inode *inode, struct file *filp)
|
||
|
{
|
||
|
filp->private_data = &newchrled; /* 设置私有数据 */
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static ssize_t led_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* 设备操作函数 */
|
||
|
|
||
|
static int led_release(struct inode *inode, struct file *filp)
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/* 设备操作函数 */
|
||
|
static struct file_operations newchrled_fops = {
|
||
|
.owner = THIS_MODULE,
|
||
|
.open = led_open,
|
||
|
.read = led_read,
|
||
|
.write = led_write,
|
||
|
.release = led_release,
|
||
|
};
|
||
|
|
||
|
static int __init led_init(void)
|
||
|
{
|
||
|
if (newchrled.major) { /* 定义了设备号 */
|
||
|
printk("newchrled.major ******************\n");
|
||
|
newchrled.devid = MKDEV(newchrled.major, 0);
|
||
|
register_chrdev_region(newchrled.devid, NEWCHRLED_CNT, NEWCHRLED_NAME);
|
||
|
} else { /* 没有定义设备号 */
|
||
|
alloc_chrdev_region(&newchrled.devid, 0, NEWCHRLED_CNT, NEWCHRLED_NAME); /* 申请设备号 */
|
||
|
newchrled.major = MAJOR(newchrled.devid); /* 获取分配号的主设备号 */
|
||
|
newchrled.minor = MINOR(newchrled.devid); /* 获取分配号的次设备号 */
|
||
|
printk("newchrled.major %d newchrled.minor %d\n",newchrled.major,newchrled.minor);
|
||
|
}
|
||
|
newchrled.cdev.owner = THIS_MODULE;
|
||
|
cdev_init(&newchrled.cdev, &newchrled_fops);
|
||
|
cdev_add(&newchrled.cdev, newchrled.devid, NEWCHRLED_CNT);
|
||
|
newchrled.class = class_create(THIS_MODULE, NEWCHRLED_NAME);
|
||
|
if (IS_ERR(newchrled.class)) {
|
||
|
return PTR_ERR(newchrled.class);
|
||
|
}
|
||
|
newchrled.device = device_create(newchrled.class, NULL,newchrled.devid, NULL, NEWCHRLED_NAME);
|
||
|
if (IS_ERR(newchrled.device)) {
|
||
|
return PTR_ERR(newchrled.device);
|
||
|
}
|
||
|
printk("init success\n");
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static void __exit led_exit(void)
|
||
|
{
|
||
|
cdev_del(&newchrled.cdev);/* 删除cdev */
|
||
|
unregister_chrdev_region(newchrled.devid, NEWCHRLED_CNT); /* 注销设备号 */
|
||
|
device_destroy(newchrled.class, newchrled.devid);
|
||
|
class_destroy(newchrled.class);
|
||
|
|
||
|
printk("exit success\n");
|
||
|
}
|
||
|
|
||
|
module_init(led_init);
|
||
|
module_exit(led_exit);
|
||
|
MODULE_LICENSE("GPL");
|
||
|
MODULE_AUTHOR("luozhikun");
|