1 /* ----------------------------------------------------------------------------
2 * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
3 * Description: LiteOS USB Driver Composite Devices
4 * Author: Yannik Li
5 * Create: 2021-02-21
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
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 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without specific prior written
15 * permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * --------------------------------------------------------------------------- */
28 /* ----------------------------------------------------------------------------
29 * Notice of Export Control Law
30 * ===============================================
31 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
32 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
33 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
34 * applicable export control laws and regulations.
35 * --------------------------------------------------------------------------- */
36
37 #include "f_common.h"
38
39 #ifdef __cplusplus
40 #if __cplusplus
41 //extern "C" {
42 #endif /* __cplusplus */
43 #endif /* __cplusplus */
44
45 struct obj_res {
46 struct list_head entry;
47 uint8_t data[0];
48 };
49
objres_alloc(size_t size)50 void *objres_alloc(size_t size)
51 {
52 size_t total_size;
53 struct obj_res *res;
54
55 total_size = sizeof(struct obj_res) + size;
56 res = malloc(total_size);
57 if (res == NULL)
58 {
59 return NULL;
60 }
61
62 INIT_LIST_HEAD(&res->entry);
63 return res->data;
64 }
65
objres_free(void * res)66 void objres_free(void *res)
67 {
68 if (res)
69 {
70 struct obj_res *objRes = container_of(res, struct obj_res, data);
71 free(objRes);
72 }
73 }
74
objres_add(struct usb_obj * obj,void * res)75 void objres_add(struct usb_obj *obj, void *res)
76 {
77 struct obj_res *objRes = container_of(res, struct obj_res, data);
78 uint32_t flags;
79
80 spin_lock_irqsave(&obj->objres_lock, flags);
81 list_add_tail(&objRes->entry, &obj->objres_head);
82 spin_unlock_irqrestore(&obj->objres_lock, flags);
83 }
84
objres_find(struct usb_obj * obj,void * res)85 static struct obj_res *objres_find(struct usb_obj *obj, void *res)
86 {
87 struct obj_res *rc;
88
89 list_for_each_entry_reverse(rc, &obj->objres_head, entry)
90 {
91 if (rc->data != res)
92 {
93 continue;
94 }
95 return rc;
96 }
97
98 return NULL;
99 }
100
objres_remove(struct usb_obj * obj,void * res)101 int objres_remove(struct usb_obj *obj, void *res)
102 {
103 struct obj_res *rc;
104 uint32_t flags;
105
106 spin_lock_irqsave(&obj->objres_lock, flags);
107 rc = objres_find(obj, res);
108 if (rc)
109 {
110 list_del_init(&rc->entry);
111 }
112 spin_unlock_irqrestore(&obj->objres_lock, flags);
113
114 return rc ? 0 : -1;
115 }
116
objres_release_all(struct usb_obj * obj)117 void objres_release_all(struct usb_obj *obj)
118 {
119 struct obj_res *res, *tmp;
120 uint32_t flags;
121
122 spin_lock_irqsave(&obj->objres_lock, flags);
123 list_for_each_entry_safe_reverse(res, tmp, &obj->objres_head, entry)
124 {
125 list_del(&res->entry);
126 free(res);
127 }
128 spin_unlock_irqrestore(&obj->objres_lock, flags);
129 }
130
usbm_malloc(struct usb_obj * obj,size_t size)131 void *usbm_malloc(struct usb_obj *obj, size_t size)
132 {
133 void *res;
134
135 if (!size)
136 {
137 return NULL;
138 }
139
140 res = objres_alloc(size);
141 if (res == NULL)
142 {
143 return NULL;
144 }
145
146 objres_add(obj, res);
147 return res;
148 }
149
usbm_zalloc(struct usb_obj * obj,size_t size)150 void *usbm_zalloc(struct usb_obj *obj, size_t size)
151 {
152 void *data;
153
154 data = usbm_malloc(obj, size);
155 if (data)
156 {
157 (void)memset_s(data, size, 0, size);
158 }
159 return data;
160 }
161
usbm_free(struct usb_obj * obj,void * p)162 void usbm_free(struct usb_obj *obj, void *p)
163 {
164 int ret;
165
166 if (p == NULL)
167 {
168 return;
169 }
170
171 ret = objres_remove(obj, p);
172 if (!ret)
173 {
174 objres_free(p);
175 }
176 }
177
usbm_strdup(struct usb_obj * obj,const char * s)178 char *usbm_strdup(struct usb_obj *obj, const char *s)
179 {
180 size_t size;
181 char *buf;
182
183 if (s == NULL)
184 {
185 return NULL;
186 }
187
188 size = strlen(s) + 1;
189 buf = usbm_malloc(obj, size);
190 if (buf)
191 {
192 (void)memcpy_s(buf, size, s, size);
193 }
194 return buf;
195 }
196
197 #ifdef __cplusplus
198 #if __cplusplus
199 //}
200 #endif /* __cplusplus */
201 #endif /* __cplusplus */
202