libertas: Consolidate lbs_host_to_card_done() function.
[linux-2.6] / drivers / net / wireless / libertas / if_sdio.c
1 /*
2  *  linux/drivers/net/wireless/libertas/if_sdio.c
3  *
4  *  Copyright 2007 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 #include <linux/moduleparam.h>
30 #include <linux/firmware.h>
31 #include <linux/netdevice.h>
32 #include <linux/delay.h>
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/sdio_func.h>
35 #include <linux/mmc/sdio_ids.h>
36
37 #include "host.h"
38 #include "decl.h"
39 #include "defs.h"
40 #include "dev.h"
41 #include "if_sdio.h"
42
43 static char *lbs_helper_name = NULL;
44 module_param_named(helper_name, lbs_helper_name, charp, 0644);
45
46 static char *lbs_fw_name = NULL;
47 module_param_named(fw_name, lbs_fw_name, charp, 0644);
48
49 static const struct sdio_device_id if_sdio_ids[] = {
50         { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
51         { /* end: all zeroes */                                         },
52 };
53
54 MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
55
56 struct if_sdio_model {
57         int model;
58         const char *helper;
59         const char *firmware;
60 };
61
62 static struct if_sdio_model if_sdio_models[] = {
63         {
64                 /* 8385 */
65                 .model = 0x04,
66                 .helper = "sd8385_helper.bin",
67                 .firmware = "sd8385.bin",
68         },
69         {
70                 /* 8686 */
71                 .model = 0x0B,
72                 .helper = "sd8686_helper.bin",
73                 .firmware = "sd8686.bin",
74         },
75 };
76
77 struct if_sdio_packet {
78         struct if_sdio_packet   *next;
79         u16                     nb;
80         u8                      buffer[0] __attribute__((aligned(4)));
81 };
82
83 struct if_sdio_card {
84         struct sdio_func        *func;
85         struct lbs_private      *priv;
86
87         int                     model;
88         unsigned long           ioport;
89
90         const char              *helper;
91         const char              *firmware;
92
93         u8                      buffer[65536];
94         u8                      int_cause;
95         u32                     event;
96
97         spinlock_t              lock;
98         struct if_sdio_packet   *packets;
99         struct work_struct      packet_worker;
100 };
101
102 /********************************************************************/
103 /* I/O                                                              */
104 /********************************************************************/
105
106 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
107 {
108         int ret, reg;
109         u16 scratch;
110
111         if (card->model == 0x04)
112                 reg = IF_SDIO_SCRATCH_OLD;
113         else
114                 reg = IF_SDIO_SCRATCH;
115
116         scratch = sdio_readb(card->func, reg, &ret);
117         if (!ret)
118                 scratch |= sdio_readb(card->func, reg + 1, &ret) << 8;
119
120         if (err)
121                 *err = ret;
122
123         if (ret)
124                 return 0xffff;
125
126         return scratch;
127 }
128
129 static int if_sdio_handle_cmd(struct if_sdio_card *card,
130                 u8 *buffer, unsigned size)
131 {
132         int ret;
133         unsigned long flags;
134
135         lbs_deb_enter(LBS_DEB_SDIO);
136
137         spin_lock_irqsave(&card->priv->adapter->driver_lock, flags);
138
139         if (!card->priv->adapter->cur_cmd) {
140                 lbs_deb_sdio("discarding spurious response\n");
141                 ret = 0;
142                 goto out;
143         }
144
145         if (size > MRVDRV_SIZE_OF_CMD_BUFFER) {
146                 lbs_deb_sdio("response packet too large (%d bytes)\n",
147                         (int)size);
148                 ret = -E2BIG;
149                 goto out;
150         }
151
152         memcpy(card->priv->adapter->cur_cmd->bufvirtualaddr, buffer, size);
153         card->priv->upld_len = size;
154
155         card->int_cause |= MRVDRV_CMD_UPLD_RDY;
156
157         lbs_interrupt(card->priv->dev);
158
159         ret = 0;
160
161 out:
162         spin_unlock_irqrestore(&card->priv->adapter->driver_lock, flags);
163
164         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
165
166         return ret;
167 }
168
169 static int if_sdio_handle_data(struct if_sdio_card *card,
170                 u8 *buffer, unsigned size)
171 {
172         int ret;
173         struct sk_buff *skb;
174         char *data;
175
176         lbs_deb_enter(LBS_DEB_SDIO);
177
178         if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
179                 lbs_deb_sdio("response packet too large (%d bytes)\n",
180                         (int)size);
181                 ret = -E2BIG;
182                 goto out;
183         }
184
185         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
186         if (!skb) {
187                 ret = -ENOMEM;
188                 goto out;
189         }
190
191         skb_reserve(skb, NET_IP_ALIGN);
192
193         data = skb_put(skb, size);
194
195         memcpy(data, buffer, size);
196
197         lbs_process_rxed_packet(card->priv, skb);
198
199         ret = 0;
200
201 out:
202         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
203
204         return ret;
205 }
206
207 static int if_sdio_handle_event(struct if_sdio_card *card,
208                 u8 *buffer, unsigned size)
209 {
210         int ret;
211         unsigned long flags;
212         u32 event;
213
214         lbs_deb_enter(LBS_DEB_SDIO);
215
216         if (card->model == 0x04) {
217                 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
218                 if (ret)
219                         goto out;
220         } else {
221                 if (size < 4) {
222                         lbs_deb_sdio("event packet too small (%d bytes)\n",
223                                 (int)size);
224                         ret = -EINVAL;
225                         goto out;
226                 }
227                 event = buffer[3] << 24;
228                 event |= buffer[2] << 16;
229                 event |= buffer[1] << 8;
230                 event |= buffer[0] << 0;
231                 event <<= SBI_EVENT_CAUSE_SHIFT;
232         }
233
234         spin_lock_irqsave(&card->priv->adapter->driver_lock, flags);
235
236         card->event = event;
237         card->int_cause |= MRVDRV_CARDEVENT;
238
239         lbs_interrupt(card->priv->dev);
240
241         spin_unlock_irqrestore(&card->priv->adapter->driver_lock, flags);
242
243         ret = 0;
244
245 out:
246         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
247
248         return ret;
249 }
250
251 static int if_sdio_card_to_host(struct if_sdio_card *card)
252 {
253         int ret;
254         u8 status;
255         u16 size, type, chunk;
256         unsigned long timeout;
257
258         lbs_deb_enter(LBS_DEB_SDIO);
259
260         size = if_sdio_read_scratch(card, &ret);
261         if (ret)
262                 goto out;
263
264         if (size < 4) {
265                 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
266                         (int)size);
267                 ret = -EINVAL;
268                 goto out;
269         }
270
271         timeout = jiffies + HZ;
272         while (1) {
273                 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
274                 if (ret)
275                         goto out;
276                 if (status & IF_SDIO_IO_RDY)
277                         break;
278                 if (time_after(jiffies, timeout)) {
279                         ret = -ETIMEDOUT;
280                         goto out;
281                 }
282                 mdelay(1);
283         }
284
285         /*
286          * The transfer must be in one transaction or the firmware
287          * goes suicidal.
288          */
289         chunk = size;
290         if ((chunk > card->func->cur_blksize) || (chunk > 512)) {
291                 chunk = (chunk + card->func->cur_blksize - 1) /
292                         card->func->cur_blksize * card->func->cur_blksize;
293         }
294
295         ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
296         if (ret)
297                 goto out;
298
299         chunk = card->buffer[0] | (card->buffer[1] << 8);
300         type = card->buffer[2] | (card->buffer[3] << 8);
301
302         lbs_deb_sdio("packet of type %d and size %d bytes\n",
303                 (int)type, (int)chunk);
304
305         if (chunk > size) {
306                 lbs_deb_sdio("packet fragment (%d > %d)\n",
307                         (int)chunk, (int)size);
308                 ret = -EINVAL;
309                 goto out;
310         }
311
312         if (chunk < size) {
313                 lbs_deb_sdio("packet fragment (%d < %d)\n",
314                         (int)chunk, (int)size);
315         }
316
317         switch (type) {
318         case MVMS_CMD:
319                 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
320                 if (ret)
321                         goto out;
322                 break;
323         case MVMS_DAT:
324                 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
325                 if (ret)
326                         goto out;
327                 break;
328         case MVMS_EVENT:
329                 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
330                 if (ret)
331                         goto out;
332                 break;
333         default:
334                 lbs_deb_sdio("invalid type (%d) from firmware\n",
335                                 (int)type);
336                 ret = -EINVAL;
337                 goto out;
338         }
339
340 out:
341         if (ret)
342                 lbs_pr_err("problem fetching packet from firmware\n");
343
344         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
345
346         return ret;
347 }
348
349 static void if_sdio_host_to_card_worker(struct work_struct *work)
350 {
351         struct if_sdio_card *card;
352         struct if_sdio_packet *packet;
353         unsigned long timeout;
354         u8 status;
355         int ret;
356         unsigned long flags;
357
358         lbs_deb_enter(LBS_DEB_SDIO);
359
360         card = container_of(work, struct if_sdio_card, packet_worker);
361
362         while (1) {
363                 spin_lock_irqsave(&card->lock, flags);
364                 packet = card->packets;
365                 if (packet)
366                         card->packets = packet->next;
367                 spin_unlock_irqrestore(&card->lock, flags);
368
369                 if (!packet)
370                         break;
371
372                 sdio_claim_host(card->func);
373
374                 timeout = jiffies + HZ;
375                 while (1) {
376                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
377                         if (ret)
378                                 goto release;
379                         if (status & IF_SDIO_IO_RDY)
380                                 break;
381                         if (time_after(jiffies, timeout)) {
382                                 ret = -ETIMEDOUT;
383                                 goto release;
384                         }
385                         mdelay(1);
386                 }
387
388                 ret = sdio_writesb(card->func, card->ioport,
389                                 packet->buffer, packet->nb);
390                 if (ret)
391                         goto release;
392 release:
393                 sdio_release_host(card->func);
394
395                 kfree(packet);
396         }
397
398         lbs_deb_leave(LBS_DEB_SDIO);
399 }
400
401 /********************************************************************/
402 /* Firmware                                                         */
403 /********************************************************************/
404
405 static int if_sdio_prog_helper(struct if_sdio_card *card)
406 {
407         int ret;
408         u8 status;
409         const struct firmware *fw;
410         unsigned long timeout;
411         u8 *chunk_buffer;
412         u32 chunk_size;
413         u8 *firmware;
414         size_t size;
415
416         lbs_deb_enter(LBS_DEB_SDIO);
417
418         ret = request_firmware(&fw, card->helper, &card->func->dev);
419         if (ret) {
420                 lbs_pr_err("can't load helper firmware\n");
421                 goto out;
422         }
423
424         chunk_buffer = kzalloc(64, GFP_KERNEL);
425         if (!chunk_buffer) {
426                 ret = -ENOMEM;
427                 goto release_fw;
428         }
429
430         sdio_claim_host(card->func);
431
432         ret = sdio_set_block_size(card->func, 32);
433         if (ret)
434                 goto release;
435
436         firmware = fw->data;
437         size = fw->size;
438
439         while (size) {
440                 timeout = jiffies + HZ;
441                 while (1) {
442                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
443                         if (ret)
444                                 goto release;
445                         if ((status & IF_SDIO_IO_RDY) &&
446                                         (status & IF_SDIO_DL_RDY))
447                                 break;
448                         if (time_after(jiffies, timeout)) {
449                                 ret = -ETIMEDOUT;
450                                 goto release;
451                         }
452                         mdelay(1);
453                 }
454
455                 chunk_size = min(size, (size_t)60);
456
457                 *((u32*)chunk_buffer) = cpu_to_le32(chunk_size);
458                 memcpy(chunk_buffer + 4, firmware, chunk_size);
459 /*
460                 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
461 */
462                 ret = sdio_writesb(card->func, card->ioport,
463                                 chunk_buffer, 64);
464                 if (ret)
465                         goto release;
466
467                 firmware += chunk_size;
468                 size -= chunk_size;
469         }
470
471         /* an empty block marks the end of the transfer */
472         memset(chunk_buffer, 0, 4);
473         ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
474         if (ret)
475                 goto release;
476
477         lbs_deb_sdio("waiting for helper to boot...\n");
478
479         /* wait for the helper to boot by looking at the size register */
480         timeout = jiffies + HZ;
481         while (1) {
482                 u16 req_size;
483
484                 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
485                 if (ret)
486                         goto release;
487
488                 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
489                 if (ret)
490                         goto release;
491
492                 if (req_size != 0)
493                         break;
494
495                 if (time_after(jiffies, timeout)) {
496                         ret = -ETIMEDOUT;
497                         goto release;
498                 }
499
500                 msleep(10);
501         }
502
503         ret = 0;
504
505 release:
506         sdio_set_block_size(card->func, 0);
507         sdio_release_host(card->func);
508         kfree(chunk_buffer);
509 release_fw:
510         release_firmware(fw);
511
512 out:
513         if (ret)
514                 lbs_pr_err("failed to load helper firmware\n");
515
516         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
517
518         return ret;
519 }
520
521 static int if_sdio_prog_real(struct if_sdio_card *card)
522 {
523         int ret;
524         u8 status;
525         const struct firmware *fw;
526         unsigned long timeout;
527         u8 *chunk_buffer;
528         u32 chunk_size;
529         u8 *firmware;
530         size_t size, req_size;
531
532         lbs_deb_enter(LBS_DEB_SDIO);
533
534         ret = request_firmware(&fw, card->firmware, &card->func->dev);
535         if (ret) {
536                 lbs_pr_err("can't load firmware\n");
537                 goto out;
538         }
539
540         chunk_buffer = kzalloc(512, GFP_KERNEL);
541         if (!chunk_buffer) {
542                 ret = -ENOMEM;
543                 goto release_fw;
544         }
545
546         sdio_claim_host(card->func);
547
548         ret = sdio_set_block_size(card->func, 32);
549         if (ret)
550                 goto release;
551
552         firmware = fw->data;
553         size = fw->size;
554
555         while (size) {
556                 timeout = jiffies + HZ;
557                 while (1) {
558                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
559                         if (ret)
560                                 goto release;
561                         if ((status & IF_SDIO_IO_RDY) &&
562                                         (status & IF_SDIO_DL_RDY))
563                                 break;
564                         if (time_after(jiffies, timeout)) {
565                                 ret = -ETIMEDOUT;
566                                 goto release;
567                         }
568                         mdelay(1);
569                 }
570
571                 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
572                 if (ret)
573                         goto release;
574
575                 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
576                 if (ret)
577                         goto release;
578 /*
579                 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
580 */
581                 if (req_size == 0) {
582                         lbs_deb_sdio("firmware helper gave up early\n");
583                         ret = -EIO;
584                         goto release;
585                 }
586
587                 if (req_size & 0x01) {
588                         lbs_deb_sdio("firmware helper signalled error\n");
589                         ret = -EIO;
590                         goto release;
591                 }
592
593                 if (req_size > size)
594                         req_size = size;
595
596                 while (req_size) {
597                         chunk_size = min(req_size, (size_t)512);
598
599                         memcpy(chunk_buffer, firmware, chunk_size);
600 /*
601                         lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
602                                 chunk_size, (chunk_size + 31) / 32 * 32);
603 */
604                         ret = sdio_writesb(card->func, card->ioport,
605                                 chunk_buffer, (chunk_size + 31) / 32 * 32);
606                         if (ret)
607                                 goto release;
608
609                         firmware += chunk_size;
610                         size -= chunk_size;
611                         req_size -= chunk_size;
612                 }
613         }
614
615         ret = 0;
616
617         lbs_deb_sdio("waiting for firmware to boot...\n");
618
619         /* wait for the firmware to boot */
620         timeout = jiffies + HZ;
621         while (1) {
622                 u16 scratch;
623
624                 scratch = if_sdio_read_scratch(card, &ret);
625                 if (ret)
626                         goto release;
627
628                 if (scratch == IF_SDIO_FIRMWARE_OK)
629                         break;
630
631                 if (time_after(jiffies, timeout)) {
632                         ret = -ETIMEDOUT;
633                         goto release;
634                 }
635
636                 msleep(10);
637         }
638
639         ret = 0;
640
641 release:
642         sdio_set_block_size(card->func, 0);
643         sdio_release_host(card->func);
644         kfree(chunk_buffer);
645 release_fw:
646         release_firmware(fw);
647
648 out:
649         if (ret)
650                 lbs_pr_err("failed to load firmware\n");
651
652         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
653
654         return ret;
655 }
656
657 static int if_sdio_prog_firmware(struct if_sdio_card *card)
658 {
659         int ret;
660         u16 scratch;
661
662         lbs_deb_enter(LBS_DEB_SDIO);
663
664         sdio_claim_host(card->func);
665         scratch = if_sdio_read_scratch(card, &ret);
666         sdio_release_host(card->func);
667
668         if (ret)
669                 goto out;
670
671         if (scratch == IF_SDIO_FIRMWARE_OK) {
672                 lbs_deb_sdio("firmware already loaded\n");
673                 goto success;
674         }
675
676         ret = if_sdio_prog_helper(card);
677         if (ret)
678                 goto out;
679
680         ret = if_sdio_prog_real(card);
681         if (ret)
682                 goto out;
683
684 success:
685         ret = 0;
686
687 out:
688         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
689
690         return ret;
691 }
692
693 /*******************************************************************/
694 /* Libertas callbacks                                              */
695 /*******************************************************************/
696
697 static int if_sdio_host_to_card(struct lbs_private *priv,
698                 u8 type, u8 *buf, u16 nb)
699 {
700         int ret;
701         struct if_sdio_card *card;
702         struct if_sdio_packet *packet, *cur;
703         u16 size;
704         unsigned long flags;
705
706         lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
707
708         card = priv->card;
709
710         if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
711                 ret = -EINVAL;
712                 goto out;
713         }
714
715         /*
716          * The transfer must be in one transaction or the firmware
717          * goes suicidal.
718          */
719         size = nb + 4;
720         if ((size > card->func->cur_blksize) || (size > 512)) {
721                 size = (size + card->func->cur_blksize - 1) /
722                         card->func->cur_blksize * card->func->cur_blksize;
723         }
724
725         packet = kzalloc(sizeof(struct if_sdio_packet) + size,
726                         GFP_ATOMIC);
727         if (!packet) {
728                 ret = -ENOMEM;
729                 goto out;
730         }
731
732         packet->next = NULL;
733         packet->nb = size;
734
735         /*
736          * SDIO specific header.
737          */
738         packet->buffer[0] = (nb + 4) & 0xff;
739         packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
740         packet->buffer[2] = type;
741         packet->buffer[3] = 0;
742
743         memcpy(packet->buffer + 4, buf, nb);
744
745         spin_lock_irqsave(&card->lock, flags);
746
747         if (!card->packets)
748                 card->packets = packet;
749         else {
750                 cur = card->packets;
751                 while (cur->next)
752                         cur = cur->next;
753                 cur->next = packet;
754         }
755
756         switch (type) {
757         case MVMS_CMD:
758                 priv->dnld_sent = DNLD_CMD_SENT;
759                 break;
760         case MVMS_DAT:
761                 priv->dnld_sent = DNLD_DATA_SENT;
762                 break;
763         default:
764                 lbs_deb_sdio("unknown packet type %d\n", (int)type);
765         }
766
767         spin_unlock_irqrestore(&card->lock, flags);
768
769         schedule_work(&card->packet_worker);
770
771         ret = 0;
772
773 out:
774         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
775
776         return ret;
777 }
778
779 static int if_sdio_get_int_status(struct lbs_private *priv, u8 *ireg)
780 {
781         struct if_sdio_card *card;
782
783         lbs_deb_enter(LBS_DEB_SDIO);
784
785         card = priv->card;
786
787         *ireg = card->int_cause;
788         card->int_cause = 0;
789
790         lbs_deb_leave(LBS_DEB_SDIO);
791
792         return 0;
793 }
794
795 static int if_sdio_read_event_cause(struct lbs_private *priv)
796 {
797         struct if_sdio_card *card;
798
799         lbs_deb_enter(LBS_DEB_SDIO);
800
801         card = priv->card;
802
803         priv->adapter->eventcause = card->event;
804
805         lbs_deb_leave(LBS_DEB_SDIO);
806
807         return 0;
808 }
809
810 /*******************************************************************/
811 /* SDIO callbacks                                                  */
812 /*******************************************************************/
813
814 static void if_sdio_interrupt(struct sdio_func *func)
815 {
816         int ret;
817         struct if_sdio_card *card;
818         u8 cause;
819
820         lbs_deb_enter(LBS_DEB_SDIO);
821
822         card = sdio_get_drvdata(func);
823
824         cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
825         if (ret)
826                 goto out;
827
828         lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
829
830         sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
831         if (ret)
832                 goto out;
833
834         /*
835          * Ignore the define name, this really means the card has
836          * successfully received the command.
837          */
838         if (cause & IF_SDIO_H_INT_DNLD)
839                 lbs_host_to_card_done(card->priv);
840
841
842         if (cause & IF_SDIO_H_INT_UPLD) {
843                 ret = if_sdio_card_to_host(card);
844                 if (ret)
845                         goto out;
846         }
847
848         ret = 0;
849
850 out:
851         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
852 }
853
854 static int if_sdio_probe(struct sdio_func *func,
855                 const struct sdio_device_id *id)
856 {
857         struct if_sdio_card *card;
858         struct lbs_private *priv;
859         int ret, i;
860         unsigned int model;
861         struct if_sdio_packet *packet;
862
863         lbs_deb_enter(LBS_DEB_SDIO);
864
865         for (i = 0;i < func->card->num_info;i++) {
866                 if (sscanf(func->card->info[i],
867                                 "802.11 SDIO ID: %x", &model) == 1)
868                         break;
869                 if (sscanf(func->card->info[i],
870                                 "ID: %x", &model) == 1)
871                         break;
872                if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
873                        model = 4;
874                        break;
875                }
876         }
877
878         if (i == func->card->num_info) {
879                 lbs_pr_err("unable to identify card model\n");
880                 return -ENODEV;
881         }
882
883         card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
884         if (!card)
885                 return -ENOMEM;
886
887         card->func = func;
888         card->model = model;
889         spin_lock_init(&card->lock);
890         INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
891
892         for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) {
893                 if (card->model == if_sdio_models[i].model)
894                         break;
895         }
896
897         if (i == ARRAY_SIZE(if_sdio_models)) {
898                 lbs_pr_err("unkown card model 0x%x\n", card->model);
899                 ret = -ENODEV;
900                 goto free;
901         }
902
903         card->helper = if_sdio_models[i].helper;
904         card->firmware = if_sdio_models[i].firmware;
905
906         if (lbs_helper_name) {
907                 lbs_deb_sdio("overriding helper firmware: %s\n",
908                         lbs_helper_name);
909                 card->helper = lbs_helper_name;
910         }
911
912         if (lbs_fw_name) {
913                 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name);
914                 card->firmware = lbs_fw_name;
915         }
916
917         sdio_claim_host(func);
918
919         ret = sdio_enable_func(func);
920         if (ret)
921                 goto release;
922
923         ret = sdio_claim_irq(func, if_sdio_interrupt);
924         if (ret)
925                 goto disable;
926
927         card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
928         if (ret)
929                 goto release_int;
930
931         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
932         if (ret)
933                 goto release_int;
934
935         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
936         if (ret)
937                 goto release_int;
938
939         sdio_release_host(func);
940
941         sdio_set_drvdata(func, card);
942
943         lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
944                         "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
945                         func->class, func->vendor, func->device,
946                         model, (unsigned)card->ioport);
947
948         ret = if_sdio_prog_firmware(card);
949         if (ret)
950                 goto reclaim;
951
952         priv = lbs_add_card(card, &func->dev);
953         if (!priv) {
954                 ret = -ENOMEM;
955                 goto reclaim;
956         }
957
958         card->priv = priv;
959
960         priv->card = card;
961         priv->hw_host_to_card = if_sdio_host_to_card;
962         priv->hw_get_int_status = if_sdio_get_int_status;
963         priv->hw_read_event_cause = if_sdio_read_event_cause;
964
965         priv->adapter->fw_ready = 1;
966
967         /*
968          * Enable interrupts now that everything is set up
969          */
970         sdio_claim_host(func);
971         sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
972         sdio_release_host(func);
973         if (ret)
974                 goto reclaim;
975
976         ret = lbs_start_card(priv);
977         if (ret)
978                 goto err_activate_card;
979
980 out:
981         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
982
983         return ret;
984
985 err_activate_card:
986         flush_scheduled_work();
987         free_netdev(priv->dev);
988         kfree(priv->adapter);
989 reclaim:
990         sdio_claim_host(func);
991 release_int:
992         sdio_release_irq(func);
993 disable:
994         sdio_disable_func(func);
995 release:
996         sdio_release_host(func);
997 free:
998         while (card->packets) {
999                 packet = card->packets;
1000                 card->packets = card->packets->next;
1001                 kfree(packet);
1002         }
1003
1004         kfree(card);
1005
1006         goto out;
1007 }
1008
1009 static void if_sdio_remove(struct sdio_func *func)
1010 {
1011         struct if_sdio_card *card;
1012         struct if_sdio_packet *packet;
1013
1014         lbs_deb_enter(LBS_DEB_SDIO);
1015
1016         card = sdio_get_drvdata(func);
1017
1018         card->priv->adapter->surpriseremoved = 1;
1019
1020         lbs_deb_sdio("call remove card\n");
1021         lbs_stop_card(card->priv);
1022         lbs_remove_card(card->priv);
1023
1024         flush_scheduled_work();
1025
1026         sdio_claim_host(func);
1027         sdio_release_irq(func);
1028         sdio_disable_func(func);
1029         sdio_release_host(func);
1030
1031         while (card->packets) {
1032                 packet = card->packets;
1033                 card->packets = card->packets->next;
1034                 kfree(packet);
1035         }
1036
1037         kfree(card);
1038
1039         lbs_deb_leave(LBS_DEB_SDIO);
1040 }
1041
1042 static struct sdio_driver if_sdio_driver = {
1043         .name           = "libertas_sdio",
1044         .id_table       = if_sdio_ids,
1045         .probe          = if_sdio_probe,
1046         .remove         = if_sdio_remove,
1047 };
1048
1049 /*******************************************************************/
1050 /* Module functions                                                */
1051 /*******************************************************************/
1052
1053 static int __init if_sdio_init_module(void)
1054 {
1055         int ret = 0;
1056
1057         lbs_deb_enter(LBS_DEB_SDIO);
1058
1059         printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1060         printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1061
1062         ret = sdio_register_driver(&if_sdio_driver);
1063
1064         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1065
1066         return ret;
1067 }
1068
1069 static void __exit if_sdio_exit_module(void)
1070 {
1071         lbs_deb_enter(LBS_DEB_SDIO);
1072
1073         sdio_unregister_driver(&if_sdio_driver);
1074
1075         lbs_deb_leave(LBS_DEB_SDIO);
1076 }
1077
1078 module_init(if_sdio_init_module);
1079 module_exit(if_sdio_exit_module);
1080
1081 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1082 MODULE_AUTHOR("Pierre Ossman");
1083 MODULE_LICENSE("GPL");