init.c 13 KB

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