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