commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.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
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                 kfree(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         if (! dev)
281                 BUG();
282
283         unregister_netdev(dev);
284
285         if (dev->irq)
286                 free_irq(dev->irq, dev);
287
288         if (priv->hw.iobase)
289                 iounmap((unsigned char *) priv->hw.iobase);
290
291         pci_set_drvdata(pdev, NULL);
292         kfree(dev);
293
294         pci_disable_device(pdev);
295 }
296
297 static int orinoco_pci_suspend(struct pci_dev *pdev, u32 state)
298 {
299         struct net_device *dev = pci_get_drvdata(pdev);
300         struct orinoco_private *priv = dev->priv;
301         unsigned long flags;
302         int err;
303         
304         printk(KERN_DEBUG "%s: Orinoco-PCI entering sleep mode (state=%d)\n",
305                dev->name, state);
306
307         err = orinoco_lock(priv, &flags);
308         if (err) {
309                 printk(KERN_ERR "%s: hw_unavailable on orinoco_pci_suspend\n",
310                        dev->name);
311                 return err;
312         }
313
314         err = __orinoco_down(dev);
315         if (err)
316                 printk(KERN_WARNING "%s: orinoco_pci_suspend(): Error %d downing interface\n",
317                        dev->name, err);
318         
319         netif_device_detach(dev);
320
321         priv->hw_unavailable++;
322         
323         orinoco_unlock(priv, &flags);
324
325         return 0;
326 }
327
328 static int orinoco_pci_resume(struct pci_dev *pdev)
329 {
330         struct net_device *dev = pci_get_drvdata(pdev);
331         struct orinoco_private *priv = dev->priv;
332         unsigned long flags;
333         int err;
334
335         printk(KERN_DEBUG "%s: Orinoco-PCI waking up\n", dev->name);
336
337         err = orinoco_reinit_firmware(dev);
338         if (err) {
339                 printk(KERN_ERR "%s: Error %d re-initializing firmware on orinoco_pci_resume()\n",
340                        dev->name, err);
341                 return err;
342         }
343
344         spin_lock_irqsave(&priv->lock, flags);
345
346         netif_device_attach(dev);
347
348         priv->hw_unavailable--;
349
350         if (priv->open && (! priv->hw_unavailable)) {
351                 err = __orinoco_up(dev);
352                 if (err)
353                         printk(KERN_ERR "%s: Error %d restarting card on orinoco_pci_resume()\n",
354                                dev->name, err);
355         }
356         
357         spin_unlock_irqrestore(&priv->lock, flags);
358
359         return 0;
360 }
361
362 static struct pci_device_id orinoco_pci_pci_id_table[] = {
363         {0x1260, 0x3873, PCI_ANY_ID, PCI_ANY_ID,},
364         {0,},
365 };
366
367 MODULE_DEVICE_TABLE(pci, orinoco_pci_pci_id_table);
368
369 static struct pci_driver orinoco_pci_driver = {
370         .name           = "orinoco_pci",
371         .id_table       = orinoco_pci_pci_id_table,
372         .probe          = orinoco_pci_init_one,
373         .remove         = __devexit_p(orinoco_pci_remove_one),
374         .suspend        = orinoco_pci_suspend,
375         .resume         = orinoco_pci_resume,
376 };
377
378 static char version[] __initdata = "orinoco_pci.c 0.13e (David Gibson <hermes@gibson.dropbear.id.au> & Jean Tourrilhes <jt@hpl.hp.com>)";
379 MODULE_AUTHOR("David Gibson <hermes@gibson.dropbear.id.au>");
380 MODULE_DESCRIPTION("Driver for wireless LAN cards using direct PCI interface");
381 MODULE_LICENSE("Dual MPL/GPL");
382
383 static int __init orinoco_pci_init(void)
384 {
385         printk(KERN_DEBUG "%s\n", version);
386         return pci_module_init(&orinoco_pci_driver);
387 }
388
389 extern void __exit orinoco_pci_exit(void)
390 {
391         pci_unregister_driver(&orinoco_pci_driver);
392 }
393
394 module_init(orinoco_pci_init);
395 module_exit(orinoco_pci_exit);
396
397 /*
398  * Local variables:
399  *  c-indent-level: 8
400  *  c-basic-offset: 8
401  *  tab-width: 8
402  * End:
403  */