isdn_common.c 56 KB

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