source: freebsd-mos-driver/if_mos.c@ 277

Last change on this file since 277 was 277, checked in by Rick van der Zwet, 14 years ago

Changes, based on instructions/review of Hans Petter Selasky.

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