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