1 /* orinoco_cs.c (formerly known as dldwd_cs.c)
2 *
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
11 */
12
13 #define DRIVER_NAME "orinoco_cs"
14 #define PFX DRIVER_NAME ": "
15
16 #include <linux/config.h>
17 #ifdef __IN_PCMCIA_PACKAGE__
18 #include <pcmcia/k_compat.h>
19 #endif /* __IN_PCMCIA_PACKAGE__ */
20
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>
33
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>
40
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
43 #include <asm/system.h>
44
45 #include "orinoco.h"
46
47 /********************************************************************/
48 /* Module stuff */
49 /********************************************************************/
50
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");
54
55 /* Module parameters */
56
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 */
60
61 module_param(ignore_cis_vcc, int, 0);
62
63 /********************************************************************/
64 /* Magic constants */
65 /********************************************************************/
66
67 /*
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.
71 */
72 static dev_info_t dev_info = DRIVER_NAME;
73
74 /********************************************************************/
75 /* Data structures */
76 /********************************************************************/
77
78 /* PCMCIA specific device information (goes in the card field of
79 * struct orinoco_private */
80 struct orinoco_pccard {
81 dev_link_t link;
82 dev_node_t node;
83
84 /* Used to handle hard reset */
85 /* yuck, we need this hack to work around the insanity of the
86 * PCMCIA layer */
87 unsigned long hard_reset_in_progress;
88 };
89
90 /*
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).
94 */
95 static dev_link_t *dev_list; /* = NULL */
96
97 /********************************************************************/
98 /* Function prototypes */
99 /********************************************************************/
100
101 /* device methods */
102 static int orinoco_cs_hard_reset(struct orinoco_private *priv);
103
104 /* PCMCIA gumpf */
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);
109
110 static dev_link_t *orinoco_cs_attach(void);
111 static void orinoco_cs_detach(dev_link_t *);
112
113 /********************************************************************/
114 /* Device methods */
115 /********************************************************************/
116
117 static int
118 orinoco_cs_hard_reset(struct orinoco_private *priv)
119 {
120 struct orinoco_pccard *card = priv->card;
121 dev_link_t *link = &card->link;
122 int err;
123
124 /* We need atomic ops here, because we're not holding the lock */
125 set_bit(0, &card->hard_reset_in_progress);
126
127 err = pcmcia_reset_card(link->handle, NULL);
128 if (err)
129 return err;
130
131 clear_bit(0, &card->hard_reset_in_progress);
132
133 return 0;
134 }
135
136 /********************************************************************/
137 /* PCMCIA stuff */
138 /********************************************************************/
139
140 /*
141 * This creates an "instance" of the driver, allocating local data
142 * structures for one device. The device is registered with Card
143 * Services.
144 *
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. */
148 static dev_link_t *
149 orinoco_cs_attach(void)
150 {
151 struct net_device *dev;
152 struct orinoco_private *priv;
153 struct orinoco_pccard *card;
154 dev_link_t *link;
155 client_reg_t client_reg;
156 int ret;
157
158 dev = alloc_orinocodev(sizeof(*card), orinoco_cs_hard_reset);
159 if (! dev)
160 return NULL;
161 priv = netdev_priv(dev);
162 card = priv->card;
163
164 /* Link both structures together */
165 link = &card->link;
166 link->priv = dev;
167
168 /* Interrupt setup */
169 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
170 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
171 link->irq.Handler = NULL;
172
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;
180
181 /* Register with Card Services */
182 /* FIXME: need a lock? */
183 link->next = dev_list;
184 dev_list = link;
185
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;
194
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);
199 return NULL;
200 }
201
202 return link;
203 } /* orinoco_cs_attach */
204
205 /*
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
209 * is released.
210 */
211 static void orinoco_cs_detach(dev_link_t *link)
212 {
213 dev_link_t **linkp;
214 struct net_device *dev = link->priv;
215
216 /* Locate device structure */
217 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
218 if (*linkp == link)
219 break;
220
221 BUG_ON(*linkp == NULL);
222
223 if (link->state & DEV_CONFIG)
224 orinoco_cs_release(link);
225
226 /* Break the link with Card Services */
227 if (link->handle)
228 pcmcia_deregister_client(link->handle);
229
230 /* Unlink device structure, and free it */
231 *linkp = link->next;
232 DEBUG(0, PFX "detach: link=%p link->dev=%p\n", link, link->dev);
233 if (link->dev) {
234 DEBUG(0, PFX "About to unregister net device %p\n",
235 dev);
236 unregister_netdev(dev);
237 }
238 free_netdev(dev);
239 } /* orinoco_cs_detach */
240
241 /*
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.
245 */
246
247 #define CS_CHECK(fn, ret) do { \
248 last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; \
249 } while (0)
250
251 static void
252 orinoco_cs_config(dev_link_t *link)
253 {
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;
260 u_char buf[64];
261 config_info_t conf;
262 cisinfo_t info;
263 tuple_t tuple;
264 cisparse_t parse;
265
266 CS_CHECK(ValidateCIS, pcmcia_validate_cis(handle, &info));
267
268 /*
269 * This reads the card's CONFIG tuple to find its
270 * configuration registers.
271 */
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];
282
283 /* Configure card */
284 link->state |= DEV_CONFIG;
285
286 /* Look up the current Vcc */
287 CS_CHECK(GetConfigurationInfo,
288 pcmcia_get_configuration_info(handle, &conf));
289 link->conf.Vcc = conf.Vcc;
290
291 /*
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.
296 *
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.
304 */
305 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
306 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
307 while (1) {
308 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
309 cistpl_cftable_entry_t dflt = { .index = 0 };
310
311 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
312 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
313 goto next_entry;
314
315 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
316 dflt = *cfg;
317 if (cfg->index == 0)
318 goto next_entry;
319 link->conf.ConfigIndex = cfg->index;
320
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;
325 }
326
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);
332 if (!ignore_cis_vcc)
333 goto next_entry;
334 }
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);
338 if(!ignore_cis_vcc)
339 goto next_entry;
340 }
341 }
342
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;
349
350 /* Do we need to allocate an interrupt? */
351 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
352 link->conf.Attributes |= CONF_ENABLE_IRQ;
353
354 /* IO window settings */
355 link->io.NumPorts1 = link->io.NumPorts2 = 0;
356 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
357 cistpl_io_t *io =
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;
370 if (io->nwin > 1) {
371 link->io.Attributes2 =
372 link->io.Attributes1;
373 link->io.BasePort2 = io->win[1].base;
374 link->io.NumPorts2 = io->win[1].len;
375 }
376
377 /* This reserves IO space but doesn't actually enable it */
378 if (pcmcia_request_io(link->handle, &link->io) != 0)
379 goto next_entry;
380 }
381
382
383 /* If we got this far, we're cool! */
384
385 break;
386
387 next_entry:
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");
395 goto cs_failed;
396 }
397 }
398
399 /*
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.
403 */
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;
409
410 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
411 }
412
413 /* We initialize the hermes structure before completing PCMCIA
414 * configuration just in case the interrupt handler gets
415 * called. */
416 hermes_struct_init(hw, link->io.BasePort1,
417 HERMES_IO, HERMES_16BIT_REGSPACING);
418
419 /*
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.
423 */
424 CS_CHECK(RequestConfiguration,
425 pcmcia_request_configuration(link->handle, &link->conf));
426
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;
432
433 /* register_netdev will give us an ethX name */
434 dev->name[0] = '\0';
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");
439 goto failed;
440 }
441
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;
449
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);
454 if (link->conf.Vpp1)
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);
465 printk("\n");
466
467 return;
468
469 cs_failed:
470 cs_error(link->handle, last_fn, last_ret);
471
472 failed:
473 orinoco_cs_release(link);
474 } /* orinoco_cs_config */
475
476 /*
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.
480 */
481 static void
482 orinoco_cs_release(dev_link_t *link)
483 {
484 struct net_device *dev = link->priv;
485 struct orinoco_private *priv = netdev_priv(dev);
486 unsigned long flags;
487
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);
493
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 */
502
503 /*
504 * The card status event handler. Mostly, this schedules other stuff
505 * to run after an event is received.
506 */
507 static int
508 orinoco_cs_event(event_t event, int priority,
509 event_callback_args_t * args)
510 {
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;
515 int err = 0;
516 unsigned long flags;
517
518 switch (event) {
519 case CS_EVENT_CARD_REMOVAL:
520 link->state &= ~DEV_PRESENT;
521 if (link->state & DEV_CONFIG) {
522 orinoco_lock(priv, &flags);
523
524 netif_device_detach(dev);
525 priv->hw_unavailable++;
526
527 orinoco_unlock(priv, &flags);
528 }
529 break;
530
531 case CS_EVENT_CARD_INSERTION:
532 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
533 orinoco_cs_config(link);
534 break;
535
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);
547
548 err = __orinoco_down(dev);
549 if (err)
550 printk(KERN_WARNING "%s: %s: Error %d downing interface\n",
551 dev->name,
552 event == CS_EVENT_PM_SUSPEND ? "SUSPEND" : "RESET_PHYSICAL",
553 err);
554
555 netif_device_detach(dev);
556 priv->hw_unavailable++;
557
558 spin_unlock_irqrestore(&priv->lock, flags);
559 }
560
561 pcmcia_release_configuration(link->handle);
562 }
563 break;
564
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);
573
574 if (! test_bit(0, &card->hard_reset_in_progress)) {
575 err = orinoco_reinit_firmware(dev);
576 if (err) {
577 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
578 dev->name, err);
579 break;
580 }
581
582 spin_lock_irqsave(&priv->lock, flags);
583
584 netif_device_attach(dev);
585 priv->hw_unavailable--;
586
587 if (priv->open && ! priv->hw_unavailable) {
588 err = __orinoco_up(dev);
589 if (err)
590 printk(KERN_ERR "%s: Error %d restarting card\n",
591 dev->name, err);
592
593 }
594
595 spin_unlock_irqrestore(&priv->lock, flags);
596 }
597 }
598 break;
599 }
600
601 return err;
602 } /* orinoco_cs_event */
603
604 /********************************************************************/
605 /* Module initialization */
606 /********************************************************************/
607
608 /* Can't be declared "const" or the whole __initdata section will
609 * become const */
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)";
613
614 static struct pcmcia_driver orinoco_driver = {
615 .owner = THIS_MODULE,
616 .drv = {
617 .name = DRIVER_NAME,
618 },
619 .attach = orinoco_cs_attach,
620 .detach = orinoco_cs_detach,
621 };
622
623 static int __init
624 init_orinoco_cs(void)
625 {
626 printk(KERN_DEBUG "%s\n", version);
627
628 return pcmcia_register_driver(&orinoco_driver);
629 }
630
631 static void __exit
632 exit_orinoco_cs(void)
633 {
634 pcmcia_unregister_driver(&orinoco_driver);
635 BUG_ON(dev_list != NULL);
636 }
637
638 module_init(init_orinoco_cs);
639 module_exit(exit_orinoco_cs);
640
|
This page was automatically generated by the
LXR engine.
|