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