vserver 1.9.3
[linux-2.6.git] / drivers / net / wireless / orinoco_plx.c
1 /* orinoco_plx.c
2  *
3  * Driver for Prism II devices which would usually be driven by orinoco_cs,
4  * but are connected to the PCI bus by a PLX9052.
5  *
6  * Current maintainers (as of 29 September 2003) are:
7  *      Pavel Roskin <proski AT gnu.org>
8  * and  David Gibson <hermes AT gibson.dropbear.id.au>
9  *
10  * (C) Copyright David Gibson, IBM Corp. 2001-2003.
11  * Copyright (C) 2001 Daniel Barlow
12  *
13  * The contents of this file are subject to the Mozilla Public License
14  * Version 1.1 (the "License"); you may not use this file except in
15  * compliance with the License. You may obtain a copy of the License
16  * at http://www.mozilla.org/MPL/
17  *
18  * Software distributed under the License is distributed on an "AS IS"
19  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20  * the License for the specific language governing rights and
21  * limitations under the License.
22  *
23  * Alternatively, the contents of this file may be used under the
24  * terms of the GNU General Public License version 2 (the "GPL"), in
25  * which case the provisions of the GPL are applicable instead of the
26  * above.  If you wish to allow the use of your version of this file
27  * only under the terms of the GPL and not to allow others to use your
28  * version of this file under the MPL, indicate your decision by
29  * deleting the provisions above and replace them with the notice and
30  * other provisions required by the GPL.  If you do not delete the
31  * provisions above, a recipient may use your version of this file
32  * under either the MPL or the GPL.
33
34  * Caution: this is experimental and probably buggy.  For success and
35  * failure reports for different cards and adaptors, see
36  * orinoco_plx_pci_id_table near the end of the file.  If you have a
37  * card we don't have the PCI id for, and looks like it should work,
38  * drop me mail with the id and "it works"/"it doesn't work".
39  *
40  * Note: if everything gets detected fine but it doesn't actually send
41  * or receive packets, your first port of call should probably be to
42  * try newer firmware in the card.  Especially if you're doing Ad-Hoc
43  * modes.
44  *
45  * The actual driving is done by orinoco.c, this is just resource
46  * allocation stuff.  The explanation below is courtesy of Ryan Niemi
47  * on the linux-wlan-ng list at
48  * http://archives.neohapsis.com/archives/dev/linux-wlan/2001-q1/0026.html
49  *
50  * The PLX9052-based cards (WL11000 and several others) are a
51  * different beast than the usual PCMCIA-based PRISM2 configuration
52  * expected by wlan-ng.  Here's the general details on how the WL11000
53  * PCI adapter works:
54  *
55  * - Two PCI I/O address spaces, one 0x80 long which contains the
56  * PLX9052 registers, and one that's 0x40 long mapped to the PCMCIA
57  * slot I/O address space.
58  *
59  * - One PCI memory address space, mapped to the PCMCIA memory space
60  * (containing the CIS).
61  *
62  * After identifying the I/O and memory space, you can read through
63  * the memory space to confirm the CIS's device ID or manufacturer ID
64  * to make sure it's the expected card.  qKeep in mind that the PCMCIA
65  * spec specifies the CIS as the lower 8 bits of each word read from
66  * the CIS, so to read the bytes of the CIS, read every other byte
67  * (0,2,4,...). Passing that test, you need to enable the I/O address
68  * space on the PCMCIA card via the PCMCIA COR register. This is the
69  * first byte following the CIS. In my case (which may not have any
70  * relation to what's on the PRISM2 cards), COR was at offset 0x800
71  * within the PCI memory space. Write 0x41 to the COR register to
72  * enable I/O mode and to select level triggered interrupts. To
73  * confirm you actually succeeded, read the COR register back and make
74  * sure it actually got set to 0x41, incase you have an unexpected
75  * card inserted.
76  *
77  * Following that, you can treat the second PCI I/O address space (the
78  * one that's not 0x80 in length) as the PCMCIA I/O space.
79  *
80  * Note that in the Eumitcom's source for their drivers, they register
81  * the interrupt as edge triggered when registering it with the
82  * Windows kernel. I don't recall how to register edge triggered on
83  * Linux (if it can be done at all). But in some experimentation, I
84  * don't see much operational difference between using either
85  * interrupt mode. Don't mess with the interrupt mode in the COR
86  * register though, as the PLX9052 wants level triggers with the way
87  * the serial EEPROM configures it on the WL11000.
88  *
89  * There's some other little quirks related to timing that I bumped
90  * into, but I don't recall right now. Also, there's two variants of
91  * the WL11000 I've seen, revision A1 and T2. These seem to differ
92  * slightly in the timings configured in the wait-state generator in
93  * the PLX9052. There have also been some comments from Eumitcom that
94  * cards shouldn't be hot swapped, apparently due to risk of cooking
95  * the PLX9052. I'm unsure why they believe this, as I can't see
96  * anything in the design that would really cause a problem, except
97  * for crashing drivers not written to expect it. And having developed
98  * drivers for the WL11000, I'd say it's quite tricky to write code
99  * that will successfully deal with a hot unplug. Very odd things
100  * happen on the I/O side of things. But anyway, be warned. Despite
101  * that, I've hot-swapped a number of times during debugging and
102  * driver development for various reasons (stuck WAIT# line after the
103  * radio card's firmware locks up).
104  *
105  * Hope this is enough info for someone to add PLX9052 support to the
106  * wlan-ng card. In the case of the WL11000, the PCI ID's are
107  * 0x1639/0x0200, with matching subsystem ID's. Other PLX9052-based
108  * manufacturers other than Eumitcom (or on cards other than the
109  * WL11000) may have different PCI ID's.
110  *
111  * If anyone needs any more specific info, let me know. I haven't had
112  * time to implement support myself yet, and with the way things are
113  * going, might not have time for a while..
114  */
115
116 #define DRIVER_NAME "orinoco_plx"
117 #define PFX DRIVER_NAME ": "
118
119 #include <linux/config.h>
120
121 #include <linux/module.h>
122 #include <linux/kernel.h>
123 #include <linux/init.h>
124 #include <linux/sched.h>
125 #include <linux/ptrace.h>
126 #include <linux/slab.h>
127 #include <linux/string.h>
128 #include <linux/timer.h>
129 #include <linux/ioport.h>
130 #include <asm/uaccess.h>
131 #include <asm/io.h>
132 #include <asm/system.h>
133 #include <linux/netdevice.h>
134 #include <linux/if_arp.h>
135 #include <linux/etherdevice.h>
136 #include <linux/list.h>
137 #include <linux/pci.h>
138 #include <linux/fcntl.h>
139
140 #include <pcmcia/cisreg.h>
141
142 #include "hermes.h"
143 #include "orinoco.h"
144
145 #define COR_OFFSET      (0x3e0/2) /* COR attribute offset of Prism2 PC card */
146 #define COR_VALUE       (COR_LEVEL_REQ | COR_FUNC_ENA) /* Enable PC card with interrupt in level trigger */
147
148 #define PLX_INTCSR              0x4c /* Interrupt Control & Status Register */
149 #define PLX_INTCSR_INTEN        (1<<6) /* Interrupt Enable bit */
150
151 static const u16 cis_magic[] = {
152         0x0001, 0x0003, 0x0000, 0x0000, 0x00ff, 0x0017, 0x0004, 0x0067
153 };
154
155 static int orinoco_plx_init_one(struct pci_dev *pdev,
156                                 const struct pci_device_id *ent)
157 {
158         int err = 0;
159         u16 *attr_mem = NULL;
160         u32 reg, addr;
161         struct orinoco_private *priv = NULL;
162         unsigned long pccard_ioaddr = 0;
163         unsigned long pccard_iolen = 0;
164         struct net_device *dev = NULL;
165         int i;
166
167         err = pci_enable_device(pdev);
168         if (err)
169                 return -EIO;
170
171         /* Resource 2 is mapped to the PCMCIA space */
172         attr_mem = ioremap(pci_resource_start(pdev, 2), PAGE_SIZE);
173         if (! attr_mem)
174                 goto fail;
175
176         printk(KERN_DEBUG "orinoco_plx: CIS: ");
177         for (i = 0; i < 16; i++) {
178                 printk("%02X:", (int)attr_mem[i]);
179         }
180         printk("\n");
181
182         /* Verify whether PC card is present */
183         /* FIXME: we probably need to be smarted about this */
184         if (memcmp(attr_mem, cis_magic, sizeof(cis_magic)) != 0) {
185                 printk(KERN_ERR "orinoco_plx: The CIS value of Prism2 PC card is invalid.\n");
186                 err = -EIO;
187                 goto fail;
188         }
189
190         /* PCMCIA COR is the first byte following CIS: this write should
191          * enable I/O mode and select level-triggered interrupts */
192         attr_mem[COR_OFFSET] = COR_VALUE;
193         mdelay(1);
194         reg = attr_mem[COR_OFFSET];
195         if (reg != COR_VALUE) {
196                 printk(KERN_ERR "orinoco_plx: Error setting COR value (reg=%x)\n", reg);
197                 goto fail;
198         }                       
199
200         iounmap(attr_mem);
201         attr_mem = NULL; /* done with this now, it seems */
202
203         /* bjoern: We need to tell the card to enable interrupts, in
204            case the serial eprom didn't do this already. See the
205            PLX9052 data book, p8-1 and 8-24 for reference. */
206         addr = pci_resource_start(pdev, 1);
207         reg = 0;
208         reg = inl(addr+PLX_INTCSR);
209         if (reg & PLX_INTCSR_INTEN)
210                 printk(KERN_DEBUG "orinoco_plx: "
211                        "Local Interrupt already enabled\n");
212         else {
213                 reg |= PLX_INTCSR_INTEN;
214                 outl(reg, addr+PLX_INTCSR);
215                 reg = inl(addr+PLX_INTCSR);
216                 if(!(reg & PLX_INTCSR_INTEN)) {
217                         printk(KERN_ERR "orinoco_plx: "
218                                "Couldn't enable Local Interrupts\n");
219                         goto fail;
220                 }
221         }
222
223         /* and 3 to the PCMCIA slot I/O address space */
224         pccard_ioaddr = pci_resource_start(pdev, 3);
225         pccard_iolen = pci_resource_len(pdev, 3);
226         if (! request_region(pccard_ioaddr, pccard_iolen, DRIVER_NAME)) {
227                 printk(KERN_ERR "orinoco_plx: I/O resource 0x%lx @ 0x%lx busy\n",
228                        pccard_iolen, pccard_ioaddr);
229                 pccard_ioaddr = 0;
230                 err = -EBUSY;
231                 goto fail;
232         }
233
234         /* Allocate network device */
235         dev = alloc_orinocodev(0, NULL);
236         if (! dev) {
237                 err = -ENOMEM;
238                 goto fail;
239         }
240
241         priv = netdev_priv(dev);
242         dev->base_addr = pccard_ioaddr;
243         SET_MODULE_OWNER(dev);
244         SET_NETDEV_DEV(dev, &pdev->dev);
245
246         printk(KERN_DEBUG PFX "Detected Orinoco/Prism2 PLX device "
247                "at %s irq:%d, io addr:0x%lx\n", pci_name(pdev), pdev->irq,
248                pccard_ioaddr);
249
250         hermes_struct_init(&(priv->hw), dev->base_addr, HERMES_IO,
251                            HERMES_16BIT_REGSPACING);
252         pci_set_drvdata(pdev, dev);
253
254         err = request_irq(pdev->irq, orinoco_interrupt, SA_SHIRQ,
255                           dev->name, dev);
256         if (err) {
257                 printk(KERN_ERR PFX "Error allocating IRQ %d.\n", pdev->irq);
258                 err = -EBUSY;
259                 goto fail;
260         }
261         dev->irq = pdev->irq;
262
263         err = register_netdev(dev);
264         if (err)
265                 goto fail;
266
267         return 0;
268
269  fail:
270         printk(KERN_DEBUG PFX "init_one(), FAIL!\n");
271
272         if (dev) {
273                 if (dev->irq)
274                         free_irq(dev->irq, dev);
275                 
276                 free_netdev(dev);
277         }
278
279         if (pccard_ioaddr)
280                 release_region(pccard_ioaddr, pccard_iolen);
281
282         if (attr_mem)
283                 iounmap(attr_mem);
284
285         pci_disable_device(pdev);
286
287         return err;
288 }
289
290 static void __devexit orinoco_plx_remove_one(struct pci_dev *pdev)
291 {
292         struct net_device *dev = pci_get_drvdata(pdev);
293
294         BUG_ON(! dev);
295
296         unregister_netdev(dev);
297                 
298         if (dev->irq)
299                 free_irq(dev->irq, dev);
300                 
301         pci_set_drvdata(pdev, NULL);
302
303         free_netdev(dev);
304
305         release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
306
307         pci_disable_device(pdev);
308 }
309
310
311 static struct pci_device_id orinoco_plx_pci_id_table[] = {
312         {0x111a, 0x1023, PCI_ANY_ID, PCI_ANY_ID,},      /* Siemens SpeedStream SS1023 */
313         {0x1385, 0x4100, PCI_ANY_ID, PCI_ANY_ID,},      /* Netgear MA301 */
314         {0x15e8, 0x0130, PCI_ANY_ID, PCI_ANY_ID,},      /* Correga  - does this work? */
315         {0x1638, 0x1100, PCI_ANY_ID, PCI_ANY_ID,},      /* SMC EZConnect SMC2602W,
316                                                            Eumitcom PCI WL11000,
317                                                            Addtron AWA-100 */
318         {0x16ab, 0x1100, PCI_ANY_ID, PCI_ANY_ID,},      /* Global Sun Tech GL24110P */
319         {0x16ab, 0x1101, PCI_ANY_ID, PCI_ANY_ID,},      /* Reported working, but unknown */
320         {0x16ab, 0x1102, PCI_ANY_ID, PCI_ANY_ID,},      /* Linksys WDT11 */
321         {0x16ec, 0x3685, PCI_ANY_ID, PCI_ANY_ID,},      /* USR 2415 */
322         {0xec80, 0xec00, PCI_ANY_ID, PCI_ANY_ID,},      /* Belkin F5D6000 tested by
323                                                            Brendan W. McAdams <rit AT jacked-in.org> */
324         {0x10b7, 0x7770, PCI_ANY_ID, PCI_ANY_ID,},      /* 3Com AirConnect PCI tested by
325                                                            Damien Persohn <damien AT persohn.net> */
326         {0,},
327 };
328
329 MODULE_DEVICE_TABLE(pci, orinoco_plx_pci_id_table);
330
331 static struct pci_driver orinoco_plx_driver = {
332         .name           = DRIVER_NAME,
333         .id_table       = orinoco_plx_pci_id_table,
334         .probe          = orinoco_plx_init_one,
335         .remove         = __devexit_p(orinoco_plx_remove_one),
336 };
337
338 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
339         " (Pavel Roskin <proski@gnu.org>,"
340         " David Gibson <hermes@gibson.dropbear.id.au>,"
341         " Daniel Barlow <dan@telent.net>)";
342 MODULE_AUTHOR("Daniel Barlow <dan@telent.net>");
343 MODULE_DESCRIPTION("Driver for wireless LAN cards using the PLX9052 PCI bridge");
344 MODULE_LICENSE("Dual MPL/GPL");
345
346 static int __init orinoco_plx_init(void)
347 {
348         printk(KERN_DEBUG "%s\n", version);
349         return pci_module_init(&orinoco_plx_driver);
350 }
351
352 static void __exit orinoco_plx_exit(void)
353 {
354         pci_unregister_driver(&orinoco_plx_driver);
355         current->state = TASK_UNINTERRUPTIBLE;
356         schedule_timeout(HZ);
357 }
358
359 module_init(orinoco_plx_init);
360 module_exit(orinoco_plx_exit);
361
362 /*
363  * Local variables:
364  *  c-indent-level: 8
365  *  c-basic-offset: 8
366  *  tab-width: 8
367  * End:
368  */