Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / net / wireless / airport.c
1 /* airport.c
2  *
3  * A driver for "Hermes" chipset based Apple Airport wireless
4  * card.
5  *
6  * Copyright notice & release notes in file orinoco.c
7  * 
8  * Note specific to airport stub:
9  * 
10  *  0.05 : first version of the new split driver
11  *  0.06 : fix possible hang on powerup, add sleep support
12  */
13
14 #define DRIVER_NAME "airport"
15 #define PFX DRIVER_NAME ": "
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <asm/pmac_feature.h>
23
24 #include "orinoco.h"
25
26 #define AIRPORT_IO_LEN  (0x1000)        /* one page */
27
28 struct airport {
29         struct macio_dev *mdev;
30         void __iomem *vaddr;
31         int irq_requested;
32         int ndev_registered;
33 };
34
35 static int
36 airport_suspend(struct macio_dev *mdev, pm_message_t state)
37 {
38         struct net_device *dev = dev_get_drvdata(&mdev->ofdev.dev);
39         struct orinoco_private *priv = netdev_priv(dev);
40         unsigned long flags;
41         int err;
42
43         printk(KERN_DEBUG "%s: Airport entering sleep mode\n", dev->name);
44
45         err = orinoco_lock(priv, &flags);
46         if (err) {
47                 printk(KERN_ERR "%s: hw_unavailable on PBOOK_SLEEP_NOW\n",
48                        dev->name);
49                 return 0;
50         }
51
52         err = __orinoco_down(dev);
53         if (err)
54                 printk(KERN_WARNING "%s: PBOOK_SLEEP_NOW: Error %d downing interface\n",
55                        dev->name, err);
56
57         netif_device_detach(dev);
58
59         priv->hw_unavailable++;
60
61         orinoco_unlock(priv, &flags);
62
63         disable_irq(dev->irq);
64         pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE, macio_get_of_node(mdev), 0, 0);
65
66         return 0;
67 }
68
69 static int
70 airport_resume(struct macio_dev *mdev)
71 {
72         struct net_device *dev = dev_get_drvdata(&mdev->ofdev.dev);
73         struct orinoco_private *priv = netdev_priv(dev);
74         unsigned long flags;
75         int err;
76
77         printk(KERN_DEBUG "%s: Airport waking up\n", dev->name);
78
79         pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE, macio_get_of_node(mdev), 0, 1);
80         msleep(200);
81
82         enable_irq(dev->irq);
83
84         err = orinoco_reinit_firmware(dev);
85         if (err) {
86                 printk(KERN_ERR "%s: Error %d re-initializing firmware on PBOOK_WAKE\n",
87                        dev->name, err);
88                 return 0;
89         }
90
91         spin_lock_irqsave(&priv->lock, flags);
92
93         netif_device_attach(dev);
94
95         priv->hw_unavailable--;
96
97         if (priv->open && (! priv->hw_unavailable)) {
98                 err = __orinoco_up(dev);
99                 if (err)
100                         printk(KERN_ERR "%s: Error %d restarting card on PBOOK_WAKE\n",
101                                dev->name, err);
102         }
103
104
105         spin_unlock_irqrestore(&priv->lock, flags);
106
107         return 0;
108 }
109
110 static int
111 airport_detach(struct macio_dev *mdev)
112 {
113         struct net_device *dev = dev_get_drvdata(&mdev->ofdev.dev);
114         struct orinoco_private *priv = netdev_priv(dev);
115         struct airport *card = priv->card;
116
117         if (card->ndev_registered)
118                 unregister_netdev(dev);
119         card->ndev_registered = 0;
120
121         if (card->irq_requested)
122                 free_irq(dev->irq, dev);
123         card->irq_requested = 0;
124
125         if (card->vaddr)
126                 iounmap(card->vaddr);
127         card->vaddr = NULL;
128
129         macio_release_resource(mdev, 0);
130
131         pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE, macio_get_of_node(mdev), 0, 0);
132         ssleep(1);
133
134         macio_set_drvdata(mdev, NULL);
135         free_orinocodev(dev);
136
137         return 0;
138 }
139
140 static int airport_hard_reset(struct orinoco_private *priv)
141 {
142         /* It would be nice to power cycle the Airport for a real hard
143          * reset, but for some reason although it appears to
144          * re-initialize properly, it falls in a screaming heap
145          * shortly afterwards. */
146 #if 0
147         struct net_device *dev = priv->ndev;
148         struct airport *card = priv->card;
149
150         /* Vitally important.  If we don't do this it seems we get an
151          * interrupt somewhere during the power cycle, since
152          * hw_unavailable is already set it doesn't get ACKed, we get
153          * into an interrupt loop and the the PMU decides to turn us
154          * off. */
155         disable_irq(dev->irq);
156
157         pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE, macio_get_of_node(card->mdev), 0, 0);
158         ssleep(1);
159         pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE, macio_get_of_node(card->mdev), 0, 1);
160         ssleep(1);
161
162         enable_irq(dev->irq);
163         ssleep(1);
164 #endif
165
166         return 0;
167 }
168
169 static int
170 airport_attach(struct macio_dev *mdev, const struct of_device_id *match)
171 {
172         struct orinoco_private *priv;
173         struct net_device *dev;
174         struct airport *card;
175         unsigned long phys_addr;
176         hermes_t *hw;
177
178         if (macio_resource_count(mdev) < 1 || macio_irq_count(mdev) < 1) {
179                 printk(KERN_ERR PFX "Wrong interrupt/addresses in OF tree\n");
180                 return -ENODEV;
181         }
182
183         /* Allocate space for private device-specific data */
184         dev = alloc_orinocodev(sizeof(*card), airport_hard_reset);
185         if (! dev) {
186                 printk(KERN_ERR PFX "Cannot allocate network device\n");
187                 return -ENODEV;
188         }
189         priv = netdev_priv(dev);
190         card = priv->card;
191
192         hw = &priv->hw;
193         card->mdev = mdev;
194
195         if (macio_request_resource(mdev, 0, "airport")) {
196                 printk(KERN_ERR PFX "can't request IO resource !\n");
197                 free_orinocodev(dev);
198                 return -EBUSY;
199         }
200
201         SET_MODULE_OWNER(dev);
202         SET_NETDEV_DEV(dev, &mdev->ofdev.dev);
203
204         macio_set_drvdata(mdev, dev);
205
206         /* Setup interrupts & base address */
207         dev->irq = macio_irq(mdev, 0);
208         phys_addr = macio_resource_start(mdev, 0);  /* Physical address */
209         printk(KERN_DEBUG PFX "Physical address %lx\n", phys_addr);
210         dev->base_addr = phys_addr;
211         card->vaddr = ioremap(phys_addr, AIRPORT_IO_LEN);
212         if (!card->vaddr) {
213                 printk(KERN_ERR PFX "ioremap() failed\n");
214                 goto failed;
215         }
216
217         hermes_struct_init(hw, card->vaddr, HERMES_16BIT_REGSPACING);
218                 
219         /* Power up card */
220         pmac_call_feature(PMAC_FTR_AIRPORT_ENABLE, macio_get_of_node(mdev), 0, 1);
221         ssleep(1);
222
223         /* Reset it before we get the interrupt */
224         hermes_init(hw);
225
226         if (request_irq(dev->irq, orinoco_interrupt, 0, dev->name, dev)) {
227                 printk(KERN_ERR PFX "Couldn't get IRQ %d\n", dev->irq);
228                 goto failed;
229         }
230         card->irq_requested = 1;
231
232         /* Tell the stack we exist */
233         if (register_netdev(dev) != 0) {
234                 printk(KERN_ERR PFX "register_netdev() failed\n");
235                 goto failed;
236         }
237         printk(KERN_DEBUG PFX "Card registered for interface %s\n", dev->name);
238         card->ndev_registered = 1;
239         return 0;
240  failed:
241         airport_detach(mdev);
242         return -ENODEV;
243 }                               /* airport_attach */
244
245
246 static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
247         " (Benjamin Herrenschmidt <benh@kernel.crashing.org>)";
248 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
249 MODULE_DESCRIPTION("Driver for the Apple Airport wireless card.");
250 MODULE_LICENSE("Dual MPL/GPL");
251
252 static struct of_device_id airport_match[] = 
253 {
254         {
255         .name           = "radio",
256         },
257         {},
258 };
259
260 MODULE_DEVICE_TABLE (of, airport_match);
261
262 static struct macio_driver airport_driver = 
263 {
264         .name           = DRIVER_NAME,
265         .match_table    = airport_match,
266         .probe          = airport_attach,
267         .remove         = airport_detach,
268         .suspend        = airport_suspend,
269         .resume         = airport_resume,
270 };
271
272 static int __init
273 init_airport(void)
274 {
275         printk(KERN_DEBUG "%s\n", version);
276
277         return macio_register_driver(&airport_driver);
278 }
279
280 static void __exit
281 exit_airport(void)
282 {
283         return macio_unregister_driver(&airport_driver);
284 }
285
286 module_init(init_airport);
287 module_exit(exit_airport);