-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspisw.c
333 lines (259 loc) · 8.18 KB
/
spisw.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* spisw.c - SPI software driver (bitbang)
*
* usage:
* make
* sudo insmod spisw.ko // insert module
* lsmod // list modules
* modinfo spisw.ko // info
* sudo rmmod spisw.ko // unload module
* dmesg // view printk()
*
* 2015, gmb */
/* standard libs cannot be used */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/mutex.h>
#include <linux/gpio.h>
#include <asm/gpio.h>
#include <linux/delay.h>
#include "spisw.h"
#define DEVICE_NAME "spisw" /* the device will appear at /dev/spisw using this value */
#define CLASS_NAME "spiswcd"
#define DEBUG 1
MODULE_LICENSE("GPL"); /* affects runtime behavior */
MODULE_AUTHOR("gmb"); /* visible when modinfo is used */
MODULE_DESCRIPTION("SPI software driver"); /* visible when modinfo is used */
MODULE_VERSION("1.0"); /* visible when modinfo is used */
static int major_number;
static struct class* hwcd_class = NULL;
static struct device* hwcd_device = NULL;
static unsigned int gpio_clck;
static unsigned int gpio_mosi;
static unsigned int gpio_miso;
static DEFINE_MUTEX(hwcd_mutex); /* declare new mutex with value 1 (unlocked) */
/* the prototype functions for the character driver */
static int dev_open(struct inode *, struct file *);
static int dev_release(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
static long dev_ioctl(struct file *, unsigned int, unsigned long);
/* devices are represented as file structure in the kernel */
static struct file_operations fops =
{
.open = dev_open,
.read = dev_read,
.write = dev_write,
.unlocked_ioctl = dev_ioctl,
.release = dev_release, /* close()*/
};
/* LKM (Linux Kernel Module) initialization */
static int __init
spisw_init(void){
#ifdef DEBUG
printk(KERN_INFO "[spisw] init() started\n");
#endif
/* try to dynamically allocate a major number for the device */
major_number = register_chrdev(0, DEVICE_NAME, &fops);
if (major_number < 0){
printk(KERN_ALERT "[spisw] failed to register a major number\n");
return major_number;
}
#ifdef DEBUG
printk(KERN_INFO "[spisw] registered correctly with major number %d\n", major_number);
#endif
/* register the device class */
hwcd_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(hwcd_class)){
printk(KERN_ALERT "[spisw] failed to register device class\n");
goto err_class;
}
#ifdef DEBUG
printk(KERN_INFO "[spisw] device class registered correctly\n");
#endif
/* register the device driver */
hwcd_device = device_create(hwcd_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME);
if (IS_ERR(hwcd_device)){
printk(KERN_ALERT "[spisw] failed to create the device\n");
goto err_dev;
}
#ifdef DEBUG
printk(KERN_INFO "[spisw] device class created correctly\n");
#endif
mutex_init(&hwcd_mutex); /* initialize the mutex dynamically */
return 0;
err_dev:
class_destroy(hwcd_class);
err_class:
unregister_chrdev(major_number, DEVICE_NAME);
return PTR_ERR(hwcd_class); /* correct way to return an error on a pointer */
}
/* LKM cleanup */
static void __exit
spisw_exit(void){
device_destroy(hwcd_class, MKDEV(major_number, 0)); /* remove the device */
class_unregister(hwcd_class); /* unregister the device class */
class_destroy(hwcd_class); /* remove the device class */
unregister_chrdev(major_number, DEVICE_NAME); /* unregister the major number */
mutex_destroy(&hwcd_mutex); /* destroy the mutex */
#ifdef DEBUG
printk(KERN_INFO "[spisw] exited");
#endif
}
/* this function is called when the device is opened */
static int
dev_open(struct inode *inodep, struct file *filep){
/* try to acquire the mutex */
if(!mutex_trylock(&hwcd_mutex)){
printk(KERN_ALERT "[spisw] device in use by another process");
return -EBUSY;
}
printk(KERN_INFO "[spisw] device is opened\n");
return 0;
}
/* this function is called when the device is closed/released */
static int
dev_release(struct inode *inodep, struct file *filep){
gpio_set_value(gpio_clck, SPISW_LOW);
gpio_set_value(gpio_mosi, SPISW_LOW);
gpio_free(gpio_clck);
gpio_free(gpio_mosi);
gpio_free(gpio_miso);
mutex_unlock(&hwcd_mutex); /* unlock the mutex */
printk(KERN_INFO "[spisw] device is closed\n");
return 0;
}
unsigned char
read(void){
unsigned char r = 0x00, i;
for(i = 0; i < 8; i++){
gpio_set_value(gpio_clck, SPISW_LOW);
gpio_set_value(gpio_clck, SPISW_HIGH);
r <<= 1;
if(gpio_get_value(gpio_miso) != 0)
r |= 0x1;
}
#ifdef DEBUG
printk(KERN_INFO "[spisw] read byte : %#02x\n", r);
#endif
return r;
}
/* this function is called whenever the device is being read from user space */
static ssize_t
dev_read(struct file *filep, char *buffer, size_t len, loff_t *offset){
int error_count = 0;
unsigned char val = read();
error_count = copy_to_user(buffer, &val, 1);
if(error_count != 0){
printk(KERN_INFO "[spisw] failed to send data to the user\n");
return -EFAULT; /* return a bad address message */
}
return 1;
}
void
write(unsigned char byte){
unsigned char bit;
for(bit = 0x80; bit; bit >>= 1) {
if(byte & bit) {
gpio_set_value(gpio_mosi, SPISW_HIGH);
} else {
gpio_set_value(gpio_mosi, SPISW_LOW);
}
gpio_set_value(gpio_clck, SPISW_HIGH);
gpio_set_value(gpio_clck, SPISW_LOW);
}
#ifdef DEBUG
//printk(KERN_INFO "[spisw] written byte : %#02x\n", byte);
#endif
}
/* this function is called whenever the device is being written to from user space */
static ssize_t
dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset){
write((unsigned char) len);
return 1;
}
static long
dev_ioctl(struct file *file, unsigned int ioctl_cmd, unsigned long ioctl_param){
switch (ioctl_cmd) {
case SPISW_SET_CLCK:
/* not implemented */
break;
case SPISW_SET_MOSI:
/* not implemented */
break;
case SPISW_SET_MISO:
/* not implemented */
break;
case SPISW_W_BYTE:
write((unsigned char) ioctl_param);
return 0;
case SPISW_R_BYTE:
return ((long) read());
case SPISW_INIT:
/* arduino board pin 13 */
gpio_clck = 40;
if(gpio_is_valid(gpio_clck) == 0){
printk(KERN_ALERT "[spisw] pin (%d) is not valid\n", gpio_clck);
return -1;
}
if(gpio_request(gpio_clck, "spisw_clck") != 0){
printk(KERN_ALERT "[spisw] unable to request clck pin (%d)\n", gpio_clck);
return -1;
}
if(gpio_direction_output(gpio_clck, SPISW_LOW) != 0){
printk(KERN_ALERT "[spisw] unable to set the direction to output in the clck pin\n");
return -1;
}
#ifdef DEBUG
printk(KERN_INFO "[spisw] clck pin (%d) is working as an output\n", gpio_clck);
#endif
/* arduino board pin 11 */
gpio_mosi = 43;
if(gpio_is_valid(gpio_mosi) == 0){
printk(KERN_ALERT "[spisw] pin (%d) is not valid\n", gpio_mosi);
return -1;
}
if(gpio_request(gpio_mosi, "spisw_mosi") != 0){
printk(KERN_ALERT "[spisw] unable to request mosi pin (%d)\n", gpio_mosi);
return -1;
}
if(gpio_direction_output(gpio_mosi, SPISW_LOW) != 0){
printk(KERN_ALERT "[spisw] unable to set the direction to output in the mosi pin\n");
return -1;
}
#ifdef DEBUG
printk(KERN_INFO "[spisw] mosi pin (%d) is working as an output\n", gpio_mosi);
#endif
/* arduino board pin 12 */
gpio_miso = 42;
if(gpio_is_valid(gpio_miso) == 0){
printk(KERN_ALERT "[spisw] pin (%d) is not valid\n", gpio_miso);
return -1;
}
if(gpio_request(gpio_miso, "spisw_miso") != 0){
printk(KERN_ALERT "[spisw] unable to request miso pin (%d)\n", gpio_miso);
return -1;
}
if(gpio_direction_input(gpio_miso) != 0){
printk(KERN_ALERT "[spisw] unable to set the direction to input in the miso pin\n");
return -1;
}
#ifdef DEBUG
printk(KERN_INFO "[spisw] miso pin (%d) is working as an input\n", gpio_miso);
#endif
gpio_set_value(gpio_clck, SPISW_LOW);
gpio_set_value(gpio_mosi, SPISW_LOW);
return 0;
break;
default:
printk(KERN_ALERT "[spisw] unsupported ioctl() command : %d\n", (int) ioctl_param);
return -1;
}
return 0;
}
/* registers the init and exit function for the LKM */
module_init(spisw_init);
module_exit(spisw_exit);