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