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(dev_link_t * link);
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 = pcmcia_reset_card(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 pcmcia_report_error(handle, &err);
157 * This creates an "instance" of the driver, allocating local data
158 * structures for one device. The device is registered with Card
161 * The dev_link structure is initialized, but we don't actually
162 * configure the card at this point -- we wait until we receive a card
163 * insertion event. */
165 orinoco_cs_attach(void)
167 struct net_device *dev;
168 struct orinoco_private *priv;
169 struct orinoco_pccard *card;
171 client_reg_t client_reg;
174 dev = alloc_orinocodev(sizeof(*card), orinoco_cs_hard_reset);
180 /* Link both structures together */
184 /* Interrupt setup */
185 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
186 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
187 if (irq_list[0] == -1)
188 link->irq.IRQInfo2 = irq_mask;
190 for (i = 0; i < 4; i++)
191 link->irq.IRQInfo2 |= 1 << irq_list[i];
192 link->irq.Handler = NULL;
194 /* General socket configuration defaults can go here. In this
195 * client, we assume very little, and rely on the CIS for
196 * almost everything. In most clients, many details (i.e.,
197 * number, sizes, and attributes of IO windows) are fixed by
198 * the nature of the device, and can be hard-wired here. */
199 link->conf.Attributes = 0;
200 link->conf.IntType = INT_MEMORY_AND_IO;
202 /* Register with Card Services */
203 /* FIXME: need a lock? */
204 link->next = dev_list;
207 client_reg.dev_info = &dev_info;
208 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
209 client_reg.EventMask =
210 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
211 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
212 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
213 client_reg.event_handler = &orinoco_cs_event;
214 client_reg.Version = 0x0210; /* FIXME: what does this mean? */
215 client_reg.event_callback_args.client_data = link;
217 ret = pcmcia_register_client(&link->handle, &client_reg);
218 if (ret != CS_SUCCESS) {
219 orinoco_cs_error(link->handle, RegisterClient, ret);
220 orinoco_cs_detach(link);
225 } /* orinoco_cs_attach */
228 * This deletes a driver "instance". The device is de-registered with
229 * Card Services. If it has been released, all local data structures
230 * are freed. Otherwise, the structures will be freed when the device
234 orinoco_cs_detach(dev_link_t * link)
237 struct net_device *dev = link->priv;
239 /* Locate device structure */
240 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
243 if (*linkp == NULL) {
248 if (link->state & DEV_CONFIG)
249 orinoco_cs_release(link);
251 /* Break the link with Card Services */
253 pcmcia_deregister_client(link->handle);
255 /* Unlink device structure, and free it */
257 DEBUG(0, "orinoco_cs: detach: link=%p link->dev=%p\n", link, link->dev);
259 DEBUG(0, "orinoco_cs: About to unregister net device %p\n",
261 unregister_netdev(dev);
264 } /* orinoco_cs_detach */
267 * orinoco_cs_config() is scheduled to run after a CARD_INSERTION
268 * event is received, to configure the PCMCIA socket, and to make the
269 * device available to the system.
272 #define CS_CHECK(fn, ret) \
273 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
276 orinoco_cs_config(dev_link_t *link)
278 struct net_device *dev = link->priv;
279 client_handle_t handle = link->handle;
280 struct orinoco_private *priv = dev->priv;
281 struct orinoco_pccard *card = priv->card;
282 hermes_t *hw = &priv->hw;
283 int last_fn, last_ret;
290 CS_CHECK(ValidateCIS, pcmcia_validate_cis(handle, &info));
293 * This reads the card's CONFIG tuple to find its
294 * configuration registers.
296 tuple.DesiredTuple = CISTPL_CONFIG;
297 tuple.Attributes = 0;
298 tuple.TupleData = buf;
299 tuple.TupleDataMax = sizeof(buf);
300 tuple.TupleOffset = 0;
301 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
302 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
303 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
304 link->conf.ConfigBase = parse.config.base;
305 link->conf.Present = parse.config.rmask[0];
308 link->state |= DEV_CONFIG;
310 /* Look up the current Vcc */
311 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
312 link->conf.Vcc = conf.Vcc;
315 * In this loop, we scan the CIS for configuration table
316 * entries, each of which describes a valid card
317 * configuration, including voltage, IO window, memory window,
318 * and interrupt settings.
320 * We make no assumptions about the card to be configured: we
321 * use just the information available in the CIS. In an ideal
322 * world, this would work for any PCMCIA card, but it requires
323 * a complete and accurate CIS. In practice, a driver usually
324 * "knows" most of these things without consulting the CIS,
325 * and most client drivers will only use the CIS to fill in
326 * implementation-defined details.
328 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
329 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
331 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
332 cistpl_cftable_entry_t dflt = { .index = 0 };
334 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
335 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
338 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
342 link->conf.ConfigIndex = cfg->index;
344 /* Does this card need audio output? */
345 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
346 link->conf.Attributes |= CONF_ENABLE_SPKR;
347 link->conf.Status = CCSR_AUDIO_ENA;
350 /* Use power settings for Vcc and Vpp if present */
351 /* Note that the CIS values need to be rescaled */
352 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
353 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
354 DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
358 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
359 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
360 DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
366 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
367 link->conf.Vpp1 = link->conf.Vpp2 =
368 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
369 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
370 link->conf.Vpp1 = link->conf.Vpp2 =
371 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
373 /* Do we need to allocate an interrupt? */
374 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
375 link->conf.Attributes |= CONF_ENABLE_IRQ;
377 /* IO window settings */
378 link->io.NumPorts1 = link->io.NumPorts2 = 0;
379 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
381 (cfg->io.nwin) ? &cfg->io : &dflt.io;
382 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
383 if (!(io->flags & CISTPL_IO_8BIT))
384 link->io.Attributes1 =
385 IO_DATA_PATH_WIDTH_16;
386 if (!(io->flags & CISTPL_IO_16BIT))
387 link->io.Attributes1 =
388 IO_DATA_PATH_WIDTH_8;
389 link->io.IOAddrLines =
390 io->flags & CISTPL_IO_LINES_MASK;
391 link->io.BasePort1 = io->win[0].base;
392 link->io.NumPorts1 = io->win[0].len;
394 link->io.Attributes2 =
395 link->io.Attributes1;
396 link->io.BasePort2 = io->win[1].base;
397 link->io.NumPorts2 = io->win[1].len;
400 /* This reserves IO space but doesn't actually enable it */
401 if (pcmcia_request_io(link->handle, &link->io) != 0)
406 /* If we got this far, we're cool! */
411 if (link->io.NumPorts1)
412 pcmcia_release_io(link->handle, &link->io);
413 last_ret = pcmcia_get_next_tuple(handle, &tuple);
414 if (last_ret == CS_NO_MORE_ITEMS) {
415 printk(KERN_ERR "GetNextTuple(). No matching CIS configuration, "
416 "maybe you need the ignore_cis_vcc=1 parameter.\n");
422 * Allocate an interrupt line. Note that this does not assign
423 * a handler to the interrupt, unless the 'Handler' member of
424 * the irq structure is initialized.
426 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
429 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
430 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
431 if (irq_list[0] == -1)
432 link->irq.IRQInfo2 = irq_mask;
435 link->irq.IRQInfo2 |= 1 << irq_list[i];
437 link->irq.Handler = orinoco_interrupt;
438 link->irq.Instance = dev;
440 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
443 /* We initialize the hermes structure before completing PCMCIA
444 * configuration just in case the interrupt handler gets
446 hermes_struct_init(hw, link->io.BasePort1,
447 HERMES_IO, HERMES_16BIT_REGSPACING);
450 * This actually configures the PCMCIA socket -- setting up
451 * the I/O windows and the interrupt mapping, and putting the
452 * card and host interface into "Memory and IO" mode.
454 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
456 /* Ok, we have the configuration, prepare to register the netdev */
457 dev->base_addr = link->io.BasePort1;
458 dev->irq = link->irq.AssignedIRQ;
459 SET_MODULE_OWNER(dev);
460 card->node.major = card->node.minor = 0;
462 /* register_netdev will give us an ethX name */
464 /* Tell the stack we exist */
465 if (register_netdev(dev) != 0) {
466 printk(KERN_ERR "orinoco_cs: register_netdev() failed\n");
470 /* At this point, the dev_node_t structure(s) needs to be
471 * initialized and arranged in a linked list at link->dev. */
472 strcpy(card->node.dev_name, dev->name);
473 link->dev = &card->node; /* link->dev being non-NULL is also
474 used to indicate that the
475 net_device has been registered */
476 link->state &= ~DEV_CONFIG_PENDING;
478 /* Finally, report what we've done */
479 printk(KERN_DEBUG "%s: index 0x%02x: Vcc %d.%d",
480 dev->name, link->conf.ConfigIndex,
481 link->conf.Vcc / 10, link->conf.Vcc % 10);
483 printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
484 link->conf.Vpp1 % 10);
485 if (link->conf.Attributes & CONF_ENABLE_IRQ)
486 printk(", irq %d", link->irq.AssignedIRQ);
487 if (link->io.NumPorts1)
488 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
489 link->io.BasePort1 + link->io.NumPorts1 - 1);
490 if (link->io.NumPorts2)
491 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
492 link->io.BasePort2 + link->io.NumPorts2 - 1);
498 orinoco_cs_error(link->handle, last_fn, last_ret);
501 orinoco_cs_release(link);
502 } /* orinoco_cs_config */
505 * After a card is removed, orinoco_cs_release() will unregister the
506 * device, and release the PCMCIA configuration. If the device is
507 * still open, this will be postponed until it is closed.
510 orinoco_cs_release(dev_link_t *link)
512 struct net_device *dev = link->priv;
513 struct orinoco_private *priv = dev->priv;
516 /* We're committed to taking the device away now, so mark the
517 * hardware as unavailable */
518 spin_lock_irqsave(&priv->lock, flags);
519 priv->hw_unavailable++;
520 spin_unlock_irqrestore(&priv->lock, flags);
522 /* Don't bother checking to see if these succeed or not */
523 pcmcia_release_configuration(link->handle);
524 if (link->io.NumPorts1)
525 pcmcia_release_io(link->handle, &link->io);
526 if (link->irq.AssignedIRQ)
527 pcmcia_release_irq(link->handle, &link->irq);
528 link->state &= ~DEV_CONFIG;
529 } /* orinoco_cs_release */
532 * The card status event handler. Mostly, this schedules other stuff
533 * to run after an event is received.
536 orinoco_cs_event(event_t event, int priority,
537 event_callback_args_t * args)
539 dev_link_t *link = args->client_data;
540 struct net_device *dev = link->priv;
541 struct orinoco_private *priv = dev->priv;
542 struct orinoco_pccard *card = priv->card;
547 case CS_EVENT_CARD_REMOVAL:
548 link->state &= ~DEV_PRESENT;
549 if (link->state & DEV_CONFIG) {
550 orinoco_lock(priv, &flags);
552 netif_device_detach(dev);
553 priv->hw_unavailable++;
555 orinoco_unlock(priv, &flags);
559 case CS_EVENT_CARD_INSERTION:
560 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
561 orinoco_cs_config(link);
564 case CS_EVENT_PM_SUSPEND:
565 link->state |= DEV_SUSPEND;
566 /* Fall through... */
567 case CS_EVENT_RESET_PHYSICAL:
568 /* Mark the device as stopped, to block IO until later */
569 if (link->state & DEV_CONFIG) {
570 /* This is probably racy, but I can't think of
571 a better way, short of rewriting the PCMCIA
572 layer to not suck :-( */
573 if (! test_bit(0, &card->hard_reset_in_progress)) {
574 spin_lock_irqsave(&priv->lock, flags);
576 err = __orinoco_down(dev);
578 printk(KERN_WARNING "%s: %s: Error %d downing interface\n",
580 event == CS_EVENT_PM_SUSPEND ? "SUSPEND" : "RESET_PHYSICAL",
583 netif_device_detach(dev);
584 priv->hw_unavailable++;
586 spin_unlock_irqrestore(&priv->lock, flags);
589 pcmcia_release_configuration(link->handle);
593 case CS_EVENT_PM_RESUME:
594 link->state &= ~DEV_SUSPEND;
595 /* Fall through... */
596 case CS_EVENT_CARD_RESET:
597 if (link->state & DEV_CONFIG) {
598 /* FIXME: should we double check that this is
599 * the same card as we had before */
600 pcmcia_request_configuration(link->handle, &link->conf);
602 if (! test_bit(0, &card->hard_reset_in_progress)) {
603 err = orinoco_reinit_firmware(dev);
605 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
610 spin_lock_irqsave(&priv->lock, flags);
612 netif_device_attach(dev);
613 priv->hw_unavailable--;
615 if (priv->open && ! priv->hw_unavailable) {
616 err = __orinoco_up(dev);
618 printk(KERN_ERR "%s: Error %d restarting card\n",
623 spin_unlock_irqrestore(&priv->lock, flags);
630 } /* orinoco_cs_event */
632 /********************************************************************/
633 /* Module initialization */
634 /********************************************************************/
636 /* Can't be declared "const" or the whole __initdata section will
638 static char version[] __initdata = "orinoco_cs.c 0.13e (David Gibson <hermes@gibson.dropbear.id.au> and others)";
640 static struct pcmcia_driver orinoco_driver = {
641 .owner = THIS_MODULE,
643 .name = "orinoco_cs",
645 .attach = orinoco_cs_attach,
646 .detach = orinoco_cs_detach,
650 init_orinoco_cs(void)
652 printk(KERN_DEBUG "%s\n", version);
654 return pcmcia_register_driver(&orinoco_driver);
658 exit_orinoco_cs(void)
660 pcmcia_unregister_driver(&orinoco_driver);
663 DEBUG(0, "orinoco_cs: Removing leftover devices.\n");
664 while (dev_list != NULL) {
665 if (dev_list->state & DEV_CONFIG)
666 orinoco_cs_release(dev_list);
667 orinoco_cs_detach(dev_list);
671 module_init(init_orinoco_cs);
672 module_exit(exit_orinoco_cs);