xref: /freebsd/sys/dev/usb/net/if_mos.c (revision 42249ef2)
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, 0, "USB mos");
148 SYSCTL_INT(_hw_usb_mos, OID_AUTO, debug, CTLFLAG_RWTUN, &mos_debug, 0,
149     "Debug level");
150 #endif
151 
152 #define MOS_DPRINTFN(fmt,...) \
153   DPRINTF("mos: %s: " fmt "\n",__FUNCTION__,## __VA_ARGS__)
154 
155 #define	USB_PRODUCT_MOSCHIP_MCS7730	0x7730
156 #define	USB_PRODUCT_SITECOMEU_LN030	0x0021
157 
158 
159 
160 /* Various supported device vendors/products. */
161 static const STRUCT_USB_HOST_ID mos_devs[] = {
162 	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7730, MCS7730)},
163 	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7830, MCS7830)},
164 	{USB_VPI(USB_VENDOR_MOSCHIP, USB_PRODUCT_MOSCHIP_MCS7832, MCS7832)},
165 	{USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN030, MCS7830)},
166 };
167 
168 static int mos_probe(device_t dev);
169 static int mos_attach(device_t dev);
170 static void mos_attach_post(struct usb_ether *ue);
171 static int mos_detach(device_t dev);
172 
173 static void mos_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error);
174 static void mos_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error);
175 static void mos_intr_callback(struct usb_xfer *xfer, usb_error_t error);
176 static void mos_tick(struct usb_ether *);
177 static void mos_start(struct usb_ether *);
178 static void mos_init(struct usb_ether *);
179 static void mos_chip_init(struct mos_softc *);
180 static void mos_stop(struct usb_ether *);
181 static int mos_miibus_readreg(device_t, int, int);
182 static int mos_miibus_writereg(device_t, int, int, int);
183 static void mos_miibus_statchg(device_t);
184 static int mos_ifmedia_upd(struct ifnet *);
185 static void mos_ifmedia_sts(struct ifnet *, struct ifmediareq *);
186 static void mos_reset(struct mos_softc *sc);
187 
188 static int mos_reg_read_1(struct mos_softc *, int);
189 static int mos_reg_read_2(struct mos_softc *, int);
190 static int mos_reg_write_1(struct mos_softc *, int, int);
191 static int mos_reg_write_2(struct mos_softc *, int, int);
192 static int mos_readmac(struct mos_softc *, uint8_t *);
193 static int mos_writemac(struct mos_softc *, uint8_t *);
194 static int mos_write_mcast(struct mos_softc *, u_char *);
195 
196 static void mos_setmulti(struct usb_ether *);
197 static void mos_setpromisc(struct usb_ether *);
198 
199 static const struct usb_config mos_config[MOS_ENDPT_MAX] = {
200 
201 	[MOS_ENDPT_TX] = {
202 		.type = UE_BULK,
203 		.endpoint = UE_ADDR_ANY,
204 		.direction = UE_DIR_OUT,
205 		.bufsize = (MCLBYTES + 2),
206 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
207 		.callback = mos_bulk_write_callback,
208 		.timeout = 10000,
209 	},
210 
211 	[MOS_ENDPT_RX] = {
212 		.type = UE_BULK,
213 		.endpoint = UE_ADDR_ANY,
214 		.direction = UE_DIR_IN,
215 		.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
216 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
217 		.callback = mos_bulk_read_callback,
218 	},
219 
220 	[MOS_ENDPT_INTR] = {
221 		.type = UE_INTERRUPT,
222 		.endpoint = UE_ADDR_ANY,
223 		.direction = UE_DIR_IN,
224 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
225 		.bufsize = 0,
226 		.callback = mos_intr_callback,
227 	},
228 };
229 
230 static device_method_t mos_methods[] = {
231 	/* Device interface */
232 	DEVMETHOD(device_probe, mos_probe),
233 	DEVMETHOD(device_attach, mos_attach),
234 	DEVMETHOD(device_detach, mos_detach),
235 
236 	/* MII interface */
237 	DEVMETHOD(miibus_readreg, mos_miibus_readreg),
238 	DEVMETHOD(miibus_writereg, mos_miibus_writereg),
239 	DEVMETHOD(miibus_statchg, mos_miibus_statchg),
240 
241 	DEVMETHOD_END
242 };
243 
244 static driver_t mos_driver = {
245 	.name = "mos",
246 	.methods = mos_methods,
247 	.size = sizeof(struct mos_softc)
248 };
249 
250 static devclass_t mos_devclass;
251 
252 DRIVER_MODULE(mos, uhub, mos_driver, mos_devclass, NULL, 0);
253 DRIVER_MODULE(miibus, mos, miibus_driver, miibus_devclass, 0, 0);
254 MODULE_DEPEND(mos, uether, 1, 1, 1);
255 MODULE_DEPEND(mos, usb, 1, 1, 1);
256 MODULE_DEPEND(mos, ether, 1, 1, 1);
257 MODULE_DEPEND(mos, miibus, 1, 1, 1);
258 USB_PNP_HOST_INFO(mos_devs);
259 
260 static const struct usb_ether_methods mos_ue_methods = {
261 	.ue_attach_post = mos_attach_post,
262 	.ue_start = mos_start,
263 	.ue_init = mos_init,
264 	.ue_stop = mos_stop,
265 	.ue_tick = mos_tick,
266 	.ue_setmulti = mos_setmulti,
267 	.ue_setpromisc = mos_setpromisc,
268 	.ue_mii_upd = mos_ifmedia_upd,
269 	.ue_mii_sts = mos_ifmedia_sts,
270 };
271 
272 
273 static int
274 mos_reg_read_1(struct mos_softc *sc, int reg)
275 {
276 	struct usb_device_request req;
277 	usb_error_t err;
278 	uByte val = 0;
279 
280 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
281 	req.bRequest = MOS_UR_READREG;
282 	USETW(req.wValue, 0);
283 	USETW(req.wIndex, reg);
284 	USETW(req.wLength, 1);
285 
286 	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
287 
288 	if (err) {
289 		MOS_DPRINTFN("mos_reg_read_1 error, reg: %d\n", reg);
290 		return (-1);
291 	}
292 	return (val);
293 }
294 
295 static int
296 mos_reg_read_2(struct mos_softc *sc, int reg)
297 {
298 	struct usb_device_request req;
299 	usb_error_t err;
300 	uWord val;
301 
302 	USETW(val, 0);
303 
304 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
305 	req.bRequest = MOS_UR_READREG;
306 	USETW(req.wValue, 0);
307 	USETW(req.wIndex, reg);
308 	USETW(req.wLength, 2);
309 
310 	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
311 
312 	if (err) {
313 		MOS_DPRINTFN("mos_reg_read_2 error, reg: %d", reg);
314 		return (-1);
315 	}
316 	return (UGETW(val));
317 }
318 
319 static int
320 mos_reg_write_1(struct mos_softc *sc, int reg, int aval)
321 {
322 	struct usb_device_request req;
323 	usb_error_t err;
324 	uByte val;
325 	val = aval;
326 
327 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
328 	req.bRequest = MOS_UR_WRITEREG;
329 	USETW(req.wValue, 0);
330 	USETW(req.wIndex, reg);
331 	USETW(req.wLength, 1);
332 
333 	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
334 
335 	if (err) {
336 		MOS_DPRINTFN("mos_reg_write_1 error, reg: %d", reg);
337 		return (-1);
338 	}
339 	return (0);
340 }
341 
342 static int
343 mos_reg_write_2(struct mos_softc *sc, int reg, int aval)
344 {
345 	struct usb_device_request req;
346 	usb_error_t err;
347 	uWord val;
348 
349 	USETW(val, aval);
350 
351 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
352 	req.bRequest = MOS_UR_WRITEREG;
353 	USETW(req.wValue, 0);
354 	USETW(req.wIndex, reg);
355 	USETW(req.wLength, 2);
356 
357 	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
358 
359 	if (err) {
360 		MOS_DPRINTFN("mos_reg_write_2 error, reg: %d", reg);
361 		return (-1);
362 	}
363 	return (0);
364 }
365 
366 static int
367 mos_readmac(struct mos_softc *sc, u_char *mac)
368 {
369 	struct usb_device_request req;
370 	usb_error_t err;
371 
372 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
373 	req.bRequest = MOS_UR_READREG;
374 	USETW(req.wValue, 0);
375 	USETW(req.wIndex, MOS_MAC);
376 	USETW(req.wLength, ETHER_ADDR_LEN);
377 
378 	err = uether_do_request(&sc->sc_ue, &req, mac, 1000);
379 
380 	if (err) {
381 		return (-1);
382 	}
383 	return (0);
384 }
385 
386 static int
387 mos_writemac(struct mos_softc *sc, uint8_t *mac)
388 {
389 	struct usb_device_request req;
390 	usb_error_t err;
391 
392 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
393 	req.bRequest = MOS_UR_WRITEREG;
394 	USETW(req.wValue, 0);
395 	USETW(req.wIndex, MOS_MAC);
396 	USETW(req.wLength, ETHER_ADDR_LEN);
397 
398 	err = uether_do_request(&sc->sc_ue, &req, mac, 1000);
399 
400 	if (err) {
401 		MOS_DPRINTFN("mos_writemac error");
402 		return (-1);
403 	}
404 	return (0);
405 }
406 
407 static int
408 mos_write_mcast(struct mos_softc *sc, u_char *hashtbl)
409 {
410 	struct usb_device_request req;
411 	usb_error_t err;
412 
413 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
414 	req.bRequest = MOS_UR_WRITEREG;
415 	USETW(req.wValue, 0);
416 	USETW(req.wIndex, MOS_MCAST_TABLE);
417 	USETW(req.wLength, 8);
418 
419 	err = uether_do_request(&sc->sc_ue, &req, hashtbl, 1000);
420 
421 	if (err) {
422 		MOS_DPRINTFN("mos_reg_mcast error");
423 		return (-1);
424 	}
425 	return (0);
426 }
427 
428 static int
429 mos_miibus_readreg(device_t dev, int phy, int reg)
430 {
431 	struct mos_softc *sc = device_get_softc(dev);
432 	uWord val;
433 	int i, res, locked;
434 
435 	USETW(val, 0);
436 
437 	locked = mtx_owned(&sc->sc_mtx);
438 	if (!locked)
439 		MOS_LOCK(sc);
440 
441 	mos_reg_write_2(sc, MOS_PHY_DATA, 0);
442 	mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
443 	    MOS_PHYCTL_READ);
444 	mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
445 	    MOS_PHYSTS_PENDING);
446 
447 	for (i = 0; i < MOS_TIMEOUT; i++) {
448 		if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY)
449 			break;
450 	}
451 	if (i == MOS_TIMEOUT) {
452 		MOS_DPRINTFN("MII read timeout");
453 	}
454 	res = mos_reg_read_2(sc, MOS_PHY_DATA);
455 
456 	if (!locked)
457 		MOS_UNLOCK(sc);
458 	return (res);
459 }
460 
461 static int
462 mos_miibus_writereg(device_t dev, int phy, int reg, int val)
463 {
464 	struct mos_softc *sc = device_get_softc(dev);
465 	int i, locked;
466 
467 	locked = mtx_owned(&sc->sc_mtx);
468 	if (!locked)
469 		MOS_LOCK(sc);
470 
471 	mos_reg_write_2(sc, MOS_PHY_DATA, val);
472 	mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
473 	    MOS_PHYCTL_WRITE);
474 	mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
475 	    MOS_PHYSTS_PENDING);
476 
477 	for (i = 0; i < MOS_TIMEOUT; i++) {
478 		if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY)
479 			break;
480 	}
481 	if (i == MOS_TIMEOUT)
482 		MOS_DPRINTFN("MII write timeout");
483 
484 	if (!locked)
485 		MOS_UNLOCK(sc);
486 	return 0;
487 }
488 
489 static void
490 mos_miibus_statchg(device_t dev)
491 {
492 	struct mos_softc *sc = device_get_softc(dev);
493 	struct mii_data *mii = GET_MII(sc);
494 	int val, err, locked;
495 
496 	locked = mtx_owned(&sc->sc_mtx);
497 	if (!locked)
498 		MOS_LOCK(sc);
499 
500 	/* disable RX, TX prior to changing FDX, SPEEDSEL */
501 	val = mos_reg_read_1(sc, MOS_CTL);
502 	val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
503 	mos_reg_write_1(sc, MOS_CTL, val);
504 
505 	/* reset register which counts dropped frames */
506 	mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
507 
508 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
509 		val |= MOS_CTL_FDX_ENB;
510 	else
511 		val &= ~(MOS_CTL_FDX_ENB);
512 
513 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
514 	case IFM_100_TX:
515 		val |= MOS_CTL_SPEEDSEL;
516 		break;
517 	case IFM_10_T:
518 		val &= ~(MOS_CTL_SPEEDSEL);
519 		break;
520 	}
521 
522 	/* re-enable TX, RX */
523 	val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
524 	err = mos_reg_write_1(sc, MOS_CTL, val);
525 
526 	if (err)
527 		MOS_DPRINTFN("media change failed");
528 
529 	if (!locked)
530 		MOS_UNLOCK(sc);
531 }
532 
533 /*
534  * Set media options.
535  */
536 static int
537 mos_ifmedia_upd(struct ifnet *ifp)
538 {
539 	struct mos_softc *sc = ifp->if_softc;
540 	struct mii_data *mii = GET_MII(sc);
541 	struct mii_softc *miisc;
542 	int error;
543 
544 	MOS_LOCK_ASSERT(sc, MA_OWNED);
545 
546 	sc->mos_link = 0;
547 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
548 		PHY_RESET(miisc);
549 	error = mii_mediachg(mii);
550 	return (error);
551 }
552 
553 /*
554  * Report current media status.
555  */
556 static void
557 mos_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
558 {
559 	struct mos_softc *sc = ifp->if_softc;
560 	struct mii_data *mii = GET_MII(sc);
561 
562 	MOS_LOCK(sc);
563 	mii_pollstat(mii);
564 
565 	ifmr->ifm_active = mii->mii_media_active;
566 	ifmr->ifm_status = mii->mii_media_status;
567 	MOS_UNLOCK(sc);
568 }
569 
570 static void
571 mos_setpromisc(struct usb_ether *ue)
572 {
573 	struct mos_softc *sc = uether_getsc(ue);
574 	struct ifnet *ifp = uether_getifp(ue);
575 
576 	uint8_t rxmode;
577 
578 	MOS_LOCK_ASSERT(sc, MA_OWNED);
579 
580 	rxmode = mos_reg_read_1(sc, MOS_CTL);
581 
582 	/* If we want promiscuous mode, set the allframes bit. */
583 	if (ifp->if_flags & IFF_PROMISC) {
584 		rxmode |= MOS_CTL_RX_PROMISC;
585 	} else {
586 		rxmode &= ~MOS_CTL_RX_PROMISC;
587 	}
588 
589 	mos_reg_write_1(sc, MOS_CTL, rxmode);
590 }
591 
592 
593 
594 static void
595 mos_setmulti(struct usb_ether *ue)
596 {
597 	struct mos_softc *sc = uether_getsc(ue);
598 	struct ifnet *ifp = uether_getifp(ue);
599 	struct ifmultiaddr *ifma;
600 
601 	uint32_t h = 0;
602 	uint8_t rxmode;
603 	uint8_t hashtbl[8] = {0, 0, 0, 0, 0, 0, 0, 0};
604 	int allmulti = 0;
605 
606 	MOS_LOCK_ASSERT(sc, MA_OWNED);
607 
608 	rxmode = mos_reg_read_1(sc, MOS_CTL);
609 
610 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC)
611 		allmulti = 1;
612 
613 	/* get all new ones */
614 	if_maddr_rlock(ifp);
615 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
616 		if (ifma->ifma_addr->sa_family != AF_LINK) {
617 			allmulti = 1;
618 			continue;
619 		}
620 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
621 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
622 		hashtbl[h / 8] |= 1 << (h % 8);
623 	}
624 	if_maddr_runlock(ifp);
625 
626 	/* now program new ones */
627 	if (allmulti == 1) {
628 		rxmode |= MOS_CTL_ALLMULTI;
629 		mos_reg_write_1(sc, MOS_CTL, rxmode);
630 	} else {
631 		rxmode &= ~MOS_CTL_ALLMULTI;
632 		mos_write_mcast(sc, (void *)&hashtbl);
633 		mos_reg_write_1(sc, MOS_CTL, rxmode);
634 	}
635 }
636 
637 static void
638 mos_reset(struct mos_softc *sc)
639 {
640 	uint8_t ctl;
641 
642 	ctl = mos_reg_read_1(sc, MOS_CTL);
643 	ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB |
644 	    MOS_CTL_RX_ENB);
645 	/* Disable RX, TX, promiscuous and allmulticast mode */
646 	mos_reg_write_1(sc, MOS_CTL, ctl);
647 
648 	/* Reset frame drop counter register to zero */
649 	mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
650 
651 	/* Wait a little while for the chip to get its brains in order. */
652 	usb_pause_mtx(&sc->sc_mtx, hz / 128);
653 	return;
654 }
655 
656 static void
657 mos_chip_init(struct mos_softc *sc)
658 {
659 	int i;
660 
661 	/*
662 	 * Rev.C devices have a pause threshold register which needs to be set
663 	 * at startup.
664 	 */
665 	if (mos_reg_read_1(sc, MOS_PAUSE_TRHD) != -1) {
666 		for (i = 0; i < MOS_PAUSE_REWRITES; i++)
667 			mos_reg_write_1(sc, MOS_PAUSE_TRHD, 0);
668 	}
669 	sc->mos_phyaddrs[0] = 1;
670 	sc->mos_phyaddrs[1] = 0xFF;
671 }
672 
673 /*
674  * Probe for a MCS7x30 chip.
675  */
676 static int
677 mos_probe(device_t dev)
678 {
679 	struct usb_attach_arg *uaa = device_get_ivars(dev);
680         int retval;
681 
682 	if (uaa->usb_mode != USB_MODE_HOST)
683 		return (ENXIO);
684 	if (uaa->info.bConfigIndex != MOS_CONFIG_IDX)
685 		return (ENXIO);
686 	if (uaa->info.bIfaceIndex != MOS_IFACE_IDX)
687 		return (ENXIO);
688 
689 	retval = usbd_lookup_id_by_uaa(mos_devs, sizeof(mos_devs), uaa);
690 	return (retval);
691 }
692 
693 /*
694  * Attach the interface. Allocate softc structures, do ifmedia
695  * setup and ethernet/BPF attach.
696  */
697 static int
698 mos_attach(device_t dev)
699 {
700 	struct usb_attach_arg *uaa = device_get_ivars(dev);
701 	struct mos_softc *sc = device_get_softc(dev);
702 	struct usb_ether *ue = &sc->sc_ue;
703 	uint8_t iface_index;
704 	int error;
705 
706 	sc->mos_flags = USB_GET_DRIVER_INFO(uaa);
707 
708 	device_set_usb_desc(dev);
709 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
710 
711 	iface_index = MOS_IFACE_IDX;
712 	error = usbd_transfer_setup(uaa->device, &iface_index,
713 	    sc->sc_xfer, mos_config, MOS_ENDPT_MAX,
714 	    sc, &sc->sc_mtx);
715 
716 	if (error) {
717 		device_printf(dev, "allocating USB transfers failed\n");
718 		goto detach;
719 	}
720 	ue->ue_sc = sc;
721 	ue->ue_dev = dev;
722 	ue->ue_udev = uaa->device;
723 	ue->ue_mtx = &sc->sc_mtx;
724 	ue->ue_methods = &mos_ue_methods;
725 
726 
727 	if (sc->mos_flags & MCS7730) {
728 		MOS_DPRINTFN("model: MCS7730");
729 	} else if (sc->mos_flags & MCS7830) {
730 		MOS_DPRINTFN("model: MCS7830");
731 	} else if (sc->mos_flags & MCS7832) {
732 		MOS_DPRINTFN("model: MCS7832");
733 	}
734 	error = uether_ifattach(ue);
735 	if (error) {
736 		device_printf(dev, "could not attach interface\n");
737 		goto detach;
738 	}
739 	return (0);
740 
741 
742 detach:
743 	mos_detach(dev);
744 	return (ENXIO);
745 }
746 
747 
748 static void
749 mos_attach_post(struct usb_ether *ue)
750 {
751 	struct mos_softc *sc = uether_getsc(ue);
752         int err;
753 
754 	/* Read MAC address, inform the world. */
755 	err = mos_readmac(sc, ue->ue_eaddr);
756 
757 	if (err)
758 	  MOS_DPRINTFN("couldn't get MAC address");
759 
760 	MOS_DPRINTFN("address: %s", ether_sprintf(ue->ue_eaddr));
761 
762 	mos_chip_init(sc);
763 }
764 
765 static int
766 mos_detach(device_t dev)
767 {
768 	struct mos_softc *sc = device_get_softc(dev);
769 	struct usb_ether *ue = &sc->sc_ue;
770 
771 	usbd_transfer_unsetup(sc->sc_xfer, MOS_ENDPT_MAX);
772 	uether_ifdetach(ue);
773 	mtx_destroy(&sc->sc_mtx);
774 
775 	return (0);
776 }
777 
778 
779 
780 
781 /*
782  * A frame has been uploaded: pass the resulting mbuf chain up to
783  * the higher level protocols.
784  */
785 static void
786 mos_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
787 {
788 	struct mos_softc *sc = usbd_xfer_softc(xfer);
789 	struct usb_ether *ue = &sc->sc_ue;
790 	struct ifnet *ifp = uether_getifp(ue);
791 
792 	uint8_t rxstat = 0;
793 	uint32_t actlen;
794 	uint16_t pktlen = 0;
795 	struct usb_page_cache *pc;
796 
797 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
798 	pc = usbd_xfer_get_frame(xfer, 0);
799 
800 	switch (USB_GET_STATE(xfer)) {
801 	case USB_ST_TRANSFERRED:
802 		MOS_DPRINTFN("actlen : %d", actlen);
803 		if (actlen <= 1) {
804 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
805 			goto tr_setup;
806 		}
807 		/* evaluate status byte at the end */
808 		usbd_copy_out(pc, actlen - sizeof(rxstat), &rxstat,
809 		    sizeof(rxstat));
810 
811 		if (rxstat != MOS_RXSTS_VALID) {
812 			MOS_DPRINTFN("erroneous frame received");
813 			if (rxstat & MOS_RXSTS_SHORT_FRAME)
814 				MOS_DPRINTFN("frame size less than 64 bytes");
815 			if (rxstat & MOS_RXSTS_LARGE_FRAME) {
816 				MOS_DPRINTFN("frame size larger than "
817 				    "1532 bytes");
818 			}
819 			if (rxstat & MOS_RXSTS_CRC_ERROR)
820 				MOS_DPRINTFN("CRC error");
821 			if (rxstat & MOS_RXSTS_ALIGN_ERROR)
822 				MOS_DPRINTFN("alignment error");
823 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
824 			goto tr_setup;
825 		}
826 		/* Remember the last byte was used for the status fields */
827 		pktlen = actlen - 1;
828 		if (pktlen < sizeof(struct ether_header)) {
829 			MOS_DPRINTFN("error: pktlen %d is smaller "
830 			    "than ether_header %zd", pktlen,
831 			    sizeof(struct ether_header));
832 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
833 			goto tr_setup;
834 		}
835 		uether_rxbuf(ue, pc, 0, actlen);
836 		/* FALLTHROUGH */
837 	case USB_ST_SETUP:
838 tr_setup:
839 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
840 		usbd_transfer_submit(xfer);
841 		uether_rxflush(ue);
842 		return;
843 	default:
844 		MOS_DPRINTFN("bulk read error, %s", usbd_errstr(error));
845 		if (error != USB_ERR_CANCELLED) {
846 			usbd_xfer_set_stall(xfer);
847 			goto tr_setup;
848 		}
849 		MOS_DPRINTFN("start rx %i", usbd_xfer_max_len(xfer));
850 		return;
851 	}
852 }
853 
854 /*
855  * A frame was downloaded to the chip. It's safe for us to clean up
856  * the list buffers.
857  */
858 static void
859 mos_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
860 {
861 	struct mos_softc *sc = usbd_xfer_softc(xfer);
862 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
863 	struct usb_page_cache *pc;
864 	struct mbuf *m;
865 
866 
867 
868 	switch (USB_GET_STATE(xfer)) {
869 	case USB_ST_TRANSFERRED:
870 		MOS_DPRINTFN("transfer of complete");
871 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
872 		/* FALLTHROUGH */
873 	case USB_ST_SETUP:
874 tr_setup:
875 		/*
876 		 * XXX: don't send anything if there is no link?
877 		 */
878 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
879 		if (m == NULL)
880 			return;
881 
882 		pc = usbd_xfer_get_frame(xfer, 0);
883 		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
884 
885 		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
886 
887 
888 		/*
889 		 * if there's a BPF listener, bounce a copy
890 		 * of this frame to him:
891 		 */
892 		BPF_MTAP(ifp, m);
893 
894 		m_freem(m);
895 
896 		usbd_transfer_submit(xfer);
897 
898 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
899 		return;
900 	default:
901 		MOS_DPRINTFN("usb error on tx: %s\n", usbd_errstr(error));
902 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
903 		if (error != USB_ERR_CANCELLED) {
904 			usbd_xfer_set_stall(xfer);
905 			goto tr_setup;
906 		}
907 		return;
908 	}
909 }
910 
911 static void
912 mos_tick(struct usb_ether *ue)
913 {
914 	struct mos_softc *sc = uether_getsc(ue);
915 	struct mii_data *mii = GET_MII(sc);
916 
917 	MOS_LOCK_ASSERT(sc, MA_OWNED);
918 
919 	mii_tick(mii);
920 	if (!sc->mos_link && mii->mii_media_status & IFM_ACTIVE &&
921 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
922 		MOS_DPRINTFN("got link");
923 		sc->mos_link++;
924 		mos_start(ue);
925 	}
926 }
927 
928 
929 static void
930 mos_start(struct usb_ether *ue)
931 {
932 	struct mos_softc *sc = uether_getsc(ue);
933 
934 	/*
935 	 * start the USB transfers, if not already started:
936 	 */
937 	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_TX]);
938 	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_RX]);
939 	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_INTR]);
940 }
941 
942 static void
943 mos_init(struct usb_ether *ue)
944 {
945 	struct mos_softc *sc = uether_getsc(ue);
946 	struct ifnet *ifp = uether_getifp(ue);
947 	uint8_t rxmode;
948 
949 	MOS_LOCK_ASSERT(sc, MA_OWNED);
950 
951 	/* Cancel pending I/O and free all RX/TX buffers. */
952 	mos_reset(sc);
953 
954 	/* Write MAC address */
955 	mos_writemac(sc, IF_LLADDR(ifp));
956 
957 	/* Read and set transmitter IPG values */
958 	sc->mos_ipgs[0] = mos_reg_read_1(sc, MOS_IPG0);
959 	sc->mos_ipgs[1] = mos_reg_read_1(sc, MOS_IPG1);
960 	mos_reg_write_1(sc, MOS_IPG0, sc->mos_ipgs[0]);
961 	mos_reg_write_1(sc, MOS_IPG1, sc->mos_ipgs[1]);
962 
963 	/*
964 	 * Enable receiver and transmitter, bridge controls speed/duplex
965 	 * mode
966 	 */
967 	rxmode = mos_reg_read_1(sc, MOS_CTL);
968 	rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
969 	rxmode &= ~(MOS_CTL_SLEEP);
970 
971 	mos_setpromisc(ue);
972 
973 	/* XXX: broadcast mode? */
974 	mos_reg_write_1(sc, MOS_CTL, rxmode);
975 
976 	/* Load the multicast filter. */
977 	mos_setmulti(ue);
978 
979 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
980 	mos_start(ue);
981 }
982 
983 
984 static void
985 mos_intr_callback(struct usb_xfer *xfer, usb_error_t error)
986 {
987 	struct mos_softc *sc = usbd_xfer_softc(xfer);
988 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
989 	struct usb_page_cache *pc;
990 	uint32_t pkt;
991 	int actlen;
992 
993 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
994 
995 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
996 	MOS_DPRINTFN("actlen %i", actlen);
997 
998 	switch (USB_GET_STATE(xfer)) {
999 	case USB_ST_TRANSFERRED:
1000 
1001 		pc = usbd_xfer_get_frame(xfer, 0);
1002 		usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
1003 		/* FALLTHROUGH */
1004 	case USB_ST_SETUP:
1005 tr_setup:
1006 		return;
1007 	default:
1008 		if (error != USB_ERR_CANCELLED) {
1009 			usbd_xfer_set_stall(xfer);
1010 			goto tr_setup;
1011 		}
1012 		return;
1013 	}
1014 }
1015 
1016 
1017 /*
1018  * Stop the adapter and free any mbufs allocated to the
1019  * RX and TX lists.
1020  */
1021 static void
1022 mos_stop(struct usb_ether *ue)
1023 {
1024 	struct mos_softc *sc = uether_getsc(ue);
1025 	struct ifnet *ifp = uether_getifp(ue);
1026 
1027 	mos_reset(sc);
1028 
1029 	MOS_LOCK_ASSERT(sc, MA_OWNED);
1030 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1031 
1032 	/* stop all the transfers, if not already stopped */
1033 	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_TX]);
1034 	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_RX]);
1035 	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_INTR]);
1036 
1037 	sc->mos_link = 0;
1038 }
1039