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