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.07.xx
15 *
16 * Description :
17 *
18 * History :
19 *******************************************************************************
20 */
21 //#include "usb_host_config.h"
22 #include <string.h>
23
24 //#include <usb/usb_os_platform.h>
25 #include <usb_host_common.h>
26
27 //#include "usb_host_base_types.h"
28 #include <usb_list.h>
29 #include <usb_gen_dev_mod.h>
30 #include <usb_drv_dev_macth.h>
31 #include "usb_virt_bus.h"
32 #include "usb_gen_hcd.h"
33 #include "usb_msg.h"
34 //#include <atomic.h>
35 #include <hal_osal.h>
36
37
38 static u32 usb_host_is_run_flag = 0;
39
40 #define USB_HOST_IS_RUN_TRUE 0x11
41 #define USB_HOST_IS_RUN_FAIL 0x22
42
43
44 //usb host开启
usb_host_enable(void)45 void usb_host_enable(void)
46 {
47 usb_host_is_run_flag = USB_HOST_IS_RUN_TRUE;
48 }
49 //usb host 禁止
usb_host_disable(void)50 void usb_host_disable(void)
51 {
52 usb_host_is_run_flag = USB_HOST_IS_RUN_FAIL;
53 }
54 //查询usb host是否开启
55 //return : 非0为enable
usb_host_is_enabled(void)56 u8 usb_host_is_enabled(void)
57 {
58 return (usb_host_is_run_flag == USB_HOST_IS_RUN_TRUE) ? 0x01 : 0x00;
59 }
60
61 /**
62 * usb_host_alloc_virt_dev - usb device constructor (usbcore-internal)
63 * @parent: hub to which device is connected; null to allocate a root hub
64 * @bus: bus used to access the device
65 * @port1: one-based index of port; ignored for root hubs
66 * Context: !in_interrupt ()
67 *
68 * Only hub drivers (including virtual root hub drivers for host
69 * controllers) should ever call this.
70 *
71 * This call may not be used in a non-sleeping context.
72 */
73 /* 创建usb_host_virt_dev结构 */
usb_host_alloc_virt_dev(struct usb_host_virt_dev * parent,struct usb_virt_bus * bus,u32 port1)74 struct usb_host_virt_dev *usb_host_alloc_virt_dev(struct usb_host_virt_dev *parent, struct usb_virt_bus *bus, u32 port1)
75 {
76 struct usb_host_virt_dev *dev = NULL;
77 s32 i = 0;
78
79 if (bus == NULL)
80 {
81 __err("PANIC : usb_host_alloc_virt_dev() : input == NULL");
82 return NULL;
83 }
84
85 // dev = hal_malloc(sizeof(*dev));
86 dev = hal_malloc(sizeof(struct usb_host_virt_dev));
87
88 if (!dev)
89 {
90 __err("PANIC : usb_host_alloc_virt_dev() : dev == NULL");
91 return NULL;
92 }
93
94 memset(dev, 0, sizeof(struct usb_host_virt_dev));
95 dev->state = USB_STATE_ATTACHED;
96
97 INIT_LIST_HEAD(&dev->ep0.urb_list);
98 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
99 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
100 /* ep0 maxpacket comes later, from device descriptor */
101 dev->ep_in[0] = dev->ep_out[0] = &dev->ep0;
102 /* Save readable and stable topology id, distinguishing devices
103 * by location for diagnostics, tools, driver model, etc. The
104 * string is a path along hub ports, from the root. Each device's
105 * dev->devpath will be stable until USB is re-cabled, and hubs
106 * are often labeled with these port numbers. The bus_id isn't
107 * as stable: bus->busnum changes easily from modprobe order,
108 * cardbus or pci hotplugging, and so on.
109 */
110 /*
111 if ((!parent)) {
112 dev->devpath [0] = '0';
113
114 dev->dev.parent = bus->controller;
115 sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
116 } else {
117 //match any labeling on the hubs; it's one-based
118 if (parent->devpath [0] == '0')
119 snprintf (dev->devpath, sizeof dev->devpath,
120 "%d", port1);
121 else
122 snprintf (dev->devpath, sizeof dev->devpath,
123 "%s.%d", parent->devpath, port1);
124
125 dev->dev.parent = &parent->dev;
126 sprintf (&dev->dev.bus_id[0], "%d-%s",
127 bus->busnum, dev->devpath);
128
129 // hub driver sets up TT records
130 }
131 */
132 dev->bus = bus;
133 dev->parent = parent;
134 //?INIT_LIST_HEAD(&dev->filelist);
135
136 dev->usb_virt_dev_semi = hal_sem_create(1);
137
138 //=================================
139 //初始化virt_sub_dev部分
140 for (i = 0; i < USB_MAX_VIRT_SUB_DEV_NR; i ++)
141 {
142 dev->virt_sub_dev_array[i].usb_virt_sub_dev_semi = hal_sem_create(1);
143 }
144
145 return dev;
146 }
147
148
149
usb_host_free_virt_dev(struct usb_host_virt_dev * virt_dev)150 void usb_host_free_virt_dev(struct usb_host_virt_dev *virt_dev)
151 {
152 s32 i = 0;
153 u8 err = 0;
154
155 if (virt_dev == NULL)
156 {
157 __err("PANIC : usb_host_free_virt_dev() : input == NULL");
158 return ;
159 }
160
161 // --<1>--释放semi
162 // 释放virt_sub_dev部分
163 for (i = 0; i < USB_MAX_VIRT_SUB_DEV_NR; i ++)
164 {
165 if (virt_dev->virt_sub_dev_array[i].usb_virt_sub_dev_semi)
166 {
167 hal_sem_delete(virt_dev->virt_sub_dev_array[i].usb_virt_sub_dev_semi);
168 }
169 else
170 {
171 __err("PANIC : usb_host_free_virt_dev() : virt_sub_dev`s semi == NULL");
172 return;
173 }
174 }
175
176 if (virt_dev->usb_virt_dev_semi)
177 {
178 hal_sem_delete(virt_dev->usb_virt_dev_semi);
179 }
180 else
181 {
182 __err("PANIC : usb_host_free_virt_dev() : virt_dev`s semi == NULL");
183 return;
184 }
185
186 //--<2>--释放string
187 if (virt_dev->manufacturer)
188 {
189 free(virt_dev->manufacturer);
190 virt_dev->manufacturer = NULL;
191 }
192 else
193 {
194 __err("ERR: parameter is NULL, can't free");
195 }
196
197 if (virt_dev->product)
198 {
199 free(virt_dev->product);
200 virt_dev->product = NULL;
201 }
202 else
203 {
204 __err("ERR: parameter is NULL, can't free");
205 }
206
207 if (virt_dev->serial)
208 {
209 free(virt_dev->serial);
210 virt_dev->serial = NULL;
211 }
212 else
213 {
214 __err("ERR: parameter is NULL, can't free");
215 }
216
217 //--<3>--释放,所有相关描述符
218 if (virt_dev->config)
219 {
220 int j = 0;
221 int k = 0;
222 int l = 0;
223 //int m = 0;
224
225 for (j = 0; j < virt_dev->descriptor.bNumConfigurations ; j++)
226 {
227 for (k = 0; k < virt_dev->config[j].desc.bNumInterfaces ; k++)
228 {
229 for (l = 0; l < virt_dev->config[j].intf_cache[k]->num_altsetting; l++)
230 {
231 //for(m = 0; m < virt_dev->config[j].intf_cache[k].altsetting_array[l].desc.bNumEndpoints; m++){
232 if (virt_dev->config[j].intf_cache[k]->altsetting_array[l].endpoint)
233 {
234 free(virt_dev->config[j].intf_cache[k]->altsetting_array[l].endpoint);
235 virt_dev->config[j].intf_cache[k]->altsetting_array[l].endpoint = NULL;
236 }
237 else
238 {
239 __err("ERR: parameter is NULL, can't free");
240 }
241
242 //}
243 }
244
245 if (virt_dev->config[j].intf_cache[k]->altsetting_array)
246 {
247 free(virt_dev->config[j].intf_cache[k]->altsetting_array);
248 virt_dev->config[j].intf_cache[k]->altsetting_array = NULL;
249 }
250 else
251 {
252 __err("ERR: parameter is NULL, can't free");
253 }
254
255 if (virt_dev->config[j].intf_cache[k])
256 {
257 free(virt_dev->config[j].intf_cache[k]);
258 virt_dev->config[j].intf_cache[k] = NULL;
259 }
260 else
261 {
262 __err("ERR: parameter is NULL, can't free");
263 }
264
265 /*--在usb_disable_device()中已经删除了
266 DMSG_INFO("new_interfaces[n]--free-- = %x",virt_dev->config[j].interfac[k]);
267 if(virt_dev->config[j].interfac[k]){
268 free(virt_dev->config[j].interfac[k]);
269 virt_dev->config[j].interfac[k] = NULL;
270 }
271 */
272 }
273
274 //free
275 //free(virt_dev->config->intf_cache);
276 //virt_dev->config->intf_cache = NULL;
277 }
278
279 //free
280 if (virt_dev->config)
281 {
282 free(virt_dev->config);
283 virt_dev->config = NULL;
284 }
285 else
286 {
287 __err("ERR: parameter is NULL, can't free");
288 }
289 }
290
291 for (i = 0; i < virt_dev->descriptor.bNumConfigurations; i++)
292 {
293 if (virt_dev->rawdescriptors[i])
294 {
295 free(virt_dev->rawdescriptors[i]);
296 virt_dev->rawdescriptors[i] = NULL;
297 }
298 else
299 {
300 __err("ERR: parameter is NULL, can't free");
301 }
302 }
303
304 if (virt_dev->rawdescriptors)
305 {
306 free(virt_dev->rawdescriptors);
307 virt_dev->rawdescriptors = NULL;
308 }
309 else
310 {
311 __err("ERR: parameter is NULL, can't free");
312 }
313
314 //--<4>--释放自己
315 free(virt_dev);
316 // virt_dev = NULL;
317 return;
318 }
319
320 //return : 0 //成功
321 // : 其余 //失败
_usb_core_func_drv_probe_interface(struct usb_host_virt_sub_dev * sub_dev)322 int32_t _usb_core_func_drv_probe_interface(struct usb_host_virt_sub_dev *sub_dev)
323 {
324 struct usb_interface *intf = NULL;
325 struct usb_host_func_drv *func_drv = NULL;
326 const struct usb_drv_dev_match_table *id = NULL;
327 s32 error = -ENODEV;
328 struct usb_host_virt_dev *virt_dev = NULL;
329
330 if (!sub_dev || !(sub_dev->sub_dev_interface))
331 {
332 __err("PANIC : _usb_core_func_drv_probe_interface() : input fail,sub_dev = 0x%x ,");
333 return -ENODEV;
334 }
335
336 intf = usb_mod_to_usb_interface(sub_dev);
337 func_drv = sub_dev->func_drv;
338 hal_log_info("----func_drv->func_drv_name = %s\n", func_drv->func_drv_name);
339
340 if (!func_drv->probe)
341 {
342 __err("PANIC : _usb_core_func_drv_probe_interface() : func_drv->probe == NULL");
343 return error;
344 }
345
346 /* FIXME we'd much prefer to just resume it ... */
347 virt_dev = usb_mod_interface_to_usbdev(intf);
348
349 if (virt_dev == NULL)
350 {
351 __err("PANIC : virt_dev == NULL");
352 return error;
353 }
354
355 if (virt_dev->state == USB_STATE_SUSPENDED)
356 {
357 __err("PANIC : virt_dev->state == USB_STATE_SUSPENDED");
358 return -EHOSTUNREACH;
359 }
360 hal_log_info("---usb_core_func_drv_probe----1---\n");
361 id = usb_match_id(intf, func_drv->match_table);
362
363 if (id)
364 {
365 intf->condition = USB_INTERFACE_BINDING;
366 hal_log_info("---usb_core_func_drv_probe----2---\n");
367 error = func_drv->probe(intf, id);
368 hal_log_info("---usb_core_func_drv_probe----3---\n");
369 intf->condition = error ? USB_INTERFACE_UNBOUND : USB_INTERFACE_BOUND;
370 return error;
371 }
372
373 return -ENODEV; //失败
374 }
375
_usb_core_func_drv_disconncet_interface(struct usb_host_virt_sub_dev * dev)376 int32_t _usb_core_func_drv_disconncet_interface(struct usb_host_virt_sub_dev *dev)
377 {
378 struct usb_interface *intf = NULL;
379 struct usb_host_func_drv *driver = NULL;
380
381 if (dev == NULL)
382 {
383 __err("ERR; invalid argment");
384 return -1;
385 }
386
387 intf = usb_mod_to_usb_interface(dev);
388
389 if (intf == NULL)
390 {
391 __err("ERR; invalid argment");
392 return -1;
393 }
394
395 driver = usb_mod_interface_to_func_drv(intf);
396
397 if (driver == NULL)
398 {
399 __err("ERR; invalid argment");
400 return -1;
401 }
402
403 intf->condition = USB_INTERFACE_UNBINDING;
404 /* release all urbs for this interface */
405 usb_disable_interface(usb_mod_interface_to_usbdev(intf), intf);
406
407 /* 调用driver的disconnect,如msc_disconnect() */
408 if (driver && driver->disconnect)
409 {
410 driver->disconnect(intf);
411 }
412
413 /* reset other interface state */
414 usb_set_interface(usb_mod_interface_to_usbdev(intf),
415 intf->altsetting[0].desc.bInterfaceNumber,
416 0);
417 usb_mod_usb_set_intf_priv_data(intf, NULL);
418 intf->condition = USB_INTERFACE_UNBOUND;
419 return 0;
420 }
421
422
423 /**
424 * usb_altnum_to_altsetting - get the altsetting structure with a given
425 * alternate setting number.
426 * @intf: the interface containing the altsetting in question
427 * @altnum: the desired alternate setting number
428 *
429 * This searches the altsetting array of the specified interface for
430 * an entry with the correct bAlternateSetting value and returns a pointer
431 * to that entry, or null.
432 *
433 * Note that altsettings need not be stored sequentially by number, so
434 * it would be incorrect to assume that the first altsetting entry in
435 * the array corresponds to altsetting zero. This routine helps device
436 * drivers avoid such mistakes.
437 *
438 * Don't call this function unless you are bound to the intf interface
439 * or you have locked the device!
440 */
usb_altnum_to_altsetting(struct usb_interface * intf,s32 altnum)441 struct usb_host_virt_interface *usb_altnum_to_altsetting(struct usb_interface *intf, s32 altnum)
442 {
443 int i;
444
445 for (i = 0; i < intf->num_altsetting; i++)
446 {
447 if (intf->altsetting[i].desc.bAlternateSetting == altnum)
448 {
449 return &intf->altsetting[i];
450 }
451 }
452
453 return NULL;
454 }
455
456 /**
457 * usb_lock_device - acquire the lock for a usb device structure
458 * @udev: device that's being locked
459 *
460 * Use this routine when you don't hold any other device locks;
461 * to acquire nested inner locks call down(&udev->serialize) directly.
462 * This is necessary for proper interaction with usb_lock_all_devices().
463 */
usb_lock_device(struct usb_host_virt_dev * virt_dev)464 void usb_lock_device(struct usb_host_virt_dev *virt_dev)
465 {
466 u8 err = 0;
467 //down_read(&usb_all_devices_rwsem);
468 //USB_OS_SEMI_PEND(virt_dev->usb_virt_dev_semi, 0, &err);
469 hal_sem_wait(virt_dev->usb_virt_dev_semi);
470 }
471
472 /**
473 * usb_unlock_device - release the lock for a usb device structure
474 * @udev: device that's being unlocked
475 *
476 * Use this routine when releasing the only device lock you hold;
477 * to release inner nested locks call up(&udev->serialize) directly.
478 * This is necessary for proper interaction with usb_lock_all_devices().
479 */
usb_unlock_device(struct usb_host_virt_dev * virt_dev)480 void usb_unlock_device(struct usb_host_virt_dev *virt_dev)
481 {
482 //USB_OS_SEMI_POST(virt_dev->usb_virt_dev_semi);
483 hal_sem_post(virt_dev->usb_virt_dev_semi);
484 //up_read(&usb_all_devices_rwsem);
485 }
486
487
488
489
usb_buffer_alloc(struct usb_host_virt_dev * dev,u32 size,u32 mem_flags,void * dma)490 void *usb_buffer_alloc(struct usb_host_virt_dev *dev, u32 size, u32 mem_flags, void *dma)
491 {
492 return hcd_ops_buffer_alloc(dev->bus, size, mem_flags, dma);
493 }
494
495 /**
496 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
497 * @dev: device the buffer was used with
498 * @size: requested buffer size
499 * @addr: CPU address of buffer
500 * @dma: DMA address of buffer
501 *
502 * This reclaims an I/O buffer, letting it be reused. The memory must have
503 * been allocated using usb_buffer_alloc(), and the parameters must match
504 * those provided in that allocation request.
505 */
usb_buffer_free(struct usb_host_virt_dev * dev,u32 size,void * addr,void * dma)506 void usb_buffer_free(struct usb_host_virt_dev *dev, u32 size, void *addr, void *dma)
507 {
508 hcd_ops_buffer_free(dev->bus, size, addr, dma);
509 }
510
511 /**
512 * usb_ifnum_to_if - get the interface object with a given interface number
513 * @dev: the device whose current configuration is considered
514 * @ifnum: the desired interface
515 *
516 * This walks the device descriptor for the currently active configuration
517 * and returns a pointer to the interface with that particular interface
518 * number, or null.
519 *
520 * Note that configuration descriptors are not required to assign interface
521 * numbers sequentially, so that it would be incorrect to assume that
522 * the first interface in that descriptor corresponds to interface zero.
523 * This routine helps device drivers avoid such mistakes.
524 * However, you should make sure that you do the right thing with any
525 * alternate settings available for this interfaces.
526 *
527 * Don't call this function unless you are bound to one of the interfaces
528 * on this device or you have locked the device!
529 */
usb_ifnum_to_if(struct usb_host_virt_dev * dev,u32 ifnum)530 struct usb_interface *usb_ifnum_to_if(struct usb_host_virt_dev *dev, u32 ifnum)
531 {
532 struct usb_host_virt_config *config = dev->actconfig;
533 int i;
534
535 if (!config)
536 {
537 __err("ERR: input = NULL");
538 return NULL;
539 }
540
541 for (i = 0; i < config->desc.bNumInterfaces; i++)
542 {
543 if (config->interfac[i]->altsetting[0].desc.bInterfaceNumber == ifnum)
544 {
545 return config->interfac[i];
546 }
547 }
548
549 return NULL;
550 }
551
552
553