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