1 /* $NetBSD: if_udav.c,v 1.2 2003/09/04 15:17:38 tsutsui Exp $ */
2 /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */
3 /*-
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Copyright (c) 2003
7 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 */
34
35 /*
36 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
37 * The spec can be found at the following url.
38 * http://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/DM9601-DS-P01-930914.pdf
39 */
40
41 /*
42 * TODO:
43 * Interrupt Endpoint support
44 * External PHYs
45 */
46
47 #include <sys/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/socket.h>
54 #include <sys/kernel.h>
55 #include <sys/bus.h>
56 #include <sys/module.h>
57 #include <sys/lock.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
61 #include <sys/sx.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
65 #include <sys/priv.h>
66
67 #include <net/if.h>
68 #include <net/if_var.h>
69 #include <net/if_media.h>
70
71 #include <dev/mii/mii.h>
72 #include <dev/mii/miivar.h>
73
74 #include <dev/usb/usb.h>
75 #include <dev/usb/usbdi.h>
76 #include <dev/usb/usbdi_util.h>
77 #include "usbdevs.h"
78
79 #include "miibus_if.h"
80
81 #define USB_DEBUG_VAR udav_debug
82 #include <dev/usb/usb_debug.h>
83 #include <dev/usb/usb_process.h>
84
85 #include <dev/usb/net/usb_ethernet.h>
86 #include <dev/usb/net/if_udavreg.h>
87
88 /* prototypes */
89
90 static device_probe_t udav_probe;
91 static device_attach_t udav_attach;
92 static device_detach_t udav_detach;
93
94 static usb_callback_t udav_bulk_write_callback;
95 static usb_callback_t udav_bulk_read_callback;
96 static usb_callback_t udav_intr_callback;
97
98 static uether_fn_t udav_attach_post;
99 static uether_fn_t udav_init;
100 static uether_fn_t udav_stop;
101 static uether_fn_t udav_start;
102 static uether_fn_t udav_tick;
103 static uether_fn_t udav_setmulti;
104 static uether_fn_t udav_setpromisc;
105
106 static int udav_csr_read(struct udav_softc *, uint16_t, void *, int);
107 static int udav_csr_write(struct udav_softc *, uint16_t, void *, int);
108 static uint8_t udav_csr_read1(struct udav_softc *, uint16_t);
109 static int udav_csr_write1(struct udav_softc *, uint16_t, uint8_t);
110 static void udav_reset(struct udav_softc *);
111 static int udav_ifmedia_upd(if_t);
112 static void udav_ifmedia_status(if_t, struct ifmediareq *);
113
114 static miibus_readreg_t udav_miibus_readreg;
115 static miibus_writereg_t udav_miibus_writereg;
116 static miibus_statchg_t udav_miibus_statchg;
117
118 static const struct usb_config udav_config[UDAV_N_TRANSFER] = {
119 [UDAV_BULK_DT_WR] = {
120 .type = UE_BULK,
121 .endpoint = UE_ADDR_ANY,
122 .direction = UE_DIR_OUT,
123 .bufsize = (MCLBYTES + 2),
124 .flags = {.pipe_bof = 1,.force_short_xfer = 1,},
125 .callback = udav_bulk_write_callback,
126 .timeout = 10000, /* 10 seconds */
127 },
128
129 [UDAV_BULK_DT_RD] = {
130 .type = UE_BULK,
131 .endpoint = UE_ADDR_ANY,
132 .direction = UE_DIR_IN,
133 .bufsize = (MCLBYTES + 3),
134 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
135 .callback = udav_bulk_read_callback,
136 .timeout = 0, /* no timeout */
137 },
138
139 [UDAV_INTR_DT_RD] = {
140 .type = UE_INTERRUPT,
141 .endpoint = UE_ADDR_ANY,
142 .direction = UE_DIR_IN,
143 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
144 .bufsize = 0, /* use wMaxPacketSize */
145 .callback = udav_intr_callback,
146 },
147 };
148
149 static device_method_t udav_methods[] = {
150 /* Device interface */
151 DEVMETHOD(device_probe, udav_probe),
152 DEVMETHOD(device_attach, udav_attach),
153 DEVMETHOD(device_detach, udav_detach),
154
155 /* MII interface */
156 DEVMETHOD(miibus_readreg, udav_miibus_readreg),
157 DEVMETHOD(miibus_writereg, udav_miibus_writereg),
158 DEVMETHOD(miibus_statchg, udav_miibus_statchg),
159
160 DEVMETHOD_END
161 };
162
163 static driver_t udav_driver = {
164 .name = "udav",
165 .methods = udav_methods,
166 .size = sizeof(struct udav_softc),
167 };
168
169 static const STRUCT_USB_HOST_ID udav_devs[] = {
170 /* ShanTou DM9601 USB NIC */
171 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)},
172 /* ShanTou ST268 USB NIC */
173 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)},
174 /* Corega USB-TXC */
175 {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)},
176 /* ShanTou AMD8515 USB NIC */
177 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515, 0)},
178 /* Kontron AG USB Ethernet */
179 {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_DM9601, 0)},
180 {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_JP1082,
181 UDAV_FLAG_NO_PHY)},
182 };
183
184 DRIVER_MODULE(udav, uhub, udav_driver, NULL, NULL);
185 DRIVER_MODULE(miibus, udav, miibus_driver, 0, 0);
186 MODULE_DEPEND(udav, uether, 1, 1, 1);
187 MODULE_DEPEND(udav, usb, 1, 1, 1);
188 MODULE_DEPEND(udav, ether, 1, 1, 1);
189 MODULE_DEPEND(udav, miibus, 1, 1, 1);
190 MODULE_VERSION(udav, 1);
191 USB_PNP_HOST_INFO(udav_devs);
192
193 static const struct usb_ether_methods udav_ue_methods = {
194 .ue_attach_post = udav_attach_post,
195 .ue_start = udav_start,
196 .ue_init = udav_init,
197 .ue_stop = udav_stop,
198 .ue_tick = udav_tick,
199 .ue_setmulti = udav_setmulti,
200 .ue_setpromisc = udav_setpromisc,
201 .ue_mii_upd = udav_ifmedia_upd,
202 .ue_mii_sts = udav_ifmedia_status,
203 };
204
205 static const struct usb_ether_methods udav_ue_methods_nophy = {
206 .ue_attach_post = udav_attach_post,
207 .ue_start = udav_start,
208 .ue_init = udav_init,
209 .ue_stop = udav_stop,
210 .ue_setmulti = udav_setmulti,
211 .ue_setpromisc = udav_setpromisc,
212 };
213
214 #ifdef USB_DEBUG
215 static int udav_debug = 0;
216
217 static SYSCTL_NODE(_hw_usb, OID_AUTO, udav, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
218 "USB udav");
219 SYSCTL_INT(_hw_usb_udav, OID_AUTO, debug, CTLFLAG_RWTUN, &udav_debug, 0,
220 "Debug level");
221 #endif
222
223 #define UDAV_SETBIT(sc, reg, x) \
224 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
225
226 #define UDAV_CLRBIT(sc, reg, x) \
227 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
228
229 static void
udav_attach_post(struct usb_ether * ue)230 udav_attach_post(struct usb_ether *ue)
231 {
232 struct udav_softc *sc = uether_getsc(ue);
233
234 /* reset the adapter */
235 udav_reset(sc);
236
237 /* Get Ethernet Address */
238 udav_csr_read(sc, UDAV_PAR, ue->ue_eaddr, ETHER_ADDR_LEN);
239 }
240
241 static int
udav_probe(device_t dev)242 udav_probe(device_t dev)
243 {
244 struct usb_attach_arg *uaa = device_get_ivars(dev);
245
246 if (uaa->usb_mode != USB_MODE_HOST)
247 return (ENXIO);
248 if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX)
249 return (ENXIO);
250 if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX)
251 return (ENXIO);
252
253 return (usbd_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa));
254 }
255
256 static int
udav_attach(device_t dev)257 udav_attach(device_t dev)
258 {
259 struct usb_attach_arg *uaa = device_get_ivars(dev);
260 struct udav_softc *sc = device_get_softc(dev);
261 struct usb_ether *ue = &sc->sc_ue;
262 uint8_t iface_index;
263 int error;
264
265 sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
266
267 device_set_usb_desc(dev);
268
269 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
270
271 iface_index = UDAV_IFACE_INDEX;
272 error = usbd_transfer_setup(uaa->device, &iface_index,
273 sc->sc_xfer, udav_config, UDAV_N_TRANSFER, sc, &sc->sc_mtx);
274 if (error) {
275 device_printf(dev, "allocating USB transfers failed\n");
276 goto detach;
277 }
278
279 /*
280 * The JP1082 has an unusable PHY and provides no link information.
281 */
282 if (sc->sc_flags & UDAV_FLAG_NO_PHY) {
283 ue->ue_methods = &udav_ue_methods_nophy;
284 sc->sc_flags |= UDAV_FLAG_LINK;
285 } else {
286 ue->ue_methods = &udav_ue_methods;
287 }
288
289 ue->ue_sc = sc;
290 ue->ue_dev = dev;
291 ue->ue_udev = uaa->device;
292 ue->ue_mtx = &sc->sc_mtx;
293
294 error = uether_ifattach(ue);
295 if (error) {
296 device_printf(dev, "could not attach interface\n");
297 goto detach;
298 }
299
300 return (0); /* success */
301
302 detach:
303 udav_detach(dev);
304 return (ENXIO); /* failure */
305 }
306
307 static int
udav_detach(device_t dev)308 udav_detach(device_t dev)
309 {
310 struct udav_softc *sc = device_get_softc(dev);
311 struct usb_ether *ue = &sc->sc_ue;
312
313 usbd_transfer_unsetup(sc->sc_xfer, UDAV_N_TRANSFER);
314 uether_ifdetach(ue);
315 mtx_destroy(&sc->sc_mtx);
316
317 return (0);
318 }
319
320 #if 0
321 static int
322 udav_mem_read(struct udav_softc *sc, uint16_t offset, void *buf,
323 int len)
324 {
325 struct usb_device_request req;
326
327 len &= 0xff;
328
329 req.bmRequestType = UT_READ_VENDOR_DEVICE;
330 req.bRequest = UDAV_REQ_MEM_READ;
331 USETW(req.wValue, 0x0000);
332 USETW(req.wIndex, offset);
333 USETW(req.wLength, len);
334
335 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
336 }
337
338 static int
339 udav_mem_write(struct udav_softc *sc, uint16_t offset, void *buf,
340 int len)
341 {
342 struct usb_device_request req;
343
344 len &= 0xff;
345
346 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
347 req.bRequest = UDAV_REQ_MEM_WRITE;
348 USETW(req.wValue, 0x0000);
349 USETW(req.wIndex, offset);
350 USETW(req.wLength, len);
351
352 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
353 }
354
355 static int
356 udav_mem_write1(struct udav_softc *sc, uint16_t offset,
357 uint8_t ch)
358 {
359 struct usb_device_request req;
360
361 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
362 req.bRequest = UDAV_REQ_MEM_WRITE1;
363 USETW(req.wValue, ch);
364 USETW(req.wIndex, offset);
365 USETW(req.wLength, 0x0000);
366
367 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
368 }
369 #endif
370
371 static int
udav_csr_read(struct udav_softc * sc,uint16_t offset,void * buf,int len)372 udav_csr_read(struct udav_softc *sc, uint16_t offset, void *buf, int len)
373 {
374 struct usb_device_request req;
375
376 len &= 0xff;
377
378 req.bmRequestType = UT_READ_VENDOR_DEVICE;
379 req.bRequest = UDAV_REQ_REG_READ;
380 USETW(req.wValue, 0x0000);
381 USETW(req.wIndex, offset);
382 USETW(req.wLength, len);
383
384 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
385 }
386
387 static int
udav_csr_write(struct udav_softc * sc,uint16_t offset,void * buf,int len)388 udav_csr_write(struct udav_softc *sc, uint16_t offset, void *buf, int len)
389 {
390 struct usb_device_request req;
391
392 offset &= 0xff;
393 len &= 0xff;
394
395 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
396 req.bRequest = UDAV_REQ_REG_WRITE;
397 USETW(req.wValue, 0x0000);
398 USETW(req.wIndex, offset);
399 USETW(req.wLength, len);
400
401 return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
402 }
403
404 static uint8_t
udav_csr_read1(struct udav_softc * sc,uint16_t offset)405 udav_csr_read1(struct udav_softc *sc, uint16_t offset)
406 {
407 uint8_t val;
408
409 udav_csr_read(sc, offset, &val, 1);
410 return (val);
411 }
412
413 static int
udav_csr_write1(struct udav_softc * sc,uint16_t offset,uint8_t ch)414 udav_csr_write1(struct udav_softc *sc, uint16_t offset,
415 uint8_t ch)
416 {
417 struct usb_device_request req;
418
419 offset &= 0xff;
420
421 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
422 req.bRequest = UDAV_REQ_REG_WRITE1;
423 USETW(req.wValue, ch);
424 USETW(req.wIndex, offset);
425 USETW(req.wLength, 0x0000);
426
427 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000));
428 }
429
430 static void
udav_init(struct usb_ether * ue)431 udav_init(struct usb_ether *ue)
432 {
433 struct udav_softc *sc = ue->ue_sc;
434 if_t ifp = uether_getifp(&sc->sc_ue);
435
436 UDAV_LOCK_ASSERT(sc, MA_OWNED);
437
438 /*
439 * Cancel pending I/O
440 */
441 udav_stop(ue);
442
443 /* set MAC address */
444 udav_csr_write(sc, UDAV_PAR, if_getlladdr(ifp), ETHER_ADDR_LEN);
445
446 /* initialize network control register */
447
448 /* disable loopback */
449 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
450
451 /* Initialize RX control register */
452 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
453
454 /* load multicast filter and update promiscious mode bit */
455 udav_setpromisc(ue);
456
457 /* enable RX */
458 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
459
460 /* clear POWER_DOWN state of internal PHY */
461 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
462 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
463
464 usbd_xfer_set_stall(sc->sc_xfer[UDAV_BULK_DT_WR]);
465
466 if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
467 udav_start(ue);
468 }
469
470 static void
udav_reset(struct udav_softc * sc)471 udav_reset(struct udav_softc *sc)
472 {
473 int i;
474
475 /* Select PHY */
476 #if 1
477 /*
478 * XXX: force select internal phy.
479 * external phy routines are not tested.
480 */
481 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
482 #else
483 if (sc->sc_flags & UDAV_EXT_PHY)
484 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
485 else
486 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
487 #endif
488
489 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
490
491 for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
492 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
493 break;
494 if (uether_pause(&sc->sc_ue, hz / 100))
495 break;
496 }
497
498 uether_pause(&sc->sc_ue, hz / 100);
499 }
500
501 static u_int
udav_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)502 udav_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
503 {
504 uint8_t *hashtbl = arg;
505 int h;
506
507 h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 26;
508 hashtbl[h / 8] |= 1 << (h % 8);
509
510 return (1);
511 }
512
513 static void
udav_setmulti(struct usb_ether * ue)514 udav_setmulti(struct usb_ether *ue)
515 {
516 struct udav_softc *sc = ue->ue_sc;
517 if_t ifp = uether_getifp(&sc->sc_ue);
518 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
519
520 UDAV_LOCK_ASSERT(sc, MA_OWNED);
521
522 if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
523 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
524 return;
525 }
526
527 /* first, zot all the existing hash bits */
528 memset(hashtbl, 0x00, sizeof(hashtbl));
529 hashtbl[7] |= 0x80; /* broadcast address */
530 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl));
531
532 /* now program new ones */
533 if_foreach_llmaddr(ifp, udav_hash_maddr, hashtbl);
534
535 /* disable all multicast */
536 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
537
538 /* write hash value to the register */
539 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl));
540 }
541
542 static void
udav_setpromisc(struct usb_ether * ue)543 udav_setpromisc(struct usb_ether *ue)
544 {
545 struct udav_softc *sc = ue->ue_sc;
546 if_t ifp = uether_getifp(&sc->sc_ue);
547 uint8_t rxmode;
548
549 rxmode = udav_csr_read1(sc, UDAV_RCR);
550 rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC);
551
552 if (if_getflags(ifp) & IFF_PROMISC)
553 rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC;
554 else if (if_getflags(ifp) & IFF_ALLMULTI)
555 rxmode |= UDAV_RCR_ALL;
556
557 /* write new mode bits */
558 udav_csr_write1(sc, UDAV_RCR, rxmode);
559 }
560
561 static void
udav_start(struct usb_ether * ue)562 udav_start(struct usb_ether *ue)
563 {
564 struct udav_softc *sc = ue->ue_sc;
565
566 /*
567 * start the USB transfers, if not already started:
568 */
569 usbd_transfer_start(sc->sc_xfer[UDAV_INTR_DT_RD]);
570 usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_RD]);
571 usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_WR]);
572 }
573
574 static void
udav_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)575 udav_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
576 {
577 struct udav_softc *sc = usbd_xfer_softc(xfer);
578 if_t ifp = uether_getifp(&sc->sc_ue);
579 struct usb_page_cache *pc;
580 struct mbuf *m;
581 int extra_len;
582 int temp_len;
583 uint8_t buf[2];
584
585 switch (USB_GET_STATE(xfer)) {
586 case USB_ST_TRANSFERRED:
587 DPRINTFN(11, "transfer complete\n");
588 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
589
590 /* FALLTHROUGH */
591 case USB_ST_SETUP:
592 tr_setup:
593 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0) {
594 /*
595 * don't send anything if there is no link !
596 */
597 return;
598 }
599 m = if_dequeue(ifp);
600
601 if (m == NULL)
602 return;
603 if (m->m_pkthdr.len > MCLBYTES)
604 m->m_pkthdr.len = MCLBYTES;
605 if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) {
606 extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len;
607 } else {
608 extra_len = 0;
609 }
610
611 temp_len = (m->m_pkthdr.len + extra_len);
612
613 /*
614 * the frame length is specified in the first 2 bytes of the
615 * buffer
616 */
617 buf[0] = (uint8_t)(temp_len);
618 buf[1] = (uint8_t)(temp_len >> 8);
619
620 temp_len += 2;
621
622 pc = usbd_xfer_get_frame(xfer, 0);
623 usbd_copy_in(pc, 0, buf, 2);
624 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
625
626 if (extra_len)
627 usbd_frame_zero(pc, temp_len - extra_len, extra_len);
628 /*
629 * if there's a BPF listener, bounce a copy
630 * of this frame to him:
631 */
632 BPF_MTAP(ifp, m);
633
634 m_freem(m);
635
636 usbd_xfer_set_frame_len(xfer, 0, temp_len);
637 usbd_transfer_submit(xfer);
638 return;
639
640 default: /* Error */
641 DPRINTFN(11, "transfer error, %s\n",
642 usbd_errstr(error));
643
644 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
645
646 if (error != USB_ERR_CANCELLED) {
647 /* try to clear stall first */
648 usbd_xfer_set_stall(xfer);
649 goto tr_setup;
650 }
651 return;
652 }
653 }
654
655 static void
udav_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)656 udav_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
657 {
658 struct udav_softc *sc = usbd_xfer_softc(xfer);
659 struct usb_ether *ue = &sc->sc_ue;
660 if_t ifp = uether_getifp(ue);
661 struct usb_page_cache *pc;
662 struct udav_rxpkt stat;
663 int len;
664 int actlen;
665
666 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
667
668 switch (USB_GET_STATE(xfer)) {
669 case USB_ST_TRANSFERRED:
670
671 if (actlen < (int)(sizeof(stat) + ETHER_CRC_LEN)) {
672 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
673 goto tr_setup;
674 }
675 pc = usbd_xfer_get_frame(xfer, 0);
676 usbd_copy_out(pc, 0, &stat, sizeof(stat));
677 actlen -= sizeof(stat);
678 len = min(actlen, le16toh(stat.pktlen));
679 len -= ETHER_CRC_LEN;
680
681 if (stat.rxstat & UDAV_RSR_LCS) {
682 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
683 goto tr_setup;
684 }
685 if (stat.rxstat & UDAV_RSR_ERR) {
686 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
687 goto tr_setup;
688 }
689 uether_rxbuf(ue, pc, sizeof(stat), len);
690 /* FALLTHROUGH */
691 case USB_ST_SETUP:
692 tr_setup:
693 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
694 usbd_transfer_submit(xfer);
695 uether_rxflush(ue);
696 return;
697
698 default: /* Error */
699 DPRINTF("bulk read error, %s\n",
700 usbd_errstr(error));
701
702 if (error != USB_ERR_CANCELLED) {
703 /* try to clear stall first */
704 usbd_xfer_set_stall(xfer);
705 goto tr_setup;
706 }
707 return;
708 }
709 }
710
711 static void
udav_intr_callback(struct usb_xfer * xfer,usb_error_t error)712 udav_intr_callback(struct usb_xfer *xfer, usb_error_t error)
713 {
714 switch (USB_GET_STATE(xfer)) {
715 case USB_ST_TRANSFERRED:
716 case USB_ST_SETUP:
717 tr_setup:
718 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
719 usbd_transfer_submit(xfer);
720 return;
721
722 default: /* Error */
723 if (error != USB_ERR_CANCELLED) {
724 /* try to clear stall first */
725 usbd_xfer_set_stall(xfer);
726 goto tr_setup;
727 }
728 return;
729 }
730 }
731
732 static void
udav_stop(struct usb_ether * ue)733 udav_stop(struct usb_ether *ue)
734 {
735 struct udav_softc *sc = ue->ue_sc;
736 if_t ifp = uether_getifp(&sc->sc_ue);
737
738 UDAV_LOCK_ASSERT(sc, MA_OWNED);
739
740 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
741 if (!(sc->sc_flags & UDAV_FLAG_NO_PHY))
742 sc->sc_flags &= ~UDAV_FLAG_LINK;
743
744 /*
745 * stop all the transfers, if not already stopped:
746 */
747 usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_WR]);
748 usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_RD]);
749 usbd_transfer_stop(sc->sc_xfer[UDAV_INTR_DT_RD]);
750
751 udav_reset(sc);
752 }
753
754 static int
udav_ifmedia_upd(if_t ifp)755 udav_ifmedia_upd(if_t ifp)
756 {
757 struct udav_softc *sc = if_getsoftc(ifp);
758 struct mii_data *mii = GET_MII(sc);
759 struct mii_softc *miisc;
760 int error;
761
762 UDAV_LOCK_ASSERT(sc, MA_OWNED);
763
764 sc->sc_flags &= ~UDAV_FLAG_LINK;
765 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
766 PHY_RESET(miisc);
767 error = mii_mediachg(mii);
768 return (error);
769 }
770
771 static void
udav_ifmedia_status(if_t ifp,struct ifmediareq * ifmr)772 udav_ifmedia_status(if_t ifp, struct ifmediareq *ifmr)
773 {
774 struct udav_softc *sc = if_getsoftc(ifp);
775 struct mii_data *mii = GET_MII(sc);
776
777 UDAV_LOCK(sc);
778 mii_pollstat(mii);
779 ifmr->ifm_active = mii->mii_media_active;
780 ifmr->ifm_status = mii->mii_media_status;
781 UDAV_UNLOCK(sc);
782 }
783
784 static void
udav_tick(struct usb_ether * ue)785 udav_tick(struct usb_ether *ue)
786 {
787 struct udav_softc *sc = ue->ue_sc;
788 struct mii_data *mii = GET_MII(sc);
789
790 UDAV_LOCK_ASSERT(sc, MA_OWNED);
791
792 mii_tick(mii);
793 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0
794 && mii->mii_media_status & IFM_ACTIVE &&
795 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
796 sc->sc_flags |= UDAV_FLAG_LINK;
797 udav_start(ue);
798 }
799 }
800
801 static int
udav_miibus_readreg(device_t dev,int phy,int reg)802 udav_miibus_readreg(device_t dev, int phy, int reg)
803 {
804 struct udav_softc *sc = device_get_softc(dev);
805 uint16_t data16;
806 uint8_t val[2];
807 int locked;
808
809 /* XXX: one PHY only for the internal PHY */
810 if (phy != 0)
811 return (0);
812
813 locked = mtx_owned(&sc->sc_mtx);
814 if (!locked)
815 UDAV_LOCK(sc);
816
817 /* select internal PHY and set PHY register address */
818 udav_csr_write1(sc, UDAV_EPAR,
819 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
820
821 /* select PHY operation and start read command */
822 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
823
824 /* XXX: should we wait? */
825
826 /* end read command */
827 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
828
829 /* retrieve the result from data registers */
830 udav_csr_read(sc, UDAV_EPDRL, val, 2);
831
832 data16 = (val[0] | (val[1] << 8));
833
834 DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n",
835 phy, reg, data16);
836
837 if (!locked)
838 UDAV_UNLOCK(sc);
839 return (data16);
840 }
841
842 static int
udav_miibus_writereg(device_t dev,int phy,int reg,int data)843 udav_miibus_writereg(device_t dev, int phy, int reg, int data)
844 {
845 struct udav_softc *sc = device_get_softc(dev);
846 uint8_t val[2];
847 int locked;
848
849 /* XXX: one PHY only for the internal PHY */
850 if (phy != 0)
851 return (0);
852
853 locked = mtx_owned(&sc->sc_mtx);
854 if (!locked)
855 UDAV_LOCK(sc);
856
857 /* select internal PHY and set PHY register address */
858 udav_csr_write1(sc, UDAV_EPAR,
859 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
860
861 /* put the value to the data registers */
862 val[0] = (data & 0xff);
863 val[1] = (data >> 8) & 0xff;
864 udav_csr_write(sc, UDAV_EPDRL, val, 2);
865
866 /* select PHY operation and start write command */
867 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
868
869 /* XXX: should we wait? */
870
871 /* end write command */
872 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
873
874 if (!locked)
875 UDAV_UNLOCK(sc);
876 return (0);
877 }
878
879 static void
udav_miibus_statchg(device_t dev)880 udav_miibus_statchg(device_t dev)
881 {
882 /* nothing to do */
883 }
884