454319acc0651291f99d21df5e08fd701faa67cd
[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.14 2004/09/14 21:48:53 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, struct ucred *);
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         callout_init(&sc->aue_stat_timer);
674
675         if (usbd_set_config_no(sc->aue_udev, AUE_CONFIG_NO, 0)) {
676                 printf("aue%d: getting interface handle failed\n",
677                     sc->aue_unit);
678                 USB_ATTACH_ERROR_RETURN;
679         }
680
681         err = usbd_device2interface_handle(uaa->device, AUE_IFACE_IDX, &iface);
682         if (err) {
683                 printf("aue%d: getting interface handle failed\n",
684                     sc->aue_unit);
685                 USB_ATTACH_ERROR_RETURN;
686         }
687
688         sc->aue_iface = iface;
689         sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
690
691         sc->aue_product = uaa->product;
692         sc->aue_vendor = uaa->vendor;
693
694         id = usbd_get_interface_descriptor(sc->aue_iface);
695
696         usbd_devinfo(uaa->device, 0, devinfo);
697         device_set_desc_copy(self, devinfo);
698         printf("%s: %s\n", USBDEVNAME(self), devinfo);
699
700         /* Find endpoints. */
701         for (i = 0; i < id->bNumEndpoints; i++) {
702                 ed = usbd_interface2endpoint_descriptor(iface, i);
703                 if (ed == NULL) {
704                         printf("aue%d: couldn't get ep %d\n",
705                             sc->aue_unit, i);
706                         USB_ATTACH_ERROR_RETURN;
707                 }
708                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
709                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
710                         sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
711                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
712                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
713                         sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
714                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
715                            UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
716                         sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
717                 }
718         }
719
720 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
721         mtx_init(&sc->aue_mtx, device_get_nameunit(self), MTX_NETWORK_LOCK,
722             MTX_DEF | MTX_RECURSE);
723 #endif
724         AUE_LOCK(sc);
725
726         /* Reset the adapter. */
727         aue_reset(sc);
728
729         /*
730          * Get station address from the EEPROM.
731          */
732         aue_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
733
734         ifp = &sc->arpcom.ac_if;
735         ifp->if_softc = sc;
736         if_initname(ifp, "aue", sc->aue_unit);
737         ifp->if_mtu = ETHERMTU;
738         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
739         ifp->if_ioctl = aue_ioctl;
740         ifp->if_start = aue_start;
741         ifp->if_watchdog = aue_watchdog;
742         ifp->if_init = aue_init;
743         ifp->if_baudrate = 10000000;
744         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
745
746         /*
747          * Do MII setup.
748          * NOTE: Doing this causes child devices to be attached to us,
749          * which we would normally disconnect at in the detach routine
750          * using device_delete_child(). However the USB code is set up
751          * such that when this driver is removed, all children devices
752          * are removed as well. In effect, the USB code ends up detaching
753          * all of our children for us, so we don't have to do is ourselves
754          * in aue_detach(). It's important to point this out since if
755          * we *do* try to detach the child devices ourselves, we will
756          * end up getting the children deleted twice, which will crash
757          * the system.
758          */
759         if (mii_phy_probe(self, &sc->aue_miibus,
760             aue_ifmedia_upd, aue_ifmedia_sts)) {
761                 printf("aue%d: MII without any PHY!\n", sc->aue_unit);
762                 AUE_UNLOCK(sc);
763 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
764                 mtx_destroy(&sc->aue_mtx);
765 #endif
766                 USB_ATTACH_ERROR_RETURN;
767         }
768
769         aue_qdat.ifp = ifp;
770         aue_qdat.if_rxstart = aue_rxstart;
771
772         /*
773          * Call MI attach routine.
774          */
775         ether_ifattach(ifp, eaddr);
776         usb_register_netisr();
777         sc->aue_dying = 0;
778
779         AUE_UNLOCK(sc);
780         USB_ATTACH_SUCCESS_RETURN;
781 }
782
783 Static int
784 aue_detach(device_ptr_t dev)
785 {
786         struct aue_softc        *sc;
787         struct ifnet            *ifp;
788
789         sc = device_get_softc(dev);
790         AUE_LOCK(sc);
791         ifp = &sc->arpcom.ac_if;
792
793         sc->aue_dying = 1;
794         callout_stop(&sc->aue_stat_timer);
795         ether_ifdetach(ifp);
796
797         if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
798                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
799         if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
800                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
801 #ifdef AUE_INTR_PIPE
802         if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
803                 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
804 #endif
805
806         AUE_UNLOCK(sc);
807 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
808         mtx_destroy(&sc->aue_mtx);
809 #endif
810
811         return (0);
812 }
813
814 /*
815  * Initialize an RX descriptor and attach an MBUF cluster.
816  */
817 Static int
818 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
819 {
820         struct mbuf             *m_new = NULL;
821
822         if (m == NULL) {
823                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
824                 if (m_new == NULL) {
825                         printf("aue%d: no memory for rx list "
826                             "-- packet dropped!\n", sc->aue_unit);
827                         return (ENOBUFS);
828                 }
829
830                 MCLGET(m_new, MB_DONTWAIT);
831                 if (!(m_new->m_flags & M_EXT)) {
832                         printf("aue%d: no memory for rx list "
833                             "-- packet dropped!\n", sc->aue_unit);
834                         m_freem(m_new);
835                         return (ENOBUFS);
836                 }
837                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
838         } else {
839                 m_new = m;
840                 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
841                 m_new->m_data = m_new->m_ext.ext_buf;
842         }
843
844         m_adj(m_new, ETHER_ALIGN);
845         c->aue_mbuf = m_new;
846
847         return (0);
848 }
849
850 Static int
851 aue_rx_list_init(struct aue_softc *sc)
852 {
853         struct aue_cdata        *cd;
854         struct aue_chain        *c;
855         int                     i;
856
857         cd = &sc->aue_cdata;
858         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
859                 c = &cd->aue_rx_chain[i];
860                 c->aue_sc = sc;
861                 c->aue_idx = i;
862                 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
863                         return (ENOBUFS);
864                 if (c->aue_xfer == NULL) {
865                         c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
866                         if (c->aue_xfer == NULL)
867                                 return (ENOBUFS);
868                 }
869         }
870
871         return (0);
872 }
873
874 Static int
875 aue_tx_list_init(struct aue_softc *sc)
876 {
877         struct aue_cdata        *cd;
878         struct aue_chain        *c;
879         int                     i;
880
881         cd = &sc->aue_cdata;
882         for (i = 0; i < AUE_TX_LIST_CNT; i++) {
883                 c = &cd->aue_tx_chain[i];
884                 c->aue_sc = sc;
885                 c->aue_idx = i;
886                 c->aue_mbuf = NULL;
887                 if (c->aue_xfer == NULL) {
888                         c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
889                         if (c->aue_xfer == NULL)
890                                 return (ENOBUFS);
891                 }
892                 c->aue_buf = malloc(AUE_BUFSZ, M_USBDEV, M_WAITOK);
893                 if (c->aue_buf == NULL)
894                         return (ENOBUFS);
895         }
896
897         return (0);
898 }
899
900 #ifdef AUE_INTR_PIPE
901 Static void
902 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
903 {
904         struct aue_softc        *sc = priv;
905         struct ifnet            *ifp;
906         struct aue_intrpkt      *p;
907
908         AUE_LOCK(sc);
909         ifp = &sc->arpcom.ac_if;
910
911         if (!(ifp->if_flags & IFF_RUNNING)) {
912                 AUE_UNLOCK(sc);
913                 return;
914         }
915
916         if (status != USBD_NORMAL_COMPLETION) {
917                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
918                         AUE_UNLOCK(sc);
919                         return;
920                 }
921                 printf("aue%d: usb error on intr: %s\n", sc->aue_unit,
922                     usbd_errstr(status));
923                 if (status == USBD_STALLED)
924                         usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
925                 AUE_UNLOCK(sc);
926                 return;
927         }
928
929         usbd_get_xfer_status(xfer, NULL, (void **)&p, NULL, NULL);
930
931         if (p->aue_txstat0)
932                 ifp->if_oerrors++;
933
934         if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL & AUE_TXSTAT0_EXCESSCOLL))
935                 ifp->if_collisions++;
936
937         AUE_UNLOCK(sc);
938         return;
939 }
940 #endif
941
942 Static void
943 aue_rxstart(struct ifnet *ifp)
944 {
945         struct aue_softc        *sc;
946         struct aue_chain        *c;
947
948         sc = ifp->if_softc;
949         AUE_LOCK(sc);
950         c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
951
952         if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
953                 ifp->if_ierrors++;
954                 AUE_UNLOCK(sc);
955                 return;
956         }
957
958         /* Setup new transfer. */
959         usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
960             c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
961             USBD_NO_TIMEOUT, aue_rxeof);
962         usbd_transfer(c->aue_xfer);
963
964         AUE_UNLOCK(sc);
965         return;
966 }
967
968 /*
969  * A frame has been uploaded: pass the resulting mbuf chain up to
970  * the higher level protocols.
971  */
972 Static void
973 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
974 {
975         struct aue_chain        *c = priv;
976         struct aue_softc        *sc = c->aue_sc;
977         struct mbuf             *m;
978         struct ifnet            *ifp;
979         int                     total_len = 0;
980         struct aue_rxpkt        r;
981
982         if (sc->aue_dying)
983                 return;
984         AUE_LOCK(sc);
985         ifp = &sc->arpcom.ac_if;
986
987         if (!(ifp->if_flags & IFF_RUNNING)) {
988                 AUE_UNLOCK(sc);
989                 return;
990         }
991
992         if (status != USBD_NORMAL_COMPLETION) {
993                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
994                         AUE_UNLOCK(sc);
995                         return;
996                 }
997                 if (usbd_ratecheck(&sc->aue_rx_notice))
998                         printf("aue%d: usb error on rx: %s\n", sc->aue_unit,
999                             usbd_errstr(status));
1000                 if (status == USBD_STALLED)
1001                         usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1002                 goto done;
1003         }
1004
1005         usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1006
1007         if (total_len <= 4 + ETHER_CRC_LEN) {
1008                 ifp->if_ierrors++;
1009                 goto done;
1010         }
1011
1012         m = c->aue_mbuf;
1013         bcopy(mtod(m, char *) + total_len - 4, (char *)&r, sizeof(r));
1014
1015         /* Turn off all the non-error bits in the rx status word. */
1016         r.aue_rxstat &= AUE_RXSTAT_MASK;
1017
1018         if (r.aue_rxstat) {
1019                 ifp->if_ierrors++;
1020                 goto done;
1021         }
1022
1023         /* No errors; receive the packet. */
1024         total_len -= (4 + ETHER_CRC_LEN);
1025
1026         ifp->if_ipackets++;
1027         m->m_pkthdr.rcvif = (struct ifnet *)&aue_qdat;
1028         m->m_pkthdr.len = m->m_len = total_len;
1029
1030         /* Put the packet on the special USB input queue. */
1031         usb_ether_input(m);
1032         AUE_UNLOCK(sc);
1033         return;
1034 done:
1035
1036         /* Setup new transfer. */
1037         usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1038             c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
1039             USBD_NO_TIMEOUT, aue_rxeof);
1040         usbd_transfer(xfer);
1041
1042         AUE_UNLOCK(sc);
1043         return;
1044 }
1045
1046 /*
1047  * A frame was downloaded to the chip. It's safe for us to clean up
1048  * the list buffers.
1049  */
1050
1051 Static void
1052 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1053 {
1054         struct aue_chain        *c = priv;
1055         struct aue_softc        *sc = c->aue_sc;
1056         struct ifnet            *ifp;
1057         usbd_status             err;
1058
1059         AUE_LOCK(sc);
1060         ifp = &sc->arpcom.ac_if;
1061
1062         if (status != USBD_NORMAL_COMPLETION) {
1063                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1064                         AUE_UNLOCK(sc);
1065                         return;
1066                 }
1067                 printf("aue%d: usb error on tx: %s\n", sc->aue_unit,
1068                     usbd_errstr(status));
1069                 if (status == USBD_STALLED)
1070                         usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1071                 AUE_UNLOCK(sc);
1072                 return;
1073         }
1074
1075         ifp->if_timer = 0;
1076         ifp->if_flags &= ~IFF_OACTIVE;
1077         usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &err);
1078
1079         if (c->aue_mbuf != NULL) {
1080                 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1081                 usb_tx_done(c->aue_mbuf);
1082                 c->aue_mbuf = NULL;
1083         }
1084
1085         if (err)
1086                 ifp->if_oerrors++;
1087         else
1088                 ifp->if_opackets++;
1089
1090         AUE_UNLOCK(sc);
1091
1092         return;
1093 }
1094
1095 Static void
1096 aue_tick(void *xsc)
1097 {
1098         struct aue_softc        *sc = xsc;
1099         struct ifnet            *ifp;
1100         struct mii_data         *mii;
1101
1102         if (sc == NULL)
1103                 return;
1104
1105         AUE_LOCK(sc);
1106
1107         ifp = &sc->arpcom.ac_if;
1108         mii = GET_MII(sc);
1109         if (mii == NULL) {
1110                 AUE_UNLOCK(sc);
1111                 return;
1112         }
1113
1114         mii_tick(mii);
1115         if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1116             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1117                 sc->aue_link++;
1118                 if (ifp->if_snd.ifq_head != NULL)
1119                         aue_start(ifp);
1120         }
1121
1122         callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1123
1124         AUE_UNLOCK(sc);
1125
1126         return;
1127 }
1128
1129 Static int
1130 aue_encap(struct aue_softc *sc, struct mbuf *m, int idx)
1131 {
1132         int                     total_len;
1133         struct aue_chain        *c;
1134         usbd_status             err;
1135
1136         c = &sc->aue_cdata.aue_tx_chain[idx];
1137
1138         /*
1139          * Copy the mbuf data into a contiguous buffer, leaving two
1140          * bytes at the beginning to hold the frame length.
1141          */
1142         m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1143         c->aue_mbuf = m;
1144
1145         total_len = m->m_pkthdr.len + 2;
1146
1147         /*
1148          * The ADMtek documentation says that the packet length is
1149          * supposed to be specified in the first two bytes of the
1150          * transfer, however it actually seems to ignore this info
1151          * and base the frame size on the bulk transfer length.
1152          */
1153         c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1154         c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1155
1156         usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1157             c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER,
1158             10000, aue_txeof);
1159
1160         /* Transmit */
1161         err = usbd_transfer(c->aue_xfer);
1162         if (err != USBD_IN_PROGRESS) {
1163                 aue_stop(sc);
1164                 return (EIO);
1165         }
1166
1167         sc->aue_cdata.aue_tx_cnt++;
1168
1169         return (0);
1170 }
1171
1172 Static void
1173 aue_start(struct ifnet *ifp)
1174 {
1175         struct aue_softc        *sc = ifp->if_softc;
1176         struct mbuf             *m_head = NULL;
1177
1178         AUE_LOCK(sc);
1179
1180         if (!sc->aue_link) {
1181                 AUE_UNLOCK(sc);
1182                 return;
1183         }
1184
1185         if (ifp->if_flags & IFF_OACTIVE) {
1186                 AUE_UNLOCK(sc);
1187                 return;
1188         }
1189
1190         IF_DEQUEUE(&ifp->if_snd, m_head);
1191         if (m_head == NULL) {
1192                 AUE_UNLOCK(sc);
1193                 return;
1194         }
1195
1196         if (aue_encap(sc, m_head, 0)) {
1197                 IF_PREPEND(&ifp->if_snd, m_head);
1198                 ifp->if_flags |= IFF_OACTIVE;
1199                 AUE_UNLOCK(sc);
1200                 return;
1201         }
1202
1203         /*
1204          * If there's a BPF listener, bounce a copy of this frame
1205          * to him.
1206          */
1207         BPF_MTAP(ifp, m_head);
1208
1209         ifp->if_flags |= IFF_OACTIVE;
1210
1211         /*
1212          * Set a timeout in case the chip goes out to lunch.
1213          */
1214         ifp->if_timer = 5;
1215         AUE_UNLOCK(sc);
1216
1217         return;
1218 }
1219
1220 Static void
1221 aue_init(void *xsc)
1222 {
1223         struct aue_softc        *sc = xsc;
1224         struct ifnet            *ifp = &sc->arpcom.ac_if;
1225         struct mii_data         *mii = GET_MII(sc);
1226         struct aue_chain        *c;
1227         usbd_status             err;
1228         int                     i;
1229
1230         AUE_LOCK(sc);
1231
1232         if (ifp->if_flags & IFF_RUNNING) {
1233                 AUE_UNLOCK(sc);
1234                 return;
1235         }
1236
1237         /*
1238          * Cancel pending I/O and free all RX/TX buffers.
1239          */
1240         aue_reset(sc);
1241
1242         /* Set MAC address */
1243         for (i = 0; i < ETHER_ADDR_LEN; i++)
1244                 aue_csr_write_1(sc, AUE_PAR0 + i, sc->arpcom.ac_enaddr[i]);
1245
1246          /* If we want promiscuous mode, set the allframes bit. */
1247         if (ifp->if_flags & IFF_PROMISC)
1248                 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1249         else
1250                 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1251
1252         /* Init TX ring. */
1253         if (aue_tx_list_init(sc) == ENOBUFS) {
1254                 printf("aue%d: tx list init failed\n", sc->aue_unit);
1255                 AUE_UNLOCK(sc);
1256                 return;
1257         }
1258
1259         /* Init RX ring. */
1260         if (aue_rx_list_init(sc) == ENOBUFS) {
1261                 printf("aue%d: rx list init failed\n", sc->aue_unit);
1262                 AUE_UNLOCK(sc);
1263                 return;
1264         }
1265
1266 #ifdef AUE_INTR_PIPE
1267         sc->aue_cdata.aue_ibuf = malloc(AUE_INTR_PKTLEN, M_USBDEV, M_WAITOK);
1268 #endif
1269
1270         /* Load the multicast filter. */
1271         aue_setmulti(sc);
1272
1273         /* Enable RX and TX */
1274         aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1275         AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1276         AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1277
1278         mii_mediachg(mii);
1279
1280         /* Open RX and TX pipes. */
1281         err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1282             USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1283         if (err) {
1284                 printf("aue%d: open rx pipe failed: %s\n",
1285                     sc->aue_unit, usbd_errstr(err));
1286                 AUE_UNLOCK(sc);
1287                 return;
1288         }
1289         err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1290             USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1291         if (err) {
1292                 printf("aue%d: open tx pipe failed: %s\n",
1293                     sc->aue_unit, usbd_errstr(err));
1294                 AUE_UNLOCK(sc);
1295                 return;
1296         }
1297
1298 #ifdef AUE_INTR_PIPE
1299         err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1300             USBD_SHORT_XFER_OK, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1301             sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1302             AUE_INTR_INTERVAL);
1303         if (err) {
1304                 printf("aue%d: open intr pipe failed: %s\n",
1305                     sc->aue_unit, usbd_errstr(err));
1306                 AUE_UNLOCK(sc);
1307                 return;
1308         }
1309 #endif
1310
1311         /* Start up the receive pipe. */
1312         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1313                 c = &sc->aue_cdata.aue_rx_chain[i];
1314                 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1315                     c, mtod(c->aue_mbuf, char *), AUE_BUFSZ,
1316                 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1317                 usbd_transfer(c->aue_xfer);
1318         }
1319
1320         ifp->if_flags |= IFF_RUNNING;
1321         ifp->if_flags &= ~IFF_OACTIVE;
1322
1323         callout_reset(&sc->aue_stat_timer, hz, aue_tick, sc);
1324
1325         AUE_UNLOCK(sc);
1326
1327         return;
1328 }
1329
1330 /*
1331  * Set media options.
1332  */
1333 Static int
1334 aue_ifmedia_upd(struct ifnet *ifp)
1335 {
1336         struct aue_softc        *sc = ifp->if_softc;
1337         struct mii_data         *mii = GET_MII(sc);
1338
1339         sc->aue_link = 0;
1340         if (mii->mii_instance) {
1341                 struct mii_softc        *miisc;
1342                 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1343                          mii_phy_reset(miisc);
1344         }
1345         mii_mediachg(mii);
1346
1347         return (0);
1348 }
1349
1350 /*
1351  * Report current media status.
1352  */
1353 Static void
1354 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1355 {
1356         struct aue_softc        *sc = ifp->if_softc;
1357         struct mii_data         *mii = GET_MII(sc);
1358
1359         mii_pollstat(mii);
1360         ifmr->ifm_active = mii->mii_media_active;
1361         ifmr->ifm_status = mii->mii_media_status;
1362
1363         return;
1364 }
1365
1366 Static int
1367 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1368 {
1369         struct aue_softc        *sc = ifp->if_softc;
1370         struct ifreq            *ifr = (struct ifreq *)data;
1371         struct mii_data         *mii;
1372         int                     error = 0;
1373
1374         AUE_LOCK(sc);
1375
1376         switch(command) {
1377         case SIOCSIFFLAGS:
1378                 if (ifp->if_flags & IFF_UP) {
1379                         if (ifp->if_flags & IFF_RUNNING &&
1380                             ifp->if_flags & IFF_PROMISC &&
1381                             !(sc->aue_if_flags & IFF_PROMISC)) {
1382                                 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1383                         } else if (ifp->if_flags & IFF_RUNNING &&
1384                             !(ifp->if_flags & IFF_PROMISC) &&
1385                             sc->aue_if_flags & IFF_PROMISC) {
1386                                 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1387                         } else if (!(ifp->if_flags & IFF_RUNNING))
1388                                 aue_init(sc);
1389                 } else {
1390                         if (ifp->if_flags & IFF_RUNNING)
1391                                 aue_stop(sc);
1392                 }
1393                 sc->aue_if_flags = ifp->if_flags;
1394                 error = 0;
1395                 break;
1396         case SIOCADDMULTI:
1397         case SIOCDELMULTI:
1398                 aue_setmulti(sc);
1399                 error = 0;
1400                 break;
1401         case SIOCGIFMEDIA:
1402         case SIOCSIFMEDIA:
1403                 mii = GET_MII(sc);
1404                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1405                 break;
1406         default:
1407                 error = ether_ioctl(ifp, command, data);
1408                 break;
1409         }
1410
1411         AUE_UNLOCK(sc);
1412
1413         return (error);
1414 }
1415
1416 Static void
1417 aue_watchdog(struct ifnet *ifp)
1418 {
1419         struct aue_softc        *sc = ifp->if_softc;
1420         struct aue_chain        *c;
1421         usbd_status             stat;
1422
1423         AUE_LOCK(sc);
1424
1425         ifp->if_oerrors++;
1426         printf("aue%d: watchdog timeout\n", sc->aue_unit);
1427
1428         c = &sc->aue_cdata.aue_tx_chain[0];
1429         usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1430         aue_txeof(c->aue_xfer, c, stat);
1431
1432         if (ifp->if_snd.ifq_head != NULL)
1433                 aue_start(ifp);
1434         AUE_UNLOCK(sc);
1435         return;
1436 }
1437
1438 /*
1439  * Stop the adapter and free any mbufs allocated to the
1440  * RX and TX lists.
1441  */
1442 Static void
1443 aue_stop(struct aue_softc *sc)
1444 {
1445         usbd_status             err;
1446         struct ifnet            *ifp;
1447         int                     i;
1448
1449         AUE_LOCK(sc);
1450         ifp = &sc->arpcom.ac_if;
1451         ifp->if_timer = 0;
1452
1453         aue_csr_write_1(sc, AUE_CTL0, 0);
1454         aue_csr_write_1(sc, AUE_CTL1, 0);
1455         aue_reset(sc);
1456         callout_stop(&sc->aue_stat_timer);
1457
1458         /* Stop transfers. */
1459         if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1460                 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1461                 if (err) {
1462                         printf("aue%d: abort rx pipe failed: %s\n",
1463                         sc->aue_unit, usbd_errstr(err));
1464                 }
1465                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1466                 if (err) {
1467                         printf("aue%d: close rx pipe failed: %s\n",
1468                         sc->aue_unit, usbd_errstr(err));
1469                 }
1470                 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1471         }
1472
1473         if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1474                 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1475                 if (err) {
1476                         printf("aue%d: abort tx pipe failed: %s\n",
1477                         sc->aue_unit, usbd_errstr(err));
1478                 }
1479                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1480                 if (err) {
1481                         printf("aue%d: close tx pipe failed: %s\n",
1482                             sc->aue_unit, usbd_errstr(err));
1483                 }
1484                 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1485         }
1486
1487 #ifdef AUE_INTR_PIPE
1488         if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1489                 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1490                 if (err) {
1491                         printf("aue%d: abort intr pipe failed: %s\n",
1492                         sc->aue_unit, usbd_errstr(err));
1493                 }
1494                 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1495                 if (err) {
1496                         printf("aue%d: close intr pipe failed: %s\n",
1497                             sc->aue_unit, usbd_errstr(err));
1498                 }
1499                 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1500         }
1501 #endif
1502
1503         /* Free RX resources. */
1504         for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1505                 if (sc->aue_cdata.aue_rx_chain[i].aue_buf != NULL) {
1506                         free(sc->aue_cdata.aue_rx_chain[i].aue_buf, M_USBDEV);
1507                         sc->aue_cdata.aue_rx_chain[i].aue_buf = NULL;
1508                 }
1509                 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1510                         m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1511                         sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1512                 }
1513                 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1514                         usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1515                         sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1516                 }
1517         }
1518
1519         /* Free TX resources. */
1520         for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1521                 if (sc->aue_cdata.aue_tx_chain[i].aue_buf != NULL) {
1522                         free(sc->aue_cdata.aue_tx_chain[i].aue_buf, M_USBDEV);
1523                         sc->aue_cdata.aue_tx_chain[i].aue_buf = NULL;
1524                 }
1525                 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1526                         m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1527                         sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1528                 }
1529                 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1530                         usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1531                         sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1532                 }
1533         }
1534
1535 #ifdef AUE_INTR_PIPE
1536         free(sc->aue_cdata.aue_ibuf, M_USBDEV);
1537         sc->aue_cdata.aue_ibuf = NULL;
1538 #endif
1539
1540         sc->aue_link = 0;
1541
1542         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1543         AUE_UNLOCK(sc);
1544
1545         return;
1546 }
1547
1548 /*
1549  * Stop all chip I/O so that the kernel's probe routines don't
1550  * get confused by errant DMAs when rebooting.
1551  */
1552 Static void
1553 aue_shutdown(device_ptr_t dev)
1554 {
1555         struct aue_softc        *sc;
1556
1557         sc = device_get_softc(dev);
1558         sc->aue_dying++;
1559         AUE_LOCK(sc);
1560         aue_reset(sc);
1561         aue_stop(sc);
1562         AUE_UNLOCK(sc);
1563
1564         return;
1565 }