if_aue.c revision 1.21 1 /* $NetBSD: if_aue.c,v 1.21 2000/02/02 20:06:55 augustss Exp $ */
2 /*
3 * Copyright (c) 1997, 1998, 1999, 2000
4 * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
34 */
35
36 /*
37 * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available
38 * from http://www.admtek.com.tw.
39 *
40 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45 /*
46 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
47 * support: the control endpoint for reading/writing registers, burst
48 * read endpoint for packet reception, burst write for packet transmission
49 * and one for "interrupts." The chip uses the same RX filter scheme
50 * as the other ADMtek ethernet parts: one perfect filter entry for the
51 * the station address and a 64-bit multicast hash table. The chip supports
52 * both MII and HomePNA attachments.
53 *
54 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
55 * you're never really going to get 100Mbps speeds from this device. I
56 * think the idea is to allow the device to connect to 10 or 100Mbps
57 * networks, not necessarily to provide 100Mbps performance. Also, since
58 * the controller uses an external PHY chip, it's possible that board
59 * designers might simply choose a 10Mbps PHY.
60 *
61 * Registers are accessed using usbd_do_request(). Packet transfers are
62 * done using usbd_transfer() and friends.
63 */
64
65 /*
66 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
67 */
68
69 /*
70 * TODO:
71 * better error messages from rxstat
72 * split out if_auevar.h
73 * add thread to avoid register reads from interrupt context
74 * more error checks
75 * investigate short rx problem
76 * proper cleanup on errors
77 */
78
79 #if defined(__NetBSD__) || defined(__OpenBSD__)
80 #include "opt_inet.h"
81 #include "opt_ns.h"
82 #include "bpfilter.h"
83 #include "rnd.h"
84 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/sockio.h>
89 #include <sys/mbuf.h>
90 #include <sys/malloc.h>
91 #include <sys/kernel.h>
92 #include <sys/socket.h>
93
94 #if defined(__FreeBSD__)
95
96 #include <net/ethernet.h>
97 #include <machine/clock.h> /* for DELAY */
98 #include <sys/bus.h>
99 /* "controller miibus0" required. See GENERIC if you get errors here. */
100 #include "miibus_if.h"
101
102 #elif defined(__NetBSD__) || defined(__OpenBSD__)
103
104 #include <sys/device.h>
105 #if NRND > 0
106 #include <sys/rnd.h>
107 #endif
108
109 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
110
111 #include <net/if.h>
112 #include <net/if_arp.h>
113 #include <net/if_dl.h>
114 #include <net/if_media.h>
115
116 #if defined(__NetBSD__) || defined(__OpenBSD__)
117 #include <net/if_ether.h>
118
119 #define bpf_mtap(ifp, m) bpf_tap((ifp)->if_bpf, mtod((m), caddr_t), (m)->m_len)
120
121 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
122
123 #if defined(__FreeBSD__) || NBPFILTER > 0
124 #include <net/bpf.h>
125 #endif
126
127 #if defined(__NetBSD__) || defined(__OpenBSD__)
128 #ifdef INET
129 #include <netinet/in.h>
130 #include <netinet/if_inarp.h>
131 #endif
132
133 #ifdef NS
134 #include <netns/ns.h>
135 #include <netns/ns_if.h>
136 #endif
137 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
138
139 #include <dev/mii/mii.h>
140 #include <dev/mii/miivar.h>
141
142 #include <dev/usb/usb.h>
143 #include <dev/usb/usbdi.h>
144 #include <dev/usb/usbdi_util.h>
145 #include <dev/usb/usbdevs.h>
146
147 #ifdef __FreeBSD__
148 #include <dev/usb/usb_ethersubr.h>
149 #endif
150
151 #include <dev/usb/if_auereg.h>
152
153 #ifdef AUE_DEBUG
154 #define DPRINTF(x) if (auedebug) logprintf x
155 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x
156 int auedebug = 0;
157 #else
158 #define DPRINTF(x)
159 #define DPRINTFN(n,x)
160 #endif
161
162 /*
163 * Various supported device vendors/products.
164 */
165 static struct aue_type aue_devs[] = {
166 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
167 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
168 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
169 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
170 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
171 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
172 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
173 { 0, 0 }
174 };
175
176 USB_DECLARE_DRIVER(aue);
177
178 static int aue_tx_list_init __P((struct aue_softc *));
179 static int aue_rx_list_init __P((struct aue_softc *));
180 static int aue_newbuf __P((struct aue_softc *, struct aue_chain *,
181 struct mbuf *));
182 static int aue_send __P((struct aue_softc *, struct mbuf *, int));
183 static void aue_intr __P((usbd_xfer_handle,
184 usbd_private_handle, usbd_status));
185 static void aue_rxeof __P((usbd_xfer_handle,
186 usbd_private_handle, usbd_status));
187 static void aue_txeof __P((usbd_xfer_handle,
188 usbd_private_handle, usbd_status));
189 static void aue_tick __P((void *));
190 static void aue_start __P((struct ifnet *));
191 static int aue_ioctl __P((struct ifnet *, u_long, caddr_t));
192 static void aue_init __P((void *));
193 static void aue_stop __P((struct aue_softc *));
194 static void aue_watchdog __P((struct ifnet *));
195 #ifdef __FreeBSD__
196 static void aue_shutdown __P((device_ptr_t));
197 #endif
198 static int aue_openpipes __P((struct aue_softc *));
199 static int aue_ifmedia_upd __P((struct ifnet *));
200 static void aue_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
201
202 static int aue_eeprom_getword __P((struct aue_softc *, int));
203 static void aue_read_mac __P((struct aue_softc *, u_char *));
204 static int aue_miibus_readreg __P((device_ptr_t, int, int));
205 #if defined(__FreeBSD__)
206 static int aue_miibus_writereg __P((device_ptr_t, int, int, int));
207 #elif defined(__NetBSD__) || defined(__OpenBSD__)
208 static void aue_miibus_writereg __P((device_ptr_t, int, int, int));
209 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
210 static void aue_miibus_statchg __P((device_ptr_t));
211
212 static void aue_setmulti __P((struct aue_softc *));
213 static u_int32_t aue_crc __P((caddr_t));
214 static void aue_reset __P((struct aue_softc *));
215
216 static int csr_read_1 __P((struct aue_softc *, int));
217 static int csr_write_1 __P((struct aue_softc *, int, int));
218 static int csr_read_2 __P((struct aue_softc *, int));
219 static int csr_write_2 __P((struct aue_softc *, int, int));
220
221 #if defined(__FreeBSD__)
222 #if !defined(lint)
223 static const char rcsid[] =
224 "$FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $";
225 #endif
226
227 static void aue_rxstart __P((struct ifnet *));
228
229 static struct usb_qdat aue_qdat;
230
231 static device_method_t aue_methods[] = {
232 /* Device interface */
233 DEVMETHOD(device_probe, aue_match),
234 DEVMETHOD(device_attach, aue_attach),
235 DEVMETHOD(device_detach, aue_detach),
236 DEVMETHOD(device_shutdown, aue_shutdown),
237
238 /* bus interface */
239 DEVMETHOD(bus_print_child, bus_generic_print_child),
240 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
241
242 /* MII interface */
243 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
244 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
245 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
246
247 { 0, 0 }
248 };
249
250 static driver_t aue_driver = {
251 "aue",
252 aue_methods,
253 sizeof(struct aue_softc)
254 };
255
256 static devclass_t aue_devclass;
257
258 DRIVER_MODULE(if_aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
259 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
260
261 #endif /* __FreeBSD__ */
262
263 #define AUE_DO_REQUEST(dev, req, data) usbd_do_request_flags(dev, req, data, USBD_NO_TSLEEP, NULL)
264
265 #define AUE_SETBIT(sc, reg, x) \
266 csr_write_1(sc, reg, csr_read_1(sc, reg) | (x))
267
268 #define AUE_CLRBIT(sc, reg, x) \
269 csr_write_1(sc, reg, csr_read_1(sc, reg) & ~(x))
270
271 static int
272 csr_read_1(sc, reg)
273 struct aue_softc *sc;
274 int reg;
275 {
276 usb_device_request_t req;
277 usbd_status err;
278 uByte val = 0;
279 int s;
280
281 req.bmRequestType = UT_READ_VENDOR_DEVICE;
282 req.bRequest = AUE_UR_READREG;
283 USETW(req.wValue, 0);
284 USETW(req.wIndex, reg);
285 USETW(req.wLength, 1);
286
287 s = splusb();
288 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
289 splx(s);
290
291 if (err)
292 return (0);
293
294 return (val);
295 }
296
297 static int
298 csr_read_2(sc, reg)
299 struct aue_softc *sc;
300 int reg;
301 {
302 usb_device_request_t req;
303 usbd_status err;
304 uWord val;
305 int s;
306
307 req.bmRequestType = UT_READ_VENDOR_DEVICE;
308 req.bRequest = AUE_UR_READREG;
309 USETW(req.wValue, 0);
310 USETW(req.wIndex, reg);
311 USETW(req.wLength, 2);
312
313 s = splusb();
314 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
315 splx(s);
316
317 if (err)
318 return (0);
319
320 return (UGETW(val));
321 }
322
323 static int
324 csr_write_1(sc, reg, aval)
325 struct aue_softc *sc;
326 int reg, aval;
327 {
328 usb_device_request_t req;
329 usbd_status err;
330 int s;
331 uByte val;
332
333 val = aval;
334 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
335 req.bRequest = AUE_UR_WRITEREG;
336 USETW(req.wValue, val);
337 USETW(req.wIndex, reg);
338 USETW(req.wLength, 1);
339
340 s = splusb();
341 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
342 splx(s);
343
344 if (err)
345 return (-1);
346
347 return (0);
348 }
349
350 static int
351 csr_write_2(sc, reg, aval)
352 struct aue_softc *sc;
353 int reg, aval;
354 {
355 usb_device_request_t req;
356 usbd_status err;
357 int s;
358 uWord val;
359
360 USETW(val, aval);
361 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
362 req.bRequest = AUE_UR_WRITEREG;
363 USETW(req.wValue, aval);
364 USETW(req.wIndex, reg);
365 USETW(req.wLength, 2);
366
367 s = splusb();
368 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
369 splx(s);
370
371 if (err)
372 return (-1);
373
374 return (0);
375 }
376
377 /*
378 * Read a word of data stored in the EEPROM at address 'addr.'
379 */
380 static int
381 aue_eeprom_getword(sc, addr)
382 struct aue_softc *sc;
383 int addr;
384 {
385 int i;
386
387 csr_write_1(sc, AUE_EE_REG, addr);
388 csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
389
390 for (i = 0; i < AUE_TIMEOUT; i++) {
391 if (csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
392 break;
393 }
394
395 if (i == AUE_TIMEOUT) {
396 printf("%s: EEPROM read timed out\n",
397 USBDEVNAME(sc->aue_dev));
398 }
399
400 return (csr_read_2(sc, AUE_EE_DATA));
401 }
402
403 /*
404 * Read the MAC from the EEPROM. It's at offset 0.
405 */
406 static void
407 aue_read_mac(sc, dest)
408 struct aue_softc *sc;
409 u_char *dest;
410 {
411 int i;
412 int off = 0;
413 int word;
414
415 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
416
417 for (i = 0; i < 3; i++) {
418 word = aue_eeprom_getword(sc, off + i);
419 dest[2 * i] = (u_char)word;
420 dest[2 * i + 1] = (u_char)(word >> 8);
421 }
422 }
423
424 static int
425 aue_miibus_readreg(dev, phy, reg)
426 device_ptr_t dev;
427 int phy, reg;
428 {
429 struct aue_softc *sc = USBGETSOFTC(dev);
430 int i;
431 u_int16_t val;
432
433 /*
434 * The Am79C901 HomePNA PHY actually contains
435 * two transceivers: a 1Mbps HomePNA PHY and a
436 * 10Mbps full/half duplex ethernet PHY with
437 * NWAY autoneg. However in the ADMtek adapter,
438 * only the 1Mbps PHY is actually connected to
439 * anything, so we ignore the 10Mbps one. It
440 * happens to be configured for MII address 3,
441 * so we filter that out.
442 */
443 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
444 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
445 if (phy != 1)
446 return (0);
447 }
448
449 csr_write_1(sc, AUE_PHY_ADDR, phy);
450 csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
451
452 for (i = 0; i < AUE_TIMEOUT; i++) {
453 if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
454 break;
455 }
456
457 if (i == AUE_TIMEOUT) {
458 printf("%s: MII read timed out\n",
459 USBDEVNAME(sc->aue_dev));
460 }
461
462 val = csr_read_2(sc, AUE_PHY_DATA);
463
464 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
465 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val));
466
467 return (val);
468 }
469
470 #if defined(__FreeBSD__)
471 static int
472 #elif defined(__NetBSD__) || defined(__OpenBSD__)
473 static void
474 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
475 aue_miibus_writereg(dev, phy, reg, data)
476 device_ptr_t dev;
477 int phy, reg, data;
478 {
479 struct aue_softc *sc = USBGETSOFTC(dev);
480 int i;
481
482 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
483 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
484 if (phy == 3)
485 #if defined(__FreeBSD__)
486 return (0);
487 #elif defined(__NetBSD__) || defined(__OpenBSD__)
488 return;
489 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
490 }
491
492 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
493 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data));
494
495 csr_write_2(sc, AUE_PHY_DATA, data);
496 csr_write_1(sc, AUE_PHY_ADDR, phy);
497 csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
498
499 for (i = 0; i < AUE_TIMEOUT; i++) {
500 if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
501 break;
502 }
503
504 if (i == AUE_TIMEOUT) {
505 printf("%s: MII read timed out\n",
506 USBDEVNAME(sc->aue_dev));
507 }
508
509 #if defined(__FreeBSD__)
510 return (0);
511 #endif
512 }
513
514 static void
515 aue_miibus_statchg(dev)
516 device_ptr_t dev;
517 {
518 struct aue_softc *sc = USBGETSOFTC(dev);
519 struct mii_data *mii = GET_MII(sc);
520 struct ifnet *ifp = GET_IFP(sc);
521
522 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
523
524 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
525
526 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
527 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
528 ifp->if_baudrate = 100000000;
529 } else {
530 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
531 ifp->if_baudrate = 10000000;
532 }
533
534 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
535 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
536 else
537 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
538
539 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
540
541 /*
542 * Set the LED modes on the LinkSys adapter.
543 * This turns on the 'dual link LED' bin in the auxmode
544 * register of the Broadcom PHY.
545 */
546 if ((sc->aue_vendor == USB_VENDOR_LINKSYS &&
547 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) ||
548 (sc->aue_vendor == USB_VENDOR_DLINK &&
549 sc->aue_product == USB_PRODUCT_DLINK_DSB650TX)) {
550 u_int16_t auxmode;
551 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
552 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
553 }
554 }
555
556 #define AUE_POLY 0xEDB88320
557 #define AUE_BITS 6
558
559 static u_int32_t
560 aue_crc(addr)
561 caddr_t addr;
562 {
563 u_int32_t idx, bit, data, crc;
564
565 /* Compute CRC for the address value. */
566 crc = 0xFFFFFFFF; /* initial value */
567
568 for (idx = 0; idx < 6; idx++) {
569 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
570 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
571 }
572
573 return (crc & ((1 << AUE_BITS) - 1));
574 }
575
576 static void
577 aue_setmulti(sc)
578 struct aue_softc *sc;
579 {
580 struct ifnet *ifp;
581 #if defined(__FreeBSD__)
582 struct ifmultiaddr *ifma;
583 #elif defined(__NetBSD__) || defined(__OpenBSD__)
584 struct ether_multi *enm;
585 struct ether_multistep step;
586 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
587 u_int32_t h = 0, i;
588
589 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
590
591 ifp = GET_IFP(sc);
592
593 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
594 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
595 return;
596 }
597
598 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
599
600 /* first, zot all the existing hash bits */
601 for (i = 0; i < 8; i++)
602 csr_write_1(sc, AUE_MAR0 + i, 0);
603
604 /* now program new ones */
605 #if defined(__FreeBSD__)
606 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
607 ifma = ifma->ifma_link.le_next) {
608 if (ifma->ifma_addr->sa_family != AF_LINK)
609 continue;
610 h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
611 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
612 }
613 #elif defined(__NetBSD__) || defined(__OpenBSD__)
614 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
615 while (enm != NULL) {
616 #if 1
617 if (memcmp(enm->enm_addrlo,
618 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
619 ifp->if_flags |= IFF_ALLMULTI;
620 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
621 return;
622 }
623 #endif
624 h = aue_crc(enm->enm_addrlo);
625 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
626 ETHER_NEXT_MULTI(step, enm);
627 }
628 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
629 }
630
631 static void
632 aue_reset(sc)
633 struct aue_softc *sc;
634 {
635 int i;
636
637 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
638
639 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
640
641 for (i = 0; i < AUE_TIMEOUT; i++) {
642 if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
643 break;
644 }
645
646 if (i == AUE_TIMEOUT)
647 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
648
649 /*
650 * The PHY(s) attached to the Pegasus chip may be held
651 * in reset until we flip on the GPIO outputs. Make sure
652 * to set the GPIO pins high so that the PHY(s) will
653 * be enabled.
654 *
655 * Note: We force all of the GPIO pins low first, *then*
656 * enable the ones we want.
657 */
658 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
659 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
660
661 /* Grrr. LinkSys has to be different from everyone else. */
662 if ((sc->aue_vendor == USB_VENDOR_LINKSYS &&
663 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) ||
664 (sc->aue_vendor == USB_VENDOR_DLINK &&
665 sc->aue_product == USB_PRODUCT_DLINK_DSB650TX)) {
666 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
667 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
668 AUE_GPIO_OUT0);
669 }
670
671 /* Wait a little while for the chip to get its brains in order. */
672 DELAY(10000); /* XXX */
673 }
674
675 /*
676 * Probe for a Pegasus chip.
677 */
678 USB_MATCH(aue)
679 {
680 USB_MATCH_START(aue, uaa);
681 struct aue_type *t;
682
683 if (uaa->iface != NULL)
684 return (UMATCH_NONE);
685
686 for (t = aue_devs; t->aue_vid != 0; t++)
687 if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
688 return (UMATCH_VENDOR_PRODUCT);
689
690 return (UMATCH_NONE);
691 }
692
693 /*
694 * Attach the interface. Allocate softc structures, do ifmedia
695 * setup and ethernet/BPF attach.
696 */
697 USB_ATTACH(aue)
698 {
699 USB_ATTACH_START(aue, sc, uaa);
700 char devinfo[1024];
701 int s;
702 u_char eaddr[ETHER_ADDR_LEN];
703 struct ifnet *ifp;
704 struct mii_data *mii;
705 usbd_device_handle dev = uaa->device;
706 usbd_interface_handle iface;
707 usbd_status err;
708 usb_interface_descriptor_t *id;
709 usb_endpoint_descriptor_t *ed;
710 int i;
711
712 #ifdef __FreeBSD__
713 bzero(sc, sizeof(struct aue_softc));
714 #endif
715
716 DPRINTFN(5,(" : aue_attach: sc=%p", sc));
717
718 usbd_devinfo(dev, 0, devinfo);
719 USB_ATTACH_SETUP;
720 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
721
722 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
723 if (err) {
724 printf("%s: setting config no failed\n",
725 USBDEVNAME(sc->aue_dev));
726 USB_ATTACH_ERROR_RETURN;
727 }
728
729 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
730 if (err) {
731 printf("%s: getting interface handle failed\n",
732 USBDEVNAME(sc->aue_dev));
733 USB_ATTACH_ERROR_RETURN;
734 }
735
736 sc->aue_udev = dev;
737 sc->aue_iface = iface;
738 sc->aue_product = uaa->product;
739 sc->aue_vendor = uaa->vendor;
740
741 id = usbd_get_interface_descriptor(iface);
742
743 /* Find endpoints. */
744 for (i = 0; i < id->bNumEndpoints; i++) {
745 ed = usbd_interface2endpoint_descriptor(iface, i);
746 if (!ed) {
747 printf("%s: couldn't get endpoint descriptor %d\n",
748 USBDEVNAME(sc->aue_dev), i);
749 USB_ATTACH_ERROR_RETURN;
750 }
751 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
752 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
753 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
754 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
755 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
756 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
757 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
758 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
759 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
760 }
761 }
762
763 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
764 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
765 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
766 USB_ATTACH_ERROR_RETURN;
767 }
768
769
770 s = splimp();
771
772 /* Reset the adapter. */
773 aue_reset(sc);
774
775 /*
776 * Get station address from the EEPROM.
777 */
778 aue_read_mac(sc, eaddr);
779
780 /*
781 * A Pegasus chip was detected. Inform the world.
782 */
783 #if defined(__FreeBSD__)
784 printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
785 eaddr, ":");
786
787 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
788
789 ifp = &sc->arpcom.ac_if;
790 ifp->if_softc = sc;
791 ifp->if_unit = sc->aue_unit;
792 ifp->if_name = "aue";
793 ifp->if_mtu = ETHERMTU;
794 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
795 ifp->if_ioctl = aue_ioctl;
796 ifp->if_output = ether_output;
797 ifp->if_start = aue_start;
798 ifp->if_watchdog = aue_watchdog;
799 ifp->if_init = aue_init;
800 ifp->if_baudrate = 10000000;
801 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
802
803 /*
804 * Do MII setup.
805 * NOTE: Doing this causes child devices to be attached to us,
806 * which we would normally disconnect at in the detach routine
807 * using device_delete_child(). However the USB code is set up
808 * such that when this driver is removed, all childred devices
809 * are removed as well. In effect, the USB code ends up detaching
810 * all of our children for us, so we don't have to do is ourselves
811 * in aue_detach(). It's important to point this out since if
812 * we *do* try to detach the child devices ourselves, we will
813 * end up getting the children deleted twice, which will crash
814 * the system.
815 */
816 if (mii_phy_probe(self, &sc->aue_miibus,
817 aue_ifmedia_upd, aue_ifmedia_sts)) {
818 printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
819 splx(s);
820 USB_ATTACH_ERROR_RETURN;
821 }
822
823 aue_qdat.ifp = ifp;
824 aue_qdat.if_rxstart = aue_rxstart;
825
826 /*
827 * Call MI attach routines.
828 */
829 if_attach(ifp);
830 ether_ifattach(ifp);
831 callout_handle_init(&sc->aue_stat_ch);
832 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
833
834 usb_register_netisr();
835
836 #elif defined(__NetBSD__) || defined(__OpenBSD__)
837
838 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
839 ether_sprintf(eaddr));
840
841 /* Initialize interface info.*/
842 ifp = &sc->aue_ec.ec_if;
843 ifp->if_softc = sc;
844 ifp->if_mtu = ETHERMTU;
845 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
846 ifp->if_ioctl = aue_ioctl;
847 ifp->if_start = aue_start;
848 ifp->if_watchdog = aue_watchdog;
849 ifp->if_baudrate = 10000000;
850 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
851
852 /* Initialize MII/media info. */
853 mii = &sc->aue_mii;
854 mii->mii_ifp = ifp;
855 mii->mii_readreg = aue_miibus_readreg;
856 mii->mii_writereg = aue_miibus_writereg;
857 mii->mii_statchg = aue_miibus_statchg;
858 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
859 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
860 if (LIST_FIRST(&mii->mii_phys) == NULL) {
861 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
862 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
863 } else
864 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
865
866 /* Attach the interface. */
867 if_attach(ifp);
868 ether_ifattach(ifp, eaddr);
869
870 #if NBPFILTER > 0
871 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
872 sizeof(struct ether_header));
873 #endif
874 #if NRND > 0
875 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
876 RND_TYPE_NET, 0);
877 #endif
878
879 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
880
881 splx(s);
882
883 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
884 USBDEV(sc->aue_dev));
885
886 USB_ATTACH_SUCCESS_RETURN;
887 }
888
889 USB_DETACH(aue)
890 {
891 USB_DETACH_START(aue, sc);
892 struct ifnet *ifp = GET_IFP(sc);
893 int s;
894
895 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
896
897 s = splusb();
898
899 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
900
901 if (ifp->if_flags & IFF_RUNNING)
902 aue_stop(sc);
903
904 #if defined(__NetBSD__)
905 #if NRND > 0
906 rnd_detach_source(&sc->rnd_source);
907 #endif
908 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
909 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
910 #if NBPFILTER > 0
911 bpfdetach(ifp);
912 #endif
913 ether_ifdetach(ifp);
914 #endif /* __NetBSD__ */
915
916 if_detach(ifp);
917
918 #ifdef DIAGNOSTIC
919 if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
920 sc->aue_ep[AUE_ENDPT_RX] != NULL ||
921 sc->aue_ep[AUE_ENDPT_INTR] != NULL)
922 printf("%s: detach has active endpoints\n",
923 USBDEVNAME(sc->aue_dev));
924 #endif
925
926 splx(s);
927
928 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
929 USBDEV(sc->aue_dev));
930
931 return (0);
932 }
933
934 #if defined(__NetBSD__) || defined(__OpenBSD__)
935 int
936 aue_activate(self, act)
937 device_ptr_t self;
938 enum devact act;
939 {
940 struct aue_softc *sc = (struct aue_softc *)self;
941
942 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
943
944 switch (act) {
945 case DVACT_ACTIVATE:
946 return (EOPNOTSUPP);
947 break;
948
949 case DVACT_DEACTIVATE:
950 if_deactivate(&sc->aue_ec.ec_if);
951 sc->aue_dying = 1;
952 break;
953 }
954 return (0);
955 }
956 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
957
958 /*
959 * Initialize an RX descriptor and attach an MBUF cluster.
960 */
961 static int
962 aue_newbuf(sc, c, m)
963 struct aue_softc *sc;
964 struct aue_chain *c;
965 struct mbuf *m;
966 {
967 struct mbuf *m_new = NULL;
968
969 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
970
971 if (m == NULL) {
972 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
973 if (m_new == NULL) {
974 printf("%s: no memory for rx list "
975 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
976 return (ENOBUFS);
977 }
978
979 MCLGET(m_new, M_DONTWAIT);
980 if (!(m_new->m_flags & M_EXT)) {
981 printf("%s: no memory for rx list "
982 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
983 m_freem(m_new);
984 return (ENOBUFS);
985 }
986 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
987 } else {
988 m_new = m;
989 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
990 m_new->m_data = m_new->m_ext.ext_buf;
991 }
992
993 m_adj(m_new, ETHER_ALIGN);
994 c->aue_mbuf = m_new;
995
996 return (0);
997 }
998
999 static int
1000 aue_rx_list_init(sc)
1001 struct aue_softc *sc;
1002 {
1003 struct aue_cdata *cd;
1004 struct aue_chain *c;
1005 int i;
1006
1007 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1008
1009 cd = &sc->aue_cdata;
1010 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1011 c = &cd->aue_rx_chain[i];
1012 c->aue_sc = sc;
1013 c->aue_idx = i;
1014 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1015 return (ENOBUFS);
1016 if (c->aue_xfer == NULL) {
1017 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1018 if (c->aue_xfer == NULL)
1019 return (ENOBUFS);
1020 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1021 if (c->aue_buf == NULL)
1022 return (ENOBUFS); /* XXX free xfer */
1023 }
1024 }
1025
1026 return (0);
1027 }
1028
1029 static int
1030 aue_tx_list_init(sc)
1031 struct aue_softc *sc;
1032 {
1033 struct aue_cdata *cd;
1034 struct aue_chain *c;
1035 int i;
1036
1037 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1038
1039 cd = &sc->aue_cdata;
1040 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1041 c = &cd->aue_tx_chain[i];
1042 c->aue_sc = sc;
1043 c->aue_idx = i;
1044 c->aue_mbuf = NULL;
1045 if (c->aue_xfer == NULL) {
1046 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1047 if (c->aue_xfer == NULL)
1048 return (ENOBUFS);
1049 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1050 if (c->aue_buf == NULL)
1051 return (ENOBUFS);
1052 }
1053 }
1054
1055 return (0);
1056 }
1057
1058 static void
1059 aue_intr(xfer, priv, status)
1060 usbd_xfer_handle xfer;
1061 usbd_private_handle priv;
1062 usbd_status status;
1063 {
1064 struct aue_softc *sc = priv;
1065 struct ifnet *ifp = GET_IFP(sc);
1066 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1067
1068 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1069
1070 if (sc->aue_dying)
1071 return;
1072
1073 if (!(ifp->if_flags & IFF_RUNNING))
1074 return;
1075
1076 if (status != USBD_NORMAL_COMPLETION) {
1077 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1078 return;
1079 }
1080 printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
1081 usbd_errstr(status));
1082 if (status == USBD_STALLED)
1083 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1084 return;
1085 }
1086
1087 if (p->aue_txstat0)
1088 ifp->if_oerrors++;
1089
1090 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1091 ifp->if_collisions++;
1092 }
1093
1094 #if defined(__FreeBSD__)
1095 static void
1096 aue_rxstart(ifp)
1097 struct ifnet *ifp;
1098 {
1099 struct aue_softc *sc;
1100 struct aue_chain *c;
1101
1102 sc = ifp->if_softc;
1103 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
1104
1105 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1106 ifp->if_ierrors++;
1107 return;
1108 }
1109
1110 /* Setup new transfer. */
1111 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1112 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
1113 USBD_NO_TIMEOUT, aue_rxeof);
1114 usbd_transfer(c->aue_xfer);
1115 }
1116 #endif
1117
1118 /*
1119 * A frame has been uploaded: pass the resulting mbuf chain up to
1120 * the higher level protocols.
1121 *
1122 * Grrr. Receiving transfers larger than about 1152 bytes sometimes
1123 * doesn't work. We get an incomplete frame. In order to avoid
1124 * this, we queue up RX transfers that are shorter than a full sized
1125 * frame. If the received frame is larger than our transfer size,
1126 * we snag the rest of the data using a second transfer. Does this
1127 * hurt performance? Yes. But after fighting with this stupid thing
1128 * for three days, I'm willing to settle. I'd rather have reliable
1129 * receive performance that fast but spotty performance.
1130 */
1131 static void
1132 aue_rxeof(xfer, priv, status)
1133 usbd_xfer_handle xfer;
1134 usbd_private_handle priv;
1135 usbd_status status;
1136 {
1137 struct aue_chain *c = priv;
1138 struct aue_softc *sc = c->aue_sc;
1139 struct ifnet *ifp = GET_IFP(sc);
1140 struct mbuf *m;
1141 u_int32_t total_len;
1142 struct aue_rxpkt r;
1143 #if defined(__NetBSD__) || defined(__OpenBSD__)
1144 int s;
1145 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1146
1147 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1148
1149 if (sc->aue_dying)
1150 return;
1151
1152 if (!(ifp->if_flags & IFF_RUNNING))
1153 return;
1154
1155 if (status != USBD_NORMAL_COMPLETION) {
1156 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1157 return;
1158 printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
1159 usbd_errstr(status));
1160 if (status == USBD_STALLED)
1161 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1162 goto done;
1163 }
1164
1165 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1166
1167 memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
1168
1169 if (total_len <= 4 + ETHER_CRC_LEN) {
1170 ifp->if_ierrors++;
1171 goto done;
1172 }
1173
1174 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1175
1176 /* Turn off all the non-error bits in the rx status word. */
1177 r.aue_rxstat &= AUE_RXSTAT_MASK;
1178 if (r.aue_rxstat) {
1179 ifp->if_ierrors++;
1180 goto done;
1181 }
1182
1183 /* No errors; receive the packet. */
1184 m = c->aue_mbuf;
1185 total_len -= ETHER_CRC_LEN + 4;
1186 m->m_pkthdr.len = m->m_len = total_len;
1187 ifp->if_ipackets++;
1188
1189 #if defined(__FreeBSD__)
1190 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
1191 /* Put the packet on the special USB input queue. */
1192 usb_ether_input(m);
1193
1194 return;
1195
1196 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1197 m->m_pkthdr.rcvif = ifp;
1198
1199 s = splimp();
1200
1201 /* XXX ugly */
1202 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1203 ifp->if_ierrors++;
1204 goto done1;
1205 }
1206
1207 /*
1208 * Handle BPF listeners. Let the BPF user see the packet, but
1209 * don't pass it up to the ether_input() layer unless it's
1210 * a broadcast packet, multicast packet, matches our ethernet
1211 * address or the interface is in promiscuous mode.
1212 */
1213 if (ifp->if_bpf) {
1214 struct ether_header *eh = mtod(m, struct ether_header *);
1215 bpf_mtap(ifp, m);
1216 if ((ifp->if_flags & IFF_PROMISC) &&
1217 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1218 ETHER_ADDR_LEN) &&
1219 !(eh->ether_dhost[0] & 1)) {
1220 m_freem(m);
1221 goto done1;
1222 }
1223 }
1224
1225 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1226 __FUNCTION__, m->m_len));
1227 (*ifp->if_input)(ifp, m);
1228 done1:
1229 splx(s);
1230 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1231
1232 done:
1233
1234 /* Setup new transfer. */
1235 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1236 c, c->aue_buf, AUE_BUFSZ,
1237 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1238 USBD_NO_TIMEOUT, aue_rxeof);
1239 usbd_transfer(xfer);
1240
1241 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1242 __FUNCTION__));
1243 }
1244
1245 /*
1246 * A frame was downloaded to the chip. It's safe for us to clean up
1247 * the list buffers.
1248 */
1249
1250 static void
1251 aue_txeof(xfer, priv, status)
1252 usbd_xfer_handle xfer;
1253 usbd_private_handle priv;
1254 usbd_status status;
1255 {
1256 struct aue_chain *c = priv;
1257 struct aue_softc *sc = c->aue_sc;
1258 struct ifnet *ifp = GET_IFP(sc);
1259 int s;
1260
1261 if (sc->aue_dying)
1262 return;
1263
1264 s = splimp();
1265
1266 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1267 __FUNCTION__, status));
1268
1269 ifp->if_timer = 0;
1270 ifp->if_flags &= ~IFF_OACTIVE;
1271
1272 if (status != USBD_NORMAL_COMPLETION) {
1273 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1274 splx(s);
1275 return;
1276 }
1277 ifp->if_oerrors++;
1278 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1279 usbd_errstr(status));
1280 if (status == USBD_STALLED)
1281 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1282 splx(s);
1283 return;
1284 }
1285
1286 ifp->if_opackets++;
1287
1288 #if defined(__FreeBSD__)
1289 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1290 usb_tx_done(c->aue_mbuf);
1291 c->aue_mbuf = NULL;
1292 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1293 m_freem(c->aue_mbuf);
1294 c->aue_mbuf = NULL;
1295
1296 if (ifp->if_snd.ifq_head != NULL)
1297 aue_start(ifp);
1298 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1299
1300 splx(s);
1301 }
1302
1303 static void
1304 aue_tick(xsc)
1305 void *xsc;
1306 {
1307 struct aue_softc *sc = xsc;
1308 struct ifnet *ifp;
1309 struct mii_data *mii;
1310 int s;
1311
1312 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1313
1314 if (sc == NULL)
1315 return;
1316
1317 if (sc->aue_dying)
1318 return;
1319
1320 ifp = GET_IFP(sc);
1321 mii = GET_MII(sc);
1322 if (mii == NULL)
1323 return;
1324
1325 s = splimp();
1326
1327 mii_tick(mii);
1328 if (!sc->aue_link) {
1329 mii_pollstat(mii);
1330 if (mii->mii_media_status & IFM_ACTIVE &&
1331 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1332 DPRINTFN(2,("%s: %s: got link\n",
1333 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1334 sc->aue_link++;
1335 if (ifp->if_snd.ifq_head != NULL)
1336 aue_start(ifp);
1337 }
1338 }
1339
1340 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1341
1342 splx(s);
1343 }
1344
1345 static int
1346 aue_send(sc, m, idx)
1347 struct aue_softc *sc;
1348 struct mbuf *m;
1349 int idx;
1350 {
1351 int total_len;
1352 struct aue_chain *c;
1353 usbd_status err;
1354
1355 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1356
1357 c = &sc->aue_cdata.aue_tx_chain[idx];
1358
1359 /*
1360 * Copy the mbuf data into a contiguous buffer, leaving two
1361 * bytes at the beginning to hold the frame length.
1362 */
1363 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1364 c->aue_mbuf = m;
1365
1366 /*
1367 * The ADMtek documentation says that the packet length is
1368 * supposed to be specified in the first two bytes of the
1369 * transfer, however it actually seems to ignore this info
1370 * and base the frame size on the bulk transfer length.
1371 */
1372 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1373 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1374 total_len = m->m_pkthdr.len + 2;
1375
1376 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1377 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1378 AUE_TX_TIMEOUT, aue_txeof);
1379
1380 /* Transmit */
1381 err = usbd_transfer(c->aue_xfer);
1382 if (err != USBD_IN_PROGRESS) {
1383 aue_stop(sc);
1384 return (EIO);
1385 }
1386 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1387 __FUNCTION__, total_len));
1388
1389 sc->aue_cdata.aue_tx_cnt++;
1390
1391 return (0);
1392 }
1393
1394 static void
1395 aue_start(ifp)
1396 struct ifnet *ifp;
1397 {
1398 struct aue_softc *sc = ifp->if_softc;
1399 struct mbuf *m_head = NULL;
1400
1401 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1402 __FUNCTION__, sc->aue_link));
1403
1404 if (sc->aue_dying)
1405 return;
1406
1407 if (!sc->aue_link)
1408 return;
1409
1410 if (ifp->if_flags & IFF_OACTIVE)
1411 return;
1412
1413 IF_DEQUEUE(&ifp->if_snd, m_head);
1414 if (m_head == NULL)
1415 return;
1416
1417 if (aue_send(sc, m_head, 0)) {
1418 IF_PREPEND(&ifp->if_snd, m_head);
1419 ifp->if_flags |= IFF_OACTIVE;
1420 return;
1421 }
1422
1423 /*
1424 * If there's a BPF listener, bounce a copy of this frame
1425 * to him.
1426 */
1427 if (ifp->if_bpf)
1428 bpf_mtap(ifp, m_head);
1429
1430 ifp->if_flags |= IFF_OACTIVE;
1431
1432 /*
1433 * Set a timeout in case the chip goes out to lunch.
1434 */
1435 ifp->if_timer = 5;
1436 }
1437
1438 static void
1439 aue_init(xsc)
1440 void *xsc;
1441 {
1442 struct aue_softc *sc = xsc;
1443 struct ifnet *ifp = GET_IFP(sc);
1444 struct mii_data *mii = GET_MII(sc);
1445 int i, s;
1446 u_char *eaddr;
1447
1448 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1449
1450 if (sc->aue_dying)
1451 return;
1452
1453 if (ifp->if_flags & IFF_RUNNING)
1454 return;
1455
1456 s = splimp();
1457
1458 /*
1459 * Cancel pending I/O and free all RX/TX buffers.
1460 */
1461 aue_reset(sc);
1462
1463 #if defined(__FreeBSD__)
1464 eaddr = sc->arpcom.ac_enaddr;
1465 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1466 eaddr = LLADDR(ifp->if_sadl);
1467 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1468 for (i = 0; i < ETHER_ADDR_LEN; i++)
1469 csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1470
1471 /* If we want promiscuous mode, set the allframes bit. */
1472 if (ifp->if_flags & IFF_PROMISC)
1473 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1474 else
1475 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1476
1477 /* Init TX ring. */
1478 if (aue_tx_list_init(sc) == ENOBUFS) {
1479 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1480 splx(s);
1481 return;
1482 }
1483
1484 /* Init RX ring. */
1485 if (aue_rx_list_init(sc) == ENOBUFS) {
1486 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1487 splx(s);
1488 return;
1489 }
1490
1491 /* Load the multicast filter. */
1492 aue_setmulti(sc);
1493
1494 /* Enable RX and TX */
1495 csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
1496 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1497 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1498
1499 mii_mediachg(mii);
1500
1501 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1502 if (aue_openpipes(sc)) {
1503 splx(s);
1504 return;
1505 }
1506 }
1507
1508 ifp->if_flags |= IFF_RUNNING;
1509 ifp->if_flags &= ~IFF_OACTIVE;
1510
1511 splx(s);
1512
1513 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1514 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1515 }
1516
1517 static int
1518 aue_openpipes(sc)
1519 struct aue_softc *sc;
1520 {
1521 struct aue_chain *c;
1522 usbd_status err;
1523 int i;
1524
1525 /* Open RX and TX pipes. */
1526 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1527 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1528 if (err) {
1529 printf("%s: open rx pipe failed: %s\n",
1530 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1531 return (EIO);
1532 }
1533 usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1534 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1535 if (err) {
1536 printf("%s: open tx pipe failed: %s\n",
1537 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1538 return (EIO);
1539 }
1540 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1541 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1542 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1543 AUE_INTR_INTERVAL);
1544 if (err) {
1545 printf("%s: open intr pipe failed: %s\n",
1546 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1547 return (EIO);
1548 }
1549
1550 /* Start up the receive pipe. */
1551 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1552 c = &sc->aue_cdata.aue_rx_chain[i];
1553 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1554 c, c->aue_buf, AUE_BUFSZ,
1555 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1556 aue_rxeof);
1557 (void)usbd_transfer(c->aue_xfer); /* XXX */
1558 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1559 __FUNCTION__));
1560
1561 }
1562 return (0);
1563 }
1564
1565 /*
1566 * Set media options.
1567 */
1568 static int
1569 aue_ifmedia_upd(ifp)
1570 struct ifnet *ifp;
1571 {
1572 struct aue_softc *sc = ifp->if_softc;
1573 struct mii_data *mii = GET_MII(sc);
1574
1575 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1576
1577 if (sc->aue_dying)
1578 return (0);
1579
1580 sc->aue_link = 0;
1581 if (mii->mii_instance) {
1582 struct mii_softc *miisc;
1583 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1584 miisc = LIST_NEXT(miisc, mii_list))
1585 mii_phy_reset(miisc);
1586 }
1587 mii_mediachg(mii);
1588
1589 return (0);
1590 }
1591
1592 /*
1593 * Report current media status.
1594 */
1595 static void
1596 aue_ifmedia_sts(ifp, ifmr)
1597 struct ifnet *ifp;
1598 struct ifmediareq *ifmr;
1599 {
1600 struct aue_softc *sc = ifp->if_softc;
1601 struct mii_data *mii = GET_MII(sc);
1602
1603 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1604
1605 mii_pollstat(mii);
1606 ifmr->ifm_active = mii->mii_media_active;
1607 ifmr->ifm_status = mii->mii_media_status;
1608 }
1609
1610 static int
1611 aue_ioctl(ifp, command, data)
1612 struct ifnet *ifp;
1613 u_long command;
1614 caddr_t data;
1615 {
1616 struct aue_softc *sc = ifp->if_softc;
1617 #if defined(__NetBSD__) || defined(__OpenBSD__)
1618 struct ifaddr *ifa = (struct ifaddr *)data;
1619 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1620 struct ifreq *ifr = (struct ifreq *)data;
1621 struct mii_data *mii;
1622 int s, error = 0;
1623
1624 if (sc->aue_dying)
1625 return (EIO);
1626
1627 s = splimp();
1628
1629 switch(command) {
1630 #if defined(__FreeBSD__)
1631 case SIOCSIFADDR:
1632 case SIOCGIFADDR:
1633 case SIOCSIFMTU:
1634 error = ether_ioctl(ifp, command, data);
1635 break;
1636 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1637 case SIOCSIFADDR:
1638 ifp->if_flags |= IFF_UP;
1639 aue_init(sc);
1640
1641 switch (ifa->ifa_addr->sa_family) {
1642 #ifdef INET
1643 case AF_INET:
1644 arp_ifinit(ifp, ifa);
1645 break;
1646 #endif /* INET */
1647 #ifdef NS
1648 case AF_NS:
1649 {
1650 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1651
1652 if (ns_nullhost(*ina))
1653 ina->x_host = *(union ns_host *)
1654 LLADDR(ifp->if_sadl);
1655 else
1656 memcpy(LLADDR(ifp->if_sadl),
1657 ina->x_host.c_host,
1658 ifp->if_addrlen);
1659 break;
1660 }
1661 #endif /* NS */
1662 }
1663 break;
1664
1665 case SIOCSIFMTU:
1666 if (ifr->ifr_mtu > ETHERMTU)
1667 error = EINVAL;
1668 else
1669 ifp->if_mtu = ifr->ifr_mtu;
1670 break;
1671
1672 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1673 case SIOCSIFFLAGS:
1674 if (ifp->if_flags & IFF_UP) {
1675 if (ifp->if_flags & IFF_RUNNING &&
1676 ifp->if_flags & IFF_PROMISC &&
1677 !(sc->aue_if_flags & IFF_PROMISC)) {
1678 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1679 } else if (ifp->if_flags & IFF_RUNNING &&
1680 !(ifp->if_flags & IFF_PROMISC) &&
1681 sc->aue_if_flags & IFF_PROMISC) {
1682 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1683 } else if (!(ifp->if_flags & IFF_RUNNING))
1684 aue_init(sc);
1685 } else {
1686 if (ifp->if_flags & IFF_RUNNING)
1687 aue_stop(sc);
1688 }
1689 sc->aue_if_flags = ifp->if_flags;
1690 error = 0;
1691 break;
1692 case SIOCADDMULTI:
1693 case SIOCDELMULTI:
1694 aue_setmulti(sc);
1695 error = 0;
1696 break;
1697 case SIOCGIFMEDIA:
1698 case SIOCSIFMEDIA:
1699 mii = GET_MII(sc);
1700 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1701 break;
1702 default:
1703 error = EINVAL;
1704 break;
1705 }
1706
1707 splx(s);
1708
1709 return (error);
1710 }
1711
1712 static void
1713 aue_watchdog(ifp)
1714 struct ifnet *ifp;
1715 {
1716 struct aue_softc *sc = ifp->if_softc;
1717
1718 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1719
1720 ifp->if_oerrors++;
1721 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1722
1723 /*
1724 * The polling business is a kludge to avoid allowing the
1725 * USB code to call tsleep() in usbd_delay_ms(), which will
1726 * kill us since the watchdog routine is invoked from
1727 * interrupt context.
1728 */
1729 usbd_set_polling(sc->aue_udev, 1);
1730 aue_stop(sc);
1731 aue_init(sc);
1732 usbd_set_polling(sc->aue_udev, 0);
1733
1734 if (ifp->if_snd.ifq_head != NULL)
1735 aue_start(ifp);
1736 }
1737
1738 /*
1739 * Stop the adapter and free any mbufs allocated to the
1740 * RX and TX lists.
1741 */
1742 static void
1743 aue_stop(sc)
1744 struct aue_softc *sc;
1745 {
1746 usbd_status err;
1747 struct ifnet *ifp;
1748 int i;
1749
1750 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1751
1752 ifp = GET_IFP(sc);
1753 ifp->if_timer = 0;
1754
1755 csr_write_1(sc, AUE_CTL0, 0);
1756 csr_write_1(sc, AUE_CTL1, 0);
1757 aue_reset(sc);
1758 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1759
1760 /* Stop transfers. */
1761 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1762 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1763 if (err) {
1764 printf("%s: abort rx pipe failed: %s\n",
1765 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1766 }
1767 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1768 if (err) {
1769 printf("%s: close rx pipe failed: %s\n",
1770 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1771 }
1772 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1773 }
1774
1775 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1776 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1777 if (err) {
1778 printf("%s: abort tx pipe failed: %s\n",
1779 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1780 }
1781 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1782 if (err) {
1783 printf("%s: close tx pipe failed: %s\n",
1784 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1785 }
1786 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1787 }
1788
1789 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1790 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1791 if (err) {
1792 printf("%s: abort intr pipe failed: %s\n",
1793 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1794 }
1795 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1796 if (err) {
1797 printf("%s: close intr pipe failed: %s\n",
1798 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1799 }
1800 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1801 }
1802
1803 /* Free RX resources. */
1804 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1805 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1806 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1807 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1808 }
1809 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1810 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1811 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1812 }
1813 }
1814
1815 /* Free TX resources. */
1816 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1817 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1818 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1819 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1820 }
1821 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1822 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1823 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1824 }
1825 }
1826
1827 sc->aue_link = 0;
1828
1829 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1830 }
1831
1832 #ifdef __FreeBSD__
1833 /*
1834 * Stop all chip I/O so that the kernel's probe routines don't
1835 * get confused by errant DMAs when rebooting.
1836 */
1837 static void
1838 aue_shutdown(dev)
1839 device_ptr_t dev;
1840 {
1841 struct aue_softc *sc = USBGETSOFTC(dev);
1842
1843 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1844
1845 aue_reset(sc);
1846 aue_stop(sc);
1847 }
1848 #endif
1849