選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ecm.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <errno.h>
  26. #include <ipxe/netdevice.h>
  27. #include <ipxe/ethernet.h>
  28. #include <ipxe/if_ether.h>
  29. #include <ipxe/base16.h>
  30. #include <ipxe/profile.h>
  31. #include <ipxe/usb.h>
  32. #include "ecm.h"
  33. /** @file
  34. *
  35. * CDC-ECM USB Ethernet driver
  36. *
  37. */
  38. /** Interrupt completion profiler */
  39. static struct profiler ecm_intr_profiler __profiler =
  40. { .name = "ecm.intr" };
  41. /** Bulk IN completion profiler */
  42. static struct profiler ecm_in_profiler __profiler =
  43. { .name = "ecm.in" };
  44. /** Bulk OUT profiler */
  45. static struct profiler ecm_out_profiler __profiler =
  46. { .name = "ecm.out" };
  47. /******************************************************************************
  48. *
  49. * Ethernet functional descriptor
  50. *
  51. ******************************************************************************
  52. */
  53. /**
  54. * Locate Ethernet functional descriptor
  55. *
  56. * @v config Configuration descriptor
  57. * @v interface Interface descriptor
  58. * @ret desc Descriptor, or NULL if not found
  59. */
  60. struct ecm_ethernet_descriptor *
  61. ecm_ethernet_descriptor ( struct usb_configuration_descriptor *config,
  62. struct usb_interface_descriptor *interface ) {
  63. struct ecm_ethernet_descriptor *desc;
  64. for_each_interface_descriptor ( desc, config, interface ) {
  65. if ( ( desc->header.type == USB_CS_INTERFACE_DESCRIPTOR ) &&
  66. ( desc->subtype == CDC_SUBTYPE_ETHERNET ) )
  67. return desc;
  68. }
  69. return NULL;
  70. }
  71. /**
  72. * Get hardware MAC address
  73. *
  74. * @v usb USB device
  75. * @v desc Ethernet functional descriptor
  76. * @v hw_addr Hardware address to fill in
  77. * @ret rc Return status code
  78. */
  79. int ecm_fetch_mac ( struct usb_device *usb,
  80. struct ecm_ethernet_descriptor *desc, uint8_t *hw_addr ) {
  81. char buf[ base16_encoded_len ( ETH_ALEN ) + 1 /* NUL */ ];
  82. int len;
  83. int rc;
  84. /* Fetch MAC address string */
  85. len = usb_get_string_descriptor ( usb, desc->mac, 0, buf,
  86. sizeof ( buf ) );
  87. if ( len < 0 ) {
  88. rc = len;
  89. return rc;
  90. }
  91. /* Sanity check */
  92. if ( len != ( ( int ) ( sizeof ( buf ) - 1 /* NUL */ ) ) )
  93. return -EINVAL;
  94. /* Decode MAC address */
  95. len = base16_decode ( buf, hw_addr, ETH_ALEN );
  96. if ( len < 0 ) {
  97. rc = len;
  98. return rc;
  99. }
  100. return 0;
  101. }
  102. /******************************************************************************
  103. *
  104. * CDC-ECM communications interface
  105. *
  106. ******************************************************************************
  107. */
  108. /**
  109. * Complete interrupt transfer
  110. *
  111. * @v ep USB endpoint
  112. * @v iobuf I/O buffer
  113. * @v rc Completion status code
  114. */
  115. static void ecm_intr_complete ( struct usb_endpoint *ep,
  116. struct io_buffer *iobuf, int rc ) {
  117. struct ecm_device *ecm = container_of ( ep, struct ecm_device,
  118. usbnet.intr );
  119. struct net_device *netdev = ecm->netdev;
  120. struct usb_setup_packet *message;
  121. size_t len = iob_len ( iobuf );
  122. /* Profile completions */
  123. profile_start ( &ecm_intr_profiler );
  124. /* Ignore packets cancelled when the endpoint closes */
  125. if ( ! ep->open )
  126. goto ignore;
  127. /* Drop packets with errors */
  128. if ( rc != 0 ) {
  129. DBGC ( ecm, "ECM %p interrupt failed: %s\n",
  130. ecm, strerror ( rc ) );
  131. DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
  132. goto error;
  133. }
  134. /* Extract message header */
  135. if ( len < sizeof ( *message ) ) {
  136. DBGC ( ecm, "ECM %p underlength interrupt:\n", ecm );
  137. DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
  138. rc = -EINVAL;
  139. goto error;
  140. }
  141. message = iobuf->data;
  142. /* Parse message header */
  143. switch ( message->request ) {
  144. case cpu_to_le16 ( CDC_NETWORK_CONNECTION ) :
  145. if ( message->value && ! netdev_link_ok ( netdev ) ) {
  146. DBGC ( ecm, "ECM %p link up\n", ecm );
  147. netdev_link_up ( netdev );
  148. } else if ( netdev_link_ok ( netdev ) && ! message->value ) {
  149. DBGC ( ecm, "ECM %p link down\n", ecm );
  150. netdev_link_down ( netdev );
  151. }
  152. break;
  153. case cpu_to_le16 ( CDC_CONNECTION_SPEED_CHANGE ) :
  154. /* Ignore */
  155. break;
  156. default:
  157. DBGC ( ecm, "ECM %p unrecognised interrupt:\n", ecm );
  158. DBGC_HDA ( ecm, 0, iobuf->data, iob_len ( iobuf ) );
  159. rc = -ENOTSUP;
  160. goto error;
  161. }
  162. /* Free I/O buffer */
  163. free_iob ( iobuf );
  164. profile_stop ( &ecm_intr_profiler );
  165. return;
  166. error:
  167. netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
  168. ignore:
  169. free_iob ( iobuf );
  170. return;
  171. }
  172. /** Interrupt endpoint operations */
  173. static struct usb_endpoint_driver_operations ecm_intr_operations = {
  174. .complete = ecm_intr_complete,
  175. };
  176. /******************************************************************************
  177. *
  178. * CDC-ECM data interface
  179. *
  180. ******************************************************************************
  181. */
  182. /**
  183. * Complete bulk IN transfer
  184. *
  185. * @v ep USB endpoint
  186. * @v iobuf I/O buffer
  187. * @v rc Completion status code
  188. */
  189. static void ecm_in_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  190. int rc ) {
  191. struct ecm_device *ecm = container_of ( ep, struct ecm_device,
  192. usbnet.in );
  193. struct net_device *netdev = ecm->netdev;
  194. /* Profile receive completions */
  195. profile_start ( &ecm_in_profiler );
  196. /* Ignore packets cancelled when the endpoint closes */
  197. if ( ! ep->open )
  198. goto ignore;
  199. /* Record USB errors against the network device */
  200. if ( rc != 0 ) {
  201. DBGC ( ecm, "ECM %p bulk IN failed: %s\n",
  202. ecm, strerror ( rc ) );
  203. goto error;
  204. }
  205. /* Hand off to network stack */
  206. netdev_rx ( netdev, iob_disown ( iobuf ) );
  207. profile_stop ( &ecm_in_profiler );
  208. return;
  209. error:
  210. netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
  211. ignore:
  212. free_iob ( iobuf );
  213. }
  214. /** Bulk IN endpoint operations */
  215. static struct usb_endpoint_driver_operations ecm_in_operations = {
  216. .complete = ecm_in_complete,
  217. };
  218. /**
  219. * Transmit packet
  220. *
  221. * @v ecm CDC-ECM device
  222. * @v iobuf I/O buffer
  223. * @ret rc Return status code
  224. */
  225. static int ecm_out_transmit ( struct ecm_device *ecm,
  226. struct io_buffer *iobuf ) {
  227. int rc;
  228. /* Profile transmissions */
  229. profile_start ( &ecm_out_profiler );
  230. /* Enqueue I/O buffer */
  231. if ( ( rc = usb_stream ( &ecm->usbnet.out, iobuf, 1 ) ) != 0 )
  232. return rc;
  233. profile_stop ( &ecm_out_profiler );
  234. return 0;
  235. }
  236. /**
  237. * Complete bulk OUT transfer
  238. *
  239. * @v ep USB endpoint
  240. * @v iobuf I/O buffer
  241. * @v rc Completion status code
  242. */
  243. static void ecm_out_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  244. int rc ) {
  245. struct ecm_device *ecm = container_of ( ep, struct ecm_device,
  246. usbnet.out );
  247. struct net_device *netdev = ecm->netdev;
  248. /* Report TX completion */
  249. netdev_tx_complete_err ( netdev, iobuf, rc );
  250. }
  251. /** Bulk OUT endpoint operations */
  252. static struct usb_endpoint_driver_operations ecm_out_operations = {
  253. .complete = ecm_out_complete,
  254. };
  255. /******************************************************************************
  256. *
  257. * Network device interface
  258. *
  259. ******************************************************************************
  260. */
  261. /**
  262. * Open network device
  263. *
  264. * @v netdev Network device
  265. * @ret rc Return status code
  266. */
  267. static int ecm_open ( struct net_device *netdev ) {
  268. struct ecm_device *ecm = netdev->priv;
  269. struct usb_device *usb = ecm->usb;
  270. unsigned int filter;
  271. int rc;
  272. /* Open USB network device */
  273. if ( ( rc = usbnet_open ( &ecm->usbnet ) ) != 0 ) {
  274. DBGC ( ecm, "ECM %p could not open: %s\n",
  275. ecm, strerror ( rc ) );
  276. goto err_open;
  277. }
  278. /* Set packet filter */
  279. filter = ( ECM_PACKET_TYPE_PROMISCUOUS |
  280. ECM_PACKET_TYPE_ALL_MULTICAST |
  281. ECM_PACKET_TYPE_DIRECTED |
  282. ECM_PACKET_TYPE_BROADCAST );
  283. if ( ( rc = usb_control ( usb, ECM_SET_ETHERNET_PACKET_FILTER,
  284. filter, ecm->usbnet.comms, NULL, 0 ) ) != 0 ){
  285. DBGC ( ecm, "ECM %p could not set packet filter: %s\n",
  286. ecm, strerror ( rc ) );
  287. goto err_set_filter;
  288. }
  289. return 0;
  290. err_set_filter:
  291. usbnet_close ( &ecm->usbnet );
  292. err_open:
  293. return rc;
  294. }
  295. /**
  296. * Close network device
  297. *
  298. * @v netdev Network device
  299. */
  300. static void ecm_close ( struct net_device *netdev ) {
  301. struct ecm_device *ecm = netdev->priv;
  302. /* Close USB network device */
  303. usbnet_close ( &ecm->usbnet );
  304. }
  305. /**
  306. * Transmit packet
  307. *
  308. * @v netdev Network device
  309. * @v iobuf I/O buffer
  310. * @ret rc Return status code
  311. */
  312. static int ecm_transmit ( struct net_device *netdev,
  313. struct io_buffer *iobuf ) {
  314. struct ecm_device *ecm = netdev->priv;
  315. int rc;
  316. /* Transmit packet */
  317. if ( ( rc = ecm_out_transmit ( ecm, iobuf ) ) != 0 )
  318. return rc;
  319. return 0;
  320. }
  321. /**
  322. * Poll for completed and received packets
  323. *
  324. * @v netdev Network device
  325. */
  326. static void ecm_poll ( struct net_device *netdev ) {
  327. struct ecm_device *ecm = netdev->priv;
  328. int rc;
  329. /* Poll USB bus */
  330. usb_poll ( ecm->bus );
  331. /* Refill endpoints */
  332. if ( ( rc = usbnet_refill ( &ecm->usbnet ) ) != 0 )
  333. netdev_rx_err ( netdev, NULL, rc );
  334. }
  335. /** CDC-ECM network device operations */
  336. static struct net_device_operations ecm_operations = {
  337. .open = ecm_open,
  338. .close = ecm_close,
  339. .transmit = ecm_transmit,
  340. .poll = ecm_poll,
  341. };
  342. /******************************************************************************
  343. *
  344. * USB interface
  345. *
  346. ******************************************************************************
  347. */
  348. /**
  349. * Probe device
  350. *
  351. * @v func USB function
  352. * @v config Configuration descriptor
  353. * @ret rc Return status code
  354. */
  355. static int ecm_probe ( struct usb_function *func,
  356. struct usb_configuration_descriptor *config ) {
  357. struct usb_device *usb = func->usb;
  358. struct net_device *netdev;
  359. struct ecm_device *ecm;
  360. struct usb_interface_descriptor *comms;
  361. struct ecm_ethernet_descriptor *ethernet;
  362. int rc;
  363. /* Allocate and initialise structure */
  364. netdev = alloc_etherdev ( sizeof ( *ecm ) );
  365. if ( ! netdev ) {
  366. rc = -ENOMEM;
  367. goto err_alloc;
  368. }
  369. netdev_init ( netdev, &ecm_operations );
  370. netdev->dev = &func->dev;
  371. ecm = netdev->priv;
  372. memset ( ecm, 0, sizeof ( *ecm ) );
  373. ecm->usb = usb;
  374. ecm->bus = usb->port->hub->bus;
  375. ecm->netdev = netdev;
  376. usbnet_init ( &ecm->usbnet, func, &ecm_intr_operations,
  377. &ecm_in_operations, &ecm_out_operations );
  378. usb_refill_init ( &ecm->usbnet.intr, 0, 0, ECM_INTR_MAX_FILL );
  379. usb_refill_init ( &ecm->usbnet.in, 0, ECM_IN_MTU, ECM_IN_MAX_FILL );
  380. DBGC ( ecm, "ECM %p on %s\n", ecm, func->name );
  381. /* Describe USB network device */
  382. if ( ( rc = usbnet_describe ( &ecm->usbnet, config ) ) != 0 ) {
  383. DBGC ( ecm, "ECM %p could not describe: %s\n",
  384. ecm, strerror ( rc ) );
  385. goto err_describe;
  386. }
  387. /* Locate Ethernet descriptor */
  388. comms = usb_interface_descriptor ( config, ecm->usbnet.comms, 0 );
  389. assert ( comms != NULL );
  390. ethernet = ecm_ethernet_descriptor ( config, comms );
  391. if ( ! ethernet ) {
  392. DBGC ( ecm, "ECM %p has no Ethernet descriptor\n", ecm );
  393. rc = -EINVAL;
  394. goto err_ethernet;
  395. }
  396. /* Fetch MAC address */
  397. if ( ( rc = ecm_fetch_mac ( usb, ethernet, netdev->hw_addr ) ) != 0 ) {
  398. DBGC ( ecm, "ECM %p could not fetch MAC address: %s\n",
  399. ecm, strerror ( rc ) );
  400. goto err_fetch_mac;
  401. }
  402. /* Register network device */
  403. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  404. goto err_register;
  405. usb_func_set_drvdata ( func, ecm );
  406. return 0;
  407. unregister_netdev ( netdev );
  408. err_register:
  409. err_fetch_mac:
  410. err_ethernet:
  411. err_describe:
  412. netdev_nullify ( netdev );
  413. netdev_put ( netdev );
  414. err_alloc:
  415. return rc;
  416. }
  417. /**
  418. * Remove device
  419. *
  420. * @v func USB function
  421. */
  422. static void ecm_remove ( struct usb_function *func ) {
  423. struct ecm_device *ecm = usb_func_get_drvdata ( func );
  424. struct net_device *netdev = ecm->netdev;
  425. unregister_netdev ( netdev );
  426. netdev_nullify ( netdev );
  427. netdev_put ( netdev );
  428. }
  429. /** CDC-ECM device IDs */
  430. static struct usb_device_id ecm_ids[] = {
  431. {
  432. .name = "cdc-ecm",
  433. .vendor = USB_ANY_ID,
  434. .product = USB_ANY_ID,
  435. },
  436. };
  437. /** CDC-ECM driver */
  438. struct usb_driver ecm_driver __usb_driver = {
  439. .ids = ecm_ids,
  440. .id_count = ( sizeof ( ecm_ids ) / sizeof ( ecm_ids[0] ) ),
  441. .class = USB_CLASS_ID ( USB_CLASS_CDC, USB_SUBCLASS_CDC_ECM, 0 ),
  442. .score = USB_SCORE_NORMAL,
  443. .probe = ecm_probe,
  444. .remove = ecm_remove,
  445. };