usb_ether.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <malloc.h>
  9. #include <usb.h>
  10. #include <dm/device-internal.h>
  11. #include "usb_ether.h"
  12. #ifdef CONFIG_DM_ETH
  13. #define USB_BULK_RECV_TIMEOUT 500
  14. int usb_ether_register(struct udevice *dev, struct ueth_data *ueth, int rxsize)
  15. {
  16. struct usb_device *udev = dev_get_parent_priv(dev);
  17. struct usb_interface_descriptor *iface_desc;
  18. bool ep_in_found = false, ep_out_found = false;
  19. struct usb_interface *iface;
  20. const int ifnum = 0; /* Always use interface 0 */
  21. int ret, i;
  22. iface = &udev->config.if_desc[ifnum];
  23. iface_desc = &udev->config.if_desc[ifnum].desc;
  24. /* Initialize the ueth_data structure with some useful info */
  25. ueth->ifnum = ifnum;
  26. ueth->subclass = iface_desc->bInterfaceSubClass;
  27. ueth->protocol = iface_desc->bInterfaceProtocol;
  28. /*
  29. * We are expecting a minimum of 3 endpoints - in, out (bulk), and int.
  30. * We will ignore any others.
  31. */
  32. for (i = 0; i < iface_desc->bNumEndpoints; i++) {
  33. int ep_addr = iface->ep_desc[i].bEndpointAddress;
  34. /* is it an BULK endpoint? */
  35. if ((iface->ep_desc[i].bmAttributes &
  36. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_BULK) {
  37. if (ep_addr & USB_DIR_IN && !ep_in_found) {
  38. ueth->ep_in = ep_addr &
  39. USB_ENDPOINT_NUMBER_MASK;
  40. ep_in_found = true;
  41. } else if (!(ep_addr & USB_DIR_IN) && !ep_out_found) {
  42. ueth->ep_out = ep_addr &
  43. USB_ENDPOINT_NUMBER_MASK;
  44. ep_out_found = true;
  45. }
  46. }
  47. /* is it an interrupt endpoint? */
  48. if ((iface->ep_desc[i].bmAttributes &
  49. USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT) {
  50. ueth->ep_int = iface->ep_desc[i].bEndpointAddress &
  51. USB_ENDPOINT_NUMBER_MASK;
  52. ueth->irqinterval = iface->ep_desc[i].bInterval;
  53. }
  54. }
  55. debug("Endpoints In %d Out %d Int %d\n", ueth->ep_in, ueth->ep_out,
  56. ueth->ep_int);
  57. /* Do some basic sanity checks, and bail if we find a problem */
  58. if (!ueth->ep_in || !ueth->ep_out || !ueth->ep_int) {
  59. debug("%s: %s: Cannot find endpoints\n", __func__, dev->name);
  60. return -ENXIO;
  61. }
  62. ueth->rxsize = rxsize;
  63. ueth->rxbuf = memalign(ARCH_DMA_MINALIGN, rxsize);
  64. if (!ueth->rxbuf)
  65. return -ENOMEM;
  66. ret = usb_set_interface(udev, iface_desc->bInterfaceNumber, ifnum);
  67. if (ret) {
  68. debug("%s: %s: Cannot set interface: %d\n", __func__, dev->name,
  69. ret);
  70. return ret;
  71. }
  72. ueth->pusb_dev = udev;
  73. return 0;
  74. }
  75. int usb_ether_deregister(struct ueth_data *ueth)
  76. {
  77. return 0;
  78. }
  79. int usb_ether_receive(struct ueth_data *ueth, int rxsize)
  80. {
  81. int actual_len;
  82. int ret;
  83. if (rxsize > ueth->rxsize)
  84. return -EINVAL;
  85. ret = usb_bulk_msg(ueth->pusb_dev,
  86. usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
  87. ueth->rxbuf, rxsize, &actual_len,
  88. USB_BULK_RECV_TIMEOUT);
  89. debug("Rx: len = %u, actual = %u, err = %d\n", rxsize, actual_len, ret);
  90. if (ret) {
  91. printf("Rx: failed to receive: %d\n", ret);
  92. return ret;
  93. }
  94. if (actual_len > rxsize) {
  95. debug("Rx: received too many bytes %d\n", actual_len);
  96. return -ENOSPC;
  97. }
  98. ueth->rxlen = actual_len;
  99. ueth->rxptr = 0;
  100. return actual_len ? 0 : -EAGAIN;
  101. }
  102. void usb_ether_advance_rxbuf(struct ueth_data *ueth, int num_bytes)
  103. {
  104. ueth->rxptr += num_bytes;
  105. if (num_bytes < 0 || ueth->rxptr >= ueth->rxlen)
  106. ueth->rxlen = 0;
  107. }
  108. int usb_ether_get_rx_bytes(struct ueth_data *ueth, uint8_t **ptrp)
  109. {
  110. if (!ueth->rxlen)
  111. return 0;
  112. *ptrp = &ueth->rxbuf[ueth->rxptr];
  113. return ueth->rxlen - ueth->rxptr;
  114. }
  115. #else
  116. typedef void (*usb_eth_before_probe)(void);
  117. typedef int (*usb_eth_probe)(struct usb_device *dev, unsigned int ifnum,
  118. struct ueth_data *ss);
  119. typedef int (*usb_eth_get_info)(struct usb_device *dev, struct ueth_data *ss,
  120. struct eth_device *dev_desc);
  121. struct usb_eth_prob_dev {
  122. usb_eth_before_probe before_probe; /* optional */
  123. usb_eth_probe probe;
  124. usb_eth_get_info get_info;
  125. };
  126. /* driver functions go here, each bracketed by #ifdef CONFIG_USB_ETHER_xxx */
  127. static const struct usb_eth_prob_dev prob_dev[] = {
  128. #ifdef CONFIG_USB_ETHER_ASIX
  129. {
  130. .before_probe = asix_eth_before_probe,
  131. .probe = asix_eth_probe,
  132. .get_info = asix_eth_get_info,
  133. },
  134. #endif
  135. #ifdef CONFIG_USB_ETHER_ASIX88179
  136. {
  137. .before_probe = ax88179_eth_before_probe,
  138. .probe = ax88179_eth_probe,
  139. .get_info = ax88179_eth_get_info,
  140. },
  141. #endif
  142. #ifdef CONFIG_USB_ETHER_MCS7830
  143. {
  144. .before_probe = mcs7830_eth_before_probe,
  145. .probe = mcs7830_eth_probe,
  146. .get_info = mcs7830_eth_get_info,
  147. },
  148. #endif
  149. #ifdef CONFIG_USB_ETHER_SMSC95XX
  150. {
  151. .before_probe = smsc95xx_eth_before_probe,
  152. .probe = smsc95xx_eth_probe,
  153. .get_info = smsc95xx_eth_get_info,
  154. },
  155. #endif
  156. #ifdef CONFIG_USB_ETHER_RTL8152
  157. {
  158. .before_probe = r8152_eth_before_probe,
  159. .probe = r8152_eth_probe,
  160. .get_info = r8152_eth_get_info,
  161. },
  162. #endif
  163. { }, /* END */
  164. };
  165. static int usb_max_eth_dev; /* number of highest available usb eth device */
  166. static struct ueth_data usb_eth[USB_MAX_ETH_DEV];
  167. /*******************************************************************************
  168. * tell if current ethernet device is a usb dongle
  169. */
  170. int is_eth_dev_on_usb_host(void)
  171. {
  172. int i;
  173. struct eth_device *dev = eth_get_dev();
  174. if (dev) {
  175. for (i = 0; i < usb_max_eth_dev; i++)
  176. if (&usb_eth[i].eth_dev == dev)
  177. return 1;
  178. }
  179. return 0;
  180. }
  181. /*
  182. * Given a USB device, ask each driver if it can support it, and attach it
  183. * to the first driver that says 'yes'
  184. */
  185. static void probe_valid_drivers(struct usb_device *dev)
  186. {
  187. struct eth_device *eth;
  188. int j;
  189. for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) {
  190. if (!prob_dev[j].probe(dev, 0, &usb_eth[usb_max_eth_dev]))
  191. continue;
  192. /*
  193. * ok, it is a supported eth device. Get info and fill it in
  194. */
  195. eth = &usb_eth[usb_max_eth_dev].eth_dev;
  196. if (prob_dev[j].get_info(dev,
  197. &usb_eth[usb_max_eth_dev],
  198. eth)) {
  199. /* found proper driver */
  200. /* register with networking stack */
  201. usb_max_eth_dev++;
  202. /*
  203. * usb_max_eth_dev must be incremented prior to this
  204. * call since eth_current_changed (internally called)
  205. * relies on it
  206. */
  207. eth_register(eth);
  208. if (eth_write_hwaddr(eth, "usbeth",
  209. usb_max_eth_dev - 1))
  210. puts("Warning: failed to set MAC address\n");
  211. break;
  212. }
  213. }
  214. }
  215. /*******************************************************************************
  216. * scan the usb and reports device info
  217. * to the user if mode = 1
  218. * returns current device or -1 if no
  219. */
  220. int usb_host_eth_scan(int mode)
  221. {
  222. int i, old_async;
  223. if (mode == 1)
  224. printf(" scanning usb for ethernet devices... ");
  225. old_async = usb_disable_asynch(1); /* asynch transfer not allowed */
  226. /* unregister a previously detected device */
  227. for (i = 0; i < usb_max_eth_dev; i++)
  228. eth_unregister(&usb_eth[i].eth_dev);
  229. memset(usb_eth, 0, sizeof(usb_eth));
  230. for (i = 0; prob_dev[i].probe; i++) {
  231. if (prob_dev[i].before_probe)
  232. prob_dev[i].before_probe();
  233. }
  234. usb_max_eth_dev = 0;
  235. #ifdef CONFIG_DM_USB
  236. /*
  237. * TODO: We should add U_BOOT_USB_DEVICE() declarations to each USB
  238. * Ethernet driver and then most of this file can be removed.
  239. */
  240. struct udevice *bus;
  241. struct uclass *uc;
  242. int ret;
  243. ret = uclass_get(UCLASS_USB, &uc);
  244. if (ret)
  245. return ret;
  246. uclass_foreach_dev(bus, uc) {
  247. for (i = 0; i < USB_MAX_DEVICE; i++) {
  248. struct usb_device *dev;
  249. dev = usb_get_dev_index(bus, i); /* get device */
  250. debug("i=%d, %s\n", i, dev ? dev->dev->name : "(done)");
  251. if (!dev)
  252. break; /* no more devices available */
  253. /*
  254. * find valid usb_ether driver for this device,
  255. * if any
  256. */
  257. probe_valid_drivers(dev);
  258. /* check limit */
  259. if (usb_max_eth_dev == USB_MAX_ETH_DEV)
  260. break;
  261. } /* for */
  262. }
  263. #else
  264. for (i = 0; i < USB_MAX_DEVICE; i++) {
  265. struct usb_device *dev;
  266. dev = usb_get_dev_index(i); /* get device */
  267. debug("i=%d\n", i);
  268. if (!dev)
  269. break; /* no more devices available */
  270. /* find valid usb_ether driver for this device, if any */
  271. probe_valid_drivers(dev);
  272. /* check limit */
  273. if (usb_max_eth_dev == USB_MAX_ETH_DEV)
  274. break;
  275. } /* for */
  276. #endif
  277. if (usb_max_eth_dev == USB_MAX_ETH_DEV) {
  278. printf("max USB Ethernet Device reached: %d stopping\n",
  279. usb_max_eth_dev);
  280. }
  281. usb_disable_asynch(old_async); /* restore asynch value */
  282. printf("%d Ethernet Device(s) found\n", usb_max_eth_dev);
  283. if (usb_max_eth_dev > 0)
  284. return 0;
  285. return -1;
  286. }
  287. #endif