File: /usr/src/linux/drivers/isdn/sc/init.c

1     #include <linux/module.h>
2     #include <linux/init.h>
3     #include "includes.h"
4     #include "hardware.h"
5     #include "card.h"
6     
7     board *adapter[MAX_CARDS];
8     int cinst;
9     
10     static char devname[] = "scX";
11     const char version[] = "2.0b1";
12     
13     const char *boardname[] = { "DataCommute/BRI", "DataCommute/PRI", "TeleCommute/BRI" };
14     
15     /* insmod set parameters */
16     static unsigned int io[] = {0,0,0,0};
17     static unsigned char irq[] = {0,0,0,0};
18     static unsigned long ram[] = {0,0,0,0};
19     static int do_reset = 0;
20     
21     static int sup_irq[] = { 11, 10, 9, 5, 12, 14, 7, 3, 4, 6 };
22     #define MAX_IRQS	10
23     
24     extern void interrupt_handler(int, void *, struct pt_regs *);
25     extern int sndpkt(int, int, int, struct sk_buff *);
26     extern int command(isdn_ctrl *);
27     extern int indicate_status(int, int, ulong, char*);
28     extern int reset(int);
29     
30     int identify_board(unsigned long, unsigned int);
31     
32     int irq_supported(int irq_x)
33     {
34     	int i;
35     	for(i=0 ; i < MAX_IRQS ; i++) {
36     		if(sup_irq[i] == irq_x)
37     			return 1;
38     	}
39     	return 0;
40     }
41     
42     MODULE_PARM(io, "1-4i");
43     MODULE_PARM(irq, "1-4i");
44     MODULE_PARM(ram, "1-4i");
45     MODULE_PARM(do_reset, "i");
46     
47     static int __init sc_init(void)
48     {
49     	int b = -1;
50     	int i, j;
51     	int status = -ENODEV;
52     
53     	unsigned long memsize = 0;
54     	unsigned long features = 0;
55     	isdn_if *interface;
56     	unsigned char channels;
57     	unsigned char pgport;
58     	unsigned long magic;
59     	int model;
60     	int last_base = IOBASE_MIN;
61     	int probe_exhasted = 0;
62     
63     #ifdef MODULE
64     	pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s Loaded\n", version);
65     #else
66     	pr_info("SpellCaster ISA ISDN Adapter Driver rev. %s\n", version);
67     #endif
68     	pr_info("Copyright (C) 1996 SpellCaster Telecommunications Inc.\n");
69     
70     	while(b++ < MAX_CARDS - 1) {
71     		pr_debug("Probing for adapter #%d\n", b);
72     		/*
73     		 * Initialize reusable variables
74     		 */
75     		model = -1;
76     		magic = 0;
77     		channels = 0;
78     		pgport = 0;
79     
80     		/* 
81     		 * See if we should probe for IO base 
82     		 */
83     		pr_debug("I/O Base for board %d is 0x%x, %s probe\n", b, io[b],
84     			io[b] == 0 ? "will" : "won't");
85     		if(io[b]) {
86     			/*
87     			 * No, I/O Base has been provided
88     			 */
89     			for (i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
90     				if(check_region(io[b] + i * 0x400, 1)) {
91     					pr_debug("check_region for 0x%x failed\n", io[b] + i * 0x400);
92     					io[b] = 0;
93     					break;
94     				}
95     			}
96     
97     			/*
98     			 * Confirm the I/O Address with a test
99     			 */
100     			if(io[b] == 0) {
101     				pr_debug("I/O Address 0x%x is in use.\n");
102     				continue;
103     			}
104     
105     			outb(0x18, io[b] + 0x400 * EXP_PAGE0);
106     			if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) {
107     				pr_debug("I/O Base 0x%x fails test\n");
108     				continue;
109     			}
110     		}
111     		else {
112     			/*
113     			 * Yes, probe for I/O Base
114     			 */
115     			if(probe_exhasted) {
116     				pr_debug("All probe addresses exhasted, skipping\n");
117     				continue;
118     			}
119     			pr_debug("Probing for I/O...\n");
120     			for (i = last_base ; i <= IOBASE_MAX ; i += IOBASE_OFFSET) {
121     				int found_io = 1;
122     				if (i == IOBASE_MAX) {
123     					probe_exhasted = 1; /* No more addresses to probe */
124     					pr_debug("End of Probes\n");
125     				}
126     				last_base = i + IOBASE_OFFSET;
127     				pr_debug("  checking 0x%x...", i);
128     				for ( j = 0 ; j < MAX_IO_REGS - 1 ; j++) {
129     					if(check_region(i + j * 0x400, 1)) {
130     						pr_debug("Failed\n");
131     						found_io = 0;
132     						break;
133     					}
134     				}	
135     
136     				if(found_io) {
137     					io[b] = i;
138     					outb(0x18, io[b] + 0x400 * EXP_PAGE0);
139     					if(inb(io[b] + 0x400 * EXP_PAGE0) != 0x18) { 
140     						pr_debug("Failed by test\n");
141     						continue;
142     					}
143     					pr_debug("Passed\n");
144     					break;
145     				}
146     			}
147     			if(probe_exhasted) {
148     				continue;
149     			}
150     		}
151     
152     		/*
153     		 * See if we should probe for shared RAM
154     		 */
155     		if(do_reset) {
156     			pr_debug("Doing a SAFE probe reset\n");
157     			outb(0xFF, io[b] + RESET_OFFSET);
158     			set_current_state(TASK_INTERRUPTIBLE);
159     			schedule_timeout(milliseconds(10000));
160     		}
161     		pr_debug("RAM Base for board %d is 0x%x, %s probe\n", b, ram[b],
162     			ram[b] == 0 ? "will" : "won't");
163     
164     		if(ram[b]) {
165     			/*
166     			 * No, the RAM base has been provided
167     			 * Just look for a signature and ID the
168     			 * board model
169     			 */
170     			if(!check_region(ram[b], SRAM_PAGESIZE)) {
171     				pr_debug("check_region for RAM base 0x%x succeeded\n", ram[b]);
172     			 	model = identify_board(ram[b], io[b]);
173     			}
174     		}
175     		else {
176     			/*
177     			 * Yes, probe for free RAM and look for
178     			 * a signature and id the board model
179     			 */
180     			for (i = SRAM_MIN ; i < SRAM_MAX ; i += SRAM_PAGESIZE) {
181     				pr_debug("Checking RAM address 0x%x...\n", i);
182     				if(!check_region(i, SRAM_PAGESIZE)) {
183     					pr_debug("  check_region succeeded\n");
184     					model = identify_board(i, io[b]);
185     					if (model >= 0) {
186     						pr_debug("  Identified a %s\n",
187     							boardname[model]);
188     						ram[b] = i;
189     						break;
190     					}
191     					pr_debug("  Unidentifed or inaccessible\n");
192     					continue;
193     				}
194     				pr_debug("  check_region failed\n");
195     			}
196     		}
197     		/*
198     		 * See if we found free RAM and the board model
199     		 */
200     		if(!ram[b] || model < 0) {
201     			/*
202     			 * Nope, there was no place in RAM for the
203     			 * board, or it couldn't be identified
204     			 */
205     			 pr_debug("Failed to find an adapter at 0x%x\n", ram[b]);
206     			 continue;
207     		}
208     
209     		/*
210     		 * Set the board's magic number, memory size and page register
211     		 */
212     		switch(model) {
213     		case PRI_BOARD:
214     			channels = 23;
215     			magic = 0x20000;
216     			memsize = 0x100000;
217     			features = PRI_FEATURES;
218     			break;
219     
220     		case BRI_BOARD:
221     		case POTS_BOARD:
222     			channels = 2;
223     			magic = 0x60000;
224     			memsize = 0x10000;
225     			features = BRI_FEATURES;
226     			break;
227     		}
228     		switch(ram[b] >> 12 & 0x0F) {
229     		case 0x0:
230     			pr_debug("RAM Page register set to EXP_PAGE0\n");
231     			pgport = EXP_PAGE0;
232     			break;
233     
234     		case 0x4:
235     			pr_debug("RAM Page register set to EXP_PAGE1\n");
236     			pgport = EXP_PAGE1;
237     			break;
238     
239     		case 0x8:
240     			pr_debug("RAM Page register set to EXP_PAGE2\n");
241     			pgport = EXP_PAGE2;
242     			break;
243     
244     		case 0xC:
245     			pr_debug("RAM Page register set to EXP_PAGE3\n");
246     			pgport = EXP_PAGE3;
247     			break;
248     
249     		default:
250     			pr_debug("RAM base address doesn't fall on 16K boundary\n");
251     			continue;
252     		}
253     
254     		pr_debug("current IRQ: %d  b: %d\n",irq[b],b);
255     		/*
256     		 * See if we should probe for an irq
257     		 */
258     		if(irq[b]) {
259     			/*
260     			 * No we were given one
261     			 * See that it is supported and free
262     			 */
263     			pr_debug("Trying for IRQ: %d\n",irq[b]);
264     			if (irq_supported(irq[b])) {
265     				if(REQUEST_IRQ(irq[b], interrupt_handler, 
266     					SA_PROBE, "sc_probe", NULL)) {
267     					pr_debug("IRQ %d is already in use\n", 
268     						irq[b]);
269     					continue;
270     				}
271     				FREE_IRQ(irq[b], NULL);
272     			}
273     		}
274     		else {
275     			/*
276     			 * Yes, we need to probe for an IRQ
277     			 */
278     			pr_debug("Probing for IRQ...\n");
279     			for (i = 0; i < MAX_IRQS ; i++) {
280     				if(!REQUEST_IRQ(sup_irq[i], interrupt_handler, SA_PROBE, "sc_probe", NULL)) {
281     					pr_debug("Probed for and found IRQ %d\n", sup_irq[i]);
282     					FREE_IRQ(sup_irq[i], NULL);
283     					irq[b] = sup_irq[i];
284     					break;
285     				}
286     			}
287     		}
288     
289     		/*
290     		 * Make sure we got an IRQ
291     		 */
292     		if(!irq[b]) {
293     			/*
294     			 * No interrupt could be used
295     			 */
296     			pr_debug("Failed to acquire an IRQ line\n");
297     			continue;
298     		}
299     
300     		/*
301     		 * Horray! We found a board, Make sure we can register
302     		 * it with ISDN4Linux
303     		 */
304     		interface = kmalloc(sizeof(isdn_if), GFP_KERNEL);
305     		if (interface == NULL) {
306     			/*
307     			 * Oops, can't malloc isdn_if
308     			 */
309     			continue;
310     		}
311     		memset(interface, 0, sizeof(isdn_if));
312     
313     		interface->hl_hdrlen = 0;
314     		interface->channels = channels;
315     		interface->maxbufsize = BUFFER_SIZE;
316     		interface->features = features;
317     		interface->writebuf_skb = sndpkt;
318     		interface->writecmd = NULL;
319     		interface->command = command;
320     		strcpy(interface->id, devname);
321     		interface->id[2] = '0' + cinst;
322     
323     		/*
324     		 * Allocate the board structure
325     		 */
326     		adapter[cinst] = kmalloc(sizeof(board), GFP_KERNEL);
327     		if (adapter[cinst] == NULL) {
328     			/*
329     			 * Oops, can't alloc memory for the board
330     			 */
331     			kfree(interface);
332     			continue;
333     		}
334     		memset(adapter[cinst], 0, sizeof(board));
335     
336     		if(!register_isdn(interface)) {
337     			/*
338     			 * Oops, couldn't register for some reason
339     			 */
340     			kfree(interface);
341     			kfree(adapter[cinst]);
342     			continue;
343     		}
344     
345     		adapter[cinst]->card = interface;
346     		adapter[cinst]->driverId = interface->channels;
347     		strcpy(adapter[cinst]->devicename, interface->id);
348     		adapter[cinst]->nChannels = channels;
349     		adapter[cinst]->ramsize = memsize;
350     		adapter[cinst]->shmem_magic = magic;
351     		adapter[cinst]->shmem_pgport = pgport;
352     		adapter[cinst]->StartOnReset = 1;
353     
354     		/*
355     		 * Allocate channels status structures
356     		 */
357     		adapter[cinst]->channel = kmalloc(sizeof(bchan) * channels, GFP_KERNEL);
358     		if (adapter[cinst]->channel == NULL) {
359     			/*
360     			 * Oops, can't alloc memory for the channels
361     			 */
362     			indicate_status(cinst, ISDN_STAT_UNLOAD, 0, NULL);	/* Fix me */
363     			kfree(interface);
364     			kfree(adapter[cinst]);
365     			continue;
366     		}
367     		memset(adapter[cinst]->channel, 0, sizeof(bchan) * channels);
368     
369     		/*
370     		 * Lock down the hardware resources
371     		 */
372     		adapter[cinst]->interrupt = irq[b];
373     		REQUEST_IRQ(adapter[cinst]->interrupt, interrupt_handler, SA_INTERRUPT, 
374     			interface->id, NULL);
375     		adapter[cinst]->iobase = io[b];
376     		for(i = 0 ; i < MAX_IO_REGS - 1 ; i++) {
377     			adapter[cinst]->ioport[i] = io[b] + i * 0x400;
378     			request_region(adapter[cinst]->ioport[i], 1, interface->id);
379     			pr_debug("Requesting I/O Port %#x\n", adapter[cinst]->ioport[i]);
380     		}
381     		adapter[cinst]->ioport[IRQ_SELECT] = io[b] + 0x2;
382     		request_region(adapter[cinst]->ioport[IRQ_SELECT], 1, interface->id);
383     		pr_debug("Requesting I/O Port %#x\n", adapter[cinst]->ioport[IRQ_SELECT]);
384     		adapter[cinst]->rambase = ram[b];
385     		request_region(adapter[cinst]->rambase, SRAM_PAGESIZE, interface->id);
386     
387     		pr_info("  %s (%d) - %s %d channels IRQ %d, I/O Base 0x%x, RAM Base 0x%lx\n", 
388     			adapter[cinst]->devicename, adapter[cinst]->driverId, 
389     			boardname[model], channels, irq[b], io[b], ram[b]);
390     		
391     		/*
392     		 * reset the adapter to put things in motion
393     		 */
394     		reset(cinst);
395     
396     		cinst++;
397     		status = 0;
398     	}
399     	if (status) 
400     		pr_info("Failed to find any adapters, driver unloaded\n");
401     	return status;
402     }
403     
404     static void __exit sc_exit(void)
405     {
406     	int i, j;
407     
408     	for(i = 0 ; i < cinst ; i++) {
409     		pr_debug("Cleaning up after adapter %d\n", i);
410     		/*
411     		 * kill the timers
412     		 */
413     		del_timer(&(adapter[i]->reset_timer));
414     		del_timer(&(adapter[i]->stat_timer));
415     
416     		/*
417     		 * Tell I4L we're toast
418     		 */
419     		indicate_status(i, ISDN_STAT_STOP, 0, NULL);
420     		indicate_status(i, ISDN_STAT_UNLOAD, 0, NULL);
421     
422     		/*
423     		 * Release shared RAM
424     		 */
425     		release_region(adapter[i]->rambase, SRAM_PAGESIZE);
426     
427     		/*
428     		 * Release the IRQ
429     		 */
430     		FREE_IRQ(adapter[i]->interrupt, NULL);
431     
432     		/*
433     		 * Reset for a clean start
434     		 */
435     		outb(0xFF, adapter[i]->ioport[SFT_RESET]);
436     
437     		/*
438     		 * Release the I/O Port regions
439     		 */
440     		for(j = 0 ; j < MAX_IO_REGS - 1; j++) {
441     			release_region(adapter[i]->ioport[j], 1);
442     			pr_debug("Releasing I/O Port %#x\n", adapter[i]->ioport[j]);
443     		}
444     		release_region(adapter[i]->ioport[IRQ_SELECT], 1);
445     		pr_debug("Releasing I/O Port %#x\n", adapter[i]->ioport[IRQ_SELECT]);
446     
447     		/*
448     		 * Release any memory we alloced
449     		 */
450     		kfree(adapter[i]->channel);
451     		kfree(adapter[i]->card);
452     		kfree(adapter[i]);
453     	}
454     	pr_info("SpellCaster ISA ISDN Adapter Driver Unloaded.\n");
455     }
456     
457     int identify_board(unsigned long rambase, unsigned int iobase) 
458     {
459     	unsigned int pgport;
460     	unsigned long sig;
461     	DualPortMemory *dpm;
462     	RspMessage rcvmsg;
463     	ReqMessage sndmsg;
464     	HWConfig_pl hwci;
465     	int x;
466     
467     	pr_debug("Attempting to identify adapter @ 0x%x io 0x%x\n",
468     		rambase, iobase);
469     
470     	/*
471     	 * Enable the base pointer
472     	 */
473     	outb(rambase >> 12, iobase + 0x2c00);
474     
475     	switch(rambase >> 12 & 0x0F) {
476     	case 0x0:
477     		pgport = iobase + PG0_OFFSET;
478     		pr_debug("Page Register offset is 0x%x\n", PG0_OFFSET);
479     		break;
480     		
481     	case 0x4:
482     		pgport = iobase + PG1_OFFSET;
483     		pr_debug("Page Register offset is 0x%x\n", PG1_OFFSET);
484     		break;
485     
486     	case 0x8:
487     		pgport = iobase + PG2_OFFSET;
488     		pr_debug("Page Register offset is 0x%x\n", PG2_OFFSET);
489     		break;
490     
491     	case 0xC:
492     		pgport = iobase + PG3_OFFSET;
493     		pr_debug("Page Register offset is 0x%x\n", PG3_OFFSET);
494     		break;
495     	default:
496     		pr_debug("Invalid rambase 0x%lx\n", rambase);
497     		return -1;
498     	}
499     
500     	/*
501     	 * Try to identify a PRI card
502     	 */
503     	outb(PRI_BASEPG_VAL, pgport);
504     	set_current_state(TASK_INTERRUPTIBLE);
505     	schedule_timeout(HZ);
506     	sig = readl(rambase + SIG_OFFSET);
507     	pr_debug("Looking for a signature, got 0x%x\n", sig);
508     	if(sig == SIGNATURE)
509     		return PRI_BOARD;
510     
511     	/*
512     	 * Try to identify a PRI card
513     	 */
514     	outb(BRI_BASEPG_VAL, pgport);
515     	set_current_state(TASK_INTERRUPTIBLE);
516     	schedule_timeout(HZ);
517     	sig = readl(rambase + SIG_OFFSET);
518     	pr_debug("Looking for a signature, got 0x%x\n", sig);
519     	if(sig == SIGNATURE)
520     		return BRI_BOARD;
521     
522     	return -1;
523     
524     	/*
525     	 * Try to spot a card
526     	 */
527     	sig = readl(rambase + SIG_OFFSET);
528     	pr_debug("Looking for a signature, got 0x%x\n", sig);
529     	if(sig != SIGNATURE)
530     		return -1;
531     
532     	dpm = (DualPortMemory *) rambase;
533     
534     	memset(&sndmsg, 0, MSG_LEN);
535     	sndmsg.msg_byte_cnt = 3;
536     	sndmsg.type = cmReqType1;
537     	sndmsg.class = cmReqClass0;
538     	sndmsg.code = cmReqHWConfig;
539     	memcpy_toio(&(dpm->req_queue[dpm->req_head++]), &sndmsg, MSG_LEN);
540     	outb(0, iobase + 0x400);
541     	pr_debug("Sent HWConfig message\n");
542     	/*
543     	 * Wait for the response
544     	 */
545     	x = 0;
546     	while((inb(iobase + FIFOSTAT_OFFSET) & RF_HAS_DATA) && x < 100) {
547     		set_current_state(TASK_INTERRUPTIBLE);
548     		schedule_timeout(1);
549     		x++;
550     	}
551     	if(x == 100) {
552     		pr_debug("Timeout waiting for response\n");
553     		return -1;
554     	}
555     
556     	memcpy_fromio(&rcvmsg, &(dpm->rsp_queue[dpm->rsp_tail]), MSG_LEN);
557     	pr_debug("Got HWConfig response, status = 0x%x\n", rcvmsg.rsp_status);
558     	memcpy(&hwci, &(rcvmsg.msg_data.HWCresponse), sizeof(HWConfig_pl));
559     	pr_debug("Hardware Config: Interface: %s, RAM Size: %d, Serial: %s\n"
560     		 "                 Part: %s, Rev: %s\n",
561     		 hwci.st_u_sense ? "S/T" : "U", hwci.ram_size,
562     		 hwci.serial_no, hwci.part_no, hwci.rev_no);
563     
564     	if(!strncmp(PRI_PARTNO, hwci.part_no, 6))
565     		return PRI_BOARD;
566     	if(!strncmp(BRI_PARTNO, hwci.part_no, 6))
567     		return BRI_BOARD;
568     		
569     	return -1;
570     }
571     
572     module_init(sc_init);
573     module_exit(sc_exit);
574