Commit | Line | Data |
---|---|---|
9670d80a VK |
1 | /* |
2 | * Copyright (c) 2008-2009 Atheros Communications Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | * | |
18 | */ | |
19 | ||
20 | ||
21 | #include <linux/module.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/types.h> | |
26 | #include <linux/errno.h> | |
27 | #include <linux/device.h> | |
28 | #include <linux/firmware.h> | |
29 | #include <linux/usb.h> | |
30 | #include <net/bluetooth/bluetooth.h> | |
31 | ||
32 | #define VERSION "1.0" | |
c3eae82a | 33 | #define ATH3K_FIRMWARE "ath3k-1.fw" |
9670d80a | 34 | |
d9f51b51 BS |
35 | #define ATH3K_DNLOAD 0x01 |
36 | #define ATH3K_GETSTATE 0x05 | |
37 | #define ATH3K_SET_NORMAL_MODE 0x07 | |
38 | #define ATH3K_GETVERSION 0x09 | |
39 | #define USB_REG_SWITCH_VID_PID 0x0a | |
40 | ||
41 | #define ATH3K_MODE_MASK 0x3F | |
42 | #define ATH3K_NORMAL_MODE 0x0E | |
43 | ||
44 | #define ATH3K_PATCH_UPDATE 0x80 | |
45 | #define ATH3K_SYSCFG_UPDATE 0x40 | |
46 | ||
47 | #define ATH3K_XTAL_FREQ_26M 0x00 | |
48 | #define ATH3K_XTAL_FREQ_40M 0x01 | |
49 | #define ATH3K_XTAL_FREQ_19P2 0x02 | |
50 | #define ATH3K_NAME_LEN 0xFF | |
51 | ||
52 | struct ath3k_version { | |
53 | unsigned int rom_version; | |
54 | unsigned int build_version; | |
55 | unsigned int ram_version; | |
56 | unsigned char ref_clock; | |
57 | unsigned char reserved[0x07]; | |
58 | }; | |
9670d80a VK |
59 | |
60 | static struct usb_device_id ath3k_table[] = { | |
61 | /* Atheros AR3011 */ | |
62 | { USB_DEVICE(0x0CF3, 0x3000) }, | |
be93112a BS |
63 | |
64 | /* Atheros AR3011 with sflash firmware*/ | |
65 | { USB_DEVICE(0x0CF3, 0x3002) }, | |
2a7bcccc | 66 | { USB_DEVICE(0x13d3, 0x3304) }, |
8e7c3d2e | 67 | { USB_DEVICE(0x0930, 0x0215) }, |
6b6ba88b | 68 | { USB_DEVICE(0x0489, 0xE03D) }, |
be93112a | 69 | |
509e7861 CYC |
70 | /* Atheros AR9285 Malbec with sflash firmware */ |
71 | { USB_DEVICE(0x03F0, 0x311D) }, | |
d9f51b51 BS |
72 | |
73 | /* Atheros AR3012 with sflash firmware*/ | |
74 | { USB_DEVICE(0x0CF3, 0x3004) }, | |
75 | ||
e9036e33 CYC |
76 | /* Atheros AR5BBU12 with sflash firmware */ |
77 | { USB_DEVICE(0x0489, 0xE02C) }, | |
b67afe7f | 78 | |
9670d80a VK |
79 | { } /* Terminating entry */ |
80 | }; | |
81 | ||
82 | MODULE_DEVICE_TABLE(usb, ath3k_table); | |
83 | ||
d9f51b51 BS |
84 | #define BTUSB_ATH3012 0x80 |
85 | /* This table is to load patch and sysconfig files | |
86 | * for AR3012 */ | |
87 | static struct usb_device_id ath3k_blist_tbl[] = { | |
88 | ||
89 | /* Atheros AR3012 with sflash firmware*/ | |
90 | { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 }, | |
91 | ||
92 | { } /* Terminating entry */ | |
93 | }; | |
94 | ||
9670d80a VK |
95 | #define USB_REQ_DFU_DNLOAD 1 |
96 | #define BULK_SIZE 4096 | |
d9f51b51 | 97 | #define FW_HDR_SIZE 20 |
9670d80a | 98 | |
86e09287 AH |
99 | static int ath3k_load_firmware(struct usb_device *udev, |
100 | const struct firmware *firmware) | |
9670d80a VK |
101 | { |
102 | u8 *send_buf; | |
103 | int err, pipe, len, size, sent = 0; | |
86e09287 | 104 | int count = firmware->size; |
9670d80a | 105 | |
86e09287 | 106 | BT_DBG("udev %p", udev); |
9670d80a | 107 | |
86e09287 | 108 | pipe = usb_sndctrlpipe(udev, 0); |
9670d80a | 109 | |
52a1020e | 110 | send_buf = kmalloc(BULK_SIZE, GFP_KERNEL); |
86e09287 AH |
111 | if (!send_buf) { |
112 | BT_ERR("Can't allocate memory chunk for firmware"); | |
113 | return -ENOMEM; | |
114 | } | |
115 | ||
116 | memcpy(send_buf, firmware->data, 20); | |
117 | if ((err = usb_control_msg(udev, pipe, | |
9670d80a VK |
118 | USB_REQ_DFU_DNLOAD, |
119 | USB_TYPE_VENDOR, 0, 0, | |
86e09287 | 120 | send_buf, 20, USB_CTRL_SET_TIMEOUT)) < 0) { |
9670d80a | 121 | BT_ERR("Can't change to loading configuration err"); |
86e09287 | 122 | goto error; |
9670d80a VK |
123 | } |
124 | sent += 20; | |
125 | count -= 20; | |
126 | ||
9670d80a VK |
127 | while (count) { |
128 | size = min_t(uint, count, BULK_SIZE); | |
86e09287 AH |
129 | pipe = usb_sndbulkpipe(udev, 0x02); |
130 | memcpy(send_buf, firmware->data + sent, size); | |
9670d80a | 131 | |
86e09287 | 132 | err = usb_bulk_msg(udev, pipe, send_buf, size, |
9670d80a VK |
133 | &len, 3000); |
134 | ||
135 | if (err || (len != size)) { | |
136 | BT_ERR("Error in firmware loading err = %d," | |
137 | "len = %d, size = %d", err, len, size); | |
138 | goto error; | |
139 | } | |
140 | ||
141 | sent += size; | |
142 | count -= size; | |
143 | } | |
144 | ||
9670d80a VK |
145 | error: |
146 | kfree(send_buf); | |
147 | return err; | |
148 | } | |
149 | ||
d9f51b51 BS |
150 | static int ath3k_get_state(struct usb_device *udev, unsigned char *state) |
151 | { | |
152 | int pipe = 0; | |
153 | ||
154 | pipe = usb_rcvctrlpipe(udev, 0); | |
155 | return usb_control_msg(udev, pipe, ATH3K_GETSTATE, | |
156 | USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, | |
157 | state, 0x01, USB_CTRL_SET_TIMEOUT); | |
158 | } | |
159 | ||
160 | static int ath3k_get_version(struct usb_device *udev, | |
161 | struct ath3k_version *version) | |
162 | { | |
163 | int pipe = 0; | |
164 | ||
165 | pipe = usb_rcvctrlpipe(udev, 0); | |
166 | return usb_control_msg(udev, pipe, ATH3K_GETVERSION, | |
167 | USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, version, | |
168 | sizeof(struct ath3k_version), | |
169 | USB_CTRL_SET_TIMEOUT); | |
170 | } | |
171 | ||
172 | static int ath3k_load_fwfile(struct usb_device *udev, | |
173 | const struct firmware *firmware) | |
174 | { | |
175 | u8 *send_buf; | |
176 | int err, pipe, len, size, count, sent = 0; | |
177 | int ret; | |
178 | ||
179 | count = firmware->size; | |
180 | ||
52a1020e | 181 | send_buf = kmalloc(BULK_SIZE, GFP_KERNEL); |
d9f51b51 BS |
182 | if (!send_buf) { |
183 | BT_ERR("Can't allocate memory chunk for firmware"); | |
184 | return -ENOMEM; | |
185 | } | |
186 | ||
187 | size = min_t(uint, count, FW_HDR_SIZE); | |
188 | memcpy(send_buf, firmware->data, size); | |
189 | ||
190 | pipe = usb_sndctrlpipe(udev, 0); | |
191 | ret = usb_control_msg(udev, pipe, ATH3K_DNLOAD, | |
192 | USB_TYPE_VENDOR, 0, 0, send_buf, | |
193 | size, USB_CTRL_SET_TIMEOUT); | |
194 | if (ret < 0) { | |
195 | BT_ERR("Can't change to loading configuration err"); | |
196 | kfree(send_buf); | |
197 | return ret; | |
198 | } | |
199 | ||
200 | sent += size; | |
201 | count -= size; | |
202 | ||
203 | while (count) { | |
204 | size = min_t(uint, count, BULK_SIZE); | |
205 | pipe = usb_sndbulkpipe(udev, 0x02); | |
206 | ||
207 | memcpy(send_buf, firmware->data + sent, size); | |
208 | ||
209 | err = usb_bulk_msg(udev, pipe, send_buf, size, | |
210 | &len, 3000); | |
211 | if (err || (len != size)) { | |
212 | BT_ERR("Error in firmware loading err = %d," | |
213 | "len = %d, size = %d", err, len, size); | |
214 | kfree(send_buf); | |
215 | return err; | |
216 | } | |
217 | sent += size; | |
218 | count -= size; | |
219 | } | |
220 | ||
221 | kfree(send_buf); | |
222 | return 0; | |
223 | } | |
224 | ||
225 | static int ath3k_switch_pid(struct usb_device *udev) | |
226 | { | |
227 | int pipe = 0; | |
228 | ||
229 | pipe = usb_sndctrlpipe(udev, 0); | |
230 | return usb_control_msg(udev, pipe, USB_REG_SWITCH_VID_PID, | |
231 | USB_TYPE_VENDOR, 0, 0, | |
232 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
233 | } | |
234 | ||
235 | static int ath3k_set_normal_mode(struct usb_device *udev) | |
236 | { | |
237 | unsigned char fw_state; | |
238 | int pipe = 0, ret; | |
239 | ||
240 | ret = ath3k_get_state(udev, &fw_state); | |
241 | if (ret < 0) { | |
242 | BT_ERR("Can't get state to change to normal mode err"); | |
243 | return ret; | |
244 | } | |
245 | ||
246 | if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) { | |
247 | BT_DBG("firmware was already in normal mode"); | |
248 | return 0; | |
249 | } | |
250 | ||
251 | pipe = usb_sndctrlpipe(udev, 0); | |
252 | return usb_control_msg(udev, pipe, ATH3K_SET_NORMAL_MODE, | |
253 | USB_TYPE_VENDOR, 0, 0, | |
254 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
255 | } | |
256 | ||
257 | static int ath3k_load_patch(struct usb_device *udev) | |
258 | { | |
259 | unsigned char fw_state; | |
260 | char filename[ATH3K_NAME_LEN] = {0}; | |
261 | const struct firmware *firmware; | |
262 | struct ath3k_version fw_version, pt_version; | |
263 | int ret; | |
264 | ||
265 | ret = ath3k_get_state(udev, &fw_state); | |
266 | if (ret < 0) { | |
267 | BT_ERR("Can't get state to change to load ram patch err"); | |
268 | return ret; | |
269 | } | |
270 | ||
271 | if (fw_state & ATH3K_PATCH_UPDATE) { | |
272 | BT_DBG("Patch was already downloaded"); | |
273 | return 0; | |
274 | } | |
275 | ||
276 | ret = ath3k_get_version(udev, &fw_version); | |
277 | if (ret < 0) { | |
278 | BT_ERR("Can't get version to change to load ram patch err"); | |
279 | return ret; | |
280 | } | |
281 | ||
282 | snprintf(filename, ATH3K_NAME_LEN, "ar3k/AthrBT_0x%08x.dfu", | |
283 | fw_version.rom_version); | |
284 | ||
285 | ret = request_firmware(&firmware, filename, &udev->dev); | |
286 | if (ret < 0) { | |
287 | BT_ERR("Patch file not found %s", filename); | |
288 | return ret; | |
289 | } | |
290 | ||
291 | pt_version.rom_version = *(int *)(firmware->data + firmware->size - 8); | |
292 | pt_version.build_version = *(int *) | |
293 | (firmware->data + firmware->size - 4); | |
294 | ||
295 | if ((pt_version.rom_version != fw_version.rom_version) || | |
296 | (pt_version.build_version <= fw_version.build_version)) { | |
297 | BT_ERR("Patch file version did not match with firmware"); | |
298 | release_firmware(firmware); | |
299 | return -EINVAL; | |
300 | } | |
301 | ||
302 | ret = ath3k_load_fwfile(udev, firmware); | |
303 | release_firmware(firmware); | |
304 | ||
305 | return ret; | |
306 | } | |
307 | ||
308 | static int ath3k_load_syscfg(struct usb_device *udev) | |
309 | { | |
310 | unsigned char fw_state; | |
311 | char filename[ATH3K_NAME_LEN] = {0}; | |
312 | const struct firmware *firmware; | |
313 | struct ath3k_version fw_version; | |
314 | int clk_value, ret; | |
315 | ||
316 | ret = ath3k_get_state(udev, &fw_state); | |
317 | if (ret < 0) { | |
318 | BT_ERR("Can't get state to change to load configration err"); | |
319 | return -EBUSY; | |
320 | } | |
321 | ||
322 | ret = ath3k_get_version(udev, &fw_version); | |
323 | if (ret < 0) { | |
324 | BT_ERR("Can't get version to change to load ram patch err"); | |
325 | return ret; | |
326 | } | |
327 | ||
328 | switch (fw_version.ref_clock) { | |
329 | ||
330 | case ATH3K_XTAL_FREQ_26M: | |
331 | clk_value = 26; | |
332 | break; | |
333 | case ATH3K_XTAL_FREQ_40M: | |
334 | clk_value = 40; | |
335 | break; | |
336 | case ATH3K_XTAL_FREQ_19P2: | |
337 | clk_value = 19; | |
338 | break; | |
339 | default: | |
340 | clk_value = 0; | |
341 | break; | |
342 | } | |
343 | ||
344 | snprintf(filename, ATH3K_NAME_LEN, "ar3k/ramps_0x%08x_%d%s", | |
345 | fw_version.rom_version, clk_value, ".dfu"); | |
346 | ||
347 | ret = request_firmware(&firmware, filename, &udev->dev); | |
348 | if (ret < 0) { | |
349 | BT_ERR("Configuration file not found %s", filename); | |
350 | return ret; | |
351 | } | |
352 | ||
353 | ret = ath3k_load_fwfile(udev, firmware); | |
354 | release_firmware(firmware); | |
355 | ||
356 | return ret; | |
357 | } | |
358 | ||
9670d80a VK |
359 | static int ath3k_probe(struct usb_interface *intf, |
360 | const struct usb_device_id *id) | |
361 | { | |
362 | const struct firmware *firmware; | |
363 | struct usb_device *udev = interface_to_usbdev(intf); | |
84f0e17f | 364 | int ret; |
9670d80a VK |
365 | |
366 | BT_DBG("intf %p id %p", intf, id); | |
367 | ||
368 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) | |
369 | return -ENODEV; | |
370 | ||
d9f51b51 BS |
371 | /* match device ID in ath3k blacklist table */ |
372 | if (!id->driver_info) { | |
373 | const struct usb_device_id *match; | |
374 | match = usb_match_id(intf, ath3k_blist_tbl); | |
375 | if (match) | |
376 | id = match; | |
377 | } | |
378 | ||
379 | /* load patch and sysconfig files for AR3012 */ | |
380 | if (id->driver_info & BTUSB_ATH3012) { | |
2d25f8b4 SL |
381 | |
382 | /* New firmware with patch and sysconfig files already loaded */ | |
383 | if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x0001) | |
384 | return -ENODEV; | |
385 | ||
d9f51b51 BS |
386 | ret = ath3k_load_patch(udev); |
387 | if (ret < 0) { | |
388 | BT_ERR("Loading patch file failed"); | |
389 | return ret; | |
390 | } | |
391 | ret = ath3k_load_syscfg(udev); | |
392 | if (ret < 0) { | |
393 | BT_ERR("Loading sysconfig file failed"); | |
394 | return ret; | |
395 | } | |
396 | ret = ath3k_set_normal_mode(udev); | |
397 | if (ret < 0) { | |
398 | BT_ERR("Set normal mode failed"); | |
399 | return ret; | |
400 | } | |
401 | ath3k_switch_pid(udev); | |
402 | return 0; | |
403 | } | |
404 | ||
c3eae82a PF |
405 | ret = request_firmware(&firmware, ATH3K_FIRMWARE, &udev->dev); |
406 | if (ret < 0) { | |
407 | if (ret == -ENOENT) | |
408 | BT_ERR("Firmware file \"%s\" not found", | |
409 | ATH3K_FIRMWARE); | |
410 | else | |
411 | BT_ERR("Firmware file \"%s\" request failed (err=%d)", | |
412 | ATH3K_FIRMWARE, ret); | |
413 | return ret; | |
9670d80a VK |
414 | } |
415 | ||
84f0e17f | 416 | ret = ath3k_load_firmware(udev, firmware); |
86e09287 | 417 | release_firmware(firmware); |
9670d80a | 418 | |
84f0e17f | 419 | return ret; |
9670d80a VK |
420 | } |
421 | ||
422 | static void ath3k_disconnect(struct usb_interface *intf) | |
423 | { | |
9670d80a | 424 | BT_DBG("ath3k_disconnect intf %p", intf); |
9670d80a VK |
425 | } |
426 | ||
427 | static struct usb_driver ath3k_driver = { | |
428 | .name = "ath3k", | |
429 | .probe = ath3k_probe, | |
430 | .disconnect = ath3k_disconnect, | |
431 | .id_table = ath3k_table, | |
432 | }; | |
433 | ||
93f1508c | 434 | module_usb_driver(ath3k_driver); |
9670d80a VK |
435 | |
436 | MODULE_AUTHOR("Atheros Communications"); | |
437 | MODULE_DESCRIPTION("Atheros AR30xx firmware driver"); | |
438 | MODULE_VERSION(VERSION); | |
439 | MODULE_LICENSE("GPL"); | |
c3eae82a | 440 | MODULE_FIRMWARE(ATH3K_FIRMWARE); |