xref: /openbsd-src/sys/dev/usb/if_mos.c (revision e825892b4374bf2b88a52670a9df9fdc64034bf3)
1 /*	$OpenBSD: if_mos.c,v 1.23 2014/01/18 15:18:01 stsp Exp $	*/
2 
3 /*
4  * Copyright (c) 2008 Johann Christian Rode <jcrode@gmx.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg@openbsd.org>
21  *
22  * Permission to use, copy, modify, and distribute this software for any
23  * purpose with or without fee is hereby granted, provided that the above
24  * copyright notice and this permission notice appear in all copies.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
27  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
29  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
31  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
32  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33  */
34 
35 /*
36  * Copyright (c) 1997, 1998, 1999, 2000-2003
37  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *	This product includes software developed by Bill Paul.
50  * 4. Neither the name of the author nor the names of any co-contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
64  * THE POSSIBILITY OF SUCH DAMAGE.
65  */
66 
67 /*
68  * Moschip MCS7730/MCS7830/MCS7832 USB to Ethernet controller
69  * The datasheet is available at the following URL:
70  * http://www.moschip.com/data/products/MCS7830/Data%20Sheet_7830.pdf
71  */
72 
73 #include "bpfilter.h"
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/sockio.h>
78 #include <sys/rwlock.h>
79 #include <sys/mbuf.h>
80 #include <sys/kernel.h>
81 #include <sys/socket.h>
82 
83 #include <sys/device.h>
84 
85 #include <machine/bus.h>
86 
87 #include <net/if.h>
88 #include <net/if_dl.h>
89 #include <net/if_media.h>
90 
91 #if NBPFILTER > 0
92 #include <net/bpf.h>
93 #endif
94 
95 #ifdef INET
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #include <netinet/if_ether.h>
100 #endif
101 
102 #include <dev/mii/mii.h>
103 #include <dev/mii/miivar.h>
104 
105 #include <dev/usb/usb.h>
106 #include <dev/usb/usbdi.h>
107 #include <dev/usb/usbdi_util.h>
108 #include <dev/usb/usbdivar.h>
109 #include <dev/usb/usbdevs.h>
110 
111 #include <dev/usb/if_mosreg.h>
112 
113 #ifdef MOS_DEBUG
114 #define DPRINTF(x)      do { if (mosdebug) printf x; } while (0)
115 #define DPRINTFN(n,x)   do { if (mosdebug >= (n)) printf x; } while (0)
116 int     mosdebug = 0;
117 #else
118 #define DPRINTF(x)
119 #define DPRINTFN(n,x)
120 #endif
121 
122 /*
123  * Various supported device vendors/products.
124  */
125 const struct mos_type mos_devs[] = {
126 	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7730 }, MCS7730 },
127 	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7830 }, MCS7830 },
128 	{ { USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7832 }, MCS7832 },
129 	{ { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN030 }, MCS7830 },
130 };
131 #define mos_lookup(v, p) ((struct mos_type *)usb_lookup(mos_devs, v, p))
132 
133 int mos_match(struct device *, void *, void *);
134 void mos_attach(struct device *, struct device *, void *);
135 int mos_detach(struct device *, int);
136 int mos_activate(struct device *, int);
137 
138 struct cfdriver mos_cd = {
139 	NULL, "mos", DV_IFNET
140 };
141 
142 const struct cfattach mos_ca = {
143 	sizeof(struct mos_softc),
144 	mos_match,
145 	mos_attach,
146 	mos_detach,
147 	mos_activate,
148 };
149 
150 int mos_tx_list_init(struct mos_softc *);
151 int mos_rx_list_init(struct mos_softc *);
152 struct mbuf *mos_newbuf(void);
153 int mos_encap(struct mos_softc *, struct mbuf *, int);
154 void mos_rxeof(struct usbd_xfer *, void *, usbd_status);
155 void mos_txeof(struct usbd_xfer *, void *, usbd_status);
156 void mos_tick(void *);
157 void mos_tick_task(void *);
158 void mos_start(struct ifnet *);
159 int mos_ioctl(struct ifnet *, u_long, caddr_t);
160 void mos_init(void *);
161 void mos_chip_init(struct mos_softc *);
162 void mos_stop(struct mos_softc *);
163 void mos_watchdog(struct ifnet *);
164 int mos_miibus_readreg(struct device *, int, int);
165 void mos_miibus_writereg(struct device *, int, int, int);
166 void mos_miibus_statchg(struct device *);
167 int mos_ifmedia_upd(struct ifnet *);
168 void mos_ifmedia_sts(struct ifnet *, struct ifmediareq *);
169 void mos_reset(struct mos_softc *sc);
170 
171 int mos_reg_read_1(struct mos_softc *, int);
172 int mos_reg_read_2(struct mos_softc *, int);
173 int mos_reg_write_1(struct mos_softc *, int, int);
174 int mos_reg_write_2(struct mos_softc *, int, int);
175 int mos_readmac(struct mos_softc *, u_char *);
176 int mos_writemac(struct mos_softc *, u_char *);
177 int mos_write_mcast(struct mos_softc *, u_char *);
178 
179 void mos_iff(struct mos_softc *);
180 void mos_lock_mii(struct mos_softc *);
181 void mos_unlock_mii(struct mos_softc *);
182 
183 /*
184  * Get exclusive access to the MII registers
185  */
186 void
187 mos_lock_mii(struct mos_softc *sc)
188 {
189 	sc->mos_refcnt++;
190 	rw_enter_write(&sc->mos_mii_lock);
191 }
192 
193 void
194 mos_unlock_mii(struct mos_softc *sc)
195 {
196 	rw_exit_write(&sc->mos_mii_lock);
197 	if (--sc->mos_refcnt < 0)
198 		usb_detach_wakeup(&sc->mos_dev);
199 }
200 
201 int
202 mos_reg_read_1(struct mos_softc *sc, int reg)
203 {
204 	usb_device_request_t	req;
205 	usbd_status		err;
206 	uByte			val = 0;
207 
208 	if (usbd_is_dying(sc->mos_udev))
209 		return(0);
210 
211 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
212 	req.bRequest = MOS_UR_READREG;
213 	USETW(req.wValue, 0);
214 	USETW(req.wIndex, reg);
215 	USETW(req.wLength, 1);
216 
217 	err = usbd_do_request(sc->mos_udev, &req, &val);
218 
219 	if (err) {
220 		DPRINTF(("mos_reg_read_1 error, reg: %d\n", reg));
221 		return (-1);
222 	}
223 
224 	return (val);
225 }
226 
227 int
228 mos_reg_read_2(struct mos_softc *sc, int reg)
229 {
230 	usb_device_request_t	req;
231 	usbd_status		err;
232 	uWord			val;
233 
234 	USETW(val,0);
235 
236 	if (usbd_is_dying(sc->mos_udev))
237 		return(0);
238 
239 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
240 	req.bRequest = MOS_UR_READREG;
241 	USETW(req.wValue, 0);
242 	USETW(req.wIndex, reg);
243 	USETW(req.wLength, 2);
244 
245 	err = usbd_do_request(sc->mos_udev, &req, &val);
246 
247 	if (err) {
248 		DPRINTF(("mos_reg_read_2 error, reg: %d\n", reg));
249 		return (-1);
250 	}
251 
252 	return(UGETW(val));
253 }
254 
255 int
256 mos_reg_write_1(struct mos_softc *sc, int reg, int aval)
257 {
258 	usb_device_request_t	req;
259 	usbd_status		err;
260 	uByte			val;
261 
262 	val = aval;
263 
264 	if (usbd_is_dying(sc->mos_udev))
265 		return(0);
266 
267 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
268 	req.bRequest = MOS_UR_WRITEREG;
269 	USETW(req.wValue, 0);
270 	USETW(req.wIndex, reg);
271 	USETW(req.wLength, 1);
272 
273 	err = usbd_do_request(sc->mos_udev, &req, &val);
274 
275 	if (err) {
276 		DPRINTF(("mos_reg_write_1 error, reg: %d\n", reg));
277 		return (-1);
278 	}
279 
280 	return(0);
281 }
282 
283 int
284 mos_reg_write_2(struct mos_softc *sc, int reg, int aval)
285 {
286 	usb_device_request_t	req;
287 	usbd_status		err;
288 	uWord			val;
289 
290 	USETW(val, aval);
291 
292 	if (usbd_is_dying(sc->mos_udev))
293 		return (0);
294 
295 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
296 	req.bRequest = MOS_UR_WRITEREG;
297 	USETW(req.wValue, 0);
298 	USETW(req.wIndex, reg);
299 	USETW(req.wLength, 2);
300 
301 	err = usbd_do_request(sc->mos_udev, &req, &val);
302 
303 	if (err) {
304 		DPRINTF(("mos_reg_write_2 error, reg: %d\n", reg));
305 		return (-1);
306 	}
307 
308 	return (0);
309 }
310 
311 int
312 mos_readmac(struct mos_softc *sc, u_char *mac)
313 {
314 	usb_device_request_t	req;
315 	usbd_status		err;
316 
317 	if (usbd_is_dying(sc->mos_udev))
318 		return(0);
319 
320 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
321 	req.bRequest = MOS_UR_READREG;
322 	USETW(req.wValue, 0);
323 	USETW(req.wIndex, MOS_MAC);
324 	USETW(req.wLength, ETHER_ADDR_LEN);
325 
326 	err = usbd_do_request(sc->mos_udev, &req, mac);
327 
328 	if (err) {
329 		DPRINTF(("mos_readmac error"));
330 		return (-1);
331 	}
332 
333 	return (0);
334 }
335 
336 int
337 mos_writemac(struct mos_softc *sc, u_char *mac)
338 {
339 	usb_device_request_t	req;
340 	usbd_status		err;
341 
342 	if (usbd_is_dying(sc->mos_udev))
343 		return(0);
344 
345 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
346 	req.bRequest = MOS_UR_WRITEREG;
347 	USETW(req.wValue, 0);
348 	USETW(req.wIndex, MOS_MAC);
349 	USETW(req.wLength, ETHER_ADDR_LEN);
350 
351 	err = usbd_do_request(sc->mos_udev, &req, mac);
352 
353 	if (err) {
354 		DPRINTF(("mos_writemac error"));
355 		return (-1);
356 	}
357 
358 	return (0);
359 }
360 
361 int
362 mos_write_mcast(struct mos_softc *sc, u_char *hashtbl)
363 {
364 	usb_device_request_t	req;
365 	usbd_status		err;
366 
367 	if (usbd_is_dying(sc->mos_udev))
368 		return(0);
369 
370 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
371 	req.bRequest = MOS_UR_WRITEREG;
372 	USETW(req.wValue, 0);
373 	USETW(req.wIndex, MOS_MCAST_TABLE);
374 	USETW(req.wLength, 8);
375 
376 	err = usbd_do_request(sc->mos_udev, &req, hashtbl);
377 
378 	if (err) {
379 		DPRINTF(("mos_reg_mcast error\n"));
380 		return(-1);
381 	}
382 
383 	return(0);
384 }
385 
386 int
387 mos_miibus_readreg(struct device *dev, int phy, int reg)
388 {
389 	struct mos_softc	*sc = (void *)dev;
390 	uWord			val;
391 	int			i,res;
392 
393 	if (usbd_is_dying(sc->mos_udev)) {
394 		DPRINTF(("mos: dying\n"));
395 		return (0);
396 	}
397 
398 	USETW(val, 0);
399 
400 	mos_lock_mii(sc);
401 
402 	mos_reg_write_2(sc, MOS_PHY_DATA, 0);
403 	mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
404 	    MOS_PHYCTL_READ);
405 	mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
406 	    MOS_PHYSTS_PENDING);
407 
408 	for (i = 0; i < MOS_TIMEOUT; i++) {
409 		if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY)
410 			break;
411 	}
412 	if (i == MOS_TIMEOUT) {
413 		printf("%s: MII read timeout\n", sc->mos_dev.dv_xname);
414 	}
415 
416 	res = mos_reg_read_2(sc, MOS_PHY_DATA);
417 
418 	mos_unlock_mii(sc);
419 
420 	return (res);
421 }
422 
423 void
424 mos_miibus_writereg(struct device *dev, int phy, int reg, int val)
425 {
426 	struct mos_softc	*sc = (void *)dev;
427 	int			i;
428 
429 	if (usbd_is_dying(sc->mos_udev))
430 		return;
431 
432 	mos_lock_mii(sc);
433 
434 	mos_reg_write_2(sc, MOS_PHY_DATA, val);
435 	mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
436 	    MOS_PHYCTL_WRITE);
437 	mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
438 	    MOS_PHYSTS_PENDING);
439 
440 	for (i = 0; i < MOS_TIMEOUT; i++) {
441 		if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY)
442 			break;
443 	}
444 	if (i == MOS_TIMEOUT) {
445 		printf("%s: MII write timeout\n", sc->mos_dev.dv_xname);
446 	}
447 
448 	mos_unlock_mii(sc);
449 
450 	return;
451 }
452 
453 void
454 mos_miibus_statchg(struct device *dev)
455 {
456 	struct mos_softc	*sc = (void *)dev;
457 	struct mii_data		*mii = GET_MII(sc);
458 	int			val, err;
459 
460 	mos_lock_mii(sc);
461 
462 	/* disable RX, TX prior to changing FDX, SPEEDSEL */
463 	val = mos_reg_read_1(sc, MOS_CTL);
464 	val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
465 	mos_reg_write_1(sc, MOS_CTL, val);
466 
467 	/* reset register which counts dropped frames */
468 	mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
469 
470 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
471 		val |= MOS_CTL_FDX_ENB;
472 	else
473 		val &= ~(MOS_CTL_FDX_ENB);
474 
475 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
476 		case IFM_100_TX:
477 			val |=  MOS_CTL_SPEEDSEL;
478 			break;
479 		case IFM_10_T:
480 			val &= ~(MOS_CTL_SPEEDSEL);
481 			break;
482 	}
483 
484 	/* re-enable TX, RX */
485 	val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
486 	err = mos_reg_write_1(sc, MOS_CTL, val);
487 	mos_unlock_mii(sc);
488 
489 	if (err) {
490 		printf("%s: media change failed\n", sc->mos_dev.dv_xname);
491 		return;
492 	}
493 }
494 
495 /*
496  * Set media options.
497  */
498 int
499 mos_ifmedia_upd(struct ifnet *ifp)
500 {
501 	struct mos_softc	*sc = ifp->if_softc;
502 	struct mii_data		*mii = GET_MII(sc);
503 
504 	sc->mos_link = 0;
505 	if (mii->mii_instance) {
506 		struct mii_softc	*miisc;
507 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
508 			mii_phy_reset(miisc);
509 	}
510 	mii_mediachg(mii);
511 
512 	return (0);
513 }
514 
515 /*
516  * Report current media status.
517  */
518 void
519 mos_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
520 {
521 	struct mos_softc	*sc = ifp->if_softc;
522 	struct mii_data		*mii = GET_MII(sc);
523 
524 	mii_pollstat(mii);
525 	ifmr->ifm_active = mii->mii_media_active;
526 	ifmr->ifm_status = mii->mii_media_status;
527 }
528 
529 void
530 mos_iff(struct mos_softc *sc)
531 {
532 	struct ifnet		*ifp = GET_IFP(sc);
533 	struct arpcom		*ac = &sc->arpcom;
534 	struct ether_multi	*enm;
535 	struct ether_multistep	step;
536 	u_int32_t		h = 0;
537 	u_int8_t		rxmode, hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
538 
539 	if (usbd_is_dying(sc->mos_udev))
540 		return;
541 
542 	rxmode = mos_reg_read_1(sc, MOS_CTL);
543 	rxmode &= ~(MOS_CTL_ALLMULTI | MOS_CTL_RX_PROMISC);
544 	ifp->if_flags &= ~IFF_ALLMULTI;
545 
546 	if (ifp->if_flags & IFF_PROMISC || ac->ac_multirangecnt > 0) {
547 		ifp->if_flags |= IFF_ALLMULTI;
548 		rxmode |= MOS_CTL_ALLMULTI;
549 		if (ifp->if_flags & IFF_PROMISC)
550 			rxmode |= MOS_CTL_RX_PROMISC;
551 	} else {
552 		/* now program new ones */
553 		ETHER_FIRST_MULTI(step, ac, enm);
554 		while (enm != NULL) {
555 			h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
556 
557 			hashtbl[h / 8] |= 1 << (h % 8);
558 
559 			ETHER_NEXT_MULTI(step, enm);
560 		}
561 		/*
562 		 * The datasheet claims broadcast frames were always accepted
563 		 * regardless of filter settings. But the hardware seems to
564 		 * filter broadcast frames, so pass them explicitly.
565 		 */
566 		h = ether_crc32_be(etherbroadcastaddr, ETHER_ADDR_LEN) >> 26;
567 		hashtbl[h / 8] |= 1 << (h % 8);
568 	}
569 
570 	mos_write_mcast(sc, (void *)&hashtbl);
571 	mos_reg_write_1(sc, MOS_CTL, rxmode);
572 }
573 
574 void
575 mos_reset(struct mos_softc *sc)
576 {
577 	u_int8_t ctl;
578 	if (usbd_is_dying(sc->mos_udev))
579 		return;
580 
581 	ctl = mos_reg_read_1(sc, MOS_CTL);
582 	ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB |
583 	    MOS_CTL_RX_ENB);
584 	/* Disable RX, TX, promiscuous and allmulticast mode */
585 	mos_reg_write_1(sc, MOS_CTL, ctl);
586 
587 	/* Reset frame drop counter register to zero */
588 	mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
589 
590 	/* Wait a little while for the chip to get its brains in order. */
591 	DELAY(1000);
592 	return;
593 }
594 
595 void
596 mos_chip_init(struct mos_softc *sc)
597 {
598 	int	i;
599 
600 	/*
601 	 * Rev.C devices have a pause threshold register which needs to be set
602 	 * at startup.
603 	 */
604 	if (mos_reg_read_1(sc, MOS_PAUSE_TRHD) != -1) {
605 		for (i=0;i<MOS_PAUSE_REWRITES;i++)
606 			mos_reg_write_1(sc, MOS_PAUSE_TRHD, 0);
607 	}
608 
609 	sc->mos_phyaddrs[0] = 1; sc->mos_phyaddrs[1] = 0xFF;
610 }
611 
612 /*
613  * Probe for a MCS7x30 chip.
614  */
615 int
616 mos_match(struct device *parent, void *match, void *aux)
617 {
618 	struct usb_attach_arg *uaa = aux;
619 
620 	if (!uaa->iface)
621 		return(UMATCH_NONE);
622 
623 	return (mos_lookup(uaa->vendor, uaa->product) != NULL ?
624 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
625 }
626 
627 /*
628  * Attach the interface. Allocate softc structures, do ifmedia
629  * setup and ethernet/BPF attach.
630  */
631 void
632 mos_attach(struct device *parent, struct device *self, void *aux)
633 {
634 	struct mos_softc	*sc = (struct mos_softc *)self;
635 	struct usb_attach_arg	*uaa = aux;
636 	struct ifnet		*ifp;
637 	struct usbd_device	*dev = uaa->device;
638 	usbd_status		err;
639 	usb_interface_descriptor_t 	*id;
640 	usb_endpoint_descriptor_t 	*ed;
641 	struct mii_data 	*mii;
642 	u_char			eaddr[ETHER_ADDR_LEN];
643 	int			i,s;
644 
645 	sc->mos_udev = dev;
646 	sc->mos_unit = self->dv_unit;
647 
648 	err = usbd_set_config_no(dev, MOS_CONFIG_NO, 1);
649 	if (err) {
650 		printf("%s: getting interface handle failed\n",
651 		    sc->mos_dev.dv_xname);
652 		return;
653 	}
654 
655 	usb_init_task(&sc->mos_tick_task, mos_tick_task, sc,
656 	    USB_TASK_TYPE_GENERIC);
657 	rw_init(&sc->mos_mii_lock, "mosmii");
658 	usb_init_task(&sc->mos_stop_task, (void (*)(void *))mos_stop, sc,
659 	    USB_TASK_TYPE_GENERIC);
660 
661 	err = usbd_device2interface_handle(dev, MOS_IFACE_IDX, &sc->mos_iface);
662 	if (err) {
663 		printf("%s: getting interface handle failed\n",
664 		    sc->mos_dev.dv_xname);
665 		return;
666 	}
667 
668 	sc->mos_flags = mos_lookup(uaa->vendor, uaa->product)->mos_flags;
669 
670 	id = usbd_get_interface_descriptor(sc->mos_iface);
671 
672 	sc->mos_bufsz = MOS_BUFSZ;
673 
674 	/* Find endpoints. */
675 	for (i = 0; i < id->bNumEndpoints; i++) {
676 		ed = usbd_interface2endpoint_descriptor(sc->mos_iface, i);
677 		if (!ed) {
678 			printf("%s: couldn't get ep %d\n",
679 			    sc->mos_dev.dv_xname, i);
680 			return;
681 		}
682 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
683 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
684 			sc->mos_ed[MOS_ENDPT_RX] = ed->bEndpointAddress;
685 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
686 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
687 			sc->mos_ed[MOS_ENDPT_TX] = ed->bEndpointAddress;
688 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
689 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
690 			sc->mos_ed[MOS_ENDPT_INTR] = ed->bEndpointAddress;
691 		}
692 	}
693 
694 	s = splnet();
695 
696 	printf("%s:", sc->mos_dev.dv_xname);
697 
698 	if (sc->mos_flags & MCS7730)
699 		printf(" MCS7730");
700 	else if (sc->mos_flags & MCS7830)
701 		printf(" MCS7830");
702 	else if (sc->mos_flags & MCS7832)
703 		printf(" MCS7832");
704 
705 	mos_chip_init(sc);
706 
707 	/*
708 	 * Read MAC address, inform the world.
709 	 */
710 	err = mos_readmac(sc, (void*)&eaddr);
711 	if (err) {
712 		printf("%s: couldn't get MAC address\n",
713 		    sc->mos_dev.dv_xname);
714 		return;
715 	}
716 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
717 	printf(", address %s\n", ether_sprintf(eaddr));
718 
719 	/* Initialize interface info.*/
720 	ifp = GET_IFP(sc);
721 	ifp->if_softc = sc;
722 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
723 	ifp->if_ioctl = mos_ioctl;
724 	ifp->if_start = mos_start;
725 	ifp->if_watchdog = mos_watchdog;
726 	strlcpy(ifp->if_xname, sc->mos_dev.dv_xname, IFNAMSIZ);
727 
728 	IFQ_SET_READY(&ifp->if_snd);
729 
730 	ifp->if_capabilities = IFCAP_VLAN_MTU;
731 
732 	/* Initialize MII/media info. */
733 	mii = GET_MII(sc);
734 	mii->mii_ifp = ifp;
735 	mii->mii_readreg = mos_miibus_readreg;
736 	mii->mii_writereg = mos_miibus_writereg;
737 	mii->mii_statchg = mos_miibus_statchg;
738 	mii->mii_flags = MIIF_AUTOTSLEEP;
739 
740 	ifmedia_init(&mii->mii_media, 0, mos_ifmedia_upd, mos_ifmedia_sts);
741 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
742 
743 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
744 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
745 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
746 	} else
747 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
748 
749 	/* Attach the interface. */
750 	if_attach(ifp);
751 	ether_ifattach(ifp);
752 
753 	timeout_set(&sc->mos_stat_ch, mos_tick, sc);
754 
755 	splx(s);
756 }
757 
758 int
759 mos_detach(struct device *self, int flags)
760 {
761 	struct mos_softc	*sc = (struct mos_softc *)self;
762 	struct ifnet		*ifp = GET_IFP(sc);
763 	int			s;
764 
765 	DPRINTFN(2,("%s: %s: enter\n", sc->mos_dev.dv_xname, __func__));
766 
767 	if (timeout_initialized(&sc->mos_stat_ch))
768 		timeout_del(&sc->mos_stat_ch);
769 
770 	if (sc->mos_ep[MOS_ENDPT_TX] != NULL)
771 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_TX]);
772 	if (sc->mos_ep[MOS_ENDPT_RX] != NULL)
773 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_RX]);
774 	if (sc->mos_ep[MOS_ENDPT_INTR] != NULL)
775 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_INTR]);
776 
777 	/*
778 	 * Remove any pending tasks.  They cannot be executing because they run
779 	 * in the same thread as detach.
780 	 */
781 	usb_rem_task(sc->mos_udev, &sc->mos_tick_task);
782 	usb_rem_task(sc->mos_udev, &sc->mos_stop_task);
783 	s = splusb();
784 
785 	if (--sc->mos_refcnt >= 0) {
786 		/* Wait for processes to go away */
787 		usb_detach_wait(&sc->mos_dev);
788 	}
789 
790 	if (ifp->if_flags & IFF_RUNNING)
791 		mos_stop(sc);
792 
793 	mii_detach(&sc->mos_mii, MII_PHY_ANY, MII_OFFSET_ANY);
794 	ifmedia_delete_instance(&sc->mos_mii.mii_media, IFM_INST_ANY);
795 	if (ifp->if_softc != NULL) {
796 		ether_ifdetach(ifp);
797 		if_detach(ifp);
798 	}
799 
800 #ifdef DIAGNOSTIC
801 	if (sc->mos_ep[MOS_ENDPT_TX] != NULL ||
802 	    sc->mos_ep[MOS_ENDPT_RX] != NULL ||
803 	    sc->mos_ep[MOS_ENDPT_INTR] != NULL)
804 		printf("%s: detach has active endpoints\n",
805 		    sc->mos_dev.dv_xname);
806 #endif
807 
808 	if (--sc->mos_refcnt >= 0) {
809 		/* Wait for processes to go away. */
810 		usb_detach_wait(&sc->mos_dev);
811 	}
812 	splx(s);
813 
814 	return (0);
815 }
816 
817 
818 int
819 mos_activate(struct device *self, int act)
820 {
821 	struct mos_softc *sc = (struct mos_softc *)self;
822 
823 	DPRINTFN(2,("%s: %s: enter\n", sc->mos_dev.dv_xname, __func__));
824 
825 	switch (act) {
826 	case DVACT_DEACTIVATE:
827 		usbd_deactivate(sc->mos_udev);
828 		break;
829 	}
830 	return (0);
831 }
832 
833 struct mbuf *
834 mos_newbuf(void)
835 {
836 	struct mbuf		*m;
837 
838 	MGETHDR(m, M_DONTWAIT, MT_DATA);
839 	if (m == NULL)
840 		return (NULL);
841 
842 	MCLGET(m, M_DONTWAIT);
843 	if (!(m->m_flags & M_EXT)) {
844 		m_freem(m);
845 		return (NULL);
846 	}
847 
848 	m->m_len = m->m_pkthdr.len = MCLBYTES;
849 	m_adj(m, ETHER_ALIGN);
850 
851 	return (m);
852 }
853 
854 int
855 mos_rx_list_init(struct mos_softc *sc)
856 {
857 	struct mos_cdata	*cd;
858 	struct mos_chain	*c;
859 	int 			i;
860 
861 	DPRINTF(("%s: %s: enter\n", sc->mos_dev.dv_xname, __func__));
862 
863 	cd = &sc->mos_cdata;
864 	for (i = 0; i < MOS_RX_LIST_CNT; i++) {
865 		c = &cd->mos_rx_chain[i];
866 		c->mos_sc = sc;
867 		c->mos_idx = i;
868 		c->mos_mbuf = NULL;
869 		if (c->mos_xfer == NULL) {
870 			c->mos_xfer = usbd_alloc_xfer(sc->mos_udev);
871 			if (c->mos_xfer == NULL)
872 				return (ENOBUFS);
873 			c->mos_buf = usbd_alloc_buffer(c->mos_xfer,
874 			    sc->mos_bufsz);
875 			if (c->mos_buf == NULL) {
876 				usbd_free_xfer(c->mos_xfer);
877 				return (ENOBUFS);
878 			}
879 		}
880 	}
881 
882 	return (0);
883 }
884 
885 int
886 mos_tx_list_init(struct mos_softc *sc)
887 {
888 	struct mos_cdata	*cd;
889 	struct mos_chain	*c;
890 	int			i;
891 
892 	DPRINTF(("%s: %s: enter\n", sc->mos_dev.dv_xname, __func__));
893 
894 	cd = &sc->mos_cdata;
895 	for (i = 0; i < MOS_TX_LIST_CNT; i++) {
896 		c = &cd->mos_tx_chain[i];
897 		c->mos_sc = sc;
898 		c->mos_idx = i;
899 		c->mos_mbuf = NULL;
900 		if (c->mos_xfer == NULL) {
901 			c->mos_xfer = usbd_alloc_xfer(sc->mos_udev);
902 			if (c->mos_xfer == NULL)
903 				return (ENOBUFS);
904 			c->mos_buf = usbd_alloc_buffer(c->mos_xfer,
905 			    sc->mos_bufsz);
906 			if (c->mos_buf == NULL) {
907 				usbd_free_xfer(c->mos_xfer);
908 				return (ENOBUFS);
909 			}
910 		}
911 	}
912 
913 	return (0);
914 }
915 
916 /*
917  * A frame has been uploaded: pass the resulting mbuf chain up to
918  * the higher level protocols.
919  */
920 void
921 mos_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
922 {
923 	struct mos_chain	*c = (struct mos_chain *)priv;
924 	struct mos_softc	*sc = c->mos_sc;
925 	struct ifnet		*ifp = GET_IFP(sc);
926 	u_char			*buf = c->mos_buf;
927 	u_int8_t		rxstat;
928 	u_int32_t		total_len;
929 	u_int16_t		pktlen = 0;
930 	struct mbuf		*m;
931 	int			s;
932 
933 	DPRINTFN(10,("%s: %s: enter\n", sc->mos_dev.dv_xname,__func__));
934 
935 	if (usbd_is_dying(sc->mos_udev))
936 		return;
937 
938 	if (!(ifp->if_flags & IFF_RUNNING))
939 		return;
940 
941 	if (status != USBD_NORMAL_COMPLETION) {
942 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
943 			return;
944 		if (usbd_ratecheck(&sc->mos_rx_notice)) {
945 			printf("%s: usb errors on rx: %s\n",
946 			    sc->mos_dev.dv_xname, usbd_errstr(status));
947 		}
948 		if (status == USBD_STALLED)
949 			usbd_clear_endpoint_stall_async(sc->mos_ep[MOS_ENDPT_RX]);
950 		goto done;
951 	}
952 
953 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
954 
955 	if (total_len <= 1)
956 		goto done;
957 
958 	/* evaluate status byte at the end */
959 	pktlen = total_len - 1;
960 	rxstat = buf[pktlen] & MOS_RXSTS_MASK;
961 
962 	if (rxstat != MOS_RXSTS_VALID) {
963 		DPRINTF(("%s: erroneous frame received: ",
964 		    sc->mos_dev.dv_xname));
965 		if (rxstat & MOS_RXSTS_SHORT_FRAME)
966 			DPRINTF(("frame size less than 64 bytes\n"));
967 		if (rxstat & MOS_RXSTS_LARGE_FRAME)
968 			DPRINTF(("frame size larger than 1532 bytes\n"));
969 		if (rxstat & MOS_RXSTS_CRC_ERROR)
970 			DPRINTF(("CRC error\n"));
971 		if (rxstat & MOS_RXSTS_ALIGN_ERROR)
972 			DPRINTF(("alignment error\n"));
973 		ifp->if_ierrors++;
974 		goto done;
975 	}
976 
977 	if ( pktlen < sizeof(struct ether_header) ) {
978 		ifp->if_ierrors++;
979 		goto done;
980 	}
981 
982 	m = mos_newbuf();
983 	if (m == NULL) {
984 		ifp->if_ierrors++;
985 		goto done;
986 	}
987 
988 	ifp->if_ipackets++;
989 	m->m_pkthdr.rcvif = ifp;
990 	m->m_pkthdr.len = m->m_len = pktlen;
991 
992 	memcpy(mtod(m, char *), buf, pktlen);
993 
994 	/* push the packet up */
995 	s = splnet();
996 #if NBPFILTER > 0
997 	if (ifp->if_bpf)
998 		bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
999 #endif
1000 
1001 	ether_input_mbuf(ifp, m);
1002 
1003 	splx(s);
1004 
1005 done:
1006 	memset(c->mos_buf, 0, sc->mos_bufsz);
1007 
1008 	/* Setup new transfer. */
1009 	usbd_setup_xfer(xfer, sc->mos_ep[MOS_ENDPT_RX],
1010 	    c, c->mos_buf, sc->mos_bufsz,
1011 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1012 	    USBD_NO_TIMEOUT, mos_rxeof);
1013 	usbd_transfer(xfer);
1014 
1015 	DPRINTFN(10,("%s: %s: start rx\n", sc->mos_dev.dv_xname, __func__));
1016 
1017 	return;
1018 }
1019 
1020 /*
1021  * A frame was downloaded to the chip. It's safe for us to clean up
1022  * the list buffers.
1023  */
1024 
1025 void
1026 mos_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1027 {
1028 	struct mos_softc	*sc;
1029 	struct mos_chain	*c;
1030 	struct ifnet		*ifp;
1031 	int			s;
1032 
1033 	c = priv;
1034 	sc = c->mos_sc;
1035 	ifp = &sc->arpcom.ac_if;
1036 
1037 	if (usbd_is_dying(sc->mos_udev))
1038 		return;
1039 
1040 	s = splnet();
1041 
1042 	if (status != USBD_NORMAL_COMPLETION) {
1043 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1044 			splx(s);
1045 			return;
1046 		}
1047 		ifp->if_oerrors++;
1048 		printf("%s: usb error on tx: %s\n", sc->mos_dev.dv_xname,
1049 		    usbd_errstr(status));
1050 		if (status == USBD_STALLED)
1051 			usbd_clear_endpoint_stall_async(sc->mos_ep[MOS_ENDPT_TX]);
1052 		splx(s);
1053 		return;
1054 	}
1055 
1056 	ifp->if_timer = 0;
1057 	ifp->if_flags &= ~IFF_OACTIVE;
1058 
1059 	m_freem(c->mos_mbuf);
1060 	c->mos_mbuf = NULL;
1061 
1062 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1063 		mos_start(ifp);
1064 
1065 	ifp->if_opackets++;
1066 	splx(s);
1067 	return;
1068 }
1069 
1070 void
1071 mos_tick(void *xsc)
1072 {
1073 	struct mos_softc *sc = xsc;
1074 
1075 	if (sc == NULL)
1076 		return;
1077 
1078 	DPRINTFN(0xff, ("%s: %s: enter\n", sc->mos_dev.dv_xname,
1079 			__func__));
1080 
1081 	if (usbd_is_dying(sc->mos_udev))
1082 		return;
1083 
1084 	/* Perform periodic stuff in process context */
1085 	usb_add_task(sc->mos_udev, &sc->mos_tick_task);
1086 
1087 }
1088 
1089 void
1090 mos_tick_task(void *xsc)
1091 {
1092 	int			s;
1093 	struct mos_softc	*sc;
1094 	struct ifnet		*ifp;
1095 	struct mii_data		*mii;
1096 
1097 	sc = xsc;
1098 
1099 	if (sc == NULL)
1100 		return;
1101 
1102 	if (usbd_is_dying(sc->mos_udev))
1103 		return;
1104 
1105 	ifp = GET_IFP(sc);
1106 	mii = GET_MII(sc);
1107 	if (mii == NULL)
1108 		return;
1109 
1110 	s = splnet();
1111 
1112 	mii_tick(mii);
1113 	if (!sc->mos_link && mii->mii_media_status & IFM_ACTIVE &&
1114 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1115 		DPRINTF(("%s: %s: got link\n",
1116 			 sc->mos_dev.dv_xname, __func__));
1117 		sc->mos_link++;
1118 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1119 			mos_start(ifp);
1120 	}
1121 
1122 	timeout_add_sec(&sc->mos_stat_ch, 1);
1123 
1124 	splx(s);
1125 }
1126 
1127 int
1128 mos_encap(struct mos_softc *sc, struct mbuf *m, int idx)
1129 {
1130 	struct mos_chain	*c;
1131 	usbd_status		err;
1132 	int			length;
1133 
1134 	c = &sc->mos_cdata.mos_tx_chain[idx];
1135 
1136 	m_copydata(m, 0, m->m_pkthdr.len, c->mos_buf);
1137 	length = m->m_pkthdr.len;
1138 
1139 	c->mos_mbuf = m;
1140 
1141 	usbd_setup_xfer(c->mos_xfer, sc->mos_ep[MOS_ENDPT_TX],
1142 	    c, c->mos_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1143 	    10000, mos_txeof);
1144 
1145 	/* Transmit */
1146 	err = usbd_transfer(c->mos_xfer);
1147 	if (err != USBD_IN_PROGRESS) {
1148 		mos_stop(sc);
1149 		return(EIO);
1150 	}
1151 
1152 	sc->mos_cdata.mos_tx_cnt++;
1153 
1154 	return(0);
1155 }
1156 
1157 void
1158 mos_start(struct ifnet *ifp)
1159 {
1160 	struct mos_softc	*sc;
1161 	struct mbuf		*m_head = NULL;
1162 
1163 	sc = ifp->if_softc;
1164 
1165 	if (!sc->mos_link)
1166 		return;
1167 
1168 	if (ifp->if_flags & IFF_OACTIVE)
1169 		return;
1170 
1171 	IFQ_POLL(&ifp->if_snd, m_head);
1172 	if (m_head == NULL)
1173 		return;
1174 
1175 	if (mos_encap(sc, m_head, 0)) {
1176 		ifp->if_flags |= IFF_OACTIVE;
1177 		return;
1178 	}
1179 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1180 
1181 	/*
1182 	 * If there's a BPF listener, bounce a copy of this frame
1183 	 * to him.
1184 	 */
1185 #if NBPFILTER > 0
1186 	if (ifp->if_bpf)
1187 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1188 #endif
1189 
1190 	ifp->if_flags |= IFF_OACTIVE;
1191 
1192 	/*
1193 	 * Set a timeout in case the chip goes out to lunch.
1194 	 */
1195 	ifp->if_timer = 5;
1196 
1197 	return;
1198 }
1199 
1200 void
1201 mos_init(void *xsc)
1202 {
1203 	struct mos_softc	*sc = xsc;
1204 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1205 	struct mos_chain	*c;
1206 	usbd_status		err;
1207 	u_int8_t		rxmode;
1208 	int			i, s;
1209 
1210 	s = splnet();
1211 
1212 	/*
1213 	 * Cancel pending I/O and free all RX/TX buffers.
1214 	 */
1215 	mos_reset(sc);
1216 
1217 	/*
1218 	 * Write MAC address
1219 	 */
1220 	mos_writemac(sc, sc->arpcom.ac_enaddr);
1221 
1222 	/* Init RX ring. */
1223 	if (mos_rx_list_init(sc) == ENOBUFS) {
1224 		printf("%s: rx list init failed\n", sc->mos_dev.dv_xname);
1225 		splx(s);
1226 		return;
1227 	}
1228 
1229 	/* Init TX ring. */
1230 	if (mos_tx_list_init(sc) == ENOBUFS) {
1231 		printf("%s: tx list init failed\n", sc->mos_dev.dv_xname);
1232 		splx(s);
1233 		return;
1234 	}
1235 
1236 	/* Read and set transmitter IPG values */
1237 	sc->mos_ipgs[0] = mos_reg_read_1(sc, MOS_IPG0);
1238 	sc->mos_ipgs[1] = mos_reg_read_1(sc, MOS_IPG1);
1239 	mos_reg_write_1(sc, MOS_IPG0, sc->mos_ipgs[0]);
1240 	mos_reg_write_1(sc, MOS_IPG1, sc->mos_ipgs[1]);
1241 
1242 	/* Program promiscuous mode and multicast filters. */
1243 	mos_iff(sc);
1244 
1245 	/* Enable receiver and transmitter, bridge controls speed/duplex mode */
1246 	rxmode = mos_reg_read_1(sc, MOS_CTL);
1247 	rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
1248 	rxmode &= ~(MOS_CTL_SLEEP);
1249 	mos_reg_write_1(sc, MOS_CTL, rxmode);
1250 
1251 	mii_mediachg(GET_MII(sc));
1252 
1253 	/* Open RX and TX pipes. */
1254 	err = usbd_open_pipe(sc->mos_iface, sc->mos_ed[MOS_ENDPT_RX],
1255 	    USBD_EXCLUSIVE_USE, &sc->mos_ep[MOS_ENDPT_RX]);
1256 	if (err) {
1257 		printf("%s: open rx pipe failed: %s\n",
1258 		    sc->mos_dev.dv_xname, usbd_errstr(err));
1259 		splx(s);
1260 		return;
1261 	}
1262 
1263 	err = usbd_open_pipe(sc->mos_iface, sc->mos_ed[MOS_ENDPT_TX],
1264 	    USBD_EXCLUSIVE_USE, &sc->mos_ep[MOS_ENDPT_TX]);
1265 	if (err) {
1266 		printf("%s: open tx pipe failed: %s\n",
1267 		    sc->mos_dev.dv_xname, usbd_errstr(err));
1268 		splx(s);
1269 		return;
1270 	}
1271 
1272 	/* Start up the receive pipe. */
1273 	for (i = 0; i < MOS_RX_LIST_CNT; i++) {
1274 		c = &sc->mos_cdata.mos_rx_chain[i];
1275 		usbd_setup_xfer(c->mos_xfer, sc->mos_ep[MOS_ENDPT_RX],
1276 		    c, c->mos_buf, sc->mos_bufsz,
1277 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1278 		    USBD_NO_TIMEOUT, mos_rxeof);
1279 		usbd_transfer(c->mos_xfer);
1280 	}
1281 
1282 	ifp->if_flags |= IFF_RUNNING;
1283 	ifp->if_flags &= ~IFF_OACTIVE;
1284 
1285 	splx(s);
1286 
1287 	timeout_add_sec(&sc->mos_stat_ch, 1);
1288 	return;
1289 }
1290 
1291 int
1292 mos_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1293 {
1294 	struct mos_softc	*sc = ifp->if_softc;
1295 	struct ifreq		*ifr = (struct ifreq *)data;
1296 	struct ifaddr		*ifa = (struct ifaddr *)data;
1297 	int			s, error = 0;
1298 
1299 	s = splnet();
1300 
1301 	switch(cmd) {
1302 	case SIOCSIFADDR:
1303 		ifp->if_flags |= IFF_UP;
1304 		if (!(ifp->if_flags & IFF_RUNNING))
1305 			mos_init(sc);
1306 #ifdef INET
1307 		if (ifa->ifa_addr->sa_family == AF_INET)
1308 			arp_ifinit(&sc->arpcom, ifa);
1309 #endif
1310 		break;
1311 
1312 	case SIOCSIFFLAGS:
1313 		if (ifp->if_flags & IFF_UP) {
1314 			if (ifp->if_flags & IFF_RUNNING)
1315 				error = ENETRESET;
1316 			else
1317 				mos_init(sc);
1318 		} else {
1319 			if (ifp->if_flags & IFF_RUNNING)
1320 				mos_stop(sc);
1321 		}
1322 		break;
1323 
1324 	case SIOCGIFMEDIA:
1325 	case SIOCSIFMEDIA:
1326 		error = ifmedia_ioctl(ifp, ifr, &sc->mos_mii.mii_media, cmd);
1327 		break;
1328 
1329 	default:
1330 		error = ether_ioctl(ifp, &sc->arpcom, cmd, data);
1331 	}
1332 
1333 	if (error == ENETRESET) {
1334 		if (ifp->if_flags & IFF_RUNNING)
1335 			mos_iff(sc);
1336 		error = 0;
1337 	}
1338 
1339 	splx(s);
1340 	return(error);
1341 }
1342 
1343 void
1344 mos_watchdog(struct ifnet *ifp)
1345 {
1346 	struct mos_softc	*sc;
1347 	struct mos_chain	*c;
1348 	usbd_status		stat;
1349 	int			s;
1350 
1351 	sc = ifp->if_softc;
1352 
1353 	ifp->if_oerrors++;
1354 	printf("%s: watchdog timeout\n", sc->mos_dev.dv_xname);
1355 
1356 	s = splusb();
1357 	c = &sc->mos_cdata.mos_tx_chain[0];
1358 	usbd_get_xfer_status(c->mos_xfer, NULL, NULL, NULL, &stat);
1359 	mos_txeof(c->mos_xfer, c, stat);
1360 
1361 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
1362 		mos_start(ifp);
1363 	splx(s);
1364 }
1365 
1366 
1367 /*
1368  * Stop the adapter and free any mbufs allocated to the
1369  * RX and TX lists.
1370  */
1371 void
1372 mos_stop(struct mos_softc *sc)
1373 {
1374 	usbd_status		err;
1375 	struct ifnet		*ifp;
1376 	int			i;
1377 
1378 	mos_reset(sc);
1379 
1380 	ifp = &sc->arpcom.ac_if;
1381 	ifp->if_timer = 0;
1382 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1383 
1384 	timeout_del(&sc->mos_stat_ch);
1385 
1386 	/* Stop transfers. */
1387 	if (sc->mos_ep[MOS_ENDPT_RX] != NULL) {
1388 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_RX]);
1389 		err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_RX]);
1390 		if (err) {
1391 			printf("%s: close rx pipe failed: %s\n",
1392 			    sc->mos_dev.dv_xname, usbd_errstr(err));
1393 		}
1394 		sc->mos_ep[MOS_ENDPT_RX] = NULL;
1395 	}
1396 
1397 	if (sc->mos_ep[MOS_ENDPT_TX] != NULL) {
1398 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_TX]);
1399 		err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_TX]);
1400 		if (err) {
1401 			printf("%s: close tx pipe failed: %s\n",
1402 			    sc->mos_dev.dv_xname, usbd_errstr(err));
1403 		}
1404 		sc->mos_ep[MOS_ENDPT_TX] = NULL;
1405 	}
1406 
1407 	if (sc->mos_ep[MOS_ENDPT_INTR] != NULL) {
1408 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_INTR]);
1409 		err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_INTR]);
1410 		if (err) {
1411 			printf("%s: close intr pipe failed: %s\n",
1412 			    sc->mos_dev.dv_xname, usbd_errstr(err));
1413 		}
1414 		sc->mos_ep[MOS_ENDPT_INTR] = NULL;
1415 	}
1416 
1417 	/* Free RX resources. */
1418 	for (i = 0; i < MOS_RX_LIST_CNT; i++) {
1419 		if (sc->mos_cdata.mos_rx_chain[i].mos_mbuf != NULL) {
1420 			m_freem(sc->mos_cdata.mos_rx_chain[i].mos_mbuf);
1421 			sc->mos_cdata.mos_rx_chain[i].mos_mbuf = NULL;
1422 		}
1423 		if (sc->mos_cdata.mos_rx_chain[i].mos_xfer != NULL) {
1424 			usbd_free_xfer(sc->mos_cdata.mos_rx_chain[i].mos_xfer);
1425 			sc->mos_cdata.mos_rx_chain[i].mos_xfer = NULL;
1426 		}
1427 	}
1428 
1429 	/* Free TX resources. */
1430 	for (i = 0; i < MOS_TX_LIST_CNT; i++) {
1431 		if (sc->mos_cdata.mos_tx_chain[i].mos_mbuf != NULL) {
1432 			m_freem(sc->mos_cdata.mos_tx_chain[i].mos_mbuf);
1433 			sc->mos_cdata.mos_tx_chain[i].mos_mbuf = NULL;
1434 		}
1435 		if (sc->mos_cdata.mos_tx_chain[i].mos_xfer != NULL) {
1436 			usbd_free_xfer(sc->mos_cdata.mos_tx_chain[i].mos_xfer);
1437 			sc->mos_cdata.mos_tx_chain[i].mos_xfer = NULL;
1438 		}
1439 	}
1440 
1441 	sc->mos_link = 0;
1442 }
1443 
1444