if_aue.c revision 1.132.4.17 1 /* $NetBSD: if_aue.c,v 1.132.4.17 2017/02/05 13:40:46 skrll 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.4.17 2017/02/05 13:40:46 skrll 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 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_extflags = IFEF_START_MPSAFE;
833 ifp->if_init = aue_init;
834 ifp->if_ioctl = aue_ioctl;
835 ifp->if_start = aue_start;
836 ifp->if_watchdog = aue_watchdog;
837 strlcpy(ifp->if_xname, device_xname(sc->aue_dev), IFNAMSIZ);
838
839 IFQ_SET_READY(&ifp->if_snd);
840
841 /* Initialize MII/media info. */
842 mii = &sc->aue_mii;
843 mii->mii_ifp = ifp;
844 mii->mii_readreg = aue_miibus_readreg;
845 mii->mii_writereg = aue_miibus_writereg;
846 mii->mii_statchg = aue_miibus_statchg;
847 mii->mii_flags = MIIF_AUTOTSLEEP;
848 sc->aue_ec.ec_mii = mii;
849 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, ether_mediastatus);
850 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
851 if (LIST_FIRST(&mii->mii_phys) == NULL) {
852 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
853 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
854 } else
855 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
856
857 /* Attach the interface. */
858 if_initialize(ifp);
859 sc->aue_ipq = if_percpuq_create(&sc->aue_ec.ec_if);
860 ether_ifattach(ifp, eaddr);
861 if_register(ifp);
862 ether_set_ifflags_cb(&sc->aue_ec, aue_ifflags_cb);
863 rnd_attach_source(&sc->rnd_source, device_xname(sc->aue_dev),
864 RND_TYPE_NET, RND_FLAG_DEFAULT);
865
866 callout_init(&(sc->aue_stat_ch), 0);
867
868 sc->aue_attached = 1;
869
870 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, sc->aue_dev);
871
872 return;
873 }
874
875 int
876 aue_detach(device_t self, int flags)
877 {
878 struct aue_softc *sc = device_private(self);
879 struct ifnet *ifp = GET_IFP(sc);
880 int s;
881
882 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
883
884 if (!sc->aue_attached) {
885 /* Detached before attached finished, so just bail out. */
886 return 0;
887 }
888
889 callout_stop(&sc->aue_stat_ch);
890 /*
891 * Remove any pending tasks. They cannot be executing because they run
892 * in the same thread as detach.
893 */
894 usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
895 usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
896
897 sc->aue_closing = 1;
898
899 mutex_enter(&sc->aue_mcmtx);
900 cv_wait(&sc->aue_closemc,&sc->aue_mcmtx);
901 mutex_exit(&sc->aue_mcmtx);
902
903 mutex_destroy(&sc->aue_mcmtx);
904 cv_destroy(&sc->aue_closemc);
905
906 s = splusb();
907
908 if (ifp->if_flags & IFF_RUNNING)
909 aue_stop(ifp, 1);
910
911 rnd_detach_source(&sc->rnd_source);
912 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
913 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
914 ether_ifdetach(ifp);
915
916 if_detach(ifp);
917
918 #ifdef DIAGNOSTIC
919 if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
920 sc->aue_ep[AUE_ENDPT_RX] != NULL ||
921 sc->aue_ep[AUE_ENDPT_INTR] != NULL)
922 aprint_error_dev(self, "detach has active endpoints\n");
923 #endif
924
925 sc->aue_attached = 0;
926
927 if (--sc->aue_refcnt >= 0) {
928 /* Wait for processes to go away. */
929 usb_detach_waitold(sc->aue_dev);
930 }
931 splx(s);
932
933 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, sc->aue_dev);
934
935 mutex_destroy(&sc->aue_mii_lock);
936 mutex_destroy(&sc->aue_lock);
937 mutex_destroy(&sc->aue_txlock);
938 mutex_destroy(&sc->aue_rxlock);
939 #if 0
940 mutex_destroy(&sc->wkmtx);
941 #endif
942 return 0;
943 }
944
945 int
946 aue_activate(device_t self, enum devact act)
947 {
948 struct aue_softc *sc = device_private(self);
949
950 DPRINTFN(2,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
951
952 switch (act) {
953 case DVACT_DEACTIVATE:
954 if_deactivate(&sc->aue_ec.ec_if);
955 sc->aue_dying = 1;
956 return 0;
957 default:
958 return EOPNOTSUPP;
959 }
960 }
961
962 /*
963 * Initialize an RX descriptor and attach an MBUF cluster.
964 */
965 Static int
966 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
967 {
968 struct mbuf *m_new = NULL;
969
970 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
971
972 if (m == NULL) {
973 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
974 if (m_new == NULL) {
975 aprint_error_dev(sc->aue_dev, "no memory for rx list "
976 "-- packet dropped!\n");
977 return ENOBUFS;
978 }
979
980 MCLGET(m_new, M_DONTWAIT);
981 if (!(m_new->m_flags & M_EXT)) {
982 aprint_error_dev(sc->aue_dev, "no memory for rx "
983 "list -- packet dropped!\n");
984 m_freem(m_new);
985 return ENOBUFS;
986 }
987 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
988 } else {
989 m_new = m;
990 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
991 m_new->m_data = m_new->m_ext.ext_buf;
992 }
993
994 m_adj(m_new, ETHER_ALIGN);
995 c->aue_mbuf = m_new;
996
997 return 0;
998 }
999
1000 Static int
1001 aue_rx_list_init(struct aue_softc *sc)
1002 {
1003 struct aue_cdata *cd;
1004 struct aue_chain *c;
1005 int i;
1006
1007 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1008
1009 cd = &sc->aue_cdata;
1010 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1011 c = &cd->aue_rx_chain[i];
1012 c->aue_sc = sc;
1013 c->aue_idx = i;
1014 if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1015 return ENOBUFS;
1016 if (c->aue_xfer == NULL) {
1017 int err = usbd_create_xfer(sc->aue_ep[AUE_ENDPT_RX],
1018 AUE_BUFSZ, USBD_SHORT_XFER_OK, 0, &c->aue_xfer);
1019 if (err) {
1020 return err;
1021 }
1022 c->aue_buf = usbd_get_buffer(c->aue_xfer);
1023 }
1024 }
1025
1026 return 0;
1027 }
1028
1029 Static void
1030 aue_rx_list_free(struct aue_softc *sc)
1031 {
1032 /* Free RX resources. */
1033 for (int i = 0; i < AUE_RX_LIST_CNT; i++) {
1034 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1035 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1036 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1037 }
1038 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1039 usbd_destroy_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1040 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1041 }
1042 }
1043 }
1044
1045 Static int
1046 aue_tx_list_init(struct aue_softc *sc)
1047 {
1048 struct aue_cdata *cd;
1049 struct aue_chain *c;
1050 int i;
1051
1052 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1053
1054 cd = &sc->aue_cdata;
1055 for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1056 c = &cd->aue_tx_chain[i];
1057 c->aue_sc = sc;
1058 c->aue_idx = i;
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_tx_list_free(struct aue_softc *sc)
1075 {
1076 /* Free TX resources. */
1077 for (int i = 0; i < AUE_TX_LIST_CNT; i++) {
1078 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1079 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1080 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1081 }
1082 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1083 usbd_destroy_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1084 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1085 }
1086 }
1087 }
1088
1089 Static void
1090 aue_intr(struct usbd_xfer *xfer, void *priv,
1091 usbd_status status)
1092 {
1093 struct aue_softc *sc = priv;
1094 struct ifnet *ifp = GET_IFP(sc);
1095 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf;
1096
1097 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1098
1099 if (sc->aue_dying)
1100 return;
1101
1102 if (!(ifp->if_flags & IFF_RUNNING))
1103 return;
1104
1105 if (status != USBD_NORMAL_COMPLETION) {
1106 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1107 return;
1108 }
1109 sc->aue_intr_errs++;
1110 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1111 aprint_debug_dev(sc->aue_dev,
1112 "%u usb errors on intr: %s\n", sc->aue_intr_errs,
1113 usbd_errstr(status));
1114 sc->aue_intr_errs = 0;
1115 }
1116 if (status == USBD_STALLED)
1117 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1118 return;
1119 }
1120
1121 if (p->aue_txstat0)
1122 ifp->if_oerrors++;
1123
1124 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1125 ifp->if_collisions++;
1126 }
1127
1128 /*
1129 * A frame has been uploaded: pass the resulting mbuf chain up to
1130 * the higher level protocols.
1131 */
1132 Static void
1133 aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1134 {
1135 struct aue_chain *c = priv;
1136 struct aue_softc *sc = c->aue_sc;
1137 struct ifnet *ifp = GET_IFP(sc);
1138 struct mbuf *m;
1139 uint32_t total_len;
1140 struct aue_rxpkt r;
1141 int s;
1142
1143 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1144
1145 if (sc->aue_dying)
1146 return;
1147
1148 if (!(ifp->if_flags & IFF_RUNNING))
1149 return;
1150
1151 if (status != USBD_NORMAL_COMPLETION) {
1152 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1153 return;
1154 sc->aue_rx_errs++;
1155 if (usbd_ratecheck(&sc->aue_rx_notice)) {
1156 aprint_error_dev(sc->aue_dev,
1157 "%u usb errors on rx: %s\n", sc->aue_rx_errs,
1158 usbd_errstr(status));
1159 sc->aue_rx_errs = 0;
1160 }
1161 if (status == USBD_STALLED)
1162 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1163 goto done;
1164 }
1165
1166 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1167
1168 memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
1169
1170 if (total_len <= 4 + ETHER_CRC_LEN) {
1171 ifp->if_ierrors++;
1172 goto done;
1173 }
1174
1175 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1176
1177 /* Turn off all the non-error bits in the rx status word. */
1178 r.aue_rxstat &= AUE_RXSTAT_MASK;
1179 if (r.aue_rxstat) {
1180 ifp->if_ierrors++;
1181 goto done;
1182 }
1183
1184 /* No errors; receive the packet. */
1185 m = c->aue_mbuf;
1186 total_len -= ETHER_CRC_LEN + 4;
1187 m->m_pkthdr.len = m->m_len = total_len;
1188
1189 m_set_rcvif(m, ifp);
1190
1191 s = splnet();
1192
1193 /* XXX ugly */
1194 if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1195 ifp->if_ierrors++;
1196 goto done1;
1197 }
1198
1199 DPRINTFN(10,("%s: %s: deliver %d\n", device_xname(sc->aue_dev),
1200 __func__, m->m_len));
1201 if_percpuq_enqueue(ifp->if_percpuq, m);
1202 done1:
1203 splx(s);
1204
1205 done:
1206
1207 /* Setup new transfer. */
1208 usbd_setup_xfer(xfer, c, c->aue_buf, AUE_BUFSZ,
1209 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1210 usbd_transfer(xfer);
1211
1212 DPRINTFN(10,("%s: %s: start rx\n", device_xname(sc->aue_dev),
1213 __func__));
1214 }
1215
1216 /*
1217 * A frame was downloaded to the chip. It's safe for us to clean up
1218 * the list buffers.
1219 */
1220
1221 Static void
1222 aue_txeof(struct usbd_xfer *xfer, void *priv,
1223 usbd_status status)
1224 {
1225 struct aue_chain *c = priv;
1226 struct aue_softc *sc = c->aue_sc;
1227 struct ifnet *ifp = GET_IFP(sc);
1228 int s;
1229
1230 if (sc->aue_dying)
1231 return;
1232
1233 s = splnet();
1234
1235 DPRINTFN(10,("%s: %s: enter status=%d\n", device_xname(sc->aue_dev),
1236 __func__, status));
1237
1238 ifp->if_timer = 0;
1239 ifp->if_flags &= ~IFF_OACTIVE;
1240
1241 if (status != USBD_NORMAL_COMPLETION) {
1242 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1243 splx(s);
1244 return;
1245 }
1246 ifp->if_oerrors++;
1247 aprint_error_dev(sc->aue_dev, "usb error on tx: %s\n",
1248 usbd_errstr(status));
1249 if (status == USBD_STALLED)
1250 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
1251 splx(s);
1252 return;
1253 }
1254
1255 ifp->if_opackets++;
1256
1257 m_freem(c->aue_mbuf);
1258 c->aue_mbuf = NULL;
1259
1260 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1261 aue_start(ifp);
1262
1263 splx(s);
1264 }
1265
1266 Static void
1267 aue_tick(void *xsc)
1268 {
1269 struct aue_softc *sc = xsc;
1270
1271 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1272
1273 if (sc == NULL)
1274 return;
1275
1276 if (sc->aue_dying)
1277 return;
1278
1279 /* Perform periodic stuff in process context. */
1280 usb_add_task(sc->aue_udev, &sc->aue_tick_task, USB_TASKQ_DRIVER);
1281 }
1282
1283 Static void
1284 aue_tick_task(void *xsc)
1285 {
1286 struct aue_softc *sc = xsc;
1287 struct ifnet *ifp;
1288 struct mii_data *mii;
1289 int s;
1290
1291 DPRINTFN(15,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1292
1293 if (sc->aue_dying)
1294 return;
1295
1296 ifp = GET_IFP(sc);
1297 mii = GET_MII(sc);
1298 if (mii == NULL)
1299 return;
1300
1301 s = splnet();
1302
1303 mii_tick(mii);
1304 if (!sc->aue_link) {
1305 mii_pollstat(mii); /* XXX FreeBSD has removed this call */
1306 if (mii->mii_media_status & IFM_ACTIVE &&
1307 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1308 DPRINTFN(2,("%s: %s: got link\n",
1309 device_xname(sc->aue_dev), __func__));
1310 sc->aue_link++;
1311 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1312 aue_start(ifp);
1313 }
1314 }
1315
1316 callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
1317
1318 splx(s);
1319 }
1320
1321 Static int
1322 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1323 {
1324 int total_len;
1325 struct aue_chain *c;
1326 usbd_status err;
1327
1328 DPRINTFN(10,("%s: %s: enter\n", device_xname(sc->aue_dev),__func__));
1329
1330 c = &sc->aue_cdata.aue_tx_chain[idx];
1331
1332 /*
1333 * Copy the mbuf data into a contiguous buffer, leaving two
1334 * bytes at the beginning to hold the frame length.
1335 */
1336 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1337 c->aue_mbuf = m;
1338
1339 /*
1340 * The ADMtek documentation says that the packet length is
1341 * supposed to be specified in the first two bytes of the
1342 * transfer, however it actually seems to ignore this info
1343 * and base the frame size on the bulk transfer length.
1344 */
1345 c->aue_buf[0] = (uint8_t)m->m_pkthdr.len;
1346 c->aue_buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
1347 total_len = m->m_pkthdr.len + 2;
1348
1349 usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, total_len,
1350 USBD_FORCE_SHORT_XFER, AUE_TX_TIMEOUT, aue_txeof);
1351
1352 /* Transmit */
1353 err = usbd_transfer(c->aue_xfer);
1354 if (err != USBD_IN_PROGRESS) {
1355 aprint_error_dev(sc->aue_dev, "aue_send error=%s\n",
1356 usbd_errstr(err));
1357 /* Stop the interface from process context. */
1358 usb_add_task(sc->aue_udev, &sc->aue_stop_task,
1359 USB_TASKQ_DRIVER);
1360 return EIO;
1361 }
1362 DPRINTFN(5,("%s: %s: send %d bytes\n", device_xname(sc->aue_dev),
1363 __func__, total_len));
1364
1365 sc->aue_cdata.aue_tx_cnt++;
1366
1367 return 0;
1368 }
1369
1370 Static void
1371 aue_start(struct ifnet *ifp)
1372 {
1373 struct aue_softc *sc = ifp->if_softc;
1374 KASSERT(ifp->if_extflags & IFEF_START_MPSAFE);
1375
1376 mutex_enter(&sc->aue_txlock);
1377 aue_start_locked(ifp);
1378 mutex_exit(&sc->aue_txlock);
1379 }
1380
1381 Static void
1382 aue_start_locked(struct ifnet *ifp)
1383 {
1384 struct aue_softc *sc = ifp->if_softc;
1385 struct mbuf *m_head = NULL;
1386
1387 DPRINTFN(5,("%s: %s: enter, link=%d\n", device_xname(sc->aue_dev),
1388 __func__, sc->aue_link));
1389
1390 if (sc->aue_dying)
1391 return;
1392
1393 if (!sc->aue_link)
1394 return;
1395
1396 if (ifp->if_flags & IFF_OACTIVE)
1397 return;
1398
1399 IFQ_POLL(&ifp->if_snd, m_head);
1400 if (m_head == NULL)
1401 return;
1402
1403 if (aue_send(sc, m_head, 0)) {
1404 return;
1405 }
1406
1407 IFQ_DEQUEUE(&ifp->if_snd, m_head);
1408
1409 /*
1410 * If there's a BPF listener, bounce a copy of this frame
1411 * to him.
1412 */
1413 bpf_mtap(ifp, m_head);
1414
1415 ifp->if_flags |= IFF_OACTIVE;
1416
1417 /*
1418 * Set a timeout in case the chip goes out to lunch.
1419 */
1420 ifp->if_timer = 5;
1421 }
1422
1423 Static int
1424 aue_init(struct ifnet *ifp)
1425 {
1426 struct aue_softc *sc = ifp->if_softc;
1427
1428 mutex_enter(&sc->aue_lock);
1429 int ret = aue_init_locked(ifp);
1430 mutex_exit(&sc->aue_lock);
1431
1432 return ret;
1433 }
1434
1435 Static int
1436 aue_init_locked(struct ifnet *ifp)
1437 {
1438 struct aue_softc *sc = ifp->if_softc;
1439 struct mii_data *mii = GET_MII(sc);
1440 int i;
1441 const u_char *eaddr;
1442 usbd_status err;
1443
1444 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1445
1446 if (sc->aue_dying)
1447 return EIO;
1448
1449 aue_stop_locked(ifp, 1);
1450
1451 /*
1452 * Cancel pending I/O and free all RX/TX buffers.
1453 */
1454 aue_reset(sc);
1455
1456 eaddr = CLLADDR(ifp->if_sadl);
1457 for (i = 0; i < ETHER_ADDR_LEN; i++)
1458 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1459
1460 /* If we want promiscuous mode, set the allframes bit. */
1461 if (ifp->if_flags & IFF_PROMISC)
1462 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1463 else
1464 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1465
1466 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1467 /* Open RX and TX pipes. */
1468 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1469 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1470 if (err) {
1471 aprint_error_dev(sc->aue_dev, "open rx pipe failed: %s\n",
1472 usbd_errstr(err));
1473 goto fail;
1474 }
1475 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1476 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1477 if (err) {
1478 aprint_error_dev(sc->aue_dev, "open tx pipe failed: %s\n",
1479 usbd_errstr(err));
1480 goto fail1;
1481 }
1482 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1483 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1484 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1485 AUE_INTR_INTERVAL);
1486 if (err) {
1487 aprint_error_dev(sc->aue_dev, "open intr pipe failed: %s\n",
1488 usbd_errstr(err));
1489 goto fail2;
1490 }
1491 }
1492 /* Init TX ring. */
1493 if (aue_tx_list_init(sc)) {
1494 aprint_error_dev(sc->aue_dev, "tx list init failed\n");
1495 goto fail3;
1496 }
1497
1498 /* Init RX ring. */
1499 if (aue_rx_list_init(sc)) {
1500 aprint_error_dev(sc->aue_dev, "rx list init failed\n");
1501 goto fail4;
1502 }
1503
1504 /* Start up the receive pipe. */
1505 for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1506 struct aue_chain *c = &sc->aue_cdata.aue_rx_chain[i];
1507
1508 usbd_setup_xfer(c->aue_xfer, c, c->aue_buf, AUE_BUFSZ,
1509 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, aue_rxeof);
1510 (void)usbd_transfer(c->aue_xfer); /* XXX */
1511 DPRINTFN(5,("%s: %s: start read\n", device_xname(sc->aue_dev),
1512 __func__));
1513
1514 }
1515
1516 /* Load the multicast filter. */
1517 aue_setmulti(sc);
1518
1519 /* Enable RX and TX */
1520 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1521 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1522 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1523
1524 mii_mediachg(mii);
1525
1526 ifp->if_flags |= IFF_RUNNING;
1527 ifp->if_flags &= ~IFF_OACTIVE;
1528
1529 callout_reset(&(sc->aue_stat_ch), (hz), (aue_tick), (sc));
1530 return 0;
1531
1532 fail4:
1533 aue_tx_list_free(sc);
1534 fail3:
1535 usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1536 fail2:
1537 usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1538 fail1:
1539 usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1540 fail:
1541 return EIO;
1542 }
1543
1544 /*
1545 * Set media options.
1546 */
1547 Static int
1548 aue_ifmedia_upd(struct ifnet *ifp)
1549 {
1550 struct aue_softc *sc = ifp->if_softc;
1551 struct mii_data *mii = GET_MII(sc);
1552 int rc;
1553
1554 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1555
1556 if (sc->aue_dying)
1557 return 0;
1558
1559 sc->aue_link = 0;
1560
1561 if ((rc = mii_mediachg(mii)) == ENXIO)
1562 return 0;
1563 return rc;
1564 }
1565
1566 Static int
1567 aue_ioctl(struct ifnet *ifp, u_long command, void *data)
1568 {
1569 struct aue_softc *sc = ifp->if_softc;
1570 int s, error = 0;
1571
1572 if (sc->aue_dying)
1573 return EIO;
1574
1575 s = splnet();
1576 error = ether_ioctl(ifp, command, data);
1577 splx(s);
1578
1579 return error;
1580 }
1581
1582 Static int
1583 aue_ifflags_cb(struct ethercom *ec)
1584 {
1585 struct ifnet *ifp = &ec->ec_if;
1586 struct aue_softc *sc = ifp->if_softc;
1587
1588 mutex_enter(&sc->aue_lock);
1589
1590 if (ifp->if_flags & IFF_UP) {
1591 if (ifp->if_flags & IFF_RUNNING &&
1592 ifp->if_flags & IFF_PROMISC &&
1593 !(sc->aue_if_flags & IFF_PROMISC)) {
1594 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1595 } else if (ifp->if_flags & IFF_RUNNING &&
1596 !(ifp->if_flags & IFF_PROMISC) &&
1597 sc->aue_if_flags & IFF_PROMISC) {
1598 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1599 }
1600 }
1601 sc->aue_if_flags = ifp->if_flags;
1602 aue_setmulti(sc);
1603 mutex_exit(&sc->aue_lock);
1604
1605 return 0;
1606 }
1607
1608 Static void
1609 aue_watchdog(struct ifnet *ifp)
1610 {
1611 struct aue_softc *sc = ifp->if_softc;
1612 struct aue_chain *c;
1613 usbd_status stat;
1614 int s;
1615
1616 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1617
1618 ifp->if_oerrors++;
1619 aprint_error_dev(sc->aue_dev, "watchdog timeout\n");
1620
1621 s = splusb();
1622 c = &sc->aue_cdata.aue_tx_chain[0];
1623 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1624 aue_txeof(c->aue_xfer, c, stat);
1625
1626 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1627 aue_start(ifp);
1628 splx(s);
1629 }
1630
1631 /*
1632 * Stop the adapter and free any mbufs allocated to the
1633 * RX and TX lists.
1634 */
1635 Static void
1636 aue_stop(struct ifnet *ifp, int disable)
1637 {
1638 struct aue_softc *sc = ifp->if_softc;
1639
1640 mutex_enter(&sc->aue_lock);
1641 aue_stop_locked(ifp, disable);
1642 mutex_exit(&sc->aue_lock);
1643 }
1644
1645 Static void
1646 aue_stop_locked(struct ifnet *ifp, int disable)
1647 {
1648 usbd_status err;
1649 struct aue_softc *sc = ifp->if_softc;
1650
1651 DPRINTFN(5,("%s: %s: enter\n", device_xname(sc->aue_dev), __func__));
1652
1653 ifp->if_timer = 0;
1654
1655 aue_csr_write_1(sc, AUE_CTL0, 0);
1656 aue_csr_write_1(sc, AUE_CTL1, 0);
1657 aue_reset(sc);
1658 callout_stop(&sc->aue_stat_ch);
1659
1660 /* Stop transfers. */
1661 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1662 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1663 if (err) {
1664 printf("%s: abort rx pipe failed: %s\n",
1665 device_xname(sc->aue_dev), usbd_errstr(err));
1666 }
1667 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1668 if (err) {
1669 printf("%s: close rx pipe failed: %s\n",
1670 device_xname(sc->aue_dev), usbd_errstr(err));
1671 }
1672 sc->aue_ep[AUE_ENDPT_RX] = NULL;
1673 }
1674
1675 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1676 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1677 if (err) {
1678 printf("%s: abort tx pipe failed: %s\n",
1679 device_xname(sc->aue_dev), usbd_errstr(err));
1680 }
1681 }
1682
1683 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1684 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1685 if (err) {
1686 printf("%s: abort intr pipe failed: %s\n",
1687 device_xname(sc->aue_dev), usbd_errstr(err));
1688 }
1689 }
1690
1691 aue_rx_list_free(sc);
1692
1693 aue_tx_list_free(sc);
1694
1695 /* Close pipes */
1696 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1697 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1698 if (err) {
1699 printf("%s: close tx pipe failed: %s\n",
1700 device_xname(sc->aue_dev), usbd_errstr(err));
1701 }
1702 sc->aue_ep[AUE_ENDPT_TX] = NULL;
1703 }
1704
1705 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1706 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1707 if (err) {
1708 printf("%s: close intr pipe failed: %s\n",
1709 device_xname(sc->aue_dev), usbd_errstr(err));
1710 }
1711 sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1712 }
1713
1714 sc->aue_link = 0;
1715
1716 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1717 }
1718
1719