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