1 /*===============================================================================================*
2 * *
3 * MMMMMMMM MMMMMMM MMMMMMM MMMMMMMM MMMMMMMM MM MM MMMMMMMMMMMMM *
4 * MMM MMM MMM MMM MM MM . MM MMM MM *
5 * MMM MMM MMM MMM MM MM MM MMM MM. *
6 * MMM MMM MMM MMM MM . MM MM MMM MMMM *
7 * MMM MMM MMM MMM MM MMMM MMM MM *
8 * MMMMMMMMMMMM MMM MMM MM MMMM MMMMMMMMMMM *
9 * MMM MMM MMM MMM MM MMMM MMM . *
10 * MMM MMM MMM MMM MM . MM MMM MM *
11 * MMM MMM MMM MMM . MM MMMM MMM MMMM *
12 * MMM MMM MM MM MM MM. MMM MM *
13 * MMM MMM . MM MM. MMMM MM MMM MM *
14 * MMMMMMMM MMMMMMM MM. MMMMM MM MMMM MMMMMMMMMMMMM. *
15 * *
16 *================================================================================================
17 *
18 * usb host module
19 *
20 * Copyright(C), 2006-2008, SoftWinners Co., Ltd.
21 * All Rights Reserved
22 *
23 * File Name :
24 *
25 * Author : GLHuang(HoLiGun)
26 *
27 * Version : 1.0
28 *
29 * Date : 2008.07.01
30 *
31 * Description :
32 *
33 * History :
34 *================================================================================================
35 */
36 //#include "usb_host_config.h"
37
38 //#include "usb_os_platform.h"
39 #include <usb_host_common.h>
40
41 //#include "usb_host_base_types.h"
42 //#include "sunxi_drv_list.h"
43
44 #include <log.h>
45 #include <string.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48
49 #include <list_head_ext.h>
50 #include <usb_list.h>
51
52
53 static struct usb_virt_bus my_usb_virt_bus; //super bus
54 static hal_spinlock_t device_lock;
55
56 /*
57 ********************************************************************************
58 * _usb_virt_bus_remov_dev_from_drv
59 * Description:
60 * 将device从driver的virt_dev_list列表中删除。
61 * Arguments:
62 * dev : input. 即将被删除的device
63 * drv : input. 驱动
64 * Return value:
65 * void
66 * note:
67 * 不用关心device是否存在于virt_dev_list列表中
68 *
69 *********************************************************************************
70 */
_usb_virt_bus_remov_dev_from_drv(struct usb_host_virt_sub_dev * dev,struct usb_host_func_drv * drv)71 static void _usb_virt_bus_remov_dev_from_drv(struct usb_host_virt_sub_dev *dev,
72 struct usb_host_func_drv *drv)
73 {
74 /* check input */
75 if (dev == NULL || drv == NULL)
76 {
77 __err("PANIC : [VIRR BUS] : _usb_virt_bus_remov_dev_from_drv() input == NULL");
78 return ;
79 }
80
81 /* 队列为空,就不用删除了 */
82 if (usb_list_empty(&(drv->virt_dev_list)))
83 {
84 __err("PANIC : [VIRR BUS] : _usb_virt_bus_remov_dev_from_drv() func_drv->dev_list is empty");
85 return ;
86 }
87
88 /* del device from driver list */
89 list_head_ext_remov_node_from_list(dev, &(drv->virt_dev_list));
90 }
91
92
93 /*
94 ********************************************************************************
95 * usb_virt_bus_drv_reg
96 * Description:
97 * 将driver添加到总线上
98 * Arguments:
99 * drv : input. 待注册的驱动
100 * Return value:
101 *
102 * note:
103 * 一个驱动不能注册两次?????????
104 *********************************************************************************
105 */
usb_virt_bus_drv_reg(struct usb_host_func_drv * drv)106 int32_t usb_virt_bus_drv_reg(struct usb_host_func_drv *drv)
107 {
108 struct usb_list_head *list_start = NULL;
109 struct usb_list_head *list_now = NULL;
110 struct usb_host_virt_sub_dev *dev = NULL;
111 if (drv == NULL)
112 {
113 __err("PANIC : [VIRR BUS] : usb_virt_bus_drv_reg() input == NULL");
114 return -1;
115 }
116
117 /* 新注册的驱动是没有设备与其匹配的 */
118 if (! usb_list_empty(&(drv->virt_dev_list)))
119 {
120 __err("PANIC : [VIRR BUS] : usb_virt_bus_drv_reg() drv`s dev_list not empty");
121 return -1;
122 }
123
124 /* bus上已经存在driver, 就不要再添加了 */
125 if (list_node_exist((void *)drv, &(my_usb_virt_bus.drv_list)) == 0)
126 {
127 __err("ERR: drv is already in the list");
128 return 0;
129 }
130
131 hal_log_info("--usb_virt_bus_drv_reg---1-2--\n");
132 //UsbLock(my_usb_virt_bus.BusLock);
133 hal_sem_wait(my_usb_virt_bus.BusLock);
134
135 /* 将driver添加到bus上 */
136 list_head_malloc_and_add(drv, &(my_usb_virt_bus.drv_list));
137 hal_log_info("--usb_virt_bus_drv_reg---1-3--\n");
138 /* 先前可能已经有device连上bus了, 这里就去寻找bus上与之匹配的deivce */
139 list_start = &(my_usb_virt_bus.dev_list);
140 list_now = list_start->next;
141
142 while (list_start != list_now)
143 {
144 dev = (struct usb_host_virt_sub_dev *)(list_now->data);
145
146 if (dev == NULL)
147 {
148 __err("dev == NULL", dev);
149 break;
150 }
151
152 list_now = list_now->next;
153 //UsbLock(dev->usb_virt_sub_dev_semi);
154 hal_sem_wait(dev->usb_virt_sub_dev_semi);
155
156 /* 找到没有被bind的dev */
157 if (dev->func_drv == NULL)
158 {
159 /* 事先尝试bind到一起 */
160 dev->func_drv = drv;
161 hal_log_info("---usb_virt_bus_drv_reg----1---\n");
162 if (drv->func_drv_ext.probe_ext(dev) == 0)
163 {
164 /* device 和 driver 匹配成功, 就将 device 添加到 driver 的virt_dev_list中*/
165 list_head_malloc_and_add(dev, &(drv->virt_dev_list));
166 }
167 else
168 {
169 /* 匹配失败, 就清空。device无人认领 */
170 dev->func_drv = NULL;
171 }
172 hal_log_info("---usb_virt_bus_drv_reg----2---\n");
173 }
174
175 //UsbUnLock(dev->usb_virt_sub_dev_semi);
176 hal_sem_post(dev->usb_virt_sub_dev_semi);
177 }
178
179 //UsbUnLock(my_usb_virt_bus.BusLock);
180 hal_sem_post(my_usb_virt_bus.BusLock);
181 return 0;
182 }
183
184 /*
185 ********************************************************************************
186 * usb_virt_bus_drv_unreg
187 * Description:
188 * 将driver从总线上摘掉
189 * Arguments:
190 * drv : input.
191 * Return value:
192 *
193 * note:
194 * 注意该期间,dev不能使用,得用dev->usb_virt_dev_semi来保护
195 * 因为这个期间dev属于临界区域,处于混沌状态
196
197 * 遍历该func_driver所支持的scsi_device list,既scsi_func_driver->scsi_dev_list
198 * 如果dev->drv == drv(该dev的驱动 == 本驱动)则做如下操作:
199 * 1,从drv->scsi_dev_list中删除本dev
200 * 2,将dev->drv == NULL
201 * 3,将调用drv->scsi_remove
202 *********************************************************************************
203 */
usb_virt_bus_drv_unreg(struct usb_host_func_drv * drv)204 int32_t usb_virt_bus_drv_unreg(struct usb_host_func_drv *drv)
205 {
206 struct usb_list_head *list_start = NULL;
207 struct usb_list_head *list_now = NULL;
208 struct usb_host_virt_sub_dev *dev = NULL;
209
210 if (drv == NULL)
211 {
212 __err("PANIC : [VIRR BUS] : usb_virt_bus_drv_unreg() input == NULL");
213 return -1;
214 }
215
216 //UsbLock(my_usb_virt_bus.BusLock);
217 hal_sem_wait(my_usb_virt_bus.BusLock);
218
219 /* 先unbind改drv与与之关联的dev */
220 if (usb_list_empty(&(drv->virt_dev_list)))
221 {
222 /* 既然bus上的driver队列都空了, 也就没有必要去删除driver了 */
223 }
224 else
225 {
226 /* 遍历driver的virt_dev_list, 删除所有的device */
227 list_start = &(drv->virt_dev_list);
228 list_now = list_start->next;
229
230 while (list_start != list_now)
231 {
232 dev = (struct usb_host_virt_sub_dev *)(list_now->data);
233
234 if (dev == NULL)
235 {
236 __err("dev == NULL", dev);
237 break;
238 }
239
240 list_now = list_now->next;
241 //UsbLock(dev->usb_virt_sub_dev_semi);
242 hal_sem_wait(dev->usb_virt_sub_dev_semi);
243
244 if (dev)
245 {
246 if (drv->func_drv_ext.disconnect_ext)
247 {
248 if (drv->func_drv_ext.disconnect_ext(dev) == 0)
249 {
250 //remove each other
251 dev->func_drv = NULL;
252 _usb_virt_bus_remov_dev_from_drv(dev, drv);
253 }
254 else
255 {
256 __err("PANIC : [VIRR BUS] : disconnect fail");
257 }
258 }
259 }
260
261 //UsbUnLock(dev->usb_virt_sub_dev_semi);
262 hal_sem_post(dev->usb_virt_sub_dev_semi);
263 }
264 }
265
266 /* disconnect失败的device, 应该强行删除 */
267 if (! usb_list_empty(&(drv->virt_dev_list)))
268 {
269 __err("PANIC : [VIRR BUS] : usb_virt_bus_drv_unreg() drv->dev_list not empty");
270 }
271
272 /* 从supper bus中删除本drv */
273 list_head_ext_remov_node_from_list(drv, &(my_usb_virt_bus.drv_list));
274 //UsbUnLock(my_usb_virt_bus.BusLock);
275 hal_sem_post(my_usb_virt_bus.BusLock);
276 return 0;
277 }
278
279
280 /*
281 ********************************************************************************
282 * usb_virt_bus_dev_add
283 * Description:
284 * 将device添加到总线上
285 * Arguments:
286 * dev : input.
287 * Return value:
288 *
289 * note:
290 * 一个设备不能注册两次?????????
291 *********************************************************************************
292 */
usb_virt_bus_dev_add(struct usb_host_virt_sub_dev * dev)293 int32_t usb_virt_bus_dev_add(struct usb_host_virt_sub_dev *dev)
294 {
295 uint32_t sr = 0;
296
297 if (!dev || !(dev->father_dev))
298 {
299 __err("PANIC : [VIRR BUS] : usb_virt_bus_dev_add() : dev == NULL");
300 return -1;
301 }
302
303 /* bus上已经存在deivce, 就不要再添加了 */
304 if (list_node_exist((void *)dev, &(my_usb_virt_bus.dev_list)) == 0)
305 {
306 __err("ERR: device is already in the list");
307 return 0;
308 }
309
310 //UsbLock(my_usb_virt_bus.BusLock);
311 hal_sem_wait(my_usb_virt_bus.BusLock);
312
313 if (dev->func_drv != NULL)
314 {
315 __err("PANIC : [VIRR BUS] : dev->drv != NULL,new dev has been bind to drv");
316 //UsbUnLock(my_usb_virt_bus.BusLock);
317 hal_sem_post(my_usb_virt_bus.BusLock);
318 return -1;
319 }
320 else
321 {
322 /* 先前可能已经有driver连上bus了, 这里就去寻找bus上与之匹配的driver */
323 struct usb_list_head *list_start = NULL;
324 struct usb_list_head *list_now = NULL;
325 //UsbLock(dev->usb_virt_sub_dev_semi);
326 hal_sem_wait(dev->usb_virt_sub_dev_semi);
327 list_start = &(my_usb_virt_bus.drv_list);
328 list_now = list_start->next;
329 hal_log_info("--usb_virt_bus_dev_add---0\n");
330
331 while (list_start != list_now)
332 {
333 struct usb_host_func_drv *func_drv = (struct usb_host_func_drv *)list_now->data;
334 list_now = list_now->next;
335 /* 事先尝试bind到一起 */
336 dev->func_drv = func_drv;
337 hal_log_info("--usb_virt_bus_dev_add---1\n");
338 if (func_drv->func_drv_ext.probe_ext(dev) == 0)
339 {
340 /* add 到drv的 dev list中 */
341 //USB_OS_ENTER_CRITICAL(sr);
342 sr = hal_spin_lock_irqsave(&device_lock);
343 list_head_malloc_and_add(dev, &(func_drv->virt_dev_list));
344 hal_spin_unlock_irqrestore(&device_lock, sr);
345 //USB_OS_EXIT_CRITICAL(sr);
346 break; /* 遇到第一个macth的drv就结束。 */
347 }
348 else
349 {
350 dev->func_drv = NULL; /* 失败则清空 */
351 }
352 hal_log_info("--usb_virt_bus_dev_add---2\n");
353 }
354
355 //UsbUnLock(dev->usb_virt_sub_dev_semi);
356 hal_sem_post(dev->usb_virt_sub_dev_semi);
357
358 }
359
360 /* 将dev添加到supper bus */
361 //USB_OS_ENTER_CRITICAL(sr);
362 sr = hal_spin_lock_irqsave(&device_lock);
363 list_head_malloc_and_add(dev, &(my_usb_virt_bus.dev_list));
364 hal_spin_unlock_irqrestore(&device_lock, sr);
365 hal_sem_post(my_usb_virt_bus.BusLock);
366 //USB_OS_EXIT_CRITICAL(sr);
367 //UsbUnLock(my_usb_virt_bus.BusLock);
368 return 0;
369 }
370
371 /*
372 ********************************************************************************
373 * usb_virt_bus_dev_add
374 * Description:
375 * 将device添加到总线上
376 * Arguments:
377 * dev : input.
378 * Return value:
379 *
380 * note:
381 * 注意该期间,dev不能使用,得用dev->scsi_dev_semi来保护
382 * 因为这个期间dev属于临界区域,处于混沌状态用dev->usb_virt_dev_semi保护
383 * 从dev->drv中删除本dev
384 * 调用drv->scsi_remove
385 * dev->drv == NULL
386 *********************************************************************************
387 */
usb_virt_bus_dev_del(struct usb_host_virt_sub_dev * dev)388 int32_t usb_virt_bus_dev_del(struct usb_host_virt_sub_dev *dev)
389 {
390 struct usb_host_func_drv *func_driver = NULL;
391
392 if (dev == NULL)
393 {
394 __err("PANIC : [VIRR BUS] : usb_virt_bus_dev_del() input == NULL");
395 return -1;
396 }
397
398 //UsbLock(my_usb_virt_bus.BusLock);
399 hal_sem_wait(my_usb_virt_bus.BusLock);
400 func_driver = dev->func_drv;
401 //UsbLock(dev->usb_virt_sub_dev_semi);
402 hal_sem_wait(dev->usb_virt_sub_dev_semi);
403
404 if (func_driver)
405 {
406 if (func_driver->func_drv_ext.disconnect_ext)
407 {
408 if (func_driver->func_drv_ext.disconnect_ext(dev) == 0)
409 {
410 //remove each other
411 dev->func_drv = NULL;
412 _usb_virt_bus_remov_dev_from_drv(dev, func_driver);
413 }
414 else
415 {
416 __err("PANIC : [VIRR BUS] : disconnect fail");
417 }
418 }
419 }
420 else
421 {
422 __err("PANIC : [VIRR BUS] : usb_virt_bus_dev_del() dev->drv == NULL");
423 }
424
425 //UsbUnLock(dev->usb_virt_sub_dev_semi);
426 hal_sem_post(dev->usb_virt_sub_dev_semi);
427 //del from supper bus
428 list_head_ext_remov_node_from_list(dev, &(my_usb_virt_bus.dev_list));
429 //UsbUnLock(my_usb_virt_bus.BusLock);
430 hal_sem_post(my_usb_virt_bus.BusLock);
431
432 return 0;
433 }
434
435 /*
436 ********************************************************************************
437 * usb_virt_bus_init
438 * Description:
439 * bus的初始化只是初始化device list和driver list
440 * Arguments:
441 * void
442 * Return value:
443 * void
444 * note:
445 * void
446 *********************************************************************************
447 */
usb_virt_bus_init(void)448 int32_t usb_virt_bus_init(void)
449 {
450 memset(&my_usb_virt_bus, 0, sizeof(struct usb_virt_bus));
451 USB_INIT_LIST_HEAD(& (my_usb_virt_bus.dev_list));
452 USB_INIT_LIST_HEAD(&(my_usb_virt_bus.drv_list));
453
454 // my_usb_virt_bus.BusLock = USB_OS_SemCreate(1);
455 my_usb_virt_bus.BusLock = hal_sem_create(1);
456
457 if (my_usb_virt_bus.BusLock == NULL)
458 {
459 __err("ERR: usb_virt_bus_init: my_usb_virt_bus.BusLock == NULL");
460 return -1;
461 }
462
463 return 0;
464 }
465
466 /*
467 ********************************************************************************
468 * usb_virt_bus_exit
469 * Description:
470 *
471 * Arguments:
472 * void
473 * Return value:
474 * void
475 * note:
476 * void
477 *********************************************************************************
478 */
usb_virt_bus_exit(void)479 int32_t usb_virt_bus_exit(void)
480 {
481 // uint8_t err = 0;
482
483 if (my_usb_virt_bus.BusLock)
484 {
485 // USB_OS_SemDel(my_usb_virt_bus.BusLock, &err);
486 hal_sem_delete(my_usb_virt_bus.BusLock);
487 my_usb_virt_bus.BusLock = NULL;
488 }
489 else
490 {
491 __err("ERR: usb_virt_bus_exit: my_usb_virt_bus.BusLock == NULL");
492 return -1;
493 }
494
495 memset(&my_usb_virt_bus, 0, sizeof(struct usb_virt_bus));
496 USB_INIT_LIST_HEAD(&(my_usb_virt_bus.dev_list));
497 USB_INIT_LIST_HEAD(&(my_usb_virt_bus.drv_list));
498 return 0;
499 }
500