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