if_aue.c revision 1.24 1 /* $NetBSD: if_aue.c,v 1.24 2000/02/17 18:42:21 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 printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
1168 usbd_errstr(status));
1169 if (status == USBD_STALLED)
1170 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1171 goto done;
1172 }
1173
1174 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1175
1176 memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
1177
1178 if (total_len <= 4 + ETHER_CRC_LEN) {
1179 ifp->if_ierrors++;
1180 goto done;
1181 }
1182
1183 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1184
1185 /* Turn off all the non-error bits in the rx status word. */
1186 r.aue_rxstat &= AUE_RXSTAT_MASK;
1187 if (r.aue_rxstat) {
1188 ifp->if_ierrors++;
1189 goto done;
1190 }
1191
1192 /* No errors; receive the packet. */
1193 m = c->aue_mbuf;
1194 total_len -= ETHER_CRC_LEN + 4;
1195 m->m_pkthdr.len = m->m_len = total_len;
1196 ifp->if_ipackets++;
1197
1198 #if defined(__FreeBSD__)
1199 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
1200 /* Put the packet on the special USB input queue. */
1201 usb_ether_input(m);
1202
1203 return;
1204
1205 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1206 m->m_pkthdr.rcvif = ifp;
1207
1208 s = splimp();
1209
1210 /* XXX ugly */
1211 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1212 ifp->if_ierrors++;
1213 goto done1;
1214 }
1215
1216 /*
1217 * Handle BPF listeners. Let the BPF user see the packet, but
1218 * don't pass it up to the ether_input() layer unless it's
1219 * a broadcast packet, multicast packet, matches our ethernet
1220 * address or the interface is in promiscuous mode.
1221 */
1222 if (ifp->if_bpf) {
1223 struct ether_header *eh = mtod(m, struct ether_header *);
1224 BPF_MTAP(ifp, m);
1225 if ((ifp->if_flags & IFF_PROMISC) &&
1226 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1227 ETHER_ADDR_LEN) &&
1228 !(eh->ether_dhost[0] & 1)) {
1229 m_freem(m);
1230 goto done1;
1231 }
1232 }
1233
1234 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1235 __FUNCTION__, m->m_len));
1236 (*ifp->if_input)(ifp, m);
1237 done1:
1238 splx(s);
1239 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1240
1241 done:
1242
1243 /* Setup new transfer. */
1244 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1245 c, c->aue_buf, AUE_BUFSZ,
1246 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1247 USBD_NO_TIMEOUT, aue_rxeof);
1248 usbd_transfer(xfer);
1249
1250 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1251 __FUNCTION__));
1252 }
1253
1254 /*
1255 * A frame was downloaded to the chip. It's safe for us to clean up
1256 * the list buffers.
1257 */
1258
1259 static void
1260 aue_txeof(xfer, priv, status)
1261 usbd_xfer_handle xfer;
1262 usbd_private_handle priv;
1263 usbd_status status;
1264 {
1265 struct aue_chain *c = priv;
1266 struct aue_softc *sc = c->aue_sc;
1267 struct ifnet *ifp = GET_IFP(sc);
1268 int s;
1269
1270 if (sc->aue_dying)
1271 return;
1272
1273 s = splimp();
1274
1275 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1276 __FUNCTION__, status));
1277
1278 ifp->if_timer = 0;
1279 ifp->if_flags &= ~IFF_OACTIVE;
1280
1281 if (status != USBD_NORMAL_COMPLETION) {
1282 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1283 splx(s);
1284 return;
1285 }
1286 ifp->if_oerrors++;
1287 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1288 usbd_errstr(status));
1289 if (status == USBD_STALLED)
1290 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1291 splx(s);
1292 return;
1293 }
1294
1295 ifp->if_opackets++;
1296
1297 #if defined(__FreeBSD__)
1298 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1299 usb_tx_done(c->aue_mbuf);
1300 c->aue_mbuf = NULL;
1301 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1302 m_freem(c->aue_mbuf);
1303 c->aue_mbuf = NULL;
1304
1305 if (ifp->if_snd.ifq_head != NULL)
1306 aue_start(ifp);
1307 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1308
1309 splx(s);
1310 }
1311
1312 static void
1313 aue_tick(xsc)
1314 void *xsc;
1315 {
1316 struct aue_softc *sc = xsc;
1317 struct ifnet *ifp;
1318 struct mii_data *mii;
1319 int s;
1320
1321 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1322
1323 if (sc == NULL)
1324 return;
1325
1326 if (sc->aue_dying)
1327 return;
1328
1329 ifp = GET_IFP(sc);
1330 mii = GET_MII(sc);
1331 if (mii == NULL)
1332 return;
1333
1334 s = splimp();
1335
1336 mii_tick(mii);
1337 if (!sc->aue_link) {
1338 mii_pollstat(mii);
1339 if (mii->mii_media_status & IFM_ACTIVE &&
1340 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1341 DPRINTFN(2,("%s: %s: got link\n",
1342 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1343 sc->aue_link++;
1344 if (ifp->if_snd.ifq_head != NULL)
1345 aue_start(ifp);
1346 }
1347 }
1348
1349 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1350
1351 splx(s);
1352 }
1353
1354 static int
1355 aue_send(sc, m, idx)
1356 struct aue_softc *sc;
1357 struct mbuf *m;
1358 int idx;
1359 {
1360 int total_len;
1361 struct aue_chain *c;
1362 usbd_status err;
1363
1364 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1365
1366 c = &sc->aue_cdata.aue_tx_chain[idx];
1367
1368 /*
1369 * Copy the mbuf data into a contiguous buffer, leaving two
1370 * bytes at the beginning to hold the frame length.
1371 */
1372 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1373 c->aue_mbuf = m;
1374
1375 /*
1376 * The ADMtek documentation says that the packet length is
1377 * supposed to be specified in the first two bytes of the
1378 * transfer, however it actually seems to ignore this info
1379 * and base the frame size on the bulk transfer length.
1380 */
1381 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1382 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1383 total_len = m->m_pkthdr.len + 2;
1384
1385 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1386 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1387 AUE_TX_TIMEOUT, aue_txeof);
1388
1389 /* Transmit */
1390 err = usbd_transfer(c->aue_xfer);
1391 if (err != USBD_IN_PROGRESS) {
1392 aue_stop(sc);
1393 return (EIO);
1394 }
1395 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1396 __FUNCTION__, total_len));
1397
1398 sc->aue_cdata.aue_tx_cnt++;
1399
1400 return (0);
1401 }
1402
1403 static void
1404 aue_start(ifp)
1405 struct ifnet *ifp;
1406 {
1407 struct aue_softc *sc = ifp->if_softc;
1408 struct mbuf *m_head = NULL;
1409
1410 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1411 __FUNCTION__, sc->aue_link));
1412
1413 if (sc->aue_dying)
1414 return;
1415
1416 if (!sc->aue_link)
1417 return;
1418
1419 if (ifp->if_flags & IFF_OACTIVE)
1420 return;
1421
1422 IF_DEQUEUE(&ifp->if_snd, m_head);
1423 if (m_head == NULL)
1424 return;
1425
1426 if (aue_send(sc, m_head, 0)) {
1427 IF_PREPEND(&ifp->if_snd, m_head);
1428 ifp->if_flags |= IFF_OACTIVE;
1429 return;
1430 }
1431
1432 /*
1433 * If there's a BPF listener, bounce a copy of this frame
1434 * to him.
1435 */
1436 if (ifp->if_bpf)
1437 BPF_MTAP(ifp, m_head);
1438
1439 ifp->if_flags |= IFF_OACTIVE;
1440
1441 /*
1442 * Set a timeout in case the chip goes out to lunch.
1443 */
1444 ifp->if_timer = 5;
1445 }
1446
1447 static void
1448 aue_init(xsc)
1449 void *xsc;
1450 {
1451 struct aue_softc *sc = xsc;
1452 struct ifnet *ifp = GET_IFP(sc);
1453 struct mii_data *mii = GET_MII(sc);
1454 int i, s;
1455 u_char *eaddr;
1456
1457 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1458
1459 if (sc->aue_dying)
1460 return;
1461
1462 if (ifp->if_flags & IFF_RUNNING)
1463 return;
1464
1465 s = splimp();
1466
1467 /*
1468 * Cancel pending I/O and free all RX/TX buffers.
1469 */
1470 aue_reset(sc);
1471
1472 #if defined(__FreeBSD__)
1473 eaddr = sc->arpcom.ac_enaddr;
1474 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1475 eaddr = LLADDR(ifp->if_sadl);
1476 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1477 for (i = 0; i < ETHER_ADDR_LEN; i++)
1478 csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1479
1480 /* If we want promiscuous mode, set the allframes bit. */
1481 if (ifp->if_flags & IFF_PROMISC)
1482 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1483 else
1484 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1485
1486 /* Init TX ring. */
1487 if (aue_tx_list_init(sc) == ENOBUFS) {
1488 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1489 splx(s);
1490 return;
1491 }
1492
1493 /* Init RX ring. */
1494 if (aue_rx_list_init(sc) == ENOBUFS) {
1495 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1496 splx(s);
1497 return;
1498 }
1499
1500 /* Load the multicast filter. */
1501 aue_setmulti(sc);
1502
1503 /* Enable RX and TX */
1504 csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
1505 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1506 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1507
1508 mii_mediachg(mii);
1509
1510 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1511 if (aue_openpipes(sc)) {
1512 splx(s);
1513 return;
1514 }
1515 }
1516
1517 ifp->if_flags |= IFF_RUNNING;
1518 ifp->if_flags &= ~IFF_OACTIVE;
1519
1520 splx(s);
1521
1522 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1523 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1524 }
1525
1526 static int
1527 aue_openpipes(sc)
1528 struct aue_softc *sc;
1529 {
1530 struct aue_chain *c;
1531 usbd_status err;
1532 int i;
1533
1534 /* Open RX and TX pipes. */
1535 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1536 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1537 if (err) {
1538 printf("%s: open rx pipe failed: %s\n",
1539 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1540 return (EIO);
1541 }
1542 usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1543 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1544 if (err) {
1545 printf("%s: open tx pipe failed: %s\n",
1546 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1547 return (EIO);
1548 }
1549 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1550 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1551 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1552 AUE_INTR_INTERVAL);
1553 if (err) {
1554 printf("%s: open intr pipe failed: %s\n",
1555 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1556 return (EIO);
1557 }
1558
1559 /* Start up the receive pipe. */
1560 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1561 c = &sc->aue_cdata.aue_rx_chain[i];
1562 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1563 c, c->aue_buf, AUE_BUFSZ,
1564 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1565 aue_rxeof);
1566 (void)usbd_transfer(c->aue_xfer); /* XXX */
1567 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1568 __FUNCTION__));
1569
1570 }
1571 return (0);
1572 }
1573
1574 /*
1575 * Set media options.
1576 */
1577 static int
1578 aue_ifmedia_upd(ifp)
1579 struct ifnet *ifp;
1580 {
1581 struct aue_softc *sc = ifp->if_softc;
1582 struct mii_data *mii = GET_MII(sc);
1583
1584 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1585
1586 if (sc->aue_dying)
1587 return (0);
1588
1589 sc->aue_link = 0;
1590 if (mii->mii_instance) {
1591 struct mii_softc *miisc;
1592 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1593 miisc = LIST_NEXT(miisc, mii_list))
1594 mii_phy_reset(miisc);
1595 }
1596 mii_mediachg(mii);
1597
1598 return (0);
1599 }
1600
1601 /*
1602 * Report current media status.
1603 */
1604 static void
1605 aue_ifmedia_sts(ifp, ifmr)
1606 struct ifnet *ifp;
1607 struct ifmediareq *ifmr;
1608 {
1609 struct aue_softc *sc = ifp->if_softc;
1610 struct mii_data *mii = GET_MII(sc);
1611
1612 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1613
1614 mii_pollstat(mii);
1615 ifmr->ifm_active = mii->mii_media_active;
1616 ifmr->ifm_status = mii->mii_media_status;
1617 }
1618
1619 static int
1620 aue_ioctl(ifp, command, data)
1621 struct ifnet *ifp;
1622 u_long command;
1623 caddr_t data;
1624 {
1625 struct aue_softc *sc = ifp->if_softc;
1626 #if defined(__NetBSD__) || defined(__OpenBSD__)
1627 struct ifaddr *ifa = (struct ifaddr *)data;
1628 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1629 struct ifreq *ifr = (struct ifreq *)data;
1630 struct mii_data *mii;
1631 int s, error = 0;
1632
1633 if (sc->aue_dying)
1634 return (EIO);
1635
1636 s = splimp();
1637
1638 switch(command) {
1639 #if defined(__FreeBSD__)
1640 case SIOCSIFADDR:
1641 case SIOCGIFADDR:
1642 case SIOCSIFMTU:
1643 error = ether_ioctl(ifp, command, data);
1644 break;
1645 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1646 case SIOCSIFADDR:
1647 ifp->if_flags |= IFF_UP;
1648 aue_init(sc);
1649
1650 switch (ifa->ifa_addr->sa_family) {
1651 #ifdef INET
1652 case AF_INET:
1653 arp_ifinit(ifp, ifa);
1654 break;
1655 #endif /* INET */
1656 #ifdef NS
1657 case AF_NS:
1658 {
1659 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1660
1661 if (ns_nullhost(*ina))
1662 ina->x_host = *(union ns_host *)
1663 LLADDR(ifp->if_sadl);
1664 else
1665 memcpy(LLADDR(ifp->if_sadl),
1666 ina->x_host.c_host,
1667 ifp->if_addrlen);
1668 break;
1669 }
1670 #endif /* NS */
1671 }
1672 break;
1673
1674 case SIOCSIFMTU:
1675 if (ifr->ifr_mtu > ETHERMTU)
1676 error = EINVAL;
1677 else
1678 ifp->if_mtu = ifr->ifr_mtu;
1679 break;
1680
1681 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1682 case SIOCSIFFLAGS:
1683 if (ifp->if_flags & IFF_UP) {
1684 if (ifp->if_flags & IFF_RUNNING &&
1685 ifp->if_flags & IFF_PROMISC &&
1686 !(sc->aue_if_flags & IFF_PROMISC)) {
1687 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1688 } else if (ifp->if_flags & IFF_RUNNING &&
1689 !(ifp->if_flags & IFF_PROMISC) &&
1690 sc->aue_if_flags & IFF_PROMISC) {
1691 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1692 } else if (!(ifp->if_flags & IFF_RUNNING))
1693 aue_init(sc);
1694 } else {
1695 if (ifp->if_flags & IFF_RUNNING)
1696 aue_stop(sc);
1697 }
1698 sc->aue_if_flags = ifp->if_flags;
1699 error = 0;
1700 break;
1701 case SIOCADDMULTI:
1702 case SIOCDELMULTI:
1703 aue_setmulti(sc);
1704 error = 0;
1705 break;
1706 case SIOCGIFMEDIA:
1707 case SIOCSIFMEDIA:
1708 mii = GET_MII(sc);
1709 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1710 break;
1711 default:
1712 error = EINVAL;
1713 break;
1714 }
1715
1716 splx(s);
1717
1718 return (error);
1719 }
1720
1721 static void
1722 aue_watchdog(ifp)
1723 struct ifnet *ifp;
1724 {
1725 struct aue_softc *sc = ifp->if_softc;
1726
1727 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1728
1729 ifp->if_oerrors++;
1730 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1731
1732 /*
1733 * The polling business is a kludge to avoid allowing the
1734 * USB code to call tsleep() in usbd_delay_ms(), which will
1735 * kill us since the watchdog routine is invoked from
1736 * interrupt context.
1737 */
1738 usbd_set_polling(sc->aue_udev, 1);
1739 aue_stop(sc);
1740 aue_init(sc);
1741 usbd_set_polling(sc->aue_udev, 0);
1742
1743 if (ifp->if_snd.ifq_head != NULL)
1744 aue_start(ifp);
1745 }
1746
1747 /*
1748 * Stop the adapter and free any mbufs allocated to the
1749 * RX and TX lists.
1750 */
1751 static void
1752 aue_stop(sc)
1753 struct aue_softc *sc;
1754 {
1755 usbd_status err;
1756 struct ifnet *ifp;
1757 int i;
1758
1759 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1760
1761 ifp = GET_IFP(sc);
1762 ifp->if_timer = 0;
1763
1764 csr_write_1(sc, AUE_CTL0, 0);
1765 csr_write_1(sc, AUE_CTL1, 0);
1766 aue_reset(sc);
1767 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1768
1769 /* Stop transfers. */
1770 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1771 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1772 if (err) {
1773 printf("%s: abort rx pipe failed: %s\n",
1774 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1775 }
1776 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1777 if (err) {
1778 printf("%s: close rx pipe failed: %s\n",
1779 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1780 }
1781 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1782 }
1783
1784 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1785 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1786 if (err) {
1787 printf("%s: abort tx pipe failed: %s\n",
1788 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1789 }
1790 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1791 if (err) {
1792 printf("%s: close tx pipe failed: %s\n",
1793 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1794 }
1795 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1796 }
1797
1798 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1799 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1800 if (err) {
1801 printf("%s: abort intr pipe failed: %s\n",
1802 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1803 }
1804 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1805 if (err) {
1806 printf("%s: close intr pipe failed: %s\n",
1807 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1808 }
1809 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1810 }
1811
1812 /* Free RX resources. */
1813 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1814 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1815 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1816 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1817 }
1818 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1819 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1820 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1821 }
1822 }
1823
1824 /* Free TX resources. */
1825 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1826 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1827 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1828 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1829 }
1830 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1831 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1832 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1833 }
1834 }
1835
1836 sc->aue_link = 0;
1837
1838 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1839 }
1840
1841 #ifdef __FreeBSD__
1842 /*
1843 * Stop all chip I/O so that the kernel's probe routines don't
1844 * get confused by errant DMAs when rebooting.
1845 */
1846 static void
1847 aue_shutdown(dev)
1848 device_ptr_t dev;
1849 {
1850 struct aue_softc *sc = USBGETSOFTC(dev);
1851
1852 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1853
1854 aue_reset(sc);
1855 aue_stop(sc);
1856 }
1857 #endif
1858