isdn_common.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  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. static __inline int
  808. isdn_minor2drv(int minor)
  809. {
  810. return (dev->drvmap[minor]);
  811. }
  812. static __inline int
  813. isdn_minor2chan(int minor)
  814. {
  815. return (dev->chanmap[minor]);
  816. }
  817. static char *
  818. isdn_statstr(void)
  819. {
  820. static char istatbuf[2048];
  821. char *p;
  822. int i;
  823. sprintf(istatbuf, "idmap:\t");
  824. p = istatbuf + strlen(istatbuf);
  825. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  826. sprintf(p, "%s ", (dev->drvmap[i] < 0) ? "-" : dev->drvid[dev->drvmap[i]]);
  827. p = istatbuf + strlen(istatbuf);
  828. }
  829. sprintf(p, "\nchmap:\t");
  830. p = istatbuf + strlen(istatbuf);
  831. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  832. sprintf(p, "%d ", dev->chanmap[i]);
  833. p = istatbuf + strlen(istatbuf);
  834. }
  835. sprintf(p, "\ndrmap:\t");
  836. p = istatbuf + strlen(istatbuf);
  837. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  838. sprintf(p, "%d ", dev->drvmap[i]);
  839. p = istatbuf + strlen(istatbuf);
  840. }
  841. sprintf(p, "\nusage:\t");
  842. p = istatbuf + strlen(istatbuf);
  843. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  844. sprintf(p, "%d ", dev->usage[i]);
  845. p = istatbuf + strlen(istatbuf);
  846. }
  847. sprintf(p, "\nflags:\t");
  848. p = istatbuf + strlen(istatbuf);
  849. for (i = 0; i < ISDN_MAX_DRIVERS; i++) {
  850. if (dev->drv[i]) {
  851. sprintf(p, "%ld ", dev->drv[i]->online);
  852. p = istatbuf + strlen(istatbuf);
  853. } else {
  854. sprintf(p, "? ");
  855. p = istatbuf + strlen(istatbuf);
  856. }
  857. }
  858. sprintf(p, "\nphone:\t");
  859. p = istatbuf + strlen(istatbuf);
  860. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  861. sprintf(p, "%s ", dev->num[i]);
  862. p = istatbuf + strlen(istatbuf);
  863. }
  864. sprintf(p, "\n");
  865. return istatbuf;
  866. }
  867. /* Module interface-code */
  868. void
  869. isdn_info_update(void)
  870. {
  871. infostruct *p = dev->infochain;
  872. while (p) {
  873. *(p->private) = 1;
  874. p = (infostruct *) p->next;
  875. }
  876. wake_up_interruptible(&(dev->info_waitq));
  877. }
  878. static ssize_t
  879. isdn_read(struct file *file, char __user *buf, size_t count, loff_t * off)
  880. {
  881. uint minor = MINOR(file->f_dentry->d_inode->i_rdev);
  882. int len = 0;
  883. int drvidx;
  884. int chidx;
  885. int retval;
  886. char *p;
  887. lock_kernel();
  888. if (minor == ISDN_MINOR_STATUS) {
  889. if (!file->private_data) {
  890. if (file->f_flags & O_NONBLOCK) {
  891. retval = -EAGAIN;
  892. goto out;
  893. }
  894. interruptible_sleep_on(&(dev->info_waitq));
  895. }
  896. p = isdn_statstr();
  897. file->private_data = NULL;
  898. if ((len = strlen(p)) <= count) {
  899. if (copy_to_user(buf, p, len)) {
  900. retval = -EFAULT;
  901. goto out;
  902. }
  903. *off += len;
  904. retval = len;
  905. goto out;
  906. }
  907. retval = 0;
  908. goto out;
  909. }
  910. if (!dev->drivers) {
  911. retval = -ENODEV;
  912. goto out;
  913. }
  914. if (minor <= ISDN_MINOR_BMAX) {
  915. printk(KERN_WARNING "isdn_read minor %d obsolete!\n", minor);
  916. drvidx = isdn_minor2drv(minor);
  917. if (drvidx < 0) {
  918. retval = -ENODEV;
  919. goto out;
  920. }
  921. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
  922. retval = -ENODEV;
  923. goto out;
  924. }
  925. chidx = isdn_minor2chan(minor);
  926. if (!(p = kmalloc(count, GFP_KERNEL))) {
  927. retval = -ENOMEM;
  928. goto out;
  929. }
  930. len = isdn_readbchan(drvidx, chidx, p, NULL, count,
  931. &dev->drv[drvidx]->rcv_waitq[chidx]);
  932. *off += len;
  933. if (copy_to_user(buf,p,len))
  934. len = -EFAULT;
  935. kfree(p);
  936. retval = len;
  937. goto out;
  938. }
  939. if (minor <= ISDN_MINOR_CTRLMAX) {
  940. drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
  941. if (drvidx < 0) {
  942. retval = -ENODEV;
  943. goto out;
  944. }
  945. if (!dev->drv[drvidx]->stavail) {
  946. if (file->f_flags & O_NONBLOCK) {
  947. retval = -EAGAIN;
  948. goto out;
  949. }
  950. interruptible_sleep_on(&(dev->drv[drvidx]->st_waitq));
  951. }
  952. if (dev->drv[drvidx]->interface->readstat) {
  953. if (count > dev->drv[drvidx]->stavail)
  954. count = dev->drv[drvidx]->stavail;
  955. len = dev->drv[drvidx]->interface->
  956. readstat(buf, count, drvidx,
  957. isdn_minor2chan(minor));
  958. } else {
  959. len = 0;
  960. }
  961. if (len)
  962. dev->drv[drvidx]->stavail -= len;
  963. else
  964. dev->drv[drvidx]->stavail = 0;
  965. *off += len;
  966. retval = len;
  967. goto out;
  968. }
  969. #ifdef CONFIG_ISDN_PPP
  970. if (minor <= ISDN_MINOR_PPPMAX) {
  971. retval = isdn_ppp_read(minor - ISDN_MINOR_PPP, file, buf, count);
  972. goto out;
  973. }
  974. #endif
  975. retval = -ENODEV;
  976. out:
  977. unlock_kernel();
  978. return retval;
  979. }
  980. static ssize_t
  981. isdn_write(struct file *file, const char __user *buf, size_t count, loff_t * off)
  982. {
  983. uint minor = MINOR(file->f_dentry->d_inode->i_rdev);
  984. int drvidx;
  985. int chidx;
  986. int retval;
  987. if (minor == ISDN_MINOR_STATUS)
  988. return -EPERM;
  989. if (!dev->drivers)
  990. return -ENODEV;
  991. lock_kernel();
  992. if (minor <= ISDN_MINOR_BMAX) {
  993. printk(KERN_WARNING "isdn_write minor %d obsolete!\n", minor);
  994. drvidx = isdn_minor2drv(minor);
  995. if (drvidx < 0) {
  996. retval = -ENODEV;
  997. goto out;
  998. }
  999. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING)) {
  1000. retval = -ENODEV;
  1001. goto out;
  1002. }
  1003. chidx = isdn_minor2chan(minor);
  1004. while (isdn_writebuf_stub(drvidx, chidx, buf, count) != count)
  1005. interruptible_sleep_on(&dev->drv[drvidx]->snd_waitq[chidx]);
  1006. retval = count;
  1007. goto out;
  1008. }
  1009. if (minor <= ISDN_MINOR_CTRLMAX) {
  1010. drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
  1011. if (drvidx < 0) {
  1012. retval = -ENODEV;
  1013. goto out;
  1014. }
  1015. /*
  1016. * We want to use the isdnctrl device to load the firmware
  1017. *
  1018. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
  1019. return -ENODEV;
  1020. */
  1021. if (dev->drv[drvidx]->interface->writecmd)
  1022. retval = dev->drv[drvidx]->interface->
  1023. writecmd(buf, count, drvidx, isdn_minor2chan(minor));
  1024. else
  1025. retval = count;
  1026. goto out;
  1027. }
  1028. #ifdef CONFIG_ISDN_PPP
  1029. if (minor <= ISDN_MINOR_PPPMAX) {
  1030. retval = isdn_ppp_write(minor - ISDN_MINOR_PPP, file, buf, count);
  1031. goto out;
  1032. }
  1033. #endif
  1034. retval = -ENODEV;
  1035. out:
  1036. unlock_kernel();
  1037. return retval;
  1038. }
  1039. static unsigned int
  1040. isdn_poll(struct file *file, poll_table * wait)
  1041. {
  1042. unsigned int mask = 0;
  1043. unsigned int minor = MINOR(file->f_dentry->d_inode->i_rdev);
  1044. int drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
  1045. lock_kernel();
  1046. if (minor == ISDN_MINOR_STATUS) {
  1047. poll_wait(file, &(dev->info_waitq), wait);
  1048. /* mask = POLLOUT | POLLWRNORM; */
  1049. if (file->private_data) {
  1050. mask |= POLLIN | POLLRDNORM;
  1051. }
  1052. goto out;
  1053. }
  1054. if (minor >= ISDN_MINOR_CTRL && minor <= ISDN_MINOR_CTRLMAX) {
  1055. if (drvidx < 0) {
  1056. /* driver deregistered while file open */
  1057. mask = POLLHUP;
  1058. goto out;
  1059. }
  1060. poll_wait(file, &(dev->drv[drvidx]->st_waitq), wait);
  1061. mask = POLLOUT | POLLWRNORM;
  1062. if (dev->drv[drvidx]->stavail) {
  1063. mask |= POLLIN | POLLRDNORM;
  1064. }
  1065. goto out;
  1066. }
  1067. #ifdef CONFIG_ISDN_PPP
  1068. if (minor <= ISDN_MINOR_PPPMAX) {
  1069. mask = isdn_ppp_poll(file, wait);
  1070. goto out;
  1071. }
  1072. #endif
  1073. mask = POLLERR;
  1074. out:
  1075. unlock_kernel();
  1076. return mask;
  1077. }
  1078. static int
  1079. isdn_ioctl(struct inode *inode, struct file *file, uint cmd, ulong arg)
  1080. {
  1081. uint minor = MINOR(inode->i_rdev);
  1082. isdn_ctrl c;
  1083. int drvidx;
  1084. int chidx;
  1085. int ret;
  1086. int i;
  1087. char __user *p;
  1088. char *s;
  1089. union iocpar {
  1090. char name[10];
  1091. char bname[22];
  1092. isdn_ioctl_struct iocts;
  1093. isdn_net_ioctl_phone phone;
  1094. isdn_net_ioctl_cfg cfg;
  1095. } iocpar;
  1096. void __user *argp = (void __user *)arg;
  1097. #define name iocpar.name
  1098. #define bname iocpar.bname
  1099. #define iocts iocpar.iocts
  1100. #define phone iocpar.phone
  1101. #define cfg iocpar.cfg
  1102. if (minor == ISDN_MINOR_STATUS) {
  1103. switch (cmd) {
  1104. case IIOCGETDVR:
  1105. return (TTY_DV +
  1106. (NET_DV << 8) +
  1107. (INF_DV << 16));
  1108. case IIOCGETCPS:
  1109. if (arg) {
  1110. ulong __user *p = argp;
  1111. int i;
  1112. if (!access_ok(VERIFY_WRITE, p,
  1113. sizeof(ulong) * ISDN_MAX_CHANNELS * 2))
  1114. return -EFAULT;
  1115. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1116. put_user(dev->ibytes[i], p++);
  1117. put_user(dev->obytes[i], p++);
  1118. }
  1119. return 0;
  1120. } else
  1121. return -EINVAL;
  1122. break;
  1123. #ifdef CONFIG_NETDEVICES
  1124. case IIOCNETGPN:
  1125. /* Get peer phone number of a connected
  1126. * isdn network interface */
  1127. if (arg) {
  1128. if (copy_from_user(&phone, argp, sizeof(phone)))
  1129. return -EFAULT;
  1130. return isdn_net_getpeer(&phone, argp);
  1131. } else
  1132. return -EINVAL;
  1133. #endif
  1134. default:
  1135. return -EINVAL;
  1136. }
  1137. }
  1138. if (!dev->drivers)
  1139. return -ENODEV;
  1140. if (minor <= ISDN_MINOR_BMAX) {
  1141. drvidx = isdn_minor2drv(minor);
  1142. if (drvidx < 0)
  1143. return -ENODEV;
  1144. chidx = isdn_minor2chan(minor);
  1145. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
  1146. return -ENODEV;
  1147. return 0;
  1148. }
  1149. if (minor <= ISDN_MINOR_CTRLMAX) {
  1150. /*
  1151. * isdn net devices manage lots of configuration variables as linked lists.
  1152. * Those lists must only be manipulated from user space. Some of the ioctl's
  1153. * service routines access user space and are not atomic. Therefor, ioctl's
  1154. * manipulating the lists and ioctl's sleeping while accessing the lists
  1155. * are serialized by means of a semaphore.
  1156. */
  1157. switch (cmd) {
  1158. case IIOCNETDWRSET:
  1159. printk(KERN_INFO "INFO: ISDN_DW_ABC_EXTENSION not enabled\n");
  1160. return(-EINVAL);
  1161. case IIOCNETLCR:
  1162. printk(KERN_INFO "INFO: ISDN_ABC_LCR_SUPPORT not enabled\n");
  1163. return -ENODEV;
  1164. #ifdef CONFIG_NETDEVICES
  1165. case IIOCNETAIF:
  1166. /* Add a network-interface */
  1167. if (arg) {
  1168. if (copy_from_user(name, argp, sizeof(name)))
  1169. return -EFAULT;
  1170. s = name;
  1171. } else {
  1172. s = NULL;
  1173. }
  1174. ret = down_interruptible(&dev->sem);
  1175. if( ret ) return ret;
  1176. if ((s = isdn_net_new(s, NULL))) {
  1177. if (copy_to_user(argp, s, strlen(s) + 1)){
  1178. ret = -EFAULT;
  1179. } else {
  1180. ret = 0;
  1181. }
  1182. } else
  1183. ret = -ENODEV;
  1184. up(&dev->sem);
  1185. return ret;
  1186. case IIOCNETASL:
  1187. /* Add a slave to a network-interface */
  1188. if (arg) {
  1189. if (copy_from_user(bname, argp, sizeof(bname) - 1))
  1190. return -EFAULT;
  1191. } else
  1192. return -EINVAL;
  1193. ret = down_interruptible(&dev->sem);
  1194. if( ret ) return ret;
  1195. if ((s = isdn_net_newslave(bname))) {
  1196. if (copy_to_user(argp, s, strlen(s) + 1)){
  1197. ret = -EFAULT;
  1198. } else {
  1199. ret = 0;
  1200. }
  1201. } else
  1202. ret = -ENODEV;
  1203. up(&dev->sem);
  1204. return ret;
  1205. case IIOCNETDIF:
  1206. /* Delete a network-interface */
  1207. if (arg) {
  1208. if (copy_from_user(name, argp, sizeof(name)))
  1209. return -EFAULT;
  1210. ret = down_interruptible(&dev->sem);
  1211. if( ret ) return ret;
  1212. ret = isdn_net_rm(name);
  1213. up(&dev->sem);
  1214. return ret;
  1215. } else
  1216. return -EINVAL;
  1217. case IIOCNETSCF:
  1218. /* Set configurable parameters of a network-interface */
  1219. if (arg) {
  1220. if (copy_from_user(&cfg, argp, sizeof(cfg)))
  1221. return -EFAULT;
  1222. return isdn_net_setcfg(&cfg);
  1223. } else
  1224. return -EINVAL;
  1225. case IIOCNETGCF:
  1226. /* Get configurable parameters of a network-interface */
  1227. if (arg) {
  1228. if (copy_from_user(&cfg, argp, sizeof(cfg)))
  1229. return -EFAULT;
  1230. if (!(ret = isdn_net_getcfg(&cfg))) {
  1231. if (copy_to_user(argp, &cfg, sizeof(cfg)))
  1232. return -EFAULT;
  1233. }
  1234. return ret;
  1235. } else
  1236. return -EINVAL;
  1237. case IIOCNETANM:
  1238. /* Add a phone-number to a network-interface */
  1239. if (arg) {
  1240. if (copy_from_user(&phone, argp, sizeof(phone)))
  1241. return -EFAULT;
  1242. ret = down_interruptible(&dev->sem);
  1243. if( ret ) return ret;
  1244. ret = isdn_net_addphone(&phone);
  1245. up(&dev->sem);
  1246. return ret;
  1247. } else
  1248. return -EINVAL;
  1249. case IIOCNETGNM:
  1250. /* Get list of phone-numbers of a network-interface */
  1251. if (arg) {
  1252. if (copy_from_user(&phone, argp, sizeof(phone)))
  1253. return -EFAULT;
  1254. ret = down_interruptible(&dev->sem);
  1255. if( ret ) return ret;
  1256. ret = isdn_net_getphones(&phone, argp);
  1257. up(&dev->sem);
  1258. return ret;
  1259. } else
  1260. return -EINVAL;
  1261. case IIOCNETDNM:
  1262. /* Delete a phone-number of a network-interface */
  1263. if (arg) {
  1264. if (copy_from_user(&phone, argp, sizeof(phone)))
  1265. return -EFAULT;
  1266. ret = down_interruptible(&dev->sem);
  1267. if( ret ) return ret;
  1268. ret = isdn_net_delphone(&phone);
  1269. up(&dev->sem);
  1270. return ret;
  1271. } else
  1272. return -EINVAL;
  1273. case IIOCNETDIL:
  1274. /* Force dialing of a network-interface */
  1275. if (arg) {
  1276. if (copy_from_user(name, argp, sizeof(name)))
  1277. return -EFAULT;
  1278. return isdn_net_force_dial(name);
  1279. } else
  1280. return -EINVAL;
  1281. #ifdef CONFIG_ISDN_PPP
  1282. case IIOCNETALN:
  1283. if (!arg)
  1284. return -EINVAL;
  1285. if (copy_from_user(name, argp, sizeof(name)))
  1286. return -EFAULT;
  1287. return isdn_ppp_dial_slave(name);
  1288. case IIOCNETDLN:
  1289. if (!arg)
  1290. return -EINVAL;
  1291. if (copy_from_user(name, argp, sizeof(name)))
  1292. return -EFAULT;
  1293. return isdn_ppp_hangup_slave(name);
  1294. #endif
  1295. case IIOCNETHUP:
  1296. /* Force hangup of a network-interface */
  1297. if (!arg)
  1298. return -EINVAL;
  1299. if (copy_from_user(name, argp, sizeof(name)))
  1300. return -EFAULT;
  1301. return isdn_net_force_hangup(name);
  1302. break;
  1303. #endif /* CONFIG_NETDEVICES */
  1304. case IIOCSETVER:
  1305. dev->net_verbose = arg;
  1306. printk(KERN_INFO "isdn: Verbose-Level is %d\n", dev->net_verbose);
  1307. return 0;
  1308. case IIOCSETGST:
  1309. if (arg)
  1310. dev->global_flags |= ISDN_GLOBAL_STOPPED;
  1311. else
  1312. dev->global_flags &= ~ISDN_GLOBAL_STOPPED;
  1313. printk(KERN_INFO "isdn: Global Mode %s\n",
  1314. (dev->global_flags & ISDN_GLOBAL_STOPPED) ? "stopped" : "running");
  1315. return 0;
  1316. case IIOCSETBRJ:
  1317. drvidx = -1;
  1318. if (arg) {
  1319. int i;
  1320. char *p;
  1321. if (copy_from_user(&iocts, argp,
  1322. sizeof(isdn_ioctl_struct)))
  1323. return -EFAULT;
  1324. if (strlen(iocts.drvid)) {
  1325. if ((p = strchr(iocts.drvid, ',')))
  1326. *p = 0;
  1327. drvidx = -1;
  1328. for (i = 0; i < ISDN_MAX_DRIVERS; i++)
  1329. if (!(strcmp(dev->drvid[i], iocts.drvid))) {
  1330. drvidx = i;
  1331. break;
  1332. }
  1333. }
  1334. }
  1335. if (drvidx == -1)
  1336. return -ENODEV;
  1337. if (iocts.arg)
  1338. dev->drv[drvidx]->flags |= DRV_FLAG_REJBUS;
  1339. else
  1340. dev->drv[drvidx]->flags &= ~DRV_FLAG_REJBUS;
  1341. return 0;
  1342. case IIOCSIGPRF:
  1343. dev->profd = current;
  1344. return 0;
  1345. break;
  1346. case IIOCGETPRF:
  1347. /* Get all Modem-Profiles */
  1348. if (arg) {
  1349. char __user *p = argp;
  1350. int i;
  1351. if (!access_ok(VERIFY_WRITE, argp,
  1352. (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
  1353. * ISDN_MAX_CHANNELS))
  1354. return -EFAULT;
  1355. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1356. if (copy_to_user(p, dev->mdm.info[i].emu.profile,
  1357. ISDN_MODEM_NUMREG))
  1358. return -EFAULT;
  1359. p += ISDN_MODEM_NUMREG;
  1360. if (copy_to_user(p, dev->mdm.info[i].emu.pmsn, ISDN_MSNLEN))
  1361. return -EFAULT;
  1362. p += ISDN_MSNLEN;
  1363. if (copy_to_user(p, dev->mdm.info[i].emu.plmsn, ISDN_LMSNLEN))
  1364. return -EFAULT;
  1365. p += ISDN_LMSNLEN;
  1366. }
  1367. return (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN) * ISDN_MAX_CHANNELS;
  1368. } else
  1369. return -EINVAL;
  1370. break;
  1371. case IIOCSETPRF:
  1372. /* Set all Modem-Profiles */
  1373. if (arg) {
  1374. char __user *p = argp;
  1375. int i;
  1376. if (!access_ok(VERIFY_READ, argp,
  1377. (ISDN_MODEM_NUMREG + ISDN_MSNLEN + ISDN_LMSNLEN)
  1378. * ISDN_MAX_CHANNELS))
  1379. return -EFAULT;
  1380. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  1381. if (copy_from_user(dev->mdm.info[i].emu.profile, p,
  1382. ISDN_MODEM_NUMREG))
  1383. return -EFAULT;
  1384. p += ISDN_MODEM_NUMREG;
  1385. if (copy_from_user(dev->mdm.info[i].emu.plmsn, p, ISDN_LMSNLEN))
  1386. return -EFAULT;
  1387. p += ISDN_LMSNLEN;
  1388. if (copy_from_user(dev->mdm.info[i].emu.pmsn, p, ISDN_MSNLEN))
  1389. return -EFAULT;
  1390. p += ISDN_MSNLEN;
  1391. }
  1392. return 0;
  1393. } else
  1394. return -EINVAL;
  1395. break;
  1396. case IIOCSETMAP:
  1397. case IIOCGETMAP:
  1398. /* Set/Get MSN->EAZ-Mapping for a driver */
  1399. if (arg) {
  1400. if (copy_from_user(&iocts, argp,
  1401. sizeof(isdn_ioctl_struct)))
  1402. return -EFAULT;
  1403. if (strlen(iocts.drvid)) {
  1404. drvidx = -1;
  1405. for (i = 0; i < ISDN_MAX_DRIVERS; i++)
  1406. if (!(strcmp(dev->drvid[i], iocts.drvid))) {
  1407. drvidx = i;
  1408. break;
  1409. }
  1410. } else
  1411. drvidx = 0;
  1412. if (drvidx == -1)
  1413. return -ENODEV;
  1414. if (cmd == IIOCSETMAP) {
  1415. int loop = 1;
  1416. p = (char __user *) iocts.arg;
  1417. i = 0;
  1418. while (loop) {
  1419. int j = 0;
  1420. while (1) {
  1421. if (!access_ok(VERIFY_READ, p, 1))
  1422. return -EFAULT;
  1423. get_user(bname[j], p++);
  1424. switch (bname[j]) {
  1425. case '\0':
  1426. loop = 0;
  1427. /* Fall through */
  1428. case ',':
  1429. bname[j] = '\0';
  1430. strcpy(dev->drv[drvidx]->msn2eaz[i], bname);
  1431. j = ISDN_MSNLEN;
  1432. break;
  1433. default:
  1434. j++;
  1435. }
  1436. if (j >= ISDN_MSNLEN)
  1437. break;
  1438. }
  1439. if (++i > 9)
  1440. break;
  1441. }
  1442. } else {
  1443. p = (char __user *) iocts.arg;
  1444. for (i = 0; i < 10; i++) {
  1445. sprintf(bname, "%s%s",
  1446. strlen(dev->drv[drvidx]->msn2eaz[i]) ?
  1447. dev->drv[drvidx]->msn2eaz[i] : "_",
  1448. (i < 9) ? "," : "\0");
  1449. if (copy_to_user(p, bname, strlen(bname) + 1))
  1450. return -EFAULT;
  1451. p += strlen(bname);
  1452. }
  1453. }
  1454. return 0;
  1455. } else
  1456. return -EINVAL;
  1457. case IIOCDBGVAR:
  1458. if (arg) {
  1459. if (copy_to_user(argp, &dev, sizeof(ulong)))
  1460. return -EFAULT;
  1461. return 0;
  1462. } else
  1463. return -EINVAL;
  1464. break;
  1465. default:
  1466. if ((cmd & IIOCDRVCTL) == IIOCDRVCTL)
  1467. cmd = ((cmd >> _IOC_NRSHIFT) & _IOC_NRMASK) & ISDN_DRVIOCTL_MASK;
  1468. else
  1469. return -EINVAL;
  1470. if (arg) {
  1471. int i;
  1472. char *p;
  1473. if (copy_from_user(&iocts, argp, sizeof(isdn_ioctl_struct)))
  1474. return -EFAULT;
  1475. if (strlen(iocts.drvid)) {
  1476. if ((p = strchr(iocts.drvid, ',')))
  1477. *p = 0;
  1478. drvidx = -1;
  1479. for (i = 0; i < ISDN_MAX_DRIVERS; i++)
  1480. if (!(strcmp(dev->drvid[i], iocts.drvid))) {
  1481. drvidx = i;
  1482. break;
  1483. }
  1484. } else
  1485. drvidx = 0;
  1486. if (drvidx == -1)
  1487. return -ENODEV;
  1488. if (!access_ok(VERIFY_WRITE, argp,
  1489. sizeof(isdn_ioctl_struct)))
  1490. return -EFAULT;
  1491. c.driver = drvidx;
  1492. c.command = ISDN_CMD_IOCTL;
  1493. c.arg = cmd;
  1494. memcpy(c.parm.num, &iocts.arg, sizeof(ulong));
  1495. ret = isdn_command(&c);
  1496. memcpy(&iocts.arg, c.parm.num, sizeof(ulong));
  1497. if (copy_to_user(argp, &iocts, sizeof(isdn_ioctl_struct)))
  1498. return -EFAULT;
  1499. return ret;
  1500. } else
  1501. return -EINVAL;
  1502. }
  1503. }
  1504. #ifdef CONFIG_ISDN_PPP
  1505. if (minor <= ISDN_MINOR_PPPMAX)
  1506. return (isdn_ppp_ioctl(minor - ISDN_MINOR_PPP, file, cmd, arg));
  1507. #endif
  1508. return -ENODEV;
  1509. #undef name
  1510. #undef bname
  1511. #undef iocts
  1512. #undef phone
  1513. #undef cfg
  1514. }
  1515. /*
  1516. * Open the device code.
  1517. */
  1518. static int
  1519. isdn_open(struct inode *ino, struct file *filep)
  1520. {
  1521. uint minor = MINOR(ino->i_rdev);
  1522. int drvidx;
  1523. int chidx;
  1524. int retval = -ENODEV;
  1525. if (minor == ISDN_MINOR_STATUS) {
  1526. infostruct *p;
  1527. if ((p = kmalloc(sizeof(infostruct), GFP_KERNEL))) {
  1528. p->next = (char *) dev->infochain;
  1529. p->private = (char *) &(filep->private_data);
  1530. dev->infochain = p;
  1531. /* At opening we allow a single update */
  1532. filep->private_data = (char *) 1;
  1533. retval = 0;
  1534. goto out;
  1535. } else {
  1536. retval = -ENOMEM;
  1537. goto out;
  1538. }
  1539. }
  1540. if (!dev->channels)
  1541. goto out;
  1542. if (minor <= ISDN_MINOR_BMAX) {
  1543. printk(KERN_WARNING "isdn_open minor %d obsolete!\n", minor);
  1544. drvidx = isdn_minor2drv(minor);
  1545. if (drvidx < 0)
  1546. goto out;
  1547. chidx = isdn_minor2chan(minor);
  1548. if (!(dev->drv[drvidx]->flags & DRV_FLAG_RUNNING))
  1549. goto out;
  1550. if (!(dev->drv[drvidx]->online & (1 << chidx)))
  1551. goto out;
  1552. isdn_lock_drivers();
  1553. retval = 0;
  1554. goto out;
  1555. }
  1556. if (minor <= ISDN_MINOR_CTRLMAX) {
  1557. drvidx = isdn_minor2drv(minor - ISDN_MINOR_CTRL);
  1558. if (drvidx < 0)
  1559. goto out;
  1560. isdn_lock_drivers();
  1561. retval = 0;
  1562. goto out;
  1563. }
  1564. #ifdef CONFIG_ISDN_PPP
  1565. if (minor <= ISDN_MINOR_PPPMAX) {
  1566. retval = isdn_ppp_open(minor - ISDN_MINOR_PPP, filep);
  1567. if (retval == 0)
  1568. isdn_lock_drivers();
  1569. goto out;
  1570. }
  1571. #endif
  1572. out:
  1573. nonseekable_open(ino, filep);
  1574. return retval;
  1575. }
  1576. static int
  1577. isdn_close(struct inode *ino, struct file *filep)
  1578. {
  1579. uint minor = MINOR(ino->i_rdev);
  1580. lock_kernel();
  1581. if (minor == ISDN_MINOR_STATUS) {
  1582. infostruct *p = dev->infochain;
  1583. infostruct *q = NULL;
  1584. while (p) {
  1585. if (p->private == (char *) &(filep->private_data)) {
  1586. if (q)
  1587. q->next = p->next;
  1588. else
  1589. dev->infochain = (infostruct *) (p->next);
  1590. kfree(p);
  1591. goto out;
  1592. }
  1593. q = p;
  1594. p = (infostruct *) (p->next);
  1595. }
  1596. printk(KERN_WARNING "isdn: No private data while closing isdnctrl\n");
  1597. goto out;
  1598. }
  1599. isdn_unlock_drivers();
  1600. if (minor <= ISDN_MINOR_BMAX)
  1601. goto out;
  1602. if (minor <= ISDN_MINOR_CTRLMAX) {
  1603. if (dev->profd == current)
  1604. dev->profd = NULL;
  1605. goto out;
  1606. }
  1607. #ifdef CONFIG_ISDN_PPP
  1608. if (minor <= ISDN_MINOR_PPPMAX)
  1609. isdn_ppp_release(minor - ISDN_MINOR_PPP, filep);
  1610. #endif
  1611. out:
  1612. unlock_kernel();
  1613. return 0;
  1614. }
  1615. static struct file_operations isdn_fops =
  1616. {
  1617. .owner = THIS_MODULE,
  1618. .llseek = no_llseek,
  1619. .read = isdn_read,
  1620. .write = isdn_write,
  1621. .poll = isdn_poll,
  1622. .ioctl = isdn_ioctl,
  1623. .open = isdn_open,
  1624. .release = isdn_close,
  1625. };
  1626. char *
  1627. isdn_map_eaz2msn(char *msn, int di)
  1628. {
  1629. isdn_driver_t *this = dev->drv[di];
  1630. int i;
  1631. if (strlen(msn) == 1) {
  1632. i = msn[0] - '0';
  1633. if ((i >= 0) && (i <= 9))
  1634. if (strlen(this->msn2eaz[i]))
  1635. return (this->msn2eaz[i]);
  1636. }
  1637. return (msn);
  1638. }
  1639. /*
  1640. * Find an unused ISDN-channel, whose feature-flags match the
  1641. * given L2- and L3-protocols.
  1642. */
  1643. #define L2V (~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038))
  1644. /*
  1645. * This function must be called with holding the dev->lock.
  1646. */
  1647. int
  1648. isdn_get_free_channel(int usage, int l2_proto, int l3_proto, int pre_dev
  1649. ,int pre_chan, char *msn)
  1650. {
  1651. int i;
  1652. ulong features;
  1653. ulong vfeatures;
  1654. features = ((1 << l2_proto) | (0x10000 << l3_proto));
  1655. vfeatures = (((1 << l2_proto) | (0x10000 << l3_proto)) &
  1656. ~(ISDN_FEATURE_L2_V11096|ISDN_FEATURE_L2_V11019|ISDN_FEATURE_L2_V11038));
  1657. /* If Layer-2 protocol is V.110, accept drivers with
  1658. * transparent feature even if these don't support V.110
  1659. * because we can emulate this in linklevel.
  1660. */
  1661. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  1662. if (USG_NONE(dev->usage[i]) &&
  1663. (dev->drvmap[i] != -1)) {
  1664. int d = dev->drvmap[i];
  1665. if ((dev->usage[i] & ISDN_USAGE_EXCLUSIVE) &&
  1666. ((pre_dev != d) || (pre_chan != dev->chanmap[i])))
  1667. continue;
  1668. if (!strcmp(isdn_map_eaz2msn(msn, d), "-"))
  1669. continue;
  1670. if (dev->usage[i] & ISDN_USAGE_DISABLED)
  1671. continue; /* usage not allowed */
  1672. if (dev->drv[d]->flags & DRV_FLAG_RUNNING) {
  1673. if (((dev->drv[d]->interface->features & features) == features) ||
  1674. (((dev->drv[d]->interface->features & vfeatures) == vfeatures) &&
  1675. (dev->drv[d]->interface->features & ISDN_FEATURE_L2_TRANS))) {
  1676. if ((pre_dev < 0) || (pre_chan < 0)) {
  1677. dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
  1678. dev->usage[i] |= usage;
  1679. isdn_info_update();
  1680. return i;
  1681. } else {
  1682. if ((pre_dev == d) && (pre_chan == dev->chanmap[i])) {
  1683. dev->usage[i] &= ISDN_USAGE_EXCLUSIVE;
  1684. dev->usage[i] |= usage;
  1685. isdn_info_update();
  1686. return i;
  1687. }
  1688. }
  1689. }
  1690. }
  1691. }
  1692. return -1;
  1693. }
  1694. /*
  1695. * Set state of ISDN-channel to 'unused'
  1696. */
  1697. void
  1698. isdn_free_channel(int di, int ch, int usage)
  1699. {
  1700. int i;
  1701. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  1702. if (((!usage) || ((dev->usage[i] & ISDN_USAGE_MASK) == usage)) &&
  1703. (dev->drvmap[i] == di) &&
  1704. (dev->chanmap[i] == ch)) {
  1705. dev->usage[i] &= (ISDN_USAGE_NONE | ISDN_USAGE_EXCLUSIVE);
  1706. strcpy(dev->num[i], "???");
  1707. dev->ibytes[i] = 0;
  1708. dev->obytes[i] = 0;
  1709. // 20.10.99 JIM, try to reinitialize v110 !
  1710. dev->v110emu[i] = 0;
  1711. atomic_set(&(dev->v110use[i]), 0);
  1712. isdn_v110_close(dev->v110[i]);
  1713. dev->v110[i] = NULL;
  1714. // 20.10.99 JIM, try to reinitialize v110 !
  1715. isdn_info_update();
  1716. skb_queue_purge(&dev->drv[di]->rpqueue[ch]);
  1717. }
  1718. }
  1719. /*
  1720. * Cancel Exclusive-Flag for ISDN-channel
  1721. */
  1722. void
  1723. isdn_unexclusive_channel(int di, int ch)
  1724. {
  1725. int i;
  1726. for (i = 0; i < ISDN_MAX_CHANNELS; i++)
  1727. if ((dev->drvmap[i] == di) &&
  1728. (dev->chanmap[i] == ch)) {
  1729. dev->usage[i] &= ~ISDN_USAGE_EXCLUSIVE;
  1730. isdn_info_update();
  1731. return;
  1732. }
  1733. }
  1734. /*
  1735. * writebuf replacement for SKB_ABLE drivers
  1736. */
  1737. static int
  1738. isdn_writebuf_stub(int drvidx, int chan, const u_char __user * buf, int len)
  1739. {
  1740. int ret;
  1741. int hl = dev->drv[drvidx]->interface->hl_hdrlen;
  1742. struct sk_buff *skb = alloc_skb(hl + len, GFP_ATOMIC);
  1743. if (!skb)
  1744. return 0;
  1745. skb_reserve(skb, hl);
  1746. copy_from_user(skb_put(skb, len), buf, len);
  1747. ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, 1, skb);
  1748. if (ret <= 0)
  1749. dev_kfree_skb(skb);
  1750. if (ret > 0)
  1751. dev->obytes[isdn_dc2minor(drvidx, chan)] += ret;
  1752. return ret;
  1753. }
  1754. /*
  1755. * Return: length of data on success, -ERRcode on failure.
  1756. */
  1757. int
  1758. isdn_writebuf_skb_stub(int drvidx, int chan, int ack, struct sk_buff *skb)
  1759. {
  1760. int ret;
  1761. struct sk_buff *nskb = NULL;
  1762. int v110_ret = skb->len;
  1763. int idx = isdn_dc2minor(drvidx, chan);
  1764. if (dev->v110[idx]) {
  1765. atomic_inc(&dev->v110use[idx]);
  1766. nskb = isdn_v110_encode(dev->v110[idx], skb);
  1767. atomic_dec(&dev->v110use[idx]);
  1768. if (!nskb)
  1769. return 0;
  1770. v110_ret = *((int *)nskb->data);
  1771. skb_pull(nskb, sizeof(int));
  1772. if (!nskb->len) {
  1773. dev_kfree_skb(nskb);
  1774. return v110_ret;
  1775. }
  1776. /* V.110 must always be acknowledged */
  1777. ack = 1;
  1778. ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, nskb);
  1779. } else {
  1780. int hl = dev->drv[drvidx]->interface->hl_hdrlen;
  1781. if( skb_headroom(skb) < hl ){
  1782. /*
  1783. * This should only occur when new HL driver with
  1784. * increased hl_hdrlen was loaded after netdevice
  1785. * was created and connected to the new driver.
  1786. *
  1787. * The V.110 branch (re-allocates on its own) does
  1788. * not need this
  1789. */
  1790. struct sk_buff * skb_tmp;
  1791. skb_tmp = skb_realloc_headroom(skb, hl);
  1792. printk(KERN_DEBUG "isdn_writebuf_skb_stub: reallocating headroom%s\n", skb_tmp ? "" : " failed");
  1793. if (!skb_tmp) return -ENOMEM; /* 0 better? */
  1794. ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb_tmp);
  1795. if( ret > 0 ){
  1796. dev_kfree_skb(skb);
  1797. } else {
  1798. dev_kfree_skb(skb_tmp);
  1799. }
  1800. } else {
  1801. ret = dev->drv[drvidx]->interface->writebuf_skb(drvidx, chan, ack, skb);
  1802. }
  1803. }
  1804. if (ret > 0) {
  1805. dev->obytes[idx] += ret;
  1806. if (dev->v110[idx]) {
  1807. atomic_inc(&dev->v110use[idx]);
  1808. dev->v110[idx]->skbuser++;
  1809. atomic_dec(&dev->v110use[idx]);
  1810. /* For V.110 return unencoded data length */
  1811. ret = v110_ret;
  1812. /* if the complete frame was send we free the skb;
  1813. if not upper function will requeue the skb */
  1814. if (ret == skb->len)
  1815. dev_kfree_skb(skb);
  1816. }
  1817. } else
  1818. if (dev->v110[idx])
  1819. dev_kfree_skb(nskb);
  1820. return ret;
  1821. }
  1822. static int
  1823. isdn_add_channels(isdn_driver_t *d, int drvidx, int n, int adding)
  1824. {
  1825. int j, k, m;
  1826. init_waitqueue_head(&d->st_waitq);
  1827. if (d->flags & DRV_FLAG_RUNNING)
  1828. return -1;
  1829. if (n < 1) return 0;
  1830. m = (adding) ? d->channels + n : n;
  1831. if (dev->channels + n > ISDN_MAX_CHANNELS) {
  1832. printk(KERN_WARNING "register_isdn: Max. %d channels supported\n",
  1833. ISDN_MAX_CHANNELS);
  1834. return -1;
  1835. }
  1836. if ((adding) && (d->rcverr))
  1837. kfree(d->rcverr);
  1838. if (!(d->rcverr = kmalloc(sizeof(int) * m, GFP_ATOMIC))) {
  1839. printk(KERN_WARNING "register_isdn: Could not alloc rcverr\n");
  1840. return -1;
  1841. }
  1842. memset((char *) d->rcverr, 0, sizeof(int) * m);
  1843. if ((adding) && (d->rcvcount))
  1844. kfree(d->rcvcount);
  1845. if (!(d->rcvcount = kmalloc(sizeof(int) * m, GFP_ATOMIC))) {
  1846. printk(KERN_WARNING "register_isdn: Could not alloc rcvcount\n");
  1847. if (!adding)
  1848. kfree(d->rcverr);
  1849. return -1;
  1850. }
  1851. memset((char *) d->rcvcount, 0, sizeof(int) * m);
  1852. if ((adding) && (d->rpqueue)) {
  1853. for (j = 0; j < d->channels; j++)
  1854. skb_queue_purge(&d->rpqueue[j]);
  1855. kfree(d->rpqueue);
  1856. }
  1857. if (!(d->rpqueue = kmalloc(sizeof(struct sk_buff_head) * m, GFP_ATOMIC))) {
  1858. printk(KERN_WARNING "register_isdn: Could not alloc rpqueue\n");
  1859. if (!adding) {
  1860. kfree(d->rcvcount);
  1861. kfree(d->rcverr);
  1862. }
  1863. return -1;
  1864. }
  1865. for (j = 0; j < m; j++) {
  1866. skb_queue_head_init(&d->rpqueue[j]);
  1867. }
  1868. if ((adding) && (d->rcv_waitq))
  1869. kfree(d->rcv_waitq);
  1870. d->rcv_waitq = kmalloc(sizeof(wait_queue_head_t) * 2 * m, GFP_ATOMIC);
  1871. if (!d->rcv_waitq) {
  1872. printk(KERN_WARNING "register_isdn: Could not alloc rcv_waitq\n");
  1873. if (!adding) {
  1874. kfree(d->rpqueue);
  1875. kfree(d->rcvcount);
  1876. kfree(d->rcverr);
  1877. }
  1878. return -1;
  1879. }
  1880. d->snd_waitq = d->rcv_waitq + m;
  1881. for (j = 0; j < m; j++) {
  1882. init_waitqueue_head(&d->rcv_waitq[j]);
  1883. init_waitqueue_head(&d->snd_waitq[j]);
  1884. }
  1885. dev->channels += n;
  1886. for (j = d->channels; j < m; j++)
  1887. for (k = 0; k < ISDN_MAX_CHANNELS; k++)
  1888. if (dev->chanmap[k] < 0) {
  1889. dev->chanmap[k] = j;
  1890. dev->drvmap[k] = drvidx;
  1891. break;
  1892. }
  1893. d->channels = m;
  1894. return 0;
  1895. }
  1896. /*
  1897. * Low-level-driver registration
  1898. */
  1899. static void
  1900. set_global_features(void)
  1901. {
  1902. int drvidx;
  1903. dev->global_features = 0;
  1904. for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++) {
  1905. if (!dev->drv[drvidx])
  1906. continue;
  1907. if (dev->drv[drvidx]->interface)
  1908. dev->global_features |= dev->drv[drvidx]->interface->features;
  1909. }
  1910. }
  1911. #ifdef CONFIG_ISDN_DIVERSION
  1912. static char *map_drvname(int di)
  1913. {
  1914. if ((di < 0) || (di >= ISDN_MAX_DRIVERS))
  1915. return(NULL);
  1916. return(dev->drvid[di]); /* driver name */
  1917. } /* map_drvname */
  1918. static int map_namedrv(char *id)
  1919. { int i;
  1920. for (i = 0; i < ISDN_MAX_DRIVERS; i++)
  1921. { if (!strcmp(dev->drvid[i],id))
  1922. return(i);
  1923. }
  1924. return(-1);
  1925. } /* map_namedrv */
  1926. int DIVERT_REG_NAME(isdn_divert_if *i_div)
  1927. {
  1928. if (i_div->if_magic != DIVERT_IF_MAGIC)
  1929. return(DIVERT_VER_ERR);
  1930. switch (i_div->cmd)
  1931. {
  1932. case DIVERT_CMD_REL:
  1933. if (divert_if != i_div)
  1934. return(DIVERT_REL_ERR);
  1935. divert_if = NULL; /* free interface */
  1936. return(DIVERT_NO_ERR);
  1937. case DIVERT_CMD_REG:
  1938. if (divert_if)
  1939. return(DIVERT_REG_ERR);
  1940. i_div->ll_cmd = isdn_command; /* set command function */
  1941. i_div->drv_to_name = map_drvname;
  1942. i_div->name_to_drv = map_namedrv;
  1943. divert_if = i_div; /* remember interface */
  1944. return(DIVERT_NO_ERR);
  1945. default:
  1946. return(DIVERT_CMD_ERR);
  1947. }
  1948. } /* DIVERT_REG_NAME */
  1949. EXPORT_SYMBOL(DIVERT_REG_NAME);
  1950. #endif /* CONFIG_ISDN_DIVERSION */
  1951. EXPORT_SYMBOL(register_isdn);
  1952. #ifdef CONFIG_ISDN_PPP
  1953. EXPORT_SYMBOL(isdn_ppp_register_compressor);
  1954. EXPORT_SYMBOL(isdn_ppp_unregister_compressor);
  1955. #endif
  1956. int
  1957. register_isdn(isdn_if * i)
  1958. {
  1959. isdn_driver_t *d;
  1960. int j;
  1961. ulong flags;
  1962. int drvidx;
  1963. if (dev->drivers >= ISDN_MAX_DRIVERS) {
  1964. printk(KERN_WARNING "register_isdn: Max. %d drivers supported\n",
  1965. ISDN_MAX_DRIVERS);
  1966. return 0;
  1967. }
  1968. if (!i->writebuf_skb) {
  1969. printk(KERN_WARNING "register_isdn: No write routine given.\n");
  1970. return 0;
  1971. }
  1972. if (!(d = kmalloc(sizeof(isdn_driver_t), GFP_KERNEL))) {
  1973. printk(KERN_WARNING "register_isdn: Could not alloc driver-struct\n");
  1974. return 0;
  1975. }
  1976. memset((char *) d, 0, sizeof(isdn_driver_t));
  1977. d->maxbufsize = i->maxbufsize;
  1978. d->pktcount = 0;
  1979. d->stavail = 0;
  1980. d->flags = DRV_FLAG_LOADED;
  1981. d->online = 0;
  1982. d->interface = i;
  1983. d->channels = 0;
  1984. spin_lock_irqsave(&dev->lock, flags);
  1985. for (drvidx = 0; drvidx < ISDN_MAX_DRIVERS; drvidx++)
  1986. if (!dev->drv[drvidx])
  1987. break;
  1988. if (isdn_add_channels(d, drvidx, i->channels, 0)) {
  1989. spin_unlock_irqrestore(&dev->lock, flags);
  1990. kfree(d);
  1991. return 0;
  1992. }
  1993. i->channels = drvidx;
  1994. i->rcvcallb_skb = isdn_receive_skb_callback;
  1995. i->statcallb = isdn_status_callback;
  1996. if (!strlen(i->id))
  1997. sprintf(i->id, "line%d", drvidx);
  1998. for (j = 0; j < drvidx; j++)
  1999. if (!strcmp(i->id, dev->drvid[j]))
  2000. sprintf(i->id, "line%d", drvidx);
  2001. dev->drv[drvidx] = d;
  2002. strcpy(dev->drvid[drvidx], i->id);
  2003. isdn_info_update();
  2004. dev->drivers++;
  2005. set_global_features();
  2006. spin_unlock_irqrestore(&dev->lock, flags);
  2007. return 1;
  2008. }
  2009. /*
  2010. *****************************************************************************
  2011. * And now the modules code.
  2012. *****************************************************************************
  2013. */
  2014. static char *
  2015. isdn_getrev(const char *revision)
  2016. {
  2017. char *rev;
  2018. char *p;
  2019. if ((p = strchr(revision, ':'))) {
  2020. rev = p + 2;
  2021. p = strchr(rev, '$');
  2022. *--p = 0;
  2023. } else
  2024. rev = "???";
  2025. return rev;
  2026. }
  2027. /*
  2028. * Allocate and initialize all data, register modem-devices
  2029. */
  2030. static int __init isdn_init(void)
  2031. {
  2032. int i;
  2033. char tmprev[50];
  2034. if (!(dev = (isdn_dev *) vmalloc(sizeof(isdn_dev)))) {
  2035. printk(KERN_WARNING "isdn: Could not allocate device-struct.\n");
  2036. return -EIO;
  2037. }
  2038. memset((char *) dev, 0, sizeof(isdn_dev));
  2039. init_timer(&dev->timer);
  2040. dev->timer.function = isdn_timer_funct;
  2041. spin_lock_init(&dev->lock);
  2042. spin_lock_init(&dev->timerlock);
  2043. #ifdef MODULE
  2044. dev->owner = THIS_MODULE;
  2045. #endif
  2046. init_MUTEX(&dev->sem);
  2047. init_waitqueue_head(&dev->info_waitq);
  2048. for (i = 0; i < ISDN_MAX_CHANNELS; i++) {
  2049. dev->drvmap[i] = -1;
  2050. dev->chanmap[i] = -1;
  2051. dev->m_idx[i] = -1;
  2052. strcpy(dev->num[i], "???");
  2053. init_waitqueue_head(&dev->mdm.info[i].open_wait);
  2054. init_waitqueue_head(&dev->mdm.info[i].close_wait);
  2055. }
  2056. if (register_chrdev(ISDN_MAJOR, "isdn", &isdn_fops)) {
  2057. printk(KERN_WARNING "isdn: Could not register control devices\n");
  2058. vfree(dev);
  2059. return -EIO;
  2060. }
  2061. if ((isdn_tty_modem_init()) < 0) {
  2062. printk(KERN_WARNING "isdn: Could not register tty devices\n");
  2063. vfree(dev);
  2064. unregister_chrdev(ISDN_MAJOR, "isdn");
  2065. return -EIO;
  2066. }
  2067. #ifdef CONFIG_ISDN_PPP
  2068. if (isdn_ppp_init() < 0) {
  2069. printk(KERN_WARNING "isdn: Could not create PPP-device-structs\n");
  2070. isdn_tty_exit();
  2071. unregister_chrdev(ISDN_MAJOR, "isdn");
  2072. vfree(dev);
  2073. return -EIO;
  2074. }
  2075. #endif /* CONFIG_ISDN_PPP */
  2076. strcpy(tmprev, isdn_revision);
  2077. printk(KERN_NOTICE "ISDN subsystem Rev: %s/", isdn_getrev(tmprev));
  2078. strcpy(tmprev, isdn_tty_revision);
  2079. printk("%s/", isdn_getrev(tmprev));
  2080. strcpy(tmprev, isdn_net_revision);
  2081. printk("%s/", isdn_getrev(tmprev));
  2082. strcpy(tmprev, isdn_ppp_revision);
  2083. printk("%s/", isdn_getrev(tmprev));
  2084. strcpy(tmprev, isdn_audio_revision);
  2085. printk("%s/", isdn_getrev(tmprev));
  2086. strcpy(tmprev, isdn_v110_revision);
  2087. printk("%s", isdn_getrev(tmprev));
  2088. #ifdef MODULE
  2089. printk(" loaded\n");
  2090. #else
  2091. printk("\n");
  2092. #endif
  2093. isdn_info_update();
  2094. return 0;
  2095. }
  2096. /*
  2097. * Unload module
  2098. */
  2099. static void __exit isdn_exit(void)
  2100. {
  2101. #ifdef CONFIG_ISDN_PPP
  2102. isdn_ppp_cleanup();
  2103. #endif
  2104. if (isdn_net_rmall() < 0) {
  2105. printk(KERN_WARNING "isdn: net-device busy, remove cancelled\n");
  2106. return;
  2107. }
  2108. isdn_tty_exit();
  2109. unregister_chrdev(ISDN_MAJOR, "isdn");
  2110. del_timer(&dev->timer);
  2111. /* call vfree with interrupts enabled, else it will hang */
  2112. vfree(dev);
  2113. printk(KERN_NOTICE "ISDN-subsystem unloaded\n");
  2114. }
  2115. module_init(isdn_init);
  2116. module_exit(isdn_exit);