drv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. dev->l2_state = L2_DOWN;
  656. wake_up_interruptible(&dev->set_running_wq);
  657. }
  658. static int set_protocol_running(struct pcbit_dev *dev)
  659. {
  660. isdn_ctrl ctl;
  661. init_timer(&dev->set_running_timer);
  662. dev->set_running_timer.function = &set_running_timeout;
  663. dev->set_running_timer.data = (ulong) dev;
  664. dev->set_running_timer.expires = jiffies + SET_RUN_TIMEOUT;
  665. /* kick it */
  666. dev->l2_state = L2_STARTING;
  667. writeb((0x80U | ((dev->rcv_seq & 0x07) << 3) | (dev->send_seq & 0x07)),
  668. dev->sh_mem + BANK4);
  669. add_timer(&dev->set_running_timer);
  670. wait_event(dev->set_running_wq, dev->l2_state == L2_RUNNING ||
  671. dev->l2_state == L2_DOWN);
  672. del_timer(&dev->set_running_timer);
  673. if (dev->l2_state == L2_RUNNING)
  674. {
  675. printk(KERN_DEBUG "pcbit: running\n");
  676. dev->unack_seq = dev->send_seq;
  677. dev->writeptr = dev->sh_mem;
  678. dev->readptr = dev->sh_mem + BANK2;
  679. /* tell the good news to the upper layer */
  680. ctl.driver = dev->id;
  681. ctl.command = ISDN_STAT_RUN;
  682. dev->dev_if->statcallb(&ctl);
  683. }
  684. else
  685. {
  686. printk(KERN_DEBUG "pcbit: initialization failed\n");
  687. printk(KERN_DEBUG "pcbit: firmware not loaded\n");
  688. #ifdef DEBUG
  689. printk(KERN_DEBUG "Bank3 = %02x\n",
  690. readb(dev->sh_mem + BANK3));
  691. #endif
  692. writeb(0x40, dev->sh_mem + BANK4);
  693. /* warn the upper layer */
  694. ctl.driver = dev->id;
  695. ctl.command = ISDN_STAT_STOP;
  696. dev->dev_if->statcallb(&ctl);
  697. return -EL2HLT; /* Level 2 halted */
  698. }
  699. return 0;
  700. }
  701. static int pcbit_ioctl(isdn_ctrl *ctl)
  702. {
  703. struct pcbit_dev *dev;
  704. struct pcbit_ioctl *cmd;
  705. dev = finddev(ctl->driver);
  706. if (!dev)
  707. {
  708. printk(KERN_DEBUG "pcbit_ioctl: unknown device\n");
  709. return -ENODEV;
  710. }
  711. cmd = (struct pcbit_ioctl *) ctl->parm.num;
  712. switch (ctl->arg) {
  713. case PCBIT_IOCTL_GETSTAT:
  714. cmd->info.l2_status = dev->l2_state;
  715. break;
  716. case PCBIT_IOCTL_STRLOAD:
  717. if (dev->l2_state == L2_RUNNING)
  718. return -EBUSY;
  719. dev->unack_seq = dev->send_seq = dev->rcv_seq = 0;
  720. dev->writeptr = dev->sh_mem;
  721. dev->readptr = dev->sh_mem + BANK2;
  722. dev->l2_state = L2_LOADING;
  723. break;
  724. case PCBIT_IOCTL_LWMODE:
  725. if (dev->l2_state != L2_LOADING)
  726. return -EINVAL;
  727. dev->l2_state = L2_LWMODE;
  728. break;
  729. case PCBIT_IOCTL_FWMODE:
  730. if (dev->l2_state == L2_RUNNING)
  731. return -EBUSY;
  732. dev->loadptr = LOAD_ZONE_START;
  733. dev->l2_state = L2_FWMODE;
  734. break;
  735. case PCBIT_IOCTL_ENDLOAD:
  736. if (dev->l2_state == L2_RUNNING)
  737. return -EBUSY;
  738. dev->l2_state = L2_DOWN;
  739. break;
  740. case PCBIT_IOCTL_SETBYTE:
  741. if (dev->l2_state == L2_RUNNING)
  742. return -EBUSY;
  743. /* check addr */
  744. if (cmd->info.rdp_byte.addr > BANK4)
  745. return -EFAULT;
  746. writeb(cmd->info.rdp_byte.value, dev->sh_mem + cmd->info.rdp_byte.addr);
  747. break;
  748. case PCBIT_IOCTL_GETBYTE:
  749. if (dev->l2_state == L2_RUNNING)
  750. return -EBUSY;
  751. /* check addr */
  752. if (cmd->info.rdp_byte.addr > BANK4)
  753. {
  754. printk("getbyte: invalid addr %04x\n", cmd->info.rdp_byte.addr);
  755. return -EFAULT;
  756. }
  757. cmd->info.rdp_byte.value = readb(dev->sh_mem + cmd->info.rdp_byte.addr);
  758. break;
  759. case PCBIT_IOCTL_RUNNING:
  760. if (dev->l2_state == L2_RUNNING)
  761. return -EBUSY;
  762. return set_protocol_running(dev);
  763. break;
  764. case PCBIT_IOCTL_WATCH188:
  765. if (dev->l2_state != L2_LOADING)
  766. return -EINVAL;
  767. pcbit_l2_write(dev, MSG_WATCH188, 0x0001, NULL, 0);
  768. break;
  769. case PCBIT_IOCTL_PING188:
  770. if (dev->l2_state != L2_LOADING)
  771. return -EINVAL;
  772. pcbit_l2_write(dev, MSG_PING188_REQ, 0x0001, NULL, 0);
  773. break;
  774. case PCBIT_IOCTL_APION:
  775. if (dev->l2_state != L2_LOADING)
  776. return -EINVAL;
  777. pcbit_l2_write(dev, MSG_API_ON, 0x0001, NULL, 0);
  778. break;
  779. case PCBIT_IOCTL_STOP:
  780. dev->l2_state = L2_DOWN;
  781. writeb(0x40, dev->sh_mem + BANK4);
  782. dev->rcv_seq = 0;
  783. dev->send_seq = 0;
  784. dev->unack_seq = 0;
  785. break;
  786. default:
  787. printk("error: unknown ioctl\n");
  788. break;
  789. };
  790. return 0;
  791. }
  792. /*
  793. * MSN list handling
  794. *
  795. * if null reject all calls
  796. * if first entry has null MSN accept all calls
  797. */
  798. static void pcbit_clear_msn(struct pcbit_dev *dev)
  799. {
  800. struct msn_entry *ptr, *back;
  801. for (ptr = dev->msn_list; ptr;)
  802. {
  803. back = ptr->next;
  804. kfree(ptr);
  805. ptr = back;
  806. }
  807. dev->msn_list = NULL;
  808. }
  809. static void pcbit_set_msn(struct pcbit_dev *dev, char *list)
  810. {
  811. struct msn_entry *ptr;
  812. struct msn_entry *back = NULL;
  813. char *cp, *sp;
  814. int len;
  815. if (strlen(list) == 0) {
  816. ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
  817. if (!ptr) {
  818. printk(KERN_WARNING "kmalloc failed\n");
  819. return;
  820. }
  821. ptr->msn = NULL;
  822. ptr->next = dev->msn_list;
  823. dev->msn_list = ptr;
  824. return;
  825. }
  826. if (dev->msn_list)
  827. for (back = dev->msn_list; back->next; back = back->next);
  828. sp = list;
  829. do {
  830. cp = strchr(sp, ',');
  831. if (cp)
  832. len = cp - sp;
  833. else
  834. len = strlen(sp);
  835. ptr = kmalloc(sizeof(struct msn_entry), GFP_ATOMIC);
  836. if (!ptr) {
  837. printk(KERN_WARNING "kmalloc failed\n");
  838. return;
  839. }
  840. ptr->next = NULL;
  841. ptr->msn = kmalloc(len + 1, GFP_ATOMIC);
  842. if (!ptr->msn) {
  843. printk(KERN_WARNING "kmalloc failed\n");
  844. kfree(ptr);
  845. return;
  846. }
  847. memcpy(ptr->msn, sp, len);
  848. ptr->msn[len] = 0;
  849. #ifdef DEBUG
  850. printk(KERN_DEBUG "msn: %s\n", ptr->msn);
  851. #endif
  852. if (dev->msn_list == NULL)
  853. dev->msn_list = ptr;
  854. else
  855. back->next = ptr;
  856. back = ptr;
  857. sp += len;
  858. } while (cp);
  859. }
  860. /*
  861. * check if we do signal or reject an incoming call
  862. */
  863. static int pcbit_check_msn(struct pcbit_dev *dev, char *msn)
  864. {
  865. struct msn_entry *ptr;
  866. for (ptr = dev->msn_list; ptr; ptr = ptr->next) {
  867. if (ptr->msn == NULL)
  868. return 1;
  869. if (strcmp(ptr->msn, msn) == 0)
  870. return 1;
  871. }
  872. return 0;
  873. }