Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ncm.c 18KB

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