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