if_aue.c revision 1.61 1 /* $NetBSD: if_aue.c,v 1.61 2001/07/16 13:49:20 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__)
80 #include "opt_inet.h"
81 #include "opt_ns.h"
82 #include "bpfilter.h"
83 #include "rnd.h"
84 #elif defined(__OpenBSD__)
85 #include "bpfilter.h"
86 #endif /* defined(__OpenBSD__) */
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/sockio.h>
91 #include <sys/lock.h>
92 #include <sys/mbuf.h>
93 #include <sys/malloc.h>
94 #include <sys/kernel.h>
95 #include <sys/socket.h>
96
97 #include <sys/device.h>
98 #if NRND > 0
99 #include <sys/rnd.h>
100 #endif
101
102 #include <net/if.h>
103 #if defined(__NetBSD__)
104 #include <net/if_arp.h>
105 #endif
106 #include <net/if_dl.h>
107 #include <net/if_media.h>
108
109 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
110
111 #if NBPFILTER > 0
112 #include <net/bpf.h>
113 #endif
114
115 #if defined(__NetBSD__)
116 #include <net/if_ether.h>
117 #ifdef INET
118 #include <netinet/in.h>
119 #include <netinet/if_inarp.h>
120 #endif
121 #endif /* defined(__NetBSD__) */
122
123 #if defined(__OpenBSD__)
124 #ifdef INET
125 #include <netinet/in.h>
126 #include <netinet/in_systm.h>
127 #include <netinet/in_var.h>
128 #include <netinet/ip.h>
129 #include <netinet/if_ether.h>
130 #endif
131 #endif /* defined(__OpenBSD__) */
132
133 #ifdef NS
134 #include <netns/ns.h>
135 #include <netns/ns_if.h>
136 #endif
137
138 #include <dev/mii/mii.h>
139 #include <dev/mii/miivar.h>
140
141 #include <dev/usb/usb.h>
142 #include <dev/usb/usbdi.h>
143 #include <dev/usb/usbdi_util.h>
144 #include <dev/usb/usbdevs.h>
145
146 #include <dev/usb/if_auereg.h>
147
148 #ifdef AUE_DEBUG
149 #define DPRINTF(x) if (auedebug) logprintf x
150 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x
151 int auedebug = 0;
152 #else
153 #define DPRINTF(x)
154 #define DPRINTFN(n,x)
155 #endif
156
157 /*
158 * Various supported device vendors/products.
159 */
160 struct aue_type {
161 u_int16_t aue_vid;
162 u_int16_t aue_did;
163 char aue_linksys;
164 };
165
166 Static const struct aue_type aue_devs[] = {
167 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA, 0 },
168 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS, 0 },
169 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100, 0 },
170 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX, 0 },
171 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650, 1 },
172 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX, 1 },
173 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA, 0 },
174 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX, 0 },
175 { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX, 0 },
176 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX, 1 },
177 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1, 1 },
178 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA, 1 },
179 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1, 0 },
180 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5, 0 },
181 { USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100, 0 },
182 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB, 0 },
183 { 0, 0, 0 }
184 };
185
186 USB_DECLARE_DRIVER(aue);
187
188 Static const struct aue_type *aue_lookup(u_int16_t vendor, u_int16_t product);
189 Static int aue_tx_list_init(struct aue_softc *);
190 Static int aue_rx_list_init(struct aue_softc *);
191 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
192 Static int aue_send(struct aue_softc *, struct mbuf *, int);
193 Static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
194 Static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
195 Static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
196 Static void aue_tick(void *);
197 Static void aue_tick_task(void *);
198 Static void aue_start(struct ifnet *);
199 Static int aue_ioctl(struct ifnet *, u_long, caddr_t);
200 Static void aue_init(void *);
201 Static void aue_stop(struct aue_softc *);
202 Static void aue_watchdog(struct ifnet *);
203 Static int aue_openpipes(struct aue_softc *);
204 Static int aue_ifmedia_upd(struct ifnet *);
205 Static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
206
207 Static int aue_eeprom_getword(struct aue_softc *, int);
208 Static void aue_read_mac(struct aue_softc *, u_char *);
209 Static int aue_miibus_readreg(device_ptr_t, int, int);
210 Static void aue_miibus_writereg(device_ptr_t, int, int, int);
211 Static void aue_miibus_statchg(device_ptr_t);
212
213 Static void aue_lock_mii(struct aue_softc *);
214 Static void aue_unlock_mii(struct aue_softc *);
215
216 Static void aue_setmulti(struct aue_softc *);
217 Static u_int32_t aue_crc(caddr_t);
218 Static void aue_reset(struct aue_softc *);
219
220 Static int aue_csr_read_1(struct aue_softc *, int);
221 Static int aue_csr_write_1(struct aue_softc *, int, int);
222 Static int aue_csr_read_2(struct aue_softc *, int);
223 Static int aue_csr_write_2(struct aue_softc *, int, int);
224
225 #define AUE_SETBIT(sc, reg, x) \
226 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
227
228 #define AUE_CLRBIT(sc, reg, x) \
229 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
230
231 Static int
232 aue_csr_read_1(struct aue_softc *sc, int reg)
233 {
234 usb_device_request_t req;
235 usbd_status err;
236 uByte val = 0;
237
238 if (sc->aue_dying)
239 return (0);
240
241 req.bmRequestType = UT_READ_VENDOR_DEVICE;
242 req.bRequest = AUE_UR_READREG;
243 USETW(req.wValue, 0);
244 USETW(req.wIndex, reg);
245 USETW(req.wLength, 1);
246
247 err = usbd_do_request(sc->aue_udev, &req, &val);
248
249 if (err) {
250 DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n",
251 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err)));
252 return (0);
253 }
254
255 return (val);
256 }
257
258 Static int
259 aue_csr_read_2(struct aue_softc *sc, int reg)
260 {
261 usb_device_request_t req;
262 usbd_status err;
263 uWord val;
264
265 if (sc->aue_dying)
266 return (0);
267
268 req.bmRequestType = UT_READ_VENDOR_DEVICE;
269 req.bRequest = AUE_UR_READREG;
270 USETW(req.wValue, 0);
271 USETW(req.wIndex, reg);
272 USETW(req.wLength, 2);
273
274 err = usbd_do_request(sc->aue_udev, &req, &val);
275
276 if (err) {
277 DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n",
278 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err)));
279 return (0);
280 }
281
282 return (UGETW(val));
283 }
284
285 Static int
286 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
287 {
288 usb_device_request_t req;
289 usbd_status err;
290 uByte val;
291
292 if (sc->aue_dying)
293 return (0);
294
295 val = aval;
296 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
297 req.bRequest = AUE_UR_WRITEREG;
298 USETW(req.wValue, val);
299 USETW(req.wIndex, reg);
300 USETW(req.wLength, 1);
301
302 err = usbd_do_request(sc->aue_udev, &req, &val);
303
304 if (err) {
305 DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n",
306 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err)));
307 return (-1);
308 }
309
310 return (0);
311 }
312
313 Static int
314 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
315 {
316 usb_device_request_t req;
317 usbd_status err;
318 uWord val;
319
320 if (sc->aue_dying)
321 return (0);
322
323 USETW(val, aval);
324 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
325 req.bRequest = AUE_UR_WRITEREG;
326 USETW(req.wValue, aval);
327 USETW(req.wIndex, reg);
328 USETW(req.wLength, 2);
329
330 err = usbd_do_request(sc->aue_udev, &req, &val);
331
332 if (err) {
333 DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n",
334 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err)));
335 return (-1);
336 }
337
338 return (0);
339 }
340
341 /*
342 * Read a word of data stored in the EEPROM at address 'addr.'
343 */
344 Static int
345 aue_eeprom_getword(struct aue_softc *sc, int addr)
346 {
347 int i;
348
349 aue_csr_write_1(sc, AUE_EE_REG, addr);
350 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
351
352 for (i = 0; i < AUE_TIMEOUT; i++) {
353 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
354 break;
355 }
356
357 if (i == AUE_TIMEOUT) {
358 printf("%s: EEPROM read timed out\n",
359 USBDEVNAME(sc->aue_dev));
360 }
361
362 return (aue_csr_read_2(sc, AUE_EE_DATA));
363 }
364
365 /*
366 * Read the MAC from the EEPROM. It's at offset 0.
367 */
368 Static void
369 aue_read_mac(struct aue_softc *sc, u_char *dest)
370 {
371 int i;
372 int off = 0;
373 int word;
374
375 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
376
377 for (i = 0; i < 3; i++) {
378 word = aue_eeprom_getword(sc, off + i);
379 dest[2 * i] = (u_char)word;
380 dest[2 * i + 1] = (u_char)(word >> 8);
381 }
382 }
383
384 /* Get exclusive access to the MII registers */
385 Static void
386 aue_lock_mii(struct aue_softc *sc)
387 {
388 lockmgr(&sc->aue_mii_lock, LK_EXCLUSIVE, NULL);
389 }
390
391 Static void
392 aue_unlock_mii(struct aue_softc *sc)
393 {
394 lockmgr(&sc->aue_mii_lock, LK_RELEASE, NULL);
395 }
396
397 Static int
398 aue_miibus_readreg(device_ptr_t dev, int phy, int reg)
399 {
400 struct aue_softc *sc = USBGETSOFTC(dev);
401 int i;
402 u_int16_t val;
403
404 #if 0
405 /*
406 * The Am79C901 HomePNA PHY actually contains
407 * two transceivers: a 1Mbps HomePNA PHY and a
408 * 10Mbps full/half duplex ethernet PHY with
409 * NWAY autoneg. However in the ADMtek adapter,
410 * only the 1Mbps PHY is actually connected to
411 * anything, so we ignore the 10Mbps one. It
412 * happens to be configured for MII address 3,
413 * so we filter that out.
414 */
415 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
416 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
417 if (phy == 3)
418 return (0);
419 }
420 #endif
421
422 aue_lock_mii(sc);
423 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
424 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
425
426 for (i = 0; i < AUE_TIMEOUT; i++) {
427 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
428 break;
429 }
430
431 if (i == AUE_TIMEOUT) {
432 printf("%s: MII read timed out\n", USBDEVNAME(sc->aue_dev));
433 }
434
435 val = aue_csr_read_2(sc, AUE_PHY_DATA);
436
437 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
438 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val));
439
440 aue_unlock_mii(sc);
441 return (val);
442 }
443
444 Static void
445 aue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
446 {
447 struct aue_softc *sc = USBGETSOFTC(dev);
448 int i;
449
450 #if 0
451 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
452 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
453 if (phy == 3)
454 return;
455 }
456 #endif
457
458 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
459 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data));
460
461 aue_lock_mii(sc);
462 aue_csr_write_2(sc, AUE_PHY_DATA, data);
463 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
464 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
465
466 for (i = 0; i < AUE_TIMEOUT; i++) {
467 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
468 break;
469 }
470
471 if (i == AUE_TIMEOUT) {
472 printf("%s: MII read timed out\n",
473 USBDEVNAME(sc->aue_dev));
474 }
475 aue_unlock_mii(sc);
476 }
477
478 Static void
479 aue_miibus_statchg(device_ptr_t dev)
480 {
481 struct aue_softc *sc = USBGETSOFTC(dev);
482 struct mii_data *mii = GET_MII(sc);
483
484 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
485
486 aue_lock_mii(sc);
487 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
488
489 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
490 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
491 } else {
492 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
493 }
494
495 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
496 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
497 else
498 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
499
500 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
501 aue_unlock_mii(sc);
502
503 /*
504 * Set the LED modes on the LinkSys adapter.
505 * This turns on the 'dual link LED' bin in the auxmode
506 * register of the Broadcom PHY.
507 */
508 if (sc->aue_linksys) {
509 u_int16_t auxmode;
510 auxmode = aue_miibus_readreg(dev, 0, 0x1b);
511 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
512 }
513 }
514
515 #define AUE_POLY 0xEDB88320
516 #define AUE_BITS 6
517
518 Static u_int32_t
519 aue_crc(caddr_t addr)
520 {
521 u_int32_t idx, bit, data, crc;
522
523 /* Compute CRC for the address value. */
524 crc = 0xFFFFFFFF; /* initial value */
525
526 for (idx = 0; idx < 6; idx++) {
527 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
528 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
529 }
530
531 return (crc & ((1 << AUE_BITS) - 1));
532 }
533
534 Static void
535 aue_setmulti(struct aue_softc *sc)
536 {
537 struct ifnet *ifp;
538 struct ether_multi *enm;
539 struct ether_multistep step;
540 u_int32_t h = 0, i;
541
542 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
543
544 ifp = GET_IFP(sc);
545
546 if (ifp->if_flags & IFF_PROMISC) {
547 allmulti:
548 ifp->if_flags |= IFF_ALLMULTI;
549 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
550 return;
551 }
552
553 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
554
555 /* first, zot all the existing hash bits */
556 for (i = 0; i < 8; i++)
557 aue_csr_write_1(sc, AUE_MAR0 + i, 0);
558
559 /* now program new ones */
560 #if defined(__NetBSD__)
561 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
562 #else
563 ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
564 #endif
565 while (enm != NULL) {
566 if (memcmp(enm->enm_addrlo,
567 enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
568 goto allmulti;
569
570 h = aue_crc(enm->enm_addrlo);
571 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
572 ETHER_NEXT_MULTI(step, enm);
573 }
574
575 ifp->if_flags &= ~IFF_ALLMULTI;
576 }
577
578 Static void
579 aue_reset(struct aue_softc *sc)
580 {
581 int i;
582
583 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
584
585 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
586
587 for (i = 0; i < AUE_TIMEOUT; i++) {
588 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
589 break;
590 }
591
592 if (i == AUE_TIMEOUT)
593 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
594
595 /*
596 * The PHY(s) attached to the Pegasus chip may be held
597 * in reset until we flip on the GPIO outputs. Make sure
598 * to set the GPIO pins high so that the PHY(s) will
599 * be enabled.
600 *
601 * Note: We force all of the GPIO pins low first, *then*
602 * enable the ones we want.
603 */
604 aue_csr_write_1(sc, AUE_GPIO0,
605 AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
606 aue_csr_write_1(sc, AUE_GPIO0,
607 AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
608
609 /* Grrr. LinkSys has to be different from everyone else. */
610 if (sc->aue_linksys) {
611 aue_csr_write_1(sc, AUE_GPIO0,
612 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
613 aue_csr_write_1(sc, AUE_GPIO0,
614 AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
615 }
616
617 /* Wait a little while for the chip to get its brains in order. */
618 delay(10000); /* XXX */
619 }
620
621 Static const struct aue_type *
622 aue_lookup(u_int16_t vendor, u_int16_t product)
623 {
624 const struct aue_type *t;
625
626 for (t = aue_devs; t->aue_vid != 0; t++)
627 if (vendor == t->aue_vid && product == t->aue_did)
628 return (t);
629 return (NULL);
630 }
631
632 /*
633 * Probe for a Pegasus chip.
634 */
635 USB_MATCH(aue)
636 {
637 USB_MATCH_START(aue, uaa);
638
639 if (uaa->iface != NULL)
640 return (UMATCH_NONE);
641
642 return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
643 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
644 }
645
646 /*
647 * Attach the interface. Allocate softc structures, do ifmedia
648 * setup and ethernet/BPF attach.
649 */
650 USB_ATTACH(aue)
651 {
652 USB_ATTACH_START(aue, sc, uaa);
653 char devinfo[1024];
654 int s;
655 u_char eaddr[ETHER_ADDR_LEN];
656 struct ifnet *ifp;
657 struct mii_data *mii;
658 usbd_device_handle dev = uaa->device;
659 usbd_interface_handle iface;
660 usbd_status err;
661 usb_interface_descriptor_t *id;
662 usb_endpoint_descriptor_t *ed;
663 int i;
664
665 DPRINTFN(5,(" : aue_attach: sc=%p", sc));
666
667 usbd_devinfo(dev, 0, devinfo);
668 USB_ATTACH_SETUP;
669 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
670
671 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
672 if (err) {
673 printf("%s: setting config no failed\n",
674 USBDEVNAME(sc->aue_dev));
675 USB_ATTACH_ERROR_RETURN;
676 }
677
678 usb_init_task(&sc->aue_tick_task, aue_tick_task, sc);
679 usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc);
680 lockinit(&sc->aue_mii_lock, PZERO, "auemii", 0, 0);
681
682 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
683 if (err) {
684 printf("%s: getting interface handle failed\n",
685 USBDEVNAME(sc->aue_dev));
686 USB_ATTACH_ERROR_RETURN;
687 }
688
689 sc->aue_linksys = aue_lookup(uaa->vendor, uaa->product)->aue_linksys;
690
691 sc->aue_udev = dev;
692 sc->aue_iface = iface;
693 sc->aue_product = uaa->product;
694 sc->aue_vendor = uaa->vendor;
695
696 id = usbd_get_interface_descriptor(iface);
697
698 /* Find endpoints. */
699 for (i = 0; i < id->bNumEndpoints; i++) {
700 ed = usbd_interface2endpoint_descriptor(iface, i);
701 if (ed == NULL) {
702 printf("%s: couldn't get endpoint descriptor %d\n",
703 USBDEVNAME(sc->aue_dev), i);
704 USB_ATTACH_ERROR_RETURN;
705 }
706 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
707 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
708 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
709 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
710 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
711 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
712 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
713 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
714 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
715 }
716 }
717
718 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
719 sc->aue_ed[AUE_ENDPT_INTR] == 0) {
720 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
721 USB_ATTACH_ERROR_RETURN;
722 }
723
724
725 s = splnet();
726
727 /* Reset the adapter. */
728 aue_reset(sc);
729
730 /*
731 * Get station address from the EEPROM.
732 */
733 aue_read_mac(sc, eaddr);
734
735 /*
736 * A Pegasus chip was detected. Inform the world.
737 */
738 ifp = GET_IFP(sc);
739 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
740 ether_sprintf(eaddr));
741
742 /* Initialize interface info.*/
743 ifp->if_softc = sc;
744 ifp->if_mtu = ETHERMTU;
745 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
746 ifp->if_ioctl = aue_ioctl;
747 ifp->if_start = aue_start;
748 ifp->if_watchdog = aue_watchdog;
749 #if defined(__OpenBSD__)
750 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
751 #endif
752 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
753
754 IFQ_SET_READY(&ifp->if_snd);
755
756 /* Initialize MII/media info. */
757 mii = &sc->aue_mii;
758 mii->mii_ifp = ifp;
759 mii->mii_readreg = aue_miibus_readreg;
760 mii->mii_writereg = aue_miibus_writereg;
761 mii->mii_statchg = aue_miibus_statchg;
762 mii->mii_flags = MIIF_AUTOTSLEEP;
763 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
764 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
765 if (LIST_FIRST(&mii->mii_phys) == NULL) {
766 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
767 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
768 } else
769 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
770
771 /* Attach the interface. */
772 if_attach(ifp);
773 Ether_ifattach(ifp, eaddr);
774 #if NRND > 0
775 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
776 RND_TYPE_NET, 0);
777 #endif
778
779 usb_callout_init(sc->aue_stat_ch);
780
781 sc->aue_attached = 1;
782 splx(s);
783
784 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
785 USBDEV(sc->aue_dev));
786
787 USB_ATTACH_SUCCESS_RETURN;
788 }
789
790 USB_DETACH(aue)
791 {
792 USB_DETACH_START(aue, sc);
793 struct ifnet *ifp = GET_IFP(sc);
794 int s;
795
796 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
797
798 if (!sc->aue_attached) {
799 /* Detached before attached finished, so just bail out. */
800 return (0);
801 }
802
803 usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
804 /*
805 * Remove any pending tasks. They cannot be executing because they run
806 * in the same thread as detach.
807 */
808 usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
809 usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
810
811 s = splusb();
812
813 if (ifp->if_flags & IFF_RUNNING)
814 aue_stop(sc);
815
816 #if defined(__NetBSD__)
817 #if NRND > 0
818 rnd_detach_source(&sc->rnd_source);
819 #endif
820 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
821 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
822 ether_ifdetach(ifp);
823 #endif /* __NetBSD__ */
824
825 if_detach(ifp);
826
827 #ifdef DIAGNOSTIC
828 if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
829 sc->aue_ep[AUE_ENDPT_RX] != NULL ||
830 sc->aue_ep[AUE_ENDPT_INTR] != NULL)
831 printf("%s: detach has active endpoints\n",
832 USBDEVNAME(sc->aue_dev));
833 #endif
834
835 sc->aue_attached = 0;
836 splx(s);
837
838 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
839 USBDEV(sc->aue_dev));
840
841 return (0);
842 }
843
844 int
845 aue_activate(device_ptr_t self, enum devact act)
846 {
847 struct aue_softc *sc = (struct aue_softc *)self;
848
849 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
850
851 switch (act) {
852 case DVACT_ACTIVATE:
853 return (EOPNOTSUPP);
854 break;
855
856 case DVACT_DEACTIVATE:
857 if_deactivate(&sc->aue_ec.ec_if);
858 sc->aue_dying = 1;
859 break;
860 }
861 return (0);
862 }
863
864 /*
865 * Initialize an RX descriptor and attach an MBUF cluster.
866 */
867 Static int
868 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
869 {
870 struct mbuf *m_new = NULL;
871
872 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
873
874 if (m == NULL) {
875 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
876 if (m_new == NULL) {
877 printf("%s: no memory for rx list "
878 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
879 return (ENOBUFS);
880 }
881
882 MCLGET(m_new, M_DONTWAIT);
883 if (!(m_new->m_flags & M_EXT)) {
884 printf("%s: no memory for rx list "
885 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
886 m_freem(m_new);
887 return (ENOBUFS);
888 }
889 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
890 } else {
891 m_new = m;
892 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
893 m_new->m_data = m_new->m_ext.ext_buf;
894 }
895
896 m_adj(m_new, ETHER_ALIGN);
897 c->aue_mbuf = m_new;
898
899 return (0);
900 }
901
902 Static int
903 aue_rx_list_init(struct aue_softc *sc)
904 {
905 struct aue_cdata *cd;
906 struct aue_chain *c;
907 int i;
908
909 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
910
911 cd = &sc->aue_cdata;
912 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
913 c = &cd->aue_rx_chain[i];
914 c->aue_sc = sc;
915 c->aue_idx = i;
916 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
917 return (ENOBUFS);
918 if (c->aue_xfer == NULL) {
919 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
920 if (c->aue_xfer == NULL)
921 return (ENOBUFS);
922 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
923 if (c->aue_buf == NULL)
924 return (ENOBUFS); /* XXX free xfer */
925 }
926 }
927
928 return (0);
929 }
930
931 Static int
932 aue_tx_list_init(struct aue_softc *sc)
933 {
934 struct aue_cdata *cd;
935 struct aue_chain *c;
936 int i;
937
938 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
939
940 cd = &sc->aue_cdata;
941 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
942 c = &cd->aue_tx_chain[i];
943 c->aue_sc = sc;
944 c->aue_idx = i;
945 c->aue_mbuf = NULL;
946 if (c->aue_xfer == NULL) {
947 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
948 if (c->aue_xfer == NULL)
949 return (ENOBUFS);
950 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
951 if (c->aue_buf == NULL)
952 return (ENOBUFS);
953 }
954 }
955
956 return (0);
957 }
958
959 Static void
960 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
961 {
962 struct aue_softc *sc = priv;
963 struct ifnet *ifp = GET_IFP(sc);
964 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
965
966 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
967
968 if (sc->aue_dying)
969 return;
970
971 if (!(ifp->if_flags & IFF_RUNNING))
972 return;
973
974 if (status != USBD_NORMAL_COMPLETION) {
975 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
976 return;
977 }
978 sc->aue_intr_errs++;
979 if (usbd_ratecheck(&sc->aue_rx_notice)) {
980 printf("%s: %u usb errors on intr: %s\n",
981 USBDEVNAME(sc->aue_dev), sc->aue_intr_errs,
982 usbd_errstr(status));
983 sc->aue_intr_errs = 0;
984 }
985 if (status == USBD_STALLED)
986 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
987 return;
988 }
989
990 if (p->aue_txstat0)
991 ifp->if_oerrors++;
992
993 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
994 ifp->if_collisions++;
995 }
996
997 /*
998 * A frame has been uploaded: pass the resulting mbuf chain up to
999 * the higher level protocols.
1000 */
1001 Static void
1002 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1003 {
1004 struct aue_chain *c = priv;
1005 struct aue_softc *sc = c->aue_sc;
1006 struct ifnet *ifp = GET_IFP(sc);
1007 struct mbuf *m;
1008 u_int32_t total_len;
1009 struct aue_rxpkt r;
1010 int s;
1011
1012 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1013
1014 if (sc->aue_dying)
1015 return;
1016
1017 if (!(ifp->if_flags & IFF_RUNNING))
1018 return;
1019
1020 if (status != USBD_NORMAL_COMPLETION) {
1021 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1022 return;
1023 sc->aue_rx_errs++;
1024 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1025 printf("%s: %u usb errors on rx: %s\n",
1026 USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
1027 usbd_errstr(status));
1028 sc->aue_rx_errs = 0;
1029 }
1030 if (status == USBD_STALLED)
1031 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1032 goto done;
1033 }
1034
1035 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1036
1037 memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
1038
1039 if (total_len <= 4 + ETHER_CRC_LEN) {
1040 ifp->if_ierrors++;
1041 goto done;
1042 }
1043
1044 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1045
1046 /* Turn off all the non-error bits in the rx status word. */
1047 r.aue_rxstat &= AUE_RXSTAT_MASK;
1048 if (r.aue_rxstat) {
1049 ifp->if_ierrors++;
1050 goto done;
1051 }
1052
1053 /* No errors; receive the packet. */
1054 m = c->aue_mbuf;
1055 total_len -= ETHER_CRC_LEN + 4;
1056 m->m_pkthdr.len = m->m_len = total_len;
1057 ifp->if_ipackets++;
1058
1059 m->m_pkthdr.rcvif = ifp;
1060
1061 s = splnet();
1062
1063 /* XXX ugly */
1064 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1065 ifp->if_ierrors++;
1066 goto done1;
1067 }
1068
1069 #if NBPFILTER > 0
1070 /*
1071 * Handle BPF listeners. Let the BPF user see the packet, but
1072 * don't pass it up to the ether_input() layer unless it's
1073 * a broadcast packet, multicast packet, matches our ethernet
1074 * address or the interface is in promiscuous mode.
1075 */
1076 if (ifp->if_bpf)
1077 BPF_MTAP(ifp, m);
1078 #endif
1079
1080 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1081 __FUNCTION__, m->m_len));
1082 IF_INPUT(ifp, m);
1083 done1:
1084 splx(s);
1085
1086 done:
1087
1088 /* Setup new transfer. */
1089 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1090 c, c->aue_buf, AUE_BUFSZ,
1091 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1092 USBD_NO_TIMEOUT, aue_rxeof);
1093 usbd_transfer(xfer);
1094
1095 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1096 __FUNCTION__));
1097 }
1098
1099 /*
1100 * A frame was downloaded to the chip. It's safe for us to clean up
1101 * the list buffers.
1102 */
1103
1104 Static void
1105 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1106 {
1107 struct aue_chain *c = priv;
1108 struct aue_softc *sc = c->aue_sc;
1109 struct ifnet *ifp = GET_IFP(sc);
1110 int s;
1111
1112 if (sc->aue_dying)
1113 return;
1114
1115 s = splnet();
1116
1117 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1118 __FUNCTION__, status));
1119
1120 ifp->if_timer = 0;
1121 ifp->if_flags &= ~IFF_OACTIVE;
1122
1123 if (status != USBD_NORMAL_COMPLETION) {
1124 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1125 splx(s);
1126 return;
1127 }
1128 ifp->if_oerrors++;
1129 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1130 usbd_errstr(status));
1131 if (status == USBD_STALLED)
1132 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1133 splx(s);
1134 return;
1135 }
1136
1137 ifp->if_opackets++;
1138
1139 m_freem(c->aue_mbuf);
1140 c->aue_mbuf = NULL;
1141
1142 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1143 aue_start(ifp);
1144
1145 splx(s);
1146 }
1147
1148 Static void
1149 aue_tick(void *xsc)
1150 {
1151 struct aue_softc *sc = xsc;
1152
1153 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1154
1155 if (sc == NULL)
1156 return;
1157
1158 if (sc->aue_dying)
1159 return;
1160
1161 /* Perform periodic stuff in process context. */
1162 usb_add_task(sc->aue_udev, &sc->aue_tick_task);
1163 }
1164
1165 Static void
1166 aue_tick_task(void *xsc)
1167 {
1168 struct aue_softc *sc = xsc;
1169 struct ifnet *ifp;
1170 struct mii_data *mii;
1171 int s;
1172
1173 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1174
1175 if (sc->aue_dying)
1176 return;
1177
1178 ifp = GET_IFP(sc);
1179 mii = GET_MII(sc);
1180 if (mii == NULL)
1181 return;
1182
1183 s = splnet();
1184
1185 mii_tick(mii);
1186 if (!sc->aue_link) {
1187 mii_pollstat(mii);
1188 if (mii->mii_media_status & IFM_ACTIVE &&
1189 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1190 DPRINTFN(2,("%s: %s: got link\n",
1191 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1192 sc->aue_link++;
1193 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1194 aue_start(ifp);
1195 }
1196 }
1197
1198 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1199
1200 splx(s);
1201 }
1202
1203 Static int
1204 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1205 {
1206 int total_len;
1207 struct aue_chain *c;
1208 usbd_status err;
1209
1210 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1211
1212 c = &sc->aue_cdata.aue_tx_chain[idx];
1213
1214 /*
1215 * Copy the mbuf data into a contiguous buffer, leaving two
1216 * bytes at the beginning to hold the frame length.
1217 */
1218 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1219 c->aue_mbuf = m;
1220
1221 /*
1222 * The ADMtek documentation says that the packet length is
1223 * supposed to be specified in the first two bytes of the
1224 * transfer, however it actually seems to ignore this info
1225 * and base the frame size on the bulk transfer length.
1226 */
1227 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1228 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1229 total_len = m->m_pkthdr.len + 2;
1230
1231 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1232 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1233 AUE_TX_TIMEOUT, aue_txeof);
1234
1235 /* Transmit */
1236 err = usbd_transfer(c->aue_xfer);
1237 if (err != USBD_IN_PROGRESS) {
1238 printf("%s: aue_send error=%s\n", USBDEVNAME(sc->aue_dev),
1239 usbd_errstr(err));
1240 /* Stop the interface from process context. */
1241 usb_add_task(sc->aue_udev, &sc->aue_stop_task);
1242 return (EIO);
1243 }
1244 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1245 __FUNCTION__, total_len));
1246
1247 sc->aue_cdata.aue_tx_cnt++;
1248
1249 return (0);
1250 }
1251
1252 Static void
1253 aue_start(struct ifnet *ifp)
1254 {
1255 struct aue_softc *sc = ifp->if_softc;
1256 struct mbuf *m_head = NULL;
1257
1258 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1259 __FUNCTION__, sc->aue_link));
1260
1261 if (sc->aue_dying)
1262 return;
1263
1264 if (!sc->aue_link)
1265 return;
1266
1267 if (ifp->if_flags & IFF_OACTIVE)
1268 return;
1269
1270 IFQ_POLL(&ifp->if_snd, m_head);
1271 if (m_head == NULL)
1272 return;
1273
1274 if (aue_send(sc, m_head, 0)) {
1275 ifp->if_flags |= IFF_OACTIVE;
1276 return;
1277 }
1278
1279 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1280
1281 #if NBPFILTER > 0
1282 /*
1283 * If there's a BPF listener, bounce a copy of this frame
1284 * to him.
1285 */
1286 if (ifp->if_bpf)
1287 BPF_MTAP(ifp, m_head);
1288 #endif
1289
1290 ifp->if_flags |= IFF_OACTIVE;
1291
1292 /*
1293 * Set a timeout in case the chip goes out to lunch.
1294 */
1295 ifp->if_timer = 5;
1296 }
1297
1298 Static void
1299 aue_init(void *xsc)
1300 {
1301 struct aue_softc *sc = xsc;
1302 struct ifnet *ifp = GET_IFP(sc);
1303 struct mii_data *mii = GET_MII(sc);
1304 int i, s;
1305 u_char *eaddr;
1306
1307 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1308
1309 if (sc->aue_dying)
1310 return;
1311
1312 if (ifp->if_flags & IFF_RUNNING)
1313 return;
1314
1315 s = splnet();
1316
1317 /*
1318 * Cancel pending I/O and free all RX/TX buffers.
1319 */
1320 aue_reset(sc);
1321
1322 #if defined(__OpenBSD__)
1323 eaddr = sc->arpcom.ac_enaddr;
1324 #elif defined(__NetBSD__)
1325 eaddr = LLADDR(ifp->if_sadl);
1326 #endif /* defined(__NetBSD__) */
1327 for (i = 0; i < ETHER_ADDR_LEN; i++)
1328 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1329
1330 /* If we want promiscuous mode, set the allframes bit. */
1331 if (ifp->if_flags & IFF_PROMISC)
1332 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1333 else
1334 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1335
1336 /* Init TX ring. */
1337 if (aue_tx_list_init(sc) == ENOBUFS) {
1338 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1339 splx(s);
1340 return;
1341 }
1342
1343 /* Init RX ring. */
1344 if (aue_rx_list_init(sc) == ENOBUFS) {
1345 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1346 splx(s);
1347 return;
1348 }
1349
1350 /* Load the multicast filter. */
1351 aue_setmulti(sc);
1352
1353 /* Enable RX and TX */
1354 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1355 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1356 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1357
1358 mii_mediachg(mii);
1359
1360 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1361 if (aue_openpipes(sc)) {
1362 splx(s);
1363 return;
1364 }
1365 }
1366
1367 ifp->if_flags |= IFF_RUNNING;
1368 ifp->if_flags &= ~IFF_OACTIVE;
1369
1370 splx(s);
1371
1372 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1373 }
1374
1375 Static int
1376 aue_openpipes(struct aue_softc *sc)
1377 {
1378 struct aue_chain *c;
1379 usbd_status err;
1380 int i;
1381
1382 /* Open RX and TX pipes. */
1383 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1384 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1385 if (err) {
1386 printf("%s: open rx pipe failed: %s\n",
1387 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1388 return (EIO);
1389 }
1390 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1391 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1392 if (err) {
1393 printf("%s: open tx pipe failed: %s\n",
1394 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1395 return (EIO);
1396 }
1397 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1398 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1399 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1400 AUE_INTR_INTERVAL);
1401 if (err) {
1402 printf("%s: open intr pipe failed: %s\n",
1403 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1404 return (EIO);
1405 }
1406
1407 /* Start up the receive pipe. */
1408 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1409 c = &sc->aue_cdata.aue_rx_chain[i];
1410 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1411 c, c->aue_buf, AUE_BUFSZ,
1412 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1413 aue_rxeof);
1414 (void)usbd_transfer(c->aue_xfer); /* XXX */
1415 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1416 __FUNCTION__));
1417
1418 }
1419 return (0);
1420 }
1421
1422 /*
1423 * Set media options.
1424 */
1425 Static int
1426 aue_ifmedia_upd(struct ifnet *ifp)
1427 {
1428 struct aue_softc *sc = ifp->if_softc;
1429 struct mii_data *mii = GET_MII(sc);
1430
1431 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1432
1433 if (sc->aue_dying)
1434 return (0);
1435
1436 sc->aue_link = 0;
1437 if (mii->mii_instance) {
1438 struct mii_softc *miisc;
1439 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1440 miisc = LIST_NEXT(miisc, mii_list))
1441 mii_phy_reset(miisc);
1442 }
1443 mii_mediachg(mii);
1444
1445 return (0);
1446 }
1447
1448 /*
1449 * Report current media status.
1450 */
1451 Static void
1452 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1453 {
1454 struct aue_softc *sc = ifp->if_softc;
1455 struct mii_data *mii = GET_MII(sc);
1456
1457 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1458
1459 mii_pollstat(mii);
1460 ifmr->ifm_active = mii->mii_media_active;
1461 ifmr->ifm_status = mii->mii_media_status;
1462 }
1463
1464 Static int
1465 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1466 {
1467 struct aue_softc *sc = ifp->if_softc;
1468 struct ifaddr *ifa = (struct ifaddr *)data;
1469 struct ifreq *ifr = (struct ifreq *)data;
1470 struct mii_data *mii;
1471 int s, error = 0;
1472
1473 if (sc->aue_dying)
1474 return (EIO);
1475
1476 s = splnet();
1477
1478 switch(command) {
1479 case SIOCSIFADDR:
1480 ifp->if_flags |= IFF_UP;
1481 aue_init(sc);
1482
1483 switch (ifa->ifa_addr->sa_family) {
1484 #ifdef INET
1485 case AF_INET:
1486 #if defined(__NetBSD__)
1487 arp_ifinit(ifp, ifa);
1488 #else
1489 arp_ifinit(&sc->arpcom, ifa);
1490 #endif
1491 break;
1492 #endif /* INET */
1493 #ifdef NS
1494 case AF_NS:
1495 {
1496 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1497
1498 if (ns_nullhost(*ina))
1499 ina->x_host = *(union ns_host *)
1500 LLADDR(ifp->if_sadl);
1501 else
1502 memcpy(LLADDR(ifp->if_sadl),
1503 ina->x_host.c_host,
1504 ifp->if_addrlen);
1505 break;
1506 }
1507 #endif /* NS */
1508 }
1509 break;
1510
1511 case SIOCSIFMTU:
1512 if (ifr->ifr_mtu > ETHERMTU)
1513 error = EINVAL;
1514 else
1515 ifp->if_mtu = ifr->ifr_mtu;
1516 break;
1517
1518 case SIOCSIFFLAGS:
1519 if (ifp->if_flags & IFF_UP) {
1520 if (ifp->if_flags & IFF_RUNNING &&
1521 ifp->if_flags & IFF_PROMISC &&
1522 !(sc->aue_if_flags & IFF_PROMISC)) {
1523 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1524 } else if (ifp->if_flags & IFF_RUNNING &&
1525 !(ifp->if_flags & IFF_PROMISC) &&
1526 sc->aue_if_flags & IFF_PROMISC) {
1527 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1528 } else if (!(ifp->if_flags & IFF_RUNNING))
1529 aue_init(sc);
1530 } else {
1531 if (ifp->if_flags & IFF_RUNNING)
1532 aue_stop(sc);
1533 }
1534 sc->aue_if_flags = ifp->if_flags;
1535 error = 0;
1536 break;
1537 case SIOCADDMULTI:
1538 case SIOCDELMULTI:
1539 error = (command == SIOCADDMULTI) ?
1540 ether_addmulti(ifr, &sc->aue_ec) :
1541 ether_delmulti(ifr, &sc->aue_ec);
1542 if (error == ENETRESET) {
1543 aue_init(sc);
1544 }
1545 aue_setmulti(sc);
1546 error = 0;
1547 break;
1548 case SIOCGIFMEDIA:
1549 case SIOCSIFMEDIA:
1550 mii = GET_MII(sc);
1551 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1552 break;
1553 default:
1554 error = EINVAL;
1555 break;
1556 }
1557
1558 splx(s);
1559
1560 return (error);
1561 }
1562
1563 Static void
1564 aue_watchdog(struct ifnet *ifp)
1565 {
1566 struct aue_softc *sc = ifp->if_softc;
1567 struct aue_chain *c;
1568 usbd_status stat;
1569 int s;
1570
1571 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1572
1573 ifp->if_oerrors++;
1574 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1575
1576 s = splusb();
1577 c = &sc->aue_cdata.aue_tx_chain[0];
1578 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1579 aue_txeof(c->aue_xfer, c, stat);
1580
1581 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1582 aue_start(ifp);
1583 splx(s);
1584 }
1585
1586 /*
1587 * Stop the adapter and free any mbufs allocated to the
1588 * RX and TX lists.
1589 */
1590 Static void
1591 aue_stop(struct aue_softc *sc)
1592 {
1593 usbd_status err;
1594 struct ifnet *ifp;
1595 int i;
1596
1597 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1598
1599 ifp = GET_IFP(sc);
1600 ifp->if_timer = 0;
1601
1602 aue_csr_write_1(sc, AUE_CTL0, 0);
1603 aue_csr_write_1(sc, AUE_CTL1, 0);
1604 aue_reset(sc);
1605 usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
1606
1607 /* Stop transfers. */
1608 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1609 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1610 if (err) {
1611 printf("%s: abort rx pipe failed: %s\n",
1612 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1613 }
1614 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1615 if (err) {
1616 printf("%s: close rx pipe failed: %s\n",
1617 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1618 }
1619 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1620 }
1621
1622 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1623 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1624 if (err) {
1625 printf("%s: abort tx pipe failed: %s\n",
1626 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1627 }
1628 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1629 if (err) {
1630 printf("%s: close tx pipe failed: %s\n",
1631 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1632 }
1633 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1634 }
1635
1636 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1637 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1638 if (err) {
1639 printf("%s: abort intr pipe failed: %s\n",
1640 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1641 }
1642 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1643 if (err) {
1644 printf("%s: close intr pipe failed: %s\n",
1645 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1646 }
1647 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1648 }
1649
1650 /* Free RX resources. */
1651 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1652 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1653 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1654 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1655 }
1656 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1657 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1658 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1659 }
1660 }
1661
1662 /* Free TX resources. */
1663 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1664 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1665 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1666 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1667 }
1668 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1669 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1670 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1671 }
1672 }
1673
1674 sc->aue_link = 0;
1675
1676 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1677 }
1678