blob: 4ce2705136952f04e71fc4986e0aebcd7a980beb [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Vikram Kandukuri9670d802010-01-06 19:04:15 +05302/*
3 * Copyright (c) 2008-2009 Atheros Communications Inc.
Vikram Kandukuri9670d802010-01-06 19:04:15 +05304 */
5
6
7#include <linux/module.h>
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/slab.h>
11#include <linux/types.h>
12#include <linux/errno.h>
13#include <linux/device.h>
14#include <linux/firmware.h>
15#include <linux/usb.h>
Marcel Holtmann72dd2b22014-07-20 17:29:59 +020016#include <asm/unaligned.h>
Vikram Kandukuri9670d802010-01-06 19:04:15 +053017#include <net/bluetooth/bluetooth.h>
18
19#define VERSION "1.0"
Paul Fertserc3eae822011-10-29 21:52:49 +040020#define ATH3K_FIRMWARE "ath3k-1.fw"
Vikram Kandukuri9670d802010-01-06 19:04:15 +053021
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053022#define ATH3K_DNLOAD 0x01
23#define ATH3K_GETSTATE 0x05
24#define ATH3K_SET_NORMAL_MODE 0x07
25#define ATH3K_GETVERSION 0x09
26#define USB_REG_SWITCH_VID_PID 0x0a
27
28#define ATH3K_MODE_MASK 0x3F
29#define ATH3K_NORMAL_MODE 0x0E
30
31#define ATH3K_PATCH_UPDATE 0x80
32#define ATH3K_SYSCFG_UPDATE 0x40
33
34#define ATH3K_XTAL_FREQ_26M 0x00
35#define ATH3K_XTAL_FREQ_40M 0x01
36#define ATH3K_XTAL_FREQ_19P2 0x02
37#define ATH3K_NAME_LEN 0xFF
38
39struct ath3k_version {
Marcel Holtmann72dd2b22014-07-20 17:29:59 +020040 __le32 rom_version;
41 __le32 build_version;
42 __le32 ram_version;
43 __u8 ref_clock;
44 __u8 reserved[7];
45} __packed;
Vikram Kandukuri9670d802010-01-06 19:04:15 +053046
Marcel Holtmann3932eb32013-10-11 07:46:21 -070047static const struct usb_device_id ath3k_table[] = {
Vikram Kandukuri9670d802010-01-06 19:04:15 +053048 /* Atheros AR3011 */
49 { USB_DEVICE(0x0CF3, 0x3000) },
Bala Shanmugambe931122010-11-26 17:35:46 +053050
51 /* Atheros AR3011 with sflash firmware*/
Andy Shevchenko0b880062014-02-18 18:26:19 +020052 { USB_DEVICE(0x0489, 0xE027) },
53 { USB_DEVICE(0x0489, 0xE03D) },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +020054 { USB_DEVICE(0x04F2, 0xAFF1) },
Andy Shevchenko0b880062014-02-18 18:26:19 +020055 { USB_DEVICE(0x0930, 0x0215) },
Bala Shanmugambe931122010-11-26 17:35:46 +053056 { USB_DEVICE(0x0CF3, 0x3002) },
Marek Vasut6eda5412012-06-08 14:32:50 +020057 { USB_DEVICE(0x0CF3, 0xE019) },
Andy Ross2a7bccc2011-05-09 16:11:16 -070058 { USB_DEVICE(0x13d3, 0x3304) },
Bala Shanmugambe931122010-11-26 17:35:46 +053059
Cho, Yu-Chen509e7862011-01-26 17:10:59 +080060 /* Atheros AR9285 Malbec with sflash firmware */
61 { USB_DEVICE(0x03F0, 0x311D) },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053062
63 /* Atheros AR3012 with sflash firmware*/
Andy Shevchenko0b880062014-02-18 18:26:19 +020064 { USB_DEVICE(0x0489, 0xe04d) },
65 { USB_DEVICE(0x0489, 0xe04e) },
66 { USB_DEVICE(0x0489, 0xe057) },
67 { USB_DEVICE(0x0489, 0xe056) },
68 { USB_DEVICE(0x0489, 0xe05f) },
Dmitry Tunin692c0622015-06-06 20:25:40 +030069 { USB_DEVICE(0x0489, 0xe076) },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +053070 { USB_DEVICE(0x0489, 0xe078) },
Dmitry Tunin28c971d2016-02-10 00:49:11 +030071 { USB_DEVICE(0x0489, 0xe095) },
Andy Shevchenko0b880062014-02-18 18:26:19 +020072 { USB_DEVICE(0x04c5, 0x1330) },
73 { USB_DEVICE(0x04CA, 0x3004) },
74 { USB_DEVICE(0x04CA, 0x3005) },
75 { USB_DEVICE(0x04CA, 0x3006) },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -070076 { USB_DEVICE(0x04CA, 0x3007) },
Andy Shevchenko0b880062014-02-18 18:26:19 +020077 { USB_DEVICE(0x04CA, 0x3008) },
78 { USB_DEVICE(0x04CA, 0x300b) },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +030079 { USB_DEVICE(0x04CA, 0x300d) },
Dmitry Tuninec0810d2015-05-02 13:36:58 +030080 { USB_DEVICE(0x04CA, 0x300f) },
Janne Heikkinen134d3b32014-12-09 07:44:51 +020081 { USB_DEVICE(0x04CA, 0x3010) },
Dmitry Tunin81d90442016-02-28 11:04:06 +030082 { USB_DEVICE(0x04CA, 0x3014) },
Dmitry Tunin441ad622017-01-05 13:19:53 +030083 { USB_DEVICE(0x04CA, 0x3018) },
Andy Shevchenko0b880062014-02-18 18:26:19 +020084 { USB_DEVICE(0x0930, 0x0219) },
Dmitry Tunincd355ff2015-10-05 19:29:33 +030085 { USB_DEVICE(0x0930, 0x021c) },
Andy Shevchenko0b880062014-02-18 18:26:19 +020086 { USB_DEVICE(0x0930, 0x0220) },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +010087 { USB_DEVICE(0x0930, 0x0227) },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +020088 { USB_DEVICE(0x0b05, 0x17d0) },
Ming Leid66629c2013-03-15 11:00:39 +080089 { USB_DEVICE(0x0CF3, 0x0036) },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +053090 { USB_DEVICE(0x0CF3, 0x3004) },
Sunguk Lee94a32d102013-03-12 04:41:58 +090091 { USB_DEVICE(0x0CF3, 0x3008) },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +020092 { USB_DEVICE(0x0CF3, 0x311D) },
Oliver Neukumb1312372014-01-16 15:37:11 +010093 { USB_DEVICE(0x0CF3, 0x311E) },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +010094 { USB_DEVICE(0x0CF3, 0x311F) },
Andy Shevchenko0b880062014-02-18 18:26:19 +020095 { USB_DEVICE(0x0cf3, 0x3121) },
Ming Leiebaf5792013-03-18 23:45:11 +080096 { USB_DEVICE(0x0CF3, 0x817a) },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +030097 { USB_DEVICE(0x0CF3, 0x817b) },
Andy Shevchenko0b880062014-02-18 18:26:19 +020098 { USB_DEVICE(0x0cf3, 0xe003) },
AceLan Kaoac713112012-04-19 14:53:45 +080099 { USB_DEVICE(0x0CF3, 0xE004) },
Peng Chen0a3658c2013-08-30 17:41:40 +0800100 { USB_DEVICE(0x0CF3, 0xE005) },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800101 { USB_DEVICE(0x0CF3, 0xE006) },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200102 { USB_DEVICE(0x13d3, 0x3362) },
103 { USB_DEVICE(0x13d3, 0x3375) },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800104 { USB_DEVICE(0x13d3, 0x3393) },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300105 { USB_DEVICE(0x13d3, 0x3395) },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530106 { USB_DEVICE(0x13d3, 0x3402) },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300107 { USB_DEVICE(0x13d3, 0x3408) },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300108 { USB_DEVICE(0x13d3, 0x3423) },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530109 { USB_DEVICE(0x13d3, 0x3432) },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300110 { USB_DEVICE(0x13d3, 0x3472) },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300111 { USB_DEVICE(0x13d3, 0x3474) },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300112 { USB_DEVICE(0x13d3, 0x3487) },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300113 { USB_DEVICE(0x13d3, 0x3490) },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530114
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800115 /* Atheros AR5BBU12 with sflash firmware */
116 { USB_DEVICE(0x0489, 0xE02C) },
John W. Linvilleb67afe72011-02-18 17:03:41 -0500117
Michael Gruetzner85d59722012-05-02 22:33:40 +0200118 /* Atheros AR5BBU22 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530119 { USB_DEVICE(0x0489, 0xE036) },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200120 { USB_DEVICE(0x0489, 0xE03C) },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200121
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530122 { } /* Terminating entry */
123};
124
125MODULE_DEVICE_TABLE(usb, ath3k_table);
126
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530127#define BTUSB_ATH3012 0x80
128/* This table is to load patch and sysconfig files
Derek Robsond98422c2017-07-22 13:47:07 +1200129 * for AR3012
130 */
Marcel Holtmann3932eb32013-10-11 07:46:21 -0700131static const struct usb_device_id ath3k_blist_tbl[] = {
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530132
133 /* Atheros AR3012 with sflash firmware*/
Andy Shevchenko0b880062014-02-18 18:26:19 +0200134 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
135 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
136 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
137 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
138 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300139 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530140 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300141 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200142 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
143 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
144 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
145 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700146 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200147 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
148 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300149 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300150 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200151 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300152 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300153 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200154 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300155 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200156 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100157 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200158 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800159 { USB_DEVICE(0x0CF3, 0x0036), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530160 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900161 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200162 { USB_DEVICE(0x0cf3, 0x311D), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100163 { USB_DEVICE(0x0cf3, 0x311E), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100164 { USB_DEVICE(0x0cf3, 0x311F), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200165 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800166 { USB_DEVICE(0x0CF3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300167 { USB_DEVICE(0x0CF3, 0x817b), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800168 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800169 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800170 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
AceLan Kao1d5b5692013-06-20 13:38:45 +0800171 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200172 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300175 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200176 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300177 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300178 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530179 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300180 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300181 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300182 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300183 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530184
Michael Gruetzner85d59722012-05-02 22:33:40 +0200185 /* Atheros AR5BBU22 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530186 { USB_DEVICE(0x0489, 0xE036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200187 { USB_DEVICE(0x0489, 0xE03C), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200188
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530189 { } /* Terminating entry */
190};
191
Luiz Carlos Ramosd6b372f2018-09-27 17:19:26 -0300192static inline void ath3k_log_failed_loading(int err, int len, int size,
193 int count)
Maxim Zhukov3d08f43c2018-02-05 00:09:46 +0300194{
Luiz Carlos Ramosd6b372f2018-09-27 17:19:26 -0300195 BT_ERR("Firmware loading err = %d, len = %d, size = %d, count = %d",
196 err, len, size, count);
Maxim Zhukov3d08f43c2018-02-05 00:09:46 +0300197}
198
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530199#define USB_REQ_DFU_DNLOAD 1
200#define BULK_SIZE 4096
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530201#define FW_HDR_SIZE 20
Adam Leec561a572015-01-28 15:30:27 -0500202#define TIMEGAP_USEC_MIN 50
203#define TIMEGAP_USEC_MAX 100
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530204
Alexander Holler86e09282010-11-22 21:09:01 +0100205static int ath3k_load_firmware(struct usb_device *udev,
206 const struct firmware *firmware)
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530207{
208 u8 *send_buf;
Dan Carpentere9fc7162016-04-22 13:02:55 +0300209 int len = 0;
210 int err, pipe, size, sent = 0;
Alexander Holler86e09282010-11-22 21:09:01 +0100211 int count = firmware->size;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530212
Alexander Holler86e09282010-11-22 21:09:01 +0100213 BT_DBG("udev %p", udev);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530214
Alexander Holler86e09282010-11-22 21:09:01 +0100215 pipe = usb_sndctrlpipe(udev, 0);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530216
David Herrmann52a10202011-10-25 12:09:52 +0200217 send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530218 if (!send_buf) {
219 BT_ERR("Can't allocate memory chunk for firmware");
220 return -ENOMEM;
221 }
222
Maxim Zhukov8b077bd2018-02-05 00:09:43 +0300223 memcpy(send_buf, firmware->data, FW_HDR_SIZE);
Valentin Iliea08b15e2013-08-12 18:46:00 +0300224 err = usb_control_msg(udev, pipe, USB_REQ_DFU_DNLOAD, USB_TYPE_VENDOR,
Maxim Zhukov8b077bd2018-02-05 00:09:43 +0300225 0, 0, send_buf, FW_HDR_SIZE,
226 USB_CTRL_SET_TIMEOUT);
Valentin Iliea08b15e2013-08-12 18:46:00 +0300227 if (err < 0) {
Alexander Holler86e09282010-11-22 21:09:01 +0100228 BT_ERR("Can't change to loading configuration err");
229 goto error;
230 }
Maxim Zhukov8b077bd2018-02-05 00:09:43 +0300231 sent += FW_HDR_SIZE;
232 count -= FW_HDR_SIZE;
Alexander Holler86e09282010-11-22 21:09:01 +0100233
Adam Lee3abb56d2014-05-27 13:49:07 +0800234 pipe = usb_sndbulkpipe(udev, 0x02);
235
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530236 while (count) {
Adam Leec561a572015-01-28 15:30:27 -0500237 /* workaround the compatibility issue with xHCI controller*/
238 usleep_range(TIMEGAP_USEC_MIN, TIMEGAP_USEC_MAX);
239
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530240 size = min_t(uint, count, BULK_SIZE);
Alexander Holler86e09282010-11-22 21:09:01 +0100241 memcpy(send_buf, firmware->data + sent, size);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530242
Alexander Holler86e09282010-11-22 21:09:01 +0100243 err = usb_bulk_msg(udev, pipe, send_buf, size,
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530244 &len, 3000);
245
246 if (err || (len != size)) {
Luiz Carlos Ramosd6b372f2018-09-27 17:19:26 -0300247 ath3k_log_failed_loading(err, len, size, count);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530248 goto error;
249 }
250
251 sent += size;
252 count -= size;
253 }
254
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530255error:
256 kfree(send_buf);
257 return err;
258}
259
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530260static int ath3k_get_state(struct usb_device *udev, unsigned char *state)
261{
Stanislaw Gruszka517828a2013-07-08 10:27:23 +0200262 int ret, pipe = 0;
263 char *buf;
264
265 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
266 if (!buf)
267 return -ENOMEM;
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530268
269 pipe = usb_rcvctrlpipe(udev, 0);
Stanislaw Gruszka517828a2013-07-08 10:27:23 +0200270 ret = usb_control_msg(udev, pipe, ATH3K_GETSTATE,
271 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
272 buf, sizeof(*buf), USB_CTRL_SET_TIMEOUT);
273
274 *state = *buf;
275 kfree(buf);
276
277 return ret;
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530278}
279
280static int ath3k_get_version(struct usb_device *udev,
281 struct ath3k_version *version)
282{
Stanislaw Gruszka517828a2013-07-08 10:27:23 +0200283 int ret, pipe = 0;
284 struct ath3k_version *buf;
285 const int size = sizeof(*buf);
286
287 buf = kmalloc(size, GFP_KERNEL);
288 if (!buf)
289 return -ENOMEM;
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530290
291 pipe = usb_rcvctrlpipe(udev, 0);
Stanislaw Gruszka517828a2013-07-08 10:27:23 +0200292 ret = usb_control_msg(udev, pipe, ATH3K_GETVERSION,
293 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
294 buf, size, USB_CTRL_SET_TIMEOUT);
295
296 memcpy(version, buf, size);
297 kfree(buf);
298
299 return ret;
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530300}
301
302static int ath3k_load_fwfile(struct usb_device *udev,
303 const struct firmware *firmware)
304{
305 u8 *send_buf;
Dan Carpentere9fc7162016-04-22 13:02:55 +0300306 int len = 0;
307 int err, pipe, size, count, sent = 0;
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530308 int ret;
309
310 count = firmware->size;
311
David Herrmann52a10202011-10-25 12:09:52 +0200312 send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530313 if (!send_buf) {
314 BT_ERR("Can't allocate memory chunk for firmware");
315 return -ENOMEM;
316 }
317
318 size = min_t(uint, count, FW_HDR_SIZE);
319 memcpy(send_buf, firmware->data, size);
320
321 pipe = usb_sndctrlpipe(udev, 0);
322 ret = usb_control_msg(udev, pipe, ATH3K_DNLOAD,
323 USB_TYPE_VENDOR, 0, 0, send_buf,
324 size, USB_CTRL_SET_TIMEOUT);
325 if (ret < 0) {
326 BT_ERR("Can't change to loading configuration err");
327 kfree(send_buf);
328 return ret;
329 }
330
331 sent += size;
332 count -= size;
333
Adam Leeb0c3e132014-06-05 21:47:44 +0800334 pipe = usb_sndbulkpipe(udev, 0x02);
335
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530336 while (count) {
Adam Leec561a572015-01-28 15:30:27 -0500337 /* workaround the compatibility issue with xHCI controller*/
338 usleep_range(TIMEGAP_USEC_MIN, TIMEGAP_USEC_MAX);
339
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530340 size = min_t(uint, count, BULK_SIZE);
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530341 memcpy(send_buf, firmware->data + sent, size);
342
343 err = usb_bulk_msg(udev, pipe, send_buf, size,
344 &len, 3000);
345 if (err || (len != size)) {
Luiz Carlos Ramosd6b372f2018-09-27 17:19:26 -0300346 ath3k_log_failed_loading(err, len, size, count);
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530347 kfree(send_buf);
348 return err;
349 }
350 sent += size;
351 count -= size;
352 }
353
354 kfree(send_buf);
355 return 0;
356}
357
358static int ath3k_switch_pid(struct usb_device *udev)
359{
360 int pipe = 0;
361
362 pipe = usb_sndctrlpipe(udev, 0);
363 return usb_control_msg(udev, pipe, USB_REG_SWITCH_VID_PID,
364 USB_TYPE_VENDOR, 0, 0,
365 NULL, 0, USB_CTRL_SET_TIMEOUT);
366}
367
368static int ath3k_set_normal_mode(struct usb_device *udev)
369{
370 unsigned char fw_state;
371 int pipe = 0, ret;
372
373 ret = ath3k_get_state(udev, &fw_state);
374 if (ret < 0) {
375 BT_ERR("Can't get state to change to normal mode err");
376 return ret;
377 }
378
379 if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
380 BT_DBG("firmware was already in normal mode");
381 return 0;
382 }
383
384 pipe = usb_sndctrlpipe(udev, 0);
385 return usb_control_msg(udev, pipe, ATH3K_SET_NORMAL_MODE,
386 USB_TYPE_VENDOR, 0, 0,
387 NULL, 0, USB_CTRL_SET_TIMEOUT);
388}
389
390static int ath3k_load_patch(struct usb_device *udev)
391{
392 unsigned char fw_state;
Maxim Zhukovb3baa242018-02-05 00:09:44 +0300393 char filename[ATH3K_NAME_LEN];
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530394 const struct firmware *firmware;
Marcel Holtmann72dd2b22014-07-20 17:29:59 +0200395 struct ath3k_version fw_version;
396 __u32 pt_rom_version, pt_build_version;
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530397 int ret;
398
399 ret = ath3k_get_state(udev, &fw_state);
400 if (ret < 0) {
401 BT_ERR("Can't get state to change to load ram patch err");
402 return ret;
403 }
404
405 if (fw_state & ATH3K_PATCH_UPDATE) {
406 BT_DBG("Patch was already downloaded");
407 return 0;
408 }
409
410 ret = ath3k_get_version(udev, &fw_version);
411 if (ret < 0) {
412 BT_ERR("Can't get version to change to load ram patch err");
413 return ret;
414 }
415
416 snprintf(filename, ATH3K_NAME_LEN, "ar3k/AthrBT_0x%08x.dfu",
Marcel Holtmann72dd2b22014-07-20 17:29:59 +0200417 le32_to_cpu(fw_version.rom_version));
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530418
419 ret = request_firmware(&firmware, filename, &udev->dev);
420 if (ret < 0) {
421 BT_ERR("Patch file not found %s", filename);
422 return ret;
423 }
424
Marcel Holtmann72dd2b22014-07-20 17:29:59 +0200425 pt_rom_version = get_unaligned_le32(firmware->data +
426 firmware->size - 8);
427 pt_build_version = get_unaligned_le32(firmware->data +
428 firmware->size - 4);
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530429
Marcel Holtmann72dd2b22014-07-20 17:29:59 +0200430 if (pt_rom_version != le32_to_cpu(fw_version.rom_version) ||
431 pt_build_version <= le32_to_cpu(fw_version.build_version)) {
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530432 BT_ERR("Patch file version did not match with firmware");
433 release_firmware(firmware);
434 return -EINVAL;
435 }
436
437 ret = ath3k_load_fwfile(udev, firmware);
438 release_firmware(firmware);
439
440 return ret;
441}
442
443static int ath3k_load_syscfg(struct usb_device *udev)
444{
445 unsigned char fw_state;
Maxim Zhukovb3baa242018-02-05 00:09:44 +0300446 char filename[ATH3K_NAME_LEN];
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530447 const struct firmware *firmware;
448 struct ath3k_version fw_version;
449 int clk_value, ret;
450
451 ret = ath3k_get_state(udev, &fw_state);
452 if (ret < 0) {
Masanari Iida8a168ca2012-12-29 02:00:09 +0900453 BT_ERR("Can't get state to change to load configuration err");
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530454 return -EBUSY;
455 }
456
457 ret = ath3k_get_version(udev, &fw_version);
458 if (ret < 0) {
459 BT_ERR("Can't get version to change to load ram patch err");
460 return ret;
461 }
462
463 switch (fw_version.ref_clock) {
464
465 case ATH3K_XTAL_FREQ_26M:
466 clk_value = 26;
467 break;
468 case ATH3K_XTAL_FREQ_40M:
469 clk_value = 40;
470 break;
471 case ATH3K_XTAL_FREQ_19P2:
472 clk_value = 19;
473 break;
474 default:
475 clk_value = 0;
476 break;
477 }
478
479 snprintf(filename, ATH3K_NAME_LEN, "ar3k/ramps_0x%08x_%d%s",
Peng Chenb9e25352012-09-06 19:30:43 +0800480 le32_to_cpu(fw_version.rom_version), clk_value, ".dfu");
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530481
482 ret = request_firmware(&firmware, filename, &udev->dev);
483 if (ret < 0) {
484 BT_ERR("Configuration file not found %s", filename);
485 return ret;
486 }
487
488 ret = ath3k_load_fwfile(udev, firmware);
489 release_firmware(firmware);
490
491 return ret;
492}
493
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530494static int ath3k_probe(struct usb_interface *intf,
495 const struct usb_device_id *id)
496{
497 const struct firmware *firmware;
498 struct usb_device *udev = interface_to_usbdev(intf);
Rogério Brito84f0e172011-02-03 01:42:05 -0200499 int ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530500
501 BT_DBG("intf %p id %p", intf, id);
502
503 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
504 return -ENODEV;
505
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530506 /* match device ID in ath3k blacklist table */
507 if (!id->driver_info) {
508 const struct usb_device_id *match;
Bhumika Goyald2ee9c22016-01-12 22:10:15 +0530509
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530510 match = usb_match_id(intf, ath3k_blist_tbl);
511 if (match)
512 id = match;
513 }
514
515 /* load patch and sysconfig files for AR3012 */
516 if (id->driver_info & BTUSB_ATH3012) {
Steven.Li2d25f8b2011-07-01 14:02:36 +0800517 /* New firmware with patch and sysconfig files already loaded */
518 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x0001)
519 return -ENODEV;
520
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530521 ret = ath3k_load_patch(udev);
522 if (ret < 0) {
523 BT_ERR("Loading patch file failed");
524 return ret;
525 }
526 ret = ath3k_load_syscfg(udev);
527 if (ret < 0) {
528 BT_ERR("Loading sysconfig file failed");
529 return ret;
530 }
531 ret = ath3k_set_normal_mode(udev);
532 if (ret < 0) {
533 BT_ERR("Set normal mode failed");
534 return ret;
535 }
536 ath3k_switch_pid(udev);
537 return 0;
538 }
539
Paul Fertserc3eae822011-10-29 21:52:49 +0400540 ret = request_firmware(&firmware, ATH3K_FIRMWARE, &udev->dev);
541 if (ret < 0) {
542 if (ret == -ENOENT)
543 BT_ERR("Firmware file \"%s\" not found",
544 ATH3K_FIRMWARE);
545 else
546 BT_ERR("Firmware file \"%s\" request failed (err=%d)",
547 ATH3K_FIRMWARE, ret);
548 return ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530549 }
550
Rogério Brito84f0e172011-02-03 01:42:05 -0200551 ret = ath3k_load_firmware(udev, firmware);
Alexander Holler86e09282010-11-22 21:09:01 +0100552 release_firmware(firmware);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530553
Rogério Brito84f0e172011-02-03 01:42:05 -0200554 return ret;
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530555}
556
557static void ath3k_disconnect(struct usb_interface *intf)
558{
Maxim Zhukov0a219632018-02-05 00:09:47 +0300559 BT_DBG("%s intf %p", __func__, intf);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530560}
561
562static struct usb_driver ath3k_driver = {
563 .name = "ath3k",
564 .probe = ath3k_probe,
565 .disconnect = ath3k_disconnect,
566 .id_table = ath3k_table,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -0700567 .disable_hub_initiated_lpm = 1,
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530568};
569
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -0800570module_usb_driver(ath3k_driver);
Vikram Kandukuri9670d802010-01-06 19:04:15 +0530571
572MODULE_AUTHOR("Atheros Communications");
573MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
574MODULE_VERSION(VERSION);
575MODULE_LICENSE("GPL");
Paul Fertserc3eae822011-10-29 21:52:49 +0400576MODULE_FIRMWARE(ATH3K_FIRMWARE);