1 /* $OpenBSD: if_udav.c,v 1.12 2005/07/02 22:21:12 brad Exp $ */
2 /* $NetBSD: if_udav.c,v 1.3 2004/04/23 17:25:25 itojun Exp $ */
3 /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */
4 /*
5 * Copyright (c) 2003
6 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 */
33
34 /*
35 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY)
36 * The spec can be found at the following url.
37 * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf
38 */
39
40 /*
41 * TODO:
42 * Interrupt Endpoint support
43 * External PHYs
44 * powerhook() support?
45 */
46
47 #include <sys/cdefs.h>
48
49 #include "bpfilter.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/lock.h>
54 #include <sys/mbuf.h>
55 #include <sys/kernel.h>
56 #include <sys/proc.h>
57 #include <sys/socket.h>
58
59 #include <sys/device.h>
60 #if NRND > 0
61 #include <sys/rnd.h>
62 #endif
63
64 #include <net/if.h>
65 #include <net/if_arp.h>
66 #include <net/if_dl.h>
67 #include <net/if_media.h>
68
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
73
74 #ifdef INET
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/if_ether.h>
80 #endif
81
82 #include <dev/mii/mii.h>
83 #include <dev/mii/miivar.h>
84
85 #include <dev/usb/usb.h>
86 #include <dev/usb/usbdi.h>
87 #include <dev/usb/usbdi_util.h>
88 #include <dev/usb/usbdevs.h>
89
90 #include <dev/usb/if_udavreg.h>
91
92
93 /* Function declarations */
94 USB_DECLARE_DRIVER_CLASS(udav, DV_IFNET);
95
96 Static int udav_openpipes(struct udav_softc *);
97 Static int udav_rx_list_init(struct udav_softc *);
98 Static int udav_tx_list_init(struct udav_softc *);
99 Static int udav_newbuf(struct udav_softc *, struct udav_chain *, struct mbuf *);
100 Static void udav_start(struct ifnet *);
101 Static int udav_send(struct udav_softc *, struct mbuf *, int);
102 Static void udav_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
103 Static void udav_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
104 Static void udav_tick(void *);
105 Static void udav_tick_task(void *);
106 Static int udav_ioctl(struct ifnet *, u_long, caddr_t);
107 Static void udav_stop_task(struct udav_softc *);
108 Static void udav_stop(struct ifnet *, int);
109 Static void udav_watchdog(struct ifnet *);
110 Static int udav_ifmedia_change(struct ifnet *);
111 Static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *);
112 Static void udav_lock_mii(struct udav_softc *);
113 Static void udav_unlock_mii(struct udav_softc *);
114 Static int udav_miibus_readreg(device_ptr_t, int, int);
115 Static void udav_miibus_writereg(device_ptr_t, int, int, int);
116 Static void udav_miibus_statchg(device_ptr_t);
117 Static int udav_init(struct ifnet *);
118 Static void udav_setmulti(struct udav_softc *);
119 Static void udav_reset(struct udav_softc *);
120
121 Static int udav_csr_read(struct udav_softc *, int, void *, int);
122 Static int udav_csr_write(struct udav_softc *, int, void *, int);
123 Static int udav_csr_read1(struct udav_softc *, int);
124 Static int udav_csr_write1(struct udav_softc *, int, unsigned char);
125
126 #if 0
127 Static int udav_mem_read(struct udav_softc *, int, void *, int);
128 Static int udav_mem_write(struct udav_softc *, int, void *, int);
129 Static int udav_mem_write1(struct udav_softc *, int, unsigned char);
130 #endif
131
132 /* Macros */
133 #ifdef UDAV_DEBUG
134 #define DPRINTF(x) do { if (udavdebug) logprintf x; } while(0)
135 #define DPRINTFN(n,x) do { if (udavdebug >= (n)) logprintf x; } while(0)
136 int udavdebug = 0;
137 #else
138 #define DPRINTF(x)
139 #define DPRINTFN(n,x)
140 #endif
141
142 #define UDAV_SETBIT(sc, reg, x) \
143 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x))
144
145 #define UDAV_CLRBIT(sc, reg, x) \
146 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x))
147
148 static const struct udav_type {
149 struct usb_devno udav_dev;
150 u_int16_t udav_flags;
151 #define UDAV_EXT_PHY 0x0001
152 } udav_devs [] = {
153 /* Corega USB-TXC */
154 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC }, 0},
155 #if 0
156 /* DAVICOM DM9601 Generic? */
157 /* XXX: The following ids was obtained from the data sheet. */
158 {{ 0x0a46, 0x9601 }, 0},
159 #endif
160 };
161 #define udav_lookup(v, p) ((struct udav_type *)usb_lookup(udav_devs, v, p))
162
163
164 /* Probe */
USB_MATCH(udav)165 USB_MATCH(udav)
166 {
167 USB_MATCH_START(udav, uaa);
168
169 if (uaa->iface != NULL)
170 return (UMATCH_NONE);
171
172 return (udav_lookup(uaa->vendor, uaa->product) != NULL ?
173 UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
174 }
175
176 /* Attach */
USB_ATTACH(udav)177 USB_ATTACH(udav)
178 {
179 USB_ATTACH_START(udav, sc, uaa);
180 usbd_device_handle dev = uaa->device;
181 usbd_interface_handle iface;
182 usbd_status err;
183 usb_interface_descriptor_t *id;
184 usb_endpoint_descriptor_t *ed;
185 char devinfo[1024];
186 char *devname = USBDEVNAME(sc->sc_dev);
187 struct ifnet *ifp;
188 struct mii_data *mii;
189 u_char eaddr[ETHER_ADDR_LEN];
190 int i, s;
191
192 usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
193 USB_ATTACH_SETUP;
194 printf("%s: %s", devname, devinfo);
195
196 /* Move the device into the configured state. */
197 err = usbd_set_config_no(dev, UDAV_CONFIG_NO, 1);
198 if (err) {
199 printf(", setting config no failed\n");
200 goto bad;
201 }
202
203 usb_init_task(&sc->sc_tick_task, udav_tick_task, sc);
204 lockinit(&sc->sc_mii_lock, PZERO, "udavmii", 0, 0);
205 usb_init_task(&sc->sc_stop_task, (void (*)(void *)) udav_stop_task, sc);
206
207 /* get control interface */
208 err = usbd_device2interface_handle(dev, UDAV_IFACE_INDEX, &iface);
209 if (err) {
210 printf(", failed to get interface, err=%s\n", usbd_errstr(err));
211 goto bad;
212 }
213
214 sc->sc_udev = dev;
215 sc->sc_ctl_iface = iface;
216 sc->sc_flags = udav_lookup(uaa->vendor, uaa->product)->udav_flags;
217
218 /* get interface descriptor */
219 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
220
221 /* find endpoints */
222 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1;
223 for (i = 0; i < id->bNumEndpoints; i++) {
224 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
225 if (ed == NULL) {
226 printf(", couldn't get endpoint %d\n", i);
227 goto bad;
228 }
229 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
230 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
231 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */
232 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK &&
233 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT)
234 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */
235 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT &&
236 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)
237 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */
238 }
239
240 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 ||
241 sc->sc_intrin_no == -1) {
242 printf(", missing endpoint\n");
243 goto bad;
244 }
245
246 s = splnet();
247
248 /* reset the adapter */
249 udav_reset(sc);
250
251 /* Get Ethernet Address */
252 err = udav_csr_read(sc, UDAV_PAR, (void *)eaddr, ETHER_ADDR_LEN);
253 if (err) {
254 printf(", read MAC address failed\n");
255 splx(s);
256 goto bad;
257 }
258
259 /* Print Ethernet Address */
260 printf(" address %s\n", ether_sprintf(eaddr));
261
262 bcopy(eaddr, (char *)&sc->sc_ac.ac_enaddr, ETHER_ADDR_LEN);
263
264 /* initialize interface infomation */
265 ifp = GET_IFP(sc);
266 ifp->if_softc = sc;
267 strlcpy(ifp->if_xname, devname, IFNAMSIZ);
268 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
269 ifp->if_start = udav_start;
270 ifp->if_ioctl = udav_ioctl;
271 ifp->if_watchdog = udav_watchdog;
272
273 IFQ_SET_READY(&ifp->if_snd);
274
275 /*
276 * Do ifmedia setup.
277 */
278 mii = &sc->sc_mii;
279 mii->mii_ifp = ifp;
280 mii->mii_readreg = udav_miibus_readreg;
281 mii->mii_writereg = udav_miibus_writereg;
282 mii->mii_statchg = udav_miibus_statchg;
283 mii->mii_flags = MIIF_AUTOTSLEEP;
284 ifmedia_init(&mii->mii_media, 0,
285 udav_ifmedia_change, udav_ifmedia_status);
286 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
287 if (LIST_FIRST(&mii->mii_phys) == NULL) {
288 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
289 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
290 } else
291 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
292
293 /* attach the interface */
294 if_attach(ifp);
295 Ether_ifattach(ifp, eaddr);
296
297 #if NRND > 0
298 rnd_attach_source(&sc->rnd_source, devname, RND_TYPE_NET, 0);
299 #endif
300
301 usb_callout_init(sc->sc_stat_ch);
302 sc->sc_attached = 1;
303 splx(s);
304
305 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, USBDEV(sc->sc_dev));
306
307 USB_ATTACH_SUCCESS_RETURN;
308
309 bad:
310 sc->sc_dying = 1;
311 USB_ATTACH_ERROR_RETURN;
312 }
313
314 /* detach */
USB_DETACH(udav)315 USB_DETACH(udav)
316 {
317 USB_DETACH_START(udav, sc);
318 struct ifnet *ifp = GET_IFP(sc);
319 int s;
320
321 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
322
323 /* Detached before attached finished */
324 if (!sc->sc_attached)
325 return (0);
326
327 usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
328
329 /* Remove any pending tasks */
330 usb_rem_task(sc->sc_udev, &sc->sc_tick_task);
331 usb_rem_task(sc->sc_udev, &sc->sc_stop_task);
332
333 s = splusb();
334
335 if (--sc->sc_refcnt >= 0) {
336 /* Wait for processes to go away */
337 usb_detach_wait(USBDEV(sc->sc_dev));
338 }
339 if (ifp->if_flags & IFF_RUNNING)
340 udav_stop(GET_IFP(sc), 1);
341
342 #if NRND > 0
343 rnd_detach_source(&sc->rnd_source);
344 #endif
345 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
346 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
347 ether_ifdetach(ifp);
348 if_detach(ifp);
349
350 #ifdef DIAGNOSTIC
351 if (sc->sc_pipe_tx != NULL)
352 printf("%s: detach has active tx endpoint.\n",
353 USBDEVNAME(sc->sc_dev));
354 if (sc->sc_pipe_rx != NULL)
355 printf("%s: detach has active rx endpoint.\n",
356 USBDEVNAME(sc->sc_dev));
357 if (sc->sc_pipe_intr != NULL)
358 printf("%s: detach has active intr endpoint.\n",
359 USBDEVNAME(sc->sc_dev));
360 #endif
361 sc->sc_attached = 0;
362
363 splx(s);
364
365 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
366 USBDEV(sc->sc_dev));
367
368 return (0);
369 }
370
371 #if 0
372 /* read memory */
373 Static int
374 udav_mem_read(struct udav_softc *sc, int offset, void *buf, int len)
375 {
376 usb_device_request_t req;
377 usbd_status err;
378
379 if (sc == NULL)
380 return (0);
381
382 DPRINTFN(0x200,
383 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
384
385 if (sc->sc_dying)
386 return (0);
387
388 offset &= 0xffff;
389 len &= 0xff;
390
391 req.bmRequestType = UT_READ_VENDOR_DEVICE;
392 req.bRequest = UDAV_REQ_MEM_READ;
393 USETW(req.wValue, 0x0000);
394 USETW(req.wIndex, offset);
395 USETW(req.wLength, len);
396
397 sc->sc_refcnt++;
398 err = usbd_do_request(sc->sc_udev, &req, buf);
399 if (--sc->sc_refcnt < 0)
400 usb_detach_wakeup(USBDEV(sc->sc_dev));
401 if (err) {
402 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
403 USBDEVNAME(sc->sc_dev), __func__, offset, err));
404 }
405
406 return (err);
407 }
408
409 /* write memory */
410 Static int
411 udav_mem_write(struct udav_softc *sc, int offset, void *buf, int len)
412 {
413 usb_device_request_t req;
414 usbd_status err;
415
416 if (sc == NULL)
417 return (0);
418
419 DPRINTFN(0x200,
420 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
421
422 if (sc->sc_dying)
423 return (0);
424
425 offset &= 0xffff;
426 len &= 0xff;
427
428 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
429 req.bRequest = UDAV_REQ_MEM_WRITE;
430 USETW(req.wValue, 0x0000);
431 USETW(req.wIndex, offset);
432 USETW(req.wLength, len);
433
434 sc->sc_refcnt++;
435 err = usbd_do_request(sc->sc_udev, &req, buf);
436 if (--sc->sc_refcnt < 0)
437 usb_detach_wakeup(USBDEV(sc->sc_dev));
438 if (err) {
439 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
440 USBDEVNAME(sc->sc_dev), __func__, offset, err));
441 }
442
443 return (err);
444 }
445
446 /* write memory */
447 Static int
448 udav_mem_write1(struct udav_softc *sc, int offset, unsigned char ch)
449 {
450 usb_device_request_t req;
451 usbd_status err;
452
453 if (sc == NULL)
454 return (0);
455
456 DPRINTFN(0x200,
457 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
458
459 if (sc->sc_dying)
460 return (0);
461
462 offset &= 0xffff;
463
464 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
465 req.bRequest = UDAV_REQ_MEM_WRITE1;
466 USETW(req.wValue, ch);
467 USETW(req.wIndex, offset);
468 USETW(req.wLength, 0x0000);
469
470 sc->sc_refcnt++;
471 err = usbd_do_request(sc->sc_udev, &req, NULL);
472 if (--sc->sc_refcnt < 0)
473 usb_detach_wakeup(USBDEV(sc->sc_dev));
474 if (err) {
475 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
476 USBDEVNAME(sc->sc_dev), __func__, offset, err));
477 }
478
479 return (err);
480 }
481 #endif
482
483 /* read register(s) */
484 Static int
udav_csr_read(struct udav_softc * sc,int offset,void * buf,int len)485 udav_csr_read(struct udav_softc *sc, int offset, void *buf, int len)
486 {
487 usb_device_request_t req;
488 usbd_status err;
489
490 if (sc == NULL)
491 return (0);
492
493 DPRINTFN(0x200,
494 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
495
496 if (sc->sc_dying)
497 return (0);
498
499 offset &= 0xff;
500 len &= 0xff;
501
502 req.bmRequestType = UT_READ_VENDOR_DEVICE;
503 req.bRequest = UDAV_REQ_REG_READ;
504 USETW(req.wValue, 0x0000);
505 USETW(req.wIndex, offset);
506 USETW(req.wLength, len);
507
508 sc->sc_refcnt++;
509 err = usbd_do_request(sc->sc_udev, &req, buf);
510 if (--sc->sc_refcnt < 0)
511 usb_detach_wakeup(USBDEV(sc->sc_dev));
512 if (err) {
513 DPRINTF(("%s: %s: read failed. off=%04x, err=%d\n",
514 USBDEVNAME(sc->sc_dev), __func__, offset, err));
515 }
516
517 return (err);
518 }
519
520 /* write register(s) */
521 Static int
udav_csr_write(struct udav_softc * sc,int offset,void * buf,int len)522 udav_csr_write(struct udav_softc *sc, int offset, void *buf, int len)
523 {
524 usb_device_request_t req;
525 usbd_status err;
526
527 if (sc == NULL)
528 return (0);
529
530 DPRINTFN(0x200,
531 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
532
533 if (sc->sc_dying)
534 return (0);
535
536 offset &= 0xff;
537 len &= 0xff;
538
539 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
540 req.bRequest = UDAV_REQ_REG_WRITE;
541 USETW(req.wValue, 0x0000);
542 USETW(req.wIndex, offset);
543 USETW(req.wLength, len);
544
545 sc->sc_refcnt++;
546 err = usbd_do_request(sc->sc_udev, &req, buf);
547 if (--sc->sc_refcnt < 0)
548 usb_detach_wakeup(USBDEV(sc->sc_dev));
549 if (err) {
550 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
551 USBDEVNAME(sc->sc_dev), __func__, offset, err));
552 }
553
554 return (err);
555 }
556
557 Static int
udav_csr_read1(struct udav_softc * sc,int offset)558 udav_csr_read1(struct udav_softc *sc, int offset)
559 {
560 u_int8_t val = 0;
561
562 if (sc == NULL)
563 return (0);
564
565 DPRINTFN(0x200,
566 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
567
568 if (sc->sc_dying)
569 return (0);
570
571 return (udav_csr_read(sc, offset, &val, 1) ? 0 : val);
572 }
573
574 /* write a register */
575 Static int
udav_csr_write1(struct udav_softc * sc,int offset,unsigned char ch)576 udav_csr_write1(struct udav_softc *sc, int offset, unsigned char ch)
577 {
578 usb_device_request_t req;
579 usbd_status err;
580
581 if (sc == NULL)
582 return (0);
583
584 DPRINTFN(0x200,
585 ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
586
587 if (sc->sc_dying)
588 return (0);
589
590 offset &= 0xff;
591
592 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
593 req.bRequest = UDAV_REQ_REG_WRITE1;
594 USETW(req.wValue, ch);
595 USETW(req.wIndex, offset);
596 USETW(req.wLength, 0x0000);
597
598 sc->sc_refcnt++;
599 err = usbd_do_request(sc->sc_udev, &req, NULL);
600 if (--sc->sc_refcnt < 0)
601 usb_detach_wakeup(USBDEV(sc->sc_dev));
602 if (err) {
603 DPRINTF(("%s: %s: write failed. off=%04x, err=%d\n",
604 USBDEVNAME(sc->sc_dev), __func__, offset, err));
605 }
606
607 return (err);
608 }
609
610 Static int
udav_init(struct ifnet * ifp)611 udav_init(struct ifnet *ifp)
612 {
613 struct udav_softc *sc = ifp->if_softc;
614 struct mii_data *mii = GET_MII(sc);
615 u_char *eaddr;
616 int s;
617
618 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
619
620 if (sc->sc_dying)
621 return (EIO);
622
623 s = splnet();
624
625 /* Cancel pending I/O and free all TX/RX buffers */
626 udav_stop(ifp, 1);
627
628 eaddr = sc->sc_ac.ac_enaddr;
629 udav_csr_write(sc, UDAV_PAR, eaddr, ETHER_ADDR_LEN);
630
631 /* Initialize network control register */
632 /* Disable loopback */
633 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1);
634
635 /* Initialize RX control register */
636 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC);
637
638 /* If we want promiscuous mode, accept all physical frames. */
639 if (ifp->if_flags & IFF_PROMISC)
640 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
641 else
642 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
643
644 /* Initialize transmit ring */
645 if (udav_tx_list_init(sc) == ENOBUFS) {
646 printf("%s: tx list init failed\n", USBDEVNAME(sc->sc_dev));
647 splx(s);
648 return (EIO);
649 }
650
651 /* Initialize receive ring */
652 if (udav_rx_list_init(sc) == ENOBUFS) {
653 printf("%s: rx list init failed\n", USBDEVNAME(sc->sc_dev));
654 splx(s);
655 return (EIO);
656 }
657
658 /* Load the multicast filter */
659 udav_setmulti(sc);
660
661 /* Enable RX */
662 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN);
663
664 /* clear POWER_DOWN state of internal PHY */
665 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0);
666 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0);
667
668 mii_mediachg(mii);
669
670 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) {
671 if (udav_openpipes(sc)) {
672 splx(s);
673 return (EIO);
674 }
675 }
676
677 ifp->if_flags |= IFF_RUNNING;
678 ifp->if_flags &= ~IFF_OACTIVE;
679
680 splx(s);
681
682 usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
683
684 return (0);
685 }
686
687 Static void
udav_reset(struct udav_softc * sc)688 udav_reset(struct udav_softc *sc)
689 {
690 int i;
691
692 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
693
694 if (sc->sc_dying)
695 return;
696
697 /* Select PHY */
698 #if 1
699 /*
700 * XXX: force select internal phy.
701 * external phy routines are not tested.
702 */
703 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
704 #else
705 if (sc->sc_flags & UDAV_EXT_PHY) {
706 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
707 } else {
708 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY);
709 }
710 #endif
711
712 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST);
713
714 for (i = 0; i < UDAV_TX_TIMEOUT; i++) {
715 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST))
716 break;
717 delay(10); /* XXX */
718 }
719 delay(10000); /* XXX */
720 }
721
722 int
udav_activate(device_ptr_t self,enum devact act)723 udav_activate(device_ptr_t self, enum devact act)
724 {
725 struct udav_softc *sc = (struct udav_softc *)self;
726
727 DPRINTF(("%s: %s: enter, act=%d\n", USBDEVNAME(sc->sc_dev),
728 __func__, act));
729 switch (act) {
730 case DVACT_ACTIVATE:
731 return (EOPNOTSUPP);
732 break;
733
734 case DVACT_DEACTIVATE:
735 if_deactivate(&sc->sc_ec.ec_if);
736 sc->sc_dying = 1;
737 break;
738 }
739 return (0);
740 }
741
742 #define UDAV_BITS 6
743
744 #define UDAV_CALCHASH(addr) \
745 (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((1 << UDAV_BITS) - 1))
746
747 Static void
udav_setmulti(struct udav_softc * sc)748 udav_setmulti(struct udav_softc *sc)
749 {
750 struct ifnet *ifp;
751 struct ether_multi *enm;
752 struct ether_multistep step;
753 u_int8_t hashes[8];
754 int h = 0;
755
756 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
757
758 if (sc->sc_dying)
759 return;
760
761 ifp = GET_IFP(sc);
762
763 if (ifp->if_flags & IFF_PROMISC) {
764 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC);
765 return;
766 } else if (ifp->if_flags & IFF_ALLMULTI) {
767 allmulti:
768 ifp->if_flags |= IFF_ALLMULTI;
769 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
770 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_PRMSC);
771 return;
772 }
773
774 /* first, zot all the existing hash bits */
775 memset(hashes, 0x00, sizeof(hashes));
776 hashes[7] |= 0x80; /* broadcast address */
777 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
778
779 /* now program new ones */
780 ETHER_FIRST_MULTI(step, &sc->sc_ac, enm);
781 while (enm != NULL) {
782 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
783 ETHER_ADDR_LEN) != 0)
784 goto allmulti;
785
786 h = UDAV_CALCHASH(enm->enm_addrlo);
787 hashes[h>>3] |= 1 << (h & 0x7);
788 ETHER_NEXT_MULTI(step, enm);
789 }
790
791 /* disable all multicast */
792 ifp->if_flags &= ~IFF_ALLMULTI;
793 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL);
794
795 /* write hash value to the register */
796 udav_csr_write(sc, UDAV_MAR, hashes, sizeof(hashes));
797 }
798
799 Static int
udav_openpipes(struct udav_softc * sc)800 udav_openpipes(struct udav_softc *sc)
801 {
802 struct udav_chain *c;
803 usbd_status err;
804 int i;
805 int error = 0;
806
807 if (sc->sc_dying)
808 return (EIO);
809
810 sc->sc_refcnt++;
811
812 /* Open RX pipe */
813 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no,
814 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx);
815 if (err) {
816 printf("%s: open rx pipe failed: %s\n",
817 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
818 error = EIO;
819 goto done;
820 }
821
822 /* Open TX pipe */
823 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no,
824 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx);
825 if (err) {
826 printf("%s: open tx pipe failed: %s\n",
827 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
828 error = EIO;
829 goto done;
830 }
831
832 #if 0
833 /* XXX: interrupt endpoint is not yet supported */
834 /* Open Interrupt pipe */
835 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no,
836 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc,
837 &sc->sc_cdata.udav_ibuf, UDAV_INTR_PKGLEN,
838 udav_intr, UDAV_INTR_INTERVAL);
839 if (err) {
840 printf("%s: open intr pipe failed: %s\n",
841 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
842 error = EIO;
843 goto done;
844 }
845 #endif
846
847
848 /* Start up the receive pipe. */
849 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
850 c = &sc->sc_cdata.udav_rx_chain[i];
851 usbd_setup_xfer(c->udav_xfer, sc->sc_pipe_rx,
852 c, c->udav_buf, UDAV_BUFSZ,
853 USBD_SHORT_XFER_OK | USBD_NO_COPY,
854 USBD_NO_TIMEOUT, udav_rxeof);
855 (void)usbd_transfer(c->udav_xfer);
856 DPRINTF(("%s: %s: start read\n", USBDEVNAME(sc->sc_dev),
857 __func__));
858 }
859
860 done:
861 if (--sc->sc_refcnt < 0)
862 usb_detach_wakeup(USBDEV(sc->sc_dev));
863
864 return (error);
865 }
866
867 Static int
udav_newbuf(struct udav_softc * sc,struct udav_chain * c,struct mbuf * m)868 udav_newbuf(struct udav_softc *sc, struct udav_chain *c, struct mbuf *m)
869 {
870 struct mbuf *m_new = NULL;
871
872 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
873
874 if (m == NULL) {
875 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
876 if (m_new == NULL) {
877 printf("%s: no memory for rx list "
878 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
879 return (ENOBUFS);
880 }
881 MCLGET(m_new, M_DONTWAIT);
882 if (!(m_new->m_flags & M_EXT)) {
883 printf("%s: no memory for rx list "
884 "-- packet dropped!\n", USBDEVNAME(sc->sc_dev));
885 m_freem(m_new);
886 return (ENOBUFS);
887 }
888 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
889 } else {
890 m_new = m;
891 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
892 m_new->m_data = m_new->m_ext.ext_buf;
893 }
894
895 m_adj(m_new, ETHER_ALIGN);
896 c->udav_mbuf = m_new;
897
898 return (0);
899 }
900
901
902 Static int
udav_rx_list_init(struct udav_softc * sc)903 udav_rx_list_init(struct udav_softc *sc)
904 {
905 struct udav_cdata *cd;
906 struct udav_chain *c;
907 int i;
908
909 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
910
911 cd = &sc->sc_cdata;
912 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
913 c = &cd->udav_rx_chain[i];
914 c->udav_sc = sc;
915 c->udav_idx = i;
916 if (udav_newbuf(sc, c, NULL) == ENOBUFS)
917 return (ENOBUFS);
918 if (c->udav_xfer == NULL) {
919 c->udav_xfer = usbd_alloc_xfer(sc->sc_udev);
920 if (c->udav_xfer == NULL)
921 return (ENOBUFS);
922 c->udav_buf = usbd_alloc_buffer(c->udav_xfer, UDAV_BUFSZ);
923 if (c->udav_buf == NULL) {
924 usbd_free_xfer(c->udav_xfer);
925 return (ENOBUFS);
926 }
927 }
928 }
929
930 return (0);
931 }
932
933 Static int
udav_tx_list_init(struct udav_softc * sc)934 udav_tx_list_init(struct udav_softc *sc)
935 {
936 struct udav_cdata *cd;
937 struct udav_chain *c;
938 int i;
939
940 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
941
942 cd = &sc->sc_cdata;
943 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
944 c = &cd->udav_tx_chain[i];
945 c->udav_sc = sc;
946 c->udav_idx = i;
947 c->udav_mbuf = NULL;
948 if (c->udav_xfer == NULL) {
949 c->udav_xfer = usbd_alloc_xfer(sc->sc_udev);
950 if (c->udav_xfer == NULL)
951 return (ENOBUFS);
952 c->udav_buf = usbd_alloc_buffer(c->udav_xfer, UDAV_BUFSZ);
953 if (c->udav_buf == NULL) {
954 usbd_free_xfer(c->udav_xfer);
955 return (ENOBUFS);
956 }
957 }
958 }
959
960 return (0);
961 }
962
963 Static void
udav_start(struct ifnet * ifp)964 udav_start(struct ifnet *ifp)
965 {
966 struct udav_softc *sc = ifp->if_softc;
967 struct mbuf *m_head = NULL;
968
969 DPRINTF(("%s: %s: enter, link=%d\n", USBDEVNAME(sc->sc_dev),
970 __func__, sc->sc_link));
971
972 if (sc->sc_dying)
973 return;
974
975 if (!sc->sc_link)
976 return;
977
978 if (ifp->if_flags & IFF_OACTIVE)
979 return;
980
981 IFQ_POLL(&ifp->if_snd, m_head);
982 if (m_head == NULL)
983 return;
984
985 if (udav_send(sc, m_head, 0)) {
986 ifp->if_flags |= IFF_OACTIVE;
987 return;
988 }
989
990 IFQ_DEQUEUE(&ifp->if_snd, m_head);
991
992 #if NBPFILTER > 0
993 if (ifp->if_bpf)
994 bpf_mtap(ifp->if_bpf, m_head);
995 #endif
996
997 ifp->if_flags |= IFF_OACTIVE;
998
999 /* Set a timeout in case the chip goes out to lunch. */
1000 ifp->if_timer = 5;
1001 }
1002
1003 Static int
udav_send(struct udav_softc * sc,struct mbuf * m,int idx)1004 udav_send(struct udav_softc *sc, struct mbuf *m, int idx)
1005 {
1006 int total_len;
1007 struct udav_chain *c;
1008 usbd_status err;
1009
1010 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
1011
1012 c = &sc->sc_cdata.udav_tx_chain[idx];
1013
1014 /* Copy the mbuf data into a contiguous buffer */
1015 /* first 2 bytes are packet length */
1016 m_copydata(m, 0, m->m_pkthdr.len, c->udav_buf + 2);
1017 c->udav_mbuf = m;
1018 total_len = m->m_pkthdr.len;
1019 if (total_len < UDAV_MIN_FRAME_LEN) {
1020 memset(c->udav_buf + 2 + total_len, 0,
1021 UDAV_MIN_FRAME_LEN - total_len);
1022 total_len = UDAV_MIN_FRAME_LEN;
1023 }
1024
1025 /* Frame length is specified in the first 2bytes of the buffer */
1026 c->udav_buf[0] = (u_int8_t)total_len;
1027 c->udav_buf[1] = (u_int8_t)(total_len >> 8);
1028 total_len += 2;
1029
1030 usbd_setup_xfer(c->udav_xfer, sc->sc_pipe_tx, c, c->udav_buf, total_len,
1031 USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1032 UDAV_TX_TIMEOUT, udav_txeof);
1033
1034 /* Transmit */
1035 sc->sc_refcnt++;
1036 err = usbd_transfer(c->udav_xfer);
1037 if (--sc->sc_refcnt < 0)
1038 usb_detach_wakeup(USBDEV(sc->sc_dev));
1039 if (err != USBD_IN_PROGRESS) {
1040 printf("%s: udav_send error=%s\n", USBDEVNAME(sc->sc_dev),
1041 usbd_errstr(err));
1042 /* Stop the interface */
1043 usb_add_task(sc->sc_udev, &sc->sc_stop_task);
1044 return (EIO);
1045 }
1046
1047 DPRINTF(("%s: %s: send %d bytes\n", USBDEVNAME(sc->sc_dev),
1048 __func__, total_len));
1049
1050 sc->sc_cdata.udav_tx_cnt++;
1051
1052 return (0);
1053 }
1054
1055 Static void
udav_txeof(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)1056 udav_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1057 {
1058 struct udav_chain *c = priv;
1059 struct udav_softc *sc = c->udav_sc;
1060 struct ifnet *ifp = GET_IFP(sc);
1061 int s;
1062
1063 if (sc->sc_dying)
1064 return;
1065
1066 s = splnet();
1067
1068 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1069
1070 ifp->if_timer = 0;
1071 ifp->if_flags &= ~IFF_OACTIVE;
1072
1073 if (status != USBD_NORMAL_COMPLETION) {
1074 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1075 splx(s);
1076 return;
1077 }
1078 ifp->if_oerrors++;
1079 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev),
1080 usbd_errstr(status));
1081 if (status == USBD_STALLED) {
1082 sc->sc_refcnt++;
1083 usbd_clear_endpoint_stall(sc->sc_pipe_tx);
1084 if (--sc->sc_refcnt < 0)
1085 usb_detach_wakeup(USBDEV(sc->sc_dev));
1086 }
1087 splx(s);
1088 return;
1089 }
1090
1091 ifp->if_opackets++;
1092
1093 m_freem(c->udav_mbuf);
1094 c->udav_mbuf = NULL;
1095
1096 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1097 udav_start(ifp);
1098
1099 splx(s);
1100 }
1101
1102 Static void
udav_rxeof(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)1103 udav_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1104 {
1105 struct udav_chain *c = priv;
1106 struct udav_softc *sc = c->udav_sc;
1107 struct ifnet *ifp = GET_IFP(sc);
1108 struct udav_rx_hdr *h;
1109 struct mbuf *m;
1110 u_int32_t total_len;
1111 int s;
1112
1113 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__));
1114
1115 if (sc->sc_dying)
1116 return;
1117
1118 if (status != USBD_NORMAL_COMPLETION) {
1119 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1120 return;
1121 sc->sc_rx_errs++;
1122 if (usbd_ratecheck(&sc->sc_rx_notice)) {
1123 printf("%s: %u usb errors on rx: %s\n",
1124 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs,
1125 usbd_errstr(status));
1126 sc->sc_rx_errs = 0;
1127 }
1128 if (status == USBD_STALLED) {
1129 sc->sc_refcnt++;
1130 usbd_clear_endpoint_stall(sc->sc_pipe_rx);
1131 if (--sc->sc_refcnt < 0)
1132 usb_detach_wakeup(USBDEV(sc->sc_dev));
1133 }
1134 goto done;
1135 }
1136
1137 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1138
1139 h = (struct udav_rx_hdr *)c->udav_buf;
1140 total_len = UGETW(h->length) - ETHER_CRC_LEN;
1141
1142 DPRINTF(("%s: RX Status: 0x%02x\n", h->pktstat));
1143
1144 if (h->pktstat & UDAV_RSR_LCS) {
1145 ifp->if_collisions++;
1146 goto done;
1147 }
1148
1149 if (total_len < sizeof(struct ether_header) ||
1150 h->pktstat & UDAV_RSR_ERR) {
1151 ifp->if_ierrors++;
1152 goto done;
1153 }
1154
1155 /* copy data to mbuf */
1156 m = c->udav_mbuf;
1157 memcpy(mtod(m, char *), c->udav_buf + UDAV_RX_HDRLEN, total_len);
1158
1159 ifp->if_ipackets++;
1160
1161 m->m_pkthdr.len = m->m_len = total_len;
1162 m->m_pkthdr.rcvif = ifp;
1163
1164 s = splnet();
1165
1166 if (udav_newbuf(sc, c, NULL) == ENOBUFS) {
1167 ifp->if_ierrors++;
1168 goto done1;
1169 }
1170
1171 #if NBPFILTER > 0
1172 if (ifp->if_bpf)
1173 BPF_MTAP(ifp, m);
1174 #endif
1175
1176 DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev),
1177 __func__, m->m_len));
1178 IF_INPUT(ifp, m);
1179
1180 done1:
1181 splx(s);
1182
1183 done:
1184 /* Setup new transfer */
1185 usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->udav_buf, UDAV_BUFSZ,
1186 USBD_SHORT_XFER_OK | USBD_NO_COPY,
1187 USBD_NO_TIMEOUT, udav_rxeof);
1188 sc->sc_refcnt++;
1189 usbd_transfer(xfer);
1190 if (--sc->sc_refcnt < 0)
1191 usb_detach_wakeup(USBDEV(sc->sc_dev));
1192
1193 DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __func__));
1194 }
1195
1196 #if 0
1197 Static void udav_intr()
1198 {
1199 }
1200 #endif
1201
1202 Static int
udav_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)1203 udav_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1204 {
1205 struct udav_softc *sc = ifp->if_softc;
1206 struct ifaddr *ifa = (struct ifaddr *)data;
1207 struct ifreq *ifr = (struct ifreq *)data;
1208 struct mii_data *mii;
1209 int s, error = 0;
1210
1211 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1212
1213 if (sc->sc_dying)
1214 return (EIO);
1215
1216 s = splnet();
1217
1218 switch (cmd) {
1219 case SIOCGIFMEDIA:
1220 case SIOCSIFMEDIA:
1221 mii = GET_MII(sc);
1222 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1223 break;
1224 case SIOCSIFADDR:
1225 ifp->if_flags |= IFF_UP;
1226 udav_init(ifp);
1227
1228 switch (ifa->ifa_addr->sa_family) {
1229 #ifdef INET
1230 case AF_INET:
1231 arp_ifinit(&sc->sc_ac, ifa);
1232 break;
1233 #endif /* INET */
1234 }
1235 break;
1236
1237 case SIOCSIFMTU:
1238 if (ifr->ifr_mtu > ETHERMTU)
1239 error = EINVAL;
1240 else
1241 ifp->if_mtu = ifr->ifr_mtu;
1242 break;
1243 case SIOCSIFFLAGS:
1244 if (ifp->if_flags & IFF_UP) {
1245 if (ifp->if_flags & IFF_RUNNING &&
1246 ifp->if_flags & IFF_PROMISC) {
1247 UDAV_SETBIT(sc, UDAV_RCR,
1248 UDAV_RCR_ALL|UDAV_RCR_PRMSC);
1249 } else if (ifp->if_flags & IFF_RUNNING &&
1250 !(ifp->if_flags & IFF_PROMISC)) {
1251 UDAV_CLRBIT(sc, UDAV_RCR,
1252 UDAV_RCR_PRMSC);
1253 } else if (!(ifp->if_flags & IFF_RUNNING))
1254 udav_init(ifp);
1255 } else {
1256 if (ifp->if_flags & IFF_RUNNING)
1257 udav_stop(ifp, 1);
1258 }
1259 error = 0;
1260 break;
1261 case SIOCADDMULTI:
1262 case SIOCDELMULTI:
1263 error = (cmd == SIOCADDMULTI) ?
1264 ether_addmulti(ifr, &sc->sc_ac) :
1265 ether_delmulti(ifr, &sc->sc_ac);
1266
1267 if (error == ENETRESET) {
1268 if (ifp->if_flags & IFF_RUNNING)
1269 udav_setmulti(sc);
1270 error = 0;
1271 }
1272 break;
1273 default:
1274 error = EINVAL;
1275 break;
1276 }
1277
1278 splx(s);
1279
1280 return (error);
1281 }
1282
1283 Static void
udav_watchdog(struct ifnet * ifp)1284 udav_watchdog(struct ifnet *ifp)
1285 {
1286 struct udav_softc *sc = ifp->if_softc;
1287 struct udav_chain *c;
1288 usbd_status stat;
1289 int s;
1290
1291 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1292
1293 ifp->if_oerrors++;
1294 printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev));
1295
1296 s = splusb();
1297 c = &sc->sc_cdata.udav_tx_chain[0];
1298 usbd_get_xfer_status(c->udav_xfer, NULL, NULL, NULL, &stat);
1299 udav_txeof(c->udav_xfer, c, stat);
1300
1301 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1302 udav_start(ifp);
1303 splx(s);
1304 }
1305
1306 Static void
udav_stop_task(struct udav_softc * sc)1307 udav_stop_task(struct udav_softc *sc)
1308 {
1309 udav_stop(GET_IFP(sc), 1);
1310 }
1311
1312 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */
1313 Static void
udav_stop(struct ifnet * ifp,int disable)1314 udav_stop(struct ifnet *ifp, int disable)
1315 {
1316 struct udav_softc *sc = ifp->if_softc;
1317 usbd_status err;
1318 int i;
1319
1320 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1321
1322 ifp->if_timer = 0;
1323 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1324
1325 udav_reset(sc);
1326
1327 usb_uncallout(sc->sc_stat_ch, udav_tick, sc);
1328
1329 /* Stop transfers */
1330 /* RX endpoint */
1331 if (sc->sc_pipe_rx != NULL) {
1332 err = usbd_abort_pipe(sc->sc_pipe_rx);
1333 if (err)
1334 printf("%s: abort rx pipe failed: %s\n",
1335 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1336 err = usbd_close_pipe(sc->sc_pipe_rx);
1337 if (err)
1338 printf("%s: close rx pipe failed: %s\n",
1339 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1340 sc->sc_pipe_rx = NULL;
1341 }
1342
1343 /* TX endpoint */
1344 if (sc->sc_pipe_tx != NULL) {
1345 err = usbd_abort_pipe(sc->sc_pipe_tx);
1346 if (err)
1347 printf("%s: abort tx pipe failed: %s\n",
1348 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1349 err = usbd_close_pipe(sc->sc_pipe_tx);
1350 if (err)
1351 printf("%s: close tx pipe failed: %s\n",
1352 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1353 sc->sc_pipe_tx = NULL;
1354 }
1355
1356 #if 0
1357 /* XXX: Interrupt endpoint is not yet supported!! */
1358 /* Interrupt endpoint */
1359 if (sc->sc_pipe_intr != NULL) {
1360 err = usbd_abort_pipe(sc->sc_pipe_intr);
1361 if (err)
1362 printf("%s: abort intr pipe failed: %s\n",
1363 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1364 err = usbd_close_pipe(sc->sc_pipe_intr);
1365 if (err)
1366 printf("%s: close intr pipe failed: %s\n",
1367 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
1368 sc->sc_pipe_intr = NULL;
1369 }
1370 #endif
1371
1372 /* Free RX resources. */
1373 for (i = 0; i < UDAV_RX_LIST_CNT; i++) {
1374 if (sc->sc_cdata.udav_rx_chain[i].udav_mbuf != NULL) {
1375 m_freem(sc->sc_cdata.udav_rx_chain[i].udav_mbuf);
1376 sc->sc_cdata.udav_rx_chain[i].udav_mbuf = NULL;
1377 }
1378 if (sc->sc_cdata.udav_rx_chain[i].udav_xfer != NULL) {
1379 usbd_free_xfer(sc->sc_cdata.udav_rx_chain[i].udav_xfer);
1380 sc->sc_cdata.udav_rx_chain[i].udav_xfer = NULL;
1381 }
1382 }
1383
1384 /* Free TX resources. */
1385 for (i = 0; i < UDAV_TX_LIST_CNT; i++) {
1386 if (sc->sc_cdata.udav_tx_chain[i].udav_mbuf != NULL) {
1387 m_freem(sc->sc_cdata.udav_tx_chain[i].udav_mbuf);
1388 sc->sc_cdata.udav_tx_chain[i].udav_mbuf = NULL;
1389 }
1390 if (sc->sc_cdata.udav_tx_chain[i].udav_xfer != NULL) {
1391 usbd_free_xfer(sc->sc_cdata.udav_tx_chain[i].udav_xfer);
1392 sc->sc_cdata.udav_tx_chain[i].udav_xfer = NULL;
1393 }
1394 }
1395
1396 sc->sc_link = 0;
1397 }
1398
1399 /* Set media options */
1400 Static int
udav_ifmedia_change(struct ifnet * ifp)1401 udav_ifmedia_change(struct ifnet *ifp)
1402 {
1403 struct udav_softc *sc = ifp->if_softc;
1404 struct mii_data *mii = GET_MII(sc);
1405
1406 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1407
1408 if (sc->sc_dying)
1409 return (0);
1410
1411 sc->sc_link = 0;
1412 if (mii->mii_instance) {
1413 struct mii_softc *miisc;
1414 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1415 miisc = LIST_NEXT(miisc, mii_list))
1416 mii_phy_reset(miisc);
1417 }
1418
1419 return (mii_mediachg(mii));
1420 }
1421
1422 /* Report current media status. */
1423 Static void
udav_ifmedia_status(struct ifnet * ifp,struct ifmediareq * ifmr)1424 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
1425 {
1426 struct udav_softc *sc = ifp->if_softc;
1427 struct mii_data *mii = GET_MII(sc);
1428
1429 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1430
1431 if (sc->sc_dying)
1432 return;
1433
1434 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1435 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
1436 ifmr->ifm_status = 0;
1437 return;
1438 }
1439
1440 mii_pollstat(mii);
1441 ifmr->ifm_active = mii->mii_media_active;
1442 ifmr->ifm_status = mii->mii_media_status;
1443 }
1444
1445 Static void
udav_tick(void * xsc)1446 udav_tick(void *xsc)
1447 {
1448 struct udav_softc *sc = xsc;
1449
1450 if (sc == NULL)
1451 return;
1452
1453 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1454 __func__));
1455
1456 if (sc->sc_dying)
1457 return;
1458
1459 /* Perform periodic stuff in process context */
1460 usb_add_task(sc->sc_udev, &sc->sc_tick_task);
1461 }
1462
1463 Static void
udav_tick_task(void * xsc)1464 udav_tick_task(void *xsc)
1465 {
1466 struct udav_softc *sc = xsc;
1467 struct ifnet *ifp;
1468 struct mii_data *mii;
1469 int s;
1470
1471 if (sc == NULL)
1472 return;
1473
1474 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1475 __func__));
1476
1477 if (sc->sc_dying)
1478 return;
1479
1480 ifp = GET_IFP(sc);
1481 mii = GET_MII(sc);
1482
1483 if (mii == NULL)
1484 return;
1485
1486 s = splnet();
1487
1488 mii_tick(mii);
1489 if (!sc->sc_link && mii->mii_media_status & IFM_ACTIVE &&
1490 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1491 DPRINTF(("%s: %s: got link\n",
1492 USBDEVNAME(sc->sc_dev), __func__));
1493 sc->sc_link++;
1494 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1495 udav_start(ifp);
1496 }
1497
1498 usb_callout(sc->sc_stat_ch, hz, udav_tick, sc);
1499
1500 splx(s);
1501 }
1502
1503 /* Get exclusive access to the MII registers */
1504 Static void
udav_lock_mii(struct udav_softc * sc)1505 udav_lock_mii(struct udav_softc *sc)
1506 {
1507 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1508 __func__));
1509
1510 sc->sc_refcnt++;
1511 usb_lockmgr(&sc->sc_mii_lock, LK_EXCLUSIVE, NULL, curproc);
1512 }
1513
1514 Static void
udav_unlock_mii(struct udav_softc * sc)1515 udav_unlock_mii(struct udav_softc *sc)
1516 {
1517 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),
1518 __func__));
1519
1520 usb_lockmgr(&sc->sc_mii_lock, LK_RELEASE, NULL, curproc);
1521 if (--sc->sc_refcnt < 0)
1522 usb_detach_wakeup(USBDEV(sc->sc_dev));
1523 }
1524
1525 Static int
udav_miibus_readreg(device_ptr_t dev,int phy,int reg)1526 udav_miibus_readreg(device_ptr_t dev, int phy, int reg)
1527 {
1528 struct udav_softc *sc;
1529 u_int8_t val[2];
1530 u_int16_t data16;
1531
1532 if (dev == NULL)
1533 return (0);
1534
1535 sc = USBGETSOFTC(dev);
1536
1537 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n",
1538 USBDEVNAME(sc->sc_dev), __func__, phy, reg));
1539
1540 if (sc->sc_dying) {
1541 #ifdef DIAGNOSTIC
1542 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1543 __func__);
1544 #endif
1545 return (0);
1546 }
1547
1548 /* XXX: one PHY only for the internal PHY */
1549 if (phy != 0) {
1550 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1551 USBDEVNAME(sc->sc_dev), __func__, phy));
1552 return (0);
1553 }
1554
1555 udav_lock_mii(sc);
1556
1557 /* select internal PHY and set PHY register address */
1558 udav_csr_write1(sc, UDAV_EPAR,
1559 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1560
1561 /* select PHY operation and start read command */
1562 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR);
1563
1564 /* XXX: should be wait? */
1565
1566 /* end read command */
1567 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR);
1568
1569 /* retrieve the result from data registers */
1570 udav_csr_read(sc, UDAV_EPDRL, val, 2);
1571
1572 udav_unlock_mii(sc);
1573
1574 data16 = val[0] | (val[1] << 8);
1575
1576 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n",
1577 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data16));
1578
1579 return (data16);
1580 }
1581
1582 Static void
udav_miibus_writereg(device_ptr_t dev,int phy,int reg,int data)1583 udav_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
1584 {
1585 struct udav_softc *sc;
1586 u_int8_t val[2];
1587
1588 if (dev == NULL)
1589 return;
1590
1591 sc = USBGETSOFTC(dev);
1592
1593 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n",
1594 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data));
1595
1596 if (sc->sc_dying) {
1597 #ifdef DIAGNOSTIC
1598 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev),
1599 __func__);
1600 #endif
1601 return;
1602 }
1603
1604 /* XXX: one PHY only for the internal PHY */
1605 if (phy != 0) {
1606 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n",
1607 USBDEVNAME(sc->sc_dev), __func__, phy));
1608 return;
1609 }
1610
1611 udav_lock_mii(sc);
1612
1613 /* select internal PHY and set PHY register address */
1614 udav_csr_write1(sc, UDAV_EPAR,
1615 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK));
1616
1617 /* put the value to the data registers */
1618 val[0] = data & 0xff;
1619 val[1] = (data >> 8) & 0xff;
1620 udav_csr_write(sc, UDAV_EPDRL, val, 2);
1621
1622 /* select PHY operation and start write command */
1623 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW);
1624
1625 /* XXX: should be wait? */
1626
1627 /* end write command */
1628 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW);
1629
1630 udav_unlock_mii(sc);
1631
1632 return;
1633 }
1634
1635 Static void
udav_miibus_statchg(device_ptr_t dev)1636 udav_miibus_statchg(device_ptr_t dev)
1637 {
1638 #ifdef UDAV_DEBUG
1639 struct udav_softc *sc;
1640
1641 if (dev == NULL)
1642 return;
1643
1644 sc = USBGETSOFTC(dev);
1645 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__));
1646 #endif
1647 /* Nothing to do */
1648 }
1649