Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
/*	$NetBSD: if_smsc.c,v 1.93 2022/08/20 14:08:59 riastradh Exp $	*/

/*	$OpenBSD: if_smsc.c,v 1.4 2012/09/27 12:38:11 jsg Exp $	*/
/*	$FreeBSD: src/sys/dev/usb/net/if_smsc.c,v 1.1 2012/08/15 04:03:55 gonzo Exp $ */
/*-
 * Copyright (c) 2012
 *	Ben Gray <bgray@freebsd.org>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * SMSC LAN9xxx devices (http://www.smsc.com/)
 *
 * The LAN9500 & LAN9500A devices are stand-alone USB to Ethernet chips that
 * support USB 2.0 and 10/100 Mbps Ethernet.
 *
 * The LAN951x devices are an integrated USB hub and USB to Ethernet adapter.
 * The driver only covers the Ethernet part, the standard USB hub driver
 * supports the hub part.
 *
 * This driver is closely modelled on the Linux driver written and copyrighted
 * by SMSC.
 *
 * H/W TCP & UDP Checksum Offloading
 * ---------------------------------
 * The chip supports both tx and rx offloading of UDP & TCP checksums, this
 * feature can be dynamically enabled/disabled.
 *
 * RX checksuming is performed across bytes after the IPv4 header to the end of
 * the Ethernet frame, this means if the frame is padded with non-zero values
 * the H/W checksum will be incorrect, however the rx code compensates for this.
 *
 * TX checksuming is more complicated, the device requires a special header to
 * be prefixed onto the start of the frame which indicates the start and end
 * positions of the UDP or TCP frame.  This requires the driver to manually
 * go through the packet data and decode the headers prior to sending.
 * On Linux they generally provide cues to the location of the csum and the
 * area to calculate it over, on FreeBSD we seem to have to do it all ourselves,
 * hence this is not as optimal and therefore h/w TX checksum is currently not
 * implemented.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_smsc.c,v 1.93 2022/08/20 14:08:59 riastradh Exp $");

#ifdef _KERNEL_OPT
#include "opt_usb.h"
#endif

#include <sys/param.h>

#include <dev/usb/usbnet.h>
#include <dev/usb/usbhist.h>

#include <dev/usb/if_smscreg.h>

#include "ioconf.h"

struct smsc_softc {
	struct usbnet		smsc_un;

	/*
	 * The following stores the settings in the mac control (MAC_CSR)
	 * register
	 */
	uint32_t		sc_mac_csr;
	uint32_t		sc_rev_id;

	uint32_t		sc_coe_ctrl;
};

#define SMSC_MIN_BUFSZ		2048
#define SMSC_MAX_BUFSZ		18944

/*
 * Various supported device vendors/products.
 */
static const struct usb_devno smsc_devs[] = {
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN89530 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9530 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_LAN9730 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_ALT },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_HAL },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500A_SAL10 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_ALT },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9500_SAL10 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_HAL },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505A_SAL10 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9505_SAL10 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14 },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_ALT },
	{ USB_VENDOR_SMSC,	USB_PRODUCT_SMSC_SMSC9512_14_SAL10 }
};

#ifdef USB_DEBUG
#ifndef USMSC_DEBUG
#define usmscdebug 0
#else
static int usmscdebug = 1;

SYSCTL_SETUP(sysctl_hw_smsc_setup, "sysctl hw.usmsc setup")
{
	int err;
	const struct sysctlnode *rnode;
	const struct sysctlnode *cnode;

	err = sysctl_createv(clog, 0, NULL, &rnode,
	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usmsc",
	    SYSCTL_DESCR("usmsc global controls"),
	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);

	if (err)
		goto fail;

	/* control debugging printfs */
	err = sysctl_createv(clog, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
	    "debug", SYSCTL_DESCR("Enable debugging output"),
	    NULL, 0, &usmscdebug, sizeof(usmscdebug), CTL_CREATE, CTL_EOL);
	if (err)
		goto fail;

	return;
fail:
	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
}

#endif /* SMSC_DEBUG */
#endif /* USB_DEBUG */

#define DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usmscdebug,FMT,A,B,C,D)
#define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usmscdebug,N,FMT,A,B,C,D)
#define USMSCHIST_FUNC()	USBHIST_FUNC()
#define USMSCHIST_CALLED()	USBHIST_CALLED(usmscdebug)

#define smsc_warn_printf(un, fmt, args...) \
	printf("%s: warning: " fmt, device_xname((un)->un_dev), ##args)

#define smsc_err_printf(un, fmt, args...) \
	printf("%s: error: " fmt, device_xname((un)->un_dev), ##args)

/* Function declarations */
static int	 smsc_match(device_t, cfdata_t, void *);
static void	 smsc_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(usmsc, sizeof(struct smsc_softc),
    smsc_match, smsc_attach, usbnet_detach, usbnet_activate);

static int	 smsc_chip_init(struct usbnet *);
static int	 smsc_setmacaddress(struct usbnet *, const uint8_t *);

static int	 smsc_uno_init(struct ifnet *);
static void	 smsc_uno_stop(struct ifnet *, int);

static void	 smsc_reset(struct smsc_softc *);

static void	 smsc_uno_miibus_statchg(struct ifnet *);
static int	 smsc_readreg(struct usbnet *, uint32_t, uint32_t *);
static int	 smsc_writereg(struct usbnet *, uint32_t, uint32_t);
static int	 smsc_wait_for_bits(struct usbnet *, uint32_t, uint32_t);
static int	 smsc_uno_miibus_readreg(struct usbnet *, int, int, uint16_t *);
static int	 smsc_uno_miibus_writereg(struct usbnet *, int, int, uint16_t);

static int	 smsc_uno_ioctl(struct ifnet *, u_long, void *);
static void	 smsc_uno_mcast(struct ifnet *);
static unsigned	 smsc_uno_tx_prepare(struct usbnet *, struct mbuf *,
		     struct usbnet_chain *);
static void	 smsc_uno_rx_loop(struct usbnet *, struct usbnet_chain *,
		     uint32_t);

static const struct usbnet_ops smsc_ops = {
	.uno_stop = smsc_uno_stop,
	.uno_ioctl = smsc_uno_ioctl,
	.uno_mcast = smsc_uno_mcast,
	.uno_read_reg = smsc_uno_miibus_readreg,
	.uno_write_reg = smsc_uno_miibus_writereg,
	.uno_statchg = smsc_uno_miibus_statchg,
	.uno_tx_prepare = smsc_uno_tx_prepare,
	.uno_rx_loop = smsc_uno_rx_loop,
	.uno_init = smsc_uno_init,
};

static int
smsc_readreg(struct usbnet *un, uint32_t off, uint32_t *data)
{
	usb_device_request_t req;
	uint32_t buf;
	usbd_status err;

	if (usbnet_isdying(un))
		return 0;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = SMSC_UR_READ_REG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, off);
	USETW(req.wLength, 4);

	err = usbd_do_request(un->un_udev, &req, &buf);
	if (err != 0)
		smsc_warn_printf(un, "Failed to read register 0x%0x\n", off);

	*data = le32toh(buf);

	return err;
}

static int
smsc_writereg(struct usbnet *un, uint32_t off, uint32_t data)
{
	usb_device_request_t req;
	uint32_t buf;
	usbd_status err;

	if (usbnet_isdying(un))
		return 0;

	buf = htole32(data);

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = SMSC_UR_WRITE_REG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, off);
	USETW(req.wLength, 4);

	err = usbd_do_request(un->un_udev, &req, &buf);
	if (err != 0)
		smsc_warn_printf(un, "Failed to write register 0x%0x\n", off);

	return err;
}

static int
smsc_wait_for_bits(struct usbnet *un, uint32_t reg, uint32_t bits)
{
	uint32_t val;
	int err, i;

	for (i = 0; i < 100; i++) {
		if (usbnet_isdying(un))
			return ENXIO;
		if ((err = smsc_readreg(un, reg, &val)) != 0)
			return err;
		if (!(val & bits))
			return 0;
		DELAY(5);
	}

	return 1;
}

static int
smsc_uno_miibus_readreg(struct usbnet *un, int phy, int reg, uint16_t *val)
{
	uint32_t addr;
	uint32_t data = 0;

	if (un->un_phyno != phy) {
		*val = 0;
		return EINVAL;
	}

	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
		smsc_warn_printf(un, "MII is busy\n");
		*val = 0;
		return ETIMEDOUT;
	}

	addr = (phy << 11) | (reg << 6) | SMSC_MII_READ;
	smsc_writereg(un, SMSC_MII_ADDR, addr);

	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
		smsc_warn_printf(un, "MII read timeout\n");
		*val = 0;
		return ETIMEDOUT;
	}

	smsc_readreg(un, SMSC_MII_DATA, &data);

	*val = data & 0xffff;
	return 0;
}

static int
smsc_uno_miibus_writereg(struct usbnet *un, int phy, int reg, uint16_t val)
{
	uint32_t addr;

	if (un->un_phyno != phy)
		return EINVAL;

	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
		smsc_warn_printf(un, "MII is busy\n");
		return ETIMEDOUT;
	}

	smsc_writereg(un, SMSC_MII_DATA, val);

	addr = (phy << 11) | (reg << 6) | SMSC_MII_WRITE;
	smsc_writereg(un, SMSC_MII_ADDR, addr);

	if (smsc_wait_for_bits(un, SMSC_MII_ADDR, SMSC_MII_BUSY) != 0) {
		smsc_warn_printf(un, "MII write timeout\n");
		return ETIMEDOUT;
	}

	return 0;
}

static void
smsc_uno_miibus_statchg(struct ifnet *ifp)
{
	USMSCHIST_FUNC(); USMSCHIST_CALLED();
	struct usbnet * const un = ifp->if_softc;

	if (usbnet_isdying(un))
		return;

	struct smsc_softc * const sc = usbnet_softc(un);
	struct mii_data * const mii = usbnet_mii(un);
	uint32_t flow;
	uint32_t afc_cfg;

	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
	    (IFM_ACTIVE | IFM_AVALID)) {
		switch (IFM_SUBTYPE(mii->mii_media_active)) {
			case IFM_10_T:
			case IFM_100_TX:
				usbnet_set_link(un, true);
				break;
			case IFM_1000_T:
				/* Gigabit ethernet not supported by chipset */
				break;
			default:
				break;
		}
	}

	/* Lost link, do nothing. */
	if (!usbnet_havelink(un))
		return;

	int err = smsc_readreg(un, SMSC_AFC_CFG, &afc_cfg);
	if (err) {
		smsc_warn_printf(un, "failed to read initial AFC_CFG, "
		    "error %d\n", err);
		return;
	}

	/* Enable/disable full duplex operation and TX/RX pause */
	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
		DPRINTF("full duplex operation", 0, 0, 0, 0);
		sc->sc_mac_csr &= ~SMSC_MAC_CSR_RCVOWN;
		sc->sc_mac_csr |= SMSC_MAC_CSR_FDPX;

		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
			flow = 0xffff0002;
		else
			flow = 0;

		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
			afc_cfg |= 0xf;
		else
			afc_cfg &= ~0xf;
	} else {
		DPRINTF("half duplex operation", 0, 0, 0, 0);
		sc->sc_mac_csr &= ~SMSC_MAC_CSR_FDPX;
		sc->sc_mac_csr |= SMSC_MAC_CSR_RCVOWN;

		flow = 0;
		afc_cfg |= 0xf;
	}

	err = smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
	err += smsc_writereg(un, SMSC_FLOW, flow);
	err += smsc_writereg(un, SMSC_AFC_CFG, afc_cfg);

	if (err)
		smsc_warn_printf(un, "media change failed, error %d\n", err);
}

static inline uint32_t
smsc_hash(uint8_t addr[ETHER_ADDR_LEN])
{

	return (ether_crc32_be(addr, ETHER_ADDR_LEN) >> 26) & 0x3f;
}

static void
smsc_uno_mcast(struct ifnet *ifp)
{
	USMSCHIST_FUNC(); USMSCHIST_CALLED();
	struct usbnet * const un = ifp->if_softc;
	struct smsc_softc * const sc = usbnet_softc(un);
	struct ethercom *ec = usbnet_ec(un);
	struct ether_multi *enm;
	struct ether_multistep step;
	uint32_t hashtbl[2] = { 0, 0 };
	uint32_t hash;

	if (usbnet_isdying(un))
		return;

	if (usbnet_ispromisc(un)) {
		ETHER_LOCK(ec);
allmulti:
		ec->ec_flags |= ETHER_F_ALLMULTI;
		ETHER_UNLOCK(ec);
		DPRINTF("receive all multicast enabled", 0, 0, 0, 0);
		sc->sc_mac_csr |= SMSC_MAC_CSR_MCPAS;
		sc->sc_mac_csr &= ~SMSC_MAC_CSR_HPFILT;
		smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
		return;
	} else {
		sc->sc_mac_csr |= SMSC_MAC_CSR_HPFILT;
		sc->sc_mac_csr &= ~(SMSC_MAC_CSR_PRMS | SMSC_MAC_CSR_MCPAS);
	}

	ETHER_LOCK(ec);
	ETHER_FIRST_MULTI(step, ec, enm);
	while (enm != NULL) {
		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
			goto allmulti;
		}

		hash = smsc_hash(enm->enm_addrlo);
		hashtbl[hash >> 5] |= 1 << (hash & 0x1F);
		ETHER_NEXT_MULTI(step, enm);
	}
	ec->ec_flags &= ~ETHER_F_ALLMULTI;
	ETHER_UNLOCK(ec);

	/* Debug */
	if (sc->sc_mac_csr & SMSC_MAC_CSR_HPFILT) {
		DPRINTF("receive select group of macs", 0, 0, 0, 0);
	} else {
		DPRINTF("receive own packets only", 0, 0, 0, 0);
	}

	/* Write the hash table and mac control registers */

	//XXX should we be doing this?
	smsc_writereg(un, SMSC_HASHH, hashtbl[1]);
	smsc_writereg(un, SMSC_HASHL, hashtbl[0]);
	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
}

static int
smsc_setoe_locked(struct usbnet *un)
{
	struct smsc_softc * const sc = usbnet_softc(un);
	struct ifnet * const ifp = usbnet_ifp(un);
	uint32_t val;
	int err;

	KASSERT(IFNET_LOCKED(ifp));

	err = smsc_readreg(un, SMSC_COE_CTRL, &val);
	if (err != 0) {
		smsc_warn_printf(un, "failed to read SMSC_COE_CTRL (err=%d)\n",
		    err);
		return err;
	}

	/* Enable/disable the Rx checksum */
	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
		val |= (SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);
	else
		val &= ~(SMSC_COE_CTRL_RX_EN | SMSC_COE_CTRL_RX_MODE);

	/* Enable/disable the Tx checksum (currently not supported) */
	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_UDPv4_Tx))
		val |= SMSC_COE_CTRL_TX_EN;
	else
		val &= ~SMSC_COE_CTRL_TX_EN;

	sc->sc_coe_ctrl = val;

	err = smsc_writereg(un, SMSC_COE_CTRL, val);
	if (err != 0) {
		smsc_warn_printf(un, "failed to write SMSC_COE_CTRL (err=%d)\n",
		    err);
		return err;
	}

	return 0;
}

static int
smsc_setmacaddress(struct usbnet *un, const uint8_t *addr)
{
	USMSCHIST_FUNC(); USMSCHIST_CALLED();
	int err;
	uint32_t val;

	DPRINTF("setting mac address to %02jx:%02jx:%02jx:...", addr[0],
	    addr[1], addr[2], 0);

	DPRINTF("... %02jx:%02jx:%02jx", addr[3], addr[4], addr[5], 0);

	val = ((uint32_t)addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8)
	    | addr[0];
	if ((err = smsc_writereg(un, SMSC_MAC_ADDRL, val)) != 0)
		goto done;

	val = (addr[5] << 8) | addr[4];
	err = smsc_writereg(un, SMSC_MAC_ADDRH, val);

done:
	return err;
}

static void
smsc_reset(struct smsc_softc *sc)
{
	struct usbnet * const un = &sc->smsc_un;

	if (usbnet_isdying(un))
		return;

	/* Wait a little while for the chip to get its brains in order. */
	DELAY(1000);

	/* Reinitialize controller to achieve full reset. */
	smsc_chip_init(un);
}

static int
smsc_uno_init(struct ifnet *ifp)
{
	struct usbnet * const un = ifp->if_softc;
	struct smsc_softc * const sc = usbnet_softc(un);

	/* Reset the ethernet interface. */
	smsc_reset(sc);

	/* TCP/UDP checksum offload engines. */
	smsc_setoe_locked(un);

	return 0;
}

static void
smsc_uno_stop(struct ifnet *ifp, int disable)
{
	struct usbnet * const un = ifp->if_softc;
	struct smsc_softc * const sc = usbnet_softc(un);

	// XXXNH didn't do this before
	smsc_reset(sc);
}

static int
smsc_chip_init(struct usbnet *un)
{
	struct smsc_softc * const sc = usbnet_softc(un);
	uint32_t reg_val;
	int burst_cap;
	int err;

	/* Enter H/W config mode */
	smsc_writereg(un, SMSC_HW_CFG, SMSC_HW_CFG_LRST);

	if ((err = smsc_wait_for_bits(un, SMSC_HW_CFG,
	    SMSC_HW_CFG_LRST)) != 0) {
		smsc_warn_printf(un, "timed-out waiting for reset to "
		    "complete\n");
		goto init_failed;
	}

	/* Reset the PHY */
	smsc_writereg(un, SMSC_PM_CTRL, SMSC_PM_CTRL_PHY_RST);

	if ((err = smsc_wait_for_bits(un, SMSC_PM_CTRL,
	    SMSC_PM_CTRL_PHY_RST)) != 0) {
		smsc_warn_printf(un, "timed-out waiting for phy reset to "
		    "complete\n");
		goto init_failed;
	}
	usbd_delay_ms(un->un_udev, 40);

	/* Set the mac address */
	struct ifnet * const ifp = usbnet_ifp(un);
	const char *eaddr = CLLADDR(ifp->if_sadl);
	if ((err = smsc_setmacaddress(un, eaddr)) != 0) {
		smsc_warn_printf(un, "failed to set the MAC address\n");
		goto init_failed;
	}

	/*
	 * Don't know what the HW_CFG_BIR bit is, but following the reset
	 * sequence as used in the Linux driver.
	 */
	if ((err = smsc_readreg(un, SMSC_HW_CFG, &reg_val)) != 0) {
		smsc_warn_printf(un, "failed to read HW_CFG: %d\n", err);
		goto init_failed;
	}
	reg_val |= SMSC_HW_CFG_BIR;
	smsc_writereg(un, SMSC_HW_CFG, reg_val);

	/*
	 * There is a so called 'turbo mode' that the linux driver supports, it
	 * seems to allow you to jam multiple frames per Rx transaction.
	 * By default this driver supports that and therefore allows multiple
	 * frames per USB transfer.
	 *
	 * The xfer buffer size needs to reflect this as well, therefore based
	 * on the calculations in the Linux driver the RX bufsize is set to
	 * 18944,
	 *     bufsz = (16 * 1024 + 5 * 512)
	 *
	 * Burst capability is the number of URBs that can be in a burst of
	 * data/ethernet frames.
	 */

	if (un->un_udev->ud_speed == USB_SPEED_HIGH)
		burst_cap = 37;
	else
		burst_cap = 128;

	smsc_writereg(un, SMSC_BURST_CAP, burst_cap);

	/* Set the default bulk in delay (magic value from Linux driver) */
	smsc_writereg(un, SMSC_BULK_IN_DLY, 0x00002000);

	/*
	 * Initialise the RX interface
	 */
	if ((err = smsc_readreg(un, SMSC_HW_CFG, &reg_val)) < 0) {
		smsc_warn_printf(un, "failed to read HW_CFG: (err = %d)\n",
		    err);
		goto init_failed;
	}

	/*
	 * The following settings are used for 'turbo mode', a.k.a multiple
	 * frames per Rx transaction (again info taken form Linux driver).
	 */
	reg_val |= (SMSC_HW_CFG_MEF | SMSC_HW_CFG_BCE);

	/*
	 * set Rx data offset to ETHER_ALIGN which will make the IP header
	 * align on a word boundary.
	 */
	reg_val |= ETHER_ALIGN << SMSC_HW_CFG_RXDOFF_SHIFT;

	smsc_writereg(un, SMSC_HW_CFG, reg_val);

	/* Clear the status register ? */
	smsc_writereg(un, SMSC_INTR_STATUS, 0xffffffff);

	/* Read and display the revision register */
	if ((err = smsc_readreg(un, SMSC_ID_REV, &sc->sc_rev_id)) < 0) {
		smsc_warn_printf(un, "failed to read ID_REV (err = %d)\n", err);
		goto init_failed;
	}

	/* GPIO/LED setup */
	reg_val = SMSC_LED_GPIO_CFG_SPD_LED | SMSC_LED_GPIO_CFG_LNK_LED |
	    SMSC_LED_GPIO_CFG_FDX_LED;
	smsc_writereg(un, SMSC_LED_GPIO_CFG, reg_val);

	/*
	 * Initialise the TX interface
	 */
	smsc_writereg(un, SMSC_FLOW, 0);

	smsc_writereg(un, SMSC_AFC_CFG, AFC_CFG_DEFAULT);

	/* Read the current MAC configuration */
	if ((err = smsc_readreg(un, SMSC_MAC_CSR, &sc->sc_mac_csr)) < 0) {
		smsc_warn_printf(un, "failed to read MAC_CSR (err=%d)\n", err);
		goto init_failed;
	}

	/* disable pad stripping, collides with checksum offload */
	sc->sc_mac_csr &= ~SMSC_MAC_CSR_PADSTR;

	/* Vlan */
	smsc_writereg(un, SMSC_VLAN1, (uint32_t)ETHERTYPE_VLAN);

	/*
	 * Start TX
	 */
	sc->sc_mac_csr |= SMSC_MAC_CSR_TXEN;
	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);
	smsc_writereg(un, SMSC_TX_CFG, SMSC_TX_CFG_ON);

	/*
	 * Start RX
	 */
	sc->sc_mac_csr |= SMSC_MAC_CSR_RXEN;
	smsc_writereg(un, SMSC_MAC_CSR, sc->sc_mac_csr);

	return 0;

init_failed:
	smsc_err_printf(un, "smsc_chip_init failed (err=%d)\n", err);
	return err;
}

static int
smsc_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct usbnet * const un = ifp->if_softc;

	switch (cmd) {
	case SIOCSIFCAP:
		smsc_setoe_locked(un);
		break;
	default:
		break;
	}

	return 0;
}

static int
smsc_match(device_t parent, cfdata_t match, void *aux)
{
	struct usb_attach_arg *uaa = aux;

	return (usb_lookup(smsc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL) ?
	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
}

static void
smsc_attach(device_t parent, device_t self, void *aux)
{
	USBNET_MII_DECL_DEFAULT(unm);
	struct smsc_softc * const sc = device_private(self);
	struct usbnet * const un = &sc->smsc_un;
	struct usb_attach_arg *uaa = aux;
	struct usbd_device *dev = uaa->uaa_device;
	usb_interface_descriptor_t *id;
	usb_endpoint_descriptor_t *ed;
	char *devinfop;
	unsigned bufsz;
	int err, i;
	uint32_t mac_h, mac_l;

	KASSERT((void *)sc == un);

	aprint_naive("\n");
	aprint_normal("\n");

	un->un_dev = self;
	un->un_udev = dev;
	un->un_sc = sc;
	un->un_ops = &smsc_ops;
	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
	un->un_rx_list_cnt = SMSC_RX_LIST_CNT;
	un->un_tx_list_cnt = SMSC_TX_LIST_CNT;

	devinfop = usbd_devinfo_alloc(un->un_udev, 0);
	aprint_normal_dev(self, "%s\n", devinfop);
	usbd_devinfo_free(devinfop);

	err = usbd_set_config_no(dev, SMSC_CONFIG_INDEX, 1);
	if (err) {
		aprint_error_dev(self, "failed to set configuration"
		    ", err=%s\n", usbd_errstr(err));
		return;
	}

	/* Setup the endpoints for the SMSC LAN95xx device(s) */
	err = usbd_device2interface_handle(dev, SMSC_IFACE_IDX, &un->un_iface);
	if (err) {
		aprint_error_dev(self, "getting interface handle failed\n");
		return;
	}

	id = usbd_get_interface_descriptor(un->un_iface);

	if (dev->ud_speed >= USB_SPEED_HIGH) {
		bufsz = SMSC_MAX_BUFSZ;
	} else {
		bufsz = SMSC_MIN_BUFSZ;
	}
	un->un_rx_bufsz = bufsz;
	un->un_tx_bufsz = bufsz;

	/* Find endpoints. */
	for (i = 0; i < id->bNumEndpoints; i++) {
		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
		if (!ed) {
			aprint_error_dev(self, "couldn't get ep %d\n", i);
			return;
		}
		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
#if 0 /* not used yet */
		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
#endif
		}
	}

	usbnet_attach(un);

#ifdef notyet
	/*
	 * We can do TCPv4, and UDPv4 checksums in hardware.
	 */
	struct ifnet *ifp = usbnet_ifp(un);

	ifp->if_capabilities |=
	    /*IFCAP_CSUM_TCPv4_Tx |*/ IFCAP_CSUM_TCPv4_Rx |
	    /*IFCAP_CSUM_UDPv4_Tx |*/ IFCAP_CSUM_UDPv4_Rx;
#endif
	struct ethercom *ec = usbnet_ec(un);
	ec->ec_capabilities = ETHERCAP_VLAN_MTU;

	/* Setup some of the basics */
	un->un_phyno = 1;

	/*
	 * Attempt to get the mac address, if an EEPROM is not attached this
	 * will just return FF:FF:FF:FF:FF:FF, so in such cases we invent a MAC
	 * address based on urandom.
	 */
	memset(un->un_eaddr, 0xff, ETHER_ADDR_LEN);

	prop_dictionary_t dict = device_properties(self);
	prop_data_t eaprop = prop_dictionary_get(dict, "mac-address");

	if (eaprop != NULL) {
		KASSERT(prop_object_type(eaprop) == PROP_TYPE_DATA);
		KASSERT(prop_data_size(eaprop) == ETHER_ADDR_LEN);
		memcpy(un->un_eaddr, prop_data_value(eaprop),
		    ETHER_ADDR_LEN);
	} else {
		/* Check if there is already a MAC address in the register */
		if ((smsc_readreg(un, SMSC_MAC_ADDRL, &mac_l) == 0) &&
		    (smsc_readreg(un, SMSC_MAC_ADDRH, &mac_h) == 0)) {
			un->un_eaddr[5] = (uint8_t)((mac_h >> 8) & 0xff);
			un->un_eaddr[4] = (uint8_t)((mac_h) & 0xff);
			un->un_eaddr[3] = (uint8_t)((mac_l >> 24) & 0xff);
			un->un_eaddr[2] = (uint8_t)((mac_l >> 16) & 0xff);
			un->un_eaddr[1] = (uint8_t)((mac_l >> 8) & 0xff);
			un->un_eaddr[0] = (uint8_t)((mac_l) & 0xff);
		}
	}

	usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
	    0, &unm);
}

static void
smsc_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
{
	USMSCHIST_FUNC(); USMSCHIST_CALLED();
	struct smsc_softc * const sc = usbnet_softc(un);
	struct ifnet *ifp = usbnet_ifp(un);
	uint8_t *buf = c->unc_buf;
	int count;

	count = 0;
	DPRINTF("total_len %jd/%#jx", total_len, total_len, 0, 0);
	while (total_len != 0) {
		uint32_t rxhdr;
		if (total_len < sizeof(rxhdr)) {
			DPRINTF("total_len %jd < sizeof(rxhdr) %jd",
			    total_len, sizeof(rxhdr), 0, 0);
			if_statinc(ifp, if_ierrors);
			return;
		}

		memcpy(&rxhdr, buf, sizeof(rxhdr));
		rxhdr = le32toh(rxhdr);
		buf += sizeof(rxhdr);
		total_len -= sizeof(rxhdr);

		if (rxhdr & SMSC_RX_STAT_COLLISION)
			if_statinc(ifp, if_collisions);

		if (rxhdr & (SMSC_RX_STAT_ERROR
			   | SMSC_RX_STAT_LENGTH_ERROR
			   | SMSC_RX_STAT_MII_ERROR)) {
			DPRINTF("rx error (hdr 0x%08jx)", rxhdr, 0, 0, 0);
			if_statinc(ifp, if_ierrors);
			return;
		}

		uint16_t pktlen = (uint16_t)SMSC_RX_STAT_FRM_LENGTH(rxhdr);
		DPRINTF("total_len %jd pktlen %jd rxhdr 0x%08jx", total_len,
		    pktlen, rxhdr, 0);

		if (pktlen < ETHER_HDR_LEN) {
			DPRINTF("pktlen %jd < ETHER_HDR_LEN %jd", pktlen,
			    ETHER_HDR_LEN, 0, 0);
			if_statinc(ifp, if_ierrors);
			return;
		}

		pktlen += ETHER_ALIGN;

		if (pktlen > MCLBYTES) {
			DPRINTF("pktlen %jd > MCLBYTES %jd", pktlen, MCLBYTES, 0,
			    0);
			if_statinc(ifp, if_ierrors);
			return;
		}

		if (pktlen > total_len) {
			DPRINTF("pktlen %jd > total_len %jd", pktlen, total_len,
			    0, 0);
			if_statinc(ifp, if_ierrors);
			return;
		}

		uint8_t *pktbuf = buf + ETHER_ALIGN;
		size_t buflen = pktlen - ETHER_ALIGN;
		int mbuf_flags = M_HASFCS;
		int csum_flags = 0;
		uint16_t csum_data = 0;

 		KASSERT(pktlen < MCLBYTES);

		/* Check if RX TCP/UDP checksumming is being offloaded */
		if (sc->sc_coe_ctrl & SMSC_COE_CTRL_RX_EN) {
			DPRINTF("RX checksum offload checking", 0, 0, 0, 0);
			struct ether_header *eh = (struct ether_header *)pktbuf;
			const size_t cssz = sizeof(csum_data);

			/* Remove the extra 2 bytes of the csum */
			buflen -= cssz;

			/*
			 * The checksum appears to be simplistically calculated
			 * over the udp/tcp header and data up to the end of the
			 * eth frame.  Which means if the eth frame is padded
			 * the csum calculation is incorrectly performed over
			 * the padding bytes as well. Therefore to be safe we
			 * ignore the H/W csum on frames less than or equal to
			 * 64 bytes.
			 *
			 * Ignore H/W csum for non-IPv4 packets.
			 */
			DPRINTF("Ethertype %02jx pktlen %02jx",
			    be16toh(eh->ether_type), pktlen, 0, 0);
			if (be16toh(eh->ether_type) == ETHERTYPE_IP &&
			    pktlen > ETHER_MIN_LEN) {

				csum_flags |=
				    (M_CSUM_TCPv4 | M_CSUM_UDPv4 | M_CSUM_DATA);

				/*
				 * Copy the TCP/UDP checksum from the last 2
				 * bytes of the transfer and put in the
				 * csum_data field.
				 */
				memcpy(&csum_data, buf + pktlen - cssz, cssz);

				/*
				 * The data is copied in network order, but the
				 * csum algorithm in the kernel expects it to be
				 * in host network order.
				 */
				csum_data = ntohs(csum_data);
				DPRINTF("RX checksum offloaded (0x%04jx)",
				    csum_data, 0, 0, 0);
			}
		}

		/* round up to next longword */
		pktlen = (pktlen + 3) & ~0x3;

		/* total_len does not include the padding */
		if (pktlen > total_len)
			pktlen = total_len;

		buf += pktlen;
		total_len -= pktlen;

		/* push the packet up */
		usbnet_enqueue(un, pktbuf, buflen, csum_flags, csum_data,
		    mbuf_flags);

		count++;
	}

	if (count != 0)
		rnd_add_uint32(usbnet_rndsrc(un), count);
}

static unsigned
smsc_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
{
	uint32_t txhdr;
	uint32_t frm_len = 0;

	const size_t hdrsz = sizeof(txhdr) * 2;

	if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - hdrsz)
		return 0;

	/*
	 * Each frame is prefixed with two 32-bit values describing the
	 * length of the packet and buffer.
	 */
	txhdr = SMSC_TX_CTRL_0_BUF_SIZE(m->m_pkthdr.len) |
	    SMSC_TX_CTRL_0_FIRST_SEG | SMSC_TX_CTRL_0_LAST_SEG;
	txhdr = htole32(txhdr);
	memcpy(c->unc_buf, &txhdr, sizeof(txhdr));

	txhdr = SMSC_TX_CTRL_1_PKT_LENGTH(m->m_pkthdr.len);
	txhdr = htole32(txhdr);
	memcpy(c->unc_buf + sizeof(txhdr), &txhdr, sizeof(txhdr));

	frm_len += hdrsz;

	/* Next copy in the actual packet */
	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf + frm_len);
	frm_len += m->m_pkthdr.len;

	return frm_len;
}

#ifdef _MODULE
#include "ioconf.c"
#endif

USBNET_MODULE(smsc)