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