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