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