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
334
335
336
|
/*
* Copyright (C) 2012 Spreadtrum Communications Inc.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/poll.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#ifdef CONFIG_OF
#include <linux/of_device.h>
#endif
#include <linux/sipc.h>
#include <linux/spipe.h>
struct spipe_device {
struct spipe_init_data *init;
int major;
int minor;
struct cdev cdev;
};
struct spipe_sbuf {
uint8_t dst;
uint8_t channel;
uint32_t bufid;
};
static struct class *spipe_class;
static int spipe_open(struct inode *inode, struct file *filp)
{
int minor = iminor(filp->f_path.dentry->d_inode);
struct spipe_device *spipe;
struct spipe_sbuf *sbuf;
spipe = container_of(inode->i_cdev, struct spipe_device, cdev);
if (sbuf_status(spipe->init->dst, spipe->init->channel) != 0) {
printk(KERN_ERR "spipe %d-%d not ready to open!\n", spipe->init->dst, spipe->init->channel);
filp->private_data = NULL;
return -ENODEV;
}
sbuf = kmalloc(sizeof(struct spipe_sbuf), GFP_KERNEL);
if (!sbuf) {
return -ENOMEM;
}
filp->private_data = sbuf;
sbuf->dst = spipe->init->dst;
sbuf->channel = spipe->init->channel;
sbuf->bufid = minor - spipe->minor;
return 0;
}
static int spipe_release(struct inode *inode, struct file *filp)
{
struct spipe_sbuf *sbuf = filp->private_data;
if (sbuf) {
kfree(sbuf);
}
return 0;
}
static ssize_t spipe_read(struct file *filp,
char __user *buf, size_t count, loff_t *ppos)
{
struct spipe_sbuf *sbuf = filp->private_data;
int timeout = -1;
if (filp->f_flags & O_NONBLOCK) {
timeout = 0;
}
return sbuf_read(sbuf->dst, sbuf->channel, sbuf->bufid,
(void *)buf, count, timeout);
}
static ssize_t spipe_write(struct file *filp,
const char __user *buf, size_t count, loff_t *ppos)
{
struct spipe_sbuf *sbuf = filp->private_data;
int timeout = -1;
if (filp->f_flags & O_NONBLOCK) {
timeout = 0;
}
return sbuf_write(sbuf->dst, sbuf->channel, sbuf->bufid,
(void *)buf, count, timeout);
}
static unsigned int spipe_poll(struct file *filp, poll_table *wait)
{
struct spipe_sbuf *sbuf = filp->private_data;
return sbuf_poll_wait(sbuf->dst, sbuf->channel, sbuf->bufid,
filp, wait);
}
static long spipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
return 0;
}
static const struct file_operations spipe_fops = {
.open = spipe_open,
.release = spipe_release,
.read = spipe_read,
.write = spipe_write,
.poll = spipe_poll,
.unlocked_ioctl = spipe_ioctl,
.owner = THIS_MODULE,
.llseek = default_llseek,
};
static int spipe_parse_dt(struct spipe_init_data **init, struct device *dev)
{
#ifdef CONFIG_OF
struct device_node *np = dev->of_node;
struct spipe_init_data *pdata = NULL;
int ret;
uint32_t data;
pdata = kzalloc(sizeof(struct spipe_init_data), GFP_KERNEL);
if (!pdata) {
printk(KERN_ERR "Failed to allocate pdata memory\n");
return -ENOMEM;
}
ret = of_property_read_string(np, "sprd,name", (const char**)&pdata->name);
if (ret) {
goto error;
}
ret = of_property_read_u32(np, "sprd,dst", (uint32_t *)&data);
if (ret) {
goto error;
}
pdata->dst = (uint8_t)data;
ret = of_property_read_u32(np, "sprd,channel", (uint32_t *)&data);
if (ret) {
goto error;
}
pdata->channel = (uint8_t)data;
ret = of_property_read_u32(np, "sprd,ringnr", (uint32_t *)&pdata->ringnr);
if (ret) {
goto error;
}
ret = of_property_read_u32(np, "sprd,size-rxbuf", (uint32_t *)&pdata->rxbuf_size);
if (ret) {
goto error;
}
ret = of_property_read_u32(np, "sprd,size-txbuf", (uint32_t *)&pdata->txbuf_size);
if (ret) {
goto error;
}
*init = pdata;
return ret;
error:
kfree(pdata);
*init = NULL;
return ret;
#else
return -ENODEV;
#endif
}
static inline void spipe_destroy_pdata(struct spipe_init_data **init)
{
#ifdef CONFIG_OF
struct spipe_init_data *pdata = *init;
if (pdata) {
kfree(pdata);
}
*init = NULL;
#else
return;
#endif
}
static int spipe_probe(struct platform_device *pdev)
{
struct spipe_init_data *init = pdev->dev.platform_data;
struct spipe_device *spipe;
dev_t devid;
int i, rval;
if (pdev->dev.of_node && !init) {
rval = spipe_parse_dt(&init, &pdev->dev);
if (rval) {
printk(KERN_ERR "Failed to parse spipe device tree, ret=%d\n", rval);
return rval;
}
}
pr_info("spipe: after parse device tree, name=%s, dst=%u, channel=%u, ringnr=%u, rxbuf_size=%u, txbuf_size=%u\n",
init->name, init->dst, init->channel, init->ringnr, init->rxbuf_size, init->txbuf_size);
rval = sbuf_create(init->dst, init->channel, init->ringnr,
init->txbuf_size, init->rxbuf_size);
if (rval != 0) {
printk(KERN_ERR "Failed to create sbuf: %d\n", rval);
spipe_destroy_pdata(&init);
return rval;
}
spipe = kzalloc(sizeof(struct spipe_device), GFP_KERNEL);
if (spipe == NULL) {
sbuf_destroy(init->dst, init->channel);
spipe_destroy_pdata(&init);
printk(KERN_ERR "Failed to allocate spipe_device\n");
return -ENOMEM;
}
rval = alloc_chrdev_region(&devid, 0, init->ringnr, init->name);
if (rval != 0) {
sbuf_destroy(init->dst, init->channel);
kfree(spipe);
spipe_destroy_pdata(&init);
printk(KERN_ERR "Failed to alloc spipe chrdev\n");
return rval;
}
cdev_init(&(spipe->cdev), &spipe_fops);
rval = cdev_add(&(spipe->cdev), devid, init->ringnr);
if (rval != 0) {
sbuf_destroy(init->dst, init->channel);
kfree(spipe);
unregister_chrdev_region(devid, init->ringnr);
spipe_destroy_pdata(&init);
printk(KERN_ERR "Failed to add spipe cdev\n");
return rval;
}
spipe->major = MAJOR(devid);
spipe->minor = MINOR(devid);
if (init->ringnr > 1) {
for (i = 0; i < init->ringnr; i++) {
device_create(spipe_class, NULL,
MKDEV(spipe->major, spipe->minor + i),
NULL, "%s%d", init->name, i);
}
} else {
device_create(spipe_class, NULL,
MKDEV(spipe->major, spipe->minor),
NULL, "%s", init->name);
}
spipe->init = init;
platform_set_drvdata(pdev, spipe);
return 0;
}
static int spipe_remove(struct platform_device *pdev)
{
struct spipe_device *spipe = platform_get_drvdata(pdev);
int i;
for (i = 0; i < spipe->init->ringnr; i++) {
device_destroy(spipe_class,
MKDEV(spipe->major, spipe->minor + i));
}
cdev_del(&(spipe->cdev));
unregister_chrdev_region(
MKDEV(spipe->major, spipe->minor), spipe->init->ringnr);
sbuf_destroy(spipe->init->dst, spipe->init->channel);
spipe_destroy_pdata(&spipe->init);
kfree(spipe);
platform_set_drvdata(pdev, NULL);
return 0;
}
static const struct of_device_id spipe_match_table[] = {
{.compatible = "sprd,spipe", },
{ },
};
static struct platform_driver spipe_driver = {
.driver = {
.owner = THIS_MODULE,
.name = "spipe",
.of_match_table = spipe_match_table,
},
.probe = spipe_probe,
.remove = spipe_remove,
};
static int __init spipe_init(void)
{
spipe_class = class_create(THIS_MODULE, "spipe");
if (IS_ERR(spipe_class))
return PTR_ERR(spipe_class);
return platform_driver_register(&spipe_driver);
}
static void __exit spipe_exit(void)
{
class_destroy(spipe_class);
platform_driver_unregister(&spipe_driver);
}
module_init(spipe_init);
module_exit(spipe_exit);
MODULE_AUTHOR("Chen Gaopeng");
MODULE_DESCRIPTION("SIPC/SPIPE driver");
MODULE_LICENSE("GPL");
|