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