This repository has been archived by the owner on Jan 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mmio.c
333 lines (285 loc) · 7.67 KB
/
mmio.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
/*
* MMIO
*
* Copyright (C) 2014 Joe Balough <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/rwsem.h>
#include <linux/err.h>
#include <linux/ctype.h>
#include <net/sctp/command.h>
#include "mmio.h"
DECLARE_RWSEM(mmio_list_lock);
LIST_HEAD(mmio_list);
static struct class *mmio_class;
/**
* mmio_get_value - Internal mechanism to get the value of a register
* @parent The mmio_classdev bank containing the entry
* @entry The mmio_entry to get
*/
u32 mmio_get_value(struct mmio_classdev *parent, struct mmio_entry *entry)
{
u32 reg, mask;
if (! parent || !entry)
{
printk(KERN_ERR "%s: preventing null pointer deref. parent is 0x%p, entry is 0x%p\n", __FUNCTION__, parent, entry);
return 0;
}
// Avoid using semaphore if uninitialized
// Allows usage before mmio_classdev_register is called (before fs_init)
// Use caution whenever calling this function without proper initialization
if(parent->dev != NULL)
{
down_read(&parent->rwsem);
}
switch(parent->size)
{
default:
case 1:
reg = __raw_readb(parent->base + parent->offset);
break;
case 2:
reg = __raw_readw(parent->base + parent->offset);
break;
case 4:
reg = __raw_readl(parent->base + parent->offset);
break;
}
if(parent->dev != NULL)
{
up_read(&parent->rwsem);
}
reg &= entry->mask;
mask = entry->mask;
while (0 == (mask & 1))
{
mask >>= 1;
reg >>= 1;
}
return reg;
}
EXPORT_SYMBOL_GPL(mmio_get_value);
/**
* mmio_value_show - Sysfs interface to show the value of a register.
*/
static ssize_t mmio_value_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
int i;
struct mmio_classdev *mmio_cdev = dev_get_drvdata(dev);
struct mmio_entry *entry = NULL;
u32 value;
for (i = 0; i < mmio_cdev->num_entries; i++)
{
if ( mmio_cdev->entries[i].name == attr->attr.name )
{
entry = &(mmio_cdev->entries[i]);
break;
}
}
if (entry == NULL)
return -EINVAL;
if (! (entry->flags & MMIO_ENTRY_READ) )
return -EPERM;
value = mmio_get_value(mmio_cdev, entry);
return sprintf(buf, "%u\n", value);
}
/**
* mmio_set_value - Internal mechanism to set value to register
* @parent The mmio_classdev bank containing the entry
* @entry The mmio_entry to modify
* @value The value to set
*/
int mmio_set_value(struct mmio_classdev *parent, struct mmio_entry *entry, unsigned long value)
{
u32 reg, mask;
if (!parent || !entry)
return -EINVAL;
// Avoid using semaphore if uninitialized
// Allows usage before mmio_classdev_register is called (before fs_init)
// Use caution whenever calling this function without proper initialization
if(parent->dev != NULL)
{
down_write(&parent->rwsem);
}
switch(parent->size)
{
default:
case 1:
reg = __raw_readb(parent->base + parent->offset);
break;
case 2:
reg = __raw_readw(parent->base + parent->offset);
break;
case 4:
reg = __raw_readl(parent->base + parent->offset);
break;
}
if (value)
{
mask = entry->mask;
while (0 == (mask & 1))
{
mask >>= 1;
value <<= 1;
}
}
if ((value & entry->mask) != value)
{
if(parent->dev != NULL)
{
up_write(&parent->rwsem);
}
return -EOVERFLOW;
}
reg &= ~entry->mask;
reg |= value;
switch(parent->size)
{
default:
case 1:
__raw_writeb((u8) reg, parent->base + parent->offset);
break;
case 2:
__raw_writew((u16) reg, parent->base + parent->offset);
break;
case 4:
__raw_writel(reg, parent->base + parent->offset);
break;
}
if(parent->dev != NULL)
{
up_write(&parent->rwsem);
}
return 0;
}
EXPORT_SYMBOL_GPL(mmio_set_value);
/**
* mmio_value_store - Sysfs interface to store a value to a register.
*/
static ssize_t mmio_value_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
int i, r;
struct mmio_classdev *mmio_cdev = dev_get_drvdata(dev);
struct mmio_entry *entry = NULL;
ssize_t ret = -EINVAL;
char *after;
unsigned long state = simple_strtoul(buf, &after, 10);
size_t count = after - buf;
for (i = 0; i < mmio_cdev->num_entries; i++)
{
if ( mmio_cdev->entries[i].name == attr->attr.name )
{
entry = &(mmio_cdev->entries[i]);
break;
}
}
if (entry == NULL)
return -EINVAL;
if (! (entry->flags & MMIO_ENTRY_WRITE) )
return -EPERM;
if (isspace(*after))
count++;
if (count == size) {
ret = count;
r = mmio_set_value(mmio_cdev, entry, state);
if (r < 0) ret = r;
}
return ret;
}
/**
* mmio_classdev_register - register a new object of the mmio_classdev class.
* @parent: The device to register
* @mmio_cdev: The mmio_classdev structure for this device
*/
int mmio_classdev_register(struct device *parent, struct mmio_classdev *mmio_cdev)
{
int i, ret;
if (!mmio_cdev->base || !mmio_cdev->entries || !mmio_cdev->name)
return -EINVAL;
if (mmio_cdev->size != 1 && mmio_cdev->size != 2 && mmio_cdev->size != 4)
return -EINVAL;
if (mmio_cdev->size == 2 && ((int) mmio_cdev->base + mmio_cdev->offset) & 0x01)
return -EINVAL;
if (mmio_cdev->size == 4 && ((int) mmio_cdev->base + mmio_cdev->offset) & 0x03)
return -EINVAL;
mmio_cdev->dev = device_create(mmio_class, parent, 0, mmio_cdev,
"%s", mmio_cdev->name);
if (IS_ERR(mmio_cdev->dev))
return PTR_ERR(mmio_cdev->dev);
init_rwsem(&mmio_cdev->rwsem);
for (i = 0; i < mmio_cdev->num_entries; i++)
{
if (!mmio_cdev->entries[i].mask)
{
printk(KERN_INFO "%s: Skipping entry %d (%s), mask is zero.\n", __FUNCTION__, i, mmio_cdev->entries[i].name);
continue;
}
mmio_cdev->entries[i].attr.attr.name = mmio_cdev->entries[i].name;
mmio_cdev->entries[i].attr.attr.mode = 0644;
mmio_cdev->entries[i].attr.show = mmio_value_show;
mmio_cdev->entries[i].attr.store = mmio_value_store;
ret = device_create_file(mmio_cdev->dev, &(mmio_cdev->entries[i].attr));
if (ret) {
printk(KERN_ERR "%s: Failed to add sysfs file %s\n", __FUNCTION__, mmio_cdev->entries[i].name);
dev_err(mmio_cdev->dev, "failed: sysfs file %s\n", mmio_cdev->entries[i].name);
goto failed_unregister_dev_file;
}
}
// add to the list of mmio devices
down_write(&mmio_list_lock);
list_add_tail(&mmio_cdev->node, &mmio_list);
up_write(&mmio_list_lock);
printk(KERN_INFO "Registered mmio device \"%s\" at 0x%p, offset 0x%x, size %d B.\n",
mmio_cdev->name, mmio_cdev->base, mmio_cdev->offset, mmio_cdev->size);
return 0;
failed_unregister_dev_file:
for (i--; i >= 0; i--)
device_remove_file(mmio_cdev->dev, &(mmio_cdev->entries[i].attr));
return ret;
}
EXPORT_SYMBOL_GPL(mmio_classdev_register);
/**
* mmio_classdev_unregister - unregisters a object of mmio_classdev class.
* @mmio_cdev: the mmio device to unregister
*
* Unregisters a previously registered via led_classdev_register object.
*/
void mmio_classdev_unregister(struct mmio_classdev *mmio_cdev)
{
int i;
for (i = 0; i < mmio_cdev->num_entries; i++)
{
device_remove_file(mmio_cdev->dev, &(mmio_cdev->entries[i].attr));
}
device_unregister(mmio_cdev->dev);
down_write(&mmio_list_lock);
list_del(&mmio_cdev->node);
up_write(&mmio_list_lock);
}
EXPORT_SYMBOL_GPL(mmio_classdev_unregister);
static int __init mmio_init(void)
{
mmio_class = class_create(THIS_MODULE, "mmio");
if (IS_ERR(mmio_class))
return PTR_ERR(mmio_class);
return 0;
}
static void __exit mmio_exit(void)
{
class_destroy(mmio_class);
}
subsys_initcall(mmio_init);
module_exit(mmio_exit);
MODULE_AUTHOR("Joe Balough <[email protected]>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("MMIO Class Interface");