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

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

Also allow writing of own mac if needed

File size: 25.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 DPRINTF("mos_readmac error");
370 return (-1);
371 }
372
373 return (0);
374}
375
376int
377mos_writemac(struct mos_softc *sc, uint8_t *mac)
378{
379 struct usb_device_request req;
380 usb_error_t err;
381
382 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
383 req.bRequest = MOS_UR_WRITEREG;
384 USETW(req.wValue, 0);
385 USETW(req.wIndex, MOS_MAC);
386 USETW(req.wLength, ETHER_ADDR_LEN);
387
388 err = uether_do_request(&sc->sc_ue, &req, mac, 1000);
389
390 if (err) {
391 DPRINTF("mos_writemac error");
392 return (-1);
393 }
394
395 return (0);
396}
397
398int
399mos_write_mcast(struct mos_softc *sc, u_char *hashtbl)
400{
401 struct usb_device_request req;
402 usb_error_t err;
403
404 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
405 req.bRequest = MOS_UR_WRITEREG;
406 USETW(req.wValue, 0);
407 USETW(req.wIndex, MOS_MCAST_TABLE);
408 USETW(req.wLength, 8);
409
410 err = uether_do_request(&sc->sc_ue, &req, hashtbl, 1000);
411
412 if (err) {
413 DPRINTF("mos_reg_mcast error\n");
414 return(-1);
415 }
416
417 return(0);
418}
419
420int
421mos_miibus_readreg(struct device *dev, int phy, int reg)
422{
423 struct mos_softc *sc = device_get_softc(dev);
424 uWord val;
425 int i,res, locked;
426 DPRINTF("%s enter\n", __func__);
427
428 USETW(val, 0);
429
430 locked = mtx_owned(&sc->sc_mtx);
431 if (!locked)
432 MOS_LOCK(sc);
433
434 mos_reg_write_2(sc, MOS_PHY_DATA, 0);
435 mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
436 MOS_PHYCTL_READ);
437 mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
438 MOS_PHYSTS_PENDING);
439
440 for (i = 0; i < MOS_TIMEOUT; i++) {
441 if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY)
442 break;
443 }
444 if (i == MOS_TIMEOUT) {
445 printf("%s: MII read timeout\n", "mos");
446 }
447
448 res = mos_reg_read_2(sc, MOS_PHY_DATA);
449
450 if (!locked)
451 MOS_UNLOCK(sc);
452 return (res);
453}
454
455int
456mos_miibus_writereg(device_t dev, int phy, int reg, int val)
457{
458 struct mos_softc *sc = device_get_softc(dev);
459 int i, locked;
460 DPRINTF("%s enter\n", __func__);
461
462 locked = mtx_owned(&sc->sc_mtx);
463 if (!locked)
464 MOS_LOCK(sc);
465
466 mos_reg_write_2(sc, MOS_PHY_DATA, val);
467 mos_reg_write_1(sc, MOS_PHY_CTL, (phy & MOS_PHYCTL_PHYADDR) |
468 MOS_PHYCTL_WRITE);
469 mos_reg_write_1(sc, MOS_PHY_STS, (reg & MOS_PHYSTS_PHYREG) |
470 MOS_PHYSTS_PENDING);
471
472 for (i = 0; i < MOS_TIMEOUT; i++) {
473 if (mos_reg_read_1(sc, MOS_PHY_STS) & MOS_PHYSTS_READY)
474 break;
475 }
476 if (i == MOS_TIMEOUT) {
477 printf("%s: MII write timeout\n", "mos");
478 }
479
480 if (!locked)
481 MOS_UNLOCK(sc);
482 return 0;
483}
484
485void
486mos_miibus_statchg(device_t dev)
487{
488 struct mos_softc *sc = device_get_softc(dev);
489 struct mii_data *mii = GET_MII(sc);
490 int val, err, locked;
491 DPRINTF("%s enter\n", __func__);
492
493 locked = mtx_owned(&sc->sc_mtx);
494 if (!locked)
495 MOS_LOCK(sc);
496
497 /* disable RX, TX prior to changing FDX, SPEEDSEL */
498 val = mos_reg_read_1(sc, MOS_CTL);
499 val &= ~(MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
500 mos_reg_write_1(sc, MOS_CTL, val);
501
502 /* reset register which counts dropped frames */
503 mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
504
505 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
506 val |= MOS_CTL_FDX_ENB;
507 else
508 val &= ~(MOS_CTL_FDX_ENB);
509
510 switch (IFM_SUBTYPE(mii->mii_media_active)) {
511 case IFM_100_TX:
512 val |= MOS_CTL_SPEEDSEL;
513 break;
514 case IFM_10_T:
515 val &= ~(MOS_CTL_SPEEDSEL);
516 break;
517 }
518
519 /* re-enable TX, RX */
520 val |= (MOS_CTL_TX_ENB | MOS_CTL_RX_ENB);
521 err = mos_reg_write_1(sc, MOS_CTL, val);
522
523 if (err)
524 printf("%s: media change failed\n", "mos");
525
526 if (!locked)
527 MOS_UNLOCK(sc);
528}
529
530/*
531 * Set media options.
532 */
533int
534mos_ifmedia_upd(struct ifnet *ifp)
535{
536 struct mos_softc *sc = ifp->if_softc;
537 struct mii_data *mii = GET_MII(sc);
538
539 MOS_LOCK_ASSERT(sc, MA_OWNED);
540
541 sc->mos_link = 0;
542 if (mii->mii_instance) {
543 struct mii_softc *miisc;
544
545 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
546 mii_phy_reset(miisc);
547 }
548 mii_mediachg(mii);
549 return (0);
550}
551
552/*
553 * Report current media status.
554 */
555void
556mos_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
557{
558 struct mos_softc *sc = ifp->if_softc;
559 struct mii_data *mii = GET_MII(sc);
560
561 MOS_LOCK(sc);
562 mii_pollstat(mii);
563 MOS_UNLOCK(sc);
564
565 ifmr->ifm_active = mii->mii_media_active;
566 ifmr->ifm_status = mii->mii_media_status;
567}
568
569void
570mos_setmulti(struct usb_ether *ue)
571{
572 struct mos_softc *sc = uether_getsc(ue);
573 struct ifnet *ifp = uether_getifp(ue);
574 struct ifmultiaddr *ifma;
575
576 u_int32_t h = 0;
577 u_int8_t rxmode;
578 u_int8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
579 int allmulti = 0;
580 DPRINTF("%s enter\n", __func__);
581
582 MOS_LOCK_ASSERT(sc, MA_OWNED);
583
584 rxmode = mos_reg_read_1(sc, MOS_CTL);
585
586 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC)
587 allmulti = 1;
588
589 /* get all new ones */
590 if_maddr_rlock(ifp);
591 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
592 if (ifma->ifma_addr->sa_family != AF_LINK) {
593 allmulti = 1;
594 continue;
595 };
596 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
597 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
598 //h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
599 hashtbl[h / 8] |= 1 << (h % 8);
600 }
601 if_maddr_runlock(ifp);
602
603 /* now program new ones */
604 if (allmulti == 1) {
605 rxmode |= MOS_CTL_ALLMULTI;
606 mos_reg_write_1(sc, MOS_CTL, rxmode);
607 } else {
608 rxmode &= ~MOS_CTL_ALLMULTI;
609 ifp->if_flags &= ~IFF_ALLMULTI;
610 mos_write_mcast(sc, (void *)&hashtbl);
611 mos_reg_write_1(sc, MOS_CTL, rxmode);
612 }
613}
614
615void
616mos_reset(struct mos_softc *sc)
617{
618 u_int8_t ctl;
619 DPRINTF("%s enter\n", __func__);
620
621 ctl = mos_reg_read_1(sc, MOS_CTL);
622 ctl &= ~(MOS_CTL_RX_PROMISC | MOS_CTL_ALLMULTI | MOS_CTL_TX_ENB |
623 MOS_CTL_RX_ENB);
624 /* Disable RX, TX, promiscuous and allmulticast mode */
625 mos_reg_write_1(sc, MOS_CTL, ctl);
626
627 /* Reset frame drop counter register to zero */
628 mos_reg_write_1(sc, MOS_FRAME_DROP_CNT, 0);
629
630 /* Wait a little while for the chip to get its brains in order. */
631 DELAY(1000);
632 return;
633}
634
635void
636mos_chip_init(struct mos_softc *sc)
637{
638 int i;
639 DPRINTF("%s enter\n", __func__);
640
641 /*
642 * Rev.C devices have a pause threshold register which needs to be set
643 * at startup.
644 */
645 if (mos_reg_read_1(sc, MOS_PAUSE_TRHD) != -1) {
646 for (i=0;i<MOS_PAUSE_REWRITES;i++)
647 mos_reg_write_1(sc, MOS_PAUSE_TRHD, 0);
648 }
649
650 sc->mos_phyaddrs[0] = 1; sc->mos_phyaddrs[1] = 0xFF;
651}
652
653/*
654 * Probe for a MCS7x30 chip.
655 */
656int
657mos_probe(device_t dev)
658{
659 DPRINTFN(11, "mos: probe\n");
660 struct usb_attach_arg *uaa = device_get_ivars(dev);
661
662 if (uaa->usb_mode != USB_MODE_HOST) {
663 DPRINTFN(11, "mos: not USB_MODE_HOST\n");
664 return (ENXIO);
665 }
666 //uint8_t bIfaceIndex;
667 //uint8_t bIfaceNum;
668 //uint8_t bConfigIndex;
669 //uint8_t bConfigNum;
670
671 if (uaa->info.bConfigNum != MOS_CONFIG_NO) {
672 DPRINTFN(11, "mos: not MOS_CONFIG_NO %i vs %i\n", uaa->info.bConfigNum , MOS_CONFIG_NO);
673 return (ENXIO);
674 }
675 if (uaa->info.bIfaceIndex != MOS_IFACE_IDX) {
676 DPRINTFN(11, "mos: not MOS_IFACE_IDX %i vs %i\n", uaa->info.bIfaceIndex, MOS_IFACE_IDX);
677 return (ENXIO);
678 }
679
680 DPRINTFN(11, "mos: probe by generic means\n");
681 int retval = usbd_lookup_id_by_uaa(mos_devs, sizeof(mos_devs), uaa);
682 DPRINTFN(11, "mos: probe retval: %i (ENXIO:%i)\n", retval, ENXIO);
683 return retval;
684}
685
686/*
687 * Attach the interface. Allocate softc structures, do ifmedia
688 * setup and ethernet/BPF attach.
689 */
690int
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
697 uint8_t iface_index;
698 int error;
699
700 sc->mos_flags = USB_GET_DRIVER_INFO(uaa);
701
702 device_set_usb_desc(dev);
703 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
704
705 iface_index = MOS_IFACE_IDX;
706 error = usbd_transfer_setup(uaa->device, &iface_index,
707 sc->sc_xfer, mos_config, MOS_CONFIG_NO,
708 sc, &sc->sc_mtx);
709
710 if (error) {
711 device_printf(dev, "allocating USB transfers failed\n");
712 goto detach;
713 }
714
715 ue->ue_sc = sc;
716 ue->ue_dev = dev;
717 ue->ue_udev = uaa->device;
718 ue->ue_mtx = &sc->sc_mtx;
719 ue->ue_methods = &mos_ue_methods;
720
721
722 DPRINTF("%s:", "mos");
723
724 if (sc->mos_flags & MCS7730) {
725 DPRINTF(" MCS7730");
726 } else if (sc->mos_flags & MCS7830) {
727 DPRINTF(" MCS7830");
728 }
729
730 error = uether_ifattach(ue);
731 if (error) {
732 device_printf(dev, "could not attach interface\n");
733 goto detach;
734 }
735 return (0); /* success */
736
737
738detach:
739 mos_detach(dev);
740 return (ENXIO); /* failure */
741}
742
743
744void
745mos_attach_post(struct usb_ether *ue)
746{
747 struct mos_softc *sc = uether_getsc(ue);
748 /*
749 * Read MAC address, inform the world.
750 */
751 int err = mos_readmac(sc, ue->ue_eaddr);
752 if (err) {
753 printf("couldn't get MAC address\n");
754 }
755 DPRINTF("address: %s\n", ether_sprintf(ue->ue_eaddr));
756
757 mos_chip_init(sc);
758
759 sc->mos_attached = 1;
760}
761
762int
763mos_detach(device_t dev)
764{
765 struct mos_softc *sc = device_get_softc(dev);
766 struct usb_ether *ue = &sc->sc_ue;
767
768 usbd_transfer_unsetup(sc->sc_xfer, MOS_CONFIG_NO);
769 uether_ifdetach(ue);
770 mtx_destroy(&sc->sc_mtx);
771
772 return (0);
773}
774
775
776
777
778/*
779 * A frame has been uploaded: pass the resulting mbuf chain up to
780 * the higher level protocols.
781 */
782void
783mos_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
784{
785 struct mos_softc *sc = usbd_xfer_softc(xfer);
786 struct usb_ether *ue = &sc->sc_ue;
787 struct ifnet *ifp = uether_getifp(ue);
788
789 u_int8_t rxstat = 0;
790 u_int32_t actlen;
791 u_int16_t pktlen = 0;
792 struct mbuf *stat;
793 uint8_t status;
794 struct usb_page_cache *pc;
795
796 DPRINTF("%s: %s: enter\n", "mos",__func__);
797
798 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
799 pc = usbd_xfer_get_frame(xfer, 0);
800
801 status = USB_GET_STATE(xfer);
802 if (status != USB_ST_SETUP) {
803 if (status == USB_ST_TRANSFERRED) {
804 if (actlen <= 1)
805 goto done;
806
807 usbd_copy_out(pc, actlen - sizeof(stat), &stat,
808 sizeof(stat));
809
810 /* evaluate status byte at the end */
811 pktlen = actlen - 1;
812 //rxstat = stat[pktlen] & MOS_RXSTS_MASK;
813 m_freem(stat);
814
815 if (rxstat != MOS_RXSTS_VALID) {
816 DPRINTF("%s: erroneous frame received: ",
817 "mos");
818 if (rxstat & MOS_RXSTS_SHORT_FRAME)
819 DPRINTF("frame size less than 64 bytes\n");
820 if (rxstat & MOS_RXSTS_LARGE_FRAME)
821 DPRINTF("frame size larger than 1532 bytes\n");
822 if (rxstat & MOS_RXSTS_CRC_ERROR)
823 DPRINTF("CRC error\n");
824 if (rxstat & MOS_RXSTS_ALIGN_ERROR)
825 DPRINTF("alignment error\n");
826 ifp->if_ierrors++;
827 goto done;
828 }
829
830 if ( pktlen < sizeof(struct ether_header) ) {
831 ifp->if_ierrors++;
832 goto done;
833 }
834
835 uether_rxbuf(ue, pc, 0, actlen);
836
837 } else {
838 DPRINTF("bulk read error, %s\n", usbd_errstr(error));
839 if (error == USB_ERR_CANCELLED) {
840 return;
841 } else {
842 /* try to clear stall first */
843 usbd_xfer_set_stall(xfer);
844 goto done;
845 }
846 }
847 }
848done:
849 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
850 usbd_transfer_submit(xfer);
851 uether_rxflush(ue);
852 DPRINTF("%s: %s: start rx\n", "mos", __func__);
853
854 return;
855}
856
857/*
858 * A frame was downloaded to the chip. It's safe for us to clean up
859 * the list buffers.
860 */
861
862void
863mos_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
864{
865 struct mos_softc *sc = usbd_xfer_softc(xfer);
866 struct ifnet *ifp = uether_getifp(&sc->sc_ue);
867 struct usb_page_cache *pc;
868 struct mbuf *m;
869 uint8_t status;
870 int actlen;
871
872 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
873 pc = usbd_xfer_get_frame(xfer, 0);
874
875 status = USB_GET_STATE(xfer);
876
877 if (status != USB_ST_SETUP) {
878 if (status == USB_ST_TRANSFERRED) {
879 DPRINTF("transfer of %d bytes complete\n", actlen);
880 ifp->if_opackets++;
881 } else {
882 DPRINTF("%s: usb error on tx: %s\n", "mos", usbd_errstr(status));
883 ifp->if_oerrors++;
884 if (error == USB_ERR_CANCELLED) {
885 return;
886 }
887 }
888 }
889
890 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
891
892 usbd_xfer_set_frame_len(xfer, 0, m->m_pkthdr.len);
893
894 usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
895
896 /*
897 * if there's a BPF listener, bounce a copy
898 * of this frame to him:
899 */
900 BPF_MTAP(ifp, m);
901
902 m_freem(m);
903
904 usbd_transfer_submit(xfer);
905
906 ifp->if_opackets++;
907 return;
908}
909
910void
911mos_tick(struct usb_ether *ue)
912{
913 struct mos_softc *sc = uether_getsc(ue);
914 struct mii_data *mii = GET_MII(sc);
915
916 MOS_LOCK_ASSERT(sc, MA_OWNED);
917 DPRINTF("%s: %s: enter\n", "mos", __func__);
918
919 mii_tick(mii);
920 if (!sc->mos_link && mii->mii_media_status & IFM_ACTIVE &&
921 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
922 DPRINTF("%s: %s: got link\n",
923 "mos", __func__);
924 sc->mos_link++;
925 mos_start(ue);
926 }
927}
928
929
930//int
931//mos_encap(struct mos_softc *sc, struct mbuf *m, int idx)
932//{
933// struct mos_chain *c;
934// int err;
935// int length;
936//
937// c = &sc->mos_cdata.mos_tx_chain[idx];
938//
939// m_copydata(m, 0, m->m_pkthdr.len, c->mos_buf);
940// length = m->m_pkthdr.len;
941//
942// c->mos_mbuf = m;
943//
944// usbd_setup_xfer(c->mos_xfer, sc->mos_ep[MOS_ENDPT_TX],
945// c, c->mos_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
946// 10000, mos_txeof);
947//
948// /* Transmit */
949// err = usbd_transfer(c->mos_xfer);
950// if (err != USBD_IN_PROGRESS) {
951// mos_stop(sc);
952// return(EIO);
953// }
954//
955// sc->mos_cdata.mos_tx_cnt++;
956//
957// return(0);
958//}
959
960void
961mos_start(struct usb_ether *ue)
962{
963 struct mos_softc *sc = uether_getsc(ue);
964
965 /*
966 * start the USB transfers, if not already started:
967 */
968 usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_TX]);
969 usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_RX]);
970 usbd_transfer_start(sc->sc_xfer[MOS_ENDPT_INTR]);
971}
972
973void
974mos_init(struct usb_ether *ue)
975{
976 struct mos_softc *sc = uether_getsc(ue);
977 struct ifnet *ifp = uether_getifp(ue);
978 u_int8_t rxmode;
979 DPRINTF("%s enter\n", __func__);
980
981 MOS_LOCK_ASSERT(sc, MA_OWNED);
982
983 /*
984 * Cancel pending I/O and free all RX/TX buffers.
985 */
986 mos_reset(sc);
987
988 /*
989 * Write MAC address
990 */
991 mos_writemac(sc, IF_LLADDR(ifp));
992
993
994
995 /* Read and set transmitter IPG values */
996 sc->mos_ipgs[0] = mos_reg_read_1(sc, MOS_IPG0);
997 sc->mos_ipgs[1] = mos_reg_read_1(sc, MOS_IPG1);
998 mos_reg_write_1(sc, MOS_IPG0, sc->mos_ipgs[0]);
999 mos_reg_write_1(sc, MOS_IPG1, sc->mos_ipgs[1]);
1000
1001 /* Enable receiver and transmitter, bridge controls speed/duplex mode */
1002 rxmode = mos_reg_read_1(sc, MOS_CTL);
1003 rxmode |= MOS_CTL_RX_ENB | MOS_CTL_TX_ENB | MOS_CTL_BS_ENB;
1004 rxmode &= ~(MOS_CTL_SLEEP);
1005
1006 /* If we want promiscuous mode, set the allframes bit. */
1007 if (ifp->if_flags & IFF_PROMISC)
1008 rxmode |= MOS_CTL_RX_PROMISC;
1009
1010 /* XXX: broadcast mode? */
1011
1012 mos_reg_write_1(sc, MOS_CTL, rxmode);
1013
1014 /* Load the multicast filter. */
1015 mos_setmulti(ue);
1016
1017 usbd_xfer_set_stall(sc->sc_xfer[MOS_ENDPT_TX]);
1018
1019 ifp->if_flags |= IFF_DRV_RUNNING;
1020 mos_start(ue);
1021}
1022
1023
1024void
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);
1029 int actlen;
1030
1031 ifp->if_oerrors++;
1032 printf("%s: watchdog timeout\n", "mos");
1033
1034 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1035
1036 //XXX: Porting, how? mos_txeof(c->mos_xfer, c, stat);
1037}
1038
1039
1040/*
1041 * Stop the adapter and free any mbufs allocated to the
1042 * RX and TX lists.
1043 */
1044void
1045mos_stop(struct usb_ether *ue)
1046{
1047 struct mos_softc *sc = uether_getsc(ue);
1048 struct ifnet *ifp = uether_getifp(ue);
1049
1050 mos_reset(sc);
1051
1052 MOS_LOCK_ASSERT(sc, MA_OWNED);
1053 ifp->if_flags &= ~IFF_DRV_RUNNING;
1054
1055 /*
1056 * stop all the transfers, if not already stopped:
1057 */
1058 usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_TX]);
1059 usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_RX]);
1060 usbd_transfer_stop(sc->sc_xfer[MOS_ENDPT_INTR]);
1061
1062 sc->mos_link = 0;
1063}
1064
Note: See TracBrowser for help on using the repository browser.