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