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

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

Damm this is slightly inconvient. How to maintain a in-tree and out-tree compiling module at the same time?

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