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