4 * Copyright (C) 2002-2003 Pavel Roskin <proski@gnu.org>
5 * Portions based on orinoco_cs.c, Copyright (C) David Gibson,
6 * Linuxcare Australia <hermes@gibson.dropbear.id.au>
7 * Portions based on Spectrum24tDnld.c, Copyright (C)
10 * Copyright notice & release notes in file orinoco.c
13 #include <linux/config.h>
14 #ifdef __IN_PCMCIA_PACKAGE__
15 #include <pcmcia/k_compat.h>
16 #endif /* __IN_PCMCIA_PACKAGE__ */
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/ptrace.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/ioport.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/etherdevice.h>
29 #include <linux/wireless.h>
31 #include <pcmcia/version.h>
32 #include <pcmcia/cs_types.h>
33 #include <pcmcia/cs.h>
34 #include <pcmcia/cistpl.h>
35 #include <pcmcia/cisreg.h>
36 #include <pcmcia/ds.h>
38 #include <asm/uaccess.h>
40 #include <asm/system.h>
44 /********************************************************************/
46 /********************************************************************/
48 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
49 MODULE_DESCRIPTION("Driver for Symbol Spectrum24 cards with firmware downloader");
51 MODULE_LICENSE("Dual MPL/GPL");
54 /* Module parameters */
56 /* The old way: bit map of interrupts to choose from */
57 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
58 static uint irq_mask = 0xdeb8;
59 /* Newer, simpler way of listing specific interrupts */
60 static int irq_list[4] = { -1 };
62 /* Some D-Link cards have buggy CIS. They do work at 5v properly, but
63 * don't have any CIS entry for it. This workaround it... */
64 static int ignore_cis_vcc; /* = 0 */
66 MODULE_PARM(irq_mask, "i");
67 MODULE_PARM(irq_list, "1-4i");
68 MODULE_PARM(ignore_cis_vcc, "i");
70 /********************************************************************/
72 /********************************************************************/
75 * The dev_info variable is the "key" that is used to match up this
76 * device driver with appropriate cards, through the card
77 * configuration database.
79 static dev_info_t dev_info = "spectrum_cs";
81 /********************************************************************/
83 /********************************************************************/
85 /* PCMCIA specific device information (goes in the card field of
86 * struct orinoco_private */
87 struct orinoco_pccard {
93 * A linked list of "instances" of the device. Each actual PCMCIA
94 * card corresponds to one device instance, and is described by one
95 * dev_link_t structure (defined in ds.h).
97 static dev_link_t *dev_list; /* = NULL */
99 /********************************************************************/
100 /* Function prototypes */
101 /********************************************************************/
104 static int spectrum_cs_hard_reset(struct orinoco_private *priv);
107 static void spectrum_cs_config(dev_link_t * link);
108 static void spectrum_cs_release(u_long arg);
109 static int spectrum_cs_event(event_t event, int priority,
110 event_callback_args_t * args);
112 static dev_link_t *spectrum_cs_attach(void);
113 static void spectrum_cs_detach(dev_link_t *);
115 /********************************************************************/
116 /* Firmware downloader */
117 /********************************************************************/
119 /* Header with the firmware */
120 #include "spectrum_fw.h"
122 /* Position of PDA in the adapter memory */
123 #define EEPROM_ADDR 0x3000
124 #define EEPROM_LEN 0x200
125 #define PDA_OFFSET 0x100
127 #define PDA_ADDR (EEPROM_ADDR + PDA_OFFSET)
128 #define PDA_WORDS ((EEPROM_LEN - PDA_OFFSET) / 2)
130 /* Constants for the CISREG_CCSR register */
131 #define HCR_RUN 0x07 /* run firmware after reset */
132 #define HCR_IDLE 0x0E /* don't run firmware after reset */
133 #define HCR_MEM16 0x10 /* memory width bit, should be preserved */
135 /* Hermes command run by primary firmware */
136 #define HERMES_CMD_READEE 0x0030 /* read serial EEPROM */
139 * AUX port access. To unlock the AUX port write the access keys to the
140 * PARAM0-2 registers, then write HERMES_AUX_ENABLE to the HERMES_CONTROL
141 * register. Then read it and make sure it's HERMES_AUX_ENABLED.
143 #define HERMES_AUX_ENABLE 0x8000 /* Enable auxiliary port access */
144 #define HERMES_AUX_DISABLE 0x4000 /* Disable to auxiliary port access */
145 #define HERMES_AUX_ENABLED 0xC000 /* Auxiliary port is open */
147 #define HERMES_AUX_PW0 0xFE01
148 #define HERMES_AUX_PW1 0xDC23
149 #define HERMES_AUX_PW2 0xBA45
152 #define PDI_END 0x00000000 /* End of PDA */
153 #define BLOCK_END 0xFFFFFFFF /* Last image block */
154 #define TEXT_END 0x1A /* End of text header */
157 * The following structures have little-endian fields denoted by
158 * the leading underscore. Don't access them directly - use inline
159 * functions defined below.
163 * The binary image to be downloaded consists of series of data blocks.
164 * Each block has the following structure.
167 u32 _addr; /* adapter address where to write the block */
168 u16 _len; /* length of the data only, in bytes */
169 char data[0]; /* data to be written */
170 } __attribute__ ((packed));
173 * Plug Data References are located in in the image after the last data
174 * block. They refer to areas in the adapter memory where the plug data
175 * items with matching ID should be written.
178 u32 _id; /* record ID */
179 u32 _addr; /* adapter address where to write the data */
180 u32 _len; /* expected length of the data, in bytes */
181 char next[0]; /* next PDR starts here */
182 } __attribute__ ((packed));
186 * Plug Data Items are located in the EEPROM read from the adapter by
187 * primary firmware. They refer to the device-specific data that should
188 * be plugged into the secondary firmware.
191 u16 _len; /* length of ID and data, in words */
192 u16 _id; /* record ID */
193 char data[0]; /* plug data */
194 } __attribute__ ((packed));;
197 /* Functions for access to little-endian data */
199 dblock_addr(const struct dblock *blk)
201 return le32_to_cpu(blk->_addr);
205 dblock_len(const struct dblock *blk)
207 return le16_to_cpu(blk->_len);
211 pdr_id(const struct pdr *pdr)
213 return le32_to_cpu(pdr->_id);
217 pdr_addr(const struct pdr *pdr)
219 return le32_to_cpu(pdr->_addr);
223 pdr_len(const struct pdr *pdr)
225 return le32_to_cpu(pdr->_len);
229 pdi_id(const struct pdi *pdi)
231 return le16_to_cpu(pdi->_id);
234 /* Return length of the data only, in bytes */
236 pdi_len(const struct pdi *pdi)
238 return 2 * (le16_to_cpu(pdi->_len) - 1);
242 /* Set address of the auxiliary port */
244 spectrum_aux_setaddr(hermes_t *hw, u32 addr)
246 hermes_write_reg(hw, HERMES_AUXPAGE, (u16) (addr >> 7));
247 hermes_write_reg(hw, HERMES_AUXOFFSET, (u16) (addr & 0x7F));
251 /* Open access to the auxiliary port */
253 spectrum_aux_open(hermes_t *hw)
258 if (hermes_read_reg(hw, HERMES_CONTROL) == HERMES_AUX_ENABLED)
261 hermes_write_reg(hw, HERMES_PARAM0, HERMES_AUX_PW0);
262 hermes_write_reg(hw, HERMES_PARAM1, HERMES_AUX_PW1);
263 hermes_write_reg(hw, HERMES_PARAM2, HERMES_AUX_PW2);
264 hermes_write_reg(hw, HERMES_CONTROL, HERMES_AUX_ENABLE);
266 for (i = 0; i < 20; i++) {
268 if (hermes_read_reg(hw, HERMES_CONTROL) ==
277 #define CS_CHECK(fn, args...) \
278 while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
280 #define CFG_CHECK(fn, args...) \
281 if (CardServices(fn, args) != 0) goto next_entry
283 static void spectrum_cs_error(client_handle_t handle, int func, int ret);
286 * Reset the card using configuration registers COR and CCSR.
287 * If IDLE is 1, stop the firmware, so that it can be safely rewritten.
290 spectrum_reset(dev_link_t *link, int idle)
292 int last_ret, last_fn;
296 /* Doing it if hardware is gone is guaranteed crash */
297 if (!(link->state & DEV_CONFIG))
300 /* Save original COR value */
302 reg.Action = CS_READ;
303 reg.Offset = CISREG_COR;
304 CS_CHECK(AccessConfigurationRegister, link->handle, ®);
305 save_cor = reg.Value;
307 /* Soft-Reset card */
308 reg.Action = CS_WRITE;
309 reg.Offset = CISREG_COR;
310 reg.Value = (save_cor | COR_SOFT_RESET);
311 CS_CHECK(AccessConfigurationRegister, link->handle, ®);
315 reg.Action = CS_READ;
316 reg.Offset = CISREG_CCSR;
317 CS_CHECK(AccessConfigurationRegister, link->handle, ®);
320 * Start or stop the firmware. Memory width bit should be
321 * preserved from the value we've just read.
323 reg.Action = CS_WRITE;
324 reg.Offset = CISREG_CCSR;
325 reg.Value = (idle ? HCR_IDLE : HCR_RUN) | (reg.Value & HCR_MEM16);
326 CS_CHECK(AccessConfigurationRegister, link->handle, ®);
329 /* Restore original COR configuration index */
330 reg.Action = CS_WRITE;
331 reg.Offset = CISREG_COR;
332 reg.Value = (save_cor & ~COR_SOFT_RESET);
333 CS_CHECK(AccessConfigurationRegister, link->handle, ®);
338 spectrum_cs_error(link->handle, last_fn, last_ret);
344 * Scan PDR for the record with the specified RECORD_ID.
345 * If it's not found, return NULL.
348 spectrum_find_pdr(struct pdr *first_pdr, u32 record_id)
350 struct pdr *pdr = first_pdr;
352 while (pdr_id(pdr) != PDI_END) {
354 * PDR area is currently not terminated by PDI_END.
355 * It's followed by CRC records, which have the type
356 * field where PDR has length. The type can be 0 or 1.
358 if (pdr_len(pdr) < 2)
361 /* If the record ID matches, we are done */
362 if (pdr_id(pdr) == record_id)
365 pdr = (struct pdr *) pdr->next;
371 /* Process one Plug Data Item - find corresponding PDR and plug it */
373 spectrum_plug_pdi(hermes_t *hw, struct pdr *first_pdr, struct pdi *pdi)
377 /* Find the PDI corresponding to this PDR */
378 pdr = spectrum_find_pdr(first_pdr, pdi_id(pdi));
380 /* No match is found, safe to ignore */
384 /* Lengths of the data in PDI and PDR must match */
385 if (pdi_len(pdi) != pdr_len(pdr))
388 /* do the actual plugging */
389 spectrum_aux_setaddr(hw, pdr_addr(pdr));
390 hermes_write_words(hw, HERMES_AUXDATA, pdi->data,
397 /* Read PDA from the adapter */
399 spectrum_read_pda(hermes_t *hw, u16 *pda, int pda_len)
404 /* Issue command to read EEPROM */
405 ret = hermes_docmd_wait(hw, HERMES_CMD_READEE, 0, NULL);
409 /* Open auxiliary port */
410 ret = spectrum_aux_open(hw);
414 /* read PDA from EEPROM */
415 spectrum_aux_setaddr(hw, PDA_ADDR);
416 hermes_read_words(hw, HERMES_AUXDATA, pda, pda_len / 2);
418 /* Check PDA length */
419 pda_size = le16_to_cpu(pda[0]);
420 if (pda_size > pda_len)
427 /* Parse PDA and write the records into the adapter */
429 spectrum_apply_pda(hermes_t *hw, const struct dblock *first_block,
434 struct pdr *first_pdr;
435 const struct dblock *blk = first_block;
437 /* Skip all blocks to locate Plug Data References */
438 while (dblock_addr(blk) != BLOCK_END)
439 blk = (struct dblock *) &blk->data[dblock_len(blk)];
441 first_pdr = (struct pdr *) blk;
443 /* Go through every PDI and plug them into the adapter */
444 pdi = (struct pdi *) (pda + 2);
445 while (pdi_id(pdi) != PDI_END) {
446 ret = spectrum_plug_pdi(hw, first_pdr, pdi);
450 /* Increment to the next PDI */
451 pdi = (struct pdi *) &pdi->data[pdi_len(pdi)];
457 /* Load firmware blocks into the adapter */
459 spectrum_load_blocks(hermes_t *hw, const struct dblock *first_block)
461 const struct dblock *blk;
466 blkaddr = dblock_addr(blk);
467 blklen = dblock_len(blk);
469 while (dblock_addr(blk) != BLOCK_END) {
470 spectrum_aux_setaddr(hw, blkaddr);
471 hermes_write_words(hw, HERMES_AUXDATA, blk->data,
474 blk = (struct dblock *) &blk->data[blklen];
475 blkaddr = dblock_addr(blk);
476 blklen = dblock_len(blk);
483 * Process a firmware image - stop the card, load the firmware, reset
484 * the card and make sure it responds. For the secondary firmware take
485 * care of the PDA - read it and then write it on top of the firmware.
488 spectrum_dl_image(hermes_t *hw, dev_link_t *link,
489 const unsigned char *image)
492 const unsigned char *ptr;
493 const struct dblock *first_block;
495 /* Plug Data Area (PDA) */
498 /* Binary block begins after the 0x1A marker */
500 while (*ptr++ != TEXT_END);
501 first_block = (const struct dblock *) ptr;
504 if (image != primsym) {
505 ret = spectrum_read_pda(hw, pda, sizeof(pda));
510 /* Stop the firmware, so that it can be safely rewritten */
511 ret = spectrum_reset(link, 1);
515 /* Program the adapter with new firmware */
516 ret = spectrum_load_blocks(hw, first_block);
520 /* Write the PDA to the adapter */
521 if (image != primsym) {
522 ret = spectrum_apply_pda(hw, first_block, pda);
527 /* Run the firmware */
528 ret = spectrum_reset(link, 0);
532 /* Reset hermes chip and make sure it responds */
533 ret = hermes_init(hw);
535 /* hermes_reset() should return 0 with the secondary firmware */
536 if (image != primsym && ret != 0)
539 /* And this should work with any firmware */
540 if (!hermes_present(hw))
548 * Download the firmware into the card, this also does a PCMCIA soft
549 * reset on the card, to make sure it's in a sane state.
552 spectrum_dl_firmware(hermes_t *hw, dev_link_t *link)
556 /* Load primary firmware */
557 ret = spectrum_dl_image(hw, link, primsym);
559 printk(KERN_ERR "spectrum_cs: "
560 "primary firmware download failed\n");
564 /* Load secondary firmware */
565 ret = spectrum_dl_image(hw, link, secsym);
568 printk(KERN_ERR "spectrum_cs: "
569 "secondary firmware download failed\n");
575 /********************************************************************/
577 /********************************************************************/
580 spectrum_cs_hard_reset(struct orinoco_private *priv)
582 struct orinoco_pccard *card = priv->card;
583 dev_link_t *link = &card->link;
586 if (!hermes_present(&priv->hw)) {
587 /* The firmware needs to be reloaded */
588 if (spectrum_dl_firmware(&priv->hw, &card->link) != 0) {
590 "spectrum_cs: firmware download failed\n");
594 /* Soft reset using COR and HCR */
595 spectrum_reset(link, 0);
601 /********************************************************************/
603 /********************************************************************/
605 /* In 2.5 (as of 2.5.69 at least) there is a cs_error exported which
606 * does this, but it's not in 2.4 so we do our own for now. */
608 spectrum_cs_error(client_handle_t handle, int func, int ret)
610 error_info_t err = { func, ret };
611 CardServices(ReportError, handle, &err);
615 /* Remove zombie instances (card removed, detach pending) */
617 flush_stale_links(void)
619 dev_link_t *link, *next;
623 for (link = dev_list; link; link = next) {
625 if (link->state & DEV_STALE_LINK) {
626 spectrum_cs_detach(link);
633 * This creates an "instance" of the driver, allocating local data
634 * structures for one device. The device is registered with Card
637 * The dev_link structure is initialized, but we don't actually
638 * configure the card at this point -- we wait until we receive a card
639 * insertion event. */
641 spectrum_cs_attach(void)
643 struct net_device *dev;
644 struct orinoco_private *priv;
645 struct orinoco_pccard *card;
647 client_reg_t client_reg;
650 /* A bit of cleanup */
653 dev = alloc_orinocodev(sizeof(*card), spectrum_cs_hard_reset);
659 /* Link both structures together */
663 /* Initialize the dev_link_t structure */
664 init_timer(&link->release);
665 link->release.function = &spectrum_cs_release;
666 link->release.data = (u_long) link;
668 /* Interrupt setup */
669 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
670 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
671 if (irq_list[0] == -1)
672 link->irq.IRQInfo2 = irq_mask;
674 for (i = 0; i < 4; i++)
675 link->irq.IRQInfo2 |= 1 << irq_list[i];
676 link->irq.Handler = NULL;
678 /* General socket configuration defaults can go here. In this
679 * client, we assume very little, and rely on the CIS for
680 * almost everything. In most clients, many details (i.e.,
681 * number, sizes, and attributes of IO windows) are fixed by
682 * the nature of the device, and can be hard-wired here. */
683 link->conf.Attributes = 0;
684 link->conf.IntType = INT_MEMORY_AND_IO;
686 /* Register with Card Services */
687 /* FIXME: need a lock? */
688 link->next = dev_list;
691 client_reg.dev_info = &dev_info;
692 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
693 client_reg.EventMask =
694 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
695 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
696 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
697 client_reg.event_handler = &spectrum_cs_event;
698 client_reg.Version = 0x0210; /* FIXME: what does this mean? */
699 client_reg.event_callback_args.client_data = link;
701 ret = CardServices(RegisterClient, &link->handle, &client_reg);
702 if (ret != CS_SUCCESS) {
703 spectrum_cs_error(link->handle, RegisterClient, ret);
704 spectrum_cs_detach(link);
709 } /* spectrum_cs_attach */
712 * This deletes a driver "instance". The device is de-registered with
713 * Card Services. If it has been released, all local data structures
714 * are freed. Otherwise, the structures will be freed when the device
718 spectrum_cs_detach(dev_link_t * link)
721 struct net_device *dev = link->priv;
723 /* Locate device structure */
724 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
727 if (*linkp == NULL) {
732 if (link->state & DEV_CONFIG) {
733 spectrum_cs_release((u_long)link);
734 if (link->state & DEV_CONFIG) {
735 link->state |= DEV_STALE_LINK;
740 /* Break the link with Card Services */
742 CardServices(DeregisterClient, link->handle);
744 /* Unlink device structure, and free it */
746 DEBUG(0, "spectrum_cs: detach: link=%p link->dev=%p\n", link, link->dev);
748 DEBUG(0, "spectrum_cs: About to unregister net device %p\n",
750 unregister_netdev(dev);
753 } /* spectrum_cs_detach */
756 * spectrum_cs_config() is scheduled to run after a CARD_INSERTION
757 * event is received, to configure the PCMCIA socket, and to make the
758 * device available to the system.
762 spectrum_cs_config(dev_link_t *link)
764 struct net_device *dev = link->priv;
765 client_handle_t handle = link->handle;
766 struct orinoco_private *priv = dev->priv;
767 struct orinoco_pccard *card = priv->card;
768 hermes_t *hw = &priv->hw;
769 int last_fn, last_ret;
776 CS_CHECK(ValidateCIS, handle, &info);
779 * This reads the card's CONFIG tuple to find its
780 * configuration registers.
782 tuple.DesiredTuple = CISTPL_CONFIG;
783 tuple.Attributes = 0;
784 tuple.TupleData = buf;
785 tuple.TupleDataMax = sizeof(buf);
786 tuple.TupleOffset = 0;
787 CS_CHECK(GetFirstTuple, handle, &tuple);
788 CS_CHECK(GetTupleData, handle, &tuple);
789 CS_CHECK(ParseTuple, handle, &tuple, &parse);
790 link->conf.ConfigBase = parse.config.base;
791 link->conf.Present = parse.config.rmask[0];
794 link->state |= DEV_CONFIG;
796 /* Look up the current Vcc */
797 CS_CHECK(GetConfigurationInfo, handle, &conf);
798 link->conf.Vcc = conf.Vcc;
801 * In this loop, we scan the CIS for configuration table
802 * entries, each of which describes a valid card
803 * configuration, including voltage, IO window, memory window,
804 * and interrupt settings.
806 * We make no assumptions about the card to be configured: we
807 * use just the information available in the CIS. In an ideal
808 * world, this would work for any PCMCIA card, but it requires
809 * a complete and accurate CIS. In practice, a driver usually
810 * "knows" most of these things without consulting the CIS,
811 * and most client drivers will only use the CIS to fill in
812 * implementation-defined details.
814 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
815 CS_CHECK(GetFirstTuple, handle, &tuple);
817 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
818 cistpl_cftable_entry_t dflt = { .index = 0 };
820 CFG_CHECK(GetTupleData, handle, &tuple);
821 CFG_CHECK(ParseTuple, handle, &tuple, &parse);
823 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
827 link->conf.ConfigIndex = cfg->index;
829 /* Does this card need audio output? */
830 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
831 link->conf.Attributes |= CONF_ENABLE_SPKR;
832 link->conf.Status = CCSR_AUDIO_ENA;
835 /* Use power settings for Vcc and Vpp if present */
836 /* Note that the CIS values need to be rescaled */
837 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
838 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
839 DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
843 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
844 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
845 DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
851 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
852 link->conf.Vpp1 = link->conf.Vpp2 =
853 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
854 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
855 link->conf.Vpp1 = link->conf.Vpp2 =
856 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
858 /* Do we need to allocate an interrupt? */
859 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
860 link->conf.Attributes |= CONF_ENABLE_IRQ;
862 /* IO window settings */
863 link->io.NumPorts1 = link->io.NumPorts2 = 0;
864 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
866 (cfg->io.nwin) ? &cfg->io : &dflt.io;
867 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
868 if (!(io->flags & CISTPL_IO_8BIT))
869 link->io.Attributes1 =
870 IO_DATA_PATH_WIDTH_16;
871 if (!(io->flags & CISTPL_IO_16BIT))
872 link->io.Attributes1 =
873 IO_DATA_PATH_WIDTH_8;
874 link->io.IOAddrLines =
875 io->flags & CISTPL_IO_LINES_MASK;
876 link->io.BasePort1 = io->win[0].base;
877 link->io.NumPorts1 = io->win[0].len;
879 link->io.Attributes2 =
880 link->io.Attributes1;
881 link->io.BasePort2 = io->win[1].base;
882 link->io.NumPorts2 = io->win[1].len;
885 /* This reserves IO space but doesn't actually enable it */
886 CFG_CHECK(RequestIO, link->handle, &link->io);
890 /* If we got this far, we're cool! */
895 if (link->io.NumPorts1)
896 CardServices(ReleaseIO, link->handle, &link->io);
897 last_ret = CardServices(GetNextTuple, handle, &tuple);
898 if (last_ret == CS_NO_MORE_ITEMS) {
899 printk(KERN_ERR "GetNextTuple(). No matching CIS configuration, "
900 "maybe you need the ignore_cis_vcc=1 parameter.\n");
906 * Allocate an interrupt line. Note that this does not assign
907 * a handler to the interrupt, unless the 'Handler' member of
908 * the irq structure is initialized.
910 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
913 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
914 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
915 if (irq_list[0] == -1)
916 link->irq.IRQInfo2 = irq_mask;
919 link->irq.IRQInfo2 |= 1 << irq_list[i];
921 link->irq.Handler = orinoco_interrupt;
922 link->irq.Instance = dev;
924 CS_CHECK(RequestIRQ, link->handle, &link->irq);
927 /* We initialize the hermes structure before completing PCMCIA
928 * configuration just in case the interrupt handler gets
930 hermes_struct_init(hw, link->io.BasePort1,
931 HERMES_IO, HERMES_16BIT_REGSPACING);
934 * This actually configures the PCMCIA socket -- setting up
935 * the I/O windows and the interrupt mapping, and putting the
936 * card and host interface into "Memory and IO" mode.
938 CS_CHECK(RequestConfiguration, link->handle, &link->conf);
940 /* Ok, we have the configuration, prepare to register the netdev */
941 dev->base_addr = link->io.BasePort1;
942 dev->irq = link->irq.AssignedIRQ;
943 SET_MODULE_OWNER(dev);
944 card->node.major = card->node.minor = 0;
946 /* Sanity check to avoid downloading firmware into a wrong card.
947 * HFA3842 should have CCSR and 7 address lines. */
948 if (link->io.IOAddrLines < 7) {
949 printk(KERN_ERR "spectrum_cs: expected at least 7 address "
950 "lines, found just %d\n", link->io.IOAddrLines);
954 if (!(link->conf.Present | PRESENT_STATUS)) {
956 "spectrum_cs: Status register not found\n");
960 /* Reset card and download firmware */
961 if (spectrum_cs_hard_reset(priv) != 0) {
965 /* register_netdev will give us an ethX name */
967 /* Tell the stack we exist */
968 if (register_netdev(dev) != 0) {
969 printk(KERN_ERR "spectrum_cs: register_netdev() failed\n");
973 /* At this point, the dev_node_t structure(s) needs to be
974 * initialized and arranged in a linked list at link->dev. */
975 strcpy(card->node.dev_name, dev->name);
976 link->dev = &card->node; /* link->dev being non-NULL is also
977 used to indicate that the
978 net_device has been registered */
979 link->state &= ~DEV_CONFIG_PENDING;
981 /* Finally, report what we've done */
982 printk(KERN_DEBUG "%s: index 0x%02x: Vcc %d.%d",
983 dev->name, link->conf.ConfigIndex,
984 link->conf.Vcc / 10, link->conf.Vcc % 10);
986 printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
987 link->conf.Vpp1 % 10);
988 if (link->conf.Attributes & CONF_ENABLE_IRQ)
989 printk(", irq %d", link->irq.AssignedIRQ);
990 if (link->io.NumPorts1)
991 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
992 link->io.BasePort1 + link->io.NumPorts1 - 1);
993 if (link->io.NumPorts2)
994 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
995 link->io.BasePort2 + link->io.NumPorts2 - 1);
1001 spectrum_cs_error(link->handle, last_fn, last_ret);
1004 spectrum_cs_release((u_long) link);
1005 } /* spectrum_cs_config */
1008 * After a card is removed, spectrum_cs_release() will unregister the
1009 * device, and release the PCMCIA configuration. If the device is
1010 * still open, this will be postponed until it is closed.
1013 spectrum_cs_release(u_long arg)
1015 dev_link_t *link = (dev_link_t *) arg;
1016 struct net_device *dev = link->priv;
1017 struct orinoco_private *priv = dev->priv;
1018 unsigned long flags;
1020 /* We're committed to taking the device away now, so mark the
1021 * hardware as unavailable */
1022 spin_lock_irqsave(&priv->lock, flags);
1023 priv->hw_unavailable++;
1024 spin_unlock_irqrestore(&priv->lock, flags);
1026 /* Don't bother checking to see if these succeed or not */
1027 CardServices(ReleaseConfiguration, link->handle);
1028 if (link->io.NumPorts1)
1029 CardServices(ReleaseIO, link->handle, &link->io);
1030 if (link->irq.AssignedIRQ)
1031 CardServices(ReleaseIRQ, link->handle, &link->irq);
1032 link->state &= ~DEV_CONFIG;
1033 } /* spectrum_cs_release */
1036 * The card status event handler. Mostly, this schedules other stuff
1037 * to run after an event is received.
1040 spectrum_cs_event(event_t event, int priority,
1041 event_callback_args_t * args)
1043 dev_link_t *link = args->client_data;
1044 struct net_device *dev = link->priv;
1045 struct orinoco_private *priv = dev->priv;
1047 unsigned long flags;
1050 case CS_EVENT_CARD_REMOVAL:
1051 link->state &= ~DEV_PRESENT;
1052 if (link->state & DEV_CONFIG) {
1053 orinoco_lock(priv, &flags);
1055 netif_device_detach(dev);
1056 priv->hw_unavailable++;
1058 orinoco_unlock(priv, &flags);
1062 case CS_EVENT_CARD_INSERTION:
1063 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1064 spectrum_cs_config(link);
1067 case CS_EVENT_PM_SUSPEND:
1068 link->state |= DEV_SUSPEND;
1069 /* Fall through... */
1070 case CS_EVENT_RESET_PHYSICAL:
1071 /* Mark the device as stopped, to block IO until later */
1072 if (link->state & DEV_CONFIG) {
1073 spin_lock_irqsave(&priv->lock, flags);
1075 err = __orinoco_down(dev);
1077 printk(KERN_WARNING "%s: %s: Error %d downing interface\n",
1079 event == CS_EVENT_PM_SUSPEND ? "SUSPEND" : "RESET_PHYSICAL",
1082 netif_device_detach(dev);
1083 priv->hw_unavailable++;
1085 spin_unlock_irqrestore(&priv->lock, flags);
1088 CardServices(ReleaseConfiguration, link->handle);
1091 case CS_EVENT_PM_RESUME:
1092 link->state &= ~DEV_SUSPEND;
1093 /* Fall through... */
1094 case CS_EVENT_CARD_RESET:
1095 if (link->state & DEV_CONFIG) {
1096 /* FIXME: should we double check that this is
1097 * the same card as we had before */
1098 CardServices(RequestConfiguration, link->handle,
1101 /* Download firmware if needed */
1102 err = spectrum_cs_hard_reset(priv);
1105 "spectrum_cs: firmware download failed\n");
1109 err = orinoco_reinit_firmware(dev);
1111 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
1116 spin_lock_irqsave(&priv->lock, flags);
1118 netif_device_attach(dev);
1119 priv->hw_unavailable--;
1121 if (priv->open && ! priv->hw_unavailable) {
1122 err = __orinoco_up(dev);
1124 printk(KERN_ERR "%s: Error %d restarting card\n",
1128 spin_unlock_irqrestore(&priv->lock, flags);
1134 } /* spectrum_cs_event */
1136 /********************************************************************/
1137 /* Module initialization */
1138 /********************************************************************/
1140 /* Can't be declared "const" or the whole __initdata section will
1142 static char version[] __initdata =
1143 "spectrum_cs.c 0.4.2 (Pavel Roskin <proski@gnu.org> and others)";
1146 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,70)
1148 /* Icky old style pcmcia registration */
1150 static int __init orinoco_pcmcia_register(void)
1154 CardServices(GetCardServicesInfo, &serv);
1155 if (serv.Revision != CS_RELEASE_CODE) {
1156 printk(KERN_NOTICE "spectrum_cs: Card Services release "
1157 "does not match!\n");
1161 register_pccard_driver(&dev_info,
1162 spectrum_cs_attach, spectrum_cs_detach);
1167 static void __exit orinoco_pcmcia_unregister(void)
1169 unregister_pccard_driver(&dev_info);
1174 static struct pcmcia_driver spectrum_cs_driver = {
1175 .owner = THIS_MODULE,
1177 .name = "spectrum_cs",
1179 .attach = spectrum_cs_attach,
1180 .detach = spectrum_cs_detach,
1183 #define orinoco_pcmcia_register() pcmcia_register_driver(&spectrum_cs_driver);
1184 #define orinoco_pcmcia_unregister() pcmcia_unregister_driver(&spectrum_cs_driver);
1189 init_spectrum_cs(void)
1191 printk(KERN_DEBUG "%s\n", version);
1193 return orinoco_pcmcia_register();
1197 exit_spectrum_cs(void)
1199 orinoco_pcmcia_unregister();
1202 DEBUG(0, "spectrum_cs: Removing leftover devices.\n");
1203 while (dev_list != NULL) {
1204 if (dev_list->state & DEV_CONFIG)
1205 spectrum_cs_release((u_long) dev_list);
1206 spectrum_cs_detach(dev_list);
1210 module_init(init_spectrum_cs);
1211 module_exit(exit_spectrum_cs);