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