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