usb_ether.c 8.2 KB

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