Thu Mar 3 05:56:58 2022 UTC ()
urndis(4): Simplify circuitous initialization logic.


(riastradh)
diff -r1.46 -r1.47 src/sys/dev/usb/if_urndis.c

cvs diff -r1.46 -r1.47 src/sys/dev/usb/if_urndis.c (expand / switch to unified diff)

--- src/sys/dev/usb/if_urndis.c 2022/03/03 05:56:28 1.46
+++ src/sys/dev/usb/if_urndis.c 2022/03/03 05:56:58 1.47
@@ -1,37 +1,37 @@ @@ -1,37 +1,37 @@
1/* $NetBSD: if_urndis.c,v 1.46 2022/03/03 05:56:28 riastradh Exp $ */ 1/* $NetBSD: if_urndis.c,v 1.47 2022/03/03 05:56:58 riastradh Exp $ */
2/* $OpenBSD: if_urndis.c,v 1.31 2011/07/03 15:47:17 matthew Exp $ */ 2/* $OpenBSD: if_urndis.c,v 1.31 2011/07/03 15:47:17 matthew Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 2010 Jonathan Armani <armani@openbsd.org> 5 * Copyright (c) 2010 Jonathan Armani <armani@openbsd.org>
6 * Copyright (c) 2010 Fabien Romano <fabien@openbsd.org> 6 * Copyright (c) 2010 Fabien Romano <fabien@openbsd.org>
7 * Copyright (c) 2010 Michael Knudsen <mk@openbsd.org> 7 * Copyright (c) 2010 Michael Knudsen <mk@openbsd.org>
8 * All rights reserved. 8 * All rights reserved.
9 * 9 *
10 * Permission to use, copy, modify, and distribute this software for any 10 * Permission to use, copy, modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above 11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies. 12 * copyright notice and this permission notice appear in all copies.
13 * 13 *
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */ 21 */
22 22
23#include <sys/cdefs.h> 23#include <sys/cdefs.h>
24__KERNEL_RCSID(0, "$NetBSD: if_urndis.c,v 1.46 2022/03/03 05:56:28 riastradh Exp $"); 24__KERNEL_RCSID(0, "$NetBSD: if_urndis.c,v 1.47 2022/03/03 05:56:58 riastradh Exp $");
25 25
26#ifdef _KERNEL_OPT 26#ifdef _KERNEL_OPT
27#include "opt_usb.h" 27#include "opt_usb.h"
28#endif 28#endif
29 29
30#include <sys/param.h> 30#include <sys/param.h>
31#include <sys/kmem.h> 31#include <sys/kmem.h>
32 32
33#include <dev/usb/usbnet.h> 33#include <dev/usb/usbnet.h>
34#include <dev/usb/usbdevs.h> 34#include <dev/usb/usbdevs.h>
35#include <dev/usb/usbcdc.h> 35#include <dev/usb/usbcdc.h>
36 36
37#include <dev/ic/rndisreg.h> 37#include <dev/ic/rndisreg.h>
@@ -62,28 +62,26 @@ struct urndis_softc { @@ -62,28 +62,26 @@ struct urndis_softc {
62 62
63#define URNDIS_RESPONSE_LEN 0x400 63#define URNDIS_RESPONSE_LEN 0x400
64 64
65#if 0 65#if 0
66static void urndis_watchdog(struct ifnet *); 66static void urndis_watchdog(struct ifnet *);
67#endif 67#endif
68 68
69static int urndis_uno_init(struct ifnet *); 69static int urndis_uno_init(struct ifnet *);
70static void urndis_uno_rx_loop(struct usbnet *, struct usbnet_chain *, 70static void urndis_uno_rx_loop(struct usbnet *, struct usbnet_chain *,
71 uint32_t); 71 uint32_t);
72static unsigned urndis_uno_tx_prepare(struct usbnet *, struct mbuf *, 72static unsigned urndis_uno_tx_prepare(struct usbnet *, struct mbuf *,
73 struct usbnet_chain *); 73 struct usbnet_chain *);
74 74
75static int urndis_init_un(struct ifnet *, struct usbnet *); 
76 
77static uint32_t urndis_ctrl_handle_init(struct usbnet *, 75static uint32_t urndis_ctrl_handle_init(struct usbnet *,
78 const struct rndis_comp_hdr *); 76 const struct rndis_comp_hdr *);
79static uint32_t urndis_ctrl_handle_query(struct usbnet *, 77static uint32_t urndis_ctrl_handle_query(struct usbnet *,
80 const struct rndis_comp_hdr *, void **, size_t *); 78 const struct rndis_comp_hdr *, void **, size_t *);
81static uint32_t urndis_ctrl_handle_reset(struct usbnet *, 79static uint32_t urndis_ctrl_handle_reset(struct usbnet *,
82 const struct rndis_comp_hdr *); 80 const struct rndis_comp_hdr *);
83static uint32_t urndis_ctrl_handle_status(struct usbnet *, 81static uint32_t urndis_ctrl_handle_status(struct usbnet *,
84 const struct rndis_comp_hdr *); 82 const struct rndis_comp_hdr *);
85 83
86static uint32_t urndis_ctrl_set(struct usbnet *, uint32_t, void *, 84static uint32_t urndis_ctrl_set(struct usbnet *, uint32_t, void *,
87 size_t); 85 size_t);
88 86
89static int urndis_match(device_t, cfdata_t, void *); 87static int urndis_match(device_t, cfdata_t, void *);
@@ -845,48 +843,34 @@ urndis_watchdog(struct ifnet *ifp) @@ -845,48 +843,34 @@ urndis_watchdog(struct ifnet *ifp)
845 struct urndis_softc *sc = usbnet_softc(un); 843 struct urndis_softc *sc = usbnet_softc(un);
846 844
847 if (un->un_dying) 845 if (un->un_dying)
848 return; 846 return;
849 847
850 if_statinc(ifp, if_oerrors); 848 if_statinc(ifp, if_oerrors);
851 printf("%s: watchdog timeout\n", DEVNAME(un)); 849 printf("%s: watchdog timeout\n", DEVNAME(un));
852 850
853 urndis_ctrl_keepalive(un); 851 urndis_ctrl_keepalive(un);
854} 852}
855#endif 853#endif
856 854
857static int 855static int
858urndis_init_un(struct ifnet *ifp, struct usbnet *un) 
859{ 
860 int err; 
861 
862 err = urndis_ctrl_init(un); 
863 if (err != RNDIS_STATUS_SUCCESS) 
864 return EIO; 
865 
866 return err; 
867} 
868 
869static int 
870urndis_uno_init(struct ifnet *ifp) 856urndis_uno_init(struct ifnet *ifp)
871{ 857{
872 struct usbnet *un = ifp->if_softc; 858 struct usbnet *un = ifp->if_softc;
873 int error; 
874 859
875 KASSERT(IFNET_LOCKED(ifp)); 860 KASSERT(IFNET_LOCKED(ifp));
876 861
877 error = urndis_init_un(ifp, un); 862 if (urndis_ctrl_init(un) != RNDIS_STATUS_SUCCESS)
878 if (error) 863 return EIO;
879 return EIO; /* XXX */ 
880 864
881 return 0; 865 return 0;
882} 866}
883 867
884static int 868static int
885urndis_match(device_t parent, cfdata_t match, void *aux) 869urndis_match(device_t parent, cfdata_t match, void *aux)
886{ 870{
887 struct usbif_attach_arg *uiaa = aux; 871 struct usbif_attach_arg *uiaa = aux;
888 usb_interface_descriptor_t *id; 872 usb_interface_descriptor_t *id;
889 873
890 if (!uiaa->uiaa_iface) 874 if (!uiaa->uiaa_iface)
891 return UMATCH_NONE; 875 return UMATCH_NONE;
892 876
@@ -1034,28 +1018,31 @@ urndis_attach(device_t parent, device_t  @@ -1034,28 +1018,31 @@ urndis_attach(device_t parent, device_t
1034 if (un->un_ed[USBNET_ENDPT_RX] == 0) 1018 if (un->un_ed[USBNET_ENDPT_RX] == 0)
1035 aprint_error("%s: could not find data bulk in\n", DEVNAME(un)); 1019 aprint_error("%s: could not find data bulk in\n", DEVNAME(un));
1036 if (un->un_ed[USBNET_ENDPT_TX] == 0) 1020 if (un->un_ed[USBNET_ENDPT_TX] == 0)
1037 aprint_error("%s: could not find data bulk out\n",DEVNAME(un)); 1021 aprint_error("%s: could not find data bulk out\n",DEVNAME(un));
1038 if (un->un_ed[USBNET_ENDPT_RX] == 0 || un->un_ed[USBNET_ENDPT_TX] == 0) 1022 if (un->un_ed[USBNET_ENDPT_RX] == 0 || un->un_ed[USBNET_ENDPT_TX] == 0)
1039 return; 1023 return;
1040 1024
1041#if 0 1025#if 0
1042 ifp->if_watchdog = urndis_watchdog; 1026 ifp->if_watchdog = urndis_watchdog;
1043#endif 1027#endif
1044 1028
1045 usbnet_attach(un); 1029 usbnet_attach(un);
1046 1030
1047 struct ifnet *ifp = usbnet_ifp(un); 1031 if (urndis_ctrl_init(un) != RNDIS_STATUS_SUCCESS) {
1048 urndis_init_un(ifp, un); 1032 aprint_error("%s: unable to initialize hardware\n",
 1033 DEVNAME(un));
 1034 return;
 1035 }
1049 1036
1050 if (urndis_ctrl_query(un, OID_802_3_PERMANENT_ADDRESS, NULL, 0, 1037 if (urndis_ctrl_query(un, OID_802_3_PERMANENT_ADDRESS, NULL, 0,
1051 &buf, &bufsz) != RNDIS_STATUS_SUCCESS) { 1038 &buf, &bufsz) != RNDIS_STATUS_SUCCESS) {
1052 aprint_error("%s: unable to get hardware address\n", 1039 aprint_error("%s: unable to get hardware address\n",
1053 DEVNAME(un)); 1040 DEVNAME(un));
1054 return; 1041 return;
1055 } 1042 }
1056 1043
1057 if (bufsz == ETHER_ADDR_LEN) { 1044 if (bufsz == ETHER_ADDR_LEN) {
1058 memcpy(un->un_eaddr, buf, ETHER_ADDR_LEN); 1045 memcpy(un->un_eaddr, buf, ETHER_ADDR_LEN);
1059 kmem_free(buf, bufsz); 1046 kmem_free(buf, bufsz);
1060 } else { 1047 } else {
1061 aprint_error("%s: invalid address\n", DEVNAME(un)); 1048 aprint_error("%s: invalid address\n", DEVNAME(un));