if_aue.c revision 1.165 1 /* $NetBSD: if_aue.c,v 1.165 2020/03/14 02:22:16 christos 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 * more error checks
74 * investigate short rx problem
75 * proper cleanup on errors
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1.165 2020/03/14 02:22:16 christos Exp $");
80
81 #ifdef _KERNEL_OPT
82 #include "opt_usb.h"
83 #include "opt_inet.h"
84 #endif
85
86 #include <sys/param.h>
87
88 #include <dev/usb/usbnet.h>
89 #include <dev/usb/usbhist.h>
90 #include <dev/usb/if_auereg.h>
91
92 #ifdef INET
93 #include <netinet/in.h>
94 #include <netinet/if_inarp.h>
95 #endif
96
97 #ifdef USB_DEBUG
98 #ifndef AUE_DEBUG
99 #define auedebug 0
100 #else
101 static int auedebug = 10;
102
103 SYSCTL_SETUP(sysctl_hw_aue_setup, "sysctl hw.aue setup")
104 {
105 int err;
106 const struct sysctlnode *rnode;
107 const struct sysctlnode *cnode;
108
109 err = sysctl_createv(clog, 0, NULL, &rnode,
110 CTLFLAG_PERMANENT, CTLTYPE_NODE, "aue",
111 SYSCTL_DESCR("aue global controls"),
112 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
113
114 if (err)
115 goto fail;
116
117 /* control debugging printfs */
118 err = sysctl_createv(clog, 0, &rnode, &cnode,
119 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
120 "debug", SYSCTL_DESCR("Enable debugging output"),
121 NULL, 0, &auedebug, sizeof(auedebug), CTL_CREATE, CTL_EOL);
122 if (err)
123 goto fail;
124
125 return;
126 fail:
127 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
128 }
129
130 #endif /* AXE_DEBUG */
131 #endif /* USB_DEBUG */
132
133 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(auedebug,1,FMT,A,B,C,D)
134 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(auedebug,N,FMT,A,B,C,D)
135 #define AUEHIST_FUNC() USBHIST_FUNC()
136 #define AUEHIST_CALLED(name) USBHIST_CALLED(auedebug)
137 #define AUEHIST_CALLARGS(FMT,A,B,C,D) \
138 USBHIST_CALLARGS(auedebug,FMT,A,B,C,D)
139 #define AUEHIST_CALLARGSN(N,FMT,A,B,C,D) \
140 USBHIST_CALLARGSN(auedebug,N,FMT,A,B,C,D)
141
142 #define AUE_TX_LIST_CNT 1
143 #define AUE_RX_LIST_CNT 1
144
145 struct aue_softc {
146 struct usbnet aue_un;
147 struct usbnet_intr aue_intr;
148 struct aue_intrpkt aue_ibuf;
149 };
150
151 #define AUE_TIMEOUT 1000
152 #define AUE_BUFSZ 1536
153 #define AUE_MIN_FRAMELEN 60
154 #define AUE_TX_TIMEOUT 10000 /* ms */
155 #define AUE_INTR_INTERVAL 100 /* ms */
156
157 /*
158 * Various supported device vendors/products.
159 */
160 struct aue_type {
161 struct usb_devno aue_dev;
162 uint16_t aue_flags;
163 #define LSYS 0x0001 /* use Linksys reset */
164 #define PNA 0x0002 /* has Home PNA */
165 #define PII 0x0004 /* Pegasus II chip */
166 };
167
168 static const struct aue_type aue_devs[] = {
169 {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII },
170 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA | PII },
171 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII },
172 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000}, LSYS },
173 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4}, PNA },
174 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5}, PNA },
175 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6}, PII },
176 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7}, PII },
177 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8}, PII },
178 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9}, PNA },
179 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10}, 0 },
180 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
181 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC}, 0 },
182 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001}, PII },
183 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS}, PNA },
184 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII}, PII },
185 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2}, PII },
186 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3}, PII },
187 {{ USB_VENDOR_AEI, USB_PRODUCT_AEI_USBTOLAN}, PII },
188 {{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN}, PII },
189 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100}, 0 },
190 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
191 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
192 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100}, PII },
193 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_HNE200}, PII },
194 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
195 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
196 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4}, LSYS | PII },
197 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1}, LSYS },
198 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX}, LSYS },
199 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA}, PNA },
200 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3}, LSYS | PII },
201 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2}, LSYS | PII },
202 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650}, 0 },
203 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0}, 0 },
204 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1}, LSYS },
205 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2}, 0 },
206 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3}, LSYS },
207 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX}, PII },
208 {{ USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET}, 0 },
209 {{ USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100}, PII },
210 {{ USB_VENDOR_HP, USB_PRODUCT_HP_HN210E}, PII },
211 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX}, 0 },
212 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS}, PII },
213 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETXUS2}, PII },
214 {{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX}, 0 },
215 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1}, LSYS | PII },
216 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T}, LSYS },
217 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX}, LSYS },
218 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1}, LSYS | PNA },
219 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA}, LSYS },
220 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2}, LSYS | PII },
221 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1}, 0 },
222 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5}, 0 },
223 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5}, PII },
224 {{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110}, PII },
225 {{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101}, PII },
226 {{ USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
227 {{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
228 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB}, 0 },
229 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB}, PII },
230 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100}, 0 },
231 };
232 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
233
234 static int aue_match(device_t, cfdata_t, void *);
235 static void aue_attach(device_t, device_t, void *);
236
237 CFATTACH_DECL_NEW(aue, sizeof(struct aue_softc), aue_match, aue_attach,
238 usbnet_detach, usbnet_activate);
239
240 static void aue_reset_pegasus_II(struct aue_softc *);
241
242 static void aue_stop_cb(struct ifnet *, int);
243 static int aue_ioctl_cb(struct ifnet *, u_long, void *);
244 static int aue_mii_read_reg(struct usbnet *, int, int, uint16_t *);
245 static int aue_mii_write_reg(struct usbnet *, int, int, uint16_t);
246 static void aue_mii_statchg(struct ifnet *);
247 static unsigned aue_tx_prepare(struct usbnet *, struct mbuf *,
248 struct usbnet_chain *);
249 static void aue_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
250 static int aue_init(struct ifnet *);
251 static void aue_intr(struct usbnet *, usbd_status);
252
253 static const struct usbnet_ops aue_ops = {
254 .uno_stop = aue_stop_cb,
255 .uno_ioctl = aue_ioctl_cb,
256 .uno_read_reg = aue_mii_read_reg,
257 .uno_write_reg = aue_mii_write_reg,
258 .uno_statchg = aue_mii_statchg,
259 .uno_tx_prepare = aue_tx_prepare,
260 .uno_rx_loop = aue_rx_loop,
261 .uno_init = aue_init,
262 .uno_intr = aue_intr,
263 };
264
265 static uint32_t aue_crc(void *);
266 static void aue_reset(struct aue_softc *);
267
268 static int aue_csr_read_1(struct aue_softc *, int);
269 static int aue_csr_write_1(struct aue_softc *, int, int);
270 static int aue_csr_read_2(struct aue_softc *, int);
271 static int aue_csr_write_2(struct aue_softc *, int, int);
272
273 #define AUE_SETBIT(sc, reg, x) \
274 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
275
276 #define AUE_CLRBIT(sc, reg, x) \
277 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
278
279 static int
280 aue_csr_read_1(struct aue_softc *sc, int reg)
281 {
282 struct usbnet * const un = &sc->aue_un;
283 usb_device_request_t req;
284 usbd_status err;
285 uByte val = 0;
286
287 usbnet_isowned_mii(un);
288
289 if (usbnet_isdying(un))
290 return 0;
291
292 req.bmRequestType = UT_READ_VENDOR_DEVICE;
293 req.bRequest = AUE_UR_READREG;
294 USETW(req.wValue, 0);
295 USETW(req.wIndex, reg);
296 USETW(req.wLength, 1);
297
298 err = usbd_do_request(un->un_udev, &req, &val);
299
300 if (err) {
301 AUEHIST_FUNC();
302 AUEHIST_CALLARGS("aue%jd: reg=%#jx err=%jd",
303 device_unit(un->un_dev), reg, err, 0);
304 return 0;
305 }
306
307 return val;
308 }
309
310 static int
311 aue_csr_read_2(struct aue_softc *sc, int reg)
312 {
313 struct usbnet * const un = &sc->aue_un;
314 usb_device_request_t req;
315 usbd_status err;
316 uWord val;
317
318 usbnet_isowned_mii(un);
319
320 if (usbnet_isdying(un))
321 return 0;
322
323 req.bmRequestType = UT_READ_VENDOR_DEVICE;
324 req.bRequest = AUE_UR_READREG;
325 USETW(req.wValue, 0);
326 USETW(req.wIndex, reg);
327 USETW(req.wLength, 2);
328
329 err = usbd_do_request(un->un_udev, &req, &val);
330
331 if (err) {
332 AUEHIST_FUNC();
333 AUEHIST_CALLARGS("aue%jd: reg=%#jx err=%jd",
334 device_unit(un->un_dev), reg, err, 0);
335 return 0;
336 }
337
338 return UGETW(val);
339 }
340
341 static int
342 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
343 {
344 struct usbnet * const un = &sc->aue_un;
345 usb_device_request_t req;
346 usbd_status err;
347 uByte val;
348
349 usbnet_isowned_mii(un);
350
351 if (usbnet_isdying(un))
352 return 0;
353
354 val = aval;
355 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
356 req.bRequest = AUE_UR_WRITEREG;
357 USETW(req.wValue, val);
358 USETW(req.wIndex, reg);
359 USETW(req.wLength, 1);
360
361 err = usbd_do_request(un->un_udev, &req, &val);
362
363 if (err) {
364 AUEHIST_FUNC();
365 AUEHIST_CALLARGS("aue%jd: reg=%#jx err=%jd",
366 device_unit(un->un_dev), reg, err, 0);
367 return -1;
368 }
369
370 return 0;
371 }
372
373 static int
374 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
375 {
376 struct usbnet * const un = &sc->aue_un;
377 usb_device_request_t req;
378 usbd_status err;
379 uWord val;
380
381 usbnet_isowned_mii(un);
382
383 if (usbnet_isdying(un))
384 return 0;
385
386 USETW(val, aval);
387 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
388 req.bRequest = AUE_UR_WRITEREG;
389 USETW(req.wValue, aval);
390 USETW(req.wIndex, reg);
391 USETW(req.wLength, 2);
392
393 err = usbd_do_request(un->un_udev, &req, &val);
394
395 if (err) {
396 AUEHIST_FUNC();
397 AUEHIST_CALLARGS("aue%jd: reg=%#jx err=%jd",
398 device_unit(un->un_dev), reg, err, 0);
399 return -1;
400 }
401
402 return 0;
403 }
404
405 /*
406 * Read a word of data stored in the EEPROM at address 'addr.'
407 */
408 static int
409 aue_eeprom_getword(struct aue_softc *sc, int addr)
410 {
411 struct usbnet * const un = &sc->aue_un;
412 int i;
413
414 AUEHIST_FUNC(); AUEHIST_CALLED();
415
416 aue_csr_write_1(sc, AUE_EE_REG, addr);
417 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
418
419 for (i = 0; i < AUE_TIMEOUT; i++) {
420 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
421 break;
422 }
423
424 if (i == AUE_TIMEOUT) {
425 printf("%s: EEPROM read timed out\n",
426 device_xname(un->un_dev));
427 }
428
429 return aue_csr_read_2(sc, AUE_EE_DATA);
430 }
431
432 /*
433 * Read the MAC from the EEPROM. It's at offset 0.
434 */
435 static void
436 aue_read_mac(struct usbnet *un)
437 {
438 struct aue_softc *sc = usbnet_softc(un);
439 int i;
440 int off = 0;
441 int word;
442
443 usbnet_isowned_mii(un);
444
445 AUEHIST_FUNC();
446 AUEHIST_CALLARGS("aue%jd: enter",
447 device_unit(un->un_dev), 0, 0, 0);
448
449 for (i = 0; i < 3; i++) {
450 word = aue_eeprom_getword(sc, off + i);
451 un->un_eaddr[2 * i] = (u_char)word;
452 un->un_eaddr[2 * i + 1] = (u_char)(word >> 8);
453 }
454 }
455
456 static int
457 aue_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
458 {
459 struct aue_softc *sc = usbnet_softc(un);
460 int i;
461
462 AUEHIST_FUNC();
463
464 usbnet_isowned_mii(un);
465
466 #if 0
467 /*
468 * The Am79C901 HomePNA PHY actually contains
469 * two transceivers: a 1Mbps HomePNA PHY and a
470 * 10Mbps full/half duplex ethernet PHY with
471 * NWAY autoneg. However in the ADMtek adapter,
472 * only the 1Mbps PHY is actually connected to
473 * anything, so we ignore the 10Mbps one. It
474 * happens to be configured for MII address 3,
475 * so we filter that out.
476 */
477 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
478 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
479 if (phy == 3)
480 return EINVAL;
481 }
482 #endif
483
484 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
485 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
486
487 for (i = 0; i < AUE_TIMEOUT; i++) {
488 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
489 break;
490 }
491
492 if (i == AUE_TIMEOUT) {
493 AUEHIST_CALLARGS("aue%jd: phy=%#jx reg=%#jx read timed out",
494 device_unit(un->un_dev), phy, reg, 0);
495 return ETIMEDOUT;
496 }
497
498 *val = aue_csr_read_2(sc, AUE_PHY_DATA);
499
500 AUEHIST_CALLARGSN(11, "aue%jd: phy=%#jx reg=%#jx => %#04jx",
501 device_unit(un->un_dev), phy, reg, *val);
502
503 return 0;
504 }
505
506 static int
507 aue_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
508 {
509 struct aue_softc *sc = usbnet_softc(un);
510 int i;
511
512 usbnet_isowned_mii(un);
513
514 AUEHIST_FUNC();
515 AUEHIST_CALLARGSN(11, "aue%jd: phy=%jd reg=%jd data=%#04jx",
516 device_unit(un->un_dev), phy, reg, val);
517
518 #if 0
519 if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
520 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
521 if (phy == 3)
522 return EINVAL;
523 }
524 #endif
525
526 aue_csr_write_2(sc, AUE_PHY_DATA, val);
527 aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
528 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
529
530 for (i = 0; i < AUE_TIMEOUT; i++) {
531 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
532 break;
533 }
534
535 if (i == AUE_TIMEOUT) {
536 DPRINTF("aue%d: phy=%#jx reg=%#jx val=%#jx write timed out",
537 device_unit(un->un_dev), phy, reg, val);
538 return ETIMEDOUT;
539 }
540
541 return 0;
542 }
543
544 static void
545 aue_mii_statchg(struct ifnet *ifp)
546 {
547 struct usbnet *un = ifp->if_softc;
548 struct aue_softc *sc = usbnet_softc(un);
549 struct mii_data *mii = usbnet_mii(un);
550 const bool hadlink __diagused = usbnet_havelink(un);
551
552 AUEHIST_FUNC(); AUEHIST_CALLED();
553 AUEHIST_CALLARGSN(5, "aue%jd: ifp=%#jx link=%jd",
554 device_unit(un->un_dev), (uintptr_t)ifp, hadlink, 0);
555
556 usbnet_lock_mii(un);
557 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
558
559 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
560 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
561 } else {
562 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
563 }
564
565 if ((mii->mii_media_active & IFM_FDX) != 0)
566 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
567 else
568 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
569
570 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
571
572 if (mii->mii_media_status & IFM_ACTIVE &&
573 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
574 usbnet_set_link(un, true);
575 }
576
577 /*
578 * Set the LED modes on the LinkSys adapter.
579 * This turns on the 'dual link LED' bin in the auxmode
580 * register of the Broadcom PHY.
581 */
582 if (!usbnet_isdying(un) && (un->un_flags & LSYS)) {
583 uint16_t auxmode;
584 aue_mii_read_reg(un, 0, 0x1b, &auxmode);
585 aue_mii_write_reg(un, 0, 0x1b, auxmode | 0x04);
586 }
587 usbnet_unlock_mii(un);
588
589 if (usbnet_havelink(un) != hadlink) {
590 DPRINTFN(5, "aue%d: exit link %d",
591 device_unit(un->un_dev), usbnet_havelink(un), 0, 0);
592 }
593 }
594
595 #define AUE_POLY 0xEDB88320
596 #define AUE_BITS 6
597
598 static uint32_t
599 aue_crc(void *addrv)
600 {
601 uint32_t idx, bit, data, crc;
602 char *addr = addrv;
603
604 /* Compute CRC for the address value. */
605 crc = 0xFFFFFFFF; /* initial value */
606
607 for (idx = 0; idx < 6; idx++) {
608 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
609 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
610 }
611
612 return crc & ((1 << AUE_BITS) - 1);
613 }
614
615 static void
616 aue_setiff_locked(struct usbnet *un)
617 {
618 struct aue_softc * const sc = usbnet_softc(un);
619 struct ifnet * const ifp = usbnet_ifp(un);
620 struct ethercom * ec = usbnet_ec(un);
621 struct ether_multi *enm;
622 struct ether_multistep step;
623 uint32_t h = 0, i;
624 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
625
626 AUEHIST_FUNC();
627 AUEHIST_CALLARGSN(5, "aue%jd: enter", device_unit(un->un_dev), 0, 0, 0);
628
629 usbnet_isowned_mii(un);
630
631 if (ifp->if_flags & IFF_PROMISC) {
632 allmulti:
633 ifp->if_flags |= IFF_ALLMULTI;
634 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
635 return;
636 }
637
638 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
639
640 /* now program new ones */
641 ETHER_LOCK(ec);
642 ETHER_FIRST_MULTI(step, ec, enm);
643 while (enm != NULL) {
644 if (memcmp(enm->enm_addrlo,
645 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
646 ETHER_UNLOCK(ec);
647 goto allmulti;
648 }
649
650 h = aue_crc(enm->enm_addrlo);
651 hashtbl[h >> 3] |= 1 << (h & 0x7);
652 ETHER_NEXT_MULTI(step, enm);
653 }
654 ETHER_UNLOCK(ec);
655
656 /* write the hashtable */
657 for (i = 0; i < 8; i++)
658 aue_csr_write_1(sc, AUE_MAR0 + i, hashtbl[i]);
659
660 ifp->if_flags &= ~IFF_ALLMULTI;
661 }
662
663 static void
664 aue_setiff(struct usbnet *un)
665 {
666 usbnet_lock_mii(un);
667 aue_setiff_locked(un);
668 usbnet_unlock_mii(un);
669 }
670
671 static void
672 aue_reset_pegasus_II(struct aue_softc *sc)
673 {
674 /* Magic constants taken from Linux driver. */
675 aue_csr_write_1(sc, AUE_REG_1D, 0);
676 aue_csr_write_1(sc, AUE_REG_7B, 2);
677 #if 0
678 if ((un->un_flags & PNA) && mii_mode)
679 aue_csr_write_1(sc, AUE_REG_81, 6);
680 else
681 #endif
682 aue_csr_write_1(sc, AUE_REG_81, 2);
683 }
684
685 static void
686 aue_reset(struct aue_softc *sc)
687 {
688 struct usbnet * const un = &sc->aue_un;
689 int i;
690
691 AUEHIST_FUNC();
692 AUEHIST_CALLARGSN(2, "aue%jd: enter", device_unit(un->un_dev), 0, 0, 0);
693
694 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
695
696 for (i = 0; i < AUE_TIMEOUT; i++) {
697 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
698 break;
699 }
700
701 if (i == AUE_TIMEOUT)
702 printf("%s: reset failed\n", device_xname(un->un_dev));
703
704 #if 0
705 /* XXX what is mii_mode supposed to be */
706 if (sc->sc_mii_mode && (un->un_flags & PNA))
707 aue_csr_write_1(sc, AUE_GPIO1, 0x34);
708 else
709 aue_csr_write_1(sc, AUE_GPIO1, 0x26);
710 #endif
711
712 /*
713 * The PHY(s) attached to the Pegasus chip may be held
714 * in reset until we flip on the GPIO outputs. Make sure
715 * to set the GPIO pins high so that the PHY(s) will
716 * be enabled.
717 *
718 * Note: We force all of the GPIO pins low first, *then*
719 * enable the ones we want.
720 */
721 if (un->un_flags & LSYS) {
722 /* Grrr. LinkSys has to be different from everyone else. */
723 aue_csr_write_1(sc, AUE_GPIO0,
724 AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
725 } else {
726 aue_csr_write_1(sc, AUE_GPIO0,
727 AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
728 }
729 aue_csr_write_1(sc, AUE_GPIO0,
730 AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
731
732 if (un->un_flags & PII)
733 aue_reset_pegasus_II(sc);
734
735 /* Wait a little while for the chip to get its brains in order. */
736 delay(10000); /* XXX */
737 //usbd_delay_ms(un->un_udev, 10); /* XXX */
738
739 DPRINTFN(2, "aue%d: exit", device_unit(un->un_dev), 0, 0, 0);
740 }
741
742 /*
743 * Probe for a Pegasus chip.
744 */
745 static int
746 aue_match(device_t parent, cfdata_t match, void *aux)
747 {
748 struct usb_attach_arg *uaa = aux;
749
750 /*
751 * Some manufacturers use the same vendor and product id for
752 * different devices. We need to sanity check the DeviceClass
753 * in this case
754 * Currently known guilty products:
755 * 0x050d/0x0121 Belkin Bluetooth and USB2LAN
756 *
757 * If this turns out to be more common, we could use a quirk
758 * table.
759 */
760 if (uaa->uaa_vendor == USB_VENDOR_BELKIN &&
761 uaa->uaa_product == USB_PRODUCT_BELKIN_USB2LAN) {
762 usb_device_descriptor_t *dd;
763
764 dd = usbd_get_device_descriptor(uaa->uaa_device);
765 if (dd != NULL &&
766 dd->bDeviceClass != UDCLASS_IN_INTERFACE)
767 return UMATCH_NONE;
768 }
769
770 return aue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
771 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
772 }
773
774 /*
775 * Attach the interface. Allocate softc structures, do ifmedia
776 * setup and ethernet/BPF attach.
777 */
778 static void
779 aue_attach(device_t parent, device_t self, void *aux)
780 {
781 USBNET_MII_DECL_DEFAULT(unm);
782 struct aue_softc * const sc = device_private(self);
783 struct usbnet * const un = &sc->aue_un;
784 struct usb_attach_arg *uaa = aux;
785 char *devinfop;
786 struct usbd_device *dev = uaa->uaa_device;
787 usbd_status err;
788 usb_interface_descriptor_t *id;
789 usb_endpoint_descriptor_t *ed;
790 int i;
791
792 AUEHIST_FUNC();
793 AUEHIST_CALLARGSN(2, "aue%jd: enter sc=%#jx",
794 device_unit(self), (uintptr_t)sc, 0, 0);
795
796 KASSERT((void *)sc == un);
797
798 aprint_naive("\n");
799 aprint_normal("\n");
800 devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
801 aprint_normal_dev(self, "%s\n", devinfop);
802 usbd_devinfo_free(devinfop);
803
804 un->un_dev = self;
805 un->un_udev = dev;
806 un->un_sc = sc;
807 un->un_ops = &aue_ops;
808 un->un_intr = &sc->aue_intr;
809 un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
810 un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
811 un->un_rx_list_cnt = AUE_RX_LIST_CNT;
812 un->un_tx_list_cnt = AUE_RX_LIST_CNT;
813 un->un_rx_bufsz = AUE_BUFSZ;
814 un->un_tx_bufsz = AUE_BUFSZ;
815
816 sc->aue_intr.uni_buf = &sc->aue_ibuf;
817 sc->aue_intr.uni_bufsz = sizeof(sc->aue_ibuf);
818 sc->aue_intr.uni_interval = AUE_INTR_INTERVAL;
819
820 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
821 if (err) {
822 aprint_error_dev(self, "failed to set configuration"
823 ", err=%s\n", usbd_errstr(err));
824 return;
825 }
826
827 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &un->un_iface);
828 if (err) {
829 aprint_error_dev(self, "getting interface handle failed\n");
830 return;
831 }
832
833 un->un_flags = aue_lookup(uaa->uaa_vendor, uaa->uaa_product)->aue_flags;
834
835 id = usbd_get_interface_descriptor(un->un_iface);
836
837 /* Find endpoints. */
838 for (i = 0; i < id->bNumEndpoints; i++) {
839 ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
840 if (ed == NULL) {
841 aprint_error_dev(self,
842 "couldn't get endpoint descriptor %d\n", i);
843 return;
844 }
845 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
846 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
847 un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
848 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
849 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
850 un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
851 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
852 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
853 un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
854 }
855 }
856
857 if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
858 un->un_ed[USBNET_ENDPT_TX] == 0 ||
859 un->un_ed[USBNET_ENDPT_INTR] == 0) {
860 aprint_error_dev(self, "missing endpoint\n");
861 return;
862 }
863
864 /* First level attach. */
865 usbnet_attach(un, "auedet");
866
867 usbnet_lock_mii(un);
868
869 /* Reset the adapter and get station address from the EEPROM. */
870 aue_reset(sc);
871 aue_read_mac(un);
872
873 usbnet_unlock_mii(un);
874
875 usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
876 0, &unm);
877 }
878
879 static void
880 aue_intr(struct usbnet *un, usbd_status status)
881 {
882 struct ifnet *ifp = usbnet_ifp(un);
883 struct aue_softc *sc = usbnet_softc(un);
884 struct aue_intrpkt *p = &sc->aue_ibuf;
885
886 AUEHIST_FUNC();
887 AUEHIST_CALLARGSN(20, "aue%jd: enter txstat0 %#jx\n",
888 device_unit(un->un_dev), p->aue_txstat0, 0, 0);
889
890 if (p->aue_txstat0)
891 if_statinc(ifp, if_oerrors);
892
893 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
894 if_statinc(ifp, if_collisions);
895 }
896
897 static void
898 aue_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
899 {
900 struct ifnet *ifp = usbnet_ifp(un);
901 uint8_t *buf = c->unc_buf;
902 struct aue_rxpkt r;
903 uint32_t pktlen;
904
905 AUEHIST_FUNC();
906 AUEHIST_CALLARGSN(10, "aue%jd: enter len %ju",
907 device_unit(un->un_dev), total_len, 0, 0);
908
909 usbnet_isowned_rx(un);
910
911 if (total_len <= 4 + ETHER_CRC_LEN) {
912 if_statinc(ifp, if_ierrors);
913 return;
914 }
915
916 memcpy(&r, buf + total_len - 4, sizeof(r));
917
918 /* Turn off all the non-error bits in the rx status word. */
919 r.aue_rxstat &= AUE_RXSTAT_MASK;
920 if (r.aue_rxstat) {
921 if_statinc(ifp, if_ierrors);
922 return;
923 }
924
925 /* No errors; receive the packet. */
926 pktlen = total_len - ETHER_CRC_LEN - 4;
927
928 usbnet_enqueue(un, buf, pktlen, 0, 0, 0);
929 }
930
931 static unsigned
932 aue_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
933 {
934 uint8_t *buf = c->unc_buf;
935 int total_len;
936
937 AUEHIST_FUNC();
938 AUEHIST_CALLARGSN(10, "aue%jd: enter pktlen=%jd",
939 device_unit(un->un_dev), m->m_pkthdr.len, 0, 0);
940
941 usbnet_isowned_tx(un);
942
943 if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - 2)
944 return 0;
945
946 /*
947 * Copy the mbuf data into a contiguous buffer, leaving two
948 * bytes at the beginning to hold the frame length.
949 */
950 m_copydata(m, 0, m->m_pkthdr.len, buf + 2);
951
952 /*
953 * The ADMtek documentation says that the packet length is
954 * supposed to be specified in the first two bytes of the
955 * transfer, however it actually seems to ignore this info
956 * and base the frame size on the bulk transfer length.
957 */
958 buf[0] = (uint8_t)m->m_pkthdr.len;
959 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
960 total_len = m->m_pkthdr.len + 2;
961
962 DPRINTFN(5, "aue%d: send %d bytes",
963 device_unit(un->un_dev), total_len, 0, 0);
964
965 return total_len;
966 }
967
968 static int
969 aue_init_locked(struct ifnet *ifp)
970 {
971 struct usbnet * const un = ifp->if_softc;
972 struct aue_softc *sc = usbnet_softc(un);
973 int i, rv;
974 const u_char *eaddr;
975
976 AUEHIST_FUNC();
977 AUEHIST_CALLARGSN(5, "aue%jd: enter link=%jd",
978 device_unit(un->un_dev), usbnet_havelink(un), 0, 0);
979
980 if (usbnet_isdying(un))
981 return EIO;
982
983 /* Cancel pending I/O */
984 if (ifp->if_flags & IFF_RUNNING)
985 return 0;
986
987 usbnet_lock_mii_un_locked(un);
988
989 /* Reset the interface. */
990 aue_reset(sc);
991
992 eaddr = CLLADDR(ifp->if_sadl);
993 for (i = 0; i < ETHER_ADDR_LEN; i++)
994 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
995
996 /* If we want promiscuous mode, set the allframes bit. */
997 if (ifp->if_flags & IFF_PROMISC)
998 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
999 else
1000 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1001
1002 usbnet_unlock_mii_un_locked(un);
1003 rv = usbnet_init_rx_tx(un);
1004 usbnet_lock_mii_un_locked(un);
1005
1006 /* Load the multicast filter. */
1007 aue_setiff_locked(un);
1008
1009 /* Enable RX and TX */
1010 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1011 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1012 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1013
1014 usbnet_unlock_mii_un_locked(un);
1015
1016 //mii_mediachg(mii);
1017
1018 return rv;
1019 }
1020
1021 static int
1022 aue_init(struct ifnet *ifp)
1023 {
1024 struct usbnet * const un = ifp->if_softc;
1025 int rv;
1026
1027 usbnet_lock(un);
1028 rv = aue_init_locked(ifp);
1029 usbnet_unlock(un);
1030
1031 return rv;
1032 }
1033
1034 static int
1035 aue_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data)
1036 {
1037 struct usbnet * const un = ifp->if_softc;
1038
1039 AUEHIST_FUNC();
1040 AUEHIST_CALLARGSN(5, "aue%jd: enter cmd %#jx data %#jx",
1041 device_unit(un->un_dev), cmd, (uintptr_t)data, 0);
1042
1043 switch (cmd) {
1044 case SIOCADDMULTI:
1045 case SIOCDELMULTI:
1046 aue_init(ifp);
1047 aue_setiff(un);
1048 break;
1049 default:
1050 break;
1051 }
1052
1053 return 0;
1054 }
1055
1056 static void
1057 aue_stop_cb(struct ifnet *ifp, int disable)
1058 {
1059 struct usbnet * const un = ifp->if_softc;
1060 struct aue_softc * const sc = usbnet_softc(un);
1061
1062 AUEHIST_FUNC();
1063 AUEHIST_CALLARGSN(5, "aue%jd: enter", device_unit(un->un_dev), 0, 0, 0);
1064
1065 usbnet_lock_mii_un_locked(un);
1066 aue_csr_write_1(sc, AUE_CTL0, 0);
1067 aue_csr_write_1(sc, AUE_CTL1, 0);
1068 aue_reset(sc);
1069 usbnet_unlock_mii_un_locked(un);
1070 }
1071
1072 #ifdef _MODULE
1073 #include "ioconf.c"
1074 #endif
1075
1076 USBNET_MODULE(aue)
1077