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

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

Random bits of documentation.

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