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