ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / wireless / orinoco_pci.c
1 /* orinoco_pci.c 0.13e
2  * 
3  * Driver for Prism II devices that have a direct PCI interface
4  * (i.e., not in a Pcmcia or PLX bridge)
5  *
6  * Specifically here we're talking about the Linksys WMP11
7  *
8  * Some of this code is borrowed from orinoco_plx.c
9  *      Copyright (C) 2001 Daniel Barlow <dan@telent.net>
10  * Some of this code is "inspired" by linux-wlan-ng-0.1.10, but nothing
11  * has been copied from it. linux-wlan-ng-0.1.10 is originally :
12  *      Copyright (C) 1999 AbsoluteValue Systems, Inc.  All Rights Reserved.
13  * This file originally written by:
14  *      Copyright (C) 2001 Jean Tourrilhes <jt@hpl.hp.com>
15  * And is now maintained by:
16  *      Copyright (C) 2002 David Gibson, IBM Corporation <herme@gibson.dropbear.id.au>
17  *
18  * The contents of this file are subject to the Mozilla Public License
19  * Version 1.1 (the "License"); you may not use this file except in
20  * compliance with the License. You may obtain a copy of the License
21  * at http://www.mozilla.org/MPL/
22  *
23  * Software distributed under the License is distributed on an "AS IS"
24  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
25  * the License for the specific language governing rights and
26  * limitations under the License.
27  *
28  * Alternatively, the contents of this file may be used under the
29  * terms of the GNU General Public License version 2 (the "GPL"), in
30  * which case the provisions of the GPL are applicable instead of the
31  * above.  If you wish to allow the use of your version of this file
32  * only under the terms of the GPL and not to allow others to use your
33  * version of this file under the MPL, indicate your decision by
34  * deleting the provisions above and replace them with the notice and
35  * other provisions required by the GPL.  If you do not delete the
36  * provisions above, a recipient may use your version of this file
37  * under either the MPL or the GPL.
38  */
39
40 /*
41  * Theory of operation...
42  * -------------------
43  * Maybe you had a look in orinoco_plx. Well, this is totally different...
44  *
45  * The card contains only one PCI region, which contains all the usual
46  * hermes registers.
47  *
48  * The driver will memory map this region in normal memory. Because
49  * the hermes registers are mapped in normal memory and not in ISA I/O
50  * post space, we can't use the usual inw/outw macros and we need to
51  * use readw/writew.
52  * This slight difference force us to compile our own version of
53  * hermes.c with the register access macro changed. That's a bit
54  * hackish but works fine.
55  *
56  * Note that the PCI region is pretty big (4K). That's much more than
57  * the usual set of hermes register (0x0 -> 0x3E). I've got a strong
58  * suspicion that the whole memory space of the adapter is in fact in
59  * this region. Accessing directly the adapter memory instead of going
60  * through the usual register would speed up significantely the
61  * operations...
62  *
63  * Finally, the card looks like this :
64 -----------------------
65   Bus  0, device  14, function  0:
66     Network controller: PCI device 1260:3873 (Harris Semiconductor) (rev 1).
67       IRQ 11.
68       Master Capable.  Latency=248.  
69       Prefetchable 32 bit memory at 0xffbcc000 [0xffbccfff].
70 -----------------------
71 00:0e.0 Network controller: Harris Semiconductor: Unknown device 3873 (rev 01)
72         Subsystem: Unknown device 1737:3874
73         Control: I/O+ Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B-
74         Status: Cap+ 66Mhz- UDF- FastB2B+ ParErr- DEVSEL=medium >TAbort- <TAbort- <MAbort- >SERR- <PERR-
75         Latency: 248 set, cache line size 08
76         Interrupt: pin A routed to IRQ 11
77         Region 0: Memory at ffbcc000 (32-bit, prefetchable) [size=4K]
78         Capabilities: [dc] Power Management version 2
79                 Flags: PMEClk- AuxPwr- DSI- D1+ D2+ PME+
80                 Status: D0 PME-Enable- DSel=0 DScale=0 PME-
81 -----------------------
82  *
83  * That's all..
84  *
85  * Jean II
86  */
87
88 #include <linux/config.h>
89
90 #include <linux/module.h>
91 #include <linux/kernel.h>
92 #include <linux/init.h>
93 #include <linux/sched.h>
94 #include <linux/ptrace.h>
95 #include <linux/slab.h>
96 #include <linux/string.h>
97 #include <linux/timer.h>
98 #include <linux/ioport.h>
99 #include <linux/netdevice.h>
100 #include <linux/if_arp.h>
101 #include <linux/etherdevice.h>
102 #include <linux/wireless.h>
103 #include <linux/list.h>
104 #include <linux/pci.h>
105 #include <linux/wireless.h>
106 #include <linux/fcntl.h>
107
108 #include <asm/uaccess.h>
109 #include <asm/io.h>
110 #include <asm/system.h>
111
112 #include "hermes.h"
113 #include "orinoco.h"
114
115 /* All the magic there is from wlan-ng */
116 /* Magic offset of the reset register of the PCI card */
117 #define HERMES_PCI_COR          (0x26)
118 /* Magic bitmask to reset the card */
119 #define HERMES_PCI_COR_MASK     (0x0080)
120 /* Magic timeouts for doing the reset.
121  * Those times are straight from wlan-ng, and it is claimed that they
122  * are necessary. Alan will kill me. Take your time and grab a coffee. */
123 #define HERMES_PCI_COR_ONT      (250)           /* ms */
124 #define HERMES_PCI_COR_OFFT     (500)           /* ms */
125 #define HERMES_PCI_COR_BUSYT    (500)           /* ms */
126
127 /*
128  * Do a soft reset of the PCI card using the Configuration Option Register
129  * We need this to get going...
130  * This is the part of the code that is strongly inspired from wlan-ng
131  *
132  * Note : This code is done with irq enabled. This mean that many
133  * interrupts will occur while we are there. This is why we use the
134  * jiffies to regulate time instead of a straight mdelay(). Usually we
135  * need only around 245 iteration of the loop to do 250 ms delay.
136  *
137  * Note bis : Don't try to access HERMES_CMD during the reset phase.
138  * It just won't work !
139  */
140 static int
141 orinoco_pci_cor_reset(struct orinoco_private *priv)
142 {
143         hermes_t *hw = &priv->hw;
144         unsigned long   timeout;
145         u16     reg;
146
147         /* Assert the reset until the card notice */
148         hermes_write_regn(hw, PCI_COR, HERMES_PCI_COR_MASK);
149         printk(KERN_NOTICE "Reset done");
150         timeout = jiffies + (HERMES_PCI_COR_ONT * HZ / 1000);
151         while(time_before(jiffies, timeout)) {
152                 printk(".");
153                 mdelay(1);
154         }
155         printk(";\n");
156         //mdelay(HERMES_PCI_COR_ONT);
157
158         /* Give time for the card to recover from this hard effort */
159         hermes_write_regn(hw, PCI_COR, 0x0000);
160         printk(KERN_NOTICE "Clear Reset");
161         timeout = jiffies + (HERMES_PCI_COR_OFFT * HZ / 1000);
162         while(time_before(jiffies, timeout)) {
163                 printk(".");
164                 mdelay(1);
165         }
166         printk(";\n");
167         //mdelay(HERMES_PCI_COR_OFFT);
168
169         /* The card is ready when it's no longer busy */
170         timeout = jiffies + (HERMES_PCI_COR_BUSYT * HZ / 1000);
171         reg = hermes_read_regn(hw, CMD);
172         while (time_before(jiffies, timeout) && (reg & HERMES_CMD_BUSY)) {
173                 mdelay(1);
174                 reg = hermes_read_regn(hw, CMD);
175         }
176         /* Did we timeout ? */
177         if(time_after_eq(jiffies, timeout)) {
178                 printk(KERN_ERR "orinoco_pci: Busy timeout\n");
179                 return -ETIMEDOUT;
180         }
181         printk(KERN_NOTICE "pci_cor : reg = 0x%X - %lX - %lX\n", reg, timeout, jiffies);
182
183         return 0;
184 }
185
186 /*
187  * Initialise a card. Mostly similar to PLX code.
188  */
189 static int orinoco_pci_init_one(struct pci_dev *pdev,
190                                 const struct pci_device_id *ent)
191 {
192         int err = 0;
193         unsigned long pci_iorange;
194         u16 *pci_ioaddr = NULL;
195         unsigned long pci_iolen;
196         struct orinoco_private *priv = NULL;
197         struct net_device *dev = NULL;
198
199         err = pci_enable_device(pdev);
200         if (err)
201                 return -EIO;
202
203         /* Resource 0 is mapped to the hermes registers */
204         pci_iorange = pci_resource_start(pdev, 0);
205         pci_iolen = pci_resource_len(pdev, 0);
206         pci_ioaddr = ioremap(pci_iorange, pci_iolen);
207         if (! pci_iorange)
208                 goto fail;
209
210         /* Usual setup of structures */
211         dev = alloc_orinocodev(0, NULL);
212         if (! dev) {
213                 err = -ENOMEM;
214                 goto fail;
215         }
216         priv = dev->priv;
217
218         dev->base_addr = (unsigned long) pci_ioaddr;
219         dev->mem_start = pci_iorange;
220         dev->mem_end = pci_iorange + pci_iolen - 1;
221
222         SET_MODULE_OWNER(dev);
223         SET_NETDEV_DEV(dev, &pdev->dev);
224
225         printk(KERN_DEBUG
226                "Detected Orinoco/Prism2 PCI device at %s, mem:0x%lX to 0x%lX -> 0x%p, irq:%d\n",
227                pci_name(pdev), dev->mem_start, dev->mem_end, pci_ioaddr, pdev->irq);
228
229         hermes_struct_init(&priv->hw, dev->base_addr,
230                            HERMES_MEM, HERMES_32BIT_REGSPACING);
231         pci_set_drvdata(pdev, dev);
232
233         err = request_irq(pdev->irq, orinoco_interrupt, SA_SHIRQ,
234                           dev->name, dev);
235         if (err) {
236                 printk(KERN_ERR "orinoco_pci: Error allocating IRQ %d.\n",
237                        pdev->irq);
238                 err = -EBUSY;
239                 goto fail;
240         }
241         dev->irq = pdev->irq;
242         /* Perform a COR reset to start the card */
243         if(orinoco_pci_cor_reset(priv) != 0) {
244                 printk(KERN_ERR "%s: Failed to start the card\n", dev->name);
245                 err = -ETIMEDOUT;
246                 goto fail;
247         }
248
249         /* Override the normal firmware detection - the Prism 2.5 PCI
250          * cards look like Lucent firmware but are actually Intersil */
251         priv->firmware_type = FIRMWARE_TYPE_INTERSIL;
252
253         err = register_netdev(dev);
254         if (err) {
255                 printk(KERN_ERR "%s: Failed to register net device\n", dev->name);
256                 goto fail;
257         }
258
259         return 0;               /* succeeded */
260  fail:
261         if (dev) {
262                 if (dev->irq)
263                         free_irq(dev->irq, dev);
264
265                 free_netdev(dev);
266         }
267
268         if (pci_ioaddr)
269                 iounmap(pci_ioaddr);
270
271         pci_disable_device(pdev);
272
273         return err;
274 }
275
276 static void __devexit orinoco_pci_remove_one(struct pci_dev *pdev)
277 {
278         struct net_device *dev = pci_get_drvdata(pdev);
279         struct orinoco_private *priv = dev->priv;
280
281         unregister_netdev(dev);
282
283         if (dev->irq)
284                 free_irq(dev->irq, dev);
285
286         if (priv->hw.iobase)
287                 iounmap((unsigned char *) priv->hw.iobase);
288
289         pci_set_drvdata(pdev, NULL);
290         free_netdev(dev);
291
292         pci_disable_device(pdev);
293 }
294
295 static int orinoco_pci_suspend(struct pci_dev *pdev, u32 state)
296 {
297         struct net_device *dev = pci_get_drvdata(pdev);
298         struct orinoco_private *priv = dev->priv;
299         unsigned long flags;
300         int err;
301         
302         printk(KERN_DEBUG "%s: Orinoco-PCI entering sleep mode (state=%d)\n",
303                dev->name, state);
304
305         err = orinoco_lock(priv, &flags);
306         if (err) {
307                 printk(KERN_ERR "%s: hw_unavailable on orinoco_pci_suspend\n",
308                        dev->name);
309                 return err;
310         }
311
312         err = __orinoco_down(dev);
313         if (err)
314                 printk(KERN_WARNING "%s: orinoco_pci_suspend(): Error %d downing interface\n",
315                        dev->name, err);
316         
317         netif_device_detach(dev);
318
319         priv->hw_unavailable++;
320         
321         orinoco_unlock(priv, &flags);
322
323         return 0;
324 }
325
326 static int orinoco_pci_resume(struct pci_dev *pdev)
327 {
328         struct net_device *dev = pci_get_drvdata(pdev);
329         struct orinoco_private *priv = dev->priv;
330         unsigned long flags;
331         int err;
332
333         printk(KERN_DEBUG "%s: Orinoco-PCI waking up\n", dev->name);
334
335         err = orinoco_reinit_firmware(dev);
336         if (err) {
337                 printk(KERN_ERR "%s: Error %d re-initializing firmware on orinoco_pci_resume()\n",
338                        dev->name, err);
339                 return err;
340         }
341
342         spin_lock_irqsave(&priv->lock, flags);
343
344         netif_device_attach(dev);
345
346         priv->hw_unavailable--;
347
348         if (priv->open && (! priv->hw_unavailable)) {
349                 err = __orinoco_up(dev);
350                 if (err)
351                         printk(KERN_ERR "%s: Error %d restarting card on orinoco_pci_resume()\n",
352                                dev->name, err);
353         }
354         
355         spin_unlock_irqrestore(&priv->lock, flags);
356
357         return 0;
358 }
359
360 static struct pci_device_id orinoco_pci_pci_id_table[] = {
361         {0x1260, 0x3872, PCI_ANY_ID, PCI_ANY_ID,},
362         {0x1260, 0x3873, PCI_ANY_ID, PCI_ANY_ID,},
363         {0,},
364 };
365
366 MODULE_DEVICE_TABLE(pci, orinoco_pci_pci_id_table);
367
368 static struct pci_driver orinoco_pci_driver = {
369         .name           = "orinoco_pci",
370         .id_table       = orinoco_pci_pci_id_table,
371         .probe          = orinoco_pci_init_one,
372         .remove         = __devexit_p(orinoco_pci_remove_one),
373         .suspend        = orinoco_pci_suspend,
374         .resume         = orinoco_pci_resume,
375 };
376
377 static char version[] __initdata = "orinoco_pci.c 0.13e (David Gibson <hermes@gibson.dropbear.id.au> & Jean Tourrilhes <jt@hpl.hp.com>)";
378 MODULE_AUTHOR("David Gibson <hermes@gibson.dropbear.id.au>");
379 MODULE_DESCRIPTION("Driver for wireless LAN cards using direct PCI interface");
380 MODULE_LICENSE("Dual MPL/GPL");
381
382 static int __init orinoco_pci_init(void)
383 {
384         printk(KERN_DEBUG "%s\n", version);
385         return pci_module_init(&orinoco_pci_driver);
386 }
387
388 extern void __exit orinoco_pci_exit(void)
389 {
390         pci_unregister_driver(&orinoco_pci_driver);
391 }
392
393 module_init(orinoco_pci_init);
394 module_exit(orinoco_pci_exit);
395
396 /*
397  * Local variables:
398  *  c-indent-level: 8
399  *  c-basic-offset: 8
400  *  tab-width: 8
401  * End:
402  */