if_udav.c revision 1.55 1 /* $NetBSD: if_udav.c,v 1.55 2018/08/02 06:09:04 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.55 2018/08/02 06:09:04 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 NULL);
360 usb_rem_task_wait(sc->sc_udev, &sc->sc_stop_task, USB_TASKQ_DRIVER,
361 NULL);
362
363 s = splusb();
364
365 if (--sc->sc_refcnt >= 0) {
366 /* Wait for processes to go away */
367 usb_detach_waitold(sc->sc_dev);
368 }
369 if (ifp->if_flags & IFF_RUNNING)
370 udav_stop(GET_IFP(sc), 1);
371
372 rnd_detach_source(&sc->rnd_source);
373 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
374 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
375 ether_ifdetach(ifp);
376 if_detach(ifp);
377
378 #ifdef DIAGNOSTIC
379 if (sc->sc_pipe_tx != NULL)
380 aprint_debug_dev(self, "detach has active tx endpoint.\n");
381 if (sc->sc_pipe_rx != NULL)
382 aprint_debug_dev(self, "detach has active rx endpoint.\n");
383 if (sc->sc_pipe_intr != NULL)
384 aprint_debug_dev(self, "detach has active intr endpoint.\n");
385 #endif
386 sc->sc_attached = 0;
387
388 splx(s);
389
390 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
391
392 mutex_destroy(&sc->sc_mii_lock);
393
394 return 0;
395 }
396
397 #if 0
398 /* read memory */
399 Static int
400 udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
401 {
402 usb_device_request_t req;
403 usbd_status err;
404
405 if (sc == NULL)
406 return 0;
407
408 DPRINTFN(0x200,
409 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
410
411 if (sc->sc_dying)
412 return 0;
413
414 offset &= 0xffff;
415 len &= 0xff;
416
417 req.bmRequestType = UT_READ_VENDOR_DEVICE;
418 req.bRequest = UDAV_REQ_MEM_READ;
419 USETW(req.wValue, 0x0000);
420 USETW(req.wIndex, offset);
421 USETW(req.wLength, len);
422
423 sc->sc_refcnt++;
424 err = usbd_do_request(sc->sc_udev, &req, buf);
425 if (--sc->sc_refcnt < 0)
426 usb_detach_wakeupold(sc->sc_dev);
427 if (err) {
428 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
429 device_xname(sc->sc_dev), __func__, offset, err));
430 }
431
432 return err;
433 }
434
435 /* write memory */
436 Static int
437 udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
438 {
439 usb_device_request_t req;
440 usbd_status err;
441
442 if (sc == NULL)
443 return 0;
444
445 DPRINTFN(0x200,
446 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
447
448 if (sc->sc_dying)
449 return 0;
450
451 offset &= 0xffff;
452 len &= 0xff;
453
454 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
455 req.bRequest = UDAV_REQ_MEM_WRITE;
456 USETW(req.wValue, 0x0000);
457 USETW(req.wIndex, offset);
458 USETW(req.wLength, len);
459
460 sc->sc_refcnt++;
461 err = usbd_do_request(sc->sc_udev, &req, buf);
462 if (--sc->sc_refcnt < 0)
463 usb_detach_wakeupold(sc->sc_dev);
464 if (err) {
465 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
466 device_xname(sc->sc_dev), __func__, offset, err));
467 }
468
469 return err;
470 }
471
472 /* write memory */
473 Static int
474 udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
475 {
476 usb_device_request_t req;
477 usbd_status err;
478
479 if (sc == NULL)
480 return 0;
481
482 DPRINTFN(0x200,
483 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
484
485 if (sc->sc_dying)
486 return 0;
487
488 offset &= 0xffff;
489
490 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
491 req.bRequest = UDAV_REQ_MEM_WRITE1;
492 USETW(req.wValue, ch);
493 USETW(req.wIndex, offset);
494 USETW(req.wLength, 0x0000);
495
496 sc->sc_refcnt++;
497 err = usbd_do_request(sc->sc_udev, &req, NULL);
498 if (--sc->sc_refcnt < 0)
499 usb_detach_wakeupold(sc->sc_dev);
500 if (err) {
501 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
502 device_xname(sc->sc_dev), __func__, offset, err));
503 }
504
505 return err;
506 }
507 #endif
508
509 /* read register(s) */
510 Static int
511 udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
512 {
513 usb_device_request_t req;
514 usbd_status err;
515
516 if (sc == NULL)
517 return 0;
518
519 DPRINTFN(0x200,
520 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
521
522 if (sc->sc_dying)
523 return 0;
524
525 offset &= 0xff;
526 len &= 0xff;
527
528 req.bmRequestType = UT_READ_VENDOR_DEVICE;
529 req.bRequest = UDAV_REQ_REG_READ;
530 USETW(req.wValue, 0x0000);
531 USETW(req.wIndex, offset);
532 USETW(req.wLength, len);
533
534 sc->sc_refcnt++;
535 err = usbd_do_request(sc->sc_udev, &req, buf);
536 if (--sc->sc_refcnt < 0)
537 usb_detach_wakeupold(sc->sc_dev);
538 if (err) {
539 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
540 device_xname(sc->sc_dev), __func__, offset, err));
541 }
542
543 return err;
544 }
545
546 /* write register(s) */
547 Static int
548 udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
549 {
550 usb_device_request_t req;
551 usbd_status err;
552
553 if (sc == NULL)
554 return 0;
555
556 DPRINTFN(0x200,
557 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
558
559 if (sc->sc_dying)
560 return 0;
561
562 offset &= 0xff;
563 len &= 0xff;
564
565 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
566 req.bRequest = UDAV_REQ_REG_WRITE;
567 USETW(req.wValue, 0x0000);
568 USETW(req.wIndex, offset);
569 USETW(req.wLength, len);
570
571 sc->sc_refcnt++;
572 err = usbd_do_request(sc->sc_udev, &req, buf);
573 if (--sc->sc_refcnt < 0)
574 usb_detach_wakeupold(sc->sc_dev);
575 if (err) {
576 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
577 device_xname(sc->sc_dev), __func__, offset, err));
578 }
579
580 return err;
581 }
582
583 Static int
584 udav_csr_read1(struct udav_softc *sc, int offset)
585 {
586 uint8_t val = 0;
587
588 if (sc == NULL)
589 return 0;
590
591 DPRINTFN(0x200,
592 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
593
594 if (sc->sc_dying)
595 return 0;
596
597 return udav_csr_read(sc, offset, &val, 1) ? 0 : val;
598 }
599
600 /* write a register */
601 Static int
602 udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
603 {
604 usb_device_request_t req;
605 usbd_status err;
606
607 if (sc == NULL)
608 return 0;
609
610 DPRINTFN(0x200,
611 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
612
613 if (sc->sc_dying)
614 return 0;
615
616 offset &= 0xff;
617
618 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
619 req.bRequest = UDAV_REQ_REG_WRITE1;
620 USETW(req.wValue, ch);
621 USETW(req.wIndex, offset);
622 USETW(req.wLength, 0x0000);
623
624 sc->sc_refcnt++;
625 err = usbd_do_request(sc->sc_udev, &req, NULL);
626 if (--sc->sc_refcnt < 0)
627 usb_detach_wakeupold(sc->sc_dev);
628 if (err) {
629 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
630 device_xname(sc->sc_dev), __func__, offset, err));
631 }
632
633 return err;
634 }
635
636 Static int
637 udav_init(struct ifnet *ifp)
638 {
639 struct udav_softc *sc = ifp->if_softc;
640 struct mii_data *mii = GET_MII(sc);
641 uint8_t eaddr[ETHER_ADDR_LEN];
642 int rc, s;
643
644 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
645
646 if (sc->sc_dying)
647 return EIO;
648
649 s = splnet();
650
651 /* Cancel pending I/O and free all TX/RX buffers */
652 udav_stop(ifp, 1);
653
654 memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
655 udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
656
657 /* Initialize network control register */
658 /* Disable loopback */
659 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
660
661 /* Initialize RX control register */
662 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
663
664 /* If we want promiscuous mode, accept all physical frames. */
665 if (ifp->if_flags & IFF_PROMISC)
666 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
667 else
668 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
669
670 /* Load the multicast filter */
671 udav_setmulti(sc);
672
673 /* Enable RX */
674 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
675
676 /* clear POWER_DOWN state of internal PHY */
677 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
678 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
679
680 if ((rc = mii_mediachg(mii)) == ENXIO)
681 rc = 0;
682 else if (rc != 0)
683 goto out;
684
685 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
686 if (udav_openpipes(sc)) {
687 splx(s);
688 return EIO;
689 }
690 }
691
692 /* Initialize transmit ring */
693 if (udav_tx_list_init(sc)) {
694 printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
695 splx(s);
696 return EIO;
697 }
698
699 /* Initialize receive ring */
700 if (udav_rx_list_init(sc)) {
701 printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
702 splx(s);
703 return EIO;
704 }
705
706 /* Start up the receive pipe. */
707 for (size_t i = 0; i < UDAV_RX_LIST_CNT; i++) {
708 struct udav_chain *c = &sc->sc_cdata.udav_rx_chain[i];
709 usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, UDAV_BUFSZ,
710 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, udav_rxeof);
711 (void)usbd_transfer(c->udav_xfer);
712 DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev),
713 __func__));
714 }
715
716 ifp->if_flags |= IFF_RUNNING;
717 ifp->if_flags &= ~IFF_OACTIVE;
718
719 callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
720
721 out:
722 splx(s);
723 return rc;
724 }
725
726 Static void
727 udav_reset(struct udav_softc *sc)
728 {
729 int i;
730
731 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
732
733 if (sc->sc_dying)
734 return;
735
736 /* Select PHY */
737 #if 1
738 /*
739 * XXX: force select internal phy.
740 * external phy routines are not tested.
741 */
742 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
743 #else
744 if (sc->sc_flags & UDAV_EXT_PHY) {
745 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
746 } else {
747 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
748 }
749 #endif
750
751 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
752
753 for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
754 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
755 break;
756 delay(10); /* XXX */
757 }
758 delay(10000); /* XXX */
759 }
760
761 int
762 udav_activate(device_t self, enum devact act)
763 {
764 struct udav_softc *sc = device_private(self);
765
766 DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev),
767 __func__, act));
768 switch (act) {
769 case DVACT_DEACTIVATE:
770 if_deactivate(&sc->sc_ec.ec_if);
771 sc->sc_dying = 1;
772 return 0;
773 default:
774 return EOPNOTSUPP;
775 }
776 }
777
778 #define UDAV_BITS 6
779
780 #define UDAV_CALCHASH(addr) \
781 (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
782
783 Static void
784 udav_setmulti(struct udav_softc *sc)
785 {
786 struct ifnet *ifp;
787 struct ether_multi *enm;
788 struct ether_multistep step;
789 uint8_t hashes[8];
790 int h = 0;
791
792 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
793
794 if (sc->sc_dying)
795 return;
796
797 ifp = GET_IFP(sc);
798
799 if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
800 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
801 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
802 return;
803 }
804
805 if (ifp->if_flags & IFF_PROMISC) {
806 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
807 return;
808 } else if (ifp->if_flags & IFF_ALLMULTI) {
809 allmulti:
810 ifp->if_flags |= IFF_ALLMULTI;
811 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
812 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
813 return;
814 }
815
816 /* first, zot all the existing hash bits */
817 memset(hashes, 0x00, sizeof(hashes));
818 hashes[7] |= 0x80; /* broadcast address */
819 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
820
821 /* now program new ones */
822 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
823 while (enm != NULL) {
824 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
825 ETHER_ADDR_LEN) != 0)
826 goto allmulti;
827
828 h = UDAV_CALCHASH(enm->enm_addrlo);
829 hashes[h>>3] |= 1 << (h & 0x7);
830 ETHER_NEXT_MULTI(step, enm);
831 }
832
833 /* disable all multicast */
834 ifp->if_flags &= ~IFF_ALLMULTI;
835 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
836
837 /* write hash value to the register */
838 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
839 }
840
841 Static int
842 udav_openpipes(struct udav_softc *sc)
843 {
844 usbd_status err;
845 int error = 0;
846
847 if (sc->sc_dying)
848 return EIO;
849
850 sc->sc_refcnt++;
851
852 /* Open RX pipe */
853 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
854 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
855 if (err) {
856 printf("%s: open rx pipe failed: %s\n",
857 device_xname(sc->sc_dev), usbd_errstr(err));
858 error = EIO;
859 goto done;
860 }
861
862 /* Open TX pipe */
863 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
864 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
865 if (err) {
866 printf("%s: open tx pipe failed: %s\n",
867 device_xname(sc->sc_dev), usbd_errstr(err));
868 error = EIO;
869 goto done;
870 }
871
872 #if 0
873 /* XXX: interrupt endpoint is not yet supported */
874 /* Open Interrupt pipe */
875 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
876 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
877 &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN,
878 udav_intr, USBD_DEFAULT_INTERVAL);
879 if (err) {
880 printf("%s: open intr pipe failed: %s\n",
881 device_xname(sc->sc_dev), usbd_errstr(err));
882 error = EIO;
883 goto done;
884 }
885 #endif
886 done:
887 if (--sc->sc_refcnt < 0)
888 usb_detach_wakeupold(sc->sc_dev);
889
890 return error;
891 }
892
893 Static int
894 udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m)
895 {
896 struct mbuf *m_new = NULL;
897
898 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
899
900 if (m == NULL) {
901 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
902 if (m_new == NULL) {
903 printf("%s: no memory for rx list "
904 "-- packet dropped!\n", device_xname(sc->sc_dev));
905 return ENOBUFS;
906 }
907 MCLGET(m_new, M_DONTWAIT);
908 if (!(m_new->m_flags & M_EXT)) {
909 printf("%s: no memory for rx list "
910 "-- packet dropped!\n", device_xname(sc->sc_dev));
911 m_freem(m_new);
912 return ENOBUFS;
913 }
914 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
915 } else {
916 m_new = m;
917 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
918 m_new->m_data = m_new->m_ext.ext_buf;
919 }
920
921 m_adj(m_new, ETHER_ALIGN);
922 c->udav_mbuf = m_new;
923
924 return 0;
925 }
926
927
928 Static int
929 udav_rx_list_init(struct udav_softc *sc)
930 {
931 struct udav_cdata *cd;
932 struct udav_chain *c;
933 int i;
934
935 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
936
937 cd = &sc->sc_cdata;
938 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
939 c = &cd->udav_rx_chain[i];
940 c->udav_sc = sc;
941 c->udav_idx = i;
942 if (udav_newbuf(sc, c, NULL) == ENOBUFS)
943 return ENOBUFS;
944 if (c->udav_xfer == NULL) {
945 int error = usbd_create_xfer(sc->sc_pipe_rx,
946 UDAV_BUFSZ, 0, 0, &c->udav_xfer);
947 if (error)
948 return error;
949 c->udav_buf = usbd_get_buffer(c->udav_xfer);
950 }
951 }
952
953 return 0;
954 }
955
956 Static int
957 udav_tx_list_init(struct udav_softc *sc)
958 {
959 struct udav_cdata *cd;
960 struct udav_chain *c;
961 int i;
962
963 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
964
965 cd = &sc->sc_cdata;
966 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
967 c = &cd->udav_tx_chain[i];
968 c->udav_sc = sc;
969 c->udav_idx = i;
970 c->udav_mbuf = NULL;
971 if (c->udav_xfer == NULL) {
972 int error = usbd_create_xfer(sc->sc_pipe_tx, UDAV_BUFSZ,
973 USBD_FORCE_SHORT_XFER, 0, &c->udav_xfer);
974 if (error)
975 return error;
976 c->udav_buf = usbd_get_buffer(c->udav_xfer);
977 }
978 }
979
980 return 0;
981 }
982
983 Static void
984 udav_start(struct ifnet *ifp)
985 {
986 struct udav_softc *sc = ifp->if_softc;
987 struct mbuf *m_head = NULL;
988
989 DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev),
990 __func__, sc->sc_link));
991
992 if (sc->sc_dying)
993 return;
994
995 if (!sc->sc_link)
996 return;
997
998 if (ifp->if_flags & IFF_OACTIVE)
999 return;
1000
1001 IFQ_POLL(&ifp->if_snd, m_head);
1002 if (m_head == NULL)
1003 return;
1004
1005 if (udav_send(sc, m_head, 0)) {
1006 ifp->if_flags |= IFF_OACTIVE;
1007 return;
1008 }
1009
1010 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1011
1012 bpf_mtap(ifp, m_head, BPF_D_OUT);
1013
1014 ifp->if_flags |= IFF_OACTIVE;
1015
1016 /* Set a timeout in case the chip goes out to lunch. */
1017 ifp->if_timer = 5;
1018 }
1019
1020 Static int
1021 udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
1022 {
1023 int total_len;
1024 struct udav_chain *c;
1025 usbd_status err;
1026
1027 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1028
1029 c = &sc->sc_cdata.udav_tx_chain[idx];
1030
1031 /* Copy the mbuf data into a contiguous buffer */
1032 /* first 2 bytes are packet length */
1033 m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2);
1034 c->udav_mbuf = m;
1035 total_len = m->m_pkthdr.len;
1036 if (total_len < UDAV_MIN_FRAME_LEN) {
1037 memset(c->udav_buf + 2 + total_len, 0,
1038 UDAV_MIN_FRAME_LEN - total_len);
1039 total_len = UDAV_MIN_FRAME_LEN;
1040 }
1041
1042 /* Frame length is specified in the first 2bytes of the buffer */
1043 c->udav_buf[0] = (uint8_t)total_len;
1044 c->udav_buf[1] = (uint8_t)(total_len >> 8);
1045 total_len += 2;
1046
1047 usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, total_len,
1048 USBD_FORCE_SHORT_XFER, UDAV_TX_TIMEOUT, udav_txeof);
1049
1050 /* Transmit */
1051 sc->sc_refcnt++;
1052 err = usbd_transfer(c->udav_xfer);
1053 if (--sc->sc_refcnt < 0)
1054 usb_detach_wakeupold(sc->sc_dev);
1055 if (err != USBD_IN_PROGRESS) {
1056 printf("%s: udav_send error=%s\n", device_xname(sc->sc_dev),
1057 usbd_errstr(err));
1058 /* Stop the interface */
1059 usb_add_task(sc->sc_udev, &sc->sc_stop_task,
1060 USB_TASKQ_DRIVER);
1061 return EIO;
1062 }
1063
1064 DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev),
1065 __func__, total_len));
1066
1067 sc->sc_cdata.udav_tx_cnt++;
1068
1069 return 0;
1070 }
1071
1072 Static void
1073 udav_txeof(struct usbd_xfer *xfer, void *priv,
1074 usbd_status status)
1075 {
1076 struct udav_chain *c = priv;
1077 struct udav_softc *sc = c->udav_sc;
1078 struct ifnet *ifp = GET_IFP(sc);
1079 int s;
1080
1081 if (sc->sc_dying)
1082 return;
1083
1084 s = splnet();
1085
1086 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1087
1088 ifp->if_timer = 0;
1089 ifp->if_flags &= ~IFF_OACTIVE;
1090
1091 if (status != USBD_NORMAL_COMPLETION) {
1092 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1093 splx(s);
1094 return;
1095 }
1096 ifp->if_oerrors++;
1097 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
1098 usbd_errstr(status));
1099 if (status == USBD_STALLED) {
1100 sc->sc_refcnt++;
1101 usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
1102 if (--sc->sc_refcnt < 0)
1103 usb_detach_wakeupold(sc->sc_dev);
1104 }
1105 splx(s);
1106 return;
1107 }
1108
1109 ifp->if_opackets++;
1110
1111 m_freem(c->udav_mbuf);
1112 c->udav_mbuf = NULL;
1113
1114 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1115 udav_start(ifp);
1116
1117 splx(s);
1118 }
1119
1120 Static void
1121 udav_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1122 {
1123 struct udav_chain *c = priv;
1124 struct udav_softc *sc = c->udav_sc;
1125 struct ifnet *ifp = GET_IFP(sc);
1126 struct mbuf *m;
1127 uint32_t total_len;
1128 uint8_t *pktstat;
1129 int s;
1130
1131 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1132
1133 if (sc->sc_dying)
1134 return;
1135
1136 if (status != USBD_NORMAL_COMPLETION) {
1137 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1138 return;
1139 sc->sc_rx_errs++;
1140 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1141 printf("%s: %u usb errors on rx: %s\n",
1142 device_xname(sc->sc_dev), sc->sc_rx_errs,
1143 usbd_errstr(status));
1144 sc->sc_rx_errs = 0;
1145 }
1146 if (status == USBD_STALLED) {
1147 sc->sc_refcnt++;
1148 usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
1149 if (--sc->sc_refcnt < 0)
1150 usb_detach_wakeupold(sc->sc_dev);
1151 }
1152 goto done;
1153 }
1154
1155 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1156
1157 /* copy data to mbuf */
1158 m = c->udav_mbuf;
1159 memcpy(mtod(m, char *), c->udav_buf, total_len);
1160
1161 /* first byte in received data */
1162 pktstat = mtod(m, uint8_t *);
1163 m_adj(m, sizeof(uint8_t));
1164 DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(sc->sc_dev),
1165 *pktstat));
1166
1167 total_len = UGETW(mtod(m, uint8_t *));
1168 m_adj(m, sizeof(uint16_t));
1169
1170 if (*pktstat & UDAV_RSR_LCS) {
1171 ifp->if_collisions++;
1172 goto done;
1173 }
1174
1175 if (total_len < sizeof(struct ether_header) ||
1176 *pktstat & UDAV_RSR_ERR) {
1177 ifp->if_ierrors++;
1178 goto done;
1179 }
1180
1181 total_len -= ETHER_CRC_LEN;
1182
1183 m->m_pkthdr.len = m->m_len = total_len;
1184 m_set_rcvif(m, ifp);
1185
1186 s = splnet();
1187
1188 if (udav_newbuf(sc, c, NULL) == ENOBUFS) {
1189 ifp->if_ierrors++;
1190 goto done1;
1191 }
1192
1193 DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev),
1194 __func__, m->m_len));
1195 if_percpuq_enqueue((ifp)->if_percpuq, (m));
1196
1197 done1:
1198 splx(s);
1199
1200 done:
1201 /* Setup new transfer */
1202 usbd_setup_xfer(xfer, c, c->udav_buf, UDAV_BUFSZ, USBD_SHORT_XFER_OK,
1203 USBD_NO_TIMEOUT, udav_rxeof);
1204 sc->sc_refcnt++;
1205 usbd_transfer(xfer);
1206 if (--sc->sc_refcnt < 0)
1207 usb_detach_wakeupold(sc->sc_dev);
1208
1209 DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__));
1210 }
1211
1212 #if 0
1213 Static void udav_intr(void)
1214 {
1215 }
1216 #endif
1217
1218 Static int
1219 udav_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1220 {
1221 struct udav_softc *sc = ifp->if_softc;
1222 int s, error = 0;
1223
1224 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1225
1226 if (sc->sc_dying)
1227 return EIO;
1228
1229 s = splnet();
1230
1231 error = ether_ioctl(ifp, cmd, data);
1232 if (error == ENETRESET) {
1233 if (ifp->if_flags & IFF_RUNNING)
1234 udav_setmulti(sc);
1235 error = 0;
1236 }
1237
1238 splx(s);
1239
1240 return error;
1241 }
1242
1243 Static void
1244 udav_watchdog(struct ifnet *ifp)
1245 {
1246 struct udav_softc *sc = ifp->if_softc;
1247 struct udav_chain *c;
1248 usbd_status stat;
1249 int s;
1250
1251 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1252
1253 ifp->if_oerrors++;
1254 printf("%s: watchdog timeout\n", device_xname(sc->sc_dev));
1255
1256 s = splusb();
1257 c = &sc->sc_cdata.udav_tx_chain[0];
1258 usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat);
1259 udav_txeof(c->udav_xfer, c, stat);
1260
1261 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1262 udav_start(ifp);
1263 splx(s);
1264 }
1265
1266 Static void
1267 udav_stop_task(struct udav_softc *sc)
1268 {
1269 udav_stop(GET_IFP(sc), 1);
1270 }
1271
1272 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1273 Static void
1274 udav_stop(struct ifnet *ifp, int disable)
1275 {
1276 struct udav_softc *sc = ifp->if_softc;
1277 usbd_status err;
1278 int i;
1279
1280 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1281
1282 ifp->if_timer = 0;
1283
1284 udav_reset(sc);
1285
1286 callout_stop(&sc->sc_stat_ch);
1287
1288 /* Stop transfers */
1289 /* RX endpoint */
1290 if (sc->sc_pipe_rx != NULL) {
1291 err = usbd_abort_pipe(sc->sc_pipe_rx);
1292 if (err)
1293 printf("%s: abort rx pipe failed: %s\n",
1294 device_xname(sc->sc_dev), usbd_errstr(err));
1295 }
1296
1297 /* TX endpoint */
1298 if (sc->sc_pipe_tx != NULL) {
1299 err = usbd_abort_pipe(sc->sc_pipe_tx);
1300 if (err)
1301 printf("%s: abort tx pipe failed: %s\n",
1302 device_xname(sc->sc_dev), usbd_errstr(err));
1303 }
1304
1305 #if 0
1306 /* XXX: Interrupt endpoint is not yet supported!! */
1307 /* Interrupt endpoint */
1308 if (sc->sc_pipe_intr != NULL) {
1309 err = usbd_abort_pipe(sc->sc_pipe_intr);
1310 if (err)
1311 printf("%s: abort intr pipe failed: %s\n",
1312 device_xname(sc->sc_dev), usbd_errstr(err));
1313 err = usbd_close_pipe(sc->sc_pipe_intr);
1314 if (err)
1315 printf("%s: close intr pipe failed: %s\n",
1316 device_xname(sc->sc_dev), usbd_errstr(err));
1317 sc->sc_pipe_intr = NULL;
1318 }
1319 #endif
1320
1321 /* Free RX resources. */
1322 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
1323 if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) {
1324 m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf);
1325 sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL;
1326 }
1327 if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) {
1328 usbd_destroy_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer);
1329 sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL;
1330 }
1331 }
1332
1333 /* Free TX resources. */
1334 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
1335 if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) {
1336 m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf);
1337 sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL;
1338 }
1339 if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) {
1340 usbd_destroy_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer);
1341 sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL;
1342 }
1343 }
1344
1345 /* Close pipes */
1346 /* RX endpoint */
1347 if (sc->sc_pipe_rx != NULL) {
1348 err = usbd_close_pipe(sc->sc_pipe_rx);
1349 if (err)
1350 printf("%s: close rx pipe failed: %s\n",
1351 device_xname(sc->sc_dev), usbd_errstr(err));
1352 sc->sc_pipe_rx = NULL;
1353 }
1354
1355 /* TX endpoint */
1356 if (sc->sc_pipe_tx != NULL) {
1357 err = usbd_close_pipe(sc->sc_pipe_tx);
1358 if (err)
1359 printf("%s: close tx pipe failed: %s\n",
1360 device_xname(sc->sc_dev), usbd_errstr(err));
1361 sc->sc_pipe_tx = NULL;
1362 }
1363
1364 if (!ISSET(sc->sc_flags, UDAV_NO_PHY))
1365 sc->sc_link = 0;
1366 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1367 }
1368
1369 /* Set media options */
1370 Static int
1371 udav_ifmedia_change(struct ifnet *ifp)
1372 {
1373 struct udav_softc *sc = ifp->if_softc;
1374 struct mii_data *mii = GET_MII(sc);
1375 int rc;
1376
1377 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1378
1379 if (sc->sc_dying)
1380 return 0;
1381
1382 sc->sc_link = 0;
1383 if ((rc = mii_mediachg(mii)) == ENXIO)
1384 return 0;
1385 return rc;
1386 }
1387
1388 /* Report current media status. */
1389 Static void
1390 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1391 {
1392 struct udav_softc *sc = ifp->if_softc;
1393
1394 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1395
1396 if (sc->sc_dying)
1397 return;
1398
1399 ether_mediastatus(ifp, ifmr);
1400 }
1401
1402 Static void
1403 udav_tick(void *xsc)
1404 {
1405 struct udav_softc *sc = xsc;
1406
1407 if (sc == NULL)
1408 return;
1409
1410 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1411 __func__));
1412
1413 if (sc->sc_dying)
1414 return;
1415
1416 /* Perform periodic stuff in process context */
1417 usb_add_task(sc->sc_udev, &sc->sc_tick_task,
1418 USB_TASKQ_DRIVER);
1419 }
1420
1421 Static void
1422 udav_tick_task(void *xsc)
1423 {
1424 struct udav_softc *sc = xsc;
1425 struct ifnet *ifp;
1426 struct mii_data *mii;
1427 int s;
1428
1429 if (sc == NULL)
1430 return;
1431
1432 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1433 __func__));
1434
1435 if (sc->sc_dying)
1436 return;
1437
1438 ifp = GET_IFP(sc);
1439 mii = GET_MII(sc);
1440
1441 if (mii == NULL)
1442 return;
1443
1444 s = splnet();
1445
1446 mii_tick(mii);
1447 if (!sc->sc_link) {
1448 mii_pollstat(mii);
1449 if (mii->mii_media_status & IFM_ACTIVE &&
1450 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1451 DPRINTF(("%s: %s: got link\n",
1452 device_xname(sc->sc_dev), __func__));
1453 sc->sc_link++;
1454 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1455 udav_start(ifp);
1456 }
1457 }
1458
1459 callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
1460
1461 splx(s);
1462 }
1463
1464 /* Get exclusive access to the MII registers */
1465 Static void
1466 udav_lock_mii(struct udav_softc *sc)
1467 {
1468 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1469 __func__));
1470
1471 sc->sc_refcnt++;
1472 mutex_enter(&sc->sc_mii_lock);
1473 }
1474
1475 Static void
1476 udav_unlock_mii(struct udav_softc *sc)
1477 {
1478 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev),
1479 __func__));
1480
1481 mutex_exit(&sc->sc_mii_lock);
1482 if (--sc->sc_refcnt < 0)
1483 usb_detach_wakeupold(sc->sc_dev);
1484 }
1485
1486 Static int
1487 udav_miibus_readreg(device_t dev, int phy, int reg)
1488 {
1489 struct udav_softc *sc;
1490 uint8_t val[2];
1491 uint16_t data16;
1492
1493 if (dev == NULL)
1494 return 0;
1495
1496 sc = device_private(dev);
1497
1498 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1499 device_xname(sc->sc_dev), __func__, phy, reg));
1500
1501 if (sc->sc_dying) {
1502 #ifdef DIAGNOSTIC
1503 printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1504 __func__);
1505 #endif
1506 return 0;
1507 }
1508
1509 /* XXX: one PHY only for the internal PHY */
1510 if (phy != 0) {
1511 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1512 device_xname(sc->sc_dev), __func__, phy));
1513 return 0;
1514 }
1515
1516 udav_lock_mii(sc);
1517
1518 /* select internal PHY and set PHY register address */
1519 udav_csr_write1(sc, UDAV_EPAR,
1520 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1521
1522 /* select PHY operation and start read command */
1523 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
1524
1525 /* XXX: should be wait? */
1526
1527 /* end read command */
1528 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
1529
1530 /* retrieve the result from data registers */
1531 udav_csr_read(sc, UDAV_EPDRL, val, 2);
1532
1533 udav_unlock_mii(sc);
1534
1535 data16 = val[0] | (val[1] << 8);
1536
1537 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1538 device_xname(sc->sc_dev), __func__, phy, reg, data16));
1539
1540 return data16;
1541 }
1542
1543 Static void
1544 udav_miibus_writereg(device_t dev, int phy, int reg, int data)
1545 {
1546 struct udav_softc *sc;
1547 uint8_t val[2];
1548
1549 if (dev == NULL)
1550 return;
1551
1552 sc = device_private(dev);
1553
1554 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1555 device_xname(sc->sc_dev), __func__, phy, reg, data));
1556
1557 if (sc->sc_dying) {
1558 #ifdef DIAGNOSTIC
1559 printf("%s: %s: dying\n", device_xname(sc->sc_dev),
1560 __func__);
1561 #endif
1562 return;
1563 }
1564
1565 /* XXX: one PHY only for the internal PHY */
1566 if (phy != 0) {
1567 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1568 device_xname(sc->sc_dev), __func__, phy));
1569 return;
1570 }
1571
1572 udav_lock_mii(sc);
1573
1574 /* select internal PHY and set PHY register address */
1575 udav_csr_write1(sc, UDAV_EPAR,
1576 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1577
1578 /* put the value to the data registers */
1579 val[0] = data & 0xff;
1580 val[1] = (data >> 8) & 0xff;
1581 udav_csr_write(sc, UDAV_EPDRL, val, 2);
1582
1583 /* select PHY operation and start write command */
1584 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
1585
1586 /* XXX: should be wait? */
1587
1588 /* end write command */
1589 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
1590
1591 udav_unlock_mii(sc);
1592
1593 return;
1594 }
1595
1596 Static void
1597 udav_miibus_statchg(struct ifnet *ifp)
1598 {
1599 #ifdef UDAV_DEBUG
1600
1601 if (ifp == NULL)
1602 return;
1603
1604 DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
1605 #endif
1606 /* Nothing to do */
1607 }
1608