usb_ether.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. { }, /* END */
  37. };
  38. static int usb_max_eth_dev; /* number of highest available usb eth device */
  39. static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
  40. /*******************************************************************************
  41. * tell if current ethernet device is a usb dongle
  42. */
  43. int is_eth_dev_on_usb_host(void)
  44. {
  45. int i;
  46. struct eth_device *dev = eth_get_dev();
  47. if (dev) {
  48. for (i = 0; i < usb_max_eth_dev; i++)
  49. if (&usb_eth[i].eth_dev == dev)
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. /*
  55. * Given a USB device, ask each driver if it can support it, and attach it
  56. * to the first driver that says 'yes'
  57. */
  58. static void probe_valid_drivers(struct usb_device *dev)
  59. {
  60. int j;
  61. for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
  62. if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
  63. continue;
  64. /*
  65. * ok, it is a supported eth device. Get info and fill it in
  66. */
  67. if (prob_dev[j].get_info(dev,
  68. &usb_eth[usb_max_eth_dev],
  69. &usb_eth[usb_max_eth_dev].eth_dev)) {
  70. /* found proper driver */
  71. /* register with networking stack */
  72. usb_max_eth_dev++;
  73. /*
  74. * usb_max_eth_dev must be incremented prior to this
  75. * call since eth_current_changed (internally called)
  76. * relies on it
  77. */
  78. eth_register(&usb_eth[usb_max_eth_dev - 1].eth_dev);
  79. break;
  80. }
  81. }
  82. }
  83. /*******************************************************************************
  84. * scan the usb and reports device info
  85. * to the user if mode = 1
  86. * returns current device or -1 if no
  87. */
  88. int usb_host_eth_scan(int mode)
  89. {
  90. int i, old_async;
  91. struct usb_device *dev;
  92. if (mode == 1)
  93. printf(" scanning bus for ethernet devices... ");
  94. old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
  95. for (i = 0; i < USB_MAX_ETH_DEV; i++)
  96. memset(&usb_eth[i], 0, sizeof(usb_eth[i]));
  97. for (i = 0; prob_dev[i].probe; i++) {
  98. if (prob_dev[i].before_probe)
  99. prob_dev[i].before_probe();
  100. }
  101. usb_max_eth_dev = 0;
  102. for (i = 0; i < USB_MAX_DEVICE; i++) {
  103. dev = usb_get_dev_index(i); /* get device */
  104. debug("i=%d\n", i);
  105. if (dev == NULL)
  106. break; /* no more devices avaiable */
  107. /* find valid usb_ether driver for this device, if any */
  108. probe_valid_drivers(dev);
  109. /* check limit */
  110. if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
  111. printf("max USB Ethernet Device reached: %d stopping\n",
  112. usb_max_eth_dev);
  113. break;
  114. }
  115. } /* for */
  116. usb_disable_asynch(old_async); /* restore asynch value */
  117. printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
  118. if (usb_max_eth_dev > 0)
  119. return 0;
  120. return -1;
  121. }