1 /* orinoco_cs.c 0.13e - (formerly known as dldwd_cs.c)
3 * A driver for "Hermes" chipset based PCMCIA wireless adaptors, such
4 * as the Lucent WavelanIEEE/Orinoco cards and their OEM (Cabletron/
5 * EnteraSys RoamAbout 802.11, ELSA Airlancer, Melco Buffalo and others).
6 * It should also be usable on various Prism II based cards such as the
7 * Linksys, D-Link and Farallon Skyline. It should also work on Symbol
8 * cards such as the 3Com AirConnect and Ericsson WLAN.
10 * Copyright notice & release notes in file orinoco.c
13 #include <linux/config.h>
14 #ifdef __IN_PCMCIA_PACKAGE__
15 #include <pcmcia/k_compat.h>
16 #endif /* __IN_PCMCIA_PACKAGE__ */
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>
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>
38 #include <asm/uaccess.h>
40 #include <asm/system.h>
44 /********************************************************************/
46 /********************************************************************/
48 MODULE_AUTHOR("David Gibson <hermes@gibson.dropbear.id.au>");
49 MODULE_DESCRIPTION("Driver for PCMCIA Lucent Orinoco, Prism II based and similar wireless cards");
51 MODULE_LICENSE("Dual MPL/GPL");
54 /* Module parameters */
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 };
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 */
66 MODULE_PARM(irq_mask, "i");
67 MODULE_PARM(irq_list, "1-4i");
68 MODULE_PARM(ignore_cis_vcc, "i");
70 /********************************************************************/
72 /********************************************************************/
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.
79 static dev_info_t dev_info = "orinoco_cs";
81 /********************************************************************/
83 /********************************************************************/
85 /* PCMCIA specific device information (goes in the card field of
86 * struct orinoco_private */
87 struct orinoco_pccard {
91 /* Used to handle hard reset */
92 /* yuck, we need this hack to work around the insanity of the
94 unsigned long hard_reset_in_progress;
98 * A linked list of "instances" of the device. Each actual PCMCIA
99 * card corresponds to one device instance, and is described by one
100 * dev_link_t structure (defined in ds.h).
102 static dev_link_t *dev_list; /* = NULL */
104 /********************************************************************/
105 /* Function prototypes */
106 /********************************************************************/
109 static int orinoco_cs_hard_reset(struct orinoco_private *priv);
112 static void orinoco_cs_config(dev_link_t * link);
113 static void orinoco_cs_release(u_long arg);
114 static int orinoco_cs_event(event_t event, int priority,
115 event_callback_args_t * args);
117 static dev_link_t *orinoco_cs_attach(void);
118 static void orinoco_cs_detach(dev_link_t *);
120 /********************************************************************/
122 /********************************************************************/
125 orinoco_cs_hard_reset(struct orinoco_private *priv)
127 struct orinoco_pccard *card = priv->card;
128 dev_link_t *link = &card->link;
131 /* We need atomic ops here, because we're not holding the lock */
132 set_bit(0, &card->hard_reset_in_progress);
134 err = CardServices(ResetCard, link->handle, NULL);
138 clear_bit(0, &card->hard_reset_in_progress);
143 /********************************************************************/
145 /********************************************************************/
147 /* In 2.5 (as of 2.5.69 at least) there is a cs_error exported which
148 * does this, but it's not in 2.4 so we do our own for now. */
150 orinoco_cs_error(client_handle_t handle, int func, int ret)
152 error_info_t err = { func, ret };
153 CardServices(ReportError, handle, &err);
157 /* Remove zombie instances (card removed, detach pending) */
159 flush_stale_links(void)
161 dev_link_t *link, *next;
165 for (link = dev_list; link; link = next) {
167 if (link->state & DEV_STALE_LINK) {
168 orinoco_cs_detach(link);
175 * This creates an "instance" of the driver, allocating local data
176 * structures for one device. The device is registered with Card
179 * The dev_link structure is initialized, but we don't actually
180 * configure the card at this point -- we wait until we receive a card
181 * insertion event. */
183 orinoco_cs_attach(void)
185 struct net_device *dev;
186 struct orinoco_private *priv;
187 struct orinoco_pccard *card;
189 client_reg_t client_reg;
192 /* A bit of cleanup */
195 dev = alloc_orinocodev(sizeof(*card), orinoco_cs_hard_reset);
201 /* Link both structures together */
205 /* Initialize the dev_link_t structure */
206 init_timer(&link->release);
207 link->release.function = &orinoco_cs_release;
208 link->release.data = (u_long) link;
210 /* Interrupt setup */
211 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
212 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
213 if (irq_list[0] == -1)
214 link->irq.IRQInfo2 = irq_mask;
216 for (i = 0; i < 4; i++)
217 link->irq.IRQInfo2 |= 1 << irq_list[i];
218 link->irq.Handler = NULL;
220 /* General socket configuration defaults can go here. In this
221 * client, we assume very little, and rely on the CIS for
222 * almost everything. In most clients, many details (i.e.,
223 * number, sizes, and attributes of IO windows) are fixed by
224 * the nature of the device, and can be hard-wired here. */
225 link->conf.Attributes = 0;
226 link->conf.IntType = INT_MEMORY_AND_IO;
228 /* Register with Card Services */
229 /* FIXME: need a lock? */
230 link->next = dev_list;
233 client_reg.dev_info = &dev_info;
234 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
235 client_reg.EventMask =
236 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
237 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
238 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
239 client_reg.event_handler = &orinoco_cs_event;
240 client_reg.Version = 0x0210; /* FIXME: what does this mean? */
241 client_reg.event_callback_args.client_data = link;
243 ret = CardServices(RegisterClient, &link->handle, &client_reg);
244 if (ret != CS_SUCCESS) {
245 orinoco_cs_error(link->handle, RegisterClient, ret);
246 orinoco_cs_detach(link);
251 } /* orinoco_cs_attach */
254 * This deletes a driver "instance". The device is de-registered with
255 * Card Services. If it has been released, all local data structures
256 * are freed. Otherwise, the structures will be freed when the device
260 orinoco_cs_detach(dev_link_t * link)
263 struct net_device *dev = link->priv;
265 /* Locate device structure */
266 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
269 if (*linkp == NULL) {
274 if (link->state & DEV_CONFIG) {
275 orinoco_cs_release((u_long)link);
276 if (link->state & DEV_CONFIG) {
277 link->state |= DEV_STALE_LINK;
282 /* Break the link with Card Services */
284 CardServices(DeregisterClient, link->handle);
286 /* Unlink device structure, and free it */
288 DEBUG(0, "orinoco_cs: detach: link=%p link->dev=%p\n", link, link->dev);
290 DEBUG(0, "orinoco_cs: About to unregister net device %p\n",
292 unregister_netdev(dev);
295 } /* orinoco_cs_detach */
298 * orinoco_cs_config() is scheduled to run after a CARD_INSERTION
299 * event is received, to configure the PCMCIA socket, and to make the
300 * device available to the system.
303 #define CS_CHECK(fn, args...) \
304 while ((last_ret=CardServices(last_fn=(fn),args))!=0) goto cs_failed
306 #define CFG_CHECK(fn, args...) \
307 if (CardServices(fn, args) != 0) goto next_entry
310 orinoco_cs_config(dev_link_t *link)
312 struct net_device *dev = link->priv;
313 client_handle_t handle = link->handle;
314 struct orinoco_private *priv = dev->priv;
315 struct orinoco_pccard *card = priv->card;
316 hermes_t *hw = &priv->hw;
317 int last_fn, last_ret;
324 CS_CHECK(ValidateCIS, handle, &info);
327 * This reads the card's CONFIG tuple to find its
328 * configuration registers.
330 tuple.DesiredTuple = CISTPL_CONFIG;
331 tuple.Attributes = 0;
332 tuple.TupleData = buf;
333 tuple.TupleDataMax = sizeof(buf);
334 tuple.TupleOffset = 0;
335 CS_CHECK(GetFirstTuple, handle, &tuple);
336 CS_CHECK(GetTupleData, handle, &tuple);
337 CS_CHECK(ParseTuple, handle, &tuple, &parse);
338 link->conf.ConfigBase = parse.config.base;
339 link->conf.Present = parse.config.rmask[0];
342 link->state |= DEV_CONFIG;
344 /* Look up the current Vcc */
345 CS_CHECK(GetConfigurationInfo, handle, &conf);
346 link->conf.Vcc = conf.Vcc;
349 * In this loop, we scan the CIS for configuration table
350 * entries, each of which describes a valid card
351 * configuration, including voltage, IO window, memory window,
352 * and interrupt settings.
354 * We make no assumptions about the card to be configured: we
355 * use just the information available in the CIS. In an ideal
356 * world, this would work for any PCMCIA card, but it requires
357 * a complete and accurate CIS. In practice, a driver usually
358 * "knows" most of these things without consulting the CIS,
359 * and most client drivers will only use the CIS to fill in
360 * implementation-defined details.
362 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
363 CS_CHECK(GetFirstTuple, handle, &tuple);
365 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
366 cistpl_cftable_entry_t dflt = { .index = 0 };
368 CFG_CHECK(GetTupleData, handle, &tuple);
369 CFG_CHECK(ParseTuple, handle, &tuple, &parse);
371 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
375 link->conf.ConfigIndex = cfg->index;
377 /* Does this card need audio output? */
378 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
379 link->conf.Attributes |= CONF_ENABLE_SPKR;
380 link->conf.Status = CCSR_AUDIO_ENA;
383 /* Use power settings for Vcc and Vpp if present */
384 /* Note that the CIS values need to be rescaled */
385 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
386 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
387 DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
391 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
392 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
393 DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
399 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
400 link->conf.Vpp1 = link->conf.Vpp2 =
401 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
402 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
403 link->conf.Vpp1 = link->conf.Vpp2 =
404 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
406 /* Do we need to allocate an interrupt? */
407 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
408 link->conf.Attributes |= CONF_ENABLE_IRQ;
410 /* IO window settings */
411 link->io.NumPorts1 = link->io.NumPorts2 = 0;
412 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
414 (cfg->io.nwin) ? &cfg->io : &dflt.io;
415 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
416 if (!(io->flags & CISTPL_IO_8BIT))
417 link->io.Attributes1 =
418 IO_DATA_PATH_WIDTH_16;
419 if (!(io->flags & CISTPL_IO_16BIT))
420 link->io.Attributes1 =
421 IO_DATA_PATH_WIDTH_8;
422 link->io.IOAddrLines =
423 io->flags & CISTPL_IO_LINES_MASK;
424 link->io.BasePort1 = io->win[0].base;
425 link->io.NumPorts1 = io->win[0].len;
427 link->io.Attributes2 =
428 link->io.Attributes1;
429 link->io.BasePort2 = io->win[1].base;
430 link->io.NumPorts2 = io->win[1].len;
433 /* This reserves IO space but doesn't actually enable it */
434 CFG_CHECK(RequestIO, link->handle, &link->io);
438 /* If we got this far, we're cool! */
443 if (link->io.NumPorts1)
444 CardServices(ReleaseIO, link->handle, &link->io);
445 last_ret = CardServices(GetNextTuple, handle, &tuple);
446 if (last_ret == CS_NO_MORE_ITEMS) {
447 printk(KERN_ERR "GetNextTuple(). No matching CIS configuration, "
448 "maybe you need the ignore_cis_vcc=1 parameter.\n");
454 * Allocate an interrupt line. Note that this does not assign
455 * a handler to the interrupt, unless the 'Handler' member of
456 * the irq structure is initialized.
458 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
461 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
462 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
463 if (irq_list[0] == -1)
464 link->irq.IRQInfo2 = irq_mask;
467 link->irq.IRQInfo2 |= 1 << irq_list[i];
469 link->irq.Handler = orinoco_interrupt;
470 link->irq.Instance = dev;
472 CS_CHECK(RequestIRQ, link->handle, &link->irq);
475 /* We initialize the hermes structure before completing PCMCIA
476 * configuration just in case the interrupt handler gets
478 hermes_struct_init(hw, link->io.BasePort1,
479 HERMES_IO, HERMES_16BIT_REGSPACING);
482 * This actually configures the PCMCIA socket -- setting up
483 * the I/O windows and the interrupt mapping, and putting the
484 * card and host interface into "Memory and IO" mode.
486 CS_CHECK(RequestConfiguration, link->handle, &link->conf);
488 /* Ok, we have the configuration, prepare to register the netdev */
489 dev->base_addr = link->io.BasePort1;
490 dev->irq = link->irq.AssignedIRQ;
491 SET_MODULE_OWNER(dev);
492 card->node.major = card->node.minor = 0;
494 /* register_netdev will give us an ethX name */
496 /* Tell the stack we exist */
497 if (register_netdev(dev) != 0) {
498 printk(KERN_ERR "orinoco_cs: register_netdev() failed\n");
502 /* At this point, the dev_node_t structure(s) needs to be
503 * initialized and arranged in a linked list at link->dev. */
504 strcpy(card->node.dev_name, dev->name);
505 link->dev = &card->node; /* link->dev being non-NULL is also
506 used to indicate that the
507 net_device has been registered */
508 link->state &= ~DEV_CONFIG_PENDING;
510 /* Finally, report what we've done */
511 printk(KERN_DEBUG "%s: index 0x%02x: Vcc %d.%d",
512 dev->name, link->conf.ConfigIndex,
513 link->conf.Vcc / 10, link->conf.Vcc % 10);
515 printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
516 link->conf.Vpp1 % 10);
517 if (link->conf.Attributes & CONF_ENABLE_IRQ)
518 printk(", irq %d", link->irq.AssignedIRQ);
519 if (link->io.NumPorts1)
520 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
521 link->io.BasePort1 + link->io.NumPorts1 - 1);
522 if (link->io.NumPorts2)
523 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
524 link->io.BasePort2 + link->io.NumPorts2 - 1);
530 orinoco_cs_error(link->handle, last_fn, last_ret);
533 orinoco_cs_release((u_long) link);
534 } /* orinoco_cs_config */
537 * After a card is removed, orinoco_cs_release() will unregister the
538 * device, and release the PCMCIA configuration. If the device is
539 * still open, this will be postponed until it is closed.
542 orinoco_cs_release(u_long arg)
544 dev_link_t *link = (dev_link_t *) arg;
545 struct net_device *dev = link->priv;
546 struct orinoco_private *priv = dev->priv;
549 /* We're committed to taking the device away now, so mark the
550 * hardware as unavailable */
551 spin_lock_irqsave(&priv->lock, flags);
552 priv->hw_unavailable++;
553 spin_unlock_irqrestore(&priv->lock, flags);
555 /* Don't bother checking to see if these succeed or not */
556 CardServices(ReleaseConfiguration, link->handle);
557 if (link->io.NumPorts1)
558 CardServices(ReleaseIO, link->handle, &link->io);
559 if (link->irq.AssignedIRQ)
560 CardServices(ReleaseIRQ, link->handle, &link->irq);
561 link->state &= ~DEV_CONFIG;
562 } /* orinoco_cs_release */
565 * The card status event handler. Mostly, this schedules other stuff
566 * to run after an event is received.
569 orinoco_cs_event(event_t event, int priority,
570 event_callback_args_t * args)
572 dev_link_t *link = args->client_data;
573 struct net_device *dev = link->priv;
574 struct orinoco_private *priv = dev->priv;
575 struct orinoco_pccard *card = priv->card;
580 case CS_EVENT_CARD_REMOVAL:
581 link->state &= ~DEV_PRESENT;
582 if (link->state & DEV_CONFIG) {
583 orinoco_lock(priv, &flags);
585 netif_device_detach(dev);
586 priv->hw_unavailable++;
588 orinoco_unlock(priv, &flags);
592 case CS_EVENT_CARD_INSERTION:
593 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
594 orinoco_cs_config(link);
597 case CS_EVENT_PM_SUSPEND:
598 link->state |= DEV_SUSPEND;
599 /* Fall through... */
600 case CS_EVENT_RESET_PHYSICAL:
601 /* Mark the device as stopped, to block IO until later */
602 if (link->state & DEV_CONFIG) {
603 /* This is probably racy, but I can't think of
604 a better way, short of rewriting the PCMCIA
605 layer to not suck :-( */
606 if (! test_bit(0, &card->hard_reset_in_progress)) {
607 spin_lock_irqsave(&priv->lock, flags);
609 err = __orinoco_down(dev);
611 printk(KERN_WARNING "%s: %s: Error %d downing interface\n",
613 event == CS_EVENT_PM_SUSPEND ? "SUSPEND" : "RESET_PHYSICAL",
616 netif_device_detach(dev);
617 priv->hw_unavailable++;
619 spin_unlock_irqrestore(&priv->lock, flags);
622 CardServices(ReleaseConfiguration, link->handle);
626 case CS_EVENT_PM_RESUME:
627 link->state &= ~DEV_SUSPEND;
628 /* Fall through... */
629 case CS_EVENT_CARD_RESET:
630 if (link->state & DEV_CONFIG) {
631 /* FIXME: should we double check that this is
632 * the same card as we had before */
633 CardServices(RequestConfiguration, link->handle,
636 if (! test_bit(0, &card->hard_reset_in_progress)) {
637 err = orinoco_reinit_firmware(dev);
639 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
644 spin_lock_irqsave(&priv->lock, flags);
646 netif_device_attach(dev);
647 priv->hw_unavailable--;
649 if (priv->open && ! priv->hw_unavailable) {
650 err = __orinoco_up(dev);
652 printk(KERN_ERR "%s: Error %d restarting card\n",
657 spin_unlock_irqrestore(&priv->lock, flags);
664 } /* orinoco_cs_event */
666 /********************************************************************/
667 /* Module initialization */
668 /********************************************************************/
670 /* Can't be declared "const" or the whole __initdata section will
672 static char version[] __initdata = "orinoco_cs.c 0.13e (David Gibson <hermes@gibson.dropbear.id.au> and others)";
674 static struct pcmcia_driver orinoco_driver = {
675 .owner = THIS_MODULE,
677 .name = "orinoco_cs",
679 .attach = orinoco_cs_attach,
680 .detach = orinoco_cs_detach,
684 init_orinoco_cs(void)
686 printk(KERN_DEBUG "%s\n", version);
688 return pcmcia_register_driver(&orinoco_driver);
692 exit_orinoco_cs(void)
694 pcmcia_unregister_driver(&orinoco_driver);
697 DEBUG(0, "orinoco_cs: Removing leftover devices.\n");
698 while (dev_list != NULL) {
699 if (dev_list->state & DEV_CONFIG)
700 orinoco_cs_release((u_long) dev_list);
701 orinoco_cs_detach(dev_list);
705 module_init(init_orinoco_cs);
706 module_exit(exit_orinoco_cs);