Merge from vendor branch NTPD:
[dragonfly.git] / sys / dev / netif / aue / if_aue.c
1 /*-
2  * Copyright (c) 1997, 1998, 1999, 2000
3  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.78 2003/12/17 14:23:07 sanpei Exp $
33  * $DragonFly: src/sys/dev/netif/aue/if_aue.c,v 1.18 2005/02/21 18:40:36 joerg Exp $
34  */
35
36 /*
37  * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
38  * Datasheet is available from http://www.admtek.com.tw.
39  *
40  * Written by Bill Paul <wpaul@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 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/sockio.h>
68 #include <sys/mbuf.h>
69 #include <sys/malloc.h>
70 #include <sys/kernel.h>
71 #include <sys/socket.h>
72
73 #include <net/if.h>
74 #include <net/ifq_var.h>
75 #include <net/if_arp.h>
76 #include <net/ethernet.h>
77 #include <net/if_dl.h>
78 #include <net/if_media.h>
79
80 #include <net/bpf.h>
81
82 #include <sys/bus.h>
83 #include <machine/bus.h>
84 #if defined(DragonFly) || __FreeBSD_version < 500000
85 #include <machine/clock.h>
86 #endif
87
88 #include <bus/usb/usb.h>
89 #include <bus/usb/usbdi.h>
90 #include <bus/usb/usbdi_util.h>
91 #include <bus/usb/usbdivar.h>
92 #include <bus/usb/usbdevs.h>
93 #include <bus/usb/usb_ethersubr.h>
94
95 #include "../mii_layer/mii.h"
96 #include "../mii_layer/miivar.h"
97
98 #include "if_auereg.h"
99
100 MODULE_DEPEND(aue, usb, 1, 1, 1);
101 MODULE_DEPEND(aue, miibus, 1, 1, 1);
102
103 /* "controller miibus0" required.  See GENERIC if you get errors here. */
104 #include "miibus_if.h"
105
106 struct aue_type {
107         struct usb_devno        aue_dev;
108         u_int16_t               aue_flags;
109 #define LSYS  0x0001          /* use Linksys reset */
110 #define PNA   0x0002          /* has Home PNA */
111 #define PII   0x0004          /* Pegasus II chip */
112 };
113
114 Static const struct aue_type aue_devs[] = {
115  {{ USB_VENDOR_3COM,            USB_PRODUCT_3COM_3C460B},         PII },
116  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX1},          PNA|PII },
117  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX2},          PII },
118  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_UFE1000},      LSYS },
119  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX4},          PNA },
120  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX5},          PNA },
121  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX6},          PII },
122  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX7},          PII },
123  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX8},          PII },
124  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX9},          PNA },
125  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_XX10},         0 },
126  {{ USB_VENDOR_ABOCOM,          USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
127  {{ USB_VENDOR_ACCTON,          USB_PRODUCT_ACCTON_USB320_EC},    0 },
128  {{ USB_VENDOR_ACCTON,          USB_PRODUCT_ACCTON_SS1001},       PII },
129  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUS},      PNA },
130  {{ USB_VENDOR_ADMTEK,          USB_PRODUCT_ADMTEK_PEGASUSII},    PII },
131  {{ USB_VENDOR_BELKIN,          USB_PRODUCT_BELKIN_USB2LAN},      PII },
132  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USB100},   0 },
133  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
134  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
135  {{ USB_VENDOR_BILLIONTON,      USB_PRODUCT_BILLIONTON_USBE100},  PII },
136  {{ USB_VENDOR_COREGA,          USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
137  {{ USB_VENDOR_COREGA,          USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
138  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX4},     LSYS|PII },
139  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX1},     LSYS },
140  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX},      LSYS },
141  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX_PNA},  PNA },
142  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX3},     LSYS|PII },
143  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650TX2},     LSYS|PII },
144  {{ USB_VENDOR_DLINK,           USB_PRODUCT_DLINK_DSB650},        LSYS },
145  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX0},     0 },
146  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX1},     LSYS },
147  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX2},     0 },
148  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBTX3},     LSYS },
149  {{ USB_VENDOR_ELECOM,          USB_PRODUCT_ELECOM_LDUSBLTX},     PII },
150  {{ USB_VENDOR_ELSA,            USB_PRODUCT_ELSA_USB2ETHERNET},   0 },
151  {{ USB_VENDOR_HAWKING,         USB_PRODUCT_HAWKING_UF100},       PII },
152  {{ USB_VENDOR_HP,              USB_PRODUCT_HP_HN210E},           PII },
153  {{ USB_VENDOR_IODATA,          USB_PRODUCT_IODATA_USBETTX},      0 },
154  {{ USB_VENDOR_IODATA,          USB_PRODUCT_IODATA_USBETTXS},     PII },
155  {{ USB_VENDOR_KINGSTON,        USB_PRODUCT_KINGSTON_KNU101TX},   0 },
156  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TX1},    LSYS|PII },
157  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10T},      LSYS },
158  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB100TX},    LSYS },
159  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB100H1},    LSYS|PNA },
160  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TA},     LSYS },
161  {{ USB_VENDOR_LINKSYS,         USB_PRODUCT_LINKSYS_USB10TX2},    LSYS|PII },
162  {{ USB_VENDOR_MICROSOFT,       USB_PRODUCT_MICROSOFT_MN110},     PII },
163  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUATX1},        0 },
164  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUATX5},        0 },
165  {{ USB_VENDOR_MELCO,           USB_PRODUCT_MELCO_LUA2TX5},       PII },
166  {{ USB_VENDOR_SIEMENS,         USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
167  {{ USB_VENDOR_SMARTBRIDGES,    USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
168  {{ USB_VENDOR_SMC,             USB_PRODUCT_SMC_2202USB},         0 },
169  {{ USB_VENDOR_SMC,             USB_PRODUCT_SMC_2206USB},         PII },
170  {{ USB_VENDOR_SOHOWARE,        USB_PRODUCT_SOHOWARE_NUB100},     0 },
171 };
172 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
173
174 Static int aue_match(device_ptr_t);
175 Static int aue_attach(device_ptr_t);
176 Static int aue_detach(device_ptr_t);
177
178 Static void aue_reset_pegasus_II(struct aue_softc *sc);
179 Static int aue_tx_list_init(struct aue_softc *);
180 Static int aue_rx_list_init(struct aue_softc *);
181 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
182 Static int aue_encap(struct aue_softc *, struct mbuf *, int);
183 #ifdef AUE_INTR_PIPE
184 Static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
185 #endif
186 Static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
187 Static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
188 Static void aue_tick(void *);
189 Static void aue_rxstart(struct ifnet *);
190 Static void aue_start(struct ifnet *);
191 Static int aue_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
192 Static void aue_init(void *);
193 Static void aue_stop(struct aue_softc *);
194 Static void aue_watchdog(struct ifnet *);
195 Static void aue_shutdown(device_ptr_t);
196 Static int aue_ifmedia_upd(struct ifnet *);
197 Static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
198
199 Static void aue_eeprom_getword(struct aue_softc *, int, u_int16_t *);
200 Static void aue_read_eeprom(struct aue_softc *, caddr_t, int, int, int);
201 Static int aue_miibus_readreg(device_ptr_t, int, int);
202 Static int aue_miibus_writereg(device_ptr_t, int, int, int);
203 Static void aue_miibus_statchg(device_ptr_t);
204
205 Static void aue_setmulti(struct aue_softc *);
206 Static uint32_t aue_mchash(const uint8_t *);
207 Static void aue_reset(struct aue_softc *);
208
209 Static int aue_csr_read_1(struct aue_softc *, int);
210 Static int aue_csr_write_1(struct aue_softc *, int, int);
211 Static int aue_csr_read_2(struct aue_softc *, int);
212 Static int aue_csr_write_2(struct aue_softc *, int, int);
213
214 Static device_method_t aue_methods[] = {
215         /* Device interface */
216         DEVMETHOD(device_probe,         aue_match),
217         DEVMETHOD(device_attach,        aue_attach),
218         DEVMETHOD(device_detach,        aue_detach),
219         DEVMETHOD(device_shutdown,      aue_shutdown),
220
221         /* bus interface */
222         DEVMETHOD(bus_print_child,      bus_generic_print_child),
223         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
224
225         /* MII interface */
226         DEVMETHOD(miibus_readreg,       aue_miibus_readreg),
227         DEVMETHOD(miibus_writereg,      aue_miibus_writereg),
228         DEVMETHOD(miibus_statchg,       aue_miibus_statchg),
229
230         { 0, 0 }
231 };
232
233 Static driver_t aue_driver = {
234         "aue",
235         aue_methods,
236         sizeof(struct aue_softc)
237 };
238
239 Static devclass_t aue_devclass;
240
241 DECLARE_DUMMY_MODULE(if_aue);
242 DRIVER_MODULE(aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
243 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
244
245 #define AUE_SETBIT(sc, reg, x)                          \
246         aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
247
248 #define AUE_CLRBIT(sc, reg, x)                          \
249         aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
250
251 Static int
252 aue_csr_read_1(struct aue_softc *sc, int reg)
253 {
254         usb_device_request_t    req;
255         usbd_status             err;
256         u_int8_t                val = 0;
257
258         if (sc->aue_dying)
259                 return(0);
260
261         AUE_LOCK(sc);
262
263         req.bmRequestType = UT_READ_VENDOR_DEVICE;
264         req.bRequest = AUE_UR_READREG;
265         USETW(req.wValue, 0);
266         USETW(req.wIndex, reg);
267         USETW(req.wLength, 1);
268
269         err = usbd_do_request(sc->aue_udev, &req, &val);
270
271         AUE_UNLOCK(sc);
272
273         if (err) {
274                 return (0);
275         }
276
277         return (val);
278 }
279
280 Static int
281 aue_csr_read_2(struct aue_softc *sc, int reg)
282 {
283         usb_device_request_t    req;
284         usbd_status             err;
285         u_int16_t               val = 0;
286
287         if (sc->aue_dying)
288                 return (0);
289
290         AUE_LOCK(sc);
291
292         req.bmRequestType = UT_READ_VENDOR_DEVICE;
293         req.bRequest = AUE_UR_READREG;
294         USETW(req.wValue, 0);
295         USETW(req.wIndex, reg);
296         USETW(req.wLength, 2);
297
298         err = usbd_do_request(sc->aue_udev, &req, &val);
299
300         AUE_UNLOCK(sc);
301
302         if (err) {
303                 return (0);
304         }
305
306         return (val);
307 }
308
309 Static int
310 aue_csr_write_1(struct aue_softc *sc, int reg, int val)
311 {
312         usb_device_request_t    req;
313         usbd_status             err;
314
315         if (sc->aue_dying)
316                 return (0);
317
318         AUE_LOCK(sc);
319
320         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
321         req.bRequest = AUE_UR_WRITEREG;
322         USETW(req.wValue, val);
323         USETW(req.wIndex, reg);
324         USETW(req.wLength, 1);
325
326         err = usbd_do_request(sc->aue_udev, &req, &val);
327
328         AUE_UNLOCK(sc);
329
330         if (err) {
331                 return (-1);
332         }
333
334         return (0);
335 }
336
337 Static int
338 aue_csr_write_2(struct aue_softc *sc, int reg, int val)
339 {
340         usb_device_request_t    req;
341         usbd_status             err;
342
343         if (sc->aue_dying)
344                 return (0);
345
346         AUE_LOCK(sc);
347
348         req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
349         req.bRequest = AUE_UR_WRITEREG;
350         USETW(req.wValue, val);
351         USETW(req.wIndex, reg);
352         USETW(req.wLength, 2);
353
354         err = usbd_do_request(sc->aue_udev, &req, &val);
355
356         AUE_UNLOCK(sc);
357
358         if (err) {
359                 return (-1);
360         }
361
362         return (0);
363 }
364
365 /*
366  * Read a word of data stored in the EEPROM at address 'addr.'
367  */
368 Static void
369 aue_eeprom_getword(struct aue_softc *sc, int addr, u_int16_t *dest)
370 {
371         int             i;
372         u_int16_t       word = 0;
373
374         aue_csr_write_1(sc, AUE_EE_REG, addr);
375         aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
376
377         for (i = 0; i < AUE_TIMEOUT; i++) {
378                 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
379                         break;
380         }
381
382         if (i == AUE_TIMEOUT) {
383                 printf("aue%d: EEPROM read timed out\n",
384                     sc->aue_unit);
385         }
386
387         word = aue_csr_read_2(sc, AUE_EE_DATA);
388         *dest = word;
389
390         return;
391 }
392
393 /*
394  * Read a sequence of words from the EEPROM.
395  */
396 Static void
397 aue_read_eeprom(struct aue_softc *sc, caddr_t dest, int off, int cnt, int swap)
398 {
399         int                     i;
400         u_int16_t               word = 0, *ptr;
401
402         for (i = 0; i < cnt; i++) {
403                 aue_eeprom_getword(sc, off + i, &word);
404                 ptr = (u_int16_t *)(dest + (i * 2));
405                 if (swap)
406                         *ptr = ntohs(word);
407                 else
408                         *ptr = word;
409         }
410
411         return;
412 }
413
414 Static int
415 aue_miibus_readreg(device_ptr_t dev, int phy, int reg)
416 {
417         struct aue_softc        *sc = USBGETSOFTC(dev);
418         int                     i;
419         u_int16_t               val = 0;
420
421         /*
422          * The Am79C901 HomePNA PHY actually contains
423          * two transceivers: a 1Mbps HomePNA PHY and a
424          * 10Mbps full/half duplex ethernet PHY with
425          * NWAY autoneg. However in the ADMtek adapter,
426          * only the 1Mbps PHY is actually connected to
427          * anything, so we ignore the 10Mbps one. It
428          * happens to be configured for MII address 3,
429          * so we filter that out.
430          */
431         if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
432             sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
433                 if (phy == 3)
434                         return (0);
435 #ifdef notdef
436                 if (phy != 1)
437                         return (0);
438 #endif
439         }
440
441         aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
442         aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
443
444         for (i = 0; i < AUE_TIMEOUT; i++) {
445                 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
446                         break;
447         }
448
449         if (i == AUE_TIMEOUT) {
450                 printf("aue%d: MII read timed out\n", sc->aue_unit);
451         }
452
453         val = aue_csr_read_2(sc, AUE_PHY_DATA);
454
455         return (val);
456 }
457
458 Static int
459 aue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
460 {
461         struct aue_softc        *sc = USBGETSOFTC(dev);
462         int                     i;
463
464         if (phy == 3)
465                 return (0);
466
467         aue_csr_write_2(sc, AUE_PHY_DATA, data);
468         aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
469         aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
470
471         for (i = 0; i < AUE_TIMEOUT; i++) {
472                 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
473                         break;
474         }
475
476         if (i == AUE_TIMEOUT) {
477                 printf("aue%d: MII read timed out\n",
478                     sc->aue_unit);
479         }
480
481         return(0);
482 }
483
484 Static void
485 aue_miibus_statchg(device_ptr_t dev)
486 {
487         struct aue_softc        *sc = USBGETSOFTC(dev);
488         struct mii_data         *mii = GET_MII(sc);
489
490         AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
491         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
492                 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
493         } else {
494                 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
495         }
496
497         if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
498                 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
499         else
500                 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
501
502         AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
503
504         /*
505          * Set the LED modes on the LinkSys adapter.
506          * This turns on the 'dual link LED' bin in the auxmode
507          * register of the Broadcom PHY.
508          */
509         if (sc->aue_flags & LSYS) {
510                 u_int16_t auxmode;
511                 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
512                 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
513         }
514
515         return;
516 }
517
518 #define AUE_POLY        0xEDB88320
519 #define AUE_BITS        6
520
521 Static u_int32_t
522 aue_mchash(const uint8_t *addr)
523 {
524         uint32_t crc;
525         int idx, bit;
526         uint8_t data;
527
528         /* Compute CRC for the address value. */
529         crc = 0xFFFFFFFF; /* initial value */
530
531         for (idx = 0; idx < 6; idx++) {
532                 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
533                         crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
534         }
535
536         return (crc & ((1 << AUE_BITS) - 1));
537 }
538
539 Static void
540 aue_setmulti(struct aue_softc *sc)
541 {
542         struct ifnet            *ifp;
543         struct ifmultiaddr      *ifma;
544         u_int32_t               h = 0, i;
545
546         ifp = &sc->arpcom.ac_if;
547
548         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
549                 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
550                 return;
551         }
552
553         AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
554
555         /* first, zot all the existing hash bits */
556         for (i = 0; i < 8; i++)
557                 aue_csr_write_1(sc, AUE_MAR0 + i, 0);
558
559         /* now program new ones */
560 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
561         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
562 #else
563         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
564 #endif
565         {
566                 if (ifma->ifma_addr->sa_family != AF_LINK)
567                         continue;
568                 h = aue_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
569                 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
570         }
571
572         return;
573 }
574
575 Static void
576 aue_reset_pegasus_II(struct aue_softc *sc)
577 {
578         /* Magic constants taken from Linux driver. */
579         aue_csr_write_1(sc, AUE_REG_1D, 0);
580         aue_csr_write_1(sc, AUE_REG_7B, 2);
581 #if 0
582         if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
583                 aue_csr_write_1(sc, AUE_REG_81, 6);
584         else
585 #endif
586                 aue_csr_write_1(sc, AUE_REG_81, 2);
587 }
588
589 Static void
590 aue_reset(struct aue_softc *sc)
591 {
592         int             i;
593
594         AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
595
596         for (i = 0; i < AUE_TIMEOUT; i++) {
597                 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
598                         break;
599         }
600
601         if (i == AUE_TIMEOUT)
602                 printf("aue%d: reset failed\n", sc->aue_unit);
603
604         /*
605          * The PHY(s) attached to the Pegasus chip may be held
606          * in reset until we flip on the GPIO outputs. Make sure
607          * to set the GPIO pins high so that the PHY(s) will
608          * be enabled.
609          *
610          * Note: We force all of the GPIO pins low first, *then*
611          * enable the ones we want.
612          */
613         aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
614         aue_csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
615
616         if (sc->aue_flags & LSYS) {
617                 /* Grrr. LinkSys has to be different from everyone else. */
618                 aue_csr_write_1(sc, AUE_GPIO0,
619                     AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
620                 aue_csr_write_1(sc, AUE_GPIO0,
621                     AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
622         }
623
624         if (sc->aue_flags & PII)
625                 aue_reset_pegasus_II(sc);
626
627         /* Wait a little while for the chip to get its brains in order. */
628         DELAY(10000);
629
630         return;
631 }
632
633 /*
634  * Probe for a Pegasus chip.
635  */
636 USB_MATCH(aue)
637 {
638         USB_MATCH_START(aue, uaa);
639
640         if (uaa->iface != NULL)
641                 return (UMATCH_NONE);
642
643         return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
644                 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
645 }
646
647 /*
648  * Attach the interface. Allocate softc structures, do ifmedia
649  * setup and ethernet/BPF attach.
650  */
651 USB_ATTACH(aue)
652 {
653         USB_ATTACH_START(aue, sc, uaa);
654         char                    devinfo[1024];
655         u_char                  eaddr[ETHER_ADDR_LEN];
656         struct ifnet            *ifp;
657         usbd_interface_handle   iface;
658         usbd_status             err;
659         usb_interface_descriptor_t      *id;
660         usb_endpoint_descriptor_t       *ed;
661         int                     i;
662
663         bzero(sc, sizeof(struct aue_softc));
664
665         usbd_devinfo(uaa->device, 0, devinfo);
666
667         sc->aue_udev = uaa->device;
668         sc->aue_unit = device_get_unit(self);
669         callout_init(&sc->aue_stat_timer);
670
671         if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) {
672                 printf("aue%d: getting interface handle failed\n",
673                     sc->aue_unit);
674                 USB_ATTACH_ERROR_RETURN;
675         }
676
677         err = usbd_device2interface_handle(uaa->device, AUE_IFACE_IDX, &iface);
678         if (err) {
679                 printf("aue%d: getting interface handle failed\n",
680                     sc->aue_unit);
681                 USB_ATTACH_ERROR_RETURN;
682         }
683
684         sc->aue_iface = iface;
685         sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
686
687         sc->aue_product = uaa->product;
688         sc->aue_vendor = uaa->vendor;
689
690         id = usbd_get_interface_descriptor(sc->aue_iface);
691
692         usbd_devinfo(uaa->device, 0, devinfo);
693         device_set_desc_copy(self, devinfo);
694         printf("%s: %s\n", USBDEVNAME(self), devinfo);
695
696         /* Find endpoints. */
697         for (i = 0; i < id->bNumEndpoints; i++) {
698                 ed = usbd_interface2endpoint_descriptor(iface, i);
699                 if (ed == NULL) {
700                         printf("aue%d: couldn't get ep %d\n",
701                             sc->aue_unit, i);
702                         USB_ATTACH_ERROR_RETURN;
703                 }
704                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
705                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
706                         sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
707                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
708                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
709                         sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
710                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
711                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
712                         sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
713                 }
714         }
715
716 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
717         mtx_init(&sc->aue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
718             MTX_DEF | MTX_RECURSE);
719 #endif
720         AUE_LOCK(sc);
721
722         /* Reset the adapter. */
723         aue_reset(sc);
724
725         /*
726          * Get station address from the EEPROM.
727          */
728         aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
729
730         ifp = &sc->arpcom.ac_if;
731         ifp->if_softc = sc;
732         if_initname(ifp, "aue", sc->aue_unit);
733         ifp->if_mtu = ETHERMTU;
734         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
735         ifp->if_ioctl = aue_ioctl;
736         ifp->if_start = aue_start;
737         ifp->if_watchdog = aue_watchdog;
738         ifp->if_init = aue_init;
739         ifp->if_baudrate = 10000000;
740         ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
741         ifq_set_ready(&ifp->if_snd);
742
743         /*
744          * Do MII setup.
745          * NOTE: Doing this causes child devices to be attached to us,
746          * which we would normally disconnect at in the detach routine
747          * using device_delete_child(). However the USB code is set up
748          * such that when this driver is removed, all children devices
749          * are removed as well. In effect, the USB code ends up detaching
750          * all of our children for us, so we don't have to do is ourselves
751          * in aue_detach(). It's important to point this out since if
752          * we *do* try to detach the child devices ourselves, we will
753          * end up getting the children deleted twice, which will crash
754          * the system.
755          */
756         if (mii_phy_probe(self, &sc->aue_miibus,
757             aue_ifmedia_upd, aue_ifmedia_sts)) {
758                 printf("aue%d: MII without any PHY!\n", sc->aue_unit);
759                 AUE_UNLOCK(sc);
760 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
761                 mtx_destroy(&sc->aue_mtx);
762 #endif
763                 USB_ATTACH_ERROR_RETURN;
764         }
765
766         /*
767          * Call MI attach routine.
768          */
769         ether_ifattach(ifp, eaddr);
770         usb_register_netisr();
771         sc->aue_dying = 0;
772
773         AUE_UNLOCK(sc);
774         USB_ATTACH_SUCCESS_RETURN;
775 }
776
777 Static int
778 aue_detach(device_ptr_t dev)
779 {
780         struct aue_softc        *sc;
781         struct ifnet            *ifp;
782
783         sc = device_get_softc(dev);
784         AUE_LOCK(sc);
785         ifp = &sc->arpcom.ac_if;
786
787         sc->aue_dying = 1;
788         callout_stop(&sc->aue_stat_timer);
789         ether_ifdetach(ifp);
790
791         if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
792                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
793         if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
794                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
795 #ifdef AUE_INTR_PIPE
796         if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
797                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
798 #endif
799
800         AUE_UNLOCK(sc);
801 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
802         mtx_destroy(&sc->aue_mtx);
803 #endif
804
805         return (0);
806 }
807
808 /*
809  * Initialize an RX descriptor and attach an MBUF cluster.
810  */
811 Static int
812 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
813 {
814         struct mbuf             *m_new = NULL;
815
816         if (m == NULL) {
817                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
818                 if (m_new == NULL) {
819                         printf("aue%d: no memory for rx list "
820                             "-- packet dropped!\n", sc->aue_unit);
821                         return (ENOBUFS);
822                 }
823
824                 MCLGET(m_new, MB_DONTWAIT);
825                 if (!(m_new->m_flags & M_EXT)) {
826                         printf("aue%d: no memory for rx list "
827                             "-- packet dropped!\n", sc->aue_unit);
828                         m_freem(m_new);
829                         return (ENOBUFS);
830                 }
831                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
832         } else {
833                 m_new = m;
834                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
835                 m_new->m_data = m_new->m_ext.ext_buf;
836         }
837
838         m_adj(m_new, ETHER_ALIGN);
839         c->aue_mbuf = m_new;
840
841         return (0);
842 }
843
844 Static int
845 aue_rx_list_init(struct aue_softc *sc)
846 {
847         struct aue_cdata        *cd;
848         struct aue_chain        *c;
849         int                     i;
850
851         cd = &sc->aue_cdata;
852         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
853                 c = &cd->aue_rx_chain[i];
854                 c->aue_sc = sc;
855                 c->aue_idx = i;
856                 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
857                         return (ENOBUFS);
858                 if (c->aue_xfer == NULL) {
859                         c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
860                         if (c->aue_xfer == NULL)
861                                 return (ENOBUFS);
862                 }
863         }
864
865         return (0);
866 }
867
868 Static int
869 aue_tx_list_init(struct aue_softc *sc)
870 {
871         struct aue_cdata        *cd;
872         struct aue_chain        *c;
873         int                     i;
874
875         cd = &sc->aue_cdata;
876         for (i = 0; i < AUE_TX_LIST_CNT; i++) {
877                 c = &cd->aue_tx_chain[i];
878                 c->aue_sc = sc;
879                 c->aue_idx = i;
880                 c->aue_mbuf = NULL;
881                 if (c->aue_xfer == NULL) {
882                         c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
883                         if (c->aue_xfer == NULL)
884                                 return (ENOBUFS);
885                 }
886                 c->aue_buf = malloc(AUE_BUFSZ, M_USBDEV, M_WAITOK);
887                 if (c->aue_buf == NULL)
888                         return (ENOBUFS);
889         }
890
891         return (0);
892 }
893
894 #ifdef AUE_INTR_PIPE
895 Static void
896 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
897 {
898         struct aue_softc        *sc = priv;
899         struct ifnet            *ifp;
900         struct aue_intrpkt      *p;
901
902         AUE_LOCK(sc);
903         ifp = &sc->arpcom.ac_if;
904
905         if (!(ifp->if_flags & IFF_RUNNING)) {
906                 AUE_UNLOCK(sc);
907                 return;
908         }
909
910         if (status != USBD_NORMAL_COMPLETION) {
911                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
912                         AUE_UNLOCK(sc);
913                         return;
914                 }
915                 printf("aue%d: usb error on intr: %s\n", sc->aue_unit,
916                     usbd_errstr(status));
917                 if (status == USBD_STALLED)
918                         usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
919                 AUE_UNLOCK(sc);
920                 return;
921         }
922
923         usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
924
925         if (p->aue_txstat0)
926                 ifp->if_oerrors++;
927
928         if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL & AUE_TXSTAT0_EXCESSCOLL))
929                 ifp->if_collisions++;
930
931         AUE_UNLOCK(sc);
932         return;
933 }
934 #endif
935
936 Static void
937 aue_rxstart(struct ifnet *ifp)
938 {
939         struct aue_softc        *sc;
940         struct aue_chain        *c;
941
942         sc = ifp->if_softc;
943         AUE_LOCK(sc);
944         c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
945
946         if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
947                 ifp->if_ierrors++;
948                 AUE_UNLOCK(sc);
949                 return;
950         }
951
952         /* Setup new transfer. */
953         usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
954             c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
955             USBD_NO_TIMEOUT, aue_rxeof);
956         usbd_transfer(c->aue_xfer);
957
958         AUE_UNLOCK(sc);
959         return;
960 }
961
962 /*
963  * A frame has been uploaded: pass the resulting mbuf chain up to
964  * the higher level protocols.
965  */
966 Static void
967 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
968 {
969         struct aue_chain        *c = priv;
970         struct aue_softc        *sc = c->aue_sc;
971         struct mbuf             *m;
972         struct ifnet            *ifp;
973         int                     total_len = 0;
974         struct aue_rxpkt        r;
975
976         if (sc->aue_dying)
977                 return;
978         AUE_LOCK(sc);
979         ifp = &sc->arpcom.ac_if;
980
981         if (!(ifp->if_flags & IFF_RUNNING)) {
982                 AUE_UNLOCK(sc);
983                 return;
984         }
985
986         if (status != USBD_NORMAL_COMPLETION) {
987                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
988                         AUE_UNLOCK(sc);
989                         return;
990                 }
991                 if (usbd_ratecheck(&sc->aue_rx_notice))
992                         printf("aue%d: usb error on rx: %s\n", sc->aue_unit,
993                             usbd_errstr(status));
994                 if (status == USBD_STALLED)
995                         usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
996                 goto done;
997         }
998
999         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1000
1001         if (total_len <= 4 + ETHER_CRC_LEN) {
1002                 ifp->if_ierrors++;
1003                 goto done;
1004         }
1005
1006         m = c->aue_mbuf;
1007         bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r));
1008
1009         /* Turn off all the non-error bits in the rx status word. */
1010         r.aue_rxstat &= AUE_RXSTAT_MASK;
1011
1012         if (r.aue_rxstat) {
1013                 ifp->if_ierrors++;
1014                 goto done;
1015         }
1016
1017         /* No errors; receive the packet. */
1018         total_len -= (4 + ETHER_CRC_LEN);
1019
1020         ifp->if_ipackets++;
1021         m->m_pkthdr.rcvif = ifp;
1022         m->m_pkthdr.len = m->m_len = total_len;
1023
1024         /* Put the packet on the special USB input queue. */
1025         usb_ether_input(m);
1026         aue_start(ifp);
1027         AUE_UNLOCK(sc);
1028         return;
1029 done:
1030
1031         /* Setup new transfer. */
1032         usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1033             c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
1034             USBD_NO_TIMEOUT, aue_rxeof);
1035         usbd_transfer(xfer);
1036
1037         AUE_UNLOCK(sc);
1038         return;
1039 }
1040
1041 /*
1042  * A frame was downloaded to the chip. It's safe for us to clean up
1043  * the list buffers.
1044  */
1045
1046 Static void
1047 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1048 {
1049         struct aue_chain        *c = priv;
1050         struct aue_softc        *sc = c->aue_sc;
1051         struct ifnet            *ifp;
1052         usbd_status             err;
1053
1054         AUE_LOCK(sc);
1055         ifp = &sc->arpcom.ac_if;
1056
1057         if (status != USBD_NORMAL_COMPLETION) {
1058                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1059                         AUE_UNLOCK(sc);
1060                         return;
1061                 }
1062                 printf("aue%d: usb error on tx: %s\n", sc->aue_unit,
1063                     usbd_errstr(status));
1064                 if (status == USBD_STALLED)
1065                         usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1066                 AUE_UNLOCK(sc);
1067                 return;
1068         }
1069
1070         ifp->if_timer = 0;
1071         ifp->if_flags &= ~IFF_OACTIVE;
1072         usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &err);
1073
1074         if (c->aue_mbuf != NULL) {
1075                 m_free(c->aue_mbuf);
1076                 c->aue_mbuf = NULL;
1077         }
1078
1079         if (err)
1080                 ifp->if_oerrors++;
1081         else
1082                 ifp->if_opackets++;
1083
1084         if (!ifq_is_empty(&ifp->if_snd))
1085                 (*ifp->if_start)(ifp);
1086
1087         AUE_UNLOCK(sc);
1088
1089         return;
1090 }
1091
1092 Static void
1093 aue_tick(void *xsc)
1094 {
1095         struct aue_softc        *sc = xsc;
1096         struct ifnet            *ifp;
1097         struct mii_data         *mii;
1098
1099         if (sc == NULL)
1100                 return;
1101
1102         AUE_LOCK(sc);
1103
1104         ifp = &sc->arpcom.ac_if;
1105         mii = GET_MII(sc);
1106         if (mii == NULL) {
1107                 AUE_UNLOCK(sc);
1108                 return;
1109         }
1110
1111         mii_tick(mii);
1112         if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1113             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1114                 sc->aue_link++;
1115                 if (!ifq_is_empty(&ifp->if_snd))
1116                         aue_start(ifp);
1117         }
1118
1119         callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1120
1121         AUE_UNLOCK(sc);
1122
1123         return;
1124 }
1125
1126 Static int
1127 aue_encap(struct aue_softc *sc, struct mbuf *m, int idx)
1128 {
1129         int                     total_len;
1130         struct aue_chain        *c;
1131         usbd_status             err;
1132
1133         c = &sc->aue_cdata.aue_tx_chain[idx];
1134
1135         /*
1136          * Copy the mbuf data into a contiguous buffer, leaving two
1137          * bytes at the beginning to hold the frame length.
1138          */
1139         m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1140         c->aue_mbuf = m;
1141
1142         total_len = m->m_pkthdr.len + 2;
1143
1144         /*
1145          * The ADMtek documentation says that the packet length is
1146          * supposed to be specified in the first two bytes of the
1147          * transfer, however it actually seems to ignore this info
1148          * and base the frame size on the bulk transfer length.
1149          */
1150         c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1151         c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1152
1153         usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1154             c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER,
1155             10000, aue_txeof);
1156
1157         /* Transmit */
1158         err = usbd_transfer(c->aue_xfer);
1159         if (err != USBD_IN_PROGRESS) {
1160                 aue_stop(sc);
1161                 return (EIO);
1162         }
1163
1164         sc->aue_cdata.aue_tx_cnt++;
1165
1166         return (0);
1167 }
1168
1169 Static void
1170 aue_start(struct ifnet *ifp)
1171 {
1172         struct aue_softc        *sc = ifp->if_softc;
1173         struct mbuf             *m_head = NULL;
1174
1175         AUE_LOCK(sc);
1176
1177         if (!sc->aue_link) {
1178                 AUE_UNLOCK(sc);
1179                 return;
1180         }
1181
1182         if (ifp->if_flags & IFF_OACTIVE) {
1183                 AUE_UNLOCK(sc);
1184                 return;
1185         }
1186
1187         m_head = ifq_poll(&ifp->if_snd);
1188         if (m_head == NULL) {
1189                 AUE_UNLOCK(sc);
1190                 return;
1191         }
1192
1193         if (aue_encap(sc, m_head, 0)) {
1194                 ifp->if_flags |= IFF_OACTIVE;
1195                 AUE_UNLOCK(sc);
1196                 return;
1197         }
1198         m_head = ifq_dequeue(&ifp->if_snd);
1199
1200         /*
1201          * If there's a BPF listener, bounce a copy of this frame
1202          * to him.
1203          */
1204         BPF_MTAP(ifp, m_head);
1205
1206         ifp->if_flags |= IFF_OACTIVE;
1207
1208         /*
1209          * Set a timeout in case the chip goes out to lunch.
1210          */
1211         ifp->if_timer = 5;
1212         AUE_UNLOCK(sc);
1213
1214         return;
1215 }
1216
1217 Static void
1218 aue_init(void *xsc)
1219 {
1220         struct aue_softc        *sc = xsc;
1221         struct ifnet            *ifp = &sc->arpcom.ac_if;
1222         struct mii_data         *mii = GET_MII(sc);
1223         struct aue_chain        *c;
1224         usbd_status             err;
1225         int                     i;
1226
1227         AUE_LOCK(sc);
1228
1229         if (ifp->if_flags & IFF_RUNNING) {
1230                 AUE_UNLOCK(sc);
1231                 return;
1232         }
1233
1234         /*
1235          * Cancel pending I/O and free all RX/TX buffers.
1236          */
1237         aue_reset(sc);
1238
1239         /* Set MAC address */
1240         for (i = 0; i < ETHER_ADDR_LEN; i++)
1241                 aue_csr_write_1(sc, AUE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1242
1243          /* If we want promiscuous mode, set the allframes bit. */
1244         if (ifp->if_flags & IFF_PROMISC)
1245                 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1246         else
1247                 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1248
1249         /* Init TX ring. */
1250         if (aue_tx_list_init(sc) == ENOBUFS) {
1251                 printf("aue%d: tx list init failed\n", sc->aue_unit);
1252                 AUE_UNLOCK(sc);
1253                 return;
1254         }
1255
1256         /* Init RX ring. */
1257         if (aue_rx_list_init(sc) == ENOBUFS) {
1258                 printf("aue%d: rx list init failed\n", sc->aue_unit);
1259                 AUE_UNLOCK(sc);
1260                 return;
1261         }
1262
1263 #ifdef AUE_INTR_PIPE
1264         sc->aue_cdata.aue_ibuf = malloc(AUE_INTR_PKTLEN, M_USBDEV, M_WAITOK);
1265 #endif
1266
1267         /* Load the multicast filter. */
1268         aue_setmulti(sc);
1269
1270         /* Enable RX and TX */
1271         aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1272         AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1273         AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1274
1275         mii_mediachg(mii);
1276
1277         /* Open RX and TX pipes. */
1278         err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1279             USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1280         if (err) {
1281                 printf("aue%d: open rx pipe failed: %s\n",
1282                     sc->aue_unit, usbd_errstr(err));
1283                 AUE_UNLOCK(sc);
1284                 return;
1285         }
1286         err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1287             USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1288         if (err) {
1289                 printf("aue%d: open tx pipe failed: %s\n",
1290                     sc->aue_unit, usbd_errstr(err));
1291                 AUE_UNLOCK(sc);
1292                 return;
1293         }
1294
1295 #ifdef AUE_INTR_PIPE
1296         err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1297             USBD_SHORT_XFER_OK, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1298             sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1299             AUE_INTR_INTERVAL);
1300         if (err) {
1301                 printf("aue%d: open intr pipe failed: %s\n",
1302                     sc->aue_unit, usbd_errstr(err));
1303                 AUE_UNLOCK(sc);
1304                 return;
1305         }
1306 #endif
1307
1308         /* Start up the receive pipe. */
1309         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1310                 c = &sc->aue_cdata.aue_rx_chain[i];
1311                 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1312                     c, mtod(c->aue_mbuf, char *), AUE_BUFSZ,
1313                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1314                 usbd_transfer(c->aue_xfer);
1315         }
1316
1317         ifp->if_flags |= IFF_RUNNING;
1318         ifp->if_flags &= ~IFF_OACTIVE;
1319
1320         callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1321
1322         AUE_UNLOCK(sc);
1323
1324         return;
1325 }
1326
1327 /*
1328  * Set media options.
1329  */
1330 Static int
1331 aue_ifmedia_upd(struct ifnet *ifp)
1332 {
1333         struct aue_softc        *sc = ifp->if_softc;
1334         struct mii_data         *mii = GET_MII(sc);
1335
1336         sc->aue_link = 0;
1337         if (mii->mii_instance) {
1338                 struct mii_softc        *miisc;
1339                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1340                          mii_phy_reset(miisc);
1341         }
1342         mii_mediachg(mii);
1343
1344         return (0);
1345 }
1346
1347 /*
1348  * Report current media status.
1349  */
1350 Static void
1351 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1352 {
1353         struct aue_softc        *sc = ifp->if_softc;
1354         struct mii_data         *mii = GET_MII(sc);
1355
1356         mii_pollstat(mii);
1357         ifmr->ifm_active = mii->mii_media_active;
1358         ifmr->ifm_status = mii->mii_media_status;
1359
1360         return;
1361 }
1362
1363 Static int
1364 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1365 {
1366         struct aue_softc        *sc = ifp->if_softc;
1367         struct ifreq            *ifr = (struct ifreq *)data;
1368         struct mii_data         *mii;
1369         int                     error = 0;
1370
1371         AUE_LOCK(sc);
1372
1373         switch(command) {
1374         case SIOCSIFFLAGS:
1375                 if (ifp->if_flags & IFF_UP) {
1376                         if (ifp->if_flags & IFF_RUNNING &&
1377                             ifp->if_flags & IFF_PROMISC &&
1378                             !(sc->aue_if_flags & IFF_PROMISC)) {
1379                                 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1380                         } else if (ifp->if_flags & IFF_RUNNING &&
1381                             !(ifp->if_flags & IFF_PROMISC) &&
1382                             sc->aue_if_flags & IFF_PROMISC) {
1383                                 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1384                         } else if (!(ifp->if_flags & IFF_RUNNING))
1385                                 aue_init(sc);
1386                 } else {
1387                         if (ifp->if_flags & IFF_RUNNING)
1388                                 aue_stop(sc);
1389                 }
1390                 sc->aue_if_flags = ifp->if_flags;
1391                 error = 0;
1392                 break;
1393         case SIOCADDMULTI:
1394         case SIOCDELMULTI:
1395                 aue_setmulti(sc);
1396                 error = 0;
1397                 break;
1398         case SIOCGIFMEDIA:
1399         case SIOCSIFMEDIA:
1400                 mii = GET_MII(sc);
1401                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1402                 break;
1403         default:
1404                 error = ether_ioctl(ifp, command, data);
1405                 break;
1406         }
1407
1408         AUE_UNLOCK(sc);
1409
1410         return (error);
1411 }
1412
1413 Static void
1414 aue_watchdog(struct ifnet *ifp)
1415 {
1416         struct aue_softc        *sc = ifp->if_softc;
1417         struct aue_chain        *c;
1418         usbd_status             stat;
1419
1420         AUE_LOCK(sc);
1421
1422         ifp->if_oerrors++;
1423         printf("aue%d: watchdog timeout\n", sc->aue_unit);
1424
1425         c = &sc->aue_cdata.aue_tx_chain[0];
1426         usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1427         aue_txeof(c->aue_xfer, c, stat);
1428
1429         if (!ifq_is_empty(&ifp->if_snd))
1430                 aue_start(ifp);
1431         AUE_UNLOCK(sc);
1432         return;
1433 }
1434
1435 /*
1436  * Stop the adapter and free any mbufs allocated to the
1437  * RX and TX lists.
1438  */
1439 Static void
1440 aue_stop(struct aue_softc *sc)
1441 {
1442         usbd_status             err;
1443         struct ifnet            *ifp;
1444         int                     i;
1445
1446         AUE_LOCK(sc);
1447         ifp = &sc->arpcom.ac_if;
1448         ifp->if_timer = 0;
1449
1450         aue_csr_write_1(sc, AUE_CTL0, 0);
1451         aue_csr_write_1(sc, AUE_CTL1, 0);
1452         aue_reset(sc);
1453         callout_stop(&sc->aue_stat_timer);
1454
1455         /* Stop transfers. */
1456         if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1457                 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1458                 if (err) {
1459                         printf("aue%d: abort rx pipe failed: %s\n",
1460                         sc->aue_unit, usbd_errstr(err));
1461                 }
1462                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1463                 if (err) {
1464                         printf("aue%d: close rx pipe failed: %s\n",
1465                         sc->aue_unit, usbd_errstr(err));
1466                 }
1467                 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1468         }
1469
1470         if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1471                 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1472                 if (err) {
1473                         printf("aue%d: abort tx pipe failed: %s\n",
1474                         sc->aue_unit, usbd_errstr(err));
1475                 }
1476                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1477                 if (err) {
1478                         printf("aue%d: close tx pipe failed: %s\n",
1479                             sc->aue_unit, usbd_errstr(err));
1480                 }
1481                 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1482         }
1483
1484 #ifdef AUE_INTR_PIPE
1485         if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1486                 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1487                 if (err) {
1488                         printf("aue%d: abort intr pipe failed: %s\n",
1489                         sc->aue_unit, usbd_errstr(err));
1490                 }
1491                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1492                 if (err) {
1493                         printf("aue%d: close intr pipe failed: %s\n",
1494                             sc->aue_unit, usbd_errstr(err));
1495                 }
1496                 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1497         }
1498 #endif
1499
1500         /* Free RX resources. */
1501         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1502                 if (sc->aue_cdata.aue_rx_chain[i].aue_buf != NULL) {
1503                         free(sc->aue_cdata.aue_rx_chain[i].aue_buf, M_USBDEV);
1504                         sc->aue_cdata.aue_rx_chain[i].aue_buf = NULL;
1505                 }
1506                 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1507                         m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1508                         sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1509                 }
1510                 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1511                         usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1512                         sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1513                 }
1514         }
1515
1516         /* Free TX resources. */
1517         for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1518                 if (sc->aue_cdata.aue_tx_chain[i].aue_buf != NULL) {
1519                         free(sc->aue_cdata.aue_tx_chain[i].aue_buf, M_USBDEV);
1520                         sc->aue_cdata.aue_tx_chain[i].aue_buf = NULL;
1521                 }
1522                 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1523                         m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1524                         sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1525                 }
1526                 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1527                         usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1528                         sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1529                 }
1530         }
1531
1532 #ifdef AUE_INTR_PIPE
1533         free(sc->aue_cdata.aue_ibuf, M_USBDEV);
1534         sc->aue_cdata.aue_ibuf = NULL;
1535 #endif
1536
1537         sc->aue_link = 0;
1538
1539         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1540         AUE_UNLOCK(sc);
1541
1542         return;
1543 }
1544
1545 /*
1546  * Stop all chip I/O so that the kernel's probe routines don't
1547  * get confused by errant DMAs when rebooting.
1548  */
1549 Static void
1550 aue_shutdown(device_ptr_t dev)
1551 {
1552         struct aue_softc        *sc;
1553
1554         sc = device_get_softc(dev);
1555         sc->aue_dying++;
1556         AUE_LOCK(sc);
1557         aue_reset(sc);
1558         aue_stop(sc);
1559         AUE_UNLOCK(sc);
1560
1561         return;
1562 }