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