orinoco: Rename orinoco.c
[safe/jmp/linux-2.6] / drivers / net / wireless / orinoco / spectrum_cs.c
1 /*
2  * Driver for 802.11b cards using RAM-loadable Symbol firmware, such as
3  * Symbol Wireless Networker LA4137, CompactFlash cards by Socket
4  * Communications and Intel PRO/Wireless 2011B.
5  *
6  * The driver implements Symbol firmware download.  The rest is handled
7  * in hermes.c and main.c.
8  *
9  * Utilities for downloading the Symbol firmware are available at
10  * http://sourceforge.net/projects/orinoco/
11  *
12  * Copyright (C) 2002-2005 Pavel Roskin <proski@gnu.org>
13  * Portions based on orinoco_cs.c:
14  *      Copyright (C) David Gibson, Linuxcare Australia
15  * Portions based on Spectrum24tDnld.c from original spectrum24 driver:
16  *      Copyright (C) Symbol Technologies.
17  *
18  * See copyright notice in file main.c.
19  */
20
21 #define DRIVER_NAME "spectrum_cs"
22 #define PFX DRIVER_NAME ": "
23
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <pcmcia/cs_types.h>
29 #include <pcmcia/cs.h>
30 #include <pcmcia/cistpl.h>
31 #include <pcmcia/cisreg.h>
32 #include <pcmcia/ds.h>
33
34 #include "orinoco.h"
35
36 /********************************************************************/
37 /* Module stuff                                                     */
38 /********************************************************************/
39
40 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
41 MODULE_DESCRIPTION("Driver for Symbol Spectrum24 Trilogy cards with firmware downloader");
42 MODULE_LICENSE("Dual MPL/GPL");
43
44 /* Module parameters */
45
46 /* Some D-Link cards have buggy CIS. They do work at 5v properly, but
47  * don't have any CIS entry for it. This workaround it... */
48 static int ignore_cis_vcc; /* = 0 */
49 module_param(ignore_cis_vcc, int, 0);
50 MODULE_PARM_DESC(ignore_cis_vcc, "Allow voltage mismatch between card and socket");
51
52 /********************************************************************/
53 /* Data structures                                                  */
54 /********************************************************************/
55
56 /* PCMCIA specific device information (goes in the card field of
57  * struct orinoco_private */
58 struct orinoco_pccard {
59         struct pcmcia_device    *p_dev;
60         dev_node_t node;
61 };
62
63 /********************************************************************/
64 /* Function prototypes                                              */
65 /********************************************************************/
66
67 static int spectrum_cs_config(struct pcmcia_device *link);
68 static void spectrum_cs_release(struct pcmcia_device *link);
69
70 /* Constants for the CISREG_CCSR register */
71 #define HCR_RUN         0x07    /* run firmware after reset */
72 #define HCR_IDLE        0x0E    /* don't run firmware after reset */
73 #define HCR_MEM16       0x10    /* memory width bit, should be preserved */
74
75
76 #define CS_CHECK(fn, ret) \
77   do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
78
79 /*
80  * Reset the card using configuration registers COR and CCSR.
81  * If IDLE is 1, stop the firmware, so that it can be safely rewritten.
82  */
83 static int
84 spectrum_reset(struct pcmcia_device *link, int idle)
85 {
86         int last_ret, last_fn;
87         conf_reg_t reg;
88         u_int save_cor;
89
90         /* Doing it if hardware is gone is guaranteed crash */
91         if (!pcmcia_dev_present(link))
92                 return -ENODEV;
93
94         /* Save original COR value */
95         reg.Function = 0;
96         reg.Action = CS_READ;
97         reg.Offset = CISREG_COR;
98         CS_CHECK(AccessConfigurationRegister,
99                  pcmcia_access_configuration_register(link, &reg));
100         save_cor = reg.Value;
101
102         /* Soft-Reset card */
103         reg.Action = CS_WRITE;
104         reg.Offset = CISREG_COR;
105         reg.Value = (save_cor | COR_SOFT_RESET);
106         CS_CHECK(AccessConfigurationRegister,
107                  pcmcia_access_configuration_register(link, &reg));
108         udelay(1000);
109
110         /* Read CCSR */
111         reg.Action = CS_READ;
112         reg.Offset = CISREG_CCSR;
113         CS_CHECK(AccessConfigurationRegister,
114                  pcmcia_access_configuration_register(link, &reg));
115
116         /*
117          * Start or stop the firmware.  Memory width bit should be
118          * preserved from the value we've just read.
119          */
120         reg.Action = CS_WRITE;
121         reg.Offset = CISREG_CCSR;
122         reg.Value = (idle ? HCR_IDLE : HCR_RUN) | (reg.Value & HCR_MEM16);
123         CS_CHECK(AccessConfigurationRegister,
124                  pcmcia_access_configuration_register(link, &reg));
125         udelay(1000);
126
127         /* Restore original COR configuration index */
128         reg.Action = CS_WRITE;
129         reg.Offset = CISREG_COR;
130         reg.Value = (save_cor & ~COR_SOFT_RESET);
131         CS_CHECK(AccessConfigurationRegister,
132                  pcmcia_access_configuration_register(link, &reg));
133         udelay(1000);
134         return 0;
135
136 cs_failed:
137         cs_error(link, last_fn, last_ret);
138         return -ENODEV;
139 }
140
141 /********************************************************************/
142 /* Device methods                                                   */
143 /********************************************************************/
144
145 static int
146 spectrum_cs_hard_reset(struct orinoco_private *priv)
147 {
148         struct orinoco_pccard *card = priv->card;
149         struct pcmcia_device *link = card->p_dev;
150
151         /* Soft reset using COR and HCR */
152         spectrum_reset(link, 0);
153
154         return 0;
155 }
156
157 static int
158 spectrum_cs_stop_firmware(struct orinoco_private *priv, int idle)
159 {
160         struct orinoco_pccard *card = priv->card;
161         struct pcmcia_device *link = card->p_dev;
162
163         return spectrum_reset(link, idle);
164 }
165
166 /********************************************************************/
167 /* PCMCIA stuff                                                     */
168 /********************************************************************/
169
170 /*
171  * This creates an "instance" of the driver, allocating local data
172  * structures for one device.  The device is registered with Card
173  * Services.
174  *
175  * The dev_link structure is initialized, but we don't actually
176  * configure the card at this point -- we wait until we receive a card
177  * insertion event.  */
178 static int
179 spectrum_cs_probe(struct pcmcia_device *link)
180 {
181         struct net_device *dev;
182         struct orinoco_private *priv;
183         struct orinoco_pccard *card;
184
185         dev = alloc_orinocodev(sizeof(*card), &handle_to_dev(link),
186                                spectrum_cs_hard_reset,
187                                spectrum_cs_stop_firmware);
188         if (!dev)
189                 return -ENOMEM;
190         priv = netdev_priv(dev);
191         card = priv->card;
192
193         /* Link both structures together */
194         card->p_dev = link;
195         link->priv = dev;
196
197         /* Interrupt setup */
198         link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
199         link->irq.IRQInfo1 = IRQ_LEVEL_ID;
200         link->irq.Handler = orinoco_interrupt;
201         link->irq.Instance = dev;
202
203         /* General socket configuration defaults can go here.  In this
204          * client, we assume very little, and rely on the CIS for
205          * almost everything.  In most clients, many details (i.e.,
206          * number, sizes, and attributes of IO windows) are fixed by
207          * the nature of the device, and can be hard-wired here. */
208         link->conf.Attributes = 0;
209         link->conf.IntType = INT_MEMORY_AND_IO;
210
211         return spectrum_cs_config(link);
212 }                               /* spectrum_cs_attach */
213
214 /*
215  * This deletes a driver "instance".  The device is de-registered with
216  * Card Services.  If it has been released, all local data structures
217  * are freed.  Otherwise, the structures will be freed when the device
218  * is released.
219  */
220 static void spectrum_cs_detach(struct pcmcia_device *link)
221 {
222         struct net_device *dev = link->priv;
223
224         if (link->dev_node)
225                 unregister_netdev(dev);
226
227         spectrum_cs_release(link);
228
229         free_orinocodev(dev);
230 }                               /* spectrum_cs_detach */
231
232 /*
233  * spectrum_cs_config() is scheduled to run after a CARD_INSERTION
234  * event is received, to configure the PCMCIA socket, and to make the
235  * device available to the system.
236  */
237
238 static int spectrum_cs_config_check(struct pcmcia_device *p_dev,
239                                     cistpl_cftable_entry_t *cfg,
240                                     cistpl_cftable_entry_t *dflt,
241                                     unsigned int vcc,
242                                     void *priv_data)
243 {
244         if (cfg->index == 0)
245                 goto next_entry;
246
247         /* Use power settings for Vcc and Vpp if present */
248         /* Note that the CIS values need to be rescaled */
249         if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
250                 if (vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
251                         DEBUG(2, "%s: Vcc mismatch (vcc = %d, CIS = %d)\n",
252                               __func__, vcc,
253                               cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
254                         if (!ignore_cis_vcc)
255                                 goto next_entry;
256                 }
257         } else if (dflt->vcc.present & (1 << CISTPL_POWER_VNOM)) {
258                 if (vcc != dflt->vcc.param[CISTPL_POWER_VNOM] / 10000) {
259                         DEBUG(2, "%s: Vcc mismatch (vcc = %d, CIS = %d)\n",
260                               __func__, vcc,
261                               dflt->vcc.param[CISTPL_POWER_VNOM] / 10000);
262                         if (!ignore_cis_vcc)
263                                 goto next_entry;
264                 }
265         }
266
267         if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
268                 p_dev->conf.Vpp =
269                         cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
270         else if (dflt->vpp1.present & (1 << CISTPL_POWER_VNOM))
271                 p_dev->conf.Vpp =
272                         dflt->vpp1.param[CISTPL_POWER_VNOM] / 10000;
273
274         /* Do we need to allocate an interrupt? */
275         p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
276
277         /* IO window settings */
278         p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
279         if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
280                 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
281                 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
282                 if (!(io->flags & CISTPL_IO_8BIT))
283                         p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
284                 if (!(io->flags & CISTPL_IO_16BIT))
285                         p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
286                 p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
287                 p_dev->io.BasePort1 = io->win[0].base;
288                 p_dev->io.NumPorts1 = io->win[0].len;
289                 if (io->nwin > 1) {
290                         p_dev->io.Attributes2 = p_dev->io.Attributes1;
291                         p_dev->io.BasePort2 = io->win[1].base;
292                         p_dev->io.NumPorts2 = io->win[1].len;
293                 }
294
295                 /* This reserves IO space but doesn't actually enable it */
296                 if (pcmcia_request_io(p_dev, &p_dev->io) != 0)
297                         goto next_entry;
298         }
299         return 0;
300
301 next_entry:
302         pcmcia_disable_device(p_dev);
303         return -ENODEV;
304 };
305
306 static int
307 spectrum_cs_config(struct pcmcia_device *link)
308 {
309         struct net_device *dev = link->priv;
310         struct orinoco_private *priv = netdev_priv(dev);
311         struct orinoco_pccard *card = priv->card;
312         hermes_t *hw = &priv->hw;
313         int last_fn, last_ret;
314         void __iomem *mem;
315
316         /*
317          * In this loop, we scan the CIS for configuration table
318          * entries, each of which describes a valid card
319          * configuration, including voltage, IO window, memory window,
320          * and interrupt settings.
321          *
322          * We make no assumptions about the card to be configured: we
323          * use just the information available in the CIS.  In an ideal
324          * world, this would work for any PCMCIA card, but it requires
325          * a complete and accurate CIS.  In practice, a driver usually
326          * "knows" most of these things without consulting the CIS,
327          * and most client drivers will only use the CIS to fill in
328          * implementation-defined details.
329          */
330         last_ret = pcmcia_loop_config(link, spectrum_cs_config_check, NULL);
331         if (last_ret) {
332                 if (!ignore_cis_vcc)
333                         printk(KERN_ERR PFX "GetNextTuple(): No matching "
334                                "CIS configuration.  Maybe you need the "
335                                "ignore_cis_vcc=1 parameter.\n");
336                 cs_error(link, RequestIO, last_ret);
337                 goto failed;
338         }
339
340         /*
341          * Allocate an interrupt line.  Note that this does not assign
342          * a handler to the interrupt, unless the 'Handler' member of
343          * the irq structure is initialized.
344          */
345         CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
346
347         /* We initialize the hermes structure before completing PCMCIA
348          * configuration just in case the interrupt handler gets
349          * called. */
350         mem = ioport_map(link->io.BasePort1, link->io.NumPorts1);
351         if (!mem)
352                 goto cs_failed;
353
354         hermes_struct_init(hw, mem, HERMES_16BIT_REGSPACING);
355
356         /*
357          * This actually configures the PCMCIA socket -- setting up
358          * the I/O windows and the interrupt mapping, and putting the
359          * card and host interface into "Memory and IO" mode.
360          */
361         CS_CHECK(RequestConfiguration,
362                  pcmcia_request_configuration(link, &link->conf));
363
364         /* Ok, we have the configuration, prepare to register the netdev */
365         dev->base_addr = link->io.BasePort1;
366         dev->irq = link->irq.AssignedIRQ;
367         card->node.major = card->node.minor = 0;
368
369         /* Reset card */
370         if (spectrum_cs_hard_reset(priv) != 0)
371                 goto failed;
372
373         SET_NETDEV_DEV(dev, &handle_to_dev(link));
374         /* Tell the stack we exist */
375         if (register_netdev(dev) != 0) {
376                 printk(KERN_ERR PFX "register_netdev() failed\n");
377                 goto failed;
378         }
379
380         /* At this point, the dev_node_t structure(s) needs to be
381          * initialized and arranged in a linked list at link->dev_node. */
382         strcpy(card->node.dev_name, dev->name);
383         link->dev_node = &card->node; /* link->dev_node being non-NULL is also
384                                        * used to indicate that the
385                                        * net_device has been registered */
386
387         /* Finally, report what we've done */
388         printk(KERN_DEBUG "%s: " DRIVER_NAME " at %s, irq %d, io "
389                "0x%04x-0x%04x\n", dev->name, dev_name(dev->dev.parent),
390                link->irq.AssignedIRQ, link->io.BasePort1,
391                link->io.BasePort1 + link->io.NumPorts1 - 1);
392
393         return 0;
394
395  cs_failed:
396         cs_error(link, last_fn, last_ret);
397
398  failed:
399         spectrum_cs_release(link);
400         return -ENODEV;
401 }                               /* spectrum_cs_config */
402
403 /*
404  * After a card is removed, spectrum_cs_release() will unregister the
405  * device, and release the PCMCIA configuration.  If the device is
406  * still open, this will be postponed until it is closed.
407  */
408 static void
409 spectrum_cs_release(struct pcmcia_device *link)
410 {
411         struct net_device *dev = link->priv;
412         struct orinoco_private *priv = netdev_priv(dev);
413         unsigned long flags;
414
415         /* We're committed to taking the device away now, so mark the
416          * hardware as unavailable */
417         spin_lock_irqsave(&priv->lock, flags);
418         priv->hw_unavailable++;
419         spin_unlock_irqrestore(&priv->lock, flags);
420
421         pcmcia_disable_device(link);
422         if (priv->hw.iobase)
423                 ioport_unmap(priv->hw.iobase);
424 }                               /* spectrum_cs_release */
425
426
427 static int
428 spectrum_cs_suspend(struct pcmcia_device *link)
429 {
430         struct net_device *dev = link->priv;
431         struct orinoco_private *priv = netdev_priv(dev);
432         unsigned long flags;
433         int err = 0;
434
435         /* Mark the device as stopped, to block IO until later */
436         spin_lock_irqsave(&priv->lock, flags);
437
438         err = __orinoco_down(dev);
439         if (err)
440                 printk(KERN_WARNING "%s: Error %d downing interface\n",
441                        dev->name, err);
442
443         netif_device_detach(dev);
444         priv->hw_unavailable++;
445
446         spin_unlock_irqrestore(&priv->lock, flags);
447
448         return err;
449 }
450
451 static int
452 spectrum_cs_resume(struct pcmcia_device *link)
453 {
454         struct net_device *dev = link->priv;
455         struct orinoco_private *priv = netdev_priv(dev);
456         unsigned long flags;
457         int err;
458
459         err = orinoco_reinit_firmware(dev);
460         if (err) {
461                 printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
462                        dev->name, err);
463                 return -EIO;
464         }
465
466         spin_lock_irqsave(&priv->lock, flags);
467
468         netif_device_attach(dev);
469         priv->hw_unavailable--;
470
471         if (priv->open && !priv->hw_unavailable) {
472                 err = __orinoco_up(dev);
473                 if (err)
474                         printk(KERN_ERR "%s: Error %d restarting card\n",
475                                dev->name, err);
476         }
477
478         spin_unlock_irqrestore(&priv->lock, flags);
479
480         return 0;
481 }
482
483
484 /********************************************************************/
485 /* Module initialization                                            */
486 /********************************************************************/
487
488 /* Can't be declared "const" or the whole __initdata section will
489  * become const */
490 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
491         " (Pavel Roskin <proski@gnu.org>,"
492         " David Gibson <hermes@gibson.dropbear.id.au>, et al)";
493
494 static struct pcmcia_device_id spectrum_cs_ids[] = {
495         PCMCIA_DEVICE_MANF_CARD(0x026c, 0x0001), /* Symbol Spectrum24 LA4137 */
496         PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0001), /* Socket Communications CF */
497         PCMCIA_DEVICE_PROD_ID12("Intel", "PRO/Wireless LAN PC Card", 0x816cc815, 0x6fbf459a), /* 2011B, not 2011 */
498         PCMCIA_DEVICE_NULL,
499 };
500 MODULE_DEVICE_TABLE(pcmcia, spectrum_cs_ids);
501
502 static struct pcmcia_driver orinoco_driver = {
503         .owner          = THIS_MODULE,
504         .drv            = {
505                 .name   = DRIVER_NAME,
506         },
507         .probe          = spectrum_cs_probe,
508         .remove         = spectrum_cs_detach,
509         .suspend        = spectrum_cs_suspend,
510         .resume         = spectrum_cs_resume,
511         .id_table       = spectrum_cs_ids,
512 };
513
514 static int __init
515 init_spectrum_cs(void)
516 {
517         printk(KERN_DEBUG "%s\n", version);
518
519         return pcmcia_register_driver(&orinoco_driver);
520 }
521
522 static void __exit
523 exit_spectrum_cs(void)
524 {
525         pcmcia_unregister_driver(&orinoco_driver);
526 }
527
528 module_init(init_spectrum_cs);
529 module_exit(exit_spectrum_cs);