-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfarm.c
144 lines (127 loc) · 2.81 KB
/
farm.c
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
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/gpio.h>
#include <linux/uaccess.h>
#include "farm.h"
#include "motor.h"
#include "led.h"
#include "speaker.h"
#include "spi.h"
#include "dht.h"
MODULE_LICENSE("GPL");
static dev_t dev_num = 0;
static struct cdev *cd_cdev = NULL;
static int farm_open(struct inode *, struct file *);
static int farm_release(struct inode *, struct file *);
static long farm_ioctl(struct file *, unsigned int, unsigned long);
static struct file_operations farm_fops = {
.open = farm_open,
.release = farm_release,
.unlocked_ioctl = farm_ioctl
};
static int farm_open(struct inode *inode, struct file *file) {
return 0;
}
static int farm_release(struct inode *inode, struct file *file) {
return 0;
}
static long farm_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
int value = 0;
unsigned long copy_size = 0;
switch(cmd) {
case MOTOR_OFF:
module_motor_off();
break;
case MOTOR_ON:
module_motor_on();
break;
case LED_OFF:
module_led_off();
break;
case LED_ON:
module_led_on();
case SPEAKER_PLAY:
module_speaker_play();
break;
case SPEAKER_STOP:
module_speaker_stop();
break;
case SPI_ON:
module_spi_on();
break;
case SPI_OFF:
module_spi_off();
break;
case DHT_ON:
module_dht_on();
break;
case DHT_OFF:
module_dht_off();
break;
case DHT_SET_INPUT:
module_dht_set_input();
break;
case DHT_SET_OUTPUT:
module_dht_set_output();
break;
case DHT_GET_DATA:
value = module_dht_get_value();
copy_size = copy_to_user((int*)arg, (int*)&value, sizeof(value));
break;
default:
break;
}
return 0;
}
/* *
* Farm Module Init
* */
static int __init farm_init(void) {
// Initialize Character Device
int ret = 0;
alloc_chrdev_region(&dev_num, 0, 1, FARM_DEV);
cd_cdev = cdev_alloc();
cdev_init(cd_cdev, &farm_fops);
ret = cdev_add(cd_cdev, dev_num, 1);
if( ret < 0 ) return -1;
// Initialize Motor Device
ret = module_motor_init();
if( ret < 0 ) return -1;
// Initialize LED Device
ret = module_led_init();
if( ret < 0 ) return -1;
// Initialize SPEAKER Device
ret = module_speaker_init();
if( ret < 0 ) return -1;
// Initialize SPI Device
ret = module_spi_init();
if( ret < 0 ) return -1;
// Initialize DHT Device
ret = module_dht_init();
if( ret < 0 ) return -1;
printk("Hello Farm!\n");
return 0;
}
/* *
* Farm Module Exit
* */
static void __exit farm_exit(void) {
// Destroy Character Device
cdev_del(cd_cdev);
unregister_chrdev_region(dev_num, 1);
// Destroy Other Modules
module_motor_exit();
module_led_exit();
module_speaker_exit();
module_spi_exit();
module_dht_exit();
printk("Goodbye Farm!\n");
}
/* *
* Module Setting
* */
module_init(farm_init);
module_exit(farm_exit);