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