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