isdn_common.c 57 KB

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