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