usb_ether.c 8.1 KB

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