-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathrtmouse_main.c
More file actions
259 lines (217 loc) · 7.51 KB
/
rtmouse_main.c
File metadata and controls
259 lines (217 loc) · 7.51 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
*
* rtmouse_main.c
* Raspberry Pi Mouse device driver
*
* Version: 3.3.4
*
* Copyright (C) 2015-2024 RT Corporation <shop@rt-net.jp>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "rtmouse.h"
MODULE_AUTHOR("RT Corporation");
MODULE_LICENSE("GPL");
MODULE_VERSION("3.3.4");
MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");
/*
* --- Device Numbers ---
* used in rtmouse_i2c.c, dev_init_module()
* and cleanup_each_dev()
*/
const unsigned int NUM_DEV[ID_DEV_SIZE] = {
[ID_DEV_LED] = 4, [ID_DEV_SWITCH] = 3, [ID_DEV_SENSOR] = 1,
[ID_DEV_BUZZER] = 1, [ID_DEV_MOTORRAWR] = 1, [ID_DEV_MOTORRAWL] = 1,
[ID_DEV_MOTOREN] = 1, [ID_DEV_MOTOR] = 1, [ID_DEV_CNT] = 2};
/*
* --- Device Names ---
* used in rtmouse_dev.c and dev_init_module()
*/
const char *NAME_DEV[ID_DEV_SIZE] = {[ID_DEV_LED] = "rtled",
[ID_DEV_SWITCH] = "rtswitch",
[ID_DEV_SENSOR] = "rtlightsensor",
[ID_DEV_BUZZER] = "rtbuzzer",
[ID_DEV_MOTORRAWR] = "rtmotor_raw_r",
[ID_DEV_MOTORRAWL] = "rtmotor_raw_l",
[ID_DEV_MOTOREN] = "rtmotoren",
[ID_DEV_MOTOR] = "rtmotor"};
// used in by rtmouse_dev.c and cleanup_each_dev()
int _major_dev[ID_DEV_SIZE] = {
[ID_DEV_LED] = DEV_MAJOR, [ID_DEV_SWITCH] = DEV_MAJOR,
[ID_DEV_SENSOR] = DEV_MAJOR, [ID_DEV_BUZZER] = DEV_MAJOR,
[ID_DEV_MOTORRAWR] = DEV_MAJOR, [ID_DEV_MOTORRAWL] = DEV_MAJOR,
[ID_DEV_MOTOREN] = DEV_MAJOR, [ID_DEV_MOTOR] = DEV_MAJOR};
// used in rtmouse_dev.c and cleanup_each_dev()
int _minor_dev[ID_DEV_SIZE] = {
[ID_DEV_LED] = DEV_MINOR, [ID_DEV_SWITCH] = DEV_MINOR,
[ID_DEV_SENSOR] = DEV_MINOR, [ID_DEV_BUZZER] = DEV_MINOR,
[ID_DEV_MOTORRAWR] = DEV_MINOR, [ID_DEV_MOTORRAWL] = DEV_MINOR,
[ID_DEV_MOTOREN] = DEV_MINOR, [ID_DEV_MOTOR] = DEV_MINOR};
/*
* --- General Options ---
* used in rtmouse_dev.c and dev_cleanup_module()
*/
struct class *class_dev[ID_DEV_SIZE] = {
[ID_DEV_LED] = NULL, [ID_DEV_SWITCH] = NULL,
[ID_DEV_SENSOR] = NULL, [ID_DEV_BUZZER] = NULL,
[ID_DEV_MOTORRAWR] = NULL, [ID_DEV_MOTORRAWL] = NULL,
[ID_DEV_MOTOREN] = NULL, [ID_DEV_MOTOR] = NULL};
// used in rtmouse_i2c.c and dev_cleanup_module()
struct cdev *cdev_array = NULL;
// used in rtmouse_i2c.c
volatile int cdev_index = 0;
// used in rtmouse_dev.c and rtmouse_gpio.c
volatile void __iomem *pwm_base;
volatile uint32_t *gpio_base;
// used in rtmouse_dev.c, rtmouse_i2c.c and rtmouse_spi.c
struct mutex lock;
/* --- Static variables --- */
// used in rtmouse_dev.c and rtmouse_spi.c
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
struct device *mcp320x_dev;
#endif
/*
* dev_init_module - register driver module
* called by module_init(dev_init_module)
*/
static int dev_init_module(void)
{
int retval, i;
int registered_devices = 0;
size_t size;
/* log loding message */
printk(KERN_INFO "%s: loading driver...\n", DRIVER_NAME);
/* Initialize mutex lock */
mutex_init(&lock);
retval = i2c_counter_init();
if (retval == 0) {
registered_devices += 2 * NUM_DEV[ID_DEV_CNT];
} else {
printk(KERN_ALERT
"%s: i2c counter device driver register failed.\n",
DRIVER_NAME);
return retval;
}
/* GPIOレジスタがマップ可能か調べる */
retval = gpio_map();
if (retval != 0) {
printk(KERN_ALERT "%s on %s: cannot use GPIO registers.\n",
__func__, DRIVER_NAME);
return -EBUSY;
}
/* GPIO初期化 */
// printk(KERN_DEBUG "%s: gpio initializing...\n", __func__);
rpi_gpio_function_set(LED0_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(LED1_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(LED2_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(LED3_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(R_LED_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(L_LED_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(RF_LED_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(LF_LED_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(BUZZER_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(MOTDIR_L_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(MOTDIR_R_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(MOTEN_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_OUTPUT);
rpi_gpio_function_set(SW1_PIN, RPI_GPF_INPUT);
rpi_gpio_function_set(SW2_PIN, RPI_GPF_INPUT);
rpi_gpio_function_set(SW3_PIN, RPI_GPF_INPUT);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTEN_BASE);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << R_LED_BASE);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << L_LED_BASE);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << RF_LED_BASE);
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << LF_LED_BASE);
for (i = 0; i < 100; i++) {
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << BUZZER_BASE);
udelay(500);
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << BUZZER_BASE);
udelay(500);
}
buzzer_init();
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << R_LED_BASE);
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << L_LED_BASE);
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << RF_LED_BASE);
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << LF_LED_BASE);
// printk(KERN_DEBUG "%s: gpio initialized\n", __func__);
/* cdev構造体の用意 */
size = sizeof(struct cdev) * NUM_DEV_TOTAL;
cdev_array = (struct cdev *)kmalloc(size, GFP_KERNEL);
/* デバイスドライバをカーネルに登録 */
for (i = 0; i < ID_DEV_SIZE - 1; i++) {
retval = register_dev(i);
if (retval != 0) {
printk(KERN_ALERT "%s: %s register failed.\n",
DRIVER_NAME, NAME_DEV[i]);
return retval;
}
}
retval = mcp3204_init();
if (retval != 0) {
printk(KERN_ALERT
"%s: optical sensor driver register failed.\n",
DRIVER_NAME);
return retval;
}
printk(KERN_INFO "%s: %d devices loaded.\n", DRIVER_NAME,
registered_devices + NUM_DEV_TOTAL);
printk(KERN_INFO "%s: module installed at %lu\n", DRIVER_NAME, jiffies);
return 0;
}
/*
* dev_cleanup_module - cleanup driver module
* called by module_exit(dev_cleanup_module)
*/
static void cleanup_each_dev(int id_dev)
{
int i;
dev_t devno;
dev_t devno_top;
devno_top = MKDEV(_major_dev[id_dev], _minor_dev[id_dev]);
for (i = 0; i < NUM_DEV[id_dev]; i++) {
devno = MKDEV(_major_dev[id_dev], _minor_dev[id_dev] + i);
device_destroy(class_dev[id_dev], devno);
}
unregister_chrdev_region(devno_top, NUM_DEV[id_dev]);
}
static void dev_cleanup_module(void)
{
int i;
/* --- remove char device --- */
for (i = 0; i < NUM_DEV_TOTAL; i++) {
cdev_del(&(cdev_array[i]));
}
/* --- free device num. and remove device --- */
for (i = 0; i < ID_DEV_SIZE - 1; i++) {
cleanup_each_dev(i);
}
/* --- remove device node --- */
for (i = 0; i < ID_DEV_SIZE - 1; i++) {
class_destroy(class_dev[i]);
}
/* remove MCP3204 */
mcp3204_exit();
/* remove I2C device */
i2c_counter_exit();
/* free cdev memory */
kfree(cdev_array);
gpio_unmap();
printk(KERN_INFO "%s: module removed at %lu\n", DRIVER_NAME, jiffies);
}
/* --- MAIN PROCESS --- */
module_init(dev_init_module);
module_exit(dev_cleanup_module);