proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / net / wireless / libertas / if_sdio.c
blobb4de0ca10feb8f9ef18b826e00f82a240fb40e91
1 /*
2 * linux/drivers/net/wireless/libertas/if_sdio.c
4 * Copyright 2007-2008 Pierre Ossman
6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
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.
13 * This hardware has more or less no CMD53 support, so all registers
14 * must be accessed using sdio_readb()/sdio_writeb().
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.
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.
29 #include <linux/kernel.h>
30 #include <linux/moduleparam.h>
31 #include <linux/slab.h>
32 #include <linux/firmware.h>
33 #include <linux/netdevice.h>
34 #include <linux/delay.h>
35 #include <linux/mmc/card.h>
36 #include <linux/mmc/sdio_func.h>
37 #include <linux/mmc/sdio_ids.h>
38 #include <linux/mmc/sdio.h>
39 #include <linux/mmc/host.h>
41 #include "host.h"
42 #include "decl.h"
43 #include "defs.h"
44 #include "dev.h"
45 #include "cmd.h"
46 #include "if_sdio.h"
48 /* The if_sdio_remove() callback function is called when
49 * user removes this module from kernel space or ejects
50 * the card from the slot. The driver handles these 2 cases
51 * differently for SD8688 combo chip.
52 * If the user is removing the module, the FUNC_SHUTDOWN
53 * command for SD8688 is sent to the firmware.
54 * If the card is removed, there is no need to send this command.
56 * The variable 'user_rmmod' is used to distinguish these two
57 * scenarios. This flag is initialized as FALSE in case the card
58 * is removed, and will be set to TRUE for module removal when
59 * module_exit function is called.
61 static u8 user_rmmod;
63 static char *lbs_helper_name = NULL;
64 module_param_named(helper_name, lbs_helper_name, charp, 0644);
66 static char *lbs_fw_name = NULL;
67 module_param_named(fw_name, lbs_fw_name, charp, 0644);
69 static const struct sdio_device_id if_sdio_ids[] = {
70 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
71 SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
72 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
73 SDIO_DEVICE_ID_MARVELL_8688WLAN) },
74 { /* end: all zeroes */ },
77 MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
79 #define MODEL_8385 0x04
80 #define MODEL_8686 0x0b
81 #define MODEL_8688 0x10
83 static const struct lbs_fw_table fw_table[] = {
84 { MODEL_8385, "libertas/sd8385_helper.bin", "libertas/sd8385.bin" },
85 { MODEL_8385, "sd8385_helper.bin", "sd8385.bin" },
86 { MODEL_8686, "libertas/sd8686_v9_helper.bin", "libertas/sd8686_v9.bin" },
87 { MODEL_8686, "libertas/sd8686_v8_helper.bin", "libertas/sd8686_v8.bin" },
88 { MODEL_8686, "sd8686_helper.bin", "sd8686.bin" },
89 { MODEL_8688, "libertas/sd8688_helper.bin", "libertas/sd8688.bin" },
90 { MODEL_8688, "sd8688_helper.bin", "sd8688.bin" },
91 { 0, NULL, NULL }
93 MODULE_FIRMWARE("libertas/sd8385_helper.bin");
94 MODULE_FIRMWARE("libertas/sd8385.bin");
95 MODULE_FIRMWARE("sd8385_helper.bin");
96 MODULE_FIRMWARE("sd8385.bin");
97 MODULE_FIRMWARE("libertas/sd8686_v9_helper.bin");
98 MODULE_FIRMWARE("libertas/sd8686_v9.bin");
99 MODULE_FIRMWARE("libertas/sd8686_v8_helper.bin");
100 MODULE_FIRMWARE("libertas/sd8686_v8.bin");
101 MODULE_FIRMWARE("sd8686_helper.bin");
102 MODULE_FIRMWARE("sd8686.bin");
103 MODULE_FIRMWARE("libertas/sd8688_helper.bin");
104 MODULE_FIRMWARE("libertas/sd8688.bin");
105 MODULE_FIRMWARE("sd8688_helper.bin");
106 MODULE_FIRMWARE("sd8688.bin");
108 struct if_sdio_packet {
109 struct if_sdio_packet *next;
110 u16 nb;
111 u8 buffer[0] __attribute__((aligned(4)));
114 struct if_sdio_card {
115 struct sdio_func *func;
116 struct lbs_private *priv;
118 int model;
119 unsigned long ioport;
120 unsigned int scratch_reg;
122 const char *helper;
123 const char *firmware;
124 bool helper_allocated;
125 bool firmware_allocated;
127 u8 buffer[65536] __attribute__((aligned(4)));
129 spinlock_t lock;
130 struct if_sdio_packet *packets;
132 struct workqueue_struct *workqueue;
133 struct work_struct packet_worker;
135 u8 rx_unit;
138 /********************************************************************/
139 /* I/O */
140 /********************************************************************/
143 * For SD8385/SD8686, this function reads firmware status after
144 * the image is downloaded, or reads RX packet length when
145 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
146 * For SD8688, this function reads firmware status only.
148 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
150 int ret;
151 u16 scratch;
153 scratch = sdio_readb(card->func, card->scratch_reg, &ret);
154 if (!ret)
155 scratch |= sdio_readb(card->func, card->scratch_reg + 1,
156 &ret) << 8;
158 if (err)
159 *err = ret;
161 if (ret)
162 return 0xffff;
164 return scratch;
167 static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
169 int ret;
170 u8 rx_unit;
172 rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);
174 if (ret)
175 rx_unit = 0;
177 return rx_unit;
180 static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
182 int ret;
183 u16 rx_len;
185 switch (card->model) {
186 case MODEL_8385:
187 case MODEL_8686:
188 rx_len = if_sdio_read_scratch(card, &ret);
189 break;
190 case MODEL_8688:
191 default: /* for newer chipsets */
192 rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
193 if (!ret)
194 rx_len <<= card->rx_unit;
195 else
196 rx_len = 0xffff; /* invalid length */
198 break;
201 if (err)
202 *err = ret;
204 return rx_len;
207 static int if_sdio_handle_cmd(struct if_sdio_card *card,
208 u8 *buffer, unsigned size)
210 struct lbs_private *priv = card->priv;
211 int ret;
212 unsigned long flags;
213 u8 i;
215 lbs_deb_enter(LBS_DEB_SDIO);
217 if (size > LBS_CMD_BUFFER_SIZE) {
218 lbs_deb_sdio("response packet too large (%d bytes)\n",
219 (int)size);
220 ret = -E2BIG;
221 goto out;
224 spin_lock_irqsave(&priv->driver_lock, flags);
226 i = (priv->resp_idx == 0) ? 1 : 0;
227 BUG_ON(priv->resp_len[i]);
228 priv->resp_len[i] = size;
229 memcpy(priv->resp_buf[i], buffer, size);
230 lbs_notify_command_response(priv, i);
232 spin_unlock_irqrestore(&card->priv->driver_lock, flags);
234 ret = 0;
236 out:
237 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
238 return ret;
241 static int if_sdio_handle_data(struct if_sdio_card *card,
242 u8 *buffer, unsigned size)
244 int ret;
245 struct sk_buff *skb;
246 char *data;
248 lbs_deb_enter(LBS_DEB_SDIO);
250 if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
251 lbs_deb_sdio("response packet too large (%d bytes)\n",
252 (int)size);
253 ret = -E2BIG;
254 goto out;
257 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
258 if (!skb) {
259 ret = -ENOMEM;
260 goto out;
263 skb_reserve(skb, NET_IP_ALIGN);
265 data = skb_put(skb, size);
267 memcpy(data, buffer, size);
269 lbs_process_rxed_packet(card->priv, skb);
271 ret = 0;
273 out:
274 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
276 return ret;
279 static int if_sdio_handle_event(struct if_sdio_card *card,
280 u8 *buffer, unsigned size)
282 int ret;
283 u32 event;
285 lbs_deb_enter(LBS_DEB_SDIO);
287 if (card->model == MODEL_8385) {
288 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
289 if (ret)
290 goto out;
292 /* right shift 3 bits to get the event id */
293 event >>= 3;
294 } else {
295 if (size < 4) {
296 lbs_deb_sdio("event packet too small (%d bytes)\n",
297 (int)size);
298 ret = -EINVAL;
299 goto out;
301 event = buffer[3] << 24;
302 event |= buffer[2] << 16;
303 event |= buffer[1] << 8;
304 event |= buffer[0] << 0;
307 lbs_queue_event(card->priv, event & 0xFF);
308 ret = 0;
310 out:
311 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
313 return ret;
316 static int if_sdio_wait_status(struct if_sdio_card *card, const u8 condition)
318 u8 status;
319 unsigned long timeout;
320 int ret = 0;
322 timeout = jiffies + HZ;
323 while (1) {
324 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
325 if (ret)
326 return ret;
327 if ((status & condition) == condition)
328 break;
329 if (time_after(jiffies, timeout))
330 return -ETIMEDOUT;
331 mdelay(1);
333 return ret;
336 static int if_sdio_card_to_host(struct if_sdio_card *card)
338 int ret;
339 u16 size, type, chunk;
341 lbs_deb_enter(LBS_DEB_SDIO);
343 size = if_sdio_read_rx_len(card, &ret);
344 if (ret)
345 goto out;
347 if (size < 4) {
348 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
349 (int)size);
350 ret = -EINVAL;
351 goto out;
354 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
355 if (ret)
356 goto out;
359 * The transfer must be in one transaction or the firmware
360 * goes suicidal. There's no way to guarantee that for all
361 * controllers, but we can at least try.
363 chunk = sdio_align_size(card->func, size);
365 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
366 if (ret)
367 goto out;
369 chunk = card->buffer[0] | (card->buffer[1] << 8);
370 type = card->buffer[2] | (card->buffer[3] << 8);
372 lbs_deb_sdio("packet of type %d and size %d bytes\n",
373 (int)type, (int)chunk);
375 if (chunk > size) {
376 lbs_deb_sdio("packet fragment (%d > %d)\n",
377 (int)chunk, (int)size);
378 ret = -EINVAL;
379 goto out;
382 if (chunk < size) {
383 lbs_deb_sdio("packet fragment (%d < %d)\n",
384 (int)chunk, (int)size);
387 switch (type) {
388 case MVMS_CMD:
389 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
390 if (ret)
391 goto out;
392 break;
393 case MVMS_DAT:
394 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
395 if (ret)
396 goto out;
397 break;
398 case MVMS_EVENT:
399 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
400 if (ret)
401 goto out;
402 break;
403 default:
404 lbs_deb_sdio("invalid type (%d) from firmware\n",
405 (int)type);
406 ret = -EINVAL;
407 goto out;
410 out:
411 if (ret)
412 lbs_pr_err("problem fetching packet from firmware\n");
414 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
416 return ret;
419 static void if_sdio_host_to_card_worker(struct work_struct *work)
421 struct if_sdio_card *card;
422 struct if_sdio_packet *packet;
423 int ret;
424 unsigned long flags;
426 lbs_deb_enter(LBS_DEB_SDIO);
428 card = container_of(work, struct if_sdio_card, packet_worker);
430 while (1) {
431 spin_lock_irqsave(&card->lock, flags);
432 packet = card->packets;
433 if (packet)
434 card->packets = packet->next;
435 spin_unlock_irqrestore(&card->lock, flags);
437 if (!packet)
438 break;
440 sdio_claim_host(card->func);
442 ret = if_sdio_wait_status(card, IF_SDIO_IO_RDY);
443 if (ret == 0) {
444 ret = sdio_writesb(card->func, card->ioport,
445 packet->buffer, packet->nb);
448 if (ret)
449 lbs_pr_err("error %d sending packet to firmware\n", ret);
451 sdio_release_host(card->func);
453 kfree(packet);
456 lbs_deb_leave(LBS_DEB_SDIO);
459 /********************************************************************/
460 /* Firmware */
461 /********************************************************************/
463 #define FW_DL_READY_STATUS (IF_SDIO_IO_RDY | IF_SDIO_DL_RDY)
465 static int if_sdio_prog_helper(struct if_sdio_card *card,
466 const struct firmware *fw)
468 int ret;
469 unsigned long timeout;
470 u8 *chunk_buffer;
471 u32 chunk_size;
472 const u8 *firmware;
473 size_t size;
475 lbs_deb_enter(LBS_DEB_SDIO);
477 chunk_buffer = kzalloc(64, GFP_KERNEL);
478 if (!chunk_buffer) {
479 ret = -ENOMEM;
480 goto out;
483 sdio_claim_host(card->func);
485 ret = sdio_set_block_size(card->func, 32);
486 if (ret)
487 goto release;
489 firmware = fw->data;
490 size = fw->size;
492 while (size) {
493 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
494 if (ret)
495 goto release;
497 /* On some platforms (like Davinci) the chip needs more time
498 * between helper blocks.
500 mdelay(2);
502 chunk_size = min(size, (size_t)60);
504 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
505 memcpy(chunk_buffer + 4, firmware, chunk_size);
507 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
509 ret = sdio_writesb(card->func, card->ioport,
510 chunk_buffer, 64);
511 if (ret)
512 goto release;
514 firmware += chunk_size;
515 size -= chunk_size;
518 /* an empty block marks the end of the transfer */
519 memset(chunk_buffer, 0, 4);
520 ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
521 if (ret)
522 goto release;
524 lbs_deb_sdio("waiting for helper to boot...\n");
526 /* wait for the helper to boot by looking at the size register */
527 timeout = jiffies + HZ;
528 while (1) {
529 u16 req_size;
531 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
532 if (ret)
533 goto release;
535 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
536 if (ret)
537 goto release;
539 if (req_size != 0)
540 break;
542 if (time_after(jiffies, timeout)) {
543 ret = -ETIMEDOUT;
544 goto release;
547 msleep(10);
550 ret = 0;
552 release:
553 sdio_release_host(card->func);
554 kfree(chunk_buffer);
556 out:
557 if (ret)
558 lbs_pr_err("failed to load helper firmware\n");
560 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
561 return ret;
564 static int if_sdio_prog_real(struct if_sdio_card *card,
565 const struct firmware *fw)
567 int ret;
568 unsigned long timeout;
569 u8 *chunk_buffer;
570 u32 chunk_size;
571 const u8 *firmware;
572 size_t size, req_size;
574 lbs_deb_enter(LBS_DEB_SDIO);
576 chunk_buffer = kzalloc(512, GFP_KERNEL);
577 if (!chunk_buffer) {
578 ret = -ENOMEM;
579 goto out;
582 sdio_claim_host(card->func);
584 ret = sdio_set_block_size(card->func, 32);
585 if (ret)
586 goto release;
588 firmware = fw->data;
589 size = fw->size;
591 while (size) {
592 ret = if_sdio_wait_status(card, FW_DL_READY_STATUS);
593 if (ret)
594 goto release;
596 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
597 if (ret)
598 goto release;
600 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
601 if (ret)
602 goto release;
604 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
606 if (req_size == 0) {
607 lbs_deb_sdio("firmware helper gave up early\n");
608 ret = -EIO;
609 goto release;
612 if (req_size & 0x01) {
613 lbs_deb_sdio("firmware helper signalled error\n");
614 ret = -EIO;
615 goto release;
618 if (req_size > size)
619 req_size = size;
621 while (req_size) {
622 chunk_size = min(req_size, (size_t)512);
624 memcpy(chunk_buffer, firmware, chunk_size);
626 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
627 chunk_size, (chunk_size + 31) / 32 * 32);
629 ret = sdio_writesb(card->func, card->ioport,
630 chunk_buffer, roundup(chunk_size, 32));
631 if (ret)
632 goto release;
634 firmware += chunk_size;
635 size -= chunk_size;
636 req_size -= chunk_size;
640 ret = 0;
642 lbs_deb_sdio("waiting for firmware to boot...\n");
644 /* wait for the firmware to boot */
645 timeout = jiffies + HZ;
646 while (1) {
647 u16 scratch;
649 scratch = if_sdio_read_scratch(card, &ret);
650 if (ret)
651 goto release;
653 if (scratch == IF_SDIO_FIRMWARE_OK)
654 break;
656 if (time_after(jiffies, timeout)) {
657 ret = -ETIMEDOUT;
658 goto release;
661 msleep(10);
664 ret = 0;
666 release:
667 sdio_release_host(card->func);
668 kfree(chunk_buffer);
670 out:
671 if (ret)
672 lbs_pr_err("failed to load firmware\n");
674 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
675 return ret;
678 static int if_sdio_prog_firmware(struct if_sdio_card *card)
680 int ret;
681 u16 scratch;
682 const struct firmware *helper = NULL;
683 const struct firmware *mainfw = NULL;
685 lbs_deb_enter(LBS_DEB_SDIO);
688 * Disable interrupts
690 sdio_claim_host(card->func);
691 sdio_writeb(card->func, 0x00, IF_SDIO_H_INT_MASK, &ret);
692 sdio_release_host(card->func);
694 sdio_claim_host(card->func);
695 scratch = if_sdio_read_scratch(card, &ret);
696 sdio_release_host(card->func);
698 lbs_deb_sdio("firmware status = %#x\n", scratch);
699 lbs_deb_sdio("scratch ret = %d\n", ret);
701 if (ret)
702 goto out;
706 * The manual clearly describes that FEDC is the right code to use
707 * to detect firmware presence, but for SD8686 it is not that simple.
708 * Scratch is also used to store the RX packet length, so we lose
709 * the FEDC value early on. So we use a non-zero check in order
710 * to validate firmware presence.
711 * Additionally, the SD8686 in the Gumstix always has the high scratch
712 * bit set, even when the firmware is not loaded. So we have to
713 * exclude that from the test.
715 if (scratch == IF_SDIO_FIRMWARE_OK) {
716 lbs_deb_sdio("firmware already loaded\n");
717 goto success;
718 } else if ((card->model == MODEL_8686) && (scratch & 0x7fff)) {
719 lbs_deb_sdio("firmware may be running\n");
720 goto success;
723 ret = lbs_get_firmware(&card->func->dev, lbs_helper_name, lbs_fw_name,
724 card->model, &fw_table[0], &helper, &mainfw);
725 if (ret) {
726 lbs_pr_err("failed to find firmware (%d)\n", ret);
727 goto out;
730 ret = if_sdio_prog_helper(card, helper);
731 if (ret)
732 goto out;
734 lbs_deb_sdio("Helper firmware loaded\n");
736 ret = if_sdio_prog_real(card, mainfw);
737 if (ret)
738 goto out;
740 lbs_deb_sdio("Firmware loaded\n");
742 success:
743 sdio_claim_host(card->func);
744 sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
745 sdio_release_host(card->func);
746 ret = 0;
748 out:
749 if (helper)
750 release_firmware(helper);
751 if (mainfw)
752 release_firmware(mainfw);
754 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
755 return ret;
758 /*******************************************************************/
759 /* Libertas callbacks */
760 /*******************************************************************/
762 static int if_sdio_host_to_card(struct lbs_private *priv,
763 u8 type, u8 *buf, u16 nb)
765 int ret;
766 struct if_sdio_card *card;
767 struct if_sdio_packet *packet, *cur;
768 u16 size;
769 unsigned long flags;
771 lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
773 card = priv->card;
775 if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
776 ret = -EINVAL;
777 goto out;
781 * The transfer must be in one transaction or the firmware
782 * goes suicidal. There's no way to guarantee that for all
783 * controllers, but we can at least try.
785 size = sdio_align_size(card->func, nb + 4);
787 packet = kzalloc(sizeof(struct if_sdio_packet) + size,
788 GFP_ATOMIC);
789 if (!packet) {
790 ret = -ENOMEM;
791 goto out;
794 packet->next = NULL;
795 packet->nb = size;
798 * SDIO specific header.
800 packet->buffer[0] = (nb + 4) & 0xff;
801 packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
802 packet->buffer[2] = type;
803 packet->buffer[3] = 0;
805 memcpy(packet->buffer + 4, buf, nb);
807 spin_lock_irqsave(&card->lock, flags);
809 if (!card->packets)
810 card->packets = packet;
811 else {
812 cur = card->packets;
813 while (cur->next)
814 cur = cur->next;
815 cur->next = packet;
818 switch (type) {
819 case MVMS_CMD:
820 priv->dnld_sent = DNLD_CMD_SENT;
821 break;
822 case MVMS_DAT:
823 priv->dnld_sent = DNLD_DATA_SENT;
824 break;
825 default:
826 lbs_deb_sdio("unknown packet type %d\n", (int)type);
829 spin_unlock_irqrestore(&card->lock, flags);
831 queue_work(card->workqueue, &card->packet_worker);
833 ret = 0;
835 out:
836 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
838 return ret;
841 static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
843 int ret = -1;
844 struct cmd_header cmd;
846 memset(&cmd, 0, sizeof(cmd));
848 lbs_deb_sdio("send DEEP_SLEEP command\n");
849 ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
850 lbs_cmd_copyback, (unsigned long) &cmd);
851 if (ret)
852 lbs_pr_err("DEEP_SLEEP cmd failed\n");
854 mdelay(200);
855 return ret;
858 static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
860 struct if_sdio_card *card = priv->card;
861 int ret = -1;
863 lbs_deb_enter(LBS_DEB_SDIO);
864 sdio_claim_host(card->func);
866 sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
867 if (ret)
868 lbs_pr_err("sdio_writeb failed!\n");
870 sdio_release_host(card->func);
871 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
872 return ret;
875 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
877 struct if_sdio_card *card = priv->card;
878 int ret = -1;
880 lbs_deb_enter(LBS_DEB_SDIO);
881 sdio_claim_host(card->func);
883 sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
884 if (ret)
885 lbs_pr_err("sdio_writeb failed!\n");
887 sdio_release_host(card->func);
888 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
889 return ret;
893 /*******************************************************************/
894 /* SDIO callbacks */
895 /*******************************************************************/
897 static void if_sdio_interrupt(struct sdio_func *func)
899 int ret;
900 struct if_sdio_card *card;
901 u8 cause;
903 lbs_deb_enter(LBS_DEB_SDIO);
905 card = sdio_get_drvdata(func);
907 cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
908 if (ret)
909 goto out;
911 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
913 sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
914 if (ret)
915 goto out;
918 * Ignore the define name, this really means the card has
919 * successfully received the command.
921 card->priv->is_activity_detected = 1;
922 if (cause & IF_SDIO_H_INT_DNLD)
923 lbs_host_to_card_done(card->priv);
926 if (cause & IF_SDIO_H_INT_UPLD) {
927 ret = if_sdio_card_to_host(card);
928 if (ret)
929 goto out;
932 ret = 0;
934 out:
935 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
938 static int if_sdio_probe(struct sdio_func *func,
939 const struct sdio_device_id *id)
941 struct if_sdio_card *card;
942 struct lbs_private *priv;
943 int ret, i;
944 unsigned int model;
945 struct if_sdio_packet *packet;
946 struct mmc_host *host = func->card->host;
948 lbs_deb_enter(LBS_DEB_SDIO);
950 for (i = 0;i < func->card->num_info;i++) {
951 if (sscanf(func->card->info[i],
952 "802.11 SDIO ID: %x", &model) == 1)
953 break;
954 if (sscanf(func->card->info[i],
955 "ID: %x", &model) == 1)
956 break;
957 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
958 model = MODEL_8385;
959 break;
963 if (i == func->card->num_info) {
964 lbs_pr_err("unable to identify card model\n");
965 return -ENODEV;
968 card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
969 if (!card)
970 return -ENOMEM;
972 card->func = func;
973 card->model = model;
975 switch (card->model) {
976 case MODEL_8385:
977 card->scratch_reg = IF_SDIO_SCRATCH_OLD;
978 break;
979 case MODEL_8686:
980 card->scratch_reg = IF_SDIO_SCRATCH;
981 break;
982 case MODEL_8688:
983 default: /* for newer chipsets */
984 card->scratch_reg = IF_SDIO_FW_STATUS;
985 break;
988 spin_lock_init(&card->lock);
989 card->workqueue = create_workqueue("libertas_sdio");
990 INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
992 /* Check if we support this card */
993 for (i = 0; i < ARRAY_SIZE(fw_table); i++) {
994 if (card->model == fw_table[i].model)
995 break;
997 if (i == ARRAY_SIZE(fw_table)) {
998 lbs_pr_err("unknown card model 0x%x\n", card->model);
999 ret = -ENODEV;
1000 goto free;
1003 sdio_claim_host(func);
1005 ret = sdio_enable_func(func);
1006 if (ret)
1007 goto release;
1009 ret = sdio_claim_irq(func, if_sdio_interrupt);
1010 if (ret)
1011 goto disable;
1013 /* For 1-bit transfers to the 8686 model, we need to enable the
1014 * interrupt flag in the CCCR register. Set the MMC_QUIRK_LENIENT_FN0
1015 * bit to allow access to non-vendor registers. */
1016 if ((card->model == MODEL_8686) &&
1017 (host->caps & MMC_CAP_SDIO_IRQ) &&
1018 (host->ios.bus_width == MMC_BUS_WIDTH_1)) {
1019 u8 reg;
1021 func->card->quirks |= MMC_QUIRK_LENIENT_FN0;
1022 reg = sdio_f0_readb(func, SDIO_CCCR_IF, &ret);
1023 if (ret)
1024 goto release_int;
1026 reg |= SDIO_BUS_ECSI;
1027 sdio_f0_writeb(func, reg, SDIO_CCCR_IF, &ret);
1028 if (ret)
1029 goto release_int;
1032 card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
1033 if (ret)
1034 goto release_int;
1036 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
1037 if (ret)
1038 goto release_int;
1040 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
1041 if (ret)
1042 goto release_int;
1044 sdio_release_host(func);
1046 sdio_set_drvdata(func, card);
1048 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1049 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1050 func->class, func->vendor, func->device,
1051 model, (unsigned)card->ioport);
1053 ret = if_sdio_prog_firmware(card);
1054 if (ret)
1055 goto reclaim;
1057 priv = lbs_add_card(card, &func->dev);
1058 if (!priv) {
1059 ret = -ENOMEM;
1060 goto reclaim;
1063 card->priv = priv;
1065 priv->card = card;
1066 priv->hw_host_to_card = if_sdio_host_to_card;
1067 priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
1068 priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
1069 priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
1071 sdio_claim_host(func);
1074 * Get rx_unit if the chip is SD8688 or newer.
1075 * SD8385 & SD8686 do not have rx_unit.
1077 if ((card->model != MODEL_8385)
1078 && (card->model != MODEL_8686))
1079 card->rx_unit = if_sdio_read_rx_unit(card);
1080 else
1081 card->rx_unit = 0;
1084 * Enable interrupts now that everything is set up
1086 sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
1087 sdio_release_host(func);
1088 if (ret)
1089 goto reclaim;
1091 priv->fw_ready = 1;
1094 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
1096 if (card->model == MODEL_8688) {
1097 struct cmd_header cmd;
1099 memset(&cmd, 0, sizeof(cmd));
1101 lbs_deb_sdio("send function INIT command\n");
1102 if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
1103 lbs_cmd_copyback, (unsigned long) &cmd))
1104 lbs_pr_alert("CMD_FUNC_INIT cmd failed\n");
1107 ret = lbs_start_card(priv);
1108 if (ret)
1109 goto err_activate_card;
1111 out:
1112 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1114 return ret;
1116 err_activate_card:
1117 flush_workqueue(card->workqueue);
1118 lbs_remove_card(priv);
1119 reclaim:
1120 sdio_claim_host(func);
1121 release_int:
1122 sdio_release_irq(func);
1123 disable:
1124 sdio_disable_func(func);
1125 release:
1126 sdio_release_host(func);
1127 free:
1128 destroy_workqueue(card->workqueue);
1129 while (card->packets) {
1130 packet = card->packets;
1131 card->packets = card->packets->next;
1132 kfree(packet);
1135 if (card->helper_allocated)
1136 kfree(card->helper);
1137 if (card->firmware_allocated)
1138 kfree(card->firmware);
1139 kfree(card);
1141 goto out;
1144 static void if_sdio_remove(struct sdio_func *func)
1146 struct if_sdio_card *card;
1147 struct if_sdio_packet *packet;
1149 lbs_deb_enter(LBS_DEB_SDIO);
1151 card = sdio_get_drvdata(func);
1153 if (user_rmmod && (card->model == MODEL_8688)) {
1155 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1156 * multiple functions
1158 struct cmd_header cmd;
1160 memset(&cmd, 0, sizeof(cmd));
1162 lbs_deb_sdio("send function SHUTDOWN command\n");
1163 if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
1164 &cmd, sizeof(cmd), lbs_cmd_copyback,
1165 (unsigned long) &cmd))
1166 lbs_pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1170 lbs_deb_sdio("call remove card\n");
1171 lbs_stop_card(card->priv);
1172 lbs_remove_card(card->priv);
1174 flush_workqueue(card->workqueue);
1175 destroy_workqueue(card->workqueue);
1177 sdio_claim_host(func);
1178 sdio_release_irq(func);
1179 sdio_disable_func(func);
1180 sdio_release_host(func);
1182 while (card->packets) {
1183 packet = card->packets;
1184 card->packets = card->packets->next;
1185 kfree(packet);
1188 if (card->helper_allocated)
1189 kfree(card->helper);
1190 if (card->firmware_allocated)
1191 kfree(card->firmware);
1192 kfree(card);
1194 lbs_deb_leave(LBS_DEB_SDIO);
1197 static int if_sdio_suspend(struct device *dev)
1199 struct sdio_func *func = dev_to_sdio_func(dev);
1200 int ret;
1201 struct if_sdio_card *card = sdio_get_drvdata(func);
1203 mmc_pm_flag_t flags = sdio_get_host_pm_caps(func);
1205 lbs_pr_info("%s: suspend: PM flags = 0x%x\n",
1206 sdio_func_id(func), flags);
1208 /* If we aren't being asked to wake on anything, we should bail out
1209 * and let the SD stack power down the card.
1211 if (card->priv->wol_criteria == EHS_REMOVE_WAKEUP) {
1212 lbs_pr_info("Suspend without wake params -- "
1213 "powering down card.");
1214 return -ENOSYS;
1217 if (!(flags & MMC_PM_KEEP_POWER)) {
1218 lbs_pr_err("%s: cannot remain alive while host is suspended\n",
1219 sdio_func_id(func));
1220 return -ENOSYS;
1223 ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
1224 if (ret)
1225 return ret;
1227 ret = lbs_suspend(card->priv);
1228 if (ret)
1229 return ret;
1231 return sdio_set_host_pm_flags(func, MMC_PM_WAKE_SDIO_IRQ);
1234 static int if_sdio_resume(struct device *dev)
1236 struct sdio_func *func = dev_to_sdio_func(dev);
1237 struct if_sdio_card *card = sdio_get_drvdata(func);
1238 int ret;
1240 lbs_pr_info("%s: resume: we're back\n", sdio_func_id(func));
1242 ret = lbs_resume(card->priv);
1244 return ret;
1247 static const struct dev_pm_ops if_sdio_pm_ops = {
1248 .suspend = if_sdio_suspend,
1249 .resume = if_sdio_resume,
1252 static struct sdio_driver if_sdio_driver = {
1253 .name = "libertas_sdio",
1254 .id_table = if_sdio_ids,
1255 .probe = if_sdio_probe,
1256 .remove = if_sdio_remove,
1257 .drv = {
1258 .pm = &if_sdio_pm_ops,
1262 /*******************************************************************/
1263 /* Module functions */
1264 /*******************************************************************/
1266 static int __init if_sdio_init_module(void)
1268 int ret = 0;
1270 lbs_deb_enter(LBS_DEB_SDIO);
1272 printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1273 printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1275 ret = sdio_register_driver(&if_sdio_driver);
1277 /* Clear the flag in case user removes the card. */
1278 user_rmmod = 0;
1280 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1282 return ret;
1285 static void __exit if_sdio_exit_module(void)
1287 lbs_deb_enter(LBS_DEB_SDIO);
1289 /* Set the flag as user is removing this module. */
1290 user_rmmod = 1;
1292 sdio_unregister_driver(&if_sdio_driver);
1294 lbs_deb_leave(LBS_DEB_SDIO);
1297 module_init(if_sdio_init_module);
1298 module_exit(if_sdio_exit_module);
1300 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1301 MODULE_AUTHOR("Pierre Ossman");
1302 MODULE_LICENSE("GPL");