libertas: Add auto deep sleep support for SD8385/SD8686/SD8688
[linux-2.6.git] / 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 static int if_sdio_enter_deep_sleep(struct lbs_private *priv)
835 {
836         int ret = -1;
837         struct cmd_header cmd;
838
839         memset(&cmd, 0, sizeof(cmd));
840
841         lbs_deb_sdio("send DEEP_SLEEP command\n");
842         ret = __lbs_cmd(priv, CMD_802_11_DEEP_SLEEP, &cmd, sizeof(cmd),
843                         lbs_cmd_copyback, (unsigned long) &cmd);
844         if (ret)
845                 lbs_pr_err("DEEP_SLEEP cmd failed\n");
846
847         mdelay(200);
848         return ret;
849 }
850
851 static int if_sdio_exit_deep_sleep(struct lbs_private *priv)
852 {
853         struct if_sdio_card *card = priv->card;
854         int ret = -1;
855
856         lbs_deb_enter(LBS_DEB_SDIO);
857         sdio_claim_host(card->func);
858
859         sdio_writeb(card->func, HOST_POWER_UP, CONFIGURATION_REG, &ret);
860         if (ret)
861                 lbs_pr_err("sdio_writeb failed!\n");
862
863         sdio_release_host(card->func);
864         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
865         return ret;
866 }
867
868 static int if_sdio_reset_deep_sleep_wakeup(struct lbs_private *priv)
869 {
870         struct if_sdio_card *card = priv->card;
871         int ret = -1;
872
873         lbs_deb_enter(LBS_DEB_SDIO);
874         sdio_claim_host(card->func);
875
876         sdio_writeb(card->func, 0, CONFIGURATION_REG, &ret);
877         if (ret)
878                 lbs_pr_err("sdio_writeb failed!\n");
879
880         sdio_release_host(card->func);
881         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
882         return ret;
883
884 }
885
886 /*******************************************************************/
887 /* SDIO callbacks                                                  */
888 /*******************************************************************/
889
890 static void if_sdio_interrupt(struct sdio_func *func)
891 {
892         int ret;
893         struct if_sdio_card *card;
894         u8 cause;
895
896         lbs_deb_enter(LBS_DEB_SDIO);
897
898         card = sdio_get_drvdata(func);
899
900         cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
901         if (ret)
902                 goto out;
903
904         lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
905
906         sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
907         if (ret)
908                 goto out;
909
910         /*
911          * Ignore the define name, this really means the card has
912          * successfully received the command.
913          */
914         card->priv->is_activity_detected = 1;
915         if (cause & IF_SDIO_H_INT_DNLD)
916                 lbs_host_to_card_done(card->priv);
917
918
919         if (cause & IF_SDIO_H_INT_UPLD) {
920                 ret = if_sdio_card_to_host(card);
921                 if (ret)
922                         goto out;
923         }
924
925         ret = 0;
926
927 out:
928         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
929 }
930
931 static int if_sdio_probe(struct sdio_func *func,
932                 const struct sdio_device_id *id)
933 {
934         struct if_sdio_card *card;
935         struct lbs_private *priv;
936         int ret, i;
937         unsigned int model;
938         struct if_sdio_packet *packet;
939
940         lbs_deb_enter(LBS_DEB_SDIO);
941
942         for (i = 0;i < func->card->num_info;i++) {
943                 if (sscanf(func->card->info[i],
944                                 "802.11 SDIO ID: %x", &model) == 1)
945                         break;
946                 if (sscanf(func->card->info[i],
947                                 "ID: %x", &model) == 1)
948                         break;
949                 if (!strcmp(func->card->info[i], "IBIS Wireless SDIO Card")) {
950                         model = IF_SDIO_MODEL_8385;
951                         break;
952                 }
953         }
954
955         if (i == func->card->num_info) {
956                 lbs_pr_err("unable to identify card model\n");
957                 return -ENODEV;
958         }
959
960         card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
961         if (!card)
962                 return -ENOMEM;
963
964         card->func = func;
965         card->model = model;
966
967         switch (card->model) {
968         case IF_SDIO_MODEL_8385:
969                 card->scratch_reg = IF_SDIO_SCRATCH_OLD;
970                 break;
971         case IF_SDIO_MODEL_8686:
972                 card->scratch_reg = IF_SDIO_SCRATCH;
973                 break;
974         case IF_SDIO_MODEL_8688:
975         default: /* for newer chipsets */
976                 card->scratch_reg = IF_SDIO_FW_STATUS;
977                 break;
978         }
979
980         spin_lock_init(&card->lock);
981         card->workqueue = create_workqueue("libertas_sdio");
982         INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
983
984         for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) {
985                 if (card->model == if_sdio_models[i].model)
986                         break;
987         }
988
989         if (i == ARRAY_SIZE(if_sdio_models)) {
990                 lbs_pr_err("unkown card model 0x%x\n", card->model);
991                 ret = -ENODEV;
992                 goto free;
993         }
994
995         card->helper = if_sdio_models[i].helper;
996         card->firmware = if_sdio_models[i].firmware;
997
998         if (lbs_helper_name) {
999                 lbs_deb_sdio("overriding helper firmware: %s\n",
1000                         lbs_helper_name);
1001                 card->helper = lbs_helper_name;
1002         }
1003
1004         if (lbs_fw_name) {
1005                 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name);
1006                 card->firmware = lbs_fw_name;
1007         }
1008
1009         sdio_claim_host(func);
1010
1011         ret = sdio_enable_func(func);
1012         if (ret)
1013                 goto release;
1014
1015         ret = sdio_claim_irq(func, if_sdio_interrupt);
1016         if (ret)
1017                 goto disable;
1018
1019         card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
1020         if (ret)
1021                 goto release_int;
1022
1023         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
1024         if (ret)
1025                 goto release_int;
1026
1027         card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
1028         if (ret)
1029                 goto release_int;
1030
1031         sdio_release_host(func);
1032
1033         sdio_set_drvdata(func, card);
1034
1035         lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
1036                         "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
1037                         func->class, func->vendor, func->device,
1038                         model, (unsigned)card->ioport);
1039
1040         ret = if_sdio_prog_firmware(card);
1041         if (ret)
1042                 goto reclaim;
1043
1044         priv = lbs_add_card(card, &func->dev);
1045         if (!priv) {
1046                 ret = -ENOMEM;
1047                 goto reclaim;
1048         }
1049
1050         card->priv = priv;
1051
1052         priv->card = card;
1053         priv->hw_host_to_card = if_sdio_host_to_card;
1054         priv->enter_deep_sleep = if_sdio_enter_deep_sleep;
1055         priv->exit_deep_sleep = if_sdio_exit_deep_sleep;
1056         priv->reset_deep_sleep_wakeup = if_sdio_reset_deep_sleep_wakeup;
1057
1058         priv->fw_ready = 1;
1059
1060         sdio_claim_host(func);
1061
1062         /*
1063          * Get rx_unit if the chip is SD8688 or newer.
1064          * SD8385 & SD8686 do not have rx_unit.
1065          */
1066         if ((card->model != IF_SDIO_MODEL_8385)
1067                         && (card->model != IF_SDIO_MODEL_8686))
1068                 card->rx_unit = if_sdio_read_rx_unit(card);
1069         else
1070                 card->rx_unit = 0;
1071
1072         /*
1073          * Enable interrupts now that everything is set up
1074          */
1075         sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
1076         sdio_release_host(func);
1077         if (ret)
1078                 goto reclaim;
1079
1080         /*
1081          * FUNC_INIT is required for SD8688 WLAN/BT multiple functions
1082          */
1083         if (card->model == IF_SDIO_MODEL_8688) {
1084                 struct cmd_header cmd;
1085
1086                 memset(&cmd, 0, sizeof(cmd));
1087
1088                 lbs_deb_sdio("send function INIT command\n");
1089                 if (__lbs_cmd(priv, CMD_FUNC_INIT, &cmd, sizeof(cmd),
1090                                 lbs_cmd_copyback, (unsigned long) &cmd))
1091                         lbs_pr_alert("CMD_FUNC_INIT cmd failed\n");
1092         }
1093
1094         ret = lbs_start_card(priv);
1095         if (ret)
1096                 goto err_activate_card;
1097
1098 out:
1099         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1100
1101         return ret;
1102
1103 err_activate_card:
1104         flush_workqueue(card->workqueue);
1105         lbs_remove_card(priv);
1106 reclaim:
1107         sdio_claim_host(func);
1108 release_int:
1109         sdio_release_irq(func);
1110 disable:
1111         sdio_disable_func(func);
1112 release:
1113         sdio_release_host(func);
1114 free:
1115         destroy_workqueue(card->workqueue);
1116         while (card->packets) {
1117                 packet = card->packets;
1118                 card->packets = card->packets->next;
1119                 kfree(packet);
1120         }
1121
1122         kfree(card);
1123
1124         goto out;
1125 }
1126
1127 static void if_sdio_remove(struct sdio_func *func)
1128 {
1129         struct if_sdio_card *card;
1130         struct if_sdio_packet *packet;
1131
1132         lbs_deb_enter(LBS_DEB_SDIO);
1133
1134         card = sdio_get_drvdata(func);
1135
1136         if (user_rmmod && (card->model == IF_SDIO_MODEL_8688)) {
1137                 /*
1138                  * FUNC_SHUTDOWN is required for SD8688 WLAN/BT
1139                  * multiple functions
1140                  */
1141                 struct cmd_header cmd;
1142
1143                 memset(&cmd, 0, sizeof(cmd));
1144
1145                 lbs_deb_sdio("send function SHUTDOWN command\n");
1146                 if (__lbs_cmd(card->priv, CMD_FUNC_SHUTDOWN,
1147                                 &cmd, sizeof(cmd), lbs_cmd_copyback,
1148                                 (unsigned long) &cmd))
1149                         lbs_pr_alert("CMD_FUNC_SHUTDOWN cmd failed\n");
1150         }
1151
1152
1153         lbs_deb_sdio("call remove card\n");
1154         lbs_stop_card(card->priv);
1155         lbs_remove_card(card->priv);
1156         card->priv->surpriseremoved = 1;
1157
1158         flush_workqueue(card->workqueue);
1159         destroy_workqueue(card->workqueue);
1160
1161         sdio_claim_host(func);
1162         sdio_release_irq(func);
1163         sdio_disable_func(func);
1164         sdio_release_host(func);
1165
1166         while (card->packets) {
1167                 packet = card->packets;
1168                 card->packets = card->packets->next;
1169                 kfree(packet);
1170         }
1171
1172         kfree(card);
1173
1174         lbs_deb_leave(LBS_DEB_SDIO);
1175 }
1176
1177 static struct sdio_driver if_sdio_driver = {
1178         .name           = "libertas_sdio",
1179         .id_table       = if_sdio_ids,
1180         .probe          = if_sdio_probe,
1181         .remove         = if_sdio_remove,
1182 };
1183
1184 /*******************************************************************/
1185 /* Module functions                                                */
1186 /*******************************************************************/
1187
1188 static int __init if_sdio_init_module(void)
1189 {
1190         int ret = 0;
1191
1192         lbs_deb_enter(LBS_DEB_SDIO);
1193
1194         printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1195         printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1196
1197         ret = sdio_register_driver(&if_sdio_driver);
1198
1199         /* Clear the flag in case user removes the card. */
1200         user_rmmod = 0;
1201
1202         lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1203
1204         return ret;
1205 }
1206
1207 static void __exit if_sdio_exit_module(void)
1208 {
1209         lbs_deb_enter(LBS_DEB_SDIO);
1210
1211         /* Set the flag as user is removing this module. */
1212         user_rmmod = 1;
1213
1214         sdio_unregister_driver(&if_sdio_driver);
1215
1216         lbs_deb_leave(LBS_DEB_SDIO);
1217 }
1218
1219 module_init(if_sdio_init_module);
1220 module_exit(if_sdio_exit_module);
1221
1222 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1223 MODULE_AUTHOR("Pierre Ossman");
1224 MODULE_LICENSE("GPL");