You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ncm.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <ipxe/netdevice.h>
  23. #include <ipxe/ethernet.h>
  24. #include <ipxe/if_ether.h>
  25. #include <ipxe/profile.h>
  26. #include <ipxe/usb.h>
  27. #include <ipxe/usbnet.h>
  28. #include "ecm.h"
  29. #include "ncm.h"
  30. /** @file
  31. *
  32. * CDC-NCM USB Ethernet driver
  33. *
  34. */
  35. /** Interrupt completion profiler */
  36. static struct profiler ncm_intr_profiler __profiler =
  37. { .name = "ncm.intr" };
  38. /** Bulk IN completion profiler */
  39. static struct profiler ncm_in_profiler __profiler =
  40. { .name = "ncm.in" };
  41. /** Bulk IN per-datagram profiler */
  42. static struct profiler ncm_in_datagram_profiler __profiler =
  43. { .name = "ncm.in_dgram" };
  44. /** Bulk OUT profiler */
  45. static struct profiler ncm_out_profiler __profiler =
  46. { .name = "ncm.out" };
  47. /******************************************************************************
  48. *
  49. * CDC-NCM communications interface
  50. *
  51. ******************************************************************************
  52. */
  53. /**
  54. * Complete interrupt transfer
  55. *
  56. * @v ep USB endpoint
  57. * @v iobuf I/O buffer
  58. * @v rc Completion status code
  59. */
  60. static void ncm_intr_complete ( struct usb_endpoint *ep,
  61. struct io_buffer *iobuf, int rc ) {
  62. struct ncm_device *ncm = container_of ( ep, struct ncm_device,
  63. usbnet.intr );
  64. struct net_device *netdev = ncm->netdev;
  65. struct usb_setup_packet *message;
  66. size_t len = iob_len ( iobuf );
  67. /* Profile completions */
  68. profile_start ( &ncm_intr_profiler );
  69. /* Ignore packets cancelled when the endpoint closes */
  70. if ( ! ep->open )
  71. goto ignore;
  72. /* Ignore packets with errors */
  73. if ( rc != 0 ) {
  74. DBGC ( ncm, "NCM %p interrupt failed: %s\n",
  75. ncm, strerror ( rc ) );
  76. DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
  77. goto error;
  78. }
  79. /* Extract message header */
  80. if ( len < sizeof ( *message ) ) {
  81. DBGC ( ncm, "NCM %p underlength interrupt:\n", ncm );
  82. DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
  83. rc = -EINVAL;
  84. goto error;
  85. }
  86. message = iobuf->data;
  87. /* Parse message header */
  88. switch ( message->request ) {
  89. case cpu_to_le16 ( CDC_NETWORK_CONNECTION ) :
  90. if ( message->value ) {
  91. DBGC ( ncm, "NCM %p link up\n", ncm );
  92. netdev_link_up ( netdev );
  93. } else {
  94. DBGC ( ncm, "NCM %p link down\n", ncm );
  95. netdev_link_down ( netdev );
  96. }
  97. break;
  98. case cpu_to_le16 ( CDC_CONNECTION_SPEED_CHANGE ) :
  99. /* Ignore */
  100. break;
  101. default:
  102. DBGC ( ncm, "NCM %p unrecognised interrupt:\n", ncm );
  103. DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
  104. goto error;
  105. }
  106. /* Free I/O buffer */
  107. free_iob ( iobuf );
  108. profile_stop ( &ncm_intr_profiler );
  109. return;
  110. error:
  111. netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
  112. ignore:
  113. free_iob ( iobuf );
  114. return;
  115. }
  116. /** Interrupt endpoint operations */
  117. static struct usb_endpoint_driver_operations ncm_intr_operations = {
  118. .complete = ncm_intr_complete,
  119. };
  120. /******************************************************************************
  121. *
  122. * CDC-NCM data interface
  123. *
  124. ******************************************************************************
  125. */
  126. /**
  127. * Prefill bulk IN endpoint
  128. *
  129. * @v ncm CDC-NCM device
  130. * @ret rc Return status code
  131. */
  132. static int ncm_in_prefill ( struct ncm_device *ncm ) {
  133. size_t mtu;
  134. unsigned int count;
  135. int rc;
  136. /* Some devices have a very small number of internal buffers,
  137. * and rely on being able to pack multiple packets into each
  138. * buffer. We therefore want to use large buffers if
  139. * possible. However, large allocations have a reasonable
  140. * chance of failure, especially if this is not the first or
  141. * only device to be opened.
  142. *
  143. * We therefore attempt to find a usable buffer size, starting
  144. * large and working downwards until allocation succeeds.
  145. * Smaller buffers will still work, albeit with a higher
  146. * chance of packet loss and so lower overall throughput.
  147. */
  148. for ( mtu = ncm->mtu ; mtu >= NCM_MIN_NTB_INPUT_SIZE ; mtu >>= 1 ) {
  149. /* Attempt allocation at this MTU */
  150. if ( mtu > NCM_MAX_NTB_INPUT_SIZE )
  151. continue;
  152. count = ( NCM_IN_MIN_SIZE / mtu );
  153. if ( count < NCM_IN_MIN_COUNT )
  154. count = NCM_IN_MIN_COUNT;
  155. if ( ( count * mtu ) > NCM_IN_MAX_SIZE )
  156. continue;
  157. usb_refill_init ( &ncm->usbnet.in, mtu, count );
  158. if ( ( rc = usb_prefill ( &ncm->usbnet.in ) ) != 0 ) {
  159. DBGC ( ncm, "NCM %p could not prefill %dx %zd-byte "
  160. "buffers for bulk IN\n", ncm, count, mtu );
  161. continue;
  162. }
  163. DBGC ( ncm, "NCM %p using %dx %zd-byte buffers for bulk IN\n",
  164. ncm, count, mtu );
  165. return 0;
  166. }
  167. DBGC ( ncm, "NCM %p could not prefill bulk IN endpoint\n", ncm );
  168. return -ENOMEM;
  169. }
  170. /**
  171. * Complete bulk IN transfer
  172. *
  173. * @v ep USB endpoint
  174. * @v iobuf I/O buffer
  175. * @v rc Completion status code
  176. */
  177. static void ncm_in_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  178. int rc ) {
  179. struct ncm_device *ncm = container_of ( ep, struct ncm_device,
  180. usbnet.in );
  181. struct net_device *netdev = ncm->netdev;
  182. struct ncm_transfer_header *nth;
  183. struct ncm_datagram_pointer *ndp;
  184. struct ncm_datagram_descriptor *desc;
  185. struct io_buffer *pkt;
  186. unsigned int remaining;
  187. size_t ndp_offset;
  188. size_t ndp_len;
  189. size_t pkt_offset;
  190. size_t pkt_len;
  191. size_t headroom;
  192. size_t len;
  193. /* Profile overall bulk IN completion */
  194. profile_start ( &ncm_in_profiler );
  195. /* Ignore packets cancelled when the endpoint closes */
  196. if ( ! ep->open )
  197. goto ignore;
  198. /* Record USB errors against the network device */
  199. if ( rc != 0 ) {
  200. DBGC ( ncm, "NCM %p bulk IN failed: %s\n",
  201. ncm, strerror ( rc ) );
  202. goto error;
  203. }
  204. /* Locate transfer header */
  205. len = iob_len ( iobuf );
  206. if ( sizeof ( *nth ) > len ) {
  207. DBGC ( ncm, "NCM %p packet too short for NTH:\n", ncm );
  208. rc = -EINVAL;
  209. goto error;
  210. }
  211. nth = iobuf->data;
  212. /* Locate datagram pointer */
  213. ndp_offset = le16_to_cpu ( nth->offset );
  214. if ( ( ndp_offset + sizeof ( *ndp ) ) > len ) {
  215. DBGC ( ncm, "NCM %p packet too short for NDP:\n", ncm );
  216. rc = -EINVAL;
  217. goto error;
  218. }
  219. ndp = ( iobuf->data + ndp_offset );
  220. ndp_len = le16_to_cpu ( ndp->header_len );
  221. if ( ndp_len < offsetof ( typeof ( *ndp ), desc ) ) {
  222. DBGC ( ncm, "NCM %p NDP header length too short:\n", ncm );
  223. rc = -EINVAL;
  224. goto error;
  225. }
  226. if ( ( ndp_offset + ndp_len ) > len ) {
  227. DBGC ( ncm, "NCM %p packet too short for NDP:\n", ncm );
  228. rc = -EINVAL;
  229. goto error;
  230. }
  231. /* Process datagrams */
  232. remaining = ( ( ndp_len - offsetof ( typeof ( *ndp ), desc ) ) /
  233. sizeof ( ndp->desc[0] ) );
  234. for ( desc = ndp->desc ; remaining && desc->offset ; remaining-- ) {
  235. /* Profile individual datagrams */
  236. profile_start ( &ncm_in_datagram_profiler );
  237. /* Locate datagram */
  238. pkt_offset = le16_to_cpu ( desc->offset );
  239. pkt_len = le16_to_cpu ( desc->len );
  240. if ( pkt_len < ETH_HLEN ) {
  241. DBGC ( ncm, "NCM %p underlength datagram:\n", ncm );
  242. rc = -EINVAL;
  243. goto error;
  244. }
  245. if ( ( pkt_offset + pkt_len ) > len ) {
  246. DBGC ( ncm, "NCM %p datagram exceeds packet:\n", ncm );
  247. rc = -EINVAL;
  248. goto error;
  249. }
  250. /* Move to next descriptor */
  251. desc++;
  252. /* Copy data to a new I/O buffer. Our USB buffers may
  253. * be very large and so we choose to recycle the
  254. * buffers directly rather than attempt reallocation
  255. * while the device is running. We therefore copy the
  256. * data to a new I/O buffer even if this is the only
  257. * (or last) packet within the buffer.
  258. *
  259. * We reserve enough space at the start of each buffer
  260. * to allow for our own transmission header, to
  261. * support protocols such as ARP which may modify the
  262. * received packet and reuse the same I/O buffer for
  263. * transmission.
  264. */
  265. headroom = ( sizeof ( struct ncm_ntb_header ) + ncm->padding );
  266. pkt = alloc_iob ( headroom + pkt_len );
  267. if ( ! pkt ) {
  268. /* Record error and continue */
  269. netdev_rx_err ( netdev, NULL, -ENOMEM );
  270. continue;
  271. }
  272. iob_reserve ( pkt, headroom );
  273. memcpy ( iob_put ( pkt, pkt_len ),
  274. ( iobuf->data + pkt_offset ), pkt_len );
  275. /* Strip CRC, if present */
  276. if ( ndp->magic & cpu_to_le32 ( NCM_DATAGRAM_POINTER_MAGIC_CRC))
  277. iob_unput ( pkt, 4 /* CRC32 */ );
  278. /* Hand off to network stack */
  279. netdev_rx ( netdev, pkt );
  280. profile_stop ( &ncm_in_datagram_profiler );
  281. }
  282. /* Recycle I/O buffer */
  283. usb_recycle ( &ncm->usbnet.in, iobuf );
  284. profile_stop ( &ncm_in_profiler );
  285. return;
  286. error:
  287. /* Record error against network device */
  288. DBGC_HDA ( ncm, 0, iobuf->data, iob_len ( iobuf ) );
  289. netdev_rx_err ( netdev, NULL, rc );
  290. ignore:
  291. usb_recycle ( &ncm->usbnet.in, iobuf );
  292. }
  293. /** Bulk IN endpoint operations */
  294. static struct usb_endpoint_driver_operations ncm_in_operations = {
  295. .complete = ncm_in_complete,
  296. };
  297. /**
  298. * Transmit packet
  299. *
  300. * @v ncm CDC-NCM device
  301. * @v iobuf I/O buffer
  302. * @ret rc Return status code
  303. */
  304. static int ncm_out_transmit ( struct ncm_device *ncm,
  305. struct io_buffer *iobuf ) {
  306. struct ncm_ntb_header *header;
  307. size_t len = iob_len ( iobuf );
  308. size_t header_len = ( sizeof ( *header ) + ncm->padding );
  309. int rc;
  310. /* Profile transmissions */
  311. profile_start ( &ncm_out_profiler );
  312. /* Prepend header */
  313. if ( ( rc = iob_ensure_headroom ( iobuf, header_len ) ) != 0 )
  314. return rc;
  315. header = iob_push ( iobuf, header_len );
  316. /* Populate header */
  317. header->nth.magic = cpu_to_le32 ( NCM_TRANSFER_HEADER_MAGIC );
  318. header->nth.header_len = cpu_to_le16 ( sizeof ( header->nth ) );
  319. header->nth.sequence = cpu_to_le16 ( ncm->sequence );
  320. header->nth.len = cpu_to_le16 ( iob_len ( iobuf ) );
  321. header->nth.offset =
  322. cpu_to_le16 ( offsetof ( typeof ( *header ), ndp ) );
  323. header->ndp.magic = cpu_to_le32 ( NCM_DATAGRAM_POINTER_MAGIC );
  324. header->ndp.header_len = cpu_to_le16 ( sizeof ( header->ndp ) +
  325. sizeof ( header->desc ) );
  326. header->ndp.offset = cpu_to_le16 ( 0 );
  327. header->desc[0].offset = cpu_to_le16 ( header_len );
  328. header->desc[0].len = cpu_to_le16 ( len );
  329. memset ( &header->desc[1], 0, sizeof ( header->desc[1] ) );
  330. /* Enqueue I/O buffer */
  331. if ( ( rc = usb_stream ( &ncm->usbnet.out, iobuf, 0 ) ) != 0 )
  332. return rc;
  333. /* Increment sequence number */
  334. ncm->sequence++;
  335. profile_stop ( &ncm_out_profiler );
  336. return 0;
  337. }
  338. /**
  339. * Complete bulk OUT transfer
  340. *
  341. * @v ep USB endpoint
  342. * @v iobuf I/O buffer
  343. * @v rc Completion status code
  344. */
  345. static void ncm_out_complete ( struct usb_endpoint *ep, struct io_buffer *iobuf,
  346. int rc ) {
  347. struct ncm_device *ncm = container_of ( ep, struct ncm_device,
  348. usbnet.out );
  349. struct net_device *netdev = ncm->netdev;
  350. /* Report TX completion */
  351. netdev_tx_complete_err ( netdev, iobuf, rc );
  352. }
  353. /** Bulk OUT endpoint operations */
  354. static struct usb_endpoint_driver_operations ncm_out_operations = {
  355. .complete = ncm_out_complete,
  356. };
  357. /******************************************************************************
  358. *
  359. * Network device interface
  360. *
  361. ******************************************************************************
  362. */
  363. /**
  364. * Open network device
  365. *
  366. * @v netdev Network device
  367. * @ret rc Return status code
  368. */
  369. static int ncm_open ( struct net_device *netdev ) {
  370. struct ncm_device *ncm = netdev->priv;
  371. struct usb_device *usb = ncm->usb;
  372. struct ncm_set_ntb_input_size size;
  373. int rc;
  374. /* Reset sequence number */
  375. ncm->sequence = 0;
  376. /* Prefill I/O buffers */
  377. if ( ( rc = ncm_in_prefill ( ncm ) ) != 0 )
  378. goto err_prefill;
  379. /* Set maximum input size */
  380. memset ( &size, 0, sizeof ( size ) );
  381. size.mtu = cpu_to_le32 ( ncm->usbnet.in.len );
  382. if ( ( rc = usb_control ( usb, NCM_SET_NTB_INPUT_SIZE, 0,
  383. ncm->usbnet.comms, &size,
  384. sizeof ( size ) ) ) != 0 ) {
  385. DBGC ( ncm, "NCM %p could not set input size to %zd: %s\n",
  386. ncm, ncm->usbnet.in.len, strerror ( rc ) );
  387. goto err_set_ntb_input_size;
  388. }
  389. /* Open USB network device */
  390. if ( ( rc = usbnet_open ( &ncm->usbnet ) ) != 0 ) {
  391. DBGC ( ncm, "NCM %p could not open: %s\n",
  392. ncm, strerror ( rc ) );
  393. goto err_open;
  394. }
  395. return 0;
  396. usbnet_close ( &ncm->usbnet );
  397. err_open:
  398. err_set_ntb_input_size:
  399. usb_flush ( &ncm->usbnet.in );
  400. err_prefill:
  401. return rc;
  402. }
  403. /**
  404. * Close network device
  405. *
  406. * @v netdev Network device
  407. */
  408. static void ncm_close ( struct net_device *netdev ) {
  409. struct ncm_device *ncm = netdev->priv;
  410. /* Close USB network device */
  411. usbnet_close ( &ncm->usbnet );
  412. }
  413. /**
  414. * Transmit packet
  415. *
  416. * @v netdev Network device
  417. * @v iobuf I/O buffer
  418. * @ret rc Return status code
  419. */
  420. static int ncm_transmit ( struct net_device *netdev,
  421. struct io_buffer *iobuf ) {
  422. struct ncm_device *ncm = netdev->priv;
  423. int rc;
  424. /* Transmit packet */
  425. if ( ( rc = ncm_out_transmit ( ncm, iobuf ) ) != 0 )
  426. return rc;
  427. return 0;
  428. }
  429. /**
  430. * Poll for completed and received packets
  431. *
  432. * @v netdev Network device
  433. */
  434. static void ncm_poll ( struct net_device *netdev ) {
  435. struct ncm_device *ncm = netdev->priv;
  436. int rc;
  437. /* Poll USB bus */
  438. usb_poll ( ncm->bus );
  439. /* Refill endpoints */
  440. if ( ( rc = usbnet_refill ( &ncm->usbnet ) ) != 0 )
  441. netdev_rx_err ( netdev, NULL, rc );
  442. }
  443. /** CDC-NCM network device operations */
  444. static struct net_device_operations ncm_operations = {
  445. .open = ncm_open,
  446. .close = ncm_close,
  447. .transmit = ncm_transmit,
  448. .poll = ncm_poll,
  449. };
  450. /******************************************************************************
  451. *
  452. * USB interface
  453. *
  454. ******************************************************************************
  455. */
  456. /**
  457. * Probe device
  458. *
  459. * @v func USB function
  460. * @v config Configuration descriptor
  461. * @ret rc Return status code
  462. */
  463. static int ncm_probe ( struct usb_function *func,
  464. struct usb_configuration_descriptor *config ) {
  465. struct usb_device *usb = func->usb;
  466. struct net_device *netdev;
  467. struct ncm_device *ncm;
  468. struct usb_interface_descriptor *comms;
  469. struct ecm_ethernet_descriptor *ethernet;
  470. struct ncm_ntb_parameters params;
  471. int rc;
  472. /* Allocate and initialise structure */
  473. netdev = alloc_etherdev ( sizeof ( *ncm ) );
  474. if ( ! netdev ) {
  475. rc = -ENOMEM;
  476. goto err_alloc;
  477. }
  478. netdev_init ( netdev, &ncm_operations );
  479. netdev->dev = &func->dev;
  480. ncm = netdev->priv;
  481. memset ( ncm, 0, sizeof ( *ncm ) );
  482. ncm->usb = usb;
  483. ncm->bus = usb->port->hub->bus;
  484. ncm->netdev = netdev;
  485. usbnet_init ( &ncm->usbnet, func, &ncm_intr_operations,
  486. &ncm_in_operations, &ncm_out_operations );
  487. usb_refill_init ( &ncm->usbnet.intr, 0, NCM_INTR_COUNT );
  488. DBGC ( ncm, "NCM %p on %s\n", ncm, func->name );
  489. /* Describe USB network device */
  490. if ( ( rc = usbnet_describe ( &ncm->usbnet, config ) ) != 0 ) {
  491. DBGC ( ncm, "NCM %p could not describe: %s\n",
  492. ncm, strerror ( rc ) );
  493. goto err_describe;
  494. }
  495. /* Locate Ethernet descriptor */
  496. comms = usb_interface_descriptor ( config, ncm->usbnet.comms, 0 );
  497. assert ( comms != NULL );
  498. ethernet = ecm_ethernet_descriptor ( config, comms );
  499. if ( ! ethernet ) {
  500. DBGC ( ncm, "NCM %p has no Ethernet descriptor\n", ncm );
  501. rc = -EINVAL;
  502. goto err_ethernet;
  503. }
  504. /* Fetch MAC address */
  505. if ( ( rc = ecm_fetch_mac ( usb, ethernet, netdev->hw_addr ) ) != 0 ) {
  506. DBGC ( ncm, "NCM %p could not fetch MAC address: %s\n",
  507. ncm, strerror ( rc ) );
  508. goto err_fetch_mac;
  509. }
  510. /* Get NTB parameters */
  511. if ( ( rc = usb_control ( usb, NCM_GET_NTB_PARAMETERS, 0,
  512. ncm->usbnet.comms, &params,
  513. sizeof ( params ) ) ) != 0 ) {
  514. DBGC ( ncm, "NCM %p could not get NTB parameters: %s\n",
  515. ncm, strerror ( rc ) );
  516. goto err_ntb_parameters;
  517. }
  518. /* Get maximum supported input size */
  519. ncm->mtu = le32_to_cpu ( params.in.mtu );
  520. DBGC2 ( ncm, "NCM %p maximum IN size is %zd bytes\n", ncm, ncm->mtu );
  521. /* Calculate transmit padding */
  522. ncm->padding = ( ( le16_to_cpu ( params.out.remainder ) -
  523. sizeof ( struct ncm_ntb_header ) - ETH_HLEN ) &
  524. ( le16_to_cpu ( params.out.divisor ) - 1 ) );
  525. DBGC2 ( ncm, "NCM %p using %zd-byte transmit padding\n",
  526. ncm, ncm->padding );
  527. assert ( ( ( sizeof ( struct ncm_ntb_header ) + ncm->padding +
  528. ETH_HLEN ) % le16_to_cpu ( params.out.divisor ) ) ==
  529. le16_to_cpu ( params.out.remainder ) );
  530. /* Register network device */
  531. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  532. goto err_register;
  533. usb_func_set_drvdata ( func, ncm );
  534. return 0;
  535. unregister_netdev ( netdev );
  536. err_register:
  537. err_ntb_parameters:
  538. err_fetch_mac:
  539. err_ethernet:
  540. err_describe:
  541. netdev_nullify ( netdev );
  542. netdev_put ( netdev );
  543. err_alloc:
  544. return rc;
  545. }
  546. /**
  547. * Remove device
  548. *
  549. * @v func USB function
  550. */
  551. static void ncm_remove ( struct usb_function *func ) {
  552. struct ncm_device *ncm = usb_func_get_drvdata ( func );
  553. struct net_device *netdev = ncm->netdev;
  554. unregister_netdev ( netdev );
  555. netdev_nullify ( netdev );
  556. netdev_put ( netdev );
  557. }
  558. /** CDC-NCM device IDs */
  559. static struct usb_device_id ncm_ids[] = {
  560. {
  561. .name = "cdc-ncm",
  562. .vendor = USB_ANY_ID,
  563. .product = USB_ANY_ID,
  564. .class = {
  565. .class = USB_CLASS_CDC,
  566. .subclass = USB_SUBCLASS_CDC_NCM,
  567. .protocol = 0,
  568. },
  569. },
  570. };
  571. /** CDC-NCM driver */
  572. struct usb_driver ncm_driver __usb_driver = {
  573. .ids = ncm_ids,
  574. .id_count = ( sizeof ( ncm_ids ) / sizeof ( ncm_ids[0] ) ),
  575. .probe = ncm_probe,
  576. .remove = ncm_remove,
  577. };