1/* $NetBSD: aubtfwl.c,v 1.6 2016/04/23 10:15:31 skrll Exp $ */
2
3/*
4 * Copyright (c) 2011 Jonathan A. Kollasch
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: aubtfwl.c,v 1.6 2016/04/23 10:15:31 skrll Exp $");
31
32#include <sys/param.h>
33#include <dev/usb/usb.h>
34#include <dev/usb/usbdevs.h>
35#include <dev/usb/usbdi.h>
36#include <dev/usb/usbdivar.h>
37#include <dev/usb/usbdi_util.h>
38#include <dev/firmload.h>
39
40#include <dev/usb/aubtfwlreg.h>
41
42#define AR3K_FIRMWARE_CHUNK_SIZE 4096
43
44static int aubtfwl_match(device_t, cfdata_t, void *);
45static void aubtfwl_attach(device_t, device_t, void *);
46static int aubtfwl_detach(device_t, int);
47static void aubtfwl_attach_hook(device_t);
48
49struct aubtfwl_softc {
50 struct usbd_device *sc_udev;
51 int sc_flags;
52#define AUBT_IS_AR3012 1
53};
54
55CFATTACH_DECL_NEW(aubtfwl, sizeof(struct aubtfwl_softc), aubtfwl_match, aubtfwl_attach, aubtfwl_detach, NULL);
56
57static const struct usb_devno ar3k_devs[] = {
58 { USB_VENDOR_ATHEROS2, USB_PRODUCT_ATHEROS2_AR3011 },
59};
60
61static const struct usb_devno ar3k12_devs[] = {
62 { USB_VENDOR_FOXCONN, USB_PRODUCT_FOXCONN_AR3012 },
63};
64
65static int
66aubtfwl_match(device_t parent, cfdata_t match, void *aux)
67{
68 const struct usb_attach_arg * const uaa = aux;
69
70 if (usb_lookup(ar3k_devs, uaa->uaa_vendor, uaa->uaa_product))
71 return UMATCH_VENDOR_PRODUCT;
72
73 if (usb_lookup(ar3k12_devs, uaa->uaa_vendor, uaa->uaa_product)) {
74 return (UGETW(uaa->uaa_device->ud_ddesc.bcdDevice) > 1)?
75 UMATCH_NONE : UMATCH_VENDOR_PRODUCT;
76 }
77
78 return UMATCH_NONE;
79}
80
81static void
82aubtfwl_attach(device_t parent, device_t self, void *aux)
83{
84 const struct usb_attach_arg * const uaa = aux;
85 struct aubtfwl_softc * const sc = device_private(self);
86 aprint_naive("\n");
87 aprint_normal("\n");
88 sc->sc_udev = uaa->uaa_device;
89 sc->sc_flags = 0;
90
91 if (usb_lookup(ar3k12_devs, uaa->uaa_vendor, uaa->uaa_product))
92 sc->sc_flags |= AUBT_IS_AR3012;
93
94 config_mountroot(self, aubtfwl_attach_hook);
95}
96
97static int
98aubtfwl_detach(device_t self, int flags)
99{
100 struct aubtfwl_softc * const sc = device_private(self);
101
102 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, self);
103
104 return 0;
105}
106
107/* Returns 0 if firmware was correctly loaded */
108static int
109aubtfwl_firmware_load(device_t self, const char *name) {
110 struct aubtfwl_softc * const sc = device_private(self);
111 struct usbd_interface *iface;
112 struct usbd_pipe *pipe;
113 struct usbd_xfer *xfer;
114 void *buf;
115 usb_device_request_t req;
116 int error = 0;
117 firmware_handle_t fwh;
118 size_t fws;
119 size_t fwo = 0;
120 uint32_t n;
121
122 memset(&req, 0, sizeof(req));
123
124 error = firmware_open("ubt", name, &fwh);
125 if (error != 0) {
126 aprint_error_dev(self, "'%s' open fail %d\n", name, error);
127 return error;
128 }
129 fws = firmware_get_size(fwh);
130
131 error = usbd_set_config_no(sc->sc_udev, 1, 0);
132 if (error != 0) {
133 aprint_error_dev(self, "failed to set configuration"
134 ", err=%s\n", usbd_errstr(error));
135 goto out_firmware;
136 }
137
138 error = usbd_device2interface_handle(sc->sc_udev, 0, &iface);
139 if (error) {
140 aprint_error_dev(self, "failed to get interface, %s\n",
141 usbd_errstr(error));
142 goto out_firmware;
143 }
144
145 error = usbd_open_pipe(iface, UE_DIR_OUT|2, USBD_EXCLUSIVE_USE, &pipe);
146 if (error) {
147 aprint_error_dev(self, "failed to open pipe, %s\n",
148 usbd_errstr(error));
149 goto out_firmware;
150 }
151
152 error = usbd_create_xfer(pipe, AR3K_FIRMWARE_CHUNK_SIZE, 0, 0, &xfer);
153 if (error) {
154 aprint_verbose_dev(self, "cannot create xfer(%d)\n",
155 error);
156 goto out_pipe;
157 }
158 buf = usbd_get_buffer(xfer);
159
160 error = firmware_read(fwh, fwo, buf, AR3K_FIRMWARE_HEADER_SIZE);
161 if (error != 0) {
162 aprint_error_dev(self, "firmware_read failed %d\n", error);
163 goto out_xfer;
164 }
165
166 req.bRequest = AR3K_SEND_FIRMWARE;
167 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
168 USETW(req.wValue, 0);
169 USETW(req.wIndex, 0);
170 USETW(req.wLength, AR3K_FIRMWARE_HEADER_SIZE);
171
172 aprint_verbose_dev(self, "beginning firmware load\n");
173
174 error = usbd_do_request(sc->sc_udev, &req, buf);
175 if (error != 0) {
176 aprint_error_dev(self, "%s\n", usbd_errstr(error));
177 return error;
178 }
179 fwo = AR3K_FIRMWARE_HEADER_SIZE;
180
181 while (fwo < fws) {
182 n = min(AR3K_FIRMWARE_CHUNK_SIZE, fws - fwo);
183 error = firmware_read(fwh, fwo, buf, n);
184 if (error != 0) {
185 break;
186 }
187 error = usbd_bulk_transfer(xfer, pipe, 0, USBD_DEFAULT_TIMEOUT,
188 buf, &n);
189 if (error != USBD_NORMAL_COMPLETION) {
190 aprint_error_dev(self, "xfer failed, %s\n",
191 usbd_errstr(error));
192 break;
193 }
194 fwo += n;
195 }
196
197 if (error == 0)
198 aprint_verbose_dev(self, "firmware load complete\n");
199
200out_xfer:
201 usbd_destroy_xfer(xfer);
202out_pipe:
203 usbd_close_pipe(pipe);
204out_firmware:
205 firmware_close(fwh);
206
207 return !!error;
208}
209
210static int
211aubtfwl_get_state(struct aubtfwl_softc *sc, uint8_t *state) {
212 usb_device_request_t req;
213 int error = 0;
214
215 memset(&req, 0, sizeof(req));
216
217 req.bRequest = AR3K_GET_STATE;
218 req.bmRequestType = UT_READ_VENDOR_DEVICE;
219 USETW(req.wValue, 0);
220 USETW(req.wIndex, 0);
221 USETW(req.wLength, sizeof(*state));
222
223 error = usbd_do_request(sc->sc_udev, &req, state);
224
225 return error;
226}
227
228static int
229aubtfwl_get_version(struct aubtfwl_softc *sc, struct ar3k_version *ver) {
230 usb_device_request_t req;
231 int error = 0;
232
233 memset(&req, 0, sizeof(req));
234
235 req.bRequest = AR3K_GET_VERSION;
236 req.bmRequestType = UT_READ_VENDOR_DEVICE;
237 USETW(req.wValue, 0);
238 USETW(req.wIndex, 0);
239 USETW(req.wLength, sizeof(*ver));
240
241 error = usbd_do_request(sc->sc_udev, &req, ver);
242
243#if BYTE_ORDER == BIG_ENDIAN
244 if (error == USBD_NORMAL_COMPLETION) {
245 ver->rom = bswap32(ver->rom);
246 ver->build = bswap32(ver->build);
247 ver->ram = bswap32(ver->ram);
248 }
249#endif
250 return error;
251}
252
253static int
254aubtfwl_send_command(struct aubtfwl_softc *sc, uByte cmd) {
255 usb_device_request_t req;
256 int error = 0;
257
258 memset(&req, 0, sizeof(req));
259
260 req.bRequest = cmd;
261 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
262 USETW(req.wValue, 0);
263 USETW(req.wIndex, 0);
264 USETW(req.wLength, 0);
265
266 error = usbd_do_request(sc->sc_udev, &req, NULL);
267
268 return error;
269}
270
271static void
272aubtfwl_attach_hook(device_t self)
273{
274 struct aubtfwl_softc * const sc = device_private(self);
275 char firmware_name[MAXPATHLEN+1];
276 struct ar3k_version ver;
277 uint8_t state;
278 int clock = 0;
279 int error = 0;
280
281 if (sc->sc_flags & AUBT_IS_AR3012) {
282 error = aubtfwl_get_version(sc, &ver);
283 if (!error)
284 error = aubtfwl_get_state(sc, &state);
285
286 if (error) {
287 aprint_error_dev(self,
288 "couldn't get version or state\n");
289 return;
290 }
291
292 aprint_verbose_dev(self, "state is 0x%02x\n", state);
293
294 if (!(state & AR3K_STATE_IS_PATCHED)) {
295 snprintf(firmware_name, sizeof(firmware_name),
296 "ar3k/AthrBT_0x%08x.dfu", ver.rom);
297 error = aubtfwl_firmware_load(self, firmware_name);
298
299 if (error)
300 return;
301 }
302
303 switch (ver.clock) {
304 case AR3K_CLOCK_19M:
305 clock = 19;
306 break;
307 case AR3K_CLOCK_26M:
308 clock = 26;
309 break;
310 case AR3K_CLOCK_40M:
311 clock = 40;
312 break;
313 }
314
315 snprintf(firmware_name, sizeof(firmware_name),
316 "ar3k/ramps_0x%08x_%d.dfu", ver.rom, clock);
317 aubtfwl_firmware_load(self, firmware_name);
318
319 if ((state & AR3K_STATE_MODE_MASK) != AR3K_STATE_MODE_NORMAL) {
320 error = aubtfwl_send_command(sc, AR3K_SET_NORMAL_MODE);
321 if (error) {
322 aprint_error_dev(self,
323 "couldn't set normal mode: %s",
324 usbd_errstr(error));
325 return;
326 }
327 }
328
329 /* Apparently some devices will fail this, so ignore result */
330 (void) aubtfwl_send_command(sc, AR3K_SWITCH_VID_PID);
331 } else {
332 aubtfwl_firmware_load(self, "ath3k-1.fw");
333 }
334
335 return;
336}
337