if_udav.c revision 1.54 1 /* $NetBSD: if_udav.c,v 1.54 2018/07/29 02:01:43 riastradh Exp $ */
2 /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */
3
4 /*
5 * Copyright (c) 2003
6 * Shingo WATANABE <nabe (at) nabechan.org>. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34 /*
35 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
36 * The spec can be found at the following url.
37 * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-F01-062202s.pdf
38 */
39
40 /*
41 * TODO:
42 * Interrupt Endpoint support
43 * External PHYs
44 * powerhook() support?
45 */
46
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: if_udav.c,v 1.54 2018/07/29 02:01:43 riastradh Exp $");
49
50 #ifdef _KERNEL_OPT
51 #include "opt_inet.h"
52 #include "opt_usb.h"
53 #endif
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/mutex.h>
58 #include <sys/mbuf.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
61 #include <sys/device.h>
62 #include <sys/rndsource.h>
63
64 #include <net/if.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68
69 #include <net/bpf.h>
70
71 #include <net/if_ether.h>
72 #ifdef INET
73 #include <netinet/in.h>
74 #include <netinet/if_inarp.h>
75 #endif
76
77 #include <dev/mii/mii.h>
78 #include <dev/mii/miivar.h>
79
80 #include <dev/usb/usb.h>
81 #include <dev/usb/usbdi.h>
82 #include <dev/usb/usbdi_util.h>
83 #include <dev/usb/usbdevs.h>
84
85 #include <dev/usb/if_udavreg.h>
86
87
88 /* Function declarations */
89 int udav_match(device_t, cfdata_t, void *);
90 void udav_attach(device_t, device_t, void *);
91 int udav_detach(device_t, int);
92 int udav_activate(device_t, enum devact);
93 extern struct cfdriver udav_cd;
94 CFATTACH_DECL_NEW(udav, sizeof(struct udav_softc), udav_match, udav_attach,
95 udav_detach, udav_activate);
96
97 Static int udav_openpipes(struct udav_softc *);
98 Static int udav_rx_list_init(struct udav_softc *);
99 Static int udav_tx_list_init(struct udav_softc *);
100 Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
101 Static void udav_start(struct ifnet *);
102 Static int udav_send(struct udav_softc *, struct mbuf *, int);
103 Static void udav_txeof(struct usbd_xfer *, void *, usbd_status);
104 Static void udav_rxeof(struct usbd_xfer *, void *, usbd_status);
105 Static void udav_tick(void *);
106 Static void udav_tick_task(void *);
107 Static int udav_ioctl(struct ifnet *, u_long, void *);
108 Static void udav_stop_task(struct udav_softc *);
109 Static void udav_stop(struct ifnet *, int);
110 Static void udav_watchdog(struct ifnet *);
111 Static int udav_ifmedia_change(struct ifnet *);
112 Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
113 Static void udav_lock_mii(struct udav_softc *);
114 Static void udav_unlock_mii(struct udav_softc *);
115 Static int udav_miibus_readreg(device_t, int, int);
116 Static void udav_miibus_writereg(device_t, int, int, int);
117 Static void udav_miibus_statchg(struct ifnet *);
118 Static int udav_init(struct ifnet *);
119 Static void udav_setmulti(struct udav_softc *);
120 Static void udav_reset(struct udav_softc *);
121
122 Static int udav_csr_read(struct udav_softc *, int, void *, int);
123 Static int udav_csr_write(struct udav_softc *, int, void *, int);
124 Static int udav_csr_read1(struct udav_softc *, int);
125 Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
126
127 #if 0
128 Static int udav_mem_read(struct udav_softc *, int, void *, int);
129 Static int udav_mem_write(struct udav_softc *, int, void *, int);
130 Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
131 #endif
132
133 /* Macros */
134 #ifdef UDAV_DEBUG
135 #define DPRINTF(x) if (udavdebug) printf x
136 #define DPRINTFN(n,x) if (udavdebug >= (n)) printf x
137 int udavdebug = 0;
138 #else
139 #define DPRINTF(x)
140 #define DPRINTFN(n,x)
141 #endif
142
143 #define UDAV_SETBIT(sc, reg, x) \
144 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
145
146 #define UDAV_CLRBIT(sc, reg, x) \
147 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
148
149 static const struct udav_type {
150 struct usb_devno udav_dev;
151 uint16_t udav_flags;
152 #define UDAV_EXT_PHY 0x0001
153 #define UDAV_NO_PHY 0x0002
154 } udav_devs [] = {
155 /* Corega USB-TXC */
156 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
157 /* ShanTou ST268 USB NIC */
158 {{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
159 /* ShanTou ADM8515 */
160 {{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 0},
161 /* SUNRISING SR9600 */
162 {{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_SR9600 }, 0 },
163 /* SUNRISING QF9700 */
164 {{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_QF9700 }, UDAV_NO_PHY },
165 /* QUAN DM9601 */
166 {{USB_VENDOR_QUAN, USB_PRODUCT_QUAN_DM9601 }, 0},
167 #if 0
168 /* DAVICOM DM9601 Generic? */
169 /* XXX: The following ids was obtained from the data sheet. */
170 {{ 0x0a46, 0x9601 }, 0},
171 #endif
172 };
173 #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
174
175
176 /* Probe */
177 int
178 udav_match(device_t parent, cfdata_t match, void *aux)
179 {
180 struct usb_attach_arg *uaa = aux;
181
182 return udav_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
183 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
184 }
185
186 /* Attach */
187 void
188 udav_attach(device_t parent, device_t self, void *aux)
189 {
190 struct udav_softc *sc = device_private(self);
191 struct usb_attach_arg *uaa = aux;
192 struct usbd_device *dev = uaa->uaa_device;
193 struct usbd_interface *iface;
194 usbd_status err;
195 usb_interface_descriptor_t *id;
196 usb_endpoint_descriptor_t *ed;
197 char *devinfop;
198 struct ifnet *ifp;
199 struct mii_data *mii;
200 u_char eaddr[ETHER_ADDR_LEN];
201 int i, s;
202
203 sc->sc_dev = self;
204
205 aprint_naive("\n");
206 aprint_normal("\n");
207
208 devinfop = usbd_devinfo_alloc(dev, 0);
209 aprint_normal_dev(self, "%s\n", devinfop);
210 usbd_devinfo_free(devinfop);
211
212 /* Move the device into the configured state. */
213 err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
214 if (err) {
215 aprint_error_dev(self, "failed to set configuration"
216 ", err=%s\n", usbd_errstr(err));
217 goto bad;
218 }
219
220 usb_init_task(&sc->sc_tick_task, udav_tick_task, sc, 0);
221 mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
222 usb_init_task(&sc->sc_stop_task, (void (*)(void *))udav_stop_task, sc,
223 0);
224
225 /* get control interface */
226 err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
227 if (err) {
228 aprint_error_dev(self, "failed to get interface, err=%s\n",
229 usbd_errstr(err));
230 goto bad;
231 }
232
233 sc->sc_udev = dev;
234 sc->sc_ctl_iface = iface;
235 sc->sc_flags = udav_lookup(uaa->uaa_vendor,
236 uaa->uaa_product)->udav_flags;
237
238 /* get interface descriptor */
239 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
240
241 /* find endpoints */
242 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
243 for (i = 0; i < id->bNumEndpoints; i++) {
244 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
245 if (ed == NULL) {
246 aprint_error_dev(self, "couldn't get endpoint %d\n", i);
247 goto bad;
248 }
249 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
250 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
251 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
252 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
253 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
254 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
255 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
256 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
257 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
258 }
259
260 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
261 sc->sc_intrin_no == -1) {
262 aprint_error_dev(self, "missing endpoint\n");
263 goto bad;
264 }
265
266 s = splnet();
267
268 /* reset the adapter */
269 udav_reset(sc);
270
271 /* Get Ethernet Address */
272 err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
273 if (err) {
274 aprint_error_dev(self, "read MAC address failed\n");
275 splx(s);
276 goto bad;
277 }
278
279 /* Print Ethernet Address */
280 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
281
282 /* initialize interface information */
283 ifp = GET_IFP(sc);
284 ifp->if_softc = sc;
285 ifp->if_mtu = ETHERMTU;
286 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
287 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
288 ifp->if_start = udav_start;
289 ifp->if_ioctl = udav_ioctl;
290 ifp->if_watchdog = udav_watchdog;
291 ifp->if_init = udav_init;
292 ifp->if_stop = udav_stop;
293
294 IFQ_SET_READY(&ifp->if_snd);
295
296 if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
297 sc->sc_link = 1;
298 goto skipmii;
299 }
300
301 /*
302 * Do ifmedia setup.
303 */
304 mii = &sc->sc_mii;
305 mii->mii_ifp = ifp;
306 mii->mii_readreg = udav_miibus_readreg;
307 mii->mii_writereg = udav_miibus_writereg;
308 mii->mii_statchg = udav_miibus_statchg;
309 mii->mii_flags = MIIF_AUTOTSLEEP;
310 sc->sc_ec.ec_mii = mii;
311 ifmedia_init(&mii->mii_media, 0,
312 udav_ifmedia_change, udav_ifmedia_status);
313 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
314 if (LIST_FIRST(&mii->mii_phys) == NULL) {
315 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
316 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
317 } else
318 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
319
320 skipmii:
321 /* attach the interface */
322 if_attach(ifp);
323 ether_ifattach(ifp, eaddr);
324
325 rnd_attach_source(&sc->rnd_source, device_xname(self),
326 RND_TYPE_NET, RND_FLAG_DEFAULT);
327
328 callout_init(&sc->sc_stat_ch, 0);
329 sc->sc_attached = 1;
330 splx(s);
331
332 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
333
334 return;
335
336 bad:
337 sc->sc_dying = 1;
338 return;
339 }
340
341 /* detach */
342 int
343 udav_detach(device_t self, int flags)
344 {
345 struct udav_softc *sc = device_private(self);
346 struct ifnet *ifp = GET_IFP(sc);
347 int s;
348
349 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
350
351 /* Detached before attached finished */
352 if (!sc->sc_attached)
353 return 0;
354
355 callout_halt(&sc->sc_stat_ch, NULL);
356
357 /* Remove any pending tasks */
358 usb_rem_task_wait(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER);
359 usb_rem_task_wait(sc->sc_udev, &sc->sc_stop_task, USB_TASKQ_DRIVER);
360
361 s = splusb();
362
363 if (--sc->sc_refcnt >= 0) {
364 /* Wait for processes to go away */
365 usb_detach_waitold(sc->sc_dev);
366 }
367 if (ifp->if_flags & IFF_RUNNING)
368 udav_stop(GET_IFP(sc), 1);
369
370 rnd_detach_source(&sc->rnd_source);
371 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
372 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
373 ether_ifdetach(ifp);
374 if_detach(ifp);
375
376 #ifdef DIAGNOSTIC
377 if (sc->sc_pipe_tx != NULL)
378 aprint_debug_dev(self, "detach has active tx endpoint.\n");
379 if (sc->sc_pipe_rx != NULL)
380 aprint_debug_dev(self, "detach has active rx endpoint.\n");
381 if (sc->sc_pipe_intr != NULL)
382 aprint_debug_dev(self, "detach has active intr endpoint.\n");
383 #endif
384 sc->sc_attached = 0;
385
386 splx(s);
387
388 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
389
390 mutex_destroy(&sc->sc_mii_lock);
391
392 return 0;
393 }
394
395 #if 0
396 /* read memory */
397 Static int
398 udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
399 {
400 usb_device_request_t req;
401 usbd_status err;
402
403 if (sc == NULL)
404 return 0;
405
406 DPRINTFN(0x200,
407 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
408
409 if (sc->sc_dying)
410 return 0;
411
412 offset &= 0xffff;
413 len &= 0xff;
414
415 req.bmRequestType = UT_READ_VENDOR_DEVICE;
416 req.bRequest = UDAV_REQ_MEM_READ;
417 USETW(req.wValue, 0x0000);
418 USETW(req.wIndex, offset);
419 USETW(req.wLength, len);
420
421 sc->sc_refcnt++;
422 err = usbd_do_request(sc->sc_udev, &req, buf);
423 if (--sc->sc_refcnt < 0)
424 usb_detach_wakeupold(sc->sc_dev);
425 if (err) {
426 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
427 device_xname(sc->sc_dev), __func__, offset, err));
428 }
429
430 return err;
431 }
432
433 /* write memory */
434 Static int
435 udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
436 {
437 usb_device_request_t req;
438 usbd_status err;
439
440 if (sc == NULL)
441 return 0;
442
443 DPRINTFN(0x200,
444 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
445
446 if (sc->sc_dying)
447 return 0;
448
449 offset &= 0xffff;
450 len &= 0xff;
451
452 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
453 req.bRequest = UDAV_REQ_MEM_WRITE;
454 USETW(req.wValue, 0x0000);
455 USETW(req.wIndex, offset);
456 USETW(req.wLength, len);
457
458 sc->sc_refcnt++;
459 err = usbd_do_request(sc->sc_udev, &req, buf);
460 if (--sc->sc_refcnt < 0)
461 usb_detach_wakeupold(sc->sc_dev);
462 if (err) {
463 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
464 device_xname(sc->sc_dev), __func__, offset, err));
465 }
466
467 return err;
468 }
469
470 /* write memory */
471 Static int
472 udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
473 {
474 usb_device_request_t req;
475 usbd_status err;
476
477 if (sc == NULL)
478 return 0;
479
480 DPRINTFN(0x200,
481 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
482
483 if (sc->sc_dying)
484 return 0;
485
486 offset &= 0xffff;
487
488 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
489 req.bRequest = UDAV_REQ_MEM_WRITE1;
490 USETW(req.wValue, ch);
491 USETW(req.wIndex, offset);
492 USETW(req.wLength, 0x0000);
493
494 sc->sc_refcnt++;
495 err = usbd_do_request(sc->sc_udev, &req, NULL);
496 if (--sc->sc_refcnt < 0)
497 usb_detach_wakeupold(sc->sc_dev);
498 if (err) {
499 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
500 device_xname(sc->sc_dev), __func__, offset, err));
501 }
502
503 return err;
504 }
505 #endif
506
507 /* read register(s) */
508 Static int
509 udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
510 {
511 usb_device_request_t req;
512 usbd_status err;
513
514 if (sc == NULL)
515 return 0;
516
517 DPRINTFN(0x200,
518 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
519
520 if (sc->sc_dying)
521 return 0;
522
523 offset &= 0xff;
524 len &= 0xff;
525
526 req.bmRequestType = UT_READ_VENDOR_DEVICE;
527 req.bRequest = UDAV_REQ_REG_READ;
528 USETW(req.wValue, 0x0000);
529 USETW(req.wIndex, offset);
530 USETW(req.wLength, len);
531
532 sc->sc_refcnt++;
533 err = usbd_do_request(sc->sc_udev, &req, buf);
534 if (--sc->sc_refcnt < 0)
535 usb_detach_wakeupold(sc->sc_dev);
536 if (err) {
537 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
538 device_xname(sc->sc_dev), __func__, offset, err));
539 }
540
541 return err;
542 }
543
544 /* write register(s) */
545 Static int
546 udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
547 {
548 usb_device_request_t req;
549 usbd_status err;
550
551 if (sc == NULL)
552 return 0;
553
554 DPRINTFN(0x200,
555 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
556
557 if (sc->sc_dying)
558 return 0;
559
560 offset &= 0xff;
561 len &= 0xff;
562
563 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
564 req.bRequest = UDAV_REQ_REG_WRITE;
565 USETW(req.wValue, 0x0000);
566 USETW(req.wIndex, offset);
567 USETW(req.wLength, len);
568
569 sc->sc_refcnt++;
570 err = usbd_do_request(sc->sc_udev, &req, buf);
571 if (--sc->sc_refcnt < 0)
572 usb_detach_wakeupold(sc->sc_dev);
573 if (err) {
574 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
575 device_xname(sc->sc_dev), __func__, offset, err));
576 }
577
578 return err;
579 }
580
581 Static int
582 udav_csr_read1(struct udav_softc *sc, int offset)
583 {
584 uint8_t val = 0;
585
586 if (sc == NULL)
587 return 0;
588
589 DPRINTFN(0x200,
590 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
591
592 if (sc->sc_dying)
593 return 0;
594
595 return udav_csr_read(sc, offset, &val, 1) ? 0 : val;
596 }
597
598 /* write a register */
599 Static int
600 udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
601 {
602 usb_device_request_t req;
603 usbd_status err;
604
605 if (sc == NULL)
606 return 0;
607
608 DPRINTFN(0x200,
609 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
610
611 if (sc->sc_dying)
612 return 0;
613
614 offset &= 0xff;
615
616 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
617 req.bRequest = UDAV_REQ_REG_WRITE1;
618 USETW(req.wValue, ch);
619 USETW(req.wIndex, offset);
620 USETW(req.wLength, 0x0000);
621
622 sc->sc_refcnt++;
623 err = usbd_do_request(sc->sc_udev, &req, NULL);
624 if (--sc->sc_refcnt < 0)
625 usb_detach_wakeupold(sc->sc_dev);
626 if (err) {
627 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
628 device_xname(sc->sc_dev), __func__, offset, err));
629 }
630
631 return err;
632 }
633
634 Static int
635 udav_init(struct ifnet *ifp)
636 {
637 struct udav_softc *sc = ifp->if_softc;
638 struct mii_data *mii = GET_MII(sc);
639 uint8_t eaddr[ETHER_ADDR_LEN];
640 int rc, s;
641
642 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
643
644 if (sc->sc_dying)
645 return EIO;
646
647 s = splnet();
648
649 /* Cancel pending I/O and free all TX/RX buffers */
650 udav_stop(ifp, 1);
651
652 memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
653 udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
654
655 /* Initialize network control register */
656 /* Disable loopback */
657 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
658
659 /* Initialize RX control register */
660 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
661
662 /* If we want promiscuous mode, accept all physical frames. */
663 if (ifp->if_flags & IFF_PROMISC)
664 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
665 else
666 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
667
668 /* Load the multicast filter */
669 udav_setmulti(sc);
670
671 /* Enable RX */
672 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
673
674 /* clear POWER_DOWN state of internal PHY */
675 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
676 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
677
678 if ((rc = mii_mediachg(mii)) == ENXIO)
679 rc = 0;
680 else if (rc != 0)
681 goto out;
682
683 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
684 if (udav_openpipes(sc)) {
685 splx(s);
686 return EIO;
687 }
688 }
689
690 /* Initialize transmit ring */
691 if (udav_tx_list_init(sc)) {
692 printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
693 splx(s);
694 return EIO;
695 }
696
697 /* Initialize receive ring */
698 if (udav_rx_list_init(sc)) {
699 printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
700 splx(s);
701 return EIO;
702 }
703
704 /* Start up the receive pipe. */
705 for (size_t i = 0; i < UDAV_RX_LIST_CNT; i++) {
706 struct udav_chain *c = &sc->sc_cdata.udav_rx_chain[i];
707 usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, UDAV_BUFSZ,
708 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, udav_rxeof);
709 (void)usbd_transfer(c->udav_xfer);
710 DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev),
711 __func__));
712 }
713
714 ifp->if_flags |= IFF_RUNNING;
715 ifp->if_flags &= ~IFF_OACTIVE;
716
717 callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
718
719 out:
720 splx(s);
721 return rc;
722 }
723
724 Static void
725 udav_reset(struct udav_softc *sc)
726 {
727 int i;
728
729 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
730
731 if (sc->sc_dying)
732 return;
733
734 /* Select PHY */
735 #if 1
736 /*
737 * XXX: force select internal phy.
738 * external phy routines are not tested.
739 */
740 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
741 #else
742 if (sc->sc_flags & UDAV_EXT_PHY) {
743 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
744 } else {
745 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
746 }
747 #endif
748
749 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
750
751 for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
752 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
753 break;
754 delay(10); /* XXX */
755 }
756 delay(10000); /* XXX */
757 }
758
759 int
760 udav_activate(device_t self, enum devact act)
761 {
762 struct udav_softc *sc = device_private(self);
763
764 DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev),
765 __func__, act));
766 switch (act) {
767 case DVACT_DEACTIVATE:
768 if_deactivate(&sc->sc_ec.ec_if);
769 sc->sc_dying = 1;
770 return 0;
771 default:
772 return EOPNOTSUPP;
773 }
774 }
775
776 #define UDAV_BITS 6
777
778 #define UDAV_CALCHASH(addr) \
779 (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
780
781 Static void
782 udav_setmulti(struct udav_softc *sc)
783 {
784 struct ifnet *ifp;
785 struct ether_multi *enm;
786 struct ether_multistep step;
787 uint8_t hashes[8];
788 int h = 0;
789
790 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
791
792 if (sc->sc_dying)
793 return;
794
795 ifp = GET_IFP(sc);
796
797 if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
798 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
799 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
800 return;
801 }
802
803 if (ifp->if_flags & IFF_PROMISC) {
804 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
805 return;
806 } else if (ifp->if_flags & IFF_ALLMULTI) {
807 allmulti:
808 ifp->if_flags |= IFF_ALLMULTI;
809 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
810 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
811 return;
812 }
813
814 /* first, zot all the existing hash bits */
815 memset(hashes, 0x00, sizeof(hashes));
816 hashes[7] |= 0x80; /* broadcast address */
817 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
818
819 /* now program new ones */
820 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
821 while (enm != NULL) {
822 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
823 ETHER_ADDR_LEN) != 0)
824 goto allmulti;
825
826 h = UDAV_CALCHASH(enm->enm_addrlo);
827 hashes[h>>3] |= 1 << (h & 0x7);
828 ETHER_NEXT_MULTI(step, enm);
829 }
830
831 /* disable all multicast */
832 ifp->if_flags &= ~IFF_ALLMULTI;
833 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
834
835 /* write hash value to the register */
836 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
837 }
838
839 Static int
840 udav_openpipes(struct udav_softc *sc)
841 {
842 usbd_status err;
843 int error = 0;
844
845 if (sc->sc_dying)
846 return EIO;
847
848 sc->sc_refcnt++;
849
850 /* Open RX pipe */
851 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
852 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
853 if (err) {
854 printf("%s: open rx pipe failed: %s\n",
855 device_xname(sc->sc_dev), usbd_errstr(err));
856 error = EIO;
857 goto done;
858 }
859
860 /* Open TX pipe */
861 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
862 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
863 if (err) {
864 printf("%s: open tx pipe failed: %s\n",
865 device_xname(sc->sc_dev), usbd_errstr(err));
866 error = EIO;
867 goto done;
868 }
869
870 #if 0
871 /* XXX: interrupt endpoint is not yet supported */
872 /* Open Interrupt pipe */
873 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
874 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
875 &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN,
876 udav_intr, USBD_DEFAULT_INTERVAL);
877 if (err) {
878 printf("%s: open intr pipe failed: %s\n",
879 device_xname(sc->sc_dev), usbd_errstr(err));
880 error = EIO;
881 goto done;
882 }
883 #endif
884 done:
885 if (--sc->sc_refcnt < 0)
886 usb_detach_wakeupold(sc->sc_dev);
887
888 return error;
889 }
890
891 Static int
892 udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m)
893 {
894 struct mbuf *m_new = NULL;
895
896 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
897
898 if (m == NULL) {
899 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
900 if (m_new == NULL) {
901 printf("%s: no memory for rx list "
902 "-- packet dropped!\n", device_xname(sc->sc_dev));
903 return ENOBUFS;
904 }
905 MCLGET(m_new, M_DONTWAIT);
906 if (!(m_new->m_flags & M_EXT)) {
907 printf("%s: no memory for rx list "
908 "-- packet dropped!\n", device_xname(sc->sc_dev));
909 m_freem(m_new);
910 return ENOBUFS;
911 }
912 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
913 } else {
914 m_new = m;
915 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
916 m_new->m_data = m_new->m_ext.ext_buf;
917 }
918
919 m_adj(m_new, ETHER_ALIGN);
920 c->udav_mbuf = m_new;
921
922 return 0;
923 }
924
925
926 Static int
927 udav_rx_list_init(struct udav_softc *sc)
928 {
929 struct udav_cdata *cd;
930 struct udav_chain *c;
931 int i;
932
933 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
934
935 cd = &sc->sc_cdata;
936 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
937 c = &cd->udav_rx_chain[i];
938 c->udav_sc = sc;
939 c->udav_idx = i;
940 if (udav_newbuf(sc, c, NULL) == ENOBUFS)
941 return ENOBUFS;
942 if (c->udav_xfer == NULL) {
943 int error = usbd_create_xfer(sc->sc_pipe_rx,
944 UDAV_BUFSZ, 0, 0, &c->udav_xfer);
945 if (error)
946 return error;
947 c->udav_buf = usbd_get_buffer(c->udav_xfer);
948 }
949 }
950
951 return 0;
952 }
953
954 Static int
955 udav_tx_list_init(struct udav_softc *sc)
956 {
957 struct udav_cdata *cd;
958 struct udav_chain *c;
959 int i;
960
961 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
962
963 cd = &sc->sc_cdata;
964 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
965 c = &cd->udav_tx_chain[i];
966 c->udav_sc = sc;
967 c->udav_idx = i;
968 c->udav_mbuf = NULL;
969 if (c->udav_xfer == NULL) {
970 int error = usbd_create_xfer(sc->sc_pipe_tx, UDAV_BUFSZ,
971 USBD_FORCE_SHORT_XFER, 0, &c->udav_xfer);
972 if (error)
973 return error;
974 c->udav_buf = usbd_get_buffer(c->udav_xfer);
975 }
976 }
977
978 return 0;
979 }
980
981 Static void
982 udav_start(struct ifnet *ifp)
983 {
984 struct udav_softc *sc = ifp->if_softc;
985 struct mbuf *m_head = NULL;
986
987 DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev),
988 __func__, sc->sc_link));
989
990 if (sc->sc_dying)
991 return;
992
993 if (!sc->sc_link)
994 return;
995
996 if (ifp->if_flags & IFF_OACTIVE)
997 return;
998
999 IFQ_POLL(&ifp->if_snd, m_head);
1000 if (m_head == NULL)
1001 return;
1002
1003 if (udav_send(sc, m_head, 0)) {
1004 ifp->if_flags |= IFF_OACTIVE;
1005 return;
1006 }
1007
1008 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1009
1010 bpf_mtap(ifp, m_head, BPF_D_OUT);
1011
1012 ifp->if_flags |= IFF_OACTIVE;
1013
1014 /* Set a timeout in case the chip goes out to lunch. */
1015 ifp->if_timer = 5;
1016 }
1017
1018 Static int
1019 udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
1020 {
1021 int total_len;
1022 struct udav_chain *c;
1023 usbd_status err;
1024
1025 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1026
1027 c = &sc->sc_cdata.udav_tx_chain[idx];
1028
1029 /* Copy the mbuf data into a contiguous buffer */
1030 /* first 2 bytes are packet length */
1031 m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2);
1032 c->udav_mbuf = m;
1033 total_len = m->m_pkthdr.len;
1034 if (total_len < UDAV_MIN_FRAME_LEN) {
1035 memset(c->udav_buf + 2 + total_len, 0,
1036 UDAV_MIN_FRAME_LEN - total_len);
1037 total_len = UDAV_MIN_FRAME_LEN;
1038 }
1039
1040 /* Frame length is specified in the first 2bytes of the buffer */
1041 c->udav_buf[0] = (uint8_t)total_len;
1042 c->udav_buf[1] = (uint8_t)(total_len >> 8);
1043 total_len += 2;
1044
1045 usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, total_len,
1046 USBD_FORCE_SHORT_XFER, UDAV_TX_TIMEOUT, udav_txeof);
1047
1048 /* Transmit */
1049 sc->sc_refcnt++;
1050 err = usbd_transfer(c->udav_xfer);
1051 if (--sc->sc_refcnt < 0)
1052 usb_detach_wakeupold(sc->sc_dev);
1053 if (err != USBD_IN_PROGRESS) {
1054 printf("%s: udav_send error=%s\n", device_xname(sc->sc_dev),
1055 usbd_errstr(err));
1056 /* Stop the interface */
1057 usb_add_task(sc->sc_udev, &sc->sc_stop_task,
1058 USB_TASKQ_DRIVER);
1059 return EIO;
1060 }
1061
1062 DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev),
1063 __func__, total_len));
1064
1065 sc->sc_cdata.udav_tx_cnt++;
1066
1067 return 0;
1068 }
1069
1070 Static void
1071 udav_txeof(struct usbd_xfer *xfer, void *priv,
1072 usbd_status status)
1073 {
1074 struct udav_chain *c = priv;
1075 struct udav_softc *sc = c->udav_sc;
1076 struct ifnet *ifp = GET_IFP(sc);
1077 int s;
1078
1079 if (sc->sc_dying)
1080 return;
1081
1082 s = splnet();
1083
1084 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1085
1086 ifp->if_timer = 0;
1087 ifp->if_flags &= ~IFF_OACTIVE;
1088
1089 if (status != USBD_NORMAL_COMPLETION) {
1090 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1091 splx(s);
1092 return;
1093 }
1094 ifp->if_oerrors++;
1095 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
1096 usbd_errstr(status));
1097 if (status == USBD_STALLED) {
1098 sc->sc_refcnt++;
1099 usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
1100 if (--sc->sc_refcnt < 0)
1101 usb_detach_wakeupold(sc->sc_dev);
1102 }
1103 splx(s);
1104 return;
1105 }
1106
1107 ifp->if_opackets++;
1108
1109 m_freem(c->udav_mbuf);
1110 c->udav_mbuf = NULL;
1111
1112 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1113 udav_start(ifp);
1114
1115 splx(s);
1116 }
1117
1118 Static void
1119 udav_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1120 {
1121 struct udav_chain *c = priv;
1122 struct udav_softc *sc = c->udav_sc;
1123 struct ifnet *ifp = GET_IFP(sc);
1124 struct mbuf *m;
1125 uint32_t total_len;
1126 uint8_t *pktstat;
1127 int s;
1128
1129 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1130
1131 if (sc->sc_dying)
1132 return;
1133
1134 if (status != USBD_NORMAL_COMPLETION) {
1135 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1136 return;
1137 sc->sc_rx_errs++;
1138 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1139 printf("%s: %u usb errors on rx: %s\n",
1140 device_xname(sc->sc_dev), sc->sc_rx_errs,
1141 usbd_errstr(status));
1142 sc->sc_rx_errs = 0;
1143 }
1144 if (status == USBD_STALLED) {
1145 sc->sc_refcnt++;
1146 usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
1147 if (--sc->sc_refcnt < 0)
1148 usb_detach_wakeupold(sc->sc_dev);
1149 }
1150 goto done;
1151 }
1152
1153 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1154
1155 /* copy data to mbuf */
1156 m = c->udav_mbuf;
1157 memcpy(mtod(m, char *), c->udav_buf, total_len);
1158
1159 /* first byte in received data */
1160 pktstat = mtod(m, uint8_t *);
1161 m_adj(m, sizeof(uint8_t));
1162 DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(sc->sc_dev),
1163 *pktstat));
1164
1165 total_len = UGETW(mtod(m, uint8_t *));
1166 m_adj(m, sizeof(uint16_t));
1167
1168 if (*pktstat & UDAV_RSR_LCS) {
1169 ifp->if_collisions++;
1170 goto done;
1171 }
1172
1173 if (total_len < sizeof(struct ether_header) ||
1174 *pktstat & UDAV_RSR_ERR) {
1175 ifp->if_ierrors++;
1176 goto done;
1177 }
1178
1179 total_len -= ETHER_CRC_LEN;
1180
1181 m->m_pkthdr.len = m->m_len = total_len;
1182 m_set_rcvif(m, ifp);
1183
1184 s = splnet();
1185
1186 if (udav_newbuf(sc, c, NULL) == ENOBUFS) {
1187 ifp->if_ierrors++;
1188 goto done1;
1189 }
1190
1191 DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
1192 __func__, m->m_len));
1193 if_percpuq_enqueue((ifp)->if_percpuq, (m));
1194
1195 done1:
1196 splx(s);
1197
1198 done:
1199 /* Setup new transfer */
1200 usbd_setup_xfer(xfer, c, c->udav_buf, UDAV_BUFSZ, USBD_SHORT_XFER_OK,
1201 USBD_NO_TIMEOUT, udav_rxeof);
1202 sc->sc_refcnt++;
1203 usbd_transfer(xfer);
1204 if (--sc->sc_refcnt < 0)
1205 usb_detach_wakeupold(sc->sc_dev);
1206
1207 DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__));
1208 }
1209
1210 #if 0
1211 Static void udav_intr(void)
1212 {
1213 }
1214 #endif
1215
1216 Static int
1217 udav_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1218 {
1219 struct udav_softc *sc = ifp->if_softc;
1220 int s, error = 0;
1221
1222 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1223
1224 if (sc->sc_dying)
1225 return EIO;
1226
1227 s = splnet();
1228
1229 error = ether_ioctl(ifp, cmd, data);
1230 if (error == ENETRESET) {
1231 if (ifp->if_flags & IFF_RUNNING)
1232 udav_setmulti(sc);
1233 error = 0;
1234 }
1235
1236 splx(s);
1237
1238 return error;
1239 }
1240
1241 Static void
1242 udav_watchdog(struct ifnet *ifp)
1243 {
1244 struct udav_softc *sc = ifp->if_softc;
1245 struct udav_chain *c;
1246 usbd_status stat;
1247 int s;
1248
1249 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1250
1251 ifp->if_oerrors++;
1252 printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
1253
1254 s = splusb();
1255 c = &sc->sc_cdata.udav_tx_chain[0];
1256 usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat);
1257 udav_txeof(c->udav_xfer, c, stat);
1258
1259 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1260 udav_start(ifp);
1261 splx(s);
1262 }
1263
1264 Static void
1265 udav_stop_task(struct udav_softc *sc)
1266 {
1267 udav_stop(GET_IFP(sc), 1);
1268 }
1269
1270 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1271 Static void
1272 udav_stop(struct ifnet *ifp, int disable)
1273 {
1274 struct udav_softc *sc = ifp->if_softc;
1275 usbd_status err;
1276 int i;
1277
1278 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1279
1280 ifp->if_timer = 0;
1281
1282 udav_reset(sc);
1283
1284 callout_stop(&sc->sc_stat_ch);
1285
1286 /* Stop transfers */
1287 /* RX endpoint */
1288 if (sc->sc_pipe_rx != NULL) {
1289 err = usbd_abort_pipe(sc->sc_pipe_rx);
1290 if (err)
1291 printf("%s: abort rx pipe failed: %s\n",
1292 device_xname(sc->sc_dev), usbd_errstr(err));
1293 }
1294
1295 /* TX endpoint */
1296 if (sc->sc_pipe_tx != NULL) {
1297 err = usbd_abort_pipe(sc->sc_pipe_tx);
1298 if (err)
1299 printf("%s: abort tx pipe failed: %s\n",
1300 device_xname(sc->sc_dev), usbd_errstr(err));
1301 }
1302
1303 #if 0
1304 /* XXX: Interrupt endpoint is not yet supported!! */
1305 /* Interrupt endpoint */
1306 if (sc->sc_pipe_intr != NULL) {
1307 err = usbd_abort_pipe(sc->sc_pipe_intr);
1308 if (err)
1309 printf("%s: abort intr pipe failed: %s\n",
1310 device_xname(sc->sc_dev), usbd_errstr(err));
1311 err = usbd_close_pipe(sc->sc_pipe_intr);
1312 if (err)
1313 printf("%s: close intr pipe failed: %s\n",
1314 device_xname(sc->sc_dev), usbd_errstr(err));
1315 sc->sc_pipe_intr = NULL;
1316 }
1317 #endif
1318
1319 /* Free RX resources. */
1320 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
1321 if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) {
1322 m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf);
1323 sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL;
1324 }
1325 if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) {
1326 usbd_destroy_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer);
1327 sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL;
1328 }
1329 }
1330
1331 /* Free TX resources. */
1332 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
1333 if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) {
1334 m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf);
1335 sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL;
1336 }
1337 if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) {
1338 usbd_destroy_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer);
1339 sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL;
1340 }
1341 }
1342
1343 /* Close pipes */
1344 /* RX endpoint */
1345 if (sc->sc_pipe_rx != NULL) {
1346 err = usbd_close_pipe(sc->sc_pipe_rx);
1347 if (err)
1348 printf("%s: close rx pipe failed: %s\n",
1349 device_xname(sc->sc_dev), usbd_errstr(err));
1350 sc->sc_pipe_rx = NULL;
1351 }
1352
1353 /* TX endpoint */
1354 if (sc->sc_pipe_tx != NULL) {
1355 err = usbd_close_pipe(sc->sc_pipe_tx);
1356 if (err)
1357 printf("%s: close tx pipe failed: %s\n",
1358 device_xname(sc->sc_dev), usbd_errstr(err));
1359 sc->sc_pipe_tx = NULL;
1360 }
1361
1362 if (!ISSET(sc->sc_flags, UDAV_NO_PHY))
1363 sc->sc_link = 0;
1364 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1365 }
1366
1367 /* Set media options */
1368 Static int
1369 udav_ifmedia_change(struct ifnet *ifp)
1370 {
1371 struct udav_softc *sc = ifp->if_softc;
1372 struct mii_data *mii = GET_MII(sc);
1373 int rc;
1374
1375 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1376
1377 if (sc->sc_dying)
1378 return 0;
1379
1380 sc->sc_link = 0;
1381 if ((rc = mii_mediachg(mii)) == ENXIO)
1382 return 0;
1383 return rc;
1384 }
1385
1386 /* Report current media status. */
1387 Static void
1388 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1389 {
1390 struct udav_softc *sc = ifp->if_softc;
1391
1392 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1393
1394 if (sc->sc_dying)
1395 return;
1396
1397 ether_mediastatus(ifp, ifmr);
1398 }
1399
1400 Static void
1401 udav_tick(void *xsc)
1402 {
1403 struct udav_softc *sc = xsc;
1404
1405 if (sc == NULL)
1406 return;
1407
1408 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1409 __func__));
1410
1411 if (sc->sc_dying)
1412 return;
1413
1414 /* Perform periodic stuff in process context */
1415 usb_add_task(sc->sc_udev, &sc->sc_tick_task,
1416 USB_TASKQ_DRIVER);
1417 }
1418
1419 Static void
1420 udav_tick_task(void *xsc)
1421 {
1422 struct udav_softc *sc = xsc;
1423 struct ifnet *ifp;
1424 struct mii_data *mii;
1425 int s;
1426
1427 if (sc == NULL)
1428 return;
1429
1430 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1431 __func__));
1432
1433 if (sc->sc_dying)
1434 return;
1435
1436 ifp = GET_IFP(sc);
1437 mii = GET_MII(sc);
1438
1439 if (mii == NULL)
1440 return;
1441
1442 s = splnet();
1443
1444 mii_tick(mii);
1445 if (!sc->sc_link) {
1446 mii_pollstat(mii);
1447 if (mii->mii_media_status & IFM_ACTIVE &&
1448 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1449 DPRINTF(("%s: %s: got link\n",
1450 device_xname(sc->sc_dev), __func__));
1451 sc->sc_link++;
1452 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1453 udav_start(ifp);
1454 }
1455 }
1456
1457 callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
1458
1459 splx(s);
1460 }
1461
1462 /* Get exclusive access to the MII registers */
1463 Static void
1464 udav_lock_mii(struct udav_softc *sc)
1465 {
1466 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1467 __func__));
1468
1469 sc->sc_refcnt++;
1470 mutex_enter(&sc->sc_mii_lock);
1471 }
1472
1473 Static void
1474 udav_unlock_mii(struct udav_softc *sc)
1475 {
1476 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1477 __func__));
1478
1479 mutex_exit(&sc->sc_mii_lock);
1480 if (--sc->sc_refcnt < 0)
1481 usb_detach_wakeupold(sc->sc_dev);
1482 }
1483
1484 Static int
1485 udav_miibus_readreg(device_t dev, int phy, int reg)
1486 {
1487 struct udav_softc *sc;
1488 uint8_t val[2];
1489 uint16_t data16;
1490
1491 if (dev == NULL)
1492 return 0;
1493
1494 sc = device_private(dev);
1495
1496 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1497 device_xname(sc->sc_dev), __func__, phy, reg));
1498
1499 if (sc->sc_dying) {
1500 #ifdef DIAGNOSTIC
1501 printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1502 __func__);
1503 #endif
1504 return 0;
1505 }
1506
1507 /* XXX: one PHY only for the internal PHY */
1508 if (phy != 0) {
1509 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1510 device_xname(sc->sc_dev), __func__, phy));
1511 return 0;
1512 }
1513
1514 udav_lock_mii(sc);
1515
1516 /* select internal PHY and set PHY register address */
1517 udav_csr_write1(sc, UDAV_EPAR,
1518 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1519
1520 /* select PHY operation and start read command */
1521 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
1522
1523 /* XXX: should be wait? */
1524
1525 /* end read command */
1526 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
1527
1528 /* retrieve the result from data registers */
1529 udav_csr_read(sc, UDAV_EPDRL, val, 2);
1530
1531 udav_unlock_mii(sc);
1532
1533 data16 = val[0] | (val[1] << 8);
1534
1535 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1536 device_xname(sc->sc_dev), __func__, phy, reg, data16));
1537
1538 return data16;
1539 }
1540
1541 Static void
1542 udav_miibus_writereg(device_t dev, int phy, int reg, int data)
1543 {
1544 struct udav_softc *sc;
1545 uint8_t val[2];
1546
1547 if (dev == NULL)
1548 return;
1549
1550 sc = device_private(dev);
1551
1552 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1553 device_xname(sc->sc_dev), __func__, phy, reg, data));
1554
1555 if (sc->sc_dying) {
1556 #ifdef DIAGNOSTIC
1557 printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1558 __func__);
1559 #endif
1560 return;
1561 }
1562
1563 /* XXX: one PHY only for the internal PHY */
1564 if (phy != 0) {
1565 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1566 device_xname(sc->sc_dev), __func__, phy));
1567 return;
1568 }
1569
1570 udav_lock_mii(sc);
1571
1572 /* select internal PHY and set PHY register address */
1573 udav_csr_write1(sc, UDAV_EPAR,
1574 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1575
1576 /* put the value to the data registers */
1577 val[0] = data & 0xff;
1578 val[1] = (data >> 8) & 0xff;
1579 udav_csr_write(sc, UDAV_EPDRL, val, 2);
1580
1581 /* select PHY operation and start write command */
1582 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
1583
1584 /* XXX: should be wait? */
1585
1586 /* end write command */
1587 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
1588
1589 udav_unlock_mii(sc);
1590
1591 return;
1592 }
1593
1594 Static void
1595 udav_miibus_statchg(struct ifnet *ifp)
1596 {
1597 #ifdef UDAV_DEBUG
1598
1599 if (ifp == NULL)
1600 return;
1601
1602 DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
1603 #endif
1604 /* Nothing to do */
1605 }
1606