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