usb_ether.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. int j;
  75. for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
  76. if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
  77. continue;
  78. /*
  79. * ok, it is a supported eth device. Get info and fill it in
  80. */
  81. if (prob_dev[j].get_info(dev,
  82. &usb_eth[usb_max_eth_dev],
  83. &usb_eth[usb_max_eth_dev].eth_dev)) {
  84. /* found proper driver */
  85. /* register with networking stack */
  86. usb_max_eth_dev++;
  87. /*
  88. * usb_max_eth_dev must be incremented prior to this
  89. * call since eth_current_changed (internally called)
  90. * relies on it
  91. */
  92. eth_register(&usb_eth[usb_max_eth_dev - 1].eth_dev);
  93. break;
  94. }
  95. }
  96. }
  97. /*******************************************************************************
  98. * scan the usb and reports device info
  99. * to the user if mode = 1
  100. * returns current device or -1 if no
  101. */
  102. int usb_host_eth_scan(int mode)
  103. {
  104. int i, old_async;
  105. struct usb_device *dev;
  106. if (mode == 1)
  107. printf(" scanning bus for ethernet devices... ");
  108. old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
  109. for (i = 0; i < USB_MAX_ETH_DEV; i++)
  110. memset(&usb_eth[i], 0, sizeof(usb_eth[i]));
  111. for (i = 0; prob_dev[i].probe; i++) {
  112. if (prob_dev[i].before_probe)
  113. prob_dev[i].before_probe();
  114. }
  115. usb_max_eth_dev = 0;
  116. for (i = 0; i < USB_MAX_DEVICE; i++) {
  117. dev = usb_get_dev_index(i); /* get device */
  118. debug("i=%d\n", i);
  119. if (dev == NULL)
  120. break; /* no more devices avaiable */
  121. /* find valid usb_ether driver for this device, if any */
  122. probe_valid_drivers(dev);
  123. /* check limit */
  124. if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
  125. printf("max USB Ethernet Device reached: %d stopping\n",
  126. usb_max_eth_dev);
  127. break;
  128. }
  129. } /* for */
  130. usb_disable_asynch(old_async); /* restore asynch value */
  131. printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
  132. if (usb_max_eth_dev > 0)
  133. return 0;
  134. return -1;
  135. }