isdn_common.c 58 KB

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