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