Linux-libre 2.6.27.45-gnu1
[librecmc/linux-libre.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 #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 = pccard_reset_card(link->socket);
69
70         if (ret != CS_SUCCESS)
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 config_ipwireless(struct ipw_dev *ipw)
83 {
84         struct pcmcia_device *link = ipw->link;
85         int ret;
86         config_info_t conf;
87         tuple_t tuple;
88         unsigned short buf[64];
89         cisparse_t parse;
90         unsigned short cor_value;
91         memreq_t memreq_attr_memory;
92         memreq_t memreq_common_memory;
93
94         ipw->is_v2_card = 0;
95
96         tuple.Attributes = 0;
97         tuple.TupleData = (cisdata_t *) buf;
98         tuple.TupleDataMax = sizeof(buf);
99         tuple.TupleOffset = 0;
100
101         tuple.DesiredTuple = RETURN_FIRST_TUPLE;
102
103         ret = pcmcia_get_first_tuple(link, &tuple);
104
105         while (ret == 0) {
106                 ret = pcmcia_get_tuple_data(link, &tuple);
107
108                 if (ret != CS_SUCCESS) {
109                         cs_error(link, GetTupleData, ret);
110                         goto exit0;
111                 }
112                 ret = pcmcia_get_next_tuple(link, &tuple);
113         }
114
115         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
116
117         ret = pcmcia_get_first_tuple(link, &tuple);
118
119         if (ret != CS_SUCCESS) {
120                 cs_error(link, GetFirstTuple, ret);
121                 goto exit0;
122         }
123
124         ret = pcmcia_get_tuple_data(link, &tuple);
125
126         if (ret != CS_SUCCESS) {
127                 cs_error(link, GetTupleData, ret);
128                 goto exit0;
129         }
130
131         ret = pcmcia_parse_tuple(link, &tuple, &parse);
132
133         if (ret != CS_SUCCESS) {
134                 cs_error(link, ParseTuple, ret);
135                 goto exit0;
136         }
137
138         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
139         link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
140         link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
141         link->io.IOAddrLines = 16;
142
143         link->irq.IRQInfo1 = parse.cftable_entry.irq.IRQInfo1;
144
145         /* 0x40 causes it to generate level mode interrupts. */
146         /* 0x04 enables IREQ pin. */
147         cor_value = parse.cftable_entry.index | 0x44;
148         link->conf.ConfigIndex = cor_value;
149
150         /* IRQ and I/O settings */
151         tuple.DesiredTuple = CISTPL_CONFIG;
152
153         ret = pcmcia_get_first_tuple(link, &tuple);
154
155         if (ret != CS_SUCCESS) {
156                 cs_error(link, GetFirstTuple, ret);
157                 goto exit0;
158         }
159
160         ret = pcmcia_get_tuple_data(link, &tuple);
161
162         if (ret != CS_SUCCESS) {
163                 cs_error(link, GetTupleData, ret);
164                 goto exit0;
165         }
166
167         ret = pcmcia_parse_tuple(link, &tuple, &parse);
168
169         if (ret != CS_SUCCESS) {
170                 cs_error(link, GetTupleData, ret);
171                 goto exit0;
172         }
173         link->conf.Attributes = CONF_ENABLE_IRQ;
174         link->conf.ConfigBase = parse.config.base;
175         link->conf.Present = parse.config.rmask[0];
176         link->conf.IntType = INT_MEMORY_AND_IO;
177
178         link->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING | IRQ_HANDLE_PRESENT;
179         link->irq.Handler = ipwireless_interrupt;
180         link->irq.Instance = ipw->hardware;
181
182         ret = pcmcia_request_io(link, &link->io);
183
184         if (ret != CS_SUCCESS) {
185                 cs_error(link, RequestIO, ret);
186                 goto exit0;
187         }
188
189         request_region(link->io.BasePort1, link->io.NumPorts1,
190                         IPWIRELESS_PCCARD_NAME);
191
192         /* memory settings */
193
194         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
195
196         ret = pcmcia_get_first_tuple(link, &tuple);
197
198         if (ret != CS_SUCCESS) {
199                 cs_error(link, GetFirstTuple, ret);
200                 goto exit1;
201         }
202
203         ret = pcmcia_get_tuple_data(link, &tuple);
204
205         if (ret != CS_SUCCESS) {
206                 cs_error(link, GetTupleData, ret);
207                 goto exit1;
208         }
209
210         ret = pcmcia_parse_tuple(link, &tuple, &parse);
211
212         if (ret != CS_SUCCESS) {
213                 cs_error(link, ParseTuple, ret);
214                 goto exit1;
215         }
216
217         if (parse.cftable_entry.mem.nwin > 0) {
218                 ipw->request_common_memory.Attributes =
219                         WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
220                 ipw->request_common_memory.Base =
221                         parse.cftable_entry.mem.win[0].host_addr;
222                 ipw->request_common_memory.Size = parse.cftable_entry.mem.win[0].len;
223                 if (ipw->request_common_memory.Size < 0x1000)
224                         ipw->request_common_memory.Size = 0x1000;
225                 ipw->request_common_memory.AccessSpeed = 0;
226
227                 ret = pcmcia_request_window(&link, &ipw->request_common_memory,
228                                 &ipw->handle_common_memory);
229
230                 if (ret != CS_SUCCESS) {
231                         cs_error(link, RequestWindow, ret);
232                         goto exit1;
233                 }
234
235                 memreq_common_memory.CardOffset =
236                         parse.cftable_entry.mem.win[0].card_addr;
237                 memreq_common_memory.Page = 0;
238
239                 ret = pcmcia_map_mem_page(ipw->handle_common_memory,
240                                 &memreq_common_memory);
241
242                 if (ret != CS_SUCCESS) {
243                         cs_error(link, MapMemPage, ret);
244                         goto exit1;
245                 }
246
247                 ipw->is_v2_card =
248                         parse.cftable_entry.mem.win[0].len == 0x100;
249
250                 ipw->common_memory = ioremap(ipw->request_common_memory.Base,
251                                 ipw->request_common_memory.Size);
252                 request_mem_region(ipw->request_common_memory.Base,
253                                 ipw->request_common_memory.Size, IPWIRELESS_PCCARD_NAME);
254
255                 ipw->request_attr_memory.Attributes =
256                         WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
257                 ipw->request_attr_memory.Base = 0;
258                 ipw->request_attr_memory.Size = 0;      /* this used to be 0x1000 */
259                 ipw->request_attr_memory.AccessSpeed = 0;
260
261                 ret = pcmcia_request_window(&link, &ipw->request_attr_memory,
262                                 &ipw->handle_attr_memory);
263
264                 if (ret != CS_SUCCESS) {
265                         cs_error(link, RequestWindow, ret);
266                         goto exit2;
267                 }
268
269                 memreq_attr_memory.CardOffset = 0;
270                 memreq_attr_memory.Page = 0;
271
272                 ret = pcmcia_map_mem_page(ipw->handle_attr_memory,
273                                 &memreq_attr_memory);
274
275                 if (ret != CS_SUCCESS) {
276                         cs_error(link, MapMemPage, ret);
277                         goto exit2;
278                 }
279
280                 ipw->attr_memory = ioremap(ipw->request_attr_memory.Base,
281                                 ipw->request_attr_memory.Size);
282                 request_mem_region(ipw->request_attr_memory.Base, ipw->request_attr_memory.Size,
283                                 IPWIRELESS_PCCARD_NAME);
284         }
285
286         INIT_WORK(&ipw->work_reboot, signalled_reboot_work);
287
288         ipwireless_init_hardware_v1(ipw->hardware, link->io.BasePort1,
289                                     ipw->attr_memory, ipw->common_memory,
290                                     ipw->is_v2_card, signalled_reboot_callback,
291                                     ipw);
292
293         ret = pcmcia_request_irq(link, &link->irq);
294
295         if (ret != CS_SUCCESS) {
296                 cs_error(link, RequestIRQ, ret);
297                 goto exit3;
298         }
299
300         /* Look up current Vcc */
301
302         ret = pcmcia_get_configuration_info(link, &conf);
303
304         if (ret != CS_SUCCESS) {
305                 cs_error(link, GetConfigurationInfo, ret);
306                 goto exit4;
307         }
308
309         printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": Card type %s\n",
310                         ipw->is_v2_card ? "V2/V3" : "V1");
311         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
312                         ": I/O ports 0x%04x-0x%04x, irq %d\n",
313                         (unsigned int) link->io.BasePort1,
314                         (unsigned int) (link->io.BasePort1 +
315                                 link->io.NumPorts1 - 1),
316                         (unsigned int) link->irq.AssignedIRQ);
317         if (ipw->attr_memory && ipw->common_memory)
318                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
319                         ": attr memory 0x%08lx-0x%08lx, common memory 0x%08lx-0x%08lx\n",
320                         ipw->request_attr_memory.Base,
321                         ipw->request_attr_memory.Base
322                         + ipw->request_attr_memory.Size - 1,
323                         ipw->request_common_memory.Base,
324                         ipw->request_common_memory.Base
325                         + ipw->request_common_memory.Size - 1);
326
327         ipw->network = ipwireless_network_create(ipw->hardware);
328         if (!ipw->network)
329                 goto exit3;
330
331         ipw->tty = ipwireless_tty_create(ipw->hardware, ipw->network,
332                         ipw->nodes);
333         if (!ipw->tty)
334                 goto exit3;
335
336         ipwireless_init_hardware_v2_v3(ipw->hardware);
337
338         /*
339          * Do the RequestConfiguration last, because it enables interrupts.
340          * Then we don't get any interrupts before we're ready for them.
341          */
342         ret = pcmcia_request_configuration(link, &link->conf);
343
344         if (ret != CS_SUCCESS) {
345                 cs_error(link, RequestConfiguration, ret);
346                 goto exit4;
347         }
348
349         link->dev_node = &ipw->nodes[0];
350
351         return 0;
352
353 exit4:
354         pcmcia_disable_device(link);
355 exit3:
356         if (ipw->attr_memory) {
357                 release_mem_region(ipw->request_attr_memory.Base,
358                                 ipw->request_attr_memory.Size);
359                 iounmap(ipw->attr_memory);
360                 pcmcia_release_window(ipw->handle_attr_memory);
361                 pcmcia_disable_device(link);
362         }
363 exit2:
364         if (ipw->common_memory) {
365                 release_mem_region(ipw->request_common_memory.Base,
366                                 ipw->request_common_memory.Size);
367                 iounmap(ipw->common_memory);
368                 pcmcia_release_window(ipw->handle_common_memory);
369         }
370 exit1:
371         pcmcia_disable_device(link);
372 exit0:
373         return -1;
374 }
375
376 static void release_ipwireless(struct ipw_dev *ipw)
377 {
378         pcmcia_disable_device(ipw->link);
379
380         if (ipw->common_memory) {
381                 release_mem_region(ipw->request_common_memory.Base,
382                                 ipw->request_common_memory.Size);
383                 iounmap(ipw->common_memory);
384         }
385         if (ipw->attr_memory) {
386                 release_mem_region(ipw->request_attr_memory.Base,
387                                 ipw->request_attr_memory.Size);
388                 iounmap(ipw->attr_memory);
389         }
390         if (ipw->common_memory)
391                 pcmcia_release_window(ipw->handle_common_memory);
392         if (ipw->attr_memory)
393                 pcmcia_release_window(ipw->handle_attr_memory);
394
395         /* Break the link with Card Services */
396         pcmcia_disable_device(ipw->link);
397 }
398
399 /*
400  * ipwireless_attach() creates an "instance" of the driver, allocating
401  * local data structures for one device (one interface).  The device
402  * is registered with Card Services.
403  *
404  * The pcmcia_device structure is initialized, but we don't actually
405  * configure the card at this point -- we wait until we receive a
406  * card insertion event.
407  */
408 static int ipwireless_attach(struct pcmcia_device *link)
409 {
410         struct ipw_dev *ipw;
411         int ret;
412
413         ipw = kzalloc(sizeof(struct ipw_dev), GFP_KERNEL);
414         if (!ipw)
415                 return -ENOMEM;
416
417         ipw->link = link;
418         link->priv = ipw;
419         link->irq.Instance = ipw;
420
421         /* Link this device into our device list. */
422         link->dev_node = &ipw->nodes[0];
423
424         ipw->hardware = ipwireless_hardware_create();
425         if (!ipw->hardware) {
426                 kfree(ipw);
427                 return -ENOMEM;
428         }
429         /* RegisterClient will call config_ipwireless */
430
431         ret = config_ipwireless(ipw);
432
433         if (ret != 0) {
434                 cs_error(link, RegisterClient, ret);
435                 ipwireless_detach(link);
436                 return ret;
437         }
438
439         return 0;
440 }
441
442 /*
443  * This deletes a driver "instance".  The device is de-registered with
444  * Card Services.  If it has been released, all local data structures
445  * are freed.  Otherwise, the structures will be freed when the device
446  * is released.
447  */
448 static void ipwireless_detach(struct pcmcia_device *link)
449 {
450         struct ipw_dev *ipw = link->priv;
451
452         release_ipwireless(ipw);
453
454         if (ipw->tty != NULL)
455                 ipwireless_tty_free(ipw->tty);
456         if (ipw->network != NULL)
457                 ipwireless_network_free(ipw->network);
458         if (ipw->hardware != NULL)
459                 ipwireless_hardware_free(ipw->hardware);
460         kfree(ipw);
461 }
462
463 static struct pcmcia_driver me = {
464         .owner          = THIS_MODULE,
465         .probe          = ipwireless_attach,
466         .remove         = ipwireless_detach,
467         .drv = { .name  = IPWIRELESS_PCCARD_NAME },
468         .id_table       = ipw_ids
469 };
470
471 /*
472  * Module insertion : initialisation of the module.
473  * Register the card with cardmgr...
474  */
475 static int __init init_ipwireless(void)
476 {
477         int ret;
478
479         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
480                IPWIRELESS_PCMCIA_VERSION " by " IPWIRELESS_PCMCIA_AUTHOR "\n");
481
482         ret = ipwireless_tty_init();
483         if (ret != 0)
484                 return ret;
485
486         ret = pcmcia_register_driver(&me);
487         if (ret != 0)
488                 ipwireless_tty_release();
489
490         return ret;
491 }
492
493 /*
494  * Module removal
495  */
496 static void __exit exit_ipwireless(void)
497 {
498         printk(KERN_INFO IPWIRELESS_PCCARD_NAME " "
499                         IPWIRELESS_PCMCIA_VERSION " removed\n");
500
501         pcmcia_unregister_driver(&me);
502         ipwireless_tty_release();
503 }
504
505 module_init(init_ipwireless);
506 module_exit(exit_ipwireless);
507
508 MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
509 MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
510 MODULE_LICENSE("GPL");