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