pcmcia: introduce autoconfiguration feature
[linux-2.6.git] / drivers / char / pcmcia / ipwireless / main.c
1 /*
2  * IPWireless 3G PCMCIA Network Driver
3  *
4  * Original code
5  *   by Stephen Blackheath <stephen@blacksapphire.com>,
6  *      Ben Martel <benm@symmetric.co.nz>
7  *
8  * Copyrighted as follows:
9  *   Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
10  *
11  * Various driver changes and rewrites, port to new kernels
12  *   Copyright (C) 2006-2007 Jiri Kosina
13  *
14  * Misc code cleanups and updates
15  *   Copyright (C) 2007 David Sterba
16  */
17
18 #include "hardware.h"
19 #include "network.h"
20 #include "main.h"
21 #include "tty.h"
22
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/io.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30
31 #include <pcmcia/cisreg.h>
32 #include <pcmcia/device_id.h>
33 #include <pcmcia/ss.h>
34 #include <pcmcia/ds.h>
35
36 static struct pcmcia_device_id ipw_ids[] = {
37         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
38         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
39         PCMCIA_DEVICE_NULL
40 };
41 MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
42
43 static void ipwireless_detach(struct pcmcia_device *link);
44
45 /*
46  * Module params
47  */
48 /* Debug mode: more verbose, print sent/recv bytes */
49 int ipwireless_debug;
50 int ipwireless_loopback;
51 int ipwireless_out_queue = 10;
52
53 module_param_named(debug, ipwireless_debug, int, 0);
54 module_param_named(loopback, ipwireless_loopback, int, 0);
55 module_param_named(out_queue, ipwireless_out_queue, int, 0);
56 MODULE_PARM_DESC(debug, "switch on debug messages [0]");
57 MODULE_PARM_DESC(loopback,
58                 "debug: enable ras_raw channel [0]");
59 MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
60
61 /* Executes in process context. */
62 static void signalled_reboot_work(struct work_struct *work_reboot)
63 {
64         struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
65                         work_reboot);
66         struct pcmcia_device *link = ipw->link;
67         pcmcia_reset_card(link->socket);
68 }
69
70 static void signalled_reboot_callback(void *callback_data)
71 {
72         struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
73
74         /* Delegate to process context. */
75         schedule_work(&ipw->work_reboot);
76 }
77
78 static int ipwireless_probe(struct pcmcia_device *p_dev,
79                             cistpl_cftable_entry_t *cfg,
80                             cistpl_cftable_entry_t *dflt,
81                             void *priv_data)
82 {
83         struct ipw_dev *ipw = priv_data;
84         struct resource *io_resource;
85         int ret;
86
87         p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
88         p_dev->resource[0]->start = cfg->io.win[0].base;
89         p_dev->resource[0]->end = cfg->io.win[0].len;
90
91         /* 0x40 causes it to generate level mode interrupts. */
92         /* 0x04 enables IREQ pin. */
93         p_dev->config_index = cfg->index | 0x44;
94         p_dev->io_lines = 16;
95         ret = pcmcia_request_io(p_dev);
96         if (ret)
97                 return ret;
98
99         io_resource = request_region(p_dev->resource[0]->start,
100                                 resource_size(p_dev->resource[0]),
101                                 IPWIRELESS_PCCARD_NAME);
102
103         if (cfg->mem.nwin == 0)
104                 return 0;
105
106         p_dev->resource[2]->flags |=
107                 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
108         p_dev->resource[2]->start = cfg->mem.win[0].host_addr;
109         p_dev->resource[2]->end = cfg->mem.win[0].len;
110         if (p_dev->resource[2]->end < 0x1000)
111                 p_dev->resource[2]->end = 0x1000;
112
113         ret = pcmcia_request_window(p_dev, p_dev->resource[2], 0);
114         if (ret != 0)
115                 goto exit1;
116
117         ret = pcmcia_map_mem_page(p_dev, p_dev->resource[2],
118                                 cfg->mem.win[0].card_addr);
119         if (ret != 0)
120                 goto exit2;
121
122         ipw->is_v2_card = cfg->mem.win[0].len == 0x100;
123
124         ipw->attr_memory = ioremap(p_dev->resource[2]->start,
125                                 resource_size(p_dev->resource[2]));
126         request_mem_region(p_dev->resource[2]->start,
127                         resource_size(p_dev->resource[2]),
128                         IPWIRELESS_PCCARD_NAME);
129
130         p_dev->resource[3]->flags |= WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM |
131                                         WIN_ENABLE;
132         p_dev->resource[3]->end = 0; /* this used to be 0x1000 */
133         ret = pcmcia_request_window(p_dev, p_dev->resource[3], 0);
134         if (ret != 0)
135                 goto exit2;
136
137         ret = pcmcia_map_mem_page(p_dev, p_dev->resource[3], 0);
138         if (ret != 0)
139                 goto exit3;
140
141         ipw->attr_memory = ioremap(p_dev->resource[3]->start,
142                                 resource_size(p_dev->resource[3]));
143         request_mem_region(p_dev->resource[3]->start,
144                         resource_size(p_dev->resource[3]),
145                         IPWIRELESS_PCCARD_NAME);
146
147         return 0;
148
149 exit3:
150 exit2:
151         if (ipw->common_memory) {
152                 release_mem_region(p_dev->resource[2]->start,
153                                 resource_size(p_dev->resource[2]));
154                 iounmap(ipw->common_memory);
155         }
156 exit1:
157         release_resource(io_resource);
158         pcmcia_disable_device(p_dev);
159         return -1;
160 }
161
162 static int config_ipwireless(struct ipw_dev *ipw)
163 {
164         struct pcmcia_device *link = ipw->link;
165         int ret = 0;
166
167         ipw->is_v2_card = 0;
168
169         ret = pcmcia_loop_config(link, ipwireless_probe, ipw);
170         if (ret != 0)
171                 return ret;
172
173         link->config_flags |= CONF_ENABLE_IRQ;
174
175         INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
176
177         ipwireless_init_hardware_v1(ipw->hardware, link->resource[0]->start,
178                                     ipw->attr_memory, ipw->common_memory,
179                                     ipw->is_v2_card, signalled_reboot_callback,
180                                     ipw);
181
182         ret = pcmcia_request_irq(link, ipwireless_interrupt);
183         if (ret != 0)
184                 goto exit;
185
186         printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
187                         ipw->is_v2_card ? "V2/V3" : "V1");
188         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
189                 ": I/O ports %pR, irq %d\n", link->resource[0],
190                         (unsigned int) link->irq);
191         if (ipw->attr_memory && ipw->common_memory)
192                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
193                         ": attr memory %pR, common memory %pR\n",
194                         link->resource[3],
195                         link->resource[2]);
196
197         ipw->network = ipwireless_network_create(ipw->hardware);
198         if (!ipw->network)
199                 goto exit;
200
201         ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network);
202         if (!ipw->tty)
203                 goto exit;
204
205         ipwireless_init_hardware_v2_v3(ipw->hardware);
206
207         /*
208          * Do the RequestConfiguration last, because it enables interrupts.
209          * Then we don't get any interrupts before we're ready for them.
210          */
211         ret = pcmcia_enable_device(link);
212         if (ret != 0)
213                 goto exit;
214
215         return 0;
216
217 exit:
218         if (ipw->common_memory) {
219                 release_mem_region(link->resource[2]->start,
220                                 resource_size(link->resource[2]));
221                 iounmap(ipw->common_memory);
222         }
223         if (ipw->attr_memory) {
224                 release_mem_region(link->resource[3]->start,
225                                 resource_size(link->resource[3]));
226                 iounmap(ipw->attr_memory);
227         }
228         pcmcia_disable_device(link);
229         return -1;
230 }
231
232 static void release_ipwireless(struct ipw_dev *ipw)
233 {
234         if (ipw->common_memory) {
235                 release_mem_region(ipw->link->resource[2]->start,
236                                 resource_size(ipw->link->resource[2]));
237                 iounmap(ipw->common_memory);
238         }
239         if (ipw->attr_memory) {
240                 release_mem_region(ipw->link->resource[3]->start,
241                                 resource_size(ipw->link->resource[3]));
242                 iounmap(ipw->attr_memory);
243         }
244         pcmcia_disable_device(ipw->link);
245 }
246
247 /*
248  * ipwireless_attach() creates an "instance" of the driver, allocating
249  * local data structures for one device (one interface).  The device
250  * is registered with Card Services.
251  *
252  * The pcmcia_device structure is initialized, but we don't actually
253  * configure the card at this point -- we wait until we receive a
254  * card insertion event.
255  */
256 static int ipwireless_attach(struct pcmcia_device *link)
257 {
258         struct ipw_dev *ipw;
259         int ret;
260
261         ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
262         if (!ipw)
263                 return -ENOMEM;
264
265         ipw->link = link;
266         link->priv = ipw;
267
268         ipw->hardware = ipwireless_hardware_create();
269         if (!ipw->hardware) {
270                 kfree(ipw);
271                 return -ENOMEM;
272         }
273         /* RegisterClient will call config_ipwireless */
274
275         ret = config_ipwireless(ipw);
276
277         if (ret != 0) {
278                 ipwireless_detach(link);
279                 return ret;
280         }
281
282         return 0;
283 }
284
285 /*
286  * This deletes a driver "instance".  The device is de-registered with
287  * Card Services.  If it has been released, all local data structures
288  * are freed.  Otherwise, the structures will be freed when the device
289  * is released.
290  */
291 static void ipwireless_detach(struct pcmcia_device *link)
292 {
293         struct ipw_dev *ipw = link->priv;
294
295         release_ipwireless(ipw);
296
297         if (ipw->tty != NULL)
298                 ipwireless_tty_free(ipw->tty);
299         if (ipw->network != NULL)
300                 ipwireless_network_free(ipw->network);
301         if (ipw->hardware != NULL)
302                 ipwireless_hardware_free(ipw->hardware);
303         kfree(ipw);
304 }
305
306 static struct pcmcia_driver me = {
307         .owner          = THIS_MODULE,
308         .probe          = ipwireless_attach,
309         .remove         = ipwireless_detach,
310         .drv = { .name  = IPWIRELESS_PCCARD_NAME },
311         .id_table       = ipw_ids
312 };
313
314 /*
315  * Module insertion : initialisation of the module.
316  * Register the card with cardmgr...
317  */
318 static int __init init_ipwireless(void)
319 {
320         int ret;
321
322         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
323                IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
324
325         ret = ipwireless_tty_init();
326         if (ret != 0)
327                 return ret;
328
329         ret = pcmcia_register_driver(&me);
330         if (ret != 0)
331                 ipwireless_tty_release();
332
333         return ret;
334 }
335
336 /*
337  * Module removal
338  */
339 static void __exit exit_ipwireless(void)
340 {
341         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
342                         IPWIRELESS_PCMCIA_VERSION " removed\n");
343
344         pcmcia_unregister_driver(&me);
345         ipwireless_tty_release();
346 }
347
348 module_init(init_ipwireless);
349 module_exit(exit_ipwireless);
350
351 MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
352 MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
353 MODULE_LICENSE("GPL");