Pull acpi_device_handle_cleanup into release branch
[pandora-kernel.git] / drivers / net / wireless / spectrum_cs.c
1 /*
2  * Driver for 802.11b cards using RAM-loadable Symbol firmware, such as
3  * Symbol Wireless Networker LA4137, CompactFlash cards by Socket
4  * Communications and Intel PRO/Wireless 2011B.
5  *
6  * The driver implements Symbol firmware download.  The rest is handled
7  * in hermes.c and orinoco.c.
8  *
9  * Utilities for downloading the Symbol firmware are available at
10  * http://sourceforge.net/projects/orinoco/
11  *
12  * Copyright (C) 2002-2005 Pavel Roskin <proski@gnu.org>
13  * Portions based on orinoco_cs.c:
14  *      Copyright (C) David Gibson, Linuxcare Australia
15  * Portions based on Spectrum24tDnld.c from original spectrum24 driver:
16  *      Copyright (C) Symbol Technologies.
17  *
18  * See copyright notice in file orinoco.c.
19  */
20
21 #define DRIVER_NAME "spectrum_cs"
22 #define PFX DRIVER_NAME ": "
23
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/firmware.h>
29 #include <pcmcia/cs_types.h>
30 #include <pcmcia/cs.h>
31 #include <pcmcia/cistpl.h>
32 #include <pcmcia/cisreg.h>
33 #include <pcmcia/ds.h>
34
35 #include "orinoco.h"
36
37 static unsigned char *primsym;
38 static unsigned char *secsym;
39 static const char primary_fw_name[] = "symbol_sp24t_prim_fw";
40 static const char secondary_fw_name[] = "symbol_sp24t_sec_fw";
41
42 /********************************************************************/
43 /* Module stuff                                                     */
44 /********************************************************************/
45
46 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
47 MODULE_DESCRIPTION("Driver for Symbol Spectrum24 Trilogy cards with firmware downloader");
48 MODULE_LICENSE("Dual MPL/GPL");
49
50 /* Module parameters */
51
52 /* Some D-Link cards have buggy CIS. They do work at 5v properly, but
53  * don't have any CIS entry for it. This workaround it... */
54 static int ignore_cis_vcc; /* = 0 */
55 module_param(ignore_cis_vcc, int, 0);
56 MODULE_PARM_DESC(ignore_cis_vcc, "Allow voltage mismatch between card and socket");
57
58 /********************************************************************/
59 /* Data structures                                                  */
60 /********************************************************************/
61
62 /* PCMCIA specific device information (goes in the card field of
63  * struct orinoco_private */
64 struct orinoco_pccard {
65         struct pcmcia_device    *p_dev;
66         dev_node_t node;
67 };
68
69 /********************************************************************/
70 /* Function prototypes                                              */
71 /********************************************************************/
72
73 static int spectrum_cs_config(struct pcmcia_device *link);
74 static void spectrum_cs_release(struct pcmcia_device *link);
75
76 /********************************************************************/
77 /* Firmware downloader                                              */
78 /********************************************************************/
79
80 /* Position of PDA in the adapter memory */
81 #define EEPROM_ADDR     0x3000
82 #define EEPROM_LEN      0x200
83 #define PDA_OFFSET      0x100
84
85 #define PDA_ADDR        (EEPROM_ADDR + PDA_OFFSET)
86 #define PDA_WORDS       ((EEPROM_LEN - PDA_OFFSET) / 2)
87
88 /* Constants for the CISREG_CCSR register */
89 #define HCR_RUN         0x07    /* run firmware after reset */
90 #define HCR_IDLE        0x0E    /* don't run firmware after reset */
91 #define HCR_MEM16       0x10    /* memory width bit, should be preserved */
92
93 /*
94  * AUX port access.  To unlock the AUX port write the access keys to the
95  * PARAM0-2 registers, then write HERMES_AUX_ENABLE to the HERMES_CONTROL
96  * register.  Then read it and make sure it's HERMES_AUX_ENABLED.
97  */
98 #define HERMES_AUX_ENABLE       0x8000  /* Enable auxiliary port access */
99 #define HERMES_AUX_DISABLE      0x4000  /* Disable to auxiliary port access */
100 #define HERMES_AUX_ENABLED      0xC000  /* Auxiliary port is open */
101
102 #define HERMES_AUX_PW0  0xFE01
103 #define HERMES_AUX_PW1  0xDC23
104 #define HERMES_AUX_PW2  0xBA45
105
106 /* End markers */
107 #define PDI_END         0x00000000      /* End of PDA */
108 #define BLOCK_END       0xFFFFFFFF      /* Last image block */
109 #define TEXT_END        0x1A            /* End of text header */
110
111 /*
112  * The following structures have little-endian fields denoted by
113  * the leading underscore.  Don't access them directly - use inline
114  * functions defined below.
115  */
116
117 /*
118  * The binary image to be downloaded consists of series of data blocks.
119  * Each block has the following structure.
120  */
121 struct dblock {
122         __le32 addr;            /* adapter address where to write the block */
123         __le16 len;             /* length of the data only, in bytes */
124         char data[0];           /* data to be written */
125 } __attribute__ ((packed));
126
127 /*
128  * Plug Data References are located in in the image after the last data
129  * block.  They refer to areas in the adapter memory where the plug data
130  * items with matching ID should be written.
131  */
132 struct pdr {
133         __le32 id;              /* record ID */
134         __le32 addr;            /* adapter address where to write the data */
135         __le32 len;             /* expected length of the data, in bytes */
136         char next[0];           /* next PDR starts here */
137 } __attribute__ ((packed));
138
139
140 /*
141  * Plug Data Items are located in the EEPROM read from the adapter by
142  * primary firmware.  They refer to the device-specific data that should
143  * be plugged into the secondary firmware.
144  */
145 struct pdi {
146         __le16 len;             /* length of ID and data, in words */
147         __le16 id;              /* record ID */
148         char data[0];           /* plug data */
149 } __attribute__ ((packed));
150
151
152 /* Functions for access to little-endian data */
153 static inline u32
154 dblock_addr(const struct dblock *blk)
155 {
156         return le32_to_cpu(blk->addr);
157 }
158
159 static inline u32
160 dblock_len(const struct dblock *blk)
161 {
162         return le16_to_cpu(blk->len);
163 }
164
165 static inline u32
166 pdr_id(const struct pdr *pdr)
167 {
168         return le32_to_cpu(pdr->id);
169 }
170
171 static inline u32
172 pdr_addr(const struct pdr *pdr)
173 {
174         return le32_to_cpu(pdr->addr);
175 }
176
177 static inline u32
178 pdr_len(const struct pdr *pdr)
179 {
180         return le32_to_cpu(pdr->len);
181 }
182
183 static inline u32
184 pdi_id(const struct pdi *pdi)
185 {
186         return le16_to_cpu(pdi->id);
187 }
188
189 /* Return length of the data only, in bytes */
190 static inline u32
191 pdi_len(const struct pdi *pdi)
192 {
193         return 2 * (le16_to_cpu(pdi->len) - 1);
194 }
195
196
197 /* Set address of the auxiliary port */
198 static inline void
199 spectrum_aux_setaddr(hermes_t *hw, u32 addr)
200 {
201         hermes_write_reg(hw, HERMES_AUXPAGE, (u16) (addr >> 7));
202         hermes_write_reg(hw, HERMES_AUXOFFSET, (u16) (addr & 0x7F));
203 }
204
205
206 /* Open access to the auxiliary port */
207 static int
208 spectrum_aux_open(hermes_t *hw)
209 {
210         int i;
211
212         /* Already open? */
213         if (hermes_read_reg(hw, HERMES_CONTROL) == HERMES_AUX_ENABLED)
214                 return 0;
215
216         hermes_write_reg(hw, HERMES_PARAM0, HERMES_AUX_PW0);
217         hermes_write_reg(hw, HERMES_PARAM1, HERMES_AUX_PW1);
218         hermes_write_reg(hw, HERMES_PARAM2, HERMES_AUX_PW2);
219         hermes_write_reg(hw, HERMES_CONTROL, HERMES_AUX_ENABLE);
220
221         for (i = 0; i < 20; i++) {
222                 udelay(10);
223                 if (hermes_read_reg(hw, HERMES_CONTROL) ==
224                     HERMES_AUX_ENABLED)
225                         return 0;
226         }
227
228         return -EBUSY;
229 }
230
231
232 #define CS_CHECK(fn, ret) \
233   do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
234
235 /*
236  * Reset the card using configuration registers COR and CCSR.
237  * If IDLE is 1, stop the firmware, so that it can be safely rewritten.
238  */
239 static int
240 spectrum_reset(struct pcmcia_device *link, int idle)
241 {
242         int last_ret, last_fn;
243         conf_reg_t reg;
244         u_int save_cor;
245
246         /* Doing it if hardware is gone is guaranteed crash */
247         if (pcmcia_dev_present(link))
248                 return -ENODEV;
249
250         /* Save original COR value */
251         reg.Function = 0;
252         reg.Action = CS_READ;
253         reg.Offset = CISREG_COR;
254         CS_CHECK(AccessConfigurationRegister,
255                  pcmcia_access_configuration_register(link, &reg));
256         save_cor = reg.Value;
257
258         /* Soft-Reset card */
259         reg.Action = CS_WRITE;
260         reg.Offset = CISREG_COR;
261         reg.Value = (save_cor | COR_SOFT_RESET);
262         CS_CHECK(AccessConfigurationRegister,
263                  pcmcia_access_configuration_register(link, &reg));
264         udelay(1000);
265
266         /* Read CCSR */
267         reg.Action = CS_READ;
268         reg.Offset = CISREG_CCSR;
269         CS_CHECK(AccessConfigurationRegister,
270                  pcmcia_access_configuration_register(link, &reg));
271
272         /*
273          * Start or stop the firmware.  Memory width bit should be
274          * preserved from the value we've just read.
275          */
276         reg.Action = CS_WRITE;
277         reg.Offset = CISREG_CCSR;
278         reg.Value = (idle ? HCR_IDLE : HCR_RUN) | (reg.Value & HCR_MEM16);
279         CS_CHECK(AccessConfigurationRegister,
280                  pcmcia_access_configuration_register(link, &reg));
281         udelay(1000);
282
283         /* Restore original COR configuration index */
284         reg.Action = CS_WRITE;
285         reg.Offset = CISREG_COR;
286         reg.Value = (save_cor & ~COR_SOFT_RESET);
287         CS_CHECK(AccessConfigurationRegister,
288                  pcmcia_access_configuration_register(link, &reg));
289         udelay(1000);
290         return 0;
291
292       cs_failed:
293         cs_error(link, last_fn, last_ret);
294         return -ENODEV;
295 }
296
297
298 /*
299  * Scan PDR for the record with the specified RECORD_ID.
300  * If it's not found, return NULL.
301  */
302 static struct pdr *
303 spectrum_find_pdr(struct pdr *first_pdr, u32 record_id)
304 {
305         struct pdr *pdr = first_pdr;
306
307         while (pdr_id(pdr) != PDI_END) {
308                 /*
309                  * PDR area is currently not terminated by PDI_END.
310                  * It's followed by CRC records, which have the type
311                  * field where PDR has length.  The type can be 0 or 1.
312                  */
313                 if (pdr_len(pdr) < 2)
314                         return NULL;
315
316                 /* If the record ID matches, we are done */
317                 if (pdr_id(pdr) == record_id)
318                         return pdr;
319
320                 pdr = (struct pdr *) pdr->next;
321         }
322         return NULL;
323 }
324
325
326 /* Process one Plug Data Item - find corresponding PDR and plug it */
327 static int
328 spectrum_plug_pdi(hermes_t *hw, struct pdr *first_pdr, struct pdi *pdi)
329 {
330         struct pdr *pdr;
331
332         /* Find the PDI corresponding to this PDR */
333         pdr = spectrum_find_pdr(first_pdr, pdi_id(pdi));
334
335         /* No match is found, safe to ignore */
336         if (!pdr)
337                 return 0;
338
339         /* Lengths of the data in PDI and PDR must match */
340         if (pdi_len(pdi) != pdr_len(pdr))
341                 return -EINVAL;
342
343         /* do the actual plugging */
344         spectrum_aux_setaddr(hw, pdr_addr(pdr));
345         hermes_write_bytes(hw, HERMES_AUXDATA, pdi->data, pdi_len(pdi));
346
347         return 0;
348 }
349
350
351 /* Read PDA from the adapter */
352 static int
353 spectrum_read_pda(hermes_t *hw, __le16 *pda, int pda_len)
354 {
355         int ret;
356         int pda_size;
357
358         /* Issue command to read EEPROM */
359         ret = hermes_docmd_wait(hw, HERMES_CMD_READMIF, 0, NULL);
360         if (ret)
361                 return ret;
362
363         /* Open auxiliary port */
364         ret = spectrum_aux_open(hw);
365         if (ret)
366                 return ret;
367
368         /* read PDA from EEPROM */
369         spectrum_aux_setaddr(hw, PDA_ADDR);
370         hermes_read_words(hw, HERMES_AUXDATA, pda, pda_len / 2);
371
372         /* Check PDA length */
373         pda_size = le16_to_cpu(pda[0]);
374         if (pda_size > pda_len)
375                 return -EINVAL;
376
377         return 0;
378 }
379
380
381 /* Parse PDA and write the records into the adapter */
382 static int
383 spectrum_apply_pda(hermes_t *hw, const struct dblock *first_block,
384                    __le16 *pda)
385 {
386         int ret;
387         struct pdi *pdi;
388         struct pdr *first_pdr;
389         const struct dblock *blk = first_block;
390
391         /* Skip all blocks to locate Plug Data References */
392         while (dblock_addr(blk) != BLOCK_END)
393                 blk = (struct dblock *) &blk->data[dblock_len(blk)];
394
395         first_pdr = (struct pdr *) blk;
396
397         /* Go through every PDI and plug them into the adapter */
398         pdi = (struct pdi *) (pda + 2);
399         while (pdi_id(pdi) != PDI_END) {
400                 ret = spectrum_plug_pdi(hw, first_pdr, pdi);
401                 if (ret)
402                         return ret;
403
404                 /* Increment to the next PDI */
405                 pdi = (struct pdi *) &pdi->data[pdi_len(pdi)];
406         }
407         return 0;
408 }
409
410
411 /* Load firmware blocks into the adapter */
412 static int
413 spectrum_load_blocks(hermes_t *hw, const struct dblock *first_block)
414 {
415         const struct dblock *blk;
416         u32 blkaddr;
417         u32 blklen;
418
419         blk = first_block;
420         blkaddr = dblock_addr(blk);
421         blklen = dblock_len(blk);
422
423         while (dblock_addr(blk) != BLOCK_END) {
424                 spectrum_aux_setaddr(hw, blkaddr);
425                 hermes_write_bytes(hw, HERMES_AUXDATA, blk->data,
426                                    blklen);
427
428                 blk = (struct dblock *) &blk->data[blklen];
429                 blkaddr = dblock_addr(blk);
430                 blklen = dblock_len(blk);
431         }
432         return 0;
433 }
434
435
436 /*
437  * Process a firmware image - stop the card, load the firmware, reset
438  * the card and make sure it responds.  For the secondary firmware take
439  * care of the PDA - read it and then write it on top of the firmware.
440  */
441 static int
442 spectrum_dl_image(hermes_t *hw, struct pcmcia_device *link,
443                   const unsigned char *image)
444 {
445         int ret;
446         const unsigned char *ptr;
447         const struct dblock *first_block;
448
449         /* Plug Data Area (PDA) */
450         __le16 pda[PDA_WORDS];
451
452         /* Binary block begins after the 0x1A marker */
453         ptr = image;
454         while (*ptr++ != TEXT_END);
455         first_block = (const struct dblock *) ptr;
456
457         /* Read the PDA */
458         if (image != primsym) {
459                 ret = spectrum_read_pda(hw, pda, sizeof(pda));
460                 if (ret)
461                         return ret;
462         }
463
464         /* Stop the firmware, so that it can be safely rewritten */
465         ret = spectrum_reset(link, 1);
466         if (ret)
467                 return ret;
468
469         /* Program the adapter with new firmware */
470         ret = spectrum_load_blocks(hw, first_block);
471         if (ret)
472                 return ret;
473
474         /* Write the PDA to the adapter */
475         if (image != primsym) {
476                 ret = spectrum_apply_pda(hw, first_block, pda);
477                 if (ret)
478                         return ret;
479         }
480
481         /* Run the firmware */
482         ret = spectrum_reset(link, 0);
483         if (ret)
484                 return ret;
485
486         /* Reset hermes chip and make sure it responds */
487         ret = hermes_init(hw);
488
489         /* hermes_reset() should return 0 with the secondary firmware */
490         if (image != primsym && ret != 0)
491                 return -ENODEV;
492
493         /* And this should work with any firmware */
494         if (!hermes_present(hw))
495                 return -ENODEV;
496
497         return 0;
498 }
499
500
501 /*
502  * Download the firmware into the card, this also does a PCMCIA soft
503  * reset on the card, to make sure it's in a sane state.
504  */
505 static int
506 spectrum_dl_firmware(hermes_t *hw, struct pcmcia_device *link)
507 {
508         int ret;
509         const struct firmware *fw_entry;
510
511         if (request_firmware(&fw_entry, primary_fw_name,
512                              &handle_to_dev(link)) == 0) {
513                 primsym = fw_entry->data;
514         } else {
515                 printk(KERN_ERR PFX "Cannot find firmware: %s\n",
516                        primary_fw_name);
517                 return -ENOENT;
518         }
519
520         if (request_firmware(&fw_entry, secondary_fw_name,
521                              &handle_to_dev(link)) == 0) {
522                 secsym = fw_entry->data;
523         } else {
524                 printk(KERN_ERR PFX "Cannot find firmware: %s\n",
525                        secondary_fw_name);
526                 return -ENOENT;
527         }
528
529         /* Load primary firmware */
530         ret = spectrum_dl_image(hw, link, primsym);
531         if (ret) {
532                 printk(KERN_ERR PFX "Primary firmware download failed\n");
533                 return ret;
534         }
535
536         /* Load secondary firmware */
537         ret = spectrum_dl_image(hw, link, secsym);
538
539         if (ret) {
540                 printk(KERN_ERR PFX "Secondary firmware download failed\n");
541         }
542
543         return ret;
544 }
545
546 /********************************************************************/
547 /* Device methods                                                   */
548 /********************************************************************/
549
550 static int
551 spectrum_cs_hard_reset(struct orinoco_private *priv)
552 {
553         struct orinoco_pccard *card = priv->card;
554         struct pcmcia_device *link = card->p_dev;
555         int err;
556
557         if (!hermes_present(&priv->hw)) {
558                 /* The firmware needs to be reloaded */
559                 if (spectrum_dl_firmware(&priv->hw, link) != 0) {
560                         printk(KERN_ERR PFX "Firmware download failed\n");
561                         err = -ENODEV;
562                 }
563         } else {
564                 /* Soft reset using COR and HCR */
565                 spectrum_reset(link, 0);
566         }
567
568         return 0;
569 }
570
571 /********************************************************************/
572 /* PCMCIA stuff                                                     */
573 /********************************************************************/
574
575 /*
576  * This creates an "instance" of the driver, allocating local data
577  * structures for one device.  The device is registered with Card
578  * Services.
579  * 
580  * The dev_link structure is initialized, but we don't actually
581  * configure the card at this point -- we wait until we receive a card
582  * insertion event.  */
583 static int
584 spectrum_cs_probe(struct pcmcia_device *link)
585 {
586         struct net_device *dev;
587         struct orinoco_private *priv;
588         struct orinoco_pccard *card;
589
590         dev = alloc_orinocodev(sizeof(*card), spectrum_cs_hard_reset);
591         if (! dev)
592                 return -ENOMEM;
593         priv = netdev_priv(dev);
594         card = priv->card;
595
596         /* Link both structures together */
597         card->p_dev = link;
598         link->priv = dev;
599
600         /* Interrupt setup */
601         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
602         link->irq.IRQInfo1 = IRQ_LEVEL_ID;
603         link->irq.Handler = orinoco_interrupt;
604         link->irq.Instance = dev; 
605
606         /* General socket configuration defaults can go here.  In this
607          * client, we assume very little, and rely on the CIS for
608          * almost everything.  In most clients, many details (i.e.,
609          * number, sizes, and attributes of IO windows) are fixed by
610          * the nature of the device, and can be hard-wired here. */
611         link->conf.Attributes = 0;
612         link->conf.IntType = INT_MEMORY_AND_IO;
613
614         return spectrum_cs_config(link);
615 }                               /* spectrum_cs_attach */
616
617 /*
618  * This deletes a driver "instance".  The device is de-registered with
619  * Card Services.  If it has been released, all local data structures
620  * are freed.  Otherwise, the structures will be freed when the device
621  * is released.
622  */
623 static void spectrum_cs_detach(struct pcmcia_device *link)
624 {
625         struct net_device *dev = link->priv;
626
627         if (link->dev_node)
628                 unregister_netdev(dev);
629
630         spectrum_cs_release(link);
631
632         free_orinocodev(dev);
633 }                               /* spectrum_cs_detach */
634
635 /*
636  * spectrum_cs_config() is scheduled to run after a CARD_INSERTION
637  * event is received, to configure the PCMCIA socket, and to make the
638  * device available to the system.
639  */
640
641 static int
642 spectrum_cs_config(struct pcmcia_device *link)
643 {
644         struct net_device *dev = link->priv;
645         struct orinoco_private *priv = netdev_priv(dev);
646         struct orinoco_pccard *card = priv->card;
647         hermes_t *hw = &priv->hw;
648         int last_fn, last_ret;
649         u_char buf[64];
650         config_info_t conf;
651         tuple_t tuple;
652         cisparse_t parse;
653         void __iomem *mem;
654
655         /*
656          * This reads the card's CONFIG tuple to find its
657          * configuration registers.
658          */
659         tuple.DesiredTuple = CISTPL_CONFIG;
660         tuple.Attributes = 0;
661         tuple.TupleData = buf;
662         tuple.TupleDataMax = sizeof(buf);
663         tuple.TupleOffset = 0;
664         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
665         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
666         CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
667         link->conf.ConfigBase = parse.config.base;
668         link->conf.Present = parse.config.rmask[0];
669
670         /* Look up the current Vcc */
671         CS_CHECK(GetConfigurationInfo,
672                  pcmcia_get_configuration_info(link, &conf));
673
674         /*
675          * In this loop, we scan the CIS for configuration table
676          * entries, each of which describes a valid card
677          * configuration, including voltage, IO window, memory window,
678          * and interrupt settings.
679          *
680          * We make no assumptions about the card to be configured: we
681          * use just the information available in the CIS.  In an ideal
682          * world, this would work for any PCMCIA card, but it requires
683          * a complete and accurate CIS.  In practice, a driver usually
684          * "knows" most of these things without consulting the CIS,
685          * and most client drivers will only use the CIS to fill in
686          * implementation-defined details.
687          */
688         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
689         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
690         while (1) {
691                 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
692                 cistpl_cftable_entry_t dflt = { .index = 0 };
693
694                 if ( (pcmcia_get_tuple_data(link, &tuple) != 0)
695                     || (pcmcia_parse_tuple(link, &tuple, &parse) != 0))
696                         goto next_entry;
697
698                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
699                         dflt = *cfg;
700                 if (cfg->index == 0)
701                         goto next_entry;
702                 link->conf.ConfigIndex = cfg->index;
703
704                 /* Use power settings for Vcc and Vpp if present */
705                 /* Note that the CIS values need to be rescaled */
706                 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
707                         if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
708                                 DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n",  conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
709                                 if (!ignore_cis_vcc)
710                                         goto next_entry;
711                         }
712                 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
713                         if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
714                                 DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n",  conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
715                                 if(!ignore_cis_vcc)
716                                         goto next_entry;
717                         }
718                 }
719
720                 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
721                         link->conf.Vpp =
722                             cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
723                 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
724                         link->conf.Vpp =
725                             dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
726                 
727                 /* Do we need to allocate an interrupt? */
728                 link->conf.Attributes |= CONF_ENABLE_IRQ;
729
730                 /* IO window settings */
731                 link->io.NumPorts1 = link->io.NumPorts2 = 0;
732                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
733                         cistpl_io_t *io =
734                             (cfg->io.nwin) ? &cfg->io : &dflt.io;
735                         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
736                         if (!(io->flags & CISTPL_IO_8BIT))
737                                 link->io.Attributes1 =
738                                     IO_DATA_PATH_WIDTH_16;
739                         if (!(io->flags & CISTPL_IO_16BIT))
740                                 link->io.Attributes1 =
741                                     IO_DATA_PATH_WIDTH_8;
742                         link->io.IOAddrLines =
743                             io->flags & CISTPL_IO_LINES_MASK;
744                         link->io.BasePort1 = io->win[0].base;
745                         link->io.NumPorts1 = io->win[0].len;
746                         if (io->nwin > 1) {
747                                 link->io.Attributes2 =
748                                     link->io.Attributes1;
749                                 link->io.BasePort2 = io->win[1].base;
750                                 link->io.NumPorts2 = io->win[1].len;
751                         }
752
753                         /* This reserves IO space but doesn't actually enable it */
754                         if (pcmcia_request_io(link, &link->io) != 0)
755                                 goto next_entry;
756                 }
757
758
759                 /* If we got this far, we're cool! */
760
761                 break;
762                 
763         next_entry:
764                 pcmcia_disable_device(link);
765                 last_ret = pcmcia_get_next_tuple(link, &tuple);
766                 if (last_ret  == CS_NO_MORE_ITEMS) {
767                         printk(KERN_ERR PFX "GetNextTuple(): No matching "
768                                "CIS configuration.  Maybe you need the "
769                                "ignore_cis_vcc=1 parameter.\n");
770                         goto cs_failed;
771                 }
772         }
773
774         /*
775          * Allocate an interrupt line.  Note that this does not assign
776          * a handler to the interrupt, unless the 'Handler' member of
777          * the irq structure is initialized.
778          */
779         CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
780
781         /* We initialize the hermes structure before completing PCMCIA
782          * configuration just in case the interrupt handler gets
783          * called. */
784         mem = ioport_map(link->io.BasePort1, link->io.NumPorts1);
785         if (!mem)
786                 goto cs_failed;
787
788         hermes_struct_init(hw, mem, HERMES_16BIT_REGSPACING);
789
790         /*
791          * This actually configures the PCMCIA socket -- setting up
792          * the I/O windows and the interrupt mapping, and putting the
793          * card and host interface into "Memory and IO" mode.
794          */
795         CS_CHECK(RequestConfiguration,
796                  pcmcia_request_configuration(link, &link->conf));
797
798         /* Ok, we have the configuration, prepare to register the netdev */
799         dev->base_addr = link->io.BasePort1;
800         dev->irq = link->irq.AssignedIRQ;
801         SET_MODULE_OWNER(dev);
802         card->node.major = card->node.minor = 0;
803
804         /* Reset card and download firmware */
805         if (spectrum_cs_hard_reset(priv) != 0) {
806                 goto failed;
807         }
808
809         SET_NETDEV_DEV(dev, &handle_to_dev(link));
810         /* Tell the stack we exist */
811         if (register_netdev(dev) != 0) {
812                 printk(KERN_ERR PFX "register_netdev() failed\n");
813                 goto failed;
814         }
815
816         /* At this point, the dev_node_t structure(s) needs to be
817          * initialized and arranged in a linked list at link->dev_node. */
818         strcpy(card->node.dev_name, dev->name);
819         link->dev_node = &card->node; /* link->dev_node being non-NULL is also
820                                     used to indicate that the
821                                     net_device has been registered */
822
823         /* Finally, report what we've done */
824         printk(KERN_DEBUG "%s: " DRIVER_NAME " at %s, irq %d, io "
825                "0x%04x-0x%04x\n", dev->name, dev->class_dev.dev->bus_id,
826                link->irq.AssignedIRQ, link->io.BasePort1,
827                link->io.BasePort1 + link->io.NumPorts1 - 1);
828
829         return 0;
830
831  cs_failed:
832         cs_error(link, last_fn, last_ret);
833
834  failed:
835         spectrum_cs_release(link);
836         return -ENODEV;
837 }                               /* spectrum_cs_config */
838
839 /*
840  * After a card is removed, spectrum_cs_release() will unregister the
841  * device, and release the PCMCIA configuration.  If the device is
842  * still open, this will be postponed until it is closed.
843  */
844 static void
845 spectrum_cs_release(struct pcmcia_device *link)
846 {
847         struct net_device *dev = link->priv;
848         struct orinoco_private *priv = netdev_priv(dev);
849         unsigned long flags;
850
851         /* We're committed to taking the device away now, so mark the
852          * hardware as unavailable */
853         spin_lock_irqsave(&priv->lock, flags);
854         priv->hw_unavailable++;
855         spin_unlock_irqrestore(&priv->lock, flags);
856
857         pcmcia_disable_device(link);
858         if (priv->hw.iobase)
859                 ioport_unmap(priv->hw.iobase);
860 }                               /* spectrum_cs_release */
861
862
863 static int
864 spectrum_cs_suspend(struct pcmcia_device *link)
865 {
866         struct net_device *dev = link->priv;
867         struct orinoco_private *priv = netdev_priv(dev);
868         int err = 0;
869
870         /* Mark the device as stopped, to block IO until later */
871         spin_lock(&priv->lock);
872
873         err = __orinoco_down(dev);
874         if (err)
875                 printk(KERN_WARNING "%s: Error %d downing interface\n",
876                        dev->name, err);
877
878         netif_device_detach(dev);
879         priv->hw_unavailable++;
880
881         spin_unlock(&priv->lock);
882
883         return err;
884 }
885
886 static int
887 spectrum_cs_resume(struct pcmcia_device *link)
888 {
889         struct net_device *dev = link->priv;
890         struct orinoco_private *priv = netdev_priv(dev);
891
892         netif_device_attach(dev);
893         priv->hw_unavailable--;
894         schedule_work(&priv->reset_work);
895
896         return 0;
897 }
898
899
900 /********************************************************************/
901 /* Module initialization                                            */
902 /********************************************************************/
903
904 /* Can't be declared "const" or the whole __initdata section will
905  * become const */
906 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
907         " (Pavel Roskin <proski@gnu.org>,"
908         " David Gibson <hermes@gibson.dropbear.id.au>, et al)";
909
910 static struct pcmcia_device_id spectrum_cs_ids[] = {
911         PCMCIA_DEVICE_MANF_CARD(0x026c, 0x0001), /* Symbol Spectrum24 LA4137 */
912         PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0001), /* Socket Communications CF */
913         PCMCIA_DEVICE_PROD_ID12("Intel", "PRO/Wireless LAN PC Card", 0x816cc815, 0x6fbf459a), /* 2011B, not 2011 */
914         PCMCIA_DEVICE_NULL,
915 };
916 MODULE_DEVICE_TABLE(pcmcia, spectrum_cs_ids);
917
918 static struct pcmcia_driver orinoco_driver = {
919         .owner          = THIS_MODULE,
920         .drv            = {
921                 .name   = DRIVER_NAME,
922         },
923         .probe          = spectrum_cs_probe,
924         .remove         = spectrum_cs_detach,
925         .suspend        = spectrum_cs_suspend,
926         .resume         = spectrum_cs_resume,
927         .id_table       = spectrum_cs_ids,
928 };
929
930 static int __init
931 init_spectrum_cs(void)
932 {
933         printk(KERN_DEBUG "%s\n", version);
934
935         return pcmcia_register_driver(&orinoco_driver);
936 }
937
938 static void __exit
939 exit_spectrum_cs(void)
940 {
941         pcmcia_unregister_driver(&orinoco_driver);
942 }
943
944 module_init(init_spectrum_cs);
945 module_exit(exit_spectrum_cs);