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