pcmcia: remove cs_types.h
[linux-2.6/cjktty.git] / drivers / net / wireless / libertas / if_cs.c
blob1d3a7e0e5f10842c1e30e1279662214befe429e0
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/slab.h>
26 #include <linux/delay.h>
27 #include <linux/moduleparam.h>
28 #include <linux/firmware.h>
29 #include <linux/netdevice.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");
51 MODULE_FIRMWARE("libertas_cs_helper.fw");
55 /********************************************************************/
56 /* Data structures */
57 /********************************************************************/
59 struct if_cs_card {
60 struct pcmcia_device *p_dev;
61 struct lbs_private *priv;
62 void __iomem *iobase;
63 bool align_regs;
68 /********************************************************************/
69 /* Hardware access */
70 /********************************************************************/
72 /* This define enables wrapper functions which allow you
73 to dump all register accesses. You normally won't this,
74 except for development */
75 /* #define DEBUG_IO */
77 #ifdef DEBUG_IO
78 static int debug_output = 0;
79 #else
80 /* This way the compiler optimizes the printk's away */
81 #define debug_output 0
82 #endif
84 static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
86 unsigned int val = ioread8(card->iobase + reg);
87 if (debug_output)
88 printk(KERN_INFO "inb %08x<%02x\n", reg, val);
89 return val;
91 static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
93 unsigned int val = ioread16(card->iobase + reg);
94 if (debug_output)
95 printk(KERN_INFO "inw %08x<%04x\n", reg, val);
96 return val;
98 static inline void if_cs_read16_rep(
99 struct if_cs_card *card,
100 uint reg,
101 void *buf,
102 unsigned long count)
104 if (debug_output)
105 printk(KERN_INFO "insw %08x<(0x%lx words)\n",
106 reg, count);
107 ioread16_rep(card->iobase + reg, buf, count);
110 static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
112 if (debug_output)
113 printk(KERN_INFO "outb %08x>%02x\n", reg, val);
114 iowrite8(val, card->iobase + reg);
117 static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
119 if (debug_output)
120 printk(KERN_INFO "outw %08x>%04x\n", reg, val);
121 iowrite16(val, card->iobase + reg);
124 static inline void if_cs_write16_rep(
125 struct if_cs_card *card,
126 uint reg,
127 const void *buf,
128 unsigned long count)
130 if (debug_output)
131 printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
132 reg, count);
133 iowrite16_rep(card->iobase + reg, buf, count);
138 * I know that polling/delaying is frowned upon. However, this procedure
139 * with polling is needed while downloading the firmware. At this stage,
140 * the hardware does unfortunately not create any interrupts.
142 * Fortunately, this function is never used once the firmware is in
143 * the card. :-)
145 * As a reference, see the "Firmware Specification v5.1", page 18
146 * and 19. I did not follow their suggested timing to the word,
147 * but this works nice & fast anyway.
149 static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
151 int i;
153 for (i = 0; i < 100000; i++) {
154 u8 val = if_cs_read8(card, addr);
155 if (val == reg)
156 return 0;
157 udelay(5);
159 return -ETIME;
165 * First the bitmasks for the host/card interrupt/status registers:
167 #define IF_CS_BIT_TX 0x0001
168 #define IF_CS_BIT_RX 0x0002
169 #define IF_CS_BIT_COMMAND 0x0004
170 #define IF_CS_BIT_RESP 0x0008
171 #define IF_CS_BIT_EVENT 0x0010
172 #define IF_CS_BIT_MASK 0x001f
177 * It's not really clear to me what the host status register is for. It
178 * needs to be set almost in union with "host int cause". The following
179 * bits from above are used:
181 * IF_CS_BIT_TX driver downloaded a data packet
182 * IF_CS_BIT_RX driver got a data packet
183 * IF_CS_BIT_COMMAND driver downloaded a command
184 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
185 * IF_CS_BIT_EVENT driver read a host event
187 #define IF_CS_HOST_STATUS 0x00000000
190 * With the host int cause register can the host (that is, Linux) cause
191 * an interrupt in the firmware, to tell the firmware about those events:
193 * IF_CS_BIT_TX a data packet has been downloaded
194 * IF_CS_BIT_RX a received data packet has retrieved
195 * IF_CS_BIT_COMMAND a firmware block or a command has been downloaded
196 * IF_CS_BIT_RESP not used (has some meaning with powerdown)
197 * IF_CS_BIT_EVENT a host event (link lost etc) has been retrieved
199 #define IF_CS_HOST_INT_CAUSE 0x00000002
202 * The host int mask register is used to enable/disable interrupt. However,
203 * I have the suspicion that disabled interrupts are lost.
205 #define IF_CS_HOST_INT_MASK 0x00000004
208 * Used to send or receive data packets:
210 #define IF_CS_WRITE 0x00000016
211 #define IF_CS_WRITE_LEN 0x00000014
212 #define IF_CS_READ 0x00000010
213 #define IF_CS_READ_LEN 0x00000024
216 * Used to send commands (and to send firmware block) and to
217 * receive command responses:
219 #define IF_CS_CMD 0x0000001A
220 #define IF_CS_CMD_LEN 0x00000018
221 #define IF_CS_RESP 0x00000012
222 #define IF_CS_RESP_LEN 0x00000030
225 * The card status registers shows what the card/firmware actually
226 * accepts:
228 * IF_CS_BIT_TX you may send a data packet
229 * IF_CS_BIT_RX you may retrieve a data packet
230 * IF_CS_BIT_COMMAND you may send a command
231 * IF_CS_BIT_RESP you may retrieve a command response
232 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
234 * When reading this register several times, you will get back the same
235 * results --- with one exception: the IF_CS_BIT_EVENT clear itself
236 * automatically.
238 * Not that we don't rely on BIT_RX,_BIT_RESP or BIT_EVENT because
239 * we handle this via the card int cause register.
241 #define IF_CS_CARD_STATUS 0x00000020
242 #define IF_CS_CARD_STATUS_MASK 0x7f00
245 * The card int cause register is used by the card/firmware to notify us
246 * about the following events:
248 * IF_CS_BIT_TX a data packet has successfully been sentx
249 * IF_CS_BIT_RX a data packet has been received and can be retrieved
250 * IF_CS_BIT_COMMAND not used
251 * IF_CS_BIT_RESP the firmware has a command response for us
252 * IF_CS_BIT_EVENT the card has a event for use (link lost, snr low etc)
254 #define IF_CS_CARD_INT_CAUSE 0x00000022
257 * This is used to for handshaking with the card's bootloader/helper image
258 * to synchronize downloading of firmware blocks.
260 #define IF_CS_SQ_READ_LOW 0x00000028
261 #define IF_CS_SQ_HELPER_OK 0x10
264 * The scratch register tells us ...
266 * IF_CS_SCRATCH_BOOT_OK the bootloader runs
267 * IF_CS_SCRATCH_HELPER_OK the helper firmware already runs
269 #define IF_CS_SCRATCH 0x0000003F
270 #define IF_CS_SCRATCH_BOOT_OK 0x00
271 #define IF_CS_SCRATCH_HELPER_OK 0x5a
274 * Used to detect ancient chips:
276 #define IF_CS_PRODUCT_ID 0x0000001C
277 #define IF_CS_CF8385_B1_REV 0x12
278 #define IF_CS_CF8381_B3_REV 0x04
279 #define IF_CS_CF8305_B1_REV 0x03
282 * Used to detect other cards than CF8385 since their revisions of silicon
283 * doesn't match those from CF8385, eg. CF8381 B3 works with this driver.
285 #define CF8305_MANFID 0x02db
286 #define CF8305_CARDID 0x8103
287 #define CF8381_MANFID 0x02db
288 #define CF8381_CARDID 0x6064
289 #define CF8385_MANFID 0x02df
290 #define CF8385_CARDID 0x8103
292 static inline int if_cs_hw_is_cf8305(struct pcmcia_device *p_dev)
294 return (p_dev->manf_id == CF8305_MANFID &&
295 p_dev->card_id == CF8305_CARDID);
298 static inline int if_cs_hw_is_cf8381(struct pcmcia_device *p_dev)
300 return (p_dev->manf_id == CF8381_MANFID &&
301 p_dev->card_id == CF8381_CARDID);
304 static inline int if_cs_hw_is_cf8385(struct pcmcia_device *p_dev)
306 return (p_dev->manf_id == CF8385_MANFID &&
307 p_dev->card_id == CF8385_CARDID);
310 /********************************************************************/
311 /* I/O and interrupt handling */
312 /********************************************************************/
314 static inline void if_cs_enable_ints(struct if_cs_card *card)
316 lbs_deb_enter(LBS_DEB_CS);
317 if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
320 static inline void if_cs_disable_ints(struct if_cs_card *card)
322 lbs_deb_enter(LBS_DEB_CS);
323 if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
327 * Called from if_cs_host_to_card to send a command to the hardware
329 static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
331 struct if_cs_card *card = (struct if_cs_card *)priv->card;
332 int ret = -1;
333 int loops = 0;
335 lbs_deb_enter(LBS_DEB_CS);
336 if_cs_disable_ints(card);
338 /* Is hardware ready? */
339 while (1) {
340 u16 status = if_cs_read16(card, IF_CS_CARD_STATUS);
341 if (status & IF_CS_BIT_COMMAND)
342 break;
343 if (++loops > 100) {
344 lbs_pr_err("card not ready for commands\n");
345 goto done;
347 mdelay(1);
350 if_cs_write16(card, IF_CS_CMD_LEN, nb);
352 if_cs_write16_rep(card, IF_CS_CMD, buf, nb / 2);
353 /* Are we supposed to transfer an odd amount of bytes? */
354 if (nb & 1)
355 if_cs_write8(card, IF_CS_CMD, buf[nb-1]);
357 /* "Assert the download over interrupt command in the Host
358 * status register" */
359 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
361 /* "Assert the download over interrupt command in the Card
362 * interrupt case register" */
363 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
364 ret = 0;
366 done:
367 if_cs_enable_ints(card);
368 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
369 return ret;
373 * Called from if_cs_host_to_card to send a data to the hardware
375 static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
377 struct if_cs_card *card = (struct if_cs_card *)priv->card;
378 u16 status;
380 lbs_deb_enter(LBS_DEB_CS);
381 if_cs_disable_ints(card);
383 status = if_cs_read16(card, IF_CS_CARD_STATUS);
384 BUG_ON((status & IF_CS_BIT_TX) == 0);
386 if_cs_write16(card, IF_CS_WRITE_LEN, nb);
388 /* write even number of bytes, then odd byte if necessary */
389 if_cs_write16_rep(card, IF_CS_WRITE, buf, nb / 2);
390 if (nb & 1)
391 if_cs_write8(card, IF_CS_WRITE, buf[nb-1]);
393 if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
394 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
395 if_cs_enable_ints(card);
397 lbs_deb_leave(LBS_DEB_CS);
401 * Get the command result out of the card.
403 static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
405 unsigned long flags;
406 int ret = -1;
407 u16 status;
409 lbs_deb_enter(LBS_DEB_CS);
411 /* is hardware ready? */
412 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
413 if ((status & IF_CS_BIT_RESP) == 0) {
414 lbs_pr_err("no cmd response in card\n");
415 *len = 0;
416 goto out;
419 *len = if_cs_read16(priv->card, IF_CS_RESP_LEN);
420 if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
421 lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
422 goto out;
425 /* read even number of bytes, then odd byte if necessary */
426 if_cs_read16_rep(priv->card, IF_CS_RESP, data, *len/sizeof(u16));
427 if (*len & 1)
428 data[*len-1] = if_cs_read8(priv->card, IF_CS_RESP);
430 /* This is a workaround for a firmware that reports too much
431 * bytes */
432 *len -= 8;
433 ret = 0;
435 /* Clear this flag again */
436 spin_lock_irqsave(&priv->driver_lock, flags);
437 priv->dnld_sent = DNLD_RES_RECEIVED;
438 spin_unlock_irqrestore(&priv->driver_lock, flags);
440 out:
441 lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
442 return ret;
445 static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
447 struct sk_buff *skb = NULL;
448 u16 len;
449 u8 *data;
451 lbs_deb_enter(LBS_DEB_CS);
453 len = if_cs_read16(priv->card, IF_CS_READ_LEN);
454 if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
455 lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
456 priv->dev->stats.rx_dropped++;
457 goto dat_err;
460 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
461 if (!skb)
462 goto out;
463 skb_put(skb, len);
464 skb_reserve(skb, 2);/* 16 byte align */
465 data = skb->data;
467 /* read even number of bytes, then odd byte if necessary */
468 if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
469 if (len & 1)
470 data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
472 dat_err:
473 if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
474 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
476 out:
477 lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
478 return skb;
481 static irqreturn_t if_cs_interrupt(int irq, void *data)
483 struct if_cs_card *card = data;
484 struct lbs_private *priv = card->priv;
485 u16 cause;
487 lbs_deb_enter(LBS_DEB_CS);
489 /* Ask card interrupt cause register if there is something for us */
490 cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
491 lbs_deb_cs("cause 0x%04x\n", cause);
493 if (cause == 0) {
494 /* Not for us */
495 return IRQ_NONE;
498 if (cause == 0xffff) {
499 /* Read in junk, the card has probably been removed */
500 card->priv->surpriseremoved = 1;
501 return IRQ_HANDLED;
504 if (cause & IF_CS_BIT_RX) {
505 struct sk_buff *skb;
506 lbs_deb_cs("rx packet\n");
507 skb = if_cs_receive_data(priv);
508 if (skb)
509 lbs_process_rxed_packet(priv, skb);
512 if (cause & IF_CS_BIT_TX) {
513 lbs_deb_cs("tx done\n");
514 lbs_host_to_card_done(priv);
517 if (cause & IF_CS_BIT_RESP) {
518 unsigned long flags;
519 u8 i;
521 lbs_deb_cs("cmd resp\n");
522 spin_lock_irqsave(&priv->driver_lock, flags);
523 i = (priv->resp_idx == 0) ? 1 : 0;
524 spin_unlock_irqrestore(&priv->driver_lock, flags);
526 BUG_ON(priv->resp_len[i]);
527 if_cs_receive_cmdres(priv, priv->resp_buf[i],
528 &priv->resp_len[i]);
530 spin_lock_irqsave(&priv->driver_lock, flags);
531 lbs_notify_command_response(priv, i);
532 spin_unlock_irqrestore(&priv->driver_lock, flags);
535 if (cause & IF_CS_BIT_EVENT) {
536 u16 status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
537 if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
538 IF_CS_BIT_EVENT);
539 lbs_queue_event(priv, (status & IF_CS_CARD_STATUS_MASK) >> 8);
542 /* Clear interrupt cause */
543 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);
545 lbs_deb_leave(LBS_DEB_CS);
546 return IRQ_HANDLED;
552 /********************************************************************/
553 /* Firmware */
554 /********************************************************************/
557 * Tries to program the helper firmware.
559 * Return 0 on success
561 static int if_cs_prog_helper(struct if_cs_card *card)
563 int ret = 0;
564 int sent = 0;
565 u8 scratch;
566 const struct firmware *fw;
568 lbs_deb_enter(LBS_DEB_CS);
571 * This is the only place where an unaligned register access happens on
572 * the CF8305 card, therefore for the sake of speed of the driver, we do
573 * the alignment correction here.
575 if (card->align_regs)
576 scratch = if_cs_read16(card, IF_CS_SCRATCH) >> 8;
577 else
578 scratch = if_cs_read8(card, IF_CS_SCRATCH);
580 /* "If the value is 0x5a, the firmware is already
581 * downloaded successfully"
583 if (scratch == IF_CS_SCRATCH_HELPER_OK)
584 goto done;
586 /* "If the value is != 00, it is invalid value of register */
587 if (scratch != IF_CS_SCRATCH_BOOT_OK) {
588 ret = -ENODEV;
589 goto done;
592 /* TODO: make firmware file configurable */
593 ret = request_firmware(&fw, "libertas_cs_helper.fw",
594 &card->p_dev->dev);
595 if (ret) {
596 lbs_pr_err("can't load helper firmware\n");
597 ret = -ENODEV;
598 goto done;
600 lbs_deb_cs("helper size %td\n", fw->size);
602 /* "Set the 5 bytes of the helper image to 0" */
603 /* Not needed, this contains an ARM branch instruction */
605 for (;;) {
606 /* "the number of bytes to send is 256" */
607 int count = 256;
608 int remain = fw->size - sent;
610 if (remain < count)
611 count = remain;
613 /* "write the number of bytes to be sent to the I/O Command
614 * write length register" */
615 if_cs_write16(card, IF_CS_CMD_LEN, count);
617 /* "write this to I/O Command port register as 16 bit writes */
618 if (count)
619 if_cs_write16_rep(card, IF_CS_CMD,
620 &fw->data[sent],
621 count >> 1);
623 /* "Assert the download over interrupt command in the Host
624 * status register" */
625 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
627 /* "Assert the download over interrupt command in the Card
628 * interrupt case register" */
629 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
631 /* "The host polls the Card Status register ... for 50 ms before
632 declaring a failure */
633 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
634 IF_CS_BIT_COMMAND);
635 if (ret < 0) {
636 lbs_pr_err("can't download helper at 0x%x, ret %d\n",
637 sent, ret);
638 goto err_release;
641 if (count == 0)
642 break;
644 sent += count;
647 err_release:
648 release_firmware(fw);
649 done:
650 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
651 return ret;
655 static int if_cs_prog_real(struct if_cs_card *card)
657 const struct firmware *fw;
658 int ret = 0;
659 int retry = 0;
660 int len = 0;
661 int sent;
663 lbs_deb_enter(LBS_DEB_CS);
665 /* TODO: make firmware file configurable */
666 ret = request_firmware(&fw, "libertas_cs.fw",
667 &card->p_dev->dev);
668 if (ret) {
669 lbs_pr_err("can't load firmware\n");
670 ret = -ENODEV;
671 goto done;
673 lbs_deb_cs("fw size %td\n", fw->size);
675 ret = if_cs_poll_while_fw_download(card, IF_CS_SQ_READ_LOW,
676 IF_CS_SQ_HELPER_OK);
677 if (ret < 0) {
678 lbs_pr_err("helper firmware doesn't answer\n");
679 goto err_release;
682 for (sent = 0; sent < fw->size; sent += len) {
683 len = if_cs_read16(card, IF_CS_SQ_READ_LOW);
684 if (len & 1) {
685 retry++;
686 lbs_pr_info("odd, need to retry this firmware block\n");
687 } else {
688 retry = 0;
691 if (retry > 20) {
692 lbs_pr_err("could not download firmware\n");
693 ret = -ENODEV;
694 goto err_release;
696 if (retry) {
697 sent -= len;
701 if_cs_write16(card, IF_CS_CMD_LEN, len);
703 if_cs_write16_rep(card, IF_CS_CMD,
704 &fw->data[sent],
705 (len+1) >> 1);
706 if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
707 if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
709 ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
710 IF_CS_BIT_COMMAND);
711 if (ret < 0) {
712 lbs_pr_err("can't download firmware at 0x%x\n", sent);
713 goto err_release;
717 ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
718 if (ret < 0)
719 lbs_pr_err("firmware download failed\n");
721 err_release:
722 release_firmware(fw);
724 done:
725 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
726 return ret;
731 /********************************************************************/
732 /* Callback functions for libertas.ko */
733 /********************************************************************/
735 /* Send commands or data packets to the card */
736 static int if_cs_host_to_card(struct lbs_private *priv,
737 u8 type,
738 u8 *buf,
739 u16 nb)
741 int ret = -1;
743 lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);
745 switch (type) {
746 case MVMS_DAT:
747 priv->dnld_sent = DNLD_DATA_SENT;
748 if_cs_send_data(priv, buf, nb);
749 ret = 0;
750 break;
751 case MVMS_CMD:
752 priv->dnld_sent = DNLD_CMD_SENT;
753 ret = if_cs_send_cmd(priv, buf, nb);
754 break;
755 default:
756 lbs_pr_err("%s: unsupported type %d\n", __func__, type);
759 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
760 return ret;
764 /********************************************************************/
765 /* Card Services */
766 /********************************************************************/
769 * After a card is removed, if_cs_release() will unregister the
770 * device, and release the PCMCIA configuration. If the device is
771 * still open, this will be postponed until it is closed.
773 static void if_cs_release(struct pcmcia_device *p_dev)
775 struct if_cs_card *card = p_dev->priv;
777 lbs_deb_enter(LBS_DEB_CS);
779 free_irq(p_dev->irq, card);
780 pcmcia_disable_device(p_dev);
781 if (card->iobase)
782 ioport_unmap(card->iobase);
784 lbs_deb_leave(LBS_DEB_CS);
789 * This creates an "instance" of the driver, allocating local data
790 * structures for one device. The device is registered with Card
791 * Services.
793 * The dev_link structure is initialized, but we don't actually
794 * configure the card at this point -- we wait until we receive a card
795 * insertion event.
798 static int if_cs_ioprobe(struct pcmcia_device *p_dev,
799 cistpl_cftable_entry_t *cfg,
800 cistpl_cftable_entry_t *dflt,
801 unsigned int vcc,
802 void *priv_data)
804 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
805 p_dev->io.BasePort1 = cfg->io.win[0].base;
806 p_dev->io.NumPorts1 = cfg->io.win[0].len;
808 /* Do we need to allocate an interrupt? */
809 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
811 /* IO window settings */
812 if (cfg->io.nwin != 1) {
813 lbs_pr_err("wrong CIS (check number of IO windows)\n");
814 return -ENODEV;
817 /* This reserves IO space but doesn't actually enable it */
818 return pcmcia_request_io(p_dev, &p_dev->io);
821 static int if_cs_probe(struct pcmcia_device *p_dev)
823 int ret = -ENOMEM;
824 unsigned int prod_id;
825 struct lbs_private *priv;
826 struct if_cs_card *card;
828 lbs_deb_enter(LBS_DEB_CS);
830 card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
831 if (!card) {
832 lbs_pr_err("error in kzalloc\n");
833 goto out;
835 card->p_dev = p_dev;
836 p_dev->priv = card;
838 p_dev->conf.Attributes = 0;
839 p_dev->conf.IntType = INT_MEMORY_AND_IO;
841 if (pcmcia_loop_config(p_dev, if_cs_ioprobe, NULL)) {
842 lbs_pr_err("error in pcmcia_loop_config\n");
843 goto out1;
848 * Allocate an interrupt line. Note that this does not assign
849 * a handler to the interrupt, unless the 'Handler' member of
850 * the irq structure is initialized.
852 if (!p_dev->irq)
853 goto out1;
855 /* Initialize io access */
856 card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
857 if (!card->iobase) {
858 lbs_pr_err("error in ioport_map\n");
859 ret = -EIO;
860 goto out1;
864 * This actually configures the PCMCIA socket -- setting up
865 * the I/O windows and the interrupt mapping, and putting the
866 * card and host interface into "Memory and IO" mode.
868 ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
869 if (ret) {
870 lbs_pr_err("error in pcmcia_request_configuration\n");
871 goto out2;
874 /* Finally, report what we've done */
875 lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
876 p_dev->irq, p_dev->io.BasePort1,
877 p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);
880 * Most of the libertas cards can do unaligned register access, but some
881 * weird ones can not. That's especially true for the CF8305 card.
883 card->align_regs = 0;
885 /* Check if we have a current silicon */
886 prod_id = if_cs_read8(card, IF_CS_PRODUCT_ID);
887 if (if_cs_hw_is_cf8305(p_dev)) {
888 card->align_regs = 1;
889 if (prod_id < IF_CS_CF8305_B1_REV) {
890 lbs_pr_err("old chips like 8305 rev B3 "
891 "aren't supported\n");
892 ret = -ENODEV;
893 goto out2;
897 if (if_cs_hw_is_cf8381(p_dev) && prod_id < IF_CS_CF8381_B3_REV) {
898 lbs_pr_err("old chips like 8381 rev B3 aren't supported\n");
899 ret = -ENODEV;
900 goto out2;
903 if (if_cs_hw_is_cf8385(p_dev) && prod_id < IF_CS_CF8385_B1_REV) {
904 lbs_pr_err("old chips like 8385 rev B1 aren't supported\n");
905 ret = -ENODEV;
906 goto out2;
909 /* Load the firmware early, before calling into libertas.ko */
910 ret = if_cs_prog_helper(card);
911 if (ret == 0 && !if_cs_hw_is_cf8305(p_dev))
912 ret = if_cs_prog_real(card);
913 if (ret)
914 goto out2;
916 /* Make this card known to the libertas driver */
917 priv = lbs_add_card(card, &p_dev->dev);
918 if (!priv) {
919 ret = -ENOMEM;
920 goto out2;
923 /* Finish setting up fields in lbs_private */
924 card->priv = priv;
925 priv->card = card;
926 priv->hw_host_to_card = if_cs_host_to_card;
927 priv->enter_deep_sleep = NULL;
928 priv->exit_deep_sleep = NULL;
929 priv->reset_deep_sleep_wakeup = NULL;
930 priv->fw_ready = 1;
932 /* Now actually get the IRQ */
933 ret = request_irq(p_dev->irq, if_cs_interrupt,
934 IRQF_SHARED, DRV_NAME, card);
935 if (ret) {
936 lbs_pr_err("error in request_irq\n");
937 goto out3;
940 /* Clear any interrupt cause that happend while sending
941 * firmware/initializing card */
942 if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
943 if_cs_enable_ints(card);
945 /* And finally bring the card up */
946 if (lbs_start_card(priv) != 0) {
947 lbs_pr_err("could not activate card\n");
948 goto out3;
951 ret = 0;
952 goto out;
954 out3:
955 lbs_remove_card(priv);
956 out2:
957 ioport_unmap(card->iobase);
958 out1:
959 pcmcia_disable_device(p_dev);
960 out:
961 lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
962 return ret;
967 * This deletes a driver "instance". The device is de-registered with
968 * Card Services. If it has been released, all local data structures
969 * are freed. Otherwise, the structures will be freed when the device
970 * is released.
972 static void if_cs_detach(struct pcmcia_device *p_dev)
974 struct if_cs_card *card = p_dev->priv;
976 lbs_deb_enter(LBS_DEB_CS);
978 lbs_stop_card(card->priv);
979 lbs_remove_card(card->priv);
980 if_cs_disable_ints(card);
981 if_cs_release(p_dev);
982 kfree(card);
984 lbs_deb_leave(LBS_DEB_CS);
989 /********************************************************************/
990 /* Module initialization */
991 /********************************************************************/
993 static struct pcmcia_device_id if_cs_ids[] = {
994 PCMCIA_DEVICE_MANF_CARD(CF8305_MANFID, CF8305_CARDID),
995 PCMCIA_DEVICE_MANF_CARD(CF8381_MANFID, CF8381_CARDID),
996 PCMCIA_DEVICE_MANF_CARD(CF8385_MANFID, CF8385_CARDID),
997 PCMCIA_DEVICE_NULL,
999 MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);
1002 static struct pcmcia_driver lbs_driver = {
1003 .owner = THIS_MODULE,
1004 .drv = {
1005 .name = DRV_NAME,
1007 .probe = if_cs_probe,
1008 .remove = if_cs_detach,
1009 .id_table = if_cs_ids,
1013 static int __init if_cs_init(void)
1015 int ret;
1017 lbs_deb_enter(LBS_DEB_CS);
1018 ret = pcmcia_register_driver(&lbs_driver);
1019 lbs_deb_leave(LBS_DEB_CS);
1020 return ret;
1024 static void __exit if_cs_exit(void)
1026 lbs_deb_enter(LBS_DEB_CS);
1027 pcmcia_unregister_driver(&lbs_driver);
1028 lbs_deb_leave(LBS_DEB_CS);
1032 module_init(if_cs_init);
1033 module_exit(if_cs_exit);