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