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