if_upgt.c revision 1.3.6.2 1 1.3.6.2 rmind /* $NetBSD: if_upgt.c,v 1.3.6.2 2011/03/05 20:54:12 rmind Exp $ */
2 1.3.6.2 rmind /* $OpenBSD: if_upgt.c,v 1.49 2010/04/20 22:05:43 tedu Exp $ */
3 1.3.6.2 rmind
4 1.3.6.2 rmind /*
5 1.3.6.2 rmind * Copyright (c) 2007 Marcus Glocker <mglocker (at) openbsd.org>
6 1.3.6.2 rmind *
7 1.3.6.2 rmind * Permission to use, copy, modify, and distribute this software for any
8 1.3.6.2 rmind * purpose with or without fee is hereby granted, provided that the above
9 1.3.6.2 rmind * copyright notice and this permission notice appear in all copies.
10 1.3.6.2 rmind *
11 1.3.6.2 rmind * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.3.6.2 rmind * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.3.6.2 rmind * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.3.6.2 rmind * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.3.6.2 rmind * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.3.6.2 rmind * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.3.6.2 rmind * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.3.6.2 rmind */
19 1.3.6.2 rmind
20 1.3.6.2 rmind #include <sys/cdefs.h>
21 1.3.6.2 rmind __KERNEL_RCSID(0, "$NetBSD: if_upgt.c,v 1.3.6.2 2011/03/05 20:54:12 rmind Exp $");
22 1.3.6.2 rmind
23 1.3.6.2 rmind #include <sys/param.h>
24 1.3.6.2 rmind #include <sys/callout.h>
25 1.3.6.2 rmind #include <sys/device.h>
26 1.3.6.2 rmind #include <sys/errno.h>
27 1.3.6.2 rmind #include <sys/kernel.h>
28 1.3.6.2 rmind #include <sys/kthread.h>
29 1.3.6.2 rmind #include <sys/mbuf.h>
30 1.3.6.2 rmind #include <sys/proc.h>
31 1.3.6.2 rmind #include <sys/sockio.h>
32 1.3.6.2 rmind #include <sys/systm.h>
33 1.3.6.2 rmind #include <sys/vnode.h>
34 1.3.6.2 rmind
35 1.3.6.2 rmind #include <sys/bus.h>
36 1.3.6.2 rmind #include <sys/endian.h>
37 1.3.6.2 rmind #include <sys/intr.h>
38 1.3.6.2 rmind
39 1.3.6.2 rmind #include <net/bpf.h>
40 1.3.6.2 rmind #include <net/if.h>
41 1.3.6.2 rmind #include <net/if_arp.h>
42 1.3.6.2 rmind #include <net/if_dl.h>
43 1.3.6.2 rmind #include <net/if_ether.h>
44 1.3.6.2 rmind #include <net/if_media.h>
45 1.3.6.2 rmind #include <net/if_types.h>
46 1.3.6.2 rmind
47 1.3.6.2 rmind #include <net80211/ieee80211_var.h>
48 1.3.6.2 rmind #include <net80211/ieee80211_radiotap.h>
49 1.3.6.2 rmind
50 1.3.6.2 rmind #include <dev/firmload.h>
51 1.3.6.2 rmind
52 1.3.6.2 rmind #include <dev/usb/usb.h>
53 1.3.6.2 rmind #include <dev/usb/usbdi.h>
54 1.3.6.2 rmind #include <dev/usb/usbdi_util.h>
55 1.3.6.2 rmind #include <dev/usb/usbdevs.h>
56 1.3.6.2 rmind
57 1.3.6.2 rmind #include <dev/usb/if_upgtvar.h>
58 1.3.6.2 rmind
59 1.3.6.2 rmind /*
60 1.3.6.2 rmind * Driver for the USB PrismGT devices.
61 1.3.6.2 rmind *
62 1.3.6.2 rmind * For now just USB 2.0 devices with the GW3887 chipset are supported.
63 1.3.6.2 rmind * The driver has been written based on the firmware version 2.13.1.0_LM87.
64 1.3.6.2 rmind *
65 1.3.6.2 rmind * TODO's:
66 1.3.6.2 rmind * - Fix MONITOR mode (MAC filter).
67 1.3.6.2 rmind * - Add HOSTAP mode.
68 1.3.6.2 rmind * - Add IBSS mode.
69 1.3.6.2 rmind * - Support the USB 1.0 devices (NET2280, ISL3880, ISL3886 chipsets).
70 1.3.6.2 rmind *
71 1.3.6.2 rmind * Parts of this driver has been influenced by reading the p54u driver
72 1.3.6.2 rmind * written by Jean-Baptiste Note <jean-baptiste.note (at) m4x.org> and
73 1.3.6.2 rmind * Sebastien Bourdeauducq <lekernel (at) prism54.org>.
74 1.3.6.2 rmind */
75 1.3.6.2 rmind
76 1.3.6.2 rmind #ifdef UPGT_DEBUG
77 1.3.6.2 rmind int upgt_debug = 2;
78 1.3.6.2 rmind #define DPRINTF(l, x...) do { if ((l) <= upgt_debug) printf(x); } while (0)
79 1.3.6.2 rmind #else
80 1.3.6.2 rmind #define DPRINTF(l, x...)
81 1.3.6.2 rmind #endif
82 1.3.6.2 rmind
83 1.3.6.2 rmind /*
84 1.3.6.2 rmind * Prototypes.
85 1.3.6.2 rmind */
86 1.3.6.2 rmind static int upgt_match(device_t, cfdata_t, void *);
87 1.3.6.2 rmind static void upgt_attach(device_t, device_t, void *);
88 1.3.6.2 rmind static int upgt_detach(device_t, int);
89 1.3.6.2 rmind static int upgt_activate(device_t, devact_t);
90 1.3.6.2 rmind
91 1.3.6.2 rmind static void upgt_attach_hook(device_t);
92 1.3.6.2 rmind static int upgt_device_type(struct upgt_softc *, uint16_t, uint16_t);
93 1.3.6.2 rmind static int upgt_device_init(struct upgt_softc *);
94 1.3.6.2 rmind static int upgt_mem_init(struct upgt_softc *);
95 1.3.6.2 rmind static uint32_t upgt_mem_alloc(struct upgt_softc *);
96 1.3.6.2 rmind static void upgt_mem_free(struct upgt_softc *, uint32_t);
97 1.3.6.2 rmind static int upgt_fw_alloc(struct upgt_softc *);
98 1.3.6.2 rmind static void upgt_fw_free(struct upgt_softc *);
99 1.3.6.2 rmind static int upgt_fw_verify(struct upgt_softc *);
100 1.3.6.2 rmind static int upgt_fw_load(struct upgt_softc *);
101 1.3.6.2 rmind static int upgt_fw_copy(char *, char *, int);
102 1.3.6.2 rmind static int upgt_eeprom_read(struct upgt_softc *);
103 1.3.6.2 rmind static int upgt_eeprom_parse(struct upgt_softc *);
104 1.3.6.2 rmind static void upgt_eeprom_parse_hwrx(struct upgt_softc *, uint8_t *);
105 1.3.6.2 rmind static void upgt_eeprom_parse_freq3(struct upgt_softc *, uint8_t *, int);
106 1.3.6.2 rmind static void upgt_eeprom_parse_freq4(struct upgt_softc *, uint8_t *, int);
107 1.3.6.2 rmind static void upgt_eeprom_parse_freq6(struct upgt_softc *, uint8_t *, int);
108 1.3.6.2 rmind
109 1.3.6.2 rmind static int upgt_ioctl(struct ifnet *, u_long, void *);
110 1.3.6.2 rmind static int upgt_init(struct ifnet *);
111 1.3.6.2 rmind static void upgt_stop(struct upgt_softc *);
112 1.3.6.2 rmind static int upgt_media_change(struct ifnet *);
113 1.3.6.2 rmind static void upgt_newassoc(struct ieee80211_node *, int);
114 1.3.6.2 rmind static int upgt_newstate(struct ieee80211com *, enum ieee80211_state,
115 1.3.6.2 rmind int);
116 1.3.6.2 rmind static void upgt_newstate_task(void *);
117 1.3.6.2 rmind static void upgt_next_scan(void *);
118 1.3.6.2 rmind static void upgt_start(struct ifnet *);
119 1.3.6.2 rmind static void upgt_watchdog(struct ifnet *);
120 1.3.6.2 rmind static void upgt_tx_task(void *);
121 1.3.6.2 rmind static void upgt_tx_done(struct upgt_softc *, uint8_t *);
122 1.3.6.2 rmind static void upgt_rx_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
123 1.3.6.2 rmind static void upgt_rx(struct upgt_softc *, uint8_t *, int);
124 1.3.6.2 rmind static void upgt_setup_rates(struct upgt_softc *);
125 1.3.6.2 rmind static uint8_t upgt_rx_rate(struct upgt_softc *, const int);
126 1.3.6.2 rmind static int upgt_set_macfilter(struct upgt_softc *, uint8_t state);
127 1.3.6.2 rmind static int upgt_set_channel(struct upgt_softc *, unsigned);
128 1.3.6.2 rmind static void upgt_set_led(struct upgt_softc *, int);
129 1.3.6.2 rmind static void upgt_set_led_blink(void *);
130 1.3.6.2 rmind static int upgt_get_stats(struct upgt_softc *);
131 1.3.6.2 rmind
132 1.3.6.2 rmind static int upgt_alloc_tx(struct upgt_softc *);
133 1.3.6.2 rmind static int upgt_alloc_rx(struct upgt_softc *);
134 1.3.6.2 rmind static int upgt_alloc_cmd(struct upgt_softc *);
135 1.3.6.2 rmind static void upgt_free_tx(struct upgt_softc *);
136 1.3.6.2 rmind static void upgt_free_rx(struct upgt_softc *);
137 1.3.6.2 rmind static void upgt_free_cmd(struct upgt_softc *);
138 1.3.6.2 rmind static int upgt_bulk_xmit(struct upgt_softc *, struct upgt_data *,
139 1.3.6.2 rmind usbd_pipe_handle, uint32_t *, int);
140 1.3.6.2 rmind
141 1.3.6.2 rmind #if 0
142 1.3.6.2 rmind static void upgt_hexdump(void *, int);
143 1.3.6.2 rmind #endif
144 1.3.6.2 rmind static uint32_t upgt_crc32_le(const void *, size_t);
145 1.3.6.2 rmind static uint32_t upgt_chksum_le(const uint32_t *, size_t);
146 1.3.6.2 rmind
147 1.3.6.2 rmind CFATTACH_DECL_NEW(upgt, sizeof(struct upgt_softc),
148 1.3.6.2 rmind upgt_match, upgt_attach, upgt_detach, upgt_activate);
149 1.3.6.2 rmind
150 1.3.6.2 rmind static const struct usb_devno upgt_devs_1[] = {
151 1.3.6.2 rmind /* version 1 devices */
152 1.3.6.2 rmind { USB_VENDOR_ALCATELT, USB_PRODUCT_ALCATELT_ST120G }
153 1.3.6.2 rmind };
154 1.3.6.2 rmind
155 1.3.6.2 rmind static const struct usb_devno upgt_devs_2[] = {
156 1.3.6.2 rmind /* version 2 devices */
157 1.3.6.2 rmind { USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_PRISM_GT },
158 1.3.6.2 rmind { USB_VENDOR_ALCATELT, USB_PRODUCT_ALCATELT_ST121G },
159 1.3.6.2 rmind { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D7050 },
160 1.3.6.2 rmind { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54AG },
161 1.3.6.2 rmind { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_WUSB54GV2 },
162 1.3.6.2 rmind { USB_VENDOR_CONCEPTRONIC2, USB_PRODUCT_CONCEPTRONIC2_PRISM_GT },
163 1.3.6.2 rmind { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_CGWLUSB2GTST },
164 1.3.6.2 rmind { USB_VENDOR_DELL, USB_PRODUCT_DELL_PRISM_GT_1 },
165 1.3.6.2 rmind { USB_VENDOR_DELL, USB_PRODUCT_DELL_PRISM_GT_2 },
166 1.3.6.2 rmind { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DWLG122A2 },
167 1.3.6.2 rmind { USB_VENDOR_FSC, USB_PRODUCT_FSC_E5400 },
168 1.3.6.2 rmind { USB_VENDOR_GLOBESPAN, USB_PRODUCT_GLOBESPAN_PRISM_GT_1 },
169 1.3.6.2 rmind { USB_VENDOR_GLOBESPAN, USB_PRODUCT_GLOBESPAN_PRISM_GT_2 },
170 1.3.6.2 rmind { USB_VENDOR_INTERSIL, USB_PRODUCT_INTERSIL_PRISM_GT },
171 1.3.6.2 rmind { USB_VENDOR_PHEENET, USB_PRODUCT_PHEENET_GWU513 },
172 1.3.6.2 rmind { USB_VENDOR_PHILIPS, USB_PRODUCT_PHILIPS_CPWUA054 },
173 1.3.6.2 rmind { USB_VENDOR_SHARP, USB_PRODUCT_SHARP_RUITZ1016YCZZ },
174 1.3.6.2 rmind { USB_VENDOR_SMC, USB_PRODUCT_SMC_2862WG },
175 1.3.6.2 rmind { USB_VENDOR_USR, USB_PRODUCT_USR_USR5422 },
176 1.3.6.2 rmind { USB_VENDOR_WISTRONNEWEB, USB_PRODUCT_WISTRONNEWEB_UR045G },
177 1.3.6.2 rmind { USB_VENDOR_XYRATEX, USB_PRODUCT_XYRATEX_PRISM_GT_1 },
178 1.3.6.2 rmind { USB_VENDOR_XYRATEX, USB_PRODUCT_XYRATEX_PRISM_GT_2 },
179 1.3.6.2 rmind { USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_MD40900 },
180 1.3.6.2 rmind { USB_VENDOR_ZCOM, USB_PRODUCT_ZCOM_XG703A }
181 1.3.6.2 rmind };
182 1.3.6.2 rmind
183 1.3.6.2 rmind static int
184 1.3.6.2 rmind firmware_load(const char *dname, const char *iname, uint8_t **ucodep,
185 1.3.6.2 rmind size_t *sizep)
186 1.3.6.2 rmind {
187 1.3.6.2 rmind firmware_handle_t fh;
188 1.3.6.2 rmind int error;
189 1.3.6.2 rmind
190 1.3.6.2 rmind if ((error = firmware_open(dname, iname, &fh)) != 0)
191 1.3.6.2 rmind return error;
192 1.3.6.2 rmind *sizep = firmware_get_size(fh);
193 1.3.6.2 rmind if ((*ucodep = firmware_malloc(*sizep)) == NULL) {
194 1.3.6.2 rmind firmware_close(fh);
195 1.3.6.2 rmind return ENOMEM;
196 1.3.6.2 rmind }
197 1.3.6.2 rmind if ((error = firmware_read(fh, 0, *ucodep, *sizep)) != 0)
198 1.3.6.2 rmind firmware_free(*ucodep, *sizep);
199 1.3.6.2 rmind firmware_close(fh);
200 1.3.6.2 rmind
201 1.3.6.2 rmind return error;
202 1.3.6.2 rmind }
203 1.3.6.2 rmind
204 1.3.6.2 rmind static int
205 1.3.6.2 rmind upgt_match(device_t parent, cfdata_t match, void *aux)
206 1.3.6.2 rmind {
207 1.3.6.2 rmind struct usb_attach_arg *uaa = aux;
208 1.3.6.2 rmind
209 1.3.6.2 rmind if (usb_lookup(upgt_devs_1, uaa->vendor, uaa->product) != NULL)
210 1.3.6.2 rmind return UMATCH_VENDOR_PRODUCT;
211 1.3.6.2 rmind
212 1.3.6.2 rmind if (usb_lookup(upgt_devs_2, uaa->vendor, uaa->product) != NULL)
213 1.3.6.2 rmind return UMATCH_VENDOR_PRODUCT;
214 1.3.6.2 rmind
215 1.3.6.2 rmind return UMATCH_NONE;
216 1.3.6.2 rmind }
217 1.3.6.2 rmind
218 1.3.6.2 rmind static void
219 1.3.6.2 rmind upgt_attach(device_t parent, device_t self, void *aux)
220 1.3.6.2 rmind {
221 1.3.6.2 rmind struct upgt_softc *sc = device_private(self);
222 1.3.6.2 rmind struct usb_attach_arg *uaa = aux;
223 1.3.6.2 rmind usb_interface_descriptor_t *id;
224 1.3.6.2 rmind usb_endpoint_descriptor_t *ed;
225 1.3.6.2 rmind usbd_status error;
226 1.3.6.2 rmind char *devinfop;
227 1.3.6.2 rmind int i;
228 1.3.6.2 rmind
229 1.3.6.2 rmind aprint_naive("\n");
230 1.3.6.2 rmind aprint_normal("\n");
231 1.3.6.2 rmind
232 1.3.6.2 rmind /*
233 1.3.6.2 rmind * Attach USB device.
234 1.3.6.2 rmind */
235 1.3.6.2 rmind sc->sc_dev = self;
236 1.3.6.2 rmind sc->sc_udev = uaa->device;
237 1.3.6.2 rmind
238 1.3.6.2 rmind devinfop = usbd_devinfo_alloc(sc->sc_udev, 0);
239 1.3.6.2 rmind aprint_normal_dev(sc->sc_dev, "%s\n", devinfop);
240 1.3.6.2 rmind usbd_devinfo_free(devinfop);
241 1.3.6.2 rmind
242 1.3.6.2 rmind /* check device type */
243 1.3.6.2 rmind if (upgt_device_type(sc, uaa->vendor, uaa->product) != 0)
244 1.3.6.2 rmind return;
245 1.3.6.2 rmind
246 1.3.6.2 rmind /* set configuration number */
247 1.3.6.2 rmind if (usbd_set_config_no(sc->sc_udev, UPGT_CONFIG_NO, 0) != 0) {
248 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
249 1.3.6.2 rmind "could not set configuration no\n");
250 1.3.6.2 rmind return;
251 1.3.6.2 rmind }
252 1.3.6.2 rmind
253 1.3.6.2 rmind /* get the first interface handle */
254 1.3.6.2 rmind error = usbd_device2interface_handle(sc->sc_udev, UPGT_IFACE_INDEX,
255 1.3.6.2 rmind &sc->sc_iface);
256 1.3.6.2 rmind if (error != 0) {
257 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
258 1.3.6.2 rmind "could not get interface handle\n");
259 1.3.6.2 rmind return;
260 1.3.6.2 rmind }
261 1.3.6.2 rmind
262 1.3.6.2 rmind /* find endpoints */
263 1.3.6.2 rmind id = usbd_get_interface_descriptor(sc->sc_iface);
264 1.3.6.2 rmind sc->sc_rx_no = sc->sc_tx_no = -1;
265 1.3.6.2 rmind for (i = 0; i < id->bNumEndpoints; i++) {
266 1.3.6.2 rmind ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
267 1.3.6.2 rmind if (ed == NULL) {
268 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
269 1.3.6.2 rmind "no endpoint descriptor for iface %d\n", i);
270 1.3.6.2 rmind return;
271 1.3.6.2 rmind }
272 1.3.6.2 rmind
273 1.3.6.2 rmind if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
274 1.3.6.2 rmind UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
275 1.3.6.2 rmind sc->sc_tx_no = ed->bEndpointAddress;
276 1.3.6.2 rmind if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
277 1.3.6.2 rmind UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
278 1.3.6.2 rmind sc->sc_rx_no = ed->bEndpointAddress;
279 1.3.6.2 rmind
280 1.3.6.2 rmind /*
281 1.3.6.2 rmind * 0x01 TX pipe
282 1.3.6.2 rmind * 0x81 RX pipe
283 1.3.6.2 rmind *
284 1.3.6.2 rmind * Deprecated scheme (not used with fw version >2.5.6.x):
285 1.3.6.2 rmind * 0x02 TX MGMT pipe
286 1.3.6.2 rmind * 0x82 TX MGMT pipe
287 1.3.6.2 rmind */
288 1.3.6.2 rmind if (sc->sc_tx_no != -1 && sc->sc_rx_no != -1)
289 1.3.6.2 rmind break;
290 1.3.6.2 rmind }
291 1.3.6.2 rmind if (sc->sc_rx_no == -1 || sc->sc_tx_no == -1) {
292 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "missing endpoint\n");
293 1.3.6.2 rmind return;
294 1.3.6.2 rmind }
295 1.3.6.2 rmind
296 1.3.6.2 rmind /* setup tasks and timeouts */
297 1.3.6.2 rmind usb_init_task(&sc->sc_task_newstate, upgt_newstate_task, sc);
298 1.3.6.2 rmind usb_init_task(&sc->sc_task_tx, upgt_tx_task, sc);
299 1.3.6.2 rmind callout_init(&sc->scan_to, 0);
300 1.3.6.2 rmind callout_setfunc(&sc->scan_to, upgt_next_scan, sc);
301 1.3.6.2 rmind callout_init(&sc->led_to, 0);
302 1.3.6.2 rmind callout_setfunc(&sc->led_to, upgt_set_led_blink, sc);
303 1.3.6.2 rmind
304 1.3.6.2 rmind /*
305 1.3.6.2 rmind * Open TX and RX USB bulk pipes.
306 1.3.6.2 rmind */
307 1.3.6.2 rmind error = usbd_open_pipe(sc->sc_iface, sc->sc_tx_no, USBD_EXCLUSIVE_USE,
308 1.3.6.2 rmind &sc->sc_tx_pipeh);
309 1.3.6.2 rmind if (error != 0) {
310 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
311 1.3.6.2 rmind "could not open TX pipe: %s\n", usbd_errstr(error));
312 1.3.6.2 rmind goto fail;
313 1.3.6.2 rmind }
314 1.3.6.2 rmind error = usbd_open_pipe(sc->sc_iface, sc->sc_rx_no, USBD_EXCLUSIVE_USE,
315 1.3.6.2 rmind &sc->sc_rx_pipeh);
316 1.3.6.2 rmind if (error != 0) {
317 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "could not open RX pipe: %s\n",
318 1.3.6.2 rmind usbd_errstr(error));
319 1.3.6.2 rmind goto fail;
320 1.3.6.2 rmind }
321 1.3.6.2 rmind
322 1.3.6.2 rmind /*
323 1.3.6.2 rmind * Allocate TX, RX, and CMD xfers.
324 1.3.6.2 rmind */
325 1.3.6.2 rmind if (upgt_alloc_tx(sc) != 0)
326 1.3.6.2 rmind goto fail;
327 1.3.6.2 rmind if (upgt_alloc_rx(sc) != 0)
328 1.3.6.2 rmind goto fail;
329 1.3.6.2 rmind if (upgt_alloc_cmd(sc) != 0)
330 1.3.6.2 rmind goto fail;
331 1.3.6.2 rmind
332 1.3.6.2 rmind /*
333 1.3.6.2 rmind * We need the firmware loaded from file system to complete the attach.
334 1.3.6.2 rmind */
335 1.3.6.2 rmind config_mountroot(self, upgt_attach_hook);
336 1.3.6.2 rmind
337 1.3.6.2 rmind return;
338 1.3.6.2 rmind fail:
339 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "%s failed\n", __func__);
340 1.3.6.2 rmind }
341 1.3.6.2 rmind
342 1.3.6.2 rmind static void
343 1.3.6.2 rmind upgt_attach_hook(device_t arg)
344 1.3.6.2 rmind {
345 1.3.6.2 rmind struct upgt_softc *sc = device_private(arg);
346 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
347 1.3.6.2 rmind struct ifnet *ifp = &sc->sc_if;
348 1.3.6.2 rmind usbd_status error;
349 1.3.6.2 rmind int i;
350 1.3.6.2 rmind
351 1.3.6.2 rmind /*
352 1.3.6.2 rmind * Load firmware file into memory.
353 1.3.6.2 rmind */
354 1.3.6.2 rmind if (upgt_fw_alloc(sc) != 0)
355 1.3.6.2 rmind goto fail;
356 1.3.6.2 rmind
357 1.3.6.2 rmind /*
358 1.3.6.2 rmind * Initialize the device.
359 1.3.6.2 rmind */
360 1.3.6.2 rmind if (upgt_device_init(sc) != 0)
361 1.3.6.2 rmind goto fail;
362 1.3.6.2 rmind
363 1.3.6.2 rmind /*
364 1.3.6.2 rmind * Verify the firmware.
365 1.3.6.2 rmind */
366 1.3.6.2 rmind if (upgt_fw_verify(sc) != 0)
367 1.3.6.2 rmind goto fail;
368 1.3.6.2 rmind
369 1.3.6.2 rmind /*
370 1.3.6.2 rmind * Calculate device memory space.
371 1.3.6.2 rmind */
372 1.3.6.2 rmind if (sc->sc_memaddr_frame_start == 0 || sc->sc_memaddr_frame_end == 0) {
373 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
374 1.3.6.2 rmind "could not find memory space addresses on FW\n");
375 1.3.6.2 rmind goto fail;
376 1.3.6.2 rmind }
377 1.3.6.2 rmind sc->sc_memaddr_frame_end -= UPGT_MEMSIZE_RX + 1;
378 1.3.6.2 rmind sc->sc_memaddr_rx_start = sc->sc_memaddr_frame_end + 1;
379 1.3.6.2 rmind
380 1.3.6.2 rmind DPRINTF(1, "%s: memory address frame start=0x%08x\n",
381 1.3.6.2 rmind device_xname(sc->sc_dev), sc->sc_memaddr_frame_start);
382 1.3.6.2 rmind DPRINTF(1, "%s: memory address frame end=0x%08x\n",
383 1.3.6.2 rmind device_xname(sc->sc_dev), sc->sc_memaddr_frame_end);
384 1.3.6.2 rmind DPRINTF(1, "%s: memory address rx start=0x%08x\n",
385 1.3.6.2 rmind device_xname(sc->sc_dev), sc->sc_memaddr_rx_start);
386 1.3.6.2 rmind
387 1.3.6.2 rmind upgt_mem_init(sc);
388 1.3.6.2 rmind
389 1.3.6.2 rmind /*
390 1.3.6.2 rmind * Load the firmware.
391 1.3.6.2 rmind */
392 1.3.6.2 rmind if (upgt_fw_load(sc) != 0)
393 1.3.6.2 rmind goto fail;
394 1.3.6.2 rmind
395 1.3.6.2 rmind /*
396 1.3.6.2 rmind * Startup the RX pipe.
397 1.3.6.2 rmind */
398 1.3.6.2 rmind struct upgt_data *data_rx = &sc->rx_data;
399 1.3.6.2 rmind
400 1.3.6.2 rmind usbd_setup_xfer(data_rx->xfer, sc->sc_rx_pipeh, data_rx, data_rx->buf,
401 1.3.6.2 rmind MCLBYTES, USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upgt_rx_cb);
402 1.3.6.2 rmind error = usbd_transfer(data_rx->xfer);
403 1.3.6.2 rmind if (error != USBD_NORMAL_COMPLETION && error != USBD_IN_PROGRESS) {
404 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
405 1.3.6.2 rmind "could not queue RX transfer\n");
406 1.3.6.2 rmind goto fail;
407 1.3.6.2 rmind }
408 1.3.6.2 rmind usbd_delay_ms(sc->sc_udev, 100);
409 1.3.6.2 rmind
410 1.3.6.2 rmind /*
411 1.3.6.2 rmind * Read the whole EEPROM content and parse it.
412 1.3.6.2 rmind */
413 1.3.6.2 rmind if (upgt_eeprom_read(sc) != 0)
414 1.3.6.2 rmind goto fail;
415 1.3.6.2 rmind if (upgt_eeprom_parse(sc) != 0)
416 1.3.6.2 rmind goto fail;
417 1.3.6.2 rmind
418 1.3.6.2 rmind /*
419 1.3.6.2 rmind * Setup the 802.11 device.
420 1.3.6.2 rmind */
421 1.3.6.2 rmind ic->ic_ifp = ifp;
422 1.3.6.2 rmind ic->ic_phytype = IEEE80211_T_OFDM;
423 1.3.6.2 rmind ic->ic_opmode = IEEE80211_M_STA;
424 1.3.6.2 rmind ic->ic_state = IEEE80211_S_INIT;
425 1.3.6.2 rmind ic->ic_caps =
426 1.3.6.2 rmind IEEE80211_C_MONITOR |
427 1.3.6.2 rmind IEEE80211_C_SHPREAMBLE |
428 1.3.6.2 rmind IEEE80211_C_SHSLOT;
429 1.3.6.2 rmind
430 1.3.6.2 rmind ic->ic_sup_rates[IEEE80211_MODE_11B] = ieee80211_std_rateset_11b;
431 1.3.6.2 rmind ic->ic_sup_rates[IEEE80211_MODE_11G] = ieee80211_std_rateset_11g;
432 1.3.6.2 rmind
433 1.3.6.2 rmind for (i = 1; i <= 14; i++) {
434 1.3.6.2 rmind ic->ic_channels[i].ic_freq =
435 1.3.6.2 rmind ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
436 1.3.6.2 rmind ic->ic_channels[i].ic_flags =
437 1.3.6.2 rmind IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
438 1.3.6.2 rmind IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
439 1.3.6.2 rmind }
440 1.3.6.2 rmind
441 1.3.6.2 rmind ifp->if_softc = sc;
442 1.3.6.2 rmind ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
443 1.3.6.2 rmind ifp->if_init = upgt_init;
444 1.3.6.2 rmind ifp->if_ioctl = upgt_ioctl;
445 1.3.6.2 rmind ifp->if_start = upgt_start;
446 1.3.6.2 rmind ifp->if_watchdog = upgt_watchdog;
447 1.3.6.2 rmind IFQ_SET_READY(&ifp->if_snd);
448 1.3.6.2 rmind memcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
449 1.3.6.2 rmind
450 1.3.6.2 rmind if_attach(ifp);
451 1.3.6.2 rmind ieee80211_ifattach(ic);
452 1.3.6.2 rmind ic->ic_newassoc = upgt_newassoc;
453 1.3.6.2 rmind
454 1.3.6.2 rmind sc->sc_newstate = ic->ic_newstate;
455 1.3.6.2 rmind ic->ic_newstate = upgt_newstate;
456 1.3.6.2 rmind ieee80211_media_init(ic, upgt_media_change, ieee80211_media_status);
457 1.3.6.2 rmind
458 1.3.6.2 rmind bpf_attach2(ifp, DLT_IEEE802_11_RADIO,
459 1.3.6.2 rmind sizeof(struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN,
460 1.3.6.2 rmind &sc->sc_drvbpf);
461 1.3.6.2 rmind
462 1.3.6.2 rmind sc->sc_rxtap_len = sizeof(sc->sc_rxtapu);
463 1.3.6.2 rmind sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
464 1.3.6.2 rmind sc->sc_rxtap.wr_ihdr.it_present = htole32(UPGT_RX_RADIOTAP_PRESENT);
465 1.3.6.2 rmind
466 1.3.6.2 rmind sc->sc_txtap_len = sizeof(sc->sc_txtapu);
467 1.3.6.2 rmind sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
468 1.3.6.2 rmind sc->sc_txtap.wt_ihdr.it_present = htole32(UPGT_TX_RADIOTAP_PRESENT);
469 1.3.6.2 rmind
470 1.3.6.2 rmind aprint_normal_dev(sc->sc_dev, "address %s\n",
471 1.3.6.2 rmind ether_sprintf(ic->ic_myaddr));
472 1.3.6.2 rmind
473 1.3.6.2 rmind ieee80211_announce(ic);
474 1.3.6.2 rmind
475 1.3.6.2 rmind usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
476 1.3.6.2 rmind
477 1.3.6.2 rmind /* device attached */
478 1.3.6.2 rmind sc->sc_flags |= UPGT_DEVICE_ATTACHED;
479 1.3.6.2 rmind
480 1.3.6.2 rmind return;
481 1.3.6.2 rmind fail:
482 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "%s failed\n", __func__);
483 1.3.6.2 rmind }
484 1.3.6.2 rmind
485 1.3.6.2 rmind static int
486 1.3.6.2 rmind upgt_detach(device_t self, int flags)
487 1.3.6.2 rmind {
488 1.3.6.2 rmind struct upgt_softc *sc = device_private(self);
489 1.3.6.2 rmind struct ifnet *ifp = &sc->sc_if;
490 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
491 1.3.6.2 rmind int s;
492 1.3.6.2 rmind
493 1.3.6.2 rmind DPRINTF(1, "%s: %s\n", device_xname(sc->sc_dev), __func__);
494 1.3.6.2 rmind
495 1.3.6.2 rmind s = splnet();
496 1.3.6.2 rmind
497 1.3.6.2 rmind if (ifp->if_flags & IFF_RUNNING)
498 1.3.6.2 rmind upgt_stop(sc);
499 1.3.6.2 rmind
500 1.3.6.2 rmind /* remove tasks and timeouts */
501 1.3.6.2 rmind usb_rem_task(sc->sc_udev, &sc->sc_task_newstate);
502 1.3.6.2 rmind usb_rem_task(sc->sc_udev, &sc->sc_task_tx);
503 1.3.6.2 rmind callout_destroy(&sc->scan_to);
504 1.3.6.2 rmind callout_destroy(&sc->led_to);
505 1.3.6.2 rmind
506 1.3.6.2 rmind /* abort and close TX / RX pipes */
507 1.3.6.2 rmind if (sc->sc_tx_pipeh != NULL) {
508 1.3.6.2 rmind usbd_abort_pipe(sc->sc_tx_pipeh);
509 1.3.6.2 rmind usbd_close_pipe(sc->sc_tx_pipeh);
510 1.3.6.2 rmind }
511 1.3.6.2 rmind if (sc->sc_rx_pipeh != NULL) {
512 1.3.6.2 rmind usbd_abort_pipe(sc->sc_rx_pipeh);
513 1.3.6.2 rmind usbd_close_pipe(sc->sc_rx_pipeh);
514 1.3.6.2 rmind }
515 1.3.6.2 rmind
516 1.3.6.2 rmind /* free xfers */
517 1.3.6.2 rmind upgt_free_tx(sc);
518 1.3.6.2 rmind upgt_free_rx(sc);
519 1.3.6.2 rmind upgt_free_cmd(sc);
520 1.3.6.2 rmind
521 1.3.6.2 rmind /* free firmware */
522 1.3.6.2 rmind upgt_fw_free(sc);
523 1.3.6.2 rmind
524 1.3.6.2 rmind if (sc->sc_flags & UPGT_DEVICE_ATTACHED) {
525 1.3.6.2 rmind /* detach interface */
526 1.3.6.2 rmind bpf_detach(ifp);
527 1.3.6.2 rmind ieee80211_ifdetach(ic);
528 1.3.6.2 rmind if_detach(ifp);
529 1.3.6.2 rmind }
530 1.3.6.2 rmind
531 1.3.6.2 rmind splx(s);
532 1.3.6.2 rmind
533 1.3.6.2 rmind usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
534 1.3.6.2 rmind
535 1.3.6.2 rmind return 0;
536 1.3.6.2 rmind }
537 1.3.6.2 rmind
538 1.3.6.2 rmind static int
539 1.3.6.2 rmind upgt_activate(device_t self, devact_t act)
540 1.3.6.2 rmind {
541 1.3.6.2 rmind struct upgt_softc *sc = device_private(self);
542 1.3.6.2 rmind
543 1.3.6.2 rmind switch (act) {
544 1.3.6.2 rmind case DVACT_DEACTIVATE:
545 1.3.6.2 rmind if_deactivate(&sc->sc_if);
546 1.3.6.2 rmind return 0;
547 1.3.6.2 rmind default:
548 1.3.6.2 rmind return EOPNOTSUPP;
549 1.3.6.2 rmind }
550 1.3.6.2 rmind }
551 1.3.6.2 rmind
552 1.3.6.2 rmind static int
553 1.3.6.2 rmind upgt_device_type(struct upgt_softc *sc, uint16_t vendor, uint16_t product)
554 1.3.6.2 rmind {
555 1.3.6.2 rmind
556 1.3.6.2 rmind if (usb_lookup(upgt_devs_1, vendor, product) != NULL) {
557 1.3.6.2 rmind sc->sc_device_type = 1;
558 1.3.6.2 rmind /* XXX */
559 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
560 1.3.6.2 rmind "version 1 devices not supported yet\n");
561 1.3.6.2 rmind return 1;
562 1.3.6.2 rmind } else
563 1.3.6.2 rmind sc->sc_device_type = 2;
564 1.3.6.2 rmind
565 1.3.6.2 rmind return 0;
566 1.3.6.2 rmind }
567 1.3.6.2 rmind
568 1.3.6.2 rmind static int
569 1.3.6.2 rmind upgt_device_init(struct upgt_softc *sc)
570 1.3.6.2 rmind {
571 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
572 1.3.6.2 rmind const uint8_t init_cmd[] = { 0x7e, 0x7e, 0x7e, 0x7e };
573 1.3.6.2 rmind int len;
574 1.3.6.2 rmind
575 1.3.6.2 rmind len = sizeof(init_cmd);
576 1.3.6.2 rmind memcpy(data_cmd->buf, init_cmd, len);
577 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
578 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
579 1.3.6.2 rmind "could not send device init string\n");
580 1.3.6.2 rmind return EIO;
581 1.3.6.2 rmind }
582 1.3.6.2 rmind usbd_delay_ms(sc->sc_udev, 100);
583 1.3.6.2 rmind
584 1.3.6.2 rmind DPRINTF(1, "%s: device initialized\n", device_xname(sc->sc_dev));
585 1.3.6.2 rmind
586 1.3.6.2 rmind return 0;
587 1.3.6.2 rmind }
588 1.3.6.2 rmind
589 1.3.6.2 rmind static int
590 1.3.6.2 rmind upgt_mem_init(struct upgt_softc *sc)
591 1.3.6.2 rmind {
592 1.3.6.2 rmind int i;
593 1.3.6.2 rmind
594 1.3.6.2 rmind for (i = 0; i < UPGT_MEMORY_MAX_PAGES; i++) {
595 1.3.6.2 rmind sc->sc_memory.page[i].used = 0;
596 1.3.6.2 rmind
597 1.3.6.2 rmind if (i == 0) {
598 1.3.6.2 rmind /*
599 1.3.6.2 rmind * The first memory page is always reserved for
600 1.3.6.2 rmind * command data.
601 1.3.6.2 rmind */
602 1.3.6.2 rmind sc->sc_memory.page[i].addr =
603 1.3.6.2 rmind sc->sc_memaddr_frame_start + MCLBYTES;
604 1.3.6.2 rmind } else {
605 1.3.6.2 rmind sc->sc_memory.page[i].addr =
606 1.3.6.2 rmind sc->sc_memory.page[i - 1].addr + MCLBYTES;
607 1.3.6.2 rmind }
608 1.3.6.2 rmind
609 1.3.6.2 rmind if (sc->sc_memory.page[i].addr + MCLBYTES >=
610 1.3.6.2 rmind sc->sc_memaddr_frame_end)
611 1.3.6.2 rmind break;
612 1.3.6.2 rmind
613 1.3.6.2 rmind DPRINTF(2, "%s: memory address page %d=0x%08x\n",
614 1.3.6.2 rmind device_xname(sc->sc_dev), i, sc->sc_memory.page[i].addr);
615 1.3.6.2 rmind }
616 1.3.6.2 rmind
617 1.3.6.2 rmind sc->sc_memory.pages = i;
618 1.3.6.2 rmind
619 1.3.6.2 rmind DPRINTF(2, "%s: memory pages=%d\n",
620 1.3.6.2 rmind device_xname(sc->sc_dev), sc->sc_memory.pages);
621 1.3.6.2 rmind
622 1.3.6.2 rmind return 0;
623 1.3.6.2 rmind }
624 1.3.6.2 rmind
625 1.3.6.2 rmind static uint32_t
626 1.3.6.2 rmind upgt_mem_alloc(struct upgt_softc *sc)
627 1.3.6.2 rmind {
628 1.3.6.2 rmind int i;
629 1.3.6.2 rmind
630 1.3.6.2 rmind for (i = 0; i < sc->sc_memory.pages; i++) {
631 1.3.6.2 rmind if (sc->sc_memory.page[i].used == 0) {
632 1.3.6.2 rmind sc->sc_memory.page[i].used = 1;
633 1.3.6.2 rmind return sc->sc_memory.page[i].addr;
634 1.3.6.2 rmind }
635 1.3.6.2 rmind }
636 1.3.6.2 rmind
637 1.3.6.2 rmind return 0;
638 1.3.6.2 rmind }
639 1.3.6.2 rmind
640 1.3.6.2 rmind static void
641 1.3.6.2 rmind upgt_mem_free(struct upgt_softc *sc, uint32_t addr)
642 1.3.6.2 rmind {
643 1.3.6.2 rmind int i;
644 1.3.6.2 rmind
645 1.3.6.2 rmind for (i = 0; i < sc->sc_memory.pages; i++) {
646 1.3.6.2 rmind if (sc->sc_memory.page[i].addr == addr) {
647 1.3.6.2 rmind sc->sc_memory.page[i].used = 0;
648 1.3.6.2 rmind return;
649 1.3.6.2 rmind }
650 1.3.6.2 rmind }
651 1.3.6.2 rmind
652 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "could not free memory address 0x%08x\n",
653 1.3.6.2 rmind addr);
654 1.3.6.2 rmind }
655 1.3.6.2 rmind
656 1.3.6.2 rmind
657 1.3.6.2 rmind static int
658 1.3.6.2 rmind upgt_fw_alloc(struct upgt_softc *sc)
659 1.3.6.2 rmind {
660 1.3.6.2 rmind const char *name = "upgt-gw3887";
661 1.3.6.2 rmind int error;
662 1.3.6.2 rmind
663 1.3.6.2 rmind if (sc->sc_fw == NULL) {
664 1.3.6.2 rmind error = firmware_load("upgt", name, &sc->sc_fw,
665 1.3.6.2 rmind &sc->sc_fw_size);
666 1.3.6.2 rmind if (error != 0) {
667 1.3.6.2 rmind if (error == ENOENT) {
668 1.3.6.2 rmind /*
669 1.3.6.2 rmind * The firmware file for upgt(4) is not in
670 1.3.6.2 rmind * the default distribution due to its lisence
671 1.3.6.2 rmind * so explicitly notify it if the firmware file
672 1.3.6.2 rmind * is not found.
673 1.3.6.2 rmind */
674 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
675 1.3.6.2 rmind "firmware file %s is not installed\n",
676 1.3.6.2 rmind name);
677 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
678 1.3.6.2 rmind "(it is not included in the default"
679 1.3.6.2 rmind " distribution)\n");
680 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
681 1.3.6.2 rmind "see upgt(4) man page for details about "
682 1.3.6.2 rmind "firmware installation\n");
683 1.3.6.2 rmind } else {
684 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
685 1.3.6.2 rmind "could not read firmware %s\n", name);
686 1.3.6.2 rmind }
687 1.3.6.2 rmind return EIO;
688 1.3.6.2 rmind }
689 1.3.6.2 rmind }
690 1.3.6.2 rmind
691 1.3.6.2 rmind DPRINTF(1, "%s: firmware %s allocated\n", device_xname(sc->sc_dev),
692 1.3.6.2 rmind name);
693 1.3.6.2 rmind
694 1.3.6.2 rmind return 0;
695 1.3.6.2 rmind }
696 1.3.6.2 rmind
697 1.3.6.2 rmind static void
698 1.3.6.2 rmind upgt_fw_free(struct upgt_softc *sc)
699 1.3.6.2 rmind {
700 1.3.6.2 rmind
701 1.3.6.2 rmind if (sc->sc_fw != NULL) {
702 1.3.6.2 rmind firmware_free(sc->sc_fw, sc->sc_fw_size);
703 1.3.6.2 rmind sc->sc_fw = NULL;
704 1.3.6.2 rmind DPRINTF(1, "%s: firmware freed\n", device_xname(sc->sc_dev));
705 1.3.6.2 rmind }
706 1.3.6.2 rmind }
707 1.3.6.2 rmind
708 1.3.6.2 rmind static int
709 1.3.6.2 rmind upgt_fw_verify(struct upgt_softc *sc)
710 1.3.6.2 rmind {
711 1.3.6.2 rmind struct upgt_fw_bra_option *bra_option;
712 1.3.6.2 rmind uint32_t bra_option_type, bra_option_len;
713 1.3.6.2 rmind uint32_t *uc;
714 1.3.6.2 rmind int offset, bra_end = 0;
715 1.3.6.2 rmind
716 1.3.6.2 rmind /*
717 1.3.6.2 rmind * Seek to beginning of Boot Record Area (BRA).
718 1.3.6.2 rmind */
719 1.3.6.2 rmind for (offset = 0; offset < sc->sc_fw_size; offset += sizeof(*uc)) {
720 1.3.6.2 rmind uc = (uint32_t *)(sc->sc_fw + offset);
721 1.3.6.2 rmind if (*uc == 0)
722 1.3.6.2 rmind break;
723 1.3.6.2 rmind }
724 1.3.6.2 rmind for (; offset < sc->sc_fw_size; offset += sizeof(*uc)) {
725 1.3.6.2 rmind uc = (uint32_t *)(sc->sc_fw + offset);
726 1.3.6.2 rmind if (*uc != 0)
727 1.3.6.2 rmind break;
728 1.3.6.2 rmind }
729 1.3.6.2 rmind if (offset == sc->sc_fw_size) {
730 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
731 1.3.6.2 rmind "firmware Boot Record Area not found\n");
732 1.3.6.2 rmind return EIO;
733 1.3.6.2 rmind }
734 1.3.6.2 rmind DPRINTF(1, "%s: firmware Boot Record Area found at offset %d\n",
735 1.3.6.2 rmind device_xname(sc->sc_dev), offset);
736 1.3.6.2 rmind
737 1.3.6.2 rmind /*
738 1.3.6.2 rmind * Parse Boot Record Area (BRA) options.
739 1.3.6.2 rmind */
740 1.3.6.2 rmind while (offset < sc->sc_fw_size && bra_end == 0) {
741 1.3.6.2 rmind /* get current BRA option */
742 1.3.6.2 rmind bra_option = (struct upgt_fw_bra_option *)(sc->sc_fw + offset);
743 1.3.6.2 rmind bra_option_type = le32toh(bra_option->type);
744 1.3.6.2 rmind bra_option_len = le32toh(bra_option->len) * sizeof(*uc);
745 1.3.6.2 rmind
746 1.3.6.2 rmind switch (bra_option_type) {
747 1.3.6.2 rmind case UPGT_BRA_TYPE_FW:
748 1.3.6.2 rmind DPRINTF(1, "%s: UPGT_BRA_TYPE_FW len=%d\n",
749 1.3.6.2 rmind device_xname(sc->sc_dev), bra_option_len);
750 1.3.6.2 rmind
751 1.3.6.2 rmind if (bra_option_len != UPGT_BRA_FWTYPE_SIZE) {
752 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
753 1.3.6.2 rmind "wrong UPGT_BRA_TYPE_FW len\n");
754 1.3.6.2 rmind return EIO;
755 1.3.6.2 rmind }
756 1.3.6.2 rmind if (memcmp(UPGT_BRA_FWTYPE_LM86, bra_option->data,
757 1.3.6.2 rmind bra_option_len) == 0) {
758 1.3.6.2 rmind sc->sc_fw_type = UPGT_FWTYPE_LM86;
759 1.3.6.2 rmind break;
760 1.3.6.2 rmind }
761 1.3.6.2 rmind if (memcmp(UPGT_BRA_FWTYPE_LM87, bra_option->data,
762 1.3.6.2 rmind bra_option_len) == 0) {
763 1.3.6.2 rmind sc->sc_fw_type = UPGT_FWTYPE_LM87;
764 1.3.6.2 rmind break;
765 1.3.6.2 rmind }
766 1.3.6.2 rmind if (memcmp(UPGT_BRA_FWTYPE_FMAC, bra_option->data,
767 1.3.6.2 rmind bra_option_len) == 0) {
768 1.3.6.2 rmind sc->sc_fw_type = UPGT_FWTYPE_FMAC;
769 1.3.6.2 rmind break;
770 1.3.6.2 rmind }
771 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
772 1.3.6.2 rmind "unsupported firmware type\n");
773 1.3.6.2 rmind return EIO;
774 1.3.6.2 rmind case UPGT_BRA_TYPE_VERSION:
775 1.3.6.2 rmind DPRINTF(1, "%s: UPGT_BRA_TYPE_VERSION len=%d\n",
776 1.3.6.2 rmind device_xname(sc->sc_dev), bra_option_len);
777 1.3.6.2 rmind break;
778 1.3.6.2 rmind case UPGT_BRA_TYPE_DEPIF:
779 1.3.6.2 rmind DPRINTF(1, "%s: UPGT_BRA_TYPE_DEPIF len=%d\n",
780 1.3.6.2 rmind device_xname(sc->sc_dev), bra_option_len);
781 1.3.6.2 rmind break;
782 1.3.6.2 rmind case UPGT_BRA_TYPE_EXPIF:
783 1.3.6.2 rmind DPRINTF(1, "%s: UPGT_BRA_TYPE_EXPIF len=%d\n",
784 1.3.6.2 rmind device_xname(sc->sc_dev), bra_option_len);
785 1.3.6.2 rmind break;
786 1.3.6.2 rmind case UPGT_BRA_TYPE_DESCR:
787 1.3.6.2 rmind DPRINTF(1, "%s: UPGT_BRA_TYPE_DESCR len=%d\n",
788 1.3.6.2 rmind device_xname(sc->sc_dev), bra_option_len);
789 1.3.6.2 rmind
790 1.3.6.2 rmind struct upgt_fw_bra_descr *descr =
791 1.3.6.2 rmind (struct upgt_fw_bra_descr *)bra_option->data;
792 1.3.6.2 rmind
793 1.3.6.2 rmind sc->sc_memaddr_frame_start =
794 1.3.6.2 rmind le32toh(descr->memaddr_space_start);
795 1.3.6.2 rmind sc->sc_memaddr_frame_end =
796 1.3.6.2 rmind le32toh(descr->memaddr_space_end);
797 1.3.6.2 rmind
798 1.3.6.2 rmind DPRINTF(2, "%s: memory address space start=0x%08x\n",
799 1.3.6.2 rmind device_xname(sc->sc_dev),
800 1.3.6.2 rmind sc->sc_memaddr_frame_start);
801 1.3.6.2 rmind DPRINTF(2, "%s: memory address space end=0x%08x\n",
802 1.3.6.2 rmind device_xname(sc->sc_dev),
803 1.3.6.2 rmind sc->sc_memaddr_frame_end);
804 1.3.6.2 rmind break;
805 1.3.6.2 rmind case UPGT_BRA_TYPE_END:
806 1.3.6.2 rmind DPRINTF(1, "%s: UPGT_BRA_TYPE_END len=%d\n",
807 1.3.6.2 rmind device_xname(sc->sc_dev), bra_option_len);
808 1.3.6.2 rmind bra_end = 1;
809 1.3.6.2 rmind break;
810 1.3.6.2 rmind default:
811 1.3.6.2 rmind DPRINTF(1, "%s: unknown BRA option len=%d\n",
812 1.3.6.2 rmind device_xname(sc->sc_dev), bra_option_len);
813 1.3.6.2 rmind return EIO;
814 1.3.6.2 rmind }
815 1.3.6.2 rmind
816 1.3.6.2 rmind /* jump to next BRA option */
817 1.3.6.2 rmind offset += sizeof(struct upgt_fw_bra_option) + bra_option_len;
818 1.3.6.2 rmind }
819 1.3.6.2 rmind
820 1.3.6.2 rmind DPRINTF(1, "%s: firmware verified\n", device_xname(sc->sc_dev));
821 1.3.6.2 rmind
822 1.3.6.2 rmind return 0;
823 1.3.6.2 rmind }
824 1.3.6.2 rmind
825 1.3.6.2 rmind static int
826 1.3.6.2 rmind upgt_fw_load(struct upgt_softc *sc)
827 1.3.6.2 rmind {
828 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
829 1.3.6.2 rmind struct upgt_data *data_rx = &sc->rx_data;
830 1.3.6.2 rmind struct upgt_fw_x2_header *x2;
831 1.3.6.2 rmind const uint8_t start_fwload_cmd[] = { 0x3c, 0x0d };
832 1.3.6.2 rmind int offset, bsize, n, i, len;
833 1.3.6.2 rmind uint32_t crc;
834 1.3.6.2 rmind
835 1.3.6.2 rmind /* send firmware start load command */
836 1.3.6.2 rmind len = sizeof(start_fwload_cmd);
837 1.3.6.2 rmind memcpy(data_cmd->buf, start_fwload_cmd, len);
838 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
839 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
840 1.3.6.2 rmind "could not send start_firmware_load command\n");
841 1.3.6.2 rmind return EIO;
842 1.3.6.2 rmind }
843 1.3.6.2 rmind
844 1.3.6.2 rmind /* send X2 header */
845 1.3.6.2 rmind len = sizeof(struct upgt_fw_x2_header);
846 1.3.6.2 rmind x2 = (struct upgt_fw_x2_header *)data_cmd->buf;
847 1.3.6.2 rmind memcpy(x2->signature, UPGT_X2_SIGNATURE, UPGT_X2_SIGNATURE_SIZE);
848 1.3.6.2 rmind x2->startaddr = htole32(UPGT_MEMADDR_FIRMWARE_START);
849 1.3.6.2 rmind x2->len = htole32(sc->sc_fw_size);
850 1.3.6.2 rmind x2->crc = upgt_crc32_le(data_cmd->buf + UPGT_X2_SIGNATURE_SIZE,
851 1.3.6.2 rmind sizeof(struct upgt_fw_x2_header) - UPGT_X2_SIGNATURE_SIZE -
852 1.3.6.2 rmind sizeof(uint32_t));
853 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
854 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
855 1.3.6.2 rmind "could not send firmware X2 header\n");
856 1.3.6.2 rmind return EIO;
857 1.3.6.2 rmind }
858 1.3.6.2 rmind
859 1.3.6.2 rmind /* download firmware */
860 1.3.6.2 rmind for (offset = 0; offset < sc->sc_fw_size; offset += bsize) {
861 1.3.6.2 rmind if (sc->sc_fw_size - offset > UPGT_FW_BLOCK_SIZE)
862 1.3.6.2 rmind bsize = UPGT_FW_BLOCK_SIZE;
863 1.3.6.2 rmind else
864 1.3.6.2 rmind bsize = sc->sc_fw_size - offset;
865 1.3.6.2 rmind
866 1.3.6.2 rmind n = upgt_fw_copy(sc->sc_fw + offset, data_cmd->buf, bsize);
867 1.3.6.2 rmind
868 1.3.6.2 rmind DPRINTF(1, "%s: FW offset=%d, read=%d, sent=%d\n",
869 1.3.6.2 rmind device_xname(sc->sc_dev), offset, n, bsize);
870 1.3.6.2 rmind
871 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &bsize, 0)
872 1.3.6.2 rmind != 0) {
873 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
874 1.3.6.2 rmind "error while downloading firmware block\n");
875 1.3.6.2 rmind return EIO;
876 1.3.6.2 rmind }
877 1.3.6.2 rmind
878 1.3.6.2 rmind bsize = n;
879 1.3.6.2 rmind }
880 1.3.6.2 rmind DPRINTF(1, "%s: firmware downloaded\n", device_xname(sc->sc_dev));
881 1.3.6.2 rmind
882 1.3.6.2 rmind /* load firmware */
883 1.3.6.2 rmind crc = upgt_crc32_le(sc->sc_fw, sc->sc_fw_size);
884 1.3.6.2 rmind *((uint32_t *)(data_cmd->buf) ) = crc;
885 1.3.6.2 rmind *((uint8_t *)(data_cmd->buf) + 4) = 'g';
886 1.3.6.2 rmind *((uint8_t *)(data_cmd->buf) + 5) = '\r';
887 1.3.6.2 rmind len = 6;
888 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
889 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
890 1.3.6.2 rmind "could not send load_firmware command\n");
891 1.3.6.2 rmind return EIO;
892 1.3.6.2 rmind }
893 1.3.6.2 rmind
894 1.3.6.2 rmind for (i = 0; i < UPGT_FIRMWARE_TIMEOUT; i++) {
895 1.3.6.2 rmind len = UPGT_FW_BLOCK_SIZE;
896 1.3.6.2 rmind memset(data_rx->buf, 0, 2);
897 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_rx, sc->sc_rx_pipeh, &len,
898 1.3.6.2 rmind USBD_SHORT_XFER_OK) != 0) {
899 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
900 1.3.6.2 rmind "could not read firmware response\n");
901 1.3.6.2 rmind return EIO;
902 1.3.6.2 rmind }
903 1.3.6.2 rmind
904 1.3.6.2 rmind if (memcmp(data_rx->buf, "OK", 2) == 0)
905 1.3.6.2 rmind break; /* firmware load was successful */
906 1.3.6.2 rmind }
907 1.3.6.2 rmind if (i == UPGT_FIRMWARE_TIMEOUT) {
908 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "firmware load failed\n");
909 1.3.6.2 rmind return EIO;
910 1.3.6.2 rmind }
911 1.3.6.2 rmind DPRINTF(1, "%s: firmware loaded\n", device_xname(sc->sc_dev));
912 1.3.6.2 rmind
913 1.3.6.2 rmind return 0;
914 1.3.6.2 rmind }
915 1.3.6.2 rmind
916 1.3.6.2 rmind /*
917 1.3.6.2 rmind * While copying the version 2 firmware, we need to replace two characters:
918 1.3.6.2 rmind *
919 1.3.6.2 rmind * 0x7e -> 0x7d 0x5e
920 1.3.6.2 rmind * 0x7d -> 0x7d 0x5d
921 1.3.6.2 rmind */
922 1.3.6.2 rmind static int
923 1.3.6.2 rmind upgt_fw_copy(char *src, char *dst, int size)
924 1.3.6.2 rmind {
925 1.3.6.2 rmind int i, j;
926 1.3.6.2 rmind
927 1.3.6.2 rmind for (i = 0, j = 0; i < size && j < size; i++) {
928 1.3.6.2 rmind switch (src[i]) {
929 1.3.6.2 rmind case 0x7e:
930 1.3.6.2 rmind dst[j] = 0x7d;
931 1.3.6.2 rmind j++;
932 1.3.6.2 rmind dst[j] = 0x5e;
933 1.3.6.2 rmind j++;
934 1.3.6.2 rmind break;
935 1.3.6.2 rmind case 0x7d:
936 1.3.6.2 rmind dst[j] = 0x7d;
937 1.3.6.2 rmind j++;
938 1.3.6.2 rmind dst[j] = 0x5d;
939 1.3.6.2 rmind j++;
940 1.3.6.2 rmind break;
941 1.3.6.2 rmind default:
942 1.3.6.2 rmind dst[j] = src[i];
943 1.3.6.2 rmind j++;
944 1.3.6.2 rmind break;
945 1.3.6.2 rmind }
946 1.3.6.2 rmind }
947 1.3.6.2 rmind
948 1.3.6.2 rmind return i;
949 1.3.6.2 rmind }
950 1.3.6.2 rmind
951 1.3.6.2 rmind static int
952 1.3.6.2 rmind upgt_eeprom_read(struct upgt_softc *sc)
953 1.3.6.2 rmind {
954 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
955 1.3.6.2 rmind struct upgt_lmac_mem *mem;
956 1.3.6.2 rmind struct upgt_lmac_eeprom *eeprom;
957 1.3.6.2 rmind int offset, block, len;
958 1.3.6.2 rmind
959 1.3.6.2 rmind offset = 0;
960 1.3.6.2 rmind block = UPGT_EEPROM_BLOCK_SIZE;
961 1.3.6.2 rmind while (offset < UPGT_EEPROM_SIZE) {
962 1.3.6.2 rmind DPRINTF(1, "%s: request EEPROM block (offset=%d, len=%d)\n",
963 1.3.6.2 rmind device_xname(sc->sc_dev), offset, block);
964 1.3.6.2 rmind
965 1.3.6.2 rmind /*
966 1.3.6.2 rmind * Transmit the URB containing the CMD data.
967 1.3.6.2 rmind */
968 1.3.6.2 rmind len = sizeof(*mem) + sizeof(*eeprom) + block;
969 1.3.6.2 rmind
970 1.3.6.2 rmind memset(data_cmd->buf, 0, len);
971 1.3.6.2 rmind
972 1.3.6.2 rmind mem = (struct upgt_lmac_mem *)data_cmd->buf;
973 1.3.6.2 rmind mem->addr = htole32(sc->sc_memaddr_frame_start +
974 1.3.6.2 rmind UPGT_MEMSIZE_FRAME_HEAD);
975 1.3.6.2 rmind
976 1.3.6.2 rmind eeprom = (struct upgt_lmac_eeprom *)(mem + 1);
977 1.3.6.2 rmind eeprom->header1.flags = 0;
978 1.3.6.2 rmind eeprom->header1.type = UPGT_H1_TYPE_CTRL;
979 1.3.6.2 rmind eeprom->header1.len = htole16((
980 1.3.6.2 rmind sizeof(struct upgt_lmac_eeprom) -
981 1.3.6.2 rmind sizeof(struct upgt_lmac_header)) + block);
982 1.3.6.2 rmind
983 1.3.6.2 rmind eeprom->header2.reqid = htole32(sc->sc_memaddr_frame_start);
984 1.3.6.2 rmind eeprom->header2.type = htole16(UPGT_H2_TYPE_EEPROM);
985 1.3.6.2 rmind eeprom->header2.flags = 0;
986 1.3.6.2 rmind
987 1.3.6.2 rmind eeprom->offset = htole16(offset);
988 1.3.6.2 rmind eeprom->len = htole16(block);
989 1.3.6.2 rmind
990 1.3.6.2 rmind mem->chksum = upgt_chksum_le((uint32_t *)eeprom,
991 1.3.6.2 rmind len - sizeof(*mem));
992 1.3.6.2 rmind
993 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len,
994 1.3.6.2 rmind USBD_FORCE_SHORT_XFER) != 0) {
995 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
996 1.3.6.2 rmind "could not transmit EEPROM data URB\n");
997 1.3.6.2 rmind return EIO;
998 1.3.6.2 rmind }
999 1.3.6.2 rmind if (tsleep(sc, 0, "eeprom_request", UPGT_USB_TIMEOUT)) {
1000 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
1001 1.3.6.2 rmind "timeout while waiting for EEPROM data\n");
1002 1.3.6.2 rmind return EIO;
1003 1.3.6.2 rmind }
1004 1.3.6.2 rmind
1005 1.3.6.2 rmind offset += block;
1006 1.3.6.2 rmind if (UPGT_EEPROM_SIZE - offset < block)
1007 1.3.6.2 rmind block = UPGT_EEPROM_SIZE - offset;
1008 1.3.6.2 rmind }
1009 1.3.6.2 rmind
1010 1.3.6.2 rmind return 0;
1011 1.3.6.2 rmind }
1012 1.3.6.2 rmind
1013 1.3.6.2 rmind static int
1014 1.3.6.2 rmind upgt_eeprom_parse(struct upgt_softc *sc)
1015 1.3.6.2 rmind {
1016 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1017 1.3.6.2 rmind struct upgt_eeprom_header *eeprom_header;
1018 1.3.6.2 rmind struct upgt_eeprom_option *eeprom_option;
1019 1.3.6.2 rmind uint16_t option_len;
1020 1.3.6.2 rmind uint16_t option_type;
1021 1.3.6.2 rmind uint16_t preamble_len;
1022 1.3.6.2 rmind int option_end = 0;
1023 1.3.6.2 rmind
1024 1.3.6.2 rmind /* calculate eeprom options start offset */
1025 1.3.6.2 rmind eeprom_header = (struct upgt_eeprom_header *)sc->sc_eeprom;
1026 1.3.6.2 rmind preamble_len = le16toh(eeprom_header->preamble_len);
1027 1.3.6.2 rmind eeprom_option = (struct upgt_eeprom_option *)(sc->sc_eeprom +
1028 1.3.6.2 rmind (sizeof(struct upgt_eeprom_header) + preamble_len));
1029 1.3.6.2 rmind
1030 1.3.6.2 rmind while (!option_end) {
1031 1.3.6.2 rmind /* the eeprom option length is stored in words */
1032 1.3.6.2 rmind option_len =
1033 1.3.6.2 rmind (le16toh(eeprom_option->len) - 1) * sizeof(uint16_t);
1034 1.3.6.2 rmind option_type =
1035 1.3.6.2 rmind le16toh(eeprom_option->type);
1036 1.3.6.2 rmind
1037 1.3.6.2 rmind switch (option_type) {
1038 1.3.6.2 rmind case UPGT_EEPROM_TYPE_NAME:
1039 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM name len=%d\n",
1040 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1041 1.3.6.2 rmind break;
1042 1.3.6.2 rmind case UPGT_EEPROM_TYPE_SERIAL:
1043 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM serial len=%d\n",
1044 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1045 1.3.6.2 rmind break;
1046 1.3.6.2 rmind case UPGT_EEPROM_TYPE_MAC:
1047 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM mac len=%d\n",
1048 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1049 1.3.6.2 rmind
1050 1.3.6.2 rmind IEEE80211_ADDR_COPY(ic->ic_myaddr, eeprom_option->data);
1051 1.3.6.2 rmind break;
1052 1.3.6.2 rmind case UPGT_EEPROM_TYPE_HWRX:
1053 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM hwrx len=%d\n",
1054 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1055 1.3.6.2 rmind
1056 1.3.6.2 rmind upgt_eeprom_parse_hwrx(sc, eeprom_option->data);
1057 1.3.6.2 rmind break;
1058 1.3.6.2 rmind case UPGT_EEPROM_TYPE_CHIP:
1059 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM chip len=%d\n",
1060 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1061 1.3.6.2 rmind break;
1062 1.3.6.2 rmind case UPGT_EEPROM_TYPE_FREQ3:
1063 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM freq3 len=%d\n",
1064 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1065 1.3.6.2 rmind
1066 1.3.6.2 rmind upgt_eeprom_parse_freq3(sc, eeprom_option->data,
1067 1.3.6.2 rmind option_len);
1068 1.3.6.2 rmind break;
1069 1.3.6.2 rmind case UPGT_EEPROM_TYPE_FREQ4:
1070 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM freq4 len=%d\n",
1071 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1072 1.3.6.2 rmind
1073 1.3.6.2 rmind upgt_eeprom_parse_freq4(sc, eeprom_option->data,
1074 1.3.6.2 rmind option_len);
1075 1.3.6.2 rmind break;
1076 1.3.6.2 rmind case UPGT_EEPROM_TYPE_FREQ5:
1077 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM freq5 len=%d\n",
1078 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1079 1.3.6.2 rmind break;
1080 1.3.6.2 rmind case UPGT_EEPROM_TYPE_FREQ6:
1081 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM freq6 len=%d\n",
1082 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1083 1.3.6.2 rmind
1084 1.3.6.2 rmind upgt_eeprom_parse_freq6(sc, eeprom_option->data,
1085 1.3.6.2 rmind option_len);
1086 1.3.6.2 rmind break;
1087 1.3.6.2 rmind case UPGT_EEPROM_TYPE_END:
1088 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM end len=%d\n",
1089 1.3.6.2 rmind device_xname(sc->sc_dev), option_len);
1090 1.3.6.2 rmind option_end = 1;
1091 1.3.6.2 rmind break;
1092 1.3.6.2 rmind case UPGT_EEPROM_TYPE_OFF:
1093 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM off without end option\n",
1094 1.3.6.2 rmind device_xname(sc->sc_dev));
1095 1.3.6.2 rmind return EIO;
1096 1.3.6.2 rmind default:
1097 1.3.6.2 rmind DPRINTF(1, "%s: EEPROM unknown type 0x%04x len=%d\n",
1098 1.3.6.2 rmind device_xname(sc->sc_dev), option_type, option_len);
1099 1.3.6.2 rmind break;
1100 1.3.6.2 rmind }
1101 1.3.6.2 rmind
1102 1.3.6.2 rmind /* jump to next EEPROM option */
1103 1.3.6.2 rmind eeprom_option = (struct upgt_eeprom_option *)
1104 1.3.6.2 rmind (eeprom_option->data + option_len);
1105 1.3.6.2 rmind }
1106 1.3.6.2 rmind
1107 1.3.6.2 rmind return 0;
1108 1.3.6.2 rmind }
1109 1.3.6.2 rmind
1110 1.3.6.2 rmind static void
1111 1.3.6.2 rmind upgt_eeprom_parse_hwrx(struct upgt_softc *sc, uint8_t *data)
1112 1.3.6.2 rmind {
1113 1.3.6.2 rmind struct upgt_eeprom_option_hwrx *option_hwrx;
1114 1.3.6.2 rmind
1115 1.3.6.2 rmind option_hwrx = (struct upgt_eeprom_option_hwrx *)data;
1116 1.3.6.2 rmind
1117 1.3.6.2 rmind sc->sc_eeprom_hwrx = option_hwrx->rxfilter - UPGT_EEPROM_RX_CONST;
1118 1.3.6.2 rmind
1119 1.3.6.2 rmind DPRINTF(2, "%s: hwrx option value=0x%04x\n",
1120 1.3.6.2 rmind device_xname(sc->sc_dev), sc->sc_eeprom_hwrx);
1121 1.3.6.2 rmind }
1122 1.3.6.2 rmind
1123 1.3.6.2 rmind static void
1124 1.3.6.2 rmind upgt_eeprom_parse_freq3(struct upgt_softc *sc, uint8_t *data, int len)
1125 1.3.6.2 rmind {
1126 1.3.6.2 rmind struct upgt_eeprom_freq3_header *freq3_header;
1127 1.3.6.2 rmind struct upgt_lmac_freq3 *freq3;
1128 1.3.6.2 rmind int i, elements, flags;
1129 1.3.6.2 rmind unsigned channel;
1130 1.3.6.2 rmind
1131 1.3.6.2 rmind freq3_header = (struct upgt_eeprom_freq3_header *)data;
1132 1.3.6.2 rmind freq3 = (struct upgt_lmac_freq3 *)(freq3_header + 1);
1133 1.3.6.2 rmind
1134 1.3.6.2 rmind flags = freq3_header->flags;
1135 1.3.6.2 rmind elements = freq3_header->elements;
1136 1.3.6.2 rmind
1137 1.3.6.2 rmind DPRINTF(2, "%s: flags=0x%02x\n", device_xname(sc->sc_dev), flags);
1138 1.3.6.2 rmind DPRINTF(2, "%s: elements=%d\n", device_xname(sc->sc_dev), elements);
1139 1.3.6.2 rmind
1140 1.3.6.2 rmind for (i = 0; i < elements; i++) {
1141 1.3.6.2 rmind channel = ieee80211_mhz2ieee(le16toh(freq3[i].freq), 0);
1142 1.3.6.2 rmind
1143 1.3.6.2 rmind sc->sc_eeprom_freq3[channel] = freq3[i];
1144 1.3.6.2 rmind
1145 1.3.6.2 rmind DPRINTF(2, "%s: frequence=%d, channel=%d\n",
1146 1.3.6.2 rmind device_xname(sc->sc_dev),
1147 1.3.6.2 rmind le16toh(sc->sc_eeprom_freq3[channel].freq), channel);
1148 1.3.6.2 rmind }
1149 1.3.6.2 rmind }
1150 1.3.6.2 rmind
1151 1.3.6.2 rmind static void
1152 1.3.6.2 rmind upgt_eeprom_parse_freq4(struct upgt_softc *sc, uint8_t *data, int len)
1153 1.3.6.2 rmind {
1154 1.3.6.2 rmind struct upgt_eeprom_freq4_header *freq4_header;
1155 1.3.6.2 rmind struct upgt_eeprom_freq4_1 *freq4_1;
1156 1.3.6.2 rmind struct upgt_eeprom_freq4_2 *freq4_2;
1157 1.3.6.2 rmind int i, j, elements, settings, flags;
1158 1.3.6.2 rmind unsigned channel;
1159 1.3.6.2 rmind
1160 1.3.6.2 rmind freq4_header = (struct upgt_eeprom_freq4_header *)data;
1161 1.3.6.2 rmind freq4_1 = (struct upgt_eeprom_freq4_1 *)(freq4_header + 1);
1162 1.3.6.2 rmind
1163 1.3.6.2 rmind flags = freq4_header->flags;
1164 1.3.6.2 rmind elements = freq4_header->elements;
1165 1.3.6.2 rmind settings = freq4_header->settings;
1166 1.3.6.2 rmind
1167 1.3.6.2 rmind /* we need this value later */
1168 1.3.6.2 rmind sc->sc_eeprom_freq6_settings = freq4_header->settings;
1169 1.3.6.2 rmind
1170 1.3.6.2 rmind DPRINTF(2, "%s: flags=0x%02x\n", device_xname(sc->sc_dev), flags);
1171 1.3.6.2 rmind DPRINTF(2, "%s: elements=%d\n", device_xname(sc->sc_dev), elements);
1172 1.3.6.2 rmind DPRINTF(2, "%s: settings=%d\n", device_xname(sc->sc_dev), settings);
1173 1.3.6.2 rmind
1174 1.3.6.2 rmind for (i = 0; i < elements; i++) {
1175 1.3.6.2 rmind channel = ieee80211_mhz2ieee(le16toh(freq4_1[i].freq), 0);
1176 1.3.6.2 rmind
1177 1.3.6.2 rmind freq4_2 = (struct upgt_eeprom_freq4_2 *)freq4_1[i].data;
1178 1.3.6.2 rmind
1179 1.3.6.2 rmind for (j = 0; j < settings; j++) {
1180 1.3.6.2 rmind sc->sc_eeprom_freq4[channel][j].cmd = freq4_2[j];
1181 1.3.6.2 rmind sc->sc_eeprom_freq4[channel][j].pad = 0;
1182 1.3.6.2 rmind }
1183 1.3.6.2 rmind
1184 1.3.6.2 rmind DPRINTF(2, "%s: frequence=%d, channel=%d\n",
1185 1.3.6.2 rmind device_xname(sc->sc_dev),
1186 1.3.6.2 rmind le16toh(freq4_1[i].freq), channel);
1187 1.3.6.2 rmind }
1188 1.3.6.2 rmind }
1189 1.3.6.2 rmind
1190 1.3.6.2 rmind static void
1191 1.3.6.2 rmind upgt_eeprom_parse_freq6(struct upgt_softc *sc, uint8_t *data, int len)
1192 1.3.6.2 rmind {
1193 1.3.6.2 rmind struct upgt_lmac_freq6 *freq6;
1194 1.3.6.2 rmind int i, elements;
1195 1.3.6.2 rmind unsigned channel;
1196 1.3.6.2 rmind
1197 1.3.6.2 rmind freq6 = (struct upgt_lmac_freq6 *)data;
1198 1.3.6.2 rmind
1199 1.3.6.2 rmind elements = len / sizeof(struct upgt_lmac_freq6);
1200 1.3.6.2 rmind
1201 1.3.6.2 rmind DPRINTF(2, "%s: elements=%d\n", device_xname(sc->sc_dev), elements);
1202 1.3.6.2 rmind
1203 1.3.6.2 rmind for (i = 0; i < elements; i++) {
1204 1.3.6.2 rmind channel = ieee80211_mhz2ieee(le16toh(freq6[i].freq), 0);
1205 1.3.6.2 rmind
1206 1.3.6.2 rmind sc->sc_eeprom_freq6[channel] = freq6[i];
1207 1.3.6.2 rmind
1208 1.3.6.2 rmind DPRINTF(2, "%s: frequence=%d, channel=%d\n",
1209 1.3.6.2 rmind device_xname(sc->sc_dev),
1210 1.3.6.2 rmind le16toh(sc->sc_eeprom_freq6[channel].freq), channel);
1211 1.3.6.2 rmind }
1212 1.3.6.2 rmind }
1213 1.3.6.2 rmind
1214 1.3.6.2 rmind static int
1215 1.3.6.2 rmind upgt_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1216 1.3.6.2 rmind {
1217 1.3.6.2 rmind struct upgt_softc *sc = ifp->if_softc;
1218 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1219 1.3.6.2 rmind int s, error = 0;
1220 1.3.6.2 rmind
1221 1.3.6.2 rmind s = splnet();
1222 1.3.6.2 rmind
1223 1.3.6.2 rmind switch (cmd) {
1224 1.3.6.2 rmind case SIOCSIFFLAGS:
1225 1.3.6.2 rmind if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1226 1.3.6.2 rmind break;
1227 1.3.6.2 rmind if (ifp->if_flags & IFF_UP) {
1228 1.3.6.2 rmind if ((ifp->if_flags & IFF_RUNNING) == 0)
1229 1.3.6.2 rmind upgt_init(ifp);
1230 1.3.6.2 rmind } else {
1231 1.3.6.2 rmind if (ifp->if_flags & IFF_RUNNING)
1232 1.3.6.2 rmind upgt_stop(sc);
1233 1.3.6.2 rmind }
1234 1.3.6.2 rmind break;
1235 1.3.6.2 rmind case SIOCADDMULTI:
1236 1.3.6.2 rmind case SIOCDELMULTI:
1237 1.3.6.2 rmind if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
1238 1.3.6.2 rmind /* setup multicast filter, etc */
1239 1.3.6.2 rmind error = 0;
1240 1.3.6.2 rmind }
1241 1.3.6.2 rmind break;
1242 1.3.6.2 rmind default:
1243 1.3.6.2 rmind error = ieee80211_ioctl(ic, cmd, data);
1244 1.3.6.2 rmind break;
1245 1.3.6.2 rmind }
1246 1.3.6.2 rmind
1247 1.3.6.2 rmind if (error == ENETRESET) {
1248 1.3.6.2 rmind if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1249 1.3.6.2 rmind (IFF_UP | IFF_RUNNING))
1250 1.3.6.2 rmind upgt_init(ifp);
1251 1.3.6.2 rmind error = 0;
1252 1.3.6.2 rmind }
1253 1.3.6.2 rmind
1254 1.3.6.2 rmind splx(s);
1255 1.3.6.2 rmind
1256 1.3.6.2 rmind return error;
1257 1.3.6.2 rmind }
1258 1.3.6.2 rmind
1259 1.3.6.2 rmind static int
1260 1.3.6.2 rmind upgt_init(struct ifnet *ifp)
1261 1.3.6.2 rmind {
1262 1.3.6.2 rmind struct upgt_softc *sc = ifp->if_softc;
1263 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1264 1.3.6.2 rmind
1265 1.3.6.2 rmind DPRINTF(1, "%s: %s\n", device_xname(sc->sc_dev), __func__);
1266 1.3.6.2 rmind
1267 1.3.6.2 rmind if (ifp->if_flags & IFF_RUNNING)
1268 1.3.6.2 rmind upgt_stop(sc);
1269 1.3.6.2 rmind
1270 1.3.6.2 rmind ifp->if_flags |= IFF_RUNNING;
1271 1.3.6.2 rmind ifp->if_flags &= ~IFF_OACTIVE;
1272 1.3.6.2 rmind
1273 1.3.6.2 rmind IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl));
1274 1.3.6.2 rmind
1275 1.3.6.2 rmind /* setup device rates */
1276 1.3.6.2 rmind upgt_setup_rates(sc);
1277 1.3.6.2 rmind
1278 1.3.6.2 rmind if (ic->ic_opmode == IEEE80211_M_MONITOR)
1279 1.3.6.2 rmind ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1280 1.3.6.2 rmind else
1281 1.3.6.2 rmind ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1282 1.3.6.2 rmind
1283 1.3.6.2 rmind return 0;
1284 1.3.6.2 rmind }
1285 1.3.6.2 rmind
1286 1.3.6.2 rmind static void
1287 1.3.6.2 rmind upgt_stop(struct upgt_softc *sc)
1288 1.3.6.2 rmind {
1289 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1290 1.3.6.2 rmind struct ifnet *ifp = &sc->sc_if;
1291 1.3.6.2 rmind
1292 1.3.6.2 rmind DPRINTF(1, "%s: %s\n", device_xname(sc->sc_dev), __func__);
1293 1.3.6.2 rmind
1294 1.3.6.2 rmind /* device down */
1295 1.3.6.2 rmind ifp->if_timer = 0;
1296 1.3.6.2 rmind ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1297 1.3.6.2 rmind
1298 1.3.6.2 rmind /* change device back to initial state */
1299 1.3.6.2 rmind ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1300 1.3.6.2 rmind }
1301 1.3.6.2 rmind
1302 1.3.6.2 rmind static int
1303 1.3.6.2 rmind upgt_media_change(struct ifnet *ifp)
1304 1.3.6.2 rmind {
1305 1.3.6.2 rmind struct upgt_softc *sc = ifp->if_softc;
1306 1.3.6.2 rmind int error;
1307 1.3.6.2 rmind
1308 1.3.6.2 rmind DPRINTF(1, "%s: %s\n", device_xname(sc->sc_dev), __func__);
1309 1.3.6.2 rmind
1310 1.3.6.2 rmind if ((error = ieee80211_media_change(ifp) != ENETRESET))
1311 1.3.6.2 rmind return error;
1312 1.3.6.2 rmind
1313 1.3.6.2 rmind if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
1314 1.3.6.2 rmind (IFF_UP | IFF_RUNNING)) {
1315 1.3.6.2 rmind /* give pending USB transfers a chance to finish */
1316 1.3.6.2 rmind usbd_delay_ms(sc->sc_udev, 100);
1317 1.3.6.2 rmind upgt_init(ifp);
1318 1.3.6.2 rmind }
1319 1.3.6.2 rmind
1320 1.3.6.2 rmind return 0;
1321 1.3.6.2 rmind }
1322 1.3.6.2 rmind
1323 1.3.6.2 rmind static void
1324 1.3.6.2 rmind upgt_newassoc(struct ieee80211_node *ni, int isnew)
1325 1.3.6.2 rmind {
1326 1.3.6.2 rmind
1327 1.3.6.2 rmind ni->ni_txrate = 0;
1328 1.3.6.2 rmind }
1329 1.3.6.2 rmind
1330 1.3.6.2 rmind static int
1331 1.3.6.2 rmind upgt_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
1332 1.3.6.2 rmind {
1333 1.3.6.2 rmind struct upgt_softc *sc = ic->ic_ifp->if_softc;
1334 1.3.6.2 rmind
1335 1.3.6.2 rmind usb_rem_task(sc->sc_udev, &sc->sc_task_newstate);
1336 1.3.6.2 rmind callout_stop(&sc->scan_to);
1337 1.3.6.2 rmind
1338 1.3.6.2 rmind /* do it in a process context */
1339 1.3.6.2 rmind sc->sc_state = nstate;
1340 1.3.6.2 rmind sc->sc_arg = arg;
1341 1.3.6.2 rmind usb_add_task(sc->sc_udev, &sc->sc_task_newstate, USB_TASKQ_DRIVER);
1342 1.3.6.2 rmind
1343 1.3.6.2 rmind return 0;
1344 1.3.6.2 rmind }
1345 1.3.6.2 rmind
1346 1.3.6.2 rmind static void
1347 1.3.6.2 rmind upgt_newstate_task(void *arg)
1348 1.3.6.2 rmind {
1349 1.3.6.2 rmind struct upgt_softc *sc = arg;
1350 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1351 1.3.6.2 rmind struct ieee80211_node *ni;
1352 1.3.6.2 rmind unsigned channel;
1353 1.3.6.2 rmind
1354 1.3.6.2 rmind mutex_enter(&sc->sc_mtx);
1355 1.3.6.2 rmind
1356 1.3.6.2 rmind switch (sc->sc_state) {
1357 1.3.6.2 rmind case IEEE80211_S_INIT:
1358 1.3.6.2 rmind DPRINTF(1, "%s: newstate is IEEE80211_S_INIT\n",
1359 1.3.6.2 rmind device_xname(sc->sc_dev));
1360 1.3.6.2 rmind
1361 1.3.6.2 rmind /* do not accept any frames if the device is down */
1362 1.3.6.2 rmind upgt_set_macfilter(sc, IEEE80211_S_INIT);
1363 1.3.6.2 rmind upgt_set_led(sc, UPGT_LED_OFF);
1364 1.3.6.2 rmind break;
1365 1.3.6.2 rmind case IEEE80211_S_SCAN:
1366 1.3.6.2 rmind DPRINTF(1, "%s: newstate is IEEE80211_S_SCAN\n",
1367 1.3.6.2 rmind device_xname(sc->sc_dev));
1368 1.3.6.2 rmind
1369 1.3.6.2 rmind channel = ieee80211_chan2ieee(ic, ic->ic_curchan);
1370 1.3.6.2 rmind upgt_set_channel(sc, channel);
1371 1.3.6.2 rmind upgt_set_macfilter(sc, IEEE80211_S_SCAN);
1372 1.3.6.2 rmind callout_schedule(&sc->scan_to, hz / 5);
1373 1.3.6.2 rmind break;
1374 1.3.6.2 rmind case IEEE80211_S_AUTH:
1375 1.3.6.2 rmind DPRINTF(1, "%s: newstate is IEEE80211_S_AUTH\n",
1376 1.3.6.2 rmind device_xname(sc->sc_dev));
1377 1.3.6.2 rmind
1378 1.3.6.2 rmind channel = ieee80211_chan2ieee(ic, ic->ic_curchan);
1379 1.3.6.2 rmind upgt_set_channel(sc, channel);
1380 1.3.6.2 rmind break;
1381 1.3.6.2 rmind case IEEE80211_S_ASSOC:
1382 1.3.6.2 rmind DPRINTF(1, "%s: newstate is IEEE80211_S_ASSOC\n",
1383 1.3.6.2 rmind device_xname(sc->sc_dev));
1384 1.3.6.2 rmind
1385 1.3.6.2 rmind channel = ieee80211_chan2ieee(ic, ic->ic_curchan);
1386 1.3.6.2 rmind upgt_set_channel(sc, channel);
1387 1.3.6.2 rmind break;
1388 1.3.6.2 rmind case IEEE80211_S_RUN:
1389 1.3.6.2 rmind DPRINTF(1, "%s: newstate is IEEE80211_S_RUN\n",
1390 1.3.6.2 rmind device_xname(sc->sc_dev));
1391 1.3.6.2 rmind
1392 1.3.6.2 rmind channel = ieee80211_chan2ieee(ic, ic->ic_curchan);
1393 1.3.6.2 rmind upgt_set_channel(sc, channel);
1394 1.3.6.2 rmind
1395 1.3.6.2 rmind ni = ic->ic_bss;
1396 1.3.6.2 rmind
1397 1.3.6.2 rmind /*
1398 1.3.6.2 rmind * TX rate control is done by the firmware.
1399 1.3.6.2 rmind * Report the maximum rate which is available therefore.
1400 1.3.6.2 rmind */
1401 1.3.6.2 rmind ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
1402 1.3.6.2 rmind
1403 1.3.6.2 rmind if (ic->ic_opmode != IEEE80211_M_MONITOR)
1404 1.3.6.2 rmind upgt_set_macfilter(sc, IEEE80211_S_RUN);
1405 1.3.6.2 rmind upgt_set_led(sc, UPGT_LED_ON);
1406 1.3.6.2 rmind break;
1407 1.3.6.2 rmind }
1408 1.3.6.2 rmind
1409 1.3.6.2 rmind mutex_exit(&sc->sc_mtx);
1410 1.3.6.2 rmind
1411 1.3.6.2 rmind sc->sc_newstate(ic, sc->sc_state, sc->sc_arg);
1412 1.3.6.2 rmind }
1413 1.3.6.2 rmind
1414 1.3.6.2 rmind static void
1415 1.3.6.2 rmind upgt_next_scan(void *arg)
1416 1.3.6.2 rmind {
1417 1.3.6.2 rmind struct upgt_softc *sc = arg;
1418 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1419 1.3.6.2 rmind
1420 1.3.6.2 rmind DPRINTF(2, "%s: %s\n", device_xname(sc->sc_dev), __func__);
1421 1.3.6.2 rmind
1422 1.3.6.2 rmind if (ic->ic_state == IEEE80211_S_SCAN)
1423 1.3.6.2 rmind ieee80211_next_scan(ic);
1424 1.3.6.2 rmind }
1425 1.3.6.2 rmind
1426 1.3.6.2 rmind static void
1427 1.3.6.2 rmind upgt_start(struct ifnet *ifp)
1428 1.3.6.2 rmind {
1429 1.3.6.2 rmind struct upgt_softc *sc = ifp->if_softc;
1430 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1431 1.3.6.2 rmind struct ether_header *eh;
1432 1.3.6.2 rmind struct ieee80211_node *ni;
1433 1.3.6.2 rmind struct mbuf *m;
1434 1.3.6.2 rmind int i;
1435 1.3.6.2 rmind
1436 1.3.6.2 rmind /* don't transmit packets if interface is busy or down */
1437 1.3.6.2 rmind if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1438 1.3.6.2 rmind return;
1439 1.3.6.2 rmind
1440 1.3.6.2 rmind DPRINTF(2, "%s: %s\n", device_xname(sc->sc_dev), __func__);
1441 1.3.6.2 rmind
1442 1.3.6.2 rmind for (i = 0; i < UPGT_TX_COUNT; i++) {
1443 1.3.6.2 rmind struct upgt_data *data_tx = &sc->tx_data[i];
1444 1.3.6.2 rmind
1445 1.3.6.2 rmind if (data_tx->m != NULL)
1446 1.3.6.2 rmind continue;
1447 1.3.6.2 rmind
1448 1.3.6.2 rmind IF_POLL(&ic->ic_mgtq, m);
1449 1.3.6.2 rmind if (m != NULL) {
1450 1.3.6.2 rmind /* management frame */
1451 1.3.6.2 rmind IF_DEQUEUE(&ic->ic_mgtq, m);
1452 1.3.6.2 rmind
1453 1.3.6.2 rmind ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1454 1.3.6.2 rmind m->m_pkthdr.rcvif = NULL;
1455 1.3.6.2 rmind
1456 1.3.6.2 rmind bpf_mtap3(ic->ic_rawbpf, m);
1457 1.3.6.2 rmind
1458 1.3.6.2 rmind if ((data_tx->addr = upgt_mem_alloc(sc)) == 0) {
1459 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
1460 1.3.6.2 rmind "no free prism memory\n");
1461 1.3.6.2 rmind m_freem(m);
1462 1.3.6.2 rmind ifp->if_oerrors++;
1463 1.3.6.2 rmind break;
1464 1.3.6.2 rmind }
1465 1.3.6.2 rmind data_tx->ni = ni;
1466 1.3.6.2 rmind data_tx->m = m;
1467 1.3.6.2 rmind sc->tx_queued++;
1468 1.3.6.2 rmind } else {
1469 1.3.6.2 rmind /* data frame */
1470 1.3.6.2 rmind if (ic->ic_state != IEEE80211_S_RUN)
1471 1.3.6.2 rmind break;
1472 1.3.6.2 rmind
1473 1.3.6.2 rmind IFQ_POLL(&ifp->if_snd, m);
1474 1.3.6.2 rmind if (m == NULL)
1475 1.3.6.2 rmind break;
1476 1.3.6.2 rmind
1477 1.3.6.2 rmind IFQ_DEQUEUE(&ifp->if_snd, m);
1478 1.3.6.2 rmind if (m->m_len < sizeof(struct ether_header) &&
1479 1.3.6.2 rmind !(m = m_pullup(m, sizeof(struct ether_header))))
1480 1.3.6.2 rmind continue;
1481 1.3.6.2 rmind
1482 1.3.6.2 rmind eh = mtod(m, struct ether_header *);
1483 1.3.6.2 rmind ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1484 1.3.6.2 rmind if (ni == NULL) {
1485 1.3.6.2 rmind m_freem(m);
1486 1.3.6.2 rmind continue;
1487 1.3.6.2 rmind }
1488 1.3.6.2 rmind
1489 1.3.6.2 rmind bpf_mtap(ifp, m);
1490 1.3.6.2 rmind
1491 1.3.6.2 rmind m = ieee80211_encap(ic, m, ni);
1492 1.3.6.2 rmind if (m == NULL) {
1493 1.3.6.2 rmind ieee80211_free_node(ni);
1494 1.3.6.2 rmind continue;
1495 1.3.6.2 rmind }
1496 1.3.6.2 rmind
1497 1.3.6.2 rmind bpf_mtap3(ic->ic_rawbpf, m);
1498 1.3.6.2 rmind
1499 1.3.6.2 rmind if ((data_tx->addr = upgt_mem_alloc(sc)) == 0) {
1500 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
1501 1.3.6.2 rmind "no free prism memory\n");
1502 1.3.6.2 rmind m_freem(m);
1503 1.3.6.2 rmind ieee80211_free_node(ni);
1504 1.3.6.2 rmind ifp->if_oerrors++;
1505 1.3.6.2 rmind break;
1506 1.3.6.2 rmind }
1507 1.3.6.2 rmind data_tx->ni = ni;
1508 1.3.6.2 rmind data_tx->m = m;
1509 1.3.6.2 rmind sc->tx_queued++;
1510 1.3.6.2 rmind }
1511 1.3.6.2 rmind }
1512 1.3.6.2 rmind
1513 1.3.6.2 rmind if (sc->tx_queued > 0) {
1514 1.3.6.2 rmind DPRINTF(2, "%s: tx_queued=%d\n",
1515 1.3.6.2 rmind device_xname(sc->sc_dev), sc->tx_queued);
1516 1.3.6.2 rmind /* process the TX queue in process context */
1517 1.3.6.2 rmind ifp->if_timer = 5;
1518 1.3.6.2 rmind ifp->if_flags |= IFF_OACTIVE;
1519 1.3.6.2 rmind usb_rem_task(sc->sc_udev, &sc->sc_task_tx);
1520 1.3.6.2 rmind usb_add_task(sc->sc_udev, &sc->sc_task_tx, USB_TASKQ_DRIVER);
1521 1.3.6.2 rmind }
1522 1.3.6.2 rmind }
1523 1.3.6.2 rmind
1524 1.3.6.2 rmind static void
1525 1.3.6.2 rmind upgt_watchdog(struct ifnet *ifp)
1526 1.3.6.2 rmind {
1527 1.3.6.2 rmind struct upgt_softc *sc = ifp->if_softc;
1528 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1529 1.3.6.2 rmind
1530 1.3.6.2 rmind if (ic->ic_state == IEEE80211_S_INIT)
1531 1.3.6.2 rmind return;
1532 1.3.6.2 rmind
1533 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "watchdog timeout\n");
1534 1.3.6.2 rmind
1535 1.3.6.2 rmind /* TODO: what shall we do on TX timeout? */
1536 1.3.6.2 rmind
1537 1.3.6.2 rmind ieee80211_watchdog(ic);
1538 1.3.6.2 rmind }
1539 1.3.6.2 rmind
1540 1.3.6.2 rmind static void
1541 1.3.6.2 rmind upgt_tx_task(void *arg)
1542 1.3.6.2 rmind {
1543 1.3.6.2 rmind struct upgt_softc *sc = arg;
1544 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1545 1.3.6.2 rmind struct ieee80211_frame *wh;
1546 1.3.6.2 rmind struct ieee80211_key *k;
1547 1.3.6.2 rmind struct ifnet *ifp = &sc->sc_if;
1548 1.3.6.2 rmind struct upgt_lmac_mem *mem;
1549 1.3.6.2 rmind struct upgt_lmac_tx_desc *txdesc;
1550 1.3.6.2 rmind struct mbuf *m;
1551 1.3.6.2 rmind uint32_t addr;
1552 1.3.6.2 rmind int i, len, pad, s;
1553 1.3.6.2 rmind usbd_status error;
1554 1.3.6.2 rmind
1555 1.3.6.2 rmind mutex_enter(&sc->sc_mtx);
1556 1.3.6.2 rmind upgt_set_led(sc, UPGT_LED_BLINK);
1557 1.3.6.2 rmind mutex_exit(&sc->sc_mtx);
1558 1.3.6.2 rmind
1559 1.3.6.2 rmind s = splnet();
1560 1.3.6.2 rmind
1561 1.3.6.2 rmind for (i = 0; i < UPGT_TX_COUNT; i++) {
1562 1.3.6.2 rmind struct upgt_data *data_tx = &sc->tx_data[i];
1563 1.3.6.2 rmind
1564 1.3.6.2 rmind if (data_tx->m == NULL)
1565 1.3.6.2 rmind continue;
1566 1.3.6.2 rmind
1567 1.3.6.2 rmind m = data_tx->m;
1568 1.3.6.2 rmind addr = data_tx->addr + UPGT_MEMSIZE_FRAME_HEAD;
1569 1.3.6.2 rmind
1570 1.3.6.2 rmind /*
1571 1.3.6.2 rmind * Software crypto.
1572 1.3.6.2 rmind */
1573 1.3.6.2 rmind wh = mtod(m, struct ieee80211_frame *);
1574 1.3.6.2 rmind
1575 1.3.6.2 rmind if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1576 1.3.6.2 rmind k = ieee80211_crypto_encap(ic, data_tx->ni, m);
1577 1.3.6.2 rmind if (k == NULL) {
1578 1.3.6.2 rmind m_freem(m);
1579 1.3.6.2 rmind data_tx->m = NULL;
1580 1.3.6.2 rmind ieee80211_free_node(data_tx->ni);
1581 1.3.6.2 rmind data_tx->ni = NULL;
1582 1.3.6.2 rmind ifp->if_oerrors++;
1583 1.3.6.2 rmind break;
1584 1.3.6.2 rmind }
1585 1.3.6.2 rmind
1586 1.3.6.2 rmind /* in case packet header moved, reset pointer */
1587 1.3.6.2 rmind wh = mtod(m, struct ieee80211_frame *);
1588 1.3.6.2 rmind }
1589 1.3.6.2 rmind
1590 1.3.6.2 rmind /*
1591 1.3.6.2 rmind * Transmit the URB containing the TX data.
1592 1.3.6.2 rmind */
1593 1.3.6.2 rmind memset(data_tx->buf, 0, sizeof(*mem) + sizeof(*txdesc));
1594 1.3.6.2 rmind
1595 1.3.6.2 rmind mem = (struct upgt_lmac_mem *)data_tx->buf;
1596 1.3.6.2 rmind mem->addr = htole32(addr);
1597 1.3.6.2 rmind
1598 1.3.6.2 rmind txdesc = (struct upgt_lmac_tx_desc *)(mem + 1);
1599 1.3.6.2 rmind
1600 1.3.6.2 rmind /* XXX differ between data and mgmt frames? */
1601 1.3.6.2 rmind txdesc->header1.flags = UPGT_H1_FLAGS_TX_DATA;
1602 1.3.6.2 rmind txdesc->header1.type = UPGT_H1_TYPE_TX_DATA;
1603 1.3.6.2 rmind txdesc->header1.len = htole16(m->m_pkthdr.len);
1604 1.3.6.2 rmind
1605 1.3.6.2 rmind txdesc->header2.reqid = htole32(data_tx->addr);
1606 1.3.6.2 rmind txdesc->header2.type = htole16(UPGT_H2_TYPE_TX_ACK_YES);
1607 1.3.6.2 rmind txdesc->header2.flags = htole16(UPGT_H2_FLAGS_TX_ACK_YES);
1608 1.3.6.2 rmind
1609 1.3.6.2 rmind if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1610 1.3.6.2 rmind IEEE80211_FC0_TYPE_MGT) {
1611 1.3.6.2 rmind /* always send mgmt frames at lowest rate (DS1) */
1612 1.3.6.2 rmind memset(txdesc->rates, 0x10, sizeof(txdesc->rates));
1613 1.3.6.2 rmind } else {
1614 1.3.6.2 rmind memcpy(txdesc->rates, sc->sc_cur_rateset,
1615 1.3.6.2 rmind sizeof(txdesc->rates));
1616 1.3.6.2 rmind }
1617 1.3.6.2 rmind txdesc->type = htole32(UPGT_TX_DESC_TYPE_DATA);
1618 1.3.6.2 rmind txdesc->pad3[0] = UPGT_TX_DESC_PAD3_SIZE;
1619 1.3.6.2 rmind
1620 1.3.6.2 rmind if (sc->sc_drvbpf != NULL) {
1621 1.3.6.2 rmind struct upgt_tx_radiotap_header *tap = &sc->sc_txtap;
1622 1.3.6.2 rmind
1623 1.3.6.2 rmind tap->wt_flags = 0;
1624 1.3.6.2 rmind tap->wt_rate = 0; /* TODO: where to get from? */
1625 1.3.6.2 rmind tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1626 1.3.6.2 rmind tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1627 1.3.6.2 rmind
1628 1.3.6.2 rmind bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
1629 1.3.6.2 rmind }
1630 1.3.6.2 rmind
1631 1.3.6.2 rmind /* copy frame below our TX descriptor header */
1632 1.3.6.2 rmind m_copydata(m, 0, m->m_pkthdr.len,
1633 1.3.6.2 rmind data_tx->buf + sizeof(*mem) + sizeof(*txdesc));
1634 1.3.6.2 rmind
1635 1.3.6.2 rmind /* calculate frame size */
1636 1.3.6.2 rmind len = sizeof(*mem) + sizeof(*txdesc) + m->m_pkthdr.len;
1637 1.3.6.2 rmind
1638 1.3.6.2 rmind if (len & 3) {
1639 1.3.6.2 rmind /* we need to align the frame to a 4 byte boundary */
1640 1.3.6.2 rmind pad = 4 - (len & 3);
1641 1.3.6.2 rmind memset(data_tx->buf + len, 0, pad);
1642 1.3.6.2 rmind len += pad;
1643 1.3.6.2 rmind }
1644 1.3.6.2 rmind
1645 1.3.6.2 rmind /* calculate frame checksum */
1646 1.3.6.2 rmind mem->chksum = upgt_chksum_le((uint32_t *)txdesc,
1647 1.3.6.2 rmind len - sizeof(*mem));
1648 1.3.6.2 rmind
1649 1.3.6.2 rmind /* we do not need the mbuf anymore */
1650 1.3.6.2 rmind m_freem(m);
1651 1.3.6.2 rmind data_tx->m = NULL;
1652 1.3.6.2 rmind
1653 1.3.6.2 rmind ieee80211_free_node(data_tx->ni);
1654 1.3.6.2 rmind data_tx->ni = NULL;
1655 1.3.6.2 rmind
1656 1.3.6.2 rmind DPRINTF(2, "%s: TX start data sending\n",
1657 1.3.6.2 rmind device_xname(sc->sc_dev));
1658 1.3.6.2 rmind
1659 1.3.6.2 rmind usbd_setup_xfer(data_tx->xfer, sc->sc_tx_pipeh, data_tx,
1660 1.3.6.2 rmind data_tx->buf, len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1661 1.3.6.2 rmind UPGT_USB_TIMEOUT, NULL);
1662 1.3.6.2 rmind error = usbd_transfer(data_tx->xfer);
1663 1.3.6.2 rmind if (error != USBD_NORMAL_COMPLETION &&
1664 1.3.6.2 rmind error != USBD_IN_PROGRESS) {
1665 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
1666 1.3.6.2 rmind "could not transmit TX data URB\n");
1667 1.3.6.2 rmind ifp->if_oerrors++;
1668 1.3.6.2 rmind break;
1669 1.3.6.2 rmind }
1670 1.3.6.2 rmind
1671 1.3.6.2 rmind DPRINTF(2, "%s: TX sent (%d bytes)\n",
1672 1.3.6.2 rmind device_xname(sc->sc_dev), len);
1673 1.3.6.2 rmind }
1674 1.3.6.2 rmind
1675 1.3.6.2 rmind splx(s);
1676 1.3.6.2 rmind
1677 1.3.6.2 rmind /*
1678 1.3.6.2 rmind * If we don't regulary read the device statistics, the RX queue
1679 1.3.6.2 rmind * will stall. It's strange, but it works, so we keep reading
1680 1.3.6.2 rmind * the statistics here. *shrug*
1681 1.3.6.2 rmind */
1682 1.3.6.2 rmind mutex_enter(&sc->sc_mtx);
1683 1.3.6.2 rmind upgt_get_stats(sc);
1684 1.3.6.2 rmind mutex_exit(&sc->sc_mtx);
1685 1.3.6.2 rmind }
1686 1.3.6.2 rmind
1687 1.3.6.2 rmind static void
1688 1.3.6.2 rmind upgt_tx_done(struct upgt_softc *sc, uint8_t *data)
1689 1.3.6.2 rmind {
1690 1.3.6.2 rmind struct ifnet *ifp = &sc->sc_if;
1691 1.3.6.2 rmind struct upgt_lmac_tx_done_desc *desc;
1692 1.3.6.2 rmind int i, s;
1693 1.3.6.2 rmind
1694 1.3.6.2 rmind s = splnet();
1695 1.3.6.2 rmind
1696 1.3.6.2 rmind desc = (struct upgt_lmac_tx_done_desc *)data;
1697 1.3.6.2 rmind
1698 1.3.6.2 rmind for (i = 0; i < UPGT_TX_COUNT; i++) {
1699 1.3.6.2 rmind struct upgt_data *data_tx = &sc->tx_data[i];
1700 1.3.6.2 rmind
1701 1.3.6.2 rmind if (data_tx->addr == le32toh(desc->header2.reqid)) {
1702 1.3.6.2 rmind upgt_mem_free(sc, data_tx->addr);
1703 1.3.6.2 rmind data_tx->addr = 0;
1704 1.3.6.2 rmind
1705 1.3.6.2 rmind sc->tx_queued--;
1706 1.3.6.2 rmind ifp->if_opackets++;
1707 1.3.6.2 rmind
1708 1.3.6.2 rmind DPRINTF(2, "%s: TX done: ", device_xname(sc->sc_dev));
1709 1.3.6.2 rmind DPRINTF(2, "memaddr=0x%08x, status=0x%04x, rssi=%d, ",
1710 1.3.6.2 rmind le32toh(desc->header2.reqid),
1711 1.3.6.2 rmind le16toh(desc->status),
1712 1.3.6.2 rmind le16toh(desc->rssi));
1713 1.3.6.2 rmind DPRINTF(2, "seq=%d\n", le16toh(desc->seq));
1714 1.3.6.2 rmind break;
1715 1.3.6.2 rmind }
1716 1.3.6.2 rmind }
1717 1.3.6.2 rmind
1718 1.3.6.2 rmind if (sc->tx_queued == 0) {
1719 1.3.6.2 rmind /* TX queued was processed, continue */
1720 1.3.6.2 rmind ifp->if_timer = 0;
1721 1.3.6.2 rmind ifp->if_flags &= ~IFF_OACTIVE;
1722 1.3.6.2 rmind upgt_start(ifp);
1723 1.3.6.2 rmind }
1724 1.3.6.2 rmind
1725 1.3.6.2 rmind splx(s);
1726 1.3.6.2 rmind }
1727 1.3.6.2 rmind
1728 1.3.6.2 rmind static void
1729 1.3.6.2 rmind upgt_rx_cb(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1730 1.3.6.2 rmind {
1731 1.3.6.2 rmind struct upgt_data *data_rx = priv;
1732 1.3.6.2 rmind struct upgt_softc *sc = data_rx->sc;
1733 1.3.6.2 rmind int len;
1734 1.3.6.2 rmind struct upgt_lmac_header *header;
1735 1.3.6.2 rmind struct upgt_lmac_eeprom *eeprom;
1736 1.3.6.2 rmind uint8_t h1_type;
1737 1.3.6.2 rmind uint16_t h2_type;
1738 1.3.6.2 rmind
1739 1.3.6.2 rmind DPRINTF(3, "%s: %s\n", device_xname(sc->sc_dev), __func__);
1740 1.3.6.2 rmind
1741 1.3.6.2 rmind if (status != USBD_NORMAL_COMPLETION) {
1742 1.3.6.2 rmind if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1743 1.3.6.2 rmind return;
1744 1.3.6.2 rmind if (status == USBD_STALLED)
1745 1.3.6.2 rmind usbd_clear_endpoint_stall_async(sc->sc_rx_pipeh);
1746 1.3.6.2 rmind goto skip;
1747 1.3.6.2 rmind }
1748 1.3.6.2 rmind usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
1749 1.3.6.2 rmind
1750 1.3.6.2 rmind /*
1751 1.3.6.2 rmind * Check what type of frame came in.
1752 1.3.6.2 rmind */
1753 1.3.6.2 rmind header = (struct upgt_lmac_header *)(data_rx->buf + 4);
1754 1.3.6.2 rmind
1755 1.3.6.2 rmind h1_type = header->header1.type;
1756 1.3.6.2 rmind h2_type = le16toh(header->header2.type);
1757 1.3.6.2 rmind
1758 1.3.6.2 rmind if (h1_type == UPGT_H1_TYPE_CTRL &&
1759 1.3.6.2 rmind h2_type == UPGT_H2_TYPE_EEPROM) {
1760 1.3.6.2 rmind eeprom = (struct upgt_lmac_eeprom *)(data_rx->buf + 4);
1761 1.3.6.2 rmind uint16_t eeprom_offset = le16toh(eeprom->offset);
1762 1.3.6.2 rmind uint16_t eeprom_len = le16toh(eeprom->len);
1763 1.3.6.2 rmind
1764 1.3.6.2 rmind DPRINTF(2, "%s: received EEPROM block (offset=%d, len=%d)\n",
1765 1.3.6.2 rmind device_xname(sc->sc_dev), eeprom_offset, eeprom_len);
1766 1.3.6.2 rmind
1767 1.3.6.2 rmind memcpy(sc->sc_eeprom + eeprom_offset,
1768 1.3.6.2 rmind data_rx->buf + sizeof(struct upgt_lmac_eeprom) + 4,
1769 1.3.6.2 rmind eeprom_len);
1770 1.3.6.2 rmind
1771 1.3.6.2 rmind /* EEPROM data has arrived in time, wakeup tsleep() */
1772 1.3.6.2 rmind wakeup(sc);
1773 1.3.6.2 rmind } else
1774 1.3.6.2 rmind if (h1_type == UPGT_H1_TYPE_CTRL &&
1775 1.3.6.2 rmind h2_type == UPGT_H2_TYPE_TX_DONE) {
1776 1.3.6.2 rmind DPRINTF(2, "%s: received 802.11 TX done\n",
1777 1.3.6.2 rmind device_xname(sc->sc_dev));
1778 1.3.6.2 rmind
1779 1.3.6.2 rmind upgt_tx_done(sc, data_rx->buf + 4);
1780 1.3.6.2 rmind } else
1781 1.3.6.2 rmind if (h1_type == UPGT_H1_TYPE_RX_DATA ||
1782 1.3.6.2 rmind h1_type == UPGT_H1_TYPE_RX_DATA_MGMT) {
1783 1.3.6.2 rmind DPRINTF(3, "%s: received 802.11 RX data\n",
1784 1.3.6.2 rmind device_xname(sc->sc_dev));
1785 1.3.6.2 rmind
1786 1.3.6.2 rmind upgt_rx(sc, data_rx->buf + 4, le16toh(header->header1.len));
1787 1.3.6.2 rmind } else
1788 1.3.6.2 rmind if (h1_type == UPGT_H1_TYPE_CTRL &&
1789 1.3.6.2 rmind h2_type == UPGT_H2_TYPE_STATS) {
1790 1.3.6.2 rmind DPRINTF(2, "%s: received statistic data\n",
1791 1.3.6.2 rmind device_xname(sc->sc_dev));
1792 1.3.6.2 rmind
1793 1.3.6.2 rmind /* TODO: what could we do with the statistic data? */
1794 1.3.6.2 rmind } else {
1795 1.3.6.2 rmind /* ignore unknown frame types */
1796 1.3.6.2 rmind DPRINTF(1, "%s: received unknown frame type 0x%02x\n",
1797 1.3.6.2 rmind device_xname(sc->sc_dev), header->header1.type);
1798 1.3.6.2 rmind }
1799 1.3.6.2 rmind
1800 1.3.6.2 rmind skip: /* setup new transfer */
1801 1.3.6.2 rmind usbd_setup_xfer(xfer, sc->sc_rx_pipeh, data_rx, data_rx->buf, MCLBYTES,
1802 1.3.6.2 rmind USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, upgt_rx_cb);
1803 1.3.6.2 rmind (void)usbd_transfer(xfer);
1804 1.3.6.2 rmind }
1805 1.3.6.2 rmind
1806 1.3.6.2 rmind static void
1807 1.3.6.2 rmind upgt_rx(struct upgt_softc *sc, uint8_t *data, int pkglen)
1808 1.3.6.2 rmind {
1809 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1810 1.3.6.2 rmind struct ifnet *ifp = &sc->sc_if;
1811 1.3.6.2 rmind struct upgt_lmac_rx_desc *rxdesc;
1812 1.3.6.2 rmind struct ieee80211_frame *wh;
1813 1.3.6.2 rmind struct ieee80211_node *ni;
1814 1.3.6.2 rmind struct mbuf *m;
1815 1.3.6.2 rmind int s;
1816 1.3.6.2 rmind
1817 1.3.6.2 rmind /* access RX packet descriptor */
1818 1.3.6.2 rmind rxdesc = (struct upgt_lmac_rx_desc *)data;
1819 1.3.6.2 rmind
1820 1.3.6.2 rmind /* create mbuf which is suitable for strict alignment archs */
1821 1.3.6.2 rmind #define ETHER_ALIGN 0
1822 1.3.6.2 rmind m = m_devget(rxdesc->data, pkglen, ETHER_ALIGN, ifp, NULL);
1823 1.3.6.2 rmind if (m == NULL) {
1824 1.3.6.2 rmind DPRINTF(1, "%s: could not create RX mbuf\n",
1825 1.3.6.2 rmind device_xname(sc->sc_dev));
1826 1.3.6.2 rmind ifp->if_ierrors++;
1827 1.3.6.2 rmind return;
1828 1.3.6.2 rmind }
1829 1.3.6.2 rmind
1830 1.3.6.2 rmind s = splnet();
1831 1.3.6.2 rmind
1832 1.3.6.2 rmind if (sc->sc_drvbpf != NULL) {
1833 1.3.6.2 rmind struct upgt_rx_radiotap_header *tap = &sc->sc_rxtap;
1834 1.3.6.2 rmind
1835 1.3.6.2 rmind tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
1836 1.3.6.2 rmind tap->wr_rate = upgt_rx_rate(sc, rxdesc->rate);
1837 1.3.6.2 rmind tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1838 1.3.6.2 rmind tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1839 1.3.6.2 rmind tap->wr_antsignal = rxdesc->rssi;
1840 1.3.6.2 rmind
1841 1.3.6.2 rmind bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
1842 1.3.6.2 rmind }
1843 1.3.6.2 rmind
1844 1.3.6.2 rmind /* trim FCS */
1845 1.3.6.2 rmind m_adj(m, -IEEE80211_CRC_LEN);
1846 1.3.6.2 rmind
1847 1.3.6.2 rmind wh = mtod(m, struct ieee80211_frame *);
1848 1.3.6.2 rmind ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1849 1.3.6.2 rmind
1850 1.3.6.2 rmind /* push the frame up to the 802.11 stack */
1851 1.3.6.2 rmind ieee80211_input(ic, m, ni, rxdesc->rssi, 0);
1852 1.3.6.2 rmind
1853 1.3.6.2 rmind /* node is no longer needed */
1854 1.3.6.2 rmind ieee80211_free_node(ni);
1855 1.3.6.2 rmind
1856 1.3.6.2 rmind splx(s);
1857 1.3.6.2 rmind
1858 1.3.6.2 rmind DPRINTF(3, "%s: RX done\n", device_xname(sc->sc_dev));
1859 1.3.6.2 rmind }
1860 1.3.6.2 rmind
1861 1.3.6.2 rmind static void
1862 1.3.6.2 rmind upgt_setup_rates(struct upgt_softc *sc)
1863 1.3.6.2 rmind {
1864 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1865 1.3.6.2 rmind
1866 1.3.6.2 rmind /*
1867 1.3.6.2 rmind * 0x01 = OFMD6 0x10 = DS1
1868 1.3.6.2 rmind * 0x04 = OFDM9 0x11 = DS2
1869 1.3.6.2 rmind * 0x06 = OFDM12 0x12 = DS5
1870 1.3.6.2 rmind * 0x07 = OFDM18 0x13 = DS11
1871 1.3.6.2 rmind * 0x08 = OFDM24
1872 1.3.6.2 rmind * 0x09 = OFDM36
1873 1.3.6.2 rmind * 0x0a = OFDM48
1874 1.3.6.2 rmind * 0x0b = OFDM54
1875 1.3.6.2 rmind */
1876 1.3.6.2 rmind const uint8_t rateset_auto_11b[] =
1877 1.3.6.2 rmind { 0x13, 0x13, 0x12, 0x11, 0x11, 0x10, 0x10, 0x10 };
1878 1.3.6.2 rmind const uint8_t rateset_auto_11g[] =
1879 1.3.6.2 rmind { 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x04, 0x01 };
1880 1.3.6.2 rmind const uint8_t rateset_fix_11bg[] =
1881 1.3.6.2 rmind { 0x10, 0x11, 0x12, 0x13, 0x01, 0x04, 0x06, 0x07,
1882 1.3.6.2 rmind 0x08, 0x09, 0x0a, 0x0b };
1883 1.3.6.2 rmind
1884 1.3.6.2 rmind if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
1885 1.3.6.2 rmind /*
1886 1.3.6.2 rmind * Automatic rate control is done by the device.
1887 1.3.6.2 rmind * We just pass the rateset from which the device
1888 1.3.6.2 rmind * will pickup a rate.
1889 1.3.6.2 rmind */
1890 1.3.6.2 rmind if (ic->ic_curmode == IEEE80211_MODE_11B)
1891 1.3.6.2 rmind memcpy(sc->sc_cur_rateset, rateset_auto_11b,
1892 1.3.6.2 rmind sizeof(sc->sc_cur_rateset));
1893 1.3.6.2 rmind if (ic->ic_curmode == IEEE80211_MODE_11G ||
1894 1.3.6.2 rmind ic->ic_curmode == IEEE80211_MODE_AUTO)
1895 1.3.6.2 rmind memcpy(sc->sc_cur_rateset, rateset_auto_11g,
1896 1.3.6.2 rmind sizeof(sc->sc_cur_rateset));
1897 1.3.6.2 rmind } else {
1898 1.3.6.2 rmind /* set a fixed rate */
1899 1.3.6.2 rmind memset(sc->sc_cur_rateset, rateset_fix_11bg[ic->ic_fixed_rate],
1900 1.3.6.2 rmind sizeof(sc->sc_cur_rateset));
1901 1.3.6.2 rmind }
1902 1.3.6.2 rmind }
1903 1.3.6.2 rmind
1904 1.3.6.2 rmind static uint8_t
1905 1.3.6.2 rmind upgt_rx_rate(struct upgt_softc *sc, const int rate)
1906 1.3.6.2 rmind {
1907 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1908 1.3.6.2 rmind
1909 1.3.6.2 rmind if (ic->ic_curmode == IEEE80211_MODE_11B) {
1910 1.3.6.2 rmind if (rate < 0 || rate > 3)
1911 1.3.6.2 rmind /* invalid rate */
1912 1.3.6.2 rmind return 0;
1913 1.3.6.2 rmind
1914 1.3.6.2 rmind switch (rate) {
1915 1.3.6.2 rmind case 0:
1916 1.3.6.2 rmind return 2;
1917 1.3.6.2 rmind case 1:
1918 1.3.6.2 rmind return 4;
1919 1.3.6.2 rmind case 2:
1920 1.3.6.2 rmind return 11;
1921 1.3.6.2 rmind case 3:
1922 1.3.6.2 rmind return 22;
1923 1.3.6.2 rmind default:
1924 1.3.6.2 rmind return 0;
1925 1.3.6.2 rmind }
1926 1.3.6.2 rmind }
1927 1.3.6.2 rmind
1928 1.3.6.2 rmind if (ic->ic_curmode == IEEE80211_MODE_11G) {
1929 1.3.6.2 rmind if (rate < 0 || rate > 11)
1930 1.3.6.2 rmind /* invalid rate */
1931 1.3.6.2 rmind return 0;
1932 1.3.6.2 rmind
1933 1.3.6.2 rmind switch (rate) {
1934 1.3.6.2 rmind case 0:
1935 1.3.6.2 rmind return 2;
1936 1.3.6.2 rmind case 1:
1937 1.3.6.2 rmind return 4;
1938 1.3.6.2 rmind case 2:
1939 1.3.6.2 rmind return 11;
1940 1.3.6.2 rmind case 3:
1941 1.3.6.2 rmind return 22;
1942 1.3.6.2 rmind case 4:
1943 1.3.6.2 rmind return 12;
1944 1.3.6.2 rmind case 5:
1945 1.3.6.2 rmind return 18;
1946 1.3.6.2 rmind case 6:
1947 1.3.6.2 rmind return 24;
1948 1.3.6.2 rmind case 7:
1949 1.3.6.2 rmind return 36;
1950 1.3.6.2 rmind case 8:
1951 1.3.6.2 rmind return 48;
1952 1.3.6.2 rmind case 9:
1953 1.3.6.2 rmind return 72;
1954 1.3.6.2 rmind case 10:
1955 1.3.6.2 rmind return 96;
1956 1.3.6.2 rmind case 11:
1957 1.3.6.2 rmind return 108;
1958 1.3.6.2 rmind default:
1959 1.3.6.2 rmind return 0;
1960 1.3.6.2 rmind }
1961 1.3.6.2 rmind }
1962 1.3.6.2 rmind
1963 1.3.6.2 rmind return 0;
1964 1.3.6.2 rmind }
1965 1.3.6.2 rmind
1966 1.3.6.2 rmind static int
1967 1.3.6.2 rmind upgt_set_macfilter(struct upgt_softc *sc, uint8_t state)
1968 1.3.6.2 rmind {
1969 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
1970 1.3.6.2 rmind struct ieee80211_node *ni = ic->ic_bss;
1971 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
1972 1.3.6.2 rmind struct upgt_lmac_mem *mem;
1973 1.3.6.2 rmind struct upgt_lmac_filter *filter;
1974 1.3.6.2 rmind int len;
1975 1.3.6.2 rmind const uint8_t broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1976 1.3.6.2 rmind
1977 1.3.6.2 rmind /*
1978 1.3.6.2 rmind * Transmit the URB containing the CMD data.
1979 1.3.6.2 rmind */
1980 1.3.6.2 rmind len = sizeof(*mem) + sizeof(*filter);
1981 1.3.6.2 rmind
1982 1.3.6.2 rmind memset(data_cmd->buf, 0, len);
1983 1.3.6.2 rmind
1984 1.3.6.2 rmind mem = (struct upgt_lmac_mem *)data_cmd->buf;
1985 1.3.6.2 rmind mem->addr = htole32(sc->sc_memaddr_frame_start +
1986 1.3.6.2 rmind UPGT_MEMSIZE_FRAME_HEAD);
1987 1.3.6.2 rmind
1988 1.3.6.2 rmind filter = (struct upgt_lmac_filter *)(mem + 1);
1989 1.3.6.2 rmind
1990 1.3.6.2 rmind filter->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
1991 1.3.6.2 rmind filter->header1.type = UPGT_H1_TYPE_CTRL;
1992 1.3.6.2 rmind filter->header1.len = htole16(
1993 1.3.6.2 rmind sizeof(struct upgt_lmac_filter) -
1994 1.3.6.2 rmind sizeof(struct upgt_lmac_header));
1995 1.3.6.2 rmind
1996 1.3.6.2 rmind filter->header2.reqid = htole32(sc->sc_memaddr_frame_start);
1997 1.3.6.2 rmind filter->header2.type = htole16(UPGT_H2_TYPE_MACFILTER);
1998 1.3.6.2 rmind filter->header2.flags = 0;
1999 1.3.6.2 rmind
2000 1.3.6.2 rmind switch (state) {
2001 1.3.6.2 rmind case IEEE80211_S_INIT:
2002 1.3.6.2 rmind DPRINTF(1, "%s: set MAC filter to INIT\n",
2003 1.3.6.2 rmind device_xname(sc->sc_dev));
2004 1.3.6.2 rmind
2005 1.3.6.2 rmind filter->type = htole16(UPGT_FILTER_TYPE_RESET);
2006 1.3.6.2 rmind break;
2007 1.3.6.2 rmind case IEEE80211_S_SCAN:
2008 1.3.6.2 rmind DPRINTF(1, "%s: set MAC filter to SCAN (bssid %s)\n",
2009 1.3.6.2 rmind device_xname(sc->sc_dev), ether_sprintf(broadcast));
2010 1.3.6.2 rmind
2011 1.3.6.2 rmind filter->type = htole16(UPGT_FILTER_TYPE_NONE);
2012 1.3.6.2 rmind IEEE80211_ADDR_COPY(filter->dst, ic->ic_myaddr);
2013 1.3.6.2 rmind IEEE80211_ADDR_COPY(filter->src, broadcast);
2014 1.3.6.2 rmind filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
2015 1.3.6.2 rmind filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
2016 1.3.6.2 rmind filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
2017 1.3.6.2 rmind filter->rxhw = htole32(sc->sc_eeprom_hwrx);
2018 1.3.6.2 rmind filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
2019 1.3.6.2 rmind break;
2020 1.3.6.2 rmind case IEEE80211_S_RUN:
2021 1.3.6.2 rmind DPRINTF(1, "%s: set MAC filter to RUN (bssid %s)\n",
2022 1.3.6.2 rmind device_xname(sc->sc_dev), ether_sprintf(ni->ni_bssid));
2023 1.3.6.2 rmind
2024 1.3.6.2 rmind filter->type = htole16(UPGT_FILTER_TYPE_STA);
2025 1.3.6.2 rmind IEEE80211_ADDR_COPY(filter->dst, ic->ic_myaddr);
2026 1.3.6.2 rmind IEEE80211_ADDR_COPY(filter->src, ni->ni_bssid);
2027 1.3.6.2 rmind filter->unknown1 = htole16(UPGT_FILTER_UNKNOWN1);
2028 1.3.6.2 rmind filter->rxaddr = htole32(sc->sc_memaddr_rx_start);
2029 1.3.6.2 rmind filter->unknown2 = htole16(UPGT_FILTER_UNKNOWN2);
2030 1.3.6.2 rmind filter->rxhw = htole32(sc->sc_eeprom_hwrx);
2031 1.3.6.2 rmind filter->unknown3 = htole16(UPGT_FILTER_UNKNOWN3);
2032 1.3.6.2 rmind break;
2033 1.3.6.2 rmind default:
2034 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2035 1.3.6.2 rmind "MAC filter does not know that state\n");
2036 1.3.6.2 rmind break;
2037 1.3.6.2 rmind }
2038 1.3.6.2 rmind
2039 1.3.6.2 rmind mem->chksum = upgt_chksum_le((uint32_t *)filter, sizeof(*filter));
2040 1.3.6.2 rmind
2041 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
2042 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2043 1.3.6.2 rmind "could not transmit macfilter CMD data URB\n");
2044 1.3.6.2 rmind return EIO;
2045 1.3.6.2 rmind }
2046 1.3.6.2 rmind
2047 1.3.6.2 rmind return 0;
2048 1.3.6.2 rmind }
2049 1.3.6.2 rmind
2050 1.3.6.2 rmind static int
2051 1.3.6.2 rmind upgt_set_channel(struct upgt_softc *sc, unsigned channel)
2052 1.3.6.2 rmind {
2053 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
2054 1.3.6.2 rmind struct upgt_lmac_mem *mem;
2055 1.3.6.2 rmind struct upgt_lmac_channel *chan;
2056 1.3.6.2 rmind int len;
2057 1.3.6.2 rmind
2058 1.3.6.2 rmind DPRINTF(1, "%s: %s: %d\n", device_xname(sc->sc_dev), __func__,
2059 1.3.6.2 rmind channel);
2060 1.3.6.2 rmind
2061 1.3.6.2 rmind /*
2062 1.3.6.2 rmind * Transmit the URB containing the CMD data.
2063 1.3.6.2 rmind */
2064 1.3.6.2 rmind len = sizeof(*mem) + sizeof(*chan);
2065 1.3.6.2 rmind
2066 1.3.6.2 rmind memset(data_cmd->buf, 0, len);
2067 1.3.6.2 rmind
2068 1.3.6.2 rmind mem = (struct upgt_lmac_mem *)data_cmd->buf;
2069 1.3.6.2 rmind mem->addr = htole32(sc->sc_memaddr_frame_start +
2070 1.3.6.2 rmind UPGT_MEMSIZE_FRAME_HEAD);
2071 1.3.6.2 rmind
2072 1.3.6.2 rmind chan = (struct upgt_lmac_channel *)(mem + 1);
2073 1.3.6.2 rmind
2074 1.3.6.2 rmind chan->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
2075 1.3.6.2 rmind chan->header1.type = UPGT_H1_TYPE_CTRL;
2076 1.3.6.2 rmind chan->header1.len = htole16(
2077 1.3.6.2 rmind sizeof(struct upgt_lmac_channel) -
2078 1.3.6.2 rmind sizeof(struct upgt_lmac_header));
2079 1.3.6.2 rmind
2080 1.3.6.2 rmind chan->header2.reqid = htole32(sc->sc_memaddr_frame_start);
2081 1.3.6.2 rmind chan->header2.type = htole16(UPGT_H2_TYPE_CHANNEL);
2082 1.3.6.2 rmind chan->header2.flags = 0;
2083 1.3.6.2 rmind
2084 1.3.6.2 rmind chan->unknown1 = htole16(UPGT_CHANNEL_UNKNOWN1);
2085 1.3.6.2 rmind chan->unknown2 = htole16(UPGT_CHANNEL_UNKNOWN2);
2086 1.3.6.2 rmind chan->freq6 = sc->sc_eeprom_freq6[channel];
2087 1.3.6.2 rmind chan->settings = sc->sc_eeprom_freq6_settings;
2088 1.3.6.2 rmind chan->unknown3 = UPGT_CHANNEL_UNKNOWN3;
2089 1.3.6.2 rmind
2090 1.3.6.2 rmind memcpy(chan->freq3_1, &sc->sc_eeprom_freq3[channel].data,
2091 1.3.6.2 rmind sizeof(chan->freq3_1));
2092 1.3.6.2 rmind
2093 1.3.6.2 rmind memcpy(chan->freq4, &sc->sc_eeprom_freq4[channel],
2094 1.3.6.2 rmind sizeof(sc->sc_eeprom_freq4[channel]));
2095 1.3.6.2 rmind
2096 1.3.6.2 rmind memcpy(chan->freq3_2, &sc->sc_eeprom_freq3[channel].data,
2097 1.3.6.2 rmind sizeof(chan->freq3_2));
2098 1.3.6.2 rmind
2099 1.3.6.2 rmind mem->chksum = upgt_chksum_le((uint32_t *)chan, sizeof(*chan));
2100 1.3.6.2 rmind
2101 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
2102 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2103 1.3.6.2 rmind "could not transmit channel CMD data URB\n");
2104 1.3.6.2 rmind return EIO;
2105 1.3.6.2 rmind }
2106 1.3.6.2 rmind
2107 1.3.6.2 rmind return 0;
2108 1.3.6.2 rmind }
2109 1.3.6.2 rmind
2110 1.3.6.2 rmind static void
2111 1.3.6.2 rmind upgt_set_led(struct upgt_softc *sc, int action)
2112 1.3.6.2 rmind {
2113 1.3.6.2 rmind struct ieee80211com *ic = &sc->sc_ic;
2114 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
2115 1.3.6.2 rmind struct upgt_lmac_mem *mem;
2116 1.3.6.2 rmind struct upgt_lmac_led *led;
2117 1.3.6.2 rmind struct timeval t;
2118 1.3.6.2 rmind int len;
2119 1.3.6.2 rmind
2120 1.3.6.2 rmind /*
2121 1.3.6.2 rmind * Transmit the URB containing the CMD data.
2122 1.3.6.2 rmind */
2123 1.3.6.2 rmind len = sizeof(*mem) + sizeof(*led);
2124 1.3.6.2 rmind
2125 1.3.6.2 rmind memset(data_cmd->buf, 0, len);
2126 1.3.6.2 rmind
2127 1.3.6.2 rmind mem = (struct upgt_lmac_mem *)data_cmd->buf;
2128 1.3.6.2 rmind mem->addr = htole32(sc->sc_memaddr_frame_start +
2129 1.3.6.2 rmind UPGT_MEMSIZE_FRAME_HEAD);
2130 1.3.6.2 rmind
2131 1.3.6.2 rmind led = (struct upgt_lmac_led *)(mem + 1);
2132 1.3.6.2 rmind
2133 1.3.6.2 rmind led->header1.flags = UPGT_H1_FLAGS_TX_NO_CALLBACK;
2134 1.3.6.2 rmind led->header1.type = UPGT_H1_TYPE_CTRL;
2135 1.3.6.2 rmind led->header1.len = htole16(
2136 1.3.6.2 rmind sizeof(struct upgt_lmac_led) -
2137 1.3.6.2 rmind sizeof(struct upgt_lmac_header));
2138 1.3.6.2 rmind
2139 1.3.6.2 rmind led->header2.reqid = htole32(sc->sc_memaddr_frame_start);
2140 1.3.6.2 rmind led->header2.type = htole16(UPGT_H2_TYPE_LED);
2141 1.3.6.2 rmind led->header2.flags = 0;
2142 1.3.6.2 rmind
2143 1.3.6.2 rmind switch (action) {
2144 1.3.6.2 rmind case UPGT_LED_OFF:
2145 1.3.6.2 rmind led->mode = htole16(UPGT_LED_MODE_SET);
2146 1.3.6.2 rmind led->action_fix = 0;
2147 1.3.6.2 rmind led->action_tmp = htole16(UPGT_LED_ACTION_OFF);
2148 1.3.6.2 rmind led->action_tmp_dur = 0;
2149 1.3.6.2 rmind break;
2150 1.3.6.2 rmind case UPGT_LED_ON:
2151 1.3.6.2 rmind led->mode = htole16(UPGT_LED_MODE_SET);
2152 1.3.6.2 rmind led->action_fix = 0;
2153 1.3.6.2 rmind led->action_tmp = htole16(UPGT_LED_ACTION_ON);
2154 1.3.6.2 rmind led->action_tmp_dur = 0;
2155 1.3.6.2 rmind break;
2156 1.3.6.2 rmind case UPGT_LED_BLINK:
2157 1.3.6.2 rmind if (ic->ic_state != IEEE80211_S_RUN)
2158 1.3.6.2 rmind return;
2159 1.3.6.2 rmind if (sc->sc_led_blink)
2160 1.3.6.2 rmind /* previous blink was not finished */
2161 1.3.6.2 rmind return;
2162 1.3.6.2 rmind led->mode = htole16(UPGT_LED_MODE_SET);
2163 1.3.6.2 rmind led->action_fix = htole16(UPGT_LED_ACTION_OFF);
2164 1.3.6.2 rmind led->action_tmp = htole16(UPGT_LED_ACTION_ON);
2165 1.3.6.2 rmind led->action_tmp_dur = htole16(UPGT_LED_ACTION_TMP_DUR);
2166 1.3.6.2 rmind /* lock blink */
2167 1.3.6.2 rmind sc->sc_led_blink = 1;
2168 1.3.6.2 rmind t.tv_sec = 0;
2169 1.3.6.2 rmind t.tv_usec = UPGT_LED_ACTION_TMP_DUR * 1000L;
2170 1.3.6.2 rmind callout_schedule(&sc->led_to, tvtohz(&t));
2171 1.3.6.2 rmind break;
2172 1.3.6.2 rmind default:
2173 1.3.6.2 rmind return;
2174 1.3.6.2 rmind }
2175 1.3.6.2 rmind
2176 1.3.6.2 rmind mem->chksum = upgt_chksum_le((uint32_t *)led, sizeof(*led));
2177 1.3.6.2 rmind
2178 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
2179 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2180 1.3.6.2 rmind "could not transmit led CMD URB\n");
2181 1.3.6.2 rmind }
2182 1.3.6.2 rmind }
2183 1.3.6.2 rmind
2184 1.3.6.2 rmind static void
2185 1.3.6.2 rmind upgt_set_led_blink(void *arg)
2186 1.3.6.2 rmind {
2187 1.3.6.2 rmind struct upgt_softc *sc = arg;
2188 1.3.6.2 rmind
2189 1.3.6.2 rmind /* blink finished, we are ready for a next one */
2190 1.3.6.2 rmind sc->sc_led_blink = 0;
2191 1.3.6.2 rmind callout_stop(&sc->led_to);
2192 1.3.6.2 rmind }
2193 1.3.6.2 rmind
2194 1.3.6.2 rmind static int
2195 1.3.6.2 rmind upgt_get_stats(struct upgt_softc *sc)
2196 1.3.6.2 rmind {
2197 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
2198 1.3.6.2 rmind struct upgt_lmac_mem *mem;
2199 1.3.6.2 rmind struct upgt_lmac_stats *stats;
2200 1.3.6.2 rmind int len;
2201 1.3.6.2 rmind
2202 1.3.6.2 rmind /*
2203 1.3.6.2 rmind * Transmit the URB containing the CMD data.
2204 1.3.6.2 rmind */
2205 1.3.6.2 rmind len = sizeof(*mem) + sizeof(*stats);
2206 1.3.6.2 rmind
2207 1.3.6.2 rmind memset(data_cmd->buf, 0, len);
2208 1.3.6.2 rmind
2209 1.3.6.2 rmind mem = (struct upgt_lmac_mem *)data_cmd->buf;
2210 1.3.6.2 rmind mem->addr = htole32(sc->sc_memaddr_frame_start +
2211 1.3.6.2 rmind UPGT_MEMSIZE_FRAME_HEAD);
2212 1.3.6.2 rmind
2213 1.3.6.2 rmind stats = (struct upgt_lmac_stats *)(mem + 1);
2214 1.3.6.2 rmind
2215 1.3.6.2 rmind stats->header1.flags = 0;
2216 1.3.6.2 rmind stats->header1.type = UPGT_H1_TYPE_CTRL;
2217 1.3.6.2 rmind stats->header1.len = htole16(
2218 1.3.6.2 rmind sizeof(struct upgt_lmac_stats) -
2219 1.3.6.2 rmind sizeof(struct upgt_lmac_header));
2220 1.3.6.2 rmind
2221 1.3.6.2 rmind stats->header2.reqid = htole32(sc->sc_memaddr_frame_start);
2222 1.3.6.2 rmind stats->header2.type = htole16(UPGT_H2_TYPE_STATS);
2223 1.3.6.2 rmind stats->header2.flags = 0;
2224 1.3.6.2 rmind
2225 1.3.6.2 rmind mem->chksum = upgt_chksum_le((uint32_t *)stats, sizeof(*stats));
2226 1.3.6.2 rmind
2227 1.3.6.2 rmind if (upgt_bulk_xmit(sc, data_cmd, sc->sc_tx_pipeh, &len, 0) != 0) {
2228 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2229 1.3.6.2 rmind "could not transmit statistics CMD data URB\n");
2230 1.3.6.2 rmind return EIO;
2231 1.3.6.2 rmind }
2232 1.3.6.2 rmind
2233 1.3.6.2 rmind return 0;
2234 1.3.6.2 rmind
2235 1.3.6.2 rmind }
2236 1.3.6.2 rmind
2237 1.3.6.2 rmind static int
2238 1.3.6.2 rmind upgt_alloc_tx(struct upgt_softc *sc)
2239 1.3.6.2 rmind {
2240 1.3.6.2 rmind int i;
2241 1.3.6.2 rmind
2242 1.3.6.2 rmind sc->tx_queued = 0;
2243 1.3.6.2 rmind
2244 1.3.6.2 rmind for (i = 0; i < UPGT_TX_COUNT; i++) {
2245 1.3.6.2 rmind struct upgt_data *data_tx = &sc->tx_data[i];
2246 1.3.6.2 rmind
2247 1.3.6.2 rmind data_tx->sc = sc;
2248 1.3.6.2 rmind
2249 1.3.6.2 rmind data_tx->xfer = usbd_alloc_xfer(sc->sc_udev);
2250 1.3.6.2 rmind if (data_tx->xfer == NULL) {
2251 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2252 1.3.6.2 rmind "could not allocate TX xfer\n");
2253 1.3.6.2 rmind return ENOMEM;
2254 1.3.6.2 rmind }
2255 1.3.6.2 rmind
2256 1.3.6.2 rmind data_tx->buf = usbd_alloc_buffer(data_tx->xfer, MCLBYTES);
2257 1.3.6.2 rmind if (data_tx->buf == NULL) {
2258 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2259 1.3.6.2 rmind "could not allocate TX buffer\n");
2260 1.3.6.2 rmind return ENOMEM;
2261 1.3.6.2 rmind }
2262 1.3.6.2 rmind }
2263 1.3.6.2 rmind
2264 1.3.6.2 rmind return 0;
2265 1.3.6.2 rmind }
2266 1.3.6.2 rmind
2267 1.3.6.2 rmind static int
2268 1.3.6.2 rmind upgt_alloc_rx(struct upgt_softc *sc)
2269 1.3.6.2 rmind {
2270 1.3.6.2 rmind struct upgt_data *data_rx = &sc->rx_data;
2271 1.3.6.2 rmind
2272 1.3.6.2 rmind data_rx->sc = sc;
2273 1.3.6.2 rmind
2274 1.3.6.2 rmind data_rx->xfer = usbd_alloc_xfer(sc->sc_udev);
2275 1.3.6.2 rmind if (data_rx->xfer == NULL) {
2276 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "could not allocate RX xfer\n");
2277 1.3.6.2 rmind return ENOMEM;
2278 1.3.6.2 rmind }
2279 1.3.6.2 rmind
2280 1.3.6.2 rmind data_rx->buf = usbd_alloc_buffer(data_rx->xfer, MCLBYTES);
2281 1.3.6.2 rmind if (data_rx->buf == NULL) {
2282 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2283 1.3.6.2 rmind "could not allocate RX buffer\n");
2284 1.3.6.2 rmind return ENOMEM;
2285 1.3.6.2 rmind }
2286 1.3.6.2 rmind
2287 1.3.6.2 rmind return 0;
2288 1.3.6.2 rmind }
2289 1.3.6.2 rmind
2290 1.3.6.2 rmind static int
2291 1.3.6.2 rmind upgt_alloc_cmd(struct upgt_softc *sc)
2292 1.3.6.2 rmind {
2293 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
2294 1.3.6.2 rmind
2295 1.3.6.2 rmind data_cmd->sc = sc;
2296 1.3.6.2 rmind
2297 1.3.6.2 rmind data_cmd->xfer = usbd_alloc_xfer(sc->sc_udev);
2298 1.3.6.2 rmind if (data_cmd->xfer == NULL) {
2299 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "could not allocate RX xfer\n");
2300 1.3.6.2 rmind return ENOMEM;
2301 1.3.6.2 rmind }
2302 1.3.6.2 rmind
2303 1.3.6.2 rmind data_cmd->buf = usbd_alloc_buffer(data_cmd->xfer, MCLBYTES);
2304 1.3.6.2 rmind if (data_cmd->buf == NULL) {
2305 1.3.6.2 rmind aprint_error_dev(sc->sc_dev,
2306 1.3.6.2 rmind "could not allocate RX buffer\n");
2307 1.3.6.2 rmind return ENOMEM;
2308 1.3.6.2 rmind }
2309 1.3.6.2 rmind
2310 1.3.6.2 rmind mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_SOFTNET);
2311 1.3.6.2 rmind
2312 1.3.6.2 rmind return 0;
2313 1.3.6.2 rmind }
2314 1.3.6.2 rmind
2315 1.3.6.2 rmind static void
2316 1.3.6.2 rmind upgt_free_tx(struct upgt_softc *sc)
2317 1.3.6.2 rmind {
2318 1.3.6.2 rmind int i;
2319 1.3.6.2 rmind
2320 1.3.6.2 rmind for (i = 0; i < UPGT_TX_COUNT; i++) {
2321 1.3.6.2 rmind struct upgt_data *data_tx = &sc->tx_data[i];
2322 1.3.6.2 rmind
2323 1.3.6.2 rmind if (data_tx->xfer != NULL) {
2324 1.3.6.2 rmind usbd_free_xfer(data_tx->xfer);
2325 1.3.6.2 rmind data_tx->xfer = NULL;
2326 1.3.6.2 rmind }
2327 1.3.6.2 rmind
2328 1.3.6.2 rmind data_tx->ni = NULL;
2329 1.3.6.2 rmind }
2330 1.3.6.2 rmind }
2331 1.3.6.2 rmind
2332 1.3.6.2 rmind static void
2333 1.3.6.2 rmind upgt_free_rx(struct upgt_softc *sc)
2334 1.3.6.2 rmind {
2335 1.3.6.2 rmind struct upgt_data *data_rx = &sc->rx_data;
2336 1.3.6.2 rmind
2337 1.3.6.2 rmind if (data_rx->xfer != NULL) {
2338 1.3.6.2 rmind usbd_free_xfer(data_rx->xfer);
2339 1.3.6.2 rmind data_rx->xfer = NULL;
2340 1.3.6.2 rmind }
2341 1.3.6.2 rmind
2342 1.3.6.2 rmind data_rx->ni = NULL;
2343 1.3.6.2 rmind }
2344 1.3.6.2 rmind
2345 1.3.6.2 rmind static void
2346 1.3.6.2 rmind upgt_free_cmd(struct upgt_softc *sc)
2347 1.3.6.2 rmind {
2348 1.3.6.2 rmind struct upgt_data *data_cmd = &sc->cmd_data;
2349 1.3.6.2 rmind
2350 1.3.6.2 rmind if (data_cmd->xfer != NULL) {
2351 1.3.6.2 rmind usbd_free_xfer(data_cmd->xfer);
2352 1.3.6.2 rmind data_cmd->xfer = NULL;
2353 1.3.6.2 rmind }
2354 1.3.6.2 rmind
2355 1.3.6.2 rmind mutex_destroy(&sc->sc_mtx);
2356 1.3.6.2 rmind }
2357 1.3.6.2 rmind
2358 1.3.6.2 rmind static int
2359 1.3.6.2 rmind upgt_bulk_xmit(struct upgt_softc *sc, struct upgt_data *data,
2360 1.3.6.2 rmind usbd_pipe_handle pipeh, uint32_t *size, int flags)
2361 1.3.6.2 rmind {
2362 1.3.6.2 rmind usbd_status status;
2363 1.3.6.2 rmind
2364 1.3.6.2 rmind status = usbd_bulk_transfer(data->xfer, pipeh,
2365 1.3.6.2 rmind USBD_NO_COPY | flags, UPGT_USB_TIMEOUT, data->buf, size,
2366 1.3.6.2 rmind "upgt_bulk_xmit");
2367 1.3.6.2 rmind if (status != USBD_NORMAL_COMPLETION) {
2368 1.3.6.2 rmind aprint_error_dev(sc->sc_dev, "%s: error %s\n", __func__,
2369 1.3.6.2 rmind usbd_errstr(status));
2370 1.3.6.2 rmind return EIO;
2371 1.3.6.2 rmind }
2372 1.3.6.2 rmind
2373 1.3.6.2 rmind return 0;
2374 1.3.6.2 rmind }
2375 1.3.6.2 rmind
2376 1.3.6.2 rmind #if 0
2377 1.3.6.2 rmind static void
2378 1.3.6.2 rmind upgt_hexdump(void *buf, int len)
2379 1.3.6.2 rmind {
2380 1.3.6.2 rmind int i;
2381 1.3.6.2 rmind
2382 1.3.6.2 rmind for (i = 0; i < len; i++) {
2383 1.3.6.2 rmind if (i % 16 == 0)
2384 1.3.6.2 rmind printf("%s%5i:", i ? "\n" : "", i);
2385 1.3.6.2 rmind if (i % 4 == 0)
2386 1.3.6.2 rmind printf(" ");
2387 1.3.6.2 rmind printf("%02x", (int)*((uint8_t *)buf + i));
2388 1.3.6.2 rmind }
2389 1.3.6.2 rmind printf("\n");
2390 1.3.6.2 rmind }
2391 1.3.6.2 rmind #endif
2392 1.3.6.2 rmind
2393 1.3.6.2 rmind static uint32_t
2394 1.3.6.2 rmind upgt_crc32_le(const void *buf, size_t size)
2395 1.3.6.2 rmind {
2396 1.3.6.2 rmind uint32_t crc;
2397 1.3.6.2 rmind
2398 1.3.6.2 rmind crc = ether_crc32_le(buf, size);
2399 1.3.6.2 rmind
2400 1.3.6.2 rmind /* apply final XOR value as common for CRC-32 */
2401 1.3.6.2 rmind crc = htole32(crc ^ 0xffffffffU);
2402 1.3.6.2 rmind
2403 1.3.6.2 rmind return crc;
2404 1.3.6.2 rmind }
2405 1.3.6.2 rmind
2406 1.3.6.2 rmind /*
2407 1.3.6.2 rmind * The firmware awaits a checksum for each frame we send to it.
2408 1.3.6.2 rmind * The algorithm used therefor is uncommon but somehow similar to CRC32.
2409 1.3.6.2 rmind */
2410 1.3.6.2 rmind static uint32_t
2411 1.3.6.2 rmind upgt_chksum_le(const uint32_t *buf, size_t size)
2412 1.3.6.2 rmind {
2413 1.3.6.2 rmind int i;
2414 1.3.6.2 rmind uint32_t crc = 0;
2415 1.3.6.2 rmind
2416 1.3.6.2 rmind for (i = 0; i < size; i += sizeof(uint32_t)) {
2417 1.3.6.2 rmind crc = htole32(crc ^ *buf++);
2418 1.3.6.2 rmind crc = htole32((crc >> 5) ^ (crc << 3));
2419 1.3.6.2 rmind }
2420 1.3.6.2 rmind
2421 1.3.6.2 rmind return crc;
2422 1.3.6.2 rmind }
2423