patch-2_6_7-vs1_9_1_12
[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/fcntl.h>
106
107 #include <asm/uaccess.h>
108 #include <asm/io.h>
109 #include <asm/system.h>
110
111 #include "hermes.h"
112 #include "orinoco.h"
113
114 /* All the magic there is from wlan-ng */
115 /* Magic offset of the reset register of the PCI card */
116 #define HERMES_PCI_COR          (0x26)
117 /* Magic bitmask to reset the card */
118 #define HERMES_PCI_COR_MASK     (0x0080)
119 /* Magic timeouts for doing the reset.
120  * Those times are straight from wlan-ng, and it is claimed that they
121  * are necessary. Alan will kill me. Take your time and grab a coffee. */
122 #define HERMES_PCI_COR_ONT      (250)           /* ms */
123 #define HERMES_PCI_COR_OFFT     (500)           /* ms */
124 #define HERMES_PCI_COR_BUSYT    (500)           /* ms */
125
126 /*
127  * Do a soft reset of the PCI card using the Configuration Option Register
128  * We need this to get going...
129  * This is the part of the code that is strongly inspired from wlan-ng
130  *
131  * Note : This code is done with irq enabled. This mean that many
132  * interrupts will occur while we are there. This is why we use the
133  * jiffies to regulate time instead of a straight mdelay(). Usually we
134  * need only around 245 iteration of the loop to do 250 ms delay.
135  *
136  * Note bis : Don't try to access HERMES_CMD during the reset phase.
137  * It just won't work !
138  */
139 static int
140 orinoco_pci_cor_reset(struct orinoco_private *priv)
141 {
142         hermes_t *hw = &priv->hw;
143         unsigned long   timeout;
144         u16     reg;
145
146         /* Assert the reset until the card notice */
147         hermes_write_regn(hw, PCI_COR, HERMES_PCI_COR_MASK);
148         printk(KERN_NOTICE "Reset done");
149         timeout = jiffies + (HERMES_PCI_COR_ONT * HZ / 1000);
150         while(time_before(jiffies, timeout)) {
151                 printk(".");
152                 mdelay(1);
153         }
154         printk(";\n");
155         //mdelay(HERMES_PCI_COR_ONT);
156
157         /* Give time for the card to recover from this hard effort */
158         hermes_write_regn(hw, PCI_COR, 0x0000);
159         printk(KERN_NOTICE "Clear Reset");
160         timeout = jiffies + (HERMES_PCI_COR_OFFT * HZ / 1000);
161         while(time_before(jiffies, timeout)) {
162                 printk(".");
163                 mdelay(1);
164         }
165         printk(";\n");
166         //mdelay(HERMES_PCI_COR_OFFT);
167
168         /* The card is ready when it's no longer busy */
169         timeout = jiffies + (HERMES_PCI_COR_BUSYT * HZ / 1000);
170         reg = hermes_read_regn(hw, CMD);
171         while (time_before(jiffies, timeout) && (reg & HERMES_CMD_BUSY)) {
172                 mdelay(1);
173                 reg = hermes_read_regn(hw, CMD);
174         }
175         /* Did we timeout ? */
176         if(time_after_eq(jiffies, timeout)) {
177                 printk(KERN_ERR "orinoco_pci: Busy timeout\n");
178                 return -ETIMEDOUT;
179         }
180         printk(KERN_NOTICE "pci_cor : reg = 0x%X - %lX - %lX\n", reg, timeout, jiffies);
181
182         return 0;
183 }
184
185 /*
186  * Initialise a card. Mostly similar to PLX code.
187  */
188 static int orinoco_pci_init_one(struct pci_dev *pdev,
189                                 const struct pci_device_id *ent)
190 {
191         int err = 0;
192         unsigned long pci_iorange;
193         u16 *pci_ioaddr = NULL;
194         unsigned long pci_iolen;
195         struct orinoco_private *priv = NULL;
196         struct net_device *dev = NULL;
197
198         err = pci_enable_device(pdev);
199         if (err)
200                 return -EIO;
201
202         /* Resource 0 is mapped to the hermes registers */
203         pci_iorange = pci_resource_start(pdev, 0);
204         pci_iolen = pci_resource_len(pdev, 0);
205         pci_ioaddr = ioremap(pci_iorange, pci_iolen);
206         if (! pci_iorange)
207                 goto fail;
208
209         /* Usual setup of structures */
210         dev = alloc_orinocodev(0, NULL);
211         if (! dev) {
212                 err = -ENOMEM;
213                 goto fail;
214         }
215         priv = dev->priv;
216
217         dev->base_addr = (unsigned long) pci_ioaddr;
218         dev->mem_start = pci_iorange;
219         dev->mem_end = pci_iorange + pci_iolen - 1;
220
221         SET_MODULE_OWNER(dev);
222         SET_NETDEV_DEV(dev, &pdev->dev);
223
224         printk(KERN_DEBUG
225                "Detected Orinoco/Prism2 PCI device at %s, mem:0x%lX to 0x%lX -> 0x%p, irq:%d\n",
226                pci_name(pdev), dev->mem_start, dev->mem_end, pci_ioaddr, pdev->irq);
227
228         hermes_struct_init(&priv->hw, dev->base_addr,
229                            HERMES_MEM, HERMES_32BIT_REGSPACING);
230         pci_set_drvdata(pdev, dev);
231
232         err = request_irq(pdev->irq, orinoco_interrupt, SA_SHIRQ,
233                           dev->name, dev);
234         if (err) {
235                 printk(KERN_ERR "orinoco_pci: Error allocating IRQ %d.\n",
236                        pdev->irq);
237                 err = -EBUSY;
238                 goto fail;
239         }
240         dev->irq = pdev->irq;
241         /* Perform a COR reset to start the card */
242         if(orinoco_pci_cor_reset(priv) != 0) {
243                 printk(KERN_ERR "%s: Failed to start the card\n", dev->name);
244                 err = -ETIMEDOUT;
245                 goto fail;
246         }
247
248         /* Override the normal firmware detection - the Prism 2.5 PCI
249          * cards look like Lucent firmware but are actually Intersil */
250         priv->firmware_type = FIRMWARE_TYPE_INTERSIL;
251
252         err = register_netdev(dev);
253         if (err) {
254                 printk(KERN_ERR "%s: Failed to register net device\n", dev->name);
255                 goto fail;
256         }
257
258         return 0;               /* succeeded */
259  fail:
260         if (dev) {
261                 if (dev->irq)
262                         free_irq(dev->irq, dev);
263
264                 free_netdev(dev);
265         }
266
267         if (pci_ioaddr)
268                 iounmap(pci_ioaddr);
269
270         pci_disable_device(pdev);
271
272         return err;
273 }
274
275 static void __devexit orinoco_pci_remove_one(struct pci_dev *pdev)
276 {
277         struct net_device *dev = pci_get_drvdata(pdev);
278         struct orinoco_private *priv = dev->priv;
279
280         unregister_netdev(dev);
281
282         if (dev->irq)
283                 free_irq(dev->irq, dev);
284
285         if (priv->hw.iobase)
286                 iounmap((unsigned char *) priv->hw.iobase);
287
288         pci_set_drvdata(pdev, NULL);
289         free_netdev(dev);
290
291         pci_disable_device(pdev);
292 }
293
294 static int orinoco_pci_suspend(struct pci_dev *pdev, u32 state)
295 {
296         struct net_device *dev = pci_get_drvdata(pdev);
297         struct orinoco_private *priv = dev->priv;
298         unsigned long flags;
299         int err;
300         
301         printk(KERN_DEBUG "%s: Orinoco-PCI entering sleep mode (state=%d)\n",
302                dev->name, state);
303
304         err = orinoco_lock(priv, &flags);
305         if (err) {
306                 printk(KERN_ERR "%s: hw_unavailable on orinoco_pci_suspend\n",
307                        dev->name);
308                 return err;
309         }
310
311         err = __orinoco_down(dev);
312         if (err)
313                 printk(KERN_WARNING "%s: orinoco_pci_suspend(): Error %d downing interface\n",
314                        dev->name, err);
315         
316         netif_device_detach(dev);
317
318         priv->hw_unavailable++;
319         
320         orinoco_unlock(priv, &flags);
321
322         return 0;
323 }
324
325 static int orinoco_pci_resume(struct pci_dev *pdev)
326 {
327         struct net_device *dev = pci_get_drvdata(pdev);
328         struct orinoco_private *priv = dev->priv;
329         unsigned long flags;
330         int err;
331
332         printk(KERN_DEBUG "%s: Orinoco-PCI waking up\n", dev->name);
333
334         err = orinoco_reinit_firmware(dev);
335         if (err) {
336                 printk(KERN_ERR "%s: Error %d re-initializing firmware on orinoco_pci_resume()\n",
337                        dev->name, err);
338                 return err;
339         }
340
341         spin_lock_irqsave(&priv->lock, flags);
342
343         netif_device_attach(dev);
344
345         priv->hw_unavailable--;
346
347         if (priv->open && (! priv->hw_unavailable)) {
348                 err = __orinoco_up(dev);
349                 if (err)
350                         printk(KERN_ERR "%s: Error %d restarting card on orinoco_pci_resume()\n",
351                                dev->name, err);
352         }
353         
354         spin_unlock_irqrestore(&priv->lock, flags);
355
356         return 0;
357 }
358
359 static struct pci_device_id orinoco_pci_pci_id_table[] = {
360         {0x1260, 0x3872, PCI_ANY_ID, PCI_ANY_ID,},
361         {0x1260, 0x3873, PCI_ANY_ID, PCI_ANY_ID,},
362         {0,},
363 };
364
365 MODULE_DEVICE_TABLE(pci, orinoco_pci_pci_id_table);
366
367 static struct pci_driver orinoco_pci_driver = {
368         .name           = "orinoco_pci",
369         .id_table       = orinoco_pci_pci_id_table,
370         .probe          = orinoco_pci_init_one,
371         .remove         = __devexit_p(orinoco_pci_remove_one),
372         .suspend        = orinoco_pci_suspend,
373         .resume         = orinoco_pci_resume,
374 };
375
376 static char version[] __initdata = "orinoco_pci.c 0.13e (David Gibson <hermes@gibson.dropbear.id.au> & Jean Tourrilhes <jt@hpl.hp.com>)";
377 MODULE_AUTHOR("David Gibson <hermes@gibson.dropbear.id.au>");
378 MODULE_DESCRIPTION("Driver for wireless LAN cards using direct PCI interface");
379 MODULE_LICENSE("Dual MPL/GPL");
380
381 static int __init orinoco_pci_init(void)
382 {
383         printk(KERN_DEBUG "%s\n", version);
384         return pci_module_init(&orinoco_pci_driver);
385 }
386
387 extern void __exit orinoco_pci_exit(void)
388 {
389         pci_unregister_driver(&orinoco_pci_driver);
390 }
391
392 module_init(orinoco_pci_init);
393 module_exit(orinoco_pci_exit);
394
395 /*
396  * Local variables:
397  *  c-indent-level: 8
398  *  c-basic-offset: 8
399  *  tab-width: 8
400  * End:
401  */