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