blob: 4d4a6044cadead7074e68763605598e2ab1ee1b3 [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) },
Marcos Chaparro2145cea2012-11-06 16:19:11 -030069 { USB_DEVICE(0x0489, 0xE027) },
Bala Shanmugambe931122010-11-26 17:35:46 +053070
Cho, Yu-Chen509e7862011-01-26 17:10:59 +080071 /* Atheros AR9285 Malbec with sflash firmware */
72 { USB_DEVICE(0x03F0, 0x311D) },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053073
74 /* Atheros AR3012 with sflash firmware*/
Ming Lei42c4b742013-03-15 11:00:39 +080075 { USB_DEVICE(0x0CF3, 0x0036) },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053076 { USB_DEVICE(0x0CF3, 0x3004) },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +020077 { USB_DEVICE(0x0CF3, 0x311D) },
Eran9498ba72011-12-05 22:15:29 +000078 { USB_DEVICE(0x13d3, 0x3375) },
AceLan Kao55ed7d42012-03-28 10:25:36 +080079 { USB_DEVICE(0x04CA, 0x3005) },
AceLan Kao87522a42012-04-13 17:39:57 +080080 { USB_DEVICE(0x13d3, 0x3362) },
AceLan Kaoac713112012-04-19 14:53:45 +080081 { USB_DEVICE(0x0CF3, 0xE004) },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053082
Cho, Yu-Chene9036e32011-02-15 10:20:07 +080083 /* Atheros AR5BBU12 with sflash firmware */
84 { USB_DEVICE(0x0489, 0xE02C) },
John W. Linvilleb67afe72011-02-18 17:03:41 -050085
Vikram Kandukuri9670d802010-01-06 19:04:15 +053086 { } /* Terminating entry */
87};
88
89MODULE_DEVICE_TABLE(usb, ath3k_table);
90
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053091#define BTUSB_ATH3012 0x80
92/* This table is to load patch and sysconfig files
93 * for AR3012 */
94static struct usb_device_id ath3k_blist_tbl[] = {
95
96 /* Atheros AR3012 with sflash firmware*/
Ming Lei42c4b742013-03-15 11:00:39 +080097 { USB_DEVICE(0x0CF3, 0x0036), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053098 { 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
105 { } /* Terminating entry */
106};
107
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530108#define USB_REQ_DFU_DNLOAD 1
109#define BULK_SIZE 4096
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530110#define FW_HDR_SIZE 20
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530111
Alexander Holler86e09282010-11-22 21:09:01 +0100112static int ath3k_load_firmware(struct usb_device *udev,
113 const struct firmware *firmware)
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530114{
115 u8 *send_buf;
116 int err, pipe, len, size, sent = 0;
Alexander Holler86e09282010-11-22 21:09:01 +0100117 int count = firmware->size;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530118
Alexander Holler86e09282010-11-22 21:09:01 +0100119 BT_DBG("udev %p", udev);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530120
Alexander Holler86e09282010-11-22 21:09:01 +0100121 pipe = usb_sndctrlpipe(udev, 0);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530122
David Herrmann52a10202011-10-25 12:09:52 +0200123 send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530124 if (!send_buf) {
125 BT_ERR("Can't allocate memory chunk for firmware");
126 return -ENOMEM;
127 }
128
Alexander Holler86e09282010-11-22 21:09:01 +0100129 memcpy(send_buf, firmware->data, 20);
130 if ((err = usb_control_msg(udev, pipe,
131 USB_REQ_DFU_DNLOAD,
132 USB_TYPE_VENDOR, 0, 0,
133 send_buf, 20, USB_CTRL_SET_TIMEOUT)) < 0) {
134 BT_ERR("Can't change to loading configuration err");
135 goto error;
136 }
137 sent += 20;
138 count -= 20;
139
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530140 while (count) {
141 size = min_t(uint, count, BULK_SIZE);
Alexander Holler86e09282010-11-22 21:09:01 +0100142 pipe = usb_sndbulkpipe(udev, 0x02);
143 memcpy(send_buf, firmware->data + sent, size);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530144
Alexander Holler86e09282010-11-22 21:09:01 +0100145 err = usb_bulk_msg(udev, pipe, send_buf, size,
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530146 &len, 3000);
147
148 if (err || (len != size)) {
149 BT_ERR("Error in firmware loading err = %d,"
150 "len = %d, size = %d", err, len, size);
151 goto error;
152 }
153
154 sent += size;
155 count -= size;
156 }
157
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530158error:
159 kfree(send_buf);
160 return err;
161}
162
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530163static int ath3k_get_state(struct usb_device *udev, unsigned char *state)
164{
165 int pipe = 0;
166
167 pipe = usb_rcvctrlpipe(udev, 0);
168 return usb_control_msg(udev, pipe, ATH3K_GETSTATE,
169 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
170 state, 0x01, USB_CTRL_SET_TIMEOUT);
171}
172
173static int ath3k_get_version(struct usb_device *udev,
174 struct ath3k_version *version)
175{
176 int pipe = 0;
177
178 pipe = usb_rcvctrlpipe(udev, 0);
179 return usb_control_msg(udev, pipe, ATH3K_GETVERSION,
180 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, version,
181 sizeof(struct ath3k_version),
182 USB_CTRL_SET_TIMEOUT);
183}
184
185static int ath3k_load_fwfile(struct usb_device *udev,
186 const struct firmware *firmware)
187{
188 u8 *send_buf;
189 int err, pipe, len, size, count, sent = 0;
190 int ret;
191
192 count = firmware->size;
193
David Herrmann52a10202011-10-25 12:09:52 +0200194 send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530195 if (!send_buf) {
196 BT_ERR("Can't allocate memory chunk for firmware");
197 return -ENOMEM;
198 }
199
200 size = min_t(uint, count, FW_HDR_SIZE);
201 memcpy(send_buf, firmware->data, size);
202
203 pipe = usb_sndctrlpipe(udev, 0);
204 ret = usb_control_msg(udev, pipe, ATH3K_DNLOAD,
205 USB_TYPE_VENDOR, 0, 0, send_buf,
206 size, USB_CTRL_SET_TIMEOUT);
207 if (ret < 0) {
208 BT_ERR("Can't change to loading configuration err");
209 kfree(send_buf);
210 return ret;
211 }
212
213 sent += size;
214 count -= size;
215
216 while (count) {
217 size = min_t(uint, count, BULK_SIZE);
218 pipe = usb_sndbulkpipe(udev, 0x02);
219
220 memcpy(send_buf, firmware->data + sent, size);
221
222 err = usb_bulk_msg(udev, pipe, send_buf, size,
223 &len, 3000);
224 if (err || (len != size)) {
225 BT_ERR("Error in firmware loading err = %d,"
226 "len = %d, size = %d", err, len, size);
227 kfree(send_buf);
228 return err;
229 }
230 sent += size;
231 count -= size;
232 }
233
234 kfree(send_buf);
235 return 0;
236}
237
238static int ath3k_switch_pid(struct usb_device *udev)
239{
240 int pipe = 0;
241
242 pipe = usb_sndctrlpipe(udev, 0);
243 return usb_control_msg(udev, pipe, USB_REG_SWITCH_VID_PID,
244 USB_TYPE_VENDOR, 0, 0,
245 NULL, 0, USB_CTRL_SET_TIMEOUT);
246}
247
248static int ath3k_set_normal_mode(struct usb_device *udev)
249{
250 unsigned char fw_state;
251 int pipe = 0, ret;
252
253 ret = ath3k_get_state(udev, &fw_state);
254 if (ret < 0) {
255 BT_ERR("Can't get state to change to normal mode err");
256 return ret;
257 }
258
259 if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
260 BT_DBG("firmware was already in normal mode");
261 return 0;
262 }
263
264 pipe = usb_sndctrlpipe(udev, 0);
265 return usb_control_msg(udev, pipe, ATH3K_SET_NORMAL_MODE,
266 USB_TYPE_VENDOR, 0, 0,
267 NULL, 0, USB_CTRL_SET_TIMEOUT);
268}
269
270static int ath3k_load_patch(struct usb_device *udev)
271{
272 unsigned char fw_state;
273 char filename[ATH3K_NAME_LEN] = {0};
274 const struct firmware *firmware;
275 struct ath3k_version fw_version, pt_version;
276 int ret;
277
278 ret = ath3k_get_state(udev, &fw_state);
279 if (ret < 0) {
280 BT_ERR("Can't get state to change to load ram patch err");
281 return ret;
282 }
283
284 if (fw_state & ATH3K_PATCH_UPDATE) {
285 BT_DBG("Patch was already downloaded");
286 return 0;
287 }
288
289 ret = ath3k_get_version(udev, &fw_version);
290 if (ret < 0) {
291 BT_ERR("Can't get version to change to load ram patch err");
292 return ret;
293 }
294
295 snprintf(filename, ATH3K_NAME_LEN, "ar3k/AthrBT_0x%08x.dfu",
296 fw_version.rom_version);
297
298 ret = request_firmware(&firmware, filename, &udev->dev);
299 if (ret < 0) {
300 BT_ERR("Patch file not found %s", filename);
301 return ret;
302 }
303
304 pt_version.rom_version = *(int *)(firmware->data + firmware->size - 8);
305 pt_version.build_version = *(int *)
306 (firmware->data + firmware->size - 4);
307
308 if ((pt_version.rom_version != fw_version.rom_version) ||
309 (pt_version.build_version <= fw_version.build_version)) {
310 BT_ERR("Patch file version did not match with firmware");
311 release_firmware(firmware);
312 return -EINVAL;
313 }
314
315 ret = ath3k_load_fwfile(udev, firmware);
316 release_firmware(firmware);
317
318 return ret;
319}
320
321static int ath3k_load_syscfg(struct usb_device *udev)
322{
323 unsigned char fw_state;
324 char filename[ATH3K_NAME_LEN] = {0};
325 const struct firmware *firmware;
326 struct ath3k_version fw_version;
327 int clk_value, ret;
328
329 ret = ath3k_get_state(udev, &fw_state);
330 if (ret < 0) {
331 BT_ERR("Can't get state to change to load configration err");
332 return -EBUSY;
333 }
334
335 ret = ath3k_get_version(udev, &fw_version);
336 if (ret < 0) {
337 BT_ERR("Can't get version to change to load ram patch err");
338 return ret;
339 }
340
341 switch (fw_version.ref_clock) {
342
343 case ATH3K_XTAL_FREQ_26M:
344 clk_value = 26;
345 break;
346 case ATH3K_XTAL_FREQ_40M:
347 clk_value = 40;
348 break;
349 case ATH3K_XTAL_FREQ_19P2:
350 clk_value = 19;
351 break;
352 default:
353 clk_value = 0;
354 break;
355 }
356
357 snprintf(filename, ATH3K_NAME_LEN, "ar3k/ramps_0x%08x_%d%s",
358 fw_version.rom_version, clk_value, ".dfu");
359
360 ret = request_firmware(&firmware, filename, &udev->dev);
361 if (ret < 0) {
362 BT_ERR("Configuration file not found %s", filename);
363 return ret;
364 }
365
366 ret = ath3k_load_fwfile(udev, firmware);
367 release_firmware(firmware);
368
369 return ret;
370}
371
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530372static int ath3k_probe(struct usb_interface *intf,
373 const struct usb_device_id *id)
374{
375 const struct firmware *firmware;
376 struct usb_device *udev = interface_to_usbdev(intf);
Rogério Brito84f0e172011-02-03 01:42:05 -0200377 int ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530378
379 BT_DBG("intf %p id %p", intf, id);
380
381 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
382 return -ENODEV;
383
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530384 /* match device ID in ath3k blacklist table */
385 if (!id->driver_info) {
386 const struct usb_device_id *match;
387 match = usb_match_id(intf, ath3k_blist_tbl);
388 if (match)
389 id = match;
390 }
391
392 /* load patch and sysconfig files for AR3012 */
393 if (id->driver_info & BTUSB_ATH3012) {
Steven.Li2d25f8b2011-07-01 14:02:36 +0800394
395 /* New firmware with patch and sysconfig files already loaded */
396 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x0001)
397 return -ENODEV;
398
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530399 ret = ath3k_load_patch(udev);
400 if (ret < 0) {
401 BT_ERR("Loading patch file failed");
402 return ret;
403 }
404 ret = ath3k_load_syscfg(udev);
405 if (ret < 0) {
406 BT_ERR("Loading sysconfig file failed");
407 return ret;
408 }
409 ret = ath3k_set_normal_mode(udev);
410 if (ret < 0) {
411 BT_ERR("Set normal mode failed");
412 return ret;
413 }
414 ath3k_switch_pid(udev);
415 return 0;
416 }
417
Paul Fertserc3eae822011-10-29 21:52:49 +0400418 ret = request_firmware(&firmware, ATH3K_FIRMWARE, &udev->dev);
419 if (ret < 0) {
420 if (ret == -ENOENT)
421 BT_ERR("Firmware file \"%s\" not found",
422 ATH3K_FIRMWARE);
423 else
424 BT_ERR("Firmware file \"%s\" request failed (err=%d)",
425 ATH3K_FIRMWARE, ret);
426 return ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530427 }
428
Rogério Brito84f0e172011-02-03 01:42:05 -0200429 ret = ath3k_load_firmware(udev, firmware);
Alexander Holler86e09282010-11-22 21:09:01 +0100430 release_firmware(firmware);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530431
Rogério Brito84f0e172011-02-03 01:42:05 -0200432 return ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530433}
434
435static void ath3k_disconnect(struct usb_interface *intf)
436{
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530437 BT_DBG("ath3k_disconnect intf %p", intf);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530438}
439
440static struct usb_driver ath3k_driver = {
441 .name = "ath3k",
442 .probe = ath3k_probe,
443 .disconnect = ath3k_disconnect,
444 .id_table = ath3k_table,
445};
446
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -0800447module_usb_driver(ath3k_driver);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530448
449MODULE_AUTHOR("Atheros Communications");
450MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
451MODULE_VERSION(VERSION);
452MODULE_LICENSE("GPL");
Paul Fertserc3eae822011-10-29 21:52:49 +0400453MODULE_FIRMWARE(ATH3K_FIRMWARE);