usb_ether.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. #include <common.h>
  22. #include <usb.h>
  23. #include "usb_ether.h"
  24. typedef void (*usb_eth_before_probe)(void);
  25. typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum,
  26. struct ueth_data *ss);
  27. typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss,
  28. struct eth_device *dev_desc);
  29. struct usb_eth_prob_dev {
  30. usb_eth_before_probe before_probe; /* optional */
  31. usb_eth_probe probe;
  32. usb_eth_get_info get_info;
  33. };
  34. /* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */
  35. static const struct usb_eth_prob_dev prob_dev[] = {
  36. #ifdef CONFIG_USB_ETHER_ASIX
  37. {
  38. .before_probe = asix_eth_before_probe,
  39. .probe = asix_eth_probe,
  40. .get_info = asix_eth_get_info,
  41. },
  42. #endif
  43. #ifdef CONFIG_USB_ETHER_SMSC95XX
  44. {
  45. .before_probe = smsc95xx_eth_before_probe,
  46. .probe = smsc95xx_eth_probe,
  47. .get_info = smsc95xx_eth_get_info,
  48. },
  49. #endif
  50. { }, /* END */
  51. };
  52. static int usb_max_eth_dev; /* number of highest available usb eth device */
  53. static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
  54. /*******************************************************************************
  55. * tell if current ethernet device is a usb dongle
  56. */
  57. int is_eth_dev_on_usb_host(void)
  58. {
  59. int i;
  60. struct eth_device *dev = eth_get_dev();
  61. if (dev) {
  62. for (i = 0; i < usb_max_eth_dev; i++)
  63. if (&usb_eth[i].eth_dev == dev)
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. /*
  69. * Given a USB device, ask each driver if it can support it, and attach it
  70. * to the first driver that says 'yes'
  71. */
  72. static void probe_valid_drivers(struct usb_device *dev)
  73. {
  74. struct eth_device *eth;
  75. int j;
  76. for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
  77. if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
  78. continue;
  79. /*
  80. * ok, it is a supported eth device. Get info and fill it in
  81. */
  82. eth = &usb_eth[usb_max_eth_dev].eth_dev;
  83. if (prob_dev[j].get_info(dev,
  84. &usb_eth[usb_max_eth_dev],
  85. eth)) {
  86. /* found proper driver */
  87. /* register with networking stack */
  88. usb_max_eth_dev++;
  89. /*
  90. * usb_max_eth_dev must be incremented prior to this
  91. * call since eth_current_changed (internally called)
  92. * relies on it
  93. */
  94. eth_register(eth);
  95. if (eth_write_hwaddr(eth, "usbeth",
  96. usb_max_eth_dev - 1))
  97. puts("Warning: failed to set MAC address\n");
  98. break;
  99. }
  100. }
  101. }
  102. /*******************************************************************************
  103. * scan the usb and reports device info
  104. * to the user if mode = 1
  105. * returns current device or -1 if no
  106. */
  107. int usb_host_eth_scan(int mode)
  108. {
  109. int i, old_async;
  110. struct usb_device *dev;
  111. if (mode == 1)
  112. printf(" scanning bus for ethernet devices... ");
  113. old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
  114. /* unregister a previously detected device */
  115. for (i = 0; i < usb_max_eth_dev; i++)
  116. eth_unregister(&usb_eth[i].eth_dev);
  117. memset(usb_eth, 0, sizeof(usb_eth));
  118. for (i = 0; prob_dev[i].probe; i++) {
  119. if (prob_dev[i].before_probe)
  120. prob_dev[i].before_probe();
  121. }
  122. usb_max_eth_dev = 0;
  123. for (i = 0; i < USB_MAX_DEVICE; i++) {
  124. dev = usb_get_dev_index(i); /* get device */
  125. debug("i=%d\n", i);
  126. if (dev == NULL)
  127. break; /* no more devices available */
  128. /* find valid usb_ether driver for this device, if any */
  129. probe_valid_drivers(dev);
  130. /* check limit */
  131. if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
  132. printf("max USB Ethernet Device reached: %d stopping\n",
  133. usb_max_eth_dev);
  134. break;
  135. }
  136. } /* for */
  137. usb_disable_asynch(old_async); /* restore asynch value */
  138. printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
  139. if (usb_max_eth_dev > 0)
  140. return 0;
  141. return -1;
  142. }