1 /*	$NetBSD: if_udav.c,v 1.48 2016/07/07 06:55:42 msaitoh 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@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.48 2016/07/07 06:55:42 msaitoh 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,
94     udav_detach, udav_activate);
95 
96 Static int udav_openpipes(struct udav_softc *);
97 Static int udav_rx_list_init(struct udav_softc *);
98 Static int udav_tx_list_init(struct udav_softc *);
99 Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
100 Static void udav_start(struct ifnet *);
101 Static int udav_send(struct udav_softc *, struct mbuf *, int);
102 Static void udav_txeof(struct usbd_xfer *, void *, usbd_status);
103 Static void udav_rxeof(struct usbd_xfer *, void *, usbd_status);
104 Static void udav_tick(void *);
105 Static void udav_tick_task(void *);
106 Static int udav_ioctl(struct ifnet *, u_long, void *);
107 Static void udav_stop_task(struct udav_softc *);
108 Static void udav_stop(struct ifnet *, int);
109 Static void udav_watchdog(struct ifnet *);
110 Static int udav_ifmedia_change(struct ifnet *);
111 Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
112 Static void udav_lock_mii(struct udav_softc *);
113 Static void udav_unlock_mii(struct udav_softc *);
114 Static int udav_miibus_readreg(device_t, int, int);
115 Static void udav_miibus_writereg(device_t, int, int, int);
116 Static void udav_miibus_statchg(struct ifnet *);
117 Static int udav_init(struct ifnet *);
118 Static void udav_setmulti(struct udav_softc *);
119 Static void udav_reset(struct udav_softc *);
120 
121 Static int udav_csr_read(struct udav_softc *, int, void *, int);
122 Static int udav_csr_write(struct udav_softc *, int, void *, int);
123 Static int udav_csr_read1(struct udav_softc *, int);
124 Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
125 
126 #if 0
127 Static int udav_mem_read(struct udav_softc *, int, void *, int);
128 Static int udav_mem_write(struct udav_softc *, int, void *, int);
129 Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
130 #endif
131 
132 /* Macros */
133 #ifdef UDAV_DEBUG
134 #define DPRINTF(x)	if (udavdebug) printf x
135 #define DPRINTFN(n,x)	if (udavdebug >= (n)) printf x
136 int udavdebug = 0;
137 #else
138 #define DPRINTF(x)
139 #define DPRINTFN(n,x)
140 #endif
141 
142 #define	UDAV_SETBIT(sc, reg, x)	\
143 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
144 
145 #define	UDAV_CLRBIT(sc, reg, x)	\
146 	udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
147 
148 static const struct udav_type {
149 	struct usb_devno udav_dev;
150 	uint16_t udav_flags;
151 #define UDAV_EXT_PHY	0x0001
152 #define UDAV_NO_PHY	0x0002
153 } udav_devs [] = {
154 	/* Corega USB-TXC */
155 	{{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
156 	/* ShanTou ST268 USB NIC */
157 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268_USB_NIC }, 0},
158 	/* ShanTou ADM8515 */
159 	{{ USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515 }, 0},
160 	/* SUNRISING SR9600 */
161 	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_SR9600 }, 0 },
162 	/* SUNRISING QF9700 */
163 	{{ USB_VENDOR_SUNRISING, USB_PRODUCT_SUNRISING_QF9700 }, UDAV_NO_PHY },
164 	/* QUAN DM9601 */
165 	{{USB_VENDOR_QUAN, USB_PRODUCT_QUAN_DM9601 }, 0},
166 #if 0
167 	/* DAVICOM DM9601 Generic? */
168 	/*  XXX: The following ids was obtained from the data sheet. */
169 	{{ 0x0a46, 0x9601 }, 0},
170 #endif
171 };
172 #define udav_lookup(v, p) ((const struct udav_type *)usb_lookup(udav_devs, v, p))
173 
174 
175 /* Probe */
176 int
udav_match(device_t parent,cfdata_t match,void * aux)177 udav_match(device_t parent, cfdata_t match, void *aux)
178 {
179 	struct usb_attach_arg *uaa = aux;
180 
181 	return udav_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
182 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
183 }
184 
185 /* Attach */
186 void
udav_attach(device_t parent,device_t self,void * aux)187 udav_attach(device_t parent, device_t self, void *aux)
188 {
189 	struct udav_softc *sc = device_private(self);
190 	struct usb_attach_arg *uaa = aux;
191 	struct usbd_device *dev = uaa->uaa_device;
192 	struct usbd_interface *iface;
193 	usbd_status err;
194 	usb_interface_descriptor_t *id;
195 	usb_endpoint_descriptor_t *ed;
196 	char *devinfop;
197 	struct ifnet *ifp;
198 	struct mii_data *mii;
199 	u_char eaddr[ETHER_ADDR_LEN];
200 	int i, s;
201 
202 	sc->sc_dev = self;
203 
204 	aprint_naive("\n");
205 	aprint_normal("\n");
206 
207 	devinfop = usbd_devinfo_alloc(dev, 0);
208 	aprint_normal_dev(self, "%s\n", devinfop);
209 	usbd_devinfo_free(devinfop);
210 
211 	/* Move the device into the configured state. */
212 	err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1); /* idx 0 */
213 	if (err) {
214 		aprint_error_dev(self, "failed to set configuration"
215 		    ", err=%s\n", usbd_errstr(err));
216 		goto bad;
217 	}
218 
219 	usb_init_task(&sc->sc_tick_task, udav_tick_task, sc, 0);
220 	mutex_init(&sc->sc_mii_lock, MUTEX_DEFAULT, IPL_NONE);
221 	usb_init_task(&sc->sc_stop_task, (void (*)(void *))udav_stop_task, sc,
222 	    0);
223 
224 	/* get control interface */
225 	err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
226 	if (err) {
227 		aprint_error_dev(self, "failed to get interface, err=%s\n",
228 		       usbd_errstr(err));
229 		goto bad;
230 	}
231 
232 	sc->sc_udev = dev;
233 	sc->sc_ctl_iface = iface;
234 	sc->sc_flags = udav_lookup(uaa->uaa_vendor,
235 	    uaa->uaa_product)->udav_flags;
236 
237 	/* get interface descriptor */
238 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
239 
240 	/* find endpoints */
241 	sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
242 	for (i = 0; i < id->bNumEndpoints; i++) {
243 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
244 		if (ed == NULL) {
245 			aprint_error_dev(self, "couldn't get endpoint %d\n", i);
246 			goto bad;
247 		}
248 		if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
249 		    UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
250 			sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
251 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
252 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
253 			sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
254 		else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
255 			 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
256 			sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
257 	}
258 
259 	if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
260 	    sc->sc_intrin_no == -1) {
261 		aprint_error_dev(self, "missing endpoint\n");
262 		goto bad;
263 	}
264 
265 	s = splnet();
266 
267 	/* reset the adapter */
268 	udav_reset(sc);
269 
270 	/* Get Ethernet Address */
271 	err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
272 	if (err) {
273 		aprint_error_dev(self, "read MAC address failed\n");
274 		splx(s);
275 		goto bad;
276 	}
277 
278 	/* Print Ethernet Address */
279 	aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr));
280 
281 	/* initialize interface information */
282 	ifp = GET_IFP(sc);
283 	ifp->if_softc = sc;
284 	ifp->if_mtu = ETHERMTU;
285 	strncpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
286 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
287 	ifp->if_start = udav_start;
288 	ifp->if_ioctl = udav_ioctl;
289 	ifp->if_watchdog = udav_watchdog;
290 	ifp->if_init = udav_init;
291 	ifp->if_stop = udav_stop;
292 
293 	IFQ_SET_READY(&ifp->if_snd);
294 
295 	if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
296 		sc->sc_link = 1;
297 		goto skipmii;
298 	}
299 
300 	/*
301 	 * Do ifmedia setup.
302 	 */
303 	mii = &sc->sc_mii;
304 	mii->mii_ifp = ifp;
305 	mii->mii_readreg = udav_miibus_readreg;
306 	mii->mii_writereg = udav_miibus_writereg;
307 	mii->mii_statchg = udav_miibus_statchg;
308 	mii->mii_flags = MIIF_AUTOTSLEEP;
309 	sc->sc_ec.ec_mii = mii;
310 	ifmedia_init(&mii->mii_media, 0,
311 		     udav_ifmedia_change, udav_ifmedia_status);
312 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
313 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
314 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
315 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
316 	} else
317 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
318 
319 skipmii:
320 	/* attach the interface */
321 	if_attach(ifp);
322 	ether_ifattach(ifp, eaddr);
323 
324 	rnd_attach_source(&sc->rnd_source, device_xname(self),
325 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
326 
327 	callout_init(&sc->sc_stat_ch, 0);
328 	sc->sc_attached = 1;
329 	splx(s);
330 
331 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev);
332 
333 	return;
334 
335  bad:
336 	sc->sc_dying = 1;
337 	return;
338 }
339 
340 /* detach */
341 int
udav_detach(device_t self,int flags)342 udav_detach(device_t self, int flags)
343 {
344 	struct udav_softc *sc = device_private(self);
345 	struct ifnet *ifp = GET_IFP(sc);
346 	int s;
347 
348 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
349 
350 	/* Detached before attached finished */
351 	if (!sc->sc_attached)
352 		return 0;
353 
354 	callout_stop(&sc->sc_stat_ch);
355 
356 	/* Remove any pending tasks */
357 	usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
358 	usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
359 
360 	s = splusb();
361 
362 	if (--sc->sc_refcnt >= 0) {
363 		/* Wait for processes to go away */
364 		usb_detach_waitold(sc->sc_dev);
365 	}
366 	if (ifp->if_flags & IFF_RUNNING)
367 		udav_stop(GET_IFP(sc), 1);
368 
369 	rnd_detach_source(&sc->rnd_source);
370 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
371 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
372 	ether_ifdetach(ifp);
373 	if_detach(ifp);
374 
375 #ifdef DIAGNOSTIC
376 	if (sc->sc_pipe_tx != NULL)
377 		aprint_debug_dev(self, "detach has active tx endpoint.\n");
378 	if (sc->sc_pipe_rx != NULL)
379 		aprint_debug_dev(self, "detach has active rx endpoint.\n");
380 	if (sc->sc_pipe_intr != NULL)
381 		aprint_debug_dev(self, "detach has active intr endpoint.\n");
382 #endif
383 	sc->sc_attached = 0;
384 
385 	splx(s);
386 
387 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
388 
389 	mutex_destroy(&sc->sc_mii_lock);
390 
391 	return 0;
392 }
393 
394 #if 0
395 /* read memory */
396 Static int
397 udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
398 {
399 	usb_device_request_t req;
400 	usbd_status err;
401 
402 	if (sc == NULL)
403 		return 0;
404 
405 	DPRINTFN(0x200,
406 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
407 
408 	if (sc->sc_dying)
409 		return 0;
410 
411 	offset &= 0xffff;
412 	len &= 0xff;
413 
414 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
415 	req.bRequest = UDAV_REQ_MEM_READ;
416 	USETW(req.wValue, 0x0000);
417 	USETW(req.wIndex, offset);
418 	USETW(req.wLength, len);
419 
420 	sc->sc_refcnt++;
421 	err = usbd_do_request(sc->sc_udev, &req, buf);
422 	if (--sc->sc_refcnt < 0)
423 		usb_detach_wakeupold(sc->sc_dev);
424 	if (err) {
425 		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
426 			 device_xname(sc->sc_dev), __func__, offset, err));
427 	}
428 
429 	return err;
430 }
431 
432 /* write memory */
433 Static int
434 udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
435 {
436 	usb_device_request_t req;
437 	usbd_status err;
438 
439 	if (sc == NULL)
440 		return 0;
441 
442 	DPRINTFN(0x200,
443 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
444 
445 	if (sc->sc_dying)
446 		return 0;
447 
448 	offset &= 0xffff;
449 	len &= 0xff;
450 
451 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
452 	req.bRequest = UDAV_REQ_MEM_WRITE;
453 	USETW(req.wValue, 0x0000);
454 	USETW(req.wIndex, offset);
455 	USETW(req.wLength, len);
456 
457 	sc->sc_refcnt++;
458 	err = usbd_do_request(sc->sc_udev, &req, buf);
459 	if (--sc->sc_refcnt < 0)
460 		usb_detach_wakeupold(sc->sc_dev);
461 	if (err) {
462 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
463 			 device_xname(sc->sc_dev), __func__, offset, err));
464 	}
465 
466 	return err;
467 }
468 
469 /* write memory */
470 Static int
471 udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
472 {
473 	usb_device_request_t req;
474 	usbd_status err;
475 
476 	if (sc == NULL)
477 		return 0;
478 
479 	DPRINTFN(0x200,
480 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
481 
482 	if (sc->sc_dying)
483 		return 0;
484 
485 	offset &= 0xffff;
486 
487 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
488 	req.bRequest = UDAV_REQ_MEM_WRITE1;
489 	USETW(req.wValue, ch);
490 	USETW(req.wIndex, offset);
491 	USETW(req.wLength, 0x0000);
492 
493 	sc->sc_refcnt++;
494 	err = usbd_do_request(sc->sc_udev, &req, NULL);
495 	if (--sc->sc_refcnt < 0)
496 		usb_detach_wakeupold(sc->sc_dev);
497 	if (err) {
498 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
499 			 device_xname(sc->sc_dev), __func__, offset, err));
500 	}
501 
502 	return err;
503 }
504 #endif
505 
506 /* read register(s) */
507 Static int
udav_csr_read(struct udav_softc * sc,int offset,void * buf,int len)508 udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
509 {
510 	usb_device_request_t req;
511 	usbd_status err;
512 
513 	if (sc == NULL)
514 		return 0;
515 
516 	DPRINTFN(0x200,
517 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
518 
519 	if (sc->sc_dying)
520 		return 0;
521 
522 	offset &= 0xff;
523 	len &= 0xff;
524 
525 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
526 	req.bRequest = UDAV_REQ_REG_READ;
527 	USETW(req.wValue, 0x0000);
528 	USETW(req.wIndex, offset);
529 	USETW(req.wLength, len);
530 
531 	sc->sc_refcnt++;
532 	err = usbd_do_request(sc->sc_udev, &req, buf);
533 	if (--sc->sc_refcnt < 0)
534 		usb_detach_wakeupold(sc->sc_dev);
535 	if (err) {
536 		DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
537 			 device_xname(sc->sc_dev), __func__, offset, err));
538 	}
539 
540 	return err;
541 }
542 
543 /* write register(s) */
544 Static int
udav_csr_write(struct udav_softc * sc,int offset,void * buf,int len)545 udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
546 {
547 	usb_device_request_t req;
548 	usbd_status err;
549 
550 	if (sc == NULL)
551 		return 0;
552 
553 	DPRINTFN(0x200,
554 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
555 
556 	if (sc->sc_dying)
557 		return 0;
558 
559 	offset &= 0xff;
560 	len &= 0xff;
561 
562 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
563 	req.bRequest = UDAV_REQ_REG_WRITE;
564 	USETW(req.wValue, 0x0000);
565 	USETW(req.wIndex, offset);
566 	USETW(req.wLength, len);
567 
568 	sc->sc_refcnt++;
569 	err = usbd_do_request(sc->sc_udev, &req, buf);
570 	if (--sc->sc_refcnt < 0)
571 		usb_detach_wakeupold(sc->sc_dev);
572 	if (err) {
573 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
574 			 device_xname(sc->sc_dev), __func__, offset, err));
575 	}
576 
577 	return err;
578 }
579 
580 Static int
udav_csr_read1(struct udav_softc * sc,int offset)581 udav_csr_read1(struct udav_softc *sc, int offset)
582 {
583 	uint8_t val = 0;
584 
585 	if (sc == NULL)
586 		return 0;
587 
588 	DPRINTFN(0x200,
589 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
590 
591 	if (sc->sc_dying)
592 		return 0;
593 
594 	return udav_csr_read(sc, offset, &val, 1) ? 0 : val;
595 }
596 
597 /* write a register */
598 Static int
udav_csr_write1(struct udav_softc * sc,int offset,unsigned char ch)599 udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
600 {
601 	usb_device_request_t req;
602 	usbd_status err;
603 
604 	if (sc == NULL)
605 		return 0;
606 
607 	DPRINTFN(0x200,
608 		("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
609 
610 	if (sc->sc_dying)
611 		return 0;
612 
613 	offset &= 0xff;
614 
615 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
616 	req.bRequest = UDAV_REQ_REG_WRITE1;
617 	USETW(req.wValue, ch);
618 	USETW(req.wIndex, offset);
619 	USETW(req.wLength, 0x0000);
620 
621 	sc->sc_refcnt++;
622 	err = usbd_do_request(sc->sc_udev, &req, NULL);
623 	if (--sc->sc_refcnt < 0)
624 		usb_detach_wakeupold(sc->sc_dev);
625 	if (err) {
626 		DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
627 			 device_xname(sc->sc_dev), __func__, offset, err));
628 	}
629 
630 	return err;
631 }
632 
633 Static int
udav_init(struct ifnet * ifp)634 udav_init(struct ifnet *ifp)
635 {
636 	struct udav_softc *sc = ifp->if_softc;
637 	struct mii_data *mii = GET_MII(sc);
638 	uint8_t eaddr[ETHER_ADDR_LEN];
639 	int rc, s;
640 
641 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
642 
643 	if (sc->sc_dying)
644 		return EIO;
645 
646 	s = splnet();
647 
648 	/* Cancel pending I/O and free all TX/RX buffers */
649 	udav_stop(ifp, 1);
650 
651 	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
652 	udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
653 
654 	/* Initialize network control register */
655 	/*  Disable loopback  */
656 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
657 
658 	/* Initialize RX control register */
659 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
660 
661 	/* If we want promiscuous mode, accept all physical frames. */
662 	if (ifp->if_flags & IFF_PROMISC)
663 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
664 	else
665 		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
666 
667 	/* Load the multicast filter */
668 	udav_setmulti(sc);
669 
670 	/* Enable RX */
671 	UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
672 
673 	/* clear POWER_DOWN state of internal PHY */
674 	UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
675 	UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
676 
677 	if ((rc = mii_mediachg(mii)) == ENXIO)
678 		rc = 0;
679 	else if (rc != 0)
680 		goto out;
681 
682 	if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
683 		if (udav_openpipes(sc)) {
684 			splx(s);
685 			return EIO;
686 		}
687 	}
688 
689 	/* Initialize transmit ring */
690 	if (udav_tx_list_init(sc)) {
691 		printf("%s: tx list init failed\n", device_xname(sc->sc_dev));
692 		splx(s);
693 		return EIO;
694 	}
695 
696 	/* Initialize receive ring */
697 	if (udav_rx_list_init(sc)) {
698 		printf("%s: rx list init failed\n", device_xname(sc->sc_dev));
699 		splx(s);
700 		return EIO;
701 	}
702 
703 	/* Start up the receive pipe. */
704 	for (size_t i = 0; i < UDAV_RX_LIST_CNT; i++) {
705 		struct udav_chain *c = &sc->sc_cdata.udav_rx_chain[i];
706 		usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, UDAV_BUFSZ,
707 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, udav_rxeof);
708 		(void)usbd_transfer(c->udav_xfer);
709 		DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev),
710 			 __func__));
711 	}
712 
713 	ifp->if_flags |= IFF_RUNNING;
714 	ifp->if_flags &= ~IFF_OACTIVE;
715 
716 	callout_reset(&sc->sc_stat_ch, hz, udav_tick, sc);
717 
718 out:
719 	splx(s);
720 	return rc;
721 }
722 
723 Static void
udav_reset(struct udav_softc * sc)724 udav_reset(struct udav_softc *sc)
725 {
726 	int i;
727 
728 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
729 
730 	if (sc->sc_dying)
731 		return;
732 
733 	/* Select PHY */
734 #if 1
735 	/*
736 	 * XXX: force select internal phy.
737 	 *	external phy routines are not tested.
738 	 */
739 	UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
740 #else
741 	if (sc->sc_flags & UDAV_EXT_PHY) {
742 		UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
743 	} else {
744 		UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
745 	}
746 #endif
747 
748 	UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
749 
750 	for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
751 		if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
752 			break;
753 		delay(10);	/* XXX */
754 	}
755 	delay(10000);		/* XXX */
756 }
757 
758 int
udav_activate(device_t self,enum devact act)759 udav_activate(device_t self, enum devact act)
760 {
761 	struct udav_softc *sc = device_private(self);
762 
763 	DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev),
764 		 __func__, act));
765 	switch (act) {
766 	case DVACT_DEACTIVATE:
767 		if_deactivate(&sc->sc_ec.ec_if);
768 		sc->sc_dying = 1;
769 		return 0;
770 	default:
771 		return EOPNOTSUPP;
772 	}
773 }
774 
775 #define UDAV_BITS	6
776 
777 #define UDAV_CALCHASH(addr) \
778 	(ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
779 
780 Static void
udav_setmulti(struct udav_softc * sc)781 udav_setmulti(struct udav_softc *sc)
782 {
783 	struct ifnet *ifp;
784 	struct ether_multi *enm;
785 	struct ether_multistep step;
786 	uint8_t hashes[8];
787 	int h = 0;
788 
789 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
790 
791 	if (sc->sc_dying)
792 		return;
793 
794 	ifp = GET_IFP(sc);
795 
796 	if (ISSET(sc->sc_flags, UDAV_NO_PHY)) {
797 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
798 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
799 		return;
800 	}
801 
802 	if (ifp->if_flags & IFF_PROMISC) {
803 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
804 		return;
805 	} else if (ifp->if_flags & IFF_ALLMULTI) {
806 	allmulti:
807 		ifp->if_flags |= IFF_ALLMULTI;
808 		UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
809 		UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
810 		return;
811 	}
812 
813 	/* first, zot all the existing hash bits */
814 	memset(hashes, 0x00, sizeof(hashes));
815 	hashes[7] |= 0x80;	/* broadcast address */
816 	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
817 
818 	/* now program new ones */
819 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
820 	while (enm != NULL) {
821 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
822 			   ETHER_ADDR_LEN) != 0)
823 			goto allmulti;
824 
825 		h = UDAV_CALCHASH(enm->enm_addrlo);
826 		hashes[h>>3] |= 1 << (h & 0x7);
827 		ETHER_NEXT_MULTI(step, enm);
828 	}
829 
830 	/* disable all multicast */
831 	ifp->if_flags &= ~IFF_ALLMULTI;
832 	UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
833 
834 	/* write hash value to the register */
835 	udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
836 }
837 
838 Static int
udav_openpipes(struct udav_softc * sc)839 udav_openpipes(struct udav_softc *sc)
840 {
841 	usbd_status err;
842 	int error = 0;
843 
844 	if (sc->sc_dying)
845 		return EIO;
846 
847 	sc->sc_refcnt++;
848 
849 	/* Open RX pipe */
850 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
851 			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
852 	if (err) {
853 		printf("%s: open rx pipe failed: %s\n",
854 		       device_xname(sc->sc_dev), usbd_errstr(err));
855 		error = EIO;
856 		goto done;
857 	}
858 
859 	/* Open TX pipe */
860 	err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
861 			     USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
862 	if (err) {
863 		printf("%s: open tx pipe failed: %s\n",
864 		       device_xname(sc->sc_dev), usbd_errstr(err));
865 		error = EIO;
866 		goto done;
867 	}
868 
869 #if 0
870 	/* XXX: interrupt endpoint is not yet supported */
871 	/* Open Interrupt pipe */
872 	err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
873 				  USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
874 				  &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN,
875 				  udav_intr, USBD_DEFAULT_INTERVAL);
876 	if (err) {
877 		printf("%s: open intr pipe failed: %s\n",
878 		       device_xname(sc->sc_dev), usbd_errstr(err));
879 		error = EIO;
880 		goto done;
881 	}
882 #endif
883  done:
884 	if (--sc->sc_refcnt < 0)
885 		usb_detach_wakeupold(sc->sc_dev);
886 
887 	return error;
888 }
889 
890 Static int
udav_newbuf(struct udav_softc * sc,struct udav_chain * c,struct mbuf * m)891 udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m)
892 {
893 	struct mbuf *m_new = NULL;
894 
895 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
896 
897 	if (m == NULL) {
898 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
899 		if (m_new == NULL) {
900 			printf("%s: no memory for rx list "
901 			       "-- packet dropped!\n", device_xname(sc->sc_dev));
902 			return ENOBUFS;
903 		}
904 		MCLGET(m_new, M_DONTWAIT);
905 		if (!(m_new->m_flags & M_EXT)) {
906 			printf("%s: no memory for rx list "
907 			       "-- packet dropped!\n", device_xname(sc->sc_dev));
908 			m_freem(m_new);
909 			return ENOBUFS;
910 		}
911 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
912 	} else {
913 		m_new = m;
914 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
915 		m_new->m_data = m_new->m_ext.ext_buf;
916 	}
917 
918 	m_adj(m_new, ETHER_ALIGN);
919 	c->udav_mbuf = m_new;
920 
921 	return 0;
922 }
923 
924 
925 Static int
udav_rx_list_init(struct udav_softc * sc)926 udav_rx_list_init(struct udav_softc *sc)
927 {
928 	struct udav_cdata *cd;
929 	struct udav_chain *c;
930 	int i;
931 
932 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
933 
934 	cd = &sc->sc_cdata;
935 	for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
936 		c = &cd->udav_rx_chain[i];
937 		c->udav_sc = sc;
938 		c->udav_idx = i;
939 		if (udav_newbuf(sc, c, NULL) == ENOBUFS)
940 			return ENOBUFS;
941 		if (c->udav_xfer == NULL) {
942 			int error = usbd_create_xfer(sc->sc_pipe_rx,
943 			    UDAV_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->udav_xfer);
944 			if (error)
945 				return error;
946 			c->udav_buf = usbd_get_buffer(c->udav_xfer);
947 		}
948 	}
949 
950 	return 0;
951 }
952 
953 Static int
udav_tx_list_init(struct udav_softc * sc)954 udav_tx_list_init(struct udav_softc *sc)
955 {
956 	struct udav_cdata *cd;
957 	struct udav_chain *c;
958 	int i;
959 
960 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
961 
962 	cd = &sc->sc_cdata;
963 	for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
964 		c = &cd->udav_tx_chain[i];
965 		c->udav_sc = sc;
966 		c->udav_idx = i;
967 		c->udav_mbuf = NULL;
968 		if (c->udav_xfer == NULL) {
969 			int error = usbd_create_xfer(sc->sc_pipe_tx, UDAV_BUFSZ,
970 			    USBD_FORCE_SHORT_XFER, 0, &c->udav_xfer);
971 			if (error)
972 				return error;
973 			c->udav_buf = usbd_get_buffer(c->udav_xfer);
974 		}
975 	}
976 
977 	return 0;
978 }
979 
980 Static void
udav_start(struct ifnet * ifp)981 udav_start(struct ifnet *ifp)
982 {
983 	struct udav_softc *sc = ifp->if_softc;
984 	struct mbuf *m_head = NULL;
985 
986 	DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev),
987 		 __func__, sc->sc_link));
988 
989 	if (sc->sc_dying)
990 		return;
991 
992 	if (!sc->sc_link)
993 		return;
994 
995 	if (ifp->if_flags & IFF_OACTIVE)
996 		return;
997 
998 	IFQ_POLL(&ifp->if_snd, m_head);
999 	if (m_head == NULL)
1000 		return;
1001 
1002 	if (udav_send(sc, m_head, 0)) {
1003 		ifp->if_flags |= IFF_OACTIVE;
1004 		return;
1005 	}
1006 
1007 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1008 
1009 	bpf_mtap(ifp, m_head);
1010 
1011 	ifp->if_flags |= IFF_OACTIVE;
1012 
1013 	/* Set a timeout in case the chip goes out to lunch. */
1014 	ifp->if_timer = 5;
1015 }
1016 
1017 Static int
udav_send(struct udav_softc * sc,struct mbuf * m,int idx)1018 udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
1019 {
1020 	int total_len;
1021 	struct udav_chain *c;
1022 	usbd_status err;
1023 
1024 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1025 
1026 	c = &sc->sc_cdata.udav_tx_chain[idx];
1027 
1028 	/* Copy the mbuf data into a contiguous buffer */
1029 	/*  first 2 bytes are packet length */
1030 	m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2);
1031 	c->udav_mbuf = m;
1032 	total_len = m->m_pkthdr.len;
1033 	if (total_len < UDAV_MIN_FRAME_LEN) {
1034 		memset(c->udav_buf + 2 + total_len, 0,
1035 		    UDAV_MIN_FRAME_LEN - total_len);
1036 		total_len = UDAV_MIN_FRAME_LEN;
1037 	}
1038 
1039 	/* Frame length is specified in the first 2bytes of the buffer */
1040 	c->udav_buf[0] = (uint8_t)total_len;
1041 	c->udav_buf[1] = (uint8_t)(total_len >> 8);
1042 	total_len += 2;
1043 
1044 	usbd_setup_xfer(c->udav_xfer, c, c->udav_buf, total_len,
1045 	    USBD_FORCE_SHORT_XFER, UDAV_TX_TIMEOUT, udav_txeof);
1046 
1047 	/* Transmit */
1048 	sc->sc_refcnt++;
1049 	err = usbd_transfer(c->udav_xfer);
1050 	if (--sc->sc_refcnt < 0)
1051 		usb_detach_wakeupold(sc->sc_dev);
1052 	if (err != USBD_IN_PROGRESS) {
1053 		printf("%s: udav_send error=%s\n", device_xname(sc->sc_dev),
1054 		       usbd_errstr(err));
1055 		/* Stop the interface */
1056 		usb_add_task(sc->sc_udev, &sc->sc_stop_task,
1057 		    USB_TASKQ_DRIVER);
1058 		return EIO;
1059 	}
1060 
1061 	DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev),
1062 		 __func__, total_len));
1063 
1064 	sc->sc_cdata.udav_tx_cnt++;
1065 
1066 	return 0;
1067 }
1068 
1069 Static void
udav_txeof(struct usbd_xfer * xfer,void * priv,usbd_status status)1070 udav_txeof(struct usbd_xfer *xfer, void *priv,
1071     usbd_status status)
1072 {
1073 	struct udav_chain *c = priv;
1074 	struct udav_softc *sc = c->udav_sc;
1075 	struct ifnet *ifp = GET_IFP(sc);
1076 	int s;
1077 
1078 	if (sc->sc_dying)
1079 		return;
1080 
1081 	s = splnet();
1082 
1083 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__));
1084 
1085 	ifp->if_timer = 0;
1086 	ifp->if_flags &= ~IFF_OACTIVE;
1087 
1088 	if (status != USBD_NORMAL_COMPLETION) {
1089 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1090 			splx(s);
1091 			return;
1092 		}
1093 		ifp->if_oerrors++;
1094 		printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev),
1095 		       usbd_errstr(status));
1096 		if (status == USBD_STALLED) {
1097 			sc->sc_refcnt++;
1098 			usbd_clear_endpoint_stall_async(sc->sc_pipe_tx);
1099 			if (--sc->sc_refcnt < 0)
1100 				usb_detach_wakeupold(sc->sc_dev);
1101 		}
1102 		splx(s);
1103 		return;
1104 	}
1105 
1106 	ifp->if_opackets++;
1107 
1108 	m_freem(c->udav_mbuf);
1109 	c->udav_mbuf = NULL;
1110 
1111 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1112 		udav_start(ifp);
1113 
1114 	splx(s);
1115 }
1116 
1117 Static void
udav_rxeof(struct usbd_xfer * xfer,void * priv,usbd_status status)1118 udav_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1119 {
1120 	struct udav_chain *c = priv;
1121 	struct udav_softc *sc = c->udav_sc;
1122 	struct ifnet *ifp = GET_IFP(sc);
1123 	struct mbuf *m;
1124 	uint32_t total_len;
1125 	uint8_t *pktstat;
1126 	int s;
1127 
1128 	DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__));
1129 
1130 	if (sc->sc_dying)
1131 		return;
1132 
1133 	if (status != USBD_NORMAL_COMPLETION) {
1134 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1135 			return;
1136 		sc->sc_rx_errs++;
1137 		if (usbd_ratecheck(&sc->sc_rx_notice)) {
1138 			printf("%s: %u usb errors on rx: %s\n",
1139 			       device_xname(sc->sc_dev), sc->sc_rx_errs,
1140 			       usbd_errstr(status));
1141 			sc->sc_rx_errs = 0;
1142 		}
1143 		if (status == USBD_STALLED) {
1144 			sc->sc_refcnt++;
1145 			usbd_clear_endpoint_stall_async(sc->sc_pipe_rx);
1146 			if (--sc->sc_refcnt < 0)
1147 				usb_detach_wakeupold(sc->sc_dev);
1148 		}
1149 		goto done;
1150 	}
1151 
1152 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1153 
1154 	/* copy data to mbuf */
1155 	m = c->udav_mbuf;
1156 	memcpy(mtod(m, char *), c->udav_buf, total_len);
1157 
1158 	/* first byte in received data */
1159 	pktstat = mtod(m, uint8_t *);
1160 	m_adj(m, sizeof(uint8_t));
1161 	DPRINTF(("%s: RX Status: 0x%02x\n", device_xname(sc->sc_dev),
1162 				*pktstat));
1163 
1164 	total_len = UGETW(mtod(m, uint8_t *));
1165 	m_adj(m, sizeof(uint16_t));
1166 
1167 	if (*pktstat & UDAV_RSR_LCS) {
1168 		ifp->if_collisions++;
1169 		goto done;
1170 	}
1171 
1172 	if (total_len < sizeof(struct ether_header) ||
1173 	    *pktstat & UDAV_RSR_ERR) {
1174 		ifp->if_ierrors++;
1175 		goto done;
1176 	}
1177 
1178 	ifp->if_ipackets++;
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 	bpf_mtap(ifp, m);
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
udav_ioctl(struct ifnet * ifp,u_long cmd,void * data)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
udav_watchdog(struct ifnet * ifp)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
udav_stop_task(struct udav_softc * sc)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
udav_stop(struct ifnet * ifp,int disable)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
udav_ifmedia_change(struct ifnet * ifp)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
udav_ifmedia_status(struct ifnet * ifp,struct ifmediareq * ifmr)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
udav_tick(void * xsc)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
udav_tick_task(void * xsc)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
udav_lock_mii(struct udav_softc * sc)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
udav_unlock_mii(struct udav_softc * sc)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
udav_miibus_readreg(device_t dev,int phy,int reg)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
udav_miibus_writereg(device_t dev,int phy,int reg,int data)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
udav_miibus_statchg(struct ifnet * ifp)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