-
Notifications
You must be signed in to change notification settings - Fork 4
/
mi2c-char.c
295 lines (218 loc) · 5.58 KB
/
mi2c-char.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
/*
* Char device wrapper for the mi2c-i2c kernel module example.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/slab.h>
#include "mi2c.h"
#include "mi2c-i2c.h"
#define USER_BUFF_SIZE 128
struct mi2c_dev {
dev_t devt;
struct cdev cdev;
struct semaphore sem;
struct class *class;
char *user_buff;
};
static struct mi2c_dev mi2c_dev;
/*
* An example just to demo how you would use the write/read functions.
* I have some BlinkM leds this driver is handling. This is how you read
* their current color value. No error handling on the val pointer.
*/
#define GET_CURRENT_RGB_COLOR 0x67
static int blinkm_read_rgb(unsigned int device_id, unsigned char *val)
{
int result;
unsigned char buff[4];
buff[0] = GET_CURRENT_RGB_COLOR;
result = mi2c_i2c_write(device_id, buff, 1);
if (result != 1)
return result;
buff[0] = 0;
buff[1] = 0;
buff[2] = 0;
result = mi2c_i2c_read(device_id, buff, 3);
if (result != 3)
return result;
val[0] = buff[0];
val[1] = buff[1];
val[2] = buff[2];
return 0;
}
/*
* See the arduino_i2c_slave.pde under the arduino_i2c_slave directory
* for the arduino code. Basically, the arduino is running a simple program
* where he responds to 1 byte commands with a two-byte response.
*/
static int arduino_run_command(unsigned char cmd, unsigned int *val)
{
int result;
unsigned char buff[2];
buff[0] = cmd;
/* We know the Arduino is device_id 2. It's just a demo... */
result = mi2c_i2c_write(2, buff, 1);
if (result != 1)
return result;
buff[0] = 0;
buff[1] = 0;
result = mi2c_i2c_read(2, buff, 2);
if (result != 2)
return result;
*val = (buff[0] << 8) | buff[1];
return 0;
}
static ssize_t mi2c_read(struct file *filp, char __user *buff,
size_t count, loff_t *offp)
{
ssize_t status;
size_t len;
unsigned int i, addr, val;
unsigned char rgb[4], cmd;
/*
Generic user progs like cat will continue calling until we
return zero. So if *offp != 0, we know this is at least the
second call.
*/
if (*offp > 0)
return 0;
if (down_interruptible(&mi2c_dev.sem))
return -ERESTARTSYS;
memset(rgb, 0, sizeof(rgb));
memset(mi2c_dev.user_buff, 0, USER_BUFF_SIZE);
len = 0;
/* This is only for my test devices, a couple of BlinkM leds. */
for (i = 0; i < 2; i++) {
if (blinkm_read_rgb(i, rgb) < 0) {
printk(KERN_ALERT "Read of BlinkM %d failed\n", i);
}
else {
addr = mi2c_i2c_get_address(i);
len += sprintf(mi2c_dev.user_buff + len,
"BlinkM at 0x%02X (r,g,b) %d %d %d\n",
addr, rgb[0], rgb[1], rgb[2]);
}
}
/* and one Arduino device */
addr = mi2c_i2c_get_address(2);
val = 0;
cmd = 0x02;
if (arduino_run_command(cmd, &val) < 0)
printk(KERN_ALERT "Read of Arduino at 0x%02X failed\n", addr);
else
len += sprintf(mi2c_dev.user_buff + len,
"Arduino at 0x%02X responded to 0x%02X with 0x%04X\n",
addr, cmd, val);
len = strlen(mi2c_dev.user_buff);
if (len > count)
len = count;
if (copy_to_user(buff, mi2c_dev.user_buff, len)) {
status = -EFAULT;
goto mi2c_read_done;
}
*offp += len;
status = len;
mi2c_read_done:
up(&mi2c_dev.sem);
return status;
}
static int mi2c_open(struct inode *inode, struct file *filp)
{
int status = 0;
if (down_interruptible(&mi2c_dev.sem))
return -ERESTARTSYS;
if (!mi2c_dev.user_buff) {
mi2c_dev.user_buff = kmalloc(USER_BUFF_SIZE, GFP_KERNEL);
if (!mi2c_dev.user_buff) {
printk(KERN_ALERT
"mi2c_open: user_buff alloc failed\n");
status = -ENOMEM;
}
}
up(&mi2c_dev.sem);
return status;
}
static const struct file_operations mi2c_fops = {
.owner = THIS_MODULE,
.open = mi2c_open,
.read = mi2c_read,
};
static int __init mi2c_init_cdev(void)
{
int error;
mi2c_dev.devt = MKDEV(0, 0);
error = alloc_chrdev_region(&mi2c_dev.devt, 0, 1, DRIVER_NAME);
if (error < 0) {
printk(KERN_ALERT
"alloc_chrdev_region() failed: error = %d \n",
error);
return -1;
}
cdev_init(&mi2c_dev.cdev, &mi2c_fops);
mi2c_dev.cdev.owner = THIS_MODULE;
error = cdev_add(&mi2c_dev.cdev, mi2c_dev.devt, 1);
if (error) {
printk(KERN_ALERT "cdev_add() failed: error = %d\n", error);
unregister_chrdev_region(mi2c_dev.devt, 1);
return -1;
}
return 0;
}
static int __init mi2c_init_class(void)
{
mi2c_dev.class = class_create(THIS_MODULE, DRIVER_NAME);
if (!mi2c_dev.class) {
printk(KERN_ALERT "class_create(mi2c) failed\n");
return -1;
}
if (!device_create(mi2c_dev.class, NULL, mi2c_dev.devt,
NULL, DRIVER_NAME)) {
class_destroy(mi2c_dev.class);
return -1;
}
return 0;
}
static int __init mi2c_init(void)
{
printk(KERN_INFO "mi2c_init()\n");
memset(&mi2c_dev, 0, sizeof(struct mi2c_dev));
sema_init(&mi2c_dev.sem, 1);
if (mi2c_init_cdev() < 0)
goto init_fail_1;
if (mi2c_init_class() < 0)
goto init_fail_2;
if (mi2c_init_i2c() < 0)
goto init_fail_3;
return 0;
init_fail_3:
device_destroy(mi2c_dev.class, mi2c_dev.devt);
class_destroy(mi2c_dev.class);
init_fail_2:
cdev_del(&mi2c_dev.cdev);
unregister_chrdev_region(mi2c_dev.devt, 1);
init_fail_1:
return -1;
}
module_init(mi2c_init);
static void __exit mi2c_exit(void)
{
printk(KERN_INFO "mi2c_exit()\n");
mi2c_cleanup_i2c();
device_destroy(mi2c_dev.class, mi2c_dev.devt);
class_destroy(mi2c_dev.class);
cdev_del(&mi2c_dev.cdev);
unregister_chrdev_region(mi2c_dev.devt, 1);
if (mi2c_dev.user_buff)
kfree(mi2c_dev.user_buff);
}
module_exit(mi2c_exit);
MODULE_AUTHOR("Scott Ellis");
MODULE_DESCRIPTION("mi2c driver");
MODULE_LICENSE("Dual BSD/GPL");
MODULE_VERSION("0.1");