[PATCH] libertas: printk warning fixes
[pandora-kernel.git] / drivers / net / wireless / libertas / if_cs.c
1 /*
2
3   Driver for the Marvell 8385 based compact flash WLAN cards.
4
5   (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de>
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; see the file COPYING.  If not, write to
19   the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
20   Boston, MA 02110-1301, USA.
21
22 */
23
24 #include <linux/module.h>
25 #include <linux/delay.h>
26 #include <linux/moduleparam.h>
27 #include <linux/firmware.h>
28 #include <linux/netdevice.h>
29
30 #include <pcmcia/cs_types.h>
31 #include <pcmcia/cs.h>
32 #include <pcmcia/cistpl.h>
33 #include <pcmcia/ds.h>
34
35 #include <linux/io.h>
36
37 #define DRV_NAME "libertas_cs"
38
39 #include "decl.h"
40 #include "defs.h"
41 #include "dev.h"
42
43
44 /********************************************************************/
45 /* Module stuff                                                     */
46 /********************************************************************/
47
48 MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
49 MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
50 MODULE_LICENSE("GPL");
51
52
53
54 /********************************************************************/
55 /* Data structures                                                  */
56 /********************************************************************/
57
58 struct if_cs_card {
59         struct pcmcia_device *p_dev;
60         wlan_private *priv;
61         void __iomem *iobase;
62 };
63
64
65
66 /********************************************************************/
67 /* Hardware access                                                  */
68 /********************************************************************/
69
70 /* This define enables wrapper functions which allow you
71    to dump all register accesses. You normally won't this,
72    except for development */
73 /* #define DEBUG_IO */
74
75 #ifdef DEBUG_IO
76 static int debug_output = 0;
77 #else
78 /* This way the compiler optimizes the printk's away */
79 #define debug_output 0
80 #endif
81
82 static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
83 {
84         unsigned int val = ioread8(card->iobase + reg);
85         if (debug_output)
86                 printk(KERN_INFO "##inb %08x<%02x\n", reg, val);
87         return val;
88 }
89 static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
90 {
91         unsigned int val = ioread16(card->iobase + reg);
92         if (debug_output)
93                 printk(KERN_INFO "##inw %08x<%04x\n", reg, val);
94         return val;
95 }
96 static inline void if_cs_read16_rep(
97         struct if_cs_card *card,
98         uint reg,
99         void *buf,
100         unsigned long count)
101 {
102         if (debug_output)
103                 printk(KERN_INFO "##insw %08x<(0x%lx words)\n",
104                         reg, count);
105         ioread16_rep(card->iobase + reg, buf, count);
106 }
107
108 static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
109 {
110         if (debug_output)
111                 printk(KERN_INFO "##outb %08x>%02x\n", reg, val);
112         iowrite8(val, card->iobase + reg);
113 }
114
115 static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
116 {
117         if (debug_output)
118                 printk(KERN_INFO "##outw %08x>%04x\n", reg, val);
119         iowrite16(val, card->iobase + reg);
120 }
121
122 static inline void if_cs_write16_rep(
123         struct if_cs_card *card,
124         uint reg,
125         void *buf,
126         unsigned long count)
127 {
128         if (debug_output)
129                 printk(KERN_INFO "##outsw %08x>(0x%lx words)\n",
130                         reg, count);
131         iowrite16_rep(card->iobase + reg, buf, count);
132 }
133
134
135 /*
136  * I know that polling/delaying is frowned upon. However, this procedure
137  * with polling is needed while downloading the firmware. At this stage,
138  * the hardware does unfortunately not create any interrupts.
139  *
140  * Fortunately, this function is never used once the firmware is in
141  * the card. :-)
142  *
143  * As a reference, see the "Firmware Specification v5.1", page 18
144  * and 19. I did not follow their suggested timing to the word,
145  * but this works nice & fast anyway.
146  */
147 static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
148 {
149         int i;
150
151         for (i = 0; i < 500; i++) {
152                 u8 val = if_cs_read8(card, addr);
153                 if (val == reg)
154                         return i;
155                 udelay(100);
156         }
157         return -ETIME;
158 }
159
160
161
162 /* Host control registers and their bit definitions */
163
164 #define IF_CS_H_STATUS                  0x00000000
165 #define IF_CS_H_STATUS_TX_OVER          0x0001
166 #define IF_CS_H_STATUS_RX_OVER          0x0002
167 #define IF_CS_H_STATUS_DNLD_OVER        0x0004
168
169 #define IF_CS_H_INT_CAUSE               0x00000002
170 #define IF_CS_H_IC_TX_OVER              0x0001
171 #define IF_CS_H_IC_RX_OVER              0x0002
172 #define IF_CS_H_IC_DNLD_OVER            0x0004
173 #define IF_CS_H_IC_HOST_EVENT           0x0008
174 #define IF_CS_H_IC_MASK                 0x001f
175
176 #define IF_CS_H_INT_MASK                0x00000004
177 #define IF_CS_H_IM_MASK                 0x001f
178
179 #define IF_CS_H_WRITE_LEN               0x00000014
180
181 #define IF_CS_H_WRITE                   0x00000016
182
183 #define IF_CS_H_CMD_LEN                 0x00000018
184
185 #define IF_CS_H_CMD                     0x0000001A
186
187 #define IF_CS_C_READ_LEN                0x00000024
188
189 #define IF_CS_H_READ                    0x00000010
190
191 /* Card control registers and their bit definitions */
192
193 #define IF_CS_C_STATUS                  0x00000020
194 #define IF_CS_C_S_TX_DNLD_RDY           0x0001
195 #define IF_CS_C_S_RX_UPLD_RDY           0x0002
196 #define IF_CS_C_S_CMD_DNLD_RDY          0x0004
197 #define IF_CS_C_S_CMD_UPLD_RDY          0x0008
198 #define IF_CS_C_S_CARDEVENT             0x0010
199 #define IF_CS_C_S_MASK                  0x001f
200 #define IF_CS_C_S_STATUS_MASK           0x7f00
201 /* The following definitions should be the same as the MRVDRV_ ones */
202
203 #if MRVDRV_CMD_DNLD_RDY != IF_CS_C_S_CMD_DNLD_RDY
204 #error MRVDRV_CMD_DNLD_RDY and IF_CS_C_S_CMD_DNLD_RDY not in sync
205 #endif
206 #if MRVDRV_CMD_UPLD_RDY != IF_CS_C_S_CMD_UPLD_RDY
207 #error MRVDRV_CMD_UPLD_RDY and IF_CS_C_S_CMD_UPLD_RDY not in sync
208 #endif
209 #if MRVDRV_CARDEVENT != IF_CS_C_S_CARDEVENT
210 #error MRVDRV_CARDEVENT and IF_CS_C_S_CARDEVENT not in sync
211 #endif
212
213 #define IF_CS_C_INT_CAUSE               0x00000022
214 #define IF_CS_C_IC_MASK                 0x001f
215
216 #define IF_CS_C_SQ_READ_LOW             0x00000028
217 #define IF_CS_C_SQ_HELPER_OK            0x10
218
219 #define IF_CS_C_CMD_LEN                 0x00000030
220
221 #define IF_CS_C_CMD                     0x00000012
222
223 #define IF_CS_SCRATCH                   0x0000003F
224
225
226
227 /********************************************************************/
228 /* Interrupts                                                       */
229 /********************************************************************/
230
231 static inline void if_cs_enable_ints(struct if_cs_card *card)
232 {
233         lbs_deb_enter(LBS_DEB_CS);
234         if_cs_write16(card, IF_CS_H_INT_MASK, 0);
235 }
236
237 static inline void if_cs_disable_ints(struct if_cs_card *card)
238 {
239         lbs_deb_enter(LBS_DEB_CS);
240         if_cs_write16(card, IF_CS_H_INT_MASK, IF_CS_H_IM_MASK);
241 }
242
243 static irqreturn_t if_cs_interrupt(int irq, void *data)
244 {
245         struct if_cs_card *card = (struct if_cs_card *)data;
246         u16 int_cause;
247
248         lbs_deb_enter(LBS_DEB_CS);
249
250         int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE);
251         switch (int_cause) {
252         case 0x0000:
253                 /* not for us */
254                 return IRQ_NONE;
255         case 0xffff:
256                 /* if one reads junk, then probably the card was removed */
257                 card->priv->adapter->surpriseremoved = 1;
258                 break;
259         case IF_CS_H_IC_TX_OVER:
260                 if (card->priv->adapter->connect_status == LIBERTAS_CONNECTED)
261                         netif_wake_queue(card->priv->dev);
262                 /* fallthrought */
263         default:
264                 /* clear interrupt */
265                 if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause & IF_CS_C_IC_MASK);
266                 if_cs_disable_ints(card);
267         }
268
269         libertas_interrupt(card->priv->dev);
270
271         return IRQ_HANDLED;
272 }
273
274
275
276
277 /********************************************************************/
278 /* I/O                                                              */
279 /********************************************************************/
280
281 /*
282  * Called from if_cs_host_to_card to send a command to the hardware
283  */
284 static int if_cs_send_cmd(wlan_private *priv, u8 *buf, u16 nb)
285 {
286         struct if_cs_card *card = (struct if_cs_card *)priv->card;
287         int ret = -1;
288         int loops = 0;
289
290         lbs_deb_enter(LBS_DEB_CS);
291
292         /* Is hardware ready? */
293         while (1) {
294                 u16 val = if_cs_read16(card, IF_CS_C_STATUS);
295                 if (val & IF_CS_C_S_CMD_DNLD_RDY)
296                         break;
297                 if (++loops > 100) {
298                         lbs_pr_err("card not ready for commands\n");
299                         goto done;
300                 }
301                 mdelay(1);
302         }
303
304         if_cs_write16(card, IF_CS_H_CMD_LEN, nb);
305
306         if_cs_write16_rep(card, IF_CS_H_CMD, buf, nb / 2);
307         /* Are we supposed to transfer an odd amount of bytes? */
308         if (nb & 1)
309                 if_cs_write8(card, IF_CS_H_CMD, buf[nb-1]);
310
311         /* "Assert the download over interrupt command in the Host
312          * status register" */
313         if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
314
315         /* "Assert the download over interrupt command in the Card
316          * interrupt case register" */
317         if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
318         ret = 0;
319
320 done:
321         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
322         return ret;
323 }
324
325
326 /*
327  * Called from if_cs_host_to_card to send a data to the hardware
328  */
329 static void if_cs_send_data(wlan_private *priv, u8 *buf, u16 nb)
330 {
331         struct if_cs_card *card = (struct if_cs_card *)priv->card;
332
333         lbs_deb_enter(LBS_DEB_CS);
334
335         if_cs_write16(card, IF_CS_H_WRITE_LEN, nb);
336
337         /* write even number of bytes, then odd byte if necessary */
338         if_cs_write16_rep(card, IF_CS_H_WRITE, buf, nb / 2);
339         if (nb & 1)
340                 if_cs_write8(card, IF_CS_H_WRITE, buf[nb-1]);
341
342         if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_TX_OVER);
343         if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_STATUS_TX_OVER);
344
345         lbs_deb_leave(LBS_DEB_CS);
346 }
347
348
349 /*
350  * Get the command result out of the card.
351  */
352 static int if_cs_receive_cmdres(wlan_private *priv, u8* data, u32 *len)
353 {
354         int ret = -1;
355         u16 val;
356
357         lbs_deb_enter(LBS_DEB_CS);
358
359         /* is hardware ready? */
360         val = if_cs_read16(priv->card, IF_CS_C_STATUS);
361         if ((val & IF_CS_C_S_CMD_UPLD_RDY) == 0) {
362                 lbs_pr_err("card not ready for CMD\n");
363                 goto out;
364         }
365
366         *len = if_cs_read16(priv->card, IF_CS_C_CMD_LEN);
367         if ((*len == 0) || (*len > MRVDRV_SIZE_OF_CMD_BUFFER)) {
368                 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
369                 goto out;
370         }
371
372         /* read even number of bytes, then odd byte if necessary */
373         if_cs_read16_rep(priv->card, IF_CS_C_CMD, data, *len/sizeof(u16));
374         if (*len & 1)
375                 data[*len-1] = if_cs_read8(priv->card, IF_CS_C_CMD);
376
377         ret = 0;
378 out:
379         lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
380         return ret;
381 }
382
383
384 static struct sk_buff *if_cs_receive_data(wlan_private *priv)
385 {
386         struct sk_buff *skb = NULL;
387         u16 len;
388         u8 *data;
389
390         lbs_deb_enter(LBS_DEB_CS);
391
392         len = if_cs_read16(priv->card, IF_CS_C_READ_LEN);
393         if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
394                 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
395                 priv->stats.rx_dropped++;
396                 printk(KERN_INFO "##HS %s:%d TODO\n", __FUNCTION__, __LINE__);
397                 goto dat_err;
398         }
399
400         //TODO: skb = dev_alloc_skb(len+ETH_FRAME_LEN+MRVDRV_SNAP_HEADER_LEN+EXTRA_LEN);
401         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
402         if (!skb)
403                 goto out;
404         data = skb_put(skb, len);
405
406         /* read even number of bytes, then odd byte if necessary */
407         if_cs_read16_rep(priv->card, IF_CS_H_READ, data, len/sizeof(u16));
408         if (len & 1)
409                 data[len-1] = if_cs_read8(priv->card, IF_CS_H_READ);
410
411 dat_err:
412         if_cs_write16(priv->card, IF_CS_H_STATUS, IF_CS_H_STATUS_RX_OVER);
413         if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_RX_OVER);
414
415 out:
416         lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
417         return skb;
418 }
419
420
421
422 /********************************************************************/
423 /* Firmware                                                         */
424 /********************************************************************/
425
426 /*
427  * Tries to program the helper firmware.
428  *
429  * Return 0 on success
430  */
431 static int if_cs_prog_helper(struct if_cs_card *card)
432 {
433         int ret = 0;
434         int sent = 0;
435         u8  scratch;
436         const struct firmware *fw;
437
438         lbs_deb_enter(LBS_DEB_CS);
439
440         scratch = if_cs_read8(card, IF_CS_SCRATCH);
441
442         /* "If the value is 0x5a, the firmware is already
443          * downloaded successfully"
444          */
445         if (scratch == 0x5a)
446                 goto done;
447
448         /* "If the value is != 00, it is invalid value of register */
449         if (scratch != 0x00) {
450                 ret = -ENODEV;
451                 goto done;
452         }
453
454         /* TODO: make firmware file configurable */
455         ret = request_firmware(&fw, "libertas_cs_helper.fw",
456                 &handle_to_dev(card->p_dev));
457         if (ret) {
458                 lbs_pr_err("can't load helper firmware\n");
459                 ret = -ENODEV;
460                 goto done;
461         }
462         lbs_deb_cs("helper size %td\n", fw->size);
463
464         /* "Set the 5 bytes of the helper image to 0" */
465         /* Not needed, this contains an ARM branch instruction */
466
467         for (;;) {
468                 /* "the number of bytes to send is 256" */
469                 int count = 256;
470                 int remain = fw->size - sent;
471
472                 if (remain < count)
473                         count = remain;
474                 /* printk(KERN_INFO "//HS %d loading %d of %d bytes\n",
475                         __LINE__, sent, fw->size); */
476
477                 /* "write the number of bytes to be sent to the I/O Command
478                  * write length register" */
479                 if_cs_write16(card, IF_CS_H_CMD_LEN, count);
480
481                 /* "write this to I/O Command port register as 16 bit writes */
482                 if (count)
483                         if_cs_write16_rep(card, IF_CS_H_CMD,
484                                 &fw->data[sent],
485                                 count >> 1);
486
487                 /* "Assert the download over interrupt command in the Host
488                  * status register" */
489                 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
490
491                 /* "Assert the download over interrupt command in the Card
492                  * interrupt case register" */
493                 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
494
495                 /* "The host polls the Card Status register ... for 50 ms before
496                    declaring a failure */
497                 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
498                         IF_CS_C_S_CMD_DNLD_RDY);
499                 if (ret < 0) {
500                         lbs_pr_err("can't download helper at 0x%x, ret %d\n",
501                                 sent, ret);
502                         goto done;
503                 }
504
505                 if (count == 0)
506                         break;
507
508                 sent += count;
509         }
510
511         release_firmware(fw);
512         ret = 0;
513
514 done:
515         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
516         return ret;
517 }
518
519
520 static int if_cs_prog_real(struct if_cs_card *card)
521 {
522         const struct firmware *fw;
523         int ret = 0;
524         int retry = 0;
525         int len = 0;
526         int sent;
527
528         lbs_deb_enter(LBS_DEB_CS);
529
530         /* TODO: make firmware file configurable */
531         ret = request_firmware(&fw, "libertas_cs.fw",
532                 &handle_to_dev(card->p_dev));
533         if (ret) {
534                 lbs_pr_err("can't load firmware\n");
535                 ret = -ENODEV;
536                 goto done;
537         }
538         lbs_deb_cs("fw size %td\n", fw->size);
539
540         ret = if_cs_poll_while_fw_download(card, IF_CS_C_SQ_READ_LOW, IF_CS_C_SQ_HELPER_OK);
541         if (ret < 0) {
542                 int i;
543                 lbs_pr_err("helper firmware doesn't answer\n");
544                 for (i = 0; i < 0x50; i += 2)
545                         printk(KERN_INFO "## HS %02x: %04x\n",
546                                 i, if_cs_read16(card, i));
547                 goto err_release;
548         }
549
550         for (sent = 0; sent < fw->size; sent += len) {
551                 len = if_cs_read16(card, IF_CS_C_SQ_READ_LOW);
552                 /* printk(KERN_INFO "//HS %d loading %d of %d bytes\n",
553                         __LINE__, sent, fw->size); */
554                 if (len & 1) {
555                         retry++;
556                         lbs_pr_info("odd, need to retry this firmware block\n");
557                 } else {
558                         retry = 0;
559                 }
560
561                 if (retry > 20) {
562                         lbs_pr_err("could not download firmware\n");
563                         ret = -ENODEV;
564                         goto err_release;
565                 }
566                 if (retry) {
567                         sent -= len;
568                 }
569
570
571                 if_cs_write16(card, IF_CS_H_CMD_LEN, len);
572
573                 if_cs_write16_rep(card, IF_CS_H_CMD,
574                         &fw->data[sent],
575                         (len+1) >> 1);
576                 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
577                 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
578
579                 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
580                         IF_CS_C_S_CMD_DNLD_RDY);
581                 if (ret < 0) {
582                         lbs_pr_err("can't download firmware at 0x%x\n", sent);
583                         goto err_release;
584                 }
585         }
586
587         ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
588         if (ret < 0) {
589                 lbs_pr_err("firmware download failed\n");
590                 goto err_release;
591         }
592
593         ret = 0;
594         goto done;
595
596
597 err_release:
598         release_firmware(fw);
599
600 done:
601         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
602         return ret;
603 }
604
605
606
607 /********************************************************************/
608 /* Callback functions for libertas.ko                               */
609 /********************************************************************/
610
611 /* Send commands or data packets to the card */
612 static int if_cs_host_to_card(wlan_private *priv, u8 type, u8 *buf, u16 nb)
613 {
614         int ret = -1;
615
616         lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
617
618         switch (type) {
619         case MVMS_DAT:
620                 priv->dnld_sent = DNLD_CMD_SENT;
621                 if_cs_send_data(priv, buf, nb);
622                 ret = 0;
623                 break;
624         case MVMS_CMD:
625                 ret = if_cs_send_cmd(priv, buf, nb);
626                 break;
627         default:
628                 lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
629         }
630
631         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
632         return ret;
633 }
634
635
636 static int if_cs_get_int_status(wlan_private *priv, u8 *ireg)
637 {
638         struct if_cs_card *card = (struct if_cs_card *)priv->card;
639         //wlan_adapter *adapter = priv->adapter;
640         int ret = 0;
641         u16 int_cause;
642         u8 *cmdbuf;
643         *ireg = 0;
644
645         lbs_deb_enter(LBS_DEB_CS);
646
647         if (priv->adapter->surpriseremoved)
648                 goto out;
649
650         int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE) & IF_CS_C_IC_MASK;
651         if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause);
652
653         *ireg = if_cs_read16(card, IF_CS_C_STATUS) & IF_CS_C_S_MASK;
654         if_cs_enable_ints(card);
655
656         if (!*ireg)
657                 goto sbi_get_int_status_exit;
658
659 sbi_get_int_status_exit:
660
661         /* is there a data packet for us? */
662         if (*ireg & IF_CS_C_S_RX_UPLD_RDY) {
663                 struct sk_buff *skb = if_cs_receive_data(priv);
664                 libertas_process_rxed_packet(priv, skb);
665                 *ireg &= ~IF_CS_C_S_RX_UPLD_RDY;
666         }
667
668         if (*ireg & IF_CS_C_S_TX_DNLD_RDY) {
669                 priv->dnld_sent = DNLD_RES_RECEIVED;
670         }
671
672         /* Card has a command result for us */
673         if (*ireg & IF_CS_C_S_CMD_UPLD_RDY) {
674                 spin_lock(&priv->adapter->driver_lock);
675                 if (!priv->adapter->cur_cmd) {
676                         cmdbuf = priv->upld_buf;
677                         priv->adapter->hisregcpy &= ~IF_CS_C_S_RX_UPLD_RDY;
678                 } else {
679                         cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
680                 }
681
682                 ret = if_cs_receive_cmdres(priv, cmdbuf, &priv->upld_len);
683                 spin_unlock(&priv->adapter->driver_lock);
684                 if (ret < 0)
685                         lbs_pr_err("could not receive cmd from card\n");
686         }
687
688 out:
689         lbs_deb_leave_args(LBS_DEB_CS, "ret %d, ireg 0x%x, hisregcpy 0x%x", ret, *ireg, priv->adapter->hisregcpy);
690         return ret;
691 }
692
693
694 static int if_cs_read_event_cause(wlan_private *priv)
695 {
696         lbs_deb_enter(LBS_DEB_CS);
697
698         priv->adapter->eventcause = (if_cs_read16(priv->card, IF_CS_C_STATUS) & IF_CS_C_S_STATUS_MASK) >> 5;
699         if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_HOST_EVENT);
700
701         return 0;
702 }
703
704
705
706 /********************************************************************/
707 /* Card Services                                                    */
708 /********************************************************************/
709
710 /*
711  * After a card is removed, if_cs_release() will unregister the
712  * device, and release the PCMCIA configuration.  If the device is
713  * still open, this will be postponed until it is closed.
714  */
715 static void if_cs_release(struct pcmcia_device *p_dev)
716 {
717         struct if_cs_card *card = p_dev->priv;
718
719         lbs_deb_enter(LBS_DEB_CS);
720
721         pcmcia_disable_device(p_dev);
722         free_irq(p_dev->irq.AssignedIRQ, card);
723         if (card->iobase)
724                 ioport_unmap(card->iobase);
725
726         lbs_deb_leave(LBS_DEB_CS);
727 }
728
729
730 /*
731  * This creates an "instance" of the driver, allocating local data
732  * structures for one device.  The device is registered with Card
733  * Services.
734  *
735  * The dev_link structure is initialized, but we don't actually
736  * configure the card at this point -- we wait until we receive a card
737  * insertion event.
738  */
739 static int if_cs_probe(struct pcmcia_device *p_dev)
740 {
741         int ret = -ENOMEM;
742         wlan_private *priv;
743         struct if_cs_card *card;
744         /* CIS parsing */
745         tuple_t tuple;
746         cisparse_t parse;
747         cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
748         cistpl_io_t *io = &cfg->io;
749         u_char buf[64];
750
751         lbs_deb_enter(LBS_DEB_CS);
752
753         card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
754         if (!card) {
755                 lbs_pr_err("error in kzalloc\n");
756                 goto out;
757         }
758         card->p_dev = p_dev;
759         p_dev->priv = card;
760
761         p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
762         p_dev->irq.Handler = NULL;
763         p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
764
765         p_dev->conf.Attributes = 0;
766         p_dev->conf.IntType = INT_MEMORY_AND_IO;
767
768         tuple.Attributes = 0;
769         tuple.TupleData = buf;
770         tuple.TupleDataMax = sizeof(buf);
771         tuple.TupleOffset = 0;
772
773         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
774         if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
775             (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
776             (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
777         {
778                 lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
779                 goto out1;
780         }
781
782         p_dev->conf.ConfigIndex = cfg->index;
783
784         /* Do we need to allocate an interrupt? */
785         if (cfg->irq.IRQInfo1) {
786                 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
787         }
788
789         /* IO window settings */
790         if (cfg->io.nwin != 1) {
791                 lbs_pr_err("wrong CIS (check number of IO windows)\n");
792                 ret = -ENODEV;
793                 goto out1;
794         }
795         p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
796         p_dev->io.BasePort1 = io->win[0].base;
797         p_dev->io.NumPorts1 = io->win[0].len;
798
799         /* This reserves IO space but doesn't actually enable it */
800         ret = pcmcia_request_io(p_dev, &p_dev->io);
801         if (ret) {
802                 lbs_pr_err("error in pcmcia_request_io\n");
803                 goto out1;
804         }
805
806         /*
807          * Allocate an interrupt line.  Note that this does not assign
808          * a handler to the interrupt, unless the 'Handler' member of
809          * the irq structure is initialized.
810          */
811         if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
812                 ret = pcmcia_request_irq(p_dev, &p_dev->irq);
813                 if (ret) {
814                         lbs_pr_err("error in pcmcia_request_irq\n");
815                         goto out1;
816                 }
817         }
818
819         /* Initialize io access */
820         card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
821         if (!card->iobase) {
822                 lbs_pr_err("error in ioport_map\n");
823                 ret = -EIO;
824                 goto out1;
825         }
826
827         /*
828          * This actually configures the PCMCIA socket -- setting up
829          * the I/O windows and the interrupt mapping, and putting the
830          * card and host interface into "Memory and IO" mode.
831          */
832         ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
833         if (ret) {
834                 lbs_pr_err("error in pcmcia_request_configuration\n");
835                 goto out2;
836         }
837
838         /* Finally, report what we've done */
839         lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
840                p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
841                p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
842
843         if_cs_enable_ints(card);
844
845         /* Load the firmware early, before calling into libertas.ko */
846         ret = if_cs_prog_helper(card);
847         if (ret == 0)
848                 ret = if_cs_prog_real(card);
849         if (ret)
850                 goto out2;
851
852         /* Make this card known to the libertas driver */
853         priv = libertas_add_card(card, &p_dev->dev);
854         if (!priv) {
855                 ret = -ENOMEM;
856                 goto out2;
857         }
858
859         /* Store pointers to our call-back functions */
860         card->priv = priv;
861         priv->card = card;
862         priv->hw_host_to_card     = if_cs_host_to_card;
863         priv->hw_get_int_status   = if_cs_get_int_status;
864         priv->hw_read_event_cause = if_cs_read_event_cause;
865
866         priv->adapter->fw_ready = 1;
867
868         /* Now actually get the IRQ */
869         ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
870                 IRQF_SHARED, DRV_NAME, card);
871         if (ret) {
872                 lbs_pr_err("error in request_irq\n");
873                 goto out3;
874         }
875
876         /* And finally bring the card up */
877         if (libertas_start_card(priv) != 0) {
878                 lbs_pr_err("could not activate card\n");
879                 goto out3;
880         }
881
882         ret = 0;
883         goto out;
884
885 out3:
886         libertas_remove_card(priv);
887 out2:
888         ioport_unmap(card->iobase);
889 out1:
890         pcmcia_disable_device(p_dev);
891 out:
892         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
893         return ret;
894 }
895
896
897 /*
898  * This deletes a driver "instance".  The device is de-registered with
899  * Card Services.  If it has been released, all local data structures
900  * are freed.  Otherwise, the structures will be freed when the device
901  * is released.
902  */
903 static void if_cs_detach(struct pcmcia_device *p_dev)
904 {
905         struct if_cs_card *card = p_dev->priv;
906
907         lbs_deb_enter(LBS_DEB_CS);
908
909         libertas_stop_card(card->priv);
910         libertas_remove_card(card->priv);
911         if_cs_release(p_dev);
912         kfree(card);
913
914         lbs_deb_leave(LBS_DEB_CS);
915 }
916
917
918
919 /********************************************************************/
920 /* Module initialization                                            */
921 /********************************************************************/
922
923 static struct pcmcia_device_id if_cs_ids[] = {
924         PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
925         PCMCIA_DEVICE_NULL,
926 };
927 MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
928
929
930 static struct pcmcia_driver libertas_driver = {
931         .owner          = THIS_MODULE,
932         .drv            = {
933                 .name   = DRV_NAME,
934         },
935         .probe          = if_cs_probe,
936         .remove         = if_cs_detach,
937         .id_table       = if_cs_ids,
938 };
939
940
941 static int __init if_cs_init(void)
942 {
943         int ret;
944
945         lbs_deb_enter(LBS_DEB_CS);
946         ret = pcmcia_register_driver(&libertas_driver);
947         lbs_deb_leave(LBS_DEB_CS);
948         return ret;
949 }
950
951
952 static void __exit if_cs_exit(void)
953 {
954         lbs_deb_enter(LBS_DEB_CS);
955         pcmcia_unregister_driver(&libertas_driver);
956         lbs_deb_leave(LBS_DEB_CS);
957 }
958
959
960 module_init(if_cs_init);
961 module_exit(if_cs_exit);