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