Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1 | /** |
| 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> |
| 22 | |
| 23 | #include <linux/mmc/sdio_ids.h> |
| 24 | #include <linux/mmc/sdio_func.h> |
| 25 | |
| 26 | #include <net/bluetooth/bluetooth.h> |
| 27 | #include <net/bluetooth/hci_core.h> |
| 28 | |
| 29 | #include "btmrvl_drv.h" |
| 30 | #include "btmrvl_sdio.h" |
| 31 | |
| 32 | #define VERSION "1.0" |
| 33 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 34 | /* The btmrvl_sdio_remove() callback function is called |
| 35 | * when user removes this module from kernel space or ejects |
| 36 | * the card from the slot. The driver handles these 2 cases |
| 37 | * differently. |
| 38 | * If the user is removing the module, a MODULE_SHUTDOWN_REQ |
| 39 | * command is sent to firmware and interrupt will be disabled. |
| 40 | * If the card is removed, there is no need to send command |
| 41 | * or disable interrupt. |
| 42 | * |
| 43 | * The variable 'user_rmmod' is used to distinguish these two |
| 44 | * scenarios. This flag is initialized as FALSE in case the card |
| 45 | * is removed, and will be set to TRUE for module removal when |
| 46 | * module_exit function is called. |
| 47 | */ |
| 48 | static u8 user_rmmod; |
| 49 | |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 50 | static const struct btmrvl_sdio_device btmrvl_sdio_sd6888 = { |
| 51 | .helper = "sd8688_helper.bin", |
| 52 | .firmware = "sd8688.bin", |
| 53 | }; |
| 54 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 55 | static const struct sdio_device_id btmrvl_sdio_ids[] = { |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 56 | /* Marvell SD8688 Bluetooth device */ |
| 57 | { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105), |
| 58 | .driver_data = (unsigned long) &btmrvl_sdio_sd6888 }, |
| 59 | |
| 60 | { } /* Terminating entry */ |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids); |
| 64 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 65 | static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card) |
| 66 | { |
| 67 | u8 reg; |
| 68 | int ret; |
| 69 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 70 | reg = sdio_readb(card->func, CARD_RX_UNIT_REG, &ret); |
| 71 | if (!ret) |
| 72 | card->rx_unit = reg; |
| 73 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat) |
| 78 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 79 | u8 fws0, fws1; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 80 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 81 | |
| 82 | *dat = 0; |
| 83 | |
| 84 | fws0 = sdio_readb(card->func, CARD_FW_STATUS0_REG, &ret); |
| 85 | |
| 86 | if (!ret) |
| 87 | fws1 = sdio_readb(card->func, CARD_FW_STATUS1_REG, &ret); |
| 88 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 89 | if (ret) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 90 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 91 | |
| 92 | *dat = (((u16) fws1) << 8) | fws0; |
| 93 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat) |
| 98 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 99 | u8 reg; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 100 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 101 | |
| 102 | reg = sdio_readb(card->func, CARD_RX_LEN_REG, &ret); |
| 103 | if (!ret) |
| 104 | *dat = (u16) reg << card->rx_unit; |
| 105 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 110 | u8 mask) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 111 | { |
| 112 | int ret; |
| 113 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 114 | sdio_writeb(card->func, mask, HOST_INT_MASK_REG, &ret); |
| 115 | if (ret) { |
| 116 | BT_ERR("Unable to enable the host interrupt!"); |
| 117 | ret = -EIO; |
| 118 | } |
| 119 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 124 | u8 mask) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 125 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 126 | u8 host_int_mask; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 127 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 128 | |
| 129 | host_int_mask = sdio_readb(card->func, HOST_INT_MASK_REG, &ret); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 130 | if (ret) |
| 131 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 132 | |
| 133 | host_int_mask &= ~mask; |
| 134 | |
| 135 | sdio_writeb(card->func, host_int_mask, HOST_INT_MASK_REG, &ret); |
| 136 | if (ret < 0) { |
| 137 | BT_ERR("Unable to disable the host interrupt!"); |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 138 | return -EIO; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 141 | return 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits) |
| 145 | { |
| 146 | unsigned int tries; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 147 | u8 status; |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 148 | int ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 149 | |
| 150 | for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) { |
| 151 | status = sdio_readb(card->func, CARD_STATUS_REG, &ret); |
| 152 | if (ret) |
| 153 | goto failed; |
| 154 | if ((status & bits) == bits) |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 155 | return ret; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 156 | |
| 157 | udelay(1); |
| 158 | } |
| 159 | |
| 160 | ret = -ETIMEDOUT; |
| 161 | |
| 162 | failed: |
| 163 | BT_ERR("FAILED! ret=%d", ret); |
| 164 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 169 | int pollnum) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 170 | { |
| 171 | int ret = -ETIMEDOUT; |
| 172 | u16 firmwarestat; |
| 173 | unsigned int tries; |
| 174 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 175 | /* Wait for firmware to become ready */ |
| 176 | for (tries = 0; tries < pollnum; tries++) { |
| 177 | if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0) |
| 178 | continue; |
| 179 | |
| 180 | if (firmwarestat == FIRMWARE_READY) { |
| 181 | ret = 0; |
| 182 | break; |
| 183 | } else { |
| 184 | msleep(10); |
| 185 | } |
| 186 | } |
| 187 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card) |
| 192 | { |
| 193 | const struct firmware *fw_helper = NULL; |
| 194 | const u8 *helper = NULL; |
| 195 | int ret; |
| 196 | void *tmphlprbuf = NULL; |
| 197 | int tmphlprbufsz, hlprblknow, helperlen; |
| 198 | u8 *helperbuf; |
| 199 | u32 tx_len; |
| 200 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 201 | ret = request_firmware(&fw_helper, card->helper, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 202 | &card->func->dev); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 203 | if ((ret < 0) || !fw_helper) { |
| 204 | BT_ERR("request_firmware(helper) failed, error code = %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 205 | ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 206 | ret = -ENOENT; |
| 207 | goto done; |
| 208 | } |
| 209 | |
| 210 | helper = fw_helper->data; |
| 211 | helperlen = fw_helper->size; |
| 212 | |
| 213 | BT_DBG("Downloading helper image (%d bytes), block size %d bytes", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 214 | helperlen, SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 215 | |
| 216 | tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); |
| 217 | |
| 218 | tmphlprbuf = kmalloc(tmphlprbufsz, GFP_KERNEL); |
| 219 | if (!tmphlprbuf) { |
| 220 | BT_ERR("Unable to allocate buffer for helper." |
| 221 | " Terminating download"); |
| 222 | ret = -ENOMEM; |
| 223 | goto done; |
| 224 | } |
| 225 | |
| 226 | memset(tmphlprbuf, 0, tmphlprbufsz); |
| 227 | |
| 228 | helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN); |
| 229 | |
| 230 | /* Perform helper data transfer */ |
| 231 | tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE) |
| 232 | - SDIO_HEADER_LEN; |
| 233 | hlprblknow = 0; |
| 234 | |
| 235 | do { |
| 236 | ret = btmrvl_sdio_poll_card_status(card, |
| 237 | CARD_IO_READY | DN_LD_CARD_RDY); |
| 238 | if (ret < 0) { |
| 239 | BT_ERR("Helper download poll status timeout @ %d", |
| 240 | hlprblknow); |
| 241 | goto done; |
| 242 | } |
| 243 | |
| 244 | /* Check if there is more data? */ |
| 245 | if (hlprblknow >= helperlen) |
| 246 | break; |
| 247 | |
| 248 | if (helperlen - hlprblknow < tx_len) |
| 249 | tx_len = helperlen - hlprblknow; |
| 250 | |
| 251 | /* Little-endian */ |
| 252 | helperbuf[0] = ((tx_len & 0x000000ff) >> 0); |
| 253 | helperbuf[1] = ((tx_len & 0x0000ff00) >> 8); |
| 254 | helperbuf[2] = ((tx_len & 0x00ff0000) >> 16); |
| 255 | helperbuf[3] = ((tx_len & 0xff000000) >> 24); |
| 256 | |
| 257 | memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow], |
| 258 | tx_len); |
| 259 | |
| 260 | /* Now send the data */ |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 261 | ret = sdio_writesb(card->func, card->ioport, helperbuf, |
| 262 | FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 263 | if (ret < 0) { |
| 264 | BT_ERR("IO error during helper download @ %d", |
| 265 | hlprblknow); |
| 266 | goto done; |
| 267 | } |
| 268 | |
| 269 | hlprblknow += tx_len; |
| 270 | } while (true); |
| 271 | |
| 272 | BT_DBG("Transferring helper image EOF block"); |
| 273 | |
| 274 | memset(helperbuf, 0x0, SDIO_BLOCK_SIZE); |
| 275 | |
| 276 | ret = sdio_writesb(card->func, card->ioport, helperbuf, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 277 | SDIO_BLOCK_SIZE); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 278 | if (ret < 0) { |
| 279 | BT_ERR("IO error in writing helper image EOF block"); |
| 280 | goto done; |
| 281 | } |
| 282 | |
| 283 | ret = 0; |
| 284 | |
| 285 | done: |
| 286 | kfree(tmphlprbuf); |
| 287 | if (fw_helper) |
| 288 | release_firmware(fw_helper); |
| 289 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card) |
| 294 | { |
| 295 | const struct firmware *fw_firmware = NULL; |
| 296 | const u8 *firmware = NULL; |
| 297 | int firmwarelen, tmpfwbufsz, ret; |
| 298 | unsigned int tries, offset; |
| 299 | u8 base0, base1; |
| 300 | void *tmpfwbuf = NULL; |
| 301 | u8 *fwbuf; |
| 302 | u16 len; |
| 303 | int txlen = 0, tx_blocks = 0, count = 0; |
| 304 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 305 | ret = request_firmware(&fw_firmware, card->firmware, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 306 | &card->func->dev); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 307 | if ((ret < 0) || !fw_firmware) { |
| 308 | BT_ERR("request_firmware(firmware) failed, error code = %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 309 | ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 310 | ret = -ENOENT; |
| 311 | goto done; |
| 312 | } |
| 313 | |
| 314 | firmware = fw_firmware->data; |
| 315 | firmwarelen = fw_firmware->size; |
| 316 | |
| 317 | BT_DBG("Downloading FW image (%d bytes)", firmwarelen); |
| 318 | |
| 319 | tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN); |
| 320 | tmpfwbuf = kmalloc(tmpfwbufsz, GFP_KERNEL); |
| 321 | if (!tmpfwbuf) { |
| 322 | BT_ERR("Unable to allocate buffer for firmware." |
| 323 | " Terminating download"); |
| 324 | ret = -ENOMEM; |
| 325 | goto done; |
| 326 | } |
| 327 | |
| 328 | memset(tmpfwbuf, 0, tmpfwbufsz); |
| 329 | |
| 330 | /* Ensure aligned firmware buffer */ |
| 331 | fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN); |
| 332 | |
| 333 | /* Perform firmware data transfer */ |
| 334 | offset = 0; |
| 335 | do { |
| 336 | ret = btmrvl_sdio_poll_card_status(card, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 337 | CARD_IO_READY | DN_LD_CARD_RDY); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 338 | if (ret < 0) { |
| 339 | BT_ERR("FW download with helper poll status" |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 340 | " timeout @ %d", offset); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 341 | goto done; |
| 342 | } |
| 343 | |
| 344 | /* Check if there is more data ? */ |
| 345 | if (offset >= firmwarelen) |
| 346 | break; |
| 347 | |
| 348 | for (tries = 0; tries < MAX_POLL_TRIES; tries++) { |
| 349 | base0 = sdio_readb(card->func, |
| 350 | SQ_READ_BASE_ADDRESS_A0_REG, &ret); |
| 351 | if (ret) { |
| 352 | BT_ERR("BASE0 register read failed:" |
| 353 | " base0 = 0x%04X(%d)." |
| 354 | " Terminating download", |
| 355 | base0, base0); |
| 356 | ret = -EIO; |
| 357 | goto done; |
| 358 | } |
| 359 | base1 = sdio_readb(card->func, |
| 360 | SQ_READ_BASE_ADDRESS_A1_REG, &ret); |
| 361 | if (ret) { |
| 362 | BT_ERR("BASE1 register read failed:" |
| 363 | " base1 = 0x%04X(%d)." |
| 364 | " Terminating download", |
| 365 | base1, base1); |
| 366 | ret = -EIO; |
| 367 | goto done; |
| 368 | } |
| 369 | |
| 370 | len = (((u16) base1) << 8) | base0; |
| 371 | if (len) |
| 372 | break; |
| 373 | |
| 374 | udelay(10); |
| 375 | } |
| 376 | |
| 377 | if (!len) |
| 378 | break; |
| 379 | else if (len > BTM_UPLD_SIZE) { |
| 380 | BT_ERR("FW download failure @%d, invalid length %d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 381 | offset, len); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 382 | ret = -EINVAL; |
| 383 | goto done; |
| 384 | } |
| 385 | |
| 386 | txlen = len; |
| 387 | |
| 388 | if (len & BIT(0)) { |
| 389 | count++; |
| 390 | if (count > MAX_WRITE_IOMEM_RETRY) { |
| 391 | BT_ERR("FW download failure @%d, " |
| 392 | "over max retry count", offset); |
| 393 | ret = -EIO; |
| 394 | goto done; |
| 395 | } |
| 396 | BT_ERR("FW CRC error indicated by the helper: " |
| 397 | "len = 0x%04X, txlen = %d", len, txlen); |
| 398 | len &= ~BIT(0); |
| 399 | /* Set txlen to 0 so as to resend from same offset */ |
| 400 | txlen = 0; |
| 401 | } else { |
| 402 | count = 0; |
| 403 | |
| 404 | /* Last block ? */ |
| 405 | if (firmwarelen - offset < txlen) |
| 406 | txlen = firmwarelen - offset; |
| 407 | |
| 408 | tx_blocks = |
| 409 | (txlen + SDIO_BLOCK_SIZE - 1) / SDIO_BLOCK_SIZE; |
| 410 | |
| 411 | memcpy(fwbuf, &firmware[offset], txlen); |
| 412 | } |
| 413 | |
| 414 | ret = sdio_writesb(card->func, card->ioport, fwbuf, |
| 415 | tx_blocks * SDIO_BLOCK_SIZE); |
| 416 | |
| 417 | if (ret < 0) { |
| 418 | BT_ERR("FW download, writesb(%d) failed @%d", |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 419 | count, offset); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 420 | sdio_writeb(card->func, HOST_CMD53_FIN, CONFIG_REG, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 421 | &ret); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 422 | if (ret) |
| 423 | BT_ERR("writeb failed (CFG)"); |
| 424 | } |
| 425 | |
| 426 | offset += txlen; |
| 427 | } while (true); |
| 428 | |
| 429 | BT_DBG("FW download over, size %d bytes", offset); |
| 430 | |
| 431 | ret = 0; |
| 432 | |
| 433 | done: |
| 434 | kfree(tmpfwbuf); |
| 435 | |
| 436 | if (fw_firmware) |
| 437 | release_firmware(fw_firmware); |
| 438 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv) |
| 443 | { |
| 444 | u16 buf_len = 0; |
| 445 | int ret, buf_block_len, blksz; |
| 446 | struct sk_buff *skb = NULL; |
| 447 | u32 type; |
| 448 | u8 *payload = NULL; |
| 449 | struct hci_dev *hdev = priv->btmrvl_dev.hcidev; |
| 450 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 451 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 452 | if (!card || !card->func) { |
| 453 | BT_ERR("card or function is NULL!"); |
| 454 | ret = -EINVAL; |
| 455 | goto exit; |
| 456 | } |
| 457 | |
| 458 | /* Read the length of data to be transferred */ |
| 459 | ret = btmrvl_sdio_read_rx_len(card, &buf_len); |
| 460 | if (ret < 0) { |
| 461 | BT_ERR("read rx_len failed"); |
| 462 | ret = -EIO; |
| 463 | goto exit; |
| 464 | } |
| 465 | |
| 466 | blksz = SDIO_BLOCK_SIZE; |
| 467 | buf_block_len = (buf_len + blksz - 1) / blksz; |
| 468 | |
| 469 | if (buf_len <= SDIO_HEADER_LEN |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 470 | || (buf_block_len * blksz) > ALLOC_BUF_SIZE) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 471 | BT_ERR("invalid packet length: %d", buf_len); |
| 472 | ret = -EINVAL; |
| 473 | goto exit; |
| 474 | } |
| 475 | |
| 476 | /* Allocate buffer */ |
| 477 | skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 478 | GFP_ATOMIC); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 479 | if (skb == NULL) { |
| 480 | BT_ERR("No free skb"); |
| 481 | goto exit; |
| 482 | } |
| 483 | |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 484 | if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) { |
| 485 | skb_put(skb, (unsigned long) skb->data & |
| 486 | (BTSDIO_DMA_ALIGN - 1)); |
| 487 | skb_pull(skb, (unsigned long) skb->data & |
| 488 | (BTSDIO_DMA_ALIGN - 1)); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 491 | payload = skb->data; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 492 | |
| 493 | ret = sdio_readsb(card->func, payload, card->ioport, |
| 494 | buf_block_len * blksz); |
| 495 | if (ret < 0) { |
| 496 | BT_ERR("readsb failed: %d", ret); |
| 497 | ret = -EIO; |
| 498 | goto exit; |
| 499 | } |
| 500 | |
| 501 | /* This is SDIO specific header length: byte[2][1][0], type: byte[3] |
| 502 | * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor) |
| 503 | */ |
| 504 | |
| 505 | buf_len = payload[0]; |
| 506 | buf_len |= (u16) payload[1] << 8; |
| 507 | type = payload[3]; |
| 508 | |
| 509 | switch (type) { |
| 510 | case HCI_ACLDATA_PKT: |
| 511 | case HCI_SCODATA_PKT: |
| 512 | case HCI_EVENT_PKT: |
| 513 | bt_cb(skb)->pkt_type = type; |
| 514 | skb->dev = (void *)hdev; |
| 515 | skb_put(skb, buf_len); |
| 516 | skb_pull(skb, SDIO_HEADER_LEN); |
| 517 | |
| 518 | if (type == HCI_EVENT_PKT) |
| 519 | btmrvl_check_evtpkt(priv, skb); |
| 520 | |
| 521 | hci_recv_frame(skb); |
| 522 | hdev->stat.byte_rx += buf_len; |
| 523 | break; |
| 524 | |
| 525 | case MRVL_VENDOR_PKT: |
| 526 | bt_cb(skb)->pkt_type = HCI_VENDOR_PKT; |
| 527 | skb->dev = (void *)hdev; |
| 528 | skb_put(skb, buf_len); |
| 529 | skb_pull(skb, SDIO_HEADER_LEN); |
| 530 | |
| 531 | if (btmrvl_process_event(priv, skb)) |
| 532 | hci_recv_frame(skb); |
| 533 | |
| 534 | hdev->stat.byte_rx += buf_len; |
| 535 | break; |
| 536 | |
| 537 | default: |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 538 | BT_ERR("Unknown packet type:%d", type); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 539 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 540 | blksz * buf_block_len); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 541 | |
| 542 | kfree_skb(skb); |
| 543 | skb = NULL; |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | exit: |
| 548 | if (ret) { |
| 549 | hdev->stat.err_rx++; |
| 550 | if (skb) |
| 551 | kfree_skb(skb); |
| 552 | } |
| 553 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | static int btmrvl_sdio_get_int_status(struct btmrvl_private *priv, u8 * ireg) |
| 558 | { |
| 559 | int ret; |
| 560 | u8 sdio_ireg = 0; |
| 561 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 562 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 563 | *ireg = 0; |
| 564 | |
| 565 | sdio_ireg = sdio_readb(card->func, HOST_INTSTATUS_REG, &ret); |
| 566 | if (ret) { |
| 567 | BT_ERR("sdio_readb: read int status register failed"); |
| 568 | ret = -EIO; |
| 569 | goto done; |
| 570 | } |
| 571 | |
| 572 | if (sdio_ireg != 0) { |
| 573 | /* |
| 574 | * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS |
| 575 | * Clear the interrupt status register and re-enable the |
| 576 | * interrupt. |
| 577 | */ |
| 578 | BT_DBG("sdio_ireg = 0x%x", sdio_ireg); |
| 579 | |
| 580 | sdio_writeb(card->func, ~(sdio_ireg) & (DN_LD_HOST_INT_STATUS | |
| 581 | UP_LD_HOST_INT_STATUS), |
| 582 | HOST_INTSTATUS_REG, &ret); |
| 583 | if (ret) { |
| 584 | BT_ERR("sdio_writeb: clear int status register " |
| 585 | "failed"); |
| 586 | ret = -EIO; |
| 587 | goto done; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | if (sdio_ireg & DN_LD_HOST_INT_STATUS) { |
| 592 | if (priv->btmrvl_dev.tx_dnld_rdy) |
| 593 | BT_DBG("tx_done already received: " |
| 594 | " int_status=0x%x", sdio_ireg); |
| 595 | else |
| 596 | priv->btmrvl_dev.tx_dnld_rdy = true; |
| 597 | } |
| 598 | |
| 599 | if (sdio_ireg & UP_LD_HOST_INT_STATUS) |
| 600 | btmrvl_sdio_card_to_host(priv); |
| 601 | |
| 602 | *ireg = sdio_ireg; |
| 603 | |
| 604 | ret = 0; |
| 605 | |
| 606 | done: |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | static void btmrvl_sdio_interrupt(struct sdio_func *func) |
| 611 | { |
| 612 | struct btmrvl_private *priv; |
| 613 | struct hci_dev *hcidev; |
| 614 | struct btmrvl_sdio_card *card; |
| 615 | u8 ireg = 0; |
| 616 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 617 | card = sdio_get_drvdata(func); |
| 618 | if (card && card->priv) { |
| 619 | priv = card->priv; |
| 620 | hcidev = priv->btmrvl_dev.hcidev; |
| 621 | |
| 622 | if (btmrvl_sdio_get_int_status(priv, &ireg)) |
| 623 | BT_ERR("reading HOST_INT_STATUS_REG failed"); |
| 624 | else |
| 625 | BT_DBG("HOST_INT_STATUS_REG %#x", ireg); |
| 626 | |
| 627 | btmrvl_interrupt(priv); |
| 628 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card) |
| 632 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 633 | struct sdio_func *func; |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 634 | u8 reg; |
| 635 | int ret = 0; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 636 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 637 | if (!card || !card->func) { |
| 638 | BT_ERR("Error: card or function is NULL!"); |
| 639 | ret = -EINVAL; |
| 640 | goto failed; |
| 641 | } |
| 642 | |
| 643 | func = card->func; |
| 644 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 645 | sdio_claim_host(func); |
| 646 | |
| 647 | ret = sdio_enable_func(func); |
| 648 | if (ret) { |
| 649 | BT_ERR("sdio_enable_func() failed: ret=%d", ret); |
| 650 | ret = -EIO; |
| 651 | goto release_host; |
| 652 | } |
| 653 | |
| 654 | ret = sdio_claim_irq(func, btmrvl_sdio_interrupt); |
| 655 | if (ret) { |
| 656 | BT_ERR("sdio_claim_irq failed: ret=%d", ret); |
| 657 | ret = -EIO; |
| 658 | goto disable_func; |
| 659 | } |
| 660 | |
| 661 | ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE); |
| 662 | if (ret) { |
| 663 | BT_ERR("cannot set SDIO block size"); |
| 664 | ret = -EIO; |
| 665 | goto release_irq; |
| 666 | } |
| 667 | |
| 668 | reg = sdio_readb(func, IO_PORT_0_REG, &ret); |
| 669 | if (ret < 0) { |
| 670 | ret = -EIO; |
| 671 | goto release_irq; |
| 672 | } |
| 673 | |
| 674 | card->ioport = reg; |
| 675 | |
| 676 | reg = sdio_readb(func, IO_PORT_1_REG, &ret); |
| 677 | if (ret < 0) { |
| 678 | ret = -EIO; |
| 679 | goto release_irq; |
| 680 | } |
| 681 | |
| 682 | card->ioport |= (reg << 8); |
| 683 | |
| 684 | reg = sdio_readb(func, IO_PORT_2_REG, &ret); |
| 685 | if (ret < 0) { |
| 686 | ret = -EIO; |
| 687 | goto release_irq; |
| 688 | } |
| 689 | |
| 690 | card->ioport |= (reg << 16); |
| 691 | |
| 692 | BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport); |
| 693 | |
| 694 | sdio_set_drvdata(func, card); |
| 695 | |
| 696 | sdio_release_host(func); |
| 697 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 698 | return 0; |
| 699 | |
| 700 | release_irq: |
| 701 | sdio_release_irq(func); |
| 702 | |
| 703 | disable_func: |
| 704 | sdio_disable_func(func); |
| 705 | |
| 706 | release_host: |
| 707 | sdio_release_host(func); |
| 708 | |
| 709 | failed: |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card) |
| 714 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 715 | if (card && card->func) { |
| 716 | sdio_claim_host(card->func); |
| 717 | sdio_release_irq(card->func); |
| 718 | sdio_disable_func(card->func); |
| 719 | sdio_release_host(card->func); |
| 720 | sdio_set_drvdata(card->func, NULL); |
| 721 | } |
| 722 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card) |
| 727 | { |
| 728 | int ret; |
| 729 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 730 | if (!card || !card->func) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 731 | return -EINVAL; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 732 | |
| 733 | sdio_claim_host(card->func); |
| 734 | |
| 735 | ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE); |
| 736 | |
| 737 | btmrvl_sdio_get_rx_unit(card); |
| 738 | |
| 739 | sdio_release_host(card->func); |
| 740 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card) |
| 745 | { |
| 746 | int ret; |
| 747 | |
Marcel Holtmann | 9374253 | 2009-06-13 07:40:18 +0200 | [diff] [blame] | 748 | if (!card || !card->func) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 749 | return -EINVAL; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 750 | |
| 751 | sdio_claim_host(card->func); |
| 752 | |
| 753 | ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE); |
| 754 | |
| 755 | sdio_release_host(card->func); |
| 756 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 757 | return ret; |
| 758 | } |
| 759 | |
| 760 | static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv, |
| 761 | u8 *payload, u16 nb) |
| 762 | { |
| 763 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 764 | int ret = 0; |
| 765 | int buf_block_len; |
| 766 | int blksz; |
| 767 | int i = 0; |
| 768 | u8 *buf = NULL; |
| 769 | void *tmpbuf = NULL; |
| 770 | int tmpbufsz; |
| 771 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 772 | if (!card || !card->func) { |
| 773 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 774 | return -EINVAL; |
| 775 | } |
| 776 | |
| 777 | buf = payload; |
Bing Zhao | 3318b23 | 2009-07-08 11:44:14 -0700 | [diff] [blame] | 778 | if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 779 | tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN); |
Julia Lawall | 5959809 | 2009-08-06 22:05:18 +0200 | [diff] [blame] | 780 | tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL); |
| 781 | if (!tmpbuf) |
| 782 | return -ENOMEM; |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 783 | buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN); |
| 784 | memcpy(buf, payload, nb); |
| 785 | } |
| 786 | |
| 787 | blksz = SDIO_BLOCK_SIZE; |
| 788 | buf_block_len = (nb + blksz - 1) / blksz; |
| 789 | |
| 790 | sdio_claim_host(card->func); |
| 791 | |
| 792 | do { |
| 793 | /* Transfer data to card */ |
| 794 | ret = sdio_writesb(card->func, card->ioport, buf, |
| 795 | buf_block_len * blksz); |
| 796 | if (ret < 0) { |
| 797 | i++; |
| 798 | BT_ERR("i=%d writesb failed: %d", i, ret); |
| 799 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 800 | payload, nb); |
| 801 | ret = -EIO; |
| 802 | if (i > MAX_WRITE_IOMEM_RETRY) |
| 803 | goto exit; |
| 804 | } |
| 805 | } while (ret); |
| 806 | |
| 807 | priv->btmrvl_dev.tx_dnld_rdy = false; |
| 808 | |
| 809 | exit: |
| 810 | sdio_release_host(card->func); |
Yoichi Yuasa | 88d1a0c | 2010-02-04 09:55:51 +0900 | [diff] [blame] | 811 | kfree(tmpbuf); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 812 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card) |
| 817 | { |
| 818 | int ret = 0; |
| 819 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 820 | if (!card || !card->func) { |
| 821 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 822 | return -EINVAL; |
| 823 | } |
| 824 | sdio_claim_host(card->func); |
| 825 | |
| 826 | if (!btmrvl_sdio_verify_fw_download(card, 1)) { |
| 827 | BT_DBG("Firmware already downloaded!"); |
| 828 | goto done; |
| 829 | } |
| 830 | |
| 831 | ret = btmrvl_sdio_download_helper(card); |
| 832 | if (ret) { |
| 833 | BT_ERR("Failed to download helper!"); |
| 834 | ret = -EIO; |
| 835 | goto done; |
| 836 | } |
| 837 | |
| 838 | if (btmrvl_sdio_download_fw_w_helper(card)) { |
| 839 | BT_ERR("Failed to download firmware!"); |
| 840 | ret = -EIO; |
| 841 | goto done; |
| 842 | } |
| 843 | |
| 844 | if (btmrvl_sdio_verify_fw_download(card, MAX_POLL_TRIES)) { |
| 845 | BT_ERR("FW failed to be active in time!"); |
| 846 | ret = -ETIMEDOUT; |
| 847 | goto done; |
| 848 | } |
| 849 | |
| 850 | done: |
| 851 | sdio_release_host(card->func); |
| 852 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv) |
| 857 | { |
| 858 | struct btmrvl_sdio_card *card = priv->btmrvl_dev.card; |
| 859 | int ret = 0; |
| 860 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 861 | if (!card || !card->func) { |
| 862 | BT_ERR("card or function is NULL!"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 863 | return -EINVAL; |
| 864 | } |
| 865 | |
| 866 | sdio_claim_host(card->func); |
| 867 | |
| 868 | sdio_writeb(card->func, HOST_POWER_UP, CONFIG_REG, &ret); |
| 869 | |
| 870 | sdio_release_host(card->func); |
| 871 | |
| 872 | BT_DBG("wake up firmware"); |
| 873 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | static int btmrvl_sdio_probe(struct sdio_func *func, |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 878 | const struct sdio_device_id *id) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 879 | { |
| 880 | int ret = 0; |
| 881 | struct btmrvl_private *priv = NULL; |
| 882 | struct btmrvl_sdio_card *card = NULL; |
| 883 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 884 | BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d", |
| 885 | id->vendor, id->device, id->class, func->num); |
| 886 | |
| 887 | card = kzalloc(sizeof(*card), GFP_KERNEL); |
| 888 | if (!card) { |
| 889 | ret = -ENOMEM; |
| 890 | goto done; |
| 891 | } |
| 892 | |
| 893 | card->func = func; |
| 894 | |
Marcel Holtmann | dcf47f3 | 2009-06-09 16:21:58 +0200 | [diff] [blame] | 895 | if (id->driver_data) { |
| 896 | struct btmrvl_sdio_device *data = (void *) id->driver_data; |
| 897 | card->helper = data->helper; |
| 898 | card->firmware = data->firmware; |
| 899 | } |
| 900 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 901 | if (btmrvl_sdio_register_dev(card) < 0) { |
| 902 | BT_ERR("Failed to register BT device!"); |
| 903 | ret = -ENODEV; |
| 904 | goto free_card; |
| 905 | } |
| 906 | |
| 907 | /* Disable the interrupts on the card */ |
| 908 | btmrvl_sdio_disable_host_int(card); |
| 909 | |
| 910 | if (btmrvl_sdio_download_fw(card)) { |
| 911 | BT_ERR("Downloading firmware failed!"); |
| 912 | ret = -ENODEV; |
| 913 | goto unreg_dev; |
| 914 | } |
| 915 | |
| 916 | msleep(100); |
| 917 | |
| 918 | btmrvl_sdio_enable_host_int(card); |
| 919 | |
| 920 | priv = btmrvl_add_card(card); |
| 921 | if (!priv) { |
| 922 | BT_ERR("Initializing card failed!"); |
| 923 | ret = -ENODEV; |
| 924 | goto disable_host_int; |
| 925 | } |
| 926 | |
| 927 | card->priv = priv; |
| 928 | |
| 929 | /* Initialize the interface specific function pointers */ |
| 930 | priv->hw_host_to_card = btmrvl_sdio_host_to_card; |
| 931 | priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw; |
| 932 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 933 | btmrvl_send_module_cfg_cmd(priv, MODULE_BRINGUP_REQ); |
Amitkumar Karwar | 32fd639 | 2009-09-30 11:18:30 -0700 | [diff] [blame] | 934 | priv->btmrvl_dev.psmode = 1; |
| 935 | btmrvl_enable_ps(priv); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 936 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 937 | return 0; |
| 938 | |
| 939 | disable_host_int: |
| 940 | btmrvl_sdio_disable_host_int(card); |
| 941 | unreg_dev: |
| 942 | btmrvl_sdio_unregister_dev(card); |
| 943 | free_card: |
| 944 | kfree(card); |
| 945 | done: |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 946 | return ret; |
| 947 | } |
| 948 | |
| 949 | static void btmrvl_sdio_remove(struct sdio_func *func) |
| 950 | { |
| 951 | struct btmrvl_sdio_card *card; |
| 952 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 953 | if (func) { |
| 954 | card = sdio_get_drvdata(func); |
| 955 | if (card) { |
| 956 | /* Send SHUTDOWN command & disable interrupt |
| 957 | * if user removes the module. |
| 958 | */ |
| 959 | if (user_rmmod) { |
| 960 | btmrvl_send_module_cfg_cmd(card->priv, |
| 961 | MODULE_SHUTDOWN_REQ); |
| 962 | btmrvl_sdio_disable_host_int(card); |
| 963 | } |
| 964 | BT_DBG("unregester dev"); |
| 965 | btmrvl_sdio_unregister_dev(card); |
| 966 | btmrvl_remove_card(card->priv); |
| 967 | kfree(card); |
| 968 | } |
| 969 | } |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static struct sdio_driver bt_mrvl_sdio = { |
| 973 | .name = "btmrvl_sdio", |
| 974 | .id_table = btmrvl_sdio_ids, |
| 975 | .probe = btmrvl_sdio_probe, |
| 976 | .remove = btmrvl_sdio_remove, |
| 977 | }; |
| 978 | |
Peter Huewe | 8e5b230 | 2009-12-22 09:34:20 +0100 | [diff] [blame] | 979 | static int __init btmrvl_sdio_init_module(void) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 980 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 981 | if (sdio_register_driver(&bt_mrvl_sdio) != 0) { |
| 982 | BT_ERR("SDIO Driver Registration Failed"); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 983 | return -ENODEV; |
| 984 | } |
| 985 | |
| 986 | /* Clear the flag in case user removes the card. */ |
| 987 | user_rmmod = 0; |
| 988 | |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 989 | return 0; |
| 990 | } |
| 991 | |
Peter Huewe | 8e5b230 | 2009-12-22 09:34:20 +0100 | [diff] [blame] | 992 | static void __exit btmrvl_sdio_exit_module(void) |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 993 | { |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 994 | /* Set the flag as user is removing this module. */ |
| 995 | user_rmmod = 1; |
| 996 | |
| 997 | sdio_unregister_driver(&bt_mrvl_sdio); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | module_init(btmrvl_sdio_init_module); |
| 1001 | module_exit(btmrvl_sdio_exit_module); |
| 1002 | |
| 1003 | MODULE_AUTHOR("Marvell International Ltd."); |
Marcel Holtmann | 9666fb3 | 2009-06-09 21:45:04 +0200 | [diff] [blame] | 1004 | MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION); |
Bing Zhao | 789221e | 2009-06-02 14:29:36 -0700 | [diff] [blame] | 1005 | MODULE_VERSION(VERSION); |
| 1006 | MODULE_LICENSE("GPL v2"); |
Ben Hutchings | 2861453 | 2009-11-07 21:41:18 +0000 | [diff] [blame] | 1007 | MODULE_FIRMWARE("sd8688_helper.bin"); |
| 1008 | MODULE_FIRMWARE("sd8688.bin"); |