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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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, 0, 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. /* Set MAC address */
  397. if ( ( rc = usb_control ( usb, NCM_SET_NET_ADDRESS, 0,
  398. ncm->usbnet.comms, netdev->ll_addr,
  399. netdev->ll_protocol->ll_addr_len ) ) != 0 ) {
  400. DBGC ( ncm, "NCM %p could not set MAC address: %s\n",
  401. ncm, strerror ( rc ) );
  402. /* Ignore error and continue */
  403. }
  404. /* Open USB network device */
  405. if ( ( rc = usbnet_open ( &ncm->usbnet ) ) != 0 ) {
  406. DBGC ( ncm, "NCM %p could not open: %s\n",
  407. ncm, strerror ( rc ) );
  408. goto err_open;
  409. }
  410. return 0;
  411. usbnet_close ( &ncm->usbnet );
  412. err_open:
  413. err_set_ntb_input_size:
  414. usb_flush ( &ncm->usbnet.in );
  415. err_prefill:
  416. return rc;
  417. }
  418. /**
  419. * Close network device
  420. *
  421. * @v netdev Network device
  422. */
  423. static void ncm_close ( struct net_device *netdev ) {
  424. struct ncm_device *ncm = netdev->priv;
  425. /* Close USB network device */
  426. usbnet_close ( &ncm->usbnet );
  427. }
  428. /**
  429. * Transmit packet
  430. *
  431. * @v netdev Network device
  432. * @v iobuf I/O buffer
  433. * @ret rc Return status code
  434. */
  435. static int ncm_transmit ( struct net_device *netdev,
  436. struct io_buffer *iobuf ) {
  437. struct ncm_device *ncm = netdev->priv;
  438. int rc;
  439. /* Transmit packet */
  440. if ( ( rc = ncm_out_transmit ( ncm, iobuf ) ) != 0 )
  441. return rc;
  442. return 0;
  443. }
  444. /**
  445. * Poll for completed and received packets
  446. *
  447. * @v netdev Network device
  448. */
  449. static void ncm_poll ( struct net_device *netdev ) {
  450. struct ncm_device *ncm = netdev->priv;
  451. int rc;
  452. /* Poll USB bus */
  453. usb_poll ( ncm->bus );
  454. /* Refill endpoints */
  455. if ( ( rc = usbnet_refill ( &ncm->usbnet ) ) != 0 )
  456. netdev_rx_err ( netdev, NULL, rc );
  457. }
  458. /** CDC-NCM network device operations */
  459. static struct net_device_operations ncm_operations = {
  460. .open = ncm_open,
  461. .close = ncm_close,
  462. .transmit = ncm_transmit,
  463. .poll = ncm_poll,
  464. };
  465. /******************************************************************************
  466. *
  467. * USB interface
  468. *
  469. ******************************************************************************
  470. */
  471. /**
  472. * Probe device
  473. *
  474. * @v func USB function
  475. * @v config Configuration descriptor
  476. * @ret rc Return status code
  477. */
  478. static int ncm_probe ( struct usb_function *func,
  479. struct usb_configuration_descriptor *config ) {
  480. struct usb_device *usb = func->usb;
  481. struct net_device *netdev;
  482. struct ncm_device *ncm;
  483. struct usb_interface_descriptor *comms;
  484. struct ecm_ethernet_descriptor *ethernet;
  485. struct ncm_ntb_parameters params;
  486. int rc;
  487. /* Allocate and initialise structure */
  488. netdev = alloc_etherdev ( sizeof ( *ncm ) );
  489. if ( ! netdev ) {
  490. rc = -ENOMEM;
  491. goto err_alloc;
  492. }
  493. netdev_init ( netdev, &ncm_operations );
  494. netdev->dev = &func->dev;
  495. ncm = netdev->priv;
  496. memset ( ncm, 0, sizeof ( *ncm ) );
  497. ncm->usb = usb;
  498. ncm->bus = usb->port->hub->bus;
  499. ncm->netdev = netdev;
  500. usbnet_init ( &ncm->usbnet, func, &ncm_intr_operations,
  501. &ncm_in_operations, &ncm_out_operations );
  502. usb_refill_init ( &ncm->usbnet.intr, 0, 0, NCM_INTR_COUNT );
  503. DBGC ( ncm, "NCM %p on %s\n", ncm, func->name );
  504. /* Describe USB network device */
  505. if ( ( rc = usbnet_describe ( &ncm->usbnet, config ) ) != 0 ) {
  506. DBGC ( ncm, "NCM %p could not describe: %s\n",
  507. ncm, strerror ( rc ) );
  508. goto err_describe;
  509. }
  510. /* Locate Ethernet descriptor */
  511. comms = usb_interface_descriptor ( config, ncm->usbnet.comms, 0 );
  512. assert ( comms != NULL );
  513. ethernet = ecm_ethernet_descriptor ( config, comms );
  514. if ( ! ethernet ) {
  515. DBGC ( ncm, "NCM %p has no Ethernet descriptor\n", ncm );
  516. rc = -EINVAL;
  517. goto err_ethernet;
  518. }
  519. /* Fetch MAC address */
  520. if ( ( rc = ecm_fetch_mac ( usb, ethernet, netdev->hw_addr ) ) != 0 ) {
  521. DBGC ( ncm, "NCM %p could not fetch MAC address: %s\n",
  522. ncm, strerror ( rc ) );
  523. goto err_fetch_mac;
  524. }
  525. /* Get NTB parameters */
  526. if ( ( rc = usb_control ( usb, NCM_GET_NTB_PARAMETERS, 0,
  527. ncm->usbnet.comms, &params,
  528. sizeof ( params ) ) ) != 0 ) {
  529. DBGC ( ncm, "NCM %p could not get NTB parameters: %s\n",
  530. ncm, strerror ( rc ) );
  531. goto err_ntb_parameters;
  532. }
  533. /* Get maximum supported input size */
  534. ncm->mtu = le32_to_cpu ( params.in.mtu );
  535. DBGC2 ( ncm, "NCM %p maximum IN size is %zd bytes\n", ncm, ncm->mtu );
  536. /* Calculate transmit padding */
  537. ncm->padding = ( ( le16_to_cpu ( params.out.remainder ) -
  538. sizeof ( struct ncm_ntb_header ) - ETH_HLEN ) &
  539. ( le16_to_cpu ( params.out.divisor ) - 1 ) );
  540. DBGC2 ( ncm, "NCM %p using %zd-byte transmit padding\n",
  541. ncm, ncm->padding );
  542. assert ( ( ( sizeof ( struct ncm_ntb_header ) + ncm->padding +
  543. ETH_HLEN ) % le16_to_cpu ( params.out.divisor ) ) ==
  544. le16_to_cpu ( params.out.remainder ) );
  545. /* Register network device */
  546. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  547. goto err_register;
  548. usb_func_set_drvdata ( func, ncm );
  549. return 0;
  550. unregister_netdev ( netdev );
  551. err_register:
  552. err_ntb_parameters:
  553. err_fetch_mac:
  554. err_ethernet:
  555. err_describe:
  556. netdev_nullify ( netdev );
  557. netdev_put ( netdev );
  558. err_alloc:
  559. return rc;
  560. }
  561. /**
  562. * Remove device
  563. *
  564. * @v func USB function
  565. */
  566. static void ncm_remove ( struct usb_function *func ) {
  567. struct ncm_device *ncm = usb_func_get_drvdata ( func );
  568. struct net_device *netdev = ncm->netdev;
  569. unregister_netdev ( netdev );
  570. netdev_nullify ( netdev );
  571. netdev_put ( netdev );
  572. }
  573. /** CDC-NCM device IDs */
  574. static struct usb_device_id ncm_ids[] = {
  575. {
  576. .name = "cdc-ncm",
  577. .vendor = USB_ANY_ID,
  578. .product = USB_ANY_ID,
  579. },
  580. };
  581. /** CDC-NCM driver */
  582. struct usb_driver ncm_driver __usb_driver = {
  583. .ids = ncm_ids,
  584. .id_count = ( sizeof ( ncm_ids ) / sizeof ( ncm_ids[0] ) ),
  585. .class = USB_CLASS_ID ( USB_CLASS_CDC, USB_SUBCLASS_CDC_NCM, 0 ),
  586. .score = USB_SCORE_NORMAL,
  587. .probe = ncm_probe,
  588. .remove = ncm_remove,
  589. };