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