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) }, | |
07c0ea87 | 75 | { USB_DEVICE(0x0CF3, 0x311D) }, |
9498ba7a | 76 | { USB_DEVICE(0x13d3, 0x3375) }, |
55ed7d4d | 77 | { USB_DEVICE(0x04CA, 0x3005) }, |
87522a43 | 78 | { USB_DEVICE(0x13d3, 0x3362) }, |
d9f51b51 | 79 | |
e9036e33 CYC |
80 | /* Atheros AR5BBU12 with sflash firmware */ |
81 | { USB_DEVICE(0x0489, 0xE02C) }, | |
b67afe7f | 82 | |
9670d80a VK |
83 | { } /* Terminating entry */ |
84 | }; | |
85 | ||
86 | MODULE_DEVICE_TABLE(usb, ath3k_table); | |
87 | ||
d9f51b51 BS |
88 | #define BTUSB_ATH3012 0x80 |
89 | /* This table is to load patch and sysconfig files | |
90 | * for AR3012 */ | |
91 | static struct usb_device_id ath3k_blist_tbl[] = { | |
92 | ||
93 | /* Atheros AR3012 with sflash firmware*/ | |
94 | { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 }, | |
07c0ea87 | 95 | { USB_DEVICE(0x0cf3, 0x311D), .driver_info = BTUSB_ATH3012 }, |
9498ba7a | 96 | { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 }, |
55ed7d4d | 97 | { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 }, |
87522a43 | 98 | { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 }, |
d9f51b51 BS |
99 | |
100 | { } /* Terminating entry */ | |
101 | }; | |
102 | ||
9670d80a VK |
103 | #define USB_REQ_DFU_DNLOAD 1 |
104 | #define BULK_SIZE 4096 | |
d9f51b51 | 105 | #define FW_HDR_SIZE 20 |
9670d80a | 106 | |
86e09287 AH |
107 | static int ath3k_load_firmware(struct usb_device *udev, |
108 | const struct firmware *firmware) | |
9670d80a VK |
109 | { |
110 | u8 *send_buf; | |
111 | int err, pipe, len, size, sent = 0; | |
86e09287 | 112 | int count = firmware->size; |
9670d80a | 113 | |
86e09287 | 114 | BT_DBG("udev %p", udev); |
9670d80a | 115 | |
86e09287 | 116 | pipe = usb_sndctrlpipe(udev, 0); |
9670d80a | 117 | |
52a1020e | 118 | send_buf = kmalloc(BULK_SIZE, GFP_KERNEL); |
86e09287 AH |
119 | if (!send_buf) { |
120 | BT_ERR("Can't allocate memory chunk for firmware"); | |
121 | return -ENOMEM; | |
122 | } | |
123 | ||
124 | memcpy(send_buf, firmware->data, 20); | |
125 | if ((err = usb_control_msg(udev, pipe, | |
9670d80a VK |
126 | USB_REQ_DFU_DNLOAD, |
127 | USB_TYPE_VENDOR, 0, 0, | |
86e09287 | 128 | send_buf, 20, USB_CTRL_SET_TIMEOUT)) < 0) { |
9670d80a | 129 | BT_ERR("Can't change to loading configuration err"); |
86e09287 | 130 | goto error; |
9670d80a VK |
131 | } |
132 | sent += 20; | |
133 | count -= 20; | |
134 | ||
9670d80a VK |
135 | while (count) { |
136 | size = min_t(uint, count, BULK_SIZE); | |
86e09287 AH |
137 | pipe = usb_sndbulkpipe(udev, 0x02); |
138 | memcpy(send_buf, firmware->data + sent, size); | |
9670d80a | 139 | |
86e09287 | 140 | err = usb_bulk_msg(udev, pipe, send_buf, size, |
9670d80a VK |
141 | &len, 3000); |
142 | ||
143 | if (err || (len != size)) { | |
144 | BT_ERR("Error in firmware loading err = %d," | |
145 | "len = %d, size = %d", err, len, size); | |
146 | goto error; | |
147 | } | |
148 | ||
149 | sent += size; | |
150 | count -= size; | |
151 | } | |
152 | ||
9670d80a VK |
153 | error: |
154 | kfree(send_buf); | |
155 | return err; | |
156 | } | |
157 | ||
d9f51b51 BS |
158 | static int ath3k_get_state(struct usb_device *udev, unsigned char *state) |
159 | { | |
160 | int pipe = 0; | |
161 | ||
162 | pipe = usb_rcvctrlpipe(udev, 0); | |
163 | return usb_control_msg(udev, pipe, ATH3K_GETSTATE, | |
164 | USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, | |
165 | state, 0x01, USB_CTRL_SET_TIMEOUT); | |
166 | } | |
167 | ||
168 | static int ath3k_get_version(struct usb_device *udev, | |
169 | struct ath3k_version *version) | |
170 | { | |
171 | int pipe = 0; | |
172 | ||
173 | pipe = usb_rcvctrlpipe(udev, 0); | |
174 | return usb_control_msg(udev, pipe, ATH3K_GETVERSION, | |
175 | USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, version, | |
176 | sizeof(struct ath3k_version), | |
177 | USB_CTRL_SET_TIMEOUT); | |
178 | } | |
179 | ||
180 | static int ath3k_load_fwfile(struct usb_device *udev, | |
181 | const struct firmware *firmware) | |
182 | { | |
183 | u8 *send_buf; | |
184 | int err, pipe, len, size, count, sent = 0; | |
185 | int ret; | |
186 | ||
187 | count = firmware->size; | |
188 | ||
52a1020e | 189 | send_buf = kmalloc(BULK_SIZE, GFP_KERNEL); |
d9f51b51 BS |
190 | if (!send_buf) { |
191 | BT_ERR("Can't allocate memory chunk for firmware"); | |
192 | return -ENOMEM; | |
193 | } | |
194 | ||
195 | size = min_t(uint, count, FW_HDR_SIZE); | |
196 | memcpy(send_buf, firmware->data, size); | |
197 | ||
198 | pipe = usb_sndctrlpipe(udev, 0); | |
199 | ret = usb_control_msg(udev, pipe, ATH3K_DNLOAD, | |
200 | USB_TYPE_VENDOR, 0, 0, send_buf, | |
201 | size, USB_CTRL_SET_TIMEOUT); | |
202 | if (ret < 0) { | |
203 | BT_ERR("Can't change to loading configuration err"); | |
204 | kfree(send_buf); | |
205 | return ret; | |
206 | } | |
207 | ||
208 | sent += size; | |
209 | count -= size; | |
210 | ||
211 | while (count) { | |
212 | size = min_t(uint, count, BULK_SIZE); | |
213 | pipe = usb_sndbulkpipe(udev, 0x02); | |
214 | ||
215 | memcpy(send_buf, firmware->data + sent, size); | |
216 | ||
217 | err = usb_bulk_msg(udev, pipe, send_buf, size, | |
218 | &len, 3000); | |
219 | if (err || (len != size)) { | |
220 | BT_ERR("Error in firmware loading err = %d," | |
221 | "len = %d, size = %d", err, len, size); | |
222 | kfree(send_buf); | |
223 | return err; | |
224 | } | |
225 | sent += size; | |
226 | count -= size; | |
227 | } | |
228 | ||
229 | kfree(send_buf); | |
230 | return 0; | |
231 | } | |
232 | ||
233 | static int ath3k_switch_pid(struct usb_device *udev) | |
234 | { | |
235 | int pipe = 0; | |
236 | ||
237 | pipe = usb_sndctrlpipe(udev, 0); | |
238 | return usb_control_msg(udev, pipe, USB_REG_SWITCH_VID_PID, | |
239 | USB_TYPE_VENDOR, 0, 0, | |
240 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
241 | } | |
242 | ||
243 | static int ath3k_set_normal_mode(struct usb_device *udev) | |
244 | { | |
245 | unsigned char fw_state; | |
246 | int pipe = 0, ret; | |
247 | ||
248 | ret = ath3k_get_state(udev, &fw_state); | |
249 | if (ret < 0) { | |
250 | BT_ERR("Can't get state to change to normal mode err"); | |
251 | return ret; | |
252 | } | |
253 | ||
254 | if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) { | |
255 | BT_DBG("firmware was already in normal mode"); | |
256 | return 0; | |
257 | } | |
258 | ||
259 | pipe = usb_sndctrlpipe(udev, 0); | |
260 | return usb_control_msg(udev, pipe, ATH3K_SET_NORMAL_MODE, | |
261 | USB_TYPE_VENDOR, 0, 0, | |
262 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
263 | } | |
264 | ||
265 | static int ath3k_load_patch(struct usb_device *udev) | |
266 | { | |
267 | unsigned char fw_state; | |
268 | char filename[ATH3K_NAME_LEN] = {0}; | |
269 | const struct firmware *firmware; | |
270 | struct ath3k_version fw_version, pt_version; | |
271 | int ret; | |
272 | ||
273 | ret = ath3k_get_state(udev, &fw_state); | |
274 | if (ret < 0) { | |
275 | BT_ERR("Can't get state to change to load ram patch err"); | |
276 | return ret; | |
277 | } | |
278 | ||
279 | if (fw_state & ATH3K_PATCH_UPDATE) { | |
280 | BT_DBG("Patch was already downloaded"); | |
281 | return 0; | |
282 | } | |
283 | ||
284 | ret = ath3k_get_version(udev, &fw_version); | |
285 | if (ret < 0) { | |
286 | BT_ERR("Can't get version to change to load ram patch err"); | |
287 | return ret; | |
288 | } | |
289 | ||
290 | snprintf(filename, ATH3K_NAME_LEN, "ar3k/AthrBT_0x%08x.dfu", | |
291 | fw_version.rom_version); | |
292 | ||
293 | ret = request_firmware(&firmware, filename, &udev->dev); | |
294 | if (ret < 0) { | |
295 | BT_ERR("Patch file not found %s", filename); | |
296 | return ret; | |
297 | } | |
298 | ||
299 | pt_version.rom_version = *(int *)(firmware->data + firmware->size - 8); | |
300 | pt_version.build_version = *(int *) | |
301 | (firmware->data + firmware->size - 4); | |
302 | ||
303 | if ((pt_version.rom_version != fw_version.rom_version) || | |
304 | (pt_version.build_version <= fw_version.build_version)) { | |
305 | BT_ERR("Patch file version did not match with firmware"); | |
306 | release_firmware(firmware); | |
307 | return -EINVAL; | |
308 | } | |
309 | ||
310 | ret = ath3k_load_fwfile(udev, firmware); | |
311 | release_firmware(firmware); | |
312 | ||
313 | return ret; | |
314 | } | |
315 | ||
316 | static int ath3k_load_syscfg(struct usb_device *udev) | |
317 | { | |
318 | unsigned char fw_state; | |
319 | char filename[ATH3K_NAME_LEN] = {0}; | |
320 | const struct firmware *firmware; | |
321 | struct ath3k_version fw_version; | |
322 | int clk_value, ret; | |
323 | ||
324 | ret = ath3k_get_state(udev, &fw_state); | |
325 | if (ret < 0) { | |
326 | BT_ERR("Can't get state to change to load configration err"); | |
327 | return -EBUSY; | |
328 | } | |
329 | ||
330 | ret = ath3k_get_version(udev, &fw_version); | |
331 | if (ret < 0) { | |
332 | BT_ERR("Can't get version to change to load ram patch err"); | |
333 | return ret; | |
334 | } | |
335 | ||
336 | switch (fw_version.ref_clock) { | |
337 | ||
338 | case ATH3K_XTAL_FREQ_26M: | |
339 | clk_value = 26; | |
340 | break; | |
341 | case ATH3K_XTAL_FREQ_40M: | |
342 | clk_value = 40; | |
343 | break; | |
344 | case ATH3K_XTAL_FREQ_19P2: | |
345 | clk_value = 19; | |
346 | break; | |
347 | default: | |
348 | clk_value = 0; | |
349 | break; | |
350 | } | |
351 | ||
352 | snprintf(filename, ATH3K_NAME_LEN, "ar3k/ramps_0x%08x_%d%s", | |
353 | fw_version.rom_version, clk_value, ".dfu"); | |
354 | ||
355 | ret = request_firmware(&firmware, filename, &udev->dev); | |
356 | if (ret < 0) { | |
357 | BT_ERR("Configuration file not found %s", filename); | |
358 | return ret; | |
359 | } | |
360 | ||
361 | ret = ath3k_load_fwfile(udev, firmware); | |
362 | release_firmware(firmware); | |
363 | ||
364 | return ret; | |
365 | } | |
366 | ||
9670d80a VK |
367 | static int ath3k_probe(struct usb_interface *intf, |
368 | const struct usb_device_id *id) | |
369 | { | |
370 | const struct firmware *firmware; | |
371 | struct usb_device *udev = interface_to_usbdev(intf); | |
84f0e17f | 372 | int ret; |
9670d80a VK |
373 | |
374 | BT_DBG("intf %p id %p", intf, id); | |
375 | ||
376 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) | |
377 | return -ENODEV; | |
378 | ||
d9f51b51 BS |
379 | /* match device ID in ath3k blacklist table */ |
380 | if (!id->driver_info) { | |
381 | const struct usb_device_id *match; | |
382 | match = usb_match_id(intf, ath3k_blist_tbl); | |
383 | if (match) | |
384 | id = match; | |
385 | } | |
386 | ||
387 | /* load patch and sysconfig files for AR3012 */ | |
388 | if (id->driver_info & BTUSB_ATH3012) { | |
2d25f8b4 SL |
389 | |
390 | /* New firmware with patch and sysconfig files already loaded */ | |
391 | if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x0001) | |
392 | return -ENODEV; | |
393 | ||
d9f51b51 BS |
394 | ret = ath3k_load_patch(udev); |
395 | if (ret < 0) { | |
396 | BT_ERR("Loading patch file failed"); | |
397 | return ret; | |
398 | } | |
399 | ret = ath3k_load_syscfg(udev); | |
400 | if (ret < 0) { | |
401 | BT_ERR("Loading sysconfig file failed"); | |
402 | return ret; | |
403 | } | |
404 | ret = ath3k_set_normal_mode(udev); | |
405 | if (ret < 0) { | |
406 | BT_ERR("Set normal mode failed"); | |
407 | return ret; | |
408 | } | |
409 | ath3k_switch_pid(udev); | |
410 | return 0; | |
411 | } | |
412 | ||
c3eae82a PF |
413 | ret = request_firmware(&firmware, ATH3K_FIRMWARE, &udev->dev); |
414 | if (ret < 0) { | |
415 | if (ret == -ENOENT) | |
416 | BT_ERR("Firmware file \"%s\" not found", | |
417 | ATH3K_FIRMWARE); | |
418 | else | |
419 | BT_ERR("Firmware file \"%s\" request failed (err=%d)", | |
420 | ATH3K_FIRMWARE, ret); | |
421 | return ret; | |
9670d80a VK |
422 | } |
423 | ||
84f0e17f | 424 | ret = ath3k_load_firmware(udev, firmware); |
86e09287 | 425 | release_firmware(firmware); |
9670d80a | 426 | |
84f0e17f | 427 | return ret; |
9670d80a VK |
428 | } |
429 | ||
430 | static void ath3k_disconnect(struct usb_interface *intf) | |
431 | { | |
9670d80a | 432 | BT_DBG("ath3k_disconnect intf %p", intf); |
9670d80a VK |
433 | } |
434 | ||
435 | static struct usb_driver ath3k_driver = { | |
436 | .name = "ath3k", | |
437 | .probe = ath3k_probe, | |
438 | .disconnect = ath3k_disconnect, | |
439 | .id_table = ath3k_table, | |
440 | }; | |
441 | ||
93f1508c | 442 | module_usb_driver(ath3k_driver); |
9670d80a VK |
443 | |
444 | MODULE_AUTHOR("Atheros Communications"); | |
445 | MODULE_DESCRIPTION("Atheros AR30xx firmware driver"); | |
446 | MODULE_VERSION(VERSION); | |
447 | MODULE_LICENSE("GPL"); | |
c3eae82a | 448 | MODULE_FIRMWARE(ATH3K_FIRMWARE); |