if_sdio.c 32.2 KB
Newer Older
P
Pierre Ossman 已提交
1 2 3
/*
 *  linux/drivers/net/wireless/libertas/if_sdio.c
 *
4
 *  Copyright 2007-2008 Pierre Ossman
P
Pierre Ossman 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This hardware has more or less no CMD53 support, so all registers
 * must be accessed using sdio_readb()/sdio_writeb().
 *
 * Transfers must be in one transaction or the firmware goes bonkers.
 * This means that the transfer must either be small enough to do a
 * byte based transfer or it must be padded to a multiple of the
 * current block size.
 *
 * As SDIO is still new to the kernel, it is unfortunately common with
22
 * bugs in the host controllers related to that. One such bug is that
P
Pierre Ossman 已提交
23 24 25 26 27 28
 * controllers cannot do transfers that aren't a multiple of 4 bytes.
 * If you don't have time to fix the host controller driver, you can
 * work around the problem by modifying if_sdio_host_to_card() and
 * if_sdio_card_to_host() to pad the data.
 */

29 30
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

31
#include <linux/kernel.h>
32
#include <linux/module.h>
33
#include <linux/slab.h>
P
Pierre Ossman 已提交
34 35 36 37 38 39
#include <linux/firmware.h>
#include <linux/netdevice.h>
#include <linux/delay.h>
#include <linux/mmc/card.h>
#include <linux/mmc/sdio_func.h>
#include <linux/mmc/sdio_ids.h>
40 41
#include <linux/mmc/sdio.h>
#include <linux/mmc/host.h>
42
#include <linux/pm_runtime.h>
P
Pierre Ossman 已提交
43 44 45 46 47

#include "host.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
48
#include "cmd.h"
P
Pierre Ossman 已提交
49 50
#include "if_sdio.h"

51 52
static void if_sdio_interrupt(struct sdio_func *func);

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/* The if_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 for SD8688 combo chip.
 * If the user is removing the module, the FUNC_SHUTDOWN
 * command for SD8688 is sent to the firmware.
 * If the card is removed, there is no need to send this command.
 *
 * 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;

P
Pierre Ossman 已提交
68
static const struct sdio_device_id if_sdio_ids[] = {
69 70 71 72 73
	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
			SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
			SDIO_DEVICE_ID_MARVELL_8688WLAN) },
	{ /* end: all zeroes */				},
P
Pierre Ossman 已提交
74 75 76 77
};

MODULE_DEVICE_TABLE(sdio, if_sdio_ids);

78 79 80 81 82 83 84 85 86 87 88 89 90
#define MODEL_8385	0x04
#define MODEL_8686	0x0b
#define MODEL_8688	0x10

static const struct lbs_fw_table fw_table[] = {
	{ MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
	{ MODEL_8385, "sd8385_helper.bin", "sd8385.bin" },
	{ MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
	{ MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
	{ MODEL_8686, "sd8686_helper.bin", "sd8686.bin" },
	{ MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
	{ MODEL_8688, "sd8688_helper.bin", "sd8688.bin" },
	{ 0, NULL, NULL }
P
Pierre Ossman 已提交
91
};
92 93
MODULE_FIRMWARE("libertas/sd8385_helper.bin");
MODULE_FIRMWARE("libertas/sd8385.bin");
B
Ben Hutchings 已提交
94 95
MODULE_FIRMWARE("sd8385_helper.bin");
MODULE_FIRMWARE("sd8385.bin");
96 97 98 99
MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
MODULE_FIRMWARE("libertas/sd8686_v9.bin");
MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
MODULE_FIRMWARE("libertas/sd8686_v8.bin");
B
Ben Hutchings 已提交
100 101
MODULE_FIRMWARE("sd8686_helper.bin");
MODULE_FIRMWARE("sd8686.bin");
102 103
MODULE_FIRMWARE("libertas/sd8688_helper.bin");
MODULE_FIRMWARE("libertas/sd8688.bin");
B
Ben Hutchings 已提交
104 105
MODULE_FIRMWARE("sd8688_helper.bin");
MODULE_FIRMWARE("sd8688.bin");
P
Pierre Ossman 已提交
106 107 108 109 110 111 112 113 114

struct if_sdio_packet {
	struct if_sdio_packet	*next;
	u16			nb;
	u8			buffer[0] __attribute__((aligned(4)));
};

struct if_sdio_card {
	struct sdio_func	*func;
115
	struct lbs_private	*priv;
P
Pierre Ossman 已提交
116 117 118

	int			model;
	unsigned long		ioport;
119
	unsigned int		scratch_reg;
120 121
	bool			started;
	wait_queue_head_t	pwron_waitq;
P
Pierre Ossman 已提交
122

123
	u8			buffer[65536] __attribute__((aligned(4)));
P
Pierre Ossman 已提交
124 125 126

	spinlock_t		lock;
	struct if_sdio_packet	*packets;
127 128

	struct workqueue_struct	*workqueue;
P
Pierre Ossman 已提交
129
	struct work_struct	packet_worker;
130 131

	u8			rx_unit;
P
Pierre Ossman 已提交
132 133
};

134 135 136
static void if_sdio_finish_power_on(struct if_sdio_card *card);
static int if_sdio_power_off(struct if_sdio_card *card);

P
Pierre Ossman 已提交
137 138 139 140
/********************************************************************/
/* I/O                                                              */
/********************************************************************/

141 142 143 144 145 146
/*
 *  For SD8385/SD8686, this function reads firmware status after
 *  the image is downloaded, or reads RX packet length when
 *  interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
 *  For SD8688, this function reads firmware status only.
 */
P
Pierre Ossman 已提交
147 148
static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
{
149
	int ret;
P
Pierre Ossman 已提交
150 151
	u16 scratch;

152
	scratch = sdio_readb(card->func, card->scratch_reg, &ret);
P
Pierre Ossman 已提交
153
	if (!ret)
154 155
		scratch |= sdio_readb(card->func, card->scratch_reg + 1,
					&ret) << 8;
P
Pierre Ossman 已提交
156 157 158 159 160 161 162 163 164 165

	if (err)
		*err = ret;

	if (ret)
		return 0xffff;

	return scratch;
}

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
{
	int ret;
	u8 rx_unit;

	rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);

	if (ret)
		rx_unit = 0;

	return rx_unit;
}

static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
{
	int ret;
	u16 rx_len;

	switch (card->model) {
185 186
	case MODEL_8385:
	case MODEL_8686:
187 188
		rx_len = if_sdio_read_scratch(card, &ret);
		break;
189
	case MODEL_8688:
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	default: /* for newer chipsets */
		rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
		if (!ret)
			rx_len <<= card->rx_unit;
		else
			rx_len = 0xffff;	/* invalid length */

		break;
	}

	if (err)
		*err = ret;

	return rx_len;
}

P
Pierre Ossman 已提交
206 207 208
static int if_sdio_handle_cmd(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
209
	struct lbs_private *priv = card->priv;
P
Pierre Ossman 已提交
210 211
	int ret;
	unsigned long flags;
212
	u8 i;
P
Pierre Ossman 已提交
213 214 215

	lbs_deb_enter(LBS_DEB_SDIO);

216
	if (size > LBS_CMD_BUFFER_SIZE) {
P
Pierre Ossman 已提交
217 218 219 220 221 222
		lbs_deb_sdio("response packet too large (%d bytes)\n",
			(int)size);
		ret = -E2BIG;
		goto out;
	}

223
	spin_lock_irqsave(&priv->driver_lock, flags);
P
Pierre Ossman 已提交
224

225 226 227 228 229
	i = (priv->resp_idx == 0) ? 1 : 0;
	BUG_ON(priv->resp_len[i]);
	priv->resp_len[i] = size;
	memcpy(priv->resp_buf[i], buffer, size);
	lbs_notify_command_response(priv, i);
P
Pierre Ossman 已提交
230

231
	spin_unlock_irqrestore(&card->priv->driver_lock, flags);
P
Pierre Ossman 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

	ret = 0;

out:
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
	return ret;
}

static int if_sdio_handle_data(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
	int ret;
	struct sk_buff *skb;
	char *data;

	lbs_deb_enter(LBS_DEB_SDIO);

	if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
		lbs_deb_sdio("response packet too large (%d bytes)\n",
			(int)size);
		ret = -E2BIG;
		goto out;
	}

256
	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
P
Pierre Ossman 已提交
257 258 259 260 261
	if (!skb) {
		ret = -ENOMEM;
		goto out;
	}

262 263
	skb_reserve(skb, NET_IP_ALIGN);

P
Pierre Ossman 已提交
264 265 266 267
	data = skb_put(skb, size);

	memcpy(data, buffer, size);

268
	lbs_process_rxed_packet(card->priv, skb);
P
Pierre Ossman 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

	ret = 0;

out:
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

static int if_sdio_handle_event(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
	int ret;
	u32 event;

	lbs_deb_enter(LBS_DEB_SDIO);

286
	if (card->model == MODEL_8385) {
P
Pierre Ossman 已提交
287 288 289
		event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
		if (ret)
			goto out;
290 291 292

		/* right shift 3 bits to get the event id */
		event >>= 3;
P
Pierre Ossman 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305
	} else {
		if (size < 4) {
			lbs_deb_sdio("event packet too small (%d bytes)\n",
				(int)size);
			ret = -EINVAL;
			goto out;
		}
		event = buffer[3] << 24;
		event |= buffer[2] << 16;
		event |= buffer[1] << 8;
		event |= buffer[0] << 0;
	}

306
	lbs_queue_event(card->priv, event & 0xFF);
P
Pierre Ossman 已提交
307 308 309 310 311 312 313 314
	ret = 0;

out:
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

315 316 317 318 319 320 321 322 323
static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition)
{
	u8 status;
	unsigned long timeout;
	int ret = 0;

	timeout = jiffies + HZ;
	while (1) {
		status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
324 325 326
		if (ret)
			return ret;
		if ((status & condition) == condition)
327 328 329 330 331 332 333 334
			break;
		if (time_after(jiffies, timeout))
			return -ETIMEDOUT;
		mdelay(1);
	}
	return ret;
}

P
Pierre Ossman 已提交
335 336 337 338 339 340 341
static int if_sdio_card_to_host(struct if_sdio_card *card)
{
	int ret;
	u16 size, type, chunk;

	lbs_deb_enter(LBS_DEB_SDIO);

342
	size = if_sdio_read_rx_len(card, &ret);
P
Pierre Ossman 已提交
343 344 345 346 347 348 349 350 351 352
	if (ret)
		goto out;

	if (size < 4) {
		lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
			(int)size);
		ret = -EINVAL;
		goto out;
	}

353 354 355
	ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
	if (ret)
		goto out;
P
Pierre Ossman 已提交
356 357 358

	/*
	 * The transfer must be in one transaction or the firmware
359 360
	 * goes suicidal. There's no way to guarantee that for all
	 * controllers, but we can at least try.
P
Pierre Ossman 已提交
361
	 */
362
	chunk = sdio_align_size(card->func, size);
P
Pierre Ossman 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

	ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
	if (ret)
		goto out;

	chunk = card->buffer[0] | (card->buffer[1] << 8);
	type = card->buffer[2] | (card->buffer[3] << 8);

	lbs_deb_sdio("packet of type %d and size %d bytes\n",
		(int)type, (int)chunk);

	if (chunk > size) {
		lbs_deb_sdio("packet fragment (%d > %d)\n",
			(int)chunk, (int)size);
		ret = -EINVAL;
		goto out;
	}

	if (chunk < size) {
		lbs_deb_sdio("packet fragment (%d < %d)\n",
			(int)chunk, (int)size);
	}

	switch (type) {
	case MVMS_CMD:
		ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
		if (ret)
			goto out;
		break;
	case MVMS_DAT:
		ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
		if (ret)
			goto out;
		break;
	case MVMS_EVENT:
		ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
		if (ret)
			goto out;
		break;
	default:
		lbs_deb_sdio("invalid type (%d) from firmware\n",
				(int)type);
		ret = -EINVAL;
		goto out;
	}

out:
	if (ret)
411
		pr_err("problem fetching packet from firmware\n");
P
Pierre Ossman 已提交
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 437 438 439 440

	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

static void if_sdio_host_to_card_worker(struct work_struct *work)
{
	struct if_sdio_card *card;
	struct if_sdio_packet *packet;
	int ret;
	unsigned long flags;

	lbs_deb_enter(LBS_DEB_SDIO);

	card = container_of(work, struct if_sdio_card, packet_worker);

	while (1) {
		spin_lock_irqsave(&card->lock, flags);
		packet = card->packets;
		if (packet)
			card->packets = packet->next;
		spin_unlock_irqrestore(&card->lock, flags);

		if (!packet)
			break;

		sdio_claim_host(card->func);

441 442 443 444
		ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
		if (ret == 0) {
			ret = sdio_writesb(card->func, card->ioport,
					   packet->buffer, packet->nb);
P
Pierre Ossman 已提交
445 446 447
		}

		if (ret)
448
			pr_err("error %d sending packet to firmware\n", ret);
449

P
Pierre Ossman 已提交
450 451 452 453 454 455 456 457 458 459 460 461
		sdio_release_host(card->func);

		kfree(packet);
	}

	lbs_deb_leave(LBS_DEB_SDIO);
}

/********************************************************************/
/* Firmware                                                         */
/********************************************************************/

462 463
#define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)

464 465
static int if_sdio_prog_helper(struct if_sdio_card *card,
				const struct firmware *fw)
P
Pierre Ossman 已提交
466 467 468 469 470
{
	int ret;
	unsigned long timeout;
	u8 *chunk_buffer;
	u32 chunk_size;
471
	const u8 *firmware;
P
Pierre Ossman 已提交
472 473 474 475 476 477 478
	size_t size;

	lbs_deb_enter(LBS_DEB_SDIO);

	chunk_buffer = kzalloc(64, GFP_KERNEL);
	if (!chunk_buffer) {
		ret = -ENOMEM;
479
		goto out;
P
Pierre Ossman 已提交
480 481 482 483 484 485 486 487 488 489 490 491
	}

	sdio_claim_host(card->func);

	ret = sdio_set_block_size(card->func, 32);
	if (ret)
		goto release;

	firmware = fw->data;
	size = fw->size;

	while (size) {
492 493 494
		ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
		if (ret)
			goto release;
P
Pierre Ossman 已提交
495

496 497 498 499
		/* On some platforms (like Davinci) the chip needs more time
		 * between helper blocks.
		 */
		mdelay(2);
P
Pierre Ossman 已提交
500 501 502

		chunk_size = min(size, (size_t)60);

H
Holger Schurig 已提交
503
		*((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
P
Pierre Ossman 已提交
504 505 506 507 508 509 510 511 512 513 514 515 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
		memcpy(chunk_buffer + 4, firmware, chunk_size);
/*
		lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
*/
		ret = sdio_writesb(card->func, card->ioport,
				chunk_buffer, 64);
		if (ret)
			goto release;

		firmware += chunk_size;
		size -= chunk_size;
	}

	/* an empty block marks the end of the transfer */
	memset(chunk_buffer, 0, 4);
	ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
	if (ret)
		goto release;

	lbs_deb_sdio("waiting for helper to boot...\n");

	/* wait for the helper to boot by looking at the size register */
	timeout = jiffies + HZ;
	while (1) {
		u16 req_size;

		req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
		if (ret)
			goto release;

		req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
		if (ret)
			goto release;

		if (req_size != 0)
			break;

		if (time_after(jiffies, timeout)) {
			ret = -ETIMEDOUT;
			goto release;
		}

		msleep(10);
	}

	ret = 0;

release:
	sdio_release_host(card->func);
	kfree(chunk_buffer);

out:
	if (ret)
557
		pr_err("failed to load helper firmware\n");
P
Pierre Ossman 已提交
558 559 560 561 562

	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
	return ret;
}

563 564
static int if_sdio_prog_real(struct if_sdio_card *card,
				const struct firmware *fw)
P
Pierre Ossman 已提交
565 566 567 568 569
{
	int ret;
	unsigned long timeout;
	u8 *chunk_buffer;
	u32 chunk_size;
570
	const u8 *firmware;
P
Pierre Ossman 已提交
571 572 573 574 575 576 577
	size_t size, req_size;

	lbs_deb_enter(LBS_DEB_SDIO);

	chunk_buffer = kzalloc(512, GFP_KERNEL);
	if (!chunk_buffer) {
		ret = -ENOMEM;
578
		goto out;
P
Pierre Ossman 已提交
579 580 581 582 583 584 585 586 587 588 589 590
	}

	sdio_claim_host(card->func);

	ret = sdio_set_block_size(card->func, 32);
	if (ret)
		goto release;

	firmware = fw->data;
	size = fw->size;

	while (size) {
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
		timeout = jiffies + HZ;
		while (1) {
			ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
			if (ret)
				goto release;

			req_size = sdio_readb(card->func, IF_SDIO_RD_BASE,
					&ret);
			if (ret)
				goto release;

			req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1,
					&ret) << 8;
			if (ret)
				goto release;

			/*
			 * For SD8688 wait until the length is not 0, 1 or 2
			 * before downloading the first FW block,
			 * since BOOT code writes the register to indicate the
			 * helper/FW download winner,
			 * the value could be 1 or 2 (Func1 or Func2).
			 */
			if ((size != fw->size) || (req_size > 2))
				break;
			if (time_after(jiffies, timeout)) {
				ret = -ETIMEDOUT;
				goto release;
			}
			mdelay(1);
		}
P
Pierre Ossman 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649

/*
		lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
*/
		if (req_size == 0) {
			lbs_deb_sdio("firmware helper gave up early\n");
			ret = -EIO;
			goto release;
		}

		if (req_size & 0x01) {
			lbs_deb_sdio("firmware helper signalled error\n");
			ret = -EIO;
			goto release;
		}

		if (req_size > size)
			req_size = size;

		while (req_size) {
			chunk_size = min(req_size, (size_t)512);

			memcpy(chunk_buffer, firmware, chunk_size);
/*
			lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
				chunk_size, (chunk_size + 31) / 32 * 32);
*/
			ret = sdio_writesb(card->func, card->ioport,
650
				chunk_buffer, roundup(chunk_size, 32));
P
Pierre Ossman 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
			if (ret)
				goto release;

			firmware += chunk_size;
			size -= chunk_size;
			req_size -= chunk_size;
		}
	}

	ret = 0;

	lbs_deb_sdio("waiting for firmware to boot...\n");

	/* wait for the firmware to boot */
	timeout = jiffies + HZ;
	while (1) {
		u16 scratch;

		scratch = if_sdio_read_scratch(card, &ret);
		if (ret)
			goto release;

		if (scratch == IF_SDIO_FIRMWARE_OK)
			break;

		if (time_after(jiffies, timeout)) {
			ret = -ETIMEDOUT;
			goto release;
		}

		msleep(10);
	}

	ret = 0;

release:
	sdio_release_host(card->func);
	kfree(chunk_buffer);

out:
	if (ret)
692
		pr_err("failed to load firmware\n");
P
Pierre Ossman 已提交
693 694 695 696 697

	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
	return ret;
}

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
static void if_sdio_do_prog_firmware(struct lbs_private *priv, int ret,
				     const struct firmware *helper,
				     const struct firmware *mainfw)
{
	struct if_sdio_card *card = priv->card;

	if (ret) {
		pr_err("failed to find firmware (%d)\n", ret);
		return;
	}

	ret = if_sdio_prog_helper(card, helper);
	if (ret)
		goto out;

	lbs_deb_sdio("Helper firmware loaded\n");

	ret = if_sdio_prog_real(card, mainfw);
	if (ret)
		goto out;

	lbs_deb_sdio("Firmware loaded\n");
	if_sdio_finish_power_on(card);

out:
	release_firmware(helper);
	release_firmware(mainfw);
}

P
Pierre Ossman 已提交
727 728 729 730 731 732 733
static int if_sdio_prog_firmware(struct if_sdio_card *card)
{
	int ret;
	u16 scratch;

	lbs_deb_enter(LBS_DEB_SDIO);

P
Paul Fox 已提交
734 735 736 737 738 739 740
	/*
	 * Disable interrupts
	 */
	sdio_claim_host(card->func);
	sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret);
	sdio_release_host(card->func);

P
Pierre Ossman 已提交
741 742 743 744
	sdio_claim_host(card->func);
	scratch = if_sdio_read_scratch(card, &ret);
	sdio_release_host(card->func);

P
Paul Fox 已提交
745 746 747
	lbs_deb_sdio("firmware status = %#x\n", scratch);
	lbs_deb_sdio("scratch ret = %d\n", ret);

P
Pierre Ossman 已提交
748 749 750
	if (ret)
		goto out;

751

P
Paul Fox 已提交
752 753 754 755 756 757 758 759 760 761
	/*
	 * The manual clearly describes that FEDC is the right code to use
	 * to detect firmware presence, but for SD8686 it is not that simple.
	 * Scratch is also used to store the RX packet length, so we lose
	 * the FEDC value early on. So we use a non-zero check in order
	 * to validate firmware presence.
	 * Additionally, the SD8686 in the Gumstix always has the high scratch
	 * bit set, even when the firmware is not loaded. So we have to
	 * exclude that from the test.
	 */
P
Pierre Ossman 已提交
762 763
	if (scratch == IF_SDIO_FIRMWARE_OK) {
		lbs_deb_sdio("firmware already loaded\n");
764 765
		if_sdio_finish_power_on(card);
		return 0;
P
Paul Fox 已提交
766 767
	} else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) {
		lbs_deb_sdio("firmware may be running\n");
768 769
		if_sdio_finish_power_on(card);
		return 0;
770 771
	}

772 773
	ret = lbs_get_firmware_async(card->priv, &card->func->dev, card->model,
				     fw_table, if_sdio_do_prog_firmware);
P
Pierre Ossman 已提交
774 775

out:
776
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
P
Pierre Ossman 已提交
777 778 779
	return ret;
}

780 781 782 783
/********************************************************************/
/* Power management                                                 */
/********************************************************************/

784 785
/* Finish power on sequence (after firmware is loaded) */
static void if_sdio_finish_power_on(struct if_sdio_card *card)
786 787 788 789 790 791
{
	struct sdio_func *func = card->func;
	struct lbs_private *priv = card->priv;
	int ret;

	sdio_claim_host(func);
792
	sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816

	/*
	 * Get rx_unit if the chip is SD8688 or newer.
	 * SD8385 & SD8686 do not have rx_unit.
	 */
	if ((card->model != MODEL_8385)
			&& (card->model != MODEL_8686))
		card->rx_unit = if_sdio_read_rx_unit(card);
	else
		card->rx_unit = 0;

	/*
	 * Set up the interrupt handler late.
	 *
	 * If we set it up earlier, the (buggy) hardware generates a spurious
	 * interrupt, even before the interrupt has been enabled, with
	 * CCCR_INTx = 0.
	 *
	 * We register the interrupt handler late so that we can handle any
	 * spurious interrupts, and also to avoid generation of that known
	 * spurious interrupt in the first place.
	 */
	ret = sdio_claim_irq(func, if_sdio_interrupt);
	if (ret)
817
		goto release;
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

	/*
	 * Enable interrupts now that everything is set up
	 */
	sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
	if (ret)
		goto release_irq;

	sdio_release_host(func);

	/*
	 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
	 */
	if (card->model == MODEL_8688) {
		struct cmd_header cmd;

		memset(&cmd, 0, sizeof(cmd));

		lbs_deb_sdio("send function INIT command\n");
		if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
				lbs_cmd_copyback, (unsigned long) &cmd))
			netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n");
	}

	priv->fw_ready = 1;
843
	wake_up(&card->pwron_waitq);
844

845 846 847 848 849 850 851 852 853 854 855 856
	if (!card->started) {
		ret = lbs_start_card(priv);
		if_sdio_power_off(card);
		if (ret == 0) {
			card->started = true;
			/* Tell PM core that we don't need the card to be
			 * powered now */
			pm_runtime_put_noidle(&func->dev);
		}
	}

	return;
857 858 859

release_irq:
	sdio_release_irq(func);
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
release:
	sdio_release_host(func);
}

static int if_sdio_power_on(struct if_sdio_card *card)
{
	struct sdio_func *func = card->func;
	struct mmc_host *host = func->card->host;
	int ret;

	sdio_claim_host(func);

	ret = sdio_enable_func(func);
	if (ret)
		goto release;

	/* For 1-bit transfers to the 8686 model, we need to enable the
	 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
	 * bit to allow access to non-vendor registers. */
	if ((card->model == MODEL_8686) &&
	    (host->caps & MMC_CAP_SDIO_IRQ) &&
	    (host->ios.bus_width == MMC_BUS_WIDTH_1)) {
		u8 reg;

		func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
		reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret);
		if (ret)
			goto disable;

		reg |= SDIO_BUS_ECSI;
		sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret);
		if (ret)
			goto disable;
	}

	card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
	if (ret)
		goto disable;

	card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
	if (ret)
		goto disable;

	card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
	if (ret)
		goto disable;

	sdio_release_host(func);
	ret = if_sdio_prog_firmware(card);
	if (ret) {
		sdio_disable_func(func);
		return ret;
	}

	return 0;

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
disable:
	sdio_disable_func(func);
release:
	sdio_release_host(func);
	return ret;
}

static int if_sdio_power_off(struct if_sdio_card *card)
{
	struct sdio_func *func = card->func;
	struct lbs_private *priv = card->priv;

	priv->fw_ready = 0;

	sdio_claim_host(func);
	sdio_release_irq(func);
	sdio_disable_func(func);
	sdio_release_host(func);
	return 0;
}


P
Pierre Ossman 已提交
938 939 940 941
/*******************************************************************/
/* Libertas callbacks                                              */
/*******************************************************************/

942 943
static int if_sdio_host_to_card(struct lbs_private *priv,
		u8 type, u8 *buf, u16 nb)
P
Pierre Ossman 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
{
	int ret;
	struct if_sdio_card *card;
	struct if_sdio_packet *packet, *cur;
	u16 size;
	unsigned long flags;

	lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);

	card = priv->card;

	if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
		ret = -EINVAL;
		goto out;
	}

	/*
	 * The transfer must be in one transaction or the firmware
962 963
	 * goes suicidal. There's no way to guarantee that for all
	 * controllers, but we can at least try.
P
Pierre Ossman 已提交
964
	 */
965
	size = sdio_align_size(card->func, nb + 4);
P
Pierre Ossman 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

	packet = kzalloc(sizeof(struct if_sdio_packet) + size,
			GFP_ATOMIC);
	if (!packet) {
		ret = -ENOMEM;
		goto out;
	}

	packet->next = NULL;
	packet->nb = size;

	/*
	 * SDIO specific header.
	 */
	packet->buffer[0] = (nb + 4) & 0xff;
	packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
	packet->buffer[2] = type;
	packet->buffer[3] = 0;

	memcpy(packet->buffer + 4, buf, nb);

	spin_lock_irqsave(&card->lock, flags);

	if (!card->packets)
		card->packets = packet;
	else {
		cur = card->packets;
		while (cur->next)
			cur = cur->next;
		cur->next = packet;
	}

	switch (type) {
	case MVMS_CMD:
		priv->dnld_sent = DNLD_CMD_SENT;
		break;
	case MVMS_DAT:
		priv->dnld_sent = DNLD_DATA_SENT;
		break;
	default:
		lbs_deb_sdio("unknown packet type %d\n", (int)type);
	}

	spin_unlock_irqrestore(&card->lock, flags);

1011
	queue_work(card->workqueue, &card->packet_worker);
P
Pierre Ossman 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020

	ret = 0;

out:
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
{
	int ret = -1;
	struct cmd_header cmd;

	memset(&cmd, 0, sizeof(cmd));

	lbs_deb_sdio("send DEEP_SLEEP command\n");
	ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
			lbs_cmd_copyback, (unsigned long) &cmd);
	if (ret)
1032
		netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n");
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

	mdelay(200);
	return ret;
}

static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
{
	struct if_sdio_card *card = priv->card;
	int ret = -1;

	lbs_deb_enter(LBS_DEB_SDIO);
	sdio_claim_host(card->func);

	sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
	if (ret)
1048
		netdev_err(priv->dev, "sdio_writeb failed!\n");
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

	sdio_release_host(card->func);
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
	return ret;
}

static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
{
	struct if_sdio_card *card = priv->card;
	int ret = -1;

	lbs_deb_enter(LBS_DEB_SDIO);
	sdio_claim_host(card->func);

	sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
	if (ret)
1065
		netdev_err(priv->dev, "sdio_writeb failed!\n");
1066 1067 1068 1069 1070 1071 1072

	sdio_release_host(card->func);
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
	return ret;

}

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
static struct mmc_host *reset_host;

static void if_sdio_reset_card_worker(struct work_struct *work)
{
	/*
	 * The actual reset operation must be run outside of lbs_thread. This
	 * is because mmc_remove_host() will cause the device to be instantly
	 * destroyed, and the libertas driver then needs to end lbs_thread,
	 * leading to a deadlock.
	 *
	 * We run it in a workqueue totally independent from the if_sdio_card
	 * instance for that reason.
	 */

	pr_info("Resetting card...");
	mmc_remove_host(reset_host);
	mmc_add_host(reset_host);
}
static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker);

static void if_sdio_reset_card(struct lbs_private *priv)
{
	struct if_sdio_card *card = priv->card;

	if (work_pending(&card_reset_work))
		return;

	reset_host = card->func->card->host;
	schedule_work(&card_reset_work);
}

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
static int if_sdio_power_save(struct lbs_private *priv)
{
	struct if_sdio_card *card = priv->card;
	int ret;

	flush_workqueue(card->workqueue);

	ret = if_sdio_power_off(card);

	/* Let runtime PM know the card is powered off */
	pm_runtime_put_sync(&card->func->dev);

	return ret;
}

static int if_sdio_power_restore(struct lbs_private *priv)
{
	struct if_sdio_card *card = priv->card;
1122
	int r;
1123 1124 1125 1126

	/* Make sure the card will not be powered off by runtime PM */
	pm_runtime_get_sync(&card->func->dev);

1127 1128 1129 1130 1131 1132
	r = if_sdio_power_on(card);
	if (r)
		return r;

	wait_event(card->pwron_waitq, priv->fw_ready);
	return 0;
1133 1134 1135
}


P
Pierre Ossman 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
/*******************************************************************/
/* SDIO callbacks                                                  */
/*******************************************************************/

static void if_sdio_interrupt(struct sdio_func *func)
{
	int ret;
	struct if_sdio_card *card;
	u8 cause;

	lbs_deb_enter(LBS_DEB_SDIO);

	card = sdio_get_drvdata(func);

	cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
1151
	if (ret || !cause)
P
Pierre Ossman 已提交
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		goto out;

	lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);

	sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
	if (ret)
		goto out;

	/*
	 * Ignore the define name, this really means the card has
	 * successfully received the command.
	 */
1164
	card->priv->is_activity_detected = 1;
1165 1166 1167
	if (cause & IF_SDIO_H_INT_DNLD)
		lbs_host_to_card_done(card->priv);

P
Pierre Ossman 已提交
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184

	if (cause & IF_SDIO_H_INT_UPLD) {
		ret = if_sdio_card_to_host(card);
		if (ret)
			goto out;
	}

	ret = 0;

out:
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
}

static int if_sdio_probe(struct sdio_func *func,
		const struct sdio_device_id *id)
{
	struct if_sdio_card *card;
1185
	struct lbs_private *priv;
P
Pierre Ossman 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	int ret, i;
	unsigned int model;
	struct if_sdio_packet *packet;

	lbs_deb_enter(LBS_DEB_SDIO);

	for (i = 0;i < func->card->num_info;i++) {
		if (sscanf(func->card->info[i],
				"802.11 SDIO ID: %x", &model) == 1)
			break;
		if (sscanf(func->card->info[i],
				"ID: %x", &model) == 1)
			break;
1199
		if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
1200
			model = MODEL_8385;
1201 1202
			break;
		}
P
Pierre Ossman 已提交
1203 1204 1205
	}

	if (i == func->card->num_info) {
1206
		pr_err("unable to identify card model\n");
P
Pierre Ossman 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215
		return -ENODEV;
	}

	card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
	if (!card)
		return -ENOMEM;

	card->func = func;
	card->model = model;
1216 1217

	switch (card->model) {
1218
	case MODEL_8385:
1219 1220
		card->scratch_reg = IF_SDIO_SCRATCH_OLD;
		break;
1221
	case MODEL_8686:
1222 1223
		card->scratch_reg = IF_SDIO_SCRATCH;
		break;
1224
	case MODEL_8688:
1225 1226 1227 1228 1229
	default: /* for newer chipsets */
		card->scratch_reg = IF_SDIO_FW_STATUS;
		break;
	}

P
Pierre Ossman 已提交
1230
	spin_lock_init(&card->lock);
1231
	card->workqueue = create_workqueue("libertas_sdio");
P
Pierre Ossman 已提交
1232
	INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
1233
	init_waitqueue_head(&card->pwron_waitq);
P
Pierre Ossman 已提交
1234

1235 1236 1237
	/* Check if we support this card */
	for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
		if (card->model == fw_table[i].model)
P
Pierre Ossman 已提交
1238 1239
			break;
	}
1240
	if (i == ARRAY_SIZE(fw_table)) {
1241
		pr_err("unknown card model 0x%x\n", card->model);
P
Pierre Ossman 已提交
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
		ret = -ENODEV;
		goto free;
	}

	sdio_set_drvdata(func, card);

	lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
			"device = 0x%X, model = 0x%X, ioport = 0x%X\n",
			func->class, func->vendor, func->device,
			model, (unsigned)card->ioport);


1254
	priv = lbs_add_card(card, &func->dev);
P
Pierre Ossman 已提交
1255 1256
	if (!priv) {
		ret = -ENOMEM;
1257
		goto free;
P
Pierre Ossman 已提交
1258 1259 1260 1261 1262 1263
	}

	card->priv = priv;

	priv->card = card;
	priv->hw_host_to_card = if_sdio_host_to_card;
1264 1265 1266
	priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
	priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
	priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
1267
	priv->reset_card = if_sdio_reset_card;
1268 1269
	priv->power_save = if_sdio_power_save;
	priv->power_restore = if_sdio_power_restore;
P
Pierre Ossman 已提交
1270

1271
	ret = if_sdio_power_on(card);
P
Pierre Ossman 已提交
1272
	if (ret)
1273
		goto err_activate_card;
1274

P
Pierre Ossman 已提交
1275 1276 1277 1278 1279 1280
out:
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;

err_activate_card:
1281 1282
	flush_workqueue(card->workqueue);
	lbs_remove_card(priv);
P
Pierre Ossman 已提交
1283
free:
1284
	destroy_workqueue(card->workqueue);
P
Pierre Ossman 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
	while (card->packets) {
		packet = card->packets;
		card->packets = card->packets->next;
		kfree(packet);
	}

	kfree(card);

	goto out;
}

static void if_sdio_remove(struct sdio_func *func)
{
	struct if_sdio_card *card;
	struct if_sdio_packet *packet;

	lbs_deb_enter(LBS_DEB_SDIO);

	card = sdio_get_drvdata(func);

1305 1306 1307
	/* Undo decrement done above in if_sdio_probe */
	pm_runtime_get_noresume(&func->dev);

1308
	if (user_rmmod && (card->model == MODEL_8688)) {
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
		/*
		 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
		 * multiple functions
		 */
		struct cmd_header cmd;

		memset(&cmd, 0, sizeof(cmd));

		lbs_deb_sdio("send function SHUTDOWN command\n");
		if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
				&cmd, sizeof(cmd), lbs_cmd_copyback,
				(unsigned long) &cmd))
1321
			pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1322
	}
1323

P
Pierre Ossman 已提交
1324 1325

	lbs_deb_sdio("call remove card\n");
1326
	lbs_stop_card(card->priv);
1327
	lbs_remove_card(card->priv);
P
Pierre Ossman 已提交
1328

1329 1330
	flush_workqueue(card->workqueue);
	destroy_workqueue(card->workqueue);
P
Pierre Ossman 已提交
1331 1332 1333 1334 1335 1336 1337

	while (card->packets) {
		packet = card->packets;
		card->packets = card->packets->next;
		kfree(packet);
	}

D
Daniel Drake 已提交
1338
	kfree(card);
P
Pierre Ossman 已提交
1339 1340 1341
	lbs_deb_leave(LBS_DEB_SDIO);
}

1342 1343 1344 1345 1346 1347 1348 1349
static int if_sdio_suspend(struct device *dev)
{
	struct sdio_func *func = dev_to_sdio_func(dev);
	int ret;
	struct if_sdio_card *card = sdio_get_drvdata(func);

	mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);

1350 1351 1352 1353 1354
	/* If we're powered off anyway, just let the mmc layer remove the
	 * card. */
	if (!lbs_iface_active(card->priv))
		return -ENOSYS;

1355 1356
	dev_info(dev, "%s: suspend: PM flags = 0x%x\n",
		 sdio_func_id(func), flags);
1357 1358 1359 1360 1361

	/* If we aren't being asked to wake on anything, we should bail out
	 * and let the SD stack power down the card.
	 */
	if (card->priv->wol_criteria == EHS_REMOVE_WAKEUP) {
1362
		dev_info(dev, "Suspend without wake params -- powering down card\n");
1363 1364 1365 1366
		return -ENOSYS;
	}

	if (!(flags & MMC_PM_KEEP_POWER)) {
1367 1368
		dev_err(dev, "%s: cannot remain alive while host is suspended\n",
			sdio_func_id(func));
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
		return -ENOSYS;
	}

	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
	if (ret)
		return ret;

	ret = lbs_suspend(card->priv);
	if (ret)
		return ret;

	return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
}

static int if_sdio_resume(struct device *dev)
{
	struct sdio_func *func = dev_to_sdio_func(dev);
	struct if_sdio_card *card = sdio_get_drvdata(func);
	int ret;

1389
	dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func));
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400

	ret = lbs_resume(card->priv);

	return ret;
}

static const struct dev_pm_ops if_sdio_pm_ops = {
	.suspend	= if_sdio_suspend,
	.resume		= if_sdio_resume,
};

P
Pierre Ossman 已提交
1401 1402 1403 1404 1405
static struct sdio_driver if_sdio_driver = {
	.name		= "libertas_sdio",
	.id_table	= if_sdio_ids,
	.probe		= if_sdio_probe,
	.remove		= if_sdio_remove,
1406 1407 1408
	.drv = {
		.pm = &if_sdio_pm_ops,
	},
P
Pierre Ossman 已提交
1409 1410 1411 1412 1413 1414
};

/*******************************************************************/
/* Module functions                                                */
/*******************************************************************/

1415
static int __init if_sdio_init_module(void)
P
Pierre Ossman 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
{
	int ret = 0;

	lbs_deb_enter(LBS_DEB_SDIO);

	printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
	printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");

	ret = sdio_register_driver(&if_sdio_driver);

1426 1427 1428
	/* Clear the flag in case user removes the card. */
	user_rmmod = 0;

P
Pierre Ossman 已提交
1429 1430 1431 1432 1433
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

1434
static void __exit if_sdio_exit_module(void)
P
Pierre Ossman 已提交
1435 1436 1437
{
	lbs_deb_enter(LBS_DEB_SDIO);

1438 1439
	/* Set the flag as user is removing this module. */
	user_rmmod = 1;
1440

1441 1442
	cancel_work_sync(&card_reset_work);

P
Pierre Ossman 已提交
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	sdio_unregister_driver(&if_sdio_driver);

	lbs_deb_leave(LBS_DEB_SDIO);
}

module_init(if_sdio_init_module);
module_exit(if_sdio_exit_module);

MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
MODULE_AUTHOR("Pierre Ossman");
MODULE_LICENSE("GPL");