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