[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / net / wireless / libertas / if_sdio.c
blob485a8d406525ccbea266cfe155cca8150ccdf967
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/firmware.h>
32 #include <linux/netdevice.h>
33 #include <linux/delay.h>
34 #include <linux/mmc/card.h>
35 #include <linux/mmc/sdio_func.h>
36 #include <linux/mmc/sdio_ids.h>
38 #include "host.h"
39 #include "decl.h"
40 #include "defs.h"
41 #include "dev.h"
42 #include "cmd.h"
43 #include "if_sdio.h"
45 /* The if_sdio_remove() callback function is called when
46 * user removes this module from kernel space or ejects
47 * the card from the slot. The driver handles these 2 cases
48 * differently for SD8688 combo chip.
49 * If the user is removing the module, the FUNC_SHUTDOWN
50 * command for SD8688 is sent to the firmware.
51 * If the card is removed, there is no need to send this command.
53 * The variable 'user_rmmod' is used to distinguish these two
54 * scenarios. This flag is initialized as FALSE in case the card
55 * is removed, and will be set to TRUE for module removal when
56 * module_exit function is called.
58 static u8 user_rmmod;
60 static char *lbs_helper_name = NULL;
61 module_param_named(helper_name, lbs_helper_name, charp, 0644);
63 static char *lbs_fw_name = NULL;
64 module_param_named(fw_name, lbs_fw_name, charp, 0644);
66 static const struct sdio_device_id if_sdio_ids[] = {
67 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
68 SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
69 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL,
70 SDIO_DEVICE_ID_MARVELL_8688WLAN) },
71 { /* end: all zeroes */ },
74 MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
76 struct if_sdio_model {
77 int model;
78 const char *helper;
79 const char *firmware;
82 static struct if_sdio_model if_sdio_models[] = {
84 /* 8385 */
85 .model = IF_SDIO_MODEL_8385,
86 .helper = "sd8385_helper.bin",
87 .firmware = "sd8385.bin",
90 /* 8686 */
91 .model = IF_SDIO_MODEL_8686,
92 .helper = "sd8686_helper.bin",
93 .firmware = "sd8686.bin",
96 /* 8688 */
97 .model = IF_SDIO_MODEL_8688,
98 .helper = "sd8688_helper.bin",
99 .firmware = "sd8688.bin",
103 struct if_sdio_packet {
104 struct if_sdio_packet *next;
105 u16 nb;
106 u8 buffer[0] __attribute__((aligned(4)));
109 struct if_sdio_card {
110 struct sdio_func *func;
111 struct lbs_private *priv;
113 int model;
114 unsigned long ioport;
115 unsigned int scratch_reg;
117 const char *helper;
118 const char *firmware;
120 u8 buffer[65536];
122 spinlock_t lock;
123 struct if_sdio_packet *packets;
125 struct workqueue_struct *workqueue;
126 struct work_struct packet_worker;
128 u8 rx_unit;
131 /********************************************************************/
132 /* I/O */
133 /********************************************************************/
136 * For SD8385/SD8686, this function reads firmware status after
137 * the image is downloaded, or reads RX packet length when
138 * interrupt (with IF_SDIO_H_INT_UPLD bit set) is received.
139 * For SD8688, this function reads firmware status only.
141 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
143 int ret;
144 u16 scratch;
146 scratch = sdio_readb(card->func, card->scratch_reg, &ret);
147 if (!ret)
148 scratch |= sdio_readb(card->func, card->scratch_reg + 1,
149 &ret) << 8;
151 if (err)
152 *err = ret;
154 if (ret)
155 return 0xffff;
157 return scratch;
160 static u8 if_sdio_read_rx_unit(struct if_sdio_card *card)
162 int ret;
163 u8 rx_unit;
165 rx_unit = sdio_readb(card->func, IF_SDIO_RX_UNIT, &ret);
167 if (ret)
168 rx_unit = 0;
170 return rx_unit;
173 static u16 if_sdio_read_rx_len(struct if_sdio_card *card, int *err)
175 int ret;
176 u16 rx_len;
178 switch (card->model) {
179 case IF_SDIO_MODEL_8385:
180 case IF_SDIO_MODEL_8686:
181 rx_len = if_sdio_read_scratch(card, &ret);
182 break;
183 case IF_SDIO_MODEL_8688:
184 default: /* for newer chipsets */
185 rx_len = sdio_readb(card->func, IF_SDIO_RX_LEN, &ret);
186 if (!ret)
187 rx_len <<= card->rx_unit;
188 else
189 rx_len = 0xffff; /* invalid length */
191 break;
194 if (err)
195 *err = ret;
197 return rx_len;
200 static int if_sdio_handle_cmd(struct if_sdio_card *card,
201 u8 *buffer, unsigned size)
203 struct lbs_private *priv = card->priv;
204 int ret;
205 unsigned long flags;
206 u8 i;
208 lbs_deb_enter(LBS_DEB_SDIO);
210 if (size > LBS_CMD_BUFFER_SIZE) {
211 lbs_deb_sdio("response packet too large (%d bytes)\n",
212 (int)size);
213 ret = -E2BIG;
214 goto out;
217 spin_lock_irqsave(&priv->driver_lock, flags);
219 i = (priv->resp_idx == 0) ? 1 : 0;
220 BUG_ON(priv->resp_len[i]);
221 priv->resp_len[i] = size;
222 memcpy(priv->resp_buf[i], buffer, size);
223 lbs_notify_command_response(priv, i);
225 spin_unlock_irqrestore(&card->priv->driver_lock, flags);
227 ret = 0;
229 out:
230 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
231 return ret;
234 static int if_sdio_handle_data(struct if_sdio_card *card,
235 u8 *buffer, unsigned size)
237 int ret;
238 struct sk_buff *skb;
239 char *data;
241 lbs_deb_enter(LBS_DEB_SDIO);
243 if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
244 lbs_deb_sdio("response packet too large (%d bytes)\n",
245 (int)size);
246 ret = -E2BIG;
247 goto out;
250 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
251 if (!skb) {
252 ret = -ENOMEM;
253 goto out;
256 skb_reserve(skb, NET_IP_ALIGN);
258 data = skb_put(skb, size);
260 memcpy(data, buffer, size);
262 lbs_process_rxed_packet(card->priv, skb);
264 ret = 0;
266 out:
267 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
269 return ret;
272 static int if_sdio_handle_event(struct if_sdio_card *card,
273 u8 *buffer, unsigned size)
275 int ret;
276 u32 event;
278 lbs_deb_enter(LBS_DEB_SDIO);
280 if (card->model == IF_SDIO_MODEL_8385) {
281 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
282 if (ret)
283 goto out;
285 /* right shift 3 bits to get the event id */
286 event >>= 3;
287 } else {
288 if (size < 4) {
289 lbs_deb_sdio("event packet too small (%d bytes)\n",
290 (int)size);
291 ret = -EINVAL;
292 goto out;
294 event = buffer[3] << 24;
295 event |= buffer[2] << 16;
296 event |= buffer[1] << 8;
297 event |= buffer[0] << 0;
300 lbs_queue_event(card->priv, event & 0xFF);
301 ret = 0;
303 out:
304 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
306 return ret;
309 static int if_sdio_card_to_host(struct if_sdio_card *card)
311 int ret;
312 u8 status;
313 u16 size, type, chunk;
314 unsigned long timeout;
316 lbs_deb_enter(LBS_DEB_SDIO);
318 size = if_sdio_read_rx_len(card, &ret);
319 if (ret)
320 goto out;
322 if (size < 4) {
323 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
324 (int)size);
325 ret = -EINVAL;
326 goto out;
329 timeout = jiffies + HZ;
330 while (1) {
331 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
332 if (ret)
333 goto out;
334 if (status & IF_SDIO_IO_RDY)
335 break;
336 if (time_after(jiffies, timeout)) {
337 ret = -ETIMEDOUT;
338 goto out;
340 mdelay(1);
344 * The transfer must be in one transaction or the firmware
345 * goes suicidal. There's no way to guarantee that for all
346 * controllers, but we can at least try.
348 chunk = sdio_align_size(card->func, size);
350 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
351 if (ret)
352 goto out;
354 chunk = card->buffer[0] | (card->buffer[1] << 8);
355 type = card->buffer[2] | (card->buffer[3] << 8);
357 lbs_deb_sdio("packet of type %d and size %d bytes\n",
358 (int)type, (int)chunk);
360 if (chunk > size) {
361 lbs_deb_sdio("packet fragment (%d > %d)\n",
362 (int)chunk, (int)size);
363 ret = -EINVAL;
364 goto out;
367 if (chunk < size) {
368 lbs_deb_sdio("packet fragment (%d < %d)\n",
369 (int)chunk, (int)size);
372 switch (type) {
373 case MVMS_CMD:
374 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
375 if (ret)
376 goto out;
377 break;
378 case MVMS_DAT:
379 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
380 if (ret)
381 goto out;
382 break;
383 case MVMS_EVENT:
384 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
385 if (ret)
386 goto out;
387 break;
388 default:
389 lbs_deb_sdio("invalid type (%d) from firmware\n",
390 (int)type);
391 ret = -EINVAL;
392 goto out;
395 out:
396 if (ret)
397 lbs_pr_err("problem fetching packet from firmware\n");
399 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
401 return ret;
404 static void if_sdio_host_to_card_worker(struct work_struct *work)
406 struct if_sdio_card *card;
407 struct if_sdio_packet *packet;
408 unsigned long timeout;
409 u8 status;
410 int ret;
411 unsigned long flags;
413 lbs_deb_enter(LBS_DEB_SDIO);
415 card = container_of(work, struct if_sdio_card, packet_worker);
417 while (1) {
418 spin_lock_irqsave(&card->lock, flags);
419 packet = card->packets;
420 if (packet)
421 card->packets = packet->next;
422 spin_unlock_irqrestore(&card->lock, flags);
424 if (!packet)
425 break;
427 sdio_claim_host(card->func);
429 timeout = jiffies + HZ;
430 while (1) {
431 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
432 if (ret)
433 goto release;
434 if (status & IF_SDIO_IO_RDY)
435 break;
436 if (time_after(jiffies, timeout)) {
437 ret = -ETIMEDOUT;
438 goto release;
440 mdelay(1);
443 ret = sdio_writesb(card->func, card->ioport,
444 packet->buffer, packet->nb);
445 if (ret)
446 goto release;
447 release:
448 sdio_release_host(card->func);
450 kfree(packet);
453 lbs_deb_leave(LBS_DEB_SDIO);
456 /********************************************************************/
457 /* Firmware */
458 /********************************************************************/
460 static int if_sdio_prog_helper(struct if_sdio_card *card)
462 int ret;
463 u8 status;
464 const struct firmware *fw;
465 unsigned long timeout;
466 u8 *chunk_buffer;
467 u32 chunk_size;
468 const u8 *firmware;
469 size_t size;
471 lbs_deb_enter(LBS_DEB_SDIO);
473 ret = request_firmware(&fw, card->helper, &card->func->dev);
474 if (ret) {
475 lbs_pr_err("can't load helper firmware\n");
476 goto out;
479 chunk_buffer = kzalloc(64, GFP_KERNEL);
480 if (!chunk_buffer) {
481 ret = -ENOMEM;
482 goto release_fw;
485 sdio_claim_host(card->func);
487 ret = sdio_set_block_size(card->func, 32);
488 if (ret)
489 goto release;
491 firmware = fw->data;
492 size = fw->size;
494 while (size) {
495 timeout = jiffies + HZ;
496 while (1) {
497 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
498 if (ret)
499 goto release;
500 if ((status & IF_SDIO_IO_RDY) &&
501 (status & IF_SDIO_DL_RDY))
502 break;
503 if (time_after(jiffies, timeout)) {
504 ret = -ETIMEDOUT;
505 goto release;
507 mdelay(1);
510 chunk_size = min(size, (size_t)60);
512 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
513 memcpy(chunk_buffer + 4, firmware, chunk_size);
515 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
517 ret = sdio_writesb(card->func, card->ioport,
518 chunk_buffer, 64);
519 if (ret)
520 goto release;
522 firmware += chunk_size;
523 size -= chunk_size;
526 /* an empty block marks the end of the transfer */
527 memset(chunk_buffer, 0, 4);
528 ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
529 if (ret)
530 goto release;
532 lbs_deb_sdio("waiting for helper to boot...\n");
534 /* wait for the helper to boot by looking at the size register */
535 timeout = jiffies + HZ;
536 while (1) {
537 u16 req_size;
539 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
540 if (ret)
541 goto release;
543 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
544 if (ret)
545 goto release;
547 if (req_size != 0)
548 break;
550 if (time_after(jiffies, timeout)) {
551 ret = -ETIMEDOUT;
552 goto release;
555 msleep(10);
558 ret = 0;
560 release:
561 sdio_release_host(card->func);
562 kfree(chunk_buffer);
563 release_fw:
564 release_firmware(fw);
566 out:
567 if (ret)
568 lbs_pr_err("failed to load helper firmware\n");
570 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
572 return ret;
575 static int if_sdio_prog_real(struct if_sdio_card *card)
577 int ret;
578 u8 status;
579 const struct firmware *fw;
580 unsigned long timeout;
581 u8 *chunk_buffer;
582 u32 chunk_size;
583 const u8 *firmware;
584 size_t size, req_size;
586 lbs_deb_enter(LBS_DEB_SDIO);
588 ret = request_firmware(&fw, card->firmware, &card->func->dev);
589 if (ret) {
590 lbs_pr_err("can't load firmware\n");
591 goto out;
594 chunk_buffer = kzalloc(512, GFP_KERNEL);
595 if (!chunk_buffer) {
596 ret = -ENOMEM;
597 goto release_fw;
600 sdio_claim_host(card->func);
602 ret = sdio_set_block_size(card->func, 32);
603 if (ret)
604 goto release;
606 firmware = fw->data;
607 size = fw->size;
609 while (size) {
610 timeout = jiffies + HZ;
611 while (1) {
612 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
613 if (ret)
614 goto release;
615 if ((status & IF_SDIO_IO_RDY) &&
616 (status & IF_SDIO_DL_RDY))
617 break;
618 if (time_after(jiffies, timeout)) {
619 ret = -ETIMEDOUT;
620 goto release;
622 mdelay(1);
625 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
626 if (ret)
627 goto release;
629 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
630 if (ret)
631 goto release;
633 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
635 if (req_size == 0) {
636 lbs_deb_sdio("firmware helper gave up early\n");
637 ret = -EIO;
638 goto release;
641 if (req_size & 0x01) {
642 lbs_deb_sdio("firmware helper signalled error\n");
643 ret = -EIO;
644 goto release;
647 if (req_size > size)
648 req_size = size;
650 while (req_size) {
651 chunk_size = min(req_size, (size_t)512);
653 memcpy(chunk_buffer, firmware, chunk_size);
655 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
656 chunk_size, (chunk_size + 31) / 32 * 32);
658 ret = sdio_writesb(card->func, card->ioport,
659 chunk_buffer, roundup(chunk_size, 32));
660 if (ret)
661 goto release;
663 firmware += chunk_size;
664 size -= chunk_size;
665 req_size -= chunk_size;
669 ret = 0;
671 lbs_deb_sdio("waiting for firmware to boot...\n");
673 /* wait for the firmware to boot */
674 timeout = jiffies + HZ;
675 while (1) {
676 u16 scratch;
678 scratch = if_sdio_read_scratch(card, &ret);
679 if (ret)
680 goto release;
682 if (scratch == IF_SDIO_FIRMWARE_OK)
683 break;
685 if (time_after(jiffies, timeout)) {
686 ret = -ETIMEDOUT;
687 goto release;
690 msleep(10);
693 ret = 0;
695 release:
696 sdio_release_host(card->func);
697 kfree(chunk_buffer);
698 release_fw:
699 release_firmware(fw);
701 out:
702 if (ret)
703 lbs_pr_err("failed to load firmware\n");
705 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
707 return ret;
710 static int if_sdio_prog_firmware(struct if_sdio_card *card)
712 int ret;
713 u16 scratch;
715 lbs_deb_enter(LBS_DEB_SDIO);
717 sdio_claim_host(card->func);
718 scratch = if_sdio_read_scratch(card, &ret);
719 sdio_release_host(card->func);
721 if (ret)
722 goto out;
724 lbs_deb_sdio("firmware status = %#x\n", scratch);
726 if (scratch == IF_SDIO_FIRMWARE_OK) {
727 lbs_deb_sdio("firmware already loaded\n");
728 goto success;
731 ret = if_sdio_prog_helper(card);
732 if (ret)
733 goto out;
735 ret = if_sdio_prog_real(card);
736 if (ret)
737 goto out;
739 success:
740 sdio_claim_host(card->func);
741 sdio_set_block_size(card->func, IF_SDIO_BLOCK_SIZE);
742 sdio_release_host(card->func);
743 ret = 0;
745 out:
746 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
748 return ret;
751 /*******************************************************************/
752 /* Libertas callbacks */
753 /*******************************************************************/
755 static int if_sdio_host_to_card(struct lbs_private *priv,
756 u8 type, u8 *buf, u16 nb)
758 int ret;
759 struct if_sdio_card *card;
760 struct if_sdio_packet *packet, *cur;
761 u16 size;
762 unsigned long flags;
764 lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
766 card = priv->card;
768 if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
769 ret = -EINVAL;
770 goto out;
774 * The transfer must be in one transaction or the firmware
775 * goes suicidal. There's no way to guarantee that for all
776 * controllers, but we can at least try.
778 size = sdio_align_size(card->func, nb + 4);
780 packet = kzalloc(sizeof(struct if_sdio_packet) + size,
781 GFP_ATOMIC);
782 if (!packet) {
783 ret = -ENOMEM;
784 goto out;
787 packet->next = NULL;
788 packet->nb = size;
791 * SDIO specific header.
793 packet->buffer[0] = (nb + 4) & 0xff;
794 packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
795 packet->buffer[2] = type;
796 packet->buffer[3] = 0;
798 memcpy(packet->buffer + 4, buf, nb);
800 spin_lock_irqsave(&card->lock, flags);
802 if (!card->packets)
803 card->packets = packet;
804 else {
805 cur = card->packets;
806 while (cur->next)
807 cur = cur->next;
808 cur->next = packet;
811 switch (type) {
812 case MVMS_CMD:
813 priv->dnld_sent = DNLD_CMD_SENT;
814 break;
815 case MVMS_DAT:
816 priv->dnld_sent = DNLD_DATA_SENT;
817 break;
818 default:
819 lbs_deb_sdio("unknown packet type %d\n", (int)type);
822 spin_unlock_irqrestore(&card->lock, flags);
824 queue_work(card->workqueue, &card->packet_worker);
826 ret = 0;
828 out:
829 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
831 return ret;
834 /*******************************************************************/
835 /* SDIO callbacks */
836 /*******************************************************************/
838 static void if_sdio_interrupt(struct sdio_func *func)
840 int ret;
841 struct if_sdio_card *card;
842 u8 cause;
844 lbs_deb_enter(LBS_DEB_SDIO);
846 card = sdio_get_drvdata(func);
848 cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
849 if (ret)
850 goto out;
852 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
854 sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
855 if (ret)
856 goto out;
859 * Ignore the define name, this really means the card has
860 * successfully received the command.
862 if (cause & IF_SDIO_H_INT_DNLD)
863 lbs_host_to_card_done(card->priv);
866 if (cause & IF_SDIO_H_INT_UPLD) {
867 ret = if_sdio_card_to_host(card);
868 if (ret)
869 goto out;
872 ret = 0;
874 out:
875 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
878 static int if_sdio_probe(struct sdio_func *func,
879 const struct sdio_device_id *id)
881 struct if_sdio_card *card;
882 struct lbs_private *priv;
883 int ret, i;
884 unsigned int model;
885 struct if_sdio_packet *packet;
887 lbs_deb_enter(LBS_DEB_SDIO);
889 for (i = 0;i < func->card->num_info;i++) {
890 if (sscanf(func->card->info[i],
891 "802.11 SDIO ID: %x", &model) == 1)
892 break;
893 if (sscanf(func->card->info[i],
894 "ID: %x", &model) == 1)
895 break;
896 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
897 model = IF_SDIO_MODEL_8385;
898 break;
902 if (i == func->card->num_info) {
903 lbs_pr_err("unable to identify card model\n");
904 return -ENODEV;
907 card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
908 if (!card)
909 return -ENOMEM;
911 card->func = func;
912 card->model = model;
914 switch (card->model) {
915 case IF_SDIO_MODEL_8385:
916 card->scratch_reg = IF_SDIO_SCRATCH_OLD;
917 break;
918 case IF_SDIO_MODEL_8686:
919 card->scratch_reg = IF_SDIO_SCRATCH;
920 break;
921 case IF_SDIO_MODEL_8688:
922 default: /* for newer chipsets */
923 card->scratch_reg = IF_SDIO_FW_STATUS;
924 break;
927 spin_lock_init(&card->lock);
928 card->workqueue = create_workqueue("libertas_sdio");
929 INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
931 for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) {
932 if (card->model == if_sdio_models[i].model)
933 break;
936 if (i == ARRAY_SIZE(if_sdio_models)) {
937 lbs_pr_err("unkown card model 0x%x\n", card->model);
938 ret = -ENODEV;
939 goto free;
942 card->helper = if_sdio_models[i].helper;
943 card->firmware = if_sdio_models[i].firmware;
945 if (lbs_helper_name) {
946 lbs_deb_sdio("overriding helper firmware: %s\n",
947 lbs_helper_name);
948 card->helper = lbs_helper_name;
951 if (lbs_fw_name) {
952 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name);
953 card->firmware = lbs_fw_name;
956 sdio_claim_host(func);
958 ret = sdio_enable_func(func);
959 if (ret)
960 goto release;
962 ret = sdio_claim_irq(func, if_sdio_interrupt);
963 if (ret)
964 goto disable;
966 card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
967 if (ret)
968 goto release_int;
970 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
971 if (ret)
972 goto release_int;
974 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
975 if (ret)
976 goto release_int;
978 sdio_release_host(func);
980 sdio_set_drvdata(func, card);
982 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
983 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
984 func->class, func->vendor, func->device,
985 model, (unsigned)card->ioport);
987 ret = if_sdio_prog_firmware(card);
988 if (ret)
989 goto reclaim;
991 priv = lbs_add_card(card, &func->dev);
992 if (!priv) {
993 ret = -ENOMEM;
994 goto reclaim;
997 card->priv = priv;
999 priv->card = card;
1000 priv->hw_host_to_card = if_sdio_host_to_card;
1002 priv->fw_ready = 1;
1004 sdio_claim_host(func);
1007 * Get rx_unit if the chip is SD8688 or newer.
1008 * SD8385 & SD8686 do not have rx_unit.
1010 if ((card->model != IF_SDIO_MODEL_8385)
1011 && (card->model != IF_SDIO_MODEL_8686))
1012 card->rx_unit = if_sdio_read_rx_unit(card);
1013 else
1014 card->rx_unit = 0;
1017 * Enable interrupts now that everything is set up
1019 sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
1020 sdio_release_host(func);
1021 if (ret)
1022 goto reclaim;
1025 * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
1027 if (card->model == IF_SDIO_MODEL_8688) {
1028 struct cmd_header cmd;
1030 memset(&cmd, 0, sizeof(cmd));
1032 lbs_deb_sdio("send function INIT command\n");
1033 if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
1034 lbs_cmd_copyback, (unsigned long) &cmd))
1035 lbs_pr_alert("CMD_FUNC_INIT cmd failed\n");
1038 ret = lbs_start_card(priv);
1039 if (ret)
1040 goto err_activate_card;
1042 out:
1043 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1045 return ret;
1047 err_activate_card:
1048 flush_workqueue(card->workqueue);
1049 lbs_remove_card(priv);
1050 reclaim:
1051 sdio_claim_host(func);
1052 release_int:
1053 sdio_release_irq(func);
1054 disable:
1055 sdio_disable_func(func);
1056 release:
1057 sdio_release_host(func);
1058 free:
1059 destroy_workqueue(card->workqueue);
1060 while (card->packets) {
1061 packet = card->packets;
1062 card->packets = card->packets->next;
1063 kfree(packet);
1066 kfree(card);
1068 goto out;
1071 static void if_sdio_remove(struct sdio_func *func)
1073 struct if_sdio_card *card;
1074 struct if_sdio_packet *packet;
1076 lbs_deb_enter(LBS_DEB_SDIO);
1078 card = sdio_get_drvdata(func);
1080 if (user_rmmod && (card->model == IF_SDIO_MODEL_8688)) {
1082 * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1083 * multiple functions
1085 struct cmd_header cmd;
1087 memset(&cmd, 0, sizeof(cmd));
1089 lbs_deb_sdio("send function SHUTDOWN command\n");
1090 if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
1091 &cmd, sizeof(cmd), lbs_cmd_copyback,
1092 (unsigned long) &cmd))
1093 lbs_pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1097 lbs_deb_sdio("call remove card\n");
1098 lbs_stop_card(card->priv);
1099 lbs_remove_card(card->priv);
1100 card->priv->surpriseremoved = 1;
1102 flush_workqueue(card->workqueue);
1103 destroy_workqueue(card->workqueue);
1105 sdio_claim_host(func);
1106 sdio_release_irq(func);
1107 sdio_disable_func(func);
1108 sdio_release_host(func);
1110 while (card->packets) {
1111 packet = card->packets;
1112 card->packets = card->packets->next;
1113 kfree(packet);
1116 kfree(card);
1118 lbs_deb_leave(LBS_DEB_SDIO);
1121 static struct sdio_driver if_sdio_driver = {
1122 .name = "libertas_sdio",
1123 .id_table = if_sdio_ids,
1124 .probe = if_sdio_probe,
1125 .remove = if_sdio_remove,
1128 /*******************************************************************/
1129 /* Module functions */
1130 /*******************************************************************/
1132 static int __init if_sdio_init_module(void)
1134 int ret = 0;
1136 lbs_deb_enter(LBS_DEB_SDIO);
1138 printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1139 printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1141 ret = sdio_register_driver(&if_sdio_driver);
1143 /* Clear the flag in case user removes the card. */
1144 user_rmmod = 0;
1146 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1148 return ret;
1151 static void __exit if_sdio_exit_module(void)
1153 lbs_deb_enter(LBS_DEB_SDIO);
1155 /* Set the flag as user is removing this module. */
1156 user_rmmod = 1;
1158 sdio_unregister_driver(&if_sdio_driver);
1160 lbs_deb_leave(LBS_DEB_SDIO);
1163 module_init(if_sdio_init_module);
1164 module_exit(if_sdio_exit_module);
1166 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1167 MODULE_AUTHOR("Pierre Ossman");
1168 MODULE_LICENSE("GPL");