blob: 2867499f7256988c4a8587b0022ac77aed5ba25f [file] [log] [blame]
Bing Zhao789221e2009-06-02 14:29:36 -07001/**
2 * Marvell BT-over-SDIO driver: SDIO interface related functions.
3 *
4 * Copyright (C) 2009, Marvell International Ltd.
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 *
15 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
18 * this warranty disclaimer.
19 **/
20
21#include <linux/firmware.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Bing Zhao789221e2009-06-02 14:29:36 -070023
24#include <linux/mmc/sdio_ids.h>
25#include <linux/mmc/sdio_func.h>
Paul Gortmakera59b9682011-08-29 16:44:23 -040026#include <linux/module.h>
Bing Zhao789221e2009-06-02 14:29:36 -070027
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30
31#include "btmrvl_drv.h"
32#include "btmrvl_sdio.h"
33
34#define VERSION "1.0"
35
Bing Zhao789221e2009-06-02 14:29:36 -070036/* The btmrvl_sdio_remove() callback function is called
37 * when user removes this module from kernel space or ejects
38 * the card from the slot. The driver handles these 2 cases
39 * differently.
40 * If the user is removing the module, a MODULE_SHUTDOWN_REQ
41 * command is sent to firmware and interrupt will be disabled.
42 * If the card is removed, there is no need to send command
43 * or disable interrupt.
44 *
45 * The variable 'user_rmmod' is used to distinguish these two
46 * scenarios. This flag is initialized as FALSE in case the card
47 * is removed, and will be set to TRUE for module removal when
48 * module_exit function is called.
49 */
50static u8 user_rmmod;
Amitkumar Karward1d10d72010-05-27 16:38:37 -070051static u8 sdio_ireg;
Bing Zhao789221e2009-06-02 14:29:36 -070052
Kevin Gan9f72c1d2011-04-08 18:19:33 -070053static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
54 .cfg = 0x03,
55 .host_int_mask = 0x04,
56 .host_intstatus = 0x05,
57 .card_status = 0x20,
58 .sq_read_base_addr_a0 = 0x10,
59 .sq_read_base_addr_a1 = 0x11,
60 .card_fw_status0 = 0x40,
61 .card_fw_status1 = 0x41,
62 .card_rx_len = 0x42,
63 .card_rx_unit = 0x43,
64 .io_port_0 = 0x00,
65 .io_port_1 = 0x01,
66 .io_port_2 = 0x02,
67};
Bing Zhao2ac654f2011-11-16 20:40:42 -080068static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
Kevin Gan9f72c1d2011-04-08 18:19:33 -070069 .cfg = 0x00,
70 .host_int_mask = 0x02,
71 .host_intstatus = 0x03,
72 .card_status = 0x30,
73 .sq_read_base_addr_a0 = 0x40,
74 .sq_read_base_addr_a1 = 0x41,
75 .card_revision = 0x5c,
76 .card_fw_status0 = 0x60,
77 .card_fw_status1 = 0x61,
78 .card_rx_len = 0x62,
79 .card_rx_unit = 0x63,
80 .io_port_0 = 0x78,
81 .io_port_1 = 0x79,
82 .io_port_2 = 0x7a,
83};
84
Paul Bollec6757862011-10-05 14:56:46 +020085static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +020086 .helper = "sd8688_helper.bin",
87 .firmware = "sd8688.bin",
Kevin Gan9f72c1d2011-04-08 18:19:33 -070088 .reg = &btmrvl_reg_8688,
89 .sd_blksz_fw_dl = 64,
90};
91
92static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
93 .helper = NULL,
94 .firmware = "mrvl/sd8787_uapsta.bin",
Bing Zhao2ac654f2011-11-16 20:40:42 -080095 .reg = &btmrvl_reg_87xx,
96 .sd_blksz_fw_dl = 256,
97};
98
99static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
100 .helper = NULL,
101 .firmware = "mrvl/sd8797_uapsta.bin",
102 .reg = &btmrvl_reg_87xx,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700103 .sd_blksz_fw_dl = 256,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200104};
105
Bing Zhao789221e2009-06-02 14:29:36 -0700106static const struct sdio_device_id btmrvl_sdio_ids[] = {
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200107 /* Marvell SD8688 Bluetooth device */
108 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
Paul Bollec6757862011-10-05 14:56:46 +0200109 .driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700110 /* Marvell SD8787 Bluetooth device */
111 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
112 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao37ed5612012-05-23 18:50:04 -0700113 /* Marvell SD8787 Bluetooth AMP device */
114 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911B),
115 .driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
Bing Zhao2ac654f2011-11-16 20:40:42 -0800116 /* Marvell SD8797 Bluetooth device */
117 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
118 .driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200119
120 { } /* Terminating entry */
Bing Zhao789221e2009-06-02 14:29:36 -0700121};
122
123MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
124
Bing Zhao789221e2009-06-02 14:29:36 -0700125static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
126{
127 u8 reg;
128 int ret;
129
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700130 reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700131 if (!ret)
132 card->rx_unit = reg;
133
Bing Zhao789221e2009-06-02 14:29:36 -0700134 return ret;
135}
136
137static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
138{
Bing Zhao789221e2009-06-02 14:29:36 -0700139 u8 fws0, fws1;
Marcel Holtmann93742532009-06-13 07:40:18 +0200140 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700141
142 *dat = 0;
143
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700144 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Kulikov Vasiliy278be272010-07-05 12:01:22 +0400145 if (ret)
146 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700147
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700148 fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200149 if (ret)
Bing Zhao789221e2009-06-02 14:29:36 -0700150 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700151
152 *dat = (((u16) fws1) << 8) | fws0;
153
Bing Zhao789221e2009-06-02 14:29:36 -0700154 return 0;
155}
156
157static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
158{
Bing Zhao789221e2009-06-02 14:29:36 -0700159 u8 reg;
Marcel Holtmann93742532009-06-13 07:40:18 +0200160 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700161
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700162 reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700163 if (!ret)
164 *dat = (u16) reg << card->rx_unit;
165
Bing Zhao789221e2009-06-02 14:29:36 -0700166 return ret;
167}
168
169static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200170 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700171{
172 int ret;
173
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700174 sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700175 if (ret) {
176 BT_ERR("Unable to enable the host interrupt!");
177 ret = -EIO;
178 }
179
Bing Zhao789221e2009-06-02 14:29:36 -0700180 return ret;
181}
182
183static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200184 u8 mask)
Bing Zhao789221e2009-06-02 14:29:36 -0700185{
Bing Zhao789221e2009-06-02 14:29:36 -0700186 u8 host_int_mask;
Marcel Holtmann93742532009-06-13 07:40:18 +0200187 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700188
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700189 host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
Marcel Holtmann93742532009-06-13 07:40:18 +0200190 if (ret)
191 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700192
193 host_int_mask &= ~mask;
194
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700195 sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700196 if (ret < 0) {
197 BT_ERR("Unable to disable the host interrupt!");
Marcel Holtmann93742532009-06-13 07:40:18 +0200198 return -EIO;
Bing Zhao789221e2009-06-02 14:29:36 -0700199 }
200
Marcel Holtmann93742532009-06-13 07:40:18 +0200201 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700202}
203
204static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
205{
206 unsigned int tries;
Bing Zhao789221e2009-06-02 14:29:36 -0700207 u8 status;
Marcel Holtmann93742532009-06-13 07:40:18 +0200208 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700209
210 for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700211 status = sdio_readb(card->func, card->reg->card_status, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700212 if (ret)
213 goto failed;
214 if ((status & bits) == bits)
Marcel Holtmann93742532009-06-13 07:40:18 +0200215 return ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700216
217 udelay(1);
218 }
219
220 ret = -ETIMEDOUT;
221
222failed:
223 BT_ERR("FAILED! ret=%d", ret);
224
Bing Zhao789221e2009-06-02 14:29:36 -0700225 return ret;
226}
227
228static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200229 int pollnum)
Bing Zhao789221e2009-06-02 14:29:36 -0700230{
231 int ret = -ETIMEDOUT;
232 u16 firmwarestat;
233 unsigned int tries;
234
Bing Zhao789221e2009-06-02 14:29:36 -0700235 /* Wait for firmware to become ready */
236 for (tries = 0; tries < pollnum; tries++) {
237 if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
238 continue;
239
240 if (firmwarestat == FIRMWARE_READY) {
241 ret = 0;
242 break;
243 } else {
244 msleep(10);
245 }
246 }
247
Bing Zhao789221e2009-06-02 14:29:36 -0700248 return ret;
249}
250
251static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
252{
253 const struct firmware *fw_helper = NULL;
254 const u8 *helper = NULL;
255 int ret;
256 void *tmphlprbuf = NULL;
257 int tmphlprbufsz, hlprblknow, helperlen;
258 u8 *helperbuf;
259 u32 tx_len;
260
Bing Zhao789221e2009-06-02 14:29:36 -0700261 ret = request_firmware(&fw_helper, card->helper,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200262 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700263 if ((ret < 0) || !fw_helper) {
264 BT_ERR("request_firmware(helper) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200265 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700266 ret = -ENOENT;
267 goto done;
268 }
269
270 helper = fw_helper->data;
271 helperlen = fw_helper->size;
272
273 BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200274 helperlen, SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700275
276 tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
277
Julia Lawallf8df39f2010-05-13 22:02:03 +0200278 tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700279 if (!tmphlprbuf) {
280 BT_ERR("Unable to allocate buffer for helper."
281 " Terminating download");
282 ret = -ENOMEM;
283 goto done;
284 }
285
Bing Zhao789221e2009-06-02 14:29:36 -0700286 helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
287
288 /* Perform helper data transfer */
289 tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
290 - SDIO_HEADER_LEN;
291 hlprblknow = 0;
292
293 do {
294 ret = btmrvl_sdio_poll_card_status(card,
295 CARD_IO_READY | DN_LD_CARD_RDY);
296 if (ret < 0) {
297 BT_ERR("Helper download poll status timeout @ %d",
298 hlprblknow);
299 goto done;
300 }
301
302 /* Check if there is more data? */
303 if (hlprblknow >= helperlen)
304 break;
305
306 if (helperlen - hlprblknow < tx_len)
307 tx_len = helperlen - hlprblknow;
308
309 /* Little-endian */
310 helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
311 helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
312 helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
313 helperbuf[3] = ((tx_len & 0xff000000) >> 24);
314
315 memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
316 tx_len);
317
318 /* Now send the data */
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200319 ret = sdio_writesb(card->func, card->ioport, helperbuf,
320 FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700321 if (ret < 0) {
322 BT_ERR("IO error during helper download @ %d",
323 hlprblknow);
324 goto done;
325 }
326
327 hlprblknow += tx_len;
328 } while (true);
329
330 BT_DBG("Transferring helper image EOF block");
331
332 memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
333
334 ret = sdio_writesb(card->func, card->ioport, helperbuf,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200335 SDIO_BLOCK_SIZE);
Bing Zhao789221e2009-06-02 14:29:36 -0700336 if (ret < 0) {
337 BT_ERR("IO error in writing helper image EOF block");
338 goto done;
339 }
340
341 ret = 0;
342
343done:
344 kfree(tmphlprbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200345 release_firmware(fw_helper);
Bing Zhao789221e2009-06-02 14:29:36 -0700346 return ret;
347}
348
349static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
350{
351 const struct firmware *fw_firmware = NULL;
352 const u8 *firmware = NULL;
353 int firmwarelen, tmpfwbufsz, ret;
354 unsigned int tries, offset;
355 u8 base0, base1;
356 void *tmpfwbuf = NULL;
357 u8 *fwbuf;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700358 u16 len, blksz_dl = card->sd_blksz_fw_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700359 int txlen = 0, tx_blocks = 0, count = 0;
360
Bing Zhao789221e2009-06-02 14:29:36 -0700361 ret = request_firmware(&fw_firmware, card->firmware,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200362 &card->func->dev);
Bing Zhao789221e2009-06-02 14:29:36 -0700363 if ((ret < 0) || !fw_firmware) {
364 BT_ERR("request_firmware(firmware) failed, error code = %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200365 ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700366 ret = -ENOENT;
367 goto done;
368 }
369
370 firmware = fw_firmware->data;
371 firmwarelen = fw_firmware->size;
372
373 BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
374
375 tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
Julia Lawallf8df39f2010-05-13 22:02:03 +0200376 tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
Bing Zhao789221e2009-06-02 14:29:36 -0700377 if (!tmpfwbuf) {
378 BT_ERR("Unable to allocate buffer for firmware."
379 " Terminating download");
380 ret = -ENOMEM;
381 goto done;
382 }
383
Bing Zhao789221e2009-06-02 14:29:36 -0700384 /* Ensure aligned firmware buffer */
385 fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
386
387 /* Perform firmware data transfer */
388 offset = 0;
389 do {
390 ret = btmrvl_sdio_poll_card_status(card,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200391 CARD_IO_READY | DN_LD_CARD_RDY);
Bing Zhao789221e2009-06-02 14:29:36 -0700392 if (ret < 0) {
393 BT_ERR("FW download with helper poll status"
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200394 " timeout @ %d", offset);
Bing Zhao789221e2009-06-02 14:29:36 -0700395 goto done;
396 }
397
398 /* Check if there is more data ? */
399 if (offset >= firmwarelen)
400 break;
401
402 for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
403 base0 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700404 card->reg->sq_read_base_addr_a0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700405 if (ret) {
406 BT_ERR("BASE0 register read failed:"
407 " base0 = 0x%04X(%d)."
408 " Terminating download",
409 base0, base0);
410 ret = -EIO;
411 goto done;
412 }
413 base1 = sdio_readb(card->func,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700414 card->reg->sq_read_base_addr_a1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700415 if (ret) {
416 BT_ERR("BASE1 register read failed:"
417 " base1 = 0x%04X(%d)."
418 " Terminating download",
419 base1, base1);
420 ret = -EIO;
421 goto done;
422 }
423
424 len = (((u16) base1) << 8) | base0;
425 if (len)
426 break;
427
428 udelay(10);
429 }
430
431 if (!len)
432 break;
433 else if (len > BTM_UPLD_SIZE) {
434 BT_ERR("FW download failure @%d, invalid length %d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200435 offset, len);
Bing Zhao789221e2009-06-02 14:29:36 -0700436 ret = -EINVAL;
437 goto done;
438 }
439
440 txlen = len;
441
442 if (len & BIT(0)) {
443 count++;
444 if (count > MAX_WRITE_IOMEM_RETRY) {
445 BT_ERR("FW download failure @%d, "
446 "over max retry count", offset);
447 ret = -EIO;
448 goto done;
449 }
450 BT_ERR("FW CRC error indicated by the helper: "
451 "len = 0x%04X, txlen = %d", len, txlen);
452 len &= ~BIT(0);
453 /* Set txlen to 0 so as to resend from same offset */
454 txlen = 0;
455 } else {
456 count = 0;
457
458 /* Last block ? */
459 if (firmwarelen - offset < txlen)
460 txlen = firmwarelen - offset;
461
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700462 tx_blocks = (txlen + blksz_dl - 1) / blksz_dl;
Bing Zhao789221e2009-06-02 14:29:36 -0700463
464 memcpy(fwbuf, &firmware[offset], txlen);
465 }
466
467 ret = sdio_writesb(card->func, card->ioport, fwbuf,
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700468 tx_blocks * blksz_dl);
Bing Zhao789221e2009-06-02 14:29:36 -0700469
470 if (ret < 0) {
471 BT_ERR("FW download, writesb(%d) failed @%d",
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200472 count, offset);
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700473 sdio_writeb(card->func, HOST_CMD53_FIN,
474 card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700475 if (ret)
476 BT_ERR("writeb failed (CFG)");
477 }
478
479 offset += txlen;
480 } while (true);
481
482 BT_DBG("FW download over, size %d bytes", offset);
483
484 ret = 0;
485
486done:
487 kfree(tmpfwbuf);
Jesper Juhl59f34fb2012-04-09 22:49:49 +0200488 release_firmware(fw_firmware);
Bing Zhao789221e2009-06-02 14:29:36 -0700489 return ret;
490}
491
492static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
493{
494 u16 buf_len = 0;
495 int ret, buf_block_len, blksz;
496 struct sk_buff *skb = NULL;
497 u32 type;
498 u8 *payload = NULL;
499 struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
500 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
501
Bing Zhao789221e2009-06-02 14:29:36 -0700502 if (!card || !card->func) {
503 BT_ERR("card or function is NULL!");
504 ret = -EINVAL;
505 goto exit;
506 }
507
508 /* Read the length of data to be transferred */
509 ret = btmrvl_sdio_read_rx_len(card, &buf_len);
510 if (ret < 0) {
511 BT_ERR("read rx_len failed");
512 ret = -EIO;
513 goto exit;
514 }
515
516 blksz = SDIO_BLOCK_SIZE;
517 buf_block_len = (buf_len + blksz - 1) / blksz;
518
519 if (buf_len <= SDIO_HEADER_LEN
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200520 || (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
Bing Zhao789221e2009-06-02 14:29:36 -0700521 BT_ERR("invalid packet length: %d", buf_len);
522 ret = -EINVAL;
523 goto exit;
524 }
525
526 /* Allocate buffer */
527 skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200528 GFP_ATOMIC);
Bing Zhao789221e2009-06-02 14:29:36 -0700529 if (skb == NULL) {
530 BT_ERR("No free skb");
531 goto exit;
532 }
533
Bing Zhao3318b232009-07-08 11:44:14 -0700534 if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
535 skb_put(skb, (unsigned long) skb->data &
536 (BTSDIO_DMA_ALIGN - 1));
537 skb_pull(skb, (unsigned long) skb->data &
538 (BTSDIO_DMA_ALIGN - 1));
Bing Zhao789221e2009-06-02 14:29:36 -0700539 }
540
Bing Zhao3318b232009-07-08 11:44:14 -0700541 payload = skb->data;
Bing Zhao789221e2009-06-02 14:29:36 -0700542
543 ret = sdio_readsb(card->func, payload, card->ioport,
544 buf_block_len * blksz);
545 if (ret < 0) {
546 BT_ERR("readsb failed: %d", ret);
547 ret = -EIO;
548 goto exit;
549 }
550
551 /* This is SDIO specific header length: byte[2][1][0], type: byte[3]
552 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
553 */
554
555 buf_len = payload[0];
556 buf_len |= (u16) payload[1] << 8;
557 type = payload[3];
558
559 switch (type) {
560 case HCI_ACLDATA_PKT:
561 case HCI_SCODATA_PKT:
562 case HCI_EVENT_PKT:
563 bt_cb(skb)->pkt_type = type;
564 skb->dev = (void *)hdev;
565 skb_put(skb, buf_len);
566 skb_pull(skb, SDIO_HEADER_LEN);
567
568 if (type == HCI_EVENT_PKT)
569 btmrvl_check_evtpkt(priv, skb);
570
571 hci_recv_frame(skb);
572 hdev->stat.byte_rx += buf_len;
573 break;
574
575 case MRVL_VENDOR_PKT:
576 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
577 skb->dev = (void *)hdev;
578 skb_put(skb, buf_len);
579 skb_pull(skb, SDIO_HEADER_LEN);
580
581 if (btmrvl_process_event(priv, skb))
582 hci_recv_frame(skb);
583
584 hdev->stat.byte_rx += buf_len;
585 break;
586
587 default:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200588 BT_ERR("Unknown packet type:%d", type);
Bing Zhao789221e2009-06-02 14:29:36 -0700589 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200590 blksz * buf_block_len);
Bing Zhao789221e2009-06-02 14:29:36 -0700591
592 kfree_skb(skb);
593 skb = NULL;
594 break;
595 }
596
597exit:
598 if (ret) {
599 hdev->stat.err_rx++;
600 if (skb)
601 kfree_skb(skb);
602 }
603
Bing Zhao789221e2009-06-02 14:29:36 -0700604 return ret;
605}
606
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700607static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
Bing Zhao789221e2009-06-02 14:29:36 -0700608{
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700609 ulong flags;
610 u8 ireg;
Bing Zhao789221e2009-06-02 14:29:36 -0700611 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
612
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700613 spin_lock_irqsave(&priv->driver_lock, flags);
614 ireg = sdio_ireg;
615 sdio_ireg = 0;
616 spin_unlock_irqrestore(&priv->driver_lock, flags);
Bing Zhao789221e2009-06-02 14:29:36 -0700617
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700618 sdio_claim_host(card->func);
619 if (ireg & DN_LD_HOST_INT_STATUS) {
Bing Zhao789221e2009-06-02 14:29:36 -0700620 if (priv->btmrvl_dev.tx_dnld_rdy)
621 BT_DBG("tx_done already received: "
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700622 " int_status=0x%x", ireg);
Bing Zhao789221e2009-06-02 14:29:36 -0700623 else
624 priv->btmrvl_dev.tx_dnld_rdy = true;
625 }
626
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700627 if (ireg & UP_LD_HOST_INT_STATUS)
Bing Zhao789221e2009-06-02 14:29:36 -0700628 btmrvl_sdio_card_to_host(priv);
629
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700630 sdio_release_host(card->func);
Bing Zhao789221e2009-06-02 14:29:36 -0700631
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700632 return 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700633}
634
635static void btmrvl_sdio_interrupt(struct sdio_func *func)
636{
637 struct btmrvl_private *priv;
Bing Zhao789221e2009-06-02 14:29:36 -0700638 struct btmrvl_sdio_card *card;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700639 ulong flags;
Bing Zhao789221e2009-06-02 14:29:36 -0700640 u8 ireg = 0;
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700641 int ret;
Bing Zhao789221e2009-06-02 14:29:36 -0700642
Bing Zhao789221e2009-06-02 14:29:36 -0700643 card = sdio_get_drvdata(func);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700644 if (!card || !card->priv) {
645 BT_ERR("sbi_interrupt(%p) card or priv is "
646 "NULL, card=%p\n", func, card);
647 return;
Bing Zhao789221e2009-06-02 14:29:36 -0700648 }
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700649
650 priv = card->priv;
651
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700652 ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700653 if (ret) {
654 BT_ERR("sdio_readb: read int status register failed");
655 return;
656 }
657
658 if (ireg != 0) {
659 /*
660 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
661 * Clear the interrupt status register and re-enable the
662 * interrupt.
663 */
664 BT_DBG("ireg = 0x%x", ireg);
665
666 sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
667 UP_LD_HOST_INT_STATUS),
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700668 card->reg->host_intstatus, &ret);
Amitkumar Karward1d10d72010-05-27 16:38:37 -0700669 if (ret) {
670 BT_ERR("sdio_writeb: clear int status register failed");
671 return;
672 }
673 }
674
675 spin_lock_irqsave(&priv->driver_lock, flags);
676 sdio_ireg |= ireg;
677 spin_unlock_irqrestore(&priv->driver_lock, flags);
678
679 btmrvl_interrupt(priv);
Bing Zhao789221e2009-06-02 14:29:36 -0700680}
681
682static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
683{
Bing Zhao789221e2009-06-02 14:29:36 -0700684 struct sdio_func *func;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200685 u8 reg;
686 int ret = 0;
Bing Zhao789221e2009-06-02 14:29:36 -0700687
Bing Zhao789221e2009-06-02 14:29:36 -0700688 if (!card || !card->func) {
689 BT_ERR("Error: card or function is NULL!");
690 ret = -EINVAL;
691 goto failed;
692 }
693
694 func = card->func;
695
Bing Zhao789221e2009-06-02 14:29:36 -0700696 sdio_claim_host(func);
697
698 ret = sdio_enable_func(func);
699 if (ret) {
700 BT_ERR("sdio_enable_func() failed: ret=%d", ret);
701 ret = -EIO;
702 goto release_host;
703 }
704
705 ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
706 if (ret) {
707 BT_ERR("sdio_claim_irq failed: ret=%d", ret);
708 ret = -EIO;
709 goto disable_func;
710 }
711
712 ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
713 if (ret) {
714 BT_ERR("cannot set SDIO block size");
715 ret = -EIO;
716 goto release_irq;
717 }
718
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700719 reg = sdio_readb(func, card->reg->io_port_0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700720 if (ret < 0) {
721 ret = -EIO;
722 goto release_irq;
723 }
724
725 card->ioport = reg;
726
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700727 reg = sdio_readb(func, card->reg->io_port_1, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700728 if (ret < 0) {
729 ret = -EIO;
730 goto release_irq;
731 }
732
733 card->ioport |= (reg << 8);
734
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700735 reg = sdio_readb(func, card->reg->io_port_2, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700736 if (ret < 0) {
737 ret = -EIO;
738 goto release_irq;
739 }
740
741 card->ioport |= (reg << 16);
742
743 BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
744
745 sdio_set_drvdata(func, card);
746
747 sdio_release_host(func);
748
Bing Zhao789221e2009-06-02 14:29:36 -0700749 return 0;
750
751release_irq:
752 sdio_release_irq(func);
753
754disable_func:
755 sdio_disable_func(func);
756
757release_host:
758 sdio_release_host(func);
759
760failed:
Bing Zhao789221e2009-06-02 14:29:36 -0700761 return ret;
762}
763
764static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
765{
Bing Zhao789221e2009-06-02 14:29:36 -0700766 if (card && card->func) {
767 sdio_claim_host(card->func);
768 sdio_release_irq(card->func);
769 sdio_disable_func(card->func);
770 sdio_release_host(card->func);
771 sdio_set_drvdata(card->func, NULL);
772 }
773
Bing Zhao789221e2009-06-02 14:29:36 -0700774 return 0;
775}
776
777static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
778{
779 int ret;
780
Marcel Holtmann93742532009-06-13 07:40:18 +0200781 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700782 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700783
784 sdio_claim_host(card->func);
785
786 ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
787
788 btmrvl_sdio_get_rx_unit(card);
789
790 sdio_release_host(card->func);
791
Bing Zhao789221e2009-06-02 14:29:36 -0700792 return ret;
793}
794
795static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
796{
797 int ret;
798
Marcel Holtmann93742532009-06-13 07:40:18 +0200799 if (!card || !card->func)
Bing Zhao789221e2009-06-02 14:29:36 -0700800 return -EINVAL;
Bing Zhao789221e2009-06-02 14:29:36 -0700801
802 sdio_claim_host(card->func);
803
804 ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
805
806 sdio_release_host(card->func);
807
Bing Zhao789221e2009-06-02 14:29:36 -0700808 return ret;
809}
810
811static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
812 u8 *payload, u16 nb)
813{
814 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
815 int ret = 0;
816 int buf_block_len;
817 int blksz;
818 int i = 0;
819 u8 *buf = NULL;
820 void *tmpbuf = NULL;
821 int tmpbufsz;
822
Bing Zhao789221e2009-06-02 14:29:36 -0700823 if (!card || !card->func) {
824 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700825 return -EINVAL;
826 }
827
828 buf = payload;
Bing Zhao3318b232009-07-08 11:44:14 -0700829 if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700830 tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
Julia Lawall59598092009-08-06 22:05:18 +0200831 tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
832 if (!tmpbuf)
833 return -ENOMEM;
Bing Zhao789221e2009-06-02 14:29:36 -0700834 buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
835 memcpy(buf, payload, nb);
836 }
837
838 blksz = SDIO_BLOCK_SIZE;
839 buf_block_len = (nb + blksz - 1) / blksz;
840
841 sdio_claim_host(card->func);
842
843 do {
844 /* Transfer data to card */
845 ret = sdio_writesb(card->func, card->ioport, buf,
846 buf_block_len * blksz);
847 if (ret < 0) {
848 i++;
849 BT_ERR("i=%d writesb failed: %d", i, ret);
850 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
851 payload, nb);
852 ret = -EIO;
853 if (i > MAX_WRITE_IOMEM_RETRY)
854 goto exit;
855 }
856 } while (ret);
857
858 priv->btmrvl_dev.tx_dnld_rdy = false;
859
860exit:
861 sdio_release_host(card->func);
Yoichi Yuasa88d1a0c2010-02-04 09:55:51 +0900862 kfree(tmpbuf);
Bing Zhao789221e2009-06-02 14:29:36 -0700863
Bing Zhao789221e2009-06-02 14:29:36 -0700864 return ret;
865}
866
867static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
868{
869 int ret = 0;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700870 u8 fws0;
871 int pollnum = MAX_POLL_TRIES;
Bing Zhao789221e2009-06-02 14:29:36 -0700872
Bing Zhao789221e2009-06-02 14:29:36 -0700873 if (!card || !card->func) {
874 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700875 return -EINVAL;
876 }
877 sdio_claim_host(card->func);
878
879 if (!btmrvl_sdio_verify_fw_download(card, 1)) {
880 BT_DBG("Firmware already downloaded!");
881 goto done;
882 }
883
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700884 /* Check if other function driver is downloading the firmware */
885 fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700886 if (ret) {
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700887 BT_ERR("Failed to read FW downloading status!");
Bing Zhao789221e2009-06-02 14:29:36 -0700888 ret = -EIO;
889 goto done;
890 }
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700891 if (fws0) {
892 BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
Bing Zhao789221e2009-06-02 14:29:36 -0700893
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700894 /* Give other function more time to download the firmware */
895 pollnum *= 10;
896 } else {
897 if (card->helper) {
898 ret = btmrvl_sdio_download_helper(card);
899 if (ret) {
900 BT_ERR("Failed to download helper!");
901 ret = -EIO;
902 goto done;
903 }
904 }
905
906 if (btmrvl_sdio_download_fw_w_helper(card)) {
907 BT_ERR("Failed to download firmware!");
908 ret = -EIO;
909 goto done;
910 }
Bing Zhao789221e2009-06-02 14:29:36 -0700911 }
912
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700913 if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
Bing Zhao789221e2009-06-02 14:29:36 -0700914 BT_ERR("FW failed to be active in time!");
915 ret = -ETIMEDOUT;
916 goto done;
917 }
918
919done:
920 sdio_release_host(card->func);
921
Bing Zhao789221e2009-06-02 14:29:36 -0700922 return ret;
923}
924
925static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
926{
927 struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
928 int ret = 0;
929
Bing Zhao789221e2009-06-02 14:29:36 -0700930 if (!card || !card->func) {
931 BT_ERR("card or function is NULL!");
Bing Zhao789221e2009-06-02 14:29:36 -0700932 return -EINVAL;
933 }
934
935 sdio_claim_host(card->func);
936
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700937 sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
Bing Zhao789221e2009-06-02 14:29:36 -0700938
939 sdio_release_host(card->func);
940
941 BT_DBG("wake up firmware");
942
Bing Zhao789221e2009-06-02 14:29:36 -0700943 return ret;
944}
945
946static int btmrvl_sdio_probe(struct sdio_func *func,
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200947 const struct sdio_device_id *id)
Bing Zhao789221e2009-06-02 14:29:36 -0700948{
949 int ret = 0;
950 struct btmrvl_private *priv = NULL;
951 struct btmrvl_sdio_card *card = NULL;
952
Bing Zhao789221e2009-06-02 14:29:36 -0700953 BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
954 id->vendor, id->device, id->class, func->num);
955
956 card = kzalloc(sizeof(*card), GFP_KERNEL);
957 if (!card) {
958 ret = -ENOMEM;
959 goto done;
960 }
961
962 card->func = func;
963
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200964 if (id->driver_data) {
965 struct btmrvl_sdio_device *data = (void *) id->driver_data;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700966 card->helper = data->helper;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200967 card->firmware = data->firmware;
Kevin Gan9f72c1d2011-04-08 18:19:33 -0700968 card->reg = data->reg;
969 card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
Marcel Holtmanndcf47f32009-06-09 16:21:58 +0200970 }
971
Bing Zhao789221e2009-06-02 14:29:36 -0700972 if (btmrvl_sdio_register_dev(card) < 0) {
973 BT_ERR("Failed to register BT device!");
974 ret = -ENODEV;
975 goto free_card;
976 }
977
978 /* Disable the interrupts on the card */
979 btmrvl_sdio_disable_host_int(card);
980
981 if (btmrvl_sdio_download_fw(card)) {
982 BT_ERR("Downloading firmware failed!");
983 ret = -ENODEV;
984 goto unreg_dev;
985 }
986
987 msleep(100);
988
989 btmrvl_sdio_enable_host_int(card);
990
991 priv = btmrvl_add_card(card);
992 if (!priv) {
993 BT_ERR("Initializing card failed!");
994 ret = -ENODEV;
995 goto disable_host_int;
996 }
997
998 card->priv = priv;
999
1000 /* Initialize the interface specific function pointers */
1001 priv->hw_host_to_card = btmrvl_sdio_host_to_card;
1002 priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
Amitkumar Karward1d10d72010-05-27 16:38:37 -07001003 priv->hw_process_int_status = btmrvl_sdio_process_int_status;
Bing Zhao789221e2009-06-02 14:29:36 -07001004
Bing Zhao64061602010-03-03 14:37:36 -08001005 if (btmrvl_register_hdev(priv)) {
1006 BT_ERR("Register hdev failed!");
1007 ret = -ENODEV;
1008 goto disable_host_int;
1009 }
1010
Amitkumar Karwar32fd6392009-09-30 11:18:30 -07001011 priv->btmrvl_dev.psmode = 1;
1012 btmrvl_enable_ps(priv);
Bing Zhao789221e2009-06-02 14:29:36 -07001013
Amitkumar Karwar2a8ff612012-04-24 16:31:40 -07001014 priv->btmrvl_dev.gpio_gap = 0xffff;
1015 btmrvl_send_hscfg_cmd(priv);
1016
Bing Zhao789221e2009-06-02 14:29:36 -07001017 return 0;
1018
1019disable_host_int:
1020 btmrvl_sdio_disable_host_int(card);
1021unreg_dev:
1022 btmrvl_sdio_unregister_dev(card);
1023free_card:
1024 kfree(card);
1025done:
Bing Zhao789221e2009-06-02 14:29:36 -07001026 return ret;
1027}
1028
1029static void btmrvl_sdio_remove(struct sdio_func *func)
1030{
1031 struct btmrvl_sdio_card *card;
1032
Bing Zhao789221e2009-06-02 14:29:36 -07001033 if (func) {
1034 card = sdio_get_drvdata(func);
1035 if (card) {
1036 /* Send SHUTDOWN command & disable interrupt
1037 * if user removes the module.
1038 */
1039 if (user_rmmod) {
1040 btmrvl_send_module_cfg_cmd(card->priv,
1041 MODULE_SHUTDOWN_REQ);
1042 btmrvl_sdio_disable_host_int(card);
1043 }
1044 BT_DBG("unregester dev");
1045 btmrvl_sdio_unregister_dev(card);
1046 btmrvl_remove_card(card->priv);
1047 kfree(card);
1048 }
1049 }
Bing Zhao789221e2009-06-02 14:29:36 -07001050}
1051
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001052static int btmrvl_sdio_suspend(struct device *dev)
1053{
1054 struct sdio_func *func = dev_to_sdio_func(dev);
1055 struct btmrvl_sdio_card *card;
1056 struct btmrvl_private *priv;
1057 mmc_pm_flag_t pm_flags;
1058 struct hci_dev *hcidev;
1059
1060 if (func) {
1061 pm_flags = sdio_get_host_pm_caps(func);
1062 BT_DBG("%s: suspend: PM flags = 0x%x", sdio_func_id(func),
1063 pm_flags);
1064 if (!(pm_flags & MMC_PM_KEEP_POWER)) {
1065 BT_ERR("%s: cannot remain alive while suspended",
1066 sdio_func_id(func));
1067 return -ENOSYS;
1068 }
1069 card = sdio_get_drvdata(func);
1070 if (!card || !card->priv) {
1071 BT_ERR("card or priv structure is not valid");
1072 return 0;
1073 }
1074 } else {
1075 BT_ERR("sdio_func is not specified");
1076 return 0;
1077 }
1078
1079 priv = card->priv;
1080
1081 if (priv->adapter->hs_state != HS_ACTIVATED) {
1082 if (btmrvl_enable_hs(priv)) {
1083 BT_ERR("HS not actived, suspend failed!");
1084 return -EBUSY;
1085 }
1086 }
1087 hcidev = priv->btmrvl_dev.hcidev;
1088 BT_DBG("%s: SDIO suspend", hcidev->name);
1089 hci_suspend_dev(hcidev);
1090 skb_queue_purge(&priv->adapter->tx_queue);
1091
1092 priv->adapter->is_suspended = true;
1093
1094 /* We will keep the power when hs enabled successfully */
1095 if (priv->adapter->hs_state == HS_ACTIVATED) {
1096 BT_DBG("suspend with MMC_PM_KEEP_POWER");
1097 return sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1098 } else {
1099 BT_DBG("suspend without MMC_PM_KEEP_POWER");
1100 return 0;
1101 }
1102}
1103
1104static int btmrvl_sdio_resume(struct device *dev)
1105{
1106 struct sdio_func *func = dev_to_sdio_func(dev);
1107 struct btmrvl_sdio_card *card;
1108 struct btmrvl_private *priv;
1109 mmc_pm_flag_t pm_flags;
1110 struct hci_dev *hcidev;
1111
1112 if (func) {
1113 pm_flags = sdio_get_host_pm_caps(func);
1114 BT_DBG("%s: resume: PM flags = 0x%x", sdio_func_id(func),
1115 pm_flags);
1116 card = sdio_get_drvdata(func);
1117 if (!card || !card->priv) {
1118 BT_ERR("card or priv structure is not valid");
1119 return 0;
1120 }
1121 } else {
1122 BT_ERR("sdio_func is not specified");
1123 return 0;
1124 }
1125 priv = card->priv;
1126
1127 if (!priv->adapter->is_suspended) {
1128 BT_DBG("device already resumed");
1129 return 0;
1130 }
1131
1132 priv->adapter->is_suspended = false;
1133 hcidev = priv->btmrvl_dev.hcidev;
1134 BT_DBG("%s: SDIO resume", hcidev->name);
1135 hci_resume_dev(hcidev);
1136 priv->hw_wakeup_firmware(priv);
1137 priv->adapter->hs_state = HS_DEACTIVATED;
1138 BT_DBG("%s: HS DEACTIVATED in resume!", hcidev->name);
1139
1140 return 0;
1141}
1142
1143static const struct dev_pm_ops btmrvl_sdio_pm_ops = {
1144 .suspend = btmrvl_sdio_suspend,
1145 .resume = btmrvl_sdio_resume,
1146};
1147
Bing Zhao789221e2009-06-02 14:29:36 -07001148static struct sdio_driver bt_mrvl_sdio = {
1149 .name = "btmrvl_sdio",
1150 .id_table = btmrvl_sdio_ids,
1151 .probe = btmrvl_sdio_probe,
1152 .remove = btmrvl_sdio_remove,
Amitkumar Karwarba54a162012-04-25 11:43:54 -07001153 .drv = {
1154 .owner = THIS_MODULE,
1155 .pm = &btmrvl_sdio_pm_ops,
1156 }
Bing Zhao789221e2009-06-02 14:29:36 -07001157};
1158
Peter Huewe8e5b2302009-12-22 09:34:20 +01001159static int __init btmrvl_sdio_init_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001160{
Bing Zhao789221e2009-06-02 14:29:36 -07001161 if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
1162 BT_ERR("SDIO Driver Registration Failed");
Bing Zhao789221e2009-06-02 14:29:36 -07001163 return -ENODEV;
1164 }
1165
1166 /* Clear the flag in case user removes the card. */
1167 user_rmmod = 0;
1168
Bing Zhao789221e2009-06-02 14:29:36 -07001169 return 0;
1170}
1171
Peter Huewe8e5b2302009-12-22 09:34:20 +01001172static void __exit btmrvl_sdio_exit_module(void)
Bing Zhao789221e2009-06-02 14:29:36 -07001173{
Bing Zhao789221e2009-06-02 14:29:36 -07001174 /* Set the flag as user is removing this module. */
1175 user_rmmod = 1;
1176
1177 sdio_unregister_driver(&bt_mrvl_sdio);
Bing Zhao789221e2009-06-02 14:29:36 -07001178}
1179
1180module_init(btmrvl_sdio_init_module);
1181module_exit(btmrvl_sdio_exit_module);
1182
1183MODULE_AUTHOR("Marvell International Ltd.");
Marcel Holtmann9666fb32009-06-09 21:45:04 +02001184MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
Bing Zhao789221e2009-06-02 14:29:36 -07001185MODULE_VERSION(VERSION);
1186MODULE_LICENSE("GPL v2");
Ben Hutchings28614532009-11-07 21:41:18 +00001187MODULE_FIRMWARE("sd8688_helper.bin");
1188MODULE_FIRMWARE("sd8688.bin");
Kevin Gan9f72c1d2011-04-08 18:19:33 -07001189MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
Bing Zhao2ac654f2011-11-16 20:40:42 -08001190MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");