-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnand1k.c
198 lines (164 loc) · 4.62 KB
/
nand1k.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
/*
* nand1k.c
*
* Copyright (C) 2013 Qiang Yu <[email protected]>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include "nfc.h"
//////////////////////////////////////////////////////////////////////////////
// Linux charactor device interface
//////////////////////////////////////////////////////////////////////////////
#define DEV_CLASS_NAME "nand1k"
#define CHAR_DEV_NAME "nand1k"
static struct class *dev_class;
static int nand1k_major;
static char *rw_buff;
static int nand1k_open(struct inode *inode, struct file *file)
{
return 0;
}
static int nand1k_close(struct inode *inode, struct file *file)
{
return 0;
}
static int nand1k_read(struct file *filp, char __user *buff, size_t count, loff_t *f_pos)
{
loff_t offs = *f_pos;
uint32_t len, ret, page, offset;
size_t size = 0;
printk(KERN_INFO "nand1k read off=%llx count=%x\n", offs, count);
if (offs > 128 * 1024 || offs < 0 ||
count < 0 || count > 128 * 1024 ||
offs + count > 128 * 1024) {
printk(KERN_ERR "nand1k is restricted to access the first 128 1K pages\n");
return -EINVAL;
}
while (size < count) {
page = offs / 1024;
offset = offs % 1024;
len = 1024 - offset;
if (len > count - size)
len = count - size;
nfc_read_page1k(page, rw_buff);
ret = copy_to_user(buff, rw_buff + offset, len);
printk(KERN_INFO "nand1k read page=%x offset=%x len=%x\n", page, offset, len);
printk(KERN_INFO "nand1k %x %x %x %x %x %x %x %x\n",
rw_buff[0], rw_buff[1], rw_buff[2], rw_buff[3],
rw_buff[4], rw_buff[5], rw_buff[6], rw_buff[7]);
size += len - ret;
offs += len - ret;
buff += len - ret;
if (ret)
break;
}
*f_pos += size;
return size;
}
static int nand1k_write(struct file *filp, const char __user *buff, size_t count, loff_t *f_pos)
{
loff_t offs = *f_pos;
uint32_t ret;
size_t size = 0;
printk(KERN_INFO "nand1k write off=%llx count=%x\n", offs, count);
if (offs > 128 * 1024 || offs < 0 ||
count < 0 || count > 128 * 1024 ||
offs + count > 128 * 1024) {
printk(KERN_ERR "nand1k is restricted to access the first 128 1K pages\n");
return -EINVAL;
}
if ((offs & (1024 - 1)) || (count & (1024 - 1))) {
printk(KERN_ERR "nand1k can't write non-1K-aligned data\n");
return -EINVAL;
}
while (size < count) {
ret = copy_from_user(rw_buff, buff, 1024);
if (ret)
break;
nfc_write_page1k(offs / 1024, rw_buff);
size += 1024;
offs += 1024;
buff += 1024;
}
*f_pos += size;
return size;
}
static long nand1k_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
default:
return -ENOTTY;
}
return 0;
}
struct file_operations nand1k_fops = {
.read = nand1k_read,
.write = nand1k_write,
.open = nand1k_open,
.release = nand1k_close,
.unlocked_ioctl = nand1k_ioctl,
};
int nand1k_init(void)
{
int err;
struct device *dev;
rw_buff = kmalloc(1024, GFP_KERNEL);
if (!rw_buff) {
printk(KERN_ERR "allocate read buffer fail\n");
err = -ENOMEM;
goto error0;
}
dev_class = class_create(THIS_MODULE, DEV_CLASS_NAME);
if (IS_ERR(dev_class)) {
printk(KERN_ERR "Create device class error\n");
err = PTR_ERR(dev_class);
goto error1;
}
nand1k_major = register_chrdev(0, CHAR_DEV_NAME, &nand1k_fops);
if (nand1k_major < 0) {
printk(KERN_ERR "register_chrdev fail\n");
err = nand1k_major;
goto error2;
}
// Send uevents to udev, so it'll create /dev nodes
dev = device_create(dev_class, NULL, MKDEV(nand1k_major, 0), NULL, CHAR_DEV_NAME);
if (IS_ERR(dev)) {
printk(KERN_ERR "device_create fail\n");
err = PTR_ERR(dev);
goto error3;
}
return 0;
error3:
unregister_chrdev(nand1k_major, CHAR_DEV_NAME);
error2:
class_destroy(dev_class);
error1:
kfree(rw_buff);
error0:
return err;
}
void nand1k_exit(void)
{
device_destroy(dev_class, MKDEV(nand1k_major, 0));
unregister_chrdev(nand1k_major, CHAR_DEV_NAME);
class_destroy(dev_class);
kfree(rw_buff);
}