aubtfwl.c revision 1.5.10.7 1 /* $NetBSD: aubtfwl.c,v 1.5.10.7 2015/09/29 11:38:28 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.5.10.7 2015/09/29 11:38:28 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
44 static int aubtfwl_match(device_t, cfdata_t, void *);
45 static void aubtfwl_attach(device_t, device_t, void *);
46 static int aubtfwl_detach(device_t, int);
47 static void aubtfwl_attach_hook(device_t);
48
49 struct aubtfwl_softc {
50 struct usbd_device *sc_udev;
51 int sc_flags;
52 #define AUBT_IS_AR3012 1
53 };
54
55 CFATTACH_DECL_NEW(aubtfwl, sizeof(struct aubtfwl_softc), aubtfwl_match, aubtfwl_attach, aubtfwl_detach, NULL);
56
57 static const struct usb_devno ar3k_devs[] = {
58 { USB_VENDOR_ATHEROS2, USB_PRODUCT_ATHEROS2_AR3011 },
59 };
60
61 static const struct usb_devno ar3k12_devs[] = {
62 { USB_VENDOR_FOXCONN, USB_PRODUCT_FOXCONN_AR3012 },
63 };
64
65 static int
66 aubtfwl_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
81 static void
82 aubtfwl_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
97 static int
98 aubtfwl_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 */
108 static int
109 aubtfwl_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 xfer = usbd_alloc_xfer(sc->sc_udev);
153 if (xfer == NULL) {
154 aprint_error_dev(self, "failed to alloc xfer\n");
155 error = 1;
156 goto out_pipe;
157 }
158
159 buf = usbd_alloc_buffer(xfer, AR3K_FIRMWARE_CHUNK_SIZE);
160 if (buf == NULL) {
161 aprint_error_dev(self, "failed to alloc buffer\n");
162 error = 1;
163 goto out_xfer;
164 }
165
166 error = firmware_read(fwh, fwo, buf, AR3K_FIRMWARE_HEADER_SIZE);
167 if (error != 0) {
168 aprint_error_dev(self, "firmware_read failed %d\n", error);
169 goto out_xfer;
170 }
171
172 req.bRequest = AR3K_SEND_FIRMWARE;
173 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
174 USETW(req.wValue, 0);
175 USETW(req.wIndex, 0);
176 USETW(req.wLength, AR3K_FIRMWARE_HEADER_SIZE);
177
178 aprint_verbose_dev(self, "beginning firmware load\n");
179
180 error = usbd_do_request(sc->sc_udev, &req, buf);
181 if (error != 0) {
182 aprint_error_dev(self, "%s\n", usbd_errstr(error));
183 return error;
184 }
185 fwo = AR3K_FIRMWARE_HEADER_SIZE;
186
187 while (fwo < fws) {
188 n = min(AR3K_FIRMWARE_CHUNK_SIZE, fws - fwo);
189 error = firmware_read(fwh, fwo, buf, n);
190 if (error != 0) {
191 break;
192 }
193 error = usbd_bulk_transfer(xfer, pipe, 0, USBD_DEFAULT_TIMEOUT,
194 buf, &n);
195 if (error != USBD_NORMAL_COMPLETION) {
196 aprint_error_dev(self, "xfer failed, %s\n",
197 usbd_errstr(error));
198 break;
199 }
200 fwo += n;
201 }
202
203 if (error == 0)
204 aprint_verbose_dev(self, "firmware load complete\n");
205
206 out_xfer:
207 usbd_free_xfer(xfer);
208 out_pipe:
209 usbd_close_pipe(pipe);
210 out_firmware:
211 firmware_close(fwh);
212
213 return !!error;
214 }
215
216 static int
217 aubtfwl_get_state(struct aubtfwl_softc *sc, uint8_t *state) {
218 usb_device_request_t req;
219 int error = 0;
220
221 memset(&req, 0, sizeof(req));
222
223 req.bRequest = AR3K_GET_STATE;
224 req.bmRequestType = UT_READ_VENDOR_DEVICE;
225 USETW(req.wValue, 0);
226 USETW(req.wIndex, 0);
227 USETW(req.wLength, sizeof(*state));
228
229 error = usbd_do_request(sc->sc_udev, &req, state);
230
231 return error;
232 }
233
234 static int
235 aubtfwl_get_version(struct aubtfwl_softc *sc, struct ar3k_version *ver) {
236 usb_device_request_t req;
237 int error = 0;
238
239 memset(&req, 0, sizeof(req));
240
241 req.bRequest = AR3K_GET_VERSION;
242 req.bmRequestType = UT_READ_VENDOR_DEVICE;
243 USETW(req.wValue, 0);
244 USETW(req.wIndex, 0);
245 USETW(req.wLength, sizeof(*ver));
246
247 error = usbd_do_request(sc->sc_udev, &req, ver);
248
249 #if BYTE_ORDER == BIG_ENDIAN
250 if (error == USBD_NORMAL_COMPLETION) {
251 ver->rom = bswap32(ver->rom);
252 ver->build = bswap32(ver->build);
253 ver->ram = bswap32(ver->ram);
254 }
255 #endif
256 return error;
257 }
258
259 static int
260 aubtfwl_send_command(struct aubtfwl_softc *sc, uByte cmd) {
261 usb_device_request_t req;
262 int error = 0;
263
264 memset(&req, 0, sizeof(req));
265
266 req.bRequest = cmd;
267 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
268 USETW(req.wValue, 0);
269 USETW(req.wIndex, 0);
270 USETW(req.wLength, 0);
271
272 error = usbd_do_request(sc->sc_udev, &req, NULL);
273
274 return error;
275 }
276
277 static void
278 aubtfwl_attach_hook(device_t self)
279 {
280 struct aubtfwl_softc * const sc = device_private(self);
281 char firmware_name[MAXPATHLEN+1];
282 struct ar3k_version ver;
283 uint8_t state;
284 int clock = 0;
285 int error = 0;
286
287 if (sc->sc_flags & AUBT_IS_AR3012) {
288 error = aubtfwl_get_version(sc, &ver);
289 if (!error)
290 error = aubtfwl_get_state(sc, &state);
291
292 if (error) {
293 aprint_error_dev(self,
294 "couldn't get version or state\n");
295 return;
296 }
297
298 aprint_verbose_dev(self, "state is 0x%02x\n", state);
299
300 if (!(state & AR3K_STATE_IS_PATCHED)) {
301 snprintf(firmware_name, sizeof(firmware_name),
302 "ar3k/AthrBT_0x%08x.dfu", ver.rom);
303 error = aubtfwl_firmware_load(self, firmware_name);
304
305 if (error)
306 return;
307 }
308
309 switch (ver.clock) {
310 case AR3K_CLOCK_19M:
311 clock = 19;
312 break;
313 case AR3K_CLOCK_26M:
314 clock = 26;
315 break;
316 case AR3K_CLOCK_40M:
317 clock = 40;
318 break;
319 }
320
321 snprintf(firmware_name, sizeof(firmware_name),
322 "ar3k/ramps_0x%08x_%d.dfu", ver.rom, clock);
323 aubtfwl_firmware_load(self, firmware_name);
324
325 if ((state & AR3K_STATE_MODE_MASK) != AR3K_STATE_MODE_NORMAL) {
326 error = aubtfwl_send_command(sc, AR3K_SET_NORMAL_MODE);
327 if (error) {
328 aprint_error_dev(self,
329 "couldn't set normal mode: %s",
330 usbd_errstr(error));
331 return;
332 }
333 }
334
335 /* Apparently some devices will fail this, so ignore result */
336 (void) aubtfwl_send_command(sc, AR3K_SWITCH_VID_PID);
337 } else {
338 aubtfwl_firmware_load(self, "ath3k-1.fw");
339 }
340
341 return;
342 }
343