Merge branch 'powerpc-next' of master.kernel.org:/pub/scm/linux/kernel/git/galak...
[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
95         spinlock_t              lock;
96         struct if_sdio_packet   *packets;
97         struct work_struct      packet_worker;
98 };
99
100 /********************************************************************/
101 /* I/O                                                              */
102 /********************************************************************/
103
104 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
105 {
106         int ret, reg;
107         u16 scratch;
108
109         if (card->model == 0x04)
110                 reg = IF_SDIO_SCRATCH_OLD;
111         else
112                 reg = IF_SDIO_SCRATCH;
113
114         scratch = sdio_readb(card->func, reg, &ret);
115         if (!ret)
116                 scratch |= sdio_readb(card->func, reg + 1, &ret) << 8;
117
118         if (err)
119                 *err = ret;
120
121         if (ret)
122                 return 0xffff;
123
124         return scratch;
125 }
126
127 static int if_sdio_handle_cmd(struct if_sdio_card *card,
128                 u8 *buffer, unsigned size)
129 {
130         struct lbs_private *priv = card->priv;
131         int ret;
132         unsigned long flags;
133         u8 i;
134
135         lbs_deb_enter(LBS_DEB_SDIO);
136
137         if (size > LBS_CMD_BUFFER_SIZE) {
138                 lbs_deb_sdio("response packet too large (%d bytes)\n",
139                         (int)size);
140                 ret = -E2BIG;
141                 goto out;
142         }
143
144         spin_lock_irqsave(&priv->driver_lock, flags);
145
146         i = (priv->resp_idx == 0) ? 1 : 0;
147         BUG_ON(priv->resp_len[i]);
148         priv->resp_len[i] = size;
149         memcpy(priv->resp_buf[i], buffer, size);
150         lbs_notify_command_response(priv, i);
151
152         spin_unlock_irqrestore(&card->priv->driver_lock, flags);
153
154         ret = 0;
155
156 out:
157         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
158         return ret;
159 }
160
161 static int if_sdio_handle_data(struct if_sdio_card *card,
162                 u8 *buffer, unsigned size)
163 {
164         int ret;
165         struct sk_buff *skb;
166         char *data;
167
168         lbs_deb_enter(LBS_DEB_SDIO);
169
170         if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
171                 lbs_deb_sdio("response packet too large (%d bytes)\n",
172                         (int)size);
173                 ret = -E2BIG;
174                 goto out;
175         }
176
177         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
178         if (!skb) {
179                 ret = -ENOMEM;
180                 goto out;
181         }
182
183         skb_reserve(skb, NET_IP_ALIGN);
184
185         data = skb_put(skb, size);
186
187         memcpy(data, buffer, size);
188
189         lbs_process_rxed_packet(card->priv, skb);
190
191         ret = 0;
192
193 out:
194         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
195
196         return ret;
197 }
198
199 static int if_sdio_handle_event(struct if_sdio_card *card,
200                 u8 *buffer, unsigned size)
201 {
202         int ret;
203         u32 event;
204
205         lbs_deb_enter(LBS_DEB_SDIO);
206
207         if (card->model == 0x04) {
208                 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
209                 if (ret)
210                         goto out;
211         } else {
212                 if (size < 4) {
213                         lbs_deb_sdio("event packet too small (%d bytes)\n",
214                                 (int)size);
215                         ret = -EINVAL;
216                         goto out;
217                 }
218                 event = buffer[3] << 24;
219                 event |= buffer[2] << 16;
220                 event |= buffer[1] << 8;
221                 event |= buffer[0] << 0;
222         }
223
224         lbs_queue_event(card->priv, event & 0xFF);
225         ret = 0;
226
227 out:
228         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
229
230         return ret;
231 }
232
233 static int if_sdio_card_to_host(struct if_sdio_card *card)
234 {
235         int ret;
236         u8 status;
237         u16 size, type, chunk;
238         unsigned long timeout;
239
240         lbs_deb_enter(LBS_DEB_SDIO);
241
242         size = if_sdio_read_scratch(card, &ret);
243         if (ret)
244                 goto out;
245
246         if (size < 4) {
247                 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
248                         (int)size);
249                 ret = -EINVAL;
250                 goto out;
251         }
252
253         timeout = jiffies + HZ;
254         while (1) {
255                 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
256                 if (ret)
257                         goto out;
258                 if (status & IF_SDIO_IO_RDY)
259                         break;
260                 if (time_after(jiffies, timeout)) {
261                         ret = -ETIMEDOUT;
262                         goto out;
263                 }
264                 mdelay(1);
265         }
266
267         /*
268          * The transfer must be in one transaction or the firmware
269          * goes suicidal.
270          */
271         chunk = size;
272         if ((chunk > card->func->cur_blksize) || (chunk > 512)) {
273                 chunk = (chunk + card->func->cur_blksize - 1) /
274                         card->func->cur_blksize * card->func->cur_blksize;
275         }
276
277         ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
278         if (ret)
279                 goto out;
280
281         chunk = card->buffer[0] | (card->buffer[1] << 8);
282         type = card->buffer[2] | (card->buffer[3] << 8);
283
284         lbs_deb_sdio("packet of type %d and size %d bytes\n",
285                 (int)type, (int)chunk);
286
287         if (chunk > size) {
288                 lbs_deb_sdio("packet fragment (%d > %d)\n",
289                         (int)chunk, (int)size);
290                 ret = -EINVAL;
291                 goto out;
292         }
293
294         if (chunk < size) {
295                 lbs_deb_sdio("packet fragment (%d < %d)\n",
296                         (int)chunk, (int)size);
297         }
298
299         switch (type) {
300         case MVMS_CMD:
301                 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
302                 if (ret)
303                         goto out;
304                 break;
305         case MVMS_DAT:
306                 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
307                 if (ret)
308                         goto out;
309                 break;
310         case MVMS_EVENT:
311                 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
312                 if (ret)
313                         goto out;
314                 break;
315         default:
316                 lbs_deb_sdio("invalid type (%d) from firmware\n",
317                                 (int)type);
318                 ret = -EINVAL;
319                 goto out;
320         }
321
322 out:
323         if (ret)
324                 lbs_pr_err("problem fetching packet from firmware\n");
325
326         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
327
328         return ret;
329 }
330
331 static void if_sdio_host_to_card_worker(struct work_struct *work)
332 {
333         struct if_sdio_card *card;
334         struct if_sdio_packet *packet;
335         unsigned long timeout;
336         u8 status;
337         int ret;
338         unsigned long flags;
339
340         lbs_deb_enter(LBS_DEB_SDIO);
341
342         card = container_of(work, struct if_sdio_card, packet_worker);
343
344         while (1) {
345                 spin_lock_irqsave(&card->lock, flags);
346                 packet = card->packets;
347                 if (packet)
348                         card->packets = packet->next;
349                 spin_unlock_irqrestore(&card->lock, flags);
350
351                 if (!packet)
352                         break;
353
354                 sdio_claim_host(card->func);
355
356                 timeout = jiffies + HZ;
357                 while (1) {
358                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
359                         if (ret)
360                                 goto release;
361                         if (status & IF_SDIO_IO_RDY)
362                                 break;
363                         if (time_after(jiffies, timeout)) {
364                                 ret = -ETIMEDOUT;
365                                 goto release;
366                         }
367                         mdelay(1);
368                 }
369
370                 ret = sdio_writesb(card->func, card->ioport,
371                                 packet->buffer, packet->nb);
372                 if (ret)
373                         goto release;
374 release:
375                 sdio_release_host(card->func);
376
377                 kfree(packet);
378         }
379
380         lbs_deb_leave(LBS_DEB_SDIO);
381 }
382
383 /********************************************************************/
384 /* Firmware                                                         */
385 /********************************************************************/
386
387 static int if_sdio_prog_helper(struct if_sdio_card *card)
388 {
389         int ret;
390         u8 status;
391         const struct firmware *fw;
392         unsigned long timeout;
393         u8 *chunk_buffer;
394         u32 chunk_size;
395         u8 *firmware;
396         size_t size;
397
398         lbs_deb_enter(LBS_DEB_SDIO);
399
400         ret = request_firmware(&fw, card->helper, &card->func->dev);
401         if (ret) {
402                 lbs_pr_err("can't load helper firmware\n");
403                 goto out;
404         }
405
406         chunk_buffer = kzalloc(64, GFP_KERNEL);
407         if (!chunk_buffer) {
408                 ret = -ENOMEM;
409                 goto release_fw;
410         }
411
412         sdio_claim_host(card->func);
413
414         ret = sdio_set_block_size(card->func, 32);
415         if (ret)
416                 goto release;
417
418         firmware = fw->data;
419         size = fw->size;
420
421         while (size) {
422                 timeout = jiffies + HZ;
423                 while (1) {
424                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
425                         if (ret)
426                                 goto release;
427                         if ((status & IF_SDIO_IO_RDY) &&
428                                         (status & IF_SDIO_DL_RDY))
429                                 break;
430                         if (time_after(jiffies, timeout)) {
431                                 ret = -ETIMEDOUT;
432                                 goto release;
433                         }
434                         mdelay(1);
435                 }
436
437                 chunk_size = min(size, (size_t)60);
438
439                 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
440                 memcpy(chunk_buffer + 4, firmware, chunk_size);
441 /*
442                 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
443 */
444                 ret = sdio_writesb(card->func, card->ioport,
445                                 chunk_buffer, 64);
446                 if (ret)
447                         goto release;
448
449                 firmware += chunk_size;
450                 size -= chunk_size;
451         }
452
453         /* an empty block marks the end of the transfer */
454         memset(chunk_buffer, 0, 4);
455         ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
456         if (ret)
457                 goto release;
458
459         lbs_deb_sdio("waiting for helper to boot...\n");
460
461         /* wait for the helper to boot by looking at the size register */
462         timeout = jiffies + HZ;
463         while (1) {
464                 u16 req_size;
465
466                 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
467                 if (ret)
468                         goto release;
469
470                 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
471                 if (ret)
472                         goto release;
473
474                 if (req_size != 0)
475                         break;
476
477                 if (time_after(jiffies, timeout)) {
478                         ret = -ETIMEDOUT;
479                         goto release;
480                 }
481
482                 msleep(10);
483         }
484
485         ret = 0;
486
487 release:
488         sdio_set_block_size(card->func, 0);
489         sdio_release_host(card->func);
490         kfree(chunk_buffer);
491 release_fw:
492         release_firmware(fw);
493
494 out:
495         if (ret)
496                 lbs_pr_err("failed to load helper firmware\n");
497
498         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
499
500         return ret;
501 }
502
503 static int if_sdio_prog_real(struct if_sdio_card *card)
504 {
505         int ret;
506         u8 status;
507         const struct firmware *fw;
508         unsigned long timeout;
509         u8 *chunk_buffer;
510         u32 chunk_size;
511         u8 *firmware;
512         size_t size, req_size;
513
514         lbs_deb_enter(LBS_DEB_SDIO);
515
516         ret = request_firmware(&fw, card->firmware, &card->func->dev);
517         if (ret) {
518                 lbs_pr_err("can't load firmware\n");
519                 goto out;
520         }
521
522         chunk_buffer = kzalloc(512, GFP_KERNEL);
523         if (!chunk_buffer) {
524                 ret = -ENOMEM;
525                 goto release_fw;
526         }
527
528         sdio_claim_host(card->func);
529
530         ret = sdio_set_block_size(card->func, 32);
531         if (ret)
532                 goto release;
533
534         firmware = fw->data;
535         size = fw->size;
536
537         while (size) {
538                 timeout = jiffies + HZ;
539                 while (1) {
540                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
541                         if (ret)
542                                 goto release;
543                         if ((status & IF_SDIO_IO_RDY) &&
544                                         (status & IF_SDIO_DL_RDY))
545                                 break;
546                         if (time_after(jiffies, timeout)) {
547                                 ret = -ETIMEDOUT;
548                                 goto release;
549                         }
550                         mdelay(1);
551                 }
552
553                 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
554                 if (ret)
555                         goto release;
556
557                 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
558                 if (ret)
559                         goto release;
560 /*
561                 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
562 */
563                 if (req_size == 0) {
564                         lbs_deb_sdio("firmware helper gave up early\n");
565                         ret = -EIO;
566                         goto release;
567                 }
568
569                 if (req_size & 0x01) {
570                         lbs_deb_sdio("firmware helper signalled error\n");
571                         ret = -EIO;
572                         goto release;
573                 }
574
575                 if (req_size > size)
576                         req_size = size;
577
578                 while (req_size) {
579                         chunk_size = min(req_size, (size_t)512);
580
581                         memcpy(chunk_buffer, firmware, chunk_size);
582 /*
583                         lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
584                                 chunk_size, (chunk_size + 31) / 32 * 32);
585 */
586                         ret = sdio_writesb(card->func, card->ioport,
587                                 chunk_buffer, (chunk_size + 31) / 32 * 32);
588                         if (ret)
589                                 goto release;
590
591                         firmware += chunk_size;
592                         size -= chunk_size;
593                         req_size -= chunk_size;
594                 }
595         }
596
597         ret = 0;
598
599         lbs_deb_sdio("waiting for firmware to boot...\n");
600
601         /* wait for the firmware to boot */
602         timeout = jiffies + HZ;
603         while (1) {
604                 u16 scratch;
605
606                 scratch = if_sdio_read_scratch(card, &ret);
607                 if (ret)
608                         goto release;
609
610                 if (scratch == IF_SDIO_FIRMWARE_OK)
611                         break;
612
613                 if (time_after(jiffies, timeout)) {
614                         ret = -ETIMEDOUT;
615                         goto release;
616                 }
617
618                 msleep(10);
619         }
620
621         ret = 0;
622
623 release:
624         sdio_set_block_size(card->func, 0);
625         sdio_release_host(card->func);
626         kfree(chunk_buffer);
627 release_fw:
628         release_firmware(fw);
629
630 out:
631         if (ret)
632                 lbs_pr_err("failed to load firmware\n");
633
634         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
635
636         return ret;
637 }
638
639 static int if_sdio_prog_firmware(struct if_sdio_card *card)
640 {
641         int ret;
642         u16 scratch;
643
644         lbs_deb_enter(LBS_DEB_SDIO);
645
646         sdio_claim_host(card->func);
647         scratch = if_sdio_read_scratch(card, &ret);
648         sdio_release_host(card->func);
649
650         if (ret)
651                 goto out;
652
653         if (scratch == IF_SDIO_FIRMWARE_OK) {
654                 lbs_deb_sdio("firmware already loaded\n");
655                 goto success;
656         }
657
658         ret = if_sdio_prog_helper(card);
659         if (ret)
660                 goto out;
661
662         ret = if_sdio_prog_real(card);
663         if (ret)
664                 goto out;
665
666 success:
667         ret = 0;
668
669 out:
670         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
671
672         return ret;
673 }
674
675 /*******************************************************************/
676 /* Libertas callbacks                                              */
677 /*******************************************************************/
678
679 static int if_sdio_host_to_card(struct lbs_private *priv,
680                 u8 type, u8 *buf, u16 nb)
681 {
682         int ret;
683         struct if_sdio_card *card;
684         struct if_sdio_packet *packet, *cur;
685         u16 size;
686         unsigned long flags;
687
688         lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
689
690         card = priv->card;
691
692         if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
693                 ret = -EINVAL;
694                 goto out;
695         }
696
697         /*
698          * The transfer must be in one transaction or the firmware
699          * goes suicidal.
700          */
701         size = nb + 4;
702         if ((size > card->func->cur_blksize) || (size > 512)) {
703                 size = (size + card->func->cur_blksize - 1) /
704                         card->func->cur_blksize * card->func->cur_blksize;
705         }
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         schedule_work(&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         INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
842
843         for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) {
844                 if (card->model == if_sdio_models[i].model)
845                         break;
846         }
847
848         if (i == ARRAY_SIZE(if_sdio_models)) {
849                 lbs_pr_err("unkown card model 0x%x\n", card->model);
850                 ret = -ENODEV;
851                 goto free;
852         }
853
854         card->helper = if_sdio_models[i].helper;
855         card->firmware = if_sdio_models[i].firmware;
856
857         if (lbs_helper_name) {
858                 lbs_deb_sdio("overriding helper firmware: %s\n",
859                         lbs_helper_name);
860                 card->helper = lbs_helper_name;
861         }
862
863         if (lbs_fw_name) {
864                 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name);
865                 card->firmware = lbs_fw_name;
866         }
867
868         sdio_claim_host(func);
869
870         ret = sdio_enable_func(func);
871         if (ret)
872                 goto release;
873
874         ret = sdio_claim_irq(func, if_sdio_interrupt);
875         if (ret)
876                 goto disable;
877
878         card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
879         if (ret)
880                 goto release_int;
881
882         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
883         if (ret)
884                 goto release_int;
885
886         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
887         if (ret)
888                 goto release_int;
889
890         sdio_release_host(func);
891
892         sdio_set_drvdata(func, card);
893
894         lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
895                         "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
896                         func->class, func->vendor, func->device,
897                         model, (unsigned)card->ioport);
898
899         ret = if_sdio_prog_firmware(card);
900         if (ret)
901                 goto reclaim;
902
903         priv = lbs_add_card(card, &func->dev);
904         if (!priv) {
905                 ret = -ENOMEM;
906                 goto reclaim;
907         }
908
909         card->priv = priv;
910
911         priv->card = card;
912         priv->hw_host_to_card = if_sdio_host_to_card;
913
914         priv->fw_ready = 1;
915
916         /*
917          * Enable interrupts now that everything is set up
918          */
919         sdio_claim_host(func);
920         sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
921         sdio_release_host(func);
922         if (ret)
923                 goto reclaim;
924
925         ret = lbs_start_card(priv);
926         if (ret)
927                 goto err_activate_card;
928
929 out:
930         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
931
932         return ret;
933
934 err_activate_card:
935         flush_scheduled_work();
936         free_netdev(priv->dev);
937         kfree(priv);
938 reclaim:
939         sdio_claim_host(func);
940 release_int:
941         sdio_release_irq(func);
942 disable:
943         sdio_disable_func(func);
944 release:
945         sdio_release_host(func);
946 free:
947         while (card->packets) {
948                 packet = card->packets;
949                 card->packets = card->packets->next;
950                 kfree(packet);
951         }
952
953         kfree(card);
954
955         goto out;
956 }
957
958 static void if_sdio_remove(struct sdio_func *func)
959 {
960         struct if_sdio_card *card;
961         struct if_sdio_packet *packet;
962
963         lbs_deb_enter(LBS_DEB_SDIO);
964
965         card = sdio_get_drvdata(func);
966
967         card->priv->surpriseremoved = 1;
968
969         lbs_deb_sdio("call remove card\n");
970         lbs_stop_card(card->priv);
971         lbs_remove_card(card->priv);
972
973         flush_scheduled_work();
974
975         sdio_claim_host(func);
976         sdio_release_irq(func);
977         sdio_disable_func(func);
978         sdio_release_host(func);
979
980         while (card->packets) {
981                 packet = card->packets;
982                 card->packets = card->packets->next;
983                 kfree(packet);
984         }
985
986         kfree(card);
987
988         lbs_deb_leave(LBS_DEB_SDIO);
989 }
990
991 static struct sdio_driver if_sdio_driver = {
992         .name           = "libertas_sdio",
993         .id_table       = if_sdio_ids,
994         .probe          = if_sdio_probe,
995         .remove         = if_sdio_remove,
996 };
997
998 /*******************************************************************/
999 /* Module functions                                                */
1000 /*******************************************************************/
1001
1002 static int __init if_sdio_init_module(void)
1003 {
1004         int ret = 0;
1005
1006         lbs_deb_enter(LBS_DEB_SDIO);
1007
1008         printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1009         printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1010
1011         ret = sdio_register_driver(&if_sdio_driver);
1012
1013         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1014
1015         return ret;
1016 }
1017
1018 static void __exit if_sdio_exit_module(void)
1019 {
1020         lbs_deb_enter(LBS_DEB_SDIO);
1021
1022         sdio_unregister_driver(&if_sdio_driver);
1023
1024         lbs_deb_leave(LBS_DEB_SDIO);
1025 }
1026
1027 module_init(if_sdio_init_module);
1028 module_exit(if_sdio_exit_module);
1029
1030 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1031 MODULE_AUTHOR("Pierre Ossman");
1032 MODULE_LICENSE("GPL");