1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
| /*
* led_drv.c : 实现led的驱动
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h> // struct file_operations
#include <linux/slab.h> // kzalloc
#include <linux/device.h> // class_create和device_create
#include <asm/io.h> // ioremap
#include <linux/uaccess.h> // copy_from_user
#include "led.h"
/* 自定义设备结构体,用于存储设备相关的信息 */
struct led_device {
int dev_major;
struct class *cls;
struct device * dev;
int value;
};
/* 全局变量 */
static struct led_device *led_dev;
volatile unsigned long * gpl10_conf;
volatile unsigned long * gpl10_data;
volatile unsigned long * gpa10_conf;
volatile unsigned long * gpa10_data;
/* 自定义函数 */
static int led_drv_open(struct inode *inode, struct file *filp)
{
printk("-------^_^ %s-------\n", __FUNCTION__);
*gpl10_conf &= (0x1<<8);
*gpa10_conf &= (0x1<<8);
*gpl10_data |= (0x1<<10);
*gpa10_data |= (0x1<<10);
return 0;
}
static ssize_t led_drv_read (struct file *filp, char __user *buf, size_t count, loff_t *fpos)
{
printk("-------^_^ %s-------\n", __FUNCTION__);
return 0;
}
static ssize_t led_drv_write (struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
{
int ret = 0;
printk("-------^_^ %s-------\n", __FUNCTION__);
ret = copy_from_user(&led_dev->value, buf, count);
if (led_dev->value)
{
*gpl10_data |= (0x1<<10);
} else {
*gpl10_data &= ~(0x1<<10);
}
return count;
}
long led_drv_ioctl(struct file *filp, unsigned int cmd, unsigned long args)
{
int num = args;
switch(cmd)
{
case LED_NUM_ON:
if (num!=1 && num!=2)
{
return -EINVAL;
} else if (num == 1){
*gpl10_data |= (1<<10);
} else {
*gpa10_data |= (1<<10);
}
break;
case LED_NUM_OFF :
if (num!=1 && num!=2)
{
return -EINVAL;
} else if (num == 1){
*gpl10_data &= ~(1<<10);
} else {
*gpa10_data &= ~(1<<10);
}
break;
default:
printk("unkonw cmd\n");
return -EINVAL;
}
return 0;
}
static int led_drv_close (struct inode *inode, struct file *filp)
{
printk("-------^_^ %s-------\n", __FUNCTION__);
*gpl10_data &= ~(0x1<<10);
*gpa10_data &= ~(0x1<<10);
return 0;
}
static struct file_operations led_drv_fops = {
.open = led_drv_open,
.read = led_drv_read,
.write = led_drv_write,
.release = led_drv_close,
.unlocked_ioctl = led_drv_ioctl,
};
/* 模块初始化和卸载 */
static int __init led_drv_init(void)
{
int ret = 0;
printk(KERN_ERR"-----------from led.ko : %s %s---------\n", __FILE__, __FUNCTION__);
led_dev = kzalloc(sizeof(struct led_device), GFP_KERNEL);
if (led_dev == NULL)
{
printk(KERN_ERR"kzalloc error\n");
return -ENOMEM;
}
led_dev->dev_major = 240;
ret = register_chrdev(led_dev->dev_major, "led_drv", &led_drv_fops);
if (ret < 0 )
{
printk("register_chrdev error\n");
ret = -EINVAL;
goto err_free;
}
led_dev->cls = class_create(THIS_MODULE,"led_cls");
if(IS_ERR(led_dev->cls))
{
printk(KERN_ERR"class_create error\n");
ret = PTR_ERR(led_dev->cls);
goto err_unregister;
}
led_dev->dev = device_create(led_dev->cls, NULL,MKDEV(led_dev->dev_major, 0), NULL, "led");
if(IS_ERR(led_dev->dev))
{
printk("device_create error\n");
ret = PTR_ERR(led_dev->dev);
goto err_class_destroy;
}
gpl10_conf = ioremap(0x01F02C04, 4);
gpl10_data = ioremap(0x01F02C10, 4);
gpa10_conf = ioremap(0x01C20804, 4);
gpa10_data = ioremap(0x01C20810, 4);
return 0;
err_class_destroy:
class_destroy(led_dev->cls);
err_unregister:
unregister_chrdev(led_dev->dev_major, "led_drv");
err_free:
kfree(led_dev);
return ret;
}
static void __exit led_drv_exit(void)
{
printk("-----------from led.ko : %s %s---------\n", __FILE__, __FUNCTION__);
iounmap(gpl10_conf);
iounmap(gpl10_data);
iounmap(gpa10_conf);
iounmap(gpa10_data);
device_destroy(led_dev->cls, MKDEV(led_dev->dev_major, 0));
class_destroy(led_dev->cls);
unregister_chrdev(led_dev->dev_major, "led_drv");
kfree(led_dev);
}
module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");
|