Merge branch 'for-rmk-fixes' of git://aeryn.fluff.org.uk/bjdooks/linux
[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/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. There's no way to guarantee that for all
270          * controllers, but we can at least try.
271          */
272         chunk = sdio_align_size(card->func, size);
273
274         ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
275         if (ret)
276                 goto out;
277
278         chunk = card->buffer[0] | (card->buffer[1] << 8);
279         type = card->buffer[2] | (card->buffer[3] << 8);
280
281         lbs_deb_sdio("packet of type %d and size %d bytes\n",
282                 (int)type, (int)chunk);
283
284         if (chunk > size) {
285                 lbs_deb_sdio("packet fragment (%d > %d)\n",
286                         (int)chunk, (int)size);
287                 ret = -EINVAL;
288                 goto out;
289         }
290
291         if (chunk < size) {
292                 lbs_deb_sdio("packet fragment (%d < %d)\n",
293                         (int)chunk, (int)size);
294         }
295
296         switch (type) {
297         case MVMS_CMD:
298                 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
299                 if (ret)
300                         goto out;
301                 break;
302         case MVMS_DAT:
303                 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
304                 if (ret)
305                         goto out;
306                 break;
307         case MVMS_EVENT:
308                 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
309                 if (ret)
310                         goto out;
311                 break;
312         default:
313                 lbs_deb_sdio("invalid type (%d) from firmware\n",
314                                 (int)type);
315                 ret = -EINVAL;
316                 goto out;
317         }
318
319 out:
320         if (ret)
321                 lbs_pr_err("problem fetching packet from firmware\n");
322
323         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
324
325         return ret;
326 }
327
328 static void if_sdio_host_to_card_worker(struct work_struct *work)
329 {
330         struct if_sdio_card *card;
331         struct if_sdio_packet *packet;
332         unsigned long timeout;
333         u8 status;
334         int ret;
335         unsigned long flags;
336
337         lbs_deb_enter(LBS_DEB_SDIO);
338
339         card = container_of(work, struct if_sdio_card, packet_worker);
340
341         while (1) {
342                 spin_lock_irqsave(&card->lock, flags);
343                 packet = card->packets;
344                 if (packet)
345                         card->packets = packet->next;
346                 spin_unlock_irqrestore(&card->lock, flags);
347
348                 if (!packet)
349                         break;
350
351                 sdio_claim_host(card->func);
352
353                 timeout = jiffies + HZ;
354                 while (1) {
355                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
356                         if (ret)
357                                 goto release;
358                         if (status & IF_SDIO_IO_RDY)
359                                 break;
360                         if (time_after(jiffies, timeout)) {
361                                 ret = -ETIMEDOUT;
362                                 goto release;
363                         }
364                         mdelay(1);
365                 }
366
367                 ret = sdio_writesb(card->func, card->ioport,
368                                 packet->buffer, packet->nb);
369                 if (ret)
370                         goto release;
371 release:
372                 sdio_release_host(card->func);
373
374                 kfree(packet);
375         }
376
377         lbs_deb_leave(LBS_DEB_SDIO);
378 }
379
380 /********************************************************************/
381 /* Firmware                                                         */
382 /********************************************************************/
383
384 static int if_sdio_prog_helper(struct if_sdio_card *card)
385 {
386         int ret;
387         u8 status;
388         const struct firmware *fw;
389         unsigned long timeout;
390         u8 *chunk_buffer;
391         u32 chunk_size;
392         const u8 *firmware;
393         size_t size;
394
395         lbs_deb_enter(LBS_DEB_SDIO);
396
397         ret = request_firmware(&fw, card->helper, &card->func->dev);
398         if (ret) {
399                 lbs_pr_err("can't load helper firmware\n");
400                 goto out;
401         }
402
403         chunk_buffer = kzalloc(64, GFP_KERNEL);
404         if (!chunk_buffer) {
405                 ret = -ENOMEM;
406                 goto release_fw;
407         }
408
409         sdio_claim_host(card->func);
410
411         ret = sdio_set_block_size(card->func, 32);
412         if (ret)
413                 goto release;
414
415         firmware = fw->data;
416         size = fw->size;
417
418         while (size) {
419                 timeout = jiffies + HZ;
420                 while (1) {
421                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
422                         if (ret)
423                                 goto release;
424                         if ((status & IF_SDIO_IO_RDY) &&
425                                         (status & IF_SDIO_DL_RDY))
426                                 break;
427                         if (time_after(jiffies, timeout)) {
428                                 ret = -ETIMEDOUT;
429                                 goto release;
430                         }
431                         mdelay(1);
432                 }
433
434                 chunk_size = min(size, (size_t)60);
435
436                 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
437                 memcpy(chunk_buffer + 4, firmware, chunk_size);
438 /*
439                 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
440 */
441                 ret = sdio_writesb(card->func, card->ioport,
442                                 chunk_buffer, 64);
443                 if (ret)
444                         goto release;
445
446                 firmware += chunk_size;
447                 size -= chunk_size;
448         }
449
450         /* an empty block marks the end of the transfer */
451         memset(chunk_buffer, 0, 4);
452         ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
453         if (ret)
454                 goto release;
455
456         lbs_deb_sdio("waiting for helper to boot...\n");
457
458         /* wait for the helper to boot by looking at the size register */
459         timeout = jiffies + HZ;
460         while (1) {
461                 u16 req_size;
462
463                 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
464                 if (ret)
465                         goto release;
466
467                 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
468                 if (ret)
469                         goto release;
470
471                 if (req_size != 0)
472                         break;
473
474                 if (time_after(jiffies, timeout)) {
475                         ret = -ETIMEDOUT;
476                         goto release;
477                 }
478
479                 msleep(10);
480         }
481
482         ret = 0;
483
484 release:
485         sdio_set_block_size(card->func, 0);
486         sdio_release_host(card->func);
487         kfree(chunk_buffer);
488 release_fw:
489         release_firmware(fw);
490
491 out:
492         if (ret)
493                 lbs_pr_err("failed to load helper firmware\n");
494
495         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
496
497         return ret;
498 }
499
500 static int if_sdio_prog_real(struct if_sdio_card *card)
501 {
502         int ret;
503         u8 status;
504         const struct firmware *fw;
505         unsigned long timeout;
506         u8 *chunk_buffer;
507         u32 chunk_size;
508         const u8 *firmware;
509         size_t size, req_size;
510
511         lbs_deb_enter(LBS_DEB_SDIO);
512
513         ret = request_firmware(&fw, card->firmware, &card->func->dev);
514         if (ret) {
515                 lbs_pr_err("can't load firmware\n");
516                 goto out;
517         }
518
519         chunk_buffer = kzalloc(512, GFP_KERNEL);
520         if (!chunk_buffer) {
521                 ret = -ENOMEM;
522                 goto release_fw;
523         }
524
525         sdio_claim_host(card->func);
526
527         ret = sdio_set_block_size(card->func, 32);
528         if (ret)
529                 goto release;
530
531         firmware = fw->data;
532         size = fw->size;
533
534         while (size) {
535                 timeout = jiffies + HZ;
536                 while (1) {
537                         status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
538                         if (ret)
539                                 goto release;
540                         if ((status & IF_SDIO_IO_RDY) &&
541                                         (status & IF_SDIO_DL_RDY))
542                                 break;
543                         if (time_after(jiffies, timeout)) {
544                                 ret = -ETIMEDOUT;
545                                 goto release;
546                         }
547                         mdelay(1);
548                 }
549
550                 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
551                 if (ret)
552                         goto release;
553
554                 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
555                 if (ret)
556                         goto release;
557 /*
558                 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
559 */
560                 if (req_size == 0) {
561                         lbs_deb_sdio("firmware helper gave up early\n");
562                         ret = -EIO;
563                         goto release;
564                 }
565
566                 if (req_size & 0x01) {
567                         lbs_deb_sdio("firmware helper signalled error\n");
568                         ret = -EIO;
569                         goto release;
570                 }
571
572                 if (req_size > size)
573                         req_size = size;
574
575                 while (req_size) {
576                         chunk_size = min(req_size, (size_t)512);
577
578                         memcpy(chunk_buffer, firmware, chunk_size);
579 /*
580                         lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
581                                 chunk_size, (chunk_size + 31) / 32 * 32);
582 */
583                         ret = sdio_writesb(card->func, card->ioport,
584                                 chunk_buffer, (chunk_size + 31) / 32 * 32);
585                         if (ret)
586                                 goto release;
587
588                         firmware += chunk_size;
589                         size -= chunk_size;
590                         req_size -= chunk_size;
591                 }
592         }
593
594         ret = 0;
595
596         lbs_deb_sdio("waiting for firmware to boot...\n");
597
598         /* wait for the firmware to boot */
599         timeout = jiffies + HZ;
600         while (1) {
601                 u16 scratch;
602
603                 scratch = if_sdio_read_scratch(card, &ret);
604                 if (ret)
605                         goto release;
606
607                 if (scratch == IF_SDIO_FIRMWARE_OK)
608                         break;
609
610                 if (time_after(jiffies, timeout)) {
611                         ret = -ETIMEDOUT;
612                         goto release;
613                 }
614
615                 msleep(10);
616         }
617
618         ret = 0;
619
620 release:
621         sdio_set_block_size(card->func, 0);
622         sdio_release_host(card->func);
623         kfree(chunk_buffer);
624 release_fw:
625         release_firmware(fw);
626
627 out:
628         if (ret)
629                 lbs_pr_err("failed to load firmware\n");
630
631         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
632
633         return ret;
634 }
635
636 static int if_sdio_prog_firmware(struct if_sdio_card *card)
637 {
638         int ret;
639         u16 scratch;
640
641         lbs_deb_enter(LBS_DEB_SDIO);
642
643         sdio_claim_host(card->func);
644         scratch = if_sdio_read_scratch(card, &ret);
645         sdio_release_host(card->func);
646
647         if (ret)
648                 goto out;
649
650         if (scratch == IF_SDIO_FIRMWARE_OK) {
651                 lbs_deb_sdio("firmware already loaded\n");
652                 goto success;
653         }
654
655         ret = if_sdio_prog_helper(card);
656         if (ret)
657                 goto out;
658
659         ret = if_sdio_prog_real(card);
660         if (ret)
661                 goto out;
662
663 success:
664         ret = 0;
665
666 out:
667         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
668
669         return ret;
670 }
671
672 /*******************************************************************/
673 /* Libertas callbacks                                              */
674 /*******************************************************************/
675
676 static int if_sdio_host_to_card(struct lbs_private *priv,
677                 u8 type, u8 *buf, u16 nb)
678 {
679         int ret;
680         struct if_sdio_card *card;
681         struct if_sdio_packet *packet, *cur;
682         u16 size;
683         unsigned long flags;
684
685         lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
686
687         card = priv->card;
688
689         if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
690                 ret = -EINVAL;
691                 goto out;
692         }
693
694         /*
695          * The transfer must be in one transaction or the firmware
696          * goes suicidal. There's no way to guarantee that for all
697          * controllers, but we can at least try.
698          */
699         size = sdio_align_size(card->func, nb + 4);
700
701         packet = kzalloc(sizeof(struct if_sdio_packet) + size,
702                         GFP_ATOMIC);
703         if (!packet) {
704                 ret = -ENOMEM;
705                 goto out;
706         }
707
708         packet->next = NULL;
709         packet->nb = size;
710
711         /*
712          * SDIO specific header.
713          */
714         packet->buffer[0] = (nb + 4) & 0xff;
715         packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
716         packet->buffer[2] = type;
717         packet->buffer[3] = 0;
718
719         memcpy(packet->buffer + 4, buf, nb);
720
721         spin_lock_irqsave(&card->lock, flags);
722
723         if (!card->packets)
724                 card->packets = packet;
725         else {
726                 cur = card->packets;
727                 while (cur->next)
728                         cur = cur->next;
729                 cur->next = packet;
730         }
731
732         switch (type) {
733         case MVMS_CMD:
734                 priv->dnld_sent = DNLD_CMD_SENT;
735                 break;
736         case MVMS_DAT:
737                 priv->dnld_sent = DNLD_DATA_SENT;
738                 break;
739         default:
740                 lbs_deb_sdio("unknown packet type %d\n", (int)type);
741         }
742
743         spin_unlock_irqrestore(&card->lock, flags);
744
745         schedule_work(&card->packet_worker);
746
747         ret = 0;
748
749 out:
750         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
751
752         return ret;
753 }
754
755 /*******************************************************************/
756 /* SDIO callbacks                                                  */
757 /*******************************************************************/
758
759 static void if_sdio_interrupt(struct sdio_func *func)
760 {
761         int ret;
762         struct if_sdio_card *card;
763         u8 cause;
764
765         lbs_deb_enter(LBS_DEB_SDIO);
766
767         card = sdio_get_drvdata(func);
768
769         cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
770         if (ret)
771                 goto out;
772
773         lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
774
775         sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
776         if (ret)
777                 goto out;
778
779         /*
780          * Ignore the define name, this really means the card has
781          * successfully received the command.
782          */
783         if (cause & IF_SDIO_H_INT_DNLD)
784                 lbs_host_to_card_done(card->priv);
785
786
787         if (cause & IF_SDIO_H_INT_UPLD) {
788                 ret = if_sdio_card_to_host(card);
789                 if (ret)
790                         goto out;
791         }
792
793         ret = 0;
794
795 out:
796         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
797 }
798
799 static int if_sdio_probe(struct sdio_func *func,
800                 const struct sdio_device_id *id)
801 {
802         struct if_sdio_card *card;
803         struct lbs_private *priv;
804         int ret, i;
805         unsigned int model;
806         struct if_sdio_packet *packet;
807
808         lbs_deb_enter(LBS_DEB_SDIO);
809
810         for (i = 0;i < func->card->num_info;i++) {
811                 if (sscanf(func->card->info[i],
812                                 "802.11 SDIO ID: %x", &model) == 1)
813                         break;
814                 if (sscanf(func->card->info[i],
815                                 "ID: %x", &model) == 1)
816                         break;
817                if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
818                        model = 4;
819                        break;
820                }
821         }
822
823         if (i == func->card->num_info) {
824                 lbs_pr_err("unable to identify card model\n");
825                 return -ENODEV;
826         }
827
828         card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
829         if (!card)
830                 return -ENOMEM;
831
832         card->func = func;
833         card->model = model;
834         spin_lock_init(&card->lock);
835         INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
836
837         for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) {
838                 if (card->model == if_sdio_models[i].model)
839                         break;
840         }
841
842         if (i == ARRAY_SIZE(if_sdio_models)) {
843                 lbs_pr_err("unkown card model 0x%x\n", card->model);
844                 ret = -ENODEV;
845                 goto free;
846         }
847
848         card->helper = if_sdio_models[i].helper;
849         card->firmware = if_sdio_models[i].firmware;
850
851         if (lbs_helper_name) {
852                 lbs_deb_sdio("overriding helper firmware: %s\n",
853                         lbs_helper_name);
854                 card->helper = lbs_helper_name;
855         }
856
857         if (lbs_fw_name) {
858                 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name);
859                 card->firmware = lbs_fw_name;
860         }
861
862         sdio_claim_host(func);
863
864         ret = sdio_enable_func(func);
865         if (ret)
866                 goto release;
867
868         ret = sdio_claim_irq(func, if_sdio_interrupt);
869         if (ret)
870                 goto disable;
871
872         card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
873         if (ret)
874                 goto release_int;
875
876         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
877         if (ret)
878                 goto release_int;
879
880         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
881         if (ret)
882                 goto release_int;
883
884         sdio_release_host(func);
885
886         sdio_set_drvdata(func, card);
887
888         lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
889                         "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
890                         func->class, func->vendor, func->device,
891                         model, (unsigned)card->ioport);
892
893         ret = if_sdio_prog_firmware(card);
894         if (ret)
895                 goto reclaim;
896
897         priv = lbs_add_card(card, &func->dev);
898         if (!priv) {
899                 ret = -ENOMEM;
900                 goto reclaim;
901         }
902
903         card->priv = priv;
904
905         priv->card = card;
906         priv->hw_host_to_card = if_sdio_host_to_card;
907
908         priv->fw_ready = 1;
909
910         /*
911          * Enable interrupts now that everything is set up
912          */
913         sdio_claim_host(func);
914         sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
915         sdio_release_host(func);
916         if (ret)
917                 goto reclaim;
918
919         ret = lbs_start_card(priv);
920         if (ret)
921                 goto err_activate_card;
922
923 out:
924         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
925
926         return ret;
927
928 err_activate_card:
929         flush_scheduled_work();
930         free_netdev(priv->dev);
931         kfree(priv);
932 reclaim:
933         sdio_claim_host(func);
934 release_int:
935         sdio_release_irq(func);
936 disable:
937         sdio_disable_func(func);
938 release:
939         sdio_release_host(func);
940 free:
941         while (card->packets) {
942                 packet = card->packets;
943                 card->packets = card->packets->next;
944                 kfree(packet);
945         }
946
947         kfree(card);
948
949         goto out;
950 }
951
952 static void if_sdio_remove(struct sdio_func *func)
953 {
954         struct if_sdio_card *card;
955         struct if_sdio_packet *packet;
956
957         lbs_deb_enter(LBS_DEB_SDIO);
958
959         card = sdio_get_drvdata(func);
960
961         card->priv->surpriseremoved = 1;
962
963         lbs_deb_sdio("call remove card\n");
964         lbs_stop_card(card->priv);
965         lbs_remove_card(card->priv);
966
967         flush_scheduled_work();
968
969         sdio_claim_host(func);
970         sdio_release_irq(func);
971         sdio_disable_func(func);
972         sdio_release_host(func);
973
974         while (card->packets) {
975                 packet = card->packets;
976                 card->packets = card->packets->next;
977                 kfree(packet);
978         }
979
980         kfree(card);
981
982         lbs_deb_leave(LBS_DEB_SDIO);
983 }
984
985 static struct sdio_driver if_sdio_driver = {
986         .name           = "libertas_sdio",
987         .id_table       = if_sdio_ids,
988         .probe          = if_sdio_probe,
989         .remove         = if_sdio_remove,
990 };
991
992 /*******************************************************************/
993 /* Module functions                                                */
994 /*******************************************************************/
995
996 static int __init if_sdio_init_module(void)
997 {
998         int ret = 0;
999
1000         lbs_deb_enter(LBS_DEB_SDIO);
1001
1002         printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1003         printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1004
1005         ret = sdio_register_driver(&if_sdio_driver);
1006
1007         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1008
1009         return ret;
1010 }
1011
1012 static void __exit if_sdio_exit_module(void)
1013 {
1014         lbs_deb_enter(LBS_DEB_SDIO);
1015
1016         sdio_unregister_driver(&if_sdio_driver);
1017
1018         lbs_deb_leave(LBS_DEB_SDIO);
1019 }
1020
1021 module_init(if_sdio_init_module);
1022 module_exit(if_sdio_exit_module);
1023
1024 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1025 MODULE_AUTHOR("Pierre Ossman");
1026 MODULE_LICENSE("GPL");