• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "fb_mem.h"
32 
33 #include "key_cache.h"
34 
35 
36 static UINT32 g_vnodeMux;
37 
FbMemInit(void)38 int FbMemInit(void)
39 {
40     int retval = LOS_MuxCreate(&g_vnodeMux);
41     if (retval != LOS_OK) {
42         PRINT_ERR("Create mutex for fbmem fail, status: %d", retval);
43         return retval;
44     }
45     return LOS_OK;
46 }
47 
FbMemHold(void)48 int FbMemHold(void)
49 {
50     int ret = LOS_MuxPend(g_vnodeMux, LOS_WAIT_FOREVER);
51     if (ret != LOS_OK) {
52         PRINT_ERR("FbMemHold lock failed !\n");
53     }
54     return ret;
55 }
56 
FbMemDrop(void)57 int FbMemDrop(void)
58 {
59     int ret = LOS_MuxPost(g_vnodeMux);
60     if (ret != LOS_OK) {
61         PRINT_ERR("FbMemDrop unlock failed !\n");
62     }
63     return ret;
64 }
65 
FbMemLookup(const char * key,struct fb_mem ** result,uint32_t flags)66 int FbMemLookup(const char *key, struct fb_mem **result, uint32_t flags)
67 {
68     struct fb_mem *fbmem = NULL;
69     size_t len = strlen(key);
70     int ret = 0;
71 
72     ret = KeyCacheLookup(key, len, &fbmem);
73     if (ret == LOS_OK) {
74         *result = fbmem;
75     } else {
76         if (flags & V_DUMMY) {
77             fbmem = (struct fb_mem *)zalloc(sizeof(struct fb_mem));
78             if (fbmem == NULL) {
79                 return LOS_NOK;
80             }
81             (void)KeyCacheAlloc(fbmem, key, len);
82             *result = fbmem;
83             ret = LOS_OK;
84         } else {
85             ret = -ENXIO;
86         }
87     }
88     return ret;
89 }
90 
register_driver(const char * key,void * prev)91 int register_driver(const char *key, void *prev)
92 {
93     struct fb_mem *fbmem = NULL;
94     int ret;
95     if (key == NULL || strlen(key) >= PATH_MAX) {
96         return -EINVAL;
97     }
98     FbMemHold();
99     ret = FbMemLookup(key, &fbmem, 0);
100     if (ret == 0) {
101         FbMemDrop();
102         return -EEXIST;
103     }
104 
105     ret = FbMemLookup(key, &fbmem, V_CREATE | V_DUMMY);
106     if (ret == OK) {
107         fbmem->data = prev;
108     }
109     FbMemDrop();
110     return ret;
111 }
112 
unregister_driver(const char * key)113 int unregister_driver(const char *key)
114 {
115     struct fb_mem *fbmem = NULL;
116     int ret;
117     if (key == NULL || strlen(key) >= PATH_MAX) {
118         return -EINVAL;
119     }
120     FbMemHold();
121     size_t len = strlen(key);
122     ret = KeyCacheLookup(key, len, &fbmem);
123     if (ret == LOS_OK) {
124         FbMemKeyCacheFree(fbmem);
125         free(fbmem);
126     }
127     FbMemDrop();
128     return ret;
129 }