initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / net / wireless / orinoco_cs.c
blob4eb92ac6a5c6b74cbe8d4d423c8e0092a4f1e4ac
1 /* orinoco_cs.c (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.
9 *
10 * Copyright notice & release notes in file orinoco.c
13 #define DRIVER_NAME "orinoco_cs"
14 #define PFX DRIVER_NAME ": "
16 #include <linux/config.h>
17 #ifdef __IN_PCMCIA_PACKAGE__
18 #include <pcmcia/k_compat.h>
19 #endif /* __IN_PCMCIA_PACKAGE__ */
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/ptrace.h>
26 #include <linux/slab.h>
27 #include <linux/string.h>
28 #include <linux/ioport.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/etherdevice.h>
32 #include <linux/wireless.h>
34 #include <pcmcia/version.h>
35 #include <pcmcia/cs_types.h>
36 #include <pcmcia/cs.h>
37 #include <pcmcia/cistpl.h>
38 #include <pcmcia/cisreg.h>
39 #include <pcmcia/ds.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
43 #include <asm/system.h>
45 #include "orinoco.h"
47 /********************************************************************/
48 /* Module stuff */
49 /********************************************************************/
51 MODULE_AUTHOR("David Gibson <hermes@gibson.dropbear.id.au>");
52 MODULE_DESCRIPTION("Driver for PCMCIA Lucent Orinoco, Prism II based and similar wireless cards");
53 MODULE_LICENSE("Dual MPL/GPL");
55 /* Module parameters */
57 /* The old way: bit map of interrupts to choose from */
58 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
59 static uint irq_mask = 0xdeb8;
60 /* Newer, simpler way of listing specific interrupts */
61 static int irq_list[4] = { -1 };
63 /* Some D-Link cards have buggy CIS. They do work at 5v properly, but
64 * don't have any CIS entry for it. This workaround it... */
65 static int ignore_cis_vcc; /* = 0 */
67 MODULE_PARM(irq_mask, "i");
68 MODULE_PARM(irq_list, "1-4i");
69 MODULE_PARM(ignore_cis_vcc, "i");
71 /********************************************************************/
72 /* Magic constants */
73 /********************************************************************/
76 * The dev_info variable is the "key" that is used to match up this
77 * device driver with appropriate cards, through the card
78 * configuration database.
80 static dev_info_t dev_info = DRIVER_NAME;
82 /********************************************************************/
83 /* Data structures */
84 /********************************************************************/
86 /* PCMCIA specific device information (goes in the card field of
87 * struct orinoco_private */
88 struct orinoco_pccard {
89 dev_link_t link;
90 dev_node_t node;
92 /* Used to handle hard reset */
93 /* yuck, we need this hack to work around the insanity of the
94 * PCMCIA layer */
95 unsigned long hard_reset_in_progress;
99 * A linked list of "instances" of the device. Each actual PCMCIA
100 * card corresponds to one device instance, and is described by one
101 * dev_link_t structure (defined in ds.h).
103 static dev_link_t *dev_list; /* = NULL */
105 /********************************************************************/
106 /* Function prototypes */
107 /********************************************************************/
109 /* device methods */
110 static int orinoco_cs_hard_reset(struct orinoco_private *priv);
112 /* PCMCIA gumpf */
113 static void orinoco_cs_config(dev_link_t * link);
114 static void orinoco_cs_release(dev_link_t * link);
115 static int orinoco_cs_event(event_t event, int priority,
116 event_callback_args_t * args);
118 static dev_link_t *orinoco_cs_attach(void);
119 static void orinoco_cs_detach(dev_link_t *);
121 /********************************************************************/
122 /* Device methods */
123 /********************************************************************/
125 static int
126 orinoco_cs_hard_reset(struct orinoco_private *priv)
128 struct orinoco_pccard *card = priv->card;
129 dev_link_t *link = &card->link;
130 int err;
132 /* We need atomic ops here, because we're not holding the lock */
133 set_bit(0, &card->hard_reset_in_progress);
135 err = pcmcia_reset_card(link->handle, NULL);
136 if (err)
137 return err;
139 clear_bit(0, &card->hard_reset_in_progress);
141 return 0;
144 /********************************************************************/
145 /* PCMCIA stuff */
146 /********************************************************************/
149 * This creates an "instance" of the driver, allocating local data
150 * structures for one device. The device is registered with Card
151 * Services.
153 * The dev_link structure is initialized, but we don't actually
154 * configure the card at this point -- we wait until we receive a card
155 * insertion event. */
156 static dev_link_t *
157 orinoco_cs_attach(void)
159 struct net_device *dev;
160 struct orinoco_private *priv;
161 struct orinoco_pccard *card;
162 dev_link_t *link;
163 client_reg_t client_reg;
164 int ret, i;
166 dev = alloc_orinocodev(sizeof(*card), orinoco_cs_hard_reset);
167 if (! dev)
168 return NULL;
169 priv = netdev_priv(dev);
170 card = priv->card;
172 /* Link both structures together */
173 link = &card->link;
174 link->priv = dev;
176 /* Interrupt setup */
177 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
178 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
179 if (irq_list[0] == -1)
180 link->irq.IRQInfo2 = irq_mask;
181 else
182 for (i = 0; i < 4; i++)
183 link->irq.IRQInfo2 |= 1 << irq_list[i];
184 link->irq.Handler = NULL;
186 /* General socket configuration defaults can go here. In this
187 * client, we assume very little, and rely on the CIS for
188 * almost everything. In most clients, many details (i.e.,
189 * number, sizes, and attributes of IO windows) are fixed by
190 * the nature of the device, and can be hard-wired here. */
191 link->conf.Attributes = 0;
192 link->conf.IntType = INT_MEMORY_AND_IO;
194 /* Register with Card Services */
195 /* FIXME: need a lock? */
196 link->next = dev_list;
197 dev_list = link;
199 client_reg.dev_info = &dev_info;
200 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
201 client_reg.EventMask =
202 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
203 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
204 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
205 client_reg.event_handler = &orinoco_cs_event;
206 client_reg.Version = 0x0210; /* FIXME: what does this mean? */
207 client_reg.event_callback_args.client_data = link;
209 ret = pcmcia_register_client(&link->handle, &client_reg);
210 if (ret != CS_SUCCESS) {
211 cs_error(link->handle, RegisterClient, ret);
212 orinoco_cs_detach(link);
213 return NULL;
216 return link;
217 } /* orinoco_cs_attach */
220 * This deletes a driver "instance". The device is de-registered with
221 * Card Services. If it has been released, all local data structures
222 * are freed. Otherwise, the structures will be freed when the device
223 * is released.
225 static void orinoco_cs_detach(dev_link_t *link)
227 dev_link_t **linkp;
228 struct net_device *dev = link->priv;
230 /* Locate device structure */
231 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
232 if (*linkp == link)
233 break;
235 BUG_ON(*linkp == NULL);
237 if (link->state & DEV_CONFIG)
238 orinoco_cs_release(link);
240 /* Break the link with Card Services */
241 if (link->handle)
242 pcmcia_deregister_client(link->handle);
244 /* Unlink device structure, and free it */
245 *linkp = link->next;
246 DEBUG(0, PFX "detach: link=%p link->dev=%p\n", link, link->dev);
247 if (link->dev) {
248 DEBUG(0, PFX "About to unregister net device %p\n",
249 dev);
250 unregister_netdev(dev);
252 free_netdev(dev);
253 } /* orinoco_cs_detach */
256 * orinoco_cs_config() is scheduled to run after a CARD_INSERTION
257 * event is received, to configure the PCMCIA socket, and to make the
258 * device available to the system.
261 #define CS_CHECK(fn, ret) do { \
262 last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; \
263 } while (0)
265 static void
266 orinoco_cs_config(dev_link_t *link)
268 struct net_device *dev = link->priv;
269 client_handle_t handle = link->handle;
270 struct orinoco_private *priv = netdev_priv(dev);
271 struct orinoco_pccard *card = priv->card;
272 hermes_t *hw = &priv->hw;
273 int last_fn, last_ret;
274 u_char buf[64];
275 config_info_t conf;
276 cisinfo_t info;
277 tuple_t tuple;
278 cisparse_t parse;
280 CS_CHECK(ValidateCIS, pcmcia_validate_cis(handle, &info));
283 * This reads the card's CONFIG tuple to find its
284 * configuration registers.
286 tuple.DesiredTuple = CISTPL_CONFIG;
287 tuple.Attributes = 0;
288 tuple.TupleData = buf;
289 tuple.TupleDataMax = sizeof(buf);
290 tuple.TupleOffset = 0;
291 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
292 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
293 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
294 link->conf.ConfigBase = parse.config.base;
295 link->conf.Present = parse.config.rmask[0];
297 /* Configure card */
298 link->state |= DEV_CONFIG;
300 /* Look up the current Vcc */
301 CS_CHECK(GetConfigurationInfo,
302 pcmcia_get_configuration_info(handle, &conf));
303 link->conf.Vcc = conf.Vcc;
306 * In this loop, we scan the CIS for configuration table
307 * entries, each of which describes a valid card
308 * configuration, including voltage, IO window, memory window,
309 * and interrupt settings.
311 * We make no assumptions about the card to be configured: we
312 * use just the information available in the CIS. In an ideal
313 * world, this would work for any PCMCIA card, but it requires
314 * a complete and accurate CIS. In practice, a driver usually
315 * "knows" most of these things without consulting the CIS,
316 * and most client drivers will only use the CIS to fill in
317 * implementation-defined details.
319 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
320 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
321 while (1) {
322 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
323 cistpl_cftable_entry_t dflt = { .index = 0 };
325 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
326 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
327 goto next_entry;
329 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
330 dflt = *cfg;
331 if (cfg->index == 0)
332 goto next_entry;
333 link->conf.ConfigIndex = cfg->index;
335 /* Does this card need audio output? */
336 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
337 link->conf.Attributes |= CONF_ENABLE_SPKR;
338 link->conf.Status = CCSR_AUDIO_ENA;
341 /* Use power settings for Vcc and Vpp if present */
342 /* Note that the CIS values need to be rescaled */
343 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
344 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
345 DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
346 if (!ignore_cis_vcc)
347 goto next_entry;
349 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
350 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
351 DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
352 if(!ignore_cis_vcc)
353 goto next_entry;
357 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
358 link->conf.Vpp1 = link->conf.Vpp2 =
359 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
360 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
361 link->conf.Vpp1 = link->conf.Vpp2 =
362 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
364 /* Do we need to allocate an interrupt? */
365 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
366 link->conf.Attributes |= CONF_ENABLE_IRQ;
368 /* IO window settings */
369 link->io.NumPorts1 = link->io.NumPorts2 = 0;
370 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
371 cistpl_io_t *io =
372 (cfg->io.nwin) ? &cfg->io : &dflt.io;
373 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
374 if (!(io->flags & CISTPL_IO_8BIT))
375 link->io.Attributes1 =
376 IO_DATA_PATH_WIDTH_16;
377 if (!(io->flags & CISTPL_IO_16BIT))
378 link->io.Attributes1 =
379 IO_DATA_PATH_WIDTH_8;
380 link->io.IOAddrLines =
381 io->flags & CISTPL_IO_LINES_MASK;
382 link->io.BasePort1 = io->win[0].base;
383 link->io.NumPorts1 = io->win[0].len;
384 if (io->nwin > 1) {
385 link->io.Attributes2 =
386 link->io.Attributes1;
387 link->io.BasePort2 = io->win[1].base;
388 link->io.NumPorts2 = io->win[1].len;
391 /* This reserves IO space but doesn't actually enable it */
392 if (pcmcia_request_io(link->handle, &link->io) != 0)
393 goto next_entry;
397 /* If we got this far, we're cool! */
399 break;
401 next_entry:
402 if (link->io.NumPorts1)
403 pcmcia_release_io(link->handle, &link->io);
404 last_ret = pcmcia_get_next_tuple(handle, &tuple);
405 if (last_ret == CS_NO_MORE_ITEMS) {
406 printk(KERN_ERR PFX "GetNextTuple(): No matching "
407 "CIS configuration, maybe you need the "
408 "ignore_cis_vcc=1 parameter.\n");
409 goto cs_failed;
414 * Allocate an interrupt line. Note that this does not assign
415 * a handler to the interrupt, unless the 'Handler' member of
416 * the irq structure is initialized.
418 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
419 int i;
421 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
422 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
423 if (irq_list[0] == -1)
424 link->irq.IRQInfo2 = irq_mask;
425 else
426 for (i=0; i<4; i++)
427 link->irq.IRQInfo2 |= 1 << irq_list[i];
429 link->irq.Handler = orinoco_interrupt;
430 link->irq.Instance = dev;
432 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
435 /* We initialize the hermes structure before completing PCMCIA
436 * configuration just in case the interrupt handler gets
437 * called. */
438 hermes_struct_init(hw, link->io.BasePort1,
439 HERMES_IO, HERMES_16BIT_REGSPACING);
442 * This actually configures the PCMCIA socket -- setting up
443 * the I/O windows and the interrupt mapping, and putting the
444 * card and host interface into "Memory and IO" mode.
446 CS_CHECK(RequestConfiguration,
447 pcmcia_request_configuration(link->handle, &link->conf));
449 /* Ok, we have the configuration, prepare to register the netdev */
450 dev->base_addr = link->io.BasePort1;
451 dev->irq = link->irq.AssignedIRQ;
452 SET_MODULE_OWNER(dev);
453 card->node.major = card->node.minor = 0;
455 /* register_netdev will give us an ethX name */
456 dev->name[0] = '\0';
457 /* Tell the stack we exist */
458 if (register_netdev(dev) != 0) {
459 printk(KERN_ERR PFX "register_netdev() failed\n");
460 goto failed;
463 /* At this point, the dev_node_t structure(s) needs to be
464 * initialized and arranged in a linked list at link->dev. */
465 strcpy(card->node.dev_name, dev->name);
466 link->dev = &card->node; /* link->dev being non-NULL is also
467 used to indicate that the
468 net_device has been registered */
469 link->state &= ~DEV_CONFIG_PENDING;
471 /* Finally, report what we've done */
472 printk(KERN_DEBUG "%s: index 0x%02x: Vcc %d.%d",
473 dev->name, link->conf.ConfigIndex,
474 link->conf.Vcc / 10, link->conf.Vcc % 10);
475 if (link->conf.Vpp1)
476 printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
477 link->conf.Vpp1 % 10);
478 if (link->conf.Attributes & CONF_ENABLE_IRQ)
479 printk(", irq %d", link->irq.AssignedIRQ);
480 if (link->io.NumPorts1)
481 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
482 link->io.BasePort1 + link->io.NumPorts1 - 1);
483 if (link->io.NumPorts2)
484 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
485 link->io.BasePort2 + link->io.NumPorts2 - 1);
486 printk("\n");
488 return;
490 cs_failed:
491 cs_error(link->handle, last_fn, last_ret);
493 failed:
494 orinoco_cs_release(link);
495 } /* orinoco_cs_config */
498 * After a card is removed, orinoco_cs_release() will unregister the
499 * device, and release the PCMCIA configuration. If the device is
500 * still open, this will be postponed until it is closed.
502 static void
503 orinoco_cs_release(dev_link_t *link)
505 struct net_device *dev = link->priv;
506 struct orinoco_private *priv = netdev_priv(dev);
507 unsigned long flags;
509 /* We're committed to taking the device away now, so mark the
510 * hardware as unavailable */
511 spin_lock_irqsave(&priv->lock, flags);
512 priv->hw_unavailable++;
513 spin_unlock_irqrestore(&priv->lock, flags);
515 /* Don't bother checking to see if these succeed or not */
516 pcmcia_release_configuration(link->handle);
517 if (link->io.NumPorts1)
518 pcmcia_release_io(link->handle, &link->io);
519 if (link->irq.AssignedIRQ)
520 pcmcia_release_irq(link->handle, &link->irq);
521 link->state &= ~DEV_CONFIG;
522 } /* orinoco_cs_release */
525 * The card status event handler. Mostly, this schedules other stuff
526 * to run after an event is received.
528 static int
529 orinoco_cs_event(event_t event, int priority,
530 event_callback_args_t * args)
532 dev_link_t *link = args->client_data;
533 struct net_device *dev = link->priv;
534 struct orinoco_private *priv = netdev_priv(dev);
535 struct orinoco_pccard *card = priv->card;
536 int err = 0;
537 unsigned long flags;
539 switch (event) {
540 case CS_EVENT_CARD_REMOVAL:
541 link->state &= ~DEV_PRESENT;
542 if (link->state & DEV_CONFIG) {
543 orinoco_lock(priv, &flags);
545 netif_device_detach(dev);
546 priv->hw_unavailable++;
548 orinoco_unlock(priv, &flags);
550 break;
552 case CS_EVENT_CARD_INSERTION:
553 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
554 orinoco_cs_config(link);
555 break;
557 case CS_EVENT_PM_SUSPEND:
558 link->state |= DEV_SUSPEND;
559 /* Fall through... */
560 case CS_EVENT_RESET_PHYSICAL:
561 /* Mark the device as stopped, to block IO until later */
562 if (link->state & DEV_CONFIG) {
563 /* This is probably racy, but I can't think of
564 a better way, short of rewriting the PCMCIA
565 layer to not suck :-( */
566 if (! test_bit(0, &card->hard_reset_in_progress)) {
567 spin_lock_irqsave(&priv->lock, flags);
569 err = __orinoco_down(dev);
570 if (err)
571 printk(KERN_WARNING "%s: %s: Error %d downing interface\n",
572 dev->name,
573 event == CS_EVENT_PM_SUSPEND ? "SUSPEND" : "RESET_PHYSICAL",
574 err);
576 netif_device_detach(dev);
577 priv->hw_unavailable++;
579 spin_unlock_irqrestore(&priv->lock, flags);
582 pcmcia_release_configuration(link->handle);
584 break;
586 case CS_EVENT_PM_RESUME:
587 link->state &= ~DEV_SUSPEND;
588 /* Fall through... */
589 case CS_EVENT_CARD_RESET:
590 if (link->state & DEV_CONFIG) {
591 /* FIXME: should we double check that this is
592 * the same card as we had before */
593 pcmcia_request_configuration(link->handle, &link->conf);
595 if (! test_bit(0, &card->hard_reset_in_progress)) {
596 err = orinoco_reinit_firmware(dev);
597 if (err) {
598 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
599 dev->name, err);
600 break;
603 spin_lock_irqsave(&priv->lock, flags);
605 netif_device_attach(dev);
606 priv->hw_unavailable--;
608 if (priv->open && ! priv->hw_unavailable) {
609 err = __orinoco_up(dev);
610 if (err)
611 printk(KERN_ERR "%s: Error %d restarting card\n",
612 dev->name, err);
616 spin_unlock_irqrestore(&priv->lock, flags);
619 break;
622 return err;
623 } /* orinoco_cs_event */
625 /********************************************************************/
626 /* Module initialization */
627 /********************************************************************/
629 /* Can't be declared "const" or the whole __initdata section will
630 * become const */
631 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
632 " (David Gibson <hermes@gibson.dropbear.id.au>, "
633 "Pavel Roskin <proski@gnu.org>, et al)";
635 static struct pcmcia_driver orinoco_driver = {
636 .owner = THIS_MODULE,
637 .drv = {
638 .name = DRIVER_NAME,
640 .attach = orinoco_cs_attach,
641 .detach = orinoco_cs_detach,
644 static int __init
645 init_orinoco_cs(void)
647 printk(KERN_DEBUG "%s\n", version);
649 return pcmcia_register_driver(&orinoco_driver);
652 static void __exit
653 exit_orinoco_cs(void)
655 pcmcia_unregister_driver(&orinoco_driver);
657 if (dev_list)
658 DEBUG(0, PFX "Removing leftover devices.\n");
659 while (dev_list != NULL) {
660 if (dev_list->state & DEV_CONFIG)
661 orinoco_cs_release(dev_list);
662 orinoco_cs_detach(dev_list);
666 module_init(init_orinoco_cs);
667 module_exit(exit_orinoco_cs);