if_aue.c revision 1.18 1 /* $NetBSD: if_aue.c,v 1.18 2000/02/02 11:42:29 augustss Exp $ */
2 /*
3 * Copyright (c) 1997, 1998, 1999, 2000
4 * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
34 */
35
36 /*
37 * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available
38 * from http://www.admtek.com.tw.
39 *
40 * Written by Bill Paul <wpaul (at) 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 /*
66 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
67 */
68
69 /*
70 * TODO:
71 * better error messages from rxstat
72 * split out if_auevar.h
73 * add thread to avoid register reads from interrupt context
74 * more error checks
75 * investigate short rx problem
76 * proper cleanup on errors
77 */
78
79 #if defined(__NetBSD__) || defined(__OpenBSD__)
80 #include "opt_inet.h"
81 #include "opt_ns.h"
82 #include "bpfilter.h"
83 #include "rnd.h"
84 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/sockio.h>
89 #include <sys/mbuf.h>
90 #include <sys/malloc.h>
91 #include <sys/kernel.h>
92 #include <sys/socket.h>
93
94 #if defined(__FreeBSD__)
95
96 #include <net/ethernet.h>
97 #include <machine/clock.h> /* for DELAY */
98 #include <sys/bus.h>
99 /* "controller miibus0" required. See GENERIC if you get errors here. */
100 #include "miibus_if.h"
101
102 #elif defined(__NetBSD__) || defined(__OpenBSD__)
103
104 #include <sys/device.h>
105
106 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
107
108 #include <net/if.h>
109 #include <net/if_arp.h>
110 #include <net/if_dl.h>
111 #include <net/if_media.h>
112
113 #if defined(__NetBSD__) || defined(__OpenBSD__)
114 #include <net/if_ether.h>
115
116 #define bpf_mtap(ifp, m) bpf_tap((ifp)->if_bpf, mtod((m), caddr_t), (m)->m_len)
117
118 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
119
120 #if defined(__FreeBSD__) || NBPFILTER > 0
121 #include <net/bpf.h>
122 #endif
123
124 #if defined(__NetBSD__) || defined(__OpenBSD__)
125 #ifdef INET
126 #include <netinet/in.h>
127 #include <netinet/if_inarp.h>
128 #endif
129
130 #ifdef NS
131 #include <netns/ns.h>
132 #include <netns/ns_if.h>
133 #endif
134 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
135
136 #include <dev/mii/mii.h>
137 #include <dev/mii/miivar.h>
138
139 #include <dev/usb/usb.h>
140 #include <dev/usb/usbdi.h>
141 #include <dev/usb/usbdi_util.h>
142 #include <dev/usb/usbdevs.h>
143
144 #ifdef __FreeBSD__
145 #include <dev/usb/usb_ethersubr.h>
146 #endif
147
148 #include <dev/usb/if_auereg.h>
149
150 #ifdef AUE_DEBUG
151 #define DPRINTF(x) if (auedebug) logprintf x
152 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x
153 int auedebug = 0;
154 #else
155 #define DPRINTF(x)
156 #define DPRINTFN(n,x)
157 #endif
158
159 /*
160 * Various supported device vendors/products.
161 */
162 static struct aue_type aue_devs[] = {
163 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
164 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
165 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
166 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
167 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
168 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
169 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
170 { 0, 0 }
171 };
172
173 USB_DECLARE_DRIVER(aue);
174
175 static int aue_tx_list_init __P((struct aue_softc *));
176 static int aue_rx_list_init __P((struct aue_softc *));
177 static int aue_newbuf __P((struct aue_softc *, struct aue_chain *,
178 struct mbuf *));
179 static int aue_send __P((struct aue_softc *, struct mbuf *, int));
180 static void aue_intr __P((usbd_xfer_handle,
181 usbd_private_handle, usbd_status));
182 static void aue_rxeof __P((usbd_xfer_handle,
183 usbd_private_handle, usbd_status));
184 static void aue_txeof __P((usbd_xfer_handle,
185 usbd_private_handle, usbd_status));
186 static void aue_tick __P((void *));
187 static void aue_start __P((struct ifnet *));
188 static int aue_ioctl __P((struct ifnet *, u_long, caddr_t));
189 static void aue_init __P((void *));
190 static void aue_stop __P((struct aue_softc *));
191 static void aue_watchdog __P((struct ifnet *));
192 #ifdef __FreeBSD__
193 static void aue_shutdown __P((device_ptr_t));
194 #endif
195 static int aue_openpipes __P((struct aue_softc *));
196 static int aue_ifmedia_upd __P((struct ifnet *));
197 static void aue_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
198
199 static int aue_eeprom_getword __P((struct aue_softc *, int));
200 static void aue_read_mac __P((struct aue_softc *, u_char *));
201 static int aue_miibus_readreg __P((device_ptr_t, int, int));
202 #if defined(__FreeBSD__)
203 static int aue_miibus_writereg __P((device_ptr_t, int, int, int));
204 #elif defined(__NetBSD__) || defined(__OpenBSD__)
205 static void aue_miibus_writereg __P((device_ptr_t, int, int, int));
206 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
207 static void aue_miibus_statchg __P((device_ptr_t));
208
209 static void aue_setmulti __P((struct aue_softc *));
210 static u_int32_t aue_crc __P((caddr_t));
211 static void aue_reset __P((struct aue_softc *));
212
213 static int csr_read_1 __P((struct aue_softc *, int));
214 static int csr_write_1 __P((struct aue_softc *, int, int));
215 static int csr_read_2 __P((struct aue_softc *, int));
216 static int csr_write_2 __P((struct aue_softc *, int, int));
217
218 #if defined(__FreeBSD__)
219 #if !defined(lint)
220 static const char rcsid[] =
221 "$FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $";
222 #endif
223
224 static void aue_rxstart __P((struct ifnet *));
225
226 static struct usb_qdat aue_qdat;
227
228 static device_method_t aue_methods[] = {
229 /* Device interface */
230 DEVMETHOD(device_probe, aue_match),
231 DEVMETHOD(device_attach, aue_attach),
232 DEVMETHOD(device_detach, aue_detach),
233 DEVMETHOD(device_shutdown, aue_shutdown),
234
235 /* bus interface */
236 DEVMETHOD(bus_print_child, bus_generic_print_child),
237 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
238
239 /* MII interface */
240 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
241 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
242 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
243
244 { 0, 0 }
245 };
246
247 static driver_t aue_driver = {
248 "aue",
249 aue_methods,
250 sizeof(struct aue_softc)
251 };
252
253 static devclass_t aue_devclass;
254
255 DRIVER_MODULE(if_aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
256 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
257
258 #endif /* __FreeBSD__ */
259
260 #define AUE_DO_REQUEST(dev, req, data) usbd_do_request_flags(dev, req, data, USBD_NO_TSLEEP, NULL)
261
262 #define AUE_SETBIT(sc, reg, x) \
263 csr_write_1(sc, reg, csr_read_1(sc, reg) | (x))
264
265 #define AUE_CLRBIT(sc, reg, x) \
266 csr_write_1(sc, reg, csr_read_1(sc, reg) & ~(x))
267
268 static int
269 csr_read_1(sc, reg)
270 struct aue_softc *sc;
271 int reg;
272 {
273 usb_device_request_t req;
274 usbd_status err;
275 uByte val = 0;
276 int s;
277
278 req.bmRequestType = UT_READ_VENDOR_DEVICE;
279 req.bRequest = AUE_UR_READREG;
280 USETW(req.wValue, 0);
281 USETW(req.wIndex, reg);
282 USETW(req.wLength, 1);
283
284 s = splusb();
285 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
286 splx(s);
287
288 if (err)
289 return (0);
290
291 return (val);
292 }
293
294 static int
295 csr_read_2(sc, reg)
296 struct aue_softc *sc;
297 int reg;
298 {
299 usb_device_request_t req;
300 usbd_status err;
301 uWord val;
302 int s;
303
304 req.bmRequestType = UT_READ_VENDOR_DEVICE;
305 req.bRequest = AUE_UR_READREG;
306 USETW(req.wValue, 0);
307 USETW(req.wIndex, reg);
308 USETW(req.wLength, 2);
309
310 s = splusb();
311 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
312 splx(s);
313
314 if (err)
315 return (0);
316
317 return (UGETW(val));
318 }
319
320 static int
321 csr_write_1(sc, reg, aval)
322 struct aue_softc *sc;
323 int reg, aval;
324 {
325 usb_device_request_t req;
326 usbd_status err;
327 int s;
328 uByte val;
329
330 val = aval;
331 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
332 req.bRequest = AUE_UR_WRITEREG;
333 USETW(req.wValue, val);
334 USETW(req.wIndex, reg);
335 USETW(req.wLength, 1);
336
337 s = splusb();
338 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
339 splx(s);
340
341 if (err)
342 return (-1);
343
344 return (0);
345 }
346
347 static int
348 csr_write_2(sc, reg, aval)
349 struct aue_softc *sc;
350 int reg, aval;
351 {
352 usb_device_request_t req;
353 usbd_status err;
354 int s;
355 uWord val;
356
357 USETW(val, aval);
358 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
359 req.bRequest = AUE_UR_WRITEREG;
360 USETW(req.wValue, aval);
361 USETW(req.wIndex, reg);
362 USETW(req.wLength, 2);
363
364 s = splusb();
365 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
366 splx(s);
367
368 if (err)
369 return (-1);
370
371 return (0);
372 }
373
374 /*
375 * Read a word of data stored in the EEPROM at address 'addr.'
376 */
377 static int
378 aue_eeprom_getword(sc, addr)
379 struct aue_softc *sc;
380 int addr;
381 {
382 int i;
383
384 csr_write_1(sc, AUE_EE_REG, addr);
385 csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
386
387 for (i = 0; i < AUE_TIMEOUT; i++) {
388 if (csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
389 break;
390 }
391
392 if (i == AUE_TIMEOUT) {
393 printf("%s: EEPROM read timed out\n",
394 USBDEVNAME(sc->aue_dev));
395 }
396
397 return (csr_read_2(sc, AUE_EE_DATA));
398 }
399
400 /*
401 * Read the MAC from the EEPROM. It's at offset 0.
402 */
403 static void
404 aue_read_mac(sc, dest)
405 struct aue_softc *sc;
406 u_char *dest;
407 {
408 int i;
409 int off = 0;
410 int word;
411
412 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
413
414 for (i = 0; i < 3; i++) {
415 word = aue_eeprom_getword(sc, off + i);
416 dest[2 * i] = (u_char)word;
417 dest[2 * i + 1] = (u_char)(word >> 8);
418 }
419 }
420
421 static int
422 aue_miibus_readreg(dev, phy, reg)
423 device_ptr_t dev;
424 int phy, reg;
425 {
426 struct aue_softc *sc = USBGETSOFTC(dev);
427 int i;
428 u_int16_t val;
429
430 /*
431 * The Am79C901 HomePNA PHY actually contains
432 * two transceivers: a 1Mbps HomePNA PHY and a
433 * 10Mbps full/half duplex ethernet PHY with
434 * NWAY autoneg. However in the ADMtek adapter,
435 * only the 1Mbps PHY is actually connected to
436 * anything, so we ignore the 10Mbps one. It
437 * happens to be configured for MII address 3,
438 * so we filter that out.
439 */
440 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
441 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
442 if (phy != 1)
443 return (0);
444 }
445
446 csr_write_1(sc, AUE_PHY_ADDR, phy);
447 csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
448
449 for (i = 0; i < AUE_TIMEOUT; i++) {
450 if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
451 break;
452 }
453
454 if (i == AUE_TIMEOUT) {
455 printf("%s: MII read timed out\n",
456 USBDEVNAME(sc->aue_dev));
457 }
458
459 val = csr_read_2(sc, AUE_PHY_DATA);
460
461 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
462 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val));
463
464 return (val);
465 }
466
467 #if defined(__FreeBSD__)
468 static int
469 #elif defined(__NetBSD__) || defined(__OpenBSD__)
470 static void
471 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
472 aue_miibus_writereg(dev, phy, reg, data)
473 device_ptr_t dev;
474 int phy, reg, data;
475 {
476 struct aue_softc *sc = USBGETSOFTC(dev);
477 int i;
478
479 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
480 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
481 if (phy == 3)
482 #if defined(__FreeBSD__)
483 return (0);
484 #elif defined(__NetBSD__) || defined(__OpenBSD__)
485 return;
486 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
487 }
488
489 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
490 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data));
491
492 csr_write_2(sc, AUE_PHY_DATA, data);
493 csr_write_1(sc, AUE_PHY_ADDR, phy);
494 csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
495
496 for (i = 0; i < AUE_TIMEOUT; i++) {
497 if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
498 break;
499 }
500
501 if (i == AUE_TIMEOUT) {
502 printf("%s: MII read timed out\n",
503 USBDEVNAME(sc->aue_dev));
504 }
505
506 #if defined(__FreeBSD__)
507 return (0);
508 #endif
509 }
510
511 static void
512 aue_miibus_statchg(dev)
513 device_ptr_t dev;
514 {
515 struct aue_softc *sc = USBGETSOFTC(dev);
516 struct mii_data *mii = GET_MII(sc);
517 struct ifnet *ifp = GET_IFP(sc);
518
519 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
520
521 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
522
523 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
524 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
525 ifp->if_baudrate = 100000000;
526 } else {
527 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
528 ifp->if_baudrate = 10000000;
529 }
530
531 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
532 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
533 else
534 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
535
536 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
537
538 /*
539 * Set the LED modes on the LinkSys adapter.
540 * This turns on the 'dual link LED' bin in the auxmode
541 * register of the Broadcom PHY.
542 */
543 if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
544 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
545 u_int16_t auxmode;
546 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
547 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
548 }
549 }
550
551 #define AUE_POLY 0xEDB88320
552 #define AUE_BITS 6
553
554 static u_int32_t
555 aue_crc(addr)
556 caddr_t addr;
557 {
558 u_int32_t idx, bit, data, crc;
559
560 /* Compute CRC for the address value. */
561 crc = 0xFFFFFFFF; /* initial value */
562
563 for (idx = 0; idx < 6; idx++) {
564 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
565 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
566 }
567
568 return (crc & ((1 << AUE_BITS) - 1));
569 }
570
571 static void
572 aue_setmulti(sc)
573 struct aue_softc *sc;
574 {
575 struct ifnet *ifp;
576 #if defined(__FreeBSD__)
577 struct ifmultiaddr *ifma;
578 #elif defined(__NetBSD__) || defined(__OpenBSD__)
579 struct ether_multi *enm;
580 struct ether_multistep step;
581 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
582 u_int32_t h = 0, i;
583
584 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
585
586 ifp = GET_IFP(sc);
587
588 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
589 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
590 return;
591 }
592
593 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
594
595 /* first, zot all the existing hash bits */
596 for (i = 0; i < 8; i++)
597 csr_write_1(sc, AUE_MAR0 + i, 0);
598
599 /* now program new ones */
600 #if defined(__FreeBSD__)
601 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
602 ifma = ifma->ifma_link.le_next) {
603 if (ifma->ifma_addr->sa_family != AF_LINK)
604 continue;
605 h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
606 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
607 }
608 #elif defined(__NetBSD__) || defined(__OpenBSD__)
609 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
610 while (enm != NULL) {
611 #if 1
612 if (memcmp(enm->enm_addrlo,
613 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
614 ifp->if_flags |= IFF_ALLMULTI;
615 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
616 return;
617 }
618 #endif
619 h = aue_crc(enm->enm_addrlo);
620 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
621 ETHER_NEXT_MULTI(step, enm);
622 }
623 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
624 }
625
626 static void
627 aue_reset(sc)
628 struct aue_softc *sc;
629 {
630 int i;
631
632 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
633
634 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
635
636 for (i = 0; i < AUE_TIMEOUT; i++) {
637 if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
638 break;
639 }
640
641 if (i == AUE_TIMEOUT)
642 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
643
644 /*
645 * The PHY(s) attached to the Pegasus chip may be held
646 * in reset until we flip on the GPIO outputs. Make sure
647 * to set the GPIO pins high so that the PHY(s) will
648 * be enabled.
649 *
650 * Note: We force all of the GPIO pins low first, *then*
651 * enable the ones we want.
652 */
653 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
654 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
655
656 /* Grrr. LinkSys has to be different from everyone else. */
657 if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
658 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
659 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
660 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
661 AUE_GPIO_OUT0);
662 }
663
664 /* Wait a little while for the chip to get its brains in order. */
665 DELAY(10000); /* XXX */
666 }
667
668 /*
669 * Probe for a Pegasus chip.
670 */
671 USB_MATCH(aue)
672 {
673 USB_MATCH_START(aue, uaa);
674 struct aue_type *t;
675
676 if (uaa->iface != NULL)
677 return (UMATCH_NONE);
678
679 for (t = aue_devs; t->aue_vid != 0; t++)
680 if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
681 return (UMATCH_VENDOR_PRODUCT);
682
683 return (UMATCH_NONE);
684 }
685
686 /*
687 * Attach the interface. Allocate softc structures, do ifmedia
688 * setup and ethernet/BPF attach.
689 */
690 USB_ATTACH(aue)
691 {
692 USB_ATTACH_START(aue, sc, uaa);
693 char devinfo[1024];
694 int s;
695 u_char eaddr[ETHER_ADDR_LEN];
696 struct ifnet *ifp;
697 struct mii_data *mii;
698 usbd_device_handle dev = uaa->device;
699 usbd_interface_handle iface;
700 usbd_status err;
701 usb_interface_descriptor_t *id;
702 usb_endpoint_descriptor_t *ed;
703 int i;
704
705 #ifdef __FreeBSD__
706 bzero(sc, sizeof(struct aue_softc));
707 #endif
708
709 DPRINTFN(5,(" : aue_attach: sc=%p", sc));
710
711 usbd_devinfo(dev, 0, devinfo);
712 USB_ATTACH_SETUP;
713 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
714
715 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
716 if (err) {
717 printf("%s: setting config no failed\n",
718 USBDEVNAME(sc->aue_dev));
719 USB_ATTACH_ERROR_RETURN;
720 }
721
722 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
723 if (err) {
724 printf("%s: getting interface handle failed\n",
725 USBDEVNAME(sc->aue_dev));
726 USB_ATTACH_ERROR_RETURN;
727 }
728
729 sc->aue_udev = dev;
730 sc->aue_iface = iface;
731 sc->aue_product = uaa->product;
732 sc->aue_vendor = uaa->vendor;
733
734 id = usbd_get_interface_descriptor(iface);
735
736 /* Find endpoints. */
737 for (i = 0; i < id->bNumEndpoints; i++) {
738 ed = usbd_interface2endpoint_descriptor(iface, i);
739 if (!ed) {
740 printf("%s: couldn't get endpoint descriptor %d\n",
741 USBDEVNAME(sc->aue_dev), i);
742 USB_ATTACH_ERROR_RETURN;
743 }
744 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
745 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
746 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
747 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
748 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
749 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
750 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
751 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
752 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
753 }
754 }
755
756 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
757 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
758 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
759 USB_ATTACH_ERROR_RETURN;
760 }
761
762
763 s = splimp();
764
765 /* Reset the adapter. */
766 aue_reset(sc);
767
768 /*
769 * Get station address from the EEPROM.
770 */
771 aue_read_mac(sc, eaddr);
772
773 /*
774 * A Pegasus chip was detected. Inform the world.
775 */
776 #if defined(__FreeBSD__)
777 printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
778 eaddr, ":");
779
780 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
781
782 ifp = &sc->arpcom.ac_if;
783 ifp->if_softc = sc;
784 ifp->if_unit = sc->aue_unit;
785 ifp->if_name = "aue";
786 ifp->if_mtu = ETHERMTU;
787 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
788 ifp->if_ioctl = aue_ioctl;
789 ifp->if_output = ether_output;
790 ifp->if_start = aue_start;
791 ifp->if_watchdog = aue_watchdog;
792 ifp->if_init = aue_init;
793 ifp->if_baudrate = 10000000;
794 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
795
796 /*
797 * Do MII setup.
798 * NOTE: Doing this causes child devices to be attached to us,
799 * which we would normally disconnect at in the detach routine
800 * using device_delete_child(). However the USB code is set up
801 * such that when this driver is removed, all childred devices
802 * are removed as well. In effect, the USB code ends up detaching
803 * all of our children for us, so we don't have to do is ourselves
804 * in aue_detach(). It's important to point this out since if
805 * we *do* try to detach the child devices ourselves, we will
806 * end up getting the children deleted twice, which will crash
807 * the system.
808 */
809 if (mii_phy_probe(self, &sc->aue_miibus,
810 aue_ifmedia_upd, aue_ifmedia_sts)) {
811 printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
812 splx(s);
813 USB_ATTACH_ERROR_RETURN;
814 }
815
816 aue_qdat.ifp = ifp;
817 aue_qdat.if_rxstart = aue_rxstart;
818
819 /*
820 * Call MI attach routines.
821 */
822 if_attach(ifp);
823 ether_ifattach(ifp);
824 callout_handle_init(&sc->aue_stat_ch);
825 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
826
827 usb_register_netisr();
828
829 #elif defined(__NetBSD__) || defined(__OpenBSD__)
830
831 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
832 ether_sprintf(eaddr));
833
834 /* Initialize interface info.*/
835 ifp = &sc->aue_ec.ec_if;
836 ifp->if_softc = sc;
837 ifp->if_mtu = ETHERMTU;
838 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
839 ifp->if_ioctl = aue_ioctl;
840 ifp->if_start = aue_start;
841 ifp->if_watchdog = aue_watchdog;
842 ifp->if_baudrate = 10000000;
843 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
844
845 /* Initialize MII/media info. */
846 mii = &sc->aue_mii;
847 mii->mii_ifp = ifp;
848 mii->mii_readreg = aue_miibus_readreg;
849 mii->mii_writereg = aue_miibus_writereg;
850 mii->mii_statchg = aue_miibus_statchg;
851 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
852 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY);
853 if (LIST_FIRST(&mii->mii_phys) == NULL) {
854 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
855 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
856 } else
857 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
858
859 /* Attach the interface. */
860 if_attach(ifp);
861 ether_ifattach(ifp, eaddr);
862
863 #if NBPFILTER > 0
864 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
865 sizeof(struct ether_header));
866 #endif
867 #if RND > 0
868 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
869 RND_TYPE_NET, 0);
870 #endif
871
872 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
873
874 splx(s);
875
876 USB_ATTACH_SUCCESS_RETURN;
877 }
878
879 USB_DETACH(aue)
880 {
881 USB_DETACH_START(aue, sc);
882 struct ifnet *ifp = GET_IFP(sc);
883 int s;
884
885 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
886
887 s = splusb();
888
889 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
890
891 if (ifp->if_flags & IFF_RUNNING)
892 aue_stop(sc);
893
894 #if defined(__NetBSD__)
895 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
896 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
897 #if NBPFILTER > 0
898 bpfdetach(ifp);
899 #endif
900 ether_ifdetach(ifp);
901 #endif /* __NetBSD__ */
902
903 if_detach(ifp);
904
905 #ifdef DIAGNOSTIC
906 if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
907 sc->aue_ep[AUE_ENDPT_RX] != NULL ||
908 sc->aue_ep[AUE_ENDPT_INTR] != NULL)
909 printf("%s: detach has active endpoints\n",
910 USBDEVNAME(sc->aue_dev));
911 #endif
912
913 splx(s);
914
915 return (0);
916 }
917
918 #if defined(__NetBSD__) || defined(__OpenBSD__)
919 int
920 aue_activate(self, act)
921 device_ptr_t self;
922 enum devact act;
923 {
924 struct aue_softc *sc = (struct aue_softc *)self;
925
926 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
927
928 switch (act) {
929 case DVACT_ACTIVATE:
930 return (EOPNOTSUPP);
931 break;
932
933 case DVACT_DEACTIVATE:
934 if_deactivate(&sc->aue_ec.ec_if);
935 sc->aue_dying = 1;
936 break;
937 }
938 return (0);
939 }
940 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
941
942 /*
943 * Initialize an RX descriptor and attach an MBUF cluster.
944 */
945 static int
946 aue_newbuf(sc, c, m)
947 struct aue_softc *sc;
948 struct aue_chain *c;
949 struct mbuf *m;
950 {
951 struct mbuf *m_new = NULL;
952
953 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
954
955 if (m == NULL) {
956 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
957 if (m_new == NULL) {
958 printf("%s: no memory for rx list "
959 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
960 return (ENOBUFS);
961 }
962
963 MCLGET(m_new, M_DONTWAIT);
964 if (!(m_new->m_flags & M_EXT)) {
965 printf("%s: no memory for rx list "
966 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
967 m_freem(m_new);
968 return (ENOBUFS);
969 }
970 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
971 } else {
972 m_new = m;
973 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
974 m_new->m_data = m_new->m_ext.ext_buf;
975 }
976
977 m_adj(m_new, ETHER_ALIGN);
978 c->aue_mbuf = m_new;
979
980 return (0);
981 }
982
983 static int
984 aue_rx_list_init(sc)
985 struct aue_softc *sc;
986 {
987 struct aue_cdata *cd;
988 struct aue_chain *c;
989 int i;
990
991 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
992
993 cd = &sc->aue_cdata;
994 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
995 c = &cd->aue_rx_chain[i];
996 c->aue_sc = sc;
997 c->aue_idx = i;
998 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
999 return (ENOBUFS);
1000 if (c->aue_xfer == NULL) {
1001 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1002 if (c->aue_xfer == NULL)
1003 return (ENOBUFS);
1004 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1005 if (c->aue_buf == NULL)
1006 return (ENOBUFS); /* XXX free xfer */
1007 }
1008 }
1009
1010 return (0);
1011 }
1012
1013 static int
1014 aue_tx_list_init(sc)
1015 struct aue_softc *sc;
1016 {
1017 struct aue_cdata *cd;
1018 struct aue_chain *c;
1019 int i;
1020
1021 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1022
1023 cd = &sc->aue_cdata;
1024 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1025 c = &cd->aue_tx_chain[i];
1026 c->aue_sc = sc;
1027 c->aue_idx = i;
1028 c->aue_mbuf = NULL;
1029 if (c->aue_xfer == NULL) {
1030 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1031 if (c->aue_xfer == NULL)
1032 return (ENOBUFS);
1033 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1034 if (c->aue_buf == NULL)
1035 return (ENOBUFS);
1036 }
1037 }
1038
1039 return (0);
1040 }
1041
1042 static void
1043 aue_intr(xfer, priv, status)
1044 usbd_xfer_handle xfer;
1045 usbd_private_handle priv;
1046 usbd_status status;
1047 {
1048 struct aue_softc *sc = priv;
1049 struct ifnet *ifp = GET_IFP(sc);
1050 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1051
1052 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1053
1054 if (sc->aue_dying)
1055 return;
1056
1057 if (!(ifp->if_flags & IFF_RUNNING))
1058 return;
1059
1060 if (status != USBD_NORMAL_COMPLETION) {
1061 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1062 return;
1063 }
1064 printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
1065 usbd_errstr(status));
1066 if (status == USBD_STALLED)
1067 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1068 return;
1069 }
1070
1071 if (p->aue_txstat0)
1072 ifp->if_oerrors++;
1073
1074 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1075 ifp->if_collisions++;
1076 }
1077
1078 #if defined(__FreeBSD__)
1079 static void
1080 aue_rxstart(ifp)
1081 struct ifnet *ifp;
1082 {
1083 struct aue_softc *sc;
1084 struct aue_chain *c;
1085
1086 sc = ifp->if_softc;
1087 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
1088
1089 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1090 ifp->if_ierrors++;
1091 return;
1092 }
1093
1094 /* Setup new transfer. */
1095 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1096 c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
1097 USBD_NO_TIMEOUT, aue_rxeof);
1098 usbd_transfer(c->aue_xfer);
1099 }
1100 #endif
1101
1102 /*
1103 * A frame has been uploaded: pass the resulting mbuf chain up to
1104 * the higher level protocols.
1105 *
1106 * Grrr. Receiving transfers larger than about 1152 bytes sometimes
1107 * doesn't work. We get an incomplete frame. In order to avoid
1108 * this, we queue up RX transfers that are shorter than a full sized
1109 * frame. If the received frame is larger than our transfer size,
1110 * we snag the rest of the data using a second transfer. Does this
1111 * hurt performance? Yes. But after fighting with this stupid thing
1112 * for three days, I'm willing to settle. I'd rather have reliable
1113 * receive performance that fast but spotty performance.
1114 */
1115 static void
1116 aue_rxeof(xfer, priv, status)
1117 usbd_xfer_handle xfer;
1118 usbd_private_handle priv;
1119 usbd_status status;
1120 {
1121 struct aue_chain *c = priv;
1122 struct aue_softc *sc = c->aue_sc;
1123 struct ifnet *ifp = GET_IFP(sc);
1124 struct mbuf *m;
1125 u_int32_t total_len;
1126 struct aue_rxpkt r;
1127 #if defined(__NetBSD__) || defined(__OpenBSD__)
1128 int s;
1129 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1130
1131 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1132
1133 if (sc->aue_dying)
1134 return;
1135
1136 if (!(ifp->if_flags & IFF_RUNNING))
1137 return;
1138
1139 if (status != USBD_NORMAL_COMPLETION) {
1140 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1141 return;
1142 printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
1143 usbd_errstr(status));
1144 if (status == USBD_STALLED)
1145 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1146 goto done;
1147 }
1148
1149 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1150
1151 memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
1152
1153 if (total_len <= 4 + ETHER_CRC_LEN) {
1154 ifp->if_ierrors++;
1155 goto done;
1156 }
1157
1158 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1159
1160 /* Turn off all the non-error bits in the rx status word. */
1161 r.aue_rxstat &= AUE_RXSTAT_MASK;
1162 if (r.aue_rxstat) {
1163 ifp->if_ierrors++;
1164 goto done;
1165 }
1166
1167 /* No errors; receive the packet. */
1168 m = c->aue_mbuf;
1169 total_len -= ETHER_CRC_LEN + 4;
1170 m->m_pkthdr.len = m->m_len = total_len;
1171 ifp->if_ipackets++;
1172
1173 #if defined(__FreeBSD__)
1174 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
1175 /* Put the packet on the special USB input queue. */
1176 usb_ether_input(m);
1177
1178 return;
1179
1180 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1181 m->m_pkthdr.rcvif = ifp;
1182
1183 s = splimp();
1184
1185 /* XXX ugly */
1186 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1187 ifp->if_ierrors++;
1188 goto done1;
1189 }
1190
1191 /*
1192 * Handle BPF listeners. Let the BPF user see the packet, but
1193 * don't pass it up to the ether_input() layer unless it's
1194 * a broadcast packet, multicast packet, matches our ethernet
1195 * address or the interface is in promiscuous mode.
1196 */
1197 if (ifp->if_bpf) {
1198 struct ether_header *eh = mtod(m, struct ether_header *);
1199 bpf_mtap(ifp, m);
1200 if ((ifp->if_flags & IFF_PROMISC) &&
1201 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1202 ETHER_ADDR_LEN) &&
1203 !(eh->ether_dhost[0] & 1)) {
1204 m_freem(m);
1205 goto done1;
1206 }
1207 }
1208
1209 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1210 __FUNCTION__, m->m_len));
1211 (*ifp->if_input)(ifp, m);
1212 done1:
1213 splx(s);
1214 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1215
1216 done:
1217
1218 /* Setup new transfer. */
1219 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1220 c, c->aue_buf, AUE_BUFSZ,
1221 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1222 USBD_NO_TIMEOUT, aue_rxeof);
1223 usbd_transfer(xfer);
1224
1225 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1226 __FUNCTION__));
1227 }
1228
1229 /*
1230 * A frame was downloaded to the chip. It's safe for us to clean up
1231 * the list buffers.
1232 */
1233
1234 static void
1235 aue_txeof(xfer, priv, status)
1236 usbd_xfer_handle xfer;
1237 usbd_private_handle priv;
1238 usbd_status status;
1239 {
1240 struct aue_chain *c = priv;
1241 struct aue_softc *sc = c->aue_sc;
1242 struct ifnet *ifp = GET_IFP(sc);
1243 int s;
1244
1245 if (sc->aue_dying)
1246 return;
1247
1248 s = splimp();
1249
1250 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1251 __FUNCTION__, status));
1252
1253 ifp->if_timer = 0;
1254 ifp->if_flags &= ~IFF_OACTIVE;
1255
1256 if (status != USBD_NORMAL_COMPLETION) {
1257 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1258 splx(s);
1259 return;
1260 }
1261 ifp->if_oerrors++;
1262 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1263 usbd_errstr(status));
1264 if (status == USBD_STALLED)
1265 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1266 splx(s);
1267 return;
1268 }
1269
1270 ifp->if_opackets++;
1271
1272 #if defined(__FreeBSD__)
1273 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1274 usb_tx_done(c->aue_mbuf);
1275 c->aue_mbuf = NULL;
1276 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1277 m_freem(c->aue_mbuf);
1278 c->aue_mbuf = NULL;
1279
1280 if (ifp->if_snd.ifq_head != NULL)
1281 aue_start(ifp);
1282 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1283
1284 splx(s);
1285 }
1286
1287 static void
1288 aue_tick(xsc)
1289 void *xsc;
1290 {
1291 struct aue_softc *sc = xsc;
1292 struct ifnet *ifp;
1293 struct mii_data *mii;
1294 int s;
1295
1296 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1297
1298 if (sc == NULL)
1299 return;
1300
1301 if (sc->aue_dying)
1302 return;
1303
1304 ifp = GET_IFP(sc);
1305 mii = GET_MII(sc);
1306 if (mii == NULL)
1307 return;
1308
1309 s = splimp();
1310
1311 mii_tick(mii);
1312 if (!sc->aue_link) {
1313 mii_pollstat(mii);
1314 if (mii->mii_media_status & IFM_ACTIVE &&
1315 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1316 DPRINTFN(2,("%s: %s: got link\n",
1317 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1318 sc->aue_link++;
1319 if (ifp->if_snd.ifq_head != NULL)
1320 aue_start(ifp);
1321 }
1322 }
1323
1324 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1325
1326 splx(s);
1327 }
1328
1329 static int
1330 aue_send(sc, m, idx)
1331 struct aue_softc *sc;
1332 struct mbuf *m;
1333 int idx;
1334 {
1335 int total_len;
1336 struct aue_chain *c;
1337 usbd_status err;
1338
1339 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1340
1341 c = &sc->aue_cdata.aue_tx_chain[idx];
1342
1343 /*
1344 * Copy the mbuf data into a contiguous buffer, leaving two
1345 * bytes at the beginning to hold the frame length.
1346 */
1347 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1348 c->aue_mbuf = m;
1349
1350 /*
1351 * The ADMtek documentation says that the packet length is
1352 * supposed to be specified in the first two bytes of the
1353 * transfer, however it actually seems to ignore this info
1354 * and base the frame size on the bulk transfer length.
1355 */
1356 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1357 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1358 total_len = m->m_pkthdr.len + 2;
1359
1360 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1361 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1362 AUE_TX_TIMEOUT, aue_txeof);
1363
1364 /* Transmit */
1365 err = usbd_transfer(c->aue_xfer);
1366 if (err != USBD_IN_PROGRESS) {
1367 aue_stop(sc);
1368 return (EIO);
1369 }
1370 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1371 __FUNCTION__, total_len));
1372
1373 sc->aue_cdata.aue_tx_cnt++;
1374
1375 return (0);
1376 }
1377
1378 static void
1379 aue_start(ifp)
1380 struct ifnet *ifp;
1381 {
1382 struct aue_softc *sc = ifp->if_softc;
1383 struct mbuf *m_head = NULL;
1384
1385 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1386 __FUNCTION__, sc->aue_link));
1387
1388 if (sc->aue_dying)
1389 return;
1390
1391 if (!sc->aue_link)
1392 return;
1393
1394 if (ifp->if_flags & IFF_OACTIVE)
1395 return;
1396
1397 IF_DEQUEUE(&ifp->if_snd, m_head);
1398 if (m_head == NULL)
1399 return;
1400
1401 if (aue_send(sc, m_head, 0)) {
1402 IF_PREPEND(&ifp->if_snd, m_head);
1403 ifp->if_flags |= IFF_OACTIVE;
1404 return;
1405 }
1406
1407 /*
1408 * If there's a BPF listener, bounce a copy of this frame
1409 * to him.
1410 */
1411 if (ifp->if_bpf)
1412 bpf_mtap(ifp, m_head);
1413
1414 ifp->if_flags |= IFF_OACTIVE;
1415
1416 /*
1417 * Set a timeout in case the chip goes out to lunch.
1418 */
1419 ifp->if_timer = 5;
1420 }
1421
1422 static void
1423 aue_init(xsc)
1424 void *xsc;
1425 {
1426 struct aue_softc *sc = xsc;
1427 struct ifnet *ifp = GET_IFP(sc);
1428 struct mii_data *mii = GET_MII(sc);
1429 int i, s;
1430 u_char *eaddr;
1431
1432 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1433
1434 if (sc->aue_dying)
1435 return;
1436
1437 if (ifp->if_flags & IFF_RUNNING)
1438 return;
1439
1440 s = splimp();
1441
1442 /*
1443 * Cancel pending I/O and free all RX/TX buffers.
1444 */
1445 aue_reset(sc);
1446
1447 #if defined(__FreeBSD__)
1448 eaddr = sc->arpcom.ac_enaddr;
1449 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1450 eaddr = LLADDR(ifp->if_sadl);
1451 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1452 for (i = 0; i < ETHER_ADDR_LEN; i++)
1453 csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1454
1455 /* If we want promiscuous mode, set the allframes bit. */
1456 if (ifp->if_flags & IFF_PROMISC)
1457 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1458 else
1459 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1460
1461 /* Init TX ring. */
1462 if (aue_tx_list_init(sc) == ENOBUFS) {
1463 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1464 splx(s);
1465 return;
1466 }
1467
1468 /* Init RX ring. */
1469 if (aue_rx_list_init(sc) == ENOBUFS) {
1470 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1471 splx(s);
1472 return;
1473 }
1474
1475 /* Load the multicast filter. */
1476 aue_setmulti(sc);
1477
1478 /* Enable RX and TX */
1479 csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
1480 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1481 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1482
1483 mii_mediachg(mii);
1484
1485 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1486 if (aue_openpipes(sc)) {
1487 splx(s);
1488 return;
1489 }
1490 }
1491
1492 ifp->if_flags |= IFF_RUNNING;
1493 ifp->if_flags &= ~IFF_OACTIVE;
1494
1495 splx(s);
1496
1497 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1498 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1499 }
1500
1501 static int
1502 aue_openpipes(sc)
1503 struct aue_softc *sc;
1504 {
1505 struct aue_chain *c;
1506 usbd_status err;
1507 int i;
1508
1509 /* Open RX and TX pipes. */
1510 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1511 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1512 if (err) {
1513 printf("%s: open rx pipe failed: %s\n",
1514 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1515 return (EIO);
1516 }
1517 usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1518 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1519 if (err) {
1520 printf("%s: open tx pipe failed: %s\n",
1521 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1522 return (EIO);
1523 }
1524 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1525 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1526 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1527 AUE_INTR_INTERVAL);
1528 if (err) {
1529 printf("%s: open intr pipe failed: %s\n",
1530 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1531 return (EIO);
1532 }
1533
1534 /* Start up the receive pipe. */
1535 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1536 c = &sc->aue_cdata.aue_rx_chain[i];
1537 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1538 c, c->aue_buf, AUE_BUFSZ,
1539 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1540 aue_rxeof);
1541 (void)usbd_transfer(c->aue_xfer); /* XXX */
1542 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1543 __FUNCTION__));
1544
1545 }
1546 return (0);
1547 }
1548
1549 /*
1550 * Set media options.
1551 */
1552 static int
1553 aue_ifmedia_upd(ifp)
1554 struct ifnet *ifp;
1555 {
1556 struct aue_softc *sc = ifp->if_softc;
1557 struct mii_data *mii = GET_MII(sc);
1558
1559 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1560
1561 if (sc->aue_dying)
1562 return (0);
1563
1564 sc->aue_link = 0;
1565 if (mii->mii_instance) {
1566 struct mii_softc *miisc;
1567 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1568 miisc = LIST_NEXT(miisc, mii_list))
1569 mii_phy_reset(miisc);
1570 }
1571 mii_mediachg(mii);
1572
1573 return (0);
1574 }
1575
1576 /*
1577 * Report current media status.
1578 */
1579 static void
1580 aue_ifmedia_sts(ifp, ifmr)
1581 struct ifnet *ifp;
1582 struct ifmediareq *ifmr;
1583 {
1584 struct aue_softc *sc = ifp->if_softc;
1585 struct mii_data *mii = GET_MII(sc);
1586
1587 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1588
1589 mii_pollstat(mii);
1590 ifmr->ifm_active = mii->mii_media_active;
1591 ifmr->ifm_status = mii->mii_media_status;
1592 }
1593
1594 static int
1595 aue_ioctl(ifp, command, data)
1596 struct ifnet *ifp;
1597 u_long command;
1598 caddr_t data;
1599 {
1600 struct aue_softc *sc = ifp->if_softc;
1601 #if defined(__NetBSD__) || defined(__OpenBSD__)
1602 struct ifaddr *ifa = (struct ifaddr *)data;
1603 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1604 struct ifreq *ifr = (struct ifreq *)data;
1605 struct mii_data *mii;
1606 int s, error = 0;
1607
1608 if (sc->aue_dying)
1609 return (EIO);
1610
1611 s = splimp();
1612
1613 switch(command) {
1614 #if defined(__FreeBSD__)
1615 case SIOCSIFADDR:
1616 case SIOCGIFADDR:
1617 case SIOCSIFMTU:
1618 error = ether_ioctl(ifp, command, data);
1619 break;
1620 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1621 case SIOCSIFADDR:
1622 ifp->if_flags |= IFF_UP;
1623 aue_init(sc);
1624
1625 switch (ifa->ifa_addr->sa_family) {
1626 #ifdef INET
1627 case AF_INET:
1628 arp_ifinit(ifp, ifa);
1629 break;
1630 #endif /* INET */
1631 #ifdef NS
1632 case AF_NS:
1633 {
1634 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1635
1636 if (ns_nullhost(*ina))
1637 ina->x_host = *(union ns_host *)
1638 LLADDR(ifp->if_sadl);
1639 else
1640 memcpy(LLADDR(ifp->if_sadl),
1641 ina->x_host.c_host,
1642 ifp->if_addrlen);
1643 break;
1644 }
1645 #endif /* NS */
1646 }
1647 break;
1648
1649 case SIOCSIFMTU:
1650 if (ifr->ifr_mtu > ETHERMTU)
1651 error = EINVAL;
1652 else
1653 ifp->if_mtu = ifr->ifr_mtu;
1654 break;
1655
1656 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1657 case SIOCSIFFLAGS:
1658 if (ifp->if_flags & IFF_UP) {
1659 if (ifp->if_flags & IFF_RUNNING &&
1660 ifp->if_flags & IFF_PROMISC &&
1661 !(sc->aue_if_flags & IFF_PROMISC)) {
1662 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1663 } else if (ifp->if_flags & IFF_RUNNING &&
1664 !(ifp->if_flags & IFF_PROMISC) &&
1665 sc->aue_if_flags & IFF_PROMISC) {
1666 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1667 } else if (!(ifp->if_flags & IFF_RUNNING))
1668 aue_init(sc);
1669 } else {
1670 if (ifp->if_flags & IFF_RUNNING)
1671 aue_stop(sc);
1672 }
1673 sc->aue_if_flags = ifp->if_flags;
1674 error = 0;
1675 break;
1676 case SIOCADDMULTI:
1677 case SIOCDELMULTI:
1678 aue_setmulti(sc);
1679 error = 0;
1680 break;
1681 case SIOCGIFMEDIA:
1682 case SIOCSIFMEDIA:
1683 mii = GET_MII(sc);
1684 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1685 break;
1686 default:
1687 error = EINVAL;
1688 break;
1689 }
1690
1691 splx(s);
1692
1693 return (error);
1694 }
1695
1696 static void
1697 aue_watchdog(ifp)
1698 struct ifnet *ifp;
1699 {
1700 struct aue_softc *sc = ifp->if_softc;
1701
1702 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1703
1704 ifp->if_oerrors++;
1705 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1706
1707 /*
1708 * The polling business is a kludge to avoid allowing the
1709 * USB code to call tsleep() in usbd_delay_ms(), which will
1710 * kill us since the watchdog routine is invoked from
1711 * interrupt context.
1712 */
1713 usbd_set_polling(sc->aue_udev, 1);
1714 aue_stop(sc);
1715 aue_init(sc);
1716 usbd_set_polling(sc->aue_udev, 0);
1717
1718 if (ifp->if_snd.ifq_head != NULL)
1719 aue_start(ifp);
1720 }
1721
1722 /*
1723 * Stop the adapter and free any mbufs allocated to the
1724 * RX and TX lists.
1725 */
1726 static void
1727 aue_stop(sc)
1728 struct aue_softc *sc;
1729 {
1730 usbd_status err;
1731 struct ifnet *ifp;
1732 int i;
1733
1734 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1735
1736 ifp = GET_IFP(sc);
1737 ifp->if_timer = 0;
1738
1739 csr_write_1(sc, AUE_CTL0, 0);
1740 csr_write_1(sc, AUE_CTL1, 0);
1741 aue_reset(sc);
1742 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1743
1744 /* Stop transfers. */
1745 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1746 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1747 if (err) {
1748 printf("%s: abort rx pipe failed: %s\n",
1749 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1750 }
1751 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1752 if (err) {
1753 printf("%s: close rx pipe failed: %s\n",
1754 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1755 }
1756 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1757 }
1758
1759 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1760 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1761 if (err) {
1762 printf("%s: abort tx pipe failed: %s\n",
1763 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1764 }
1765 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1766 if (err) {
1767 printf("%s: close tx pipe failed: %s\n",
1768 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1769 }
1770 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1771 }
1772
1773 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1774 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1775 if (err) {
1776 printf("%s: abort intr pipe failed: %s\n",
1777 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1778 }
1779 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1780 if (err) {
1781 printf("%s: close intr pipe failed: %s\n",
1782 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1783 }
1784 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1785 }
1786
1787 /* Free RX resources. */
1788 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1789 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1790 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1791 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1792 }
1793 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1794 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1795 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1796 }
1797 }
1798
1799 /* Free TX resources. */
1800 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1801 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1802 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1803 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1804 }
1805 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1806 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1807 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1808 }
1809 }
1810
1811 sc->aue_link = 0;
1812
1813 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1814 }
1815
1816 #ifdef __FreeBSD__
1817 /*
1818 * Stop all chip I/O so that the kernel's probe routines don't
1819 * get confused by errant DMAs when rebooting.
1820 */
1821 static void
1822 aue_shutdown(dev)
1823 device_ptr_t dev;
1824 {
1825 struct aue_softc *sc = USBGETSOFTC(dev);
1826
1827 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1828
1829 aue_reset(sc);
1830 aue_stop(sc);
1831 }
1832 #endif
1833