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