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