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