if_url.c revision 1.83 1 /* $NetBSD: if_url.c,v 1.83 2022/03/03 05:51:27 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2001, 2002
5 * Shingo WATANABE <nabe (at) nabechan.org>. 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. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33 /*
34 * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at
35 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf
36 * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf
37 */
38
39 /*
40 * TODO:
41 * Interrupt Endpoint support
42 * External PHYs
43 * powerhook() support?
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.83 2022/03/03 05:51:27 riastradh Exp $");
48
49 #ifdef _KERNEL_OPT
50 #include "opt_inet.h"
51 #include "opt_usb.h"
52 #endif
53
54 #include <sys/param.h>
55
56 #include <net/if_ether.h>
57 #ifdef INET
58 #include <netinet/in.h>
59 #include <netinet/if_inarp.h>
60 #endif
61
62 #include <dev/mii/urlphyreg.h>
63
64 #include <dev/usb/usbnet.h>
65
66 #include <dev/usb/if_urlreg.h>
67
68 /* Function declarations */
69 static int url_match(device_t, cfdata_t, void *);
70 static void url_attach(device_t, device_t, void *);
71
72 CFATTACH_DECL_NEW(url, sizeof(struct usbnet), url_match, url_attach,
73 usbnet_detach, usbnet_activate);
74
75 static unsigned url_uno_tx_prepare(struct usbnet *, struct mbuf *,
76 struct usbnet_chain *);
77 static void url_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
78 static int url_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *);
79 static int url_uno_mii_write_reg(struct usbnet *, int, int, uint16_t);
80 static void url_uno_mcast(struct ifnet *);
81 static void url_uno_stop(struct ifnet *, int);
82 static void url_uno_mii_statchg(struct ifnet *);
83 static int url_uno_init(struct ifnet *);
84 static void url_rcvfilt_locked(struct usbnet *);
85 static void url_reset(struct usbnet *);
86
87 static int url_csr_read_1(struct usbnet *, int);
88 static int url_csr_read_2(struct usbnet *, int);
89 static int url_csr_write_1(struct usbnet *, int, int);
90 static int url_csr_write_2(struct usbnet *, int, int);
91 static int url_csr_write_4(struct usbnet *, int, int);
92 static int url_mem(struct usbnet *, int, int, void *, int);
93
94 static const struct usbnet_ops url_ops = {
95 .uno_stop = url_uno_stop,
96 .uno_mcast = url_uno_mcast,
97 .uno_read_reg = url_uno_mii_read_reg,
98 .uno_write_reg = url_uno_mii_write_reg,
99 .uno_statchg = url_uno_mii_statchg,
100 .uno_tx_prepare = url_uno_tx_prepare,
101 .uno_rx_loop = url_uno_rx_loop,
102 .uno_init = url_uno_init,
103 };
104
105 /* Macros */
106 #ifdef URL_DEBUG
107 #define DPRINTF(x) if (urldebug) printf x
108 #define DPRINTFN(n, x) if (urldebug >= (n)) printf x
109 int urldebug = 0;
110 #else
111 #define DPRINTF(x)
112 #define DPRINTFN(n, x)
113 #endif
114
115 #define URL_SETBIT(un, reg, x) \
116 url_csr_write_1(un, reg, url_csr_read_1(un, reg) | (x))
117
118 #define URL_SETBIT2(un, reg, x) \
119 url_csr_write_2(un, reg, url_csr_read_2(un, reg) | (x))
120
121 #define URL_CLRBIT(un, reg, x) \
122 url_csr_write_1(un, reg, url_csr_read_1(un, reg) & ~(x))
123
124 #define URL_CLRBIT2(un, reg, x) \
125 url_csr_write_2(un, reg, url_csr_read_2(un, reg) & ~(x))
126
127 static const struct url_type {
128 struct usb_devno url_dev;
129 uint16_t url_flags;
130 #define URL_EXT_PHY 0x0001
131 } url_devs [] = {
132 /* MELCO LUA-KTX */
133 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0},
134 /* Realtek RTL8150L Generic (GREEN HOUSE USBKR100) */
135 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150L}, 0},
136 /* Longshine LCS-8138TX */
137 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0},
138 /* Micronet SP128AR */
139 {{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0},
140 /* OQO model 01 */
141 {{ USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01}, 0},
142 };
143 #define url_lookup(v, p) ((const struct url_type *)usb_lookup(url_devs, v, p))
144
145
146 /* Probe */
147 static int
148 url_match(device_t parent, cfdata_t match, void *aux)
149 {
150 struct usb_attach_arg *uaa = aux;
151
152 return url_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
153 UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
154 }
155 /* Attach */
156 static void
157 url_attach(device_t parent, device_t self, void *aux)
158 {
159 USBNET_MII_DECL_DEFAULT(unm);
160 struct usbnet * const un = device_private(self);
161 struct usb_attach_arg *uaa = aux;
162 struct usbd_device *dev = uaa->uaa_device;
163 struct usbd_interface *iface;
164 usbd_status err;
165 usb_interface_descriptor_t *id;
166 usb_endpoint_descriptor_t *ed;
167 char *devinfop;
168 int i;
169
170 aprint_naive("\n");
171 aprint_normal("\n");
172 devinfop = usbd_devinfo_alloc(dev, 0);
173 aprint_normal_dev(self, "%s\n", devinfop);
174 usbd_devinfo_free(devinfop);
175
176 un->un_dev = self;
177 un->un_udev = dev;
178 un->un_sc = un;
179 un->un_ops = &url_ops;
180 un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
181 un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
182 un->un_rx_list_cnt = URL_RX_LIST_CNT;
183 un->un_tx_list_cnt = URL_TX_LIST_CNT;
184 un->un_rx_bufsz = URL_BUFSZ;
185 un->un_tx_bufsz = URL_BUFSZ;
186
187 /* Move the device into the configured state. */
188 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1);
189 if (err) {
190 aprint_error_dev(self, "failed to set configuration"
191 ", err=%s\n", usbd_errstr(err));
192 return;
193 }
194
195 /* get control interface */
196 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface);
197 if (err) {
198 aprint_error_dev(self, "failed to get interface, err=%s\n",
199 usbd_errstr(err));
200 return;
201 }
202
203 un->un_iface = iface;
204 un->un_flags = url_lookup(uaa->uaa_vendor, uaa->uaa_product)->url_flags;
205 #if 0
206 if (un->un_flags & URL_EXT_PHY) {
207 un->un_read_reg_cb = url_ext_mii_read_reg;
208 un->un_write_reg_cb = url_ext_mii_write_reg;
209 }
210 #endif
211
212 /* get interface descriptor */
213 id = usbd_get_interface_descriptor(un->un_iface);
214
215 /* find endpoints */
216 un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] =
217 un->un_ed[USBNET_ENDPT_INTR] = 0;
218 for (i = 0; i < id->bNumEndpoints; i++) {
219 ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
220 if (ed == NULL) {
221 aprint_error_dev(self,
222 "couldn't get endpoint %d\n", i);
223 return;
224 }
225 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
226 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
227 un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
228 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
229 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
230 un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
231 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
232 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
233 un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
234 }
235
236 if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
237 un->un_ed[USBNET_ENDPT_TX] == 0 ||
238 un->un_ed[USBNET_ENDPT_INTR] == 0) {
239 aprint_error_dev(self, "missing endpoint\n");
240 return;
241 }
242
243 /* Set these up now for url_mem(). */
244 usbnet_attach(un, "urldet");
245
246 usbnet_lock_core(un);
247 usbnet_busy(un);
248
249 /* reset the adapter */
250 url_reset(un);
251
252 /* Get Ethernet Address */
253 err = url_mem(un, URL_CMD_READMEM, URL_IDR0, (void *)un->un_eaddr,
254 ETHER_ADDR_LEN);
255 usbnet_unbusy(un);
256 usbnet_unlock_core(un);
257 if (err) {
258 aprint_error_dev(self, "read MAC address failed\n");
259 return;
260 }
261
262 /* initialize interface information */
263 usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
264 0, &unm);
265 }
266
267 /* read/write memory */
268 static int
269 url_mem(struct usbnet *un, int cmd, int offset, void *buf, int len)
270 {
271 usb_device_request_t req;
272 usbd_status err;
273
274 usbnet_isowned_core(un);
275
276 DPRINTFN(0x200,
277 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
278
279 if (usbnet_isdying(un))
280 return 0;
281
282 if (cmd == URL_CMD_READMEM)
283 req.bmRequestType = UT_READ_VENDOR_DEVICE;
284 else
285 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
286 req.bRequest = URL_REQ_MEM;
287 USETW(req.wValue, offset);
288 USETW(req.wIndex, 0x0000);
289 USETW(req.wLength, len);
290
291 err = usbd_do_request(un->un_udev, &req, buf);
292 if (err) {
293 DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n",
294 device_xname(un->un_dev),
295 cmd == URL_CMD_READMEM ? "read" : "write",
296 offset, err));
297 }
298
299 return err;
300 }
301
302 /* read 1byte from register */
303 static int
304 url_csr_read_1(struct usbnet *un, int reg)
305 {
306 uint8_t val = 0;
307
308 DPRINTFN(0x100,
309 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
310
311 return url_mem(un, URL_CMD_READMEM, reg, &val, 1) ? 0 : val;
312 }
313
314 /* read 2bytes from register */
315 static int
316 url_csr_read_2(struct usbnet *un, int reg)
317 {
318 uWord val;
319
320 DPRINTFN(0x100,
321 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
322
323 USETW(val, 0);
324 return url_mem(un, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val);
325 }
326
327 /* write 1byte to register */
328 static int
329 url_csr_write_1(struct usbnet *un, int reg, int aval)
330 {
331 uint8_t val = aval;
332
333 DPRINTFN(0x100,
334 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
335
336 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0;
337 }
338
339 /* write 2bytes to register */
340 static int
341 url_csr_write_2(struct usbnet *un, int reg, int aval)
342 {
343 uWord val;
344
345 DPRINTFN(0x100,
346 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
347
348 USETW(val, aval);
349
350 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0;
351 }
352
353 /* write 4bytes to register */
354 static int
355 url_csr_write_4(struct usbnet *un, int reg, int aval)
356 {
357 uDWord val;
358
359 DPRINTFN(0x100,
360 ("%s: %s: enter\n", device_xname(un->un_dev), __func__));
361
362 USETDW(val, aval);
363
364 return url_mem(un, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0;
365 }
366
367 static int
368 url_init_locked(struct ifnet *ifp)
369 {
370 struct usbnet * const un = ifp->if_softc;
371 const u_char *eaddr;
372 int i;
373
374 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
375
376 usbnet_isowned_core(un);
377
378 if (usbnet_isdying(un))
379 return EIO;
380
381 /* Cancel pending I/O and free all TX/RX buffers */
382 usbnet_stop(un, ifp, 1);
383
384 eaddr = CLLADDR(ifp->if_sadl);
385 for (i = 0; i < ETHER_ADDR_LEN; i++)
386 url_csr_write_1(un, URL_IDR0 + i, eaddr[i]);
387
388 /* Init transmission control register */
389 URL_CLRBIT(un, URL_TCR,
390 URL_TCR_TXRR1 | URL_TCR_TXRR0 |
391 URL_TCR_IFG1 | URL_TCR_IFG0 |
392 URL_TCR_NOCRC);
393
394 /* Init receive control register */
395 URL_SETBIT2(un, URL_RCR, URL_RCR_TAIL | URL_RCR_AD | URL_RCR_AB);
396
397 /* Accept multicast frame or run promisc. mode */
398 url_rcvfilt_locked(un);
399
400 /* Enable RX and TX */
401 URL_SETBIT(un, URL_CR, URL_CR_TE | URL_CR_RE);
402
403 return usbnet_init_rx_tx(un);
404 }
405
406 static int
407 url_uno_init(struct ifnet *ifp)
408 {
409 int ret = url_init_locked(ifp);
410
411 return ret;
412 }
413
414 static void
415 url_reset(struct usbnet *un)
416 {
417 int i;
418
419 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
420
421 if (usbnet_isdying(un))
422 return;
423
424 URL_SETBIT(un, URL_CR, URL_CR_SOFT_RST);
425
426 for (i = 0; i < URL_TX_TIMEOUT; i++) {
427 if (usbnet_isdying(un))
428 return;
429 if (!(url_csr_read_1(un, URL_CR) & URL_CR_SOFT_RST))
430 break;
431 delay(10); /* XXX */
432 }
433
434 delay(10000); /* XXX */
435 }
436
437 static void
438 url_rcvfilt_locked(struct usbnet *un)
439 {
440 struct ifnet * const ifp = usbnet_ifp(un);
441 struct ethercom *ec = usbnet_ec(un);
442 struct ether_multi *enm;
443 struct ether_multistep step;
444 uint32_t mchash[2] = { 0, 0 };
445 int h = 0, rcr;
446
447 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
448
449 usbnet_isowned_core(un);
450
451 if (usbnet_isdying(un))
452 return;
453
454 rcr = url_csr_read_2(un, URL_RCR);
455 rcr &= ~(URL_RCR_AAP | URL_RCR_AAM | URL_RCR_AM);
456
457 ETHER_LOCK(ec);
458 if (ifp->if_flags & IFF_PROMISC) {
459 ec->ec_flags |= ETHER_F_ALLMULTI;
460 ETHER_UNLOCK(ec);
461 /* run promisc. mode */
462 rcr |= URL_RCR_AAM; /* ??? */
463 rcr |= URL_RCR_AAP;
464 goto update;
465 }
466 ec->ec_flags &= ~ETHER_F_ALLMULTI;
467 ETHER_FIRST_MULTI(step, ec, enm);
468 while (enm != NULL) {
469 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
470 ec->ec_flags |= ETHER_F_ALLMULTI;
471 ETHER_UNLOCK(ec);
472 /* accept all multicast frames */
473 rcr |= URL_RCR_AAM;
474 goto update;
475 }
476 h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
477 /* 1(31) and 5(30:26) bit sampling */
478 mchash[h >> 31] |= 1 << ((h >> 26) & 0x1f);
479 ETHER_NEXT_MULTI(step, enm);
480 }
481 ETHER_UNLOCK(ec);
482 if (h != 0)
483 rcr |= URL_RCR_AM; /* activate mcast hash filter */
484 url_csr_write_4(un, URL_MAR0, mchash[0]);
485 url_csr_write_4(un, URL_MAR4, mchash[1]);
486 update:
487 url_csr_write_2(un, URL_RCR, rcr);
488 }
489
490 static unsigned
491 url_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
492 {
493 int total_len;
494
495 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__));
496
497 KASSERT(un->un_tx_bufsz >= URL_MIN_FRAME_LEN);
498 if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz)
499 return 0;
500
501 /* Copy the mbuf data into a contiguous buffer */
502 m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
503 total_len = m->m_pkthdr.len;
504
505 if (total_len < URL_MIN_FRAME_LEN) {
506 memset(c->unc_buf + total_len, 0,
507 URL_MIN_FRAME_LEN - total_len);
508 total_len = URL_MIN_FRAME_LEN;
509 }
510
511 DPRINTF(("%s: %s: send %d bytes\n", device_xname(un->un_dev),
512 __func__, total_len));
513
514 return total_len;
515 }
516
517 static void
518 url_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
519 {
520 struct ifnet *ifp = usbnet_ifp(un);
521 url_rxhdr_t rxhdr;
522
523 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev),__func__));
524
525 if (total_len <= ETHER_CRC_LEN || total_len <= sizeof(rxhdr)) {
526 if_statinc(ifp, if_ierrors);
527 return;
528 }
529
530 memcpy(&rxhdr, c->unc_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr));
531
532 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n",
533 device_xname(un->un_dev),
534 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK,
535 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "",
536 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "",
537 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "",
538 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : ""));
539
540 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) {
541 if_statinc(ifp, if_ierrors);
542 return;
543 }
544
545 total_len -= ETHER_CRC_LEN;
546
547 DPRINTF(("%s: %s: deliver %d\n", device_xname(un->un_dev),
548 __func__, total_len));
549 usbnet_enqueue(un, c->unc_buf, total_len, 0, 0, 0);
550 }
551
552 #if 0
553 static void url_intr(void)
554 {
555 }
556 #endif
557
558 static void
559 url_uno_mcast(struct ifnet *ifp)
560 {
561 struct usbnet * const un = ifp->if_softc;
562
563 usbnet_lock_core(un);
564
565 url_rcvfilt_locked(un);
566
567 usbnet_unlock_core(un);
568 }
569
570 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
571 static void
572 url_uno_stop(struct ifnet *ifp, int disable)
573 {
574 struct usbnet * const un = ifp->if_softc;
575
576 DPRINTF(("%s: %s: enter\n", device_xname(un->un_dev), __func__));
577
578 url_reset(un);
579 }
580
581 static int
582 url_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
583 {
584 uint16_t data;
585 usbd_status err = USBD_NORMAL_COMPLETION;
586
587 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
588 device_xname(un->un_dev), __func__, phy, reg));
589
590 /* XXX: one PHY only for the RTL8150 internal PHY */
591 if (phy != 0) {
592 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
593 device_xname(un->un_dev), __func__, phy));
594 return EINVAL;
595 }
596
597 switch (reg) {
598 case MII_BMCR: /* Control Register */
599 reg = URL_BMCR;
600 break;
601 case MII_BMSR: /* Status Register */
602 reg = URL_BMSR;
603 break;
604 case MII_PHYIDR1:
605 case MII_PHYIDR2:
606 *val = 0;
607 goto R_DONE;
608 break;
609 case MII_ANAR: /* Autonegotiation advertisement */
610 reg = URL_ANAR;
611 break;
612 case MII_ANLPAR: /* Autonegotiation link partner abilities */
613 reg = URL_ANLP;
614 break;
615 case URLPHY_MSR: /* Media Status Register */
616 reg = URL_MSR;
617 break;
618 default:
619 printf("%s: %s: bad register %04x\n",
620 device_xname(un->un_dev), __func__, reg);
621 return EINVAL;
622 }
623
624 if (reg == URL_MSR)
625 data = url_csr_read_1(un, reg);
626 else
627 data = url_csr_read_2(un, reg);
628 *val = data;
629
630 R_DONE:
631 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04hx\n",
632 device_xname(un->un_dev), __func__, phy, reg, *val));
633
634 return err;
635 }
636
637 static int
638 url_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
639 {
640
641 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x val=0x%04hx\n",
642 device_xname(un->un_dev), __func__, phy, reg, val));
643
644 /* XXX: one PHY only for the RTL8150 internal PHY */
645 if (phy != 0) {
646 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
647 device_xname(un->un_dev), __func__, phy));
648 return EINVAL;
649 }
650
651 switch (reg) {
652 case MII_BMCR: /* Control Register */
653 reg = URL_BMCR;
654 break;
655 case MII_BMSR: /* Status Register */
656 reg = URL_BMSR;
657 break;
658 case MII_PHYIDR1:
659 case MII_PHYIDR2:
660 return 0;
661 case MII_ANAR: /* Autonegotiation advertisement */
662 reg = URL_ANAR;
663 break;
664 case MII_ANLPAR: /* Autonegotiation link partner abilities */
665 reg = URL_ANLP;
666 break;
667 case URLPHY_MSR: /* Media Status Register */
668 reg = URL_MSR;
669 break;
670 default:
671 printf("%s: %s: bad register %04x\n",
672 device_xname(un->un_dev), __func__, reg);
673 return EINVAL;
674 }
675
676 if (reg == URL_MSR)
677 url_csr_write_1(un, reg, val);
678 else
679 url_csr_write_2(un, reg, val);
680
681 return 0;
682 }
683
684 static void
685 url_uno_mii_statchg(struct ifnet *ifp)
686 {
687 struct usbnet * const un = ifp->if_softc;
688
689 DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__));
690
691 /* XXX */
692 usbnet_set_link(un, true);
693 }
694
695 #if 0
696 /*
697 * external PHYs support, but not test.
698 */
699 static usbd_status
700 url_ext_mii_read_reg(struct usbnet *un, int phy, int reg)
701 {
702 uint16_t val;
703
704 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n",
705 device_xname(un->un_dev), __func__, phy, reg));
706
707 url_csr_write_1(un, URL_PHYADD, phy & URL_PHYADD_MASK);
708 /*
709 * RTL8150L will initiate a MII management data transaction
710 * if PHYCNT_OWN bit is set 1 by software. After transaction,
711 * this bit is auto cleared by TRL8150L.
712 */
713 url_csr_write_1(un, URL_PHYCNT,
714 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR);
715 for (i = 0; i < URL_TIMEOUT; i++) {
716 if ((url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0)
717 break;
718 }
719 if (i == URL_TIMEOUT) {
720 printf("%s: MII read timed out\n", device_xname(un->un_dev));
721 }
722
723 val = url_csr_read_2(un, URL_PHYDAT);
724
725 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
726 device_xname(un->un_dev), __func__, phy, reg, val));
727
728 return USBD_NORMAL_COMPLETION;
729 }
730
731 static usbd_status
732 url_ext_mii_write_reg(struct usbnet *un, int phy, int reg, int data)
733 {
734
735 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
736 device_xname(un->un_dev), __func__, phy, reg, data));
737
738 url_csr_write_2(un, URL_PHYDAT, data);
739 url_csr_write_1(un, URL_PHYADD, phy);
740 url_csr_write_1(un, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */
741
742 for (i=0; i < URL_TIMEOUT; i++) {
743 if (url_csr_read_1(un, URL_PHYCNT) & URL_PHYCNT_PHYOWN)
744 break;
745 }
746
747 if (i == URL_TIMEOUT) {
748 printf("%s: MII write timed out\n",
749 device_xname(un->un_dev));
750 return USBD_TIMEOUT;
751 }
752
753 return USBD_NORMAL_COMPLETION;
754 }
755 #endif
756
757 #ifdef _MODULE
758 #include "ioconf.c"
759 #endif
760
761 USBNET_MODULE(url)
762