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