init.c 13 KB

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