pcmcia: CompactFlash driver for PA Semi Electra boards
[pv_ops_mirror.git] / drivers / net / wireless / libertas / if_cs.c
blob0360cad363a8dacd314be2fb57272daed142c1af
1 /*
3 Driver for the Marvell 8385 based compact flash WLAN cards.
5 (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de>
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.
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.
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.
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>
30 #include <pcmcia/cs_types.h>
31 #include <pcmcia/cs.h>
32 #include <pcmcia/cistpl.h>
33 #include <pcmcia/ds.h>
35 #include <linux/io.h>
37 #define DRV_NAME "libertas_cs"
39 #include "decl.h"
40 #include "defs.h"
41 #include "dev.h"
44 /********************************************************************/
45 /* Module stuff */
46 /********************************************************************/
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");
54 /********************************************************************/
55 /* Data structures */
56 /********************************************************************/
58 struct if_cs_card {
59 struct pcmcia_device *p_dev;
60 wlan_private *priv;
61 void __iomem *iobase;
66 /********************************************************************/
67 /* Hardware access */
68 /********************************************************************/
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 */
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
82 static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
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;
89 static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
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;
96 static inline void if_cs_read16_rep(
97 struct if_cs_card *card,
98 uint reg,
99 void *buf,
100 unsigned long count)
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);
108 static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
110 if (debug_output)
111 printk(KERN_INFO "##outb %08x>%02x\n", reg, val);
112 iowrite8(val, card->iobase + reg);
115 static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
117 if (debug_output)
118 printk(KERN_INFO "##outw %08x>%04x\n", reg, val);
119 iowrite16(val, card->iobase + reg);
122 static inline void if_cs_write16_rep(
123 struct if_cs_card *card,
124 uint reg,
125 void *buf,
126 unsigned long count)
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);
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.
140 * Fortunately, this function is never used once the firmware is in
141 * the card. :-)
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.
147 static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
149 int i;
151 for (i = 0; i < 500; i++) {
152 u8 val = if_cs_read8(card, addr);
153 if (val == reg)
154 return i;
155 udelay(100);
157 return -ETIME;
162 /* Host control registers and their bit definitions */
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
169 #define IF_CS_H_INT_CAUSE 0x00000002
170 #define IF_CS_H_IC_TX_OVER 0x0001
171 #define IF_CS_H_IC_RX_OVER 0x0002
172 #define IF_CS_H_IC_DNLD_OVER 0x0004
173 #define IF_CS_H_IC_HOST_EVENT 0x0008
174 #define IF_CS_H_IC_MASK 0x001f
176 #define IF_CS_H_INT_MASK 0x00000004
177 #define IF_CS_H_IM_MASK 0x001f
179 #define IF_CS_H_WRITE_LEN 0x00000014
181 #define IF_CS_H_WRITE 0x00000016
183 #define IF_CS_H_CMD_LEN 0x00000018
185 #define IF_CS_H_CMD 0x0000001A
187 #define IF_CS_C_READ_LEN 0x00000024
189 #define IF_CS_H_READ 0x00000010
191 /* Card control registers and their bit definitions */
193 #define IF_CS_C_STATUS 0x00000020
194 #define IF_CS_C_S_TX_DNLD_RDY 0x0001
195 #define IF_CS_C_S_RX_UPLD_RDY 0x0002
196 #define IF_CS_C_S_CMD_DNLD_RDY 0x0004
197 #define IF_CS_C_S_CMD_UPLD_RDY 0x0008
198 #define IF_CS_C_S_CARDEVENT 0x0010
199 #define IF_CS_C_S_MASK 0x001f
200 #define IF_CS_C_S_STATUS_MASK 0x7f00
201 /* The following definitions should be the same as the MRVDRV_ ones */
203 #if MRVDRV_CMD_DNLD_RDY != IF_CS_C_S_CMD_DNLD_RDY
204 #error MRVDRV_CMD_DNLD_RDY and IF_CS_C_S_CMD_DNLD_RDY not in sync
205 #endif
206 #if MRVDRV_CMD_UPLD_RDY != IF_CS_C_S_CMD_UPLD_RDY
207 #error MRVDRV_CMD_UPLD_RDY and IF_CS_C_S_CMD_UPLD_RDY not in sync
208 #endif
209 #if MRVDRV_CARDEVENT != IF_CS_C_S_CARDEVENT
210 #error MRVDRV_CARDEVENT and IF_CS_C_S_CARDEVENT not in sync
211 #endif
213 #define IF_CS_C_INT_CAUSE 0x00000022
214 #define IF_CS_C_IC_MASK 0x001f
216 #define IF_CS_C_SQ_READ_LOW 0x00000028
217 #define IF_CS_C_SQ_HELPER_OK 0x10
219 #define IF_CS_C_CMD_LEN 0x00000030
221 #define IF_CS_C_CMD 0x00000012
223 #define IF_CS_SCRATCH 0x0000003F
227 /********************************************************************/
228 /* Interrupts */
229 /********************************************************************/
231 static inline void if_cs_enable_ints(struct if_cs_card *card)
233 lbs_deb_enter(LBS_DEB_CS);
234 if_cs_write16(card, IF_CS_H_INT_MASK, 0);
237 static inline void if_cs_disable_ints(struct if_cs_card *card)
239 lbs_deb_enter(LBS_DEB_CS);
240 if_cs_write16(card, IF_CS_H_INT_MASK, IF_CS_H_IM_MASK);
243 static irqreturn_t if_cs_interrupt(int irq, void *data)
245 struct if_cs_card *card = (struct if_cs_card *)data;
246 u16 int_cause;
248 lbs_deb_enter(LBS_DEB_CS);
250 int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE);
251 if(int_cause == 0x0) {
252 /* Not for us */
253 return IRQ_NONE;
255 } else if(int_cause == 0xffff) {
256 /* Read in junk, the card has probably been removed */
257 card->priv->adapter->surpriseremoved = 1;
259 } else {
260 if(int_cause & IF_CS_H_IC_TX_OVER) {
261 card->priv->dnld_sent = DNLD_RES_RECEIVED;
262 if (!card->priv->adapter->cur_cmd)
263 wake_up_interruptible(&card->priv->waitq);
265 if (card->priv->adapter->connect_status == LIBERTAS_CONNECTED)
266 netif_wake_queue(card->priv->dev);
269 /* clear interrupt */
270 if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause & IF_CS_C_IC_MASK);
273 libertas_interrupt(card->priv->dev);
275 return IRQ_HANDLED;
281 /********************************************************************/
282 /* I/O */
283 /********************************************************************/
286 * Called from if_cs_host_to_card to send a command to the hardware
288 static int if_cs_send_cmd(wlan_private *priv, u8 *buf, u16 nb)
290 struct if_cs_card *card = (struct if_cs_card *)priv->card;
291 int ret = -1;
292 int loops = 0;
294 lbs_deb_enter(LBS_DEB_CS);
296 /* Is hardware ready? */
297 while (1) {
298 u16 val = if_cs_read16(card, IF_CS_C_STATUS);
299 if (val & IF_CS_C_S_CMD_DNLD_RDY)
300 break;
301 if (++loops > 100) {
302 lbs_pr_err("card not ready for commands\n");
303 goto done;
305 mdelay(1);
308 if_cs_write16(card, IF_CS_H_CMD_LEN, nb);
310 if_cs_write16_rep(card, IF_CS_H_CMD, buf, nb / 2);
311 /* Are we supposed to transfer an odd amount of bytes? */
312 if (nb & 1)
313 if_cs_write8(card, IF_CS_H_CMD, buf[nb-1]);
315 /* "Assert the download over interrupt command in the Host
316 * status register" */
317 if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
319 /* "Assert the download over interrupt command in the Card
320 * interrupt case register" */
321 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
322 ret = 0;
324 done:
325 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
326 return ret;
331 * Called from if_cs_host_to_card to send a data to the hardware
333 static void if_cs_send_data(wlan_private *priv, u8 *buf, u16 nb)
335 struct if_cs_card *card = (struct if_cs_card *)priv->card;
337 lbs_deb_enter(LBS_DEB_CS);
339 if_cs_write16(card, IF_CS_H_WRITE_LEN, nb);
341 /* write even number of bytes, then odd byte if necessary */
342 if_cs_write16_rep(card, IF_CS_H_WRITE, buf, nb / 2);
343 if (nb & 1)
344 if_cs_write8(card, IF_CS_H_WRITE, buf[nb-1]);
346 if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_TX_OVER);
347 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_STATUS_TX_OVER);
349 lbs_deb_leave(LBS_DEB_CS);
354 * Get the command result out of the card.
356 static int if_cs_receive_cmdres(wlan_private *priv, u8* data, u32 *len)
358 int ret = -1;
359 u16 val;
361 lbs_deb_enter(LBS_DEB_CS);
363 /* is hardware ready? */
364 val = if_cs_read16(priv->card, IF_CS_C_STATUS);
365 if ((val & IF_CS_C_S_CMD_UPLD_RDY) == 0) {
366 lbs_pr_err("card not ready for CMD\n");
367 goto out;
370 *len = if_cs_read16(priv->card, IF_CS_C_CMD_LEN);
371 if ((*len == 0) || (*len > MRVDRV_SIZE_OF_CMD_BUFFER)) {
372 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
373 goto out;
376 /* read even number of bytes, then odd byte if necessary */
377 if_cs_read16_rep(priv->card, IF_CS_C_CMD, data, *len/sizeof(u16));
378 if (*len & 1)
379 data[*len-1] = if_cs_read8(priv->card, IF_CS_C_CMD);
381 ret = 0;
382 out:
383 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
384 return ret;
388 static struct sk_buff *if_cs_receive_data(wlan_private *priv)
390 struct sk_buff *skb = NULL;
391 u16 len;
392 u8 *data;
394 lbs_deb_enter(LBS_DEB_CS);
396 len = if_cs_read16(priv->card, IF_CS_C_READ_LEN);
397 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
398 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
399 priv->stats.rx_dropped++;
400 printk(KERN_INFO "##HS %s:%d TODO\n", __FUNCTION__, __LINE__);
401 goto dat_err;
404 //TODO: skb = dev_alloc_skb(len+ETH_FRAME_LEN+MRVDRV_SNAP_HEADER_LEN+EXTRA_LEN);
405 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
406 if (!skb)
407 goto out;
408 skb_put(skb, len);
409 skb_reserve(skb, 2);/* 16 byte align */
410 data = skb->data;
412 /* read even number of bytes, then odd byte if necessary */
413 if_cs_read16_rep(priv->card, IF_CS_H_READ, data, len/sizeof(u16));
414 if (len & 1)
415 data[len-1] = if_cs_read8(priv->card, IF_CS_H_READ);
417 dat_err:
418 if_cs_write16(priv->card, IF_CS_H_STATUS, IF_CS_H_STATUS_RX_OVER);
419 if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_RX_OVER);
421 out:
422 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
423 return skb;
428 /********************************************************************/
429 /* Firmware */
430 /********************************************************************/
433 * Tries to program the helper firmware.
435 * Return 0 on success
437 static int if_cs_prog_helper(struct if_cs_card *card)
439 int ret = 0;
440 int sent = 0;
441 u8 scratch;
442 const struct firmware *fw;
444 lbs_deb_enter(LBS_DEB_CS);
446 scratch = if_cs_read8(card, IF_CS_SCRATCH);
448 /* "If the value is 0x5a, the firmware is already
449 * downloaded successfully"
451 if (scratch == 0x5a)
452 goto done;
454 /* "If the value is != 00, it is invalid value of register */
455 if (scratch != 0x00) {
456 ret = -ENODEV;
457 goto done;
460 /* TODO: make firmware file configurable */
461 ret = request_firmware(&fw, "libertas_cs_helper.fw",
462 &handle_to_dev(card->p_dev));
463 if (ret) {
464 lbs_pr_err("can't load helper firmware\n");
465 ret = -ENODEV;
466 goto done;
468 lbs_deb_cs("helper size %td\n", fw->size);
470 /* "Set the 5 bytes of the helper image to 0" */
471 /* Not needed, this contains an ARM branch instruction */
473 for (;;) {
474 /* "the number of bytes to send is 256" */
475 int count = 256;
476 int remain = fw->size - sent;
478 if (remain < count)
479 count = remain;
480 /* printk(KERN_INFO "//HS %d loading %d of %d bytes\n",
481 __LINE__, sent, fw->size); */
483 /* "write the number of bytes to be sent to the I/O Command
484 * write length register" */
485 if_cs_write16(card, IF_CS_H_CMD_LEN, count);
487 /* "write this to I/O Command port register as 16 bit writes */
488 if (count)
489 if_cs_write16_rep(card, IF_CS_H_CMD,
490 &fw->data[sent],
491 count >> 1);
493 /* "Assert the download over interrupt command in the Host
494 * status register" */
495 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
497 /* "Assert the download over interrupt command in the Card
498 * interrupt case register" */
499 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
501 /* "The host polls the Card Status register ... for 50 ms before
502 declaring a failure */
503 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
504 IF_CS_C_S_CMD_DNLD_RDY);
505 if (ret < 0) {
506 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
507 sent, ret);
508 goto done;
511 if (count == 0)
512 break;
514 sent += count;
517 release_firmware(fw);
518 ret = 0;
520 done:
521 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
522 return ret;
526 static int if_cs_prog_real(struct if_cs_card *card)
528 const struct firmware *fw;
529 int ret = 0;
530 int retry = 0;
531 int len = 0;
532 int sent;
534 lbs_deb_enter(LBS_DEB_CS);
536 /* TODO: make firmware file configurable */
537 ret = request_firmware(&fw, "libertas_cs.fw",
538 &handle_to_dev(card->p_dev));
539 if (ret) {
540 lbs_pr_err("can't load firmware\n");
541 ret = -ENODEV;
542 goto done;
544 lbs_deb_cs("fw size %td\n", fw->size);
546 ret = if_cs_poll_while_fw_download(card, IF_CS_C_SQ_READ_LOW, IF_CS_C_SQ_HELPER_OK);
547 if (ret < 0) {
548 int i;
549 lbs_pr_err("helper firmware doesn't answer\n");
550 for (i = 0; i < 0x50; i += 2)
551 printk(KERN_INFO "## HS %02x: %04x\n",
552 i, if_cs_read16(card, i));
553 goto err_release;
556 for (sent = 0; sent < fw->size; sent += len) {
557 len = if_cs_read16(card, IF_CS_C_SQ_READ_LOW);
558 /* printk(KERN_INFO "//HS %d loading %d of %d bytes\n",
559 __LINE__, sent, fw->size); */
560 if (len & 1) {
561 retry++;
562 lbs_pr_info("odd, need to retry this firmware block\n");
563 } else {
564 retry = 0;
567 if (retry > 20) {
568 lbs_pr_err("could not download firmware\n");
569 ret = -ENODEV;
570 goto err_release;
572 if (retry) {
573 sent -= len;
577 if_cs_write16(card, IF_CS_H_CMD_LEN, len);
579 if_cs_write16_rep(card, IF_CS_H_CMD,
580 &fw->data[sent],
581 (len+1) >> 1);
582 if_cs_write8(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);
583 if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);
585 ret = if_cs_poll_while_fw_download(card, IF_CS_C_STATUS,
586 IF_CS_C_S_CMD_DNLD_RDY);
587 if (ret < 0) {
588 lbs_pr_err("can't download firmware at 0x%x\n", sent);
589 goto err_release;
593 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
594 if (ret < 0) {
595 lbs_pr_err("firmware download failed\n");
596 goto err_release;
599 ret = 0;
600 goto done;
603 err_release:
604 release_firmware(fw);
606 done:
607 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
608 return ret;
613 /********************************************************************/
614 /* Callback functions for libertas.ko */
615 /********************************************************************/
617 /* Send commands or data packets to the card */
618 static int if_cs_host_to_card(wlan_private *priv, u8 type, u8 *buf, u16 nb)
620 int ret = -1;
622 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
624 switch (type) {
625 case MVMS_DAT:
626 priv->dnld_sent = DNLD_DATA_SENT;
627 if_cs_send_data(priv, buf, nb);
628 ret = 0;
629 break;
630 case MVMS_CMD:
631 priv->dnld_sent = DNLD_CMD_SENT;
632 ret = if_cs_send_cmd(priv, buf, nb);
633 break;
634 default:
635 lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
638 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
639 return ret;
643 static int if_cs_get_int_status(wlan_private *priv, u8 *ireg)
645 struct if_cs_card *card = (struct if_cs_card *)priv->card;
646 //wlan_adapter *adapter = priv->adapter;
647 int ret = 0;
648 u16 int_cause;
649 u8 *cmdbuf;
650 *ireg = 0;
652 lbs_deb_enter(LBS_DEB_CS);
654 if (priv->adapter->surpriseremoved)
655 goto out;
657 int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE) & IF_CS_C_IC_MASK;
658 if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause);
660 *ireg = if_cs_read16(card, IF_CS_C_STATUS) & IF_CS_C_S_MASK;
662 if (!*ireg)
663 goto sbi_get_int_status_exit;
665 sbi_get_int_status_exit:
667 /* is there a data packet for us? */
668 if (*ireg & IF_CS_C_S_RX_UPLD_RDY) {
669 struct sk_buff *skb = if_cs_receive_data(priv);
670 libertas_process_rxed_packet(priv, skb);
671 *ireg &= ~IF_CS_C_S_RX_UPLD_RDY;
674 if (*ireg & IF_CS_C_S_TX_DNLD_RDY) {
675 priv->dnld_sent = DNLD_RES_RECEIVED;
678 /* Card has a command result for us */
679 if (*ireg & IF_CS_C_S_CMD_UPLD_RDY) {
680 spin_lock(&priv->adapter->driver_lock);
681 if (!priv->adapter->cur_cmd) {
682 cmdbuf = priv->upld_buf;
683 priv->adapter->hisregcpy &= ~IF_CS_C_S_RX_UPLD_RDY;
684 } else {
685 cmdbuf = priv->adapter->cur_cmd->bufvirtualaddr;
688 ret = if_cs_receive_cmdres(priv, cmdbuf, &priv->upld_len);
689 spin_unlock(&priv->adapter->driver_lock);
690 if (ret < 0)
691 lbs_pr_err("could not receive cmd from card\n");
694 out:
695 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, ireg 0x%x, hisregcpy 0x%x", ret, *ireg, priv->adapter->hisregcpy);
696 return ret;
700 static int if_cs_read_event_cause(wlan_private *priv)
702 lbs_deb_enter(LBS_DEB_CS);
704 priv->adapter->eventcause = (if_cs_read16(priv->card, IF_CS_C_STATUS) & IF_CS_C_S_STATUS_MASK) >> 5;
705 if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_HOST_EVENT);
707 return 0;
712 /********************************************************************/
713 /* Card Services */
714 /********************************************************************/
717 * After a card is removed, if_cs_release() will unregister the
718 * device, and release the PCMCIA configuration. If the device is
719 * still open, this will be postponed until it is closed.
721 static void if_cs_release(struct pcmcia_device *p_dev)
723 struct if_cs_card *card = p_dev->priv;
725 lbs_deb_enter(LBS_DEB_CS);
727 pcmcia_disable_device(p_dev);
728 free_irq(p_dev->irq.AssignedIRQ, card);
729 if (card->iobase)
730 ioport_unmap(card->iobase);
732 lbs_deb_leave(LBS_DEB_CS);
737 * This creates an "instance" of the driver, allocating local data
738 * structures for one device. The device is registered with Card
739 * Services.
741 * The dev_link structure is initialized, but we don't actually
742 * configure the card at this point -- we wait until we receive a card
743 * insertion event.
745 static int if_cs_probe(struct pcmcia_device *p_dev)
747 int ret = -ENOMEM;
748 wlan_private *priv;
749 struct if_cs_card *card;
750 /* CIS parsing */
751 tuple_t tuple;
752 cisparse_t parse;
753 cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
754 cistpl_io_t *io = &cfg->io;
755 u_char buf[64];
757 lbs_deb_enter(LBS_DEB_CS);
759 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
760 if (!card) {
761 lbs_pr_err("error in kzalloc\n");
762 goto out;
764 card->p_dev = p_dev;
765 p_dev->priv = card;
767 p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
768 p_dev->irq.Handler = NULL;
769 p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
771 p_dev->conf.Attributes = 0;
772 p_dev->conf.IntType = INT_MEMORY_AND_IO;
774 tuple.Attributes = 0;
775 tuple.TupleData = buf;
776 tuple.TupleDataMax = sizeof(buf);
777 tuple.TupleOffset = 0;
779 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
780 if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
781 (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
782 (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
784 lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
785 goto out1;
788 p_dev->conf.ConfigIndex = cfg->index;
790 /* Do we need to allocate an interrupt? */
791 if (cfg->irq.IRQInfo1) {
792 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
795 /* IO window settings */
796 if (cfg->io.nwin != 1) {
797 lbs_pr_err("wrong CIS (check number of IO windows)\n");
798 ret = -ENODEV;
799 goto out1;
801 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
802 p_dev->io.BasePort1 = io->win[0].base;
803 p_dev->io.NumPorts1 = io->win[0].len;
805 /* This reserves IO space but doesn't actually enable it */
806 ret = pcmcia_request_io(p_dev, &p_dev->io);
807 if (ret) {
808 lbs_pr_err("error in pcmcia_request_io\n");
809 goto out1;
813 * Allocate an interrupt line. Note that this does not assign
814 * a handler to the interrupt, unless the 'Handler' member of
815 * the irq structure is initialized.
817 if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
818 ret = pcmcia_request_irq(p_dev, &p_dev->irq);
819 if (ret) {
820 lbs_pr_err("error in pcmcia_request_irq\n");
821 goto out1;
825 /* Initialize io access */
826 card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
827 if (!card->iobase) {
828 lbs_pr_err("error in ioport_map\n");
829 ret = -EIO;
830 goto out1;
834 * This actually configures the PCMCIA socket -- setting up
835 * the I/O windows and the interrupt mapping, and putting the
836 * card and host interface into "Memory and IO" mode.
838 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
839 if (ret) {
840 lbs_pr_err("error in pcmcia_request_configuration\n");
841 goto out2;
844 /* Finally, report what we've done */
845 lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
846 p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
847 p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
850 /* Load the firmware early, before calling into libertas.ko */
851 ret = if_cs_prog_helper(card);
852 if (ret == 0)
853 ret = if_cs_prog_real(card);
854 if (ret)
855 goto out2;
857 /* Make this card known to the libertas driver */
858 priv = libertas_add_card(card, &p_dev->dev);
859 if (!priv) {
860 ret = -ENOMEM;
861 goto out2;
864 /* Store pointers to our call-back functions */
865 card->priv = priv;
866 priv->card = card;
867 priv->hw_host_to_card = if_cs_host_to_card;
868 priv->hw_get_int_status = if_cs_get_int_status;
869 priv->hw_read_event_cause = if_cs_read_event_cause;
871 priv->adapter->fw_ready = 1;
873 /* Now actually get the IRQ */
874 ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
875 IRQF_SHARED, DRV_NAME, card);
876 if (ret) {
877 lbs_pr_err("error in request_irq\n");
878 goto out3;
881 if_cs_enable_ints(card);
883 /* And finally bring the card up */
884 if (libertas_start_card(priv) != 0) {
885 lbs_pr_err("could not activate card\n");
886 goto out3;
889 ret = 0;
890 goto out;
892 out3:
893 libertas_remove_card(priv);
894 out2:
895 ioport_unmap(card->iobase);
896 out1:
897 pcmcia_disable_device(p_dev);
898 out:
899 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
900 return ret;
905 * This deletes a driver "instance". The device is de-registered with
906 * Card Services. If it has been released, all local data structures
907 * are freed. Otherwise, the structures will be freed when the device
908 * is released.
910 static void if_cs_detach(struct pcmcia_device *p_dev)
912 struct if_cs_card *card = p_dev->priv;
914 lbs_deb_enter(LBS_DEB_CS);
916 libertas_stop_card(card->priv);
917 libertas_remove_card(card->priv);
918 if_cs_disable_ints(card);
919 if_cs_release(p_dev);
920 kfree(card);
922 lbs_deb_leave(LBS_DEB_CS);
927 /********************************************************************/
928 /* Module initialization */
929 /********************************************************************/
931 static struct pcmcia_device_id if_cs_ids[] = {
932 PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
933 PCMCIA_DEVICE_NULL,
935 MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
938 static struct pcmcia_driver libertas_driver = {
939 .owner = THIS_MODULE,
940 .drv = {
941 .name = DRV_NAME,
943 .probe = if_cs_probe,
944 .remove = if_cs_detach,
945 .id_table = if_cs_ids,
949 static int __init if_cs_init(void)
951 int ret;
953 lbs_deb_enter(LBS_DEB_CS);
954 ret = pcmcia_register_driver(&libertas_driver);
955 lbs_deb_leave(LBS_DEB_CS);
956 return ret;
960 static void __exit if_cs_exit(void)
962 lbs_deb_enter(LBS_DEB_CS);
963 pcmcia_unregister_driver(&libertas_driver);
964 lbs_deb_leave(LBS_DEB_CS);
968 module_init(if_cs_init);
969 module_exit(if_cs_exit);