Blame view

drivers/bluetooth/btmrvl_sdio.c 23.5 KB
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /**
   * Marvell BT-over-SDIO driver: SDIO interface related functions.
   *
   * Copyright (C) 2009, Marvell International Ltd.
   *
   * This software file (the "File") is distributed by Marvell International
   * Ltd. under the terms of the GNU General Public License Version 2, June 1991
   * (the "License").  You may use, redistribute and/or modify this File in
   * accordance with the terms and conditions of the License, a copy of which
   * is available by writing to the Free Software Foundation, Inc.,
   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
   * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
   *
   *
   * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
   * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
   * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
   * this warranty disclaimer.
   **/
  
  #include <linux/firmware.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
22
  #include <linux/slab.h>
789221ecc   Bing Zhao   Bluetooth: Add Ma...
23
24
25
  
  #include <linux/mmc/sdio_ids.h>
  #include <linux/mmc/sdio_func.h>
a59b968ee   Paul Gortmaker   bluetooth: add mo...
26
  #include <linux/module.h>
789221ecc   Bing Zhao   Bluetooth: Add Ma...
27
28
29
30
31
32
33
34
  
  #include <net/bluetooth/bluetooth.h>
  #include <net/bluetooth/hci_core.h>
  
  #include "btmrvl_drv.h"
  #include "btmrvl_sdio.h"
  
  #define VERSION "1.0"
789221ecc   Bing Zhao   Bluetooth: Add Ma...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  /* The btmrvl_sdio_remove() callback function is called
   * when user removes this module from kernel space or ejects
   * the card from the slot. The driver handles these 2 cases
   * differently.
   * If the user is removing the module, a MODULE_SHUTDOWN_REQ
   * command is sent to firmware and interrupt will be disabled.
   * If the card is removed, there is no need to send command
   * or disable interrupt.
   *
   * The variable 'user_rmmod' is used to distinguish these two
   * scenarios. This flag is initialized as FALSE in case the card
   * is removed, and will be set to TRUE for module removal when
   * module_exit function is called.
   */
  static u8 user_rmmod;
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
50
  static u8 sdio_ireg;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
51

9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  static const struct btmrvl_sdio_card_reg btmrvl_reg_8688 = {
  	.cfg = 0x03,
  	.host_int_mask = 0x04,
  	.host_intstatus = 0x05,
  	.card_status = 0x20,
  	.sq_read_base_addr_a0 = 0x10,
  	.sq_read_base_addr_a1 = 0x11,
  	.card_fw_status0 = 0x40,
  	.card_fw_status1 = 0x41,
  	.card_rx_len = 0x42,
  	.card_rx_unit = 0x43,
  	.io_port_0 = 0x00,
  	.io_port_1 = 0x01,
  	.io_port_2 = 0x02,
  };
2ac654f74   Bing Zhao   Bluetooth: btmrvl...
67
  static const struct btmrvl_sdio_card_reg btmrvl_reg_87xx = {
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  	.cfg = 0x00,
  	.host_int_mask = 0x02,
  	.host_intstatus = 0x03,
  	.card_status = 0x30,
  	.sq_read_base_addr_a0 = 0x40,
  	.sq_read_base_addr_a1 = 0x41,
  	.card_revision = 0x5c,
  	.card_fw_status0 = 0x60,
  	.card_fw_status1 = 0x61,
  	.card_rx_len = 0x62,
  	.card_rx_unit = 0x63,
  	.io_port_0 = 0x78,
  	.io_port_1 = 0x79,
  	.io_port_2 = 0x7a,
  };
c675786a1   Paul Bolle   btmrvl_sdio: fix ...
83
  static const struct btmrvl_sdio_device btmrvl_sdio_sd8688 = {
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
84
85
  	.helper		= "sd8688_helper.bin",
  	.firmware	= "sd8688.bin",
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
86
87
88
89
90
91
92
  	.reg		= &btmrvl_reg_8688,
  	.sd_blksz_fw_dl	= 64,
  };
  
  static const struct btmrvl_sdio_device btmrvl_sdio_sd8787 = {
  	.helper		= NULL,
  	.firmware	= "mrvl/sd8787_uapsta.bin",
2ac654f74   Bing Zhao   Bluetooth: btmrvl...
93
94
95
96
97
98
99
100
  	.reg		= &btmrvl_reg_87xx,
  	.sd_blksz_fw_dl	= 256,
  };
  
  static const struct btmrvl_sdio_device btmrvl_sdio_sd8797 = {
  	.helper		= NULL,
  	.firmware	= "mrvl/sd8797_uapsta.bin",
  	.reg		= &btmrvl_reg_87xx,
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
101
  	.sd_blksz_fw_dl	= 256,
789221ecc   Bing Zhao   Bluetooth: Add Ma...
102
  };
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
103
104
105
  static const struct sdio_device_id btmrvl_sdio_ids[] = {
  	/* Marvell SD8688 Bluetooth device */
  	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x9105),
c675786a1   Paul Bolle   btmrvl_sdio: fix ...
106
  			.driver_data = (unsigned long) &btmrvl_sdio_sd8688 },
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
107
108
109
  	/* Marvell SD8787 Bluetooth device */
  	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x911A),
  			.driver_data = (unsigned long) &btmrvl_sdio_sd8787 },
2ac654f74   Bing Zhao   Bluetooth: btmrvl...
110
111
112
  	/* Marvell SD8797 Bluetooth device */
  	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, 0x912A),
  			.driver_data = (unsigned long) &btmrvl_sdio_sd8797 },
789221ecc   Bing Zhao   Bluetooth: Add Ma...
113

dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
114
  	{ }	/* Terminating entry */
789221ecc   Bing Zhao   Bluetooth: Add Ma...
115
  };
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
116
  MODULE_DEVICE_TABLE(sdio, btmrvl_sdio_ids);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
117
118
119
120
  static int btmrvl_sdio_get_rx_unit(struct btmrvl_sdio_card *card)
  {
  	u8 reg;
  	int ret;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
121
  	reg = sdio_readb(card->func, card->reg->card_rx_unit, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
122
123
  	if (!ret)
  		card->rx_unit = reg;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
124
125
126
127
128
  	return ret;
  }
  
  static int btmrvl_sdio_read_fw_status(struct btmrvl_sdio_card *card, u16 *dat)
  {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
129
  	u8 fws0, fws1;
9374253ff   Marcel Holtmann   Bluetooth: Remove...
130
  	int ret;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
131
132
  
  	*dat = 0;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
133
  	fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
278be27fc   Kulikov Vasiliy   Bluetooth: Silenc...
134
135
  	if (ret)
  		return -EIO;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
136

9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
137
  	fws1 = sdio_readb(card->func, card->reg->card_fw_status1, &ret);
9374253ff   Marcel Holtmann   Bluetooth: Remove...
138
  	if (ret)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
139
  		return -EIO;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
140
141
  
  	*dat = (((u16) fws1) << 8) | fws0;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
142
143
144
145
146
  	return 0;
  }
  
  static int btmrvl_sdio_read_rx_len(struct btmrvl_sdio_card *card, u16 *dat)
  {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
147
  	u8 reg;
9374253ff   Marcel Holtmann   Bluetooth: Remove...
148
  	int ret;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
149

9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
150
  	reg = sdio_readb(card->func, card->reg->card_rx_len, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
151
152
  	if (!ret)
  		*dat = (u16) reg << card->rx_unit;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
153
154
155
156
  	return ret;
  }
  
  static int btmrvl_sdio_enable_host_int_mask(struct btmrvl_sdio_card *card,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
157
  								u8 mask)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
158
159
  {
  	int ret;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
160
  	sdio_writeb(card->func, mask, card->reg->host_int_mask, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
161
162
163
164
  	if (ret) {
  		BT_ERR("Unable to enable the host interrupt!");
  		ret = -EIO;
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
165
166
167
168
  	return ret;
  }
  
  static int btmrvl_sdio_disable_host_int_mask(struct btmrvl_sdio_card *card,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
169
  								u8 mask)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
170
  {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
171
  	u8 host_int_mask;
9374253ff   Marcel Holtmann   Bluetooth: Remove...
172
  	int ret;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
173

9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
174
  	host_int_mask = sdio_readb(card->func, card->reg->host_int_mask, &ret);
9374253ff   Marcel Holtmann   Bluetooth: Remove...
175
176
  	if (ret)
  		return -EIO;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
177
178
  
  	host_int_mask &= ~mask;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
179
  	sdio_writeb(card->func, host_int_mask, card->reg->host_int_mask, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
180
181
  	if (ret < 0) {
  		BT_ERR("Unable to disable the host interrupt!");
9374253ff   Marcel Holtmann   Bluetooth: Remove...
182
  		return -EIO;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
183
  	}
9374253ff   Marcel Holtmann   Bluetooth: Remove...
184
  	return 0;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
185
186
187
188
189
  }
  
  static int btmrvl_sdio_poll_card_status(struct btmrvl_sdio_card *card, u8 bits)
  {
  	unsigned int tries;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
190
  	u8 status;
9374253ff   Marcel Holtmann   Bluetooth: Remove...
191
  	int ret;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
192
193
  
  	for (tries = 0; tries < MAX_POLL_TRIES * 1000; tries++) {
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
194
  		status = sdio_readb(card->func, card->reg->card_status,	&ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
195
196
197
  		if (ret)
  			goto failed;
  		if ((status & bits) == bits)
9374253ff   Marcel Holtmann   Bluetooth: Remove...
198
  			return ret;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
199
200
201
202
203
204
205
206
  
  		udelay(1);
  	}
  
  	ret = -ETIMEDOUT;
  
  failed:
  	BT_ERR("FAILED! ret=%d", ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
207
208
209
210
  	return ret;
  }
  
  static int btmrvl_sdio_verify_fw_download(struct btmrvl_sdio_card *card,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
211
  								int pollnum)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
212
213
214
215
  {
  	int ret = -ETIMEDOUT;
  	u16 firmwarestat;
  	unsigned int tries;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
216
217
218
219
220
221
222
223
224
225
226
227
  	 /* Wait for firmware to become ready */
  	for (tries = 0; tries < pollnum; tries++) {
  		if (btmrvl_sdio_read_fw_status(card, &firmwarestat) < 0)
  			continue;
  
  		if (firmwarestat == FIRMWARE_READY) {
  			ret = 0;
  			break;
  		} else {
  			msleep(10);
  		}
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
228
229
230
231
232
233
234
235
236
237
238
239
  	return ret;
  }
  
  static int btmrvl_sdio_download_helper(struct btmrvl_sdio_card *card)
  {
  	const struct firmware *fw_helper = NULL;
  	const u8 *helper = NULL;
  	int ret;
  	void *tmphlprbuf = NULL;
  	int tmphlprbufsz, hlprblknow, helperlen;
  	u8 *helperbuf;
  	u32 tx_len;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
240
  	ret = request_firmware(&fw_helper, card->helper,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
241
  						&card->func->dev);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
242
243
  	if ((ret < 0) || !fw_helper) {
  		BT_ERR("request_firmware(helper) failed, error code = %d",
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
244
  									ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
245
246
247
248
249
250
251
252
  		ret = -ENOENT;
  		goto done;
  	}
  
  	helper = fw_helper->data;
  	helperlen = fw_helper->size;
  
  	BT_DBG("Downloading helper image (%d bytes), block size %d bytes",
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
253
  						helperlen, SDIO_BLOCK_SIZE);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
254
255
  
  	tmphlprbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
f8df39f18   Julia Lawall   Bluetooth: Use kz...
256
  	tmphlprbuf = kzalloc(tmphlprbufsz, GFP_KERNEL);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
257
258
259
260
261
262
  	if (!tmphlprbuf) {
  		BT_ERR("Unable to allocate buffer for helper."
  			" Terminating download");
  		ret = -ENOMEM;
  		goto done;
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  	helperbuf = (u8 *) ALIGN_ADDR(tmphlprbuf, BTSDIO_DMA_ALIGN);
  
  	/* Perform helper data transfer */
  	tx_len = (FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE)
  			- SDIO_HEADER_LEN;
  	hlprblknow = 0;
  
  	do {
  		ret = btmrvl_sdio_poll_card_status(card,
  					    CARD_IO_READY | DN_LD_CARD_RDY);
  		if (ret < 0) {
  			BT_ERR("Helper download poll status timeout @ %d",
  				hlprblknow);
  			goto done;
  		}
  
  		/* Check if there is more data? */
  		if (hlprblknow >= helperlen)
  			break;
  
  		if (helperlen - hlprblknow < tx_len)
  			tx_len = helperlen - hlprblknow;
  
  		/* Little-endian */
  		helperbuf[0] = ((tx_len & 0x000000ff) >> 0);
  		helperbuf[1] = ((tx_len & 0x0000ff00) >> 8);
  		helperbuf[2] = ((tx_len & 0x00ff0000) >> 16);
  		helperbuf[3] = ((tx_len & 0xff000000) >> 24);
  
  		memcpy(&helperbuf[SDIO_HEADER_LEN], &helper[hlprblknow],
  				tx_len);
  
  		/* Now send the data */
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
296
297
  		ret = sdio_writesb(card->func, card->ioport, helperbuf,
  				FIRMWARE_TRANSFER_NBLOCK * SDIO_BLOCK_SIZE);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  		if (ret < 0) {
  			BT_ERR("IO error during helper download @ %d",
  				hlprblknow);
  			goto done;
  		}
  
  		hlprblknow += tx_len;
  	} while (true);
  
  	BT_DBG("Transferring helper image EOF block");
  
  	memset(helperbuf, 0x0, SDIO_BLOCK_SIZE);
  
  	ret = sdio_writesb(card->func, card->ioport, helperbuf,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
312
  							SDIO_BLOCK_SIZE);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
313
314
315
316
317
318
319
320
321
322
323
  	if (ret < 0) {
  		BT_ERR("IO error in writing helper image EOF block");
  		goto done;
  	}
  
  	ret = 0;
  
  done:
  	kfree(tmphlprbuf);
  	if (fw_helper)
  		release_firmware(fw_helper);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
324
325
326
327
328
329
330
331
332
333
334
335
  	return ret;
  }
  
  static int btmrvl_sdio_download_fw_w_helper(struct btmrvl_sdio_card *card)
  {
  	const struct firmware *fw_firmware = NULL;
  	const u8 *firmware = NULL;
  	int firmwarelen, tmpfwbufsz, ret;
  	unsigned int tries, offset;
  	u8 base0, base1;
  	void *tmpfwbuf = NULL;
  	u8 *fwbuf;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
336
  	u16 len, blksz_dl = card->sd_blksz_fw_dl;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
337
  	int txlen = 0, tx_blocks = 0, count = 0;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
338
  	ret = request_firmware(&fw_firmware, card->firmware,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
339
  							&card->func->dev);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
340
341
  	if ((ret < 0) || !fw_firmware) {
  		BT_ERR("request_firmware(firmware) failed, error code = %d",
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
342
  									ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
343
344
345
346
347
348
349
350
351
352
  		ret = -ENOENT;
  		goto done;
  	}
  
  	firmware = fw_firmware->data;
  	firmwarelen = fw_firmware->size;
  
  	BT_DBG("Downloading FW image (%d bytes)", firmwarelen);
  
  	tmpfwbufsz = ALIGN_SZ(BTM_UPLD_SIZE, BTSDIO_DMA_ALIGN);
f8df39f18   Julia Lawall   Bluetooth: Use kz...
353
  	tmpfwbuf = kzalloc(tmpfwbufsz, GFP_KERNEL);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
354
355
356
357
358
359
  	if (!tmpfwbuf) {
  		BT_ERR("Unable to allocate buffer for firmware."
  		       " Terminating download");
  		ret = -ENOMEM;
  		goto done;
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
360
361
362
363
364
365
366
  	/* Ensure aligned firmware buffer */
  	fwbuf = (u8 *) ALIGN_ADDR(tmpfwbuf, BTSDIO_DMA_ALIGN);
  
  	/* Perform firmware data transfer */
  	offset = 0;
  	do {
  		ret = btmrvl_sdio_poll_card_status(card,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
367
  					CARD_IO_READY | DN_LD_CARD_RDY);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
368
369
  		if (ret < 0) {
  			BT_ERR("FW download with helper poll status"
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
370
  						" timeout @ %d", offset);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
371
372
373
374
375
376
377
378
379
  			goto done;
  		}
  
  		/* Check if there is more data ? */
  		if (offset >= firmwarelen)
  			break;
  
  		for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  			base0 = sdio_readb(card->func,
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
380
  					card->reg->sq_read_base_addr_a0, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
381
382
383
384
385
386
387
388
389
  			if (ret) {
  				BT_ERR("BASE0 register read failed:"
  					" base0 = 0x%04X(%d)."
  					" Terminating download",
  					base0, base0);
  				ret = -EIO;
  				goto done;
  			}
  			base1 = sdio_readb(card->func,
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
390
  					card->reg->sq_read_base_addr_a1, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
  			if (ret) {
  				BT_ERR("BASE1 register read failed:"
  					" base1 = 0x%04X(%d)."
  					" Terminating download",
  					base1, base1);
  				ret = -EIO;
  				goto done;
  			}
  
  			len = (((u16) base1) << 8) | base0;
  			if (len)
  				break;
  
  			udelay(10);
  		}
  
  		if (!len)
  			break;
  		else if (len > BTM_UPLD_SIZE) {
  			BT_ERR("FW download failure @%d, invalid length %d",
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
411
  								offset, len);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
  			ret = -EINVAL;
  			goto done;
  		}
  
  		txlen = len;
  
  		if (len & BIT(0)) {
  			count++;
  			if (count > MAX_WRITE_IOMEM_RETRY) {
  				BT_ERR("FW download failure @%d, "
  					"over max retry count", offset);
  				ret = -EIO;
  				goto done;
  			}
  			BT_ERR("FW CRC error indicated by the helper: "
  				"len = 0x%04X, txlen = %d", len, txlen);
  			len &= ~BIT(0);
  			/* Set txlen to 0 so as to resend from same offset */
  			txlen = 0;
  		} else {
  			count = 0;
  
  			/* Last block ? */
  			if (firmwarelen - offset < txlen)
  				txlen = firmwarelen - offset;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
437
  			tx_blocks = (txlen + blksz_dl - 1) / blksz_dl;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
438
439
440
441
442
  
  			memcpy(fwbuf, &firmware[offset], txlen);
  		}
  
  		ret = sdio_writesb(card->func, card->ioport, fwbuf,
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
443
  						tx_blocks * blksz_dl);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
444
445
446
  
  		if (ret < 0) {
  			BT_ERR("FW download, writesb(%d) failed @%d",
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
447
  							count, offset);
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
448
449
  			sdio_writeb(card->func, HOST_CMD53_FIN,
  						card->reg->cfg, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
  			if (ret)
  				BT_ERR("writeb failed (CFG)");
  		}
  
  		offset += txlen;
  	} while (true);
  
  	BT_DBG("FW download over, size %d bytes", offset);
  
  	ret = 0;
  
  done:
  	kfree(tmpfwbuf);
  
  	if (fw_firmware)
  		release_firmware(fw_firmware);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
466
467
468
469
470
471
472
473
474
475
476
477
  	return ret;
  }
  
  static int btmrvl_sdio_card_to_host(struct btmrvl_private *priv)
  {
  	u16 buf_len = 0;
  	int ret, buf_block_len, blksz;
  	struct sk_buff *skb = NULL;
  	u32 type;
  	u8 *payload = NULL;
  	struct hci_dev *hdev = priv->btmrvl_dev.hcidev;
  	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  	if (!card || !card->func) {
  		BT_ERR("card or function is NULL!");
  		ret = -EINVAL;
  		goto exit;
  	}
  
  	/* Read the length of data to be transferred */
  	ret = btmrvl_sdio_read_rx_len(card, &buf_len);
  	if (ret < 0) {
  		BT_ERR("read rx_len failed");
  		ret = -EIO;
  		goto exit;
  	}
  
  	blksz = SDIO_BLOCK_SIZE;
  	buf_block_len = (buf_len + blksz - 1) / blksz;
  
  	if (buf_len <= SDIO_HEADER_LEN
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
496
  			|| (buf_block_len * blksz) > ALLOC_BUF_SIZE) {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
497
498
499
500
501
502
503
  		BT_ERR("invalid packet length: %d", buf_len);
  		ret = -EINVAL;
  		goto exit;
  	}
  
  	/* Allocate buffer */
  	skb = bt_skb_alloc(buf_block_len * blksz + BTSDIO_DMA_ALIGN,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
504
  								GFP_ATOMIC);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
505
506
507
508
  	if (skb == NULL) {
  		BT_ERR("No free skb");
  		goto exit;
  	}
3318b2362   Bing Zhao   Bluetooth: Fix in...
509
510
511
512
513
  	if ((unsigned long) skb->data & (BTSDIO_DMA_ALIGN - 1)) {
  		skb_put(skb, (unsigned long) skb->data &
  					(BTSDIO_DMA_ALIGN - 1));
  		skb_pull(skb, (unsigned long) skb->data &
  					(BTSDIO_DMA_ALIGN - 1));
789221ecc   Bing Zhao   Bluetooth: Add Ma...
514
  	}
3318b2362   Bing Zhao   Bluetooth: Fix in...
515
  	payload = skb->data;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
  
  	ret = sdio_readsb(card->func, payload, card->ioport,
  			  buf_block_len * blksz);
  	if (ret < 0) {
  		BT_ERR("readsb failed: %d", ret);
  		ret = -EIO;
  		goto exit;
  	}
  
  	/* This is SDIO specific header length: byte[2][1][0], type: byte[3]
  	 * (HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor)
  	 */
  
  	buf_len = payload[0];
  	buf_len |= (u16) payload[1] << 8;
  	type = payload[3];
  
  	switch (type) {
  	case HCI_ACLDATA_PKT:
  	case HCI_SCODATA_PKT:
  	case HCI_EVENT_PKT:
  		bt_cb(skb)->pkt_type = type;
  		skb->dev = (void *)hdev;
  		skb_put(skb, buf_len);
  		skb_pull(skb, SDIO_HEADER_LEN);
  
  		if (type == HCI_EVENT_PKT)
  			btmrvl_check_evtpkt(priv, skb);
  
  		hci_recv_frame(skb);
  		hdev->stat.byte_rx += buf_len;
  		break;
  
  	case MRVL_VENDOR_PKT:
  		bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
  		skb->dev = (void *)hdev;
  		skb_put(skb, buf_len);
  		skb_pull(skb, SDIO_HEADER_LEN);
  
  		if (btmrvl_process_event(priv, skb))
  			hci_recv_frame(skb);
  
  		hdev->stat.byte_rx += buf_len;
  		break;
  
  	default:
af901ca18   André Goddard Rosa   tree-wide: fix as...
562
  		BT_ERR("Unknown packet type:%d", type);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
563
  		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, payload,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
564
  						blksz * buf_block_len);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
565
566
567
568
569
570
571
572
573
574
575
576
  
  		kfree_skb(skb);
  		skb = NULL;
  		break;
  	}
  
  exit:
  	if (ret) {
  		hdev->stat.err_rx++;
  		if (skb)
  			kfree_skb(skb);
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
577
578
  	return ret;
  }
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
579
  static int btmrvl_sdio_process_int_status(struct btmrvl_private *priv)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
580
  {
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
581
582
  	ulong flags;
  	u8 ireg;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
583
  	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
584
585
586
587
  	spin_lock_irqsave(&priv->driver_lock, flags);
  	ireg = sdio_ireg;
  	sdio_ireg = 0;
  	spin_unlock_irqrestore(&priv->driver_lock, flags);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
588

d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
589
590
  	sdio_claim_host(card->func);
  	if (ireg & DN_LD_HOST_INT_STATUS) {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
591
592
  		if (priv->btmrvl_dev.tx_dnld_rdy)
  			BT_DBG("tx_done already received: "
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
593
  				" int_status=0x%x", ireg);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
594
595
596
  		else
  			priv->btmrvl_dev.tx_dnld_rdy = true;
  	}
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
597
  	if (ireg & UP_LD_HOST_INT_STATUS)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
598
  		btmrvl_sdio_card_to_host(priv);
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
599
  	sdio_release_host(card->func);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
600

d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
601
  	return 0;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
602
603
604
605
606
  }
  
  static void btmrvl_sdio_interrupt(struct sdio_func *func)
  {
  	struct btmrvl_private *priv;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
607
  	struct btmrvl_sdio_card *card;
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
608
  	ulong flags;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
609
  	u8 ireg = 0;
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
610
  	int ret;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
611

789221ecc   Bing Zhao   Bluetooth: Add Ma...
612
  	card = sdio_get_drvdata(func);
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
613
614
615
616
617
618
  	if (!card || !card->priv) {
  		BT_ERR("sbi_interrupt(%p) card or priv is "
  				"NULL, card=%p
  ", func, card);
  		return;
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
619

d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
620
  	priv = card->priv;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
621

9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
622
  	ireg = sdio_readb(card->func, card->reg->host_intstatus, &ret);
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
623
624
625
  	if (ret) {
  		BT_ERR("sdio_readb: read int status register failed");
  		return;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
626
  	}
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
627
628
629
630
631
632
633
634
635
636
637
  
  	if (ireg != 0) {
  		/*
  		 * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
  		 * Clear the interrupt status register and re-enable the
  		 * interrupt.
  		 */
  		BT_DBG("ireg = 0x%x", ireg);
  
  		sdio_writeb(card->func, ~(ireg) & (DN_LD_HOST_INT_STATUS |
  					UP_LD_HOST_INT_STATUS),
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
638
  				card->reg->host_intstatus, &ret);
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
639
640
641
642
643
644
645
646
647
648
649
  		if (ret) {
  			BT_ERR("sdio_writeb: clear int status register failed");
  			return;
  		}
  	}
  
  	spin_lock_irqsave(&priv->driver_lock, flags);
  	sdio_ireg |= ireg;
  	spin_unlock_irqrestore(&priv->driver_lock, flags);
  
  	btmrvl_interrupt(priv);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
650
651
652
653
  }
  
  static int btmrvl_sdio_register_dev(struct btmrvl_sdio_card *card)
  {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
654
  	struct sdio_func *func;
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
655
656
  	u8 reg;
  	int ret = 0;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
657

789221ecc   Bing Zhao   Bluetooth: Add Ma...
658
659
660
661
662
663
664
  	if (!card || !card->func) {
  		BT_ERR("Error: card or function is NULL!");
  		ret = -EINVAL;
  		goto failed;
  	}
  
  	func = card->func;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
  	sdio_claim_host(func);
  
  	ret = sdio_enable_func(func);
  	if (ret) {
  		BT_ERR("sdio_enable_func() failed: ret=%d", ret);
  		ret = -EIO;
  		goto release_host;
  	}
  
  	ret = sdio_claim_irq(func, btmrvl_sdio_interrupt);
  	if (ret) {
  		BT_ERR("sdio_claim_irq failed: ret=%d", ret);
  		ret = -EIO;
  		goto disable_func;
  	}
  
  	ret = sdio_set_block_size(card->func, SDIO_BLOCK_SIZE);
  	if (ret) {
  		BT_ERR("cannot set SDIO block size");
  		ret = -EIO;
  		goto release_irq;
  	}
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
687
  	reg = sdio_readb(func, card->reg->io_port_0, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
688
689
690
691
692
693
  	if (ret < 0) {
  		ret = -EIO;
  		goto release_irq;
  	}
  
  	card->ioport = reg;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
694
  	reg = sdio_readb(func, card->reg->io_port_1, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
695
696
697
698
699
700
  	if (ret < 0) {
  		ret = -EIO;
  		goto release_irq;
  	}
  
  	card->ioport |= (reg << 8);
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
701
  	reg = sdio_readb(func, card->reg->io_port_2, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
702
703
704
705
706
707
708
709
710
711
712
713
  	if (ret < 0) {
  		ret = -EIO;
  		goto release_irq;
  	}
  
  	card->ioport |= (reg << 16);
  
  	BT_DBG("SDIO FUNC%d IO port: 0x%x", func->num, card->ioport);
  
  	sdio_set_drvdata(func, card);
  
  	sdio_release_host(func);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
714
715
716
717
718
719
720
721
722
723
724
725
  	return 0;
  
  release_irq:
  	sdio_release_irq(func);
  
  disable_func:
  	sdio_disable_func(func);
  
  release_host:
  	sdio_release_host(func);
  
  failed:
789221ecc   Bing Zhao   Bluetooth: Add Ma...
726
727
728
729
730
  	return ret;
  }
  
  static int btmrvl_sdio_unregister_dev(struct btmrvl_sdio_card *card)
  {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
731
732
733
734
735
736
737
  	if (card && card->func) {
  		sdio_claim_host(card->func);
  		sdio_release_irq(card->func);
  		sdio_disable_func(card->func);
  		sdio_release_host(card->func);
  		sdio_set_drvdata(card->func, NULL);
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
738
739
740
741
742
743
  	return 0;
  }
  
  static int btmrvl_sdio_enable_host_int(struct btmrvl_sdio_card *card)
  {
  	int ret;
9374253ff   Marcel Holtmann   Bluetooth: Remove...
744
  	if (!card || !card->func)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
745
  		return -EINVAL;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
746
747
748
749
750
751
752
753
  
  	sdio_claim_host(card->func);
  
  	ret = btmrvl_sdio_enable_host_int_mask(card, HIM_ENABLE);
  
  	btmrvl_sdio_get_rx_unit(card);
  
  	sdio_release_host(card->func);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
754
755
756
757
758
759
  	return ret;
  }
  
  static int btmrvl_sdio_disable_host_int(struct btmrvl_sdio_card *card)
  {
  	int ret;
9374253ff   Marcel Holtmann   Bluetooth: Remove...
760
  	if (!card || !card->func)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
761
  		return -EINVAL;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
762
763
764
765
766
767
  
  	sdio_claim_host(card->func);
  
  	ret = btmrvl_sdio_disable_host_int_mask(card, HIM_DISABLE);
  
  	sdio_release_host(card->func);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
768
769
770
771
772
773
774
775
776
777
778
779
780
781
  	return ret;
  }
  
  static int btmrvl_sdio_host_to_card(struct btmrvl_private *priv,
  				u8 *payload, u16 nb)
  {
  	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  	int ret = 0;
  	int buf_block_len;
  	int blksz;
  	int i = 0;
  	u8 *buf = NULL;
  	void *tmpbuf = NULL;
  	int tmpbufsz;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
782
783
  	if (!card || !card->func) {
  		BT_ERR("card or function is NULL!");
789221ecc   Bing Zhao   Bluetooth: Add Ma...
784
785
786
787
  		return -EINVAL;
  	}
  
  	buf = payload;
3318b2362   Bing Zhao   Bluetooth: Fix in...
788
  	if ((unsigned long) payload & (BTSDIO_DMA_ALIGN - 1)) {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
789
  		tmpbufsz = ALIGN_SZ(nb, BTSDIO_DMA_ALIGN);
5959809de   Julia Lawall   Bluetooth: Add mi...
790
791
792
  		tmpbuf = kzalloc(tmpbufsz, GFP_KERNEL);
  		if (!tmpbuf)
  			return -ENOMEM;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
  		buf = (u8 *) ALIGN_ADDR(tmpbuf, BTSDIO_DMA_ALIGN);
  		memcpy(buf, payload, nb);
  	}
  
  	blksz = SDIO_BLOCK_SIZE;
  	buf_block_len = (nb + blksz - 1) / blksz;
  
  	sdio_claim_host(card->func);
  
  	do {
  		/* Transfer data to card */
  		ret = sdio_writesb(card->func, card->ioport, buf,
  				   buf_block_len * blksz);
  		if (ret < 0) {
  			i++;
  			BT_ERR("i=%d writesb failed: %d", i, ret);
  			print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
  						payload, nb);
  			ret = -EIO;
  			if (i > MAX_WRITE_IOMEM_RETRY)
  				goto exit;
  		}
  	} while (ret);
  
  	priv->btmrvl_dev.tx_dnld_rdy = false;
  
  exit:
  	sdio_release_host(card->func);
88d1a0cf6   Yoichi Yuasa   Bluetooth: Fix me...
821
  	kfree(tmpbuf);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
822

789221ecc   Bing Zhao   Bluetooth: Add Ma...
823
824
825
826
827
828
  	return ret;
  }
  
  static int btmrvl_sdio_download_fw(struct btmrvl_sdio_card *card)
  {
  	int ret = 0;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
829
830
  	u8 fws0;
  	int pollnum = MAX_POLL_TRIES;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
831

789221ecc   Bing Zhao   Bluetooth: Add Ma...
832
833
  	if (!card || !card->func) {
  		BT_ERR("card or function is NULL!");
789221ecc   Bing Zhao   Bluetooth: Add Ma...
834
835
836
837
838
839
840
841
  		return -EINVAL;
  	}
  	sdio_claim_host(card->func);
  
  	if (!btmrvl_sdio_verify_fw_download(card, 1)) {
  		BT_DBG("Firmware already downloaded!");
  		goto done;
  	}
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
842
843
  	/* Check if other function driver is downloading the firmware */
  	fws0 = sdio_readb(card->func, card->reg->card_fw_status0, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
844
  	if (ret) {
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
845
  		BT_ERR("Failed to read FW downloading status!");
789221ecc   Bing Zhao   Bluetooth: Add Ma...
846
847
848
  		ret = -EIO;
  		goto done;
  	}
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
849
850
851
852
853
854
855
856
857
858
859
860
861
862
  	if (fws0) {
  		BT_DBG("BT not the winner (%#x). Skip FW downloading", fws0);
  
  		/* Give other function more time to download the firmware */
  		pollnum *= 10;
  	} else {
  		if (card->helper) {
  			ret = btmrvl_sdio_download_helper(card);
  			if (ret) {
  				BT_ERR("Failed to download helper!");
  				ret = -EIO;
  				goto done;
  			}
  		}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
863

9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
864
865
866
867
868
  		if (btmrvl_sdio_download_fw_w_helper(card)) {
  			BT_ERR("Failed to download firmware!");
  			ret = -EIO;
  			goto done;
  		}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
869
  	}
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
870
  	if (btmrvl_sdio_verify_fw_download(card, pollnum)) {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
871
872
873
874
875
876
877
  		BT_ERR("FW failed to be active in time!");
  		ret = -ETIMEDOUT;
  		goto done;
  	}
  
  done:
  	sdio_release_host(card->func);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
878
879
880
881
882
883
884
  	return ret;
  }
  
  static int btmrvl_sdio_wakeup_fw(struct btmrvl_private *priv)
  {
  	struct btmrvl_sdio_card *card = priv->btmrvl_dev.card;
  	int ret = 0;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
885
886
  	if (!card || !card->func) {
  		BT_ERR("card or function is NULL!");
789221ecc   Bing Zhao   Bluetooth: Add Ma...
887
888
889
890
  		return -EINVAL;
  	}
  
  	sdio_claim_host(card->func);
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
891
  	sdio_writeb(card->func, HOST_POWER_UP, card->reg->cfg, &ret);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
892
893
894
895
  
  	sdio_release_host(card->func);
  
  	BT_DBG("wake up firmware");
789221ecc   Bing Zhao   Bluetooth: Add Ma...
896
897
898
899
  	return ret;
  }
  
  static int btmrvl_sdio_probe(struct sdio_func *func,
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
900
  					const struct sdio_device_id *id)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
901
902
903
904
  {
  	int ret = 0;
  	struct btmrvl_private *priv = NULL;
  	struct btmrvl_sdio_card *card = NULL;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
905
906
907
908
909
910
911
912
913
914
  	BT_INFO("vendor=0x%x, device=0x%x, class=%d, fn=%d",
  			id->vendor, id->device, id->class, func->num);
  
  	card = kzalloc(sizeof(*card), GFP_KERNEL);
  	if (!card) {
  		ret = -ENOMEM;
  		goto done;
  	}
  
  	card->func = func;
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
915
916
  	if (id->driver_data) {
  		struct btmrvl_sdio_device *data = (void *) id->driver_data;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
917
  		card->helper = data->helper;
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
918
  		card->firmware = data->firmware;
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
919
920
  		card->reg = data->reg;
  		card->sd_blksz_fw_dl = data->sd_blksz_fw_dl;
dcf47f3bc   Marcel Holtmann   Bluetooth: Fix co...
921
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
  	if (btmrvl_sdio_register_dev(card) < 0) {
  		BT_ERR("Failed to register BT device!");
  		ret = -ENODEV;
  		goto free_card;
  	}
  
  	/* Disable the interrupts on the card */
  	btmrvl_sdio_disable_host_int(card);
  
  	if (btmrvl_sdio_download_fw(card)) {
  		BT_ERR("Downloading firmware failed!");
  		ret = -ENODEV;
  		goto unreg_dev;
  	}
  
  	msleep(100);
  
  	btmrvl_sdio_enable_host_int(card);
  
  	priv = btmrvl_add_card(card);
  	if (!priv) {
  		BT_ERR("Initializing card failed!");
  		ret = -ENODEV;
  		goto disable_host_int;
  	}
  
  	card->priv = priv;
  
  	/* Initialize the interface specific function pointers */
  	priv->hw_host_to_card = btmrvl_sdio_host_to_card;
  	priv->hw_wakeup_firmware = btmrvl_sdio_wakeup_fw;
d1d10d783   Amitkumar Karwar   Bluetooth: Proces...
953
  	priv->hw_process_int_status = btmrvl_sdio_process_int_status;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
954

64061607e   Bing Zhao   Bluetooth: Separa...
955
956
957
958
959
  	if (btmrvl_register_hdev(priv)) {
  		BT_ERR("Register hdev failed!");
  		ret = -ENODEV;
  		goto disable_host_int;
  	}
32fd63981   Amitkumar Karwar   Bluetooth: Enable...
960
961
  	priv->btmrvl_dev.psmode = 1;
  	btmrvl_enable_ps(priv);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
962

789221ecc   Bing Zhao   Bluetooth: Add Ma...
963
964
965
966
967
968
969
970
971
  	return 0;
  
  disable_host_int:
  	btmrvl_sdio_disable_host_int(card);
  unreg_dev:
  	btmrvl_sdio_unregister_dev(card);
  free_card:
  	kfree(card);
  done:
789221ecc   Bing Zhao   Bluetooth: Add Ma...
972
973
974
975
976
977
  	return ret;
  }
  
  static void btmrvl_sdio_remove(struct sdio_func *func)
  {
  	struct btmrvl_sdio_card *card;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
  	if (func) {
  		card = sdio_get_drvdata(func);
  		if (card) {
  			/* Send SHUTDOWN command & disable interrupt
  			 * if user removes the module.
  			 */
  			if (user_rmmod) {
  				btmrvl_send_module_cfg_cmd(card->priv,
  							MODULE_SHUTDOWN_REQ);
  				btmrvl_sdio_disable_host_int(card);
  			}
  			BT_DBG("unregester dev");
  			btmrvl_sdio_unregister_dev(card);
  			btmrvl_remove_card(card->priv);
  			kfree(card);
  		}
  	}
789221ecc   Bing Zhao   Bluetooth: Add Ma...
995
996
997
998
999
1000
1001
1002
  }
  
  static struct sdio_driver bt_mrvl_sdio = {
  	.name		= "btmrvl_sdio",
  	.id_table	= btmrvl_sdio_ids,
  	.probe		= btmrvl_sdio_probe,
  	.remove		= btmrvl_sdio_remove,
  };
8e5b23084   Peter Huewe   Bluetooth: Add __...
1003
  static int __init btmrvl_sdio_init_module(void)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1004
  {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1005
1006
  	if (sdio_register_driver(&bt_mrvl_sdio) != 0) {
  		BT_ERR("SDIO Driver Registration Failed");
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1007
1008
1009
1010
1011
  		return -ENODEV;
  	}
  
  	/* Clear the flag in case user removes the card. */
  	user_rmmod = 0;
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1012
1013
  	return 0;
  }
8e5b23084   Peter Huewe   Bluetooth: Add __...
1014
  static void __exit btmrvl_sdio_exit_module(void)
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1015
  {
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1016
1017
1018
1019
  	/* Set the flag as user is removing this module. */
  	user_rmmod = 1;
  
  	sdio_unregister_driver(&bt_mrvl_sdio);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1020
1021
1022
1023
1024
1025
  }
  
  module_init(btmrvl_sdio_init_module);
  module_exit(btmrvl_sdio_exit_module);
  
  MODULE_AUTHOR("Marvell International Ltd.");
9666fb356   Marcel Holtmann   Bluetooth: Fix mo...
1026
  MODULE_DESCRIPTION("Marvell BT-over-SDIO driver ver " VERSION);
789221ecc   Bing Zhao   Bluetooth: Add Ma...
1027
1028
  MODULE_VERSION(VERSION);
  MODULE_LICENSE("GPL v2");
2861453b1   Ben Hutchings   Bluetooth: Declar...
1029
1030
  MODULE_FIRMWARE("sd8688_helper.bin");
  MODULE_FIRMWARE("sd8688.bin");
9f72c1d97   Kevin Gan   Bluetooth: btmrvl...
1031
  MODULE_FIRMWARE("mrvl/sd8787_uapsta.bin");
2ac654f74   Bing Zhao   Bluetooth: btmrvl...
1032
  MODULE_FIRMWARE("mrvl/sd8797_uapsta.bin");