Fri Aug 28 19:36:34 2020 UTC ()
Pull up following revision(s) (requested by riastradh in ticket #1067):

	sys/dev/usb/usbnet.c: revision 1.39
	sys/dev/usb/if_atu.c: revision 1.73

usbnet: Reject buflen>MCLBYTES in usbnet_newbuf.
atu(4): Reject packets larger than MCLBYTES.


(martin)
diff -r1.65 -r1.65.2.1 src/sys/dev/usb/if_atu.c
diff -r1.25.2.4 -r1.25.2.5 src/sys/dev/usb/usbnet.c

cvs diff -r1.65 -r1.65.2.1 src/sys/dev/usb/if_atu.c (switch to unified diff)

--- src/sys/dev/usb/if_atu.c 2019/05/05 03:17:54 1.65
+++ src/sys/dev/usb/if_atu.c 2020/08/28 19:36:34 1.65.2.1
@@ -1,2283 +1,2287 @@ @@ -1,2283 +1,2287 @@
1/* $NetBSD: if_atu.c,v 1.65 2019/05/05 03:17:54 mrg Exp $ */ 1/* $NetBSD: if_atu.c,v 1.65.2.1 2020/08/28 19:36:34 martin Exp $ */
2/* $OpenBSD: if_atu.c,v 1.48 2004/12/30 01:53:21 dlg Exp $ */ 2/* $OpenBSD: if_atu.c,v 1.48 2004/12/30 01:53:21 dlg Exp $ */
3/* 3/*
4 * Copyright (c) 2003, 2004 4 * Copyright (c) 2003, 2004
5 * Daan Vreeken <Danovitsch@Vitsch.net>. All rights reserved. 5 * Daan Vreeken <Danovitsch@Vitsch.net>. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 16 * must display the following acknowledgement:
17 * This product includes software developed by Daan Vreeken. 17 * This product includes software developed by Daan Vreeken.
18 * 4. Neither the name of the author nor the names of any co-contributors 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 19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission. 20 * without specific prior written permission.
21 * 21 *
22 * THIS SOFTWARE IS PROVIDED BY Daan Vreeken AND CONTRIBUTORS ``AS IS'' AND 22 * THIS SOFTWARE IS PROVIDED BY Daan Vreeken AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Daan Vreeken OR THE VOICES IN HIS HEAD 25 * ARE DISCLAIMED. IN NO EVENT SHALL Daan Vreeken OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE. 32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35/* 35/*
36 * Atmel AT76c503 / AT76c503a / AT76c505 / AT76c505a USB WLAN driver 36 * Atmel AT76c503 / AT76c503a / AT76c505 / AT76c505a USB WLAN driver
37 * version 0.5 - 2004-08-03 37 * version 0.5 - 2004-08-03
38 * 38 *
39 * Originally written by Daan Vreeken <Danovitsch @ Vitsch . net> 39 * Originally written by Daan Vreeken <Danovitsch @ Vitsch . net>
40 * http://vitsch.net/bsd/atuwi 40 * http://vitsch.net/bsd/atuwi
41 * 41 *
42 * Contributed to by : 42 * Contributed to by :
43 * Chris Whitehouse, Alistair Phillips, Peter Pilka, Martijn van Buul, 43 * Chris Whitehouse, Alistair Phillips, Peter Pilka, Martijn van Buul,
44 * Suihong Liang, Arjan van Leeuwen, Stuart Walsh 44 * Suihong Liang, Arjan van Leeuwen, Stuart Walsh
45 * 45 *
46 * Ported to OpenBSD by Theo de Raadt and David Gwynne. 46 * Ported to OpenBSD by Theo de Raadt and David Gwynne.
47 * Ported to NetBSD by Jesse Off 47 * Ported to NetBSD by Jesse Off
48 */ 48 */
49 49
50#include <sys/cdefs.h> 50#include <sys/cdefs.h>
51__KERNEL_RCSID(0, "$NetBSD: if_atu.c,v 1.65 2019/05/05 03:17:54 mrg Exp $"); 51__KERNEL_RCSID(0, "$NetBSD: if_atu.c,v 1.65.2.1 2020/08/28 19:36:34 martin Exp $");
52 52
53#ifdef _KERNEL_OPT 53#ifdef _KERNEL_OPT
54#include "opt_usb.h" 54#include "opt_usb.h"
55#endif 55#endif
56 56
57#include <sys/param.h> 57#include <sys/param.h>
58#include <sys/sockio.h> 58#include <sys/sockio.h>
59#include <sys/mbuf.h> 59#include <sys/mbuf.h>
60#include <sys/kernel.h> 60#include <sys/kernel.h>
61#include <sys/socket.h> 61#include <sys/socket.h>
62#include <sys/systm.h> 62#include <sys/systm.h>
63#include <sys/kthread.h> 63#include <sys/kthread.h>
64#include <sys/queue.h> 64#include <sys/queue.h>
65#include <sys/device.h> 65#include <sys/device.h>
66#include <sys/bus.h> 66#include <sys/bus.h>
67 67
68#include <dev/usb/usb.h> 68#include <dev/usb/usb.h>
69#include <dev/usb/usbdi.h> 69#include <dev/usb/usbdi.h>
70#include <dev/usb/usbdi_util.h> 70#include <dev/usb/usbdi_util.h>
71#include <dev/usb/usbdivar.h> 71#include <dev/usb/usbdivar.h>
72#include <dev/usb/usbdevs.h> 72#include <dev/usb/usbdevs.h>
73 73
74#include <dev/microcode/atmel/atmel_intersil_fw.h> 74#include <dev/microcode/atmel/atmel_intersil_fw.h>
75#include <dev/microcode/atmel/atmel_rfmd2958-smc_fw.h> 75#include <dev/microcode/atmel/atmel_rfmd2958-smc_fw.h>
76#include <dev/microcode/atmel/atmel_rfmd2958_fw.h> 76#include <dev/microcode/atmel/atmel_rfmd2958_fw.h>
77#include <dev/microcode/atmel/atmel_rfmd_fw.h> 77#include <dev/microcode/atmel/atmel_rfmd_fw.h>
78 78
79#include <net/bpf.h> 79#include <net/bpf.h>
80#include <net/if.h> 80#include <net/if.h>
81#include <net/if_dl.h> 81#include <net/if_dl.h>
82#include <net/if_media.h> 82#include <net/if_media.h>
83#include <net/if_ether.h> 83#include <net/if_ether.h>
84 84
85#ifdef INET 85#ifdef INET
86#include <netinet/in.h> 86#include <netinet/in.h>
87#include <netinet/if_ether.h> 87#include <netinet/if_ether.h>
88#endif 88#endif
89 89
90#include <net80211/ieee80211_var.h> 90#include <net80211/ieee80211_var.h>
91#include <net80211/ieee80211_radiotap.h> 91#include <net80211/ieee80211_radiotap.h>
92 92
93#include <dev/usb/if_atureg.h> 93#include <dev/usb/if_atureg.h>
94 94
95#ifdef ATU_DEBUG 95#ifdef ATU_DEBUG
96#define DPRINTF(x) do { if (atudebug) printf x; } while (0) 96#define DPRINTF(x) do { if (atudebug) printf x; } while (0)
97#define DPRINTFN(n,x) do { if (atudebug>(n)) printf x; } while (0) 97#define DPRINTFN(n,x) do { if (atudebug>(n)) printf x; } while (0)
98int atudebug = 1; 98int atudebug = 1;
99#else 99#else
100#define DPRINTF(x) 100#define DPRINTF(x)
101#define DPRINTFN(n,x) 101#define DPRINTFN(n,x)
102#endif 102#endif
103 103
104/* 104/*
105 * Various supported device vendors/products/radio type. 105 * Various supported device vendors/products/radio type.
106 */ 106 */
107static const struct atu_type atu_devs[] = { 107static const struct atu_type atu_devs[] = {
108 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3CRSHEW696, 108 { USB_VENDOR_3COM, USB_PRODUCT_3COM_3CRSHEW696,
109 RadioRFMD, ATU_NO_QUIRK }, 109 RadioRFMD, ATU_NO_QUIRK },
110 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_BWU613, 110 { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_BWU613,
111 RadioRFMD, ATU_NO_QUIRK }, 111 RadioRFMD, ATU_NO_QUIRK },
112 { USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_2664W, 112 { USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_2664W,
113 AT76C503_rfmd_acc, ATU_NO_QUIRK }, 113 AT76C503_rfmd_acc, ATU_NO_QUIRK },
114 { USB_VENDOR_ACERP, USB_PRODUCT_ACERP_AWL300, 114 { USB_VENDOR_ACERP, USB_PRODUCT_ACERP_AWL300,
115 RadioIntersil, ATU_NO_QUIRK }, 115 RadioIntersil, ATU_NO_QUIRK },
116 { USB_VENDOR_ACERP, USB_PRODUCT_ACERP_AWL400, 116 { USB_VENDOR_ACERP, USB_PRODUCT_ACERP_AWL400,
117 RadioRFMD, ATU_NO_QUIRK }, 117 RadioRFMD, ATU_NO_QUIRK },
118 { USB_VENDOR_ACTIONTEC, USB_PRODUCT_ACTIONTEC_UAT1, 118 { USB_VENDOR_ACTIONTEC, USB_PRODUCT_ACTIONTEC_UAT1,
119 RadioRFMD, ATU_NO_QUIRK }, 119 RadioRFMD, ATU_NO_QUIRK },
120 { USB_VENDOR_ADDTRON, USB_PRODUCT_ADDTRON_AWU120, 120 { USB_VENDOR_ADDTRON, USB_PRODUCT_ADDTRON_AWU120,
121 RadioIntersil, ATU_NO_QUIRK }, 121 RadioIntersil, ATU_NO_QUIRK },
122 { USB_VENDOR_AINCOMM, USB_PRODUCT_AINCOMM_AWU2000B, 122 { USB_VENDOR_AINCOMM, USB_PRODUCT_AINCOMM_AWU2000B,
123 RadioRFMD2958, ATU_NO_QUIRK }, 123 RadioRFMD2958, ATU_NO_QUIRK },
124 { USB_VENDOR_ASKEY, USB_PRODUCT_ASKEY_VOYAGER1010, 124 { USB_VENDOR_ASKEY, USB_PRODUCT_ASKEY_VOYAGER1010,
125 RadioIntersil, ATU_NO_QUIRK }, 125 RadioIntersil, ATU_NO_QUIRK },
126 { USB_VENDOR_ASKEY, USB_PRODUCT_ASKEY_WLL013I, 126 { USB_VENDOR_ASKEY, USB_PRODUCT_ASKEY_WLL013I,
127 RadioIntersil, ATU_NO_QUIRK }, 127 RadioIntersil, ATU_NO_QUIRK },
128 { USB_VENDOR_ASKEY, USB_PRODUCT_ASKEY_WLL013, 128 { USB_VENDOR_ASKEY, USB_PRODUCT_ASKEY_WLL013,
129 RadioRFMD, ATU_NO_QUIRK }, 129 RadioRFMD, ATU_NO_QUIRK },
130 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C503I1, 130 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C503I1,
131 RadioIntersil, ATU_NO_QUIRK }, 131 RadioIntersil, ATU_NO_QUIRK },
132 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C503I2, 132 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C503I2,
133 AT76C503_i3863, ATU_NO_QUIRK }, 133 AT76C503_i3863, ATU_NO_QUIRK },
134 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C503RFMD, 134 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C503RFMD,
135 RadioRFMD, ATU_NO_QUIRK }, 135 RadioRFMD, ATU_NO_QUIRK },
136 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C505RFMD, 136 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C505RFMD,
137 AT76C505_rfmd, ATU_NO_QUIRK }, 137 AT76C505_rfmd, ATU_NO_QUIRK },
138 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C505RFMD2958, 138 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C505RFMD2958,
139 RadioRFMD2958, ATU_NO_QUIRK }, 139 RadioRFMD2958, ATU_NO_QUIRK },
140 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C505A, /* SMC2662 V.4 */ 140 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C505A, /* SMC2662 V.4 */
141 RadioRFMD2958_SMC, ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY }, 141 RadioRFMD2958_SMC, ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
142 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C505AS, /* quirk? */ 142 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_AT76C505AS, /* quirk? */
143 RadioRFMD2958_SMC, ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY }, 143 RadioRFMD2958_SMC, ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
144 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_WN210, 144 { USB_VENDOR_ATMEL, USB_PRODUCT_ATMEL_WN210,
145 RadioRFMD, ATU_NO_QUIRK }, 145 RadioRFMD, ATU_NO_QUIRK },
146 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D6050, 146 { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D6050,
147 RadioRFMD, ATU_NO_QUIRK }, 147 RadioRFMD, ATU_NO_QUIRK },
148 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C11U, 148 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_C11U,
149 RadioIntersil, ATU_NO_QUIRK }, 149 RadioIntersil, ATU_NO_QUIRK },
150 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_WL210, 150 { USB_VENDOR_CONCEPTRONIC, USB_PRODUCT_CONCEPTRONIC_WL210,
151 RadioIntersil, ATU_NO_QUIRK }, 151 RadioIntersil, ATU_NO_QUIRK },
152 { USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQWLAN, 152 { USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQWLAN,
153 RadioRFMD, ATU_NO_QUIRK }, 153 RadioRFMD, ATU_NO_QUIRK },
154 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_WLUSB_11_STICK, 154 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_WLUSB_11_STICK,
155 RadioRFMD2958, ATU_NO_QUIRK }, 155 RadioRFMD2958, ATU_NO_QUIRK },
156 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CHUSB611G, 156 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_CHUSB611G,
157 RadioRFMD2958, ATU_NO_QUIRK }, 157 RadioRFMD2958, ATU_NO_QUIRK },
158 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_WL200U, 158 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_WL200U,
159 RadioRFMD, ATU_NO_QUIRK }, 159 RadioRFMD, ATU_NO_QUIRK },
160 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_WL240U, 160 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_WL240U,
161 RadioRFMD2958, ATU_NO_QUIRK }, 161 RadioRFMD2958, ATU_NO_QUIRK },
162 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_XH1153, 162 { USB_VENDOR_DICKSMITH, USB_PRODUCT_DICKSMITH_XH1153,
163 RadioRFMD, ATU_NO_QUIRK }, 163 RadioRFMD, ATU_NO_QUIRK },
164 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWL120E, 164 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWL120E,
165 RadioRFMD, ATU_NO_QUIRK }, 165 RadioRFMD, ATU_NO_QUIRK },
166 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWLBM101, 166 { USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNWLBM101,
167 RadioRFMD, ATU_NO_QUIRK }, 167 RadioRFMD, ATU_NO_QUIRK },
168 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_WLAN, /* quirk? */ 168 { USB_VENDOR_GIGASET, USB_PRODUCT_GIGASET_WLAN, /* quirk? */
169 RadioRFMD2958_SMC, ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY }, 169 RadioRFMD2958_SMC, ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
170 { USB_VENDOR_HP, USB_PRODUCT_HP_HN210W, 170 { USB_VENDOR_HP, USB_PRODUCT_HP_HN210W,
171 RadioIntersil, ATU_NO_QUIRK }, 171 RadioIntersil, ATU_NO_QUIRK },
172 { USB_VENDOR_INTEL, USB_PRODUCT_INTEL_AP310, 172 { USB_VENDOR_INTEL, USB_PRODUCT_INTEL_AP310,
173 RadioIntersil, ATU_NO_QUIRK }, 173 RadioIntersil, ATU_NO_QUIRK },
174 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBWNB11A, 174 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBWNB11A,
175 RadioIntersil, ATU_NO_QUIRK }, 175 RadioIntersil, ATU_NO_QUIRK },
176 { USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_2662WAR, 176 { USB_VENDOR_LEXAR, USB_PRODUCT_LEXAR_2662WAR,
177 RadioRFMD, ATU_NO_QUIRK }, 177 RadioRFMD, ATU_NO_QUIRK },
178 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_WUSB11, 178 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_WUSB11,
179 RadioIntersil, ATU_NO_QUIRK }, 179 RadioIntersil, ATU_NO_QUIRK },
180 { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_WUSB11, 180 { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_WUSB11,
181 RadioRFMD, ATU_NO_QUIRK }, 181 RadioRFMD, ATU_NO_QUIRK },
182 { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_NWU11B, 182 { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_NWU11B,
183 RadioRFMD, ATU_NO_QUIRK }, 183 RadioRFMD, ATU_NO_QUIRK },
184 { USB_VENDOR_LINKSYS3, USB_PRODUCT_LINKSYS3_WUSB11V28, 184 { USB_VENDOR_LINKSYS3, USB_PRODUCT_LINKSYS3_WUSB11V28,
185 RadioRFMD2958, ATU_NO_QUIRK }, 185 RadioRFMD2958, ATU_NO_QUIRK },
186 { USB_VENDOR_MSI, USB_PRODUCT_MSI_WLAN, 186 { USB_VENDOR_MSI, USB_PRODUCT_MSI_WLAN,
187 RadioRFMD2958, ATU_NO_QUIRK }, 187 RadioRFMD2958, ATU_NO_QUIRK },
188 { USB_VENDOR_NETGEAR2, USB_PRODUCT_NETGEAR2_MA101, 188 { USB_VENDOR_NETGEAR2, USB_PRODUCT_NETGEAR2_MA101,
189 RadioIntersil, ATU_NO_QUIRK }, 189 RadioIntersil, ATU_NO_QUIRK },
190 { USB_VENDOR_NETGEAR2, USB_PRODUCT_NETGEAR2_MA101B, 190 { USB_VENDOR_NETGEAR2, USB_PRODUCT_NETGEAR2_MA101B,
191 RadioRFMD, ATU_NO_QUIRK }, 191 RadioRFMD, ATU_NO_QUIRK },
192 { USB_VENDOR_OQO, USB_PRODUCT_OQO_WIFI01, 192 { USB_VENDOR_OQO, USB_PRODUCT_OQO_WIFI01,
193 RadioRFMD2958_SMC, ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY }, 193 RadioRFMD2958_SMC, ATU_QUIRK_NO_REMAP | ATU_QUIRK_FW_DELAY },
194 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GW_US11S, 194 { USB_VENDOR_PLANEX2, USB_PRODUCT_PLANEX2_GW_US11S,
195 RadioRFMD, ATU_NO_QUIRK }, 195 RadioRFMD, ATU_NO_QUIRK },
196 { USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_SWL2100W, 196 { USB_VENDOR_SAMSUNG, USB_PRODUCT_SAMSUNG_SWL2100W,
197 AT76C503_i3863, ATU_NO_QUIRK }, 197 AT76C503_i3863, ATU_NO_QUIRK },
198 { USB_VENDOR_SIEMENS2, USB_PRODUCT_SIEMENS2_WLL013, 198 { USB_VENDOR_SIEMENS2, USB_PRODUCT_SIEMENS2_WLL013,
199 RadioRFMD, ATU_NO_QUIRK }, 199 RadioRFMD, ATU_NO_QUIRK },
200 { USB_VENDOR_SMC3, USB_PRODUCT_SMC3_2662WV1, 200 { USB_VENDOR_SMC3, USB_PRODUCT_SMC3_2662WV1,
201 RadioIntersil, ATU_NO_QUIRK }, 201 RadioIntersil, ATU_NO_QUIRK },
202 { USB_VENDOR_SMC3, USB_PRODUCT_SMC3_2662WV2, 202 { USB_VENDOR_SMC3, USB_PRODUCT_SMC3_2662WV2,
203 AT76C503_rfmd_acc, ATU_NO_QUIRK }, 203 AT76C503_rfmd_acc, ATU_NO_QUIRK },
204 { USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_U300C, 204 { USB_VENDOR_TEKRAM, USB_PRODUCT_TEKRAM_U300C,
205 RadioIntersil, ATU_NO_QUIRK }, 205 RadioIntersil, ATU_NO_QUIRK },
206 { USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_M4Y750, 206 { USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_M4Y750,
207 RadioIntersil, ATU_NO_QUIRK }, 207 RadioIntersil, ATU_NO_QUIRK },
208}; 208};
209 209
210static const struct atu_radfirm { 210static const struct atu_radfirm {
211 enum atu_radio_type atur_type; 211 enum atu_radio_type atur_type;
212 unsigned char *atur_internal; 212 unsigned char *atur_internal;
213 size_t atur_internal_sz; 213 size_t atur_internal_sz;
214 unsigned char *atur_external; 214 unsigned char *atur_external;
215 size_t atur_external_sz; 215 size_t atur_external_sz;
216} atu_radfirm[] = { 216} atu_radfirm[] = {
217 { RadioRFMD, 217 { RadioRFMD,
218 atmel_fw_rfmd_int, sizeof(atmel_fw_rfmd_int), 218 atmel_fw_rfmd_int, sizeof(atmel_fw_rfmd_int),
219 atmel_fw_rfmd_ext, sizeof(atmel_fw_rfmd_ext) }, 219 atmel_fw_rfmd_ext, sizeof(atmel_fw_rfmd_ext) },
220 { RadioRFMD2958, 220 { RadioRFMD2958,
221 atmel_fw_rfmd2958_int, sizeof(atmel_fw_rfmd2958_int), 221 atmel_fw_rfmd2958_int, sizeof(atmel_fw_rfmd2958_int),
222 atmel_fw_rfmd2958_ext, sizeof(atmel_fw_rfmd2958_ext) }, 222 atmel_fw_rfmd2958_ext, sizeof(atmel_fw_rfmd2958_ext) },
223 { RadioRFMD2958_SMC, 223 { RadioRFMD2958_SMC,
224 atmel_fw_rfmd2958_smc_int, sizeof(atmel_fw_rfmd2958_smc_int), 224 atmel_fw_rfmd2958_smc_int, sizeof(atmel_fw_rfmd2958_smc_int),
225 atmel_fw_rfmd2958_smc_ext, sizeof(atmel_fw_rfmd2958_smc_ext) }, 225 atmel_fw_rfmd2958_smc_ext, sizeof(atmel_fw_rfmd2958_smc_ext) },
226 { RadioIntersil, 226 { RadioIntersil,
227 atmel_fw_intersil_int, sizeof(atmel_fw_intersil_int), 227 atmel_fw_intersil_int, sizeof(atmel_fw_intersil_int),
228 atmel_fw_intersil_ext, sizeof(atmel_fw_intersil_ext) } 228 atmel_fw_intersil_ext, sizeof(atmel_fw_intersil_ext) }
229}; 229};
230 230
231int atu_newbuf(struct atu_softc *, struct atu_chain *, struct mbuf *); 231int atu_newbuf(struct atu_softc *, struct atu_chain *, struct mbuf *);
232void atu_rxeof(struct usbd_xfer *, void *, usbd_status); 232void atu_rxeof(struct usbd_xfer *, void *, usbd_status);
233void atu_txeof(struct usbd_xfer *, void *, usbd_status); 233void atu_txeof(struct usbd_xfer *, void *, usbd_status);
234void atu_start(struct ifnet *); 234void atu_start(struct ifnet *);
235int atu_ioctl(struct ifnet *, u_long, void *); 235int atu_ioctl(struct ifnet *, u_long, void *);
236int atu_init(struct ifnet *); 236int atu_init(struct ifnet *);
237void atu_stop(struct ifnet *, int); 237void atu_stop(struct ifnet *, int);
238void atu_watchdog(struct ifnet *); 238void atu_watchdog(struct ifnet *);
239usbd_status atu_usb_request(struct atu_softc *, uint8_t, 239usbd_status atu_usb_request(struct atu_softc *, uint8_t,
240 uint8_t, uint16_t, uint16_t, 240 uint8_t, uint16_t, uint16_t,
241 uint16_t, uint8_t *); 241 uint16_t, uint8_t *);
242int atu_send_command(struct atu_softc *, uint8_t *, int); 242int atu_send_command(struct atu_softc *, uint8_t *, int);
243int atu_get_cmd_status(struct atu_softc *, uint8_t, 243int atu_get_cmd_status(struct atu_softc *, uint8_t,
244 uint8_t *); 244 uint8_t *);
245int atu_wait_completion(struct atu_softc *, uint8_t, 245int atu_wait_completion(struct atu_softc *, uint8_t,
246 uint8_t *); 246 uint8_t *);
247int atu_send_mib(struct atu_softc *, uint8_t, 247int atu_send_mib(struct atu_softc *, uint8_t,
248 uint8_t, uint8_t, void *); 248 uint8_t, uint8_t, void *);
249int atu_get_mib(struct atu_softc *, uint8_t, 249int atu_get_mib(struct atu_softc *, uint8_t,
250 uint8_t, uint8_t, uint8_t *); 250 uint8_t, uint8_t, uint8_t *);
251#if 0 251#if 0
252int atu_start_ibss(struct atu_softc *); 252int atu_start_ibss(struct atu_softc *);
253#endif 253#endif
254int atu_start_scan(struct atu_softc *); 254int atu_start_scan(struct atu_softc *);
255int atu_switch_radio(struct atu_softc *, int); 255int atu_switch_radio(struct atu_softc *, int);
256int atu_initial_config(struct atu_softc *); 256int atu_initial_config(struct atu_softc *);
257int atu_join(struct atu_softc *, struct ieee80211_node *); 257int atu_join(struct atu_softc *, struct ieee80211_node *);
258int8_t atu_get_dfu_state(struct atu_softc *); 258int8_t atu_get_dfu_state(struct atu_softc *);
259uint8_t atu_get_opmode(struct atu_softc *, uint8_t *); 259uint8_t atu_get_opmode(struct atu_softc *, uint8_t *);
260void atu_internal_firmware(device_t); 260void atu_internal_firmware(device_t);
261void atu_external_firmware(device_t); 261void atu_external_firmware(device_t);
262int atu_get_card_config(struct atu_softc *); 262int atu_get_card_config(struct atu_softc *);
263int atu_media_change(struct ifnet *); 263int atu_media_change(struct ifnet *);
264void atu_media_status(struct ifnet *, struct ifmediareq *); 264void atu_media_status(struct ifnet *, struct ifmediareq *);
265int atu_tx_list_init(struct atu_softc *); 265int atu_tx_list_init(struct atu_softc *);
266int atu_rx_list_init(struct atu_softc *); 266int atu_rx_list_init(struct atu_softc *);
267void atu_xfer_list_free(struct atu_softc *, struct atu_chain *, 267void atu_xfer_list_free(struct atu_softc *, struct atu_chain *,
268 int); 268 int);
269 269
270#ifdef ATU_DEBUG 270#ifdef ATU_DEBUG
271void atu_debug_print(struct atu_softc *); 271void atu_debug_print(struct atu_softc *);
272#endif 272#endif
273 273
274void atu_task(void *); 274void atu_task(void *);
275int atu_newstate(struct ieee80211com *, enum ieee80211_state, int); 275int atu_newstate(struct ieee80211com *, enum ieee80211_state, int);
276int atu_tx_start(struct atu_softc *, struct ieee80211_node *, 276int atu_tx_start(struct atu_softc *, struct ieee80211_node *,
277 struct atu_chain *, struct mbuf *); 277 struct atu_chain *, struct mbuf *);
278void atu_complete_attach(struct atu_softc *); 278void atu_complete_attach(struct atu_softc *);
279uint8_t atu_calculate_padding(int); 279uint8_t atu_calculate_padding(int);
280 280
281int atu_match(device_t, cfdata_t, void *); 281int atu_match(device_t, cfdata_t, void *);
282void atu_attach(device_t, device_t, void *); 282void atu_attach(device_t, device_t, void *);
283int atu_detach(device_t, int); 283int atu_detach(device_t, int);
284int atu_activate(device_t, enum devact); 284int atu_activate(device_t, enum devact);
285 285
286CFATTACH_DECL_NEW(atu, sizeof(struct atu_softc), atu_match, atu_attach, 286CFATTACH_DECL_NEW(atu, sizeof(struct atu_softc), atu_match, atu_attach,
287 atu_detach, atu_activate); 287 atu_detach, atu_activate);
288 288
289usbd_status 289usbd_status
290atu_usb_request(struct atu_softc *sc, uint8_t type, 290atu_usb_request(struct atu_softc *sc, uint8_t type,
291 uint8_t request, uint16_t value, uint16_t index, uint16_t length, 291 uint8_t request, uint16_t value, uint16_t index, uint16_t length,
292 uint8_t *data) 292 uint8_t *data)
293{ 293{
294 usb_device_request_t req; 294 usb_device_request_t req;
295 struct usbd_xfer *xfer; 295 struct usbd_xfer *xfer;
296 usbd_status err; 296 usbd_status err;
297 int total_len = 0, s; 297 int total_len = 0, s;
298 298
299 req.bmRequestType = type; 299 req.bmRequestType = type;
300 req.bRequest = request; 300 req.bRequest = request;
301 USETW(req.wValue, value); 301 USETW(req.wValue, value);
302 USETW(req.wIndex, index); 302 USETW(req.wIndex, index);
303 USETW(req.wLength, length); 303 USETW(req.wLength, length);
304 304
305#ifdef ATU_DEBUG 305#ifdef ATU_DEBUG
306 if (atudebug) { 306 if (atudebug) {
307 DPRINTFN(20, ("%s: req=%02x val=%02x ind=%02x " 307 DPRINTFN(20, ("%s: req=%02x val=%02x ind=%02x "
308 "len=%02x\n", device_xname(sc->atu_dev), request, 308 "len=%02x\n", device_xname(sc->atu_dev), request,
309 value, index, length)); 309 value, index, length));
310 } 310 }
311#endif /* ATU_DEBUG */ 311#endif /* ATU_DEBUG */
312 312
313 s = splnet(); 313 s = splnet();
314 314
315 struct usbd_pipe *pipe0 = usbd_get_pipe0(sc->atu_udev); 315 struct usbd_pipe *pipe0 = usbd_get_pipe0(sc->atu_udev);
316 int error = usbd_create_xfer(pipe0, length, 0, 0, 316 int error = usbd_create_xfer(pipe0, length, 0, 0,
317 &xfer); 317 &xfer);
318 if (error) { 318 if (error) {
319 splx(s); 319 splx(s);
320 return USBD_IOERROR; 320 return USBD_IOERROR;
321 } 321 }
322 usbd_setup_default_xfer(xfer, sc->atu_udev, 0, 500000, &req, data, 322 usbd_setup_default_xfer(xfer, sc->atu_udev, 0, 500000, &req, data,
323 length, USBD_SHORT_XFER_OK, NULL); 323 length, USBD_SHORT_XFER_OK, NULL);
324 324
325 err = usbd_sync_transfer(xfer); 325 err = usbd_sync_transfer(xfer);
326 326
327 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 327 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
328 328
329#ifdef ATU_DEBUG 329#ifdef ATU_DEBUG
330 if (atudebug) { 330 if (atudebug) {
331 if (type & UT_READ) { 331 if (type & UT_READ) {
332 DPRINTFN(20, ("%s: transfered 0x%x bytes in\n", 332 DPRINTFN(20, ("%s: transfered 0x%x bytes in\n",
333 device_xname(sc->atu_dev), total_len)); 333 device_xname(sc->atu_dev), total_len));
334 } else { 334 } else {
335 if (total_len != length) 335 if (total_len != length)
336 DPRINTF(("%s: wrote only %x bytes\n", 336 DPRINTF(("%s: wrote only %x bytes\n",
337 device_xname(sc->atu_dev), total_len)); 337 device_xname(sc->atu_dev), total_len));
338 } 338 }
339 } 339 }
340#endif /* ATU_DEBUG */ 340#endif /* ATU_DEBUG */
341 341
342 usbd_destroy_xfer(xfer); 342 usbd_destroy_xfer(xfer);
343 343
344 splx(s); 344 splx(s);
345 return err; 345 return err;
346} 346}
347 347
348int 348int
349atu_send_command(struct atu_softc *sc, uint8_t *command, int size) 349atu_send_command(struct atu_softc *sc, uint8_t *command, int size)
350{ 350{
351 return atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000, 351 return atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000,
352 0x0000, size, command); 352 0x0000, size, command);
353} 353}
354 354
355int 355int
356atu_get_cmd_status(struct atu_softc *sc, uint8_t cmd, uint8_t *status) 356atu_get_cmd_status(struct atu_softc *sc, uint8_t cmd, uint8_t *status)
357{ 357{
358 /* 358 /*
359 * all other drivers (including Windoze) request 40 bytes of status 359 * all other drivers (including Windoze) request 40 bytes of status
360 * and get a short-xfer of just 6 bytes. we can save 34 bytes of 360 * and get a short-xfer of just 6 bytes. we can save 34 bytes of
361 * buffer if we just request those 6 bytes in the first place :) 361 * buffer if we just request those 6 bytes in the first place :)
362 */ 362 */
363 /* 363 /*
364 return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd, 364 return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd,
365 0x0000, 40, status); 365 0x0000, 40, status);
366 */ 366 */
367 return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd, 367 return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x22, cmd,
368 0x0000, 6, status); 368 0x0000, 6, status);
369} 369}
370 370
371int 371int
372atu_wait_completion(struct atu_softc *sc, uint8_t cmd, uint8_t *status) 372atu_wait_completion(struct atu_softc *sc, uint8_t cmd, uint8_t *status)
373{ 373{
374 int idle_count = 0, err; 374 int idle_count = 0, err;
375 uint8_t statusreq[6]; 375 uint8_t statusreq[6];
376 376
377 DPRINTFN(15, ("%s: wait-completion: cmd=%02x\n", 377 DPRINTFN(15, ("%s: wait-completion: cmd=%02x\n",
378 device_xname(sc->atu_dev), cmd)); 378 device_xname(sc->atu_dev), cmd));
379 379
380 while (1) { 380 while (1) {
381 err = atu_get_cmd_status(sc, cmd, statusreq); 381 err = atu_get_cmd_status(sc, cmd, statusreq);
382 if (err) 382 if (err)
383 return err; 383 return err;
384 384
385#ifdef ATU_DEBUG 385#ifdef ATU_DEBUG
386 if (atudebug) { 386 if (atudebug) {
387 DPRINTFN(20, ("%s: status=%s cmd=%02x\n", 387 DPRINTFN(20, ("%s: status=%s cmd=%02x\n",
388 device_xname(sc->atu_dev), 388 device_xname(sc->atu_dev),
389 ether_sprintf(statusreq), cmd)); 389 ether_sprintf(statusreq), cmd));
390 } 390 }
391#endif /* ATU_DEBUG */ 391#endif /* ATU_DEBUG */
392 392
393 /* 393 /*
394 * during normal operations waiting on STATUS_IDLE 394 * during normal operations waiting on STATUS_IDLE
395 * will never happen more than once 395 * will never happen more than once
396 */ 396 */
397 if ((statusreq[5] == STATUS_IDLE) && (idle_count++ > 20)) { 397 if ((statusreq[5] == STATUS_IDLE) && (idle_count++ > 20)) {
398 DPRINTF(("%s: idle_count > 20!\n", 398 DPRINTF(("%s: idle_count > 20!\n",
399 device_xname(sc->atu_dev))); 399 device_xname(sc->atu_dev)));
400 return 0; 400 return 0;
401 } 401 }
402 402
403 if ((statusreq[5] != STATUS_IN_PROGRESS) && 403 if ((statusreq[5] != STATUS_IN_PROGRESS) &&
404 (statusreq[5] != STATUS_IDLE)) { 404 (statusreq[5] != STATUS_IDLE)) {
405 if (status != NULL) 405 if (status != NULL)
406 *status = statusreq[5]; 406 *status = statusreq[5];
407 return 0; 407 return 0;
408 } 408 }
409 usbd_delay_ms(sc->atu_udev, 25); 409 usbd_delay_ms(sc->atu_udev, 25);
410 } 410 }
411} 411}
412 412
413int 413int
414atu_send_mib(struct atu_softc *sc, uint8_t type, uint8_t size, 414atu_send_mib(struct atu_softc *sc, uint8_t type, uint8_t size,
415 uint8_t index, void *data) 415 uint8_t index, void *data)
416{ 416{
417 int err; 417 int err;
418 struct atu_cmd_set_mib request; 418 struct atu_cmd_set_mib request;
419 419
420 /* 420 /*
421 * We don't construct a MIB packet first and then memcpy it into an 421 * We don't construct a MIB packet first and then memcpy it into an
422 * Atmel-command-packet, we just construct it the right way at once :) 422 * Atmel-command-packet, we just construct it the right way at once :)
423 */ 423 */
424 424
425 memset(&request, 0, sizeof(request)); 425 memset(&request, 0, sizeof(request));
426 426
427 request.AtCmd = CMD_SET_MIB; 427 request.AtCmd = CMD_SET_MIB;
428 USETW(request.AtSize, size + 4); 428 USETW(request.AtSize, size + 4);
429 429
430 request.MIBType = type; 430 request.MIBType = type;
431 request.MIBSize = size; 431 request.MIBSize = size;
432 request.MIBIndex = index; 432 request.MIBIndex = index;
433 request.MIBReserved = 0; 433 request.MIBReserved = 0;
434 434
435 /* 435 /*
436 * For 1 and 2 byte requests we assume a direct value, 436 * For 1 and 2 byte requests we assume a direct value,
437 * everything bigger than 2 bytes we assume a pointer to the data 437 * everything bigger than 2 bytes we assume a pointer to the data
438 */ 438 */
439 switch (size) { 439 switch (size) {
440 case 0: 440 case 0:
441 break; 441 break;
442 case 1: 442 case 1:
443 request.data[0]=(long)data & 0x000000ff; 443 request.data[0]=(long)data & 0x000000ff;
444 break; 444 break;
445 case 2: 445 case 2:
446 request.data[0]=(long)data & 0x000000ff; 446 request.data[0]=(long)data & 0x000000ff;
447 request.data[1]=(long)data >> 8; 447 request.data[1]=(long)data >> 8;
448 break; 448 break;
449 default: 449 default:
450 memcpy(request.data, data, size); 450 memcpy(request.data, data, size);
451 break; 451 break;
452 } 452 }
453 453
454 err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000, 454 err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0000,
455 0x0000, size+8, (uByte *)&request); 455 0x0000, size+8, (uByte *)&request);
456 if (err) 456 if (err)
457 return err; 457 return err;
458 458
459 DPRINTFN(15, ("%s: sendmib : waitcompletion...\n", 459 DPRINTFN(15, ("%s: sendmib : waitcompletion...\n",
460 device_xname(sc->atu_dev))); 460 device_xname(sc->atu_dev)));
461 return atu_wait_completion(sc, CMD_SET_MIB, NULL); 461 return atu_wait_completion(sc, CMD_SET_MIB, NULL);
462} 462}
463 463
464int 464int
465atu_get_mib(struct atu_softc *sc, uint8_t type, uint8_t size, 465atu_get_mib(struct atu_softc *sc, uint8_t type, uint8_t size,
466 uint8_t index, uint8_t *buf) 466 uint8_t index, uint8_t *buf)
467{ 467{
468 468
469 /* linux/at76c503.c - 478 */ 469 /* linux/at76c503.c - 478 */
470 return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x033, 470 return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x033,
471 type << 8, index, size, buf); 471 type << 8, index, size, buf);
472} 472}
473 473
474#if 0 474#if 0
475int 475int
476atu_start_ibss(struct atu_softc *sc) 476atu_start_ibss(struct atu_softc *sc)
477{ 477{
478 struct ieee80211com *ic = &sc->sc_ic; 478 struct ieee80211com *ic = &sc->sc_ic;
479 int err; 479 int err;
480 struct atu_cmd_start_ibss Request; 480 struct atu_cmd_start_ibss Request;
481 481
482 Request.Cmd = CMD_START_IBSS; 482 Request.Cmd = CMD_START_IBSS;
483 Request.Reserved = 0; 483 Request.Reserved = 0;
484 Request.Size = sizeof(Request) - 4; 484 Request.Size = sizeof(Request) - 4;
485 485
486 memset(Request.BSSID, 0x00, sizeof(Request.BSSID)); 486 memset(Request.BSSID, 0x00, sizeof(Request.BSSID));
487 memset(Request.SSID, 0x00, sizeof(Request.SSID)); 487 memset(Request.SSID, 0x00, sizeof(Request.SSID));
488 memcpy(Request.SSID, ic->ic_des_ssid, ic->ic_des_ssidlen); 488 memcpy(Request.SSID, ic->ic_des_ssid, ic->ic_des_ssidlen);
489 Request.SSIDSize = ic->ic_des_ssidlen; 489 Request.SSIDSize = ic->ic_des_ssidlen;
490 if (sc->atu_desired_channel != IEEE80211_CHAN_ANY) 490 if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
491 Request.Channel = (uint8_t)sc->atu_desired_channel; 491 Request.Channel = (uint8_t)sc->atu_desired_channel;
492 else 492 else
493 Request.Channel = ATU_DEFAULT_CHANNEL; 493 Request.Channel = ATU_DEFAULT_CHANNEL;
494 Request.BSSType = AD_HOC_MODE; 494 Request.BSSType = AD_HOC_MODE;
495 memset(Request.Res, 0x00, sizeof(Request.Res)); 495 memset(Request.Res, 0x00, sizeof(Request.Res));
496 496
497 /* Write config to adapter */ 497 /* Write config to adapter */
498 err = atu_send_command(sc, (uint8_t *)&Request, sizeof(Request)); 498 err = atu_send_command(sc, (uint8_t *)&Request, sizeof(Request));
499 if (err) { 499 if (err) {
500 DPRINTF(("%s: start ibss failed!\n", 500 DPRINTF(("%s: start ibss failed!\n",
501 device_xname(sc->atu_dev))); 501 device_xname(sc->atu_dev)));
502 return err; 502 return err;
503 } 503 }
504 504
505 /* Wait for the adapter to do its thing */ 505 /* Wait for the adapter to do its thing */
506 err = atu_wait_completion(sc, CMD_START_IBSS, NULL); 506 err = atu_wait_completion(sc, CMD_START_IBSS, NULL);
507 if (err) { 507 if (err) {
508 DPRINTF(("%s: error waiting for start_ibss\n", 508 DPRINTF(("%s: error waiting for start_ibss\n",
509 device_xname(sc->atu_dev))); 509 device_xname(sc->atu_dev)));
510 return err; 510 return err;
511 } 511 }
512 512
513 /* Get the current BSSID */ 513 /* Get the current BSSID */
514 err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, sc->atu_bssid); 514 err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, sc->atu_bssid);
515 if (err) { 515 if (err) {
516 DPRINTF(("%s: could not get BSSID!\n", 516 DPRINTF(("%s: could not get BSSID!\n",
517 device_xname(sc->atu_dev))); 517 device_xname(sc->atu_dev)));
518 return err; 518 return err;
519 } 519 }
520 520
521 DPRINTF(("%s: started a new IBSS (BSSID=%s)\n", 521 DPRINTF(("%s: started a new IBSS (BSSID=%s)\n",
522 device_xname(sc->atu_dev), ether_sprintf(sc->atu_bssid))); 522 device_xname(sc->atu_dev), ether_sprintf(sc->atu_bssid)));
523 return 0; 523 return 0;
524} 524}
525#endif 525#endif
526 526
527int 527int
528atu_start_scan(struct atu_softc *sc) 528atu_start_scan(struct atu_softc *sc)
529{ 529{
530 struct ieee80211com *ic = &sc->sc_ic; 530 struct ieee80211com *ic = &sc->sc_ic;
531 struct atu_cmd_do_scan Scan; 531 struct atu_cmd_do_scan Scan;
532 usbd_status err; 532 usbd_status err;
533 int Cnt; 533 int Cnt;
534 534
535 memset(&Scan, 0, sizeof(Scan)); 535 memset(&Scan, 0, sizeof(Scan));
536 536
537 Scan.Cmd = CMD_START_SCAN; 537 Scan.Cmd = CMD_START_SCAN;
538 Scan.Reserved = 0; 538 Scan.Reserved = 0;
539 USETW(Scan.Size, sizeof(Scan) - 4); 539 USETW(Scan.Size, sizeof(Scan) - 4);
540 540
541 /* use the broadcast BSSID (in active scan) */ 541 /* use the broadcast BSSID (in active scan) */
542 for (Cnt=0; Cnt<6; Cnt++) 542 for (Cnt=0; Cnt<6; Cnt++)
543 Scan.BSSID[Cnt] = 0xff; 543 Scan.BSSID[Cnt] = 0xff;
544 544
545 memset(Scan.SSID, 0x00, sizeof(Scan.SSID)); 545 memset(Scan.SSID, 0x00, sizeof(Scan.SSID));
546 memcpy(Scan.SSID, ic->ic_des_essid, ic->ic_des_esslen); 546 memcpy(Scan.SSID, ic->ic_des_essid, ic->ic_des_esslen);
547 Scan.SSID_Len = ic->ic_des_esslen; 547 Scan.SSID_Len = ic->ic_des_esslen;
548 548
549 /* default values for scan */ 549 /* default values for scan */
550 Scan.ScanType = ATU_SCAN_ACTIVE; 550 Scan.ScanType = ATU_SCAN_ACTIVE;
551 if (sc->atu_desired_channel != IEEE80211_CHAN_ANY) 551 if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
552 Scan.Channel = (uint8_t)sc->atu_desired_channel; 552 Scan.Channel = (uint8_t)sc->atu_desired_channel;
553 else 553 else
554 Scan.Channel = sc->atu_channel; 554 Scan.Channel = sc->atu_channel;
555 555
556 ic->ic_curchan = &ic->ic_channels[Scan.Channel]; 556 ic->ic_curchan = &ic->ic_channels[Scan.Channel];
557 557
558 /* we like scans to be quick :) */ 558 /* we like scans to be quick :) */
559 /* the time we wait before sending probe's */ 559 /* the time we wait before sending probe's */
560 USETW(Scan.ProbeDelay, 0); 560 USETW(Scan.ProbeDelay, 0);
561 /* the time we stay on one channel */ 561 /* the time we stay on one channel */
562 USETW(Scan.MinChannelTime, 100); 562 USETW(Scan.MinChannelTime, 100);
563 USETW(Scan.MaxChannelTime, 200); 563 USETW(Scan.MaxChannelTime, 200);
564 /* whether or not we scan all channels */ 564 /* whether or not we scan all channels */
565 Scan.InternationalScan = 0xc1; 565 Scan.InternationalScan = 0xc1;
566 566
567#ifdef ATU_DEBUG 567#ifdef ATU_DEBUG
568 if (atudebug) { 568 if (atudebug) {
569 DPRINTFN(20, ("%s: scan cmd len=%02zx\n", 569 DPRINTFN(20, ("%s: scan cmd len=%02zx\n",
570 device_xname(sc->atu_dev), sizeof(Scan))); 570 device_xname(sc->atu_dev), sizeof(Scan)));
571 } 571 }
572#endif /* ATU_DEBUG */ 572#endif /* ATU_DEBUG */
573 573
574 /* Write config to adapter */ 574 /* Write config to adapter */
575 err = atu_send_command(sc, (uint8_t *)&Scan, sizeof(Scan)); 575 err = atu_send_command(sc, (uint8_t *)&Scan, sizeof(Scan));
576 if (err) 576 if (err)
577 return err; 577 return err;
578 578
579 /* 579 /*
580 * We don't wait for the command to finish... the mgmt-thread will do 580 * We don't wait for the command to finish... the mgmt-thread will do
581 * that for us 581 * that for us
582 */ 582 */
583 /* 583 /*
584 err = atu_wait_completion(sc, CMD_START_SCAN, NULL); 584 err = atu_wait_completion(sc, CMD_START_SCAN, NULL);
585 if (err) 585 if (err)
586 return err; 586 return err;
587 */ 587 */
588 return 0; 588 return 0;
589} 589}
590 590
591int 591int
592atu_switch_radio(struct atu_softc *sc, int state) 592atu_switch_radio(struct atu_softc *sc, int state)
593{ 593{
594 usbd_status err; 594 usbd_status err;
595 struct atu_cmd CmdRadio; 595 struct atu_cmd CmdRadio;
596 596
597 if (sc->atu_radio == RadioIntersil) { 597 if (sc->atu_radio == RadioIntersil) {
598 /* 598 /*
599 * Intersil doesn't seem to need/support switching the radio 599 * Intersil doesn't seem to need/support switching the radio
600 * on/off 600 * on/off
601 */ 601 */
602 return 0; 602 return 0;
603 } 603 }
604 604
605 memset(&CmdRadio, 0, sizeof(CmdRadio)); 605 memset(&CmdRadio, 0, sizeof(CmdRadio));
606 CmdRadio.Cmd = CMD_RADIO_ON; 606 CmdRadio.Cmd = CMD_RADIO_ON;
607 607
608 if (sc->atu_radio_on != state) { 608 if (sc->atu_radio_on != state) {
609 if (state == 0) 609 if (state == 0)
610 CmdRadio.Cmd = CMD_RADIO_OFF; 610 CmdRadio.Cmd = CMD_RADIO_OFF;
611 611
612 err = atu_send_command(sc, (uint8_t *)&CmdRadio, 612 err = atu_send_command(sc, (uint8_t *)&CmdRadio,
613 sizeof(CmdRadio)); 613 sizeof(CmdRadio));
614 if (err) 614 if (err)
615 return err; 615 return err;
616 616
617 err = atu_wait_completion(sc, CmdRadio.Cmd, NULL); 617 err = atu_wait_completion(sc, CmdRadio.Cmd, NULL);
618 if (err) 618 if (err)
619 return err; 619 return err;
620 620
621 DPRINTFN(10, ("%s: radio turned %s\n", 621 DPRINTFN(10, ("%s: radio turned %s\n",
622 device_xname(sc->atu_dev), state ? "on" : "off")); 622 device_xname(sc->atu_dev), state ? "on" : "off"));
623 sc->atu_radio_on = state; 623 sc->atu_radio_on = state;
624 } 624 }
625 return 0; 625 return 0;
626} 626}
627 627
628int 628int
629atu_initial_config(struct atu_softc *sc) 629atu_initial_config(struct atu_softc *sc)
630{ 630{
631 struct ieee80211com *ic = &sc->sc_ic; 631 struct ieee80211com *ic = &sc->sc_ic;
632 uint32_t i; 632 uint32_t i;
633 usbd_status err; 633 usbd_status err;
634/* uint8_t rates[4] = {0x82, 0x84, 0x8B, 0x96};*/ 634/* uint8_t rates[4] = {0x82, 0x84, 0x8B, 0x96};*/
635 uint8_t rates[4] = {0x82, 0x04, 0x0B, 0x16}; 635 uint8_t rates[4] = {0x82, 0x04, 0x0B, 0x16};
636 struct atu_cmd_card_config cmd; 636 struct atu_cmd_card_config cmd;
637 uint8_t reg_domain; 637 uint8_t reg_domain;
638 638
639 DPRINTFN(10, ("%s: sending mac-addr\n", device_xname(sc->atu_dev))); 639 DPRINTFN(10, ("%s: sending mac-addr\n", device_xname(sc->atu_dev)));
640 err = atu_send_mib(sc, MIB_MAC_ADDR__ADDR, ic->ic_myaddr); 640 err = atu_send_mib(sc, MIB_MAC_ADDR__ADDR, ic->ic_myaddr);
641 if (err) { 641 if (err) {
642 DPRINTF(("%s: error setting mac-addr\n", 642 DPRINTF(("%s: error setting mac-addr\n",
643 device_xname(sc->atu_dev))); 643 device_xname(sc->atu_dev)));
644 return err; 644 return err;
645 } 645 }
646 646
647 /* 647 /*
648 DPRINTF(("%s: sending reg-domain\n", device_xname(sc->atu_dev))); 648 DPRINTF(("%s: sending reg-domain\n", device_xname(sc->atu_dev)));
649 err = atu_send_mib(sc, MIB_PHY__REG_DOMAIN, NR(0x30)); 649 err = atu_send_mib(sc, MIB_PHY__REG_DOMAIN, NR(0x30));
650 if (err) { 650 if (err) {
651 DPRINTF(("%s: error setting mac-addr\n", 651 DPRINTF(("%s: error setting mac-addr\n",
652 device_xname(sc->atu_dev))); 652 device_xname(sc->atu_dev)));
653 return err; 653 return err;
654 } 654 }
655 */ 655 */
656 656
657 memset(&cmd, 0, sizeof(cmd)); 657 memset(&cmd, 0, sizeof(cmd));
658 cmd.Cmd = CMD_STARTUP; 658 cmd.Cmd = CMD_STARTUP;
659 cmd.Reserved = 0; 659 cmd.Reserved = 0;
660 USETW(cmd.Size, sizeof(cmd) - 4); 660 USETW(cmd.Size, sizeof(cmd) - 4);
661 661
662 if (sc->atu_desired_channel != IEEE80211_CHAN_ANY) 662 if (sc->atu_desired_channel != IEEE80211_CHAN_ANY)
663 cmd.Channel = (uint8_t)sc->atu_desired_channel; 663 cmd.Channel = (uint8_t)sc->atu_desired_channel;
664 else 664 else
665 cmd.Channel = sc->atu_channel; 665 cmd.Channel = sc->atu_channel;
666 cmd.AutoRateFallback = 1; 666 cmd.AutoRateFallback = 1;
667 memcpy(cmd.BasicRateSet, rates, 4); 667 memcpy(cmd.BasicRateSet, rates, 4);
668 668
669 /* ShortRetryLimit should be 7 according to 802.11 spec */ 669 /* ShortRetryLimit should be 7 according to 802.11 spec */
670 cmd.ShortRetryLimit = 7; 670 cmd.ShortRetryLimit = 7;
671 USETW(cmd.RTS_Threshold, 2347); 671 USETW(cmd.RTS_Threshold, 2347);
672 USETW(cmd.FragThreshold, 2346); 672 USETW(cmd.FragThreshold, 2346);
673 673
674 /* Doesn't seem to work, but we'll set it to 1 anyway */ 674 /* Doesn't seem to work, but we'll set it to 1 anyway */
675 cmd.PromiscuousMode = 1; 675 cmd.PromiscuousMode = 1;
676 676
677 /* this goes into the beacon we transmit */ 677 /* this goes into the beacon we transmit */
678 if (ic->ic_flags & IEEE80211_F_PRIVACY) 678 if (ic->ic_flags & IEEE80211_F_PRIVACY)
679 cmd.PrivacyInvoked = 1; 679 cmd.PrivacyInvoked = 1;
680 else 680 else
681 cmd.PrivacyInvoked = 0; 681 cmd.PrivacyInvoked = 0;
682 682
683 cmd.ExcludeUnencrypted = 0; 683 cmd.ExcludeUnencrypted = 0;
684 684
685 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 685 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
686 switch (ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen) { 686 switch (ic->ic_nw_keys[ic->ic_def_txkey].wk_keylen) {
687 case 5: 687 case 5:
688 cmd.EncryptionType = ATU_WEP_40BITS; 688 cmd.EncryptionType = ATU_WEP_40BITS;
689 break; 689 break;
690 case 13: 690 case 13:
691 cmd.EncryptionType = ATU_WEP_104BITS; 691 cmd.EncryptionType = ATU_WEP_104BITS;
692 break; 692 break;
693 default: 693 default:
694 cmd.EncryptionType = ATU_WEP_OFF; 694 cmd.EncryptionType = ATU_WEP_OFF;
695 break; 695 break;
696 } 696 }
697 697
698 698
699 cmd.WEP_DefaultKeyID = ic->ic_def_txkey; 699 cmd.WEP_DefaultKeyID = ic->ic_def_txkey;
700 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 700 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
701 memcpy(cmd.WEP_DefaultKey[i], ic->ic_nw_keys[i].wk_key, 701 memcpy(cmd.WEP_DefaultKey[i], ic->ic_nw_keys[i].wk_key,
702 ic->ic_nw_keys[i].wk_keylen); 702 ic->ic_nw_keys[i].wk_keylen);
703 } 703 }
704 } 704 }
705 705
706 /* Setting the SSID here doesn't seem to do anything */ 706 /* Setting the SSID here doesn't seem to do anything */
707 memset(cmd.SSID, 0x00, sizeof(cmd.SSID)); 707 memset(cmd.SSID, 0x00, sizeof(cmd.SSID));
708 memcpy(cmd.SSID, ic->ic_des_essid, ic->ic_des_esslen); 708 memcpy(cmd.SSID, ic->ic_des_essid, ic->ic_des_esslen);
709 cmd.SSID_Len = ic->ic_des_esslen; 709 cmd.SSID_Len = ic->ic_des_esslen;
710 710
711 cmd.ShortPreamble = 0; 711 cmd.ShortPreamble = 0;
712 USETW(cmd.BeaconPeriod, 100); 712 USETW(cmd.BeaconPeriod, 100);
713 /* cmd.BeaconPeriod = 65535; */ 713 /* cmd.BeaconPeriod = 65535; */
714 714
715 /* 715 /*
716 * TODO: 716 * TODO:
717 * read reg domain MIB_PHY @ 0x17 (1 byte), (reply = 0x30) 717 * read reg domain MIB_PHY @ 0x17 (1 byte), (reply = 0x30)
718 * we should do something useful with this info. right now it's just 718 * we should do something useful with this info. right now it's just
719 * ignored 719 * ignored
720 */ 720 */
721 err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, &reg_domain); 721 err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, &reg_domain);
722 if (err) { 722 if (err) {
723 DPRINTF(("%s: could not get regdomain!\n", 723 DPRINTF(("%s: could not get regdomain!\n",
724 device_xname(sc->atu_dev))); 724 device_xname(sc->atu_dev)));
725 } else { 725 } else {
726 DPRINTF(("%s: in reg domain 0x%x according to the " 726 DPRINTF(("%s: in reg domain 0x%x according to the "
727 "adapter\n", device_xname(sc->atu_dev), reg_domain)); 727 "adapter\n", device_xname(sc->atu_dev), reg_domain));
728 } 728 }
729 729
730#ifdef ATU_DEBUG 730#ifdef ATU_DEBUG
731 if (atudebug) { 731 if (atudebug) {
732 DPRINTFN(20, ("%s: configlen=%02zx\n", 732 DPRINTFN(20, ("%s: configlen=%02zx\n",
733 device_xname(sc->atu_dev), sizeof(cmd))); 733 device_xname(sc->atu_dev), sizeof(cmd)));
734 } 734 }
735#endif /* ATU_DEBUG */ 735#endif /* ATU_DEBUG */
736 736
737 /* Windoze : driver says exclude-unencrypted=1 & encr-type=1 */ 737 /* Windoze : driver says exclude-unencrypted=1 & encr-type=1 */
738 738
739 err = atu_send_command(sc, (uint8_t *)&cmd, sizeof(cmd)); 739 err = atu_send_command(sc, (uint8_t *)&cmd, sizeof(cmd));
740 if (err) 740 if (err)
741 return err; 741 return err;
742 err = atu_wait_completion(sc, CMD_STARTUP, NULL); 742 err = atu_wait_completion(sc, CMD_STARTUP, NULL);
743 if (err) 743 if (err)
744 return err; 744 return err;
745 745
746 /* Turn on radio now */ 746 /* Turn on radio now */
747 err = atu_switch_radio(sc, 1); 747 err = atu_switch_radio(sc, 1);
748 if (err) 748 if (err)
749 return err; 749 return err;
750 750
751 /* preamble type = short */ 751 /* preamble type = short */
752 err = atu_send_mib(sc, MIB_LOCAL__PREAMBLE, NR(PREAMBLE_SHORT)); 752 err = atu_send_mib(sc, MIB_LOCAL__PREAMBLE, NR(PREAMBLE_SHORT));
753 if (err) 753 if (err)
754 return err; 754 return err;
755 755
756 /* frag = 1536 */ 756 /* frag = 1536 */
757 err = atu_send_mib(sc, MIB_MAC__FRAG, NR(2346)); 757 err = atu_send_mib(sc, MIB_MAC__FRAG, NR(2346));
758 if (err) 758 if (err)
759 return err; 759 return err;
760 760
761 /* rts = 1536 */ 761 /* rts = 1536 */
762 err = atu_send_mib(sc, MIB_MAC__RTS, NR(2347)); 762 err = atu_send_mib(sc, MIB_MAC__RTS, NR(2347));
763 if (err) 763 if (err)
764 return err; 764 return err;
765 765
766 /* auto rate fallback = 1 */ 766 /* auto rate fallback = 1 */
767 err = atu_send_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, NR(1)); 767 err = atu_send_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, NR(1));
768 if (err) 768 if (err)
769 return err; 769 return err;
770 770
771 /* power mode = full on, no power saving */ 771 /* power mode = full on, no power saving */
772 err = atu_send_mib(sc, MIB_MAC_MGMT__POWER_MODE, 772 err = atu_send_mib(sc, MIB_MAC_MGMT__POWER_MODE,
773 NR(POWER_MODE_ACTIVE)); 773 NR(POWER_MODE_ACTIVE));
774 if (err) 774 if (err)
775 return err; 775 return err;
776 776
777 DPRINTFN(10, ("%s: completed initial config\n", 777 DPRINTFN(10, ("%s: completed initial config\n",
778 device_xname(sc->atu_dev))); 778 device_xname(sc->atu_dev)));
779 return 0; 779 return 0;
780} 780}
781 781
782int 782int
783atu_join(struct atu_softc *sc, struct ieee80211_node *node) 783atu_join(struct atu_softc *sc, struct ieee80211_node *node)
784{ 784{
785 struct atu_cmd_join join; 785 struct atu_cmd_join join;
786 uint8_t status = 0; /* XXX: GCC */ 786 uint8_t status = 0; /* XXX: GCC */
787 usbd_status err; 787 usbd_status err;
788 788
789 memset(&join, 0, sizeof(join)); 789 memset(&join, 0, sizeof(join));
790 790
791 join.Cmd = CMD_JOIN; 791 join.Cmd = CMD_JOIN;
792 join.Reserved = 0x00; 792 join.Reserved = 0x00;
793 USETW(join.Size, sizeof(join) - 4); 793 USETW(join.Size, sizeof(join) - 4);
794 794
795 DPRINTFN(15, ("%s: pre-join sc->atu_bssid=%s\n", 795 DPRINTFN(15, ("%s: pre-join sc->atu_bssid=%s\n",
796 device_xname(sc->atu_dev), ether_sprintf(sc->atu_bssid))); 796 device_xname(sc->atu_dev), ether_sprintf(sc->atu_bssid)));
797 DPRINTFN(15, ("%s: mode=%d\n", device_xname(sc->atu_dev), 797 DPRINTFN(15, ("%s: mode=%d\n", device_xname(sc->atu_dev),
798 sc->atu_mode)); 798 sc->atu_mode));
799 memcpy(join.bssid, node->ni_bssid, IEEE80211_ADDR_LEN); 799 memcpy(join.bssid, node->ni_bssid, IEEE80211_ADDR_LEN);
800 memset(join.essid, 0x00, 32); 800 memset(join.essid, 0x00, 32);
801 memcpy(join.essid, node->ni_essid, node->ni_esslen); 801 memcpy(join.essid, node->ni_essid, node->ni_esslen);
802 join.essid_size = node->ni_esslen; 802 join.essid_size = node->ni_esslen;
803 if (node->ni_capinfo & IEEE80211_CAPINFO_IBSS) 803 if (node->ni_capinfo & IEEE80211_CAPINFO_IBSS)
804 join.bss_type = AD_HOC_MODE; 804 join.bss_type = AD_HOC_MODE;
805 else 805 else
806 join.bss_type = INFRASTRUCTURE_MODE; 806 join.bss_type = INFRASTRUCTURE_MODE;
807 join.channel = ieee80211_chan2ieee(&sc->sc_ic, node->ni_chan); 807 join.channel = ieee80211_chan2ieee(&sc->sc_ic, node->ni_chan);
808 808
809 USETW(join.timeout, ATU_JOIN_TIMEOUT); 809 USETW(join.timeout, ATU_JOIN_TIMEOUT);
810 join.reserved = 0x00; 810 join.reserved = 0x00;
811 811
812 DPRINTFN(10, ("%s: trying to join BSSID=%s\n", 812 DPRINTFN(10, ("%s: trying to join BSSID=%s\n",
813 device_xname(sc->atu_dev), ether_sprintf(join.bssid))); 813 device_xname(sc->atu_dev), ether_sprintf(join.bssid)));
814 err = atu_send_command(sc, (uint8_t *)&join, sizeof(join)); 814 err = atu_send_command(sc, (uint8_t *)&join, sizeof(join));
815 if (err) { 815 if (err) {
816 DPRINTF(("%s: ERROR trying to join IBSS\n", 816 DPRINTF(("%s: ERROR trying to join IBSS\n",
817 device_xname(sc->atu_dev))); 817 device_xname(sc->atu_dev)));
818 return err; 818 return err;
819 } 819 }
820 err = atu_wait_completion(sc, CMD_JOIN, &status); 820 err = atu_wait_completion(sc, CMD_JOIN, &status);
821 if (err) { 821 if (err) {
822 DPRINTF(("%s: error joining BSS!\n", 822 DPRINTF(("%s: error joining BSS!\n",
823 device_xname(sc->atu_dev))); 823 device_xname(sc->atu_dev)));
824 return err; 824 return err;
825 } 825 }
826 if (status != STATUS_COMPLETE) { 826 if (status != STATUS_COMPLETE) {
827 DPRINTF(("%s: error joining... [status=%02x]\n", 827 DPRINTF(("%s: error joining... [status=%02x]\n",
828 device_xname(sc->atu_dev), status)); 828 device_xname(sc->atu_dev), status));
829 return status; 829 return status;
830 } else { 830 } else {
831 DPRINTFN(10, ("%s: joined BSS\n", device_xname(sc->atu_dev))); 831 DPRINTFN(10, ("%s: joined BSS\n", device_xname(sc->atu_dev)));
832 } 832 }
833 return err; 833 return err;
834} 834}
835 835
836/* 836/*
837 * Get the state of the DFU unit 837 * Get the state of the DFU unit
838 */ 838 */
839int8_t 839int8_t
840atu_get_dfu_state(struct atu_softc *sc) 840atu_get_dfu_state(struct atu_softc *sc)
841{ 841{
842 uint8_t state; 842 uint8_t state;
843 843
844 if (atu_usb_request(sc, DFU_GETSTATE, 0, 0, 1, &state)) 844 if (atu_usb_request(sc, DFU_GETSTATE, 0, 0, 1, &state))
845 return -1; 845 return -1;
846 return state; 846 return state;
847} 847}
848 848
849/* 849/*
850 * Get MAC opmode 850 * Get MAC opmode
851 */ 851 */
852uint8_t 852uint8_t
853atu_get_opmode(struct atu_softc *sc, uint8_t *mode) 853atu_get_opmode(struct atu_softc *sc, uint8_t *mode)
854{ 854{
855 855
856 return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33, 0x0001, 856 return atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33, 0x0001,
857 0x0000, 1, mode); 857 0x0000, 1, mode);
858} 858}
859 859
860/* 860/*
861 * Upload the internal firmware into the device 861 * Upload the internal firmware into the device
862 */ 862 */
863void 863void
864atu_internal_firmware(device_t arg) 864atu_internal_firmware(device_t arg)
865{ 865{
866 struct atu_softc *sc = device_private(arg); 866 struct atu_softc *sc = device_private(arg);
867 u_char state, *ptr = NULL, *firm = NULL, status[6]; 867 u_char state, *ptr = NULL, *firm = NULL, status[6];
868 int block_size, block = 0, err, i; 868 int block_size, block = 0, err, i;
869 size_t bytes_left = 0; 869 size_t bytes_left = 0;
870 870
871 /* 871 /*
872 * Uploading firmware is done with the DFU (Device Firmware Upgrade) 872 * Uploading firmware is done with the DFU (Device Firmware Upgrade)
873 * interface. See "Universal Serial Bus - Device Class Specification 873 * interface. See "Universal Serial Bus - Device Class Specification
874 * for Device Firmware Upgrade" pdf for details of the protocol. 874 * for Device Firmware Upgrade" pdf for details of the protocol.
875 * Maybe this could be moved to a separate 'firmware driver' once more 875 * Maybe this could be moved to a separate 'firmware driver' once more
876 * device drivers need it... For now we'll just do it here. 876 * device drivers need it... For now we'll just do it here.
877 * 877 *
878 * Just for your information, the Atmel's DFU descriptor looks like 878 * Just for your information, the Atmel's DFU descriptor looks like
879 * this: 879 * this:
880 * 880 *
881 * 07 size 881 * 07 size
882 * 21 type 882 * 21 type
883 * 01 capabilities : only firmware download, need reset 883 * 01 capabilities : only firmware download, need reset
884 * after download 884 * after download
885 * 13 05 detach timeout : max 1299ms between DFU_DETACH and 885 * 13 05 detach timeout : max 1299ms between DFU_DETACH and
886 * reset 886 * reset
887 * 00 04 max bytes of firmware per transaction : 1024 887 * 00 04 max bytes of firmware per transaction : 1024
888 */ 888 */
889 889
890 /* Choose the right firmware for the device */ 890 /* Choose the right firmware for the device */
891 for (i = 0; i < __arraycount(atu_radfirm); i++) 891 for (i = 0; i < __arraycount(atu_radfirm); i++)
892 if (sc->atu_radio == atu_radfirm[i].atur_type) { 892 if (sc->atu_radio == atu_radfirm[i].atur_type) {
893 firm = atu_radfirm[i].atur_internal; 893 firm = atu_radfirm[i].atur_internal;
894 bytes_left = atu_radfirm[i].atur_internal_sz; 894 bytes_left = atu_radfirm[i].atur_internal_sz;
895 } 895 }
896 896
897 if (firm == NULL) { 897 if (firm == NULL) {
898 aprint_error_dev(arg, "no firmware found\n"); 898 aprint_error_dev(arg, "no firmware found\n");
899 return; 899 return;
900 } 900 }
901 901
902 ptr = firm; 902 ptr = firm;
903 state = atu_get_dfu_state(sc); 903 state = atu_get_dfu_state(sc);
904 904
905 while (block >= 0 && state > 0) { 905 while (block >= 0 && state > 0) {
906 switch (state) { 906 switch (state) {
907 case DFUState_DnLoadSync: 907 case DFUState_DnLoadSync:
908 /* get DFU status */ 908 /* get DFU status */
909 err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0 , 6, 909 err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0 , 6,
910 status); 910 status);
911 if (err) { 911 if (err) {
912 DPRINTF(("%s: dfu_getstatus failed!\n", 912 DPRINTF(("%s: dfu_getstatus failed!\n",
913 device_xname(sc->atu_dev))); 913 device_xname(sc->atu_dev)));
914 return; 914 return;
915 } 915 }
916 /* success means state => DnLoadIdle */ 916 /* success means state => DnLoadIdle */
917 state = DFUState_DnLoadIdle; 917 state = DFUState_DnLoadIdle;
918 continue; 918 continue;
919 break; 919 break;
920 920
921 case DFUState_DFUIdle: 921 case DFUState_DFUIdle:
922 case DFUState_DnLoadIdle: 922 case DFUState_DnLoadIdle:
923 if (bytes_left>=DFU_MaxBlockSize) 923 if (bytes_left>=DFU_MaxBlockSize)
924 block_size = DFU_MaxBlockSize; 924 block_size = DFU_MaxBlockSize;
925 else 925 else
926 block_size = bytes_left; 926 block_size = bytes_left;
927 DPRINTFN(15, ("%s: firmware block %d\n", 927 DPRINTFN(15, ("%s: firmware block %d\n",
928 device_xname(sc->atu_dev), block)); 928 device_xname(sc->atu_dev), block));
929 929
930 err = atu_usb_request(sc, DFU_DNLOAD, block++, 0, 930 err = atu_usb_request(sc, DFU_DNLOAD, block++, 0,
931 block_size, ptr); 931 block_size, ptr);
932 if (err) { 932 if (err) {
933 DPRINTF(("%s: dfu_dnload failed\n", 933 DPRINTF(("%s: dfu_dnload failed\n",
934 device_xname(sc->atu_dev))); 934 device_xname(sc->atu_dev)));
935 return; 935 return;
936 } 936 }
937 937
938 ptr += block_size; 938 ptr += block_size;
939 bytes_left -= block_size; 939 bytes_left -= block_size;
940 if (block_size == 0) 940 if (block_size == 0)
941 block = -1; 941 block = -1;
942 break; 942 break;
943 943
944 default: 944 default:
945 usbd_delay_ms(sc->atu_udev, 100); 945 usbd_delay_ms(sc->atu_udev, 100);
946 DPRINTFN(20, ("%s: sleeping for a while\n", 946 DPRINTFN(20, ("%s: sleeping for a while\n",
947 device_xname(sc->atu_dev))); 947 device_xname(sc->atu_dev)));
948 break; 948 break;
949 } 949 }
950 950
951 state = atu_get_dfu_state(sc); 951 state = atu_get_dfu_state(sc);
952 } 952 }
953 953
954 if (state != DFUState_ManifestSync) { 954 if (state != DFUState_ManifestSync) {
955 DPRINTF(("%s: state != manifestsync... eek!\n", 955 DPRINTF(("%s: state != manifestsync... eek!\n",
956 device_xname(sc->atu_dev))); 956 device_xname(sc->atu_dev)));
957 } 957 }
958 958
959 err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0, 6, status); 959 err = atu_usb_request(sc, DFU_GETSTATUS, 0, 0, 6, status);
960 if (err) { 960 if (err) {
961 DPRINTF(("%s: dfu_getstatus failed!\n", 961 DPRINTF(("%s: dfu_getstatus failed!\n",
962 device_xname(sc->atu_dev))); 962 device_xname(sc->atu_dev)));
963 return; 963 return;
964 } 964 }
965 965
966 DPRINTFN(15, ("%s: sending remap\n", device_xname(sc->atu_dev))); 966 DPRINTFN(15, ("%s: sending remap\n", device_xname(sc->atu_dev)));
967 err = atu_usb_request(sc, DFU_REMAP, 0, 0, 0, NULL); 967 err = atu_usb_request(sc, DFU_REMAP, 0, 0, 0, NULL);
968 if ((err) && !(sc->atu_quirk & ATU_QUIRK_NO_REMAP)) { 968 if ((err) && !(sc->atu_quirk & ATU_QUIRK_NO_REMAP)) {
969 DPRINTF(("%s: remap failed!\n", device_xname(sc->atu_dev))); 969 DPRINTF(("%s: remap failed!\n", device_xname(sc->atu_dev)));
970 return; 970 return;
971 } 971 }
972 972
973 /* after a lot of trying and measuring I found out the device needs 973 /* after a lot of trying and measuring I found out the device needs
974 * about 56 miliseconds after sending the remap command before 974 * about 56 miliseconds after sending the remap command before
975 * it's ready to communicate again. So we'll wait just a little bit 975 * it's ready to communicate again. So we'll wait just a little bit
976 * longer than that to be sure... 976 * longer than that to be sure...
977 */ 977 */
978 usbd_delay_ms(sc->atu_udev, 56+100); 978 usbd_delay_ms(sc->atu_udev, 56+100);
979 979
980 aprint_error_dev(arg, "reattaching after firmware upload\n"); 980 aprint_error_dev(arg, "reattaching after firmware upload\n");
981 usb_needs_reattach(sc->atu_udev); 981 usb_needs_reattach(sc->atu_udev);
982} 982}
983 983
984void 984void
985atu_external_firmware(device_t arg) 985atu_external_firmware(device_t arg)
986{ 986{
987 struct atu_softc *sc = device_private(arg); 987 struct atu_softc *sc = device_private(arg);
988 u_char *ptr = NULL, *firm = NULL; 988 u_char *ptr = NULL, *firm = NULL;
989 int block_size, block = 0, err, i; 989 int block_size, block = 0, err, i;
990 size_t bytes_left = 0; 990 size_t bytes_left = 0;
991 991
992 for (i = 0; i < __arraycount(atu_radfirm); i++) 992 for (i = 0; i < __arraycount(atu_radfirm); i++)
993 if (sc->atu_radio == atu_radfirm[i].atur_type) { 993 if (sc->atu_radio == atu_radfirm[i].atur_type) {
994 firm = atu_radfirm[i].atur_external; 994 firm = atu_radfirm[i].atur_external;
995 bytes_left = atu_radfirm[i].atur_external_sz; 995 bytes_left = atu_radfirm[i].atur_external_sz;
996 } 996 }
997 997
998 if (firm == NULL) { 998 if (firm == NULL) {
999 aprint_error_dev(arg, "no firmware found\n"); 999 aprint_error_dev(arg, "no firmware found\n");
1000 return; 1000 return;
1001 } 1001 }
1002 ptr = firm; 1002 ptr = firm;
1003 1003
1004 while (bytes_left) { 1004 while (bytes_left) {
1005 if (bytes_left > 1024) 1005 if (bytes_left > 1024)
1006 block_size = 1024; 1006 block_size = 1024;
1007 else 1007 else
1008 block_size = bytes_left; 1008 block_size = bytes_left;
1009 1009
1010 DPRINTFN(15, ("%s: block:%d size:%d\n", 1010 DPRINTFN(15, ("%s: block:%d size:%d\n",
1011 device_xname(sc->atu_dev), block, block_size)); 1011 device_xname(sc->atu_dev), block, block_size));
1012 err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 1012 err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e,
1013 0x0802, block, block_size, ptr); 1013 0x0802, block, block_size, ptr);
1014 if (err) { 1014 if (err) {
1015 DPRINTF(("%s: could not load external firmware " 1015 DPRINTF(("%s: could not load external firmware "
1016 "block\n", device_xname(sc->atu_dev))); 1016 "block\n", device_xname(sc->atu_dev)));
1017 return; 1017 return;
1018 } 1018 }
1019 1019
1020 ptr += block_size; 1020 ptr += block_size;
1021 block++; 1021 block++;
1022 bytes_left -= block_size; 1022 bytes_left -= block_size;
1023 } 1023 }
1024 1024
1025 err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0802, 1025 err = atu_usb_request(sc, UT_WRITE_VENDOR_DEVICE, 0x0e, 0x0802,
1026 block, 0, NULL); 1026 block, 0, NULL);
1027 if (err) { 1027 if (err) {
1028 DPRINTF(("%s: could not load last zero-length firmware " 1028 DPRINTF(("%s: could not load last zero-length firmware "
1029 "block\n", device_xname(sc->atu_dev))); 1029 "block\n", device_xname(sc->atu_dev)));
1030 return; 1030 return;
1031 } 1031 }
1032 1032
1033 /* 1033 /*
1034 * The SMC2662w V.4 seems to require some time to do its thing with 1034 * The SMC2662w V.4 seems to require some time to do its thing with
1035 * the external firmware... 20 ms isn't enough, but 21 ms works 100 1035 * the external firmware... 20 ms isn't enough, but 21 ms works 100
1036 * times out of 100 tries. We'll wait a bit longer just to be sure 1036 * times out of 100 tries. We'll wait a bit longer just to be sure
1037 */ 1037 */
1038 if (sc->atu_quirk & ATU_QUIRK_FW_DELAY) 1038 if (sc->atu_quirk & ATU_QUIRK_FW_DELAY)
1039 usbd_delay_ms(sc->atu_udev, 21 + 100); 1039 usbd_delay_ms(sc->atu_udev, 21 + 100);
1040 1040
1041 DPRINTFN(10, ("%s: external firmware upload done\n", 1041 DPRINTFN(10, ("%s: external firmware upload done\n",
1042 device_xname(sc->atu_dev))); 1042 device_xname(sc->atu_dev)));
1043 /* complete configuration after the firmwares have been uploaded */ 1043 /* complete configuration after the firmwares have been uploaded */
1044 atu_complete_attach(sc); 1044 atu_complete_attach(sc);
1045} 1045}
1046 1046
1047int 1047int
1048atu_get_card_config(struct atu_softc *sc) 1048atu_get_card_config(struct atu_softc *sc)
1049{ 1049{
1050 struct ieee80211com *ic = &sc->sc_ic; 1050 struct ieee80211com *ic = &sc->sc_ic;
1051 struct atu_rfmd_conf rfmd_conf; 1051 struct atu_rfmd_conf rfmd_conf;
1052 struct atu_intersil_conf intersil_conf; 1052 struct atu_intersil_conf intersil_conf;
1053 int err; 1053 int err;
1054 1054
1055 switch (sc->atu_radio) { 1055 switch (sc->atu_radio) {
1056 1056
1057 case RadioRFMD: 1057 case RadioRFMD:
1058 case RadioRFMD2958: 1058 case RadioRFMD2958:
1059 case RadioRFMD2958_SMC: 1059 case RadioRFMD2958_SMC:
1060 case AT76C503_rfmd_acc: 1060 case AT76C503_rfmd_acc:
1061 case AT76C505_rfmd: 1061 case AT76C505_rfmd:
1062 err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33, 1062 err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33,
1063 0x0a02, 0x0000, sizeof(rfmd_conf), 1063 0x0a02, 0x0000, sizeof(rfmd_conf),
1064 (uint8_t *)&rfmd_conf); 1064 (uint8_t *)&rfmd_conf);
1065 if (err) { 1065 if (err) {
1066 DPRINTF(("%s: could not get rfmd config!\n", 1066 DPRINTF(("%s: could not get rfmd config!\n",
1067 device_xname(sc->atu_dev))); 1067 device_xname(sc->atu_dev)));
1068 return err; 1068 return err;
1069 } 1069 }
1070 memcpy(ic->ic_myaddr, rfmd_conf.MACAddr, IEEE80211_ADDR_LEN); 1070 memcpy(ic->ic_myaddr, rfmd_conf.MACAddr, IEEE80211_ADDR_LEN);
1071 break; 1071 break;
1072 1072
1073 case RadioIntersil: 1073 case RadioIntersil:
1074 case AT76C503_i3863: 1074 case AT76C503_i3863:
1075 err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33, 1075 err = atu_usb_request(sc, UT_READ_VENDOR_INTERFACE, 0x33,
1076 0x0902, 0x0000, sizeof(intersil_conf), 1076 0x0902, 0x0000, sizeof(intersil_conf),
1077 (uint8_t *)&intersil_conf); 1077 (uint8_t *)&intersil_conf);
1078 if (err) { 1078 if (err) {
1079 DPRINTF(("%s: could not get intersil config!\n", 1079 DPRINTF(("%s: could not get intersil config!\n",
1080 device_xname(sc->atu_dev))); 1080 device_xname(sc->atu_dev)));
1081 return err; 1081 return err;
1082 } 1082 }
1083 memcpy(ic->ic_myaddr, intersil_conf.MACAddr, 1083 memcpy(ic->ic_myaddr, intersil_conf.MACAddr,
1084 IEEE80211_ADDR_LEN); 1084 IEEE80211_ADDR_LEN);
1085 break; 1085 break;
1086 } 1086 }
1087 return 0; 1087 return 0;
1088} 1088}
1089 1089
1090/* 1090/*
1091 * Probe for an AT76c503 chip. 1091 * Probe for an AT76c503 chip.
1092 */ 1092 */
1093int 1093int
1094atu_match(device_t parent, cfdata_t match, void *aux) 1094atu_match(device_t parent, cfdata_t match, void *aux)
1095{ 1095{
1096 struct usb_attach_arg *uaa = aux; 1096 struct usb_attach_arg *uaa = aux;
1097 int i; 1097 int i;
1098 1098
1099 for (i = 0; i < __arraycount(atu_devs); i++) { 1099 for (i = 0; i < __arraycount(atu_devs); i++) {
1100 const struct atu_type *t = &atu_devs[i]; 1100 const struct atu_type *t = &atu_devs[i];
1101 1101
1102 if (uaa->uaa_vendor == t->atu_vid && 1102 if (uaa->uaa_vendor == t->atu_vid &&
1103 uaa->uaa_product == t->atu_pid) { 1103 uaa->uaa_product == t->atu_pid) {
1104 return UMATCH_VENDOR_PRODUCT; 1104 return UMATCH_VENDOR_PRODUCT;
1105 } 1105 }
1106 } 1106 }
1107 return UMATCH_NONE; 1107 return UMATCH_NONE;
1108} 1108}
1109 1109
1110int 1110int
1111atu_media_change(struct ifnet *ifp) 1111atu_media_change(struct ifnet *ifp)
1112{ 1112{
1113 struct atu_softc *sc = ifp->if_softc; 1113 struct atu_softc *sc = ifp->if_softc;
1114 struct ieee80211com *ic = &sc->sc_ic; 1114 struct ieee80211com *ic = &sc->sc_ic;
1115 int err, s; 1115 int err, s;
1116 1116
1117 DPRINTFN(10, ("%s: atu_media_change\n", device_xname(sc->atu_dev))); 1117 DPRINTFN(10, ("%s: atu_media_change\n", device_xname(sc->atu_dev)));
1118 1118
1119 err = ieee80211_media_change(ifp); 1119 err = ieee80211_media_change(ifp);
1120 if (err == ENETRESET) { 1120 if (err == ENETRESET) {
1121 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == 1121 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
1122 (IFF_RUNNING|IFF_UP)) { 1122 (IFF_RUNNING|IFF_UP)) {
1123 s = splnet(); 1123 s = splnet();
1124 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 1124 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1125 atu_initial_config(sc); 1125 atu_initial_config(sc);
1126 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 1126 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1127 splx(s); 1127 splx(s);
1128 } 1128 }
1129 err = 0; 1129 err = 0;
1130 } 1130 }
1131 1131
1132 return err; 1132 return err;
1133} 1133}
1134 1134
1135void 1135void
1136atu_media_status(struct ifnet *ifp, struct ifmediareq *req) 1136atu_media_status(struct ifnet *ifp, struct ifmediareq *req)
1137{ 1137{
1138#ifdef ATU_DEBUG 1138#ifdef ATU_DEBUG
1139 struct atu_softc *sc = ifp->if_softc; 1139 struct atu_softc *sc = ifp->if_softc;
1140#endif /* ATU_DEBUG */ 1140#endif /* ATU_DEBUG */
1141 1141
1142 DPRINTFN(10, ("%s: atu_media_status\n", device_xname(sc->atu_dev))); 1142 DPRINTFN(10, ("%s: atu_media_status\n", device_xname(sc->atu_dev)));
1143 1143
1144 ieee80211_media_status(ifp, req); 1144 ieee80211_media_status(ifp, req);
1145} 1145}
1146 1146
1147void 1147void
1148atu_task(void *arg) 1148atu_task(void *arg)
1149{ 1149{
1150 struct atu_softc *sc = (struct atu_softc *)arg; 1150 struct atu_softc *sc = (struct atu_softc *)arg;
1151 struct ieee80211com *ic = &sc->sc_ic; 1151 struct ieee80211com *ic = &sc->sc_ic;
1152 usbd_status err; 1152 usbd_status err;
1153 int s; 1153 int s;
1154 1154
1155 DPRINTFN(10, ("%s: atu_task\n", device_xname(sc->atu_dev))); 1155 DPRINTFN(10, ("%s: atu_task\n", device_xname(sc->atu_dev)));
1156 1156
1157 if (sc->sc_state != ATU_S_OK) 1157 if (sc->sc_state != ATU_S_OK)
1158 return; 1158 return;
1159 1159
1160 switch (sc->sc_cmd) { 1160 switch (sc->sc_cmd) {
1161 case ATU_C_SCAN: 1161 case ATU_C_SCAN:
1162 1162
1163 err = atu_start_scan(sc); 1163 err = atu_start_scan(sc);
1164 if (err) { 1164 if (err) {
1165 DPRINTFN(1, ("%s: atu_task: couldn't start scan!\n", 1165 DPRINTFN(1, ("%s: atu_task: couldn't start scan!\n",
1166 device_xname(sc->atu_dev))); 1166 device_xname(sc->atu_dev)));
1167 return; 1167 return;
1168 } 1168 }
1169 1169
1170 err = atu_wait_completion(sc, CMD_START_SCAN, NULL); 1170 err = atu_wait_completion(sc, CMD_START_SCAN, NULL);
1171 if (err) { 1171 if (err) {
1172 DPRINTF(("%s: atu_task: error waiting for scan\n", 1172 DPRINTF(("%s: atu_task: error waiting for scan\n",
1173 device_xname(sc->atu_dev))); 1173 device_xname(sc->atu_dev)));
1174 return; 1174 return;
1175 } 1175 }
1176 1176
1177 DPRINTF(("%s: ==========================> END OF SCAN!\n", 1177 DPRINTF(("%s: ==========================> END OF SCAN!\n",
1178 device_xname(sc->atu_dev))); 1178 device_xname(sc->atu_dev)));
1179 1179
1180 s = splnet(); 1180 s = splnet();
1181 ieee80211_next_scan(ic); 1181 ieee80211_next_scan(ic);
1182 splx(s); 1182 splx(s);
1183 1183
1184 DPRINTF(("%s: ----------------------======> END OF SCAN2!\n", 1184 DPRINTF(("%s: ----------------------======> END OF SCAN2!\n",
1185 device_xname(sc->atu_dev))); 1185 device_xname(sc->atu_dev)));
1186 break; 1186 break;
1187 1187
1188 case ATU_C_JOIN: 1188 case ATU_C_JOIN:
1189 atu_join(sc, ic->ic_bss); 1189 atu_join(sc, ic->ic_bss);
1190 } 1190 }
1191} 1191}
1192 1192
1193int 1193int
1194atu_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 1194atu_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1195{ 1195{
1196 struct ifnet *ifp = ic->ic_ifp; 1196 struct ifnet *ifp = ic->ic_ifp;
1197 struct atu_softc *sc = ifp->if_softc; 1197 struct atu_softc *sc = ifp->if_softc;
1198 enum ieee80211_state ostate = ic->ic_state; 1198 enum ieee80211_state ostate = ic->ic_state;
1199 1199
1200 DPRINTFN(10, ("%s: atu_newstate: %s -> %s\n", device_xname(sc->atu_dev), 1200 DPRINTFN(10, ("%s: atu_newstate: %s -> %s\n", device_xname(sc->atu_dev),
1201 ieee80211_state_name[ostate], ieee80211_state_name[nstate])); 1201 ieee80211_state_name[ostate], ieee80211_state_name[nstate]));
1202 1202
1203 switch (nstate) { 1203 switch (nstate) {
1204 case IEEE80211_S_SCAN: 1204 case IEEE80211_S_SCAN:
1205 memcpy(ic->ic_chan_scan, ic->ic_chan_active, 1205 memcpy(ic->ic_chan_scan, ic->ic_chan_active,
1206 sizeof(ic->ic_chan_active)); 1206 sizeof(ic->ic_chan_active));
1207 ieee80211_node_table_reset(&ic->ic_scan); 1207 ieee80211_node_table_reset(&ic->ic_scan);
1208 1208
1209 /* tell the event thread that we want a scan */ 1209 /* tell the event thread that we want a scan */
1210 sc->sc_cmd = ATU_C_SCAN; 1210 sc->sc_cmd = ATU_C_SCAN;
1211 usb_add_task(sc->atu_udev, &sc->sc_task, USB_TASKQ_DRIVER); 1211 usb_add_task(sc->atu_udev, &sc->sc_task, USB_TASKQ_DRIVER);
1212 1212
1213 /* handle this ourselves */ 1213 /* handle this ourselves */
1214 ic->ic_state = nstate; 1214 ic->ic_state = nstate;
1215 return 0; 1215 return 0;
1216 1216
1217 case IEEE80211_S_AUTH: 1217 case IEEE80211_S_AUTH:
1218 case IEEE80211_S_RUN: 1218 case IEEE80211_S_RUN:
1219 if (ostate == IEEE80211_S_SCAN) { 1219 if (ostate == IEEE80211_S_SCAN) {
1220 sc->sc_cmd = ATU_C_JOIN; 1220 sc->sc_cmd = ATU_C_JOIN;
1221 usb_add_task(sc->atu_udev, &sc->sc_task, 1221 usb_add_task(sc->atu_udev, &sc->sc_task,
1222 USB_TASKQ_DRIVER); 1222 USB_TASKQ_DRIVER);
1223 } 1223 }
1224 break; 1224 break;
1225 default: 1225 default:
1226 /* nothing to do */ 1226 /* nothing to do */
1227 break; 1227 break;
1228 } 1228 }
1229 1229
1230 return (*sc->sc_newstate)(ic, nstate, arg); 1230 return (*sc->sc_newstate)(ic, nstate, arg);
1231} 1231}
1232 1232
1233/* 1233/*
1234 * Attach the interface. Allocate softc structures, do 1234 * Attach the interface. Allocate softc structures, do
1235 * setup and ethernet/BPF attach. 1235 * setup and ethernet/BPF attach.
1236 */ 1236 */
1237void 1237void
1238atu_attach(device_t parent, device_t self, void *aux) 1238atu_attach(device_t parent, device_t self, void *aux)
1239{ 1239{
1240 struct atu_softc *sc = device_private(self); 1240 struct atu_softc *sc = device_private(self);
1241 struct usb_attach_arg *uaa = aux; 1241 struct usb_attach_arg *uaa = aux;
1242 char *devinfop; 1242 char *devinfop;
1243 usbd_status err; 1243 usbd_status err;
1244 struct usbd_device *dev = uaa->uaa_device; 1244 struct usbd_device *dev = uaa->uaa_device;
1245 uint8_t mode, channel; 1245 uint8_t mode, channel;
1246 int i; 1246 int i;
1247 1247
1248 sc->atu_dev = self; 1248 sc->atu_dev = self;
1249 sc->sc_state = ATU_S_UNCONFIG; 1249 sc->sc_state = ATU_S_UNCONFIG;
1250 1250
1251 aprint_naive("\n"); 1251 aprint_naive("\n");
1252 aprint_normal("\n"); 1252 aprint_normal("\n");
1253 1253
1254 devinfop = usbd_devinfo_alloc(dev, 0); 1254 devinfop = usbd_devinfo_alloc(dev, 0);
1255 aprint_normal_dev(self, "%s\n", devinfop); 1255 aprint_normal_dev(self, "%s\n", devinfop);
1256 usbd_devinfo_free(devinfop); 1256 usbd_devinfo_free(devinfop);
1257 1257
1258 err = usbd_set_config_no(dev, ATU_CONFIG_NO, 1); 1258 err = usbd_set_config_no(dev, ATU_CONFIG_NO, 1);
1259 if (err) { 1259 if (err) {
1260 aprint_error_dev(self, "failed to set configuration" 1260 aprint_error_dev(self, "failed to set configuration"
1261 ", err=%s\n", usbd_errstr(err)); 1261 ", err=%s\n", usbd_errstr(err));
1262 return; 1262 return;
1263 } 1263 }
1264 1264
1265 err = usbd_device2interface_handle(dev, ATU_IFACE_IDX, &sc->atu_iface); 1265 err = usbd_device2interface_handle(dev, ATU_IFACE_IDX, &sc->atu_iface);
1266 if (err) { 1266 if (err) {
1267 aprint_error_dev(self, "getting interface handle failed\n"); 1267 aprint_error_dev(self, "getting interface handle failed\n");
1268 return; 1268 return;
1269 } 1269 }
1270 1270
1271 sc->atu_unit = device_unit(self); 1271 sc->atu_unit = device_unit(self);
1272 sc->atu_udev = dev; 1272 sc->atu_udev = dev;
1273 1273
1274 /* 1274 /*
1275 * look up the radio_type for the device 1275 * look up the radio_type for the device
1276 * basically does the same as atu_match 1276 * basically does the same as atu_match
1277 */ 1277 */
1278 for (i = 0; i < __arraycount(atu_devs); i++) { 1278 for (i = 0; i < __arraycount(atu_devs); i++) {
1279 const struct atu_type *t = &atu_devs[i]; 1279 const struct atu_type *t = &atu_devs[i];
1280 1280
1281 if (uaa->uaa_vendor == t->atu_vid && 1281 if (uaa->uaa_vendor == t->atu_vid &&
1282 uaa->uaa_product == t->atu_pid) { 1282 uaa->uaa_product == t->atu_pid) {
1283 sc->atu_radio = t->atu_radio; 1283 sc->atu_radio = t->atu_radio;
1284 sc->atu_quirk = t->atu_quirk; 1284 sc->atu_quirk = t->atu_quirk;
1285 } 1285 }
1286 } 1286 }
1287 1287
1288 /* 1288 /*
1289 * Check in the interface descriptor if we're in DFU mode 1289 * Check in the interface descriptor if we're in DFU mode
1290 * If we're in DFU mode, we upload the external firmware 1290 * If we're in DFU mode, we upload the external firmware
1291 * If we're not, the PC must have rebooted without power-cycling 1291 * If we're not, the PC must have rebooted without power-cycling
1292 * the device.. I've tried this out, a reboot only requeres the 1292 * the device.. I've tried this out, a reboot only requeres the
1293 * external firmware to be reloaded :) 1293 * external firmware to be reloaded :)
1294 * 1294 *
1295 * Hmm. The at76c505a doesn't report a DFU descriptor when it's 1295 * Hmm. The at76c505a doesn't report a DFU descriptor when it's
1296 * in DFU mode... Let's just try to get the opmode 1296 * in DFU mode... Let's just try to get the opmode
1297 */ 1297 */
1298 err = atu_get_opmode(sc, &mode); 1298 err = atu_get_opmode(sc, &mode);
1299 DPRINTFN(20, ("%s: opmode: %d\n", device_xname(sc->atu_dev), mode)); 1299 DPRINTFN(20, ("%s: opmode: %d\n", device_xname(sc->atu_dev), mode));
1300 if (err || (mode != MODE_NETCARD && mode != MODE_NOFLASHNETCARD)) { 1300 if (err || (mode != MODE_NETCARD && mode != MODE_NOFLASHNETCARD)) {
1301 DPRINTF(("%s: starting internal firmware download\n", 1301 DPRINTF(("%s: starting internal firmware download\n",
1302 device_xname(sc->atu_dev))); 1302 device_xname(sc->atu_dev)));
1303 1303
1304 atu_internal_firmware(sc->atu_dev); 1304 atu_internal_firmware(sc->atu_dev);
1305 /* 1305 /*
1306 * atu_internal_firmware will cause a reset of the device 1306 * atu_internal_firmware will cause a reset of the device
1307 * so we don't want to do any more configuration after this 1307 * so we don't want to do any more configuration after this
1308 * point. 1308 * point.
1309 */ 1309 */
1310 return; 1310 return;
1311 } 1311 }
1312 1312
1313 if (mode != MODE_NETCARD) { 1313 if (mode != MODE_NETCARD) {
1314 DPRINTFN(15, ("%s: device needs external firmware\n", 1314 DPRINTFN(15, ("%s: device needs external firmware\n",
1315 device_xname(sc->atu_dev))); 1315 device_xname(sc->atu_dev)));
1316 1316
1317 if (mode != MODE_NOFLASHNETCARD) { 1317 if (mode != MODE_NOFLASHNETCARD) {
1318 DPRINTF(("%s: unexpected opmode=%d\n", 1318 DPRINTF(("%s: unexpected opmode=%d\n",
1319 device_xname(sc->atu_dev), mode)); 1319 device_xname(sc->atu_dev), mode));
1320 } 1320 }
1321 1321
1322 /* 1322 /*
1323 * There is no difference in opmode before and after external 1323 * There is no difference in opmode before and after external
1324 * firmware upload with the SMC2662 V.4 . So instead we'll try 1324 * firmware upload with the SMC2662 V.4 . So instead we'll try
1325 * to read the channel number. If we succeed, external 1325 * to read the channel number. If we succeed, external
1326 * firmwaremust have been already uploaded... 1326 * firmwaremust have been already uploaded...
1327 */ 1327 */
1328 if (sc->atu_radio != RadioIntersil) { 1328 if (sc->atu_radio != RadioIntersil) {
1329 err = atu_get_mib(sc, MIB_PHY__CHANNEL, &channel); 1329 err = atu_get_mib(sc, MIB_PHY__CHANNEL, &channel);
1330 if (!err) { 1330 if (!err) {
1331 DPRINTF(("%s: external firmware has already" 1331 DPRINTF(("%s: external firmware has already"
1332 " been downloaded\n", 1332 " been downloaded\n",
1333 device_xname(sc->atu_dev))); 1333 device_xname(sc->atu_dev)));
1334 atu_complete_attach(sc); 1334 atu_complete_attach(sc);
1335 return; 1335 return;
1336 } 1336 }
1337 } 1337 }
1338 1338
1339 atu_external_firmware(sc->atu_dev); 1339 atu_external_firmware(sc->atu_dev);
1340 1340
1341 /* 1341 /*
1342 * atu_external_firmware will call atu_complete_attach after 1342 * atu_external_firmware will call atu_complete_attach after
1343 * it's finished so we can just return. 1343 * it's finished so we can just return.
1344 */ 1344 */
1345 } else { 1345 } else {
1346 /* all the firmwares are in place, so complete the attach */ 1346 /* all the firmwares are in place, so complete the attach */
1347 atu_complete_attach(sc); 1347 atu_complete_attach(sc);
1348 } 1348 }
1349 1349
1350 return; 1350 return;
1351} 1351}
1352 1352
1353void 1353void
1354atu_complete_attach(struct atu_softc *sc) 1354atu_complete_attach(struct atu_softc *sc)
1355{ 1355{
1356 struct ieee80211com *ic = &sc->sc_ic; 1356 struct ieee80211com *ic = &sc->sc_ic;
1357 struct ifnet *ifp = &sc->sc_if; 1357 struct ifnet *ifp = &sc->sc_if;
1358 usb_interface_descriptor_t *id; 1358 usb_interface_descriptor_t *id;
1359 usb_endpoint_descriptor_t *ed; 1359 usb_endpoint_descriptor_t *ed;
1360 usbd_status err; 1360 usbd_status err;
1361 int i; 1361 int i;
1362#ifdef ATU_DEBUG 1362#ifdef ATU_DEBUG
1363 struct atu_fw fw; 1363 struct atu_fw fw;
1364#endif 1364#endif
1365 1365
1366 id = usbd_get_interface_descriptor(sc->atu_iface); 1366 id = usbd_get_interface_descriptor(sc->atu_iface);
1367 1367
1368 /* Find endpoints. */ 1368 /* Find endpoints. */
1369 for (i = 0; i < id->bNumEndpoints; i++) { 1369 for (i = 0; i < id->bNumEndpoints; i++) {
1370 ed = usbd_interface2endpoint_descriptor(sc->atu_iface, i); 1370 ed = usbd_interface2endpoint_descriptor(sc->atu_iface, i);
1371 if (!ed) { 1371 if (!ed) {
1372 DPRINTF(("%s: num_endp:%d\n", device_xname(sc->atu_dev), 1372 DPRINTF(("%s: num_endp:%d\n", device_xname(sc->atu_dev),
1373 sc->atu_iface->ui_idesc->bNumEndpoints)); 1373 sc->atu_iface->ui_idesc->bNumEndpoints));
1374 DPRINTF(("%s: couldn't get ep %d\n", 1374 DPRINTF(("%s: couldn't get ep %d\n",
1375 device_xname(sc->atu_dev), i)); 1375 device_xname(sc->atu_dev), i));
1376 return; 1376 return;
1377 } 1377 }
1378 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 1378 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1379 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 1379 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1380 sc->atu_ed[ATU_ENDPT_RX] = ed->bEndpointAddress; 1380 sc->atu_ed[ATU_ENDPT_RX] = ed->bEndpointAddress;
1381 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 1381 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1382 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 1382 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
1383 sc->atu_ed[ATU_ENDPT_TX] = ed->bEndpointAddress; 1383 sc->atu_ed[ATU_ENDPT_TX] = ed->bEndpointAddress;
1384 } 1384 }
1385 } 1385 }
1386 1386
1387 /* read device config & get MAC address */ 1387 /* read device config & get MAC address */
1388 err = atu_get_card_config(sc); 1388 err = atu_get_card_config(sc);
1389 if (err) { 1389 if (err) {
1390 aprint_error("\n%s: could not get card cfg!\n", 1390 aprint_error("\n%s: could not get card cfg!\n",
1391 device_xname(sc->atu_dev)); 1391 device_xname(sc->atu_dev));
1392 return; 1392 return;
1393 } 1393 }
1394 1394
1395#ifdef ATU_DEBUG 1395#ifdef ATU_DEBUG
1396 /* DEBUG : try to get firmware version */ 1396 /* DEBUG : try to get firmware version */
1397 err = atu_get_mib(sc, MIB_FW_VERSION, sizeof(fw), 0, (uint8_t *)&fw); 1397 err = atu_get_mib(sc, MIB_FW_VERSION, sizeof(fw), 0, (uint8_t *)&fw);
1398 if (!err) { 1398 if (!err) {
1399 DPRINTFN(15, ("%s: firmware: maj:%d min:%d patch:%d " 1399 DPRINTFN(15, ("%s: firmware: maj:%d min:%d patch:%d "
1400 "build:%d\n", device_xname(sc->atu_dev), fw.major, 1400 "build:%d\n", device_xname(sc->atu_dev), fw.major,
1401 fw.minor, fw.patch, fw.build)); 1401 fw.minor, fw.patch, fw.build));
1402 } else { 1402 } else {
1403 DPRINTF(("%s: get firmware version failed\n", 1403 DPRINTF(("%s: get firmware version failed\n",
1404 device_xname(sc->atu_dev))); 1404 device_xname(sc->atu_dev)));
1405 } 1405 }
1406#endif /* ATU_DEBUG */ 1406#endif /* ATU_DEBUG */
1407 1407
1408 /* Show the world our MAC address */ 1408 /* Show the world our MAC address */
1409 aprint_normal_dev(sc->atu_dev, "MAC address %s\n", 1409 aprint_normal_dev(sc->atu_dev, "MAC address %s\n",
1410 ether_sprintf(ic->ic_myaddr)); 1410 ether_sprintf(ic->ic_myaddr));
1411 1411
1412 sc->atu_cdata.atu_tx_inuse = 0; 1412 sc->atu_cdata.atu_tx_inuse = 0;
1413 sc->atu_encrypt = ATU_WEP_OFF; 1413 sc->atu_encrypt = ATU_WEP_OFF;
1414 sc->atu_wepkeylen = ATU_WEP_104BITS; 1414 sc->atu_wepkeylen = ATU_WEP_104BITS;
1415 sc->atu_wepkey = 0; 1415 sc->atu_wepkey = 0;
1416 1416
1417 memset(sc->atu_bssid, 0, ETHER_ADDR_LEN); 1417 memset(sc->atu_bssid, 0, ETHER_ADDR_LEN);
1418 sc->atu_channel = ATU_DEFAULT_CHANNEL; 1418 sc->atu_channel = ATU_DEFAULT_CHANNEL;
1419 sc->atu_desired_channel = IEEE80211_CHAN_ANY; 1419 sc->atu_desired_channel = IEEE80211_CHAN_ANY;
1420 sc->atu_mode = INFRASTRUCTURE_MODE; 1420 sc->atu_mode = INFRASTRUCTURE_MODE;
1421 1421
1422 ic->ic_ifp = ifp; 1422 ic->ic_ifp = ifp;
1423 ic->ic_phytype = IEEE80211_T_DS; 1423 ic->ic_phytype = IEEE80211_T_DS;
1424 ic->ic_opmode = IEEE80211_M_STA; 1424 ic->ic_opmode = IEEE80211_M_STA;
1425 ic->ic_state = IEEE80211_S_INIT; 1425 ic->ic_state = IEEE80211_S_INIT;
1426#ifdef FIXME 1426#ifdef FIXME
1427 ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP | IEEE80211_C_SCANALL; 1427 ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP | IEEE80211_C_SCANALL;
1428#else 1428#else
1429 ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP; 1429 ic->ic_caps = IEEE80211_C_IBSS | IEEE80211_C_WEP;
1430#endif 1430#endif
1431 1431
1432 i = 0; 1432 i = 0;
1433 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b; 1433 ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
1434 1434
1435 for (i = 1; i <= 14; i++) { 1435 for (i = 1; i <= 14; i++) {
1436 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B | 1436 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_B |
1437 IEEE80211_CHAN_PASSIVE; 1437 IEEE80211_CHAN_PASSIVE;
1438 ic->ic_channels[i].ic_freq = ieee80211_ieee2mhz(i, 1438 ic->ic_channels[i].ic_freq = ieee80211_ieee2mhz(i,
1439 ic->ic_channels[i].ic_flags); 1439 ic->ic_channels[i].ic_flags);
1440 } 1440 }
1441 1441
1442 ic->ic_ibss_chan = &ic->ic_channels[0]; 1442 ic->ic_ibss_chan = &ic->ic_channels[0];
1443 1443
1444 ifp->if_softc = sc; 1444 ifp->if_softc = sc;
1445 memcpy(ifp->if_xname, device_xname(sc->atu_dev), IFNAMSIZ); 1445 memcpy(ifp->if_xname, device_xname(sc->atu_dev), IFNAMSIZ);
1446 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1446 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1447 ifp->if_init = atu_init; 1447 ifp->if_init = atu_init;
1448 ifp->if_stop = atu_stop; 1448 ifp->if_stop = atu_stop;
1449 ifp->if_start = atu_start; 1449 ifp->if_start = atu_start;
1450 ifp->if_ioctl = atu_ioctl; 1450 ifp->if_ioctl = atu_ioctl;
1451 ifp->if_watchdog = atu_watchdog; 1451 ifp->if_watchdog = atu_watchdog;
1452 ifp->if_mtu = ATU_DEFAULT_MTU; 1452 ifp->if_mtu = ATU_DEFAULT_MTU;
1453 IFQ_SET_READY(&ifp->if_snd); 1453 IFQ_SET_READY(&ifp->if_snd);
1454 1454
1455 /* Call MI attach routine. */ 1455 /* Call MI attach routine. */
1456 if_attach(ifp); 1456 if_attach(ifp);
1457 ieee80211_ifattach(ic); 1457 ieee80211_ifattach(ic);
1458 1458
1459 sc->sc_newstate = ic->ic_newstate; 1459 sc->sc_newstate = ic->ic_newstate;
1460 ic->ic_newstate = atu_newstate; 1460 ic->ic_newstate = atu_newstate;
1461 1461
1462 /* setup ifmedia interface */ 1462 /* setup ifmedia interface */
1463 ieee80211_media_init(ic, atu_media_change, atu_media_status); 1463 ieee80211_media_init(ic, atu_media_change, atu_media_status);
1464 1464
1465 usb_init_task(&sc->sc_task, atu_task, sc, 0); 1465 usb_init_task(&sc->sc_task, atu_task, sc, 0);
1466 1466
1467 sc->sc_state = ATU_S_OK; 1467 sc->sc_state = ATU_S_OK;
1468} 1468}
1469 1469
1470int 1470int
1471atu_detach(device_t self, int flags) 1471atu_detach(device_t self, int flags)
1472{ 1472{
1473 struct atu_softc *sc = device_private(self); 1473 struct atu_softc *sc = device_private(self);
1474 struct ifnet *ifp = &sc->sc_if; 1474 struct ifnet *ifp = &sc->sc_if;
1475 1475
1476 DPRINTFN(10, ("%s: atu_detach state=%d\n", device_xname(sc->atu_dev), 1476 DPRINTFN(10, ("%s: atu_detach state=%d\n", device_xname(sc->atu_dev),
1477 sc->sc_state)); 1477 sc->sc_state));
1478 1478
1479 if (sc->sc_state != ATU_S_UNCONFIG) { 1479 if (sc->sc_state != ATU_S_UNCONFIG) {
1480 atu_stop(ifp, 1); 1480 atu_stop(ifp, 1);
1481 1481
1482 ieee80211_ifdetach(&sc->sc_ic); 1482 ieee80211_ifdetach(&sc->sc_ic);
1483 if_detach(ifp); 1483 if_detach(ifp);
1484 } 1484 }
1485 1485
1486 return 0; 1486 return 0;
1487} 1487}
1488 1488
1489int 1489int
1490atu_activate(device_t self, enum devact act) 1490atu_activate(device_t self, enum devact act)
1491{ 1491{
1492 struct atu_softc *sc = device_private(self); 1492 struct atu_softc *sc = device_private(self);
1493 1493
1494 switch (act) { 1494 switch (act) {
1495 case DVACT_DEACTIVATE: 1495 case DVACT_DEACTIVATE:
1496 if (sc->sc_state != ATU_S_UNCONFIG) { 1496 if (sc->sc_state != ATU_S_UNCONFIG) {
1497 if_deactivate(&sc->atu_ec.ec_if); 1497 if_deactivate(&sc->atu_ec.ec_if);
1498 sc->sc_state = ATU_S_DEAD; 1498 sc->sc_state = ATU_S_DEAD;
1499 } 1499 }
1500 return 0; 1500 return 0;
1501 default: 1501 default:
1502 return EOPNOTSUPP; 1502 return EOPNOTSUPP;
1503 } 1503 }
1504} 1504}
1505 1505
1506/* 1506/*
1507 * Initialize an RX descriptor and attach an MBUF cluster. 1507 * Initialize an RX descriptor and attach an MBUF cluster.
1508 */ 1508 */
1509int 1509int
1510atu_newbuf(struct atu_softc *sc, struct atu_chain *c, struct mbuf *m) 1510atu_newbuf(struct atu_softc *sc, struct atu_chain *c, struct mbuf *m)
1511{ 1511{
1512 struct mbuf *m_new = NULL; 1512 struct mbuf *m_new = NULL;
1513 1513
1514 if (m == NULL) { 1514 if (m == NULL) {
1515 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1515 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1516 if (m_new == NULL) { 1516 if (m_new == NULL) {
1517 DPRINTF(("%s: no memory for rx list\n", 1517 DPRINTF(("%s: no memory for rx list\n",
1518 device_xname(sc->atu_dev))); 1518 device_xname(sc->atu_dev)));
1519 return ENOBUFS; 1519 return ENOBUFS;
1520 } 1520 }
1521 1521
1522 MCLGET(m_new, M_DONTWAIT); 1522 MCLGET(m_new, M_DONTWAIT);
1523 if (!(m_new->m_flags & M_EXT)) { 1523 if (!(m_new->m_flags & M_EXT)) {
1524 DPRINTF(("%s: no memory for rx list\n", 1524 DPRINTF(("%s: no memory for rx list\n",
1525 device_xname(sc->atu_dev))); 1525 device_xname(sc->atu_dev)));
1526 m_freem(m_new); 1526 m_freem(m_new);
1527 return ENOBUFS; 1527 return ENOBUFS;
1528 } 1528 }
1529 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 1529 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1530 } else { 1530 } else {
1531 m_new = m; 1531 m_new = m;
1532 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 1532 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1533 m_new->m_data = m_new->m_ext.ext_buf; 1533 m_new->m_data = m_new->m_ext.ext_buf;
1534 } 1534 }
1535 c->atu_mbuf = m_new; 1535 c->atu_mbuf = m_new;
1536 return 0; 1536 return 0;
1537} 1537}
1538 1538
1539int 1539int
1540atu_rx_list_init(struct atu_softc *sc) 1540atu_rx_list_init(struct atu_softc *sc)
1541{ 1541{
1542 struct atu_cdata *cd = &sc->atu_cdata; 1542 struct atu_cdata *cd = &sc->atu_cdata;
1543 struct atu_chain *c; 1543 struct atu_chain *c;
1544 int i; 1544 int i;
1545 1545
1546 DPRINTFN(15, ("%s: atu_rx_list_init: enter\n", 1546 DPRINTFN(15, ("%s: atu_rx_list_init: enter\n",
1547 device_xname(sc->atu_dev))); 1547 device_xname(sc->atu_dev)));
1548 1548
1549 for (i = 0; i < ATU_RX_LIST_CNT; i++) { 1549 for (i = 0; i < ATU_RX_LIST_CNT; i++) {
1550 c = &cd->atu_rx_chain[i]; 1550 c = &cd->atu_rx_chain[i];
1551 c->atu_sc = sc; 1551 c->atu_sc = sc;
1552 c->atu_idx = i; 1552 c->atu_idx = i;
1553 if (c->atu_xfer == NULL) { 1553 if (c->atu_xfer == NULL) {
1554 int err = usbd_create_xfer(sc->atu_ep[ATU_ENDPT_RX], 1554 int err = usbd_create_xfer(sc->atu_ep[ATU_ENDPT_RX],
1555 ATU_RX_BUFSZ, 0, 0, &c->atu_xfer); 1555 ATU_RX_BUFSZ, 0, 0, &c->atu_xfer);
1556 if (err) 1556 if (err)
1557 return err; 1557 return err;
1558 c->atu_buf = usbd_get_buffer(c->atu_xfer); 1558 c->atu_buf = usbd_get_buffer(c->atu_xfer);
1559 if (atu_newbuf(sc, c, NULL) == ENOBUFS) /* XXX free? */ 1559 if (atu_newbuf(sc, c, NULL) == ENOBUFS) /* XXX free? */
1560 return ENOBUFS; 1560 return ENOBUFS;
1561 } 1561 }
1562 } 1562 }
1563 return 0; 1563 return 0;
1564} 1564}
1565 1565
1566int 1566int
1567atu_tx_list_init(struct atu_softc *sc) 1567atu_tx_list_init(struct atu_softc *sc)
1568{ 1568{
1569 struct atu_cdata *cd = &sc->atu_cdata; 1569 struct atu_cdata *cd = &sc->atu_cdata;
1570 struct atu_chain *c; 1570 struct atu_chain *c;
1571 int i; 1571 int i;
1572 1572
1573 DPRINTFN(15, ("%s: atu_tx_list_init\n", 1573 DPRINTFN(15, ("%s: atu_tx_list_init\n",
1574 device_xname(sc->atu_dev))); 1574 device_xname(sc->atu_dev)));
1575 1575
1576 SLIST_INIT(&cd->atu_tx_free); 1576 SLIST_INIT(&cd->atu_tx_free);
1577 sc->atu_cdata.atu_tx_inuse = 0; 1577 sc->atu_cdata.atu_tx_inuse = 0;
1578 1578
1579 for (i = 0; i < ATU_TX_LIST_CNT; i++) { 1579 for (i = 0; i < ATU_TX_LIST_CNT; i++) {
1580 c = &cd->atu_tx_chain[i]; 1580 c = &cd->atu_tx_chain[i];
1581 c->atu_sc = sc; 1581 c->atu_sc = sc;
1582 c->atu_idx = i; 1582 c->atu_idx = i;
1583 if (c->atu_xfer == NULL) { 1583 if (c->atu_xfer == NULL) {
1584 int err = usbd_create_xfer(sc->atu_ep[ATU_ENDPT_TX], 1584 int err = usbd_create_xfer(sc->atu_ep[ATU_ENDPT_TX],
1585 ATU_TX_BUFSZ, 0, 0, &c->atu_xfer); 1585 ATU_TX_BUFSZ, 0, 0, &c->atu_xfer);
1586 if (err) { 1586 if (err) {
1587 return err; 1587 return err;
1588 } 1588 }
1589 c->atu_buf = usbd_get_buffer(c->atu_xfer); 1589 c->atu_buf = usbd_get_buffer(c->atu_xfer);
1590 SLIST_INSERT_HEAD(&cd->atu_tx_free, c, atu_list); 1590 SLIST_INSERT_HEAD(&cd->atu_tx_free, c, atu_list);
1591 } 1591 }
1592 } 1592 }
1593 return 0; 1593 return 0;
1594} 1594}
1595 1595
1596void 1596void
1597atu_xfer_list_free(struct atu_softc *sc, struct atu_chain *ch, int listlen) 1597atu_xfer_list_free(struct atu_softc *sc, struct atu_chain *ch, int listlen)
1598{ 1598{
1599 int i; 1599 int i;
1600 1600
1601 /* Free resources. */ 1601 /* Free resources. */
1602 for (i = 0; i < listlen; i++) { 1602 for (i = 0; i < listlen; i++) {
1603 if (ch[i].atu_buf != NULL) 1603 if (ch[i].atu_buf != NULL)
1604 ch[i].atu_buf = NULL; 1604 ch[i].atu_buf = NULL;
1605 if (ch[i].atu_mbuf != NULL) { 1605 if (ch[i].atu_mbuf != NULL) {
1606 m_freem(ch[i].atu_mbuf); 1606 m_freem(ch[i].atu_mbuf);
1607 ch[i].atu_mbuf = NULL; 1607 ch[i].atu_mbuf = NULL;
1608 } 1608 }
1609 if (ch[i].atu_xfer != NULL) { 1609 if (ch[i].atu_xfer != NULL) {
1610 usbd_destroy_xfer(ch[i].atu_xfer); 1610 usbd_destroy_xfer(ch[i].atu_xfer);
1611 ch[i].atu_xfer = NULL; 1611 ch[i].atu_xfer = NULL;
1612 } 1612 }
1613 } 1613 }
1614} 1614}
1615 1615
1616/* 1616/*
1617 * A frame has been uploaded: pass the resulting mbuf chain up to 1617 * A frame has been uploaded: pass the resulting mbuf chain up to
1618 * the higher level protocols. 1618 * the higher level protocols.
1619 */ 1619 */
1620void 1620void
1621atu_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1621atu_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1622{ 1622{
1623 struct atu_chain *c = (struct atu_chain *)priv; 1623 struct atu_chain *c = (struct atu_chain *)priv;
1624 struct atu_softc *sc = c->atu_sc; 1624 struct atu_softc *sc = c->atu_sc;
1625 struct ieee80211com *ic = &sc->sc_ic; 1625 struct ieee80211com *ic = &sc->sc_ic;
1626 struct ifnet *ifp = &sc->sc_if; 1626 struct ifnet *ifp = &sc->sc_if;
1627 struct atu_rx_hdr *h; 1627 struct atu_rx_hdr *h;
1628 struct ieee80211_frame_min *wh; 1628 struct ieee80211_frame_min *wh;
1629 struct ieee80211_node *ni; 1629 struct ieee80211_node *ni;
1630 struct mbuf *m; 1630 struct mbuf *m;
1631 uint32_t len; 1631 uint32_t len;
1632 int s; 1632 int s;
1633 1633
1634 DPRINTFN(25, ("%s: atu_rxeof\n", device_xname(sc->atu_dev))); 1634 DPRINTFN(25, ("%s: atu_rxeof\n", device_xname(sc->atu_dev)));
1635 1635
1636 if (sc->sc_state != ATU_S_OK) 1636 if (sc->sc_state != ATU_S_OK)
1637 return; 1637 return;
1638 1638
1639 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) 1639 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP))
1640 goto done; 1640 goto done;
1641 1641
1642 if (status != USBD_NORMAL_COMPLETION) { 1642 if (status != USBD_NORMAL_COMPLETION) {
1643 DPRINTF(("%s: status != USBD_NORMAL_COMPLETION\n", 1643 DPRINTF(("%s: status != USBD_NORMAL_COMPLETION\n",
1644 device_xname(sc->atu_dev))); 1644 device_xname(sc->atu_dev)));
1645 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1645 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1646 return; 1646 return;
1647 } 1647 }
1648#if 0 1648#if 0
1649 if (status == USBD_IOERROR) { 1649 if (status == USBD_IOERROR) {
1650 DPRINTF(("%s: rx: EEK! lost device?\n", 1650 DPRINTF(("%s: rx: EEK! lost device?\n",
1651 device_xname(sc->atu_dev))); 1651 device_xname(sc->atu_dev)));
1652 1652
1653 /* 1653 /*
1654 * My experience with USBD_IOERROR is that trying to 1654 * My experience with USBD_IOERROR is that trying to
1655 * restart the transfer will always fail and we'll 1655 * restart the transfer will always fail and we'll
1656 * keep on looping restarting transfers untill someone 1656 * keep on looping restarting transfers untill someone
1657 * pulls the plug of the device. 1657 * pulls the plug of the device.
1658 * So we don't restart the transfer, but just let it 1658 * So we don't restart the transfer, but just let it
1659 * die... If someone knows of a situation where we can 1659 * die... If someone knows of a situation where we can
1660 * recover from USBD_IOERROR, let me know. 1660 * recover from USBD_IOERROR, let me know.
1661 */ 1661 */
1662 splx(s); 1662 splx(s);
1663 return; 1663 return;
1664 } 1664 }
1665#endif /* 0 */ 1665#endif /* 0 */
1666 1666
1667 if (usbd_ratecheck(&sc->atu_rx_notice)) { 1667 if (usbd_ratecheck(&sc->atu_rx_notice)) {
1668 DPRINTF(("%s: usb error on rx: %s\n", 1668 DPRINTF(("%s: usb error on rx: %s\n",
1669 device_xname(sc->atu_dev), usbd_errstr(status))); 1669 device_xname(sc->atu_dev), usbd_errstr(status)));
1670 } 1670 }
1671 if (status == USBD_STALLED) 1671 if (status == USBD_STALLED)
1672 usbd_clear_endpoint_stall_async( 1672 usbd_clear_endpoint_stall_async(
1673 sc->atu_ep[ATU_ENDPT_RX]); 1673 sc->atu_ep[ATU_ENDPT_RX]);
1674 goto done; 1674 goto done;
1675 } 1675 }
1676 1676
1677 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 1677 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1678 1678
1679 if (len <= 1) { 1679 if (len <= 1) {
1680 DPRINTF(("%s: atu_rxeof: too short\n", 1680 DPRINTF(("%s: atu_rxeof: too short\n",
1681 device_xname(sc->atu_dev))); 1681 device_xname(sc->atu_dev)));
1682 goto done; 1682 goto done;
 1683 } else if (len > MCLBYTES) {
 1684 DPRINTF(("%s: atu_rxeof: too long\n",
 1685 device_xname(sc->atu_dev)));
 1686 goto done;
1683 } 1687 }
1684 1688
1685 h = (struct atu_rx_hdr *)c->atu_buf; 1689 h = (struct atu_rx_hdr *)c->atu_buf;
1686 len = UGETW(h->length) - 4; /* XXX magic number */ 1690 len = UGETW(h->length) - 4; /* XXX magic number */
1687 1691
1688 m = c->atu_mbuf; 1692 m = c->atu_mbuf;
1689 memcpy(mtod(m, char *), c->atu_buf + ATU_RX_HDRLEN, len); 1693 memcpy(mtod(m, char *), c->atu_buf + ATU_RX_HDRLEN, len);
1690 m_set_rcvif(m, ifp); 1694 m_set_rcvif(m, ifp);
1691 m->m_pkthdr.len = m->m_len = len; 1695 m->m_pkthdr.len = m->m_len = len;
1692 1696
1693 wh = mtod(m, struct ieee80211_frame_min *); 1697 wh = mtod(m, struct ieee80211_frame_min *);
1694 ni = ieee80211_find_rxnode(ic, wh); 1698 ni = ieee80211_find_rxnode(ic, wh);
1695 1699
1696 ifp->if_ipackets++; 1700 ifp->if_ipackets++;
1697 1701
1698 s = splnet(); 1702 s = splnet();
1699 1703
1700 if (atu_newbuf(sc, c, NULL) == ENOBUFS) { 1704 if (atu_newbuf(sc, c, NULL) == ENOBUFS) {
1701 ifp->if_ierrors++; 1705 ifp->if_ierrors++;
1702 goto done1; /* XXX if we can't allocate, why restart it? */ 1706 goto done1; /* XXX if we can't allocate, why restart it? */
1703 } 1707 }
1704 1708
1705 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1709 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1706 /* 1710 /*
1707 * WEP is decrypted by hardware. Clear WEP bit 1711 * WEP is decrypted by hardware. Clear WEP bit
1708 * header for ieee80211_input(). 1712 * header for ieee80211_input().
1709 */ 1713 */
1710 wh->i_fc[1] &= ~IEEE80211_FC1_WEP; 1714 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
1711 } 1715 }
1712 1716
1713 ieee80211_input(ic, m, ni, h->rssi, UGETDW(h->rx_time)); 1717 ieee80211_input(ic, m, ni, h->rssi, UGETDW(h->rx_time));
1714 1718
1715 ieee80211_free_node(ni); 1719 ieee80211_free_node(ni);
1716done1: 1720done1:
1717 splx(s); 1721 splx(s);
1718done: 1722done:
1719 /* Setup new transfer. */ 1723 /* Setup new transfer. */
1720 usbd_setup_xfer(c->atu_xfer, c, c->atu_buf, ATU_RX_BUFSZ, 1724 usbd_setup_xfer(c->atu_xfer, c, c->atu_buf, ATU_RX_BUFSZ,
1721 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, atu_rxeof); 1725 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, atu_rxeof);
1722 usbd_transfer(c->atu_xfer); 1726 usbd_transfer(c->atu_xfer);
1723} 1727}
1724 1728
1725/* 1729/*
1726 * A frame was downloaded to the chip. It's safe for us to clean up 1730 * A frame was downloaded to the chip. It's safe for us to clean up
1727 * the list buffers. 1731 * the list buffers.
1728 */ 1732 */
1729void 1733void
1730atu_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 1734atu_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1731{ 1735{
1732 struct atu_chain *c = (struct atu_chain *)priv; 1736 struct atu_chain *c = (struct atu_chain *)priv;
1733 struct atu_softc *sc = c->atu_sc; 1737 struct atu_softc *sc = c->atu_sc;
1734 struct ifnet *ifp = &sc->sc_if; 1738 struct ifnet *ifp = &sc->sc_if;
1735 usbd_status err; 1739 usbd_status err;
1736 int s; 1740 int s;
1737 1741
1738 DPRINTFN(25, ("%s: atu_txeof status=%d\n", device_xname(sc->atu_dev), 1742 DPRINTFN(25, ("%s: atu_txeof status=%d\n", device_xname(sc->atu_dev),
1739 status)); 1743 status));
1740 1744
1741 if (c->atu_mbuf) { 1745 if (c->atu_mbuf) {
1742 m_freem(c->atu_mbuf); 1746 m_freem(c->atu_mbuf);
1743 c->atu_mbuf = NULL; 1747 c->atu_mbuf = NULL;
1744 } 1748 }
1745 1749
1746 if (status != USBD_NORMAL_COMPLETION) { 1750 if (status != USBD_NORMAL_COMPLETION) {
1747 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1751 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1748 return; 1752 return;
1749 1753
1750 DPRINTF(("%s: usb error on tx: %s\n", 1754 DPRINTF(("%s: usb error on tx: %s\n",
1751 device_xname(sc->atu_dev), usbd_errstr(status))); 1755 device_xname(sc->atu_dev), usbd_errstr(status)));
1752 if (status == USBD_STALLED) 1756 if (status == USBD_STALLED)
1753 usbd_clear_endpoint_stall_async( 1757 usbd_clear_endpoint_stall_async(
1754 sc->atu_ep[ATU_ENDPT_TX]); 1758 sc->atu_ep[ATU_ENDPT_TX]);
1755 return; 1759 return;
1756 } 1760 }
1757 1761
1758 usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL, &err); 1762 usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL, &err);
1759 1763
1760 if (err) 1764 if (err)
1761 ifp->if_oerrors++; 1765 ifp->if_oerrors++;
1762 else 1766 else
1763 ifp->if_opackets++; 1767 ifp->if_opackets++;
1764 1768
1765 s = splnet(); 1769 s = splnet();
1766 SLIST_INSERT_HEAD(&sc->atu_cdata.atu_tx_free, c, atu_list); 1770 SLIST_INSERT_HEAD(&sc->atu_cdata.atu_tx_free, c, atu_list);
1767 sc->atu_cdata.atu_tx_inuse--; 1771 sc->atu_cdata.atu_tx_inuse--;
1768 if (sc->atu_cdata.atu_tx_inuse == 0) 1772 if (sc->atu_cdata.atu_tx_inuse == 0)
1769 ifp->if_timer = 0; 1773 ifp->if_timer = 0;
1770 ifp->if_flags &= ~IFF_OACTIVE; 1774 ifp->if_flags &= ~IFF_OACTIVE;
1771 splx(s); 1775 splx(s);
1772 1776
1773 atu_start(ifp); 1777 atu_start(ifp);
1774} 1778}
1775 1779
1776uint8_t 1780uint8_t
1777atu_calculate_padding(int size) 1781atu_calculate_padding(int size)
1778{ 1782{
1779 size %= 64; 1783 size %= 64;
1780 1784
1781 if (size < 50) 1785 if (size < 50)
1782 return 50 - size; 1786 return 50 - size;
1783 if (size >=61) 1787 if (size >=61)
1784 return 64 + 50 - size; 1788 return 64 + 50 - size;
1785 return 0; 1789 return 0;
1786} 1790}
1787 1791
1788int 1792int
1789atu_tx_start(struct atu_softc *sc, struct ieee80211_node *ni, 1793atu_tx_start(struct atu_softc *sc, struct ieee80211_node *ni,
1790 struct atu_chain *c, struct mbuf *m) 1794 struct atu_chain *c, struct mbuf *m)
1791{ 1795{
1792 int len; 1796 int len;
1793 struct atu_tx_hdr *h; 1797 struct atu_tx_hdr *h;
1794 usbd_status err; 1798 usbd_status err;
1795 uint8_t pad; 1799 uint8_t pad;
1796 1800
1797 DPRINTFN(25, ("%s: atu_tx_start\n", device_xname(sc->atu_dev))); 1801 DPRINTFN(25, ("%s: atu_tx_start\n", device_xname(sc->atu_dev)));
1798 1802
1799 /* Don't try to send when we're shutting down the driver */ 1803 /* Don't try to send when we're shutting down the driver */
1800 if (sc->sc_state != ATU_S_OK) { 1804 if (sc->sc_state != ATU_S_OK) {
1801 m_freem(m); 1805 m_freem(m);
1802 return EIO; 1806 return EIO;
1803 } 1807 }
1804 1808
1805 /* 1809 /*
1806 * Copy the mbuf data into a contiguous buffer, leaving 1810 * Copy the mbuf data into a contiguous buffer, leaving
1807 * enough room for the atmel headers 1811 * enough room for the atmel headers
1808 */ 1812 */
1809 len = m->m_pkthdr.len; 1813 len = m->m_pkthdr.len;
1810 1814
1811 m_copydata(m, 0, m->m_pkthdr.len, c->atu_buf + ATU_TX_HDRLEN); 1815 m_copydata(m, 0, m->m_pkthdr.len, c->atu_buf + ATU_TX_HDRLEN);
1812 1816
1813 h = (struct atu_tx_hdr *)c->atu_buf; 1817 h = (struct atu_tx_hdr *)c->atu_buf;
1814 memset(h, 0, ATU_TX_HDRLEN); 1818 memset(h, 0, ATU_TX_HDRLEN);
1815 USETW(h->length, len); 1819 USETW(h->length, len);
1816 h->tx_rate = 4; /* XXX rate = auto */ 1820 h->tx_rate = 4; /* XXX rate = auto */
1817 len += ATU_TX_HDRLEN; 1821 len += ATU_TX_HDRLEN;
1818 1822
1819 pad = atu_calculate_padding(len); 1823 pad = atu_calculate_padding(len);
1820 len += pad; 1824 len += pad;
1821 h->padding = pad; 1825 h->padding = pad;
1822 1826
1823 c->atu_length = len; 1827 c->atu_length = len;
1824 c->atu_mbuf = m; 1828 c->atu_mbuf = m;
1825 1829
1826 usbd_setup_xfer(c->atu_xfer, c, c->atu_buf, c->atu_length, 0, 1830 usbd_setup_xfer(c->atu_xfer, c, c->atu_buf, c->atu_length, 0,
1827 ATU_TX_TIMEOUT, atu_txeof); 1831 ATU_TX_TIMEOUT, atu_txeof);
1828 1832
1829 /* Let's get this thing into the air! */ 1833 /* Let's get this thing into the air! */
1830 c->atu_in_xfer = 1; 1834 c->atu_in_xfer = 1;
1831 err = usbd_transfer(c->atu_xfer); 1835 err = usbd_transfer(c->atu_xfer);
1832 if (err != USBD_IN_PROGRESS) { 1836 if (err != USBD_IN_PROGRESS) {
1833 DPRINTFN(25, ("%s: atu_tx_start, err=%d", 1837 DPRINTFN(25, ("%s: atu_tx_start, err=%d",
1834 device_xname(sc->atu_dev), err)); 1838 device_xname(sc->atu_dev), err));
1835 c->atu_mbuf = NULL; 1839 c->atu_mbuf = NULL;
1836 m_freem(m); 1840 m_freem(m);
1837 return EIO; 1841 return EIO;
1838 } 1842 }
1839 1843
1840 return 0; 1844 return 0;
1841} 1845}
1842 1846
1843void 1847void
1844atu_start(struct ifnet *ifp) 1848atu_start(struct ifnet *ifp)
1845{ 1849{
1846 struct atu_softc *sc = ifp->if_softc; 1850 struct atu_softc *sc = ifp->if_softc;
1847 struct ieee80211com *ic = &sc->sc_ic; 1851 struct ieee80211com *ic = &sc->sc_ic;
1848 struct atu_cdata *cd = &sc->atu_cdata; 1852 struct atu_cdata *cd = &sc->atu_cdata;
1849 struct ieee80211_node *ni; 1853 struct ieee80211_node *ni;
1850 struct atu_chain *c; 1854 struct atu_chain *c;
1851 struct mbuf *m = NULL; 1855 struct mbuf *m = NULL;
1852 int s; 1856 int s;
1853 1857
1854 DPRINTFN(25, ("%s: atu_start: enter\n", device_xname(sc->atu_dev))); 1858 DPRINTFN(25, ("%s: atu_start: enter\n", device_xname(sc->atu_dev)));
1855 1859
1856 if ((ifp->if_flags & IFF_RUNNING) == 0) { 1860 if ((ifp->if_flags & IFF_RUNNING) == 0) {
1857 return; 1861 return;
1858 } 1862 }
1859 if (ifp->if_flags & IFF_OACTIVE) { 1863 if (ifp->if_flags & IFF_OACTIVE) {
1860 DPRINTFN(30, ("%s: atu_start: IFF_OACTIVE\n", 1864 DPRINTFN(30, ("%s: atu_start: IFF_OACTIVE\n",
1861 device_xname(sc->atu_dev))); 1865 device_xname(sc->atu_dev)));
1862 return; 1866 return;
1863 } 1867 }
1864 1868
1865 for (;;) { 1869 for (;;) {
1866 /* grab a TX buffer */ 1870 /* grab a TX buffer */
1867 s = splnet(); 1871 s = splnet();
1868 c = SLIST_FIRST(&cd->atu_tx_free); 1872 c = SLIST_FIRST(&cd->atu_tx_free);
1869 if (c != NULL) { 1873 if (c != NULL) {
1870 SLIST_REMOVE_HEAD(&cd->atu_tx_free, atu_list); 1874 SLIST_REMOVE_HEAD(&cd->atu_tx_free, atu_list);
1871 cd->atu_tx_inuse++; 1875 cd->atu_tx_inuse++;
1872 if (cd->atu_tx_inuse == ATU_TX_LIST_CNT) 1876 if (cd->atu_tx_inuse == ATU_TX_LIST_CNT)
1873 ifp->if_flags |= IFF_OACTIVE; 1877 ifp->if_flags |= IFF_OACTIVE;
1874 } 1878 }
1875 splx(s); 1879 splx(s);
1876 if (c == NULL) { 1880 if (c == NULL) {
1877 DPRINTFN(10, ("%s: out of tx xfers\n", 1881 DPRINTFN(10, ("%s: out of tx xfers\n",
1878 device_xname(sc->atu_dev))); 1882 device_xname(sc->atu_dev)));
1879 ifp->if_flags |= IFF_OACTIVE; 1883 ifp->if_flags |= IFF_OACTIVE;
1880 break; 1884 break;
1881 } 1885 }
1882 1886
1883 /* 1887 /*
1884 * Poll the management queue for frames, it has priority over 1888 * Poll the management queue for frames, it has priority over
1885 * normal data frames. 1889 * normal data frames.
1886 */ 1890 */
1887 IF_DEQUEUE(&ic->ic_mgtq, m); 1891 IF_DEQUEUE(&ic->ic_mgtq, m);
1888 if (m == NULL) { 1892 if (m == NULL) {
1889 DPRINTFN(10, ("%s: atu_start: data packet\n", 1893 DPRINTFN(10, ("%s: atu_start: data packet\n",
1890 device_xname(sc->atu_dev))); 1894 device_xname(sc->atu_dev)));
1891 if (ic->ic_state != IEEE80211_S_RUN) { 1895 if (ic->ic_state != IEEE80211_S_RUN) {
1892 DPRINTFN(25, ("%s: no data till running\n", 1896 DPRINTFN(25, ("%s: no data till running\n",
1893 device_xname(sc->atu_dev))); 1897 device_xname(sc->atu_dev)));
1894 /* put the xfer back on the list */ 1898 /* put the xfer back on the list */
1895 s = splnet(); 1899 s = splnet();
1896 SLIST_INSERT_HEAD(&cd->atu_tx_free, c, 1900 SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1897 atu_list); 1901 atu_list);
1898 cd->atu_tx_inuse--; 1902 cd->atu_tx_inuse--;
1899 splx(s); 1903 splx(s);
1900 break; 1904 break;
1901 } 1905 }
1902 1906
1903 IFQ_DEQUEUE(&ifp->if_snd, m); 1907 IFQ_DEQUEUE(&ifp->if_snd, m);
1904 if (m == NULL) { 1908 if (m == NULL) {
1905 DPRINTFN(25, ("%s: nothing to send\n", 1909 DPRINTFN(25, ("%s: nothing to send\n",
1906 device_xname(sc->atu_dev))); 1910 device_xname(sc->atu_dev)));
1907 s = splnet(); 1911 s = splnet();
1908 SLIST_INSERT_HEAD(&cd->atu_tx_free, c, 1912 SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1909 atu_list); 1913 atu_list);
1910 cd->atu_tx_inuse--; 1914 cd->atu_tx_inuse--;
1911 splx(s); 1915 splx(s);
1912 break; 1916 break;
1913 } 1917 }
1914 bpf_mtap(ifp, m, BPF_D_OUT); 1918 bpf_mtap(ifp, m, BPF_D_OUT);
1915 ni = ieee80211_find_txnode(ic, 1919 ni = ieee80211_find_txnode(ic,
1916 mtod(m, struct ether_header *)->ether_dhost); 1920 mtod(m, struct ether_header *)->ether_dhost);
1917 if (ni == NULL) { 1921 if (ni == NULL) {
1918 m_freem(m); 1922 m_freem(m);
1919 goto bad; 1923 goto bad;
1920 } 1924 }
1921 m = ieee80211_encap(ic, m, ni); 1925 m = ieee80211_encap(ic, m, ni);
1922 if (m == NULL) 1926 if (m == NULL)
1923 goto bad; 1927 goto bad;
1924 } else { 1928 } else {
1925 DPRINTFN(25, ("%s: atu_start: mgmt packet\n", 1929 DPRINTFN(25, ("%s: atu_start: mgmt packet\n",
1926 device_xname(sc->atu_dev))); 1930 device_xname(sc->atu_dev)));
1927 1931
1928 /* 1932 /*
1929 * Hack! The referenced node pointer is in the 1933 * Hack! The referenced node pointer is in the
1930 * rcvif field of the packet header. This is 1934 * rcvif field of the packet header. This is
1931 * placed there by ieee80211_mgmt_output because 1935 * placed there by ieee80211_mgmt_output because
1932 * we need to hold the reference with the frame 1936 * we need to hold the reference with the frame
1933 * and there's no other way (other than packet 1937 * and there's no other way (other than packet
1934 * tags which we consider too expensive to use) 1938 * tags which we consider too expensive to use)
1935 * to pass it along. 1939 * to pass it along.
1936 */ 1940 */
1937 ni = M_GETCTX(m, struct ieee80211_node *); 1941 ni = M_GETCTX(m, struct ieee80211_node *);
1938 M_CLEARCTX(m); 1942 M_CLEARCTX(m);
1939 1943
1940 /* sc->sc_stats.ast_tx_mgmt++; */ 1944 /* sc->sc_stats.ast_tx_mgmt++; */
1941 } 1945 }
1942 1946
1943 bpf_mtap3(ic->ic_rawbpf, m, BPF_D_OUT); 1947 bpf_mtap3(ic->ic_rawbpf, m, BPF_D_OUT);
1944 1948
1945 if (atu_tx_start(sc, ni, c, m)) { 1949 if (atu_tx_start(sc, ni, c, m)) {
1946bad: 1950bad:
1947 s = splnet(); 1951 s = splnet();
1948 SLIST_INSERT_HEAD(&cd->atu_tx_free, c, 1952 SLIST_INSERT_HEAD(&cd->atu_tx_free, c,
1949 atu_list); 1953 atu_list);
1950 cd->atu_tx_inuse--; 1954 cd->atu_tx_inuse--;
1951 splx(s); 1955 splx(s);
1952 /* ifp_if_oerrors++; */ 1956 /* ifp_if_oerrors++; */
1953 if (ni != NULL) 1957 if (ni != NULL)
1954 ieee80211_free_node(ni); 1958 ieee80211_free_node(ni);
1955 continue; 1959 continue;
1956 } 1960 }
1957 ifp->if_timer = 5; 1961 ifp->if_timer = 5;
1958 } 1962 }
1959} 1963}
1960 1964
1961int 1965int
1962atu_init(struct ifnet *ifp) 1966atu_init(struct ifnet *ifp)
1963{ 1967{
1964 struct atu_softc *sc = ifp->if_softc; 1968 struct atu_softc *sc = ifp->if_softc;
1965 struct ieee80211com *ic = &sc->sc_ic; 1969 struct ieee80211com *ic = &sc->sc_ic;
1966 struct atu_chain *c; 1970 struct atu_chain *c;
1967 usbd_status err; 1971 usbd_status err;
1968 int i, s; 1972 int i, s;
1969 1973
1970 s = splnet(); 1974 s = splnet();
1971 1975
1972 DPRINTFN(10, ("%s: atu_init\n", device_xname(sc->atu_dev))); 1976 DPRINTFN(10, ("%s: atu_init\n", device_xname(sc->atu_dev)));
1973 1977
1974 if (ifp->if_flags & IFF_RUNNING) { 1978 if (ifp->if_flags & IFF_RUNNING) {
1975 splx(s); 1979 splx(s);
1976 return 0; 1980 return 0;
1977 } 1981 }
1978 1982
1979 /* Load the multicast filter. */ 1983 /* Load the multicast filter. */
1980 /*atu_setmulti(sc); */ 1984 /*atu_setmulti(sc); */
1981 1985
1982 /* Open RX and TX pipes. */ 1986 /* Open RX and TX pipes. */
1983 err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_RX], 1987 err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_RX],
1984 USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_RX]); 1988 USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_RX]);
1985 if (err) { 1989 if (err) {
1986 DPRINTF(("%s: open rx pipe failed: %s\n", 1990 DPRINTF(("%s: open rx pipe failed: %s\n",
1987 device_xname(sc->atu_dev), usbd_errstr(err))); 1991 device_xname(sc->atu_dev), usbd_errstr(err)));
1988 splx(s); 1992 splx(s);
1989 return EIO; 1993 return EIO;
1990 } 1994 }
1991 1995
1992 err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_TX], 1996 err = usbd_open_pipe(sc->atu_iface, sc->atu_ed[ATU_ENDPT_TX],
1993 USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_TX]); 1997 USBD_EXCLUSIVE_USE, &sc->atu_ep[ATU_ENDPT_TX]);
1994 if (err) { 1998 if (err) {
1995 DPRINTF(("%s: open tx pipe failed: %s\n", 1999 DPRINTF(("%s: open tx pipe failed: %s\n",
1996 device_xname(sc->atu_dev), usbd_errstr(err))); 2000 device_xname(sc->atu_dev), usbd_errstr(err)));
1997 splx(s); 2001 splx(s);
1998 return EIO; 2002 return EIO;
1999 } 2003 }
2000 2004
2001 /* Init TX ring */ 2005 /* Init TX ring */
2002 if (atu_tx_list_init(sc)) 2006 if (atu_tx_list_init(sc))
2003 printf("%s: tx list init failed\n", device_xname(sc->atu_dev)); 2007 printf("%s: tx list init failed\n", device_xname(sc->atu_dev));
2004 2008
2005 /* Init RX ring */ 2009 /* Init RX ring */
2006 if (atu_rx_list_init(sc)) 2010 if (atu_rx_list_init(sc))
2007 printf("%s: rx list init failed\n", device_xname(sc->atu_dev)); 2011 printf("%s: rx list init failed\n", device_xname(sc->atu_dev));
2008 2012
2009 /* Start up the receive pipe. */ 2013 /* Start up the receive pipe. */
2010 for (i = 0; i < ATU_RX_LIST_CNT; i++) { 2014 for (i = 0; i < ATU_RX_LIST_CNT; i++) {
2011 c = &sc->atu_cdata.atu_rx_chain[i]; 2015 c = &sc->atu_cdata.atu_rx_chain[i];
2012 2016
2013 usbd_setup_xfer(c->atu_xfer, c, c->atu_buf, ATU_RX_BUFSZ, 2017 usbd_setup_xfer(c->atu_xfer, c, c->atu_buf, ATU_RX_BUFSZ,
2014 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, atu_rxeof); 2018 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, atu_rxeof);
2015 usbd_transfer(c->atu_xfer); 2019 usbd_transfer(c->atu_xfer);
2016 } 2020 }
2017 2021
2018 DPRINTFN(10, ("%s: starting up using MAC=%s\n", 2022 DPRINTFN(10, ("%s: starting up using MAC=%s\n",
2019 device_xname(sc->atu_dev), ether_sprintf(ic->ic_myaddr))); 2023 device_xname(sc->atu_dev), ether_sprintf(ic->ic_myaddr)));
2020 2024
2021 /* Do initial setup */ 2025 /* Do initial setup */
2022 err = atu_initial_config(sc); 2026 err = atu_initial_config(sc);
2023 if (err) { 2027 if (err) {
2024 DPRINTF(("%s: initial config failed!\n", 2028 DPRINTF(("%s: initial config failed!\n",
2025 device_xname(sc->atu_dev))); 2029 device_xname(sc->atu_dev)));
2026 splx(s); 2030 splx(s);
2027 return EIO; 2031 return EIO;
2028 } 2032 }
2029 DPRINTFN(10, ("%s: initialised transceiver\n", 2033 DPRINTFN(10, ("%s: initialised transceiver\n",
2030 device_xname(sc->atu_dev))); 2034 device_xname(sc->atu_dev)));
2031 2035
2032 /* sc->atu_rxfilt = ATU_RXFILT_UNICAST|ATU_RXFILT_BROADCAST; */ 2036 /* sc->atu_rxfilt = ATU_RXFILT_UNICAST|ATU_RXFILT_BROADCAST; */
2033 2037
2034 /* If we want promiscuous mode, set the allframes bit. */ 2038 /* If we want promiscuous mode, set the allframes bit. */
2035 /* 2039 /*
2036 if (ifp->if_flags & IFF_PROMISC) 2040 if (ifp->if_flags & IFF_PROMISC)
2037 sc->atu_rxfilt |= ATU_RXFILT_PROMISC; 2041 sc->atu_rxfilt |= ATU_RXFILT_PROMISC;
2038 */ 2042 */
2039 2043
2040 ifp->if_flags |= IFF_RUNNING; 2044 ifp->if_flags |= IFF_RUNNING;
2041 ifp->if_flags &= ~IFF_OACTIVE; 2045 ifp->if_flags &= ~IFF_OACTIVE;
2042 splx(s); 2046 splx(s);
2043 2047
2044 /* XXX the following HAS to be replaced */ 2048 /* XXX the following HAS to be replaced */
2045 s = splnet(); 2049 s = splnet();
2046 err = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2050 err = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2047 if (err) { 2051 if (err) {
2048 DPRINTFN(1, ("%s: atu_init: error calling " 2052 DPRINTFN(1, ("%s: atu_init: error calling "
2049 "ieee80211_net_state", device_xname(sc->atu_dev))); 2053 "ieee80211_net_state", device_xname(sc->atu_dev)));
2050 } 2054 }
2051 splx(s); 2055 splx(s);
2052 2056
2053 return 0; 2057 return 0;
2054} 2058}
2055 2059
2056#ifdef ATU_DEBUG 2060#ifdef ATU_DEBUG
2057void 2061void
2058atu_debug_print(struct atu_softc *sc) 2062atu_debug_print(struct atu_softc *sc)
2059{ 2063{
2060 usbd_status err; 2064 usbd_status err;
2061 uint8_t tmp[32]; 2065 uint8_t tmp[32];
2062 2066
2063 /* DEBUG */ 2067 /* DEBUG */
2064 if ((err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, tmp))) 2068 if ((err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_BSSID, tmp)))
2065 return; 2069 return;
2066 DPRINTF(("%s: DEBUG: current BSSID=%s\n", device_xname(sc->atu_dev), 2070 DPRINTF(("%s: DEBUG: current BSSID=%s\n", device_xname(sc->atu_dev),
2067 ether_sprintf(tmp))); 2071 ether_sprintf(tmp)));
2068 2072
2069 if ((err = atu_get_mib(sc, MIB_MAC_MGMT__BEACON_PERIOD, tmp))) 2073 if ((err = atu_get_mib(sc, MIB_MAC_MGMT__BEACON_PERIOD, tmp)))
2070 return; 2074 return;
2071 DPRINTF(("%s: DEBUG: beacon period=%d\n", device_xname(sc->atu_dev), 2075 DPRINTF(("%s: DEBUG: beacon period=%d\n", device_xname(sc->atu_dev),
2072 tmp[0])); 2076 tmp[0]));
2073 2077
2074 if ((err = atu_get_mib(sc, MIB_MAC_WEP__PRIVACY_INVOKED, tmp))) 2078 if ((err = atu_get_mib(sc, MIB_MAC_WEP__PRIVACY_INVOKED, tmp)))
2075 return; 2079 return;
2076 DPRINTF(("%s: DEBUG: privacy invoked=%d\n", device_xname(sc->atu_dev), 2080 DPRINTF(("%s: DEBUG: privacy invoked=%d\n", device_xname(sc->atu_dev),
2077 tmp[0])); 2081 tmp[0]));
2078 2082
2079 if ((err = atu_get_mib(sc, MIB_MAC_WEP__ENCR_LEVEL, tmp))) 2083 if ((err = atu_get_mib(sc, MIB_MAC_WEP__ENCR_LEVEL, tmp)))
2080 return; 2084 return;
2081 DPRINTF(("%s: DEBUG: encr_level=%d\n", device_xname(sc->atu_dev), 2085 DPRINTF(("%s: DEBUG: encr_level=%d\n", device_xname(sc->atu_dev),
2082 tmp[0])); 2086 tmp[0]));
2083 2087
2084 if ((err = atu_get_mib(sc, MIB_MAC_WEP__ICV_ERROR_COUNT, tmp))) 2088 if ((err = atu_get_mib(sc, MIB_MAC_WEP__ICV_ERROR_COUNT, tmp)))
2085 return; 2089 return;
2086 DPRINTF(("%s: DEBUG: icv error count=%d\n", device_xname(sc->atu_dev), 2090 DPRINTF(("%s: DEBUG: icv error count=%d\n", device_xname(sc->atu_dev),
2087 *(short *)tmp)); 2091 *(short *)tmp));
2088 2092
2089 if ((err = atu_get_mib(sc, MIB_MAC_WEP__EXCLUDED_COUNT, tmp))) 2093 if ((err = atu_get_mib(sc, MIB_MAC_WEP__EXCLUDED_COUNT, tmp)))
2090 return; 2094 return;
2091 DPRINTF(("%s: DEBUG: wep excluded count=%d\n", 2095 DPRINTF(("%s: DEBUG: wep excluded count=%d\n",
2092 device_xname(sc->atu_dev), *(short *)tmp)); 2096 device_xname(sc->atu_dev), *(short *)tmp));
2093 2097
2094 if ((err = atu_get_mib(sc, MIB_MAC_MGMT__POWER_MODE, tmp))) 2098 if ((err = atu_get_mib(sc, MIB_MAC_MGMT__POWER_MODE, tmp)))
2095 return; 2099 return;
2096 DPRINTF(("%s: DEBUG: power mode=%d\n", device_xname(sc->atu_dev), 2100 DPRINTF(("%s: DEBUG: power mode=%d\n", device_xname(sc->atu_dev),
2097 tmp[0])); 2101 tmp[0]));
2098 2102
2099 if ((err = atu_get_mib(sc, MIB_PHY__CHANNEL, tmp))) 2103 if ((err = atu_get_mib(sc, MIB_PHY__CHANNEL, tmp)))
2100 return; 2104 return;
2101 DPRINTF(("%s: DEBUG: channel=%d\n", device_xname(sc->atu_dev), tmp[0])); 2105 DPRINTF(("%s: DEBUG: channel=%d\n", device_xname(sc->atu_dev), tmp[0]));
2102 2106
2103 if ((err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, tmp))) 2107 if ((err = atu_get_mib(sc, MIB_PHY__REG_DOMAIN, tmp)))
2104 return; 2108 return;
2105 DPRINTF(("%s: DEBUG: reg domain=%d\n", device_xname(sc->atu_dev), 2109 DPRINTF(("%s: DEBUG: reg domain=%d\n", device_xname(sc->atu_dev),
2106 tmp[0])); 2110 tmp[0]));
2107 2111
2108 if ((err = atu_get_mib(sc, MIB_LOCAL__SSID_SIZE, tmp))) 2112 if ((err = atu_get_mib(sc, MIB_LOCAL__SSID_SIZE, tmp)))
2109 return; 2113 return;
2110 DPRINTF(("%s: DEBUG: ssid size=%d\n", device_xname(sc->atu_dev), 2114 DPRINTF(("%s: DEBUG: ssid size=%d\n", device_xname(sc->atu_dev),
2111 tmp[0])); 2115 tmp[0]));
2112 2116
2113 if ((err = atu_get_mib(sc, MIB_LOCAL__BEACON_ENABLE, tmp))) 2117 if ((err = atu_get_mib(sc, MIB_LOCAL__BEACON_ENABLE, tmp)))
2114 return; 2118 return;
2115 DPRINTF(("%s: DEBUG: beacon enable=%d\n", device_xname(sc->atu_dev), 2119 DPRINTF(("%s: DEBUG: beacon enable=%d\n", device_xname(sc->atu_dev),
2116 tmp[0])); 2120 tmp[0]));
2117 2121
2118 if ((err = atu_get_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, tmp))) 2122 if ((err = atu_get_mib(sc, MIB_LOCAL__AUTO_RATE_FALLBACK, tmp)))
2119 return; 2123 return;
2120 DPRINTF(("%s: DEBUG: auto rate fallback=%d\n", 2124 DPRINTF(("%s: DEBUG: auto rate fallback=%d\n",
2121 device_xname(sc->atu_dev), tmp[0])); 2125 device_xname(sc->atu_dev), tmp[0]));
2122 2126
2123 if ((err = atu_get_mib(sc, MIB_MAC_ADDR__ADDR, tmp))) 2127 if ((err = atu_get_mib(sc, MIB_MAC_ADDR__ADDR, tmp)))
2124 return; 2128 return;
2125 DPRINTF(("%s: DEBUG: mac addr=%s\n", device_xname(sc->atu_dev), 2129 DPRINTF(("%s: DEBUG: mac addr=%s\n", device_xname(sc->atu_dev),
2126 ether_sprintf(tmp))); 2130 ether_sprintf(tmp)));
2127 2131
2128 if ((err = atu_get_mib(sc, MIB_MAC__DESIRED_SSID, tmp))) 2132 if ((err = atu_get_mib(sc, MIB_MAC__DESIRED_SSID, tmp)))
2129 return; 2133 return;
2130 DPRINTF(("%s: DEBUG: desired ssid=%s\n", device_xname(sc->atu_dev), 2134 DPRINTF(("%s: DEBUG: desired ssid=%s\n", device_xname(sc->atu_dev),
2131 tmp)); 2135 tmp));
2132 2136
2133 if ((err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_ESSID, tmp))) 2137 if ((err = atu_get_mib(sc, MIB_MAC_MGMT__CURRENT_ESSID, tmp)))
2134 return; 2138 return;
2135 DPRINTF(("%s: DEBUG: current ESSID=%s\n", device_xname(sc->atu_dev), 2139 DPRINTF(("%s: DEBUG: current ESSID=%s\n", device_xname(sc->atu_dev),
2136 tmp)); 2140 tmp));
2137} 2141}
2138#endif /* ATU_DEBUG */ 2142#endif /* ATU_DEBUG */
2139 2143
2140int 2144int
2141atu_ioctl(struct ifnet *ifp, u_long command, void *data) 2145atu_ioctl(struct ifnet *ifp, u_long command, void *data)
2142{ 2146{
2143 struct atu_softc *sc = ifp->if_softc; 2147 struct atu_softc *sc = ifp->if_softc;
2144 struct ifreq *ifr = (struct ifreq *)data; 2148 struct ifreq *ifr = (struct ifreq *)data;
2145 struct ieee80211com *ic = &sc->sc_ic; 2149 struct ieee80211com *ic = &sc->sc_ic;
2146 int err = 0, s; 2150 int err = 0, s;
2147 2151
2148 s = splnet(); 2152 s = splnet();
2149 switch (command) { 2153 switch (command) {
2150 case SIOCSIFMEDIA: 2154 case SIOCSIFMEDIA:
2151 case SIOCGIFMEDIA: 2155 case SIOCGIFMEDIA:
2152 err = ifmedia_ioctl(ifp, ifr, &ic->ic_media, command); 2156 err = ifmedia_ioctl(ifp, ifr, &ic->ic_media, command);
2153 break; 2157 break;
2154 2158
2155 default: 2159 default:
2156 DPRINTFN(15, ("%s: ieee80211_ioctl (%lu)\n", 2160 DPRINTFN(15, ("%s: ieee80211_ioctl (%lu)\n",
2157 device_xname(sc->atu_dev), command)); 2161 device_xname(sc->atu_dev), command));
2158 err = ieee80211_ioctl(ic, command, data); 2162 err = ieee80211_ioctl(ic, command, data);
2159 break; 2163 break;
2160 } 2164 }
2161 2165
2162 if (err == ENETRESET) { 2166 if (err == ENETRESET) {
2163 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == 2167 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
2164 (IFF_RUNNING|IFF_UP)) { 2168 (IFF_RUNNING|IFF_UP)) {
2165 DPRINTF(("%s: atu_ioctl(): netreset %lu\n", 2169 DPRINTF(("%s: atu_ioctl(): netreset %lu\n",
2166 device_xname(sc->atu_dev), command)); 2170 device_xname(sc->atu_dev), command));
2167 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2171 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2168 atu_initial_config(sc); 2172 atu_initial_config(sc);
2169 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2173 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2170 } 2174 }
2171 err = 0; 2175 err = 0;
2172 } 2176 }
2173 2177
2174 splx(s); 2178 splx(s);
2175 return err; 2179 return err;
2176} 2180}
2177 2181
2178void 2182void
2179atu_watchdog(struct ifnet *ifp) 2183atu_watchdog(struct ifnet *ifp)
2180{ 2184{
2181 struct atu_softc *sc = ifp->if_softc; 2185 struct atu_softc *sc = ifp->if_softc;
2182 struct atu_chain *c; 2186 struct atu_chain *c;
2183 usbd_status stat; 2187 usbd_status stat;
2184 int cnt, s; 2188 int cnt, s;
2185 2189
2186 DPRINTF(("%s: atu_watchdog\n", device_xname(sc->atu_dev))); 2190 DPRINTF(("%s: atu_watchdog\n", device_xname(sc->atu_dev)));
2187 2191
2188 ifp->if_timer = 0; 2192 ifp->if_timer = 0;
2189 2193
2190 if (sc->sc_state != ATU_S_OK || (ifp->if_flags & IFF_RUNNING) == 0) 2194 if (sc->sc_state != ATU_S_OK || (ifp->if_flags & IFF_RUNNING) == 0)
2191 return; 2195 return;
2192 2196
2193 sc = ifp->if_softc; 2197 sc = ifp->if_softc;
2194 s = splnet(); 2198 s = splnet();
2195 ifp->if_oerrors++; 2199 ifp->if_oerrors++;
2196 DPRINTF(("%s: watchdog timeout\n", device_xname(sc->atu_dev))); 2200 DPRINTF(("%s: watchdog timeout\n", device_xname(sc->atu_dev)));
2197 2201
2198 /* 2202 /*
2199 * TODO: 2203 * TODO:
2200 * we should change this since we have multiple TX tranfers... 2204 * we should change this since we have multiple TX tranfers...
2201 */ 2205 */
2202 for (cnt = 0; cnt < ATU_TX_LIST_CNT; cnt++) { 2206 for (cnt = 0; cnt < ATU_TX_LIST_CNT; cnt++) {
2203 c = &sc->atu_cdata.atu_tx_chain[cnt]; 2207 c = &sc->atu_cdata.atu_tx_chain[cnt];
2204 if (c->atu_in_xfer) { 2208 if (c->atu_in_xfer) {
2205 usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL, 2209 usbd_get_xfer_status(c->atu_xfer, NULL, NULL, NULL,
2206 &stat); 2210 &stat);
2207 atu_txeof(c->atu_xfer, c, stat); 2211 atu_txeof(c->atu_xfer, c, stat);
2208 } 2212 }
2209 } 2213 }
2210 2214
2211 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 2215 if (!IFQ_IS_EMPTY(&ifp->if_snd))
2212 atu_start(ifp); 2216 atu_start(ifp);
2213 splx(s); 2217 splx(s);
2214 2218
2215 ieee80211_watchdog(&sc->sc_ic); 2219 ieee80211_watchdog(&sc->sc_ic);
2216} 2220}
2217 2221
2218/* 2222/*
2219 * Stop the adapter and free any mbufs allocated to the 2223 * Stop the adapter and free any mbufs allocated to the
2220 * RX and TX lists. 2224 * RX and TX lists.
2221 */ 2225 */
2222void 2226void
2223atu_stop(struct ifnet *ifp, int disable) 2227atu_stop(struct ifnet *ifp, int disable)
2224{ 2228{
2225 struct atu_softc *sc = ifp->if_softc; 2229 struct atu_softc *sc = ifp->if_softc;
2226 struct ieee80211com *ic = &sc->sc_ic; 2230 struct ieee80211com *ic = &sc->sc_ic;
2227 struct atu_cdata *cd; 2231 struct atu_cdata *cd;
2228 usbd_status err; 2232 usbd_status err;
2229 int s; 2233 int s;
2230 2234
2231 s = splnet(); 2235 s = splnet();
2232 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2236 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2233 ifp->if_timer = 0; 2237 ifp->if_timer = 0;
2234 2238
2235 usb_rem_task_wait(sc->atu_udev, &sc->sc_task, USB_TASKQ_DRIVER, NULL); 2239 usb_rem_task_wait(sc->atu_udev, &sc->sc_task, USB_TASKQ_DRIVER, NULL);
2236 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2240 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2237 2241
2238 /* Stop transfers. */ 2242 /* Stop transfers. */
2239 if (sc->atu_ep[ATU_ENDPT_RX] != NULL) { 2243 if (sc->atu_ep[ATU_ENDPT_RX] != NULL) {
2240 err = usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_RX]); 2244 err = usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_RX]);
2241 if (err) { 2245 if (err) {
2242 DPRINTF(("%s: abort rx pipe failed: %s\n", 2246 DPRINTF(("%s: abort rx pipe failed: %s\n",
2243 device_xname(sc->atu_dev), usbd_errstr(err))); 2247 device_xname(sc->atu_dev), usbd_errstr(err)));
2244 } 2248 }
2245 } 2249 }
2246 2250
2247 if (sc->atu_ep[ATU_ENDPT_TX] != NULL) { 2251 if (sc->atu_ep[ATU_ENDPT_TX] != NULL) {
2248 err = usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_TX]); 2252 err = usbd_abort_pipe(sc->atu_ep[ATU_ENDPT_TX]);
2249 if (err) { 2253 if (err) {
2250 DPRINTF(("%s: abort tx pipe failed: %s\n", 2254 DPRINTF(("%s: abort tx pipe failed: %s\n",
2251 device_xname(sc->atu_dev), usbd_errstr(err))); 2255 device_xname(sc->atu_dev), usbd_errstr(err)));
2252 } 2256 }
2253 } 2257 }
2254 2258
2255 /* Free RX/TX/MGMT list resources. */ 2259 /* Free RX/TX/MGMT list resources. */
2256 cd = &sc->atu_cdata; 2260 cd = &sc->atu_cdata;
2257 atu_xfer_list_free(sc, cd->atu_rx_chain, ATU_RX_LIST_CNT); 2261 atu_xfer_list_free(sc, cd->atu_rx_chain, ATU_RX_LIST_CNT);
2258 atu_xfer_list_free(sc, cd->atu_tx_chain, ATU_TX_LIST_CNT); 2262 atu_xfer_list_free(sc, cd->atu_tx_chain, ATU_TX_LIST_CNT);
2259 2263
2260 /* Close pipes */ 2264 /* Close pipes */
2261 if (sc->atu_ep[ATU_ENDPT_RX] != NULL) { 2265 if (sc->atu_ep[ATU_ENDPT_RX] != NULL) {
2262 err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_RX]); 2266 err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_RX]);
2263 if (err) { 2267 if (err) {
2264 DPRINTF(("%s: close rx pipe failed: %s\n", 2268 DPRINTF(("%s: close rx pipe failed: %s\n",
2265 device_xname(sc->atu_dev), usbd_errstr(err))); 2269 device_xname(sc->atu_dev), usbd_errstr(err)));
2266 } 2270 }
2267 sc->atu_ep[ATU_ENDPT_RX] = NULL; 2271 sc->atu_ep[ATU_ENDPT_RX] = NULL;
2268 } 2272 }
2269 2273
2270 if (sc->atu_ep[ATU_ENDPT_TX] != NULL) { 2274 if (sc->atu_ep[ATU_ENDPT_TX] != NULL) {
2271 err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_TX]); 2275 err = usbd_close_pipe(sc->atu_ep[ATU_ENDPT_TX]);
2272 if (err) { 2276 if (err) {
2273 DPRINTF(("%s: close tx pipe failed: %s\n", 2277 DPRINTF(("%s: close tx pipe failed: %s\n",
2274 device_xname(sc->atu_dev), usbd_errstr(err))); 2278 device_xname(sc->atu_dev), usbd_errstr(err)));
2275 } 2279 }
2276 sc->atu_ep[ATU_ENDPT_TX] = NULL; 2280 sc->atu_ep[ATU_ENDPT_TX] = NULL;
2277 } 2281 }
2278 2282
2279 /* Let's be nice and turn off the radio before we leave */ 2283 /* Let's be nice and turn off the radio before we leave */
2280 atu_switch_radio(sc, 0); 2284 atu_switch_radio(sc, 0);
2281 2285
2282 splx(s); 2286 splx(s);
2283} 2287}

cvs diff -r1.25.2.4 -r1.25.2.5 src/sys/dev/usb/usbnet.c (switch to unified diff)

--- src/sys/dev/usb/usbnet.c 2019/12/17 12:55:10 1.25.2.4
+++ src/sys/dev/usb/usbnet.c 2020/08/28 19:36:34 1.25.2.5
@@ -1,1228 +1,1231 @@ @@ -1,1228 +1,1231 @@
1/* $NetBSD: usbnet.c,v 1.25.2.4 2019/12/17 12:55:10 martin Exp $ */ 1/* $NetBSD: usbnet.c,v 1.25.2.5 2020/08/28 19:36:34 martin Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2019 Matthew R. Green 4 * Copyright (c) 2019 Matthew R. Green
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products 15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission. 16 * derived from this software without specific prior written permission.
17 * 17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE. 28 * SUCH DAMAGE.
29 */ 29 */
30 30
31/* 31/*
32 * Common code shared between USB network drivers. 32 * Common code shared between USB network drivers.
33 */ 33 */
34 34
35#include <sys/cdefs.h> 35#include <sys/cdefs.h>
36__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.25.2.4 2019/12/17 12:55:10 martin Exp $"); 36__KERNEL_RCSID(0, "$NetBSD: usbnet.c,v 1.25.2.5 2020/08/28 19:36:34 martin Exp $");
37 37
38#include <sys/param.h> 38#include <sys/param.h>
39#include <sys/kernel.h> 39#include <sys/kernel.h>
40#include <sys/kmem.h> 40#include <sys/kmem.h>
41#include <sys/module.h> 41#include <sys/module.h>
42#include <sys/atomic.h> 42#include <sys/atomic.h>
43 43
44#include <dev/usb/usbnet.h> 44#include <dev/usb/usbnet.h>
45#include <dev/usb/usbhist.h> 45#include <dev/usb/usbhist.h>
46 46
47struct usbnet_cdata { 47struct usbnet_cdata {
48 struct usbnet_chain *uncd_tx_chain; 48 struct usbnet_chain *uncd_tx_chain;
49 struct usbnet_chain *uncd_rx_chain; 49 struct usbnet_chain *uncd_rx_chain;
50 50
51 int uncd_tx_prod; 51 int uncd_tx_prod;
52 int uncd_tx_cnt; 52 int uncd_tx_cnt;
53}; 53};
54 54
55struct usbnet_private { 55struct usbnet_private {
56 /* 56 /*
57 * - unp_lock protects most of the structure, and the public one 57 * - unp_lock protects most of the structure, and the public one
58 * - unp_miilock must be held to access this device's MII bus 58 * - unp_miilock must be held to access this device's MII bus
59 * - unp_rxlock protects the rx path and its data 59 * - unp_rxlock protects the rx path and its data
60 * - unp_txlock protects the tx path and its data 60 * - unp_txlock protects the tx path and its data
61 * - unp_detachcv handles detach vs open references 61 * - unp_detachcv handles detach vs open references
62 * 62 *
63 * the lock ordering is: 63 * the lock ordering is:
64 * ifnet lock -> unp_lock -> unp_rxlock -> unp_txlock 64 * ifnet lock -> unp_lock -> unp_rxlock -> unp_txlock
65 * unp_lock -> unp_miilock 65 * unp_lock -> unp_miilock
66 * and unp_lock may be dropped after taking unp_miilock. 66 * and unp_lock may be dropped after taking unp_miilock.
67 */ 67 */
68 kmutex_t unp_lock; 68 kmutex_t unp_lock;
69 kmutex_t unp_miilock; 69 kmutex_t unp_miilock;
70 kmutex_t unp_rxlock; 70 kmutex_t unp_rxlock;
71 kmutex_t unp_txlock; 71 kmutex_t unp_txlock;
72 kcondvar_t unp_detachcv; 72 kcondvar_t unp_detachcv;
73 73
74 struct usbnet_cdata unp_cdata; 74 struct usbnet_cdata unp_cdata;
75 75
76 struct ethercom unp_ec; 76 struct ethercom unp_ec;
77 struct mii_data unp_mii; 77 struct mii_data unp_mii;
78 struct usb_task unp_ticktask; 78 struct usb_task unp_ticktask;
79 struct callout unp_stat_ch; 79 struct callout unp_stat_ch;
80 struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX]; 80 struct usbd_pipe *unp_ep[USBNET_ENDPT_MAX];
81 81
82 bool unp_dying; 82 bool unp_dying;
83 bool unp_stopping; 83 bool unp_stopping;
84 bool unp_attached; 84 bool unp_attached;
85 bool unp_link; 85 bool unp_link;
86 86
87 int unp_refcnt; 87 int unp_refcnt;
88 int unp_timer; 88 int unp_timer;
89 int unp_if_flags; 89 int unp_if_flags;
90 unsigned unp_number; 90 unsigned unp_number;
91 91
92 krndsource_t unp_rndsrc; 92 krndsource_t unp_rndsrc;
93 93
94 struct timeval unp_rx_notice; 94 struct timeval unp_rx_notice;
95 struct timeval unp_tx_notice; 95 struct timeval unp_tx_notice;
96 struct timeval unp_intr_notice; 96 struct timeval unp_intr_notice;
97}; 97};
98 98
99#define un_cdata(un) (&(un)->un_pri->unp_cdata) 99#define un_cdata(un) (&(un)->un_pri->unp_cdata)
100 100
101volatile unsigned usbnet_number; 101volatile unsigned usbnet_number;
102 102
103static int usbnet_modcmd(modcmd_t, void *); 103static int usbnet_modcmd(modcmd_t, void *);
104 104
105#ifdef USB_DEBUG 105#ifdef USB_DEBUG
106#ifndef USBNET_DEBUG 106#ifndef USBNET_DEBUG
107#define usbnetdebug 0 107#define usbnetdebug 0
108#else 108#else
109static int usbnetdebug = 0; 109static int usbnetdebug = 0;
110 110
111SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup") 111SYSCTL_SETUP(sysctl_hw_usbnet_setup, "sysctl hw.usbnet setup")
112{ 112{
113 int err; 113 int err;
114 const struct sysctlnode *rnode; 114 const struct sysctlnode *rnode;
115 const struct sysctlnode *cnode; 115 const struct sysctlnode *cnode;
116 116
117 err = sysctl_createv(clog, 0, NULL, &rnode, 117 err = sysctl_createv(clog, 0, NULL, &rnode,
118 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet", 118 CTLFLAG_PERMANENT, CTLTYPE_NODE, "usbnet",
119 SYSCTL_DESCR("usbnet global controls"), 119 SYSCTL_DESCR("usbnet global controls"),
120 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 120 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
121 121
122 if (err) 122 if (err)
123 goto fail; 123 goto fail;
124 124
125 /* control debugging printfs */ 125 /* control debugging printfs */
126 err = sysctl_createv(clog, 0, &rnode, &cnode, 126 err = sysctl_createv(clog, 0, &rnode, &cnode,
127 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT, 127 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
128 "debug", SYSCTL_DESCR("Enable debugging output"), 128 "debug", SYSCTL_DESCR("Enable debugging output"),
129 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL); 129 NULL, 0, &usbnetdebug, sizeof(usbnetdebug), CTL_CREATE, CTL_EOL);
130 if (err) 130 if (err)
131 goto fail; 131 goto fail;
132 132
133 return; 133 return;
134fail: 134fail:
135 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 135 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
136} 136}
137 137
138#endif /* USBNET_DEBUG */ 138#endif /* USBNET_DEBUG */
139#endif /* USB_DEBUG */ 139#endif /* USB_DEBUG */
140 140
141#define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D) 141#define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,1,FMT,A,B,C,D)
142#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D) 142#define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbnetdebug,N,FMT,A,B,C,D)
143#define USBNETHIST_FUNC() USBHIST_FUNC() 143#define USBNETHIST_FUNC() USBHIST_FUNC()
144#define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug) 144#define USBNETHIST_CALLED(name) USBHIST_CALLED(usbnetdebug)
145#define USBNETHIST_CALLARGS(FMT,A,B,C,D) \ 145#define USBNETHIST_CALLARGS(FMT,A,B,C,D) \
146 USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D) 146 USBHIST_CALLARGS(usbnetdebug,FMT,A,B,C,D)
147#define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \ 147#define USBNETHIST_CALLARGSN(N,FMT,A,B,C,D) \
148 USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D) 148 USBHIST_CALLARGSN(usbnetdebug,N,FMT,A,B,C,D)
149 149
150/* Callback vectors. */ 150/* Callback vectors. */
151 151
152static void 152static void
153uno_stop(struct usbnet *un, struct ifnet *ifp, int disable) 153uno_stop(struct usbnet *un, struct ifnet *ifp, int disable)
154{ 154{
155 if (un->un_ops->uno_stop) 155 if (un->un_ops->uno_stop)
156 (*un->un_ops->uno_stop)(ifp, disable); 156 (*un->un_ops->uno_stop)(ifp, disable);
157} 157}
158 158
159static int 159static int
160uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) 160uno_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data)
161{ 161{
162 if (un->un_ops->uno_ioctl) 162 if (un->un_ops->uno_ioctl)
163 return (*un->un_ops->uno_ioctl)(ifp, cmd, data); 163 return (*un->un_ops->uno_ioctl)(ifp, cmd, data);
164 return 0; 164 return 0;
165} 165}
166 166
167static int 167static int
168uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data) 168uno_override_ioctl(struct usbnet *un, struct ifnet *ifp, u_long cmd, void *data)
169{ 169{
170 return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data); 170 return (*un->un_ops->uno_override_ioctl)(ifp, cmd, data);
171} 171}
172 172
173static int 173static int
174uno_init(struct usbnet *un, struct ifnet *ifp) 174uno_init(struct usbnet *un, struct ifnet *ifp)
175{ 175{
176 return (*un->un_ops->uno_init)(ifp); 176 return (*un->un_ops->uno_init)(ifp);
177} 177}
178 178
179static int 179static int
180uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val) 180uno_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
181{ 181{
182 return (*un->un_ops->uno_read_reg)(un, phy, reg, val); 182 return (*un->un_ops->uno_read_reg)(un, phy, reg, val);
183} 183}
184 184
185static int 185static int
186uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val) 186uno_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
187{ 187{
188 return (*un->un_ops->uno_write_reg)(un, phy, reg, val); 188 return (*un->un_ops->uno_write_reg)(un, phy, reg, val);
189} 189}
190 190
191static void 191static void
192uno_mii_statchg(struct usbnet *un, struct ifnet *ifp) 192uno_mii_statchg(struct usbnet *un, struct ifnet *ifp)
193{ 193{
194 (*un->un_ops->uno_statchg)(ifp); 194 (*un->un_ops->uno_statchg)(ifp);
195} 195}
196 196
197static unsigned 197static unsigned
198uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) 198uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
199{ 199{
200 return (*un->un_ops->uno_tx_prepare)(un, m, c); 200 return (*un->un_ops->uno_tx_prepare)(un, m, c);
201} 201}
202 202
203static void 203static void
204uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) 204uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
205{ 205{
206 (*un->un_ops->uno_rx_loop)(un, c, total_len); 206 (*un->un_ops->uno_rx_loop)(un, c, total_len);
207} 207}
208 208
209static void 209static void
210uno_tick(struct usbnet *un) 210uno_tick(struct usbnet *un)
211{ 211{
212 if (un->un_ops->uno_tick) 212 if (un->un_ops->uno_tick)
213 (*un->un_ops->uno_tick)(un); 213 (*un->un_ops->uno_tick)(un);
214} 214}
215 215
216static void 216static void
217uno_intr(struct usbnet *un, usbd_status status) 217uno_intr(struct usbnet *un, usbd_status status)
218{ 218{
219 if (un->un_ops->uno_intr) 219 if (un->un_ops->uno_intr)
220 (*un->un_ops->uno_intr)(un, status); 220 (*un->un_ops->uno_intr)(un, status);
221} 221}
222 222
223/* Interrupt handling. */ 223/* Interrupt handling. */
224 224
225static struct mbuf * 225static struct mbuf *
226usbnet_newbuf(size_t buflen) 226usbnet_newbuf(size_t buflen)
227{ 227{
228 struct mbuf *m; 228 struct mbuf *m;
229 229
 230 if (buflen > MCLBYTES)
 231 return NULL;
 232
230 MGETHDR(m, M_DONTWAIT, MT_DATA); 233 MGETHDR(m, M_DONTWAIT, MT_DATA);
231 if (m == NULL) 234 if (m == NULL)
232 return NULL; 235 return NULL;
233 236
234 if (buflen > MHLEN - ETHER_ALIGN) { 237 if (buflen > MHLEN - ETHER_ALIGN) {
235 MCLGET(m, M_DONTWAIT); 238 MCLGET(m, M_DONTWAIT);
236 if (!(m->m_flags & M_EXT)) { 239 if (!(m->m_flags & M_EXT)) {
237 m_freem(m); 240 m_freem(m);
238 return NULL; 241 return NULL;
239 } 242 }
240 } 243 }
241 244
242 m_adj(m, ETHER_ALIGN); 245 m_adj(m, ETHER_ALIGN);
243 m->m_len = m->m_pkthdr.len = buflen; 246 m->m_len = m->m_pkthdr.len = buflen;
244 247
245 return m; 248 return m;
246} 249}
247 250
248/* 251/*
249 * usbnet_rxeof() is designed to be the done callback for rx completion. 252 * usbnet_rxeof() is designed to be the done callback for rx completion.
250 * it provides generic setup and finalisation, calls a different usbnet 253 * it provides generic setup and finalisation, calls a different usbnet
251 * rx_loop callback in the middle, which can use usbnet_enqueue() to 254 * rx_loop callback in the middle, which can use usbnet_enqueue() to
252 * enqueue a packet for higher levels (or usbnet_input() if previously 255 * enqueue a packet for higher levels (or usbnet_input() if previously
253 * using if_input() path.) 256 * using if_input() path.)
254 */ 257 */
255void 258void
256usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen, 259usbnet_enqueue(struct usbnet * const un, uint8_t *buf, size_t buflen,
257 int csum_flags, uint32_t csum_data, int mbuf_flags) 260 int csum_flags, uint32_t csum_data, int mbuf_flags)
258{ 261{
259 USBNETHIST_FUNC(); 262 USBNETHIST_FUNC();
260 struct ifnet * const ifp = usbnet_ifp(un); 263 struct ifnet * const ifp = usbnet_ifp(un);
261 struct usbnet_private * const unp __unused = un->un_pri; 264 struct usbnet_private * const unp __unused = un->un_pri;
262 struct mbuf *m; 265 struct mbuf *m;
263 266
264 USBNETHIST_CALLARGSN(5, "%d: enter: len=%zu csf %x mbf %x", 267 USBNETHIST_CALLARGSN(5, "%d: enter: len=%zu csf %x mbf %x",
265 unp->unp_number, buflen, csum_flags, mbuf_flags); 268 unp->unp_number, buflen, csum_flags, mbuf_flags);
266 269
267 usbnet_isowned_rx(un); 270 usbnet_isowned_rx(un);
268 271
269 m = usbnet_newbuf(buflen); 272 m = usbnet_newbuf(buflen);
270 if (m == NULL) { 273 if (m == NULL) {
271 DPRINTF("%d: no memory", unp->unp_number, 0, 0, 0); 274 DPRINTF("%d: no memory", unp->unp_number, 0, 0, 0);
272 ifp->if_ierrors++; 275 ifp->if_ierrors++;
273 return; 276 return;
274 } 277 }
275 278
276 m_set_rcvif(m, ifp); 279 m_set_rcvif(m, ifp);
277 m->m_pkthdr.csum_flags = csum_flags; 280 m->m_pkthdr.csum_flags = csum_flags;
278 m->m_pkthdr.csum_data = csum_data; 281 m->m_pkthdr.csum_data = csum_data;
279 m->m_flags |= mbuf_flags; 282 m->m_flags |= mbuf_flags;
280 memcpy(mtod(m, uint8_t *), buf, buflen); 283 memcpy(mtod(m, uint8_t *), buf, buflen);
281 284
282 /* push the packet up */ 285 /* push the packet up */
283 if_percpuq_enqueue(ifp->if_percpuq, m); 286 if_percpuq_enqueue(ifp->if_percpuq, m);
284} 287}
285 288
286void 289void
287usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen) 290usbnet_input(struct usbnet * const un, uint8_t *buf, size_t buflen)
288{ 291{
289 USBNETHIST_FUNC(); 292 USBNETHIST_FUNC();
290 struct ifnet * const ifp = usbnet_ifp(un); 293 struct ifnet * const ifp = usbnet_ifp(un);
291 struct usbnet_private * const unp __unused = un->un_pri; 294 struct usbnet_private * const unp __unused = un->un_pri;
292 struct mbuf *m; 295 struct mbuf *m;
293 296
294 USBNETHIST_CALLARGSN(5, "%d: enter: buf %jx len %ju", 297 USBNETHIST_CALLARGSN(5, "%d: enter: buf %jx len %ju",
295 unp->unp_number, (uintptr_t)buf, buflen, 0); 298 unp->unp_number, (uintptr_t)buf, buflen, 0);
296 299
297 usbnet_isowned_rx(un); 300 usbnet_isowned_rx(un);
298 301
299 m = usbnet_newbuf(buflen); 302 m = usbnet_newbuf(buflen);
300 if (m == NULL) { 303 if (m == NULL) {
301 ifp->if_ierrors++; 304 ifp->if_ierrors++;
302 return; 305 return;
303 } 306 }
304 307
305 m_set_rcvif(m, ifp); 308 m_set_rcvif(m, ifp);
306 memcpy(mtod(m, char *), buf, buflen); 309 memcpy(mtod(m, char *), buf, buflen);
307 310
308 /* push the packet up */ 311 /* push the packet up */
309 if_input(ifp, m); 312 if_input(ifp, m);
310} 313}
311 314
312/* 315/*
313 * A frame has been uploaded: pass the resulting mbuf chain up to 316 * A frame has been uploaded: pass the resulting mbuf chain up to
314 * the higher level protocols. 317 * the higher level protocols.
315 */ 318 */
316static void 319static void
317usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 320usbnet_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
318{ 321{
319 USBNETHIST_FUNC(); 322 USBNETHIST_FUNC();
320 struct usbnet_chain * const c = priv; 323 struct usbnet_chain * const c = priv;
321 struct usbnet * const un = c->unc_un; 324 struct usbnet * const un = c->unc_un;
322 struct usbnet_private * const unp = un->un_pri; 325 struct usbnet_private * const unp = un->un_pri;
323 struct ifnet * const ifp = usbnet_ifp(un); 326 struct ifnet * const ifp = usbnet_ifp(un);
324 uint32_t total_len; 327 uint32_t total_len;
325 328
326 USBNETHIST_CALLARGSN(5, "%d: enter: status %x xfer %jx", 329 USBNETHIST_CALLARGSN(5, "%d: enter: status %x xfer %jx",
327 unp->unp_number, status, (uintptr_t)xfer, 0); 330 unp->unp_number, status, (uintptr_t)xfer, 0);
328 331
329 mutex_enter(&unp->unp_rxlock); 332 mutex_enter(&unp->unp_rxlock);
330 333
331 if (unp->unp_dying || unp->unp_stopping || 334 if (unp->unp_dying || unp->unp_stopping ||
332 status == USBD_INVAL || status == USBD_NOT_STARTED || 335 status == USBD_INVAL || status == USBD_NOT_STARTED ||
333 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) 336 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING))
334 goto out; 337 goto out;
335 338
336 if (status != USBD_NORMAL_COMPLETION) { 339 if (status != USBD_NORMAL_COMPLETION) {
337 if (usbd_ratecheck(&unp->unp_rx_notice)) 340 if (usbd_ratecheck(&unp->unp_rx_notice))
338 aprint_error_dev(un->un_dev, "usb errors on rx: %s\n", 341 aprint_error_dev(un->un_dev, "usb errors on rx: %s\n",
339 usbd_errstr(status)); 342 usbd_errstr(status));
340 if (status == USBD_STALLED) 343 if (status == USBD_STALLED)
341 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]); 344 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_RX]);
342 goto done; 345 goto done;
343 } 346 }
344 347
345 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 348 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
346 349
347 if (total_len > un->un_rx_bufsz) { 350 if (total_len > un->un_rx_bufsz) {
348 aprint_error_dev(un->un_dev, 351 aprint_error_dev(un->un_dev,
349 "rxeof: too large transfer (%u > %u)\n", 352 "rxeof: too large transfer (%u > %u)\n",
350 total_len, un->un_rx_bufsz); 353 total_len, un->un_rx_bufsz);
351 goto done; 354 goto done;
352 } 355 }
353 356
354 uno_rx_loop(un, c, total_len); 357 uno_rx_loop(un, c, total_len);
355 usbnet_isowned_rx(un); 358 usbnet_isowned_rx(un);
356 359
357done: 360done:
358 if (unp->unp_dying || unp->unp_stopping) 361 if (unp->unp_dying || unp->unp_stopping)
359 goto out; 362 goto out;
360 363
361 mutex_exit(&unp->unp_rxlock); 364 mutex_exit(&unp->unp_rxlock);
362 365
363 /* Setup new transfer. */ 366 /* Setup new transfer. */
364 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz, 367 usbd_setup_xfer(xfer, c, c->unc_buf, un->un_rx_bufsz,
365 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); 368 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
366 usbd_transfer(xfer); 369 usbd_transfer(xfer);
367 return; 370 return;
368 371
369out: 372out:
370 mutex_exit(&unp->unp_rxlock); 373 mutex_exit(&unp->unp_rxlock);
371} 374}
372 375
373static void 376static void
374usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 377usbnet_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
375{ 378{
376 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 379 USBNETHIST_FUNC(); USBNETHIST_CALLED();
377 struct usbnet_chain * const c = priv; 380 struct usbnet_chain * const c = priv;
378 struct usbnet * const un = c->unc_un; 381 struct usbnet * const un = c->unc_un;
379 struct usbnet_cdata * const cd = un_cdata(un); 382 struct usbnet_cdata * const cd = un_cdata(un);
380 struct usbnet_private * const unp = un->un_pri; 383 struct usbnet_private * const unp = un->un_pri;
381 struct ifnet * const ifp = usbnet_ifp(un); 384 struct ifnet * const ifp = usbnet_ifp(un);
382 385
383 USBNETHIST_CALLARGSN(5, "%d: enter: status %x xfer %jx", 386 USBNETHIST_CALLARGSN(5, "%d: enter: status %x xfer %jx",
384 unp->unp_number, status, (uintptr_t)xfer, 0); 387 unp->unp_number, status, (uintptr_t)xfer, 0);
385 388
386 mutex_enter(&unp->unp_txlock); 389 mutex_enter(&unp->unp_txlock);
387 if (unp->unp_stopping || unp->unp_dying) { 390 if (unp->unp_stopping || unp->unp_dying) {
388 mutex_exit(&unp->unp_txlock); 391 mutex_exit(&unp->unp_txlock);
389 return; 392 return;
390 } 393 }
391 394
392 KASSERT(cd->uncd_tx_cnt > 0); 395 KASSERT(cd->uncd_tx_cnt > 0);
393 cd->uncd_tx_cnt--; 396 cd->uncd_tx_cnt--;
394 397
395 unp->unp_timer = 0; 398 unp->unp_timer = 0;
396 399
397 switch (status) { 400 switch (status) {
398 case USBD_NOT_STARTED: 401 case USBD_NOT_STARTED:
399 case USBD_CANCELLED: 402 case USBD_CANCELLED:
400 break; 403 break;
401 404
402 case USBD_NORMAL_COMPLETION: 405 case USBD_NORMAL_COMPLETION:
403 ifp->if_opackets++; 406 ifp->if_opackets++;
404 break; 407 break;
405 408
406 default: 409 default:
407 410
408 ifp->if_oerrors++; 411 ifp->if_oerrors++;
409 if (usbd_ratecheck(&unp->unp_tx_notice)) 412 if (usbd_ratecheck(&unp->unp_tx_notice))
410 aprint_error_dev(un->un_dev, "usb error on tx: %s\n", 413 aprint_error_dev(un->un_dev, "usb error on tx: %s\n",
411 usbd_errstr(status)); 414 usbd_errstr(status));
412 if (status == USBD_STALLED) 415 if (status == USBD_STALLED)
413 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]); 416 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_TX]);
414 break; 417 break;
415 } 418 }
416 419
417 mutex_exit(&unp->unp_txlock); 420 mutex_exit(&unp->unp_txlock);
418 421
419 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd)) 422 if (status == USBD_NORMAL_COMPLETION && !IFQ_IS_EMPTY(&ifp->if_snd))
420 (*ifp->if_start)(ifp); 423 (*ifp->if_start)(ifp);
421} 424}
422 425
423static void 426static void
424usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status) 427usbnet_pipe_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
425{ 428{
426 USBNETHIST_FUNC(); 429 USBNETHIST_FUNC();
427 struct usbnet * const un = priv; 430 struct usbnet * const un = priv;
428 struct usbnet_private * const unp = un->un_pri; 431 struct usbnet_private * const unp = un->un_pri;
429 struct usbnet_intr * const uni = un->un_intr; 432 struct usbnet_intr * const uni = un->un_intr;
430 struct ifnet * const ifp = usbnet_ifp(un); 433 struct ifnet * const ifp = usbnet_ifp(un);
431 434
432 if (uni == NULL || unp->unp_dying || unp->unp_stopping || 435 if (uni == NULL || unp->unp_dying || unp->unp_stopping ||
433 status == USBD_INVAL || status == USBD_NOT_STARTED || 436 status == USBD_INVAL || status == USBD_NOT_STARTED ||
434 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) { 437 status == USBD_CANCELLED || !(ifp->if_flags & IFF_RUNNING)) {
435 USBNETHIST_CALLARGS("%d: uni %jx d/s %x status %x", 438 USBNETHIST_CALLARGS("%d: uni %jx d/s %x status %x",
436 unp->unp_number, (uintptr_t)uni, 439 unp->unp_number, (uintptr_t)uni,
437 (unp->unp_dying << 8) | unp->unp_stopping, status); 440 (unp->unp_dying << 8) | unp->unp_stopping, status);
438 return; 441 return;
439 } 442 }
440 443
441 if (status != USBD_NORMAL_COMPLETION) { 444 if (status != USBD_NORMAL_COMPLETION) {
442 if (usbd_ratecheck(&unp->unp_intr_notice)) { 445 if (usbd_ratecheck(&unp->unp_intr_notice)) {
443 aprint_error_dev(un->un_dev, "usb error on intr: %s\n", 446 aprint_error_dev(un->un_dev, "usb error on intr: %s\n",
444 usbd_errstr(status)); 447 usbd_errstr(status));
445 } 448 }
446 if (status == USBD_STALLED) 449 if (status == USBD_STALLED)
447 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]); 450 usbd_clear_endpoint_stall_async(unp->unp_ep[USBNET_ENDPT_INTR]);
448 USBNETHIST_CALLARGS("%d: not normal status %x", 451 USBNETHIST_CALLARGS("%d: not normal status %x",
449 unp->unp_number, status, 0, 0); 452 unp->unp_number, status, 0, 0);
450 return; 453 return;
451 } 454 }
452 455
453 uno_intr(un, status); 456 uno_intr(un, status);
454} 457}
455 458
456static void 459static void
457usbnet_start_locked(struct ifnet *ifp) 460usbnet_start_locked(struct ifnet *ifp)
458{ 461{
459 USBNETHIST_FUNC(); 462 USBNETHIST_FUNC();
460 struct usbnet * const un = ifp->if_softc; 463 struct usbnet * const un = ifp->if_softc;
461 struct usbnet_cdata * const cd = un_cdata(un); 464 struct usbnet_cdata * const cd = un_cdata(un);
462 struct usbnet_private * const unp = un->un_pri; 465 struct usbnet_private * const unp = un->un_pri;
463 struct mbuf *m; 466 struct mbuf *m;
464 unsigned length; 467 unsigned length;
465 bool done_transmit = false; 468 bool done_transmit = false;
466 int idx; 469 int idx;
467 470
468 USBNETHIST_CALLARGS("%d: tx_cnt %d list_cnt %d link %d", 471 USBNETHIST_CALLARGS("%d: tx_cnt %d list_cnt %d link %d",
469 unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt, 472 unp->unp_number, cd->uncd_tx_cnt, un->un_tx_list_cnt,
470 unp->unp_link); 473 unp->unp_link);
471 474
472 usbnet_isowned_tx(un); 475 usbnet_isowned_tx(un);
473 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt); 476 KASSERT(cd->uncd_tx_cnt <= un->un_tx_list_cnt);
474 477
475 if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) { 478 if (!unp->unp_link || (ifp->if_flags & IFF_RUNNING) == 0) {
476 DPRINTF("start called no link (%x) or running (flags %x)", 479 DPRINTF("start called no link (%x) or running (flags %x)",
477 unp->unp_link, ifp->if_flags, 0, 0); 480 unp->unp_link, ifp->if_flags, 0, 0);
478 return; 481 return;
479 } 482 }
480 483
481 if (cd->uncd_tx_cnt == un->un_tx_list_cnt) { 484 if (cd->uncd_tx_cnt == un->un_tx_list_cnt) {
482 DPRINTF("start called, tx busy (%jx == %jx)", 485 DPRINTF("start called, tx busy (%jx == %jx)",
483 cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0); 486 cd->uncd_tx_cnt, un->un_tx_list_cnt, 0, 0);
484 return; 487 return;
485 } 488 }
486 489
487 idx = cd->uncd_tx_prod; 490 idx = cd->uncd_tx_prod;
488 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) { 491 while (cd->uncd_tx_cnt < un->un_tx_list_cnt) {
489 IFQ_POLL(&ifp->if_snd, m); 492 IFQ_POLL(&ifp->if_snd, m);
490 if (m == NULL) { 493 if (m == NULL) {
491 DPRINTF("start called, queue empty", 0, 0, 0, 0); 494 DPRINTF("start called, queue empty", 0, 0, 0, 0);
492 break; 495 break;
493 } 496 }
494 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz); 497 KASSERT(m->m_pkthdr.len <= un->un_tx_bufsz);
495 498
496 struct usbnet_chain *c = &cd->uncd_tx_chain[idx]; 499 struct usbnet_chain *c = &cd->uncd_tx_chain[idx];
497 500
498 length = uno_tx_prepare(un, m, c); 501 length = uno_tx_prepare(un, m, c);
499 if (length == 0) { 502 if (length == 0) {
500 DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0); 503 DPRINTF("uno_tx_prepare gave zero length", 0, 0, 0, 0);
501 ifp->if_oerrors++; 504 ifp->if_oerrors++;
502 break; 505 break;
503 } 506 }
504 507
505 if (__predict_false(c->unc_xfer == NULL)) { 508 if (__predict_false(c->unc_xfer == NULL)) {
506 DPRINTF("unc_xfer is NULL", 0, 0, 0, 0); 509 DPRINTF("unc_xfer is NULL", 0, 0, 0, 0);
507 ifp->if_oerrors++; 510 ifp->if_oerrors++;
508 break; 511 break;
509 } 512 }
510 513
511 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length, 514 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, length,
512 un->un_tx_xfer_flags, 10000, usbnet_txeof); 515 un->un_tx_xfer_flags, 10000, usbnet_txeof);
513 516
514 /* Transmit */ 517 /* Transmit */
515 usbd_status err = usbd_transfer(c->unc_xfer); 518 usbd_status err = usbd_transfer(c->unc_xfer);
516 if (err != USBD_IN_PROGRESS) { 519 if (err != USBD_IN_PROGRESS) {
517 DPRINTF("usbd_transfer on %jx for %ju bytes: %d", 520 DPRINTF("usbd_transfer on %jx for %ju bytes: %d",
518 (uintptr_t)c->unc_buf, length, err, 0); 521 (uintptr_t)c->unc_buf, length, err, 0);
519 ifp->if_oerrors++; 522 ifp->if_oerrors++;
520 break; 523 break;
521 } 524 }
522 done_transmit = true; 525 done_transmit = true;
523 526
524 IFQ_DEQUEUE(&ifp->if_snd, m); 527 IFQ_DEQUEUE(&ifp->if_snd, m);
525 528
526 /* 529 /*
527 * If there's a BPF listener, bounce a copy of this frame 530 * If there's a BPF listener, bounce a copy of this frame
528 * to him. 531 * to him.
529 */ 532 */
530 bpf_mtap(ifp, m, BPF_D_OUT); 533 bpf_mtap(ifp, m, BPF_D_OUT);
531 m_freem(m); 534 m_freem(m);
532 535
533 idx = (idx + 1) % un->un_tx_list_cnt; 536 idx = (idx + 1) % un->un_tx_list_cnt;
534 cd->uncd_tx_cnt++; 537 cd->uncd_tx_cnt++;
535 } 538 }
536 cd->uncd_tx_prod = idx; 539 cd->uncd_tx_prod = idx;
537 540
538 DPRINTF("finished with start; tx_cnt %d list_cnt %d link %d", 541 DPRINTF("finished with start; tx_cnt %d list_cnt %d link %d",
539 cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0); 542 cd->uncd_tx_cnt, un->un_tx_list_cnt, unp->unp_link, 0);
540 543
541 /* 544 /*
542 * Set a timeout in case the chip goes out to lunch. 545 * Set a timeout in case the chip goes out to lunch.
543 */ 546 */
544 if (done_transmit) 547 if (done_transmit)
545 unp->unp_timer = 5; 548 unp->unp_timer = 5;
546} 549}
547 550
548static void 551static void
549usbnet_start(struct ifnet *ifp) 552usbnet_start(struct ifnet *ifp)
550{ 553{
551 struct usbnet * const un = ifp->if_softc; 554 struct usbnet * const un = ifp->if_softc;
552 struct usbnet_private * const unp = un->un_pri; 555 struct usbnet_private * const unp = un->un_pri;
553 556
554 USBNETHIST_FUNC(); 557 USBNETHIST_FUNC();
555 USBNETHIST_CALLARGS("%d, stopping %d", 558 USBNETHIST_CALLARGS("%d, stopping %d",
556 unp->unp_number, unp->unp_stopping, 0, 0); 559 unp->unp_number, unp->unp_stopping, 0, 0);
557 560
558 mutex_enter(&unp->unp_txlock); 561 mutex_enter(&unp->unp_txlock);
559 if (!unp->unp_stopping) 562 if (!unp->unp_stopping)
560 usbnet_start_locked(ifp); 563 usbnet_start_locked(ifp);
561 mutex_exit(&unp->unp_txlock); 564 mutex_exit(&unp->unp_txlock);
562} 565}
563 566
564/* 567/*
565 * Chain management. 568 * Chain management.
566 * 569 *
567 * RX and TX are identical. Keep them that way. 570 * RX and TX are identical. Keep them that way.
568 */ 571 */
569 572
570/* Start of common RX functions */ 573/* Start of common RX functions */
571 574
572static size_t 575static size_t
573usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) 576usbnet_rx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
574{ 577{
575 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt; 578 return sizeof(*cd->uncd_rx_chain) * un->un_rx_list_cnt;
576} 579}
577 580
578static void 581static void
579usbnet_rx_list_alloc(struct usbnet * const un) 582usbnet_rx_list_alloc(struct usbnet * const un)
580{ 583{
581 struct usbnet_cdata * const cd = un_cdata(un); 584 struct usbnet_cdata * const cd = un_cdata(un);
582 585
583 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP); 586 cd->uncd_rx_chain = kmem_zalloc(usbnet_rx_list_size(cd, un), KM_SLEEP);
584} 587}
585 588
586static void 589static void
587usbnet_rx_list_free(struct usbnet * const un) 590usbnet_rx_list_free(struct usbnet * const un)
588{ 591{
589 struct usbnet_cdata * const cd = un_cdata(un); 592 struct usbnet_cdata * const cd = un_cdata(un);
590 593
591 if (cd->uncd_rx_chain) { 594 if (cd->uncd_rx_chain) {
592 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un)); 595 kmem_free(cd->uncd_rx_chain, usbnet_rx_list_size(cd, un));
593 cd->uncd_rx_chain = NULL; 596 cd->uncd_rx_chain = NULL;
594 } 597 }
595} 598}
596 599
597static int 600static int
598usbnet_rx_list_init(struct usbnet * const un) 601usbnet_rx_list_init(struct usbnet * const un)
599{ 602{
600 struct usbnet_cdata * const cd = un_cdata(un); 603 struct usbnet_cdata * const cd = un_cdata(un);
601 struct usbnet_private * const unp = un->un_pri; 604 struct usbnet_private * const unp = un->un_pri;
602 605
603 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 606 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
604 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 607 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
605 608
606 c->unc_un = un; 609 c->unc_un = un;
607 if (c->unc_xfer == NULL) { 610 if (c->unc_xfer == NULL) {
608 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX], 611 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_RX],
609 un->un_rx_bufsz, un->un_rx_xfer_flags, 0, 612 un->un_rx_bufsz, un->un_rx_xfer_flags, 0,
610 &c->unc_xfer); 613 &c->unc_xfer);
611 if (err) 614 if (err)
612 return err; 615 return err;
613 c->unc_buf = usbd_get_buffer(c->unc_xfer); 616 c->unc_buf = usbd_get_buffer(c->unc_xfer);
614 } 617 }
615 } 618 }
616 619
617 return 0; 620 return 0;
618} 621}
619 622
620static void 623static void
621usbnet_rx_list_fini(struct usbnet * const un) 624usbnet_rx_list_fini(struct usbnet * const un)
622{ 625{
623 struct usbnet_cdata * const cd = un_cdata(un); 626 struct usbnet_cdata * const cd = un_cdata(un);
624 627
625 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 628 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
626 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 629 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
627 630
628 if (c->unc_xfer != NULL) { 631 if (c->unc_xfer != NULL) {
629 usbd_destroy_xfer(c->unc_xfer); 632 usbd_destroy_xfer(c->unc_xfer);
630 c->unc_xfer = NULL; 633 c->unc_xfer = NULL;
631 c->unc_buf = NULL; 634 c->unc_buf = NULL;
632 } 635 }
633 } 636 }
634} 637}
635 638
636/* End of common RX functions */ 639/* End of common RX functions */
637 640
638static void 641static void
639usbnet_rx_start_pipes(struct usbnet * const un) 642usbnet_rx_start_pipes(struct usbnet * const un)
640{ 643{
641 struct usbnet_cdata * const cd = un_cdata(un); 644 struct usbnet_cdata * const cd = un_cdata(un);
642 struct usbnet_private * const unp = un->un_pri; 645 struct usbnet_private * const unp = un->un_pri;
643 646
644 mutex_enter(&unp->unp_rxlock); 647 mutex_enter(&unp->unp_rxlock);
645 mutex_enter(&unp->unp_txlock); 648 mutex_enter(&unp->unp_txlock);
646 unp->unp_stopping = false; 649 unp->unp_stopping = false;
647 650
648 for (size_t i = 0; i < un->un_rx_list_cnt; i++) { 651 for (size_t i = 0; i < un->un_rx_list_cnt; i++) {
649 struct usbnet_chain *c = &cd->uncd_rx_chain[i]; 652 struct usbnet_chain *c = &cd->uncd_rx_chain[i];
650 653
651 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz, 654 usbd_setup_xfer(c->unc_xfer, c, c->unc_buf, un->un_rx_bufsz,
652 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof); 655 un->un_rx_xfer_flags, USBD_NO_TIMEOUT, usbnet_rxeof);
653 usbd_transfer(c->unc_xfer); 656 usbd_transfer(c->unc_xfer);
654 } 657 }
655 658
656 mutex_exit(&unp->unp_txlock); 659 mutex_exit(&unp->unp_txlock);
657 mutex_exit(&unp->unp_rxlock); 660 mutex_exit(&unp->unp_rxlock);
658} 661}
659 662
660/* Start of common TX functions */ 663/* Start of common TX functions */
661 664
662static size_t 665static size_t
663usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un) 666usbnet_tx_list_size(struct usbnet_cdata * const cd, struct usbnet * const un)
664{ 667{
665 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt; 668 return sizeof(*cd->uncd_tx_chain) * un->un_tx_list_cnt;
666} 669}
667 670
668static void 671static void
669usbnet_tx_list_alloc(struct usbnet * const un) 672usbnet_tx_list_alloc(struct usbnet * const un)
670{ 673{
671 struct usbnet_cdata * const cd = un_cdata(un); 674 struct usbnet_cdata * const cd = un_cdata(un);
672 675
673 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP); 676 cd->uncd_tx_chain = kmem_zalloc(usbnet_tx_list_size(cd, un), KM_SLEEP);
674} 677}
675 678
676static void 679static void
677usbnet_tx_list_free(struct usbnet * const un) 680usbnet_tx_list_free(struct usbnet * const un)
678{ 681{
679 struct usbnet_cdata * const cd = un_cdata(un); 682 struct usbnet_cdata * const cd = un_cdata(un);
680 683
681 if (cd->uncd_tx_chain) { 684 if (cd->uncd_tx_chain) {
682 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un)); 685 kmem_free(cd->uncd_tx_chain, usbnet_tx_list_size(cd, un));
683 cd->uncd_tx_chain = NULL; 686 cd->uncd_tx_chain = NULL;
684 } 687 }
685} 688}
686 689
687static int 690static int
688usbnet_tx_list_init(struct usbnet * const un) 691usbnet_tx_list_init(struct usbnet * const un)
689{ 692{
690 struct usbnet_cdata * const cd = un_cdata(un); 693 struct usbnet_cdata * const cd = un_cdata(un);
691 struct usbnet_private * const unp = un->un_pri; 694 struct usbnet_private * const unp = un->un_pri;
692 695
693 for (size_t i = 0; i < un->un_tx_list_cnt; i++) { 696 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
694 struct usbnet_chain *c = &cd->uncd_tx_chain[i]; 697 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
695 698
696 c->unc_un = un; 699 c->unc_un = un;
697 if (c->unc_xfer == NULL) { 700 if (c->unc_xfer == NULL) {
698 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX], 701 int err = usbd_create_xfer(unp->unp_ep[USBNET_ENDPT_TX],
699 un->un_tx_bufsz, un->un_tx_xfer_flags, 0, 702 un->un_tx_bufsz, un->un_tx_xfer_flags, 0,
700 &c->unc_xfer); 703 &c->unc_xfer);
701 if (err) 704 if (err)
702 return err; 705 return err;
703 c->unc_buf = usbd_get_buffer(c->unc_xfer); 706 c->unc_buf = usbd_get_buffer(c->unc_xfer);
704 } 707 }
705 } 708 }
706 709
707 return 0; 710 return 0;
708} 711}
709 712
710static void 713static void
711usbnet_tx_list_fini(struct usbnet * const un) 714usbnet_tx_list_fini(struct usbnet * const un)
712{ 715{
713 struct usbnet_cdata * const cd = un_cdata(un); 716 struct usbnet_cdata * const cd = un_cdata(un);
714 717
715 for (size_t i = 0; i < un->un_tx_list_cnt; i++) { 718 for (size_t i = 0; i < un->un_tx_list_cnt; i++) {
716 struct usbnet_chain *c = &cd->uncd_tx_chain[i]; 719 struct usbnet_chain *c = &cd->uncd_tx_chain[i];
717 720
718 if (c->unc_xfer != NULL) { 721 if (c->unc_xfer != NULL) {
719 usbd_destroy_xfer(c->unc_xfer); 722 usbd_destroy_xfer(c->unc_xfer);
720 c->unc_xfer = NULL; 723 c->unc_xfer = NULL;
721 c->unc_buf = NULL; 724 c->unc_buf = NULL;
722 } 725 }
723 } 726 }
724 cd->uncd_tx_prod = cd->uncd_tx_cnt = 0; 727 cd->uncd_tx_prod = cd->uncd_tx_cnt = 0;
725} 728}
726 729
727/* End of common TX functions */ 730/* End of common TX functions */
728 731
729/* Endpoint pipe management. */ 732/* Endpoint pipe management. */
730 733
731static void 734static void
732usbnet_ep_close_pipes(struct usbnet * const un) 735usbnet_ep_close_pipes(struct usbnet * const un)
733{ 736{
734 struct usbnet_private * const unp = un->un_pri; 737 struct usbnet_private * const unp = un->un_pri;
735 738
736 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 739 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
737 if (unp->unp_ep[i] == NULL) 740 if (unp->unp_ep[i] == NULL)
738 continue; 741 continue;
739 usbd_status err = usbd_close_pipe(unp->unp_ep[i]); 742 usbd_status err = usbd_close_pipe(unp->unp_ep[i]);
740 if (err) 743 if (err)
741 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i, 744 aprint_error_dev(un->un_dev, "close pipe %zu: %s\n", i,
742 usbd_errstr(err)); 745 usbd_errstr(err));
743 unp->unp_ep[i] = NULL; 746 unp->unp_ep[i] = NULL;
744 } 747 }
745} 748}
746 749
747static usbd_status 750static usbd_status
748usbnet_ep_open_pipes(struct usbnet * const un) 751usbnet_ep_open_pipes(struct usbnet * const un)
749{ 752{
750 struct usbnet_intr * const uni = un->un_intr; 753 struct usbnet_intr * const uni = un->un_intr;
751 struct usbnet_private * const unp = un->un_pri; 754 struct usbnet_private * const unp = un->un_pri;
752 755
753 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 756 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
754 usbd_status err; 757 usbd_status err;
755 758
756 if (un->un_ed[i] == 0) 759 if (un->un_ed[i] == 0)
757 continue; 760 continue;
758 761
759 if (i == USBNET_ENDPT_INTR && uni) { 762 if (i == USBNET_ENDPT_INTR && uni) {
760 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i], 763 err = usbd_open_pipe_intr(un->un_iface, un->un_ed[i],
761 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un, 764 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i], un,
762 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr, 765 uni->uni_buf, uni->uni_bufsz, usbnet_pipe_intr,
763 uni->uni_interval); 766 uni->uni_interval);
764 } else { 767 } else {
765 err = usbd_open_pipe(un->un_iface, un->un_ed[i], 768 err = usbd_open_pipe(un->un_iface, un->un_ed[i],
766 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]); 769 USBD_EXCLUSIVE_USE | USBD_MPSAFE, &unp->unp_ep[i]);
767 } 770 }
768 if (err) { 771 if (err) {
769 usbnet_ep_close_pipes(un); 772 usbnet_ep_close_pipes(un);
770 return err; 773 return err;
771 } 774 }
772 } 775 }
773 776
774 return USBD_NORMAL_COMPLETION; 777 return USBD_NORMAL_COMPLETION;
775} 778}
776 779
777static usbd_status 780static usbd_status
778usbnet_ep_stop_pipes(struct usbnet * const un) 781usbnet_ep_stop_pipes(struct usbnet * const un)
779{ 782{
780 struct usbnet_private * const unp = un->un_pri; 783 struct usbnet_private * const unp = un->un_pri;
781 usbd_status err = USBD_NORMAL_COMPLETION; 784 usbd_status err = USBD_NORMAL_COMPLETION;
782 785
783 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) { 786 for (size_t i = 0; i < __arraycount(unp->unp_ep); i++) {
784 if (unp->unp_ep[i] == NULL) 787 if (unp->unp_ep[i] == NULL)
785 continue; 788 continue;
786 usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]); 789 usbd_status err2 = usbd_abort_pipe(unp->unp_ep[i]);
787 if (err == USBD_NORMAL_COMPLETION && err2) 790 if (err == USBD_NORMAL_COMPLETION && err2)
788 err = err2; 791 err = err2;
789 } 792 }
790 793
791 return err; 794 return err;
792} 795}
793 796
794int 797int
795usbnet_init_rx_tx(struct usbnet * const un) 798usbnet_init_rx_tx(struct usbnet * const un)
796{ 799{
797 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 800 USBNETHIST_FUNC(); USBNETHIST_CALLED();
798 struct usbnet_private * const unp = un->un_pri; 801 struct usbnet_private * const unp = un->un_pri;
799 struct ifnet * const ifp = usbnet_ifp(un); 802 struct ifnet * const ifp = usbnet_ifp(un);
800 usbd_status err; 803 usbd_status err;
801 int error = 0; 804 int error = 0;
802 805
803 usbnet_isowned(un); 806 usbnet_isowned(un);
804 807
805 if (unp->unp_dying) { 808 if (unp->unp_dying) {
806 return EIO; 809 return EIO;
807 } 810 }
808 unp->unp_refcnt++; 811 unp->unp_refcnt++;
809 812
810 /* Open RX and TX pipes. */ 813 /* Open RX and TX pipes. */
811 err = usbnet_ep_open_pipes(un); 814 err = usbnet_ep_open_pipes(un);
812 if (err) { 815 if (err) {
813 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n", 816 aprint_error_dev(un->un_dev, "open rx/tx pipes failed: %s\n",
814 usbd_errstr(err)); 817 usbd_errstr(err));
815 error = EIO; 818 error = EIO;
816 goto out; 819 goto out;
817 } 820 }
818 821
819 /* Init RX ring. */ 822 /* Init RX ring. */
820 if (usbnet_rx_list_init(un)) { 823 if (usbnet_rx_list_init(un)) {
821 aprint_error_dev(un->un_dev, "rx list init failed\n"); 824 aprint_error_dev(un->un_dev, "rx list init failed\n");
822 error = ENOBUFS; 825 error = ENOBUFS;
823 goto out; 826 goto out;
824 } 827 }
825 828
826 /* Init TX ring. */ 829 /* Init TX ring. */
827 if (usbnet_tx_list_init(un)) { 830 if (usbnet_tx_list_init(un)) {
828 aprint_error_dev(un->un_dev, "tx list init failed\n"); 831 aprint_error_dev(un->un_dev, "tx list init failed\n");
829 error = ENOBUFS; 832 error = ENOBUFS;
830 goto out; 833 goto out;
831 } 834 }
832 835
833 /* Start up the receive pipe(s). */ 836 /* Start up the receive pipe(s). */
834 usbnet_rx_start_pipes(un); 837 usbnet_rx_start_pipes(un);
835 838
836 /* Indicate we are up and running. */ 839 /* Indicate we are up and running. */
837#if 0 840#if 0
838 /* XXX if_mcast_op() can call this without ifnet locked */ 841 /* XXX if_mcast_op() can call this without ifnet locked */
839 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp)); 842 KASSERT(ifp->if_softc == NULL || IFNET_LOCKED(ifp));
840#endif 843#endif
841 ifp->if_flags |= IFF_RUNNING; 844 ifp->if_flags |= IFF_RUNNING;
842 845
843 callout_schedule(&unp->unp_stat_ch, hz); 846 callout_schedule(&unp->unp_stat_ch, hz);
844 847
845out: 848out:
846 if (error) { 849 if (error) {
847 usbnet_rx_list_fini(un); 850 usbnet_rx_list_fini(un);
848 usbnet_tx_list_fini(un); 851 usbnet_tx_list_fini(un);
849 usbnet_ep_close_pipes(un); 852 usbnet_ep_close_pipes(un);
850 } 853 }
851 if (--unp->unp_refcnt < 0) 854 if (--unp->unp_refcnt < 0)
852 cv_broadcast(&unp->unp_detachcv); 855 cv_broadcast(&unp->unp_detachcv);
853 856
854 usbnet_isowned(un); 857 usbnet_isowned(un);
855 858
856 return error; 859 return error;
857} 860}
858 861
859/* MII management. */ 862/* MII management. */
860 863
861/* 864/*
862 * Access functions for MII. Take the MII lock to call access MII regs. 865 * Access functions for MII. Take the MII lock to call access MII regs.
863 * Two forms: usbnet (softc) lock currently held or not. 866 * Two forms: usbnet (softc) lock currently held or not.
864 */ 867 */
865void 868void
866usbnet_lock_mii(struct usbnet *un) 869usbnet_lock_mii(struct usbnet *un)
867{ 870{
868 struct usbnet_private * const unp = un->un_pri; 871 struct usbnet_private * const unp = un->un_pri;
869 872
870 mutex_enter(&unp->unp_lock); 873 mutex_enter(&unp->unp_lock);
871 unp->unp_refcnt++; 874 unp->unp_refcnt++;
872 mutex_exit(&unp->unp_lock); 875 mutex_exit(&unp->unp_lock);
873 876
874 mutex_enter(&unp->unp_miilock); 877 mutex_enter(&unp->unp_miilock);
875} 878}
876 879
877void 880void
878usbnet_lock_mii_un_locked(struct usbnet *un) 881usbnet_lock_mii_un_locked(struct usbnet *un)
879{ 882{
880 struct usbnet_private * const unp = un->un_pri; 883 struct usbnet_private * const unp = un->un_pri;
881 884
882 usbnet_isowned(un); 885 usbnet_isowned(un);
883 886
884 unp->unp_refcnt++; 887 unp->unp_refcnt++;
885 mutex_enter(&unp->unp_miilock); 888 mutex_enter(&unp->unp_miilock);
886} 889}
887 890
888void 891void
889usbnet_unlock_mii(struct usbnet *un) 892usbnet_unlock_mii(struct usbnet *un)
890{ 893{
891 struct usbnet_private * const unp = un->un_pri; 894 struct usbnet_private * const unp = un->un_pri;
892 895
893 mutex_exit(&unp->unp_miilock); 896 mutex_exit(&unp->unp_miilock);
894 mutex_enter(&unp->unp_lock); 897 mutex_enter(&unp->unp_lock);
895 if (--unp->unp_refcnt < 0) 898 if (--unp->unp_refcnt < 0)
896 cv_broadcast(&unp->unp_detachcv); 899 cv_broadcast(&unp->unp_detachcv);
897 mutex_exit(&unp->unp_lock); 900 mutex_exit(&unp->unp_lock);
898} 901}
899 902
900void 903void
901usbnet_unlock_mii_un_locked(struct usbnet *un) 904usbnet_unlock_mii_un_locked(struct usbnet *un)
902{ 905{
903 struct usbnet_private * const unp = un->un_pri; 906 struct usbnet_private * const unp = un->un_pri;
904 907
905 usbnet_isowned(un); 908 usbnet_isowned(un);
906 909
907 mutex_exit(&unp->unp_miilock); 910 mutex_exit(&unp->unp_miilock);
908 if (--unp->unp_refcnt < 0) 911 if (--unp->unp_refcnt < 0)
909 cv_broadcast(&unp->unp_detachcv); 912 cv_broadcast(&unp->unp_detachcv);
910} 913}
911 914
912kmutex_t * 915kmutex_t *
913usbnet_mutex_mii(struct usbnet *un) 916usbnet_mutex_mii(struct usbnet *un)
914{ 917{
915 struct usbnet_private * const unp = un->un_pri; 918 struct usbnet_private * const unp = un->un_pri;
916 919
917 return &unp->unp_miilock; 920 return &unp->unp_miilock;
918} 921}
919 922
920int 923int
921usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val) 924usbnet_mii_readreg(device_t dev, int phy, int reg, uint16_t *val)
922{ 925{
923 USBNETHIST_FUNC(); 926 USBNETHIST_FUNC();
924 struct usbnet * const un = device_private(dev); 927 struct usbnet * const un = device_private(dev);
925 struct usbnet_private * const unp = un->un_pri; 928 struct usbnet_private * const unp = un->un_pri;
926 int err; 929 int err;
927 930
928 mutex_enter(&unp->unp_lock); 931 mutex_enter(&unp->unp_lock);
929 if (unp->unp_dying) { 932 if (unp->unp_dying) {
930 mutex_exit(&unp->unp_lock); 933 mutex_exit(&unp->unp_lock);
931 return EIO; 934 return EIO;
932 } 935 }
933 936
934 usbnet_lock_mii_un_locked(un); 937 usbnet_lock_mii_un_locked(un);
935 mutex_exit(&unp->unp_lock); 938 mutex_exit(&unp->unp_lock);
936 err = uno_read_reg(un, phy, reg, val); 939 err = uno_read_reg(un, phy, reg, val);
937 usbnet_unlock_mii(un); 940 usbnet_unlock_mii(un);
938 941
939 if (err) { 942 if (err) {
940 USBNETHIST_CALLARGS("read PHY failed: %d", err, 0, 0, 0); 943 USBNETHIST_CALLARGS("read PHY failed: %d", err, 0, 0, 0);
941 return err; 944 return err;
942 } 945 }
943 946
944 return 0; 947 return 0;
945} 948}
946 949
947int 950int
948usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val) 951usbnet_mii_writereg(device_t dev, int phy, int reg, uint16_t val)
949{ 952{
950 USBNETHIST_FUNC(); 953 USBNETHIST_FUNC();
951 struct usbnet * const un = device_private(dev); 954 struct usbnet * const un = device_private(dev);
952 struct usbnet_private * const unp = un->un_pri; 955 struct usbnet_private * const unp = un->un_pri;
953 int err; 956 int err;
954 957
955 mutex_enter(&unp->unp_lock); 958 mutex_enter(&unp->unp_lock);
956 if (unp->unp_dying) { 959 if (unp->unp_dying) {
957 mutex_exit(&unp->unp_lock); 960 mutex_exit(&unp->unp_lock);
958 return EIO; 961 return EIO;
959 } 962 }
960 963
961 usbnet_lock_mii_un_locked(un); 964 usbnet_lock_mii_un_locked(un);
962 mutex_exit(&unp->unp_lock); 965 mutex_exit(&unp->unp_lock);
963 err = uno_write_reg(un, phy, reg, val); 966 err = uno_write_reg(un, phy, reg, val);
964 usbnet_unlock_mii(un); 967 usbnet_unlock_mii(un);
965 968
966 if (err) { 969 if (err) {
967 USBNETHIST_CALLARGS("write PHY failed: %d", err, 0, 0, 0); 970 USBNETHIST_CALLARGS("write PHY failed: %d", err, 0, 0, 0);
968 return err; 971 return err;
969 } 972 }
970 973
971 return 0; 974 return 0;
972} 975}
973 976
974void 977void
975usbnet_mii_statchg(struct ifnet *ifp) 978usbnet_mii_statchg(struct ifnet *ifp)
976{ 979{
977 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 980 USBNETHIST_FUNC(); USBNETHIST_CALLED();
978 struct usbnet * const un = ifp->if_softc; 981 struct usbnet * const un = ifp->if_softc;
979 982
980 uno_mii_statchg(un, ifp); 983 uno_mii_statchg(un, ifp);
981} 984}
982 985
983static int 986static int
984usbnet_media_upd(struct ifnet *ifp) 987usbnet_media_upd(struct ifnet *ifp)
985{ 988{
986 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 989 USBNETHIST_FUNC(); USBNETHIST_CALLED();
987 struct usbnet * const un = ifp->if_softc; 990 struct usbnet * const un = ifp->if_softc;
988 struct usbnet_private * const unp = un->un_pri; 991 struct usbnet_private * const unp = un->un_pri;
989 struct mii_data * const mii = usbnet_mii(un); 992 struct mii_data * const mii = usbnet_mii(un);
990 993
991 if (unp->unp_dying) 994 if (unp->unp_dying)
992 return EIO; 995 return EIO;
993 996
994 unp->unp_link = false; 997 unp->unp_link = false;
995 998
996 if (mii->mii_instance) { 999 if (mii->mii_instance) {
997 struct mii_softc *miisc; 1000 struct mii_softc *miisc;
998 1001
999 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 1002 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1000 mii_phy_reset(miisc); 1003 mii_phy_reset(miisc);
1001 } 1004 }
1002 1005
1003 return ether_mediachange(ifp); 1006 return ether_mediachange(ifp);
1004} 1007}
1005 1008
1006/* ioctl */ 1009/* ioctl */
1007 1010
1008static int 1011static int
1009usbnet_ifflags_cb(struct ethercom *ec) 1012usbnet_ifflags_cb(struct ethercom *ec)
1010{ 1013{
1011 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1014 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1012 struct ifnet *ifp = &ec->ec_if; 1015 struct ifnet *ifp = &ec->ec_if;
1013 struct usbnet *un = ifp->if_softc; 1016 struct usbnet *un = ifp->if_softc;
1014 struct usbnet_private * const unp = un->un_pri; 1017 struct usbnet_private * const unp = un->un_pri;
1015 int rv = 0; 1018 int rv = 0;
1016 1019
1017 mutex_enter(&unp->unp_lock); 1020 mutex_enter(&unp->unp_lock);
1018 1021
1019 const int changed = ifp->if_flags ^ unp->unp_if_flags; 1022 const int changed = ifp->if_flags ^ unp->unp_if_flags;
1020 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) { 1023 if ((changed & ~(IFF_CANTCHANGE | IFF_DEBUG)) == 0) {
1021 unp->unp_if_flags = ifp->if_flags; 1024 unp->unp_if_flags = ifp->if_flags;
1022 if ((changed & IFF_PROMISC) != 0) 1025 if ((changed & IFF_PROMISC) != 0)
1023 rv = ENETRESET; 1026 rv = ENETRESET;
1024 } else { 1027 } else {
1025 rv = ENETRESET; 1028 rv = ENETRESET;
1026 } 1029 }
1027 1030
1028 mutex_exit(&unp->unp_lock); 1031 mutex_exit(&unp->unp_lock);
1029 1032
1030 return rv; 1033 return rv;
1031} 1034}
1032 1035
1033static int 1036static int
1034usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1037usbnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1035{ 1038{
1036 USBNETHIST_FUNC(); 1039 USBNETHIST_FUNC();
1037 struct usbnet * const un = ifp->if_softc; 1040 struct usbnet * const un = ifp->if_softc;
1038 struct usbnet_private * const unp __unused = un->un_pri; 1041 struct usbnet_private * const unp __unused = un->un_pri;
1039 int error; 1042 int error;
1040 1043
1041 USBNETHIST_CALLARGSN(11, "%d: enter %jx data %x", 1044 USBNETHIST_CALLARGSN(11, "%d: enter %jx data %x",
1042 unp->unp_number, cmd, (uintptr_t)data, 0); 1045 unp->unp_number, cmd, (uintptr_t)data, 0);
1043 1046
1044 if (un->un_ops->uno_override_ioctl) 1047 if (un->un_ops->uno_override_ioctl)
1045 return uno_override_ioctl(un, ifp, cmd, data); 1048 return uno_override_ioctl(un, ifp, cmd, data);
1046 1049
1047 error = ether_ioctl(ifp, cmd, data); 1050 error = ether_ioctl(ifp, cmd, data);
1048 if (error == ENETRESET) 1051 if (error == ENETRESET)
1049 error = uno_ioctl(un, ifp, cmd, data); 1052 error = uno_ioctl(un, ifp, cmd, data);
1050 1053
1051 return error; 1054 return error;
1052} 1055}
1053 1056
1054/* 1057/*
1055 * Generic stop network function: 1058 * Generic stop network function:
1056 * - mark as stopping 1059 * - mark as stopping
1057 * - call DD routine to stop the device 1060 * - call DD routine to stop the device
1058 * - turn off running, timer, statchg callout, link 1061 * - turn off running, timer, statchg callout, link
1059 * - stop transfers 1062 * - stop transfers
1060 * - free RX and TX resources 1063 * - free RX and TX resources
1061 * - close pipes 1064 * - close pipes
1062 * 1065 *
1063 * usbnet_stop() is exported for drivers to use, expects lock held. 1066 * usbnet_stop() is exported for drivers to use, expects lock held.
1064 * 1067 *
1065 * usbnet_stop_ifp() is for the if_stop handler. 1068 * usbnet_stop_ifp() is for the if_stop handler.
1066 */ 1069 */
1067void 1070void
1068usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable) 1071usbnet_stop(struct usbnet *un, struct ifnet *ifp, int disable)
1069{ 1072{
1070 struct usbnet_private * const unp = un->un_pri; 1073 struct usbnet_private * const unp = un->un_pri;
1071 1074
1072 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1075 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1073 1076
1074 usbnet_isowned(un); 1077 usbnet_isowned(un);
1075 1078
1076 mutex_enter(&unp->unp_rxlock); 1079 mutex_enter(&unp->unp_rxlock);
1077 mutex_enter(&unp->unp_txlock); 1080 mutex_enter(&unp->unp_txlock);
1078 unp->unp_stopping = true; 1081 unp->unp_stopping = true;
1079 mutex_exit(&unp->unp_txlock); 1082 mutex_exit(&unp->unp_txlock);
1080 mutex_exit(&unp->unp_rxlock); 1083 mutex_exit(&unp->unp_rxlock);
1081 1084
1082 uno_stop(un, ifp, disable); 1085 uno_stop(un, ifp, disable);
1083 1086
1084 /* 1087 /*
1085 * XXXSMP Would like to 1088 * XXXSMP Would like to
1086 * KASSERT(IFNET_LOCKED(ifp)) 1089 * KASSERT(IFNET_LOCKED(ifp))
1087 * here but the locking order is: 1090 * here but the locking order is:
1088 * ifnet -> unlock -> rxlock -> txlock 1091 * ifnet -> unlock -> rxlock -> txlock
1089 * and unlock is already held. 1092 * and unlock is already held.
1090 */ 1093 */
1091 ifp->if_flags &= ~IFF_RUNNING; 1094 ifp->if_flags &= ~IFF_RUNNING;
1092 unp->unp_timer = 0; 1095 unp->unp_timer = 0;
1093 1096
1094 callout_halt(&unp->unp_stat_ch, &unp->unp_lock); 1097 callout_halt(&unp->unp_stat_ch, &unp->unp_lock);
1095 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER, 1098 usb_rem_task_wait(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER,
1096 &unp->unp_lock); 1099 &unp->unp_lock);
1097 1100
1098 /* Stop transfers. */ 1101 /* Stop transfers. */
1099 usbnet_ep_stop_pipes(un); 1102 usbnet_ep_stop_pipes(un);
1100 1103
1101 /* Free RX/TX resources. */ 1104 /* Free RX/TX resources. */
1102 usbnet_rx_list_fini(un); 1105 usbnet_rx_list_fini(un);
1103 usbnet_tx_list_fini(un); 1106 usbnet_tx_list_fini(un);
1104 1107
1105 /* Close pipes. */ 1108 /* Close pipes. */
1106 usbnet_ep_close_pipes(un); 1109 usbnet_ep_close_pipes(un);
1107} 1110}
1108 1111
1109static void 1112static void
1110usbnet_stop_ifp(struct ifnet *ifp, int disable) 1113usbnet_stop_ifp(struct ifnet *ifp, int disable)
1111{ 1114{
1112 struct usbnet * const un = ifp->if_softc; 1115 struct usbnet * const un = ifp->if_softc;
1113 struct usbnet_private * const unp = un->un_pri; 1116 struct usbnet_private * const unp = un->un_pri;
1114 1117
1115 mutex_enter(&unp->unp_lock); 1118 mutex_enter(&unp->unp_lock);
1116 usbnet_stop(un, ifp, disable); 1119 usbnet_stop(un, ifp, disable);
1117 mutex_exit(&unp->unp_lock); 1120 mutex_exit(&unp->unp_lock);
1118} 1121}
1119 1122
1120/* 1123/*
1121 * Generic tick task function. 1124 * Generic tick task function.
1122 * 1125 *
1123 * usbnet_tick() is triggered from a callout, and triggers a call to 1126 * usbnet_tick() is triggered from a callout, and triggers a call to
1124 * usbnet_tick_task() from the usb_task subsystem. 1127 * usbnet_tick_task() from the usb_task subsystem.
1125 */ 1128 */
1126static void 1129static void
1127usbnet_tick(void *arg) 1130usbnet_tick(void *arg)
1128{ 1131{
1129 USBNETHIST_FUNC(); 1132 USBNETHIST_FUNC();
1130 struct usbnet * const un = arg; 1133 struct usbnet * const un = arg;
1131 struct usbnet_private * const unp = un->un_pri; 1134 struct usbnet_private * const unp = un->un_pri;
1132 1135
1133 USBNETHIST_CALLARGSN(10, "%d: enter", unp->unp_number, 0, 0, 0); 1136 USBNETHIST_CALLARGSN(10, "%d: enter", unp->unp_number, 0, 0, 0);
1134 1137
1135 if (unp != NULL && !unp->unp_stopping && !unp->unp_dying) { 1138 if (unp != NULL && !unp->unp_stopping && !unp->unp_dying) {
1136 /* Perform periodic stuff in process context */ 1139 /* Perform periodic stuff in process context */
1137 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER); 1140 usb_add_task(un->un_udev, &unp->unp_ticktask, USB_TASKQ_DRIVER);
1138 } 1141 }
1139} 1142}
1140 1143
1141static void 1144static void
1142usbnet_watchdog(struct ifnet *ifp) 1145usbnet_watchdog(struct ifnet *ifp)
1143{ 1146{
1144 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1147 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1145 struct usbnet * const un = ifp->if_softc; 1148 struct usbnet * const un = ifp->if_softc;
1146 struct usbnet_private * const unp = un->un_pri; 1149 struct usbnet_private * const unp = un->un_pri;
1147 struct usbnet_cdata * const cd = un_cdata(un); 1150 struct usbnet_cdata * const cd = un_cdata(un);
1148 usbd_status err; 1151 usbd_status err;
1149 1152
1150 ifp->if_oerrors++; 1153 ifp->if_oerrors++;
1151 aprint_error_dev(un->un_dev, "watchdog timeout\n"); 1154 aprint_error_dev(un->un_dev, "watchdog timeout\n");
1152 1155
1153 if (cd->uncd_tx_cnt > 0) { 1156 if (cd->uncd_tx_cnt > 0) {
1154 DPRINTF("uncd_tx_cnt=%u non zero, aborting pipe", 0, 0, 0, 0); 1157 DPRINTF("uncd_tx_cnt=%u non zero, aborting pipe", 0, 0, 0, 0);
1155 err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]); 1158 err = usbd_abort_pipe(unp->unp_ep[USBNET_ENDPT_TX]);
1156 if (err) 1159 if (err)
1157 aprint_error_dev(un->un_dev, "pipe abort failed: %s\n", 1160 aprint_error_dev(un->un_dev, "pipe abort failed: %s\n",
1158 usbd_errstr(err)); 1161 usbd_errstr(err));
1159 if (cd->uncd_tx_cnt != 0) 1162 if (cd->uncd_tx_cnt != 0)
1160 DPRINTF("uncd_tx_cnt now %u", cd->uncd_tx_cnt, 0, 0, 0); 1163 DPRINTF("uncd_tx_cnt now %u", cd->uncd_tx_cnt, 0, 0, 0);
1161 } 1164 }
1162 1165
1163 if (!IFQ_IS_EMPTY(&ifp->if_snd)) 1166 if (!IFQ_IS_EMPTY(&ifp->if_snd))
1164 (*ifp->if_start)(ifp); 1167 (*ifp->if_start)(ifp);
1165} 1168}
1166 1169
1167static void 1170static void
1168usbnet_tick_task(void *arg) 1171usbnet_tick_task(void *arg)
1169{ 1172{
1170 USBNETHIST_FUNC(); 1173 USBNETHIST_FUNC();
1171 struct usbnet * const un = arg; 1174 struct usbnet * const un = arg;
1172 struct usbnet_private * const unp = un->un_pri; 1175 struct usbnet_private * const unp = un->un_pri;
1173 1176
1174 if (unp == NULL) 1177 if (unp == NULL)
1175 return; 1178 return;
1176 1179
1177 USBNETHIST_CALLARGSN(8, "%d: enter", unp->unp_number, 0, 0, 0); 1180 USBNETHIST_CALLARGSN(8, "%d: enter", unp->unp_number, 0, 0, 0);
1178 1181
1179 mutex_enter(&unp->unp_lock); 1182 mutex_enter(&unp->unp_lock);
1180 if (unp->unp_stopping || unp->unp_dying) { 1183 if (unp->unp_stopping || unp->unp_dying) {
1181 mutex_exit(&unp->unp_lock); 1184 mutex_exit(&unp->unp_lock);
1182 return; 1185 return;
1183 } 1186 }
1184 1187
1185 struct ifnet * const ifp = usbnet_ifp(un); 1188 struct ifnet * const ifp = usbnet_ifp(un);
1186 struct mii_data * const mii = usbnet_mii(un); 1189 struct mii_data * const mii = usbnet_mii(un);
1187 1190
1188 KASSERT(ifp != NULL); /* embedded member */ 1191 KASSERT(ifp != NULL); /* embedded member */
1189 1192
1190 unp->unp_refcnt++; 1193 unp->unp_refcnt++;
1191 mutex_exit(&unp->unp_lock); 1194 mutex_exit(&unp->unp_lock);
1192 1195
1193 if (unp->unp_timer != 0 && --unp->unp_timer == 0) 1196 if (unp->unp_timer != 0 && --unp->unp_timer == 0)
1194 usbnet_watchdog(ifp); 1197 usbnet_watchdog(ifp);
1195 1198
1196 DPRINTFN(8, "mii %jx ifp %jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0); 1199 DPRINTFN(8, "mii %jx ifp %jx", (uintptr_t)mii, (uintptr_t)ifp, 0, 0);
1197 if (mii) { 1200 if (mii) {
1198 mii_tick(mii); 1201 mii_tick(mii);
1199 if (!unp->unp_link) 1202 if (!unp->unp_link)
1200 (*mii->mii_statchg)(ifp); 1203 (*mii->mii_statchg)(ifp);
1201 } 1204 }
1202 1205
1203 /* Call driver if requested. */ 1206 /* Call driver if requested. */
1204 uno_tick(un); 1207 uno_tick(un);
1205 1208
1206 mutex_enter(&unp->unp_lock); 1209 mutex_enter(&unp->unp_lock);
1207 if (--unp->unp_refcnt < 0) 1210 if (--unp->unp_refcnt < 0)
1208 cv_broadcast(&unp->unp_detachcv); 1211 cv_broadcast(&unp->unp_detachcv);
1209 if (!unp->unp_stopping && !unp->unp_dying) 1212 if (!unp->unp_stopping && !unp->unp_dying)
1210 callout_schedule(&unp->unp_stat_ch, hz); 1213 callout_schedule(&unp->unp_stat_ch, hz);
1211 mutex_exit(&unp->unp_lock); 1214 mutex_exit(&unp->unp_lock);
1212} 1215}
1213 1216
1214static int 1217static int
1215usbnet_init(struct ifnet *ifp) 1218usbnet_init(struct ifnet *ifp)
1216{ 1219{
1217 USBNETHIST_FUNC(); USBNETHIST_CALLED(); 1220 USBNETHIST_FUNC(); USBNETHIST_CALLED();
1218 struct usbnet * const un = ifp->if_softc; 1221 struct usbnet * const un = ifp->if_softc;
1219 1222
1220 return uno_init(un, ifp); 1223 return uno_init(un, ifp);
1221} 1224}
1222 1225
1223 1226
1224/* Various accessors. */ 1227/* Various accessors. */
1225 1228
1226void 1229void
1227usbnet_set_link(struct usbnet *un, bool link) 1230usbnet_set_link(struct usbnet *un, bool link)
1228{ 1231{