MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / isdn / sc / init.c
blob930845cff800857c31a0eb102f971e22963b6d3c
1 /*
2 * This software may be used and distributed according to the terms
3 * of the GNU General Public License, incorporated herein by reference.
5 */
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include "includes.h"
11 #include "hardware.h"
12 #include "card.h"
14 MODULE_DESCRIPTION("ISDN4Linux: Driver for Spellcaster card");
15 MODULE_AUTHOR("Spellcaster Telecommunications Inc.");
16 MODULE_LICENSE("GPL");
17 MODULE_PARM( io, "1-" __MODULE_STRING(MAX_CARDS) "i");
18 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_CARDS) "i");
19 MODULE_PARM(ram, "1-" __MODULE_STRING(MAX_CARDS) "i");
20 MODULE_PARM(do_reset, "i");
22 board *sc_adapter[MAX_CARDS];
23 int cinst;
25 static char devname[] = "scX";
26 const char version[] = "2.0b1";
28 const char *boardname[] = { "DataCommute/BRI", "DataCommute/PRI", "TeleCommute/BRI" };
30 /* insmod set parameters */
31 static unsigned int io[] = {0,0,0,0};
32 static unsigned char irq[] = {0,0,0,0};
33 static unsigned long ram[] = {0,0,0,0};
34 static int do_reset = 0;
36 static int sup_irq[] = { 11, 10, 9, 5, 12, 14, 7, 3, 4, 6 };
37 #define MAX_IRQS 10
39 extern irqreturn_t interrupt_handler(int, void *, struct pt_regs *);
40 extern int sndpkt(int, int, int, struct sk_buff *);
41 extern int command(isdn_ctrl *);
42 extern int indicate_status(int, int, ulong, char*);
43 extern int reset(int);
45 int identify_board(unsigned long, unsigned int);
47 int irq_supported(int irq_x)
49 int i;
50 for(i=0 ; i < MAX_IRQS ; i++) {
51 if(sup_irq[i] == irq_x)
52 return 1;
54 return 0;
57 static int __init sc_init(void)
59 int b = -1;
60 int i, j;
61 int status = -ENODEV;
63 unsigned long memsize = 0;
64 unsigned long features = 0;
65 isdn_if *interface;
66 unsigned char channels;
67 unsigned char pgport;
68 unsigned long magic;
69 int model;
70 int last_base = IOBASE_MIN;
71 int probe_exhasted = 0;
73 #ifdef MODULE
74 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s Loaded\n", version);
75 #else
76 pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s\n", version);
77 #endif
78 pr_info("Copyright (C) 1996 SpellCaster Telecommunications Inc.\n");
80 while(b++ < MAX_CARDS - 1) {
81 pr_debug("Probing for adapter #%d\n", b);
83 * Initialize reusable variables
85 model = -1;
86 magic = 0;
87 channels = 0;
88 pgport = 0;
90 /*
91 * See if we should probe for IO base
93 pr_debug("I/O Base for board %d is 0x%x, %s probe\n", b, io[b],
94 io[b] == 0 ? "will" : "won't");
95 if(io[b]) {
97 * No, I/O Base has been provided
99 for (i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
100 if(!request_region(io[b] + i * 0x400, 1, "sc test")) {
101 pr_debug("check_region for 0x%x failed\n", io[b] + i * 0x400);
102 io[b] = 0;
103 break;
104 } else
105 release_region(io[b] + i * 0x400, 1);
109 * Confirm the I/O Address with a test
111 if(io[b] == 0) {
112 pr_debug("I/O Address 0x%x is in use.\n");
113 continue;
116 outb(0x18, io[b] + 0x400 * EXP_PAGE0);
117 if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) {
118 pr_debug("I/O Base 0x%x fails test\n");
119 continue;
122 else {
124 * Yes, probe for I/O Base
126 if(probe_exhasted) {
127 pr_debug("All probe addresses exhasted, skipping\n");
128 continue;
130 pr_debug("Probing for I/O...\n");
131 for (i = last_base ; i <= IOBASE_MAX ; i += IOBASE_OFFSET) {
132 int found_io = 1;
133 if (i == IOBASE_MAX) {
134 probe_exhasted = 1; /* No more addresses to probe */
135 pr_debug("End of Probes\n");
137 last_base = i + IOBASE_OFFSET;
138 pr_debug(" checking 0x%x...", i);
139 for ( j = 0 ; j < MAX_IO_REGS - 1 ; j++) {
140 if(!request_region(i + j * 0x400, 1, "sc test")) {
141 pr_debug("Failed\n");
142 found_io = 0;
143 break;
144 } else
145 release_region(i + j * 0x400, 1);
148 if(found_io) {
149 io[b] = i;
150 outb(0x18, io[b] + 0x400 * EXP_PAGE0);
151 if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) {
152 pr_debug("Failed by test\n");
153 continue;
155 pr_debug("Passed\n");
156 break;
159 if(probe_exhasted) {
160 continue;
165 * See if we should probe for shared RAM
167 if(do_reset) {
168 pr_debug("Doing a SAFE probe reset\n");
169 outb(0xFF, io[b] + RESET_OFFSET);
170 set_current_state(TASK_INTERRUPTIBLE);
171 schedule_timeout(milliseconds(10000));
173 pr_debug("RAM Base for board %d is 0x%x, %s probe\n", b, ram[b],
174 ram[b] == 0 ? "will" : "won't");
176 if(ram[b]) {
178 * No, the RAM base has been provided
179 * Just look for a signature and ID the
180 * board model
182 if(request_region(ram[b], SRAM_PAGESIZE, "sc test")) {
183 pr_debug("request_region for RAM base 0x%x succeeded\n", ram[b]);
184 model = identify_board(ram[b], io[b]);
185 release_region(ram[b], SRAM_PAGESIZE);
188 else {
190 * Yes, probe for free RAM and look for
191 * a signature and id the board model
193 for (i = SRAM_MIN ; i < SRAM_MAX ; i += SRAM_PAGESIZE) {
194 pr_debug("Checking RAM address 0x%x...\n", i);
195 if(request_region(i, SRAM_PAGESIZE, "sc test")) {
196 pr_debug(" check_region succeeded\n");
197 model = identify_board(i, io[b]);
198 release_region(i, SRAM_PAGESIZE);
199 if (model >= 0) {
200 pr_debug(" Identified a %s\n",
201 boardname[model]);
202 ram[b] = i;
203 break;
205 pr_debug(" Unidentifed or inaccessible\n");
206 continue;
208 pr_debug(" request failed\n");
212 * See if we found free RAM and the board model
214 if(!ram[b] || model < 0) {
216 * Nope, there was no place in RAM for the
217 * board, or it couldn't be identified
219 pr_debug("Failed to find an adapter at 0x%x\n", ram[b]);
220 continue;
224 * Set the board's magic number, memory size and page register
226 switch(model) {
227 case PRI_BOARD:
228 channels = 23;
229 magic = 0x20000;
230 memsize = 0x100000;
231 features = PRI_FEATURES;
232 break;
234 case BRI_BOARD:
235 case POTS_BOARD:
236 channels = 2;
237 magic = 0x60000;
238 memsize = 0x10000;
239 features = BRI_FEATURES;
240 break;
242 switch(ram[b] >> 12 & 0x0F) {
243 case 0x0:
244 pr_debug("RAM Page register set to EXP_PAGE0\n");
245 pgport = EXP_PAGE0;
246 break;
248 case 0x4:
249 pr_debug("RAM Page register set to EXP_PAGE1\n");
250 pgport = EXP_PAGE1;
251 break;
253 case 0x8:
254 pr_debug("RAM Page register set to EXP_PAGE2\n");
255 pgport = EXP_PAGE2;
256 break;
258 case 0xC:
259 pr_debug("RAM Page register set to EXP_PAGE3\n");
260 pgport = EXP_PAGE3;
261 break;
263 default:
264 pr_debug("RAM base address doesn't fall on 16K boundary\n");
265 continue;
268 pr_debug("current IRQ: %d b: %d\n",irq[b],b);
271 * Make sure we got an IRQ
273 if(!irq[b]) {
275 * No interrupt could be used
277 pr_debug("Failed to acquire an IRQ line\n");
278 continue;
282 * Horray! We found a board, Make sure we can register
283 * it with ISDN4Linux
285 interface = kmalloc(sizeof(isdn_if), GFP_KERNEL);
286 if (interface == NULL) {
288 * Oops, can't malloc isdn_if
290 continue;
292 memset(interface, 0, sizeof(isdn_if));
294 interface->owner = THIS_MODULE;
295 interface->hl_hdrlen = 0;
296 interface->channels = channels;
297 interface->maxbufsize = BUFFER_SIZE;
298 interface->features = features;
299 interface->writebuf_skb = sndpkt;
300 interface->writecmd = NULL;
301 interface->command = command;
302 strcpy(interface->id, devname);
303 interface->id[2] = '0' + cinst;
306 * Allocate the board structure
308 sc_adapter[cinst] = kmalloc(sizeof(board), GFP_KERNEL);
309 if (sc_adapter[cinst] == NULL) {
311 * Oops, can't alloc memory for the board
313 kfree(interface);
314 continue;
316 memset(sc_adapter[cinst], 0, sizeof(board));
317 spin_lock_init(&sc_adapter[cinst]->lock);
319 if(!register_isdn(interface)) {
321 * Oops, couldn't register for some reason
323 kfree(interface);
324 kfree(sc_adapter[cinst]);
325 continue;
328 sc_adapter[cinst]->card = interface;
329 sc_adapter[cinst]->driverId = interface->channels;
330 strcpy(sc_adapter[cinst]->devicename, interface->id);
331 sc_adapter[cinst]->nChannels = channels;
332 sc_adapter[cinst]->ramsize = memsize;
333 sc_adapter[cinst]->shmem_magic = magic;
334 sc_adapter[cinst]->shmem_pgport = pgport;
335 sc_adapter[cinst]->StartOnReset = 1;
338 * Allocate channels status structures
340 sc_adapter[cinst]->channel = kmalloc(sizeof(bchan) * channels, GFP_KERNEL);
341 if (sc_adapter[cinst]->channel == NULL) {
343 * Oops, can't alloc memory for the channels
345 indicate_status(cinst, ISDN_STAT_UNLOAD, 0, NULL); /* Fix me */
346 kfree(interface);
347 kfree(sc_adapter[cinst]);
348 continue;
350 memset(sc_adapter[cinst]->channel, 0, sizeof(bchan) * channels);
353 * Lock down the hardware resources
355 sc_adapter[cinst]->interrupt = irq[b];
356 if (request_irq(sc_adapter[cinst]->interrupt, interrupt_handler,
357 SA_INTERRUPT, interface->id, NULL))
359 kfree(sc_adapter[cinst]->channel);
360 indicate_status(cinst, ISDN_STAT_UNLOAD, 0, NULL); /* Fix me */
361 kfree(interface);
362 kfree(sc_adapter[cinst]);
363 continue;
366 sc_adapter[cinst]->iobase = io[b];
367 for(i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
368 sc_adapter[cinst]->ioport[i] = io[b] + i * 0x400;
369 request_region(sc_adapter[cinst]->ioport[i], 1,
370 interface->id);
371 pr_debug("Requesting I/O Port %#x\n",
372 sc_adapter[cinst]->ioport[i]);
374 sc_adapter[cinst]->ioport[IRQ_SELECT] = io[b] + 0x2;
375 request_region(sc_adapter[cinst]->ioport[IRQ_SELECT], 1,
376 interface->id);
377 pr_debug("Requesting I/O Port %#x\n",
378 sc_adapter[cinst]->ioport[IRQ_SELECT]);
379 sc_adapter[cinst]->rambase = ram[b];
380 request_region(sc_adapter[cinst]->rambase, SRAM_PAGESIZE,
381 interface->id);
383 pr_info(" %s (%d) - %s %d channels IRQ %d, I/O Base 0x%x, RAM Base 0x%lx\n",
384 sc_adapter[cinst]->devicename,
385 sc_adapter[cinst]->driverId,
386 boardname[model], channels, irq[b], io[b], ram[b]);
389 * reset the adapter to put things in motion
391 reset(cinst);
393 cinst++;
394 status = 0;
396 if (status)
397 pr_info("Failed to find any adapters, driver unloaded\n");
398 return status;
401 static void __exit sc_exit(void)
403 int i, j;
405 for(i = 0 ; i < cinst ; i++) {
406 pr_debug("Cleaning up after adapter %d\n", i);
408 * kill the timers
410 del_timer(&(sc_adapter[i]->reset_timer));
411 del_timer(&(sc_adapter[i]->stat_timer));
414 * Tell I4L we're toast
416 indicate_status(i, ISDN_STAT_STOP, 0, NULL);
417 indicate_status(i, ISDN_STAT_UNLOAD, 0, NULL);
420 * Release shared RAM
422 release_region(sc_adapter[i]->rambase, SRAM_PAGESIZE);
425 * Release the IRQ
427 FREE_IRQ(sc_adapter[i]->interrupt, NULL);
430 * Reset for a clean start
432 outb(0xFF, sc_adapter[i]->ioport[SFT_RESET]);
435 * Release the I/O Port regions
437 for(j = 0 ; j < MAX_IO_REGS - 1; j++) {
438 release_region(sc_adapter[i]->ioport[j], 1);
439 pr_debug("Releasing I/O Port %#x\n",
440 sc_adapter[i]->ioport[j]);
442 release_region(sc_adapter[i]->ioport[IRQ_SELECT], 1);
443 pr_debug("Releasing I/O Port %#x\n",
444 sc_adapter[i]->ioport[IRQ_SELECT]);
447 * Release any memory we alloced
449 kfree(sc_adapter[i]->channel);
450 kfree(sc_adapter[i]->card);
451 kfree(sc_adapter[i]);
453 pr_info("SpellCaster ISA ISDN Adapter Driver Unloaded.\n");
456 int identify_board(unsigned long rambase, unsigned int iobase)
458 unsigned int pgport;
459 unsigned long sig;
460 DualPortMemory *dpm;
461 RspMessage rcvmsg;
462 ReqMessage sndmsg;
463 HWConfig_pl hwci;
464 int x;
466 pr_debug("Attempting to identify adapter @ 0x%x io 0x%x\n",
467 rambase, iobase);
470 * Enable the base pointer
472 outb(rambase >> 12, iobase + 0x2c00);
474 switch(rambase >> 12 & 0x0F) {
475 case 0x0:
476 pgport = iobase + PG0_OFFSET;
477 pr_debug("Page Register offset is 0x%x\n", PG0_OFFSET);
478 break;
480 case 0x4:
481 pgport = iobase + PG1_OFFSET;
482 pr_debug("Page Register offset is 0x%x\n", PG1_OFFSET);
483 break;
485 case 0x8:
486 pgport = iobase + PG2_OFFSET;
487 pr_debug("Page Register offset is 0x%x\n", PG2_OFFSET);
488 break;
490 case 0xC:
491 pgport = iobase + PG3_OFFSET;
492 pr_debug("Page Register offset is 0x%x\n", PG3_OFFSET);
493 break;
494 default:
495 pr_debug("Invalid rambase 0x%lx\n", rambase);
496 return -1;
500 * Try to identify a PRI card
502 outb(PRI_BASEPG_VAL, pgport);
503 set_current_state(TASK_INTERRUPTIBLE);
504 schedule_timeout(HZ);
505 sig = readl(rambase + SIG_OFFSET);
506 pr_debug("Looking for a signature, got 0x%x\n", sig);
507 if(sig == SIGNATURE)
508 return PRI_BOARD;
511 * Try to identify a PRI card
513 outb(BRI_BASEPG_VAL, pgport);
514 set_current_state(TASK_INTERRUPTIBLE);
515 schedule_timeout(HZ);
516 sig = readl(rambase + SIG_OFFSET);
517 pr_debug("Looking for a signature, got 0x%x\n", sig);
518 if(sig == SIGNATURE)
519 return BRI_BOARD;
521 return -1;
524 * Try to spot a card
526 sig = readl(rambase + SIG_OFFSET);
527 pr_debug("Looking for a signature, got 0x%x\n", sig);
528 if(sig != SIGNATURE)
529 return -1;
531 dpm = (DualPortMemory *) rambase;
533 memset(&sndmsg, 0, MSG_LEN);
534 sndmsg.msg_byte_cnt = 3;
535 sndmsg.type = cmReqType1;
536 sndmsg.class = cmReqClass0;
537 sndmsg.code = cmReqHWConfig;
538 memcpy_toio(&(dpm->req_queue[dpm->req_head++]), &sndmsg, MSG_LEN);
539 outb(0, iobase + 0x400);
540 pr_debug("Sent HWConfig message\n");
542 * Wait for the response
544 x = 0;
545 while((inb(iobase + FIFOSTAT_OFFSET) & RF_HAS_DATA) && x < 100) {
546 set_current_state(TASK_INTERRUPTIBLE);
547 schedule_timeout(1);
548 x++;
550 if(x == 100) {
551 pr_debug("Timeout waiting for response\n");
552 return -1;
555 memcpy_fromio(&rcvmsg, &(dpm->rsp_queue[dpm->rsp_tail]), MSG_LEN);
556 pr_debug("Got HWConfig response, status = 0x%x\n", rcvmsg.rsp_status);
557 memcpy(&hwci, &(rcvmsg.msg_data.HWCresponse), sizeof(HWConfig_pl));
558 pr_debug("Hardware Config: Interface: %s, RAM Size: %d, Serial: %s\n"
559 " Part: %s, Rev: %s\n",
560 hwci.st_u_sense ? "S/T" : "U", hwci.ram_size,
561 hwci.serial_no, hwci.part_no, hwci.rev_no);
563 if(!strncmp(PRI_PARTNO, hwci.part_no, 6))
564 return PRI_BOARD;
565 if(!strncmp(BRI_PARTNO, hwci.part_no, 6))
566 return BRI_BOARD;
568 return -1;
571 module_init(sc_init);
572 module_exit(sc_exit);