-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.c
114 lines (88 loc) · 2.4 KB
/
importer.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
#include <linux/dma-buf.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <kunit/test.h>
#include "importer.h"
extern struct dma_buf *dmabuf_exported;
static int importer_test_sg(struct dma_buf *dmabuf)
{
struct dma_buf_attachment *attachment;
struct sg_table *table;
struct device *dev;
unsigned int reg_addr, reg_size;
if (!dmabuf)
return -EINVAL;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev)
return -EINVAL;
dev_set_name(dev, "importer");
attachment = dma_buf_attach(dmabuf, dev);
if (IS_ERR(attachment)) {
print_err("dma_buf_attach() failed\n");
return PTR_ERR(attachment);
}
table = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
if (IS_ERR(table)) {
print_err("dma_buf_map_attachment() failed\n");
dma_buf_detach(dmabuf, attachment);
}
reg_addr = sg_dma_address(table->sgl);
reg_size = sg_dma_len(table->sgl);
print_info("reg_addr = 0x%08x, reg_size = 0x%08x\n", reg_addr, reg_size);
dma_buf_unmap_attachment(attachment, table, DMA_BIDIRECTIONAL);
dma_buf_detach(dmabuf, attachment);
kfree(dev);
return 0;
}
static int importer_test_vmap(struct dma_buf *dmabuf)
{
struct iosys_map map;
if (!dmabuf) {
print_err("dmabuf_exported is null\n");
return -EINVAL;
}
dma_buf_vmap(dmabuf, &map);
print_info("read from dmabuf vmap: %s\n", (char *)map.vaddr);
dma_buf_vunmap(dmabuf, &map);
return 0;
}
static void kunit_importer_test_sg(struct kunit *test)
{
KUNIT_EXPECT_EQ(test, importer_test_sg(dmabuf_exported), 0);
}
static void kunit_importer_test_vmap(struct kunit *test)
{
KUNIT_EXPECT_EQ(test, importer_test_vmap(dmabuf_exported), 0);
}
static struct kunit_case kunit_importer_test_cases[] = {
KUNIT_CASE(kunit_importer_test_sg),
KUNIT_CASE(kunit_importer_test_vmap),
{},
};
static struct kunit_suite kunit_importer_test_suite = {
.name = "kunit_importer_test",
.test_cases = kunit_importer_test_cases,
};
static int __init importer_init(void)
{
int ret;
print_info("start\n");
ret = kunit_run_tests(&kunit_importer_test_suite);
if (ret)
print_err("Kunit tests failed: %d\n", ret);
else
print_info("Kunit tests passed\n");
print_info("end\n");
return 0;
}
static void __exit importer_exit(void)
{
print_info("start\n");
print_info("end\n");
}
module_init(importer_init);
module_exit(importer_exit);
MODULE_AUTHOR("Jihyuk Park");
MODULE_IMPORT_NS(DMA_BUF);
MODULE_DESCRIPTION("DMA-BUF example for IMPORTER");
MODULE_LICENSE("GPL v2");