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