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
/*	$NetBSD: if_mos.c,v 1.24 2022/10/10 18:30:28 martin Exp $	*/
/*	$OpenBSD: if_mos.c,v 1.40 2019/07/07 06:40:10 kevlo Exp $	*/

/*
 * Copyright (c) 2008 Johann Christian Rode <jcrode@gmx.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Copyright (c) 1997, 1998, 1999, 2000-2003
 *	Bill Paul <wpaul@windriver.com>.  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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bill Paul.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``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 Bill Paul OR THE VOICES IN HIS HEAD
 * 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.
 */

/*
 * Moschip MCS7730/MCS7830/MCS7832 USB to Ethernet controller 
 * The datasheet is available at the following URL: 
 * http://www.moschip.com/data/products/MCS7830/Data%20Sheet_7830.pdf
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_mos.c,v 1.24 2022/10/10 18:30:28 martin Exp $");

#include <sys/param.h>

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

#define MOS_PAUSE_REWRITES	3

#define MOS_TIMEOUT		1000

#define MOS_RX_LIST_CNT		1
#define MOS_TX_LIST_CNT		1

/* Maximum size of a fast ethernet frame plus one byte for the status */
#define MOS_BUFSZ	 	(ETHER_MAX_LEN+1)

/*
 * USB endpoints.
 */
#define MOS_ENDPT_RX		0
#define MOS_ENDPT_TX		1
#define MOS_ENDPT_INTR		2
#define MOS_ENDPT_MAX		3

/*
 * USB vendor requests.
 */
#define MOS_UR_READREG		0x0e
#define MOS_UR_WRITEREG		0x0d

#define MOS_CONFIG_NO		1
#define MOS_IFACE_IDX		0

struct mos_type {
	struct usb_devno	mos_dev;
	u_int16_t		mos_flags;
#define MCS7730	0x0001		/* MCS7730 */
#define MCS7830	0x0002		/* MCS7830 */
#define MCS7832	0x0004		/* MCS7832 */
};

#define MOS_INC(x, y)           (x) = (x + 1) % y

#ifdef MOS_DEBUG
#define DPRINTF(x)      do { if (mosdebug) printf x; } while (0)
#define DPRINTFN(n,x)   do { if (mosdebug >= (n)) printf x; } while (0)
int     mosdebug = 0;
#else
#define DPRINTF(x)	__nothing
#define DPRINTFN(n,x)	__nothing
#endif

/*
 * Various supported device vendors/products.
 */
static const struct mos_type mos_devs[] = {
	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7730 }, MCS7730 },
	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7830 }, MCS7830 },
	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7832 }, MCS7832 },
	{ { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN030 }, MCS7830 },
};
#define mos_lookup(v, p) ((const struct mos_type *)usb_lookup(mos_devs, v, p))

static int mos_match(device_t, cfdata_t, void *);
static void mos_attach(device_t, device_t, void *);

CFATTACH_DECL_NEW(mos, sizeof(struct usbnet),
	mos_match, mos_attach, usbnet_detach, usbnet_activate);

static void mos_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
static unsigned mos_uno_tx_prepare(struct usbnet *, struct mbuf *,
				   struct usbnet_chain *);
static void mos_uno_mcast(struct ifnet *);
static int mos_uno_init(struct ifnet *);
static void mos_chip_init(struct usbnet *);
static void mos_uno_stop(struct ifnet *ifp, int disable);
static int mos_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *);
static int mos_uno_mii_write_reg(struct usbnet *, int, int, uint16_t);
static void mos_uno_mii_statchg(struct ifnet *);
static void mos_reset(struct usbnet *);

static int mos_reg_read_1(struct usbnet *, int);
static int mos_reg_read_2(struct usbnet *, int);
static int mos_reg_write_1(struct usbnet *, int, int);
static int mos_reg_write_2(struct usbnet *, int, int);
static int mos_readmac(struct usbnet *);
static int mos_writemac(struct usbnet *);
static int mos_write_mcast(struct usbnet *, uint8_t *);

static const struct usbnet_ops mos_ops = {
	.uno_stop = mos_uno_stop,
	.uno_mcast = mos_uno_mcast,
	.uno_read_reg = mos_uno_mii_read_reg,
	.uno_write_reg = mos_uno_mii_write_reg,
	.uno_statchg = mos_uno_mii_statchg,
	.uno_tx_prepare = mos_uno_tx_prepare,
	.uno_rx_loop = mos_uno_rx_loop,
	.uno_init = mos_uno_init,
};

static int
mos_reg_read_1(struct usbnet *un, int reg)
{
	usb_device_request_t	req;
	usbd_status		err;
	uByte			val = 0;

	if (usbnet_isdying(un))
		return 0;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = MOS_UR_READREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 1);

	err = usbd_do_request(un->un_udev, &req, &val);

	if (err) {
		aprint_error_dev(un->un_dev, "read reg %x\n", reg);
		return 0;
	}

	return val;
}

static int
mos_reg_read_2(struct usbnet *un, int reg)
{
	usb_device_request_t	req;
	usbd_status		err;
	uWord			val;

	if (usbnet_isdying(un))
		return 0;

	USETW(val,0);

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = MOS_UR_READREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 2);

	err = usbd_do_request(un->un_udev, &req, &val);

	if (err) {
		aprint_error_dev(un->un_dev, "read reg2 %x\n", reg);
		return 0;
	}

	return UGETW(val);
}

static int
mos_reg_write_1(struct usbnet *un, int reg, int aval)
{
	usb_device_request_t	req;
	usbd_status		err;
	uByte			val;

	if (usbnet_isdying(un))
		return 0;

	val = aval;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = MOS_UR_WRITEREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 1);

	err = usbd_do_request(un->un_udev, &req, &val);

	if (err)
		aprint_error_dev(un->un_dev, "write reg %x <- %x\n", 
		    reg, aval);

	return 0;
}

static int
mos_reg_write_2(struct usbnet *un, int reg, int aval)
{
	usb_device_request_t	req;
	usbd_status		err;
	uWord			val;

	USETW(val, aval);

	if (usbnet_isdying(un))
		return EIO;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = MOS_UR_WRITEREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 2);

	err = usbd_do_request(un->un_udev, &req, &val);

	if (err)
		aprint_error_dev(un->un_dev, "write reg2 %x <- %x\n", 
		    reg, aval);

	return 0;
}

static int
mos_readmac(struct usbnet *un)
{
	usb_device_request_t	req;
	usbd_status		err;

	if (usbnet_isdying(un))
		return 0;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = MOS_UR_READREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, MOS_MAC);
	USETW(req.wLength, ETHER_ADDR_LEN);

	err = usbd_do_request(un->un_udev, &req, un->un_eaddr);

	if (err)
		aprint_error_dev(un->un_dev, "%s: failed", __func__);

	return err;
}

static int
mos_writemac(struct usbnet *un)
{
	usb_device_request_t	req;
	usbd_status		err;

	if (usbnet_isdying(un))
		return 0;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = MOS_UR_WRITEREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, MOS_MAC);
	USETW(req.wLength, ETHER_ADDR_LEN);

	err = usbd_do_request(un->un_udev, &req, un->un_eaddr);

	if (err)
		aprint_error_dev(un->un_dev, "%s: failed", __func__);

	return 0;
}

static int
mos_write_mcast(struct usbnet *un, uint8_t *hashtbl)
{
	usb_device_request_t	req;
	usbd_status		err;

	if (usbnet_isdying(un))
		return EIO;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = MOS_UR_WRITEREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, MOS_MCAST_TABLE);
	USETW(req.wLength, 8);

	err = usbd_do_request(un->un_udev, &req, hashtbl);

	if (err) {
		aprint_error_dev(un->un_dev, "%s: failed", __func__);
		return(-1);
	}

	return 0;
}

static int
mos_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
{
	int			i, res;

	mos_reg_write_2(un, MOS_PHY_DATA, 0);
	mos_reg_write_1(un, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
	    MOS_PHYCTL_READ);
	mos_reg_write_1(un, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
	    MOS_PHYSTS_PENDING);

	for (i = 0; i < MOS_TIMEOUT; i++) {
		if (usbnet_isdying(un)) {
			*val = 0;
			return ENXIO;
		}
		if (mos_reg_read_1(un, MOS_PHY_STS) & MOS_PHYSTS_READY)
			break;
	}
	if (i == MOS_TIMEOUT) {
		aprint_error_dev(un->un_dev, "read PHY failed\n");
		*val = 0;
		return EIO;
	}

	res = mos_reg_read_2(un, MOS_PHY_DATA);
	*val = res;

	DPRINTFN(10,("%s: %s: phy %d reg %d val %u\n",
	    device_xname(un->un_dev), __func__, phy, reg, res));

	return 0;
}

static int
mos_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
{
	int			i;

	DPRINTFN(10,("%s: %s: phy %d reg %d val %u\n",
	    device_xname(un->un_dev), __func__, phy, reg, val));

	mos_reg_write_2(un, MOS_PHY_DATA, val);
	mos_reg_write_1(un, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
	    MOS_PHYCTL_WRITE);
	mos_reg_write_1(un, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
	    MOS_PHYSTS_PENDING);

	for (i = 0; i < MOS_TIMEOUT; i++) {
		if (usbnet_isdying(un))
			return ENXIO;
		if (mos_reg_read_1(un, MOS_PHY_STS) & MOS_PHYSTS_READY)
			break;
	}
	if (i == MOS_TIMEOUT) {
		aprint_error_dev(un->un_dev, "write PHY failed\n");
		return EIO;
	}

	return 0;
}

void
mos_uno_mii_statchg(struct ifnet *ifp)
{
	struct usbnet * const		un = ifp->if_softc;
	struct mii_data * const		mii = usbnet_mii(un);
	int				val, err;

	if (usbnet_isdying(un))
		return;

	DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__));

	/* disable RX, TX prior to changing FDX, SPEEDSEL */
	val = mos_reg_read_1(un, MOS_CTL);
	val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
	mos_reg_write_1(un, MOS_CTL, val);

	/* reset register which counts dropped frames */
	mos_reg_write_1(un, MOS_FRAME_DROP_CNT, 0);

	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
		val |= MOS_CTL_FDX_ENB;
	else
		val &= ~(MOS_CTL_FDX_ENB);

	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
	    (IFM_ACTIVE | IFM_AVALID)) {
		switch (IFM_SUBTYPE(mii->mii_media_active)) {
		case IFM_100_TX:
			val |=  MOS_CTL_SPEEDSEL;
			break;
		case IFM_10_T:
			val &= ~(MOS_CTL_SPEEDSEL);
			break;
		}
		usbnet_set_link(un, true);
	}

	/* re-enable TX, RX */
	val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
	err = mos_reg_write_1(un, MOS_CTL, val);

	if (err)
		aprint_error_dev(un->un_dev, "media change failed\n");
}

static void
mos_uno_mcast(struct ifnet *ifp)
{
	struct usbnet		*un = ifp->if_softc;
	struct ethercom		*ec = usbnet_ec(un);
	struct ether_multi	*enm;
	struct ether_multistep	step;
	u_int32_t h = 0;
	u_int8_t rxmode, mchash[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };

	if (usbnet_isdying(un))
		return;

	rxmode = mos_reg_read_1(un, MOS_CTL);
	rxmode &= ~(MOS_CTL_ALLMULTI | MOS_CTL_RX_PROMISC);

	ETHER_LOCK(ec);
	if (usbnet_ispromisc(un)) {
		ec->ec_flags |= ETHER_F_ALLMULTI;
		ETHER_UNLOCK(ec);
		/* run promisc. mode */
		rxmode |= MOS_CTL_ALLMULTI; /* ??? */
		rxmode |= MOS_CTL_RX_PROMISC;
		goto update;
	}
	ec->ec_flags &= ~ETHER_F_ALLMULTI;
	ETHER_FIRST_MULTI(step, ec, enm);
	while (enm != NULL) {
		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
			ec->ec_flags |= ETHER_F_ALLMULTI;
			ETHER_UNLOCK(ec);
			memset(mchash, 0, sizeof(mchash)); /* correct ??? */
			/* accept all multicast frame */
			rxmode |= MOS_CTL_ALLMULTI;
			goto update;
		}
		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
		/* 3(31:29) and 3(28:26) sampling to have uint8_t[8] */
		mchash[h >> 29] |= 1 << ((h >> 26) % 8);
		ETHER_NEXT_MULTI(step, enm);
	}
	ETHER_UNLOCK(ec);
	/* MOS receive filter is always on */
 update:
	/* 
	 * The datasheet claims broadcast frames were always accepted
	 * regardless of filter settings. But the hardware seems to
	 * filter broadcast frames, so pass them explicitly.
	 */
	mchash[7] |= 0x80;
	mos_write_mcast(un, mchash);
	mos_reg_write_1(un, MOS_CTL, rxmode);
}

static void
mos_reset(struct usbnet *un)
{
	u_int8_t ctl;

	if (usbnet_isdying(un))
		return;

	ctl = mos_reg_read_1(un, MOS_CTL);
	ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB |
	    MOS_CTL_RX_ENB);
	/* Disable RX, TX, promiscuous and allmulticast mode */
	mos_reg_write_1(un, MOS_CTL, ctl);

	/* Reset frame drop counter register to zero */
	mos_reg_write_1(un, MOS_FRAME_DROP_CNT, 0);

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

void
mos_chip_init(struct usbnet *un)
{
	int	i;

	/*
	 * Rev.C devices have a pause threshold register which needs to be set
	 * at startup.
	 */
	if (mos_reg_read_1(un, MOS_PAUSE_TRHD) != -1) {
		for (i = 0; i < MOS_PAUSE_REWRITES; i++)
			mos_reg_write_1(un, MOS_PAUSE_TRHD, 0);
	}
}

/*
 * Probe for a MCS7x30 chip.
 */
static int
mos_match(device_t parent, cfdata_t match, void *aux)
{
	struct usb_attach_arg *uaa = aux;

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

/*
 * Attach the interface.
 */
static void
mos_attach(device_t parent, device_t self, void *aux)
{
	USBNET_MII_DECL_DEFAULT(unm);
	struct usbnet *		un = device_private(self);
	struct usb_attach_arg	*uaa = aux;
	struct usbd_device	*dev = uaa->uaa_device;
	usbd_status		err;
	usb_interface_descriptor_t 	*id;
	usb_endpoint_descriptor_t 	*ed;
	char			*devinfop;
	int			i;

	aprint_naive("\n");
	aprint_normal("\n");
	devinfop = usbd_devinfo_alloc(dev, 0);
	aprint_normal_dev(self, "%s\n", devinfop);
	usbd_devinfo_free(devinfop);

	un->un_dev = self;
	un->un_udev = dev;
	un->un_sc = un;
	un->un_ops = &mos_ops;
	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
	un->un_rx_list_cnt = MOS_RX_LIST_CNT;
	un->un_tx_list_cnt = MOS_TX_LIST_CNT;
	un->un_rx_bufsz = un->un_tx_bufsz = MOS_BUFSZ;

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

	err = usbd_device2interface_handle(dev, MOS_IFACE_IDX, &un->un_iface);
	if (err) {
		aprint_error_dev(self, "failed getting interface handle"
		    ", err=%s\n", usbd_errstr(err));
		return;
	}

	un->un_flags = mos_lookup(uaa->uaa_vendor, uaa->uaa_product)->mos_flags;

	id = usbd_get_interface_descriptor(un->un_iface);

	/* 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;
		} 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;
		}
	}

	if (un->un_flags & MCS7730)
		aprint_normal_dev(self, "MCS7730\n");
	else if (un->un_flags & MCS7830)
		aprint_normal_dev(self, "MCS7830\n");
	else if (un->un_flags & MCS7832)
		aprint_normal_dev(self, "MCS7832\n");

	/* Set these up now for register access. */
	usbnet_attach(un);

	mos_chip_init(un);

	/*
	 * Read MAC address, inform the world.
	 */
	err = mos_readmac(un);
	if (err) {
		aprint_error_dev(self, "couldn't read MAC address\n");
		return;
	}

	struct ethercom *ec = usbnet_ec(un);
	ec->ec_capabilities = ETHERCAP_VLAN_MTU;

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

/*
 * A frame has been uploaded: pass the resulting mbuf chain up to
 * the higher level protocols.
 */
void
mos_uno_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len)
{
	struct ifnet		*ifp = usbnet_ifp(un);
	uint8_t			*buf = c->unc_buf;
	u_int8_t		rxstat;
	u_int16_t		pktlen = 0;

	DPRINTFN(5,("%s: %s: enter len %u\n",
	    device_xname(un->un_dev), __func__, total_len));

	if (total_len <= 1)
		return;

	/* evaluate status byte at the end */
	pktlen = total_len - 1;
	if (pktlen > un->un_rx_bufsz) {
		if_statinc(ifp, if_ierrors);
		return;
	}
	rxstat = buf[pktlen] & MOS_RXSTS_MASK;

	if (rxstat != MOS_RXSTS_VALID) {
		DPRINTF(("%s: erroneous frame received: ",
		    device_xname(un->un_dev)));
		if (rxstat & MOS_RXSTS_SHORT_FRAME)
			DPRINTF(("frame size less than 64 bytes\n"));
		if (rxstat & MOS_RXSTS_LARGE_FRAME)
			DPRINTF(("frame size larger than 1532 bytes\n"));
		if (rxstat & MOS_RXSTS_CRC_ERROR)
			DPRINTF(("CRC error\n"));
		if (rxstat & MOS_RXSTS_ALIGN_ERROR)
			DPRINTF(("alignment error\n"));
		if_statinc(ifp, if_ierrors);
		return;
	}

	if (pktlen < sizeof(struct ether_header) ) {
		if_statinc(ifp, if_ierrors);
		return;
	}

	usbnet_enqueue(un, c->unc_buf, pktlen, 0, 0, 0);
}

static unsigned
mos_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
{
	int			length;

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

	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
	length = m->m_pkthdr.len;

	DPRINTFN(5,("%s: %s: len %u\n",
	    device_xname(un->un_dev), __func__, length));

	return length;
}

static int
mos_uno_init(struct ifnet *ifp)
{
	struct usbnet * const un = ifp->if_softc;
	u_int8_t		rxmode;
	unsigned char		ipgs[2];

	/* Reset the ethernet interface. */
	mos_reset(un);

	/* Write MAC address. */
	mos_writemac(un);

	/* Read and set transmitter IPG values */
	ipgs[0] = mos_reg_read_1(un, MOS_IPG0);
	ipgs[1] = mos_reg_read_1(un, MOS_IPG1);
	mos_reg_write_1(un, MOS_IPG0, ipgs[0]);
	mos_reg_write_1(un, MOS_IPG1, ipgs[1]);

	/* Enable receiver and transmitter, bridge controls speed/duplex mode */
	rxmode = mos_reg_read_1(un, MOS_CTL);
	rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
	rxmode &= ~(MOS_CTL_SLEEP);
	mos_reg_write_1(un, MOS_CTL, rxmode);

	return 0;
}

void
mos_uno_stop(struct ifnet *ifp, int disable)
{
	struct usbnet * const un = ifp->if_softc;

	mos_reset(un);
}