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