init.c 13 KB

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