if_aue.c revision 1.48.2.8 1 /* $NetBSD: if_aue.c,v 1.48.2.8 2001/04/21 17:49:52 bouyer 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 usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
797 /*
798 * Remove any pending tasks. They cannot be executing because they run
799 * in the same thread as detach.
800 */
801 usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
802 usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
803
804 s = splusb();
805
806 if (!sc->aue_attached) {
807 /* Detached before attached finished, so just bail out. */
808 splx(s);
809 return (0);
810 }
811
812 if (ifp->if_flags & IFF_RUNNING)
813 aue_stop(sc);
814
815 #if defined(__NetBSD__)
816 #if NRND > 0
817 rnd_detach_source(&sc->rnd_source);
818 #endif
819 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
820 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
821 ether_ifdetach(ifp);
822 #endif /* __NetBSD__ */
823
824 if_detach(ifp);
825
826 #ifdef DIAGNOSTIC
827 if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
828 sc->aue_ep[AUE_ENDPT_RX] != NULL ||
829 sc->aue_ep[AUE_ENDPT_INTR] != NULL)
830 printf("%s: detach has active endpoints\n",
831 USBDEVNAME(sc->aue_dev));
832 #endif
833
834 sc->aue_attached = 0;
835 splx(s);
836
837 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
838 USBDEV(sc->aue_dev));
839
840 return (0);
841 }
842
843 int
844 aue_activate(device_ptr_t self, enum devact act)
845 {
846 struct aue_softc *sc = (struct aue_softc *)self;
847
848 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
849
850 switch (act) {
851 case DVACT_ACTIVATE:
852 return (EOPNOTSUPP);
853 break;
854
855 case DVACT_DEACTIVATE:
856 if_deactivate(&sc->aue_ec.ec_if);
857 sc->aue_dying = 1;
858 break;
859 }
860 return (0);
861 }
862
863 /*
864 * Initialize an RX descriptor and attach an MBUF cluster.
865 */
866 Static int
867 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
868 {
869 struct mbuf *m_new = NULL;
870
871 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
872
873 if (m == NULL) {
874 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
875 if (m_new == NULL) {
876 printf("%s: no memory for rx list "
877 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
878 return (ENOBUFS);
879 }
880
881 MCLGET(m_new, M_DONTWAIT);
882 if (!(m_new->m_flags & M_EXT)) {
883 printf("%s: no memory for rx list "
884 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
885 m_freem(m_new);
886 return (ENOBUFS);
887 }
888 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
889 } else {
890 m_new = m;
891 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
892 m_new->m_data = m_new->m_ext.ext_buf;
893 }
894
895 m_adj(m_new, ETHER_ALIGN);
896 c->aue_mbuf = m_new;
897
898 return (0);
899 }
900
901 Static int
902 aue_rx_list_init(struct aue_softc *sc)
903 {
904 struct aue_cdata *cd;
905 struct aue_chain *c;
906 int i;
907
908 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
909
910 cd = &sc->aue_cdata;
911 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
912 c = &cd->aue_rx_chain[i];
913 c->aue_sc = sc;
914 c->aue_idx = i;
915 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
916 return (ENOBUFS);
917 if (c->aue_xfer == NULL) {
918 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
919 if (c->aue_xfer == NULL)
920 return (ENOBUFS);
921 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
922 if (c->aue_buf == NULL)
923 return (ENOBUFS); /* XXX free xfer */
924 }
925 }
926
927 return (0);
928 }
929
930 Static int
931 aue_tx_list_init(struct aue_softc *sc)
932 {
933 struct aue_cdata *cd;
934 struct aue_chain *c;
935 int i;
936
937 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
938
939 cd = &sc->aue_cdata;
940 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
941 c = &cd->aue_tx_chain[i];
942 c->aue_sc = sc;
943 c->aue_idx = i;
944 c->aue_mbuf = NULL;
945 if (c->aue_xfer == NULL) {
946 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
947 if (c->aue_xfer == NULL)
948 return (ENOBUFS);
949 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
950 if (c->aue_buf == NULL)
951 return (ENOBUFS);
952 }
953 }
954
955 return (0);
956 }
957
958 Static void
959 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
960 {
961 struct aue_softc *sc = priv;
962 struct ifnet *ifp = GET_IFP(sc);
963 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
964
965 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
966
967 if (sc->aue_dying)
968 return;
969
970 if (!(ifp->if_flags & IFF_RUNNING))
971 return;
972
973 if (status != USBD_NORMAL_COMPLETION) {
974 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
975 return;
976 }
977 sc->aue_intr_errs++;
978 if (usbd_ratecheck(&sc->aue_rx_notice)) {
979 printf("%s: %u usb errors on intr: %s\n",
980 USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
981 usbd_errstr(status));
982 sc->aue_intr_errs = 0;
983 }
984 if (status == USBD_STALLED)
985 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
986 return;
987 }
988
989 if (p->aue_txstat0)
990 ifp->if_oerrors++;
991
992 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
993 ifp->if_collisions++;
994 }
995
996 /*
997 * A frame has been uploaded: pass the resulting mbuf chain up to
998 * the higher level protocols.
999 */
1000 Static void
1001 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1002 {
1003 struct aue_chain *c = priv;
1004 struct aue_softc *sc = c->aue_sc;
1005 struct ifnet *ifp = GET_IFP(sc);
1006 struct mbuf *m;
1007 u_int32_t total_len;
1008 struct aue_rxpkt r;
1009 int s;
1010
1011 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1012
1013 if (sc->aue_dying)
1014 return;
1015
1016 if (!(ifp->if_flags & IFF_RUNNING))
1017 return;
1018
1019 if (status != USBD_NORMAL_COMPLETION) {
1020 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1021 return;
1022 sc->aue_rx_errs++;
1023 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1024 printf("%s: %u usb errors on rx: %s\n",
1025 USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
1026 usbd_errstr(status));
1027 sc->aue_rx_errs = 0;
1028 }
1029 if (status == USBD_STALLED)
1030 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1031 goto done;
1032 }
1033
1034 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1035
1036 memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
1037
1038 if (total_len <= 4 + ETHER_CRC_LEN) {
1039 ifp->if_ierrors++;
1040 goto done;
1041 }
1042
1043 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1044
1045 /* Turn off all the non-error bits in the rx status word. */
1046 r.aue_rxstat &= AUE_RXSTAT_MASK;
1047 if (r.aue_rxstat) {
1048 ifp->if_ierrors++;
1049 goto done;
1050 }
1051
1052 /* No errors; receive the packet. */
1053 m = c->aue_mbuf;
1054 total_len -= ETHER_CRC_LEN + 4;
1055 m->m_pkthdr.len = m->m_len = total_len;
1056 ifp->if_ipackets++;
1057
1058 m->m_pkthdr.rcvif = ifp;
1059
1060 s = splnet();
1061
1062 /* XXX ugly */
1063 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1064 ifp->if_ierrors++;
1065 goto done1;
1066 }
1067
1068 #if NBPFILTER > 0
1069 /*
1070 * Handle BPF listeners. Let the BPF user see the packet, but
1071 * don't pass it up to the ether_input() layer unless it's
1072 * a broadcast packet, multicast packet, matches our ethernet
1073 * address or the interface is in promiscuous mode.
1074 */
1075 if (ifp->if_bpf)
1076 BPF_MTAP(ifp, m);
1077 #endif
1078
1079 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1080 __FUNCTION__, m->m_len));
1081 IF_INPUT(ifp, m);
1082 done1:
1083 splx(s);
1084
1085 done:
1086
1087 /* Setup new transfer. */
1088 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1089 c, c->aue_buf, AUE_BUFSZ,
1090 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1091 USBD_NO_TIMEOUT, aue_rxeof);
1092 usbd_transfer(xfer);
1093
1094 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1095 __FUNCTION__));
1096 }
1097
1098 /*
1099 * A frame was downloaded to the chip. It's safe for us to clean up
1100 * the list buffers.
1101 */
1102
1103 Static void
1104 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1105 {
1106 struct aue_chain *c = priv;
1107 struct aue_softc *sc = c->aue_sc;
1108 struct ifnet *ifp = GET_IFP(sc);
1109 int s;
1110
1111 if (sc->aue_dying)
1112 return;
1113
1114 s = splnet();
1115
1116 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1117 __FUNCTION__, status));
1118
1119 ifp->if_timer = 0;
1120 ifp->if_flags &= ~IFF_OACTIVE;
1121
1122 if (status != USBD_NORMAL_COMPLETION) {
1123 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1124 splx(s);
1125 return;
1126 }
1127 ifp->if_oerrors++;
1128 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1129 usbd_errstr(status));
1130 if (status == USBD_STALLED)
1131 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1132 splx(s);
1133 return;
1134 }
1135
1136 ifp->if_opackets++;
1137
1138 m_freem(c->aue_mbuf);
1139 c->aue_mbuf = NULL;
1140
1141 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1142 aue_start(ifp);
1143
1144 splx(s);
1145 }
1146
1147 Static void
1148 aue_tick(void *xsc)
1149 {
1150 struct aue_softc *sc = xsc;
1151
1152 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1153
1154 if (sc == NULL)
1155 return;
1156
1157 if (sc->aue_dying)
1158 return;
1159
1160 /* Perform periodic stuff in process context. */
1161 usb_add_task(sc->aue_udev, &sc->aue_tick_task);
1162 }
1163
1164 Static void
1165 aue_tick_task(void *xsc)
1166 {
1167 struct aue_softc *sc = xsc;
1168 struct ifnet *ifp;
1169 struct mii_data *mii;
1170 int s;
1171
1172 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1173
1174 if (sc->aue_dying)
1175 return;
1176
1177 ifp = GET_IFP(sc);
1178 mii = GET_MII(sc);
1179 if (mii == NULL)
1180 return;
1181
1182 s = splnet();
1183
1184 mii_tick(mii);
1185 if (!sc->aue_link) {
1186 mii_pollstat(mii);
1187 if (mii->mii_media_status & IFM_ACTIVE &&
1188 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1189 DPRINTFN(2,("%s: %s: got link\n",
1190 USBDEVNAME(sc->aue_dev),__FUNCTION__));
1191 sc->aue_link++;
1192 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1193 aue_start(ifp);
1194 }
1195 }
1196
1197 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1198
1199 splx(s);
1200 }
1201
1202 Static int
1203 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1204 {
1205 int total_len;
1206 struct aue_chain *c;
1207 usbd_status err;
1208
1209 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1210
1211 c = &sc->aue_cdata.aue_tx_chain[idx];
1212
1213 /*
1214 * Copy the mbuf data into a contiguous buffer, leaving two
1215 * bytes at the beginning to hold the frame length.
1216 */
1217 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1218 c->aue_mbuf = m;
1219
1220 /*
1221 * The ADMtek documentation says that the packet length is
1222 * supposed to be specified in the first two bytes of the
1223 * transfer, however it actually seems to ignore this info
1224 * and base the frame size on the bulk transfer length.
1225 */
1226 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1227 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1228 total_len = m->m_pkthdr.len + 2;
1229
1230 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1231 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1232 AUE_TX_TIMEOUT, aue_txeof);
1233
1234 /* Transmit */
1235 err = usbd_transfer(c->aue_xfer);
1236 if (err != USBD_IN_PROGRESS) {
1237 printf("%s: aue_send error=%s\n", USBDEVNAME(sc->aue_dev),
1238 usbd_errstr(err));
1239 /* Stop the interface from process context. */
1240 usb_add_task(sc->aue_udev, &sc->aue_stop_task);
1241 return (EIO);
1242 }
1243 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1244 __FUNCTION__, total_len));
1245
1246 sc->aue_cdata.aue_tx_cnt++;
1247
1248 return (0);
1249 }
1250
1251 Static void
1252 aue_start(struct ifnet *ifp)
1253 {
1254 struct aue_softc *sc = ifp->if_softc;
1255 struct mbuf *m_head = NULL;
1256
1257 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1258 __FUNCTION__, sc->aue_link));
1259
1260 if (sc->aue_dying)
1261 return;
1262
1263 if (!sc->aue_link)
1264 return;
1265
1266 if (ifp->if_flags & IFF_OACTIVE)
1267 return;
1268
1269 IFQ_POLL(&ifp->if_snd, m_head);
1270 if (m_head == NULL)
1271 return;
1272
1273 if (aue_send(sc, m_head, 0)) {
1274 ifp->if_flags |= IFF_OACTIVE;
1275 return;
1276 }
1277
1278 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1279
1280 #if NBPFILTER > 0
1281 /*
1282 * If there's a BPF listener, bounce a copy of this frame
1283 * to him.
1284 */
1285 if (ifp->if_bpf)
1286 BPF_MTAP(ifp, m_head);
1287 #endif
1288
1289 ifp->if_flags |= IFF_OACTIVE;
1290
1291 /*
1292 * Set a timeout in case the chip goes out to lunch.
1293 */
1294 ifp->if_timer = 5;
1295 }
1296
1297 Static void
1298 aue_init(void *xsc)
1299 {
1300 struct aue_softc *sc = xsc;
1301 struct ifnet *ifp = GET_IFP(sc);
1302 struct mii_data *mii = GET_MII(sc);
1303 int i, s;
1304 u_char *eaddr;
1305
1306 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1307
1308 if (sc->aue_dying)
1309 return;
1310
1311 if (ifp->if_flags & IFF_RUNNING)
1312 return;
1313
1314 s = splnet();
1315
1316 /*
1317 * Cancel pending I/O and free all RX/TX buffers.
1318 */
1319 aue_reset(sc);
1320
1321 #if defined(__OpenBSD__)
1322 eaddr = sc->arpcom.ac_enaddr;
1323 #elif defined(__NetBSD__)
1324 eaddr = LLADDR(ifp->if_sadl);
1325 #endif /* defined(__NetBSD__) */
1326 for (i = 0; i < ETHER_ADDR_LEN; i++)
1327 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1328
1329 /* If we want promiscuous mode, set the allframes bit. */
1330 if (ifp->if_flags & IFF_PROMISC)
1331 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1332 else
1333 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1334
1335 /* Init TX ring. */
1336 if (aue_tx_list_init(sc) == ENOBUFS) {
1337 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1338 splx(s);
1339 return;
1340 }
1341
1342 /* Init RX ring. */
1343 if (aue_rx_list_init(sc) == ENOBUFS) {
1344 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1345 splx(s);
1346 return;
1347 }
1348
1349 /* Load the multicast filter. */
1350 aue_setmulti(sc);
1351
1352 /* Enable RX and TX */
1353 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1354 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1355 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1356
1357 mii_mediachg(mii);
1358
1359 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1360 if (aue_openpipes(sc)) {
1361 splx(s);
1362 return;
1363 }
1364 }
1365
1366 ifp->if_flags |= IFF_RUNNING;
1367 ifp->if_flags &= ~IFF_OACTIVE;
1368
1369 splx(s);
1370
1371 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1372 }
1373
1374 Static int
1375 aue_openpipes(struct aue_softc *sc)
1376 {
1377 struct aue_chain *c;
1378 usbd_status err;
1379 int i;
1380
1381 /* Open RX and TX pipes. */
1382 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1383 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1384 if (err) {
1385 printf("%s: open rx pipe failed: %s\n",
1386 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1387 return (EIO);
1388 }
1389 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1390 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1391 if (err) {
1392 printf("%s: open tx pipe failed: %s\n",
1393 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1394 return (EIO);
1395 }
1396 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1397 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1398 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1399 AUE_INTR_INTERVAL);
1400 if (err) {
1401 printf("%s: open intr pipe failed: %s\n",
1402 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1403 return (EIO);
1404 }
1405
1406 /* Start up the receive pipe. */
1407 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1408 c = &sc->aue_cdata.aue_rx_chain[i];
1409 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1410 c, c->aue_buf, AUE_BUFSZ,
1411 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1412 aue_rxeof);
1413 (void)usbd_transfer(c->aue_xfer); /* XXX */
1414 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1415 __FUNCTION__));
1416
1417 }
1418 return (0);
1419 }
1420
1421 /*
1422 * Set media options.
1423 */
1424 Static int
1425 aue_ifmedia_upd(struct ifnet *ifp)
1426 {
1427 struct aue_softc *sc = ifp->if_softc;
1428 struct mii_data *mii = GET_MII(sc);
1429
1430 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1431
1432 if (sc->aue_dying)
1433 return (0);
1434
1435 sc->aue_link = 0;
1436 if (mii->mii_instance) {
1437 struct mii_softc *miisc;
1438 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1439 miisc = LIST_NEXT(miisc, mii_list))
1440 mii_phy_reset(miisc);
1441 }
1442 mii_mediachg(mii);
1443
1444 return (0);
1445 }
1446
1447 /*
1448 * Report current media status.
1449 */
1450 Static void
1451 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1452 {
1453 struct aue_softc *sc = ifp->if_softc;
1454 struct mii_data *mii = GET_MII(sc);
1455
1456 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1457
1458 mii_pollstat(mii);
1459 ifmr->ifm_active = mii->mii_media_active;
1460 ifmr->ifm_status = mii->mii_media_status;
1461 }
1462
1463 Static int
1464 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1465 {
1466 struct aue_softc *sc = ifp->if_softc;
1467 struct ifaddr *ifa = (struct ifaddr *)data;
1468 struct ifreq *ifr = (struct ifreq *)data;
1469 struct mii_data *mii;
1470 int s, error = 0;
1471
1472 if (sc->aue_dying)
1473 return (EIO);
1474
1475 s = splnet();
1476
1477 switch(command) {
1478 case SIOCSIFADDR:
1479 ifp->if_flags |= IFF_UP;
1480 aue_init(sc);
1481
1482 switch (ifa->ifa_addr->sa_family) {
1483 #ifdef INET
1484 case AF_INET:
1485 #if defined(__NetBSD__)
1486 arp_ifinit(ifp, ifa);
1487 #else
1488 arp_ifinit(&sc->arpcom, ifa);
1489 #endif
1490 break;
1491 #endif /* INET */
1492 #ifdef NS
1493 case AF_NS:
1494 {
1495 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1496
1497 if (ns_nullhost(*ina))
1498 ina->x_host = *(union ns_host *)
1499 LLADDR(ifp->if_sadl);
1500 else
1501 memcpy(LLADDR(ifp->if_sadl),
1502 ina->x_host.c_host,
1503 ifp->if_addrlen);
1504 break;
1505 }
1506 #endif /* NS */
1507 }
1508 break;
1509
1510 case SIOCSIFMTU:
1511 if (ifr->ifr_mtu > ETHERMTU)
1512 error = EINVAL;
1513 else
1514 ifp->if_mtu = ifr->ifr_mtu;
1515 break;
1516
1517 case SIOCSIFFLAGS:
1518 if (ifp->if_flags & IFF_UP) {
1519 if (ifp->if_flags & IFF_RUNNING &&
1520 ifp->if_flags & IFF_PROMISC &&
1521 !(sc->aue_if_flags & IFF_PROMISC)) {
1522 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1523 } else if (ifp->if_flags & IFF_RUNNING &&
1524 !(ifp->if_flags & IFF_PROMISC) &&
1525 sc->aue_if_flags & IFF_PROMISC) {
1526 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1527 } else if (!(ifp->if_flags & IFF_RUNNING))
1528 aue_init(sc);
1529 } else {
1530 if (ifp->if_flags & IFF_RUNNING)
1531 aue_stop(sc);
1532 }
1533 sc->aue_if_flags = ifp->if_flags;
1534 error = 0;
1535 break;
1536 case SIOCADDMULTI:
1537 case SIOCDELMULTI:
1538 error = (command == SIOCADDMULTI) ?
1539 ether_addmulti(ifr, &sc->aue_ec) :
1540 ether_delmulti(ifr, &sc->aue_ec);
1541 if (error == ENETRESET) {
1542 aue_init(sc);
1543 }
1544 aue_setmulti(sc);
1545 error = 0;
1546 break;
1547 case SIOCGIFMEDIA:
1548 case SIOCSIFMEDIA:
1549 mii = GET_MII(sc);
1550 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1551 break;
1552 default:
1553 error = EINVAL;
1554 break;
1555 }
1556
1557 splx(s);
1558
1559 return (error);
1560 }
1561
1562 Static void
1563 aue_watchdog(struct ifnet *ifp)
1564 {
1565 struct aue_softc *sc = ifp->if_softc;
1566 struct aue_chain *c;
1567 usbd_status stat;
1568 int s;
1569
1570 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1571
1572 ifp->if_oerrors++;
1573 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1574
1575 s = splusb();
1576 c = &sc->aue_cdata.aue_tx_chain[0];
1577 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1578 aue_txeof(c->aue_xfer, c, stat);
1579
1580 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1581 aue_start(ifp);
1582 splx(s);
1583 }
1584
1585 /*
1586 * Stop the adapter and free any mbufs allocated to the
1587 * RX and TX lists.
1588 */
1589 Static void
1590 aue_stop(struct aue_softc *sc)
1591 {
1592 usbd_status err;
1593 struct ifnet *ifp;
1594 int i;
1595
1596 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1597
1598 ifp = GET_IFP(sc);
1599 ifp->if_timer = 0;
1600
1601 aue_csr_write_1(sc, AUE_CTL0, 0);
1602 aue_csr_write_1(sc, AUE_CTL1, 0);
1603 aue_reset(sc);
1604 usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
1605
1606 /* Stop transfers. */
1607 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1608 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1609 if (err) {
1610 printf("%s: abort rx pipe failed: %s\n",
1611 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1612 }
1613 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1614 if (err) {
1615 printf("%s: close rx pipe failed: %s\n",
1616 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1617 }
1618 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1619 }
1620
1621 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1622 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1623 if (err) {
1624 printf("%s: abort tx pipe failed: %s\n",
1625 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1626 }
1627 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1628 if (err) {
1629 printf("%s: close tx pipe failed: %s\n",
1630 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1631 }
1632 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1633 }
1634
1635 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1636 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1637 if (err) {
1638 printf("%s: abort intr pipe failed: %s\n",
1639 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1640 }
1641 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1642 if (err) {
1643 printf("%s: close intr pipe failed: %s\n",
1644 USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1645 }
1646 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1647 }
1648
1649 /* Free RX resources. */
1650 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1651 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1652 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1653 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1654 }
1655 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1656 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1657 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1658 }
1659 }
1660
1661 /* Free TX resources. */
1662 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1663 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1664 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1665 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1666 }
1667 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1668 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1669 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1670 }
1671 }
1672
1673 sc->aue_link = 0;
1674
1675 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1676 }
1677