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