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.
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>
43 #include <asm/system.h>
47 /********************************************************************/
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 /* Some D-Link cards have buggy CIS. They do work at 5v properly, but
58 * don't have any CIS entry for it. This workaround it... */
59 static int ignore_cis_vcc; /* = 0 */
61 module_param(ignore_cis_vcc, int, 0);
63 /********************************************************************/
65 /********************************************************************/
68 * The dev_info variable is the "key" that is used to match up this
69 * device driver with appropriate cards, through the card
70 * configuration database.
72 static dev_info_t dev_info = DRIVER_NAME;
74 /********************************************************************/
76 /********************************************************************/
78 /* PCMCIA specific device information (goes in the card field of
79 * struct orinoco_private */
80 struct orinoco_pccard {
84 /* Used to handle hard reset */
85 /* yuck, we need this hack to work around the insanity of the
87 unsigned long hard_reset_in_progress;
91 * A linked list of "instances" of the device. Each actual PCMCIA
92 * card corresponds to one device instance, and is described by one
93 * dev_link_t structure (defined in ds.h).
95 static dev_link_t *dev_list; /* = NULL */
97 /********************************************************************/
98 /* Function prototypes */
99 /********************************************************************/
102 static int orinoco_cs_hard_reset(struct orinoco_private *priv);
105 static void orinoco_cs_config(dev_link_t * link);
106 static void orinoco_cs_release(dev_link_t * link);
107 static int orinoco_cs_event(event_t event, int priority,
108 event_callback_args_t * args);
110 static dev_link_t *orinoco_cs_attach(void);
111 static void orinoco_cs_detach(dev_link_t *);
113 /********************************************************************/
115 /********************************************************************/
118 orinoco_cs_hard_reset(struct orinoco_private *priv)
120 struct orinoco_pccard *card = priv->card;
121 dev_link_t *link = &card->link;
124 /* We need atomic ops here, because we're not holding the lock */
125 set_bit(0, &card->hard_reset_in_progress);
127 err = pcmcia_reset_card(link->handle, NULL);
131 clear_bit(0, &card->hard_reset_in_progress);
136 /********************************************************************/
138 /********************************************************************/
141 * This creates an "instance" of the driver, allocating local data
142 * structures for one device. The device is registered with Card
145 * The dev_link structure is initialized, but we don't actually
146 * configure the card at this point -- we wait until we receive a card
147 * insertion event. */
149 orinoco_cs_attach(void)
151 struct net_device *dev;
152 struct orinoco_private *priv;
153 struct orinoco_pccard *card;
155 client_reg_t client_reg;
158 dev = alloc_orinocodev(sizeof(*card), orinoco_cs_hard_reset);
161 priv = netdev_priv(dev);
164 /* Link both structures together */
168 /* Interrupt setup */
169 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
170 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
171 link->irq.Handler = NULL;
173 /* General socket configuration defaults can go here. In this
174 * client, we assume very little, and rely on the CIS for
175 * almost everything. In most clients, many details (i.e.,
176 * number, sizes, and attributes of IO windows) are fixed by
177 * the nature of the device, and can be hard-wired here. */
178 link->conf.Attributes = 0;
179 link->conf.IntType = INT_MEMORY_AND_IO;
181 /* Register with Card Services */
182 /* FIXME: need a lock? */
183 link->next = dev_list;
186 client_reg.dev_info = &dev_info;
187 client_reg.EventMask =
188 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
189 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
190 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
191 client_reg.event_handler = &orinoco_cs_event;
192 client_reg.Version = 0x0210; /* FIXME: what does this mean? */
193 client_reg.event_callback_args.client_data = link;
195 ret = pcmcia_register_client(&link->handle, &client_reg);
196 if (ret != CS_SUCCESS) {
197 cs_error(link->handle, RegisterClient, ret);
198 orinoco_cs_detach(link);
203 } /* orinoco_cs_attach */
206 * This deletes a driver "instance". The device is de-registered with
207 * Card Services. If it has been released, all local data structures
208 * are freed. Otherwise, the structures will be freed when the device
211 static void orinoco_cs_detach(dev_link_t *link)
214 struct net_device *dev = link->priv;
216 /* Locate device structure */
217 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
221 BUG_ON(*linkp == NULL);
223 if (link->state & DEV_CONFIG)
224 orinoco_cs_release(link);
226 /* Break the link with Card Services */
228 pcmcia_deregister_client(link->handle);
230 /* Unlink device structure, and free it */
232 DEBUG(0, PFX "detach: link=%p link->dev=%p\n", link, link->dev);
234 DEBUG(0, PFX "About to unregister net device %p\n",
236 unregister_netdev(dev);
239 } /* orinoco_cs_detach */
242 * orinoco_cs_config() is scheduled to run after a CARD_INSERTION
243 * event is received, to configure the PCMCIA socket, and to make the
244 * device available to the system.
247 #define CS_CHECK(fn, ret) do { \
248 last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; \
252 orinoco_cs_config(dev_link_t *link)
254 struct net_device *dev = link->priv;
255 client_handle_t handle = link->handle;
256 struct orinoco_private *priv = netdev_priv(dev);
257 struct orinoco_pccard *card = priv->card;
258 hermes_t *hw = &priv->hw;
259 int last_fn, last_ret;
266 CS_CHECK(ValidateCIS, pcmcia_validate_cis(handle, &info));
269 * This reads the card's CONFIG tuple to find its
270 * configuration registers.
272 tuple.DesiredTuple = CISTPL_CONFIG;
273 tuple.Attributes = 0;
274 tuple.TupleData = buf;
275 tuple.TupleDataMax = sizeof(buf);
276 tuple.TupleOffset = 0;
277 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
278 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
279 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
280 link->conf.ConfigBase = parse.config.base;
281 link->conf.Present = parse.config.rmask[0];
284 link->state |= DEV_CONFIG;
286 /* Look up the current Vcc */
287 CS_CHECK(GetConfigurationInfo,
288 pcmcia_get_configuration_info(handle, &conf));
289 link->conf.Vcc = conf.Vcc;
292 * In this loop, we scan the CIS for configuration table
293 * entries, each of which describes a valid card
294 * configuration, including voltage, IO window, memory window,
295 * and interrupt settings.
297 * We make no assumptions about the card to be configured: we
298 * use just the information available in the CIS. In an ideal
299 * world, this would work for any PCMCIA card, but it requires
300 * a complete and accurate CIS. In practice, a driver usually
301 * "knows" most of these things without consulting the CIS,
302 * and most client drivers will only use the CIS to fill in
303 * implementation-defined details.
305 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
306 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
308 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
309 cistpl_cftable_entry_t dflt = { .index = 0 };
311 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
312 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
315 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
319 link->conf.ConfigIndex = cfg->index;
321 /* Does this card need audio output? */
322 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
323 link->conf.Attributes |= CONF_ENABLE_SPKR;
324 link->conf.Status = CCSR_AUDIO_ENA;
327 /* Use power settings for Vcc and Vpp if present */
328 /* Note that the CIS values need to be rescaled */
329 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
330 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
331 DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
335 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
336 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
337 DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
343 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
344 link->conf.Vpp1 = link->conf.Vpp2 =
345 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
346 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
347 link->conf.Vpp1 = link->conf.Vpp2 =
348 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
350 /* Do we need to allocate an interrupt? */
351 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
352 link->conf.Attributes |= CONF_ENABLE_IRQ;
354 /* IO window settings */
355 link->io.NumPorts1 = link->io.NumPorts2 = 0;
356 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
358 (cfg->io.nwin) ? &cfg->io : &dflt.io;
359 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
360 if (!(io->flags & CISTPL_IO_8BIT))
361 link->io.Attributes1 =
362 IO_DATA_PATH_WIDTH_16;
363 if (!(io->flags & CISTPL_IO_16BIT))
364 link->io.Attributes1 =
365 IO_DATA_PATH_WIDTH_8;
366 link->io.IOAddrLines =
367 io->flags & CISTPL_IO_LINES_MASK;
368 link->io.BasePort1 = io->win[0].base;
369 link->io.NumPorts1 = io->win[0].len;
371 link->io.Attributes2 =
372 link->io.Attributes1;
373 link->io.BasePort2 = io->win[1].base;
374 link->io.NumPorts2 = io->win[1].len;
377 /* This reserves IO space but doesn't actually enable it */
378 if (pcmcia_request_io(link->handle, &link->io) != 0)
383 /* If we got this far, we're cool! */
388 if (link->io.NumPorts1)
389 pcmcia_release_io(link->handle, &link->io);
390 last_ret = pcmcia_get_next_tuple(handle, &tuple);
391 if (last_ret == CS_NO_MORE_ITEMS) {
392 printk(KERN_ERR PFX "GetNextTuple(): No matching "
393 "CIS configuration, maybe you need the "
394 "ignore_cis_vcc=1 parameter.\n");
400 * Allocate an interrupt line. Note that this does not assign
401 * a handler to the interrupt, unless the 'Handler' member of
402 * the irq structure is initialized.
404 if (link->conf.Attributes & CONF_ENABLE_IRQ) {
405 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
406 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
407 link->irq.Handler = orinoco_interrupt;
408 link->irq.Instance = dev;
410 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
413 /* We initialize the hermes structure before completing PCMCIA
414 * configuration just in case the interrupt handler gets
416 hermes_struct_init(hw, link->io.BasePort1,
417 HERMES_IO, HERMES_16BIT_REGSPACING);
420 * This actually configures the PCMCIA socket -- setting up
421 * the I/O windows and the interrupt mapping, and putting the
422 * card and host interface into "Memory and IO" mode.
424 CS_CHECK(RequestConfiguration,
425 pcmcia_request_configuration(link->handle, &link->conf));
427 /* Ok, we have the configuration, prepare to register the netdev */
428 dev->base_addr = link->io.BasePort1;
429 dev->irq = link->irq.AssignedIRQ;
430 SET_MODULE_OWNER(dev);
431 card->node.major = card->node.minor = 0;
433 /* register_netdev will give us an ethX name */
435 SET_NETDEV_DEV(dev, &handle_to_dev(handle));
436 /* Tell the stack we exist */
437 if (register_netdev(dev) != 0) {
438 printk(KERN_ERR PFX "register_netdev() failed\n");
442 /* At this point, the dev_node_t structure(s) needs to be
443 * initialized and arranged in a linked list at link->dev. */
444 strcpy(card->node.dev_name, dev->name);
445 link->dev = &card->node; /* link->dev being non-NULL is also
446 used to indicate that the
447 net_device has been registered */
448 link->state &= ~DEV_CONFIG_PENDING;
450 /* Finally, report what we've done */
451 printk(KERN_DEBUG "%s: index 0x%02x: Vcc %d.%d",
452 dev->name, link->conf.ConfigIndex,
453 link->conf.Vcc / 10, link->conf.Vcc % 10);
455 printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
456 link->conf.Vpp1 % 10);
457 if (link->conf.Attributes & CONF_ENABLE_IRQ)
458 printk(", irq %d", link->irq.AssignedIRQ);
459 if (link->io.NumPorts1)
460 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
461 link->io.BasePort1 + link->io.NumPorts1 - 1);
462 if (link->io.NumPorts2)
463 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
464 link->io.BasePort2 + link->io.NumPorts2 - 1);
470 cs_error(link->handle, last_fn, last_ret);
473 orinoco_cs_release(link);
474 } /* orinoco_cs_config */
477 * After a card is removed, orinoco_cs_release() will unregister the
478 * device, and release the PCMCIA configuration. If the device is
479 * still open, this will be postponed until it is closed.
482 orinoco_cs_release(dev_link_t *link)
484 struct net_device *dev = link->priv;
485 struct orinoco_private *priv = netdev_priv(dev);
488 /* We're committed to taking the device away now, so mark the
489 * hardware as unavailable */
490 spin_lock_irqsave(&priv->lock, flags);
491 priv->hw_unavailable++;
492 spin_unlock_irqrestore(&priv->lock, flags);
494 /* Don't bother checking to see if these succeed or not */
495 pcmcia_release_configuration(link->handle);
496 if (link->io.NumPorts1)
497 pcmcia_release_io(link->handle, &link->io);
498 if (link->irq.AssignedIRQ)
499 pcmcia_release_irq(link->handle, &link->irq);
500 link->state &= ~DEV_CONFIG;
501 } /* orinoco_cs_release */
504 * The card status event handler. Mostly, this schedules other stuff
505 * to run after an event is received.
508 orinoco_cs_event(event_t event, int priority,
509 event_callback_args_t * args)
511 dev_link_t *link = args->client_data;
512 struct net_device *dev = link->priv;
513 struct orinoco_private *priv = netdev_priv(dev);
514 struct orinoco_pccard *card = priv->card;
519 case CS_EVENT_CARD_REMOVAL:
520 link->state &= ~DEV_PRESENT;
521 if (link->state & DEV_CONFIG) {
522 orinoco_lock(priv, &flags);
524 netif_device_detach(dev);
525 priv->hw_unavailable++;
527 orinoco_unlock(priv, &flags);
531 case CS_EVENT_CARD_INSERTION:
532 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
533 orinoco_cs_config(link);
536 case CS_EVENT_PM_SUSPEND:
537 link->state |= DEV_SUSPEND;
538 /* Fall through... */
539 case CS_EVENT_RESET_PHYSICAL:
540 /* Mark the device as stopped, to block IO until later */
541 if (link->state & DEV_CONFIG) {
542 /* This is probably racy, but I can't think of
543 a better way, short of rewriting the PCMCIA
544 layer to not suck :-( */
545 if (! test_bit(0, &card->hard_reset_in_progress)) {
546 spin_lock_irqsave(&priv->lock, flags);
548 err = __orinoco_down(dev);
550 printk(KERN_WARNING "%s: %s: Error %d downing interface\n",
552 event == CS_EVENT_PM_SUSPEND ? "SUSPEND" : "RESET_PHYSICAL",
555 netif_device_detach(dev);
556 priv->hw_unavailable++;
558 spin_unlock_irqrestore(&priv->lock, flags);
561 pcmcia_release_configuration(link->handle);
565 case CS_EVENT_PM_RESUME:
566 link->state &= ~DEV_SUSPEND;
567 /* Fall through... */
568 case CS_EVENT_CARD_RESET:
569 if (link->state & DEV_CONFIG) {
570 /* FIXME: should we double check that this is
571 * the same card as we had before */
572 pcmcia_request_configuration(link->handle, &link->conf);
574 if (! test_bit(0, &card->hard_reset_in_progress)) {
575 err = orinoco_reinit_firmware(dev);
577 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
582 spin_lock_irqsave(&priv->lock, flags);
584 netif_device_attach(dev);
585 priv->hw_unavailable--;
587 if (priv->open && ! priv->hw_unavailable) {
588 err = __orinoco_up(dev);
590 printk(KERN_ERR "%s: Error %d restarting card\n",
595 spin_unlock_irqrestore(&priv->lock, flags);
602 } /* orinoco_cs_event */
604 /********************************************************************/
605 /* Module initialization */
606 /********************************************************************/
608 /* Can't be declared "const" or the whole __initdata section will
610 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
611 " (David Gibson <hermes@gibson.dropbear.id.au>, "
612 "Pavel Roskin <proski@gnu.org>, et al)";
614 static struct pcmcia_driver orinoco_driver = {
615 .owner = THIS_MODULE,
619 .attach = orinoco_cs_attach,
620 .detach = orinoco_cs_detach,
624 init_orinoco_cs(void)
626 printk(KERN_DEBUG "%s\n", version);
628 return pcmcia_register_driver(&orinoco_driver);
632 exit_orinoco_cs(void)
634 pcmcia_unregister_driver(&orinoco_driver);
635 BUG_ON(dev_list != NULL);
638 module_init(init_orinoco_cs);
639 module_exit(exit_orinoco_cs);