if_aue.c revision 1.14 1 /* $NetBSD: if_aue.c,v 1.14 2000/01/19 00:25:23 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
106 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
107
108 #include <net/if.h>
109 #include <net/if_arp.h>
110 #include <net/if_dl.h>
111 #include <net/if_media.h>
112
113 #if defined(__NetBSD__) || defined(__OpenBSD__)
114 #include <net/if_ether.h>
115
116 #define bpf_mtap(ifp, m) bpf_tap((ifp)->if_bpf, mtod((m), caddr_t), (m)->m_len)
117
118 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
119
120 #if defined(__FreeBSD__) || NBPFILTER > 0
121 #include <net/bpf.h>
122 #endif
123
124 #if defined(__NetBSD__) || defined(__OpenBSD__)
125 #ifdef INET
126 #include <netinet/in.h>
127 #include <netinet/if_inarp.h>
128 #endif
129
130 #ifdef NS
131 #include <netns/ns.h>
132 #include <netns/ns_if.h>
133 #endif
134 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
135
136 #include <dev/mii/mii.h>
137 #include <dev/mii/miivar.h>
138
139 #include <dev/usb/usb.h>
140 #include <dev/usb/usbdi.h>
141 #include <dev/usb/usbdi_util.h>
142 #include <dev/usb/usbdevs.h>
143
144 #ifdef __FreeBSD__
145 #include <dev/usb/usb_ethersubr.h>
146 #endif
147
148 #include <dev/usb/if_auereg.h>
149
150 #ifdef AUE_DEBUG
151 #define DPRINTF(x) if (auedebug) logprintf x
152 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x
153 int auedebug = 0;
154 #else
155 #define DPRINTF(x)
156 #define DPRINTFN(n,x)
157 #endif
158
159 int aue_cutoff = AUE_CUTOFF;
160 #undef AUE_CUTOFF
161 #define AUE_CUTOFF aue_cutoff
162
163 /*
164 * Various supported device vendors/products.
165 */
166 static struct aue_type aue_devs[] = {
167 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
168 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
169 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
170 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
171 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
172 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
173 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
174 { 0, 0 }
175 };
176
177 USB_DECLARE_DRIVER(aue);
178
179 static int aue_tx_list_init __P((struct aue_softc *));
180 static int aue_rx_list_init __P((struct aue_softc *));
181 static int aue_newbuf __P((struct aue_softc *, struct aue_chain *,
182 struct mbuf *));
183 static int aue_send __P((struct aue_softc *, struct mbuf *, int));
184 static void aue_intr __P((usbd_xfer_handle,
185 usbd_private_handle, usbd_status));
186 static void aue_rxeof __P((usbd_xfer_handle,
187 usbd_private_handle, usbd_status));
188 static void aue_txeof __P((usbd_xfer_handle,
189 usbd_private_handle, usbd_status));
190 static void aue_tick __P((void *));
191 static void aue_start __P((struct ifnet *));
192 static int aue_ioctl __P((struct ifnet *, u_long, caddr_t));
193 static void aue_init __P((void *));
194 static void aue_stop __P((struct aue_softc *));
195 static void aue_watchdog __P((struct ifnet *));
196 #ifdef __FreeBSD__
197 static void aue_shutdown __P((device_ptr_t));
198 #endif
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 u_int16_t auxmode;
549 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
550 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
551 }
552 }
553
554 #define AUE_POLY 0xEDB88320
555 #define AUE_BITS 6
556
557 static u_int32_t
558 aue_crc(addr)
559 caddr_t addr;
560 {
561 u_int32_t idx, bit, data, crc;
562
563 /* Compute CRC for the address value. */
564 crc = 0xFFFFFFFF; /* initial value */
565
566 for (idx = 0; idx < 6; idx++) {
567 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
568 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
569 }
570
571 return (crc & ((1 << AUE_BITS) - 1));
572 }
573
574 static void
575 aue_setmulti(sc)
576 struct aue_softc *sc;
577 {
578 struct ifnet *ifp;
579 #if defined(__FreeBSD__)
580 struct ifmultiaddr *ifma;
581 #elif defined(__NetBSD__) || defined(__OpenBSD__)
582 struct ether_multi *enm;
583 struct ether_multistep step;
584 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
585 u_int32_t h = 0, i;
586
587 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
588
589 ifp = GET_IFP(sc);
590
591 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
592 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
593 return;
594 }
595
596 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
597
598 /* first, zot all the existing hash bits */
599 for (i = 0; i < 8; i++)
600 csr_write_1(sc, AUE_MAR0 + i, 0);
601
602 /* now program new ones */
603 #if defined(__FreeBSD__)
604 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
605 ifma = ifma->ifma_link.le_next) {
606 if (ifma->ifma_addr->sa_family != AF_LINK)
607 continue;
608 h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
609 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
610 }
611 #elif defined(__NetBSD__) || defined(__OpenBSD__)
612 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
613 while (enm != NULL) {
614 #if 1
615 if (memcmp(enm->enm_addrlo,
616 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
617 ifp->if_flags |= IFF_ALLMULTI;
618 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
619 return;
620 }
621 #endif
622 h = aue_crc(enm->enm_addrlo);
623 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
624 ETHER_NEXT_MULTI(step, enm);
625 }
626 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
627 }
628
629 static void
630 aue_reset(sc)
631 struct aue_softc *sc;
632 {
633 int i;
634
635 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
636
637 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
638
639 for (i = 0; i < AUE_TIMEOUT; i++) {
640 if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
641 break;
642 }
643
644 if (i == AUE_TIMEOUT)
645 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
646
647 /*
648 * The PHY(s) attached to the Pegasus chip may be held
649 * in reset until we flip on the GPIO outputs. Make sure
650 * to set the GPIO pins high so that the PHY(s) will
651 * be enabled.
652 *
653 * Note: We force all of the GPIO pins low first, *then*
654 * enable the ones we want.
655 */
656 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
657 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
658
659 /* Grrr. LinkSys has to be different from everyone else. */
660 if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
661 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
662 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
663 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
664 AUE_GPIO_OUT0);
665 }
666
667 /* Wait a little while for the chip to get its brains in order. */
668 DELAY(10000); /* XXX */
669 }
670
671 /*
672 * Probe for a Pegasus chip.
673 */
674 USB_MATCH(aue)
675 {
676 USB_MATCH_START(aue, uaa);
677 struct aue_type *t;
678
679 if (uaa->iface != NULL)
680 return (UMATCH_NONE);
681
682 for (t = aue_devs; t->aue_vid != 0; t++)
683 if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
684 return (UMATCH_VENDOR_PRODUCT);
685
686 return (UMATCH_NONE);
687 }
688
689 /*
690 * Attach the interface. Allocate softc structures, do ifmedia
691 * setup and ethernet/BPF attach.
692 */
693 USB_ATTACH(aue)
694 {
695 USB_ATTACH_START(aue, sc, uaa);
696 char devinfo[1024];
697 int s;
698 u_char eaddr[ETHER_ADDR_LEN];
699 struct ifnet *ifp;
700 struct mii_data *mii;
701 usbd_device_handle dev = uaa->device;
702 usbd_interface_handle iface;
703 usbd_status err;
704 usb_interface_descriptor_t *id;
705 usb_endpoint_descriptor_t *ed;
706 int i;
707
708 #ifdef __FreeBSD__
709 bzero(sc, sizeof(struct aue_softc));
710 #endif
711
712 DPRINTFN(5,(" : aue_attach: sc=%p", sc));
713
714 usbd_devinfo(dev, 0, devinfo);
715 USB_ATTACH_SETUP;
716 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
717
718 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
719 if (err) {
720 printf("%s: setting config no failed\n",
721 USBDEVNAME(sc->aue_dev));
722 USB_ATTACH_ERROR_RETURN;
723 }
724
725 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
726 if (err) {
727 printf("%s: getting interface handle failed\n",
728 USBDEVNAME(sc->aue_dev));
729 USB_ATTACH_ERROR_RETURN;
730 }
731
732 sc->aue_udev = dev;
733 sc->aue_iface = iface;
734 sc->aue_product = uaa->product;
735 sc->aue_vendor = uaa->vendor;
736
737 id = usbd_get_interface_descriptor(iface);
738
739 /* Find endpoints. */
740 for (i = 0; i < id->bNumEndpoints; i++) {
741 ed = usbd_interface2endpoint_descriptor(iface, i);
742 if (!ed) {
743 printf("%s: couldn't get endpoint descriptor %d\n",
744 USBDEVNAME(sc->aue_dev), i);
745 USB_ATTACH_ERROR_RETURN;
746 }
747 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
748 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
749 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
750 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
751 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
752 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
753 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
754 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
755 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
756 }
757 }
758
759 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
760 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
761 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
762 USB_ATTACH_ERROR_RETURN;
763 }
764
765
766 s = splimp();
767
768 /* Reset the adapter. */
769 aue_reset(sc);
770
771 /*
772 * Get station address from the EEPROM.
773 */
774 aue_read_mac(sc, eaddr);
775
776 /*
777 * A Pegasus chip was detected. Inform the world.
778 */
779 #if defined(__FreeBSD__)
780 printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
781 eaddr, ":");
782
783 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
784
785 ifp = &sc->arpcom.ac_if;
786 ifp->if_softc = sc;
787 ifp->if_unit = sc->aue_unit;
788 ifp->if_name = "aue";
789 ifp->if_mtu = ETHERMTU;
790 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
791 ifp->if_ioctl = aue_ioctl;
792 ifp->if_output = ether_output;
793 ifp->if_start = aue_start;
794 ifp->if_watchdog = aue_watchdog;
795 ifp->if_init = aue_init;
796 ifp->if_baudrate = 10000000;
797 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
798
799 /*
800 * Do MII setup.
801 * NOTE: Doing this causes child devices to be attached to us,
802 * which we would normally disconnect at in the detach routine
803 * using device_delete_child(). However the USB code is set up
804 * such that when this driver is removed, all childred devices
805 * are removed as well. In effect, the USB code ends up detaching
806 * all of our children for us, so we don't have to do is ourselves
807 * in aue_detach(). It's important to point this out since if
808 * we *do* try to detach the child devices ourselves, we will
809 * end up getting the children deleted twice, which will crash
810 * the system.
811 */
812 if (mii_phy_probe(self, &sc->aue_miibus,
813 aue_ifmedia_upd, aue_ifmedia_sts)) {
814 printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
815 splx(s);
816 USB_ATTACH_ERROR_RETURN;
817 }
818
819 aue_qdat.ifp = ifp;
820 aue_qdat.if_rxstart = aue_rxstart;
821
822 /*
823 * Call MI attach routines.
824 */
825 if_attach(ifp);
826 ether_ifattach(ifp);
827 callout_handle_init(&sc->aue_stat_ch);
828 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
829
830 usb_register_netisr();
831
832 #elif defined(__NetBSD__) || defined(__OpenBSD__)
833
834 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
835 ether_sprintf(eaddr));
836
837 /* Initialize interface info.*/
838 ifp = &sc->aue_ec.ec_if;
839 ifp->if_softc = sc;
840 ifp->if_mtu = ETHERMTU;
841 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
842 ifp->if_ioctl = aue_ioctl;
843 ifp->if_start = aue_start;
844 ifp->if_watchdog = aue_watchdog;
845 ifp->if_baudrate = 10000000;
846 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
847
848 /* Initialize MII/media info. */
849 mii = &sc->aue_mii;
850 mii->mii_ifp = ifp;
851 mii->mii_readreg = aue_miibus_readreg;
852 mii->mii_writereg = aue_miibus_writereg;
853 mii->mii_statchg = aue_miibus_statchg;
854 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
855 mii_phy_probe(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY);
856 if (LIST_FIRST(&mii->mii_phys) == NULL) {
857 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
858 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
859 } else
860 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
861
862 /* Attach the interface. */
863 if_attach(ifp);
864 ether_ifattach(ifp, eaddr);
865
866 #if NBPFILTER > 0
867 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
868 sizeof(struct ether_header));
869 #endif
870 #if RND > 0
871 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
872 RND_TYPE_NET, 0);
873 #endif
874
875 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
876
877 splx(s);
878
879 USB_ATTACH_SUCCESS_RETURN;
880 }
881
882 USB_DETACH(aue)
883 {
884 USB_DETACH_START(aue, sc);
885 #if defined(__FreeBSD__)
886 struct ifnet *ifp;
887 int s;
888
889 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
890
891 s = splusb();
892
893 ifp = &sc->arpcom.ac_if;
894
895 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
896 if_detach(ifp);
897
898 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
899 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
900 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
901 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
902 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
903 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
904
905 splx(s);
906
907 return (0);
908 #elif defined(__NetBSD__) || defined(__OpenBSD__)
909 sc = sc; /* XXX use sc */
910 /* XXX deallocate */
911
912 #ifdef notyet
913 /*
914 * Our softc is about to go away, so drop our refernce
915 * to the ifnet.
916 */
917 if_delref(sc->aue_ec.ec_if);
918 #else
919 return (0);
920 #endif
921
922 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
923 }
924
925 #if defined(__NetBSD__) || defined(__OpenBSD__)
926 int
927 aue_activate(self, act)
928 device_ptr_t self;
929 enum devact act;
930 {
931 struct aue_softc *sc = (struct aue_softc *)self;
932
933 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
934
935 switch (act) {
936 case DVACT_ACTIVATE:
937 return (EOPNOTSUPP);
938 break;
939
940 case DVACT_DEACTIVATE:
941 #ifdef notyet
942 /* First, kill off the interface. */
943 if_detach(sc->aue_ec.ec_if);
944 #endif
945 sc->aue_dying = 1;
946 break;
947 }
948 return (0);
949 }
950 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
951
952 /*
953 * Initialize an RX descriptor and attach an MBUF cluster.
954 */
955 static int
956 aue_newbuf(sc, c, m)
957 struct aue_softc *sc;
958 struct aue_chain *c;
959 struct mbuf *m;
960 {
961 struct mbuf *m_new = NULL;
962
963 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
964
965 if (m == NULL) {
966 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
967 if (m_new == NULL) {
968 printf("%s: no memory for rx list "
969 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
970 return (ENOBUFS);
971 }
972
973 MCLGET(m_new, M_DONTWAIT);
974 if (!(m_new->m_flags & M_EXT)) {
975 printf("%s: no memory for rx list "
976 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
977 m_freem(m_new);
978 return (ENOBUFS);
979 }
980 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
981 } else {
982 m_new = m;
983 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
984 m_new->m_data = m_new->m_ext.ext_buf;
985 }
986
987 m_adj(m_new, ETHER_ALIGN);
988 c->aue_mbuf = m_new;
989
990 return (0);
991 }
992
993 static int
994 aue_rx_list_init(sc)
995 struct aue_softc *sc;
996 {
997 struct aue_cdata *cd;
998 struct aue_chain *c;
999 int i;
1000
1001 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1002
1003 cd = &sc->aue_cdata;
1004 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1005 c = &cd->aue_rx_chain[i];
1006 c->aue_sc = sc;
1007 c->aue_idx = i;
1008 c->aue_accum = 0;
1009 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1010 return (ENOBUFS);
1011 if (c->aue_xfer == NULL) {
1012 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1013 if (c->aue_xfer == NULL)
1014 return (ENOBUFS);
1015 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1016 if (c->aue_buf == NULL)
1017 return (ENOBUFS); /* XXX free xfer */
1018 }
1019 }
1020
1021 return (0);
1022 }
1023
1024 static int
1025 aue_tx_list_init(sc)
1026 struct aue_softc *sc;
1027 {
1028 struct aue_cdata *cd;
1029 struct aue_chain *c;
1030 int i;
1031
1032 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1033
1034 cd = &sc->aue_cdata;
1035 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1036 c = &cd->aue_tx_chain[i];
1037 c->aue_sc = sc;
1038 c->aue_idx = i;
1039 c->aue_mbuf = NULL;
1040 if (c->aue_xfer == NULL) {
1041 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1042 if (c->aue_xfer == NULL)
1043 return (ENOBUFS);
1044 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1045 if (c->aue_buf == NULL)
1046 return (ENOBUFS);
1047 }
1048 }
1049
1050 return (0);
1051 }
1052
1053 static void
1054 aue_intr(xfer, priv, status)
1055 usbd_xfer_handle xfer;
1056 usbd_private_handle priv;
1057 usbd_status status;
1058 {
1059 struct aue_softc *sc = priv;
1060 struct ifnet *ifp = GET_IFP(sc);
1061 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1062
1063 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1064
1065 if (!(ifp->if_flags & IFF_RUNNING))
1066 return;
1067
1068 if (status != USBD_NORMAL_COMPLETION) {
1069 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1070 return;
1071 }
1072 printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
1073 usbd_errstr(status));
1074 if (status == USBD_STALLED)
1075 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1076 return;
1077 }
1078
1079 if (p->aue_txstat0)
1080 ifp->if_oerrors++;
1081
1082 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1083 ifp->if_collisions++;
1084 }
1085
1086 #if defined(__FreeBSD__)
1087 static void
1088 aue_rxstart(ifp)
1089 struct ifnet *ifp;
1090 {
1091 struct aue_softc *sc;
1092 struct aue_chain *c;
1093
1094 sc = ifp->if_softc;
1095 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
1096
1097 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1098 ifp->if_ierrors++;
1099 return;
1100 }
1101
1102 /* Setup new transfer. */
1103 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1104 c, mtod(c->aue_mbuf, char *), AUE_CUTOFF, USBD_SHORT_XFER_OK,
1105 USBD_NO_TIMEOUT, aue_rxeof);
1106 usbd_transfer(c->aue_xfer);
1107 }
1108 #endif
1109
1110 /*
1111 * A frame has been uploaded: pass the resulting mbuf chain up to
1112 * the higher level protocols.
1113 *
1114 * Grrr. Receiving transfers larger than about 1152 bytes sometimes
1115 * doesn't work. We get an incomplete frame. In order to avoid
1116 * this, we queue up RX transfers that are shorter than a full sized
1117 * frame. If the received frame is larger than our transfer size,
1118 * we snag the rest of the data using a second transfer. Does this
1119 * hurt performance? Yes. But after fighting with this stupid thing
1120 * for three days, I'm willing to settle. I'd rather have reliable
1121 * receive performance that fast but spotty performance.
1122 */
1123 static void
1124 aue_rxeof(xfer, priv, status)
1125 usbd_xfer_handle xfer;
1126 usbd_private_handle priv;
1127 usbd_status status;
1128 {
1129 struct aue_chain *c = priv;
1130 struct aue_softc *sc = c->aue_sc;
1131 struct ifnet *ifp = GET_IFP(sc);
1132 struct mbuf *m;
1133 u_int32_t total_len;
1134 struct aue_rxpkt r;
1135 #if defined(__NetBSD__) || defined(__OpenBSD__)
1136 int s;
1137 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1138
1139 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1140
1141 if (!(ifp->if_flags & IFF_RUNNING))
1142 return;
1143
1144 if (status != USBD_NORMAL_COMPLETION) {
1145 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1146 return;
1147 printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
1148 usbd_errstr(status));
1149 if (status == USBD_STALLED)
1150 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1151 goto done;
1152 }
1153
1154 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1155
1156 /* XXX copy data to mbuf */
1157 memcpy(mtod(c->aue_mbuf, char*) + c->aue_accum, c->aue_buf, total_len);
1158
1159 /*
1160 * See if we've already accumulated some data from
1161 * a previous transfer.
1162 */
1163 if (c->aue_accum) {
1164 total_len += c->aue_accum;
1165 c->aue_accum = 0;
1166 }
1167
1168 if (total_len <= 4 + ETHER_CRC_LEN) {
1169 ifp->if_ierrors++;
1170 goto done;
1171 }
1172
1173 m = c->aue_mbuf;
1174 memcpy(&r, mtod(m, char *) + 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
1179 /*
1180 * Check to see if this is just the first chunk of a
1181 * split transfer. We really need a more reliable way
1182 * to detect this.
1183 */
1184 if (UGETW(r.aue_pktlen) != total_len && total_len == AUE_CUTOFF) {
1185 c->aue_accum = AUE_CUTOFF;
1186 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1187 c, c->aue_buf,
1188 AUE_CUTOFF, USBD_SHORT_XFER_OK | USBD_NO_COPY,
1189 USBD_NO_TIMEOUT, aue_rxeof);
1190 DPRINTFN(5,("%s: %s: extra rx\n", USBDEVNAME(sc->aue_dev),
1191 __FUNCTION__));
1192 usbd_transfer(xfer);
1193 return;
1194 }
1195
1196 if (r.aue_rxstat) {
1197 ifp->if_ierrors++;
1198 goto done;
1199 }
1200
1201 /* No errors; receive the packet. */
1202 total_len -= ETHER_CRC_LEN + 4;
1203 m->m_pkthdr.len = m->m_len = total_len;
1204 ifp->if_ipackets++;
1205
1206 #if defined(__FreeBSD__)
1207 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
1208 /* Put the packet on the special USB input queue. */
1209 usb_ether_input(m);
1210
1211 return;
1212
1213 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1214 m->m_pkthdr.rcvif = ifp;
1215
1216 s = splimp();
1217
1218 /* XXX ugly */
1219 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1220 ifp->if_ierrors++;
1221 goto done1;
1222 }
1223
1224 /*
1225 * Handle BPF listeners. Let the BPF user see the packet, but
1226 * don't pass it up to the ether_input() layer unless it's
1227 * a broadcast packet, multicast packet, matches our ethernet
1228 * address or the interface is in promiscuous mode.
1229 */
1230 if (ifp->if_bpf) {
1231 struct ether_header *eh = mtod(m, struct ether_header *);
1232 bpf_mtap(ifp, m);
1233 if ((ifp->if_flags & IFF_PROMISC) &&
1234 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1235 ETHER_ADDR_LEN) &&
1236 !(eh->ether_dhost[0] & 1)) {
1237 m_freem(m);
1238 goto done1;
1239 }
1240 }
1241
1242 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1243 __FUNCTION__, m->m_len));
1244 (*ifp->if_input)(ifp, m);
1245 done1:
1246 splx(s);
1247 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1248
1249 done:
1250
1251 /* Setup new transfer. */
1252 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1253 c, c->aue_buf, AUE_CUTOFF,
1254 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1255 USBD_NO_TIMEOUT, aue_rxeof);
1256 usbd_transfer(xfer);
1257
1258 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1259 __FUNCTION__));
1260 }
1261
1262 /*
1263 * A frame was downloaded to the chip. It's safe for us to clean up
1264 * the list buffers.
1265 */
1266
1267 static void
1268 aue_txeof(xfer, priv, status)
1269 usbd_xfer_handle xfer;
1270 usbd_private_handle priv;
1271 usbd_status status;
1272 {
1273 struct aue_chain *c = priv;
1274 struct aue_softc *sc = c->aue_sc;
1275 struct ifnet *ifp = GET_IFP(sc);
1276 int s;
1277
1278 s = splimp();
1279
1280 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1281 __FUNCTION__, status));
1282
1283 ifp->if_timer = 0;
1284 ifp->if_flags &= ~IFF_OACTIVE;
1285
1286 if (status != USBD_NORMAL_COMPLETION) {
1287 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1288 splx(s);
1289 return;
1290 }
1291 ifp->if_oerrors++;
1292 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1293 usbd_errstr(status));
1294 if (status == USBD_STALLED)
1295 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1296 splx(s);
1297 return;
1298 }
1299
1300 ifp->if_opackets++;
1301
1302 #if defined(__FreeBSD__)
1303 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1304 usb_tx_done(c->aue_mbuf);
1305 c->aue_mbuf = NULL;
1306 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1307 m_freem(c->aue_mbuf);
1308 c->aue_mbuf = NULL;
1309
1310 if (ifp->if_snd.ifq_head != NULL)
1311 aue_start(ifp);
1312 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1313
1314 splx(s);
1315 }
1316
1317 static void
1318 aue_tick(xsc)
1319 void *xsc;
1320 {
1321 struct aue_softc *sc = xsc;
1322 struct ifnet *ifp;
1323 struct mii_data *mii;
1324 int s;
1325
1326 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1327
1328 if (sc == NULL)
1329 return;
1330
1331 ifp = GET_IFP(sc);
1332 mii = GET_MII(sc);
1333 if (mii == NULL)
1334 return;
1335
1336 s = splimp();
1337
1338 mii_tick(mii);
1339 if (!sc->aue_link) {
1340 mii_pollstat(mii);
1341 if (mii->mii_media_status & IFM_ACTIVE &&
1342 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1343 DPRINTFN(2,("%s: %s: got link\n",
1344 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1345 sc->aue_link++;
1346 if (ifp->if_snd.ifq_head != NULL)
1347 aue_start(ifp);
1348 }
1349 }
1350
1351 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1352
1353 splx(s);
1354 }
1355
1356 static int
1357 aue_send(sc, m, idx)
1358 struct aue_softc *sc;
1359 struct mbuf *m;
1360 int idx;
1361 {
1362 int total_len;
1363 struct aue_chain *c;
1364 usbd_status err;
1365
1366 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1367
1368 c = &sc->aue_cdata.aue_tx_chain[idx];
1369
1370 /*
1371 * Copy the mbuf data into a contiguous buffer, leaving two
1372 * bytes at the beginning to hold the frame length.
1373 */
1374 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1375 c->aue_mbuf = m;
1376
1377 /*
1378 * The ADMtek documentation says that the packet length is
1379 * supposed to be specified in the first two bytes of the
1380 * transfer, however it actually seems to ignore this info
1381 * and base the frame size on the bulk transfer length.
1382 */
1383 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1384 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1385 total_len = m->m_pkthdr.len + 2;
1386
1387 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1388 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1389 AUE_TX_TIMEOUT, aue_txeof);
1390
1391 /* Transmit */
1392 err = usbd_transfer(c->aue_xfer);
1393 if (err != USBD_IN_PROGRESS) {
1394 aue_stop(sc);
1395 return (EIO);
1396 }
1397 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1398 __FUNCTION__, total_len));
1399
1400 sc->aue_cdata.aue_tx_cnt++;
1401
1402 return (0);
1403 }
1404
1405 static void
1406 aue_start(ifp)
1407 struct ifnet *ifp;
1408 {
1409 struct aue_softc *sc = ifp->if_softc;
1410 struct mbuf *m_head = NULL;
1411
1412 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1413 __FUNCTION__, sc->aue_link));
1414
1415 if (!sc->aue_link)
1416 return;
1417
1418 if (ifp->if_flags & IFF_OACTIVE)
1419 return;
1420
1421 IF_DEQUEUE(&ifp->if_snd, m_head);
1422 if (m_head == NULL)
1423 return;
1424
1425 if (aue_send(sc, m_head, 0)) {
1426 IF_PREPEND(&ifp->if_snd, m_head);
1427 ifp->if_flags |= IFF_OACTIVE;
1428 return;
1429 }
1430
1431 /*
1432 * If there's a BPF listener, bounce a copy of this frame
1433 * to him.
1434 */
1435 if (ifp->if_bpf)
1436 bpf_mtap(ifp, m_head);
1437
1438 ifp->if_flags |= IFF_OACTIVE;
1439
1440 /*
1441 * Set a timeout in case the chip goes out to lunch.
1442 */
1443 ifp->if_timer = 5;
1444 }
1445
1446 static void
1447 aue_init(xsc)
1448 void *xsc;
1449 {
1450 struct aue_softc *sc = xsc;
1451 struct ifnet *ifp = GET_IFP(sc);
1452 struct mii_data *mii = GET_MII(sc);
1453 struct aue_chain *c;
1454 usbd_status err;
1455 int i, s;
1456 u_char *eaddr;
1457
1458 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1459
1460 if (ifp->if_flags & IFF_RUNNING)
1461 return;
1462
1463 s = splimp();
1464
1465 /*
1466 * Cancel pending I/O and free all RX/TX buffers.
1467 */
1468 aue_reset(sc);
1469
1470 #if defined(__FreeBSD__)
1471 eaddr = sc->arpcom.ac_enaddr;
1472 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1473 eaddr = LLADDR(ifp->if_sadl);
1474 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1475 for (i = 0; i < ETHER_ADDR_LEN; i++)
1476 csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1477
1478 /* If we want promiscuous mode, set the allframes bit. */
1479 if (ifp->if_flags & IFF_PROMISC)
1480 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1481 else
1482 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1483
1484 /* Init TX ring. */
1485 if (aue_tx_list_init(sc) == ENOBUFS) {
1486 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1487 splx(s);
1488 return;
1489 }
1490
1491 /* Init RX ring. */
1492 if (aue_rx_list_init(sc) == ENOBUFS) {
1493 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1494 splx(s);
1495 return;
1496 }
1497
1498 /* Load the multicast filter. */
1499 aue_setmulti(sc);
1500
1501 /* Enable RX and TX */
1502 csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
1503 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1504 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1505
1506 mii_mediachg(mii);
1507
1508 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1509 /* Open RX and TX pipes. */
1510 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1511 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1512 if (err) {
1513 printf("%s: open rx pipe failed: %s\n",
1514 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1515 splx(s);
1516 return;
1517 }
1518 usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1519 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1520 if (err) {
1521 printf("%s: open tx pipe failed: %s\n",
1522 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1523 splx(s);
1524 return;
1525 }
1526 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1527 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1528 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1529 AUE_INTR_INTERVAL);
1530 if (err) {
1531 printf("%s: open intr pipe failed: %s\n",
1532 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1533 splx(s);
1534 return;
1535 }
1536
1537 /* Start up the receive pipe. */
1538 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1539 c = &sc->aue_cdata.aue_rx_chain[i];
1540 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1541 c, c->aue_buf, AUE_CUTOFF,
1542 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1543 aue_rxeof);
1544 usbd_transfer(c->aue_xfer);
1545 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1546 __FUNCTION__));
1547
1548 }
1549 }
1550
1551 ifp->if_flags |= IFF_RUNNING;
1552 ifp->if_flags &= ~IFF_OACTIVE;
1553
1554 splx(s);
1555
1556 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1557 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1558 }
1559
1560 /*
1561 * Set media options.
1562 */
1563 static int
1564 aue_ifmedia_upd(ifp)
1565 struct ifnet *ifp;
1566 {
1567 struct aue_softc *sc = ifp->if_softc;
1568 struct mii_data *mii = GET_MII(sc);
1569
1570 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1571
1572 sc->aue_link = 0;
1573 if (mii->mii_instance) {
1574 struct mii_softc *miisc;
1575 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1576 miisc = LIST_NEXT(miisc, mii_list))
1577 mii_phy_reset(miisc);
1578 }
1579 mii_mediachg(mii);
1580
1581 return (0);
1582 }
1583
1584 /*
1585 * Report current media status.
1586 */
1587 static void
1588 aue_ifmedia_sts(ifp, ifmr)
1589 struct ifnet *ifp;
1590 struct ifmediareq *ifmr;
1591 {
1592 struct aue_softc *sc = ifp->if_softc;
1593 struct mii_data *mii = GET_MII(sc);
1594
1595 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1596
1597 mii_pollstat(mii);
1598 ifmr->ifm_active = mii->mii_media_active;
1599 ifmr->ifm_status = mii->mii_media_status;
1600 }
1601
1602 static int
1603 aue_ioctl(ifp, command, data)
1604 struct ifnet *ifp;
1605 u_long command;
1606 caddr_t data;
1607 {
1608 struct aue_softc *sc = ifp->if_softc;
1609 #if defined(__NetBSD__) || defined(__OpenBSD__)
1610 struct ifaddr *ifa = (struct ifaddr *)data;
1611 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1612 struct ifreq *ifr = (struct ifreq *)data;
1613 struct mii_data *mii;
1614 int s, error = 0;
1615
1616 s = splimp();
1617
1618 switch(command) {
1619 #if defined(__FreeBSD__)
1620 case SIOCSIFADDR:
1621 case SIOCGIFADDR:
1622 case SIOCSIFMTU:
1623 error = ether_ioctl(ifp, command, data);
1624 break;
1625 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1626 case SIOCSIFADDR:
1627 ifp->if_flags |= IFF_UP;
1628 aue_init(sc);
1629
1630 switch (ifa->ifa_addr->sa_family) {
1631 #ifdef INET
1632 case AF_INET:
1633 arp_ifinit(ifp, ifa);
1634 break;
1635 #endif /* INET */
1636 #ifdef NS
1637 case AF_NS:
1638 {
1639 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1640
1641 if (ns_nullhost(*ina))
1642 ina->x_host = *(union ns_host *)
1643 LLADDR(ifp->if_sadl);
1644 else
1645 memcpy(LLADDR(ifp->if_sadl),
1646 ina->x_host.c_host,
1647 ifp->if_addrlen);
1648 break;
1649 }
1650 #endif /* NS */
1651 }
1652 break;
1653
1654 case SIOCSIFMTU:
1655 if (ifr->ifr_mtu > ETHERMTU)
1656 error = EINVAL;
1657 else
1658 ifp->if_mtu = ifr->ifr_mtu;
1659 break;
1660
1661 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1662 case SIOCSIFFLAGS:
1663 if (ifp->if_flags & IFF_UP) {
1664 if (ifp->if_flags & IFF_RUNNING &&
1665 ifp->if_flags & IFF_PROMISC &&
1666 !(sc->aue_if_flags & IFF_PROMISC)) {
1667 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1668 } else if (ifp->if_flags & IFF_RUNNING &&
1669 !(ifp->if_flags & IFF_PROMISC) &&
1670 sc->aue_if_flags & IFF_PROMISC) {
1671 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1672 } else if (!(ifp->if_flags & IFF_RUNNING))
1673 aue_init(sc);
1674 } else {
1675 if (ifp->if_flags & IFF_RUNNING)
1676 aue_stop(sc);
1677 }
1678 sc->aue_if_flags = ifp->if_flags;
1679 error = 0;
1680 break;
1681 case SIOCADDMULTI:
1682 case SIOCDELMULTI:
1683 aue_setmulti(sc);
1684 error = 0;
1685 break;
1686 case SIOCGIFMEDIA:
1687 case SIOCSIFMEDIA:
1688 mii = GET_MII(sc);
1689 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1690 break;
1691 default:
1692 error = EINVAL;
1693 break;
1694 }
1695
1696 splx(s);
1697
1698 return (error);
1699 }
1700
1701 static void
1702 aue_watchdog(ifp)
1703 struct ifnet *ifp;
1704 {
1705 struct aue_softc *sc = ifp->if_softc;
1706
1707 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1708
1709 ifp->if_oerrors++;
1710 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1711
1712 /*
1713 * The polling business is a kludge to avoid allowing the
1714 * USB code to call tsleep() in usbd_delay_ms(), which will
1715 * kill us since the watchdog routine is invoked from
1716 * interrupt context.
1717 */
1718 usbd_set_polling(sc->aue_udev, 1);
1719 aue_stop(sc);
1720 aue_init(sc);
1721 usbd_set_polling(sc->aue_udev, 0);
1722
1723 if (ifp->if_snd.ifq_head != NULL)
1724 aue_start(ifp);
1725 }
1726
1727 /*
1728 * Stop the adapter and free any mbufs allocated to the
1729 * RX and TX lists.
1730 */
1731 static void
1732 aue_stop(sc)
1733 struct aue_softc *sc;
1734 {
1735 usbd_status err;
1736 struct ifnet *ifp;
1737 int i;
1738
1739 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1740
1741 ifp = GET_IFP(sc);
1742 ifp->if_timer = 0;
1743
1744 csr_write_1(sc, AUE_CTL0, 0);
1745 csr_write_1(sc, AUE_CTL1, 0);
1746 aue_reset(sc);
1747 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1748
1749 /* Stop transfers. */
1750 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1751 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1752 if (err) {
1753 printf("%s: abort rx pipe failed: %s\n",
1754 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1755 }
1756 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1757 if (err) {
1758 printf("%s: close rx pipe failed: %s\n",
1759 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1760 }
1761 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1762 }
1763
1764 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1765 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1766 if (err) {
1767 printf("%s: abort tx pipe failed: %s\n",
1768 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1769 }
1770 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1771 if (err) {
1772 printf("%s: close tx pipe failed: %s\n",
1773 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1774 }
1775 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1776 }
1777
1778 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1779 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1780 if (err) {
1781 printf("%s: abort intr pipe failed: %s\n",
1782 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1783 }
1784 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1785 if (err) {
1786 printf("%s: close intr pipe failed: %s\n",
1787 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1788 }
1789 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1790 }
1791
1792 /* Free RX resources. */
1793 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1794 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1795 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1796 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1797 }
1798 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1799 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1800 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1801 }
1802 }
1803
1804 /* Free TX resources. */
1805 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1806 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1807 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1808 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1809 }
1810 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1811 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1812 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1813 }
1814 }
1815
1816 sc->aue_link = 0;
1817
1818 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1819 }
1820
1821 #ifdef __FreeBSD__
1822 /*
1823 * Stop all chip I/O so that the kernel's probe routines don't
1824 * get confused by errant DMAs when rebooting.
1825 */
1826 static void
1827 aue_shutdown(dev)
1828 device_ptr_t dev;
1829 {
1830 struct aue_softc *sc = USBGETSOFTC(dev);
1831
1832 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1833
1834 aue_reset(sc);
1835 aue_stop(sc);
1836 }
1837 #endif
1838