1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (c) 2008-2009 Atheros Communications Inc.
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/init.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/types.h>
12*4882a593Smuzhiyun #include <linux/errno.h>
13*4882a593Smuzhiyun #include <linux/device.h>
14*4882a593Smuzhiyun #include <linux/firmware.h>
15*4882a593Smuzhiyun #include <linux/usb.h>
16*4882a593Smuzhiyun #include <asm/unaligned.h>
17*4882a593Smuzhiyun #include <net/bluetooth/bluetooth.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #define VERSION "1.0"
20*4882a593Smuzhiyun #define ATH3K_FIRMWARE "ath3k-1.fw"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #define ATH3K_DNLOAD 0x01
23*4882a593Smuzhiyun #define ATH3K_GETSTATE 0x05
24*4882a593Smuzhiyun #define ATH3K_SET_NORMAL_MODE 0x07
25*4882a593Smuzhiyun #define ATH3K_GETVERSION 0x09
26*4882a593Smuzhiyun #define USB_REG_SWITCH_VID_PID 0x0a
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #define ATH3K_MODE_MASK 0x3F
29*4882a593Smuzhiyun #define ATH3K_NORMAL_MODE 0x0E
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #define ATH3K_PATCH_UPDATE 0x80
32*4882a593Smuzhiyun #define ATH3K_SYSCFG_UPDATE 0x40
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #define ATH3K_XTAL_FREQ_26M 0x00
35*4882a593Smuzhiyun #define ATH3K_XTAL_FREQ_40M 0x01
36*4882a593Smuzhiyun #define ATH3K_XTAL_FREQ_19P2 0x02
37*4882a593Smuzhiyun #define ATH3K_NAME_LEN 0xFF
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun struct ath3k_version {
40*4882a593Smuzhiyun __le32 rom_version;
41*4882a593Smuzhiyun __le32 build_version;
42*4882a593Smuzhiyun __le32 ram_version;
43*4882a593Smuzhiyun __u8 ref_clock;
44*4882a593Smuzhiyun __u8 reserved[7];
45*4882a593Smuzhiyun } __packed;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun static const struct usb_device_id ath3k_table[] = {
48*4882a593Smuzhiyun /* Atheros AR3011 */
49*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x3000) },
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun /* Atheros AR3011 with sflash firmware*/
52*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xE027) },
53*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xE03D) },
54*4882a593Smuzhiyun { USB_DEVICE(0x04F2, 0xAFF1) },
55*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x0215) },
56*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x3002) },
57*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0xE019) },
58*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3304) },
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /* Atheros AR9285 Malbec with sflash firmware */
61*4882a593Smuzhiyun { USB_DEVICE(0x03F0, 0x311D) },
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* Atheros AR3012 with sflash firmware*/
64*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe04d) },
65*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe04e) },
66*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe057) },
67*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe056) },
68*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe05f) },
69*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe076) },
70*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe078) },
71*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe095) },
72*4882a593Smuzhiyun { USB_DEVICE(0x04c5, 0x1330) },
73*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x3004) },
74*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x3005) },
75*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x3006) },
76*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x3007) },
77*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x3008) },
78*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x300b) },
79*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x300d) },
80*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x300f) },
81*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x3010) },
82*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x3014) },
83*4882a593Smuzhiyun { USB_DEVICE(0x04CA, 0x3018) },
84*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x0219) },
85*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x021c) },
86*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x0220) },
87*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x0227) },
88*4882a593Smuzhiyun { USB_DEVICE(0x0b05, 0x17d0) },
89*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x0036) },
90*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x3004) },
91*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x3008) },
92*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x311D) },
93*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x311E) },
94*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x311F) },
95*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0x3121) },
96*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x817a) },
97*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x817b) },
98*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0xe003) },
99*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0xE004) },
100*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0xE005) },
101*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0xE006) },
102*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3362) },
103*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3375) },
104*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3393) },
105*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3395) },
106*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3402) },
107*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3408) },
108*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3423) },
109*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3432) },
110*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3472) },
111*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3474) },
112*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3487) },
113*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3490) },
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* Atheros AR5BBU12 with sflash firmware */
116*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xE02C) },
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun /* Atheros AR5BBU22 with sflash firmware */
119*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xE036) },
120*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xE03C) },
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun { } /* Terminating entry */
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, ath3k_table);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun #define BTUSB_ATH3012 0x80
128*4882a593Smuzhiyun /* This table is to load patch and sysconfig files
129*4882a593Smuzhiyun * for AR3012
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun static const struct usb_device_id ath3k_blist_tbl[] = {
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* Atheros AR3012 with sflash firmware*/
134*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
135*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
136*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
137*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
138*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
139*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
140*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
141*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
142*4882a593Smuzhiyun { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
143*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
144*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
145*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
146*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
147*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
148*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
149*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
150*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
151*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
152*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
153*4882a593Smuzhiyun { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
154*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
155*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
156*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
157*4882a593Smuzhiyun { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
158*4882a593Smuzhiyun { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
159*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x0036), .driver_info = BTUSB_ATH3012 },
160*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
161*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
162*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0x311D), .driver_info = BTUSB_ATH3012 },
163*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0x311E), .driver_info = BTUSB_ATH3012 },
164*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0x311F), .driver_info = BTUSB_ATH3012 },
165*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
166*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x817a), .driver_info = BTUSB_ATH3012 },
167*4882a593Smuzhiyun { USB_DEVICE(0x0CF3, 0x817b), .driver_info = BTUSB_ATH3012 },
168*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
169*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
170*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
171*4882a593Smuzhiyun { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
172*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
173*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
174*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
175*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
176*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
177*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
178*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
179*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
180*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
181*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
182*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
183*4882a593Smuzhiyun { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* Atheros AR5BBU22 with sflash firmware */
186*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xE036), .driver_info = BTUSB_ATH3012 },
187*4882a593Smuzhiyun { USB_DEVICE(0x0489, 0xE03C), .driver_info = BTUSB_ATH3012 },
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun { } /* Terminating entry */
190*4882a593Smuzhiyun };
191*4882a593Smuzhiyun
ath3k_log_failed_loading(int err,int len,int size,int count)192*4882a593Smuzhiyun static inline void ath3k_log_failed_loading(int err, int len, int size,
193*4882a593Smuzhiyun int count)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun BT_ERR("Firmware loading err = %d, len = %d, size = %d, count = %d",
196*4882a593Smuzhiyun err, len, size, count);
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun #define USB_REQ_DFU_DNLOAD 1
200*4882a593Smuzhiyun #define BULK_SIZE 4096
201*4882a593Smuzhiyun #define FW_HDR_SIZE 20
202*4882a593Smuzhiyun #define TIMEGAP_USEC_MIN 50
203*4882a593Smuzhiyun #define TIMEGAP_USEC_MAX 100
204*4882a593Smuzhiyun
ath3k_load_firmware(struct usb_device * udev,const struct firmware * firmware)205*4882a593Smuzhiyun static int ath3k_load_firmware(struct usb_device *udev,
206*4882a593Smuzhiyun const struct firmware *firmware)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun u8 *send_buf;
209*4882a593Smuzhiyun int len = 0;
210*4882a593Smuzhiyun int err, pipe, size, sent = 0;
211*4882a593Smuzhiyun int count = firmware->size;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun BT_DBG("udev %p", udev);
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
216*4882a593Smuzhiyun if (!send_buf) {
217*4882a593Smuzhiyun BT_ERR("Can't allocate memory chunk for firmware");
218*4882a593Smuzhiyun return -ENOMEM;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun err = usb_control_msg_send(udev, 0, USB_REQ_DFU_DNLOAD, USB_TYPE_VENDOR,
222*4882a593Smuzhiyun 0, 0, firmware->data, FW_HDR_SIZE,
223*4882a593Smuzhiyun USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
224*4882a593Smuzhiyun if (err) {
225*4882a593Smuzhiyun BT_ERR("Can't change to loading configuration err");
226*4882a593Smuzhiyun goto error;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun sent += FW_HDR_SIZE;
229*4882a593Smuzhiyun count -= FW_HDR_SIZE;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun pipe = usb_sndbulkpipe(udev, 0x02);
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun while (count) {
234*4882a593Smuzhiyun /* workaround the compatibility issue with xHCI controller*/
235*4882a593Smuzhiyun usleep_range(TIMEGAP_USEC_MIN, TIMEGAP_USEC_MAX);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun size = min_t(uint, count, BULK_SIZE);
238*4882a593Smuzhiyun memcpy(send_buf, firmware->data + sent, size);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun err = usb_bulk_msg(udev, pipe, send_buf, size,
241*4882a593Smuzhiyun &len, 3000);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun if (err || (len != size)) {
244*4882a593Smuzhiyun ath3k_log_failed_loading(err, len, size, count);
245*4882a593Smuzhiyun goto error;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun sent += size;
249*4882a593Smuzhiyun count -= size;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun error:
253*4882a593Smuzhiyun kfree(send_buf);
254*4882a593Smuzhiyun return err;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
ath3k_get_state(struct usb_device * udev,unsigned char * state)257*4882a593Smuzhiyun static int ath3k_get_state(struct usb_device *udev, unsigned char *state)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun return usb_control_msg_recv(udev, 0, ATH3K_GETSTATE,
260*4882a593Smuzhiyun USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
261*4882a593Smuzhiyun state, 1, USB_CTRL_SET_TIMEOUT,
262*4882a593Smuzhiyun GFP_KERNEL);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
ath3k_get_version(struct usb_device * udev,struct ath3k_version * version)265*4882a593Smuzhiyun static int ath3k_get_version(struct usb_device *udev,
266*4882a593Smuzhiyun struct ath3k_version *version)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun return usb_control_msg_recv(udev, 0, ATH3K_GETVERSION,
269*4882a593Smuzhiyun USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
270*4882a593Smuzhiyun version, sizeof(*version), USB_CTRL_SET_TIMEOUT,
271*4882a593Smuzhiyun GFP_KERNEL);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun
ath3k_load_fwfile(struct usb_device * udev,const struct firmware * firmware)274*4882a593Smuzhiyun static int ath3k_load_fwfile(struct usb_device *udev,
275*4882a593Smuzhiyun const struct firmware *firmware)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun u8 *send_buf;
278*4882a593Smuzhiyun int len = 0;
279*4882a593Smuzhiyun int err, pipe, size, count, sent = 0;
280*4882a593Smuzhiyun int ret;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun count = firmware->size;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun send_buf = kmalloc(BULK_SIZE, GFP_KERNEL);
285*4882a593Smuzhiyun if (!send_buf) {
286*4882a593Smuzhiyun BT_ERR("Can't allocate memory chunk for firmware");
287*4882a593Smuzhiyun return -ENOMEM;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun size = min_t(uint, count, FW_HDR_SIZE);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun ret = usb_control_msg_send(udev, 0, ATH3K_DNLOAD, USB_TYPE_VENDOR, 0, 0,
293*4882a593Smuzhiyun firmware->data, size, USB_CTRL_SET_TIMEOUT,
294*4882a593Smuzhiyun GFP_KERNEL);
295*4882a593Smuzhiyun if (ret) {
296*4882a593Smuzhiyun BT_ERR("Can't change to loading configuration err");
297*4882a593Smuzhiyun kfree(send_buf);
298*4882a593Smuzhiyun return ret;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun sent += size;
302*4882a593Smuzhiyun count -= size;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun pipe = usb_sndbulkpipe(udev, 0x02);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun while (count) {
307*4882a593Smuzhiyun /* workaround the compatibility issue with xHCI controller*/
308*4882a593Smuzhiyun usleep_range(TIMEGAP_USEC_MIN, TIMEGAP_USEC_MAX);
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun size = min_t(uint, count, BULK_SIZE);
311*4882a593Smuzhiyun memcpy(send_buf, firmware->data + sent, size);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun err = usb_bulk_msg(udev, pipe, send_buf, size,
314*4882a593Smuzhiyun &len, 3000);
315*4882a593Smuzhiyun if (err || (len != size)) {
316*4882a593Smuzhiyun ath3k_log_failed_loading(err, len, size, count);
317*4882a593Smuzhiyun kfree(send_buf);
318*4882a593Smuzhiyun return err;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun sent += size;
321*4882a593Smuzhiyun count -= size;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun kfree(send_buf);
325*4882a593Smuzhiyun return 0;
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
ath3k_switch_pid(struct usb_device * udev)328*4882a593Smuzhiyun static void ath3k_switch_pid(struct usb_device *udev)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun usb_control_msg_send(udev, 0, USB_REG_SWITCH_VID_PID, USB_TYPE_VENDOR,
331*4882a593Smuzhiyun 0, 0, NULL, 0, USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
ath3k_set_normal_mode(struct usb_device * udev)334*4882a593Smuzhiyun static int ath3k_set_normal_mode(struct usb_device *udev)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun unsigned char fw_state;
337*4882a593Smuzhiyun int ret;
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun ret = ath3k_get_state(udev, &fw_state);
340*4882a593Smuzhiyun if (ret) {
341*4882a593Smuzhiyun BT_ERR("Can't get state to change to normal mode err");
342*4882a593Smuzhiyun return ret;
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun if ((fw_state & ATH3K_MODE_MASK) == ATH3K_NORMAL_MODE) {
346*4882a593Smuzhiyun BT_DBG("firmware was already in normal mode");
347*4882a593Smuzhiyun return 0;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun return usb_control_msg_send(udev, 0, ATH3K_SET_NORMAL_MODE,
351*4882a593Smuzhiyun USB_TYPE_VENDOR, 0, 0, NULL, 0,
352*4882a593Smuzhiyun USB_CTRL_SET_TIMEOUT, GFP_KERNEL);
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
ath3k_load_patch(struct usb_device * udev)355*4882a593Smuzhiyun static int ath3k_load_patch(struct usb_device *udev)
356*4882a593Smuzhiyun {
357*4882a593Smuzhiyun unsigned char fw_state;
358*4882a593Smuzhiyun char filename[ATH3K_NAME_LEN];
359*4882a593Smuzhiyun const struct firmware *firmware;
360*4882a593Smuzhiyun struct ath3k_version fw_version;
361*4882a593Smuzhiyun __u32 pt_rom_version, pt_build_version;
362*4882a593Smuzhiyun int ret;
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun ret = ath3k_get_state(udev, &fw_state);
365*4882a593Smuzhiyun if (ret) {
366*4882a593Smuzhiyun BT_ERR("Can't get state to change to load ram patch err");
367*4882a593Smuzhiyun return ret;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun if (fw_state & ATH3K_PATCH_UPDATE) {
371*4882a593Smuzhiyun BT_DBG("Patch was already downloaded");
372*4882a593Smuzhiyun return 0;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun
375*4882a593Smuzhiyun ret = ath3k_get_version(udev, &fw_version);
376*4882a593Smuzhiyun if (ret) {
377*4882a593Smuzhiyun BT_ERR("Can't get version to change to load ram patch err");
378*4882a593Smuzhiyun return ret;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun snprintf(filename, ATH3K_NAME_LEN, "ar3k/AthrBT_0x%08x.dfu",
382*4882a593Smuzhiyun le32_to_cpu(fw_version.rom_version));
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun ret = request_firmware(&firmware, filename, &udev->dev);
385*4882a593Smuzhiyun if (ret < 0) {
386*4882a593Smuzhiyun BT_ERR("Patch file not found %s", filename);
387*4882a593Smuzhiyun return ret;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun pt_rom_version = get_unaligned_le32(firmware->data +
391*4882a593Smuzhiyun firmware->size - 8);
392*4882a593Smuzhiyun pt_build_version = get_unaligned_le32(firmware->data +
393*4882a593Smuzhiyun firmware->size - 4);
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (pt_rom_version != le32_to_cpu(fw_version.rom_version) ||
396*4882a593Smuzhiyun pt_build_version <= le32_to_cpu(fw_version.build_version)) {
397*4882a593Smuzhiyun BT_ERR("Patch file version did not match with firmware");
398*4882a593Smuzhiyun release_firmware(firmware);
399*4882a593Smuzhiyun return -EINVAL;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun ret = ath3k_load_fwfile(udev, firmware);
403*4882a593Smuzhiyun release_firmware(firmware);
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun return ret;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
ath3k_load_syscfg(struct usb_device * udev)408*4882a593Smuzhiyun static int ath3k_load_syscfg(struct usb_device *udev)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun unsigned char fw_state;
411*4882a593Smuzhiyun char filename[ATH3K_NAME_LEN];
412*4882a593Smuzhiyun const struct firmware *firmware;
413*4882a593Smuzhiyun struct ath3k_version fw_version;
414*4882a593Smuzhiyun int clk_value, ret;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun ret = ath3k_get_state(udev, &fw_state);
417*4882a593Smuzhiyun if (ret) {
418*4882a593Smuzhiyun BT_ERR("Can't get state to change to load configuration err");
419*4882a593Smuzhiyun return -EBUSY;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun ret = ath3k_get_version(udev, &fw_version);
423*4882a593Smuzhiyun if (ret) {
424*4882a593Smuzhiyun BT_ERR("Can't get version to change to load ram patch err");
425*4882a593Smuzhiyun return ret;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun switch (fw_version.ref_clock) {
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun case ATH3K_XTAL_FREQ_26M:
431*4882a593Smuzhiyun clk_value = 26;
432*4882a593Smuzhiyun break;
433*4882a593Smuzhiyun case ATH3K_XTAL_FREQ_40M:
434*4882a593Smuzhiyun clk_value = 40;
435*4882a593Smuzhiyun break;
436*4882a593Smuzhiyun case ATH3K_XTAL_FREQ_19P2:
437*4882a593Smuzhiyun clk_value = 19;
438*4882a593Smuzhiyun break;
439*4882a593Smuzhiyun default:
440*4882a593Smuzhiyun clk_value = 0;
441*4882a593Smuzhiyun break;
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun snprintf(filename, ATH3K_NAME_LEN, "ar3k/ramps_0x%08x_%d%s",
445*4882a593Smuzhiyun le32_to_cpu(fw_version.rom_version), clk_value, ".dfu");
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun ret = request_firmware(&firmware, filename, &udev->dev);
448*4882a593Smuzhiyun if (ret < 0) {
449*4882a593Smuzhiyun BT_ERR("Configuration file not found %s", filename);
450*4882a593Smuzhiyun return ret;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun ret = ath3k_load_fwfile(udev, firmware);
454*4882a593Smuzhiyun release_firmware(firmware);
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun return ret;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
ath3k_probe(struct usb_interface * intf,const struct usb_device_id * id)459*4882a593Smuzhiyun static int ath3k_probe(struct usb_interface *intf,
460*4882a593Smuzhiyun const struct usb_device_id *id)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun const struct firmware *firmware;
463*4882a593Smuzhiyun struct usb_device *udev = interface_to_usbdev(intf);
464*4882a593Smuzhiyun int ret;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun BT_DBG("intf %p id %p", intf, id);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
469*4882a593Smuzhiyun return -ENODEV;
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /* match device ID in ath3k blacklist table */
472*4882a593Smuzhiyun if (!id->driver_info) {
473*4882a593Smuzhiyun const struct usb_device_id *match;
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun match = usb_match_id(intf, ath3k_blist_tbl);
476*4882a593Smuzhiyun if (match)
477*4882a593Smuzhiyun id = match;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /* load patch and sysconfig files for AR3012 */
481*4882a593Smuzhiyun if (id->driver_info & BTUSB_ATH3012) {
482*4882a593Smuzhiyun /* New firmware with patch and sysconfig files already loaded */
483*4882a593Smuzhiyun if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x0001)
484*4882a593Smuzhiyun return -ENODEV;
485*4882a593Smuzhiyun
486*4882a593Smuzhiyun ret = ath3k_load_patch(udev);
487*4882a593Smuzhiyun if (ret < 0) {
488*4882a593Smuzhiyun BT_ERR("Loading patch file failed");
489*4882a593Smuzhiyun return ret;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun ret = ath3k_load_syscfg(udev);
492*4882a593Smuzhiyun if (ret < 0) {
493*4882a593Smuzhiyun BT_ERR("Loading sysconfig file failed");
494*4882a593Smuzhiyun return ret;
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun ret = ath3k_set_normal_mode(udev);
497*4882a593Smuzhiyun if (ret) {
498*4882a593Smuzhiyun BT_ERR("Set normal mode failed");
499*4882a593Smuzhiyun return ret;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun ath3k_switch_pid(udev);
502*4882a593Smuzhiyun return 0;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun ret = request_firmware(&firmware, ATH3K_FIRMWARE, &udev->dev);
506*4882a593Smuzhiyun if (ret < 0) {
507*4882a593Smuzhiyun if (ret == -ENOENT)
508*4882a593Smuzhiyun BT_ERR("Firmware file \"%s\" not found",
509*4882a593Smuzhiyun ATH3K_FIRMWARE);
510*4882a593Smuzhiyun else
511*4882a593Smuzhiyun BT_ERR("Firmware file \"%s\" request failed (err=%d)",
512*4882a593Smuzhiyun ATH3K_FIRMWARE, ret);
513*4882a593Smuzhiyun return ret;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun ret = ath3k_load_firmware(udev, firmware);
517*4882a593Smuzhiyun release_firmware(firmware);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun return ret;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
ath3k_disconnect(struct usb_interface * intf)522*4882a593Smuzhiyun static void ath3k_disconnect(struct usb_interface *intf)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun BT_DBG("%s intf %p", __func__, intf);
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun static struct usb_driver ath3k_driver = {
528*4882a593Smuzhiyun .name = "ath3k",
529*4882a593Smuzhiyun .probe = ath3k_probe,
530*4882a593Smuzhiyun .disconnect = ath3k_disconnect,
531*4882a593Smuzhiyun .id_table = ath3k_table,
532*4882a593Smuzhiyun .disable_hub_initiated_lpm = 1,
533*4882a593Smuzhiyun };
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun module_usb_driver(ath3k_driver);
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun MODULE_AUTHOR("Atheros Communications");
538*4882a593Smuzhiyun MODULE_DESCRIPTION("Atheros AR30xx firmware driver");
539*4882a593Smuzhiyun MODULE_VERSION(VERSION);
540*4882a593Smuzhiyun MODULE_LICENSE("GPL");
541*4882a593Smuzhiyun MODULE_FIRMWARE(ATH3K_FIRMWARE);
542