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