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