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