hc
2023-12-06 d38611ca164021d018c1b23eee65bbebc09c63e0
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
// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
 
/* Copyright (c) 2023 Rockchip Electronics Co., Ltd */
 
#include <linux/kernel.h>
#include <linux/debugfs.h>
#include <linux/dma-mapping.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/list.h>
#include <linux/io.h>
#include <linux/mempolicy.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/of_reserved_mem.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/soc/rockchip/rk_vendor_storage.h>
#include <linux/uaccess.h>
#include <misc/rkflash_vendor_storage.h>
 
static struct vendor_info *g_vendor;
 
static int ram_vendor_read(u32 id, void *pbuf, u32 size)
{
   u32 i;
 
   if (!g_vendor)
       return -ENOMEM;
 
   for (i = 0; i < g_vendor->item_num; i++) {
       if (g_vendor->item[i].id == id) {
           if (size > g_vendor->item[i].size)
               size = g_vendor->item[i].size;
           memcpy(pbuf, &g_vendor->data[g_vendor->item[i].offset], size);
           return size;
       }
   }
 
   return (-1);
}
 
static int ram_vendor_storage_open(struct inode *inode, struct file *file)
{
   return 0;
}
 
static int ram_vendor_storage_release(struct inode *inode, struct file *file)
{
   return 0;
}
 
static long ram_vendor_storage_ioctl(struct file *file, unsigned int cmd,
                unsigned long arg)
{
   long ret = -1;
   int size;
   struct RK_VENDOR_REQ *v_req;
   u32 *page_buf;
 
   page_buf = kmalloc(4096, GFP_KERNEL);
   if (!page_buf)
       return -ENOMEM;
 
   v_req = (struct RK_VENDOR_REQ *)page_buf;
 
   switch (cmd) {
   case VENDOR_READ_IO:
   {
       if (copy_from_user(page_buf, (void __user *)arg, 8)) {
           ret = -EFAULT;
           break;
       }
       if (v_req->tag == VENDOR_REQ_TAG && v_req->len <= 4096 - 8) {
           size = ram_vendor_read(v_req->id, v_req->data, v_req->len);
           if (size != -1) {
               v_req->len = size;
               ret = 0;
               if (copy_to_user((void __user *)arg,
                        page_buf,
                        v_req->len + 8))
                   ret = -EFAULT;
           }
       }
   } break;
 
   case VENDOR_WRITE_IO:
   default:
       ret = -EINVAL;
       goto exit;
   }
exit:
   kfree(page_buf);
   return ret;
}
 
static const struct file_operations vendor_storage_fops = {
   .open = ram_vendor_storage_open,
   .compat_ioctl = ram_vendor_storage_ioctl,
   .unlocked_ioctl = ram_vendor_storage_ioctl,
   .release = ram_vendor_storage_release,
};
 
static struct miscdevice vender_storage_dev = {
   .minor = MISC_DYNAMIC_MINOR,
   .name  = "vendor_storage",
   .fops  = &vendor_storage_fops,
};
 
static void *ram_vendor_stroage_map(phys_addr_t start, size_t len)
{
   int i;
   void *vaddr;
   pgprot_t pgprot = PAGE_KERNEL;
   phys_addr_t phys;
   int npages = PAGE_ALIGN(len) / PAGE_SIZE;
   struct page **p = vmalloc(sizeof(struct page *) * npages);
 
   if (!p)
       return NULL;
 
   phys = start;
   for (i = 0; i < npages; i++) {
       p[i] = phys_to_page(phys);
       phys += PAGE_SIZE;
   }
 
   vaddr = vmap(p, npages, VM_MAP, pgprot);
   vfree(p);
 
   return vaddr;
}
 
static int ram_vendor_storage_probe(struct platform_device *pdev)
{
   struct device_node *np = pdev->dev.of_node;
   struct device_node *node;
   struct resource res;
   int ret;
   phys_addr_t size, start;
 
   if (g_vendor)
       return -EINVAL;
 
   node = of_parse_phandle(np, "memory-region", 0);
   if (!node)
       return -ENOMEM;
 
   ret = of_address_to_resource(node, 0, &res);
   if (ret)
       return ret;
 
   ret = -EINVAL;
 
   size = resource_size(&res);
   start = res.start;
   if (size != VENDOR_PART_SIZE << 9 || (start & (PAGE_SIZE - 1)))
       goto un_reserved;
 
   g_vendor = ram_vendor_stroage_map(start, size);
   if (IS_ERR(g_vendor))
       goto un_reserved;
 
   if (g_vendor->tag != VENDOR_HEAD_TAG)
       goto un_remap;
 
   misc_register(&vender_storage_dev);
   rk_vendor_register(ram_vendor_read, NULL);
 
   return 0;
 
un_remap:
   vunmap(g_vendor);
un_reserved:
#ifndef MODULE
   free_reserved_area(phys_to_virt(start), phys_to_virt(start) + size, -1, "memory-region");
#endif
   g_vendor = NULL;
 
   return ret;
}
 
static int ram_vendor_storage_remove(struct platform_device *pdev)
{
   if (g_vendor) {
       misc_deregister(&vender_storage_dev);
       vunmap(g_vendor);
       g_vendor = NULL;
   }
 
   return 0;
}
 
static const struct of_device_id dt_match[] = {
   { .compatible = "rockchip,ram-vendor-storage" },
   {}
};
 
static struct platform_driver vendor_storage_driver = {
   .probe        = ram_vendor_storage_probe,
   .remove        = ram_vendor_storage_remove,
   .driver        = {
       .name        = "vendor-storage",
       .of_match_table    = dt_match,
   },
};
 
module_platform_driver(vendor_storage_driver);
MODULE_LICENSE("GPL");