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