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