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