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