init.c 13 KB

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