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 18KB

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