isdn_common.c 54 KB

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