drv.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /*
  2. * PCBIT-D interface with isdn4linux
  3. *
  4. * Copyright (C) 1996 Universidade de Lisboa
  5. *
  6. * Written by Pedro Roque Marques (roque@di.fc.ul.pt)
  7. *
  8. * This software may be used and distributed according to the terms of
  9. * the GNU General Public License, incorporated herein by reference.
  10. */
  11. /*
  12. * Fixes:
  13. *
  14. * Nuno Grilo <l38486@alfa.ist.utl.pt>
  15. * fixed msn_list NULL pointer dereference.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/types.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/string.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/isdnif.h>
  28. #include <asm/string.h>
  29. #include <asm/io.h>
  30. #include <linux/ioport.h>
  31. #include "pcbit.h"
  32. #include "edss1.h"
  33. #include "layer2.h"
  34. #include "capi.h"
  35. extern ushort last_ref_num;
  36. static int pcbit_ioctl(isdn_ctrl* ctl);
  37. static char* pcbit_devname[MAX_PCBIT_CARDS] = {
  38. "pcbit0",
  39. "pcbit1",
  40. "pcbit2",
  41. "pcbit3"
  42. };
  43. /*
  44. * prototypes
  45. */
  46. static int pcbit_command(isdn_ctrl* ctl);
  47. static int pcbit_stat(u_char __user * buf, int len, int, int);
  48. static int pcbit_xmit(int driver, int chan, int ack, struct sk_buff *skb);
  49. static int pcbit_writecmd(const u_char __user *, int, int, int);
  50. static int set_protocol_running(struct pcbit_dev * dev);
  51. static void pcbit_clear_msn(struct pcbit_dev *dev);
  52. static void pcbit_set_msn(struct pcbit_dev *dev, char *list);
  53. static int pcbit_check_msn(struct pcbit_dev *dev, char *msn);
  54. int pcbit_init_dev(int board, int mem_base, int irq)
  55. {
  56. struct pcbit_dev *dev;
  57. isdn_if *dev_if;
  58. if ((dev=kzalloc(sizeof(struct pcbit_dev), GFP_KERNEL)) == NULL)
  59. {
  60. printk("pcbit_init: couldn't malloc pcbit_dev struct\n");
  61. return -ENOMEM;
  62. }
  63. dev_pcbit[board] = dev;
  64. init_waitqueue_head(&dev->set_running_wq);
  65. spin_lock_init(&dev->lock);
  66. if (mem_base >= 0xA0000 && mem_base <= 0xFFFFF ) {
  67. dev->ph_mem = mem_base;
  68. if (!request_mem_region(dev->ph_mem, 4096, "PCBIT mem")) {
  69. printk(KERN_WARNING
  70. "PCBIT: memory region %lx-%lx already in use\n",
  71. dev->ph_mem, dev->ph_mem + 4096);
  72. kfree(dev);
  73. dev_pcbit[board] = NULL;
  74. return -EACCES;
  75. }
  76. dev->sh_mem = ioremap(dev->ph_mem, 4096);
  77. }
  78. else
  79. {
  80. printk("memory address invalid");
  81. kfree(dev);
  82. dev_pcbit[board] = NULL;
  83. return -EACCES;
  84. }
  85. dev->b1 = kzalloc(sizeof(struct pcbit_chan), GFP_KERNEL);
  86. if (!dev->b1) {
  87. printk("pcbit_init: couldn't malloc pcbit_chan struct\n");
  88. iounmap(dev->sh_mem);
  89. release_mem_region(dev->ph_mem, 4096);
  90. kfree(dev);
  91. return -ENOMEM;
  92. }
  93. dev->b2 = kzalloc(sizeof(struct pcbit_chan), GFP_KERNEL);
  94. if (!dev->b2) {
  95. printk("pcbit_init: couldn't malloc pcbit_chan struct\n");
  96. kfree(dev->b1);
  97. iounmap(dev->sh_mem);
  98. release_mem_region(dev->ph_mem, 4096);
  99. kfree(dev);
  100. return -ENOMEM;
  101. }
  102. dev->b2->id = 1;
  103. INIT_WORK(&dev->qdelivery, pcbit_deliver);
  104. /*
  105. * interrupts
  106. */
  107. if (request_irq(irq, &pcbit_irq_handler, 0, pcbit_devname[board], dev) != 0)
  108. {
  109. kfree(dev->b1);
  110. kfree(dev->b2);
  111. iounmap(dev->sh_mem);
  112. release_mem_region(dev->ph_mem, 4096);
  113. kfree(dev);
  114. dev_pcbit[board] = NULL;
  115. return -EIO;
  116. }
  117. dev->irq = irq;
  118. /* next frame to be received */
  119. dev->rcv_seq = 0;
  120. dev->send_seq = 0;
  121. dev->unack_seq = 0;
  122. dev->hl_hdrlen = 16;
  123. dev_if = kmalloc(sizeof(isdn_if), GFP_KERNEL);
  124. if (!dev_if) {
  125. free_irq(irq, dev);
  126. kfree(dev->b1);
  127. kfree(dev->b2);
  128. iounmap(dev->sh_mem);
  129. release_mem_region(dev->ph_mem, 4096);
  130. kfree(dev);
  131. dev_pcbit[board] = NULL;
  132. return -EIO;
  133. }
  134. dev->dev_if = dev_if;
  135. dev_if->owner = THIS_MODULE;
  136. dev_if->channels = 2;
  137. dev_if->features = (ISDN_FEATURE_P_EURO | ISDN_FEATURE_L3_TRANS |
  138. ISDN_FEATURE_L2_HDLC | ISDN_FEATURE_L2_TRANS );
  139. dev_if->writebuf_skb = pcbit_xmit;
  140. dev_if->hl_hdrlen = 16;
  141. dev_if->maxbufsize = MAXBUFSIZE;
  142. dev_if->command = pcbit_command;
  143. dev_if->writecmd = pcbit_writecmd;
  144. dev_if->readstat = pcbit_stat;
  145. strcpy(dev_if->id, pcbit_devname[board]);
  146. if (!register_isdn(dev_if)) {
  147. free_irq(irq, dev);
  148. kfree(dev->b1);
  149. kfree(dev->b2);
  150. iounmap(dev->sh_mem);
  151. release_mem_region(dev->ph_mem, 4096);
  152. kfree(dev);
  153. dev_pcbit[board] = NULL;
  154. return -EIO;
  155. }
  156. dev->id = dev_if->channels;
  157. dev->l2_state = L2_DOWN;
  158. dev->free = 511;
  159. /*
  160. * set_protocol_running(dev);
  161. */
  162. return 0;
  163. }
  164. #ifdef MODULE
  165. void pcbit_terminate(int board)
  166. {
  167. struct pcbit_dev * dev;
  168. dev = dev_pcbit[board];
  169. if (dev) {
  170. /* unregister_isdn(dev->dev_if); */
  171. free_irq(dev->irq, dev);
  172. pcbit_clear_msn(dev);
  173. kfree(dev->dev_if);
  174. if (dev->b1->fsm_timer.function)
  175. del_timer(&dev->b1->fsm_timer);
  176. if (dev->b2->fsm_timer.function)
  177. del_timer(&dev->b2->fsm_timer);
  178. kfree(dev->b1);
  179. kfree(dev->b2);
  180. iounmap(dev->sh_mem);
  181. release_mem_region(dev->ph_mem, 4096);
  182. kfree(dev);
  183. }
  184. }
  185. #endif
  186. static int pcbit_command(isdn_ctrl* ctl)
  187. {
  188. struct pcbit_dev *dev;
  189. struct pcbit_chan *chan;
  190. struct callb_data info;
  191. dev = finddev(ctl->driver);
  192. if (!dev)
  193. {
  194. printk("pcbit_command: unknown device\n");
  195. return -1;
  196. }
  197. chan = (ctl->arg & 0x0F) ? dev->b2 : dev->b1;
  198. switch(ctl->command) {
  199. case ISDN_CMD_IOCTL:
  200. return pcbit_ioctl(ctl);
  201. break;
  202. case ISDN_CMD_DIAL:
  203. info.type = EV_USR_SETUP_REQ;
  204. info.data.setup.CalledPN = (char *) &ctl->parm.setup.phone;
  205. pcbit_fsm_event(dev, chan, EV_USR_SETUP_REQ, &info);
  206. break;
  207. case ISDN_CMD_ACCEPTD:
  208. pcbit_fsm_event(dev, chan, EV_USR_SETUP_RESP, NULL);
  209. break;
  210. case ISDN_CMD_ACCEPTB:
  211. printk("ISDN_CMD_ACCEPTB - not really needed\n");
  212. break;
  213. case ISDN_CMD_HANGUP:
  214. pcbit_fsm_event(dev, chan, EV_USR_RELEASE_REQ, NULL);
  215. break;
  216. case ISDN_CMD_SETL2:
  217. chan->proto = (ctl->arg >> 8);
  218. break;
  219. case ISDN_CMD_CLREAZ:
  220. pcbit_clear_msn(dev);
  221. break;
  222. case ISDN_CMD_SETEAZ:
  223. pcbit_set_msn(dev, ctl->parm.num);
  224. break;
  225. case ISDN_CMD_SETL3:
  226. if ((ctl->arg >> 8) != ISDN_PROTO_L3_TRANS)
  227. printk(KERN_DEBUG "L3 protocol unknown\n");
  228. break;
  229. default:
  230. printk(KERN_DEBUG "pcbit_command: unknown command\n");
  231. break;
  232. };
  233. return 0;
  234. }
  235. /*
  236. * Another Hack :-(
  237. * on some conditions the board stops sending TDATA_CONFs
  238. * let's see if we can turn around the problem
  239. */
  240. #ifdef BLOCK_TIMER
  241. static void pcbit_block_timer(unsigned long data)
  242. {
  243. struct pcbit_chan *chan;
  244. struct pcbit_dev * dev;
  245. isdn_ctrl ictl;
  246. chan = (struct pcbit_chan *) data;
  247. dev = chan2dev(chan);
  248. if (dev == NULL) {
  249. printk(KERN_DEBUG "pcbit: chan2dev failed\n");
  250. return;
  251. }
  252. del_timer(&chan->block_timer);
  253. chan->block_timer.function = NULL;
  254. #ifdef DEBUG
  255. printk(KERN_DEBUG "pcbit_block_timer\n");
  256. #endif
  257. chan->queued = 0;
  258. ictl.driver = dev->id;
  259. ictl.command = ISDN_STAT_BSENT;
  260. ictl.arg = chan->id;
  261. dev->dev_if->statcallb(&ictl);
  262. }
  263. #endif
  264. static int pcbit_xmit(int driver, int chnum, int ack, struct sk_buff *skb)
  265. {
  266. ushort hdrlen;
  267. int refnum, len;
  268. struct pcbit_chan * chan;
  269. struct pcbit_dev *dev;
  270. dev = finddev(driver);
  271. if (dev == NULL)
  272. {
  273. printk("finddev returned NULL");
  274. return -1;
  275. }
  276. chan = chnum ? dev->b2 : dev->b1;
  277. if (chan->fsm_state != ST_ACTIVE)
  278. return -1;
  279. if (chan->queued >= MAX_QUEUED )
  280. {
  281. #ifdef DEBUG_QUEUE
  282. printk(KERN_DEBUG
  283. "pcbit: %d packets already in queue - write fails\n",
  284. chan->queued);
  285. #endif
  286. /*
  287. * packet stays on the head of the device queue
  288. * since dev_start_xmit will fail
  289. * see net/core/dev.c
  290. */
  291. #ifdef BLOCK_TIMER
  292. if (chan->block_timer.function == NULL) {
  293. init_timer(&chan->block_timer);
  294. chan->block_timer.function = &pcbit_block_timer;
  295. chan->block_timer.data = (long) chan;
  296. chan->block_timer.expires = jiffies + 1 * HZ;
  297. add_timer(&chan->block_timer);
  298. }
  299. #endif
  300. return 0;
  301. }
  302. chan->queued++;
  303. len = skb->len;
  304. hdrlen = capi_tdata_req(chan, skb);
  305. refnum = last_ref_num++ & 0x7fffU;
  306. chan->s_refnum = refnum;
  307. pcbit_l2_write(dev, MSG_TDATA_REQ, refnum, skb, hdrlen);
  308. return len;
  309. }
  310. static int pcbit_writecmd(const u_char __user *buf, int len, int driver, int channel)
  311. {
  312. struct pcbit_dev * dev;
  313. int i, j;
  314. const u_char * loadbuf;
  315. u_char * ptr = NULL;
  316. u_char *cbuf;
  317. int errstat;
  318. dev = finddev(driver);
  319. if (!dev)
  320. {
  321. printk("pcbit_writecmd: couldn't find device");
  322. return -ENODEV;
  323. }
  324. switch(dev->l2_state) {
  325. case L2_LWMODE:
  326. /* check (size <= rdp_size); write buf into board */
  327. if (len < 0 || len > BANK4 + 1 || len > 1024)
  328. {
  329. printk("pcbit_writecmd: invalid length %d\n", len);
  330. return -EINVAL;
  331. }
  332. cbuf = memdup_user(buf, len);
  333. if (IS_ERR(cbuf))
  334. return PTR_ERR(cbuf);
  335. memcpy_toio(dev->sh_mem, cbuf, len);
  336. kfree(cbuf);
  337. return len;
  338. case L2_FWMODE:
  339. /* this is the hard part */
  340. /* dumb board */
  341. /* get it into kernel space */
  342. if ((ptr = kmalloc(len, GFP_KERNEL))==NULL)
  343. return -ENOMEM;
  344. if (copy_from_user(ptr, buf, len)) {
  345. kfree(ptr);
  346. return -EFAULT;
  347. }
  348. loadbuf = ptr;
  349. errstat = 0;
  350. for (i=0; i < len; i++)
  351. {
  352. for(j=0; j < LOAD_RETRY; j++)
  353. if (!(readb(dev->sh_mem + dev->loadptr)))
  354. break;
  355. if (j == LOAD_RETRY)
  356. {
  357. errstat = -ETIME;
  358. printk("TIMEOUT i=%d\n", i);
  359. break;
  360. }
  361. writeb(loadbuf[i], dev->sh_mem + dev->loadptr + 1);
  362. writeb(0x01, dev->sh_mem + dev->loadptr);
  363. dev->loadptr += 2;
  364. if (dev->loadptr > LOAD_ZONE_END)
  365. dev->loadptr = LOAD_ZONE_START;
  366. }
  367. kfree(ptr);
  368. return errstat ? errstat : len;
  369. default:
  370. return -EBUSY;
  371. }
  372. }
  373. /*
  374. * demultiplexing of messages
  375. *
  376. */
  377. void pcbit_l3_receive(struct pcbit_dev * dev, ulong msg,
  378. struct sk_buff * skb,
  379. ushort hdr_len, ushort refnum)
  380. {
  381. struct pcbit_chan *chan;
  382. struct sk_buff *skb2;
  383. unsigned short len;
  384. struct callb_data cbdata;
  385. int complete, err;
  386. isdn_ctrl ictl;
  387. switch(msg) {
  388. case MSG_TDATA_IND:
  389. if (!(chan = capi_channel(dev, skb))) {
  390. printk(KERN_WARNING
  391. "CAPI header: unknown channel id\n");
  392. break;
  393. }
  394. chan->r_refnum = skb->data[7];
  395. skb_pull(skb, 8);
  396. dev->dev_if->rcvcallb_skb(dev->id, chan->id, skb);
  397. if (capi_tdata_resp(chan, &skb2) > 0)
  398. pcbit_l2_write(dev, MSG_TDATA_RESP, refnum,
  399. skb2, skb2->len);
  400. return;
  401. break;
  402. case MSG_TDATA_CONF:
  403. if (!(chan = capi_channel(dev, skb))) {
  404. printk(KERN_WARNING
  405. "CAPI header: unknown channel id\n");
  406. break;
  407. }
  408. #ifdef DEBUG
  409. if ( (*((ushort *) (skb->data + 2) )) != 0) {
  410. printk(KERN_DEBUG "TDATA_CONF error\n");
  411. }
  412. #endif
  413. #ifdef BLOCK_TIMER
  414. if (chan->queued == MAX_QUEUED) {
  415. del_timer(&chan->block_timer);
  416. chan->block_timer.function = NULL;
  417. }
  418. #endif
  419. chan->queued--;
  420. ictl.driver = dev->id;
  421. ictl.command = ISDN_STAT_BSENT;
  422. ictl.arg = chan->id;
  423. dev->dev_if->statcallb(&ictl);
  424. break;
  425. case MSG_CONN_IND:
  426. /*
  427. * channel: 1st not used will do
  428. * if both are used we're in trouble
  429. */
  430. if (!dev->b1->fsm_state)
  431. chan = dev->b1;
  432. else if (!dev->b2->fsm_state)
  433. chan = dev->b2;
  434. else {
  435. printk(KERN_INFO
  436. "Incoming connection: no channels available");
  437. if ((len = capi_disc_req(*(ushort*)(skb->data), &skb2, CAUSE_NOCHAN)) > 0)
  438. pcbit_l2_write(dev, MSG_DISC_REQ, refnum, skb2, len);
  439. break;
  440. }
  441. cbdata.data.setup.CalledPN = NULL;
  442. cbdata.data.setup.CallingPN = NULL;
  443. capi_decode_conn_ind(chan, skb, &cbdata);
  444. cbdata.type = EV_NET_SETUP;
  445. pcbit_fsm_event(dev, chan, EV_NET_SETUP, NULL);
  446. if (pcbit_check_msn(dev, cbdata.data.setup.CallingPN))
  447. pcbit_fsm_event(dev, chan, EV_USR_PROCED_REQ, &cbdata);
  448. else
  449. pcbit_fsm_event(dev, chan, EV_USR_RELEASE_REQ, NULL);
  450. kfree(cbdata.data.setup.CalledPN);
  451. kfree(cbdata.data.setup.CallingPN);
  452. break;
  453. case MSG_CONN_CONF:
  454. /*
  455. * We should be able to find the channel by the message
  456. * reference number. The current version of the firmware
  457. * doesn't sent the ref number correctly.
  458. */
  459. #ifdef DEBUG
  460. printk(KERN_DEBUG "refnum=%04x b1=%04x b2=%04x\n", refnum,
  461. dev->b1->s_refnum,
  462. dev->b2->s_refnum);
  463. #endif
  464. /* We just try to find a channel in the right state */
  465. if (dev->b1->fsm_state == ST_CALL_INIT)
  466. chan = dev->b1;
  467. else {
  468. if (dev->b2->s_refnum == ST_CALL_INIT)
  469. chan = dev->b2;
  470. else {
  471. chan = NULL;
  472. printk(KERN_WARNING "Connection Confirm - no channel in Call Init state\n");
  473. break;
  474. }
  475. }
  476. if (capi_decode_conn_conf(chan, skb, &complete)) {
  477. printk(KERN_DEBUG "conn_conf indicates error\n");
  478. pcbit_fsm_event(dev, chan, EV_ERROR, NULL);
  479. }
  480. else
  481. if (complete)
  482. pcbit_fsm_event(dev, chan, EV_NET_CALL_PROC, NULL);
  483. else
  484. pcbit_fsm_event(dev, chan, EV_NET_SETUP_ACK, NULL);
  485. break;
  486. case MSG_CONN_ACTV_IND:
  487. if (!(chan = capi_channel(dev, skb))) {
  488. printk(KERN_WARNING
  489. "CAPI header: unknown channel id\n");
  490. break;
  491. }
  492. if (capi_decode_conn_actv_ind(chan, skb)) {
  493. printk("error in capi_decode_conn_actv_ind\n");
  494. /* pcbit_fsm_event(dev, chan, EV_ERROR, NULL); */
  495. break;
  496. }
  497. chan->r_refnum = refnum;
  498. pcbit_fsm_event(dev, chan, EV_NET_CONN, NULL);
  499. break;
  500. case MSG_CONN_ACTV_CONF:
  501. if (!(chan = capi_channel(dev, skb))) {
  502. printk(KERN_WARNING
  503. "CAPI header: unknown channel id\n");
  504. break;
  505. }
  506. if (capi_decode_conn_actv_conf(chan, skb) == 0)
  507. pcbit_fsm_event(dev, chan, EV_NET_CONN_ACK, NULL);
  508. else
  509. printk(KERN_DEBUG "decode_conn_actv_conf failed\n");
  510. break;
  511. case MSG_SELP_CONF:
  512. if (!(chan = capi_channel(dev, skb))) {
  513. printk(KERN_WARNING
  514. "CAPI header: unknown channel id\n");
  515. break;
  516. }
  517. if (!(err = capi_decode_sel_proto_conf(chan, skb)))
  518. pcbit_fsm_event(dev, chan, EV_NET_SELP_RESP, NULL);
  519. else {
  520. /* Error */
  521. printk("error %d - capi_decode_sel_proto_conf\n", err);
  522. }
  523. break;
  524. case MSG_ACT_TRANSP_CONF:
  525. if (!(chan = capi_channel(dev, skb))) {
  526. printk(KERN_WARNING
  527. "CAPI header: unknown channel id\n");
  528. break;
  529. }
  530. if (!capi_decode_actv_trans_conf(chan, skb))
  531. pcbit_fsm_event(dev, chan, EV_NET_ACTV_RESP, NULL);
  532. break;
  533. case MSG_DISC_IND:
  534. if (!(chan = capi_channel(dev, skb))) {
  535. printk(KERN_WARNING
  536. "CAPI header: unknown channel id\n");
  537. break;
  538. }
  539. if (!capi_decode_disc_ind(chan, skb))
  540. pcbit_fsm_event(dev, chan, EV_NET_DISC, NULL);
  541. else
  542. printk(KERN_WARNING "capi_decode_disc_ind - error\n");
  543. break;
  544. case MSG_DISC_CONF:
  545. if (!(chan = capi_channel(dev, skb))) {
  546. printk(KERN_WARNING
  547. "CAPI header: unknown channel id\n");
  548. break;
  549. }
  550. if (!capi_decode_disc_ind(chan, skb))
  551. pcbit_fsm_event(dev, chan, EV_NET_RELEASE, NULL);
  552. else
  553. printk(KERN_WARNING "capi_decode_disc_conf - error\n");
  554. break;
  555. case MSG_INFO_IND:
  556. #ifdef DEBUG
  557. printk(KERN_DEBUG "received Info Indication - discarded\n");
  558. #endif
  559. break;
  560. #ifdef DEBUG
  561. case MSG_DEBUG_188:
  562. capi_decode_debug_188(skb->data, skb->len);
  563. break;
  564. default:
  565. printk(KERN_DEBUG "pcbit_l3_receive: unknown message %08lx\n",
  566. msg);
  567. break;
  568. #endif
  569. }
  570. kfree_skb(skb);
  571. }
  572. /*
  573. * Single statbuf
  574. * should be a statbuf per device
  575. */
  576. static char statbuf[STATBUF_LEN];
  577. static int stat_st = 0;
  578. static int stat_end = 0;
  579. static int pcbit_stat(u_char __user *buf, int len, int driver, int channel)
  580. {
  581. int stat_count;
  582. stat_count = stat_end - stat_st;
  583. if (stat_count < 0)
  584. stat_count = STATBUF_LEN - stat_st + stat_end;
  585. /* FIXME: should we sleep and wait for more cookies ? */
  586. if (len > stat_count)
  587. len = stat_count;
  588. if (stat_st < stat_end)
  589. {
  590. if (copy_to_user(buf, statbuf + stat_st, len))
  591. return -EFAULT;
  592. stat_st += len;
  593. }
  594. else
  595. {
  596. if (len > STATBUF_LEN - stat_st)
  597. {
  598. if (copy_to_user(buf, statbuf + stat_st,
  599. STATBUF_LEN - stat_st))
  600. return -EFAULT;
  601. if (copy_to_user(buf, statbuf,
  602. len - (STATBUF_LEN - stat_st)))
  603. return -EFAULT;
  604. stat_st = len - (STATBUF_LEN - stat_st);
  605. }
  606. else
  607. {
  608. if (copy_to_user(buf, statbuf + stat_st, len))
  609. return -EFAULT;
  610. stat_st += len;
  611. if (stat_st == STATBUF_LEN)
  612. stat_st = 0;
  613. }
  614. }
  615. if (stat_st == stat_end)
  616. stat_st = stat_end = 0;
  617. return len;
  618. }
  619. static void pcbit_logstat(struct pcbit_dev *dev, char *str)
  620. {
  621. int i;
  622. isdn_ctrl ictl;
  623. for (i=stat_end; i<strlen(str); i++)
  624. {
  625. statbuf[i]=str[i];
  626. stat_end = (stat_end + 1) % STATBUF_LEN;
  627. if (stat_end == stat_st)
  628. stat_st = (stat_st + 1) % STATBUF_LEN;
  629. }
  630. ictl.command=ISDN_STAT_STAVAIL;
  631. ictl.driver=dev->id;
  632. ictl.arg=strlen(str);
  633. dev->dev_if->statcallb(&ictl);
  634. }
  635. void pcbit_state_change(struct pcbit_dev * dev, struct pcbit_chan * chan,
  636. unsigned short i, unsigned short ev, unsigned short f)
  637. {
  638. char buf[256];
  639. sprintf(buf, "change on device: %d channel:%d\n%s -> %s -> %s\n",
  640. dev->id, chan->id,
  641. isdn_state_table[i], strisdnevent(ev), isdn_state_table[f]
  642. );
  643. #ifdef DEBUG
  644. printk("%s", buf);
  645. #endif
  646. pcbit_logstat(dev, buf);
  647. }
  648. static void set_running_timeout(unsigned long ptr)
  649. {
  650. struct pcbit_dev * dev;
  651. #ifdef DEBUG
  652. printk(KERN_DEBUG "set_running_timeout\n");
  653. #endif
  654. dev = (struct pcbit_dev *) ptr;
  655. wake_up_interruptible(&dev->set_running_wq);
  656. }
  657. static int set_protocol_running(struct pcbit_dev * dev)
  658. {
  659. isdn_ctrl ctl;
  660. init_timer(&dev->set_running_timer);
  661. dev->set_running_timer.function = &set_running_timeout;
  662. dev->set_running_timer.data = (ulong) dev;
  663. dev->set_running_timer.expires = jiffies + SET_RUN_TIMEOUT;
  664. /* kick it */
  665. dev->l2_state = L2_STARTING;
  666. writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
  667. dev->sh_mem + BANK4);
  668. add_timer(&dev->set_running_timer);
  669. interruptible_sleep_on(&dev->set_running_wq);
  670. del_timer(&dev->set_running_timer);
  671. if (dev->l2_state == L2_RUNNING)
  672. {
  673. printk(KERN_DEBUG "pcbit: running\n");
  674. dev->unack_seq = dev->send_seq;
  675. dev->writeptr = dev->sh_mem;
  676. dev->readptr = dev->sh_mem + BANK2;
  677. /* tell the good news to the upper layer */
  678. ctl.driver = dev->id;
  679. ctl.command = ISDN_STAT_RUN;
  680. dev->dev_if->statcallb(&ctl);
  681. }
  682. else
  683. {
  684. printk(KERN_DEBUG "pcbit: initialization failed\n");
  685. printk(KERN_DEBUG "pcbit: firmware not loaded\n");
  686. dev->l2_state = L2_DOWN;
  687. #ifdef DEBUG
  688. printk(KERN_DEBUG "Bank3 = %02x\n",
  689. readb(dev->sh_mem + BANK3));
  690. #endif
  691. writeb(0x40, dev->sh_mem + BANK4);
  692. /* warn the upper layer */
  693. ctl.driver = dev->id;
  694. ctl.command = ISDN_STAT_STOP;
  695. dev->dev_if->statcallb(&ctl);
  696. return -EL2HLT; /* Level 2 halted */
  697. }
  698. return 0;
  699. }
  700. static int pcbit_ioctl(isdn_ctrl* ctl)
  701. {
  702. struct pcbit_dev * dev;
  703. struct pcbit_ioctl *cmd;
  704. dev = finddev(ctl->driver);
  705. if (!dev)
  706. {
  707. printk(KERN_DEBUG "pcbit_ioctl: unknown device\n");
  708. return -ENODEV;
  709. }
  710. cmd = (struct pcbit_ioctl *) ctl->parm.num;
  711. switch(ctl->arg) {
  712. case PCBIT_IOCTL_GETSTAT:
  713. cmd->info.l2_status = dev->l2_state;
  714. break;
  715. case PCBIT_IOCTL_STRLOAD:
  716. if (dev->l2_state == L2_RUNNING)
  717. return -EBUSY;
  718. dev->unack_seq = dev->send_seq = dev->rcv_seq = 0;
  719. dev->writeptr = dev->sh_mem;
  720. dev->readptr = dev->sh_mem + BANK2;
  721. dev->l2_state = L2_LOADING;
  722. break;
  723. case PCBIT_IOCTL_LWMODE:
  724. if (dev->l2_state != L2_LOADING)
  725. return -EINVAL;
  726. dev->l2_state = L2_LWMODE;
  727. break;
  728. case PCBIT_IOCTL_FWMODE:
  729. if (dev->l2_state == L2_RUNNING)
  730. return -EBUSY;
  731. dev->loadptr = LOAD_ZONE_START;
  732. dev->l2_state = L2_FWMODE;
  733. break;
  734. case PCBIT_IOCTL_ENDLOAD:
  735. if (dev->l2_state == L2_RUNNING)
  736. return -EBUSY;
  737. dev->l2_state = L2_DOWN;
  738. break;
  739. case PCBIT_IOCTL_SETBYTE:
  740. if (dev->l2_state == L2_RUNNING)
  741. return -EBUSY;
  742. /* check addr */
  743. if (cmd->info.rdp_byte.addr > BANK4)
  744. return -EFAULT;
  745. writeb(cmd->info.rdp_byte.value, dev->sh_mem + cmd->info.rdp_byte.addr);
  746. break;
  747. case PCBIT_IOCTL_GETBYTE:
  748. if (dev->l2_state == L2_RUNNING)
  749. return -EBUSY;
  750. /* check addr */
  751. if (cmd->info.rdp_byte.addr > BANK4)
  752. {
  753. printk("getbyte: invalid addr %04x\n", cmd->info.rdp_byte.addr);
  754. return -EFAULT;
  755. }
  756. cmd->info.rdp_byte.value = readb(dev->sh_mem + cmd->info.rdp_byte.addr);
  757. break;
  758. case PCBIT_IOCTL_RUNNING:
  759. if (dev->l2_state == L2_RUNNING)
  760. return -EBUSY;
  761. return set_protocol_running(dev);
  762. break;
  763. case PCBIT_IOCTL_WATCH188:
  764. if (dev->l2_state != L2_LOADING)
  765. return -EINVAL;
  766. pcbit_l2_write(dev, MSG_WATCH188, 0x0001, NULL, 0);
  767. break;
  768. case PCBIT_IOCTL_PING188:
  769. if (dev->l2_state != L2_LOADING)
  770. return -EINVAL;
  771. pcbit_l2_write(dev, MSG_PING188_REQ, 0x0001, NULL, 0);
  772. break;
  773. case PCBIT_IOCTL_APION:
  774. if (dev->l2_state != L2_LOADING)
  775. return -EINVAL;
  776. pcbit_l2_write(dev, MSG_API_ON, 0x0001, NULL, 0);
  777. break;
  778. case PCBIT_IOCTL_STOP:
  779. dev->l2_state = L2_DOWN;
  780. writeb(0x40, dev->sh_mem + BANK4);
  781. dev->rcv_seq = 0;
  782. dev->send_seq = 0;
  783. dev->unack_seq = 0;
  784. break;
  785. default:
  786. printk("error: unknown ioctl\n");
  787. break;
  788. };
  789. return 0;
  790. }
  791. /*
  792. * MSN list handling
  793. *
  794. * if null reject all calls
  795. * if first entry has null MSN accept all calls
  796. */
  797. static void pcbit_clear_msn(struct pcbit_dev *dev)
  798. {
  799. struct msn_entry *ptr, *back;
  800. for (ptr=dev->msn_list; ptr; )
  801. {
  802. back = ptr->next;
  803. kfree(ptr);
  804. ptr = back;
  805. }
  806. dev->msn_list = NULL;
  807. }
  808. static void pcbit_set_msn(struct pcbit_dev *dev, char *list)
  809. {
  810. struct msn_entry *ptr;
  811. struct msn_entry *back = NULL;
  812. char *cp, *sp;
  813. int len;
  814. if (strlen(list) == 0) {
  815. ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
  816. if (!ptr) {
  817. printk(KERN_WARNING "kmalloc failed\n");
  818. return;
  819. }
  820. ptr->msn = NULL;
  821. ptr->next = dev->msn_list;
  822. dev->msn_list = ptr;
  823. return;
  824. }
  825. if (dev->msn_list)
  826. for (back=dev->msn_list; back->next; back=back->next);
  827. sp = list;
  828. do {
  829. cp=strchr(sp, ',');
  830. if (cp)
  831. len = cp - sp;
  832. else
  833. len = strlen(sp);
  834. ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
  835. if (!ptr) {
  836. printk(KERN_WARNING "kmalloc failed\n");
  837. return;
  838. }
  839. ptr->next = NULL;
  840. ptr->msn = kmalloc(len, GFP_ATOMIC);
  841. if (!ptr->msn) {
  842. printk(KERN_WARNING "kmalloc failed\n");
  843. kfree(ptr);
  844. return;
  845. }
  846. memcpy(ptr->msn, sp, len - 1);
  847. ptr->msn[len] = 0;
  848. #ifdef DEBUG
  849. printk(KERN_DEBUG "msn: %s\n", ptr->msn);
  850. #endif
  851. if (dev->msn_list == NULL)
  852. dev->msn_list = ptr;
  853. else
  854. back->next = ptr;
  855. back = ptr;
  856. sp += len;
  857. } while(cp);
  858. }
  859. /*
  860. * check if we do signal or reject an incoming call
  861. */
  862. static int pcbit_check_msn(struct pcbit_dev *dev, char *msn)
  863. {
  864. struct msn_entry *ptr;
  865. for (ptr=dev->msn_list; ptr; ptr=ptr->next) {
  866. if (ptr->msn == NULL)
  867. return 1;
  868. if (strcmp(ptr->msn, msn) == 0)
  869. return 1;
  870. }
  871. return 0;
  872. }