Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

smsc95xx.c 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Copyright (C) 2015 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 <unistd.h>
  26. #include <errno.h>
  27. #include <byteswap.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/usb.h>
  30. #include <ipxe/usbnet.h>
  31. #include <ipxe/profile.h>
  32. #include <ipxe/base16.h>
  33. #include <ipxe/smbios.h>
  34. #include "smsc95xx.h"
  35. /** @file
  36. *
  37. * SMSC LAN95xx USB Ethernet driver
  38. *
  39. */
  40. /** Bulk IN completion profiler */
  41. static struct profiler smsc95xx_in_profiler __profiler =
  42. { .name = "smsc95xx.in" };
  43. /** Bulk OUT profiler */
  44. static struct profiler smsc95xx_out_profiler __profiler =
  45. { .name = "smsc95xx.out" };
  46. /******************************************************************************
  47. *
  48. * MAC address
  49. *
  50. ******************************************************************************
  51. */
  52. /**
  53. * Construct MAC address for Honeywell VM3
  54. *
  55. * @v smscusb SMSC USB device
  56. * @ret rc Return status code
  57. */
  58. static int smsc95xx_vm3_fetch_mac ( struct smscusb_device *smscusb ) {
  59. struct net_device *netdev = smscusb->netdev;
  60. struct smbios_structure structure;
  61. struct smbios_system_information system;
  62. struct {
  63. char manufacturer[ 10 /* "Honeywell" + NUL */ ];
  64. char product[ 4 /* "VM3" + NUL */ ];
  65. char mac[ base16_encoded_len ( ETH_ALEN ) + 1 /* NUL */ ];
  66. } strings;
  67. int len;
  68. int rc;
  69. /* Find system information */
  70. if ( ( rc = find_smbios_structure ( SMBIOS_TYPE_SYSTEM_INFORMATION, 0,
  71. &structure ) ) != 0 ) {
  72. DBGC ( smscusb, "SMSC95XX %p could not find system "
  73. "information: %s\n", smscusb, strerror ( rc ) );
  74. return rc;
  75. }
  76. /* Read system information */
  77. if ( ( rc = read_smbios_structure ( &structure, &system,
  78. sizeof ( system ) ) ) != 0 ) {
  79. DBGC ( smscusb, "SMSC95XX %p could not read system "
  80. "information: %s\n", smscusb, strerror ( rc ) );
  81. return rc;
  82. }
  83. /* NUL-terminate all strings to be fetched */
  84. memset ( &strings, 0, sizeof ( strings ) );
  85. /* Fetch system manufacturer name */
  86. len = read_smbios_string ( &structure, system.manufacturer,
  87. strings.manufacturer,
  88. ( sizeof ( strings.manufacturer ) - 1 ) );
  89. if ( len < 0 ) {
  90. rc = len;
  91. DBGC ( smscusb, "SMSC95XX %p could not read manufacturer "
  92. "name: %s\n", smscusb, strerror ( rc ) );
  93. return rc;
  94. }
  95. /* Fetch system product name */
  96. len = read_smbios_string ( &structure, system.product, strings.product,
  97. ( sizeof ( strings.product ) - 1 ) );
  98. if ( len < 0 ) {
  99. rc = len;
  100. DBGC ( smscusb, "SMSC95XX %p could not read product name: "
  101. "%s\n", smscusb, strerror ( rc ) );
  102. return rc;
  103. }
  104. /* Ignore non-VM3 devices */
  105. if ( ( strcmp ( strings.manufacturer, "Honeywell" ) != 0 ) ||
  106. ( strcmp ( strings.product, "VM3" ) != 0 ) )
  107. return -ENOTTY;
  108. /* Find OEM strings */
  109. if ( ( rc = find_smbios_structure ( SMBIOS_TYPE_OEM_STRINGS, 0,
  110. &structure ) ) != 0 ) {
  111. DBGC ( smscusb, "SMSC95XX %p could not find OEM strings: %s\n",
  112. smscusb, strerror ( rc ) );
  113. return rc;
  114. }
  115. /* Fetch MAC address */
  116. len = read_smbios_string ( &structure, SMSC95XX_VM3_OEM_STRING_MAC,
  117. strings.mac, ( sizeof ( strings.mac ) - 1 ));
  118. if ( len < 0 ) {
  119. rc = len;
  120. DBGC ( smscusb, "SMSC95XX %p could not read OEM string: %s\n",
  121. smscusb, strerror ( rc ) );
  122. return rc;
  123. }
  124. /* Sanity check */
  125. if ( len != ( ( int ) ( sizeof ( strings.mac ) - 1 ) ) ) {
  126. DBGC ( smscusb, "SMSC95XX %p invalid MAC address \"%s\"\n",
  127. smscusb, strings.mac );
  128. return -EINVAL;
  129. }
  130. /* Decode MAC address */
  131. len = base16_decode ( strings.mac, netdev->hw_addr, ETH_ALEN );
  132. if ( len < 0 ) {
  133. rc = len;
  134. DBGC ( smscusb, "SMSC95XX %p invalid MAC address \"%s\"\n",
  135. smscusb, strings.mac );
  136. return rc;
  137. }
  138. DBGC ( smscusb, "SMSC95XX %p using VM3 MAC %s\n",
  139. smscusb, eth_ntoa ( netdev->hw_addr ) );
  140. return 0;
  141. }
  142. /**
  143. * Fetch MAC address
  144. *
  145. * @v smscusb SMSC USB device
  146. * @ret rc Return status code
  147. */
  148. static int smsc95xx_fetch_mac ( struct smscusb_device *smscusb ) {
  149. struct net_device *netdev = smscusb->netdev;
  150. int rc;
  151. /* Read MAC address from EEPROM, if present */
  152. if ( ( rc = smscusb_eeprom_fetch_mac ( smscusb,
  153. SMSC95XX_E2P_BASE ) ) == 0 )
  154. return 0;
  155. /* Construct MAC address for Honeywell VM3, if applicable */
  156. if ( ( rc = smsc95xx_vm3_fetch_mac ( smscusb ) ) == 0 )
  157. return 0;
  158. /* Otherwise, generate a random MAC address */
  159. eth_random_addr ( netdev->hw_addr );
  160. DBGC ( smscusb, "SMSC95XX %p using random MAC %s\n",
  161. smscusb, eth_ntoa ( netdev->hw_addr ) );
  162. return 0;
  163. }
  164. /******************************************************************************
  165. *
  166. * Statistics (for debugging)
  167. *
  168. ******************************************************************************
  169. */
  170. /**
  171. * Dump statistics (for debugging)
  172. *
  173. * @v smscusb SMSC USB device
  174. * @ret rc Return status code
  175. */
  176. static int smsc95xx_dump_statistics ( struct smscusb_device *smscusb ) {
  177. struct smsc95xx_rx_statistics rx;
  178. struct smsc95xx_tx_statistics tx;
  179. int rc;
  180. /* Do nothing unless debugging is enabled */
  181. if ( ! DBG_LOG )
  182. return 0;
  183. /* Get RX statistics */
  184. if ( ( rc = smscusb_get_statistics ( smscusb, SMSC95XX_RX_STATISTICS,
  185. &rx, sizeof ( rx ) ) ) != 0 ) {
  186. DBGC ( smscusb, "SMSC95XX %p could not get RX statistics: "
  187. "%s\n", smscusb, strerror ( rc ) );
  188. return rc;
  189. }
  190. /* Get TX statistics */
  191. if ( ( rc = smscusb_get_statistics ( smscusb, SMSC95XX_TX_STATISTICS,
  192. &tx, sizeof ( tx ) ) ) != 0 ) {
  193. DBGC ( smscusb, "SMSC95XX %p could not get TX statistics: "
  194. "%s\n", smscusb, strerror ( rc ) );
  195. return rc;
  196. }
  197. /* Dump statistics */
  198. DBGC ( smscusb, "SMSC95XX %p RX good %d bad %d crc %d und %d aln %d "
  199. "ovr %d lat %d drp %d\n", smscusb, le32_to_cpu ( rx.good ),
  200. le32_to_cpu ( rx.bad ), le32_to_cpu ( rx.crc ),
  201. le32_to_cpu ( rx.undersize ), le32_to_cpu ( rx.alignment ),
  202. le32_to_cpu ( rx.oversize ), le32_to_cpu ( rx.late ),
  203. le32_to_cpu ( rx.dropped ) );
  204. DBGC ( smscusb, "SMSC95XX %p TX good %d bad %d pau %d sgl %d mul %d "
  205. "exc %d lat %d und %d def %d car %d\n", smscusb,
  206. le32_to_cpu ( tx.good ), le32_to_cpu ( tx.bad ),
  207. le32_to_cpu ( tx.pause ), le32_to_cpu ( tx.single ),
  208. le32_to_cpu ( tx.multiple ), le32_to_cpu ( tx.excessive ),
  209. le32_to_cpu ( tx.late ), le32_to_cpu ( tx.underrun ),
  210. le32_to_cpu ( tx.deferred ), le32_to_cpu ( tx.carrier ) );
  211. return 0;
  212. }
  213. /******************************************************************************
  214. *
  215. * Device reset
  216. *
  217. ******************************************************************************
  218. */
  219. /**
  220. * Reset device
  221. *
  222. * @v smscusb SMSC USB device
  223. * @ret rc Return status code
  224. */
  225. static int smsc95xx_reset ( struct smscusb_device *smscusb ) {
  226. uint32_t hw_cfg;
  227. uint32_t led_gpio_cfg;
  228. int rc;
  229. /* Reset device */
  230. if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_HW_CFG,
  231. SMSC95XX_HW_CFG_LRST ) ) != 0 )
  232. return rc;
  233. /* Wait for reset to complete */
  234. udelay ( SMSC95XX_RESET_DELAY_US );
  235. /* Check that reset has completed */
  236. if ( ( rc = smscusb_readl ( smscusb, SMSC95XX_HW_CFG, &hw_cfg ) ) != 0 )
  237. return rc;
  238. if ( hw_cfg & SMSC95XX_HW_CFG_LRST ) {
  239. DBGC ( smscusb, "SMSC95XX %p failed to reset\n", smscusb );
  240. return -ETIMEDOUT;
  241. }
  242. /* Configure LEDs */
  243. led_gpio_cfg = ( SMSC95XX_LED_GPIO_CFG_GPCTL2_NSPD_LED |
  244. SMSC95XX_LED_GPIO_CFG_GPCTL1_NLNKA_LED |
  245. SMSC95XX_LED_GPIO_CFG_GPCTL0_NFDX_LED );
  246. if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_LED_GPIO_CFG,
  247. led_gpio_cfg ) ) != 0 ) {
  248. DBGC ( smscusb, "SMSC95XX %p could not configure LEDs: %s\n",
  249. smscusb, strerror ( rc ) );
  250. /* Ignore error and continue */
  251. }
  252. return 0;
  253. }
  254. /******************************************************************************
  255. *
  256. * Endpoint operations
  257. *
  258. ******************************************************************************
  259. */
  260. /**
  261. * Complete bulk IN transfer
  262. *
  263. * @v ep USB endpoint
  264. * @v iobuf I/O buffer
  265. * @v rc Completion status code
  266. */
  267. static void smsc95xx_in_complete ( struct usb_endpoint *ep,
  268. struct io_buffer *iobuf, int rc ) {
  269. struct smscusb_device *smscusb =
  270. container_of ( ep, struct smscusb_device, usbnet.in );
  271. struct net_device *netdev = smscusb->netdev;
  272. struct smsc95xx_rx_header *header;
  273. /* Profile completions */
  274. profile_start ( &smsc95xx_in_profiler );
  275. /* Ignore packets cancelled when the endpoint closes */
  276. if ( ! ep->open ) {
  277. free_iob ( iobuf );
  278. return;
  279. }
  280. /* Record USB errors against the network device */
  281. if ( rc != 0 ) {
  282. DBGC ( smscusb, "SMSC95XX %p bulk IN failed: %s\n",
  283. smscusb, strerror ( rc ) );
  284. goto err;
  285. }
  286. /* Sanity check */
  287. if ( iob_len ( iobuf ) < ( sizeof ( *header ) + 4 /* CRC */ ) ) {
  288. DBGC ( smscusb, "SMSC95XX %p underlength bulk IN\n",
  289. smscusb );
  290. DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
  291. rc = -EINVAL;
  292. goto err;
  293. }
  294. /* Strip header and CRC */
  295. header = iobuf->data;
  296. iob_pull ( iobuf, sizeof ( *header ) );
  297. iob_unput ( iobuf, 4 /* CRC */ );
  298. /* Check for errors */
  299. if ( header->command & cpu_to_le32 ( SMSC95XX_RX_RUNT |
  300. SMSC95XX_RX_LATE |
  301. SMSC95XX_RX_CRC ) ) {
  302. DBGC ( smscusb, "SMSC95XX %p receive error (%08x):\n",
  303. smscusb, le32_to_cpu ( header->command ) );
  304. DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
  305. rc = -EIO;
  306. goto err;
  307. }
  308. /* Hand off to network stack */
  309. netdev_rx ( netdev, iob_disown ( iobuf ) );
  310. profile_stop ( &smsc95xx_in_profiler );
  311. return;
  312. err:
  313. /* Hand off to network stack */
  314. netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
  315. }
  316. /** Bulk IN endpoint operations */
  317. static struct usb_endpoint_driver_operations smsc95xx_in_operations = {
  318. .complete = smsc95xx_in_complete,
  319. };
  320. /**
  321. * Transmit packet
  322. *
  323. * @v smscusb SMSC USB device
  324. * @v iobuf I/O buffer
  325. * @ret rc Return status code
  326. */
  327. static int smsc95xx_out_transmit ( struct smscusb_device *smscusb,
  328. struct io_buffer *iobuf ) {
  329. struct smsc95xx_tx_header *header;
  330. size_t len = iob_len ( iobuf );
  331. int rc;
  332. /* Profile transmissions */
  333. profile_start ( &smsc95xx_out_profiler );
  334. /* Prepend header */
  335. if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *header ) ) ) != 0 )
  336. return rc;
  337. header = iob_push ( iobuf, sizeof ( *header ) );
  338. header->command = cpu_to_le32 ( SMSC95XX_TX_FIRST | SMSC95XX_TX_LAST |
  339. SMSC95XX_TX_LEN ( len ) );
  340. header->len = cpu_to_le32 ( len );
  341. /* Enqueue I/O buffer */
  342. if ( ( rc = usb_stream ( &smscusb->usbnet.out, iobuf, 0 ) ) != 0 )
  343. return rc;
  344. profile_stop ( &smsc95xx_out_profiler );
  345. return 0;
  346. }
  347. /******************************************************************************
  348. *
  349. * Network device interface
  350. *
  351. ******************************************************************************
  352. */
  353. /**
  354. * Open network device
  355. *
  356. * @v netdev Network device
  357. * @ret rc Return status code
  358. */
  359. static int smsc95xx_open ( struct net_device *netdev ) {
  360. struct smscusb_device *smscusb = netdev->priv;
  361. int rc;
  362. /* Clear stored interrupt status */
  363. smscusb->int_sts = 0;
  364. /* Configure bulk IN empty response */
  365. if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_HW_CFG,
  366. SMSC95XX_HW_CFG_BIR ) ) != 0 )
  367. goto err_hw_cfg;
  368. /* Open USB network device */
  369. if ( ( rc = usbnet_open ( &smscusb->usbnet ) ) != 0 ) {
  370. DBGC ( smscusb, "SMSC95XX %p could not open: %s\n",
  371. smscusb, strerror ( rc ) );
  372. goto err_open;
  373. }
  374. /* Configure interrupt endpoint */
  375. if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_INT_EP_CTL,
  376. ( SMSC95XX_INT_EP_CTL_RXDF_EN |
  377. SMSC95XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
  378. goto err_int_ep_ctl;
  379. /* Configure bulk IN delay */
  380. if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_BULK_IN_DLY,
  381. SMSC95XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
  382. goto err_bulk_in_dly;
  383. /* Configure MAC */
  384. if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_MAC_CR,
  385. ( SMSC95XX_MAC_CR_RXALL |
  386. SMSC95XX_MAC_CR_FDPX |
  387. SMSC95XX_MAC_CR_MCPAS |
  388. SMSC95XX_MAC_CR_PRMS |
  389. SMSC95XX_MAC_CR_PASSBAD |
  390. SMSC95XX_MAC_CR_TXEN |
  391. SMSC95XX_MAC_CR_RXEN ) ) ) != 0 )
  392. goto err_mac_cr;
  393. /* Configure transmit datapath */
  394. if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_TX_CFG,
  395. SMSC95XX_TX_CFG_ON ) ) != 0 )
  396. goto err_tx_cfg;
  397. /* Set MAC address */
  398. if ( ( rc = smscusb_set_address ( smscusb, SMSC95XX_ADDR_BASE ) ) != 0 )
  399. goto err_set_address;
  400. /* Enable PHY interrupts and update link status */
  401. if ( ( rc = smscusb_mii_open ( smscusb ) ) != 0 )
  402. goto err_mii_open;
  403. return 0;
  404. err_mii_open:
  405. err_set_address:
  406. err_tx_cfg:
  407. err_mac_cr:
  408. err_bulk_in_dly:
  409. err_int_ep_ctl:
  410. usbnet_close ( &smscusb->usbnet );
  411. err_open:
  412. err_hw_cfg:
  413. smsc95xx_reset ( smscusb );
  414. return rc;
  415. }
  416. /**
  417. * Close network device
  418. *
  419. * @v netdev Network device
  420. */
  421. static void smsc95xx_close ( struct net_device *netdev ) {
  422. struct smscusb_device *smscusb = netdev->priv;
  423. /* Close USB network device */
  424. usbnet_close ( &smscusb->usbnet );
  425. /* Dump statistics (for debugging) */
  426. smsc95xx_dump_statistics ( smscusb );
  427. /* Reset device */
  428. smsc95xx_reset ( smscusb );
  429. }
  430. /**
  431. * Transmit packet
  432. *
  433. * @v netdev Network device
  434. * @v iobuf I/O buffer
  435. * @ret rc Return status code
  436. */
  437. static int smsc95xx_transmit ( struct net_device *netdev,
  438. struct io_buffer *iobuf ) {
  439. struct smscusb_device *smscusb = netdev->priv;
  440. int rc;
  441. /* Transmit packet */
  442. if ( ( rc = smsc95xx_out_transmit ( smscusb, iobuf ) ) != 0 )
  443. return rc;
  444. return 0;
  445. }
  446. /**
  447. * Poll for completed and received packets
  448. *
  449. * @v netdev Network device
  450. */
  451. static void smsc95xx_poll ( struct net_device *netdev ) {
  452. struct smscusb_device *smscusb = netdev->priv;
  453. uint32_t int_sts;
  454. int rc;
  455. /* Poll USB bus */
  456. usb_poll ( smscusb->bus );
  457. /* Refill endpoints */
  458. if ( ( rc = usbnet_refill ( &smscusb->usbnet ) ) != 0 )
  459. netdev_rx_err ( netdev, NULL, rc );
  460. /* Do nothing more unless there are interrupts to handle */
  461. int_sts = smscusb->int_sts;
  462. if ( ! int_sts )
  463. return;
  464. /* Check link status if applicable */
  465. if ( int_sts & SMSC95XX_INT_STS_PHY_INT ) {
  466. smscusb_mii_check_link ( smscusb );
  467. int_sts &= ~SMSC95XX_INT_STS_PHY_INT;
  468. }
  469. /* Record RX FIFO overflow if applicable */
  470. if ( int_sts & SMSC95XX_INT_STS_RXDF_INT ) {
  471. DBGC2 ( smscusb, "SMSC95XX %p RX FIFO overflowed\n",
  472. smscusb );
  473. netdev_rx_err ( netdev, NULL, -ENOBUFS );
  474. int_sts &= ~SMSC95XX_INT_STS_RXDF_INT;
  475. }
  476. /* Check for unexpected interrupts */
  477. if ( int_sts ) {
  478. DBGC ( smscusb, "SMSC95XX %p unexpected interrupt %#08x\n",
  479. smscusb, int_sts );
  480. netdev_rx_err ( netdev, NULL, -ENOTTY );
  481. }
  482. /* Clear interrupts */
  483. if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_INT_STS,
  484. smscusb->int_sts ) ) != 0 )
  485. netdev_rx_err ( netdev, NULL, rc );
  486. smscusb->int_sts = 0;
  487. }
  488. /** SMSC95xx network device operations */
  489. static struct net_device_operations smsc95xx_operations = {
  490. .open = smsc95xx_open,
  491. .close = smsc95xx_close,
  492. .transmit = smsc95xx_transmit,
  493. .poll = smsc95xx_poll,
  494. };
  495. /******************************************************************************
  496. *
  497. * USB interface
  498. *
  499. ******************************************************************************
  500. */
  501. /**
  502. * Probe device
  503. *
  504. * @v func USB function
  505. * @v config Configuration descriptor
  506. * @ret rc Return status code
  507. */
  508. static int smsc95xx_probe ( struct usb_function *func,
  509. struct usb_configuration_descriptor *config ) {
  510. struct net_device *netdev;
  511. struct smscusb_device *smscusb;
  512. int rc;
  513. /* Allocate and initialise structure */
  514. netdev = alloc_etherdev ( sizeof ( *smscusb ) );
  515. if ( ! netdev ) {
  516. rc = -ENOMEM;
  517. goto err_alloc;
  518. }
  519. netdev_init ( netdev, &smsc95xx_operations );
  520. netdev->dev = &func->dev;
  521. smscusb = netdev->priv;
  522. memset ( smscusb, 0, sizeof ( *smscusb ) );
  523. smscusb_init ( smscusb, netdev, func, &smsc95xx_in_operations );
  524. smscusb_mii_init ( smscusb, SMSC95XX_MII_BASE );
  525. usb_refill_init ( &smscusb->usbnet.in,
  526. ( sizeof ( struct smsc95xx_tx_header ) -
  527. sizeof ( struct smsc95xx_rx_header ) ),
  528. SMSC95XX_IN_MTU, SMSC95XX_IN_MAX_FILL );
  529. DBGC ( smscusb, "SMSC95XX %p on %s\n", smscusb, func->name );
  530. /* Describe USB network device */
  531. if ( ( rc = usbnet_describe ( &smscusb->usbnet, config ) ) != 0 ) {
  532. DBGC ( smscusb, "SMSC95XX %p could not describe: %s\n",
  533. smscusb, strerror ( rc ) );
  534. goto err_describe;
  535. }
  536. /* Reset device */
  537. if ( ( rc = smsc95xx_reset ( smscusb ) ) != 0 )
  538. goto err_reset;
  539. /* Read MAC address */
  540. if ( ( rc = smsc95xx_fetch_mac ( smscusb ) ) != 0 )
  541. goto err_fetch_mac;
  542. /* Register network device */
  543. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  544. goto err_register;
  545. usb_func_set_drvdata ( func, netdev );
  546. return 0;
  547. unregister_netdev ( netdev );
  548. err_register:
  549. err_fetch_mac:
  550. err_reset:
  551. err_describe:
  552. netdev_nullify ( netdev );
  553. netdev_put ( netdev );
  554. err_alloc:
  555. return rc;
  556. }
  557. /**
  558. * Remove device
  559. *
  560. * @v func USB function
  561. */
  562. static void smsc95xx_remove ( struct usb_function *func ) {
  563. struct net_device *netdev = usb_func_get_drvdata ( func );
  564. unregister_netdev ( netdev );
  565. netdev_nullify ( netdev );
  566. netdev_put ( netdev );
  567. }
  568. /** SMSC95xx device IDs */
  569. static struct usb_device_id smsc95xx_ids[] = {
  570. {
  571. .name = "smsc9500",
  572. .vendor = 0x0424,
  573. .product = 0x9500,
  574. },
  575. {
  576. .name = "smsc9505",
  577. .vendor = 0x0424,
  578. .product = 0x9505,
  579. },
  580. {
  581. .name = "smsc9500a",
  582. .vendor = 0x0424,
  583. .product = 0x9e00,
  584. },
  585. {
  586. .name = "smsc9505a",
  587. .vendor = 0x0424,
  588. .product = 0x9e01,
  589. },
  590. {
  591. .name = "smsc9514",
  592. .vendor = 0x0424,
  593. .product = 0xec00,
  594. },
  595. {
  596. .name = "smsc9500-s",
  597. .vendor = 0x0424,
  598. .product = 0x9900,
  599. },
  600. {
  601. .name = "smsc9505-s",
  602. .vendor = 0x0424,
  603. .product = 0x9901,
  604. },
  605. {
  606. .name = "smsc9500a-s",
  607. .vendor = 0x0424,
  608. .product = 0x9902,
  609. },
  610. {
  611. .name = "smsc9505a-s",
  612. .vendor = 0x0424,
  613. .product = 0x9903,
  614. },
  615. {
  616. .name = "smsc9514-s",
  617. .vendor = 0x0424,
  618. .product = 0x9904,
  619. },
  620. {
  621. .name = "smsc9500a-h",
  622. .vendor = 0x0424,
  623. .product = 0x9905,
  624. },
  625. {
  626. .name = "smsc9505a-h",
  627. .vendor = 0x0424,
  628. .product = 0x9906,
  629. },
  630. {
  631. .name = "smsc9500-2",
  632. .vendor = 0x0424,
  633. .product = 0x9907,
  634. },
  635. {
  636. .name = "smsc9500a-2",
  637. .vendor = 0x0424,
  638. .product = 0x9908,
  639. },
  640. {
  641. .name = "smsc9514-2",
  642. .vendor = 0x0424,
  643. .product = 0x9909,
  644. },
  645. {
  646. .name = "smsc9530",
  647. .vendor = 0x0424,
  648. .product = 0x9530,
  649. },
  650. {
  651. .name = "smsc9730",
  652. .vendor = 0x0424,
  653. .product = 0x9730,
  654. },
  655. {
  656. .name = "smsc89530",
  657. .vendor = 0x0424,
  658. .product = 0x9e08,
  659. },
  660. };
  661. /** SMSC LAN95xx driver */
  662. struct usb_driver smsc95xx_driver __usb_driver = {
  663. .ids = smsc95xx_ids,
  664. .id_count = ( sizeof ( smsc95xx_ids ) / sizeof ( smsc95xx_ids[0] ) ),
  665. .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
  666. .score = USB_SCORE_NORMAL,
  667. .probe = smsc95xx_probe,
  668. .remove = smsc95xx_remove,
  669. };