iPXE
smsc95xx.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <string.h>
00027 #include <unistd.h>
00028 #include <errno.h>
00029 #include <byteswap.h>
00030 #include <ipxe/ethernet.h>
00031 #include <ipxe/usb.h>
00032 #include <ipxe/usbnet.h>
00033 #include <ipxe/profile.h>
00034 #include <ipxe/base16.h>
00035 #include <ipxe/smbios.h>
00036 #include "smsc95xx.h"
00037 
00038 /** @file
00039  *
00040  * SMSC LAN95xx USB Ethernet driver
00041  *
00042  */
00043 
00044 /** Bulk IN completion profiler */
00045 static struct profiler smsc95xx_in_profiler __profiler =
00046         { .name = "smsc95xx.in" };
00047 
00048 /** Bulk OUT profiler */
00049 static struct profiler smsc95xx_out_profiler __profiler =
00050         { .name = "smsc95xx.out" };
00051 
00052 /******************************************************************************
00053  *
00054  * MAC address
00055  *
00056  ******************************************************************************
00057  */
00058 
00059 /**
00060  * Construct MAC address for Honeywell VM3
00061  *
00062  * @v smscusb           SMSC USB device
00063  * @ret rc              Return status code
00064  */
00065 static int smsc95xx_vm3_fetch_mac ( struct smscusb_device *smscusb ) {
00066         struct net_device *netdev = smscusb->netdev;
00067         struct smbios_structure structure;
00068         struct smbios_system_information system;
00069         struct {
00070                 char manufacturer[ 10 /* "Honeywell" + NUL */ ];
00071                 char product[ 4 /* "VM3" + NUL */ ];
00072                 char mac[ base16_encoded_len ( ETH_ALEN ) + 1 /* NUL */ ];
00073         } strings;
00074         int len;
00075         int rc;
00076 
00077         /* Find system information */
00078         if ( ( rc = find_smbios_structure ( SMBIOS_TYPE_SYSTEM_INFORMATION, 0,
00079                                             &structure ) ) != 0 ) {
00080                 DBGC ( smscusb, "SMSC95XX %p could not find system "
00081                        "information: %s\n", smscusb, strerror ( rc ) );
00082                 return rc;
00083         }
00084 
00085         /* Read system information */
00086         if ( ( rc = read_smbios_structure ( &structure, &system,
00087                                             sizeof ( system ) ) ) != 0 ) {
00088                 DBGC ( smscusb, "SMSC95XX %p could not read system "
00089                        "information: %s\n", smscusb, strerror ( rc ) );
00090                 return rc;
00091         }
00092 
00093         /* NUL-terminate all strings to be fetched */
00094         memset ( &strings, 0, sizeof ( strings ) );
00095 
00096         /* Fetch system manufacturer name */
00097         len = read_smbios_string ( &structure, system.manufacturer,
00098                                    strings.manufacturer,
00099                                    ( sizeof ( strings.manufacturer ) - 1 ) );
00100         if ( len < 0 ) {
00101                 rc = len;
00102                 DBGC ( smscusb, "SMSC95XX %p could not read manufacturer "
00103                        "name: %s\n", smscusb, strerror ( rc ) );
00104                 return rc;
00105         }
00106 
00107         /* Fetch system product name */
00108         len = read_smbios_string ( &structure, system.product, strings.product,
00109                                    ( sizeof ( strings.product ) - 1 ) );
00110         if ( len < 0 ) {
00111                 rc = len;
00112                 DBGC ( smscusb, "SMSC95XX %p could not read product name: "
00113                        "%s\n", smscusb, strerror ( rc ) );
00114                 return rc;
00115         }
00116 
00117         /* Ignore non-VM3 devices */
00118         if ( ( strcmp ( strings.manufacturer, "Honeywell" ) != 0 ) ||
00119              ( strcmp ( strings.product, "VM3" ) != 0 ) )
00120                 return -ENOTTY;
00121 
00122         /* Find OEM strings */
00123         if ( ( rc = find_smbios_structure ( SMBIOS_TYPE_OEM_STRINGS, 0,
00124                                             &structure ) ) != 0 ) {
00125                 DBGC ( smscusb, "SMSC95XX %p could not find OEM strings: %s\n",
00126                        smscusb, strerror ( rc ) );
00127                 return rc;
00128         }
00129 
00130         /* Fetch MAC address */
00131         len = read_smbios_string ( &structure, SMSC95XX_VM3_OEM_STRING_MAC,
00132                                    strings.mac, ( sizeof ( strings.mac ) - 1 ));
00133         if ( len < 0 ) {
00134                 rc = len;
00135                 DBGC ( smscusb, "SMSC95XX %p could not read OEM string: %s\n",
00136                        smscusb, strerror ( rc ) );
00137                 return rc;
00138         }
00139 
00140         /* Sanity check */
00141         if ( len != ( ( int ) ( sizeof ( strings.mac ) - 1 ) ) ) {
00142                 DBGC ( smscusb, "SMSC95XX %p invalid MAC address \"%s\"\n",
00143                        smscusb, strings.mac );
00144                 return -EINVAL;
00145         }
00146 
00147         /* Decode MAC address */
00148         len = base16_decode ( strings.mac, netdev->hw_addr, ETH_ALEN );
00149         if ( len < 0 ) {
00150                 rc = len;
00151                 DBGC ( smscusb, "SMSC95XX %p invalid MAC address \"%s\"\n",
00152                        smscusb, strings.mac );
00153                 return rc;
00154         }
00155 
00156         DBGC ( smscusb, "SMSC95XX %p using VM3 MAC %s\n",
00157                smscusb, eth_ntoa ( netdev->hw_addr ) );
00158         return 0;
00159 }
00160 
00161 /**
00162  * Fetch MAC address
00163  *
00164  * @v smscusb           SMSC USB device
00165  * @ret rc              Return status code
00166  */
00167 static int smsc95xx_fetch_mac ( struct smscusb_device *smscusb ) {
00168         struct net_device *netdev = smscusb->netdev;
00169         int rc;
00170 
00171         /* Read MAC address from EEPROM, if present */
00172         if ( ( rc = smscusb_eeprom_fetch_mac ( smscusb,
00173                                                SMSC95XX_E2P_BASE ) ) == 0 )
00174                 return 0;
00175 
00176         /* Read MAC address from device tree, if present */
00177         if ( ( rc = smscusb_fdt_fetch_mac ( smscusb ) ) == 0 )
00178                 return 0;
00179 
00180         /* Construct MAC address for Honeywell VM3, if applicable */
00181         if ( ( rc = smsc95xx_vm3_fetch_mac ( smscusb ) ) == 0 )
00182                 return 0;
00183 
00184         /* Otherwise, generate a random MAC address */
00185         eth_random_addr ( netdev->hw_addr );
00186         DBGC ( smscusb, "SMSC95XX %p using random MAC %s\n",
00187                smscusb, eth_ntoa ( netdev->hw_addr ) );
00188         return 0;
00189 }
00190 
00191 /******************************************************************************
00192  *
00193  * Statistics (for debugging)
00194  *
00195  ******************************************************************************
00196  */
00197 
00198 /**
00199  * Dump statistics (for debugging)
00200  *
00201  * @v smscusb           SMSC USB device
00202  * @ret rc              Return status code
00203  */
00204 static int smsc95xx_dump_statistics ( struct smscusb_device *smscusb ) {
00205         struct smsc95xx_rx_statistics rx;
00206         struct smsc95xx_tx_statistics tx;
00207         int rc;
00208 
00209         /* Do nothing unless debugging is enabled */
00210         if ( ! DBG_LOG )
00211                 return 0;
00212 
00213         /* Get RX statistics */
00214         if ( ( rc = smscusb_get_statistics ( smscusb, SMSC95XX_RX_STATISTICS,
00215                                              &rx, sizeof ( rx ) ) ) != 0 ) {
00216                 DBGC ( smscusb, "SMSC95XX %p could not get RX statistics: "
00217                        "%s\n", smscusb, strerror ( rc ) );
00218                 return rc;
00219         }
00220 
00221         /* Get TX statistics */
00222         if ( ( rc = smscusb_get_statistics ( smscusb, SMSC95XX_TX_STATISTICS,
00223                                              &tx, sizeof ( tx ) ) ) != 0 ) {
00224                 DBGC ( smscusb, "SMSC95XX %p could not get TX statistics: "
00225                        "%s\n", smscusb, strerror ( rc ) );
00226                 return rc;
00227         }
00228 
00229         /* Dump statistics */
00230         DBGC ( smscusb, "SMSC95XX %p RX good %d bad %d crc %d und %d aln %d "
00231                "ovr %d lat %d drp %d\n", smscusb, le32_to_cpu ( rx.good ),
00232                le32_to_cpu ( rx.bad ), le32_to_cpu ( rx.crc ),
00233                le32_to_cpu ( rx.undersize ), le32_to_cpu ( rx.alignment ),
00234                le32_to_cpu ( rx.oversize ), le32_to_cpu ( rx.late ),
00235                le32_to_cpu ( rx.dropped ) );
00236         DBGC ( smscusb, "SMSC95XX %p TX good %d bad %d pau %d sgl %d mul %d "
00237                "exc %d lat %d und %d def %d car %d\n", smscusb,
00238                le32_to_cpu ( tx.good ), le32_to_cpu ( tx.bad ),
00239                le32_to_cpu ( tx.pause ), le32_to_cpu ( tx.single ),
00240                le32_to_cpu ( tx.multiple ), le32_to_cpu ( tx.excessive ),
00241                le32_to_cpu ( tx.late ), le32_to_cpu ( tx.underrun ),
00242                le32_to_cpu ( tx.deferred ), le32_to_cpu ( tx.carrier ) );
00243 
00244         return 0;
00245 }
00246 
00247 /******************************************************************************
00248  *
00249  * Device reset
00250  *
00251  ******************************************************************************
00252  */
00253 
00254 /**
00255  * Reset device
00256  *
00257  * @v smscusb           SMSC USB device
00258  * @ret rc              Return status code
00259  */
00260 static int smsc95xx_reset ( struct smscusb_device *smscusb ) {
00261         uint32_t hw_cfg;
00262         uint32_t led_gpio_cfg;
00263         int rc;
00264 
00265         /* Reset device */
00266         if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_HW_CFG,
00267                                      SMSC95XX_HW_CFG_LRST ) ) != 0 )
00268                 return rc;
00269 
00270         /* Wait for reset to complete */
00271         udelay ( SMSC95XX_RESET_DELAY_US );
00272 
00273         /* Check that reset has completed */
00274         if ( ( rc = smscusb_readl ( smscusb, SMSC95XX_HW_CFG, &hw_cfg ) ) != 0 )
00275                 return rc;
00276         if ( hw_cfg & SMSC95XX_HW_CFG_LRST ) {
00277                 DBGC ( smscusb, "SMSC95XX %p failed to reset\n", smscusb );
00278                 return -ETIMEDOUT;
00279         }
00280 
00281         /* Configure LEDs */
00282         led_gpio_cfg = ( SMSC95XX_LED_GPIO_CFG_GPCTL2_NSPD_LED |
00283                          SMSC95XX_LED_GPIO_CFG_GPCTL1_NLNKA_LED |
00284                          SMSC95XX_LED_GPIO_CFG_GPCTL0_NFDX_LED );
00285         if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_LED_GPIO_CFG,
00286                                      led_gpio_cfg ) ) != 0 ) {
00287                 DBGC ( smscusb, "SMSC95XX %p could not configure LEDs: %s\n",
00288                        smscusb, strerror ( rc ) );
00289                 /* Ignore error and continue */
00290         }
00291 
00292         return 0;
00293 }
00294 
00295 /******************************************************************************
00296  *
00297  * Endpoint operations
00298  *
00299  ******************************************************************************
00300  */
00301 
00302 /**
00303  * Complete bulk IN transfer
00304  *
00305  * @v ep                USB endpoint
00306  * @v iobuf             I/O buffer
00307  * @v rc                Completion status code
00308  */
00309 static void smsc95xx_in_complete ( struct usb_endpoint *ep,
00310                                    struct io_buffer *iobuf, int rc ) {
00311         struct smscusb_device *smscusb =
00312                 container_of ( ep, struct smscusb_device, usbnet.in );
00313         struct net_device *netdev = smscusb->netdev;
00314         struct smsc95xx_rx_header *header;
00315 
00316         /* Profile completions */
00317         profile_start ( &smsc95xx_in_profiler );
00318 
00319         /* Ignore packets cancelled when the endpoint closes */
00320         if ( ! ep->open ) {
00321                 free_iob ( iobuf );
00322                 return;
00323         }
00324 
00325         /* Record USB errors against the network device */
00326         if ( rc != 0 ) {
00327                 DBGC ( smscusb, "SMSC95XX %p bulk IN failed: %s\n",
00328                        smscusb, strerror ( rc ) );
00329                 goto err;
00330         }
00331 
00332         /* Sanity check */
00333         if ( iob_len ( iobuf ) < ( sizeof ( *header ) + 4 /* CRC */ ) ) {
00334                 DBGC ( smscusb, "SMSC95XX %p underlength bulk IN\n",
00335                        smscusb );
00336                 DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
00337                 rc = -EINVAL;
00338                 goto err;
00339         }
00340 
00341         /* Strip header and CRC */
00342         header = iobuf->data;
00343         iob_pull ( iobuf, sizeof ( *header ) );
00344         iob_unput ( iobuf, 4 /* CRC */ );
00345 
00346         /* Check for errors */
00347         if ( header->command & cpu_to_le32 ( SMSC95XX_RX_RUNT |
00348                                              SMSC95XX_RX_LATE |
00349                                              SMSC95XX_RX_CRC ) ) {
00350                 DBGC ( smscusb, "SMSC95XX %p receive error (%08x):\n",
00351                        smscusb, le32_to_cpu ( header->command ) );
00352                 DBGC_HDA ( smscusb, 0, iobuf->data, iob_len ( iobuf ) );
00353                 rc = -EIO;
00354                 goto err;
00355         }
00356 
00357         /* Hand off to network stack */
00358         netdev_rx ( netdev, iob_disown ( iobuf ) );
00359 
00360         profile_stop ( &smsc95xx_in_profiler );
00361         return;
00362 
00363  err:
00364         /* Hand off to network stack */
00365         netdev_rx_err ( netdev, iob_disown ( iobuf ), rc );
00366 }
00367 
00368 /** Bulk IN endpoint operations */
00369 static struct usb_endpoint_driver_operations smsc95xx_in_operations = {
00370         .complete = smsc95xx_in_complete,
00371 };
00372 
00373 /**
00374  * Transmit packet
00375  *
00376  * @v smscusb           SMSC USB device
00377  * @v iobuf             I/O buffer
00378  * @ret rc              Return status code
00379  */
00380 static int smsc95xx_out_transmit ( struct smscusb_device *smscusb,
00381                                    struct io_buffer *iobuf ) {
00382         struct smsc95xx_tx_header *header;
00383         size_t len = iob_len ( iobuf );
00384         int rc;
00385 
00386         /* Profile transmissions */
00387         profile_start ( &smsc95xx_out_profiler );
00388 
00389         /* Prepend header */
00390         if ( ( rc = iob_ensure_headroom ( iobuf, sizeof ( *header ) ) ) != 0 )
00391                 return rc;
00392         header = iob_push ( iobuf, sizeof ( *header ) );
00393         header->command = cpu_to_le32 ( SMSC95XX_TX_FIRST | SMSC95XX_TX_LAST |
00394                                         SMSC95XX_TX_LEN ( len ) );
00395         header->len = cpu_to_le32 ( len );
00396 
00397         /* Enqueue I/O buffer */
00398         if ( ( rc = usb_stream ( &smscusb->usbnet.out, iobuf, 0 ) ) != 0 )
00399                 return rc;
00400 
00401         profile_stop ( &smsc95xx_out_profiler );
00402         return 0;
00403 }
00404 
00405 /******************************************************************************
00406  *
00407  * Network device interface
00408  *
00409  ******************************************************************************
00410  */
00411 
00412 /**
00413  * Open network device
00414  *
00415  * @v netdev            Network device
00416  * @ret rc              Return status code
00417  */
00418 static int smsc95xx_open ( struct net_device *netdev ) {
00419         struct smscusb_device *smscusb = netdev->priv;
00420         int rc;
00421 
00422         /* Clear stored interrupt status */
00423         smscusb->int_sts = 0;
00424 
00425         /* Configure bulk IN empty response */
00426         if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_HW_CFG,
00427                                      SMSC95XX_HW_CFG_BIR ) ) != 0 )
00428                 goto err_hw_cfg;
00429 
00430         /* Open USB network device */
00431         if ( ( rc = usbnet_open ( &smscusb->usbnet ) ) != 0 ) {
00432                 DBGC ( smscusb, "SMSC95XX %p could not open: %s\n",
00433                        smscusb, strerror ( rc ) );
00434                 goto err_open;
00435         }
00436 
00437         /* Configure interrupt endpoint */
00438         if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_INT_EP_CTL,
00439                                      ( SMSC95XX_INT_EP_CTL_RXDF_EN |
00440                                        SMSC95XX_INT_EP_CTL_PHY_EN ) ) ) != 0 )
00441                 goto err_int_ep_ctl;
00442 
00443         /* Configure bulk IN delay */
00444         if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_BULK_IN_DLY,
00445                                      SMSC95XX_BULK_IN_DLY_SET ( 0 ) ) ) != 0 )
00446                 goto err_bulk_in_dly;
00447 
00448         /* Configure MAC */
00449         if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_MAC_CR,
00450                                      ( SMSC95XX_MAC_CR_RXALL |
00451                                        SMSC95XX_MAC_CR_FDPX |
00452                                        SMSC95XX_MAC_CR_MCPAS |
00453                                        SMSC95XX_MAC_CR_PRMS |
00454                                        SMSC95XX_MAC_CR_PASSBAD |
00455                                        SMSC95XX_MAC_CR_TXEN |
00456                                        SMSC95XX_MAC_CR_RXEN ) ) ) != 0 )
00457                 goto err_mac_cr;
00458 
00459         /* Configure transmit datapath */
00460         if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_TX_CFG,
00461                                      SMSC95XX_TX_CFG_ON ) ) != 0 )
00462                 goto err_tx_cfg;
00463 
00464         /* Set MAC address */
00465         if ( ( rc = smscusb_set_address ( smscusb, SMSC95XX_ADDR_BASE ) ) != 0 )
00466                 goto err_set_address;
00467 
00468         /* Enable PHY interrupts and update link status */
00469         if ( ( rc = smscusb_mii_open ( smscusb, SMSC95XX_MII_PHY_INTR_MASK,
00470                                        ( SMSC95XX_PHY_INTR_ANEG_DONE |
00471                                          SMSC95XX_PHY_INTR_LINK_DOWN ) ) ) != 0)
00472                 goto err_mii_open;
00473 
00474         return 0;
00475 
00476  err_mii_open:
00477  err_set_address:
00478  err_tx_cfg:
00479  err_mac_cr:
00480  err_bulk_in_dly:
00481  err_int_ep_ctl:
00482         usbnet_close ( &smscusb->usbnet );
00483  err_open:
00484  err_hw_cfg:
00485         smsc95xx_reset ( smscusb );
00486         return rc;
00487 }
00488 
00489 /**
00490  * Close network device
00491  *
00492  * @v netdev            Network device
00493  */
00494 static void smsc95xx_close ( struct net_device *netdev ) {
00495         struct smscusb_device *smscusb = netdev->priv;
00496 
00497         /* Close USB network device */
00498         usbnet_close ( &smscusb->usbnet );
00499 
00500         /* Dump statistics (for debugging) */
00501         smsc95xx_dump_statistics ( smscusb );
00502 
00503         /* Reset device */
00504         smsc95xx_reset ( smscusb );
00505 }
00506 
00507 /**
00508  * Transmit packet
00509  *
00510  * @v netdev            Network device
00511  * @v iobuf             I/O buffer
00512  * @ret rc              Return status code
00513  */
00514 static int smsc95xx_transmit ( struct net_device *netdev,
00515                                struct io_buffer *iobuf ) {
00516         struct smscusb_device *smscusb = netdev->priv;
00517         int rc;
00518 
00519         /* Transmit packet */
00520         if ( ( rc = smsc95xx_out_transmit ( smscusb, iobuf ) ) != 0 )
00521                 return rc;
00522 
00523         return 0;
00524 }
00525 
00526 /**
00527  * Poll for completed and received packets
00528  *
00529  * @v netdev            Network device
00530  */
00531 static void smsc95xx_poll ( struct net_device *netdev ) {
00532         struct smscusb_device *smscusb = netdev->priv;
00533         uint32_t int_sts;
00534         int rc;
00535 
00536         /* Poll USB bus */
00537         usb_poll ( smscusb->bus );
00538 
00539         /* Refill endpoints */
00540         if ( ( rc = usbnet_refill ( &smscusb->usbnet ) ) != 0 )
00541                 netdev_rx_err ( netdev, NULL, rc );
00542 
00543         /* Do nothing more unless there are interrupts to handle */
00544         int_sts = smscusb->int_sts;
00545         if ( ! int_sts )
00546                 return;
00547 
00548         /* Check link status if applicable */
00549         if ( int_sts & SMSC95XX_INT_STS_PHY_INT ) {
00550                 smscusb_mii_check_link ( smscusb );
00551                 int_sts &= ~SMSC95XX_INT_STS_PHY_INT;
00552         }
00553 
00554         /* Record RX FIFO overflow if applicable */
00555         if ( int_sts & SMSC95XX_INT_STS_RXDF_INT ) {
00556                 DBGC2 ( smscusb, "SMSC95XX %p RX FIFO overflowed\n",
00557                         smscusb );
00558                 netdev_rx_err ( netdev, NULL, -ENOBUFS );
00559                 int_sts &= ~SMSC95XX_INT_STS_RXDF_INT;
00560         }
00561 
00562         /* Check for unexpected interrupts */
00563         if ( int_sts ) {
00564                 DBGC ( smscusb, "SMSC95XX %p unexpected interrupt %#08x\n",
00565                        smscusb, int_sts );
00566                 netdev_rx_err ( netdev, NULL, -ENOTTY );
00567         }
00568 
00569         /* Clear interrupts */
00570         if ( ( rc = smscusb_writel ( smscusb, SMSC95XX_INT_STS,
00571                                      smscusb->int_sts ) ) != 0 )
00572                 netdev_rx_err ( netdev, NULL, rc );
00573         smscusb->int_sts = 0;
00574 }
00575 
00576 /** SMSC95xx network device operations */
00577 static struct net_device_operations smsc95xx_operations = {
00578         .open           = smsc95xx_open,
00579         .close          = smsc95xx_close,
00580         .transmit       = smsc95xx_transmit,
00581         .poll           = smsc95xx_poll,
00582 };
00583 
00584 /******************************************************************************
00585  *
00586  * USB interface
00587  *
00588  ******************************************************************************
00589  */
00590 
00591 /**
00592  * Probe device
00593  *
00594  * @v func              USB function
00595  * @v config            Configuration descriptor
00596  * @ret rc              Return status code
00597  */
00598 static int smsc95xx_probe ( struct usb_function *func,
00599                             struct usb_configuration_descriptor *config ) {
00600         struct net_device *netdev;
00601         struct smscusb_device *smscusb;
00602         int rc;
00603 
00604         /* Allocate and initialise structure */
00605         netdev = alloc_etherdev ( sizeof ( *smscusb ) );
00606         if ( ! netdev ) {
00607                 rc = -ENOMEM;
00608                 goto err_alloc;
00609         }
00610         netdev_init ( netdev, &smsc95xx_operations );
00611         netdev->dev = &func->dev;
00612         smscusb = netdev->priv;
00613         memset ( smscusb, 0, sizeof ( *smscusb ) );
00614         smscusb_init ( smscusb, netdev, func, &smsc95xx_in_operations );
00615         smscusb_mii_init ( smscusb, SMSC95XX_MII_BASE,
00616                            SMSC95XX_MII_PHY_INTR_SOURCE );
00617         usb_refill_init ( &smscusb->usbnet.in,
00618                           ( sizeof ( struct smsc95xx_tx_header ) -
00619                             sizeof ( struct smsc95xx_rx_header ) ),
00620                           SMSC95XX_IN_MTU, SMSC95XX_IN_MAX_FILL );
00621         DBGC ( smscusb, "SMSC95XX %p on %s\n", smscusb, func->name );
00622 
00623         /* Describe USB network device */
00624         if ( ( rc = usbnet_describe ( &smscusb->usbnet, config ) ) != 0 ) {
00625                 DBGC ( smscusb, "SMSC95XX %p could not describe: %s\n",
00626                        smscusb, strerror ( rc ) );
00627                 goto err_describe;
00628         }
00629 
00630         /* Reset device */
00631         if ( ( rc = smsc95xx_reset ( smscusb ) ) != 0 )
00632                 goto err_reset;
00633 
00634         /* Read MAC address */
00635         if ( ( rc = smsc95xx_fetch_mac ( smscusb ) ) != 0 )
00636                 goto err_fetch_mac;
00637 
00638         /* Register network device */
00639         if ( ( rc = register_netdev ( netdev ) ) != 0 )
00640                 goto err_register;
00641 
00642         usb_func_set_drvdata ( func, netdev );
00643         return 0;
00644 
00645         unregister_netdev ( netdev );
00646  err_register:
00647  err_fetch_mac:
00648  err_reset:
00649  err_describe:
00650         netdev_nullify ( netdev );
00651         netdev_put ( netdev );
00652  err_alloc:
00653         return rc;
00654 }
00655 
00656 /**
00657  * Remove device
00658  *
00659  * @v func              USB function
00660  */
00661 static void smsc95xx_remove ( struct usb_function *func ) {
00662         struct net_device *netdev = usb_func_get_drvdata ( func );
00663 
00664         unregister_netdev ( netdev );
00665         netdev_nullify ( netdev );
00666         netdev_put ( netdev );
00667 }
00668 
00669 /** SMSC95xx device IDs */
00670 static struct usb_device_id smsc95xx_ids[] = {
00671         {
00672                 .name = "smsc9500",
00673                 .vendor = 0x0424,
00674                 .product = 0x9500,
00675         },
00676         {
00677                 .name = "smsc9505",
00678                 .vendor = 0x0424,
00679                 .product = 0x9505,
00680         },
00681         {
00682                 .name = "smsc9500a",
00683                 .vendor = 0x0424,
00684                 .product = 0x9e00,
00685         },
00686         {
00687                 .name = "smsc9505a",
00688                 .vendor = 0x0424,
00689                 .product = 0x9e01,
00690         },
00691         {
00692                 .name = "smsc9514",
00693                 .vendor = 0x0424,
00694                 .product = 0xec00,
00695         },
00696         {
00697                 .name = "smsc9500-s",
00698                 .vendor = 0x0424,
00699                 .product = 0x9900,
00700         },
00701         {
00702                 .name = "smsc9505-s",
00703                 .vendor = 0x0424,
00704                 .product = 0x9901,
00705         },
00706         {
00707                 .name = "smsc9500a-s",
00708                 .vendor = 0x0424,
00709                 .product = 0x9902,
00710         },
00711         {
00712                 .name = "smsc9505a-s",
00713                 .vendor = 0x0424,
00714                 .product = 0x9903,
00715         },
00716         {
00717                 .name = "smsc9514-s",
00718                 .vendor = 0x0424,
00719                 .product = 0x9904,
00720         },
00721         {
00722                 .name = "smsc9500a-h",
00723                 .vendor = 0x0424,
00724                 .product = 0x9905,
00725         },
00726         {
00727                 .name = "smsc9505a-h",
00728                 .vendor = 0x0424,
00729                 .product = 0x9906,
00730         },
00731         {
00732                 .name = "smsc9500-2",
00733                 .vendor = 0x0424,
00734                 .product = 0x9907,
00735         },
00736         {
00737                 .name = "smsc9500a-2",
00738                 .vendor = 0x0424,
00739                 .product = 0x9908,
00740         },
00741         {
00742                 .name = "smsc9514-2",
00743                 .vendor = 0x0424,
00744                 .product = 0x9909,
00745         },
00746         {
00747                 .name = "smsc9530",
00748                 .vendor = 0x0424,
00749                 .product = 0x9530,
00750         },
00751         {
00752                 .name = "smsc9730",
00753                 .vendor = 0x0424,
00754                 .product = 0x9730,
00755         },
00756         {
00757                 .name = "smsc89530",
00758                 .vendor = 0x0424,
00759                 .product = 0x9e08,
00760         },
00761 };
00762 
00763 /** SMSC LAN95xx driver */
00764 struct usb_driver smsc95xx_driver __usb_driver = {
00765         .ids = smsc95xx_ids,
00766         .id_count = ( sizeof ( smsc95xx_ids ) / sizeof ( smsc95xx_ids[0] ) ),
00767         .class = USB_CLASS_ID ( 0xff, 0x00, 0xff ),
00768         .score = USB_SCORE_NORMAL,
00769         .probe = smsc95xx_probe,
00770         .remove = smsc95xx_remove,
00771 };