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