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