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