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