blob: ad591bd240ec3d5fed2fd2444a4d26313c76eb12 [file] [log] [blame]
Vikram Kandukuri9670d802010-01-06 19:04:15 +05301/*
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"
Paul Fertserc3eae822011-10-29 21:52:49 +040033#define ATH3K_FIRMWARE "ath3k-1.fw"
Vikram Kandukuri9670d802010-01-06 19:04:15 +053034
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053035#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
52struct 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};
Vikram Kandukuri9670d802010-01-06 19:04:15 +053059
60static struct usb_device_id ath3k_table[] = {
61 /* Atheros AR3011 */
62 { USB_DEVICE(0x0CF3, 0x3000) },
Bala Shanmugambe931122010-11-26 17:35:46 +053063
64 /* Atheros AR3011 with sflash firmware*/
65 { USB_DEVICE(0x0CF3, 0x3002) },
Andy Ross2a7bccc2011-05-09 16:11:16 -070066 { USB_DEVICE(0x13d3, 0x3304) },
Ricardo Mendoza8e7c3d22011-07-13 16:04:29 +010067 { USB_DEVICE(0x0930, 0x0215) },
Keng-Yu Lin6b6ba882011-11-30 18:32:37 +080068 { USB_DEVICE(0x0489, 0xE03D) },
Bala Shanmugambe931122010-11-26 17:35:46 +053069
Cho, Yu-Chen509e7862011-01-26 17:10:59 +080070 /* Atheros AR9285 Malbec with sflash firmware */
71 { USB_DEVICE(0x03F0, 0x311D) },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053072
73 /* Atheros AR3012 with sflash firmware*/
74 { USB_DEVICE(0x0CF3, 0x3004) },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +020075 { USB_DEVICE(0x0CF3, 0x311D) },
Eran9498ba72011-12-05 22:15:29 +000076 { USB_DEVICE(0x13d3, 0x3375) },
AceLan Kao55ed7d42012-03-28 10:25:36 +080077 { USB_DEVICE(0x04CA, 0x3005) },
AceLan Kao87522a42012-04-13 17:39:57 +080078 { USB_DEVICE(0x13d3, 0x3362) },
AceLan Kaoac713112012-04-19 14:53:45 +080079 { USB_DEVICE(0x0CF3, 0xE004) },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053080
Cho, Yu-Chene9036e32011-02-15 10:20:07 +080081 /* Atheros AR5BBU12 with sflash firmware */
82 { USB_DEVICE(0x0489, 0xE02C) },
John W. Linvilleb67afe72011-02-18 17:03:41 -050083
Michael Gruetzner85d59722012-05-02 22:33:40 +020084 /* Atheros AR5BBU22 with sflash firmware */
85 { USB_DEVICE(0x0489, 0xE03C) },
86
Vikram Kandukuri9670d802010-01-06 19:04:15 +053087 { } /* Terminating entry */
88};
89
90MODULE_DEVICE_TABLE(usb, ath3k_table);
91
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053092#define BTUSB_ATH3012 0x80
93/* This table is to load patch and sysconfig files
94 * for AR3012 */
95static struct usb_device_id ath3k_blist_tbl[] = {
96
97 /* Atheros AR3012 with sflash firmware*/
98 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +020099 { USB_DEVICE(0x0cf3, 0x311D), .driver_info = BTUSB_ATH3012 },
Eran9498ba72011-12-05 22:15:29 +0000100 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kao55ed7d42012-03-28 10:25:36 +0800101 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
AceLan Kao87522a42012-04-13 17:39:57 +0800102 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800103 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530104
Michael Gruetzner85d59722012-05-02 22:33:40 +0200105 /* Atheros AR5BBU22 with sflash firmware */
106 { USB_DEVICE(0x0489, 0xE03C), .driver_info = BTUSB_ATH3012 },
107
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530108 { } /* Terminating entry */
109};
110
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530111#define USB_REQ_DFU_DNLOAD 1
112#define BULK_SIZE 4096
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530113#define FW_HDR_SIZE 20
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530114
Alexander Holler86e09282010-11-22 21:09:01 +0100115static int ath3k_load_firmware(struct usb_device *udev,
116 const struct firmware *firmware)
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530117{
118 u8 *send_buf;
119 int err, pipe, len, size, sent = 0;
Alexander Holler86e09282010-11-22 21:09:01 +0100120 int count = firmware->size;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530121
Alexander Holler86e09282010-11-22 21:09:01 +0100122 BT_DBG("udev %p", udev);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530123
Alexander Holler86e09282010-11-22 21:09:01 +0100124 pipe = usb_sndctrlpipe(udev, 0);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530125
David Herrmann52a10202011-10-25 12:09:52 +0200126 send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530127 if (!send_buf) {
128 BT_ERR("Can't allocate memory chunk for firmware");
129 return -ENOMEM;
130 }
131
Alexander Holler86e09282010-11-22 21:09:01 +0100132 memcpy(send_buf, firmware->data, 20);
133 if ((err = usb_control_msg(udev, pipe,
134 USB_REQ_DFU_DNLOAD,
135 USB_TYPE_VENDOR, 0, 0,
136 send_buf, 20, USB_CTRL_SET_TIMEOUT)) < 0) {
137 BT_ERR("Can't change to loading configuration err");
138 goto error;
139 }
140 sent += 20;
141 count -= 20;
142
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530143 while (count) {
144 size = min_t(uint, count, BULK_SIZE);
Alexander Holler86e09282010-11-22 21:09:01 +0100145 pipe = usb_sndbulkpipe(udev, 0x02);
146 memcpy(send_buf, firmware->data + sent, size);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530147
Alexander Holler86e09282010-11-22 21:09:01 +0100148 err = usb_bulk_msg(udev, pipe, send_buf, size,
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530149 &len, 3000);
150
151 if (err || (len != size)) {
152 BT_ERR("Error in firmware loading err = %d,"
153 "len = %d, size = %d", err, len, size);
154 goto error;
155 }
156
157 sent += size;
158 count -= size;
159 }
160
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530161error:
162 kfree(send_buf);
163 return err;
164}
165
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530166static int ath3k_get_state(struct usb_device *udev, unsigned char *state)
167{
168 int pipe = 0;
169
170 pipe = usb_rcvctrlpipe(udev, 0);
171 return usb_control_msg(udev, pipe, ATH3K_GETSTATE,
172 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
173 state, 0x01, USB_CTRL_SET_TIMEOUT);
174}
175
176static int ath3k_get_version(struct usb_device *udev,
177 struct ath3k_version *version)
178{
179 int pipe = 0;
180
181 pipe = usb_rcvctrlpipe(udev, 0);
182 return usb_control_msg(udev, pipe, ATH3K_GETVERSION,
183 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, version,
184 sizeof(struct ath3k_version),
185 USB_CTRL_SET_TIMEOUT);
186}
187
188static int ath3k_load_fwfile(struct usb_device *udev,
189 const struct firmware *firmware)
190{
191 u8 *send_buf;
192 int err, pipe, len, size, count, sent = 0;
193 int ret;
194
195 count = firmware->size;
196
David Herrmann52a10202011-10-25 12:09:52 +0200197 send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530198 if (!send_buf) {
199 BT_ERR("Can't allocate memory chunk for firmware");
200 return -ENOMEM;
201 }
202
203 size = min_t(uint, count, FW_HDR_SIZE);
204 memcpy(send_buf, firmware->data, size);
205
206 pipe = usb_sndctrlpipe(udev, 0);
207 ret = usb_control_msg(udev, pipe, ATH3K_DNLOAD,
208 USB_TYPE_VENDOR, 0, 0, send_buf,
209 size, USB_CTRL_SET_TIMEOUT);
210 if (ret < 0) {
211 BT_ERR("Can't change to loading configuration err");
212 kfree(send_buf);
213 return ret;
214 }
215
216 sent += size;
217 count -= size;
218
219 while (count) {
220 size = min_t(uint, count, BULK_SIZE);
221 pipe = usb_sndbulkpipe(udev, 0x02);
222
223 memcpy(send_buf, firmware->data + sent, size);
224
225 err = usb_bulk_msg(udev, pipe, send_buf, size,
226 &len, 3000);
227 if (err || (len != size)) {
228 BT_ERR("Error in firmware loading err = %d,"
229 "len = %d, size = %d", err, len, size);
230 kfree(send_buf);
231 return err;
232 }
233 sent += size;
234 count -= size;
235 }
236
237 kfree(send_buf);
238 return 0;
239}
240
241static int ath3k_switch_pid(struct usb_device *udev)
242{
243 int pipe = 0;
244
245 pipe = usb_sndctrlpipe(udev, 0);
246 return usb_control_msg(udev, pipe, USB_REG_SWITCH_VID_PID,
247 USB_TYPE_VENDOR, 0, 0,
248 NULL, 0, USB_CTRL_SET_TIMEOUT);
249}
250
251static int ath3k_set_normal_mode(struct usb_device *udev)
252{
253 unsigned char fw_state;
254 int pipe = 0, ret;
255
256 ret = ath3k_get_state(udev, &fw_state);
257 if (ret < 0) {
258 BT_ERR("Can't get state to change to normal mode err");
259 return ret;
260 }
261
262 if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
263 BT_DBG("firmware was already in normal mode");
264 return 0;
265 }
266
267 pipe = usb_sndctrlpipe(udev, 0);
268 return usb_control_msg(udev, pipe, ATH3K_SET_NORMAL_MODE,
269 USB_TYPE_VENDOR, 0, 0,
270 NULL, 0, USB_CTRL_SET_TIMEOUT);
271}
272
273static int ath3k_load_patch(struct usb_device *udev)
274{
275 unsigned char fw_state;
276 char filename[ATH3K_NAME_LEN] = {0};
277 const struct firmware *firmware;
278 struct ath3k_version fw_version, pt_version;
279 int ret;
280
281 ret = ath3k_get_state(udev, &fw_state);
282 if (ret < 0) {
283 BT_ERR("Can't get state to change to load ram patch err");
284 return ret;
285 }
286
287 if (fw_state & ATH3K_PATCH_UPDATE) {
288 BT_DBG("Patch was already downloaded");
289 return 0;
290 }
291
292 ret = ath3k_get_version(udev, &fw_version);
293 if (ret < 0) {
294 BT_ERR("Can't get version to change to load ram patch err");
295 return ret;
296 }
297
298 snprintf(filename, ATH3K_NAME_LEN, "ar3k/AthrBT_0x%08x.dfu",
299 fw_version.rom_version);
300
301 ret = request_firmware(&firmware, filename, &udev->dev);
302 if (ret < 0) {
303 BT_ERR("Patch file not found %s", filename);
304 return ret;
305 }
306
307 pt_version.rom_version = *(int *)(firmware->data + firmware->size - 8);
308 pt_version.build_version = *(int *)
309 (firmware->data + firmware->size - 4);
310
311 if ((pt_version.rom_version != fw_version.rom_version) ||
312 (pt_version.build_version <= fw_version.build_version)) {
313 BT_ERR("Patch file version did not match with firmware");
314 release_firmware(firmware);
315 return -EINVAL;
316 }
317
318 ret = ath3k_load_fwfile(udev, firmware);
319 release_firmware(firmware);
320
321 return ret;
322}
323
324static int ath3k_load_syscfg(struct usb_device *udev)
325{
326 unsigned char fw_state;
327 char filename[ATH3K_NAME_LEN] = {0};
328 const struct firmware *firmware;
329 struct ath3k_version fw_version;
330 int clk_value, ret;
331
332 ret = ath3k_get_state(udev, &fw_state);
333 if (ret < 0) {
334 BT_ERR("Can't get state to change to load configration err");
335 return -EBUSY;
336 }
337
338 ret = ath3k_get_version(udev, &fw_version);
339 if (ret < 0) {
340 BT_ERR("Can't get version to change to load ram patch err");
341 return ret;
342 }
343
344 switch (fw_version.ref_clock) {
345
346 case ATH3K_XTAL_FREQ_26M:
347 clk_value = 26;
348 break;
349 case ATH3K_XTAL_FREQ_40M:
350 clk_value = 40;
351 break;
352 case ATH3K_XTAL_FREQ_19P2:
353 clk_value = 19;
354 break;
355 default:
356 clk_value = 0;
357 break;
358 }
359
360 snprintf(filename, ATH3K_NAME_LEN, "ar3k/ramps_0x%08x_%d%s",
361 fw_version.rom_version, clk_value, ".dfu");
362
363 ret = request_firmware(&firmware, filename, &udev->dev);
364 if (ret < 0) {
365 BT_ERR("Configuration file not found %s", filename);
366 return ret;
367 }
368
369 ret = ath3k_load_fwfile(udev, firmware);
370 release_firmware(firmware);
371
372 return ret;
373}
374
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530375static int ath3k_probe(struct usb_interface *intf,
376 const struct usb_device_id *id)
377{
378 const struct firmware *firmware;
379 struct usb_device *udev = interface_to_usbdev(intf);
Rogério Brito84f0e172011-02-03 01:42:05 -0200380 int ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530381
382 BT_DBG("intf %p id %p", intf, id);
383
384 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
385 return -ENODEV;
386
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530387 /* match device ID in ath3k blacklist table */
388 if (!id->driver_info) {
389 const struct usb_device_id *match;
390 match = usb_match_id(intf, ath3k_blist_tbl);
391 if (match)
392 id = match;
393 }
394
395 /* load patch and sysconfig files for AR3012 */
396 if (id->driver_info & BTUSB_ATH3012) {
Steven.Li2d25f8b2011-07-01 14:02:36 +0800397
398 /* New firmware with patch and sysconfig files already loaded */
399 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x0001)
400 return -ENODEV;
401
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530402 ret = ath3k_load_patch(udev);
403 if (ret < 0) {
404 BT_ERR("Loading patch file failed");
405 return ret;
406 }
407 ret = ath3k_load_syscfg(udev);
408 if (ret < 0) {
409 BT_ERR("Loading sysconfig file failed");
410 return ret;
411 }
412 ret = ath3k_set_normal_mode(udev);
413 if (ret < 0) {
414 BT_ERR("Set normal mode failed");
415 return ret;
416 }
417 ath3k_switch_pid(udev);
418 return 0;
419 }
420
Paul Fertserc3eae822011-10-29 21:52:49 +0400421 ret = request_firmware(&firmware, ATH3K_FIRMWARE, &udev->dev);
422 if (ret < 0) {
423 if (ret == -ENOENT)
424 BT_ERR("Firmware file \"%s\" not found",
425 ATH3K_FIRMWARE);
426 else
427 BT_ERR("Firmware file \"%s\" request failed (err=%d)",
428 ATH3K_FIRMWARE, ret);
429 return ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530430 }
431
Rogério Brito84f0e172011-02-03 01:42:05 -0200432 ret = ath3k_load_firmware(udev, firmware);
Alexander Holler86e09282010-11-22 21:09:01 +0100433 release_firmware(firmware);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530434
Rogério Brito84f0e172011-02-03 01:42:05 -0200435 return ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530436}
437
438static void ath3k_disconnect(struct usb_interface *intf)
439{
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530440 BT_DBG("ath3k_disconnect intf %p", intf);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530441}
442
443static struct usb_driver ath3k_driver = {
444 .name = "ath3k",
445 .probe = ath3k_probe,
446 .disconnect = ath3k_disconnect,
447 .id_table = ath3k_table,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700448 .disable_hub_initiated_lpm = 1,
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530449};
450
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -0800451module_usb_driver(ath3k_driver);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530452
453MODULE_AUTHOR("Atheros Communications");
454MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
455MODULE_VERSION(VERSION);
456MODULE_LICENSE("GPL");
Paul Fertserc3eae822011-10-29 21:52:49 +0400457MODULE_FIRMWARE(ATH3K_FIRMWARE);