1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78
79 //#include <MsCommon.h> // NUSED
80
81 //#include "include/drvPorts.h" // NUSED
82 #include "include/drvConfig.h"
83 //#include "include/drvCompiler.h" // NUSED
84 //#include "include/drvErrno.h" // NUSED
85 //#include "include/drvTimer.h" // NUSED
86 //#include "include/drvList.h" // NUSED
87 #include "include/drvKernel.h"
88 #include "include/drvBitops.h"
89 //#include "include/drvCPE_AMBA.h" // NUSED
90
91 // USB related header files
92 //#include "include/drvUSBHost.h" // NUSED
93 #include "drvUSBCore.h"
94 #include "drvUsbd.h"
95 #include "drvUSBHwCtl.h"
96 #include "drvEHCI.h"
97 //#include "drvUSB.h" // NUSED
98 /* applying drvUsbHostConfig.h (inside drvUsbd.h <- drvHub.h) */
99
100 extern int ms_usb_hub_init(void);
101 extern int ms_usb_hub_uninit(void);
102 //extern void ms_qh_ehci_reinit(struct usb_hcd *pHcd, int fix);
103
104 //int USB_core_lock=0; // NUSED
105 //int Timer_task_lock=0; // NUSED
106
107 char usb_product_string_buf[64];
108 char usb_manufacturer_string_buf[64];
109 char usb_serialnumber_string_buf[64];
110
ms_generic_probe(struct device_s * pDev)111 static int ms_generic_probe (struct device_s *pDev)
112 {
113 return 0;
114 }
ms_generic_remove(struct device_s * pDev)115 static int ms_generic_remove (struct device_s *pDev)
116 {
117 return 0;
118 }
119
120 static struct device_driver ms_usb_generic_driver = {
121 "usb_gdrv",
122 &usb_bus_type,
123 {NULL,NULL},
124 {&ms_usb_generic_driver.devices,&ms_usb_generic_driver.devices},
125 ms_generic_probe,
126 ms_generic_remove,
127 };
128
129 static int usb_generic_driver_data;
130 #if 0 // NUSED
131 void ms_usb_show_string(struct usb_device *pDev, char *pStringId, int iIdx)
132 {
133 char *pCbuf;
134
135 if (!iIdx)
136 return;
137
138 if (!(pCbuf = (char*) kmalloc(256, GFP_KERNEL)))
139 return;
140
141 if (ms_usb_string(pDev, iIdx, pCbuf, 256) > 0)
142 {
143 diag_printf("<%s> %s: %s\n",__FUNCTION__, pStringId, pCbuf);
144 }
145 kfree(pCbuf);
146 }
147 #endif
148 const struct usb_device_id *
ms_usb_match_id(struct usb_interface * pUintf,const struct usb_device_id * pDevId)149 ms_usb_match_id(struct usb_interface *pUintf, const struct usb_device_id *pDevId)
150 {
151 struct usb_host_interface *ms_intf;
152 struct usb_device *pUdev;
153
154 if (pDevId == NULL)
155 return NULL;
156
157 ms_intf = &pUintf->altsetting [pUintf->act_altsetting];
158 pUdev = interface_to_usbdev(pUintf);
159
160 for (; pDevId->idVendor || pDevId->bDeviceClass || pDevId->bInterfaceClass ||
161 pDevId->match_id_info; pDevId++)
162 {
163 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_VENDOR) &&
164 pDevId->idVendor != pUdev->descriptor.idVendor)
165 continue;
166
167 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_PRODUCT) &&
168 pDevId->idProduct != pUdev->descriptor.idProduct)
169 continue;
170
171 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_DEV_LO) &&
172 (pDevId->bcdDevice_lo >= pUdev->descriptor.bcdDevice))
173 continue;
174
175 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_DEV_HI) &&
176 (pDevId->bcdDevice_hi <= pUdev->descriptor.bcdDevice))
177 continue;
178
179 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_DEV_CLASS) &&
180 (pDevId->bDeviceClass != pUdev->descriptor.bDeviceClass))
181 continue;
182
183 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_DEV_SUBCLASS) &&
184 (pDevId->bDeviceSubClass!= pUdev->descriptor.bDeviceSubClass))
185 continue;
186
187 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_DEV_PROTOCOL) &&
188 (pDevId->bDeviceProtocol != pUdev->descriptor.bDeviceProtocol))
189 continue;
190
191 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_INT_CLASS) &&
192 (pDevId->bInterfaceClass != ms_intf->desc.bInterfaceClass))
193 continue;
194
195 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_INT_SUBCLASS) &&
196 (pDevId->bInterfaceSubClass != ms_intf->desc.bInterfaceSubClass))
197 continue;
198
199 if ((pDevId->match_id_flags & USBDEV_MATCH_ID_INT_PROTOCOL) &&
200 (pDevId->bInterfaceProtocol != ms_intf->desc.bInterfaceProtocol))
201 continue;
202
203 return pDevId;
204 }
205
206 return NULL;
207 }
208
ms_usb_probe_interface(struct device_s * pDev)209 int ms_usb_probe_interface(struct device_s *pDev)
210 {
211 struct usb_interface * pUintf = get_usb_interface(pDev);
212 struct usb_driver * pUdriver = get_usb_driver(pDev->driver);
213 const struct usb_device_id *pUdev_id;
214 int s32Error = -ENODEV;
215
216 if (!pUdriver->probe_func)
217 return s32Error;
218
219 pUdev_id = ms_usb_match_id (pUintf, pUdriver->match_id_table);
220 if (pUdev_id)
221 {
222 ms_usbhost_debug ("<%s> - got id\n", __FUNCTION__);
223 s32Error = pUdriver->probe_func (pUintf, pUdev_id);
224 }
225 if (!s32Error)
226 pUintf->driver = pUdriver;
227
228 return s32Error;
229 }
230
ms_usb_driver_release_interface(struct usb_driver * pUdriver,struct usb_interface * iface)231 void ms_usb_driver_release_interface(struct usb_driver *pUdriver, struct usb_interface *iface)
232 {
233 // this should never happen, don't release something that's not ours
234 if (iface->driver && iface->driver != pUdriver)
235 return;
236
237 #if 0 // ???
238 iface->driver = NULL;
239 ms_usb_set_interface(interface_to_usbdev(iface),
240 iface->altsetting[0].desc.bInterfaceNumber,
241 0);
242 #endif
243 ms_usb_set_intfdata(iface, NULL);
244 iface->driver = NULL;
245 }
246
ms_usb_unbind_interface(struct device_s * pDev)247 int ms_usb_unbind_interface(struct device_s *pDev)
248 {
249 struct usb_interface *pUintf = get_usb_interface(pDev);
250 struct usb_driver *pUdriver = get_usb_driver(pDev->driver);
251
252 ms_usb_disable_interface(interface_to_usbdev(pUintf), pUintf);
253
254 if (pUintf->driver && pUintf->driver->discon_func)
255 pUintf->driver->discon_func(pUintf);
256
257 ms_usb_driver_release_interface(pUdriver, pUintf);
258
259 return 0;
260 }
261
ms_usb_register(struct usb_driver * n_driver)262 int ms_usb_register(struct usb_driver *n_driver)
263 {
264 int s32Retval = 0;
265
266 memcpy(n_driver->driver.name, n_driver->name, sizeof(n_driver->name));
267 n_driver->driver.bus = &usb_bus_type;
268 n_driver->driver.probe = ms_usb_probe_interface;
269 n_driver->driver.remove = ms_usb_unbind_interface;
270
271 s32Retval = ms_driver_register(&n_driver->driver);
272
273 if (!s32Retval)
274 {
275 ms_usbhost_debug("regtered new driver %s", n_driver->name);
276 }
277 else
278 {
279 ms_usbhost_err("problem %d when registering driver %s",
280 s32Retval, n_driver->name);
281 }
282
283 return s32Retval;
284 }
285
ms_usb_deregister(struct usb_driver * pUdriver)286 void ms_usb_deregister(struct usb_driver *pUdriver)
287 {
288 ms_usbhost_msg("deregistering driver %s\n", pUdriver->name);
289 ms_driver_unregister (&pUdriver->driver);
290 }
291
ms_usb_ifnum_to_if(struct usb_device * pUdev,int ifnum)292 struct usb_interface *ms_usb_ifnum_to_if(struct usb_device *pUdev, int ifnum)
293 {
294 int i;
295
296 for (i = 0; i < pUdev->actconfig->desc.bNumInterfaces; i++)
297 if (pUdev->actconfig->interface[i]->altsetting[0]
298 .desc.bInterfaceNumber == ifnum)
299 return pUdev->actconfig->interface[i];
300
301 return NULL;
302 }
303 #if 0 // NUSED
304 BOOL ms_usb_find_dev_for_driver(struct device_driver *pDriver, struct usb_device **pudev, struct usb_hcd *hcd)
305 {
306 struct list_head *entry;
307 struct device_s *pDev;
308
309 list_for_loop(entry, &pDriver->devices)
310 {
311 const struct list_head *_mptr = entry;
312 pDev = (struct device_s *) ( (char *)_mptr - (char *)offsetof(struct device_s, driver_list) );
313
314 *pudev = (struct usb_device *) ((char *)pDev - (char *)offsetof( struct usb_device, dev));
315 if ((*pudev)->parent == NULL) // skip root hub, 2012.02.29
316 continue;
317
318 if ((*pudev)->bus->hcpriv == hcd)
319 return TRUE;
320 }
321 diag_printf(" \n\nusb_find_dev_for_driver> NOT FOUND!!!\n\n");
322 return FALSE;
323 }
324 #endif
325 //NSTL_060406 Sony MSAC-US5 MS Reader
ms_usb_get_connected_dev_state(int * pdevstate,unsigned char * pDevClass,struct usb_device * pusbdev,BOOL * pIntfDrvMatched)326 BOOL ms_usb_get_connected_dev_state(int *pdevstate,
327 unsigned char *pDevClass, struct usb_device *pusbdev, BOOL *pIntfDrvMatched)
328 {
329 BOOL status = FALSE;
330 struct usb_interface *pusbintf = NULL;
331 *pIntfDrvMatched = FALSE;
332
333 if (!pusbdev)
334 return status;
335
336 status = TRUE;
337 *pdevstate = pusbdev->eState;
338 if (pusbdev->eState < USB_STATE_ADDRESS)
339 return status;
340
341 if (pusbdev->descriptor.bDeviceClass)
342 {
343 *pDevClass = pusbdev->descriptor.bDeviceClass;
344 }
345 else if (pusbdev->actconfig && pusbdev->actconfig->desc.bNumInterfaces)
346 {
347 pusbintf = ms_usb_ifnum_to_if(pusbdev, 0);
348
349 if(pusbintf)
350 {
351 *pDevClass = pusbintf->altsetting->desc.bInterfaceClass;
352 *pIntfDrvMatched = (pusbintf->driver != NULL) ? TRUE : FALSE;
353 }
354 }
355
356 return status;
357 }
358
ms_usb_device_match(struct device_s * pDev,struct device_driver * pDrv)359 static int ms_usb_device_match (struct device_s *pDev, struct device_driver *pDrv)
360 {
361 struct usb_interface *pUintf;
362 struct usb_driver *usb_drv;
363 const struct usb_device_id *pUdev_id;
364
365 /* check for generic driver, which we don't match any device with */
366 if (pDrv == &ms_usb_generic_driver)
367 return 0;
368
369 pUintf = get_usb_interface(pDev);
370 usb_drv = get_usb_driver(pDrv);
371 pUdev_id = usb_drv->match_id_table;
372
373 pUdev_id = ms_usb_match_id (pUintf, usb_drv->match_id_table);
374 if (pUdev_id)
375 return 1; //found id
376
377 return 0; //id not found
378 }
379
ms_usb_release_dev(struct device_s * pDev)380 void ms_usb_release_dev(struct device_s *pDev)
381 {
382 struct usb_device *pUdev;
383 int retv;
384
385 pUdev = ms_to_usb_device(pDev);
386
387 //if (pUdev->bus && pUdev->bus->bus_ops && pUdev->bus->bus_ops->ms_deallo_hcd_dev)
388 // pUdev->bus->bus_ops->ms_deallo_hcd_dev(pUdev);
389 if ((retv = ms_hcd_free_dev(pUdev)) != ENOERR) {
390 ms_usbhost_err("free device error %d\n", retv);
391 }
392 ms_usb_destroy_config(pUdev);
393 kfree (pUdev);
394 }
395
ms_usb_alloc_dev(struct usb_device * parent,struct usb_bus * bus)396 struct usb_device *ms_usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus)
397 {
398 struct usb_device *pUdev;
399 int retv;
400
401 pUdev = (struct usb_device*) kmalloc(sizeof(*pUdev), GFP_KERNEL);
402 if (!pUdev)
403 return NULL;
404
405 memset(pUdev, 0, sizeof(*pUdev));
406
407 ms_device_initialize(&pUdev->dev);
408 pUdev->dev.release_func = ms_usb_release_dev;
409 /* the only way to escape from USB_STATE_NOTATTACHED */
410 pUdev->eState = USB_STATE_ATTACHED;
411
412 if (!parent)
413 pUdev->devpath [0] = '0';
414 pUdev->bus = bus;
415 pUdev->parent = parent;
416
417 //if (pUdev->bus->bus_ops->ms_alloc_hcd_dev)
418 // pUdev->bus->bus_ops->ms_alloc_hcd_dev(pUdev);
419 if ((retv = ms_hcd_alloc_dev(pUdev)) != ENOERR) {
420 ms_usbhost_err("alloc device error %d\n", retv);
421 }
422
423 return pUdev;
424 }
425
ms_usb_get_dev(struct usb_device * pUdev)426 struct usb_device *ms_usb_get_dev (struct usb_device *pUdev)
427 {
428 struct device_s *pDev_t;
429
430 if (!pUdev)
431 return NULL;
432
433 pDev_t = ms_get_device(&pUdev->dev);
434 if (pDev_t)
435 return ms_to_usb_device(pDev_t);
436 else
437 return NULL;
438 }
439
ms_usb_put_dev(struct usb_device * pUdev)440 void ms_usb_put_dev(struct usb_device *pUdev)
441 {
442 if (pUdev)
443 {
444 if (osapi_atomic_dec_and_test(&((&pUdev->dev)->dev_refcnt)))
445 {
446 //release allocated buffer for saving device's configurations and device info
447 ms_device_unregister(&pUdev->dev);
448 }
449 }
450 }
451
ms_usb_disconnect(struct usb_device ** pdev)452 void ms_usb_disconnect(struct usb_device **pdev)
453 {
454 struct usb_device *pUdev = *pdev;
455 //struct usb_bus *pUbus; // NUSED
456 int i, devnum = 0;
457 struct usb_hcd *pHcd = (struct usb_hcd*) pUdev->bus->hcpriv;
458
459 if (!pUdev)
460 {
461 ms_usbhost_debug ("%s nodev\n", __FUNCTION__);
462 return;
463 }
464 //pUbus = pUdev->bus;
465 //if (!pUbus)
466 //{
467 // ms_usbhost_debug ("%s nobus\n", __FUNCTION__);
468 // return;
469 //}
470
471 //*pdev = NULL; // moved
472
473 //pUdev->eState = USB_STATE_NOTATTACHED;
474 ms_usb_set_device_state(pUdev, USB_STATE_NOTATTACHED); // patch from Linux 2.6.28
475
476 ms_usbhost_msg ("USB disconnect, address %d\n", pUdev->u32DevNum);
477 if (pUdev->parent)
478 devnum = pUdev->parent->u32DevNum;
479
480 /* If EHC halted while roothub dev disconnect, restart EHC by set
481 * its run bit. The periodic schedule urb reclaiming will reference
482 * the frame index, so we should not halt the EHC while disconnect.
483 */
484 if (devnum == 1)
485 {
486 /* Make sure the EHC is halted already */
487 ms_forceHcdRun(pHcd);
488 }
489
490 /* Free up all the children before we remove this device */
491 for (i = 0; i < USB_MAXCHILDREN; i++)
492 {
493 struct usb_device **child = pUdev->children + i;
494 if (*child)
495 ms_usb_disconnect(child);
496 }
497
498 /* the following code merged into ms_usb_disable_device()
499 * based on Linux USB driver
500 */
501 //ms_usbhost_debug("%s - unregistering interface %s\n", pUdev->dev.dev_name, pUdev->dev.bus_id);
502 //
503 //if (pUdev->actconfig)
504 //{
505 // for (i = 0; i < pUdev->actconfig->desc.bNumInterfaces; i++)
506 // {
507 // struct usb_interface *pUiface;
508 //
509 // /* remove this interface */
510 // pUiface = pUdev->actconfig->interface[i];
511 // ms_device_unregister(&pUiface->dev);
512 // }
513 //}
514
515 /* deallocate hcd/hardware state ... and nuke all pending urbs */
516 ms_usb_disable_device(pUdev, 0);
517 /* synchronize unlinks, may this works only in SMP case */
518 osapi_spin_lock_irq(&hcd_urb_unlink_lock);
519 osapi_spin_unlock_irq(&hcd_urb_unlink_lock);
520
521 /* patch code sequence by Linux */
522 ms_usbhost_debug("unregistering device\n");
523 ms_device_unregister(&pUdev->dev);
524
525 /* Free the device number and remove the /proc/bus/usb entry */
526 if (pUdev->u32DevNum > 0)
527 {
528 //ms_clear_bit(pUdev->u32DevNum, pUdev->bus->devmap.usb_devicemap, unsigned long);
529 ms_devmap_clear_bit(pUdev->u32DevNum, pUdev->bus->devmap.usb_devicemap);
530 pUdev->u32DevNum = -1;
531 }
532
533 osapi_spin_lock_irq(&device_state_lock);
534 *pdev = NULL;
535 osapi_spin_unlock_irq(&device_state_lock);
536
537 #ifdef ENABLE_ROOTHUB_DISCONN_REINIT // new patch to release qh and recover async qh
538 if (devnum == 1) // root hub is always assigned address 1
539 {
540 ms_roothub_disconn_reinit(pHcd);
541 }
542 #endif
543 }
544
ms_usb_choose_address(struct usb_device * pUdev)545 void ms_usb_choose_address(struct usb_device *pUdev)
546 {
547 int s32Devnum;
548 // TODO: FIXME needs locking for SMP!!
549
550 s32Devnum = find_next_zero_bit(pUdev->bus->devmap.usb_devicemap, 128, pUdev->bus->devnum_next);
551 if (s32Devnum >= 128)
552 s32Devnum = find_next_zero_bit(pUdev->bus->devmap.usb_devicemap, 128, 1);
553
554 pUdev->bus->devnum_next = ( s32Devnum >= 127 ? 1 : s32Devnum + 1);
555
556 if (s32Devnum < 128)
557 {
558 //ms_set_bit(s32Devnum, pUdev->bus->devmap.usb_devicemap, unsigned long);
559 ms_devmap_set_bit(s32Devnum, pUdev->bus->devmap.usb_devicemap);
560 pUdev->u32DevNum = s32Devnum;
561 }
562 }
563
ms_usb_set_address(struct usb_device * pUdev)564 int ms_usb_set_address(struct usb_device *pUdev)
565 {
566 int s32Retval;
567
568 if (pUdev->u32DevNum == 0)
569 return -EINVAL;
570 if ((pUdev->eState != USB_STATE_DEFAULT) && (pUdev->eState != USB_STATE_ADDRESS))
571 return -EINVAL;
572
573 s32Retval = ms_usb_control_cmd(pUdev, usb_snddefctrl(pUdev), USB_REQ_SET_ADDRESS,
574 0, pUdev->u32DevNum, 0, NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
575
576 if (s32Retval == 0)
577 //pUdev->eState = USB_STATE_ADDRESS;
578 ms_usb_set_device_state(pUdev, USB_STATE_ADDRESS);
579 return s32Retval;
580 }
581
582 #if USB_IF_EHSET_SUPPORT
583 extern int ms_usb_test_proc_vid(U16 vid, U16 pid);
584 extern void ms_usb_high_speed_test_proc(struct usb_device *udev, int flag);
585 #endif
586 #define NEW_DEVICE_RETRYS 2
587 #define SET_ADDRESS_RETRYS 2
588
ms_usb_new_device(struct usb_device * pUdev,struct device_s * parent)589 int ms_usb_new_device(struct usb_device *pUdev, struct device_s *parent)
590 {
591 static U8 u8MaxPacketSize = 8;
592 int s32Err = -EINVAL;
593 int i;
594 int j;
595
596 //diag_printf("usb_new_device\n");
597
598 ms_usb_generic_driver.bus = &usb_bus_type;
599 pUdev->dev.parent = parent;
600 pUdev->dev.driver = &ms_usb_generic_driver;
601 pUdev->dev.bus_type = &usb_bus_type;
602 pUdev->dev.driver_data = &usb_generic_driver_data;
603 strcpy(pUdev->dev.dev_name,"usbdev");
604
605 ms_usb_get_dev(pUdev);
606
607 if (pUdev->dev.bus_id[0] == 0)
608 USB_sprintf(&pUdev->dev.bus_id[0], "%d-%s", (int)pUdev->bus->busnum, pUdev->devpath);
609 pUdev->dev.dma_mask = parent->dma_mask;
610
611 switch (pUdev->eSpeed)
612 {
613 case USB_HIGH_SPEED:
614 i = 64;
615 break;
616 case USB_FULL_SPEED:
617 i = u8MaxPacketSize;
618 break;
619 case USB_LOW_SPEED:
620 i = 8;
621 break;
622
623 default:
624 goto fail;
625 }
626 pUdev->pEpMaxPacketIn [0] = i;
627 pUdev->pEpMaxPacketOut[0] = i;
628
629
630 for (i = 0; i < NEW_DEVICE_RETRYS; ++i)
631 {
632 for (j = 0; j < SET_ADDRESS_RETRYS; ++j)
633 {
634 s32Err = ms_usb_set_address(pUdev);
635 if (s32Err >= 0)
636 break;
637
638 diag_printf("Device state = %s, (i,j) = (%d, %d)\n",
639 pUdev->eState == 3 ? "default" :
640 pUdev->eState == 4 ? "address": "unknown", i, j);
641 //if (0) // unpatch from Linux
642 //{
643 // struct usb_hcd *hcd = pUdev->bus->hcpriv;
644 // if (hcd->isBadDevice)
645 // {
646 // diag_printf(" @ usb_new_device(): set address fail to set gIsBadDevice=1\n");
647 // break;
648 // }
649 //}
650
651 wait_ms(200);
652 }
653
654 if (s32Err < 0) {
655 ms_usbhost_err("device not accepting address %d, error %d\n", (int)pUdev->u32DevNum, s32Err);
656 goto fail;
657 }
658
659 wait_ms(10); /* Let the SET_ADDRESS settle */
660 s32Err = ms_usb_get_descriptor(pUdev, USB_DT_DEVICE, 0, &pUdev->descriptor, 8);
661 if (s32Err >= 8 || s32Err == -ENODEV) //Received 8 byress?
662 break;
663 wait_ms(100);
664 }
665
666 if (s32Err < 8)
667 {
668 ms_usbhost_err("device descriptor read/8, error %d\n", s32Err);
669 goto fail;
670 }
671
672 if (pUdev->eSpeed == USB_FULL_SPEED)
673 {
674 pUdev->pEpMaxPacketIn [0] = pUdev->descriptor.bMaxPacketSize0;
675 pUdev->pEpMaxPacketOut[0] = pUdev->descriptor.bMaxPacketSize0;
676 u8MaxPacketSize = pUdev->descriptor.bMaxPacketSize0;
677 }
678
679 s32Err = ms_usb_get_dev_descriptor(pUdev);
680 if (s32Err < (signed)sizeof(pUdev->descriptor))
681 {
682 ms_usbhost_err("device descriptor read/all, error %d\n", s32Err);
683 goto fail;
684 }
685
686 // annouce the VID/PID to the world
687 if (pUdev->parent) // not root hub
688 diag_printf("usb new device VID = %4x, PID = %4x\n", pUdev->descriptor.idVendor, pUdev->descriptor.idProduct);
689
690 #if USB_IF_EHSET_SUPPORT // embedded host electrical test procedure
691 if (pUdev->parent ) // not root hub
692 {
693 //dev->descriptor.idVendor = 0x1A0A; // test only, marked for fomal release
694 //dev->descriptor.idProduct = 0x0102;
695 //diag_printf("<usb_new_device> force VID = %x, PID = %x\n", pUdev->descriptor.idVendor, pUdev->descriptor.idProduct);
696 if (ms_usb_test_proc_vid(pUdev->descriptor.idVendor, pUdev->descriptor.idProduct) == 1)
697 ms_usb_high_speed_test_proc(pUdev, 1);
698 }
699 #endif
700
701 s32Err = ms_usb_get_config(pUdev);
702 if (s32Err < 0)
703 {
704 ms_usbhost_err("unable to get device %d configuration (error=%d)\n",
705 (int)pUdev->u32DevNum, s32Err);
706 goto fail;
707 }
708
709 if (pUdev->descriptor.bNumConfigurations != 1)
710 {
711 diag_printf("configuration #%d chosen from %d choices\n",
712 pUdev->config[0].desc.bConfigurationValue,
713 pUdev->descriptor.bNumConfigurations);
714 }
715 // Add string
716 if (pUdev->descriptor.iManufacturer)
717 {
718 int retv;
719
720 retv = ms_usb_string(pUdev, pUdev->descriptor.iManufacturer, usb_manufacturer_string_buf, 64);
721 if (retv > 0)
722 pUdev->pManufacturerString = usb_manufacturer_string_buf;
723 else
724 pUdev->pManufacturerString = NULL;
725 }
726 else
727 pUdev->pManufacturerString = NULL;
728 if (pUdev->pManufacturerString)
729 ms_usbhost_msg("USB device manufacturer string (index %d) = %s\n", pUdev->descriptor.iManufacturer, pUdev->pManufacturerString);
730 if (pUdev->descriptor.iProduct)
731 {
732 int retv;
733
734 retv = ms_usb_string(pUdev, pUdev->descriptor.iProduct, usb_product_string_buf, 64);
735 if (retv > 0)
736 pUdev->pProductString = usb_product_string_buf;
737 else
738 pUdev->pProductString = NULL;
739 }
740 else
741 pUdev->pProductString = NULL;
742 if (pUdev->pProductString)
743 ms_usbhost_msg("USB device product string (index %d) = %s\n", pUdev->descriptor.iProduct, pUdev->pProductString);
744 if (pUdev->descriptor.iSerialNumber)
745 {
746 int retv;
747
748 retv = ms_usb_string(pUdev, pUdev->descriptor.iSerialNumber, usb_serialnumber_string_buf, 64);
749 if (retv > 0)
750 pUdev->pSerialNumberString = usb_serialnumber_string_buf;
751 else
752 pUdev->pSerialNumberString = NULL;
753 }
754 else
755 pUdev->pSerialNumberString = NULL;
756 if (pUdev->pSerialNumberString)
757 ms_usbhost_msg("USB device serial number string (index %d) = %s\n", pUdev->descriptor.iSerialNumber, pUdev->pSerialNumberString);
758
759 //if(pUdev->descriptor.bDeviceClass == USB_CLASS_COMM)
760 // s32Err = ms_usb_set_config(pUdev, pUdev->config[1].desc.bConfigurationValue);
761 //else
762 s32Err = ms_usb_set_config(pUdev, pUdev->config[0].desc.bConfigurationValue);
763
764 if (s32Err)
765 {
766 ms_usbhost_err("failed to set device %d default configuration (error=%d)\n",
767 (int)pUdev->u32DevNum, s32Err);
768 goto fail;
769 }
770
771 #if USB_IF_EHSET_SUPPORT // embedded host electrical test procedure
772 if (pUdev->parent ) // not root hub
773 {
774 if (ms_usb_test_proc_vid(pUdev->descriptor.idVendor, pUdev->descriptor.idProduct) == 2)
775 ms_usb_high_speed_test_proc(pUdev, 2);
776 }
777 #endif
778
779 s32Err = ms_device_add (&pUdev->dev);
780 if (s32Err)
781 goto fail;
782
783 for (i = 0; i < pUdev->actconfig->desc.bNumInterfaces; i++)
784 {
785 struct usb_interface *iface = pUdev->actconfig->interface[i];
786 struct usb_interface_descriptor *i_desc;
787
788 i_desc = &iface->altsetting [iface->act_altsetting].desc;
789 iface->dev.parent = &pUdev->dev;
790 iface->dev.driver = NULL;
791 iface->dev.bus_type = &usb_bus_type;
792 iface->dev.dma_mask = parent->dma_mask;
793
794 strcpy(iface->dev.dev_name,"intf");
795 USB_sprintf(&iface->dev.bus_id[0], "%d-%s:%d",
796 (int)pUdev->bus->busnum, pUdev->devpath,
797 i_desc->bInterfaceNumber);
798 ms_usbhost_msg("%s %s - registering interface %s\n", pUdev->dev.dev_name, pUdev->dev.bus_id, iface->dev.bus_id);
799 ms_device_add (&iface->dev);
800 // Unsupport usb driverfs
801 }
802 return 0;
803 fail:
804 //pUdev->eState = USB_STATE_DEFAULT;
805 ms_usb_set_device_state(pUdev, USB_STATE_NOTATTACHED); // patch from Linux 3.1.10
806 //ms_clear_bit(pUdev->u32DevNum, pUdev->bus->devmap.usb_devicemap, unsigned long);
807 ms_devmap_clear_bit(pUdev->u32DevNum, pUdev->bus->devmap.usb_devicemap);
808 pUdev->u32DevNum = -1;
809 ms_usb_put_dev(pUdev);
810 return s32Err;
811 }
812
ms_usb_buffer_alloc(struct usb_device * pUdev,size_t size,int mem_flags,dma_addr_t * dma)813 void *ms_usb_buffer_alloc (
814 struct usb_device *pUdev,
815 size_t size,
816 int mem_flags,
817 dma_addr_t *dma
818 )
819 {
820 if (!pUdev || !pUdev->bus /*|| !pUdev->bus->bus_ops || !pUdev->bus->bus_ops->ms_alloc_hcd_buffer*/)
821 return 0;
822 //return pUdev->bus->bus_ops->ms_alloc_hcd_buffer (pUdev->bus, size, mem_flags, dma);
823 return ms_hcd_buffer_alloc(pUdev->bus, size, mem_flags, dma);
824 }
825
ms_usb_buffer_free(struct usb_device * pUdev,size_t size,void * addr,dma_addr_t dma)826 void ms_usb_buffer_free (
827 struct usb_device *pUdev,
828 size_t size,
829 void *addr,
830 dma_addr_t dma
831 )
832 {
833 if (!pUdev || !pUdev->bus /*|| !pUdev->bus->bus_ops || !pUdev->bus->bus_ops->ms_free_hcd_buffer*/)
834 return;
835 if (!addr)
836 return;
837 //pUdev->bus->bus_ops->ms_free_hcd_buffer (pUdev->bus, size, addr, dma);
838 ms_hcd_buffer_free(pUdev->bus, size, addr, dma);
839 }
840
841
842 struct ms_bus_type usb_bus_type = {
843 {&usb_bus_type.drivers_list,&usb_bus_type.drivers_list},
844 {&usb_bus_type.devices_list,&usb_bus_type.devices_list},
845 ms_usb_device_match,
846 };
847
ms_usb_core_init(void)848 int ms_usb_core_init(void)
849 {
850 ms_usb_hub_init();
851 ms_driver_register(&ms_usb_generic_driver);
852
853 return 0;
854 }
855
ms_usb_core_uninit(void)856 int ms_usb_core_uninit(void)
857 {
858 ms_usb_hub_uninit();
859 ms_driver_unregister(&ms_usb_generic_driver);
860
861 return 0;
862 }
863
864