Merge /spare/repo/netdev-2.6 branch 'ieee80211'
[linux-2.6] / drivers / net / wireless / hostap / hostap_cs.c
1 #define PRISM2_PCCARD
2
3 #include <linux/config.h>
4 #include <linux/version.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/if.h>
8 #include <linux/wait.h>
9 #include <linux/timer.h>
10 #include <linux/skbuff.h>
11 #include <linux/netdevice.h>
12 #include <linux/workqueue.h>
13 #include <linux/wireless.h>
14 #include <net/iw_handler.h>
15
16 #include <pcmcia/version.h>
17 #include <pcmcia/cs_types.h>
18 #include <pcmcia/cs.h>
19 #include <pcmcia/cistpl.h>
20 #include <pcmcia/cisreg.h>
21 #include <pcmcia/ds.h>
22
23 #include <asm/io.h>
24
25 #include "hostap_wlan.h"
26
27
28 static char *version = PRISM2_VERSION " (Jouni Malinen <jkmaline@cc.hut.fi>)";
29 static dev_info_t dev_info = "hostap_cs";
30 static dev_link_t *dev_list = NULL;
31
32 MODULE_AUTHOR("Jouni Malinen");
33 MODULE_DESCRIPTION("Support for Intersil Prism2-based 802.11 wireless LAN "
34                    "cards (PC Card).");
35 MODULE_SUPPORTED_DEVICE("Intersil Prism2-based WLAN cards (PC Card)");
36 MODULE_LICENSE("GPL");
37
38
39 static int irq_mask = 0xdeb8;
40 module_param(irq_mask, int, 0444);
41
42 static int irq_list[4] = { -1 };
43 module_param_array(irq_list, int, NULL, 0444);
44
45 static int ignore_cis_vcc;
46 module_param(ignore_cis_vcc, int, 0444);
47 MODULE_PARM_DESC(ignore_cis_vcc, "Ignore broken CIS VCC entry");
48
49
50 #ifdef PRISM2_IO_DEBUG
51
52 static inline void hfa384x_outb_debug(struct net_device *dev, int a, u8 v)
53 {
54         struct hostap_interface *iface;
55         local_info_t *local;
56         unsigned long flags;
57
58         iface = netdev_priv(dev);
59         local = iface->local;
60         spin_lock_irqsave(&local->lock, flags);
61         prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_OUTB, a, v);
62         outb(v, dev->base_addr + a);
63         spin_unlock_irqrestore(&local->lock, flags);
64 }
65
66 static inline u8 hfa384x_inb_debug(struct net_device *dev, int a)
67 {
68         struct hostap_interface *iface;
69         local_info_t *local;
70         unsigned long flags;
71         u8 v;
72
73         iface = netdev_priv(dev);
74         local = iface->local;
75         spin_lock_irqsave(&local->lock, flags);
76         v = inb(dev->base_addr + a);
77         prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INB, a, v);
78         spin_unlock_irqrestore(&local->lock, flags);
79         return v;
80 }
81
82 static inline void hfa384x_outw_debug(struct net_device *dev, int a, u16 v)
83 {
84         struct hostap_interface *iface;
85         local_info_t *local;
86         unsigned long flags;
87
88         iface = netdev_priv(dev);
89         local = iface->local;
90         spin_lock_irqsave(&local->lock, flags);
91         prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_OUTW, a, v);
92         outw(v, dev->base_addr + a);
93         spin_unlock_irqrestore(&local->lock, flags);
94 }
95
96 static inline u16 hfa384x_inw_debug(struct net_device *dev, int a)
97 {
98         struct hostap_interface *iface;
99         local_info_t *local;
100         unsigned long flags;
101         u16 v;
102
103         iface = netdev_priv(dev);
104         local = iface->local;
105         spin_lock_irqsave(&local->lock, flags);
106         v = inw(dev->base_addr + a);
107         prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INW, a, v);
108         spin_unlock_irqrestore(&local->lock, flags);
109         return v;
110 }
111
112 static inline void hfa384x_outsw_debug(struct net_device *dev, int a,
113                                        u8 *buf, int wc)
114 {
115         struct hostap_interface *iface;
116         local_info_t *local;
117         unsigned long flags;
118
119         iface = netdev_priv(dev);
120         local = iface->local;
121         spin_lock_irqsave(&local->lock, flags);
122         prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_OUTSW, a, wc);
123         outsw(dev->base_addr + a, buf, wc);
124         spin_unlock_irqrestore(&local->lock, flags);
125 }
126
127 static inline void hfa384x_insw_debug(struct net_device *dev, int a,
128                                       u8 *buf, int wc)
129 {
130         struct hostap_interface *iface;
131         local_info_t *local;
132         unsigned long flags;
133
134         iface = netdev_priv(dev);
135         local = iface->local;
136         spin_lock_irqsave(&local->lock, flags);
137         prism2_io_debug_add(dev, PRISM2_IO_DEBUG_CMD_INSW, a, wc);
138         insw(dev->base_addr + a, buf, wc);
139         spin_unlock_irqrestore(&local->lock, flags);
140 }
141
142 #define HFA384X_OUTB(v,a) hfa384x_outb_debug(dev, (a), (v))
143 #define HFA384X_INB(a) hfa384x_inb_debug(dev, (a))
144 #define HFA384X_OUTW(v,a) hfa384x_outw_debug(dev, (a), (v))
145 #define HFA384X_INW(a) hfa384x_inw_debug(dev, (a))
146 #define HFA384X_OUTSW(a, buf, wc) hfa384x_outsw_debug(dev, (a), (buf), (wc))
147 #define HFA384X_INSW(a, buf, wc) hfa384x_insw_debug(dev, (a), (buf), (wc))
148
149 #else /* PRISM2_IO_DEBUG */
150
151 #define HFA384X_OUTB(v,a) outb((v), dev->base_addr + (a))
152 #define HFA384X_INB(a) inb(dev->base_addr + (a))
153 #define HFA384X_OUTW(v,a) outw((v), dev->base_addr + (a))
154 #define HFA384X_INW(a) inw(dev->base_addr + (a))
155 #define HFA384X_INSW(a, buf, wc) insw(dev->base_addr + (a), buf, wc)
156 #define HFA384X_OUTSW(a, buf, wc) outsw(dev->base_addr + (a), buf, wc)
157
158 #endif /* PRISM2_IO_DEBUG */
159
160
161 static int hfa384x_from_bap(struct net_device *dev, u16 bap, void *buf,
162                             int len)
163 {
164         u16 d_off;
165         u16 *pos;
166
167         d_off = (bap == 1) ? HFA384X_DATA1_OFF : HFA384X_DATA0_OFF;
168         pos = (u16 *) buf;
169
170         if (len / 2)
171                 HFA384X_INSW(d_off, buf, len / 2);
172         pos += len / 2;
173
174         if (len & 1)
175                 *((char *) pos) = HFA384X_INB(d_off);
176
177         return 0;
178 }
179
180
181 static int hfa384x_to_bap(struct net_device *dev, u16 bap, void *buf, int len)
182 {
183         u16 d_off;
184         u16 *pos;
185
186         d_off = (bap == 1) ? HFA384X_DATA1_OFF : HFA384X_DATA0_OFF;
187         pos = (u16 *) buf;
188
189         if (len / 2)
190                 HFA384X_OUTSW(d_off, buf, len / 2);
191         pos += len / 2;
192
193         if (len & 1)
194                 HFA384X_OUTB(*((char *) pos), d_off);
195
196         return 0;
197 }
198
199
200 /* FIX: This might change at some point.. */
201 #include "hostap_hw.c"
202
203
204
205 static void prism2_detach(dev_link_t *link);
206 static void prism2_release(u_long arg);
207 static int prism2_event(event_t event, int priority,
208                         event_callback_args_t *args);
209
210
211 static int prism2_pccard_card_present(local_info_t *local)
212 {
213         if (local->link != NULL &&
214             ((local->link->state & (DEV_PRESENT | DEV_CONFIG)) ==
215              (DEV_PRESENT | DEV_CONFIG)))
216                 return 1;
217         return 0;
218 }
219
220
221 /*
222  * SanDisk CompactFlash WLAN Flashcard - Product Manual v1.0
223  * Document No. 20-10-00058, January 2004
224  * http://www.sandisk.com/pdf/industrial/ProdManualCFWLANv1.0.pdf
225  */
226 #define SANDISK_WLAN_ACTIVATION_OFF 0x40
227 #define SANDISK_HCR_OFF 0x42
228
229
230 static void sandisk_set_iobase(local_info_t *local)
231 {
232         int res;
233         conf_reg_t reg;
234
235         reg.Function = 0;
236         reg.Action = CS_WRITE;
237         reg.Offset = 0x10; /* 0x3f0 IO base 1 */
238         reg.Value = local->link->io.BasePort1 & 0x00ff;
239         res = pcmcia_access_configuration_register(local->link->handle, &reg);
240         if (res != CS_SUCCESS) {
241                 printk(KERN_DEBUG "Prism3 SanDisk - failed to set I/O base 0 -"
242                        " res=%d\n", res);
243         }
244         udelay(10);
245
246         reg.Function = 0;
247         reg.Action = CS_WRITE;
248         reg.Offset = 0x12; /* 0x3f2 IO base 2 */
249         reg.Value = (local->link->io.BasePort1 & 0xff00) >> 8;
250         res = pcmcia_access_configuration_register(local->link->handle, &reg);
251         if (res != CS_SUCCESS) {
252                 printk(KERN_DEBUG "Prism3 SanDisk - failed to set I/O base 1 -"
253                        " res=%d\n", res);
254         }
255 }
256
257
258 static void sandisk_write_hcr(local_info_t *local, int hcr)
259 {
260         struct net_device *dev = local->dev;
261         int i;
262
263         HFA384X_OUTB(0x80, SANDISK_WLAN_ACTIVATION_OFF);
264         udelay(50);
265         for (i = 0; i < 10; i++) {
266                 HFA384X_OUTB(hcr, SANDISK_HCR_OFF);
267         }
268         udelay(55);
269         HFA384X_OUTB(0x45, SANDISK_WLAN_ACTIVATION_OFF);
270 }
271
272
273 static int sandisk_enable_wireless(struct net_device *dev)
274 {
275         int res, ret = 0;
276         conf_reg_t reg;
277         struct hostap_interface *iface = dev->priv;
278         local_info_t *local = iface->local;
279         tuple_t tuple;
280         cisparse_t *parse = NULL;
281         u_char buf[64];
282
283         if (local->link->io.NumPorts1 < 0x42) {
284                 /* Not enough ports to be SanDisk multi-function card */
285                 ret = -ENODEV;
286                 goto done;
287         }
288
289         parse = kmalloc(sizeof(cisparse_t), GFP_KERNEL);
290         if (parse == NULL) {
291                 ret = -ENOMEM;
292                 goto done;
293         }
294
295         tuple.DesiredTuple = CISTPL_MANFID;
296         tuple.Attributes = TUPLE_RETURN_COMMON;
297         tuple.TupleData = buf;
298         tuple.TupleDataMax = sizeof(buf);
299         tuple.TupleOffset = 0;
300         if (pcmcia_get_first_tuple(local->link->handle, &tuple) ||
301             pcmcia_get_tuple_data(local->link->handle, &tuple) ||
302             pcmcia_parse_tuple(local->link->handle, &tuple, parse) ||
303             parse->manfid.manf != 0xd601 || parse->manfid.card != 0x0101) {
304                 /* No SanDisk manfid found */
305                 ret = -ENODEV;
306                 goto done;
307         }
308
309         tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
310         if (pcmcia_get_first_tuple(local->link->handle, &tuple) ||
311             pcmcia_get_tuple_data(local->link->handle, &tuple) ||
312             pcmcia_parse_tuple(local->link->handle, &tuple, parse) ||
313                 parse->longlink_mfc.nfn < 2) {
314                 /* No multi-function links found */
315                 ret = -ENODEV;
316                 goto done;
317         }
318
319         printk(KERN_DEBUG "%s: Multi-function SanDisk ConnectPlus detected"
320                " - using vendor-specific initialization\n", dev->name);
321         local->sandisk_connectplus = 1;
322
323         reg.Function = 0;
324         reg.Action = CS_WRITE;
325         reg.Offset = CISREG_COR;
326         reg.Value = COR_SOFT_RESET;
327         res = pcmcia_access_configuration_register(local->link->handle, &reg);
328         if (res != CS_SUCCESS) {
329                 printk(KERN_DEBUG "%s: SanDisk - COR sreset failed (%d)\n",
330                        dev->name, res);
331                 goto done;
332         }
333         mdelay(5);
334
335         reg.Function = 0;
336         reg.Action = CS_WRITE;
337         reg.Offset = CISREG_COR;
338         /*
339          * Do not enable interrupts here to avoid some bogus events. Interrupts
340          * will be enabled during the first cor_sreset call.
341          */
342         reg.Value = COR_LEVEL_REQ | 0x8 | COR_ADDR_DECODE | COR_FUNC_ENA;
343         res = pcmcia_access_configuration_register(local->link->handle, &reg);
344         if (res != CS_SUCCESS) {
345                 printk(KERN_DEBUG "%s: SanDisk - COR sreset failed (%d)\n",
346                        dev->name, res);
347                 goto done;
348         }
349         mdelay(5);
350
351         sandisk_set_iobase(local);
352
353         HFA384X_OUTB(0xc5, SANDISK_WLAN_ACTIVATION_OFF);
354         udelay(10);
355         HFA384X_OUTB(0x4b, SANDISK_WLAN_ACTIVATION_OFF);
356         udelay(10);
357
358 done:
359         kfree(parse);
360         return ret;
361 }
362
363
364 static void prism2_pccard_cor_sreset(local_info_t *local)
365 {
366         int res;
367         conf_reg_t reg;
368
369         if (!prism2_pccard_card_present(local))
370                return;
371
372         reg.Function = 0;
373         reg.Action = CS_READ;
374         reg.Offset = CISREG_COR;
375         reg.Value = 0;
376         res = pcmcia_access_configuration_register(local->link->handle, &reg);
377         if (res != CS_SUCCESS) {
378                 printk(KERN_DEBUG "prism2_pccard_cor_sreset failed 1 (%d)\n",
379                        res);
380                 return;
381         }
382         printk(KERN_DEBUG "prism2_pccard_cor_sreset: original COR %02x\n",
383                reg.Value);
384
385         reg.Action = CS_WRITE;
386         reg.Value |= COR_SOFT_RESET;
387         res = pcmcia_access_configuration_register(local->link->handle, &reg);
388         if (res != CS_SUCCESS) {
389                 printk(KERN_DEBUG "prism2_pccard_cor_sreset failed 2 (%d)\n",
390                        res);
391                 return;
392         }
393
394         mdelay(local->sandisk_connectplus ? 5 : 2);
395
396         reg.Value &= ~COR_SOFT_RESET;
397         if (local->sandisk_connectplus)
398                 reg.Value |= COR_IREQ_ENA;
399         res = pcmcia_access_configuration_register(local->link->handle, &reg);
400         if (res != CS_SUCCESS) {
401                 printk(KERN_DEBUG "prism2_pccard_cor_sreset failed 3 (%d)\n",
402                        res);
403                 return;
404         }
405
406         mdelay(local->sandisk_connectplus ? 5 : 2);
407
408         if (local->sandisk_connectplus)
409                 sandisk_set_iobase(local);
410 }
411
412
413 static void prism2_pccard_genesis_reset(local_info_t *local, int hcr)
414 {
415         int res;
416         conf_reg_t reg;
417         int old_cor;
418
419         if (!prism2_pccard_card_present(local))
420                return;
421
422         if (local->sandisk_connectplus) {
423                 sandisk_write_hcr(local, hcr);
424                 return;
425         }
426
427         reg.Function = 0;
428         reg.Action = CS_READ;
429         reg.Offset = CISREG_COR;
430         reg.Value = 0;
431         res = pcmcia_access_configuration_register(local->link->handle, &reg);
432         if (res != CS_SUCCESS) {
433                 printk(KERN_DEBUG "prism2_pccard_genesis_sreset failed 1 "
434                        "(%d)\n", res);
435                 return;
436         }
437         printk(KERN_DEBUG "prism2_pccard_genesis_sreset: original COR %02x\n",
438                reg.Value);
439         old_cor = reg.Value;
440
441         reg.Action = CS_WRITE;
442         reg.Value |= COR_SOFT_RESET;
443         res = pcmcia_access_configuration_register(local->link->handle, &reg);
444         if (res != CS_SUCCESS) {
445                 printk(KERN_DEBUG "prism2_pccard_genesis_sreset failed 2 "
446                        "(%d)\n", res);
447                 return;
448         }
449
450         mdelay(10);
451
452         /* Setup Genesis mode */
453         reg.Action = CS_WRITE;
454         reg.Value = hcr;
455         reg.Offset = CISREG_CCSR;
456         res = pcmcia_access_configuration_register(local->link->handle, &reg);
457         if (res != CS_SUCCESS) {
458                 printk(KERN_DEBUG "prism2_pccard_genesis_sreset failed 3 "
459                        "(%d)\n", res);
460                 return;
461         }
462         mdelay(10);
463
464         reg.Action = CS_WRITE;
465         reg.Offset = CISREG_COR;
466         reg.Value = old_cor & ~COR_SOFT_RESET;
467         res = pcmcia_access_configuration_register(local->link->handle, &reg);
468         if (res != CS_SUCCESS) {
469                 printk(KERN_DEBUG "prism2_pccard_genesis_sreset failed 4 "
470                        "(%d)\n", res);
471                 return;
472         }
473
474         mdelay(10);
475 }
476
477
478 static int prism2_pccard_dev_open(local_info_t *local)
479 {
480         local->link->open++;
481         return 0;
482 }
483
484
485 static int prism2_pccard_dev_close(local_info_t *local)
486 {
487         if (local == NULL || local->link == NULL)
488                 return 1;
489
490         if (!local->link->open) {
491                 printk(KERN_WARNING "%s: prism2_pccard_dev_close(): "
492                        "link not open?!\n", local->dev->name);
493                 return 1;
494         }
495
496         local->link->open--;
497
498         return 0;
499 }
500
501
502 static struct prism2_helper_functions prism2_pccard_funcs =
503 {
504         .card_present   = prism2_pccard_card_present,
505         .cor_sreset     = prism2_pccard_cor_sreset,
506         .dev_open       = prism2_pccard_dev_open,
507         .dev_close      = prism2_pccard_dev_close,
508         .genesis_reset  = prism2_pccard_genesis_reset,
509         .hw_type        = HOSTAP_HW_PCCARD,
510 };
511
512
513 /* allocate local data and register with CardServices
514  * initialize dev_link structure, but do not configure the card yet */
515 static dev_link_t *prism2_attach(void)
516 {
517         dev_link_t *link;
518         client_reg_t client_reg;
519         int ret;
520
521         link = kmalloc(sizeof(dev_link_t), GFP_KERNEL);
522         if (link == NULL)
523                 return NULL;
524
525         memset(link, 0, sizeof(dev_link_t));
526
527         PDEBUG(DEBUG_HW, "%s: setting Vcc=33 (constant)\n", dev_info);
528         link->conf.Vcc = 33;
529         link->conf.IntType = INT_MEMORY_AND_IO;
530
531         /* register with CardServices */
532         link->next = dev_list;
533         dev_list = link;
534         client_reg.dev_info = &dev_info;
535         client_reg.Attributes = INFO_IO_CLIENT;
536         client_reg.EventMask = CS_EVENT_CARD_INSERTION |
537                 CS_EVENT_CARD_REMOVAL |
538                 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
539                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
540         client_reg.event_handler = &prism2_event;
541         client_reg.Version = 0x0210;
542         client_reg.event_callback_args.client_data = link;
543         ret = pcmcia_register_client(&link->handle, &client_reg);
544         if (ret != CS_SUCCESS) {
545                 cs_error(link->handle, RegisterClient, ret);
546                 prism2_detach(link);
547                 return NULL;
548         }
549         return link;
550 }
551
552
553 static void prism2_detach(dev_link_t *link)
554 {
555         dev_link_t **linkp;
556
557         PDEBUG(DEBUG_FLOW, "prism2_detach\n");
558
559         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
560                 if (*linkp == link)
561                         break;
562         if (*linkp == NULL) {
563                 printk(KERN_WARNING "%s: Attempt to detach non-existing "
564                        "PCMCIA client\n", dev_info);
565                 return;
566         }
567
568         if (link->state & DEV_CONFIG) {
569                 prism2_release((u_long)link);
570         }
571
572         if (link->handle) {
573                 int res = pcmcia_deregister_client(link->handle);
574                 if (res) {
575                         printk("CardService(DeregisterClient) => %d\n", res);
576                         cs_error(link->handle, DeregisterClient, res);
577                 }
578         }
579
580         *linkp = link->next;
581         /* release net devices */
582         if (link->priv) {
583                 prism2_free_local_data((struct net_device *) link->priv);
584
585         }
586         kfree(link);
587 }
588
589
590 #define CS_CHECK(fn, ret) \
591 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
592
593 #define CFG_CHECK2(fn, retf) \
594 do { int ret = (retf); \
595 if (ret != 0) { \
596         PDEBUG(DEBUG_EXTRA, "CardServices(" #fn ") returned %d\n", ret); \
597         cs_error(link->handle, fn, ret); \
598         goto next_entry; \
599 } \
600 } while (0)
601
602
603 /* run after a CARD_INSERTION event is received to configure the PCMCIA
604  * socket and make the device available to the system */
605 static int prism2_config(dev_link_t *link)
606 {
607         struct net_device *dev;
608         struct hostap_interface *iface;
609         local_info_t *local;
610         int ret = 1;
611         tuple_t tuple;
612         cisparse_t *parse;
613         int last_fn, last_ret;
614         u_char buf[64];
615         config_info_t conf;
616         cistpl_cftable_entry_t dflt = { 0 };
617
618         PDEBUG(DEBUG_FLOW, "prism2_config()\n");
619
620         parse = kmalloc(sizeof(cisparse_t), GFP_KERNEL);
621         if (parse == NULL) {
622                 ret = -ENOMEM;
623                 goto failed;
624         }
625
626         tuple.DesiredTuple = CISTPL_CONFIG;
627         tuple.Attributes = 0;
628         tuple.TupleData = buf;
629         tuple.TupleDataMax = sizeof(buf);
630         tuple.TupleOffset = 0;
631         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link->handle, &tuple));
632         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link->handle, &tuple));
633         CS_CHECK(ParseTuple, pcmcia_parse_tuple(link->handle, &tuple, parse));
634         link->conf.ConfigBase = parse->config.base;
635         link->conf.Present = parse->config.rmask[0];
636
637         CS_CHECK(GetConfigurationInfo,
638                  pcmcia_get_configuration_info(link->handle, &conf));
639         PDEBUG(DEBUG_HW, "%s: %s Vcc=%d (from config)\n", dev_info,
640                ignore_cis_vcc ? "ignoring" : "setting", conf.Vcc);
641         link->conf.Vcc = conf.Vcc;
642
643         /* Look for an appropriate configuration table entry in the CIS */
644         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
645         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link->handle, &tuple));
646         for (;;) {
647                 cistpl_cftable_entry_t *cfg = &(parse->cftable_entry);
648                 CFG_CHECK2(GetTupleData,
649                            pcmcia_get_tuple_data(link->handle, &tuple));
650                 CFG_CHECK2(ParseTuple,
651                            pcmcia_parse_tuple(link->handle, &tuple, parse));
652
653                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
654                         dflt = *cfg;
655                 if (cfg->index == 0)
656                         goto next_entry;
657                 link->conf.ConfigIndex = cfg->index;
658                 PDEBUG(DEBUG_EXTRA, "Checking CFTABLE_ENTRY 0x%02X "
659                        "(default 0x%02X)\n", cfg->index, dflt.index);
660         
661                 /* Does this card need audio output? */
662                 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
663                         link->conf.Attributes |= CONF_ENABLE_SPKR;
664                         link->conf.Status = CCSR_AUDIO_ENA;
665                 }
666         
667                 /* Use power settings for Vcc and Vpp if present */
668                 /*  Note that the CIS values need to be rescaled */
669                 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
670                         if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] /
671                             10000 && !ignore_cis_vcc) {
672                                 PDEBUG(DEBUG_EXTRA, "  Vcc mismatch - skipping"
673                                        " this entry\n");
674                                 goto next_entry;
675                         }
676                 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
677                         if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] /
678                             10000 && !ignore_cis_vcc) {
679                                 PDEBUG(DEBUG_EXTRA, "  Vcc (default) mismatch "
680                                        "- skipping this entry\n");
681                                 goto next_entry;
682                         }
683                 }
684
685                 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
686                         link->conf.Vpp1 = link->conf.Vpp2 =
687                                 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
688                 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
689                         link->conf.Vpp1 = link->conf.Vpp2 =
690                                 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
691
692                 /* Do we need to allocate an interrupt? */
693                 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
694                         link->conf.Attributes |= CONF_ENABLE_IRQ;
695                 else if (!(link->conf.Attributes & CONF_ENABLE_IRQ)) {
696                         /* At least Compaq WL200 does not have IRQInfo1 set,
697                          * but it does not work without interrupts.. */
698                         printk("Config has no IRQ info, but trying to enable "
699                                "IRQ anyway..\n");
700                         link->conf.Attributes |= CONF_ENABLE_IRQ;
701                 }
702
703                 /* IO window settings */
704                 PDEBUG(DEBUG_EXTRA, "IO window settings: cfg->io.nwin=%d "
705                        "dflt.io.nwin=%d\n",
706                        cfg->io.nwin, dflt.io.nwin);
707                 link->io.NumPorts1 = link->io.NumPorts2 = 0;
708                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
709                         cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
710                         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
711                         PDEBUG(DEBUG_EXTRA, "io->flags = 0x%04X, "
712                                "io.base=0x%04x, len=%d\n", io->flags,
713                                io->win[0].base, io->win[0].len);
714                         if (!(io->flags & CISTPL_IO_8BIT))
715                                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
716                         if (!(io->flags & CISTPL_IO_16BIT))
717                                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
718                         link->io.IOAddrLines = io->flags &
719                                 CISTPL_IO_LINES_MASK;
720                         link->io.BasePort1 = io->win[0].base;
721                         link->io.NumPorts1 = io->win[0].len;
722                         if (io->nwin > 1) {
723                                 link->io.Attributes2 = link->io.Attributes1;
724                                 link->io.BasePort2 = io->win[1].base;
725                                 link->io.NumPorts2 = io->win[1].len;
726                         }
727                 }
728
729                 /* This reserves IO space but doesn't actually enable it */
730                 CFG_CHECK2(RequestIO,
731                            pcmcia_request_io(link->handle, &link->io));
732
733                 /* This configuration table entry is OK */
734                 break;
735
736         next_entry:
737                 CS_CHECK(GetNextTuple,
738                          pcmcia_get_next_tuple(link->handle, &tuple));
739         }
740
741         /* Need to allocate net_device before requesting IRQ handler */
742         dev = prism2_init_local_data(&prism2_pccard_funcs, 0);
743         if (dev == NULL)
744                 goto failed;
745         link->priv = dev;
746
747         /*
748          * Allocate an interrupt line.  Note that this does not assign a
749          * handler to the interrupt, unless the 'Handler' member of the
750          * irq structure is initialized.
751          */
752         if (link->conf.Attributes & CONF_ENABLE_IRQ) {
753                 int i;
754                 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
755                 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
756                 if (irq_list[0] == -1)
757                         link->irq.IRQInfo2 = irq_mask;
758                 else
759                         for (i = 0; i < 4; i++)
760                                 link->irq.IRQInfo2 |= 1 << irq_list[i];
761                 link->irq.Handler = prism2_interrupt;
762                 link->irq.Instance = dev;
763                 CS_CHECK(RequestIRQ,
764                          pcmcia_request_irq(link->handle, &link->irq));
765         }
766
767         /*
768          * This actually configures the PCMCIA socket -- setting up
769          * the I/O windows and the interrupt mapping, and putting the
770          * card and host interface into "Memory and IO" mode.
771          */
772         CS_CHECK(RequestConfiguration,
773                  pcmcia_request_configuration(link->handle, &link->conf));
774
775         dev->irq = link->irq.AssignedIRQ;
776         dev->base_addr = link->io.BasePort1;
777
778         /* Finally, report what we've done */
779         printk(KERN_INFO "%s: index 0x%02x: Vcc %d.%d",
780                dev_info, link->conf.ConfigIndex,
781                link->conf.Vcc / 10, link->conf.Vcc % 10);
782         if (link->conf.Vpp1)
783                 printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
784                        link->conf.Vpp1 % 10);
785         if (link->conf.Attributes & CONF_ENABLE_IRQ)
786                 printk(", irq %d", link->irq.AssignedIRQ);
787         if (link->io.NumPorts1)
788                 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
789                        link->io.BasePort1+link->io.NumPorts1-1);
790         if (link->io.NumPorts2)
791                 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
792                        link->io.BasePort2+link->io.NumPorts2-1);
793         printk("\n");
794
795         link->state |= DEV_CONFIG;
796         link->state &= ~DEV_CONFIG_PENDING;
797
798         iface = netdev_priv(dev);
799         local = iface->local;
800         local->link = link;
801         strcpy(local->node.dev_name, dev->name);
802         link->dev = &local->node;
803
804         local->shutdown = 0;
805
806         sandisk_enable_wireless(dev);
807
808         ret = prism2_hw_config(dev, 1);
809         if (!ret) {
810                 ret = hostap_hw_ready(dev);
811                 if (ret == 0 && local->ddev)
812                         strcpy(local->node.dev_name, local->ddev->name);
813         }
814         kfree(parse);
815         return ret;
816
817  cs_failed:
818         cs_error(link->handle, last_fn, last_ret);
819
820  failed:
821         kfree(parse);
822         prism2_release((u_long)link);
823         return ret;
824 }
825
826
827 static void prism2_release(u_long arg)
828 {
829         dev_link_t *link = (dev_link_t *)arg;
830
831         PDEBUG(DEBUG_FLOW, "prism2_release\n");
832
833         if (link->priv) {
834                 struct net_device *dev = link->priv;
835                 struct hostap_interface *iface;
836
837                 iface = netdev_priv(dev);
838                 if (link->state & DEV_CONFIG)
839                         prism2_hw_shutdown(dev, 0);
840                 iface->local->shutdown = 1;
841         }
842
843         if (link->win)
844                 pcmcia_release_window(link->win);
845         pcmcia_release_configuration(link->handle);
846         if (link->io.NumPorts1)
847                 pcmcia_release_io(link->handle, &link->io);
848         if (link->irq.AssignedIRQ)
849                 pcmcia_release_irq(link->handle, &link->irq);
850
851         link->state &= ~DEV_CONFIG;
852
853         PDEBUG(DEBUG_FLOW, "release - done\n");
854 }
855
856
857 static int prism2_event(event_t event, int priority,
858                         event_callback_args_t *args)
859 {
860         dev_link_t *link = args->client_data;
861         struct net_device *dev = (struct net_device *) link->priv;
862
863         switch (event) {
864         case CS_EVENT_CARD_INSERTION:
865                 PDEBUG(DEBUG_EXTRA, "%s: CS_EVENT_CARD_INSERTION\n", dev_info);
866                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
867                 if (prism2_config(link)) {
868                         PDEBUG(DEBUG_EXTRA, "prism2_config() failed\n");
869                 }
870                 break;
871
872         case CS_EVENT_CARD_REMOVAL:
873                 PDEBUG(DEBUG_EXTRA, "%s: CS_EVENT_CARD_REMOVAL\n", dev_info);
874                 link->state &= ~DEV_PRESENT;
875                 if (link->state & DEV_CONFIG) {
876                         netif_stop_queue(dev);
877                         netif_device_detach(dev);
878                         prism2_release((u_long) link);
879                 }
880                 break;
881
882         case CS_EVENT_PM_SUSPEND:
883                 PDEBUG(DEBUG_EXTRA, "%s: CS_EVENT_PM_SUSPEND\n", dev_info);
884                 link->state |= DEV_SUSPEND;
885                 /* fall through */
886
887         case CS_EVENT_RESET_PHYSICAL:
888                 PDEBUG(DEBUG_EXTRA, "%s: CS_EVENT_RESET_PHYSICAL\n", dev_info);
889                 if (link->state & DEV_CONFIG) {
890                         if (link->open) {
891                                 netif_stop_queue(dev);
892                                 netif_device_detach(dev);
893                         }
894                         prism2_suspend(dev);
895                         pcmcia_release_configuration(link->handle);
896                 }
897                 break;
898
899         case CS_EVENT_PM_RESUME:
900                 PDEBUG(DEBUG_EXTRA, "%s: CS_EVENT_PM_RESUME\n", dev_info);
901                 link->state &= ~DEV_SUSPEND;
902                 /* fall through */
903
904         case CS_EVENT_CARD_RESET:
905                 PDEBUG(DEBUG_EXTRA, "%s: CS_EVENT_CARD_RESET\n", dev_info);
906                 if (link->state & DEV_CONFIG) {
907                         pcmcia_request_configuration(link->handle,
908                                                      &link->conf);
909                         prism2_hw_shutdown(dev, 1);
910                         prism2_hw_config(dev, link->open ? 0 : 1);
911                         if (link->open) {
912                                 netif_device_attach(dev);
913                                 netif_start_queue(dev);
914                         }
915                 }
916                 break;
917
918         default:
919                 PDEBUG(DEBUG_EXTRA, "%s: prism2_event() - unknown event %d\n",
920                        dev_info, event);
921                 break;
922         }
923         return 0;
924 }
925
926
927 static struct pcmcia_driver hostap_driver = {
928         .drv            = {
929                 .name   = "hostap_cs",
930         },
931         .attach         = prism2_attach,
932         .detach         = prism2_detach,
933         .owner          = THIS_MODULE,
934 };
935
936 static int __init init_prism2_pccard(void)
937 {
938         printk(KERN_INFO "%s: %s\n", dev_info, version);
939         return pcmcia_register_driver(&hostap_driver);
940 }
941
942 static void __exit exit_prism2_pccard(void)
943 {
944         pcmcia_unregister_driver(&hostap_driver);
945         printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
946 }
947
948
949 module_init(init_prism2_pccard);
950 module_exit(exit_prism2_pccard);