usb_ether.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <usb.h>
  8. #include "usb_ether.h"
  9. typedef void (*usb_eth_before_probe)(void);
  10. typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum,
  11. struct ueth_data *ss);
  12. typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss,
  13. struct eth_device *dev_desc);
  14. struct usb_eth_prob_dev {
  15. usb_eth_before_probe before_probe; /* optional */
  16. usb_eth_probe probe;
  17. usb_eth_get_info get_info;
  18. };
  19. /* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */
  20. static const struct usb_eth_prob_dev prob_dev[] = {
  21. #ifdef CONFIG_USB_ETHER_ASIX
  22. {
  23. .before_probe = asix_eth_before_probe,
  24. .probe = asix_eth_probe,
  25. .get_info = asix_eth_get_info,
  26. },
  27. #endif
  28. #ifdef CONFIG_USB_ETHER_MCS7830
  29. {
  30. .before_probe = mcs7830_eth_before_probe,
  31. .probe = mcs7830_eth_probe,
  32. .get_info = mcs7830_eth_get_info,
  33. },
  34. #endif
  35. #ifdef CONFIG_USB_ETHER_SMSC95XX
  36. {
  37. .before_probe = smsc95xx_eth_before_probe,
  38. .probe = smsc95xx_eth_probe,
  39. .get_info = smsc95xx_eth_get_info,
  40. },
  41. #endif
  42. { }, /* END */
  43. };
  44. static int usb_max_eth_dev; /* number of highest available usb eth device */
  45. static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
  46. /*******************************************************************************
  47. * tell if current ethernet device is a usb dongle
  48. */
  49. int is_eth_dev_on_usb_host(void)
  50. {
  51. int i;
  52. struct eth_device *dev = eth_get_dev();
  53. if (dev) {
  54. for (i = 0; i < usb_max_eth_dev; i++)
  55. if (&usb_eth[i].eth_dev == dev)
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. /*
  61. * Given a USB device, ask each driver if it can support it, and attach it
  62. * to the first driver that says 'yes'
  63. */
  64. static void probe_valid_drivers(struct usb_device *dev)
  65. {
  66. struct eth_device *eth;
  67. int j;
  68. for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
  69. if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
  70. continue;
  71. /*
  72. * ok, it is a supported eth device. Get info and fill it in
  73. */
  74. eth = &usb_eth[usb_max_eth_dev].eth_dev;
  75. if (prob_dev[j].get_info(dev,
  76. &usb_eth[usb_max_eth_dev],
  77. eth)) {
  78. /* found proper driver */
  79. /* register with networking stack */
  80. usb_max_eth_dev++;
  81. /*
  82. * usb_max_eth_dev must be incremented prior to this
  83. * call since eth_current_changed (internally called)
  84. * relies on it
  85. */
  86. eth_register(eth);
  87. if (eth_write_hwaddr(eth, "usbeth",
  88. usb_max_eth_dev - 1))
  89. puts("Warning: failed to set MAC address\n");
  90. break;
  91. }
  92. }
  93. }
  94. /*******************************************************************************
  95. * scan the usb and reports device info
  96. * to the user if mode = 1
  97. * returns current device or -1 if no
  98. */
  99. int usb_host_eth_scan(int mode)
  100. {
  101. int i, old_async;
  102. struct usb_device *dev;
  103. if (mode == 1)
  104. printf(" scanning usb for ethernet devices... ");
  105. old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
  106. /* unregister a previously detected device */
  107. for (i = 0; i < usb_max_eth_dev; i++)
  108. eth_unregister(&usb_eth[i].eth_dev);
  109. memset(usb_eth, 0, sizeof(usb_eth));
  110. for (i = 0; prob_dev[i].probe; i++) {
  111. if (prob_dev[i].before_probe)
  112. prob_dev[i].before_probe();
  113. }
  114. usb_max_eth_dev = 0;
  115. for (i = 0; i < USB_MAX_DEVICE; i++) {
  116. dev = usb_get_dev_index(i); /* get device */
  117. debug("i=%d\n", i);
  118. if (dev == NULL)
  119. break; /* no more devices available */
  120. /* find valid usb_ether driver for this device, if any */
  121. probe_valid_drivers(dev);
  122. /* check limit */
  123. if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
  124. printf("max USB Ethernet Device reached: %d stopping\n",
  125. usb_max_eth_dev);
  126. break;
  127. }
  128. } /* for */
  129. usb_disable_asynch(old_async); /* restore asynch value */
  130. printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
  131. if (usb_max_eth_dev > 0)
  132. return 0;
  133. return -1;
  134. }