xref: /openbsd-src/sys/dev/usb/if_mos.c (revision d66d8ee2788197b4ee18d763efd35443169bcdc7)
1 /*	$OpenBSD: if_mos.c,v 1.29 2015/04/10 08:41:43 mpi 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_list	ml = MBUF_LIST_INITIALIZER();
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.len = m->m_len = pktlen;
966 
967 	memcpy(mtod(m, char *), buf, pktlen);
968 
969 	ml_enqueue(&ml, m);
970 
971 	s = splnet();
972 	if_input(ifp, &ml);
973 	splx(s);
974 
975 done:
976 	memset(c->mos_buf, 0, sc->mos_bufsz);
977 
978 	/* Setup new transfer. */
979 	usbd_setup_xfer(xfer, sc->mos_ep[MOS_ENDPT_RX],
980 	    c, c->mos_buf, sc->mos_bufsz,
981 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
982 	    USBD_NO_TIMEOUT, mos_rxeof);
983 	usbd_transfer(xfer);
984 
985 	DPRINTFN(10,("%s: %s: start rx\n", sc->mos_dev.dv_xname, __func__));
986 
987 	return;
988 }
989 
990 /*
991  * A frame was downloaded to the chip. It's safe for us to clean up
992  * the list buffers.
993  */
994 
995 void
996 mos_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
997 {
998 	struct mos_softc	*sc;
999 	struct mos_chain	*c;
1000 	struct ifnet		*ifp;
1001 	int			s;
1002 
1003 	c = priv;
1004 	sc = c->mos_sc;
1005 	ifp = &sc->arpcom.ac_if;
1006 
1007 	if (usbd_is_dying(sc->mos_udev))
1008 		return;
1009 
1010 	s = splnet();
1011 
1012 	if (status != USBD_NORMAL_COMPLETION) {
1013 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1014 			splx(s);
1015 			return;
1016 		}
1017 		ifp->if_oerrors++;
1018 		printf("%s: usb error on tx: %s\n", sc->mos_dev.dv_xname,
1019 		    usbd_errstr(status));
1020 		if (status == USBD_STALLED)
1021 			usbd_clear_endpoint_stall_async(sc->mos_ep[MOS_ENDPT_TX]);
1022 		splx(s);
1023 		return;
1024 	}
1025 
1026 	ifp->if_timer = 0;
1027 	ifp->if_flags &= ~IFF_OACTIVE;
1028 
1029 	m_freem(c->mos_mbuf);
1030 	c->mos_mbuf = NULL;
1031 
1032 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1033 		mos_start(ifp);
1034 
1035 	ifp->if_opackets++;
1036 	splx(s);
1037 	return;
1038 }
1039 
1040 void
1041 mos_tick(void *xsc)
1042 {
1043 	struct mos_softc *sc = xsc;
1044 
1045 	if (sc == NULL)
1046 		return;
1047 
1048 	DPRINTFN(0xff, ("%s: %s: enter\n", sc->mos_dev.dv_xname,
1049 			__func__));
1050 
1051 	if (usbd_is_dying(sc->mos_udev))
1052 		return;
1053 
1054 	/* Perform periodic stuff in process context */
1055 	usb_add_task(sc->mos_udev, &sc->mos_tick_task);
1056 
1057 }
1058 
1059 void
1060 mos_tick_task(void *xsc)
1061 {
1062 	int			s;
1063 	struct mos_softc	*sc;
1064 	struct ifnet		*ifp;
1065 	struct mii_data		*mii;
1066 
1067 	sc = xsc;
1068 
1069 	if (sc == NULL)
1070 		return;
1071 
1072 	if (usbd_is_dying(sc->mos_udev))
1073 		return;
1074 
1075 	ifp = GET_IFP(sc);
1076 	mii = GET_MII(sc);
1077 	if (mii == NULL)
1078 		return;
1079 
1080 	s = splnet();
1081 
1082 	mii_tick(mii);
1083 	if (!sc->mos_link && mii->mii_media_status & IFM_ACTIVE &&
1084 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1085 		DPRINTF(("%s: %s: got link\n",
1086 			 sc->mos_dev.dv_xname, __func__));
1087 		sc->mos_link++;
1088 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1089 			mos_start(ifp);
1090 	}
1091 
1092 	timeout_add_sec(&sc->mos_stat_ch, 1);
1093 
1094 	splx(s);
1095 }
1096 
1097 int
1098 mos_encap(struct mos_softc *sc, struct mbuf *m, int idx)
1099 {
1100 	struct mos_chain	*c;
1101 	usbd_status		err;
1102 	int			length;
1103 
1104 	c = &sc->mos_cdata.mos_tx_chain[idx];
1105 
1106 	m_copydata(m, 0, m->m_pkthdr.len, c->mos_buf);
1107 	length = m->m_pkthdr.len;
1108 
1109 	c->mos_mbuf = m;
1110 
1111 	usbd_setup_xfer(c->mos_xfer, sc->mos_ep[MOS_ENDPT_TX],
1112 	    c, c->mos_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1113 	    10000, mos_txeof);
1114 
1115 	/* Transmit */
1116 	err = usbd_transfer(c->mos_xfer);
1117 	if (err != USBD_IN_PROGRESS) {
1118 		mos_stop(sc);
1119 		return(EIO);
1120 	}
1121 
1122 	sc->mos_cdata.mos_tx_cnt++;
1123 
1124 	return(0);
1125 }
1126 
1127 void
1128 mos_start(struct ifnet *ifp)
1129 {
1130 	struct mos_softc	*sc;
1131 	struct mbuf		*m_head = NULL;
1132 
1133 	sc = ifp->if_softc;
1134 
1135 	if (!sc->mos_link)
1136 		return;
1137 
1138 	if (ifp->if_flags & IFF_OACTIVE)
1139 		return;
1140 
1141 	IFQ_POLL(&ifp->if_snd, m_head);
1142 	if (m_head == NULL)
1143 		return;
1144 
1145 	if (mos_encap(sc, m_head, 0)) {
1146 		ifp->if_flags |= IFF_OACTIVE;
1147 		return;
1148 	}
1149 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1150 
1151 	/*
1152 	 * If there's a BPF listener, bounce a copy of this frame
1153 	 * to him.
1154 	 */
1155 #if NBPFILTER > 0
1156 	if (ifp->if_bpf)
1157 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1158 #endif
1159 
1160 	ifp->if_flags |= IFF_OACTIVE;
1161 
1162 	/*
1163 	 * Set a timeout in case the chip goes out to lunch.
1164 	 */
1165 	ifp->if_timer = 5;
1166 
1167 	return;
1168 }
1169 
1170 void
1171 mos_init(void *xsc)
1172 {
1173 	struct mos_softc	*sc = xsc;
1174 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1175 	struct mos_chain	*c;
1176 	usbd_status		err;
1177 	u_int8_t		rxmode;
1178 	int			i, s;
1179 
1180 	s = splnet();
1181 
1182 	/*
1183 	 * Cancel pending I/O and free all RX/TX buffers.
1184 	 */
1185 	mos_reset(sc);
1186 
1187 	/*
1188 	 * Write MAC address
1189 	 */
1190 	mos_writemac(sc, sc->arpcom.ac_enaddr);
1191 
1192 	/* Init RX ring. */
1193 	if (mos_rx_list_init(sc) == ENOBUFS) {
1194 		printf("%s: rx list init failed\n", sc->mos_dev.dv_xname);
1195 		splx(s);
1196 		return;
1197 	}
1198 
1199 	/* Init TX ring. */
1200 	if (mos_tx_list_init(sc) == ENOBUFS) {
1201 		printf("%s: tx list init failed\n", sc->mos_dev.dv_xname);
1202 		splx(s);
1203 		return;
1204 	}
1205 
1206 	/* Read and set transmitter IPG values */
1207 	sc->mos_ipgs[0] = mos_reg_read_1(sc, MOS_IPG0);
1208 	sc->mos_ipgs[1] = mos_reg_read_1(sc, MOS_IPG1);
1209 	mos_reg_write_1(sc, MOS_IPG0, sc->mos_ipgs[0]);
1210 	mos_reg_write_1(sc, MOS_IPG1, sc->mos_ipgs[1]);
1211 
1212 	/* Program promiscuous mode and multicast filters. */
1213 	mos_iff(sc);
1214 
1215 	/* Enable receiver and transmitter, bridge controls speed/duplex mode */
1216 	rxmode = mos_reg_read_1(sc, MOS_CTL);
1217 	rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
1218 	rxmode &= ~(MOS_CTL_SLEEP);
1219 	mos_reg_write_1(sc, MOS_CTL, rxmode);
1220 
1221 	mii_mediachg(GET_MII(sc));
1222 
1223 	/* Open RX and TX pipes. */
1224 	err = usbd_open_pipe(sc->mos_iface, sc->mos_ed[MOS_ENDPT_RX],
1225 	    USBD_EXCLUSIVE_USE, &sc->mos_ep[MOS_ENDPT_RX]);
1226 	if (err) {
1227 		printf("%s: open rx pipe failed: %s\n",
1228 		    sc->mos_dev.dv_xname, usbd_errstr(err));
1229 		splx(s);
1230 		return;
1231 	}
1232 
1233 	err = usbd_open_pipe(sc->mos_iface, sc->mos_ed[MOS_ENDPT_TX],
1234 	    USBD_EXCLUSIVE_USE, &sc->mos_ep[MOS_ENDPT_TX]);
1235 	if (err) {
1236 		printf("%s: open tx pipe failed: %s\n",
1237 		    sc->mos_dev.dv_xname, usbd_errstr(err));
1238 		splx(s);
1239 		return;
1240 	}
1241 
1242 	/* Start up the receive pipe. */
1243 	for (i = 0; i < MOS_RX_LIST_CNT; i++) {
1244 		c = &sc->mos_cdata.mos_rx_chain[i];
1245 		usbd_setup_xfer(c->mos_xfer, sc->mos_ep[MOS_ENDPT_RX],
1246 		    c, c->mos_buf, sc->mos_bufsz,
1247 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1248 		    USBD_NO_TIMEOUT, mos_rxeof);
1249 		usbd_transfer(c->mos_xfer);
1250 	}
1251 
1252 	ifp->if_flags |= IFF_RUNNING;
1253 	ifp->if_flags &= ~IFF_OACTIVE;
1254 
1255 	splx(s);
1256 
1257 	timeout_add_sec(&sc->mos_stat_ch, 1);
1258 	return;
1259 }
1260 
1261 int
1262 mos_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1263 {
1264 	struct mos_softc	*sc = ifp->if_softc;
1265 	struct ifreq		*ifr = (struct ifreq *)data;
1266 	struct ifaddr		*ifa = (struct ifaddr *)data;
1267 	int			s, error = 0;
1268 
1269 	s = splnet();
1270 
1271 	switch(cmd) {
1272 	case SIOCSIFADDR:
1273 		ifp->if_flags |= IFF_UP;
1274 		if (!(ifp->if_flags & IFF_RUNNING))
1275 			mos_init(sc);
1276 		if (ifa->ifa_addr->sa_family == AF_INET)
1277 			arp_ifinit(&sc->arpcom, ifa);
1278 		break;
1279 
1280 	case SIOCSIFFLAGS:
1281 		if (ifp->if_flags & IFF_UP) {
1282 			if (ifp->if_flags & IFF_RUNNING)
1283 				error = ENETRESET;
1284 			else
1285 				mos_init(sc);
1286 		} else {
1287 			if (ifp->if_flags & IFF_RUNNING)
1288 				mos_stop(sc);
1289 		}
1290 		break;
1291 
1292 	case SIOCGIFMEDIA:
1293 	case SIOCSIFMEDIA:
1294 		error = ifmedia_ioctl(ifp, ifr, &sc->mos_mii.mii_media, cmd);
1295 		break;
1296 
1297 	default:
1298 		error = ether_ioctl(ifp, &sc->arpcom, cmd, data);
1299 	}
1300 
1301 	if (error == ENETRESET) {
1302 		if (ifp->if_flags & IFF_RUNNING)
1303 			mos_iff(sc);
1304 		error = 0;
1305 	}
1306 
1307 	splx(s);
1308 	return(error);
1309 }
1310 
1311 void
1312 mos_watchdog(struct ifnet *ifp)
1313 {
1314 	struct mos_softc	*sc;
1315 	struct mos_chain	*c;
1316 	usbd_status		stat;
1317 	int			s;
1318 
1319 	sc = ifp->if_softc;
1320 
1321 	ifp->if_oerrors++;
1322 	printf("%s: watchdog timeout\n", sc->mos_dev.dv_xname);
1323 
1324 	s = splusb();
1325 	c = &sc->mos_cdata.mos_tx_chain[0];
1326 	usbd_get_xfer_status(c->mos_xfer, NULL, NULL, NULL, &stat);
1327 	mos_txeof(c->mos_xfer, c, stat);
1328 
1329 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
1330 		mos_start(ifp);
1331 	splx(s);
1332 }
1333 
1334 
1335 /*
1336  * Stop the adapter and free any mbufs allocated to the
1337  * RX and TX lists.
1338  */
1339 void
1340 mos_stop(struct mos_softc *sc)
1341 {
1342 	usbd_status		err;
1343 	struct ifnet		*ifp;
1344 	int			i;
1345 
1346 	mos_reset(sc);
1347 
1348 	ifp = &sc->arpcom.ac_if;
1349 	ifp->if_timer = 0;
1350 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1351 
1352 	timeout_del(&sc->mos_stat_ch);
1353 
1354 	/* Stop transfers. */
1355 	if (sc->mos_ep[MOS_ENDPT_RX] != NULL) {
1356 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_RX]);
1357 		err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_RX]);
1358 		if (err) {
1359 			printf("%s: close rx pipe failed: %s\n",
1360 			    sc->mos_dev.dv_xname, usbd_errstr(err));
1361 		}
1362 		sc->mos_ep[MOS_ENDPT_RX] = NULL;
1363 	}
1364 
1365 	if (sc->mos_ep[MOS_ENDPT_TX] != NULL) {
1366 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_TX]);
1367 		err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_TX]);
1368 		if (err) {
1369 			printf("%s: close tx pipe failed: %s\n",
1370 			    sc->mos_dev.dv_xname, usbd_errstr(err));
1371 		}
1372 		sc->mos_ep[MOS_ENDPT_TX] = NULL;
1373 	}
1374 
1375 	if (sc->mos_ep[MOS_ENDPT_INTR] != NULL) {
1376 		usbd_abort_pipe(sc->mos_ep[MOS_ENDPT_INTR]);
1377 		err = usbd_close_pipe(sc->mos_ep[MOS_ENDPT_INTR]);
1378 		if (err) {
1379 			printf("%s: close intr pipe failed: %s\n",
1380 			    sc->mos_dev.dv_xname, usbd_errstr(err));
1381 		}
1382 		sc->mos_ep[MOS_ENDPT_INTR] = NULL;
1383 	}
1384 
1385 	/* Free RX resources. */
1386 	for (i = 0; i < MOS_RX_LIST_CNT; i++) {
1387 		if (sc->mos_cdata.mos_rx_chain[i].mos_mbuf != NULL) {
1388 			m_freem(sc->mos_cdata.mos_rx_chain[i].mos_mbuf);
1389 			sc->mos_cdata.mos_rx_chain[i].mos_mbuf = NULL;
1390 		}
1391 		if (sc->mos_cdata.mos_rx_chain[i].mos_xfer != NULL) {
1392 			usbd_free_xfer(sc->mos_cdata.mos_rx_chain[i].mos_xfer);
1393 			sc->mos_cdata.mos_rx_chain[i].mos_xfer = NULL;
1394 		}
1395 	}
1396 
1397 	/* Free TX resources. */
1398 	for (i = 0; i < MOS_TX_LIST_CNT; i++) {
1399 		if (sc->mos_cdata.mos_tx_chain[i].mos_mbuf != NULL) {
1400 			m_freem(sc->mos_cdata.mos_tx_chain[i].mos_mbuf);
1401 			sc->mos_cdata.mos_tx_chain[i].mos_mbuf = NULL;
1402 		}
1403 		if (sc->mos_cdata.mos_tx_chain[i].mos_xfer != NULL) {
1404 			usbd_free_xfer(sc->mos_cdata.mos_tx_chain[i].mos_xfer);
1405 			sc->mos_cdata.mos_tx_chain[i].mos_xfer = NULL;
1406 		}
1407 	}
1408 
1409 	sc->mos_link = 0;
1410 }
1411 
1412