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