isdn_common.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:58k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /* $Id: isdn_common.c,v 1.1.4.1 2001/11/20 14:19:34 kai Exp $
  2.  *
  3.  * Linux ISDN subsystem, common used functions (linklevel).
  4.  *
  5.  * Copyright 1994-1999  by Fritz Elfert (fritz@isdn4linux.de)
  6.  * Copyright 1995,96    Thinking Objects Software GmbH Wuerzburg
  7.  * Copyright 1995,96    by Michael Hipp (Michael.Hipp@student.uni-tuebingen.de)
  8.  *
  9.  * This software may be used and distributed according to the terms
  10.  * of the GNU General Public License, incorporated herein by reference.
  11.  *
  12.  */
  13. #include <linux/config.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/version.h>
  17. #include <linux/poll.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/isdn.h>
  20. #include <linux/smp_lock.h>
  21. #include "isdn_common.h"
  22. #include "isdn_tty.h"
  23. #include "isdn_net.h"
  24. #include "isdn_ppp.h"
  25. #ifdef CONFIG_ISDN_AUDIO
  26. #include "isdn_audio.h"
  27. #endif
  28. #ifdef CONFIG_ISDN_DIVERSION_MODULE
  29. #define CONFIG_ISDN_DIVERSION
  30. #endif
  31. #ifdef CONFIG_ISDN_DIVERSION
  32. #include <linux/isdn_divertif.h>
  33. #endif /* CONFIG_ISDN_DIVERSION */
  34. #include "isdn_v110.h"
  35. #include <linux/devfs_fs_kernel.h>
  36. /* Debugflags */
  37. #undef ISDN_DEBUG_STATCALLB
  38. MODULE_DESCRIPTION("ISDN4Linux: link layer");
  39. MODULE_AUTHOR("Fritz Elfert");
  40. MODULE_LICENSE("GPL");
  41. isdn_dev *dev;
  42. static char *isdn_revision = "$Revision: 1.1.4.1 $";
  43. extern char *isdn_net_revision;
  44. extern char *isdn_tty_revision;
  45. #ifdef CONFIG_ISDN_PPP
  46. extern char *isdn_ppp_revision;
  47. #else
  48. static char *isdn_ppp_revision = ": none $";
  49. #endif
  50. #ifdef CONFIG_ISDN_AUDIO
  51. extern char *isdn_audio_revision;
  52. #else
  53. static char *isdn_audio_revision = ": none $";
  54. #endif
  55. extern char *isdn_v110_revision;
  56. #ifdef CONFIG_ISDN_DIVERSION
  57. static isdn_divert_if *divert_if; /* = NULL */
  58. #endif /* CONFIG_ISDN_DIVERSION */
  59. static int isdn_writebuf_stub(int, int, const u_char *, int, int);
  60. static void set_global_features(void);
  61. static void isdn_register_devfs(int);
  62. static void isdn_unregister_devfs(int);
  63. static int isdn_wildmat(char *s, char *p);
  64. void
  65. isdn_lock_drivers(void)
  66. {
  67. int i;
  68. isdn_ctrl cmd;
  69. for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
  70. if (!dev->drv[i])
  71. continue;
  72. cmd.driver = i;
  73. cmd.arg = 0;
  74. cmd.command = ISDN_CMD_LOCK;
  75. isdn_command(&cmd);
  76. dev->drv[i]->locks++;
  77. }
  78. }
  79. void
  80. isdn_MOD_INC_USE_COUNT(void)
  81. {
  82. MOD_INC_USE_COUNT;
  83. isdn_lock_drivers();
  84. }
  85. void
  86. isdn_unlock_drivers(void)
  87. {
  88. int i;
  89. for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
  90. if (!dev->drv[i])
  91. continue;
  92. if (dev->drv[i]->locks > 0) {
  93. isdn_ctrl cmd;
  94. cmd.driver = i;
  95. cmd.arg = 0;
  96. cmd.command = ISDN_CMD_UNLOCK;
  97. isdn_command(&cmd);
  98. dev->drv[i]->locks--;
  99. }
  100. }
  101. }
  102. void
  103. isdn_MOD_DEC_USE_COUNT(void)
  104. {
  105. MOD_DEC_USE_COUNT;
  106. isdn_unlock_drivers();
  107. }
  108. #if defined(ISDN_DEBUG_NET_DUMP) || defined(ISDN_DEBUG_MODEM_DUMP)
  109. void
  110. isdn_dumppkt(char *s, u_char * p, int len, int dumplen)
  111. {
  112. int dumpc;
  113. printk(KERN_DEBUG "%s(%d) ", s, len);
  114. for (dumpc = 0; (dumpc < dumplen) && (len); len--, dumpc++)
  115. printk(" %02x", *p++);
  116. printk("n");
  117. }
  118. #endif
  119. /*
  120.  * I picked the pattern-matching-functions from an old GNU-tar version (1.10)
  121.  * It was originally written and put to PD by rs@mirror.TMC.COM (Rich Salz)
  122.  */
  123. static int
  124. isdn_star(char *s, char *p)
  125. {
  126. while (isdn_wildmat(s, p)) {
  127. if (*++s == '')
  128. return (2);
  129. }
  130. return (0);
  131. }
  132. /*
  133.  * Shell-type Pattern-matching for incoming caller-Ids
  134.  * This function gets a string in s and checks, if it matches the pattern
  135.  * given in p.
  136.  *
  137.  * Return:
  138.  *   0 = match.
  139.  *   1 = no match.
  140.  *   2 = no match. Would eventually match, if s would be longer.
  141.  *
  142.  * Possible Patterns:
  143.  *
  144.  * '?'     matches one character
  145.  * '*'     matches zero or more characters
  146.  * [xyz]   matches the set of characters in brackets.
  147.  * [^xyz]  matches any single character not in the set of characters
  148.  */
  149. static int
  150. isdn_wildmat(char *s, char *p)
  151. {
  152. register int last;
  153. register int matched;
  154. register int reverse;
  155. register int nostar = 1;
  156. if (!(*s) && !(*p))
  157. return(1);
  158. for (; *p; s++, p++)
  159. switch (*p) {
  160. case '\':
  161. /*
  162.  * Literal match with following character,
  163.  * fall through.
  164.  */
  165. p++;
  166. default:
  167. if (*s != *p)
  168. return (*s == '')?2:1;
  169. continue;
  170. case '?':
  171. /* Match anything. */
  172. if (*s == '')
  173. return (2);
  174. continue;
  175. case '*':
  176. nostar = 0;
  177. /* Trailing star matches everything. */
  178. return (*++p ? isdn_star(s, p) : 0);
  179. case '[':
  180. /* [^....] means inverse character class. */
  181. if ((reverse = (p[1] == '^')))
  182. p++;
  183. for (last = 0, matched = 0; *++p && (*p != ']'); last = *p)
  184. /* This next line requires a good C compiler. */
  185. if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
  186. matched = 1;
  187. if (matched == reverse)
  188. return (1);
  189. continue;
  190. }
  191. return (*s == '')?0:nostar;
  192. }
  193. int isdn_msncmp( const char * msn1, const char * msn2 )
  194. {
  195. char TmpMsn1[ ISDN_MSNLEN ];
  196. char TmpMsn2[ ISDN_MSNLEN ];
  197. char *p;
  198. for ( p = TmpMsn1; *msn1 && *msn1 != ':'; )  // Strip off a SPID
  199. *p++ = *msn1++;
  200. *p = '';
  201. for ( p = TmpMsn2; *msn2 && *msn2 != ':'; )  // Strip off a SPID
  202. *p++ = *msn2++;
  203. *p = '';
  204. return isdn_wildmat( TmpMsn1, TmpMsn2 );
  205. }
  206. int
  207. isdn_dc2minor(int di, int ch)
  208. {
  209. int i;
  210. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  211. if (dev->chanmap[i] == ch && dev->drvmap[i] == di)
  212. return i;
  213. return -1;
  214. }
  215. static int isdn_timer_cnt1 = 0;
  216. static int isdn_timer_cnt2 = 0;
  217. static int isdn_timer_cnt3 = 0;
  218. static void
  219. isdn_timer_funct(ulong dummy)
  220. {
  221. int tf = dev->tflags;
  222. if (tf & ISDN_TIMER_FAST) {
  223. if (tf & ISDN_TIMER_MODEMREAD)
  224. isdn_tty_readmodem();
  225. if (tf & ISDN_TIMER_MODEMPLUS)
  226. isdn_tty_modem_escape();
  227. if (tf & ISDN_TIMER_MODEMXMIT)
  228. isdn_tty_modem_xmit();
  229. }
  230. if (tf & ISDN_TIMER_SLOW) {
  231. if (++isdn_timer_cnt1 >= ISDN_TIMER_02SEC) {
  232. isdn_timer_cnt1 = 0;
  233. if (tf & ISDN_TIMER_NETDIAL)
  234. isdn_net_dial();
  235. }
  236. if (++isdn_timer_cnt2 >= ISDN_TIMER_1SEC) {
  237. isdn_timer_cnt2 = 0;
  238. if (tf & ISDN_TIMER_NETHANGUP)
  239. isdn_net_autohup();
  240. if (++isdn_timer_cnt3 >= ISDN_TIMER_RINGING) {
  241. isdn_timer_cnt3 = 0;
  242. if (tf & ISDN_TIMER_MODEMRING)
  243. isdn_tty_modem_ring();
  244. }
  245. if (tf & ISDN_TIMER_CARRIER)
  246. isdn_tty_carrier_timeout();
  247. }
  248. }
  249. if (tf) 
  250. {
  251. unsigned long flags;
  252. save_flags(flags);
  253. cli();
  254. mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
  255. restore_flags(flags);
  256. }
  257. }
  258. void
  259. isdn_timer_ctrl(int tf, int onoff)
  260. {
  261. unsigned long flags;
  262. int old_tflags;
  263. save_flags(flags);
  264. cli();
  265. if ((tf & ISDN_TIMER_SLOW) && (!(dev->tflags & ISDN_TIMER_SLOW))) {
  266. /* If the slow-timer wasn't activated until now */
  267. isdn_timer_cnt1 = 0;
  268. isdn_timer_cnt2 = 0;
  269. }
  270. old_tflags = dev->tflags;
  271. if (onoff)
  272. dev->tflags |= tf;
  273. else
  274. dev->tflags &= ~tf;
  275. if (dev->tflags && !old_tflags)
  276. mod_timer(&dev->timer, jiffies+ISDN_TIMER_RES);
  277. restore_flags(flags);
  278. }
  279. /*
  280.  * Receive a packet from B-Channel. (Called from low-level-module)
  281.  */
  282. static void
  283. isdn_receive_skb_callback(int di, int channel, struct sk_buff *skb)
  284. {
  285. int i;
  286. if ((i = isdn_dc2minor(di, channel)) == -1) {
  287. dev_kfree_skb(skb);
  288. return;
  289. }
  290. /* Update statistics */
  291. dev->ibytes[i] += skb->len;
  292. /* First, try to deliver data to network-device */
  293. if (isdn_net_rcv_skb(i, skb))
  294. return;
  295. /* V.110 handling
  296.  * makes sense for async streams only, so it is
  297.  * called after possible net-device delivery.
  298.  */
  299. if (dev->v110[i]) {
  300. atomic_inc(&dev->v110use[i]);
  301. skb = isdn_v110_decode(dev->v110[i], skb);
  302. atomic_dec(&dev->v110use[i]);
  303. if (!skb)
  304. return;
  305. }
  306. /* No network-device found, deliver to tty or raw-channel */
  307. if (skb->len) {
  308. if (isdn_tty_rcv_skb(i, di, channel, skb))
  309. return;
  310. wake_up_interruptible(&dev->drv[di]->rcv_waitq[channel]);
  311. } else
  312. dev_kfree_skb(skb);
  313. }
  314. /*
  315.  * Intercept command from Linklevel to Lowlevel.
  316.  * If layer 2 protocol is V.110 and this is not supported by current
  317.  * lowlevel-driver, use driver's transparent mode and handle V.110 in
  318.  * linklevel instead.
  319.  */
  320. int
  321. isdn_command(isdn_ctrl *cmd)
  322. {
  323. if (cmd->driver == -1) {
  324. printk(KERN_WARNING "isdn_command command(%x) driver -1n", cmd->command);
  325. return(1);
  326. }
  327. if (cmd->command == ISDN_CMD_SETL2) {
  328. int idx = isdn_dc2minor(cmd->driver, cmd->arg & 255);
  329. unsigned long l2prot = (cmd->arg >> 8) & 255;
  330. unsigned long features = (dev->drv[cmd->driver]->interface->features
  331. >> ISDN_FEATURE_L2_SHIFT) &
  332. ISDN_FEATURE_L2_MASK;
  333. unsigned long l2_feature = (1 << l2prot);
  334. switch (l2prot) {
  335. case ISDN_PROTO_L2_V11096:
  336. case ISDN_PROTO_L2_V11019:
  337. case ISDN_PROTO_L2_V11038:
  338. /* If V.110 requested, but not supported by
  339.  * HL-driver, set emulator-flag and change
  340.  * Layer-2 to transparent
  341.  */
  342. if (!(features & l2_feature)) {
  343. dev->v110emu[idx] = l2prot;
  344. cmd->arg = (cmd->arg & 255) |
  345. (ISDN_PROTO_L2_TRANS << 8);
  346. } else
  347. dev->v110emu[idx] = 0;
  348. }
  349. }
  350. return dev->drv[cmd->driver]->interface->command(cmd);
  351. }
  352. void
  353. isdn_all_eaz(int di, int ch)
  354. {
  355. isdn_ctrl cmd;
  356. if (di < 0)
  357. return;
  358. cmd.driver = di;
  359. cmd.arg = ch;
  360. cmd.command = ISDN_CMD_SETEAZ;
  361. cmd.parm.num[0] = '';
  362. isdn_command(&cmd);
  363. }
  364. /*
  365.  * Begin of a CAPI like LL<->HL interface, currently used only for 
  366.  * supplementary service (CAPI 2.0 part III)
  367.  */
  368. #include "avmb1/capicmd.h"  /* this should be moved in a common place */
  369. int
  370. isdn_capi_rec_hl_msg(capi_msg *cm) {
  371. int di;
  372. int ch;
  373. di = (cm->adr.Controller & 0x7f) -1;
  374. ch = isdn_dc2minor(di, (cm->adr.Controller>>8)& 0x7f);
  375. switch(cm->Command) {
  376. case CAPI_FACILITY:
  377. /* in the moment only handled in tty */
  378. return(isdn_tty_capi_facility(cm));
  379. default:
  380. return(-1);
  381. }
  382. }
  383. static int
  384. isdn_status_callback(isdn_ctrl * c)
  385. {
  386. int di;
  387. ulong flags;
  388. int i;
  389. int r;
  390. int retval = 0;
  391. isdn_ctrl cmd;
  392. isdn_net_dev *p;
  393. di = c->driver;
  394. i = isdn_dc2minor(di, c->arg);
  395. switch (c->command) {
  396. case ISDN_STAT_BSENT:
  397. if (i < 0)
  398. return -1;
  399. if (dev->global_flags & ISDN_GLOBAL_STOPPED)
  400. return 0;
  401. if (isdn_net_stat_callback(i, c))
  402. return 0;
  403. if (isdn_v110_stat_callback(i, c))
  404. return 0;
  405. if (isdn_tty_stat_callback(i, c))
  406. return 0;
  407. wake_up_interruptible(&dev->drv[di]->snd_waitq[c->arg]);
  408. break;
  409. case ISDN_STAT_STAVAIL:
  410. save_flags(flags);
  411. cli();
  412. dev->drv[di]->stavail += c->arg;
  413. restore_flags(flags);
  414. wake_up_interruptible(&dev->drv[di]->st_waitq);
  415. break;
  416. case ISDN_STAT_RUN:
  417. dev->drv[di]->flags |= DRV_FLAG_RUNNING;
  418. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  419. if (dev->drvmap[i] == di)
  420. isdn_all_eaz(di, dev->chanmap[i]);
  421. set_global_features();
  422. break;
  423. case ISDN_STAT_STOP:
  424. dev->drv[di]->flags &= ~DRV_FLAG_RUNNING;
  425. break;
  426. case ISDN_STAT_ICALL:
  427. if (i < 0)
  428. return -1;
  429. #ifdef ISDN_DEBUG_STATCALLB
  430. printk(KERN_DEBUG "ICALL (net): %d %ld %sn", di, c->arg, c->parm.num);
  431. #endif
  432. if (dev->global_flags & ISDN_GLOBAL_STOPPED) {
  433. cmd.driver = di;
  434. cmd.arg = c->arg;
  435. cmd.command = ISDN_CMD_HANGUP;
  436. isdn_command(&cmd);
  437. return 0;
  438. }
  439. /* Try to find a network-interface which will accept incoming call */
  440. r = ((c->command == ISDN_STAT_ICALLW) ? 0 : isdn_net_find_icall(di, c->arg, i, &c->parm.setup));
  441. switch (r) {
  442. case 0:
  443. /* No network-device replies.
  444.  * Try ttyI's.
  445.  * These return 0 on no match, 1 on match and
  446.  * 3 on eventually match, if CID is longer.
  447.  */
  448.                                         if (c->command == ISDN_STAT_ICALL)
  449.   if ((retval = isdn_tty_find_icall(di, c->arg, &c->parm.setup))) return(retval);
  450. #ifdef CONFIG_ISDN_DIVERSION 
  451.                                          if (divert_if)
  452.                                     if ((retval = divert_if->stat_callback(c))) 
  453.     return(retval); /* processed */
  454. #endif /* CONFIG_ISDN_DIVERSION */                       
  455. if ((!retval) && (dev->drv[di]->flags & DRV_FLAG_REJBUS)) {
  456. /* No tty responding */
  457. cmd.driver = di;
  458. cmd.arg = c->arg;
  459. cmd.command = ISDN_CMD_HANGUP;
  460. isdn_command(&cmd);
  461. retval = 2;
  462. }
  463. break;
  464. case 1:
  465. /* Schedule connection-setup */
  466. isdn_net_dial();
  467. cmd.driver = di;
  468. cmd.arg = c->arg;
  469. cmd.command = ISDN_CMD_ACCEPTD;
  470. for ( p = dev->netdev; p; p = p->next )
  471. if ( p->local->isdn_channel == cmd.arg )
  472. {
  473. strcpy( cmd.parm.setup.eazmsn, p->local->msn );
  474. isdn_command(&cmd);
  475. retval = 1;
  476. break;
  477. }
  478. break;
  479. case 2: /* For calling back, first reject incoming call ... */
  480. case 3: /* Interface found, but down, reject call actively  */
  481. retval = 2;
  482. printk(KERN_INFO "isdn: Rejecting Calln");
  483. cmd.driver = di;
  484. cmd.arg = c->arg;
  485. cmd.command = ISDN_CMD_HANGUP;
  486. isdn_command(&cmd);
  487. if (r == 3)
  488. break;
  489. /* Fall through */
  490. case 4:
  491. /* ... then start callback. */
  492. isdn_net_dial();
  493. break;
  494. case 5:
  495. /* Number would eventually match, if longer */
  496. retval = 3;
  497. break;
  498. }
  499. #ifdef ISDN_DEBUG_STATCALLB
  500. printk(KERN_DEBUG "ICALL: ret=%dn", retval);
  501. #endif
  502. return retval;
  503. break;
  504. case ISDN_STAT_CINF:
  505. if (i < 0)
  506. return -1;
  507. #ifdef ISDN_DEBUG_STATCALLB
  508. printk(KERN_DEBUG "CINF: %ld %sn", c->arg, c->parm.num);
  509. #endif
  510. if (dev->global_flags & ISDN_GLOBAL_STOPPED)
  511. return 0;
  512. if (strcmp(c->parm.num, "0"))
  513. isdn_net_stat_callback(i, c);
  514. isdn_tty_stat_callback(i, c);
  515. break;
  516. case ISDN_STAT_CAUSE:
  517. #ifdef ISDN_DEBUG_STATCALLB
  518. printk(KERN_DEBUG "CAUSE: %ld %sn", c->arg, c->parm.num);
  519. #endif
  520. printk(KERN_INFO "isdn: %s,ch%ld cause: %sn",
  521.        dev->drvid[di], c->arg, c->parm.num);
  522. isdn_tty_stat_callback(i, c);
  523. #ifdef CONFIG_ISDN_DIVERSION
  524.                         if (divert_if)
  525.                          divert_if->stat_callback(c); 
  526. #endif /* CONFIG_ISDN_DIVERSION */
  527. break;
  528. case ISDN_STAT_DISPLAY:
  529. #ifdef ISDN_DEBUG_STATCALLB
  530. printk(KERN_DEBUG "DISPLAY: %ld %sn", c->arg, c->parm.display);
  531. #endif
  532. isdn_tty_stat_callback(i, c);
  533. #ifdef CONFIG_ISDN_DIVERSION
  534.                         if (divert_if)
  535.                          divert_if->stat_callback(c); 
  536. #endif /* CONFIG_ISDN_DIVERSION */
  537. break;
  538. case ISDN_STAT_DCONN:
  539. if (i < 0)
  540. return -1;
  541. #ifdef ISDN_DEBUG_STATCALLB
  542. printk(KERN_DEBUG "DCONN: %ldn", c->arg);
  543. #endif
  544. if (dev->global_flags & ISDN_GLOBAL_STOPPED)
  545. return 0;
  546. /* Find any net-device, waiting for D-channel setup */
  547. if (isdn_net_stat_callback(i, c))
  548. break;
  549. isdn_v110_stat_callback(i, c);
  550. /* Find any ttyI, waiting for D-channel setup */
  551. if (isdn_tty_stat_callback(i, c)) {
  552. cmd.driver = di;
  553. cmd.arg = c->arg;
  554. cmd.command = ISDN_CMD_ACCEPTB;
  555. isdn_command(&cmd);
  556. break;
  557. }
  558. break;
  559. case ISDN_STAT_DHUP:
  560. if (i < 0)
  561. return -1;
  562. #ifdef ISDN_DEBUG_STATCALLB
  563. printk(KERN_DEBUG "DHUP: %ldn", c->arg);
  564. #endif
  565. if (dev->global_flags & ISDN_GLOBAL_STOPPED)
  566. return 0;
  567. dev->drv[di]->online &= ~(1 << (c->arg));
  568. isdn_info_update();
  569. /* Signal hangup to network-devices */
  570. if (isdn_net_stat_callback(i, c))
  571. break;
  572. isdn_v110_stat_callback(i, c);
  573. if (isdn_tty_stat_callback(i, c))
  574. break;
  575. #ifdef CONFIG_ISDN_DIVERSION
  576.                         if (divert_if)
  577.                          divert_if->stat_callback(c); 
  578. #endif /* CONFIG_ISDN_DIVERSION */
  579. break;
  580. break;
  581. case ISDN_STAT_BCONN:
  582. if (i < 0)
  583. return -1;
  584. #ifdef ISDN_DEBUG_STATCALLB
  585. printk(KERN_DEBUG "BCONN: %ldn", c->arg);
  586. #endif
  587. /* Signal B-channel-connect to network-devices */
  588. if (dev->global_flags & ISDN_GLOBAL_STOPPED)
  589. return 0;
  590. dev->drv[di]->online |= (1 << (c->arg));
  591. isdn_info_update();
  592. if (isdn_net_stat_callback(i, c))
  593. break;
  594. isdn_v110_stat_callback(i, c);
  595. if (isdn_tty_stat_callback(i, c))
  596. break;
  597. break;
  598. case ISDN_STAT_BHUP:
  599. if (i < 0)
  600. return -1;
  601. #ifdef ISDN_DEBUG_STATCALLB
  602. printk(KERN_DEBUG "BHUP: %ldn", c->arg);
  603. #endif
  604. if (dev->global_flags & ISDN_GLOBAL_STOPPED)
  605. return 0;
  606. dev->drv[di]->online &= ~(1 << (c->arg));
  607. isdn_info_update();
  608. #ifdef CONFIG_ISDN_X25
  609. /* Signal hangup to network-devices */
  610. if (isdn_net_stat_callback(i, c))
  611. break;
  612. #endif
  613. isdn_v110_stat_callback(i, c);
  614. if (isdn_tty_stat_callback(i, c))
  615. break;
  616. break;
  617. case ISDN_STAT_NODCH:
  618. if (i < 0)
  619. return -1;
  620. #ifdef ISDN_DEBUG_STATCALLB
  621. printk(KERN_DEBUG "NODCH: %ldn", c->arg);
  622. #endif
  623. if (dev->global_flags & ISDN_GLOBAL_STOPPED)
  624. return 0;
  625. if (isdn_net_stat_callback(i, c))
  626. break;
  627. if (isdn_tty_stat_callback(i, c))
  628. break;
  629. break;
  630. case ISDN_STAT_ADDCH:
  631. if (isdn_add_channels(dev->drv[di], di, c->arg, 1))
  632. return -1;
  633. isdn_info_update();
  634. break;
  635. case ISDN_STAT_DISCH:
  636. save_flags(flags);
  637. cli();
  638. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  639. if ((dev->drvmap[i] == di) &&
  640.     (dev->chanmap[i] == c->arg)) {
  641.     if (c->parm.num[0])
  642.       dev->usage[i] &= ~ISDN_USAGE_DISABLED;
  643.     else
  644.       if (USG_NONE(dev->usage[i])) {
  645. dev->usage[i] |= ISDN_USAGE_DISABLED;
  646.       }
  647.       else 
  648. retval = -1;
  649.     break;
  650. }
  651. restore_flags(flags);
  652. isdn_info_update();
  653. break;
  654. case ISDN_STAT_UNLOAD:
  655. while (dev->drv[di]->locks > 0) {
  656. isdn_ctrl cmd;
  657. cmd.driver = di;
  658. cmd.arg = 0;
  659. cmd.command = ISDN_CMD_UNLOCK;
  660. isdn_command(&cmd);
  661. dev->drv[di]->locks--;
  662. }
  663. save_flags(flags);
  664. cli();
  665. isdn_tty_stat_callback(i, c);
  666. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  667. if (dev->drvmap[i] == di) {
  668. dev->drvmap[i] = -1;
  669. dev->chanmap[i] = -1;
  670. dev->usage[i] &= ~ISDN_USAGE_DISABLED;
  671. isdn_unregister_devfs(i);
  672. }
  673. dev->drivers--;
  674. dev->channels -= dev->drv[di]->channels;
  675. kfree(dev->drv[di]->rcverr);
  676. kfree(dev->drv[di]->rcvcount);
  677. for (i = 0; i < dev->drv[di]->channels; i++)
  678. skb_queue_purge(&dev->drv[di]->rpqueue[i]);
  679. kfree(dev->drv[di]->rpqueue);
  680. kfree(dev->drv[di]->rcv_waitq);
  681. kfree(dev->drv[di]);
  682. dev->drv[di] = NULL;
  683. dev->drvid[di][0] = '';
  684. isdn_info_update();
  685. set_global_features();
  686. restore_flags(flags);
  687. return 0;
  688. case ISDN_STAT_L1ERR:
  689. break;
  690. case CAPI_PUT_MESSAGE:
  691. return(isdn_capi_rec_hl_msg(&c->parm.cmsg));
  692. #ifdef CONFIG_ISDN_TTY_FAX
  693. case ISDN_STAT_FAXIND:
  694. isdn_tty_stat_callback(i, c);
  695. break;
  696. #endif
  697. #ifdef CONFIG_ISDN_AUDIO
  698. case ISDN_STAT_AUDIO:
  699. isdn_tty_stat_callback(i, c);
  700. break;
  701. #endif
  702. #ifdef CONFIG_ISDN_DIVERSION
  703.         case ISDN_STAT_PROT:
  704.         case ISDN_STAT_REDIR:
  705.                         if (divert_if)
  706.                           return(divert_if->stat_callback(c));
  707. #endif /* CONFIG_ISDN_DIVERSION */
  708. default:
  709. return -1;
  710. }
  711. return 0;
  712. }
  713. /*
  714.  * Get integer from char-pointer, set pointer to end of number
  715.  */
  716. int
  717. isdn_getnum(char **p)
  718. {
  719. int v = -1;
  720. while (*p[0] >= '0' && *p[0] <= '9')
  721. v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p[0]++) - '0');
  722. return v;
  723. }
  724. #define DLE 0x10
  725. /*
  726.  * isdn_readbchan() tries to get data from the read-queue.
  727.  * It MUST be called with interrupts off.
  728.  *
  729.  * Be aware that this is not an atomic operation when sleep != 0, even though 
  730.  * interrupts are turned off! Well, like that we are currently only called
  731.  * on behalf of a read system call on raw device files (which are documented
  732.  * to be dangerous and for for debugging purpose only). The inode semaphore
  733.  * takes care that this is not called for the same minor device number while
  734.  * we are sleeping, but access is not serialized against simultaneous read()
  735.  * from the corresponding ttyI device. Can other ugly events, like changes
  736.  * of the mapping (di,ch)<->minor, happen during the sleep? --he 
  737.  */
  738. int
  739. isdn_readbchan(int di, int channel, u_char * buf, u_char * fp, int len, wait_queue_head_t *sleep)
  740. {
  741. int count;
  742. int count_pull;
  743. int count_put;
  744. int dflag;
  745. struct sk_buff *skb;
  746. u_char *cp;
  747. if (!dev->drv[di])
  748. return 0;
  749. if (skb_queue_empty(&dev->drv[di]->rpqueue[channel])) {
  750. if (sleep)
  751. interruptible_sleep_on(sleep);
  752. else
  753. return 0;
  754. }
  755. if (len > dev->drv[di]->rcvcount[channel])
  756. len = dev->drv[di]->rcvcount[channel];
  757. cp = buf;
  758. count = 0;
  759. while (len) {
  760. if (!(skb = skb_peek(&dev->drv[di]->rpqueue[channel])))
  761. break;
  762. #ifdef CONFIG_ISDN_AUDIO
  763. if (ISDN_AUDIO_SKB_LOCK(skb))
  764. break;
  765. ISDN_AUDIO_SKB_LOCK(skb) = 1;
  766. if ((ISDN_AUDIO_SKB_DLECOUNT(skb)) || (dev->drv[di]->DLEflag & (1 << channel))) {
  767. char *p = skb->data;
  768. unsigned long DLEmask = (1 << channel);
  769. dflag = 0;
  770. count_pull = count_put = 0;
  771. while ((count_pull < skb->len) && (len > 0)) {
  772. len--;
  773. if (dev->drv[di]->DLEflag & DLEmask) {
  774. *cp++ = DLE;
  775. dev->drv[di]->DLEflag &= ~DLEmask;
  776. } else {
  777. *cp++ = *p;
  778. if (*p == DLE) {
  779. dev->drv[di]->DLEflag |= DLEmask;
  780. (ISDN_AUDIO_SKB_DLECOUNT(skb))--;
  781. }
  782. p++;
  783. count_pull++;
  784. }
  785. count_put++;
  786. }
  787. if (count_pull >= skb->len)
  788. dflag = 1;
  789. } else {
  790. #endif
  791. /* No DLE's in buff, so simply copy it */
  792. dflag = 1;
  793. if ((count_pull = skb->len) > len) {
  794. count_pull = len;
  795. dflag = 0;
  796. }
  797. count_put = count_pull;
  798. memcpy(cp, skb->data, count_put);
  799. cp += count_put;
  800. len -= count_put;
  801. #ifdef CONFIG_ISDN_AUDIO
  802. }
  803. #endif
  804. count += count_put;
  805. if (fp) {
  806. memset(fp, 0, count_put);
  807. fp += count_put;
  808. }
  809. if (dflag) {
  810. /* We got all the data in this buff.
  811.  * Now we can dequeue it.
  812.  */
  813. if (fp)
  814. *(fp - 1) = 0xff;
  815. #ifdef CONFIG_ISDN_AUDIO
  816. ISDN_AUDIO_SKB_LOCK(skb) = 0;
  817. #endif
  818. skb = skb_dequeue(&dev->drv[di]->rpqueue[channel]);
  819. dev_kfree_skb(skb);
  820. } else {
  821. /* Not yet emptied this buff, so it
  822.  * must stay in the queue, for further calls
  823.  * but we pull off the data we got until now.
  824.  */
  825. skb_pull(skb, count_pull);
  826. #ifdef CONFIG_ISDN_AUDIO
  827. ISDN_AUDIO_SKB_LOCK(skb) = 0;
  828. #endif
  829. }
  830. dev->drv[di]->rcvcount[channel] -= count_put;
  831. }
  832. return count;
  833. }
  834. static __inline int
  835. isdn_minor2drv(int minor)
  836. {
  837. return (dev->drvmap[minor]);
  838. }
  839. static __inline int
  840. isdn_minor2chan(int minor)
  841. {
  842. return (dev->chanmap[minor]);
  843. }
  844. static char *
  845. isdn_statstr(void)
  846. {
  847. static char istatbuf[2048];
  848. char *p;
  849. int i;
  850. sprintf(istatbuf, "idmap:t");
  851. p = istatbuf + strlen(istatbuf);
  852. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  853. sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
  854. p = istatbuf + strlen(istatbuf);
  855. }
  856. sprintf(p, "nchmap:t");
  857. p = istatbuf + strlen(istatbuf);
  858. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  859. sprintf(p, "%d ", dev->chanmap[i]);
  860. p = istatbuf + strlen(istatbuf);
  861. }
  862. sprintf(p, "ndrmap:t");
  863. p = istatbuf + strlen(istatbuf);
  864. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  865. sprintf(p, "%d ", dev->drvmap[i]);
  866. p = istatbuf + strlen(istatbuf);
  867. }
  868. sprintf(p, "nusage:t");
  869. p = istatbuf + strlen(istatbuf);
  870. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  871. sprintf(p, "%d ", dev->usage[i]);
  872. p = istatbuf + strlen(istatbuf);
  873. }
  874. sprintf(p, "nflags:t");
  875. p = istatbuf + strlen(istatbuf);
  876. for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
  877. if (dev->drv[i]) {
  878. sprintf(p, "%ld ", dev->drv[i]->online);
  879. p = istatbuf + strlen(istatbuf);
  880. } else {
  881. sprintf(p, "? ");
  882. p = istatbuf + strlen(istatbuf);
  883. }
  884. }
  885. sprintf(p, "nphone:t");
  886. p = istatbuf + strlen(istatbuf);
  887. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  888. sprintf(p, "%s ", dev->num[i]);
  889. p = istatbuf + strlen(istatbuf);
  890. }
  891. sprintf(p, "n");
  892. return istatbuf;
  893. }
  894. /* Module interface-code */
  895. void
  896. isdn_info_update(void)
  897. {
  898. infostruct *p = dev->infochain;
  899. while (p) {
  900. *(p->private) = 1;
  901. p = (infostruct *) p->next;
  902. }
  903. wake_up_interruptible(&(dev->info_waitq));
  904. }
  905. static ssize_t
  906. isdn_read(struct file *file, char *buf, size_t count, loff_t * off)
  907. {
  908. uint minor = MINOR(file->f_dentry->d_inode->i_rdev);
  909. int len = 0;
  910. ulong flags;
  911. int drvidx;
  912. int chidx;
  913. int retval;
  914. char *p;
  915. if (off != &file->f_pos)
  916. return -ESPIPE;
  917. lock_kernel();
  918. if (minor == ISDN_MINOR_STATUS) {
  919. if (!file->private_data) {
  920. if (file->f_flags & O_NONBLOCK) {
  921. retval = -EAGAIN;
  922. goto out;
  923. }
  924. interruptible_sleep_on(&(dev->info_waitq));
  925. }
  926. p = isdn_statstr();
  927. file->private_data = 0;
  928. if ((len = strlen(p)) <= count) {
  929. if (copy_to_user(buf, p, len)) {
  930. retval = -EFAULT;
  931. goto out;
  932. }
  933. *off += len;
  934. retval = len;
  935. goto out;
  936. }
  937. retval = 0;
  938. goto out;
  939. }
  940. if (!dev->drivers) {
  941. retval = -ENODEV;
  942. goto out;
  943. }
  944. if (minor <= ISDN_MINOR_BMAX) {
  945. printk(KERN_WARNING "isdn_read minor %d obsolete!n", minor);
  946. drvidx = isdn_minor2drv(minor);
  947. if (drvidx < 0) {
  948. retval = -ENODEV;
  949. goto out;
  950. }
  951. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
  952. retval = -ENODEV;
  953. goto out;
  954. }
  955. chidx = isdn_minor2chan(minor);
  956. if (!(p = kmalloc(count, GFP_KERNEL))) {
  957. retval = -ENOMEM;
  958. goto out;
  959. }
  960. save_flags(flags);
  961. cli();
  962. len = isdn_readbchan(drvidx, chidx, p, 0, count,
  963.      &dev->drv[drvidx]->rcv_waitq[chidx]);
  964. *off += len;
  965. restore_flags(flags);
  966. if (copy_to_user(buf,p,len)) 
  967. len = -EFAULT;
  968. kfree(p);
  969. retval = len;
  970. goto out;
  971. }
  972. if (minor <= ISDN_MINOR_CTRLMAX) {
  973. drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
  974. if (drvidx < 0) {
  975. retval = -ENODEV;
  976. goto out;
  977. }
  978. if (!dev->drv[drvidx]->stavail) {
  979. if (file->f_flags & O_NONBLOCK) {
  980. retval = -EAGAIN;
  981. goto out;
  982. }
  983. interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
  984. }
  985. if (dev->drv[drvidx]->interface->readstat) {
  986. if (count > dev->drv[drvidx]->stavail)
  987. count = dev->drv[drvidx]->stavail;
  988. len = dev->drv[drvidx]->interface->
  989. readstat(buf, count, 1, drvidx,
  990.  isdn_minor2chan(minor));
  991. } else {
  992. len = 0;
  993. }
  994. save_flags(flags);
  995. cli();
  996. if (len)
  997. dev->drv[drvidx]->stavail -= len;
  998. else
  999. dev->drv[drvidx]->stavail = 0;
  1000. restore_flags(flags);
  1001. *off += len;
  1002. retval = len;
  1003. goto out;
  1004. }
  1005. #ifdef CONFIG_ISDN_PPP
  1006. if (minor <= ISDN_MINOR_PPPMAX) {
  1007. retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
  1008. goto out;
  1009. }
  1010. #endif
  1011. retval = -ENODEV;
  1012.  out:
  1013. unlock_kernel();
  1014. return retval;
  1015. }
  1016. static ssize_t
  1017. isdn_write(struct file *file, const char *buf, size_t count, loff_t * off)
  1018. {
  1019. uint minor = MINOR(file->f_dentry->d_inode->i_rdev);
  1020. int drvidx;
  1021. int chidx;
  1022. int retval;
  1023. if (off != &file->f_pos)
  1024. return -ESPIPE;
  1025. if (minor == ISDN_MINOR_STATUS)
  1026. return -EPERM;
  1027. if (!dev->drivers)
  1028. return -ENODEV;
  1029. lock_kernel();
  1030. if (minor <= ISDN_MINOR_BMAX) {
  1031. printk(KERN_WARNING "isdn_write minor %d obsolete!n", minor);
  1032. drvidx = isdn_minor2drv(minor);
  1033. if (drvidx < 0) {
  1034. retval = -ENODEV;
  1035. goto out;
  1036. }
  1037. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
  1038. retval = -ENODEV;
  1039. goto out;
  1040. }
  1041. chidx = isdn_minor2chan(minor);
  1042. while (isdn_writebuf_stub(drvidx, chidx, buf, count, 1) != count)
  1043. interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
  1044. retval = count;
  1045. goto out;
  1046. }
  1047. if (minor <= ISDN_MINOR_CTRLMAX) {
  1048. drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
  1049. if (drvidx < 0) {
  1050. retval = -ENODEV;
  1051. goto out;
  1052. }
  1053. /*
  1054.  * We want to use the isdnctrl device to load the firmware
  1055.  *
  1056.  if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
  1057.  return -ENODEV;
  1058.  */
  1059. if (dev->drv[drvidx]->interface->writecmd)
  1060. retval = dev->drv[drvidx]->interface->
  1061. writecmd(buf, count, 1, drvidx, isdn_minor2chan(minor));
  1062. else
  1063. retval = count;
  1064. goto out;
  1065. }
  1066. #ifdef CONFIG_ISDN_PPP
  1067. if (minor <= ISDN_MINOR_PPPMAX) {
  1068. retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
  1069. goto out;
  1070. }
  1071. #endif
  1072. retval = -ENODEV;
  1073.  out:
  1074. unlock_kernel();
  1075. return retval;
  1076. }
  1077. static unsigned int
  1078. isdn_poll(struct file *file, poll_table * wait)
  1079. {
  1080. unsigned int mask = 0;
  1081. unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
  1082. int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
  1083. lock_kernel();
  1084. if (minor == ISDN_MINOR_STATUS) {
  1085. poll_wait(file, &(dev->info_waitq), wait);
  1086. /* mask = POLLOUT | POLLWRNORM; */
  1087. if (file->private_data) {
  1088. mask |= POLLIN | POLLRDNORM;
  1089. }
  1090. goto out;
  1091. }
  1092. if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
  1093. if (drvidx < 0) {
  1094. /* driver deregistered while file open */
  1095. mask = POLLHUP;
  1096. goto out;
  1097. }
  1098. poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
  1099. mask = POLLOUT | POLLWRNORM;
  1100. if (dev->drv[drvidx]->stavail) {
  1101. mask |= POLLIN | POLLRDNORM;
  1102. }
  1103. goto out;
  1104. }
  1105. #ifdef CONFIG_ISDN_PPP
  1106. if (minor <= ISDN_MINOR_PPPMAX) {
  1107. mask = isdn_ppp_poll(file, wait);
  1108. goto out;
  1109. }
  1110. #endif
  1111. mask = POLLERR;
  1112.  out:
  1113. unlock_kernel();
  1114. return mask;
  1115. }
  1116. static int
  1117. isdn_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
  1118. {
  1119. uint minor = MINOR(inode->i_rdev);
  1120. isdn_ctrl c;
  1121. int drvidx;
  1122. int chidx;
  1123. int ret;
  1124. int i;
  1125. char *p;
  1126. char *s;
  1127. union iocpar {
  1128. char name[10];
  1129. char bname[22];
  1130. isdn_ioctl_struct iocts;
  1131. isdn_net_ioctl_phone phone;
  1132. isdn_net_ioctl_cfg cfg;
  1133. } iocpar;
  1134. #define name  iocpar.name
  1135. #define bname iocpar.bname
  1136. #define iocts iocpar.iocts
  1137. #define phone iocpar.phone
  1138. #define cfg   iocpar.cfg
  1139. if (minor == ISDN_MINOR_STATUS) {
  1140. switch (cmd) {
  1141. case IIOCGETDVR:
  1142. return (TTY_DV +
  1143. (NET_DV << 8) +
  1144. (INF_DV << 16));
  1145. case IIOCGETCPS:
  1146. if (arg) {
  1147. ulong *p = (ulong *) arg;
  1148. int i;
  1149. if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
  1150.        sizeof(ulong) * ISDN_MAX_CHANNELS * 2)))
  1151. return ret;
  1152. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1153. put_user(dev->ibytes[i], p++);
  1154. put_user(dev->obytes[i], p++);
  1155. }
  1156. return 0;
  1157. } else
  1158. return -EINVAL;
  1159. break;
  1160. #ifdef CONFIG_NETDEVICES
  1161. case IIOCNETGPN:
  1162. /* Get peer phone number of a connected 
  1163.  * isdn network interface */
  1164. if (arg) {
  1165. if (copy_from_user((char *) &phone, (char *) arg, sizeof(phone)))
  1166. return -EFAULT;
  1167. return isdn_net_getpeer(&phone, (isdn_net_ioctl_phone *) arg);
  1168. } else
  1169. return -EINVAL;
  1170. #endif
  1171. default:
  1172. return -EINVAL;
  1173. }
  1174. }
  1175. if (!dev->drivers)
  1176. return -ENODEV;
  1177. if (minor <= ISDN_MINOR_BMAX) {
  1178. drvidx = isdn_minor2drv(minor);
  1179. if (drvidx < 0)
  1180. return -ENODEV;
  1181. chidx = isdn_minor2chan(minor);
  1182. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
  1183. return -ENODEV;
  1184. return 0;
  1185. }
  1186. if (minor <= ISDN_MINOR_CTRLMAX) {
  1187. /*
  1188.  * isdn net devices manage lots of configuration variables as linked lists.
  1189.  * Those lists must only be manipulated from user space. Some of the ioctl's
  1190.  * service routines access user space and are not atomic. Therefor, ioctl's
  1191.  * manipulating the lists and ioctl's sleeping while accessing the lists
  1192.  * are serialized by means of a semaphore.
  1193.  */
  1194. switch (cmd) {
  1195. case IIOCNETDWRSET:
  1196. printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabledn");
  1197. return(-EINVAL);
  1198. case IIOCNETLCR:
  1199. printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabledn");
  1200. return -ENODEV;
  1201. #ifdef CONFIG_NETDEVICES
  1202. case IIOCNETAIF:
  1203. /* Add a network-interface */
  1204. if (arg) {
  1205. if (copy_from_user(name, (char *) arg, sizeof(name)))
  1206. return -EFAULT;
  1207. s = name;
  1208. } else {
  1209. s = NULL;
  1210. }
  1211. ret = down_interruptible(&dev->sem);
  1212. if( ret ) return ret;
  1213. if ((s = isdn_net_new(s, NULL))) {
  1214. if (copy_to_user((char *) arg, s, strlen(s) + 1)){
  1215. ret = -EFAULT;
  1216. } else {
  1217. ret = 0;
  1218. }
  1219. } else
  1220. ret = -ENODEV;
  1221. up(&dev->sem);
  1222. return ret;
  1223. case IIOCNETASL:
  1224. /* Add a slave to a network-interface */
  1225. if (arg) {
  1226. if (copy_from_user(bname, (char *) arg, sizeof(bname) - 1))
  1227. return -EFAULT;
  1228. } else
  1229. return -EINVAL;
  1230. ret = down_interruptible(&dev->sem);
  1231. if( ret ) return ret;
  1232. if ((s = isdn_net_newslave(bname))) {
  1233. if (copy_to_user((char *) arg, s, strlen(s) + 1)){
  1234. ret = -EFAULT;
  1235. } else {
  1236. ret = 0;
  1237. }
  1238. } else
  1239. ret = -ENODEV;
  1240. up(&dev->sem);
  1241. return ret;
  1242. case IIOCNETDIF:
  1243. /* Delete a network-interface */
  1244. if (arg) {
  1245. if (copy_from_user(name, (char *) arg, sizeof(name)))
  1246. return -EFAULT;
  1247. ret = down_interruptible(&dev->sem);
  1248. if( ret ) return ret;
  1249. ret = isdn_net_rm(name);
  1250. up(&dev->sem);
  1251. return ret;
  1252. } else
  1253. return -EINVAL;
  1254. case IIOCNETSCF:
  1255. /* Set configurable parameters of a network-interface */
  1256. if (arg) {
  1257. if (copy_from_user((char *) &cfg, (char *) arg, sizeof(cfg)))
  1258. return -EFAULT;
  1259. return isdn_net_setcfg(&cfg);
  1260. } else
  1261. return -EINVAL;
  1262. case IIOCNETGCF:
  1263. /* Get configurable parameters of a network-interface */
  1264. if (arg) {
  1265. if (copy_from_user((char *) &cfg, (char *) arg, sizeof(cfg)))
  1266. return -EFAULT;
  1267. if (!(ret = isdn_net_getcfg(&cfg))) {
  1268. if (copy_to_user((char *) arg, (char *) &cfg, sizeof(cfg)))
  1269. return -EFAULT;
  1270. }
  1271. return ret;
  1272. } else
  1273. return -EINVAL;
  1274. case IIOCNETANM:
  1275. /* Add a phone-number to a network-interface */
  1276. if (arg) {
  1277. if (copy_from_user((char *) &phone, (char *) arg, sizeof(phone)))
  1278. return -EFAULT;
  1279. ret = down_interruptible(&dev->sem);
  1280. if( ret ) return ret;
  1281. ret = isdn_net_addphone(&phone);
  1282. up(&dev->sem);
  1283. return ret;
  1284. } else
  1285. return -EINVAL;
  1286. case IIOCNETGNM:
  1287. /* Get list of phone-numbers of a network-interface */
  1288. if (arg) {
  1289. if (copy_from_user((char *) &phone, (char *) arg, sizeof(phone)))
  1290. return -EFAULT;
  1291. ret = down_interruptible(&dev->sem);
  1292. if( ret ) return ret;
  1293. ret = isdn_net_getphones(&phone, (char *) arg);
  1294. up(&dev->sem);
  1295. return ret;
  1296. } else
  1297. return -EINVAL;
  1298. case IIOCNETDNM:
  1299. /* Delete a phone-number of a network-interface */
  1300. if (arg) {
  1301. if (copy_from_user((char *) &phone, (char *) arg, sizeof(phone)))
  1302. return -EFAULT;
  1303. ret = down_interruptible(&dev->sem);
  1304. if( ret ) return ret;
  1305. ret = isdn_net_delphone(&phone);
  1306. up(&dev->sem);
  1307. return ret;
  1308. } else
  1309. return -EINVAL;
  1310. case IIOCNETDIL:
  1311. /* Force dialing of a network-interface */
  1312. if (arg) {
  1313. if (copy_from_user(name, (char *) arg, sizeof(name)))
  1314. return -EFAULT;
  1315. return isdn_net_force_dial(name);
  1316. } else
  1317. return -EINVAL;
  1318. #ifdef CONFIG_ISDN_PPP
  1319. case IIOCNETALN:
  1320. if (!arg)
  1321. return -EINVAL;
  1322. if (copy_from_user(name, (char *) arg, sizeof(name)))
  1323. return -EFAULT;
  1324. return isdn_ppp_dial_slave(name);
  1325. case IIOCNETDLN:
  1326. if (!arg)
  1327. return -EINVAL;
  1328. if (copy_from_user(name, (char *) arg, sizeof(name)))
  1329. return -EFAULT;
  1330. return isdn_ppp_hangup_slave(name);
  1331. #endif
  1332. case IIOCNETHUP:
  1333. /* Force hangup of a network-interface */
  1334. if (!arg)
  1335. return -EINVAL;
  1336. if (copy_from_user(name, (char *) arg, sizeof(name)))
  1337. return -EFAULT;
  1338. return isdn_net_force_hangup(name);
  1339. break;
  1340. #endif                          /* CONFIG_NETDEVICES */
  1341. case IIOCSETVER:
  1342. dev->net_verbose = arg;
  1343. printk(KERN_INFO "isdn: Verbose-Level is %dn", dev->net_verbose);
  1344. return 0;
  1345. case IIOCSETGST:
  1346. if (arg)
  1347. dev->global_flags |= ISDN_GLOBAL_STOPPED;
  1348. else
  1349. dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
  1350. printk(KERN_INFO "isdn: Global Mode %sn",
  1351.        (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
  1352. return 0;
  1353. case IIOCSETBRJ:
  1354. drvidx = -1;
  1355. if (arg) {
  1356. int i;
  1357. char *p;
  1358. if (copy_from_user((char *) &iocts, (char *) arg,
  1359.      sizeof(isdn_ioctl_struct)))
  1360. return -EFAULT;
  1361. if (strlen(iocts.drvid)) {
  1362. if ((p = strchr(iocts.drvid, ',')))
  1363. *p = 0;
  1364. drvidx = -1;
  1365. for (i = 0; i < ISDN_MAX_DRIVERS; i++)
  1366. if (!(strcmp(dev->drvid[i], iocts.drvid))) {
  1367. drvidx = i;
  1368. break;
  1369. }
  1370. }
  1371. }
  1372. if (drvidx == -1)
  1373. return -ENODEV;
  1374. if (iocts.arg)
  1375. dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
  1376. else
  1377. dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
  1378. return 0;
  1379. case IIOCSIGPRF:
  1380. dev->profd = current;
  1381. return 0;
  1382. break;
  1383. case IIOCGETPRF:
  1384. /* Get all Modem-Profiles */
  1385. if (arg) {
  1386. char *p = (char *) arg;
  1387. int i;
  1388. if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
  1389. (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
  1390.    * ISDN_MAX_CHANNELS)))
  1391. return ret;
  1392. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1393. if (copy_to_user(p, dev->mdm.info[i].emu.profile,
  1394.       ISDN_MODEM_NUMREG))
  1395. return -EFAULT;
  1396. p += ISDN_MODEM_NUMREG;
  1397. if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
  1398. return -EFAULT;
  1399. p += ISDN_MSNLEN;
  1400. if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
  1401. return -EFAULT;
  1402. p += ISDN_LMSNLEN;
  1403. }
  1404. return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
  1405. } else
  1406. return -EINVAL;
  1407. break;
  1408. case IIOCSETPRF:
  1409. /* Set all Modem-Profiles */
  1410. if (arg) {
  1411. char *p = (char *) arg;
  1412. int i;
  1413. if ((ret = verify_area(VERIFY_READ, (void *) arg,
  1414. (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
  1415.    * ISDN_MAX_CHANNELS)))
  1416. return ret;
  1417. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1418. if (copy_from_user(dev->mdm.info[i].emu.profile, p,
  1419.      ISDN_MODEM_NUMREG))
  1420. return -EFAULT;
  1421. p += ISDN_MODEM_NUMREG;
  1422. if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
  1423. return -EFAULT;
  1424. p += ISDN_LMSNLEN;
  1425. if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
  1426. return -EFAULT;
  1427. p += ISDN_MSNLEN;
  1428. }
  1429. return 0;
  1430. } else
  1431. return -EINVAL;
  1432. break;
  1433. case IIOCSETMAP:
  1434. case IIOCGETMAP:
  1435. /* Set/Get MSN->EAZ-Mapping for a driver */
  1436. if (arg) {
  1437. if (copy_from_user((char *) &iocts,
  1438.     (char *) arg,
  1439.      sizeof(isdn_ioctl_struct)))
  1440. return -EFAULT;
  1441. if (strlen(iocts.drvid)) {
  1442. drvidx = -1;
  1443. for (i = 0; i < ISDN_MAX_DRIVERS; i++)
  1444. if (!(strcmp(dev->drvid[i], iocts.drvid))) {
  1445. drvidx = i;
  1446. break;
  1447. }
  1448. } else
  1449. drvidx = 0;
  1450. if (drvidx == -1)
  1451. return -ENODEV;
  1452. if (cmd == IIOCSETMAP) {
  1453. int loop = 1;
  1454. p = (char *) iocts.arg;
  1455. i = 0;
  1456. while (loop) {
  1457. int j = 0;
  1458. while (1) {
  1459. if ((ret = verify_area(VERIFY_READ, p, 1)))
  1460. return ret;
  1461. get_user(bname[j], p++);
  1462. switch (bname[j]) {
  1463. case '':
  1464. loop = 0;
  1465. /* Fall through */
  1466. case ',':
  1467. bname[j] = '';
  1468. strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
  1469. j = ISDN_MSNLEN;
  1470. break;
  1471. default:
  1472. j++;
  1473. }
  1474. if (j >= ISDN_MSNLEN)
  1475. break;
  1476. }
  1477. if (++i > 9)
  1478. break;
  1479. }
  1480. } else {
  1481. p = (char *) iocts.arg;
  1482. for (i = 0; i < 10; i++) {
  1483. sprintf(bname, "%s%s",
  1484. strlen(dev->drv[drvidx]->msn2eaz[i]) ?
  1485. dev->drv[drvidx]->msn2eaz[i] : "_",
  1486. (i < 9) ? "," : "");
  1487. if (copy_to_user(p, bname, strlen(bname) + 1))
  1488. return -EFAULT;
  1489. p += strlen(bname);
  1490. }
  1491. }
  1492. return 0;
  1493. } else
  1494. return -EINVAL;
  1495. case IIOCDBGVAR:
  1496. if (arg) {
  1497. if (copy_to_user((char *) arg, (char *) &dev, sizeof(ulong)))
  1498. return -EFAULT;
  1499. return 0;
  1500. } else
  1501. return -EINVAL;
  1502. break;
  1503. default:
  1504. if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
  1505. cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
  1506. else
  1507. return -EINVAL;
  1508. if (arg) {
  1509. int i;
  1510. char *p;
  1511. if (copy_from_user((char *) &iocts, (char *) arg, sizeof(isdn_ioctl_struct)))
  1512. return -EFAULT;
  1513. if (strlen(iocts.drvid)) {
  1514. if ((p = strchr(iocts.drvid, ',')))
  1515. *p = 0;
  1516. drvidx = -1;
  1517. for (i = 0; i < ISDN_MAX_DRIVERS; i++)
  1518. if (!(strcmp(dev->drvid[i], iocts.drvid))) {
  1519. drvidx = i;
  1520. break;
  1521. }
  1522. } else
  1523. drvidx = 0;
  1524. if (drvidx == -1)
  1525. return -ENODEV;
  1526. if ((ret = verify_area(VERIFY_WRITE, (void *) arg,
  1527.      sizeof(isdn_ioctl_struct))))
  1528. return ret;
  1529. c.driver = drvidx;
  1530. c.command = ISDN_CMD_IOCTL;
  1531. c.arg = cmd;
  1532. memcpy(c.parm.num, (char *) &iocts.arg, sizeof(ulong));
  1533. ret = isdn_command(&c);
  1534. memcpy((char *) &iocts.arg, c.parm.num, sizeof(ulong));
  1535. if (copy_to_user((char *) arg, &iocts, sizeof(isdn_ioctl_struct)))
  1536. return -EFAULT;
  1537. return ret;
  1538. } else
  1539. return -EINVAL;
  1540. }
  1541. }
  1542. #ifdef CONFIG_ISDN_PPP
  1543. if (minor <= ISDN_MINOR_PPPMAX)
  1544. return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
  1545. #endif
  1546. return -ENODEV;
  1547. #undef name
  1548. #undef bname
  1549. #undef iocts
  1550. #undef phone
  1551. #undef cfg
  1552. }
  1553. /*
  1554.  * Open the device code.
  1555.  */
  1556. static int
  1557. isdn_open(struct inode *ino, struct file *filep)
  1558. {
  1559. uint minor = MINOR(ino->i_rdev);
  1560. int drvidx;
  1561. int chidx;
  1562. int retval = -ENODEV;
  1563. if (minor == ISDN_MINOR_STATUS) {
  1564. infostruct *p;
  1565. if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
  1566. p->next = (char *) dev->infochain;
  1567. p->private = (char *) &(filep->private_data);
  1568. dev->infochain = p;
  1569. /* At opening we allow a single update */
  1570. filep->private_data = (char *) 1;
  1571. retval = 0;
  1572. goto out;
  1573. } else {
  1574. retval = -ENOMEM;
  1575. goto out;
  1576. }
  1577. }
  1578. if (!dev->channels)
  1579. goto out;
  1580. if (minor <= ISDN_MINOR_BMAX) {
  1581. printk(KERN_WARNING "isdn_open minor %d obsolete!n", minor);
  1582. drvidx = isdn_minor2drv(minor);
  1583. if (drvidx < 0)
  1584. goto out;
  1585. chidx = isdn_minor2chan(minor);
  1586. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
  1587. goto out;
  1588. if (!(dev->drv[drvidx]->online & (1 << chidx)))
  1589. goto out;
  1590. isdn_lock_drivers();
  1591. retval = 0;
  1592. goto out;
  1593. }
  1594. if (minor <= ISDN_MINOR_CTRLMAX) {
  1595. drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
  1596. if (drvidx < 0)
  1597. goto out;
  1598. isdn_lock_drivers();
  1599. retval = 0;
  1600. goto out;
  1601. }
  1602. #ifdef CONFIG_ISDN_PPP
  1603. if (minor <= ISDN_MINOR_PPPMAX) {
  1604. retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
  1605. if (retval == 0)
  1606. isdn_lock_drivers();
  1607. goto out;
  1608. }
  1609. #endif
  1610.  out:
  1611. return retval;
  1612. }
  1613. static int
  1614. isdn_close(struct inode *ino, struct file *filep)
  1615. {
  1616. uint minor = MINOR(ino->i_rdev);
  1617. lock_kernel();
  1618. if (minor == ISDN_MINOR_STATUS) {
  1619. infostruct *p = dev->infochain;
  1620. infostruct *q = NULL;
  1621. while (p) {
  1622. if (p->private == (char *) &(filep->private_data)) {
  1623. if (q)
  1624. q->next = p->next;
  1625. else
  1626. dev->infochain = (infostruct *) (p->next);
  1627. kfree(p);
  1628. goto out;
  1629. }
  1630. q = p;
  1631. p = (infostruct *) (p->next);
  1632. }
  1633. printk(KERN_WARNING "isdn: No private data while closing isdnctrln");
  1634. goto out;
  1635. }
  1636. isdn_unlock_drivers();
  1637. if (minor <= ISDN_MINOR_BMAX)
  1638. goto out;
  1639. if (minor <= ISDN_MINOR_CTRLMAX) {
  1640. if (dev->profd == current)
  1641. dev->profd = NULL;
  1642. goto out;
  1643. }
  1644. #ifdef CONFIG_ISDN_PPP
  1645. if (minor <= ISDN_MINOR_PPPMAX)
  1646. isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
  1647. #endif
  1648.  out:
  1649. unlock_kernel();
  1650. return 0;
  1651. }
  1652. static struct file_operations isdn_fops =
  1653. {
  1654. owner: THIS_MODULE,
  1655. llseek: no_llseek,
  1656. read: isdn_read,
  1657. write: isdn_write,
  1658. poll: isdn_poll,
  1659. ioctl: isdn_ioctl,
  1660. open: isdn_open,
  1661. release: isdn_close,
  1662. };
  1663. char *
  1664. isdn_map_eaz2msn(char *msn, int di)
  1665. {
  1666. driver *this = dev->drv[di];
  1667. int i;
  1668. if (strlen(msn) == 1) {
  1669. i = msn[0] - '0';
  1670. if ((i >= 0) && (i <= 9))
  1671. if (strlen(this->msn2eaz[i]))
  1672. return (this->msn2eaz[i]);
  1673. }
  1674. return (msn);
  1675. }
  1676. /*
  1677.  * Find an unused ISDN-channel, whose feature-flags match the
  1678.  * given L2- and L3-protocols.
  1679.  */
  1680. #define L2V (~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038))
  1681. int
  1682. isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
  1683.       ,int pre_chan, char *msn)
  1684. {
  1685. int i;
  1686. ulong flags;
  1687. ulong features;
  1688. ulong vfeatures;
  1689. save_flags(flags);
  1690. cli();
  1691. features = ((1 << l2_proto) | (0x10000 << l3_proto));
  1692. vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
  1693.      ~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038));
  1694. /* If Layer-2 protocol is V.110, accept drivers with
  1695.  * transparent feature even if these don't support V.110
  1696.  * because we can emulate this in linklevel.
  1697.  */
  1698. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  1699. if (USG_NONE(dev->usage[i]) &&
  1700.     (dev->drvmap[i] != -1)) {
  1701. int d = dev->drvmap[i];
  1702. if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
  1703. ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
  1704. continue;
  1705. if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
  1706. continue;
  1707. if (dev->usage[i] & ISDN_USAGE_DISABLED)
  1708.         continue; /* usage not allowed */
  1709. if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
  1710. if (((dev->drv[d]->interface->features & features) == features) ||
  1711.     (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
  1712.      (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
  1713. if ((pre_dev < 0) || (pre_chan < 0)) {
  1714. dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
  1715. dev->usage[i] |= usage;
  1716. isdn_info_update();
  1717. restore_flags(flags);
  1718. return i;
  1719. } else {
  1720. if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
  1721. dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
  1722. dev->usage[i] |= usage;
  1723. isdn_info_update();
  1724. restore_flags(flags);
  1725. return i;
  1726. }
  1727. }
  1728. }
  1729. }
  1730. }
  1731. restore_flags(flags);
  1732. return -1;
  1733. }
  1734. /*
  1735.  * Set state of ISDN-channel to 'unused'
  1736.  */
  1737. void
  1738. isdn_free_channel(int di, int ch, int usage)
  1739. {
  1740. int i;
  1741. ulong flags;
  1742. save_flags(flags);
  1743. cli();
  1744. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  1745. if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
  1746.     (dev->drvmap[i] == di) &&
  1747.     (dev->chanmap[i] == ch)) {
  1748. dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
  1749. strcpy(dev->num[i], "???");
  1750. dev->ibytes[i] = 0;
  1751. dev->obytes[i] = 0;
  1752. // 20.10.99 JIM, try to reinitialize v110 !
  1753. dev->v110emu[i] = 0;
  1754. atomic_set(&(dev->v110use[i]), 0);
  1755. isdn_v110_close(dev->v110[i]);
  1756. dev->v110[i] = NULL;
  1757. // 20.10.99 JIM, try to reinitialize v110 !
  1758. isdn_info_update();
  1759. skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
  1760. }
  1761. restore_flags(flags);
  1762. }
  1763. /*
  1764.  * Cancel Exclusive-Flag for ISDN-channel
  1765.  */
  1766. void
  1767. isdn_unexclusive_channel(int di, int ch)
  1768. {
  1769. int i;
  1770. ulong flags;
  1771. save_flags(flags);
  1772. cli();
  1773. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  1774. if ((dev->drvmap[i] == di) &&
  1775.     (dev->chanmap[i] == ch)) {
  1776. dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
  1777. isdn_info_update();
  1778. restore_flags(flags);
  1779. return;
  1780. }
  1781. restore_flags(flags);
  1782. }
  1783. /*
  1784.  *  writebuf replacement for SKB_ABLE drivers
  1785.  */
  1786. static int
  1787. isdn_writebuf_stub(int drvidx, int chan, const u_char * buf, int len,
  1788.    int user)
  1789. {
  1790. int ret;
  1791. int hl = dev->drv[drvidx]->interface->hl_hdrlen;
  1792. struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
  1793. if (!skb)
  1794. return 0;
  1795. skb_reserve(skb, hl);
  1796. if (user)
  1797. copy_from_user(skb_put(skb, len), buf, len);
  1798. else
  1799. memcpy(skb_put(skb, len), buf, len);
  1800. ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
  1801. if (ret <= 0)
  1802. dev_kfree_skb(skb);
  1803. if (ret > 0)
  1804. dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
  1805. return ret;
  1806. }
  1807. /*
  1808.  * Return: length of data on success, -ERRcode on failure.
  1809.  */
  1810. int
  1811. isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
  1812. {
  1813. int ret;
  1814. struct sk_buff *nskb = NULL;
  1815. int v110_ret = skb->len;
  1816. int idx = isdn_dc2minor(drvidx, chan);
  1817. if (dev->v110[idx]) {
  1818. atomic_inc(&dev->v110use[idx]);
  1819. nskb = isdn_v110_encode(dev->v110[idx], skb);
  1820. atomic_dec(&dev->v110use[idx]);
  1821. if (!nskb)
  1822. return 0;
  1823. v110_ret = *((int *)nskb->data);
  1824. skb_pull(nskb, sizeof(int));
  1825. if (!nskb->len) {
  1826. dev_kfree_skb(nskb);
  1827. return v110_ret;
  1828. }
  1829. /* V.110 must always be acknowledged */
  1830. ack = 1;
  1831. ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
  1832. } else {
  1833. int hl = dev->drv[drvidx]->interface->hl_hdrlen;
  1834. if( skb_headroom(skb) < hl ){
  1835. /* 
  1836.  * This should only occur when new HL driver with
  1837.  * increased hl_hdrlen was loaded after netdevice
  1838.  * was created and connected to the new driver.
  1839.  *
  1840.  * The V.110 branch (re-allocates on its own) does
  1841.  * not need this
  1842.  */
  1843. struct sk_buff * skb_tmp;
  1844. skb_tmp = skb_realloc_headroom(skb, hl);
  1845. printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%sn", skb_tmp ? "" : " failed");
  1846. if (!skb_tmp) return -ENOMEM; /* 0 better? */
  1847. ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
  1848. if( ret > 0 ){
  1849. dev_kfree_skb(skb);
  1850. } else {
  1851. dev_kfree_skb(skb_tmp);
  1852. }
  1853. } else {
  1854. ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
  1855. }
  1856. }
  1857. if (ret > 0) {
  1858. dev->obytes[idx] += ret;
  1859. if (dev->v110[idx]) {
  1860. atomic_inc(&dev->v110use[idx]);
  1861. dev->v110[idx]->skbuser++;
  1862. atomic_dec(&dev->v110use[idx]);
  1863. /* For V.110 return unencoded data length */
  1864. ret = v110_ret;
  1865. /* if the complete frame was send we free the skb;
  1866.    if not upper function will requeue the skb */ 
  1867. if (ret == skb->len)
  1868. dev_kfree_skb(skb);
  1869. }
  1870. } else
  1871. if (dev->v110[idx])
  1872. dev_kfree_skb(nskb);
  1873. return ret;
  1874. }
  1875. int
  1876. isdn_add_channels(driver *d, int drvidx, int n, int adding)
  1877. {
  1878. int j, k, m;
  1879. ulong flags;
  1880. init_waitqueue_head(&d->st_waitq);
  1881. if (d->flags & DRV_FLAG_RUNNING)
  1882. return -1;
  1883.         if (n < 1) return 0;
  1884. m = (adding) ? d->channels + n : n;
  1885. if (dev->channels + n > ISDN_MAX_CHANNELS) {
  1886. printk(KERN_WARNING "register_isdn: Max. %d channels supportedn",
  1887.        ISDN_MAX_CHANNELS);
  1888. return -1;
  1889. }
  1890. if ((adding) && (d->rcverr))
  1891. kfree(d->rcverr);
  1892. if (!(d->rcverr = kmalloc(sizeof(int) * m, GFP_KERNEL))) {
  1893. printk(KERN_WARNING "register_isdn: Could not alloc rcverrn");
  1894. return -1;
  1895. }
  1896. memset((char *) d->rcverr, 0, sizeof(int) * m);
  1897. if ((adding) && (d->rcvcount))
  1898. kfree(d->rcvcount);
  1899. if (!(d->rcvcount = kmalloc(sizeof(int) * m, GFP_KERNEL))) {
  1900. printk(KERN_WARNING "register_isdn: Could not alloc rcvcountn");
  1901. if (!adding) kfree(d->rcverr);
  1902. return -1;
  1903. }
  1904. memset((char *) d->rcvcount, 0, sizeof(int) * m);
  1905. if ((adding) && (d->rpqueue)) {
  1906. for (j = 0; j < d->channels; j++)
  1907. skb_queue_purge(&d->rpqueue[j]);
  1908. kfree(d->rpqueue);
  1909. }
  1910. if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_KERNEL))) {
  1911. printk(KERN_WARNING "register_isdn: Could not alloc rpqueuen");
  1912. if (!adding) {
  1913. kfree(d->rcvcount);
  1914. kfree(d->rcverr);
  1915. }
  1916. return -1; 
  1917. }
  1918. for (j = 0; j < m; j++) {
  1919. skb_queue_head_init(&d->rpqueue[j]);
  1920. }
  1921. if ((adding) && (d->rcv_waitq))
  1922. kfree(d->rcv_waitq);
  1923. d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_KERNEL);
  1924. if (!d->rcv_waitq) {
  1925. printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitqn");
  1926. if (!adding) {
  1927. kfree(d->rpqueue);
  1928. kfree(d->rcvcount);
  1929. kfree(d->rcverr);
  1930. }
  1931. return -1;
  1932. }
  1933. d->snd_waitq = d->rcv_waitq + m;
  1934. for (j = 0; j < m; j++) {
  1935. init_waitqueue_head(&d->rcv_waitq[j]);
  1936. init_waitqueue_head(&d->snd_waitq[j]);
  1937. }
  1938. dev->channels += n;
  1939. save_flags(flags);
  1940. cli();
  1941. for (j = d->channels; j < m; j++)
  1942. for (k = 0; k < ISDN_MAX_CHANNELS; k++)
  1943. if (dev->chanmap[k] < 0) {
  1944. dev->chanmap[k] = j;
  1945. dev->drvmap[k] = drvidx;
  1946. isdn_register_devfs(k);
  1947. break;
  1948. }
  1949. restore_flags(flags);
  1950. d->channels = m;
  1951. return 0;
  1952. }
  1953. /*
  1954.  * Low-level-driver registration
  1955.  */
  1956. static void
  1957. set_global_features(void)
  1958. {
  1959. int drvidx;
  1960. dev->global_features = 0;
  1961. for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
  1962. if (!dev->drv[drvidx])
  1963. continue;
  1964. if (dev->drv[drvidx]->interface)
  1965. dev->global_features |= dev->drv[drvidx]->interface->features;
  1966. }
  1967. }
  1968. #ifdef CONFIG_ISDN_DIVERSION
  1969. static char *map_drvname(int di)
  1970. {
  1971.   if ((di < 0) || (di >= ISDN_MAX_DRIVERS)) 
  1972.     return(NULL);
  1973.   return(dev->drvid[di]); /* driver name */
  1974. } /* map_drvname */
  1975. static int map_namedrv(char *id)
  1976. {  int i;
  1977.    for (i = 0; i < ISDN_MAX_DRIVERS; i++)
  1978.     { if (!strcmp(dev->drvid[i],id)) 
  1979.         return(i);
  1980.     }
  1981.    return(-1);
  1982. } /* map_namedrv */
  1983. int DIVERT_REG_NAME(isdn_divert_if *i_div)
  1984. {
  1985.   if (i_div->if_magic != DIVERT_IF_MAGIC) 
  1986.     return(DIVERT_VER_ERR);
  1987.   switch (i_div->cmd)
  1988.     {
  1989.       case DIVERT_CMD_REL:
  1990.         if (divert_if != i_div) 
  1991.           return(DIVERT_REL_ERR);
  1992.         divert_if = NULL; /* free interface */
  1993.         MOD_DEC_USE_COUNT;
  1994.         return(DIVERT_NO_ERR);
  1995.       case DIVERT_CMD_REG:
  1996.         if (divert_if) 
  1997.           return(DIVERT_REG_ERR);
  1998.         i_div->ll_cmd = isdn_command; /* set command function */
  1999.         i_div->drv_to_name = map_drvname; 
  2000.         i_div->name_to_drv = map_namedrv; 
  2001.         MOD_INC_USE_COUNT;
  2002.         divert_if = i_div; /* remember interface */
  2003.         return(DIVERT_NO_ERR);
  2004.       default:
  2005.         return(DIVERT_CMD_ERR);   
  2006.     }
  2007. } /* DIVERT_REG_NAME */
  2008. EXPORT_SYMBOL(DIVERT_REG_NAME);
  2009. #endif /* CONFIG_ISDN_DIVERSION */
  2010. EXPORT_SYMBOL(register_isdn);
  2011. #ifdef CONFIG_ISDN_PPP
  2012. EXPORT_SYMBOL(isdn_ppp_register_compressor);
  2013. EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
  2014. #endif
  2015. int
  2016. register_isdn(isdn_if * i)
  2017. {
  2018. driver *d;
  2019. int j;
  2020. ulong flags;
  2021. int drvidx;
  2022. if (dev->drivers >= ISDN_MAX_DRIVERS) {
  2023. printk(KERN_WARNING "register_isdn: Max. %d drivers supportedn",
  2024.        ISDN_MAX_DRIVERS);
  2025. return 0;
  2026. }
  2027. if (!i->writebuf_skb) {
  2028. printk(KERN_WARNING "register_isdn: No write routine given.n");
  2029. return 0;
  2030. }
  2031. if (!(d = kmalloc(sizeof(driver), GFP_KERNEL))) {
  2032. printk(KERN_WARNING "register_isdn: Could not alloc driver-structn");
  2033. return 0;
  2034. }
  2035. memset((char *) d, 0, sizeof(driver));
  2036. d->maxbufsize = i->maxbufsize;
  2037. d->pktcount = 0;
  2038. d->stavail = 0;
  2039. d->flags = DRV_FLAG_LOADED;
  2040. d->online = 0;
  2041. d->interface = i;
  2042. d->channels = 0;
  2043. for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
  2044. if (!dev->drv[drvidx])
  2045. break;
  2046. if (isdn_add_channels(d, drvidx, i->channels, 0)) {
  2047. kfree(d);
  2048. return 0;
  2049. }
  2050. i->channels = drvidx;
  2051. i->rcvcallb_skb = isdn_receive_skb_callback;
  2052. i->statcallb = isdn_status_callback;
  2053. if (!strlen(i->id))
  2054. sprintf(i->id, "line%d", drvidx);
  2055. save_flags(flags);
  2056. cli();
  2057. for (j = 0; j < drvidx; j++)
  2058. if (!strcmp(i->id, dev->drvid[j]))
  2059. sprintf(i->id, "line%d", drvidx);
  2060. dev->drv[drvidx] = d;
  2061. strcpy(dev->drvid[drvidx], i->id);
  2062. isdn_info_update();
  2063. dev->drivers++;
  2064. set_global_features();
  2065. restore_flags(flags);
  2066. return 1;
  2067. }
  2068. /*
  2069.  *****************************************************************************
  2070.  * And now the modules code.
  2071.  *****************************************************************************
  2072.  */
  2073. static char *
  2074. isdn_getrev(const char *revision)
  2075. {
  2076. char *rev;
  2077. char *p;
  2078. if ((p = strchr(revision, ':'))) {
  2079. rev = p + 2;
  2080. p = strchr(rev, '$');
  2081. *--p = 0;
  2082. } else
  2083. rev = "???";
  2084. return rev;
  2085. }
  2086. #ifdef CONFIG_DEVFS_FS
  2087. static devfs_handle_t devfs_handle;
  2088. static void isdn_register_devfs(int k)
  2089. {
  2090. char buf[11];
  2091. sprintf (buf, "isdn%d", k);
  2092. dev->devfs_handle_isdnX[k] =
  2093.     devfs_register (devfs_handle, buf, DEVFS_FL_DEFAULT,
  2094.     ISDN_MAJOR, ISDN_MINOR_B + k,0600 | S_IFCHR,
  2095.     &isdn_fops, NULL);
  2096. sprintf (buf, "isdnctrl%d", k);
  2097. dev->devfs_handle_isdnctrlX[k] =
  2098.     devfs_register (devfs_handle, buf, DEVFS_FL_DEFAULT,
  2099.     ISDN_MAJOR, ISDN_MINOR_CTRL + k, 0600 | S_IFCHR,
  2100.     &isdn_fops, NULL);
  2101. }
  2102. static void isdn_unregister_devfs(int k)
  2103. {
  2104. devfs_unregister (dev->devfs_handle_isdnX[k]);
  2105. devfs_unregister (dev->devfs_handle_isdnctrlX[k]);
  2106. }
  2107. static void isdn_init_devfs(void)
  2108. {
  2109. #  ifdef CONFIG_ISDN_PPP
  2110. int i;
  2111. #  endif
  2112. devfs_handle = devfs_mk_dir (NULL, "isdn", NULL);
  2113. #  ifdef CONFIG_ISDN_PPP
  2114. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  2115. char buf[8];
  2116. sprintf (buf, "ippp%d", i);
  2117. dev->devfs_handle_ipppX[i] =
  2118.     devfs_register (devfs_handle, buf, DEVFS_FL_DEFAULT,
  2119.     ISDN_MAJOR, ISDN_MINOR_PPP + i,
  2120.     0600 | S_IFCHR, &isdn_fops, NULL);
  2121. }
  2122. #  endif
  2123. dev->devfs_handle_isdninfo =
  2124.     devfs_register (devfs_handle, "isdninfo", DEVFS_FL_DEFAULT,
  2125.     ISDN_MAJOR, ISDN_MINOR_STATUS, 0600 | S_IFCHR,
  2126.     &isdn_fops, NULL);
  2127. dev->devfs_handle_isdnctrl =
  2128.     devfs_register (devfs_handle, "isdnctrl", DEVFS_FL_DEFAULT,
  2129.     ISDN_MAJOR, ISDN_MINOR_CTRL, 0600 | S_IFCHR, 
  2130.     &isdn_fops, NULL);
  2131. }
  2132. static void isdn_cleanup_devfs(void)
  2133. {
  2134. #  ifdef CONFIG_ISDN_PPP
  2135. int i;
  2136. for (i = 0; i < ISDN_MAX_CHANNELS; i++) 
  2137. devfs_unregister (dev->devfs_handle_ipppX[i]);
  2138. #  endif
  2139. devfs_unregister (dev->devfs_handle_isdninfo);
  2140. devfs_unregister (dev->devfs_handle_isdnctrl);
  2141. devfs_unregister (devfs_handle);
  2142. }
  2143. #else   /* CONFIG_DEVFS_FS */
  2144. static void isdn_register_devfs(int dummy)
  2145. {
  2146. return;
  2147. }
  2148. static void isdn_unregister_devfs(int dummy)
  2149. {
  2150. return;
  2151. }
  2152. static void isdn_init_devfs(void)
  2153. {
  2154.     return;
  2155. }
  2156. static void isdn_cleanup_devfs(void)
  2157. {
  2158.     return;
  2159. }
  2160. #endif  /* CONFIG_DEVFS_FS */
  2161. /*
  2162.  * Allocate and initialize all data, register modem-devices
  2163.  */
  2164. static int __init isdn_init(void)
  2165. {
  2166. int i;
  2167. char tmprev[50];
  2168. if (!(dev = (isdn_dev *) vmalloc(sizeof(isdn_dev)))) {
  2169. printk(KERN_WARNING "isdn: Could not allocate device-struct.n");
  2170. return -EIO;
  2171. }
  2172. memset((char *) dev, 0, sizeof(isdn_dev));
  2173. init_timer(&dev->timer);
  2174. dev->timer.function = isdn_timer_funct;
  2175. init_MUTEX(&dev->sem);
  2176. init_waitqueue_head(&dev->info_waitq);
  2177. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  2178. dev->drvmap[i] = -1;
  2179. dev->chanmap[i] = -1;
  2180. dev->m_idx[i] = -1;
  2181. strcpy(dev->num[i], "???");
  2182. init_waitqueue_head(&dev->mdm.info[i].open_wait);
  2183. init_waitqueue_head(&dev->mdm.info[i].close_wait);
  2184. }
  2185. if (devfs_register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
  2186. printk(KERN_WARNING "isdn: Could not register control devicesn");
  2187. vfree(dev);
  2188. return -EIO;
  2189. }
  2190. isdn_init_devfs();
  2191. if ((i = isdn_tty_modem_init()) < 0) {
  2192. printk(KERN_WARNING "isdn: Could not register tty devicesn");
  2193. if (i == -3)
  2194. tty_unregister_driver(&dev->mdm.cua_modem);
  2195. if (i <= -2)
  2196. tty_unregister_driver(&dev->mdm.tty_modem);
  2197. vfree(dev);
  2198. isdn_cleanup_devfs();
  2199. devfs_unregister_chrdev(ISDN_MAJOR, "isdn");
  2200. return -EIO;
  2201. }
  2202. #ifdef CONFIG_ISDN_PPP
  2203. if (isdn_ppp_init() < 0) {
  2204. printk(KERN_WARNING "isdn: Could not create PPP-device-structsn");
  2205. tty_unregister_driver(&dev->mdm.tty_modem);
  2206. tty_unregister_driver(&dev->mdm.cua_modem);
  2207. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  2208. kfree(dev->mdm.info[i].xmit_buf - 4);
  2209. isdn_cleanup_devfs();
  2210. devfs_unregister_chrdev(ISDN_MAJOR, "isdn");
  2211. vfree(dev);
  2212. return -EIO;
  2213. }
  2214. #endif                          /* CONFIG_ISDN_PPP */
  2215. strcpy(tmprev, isdn_revision);
  2216. printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
  2217. strcpy(tmprev, isdn_tty_revision);
  2218. printk("%s/", isdn_getrev(tmprev));
  2219. strcpy(tmprev, isdn_net_revision);
  2220. printk("%s/", isdn_getrev(tmprev));
  2221. strcpy(tmprev, isdn_ppp_revision);
  2222. printk("%s/", isdn_getrev(tmprev));
  2223. strcpy(tmprev, isdn_audio_revision);
  2224. printk("%s/", isdn_getrev(tmprev));
  2225. strcpy(tmprev, isdn_v110_revision);
  2226. printk("%s", isdn_getrev(tmprev));
  2227. #ifdef MODULE
  2228. printk(" loadedn");
  2229. #else
  2230. printk("n");
  2231. #endif
  2232. isdn_info_update();
  2233. return 0;
  2234. }
  2235. /*
  2236.  * Unload module
  2237.  */
  2238. static void __exit isdn_exit(void)
  2239. {
  2240. unsigned long flags;
  2241. int i;
  2242. #ifdef CONFIG_ISDN_PPP
  2243. isdn_ppp_cleanup();
  2244. #endif
  2245. save_flags(flags);
  2246. cli();
  2247. if (isdn_net_rmall() < 0) {
  2248. printk(KERN_WARNING "isdn: net-device busy, remove cancelledn");
  2249. restore_flags(flags);
  2250. return;
  2251. }
  2252. if (tty_unregister_driver(&dev->mdm.tty_modem)) {
  2253. printk(KERN_WARNING "isdn: ttyI-device busy, remove cancelledn");
  2254. restore_flags(flags);
  2255. return;
  2256. }
  2257. if (tty_unregister_driver(&dev->mdm.cua_modem)) {
  2258. printk(KERN_WARNING "isdn: cui-device busy, remove cancelledn");
  2259. restore_flags(flags);
  2260. return;
  2261. }
  2262. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  2263. isdn_tty_cleanup_xmit(&dev->mdm.info[i]);
  2264. kfree(dev->mdm.info[i].xmit_buf - 4);
  2265. #ifdef CONFIG_ISDN_TTY_FAX
  2266. kfree(dev->mdm.info[i].fax);
  2267. #endif
  2268. }
  2269. if (devfs_unregister_chrdev(ISDN_MAJOR, "isdn") != 0) {
  2270. printk(KERN_WARNING "isdn: controldevice busy, remove cancelledn");
  2271. restore_flags(flags);
  2272. } else {
  2273. isdn_cleanup_devfs();
  2274. del_timer(&dev->timer);
  2275. restore_flags(flags);
  2276. /* call vfree with interrupts enabled, else it will hang */
  2277. vfree(dev);
  2278. printk(KERN_NOTICE "ISDN-subsystem unloadedn");
  2279. }
  2280. }
  2281. module_init(isdn_init);
  2282. module_exit(isdn_exit);