if_aue.c revision 1.8 1 /* $NetBSD: if_aue.c,v 1.8 2000/01/16 16:07:42 augustss Exp $ */
2 /*
3 * Copyright (c) 1997, 1998, 1999, 2000
4 * Bill Paul <wpaul (at) ee.columbia.edu>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Bill Paul.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
34 */
35
36 /*
37 * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available
38 * from http://www.admtek.com.tw.
39 *
40 * Written by Bill Paul <wpaul (at) ee.columbia.edu>
41 * Electrical Engineering Department
42 * Columbia University, New York City
43 */
44
45 /*
46 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
47 * support: the control endpoint for reading/writing registers, burst
48 * read endpoint for packet reception, burst write for packet transmission
49 * and one for "interrupts." The chip uses the same RX filter scheme
50 * as the other ADMtek ethernet parts: one perfect filter entry for the
51 * the station address and a 64-bit multicast hash table. The chip supports
52 * both MII and HomePNA attachments.
53 *
54 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
55 * you're never really going to get 100Mbps speeds from this device. I
56 * think the idea is to allow the device to connect to 10 or 100Mbps
57 * networks, not necessarily to provide 100Mbps performance. Also, since
58 * the controller uses an external PHY chip, it's possible that board
59 * designers might simply choose a 10Mbps PHY.
60 *
61 * Registers are accessed using usbd_do_request(). Packet transfers are
62 * done using usbd_transfer() and friends.
63 */
64
65 /*
66 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
67 */
68
69 /*
70 * TODO:
71 * better error messages from rxstat
72 * split out if_auevar.h
73 * add thread to avoid register reads from interrupt context
74 * more error checks
75 * investigate short rx problem
76 */
77
78 #if defined(__NetBSD__) || defined(__OpenBSD__)
79 #include "opt_inet.h"
80 #include "opt_ns.h"
81 #include "bpfilter.h"
82 #include "rnd.h"
83 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/sockio.h>
88 #include <sys/mbuf.h>
89 #include <sys/malloc.h>
90 #include <sys/kernel.h>
91 #include <sys/socket.h>
92
93 #if defined(__FreeBSD__)
94
95 #include <net/ethernet.h>
96 #include <machine/clock.h> /* for DELAY */
97 #include <sys/bus.h>
98 /* "controller miibus0" required. See GENERIC if you get errors here. */
99 #include "miibus_if.h"
100
101 #elif defined(__NetBSD__) || defined(__OpenBSD__)
102
103 #include <sys/device.h>
104
105 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
106
107 #include <net/if.h>
108 #include <net/if_arp.h>
109 #include <net/if_dl.h>
110 #include <net/if_media.h>
111
112 #if defined(__NetBSD__) || defined(__OpenBSD__)
113 #include <net/if_ether.h>
114
115 #define bpf_mtap(ifp, m) bpf_tap((ifp)->if_bpf, mtod((m), caddr_t), (m)->m_len)
116
117 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
118
119 #if defined(__FreeBSD__) || NBPFILTER > 0
120 #include <net/bpf.h>
121 #endif
122
123 #if defined(__NetBSD__) || defined(__OpenBSD__)
124 #ifdef INET
125 #include <netinet/in.h>
126 #include <netinet/if_inarp.h>
127 #endif
128
129 #ifdef NS
130 #include <netns/ns.h>
131 #include <netns/ns_if.h>
132 #endif
133 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
134
135 #include <dev/mii/mii.h>
136 #include <dev/mii/miivar.h>
137
138 #include <dev/usb/usb.h>
139 #include <dev/usb/usbdi.h>
140 #include <dev/usb/usbdi_util.h>
141 #include <dev/usb/usbdevs.h>
142
143 #ifdef __FreeBSD__
144 #include <dev/usb/usb_ethersubr.h>
145 #endif
146
147 #include <dev/usb/if_auereg.h>
148
149 #ifdef AUE_DEBUG
150 #define DPRINTF(x) if (auedebug) logprintf x
151 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x
152 int auedebug = 0;
153 #else
154 #define DPRINTF(x)
155 #define DPRINTFN(n,x)
156 #endif
157
158 /*
159 * Various supported device vendors/types and their names.
160 */
161 static struct aue_type aue_devs[] = {
162 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100 },
163 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX },
164 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX },
165 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS },
166 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX },
167 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA },
168 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB },
169 { 0, 0 }
170 };
171
172 USB_DECLARE_DRIVER(aue);
173
174 static int aue_tx_list_init __P((struct aue_softc *));
175 static int aue_rx_list_init __P((struct aue_softc *));
176 static int aue_newbuf __P((struct aue_softc *, struct aue_chain *,
177 struct mbuf *));
178 static int aue_send __P((struct aue_softc *, struct mbuf *, int));
179 static void aue_intr __P((usbd_xfer_handle,
180 usbd_private_handle, usbd_status));
181 static void aue_rxeof __P((usbd_xfer_handle,
182 usbd_private_handle, usbd_status));
183 static void aue_txeof __P((usbd_xfer_handle,
184 usbd_private_handle, usbd_status));
185 static void aue_tick __P((void *));
186 static void aue_start __P((struct ifnet *));
187 static int aue_ioctl __P((struct ifnet *, u_long, caddr_t));
188 static void aue_init __P((void *));
189 static void aue_stop __P((struct aue_softc *));
190 static void aue_watchdog __P((struct ifnet *));
191 #ifdef __FreeBSD__
192 static void aue_shutdown __P((device_ptr_t));
193 #endif
194 static int aue_ifmedia_upd __P((struct ifnet *));
195 static void aue_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
196
197 static int aue_eeprom_getword __P((struct aue_softc *, int));
198 static void aue_read_mac __P((struct aue_softc *, u_char *));
199 static int aue_miibus_readreg __P((device_ptr_t, int, int));
200 #if defined(__FreeBSD__)
201 static int aue_miibus_writereg __P((device_ptr_t, int, int, int));
202 #elif defined(__NetBSD__) || defined(__OpenBSD__)
203 static void aue_miibus_writereg __P((device_ptr_t, int, int, int));
204 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
205 static void aue_miibus_statchg __P((device_ptr_t));
206
207 static void aue_setmulti __P((struct aue_softc *));
208 static u_int32_t aue_crc __P((caddr_t));
209 static void aue_reset __P((struct aue_softc *));
210
211 static int csr_read_1 __P((struct aue_softc *, int));
212 static int csr_write_1 __P((struct aue_softc *, int, int));
213 static int csr_read_2 __P((struct aue_softc *, int));
214 static int csr_write_2 __P((struct aue_softc *, int, int));
215
216 #if defined(__FreeBSD__)
217 #if !defined(lint)
218 static const char rcsid[] =
219 "$FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $";
220 #endif
221
222 static void aue_rxstart __P((struct ifnet *));
223
224 static struct usb_qdat aue_qdat;
225
226 static device_method_t aue_methods[] = {
227 /* Device interface */
228 DEVMETHOD(device_probe, aue_match),
229 DEVMETHOD(device_attach, aue_attach),
230 DEVMETHOD(device_detach, aue_detach),
231 DEVMETHOD(device_shutdown, aue_shutdown),
232
233 /* bus interface */
234 DEVMETHOD(bus_print_child, bus_generic_print_child),
235 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
236
237 /* MII interface */
238 DEVMETHOD(miibus_readreg, aue_miibus_readreg),
239 DEVMETHOD(miibus_writereg, aue_miibus_writereg),
240 DEVMETHOD(miibus_statchg, aue_miibus_statchg),
241
242 { 0, 0 }
243 };
244
245 static driver_t aue_driver = {
246 "aue",
247 aue_methods,
248 sizeof(struct aue_softc)
249 };
250
251 static devclass_t aue_devclass;
252
253 DRIVER_MODULE(if_aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
254 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
255
256 #endif /* __FreeBSD__ */
257
258 #define AUE_DO_REQUEST(dev, req, data) usbd_do_request_flags(dev, req, data, USBD_NO_TSLEEP, NULL)
259
260 #define AUE_SETBIT(sc, reg, x) \
261 csr_write_1(sc, reg, csr_read_1(sc, reg) | (x))
262
263 #define AUE_CLRBIT(sc, reg, x) \
264 csr_write_1(sc, reg, csr_read_1(sc, reg) & ~(x))
265
266 static int
267 csr_read_1(sc, reg)
268 struct aue_softc *sc;
269 int reg;
270 {
271 usb_device_request_t req;
272 usbd_status err;
273 uByte val = 0;
274 int s;
275
276 req.bmRequestType = UT_READ_VENDOR_DEVICE;
277 req.bRequest = AUE_UR_READREG;
278 USETW(req.wValue, 0);
279 USETW(req.wIndex, reg);
280 USETW(req.wLength, 1);
281
282 s = splusb();
283 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
284 splx(s);
285
286 if (err)
287 return (0);
288
289 return (val);
290 }
291
292 static int
293 csr_read_2(sc, reg)
294 struct aue_softc *sc;
295 int reg;
296 {
297 usb_device_request_t req;
298 usbd_status err;
299 uWord val;
300 int s;
301
302 req.bmRequestType = UT_READ_VENDOR_DEVICE;
303 req.bRequest = AUE_UR_READREG;
304 USETW(req.wValue, 0);
305 USETW(req.wIndex, reg);
306 USETW(req.wLength, 2);
307
308 s = splusb();
309 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
310 splx(s);
311
312 if (err)
313 return (0);
314
315 return (UGETW(val));
316 }
317
318 static int
319 csr_write_1(sc, reg, aval)
320 struct aue_softc *sc;
321 int reg, aval;
322 {
323 usb_device_request_t req;
324 usbd_status err;
325 int s;
326 uByte val;
327
328 val = aval;
329 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
330 req.bRequest = AUE_UR_WRITEREG;
331 USETW(req.wValue, val);
332 USETW(req.wIndex, reg);
333 USETW(req.wLength, 1);
334
335 s = splusb();
336 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
337 splx(s);
338
339 if (err)
340 return (-1);
341
342 return (0);
343 }
344
345 static int
346 csr_write_2(sc, reg, aval)
347 struct aue_softc *sc;
348 int reg, aval;
349 {
350 usb_device_request_t req;
351 usbd_status err;
352 int s;
353 uWord val;
354
355 USETW(val, aval);
356 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
357 req.bRequest = AUE_UR_WRITEREG;
358 USETW(req.wValue, aval);
359 USETW(req.wIndex, reg);
360 USETW(req.wLength, 2);
361
362 s = splusb();
363 err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
364 splx(s);
365
366 if (err)
367 return (-1);
368
369 return (0);
370 }
371
372 /*
373 * Read a word of data stored in the EEPROM at address 'addr.'
374 */
375 static int
376 aue_eeprom_getword(sc, addr)
377 struct aue_softc *sc;
378 int addr;
379 {
380 int i;
381
382 csr_write_1(sc, AUE_EE_REG, addr);
383 csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
384
385 for (i = 0; i < AUE_TIMEOUT; i++) {
386 if (csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
387 break;
388 }
389
390 if (i == AUE_TIMEOUT) {
391 printf("%s: EEPROM read timed out\n",
392 USBDEVNAME(sc->aue_dev));
393 }
394
395 return (csr_read_2(sc, AUE_EE_DATA));
396 }
397
398 /*
399 * Read the MAC from the EEPROM. It's at offset 0.
400 */
401 static void
402 aue_read_mac(sc, dest)
403 struct aue_softc *sc;
404 u_char *dest;
405 {
406 int i;
407 int off = 0;
408 int word;
409
410 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
411
412 for (i = 0; i < 3; i++) {
413 word = aue_eeprom_getword(sc, off + i);
414 dest[2 * i] = (u_char)word;
415 dest[2 * i + 1] = (u_char)(word >> 8);
416 }
417 }
418
419 static int
420 aue_miibus_readreg(dev, phy, reg)
421 device_ptr_t dev;
422 int phy, reg;
423 {
424 struct aue_softc *sc = USBGETSOFTC(dev);
425 int i;
426 u_int16_t val;
427
428 /*
429 * The Am79C901 HomePNA PHY actually contains
430 * two transceivers: a 1Mbps HomePNA PHY and a
431 * 10Mbps full/half duplex ethernet PHY with
432 * NWAY autoneg. However in the ADMtek adapter,
433 * only the 1Mbps PHY is actually connected to
434 * anything, so we ignore the 10Mbps one. It
435 * happens to be configured for MII address 3,
436 * so we filter that out.
437 */
438 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
439 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
440 if (phy != 1)
441 return (0);
442 }
443
444 csr_write_1(sc, AUE_PHY_ADDR, phy);
445 csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
446
447 for (i = 0; i < AUE_TIMEOUT; i++) {
448 if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
449 break;
450 }
451
452 if (i == AUE_TIMEOUT) {
453 printf("%s: MII read timed out\n",
454 USBDEVNAME(sc->aue_dev));
455 }
456
457 val = csr_read_2(sc, AUE_PHY_DATA);
458
459 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
460 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val));
461
462 return (val);
463 }
464
465 #if defined(__FreeBSD__)
466 static int
467 #elif defined(__NetBSD__) || defined(__OpenBSD__)
468 static void
469 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
470 aue_miibus_writereg(dev, phy, reg, data)
471 device_ptr_t dev;
472 int phy, reg, data;
473 {
474 struct aue_softc *sc = USBGETSOFTC(dev);
475 int i;
476
477 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
478 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
479 if (phy == 3)
480 #if defined(__FreeBSD__)
481 return (0);
482 #elif defined(__NetBSD__) || defined(__OpenBSD__)
483 return;
484 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
485 }
486
487 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
488 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data));
489
490 csr_write_2(sc, AUE_PHY_DATA, data);
491 csr_write_1(sc, AUE_PHY_ADDR, phy);
492 csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
493
494 for (i = 0; i < AUE_TIMEOUT; i++) {
495 if (csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
496 break;
497 }
498
499 if (i == AUE_TIMEOUT) {
500 printf("%s: MII read timed out\n",
501 USBDEVNAME(sc->aue_dev));
502 }
503
504 #if defined(__FreeBSD__)
505 return (0);
506 #endif
507 }
508
509 static void
510 aue_miibus_statchg(dev)
511 device_ptr_t dev;
512 {
513 struct aue_softc *sc = USBGETSOFTC(dev);
514 struct mii_data *mii = GET_MII(sc);
515 struct ifnet *ifp = GET_IFP(sc);
516
517 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
518
519 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
520
521 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
522 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
523 ifp->if_baudrate = 100000000;
524 } else {
525 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
526 ifp->if_baudrate = 10000000;
527 }
528
529 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
530 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
531 else
532 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
533
534 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
535
536 /*
537 * Set the LED modes on the LinkSys adapter.
538 * This turns on the 'dual link LED' bin in the auxmode
539 * register of the Broadcom PHY.
540 */
541 if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
542 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
543 u_int16_t auxmode;
544 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
545 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
546 }
547 }
548
549 #define AUE_POLY 0xEDB88320
550 #define AUE_BITS 6
551
552 static u_int32_t
553 aue_crc(addr)
554 caddr_t addr;
555 {
556 u_int32_t idx, bit, data, crc;
557
558 /* Compute CRC for the address value. */
559 crc = 0xFFFFFFFF; /* initial value */
560
561 for (idx = 0; idx < 6; idx++) {
562 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
563 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
564 }
565
566 return (crc & ((1 << AUE_BITS) - 1));
567 }
568
569 static void
570 aue_setmulti(sc)
571 struct aue_softc *sc;
572 {
573 struct ifnet *ifp;
574 #if defined(__FreeBSD__)
575 struct ifmultiaddr *ifma;
576 #elif defined(__NetBSD__) || defined(__OpenBSD__)
577 struct ether_multi *enm;
578 struct ether_multistep step;
579 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
580 u_int32_t h = 0, i;
581
582 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
583
584 ifp = GET_IFP(sc);
585
586 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
587 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
588 return;
589 }
590
591 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
592
593 /* first, zot all the existing hash bits */
594 for (i = 0; i < 8; i++)
595 csr_write_1(sc, AUE_MAR0 + i, 0);
596
597 /* now program new ones */
598 #if defined(__FreeBSD__)
599 for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
600 ifma = ifma->ifma_link.le_next) {
601 if (ifma->ifma_addr->sa_family != AF_LINK)
602 continue;
603 h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
604 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
605 }
606 #elif defined(__NetBSD__) || defined(__OpenBSD__)
607 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
608 while (enm != NULL) {
609 #if 1
610 if (memcmp(enm->enm_addrlo,
611 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
612 ifp->if_flags |= IFF_ALLMULTI;
613 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
614 return;
615 }
616 #endif
617 h = aue_crc(enm->enm_addrlo);
618 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
619 ETHER_NEXT_MULTI(step, enm);
620 }
621 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
622 }
623
624 static void
625 aue_reset(sc)
626 struct aue_softc *sc;
627 {
628 int i;
629
630 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
631
632 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
633
634 for (i = 0; i < AUE_TIMEOUT; i++) {
635 if (!(csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
636 break;
637 }
638
639 if (i == AUE_TIMEOUT)
640 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
641
642 /*
643 * The PHY(s) attached to the Pegasus chip may be held
644 * in reset until we flip on the GPIO outputs. Make sure
645 * to set the GPIO pins high so that the PHY(s) will
646 * be enabled.
647 *
648 * Note: We force all of the GPIO pins low first, *then*
649 * enable the ones we want.
650 */
651 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0);
652 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_OUT0|AUE_GPIO_SEL0|AUE_GPIO_SEL1);
653
654 /* Grrr. LinkSys has to be different from everyone else. */
655 if (sc->aue_vendor == USB_VENDOR_LINKSYS &&
656 sc->aue_product == USB_PRODUCT_LINKSYS_USB100TX) {
657 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1);
658 csr_write_1(sc, AUE_GPIO0, AUE_GPIO_SEL0|AUE_GPIO_SEL1|
659 AUE_GPIO_OUT0);
660 }
661
662 /* Wait a little while for the chip to get its brains in order. */
663 DELAY(10000); /* XXX */
664 }
665
666 /*
667 * Probe for a Pegasus chip.
668 */
669 USB_MATCH(aue)
670 {
671 USB_MATCH_START(aue, uaa);
672 struct aue_type *t;
673
674 if (uaa->iface != NULL)
675 return (UMATCH_NONE);
676
677 for (t = aue_devs; t->aue_vid != 0; t++)
678 if (uaa->vendor == t->aue_vid && uaa->product == t->aue_did)
679 return (UMATCH_VENDOR_PRODUCT);
680
681 return (UMATCH_NONE);
682 }
683
684 /*
685 * Attach the interface. Allocate softc structures, do ifmedia
686 * setup and ethernet/BPF attach.
687 */
688 USB_ATTACH(aue)
689 {
690 USB_ATTACH_START(aue, sc, uaa);
691 char devinfo[1024];
692 int s;
693 u_char eaddr[ETHER_ADDR_LEN];
694 struct ifnet *ifp;
695 struct mii_data *mii;
696 usbd_device_handle dev = uaa->device;
697 usbd_interface_handle iface;
698 usb_interface_descriptor_t *id;
699 usb_endpoint_descriptor_t *ed;
700 usbd_status err;
701 int i;
702
703 #ifdef __FreeBSD__
704 bzero(sc, sizeof(struct aue_softc));
705 #endif
706
707 DPRINTFN(5,(" : uan_attach: sc=%p", sc));
708
709 usbd_devinfo(dev, 0, devinfo);
710 USB_ATTACH_SETUP;
711 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
712
713 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
714 if (err) {
715 printf("%s: setting config no failed\n",
716 USBDEVNAME(sc->aue_dev));
717 USB_ATTACH_ERROR_RETURN;
718 }
719
720 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
721 if (err) {
722 printf("%s: getting interface handle failed\n",
723 USBDEVNAME(sc->aue_dev));
724 USB_ATTACH_ERROR_RETURN;
725 }
726
727 sc->aue_udev = dev;
728 sc->aue_iface = iface;
729 sc->aue_product = uaa->product;
730 sc->aue_vendor = uaa->vendor;
731
732 id = usbd_get_interface_descriptor(iface);
733
734 /* Find endpoints. */
735 for (i = 0; i < id->bNumEndpoints; i++) {
736 ed = usbd_interface2endpoint_descriptor(iface, i);
737 if (!ed) {
738 printf("%s: couldn't get endpoint descriptor %d\n",
739 USBDEVNAME(sc->aue_dev), i);
740 USB_ATTACH_ERROR_RETURN;
741 }
742 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
743 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
744 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
745 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
746 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
747 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
748 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
749 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
750 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
751 }
752 }
753
754 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
755 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
756 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
757 USB_ATTACH_ERROR_RETURN;
758 }
759
760
761 s = splimp();
762
763 /* Reset the adapter. */
764 aue_reset(sc);
765
766 /*
767 * Get station address from the EEPROM.
768 */
769 aue_read_mac(sc, eaddr);
770
771 /*
772 * A Pegasus chip was detected. Inform the world.
773 */
774 #if defined(__FreeBSD__)
775 printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
776 eaddr, ":");
777
778 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
779
780 ifp = &sc->arpcom.ac_if;
781 ifp->if_softc = sc;
782 ifp->if_unit = sc->aue_unit;
783 ifp->if_name = "aue";
784 ifp->if_mtu = ETHERMTU;
785 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
786 ifp->if_ioctl = aue_ioctl;
787 ifp->if_output = ether_output;
788 ifp->if_start = aue_start;
789 ifp->if_watchdog = aue_watchdog;
790 ifp->if_init = aue_init;
791 ifp->if_baudrate = 10000000;
792 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
793
794 /*
795 * Do MII setup.
796 * NOTE: Doing this causes child devices to be attached to us,
797 * which we would normally disconnect at in the detach routine
798 * using device_delete_child(). However the USB code is set up
799 * such that when this driver is removed, all childred devices
800 * are removed as well. In effect, the USB code ends up detaching
801 * all of our children for us, so we don't have to do is ourselves
802 * in aue_detach(). It's important to point this out since if
803 * we *do* try to detach the child devices ourselves, we will
804 * end up getting the children deleted twice, which will crash
805 * the system.
806 */
807 if (mii_phy_probe(self, &sc->aue_miibus,
808 aue_ifmedia_upd, aue_ifmedia_sts)) {
809 printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
810 splx(s);
811 USB_ATTACH_ERROR_RETURN;
812 }
813
814 aue_qdat.ifp = ifp;
815 aue_qdat.if_rxstart = aue_rxstart;
816
817 /*
818 * Call MI attach routines.
819 */
820 if_attach(ifp);
821 ether_ifattach(ifp);
822 callout_handle_init(&sc->aue_stat_ch);
823 bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
824
825 usb_register_netisr();
826
827 #elif defined(__NetBSD__) || defined(__OpenBSD__)
828
829 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
830 ether_sprintf(eaddr));
831
832 /* Initialize interface info.*/
833 ifp = &sc->aue_ec.ec_if;
834 ifp->if_softc = sc;
835 ifp->if_mtu = ETHERMTU;
836 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
837 ifp->if_ioctl = aue_ioctl;
838 ifp->if_start = aue_start;
839 ifp->if_watchdog = aue_watchdog;
840 ifp->if_baudrate = 10000000;
841 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
842
843 /* Initialize MII/media info. */
844 mii = &sc->aue_mii;
845 mii->mii_ifp = ifp;
846 mii->mii_readreg = aue_miibus_readreg;
847 mii->mii_writereg = aue_miibus_writereg;
848 mii->mii_statchg = aue_miibus_statchg;
849 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
850 mii_phy_probe(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY);
851 if (LIST_FIRST(&mii->mii_phys) == NULL) {
852 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
853 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
854 } else
855 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
856
857 /* Attach the interface. */
858 if_attach(ifp);
859 ether_ifattach(ifp, eaddr);
860
861 #if NBPFILTER > 0
862 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
863 sizeof(struct ether_header));
864 #endif
865 #if RND > 0
866 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
867 RND_TYPE_NET, 0);
868 #endif
869
870 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
871
872 splx(s);
873
874 USB_ATTACH_SUCCESS_RETURN;
875 }
876
877 USB_DETACH(aue)
878 {
879 USB_DETACH_START(aue, sc);
880 #if defined(__FreeBSD__)
881 struct ifnet *ifp;
882 int s;
883
884 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
885
886 s = splusb();
887
888 ifp = &sc->arpcom.ac_if;
889
890 usb_untimeout(aue_tick, sc, sc->aue_stat_ch);
891 if_detach(ifp);
892
893 if (sc->aue_ep[AUE_ENDPT_TX] != NULL)
894 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
895 if (sc->aue_ep[AUE_ENDPT_RX] != NULL)
896 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
897 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL)
898 usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
899
900 splx(s);
901
902 return (0);
903 #elif defined(__NetBSD__) || defined(__OpenBSD__)
904 sc = sc; /* XXX use sc */
905 /* XXX deallocate */
906
907 #ifdef notyet
908 /*
909 * Our softc is about to go away, so drop our refernce
910 * to the ifnet.
911 */
912 if_delref(sc->aue_ec.ec_if);
913 return (0);
914 #else
915 return (EBUSY);
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,
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 >> 3) & 0xE0;
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_hsot = *(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