86bb1268885a292e01ff6b02442972d9910cedb9
[safe/jmp/linux-2.6] / 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         struct lbs_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 < 100000; i++) {
152                 u8 val = if_cs_read8(card, addr);
153                 if (val == reg)
154                         return i;
155                 udelay(5);
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_POWER_DOWN           0x0008
174 #define IF_CS_H_IC_HOST_EVENT           0x0010
175 #define IF_CS_H_IC_MASK                 0x001f
176
177 #define IF_CS_H_INT_MASK                0x00000004
178 #define IF_CS_H_IM_MASK                 0x001f
179
180 #define IF_CS_H_WRITE_LEN               0x00000014
181
182 #define IF_CS_H_WRITE                   0x00000016
183
184 #define IF_CS_H_CMD_LEN                 0x00000018
185
186 #define IF_CS_H_CMD                     0x0000001A
187
188 #define IF_CS_C_READ_LEN                0x00000024
189
190 #define IF_CS_H_READ                    0x00000010
191
192 /* Card control registers and their bit definitions */
193
194 #define IF_CS_C_STATUS                  0x00000020
195 #define IF_CS_C_S_TX_DNLD_RDY           0x0001
196 #define IF_CS_C_S_RX_UPLD_RDY           0x0002
197 #define IF_CS_C_S_CMD_DNLD_RDY          0x0004
198 #define IF_CS_C_S_CMD_UPLD_RDY          0x0008
199 #define IF_CS_C_S_CARDEVENT             0x0010
200 #define IF_CS_C_S_MASK                  0x001f
201 #define IF_CS_C_S_STATUS_MASK           0x7f00
202
203 #define IF_CS_C_INT_CAUSE               0x00000022
204 #define IF_CS_C_IC_MASK                 0x001f
205
206 #define IF_CS_C_SQ_READ_LOW             0x00000028
207 #define IF_CS_C_SQ_HELPER_OK            0x10
208
209 #define IF_CS_C_CMD_LEN                 0x00000030
210
211 #define IF_CS_C_CMD                     0x00000012
212
213 #define IF_CS_SCRATCH                   0x0000003F
214
215
216
217 /********************************************************************/
218 /* I/O                                                              */
219 /********************************************************************/
220
221 /*
222  * Called from if_cs_host_to_card to send a command to the hardware
223  */
224 static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
225 {
226         struct if_cs_card *card = (struct if_cs_card *)priv->card;
227         int ret = -1;
228         int loops = 0;
229
230         lbs_deb_enter(LBS_DEB_CS);
231
232         /* Is hardware ready? */
233         while (1) {
234                 u16 val = if_cs_read16(card, IF_CS_C_STATUS);
235                 if (val & IF_CS_C_S_CMD_DNLD_RDY)
236                         break;
237                 if (++loops > 100) {
238                         lbs_pr_err("card not ready for commands\n");
239                         goto done;
240                 }
241                 mdelay(1);
242         }
243
244         if_cs_write16(card, IF_CS_H_CMD_LEN, nb);
245
246         if_cs_write16_rep(card, IF_CS_H_CMD, buf, nb / 2);
247         /* Are we supposed to transfer an odd amount of bytes? */
248         if (nb & 1)
249                 if_cs_write8(card, IF_CS_H_CMD, buf[nb-1]);
250
251         /* "Assert the download over interrupt command in the Host
252          * status register" */
253         if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
254
255         /* "Assert the download over interrupt command in the Card
256          * interrupt case register" */
257         if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
258         ret = 0;
259
260 done:
261         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
262         return ret;
263 }
264
265
266 /*
267  * Called from if_cs_host_to_card to send a data to the hardware
268  */
269 static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
270 {
271         struct if_cs_card *card = (struct if_cs_card *)priv->card;
272
273         lbs_deb_enter(LBS_DEB_CS);
274
275         if_cs_write16(card, IF_CS_H_WRITE_LEN, nb);
276
277         /* write even number of bytes, then odd byte if necessary */
278         if_cs_write16_rep(card, IF_CS_H_WRITE, buf, nb / 2);
279         if (nb & 1)
280                 if_cs_write8(card, IF_CS_H_WRITE, buf[nb-1]);
281
282         if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_TX_OVER);
283         if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_STATUS_TX_OVER);
284
285         lbs_deb_leave(LBS_DEB_CS);
286 }
287
288
289 /*
290  * Get the command result out of the card.
291  */
292 static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
293 {
294         unsigned long flags;
295         int ret = -1;
296         u16 val;
297
298         lbs_deb_enter(LBS_DEB_CS);
299
300         /* is hardware ready? */
301         val = if_cs_read16(priv->card, IF_CS_C_STATUS);
302         if ((val & IF_CS_C_S_CMD_UPLD_RDY) == 0) {
303                 lbs_pr_err("card not ready for CMD\n");
304                 goto out;
305         }
306
307         *len = if_cs_read16(priv->card, IF_CS_C_CMD_LEN);
308         if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
309                 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
310                 goto out;
311         }
312
313         /* read even number of bytes, then odd byte if necessary */
314         if_cs_read16_rep(priv->card, IF_CS_C_CMD, data, *len/sizeof(u16));
315         if (*len & 1)
316                 data[*len-1] = if_cs_read8(priv->card, IF_CS_C_CMD);
317
318         /* This is a workaround for a firmware that reports too much
319          * bytes */
320         *len -= 8;
321         ret = 0;
322
323         /* Clear this flag again */
324         spin_lock_irqsave(&priv->driver_lock, flags);
325         priv->dnld_sent = DNLD_RES_RECEIVED;
326         spin_unlock_irqrestore(&priv->driver_lock, flags);
327
328 out:
329         lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
330         return ret;
331 }
332
333
334 static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
335 {
336         struct sk_buff *skb = NULL;
337         u16 len;
338         u8 *data;
339
340         lbs_deb_enter(LBS_DEB_CS);
341
342         len = if_cs_read16(priv->card, IF_CS_C_READ_LEN);
343         if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
344                 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
345                 priv->stats.rx_dropped++;
346                 goto dat_err;
347         }
348
349         skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
350         if (!skb)
351                 goto out;
352         skb_put(skb, len);
353         skb_reserve(skb, 2);/* 16 byte align */
354         data = skb->data;
355
356         /* read even number of bytes, then odd byte if necessary */
357         if_cs_read16_rep(priv->card, IF_CS_H_READ, data, len/sizeof(u16));
358         if (len & 1)
359                 data[len-1] = if_cs_read8(priv->card, IF_CS_H_READ);
360
361 dat_err:
362         if_cs_write16(priv->card, IF_CS_H_STATUS, IF_CS_H_STATUS_RX_OVER);
363         if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_RX_OVER);
364
365 out:
366         lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
367         return skb;
368 }
369
370
371
372 /********************************************************************/
373 /* Interrupts                                                       */
374 /********************************************************************/
375
376 static inline void if_cs_enable_ints(struct if_cs_card *card)
377 {
378         lbs_deb_enter(LBS_DEB_CS);
379         if_cs_write16(card, IF_CS_H_INT_MASK, 0);
380 }
381
382 static inline void if_cs_disable_ints(struct if_cs_card *card)
383 {
384         lbs_deb_enter(LBS_DEB_CS);
385         if_cs_write16(card, IF_CS_H_INT_MASK, IF_CS_H_IM_MASK);
386 }
387
388
389 static irqreturn_t if_cs_interrupt(int irq, void *data)
390 {
391         struct if_cs_card *card = data;
392         struct lbs_private *priv = card->priv;
393         u16 cause;
394
395         lbs_deb_enter(LBS_DEB_CS);
396
397         cause = if_cs_read16(card, IF_CS_C_INT_CAUSE);
398         if_cs_write16(card, IF_CS_C_INT_CAUSE, cause & IF_CS_C_IC_MASK);
399
400         lbs_deb_cs("cause 0x%04x\n", cause);
401         if (cause == 0) {
402                 /* Not for us */
403                 return IRQ_NONE;
404         }
405
406         if (cause == 0xffff) {
407                 /* Read in junk, the card has probably been removed */
408                 card->priv->surpriseremoved = 1;
409                 return IRQ_HANDLED;
410         }
411
412         /* TODO: I'm not sure what the best ordering is */
413
414         cause = if_cs_read16(card, IF_CS_C_STATUS) & IF_CS_C_S_MASK;
415
416         if (cause & IF_CS_C_S_RX_UPLD_RDY) {
417                 struct sk_buff *skb;
418                 lbs_deb_cs("rx packet\n");
419                 skb = if_cs_receive_data(priv);
420                 if (skb)
421                         lbs_process_rxed_packet(priv, skb);
422         }
423
424         if (cause & IF_CS_H_IC_TX_OVER) {
425                 lbs_deb_cs("tx over\n");
426                 lbs_host_to_card_done(priv);
427         }
428
429         if (cause & IF_CS_C_S_CMD_UPLD_RDY) {
430                 unsigned long flags;
431                 u8 i;
432
433                 lbs_deb_cs("cmd upload ready\n");
434                 spin_lock_irqsave(&priv->driver_lock, flags);
435                 i = (priv->resp_idx == 0) ? 1 : 0;
436                 spin_unlock_irqrestore(&priv->driver_lock, flags);
437
438                 BUG_ON(priv->resp_len[i]);
439                 if_cs_receive_cmdres(priv, priv->resp_buf[i],
440                         &priv->resp_len[i]);
441
442                 spin_lock_irqsave(&priv->driver_lock, flags);
443                 lbs_notify_command_response(priv, i);
444                 spin_unlock_irqrestore(&priv->driver_lock, flags);
445         }
446
447         if (cause & IF_CS_H_IC_HOST_EVENT) {
448                 u16 event = if_cs_read16(priv->card, IF_CS_C_STATUS)
449                         & IF_CS_C_S_STATUS_MASK;
450                 if_cs_write16(priv->card, IF_CS_H_INT_CAUSE,
451                         IF_CS_H_IC_HOST_EVENT);
452                 lbs_deb_cs("eventcause 0x%04x\n", event);
453                 lbs_queue_event(priv, event >> 8 & 0xff);
454         }
455
456         return IRQ_HANDLED;
457 }
458
459
460
461
462 /********************************************************************/
463 /* Firmware                                                         */
464 /********************************************************************/
465
466 /*
467  * Tries to program the helper firmware.
468  *
469  * Return 0 on success
470  */
471 static int if_cs_prog_helper(struct if_cs_card *card)
472 {
473         int ret = 0;
474         int sent = 0;
475         u8  scratch;
476         const struct firmware *fw;
477
478         lbs_deb_enter(LBS_DEB_CS);
479
480         scratch = if_cs_read8(card, IF_CS_SCRATCH);
481
482         /* "If the value is 0x5a, the firmware is already
483          * downloaded successfully"
484          */
485         if (scratch == 0x5a)
486                 goto done;
487
488         /* "If the value is != 00, it is invalid value of register */
489         if (scratch != 0x00) {
490                 ret = -ENODEV;
491                 goto done;
492         }
493
494         /* TODO: make firmware file configurable */
495         ret = request_firmware(&fw, "libertas_cs_helper.fw",
496                 &handle_to_dev(card->p_dev));
497         if (ret) {
498                 lbs_pr_err("can't load helper firmware\n");
499                 ret = -ENODEV;
500                 goto done;
501         }
502         lbs_deb_cs("helper size %td\n", fw->size);
503
504         /* "Set the 5 bytes of the helper image to 0" */
505         /* Not needed, this contains an ARM branch instruction */
506
507         for (;;) {
508                 /* "the number of bytes to send is 256" */
509                 int count = 256;
510                 int remain = fw->size - sent;
511
512                 if (remain < count)
513                         count = remain;
514
515                 /* "write the number of bytes to be sent to the I/O Command
516                  * write length register" */
517                 if_cs_write16(card, IF_CS_H_CMD_LEN, count);
518
519                 /* "write this to I/O Command port register as 16 bit writes */
520                 if (count)
521                         if_cs_write16_rep(card, IF_CS_H_CMD,
522                                 &fw->data[sent],
523                                 count >> 1);
524
525                 /* "Assert the download over interrupt command in the Host
526                  * status register" */
527                 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
528
529                 /* "Assert the download over interrupt command in the Card
530                  * interrupt case register" */
531                 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
532
533                 /* "The host polls the Card Status register ... for 50 ms before
534                    declaring a failure */
535                 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
536                         IF_CS_C_S_CMD_DNLD_RDY);
537                 if (ret < 0) {
538                         lbs_pr_err("can't download helper at 0x%x, ret %d\n",
539                                 sent, ret);
540                         goto done;
541                 }
542
543                 if (count == 0)
544                         break;
545
546                 sent += count;
547         }
548
549         release_firmware(fw);
550         ret = 0;
551
552 done:
553         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
554         return ret;
555 }
556
557
558 static int if_cs_prog_real(struct if_cs_card *card)
559 {
560         const struct firmware *fw;
561         int ret = 0;
562         int retry = 0;
563         int len = 0;
564         int sent;
565
566         lbs_deb_enter(LBS_DEB_CS);
567
568         /* TODO: make firmware file configurable */
569         ret = request_firmware(&fw, "libertas_cs.fw",
570                 &handle_to_dev(card->p_dev));
571         if (ret) {
572                 lbs_pr_err("can't load firmware\n");
573                 ret = -ENODEV;
574                 goto done;
575         }
576         lbs_deb_cs("fw size %td\n", fw->size);
577
578         ret = if_cs_poll_while_fw_download(card, IF_CS_C_SQ_READ_LOW, IF_CS_C_SQ_HELPER_OK);
579         if (ret < 0) {
580                 lbs_pr_err("helper firmware doesn't answer\n");
581                 goto err_release;
582         }
583
584         for (sent = 0; sent < fw->size; sent += len) {
585                 len = if_cs_read16(card, IF_CS_C_SQ_READ_LOW);
586                 if (len & 1) {
587                         retry++;
588                         lbs_pr_info("odd, need to retry this firmware block\n");
589                 } else {
590                         retry = 0;
591                 }
592
593                 if (retry > 20) {
594                         lbs_pr_err("could not download firmware\n");
595                         ret = -ENODEV;
596                         goto err_release;
597                 }
598                 if (retry) {
599                         sent -= len;
600                 }
601
602
603                 if_cs_write16(card, IF_CS_H_CMD_LEN, len);
604
605                 if_cs_write16_rep(card, IF_CS_H_CMD,
606                         &fw->data[sent],
607                         (len+1) >> 1);
608                 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
609                 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
610
611                 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
612                         IF_CS_C_S_CMD_DNLD_RDY);
613                 if (ret < 0) {
614                         lbs_pr_err("can't download firmware at 0x%x\n", sent);
615                         goto err_release;
616                 }
617         }
618
619         ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
620         if (ret < 0) {
621                 lbs_pr_err("firmware download failed\n");
622                 goto err_release;
623         }
624
625         ret = 0;
626         goto done;
627
628
629 err_release:
630         release_firmware(fw);
631
632 done:
633         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
634         return ret;
635 }
636
637
638
639 /********************************************************************/
640 /* Callback functions for libertas.ko                               */
641 /********************************************************************/
642
643 /* Send commands or data packets to the card */
644 static int if_cs_host_to_card(struct lbs_private *priv,
645         u8 type,
646         u8 *buf,
647         u16 nb)
648 {
649         int ret = -1;
650
651         lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
652
653         switch (type) {
654         case MVMS_DAT:
655                 priv->dnld_sent = DNLD_DATA_SENT;
656                 if_cs_send_data(priv, buf, nb);
657                 ret = 0;
658                 break;
659         case MVMS_CMD:
660                 priv->dnld_sent = DNLD_CMD_SENT;
661                 ret = if_cs_send_cmd(priv, buf, nb);
662                 break;
663         default:
664                 lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
665         }
666
667         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
668         return ret;
669 }
670
671
672 /********************************************************************/
673 /* Card Services                                                    */
674 /********************************************************************/
675
676 /*
677  * After a card is removed, if_cs_release() will unregister the
678  * device, and release the PCMCIA configuration.  If the device is
679  * still open, this will be postponed until it is closed.
680  */
681 static void if_cs_release(struct pcmcia_device *p_dev)
682 {
683         struct if_cs_card *card = p_dev->priv;
684
685         lbs_deb_enter(LBS_DEB_CS);
686
687         free_irq(p_dev->irq.AssignedIRQ, card);
688         pcmcia_disable_device(p_dev);
689         if (card->iobase)
690                 ioport_unmap(card->iobase);
691
692         lbs_deb_leave(LBS_DEB_CS);
693 }
694
695
696 /*
697  * This creates an "instance" of the driver, allocating local data
698  * structures for one device.  The device is registered with Card
699  * Services.
700  *
701  * The dev_link structure is initialized, but we don't actually
702  * configure the card at this point -- we wait until we receive a card
703  * insertion event.
704  */
705 static int if_cs_probe(struct pcmcia_device *p_dev)
706 {
707         int ret = -ENOMEM;
708         struct lbs_private *priv;
709         struct if_cs_card *card;
710         /* CIS parsing */
711         tuple_t tuple;
712         cisparse_t parse;
713         cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
714         cistpl_io_t *io = &cfg->io;
715         u_char buf[64];
716
717         lbs_deb_enter(LBS_DEB_CS);
718
719         card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
720         if (!card) {
721                 lbs_pr_err("error in kzalloc\n");
722                 goto out;
723         }
724         card->p_dev = p_dev;
725         p_dev->priv = card;
726
727         p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
728         p_dev->irq.Handler = NULL;
729         p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
730
731         p_dev->conf.Attributes = 0;
732         p_dev->conf.IntType = INT_MEMORY_AND_IO;
733
734         tuple.Attributes = 0;
735         tuple.TupleData = buf;
736         tuple.TupleDataMax = sizeof(buf);
737         tuple.TupleOffset = 0;
738
739         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
740         if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
741             (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
742             (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
743         {
744                 lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
745                 goto out1;
746         }
747
748         p_dev->conf.ConfigIndex = cfg->index;
749
750         /* Do we need to allocate an interrupt? */
751         if (cfg->irq.IRQInfo1) {
752                 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
753         }
754
755         /* IO window settings */
756         if (cfg->io.nwin != 1) {
757                 lbs_pr_err("wrong CIS (check number of IO windows)\n");
758                 ret = -ENODEV;
759                 goto out1;
760         }
761         p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
762         p_dev->io.BasePort1 = io->win[0].base;
763         p_dev->io.NumPorts1 = io->win[0].len;
764
765         /* This reserves IO space but doesn't actually enable it */
766         ret = pcmcia_request_io(p_dev, &p_dev->io);
767         if (ret) {
768                 lbs_pr_err("error in pcmcia_request_io\n");
769                 goto out1;
770         }
771
772         /*
773          * Allocate an interrupt line.  Note that this does not assign
774          * a handler to the interrupt, unless the 'Handler' member of
775          * the irq structure is initialized.
776          */
777         if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
778                 ret = pcmcia_request_irq(p_dev, &p_dev->irq);
779                 if (ret) {
780                         lbs_pr_err("error in pcmcia_request_irq\n");
781                         goto out1;
782                 }
783         }
784
785         /* Initialize io access */
786         card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
787         if (!card->iobase) {
788                 lbs_pr_err("error in ioport_map\n");
789                 ret = -EIO;
790                 goto out1;
791         }
792
793         /*
794          * This actually configures the PCMCIA socket -- setting up
795          * the I/O windows and the interrupt mapping, and putting the
796          * card and host interface into "Memory and IO" mode.
797          */
798         ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
799         if (ret) {
800                 lbs_pr_err("error in pcmcia_request_configuration\n");
801                 goto out2;
802         }
803
804         /* Finally, report what we've done */
805         lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
806                p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
807                p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
808
809
810         /* Load the firmware early, before calling into libertas.ko */
811         ret = if_cs_prog_helper(card);
812         if (ret == 0)
813                 ret = if_cs_prog_real(card);
814         if (ret)
815                 goto out2;
816
817         /* Make this card known to the libertas driver */
818         priv = lbs_add_card(card, &p_dev->dev);
819         if (!priv) {
820                 ret = -ENOMEM;
821                 goto out2;
822         }
823
824         /* Finish setting up fields in lbs_private */
825         card->priv = priv;
826         priv->card = card;
827         priv->hw_host_to_card = if_cs_host_to_card;
828         priv->fw_ready = 1;
829
830         /* Now actually get the IRQ */
831         ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
832                 IRQF_SHARED, DRV_NAME, card);
833         if (ret) {
834                 lbs_pr_err("error in request_irq\n");
835                 goto out3;
836         }
837
838         /* Clear any interrupt cause that happend while sending
839          * firmware/initializing card */
840         if_cs_write16(card, IF_CS_C_INT_CAUSE, IF_CS_C_IC_MASK);
841         if_cs_enable_ints(card);
842
843         /* And finally bring the card up */
844         if (lbs_start_card(priv) != 0) {
845                 lbs_pr_err("could not activate card\n");
846                 goto out3;
847         }
848
849         /* The firmware for the CF card supports powersave */
850         priv->ps_supported = 1;
851
852         ret = 0;
853         goto out;
854
855 out3:
856         lbs_remove_card(priv);
857 out2:
858         ioport_unmap(card->iobase);
859 out1:
860         pcmcia_disable_device(p_dev);
861 out:
862         lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
863         return ret;
864 }
865
866
867 /*
868  * This deletes a driver "instance".  The device is de-registered with
869  * Card Services.  If it has been released, all local data structures
870  * are freed.  Otherwise, the structures will be freed when the device
871  * is released.
872  */
873 static void if_cs_detach(struct pcmcia_device *p_dev)
874 {
875         struct if_cs_card *card = p_dev->priv;
876
877         lbs_deb_enter(LBS_DEB_CS);
878
879         lbs_stop_card(card->priv);
880         lbs_remove_card(card->priv);
881         if_cs_disable_ints(card);
882         if_cs_release(p_dev);
883         kfree(card);
884
885         lbs_deb_leave(LBS_DEB_CS);
886 }
887
888
889
890 /********************************************************************/
891 /* Module initialization                                            */
892 /********************************************************************/
893
894 static struct pcmcia_device_id if_cs_ids[] = {
895         PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
896         PCMCIA_DEVICE_NULL,
897 };
898 MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
899
900
901 static struct pcmcia_driver lbs_driver = {
902         .owner          = THIS_MODULE,
903         .drv            = {
904                 .name   = DRV_NAME,
905         },
906         .probe          = if_cs_probe,
907         .remove         = if_cs_detach,
908         .id_table       = if_cs_ids,
909 };
910
911
912 static int __init if_cs_init(void)
913 {
914         int ret;
915
916         lbs_deb_enter(LBS_DEB_CS);
917         ret = pcmcia_register_driver(&lbs_driver);
918         lbs_deb_leave(LBS_DEB_CS);
919         return ret;
920 }
921
922
923 static void __exit if_cs_exit(void)
924 {
925         lbs_deb_enter(LBS_DEB_CS);
926         pcmcia_unregister_driver(&lbs_driver);
927         lbs_deb_leave(LBS_DEB_CS);
928 }
929
930
931 module_init(if_cs_init);
932 module_exit(if_cs_exit);