if_aue.c revision 1.13 1 /* $NetBSD: if_aue.c,v 1.13 2000/01/18 19:46:55 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 c->aue_accum = 0;
1005 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1006 return (ENOBUFS);
1007 if (c->aue_xfer == NULL) {
1008 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1009 if (c->aue_xfer == NULL)
1010 return (ENOBUFS);
1011 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1012 if (c->aue_buf == NULL)
1013 return (ENOBUFS); /* XXX free xfer */
1014 }
1015 }
1016
1017 return (0);
1018 }
1019
1020 static int
1021 aue_tx_list_init(sc)
1022 struct aue_softc *sc;
1023 {
1024 struct aue_cdata *cd;
1025 struct aue_chain *c;
1026 int i;
1027
1028 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1029
1030 cd = &sc->aue_cdata;
1031 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1032 c = &cd->aue_tx_chain[i];
1033 c->aue_sc = sc;
1034 c->aue_idx = i;
1035 c->aue_mbuf = NULL;
1036 if (c->aue_xfer == NULL) {
1037 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1038 if (c->aue_xfer == NULL)
1039 return (ENOBUFS);
1040 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1041 if (c->aue_buf == NULL)
1042 return (ENOBUFS);
1043 }
1044 }
1045
1046 return (0);
1047 }
1048
1049 static void
1050 aue_intr(xfer, priv, status)
1051 usbd_xfer_handle xfer;
1052 usbd_private_handle priv;
1053 usbd_status status;
1054 {
1055 struct aue_softc *sc = priv;
1056 struct ifnet *ifp = GET_IFP(sc);
1057 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1058
1059 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1060
1061 if (!(ifp->if_flags & IFF_RUNNING))
1062 return;
1063
1064 if (status != USBD_NORMAL_COMPLETION) {
1065 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1066 return;
1067 }
1068 printf("%s: usb error on intr: %s\n", USBDEVNAME(sc->aue_dev),
1069 usbd_errstr(status));
1070 if (status == USBD_STALLED)
1071 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1072 return;
1073 }
1074
1075 if (p->aue_txstat0)
1076 ifp->if_oerrors++;
1077
1078 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1079 ifp->if_collisions++;
1080 }
1081
1082 #if defined(__FreeBSD__)
1083 static void
1084 aue_rxstart(ifp)
1085 struct ifnet *ifp;
1086 {
1087 struct aue_softc *sc;
1088 struct aue_chain *c;
1089
1090 sc = ifp->if_softc;
1091 c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
1092
1093 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1094 ifp->if_ierrors++;
1095 return;
1096 }
1097
1098 /* Setup new transfer. */
1099 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1100 c, mtod(c->aue_mbuf, char *), AUE_CUTOFF, USBD_SHORT_XFER_OK,
1101 USBD_NO_TIMEOUT, aue_rxeof);
1102 usbd_transfer(c->aue_xfer);
1103 }
1104 #endif
1105
1106 /*
1107 * A frame has been uploaded: pass the resulting mbuf chain up to
1108 * the higher level protocols.
1109 *
1110 * Grrr. Receiving transfers larger than about 1152 bytes sometimes
1111 * doesn't work. We get an incomplete frame. In order to avoid
1112 * this, we queue up RX transfers that are shorter than a full sized
1113 * frame. If the received frame is larger than our transfer size,
1114 * we snag the rest of the data using a second transfer. Does this
1115 * hurt performance? Yes. But after fighting with this stupid thing
1116 * for three days, I'm willing to settle. I'd rather have reliable
1117 * receive performance that fast but spotty performance.
1118 */
1119 static void
1120 aue_rxeof(xfer, priv, status)
1121 usbd_xfer_handle xfer;
1122 usbd_private_handle priv;
1123 usbd_status status;
1124 {
1125 struct aue_chain *c = priv;
1126 struct aue_softc *sc = c->aue_sc;
1127 struct ifnet *ifp = GET_IFP(sc);
1128 struct mbuf *m;
1129 u_int32_t total_len;
1130 struct aue_rxpkt r;
1131 #if defined(__NetBSD__) || defined(__OpenBSD__)
1132 int s;
1133 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1134
1135 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1136
1137 if (!(ifp->if_flags & IFF_RUNNING))
1138 return;
1139
1140 if (status != USBD_NORMAL_COMPLETION) {
1141 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1142 return;
1143 printf("%s: usb error on rx: %s\n", USBDEVNAME(sc->aue_dev),
1144 usbd_errstr(status));
1145 if (status == USBD_STALLED)
1146 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1147 goto done;
1148 }
1149
1150 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1151
1152 /* XXX copy data to mbuf */
1153 memcpy(mtod(c->aue_mbuf, char*) + c->aue_accum, c->aue_buf, total_len);
1154
1155 /*
1156 * See if we've already accumulated some data from
1157 * a previous transfer.
1158 */
1159 if (c->aue_accum) {
1160 total_len += c->aue_accum;
1161 c->aue_accum = 0;
1162 }
1163
1164 if (total_len <= 4 + ETHER_CRC_LEN) {
1165 ifp->if_ierrors++;
1166 goto done;
1167 }
1168
1169 m = c->aue_mbuf;
1170 memcpy(&r, mtod(m, char *) + total_len - 4, sizeof(r));
1171
1172 /* Turn off all the non-error bits in the rx status word. */
1173 r.aue_rxstat &= AUE_RXSTAT_MASK;
1174
1175 /*
1176 * Check to see if this is just the first chunk of a
1177 * split transfer. We really need a more reliable way
1178 * to detect this.
1179 */
1180 if (UGETW(r.aue_pktlen) != total_len && total_len == AUE_CUTOFF) {
1181 c->aue_accum = AUE_CUTOFF;
1182 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1183 c, c->aue_buf,
1184 AUE_CUTOFF, USBD_SHORT_XFER_OK | USBD_NO_COPY,
1185 USBD_NO_TIMEOUT, aue_rxeof);
1186 DPRINTFN(5,("%s: %s: extra rx\n", USBDEVNAME(sc->aue_dev),
1187 __FUNCTION__));
1188 usbd_transfer(xfer);
1189 return;
1190 }
1191
1192 if (r.aue_rxstat) {
1193 ifp->if_ierrors++;
1194 goto done;
1195 }
1196
1197 /* No errors; receive the packet. */
1198 total_len -= ETHER_CRC_LEN + 4;
1199 m->m_pkthdr.len = m->m_len = total_len;
1200 ifp->if_ipackets++;
1201
1202 #if defined(__FreeBSD__)
1203 m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
1204 /* Put the packet on the special USB input queue. */
1205 usb_ether_input(m);
1206
1207 return;
1208
1209 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1210 m->m_pkthdr.rcvif = ifp;
1211
1212 s = splimp();
1213
1214 /* XXX ugly */
1215 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1216 ifp->if_ierrors++;
1217 goto done1;
1218 }
1219
1220 /*
1221 * Handle BPF listeners. Let the BPF user see the packet, but
1222 * don't pass it up to the ether_input() layer unless it's
1223 * a broadcast packet, multicast packet, matches our ethernet
1224 * address or the interface is in promiscuous mode.
1225 */
1226 if (ifp->if_bpf) {
1227 struct ether_header *eh = mtod(m, struct ether_header *);
1228 bpf_mtap(ifp, m);
1229 if ((ifp->if_flags & IFF_PROMISC) &&
1230 memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1231 ETHER_ADDR_LEN) &&
1232 !(eh->ether_dhost[0] & 1)) {
1233 m_freem(m);
1234 goto done1;
1235 }
1236 }
1237
1238 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1239 __FUNCTION__, m->m_len));
1240 (*ifp->if_input)(ifp, m);
1241 done1:
1242 splx(s);
1243 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1244
1245 done:
1246
1247 /* Setup new transfer. */
1248 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1249 c, c->aue_buf, AUE_CUTOFF,
1250 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1251 USBD_NO_TIMEOUT, aue_rxeof);
1252 usbd_transfer(xfer);
1253
1254 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1255 __FUNCTION__));
1256 }
1257
1258 /*
1259 * A frame was downloaded to the chip. It's safe for us to clean up
1260 * the list buffers.
1261 */
1262
1263 static void
1264 aue_txeof(xfer, priv, status)
1265 usbd_xfer_handle xfer;
1266 usbd_private_handle priv;
1267 usbd_status status;
1268 {
1269 struct aue_chain *c = priv;
1270 struct aue_softc *sc = c->aue_sc;
1271 struct ifnet *ifp = GET_IFP(sc);
1272 int s;
1273
1274 s = splimp();
1275
1276 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1277 __FUNCTION__, status));
1278
1279 ifp->if_timer = 0;
1280 ifp->if_flags &= ~IFF_OACTIVE;
1281
1282 if (status != USBD_NORMAL_COMPLETION) {
1283 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1284 splx(s);
1285 return;
1286 }
1287 ifp->if_oerrors++;
1288 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1289 usbd_errstr(status));
1290 if (status == USBD_STALLED)
1291 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1292 splx(s);
1293 return;
1294 }
1295
1296 ifp->if_opackets++;
1297
1298 #if defined(__FreeBSD__)
1299 c->aue_mbuf->m_pkthdr.rcvif = ifp;
1300 usb_tx_done(c->aue_mbuf);
1301 c->aue_mbuf = NULL;
1302 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1303 m_freem(c->aue_mbuf);
1304 c->aue_mbuf = NULL;
1305
1306 if (ifp->if_snd.ifq_head != NULL)
1307 aue_start(ifp);
1308 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1309
1310 splx(s);
1311 }
1312
1313 static void
1314 aue_tick(xsc)
1315 void *xsc;
1316 {
1317 struct aue_softc *sc = xsc;
1318 struct ifnet *ifp;
1319 struct mii_data *mii;
1320 int s;
1321
1322 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1323
1324 if (sc == NULL)
1325 return;
1326
1327 ifp = GET_IFP(sc);
1328 mii = GET_MII(sc);
1329 if (mii == NULL)
1330 return;
1331
1332 s = splimp();
1333
1334 mii_tick(mii);
1335 if (!sc->aue_link) {
1336 mii_pollstat(mii);
1337 if (mii->mii_media_status & IFM_ACTIVE &&
1338 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1339 DPRINTFN(2,("%s: %s: got link\n",
1340 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1341 sc->aue_link++;
1342 if (ifp->if_snd.ifq_head != NULL)
1343 aue_start(ifp);
1344 }
1345 }
1346
1347 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1348
1349 splx(s);
1350 }
1351
1352 static int
1353 aue_send(sc, m, idx)
1354 struct aue_softc *sc;
1355 struct mbuf *m;
1356 int idx;
1357 {
1358 int total_len;
1359 struct aue_chain *c;
1360 usbd_status err;
1361
1362 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1363
1364 c = &sc->aue_cdata.aue_tx_chain[idx];
1365
1366 /*
1367 * Copy the mbuf data into a contiguous buffer, leaving two
1368 * bytes at the beginning to hold the frame length.
1369 */
1370 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1371 c->aue_mbuf = m;
1372
1373 /*
1374 * The ADMtek documentation says that the packet length is
1375 * supposed to be specified in the first two bytes of the
1376 * transfer, however it actually seems to ignore this info
1377 * and base the frame size on the bulk transfer length.
1378 */
1379 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1380 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1381 total_len = m->m_pkthdr.len + 2;
1382
1383 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1384 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1385 AUE_TX_TIMEOUT, aue_txeof);
1386
1387 /* Transmit */
1388 err = usbd_transfer(c->aue_xfer);
1389 if (err != USBD_IN_PROGRESS) {
1390 aue_stop(sc);
1391 return (EIO);
1392 }
1393 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1394 __FUNCTION__, total_len));
1395
1396 sc->aue_cdata.aue_tx_cnt++;
1397
1398 return (0);
1399 }
1400
1401 static void
1402 aue_start(ifp)
1403 struct ifnet *ifp;
1404 {
1405 struct aue_softc *sc = ifp->if_softc;
1406 struct mbuf *m_head = NULL;
1407
1408 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1409 __FUNCTION__, sc->aue_link));
1410
1411 if (!sc->aue_link)
1412 return;
1413
1414 if (ifp->if_flags & IFF_OACTIVE)
1415 return;
1416
1417 IF_DEQUEUE(&ifp->if_snd, m_head);
1418 if (m_head == NULL)
1419 return;
1420
1421 if (aue_send(sc, m_head, 0)) {
1422 IF_PREPEND(&ifp->if_snd, m_head);
1423 ifp->if_flags |= IFF_OACTIVE;
1424 return;
1425 }
1426
1427 /*
1428 * If there's a BPF listener, bounce a copy of this frame
1429 * to him.
1430 */
1431 if (ifp->if_bpf)
1432 bpf_mtap(ifp, m_head);
1433
1434 ifp->if_flags |= IFF_OACTIVE;
1435
1436 /*
1437 * Set a timeout in case the chip goes out to lunch.
1438 */
1439 ifp->if_timer = 5;
1440 }
1441
1442 static void
1443 aue_init(xsc)
1444 void *xsc;
1445 {
1446 struct aue_softc *sc = xsc;
1447 struct ifnet *ifp = GET_IFP(sc);
1448 struct mii_data *mii = GET_MII(sc);
1449 struct aue_chain *c;
1450 usbd_status err;
1451 int i, s;
1452 u_char *eaddr;
1453
1454 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1455
1456 if (ifp->if_flags & IFF_RUNNING)
1457 return;
1458
1459 s = splimp();
1460
1461 /*
1462 * Cancel pending I/O and free all RX/TX buffers.
1463 */
1464 aue_reset(sc);
1465
1466 #if defined(__FreeBSD__)
1467 eaddr = sc->arpcom.ac_enaddr;
1468 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1469 eaddr = LLADDR(ifp->if_sadl);
1470 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1471 for (i = 0; i < ETHER_ADDR_LEN; i++)
1472 csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1473
1474 /* If we want promiscuous mode, set the allframes bit. */
1475 if (ifp->if_flags & IFF_PROMISC)
1476 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1477 else
1478 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1479
1480 /* Init TX ring. */
1481 if (aue_tx_list_init(sc) == ENOBUFS) {
1482 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1483 splx(s);
1484 return;
1485 }
1486
1487 /* Init RX ring. */
1488 if (aue_rx_list_init(sc) == ENOBUFS) {
1489 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1490 splx(s);
1491 return;
1492 }
1493
1494 /* Load the multicast filter. */
1495 aue_setmulti(sc);
1496
1497 /* Enable RX and TX */
1498 csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND|AUE_CTL0_RX_ENB);
1499 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1500 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1501
1502 mii_mediachg(mii);
1503
1504 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1505 /* Open RX and TX pipes. */
1506 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1507 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1508 if (err) {
1509 printf("%s: open rx pipe failed: %s\n",
1510 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1511 splx(s);
1512 return;
1513 }
1514 usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1515 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1516 if (err) {
1517 printf("%s: open tx pipe failed: %s\n",
1518 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1519 splx(s);
1520 return;
1521 }
1522 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1523 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1524 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr);
1525 if (err) {
1526 printf("%s: open intr pipe failed: %s\n",
1527 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1528 splx(s);
1529 return;
1530 }
1531
1532 /* Start up the receive pipe. */
1533 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1534 c = &sc->aue_cdata.aue_rx_chain[i];
1535 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1536 c, c->aue_buf, AUE_CUTOFF,
1537 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1538 aue_rxeof);
1539 usbd_transfer(c->aue_xfer);
1540 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1541 __FUNCTION__));
1542
1543 }
1544 }
1545
1546 ifp->if_flags |= IFF_RUNNING;
1547 ifp->if_flags &= ~IFF_OACTIVE;
1548
1549 splx(s);
1550
1551 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1552 usb_timeout(aue_tick, sc, hz, sc->aue_stat_ch);
1553 }
1554
1555 /*
1556 * Set media options.
1557 */
1558 static int
1559 aue_ifmedia_upd(ifp)
1560 struct ifnet *ifp;
1561 {
1562 struct aue_softc *sc = ifp->if_softc;
1563 struct mii_data *mii = GET_MII(sc);
1564
1565 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1566
1567 sc->aue_link = 0;
1568 if (mii->mii_instance) {
1569 struct mii_softc *miisc;
1570 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1571 miisc = LIST_NEXT(miisc, mii_list))
1572 mii_phy_reset(miisc);
1573 }
1574 mii_mediachg(mii);
1575
1576 return (0);
1577 }
1578
1579 /*
1580 * Report current media status.
1581 */
1582 static void
1583 aue_ifmedia_sts(ifp, ifmr)
1584 struct ifnet *ifp;
1585 struct ifmediareq *ifmr;
1586 {
1587 struct aue_softc *sc = ifp->if_softc;
1588 struct mii_data *mii = GET_MII(sc);
1589
1590 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1591
1592 mii_pollstat(mii);
1593 ifmr->ifm_active = mii->mii_media_active;
1594 ifmr->ifm_status = mii->mii_media_status;
1595 }
1596
1597 static int
1598 aue_ioctl(ifp, command, data)
1599 struct ifnet *ifp;
1600 u_long command;
1601 caddr_t data;
1602 {
1603 struct aue_softc *sc = ifp->if_softc;
1604 #if defined(__NetBSD__) || defined(__OpenBSD__)
1605 struct ifaddr *ifa = (struct ifaddr *)data;
1606 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1607 struct ifreq *ifr = (struct ifreq *)data;
1608 struct mii_data *mii;
1609 int s, error = 0;
1610
1611 s = splimp();
1612
1613 switch(command) {
1614 #if defined(__FreeBSD__)
1615 case SIOCSIFADDR:
1616 case SIOCGIFADDR:
1617 case SIOCSIFMTU:
1618 error = ether_ioctl(ifp, command, data);
1619 break;
1620 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1621 case SIOCSIFADDR:
1622 ifp->if_flags |= IFF_UP;
1623 aue_init(sc);
1624
1625 switch (ifa->ifa_addr->sa_family) {
1626 #ifdef INET
1627 case AF_INET:
1628 arp_ifinit(ifp, ifa);
1629 break;
1630 #endif /* INET */
1631 #ifdef NS
1632 case AF_NS:
1633 {
1634 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1635
1636 if (ns_nullhost(*ina))
1637 ina->x_host = *(union ns_host *)
1638 LLADDR(ifp->if_sadl);
1639 else
1640 memcpy(LLADDR(ifp->if_sadl),
1641 ina->x_host.c_host,
1642 ifp->if_addrlen);
1643 break;
1644 }
1645 #endif /* NS */
1646 }
1647 break;
1648
1649 case SIOCSIFMTU:
1650 if (ifr->ifr_mtu > ETHERMTU)
1651 error = EINVAL;
1652 else
1653 ifp->if_mtu = ifr->ifr_mtu;
1654 break;
1655
1656 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1657 case SIOCSIFFLAGS:
1658 if (ifp->if_flags & IFF_UP) {
1659 if (ifp->if_flags & IFF_RUNNING &&
1660 ifp->if_flags & IFF_PROMISC &&
1661 !(sc->aue_if_flags & IFF_PROMISC)) {
1662 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1663 } else if (ifp->if_flags & IFF_RUNNING &&
1664 !(ifp->if_flags & IFF_PROMISC) &&
1665 sc->aue_if_flags & IFF_PROMISC) {
1666 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1667 } else if (!(ifp->if_flags & IFF_RUNNING))
1668 aue_init(sc);
1669 } else {
1670 if (ifp->if_flags & IFF_RUNNING)
1671 aue_stop(sc);
1672 }
1673 sc->aue_if_flags = ifp->if_flags;
1674 error = 0;
1675 break;
1676 case SIOCADDMULTI:
1677 case SIOCDELMULTI:
1678 aue_setmulti(sc);
1679 error = 0;
1680 break;
1681 case SIOCGIFMEDIA:
1682 case SIOCSIFMEDIA:
1683 mii = GET_MII(sc);
1684 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1685 break;
1686 default:
1687 error = EINVAL;
1688 break;
1689 }
1690
1691 splx(s);
1692
1693 return (error);
1694 }
1695
1696 static void
1697 aue_watchdog(ifp)
1698 struct ifnet *ifp;
1699 {
1700 struct aue_softc *sc = ifp->if_softc;
1701
1702 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1703
1704 ifp->if_oerrors++;
1705 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1706
1707 /*
1708 * The polling business is a kludge to avoid allowing the
1709 * USB code to call tsleep() in usbd_delay_ms(), which will
1710 * kill us since the watchdog routine is invoked from
1711 * interrupt context.
1712 */
1713 usbd_set_polling(sc->aue_udev, 1);
1714 aue_stop(sc);
1715 aue_init(sc);
1716 usbd_set_polling(sc->aue_udev, 0);
1717
1718 if (ifp->if_snd.ifq_head != NULL)
1719 aue_start(ifp);
1720 }
1721
1722 /*
1723 * Stop the adapter and free any mbufs allocated to the
1724 * RX and TX lists.
1725 */
1726 static void
1727 aue_stop(sc)
1728 struct aue_softc *sc;
1729 {
1730 usbd_status err;
1731 struct ifnet *ifp;
1732 int i;
1733
1734 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1735
1736 ifp = GET_IFP(sc);
1737 ifp->if_timer = 0;
1738
1739 csr_write_1(sc, AUE_CTL0, 0);
1740 csr_write_1(sc, AUE_CTL1, 0);
1741 aue_reset(sc);
1742 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
1743
1744 /* Stop transfers. */
1745 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1746 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1747 if (err) {
1748 printf("%s: abort rx pipe failed: %s\n",
1749 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1750 }
1751 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1752 if (err) {
1753 printf("%s: close rx pipe failed: %s\n",
1754 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1755 }
1756 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1757 }
1758
1759 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1760 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1761 if (err) {
1762 printf("%s: abort tx pipe failed: %s\n",
1763 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1764 }
1765 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1766 if (err) {
1767 printf("%s: close tx pipe failed: %s\n",
1768 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1769 }
1770 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1771 }
1772
1773 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1774 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1775 if (err) {
1776 printf("%s: abort intr pipe failed: %s\n",
1777 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1778 }
1779 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1780 if (err) {
1781 printf("%s: close intr pipe failed: %s\n",
1782 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1783 }
1784 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1785 }
1786
1787 /* Free RX resources. */
1788 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1789 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1790 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1791 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1792 }
1793 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1794 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1795 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1796 }
1797 }
1798
1799 /* Free TX resources. */
1800 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1801 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1802 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1803 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1804 }
1805 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1806 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1807 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1808 }
1809 }
1810
1811 sc->aue_link = 0;
1812
1813 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1814 }
1815
1816 #ifdef __FreeBSD__
1817 /*
1818 * Stop all chip I/O so that the kernel's probe routines don't
1819 * get confused by errant DMAs when rebooting.
1820 */
1821 static void
1822 aue_shutdown(dev)
1823 device_ptr_t dev;
1824 {
1825 struct aue_softc *sc = USBGETSOFTC(dev);
1826
1827 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1828
1829 aue_reset(sc);
1830 aue_stop(sc);
1831 }
1832 #endif
1833