act2000_isa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* $Id: act2000_isa.c,v 1.11.6.3 2001/09/23 22:24:32 kai Exp $
  2. *
  3. * ISDN lowlevel-module for the IBM ISDN-S0 Active 2000 (ISA-Version).
  4. *
  5. * Author Fritz Elfert
  6. * Copyright by Fritz Elfert <fritz@isdn4linux.de>
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. * Thanks to Friedemann Baitinger and IBM Germany
  12. *
  13. */
  14. #include "act2000.h"
  15. #include "act2000_isa.h"
  16. #include "capi.h"
  17. /*
  18. * Reset Controller, then try to read the Card's signature.
  19. + Return:
  20. * 1 = Signature found.
  21. * 0 = Signature not found.
  22. */
  23. static int
  24. act2000_isa_reset(unsigned short portbase)
  25. {
  26. unsigned char reg;
  27. int i;
  28. int found;
  29. int serial = 0;
  30. found = 0;
  31. if ((reg = inb(portbase + ISA_COR)) != 0xff) {
  32. outb(reg | ISA_COR_RESET, portbase + ISA_COR);
  33. mdelay(10);
  34. outb(reg, portbase + ISA_COR);
  35. mdelay(10);
  36. for (i = 0; i < 16; i++) {
  37. if (inb(portbase + ISA_ISR) & ISA_ISR_SERIAL)
  38. serial |= 0x10000;
  39. serial >>= 1;
  40. }
  41. if (serial == ISA_SER_ID)
  42. found++;
  43. }
  44. return found;
  45. }
  46. int
  47. act2000_isa_detect(unsigned short portbase)
  48. {
  49. int ret = 0;
  50. if (request_region(portbase, ACT2000_PORTLEN, "act2000isa")) {
  51. ret = act2000_isa_reset(portbase);
  52. release_region(portbase, ISA_REGION);
  53. }
  54. return ret;
  55. }
  56. static irqreturn_t
  57. act2000_isa_interrupt(int dummy, void *dev_id)
  58. {
  59. act2000_card *card = dev_id;
  60. u_char istatus;
  61. istatus = (inb(ISA_PORT_ISR) & 0x07);
  62. if (istatus & ISA_ISR_OUT) {
  63. /* RX fifo has data */
  64. istatus &= ISA_ISR_OUT_MASK;
  65. outb(0, ISA_PORT_SIS);
  66. act2000_isa_receive(card);
  67. outb(ISA_SIS_INT, ISA_PORT_SIS);
  68. }
  69. if (istatus & ISA_ISR_ERR) {
  70. /* Error Interrupt */
  71. istatus &= ISA_ISR_ERR_MASK;
  72. printk(KERN_WARNING "act2000: errIRQ\n");
  73. }
  74. if (istatus)
  75. printk(KERN_DEBUG "act2000: ?IRQ %d %02x\n", card->irq, istatus);
  76. return IRQ_HANDLED;
  77. }
  78. static void
  79. act2000_isa_select_irq(act2000_card * card)
  80. {
  81. unsigned char reg;
  82. reg = (inb(ISA_PORT_COR) & ~ISA_COR_IRQOFF) | ISA_COR_PERR;
  83. switch (card->irq) {
  84. case 3:
  85. reg = ISA_COR_IRQ03;
  86. break;
  87. case 5:
  88. reg = ISA_COR_IRQ05;
  89. break;
  90. case 7:
  91. reg = ISA_COR_IRQ07;
  92. break;
  93. case 10:
  94. reg = ISA_COR_IRQ10;
  95. break;
  96. case 11:
  97. reg = ISA_COR_IRQ11;
  98. break;
  99. case 12:
  100. reg = ISA_COR_IRQ12;
  101. break;
  102. case 15:
  103. reg = ISA_COR_IRQ15;
  104. break;
  105. }
  106. outb(reg, ISA_PORT_COR);
  107. }
  108. static void
  109. act2000_isa_enable_irq(act2000_card * card)
  110. {
  111. act2000_isa_select_irq(card);
  112. /* Enable READ irq */
  113. outb(ISA_SIS_INT, ISA_PORT_SIS);
  114. }
  115. /*
  116. * Install interrupt handler, enable irq on card.
  117. * If irq is -1, choose next free irq, else irq is given explicitly.
  118. */
  119. int
  120. act2000_isa_config_irq(act2000_card * card, short irq)
  121. {
  122. int old_irq;
  123. if (card->flags & ACT2000_FLAGS_IVALID) {
  124. free_irq(card->irq, card);
  125. }
  126. card->flags &= ~ACT2000_FLAGS_IVALID;
  127. outb(ISA_COR_IRQOFF, ISA_PORT_COR);
  128. if (!irq)
  129. return 0;
  130. old_irq = card->irq;
  131. card->irq = irq;
  132. if (request_irq(irq, &act2000_isa_interrupt, 0, card->regname, card)) {
  133. card->irq = old_irq;
  134. card->flags |= ACT2000_FLAGS_IVALID;
  135. printk(KERN_WARNING
  136. "act2000: Could not request irq %d\n",irq);
  137. return -EBUSY;
  138. } else {
  139. act2000_isa_select_irq(card);
  140. /* Disable READ and WRITE irq */
  141. outb(0, ISA_PORT_SIS);
  142. outb(0, ISA_PORT_SOS);
  143. }
  144. return 0;
  145. }
  146. int
  147. act2000_isa_config_port(act2000_card * card, unsigned short portbase)
  148. {
  149. if (card->flags & ACT2000_FLAGS_PVALID) {
  150. release_region(card->port, ISA_REGION);
  151. card->flags &= ~ACT2000_FLAGS_PVALID;
  152. }
  153. if (request_region(portbase, ACT2000_PORTLEN, card->regname) == NULL)
  154. return -EBUSY;
  155. else {
  156. card->port = portbase;
  157. card->flags |= ACT2000_FLAGS_PVALID;
  158. return 0;
  159. }
  160. }
  161. /*
  162. * Release ressources, used by an adaptor.
  163. */
  164. void
  165. act2000_isa_release(act2000_card * card)
  166. {
  167. unsigned long flags;
  168. spin_lock_irqsave(&card->lock, flags);
  169. if (card->flags & ACT2000_FLAGS_IVALID)
  170. free_irq(card->irq, card);
  171. card->flags &= ~ACT2000_FLAGS_IVALID;
  172. if (card->flags & ACT2000_FLAGS_PVALID)
  173. release_region(card->port, ISA_REGION);
  174. card->flags &= ~ACT2000_FLAGS_PVALID;
  175. spin_unlock_irqrestore(&card->lock, flags);
  176. }
  177. static int
  178. act2000_isa_writeb(act2000_card * card, u_char data)
  179. {
  180. u_char timeout = 40;
  181. while (timeout) {
  182. if (inb(ISA_PORT_SOS) & ISA_SOS_READY) {
  183. outb(data, ISA_PORT_SDO);
  184. return 0;
  185. } else {
  186. timeout--;
  187. udelay(10);
  188. }
  189. }
  190. return 1;
  191. }
  192. static int
  193. act2000_isa_readb(act2000_card * card, u_char * data)
  194. {
  195. u_char timeout = 40;
  196. while (timeout) {
  197. if (inb(ISA_PORT_SIS) & ISA_SIS_READY) {
  198. *data = inb(ISA_PORT_SDI);
  199. return 0;
  200. } else {
  201. timeout--;
  202. udelay(10);
  203. }
  204. }
  205. return 1;
  206. }
  207. void
  208. act2000_isa_receive(act2000_card *card)
  209. {
  210. u_char c;
  211. if (test_and_set_bit(ACT2000_LOCK_RX, (void *) &card->ilock) != 0)
  212. return;
  213. while (!act2000_isa_readb(card, &c)) {
  214. if (card->idat.isa.rcvidx < 8) {
  215. card->idat.isa.rcvhdr[card->idat.isa.rcvidx++] = c;
  216. if (card->idat.isa.rcvidx == 8) {
  217. int valid = actcapi_chkhdr(card, (actcapi_msghdr *)&card->idat.isa.rcvhdr);
  218. if (valid) {
  219. card->idat.isa.rcvlen = ((actcapi_msghdr *)&card->idat.isa.rcvhdr)->len;
  220. card->idat.isa.rcvskb = dev_alloc_skb(card->idat.isa.rcvlen);
  221. if (card->idat.isa.rcvskb == NULL) {
  222. card->idat.isa.rcvignore = 1;
  223. printk(KERN_WARNING
  224. "act2000_isa_receive: no memory\n");
  225. test_and_clear_bit(ACT2000_LOCK_RX, (void *) &card->ilock);
  226. return;
  227. }
  228. memcpy(skb_put(card->idat.isa.rcvskb, 8), card->idat.isa.rcvhdr, 8);
  229. card->idat.isa.rcvptr = skb_put(card->idat.isa.rcvskb, card->idat.isa.rcvlen - 8);
  230. } else {
  231. card->idat.isa.rcvidx = 0;
  232. printk(KERN_WARNING
  233. "act2000_isa_receive: Invalid CAPI msg\n");
  234. {
  235. int i; __u8 *p; __u8 *t; __u8 tmp[30];
  236. for (i = 0, p = (__u8 *)&card->idat.isa.rcvhdr, t = tmp; i < 8; i++)
  237. t += sprintf(t, "%02x ", *(p++));
  238. printk(KERN_WARNING "act2000_isa_receive: %s\n", tmp);
  239. }
  240. }
  241. }
  242. } else {
  243. if (!card->idat.isa.rcvignore)
  244. *card->idat.isa.rcvptr++ = c;
  245. if (++card->idat.isa.rcvidx >= card->idat.isa.rcvlen) {
  246. if (!card->idat.isa.rcvignore) {
  247. skb_queue_tail(&card->rcvq, card->idat.isa.rcvskb);
  248. act2000_schedule_rx(card);
  249. }
  250. card->idat.isa.rcvidx = 0;
  251. card->idat.isa.rcvlen = 8;
  252. card->idat.isa.rcvignore = 0;
  253. card->idat.isa.rcvskb = NULL;
  254. card->idat.isa.rcvptr = card->idat.isa.rcvhdr;
  255. }
  256. }
  257. }
  258. if (!(card->flags & ACT2000_FLAGS_IVALID)) {
  259. /* In polling mode, schedule myself */
  260. if ((card->idat.isa.rcvidx) &&
  261. (card->idat.isa.rcvignore ||
  262. (card->idat.isa.rcvidx < card->idat.isa.rcvlen)))
  263. act2000_schedule_poll(card);
  264. }
  265. test_and_clear_bit(ACT2000_LOCK_RX, (void *) &card->ilock);
  266. }
  267. void
  268. act2000_isa_send(act2000_card * card)
  269. {
  270. unsigned long flags;
  271. struct sk_buff *skb;
  272. actcapi_msg *msg;
  273. int l;
  274. if (test_and_set_bit(ACT2000_LOCK_TX, (void *) &card->ilock) != 0)
  275. return;
  276. while (1) {
  277. spin_lock_irqsave(&card->lock, flags);
  278. if (!(card->sbuf)) {
  279. if ((card->sbuf = skb_dequeue(&card->sndq))) {
  280. card->ack_msg = card->sbuf->data;
  281. msg = (actcapi_msg *)card->sbuf->data;
  282. if ((msg->hdr.cmd.cmd == 0x86) &&
  283. (msg->hdr.cmd.subcmd == 0) ) {
  284. /* Save flags in message */
  285. card->need_b3ack = msg->msg.data_b3_req.flags;
  286. msg->msg.data_b3_req.flags = 0;
  287. }
  288. }
  289. }
  290. spin_unlock_irqrestore(&card->lock, flags);
  291. if (!(card->sbuf)) {
  292. /* No more data to send */
  293. test_and_clear_bit(ACT2000_LOCK_TX, (void *) &card->ilock);
  294. return;
  295. }
  296. skb = card->sbuf;
  297. l = 0;
  298. while (skb->len) {
  299. if (act2000_isa_writeb(card, *(skb->data))) {
  300. /* Fifo is full, but more data to send */
  301. test_and_clear_bit(ACT2000_LOCK_TX, (void *) &card->ilock);
  302. /* Schedule myself */
  303. act2000_schedule_tx(card);
  304. return;
  305. }
  306. skb_pull(skb, 1);
  307. l++;
  308. }
  309. msg = (actcapi_msg *)card->ack_msg;
  310. if ((msg->hdr.cmd.cmd == 0x86) &&
  311. (msg->hdr.cmd.subcmd == 0) ) {
  312. /*
  313. * If it's user data, reset data-ptr
  314. * and put skb into ackq.
  315. */
  316. skb->data = card->ack_msg;
  317. /* Restore flags in message */
  318. msg->msg.data_b3_req.flags = card->need_b3ack;
  319. skb_queue_tail(&card->ackq, skb);
  320. } else
  321. dev_kfree_skb(skb);
  322. card->sbuf = NULL;
  323. }
  324. }
  325. /*
  326. * Get firmware ID, check for 'ISDN' signature.
  327. */
  328. static int
  329. act2000_isa_getid(act2000_card * card)
  330. {
  331. act2000_fwid fid;
  332. u_char *p = (u_char *) & fid;
  333. int count = 0;
  334. while (1) {
  335. if (count > 510)
  336. return -EPROTO;
  337. if (act2000_isa_readb(card, p++))
  338. break;
  339. count++;
  340. }
  341. if (count <= 20) {
  342. printk(KERN_WARNING "act2000: No Firmware-ID!\n");
  343. return -ETIME;
  344. }
  345. *p = '\0';
  346. fid.revlen[0] = '\0';
  347. if (strcmp(fid.isdn, "ISDN")) {
  348. printk(KERN_WARNING "act2000: Wrong Firmware-ID!\n");
  349. return -EPROTO;
  350. }
  351. if ((p = strchr(fid.revision, '\n')))
  352. *p = '\0';
  353. printk(KERN_INFO "act2000: Firmware-ID: %s\n", fid.revision);
  354. if (card->flags & ACT2000_FLAGS_IVALID) {
  355. printk(KERN_DEBUG "Enabling Interrupts ...\n");
  356. act2000_isa_enable_irq(card);
  357. }
  358. return 0;
  359. }
  360. /*
  361. * Download microcode into card, check Firmware signature.
  362. */
  363. int
  364. act2000_isa_download(act2000_card * card, act2000_ddef __user * cb)
  365. {
  366. unsigned int length;
  367. int l;
  368. int c;
  369. long timeout;
  370. u_char *b;
  371. u_char __user *p;
  372. u_char *buf;
  373. act2000_ddef cblock;
  374. if (!act2000_isa_reset(card->port))
  375. return -ENXIO;
  376. msleep_interruptible(500);
  377. if (copy_from_user(&cblock, cb, sizeof(cblock)))
  378. return -EFAULT;
  379. length = cblock.length;
  380. p = cblock.buffer;
  381. if (!access_ok(VERIFY_READ, p, length))
  382. return -EFAULT;
  383. buf = kmalloc(1024, GFP_KERNEL);
  384. if (!buf)
  385. return -ENOMEM;
  386. timeout = 0;
  387. while (length) {
  388. l = (length > 1024) ? 1024 : length;
  389. c = 0;
  390. b = buf;
  391. if (copy_from_user(buf, p, l)) {
  392. kfree(buf);
  393. return -EFAULT;
  394. }
  395. while (c < l) {
  396. if (act2000_isa_writeb(card, *b++)) {
  397. printk(KERN_WARNING
  398. "act2000: loader timed out"
  399. " len=%d c=%d\n", length, c);
  400. kfree(buf);
  401. return -ETIME;
  402. }
  403. c++;
  404. }
  405. length -= l;
  406. p += l;
  407. }
  408. kfree(buf);
  409. msleep_interruptible(500);
  410. return (act2000_isa_getid(card));
  411. }