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

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

Nasty restructure:

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