if_mos.c revision 223486
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 223486 2011-06-24 02:30:02Z hselasky $");
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
135SYSCTL_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	/* bus interface */
224	DEVMETHOD(bus_print_child, bus_generic_print_child),
225	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
226
227	/* MII interface */
228	DEVMETHOD(miibus_readreg, mos_miibus_readreg),
229	DEVMETHOD(miibus_writereg, mos_miibus_writereg),
230	DEVMETHOD(miibus_statchg, mos_miibus_statchg),
231
232	{0, 0}
233};
234
235static driver_t mos_driver = {
236	.name = "mos",
237	.methods = mos_methods,
238	.size = sizeof(struct mos_softc)
239};
240
241static devclass_t mos_devclass;
242
243DRIVER_MODULE(mos, uhub, mos_driver, mos_devclass, NULL, 0);
244DRIVER_MODULE(miibus, mos, miibus_driver, miibus_devclass, 0, 0);
245MODULE_DEPEND(mos, uether, 1, 1, 1);
246MODULE_DEPEND(mos, usb, 1, 1, 1);
247MODULE_DEPEND(mos, ether, 1, 1, 1);
248MODULE_DEPEND(mos, miibus, 1, 1, 1);
249
250static const struct usb_ether_methods mos_ue_methods = {
251	.ue_attach_post = mos_attach_post,
252	.ue_start = mos_start,
253	.ue_init = mos_init,
254	.ue_stop = mos_stop,
255	.ue_tick = mos_tick,
256	.ue_setmulti = mos_setmulti,
257	.ue_setpromisc = mos_setpromisc,
258	.ue_mii_upd = mos_ifmedia_upd,
259	.ue_mii_sts = mos_ifmedia_sts,
260};
261
262
263static int
264mos_reg_read_1(struct mos_softc *sc, int reg)
265{
266	struct usb_device_request req;
267	usb_error_t err;
268	uByte val = 0;
269
270	req.bmRequestType = UT_READ_VENDOR_DEVICE;
271	req.bRequest = MOS_UR_READREG;
272	USETW(req.wValue, 0);
273	USETW(req.wIndex, reg);
274	USETW(req.wLength, 1);
275
276	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
277
278	if (err) {
279		MOS_DPRINTFN("mos_reg_read_1 error, reg: %d\n", reg);
280		return (-1);
281	}
282	return (val);
283}
284
285static int
286mos_reg_read_2(struct mos_softc *sc, int reg)
287{
288	struct usb_device_request req;
289	usb_error_t err;
290	uWord val;
291
292	USETW(val, 0);
293
294	req.bmRequestType = UT_READ_VENDOR_DEVICE;
295	req.bRequest = MOS_UR_READREG;
296	USETW(req.wValue, 0);
297	USETW(req.wIndex, reg);
298	USETW(req.wLength, 2);
299
300	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
301
302	if (err) {
303		MOS_DPRINTFN("mos_reg_read_2 error, reg: %d", reg);
304		return (-1);
305	}
306	return (UGETW(val));
307}
308
309static int
310mos_reg_write_1(struct mos_softc *sc, int reg, int aval)
311{
312	struct usb_device_request req;
313	usb_error_t err;
314	uByte val;
315	val = aval;
316
317	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
318	req.bRequest = MOS_UR_WRITEREG;
319	USETW(req.wValue, 0);
320	USETW(req.wIndex, reg);
321	USETW(req.wLength, 1);
322
323	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
324
325	if (err) {
326		MOS_DPRINTFN("mos_reg_write_1 error, reg: %d", reg);
327		return (-1);
328	}
329	return (0);
330}
331
332static int
333mos_reg_write_2(struct mos_softc *sc, int reg, int aval)
334{
335	struct usb_device_request req;
336	usb_error_t err;
337	uWord val;
338
339	USETW(val, aval);
340
341	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
342	req.bRequest = MOS_UR_WRITEREG;
343	USETW(req.wValue, 0);
344	USETW(req.wIndex, reg);
345	USETW(req.wLength, 2);
346
347	err = uether_do_request(&sc->sc_ue, &req, &val, 1000);
348
349	if (err) {
350		MOS_DPRINTFN("mos_reg_write_2 error, reg: %d", reg);
351		return (-1);
352	}
353	return (0);
354}
355
356static int
357mos_readmac(struct mos_softc *sc, u_char *mac)
358{
359	struct usb_device_request req;
360	usb_error_t err;
361
362	req.bmRequestType = UT_READ_VENDOR_DEVICE;
363	req.bRequest = MOS_UR_READREG;
364	USETW(req.wValue, 0);
365	USETW(req.wIndex, MOS_MAC);
366	USETW(req.wLength, ETHER_ADDR_LEN);
367
368	err = uether_do_request(&sc->sc_ue, &req, mac, 1000);
369
370	if (err) {
371		return (-1);
372	}
373	return (0);
374}
375
376static int
377mos_writemac(struct mos_softc *sc, uint8_t *mac)
378{
379	struct usb_device_request req;
380	usb_error_t err;
381
382	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
383	req.bRequest = MOS_UR_WRITEREG;
384	USETW(req.wValue, 0);
385	USETW(req.wIndex, MOS_MAC);
386	USETW(req.wLength, ETHER_ADDR_LEN);
387
388	err = uether_do_request(&sc->sc_ue, &req, mac, 1000);
389
390	if (err) {
391		MOS_DPRINTFN("mos_writemac error");
392		return (-1);
393	}
394	return (0);
395}
396
397static int
398mos_write_mcast(struct mos_softc *sc, u_char *hashtbl)
399{
400	struct usb_device_request req;
401	usb_error_t err;
402
403	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
404	req.bRequest = MOS_UR_WRITEREG;
405	USETW(req.wValue, 0);
406	USETW(req.wIndex, MOS_MCAST_TABLE);
407	USETW(req.wLength, 8);
408
409	err = uether_do_request(&sc->sc_ue, &req, hashtbl, 1000);
410
411	if (err) {
412		MOS_DPRINTFN("mos_reg_mcast error");
413		return (-1);
414	}
415	return (0);
416}
417
418static int
419mos_miibus_readreg(struct device *dev, int phy, int reg)
420{
421	struct mos_softc *sc = device_get_softc(dev);
422	uWord val;
423	int i, res, locked;
424
425	USETW(val, 0);
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
451static int
452mos_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
479static void
480mos_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 */
526static int
527mos_ifmedia_upd(struct ifnet *ifp)
528{
529	struct mos_softc *sc = ifp->if_softc;
530	struct mii_data *mii = GET_MII(sc);
531	struct mii_softc *miisc;
532
533	MOS_LOCK_ASSERT(sc, MA_OWNED);
534
535	sc->mos_link = 0;
536	if (mii->mii_instance) {
537		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
538		    mii_phy_reset(miisc);
539	}
540	mii_mediachg(mii);
541	return (0);
542}
543
544/*
545 * Report current media status.
546 */
547static void
548mos_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
549{
550	struct mos_softc *sc = ifp->if_softc;
551	struct mii_data *mii = GET_MII(sc);
552
553	MOS_LOCK(sc);
554	mii_pollstat(mii);
555	MOS_UNLOCK(sc);
556
557	ifmr->ifm_active = mii->mii_media_active;
558	ifmr->ifm_status = mii->mii_media_status;
559}
560
561static void
562mos_setpromisc(struct usb_ether *ue)
563{
564	struct mos_softc *sc = uether_getsc(ue);
565	struct ifnet *ifp = uether_getifp(ue);
566
567	uint8_t rxmode;
568
569	MOS_LOCK_ASSERT(sc, MA_OWNED);
570
571	rxmode = mos_reg_read_1(sc, MOS_CTL);
572
573	/* If we want promiscuous mode, set the allframes bit. */
574	if (ifp->if_flags & IFF_PROMISC) {
575		rxmode |= MOS_CTL_RX_PROMISC;
576	} else {
577		rxmode &= ~MOS_CTL_RX_PROMISC;
578	}
579
580	mos_reg_write_1(sc, MOS_CTL, rxmode);
581}
582
583
584
585static void
586mos_setmulti(struct usb_ether *ue)
587{
588	struct mos_softc *sc = uether_getsc(ue);
589	struct ifnet *ifp = uether_getifp(ue);
590	struct ifmultiaddr *ifma;
591
592	uint32_t h = 0;
593	uint8_t rxmode;
594	uint8_t hashtbl[8] = {0, 0, 0, 0, 0, 0, 0, 0};
595	int allmulti = 0;
596
597	MOS_LOCK_ASSERT(sc, MA_OWNED);
598
599	rxmode = mos_reg_read_1(sc, MOS_CTL);
600
601	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC)
602		allmulti = 1;
603
604	/* get all new ones */
605	if_maddr_rlock(ifp);
606	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
607		if (ifma->ifma_addr->sa_family != AF_LINK) {
608			allmulti = 1;
609			continue;
610		};
611		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
612		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
613		hashtbl[h / 8] |= 1 << (h % 8);
614	}
615	if_maddr_runlock(ifp);
616
617	/* now program new ones */
618	if (allmulti == 1) {
619		rxmode |= MOS_CTL_ALLMULTI;
620		mos_reg_write_1(sc, MOS_CTL, rxmode);
621	} else {
622		rxmode &= ~MOS_CTL_ALLMULTI;
623		mos_write_mcast(sc, (void *)&hashtbl);
624		mos_reg_write_1(sc, MOS_CTL, rxmode);
625	}
626}
627
628static void
629mos_reset(struct mos_softc *sc)
630{
631	uint8_t ctl;
632
633	ctl = mos_reg_read_1(sc, MOS_CTL);
634	ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB |
635	    MOS_CTL_RX_ENB);
636	/* Disable RX, TX, promiscuous and allmulticast mode */
637	mos_reg_write_1(sc, MOS_CTL, ctl);
638
639	/* Reset frame drop counter register to zero */
640	mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
641
642	/* Wait a little while for the chip to get its brains in order. */
643	usb_pause_mtx(&sc->sc_mtx, hz / 128);
644	return;
645}
646
647static void
648mos_chip_init(struct mos_softc *sc)
649{
650	int i;
651
652	/*
653	 * Rev.C devices have a pause threshold register which needs to be set
654	 * at startup.
655	 */
656	if (mos_reg_read_1(sc, MOS_PAUSE_TRHD) != -1) {
657		for (i = 0; i < MOS_PAUSE_REWRITES; i++)
658			mos_reg_write_1(sc, MOS_PAUSE_TRHD, 0);
659	}
660	sc->mos_phyaddrs[0] = 1;
661	sc->mos_phyaddrs[1] = 0xFF;
662}
663
664/*
665 * Probe for a MCS7x30 chip.
666 */
667static int
668mos_probe(device_t dev)
669{
670	struct usb_attach_arg *uaa = device_get_ivars(dev);
671        int retval;
672
673	if (uaa->usb_mode != USB_MODE_HOST)
674		return (ENXIO);
675	if (uaa->info.bConfigIndex != MOS_CONFIG_IDX)
676		return (ENXIO);
677	if (uaa->info.bIfaceIndex != MOS_IFACE_IDX)
678		return (ENXIO);
679
680	retval = usbd_lookup_id_by_uaa(mos_devs, sizeof(mos_devs), uaa);
681	return (retval);
682}
683
684/*
685 * Attach the interface. Allocate softc structures, do ifmedia
686 * setup and ethernet/BPF attach.
687 */
688static int
689mos_attach(device_t dev)
690{
691	struct usb_attach_arg *uaa = device_get_ivars(dev);
692	struct mos_softc *sc = device_get_softc(dev);
693	struct usb_ether *ue = &sc->sc_ue;
694	uint8_t iface_index;
695	int error;
696
697	sc->mos_flags = USB_GET_DRIVER_INFO(uaa);
698
699	device_set_usb_desc(dev);
700	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
701
702	iface_index = MOS_IFACE_IDX;
703	error = usbd_transfer_setup(uaa->device, &iface_index,
704	    sc->sc_xfer, mos_config, MOS_ENDPT_MAX,
705	    sc, &sc->sc_mtx);
706
707	if (error) {
708		device_printf(dev, "allocating USB transfers failed\n");
709		goto detach;
710	}
711	ue->ue_sc = sc;
712	ue->ue_dev = dev;
713	ue->ue_udev = uaa->device;
714	ue->ue_mtx = &sc->sc_mtx;
715	ue->ue_methods = &mos_ue_methods;
716
717
718	if (sc->mos_flags & MCS7730) {
719		MOS_DPRINTFN("model: MCS7730");
720	} else if (sc->mos_flags & MCS7830) {
721		MOS_DPRINTFN("model: MCS7830");
722	}
723	error = uether_ifattach(ue);
724	if (error) {
725		device_printf(dev, "could not attach interface\n");
726		goto detach;
727	}
728	return (0);
729
730
731detach:
732	mos_detach(dev);
733	return (ENXIO);
734}
735
736
737static void
738mos_attach_post(struct usb_ether *ue)
739{
740	struct mos_softc *sc = uether_getsc(ue);
741        int err;
742
743	/* Read MAC address, inform the world. */
744	err = mos_readmac(sc, ue->ue_eaddr);
745
746	if (err)
747	  MOS_DPRINTFN("couldn't get MAC address");
748
749	MOS_DPRINTFN("address: %s", ether_sprintf(ue->ue_eaddr));
750
751	mos_chip_init(sc);
752}
753
754static int
755mos_detach(device_t dev)
756{
757	struct mos_softc *sc = device_get_softc(dev);
758	struct usb_ether *ue = &sc->sc_ue;
759
760	usbd_transfer_unsetup(sc->sc_xfer, MOS_ENDPT_MAX);
761	uether_ifdetach(ue);
762	mtx_destroy(&sc->sc_mtx);
763
764	return (0);
765}
766
767
768
769
770/*
771 * A frame has been uploaded: pass the resulting mbuf chain up to
772 * the higher level protocols.
773 */
774static void
775mos_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
776{
777	struct mos_softc *sc = usbd_xfer_softc(xfer);
778	struct usb_ether *ue = &sc->sc_ue;
779	struct ifnet *ifp = uether_getifp(ue);
780
781	uint8_t rxstat = 0;
782	uint32_t actlen;
783	uint16_t pktlen = 0;
784	struct usb_page_cache *pc;
785
786	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
787	pc = usbd_xfer_get_frame(xfer, 0);
788
789	switch (USB_GET_STATE(xfer)) {
790	case USB_ST_TRANSFERRED:
791		MOS_DPRINTFN("actlen : %d", actlen);
792		if (actlen <= 1) {
793			ifp->if_ierrors++;
794			goto tr_setup;
795		}
796		/* evaluate status byte at the end */
797		usbd_copy_out(pc, actlen - sizeof(rxstat), &rxstat,
798		    sizeof(rxstat));
799
800		if (rxstat != MOS_RXSTS_VALID) {
801			MOS_DPRINTFN("erroneous frame received");
802			if (rxstat & MOS_RXSTS_SHORT_FRAME)
803				MOS_DPRINTFN("frame size less than 64 bytes");
804			if (rxstat & MOS_RXSTS_LARGE_FRAME) {
805				MOS_DPRINTFN("frame size larger than "
806				    "1532 bytes");
807			}
808			if (rxstat & MOS_RXSTS_CRC_ERROR)
809				MOS_DPRINTFN("CRC error");
810			if (rxstat & MOS_RXSTS_ALIGN_ERROR)
811				MOS_DPRINTFN("alignment error");
812			ifp->if_ierrors++;
813			goto tr_setup;
814		}
815		/* Remember the last byte was used for the status fields */
816		pktlen = actlen - 1;
817		if (pktlen < sizeof(struct ether_header)) {
818			MOS_DPRINTFN("error: pktlen %d is smaller "
819			    "than ether_header %zd", pktlen,
820			    sizeof(struct ether_header));
821			ifp->if_ierrors++;
822			goto tr_setup;
823		}
824		uether_rxbuf(ue, pc, 0, actlen);
825		/* FALLTHROUGH */
826	case USB_ST_SETUP:
827tr_setup:
828		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
829		usbd_transfer_submit(xfer);
830		uether_rxflush(ue);
831		return;
832	default:
833		MOS_DPRINTFN("bulk read error, %s", usbd_errstr(error));
834		if (error != USB_ERR_CANCELLED) {
835			usbd_xfer_set_stall(xfer);
836			goto tr_setup;
837		}
838		MOS_DPRINTFN("start rx %i", usbd_xfer_max_len(xfer));
839		return;
840	}
841}
842
843/*
844 * A frame was downloaded to the chip. It's safe for us to clean up
845 * the list buffers.
846 */
847static void
848mos_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
849{
850	struct mos_softc *sc = usbd_xfer_softc(xfer);
851	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
852	struct usb_page_cache *pc;
853	struct mbuf *m;
854
855
856
857	switch (USB_GET_STATE(xfer)) {
858	case USB_ST_TRANSFERRED:
859		MOS_DPRINTFN("transfer of complete");
860		ifp->if_opackets++;
861		/* FALLTHROUGH */
862	case USB_ST_SETUP:
863tr_setup:
864		/*
865		 * XXX: don't send anything if there is no link?
866		 */
867		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
868		if (m == NULL)
869			return;
870
871		pc = usbd_xfer_get_frame(xfer, 0);
872		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
873
874		usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
875
876
877		/*
878		 * if there's a BPF listener, bounce a copy
879		 * of this frame to him:
880		 */
881		BPF_MTAP(ifp, m);
882
883		m_freem(m);
884
885		usbd_transfer_submit(xfer);
886
887		ifp->if_opackets++;
888		return;
889	default:
890		MOS_DPRINTFN("usb error on tx: %s\n", usbd_errstr(error));
891		ifp->if_oerrors++;
892		if (error != USB_ERR_CANCELLED) {
893			usbd_xfer_set_stall(xfer);
894			goto tr_setup;
895		}
896		return;
897	}
898}
899
900static void
901mos_tick(struct usb_ether *ue)
902{
903	struct mos_softc *sc = uether_getsc(ue);
904	struct mii_data *mii = GET_MII(sc);
905
906	MOS_LOCK_ASSERT(sc, MA_OWNED);
907
908	mii_tick(mii);
909	if (!sc->mos_link && mii->mii_media_status & IFM_ACTIVE &&
910	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
911		MOS_DPRINTFN("got link");
912		sc->mos_link++;
913		mos_start(ue);
914	}
915}
916
917
918static void
919mos_start(struct usb_ether *ue)
920{
921	struct mos_softc *sc = uether_getsc(ue);
922
923	/*
924	 * start the USB transfers, if not already started:
925	 */
926	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_TX]);
927	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_RX]);
928	usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_INTR]);
929}
930
931static void
932mos_init(struct usb_ether *ue)
933{
934	struct mos_softc *sc = uether_getsc(ue);
935	struct ifnet *ifp = uether_getifp(ue);
936	uint8_t rxmode;
937
938	MOS_LOCK_ASSERT(sc, MA_OWNED);
939
940	/* Cancel pending I/O and free all RX/TX buffers. */
941	mos_reset(sc);
942
943	/* Write MAC address */
944	mos_writemac(sc, IF_LLADDR(ifp));
945
946	/* Read and set transmitter IPG values */
947	sc->mos_ipgs[0] = mos_reg_read_1(sc, MOS_IPG0);
948	sc->mos_ipgs[1] = mos_reg_read_1(sc, MOS_IPG1);
949	mos_reg_write_1(sc, MOS_IPG0, sc->mos_ipgs[0]);
950	mos_reg_write_1(sc, MOS_IPG1, sc->mos_ipgs[1]);
951
952	/*
953	 * Enable receiver and transmitter, bridge controls speed/duplex
954	 * mode
955	 */
956	rxmode = mos_reg_read_1(sc, MOS_CTL);
957	rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
958	rxmode &= ~(MOS_CTL_SLEEP);
959
960	mos_setpromisc(ue);
961
962	/* XXX: broadcast mode? */
963	mos_reg_write_1(sc, MOS_CTL, rxmode);
964
965	/* Load the multicast filter. */
966	mos_setmulti(ue);
967
968	ifp->if_drv_flags |= IFF_DRV_RUNNING;
969	mos_start(ue);
970}
971
972
973static void
974mos_intr_callback(struct usb_xfer *xfer, usb_error_t error)
975{
976	struct mos_softc *sc = usbd_xfer_softc(xfer);
977	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
978	struct usb_page_cache *pc;
979	uint32_t pkt;
980	int actlen;
981
982	ifp->if_oerrors++;
983
984	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
985	MOS_DPRINTFN("actlen %i", actlen);
986
987	switch (USB_GET_STATE(xfer)) {
988	case USB_ST_TRANSFERRED:
989
990		pc = usbd_xfer_get_frame(xfer, 0);
991		usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
992		/* FALLTHROUGH */
993	case USB_ST_SETUP:
994tr_setup:
995		return;
996	default:
997		if (error != USB_ERR_CANCELLED) {
998			usbd_xfer_set_stall(xfer);
999			goto tr_setup;
1000		}
1001		return;
1002	}
1003}
1004
1005
1006/*
1007 * Stop the adapter and free any mbufs allocated to the
1008 * RX and TX lists.
1009 */
1010static void
1011mos_stop(struct usb_ether *ue)
1012{
1013	struct mos_softc *sc = uether_getsc(ue);
1014	struct ifnet *ifp = uether_getifp(ue);
1015
1016	mos_reset(sc);
1017
1018	MOS_LOCK_ASSERT(sc, MA_OWNED);
1019	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1020
1021	/* stop all the transfers, if not already stopped */
1022	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_TX]);
1023	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_RX]);
1024	usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_INTR]);
1025
1026	sc->mos_link = 0;
1027}
1028