Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 *  linux/drivers/net/wireless/libertas/if_sdio.c
   3 *
   4 *  Copyright 2007-2008 Pierre Ossman
   5 *
   6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or (at
  11 * your option) any later version.
  12 *
  13 * This hardware has more or less no CMD53 support, so all registers
  14 * must be accessed using sdio_readb()/sdio_writeb().
  15 *
  16 * Transfers must be in one transaction or the firmware goes bonkers.
  17 * This means that the transfer must either be small enough to do a
  18 * byte based transfer or it must be padded to a multiple of the
  19 * current block size.
  20 *
  21 * 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
  23 * controllers cannot do transfers that aren't a multiple of 4 bytes.
  24 * If you don't have time to fix the host controller driver, you can
  25 * work around the problem by modifying if_sdio_host_to_card() and
  26 * if_sdio_card_to_host() to pad the data.
  27 */
  28
  29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30
  31#include <linux/kernel.h>
  32#include <linux/moduleparam.h>
  33#include <linux/slab.h>
  34#include <linux/firmware.h>
  35#include <linux/netdevice.h>
  36#include <linux/delay.h>
  37#include <linux/mmc/card.h>
  38#include <linux/mmc/sdio_func.h>
  39#include <linux/mmc/sdio_ids.h>
  40#include <linux/mmc/sdio.h>
  41#include <linux/mmc/host.h>
 
  42
  43#include "host.h"
  44#include "decl.h"
  45#include "defs.h"
  46#include "dev.h"
  47#include "cmd.h"
  48#include "if_sdio.h"
  49
 
 
  50/* The if_sdio_remove() callback function is called when
  51 * user removes this module from kernel space or ejects
  52 * the card from the slot. The driver handles these 2 cases
  53 * differently for SD8688 combo chip.
  54 * If the user is removing the module, the FUNC_SHUTDOWN
  55 * command for SD8688 is sent to the firmware.
  56 * If the card is removed, there is no need to send this command.
  57 *
  58 * The variable 'user_rmmod' is used to distinguish these two
  59 * scenarios. This flag is initialized as FALSE in case the card
  60 * is removed, and will be set to TRUE for module removal when
  61 * module_exit function is called.
  62 */
  63static u8 user_rmmod;
  64
  65static char *lbs_helper_name = NULL;
  66module_param_named(helper_name, lbs_helper_name, charp, 0644);
  67
  68static char *lbs_fw_name = NULL;
  69module_param_named(fw_name, lbs_fw_name, charp, 0644);
  70
  71static const struct sdio_device_id if_sdio_ids[] = {
  72	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
  73			SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
  74	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
  75			SDIO_DEVICE_ID_MARVELL_8688WLAN) },
  76	{ /* end: all zeroes */				},
  77};
  78
  79MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
  80
  81#define MODEL_8385	0x04
  82#define MODEL_8686	0x0b
  83#define MODEL_8688	0x10
  84
  85static const struct lbs_fw_table fw_table[] = {
  86	{ MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
  87	{ MODEL_8385, "sd8385_helper.bin", "sd8385.bin" },
  88	{ MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
  89	{ MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
  90	{ MODEL_8686, "sd8686_helper.bin", "sd8686.bin" },
  91	{ MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
  92	{ MODEL_8688, "sd8688_helper.bin", "sd8688.bin" },
  93	{ 0, NULL, NULL }
  94};
  95MODULE_FIRMWARE("libertas/sd8385_helper.bin");
  96MODULE_FIRMWARE("libertas/sd8385.bin");
  97MODULE_FIRMWARE("sd8385_helper.bin");
  98MODULE_FIRMWARE("sd8385.bin");
  99MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
 100MODULE_FIRMWARE("libertas/sd8686_v9.bin");
 101MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
 102MODULE_FIRMWARE("libertas/sd8686_v8.bin");
 103MODULE_FIRMWARE("sd8686_helper.bin");
 104MODULE_FIRMWARE("sd8686.bin");
 105MODULE_FIRMWARE("libertas/sd8688_helper.bin");
 106MODULE_FIRMWARE("libertas/sd8688.bin");
 107MODULE_FIRMWARE("sd8688_helper.bin");
 108MODULE_FIRMWARE("sd8688.bin");
 109
 110struct if_sdio_packet {
 111	struct if_sdio_packet	*next;
 112	u16			nb;
 113	u8			buffer[0] __attribute__((aligned(4)));
 114};
 115
 116struct if_sdio_card {
 117	struct sdio_func	*func;
 118	struct lbs_private	*priv;
 119
 120	int			model;
 121	unsigned long		ioport;
 122	unsigned int		scratch_reg;
 123
 124	const char		*helper;
 125	const char		*firmware;
 126	bool			helper_allocated;
 127	bool			firmware_allocated;
 128
 129	u8			buffer[65536] __attribute__((aligned(4)));
 130
 131	spinlock_t		lock;
 132	struct if_sdio_packet	*packets;
 133
 134	struct workqueue_struct	*workqueue;
 135	struct work_struct	packet_worker;
 136
 137	u8			rx_unit;
 138};
 139
 
 
 
 140/********************************************************************/
 141/* I/O                                                              */
 142/********************************************************************/
 143
 144/*
 145 *  For SD8385/SD8686, this function reads firmware status after
 146 *  the image is downloaded, or reads RX packet length when
 147 *  interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
 148 *  For SD8688, this function reads firmware status only.
 149 */
 150static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
 151{
 152	int ret;
 153	u16 scratch;
 154
 155	scratch = sdio_readb(card->func, card->scratch_reg, &ret);
 156	if (!ret)
 157		scratch |= sdio_readb(card->func, card->scratch_reg + 1,
 158					&ret) << 8;
 159
 160	if (err)
 161		*err = ret;
 162
 163	if (ret)
 164		return 0xffff;
 165
 166	return scratch;
 167}
 168
 169static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
 170{
 171	int ret;
 172	u8 rx_unit;
 173
 174	rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);
 175
 176	if (ret)
 177		rx_unit = 0;
 178
 179	return rx_unit;
 180}
 181
 182static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
 183{
 184	int ret;
 185	u16 rx_len;
 186
 187	switch (card->model) {
 188	case MODEL_8385:
 189	case MODEL_8686:
 190		rx_len = if_sdio_read_scratch(card, &ret);
 191		break;
 192	case MODEL_8688:
 193	default: /* for newer chipsets */
 194		rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
 195		if (!ret)
 196			rx_len <<= card->rx_unit;
 197		else
 198			rx_len = 0xffff;	/* invalid length */
 199
 200		break;
 201	}
 202
 203	if (err)
 204		*err = ret;
 205
 206	return rx_len;
 207}
 208
 209static int if_sdio_handle_cmd(struct if_sdio_card *card,
 210		u8 *buffer, unsigned size)
 211{
 212	struct lbs_private *priv = card->priv;
 213	int ret;
 214	unsigned long flags;
 215	u8 i;
 216
 217	lbs_deb_enter(LBS_DEB_SDIO);
 218
 219	if (size > LBS_CMD_BUFFER_SIZE) {
 220		lbs_deb_sdio("response packet too large (%d bytes)\n",
 221			(int)size);
 222		ret = -E2BIG;
 223		goto out;
 224	}
 225
 226	spin_lock_irqsave(&priv->driver_lock, flags);
 227
 228	i = (priv->resp_idx == 0) ? 1 : 0;
 229	BUG_ON(priv->resp_len[i]);
 230	priv->resp_len[i] = size;
 231	memcpy(priv->resp_buf[i], buffer, size);
 232	lbs_notify_command_response(priv, i);
 233
 234	spin_unlock_irqrestore(&card->priv->driver_lock, flags);
 235
 236	ret = 0;
 237
 238out:
 239	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 240	return ret;
 241}
 242
 243static int if_sdio_handle_data(struct if_sdio_card *card,
 244		u8 *buffer, unsigned size)
 245{
 246	int ret;
 247	struct sk_buff *skb;
 248	char *data;
 249
 250	lbs_deb_enter(LBS_DEB_SDIO);
 251
 252	if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
 253		lbs_deb_sdio("response packet too large (%d bytes)\n",
 254			(int)size);
 255		ret = -E2BIG;
 256		goto out;
 257	}
 258
 259	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
 260	if (!skb) {
 261		ret = -ENOMEM;
 262		goto out;
 263	}
 264
 265	skb_reserve(skb, NET_IP_ALIGN);
 266
 267	data = skb_put(skb, size);
 268
 269	memcpy(data, buffer, size);
 270
 271	lbs_process_rxed_packet(card->priv, skb);
 272
 273	ret = 0;
 274
 275out:
 276	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 277
 278	return ret;
 279}
 280
 281static int if_sdio_handle_event(struct if_sdio_card *card,
 282		u8 *buffer, unsigned size)
 283{
 284	int ret;
 285	u32 event;
 286
 287	lbs_deb_enter(LBS_DEB_SDIO);
 288
 289	if (card->model == MODEL_8385) {
 290		event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
 291		if (ret)
 292			goto out;
 293
 294		/* right shift 3 bits to get the event id */
 295		event >>= 3;
 296	} else {
 297		if (size < 4) {
 298			lbs_deb_sdio("event packet too small (%d bytes)\n",
 299				(int)size);
 300			ret = -EINVAL;
 301			goto out;
 302		}
 303		event = buffer[3] << 24;
 304		event |= buffer[2] << 16;
 305		event |= buffer[1] << 8;
 306		event |= buffer[0] << 0;
 307	}
 308
 309	lbs_queue_event(card->priv, event & 0xFF);
 310	ret = 0;
 311
 312out:
 313	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 314
 315	return ret;
 316}
 317
 318static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition)
 319{
 320	u8 status;
 321	unsigned long timeout;
 322	int ret = 0;
 323
 324	timeout = jiffies + HZ;
 325	while (1) {
 326		status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
 327		if (ret)
 328			return ret;
 329		if ((status & condition) == condition)
 330			break;
 331		if (time_after(jiffies, timeout))
 332			return -ETIMEDOUT;
 333		mdelay(1);
 334	}
 335	return ret;
 336}
 337
 338static int if_sdio_card_to_host(struct if_sdio_card *card)
 339{
 340	int ret;
 341	u16 size, type, chunk;
 342
 343	lbs_deb_enter(LBS_DEB_SDIO);
 344
 345	size = if_sdio_read_rx_len(card, &ret);
 346	if (ret)
 347		goto out;
 348
 349	if (size < 4) {
 350		lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
 351			(int)size);
 352		ret = -EINVAL;
 353		goto out;
 354	}
 355
 356	ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
 357	if (ret)
 358		goto out;
 359
 360	/*
 361	 * The transfer must be in one transaction or the firmware
 362	 * goes suicidal. There's no way to guarantee that for all
 363	 * controllers, but we can at least try.
 364	 */
 365	chunk = sdio_align_size(card->func, size);
 366
 367	ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
 368	if (ret)
 369		goto out;
 370
 371	chunk = card->buffer[0] | (card->buffer[1] << 8);
 372	type = card->buffer[2] | (card->buffer[3] << 8);
 373
 374	lbs_deb_sdio("packet of type %d and size %d bytes\n",
 375		(int)type, (int)chunk);
 376
 377	if (chunk > size) {
 378		lbs_deb_sdio("packet fragment (%d > %d)\n",
 379			(int)chunk, (int)size);
 380		ret = -EINVAL;
 381		goto out;
 382	}
 383
 384	if (chunk < size) {
 385		lbs_deb_sdio("packet fragment (%d < %d)\n",
 386			(int)chunk, (int)size);
 387	}
 388
 389	switch (type) {
 390	case MVMS_CMD:
 391		ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
 392		if (ret)
 393			goto out;
 394		break;
 395	case MVMS_DAT:
 396		ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
 397		if (ret)
 398			goto out;
 399		break;
 400	case MVMS_EVENT:
 401		ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
 402		if (ret)
 403			goto out;
 404		break;
 405	default:
 406		lbs_deb_sdio("invalid type (%d) from firmware\n",
 407				(int)type);
 408		ret = -EINVAL;
 409		goto out;
 410	}
 411
 412out:
 413	if (ret)
 414		pr_err("problem fetching packet from firmware\n");
 415
 416	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 417
 418	return ret;
 419}
 420
 421static void if_sdio_host_to_card_worker(struct work_struct *work)
 422{
 423	struct if_sdio_card *card;
 424	struct if_sdio_packet *packet;
 425	int ret;
 426	unsigned long flags;
 427
 428	lbs_deb_enter(LBS_DEB_SDIO);
 429
 430	card = container_of(work, struct if_sdio_card, packet_worker);
 431
 432	while (1) {
 433		spin_lock_irqsave(&card->lock, flags);
 434		packet = card->packets;
 435		if (packet)
 436			card->packets = packet->next;
 437		spin_unlock_irqrestore(&card->lock, flags);
 438
 439		if (!packet)
 440			break;
 441
 442		sdio_claim_host(card->func);
 443
 444		ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
 445		if (ret == 0) {
 446			ret = sdio_writesb(card->func, card->ioport,
 447					   packet->buffer, packet->nb);
 448		}
 449
 450		if (ret)
 451			pr_err("error %d sending packet to firmware\n", ret);
 452
 453		sdio_release_host(card->func);
 454
 455		kfree(packet);
 456	}
 457
 458	lbs_deb_leave(LBS_DEB_SDIO);
 459}
 460
 461/********************************************************************/
 462/* Firmware                                                         */
 463/********************************************************************/
 464
 465#define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
 466
 467static int if_sdio_prog_helper(struct if_sdio_card *card,
 468				const struct firmware *fw)
 469{
 470	int ret;
 471	unsigned long timeout;
 472	u8 *chunk_buffer;
 473	u32 chunk_size;
 474	const u8 *firmware;
 475	size_t size;
 476
 477	lbs_deb_enter(LBS_DEB_SDIO);
 478
 479	chunk_buffer = kzalloc(64, GFP_KERNEL);
 480	if (!chunk_buffer) {
 481		ret = -ENOMEM;
 482		goto out;
 483	}
 484
 485	sdio_claim_host(card->func);
 486
 487	ret = sdio_set_block_size(card->func, 32);
 488	if (ret)
 489		goto release;
 490
 491	firmware = fw->data;
 492	size = fw->size;
 493
 494	while (size) {
 495		ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
 496		if (ret)
 497			goto release;
 498
 499		/* On some platforms (like Davinci) the chip needs more time
 500		 * between helper blocks.
 501		 */
 502		mdelay(2);
 503
 504		chunk_size = min(size, (size_t)60);
 505
 506		*((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
 507		memcpy(chunk_buffer + 4, firmware, chunk_size);
 508/*
 509		lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
 510*/
 511		ret = sdio_writesb(card->func, card->ioport,
 512				chunk_buffer, 64);
 513		if (ret)
 514			goto release;
 515
 516		firmware += chunk_size;
 517		size -= chunk_size;
 518	}
 519
 520	/* an empty block marks the end of the transfer */
 521	memset(chunk_buffer, 0, 4);
 522	ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
 523	if (ret)
 524		goto release;
 525
 526	lbs_deb_sdio("waiting for helper to boot...\n");
 527
 528	/* wait for the helper to boot by looking at the size register */
 529	timeout = jiffies + HZ;
 530	while (1) {
 531		u16 req_size;
 532
 533		req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
 534		if (ret)
 535			goto release;
 536
 537		req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
 538		if (ret)
 539			goto release;
 540
 541		if (req_size != 0)
 542			break;
 543
 544		if (time_after(jiffies, timeout)) {
 545			ret = -ETIMEDOUT;
 546			goto release;
 547		}
 548
 549		msleep(10);
 550	}
 551
 552	ret = 0;
 553
 554release:
 555	sdio_release_host(card->func);
 556	kfree(chunk_buffer);
 557
 558out:
 559	if (ret)
 560		pr_err("failed to load helper firmware\n");
 561
 562	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 563	return ret;
 564}
 565
 566static int if_sdio_prog_real(struct if_sdio_card *card,
 567				const struct firmware *fw)
 568{
 569	int ret;
 570	unsigned long timeout;
 571	u8 *chunk_buffer;
 572	u32 chunk_size;
 573	const u8 *firmware;
 574	size_t size, req_size;
 575
 576	lbs_deb_enter(LBS_DEB_SDIO);
 577
 578	chunk_buffer = kzalloc(512, GFP_KERNEL);
 579	if (!chunk_buffer) {
 580		ret = -ENOMEM;
 581		goto out;
 582	}
 583
 584	sdio_claim_host(card->func);
 585
 586	ret = sdio_set_block_size(card->func, 32);
 587	if (ret)
 588		goto release;
 589
 590	firmware = fw->data;
 591	size = fw->size;
 592
 593	while (size) {
 594		ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
 595		if (ret)
 596			goto release;
 
 
 597
 598		req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
 599		if (ret)
 600			goto release;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 601
 602		req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
 603		if (ret)
 604			goto release;
 605/*
 606		lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
 607*/
 608		if (req_size == 0) {
 609			lbs_deb_sdio("firmware helper gave up early\n");
 610			ret = -EIO;
 611			goto release;
 612		}
 613
 614		if (req_size & 0x01) {
 615			lbs_deb_sdio("firmware helper signalled error\n");
 616			ret = -EIO;
 617			goto release;
 618		}
 619
 620		if (req_size > size)
 621			req_size = size;
 622
 623		while (req_size) {
 624			chunk_size = min(req_size, (size_t)512);
 625
 626			memcpy(chunk_buffer, firmware, chunk_size);
 627/*
 628			lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
 629				chunk_size, (chunk_size + 31) / 32 * 32);
 630*/
 631			ret = sdio_writesb(card->func, card->ioport,
 632				chunk_buffer, roundup(chunk_size, 32));
 633			if (ret)
 634				goto release;
 635
 636			firmware += chunk_size;
 637			size -= chunk_size;
 638			req_size -= chunk_size;
 639		}
 640	}
 641
 642	ret = 0;
 643
 644	lbs_deb_sdio("waiting for firmware to boot...\n");
 645
 646	/* wait for the firmware to boot */
 647	timeout = jiffies + HZ;
 648	while (1) {
 649		u16 scratch;
 650
 651		scratch = if_sdio_read_scratch(card, &ret);
 652		if (ret)
 653			goto release;
 654
 655		if (scratch == IF_SDIO_FIRMWARE_OK)
 656			break;
 657
 658		if (time_after(jiffies, timeout)) {
 659			ret = -ETIMEDOUT;
 660			goto release;
 661		}
 662
 663		msleep(10);
 664	}
 665
 666	ret = 0;
 667
 668release:
 669	sdio_release_host(card->func);
 670	kfree(chunk_buffer);
 671
 672out:
 673	if (ret)
 674		pr_err("failed to load firmware\n");
 675
 676	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 677	return ret;
 678}
 679
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 680static int if_sdio_prog_firmware(struct if_sdio_card *card)
 681{
 682	int ret;
 683	u16 scratch;
 684	const struct firmware *helper = NULL;
 685	const struct firmware *mainfw = NULL;
 686
 687	lbs_deb_enter(LBS_DEB_SDIO);
 688
 689	/*
 690	 * Disable interrupts
 691	 */
 692	sdio_claim_host(card->func);
 693	sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret);
 694	sdio_release_host(card->func);
 695
 696	sdio_claim_host(card->func);
 697	scratch = if_sdio_read_scratch(card, &ret);
 698	sdio_release_host(card->func);
 699
 700	lbs_deb_sdio("firmware status = %#x\n", scratch);
 701	lbs_deb_sdio("scratch ret = %d\n", ret);
 702
 703	if (ret)
 704		goto out;
 705
 706
 707	/*
 708	 * The manual clearly describes that FEDC is the right code to use
 709	 * to detect firmware presence, but for SD8686 it is not that simple.
 710	 * Scratch is also used to store the RX packet length, so we lose
 711	 * the FEDC value early on. So we use a non-zero check in order
 712	 * to validate firmware presence.
 713	 * Additionally, the SD8686 in the Gumstix always has the high scratch
 714	 * bit set, even when the firmware is not loaded. So we have to
 715	 * exclude that from the test.
 716	 */
 717	if (scratch == IF_SDIO_FIRMWARE_OK) {
 718		lbs_deb_sdio("firmware already loaded\n");
 719		goto success;
 
 720	} else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) {
 721		lbs_deb_sdio("firmware may be running\n");
 722		goto success;
 
 723	}
 724
 725	ret = lbs_get_firmware(&card->func->dev, lbs_helper_name, lbs_fw_name,
 726				card->model, &fw_table[0], &helper, &mainfw);
 727	if (ret) {
 728		pr_err("failed to find firmware (%d)\n", ret);
 729		goto out;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 730	}
 731
 732	ret = if_sdio_prog_helper(card, helper);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 733	if (ret)
 734		goto out;
 735
 736	lbs_deb_sdio("Helper firmware loaded\n");
 
 
 
 
 
 
 737
 738	ret = if_sdio_prog_real(card, mainfw);
 
 
 
 
 
 
 
 
 
 
 
 739	if (ret)
 740		goto out;
 741
 742	lbs_deb_sdio("Firmware loaded\n");
 
 
 743
 744success:
 745	sdio_claim_host(card->func);
 746	sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
 747	sdio_release_host(card->func);
 748	ret = 0;
 749
 750out:
 751	if (helper)
 752		release_firmware(helper);
 753	if (mainfw)
 754		release_firmware(mainfw);
 
 755
 756	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 
 
 
 
 
 757	return ret;
 758}
 759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 760/*******************************************************************/
 761/* Libertas callbacks                                              */
 762/*******************************************************************/
 763
 764static int if_sdio_host_to_card(struct lbs_private *priv,
 765		u8 type, u8 *buf, u16 nb)
 766{
 767	int ret;
 768	struct if_sdio_card *card;
 769	struct if_sdio_packet *packet, *cur;
 770	u16 size;
 771	unsigned long flags;
 772
 773	lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
 774
 775	card = priv->card;
 776
 777	if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
 778		ret = -EINVAL;
 779		goto out;
 780	}
 781
 782	/*
 783	 * The transfer must be in one transaction or the firmware
 784	 * goes suicidal. There's no way to guarantee that for all
 785	 * controllers, but we can at least try.
 786	 */
 787	size = sdio_align_size(card->func, nb + 4);
 788
 789	packet = kzalloc(sizeof(struct if_sdio_packet) + size,
 790			GFP_ATOMIC);
 791	if (!packet) {
 792		ret = -ENOMEM;
 793		goto out;
 794	}
 795
 796	packet->next = NULL;
 797	packet->nb = size;
 798
 799	/*
 800	 * SDIO specific header.
 801	 */
 802	packet->buffer[0] = (nb + 4) & 0xff;
 803	packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
 804	packet->buffer[2] = type;
 805	packet->buffer[3] = 0;
 806
 807	memcpy(packet->buffer + 4, buf, nb);
 808
 809	spin_lock_irqsave(&card->lock, flags);
 810
 811	if (!card->packets)
 812		card->packets = packet;
 813	else {
 814		cur = card->packets;
 815		while (cur->next)
 816			cur = cur->next;
 817		cur->next = packet;
 818	}
 819
 820	switch (type) {
 821	case MVMS_CMD:
 822		priv->dnld_sent = DNLD_CMD_SENT;
 823		break;
 824	case MVMS_DAT:
 825		priv->dnld_sent = DNLD_DATA_SENT;
 826		break;
 827	default:
 828		lbs_deb_sdio("unknown packet type %d\n", (int)type);
 829	}
 830
 831	spin_unlock_irqrestore(&card->lock, flags);
 832
 833	queue_work(card->workqueue, &card->packet_worker);
 834
 835	ret = 0;
 836
 837out:
 838	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 839
 840	return ret;
 841}
 842
 843static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
 844{
 845	int ret = -1;
 846	struct cmd_header cmd;
 847
 848	memset(&cmd, 0, sizeof(cmd));
 849
 850	lbs_deb_sdio("send DEEP_SLEEP command\n");
 851	ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
 852			lbs_cmd_copyback, (unsigned long) &cmd);
 853	if (ret)
 854		netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n");
 855
 856	mdelay(200);
 857	return ret;
 858}
 859
 860static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
 861{
 862	struct if_sdio_card *card = priv->card;
 863	int ret = -1;
 864
 865	lbs_deb_enter(LBS_DEB_SDIO);
 866	sdio_claim_host(card->func);
 867
 868	sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
 869	if (ret)
 870		netdev_err(priv->dev, "sdio_writeb failed!\n");
 871
 872	sdio_release_host(card->func);
 873	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 874	return ret;
 875}
 876
 877static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
 878{
 879	struct if_sdio_card *card = priv->card;
 880	int ret = -1;
 881
 882	lbs_deb_enter(LBS_DEB_SDIO);
 883	sdio_claim_host(card->func);
 884
 885	sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
 886	if (ret)
 887		netdev_err(priv->dev, "sdio_writeb failed!\n");
 888
 889	sdio_release_host(card->func);
 890	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 891	return ret;
 892
 893}
 894
 895static struct mmc_host *reset_host;
 896
 897static void if_sdio_reset_card_worker(struct work_struct *work)
 898{
 899	/*
 900	 * The actual reset operation must be run outside of lbs_thread. This
 901	 * is because mmc_remove_host() will cause the device to be instantly
 902	 * destroyed, and the libertas driver then needs to end lbs_thread,
 903	 * leading to a deadlock.
 904	 *
 905	 * We run it in a workqueue totally independent from the if_sdio_card
 906	 * instance for that reason.
 907	 */
 908
 909	pr_info("Resetting card...");
 910	mmc_remove_host(reset_host);
 911	mmc_add_host(reset_host);
 912}
 913static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker);
 914
 915static void if_sdio_reset_card(struct lbs_private *priv)
 916{
 917	struct if_sdio_card *card = priv->card;
 918
 919	if (work_pending(&card_reset_work))
 920		return;
 921
 922	reset_host = card->func->card->host;
 923	schedule_work(&card_reset_work);
 924}
 925
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 926/*******************************************************************/
 927/* SDIO callbacks                                                  */
 928/*******************************************************************/
 929
 930static void if_sdio_interrupt(struct sdio_func *func)
 931{
 932	int ret;
 933	struct if_sdio_card *card;
 934	u8 cause;
 935
 936	lbs_deb_enter(LBS_DEB_SDIO);
 937
 938	card = sdio_get_drvdata(func);
 939
 940	cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
 941	if (ret || !cause)
 942		goto out;
 943
 944	lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
 945
 946	sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
 947	if (ret)
 948		goto out;
 949
 950	/*
 951	 * Ignore the define name, this really means the card has
 952	 * successfully received the command.
 953	 */
 954	card->priv->is_activity_detected = 1;
 955	if (cause & IF_SDIO_H_INT_DNLD)
 956		lbs_host_to_card_done(card->priv);
 957
 958
 959	if (cause & IF_SDIO_H_INT_UPLD) {
 960		ret = if_sdio_card_to_host(card);
 961		if (ret)
 962			goto out;
 963	}
 964
 965	ret = 0;
 966
 967out:
 968	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 969}
 970
 971static int if_sdio_probe(struct sdio_func *func,
 972		const struct sdio_device_id *id)
 973{
 974	struct if_sdio_card *card;
 975	struct lbs_private *priv;
 976	int ret, i;
 977	unsigned int model;
 978	struct if_sdio_packet *packet;
 979	struct mmc_host *host = func->card->host;
 980
 981	lbs_deb_enter(LBS_DEB_SDIO);
 982
 983	for (i = 0;i < func->card->num_info;i++) {
 984		if (sscanf(func->card->info[i],
 985				"802.11 SDIO ID: %x", &model) == 1)
 986			break;
 987		if (sscanf(func->card->info[i],
 988				"ID: %x", &model) == 1)
 989			break;
 990		if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
 991			model = MODEL_8385;
 992			break;
 993		}
 994	}
 995
 996	if (i == func->card->num_info) {
 997		pr_err("unable to identify card model\n");
 998		return -ENODEV;
 999	}
1000
1001	card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
1002	if (!card)
1003		return -ENOMEM;
1004
1005	card->func = func;
1006	card->model = model;
1007
1008	switch (card->model) {
1009	case MODEL_8385:
1010		card->scratch_reg = IF_SDIO_SCRATCH_OLD;
1011		break;
1012	case MODEL_8686:
1013		card->scratch_reg = IF_SDIO_SCRATCH;
1014		break;
1015	case MODEL_8688:
1016	default: /* for newer chipsets */
1017		card->scratch_reg = IF_SDIO_FW_STATUS;
1018		break;
1019	}
1020
1021	spin_lock_init(&card->lock);
1022	card->workqueue = create_workqueue("libertas_sdio");
1023	INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
 
1024
1025	/* Check if we support this card */
1026	for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1027		if (card->model == fw_table[i].model)
1028			break;
1029	}
1030	if (i == ARRAY_SIZE(fw_table)) {
1031		pr_err("unknown card model 0x%x\n", card->model);
1032		ret = -ENODEV;
1033		goto free;
1034	}
1035
1036	sdio_claim_host(func);
1037
1038	ret = sdio_enable_func(func);
1039	if (ret)
1040		goto release;
1041
1042	/* For 1-bit transfers to the 8686 model, we need to enable the
1043	 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
1044	 * bit to allow access to non-vendor registers. */
1045	if ((card->model == MODEL_8686) &&
1046	    (host->caps & MMC_CAP_SDIO_IRQ) &&
1047	    (host->ios.bus_width == MMC_BUS_WIDTH_1)) {
1048		u8 reg;
1049
1050		func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
1051		reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret);
1052		if (ret)
1053			goto release_int;
1054
1055		reg |= SDIO_BUS_ECSI;
1056		sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret);
1057		if (ret)
1058			goto release_int;
1059	}
1060
1061	card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
1062	if (ret)
1063		goto release_int;
1064
1065	card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
1066	if (ret)
1067		goto release_int;
1068
1069	card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
1070	if (ret)
1071		goto release_int;
1072
1073	sdio_release_host(func);
1074
1075	sdio_set_drvdata(func, card);
1076
1077	lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1078			"device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1079			func->class, func->vendor, func->device,
1080			model, (unsigned)card->ioport);
1081
1082	ret = if_sdio_prog_firmware(card);
1083	if (ret)
1084		goto reclaim;
1085
1086	priv = lbs_add_card(card, &func->dev);
1087	if (!priv) {
1088		ret = -ENOMEM;
1089		goto reclaim;
1090	}
1091
1092	card->priv = priv;
1093
1094	priv->card = card;
1095	priv->hw_host_to_card = if_sdio_host_to_card;
1096	priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
1097	priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
1098	priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
1099	priv->reset_card = if_sdio_reset_card;
 
 
1100
1101	sdio_claim_host(func);
1102
1103	/*
1104	 * Get rx_unit if the chip is SD8688 or newer.
1105	 * SD8385 & SD8686 do not have rx_unit.
1106	 */
1107	if ((card->model != MODEL_8385)
1108			&& (card->model != MODEL_8686))
1109		card->rx_unit = if_sdio_read_rx_unit(card);
1110	else
1111		card->rx_unit = 0;
1112
1113	/*
1114	 * Set up the interrupt handler late.
1115	 *
1116	 * If we set it up earlier, the (buggy) hardware generates a spurious
1117	 * interrupt, even before the interrupt has been enabled, with
1118	 * CCCR_INTx = 0.
1119	 *
1120	 * We register the interrupt handler late so that we can handle any
1121	 * spurious interrupts, and also to avoid generation of that known
1122	 * spurious interrupt in the first place.
1123	 */
1124	ret = sdio_claim_irq(func, if_sdio_interrupt);
1125	if (ret)
1126		goto disable;
1127
1128	/*
1129	 * Enable interrupts now that everything is set up
1130	 */
1131	sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
1132	sdio_release_host(func);
1133	if (ret)
1134		goto reclaim;
1135
1136	priv->fw_ready = 1;
1137
1138	/*
1139	 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
1140	 */
1141	if (card->model == MODEL_8688) {
1142		struct cmd_header cmd;
1143
1144		memset(&cmd, 0, sizeof(cmd));
1145
1146		lbs_deb_sdio("send function INIT command\n");
1147		if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
1148				lbs_cmd_copyback, (unsigned long) &cmd))
1149			netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n");
1150	}
1151
1152	ret = lbs_start_card(priv);
1153	if (ret)
1154		goto err_activate_card;
1155
1156out:
1157	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1158
1159	return ret;
1160
1161err_activate_card:
1162	flush_workqueue(card->workqueue);
1163	lbs_remove_card(priv);
1164reclaim:
1165	sdio_claim_host(func);
1166release_int:
1167	sdio_release_irq(func);
1168disable:
1169	sdio_disable_func(func);
1170release:
1171	sdio_release_host(func);
1172free:
1173	destroy_workqueue(card->workqueue);
1174	while (card->packets) {
1175		packet = card->packets;
1176		card->packets = card->packets->next;
1177		kfree(packet);
1178	}
1179
1180	if (card->helper_allocated)
1181		kfree(card->helper);
1182	if (card->firmware_allocated)
1183		kfree(card->firmware);
1184	kfree(card);
1185
1186	goto out;
1187}
1188
1189static void if_sdio_remove(struct sdio_func *func)
1190{
1191	struct if_sdio_card *card;
1192	struct if_sdio_packet *packet;
1193
1194	lbs_deb_enter(LBS_DEB_SDIO);
1195
1196	card = sdio_get_drvdata(func);
1197
 
 
 
1198	if (user_rmmod && (card->model == MODEL_8688)) {
1199		/*
1200		 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1201		 * multiple functions
1202		 */
1203		struct cmd_header cmd;
1204
1205		memset(&cmd, 0, sizeof(cmd));
1206
1207		lbs_deb_sdio("send function SHUTDOWN command\n");
1208		if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
1209				&cmd, sizeof(cmd), lbs_cmd_copyback,
1210				(unsigned long) &cmd))
1211			pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1212	}
1213
1214
1215	lbs_deb_sdio("call remove card\n");
1216	lbs_stop_card(card->priv);
1217	lbs_remove_card(card->priv);
1218
1219	flush_workqueue(card->workqueue);
1220	destroy_workqueue(card->workqueue);
1221
1222	sdio_claim_host(func);
1223	sdio_release_irq(func);
1224	sdio_disable_func(func);
1225	sdio_release_host(func);
1226
1227	while (card->packets) {
1228		packet = card->packets;
1229		card->packets = card->packets->next;
1230		kfree(packet);
1231	}
1232
1233	if (card->helper_allocated)
1234		kfree(card->helper);
1235	if (card->firmware_allocated)
1236		kfree(card->firmware);
1237	kfree(card);
1238
1239	lbs_deb_leave(LBS_DEB_SDIO);
1240}
1241
1242static int if_sdio_suspend(struct device *dev)
1243{
1244	struct sdio_func *func = dev_to_sdio_func(dev);
1245	int ret;
1246	struct if_sdio_card *card = sdio_get_drvdata(func);
1247
1248	mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
 
 
 
 
 
1249
1250	dev_info(dev, "%s: suspend: PM flags = 0x%x\n",
1251		 sdio_func_id(func), flags);
1252
1253	/* If we aren't being asked to wake on anything, we should bail out
1254	 * and let the SD stack power down the card.
1255	 */
1256	if (card->priv->wol_criteria == EHS_REMOVE_WAKEUP) {
1257		dev_info(dev, "Suspend without wake params -- powering down card\n");
1258		return -ENOSYS;
1259	}
1260
1261	if (!(flags & MMC_PM_KEEP_POWER)) {
1262		dev_err(dev, "%s: cannot remain alive while host is suspended\n",
1263			sdio_func_id(func));
1264		return -ENOSYS;
1265	}
1266
1267	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1268	if (ret)
1269		return ret;
1270
1271	ret = lbs_suspend(card->priv);
1272	if (ret)
1273		return ret;
1274
1275	return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
1276}
1277
1278static int if_sdio_resume(struct device *dev)
1279{
1280	struct sdio_func *func = dev_to_sdio_func(dev);
1281	struct if_sdio_card *card = sdio_get_drvdata(func);
1282	int ret;
1283
1284	dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func));
1285
1286	ret = lbs_resume(card->priv);
1287
1288	return ret;
1289}
1290
1291static const struct dev_pm_ops if_sdio_pm_ops = {
1292	.suspend	= if_sdio_suspend,
1293	.resume		= if_sdio_resume,
1294};
1295
1296static struct sdio_driver if_sdio_driver = {
1297	.name		= "libertas_sdio",
1298	.id_table	= if_sdio_ids,
1299	.probe		= if_sdio_probe,
1300	.remove		= if_sdio_remove,
1301	.drv = {
1302		.pm = &if_sdio_pm_ops,
1303	},
1304};
1305
1306/*******************************************************************/
1307/* Module functions                                                */
1308/*******************************************************************/
1309
1310static int __init if_sdio_init_module(void)
1311{
1312	int ret = 0;
1313
1314	lbs_deb_enter(LBS_DEB_SDIO);
1315
1316	printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1317	printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1318
1319	ret = sdio_register_driver(&if_sdio_driver);
1320
1321	/* Clear the flag in case user removes the card. */
1322	user_rmmod = 0;
1323
1324	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1325
1326	return ret;
1327}
1328
1329static void __exit if_sdio_exit_module(void)
1330{
1331	lbs_deb_enter(LBS_DEB_SDIO);
1332
1333	/* Set the flag as user is removing this module. */
1334	user_rmmod = 1;
1335
1336	cancel_work_sync(&card_reset_work);
1337
1338	sdio_unregister_driver(&if_sdio_driver);
1339
1340	lbs_deb_leave(LBS_DEB_SDIO);
1341}
1342
1343module_init(if_sdio_init_module);
1344module_exit(if_sdio_exit_module);
1345
1346MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1347MODULE_AUTHOR("Pierre Ossman");
1348MODULE_LICENSE("GPL");
v3.15
   1/*
   2 *  linux/drivers/net/wireless/libertas/if_sdio.c
   3 *
   4 *  Copyright 2007-2008 Pierre Ossman
   5 *
   6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or (at
  11 * your option) any later version.
  12 *
  13 * This hardware has more or less no CMD53 support, so all registers
  14 * must be accessed using sdio_readb()/sdio_writeb().
  15 *
  16 * Transfers must be in one transaction or the firmware goes bonkers.
  17 * This means that the transfer must either be small enough to do a
  18 * byte based transfer or it must be padded to a multiple of the
  19 * current block size.
  20 *
  21 * 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
  23 * controllers cannot do transfers that aren't a multiple of 4 bytes.
  24 * If you don't have time to fix the host controller driver, you can
  25 * work around the problem by modifying if_sdio_host_to_card() and
  26 * if_sdio_card_to_host() to pad the data.
  27 */
  28
  29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30
  31#include <linux/kernel.h>
  32#include <linux/module.h>
  33#include <linux/slab.h>
  34#include <linux/firmware.h>
  35#include <linux/netdevice.h>
  36#include <linux/delay.h>
  37#include <linux/mmc/card.h>
  38#include <linux/mmc/sdio_func.h>
  39#include <linux/mmc/sdio_ids.h>
  40#include <linux/mmc/sdio.h>
  41#include <linux/mmc/host.h>
  42#include <linux/pm_runtime.h>
  43
  44#include "host.h"
  45#include "decl.h"
  46#include "defs.h"
  47#include "dev.h"
  48#include "cmd.h"
  49#include "if_sdio.h"
  50
  51static void if_sdio_interrupt(struct sdio_func *func);
  52
  53/* The if_sdio_remove() callback function is called when
  54 * user removes this module from kernel space or ejects
  55 * the card from the slot. The driver handles these 2 cases
  56 * differently for SD8688 combo chip.
  57 * If the user is removing the module, the FUNC_SHUTDOWN
  58 * command for SD8688 is sent to the firmware.
  59 * If the card is removed, there is no need to send this command.
  60 *
  61 * The variable 'user_rmmod' is used to distinguish these two
  62 * scenarios. This flag is initialized as FALSE in case the card
  63 * is removed, and will be set to TRUE for module removal when
  64 * module_exit function is called.
  65 */
  66static u8 user_rmmod;
  67
 
 
 
 
 
 
  68static const struct sdio_device_id if_sdio_ids[] = {
  69	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
  70			SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
  71	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
  72			SDIO_DEVICE_ID_MARVELL_8688WLAN) },
  73	{ /* end: all zeroes */				},
  74};
  75
  76MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
  77
  78#define MODEL_8385	0x04
  79#define MODEL_8686	0x0b
  80#define MODEL_8688	0x10
  81
  82static const struct lbs_fw_table fw_table[] = {
  83	{ MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
  84	{ MODEL_8385, "sd8385_helper.bin", "sd8385.bin" },
  85	{ MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
  86	{ MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
  87	{ MODEL_8686, "sd8686_helper.bin", "sd8686.bin" },
  88	{ MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
  89	{ MODEL_8688, "sd8688_helper.bin", "sd8688.bin" },
  90	{ 0, NULL, NULL }
  91};
  92MODULE_FIRMWARE("libertas/sd8385_helper.bin");
  93MODULE_FIRMWARE("libertas/sd8385.bin");
  94MODULE_FIRMWARE("sd8385_helper.bin");
  95MODULE_FIRMWARE("sd8385.bin");
  96MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
  97MODULE_FIRMWARE("libertas/sd8686_v9.bin");
  98MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
  99MODULE_FIRMWARE("libertas/sd8686_v8.bin");
 100MODULE_FIRMWARE("sd8686_helper.bin");
 101MODULE_FIRMWARE("sd8686.bin");
 102MODULE_FIRMWARE("libertas/sd8688_helper.bin");
 103MODULE_FIRMWARE("libertas/sd8688.bin");
 104MODULE_FIRMWARE("sd8688_helper.bin");
 105MODULE_FIRMWARE("sd8688.bin");
 106
 107struct if_sdio_packet {
 108	struct if_sdio_packet	*next;
 109	u16			nb;
 110	u8			buffer[0] __attribute__((aligned(4)));
 111};
 112
 113struct if_sdio_card {
 114	struct sdio_func	*func;
 115	struct lbs_private	*priv;
 116
 117	int			model;
 118	unsigned long		ioport;
 119	unsigned int		scratch_reg;
 120	bool			started;
 121	wait_queue_head_t	pwron_waitq;
 
 
 
 122
 123	u8			buffer[65536] __attribute__((aligned(4)));
 124
 125	spinlock_t		lock;
 126	struct if_sdio_packet	*packets;
 127
 128	struct workqueue_struct	*workqueue;
 129	struct work_struct	packet_worker;
 130
 131	u8			rx_unit;
 132};
 133
 134static void if_sdio_finish_power_on(struct if_sdio_card *card);
 135static int if_sdio_power_off(struct if_sdio_card *card);
 136
 137/********************************************************************/
 138/* I/O                                                              */
 139/********************************************************************/
 140
 141/*
 142 *  For SD8385/SD8686, this function reads firmware status after
 143 *  the image is downloaded, or reads RX packet length when
 144 *  interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
 145 *  For SD8688, this function reads firmware status only.
 146 */
 147static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
 148{
 149	int ret;
 150	u16 scratch;
 151
 152	scratch = sdio_readb(card->func, card->scratch_reg, &ret);
 153	if (!ret)
 154		scratch |= sdio_readb(card->func, card->scratch_reg + 1,
 155					&ret) << 8;
 156
 157	if (err)
 158		*err = ret;
 159
 160	if (ret)
 161		return 0xffff;
 162
 163	return scratch;
 164}
 165
 166static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
 167{
 168	int ret;
 169	u8 rx_unit;
 170
 171	rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);
 172
 173	if (ret)
 174		rx_unit = 0;
 175
 176	return rx_unit;
 177}
 178
 179static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
 180{
 181	int ret;
 182	u16 rx_len;
 183
 184	switch (card->model) {
 185	case MODEL_8385:
 186	case MODEL_8686:
 187		rx_len = if_sdio_read_scratch(card, &ret);
 188		break;
 189	case MODEL_8688:
 190	default: /* for newer chipsets */
 191		rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
 192		if (!ret)
 193			rx_len <<= card->rx_unit;
 194		else
 195			rx_len = 0xffff;	/* invalid length */
 196
 197		break;
 198	}
 199
 200	if (err)
 201		*err = ret;
 202
 203	return rx_len;
 204}
 205
 206static int if_sdio_handle_cmd(struct if_sdio_card *card,
 207		u8 *buffer, unsigned size)
 208{
 209	struct lbs_private *priv = card->priv;
 210	int ret;
 211	unsigned long flags;
 212	u8 i;
 213
 214	lbs_deb_enter(LBS_DEB_SDIO);
 215
 216	if (size > LBS_CMD_BUFFER_SIZE) {
 217		lbs_deb_sdio("response packet too large (%d bytes)\n",
 218			(int)size);
 219		ret = -E2BIG;
 220		goto out;
 221	}
 222
 223	spin_lock_irqsave(&priv->driver_lock, flags);
 224
 225	i = (priv->resp_idx == 0) ? 1 : 0;
 226	BUG_ON(priv->resp_len[i]);
 227	priv->resp_len[i] = size;
 228	memcpy(priv->resp_buf[i], buffer, size);
 229	lbs_notify_command_response(priv, i);
 230
 231	spin_unlock_irqrestore(&card->priv->driver_lock, flags);
 232
 233	ret = 0;
 234
 235out:
 236	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 237	return ret;
 238}
 239
 240static int if_sdio_handle_data(struct if_sdio_card *card,
 241		u8 *buffer, unsigned size)
 242{
 243	int ret;
 244	struct sk_buff *skb;
 245	char *data;
 246
 247	lbs_deb_enter(LBS_DEB_SDIO);
 248
 249	if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
 250		lbs_deb_sdio("response packet too large (%d bytes)\n",
 251			(int)size);
 252		ret = -E2BIG;
 253		goto out;
 254	}
 255
 256	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
 257	if (!skb) {
 258		ret = -ENOMEM;
 259		goto out;
 260	}
 261
 262	skb_reserve(skb, NET_IP_ALIGN);
 263
 264	data = skb_put(skb, size);
 265
 266	memcpy(data, buffer, size);
 267
 268	lbs_process_rxed_packet(card->priv, skb);
 269
 270	ret = 0;
 271
 272out:
 273	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 274
 275	return ret;
 276}
 277
 278static int if_sdio_handle_event(struct if_sdio_card *card,
 279		u8 *buffer, unsigned size)
 280{
 281	int ret;
 282	u32 event;
 283
 284	lbs_deb_enter(LBS_DEB_SDIO);
 285
 286	if (card->model == MODEL_8385) {
 287		event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
 288		if (ret)
 289			goto out;
 290
 291		/* right shift 3 bits to get the event id */
 292		event >>= 3;
 293	} else {
 294		if (size < 4) {
 295			lbs_deb_sdio("event packet too small (%d bytes)\n",
 296				(int)size);
 297			ret = -EINVAL;
 298			goto out;
 299		}
 300		event = buffer[3] << 24;
 301		event |= buffer[2] << 16;
 302		event |= buffer[1] << 8;
 303		event |= buffer[0] << 0;
 304	}
 305
 306	lbs_queue_event(card->priv, event & 0xFF);
 307	ret = 0;
 308
 309out:
 310	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 311
 312	return ret;
 313}
 314
 315static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition)
 316{
 317	u8 status;
 318	unsigned long timeout;
 319	int ret = 0;
 320
 321	timeout = jiffies + HZ;
 322	while (1) {
 323		status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
 324		if (ret)
 325			return ret;
 326		if ((status & condition) == condition)
 327			break;
 328		if (time_after(jiffies, timeout))
 329			return -ETIMEDOUT;
 330		mdelay(1);
 331	}
 332	return ret;
 333}
 334
 335static int if_sdio_card_to_host(struct if_sdio_card *card)
 336{
 337	int ret;
 338	u16 size, type, chunk;
 339
 340	lbs_deb_enter(LBS_DEB_SDIO);
 341
 342	size = if_sdio_read_rx_len(card, &ret);
 343	if (ret)
 344		goto out;
 345
 346	if (size < 4) {
 347		lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
 348			(int)size);
 349		ret = -EINVAL;
 350		goto out;
 351	}
 352
 353	ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
 354	if (ret)
 355		goto out;
 356
 357	/*
 358	 * The transfer must be in one transaction or the firmware
 359	 * goes suicidal. There's no way to guarantee that for all
 360	 * controllers, but we can at least try.
 361	 */
 362	chunk = sdio_align_size(card->func, size);
 363
 364	ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
 365	if (ret)
 366		goto out;
 367
 368	chunk = card->buffer[0] | (card->buffer[1] << 8);
 369	type = card->buffer[2] | (card->buffer[3] << 8);
 370
 371	lbs_deb_sdio("packet of type %d and size %d bytes\n",
 372		(int)type, (int)chunk);
 373
 374	if (chunk > size) {
 375		lbs_deb_sdio("packet fragment (%d > %d)\n",
 376			(int)chunk, (int)size);
 377		ret = -EINVAL;
 378		goto out;
 379	}
 380
 381	if (chunk < size) {
 382		lbs_deb_sdio("packet fragment (%d < %d)\n",
 383			(int)chunk, (int)size);
 384	}
 385
 386	switch (type) {
 387	case MVMS_CMD:
 388		ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
 389		if (ret)
 390			goto out;
 391		break;
 392	case MVMS_DAT:
 393		ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
 394		if (ret)
 395			goto out;
 396		break;
 397	case MVMS_EVENT:
 398		ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
 399		if (ret)
 400			goto out;
 401		break;
 402	default:
 403		lbs_deb_sdio("invalid type (%d) from firmware\n",
 404				(int)type);
 405		ret = -EINVAL;
 406		goto out;
 407	}
 408
 409out:
 410	if (ret)
 411		pr_err("problem fetching packet from firmware\n");
 412
 413	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 414
 415	return ret;
 416}
 417
 418static void if_sdio_host_to_card_worker(struct work_struct *work)
 419{
 420	struct if_sdio_card *card;
 421	struct if_sdio_packet *packet;
 422	int ret;
 423	unsigned long flags;
 424
 425	lbs_deb_enter(LBS_DEB_SDIO);
 426
 427	card = container_of(work, struct if_sdio_card, packet_worker);
 428
 429	while (1) {
 430		spin_lock_irqsave(&card->lock, flags);
 431		packet = card->packets;
 432		if (packet)
 433			card->packets = packet->next;
 434		spin_unlock_irqrestore(&card->lock, flags);
 435
 436		if (!packet)
 437			break;
 438
 439		sdio_claim_host(card->func);
 440
 441		ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
 442		if (ret == 0) {
 443			ret = sdio_writesb(card->func, card->ioport,
 444					   packet->buffer, packet->nb);
 445		}
 446
 447		if (ret)
 448			pr_err("error %d sending packet to firmware\n", ret);
 449
 450		sdio_release_host(card->func);
 451
 452		kfree(packet);
 453	}
 454
 455	lbs_deb_leave(LBS_DEB_SDIO);
 456}
 457
 458/********************************************************************/
 459/* Firmware                                                         */
 460/********************************************************************/
 461
 462#define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
 463
 464static int if_sdio_prog_helper(struct if_sdio_card *card,
 465				const struct firmware *fw)
 466{
 467	int ret;
 468	unsigned long timeout;
 469	u8 *chunk_buffer;
 470	u32 chunk_size;
 471	const u8 *firmware;
 472	size_t size;
 473
 474	lbs_deb_enter(LBS_DEB_SDIO);
 475
 476	chunk_buffer = kzalloc(64, GFP_KERNEL);
 477	if (!chunk_buffer) {
 478		ret = -ENOMEM;
 479		goto out;
 480	}
 481
 482	sdio_claim_host(card->func);
 483
 484	ret = sdio_set_block_size(card->func, 32);
 485	if (ret)
 486		goto release;
 487
 488	firmware = fw->data;
 489	size = fw->size;
 490
 491	while (size) {
 492		ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
 493		if (ret)
 494			goto release;
 495
 496		/* On some platforms (like Davinci) the chip needs more time
 497		 * between helper blocks.
 498		 */
 499		mdelay(2);
 500
 501		chunk_size = min_t(size_t, size, 60);
 502
 503		*((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
 504		memcpy(chunk_buffer + 4, firmware, chunk_size);
 505/*
 506		lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
 507*/
 508		ret = sdio_writesb(card->func, card->ioport,
 509				chunk_buffer, 64);
 510		if (ret)
 511			goto release;
 512
 513		firmware += chunk_size;
 514		size -= chunk_size;
 515	}
 516
 517	/* an empty block marks the end of the transfer */
 518	memset(chunk_buffer, 0, 4);
 519	ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
 520	if (ret)
 521		goto release;
 522
 523	lbs_deb_sdio("waiting for helper to boot...\n");
 524
 525	/* wait for the helper to boot by looking at the size register */
 526	timeout = jiffies + HZ;
 527	while (1) {
 528		u16 req_size;
 529
 530		req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
 531		if (ret)
 532			goto release;
 533
 534		req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
 535		if (ret)
 536			goto release;
 537
 538		if (req_size != 0)
 539			break;
 540
 541		if (time_after(jiffies, timeout)) {
 542			ret = -ETIMEDOUT;
 543			goto release;
 544		}
 545
 546		msleep(10);
 547	}
 548
 549	ret = 0;
 550
 551release:
 552	sdio_release_host(card->func);
 553	kfree(chunk_buffer);
 554
 555out:
 556	if (ret)
 557		pr_err("failed to load helper firmware\n");
 558
 559	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 560	return ret;
 561}
 562
 563static int if_sdio_prog_real(struct if_sdio_card *card,
 564				const struct firmware *fw)
 565{
 566	int ret;
 567	unsigned long timeout;
 568	u8 *chunk_buffer;
 569	u32 chunk_size;
 570	const u8 *firmware;
 571	size_t size, req_size;
 572
 573	lbs_deb_enter(LBS_DEB_SDIO);
 574
 575	chunk_buffer = kzalloc(512, GFP_KERNEL);
 576	if (!chunk_buffer) {
 577		ret = -ENOMEM;
 578		goto out;
 579	}
 580
 581	sdio_claim_host(card->func);
 582
 583	ret = sdio_set_block_size(card->func, 32);
 584	if (ret)
 585		goto release;
 586
 587	firmware = fw->data;
 588	size = fw->size;
 589
 590	while (size) {
 591		timeout = jiffies + HZ;
 592		while (1) {
 593			ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
 594			if (ret)
 595				goto release;
 596
 597			req_size = sdio_readb(card->func, IF_SDIO_RD_BASE,
 598					&ret);
 599			if (ret)
 600				goto release;
 601
 602			req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1,
 603					&ret) << 8;
 604			if (ret)
 605				goto release;
 606
 607			/*
 608			 * For SD8688 wait until the length is not 0, 1 or 2
 609			 * before downloading the first FW block,
 610			 * since BOOT code writes the register to indicate the
 611			 * helper/FW download winner,
 612			 * the value could be 1 or 2 (Func1 or Func2).
 613			 */
 614			if ((size != fw->size) || (req_size > 2))
 615				break;
 616			if (time_after(jiffies, timeout)) {
 617				ret = -ETIMEDOUT;
 618				goto release;
 619			}
 620			mdelay(1);
 621		}
 622
 
 
 
 623/*
 624		lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
 625*/
 626		if (req_size == 0) {
 627			lbs_deb_sdio("firmware helper gave up early\n");
 628			ret = -EIO;
 629			goto release;
 630		}
 631
 632		if (req_size & 0x01) {
 633			lbs_deb_sdio("firmware helper signalled error\n");
 634			ret = -EIO;
 635			goto release;
 636		}
 637
 638		if (req_size > size)
 639			req_size = size;
 640
 641		while (req_size) {
 642			chunk_size = min_t(size_t, req_size, 512);
 643
 644			memcpy(chunk_buffer, firmware, chunk_size);
 645/*
 646			lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
 647				chunk_size, (chunk_size + 31) / 32 * 32);
 648*/
 649			ret = sdio_writesb(card->func, card->ioport,
 650				chunk_buffer, roundup(chunk_size, 32));
 651			if (ret)
 652				goto release;
 653
 654			firmware += chunk_size;
 655			size -= chunk_size;
 656			req_size -= chunk_size;
 657		}
 658	}
 659
 660	ret = 0;
 661
 662	lbs_deb_sdio("waiting for firmware to boot...\n");
 663
 664	/* wait for the firmware to boot */
 665	timeout = jiffies + HZ;
 666	while (1) {
 667		u16 scratch;
 668
 669		scratch = if_sdio_read_scratch(card, &ret);
 670		if (ret)
 671			goto release;
 672
 673		if (scratch == IF_SDIO_FIRMWARE_OK)
 674			break;
 675
 676		if (time_after(jiffies, timeout)) {
 677			ret = -ETIMEDOUT;
 678			goto release;
 679		}
 680
 681		msleep(10);
 682	}
 683
 684	ret = 0;
 685
 686release:
 687	sdio_release_host(card->func);
 688	kfree(chunk_buffer);
 689
 690out:
 691	if (ret)
 692		pr_err("failed to load firmware\n");
 693
 694	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 695	return ret;
 696}
 697
 698static void if_sdio_do_prog_firmware(struct lbs_private *priv, int ret,
 699				     const struct firmware *helper,
 700				     const struct firmware *mainfw)
 701{
 702	struct if_sdio_card *card = priv->card;
 703
 704	if (ret) {
 705		pr_err("failed to find firmware (%d)\n", ret);
 706		return;
 707	}
 708
 709	ret = if_sdio_prog_helper(card, helper);
 710	if (ret)
 711		return;
 712
 713	lbs_deb_sdio("Helper firmware loaded\n");
 714
 715	ret = if_sdio_prog_real(card, mainfw);
 716	if (ret)
 717		return;
 718
 719	lbs_deb_sdio("Firmware loaded\n");
 720	if_sdio_finish_power_on(card);
 721}
 722
 723static int if_sdio_prog_firmware(struct if_sdio_card *card)
 724{
 725	int ret;
 726	u16 scratch;
 
 
 727
 728	lbs_deb_enter(LBS_DEB_SDIO);
 729
 730	/*
 731	 * Disable interrupts
 732	 */
 733	sdio_claim_host(card->func);
 734	sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret);
 735	sdio_release_host(card->func);
 736
 737	sdio_claim_host(card->func);
 738	scratch = if_sdio_read_scratch(card, &ret);
 739	sdio_release_host(card->func);
 740
 741	lbs_deb_sdio("firmware status = %#x\n", scratch);
 742	lbs_deb_sdio("scratch ret = %d\n", ret);
 743
 744	if (ret)
 745		goto out;
 746
 747
 748	/*
 749	 * The manual clearly describes that FEDC is the right code to use
 750	 * to detect firmware presence, but for SD8686 it is not that simple.
 751	 * Scratch is also used to store the RX packet length, so we lose
 752	 * the FEDC value early on. So we use a non-zero check in order
 753	 * to validate firmware presence.
 754	 * Additionally, the SD8686 in the Gumstix always has the high scratch
 755	 * bit set, even when the firmware is not loaded. So we have to
 756	 * exclude that from the test.
 757	 */
 758	if (scratch == IF_SDIO_FIRMWARE_OK) {
 759		lbs_deb_sdio("firmware already loaded\n");
 760		if_sdio_finish_power_on(card);
 761		return 0;
 762	} else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) {
 763		lbs_deb_sdio("firmware may be running\n");
 764		if_sdio_finish_power_on(card);
 765		return 0;
 766	}
 767
 768	ret = lbs_get_firmware_async(card->priv, &card->func->dev, card->model,
 769				     fw_table, if_sdio_do_prog_firmware);
 770
 771out:
 772	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
 773	return ret;
 774}
 775
 776/********************************************************************/
 777/* Power management                                                 */
 778/********************************************************************/
 779
 780/* Finish power on sequence (after firmware is loaded) */
 781static void if_sdio_finish_power_on(struct if_sdio_card *card)
 782{
 783	struct sdio_func *func = card->func;
 784	struct lbs_private *priv = card->priv;
 785	int ret;
 786
 787	sdio_claim_host(func);
 788	sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
 789
 790	/*
 791	 * Get rx_unit if the chip is SD8688 or newer.
 792	 * SD8385 & SD8686 do not have rx_unit.
 793	 */
 794	if ((card->model != MODEL_8385)
 795			&& (card->model != MODEL_8686))
 796		card->rx_unit = if_sdio_read_rx_unit(card);
 797	else
 798		card->rx_unit = 0;
 799
 800	/*
 801	 * Set up the interrupt handler late.
 802	 *
 803	 * If we set it up earlier, the (buggy) hardware generates a spurious
 804	 * interrupt, even before the interrupt has been enabled, with
 805	 * CCCR_INTx = 0.
 806	 *
 807	 * We register the interrupt handler late so that we can handle any
 808	 * spurious interrupts, and also to avoid generation of that known
 809	 * spurious interrupt in the first place.
 810	 */
 811	ret = sdio_claim_irq(func, if_sdio_interrupt);
 812	if (ret)
 813		goto release;
 814
 815	/*
 816	 * Enable interrupts now that everything is set up
 817	 */
 818	sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
 819	if (ret)
 820		goto release_irq;
 821
 822	sdio_release_host(func);
 823
 824	/* Set fw_ready before queuing any commands so that
 825	 * lbs_thread won't block from sending them to firmware.
 826	 */
 827	priv->fw_ready = 1;
 828
 829	/*
 830	 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
 831	 */
 832	if (card->model == MODEL_8688) {
 833		struct cmd_header cmd;
 834
 835		memset(&cmd, 0, sizeof(cmd));
 836
 837		lbs_deb_sdio("send function INIT command\n");
 838		if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
 839				lbs_cmd_copyback, (unsigned long) &cmd))
 840			netdev_alert(priv->dev, "CMD_FUNC_INIT cmd failed\n");
 841	}
 842
 843	wake_up(&card->pwron_waitq);
 844
 845	if (!card->started) {
 846		ret = lbs_start_card(priv);
 847		if_sdio_power_off(card);
 848		if (ret == 0) {
 849			card->started = true;
 850			/* Tell PM core that we don't need the card to be
 851			 * powered now */
 852			pm_runtime_put(&func->dev);
 853		}
 854	}
 855
 856	return;
 857
 858release_irq:
 859	sdio_release_irq(func);
 860release:
 861	sdio_release_host(func);
 862}
 863
 864static int if_sdio_power_on(struct if_sdio_card *card)
 865{
 866	struct sdio_func *func = card->func;
 867	struct mmc_host *host = func->card->host;
 868	int ret;
 869
 870	sdio_claim_host(func);
 871
 872	ret = sdio_enable_func(func);
 873	if (ret)
 874		goto release;
 875
 876	/* For 1-bit transfers to the 8686 model, we need to enable the
 877	 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
 878	 * bit to allow access to non-vendor registers. */
 879	if ((card->model == MODEL_8686) &&
 880	    (host->caps & MMC_CAP_SDIO_IRQ) &&
 881	    (host->ios.bus_width == MMC_BUS_WIDTH_1)) {
 882		u8 reg;
 883
 884		func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
 885		reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret);
 886		if (ret)
 887			goto disable;
 888
 889		reg |= SDIO_BUS_ECSI;
 890		sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret);
 891		if (ret)
 892			goto disable;
 893	}
 894
 895	card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
 896	if (ret)
 897		goto disable;
 898
 899	card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
 900	if (ret)
 901		goto disable;
 902
 903	card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
 904	if (ret)
 905		goto disable;
 
 
 906
 907	sdio_release_host(func);
 908	ret = if_sdio_prog_firmware(card);
 909	if (ret) {
 910		sdio_claim_host(func);
 911		goto disable;
 912	}
 913
 914	return 0;
 915
 916disable:
 917	sdio_disable_func(func);
 918release:
 919	sdio_release_host(func);
 920	return ret;
 921}
 922
 923static int if_sdio_power_off(struct if_sdio_card *card)
 924{
 925	struct sdio_func *func = card->func;
 926	struct lbs_private *priv = card->priv;
 927
 928	priv->fw_ready = 0;
 929
 930	sdio_claim_host(func);
 931	sdio_release_irq(func);
 932	sdio_disable_func(func);
 933	sdio_release_host(func);
 934	return 0;
 935}
 936
 937
 938/*******************************************************************/
 939/* Libertas callbacks                                              */
 940/*******************************************************************/
 941
 942static int if_sdio_host_to_card(struct lbs_private *priv,
 943		u8 type, u8 *buf, u16 nb)
 944{
 945	int ret;
 946	struct if_sdio_card *card;
 947	struct if_sdio_packet *packet, *cur;
 948	u16 size;
 949	unsigned long flags;
 950
 951	lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
 952
 953	card = priv->card;
 954
 955	if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
 956		ret = -EINVAL;
 957		goto out;
 958	}
 959
 960	/*
 961	 * The transfer must be in one transaction or the firmware
 962	 * goes suicidal. There's no way to guarantee that for all
 963	 * controllers, but we can at least try.
 964	 */
 965	size = sdio_align_size(card->func, nb + 4);
 966
 967	packet = kzalloc(sizeof(struct if_sdio_packet) + size,
 968			GFP_ATOMIC);
 969	if (!packet) {
 970		ret = -ENOMEM;
 971		goto out;
 972	}
 973
 974	packet->next = NULL;
 975	packet->nb = size;
 976
 977	/*
 978	 * SDIO specific header.
 979	 */
 980	packet->buffer[0] = (nb + 4) & 0xff;
 981	packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
 982	packet->buffer[2] = type;
 983	packet->buffer[3] = 0;
 984
 985	memcpy(packet->buffer + 4, buf, nb);
 986
 987	spin_lock_irqsave(&card->lock, flags);
 988
 989	if (!card->packets)
 990		card->packets = packet;
 991	else {
 992		cur = card->packets;
 993		while (cur->next)
 994			cur = cur->next;
 995		cur->next = packet;
 996	}
 997
 998	switch (type) {
 999	case MVMS_CMD:
1000		priv->dnld_sent = DNLD_CMD_SENT;
1001		break;
1002	case MVMS_DAT:
1003		priv->dnld_sent = DNLD_DATA_SENT;
1004		break;
1005	default:
1006		lbs_deb_sdio("unknown packet type %d\n", (int)type);
1007	}
1008
1009	spin_unlock_irqrestore(&card->lock, flags);
1010
1011	queue_work(card->workqueue, &card->packet_worker);
1012
1013	ret = 0;
1014
1015out:
1016	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1017
1018	return ret;
1019}
1020
1021static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
1022{
1023	int ret = -1;
1024	struct cmd_header cmd;
1025
1026	memset(&cmd, 0, sizeof(cmd));
1027
1028	lbs_deb_sdio("send DEEP_SLEEP command\n");
1029	ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
1030			lbs_cmd_copyback, (unsigned long) &cmd);
1031	if (ret)
1032		netdev_err(priv->dev, "DEEP_SLEEP cmd failed\n");
1033
1034	mdelay(200);
1035	return ret;
1036}
1037
1038static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
1039{
1040	struct if_sdio_card *card = priv->card;
1041	int ret = -1;
1042
1043	lbs_deb_enter(LBS_DEB_SDIO);
1044	sdio_claim_host(card->func);
1045
1046	sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
1047	if (ret)
1048		netdev_err(priv->dev, "sdio_writeb failed!\n");
1049
1050	sdio_release_host(card->func);
1051	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1052	return ret;
1053}
1054
1055static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
1056{
1057	struct if_sdio_card *card = priv->card;
1058	int ret = -1;
1059
1060	lbs_deb_enter(LBS_DEB_SDIO);
1061	sdio_claim_host(card->func);
1062
1063	sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
1064	if (ret)
1065		netdev_err(priv->dev, "sdio_writeb failed!\n");
1066
1067	sdio_release_host(card->func);
1068	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1069	return ret;
1070
1071}
1072
1073static struct mmc_host *reset_host;
1074
1075static void if_sdio_reset_card_worker(struct work_struct *work)
1076{
1077	/*
1078	 * The actual reset operation must be run outside of lbs_thread. This
1079	 * is because mmc_remove_host() will cause the device to be instantly
1080	 * destroyed, and the libertas driver then needs to end lbs_thread,
1081	 * leading to a deadlock.
1082	 *
1083	 * We run it in a workqueue totally independent from the if_sdio_card
1084	 * instance for that reason.
1085	 */
1086
1087	pr_info("Resetting card...");
1088	mmc_remove_host(reset_host);
1089	mmc_add_host(reset_host);
1090}
1091static DECLARE_WORK(card_reset_work, if_sdio_reset_card_worker);
1092
1093static void if_sdio_reset_card(struct lbs_private *priv)
1094{
1095	struct if_sdio_card *card = priv->card;
1096
1097	if (work_pending(&card_reset_work))
1098		return;
1099
1100	reset_host = card->func->card->host;
1101	schedule_work(&card_reset_work);
1102}
1103
1104static int if_sdio_power_save(struct lbs_private *priv)
1105{
1106	struct if_sdio_card *card = priv->card;
1107	int ret;
1108
1109	flush_workqueue(card->workqueue);
1110
1111	ret = if_sdio_power_off(card);
1112
1113	/* Let runtime PM know the card is powered off */
1114	pm_runtime_put_sync(&card->func->dev);
1115
1116	return ret;
1117}
1118
1119static int if_sdio_power_restore(struct lbs_private *priv)
1120{
1121	struct if_sdio_card *card = priv->card;
1122	int r;
1123
1124	/* Make sure the card will not be powered off by runtime PM */
1125	pm_runtime_get_sync(&card->func->dev);
1126
1127	r = if_sdio_power_on(card);
1128	if (r)
1129		return r;
1130
1131	wait_event(card->pwron_waitq, priv->fw_ready);
1132	return 0;
1133}
1134
1135
1136/*******************************************************************/
1137/* SDIO callbacks                                                  */
1138/*******************************************************************/
1139
1140static void if_sdio_interrupt(struct sdio_func *func)
1141{
1142	int ret;
1143	struct if_sdio_card *card;
1144	u8 cause;
1145
1146	lbs_deb_enter(LBS_DEB_SDIO);
1147
1148	card = sdio_get_drvdata(func);
1149
1150	cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
1151	if (ret || !cause)
1152		goto out;
1153
1154	lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
1155
1156	sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
1157	if (ret)
1158		goto out;
1159
1160	/*
1161	 * Ignore the define name, this really means the card has
1162	 * successfully received the command.
1163	 */
1164	card->priv->is_activity_detected = 1;
1165	if (cause & IF_SDIO_H_INT_DNLD)
1166		lbs_host_to_card_done(card->priv);
1167
1168
1169	if (cause & IF_SDIO_H_INT_UPLD) {
1170		ret = if_sdio_card_to_host(card);
1171		if (ret)
1172			goto out;
1173	}
1174
1175	ret = 0;
1176
1177out:
1178	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1179}
1180
1181static int if_sdio_probe(struct sdio_func *func,
1182		const struct sdio_device_id *id)
1183{
1184	struct if_sdio_card *card;
1185	struct lbs_private *priv;
1186	int ret, i;
1187	unsigned int model;
1188	struct if_sdio_packet *packet;
 
1189
1190	lbs_deb_enter(LBS_DEB_SDIO);
1191
1192	for (i = 0;i < func->card->num_info;i++) {
1193		if (sscanf(func->card->info[i],
1194				"802.11 SDIO ID: %x", &model) == 1)
1195			break;
1196		if (sscanf(func->card->info[i],
1197				"ID: %x", &model) == 1)
1198			break;
1199		if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
1200			model = MODEL_8385;
1201			break;
1202		}
1203	}
1204
1205	if (i == func->card->num_info) {
1206		pr_err("unable to identify card model\n");
1207		return -ENODEV;
1208	}
1209
1210	card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
1211	if (!card)
1212		return -ENOMEM;
1213
1214	card->func = func;
1215	card->model = model;
1216
1217	switch (card->model) {
1218	case MODEL_8385:
1219		card->scratch_reg = IF_SDIO_SCRATCH_OLD;
1220		break;
1221	case MODEL_8686:
1222		card->scratch_reg = IF_SDIO_SCRATCH;
1223		break;
1224	case MODEL_8688:
1225	default: /* for newer chipsets */
1226		card->scratch_reg = IF_SDIO_FW_STATUS;
1227		break;
1228	}
1229
1230	spin_lock_init(&card->lock);
1231	card->workqueue = create_workqueue("libertas_sdio");
1232	INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
1233	init_waitqueue_head(&card->pwron_waitq);
1234
1235	/* Check if we support this card */
1236	for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
1237		if (card->model == fw_table[i].model)
1238			break;
1239	}
1240	if (i == ARRAY_SIZE(fw_table)) {
1241		pr_err("unknown card model 0x%x\n", card->model);
1242		ret = -ENODEV;
1243		goto free;
1244	}
1245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1246	sdio_set_drvdata(func, card);
1247
1248	lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1249			"device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1250			func->class, func->vendor, func->device,
1251			model, (unsigned)card->ioport);
1252
 
 
 
1253
1254	priv = lbs_add_card(card, &func->dev);
1255	if (!priv) {
1256		ret = -ENOMEM;
1257		goto free;
1258	}
1259
1260	card->priv = priv;
1261
1262	priv->card = card;
1263	priv->hw_host_to_card = if_sdio_host_to_card;
1264	priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
1265	priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
1266	priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
1267	priv->reset_card = if_sdio_reset_card;
1268	priv->power_save = if_sdio_power_save;
1269	priv->power_restore = if_sdio_power_restore;
1270
1271	ret = if_sdio_power_on(card);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1272	if (ret)
1273		goto err_activate_card;
1274
1275out:
1276	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1277
1278	return ret;
1279
1280err_activate_card:
1281	flush_workqueue(card->workqueue);
1282	lbs_remove_card(priv);
 
 
 
 
 
 
 
 
1283free:
1284	destroy_workqueue(card->workqueue);
1285	while (card->packets) {
1286		packet = card->packets;
1287		card->packets = card->packets->next;
1288		kfree(packet);
1289	}
1290
 
 
 
 
1291	kfree(card);
1292
1293	goto out;
1294}
1295
1296static void if_sdio_remove(struct sdio_func *func)
1297{
1298	struct if_sdio_card *card;
1299	struct if_sdio_packet *packet;
1300
1301	lbs_deb_enter(LBS_DEB_SDIO);
1302
1303	card = sdio_get_drvdata(func);
1304
1305	/* Undo decrement done above in if_sdio_probe */
1306	pm_runtime_get_noresume(&func->dev);
1307
1308	if (user_rmmod && (card->model == MODEL_8688)) {
1309		/*
1310		 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1311		 * multiple functions
1312		 */
1313		struct cmd_header cmd;
1314
1315		memset(&cmd, 0, sizeof(cmd));
1316
1317		lbs_deb_sdio("send function SHUTDOWN command\n");
1318		if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
1319				&cmd, sizeof(cmd), lbs_cmd_copyback,
1320				(unsigned long) &cmd))
1321			pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1322	}
1323
1324
1325	lbs_deb_sdio("call remove card\n");
1326	lbs_stop_card(card->priv);
1327	lbs_remove_card(card->priv);
1328
1329	flush_workqueue(card->workqueue);
1330	destroy_workqueue(card->workqueue);
1331
 
 
 
 
 
1332	while (card->packets) {
1333		packet = card->packets;
1334		card->packets = card->packets->next;
1335		kfree(packet);
1336	}
1337
 
 
 
 
1338	kfree(card);
 
1339	lbs_deb_leave(LBS_DEB_SDIO);
1340}
1341
1342static int if_sdio_suspend(struct device *dev)
1343{
1344	struct sdio_func *func = dev_to_sdio_func(dev);
1345	int ret;
1346	struct if_sdio_card *card = sdio_get_drvdata(func);
1347
1348	mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
1349
1350	/* If we're powered off anyway, just let the mmc layer remove the
1351	 * card. */
1352	if (!lbs_iface_active(card->priv))
1353		return -ENOSYS;
1354
1355	dev_info(dev, "%s: suspend: PM flags = 0x%x\n",
1356		 sdio_func_id(func), flags);
1357
1358	/* If we aren't being asked to wake on anything, we should bail out
1359	 * and let the SD stack power down the card.
1360	 */
1361	if (card->priv->wol_criteria == EHS_REMOVE_WAKEUP) {
1362		dev_info(dev, "Suspend without wake params -- powering down card\n");
1363		return -ENOSYS;
1364	}
1365
1366	if (!(flags & MMC_PM_KEEP_POWER)) {
1367		dev_err(dev, "%s: cannot remain alive while host is suspended\n",
1368			sdio_func_id(func));
1369		return -ENOSYS;
1370	}
1371
1372	ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1373	if (ret)
1374		return ret;
1375
1376	ret = lbs_suspend(card->priv);
1377	if (ret)
1378		return ret;
1379
1380	return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
1381}
1382
1383static int if_sdio_resume(struct device *dev)
1384{
1385	struct sdio_func *func = dev_to_sdio_func(dev);
1386	struct if_sdio_card *card = sdio_get_drvdata(func);
1387	int ret;
1388
1389	dev_info(dev, "%s: resume: we're back\n", sdio_func_id(func));
1390
1391	ret = lbs_resume(card->priv);
1392
1393	return ret;
1394}
1395
1396static const struct dev_pm_ops if_sdio_pm_ops = {
1397	.suspend	= if_sdio_suspend,
1398	.resume		= if_sdio_resume,
1399};
1400
1401static struct sdio_driver if_sdio_driver = {
1402	.name		= "libertas_sdio",
1403	.id_table	= if_sdio_ids,
1404	.probe		= if_sdio_probe,
1405	.remove		= if_sdio_remove,
1406	.drv = {
1407		.pm = &if_sdio_pm_ops,
1408	},
1409};
1410
1411/*******************************************************************/
1412/* Module functions                                                */
1413/*******************************************************************/
1414
1415static int __init if_sdio_init_module(void)
1416{
1417	int ret = 0;
1418
1419	lbs_deb_enter(LBS_DEB_SDIO);
1420
1421	printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1422	printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1423
1424	ret = sdio_register_driver(&if_sdio_driver);
1425
1426	/* Clear the flag in case user removes the card. */
1427	user_rmmod = 0;
1428
1429	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1430
1431	return ret;
1432}
1433
1434static void __exit if_sdio_exit_module(void)
1435{
1436	lbs_deb_enter(LBS_DEB_SDIO);
1437
1438	/* Set the flag as user is removing this module. */
1439	user_rmmod = 1;
1440
1441	cancel_work_sync(&card_reset_work);
1442
1443	sdio_unregister_driver(&if_sdio_driver);
1444
1445	lbs_deb_leave(LBS_DEB_SDIO);
1446}
1447
1448module_init(if_sdio_init_module);
1449module_exit(if_sdio_exit_module);
1450
1451MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1452MODULE_AUTHOR("Pierre Ossman");
1453MODULE_LICENSE("GPL");