]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/spectrum_cs.c
[PATCH] pcmcia: unify detach, REMOVAL_EVENT handlers into one remove callback
[linux-2.6-omap-h63xx.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 #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(struct pcmcia_device *p_dev);
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->handle);
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(struct pcmcia_device *p_dev)
664 {
665         dev_link_t *link = dev_to_instance(p_dev);
666         struct net_device *dev = link->priv;
667
668         if (link->state & DEV_CONFIG)
669                 spectrum_cs_release(link);
670
671         DEBUG(0, PFX "detach: link=%p link->dev=%p\n", link, link->dev);
672         if (link->dev) {
673                 DEBUG(0, PFX "About to unregister net device %p\n",
674                       dev);
675                 unregister_netdev(dev);
676         }
677         free_orinocodev(dev);
678 }                               /* spectrum_cs_detach */
679
680 /*
681  * spectrum_cs_config() is scheduled to run after a CARD_INSERTION
682  * event is received, to configure the PCMCIA socket, and to make the
683  * device available to the system.
684  */
685
686 static void
687 spectrum_cs_config(dev_link_t *link)
688 {
689         struct net_device *dev = link->priv;
690         client_handle_t handle = link->handle;
691         struct orinoco_private *priv = netdev_priv(dev);
692         struct orinoco_pccard *card = priv->card;
693         hermes_t *hw = &priv->hw;
694         int last_fn, last_ret;
695         u_char buf[64];
696         config_info_t conf;
697         cisinfo_t info;
698         tuple_t tuple;
699         cisparse_t parse;
700         void __iomem *mem;
701
702         CS_CHECK(ValidateCIS, pcmcia_validate_cis(handle, &info));
703
704         /*
705          * This reads the card's CONFIG tuple to find its
706          * configuration registers.
707          */
708         tuple.DesiredTuple = CISTPL_CONFIG;
709         tuple.Attributes = 0;
710         tuple.TupleData = buf;
711         tuple.TupleDataMax = sizeof(buf);
712         tuple.TupleOffset = 0;
713         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
714         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
715         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
716         link->conf.ConfigBase = parse.config.base;
717         link->conf.Present = parse.config.rmask[0];
718
719         /* Configure card */
720         link->state |= DEV_CONFIG;
721
722         /* Look up the current Vcc */
723         CS_CHECK(GetConfigurationInfo,
724                  pcmcia_get_configuration_info(handle, &conf));
725         link->conf.Vcc = conf.Vcc;
726
727         /*
728          * In this loop, we scan the CIS for configuration table
729          * entries, each of which describes a valid card
730          * configuration, including voltage, IO window, memory window,
731          * and interrupt settings.
732          *
733          * We make no assumptions about the card to be configured: we
734          * use just the information available in the CIS.  In an ideal
735          * world, this would work for any PCMCIA card, but it requires
736          * a complete and accurate CIS.  In practice, a driver usually
737          * "knows" most of these things without consulting the CIS,
738          * and most client drivers will only use the CIS to fill in
739          * implementation-defined details.
740          */
741         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
742         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
743         while (1) {
744                 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
745                 cistpl_cftable_entry_t dflt = { .index = 0 };
746
747                 if ( (pcmcia_get_tuple_data(handle, &tuple) != 0)
748                     || (pcmcia_parse_tuple(handle, &tuple, &parse) != 0))
749                         goto next_entry;
750
751                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
752                         dflt = *cfg;
753                 if (cfg->index == 0)
754                         goto next_entry;
755                 link->conf.ConfigIndex = cfg->index;
756
757                 /* Does this card need audio output? */
758                 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
759                         link->conf.Attributes |= CONF_ENABLE_SPKR;
760                         link->conf.Status = CCSR_AUDIO_ENA;
761                 }
762
763                 /* Use power settings for Vcc and Vpp if present */
764                 /* Note that the CIS values need to be rescaled */
765                 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
766                         if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
767                                 DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n",  conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
768                                 if (!ignore_cis_vcc)
769                                         goto next_entry;
770                         }
771                 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
772                         if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
773                                 DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n",  conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
774                                 if(!ignore_cis_vcc)
775                                         goto next_entry;
776                         }
777                 }
778
779                 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
780                         link->conf.Vpp1 = link->conf.Vpp2 =
781                             cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
782                 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
783                         link->conf.Vpp1 = link->conf.Vpp2 =
784                             dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
785                 
786                 /* Do we need to allocate an interrupt? */
787                 link->conf.Attributes |= CONF_ENABLE_IRQ;
788
789                 /* IO window settings */
790                 link->io.NumPorts1 = link->io.NumPorts2 = 0;
791                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
792                         cistpl_io_t *io =
793                             (cfg->io.nwin) ? &cfg->io : &dflt.io;
794                         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
795                         if (!(io->flags & CISTPL_IO_8BIT))
796                                 link->io.Attributes1 =
797                                     IO_DATA_PATH_WIDTH_16;
798                         if (!(io->flags & CISTPL_IO_16BIT))
799                                 link->io.Attributes1 =
800                                     IO_DATA_PATH_WIDTH_8;
801                         link->io.IOAddrLines =
802                             io->flags & CISTPL_IO_LINES_MASK;
803                         link->io.BasePort1 = io->win[0].base;
804                         link->io.NumPorts1 = io->win[0].len;
805                         if (io->nwin > 1) {
806                                 link->io.Attributes2 =
807                                     link->io.Attributes1;
808                                 link->io.BasePort2 = io->win[1].base;
809                                 link->io.NumPorts2 = io->win[1].len;
810                         }
811
812                         /* This reserves IO space but doesn't actually enable it */
813                         if (pcmcia_request_io(link->handle, &link->io) != 0)
814                                 goto next_entry;
815                 }
816
817
818                 /* If we got this far, we're cool! */
819
820                 break;
821                 
822         next_entry:
823                 if (link->io.NumPorts1)
824                         pcmcia_release_io(link->handle, &link->io);
825                 last_ret = pcmcia_get_next_tuple(handle, &tuple);
826                 if (last_ret  == CS_NO_MORE_ITEMS) {
827                         printk(KERN_ERR PFX "GetNextTuple(): No matching "
828                                "CIS configuration.  Maybe you need the "
829                                "ignore_cis_vcc=1 parameter.\n");
830                         goto cs_failed;
831                 }
832         }
833
834         /*
835          * Allocate an interrupt line.  Note that this does not assign
836          * a handler to the interrupt, unless the 'Handler' member of
837          * the irq structure is initialized.
838          */
839         CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
840
841         /* We initialize the hermes structure before completing PCMCIA
842          * configuration just in case the interrupt handler gets
843          * called. */
844         mem = ioport_map(link->io.BasePort1, link->io.NumPorts1);
845         if (!mem)
846                 goto cs_failed;
847
848         hermes_struct_init(hw, mem, HERMES_16BIT_REGSPACING);
849
850         /*
851          * This actually configures the PCMCIA socket -- setting up
852          * the I/O windows and the interrupt mapping, and putting the
853          * card and host interface into "Memory and IO" mode.
854          */
855         CS_CHECK(RequestConfiguration,
856                  pcmcia_request_configuration(link->handle, &link->conf));
857
858         /* Ok, we have the configuration, prepare to register the netdev */
859         dev->base_addr = link->io.BasePort1;
860         dev->irq = link->irq.AssignedIRQ;
861         SET_MODULE_OWNER(dev);
862         card->node.major = card->node.minor = 0;
863
864         /* Reset card and download firmware */
865         if (spectrum_cs_hard_reset(priv) != 0) {
866                 goto failed;
867         }
868
869         SET_NETDEV_DEV(dev, &handle_to_dev(handle));
870         /* Tell the stack we exist */
871         if (register_netdev(dev) != 0) {
872                 printk(KERN_ERR PFX "register_netdev() failed\n");
873                 goto failed;
874         }
875
876         /* At this point, the dev_node_t structure(s) needs to be
877          * initialized and arranged in a linked list at link->dev. */
878         strcpy(card->node.dev_name, dev->name);
879         link->dev = &card->node; /* link->dev being non-NULL is also
880                                     used to indicate that the
881                                     net_device has been registered */
882         link->state &= ~DEV_CONFIG_PENDING;
883
884         /* Finally, report what we've done */
885         printk(KERN_DEBUG "%s: index 0x%02x: Vcc %d.%d",
886                dev->name, link->conf.ConfigIndex,
887                link->conf.Vcc / 10, link->conf.Vcc % 10);
888         if (link->conf.Vpp1)
889                 printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
890                        link->conf.Vpp1 % 10);
891         printk(", irq %d", link->irq.AssignedIRQ);
892         if (link->io.NumPorts1)
893                 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
894                        link->io.BasePort1 + link->io.NumPorts1 - 1);
895         if (link->io.NumPorts2)
896                 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
897                        link->io.BasePort2 + link->io.NumPorts2 - 1);
898         printk("\n");
899
900         return;
901
902  cs_failed:
903         cs_error(link->handle, last_fn, last_ret);
904
905  failed:
906         spectrum_cs_release(link);
907 }                               /* spectrum_cs_config */
908
909 /*
910  * After a card is removed, spectrum_cs_release() will unregister the
911  * device, and release the PCMCIA configuration.  If the device is
912  * still open, this will be postponed until it is closed.
913  */
914 static void
915 spectrum_cs_release(dev_link_t *link)
916 {
917         struct net_device *dev = link->priv;
918         struct orinoco_private *priv = netdev_priv(dev);
919         unsigned long flags;
920
921         /* We're committed to taking the device away now, so mark the
922          * hardware as unavailable */
923         spin_lock_irqsave(&priv->lock, flags);
924         priv->hw_unavailable++;
925         spin_unlock_irqrestore(&priv->lock, flags);
926
927         /* Don't bother checking to see if these succeed or not */
928         pcmcia_release_configuration(link->handle);
929         if (link->io.NumPorts1)
930                 pcmcia_release_io(link->handle, &link->io);
931         if (link->irq.AssignedIRQ)
932                 pcmcia_release_irq(link->handle, &link->irq);
933         link->state &= ~DEV_CONFIG;
934         if (priv->hw.iobase)
935                 ioport_unmap(priv->hw.iobase);
936 }                               /* spectrum_cs_release */
937
938
939 static int
940 spectrum_cs_suspend(struct pcmcia_device *p_dev)
941 {
942         dev_link_t *link = dev_to_instance(p_dev);
943         struct net_device *dev = link->priv;
944         struct orinoco_private *priv = netdev_priv(dev);
945         unsigned long flags;
946         int err = 0;
947
948         link->state |= DEV_SUSPEND;
949         /* Mark the device as stopped, to block IO until later */
950         if (link->state & DEV_CONFIG) {
951                 spin_lock_irqsave(&priv->lock, flags);
952
953                 err = __orinoco_down(dev);
954                 if (err)
955                         printk(KERN_WARNING "%s: Error %d downing interface\n",
956                                dev->name, err);
957
958                 netif_device_detach(dev);
959                 priv->hw_unavailable++;
960
961                 spin_unlock_irqrestore(&priv->lock, flags);
962
963                 pcmcia_release_configuration(link->handle);
964         }
965
966         return 0;
967 }
968
969 static int
970 spectrum_cs_resume(struct pcmcia_device *p_dev)
971 {
972         dev_link_t *link = dev_to_instance(p_dev);
973         struct net_device *dev = link->priv;
974         struct orinoco_private *priv = netdev_priv(dev);
975
976         link->state &= ~DEV_SUSPEND;
977         if (link->state & DEV_CONFIG) {
978                 /* FIXME: should we double check that this is
979                  * the same card as we had before */
980                 pcmcia_request_configuration(link->handle, &link->conf);
981                 netif_device_attach(dev);
982                 priv->hw_unavailable--;
983                 schedule_work(&priv->reset_work);
984         }
985         return 0;
986 }
987
988 /*
989  * The card status event handler.  Mostly, this schedules other stuff
990  * to run after an event is received.
991  */
992 static int
993 spectrum_cs_event(event_t event, int priority,
994                        event_callback_args_t * args)
995 {
996         dev_link_t *link = args->client_data;
997
998         switch (event) {
999         case CS_EVENT_CARD_INSERTION:
1000                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1001                 spectrum_cs_config(link);
1002                 break;
1003
1004         }
1005
1006         return 0;
1007 }                               /* spectrum_cs_event */
1008
1009 /********************************************************************/
1010 /* Module initialization                                            */
1011 /********************************************************************/
1012
1013 /* Can't be declared "const" or the whole __initdata section will
1014  * become const */
1015 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
1016         " (Pavel Roskin <proski@gnu.org>,"
1017         " David Gibson <hermes@gibson.dropbear.id.au>, et al)";
1018
1019 static struct pcmcia_device_id spectrum_cs_ids[] = {
1020         PCMCIA_DEVICE_MANF_CARD(0x026c, 0x0001), /* Symbol Spectrum24 LA4100 */
1021         PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0001), /* Socket Communications CF */
1022         PCMCIA_DEVICE_PROD_ID12("Intel", "PRO/Wireless LAN PC Card", 0x816cc815, 0x6fbf459a), /* 2011B, not 2011 */
1023         PCMCIA_DEVICE_NULL,
1024 };
1025 MODULE_DEVICE_TABLE(pcmcia, spectrum_cs_ids);
1026
1027 static struct pcmcia_driver orinoco_driver = {
1028         .owner          = THIS_MODULE,
1029         .drv            = {
1030                 .name   = DRIVER_NAME,
1031         },
1032         .attach         = spectrum_cs_attach,
1033         .remove         = spectrum_cs_detach,
1034         .suspend        = spectrum_cs_suspend,
1035         .resume         = spectrum_cs_resume,
1036         .event          = spectrum_cs_event,
1037         .id_table       = spectrum_cs_ids,
1038 };
1039
1040 static int __init
1041 init_spectrum_cs(void)
1042 {
1043         printk(KERN_DEBUG "%s\n", version);
1044
1045         return pcmcia_register_driver(&orinoco_driver);
1046 }
1047
1048 static void __exit
1049 exit_spectrum_cs(void)
1050 {
1051         pcmcia_unregister_driver(&orinoco_driver);
1052         BUG_ON(dev_list != NULL);
1053 }
1054
1055 module_init(init_spectrum_cs);
1056 module_exit(exit_spectrum_cs);