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