1 /*
2 *******************************************************************************
3 *                                              usb host module
4 *
5 *                             Copyright(C), 2006-2008, SoftWinners Co., Ltd.
6 *                                                  All Rights Reserved
7 *
8 * File Name :
9 *
10 * Author : GLHuang(HoLiGun)
11 *
12 * Version : 1.0
13 *
14 * Date : 2008.05.xx
15 *
16 * Description :
17 *
18 * History :
19 *注意*:别忘记修改list 的头指针,我犯过这个错误。
20 ********************************************************************************************************************
21 */
22 
23 
24 
25 #ifndef _USB_LIST_H_
26 #define _USB_LIST_H_
27 
28 #include <log.h>
29 #include <hal_osal.h>
30 //#include "usb_host_config.h"
31 
32 
33 //#define   USE_USB_LIST_BUFF   0x01        //使用usb_list的buff机制
34 
35 struct usb_list_head
36 {
37     struct usb_list_head *next;
38     struct usb_list_head *prev;
39     void *data;
40 };
41 
42 #define __USB_LIST_HEAD_INIT(name) { &(name), &(name) ,NULL}
43 
44 //创建并初始化
45 #define USB_LIST_HEAD(name) \
46     struct usb_list_head name = __USB_LIST_HEAD_INIT(name)
47 
48 //只初始化
49 #define USB_INIT_LIST_HEAD(ptr) do { \
50         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
51         (ptr)->data = NULL;\
52     } while (0)
53 
54 /* 获得list中每个node的内容
55  *
56  * list_head  :  链表头
57  * list_next  :  下一个node
58  * data       :  node的内容
59  */
60 #define usb_list_for_each_entry(list_head, list_next, data) \
61     for((list_next) = (list_head)->next, (data) = list_next->data; \
62         (list_next) != (list_head);       \
63         (list_next) = (list_head)->next, (data) = list_next->data)
64 
65 //=========================================================
66 
67 //用这个的前提:prev,next必须是存在的,否则崩溃。
__usb_list_add(struct usb_list_head * node,struct usb_list_head * prev,struct usb_list_head * next)68 static inline void __usb_list_add(struct usb_list_head *node,
69                                   struct usb_list_head *prev,
70                                   struct usb_list_head *next)
71 {
72     node->next = next;
73     node->prev = prev;
74     prev->next = node;
75     next->prev = node;
76 }
77 
78 //将node添加到head的后面
usb_list_add(struct usb_list_head * node,struct usb_list_head * head)79 static inline void usb_list_add(struct usb_list_head *node, struct usb_list_head *head)
80 {
81     __usb_list_add(node, head, head->next);
82 }
83 
84 //将node添加到head的最后面,既其前面
usb_list_add_tail(struct usb_list_head * node,struct usb_list_head * head)85 static inline void usb_list_add_tail(struct usb_list_head *node, struct usb_list_head *head)
86 {
87     __usb_list_add(node, head->prev, head);
88 }
89 
90 //=========================================================
91 
92 //内部函数,删除prev,next之间的
__usb_list_del(struct usb_list_head * prev,struct usb_list_head * next)93 static inline void __usb_list_del(struct usb_list_head *prev, struct usb_list_head *next)
94 {
95     next->prev = prev;
96     prev->next = next;
97 }
98 
99 //从list中删除entry
usb_list_del(struct usb_list_head * entry)100 static inline void usb_list_del(struct usb_list_head *entry)
101 {
102     __usb_list_del(entry->prev, entry->next);
103     //entry->next = LIST_POISON1;
104     //entry->prev = LIST_POISON2;
105     entry->next = entry;
106     entry->prev = entry;
107 }
108 
109 //比list_del多清除了指向data的
usb_list_del_init(struct usb_list_head * entry)110 static inline void usb_list_del_init(struct usb_list_head *entry)
111 {
112     __usb_list_del(entry->prev, entry->next);
113     USB_INIT_LIST_HEAD(entry);
114 }
115 
116 //判断是否为空
usb_list_empty(const struct usb_list_head * head)117 static inline int usb_list_empty(const struct usb_list_head *head)
118 {
119     return head->next == head;
120 }
121 
122 //-------------------------------------------------------------------
123 //
124 //
125 //-------------------------------------------------------------------
126 __s32 ListMemoryInit(void);
127 __s32 ListMemoryExit(void);
128 void *ListMemoryMalloc(__u32 size, u8 *file_name, u32 line_nr);
129 void ListMemoryFree(void *addr, u8 *file_name, u32 line_nr);
130 
131 
132 /* list_head结构的分配和init */
_list_head_malloc_init(void)133 static struct usb_list_head *_list_head_malloc_init(void)
134 {
135     struct usb_list_head *list = NULL;
136     //先从buff中分配
137     //list = ListMemoryMalloc(sizeof(struct usb_list_head), USB_MEM_FILE_TRIGGER, USB_MEM_LINE_TRIGGER);
138     list = hal_malloc(sizeof(struct usb_list_head));
139 
140     if (list == NULL)
141     {
142         __err("PANIC : _list_head_malloc_init() : no mem\n");
143         return NULL;
144     }
145 
146     USB_INIT_LIST_HEAD(list);
147     return list;
148 }
149 
150 /* list_head结构的释放 */
_usb_list_head_free(struct usb_list_head * list)151 static void _usb_list_head_free(struct usb_list_head *list)
152 {
153     if (list == NULL)
154     {
155         __err("PANIC : _list_head_free() : try to free NULL\n");
156         return ;
157     }
158 
159     //ListMemoryFree((void *)list, USB_MEM_FILE_TRIGGER, USB_MEM_LINE_TRIGGER);
160     hal_free((void *)list);
161     return ;
162 }
163 
164 /*
165 ********************************************************************************
166 *                     list_head_malloc_and_add
167 *
168 * Description:
169 *     创建list_head并将data挂载到list_head,同时添加到目标list
170 * 与list_head_malloc_and_add()配对使用
171 *
172 * Arguments:
173 *     data  : input. 需要加入队列的数据
174 *     head  : input. 队列的头
175 * Return value:
176 *     返回 head->data
177 * note:
178 *     void
179 *********************************************************************************
180 */
list_head_malloc_and_add(void * data,struct usb_list_head * head)181 static inline void list_head_malloc_and_add(void *data, struct usb_list_head *head)
182 {
183     __u32 lock = 0;
184     struct usb_list_head *list = _list_head_malloc_init();
185 
186     if (list == NULL)
187     {
188         __err("ERR: _list_head_malloc_init failed\n");
189         return;
190     }
191 
192     //USB_OS_ENTER_CRITICAL(lock);
193     list->data = data;
194     usb_list_add_tail(list, head);
195     //USB_OS_EXIT_CRITICAL(lock);
196     return;
197 }
198 
199 /*
200 ********************************************************************************
201 *                     list_head_unlink_and_del
202 *
203 * Description:
204 *     从list中删除head这个node ,且free这个node的内存, 返回这个node的内容
205 * 与list_head_malloc_and_add()配对使用
206 *
207 * Arguments:
208 *     head  : input. 即将被删除的node
209 * Return value:
210 *     返回 head->data
211 * note:
212 *     void
213 *********************************************************************************
214 */
list_head_unlink_and_del(struct usb_list_head * head)215 static inline void *list_head_unlink_and_del(struct usb_list_head *head)
216 {
217     void *ret = NULL;
218     __u32 lock = 0;
219 
220     if (head == NULL)
221     {
222         __err("PANIC : list_head_unlink_and_del() : try to free NULL\n");
223         return NULL ;
224     }
225 
226     //--1--unlink from list
227     //USB_OS_ENTER_CRITICAL(lock);
228     usb_list_del(head);
229     ret = head->data;
230     //USB_OS_EXIT_CRITICAL(lock);
231     _usb_list_head_free(head);
232     return ret;
233 }
234 
235 #endif
236 
237 
238