263a18f381b33e847960052344afa8e67329f947
[safe/jmp/linux-2.6] / 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 #include <pcmcia/cs.h>
36
37 static struct pcmcia_device_id ipw_ids[] = {
38         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0100),
39         PCMCIA_DEVICE_MANF_CARD(0x02f2, 0x0200),
40         PCMCIA_DEVICE_NULL
41 };
42 MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
43
44 static void ipwireless_detach(struct pcmcia_device *link);
45
46 /*
47  * Module params
48  */
49 /* Debug mode: more verbose, print sent/recv bytes */
50 int ipwireless_debug;
51 int ipwireless_loopback;
52 int ipwireless_out_queue = 10;
53
54 module_param_named(debug, ipwireless_debug, int, 0);
55 module_param_named(loopback, ipwireless_loopback, int, 0);
56 module_param_named(out_queue, ipwireless_out_queue, int, 0);
57 MODULE_PARM_DESC(debug, "switch on debug messages [0]");
58 MODULE_PARM_DESC(loopback,
59                 "debug: enable ras_raw channel [0]");
60 MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
61
62 /* Executes in process context. */
63 static void signalled_reboot_work(struct work_struct *work_reboot)
64 {
65         struct ipw_dev *ipw = container_of(work_reboot, struct ipw_dev,
66                         work_reboot);
67         struct pcmcia_device *link = ipw->link;
68         int ret = pcmcia_reset_card(link->socket);
69
70         if (ret != 0)
71                 cs_error(link, ResetCard, ret);
72 }
73
74 static void signalled_reboot_callback(void *callback_data)
75 {
76         struct ipw_dev *ipw = (struct ipw_dev *) callback_data;
77
78         /* Delegate to process context. */
79         schedule_work(&ipw->work_reboot);
80 }
81
82 static int ipwireless_ioprobe(struct pcmcia_device *p_dev,
83                               cistpl_cftable_entry_t *cfg,
84                               cistpl_cftable_entry_t *dflt,
85                               unsigned int vcc,
86                               void *priv_data)
87 {
88         p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
89         p_dev->io.BasePort1 = cfg->io.win[0].base;
90         p_dev->io.NumPorts1 = cfg->io.win[0].len;
91         p_dev->io.IOAddrLines = 16;
92
93         p_dev->irq.IRQInfo1 = cfg->irq.IRQInfo1;
94
95         /* 0x40 causes it to generate level mode interrupts. */
96         /* 0x04 enables IREQ pin. */
97         p_dev->conf.ConfigIndex = cfg->index | 0x44;
98         return pcmcia_request_io(p_dev, &p_dev->io);
99 }
100
101 static int config_ipwireless(struct ipw_dev *ipw)
102 {
103         struct pcmcia_device *link = ipw->link;
104         int ret = 0;
105         tuple_t tuple;
106         unsigned short buf[64];
107         cisparse_t parse;
108         memreq_t memreq_attr_memory;
109         memreq_t memreq_common_memory;
110
111         ipw->is_v2_card = 0;
112
113         tuple.Attributes = 0;
114         tuple.TupleData = (cisdata_t *) buf;
115         tuple.TupleDataMax = sizeof(buf);
116         tuple.TupleOffset = 0;
117
118         ret = pcmcia_loop_config(link, ipwireless_ioprobe, NULL);
119         if (ret != 0) {
120                 cs_error(link, RequestIO, ret);
121                 goto exit0;
122         }
123
124         link->conf.Attributes = CONF_ENABLE_IRQ;
125         link->conf.IntType = INT_MEMORY_AND_IO;
126
127         link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
128         link->irq.Handler = ipwireless_interrupt;
129         link->irq.Instance = ipw->hardware;
130
131         request_region(link->io.BasePort1, link->io.NumPorts1,
132                         IPWIRELESS_PCCARD_NAME);
133
134         /* memory settings */
135         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
136
137         ret = pcmcia_get_first_tuple(link, &tuple);
138         if (ret != 0) {
139                 cs_error(link, GetFirstTuple, ret);
140                 goto exit1;
141         }
142
143         ret = pcmcia_get_tuple_data(link, &tuple);
144
145         if (ret != 0) {
146                 cs_error(link, GetTupleData, ret);
147                 goto exit1;
148         }
149
150         ret = pcmcia_parse_tuple(&tuple, &parse);
151
152         if (ret != 0) {
153                 cs_error(link, ParseTuple, ret);
154                 goto exit1;
155         }
156
157         if (parse.cftable_entry.mem.nwin > 0) {
158                 ipw->request_common_memory.Attributes =
159                         WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
160                 ipw->request_common_memory.Base =
161                         parse.cftable_entry.mem.win[0].host_addr;
162                 ipw->request_common_memory.Size = parse.cftable_entry.mem.win[0].len;
163                 if (ipw->request_common_memory.Size < 0x1000)
164                         ipw->request_common_memory.Size = 0x1000;
165                 ipw->request_common_memory.AccessSpeed = 0;
166
167                 ret = pcmcia_request_window(&link, &ipw->request_common_memory,
168                                 &ipw->handle_common_memory);
169
170                 if (ret != 0) {
171                         cs_error(link, RequestWindow, ret);
172                         goto exit1;
173                 }
174
175                 memreq_common_memory.CardOffset =
176                         parse.cftable_entry.mem.win[0].card_addr;
177                 memreq_common_memory.Page = 0;
178
179                 ret = pcmcia_map_mem_page(ipw->handle_common_memory,
180                                 &memreq_common_memory);
181
182                 if (ret != 0) {
183                         cs_error(link, MapMemPage, ret);
184                         goto exit1;
185                 }
186
187                 ipw->is_v2_card =
188                         parse.cftable_entry.mem.win[0].len == 0x100;
189
190                 ipw->common_memory = ioremap(ipw->request_common_memory.Base,
191                                 ipw->request_common_memory.Size);
192                 request_mem_region(ipw->request_common_memory.Base,
193                                 ipw->request_common_memory.Size, IPWIRELESS_PCCARD_NAME);
194
195                 ipw->request_attr_memory.Attributes =
196                         WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
197                 ipw->request_attr_memory.Base = 0;
198                 ipw->request_attr_memory.Size = 0;      /* this used to be 0x1000 */
199                 ipw->request_attr_memory.AccessSpeed = 0;
200
201                 ret = pcmcia_request_window(&link, &ipw->request_attr_memory,
202                                 &ipw->handle_attr_memory);
203
204                 if (ret != 0) {
205                         cs_error(link, RequestWindow, ret);
206                         goto exit2;
207                 }
208
209                 memreq_attr_memory.CardOffset = 0;
210                 memreq_attr_memory.Page = 0;
211
212                 ret = pcmcia_map_mem_page(ipw->handle_attr_memory,
213                                 &memreq_attr_memory);
214
215                 if (ret != 0) {
216                         cs_error(link, MapMemPage, ret);
217                         goto exit2;
218                 }
219
220                 ipw->attr_memory = ioremap(ipw->request_attr_memory.Base,
221                                 ipw->request_attr_memory.Size);
222                 request_mem_region(ipw->request_attr_memory.Base, ipw->request_attr_memory.Size,
223                                 IPWIRELESS_PCCARD_NAME);
224         }
225
226         INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
227
228         ipwireless_init_hardware_v1(ipw->hardware, link->io.BasePort1,
229                                     ipw->attr_memory, ipw->common_memory,
230                                     ipw->is_v2_card, signalled_reboot_callback,
231                                     ipw);
232
233         ret = pcmcia_request_irq(link, &link->irq);
234
235         if (ret != 0) {
236                 cs_error(link, RequestIRQ, ret);
237                 goto exit3;
238         }
239
240         printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
241                         ipw->is_v2_card ? "V2/V3" : "V1");
242         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
243                         ": I/O ports 0x%04x-0x%04x, irq %d\n",
244                         (unsigned int) link->io.BasePort1,
245                         (unsigned int) (link->io.BasePort1 +
246                                 link->io.NumPorts1 - 1),
247                         (unsigned int) link->irq.AssignedIRQ);
248         if (ipw->attr_memory && ipw->common_memory)
249                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
250                         ": attr memory 0x%08lx-0x%08lx, common memory 0x%08lx-0x%08lx\n",
251                         ipw->request_attr_memory.Base,
252                         ipw->request_attr_memory.Base
253                         + ipw->request_attr_memory.Size - 1,
254                         ipw->request_common_memory.Base,
255                         ipw->request_common_memory.Base
256                         + ipw->request_common_memory.Size - 1);
257
258         ipw->network = ipwireless_network_create(ipw->hardware);
259         if (!ipw->network)
260                 goto exit3;
261
262         ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network,
263                         ipw->nodes);
264         if (!ipw->tty)
265                 goto exit3;
266
267         ipwireless_init_hardware_v2_v3(ipw->hardware);
268
269         /*
270          * Do the RequestConfiguration last, because it enables interrupts.
271          * Then we don't get any interrupts before we're ready for them.
272          */
273         ret = pcmcia_request_configuration(link, &link->conf);
274
275         if (ret != 0) {
276                 cs_error(link, RequestConfiguration, ret);
277                 goto exit4;
278         }
279
280         link->dev_node = &ipw->nodes[0];
281
282         return 0;
283
284 exit4:
285         pcmcia_disable_device(link);
286 exit3:
287         if (ipw->attr_memory) {
288                 release_mem_region(ipw->request_attr_memory.Base,
289                                 ipw->request_attr_memory.Size);
290                 iounmap(ipw->attr_memory);
291                 pcmcia_release_window(ipw->handle_attr_memory);
292                 pcmcia_disable_device(link);
293         }
294 exit2:
295         if (ipw->common_memory) {
296                 release_mem_region(ipw->request_common_memory.Base,
297                                 ipw->request_common_memory.Size);
298                 iounmap(ipw->common_memory);
299                 pcmcia_release_window(ipw->handle_common_memory);
300         }
301 exit1:
302         pcmcia_disable_device(link);
303 exit0:
304         return -1;
305 }
306
307 static void release_ipwireless(struct ipw_dev *ipw)
308 {
309         pcmcia_disable_device(ipw->link);
310
311         if (ipw->common_memory) {
312                 release_mem_region(ipw->request_common_memory.Base,
313                                 ipw->request_common_memory.Size);
314                 iounmap(ipw->common_memory);
315         }
316         if (ipw->attr_memory) {
317                 release_mem_region(ipw->request_attr_memory.Base,
318                                 ipw->request_attr_memory.Size);
319                 iounmap(ipw->attr_memory);
320         }
321         if (ipw->common_memory)
322                 pcmcia_release_window(ipw->handle_common_memory);
323         if (ipw->attr_memory)
324                 pcmcia_release_window(ipw->handle_attr_memory);
325
326         /* Break the link with Card Services */
327         pcmcia_disable_device(ipw->link);
328 }
329
330 /*
331  * ipwireless_attach() creates an "instance" of the driver, allocating
332  * local data structures for one device (one interface).  The device
333  * is registered with Card Services.
334  *
335  * The pcmcia_device structure is initialized, but we don't actually
336  * configure the card at this point -- we wait until we receive a
337  * card insertion event.
338  */
339 static int ipwireless_attach(struct pcmcia_device *link)
340 {
341         struct ipw_dev *ipw;
342         int ret;
343
344         ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
345         if (!ipw)
346                 return -ENOMEM;
347
348         ipw->link = link;
349         link->priv = ipw;
350         link->irq.Instance = ipw;
351
352         /* Link this device into our device list. */
353         link->dev_node = &ipw->nodes[0];
354
355         ipw->hardware = ipwireless_hardware_create();
356         if (!ipw->hardware) {
357                 kfree(ipw);
358                 return -ENOMEM;
359         }
360         /* RegisterClient will call config_ipwireless */
361
362         ret = config_ipwireless(ipw);
363
364         if (ret != 0) {
365                 cs_error(link, RegisterClient, ret);
366                 ipwireless_detach(link);
367                 return ret;
368         }
369
370         return 0;
371 }
372
373 /*
374  * This deletes a driver "instance".  The device is de-registered with
375  * Card Services.  If it has been released, all local data structures
376  * are freed.  Otherwise, the structures will be freed when the device
377  * is released.
378  */
379 static void ipwireless_detach(struct pcmcia_device *link)
380 {
381         struct ipw_dev *ipw = link->priv;
382
383         release_ipwireless(ipw);
384
385         if (ipw->tty != NULL)
386                 ipwireless_tty_free(ipw->tty);
387         if (ipw->network != NULL)
388                 ipwireless_network_free(ipw->network);
389         if (ipw->hardware != NULL)
390                 ipwireless_hardware_free(ipw->hardware);
391         kfree(ipw);
392 }
393
394 static struct pcmcia_driver me = {
395         .owner          = THIS_MODULE,
396         .probe          = ipwireless_attach,
397         .remove         = ipwireless_detach,
398         .drv = { .name  = IPWIRELESS_PCCARD_NAME },
399         .id_table       = ipw_ids
400 };
401
402 /*
403  * Module insertion : initialisation of the module.
404  * Register the card with cardmgr...
405  */
406 static int __init init_ipwireless(void)
407 {
408         int ret;
409
410         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
411                IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
412
413         ret = ipwireless_tty_init();
414         if (ret != 0)
415                 return ret;
416
417         ret = pcmcia_register_driver(&me);
418         if (ret != 0)
419                 ipwireless_tty_release();
420
421         return ret;
422 }
423
424 /*
425  * Module removal
426  */
427 static void __exit exit_ipwireless(void)
428 {
429         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
430                         IPWIRELESS_PCMCIA_VERSION " removed\n");
431
432         pcmcia_unregister_driver(&me);
433         ipwireless_tty_release();
434 }
435
436 module_init(init_ipwireless);
437 module_exit(exit_ipwireless);
438
439 MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
440 MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
441 MODULE_LICENSE("GPL");