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