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