xref: /freebsd/sys/dev/usb/net/if_mos.c (revision ddd5b8e9b4d8957fce018c520657cdfa4ecffad3)
1 /*-
2  * Copyright (c) 2011 Rick van der Zwet <info@rickvanderzwet.nl>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /*-
18  * Copyright (c) 2008 Johann Christian Rode <jcrode@gmx.net>
19  *
20  * Permission to use, copy, modify, and distribute this software for any
21  * purpose with or without fee is hereby granted, provided that the above
22  * copyright notice and this permission notice appear in all copies.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
25  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
27  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
28  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
29  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31  */
32 
33 /*-
34  * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg@openbsd.org>
35  *
36  * Permission to use, copy, modify, and distribute this software for any
37  * purpose with or without fee is hereby granted, provided that the above
38  * copyright notice and this permission notice appear in all copies.
39  *
40  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
46  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47  */
48 
49 /*-
50  * Copyright (c) 1997, 1998, 1999, 2000-2003
51  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
52  *
53  * Redistribution and use in source and binary forms, with or without
54  * modification, are permitted provided that the following conditions
55  * are met:
56  * 1. Redistributions of source code must retain the above copyright
57  *    notice, this list of conditions and the following disclaimer.
58  * 2. Redistributions in binary form must reproduce the above copyright
59  *    notice, this list of conditions and the following disclaimer in the
60  *    documentation and/or other materials provided with the distribution.
61  * 3. All advertising materials mentioning features or use of this software
62  *    must display the following acknowledgement:
63  *	This product includes software developed by Bill Paul.
64  * 4. Neither the name of the author nor the names of any co-contributors
65  *    may be used to endorse or promote products derived from this software
66  *    without specific prior written permission.
67  *
68  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
69  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
72  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
73  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
74  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
75  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
76  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
77  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
78  * THE POSSIBILITY OF SUCH DAMAGE.
79  */
80 
81 #include <sys/cdefs.h>
82 __FBSDID("$FreeBSD$");
83 
84 /*
85  * Moschip MCS7730/MCS7830/MCS7832 USB to Ethernet controller
86  * The datasheet is available at the following URL:
87  * http://www.moschip.com/data/products/MCS7830/Data%20Sheet_7830.pdf
88  */
89 
90 /*
91  * The FreeBSD if_mos.c driver is based on various different sources:
92  * The vendor provided driver at the following URL:
93  * http://www.moschip.com/data/products/MCS7830/Driver_FreeBSD_7830.tar.gz
94  *
95  * Mixed together with the OpenBSD if_mos.c driver for validation and checking
96  * and the FreeBSD if_reu.c as reference for the USB Ethernet framework.
97  */
98 
99 #include <sys/stdint.h>
100 #include <sys/stddef.h>
101 #include <sys/param.h>
102 #include <sys/queue.h>
103 #include <sys/types.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/bus.h>
107 #include <sys/module.h>
108 #include <sys/lock.h>
109 #include <sys/mutex.h>
110 #include <sys/condvar.h>
111 #include <sys/sysctl.h>
112 #include <sys/sx.h>
113 #include <sys/unistd.h>
114 #include <sys/callout.h>
115 #include <sys/malloc.h>
116 #include <sys/priv.h>
117 
118 #include <dev/usb/usb.h>
119 #include <dev/usb/usbdi.h>
120 #include <dev/usb/usbdi_util.h>
121 #include "usbdevs.h"
122 
123 #define	USB_DEBUG_VAR mos_debug
124 #include <dev/usb/usb_debug.h>
125 #include <dev/usb/usb_process.h>
126 
127 #include <dev/usb/net/usb_ethernet.h>
128 
129 //#include <dev/usb/net/if_mosreg.h>
130 #include "if_mosreg.h"
131 
132 #ifdef USB_DEBUG
133 static int mos_debug = 0;
134 
135 static SYSCTL_NODE(_hw_usb, OID_AUTO, mos, CTLFLAG_RW, 0, "USB mos");
136 SYSCTL_INT(_hw_usb_mos, OID_AUTO, debug, CTLFLAG_RW, &mos_debug, 0,
137     "Debug level");
138 #endif
139 
140 #define MOS_DPRINTFN(fmt,...) \
141   DPRINTF("mos: %s: " fmt "\n",__FUNCTION__,## __VA_ARGS__)
142 
143 #define	USB_PRODUCT_MOSCHIP_MCS7730	0x7730
144 #define	USB_PRODUCT_SITECOMEU_LN030	0x0021
145 
146 
147 
148 /* Various supported device vendors/products. */
149 static const STRUCT_USB_HOST_ID mos_devs[] = {
150 	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7730, MCS7730)},
151 	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7830, MCS7830)},
152 	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7832, MCS7832)},
153 	{USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN030, MCS7830)},
154 };
155 
156 static int mos_probe(device_t dev);
157 static int mos_attach(device_t dev);
158 static void mos_attach_post(struct usb_ether *ue);
159 static int mos_detach(device_t dev);
160 
161 static void mos_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error);
162 static void mos_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error);
163 static void mos_intr_callback(struct usb_xfer *xfer, usb_error_t error);
164 static void mos_tick(struct usb_ether *);
165 static void mos_start(struct usb_ether *);
166 static void mos_init(struct usb_ether *);
167 static void mos_chip_init(struct mos_softc *);
168 static void mos_stop(struct usb_ether *);
169 static int mos_miibus_readreg(device_t, int, int);
170 static int mos_miibus_writereg(device_t, int, int, int);
171 static void mos_miibus_statchg(device_t);
172 static int mos_ifmedia_upd(struct ifnet *);
173 static void mos_ifmedia_sts(struct ifnet *, struct ifmediareq *);
174 static void mos_reset(struct mos_softc *sc);
175 
176 static int mos_reg_read_1(struct mos_softc *, int);
177 static int mos_reg_read_2(struct mos_softc *, int);
178 static int mos_reg_write_1(struct mos_softc *, int, int);
179 static int mos_reg_write_2(struct mos_softc *, int, int);
180 static int mos_readmac(struct mos_softc *, uint8_t *);
181 static int mos_writemac(struct mos_softc *, uint8_t *);
182 static int mos_write_mcast(struct mos_softc *, u_char *);
183 
184 static void mos_setmulti(struct usb_ether *);
185 static void mos_setpromisc(struct usb_ether *);
186 
187 static const struct usb_config mos_config[MOS_ENDPT_MAX] = {
188 
189 	[MOS_ENDPT_TX] = {
190 		.type = UE_BULK,
191 		.endpoint = UE_ADDR_ANY,
192 		.direction = UE_DIR_OUT,
193 		.bufsize = (MCLBYTES + 2),
194 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
195 		.callback = mos_bulk_write_callback,
196 		.timeout = 10000,
197 	},
198 
199 	[MOS_ENDPT_RX] = {
200 		.type = UE_BULK,
201 		.endpoint = UE_ADDR_ANY,
202 		.direction = UE_DIR_IN,
203 		.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
204 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
205 		.callback = mos_bulk_read_callback,
206 	},
207 
208 	[MOS_ENDPT_INTR] = {
209 		.type = UE_INTERRUPT,
210 		.endpoint = UE_ADDR_ANY,
211 		.direction = UE_DIR_IN,
212 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
213 		.bufsize = 0,
214 		.callback = mos_intr_callback,
215 	},
216 };
217 
218 static device_method_t mos_methods[] = {
219 	/* Device interface */
220 	DEVMETHOD(device_probe, mos_probe),
221 	DEVMETHOD(device_attach, mos_attach),
222 	DEVMETHOD(device_detach, mos_detach),
223 
224 	/* MII interface */
225 	DEVMETHOD(miibus_readreg, mos_miibus_readreg),
226 	DEVMETHOD(miibus_writereg, mos_miibus_writereg),
227 	DEVMETHOD(miibus_statchg, mos_miibus_statchg),
228 
229 	DEVMETHOD_END
230 };
231 
232 static driver_t mos_driver = {
233 	.name = "mos",
234 	.methods = mos_methods,
235 	.size = sizeof(struct mos_softc)
236 };
237 
238 static devclass_t mos_devclass;
239 
240 DRIVER_MODULE(mos, uhub, mos_driver, mos_devclass, NULL, 0);
241 DRIVER_MODULE(miibus, mos, miibus_driver, miibus_devclass, 0, 0);
242 MODULE_DEPEND(mos, uether, 1, 1, 1);
243 MODULE_DEPEND(mos, usb, 1, 1, 1);
244 MODULE_DEPEND(mos, ether, 1, 1, 1);
245 MODULE_DEPEND(mos, miibus, 1, 1, 1);
246 
247 static const struct usb_ether_methods mos_ue_methods = {
248 	.ue_attach_post = mos_attach_post,
249 	.ue_start = mos_start,
250 	.ue_init = mos_init,
251 	.ue_stop = mos_stop,
252 	.ue_tick = mos_tick,
253 	.ue_setmulti = mos_setmulti,
254 	.ue_setpromisc = mos_setpromisc,
255 	.ue_mii_upd = mos_ifmedia_upd,
256 	.ue_mii_sts = mos_ifmedia_sts,
257 };
258 
259 
260 static int
261 mos_reg_read_1(struct mos_softc *sc, int reg)
262 {
263 	struct usb_device_request req;
264 	usb_error_t err;
265 	uByte val = 0;
266 
267 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
268 	req.bRequest = MOS_UR_READREG;
269 	USETW(req.wValue, 0);
270 	USETW(req.wIndex, reg);
271 	USETW(req.wLength, 1);
272 
273 	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
274 
275 	if (err) {
276 		MOS_DPRINTFN("mos_reg_read_1 error, reg: %d\n", reg);
277 		return (-1);
278 	}
279 	return (val);
280 }
281 
282 static int
283 mos_reg_read_2(struct mos_softc *sc, int reg)
284 {
285 	struct usb_device_request req;
286 	usb_error_t err;
287 	uWord val;
288 
289 	USETW(val, 0);
290 
291 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
292 	req.bRequest = MOS_UR_READREG;
293 	USETW(req.wValue, 0);
294 	USETW(req.wIndex, reg);
295 	USETW(req.wLength, 2);
296 
297 	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
298 
299 	if (err) {
300 		MOS_DPRINTFN("mos_reg_read_2 error, reg: %d", reg);
301 		return (-1);
302 	}
303 	return (UGETW(val));
304 }
305 
306 static int
307 mos_reg_write_1(struct mos_softc *sc, int reg, int aval)
308 {
309 	struct usb_device_request req;
310 	usb_error_t err;
311 	uByte val;
312 	val = aval;
313 
314 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
315 	req.bRequest = MOS_UR_WRITEREG;
316 	USETW(req.wValue, 0);
317 	USETW(req.wIndex, reg);
318 	USETW(req.wLength, 1);
319 
320 	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
321 
322 	if (err) {
323 		MOS_DPRINTFN("mos_reg_write_1 error, reg: %d", reg);
324 		return (-1);
325 	}
326 	return (0);
327 }
328 
329 static int
330 mos_reg_write_2(struct mos_softc *sc, int reg, int aval)
331 {
332 	struct usb_device_request req;
333 	usb_error_t err;
334 	uWord val;
335 
336 	USETW(val, aval);
337 
338 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
339 	req.bRequest = MOS_UR_WRITEREG;
340 	USETW(req.wValue, 0);
341 	USETW(req.wIndex, reg);
342 	USETW(req.wLength, 2);
343 
344 	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
345 
346 	if (err) {
347 		MOS_DPRINTFN("mos_reg_write_2 error, reg: %d", reg);
348 		return (-1);
349 	}
350 	return (0);
351 }
352 
353 static int
354 mos_readmac(struct mos_softc *sc, u_char *mac)
355 {
356 	struct usb_device_request req;
357 	usb_error_t err;
358 
359 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
360 	req.bRequest = MOS_UR_READREG;
361 	USETW(req.wValue, 0);
362 	USETW(req.wIndex, MOS_MAC);
363 	USETW(req.wLength, ETHER_ADDR_LEN);
364 
365 	err = uether_do_request(&sc->sc_ue, &req, mac, 1000);
366 
367 	if (err) {
368 		return (-1);
369 	}
370 	return (0);
371 }
372 
373 static int
374 mos_writemac(struct mos_softc *sc, uint8_t *mac)
375 {
376 	struct usb_device_request req;
377 	usb_error_t err;
378 
379 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
380 	req.bRequest = MOS_UR_WRITEREG;
381 	USETW(req.wValue, 0);
382 	USETW(req.wIndex, MOS_MAC);
383 	USETW(req.wLength, ETHER_ADDR_LEN);
384 
385 	err = uether_do_request(&sc->sc_ue, &req, mac, 1000);
386 
387 	if (err) {
388 		MOS_DPRINTFN("mos_writemac error");
389 		return (-1);
390 	}
391 	return (0);
392 }
393 
394 static int
395 mos_write_mcast(struct mos_softc *sc, u_char *hashtbl)
396 {
397 	struct usb_device_request req;
398 	usb_error_t err;
399 
400 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
401 	req.bRequest = MOS_UR_WRITEREG;
402 	USETW(req.wValue, 0);
403 	USETW(req.wIndex, MOS_MCAST_TABLE);
404 	USETW(req.wLength, 8);
405 
406 	err = uether_do_request(&sc->sc_ue, &req, hashtbl, 1000);
407 
408 	if (err) {
409 		MOS_DPRINTFN("mos_reg_mcast error");
410 		return (-1);
411 	}
412 	return (0);
413 }
414 
415 static int
416 mos_miibus_readreg(struct device *dev, int phy, int reg)
417 {
418 	struct mos_softc *sc = device_get_softc(dev);
419 	uWord val;
420 	int i, res, locked;
421 
422 	USETW(val, 0);
423 
424 	locked = mtx_owned(&sc->sc_mtx);
425 	if (!locked)
426 		MOS_LOCK(sc);
427 
428 	mos_reg_write_2(sc, MOS_PHY_DATA, 0);
429 	mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
430 	    MOS_PHYCTL_READ);
431 	mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
432 	    MOS_PHYSTS_PENDING);
433 
434 	for (i = 0; i < MOS_TIMEOUT; i++) {
435 		if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY)
436 			break;
437 	}
438 	if (i == MOS_TIMEOUT) {
439 		MOS_DPRINTFN("MII read timeout");
440 	}
441 	res = mos_reg_read_2(sc, MOS_PHY_DATA);
442 
443 	if (!locked)
444 		MOS_UNLOCK(sc);
445 	return (res);
446 }
447 
448 static int
449 mos_miibus_writereg(device_t dev, int phy, int reg, int val)
450 {
451 	struct mos_softc *sc = device_get_softc(dev);
452 	int i, locked;
453 
454 	locked = mtx_owned(&sc->sc_mtx);
455 	if (!locked)
456 		MOS_LOCK(sc);
457 
458 	mos_reg_write_2(sc, MOS_PHY_DATA, val);
459 	mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
460 	    MOS_PHYCTL_WRITE);
461 	mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
462 	    MOS_PHYSTS_PENDING);
463 
464 	for (i = 0; i < MOS_TIMEOUT; i++) {
465 		if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY)
466 			break;
467 	}
468 	if (i == MOS_TIMEOUT)
469 		MOS_DPRINTFN("MII write timeout");
470 
471 	if (!locked)
472 		MOS_UNLOCK(sc);
473 	return 0;
474 }
475 
476 static void
477 mos_miibus_statchg(device_t dev)
478 {
479 	struct mos_softc *sc = device_get_softc(dev);
480 	struct mii_data *mii = GET_MII(sc);
481 	int val, err, locked;
482 
483 	locked = mtx_owned(&sc->sc_mtx);
484 	if (!locked)
485 		MOS_LOCK(sc);
486 
487 	/* disable RX, TX prior to changing FDX, SPEEDSEL */
488 	val = mos_reg_read_1(sc, MOS_CTL);
489 	val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
490 	mos_reg_write_1(sc, MOS_CTL, val);
491 
492 	/* reset register which counts dropped frames */
493 	mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
494 
495 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
496 		val |= MOS_CTL_FDX_ENB;
497 	else
498 		val &= ~(MOS_CTL_FDX_ENB);
499 
500 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
501 	case IFM_100_TX:
502 		val |= MOS_CTL_SPEEDSEL;
503 		break;
504 	case IFM_10_T:
505 		val &= ~(MOS_CTL_SPEEDSEL);
506 		break;
507 	}
508 
509 	/* re-enable TX, RX */
510 	val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
511 	err = mos_reg_write_1(sc, MOS_CTL, val);
512 
513 	if (err)
514 		MOS_DPRINTFN("media change failed");
515 
516 	if (!locked)
517 		MOS_UNLOCK(sc);
518 }
519 
520 /*
521  * Set media options.
522  */
523 static int
524 mos_ifmedia_upd(struct ifnet *ifp)
525 {
526 	struct mos_softc *sc = ifp->if_softc;
527 	struct mii_data *mii = GET_MII(sc);
528 	struct mii_softc *miisc;
529 
530 	MOS_LOCK_ASSERT(sc, MA_OWNED);
531 
532 	sc->mos_link = 0;
533 	if (mii->mii_instance) {
534 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
535 		    mii_phy_reset(miisc);
536 	}
537 	mii_mediachg(mii);
538 	return (0);
539 }
540 
541 /*
542  * Report current media status.
543  */
544 static void
545 mos_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
546 {
547 	struct mos_softc *sc = ifp->if_softc;
548 	struct mii_data *mii = GET_MII(sc);
549 
550 	MOS_LOCK(sc);
551 	mii_pollstat(mii);
552 
553 	ifmr->ifm_active = mii->mii_media_active;
554 	ifmr->ifm_status = mii->mii_media_status;
555 	MOS_UNLOCK(sc);
556 }
557 
558 static void
559 mos_setpromisc(struct usb_ether *ue)
560 {
561 	struct mos_softc *sc = uether_getsc(ue);
562 	struct ifnet *ifp = uether_getifp(ue);
563 
564 	uint8_t rxmode;
565 
566 	MOS_LOCK_ASSERT(sc, MA_OWNED);
567 
568 	rxmode = mos_reg_read_1(sc, MOS_CTL);
569 
570 	/* If we want promiscuous mode, set the allframes bit. */
571 	if (ifp->if_flags & IFF_PROMISC) {
572 		rxmode |= MOS_CTL_RX_PROMISC;
573 	} else {
574 		rxmode &= ~MOS_CTL_RX_PROMISC;
575 	}
576 
577 	mos_reg_write_1(sc, MOS_CTL, rxmode);
578 }
579 
580 
581 
582 static void
583 mos_setmulti(struct usb_ether *ue)
584 {
585 	struct mos_softc *sc = uether_getsc(ue);
586 	struct ifnet *ifp = uether_getifp(ue);
587 	struct ifmultiaddr *ifma;
588 
589 	uint32_t h = 0;
590 	uint8_t rxmode;
591 	uint8_t hashtbl[8] = {0, 0, 0, 0, 0, 0, 0, 0};
592 	int allmulti = 0;
593 
594 	MOS_LOCK_ASSERT(sc, MA_OWNED);
595 
596 	rxmode = mos_reg_read_1(sc, MOS_CTL);
597 
598 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC)
599 		allmulti = 1;
600 
601 	/* get all new ones */
602 	if_maddr_rlock(ifp);
603 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
604 		if (ifma->ifma_addr->sa_family != AF_LINK) {
605 			allmulti = 1;
606 			continue;
607 		};
608 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
609 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
610 		hashtbl[h / 8] |= 1 << (h % 8);
611 	}
612 	if_maddr_runlock(ifp);
613 
614 	/* now program new ones */
615 	if (allmulti == 1) {
616 		rxmode |= MOS_CTL_ALLMULTI;
617 		mos_reg_write_1(sc, MOS_CTL, rxmode);
618 	} else {
619 		rxmode &= ~MOS_CTL_ALLMULTI;
620 		mos_write_mcast(sc, (void *)&hashtbl);
621 		mos_reg_write_1(sc, MOS_CTL, rxmode);
622 	}
623 }
624 
625 static void
626 mos_reset(struct mos_softc *sc)
627 {
628 	uint8_t ctl;
629 
630 	ctl = mos_reg_read_1(sc, MOS_CTL);
631 	ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB |
632 	    MOS_CTL_RX_ENB);
633 	/* Disable RX, TX, promiscuous and allmulticast mode */
634 	mos_reg_write_1(sc, MOS_CTL, ctl);
635 
636 	/* Reset frame drop counter register to zero */
637 	mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
638 
639 	/* Wait a little while for the chip to get its brains in order. */
640 	usb_pause_mtx(&sc->sc_mtx, hz / 128);
641 	return;
642 }
643 
644 static void
645 mos_chip_init(struct mos_softc *sc)
646 {
647 	int i;
648 
649 	/*
650 	 * Rev.C devices have a pause threshold register which needs to be set
651 	 * at startup.
652 	 */
653 	if (mos_reg_read_1(sc, MOS_PAUSE_TRHD) != -1) {
654 		for (i = 0; i < MOS_PAUSE_REWRITES; i++)
655 			mos_reg_write_1(sc, MOS_PAUSE_TRHD, 0);
656 	}
657 	sc->mos_phyaddrs[0] = 1;
658 	sc->mos_phyaddrs[1] = 0xFF;
659 }
660 
661 /*
662  * Probe for a MCS7x30 chip.
663  */
664 static int
665 mos_probe(device_t dev)
666 {
667 	struct usb_attach_arg *uaa = device_get_ivars(dev);
668         int retval;
669 
670 	if (uaa->usb_mode != USB_MODE_HOST)
671 		return (ENXIO);
672 	if (uaa->info.bConfigIndex != MOS_CONFIG_IDX)
673 		return (ENXIO);
674 	if (uaa->info.bIfaceIndex != MOS_IFACE_IDX)
675 		return (ENXIO);
676 
677 	retval = usbd_lookup_id_by_uaa(mos_devs, sizeof(mos_devs), uaa);
678 	return (retval);
679 }
680 
681 /*
682  * Attach the interface. Allocate softc structures, do ifmedia
683  * setup and ethernet/BPF attach.
684  */
685 static int
686 mos_attach(device_t dev)
687 {
688 	struct usb_attach_arg *uaa = device_get_ivars(dev);
689 	struct mos_softc *sc = device_get_softc(dev);
690 	struct usb_ether *ue = &sc->sc_ue;
691 	uint8_t iface_index;
692 	int error;
693 
694 	sc->mos_flags = USB_GET_DRIVER_INFO(uaa);
695 
696 	device_set_usb_desc(dev);
697 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
698 
699 	iface_index = MOS_IFACE_IDX;
700 	error = usbd_transfer_setup(uaa->device, &iface_index,
701 	    sc->sc_xfer, mos_config, MOS_ENDPT_MAX,
702 	    sc, &sc->sc_mtx);
703 
704 	if (error) {
705 		device_printf(dev, "allocating USB transfers failed\n");
706 		goto detach;
707 	}
708 	ue->ue_sc = sc;
709 	ue->ue_dev = dev;
710 	ue->ue_udev = uaa->device;
711 	ue->ue_mtx = &sc->sc_mtx;
712 	ue->ue_methods = &mos_ue_methods;
713 
714 
715 	if (sc->mos_flags & MCS7730) {
716 		MOS_DPRINTFN("model: MCS7730");
717 	} else if (sc->mos_flags & MCS7830) {
718 		MOS_DPRINTFN("model: MCS7830");
719 	} else if (sc->mos_flags & MCS7832) {
720 		MOS_DPRINTFN("model: MCS7832");
721 	}
722 	error = uether_ifattach(ue);
723 	if (error) {
724 		device_printf(dev, "could not attach interface\n");
725 		goto detach;
726 	}
727 	return (0);
728 
729 
730 detach:
731 	mos_detach(dev);
732 	return (ENXIO);
733 }
734 
735 
736 static void
737 mos_attach_post(struct usb_ether *ue)
738 {
739 	struct mos_softc *sc = uether_getsc(ue);
740         int err;
741 
742 	/* Read MAC address, inform the world. */
743 	err = mos_readmac(sc, ue->ue_eaddr);
744 
745 	if (err)
746 	  MOS_DPRINTFN("couldn't get MAC address");
747 
748 	MOS_DPRINTFN("address: %s", ether_sprintf(ue->ue_eaddr));
749 
750 	mos_chip_init(sc);
751 }
752 
753 static int
754 mos_detach(device_t dev)
755 {
756 	struct mos_softc *sc = device_get_softc(dev);
757 	struct usb_ether *ue = &sc->sc_ue;
758 
759 	usbd_transfer_unsetup(sc->sc_xfer, MOS_ENDPT_MAX);
760 	uether_ifdetach(ue);
761 	mtx_destroy(&sc->sc_mtx);
762 
763 	return (0);
764 }
765 
766 
767 
768 
769 /*
770  * A frame has been uploaded: pass the resulting mbuf chain up to
771  * the higher level protocols.
772  */
773 static void
774 mos_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
775 {
776 	struct mos_softc *sc = usbd_xfer_softc(xfer);
777 	struct usb_ether *ue = &sc->sc_ue;
778 	struct ifnet *ifp = uether_getifp(ue);
779 
780 	uint8_t rxstat = 0;
781 	uint32_t actlen;
782 	uint16_t pktlen = 0;
783 	struct usb_page_cache *pc;
784 
785 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
786 	pc = usbd_xfer_get_frame(xfer, 0);
787 
788 	switch (USB_GET_STATE(xfer)) {
789 	case USB_ST_TRANSFERRED:
790 		MOS_DPRINTFN("actlen : %d", actlen);
791 		if (actlen <= 1) {
792 			ifp->if_ierrors++;
793 			goto tr_setup;
794 		}
795 		/* evaluate status byte at the end */
796 		usbd_copy_out(pc, actlen - sizeof(rxstat), &rxstat,
797 		    sizeof(rxstat));
798 
799 		if (rxstat != MOS_RXSTS_VALID) {
800 			MOS_DPRINTFN("erroneous frame received");
801 			if (rxstat & MOS_RXSTS_SHORT_FRAME)
802 				MOS_DPRINTFN("frame size less than 64 bytes");
803 			if (rxstat & MOS_RXSTS_LARGE_FRAME) {
804 				MOS_DPRINTFN("frame size larger than "
805 				    "1532 bytes");
806 			}
807 			if (rxstat & MOS_RXSTS_CRC_ERROR)
808 				MOS_DPRINTFN("CRC error");
809 			if (rxstat & MOS_RXSTS_ALIGN_ERROR)
810 				MOS_DPRINTFN("alignment error");
811 			ifp->if_ierrors++;
812 			goto tr_setup;
813 		}
814 		/* Remember the last byte was used for the status fields */
815 		pktlen = actlen - 1;
816 		if (pktlen < sizeof(struct ether_header)) {
817 			MOS_DPRINTFN("error: pktlen %d is smaller "
818 			    "than ether_header %zd", pktlen,
819 			    sizeof(struct ether_header));
820 			ifp->if_ierrors++;
821 			goto tr_setup;
822 		}
823 		uether_rxbuf(ue, pc, 0, actlen);
824 		/* FALLTHROUGH */
825 	case USB_ST_SETUP:
826 tr_setup:
827 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
828 		usbd_transfer_submit(xfer);
829 		uether_rxflush(ue);
830 		return;
831 	default:
832 		MOS_DPRINTFN("bulk read error, %s", usbd_errstr(error));
833 		if (error != USB_ERR_CANCELLED) {
834 			usbd_xfer_set_stall(xfer);
835 			goto tr_setup;
836 		}
837 		MOS_DPRINTFN("start rx %i", usbd_xfer_max_len(xfer));
838 		return;
839 	}
840 }
841 
842 /*
843  * A frame was downloaded to the chip. It's safe for us to clean up
844  * the list buffers.
845  */
846 static void
847 mos_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
848 {
849 	struct mos_softc *sc = usbd_xfer_softc(xfer);
850 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
851 	struct usb_page_cache *pc;
852 	struct mbuf *m;
853 
854 
855 
856 	switch (USB_GET_STATE(xfer)) {
857 	case USB_ST_TRANSFERRED:
858 		MOS_DPRINTFN("transfer of complete");
859 		ifp->if_opackets++;
860 		/* FALLTHROUGH */
861 	case USB_ST_SETUP:
862 tr_setup:
863 		/*
864 		 * XXX: don't send anything if there is no link?
865 		 */
866 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
867 		if (m == NULL)
868 			return;
869 
870 		pc = usbd_xfer_get_frame(xfer, 0);
871 		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
872 
873 		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
874 
875 
876 		/*
877 		 * if there's a BPF listener, bounce a copy
878 		 * of this frame to him:
879 		 */
880 		BPF_MTAP(ifp, m);
881 
882 		m_freem(m);
883 
884 		usbd_transfer_submit(xfer);
885 
886 		ifp->if_opackets++;
887 		return;
888 	default:
889 		MOS_DPRINTFN("usb error on tx: %s\n", usbd_errstr(error));
890 		ifp->if_oerrors++;
891 		if (error != USB_ERR_CANCELLED) {
892 			usbd_xfer_set_stall(xfer);
893 			goto tr_setup;
894 		}
895 		return;
896 	}
897 }
898 
899 static void
900 mos_tick(struct usb_ether *ue)
901 {
902 	struct mos_softc *sc = uether_getsc(ue);
903 	struct mii_data *mii = GET_MII(sc);
904 
905 	MOS_LOCK_ASSERT(sc, MA_OWNED);
906 
907 	mii_tick(mii);
908 	if (!sc->mos_link && mii->mii_media_status & IFM_ACTIVE &&
909 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
910 		MOS_DPRINTFN("got link");
911 		sc->mos_link++;
912 		mos_start(ue);
913 	}
914 }
915 
916 
917 static void
918 mos_start(struct usb_ether *ue)
919 {
920 	struct mos_softc *sc = uether_getsc(ue);
921 
922 	/*
923 	 * start the USB transfers, if not already started:
924 	 */
925 	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_TX]);
926 	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_RX]);
927 	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_INTR]);
928 }
929 
930 static void
931 mos_init(struct usb_ether *ue)
932 {
933 	struct mos_softc *sc = uether_getsc(ue);
934 	struct ifnet *ifp = uether_getifp(ue);
935 	uint8_t rxmode;
936 
937 	MOS_LOCK_ASSERT(sc, MA_OWNED);
938 
939 	/* Cancel pending I/O and free all RX/TX buffers. */
940 	mos_reset(sc);
941 
942 	/* Write MAC address */
943 	mos_writemac(sc, IF_LLADDR(ifp));
944 
945 	/* Read and set transmitter IPG values */
946 	sc->mos_ipgs[0] = mos_reg_read_1(sc, MOS_IPG0);
947 	sc->mos_ipgs[1] = mos_reg_read_1(sc, MOS_IPG1);
948 	mos_reg_write_1(sc, MOS_IPG0, sc->mos_ipgs[0]);
949 	mos_reg_write_1(sc, MOS_IPG1, sc->mos_ipgs[1]);
950 
951 	/*
952 	 * Enable receiver and transmitter, bridge controls speed/duplex
953 	 * mode
954 	 */
955 	rxmode = mos_reg_read_1(sc, MOS_CTL);
956 	rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
957 	rxmode &= ~(MOS_CTL_SLEEP);
958 
959 	mos_setpromisc(ue);
960 
961 	/* XXX: broadcast mode? */
962 	mos_reg_write_1(sc, MOS_CTL, rxmode);
963 
964 	/* Load the multicast filter. */
965 	mos_setmulti(ue);
966 
967 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
968 	mos_start(ue);
969 }
970 
971 
972 static void
973 mos_intr_callback(struct usb_xfer *xfer, usb_error_t error)
974 {
975 	struct mos_softc *sc = usbd_xfer_softc(xfer);
976 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
977 	struct usb_page_cache *pc;
978 	uint32_t pkt;
979 	int actlen;
980 
981 	ifp->if_oerrors++;
982 
983 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
984 	MOS_DPRINTFN("actlen %i", actlen);
985 
986 	switch (USB_GET_STATE(xfer)) {
987 	case USB_ST_TRANSFERRED:
988 
989 		pc = usbd_xfer_get_frame(xfer, 0);
990 		usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
991 		/* FALLTHROUGH */
992 	case USB_ST_SETUP:
993 tr_setup:
994 		return;
995 	default:
996 		if (error != USB_ERR_CANCELLED) {
997 			usbd_xfer_set_stall(xfer);
998 			goto tr_setup;
999 		}
1000 		return;
1001 	}
1002 }
1003 
1004 
1005 /*
1006  * Stop the adapter and free any mbufs allocated to the
1007  * RX and TX lists.
1008  */
1009 static void
1010 mos_stop(struct usb_ether *ue)
1011 {
1012 	struct mos_softc *sc = uether_getsc(ue);
1013 	struct ifnet *ifp = uether_getifp(ue);
1014 
1015 	mos_reset(sc);
1016 
1017 	MOS_LOCK_ASSERT(sc, MA_OWNED);
1018 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1019 
1020 	/* stop all the transfers, if not already stopped */
1021 	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_TX]);
1022 	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_RX]);
1023 	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_INTR]);
1024 
1025 	sc->mos_link = 0;
1026 }
1027