blob: 5eca7a99afe6c3daf1c793f4c0990e5125a72862 [file] [log] [blame]
David Sterba099dc4f2008-02-07 10:57:12 +01001/*
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
David Sterba099dc4f2008-02-07 10:57:12 +010031#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
37static 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};
42MODULE_DEVICE_TABLE(pcmcia, ipw_ids);
43
44static void ipwireless_detach(struct pcmcia_device *link);
45
46/*
47 * Module params
48 */
49/* Debug mode: more verbose, print sent/recv bytes */
50int ipwireless_debug;
51int ipwireless_loopback;
David Sterbabee9c7c2008-07-28 16:53:21 +020052int ipwireless_out_queue = 10;
David Sterba099dc4f2008-02-07 10:57:12 +010053
54module_param_named(debug, ipwireless_debug, int, 0);
55module_param_named(loopback, ipwireless_loopback, int, 0);
56module_param_named(out_queue, ipwireless_out_queue, int, 0);
57MODULE_PARM_DESC(debug, "switch on debug messages [0]");
58MODULE_PARM_DESC(loopback,
59 "debug: enable ras_raw channel [0]");
David Sterbabee9c7c2008-07-28 16:53:21 +020060MODULE_PARM_DESC(out_queue, "debug: set size of outgoing PPP queue [10]");
David Sterba099dc4f2008-02-07 10:57:12 +010061
62/* Executes in process context. */
63static 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
74static 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
82static 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;
David Sterba099dc4f2008-02-07 10:57:12 +010091 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
David Sterba09e491e2008-07-28 16:53:16 +0200189 request_region(link->io.BasePort1, link->io.NumPorts1,
190 IPWIRELESS_PCCARD_NAME);
191
David Sterba099dc4f2008-02-07 10:57:12 +0100192 /* 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) {
David Sterba09e491e2008-07-28 16:53:16 +0200218 ipw->request_common_memory.Attributes =
David Sterba099dc4f2008-02-07 10:57:12 +0100219 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_CM | WIN_ENABLE;
David Sterba09e491e2008-07-28 16:53:16 +0200220 ipw->request_common_memory.Base =
David Sterba099dc4f2008-02-07 10:57:12 +0100221 parse.cftable_entry.mem.win[0].host_addr;
David Sterba09e491e2008-07-28 16:53:16 +0200222 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;
David Sterba099dc4f2008-02-07 10:57:12 +0100226
David Sterba09e491e2008-07-28 16:53:16 +0200227 ret = pcmcia_request_window(&link, &ipw->request_common_memory,
David Sterba099dc4f2008-02-07 10:57:12 +0100228 &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
David Sterba09e491e2008-07-28 16:53:16 +0200250 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);
David Sterba099dc4f2008-02-07 10:57:12 +0100254
David Sterba09e491e2008-07-28 16:53:16 +0200255 ipw->request_attr_memory.Attributes =
David Sterba099dc4f2008-02-07 10:57:12 +0100256 WIN_DATA_WIDTH_16 | WIN_MEMORY_TYPE_AM | WIN_ENABLE;
David Sterba09e491e2008-07-28 16:53:16 +0200257 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;
David Sterba099dc4f2008-02-07 10:57:12 +0100260
David Sterba09e491e2008-07-28 16:53:16 +0200261 ret = pcmcia_request_window(&link, &ipw->request_attr_memory,
David Sterba099dc4f2008-02-07 10:57:12 +0100262 &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
David Sterba09e491e2008-07-28 16:53:16 +0200280 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);
David Sterba099dc4f2008-02-07 10:57:12 +0100284 }
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
David Sterba622e7132008-07-28 16:52:55 +0200319 ": attr memory 0x%08lx-0x%08lx, common memory 0x%08lx-0x%08lx\n",
David Sterba09e491e2008-07-28 16:53:16 +0200320 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);
David Sterba099dc4f2008-02-07 10:57:12 +0100326
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
353exit4:
354 pcmcia_disable_device(link);
355exit3:
356 if (ipw->attr_memory) {
David Sterba09e491e2008-07-28 16:53:16 +0200357 release_mem_region(ipw->request_attr_memory.Base,
358 ipw->request_attr_memory.Size);
David Sterba099dc4f2008-02-07 10:57:12 +0100359 iounmap(ipw->attr_memory);
360 pcmcia_release_window(ipw->handle_attr_memory);
361 pcmcia_disable_device(link);
362 }
363exit2:
364 if (ipw->common_memory) {
David Sterba09e491e2008-07-28 16:53:16 +0200365 release_mem_region(ipw->request_common_memory.Base,
366 ipw->request_common_memory.Size);
David Sterba099dc4f2008-02-07 10:57:12 +0100367 iounmap(ipw->common_memory);
368 pcmcia_release_window(ipw->handle_common_memory);
369 }
370exit1:
371 pcmcia_disable_device(link);
372exit0:
373 return -1;
374}
375
376static void release_ipwireless(struct ipw_dev *ipw)
377{
David Sterba09e491e2008-07-28 16:53:16 +0200378 pcmcia_disable_device(ipw->link);
David Sterba099dc4f2008-02-07 10:57:12 +0100379
David Sterba09e491e2008-07-28 16:53:16 +0200380 if (ipw->common_memory) {
381 release_mem_region(ipw->request_common_memory.Base,
382 ipw->request_common_memory.Size);
David Sterba099dc4f2008-02-07 10:57:12 +0100383 iounmap(ipw->common_memory);
David Sterba09e491e2008-07-28 16:53:16 +0200384 }
385 if (ipw->attr_memory) {
386 release_mem_region(ipw->request_attr_memory.Base,
387 ipw->request_attr_memory.Size);
David Sterba099dc4f2008-02-07 10:57:12 +0100388 iounmap(ipw->attr_memory);
David Sterba09e491e2008-07-28 16:53:16 +0200389 }
David Sterba099dc4f2008-02-07 10:57:12 +0100390 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);
David Sterba09e491e2008-07-28 16:53:16 +0200394
395 /* Break the link with Card Services */
396 pcmcia_disable_device(ipw->link);
David Sterba099dc4f2008-02-07 10:57:12 +0100397}
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 */
408static 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 */
448static void ipwireless_detach(struct pcmcia_device *link)
449{
450 struct ipw_dev *ipw = link->priv;
451
452 release_ipwireless(ipw);
453
David Sterba099dc4f2008-02-07 10:57:12 +0100454 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
463static 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 */
475static 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 */
496static 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
505module_init(init_ipwireless);
506module_exit(exit_ipwireless);
507
508MODULE_AUTHOR(IPWIRELESS_PCMCIA_AUTHOR);
509MODULE_DESCRIPTION(IPWIRELESS_PCCARD_NAME " " IPWIRELESS_PCMCIA_VERSION);
510MODULE_LICENSE("GPL");