if_aue.c revision 1.6 1 /* $NetBSD: if_aue.c,v 1.6 2000/01/16 15:43:24 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 */
77
78 #if defined(__NetBSD__) || defined(__OpenBSD__)
79 #include "opt_inet.h"
80 #include "opt_ns.h"
81 #include "bpfilter.h"
82 #include "rnd.h"
83 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/sockio.h>
88 #include <sys/mbuf.h>
89 #include <sys/malloc.h>
90 #include <sys/kernel.h>
91 #include <sys/socket.h>
92
93 #if defined(__FreeBSD__)
94
95 #include <net/ethernet.h>
96 #include <machine/clock.h> /* for DELAY */
97 #include <sys/bus.h>
98 /* "controller miibus0" required. See GENERIC if you get errors here. */
99 #include "miibus_if.h"
100
101 #elif defined(__NetBSD__) || defined(__OpenBSD__)
102
103 #include <sys/device.h>
104
105 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
106
107 #include <net/if.h>
108 #include <net/if_arp.h>
109 #include <net/if_dl.h>
110 #include <net/if_media.h>
111
112 #if defined(__NetBSD__) || defined(__OpenBSD__)
113 #include <net/if_ether.h>
114
115 #define bpf_mtap(ifp, m) bpf_tap((ifp)->if_bpf, mtod((m), caddr_t), (m)->m_len)
116
117 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
118
119 #if defined(__FreeBSD__) || NBPFILTER > 0
120 #include <net/bpf.h>
121 #endif
122
123 #if defined(__NetBSD__) || defined(__OpenBSD__)
124 #ifdef INET
125 #include <netinet/in.h>
126 #include <netinet/if_inarp.h>
127 #endif
128
129 #ifdef NS
130 #include <netns/ns.h>
131 #include <netns/ns_if.h>
132 #endif
133 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
134
135 #include <dev/mii/mii.h>
136 #include <dev/mii/miivar.h>
137
138 #include <dev/usb/usb.h>
139 #include <dev/usb/usbdi.h>
140 #include <dev/usb/usbdi_util.h>
141 #include <dev/usb/usbdevs.h>
142
143 #ifdef __FreeBSD__
144 #include <dev/usb/usb_ethersubr.h>
145 #endif
146
147 #include <dev/usb/if_auereg.h>
148
149 #define AUE_DEBUG
150
151 #ifdef AUE_DEBUG
152 #define DPRINTF(x) if (auedebug) logprintf x
153 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x
154 int auedebug = 1;
155 #else
156 #define DPRINTF(x)
157 #define DPRINTFN(n,x)
158 #endif
159
160 int aue_cutoff = AUE_CUTOFF;
161 #undef AUE_CUTOFF
162 #define AUE_CUTOFF aue_cutoff
163
164 /*
165 * Various supported device vendors/types and their names.
166 */
167 static struct aue_type aue_devs[] = {
168 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
169 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
170 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
171 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
172 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
173 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
174 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
175 { 0, 0 }
176 };
177
178 USB_DECLARE_DRIVER(aue);
179
180 static int aue_tx_list_init __P((struct aue_softc *));
181 static int aue_rx_list_init __P((struct aue_softc *));
182 static int aue_newbuf __P((struct aue_softc *, struct aue_chain *,
183 struct mbuf *));
184 static int aue_send __P((struct aue_softc *, struct mbuf *, int));
185 static void aue_intr __P((usbd_xfer_handle,
186 usbd_private_handle, usbd_status));
187 static void aue_rxeof __P((usbd_xfer_handle,
188 usbd_private_handle, usbd_status));
189 static void aue_txeof __P((usbd_xfer_handle,
190 usbd_private_handle, usbd_status));
191 static void aue_tick __P((void *));
192 static void aue_start __P((struct ifnet *));
193 static int aue_ioctl __P((struct ifnet *, u_long, caddr_t));
194 static void aue_init __P((void *));
195 static void aue_stop __P((struct aue_softc *));
196 static void aue_watchdog __P((struct ifnet *));
197 #ifdef __FreeBSD__
198 static void aue_shutdown __P((device_ptr_t));
199 #endif
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 u_int16_t auxmode;
550 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
551 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
552 }
553 }
554
555 #define AUE_POLY 0xEDB88320
556 #define AUE_BITS 6
557
558 static u_int32_t
559 aue_crc(addr)
560 caddr_t addr;
561 {
562 u_int32_t idx, bit, data, crc;
563
564 /* Compute CRC for the address value. */
565 crc = 0xFFFFFFFF; /* initial value */
566
567 for (idx = 0; idx < 6; idx++) {
568 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
569 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
570 }
571
572 return (crc & ((1 << AUE_BITS) - 1));
573 }
574
575 static void
576 aue_setmulti(sc)
577 struct aue_softc *sc;
578 {
579 struct ifnet *ifp;
580 #if defined(__FreeBSD__)
581 struct ifmultiaddr *ifma;
582 #elif defined(__NetBSD__) || defined(__OpenBSD__)
583 struct ether_multi *enm;
584 struct ether_multistep step;
585 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
586 u_int32_t h = 0, i;
587
588 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
589
590 ifp = GET_IFP(sc);
591
592 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
593 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
594 return;
595 }
596
597 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
598
599 /* first, zot all the existing hash bits */
600 for (i = 0; i < 8; i++)
601 csr_write_1(sc, AUE_MAR0 + i, 0);
602
603 /* now program new ones */
604 #if defined(__FreeBSD__)
605 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
606 ifma = ifma->ifma_link.le_next) {
607 if (ifma->ifma_addr->sa_family != AF_LINK)
608 continue;
609 h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
610 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
611 }
612 #elif defined(__NetBSD__) || defined(__OpenBSD__)
613 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
614 while (enm != NULL) {
615 #if 1
616 if (memcmp(enm->enm_addrlo,
617 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
618 ifp->if_flags |= IFF_ALLMULTI;
619 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
620 return;
621 }
622 #endif
623 h = aue_crc(enm->enm_addrlo);
624 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
625 ETHER_NEXT_MULTI(step, enm);
626 }
627 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
628 }
629
630 static void
631 aue_reset(sc)
632 struct aue_softc *sc;
633 {
634 int i;
635
636 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
637
638 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
639
640 for (i = 0; i < AUE_TIMEOUT; i++) {
641 if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
642 break;
643 }
644
645 if (i == AUE_TIMEOUT)
646 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
647
648 /*
649 * The PHY(s) attached to the Pegasus chip may be held
650 * in reset until we flip on the GPIO outputs. Make sure
651 * to set the GPIO pins high so that the PHY(s) will
652 * be enabled.
653 *
654 * Note: We force all of the GPIO pins low first, *then*
655 * enable the ones we want.
656 */
657 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
658 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
659
660 /* Grrr. LinkSys has to be different from everyone else. */
661 if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
662 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
663 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
664 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
665 AUE_GPIO_OUT0);
666 }
667
668 /* Wait a little while for the chip to get its brains in order. */
669 DELAY(10000); /* XXX */
670 }
671
672 /*
673 * Probe for a Pegasus chip.
674 */
675 USB_MATCH(aue)
676 {
677 USB_MATCH_START(aue, uaa);
678 struct aue_type *t;
679
680 if (uaa->iface != NULL)
681 return (UMATCH_NONE);
682
683 for (t = aue_devs; t->aue_vid != 0; t++)
684 if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
685 return (UMATCH_VENDOR_PRODUCT);
686
687 return (UMATCH_NONE);
688 }
689
690 /*
691 * Attach the interface. Allocate softc structures, do ifmedia
692 * setup and ethernet/BPF attach.
693 */
694 USB_ATTACH(aue)
695 {
696 USB_ATTACH_START(aue, sc, uaa);
697 char devinfo[1024];
698 int s;
699 u_char eaddr[ETHER_ADDR_LEN];
700 struct ifnet *ifp;
701 struct mii_data *mii;
702 usbd_device_handle dev = uaa->device;
703 usbd_interface_handle iface;
704 usb_interface_descriptor_t *id;
705 usb_endpoint_descriptor_t *ed;
706 usbd_status err;
707 int i;
708
709 #ifdef __FreeBSD__
710 bzero(sc, sizeof(struct aue_softc));
711 #endif
712
713 DPRINTFN(5,(" : uan_attach: sc=%p", sc));
714
715 usbd_devinfo(dev, 0, devinfo);
716 USB_ATTACH_SETUP;
717 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
718
719 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
720 if (err) {
721 printf("%s: setting config no failed\n",
722 USBDEVNAME(sc->aue_dev));
723 USB_ATTACH_ERROR_RETURN;
724 }
725
726 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
727 if (err) {
728 printf("%s: getting interface handle failed\n",
729 USBDEVNAME(sc->aue_dev));
730 USB_ATTACH_ERROR_RETURN;
731 }
732
733 sc->aue_udev = dev;
734 sc->aue_iface = iface;
735 sc->aue_product = uaa->product;
736 sc->aue_vendor = uaa->vendor;
737
738 id = usbd_get_interface_descriptor(iface);
739
740 /* Find endpoints. */
741 for (i = 0; i < id->bNumEndpoints; i++) {
742 ed = usbd_interface2endpoint_descriptor(iface, i);
743 if (!ed) {
744 printf("%s: couldn't get endpoint descriptor %d\n",
745 USBDEVNAME(sc->aue_dev), i);
746 USB_ATTACH_ERROR_RETURN;
747 }
748 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
749 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
750 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
751 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
752 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
753 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
754 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
755 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
756 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
757 }
758 }
759
760 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
761 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
762 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
763 USB_ATTACH_ERROR_RETURN;
764 }
765
766
767 s = splimp();
768
769 /* Reset the adapter. */
770 aue_reset(sc);
771
772 /*
773 * Get station address from the EEPROM.
774 */
775 aue_read_mac(sc, eaddr);
776
777 /*
778 * A Pegasus chip was detected. Inform the world.
779 */
780 #if defined(__FreeBSD__)
781 printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
782 eaddr, ":");
783
784 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
785
786 ifp = &sc->arpcom.ac_if;
787 ifp->if_softc = sc;
788 ifp->if_unit = sc->aue_unit;
789 ifp->if_name = "aue";
790 ifp->if_mtu = ETHERMTU;
791 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
792 ifp->if_ioctl = aue_ioctl;
793 ifp->if_output = ether_output;
794 ifp->if_start = aue_start;
795 ifp->if_watchdog = aue_watchdog;
796 ifp->if_init = aue_init;
797 ifp->if_baudrate = 10000000;
798 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
799
800 /*
801 * Do MII setup.
802 * NOTE: Doing this causes child devices to be attached to us,
803 * which we would normally disconnect at in the detach routine
804 * using device_delete_child(). However the USB code is set up
805 * such that when this driver is removed, all childred devices
806 * are removed as well. In effect, the USB code ends up detaching
807 * all of our children for us, so we don't have to do is ourselves
808 * in aue_detach(). It's important to point this out since if
809 * we *do* try to detach the child devices ourselves, we will
810 * end up getting the children deleted twice, which will crash
811 * the system.
812 */
813 if (mii_phy_probe(self, &sc->aue_miibus,
814 aue_ifmedia_upd, aue_ifmedia_sts)) {
815 printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
816 splx(s);
817 USB_ATTACH_ERROR_RETURN;
818 }
819
820 aue_qdat.ifp = ifp;
821 aue_qdat.if_rxstart = aue_rxstart;
822
823 /*
824 * Call MI attach routines.
825 */
826 if_attach(ifp);
827 ether_ifattach(ifp);
828 callout_handle_init(&sc->aue_stat_ch);
829 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
830
831 usb_register_netisr();
832
833 #elif defined(__NetBSD__) || defined(__OpenBSD__)
834
835 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
836 ether_sprintf(eaddr));
837
838 /* Initialize interface info.*/
839 ifp = &sc->aue_ec.ec_if;
840 ifp->if_softc = sc;
841 ifp->if_mtu = ETHERMTU;
842 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
843 ifp->if_ioctl = aue_ioctl;
844 ifp->if_start = aue_start;
845 ifp->if_watchdog = aue_watchdog;
846 ifp->if_baudrate = 10000000;
847 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
848
849 /* Initialize MII/media info. */
850 mii = &sc->aue_mii;
851 mii->mii_ifp = ifp;
852 mii->mii_readreg = aue_miibus_readreg;
853 mii->mii_writereg = aue_miibus_writereg;
854 mii->mii_statchg = aue_miibus_statchg;
855 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
856 mii_phy_probe(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY);
857 if (LIST_FIRST(&mii->mii_phys) == NULL) {
858 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
859 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
860 } else
861 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
862
863 /* Attach the interface. */
864 if_attach(ifp);
865 ether_ifattach(ifp, eaddr);
866
867 #if NBPFILTER > 0
868 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
869 sizeof(struct ether_header));
870 #endif
871 #if RND > 0
872 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
873 RND_TYPE_NET, 0);
874 #endif
875
876 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
877
878 splx(s);
879
880 USB_ATTACH_SUCCESS_RETURN;
881 }
882
883 USB_DETACH(aue)
884 {
885 USB_DETACH_START(aue, sc);
886 #if defined(__FreeBSD__)
887 struct ifnet *ifp;
888 int s;
889
890 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
891
892 s = splusb();
893
894 ifp = &sc->arpcom.ac_if;
895
896 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
897 if_detach(ifp);
898
899 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
900 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
901 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
902 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
903 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
904 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
905
906 splx(s);
907
908 return (0);
909 #elif defined(__NetBSD__) || defined(__OpenBSD__)
910 sc = sc; /* XXX use sc */
911 /* XXX deallocate */
912
913 #ifdef notyet
914 /*
915 * Our softc is about to go away, so drop our refernce
916 * to the ifnet.
917 */
918 if_delref(sc->aue_ec.ec_if);
919 return (0);
920 #else
921 return (EBUSY);
922 #endif
923
924 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
925 }
926
927 #if defined(__NetBSD__) || defined(__OpenBSD__)
928 int
929 aue_activate(self, act)
930 device_ptr_t self;
931 enum devact act;
932 {
933 struct aue_softc *sc = (struct aue_softc *)self;
934
935 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
936
937 switch (act) {
938 case DVACT_ACTIVATE:
939 return (EOPNOTSUPP);
940 break;
941
942 case DVACT_DEACTIVATE:
943 #ifdef notyet
944 /* First, kill off the interface. */
945 if_detach(sc->aue_ec.ec_if);
946 #endif
947 sc->aue_dying = 1;
948 break;
949 }
950 return (0);
951 }
952 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
953
954 /*
955 * Initialize an RX descriptor and attach an MBUF cluster.
956 */
957 static int
958 aue_newbuf(sc, c, m)
959 struct aue_softc *sc;
960 struct aue_chain *c;
961 struct mbuf *m;
962 {
963 struct mbuf *m_new = NULL;
964
965 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
966
967 if (m == NULL) {
968 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
969 if (m_new == NULL) {
970 printf("%s: no memory for rx list "
971 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
972 return (ENOBUFS);
973 }
974
975 MCLGET(m_new, M_DONTWAIT);
976 if (!(m_new->m_flags & M_EXT)) {
977 printf("%s: no memory for rx list "
978 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
979 m_freem(m_new);
980 return (ENOBUFS);
981 }
982 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
983 } else {
984 m_new = m;
985 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
986 m_new->m_data = m_new->m_ext.ext_buf;
987 }
988
989 m_adj(m_new, ETHER_ALIGN);
990 c->aue_mbuf = m_new;
991
992 return (0);
993 }
994
995 static int
996 aue_rx_list_init(sc)
997 struct aue_softc *sc;
998 {
999 struct aue_cdata *cd;
1000 struct aue_chain *c;
1001 int i;
1002
1003 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1004
1005 cd = &sc->aue_cdata;
1006 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1007 c = &cd->aue_rx_chain[i];
1008 c->aue_sc = sc;
1009 c->aue_idx = i;
1010 c->aue_accum = 0;
1011 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1012 return (ENOBUFS);
1013 if (c->aue_xfer == NULL) {
1014 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1015 if (c->aue_xfer == NULL)
1016 return (ENOBUFS);
1017 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1018 if (c->aue_buf == NULL)
1019 return (ENOBUFS); /* XXX free xfer */
1020 }
1021 }
1022
1023 return (0);
1024 }
1025
1026 static int
1027 aue_tx_list_init(sc)
1028 struct aue_softc *sc;
1029 {
1030 struct aue_cdata *cd;
1031 struct aue_chain *c;
1032 int i;
1033
1034 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1035
1036 cd = &sc->aue_cdata;
1037 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1038 c = &cd->aue_tx_chain[i];
1039 c->aue_sc = sc;
1040 c->aue_idx = i;
1041 c->aue_mbuf = NULL;
1042 if (c->aue_xfer == NULL) {
1043 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1044 if (c->aue_xfer == NULL)
1045 return (ENOBUFS);
1046 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1047 if (c->aue_buf == NULL)
1048 return (ENOBUFS);
1049 }
1050 }
1051
1052 return (0);
1053 }
1054
1055 static void
1056 aue_intr(xfer, priv, status)
1057 usbd_xfer_handle xfer;
1058 usbd_private_handle priv;
1059 usbd_status status;
1060 {
1061 struct aue_softc *sc = priv;
1062 struct ifnet *ifp = GET_IFP(sc);
1063 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1064
1065 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1066
1067 if (!(ifp->if_flags & IFF_RUNNING))
1068 return;
1069
1070 if (status != USBD_NORMAL_COMPLETION) {
1071 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1072 return;
1073 }
1074 printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
1075 usbd_errstr(status));
1076 if (status == USBD_STALLED)
1077 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1078 return;
1079 }
1080
1081 if (p->aue_txstat0)
1082 ifp->if_oerrors++;
1083
1084 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1085 ifp->if_collisions++;
1086 }
1087
1088 #if defined(__FreeBSD__)
1089 static void
1090 aue_rxstart(ifp)
1091 struct ifnet *ifp;
1092 {
1093 struct aue_softc *sc;
1094 struct aue_chain *c;
1095
1096 sc = ifp->if_softc;
1097 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
1098
1099 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1100 ifp->if_ierrors++;
1101 return;
1102 }
1103
1104 /* Setup new transfer. */
1105 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1106 c, mtod(c->aue_mbuf, char *), AUE_CUTOFF, USBD_SHORT_XFER_OK,
1107 USBD_NO_TIMEOUT, aue_rxeof);
1108 usbd_transfer(c->aue_xfer);
1109 }
1110 #endif
1111
1112 /*
1113 * A frame has been uploaded: pass the resulting mbuf chain up to
1114 * the higher level protocols.
1115 *
1116 * Grrr. Receiving transfers larger than about 1152 bytes sometimes
1117 * doesn't work. We get an incomplete frame. In order to avoid
1118 * this, we queue up RX transfers that are shorter than a full sized
1119 * frame. If the received frame is larger than our transfer size,
1120 * we snag the rest of the data using a second transfer. Does this
1121 * hurt performance? Yes. But after fighting with this stupid thing
1122 * for three days, I'm willing to settle. I'd rather have reliable
1123 * receive performance that fast but spotty performance.
1124 */
1125 static void
1126 aue_rxeof(xfer, priv, status)
1127 usbd_xfer_handle xfer;
1128 usbd_private_handle priv;
1129 usbd_status status;
1130 {
1131 struct aue_chain *c = priv;
1132 struct aue_softc *sc = c->aue_sc;
1133 struct ifnet *ifp = GET_IFP(sc);
1134 struct mbuf *m;
1135 u_int32_t total_len;
1136 struct aue_rxpkt r;
1137 #if defined(__NetBSD__) || defined(__OpenBSD__)
1138 int s;
1139 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1140
1141 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1142
1143 if (!(ifp->if_flags & IFF_RUNNING))
1144 return;
1145
1146 if (status != USBD_NORMAL_COMPLETION) {
1147 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1148 return;
1149 printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
1150 usbd_errstr(status));
1151 if (status == USBD_STALLED)
1152 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1153 goto done;
1154 }
1155
1156 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1157
1158 /* XXX copy data to mbuf */
1159 memcpy(mtod(c->aue_mbuf, char*) + c->aue_accum, c->aue_buf, total_len);
1160
1161 /*
1162 * See if we've already accumulated some data from
1163 * a previous transfer.
1164 */
1165 if (c->aue_accum) {
1166 total_len += c->aue_accum;
1167 c->aue_accum = 0;
1168 }
1169
1170 if (total_len <= 4 + ETHER_CRC_LEN) {
1171 ifp->if_ierrors++;
1172 goto done;
1173 }
1174
1175 m = c->aue_mbuf;
1176 memcpy(&r, mtod(m, char *) + total_len - 4, sizeof(r));
1177
1178 /* Turn off all the non-error bits in the rx status word. */
1179 r.aue_rxstat &= AUE_RXSTAT_MASK;
1180
1181 /*
1182 * Check to see if this is just the first chunk of a
1183 * split transfer. We really need a more reliable way
1184 * to detect this.
1185 */
1186 if (UGETW(r.aue_pktlen) != total_len && total_len == AUE_CUTOFF) {
1187 c->aue_accum = AUE_CUTOFF;
1188 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1189 c, c->aue_buf,
1190 AUE_CUTOFF, USBD_SHORT_XFER_OK,
1191 USBD_NO_TIMEOUT, aue_rxeof);
1192 DPRINTFN(5,("%s: %s: extra rx\n", USBDEVNAME(sc->aue_dev),
1193 __FUNCTION__));
1194 usbd_transfer(xfer);
1195 return;
1196 }
1197
1198 if (r.aue_rxstat) {
1199 ifp->if_ierrors++;
1200 goto done;
1201 }
1202
1203 /* No errors; receive the packet. */
1204 total_len -= ETHER_CRC_LEN + 4;
1205 m->m_pkthdr.len = m->m_len = total_len;
1206 ifp->if_ipackets++;
1207
1208 #if defined(__FreeBSD__)
1209 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
1210 /* Put the packet on the special USB input queue. */
1211 usb_ether_input(m);
1212
1213 return;
1214
1215 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1216 m->m_pkthdr.rcvif = ifp;
1217
1218 s = splimp();
1219
1220 /* XXX ugly */
1221 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1222 ifp->if_ierrors++;
1223 goto done1;
1224 }
1225
1226 /*
1227 * Handle BPF listeners. Let the BPF user see the packet, but
1228 * don't pass it up to the ether_input() layer unless it's
1229 * a broadcast packet, multicast packet, matches our ethernet
1230 * address or the interface is in promiscuous mode.
1231 */
1232 if (ifp->if_bpf) {
1233 struct ether_header *eh = mtod(m, struct ether_header *);
1234 bpf_mtap(ifp, m);
1235 if ((ifp->if_flags & IFF_PROMISC) &&
1236 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1237 ETHER_ADDR_LEN) &&
1238 !(eh->ether_dhost[0] & 1)) {
1239 m_freem(m);
1240 goto done1;
1241 }
1242 }
1243
1244 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1245 __FUNCTION__, m->m_len));
1246 (*ifp->if_input)(ifp, m);
1247 done1:
1248 splx(s);
1249 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1250
1251 done:
1252
1253 /* Setup new transfer. */
1254 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1255 c, c->aue_buf, AUE_CUTOFF,
1256 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1257 USBD_NO_TIMEOUT, aue_rxeof);
1258 usbd_transfer(xfer);
1259 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1260 __FUNCTION__));
1261 }
1262
1263 /*
1264 * A frame was downloaded to the chip. It's safe for us to clean up
1265 * the list buffers.
1266 */
1267
1268 static void
1269 aue_txeof(xfer, priv, status)
1270 usbd_xfer_handle xfer;
1271 usbd_private_handle priv;
1272 usbd_status status;
1273 {
1274 struct aue_chain *c = priv;
1275 struct aue_softc *sc = c->aue_sc;
1276 struct ifnet *ifp = GET_IFP(sc);
1277 int s;
1278
1279 s = splimp();
1280
1281 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1282 __FUNCTION__, status));
1283
1284 if (status != USBD_NORMAL_COMPLETION) {
1285 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1286 splx(s);
1287 return;
1288 }
1289 ifp->if_oerrors++;
1290 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1291 usbd_errstr(status));
1292 if (status == USBD_STALLED)
1293 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1294 splx(s);
1295 return;
1296 }
1297
1298 ifp->if_timer = 0;
1299 ifp->if_flags &= ~IFF_OACTIVE;
1300
1301 ifp->if_opackets++;
1302
1303 #if defined(__FreeBSD__)
1304 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1305 usb_tx_done(c->aue_mbuf);
1306 c->aue_mbuf = NULL;
1307 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1308 m_freem(c->aue_mbuf);
1309 c->aue_mbuf = NULL;
1310
1311 if (ifp->if_snd.ifq_head != NULL)
1312 aue_start(ifp);
1313 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1314
1315 splx(s);
1316 }
1317
1318 static void
1319 aue_tick(xsc)
1320 void *xsc;
1321 {
1322 struct aue_softc *sc = xsc;
1323 struct ifnet *ifp;
1324 struct mii_data *mii;
1325 int s;
1326
1327 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1328
1329 if (sc == NULL)
1330 return;
1331
1332 ifp = GET_IFP(sc);
1333 mii = GET_MII(sc);
1334 if (mii == NULL)
1335 return;
1336
1337 s = splimp();
1338
1339 mii_tick(mii);
1340 if (!sc->aue_link) {
1341 mii_pollstat(mii);
1342 if (mii->mii_media_status & IFM_ACTIVE &&
1343 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1344 DPRINTFN(2,("%s: %s: got link\n",
1345 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1346 sc->aue_link++;
1347 if (ifp->if_snd.ifq_head != NULL)
1348 aue_start(ifp);
1349 }
1350 }
1351
1352 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1353
1354 splx(s);
1355 }
1356
1357 static int
1358 aue_send(sc, m, idx)
1359 struct aue_softc *sc;
1360 struct mbuf *m;
1361 int idx;
1362 {
1363 int total_len;
1364 struct aue_chain *c;
1365 usbd_status err;
1366
1367 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1368
1369 c = &sc->aue_cdata.aue_tx_chain[idx];
1370
1371 /*
1372 * Copy the mbuf data into a contiguous buffer, leaving two
1373 * bytes at the beginning to hold the frame length.
1374 */
1375 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1376 c->aue_mbuf = m;
1377
1378 /*
1379 * The ADMtek documentation says that the packet length is
1380 * supposed to be specified in the first two bytes of the
1381 * transfer, however it actually seems to ignore this info
1382 * and base the frame size on the bulk transfer length.
1383 */
1384 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1385 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 3) & 0xE0;
1386 total_len = m->m_pkthdr.len + 2;
1387
1388 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1389 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1390 AUE_TX_TIMEOUT, aue_txeof);
1391
1392 /* Transmit */
1393 err = usbd_transfer(c->aue_xfer);
1394 if (err != USBD_IN_PROGRESS) {
1395 aue_stop(sc);
1396 return (EIO);
1397 }
1398 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1399 __FUNCTION__, total_len));
1400
1401 sc->aue_cdata.aue_tx_cnt++;
1402
1403 return (0);
1404 }
1405
1406 static void
1407 aue_start(ifp)
1408 struct ifnet *ifp;
1409 {
1410 struct aue_softc *sc = ifp->if_softc;
1411 struct mbuf *m_head = NULL;
1412
1413 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1414 __FUNCTION__, sc->aue_link));
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 struct aue_chain *c;
1455 usbd_status err;
1456 int i, s;
1457 u_char *eaddr;
1458
1459 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1460
1461 if (ifp->if_flags & IFF_RUNNING)
1462 return;
1463
1464 s = splimp();
1465
1466 /*
1467 * Cancel pending I/O and free all RX/TX buffers.
1468 */
1469 aue_reset(sc);
1470
1471 #if defined(__FreeBSD__)
1472 eaddr = sc->arpcom.ac_enaddr;
1473 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1474 eaddr = LLADDR(ifp->if_sadl);
1475 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1476 for (i = 0; i < ETHER_ADDR_LEN; i++)
1477 csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1478
1479 /* If we want promiscuous mode, set the allframes bit. */
1480 if (ifp->if_flags & IFF_PROMISC)
1481 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1482 else
1483 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1484
1485 /* Init TX ring. */
1486 if (aue_tx_list_init(sc) == ENOBUFS) {
1487 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1488 splx(s);
1489 return;
1490 }
1491
1492 /* Init RX ring. */
1493 if (aue_rx_list_init(sc) == ENOBUFS) {
1494 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1495 splx(s);
1496 return;
1497 }
1498
1499 /* Load the multicast filter. */
1500 aue_setmulti(sc);
1501
1502 /* Enable RX and TX */
1503 csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
1504 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1505 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1506
1507 mii_mediachg(mii);
1508
1509 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1510 /* Open RX and TX pipes. */
1511 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1512 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1513 if (err) {
1514 printf("%s: open rx pipe failed: %s\n",
1515 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1516 splx(s);
1517 return;
1518 }
1519 usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1520 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1521 if (err) {
1522 printf("%s: open tx pipe failed: %s\n",
1523 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1524 splx(s);
1525 return;
1526 }
1527 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1528 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1529 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr);
1530 if (err) {
1531 printf("%s: open intr pipe failed: %s\n",
1532 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1533 splx(s);
1534 return;
1535 }
1536
1537 /* Start up the receive pipe. */
1538 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1539 c = &sc->aue_cdata.aue_rx_chain[i];
1540 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1541 c, c->aue_buf, AUE_CUTOFF,
1542 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1543 aue_rxeof);
1544 usbd_transfer(c->aue_xfer);
1545 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1546 __FUNCTION__));
1547
1548 }
1549 }
1550
1551 ifp->if_flags |= IFF_RUNNING;
1552 ifp->if_flags &= ~IFF_OACTIVE;
1553
1554 splx(s);
1555
1556 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1557 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1558 }
1559
1560 /*
1561 * Set media options.
1562 */
1563 static int
1564 aue_ifmedia_upd(ifp)
1565 struct ifnet *ifp;
1566 {
1567 struct aue_softc *sc = ifp->if_softc;
1568 struct mii_data *mii = GET_MII(sc);
1569
1570 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1571
1572 sc->aue_link = 0;
1573 if (mii->mii_instance) {
1574 struct mii_softc *miisc;
1575 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1576 miisc = LIST_NEXT(miisc, mii_list))
1577 mii_phy_reset(miisc);
1578 }
1579 mii_mediachg(mii);
1580
1581 return (0);
1582 }
1583
1584 /*
1585 * Report current media status.
1586 */
1587 static void
1588 aue_ifmedia_sts(ifp, ifmr)
1589 struct ifnet *ifp;
1590 struct ifmediareq *ifmr;
1591 {
1592 struct aue_softc *sc = ifp->if_softc;
1593 struct mii_data *mii = GET_MII(sc);
1594
1595 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1596
1597 mii_pollstat(mii);
1598 ifmr->ifm_active = mii->mii_media_active;
1599 ifmr->ifm_status = mii->mii_media_status;
1600 }
1601
1602 static int
1603 aue_ioctl(ifp, command, data)
1604 struct ifnet *ifp;
1605 u_long command;
1606 caddr_t data;
1607 {
1608 struct aue_softc *sc = ifp->if_softc;
1609 #if defined(__NetBSD__) || defined(__OpenBSD__)
1610 struct ifaddr *ifa = (struct ifaddr *)data;
1611 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1612 struct ifreq *ifr = (struct ifreq *)data;
1613 struct mii_data *mii;
1614 int s, error = 0;
1615
1616 s = splimp();
1617
1618 switch(command) {
1619 #if defined(__FreeBSD__)
1620 case SIOCSIFADDR:
1621 case SIOCGIFADDR:
1622 case SIOCSIFMTU:
1623 error = ether_ioctl(ifp, command, data);
1624 break;
1625 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1626 case SIOCSIFADDR:
1627 ifp->if_flags |= IFF_UP;
1628 aue_init(sc);
1629
1630 switch (ifa->ifa_addr->sa_family) {
1631 #ifdef INET
1632 case AF_INET:
1633 arp_ifinit(ifp, ifa);
1634 break;
1635 #endif /* INET */
1636 #ifdef NS
1637 case AF_NS:
1638 {
1639 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1640
1641 if (ns_nullhost(*ina))
1642 ina->x_hsot = *(union ns_host *)
1643 LLADDR(ifp->if_sadl);
1644 else
1645 memcpy(LLADDR(ifp->if_sadl),
1646 ina->x_host.c_host,
1647 ifp->if_addrlen);
1648 break;
1649 }
1650 #endif /* NS */
1651 }
1652 break;
1653
1654 case SIOCSIFMTU:
1655 if (ifr->ifr_mtu > ETHERMTU)
1656 error = EINVAL;
1657 else
1658 ifp->if_mtu = ifr->ifr_mtu;
1659 break;
1660
1661 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1662 case SIOCSIFFLAGS:
1663 if (ifp->if_flags & IFF_UP) {
1664 if (ifp->if_flags & IFF_RUNNING &&
1665 ifp->if_flags & IFF_PROMISC &&
1666 !(sc->aue_if_flags & IFF_PROMISC)) {
1667 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1668 } else if (ifp->if_flags & IFF_RUNNING &&
1669 !(ifp->if_flags & IFF_PROMISC) &&
1670 sc->aue_if_flags & IFF_PROMISC) {
1671 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1672 } else if (!(ifp->if_flags & IFF_RUNNING))
1673 aue_init(sc);
1674 } else {
1675 if (ifp->if_flags & IFF_RUNNING)
1676 aue_stop(sc);
1677 }
1678 sc->aue_if_flags = ifp->if_flags;
1679 error = 0;
1680 break;
1681 case SIOCADDMULTI:
1682 case SIOCDELMULTI:
1683 aue_setmulti(sc);
1684 error = 0;
1685 break;
1686 case SIOCGIFMEDIA:
1687 case SIOCSIFMEDIA:
1688 mii = GET_MII(sc);
1689 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1690 break;
1691 default:
1692 error = EINVAL;
1693 break;
1694 }
1695
1696 splx(s);
1697
1698 return (error);
1699 }
1700
1701 static void
1702 aue_watchdog(ifp)
1703 struct ifnet *ifp;
1704 {
1705 struct aue_softc *sc = ifp->if_softc;
1706
1707 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1708
1709 ifp->if_oerrors++;
1710 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1711
1712 /*
1713 * The polling business is a kludge to avoid allowing the
1714 * USB code to call tsleep() in usbd_delay_ms(), which will
1715 * kill us since the watchdog routine is invoked from
1716 * interrupt context.
1717 */
1718 usbd_set_polling(sc->aue_udev, 1);
1719 aue_stop(sc);
1720 aue_init(sc);
1721 usbd_set_polling(sc->aue_udev, 0);
1722
1723 if (ifp->if_snd.ifq_head != NULL)
1724 aue_start(ifp);
1725 }
1726
1727 /*
1728 * Stop the adapter and free any mbufs allocated to the
1729 * RX and TX lists.
1730 */
1731 static void
1732 aue_stop(sc)
1733 struct aue_softc *sc;
1734 {
1735 usbd_status err;
1736 struct ifnet *ifp;
1737 int i;
1738
1739 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1740
1741 ifp = GET_IFP(sc);
1742 ifp->if_timer = 0;
1743
1744 csr_write_1(sc, AUE_CTL0, 0);
1745 csr_write_1(sc, AUE_CTL1, 0);
1746 aue_reset(sc);
1747 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1748
1749 /* Stop transfers. */
1750 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1751 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1752 if (err) {
1753 printf("%s: abort rx pipe failed: %s\n",
1754 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1755 }
1756 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1757 if (err) {
1758 printf("%s: close rx pipe failed: %s\n",
1759 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1760 }
1761 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1762 }
1763
1764 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1765 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1766 if (err) {
1767 printf("%s: abort tx pipe failed: %s\n",
1768 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1769 }
1770 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1771 if (err) {
1772 printf("%s: close tx pipe failed: %s\n",
1773 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1774 }
1775 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1776 }
1777
1778 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1779 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1780 if (err) {
1781 printf("%s: abort intr pipe failed: %s\n",
1782 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1783 }
1784 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1785 if (err) {
1786 printf("%s: close intr pipe failed: %s\n",
1787 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1788 }
1789 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1790 }
1791
1792 /* Free RX resources. */
1793 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1794 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1795 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1796 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1797 }
1798 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1799 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1800 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1801 }
1802 }
1803
1804 /* Free TX resources. */
1805 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1806 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1807 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1808 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1809 }
1810 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1811 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1812 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1813 }
1814 }
1815
1816 sc->aue_link = 0;
1817
1818 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1819 }
1820
1821 #ifdef __FreeBSD__
1822 /*
1823 * Stop all chip I/O so that the kernel's probe routines don't
1824 * get confused by errant DMAs when rebooting.
1825 */
1826 static void
1827 aue_shutdown(dev)
1828 device_ptr_t dev;
1829 {
1830 struct aue_softc *sc = USBGETSOFTC(dev);
1831
1832 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1833
1834 aue_reset(sc);
1835 aue_stop(sc);
1836 }
1837 #endif
1838