capidrv.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  1. /* $Id: capidrv.c,v 1.1.2.2 2004/01/12 23:17:24 keil Exp $
  2. *
  3. * ISDN4Linux Driver, using capi20 interface (kernelcapi)
  4. *
  5. * Copyright 1997 by Carsten Paeth <calle@calle.de>
  6. *
  7. * This software may be used and distributed according to the terms
  8. * of the GNU General Public License, incorporated herein by reference.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/major.h>
  15. #include <linux/slab.h>
  16. #include <linux/fcntl.h>
  17. #include <linux/fs.h>
  18. #include <linux/signal.h>
  19. #include <linux/mm.h>
  20. #include <linux/timer.h>
  21. #include <linux/wait.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/isdn.h>
  24. #include <linux/isdnif.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/capi.h>
  27. #include <linux/kernelcapi.h>
  28. #include <linux/ctype.h>
  29. #include <linux/init.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/isdn/capiutil.h>
  32. #include <linux/isdn/capicmd.h>
  33. #include "capidrv.h"
  34. static char *revision = "$Revision: 1.1.2.2 $";
  35. static int debugmode = 0;
  36. MODULE_DESCRIPTION("CAPI4Linux: Interface to ISDN4Linux");
  37. MODULE_AUTHOR("Carsten Paeth");
  38. MODULE_LICENSE("GPL");
  39. module_param(debugmode, uint, 0);
  40. /* -------- type definitions ----------------------------------------- */
  41. struct capidrv_contr {
  42. struct capidrv_contr *next;
  43. struct module *owner;
  44. u32 contrnr;
  45. char name[20];
  46. /*
  47. * for isdn4linux
  48. */
  49. isdn_if interface;
  50. int myid;
  51. /*
  52. * LISTEN state
  53. */
  54. int state;
  55. u32 cipmask;
  56. u32 cipmask2;
  57. struct timer_list listentimer;
  58. /*
  59. * ID of capi message sent
  60. */
  61. u16 msgid;
  62. /*
  63. * B-Channels
  64. */
  65. int nbchan;
  66. struct capidrv_bchan {
  67. struct capidrv_contr *contr;
  68. u8 msn[ISDN_MSNLEN];
  69. int l2;
  70. int l3;
  71. u8 num[ISDN_MSNLEN];
  72. u8 mynum[ISDN_MSNLEN];
  73. int si1;
  74. int si2;
  75. int incoming;
  76. int disconnecting;
  77. struct capidrv_plci {
  78. struct capidrv_plci *next;
  79. u32 plci;
  80. u32 ncci; /* ncci for CONNECT_ACTIVE_IND */
  81. u16 msgid; /* to identfy CONNECT_CONF */
  82. int chan;
  83. int state;
  84. int leasedline;
  85. struct capidrv_ncci {
  86. struct capidrv_ncci *next;
  87. struct capidrv_plci *plcip;
  88. u32 ncci;
  89. u16 msgid; /* to identfy CONNECT_B3_CONF */
  90. int chan;
  91. int state;
  92. int oldstate;
  93. /* */
  94. u16 datahandle;
  95. struct ncci_datahandle_queue {
  96. struct ncci_datahandle_queue *next;
  97. u16 datahandle;
  98. int len;
  99. } *ackqueue;
  100. } *ncci_list;
  101. } *plcip;
  102. struct capidrv_ncci *nccip;
  103. } *bchans;
  104. struct capidrv_plci *plci_list;
  105. /* for q931 data */
  106. u8 q931_buf[4096];
  107. u8 *q931_read;
  108. u8 *q931_write;
  109. u8 *q931_end;
  110. };
  111. struct capidrv_data {
  112. struct capi20_appl ap;
  113. int ncontr;
  114. struct capidrv_contr *contr_list;
  115. };
  116. typedef struct capidrv_plci capidrv_plci;
  117. typedef struct capidrv_ncci capidrv_ncci;
  118. typedef struct capidrv_contr capidrv_contr;
  119. typedef struct capidrv_data capidrv_data;
  120. typedef struct capidrv_bchan capidrv_bchan;
  121. /* -------- data definitions ----------------------------------------- */
  122. static capidrv_data global;
  123. static DEFINE_SPINLOCK(global_lock);
  124. static void handle_dtrace_data(capidrv_contr *card,
  125. int send, int level2, u8 *data, u16 len);
  126. /* -------- convert functions ---------------------------------------- */
  127. static inline u32 b1prot(int l2, int l3)
  128. {
  129. switch (l2) {
  130. case ISDN_PROTO_L2_X75I:
  131. case ISDN_PROTO_L2_X75UI:
  132. case ISDN_PROTO_L2_X75BUI:
  133. return 0;
  134. case ISDN_PROTO_L2_HDLC:
  135. default:
  136. return 0;
  137. case ISDN_PROTO_L2_TRANS:
  138. return 1;
  139. case ISDN_PROTO_L2_V11096:
  140. case ISDN_PROTO_L2_V11019:
  141. case ISDN_PROTO_L2_V11038:
  142. return 2;
  143. case ISDN_PROTO_L2_FAX:
  144. return 4;
  145. case ISDN_PROTO_L2_MODEM:
  146. return 8;
  147. }
  148. }
  149. static inline u32 b2prot(int l2, int l3)
  150. {
  151. switch (l2) {
  152. case ISDN_PROTO_L2_X75I:
  153. case ISDN_PROTO_L2_X75UI:
  154. case ISDN_PROTO_L2_X75BUI:
  155. default:
  156. return 0;
  157. case ISDN_PROTO_L2_HDLC:
  158. case ISDN_PROTO_L2_TRANS:
  159. case ISDN_PROTO_L2_V11096:
  160. case ISDN_PROTO_L2_V11019:
  161. case ISDN_PROTO_L2_V11038:
  162. case ISDN_PROTO_L2_MODEM:
  163. return 1;
  164. case ISDN_PROTO_L2_FAX:
  165. return 4;
  166. }
  167. }
  168. static inline u32 b3prot(int l2, int l3)
  169. {
  170. switch (l2) {
  171. case ISDN_PROTO_L2_X75I:
  172. case ISDN_PROTO_L2_X75UI:
  173. case ISDN_PROTO_L2_X75BUI:
  174. case ISDN_PROTO_L2_HDLC:
  175. case ISDN_PROTO_L2_TRANS:
  176. case ISDN_PROTO_L2_V11096:
  177. case ISDN_PROTO_L2_V11019:
  178. case ISDN_PROTO_L2_V11038:
  179. case ISDN_PROTO_L2_MODEM:
  180. default:
  181. return 0;
  182. case ISDN_PROTO_L2_FAX:
  183. return 4;
  184. }
  185. }
  186. static _cstruct b1config_async_v110(u16 rate)
  187. {
  188. /* CAPI-Spec "B1 Configuration" */
  189. static unsigned char buf[9];
  190. buf[0] = 8; /* len */
  191. /* maximum bitrate */
  192. buf[1] = rate & 0xff; buf[2] = (rate >> 8) & 0xff;
  193. buf[3] = 8; buf[4] = 0; /* 8 bits per character */
  194. buf[5] = 0; buf[6] = 0; /* parity none */
  195. buf[7] = 0; buf[8] = 0; /* 1 stop bit */
  196. return buf;
  197. }
  198. static _cstruct b1config(int l2, int l3)
  199. {
  200. switch (l2) {
  201. case ISDN_PROTO_L2_X75I:
  202. case ISDN_PROTO_L2_X75UI:
  203. case ISDN_PROTO_L2_X75BUI:
  204. case ISDN_PROTO_L2_HDLC:
  205. case ISDN_PROTO_L2_TRANS:
  206. default:
  207. return NULL;
  208. case ISDN_PROTO_L2_V11096:
  209. return b1config_async_v110(9600);
  210. case ISDN_PROTO_L2_V11019:
  211. return b1config_async_v110(19200);
  212. case ISDN_PROTO_L2_V11038:
  213. return b1config_async_v110(38400);
  214. }
  215. }
  216. static inline u16 si2cip(u8 si1, u8 si2)
  217. {
  218. static const u8 cip[17][5] =
  219. {
  220. /* 0 1 2 3 4 */
  221. {0, 0, 0, 0, 0}, /*0 */
  222. {16, 16, 4, 26, 16}, /*1 */
  223. {17, 17, 17, 4, 4}, /*2 */
  224. {2, 2, 2, 2, 2}, /*3 */
  225. {18, 18, 18, 18, 18}, /*4 */
  226. {2, 2, 2, 2, 2}, /*5 */
  227. {0, 0, 0, 0, 0}, /*6 */
  228. {2, 2, 2, 2, 2}, /*7 */
  229. {2, 2, 2, 2, 2}, /*8 */
  230. {21, 21, 21, 21, 21}, /*9 */
  231. {19, 19, 19, 19, 19}, /*10 */
  232. {0, 0, 0, 0, 0}, /*11 */
  233. {0, 0, 0, 0, 0}, /*12 */
  234. {0, 0, 0, 0, 0}, /*13 */
  235. {0, 0, 0, 0, 0}, /*14 */
  236. {22, 22, 22, 22, 22}, /*15 */
  237. {27, 27, 27, 28, 27} /*16 */
  238. };
  239. if (si1 > 16)
  240. si1 = 0;
  241. if (si2 > 4)
  242. si2 = 0;
  243. return (u16) cip[si1][si2];
  244. }
  245. static inline u8 cip2si1(u16 cipval)
  246. {
  247. static const u8 si[32] =
  248. {7, 1, 7, 7, 1, 1, 7, 7, /*0-7 */
  249. 7, 1, 0, 0, 0, 0, 0, 0, /*8-15 */
  250. 1, 2, 4, 10, 9, 9, 15, 7, /*16-23 */
  251. 7, 7, 1, 16, 16, 0, 0, 0}; /*24-31 */
  252. if (cipval > 31)
  253. cipval = 0; /* .... */
  254. return si[cipval];
  255. }
  256. static inline u8 cip2si2(u16 cipval)
  257. {
  258. static const u8 si[32] =
  259. {0, 0, 0, 0, 2, 3, 0, 0, /*0-7 */
  260. 0, 3, 0, 0, 0, 0, 0, 0, /*8-15 */
  261. 1, 2, 0, 0, 9, 0, 0, 0, /*16-23 */
  262. 0, 0, 3, 2, 3, 0, 0, 0}; /*24-31 */
  263. if (cipval > 31)
  264. cipval = 0; /* .... */
  265. return si[cipval];
  266. }
  267. /* -------- controller management ------------------------------------- */
  268. static inline capidrv_contr *findcontrbydriverid(int driverid)
  269. {
  270. unsigned long flags;
  271. capidrv_contr *p;
  272. spin_lock_irqsave(&global_lock, flags);
  273. for (p = global.contr_list; p; p = p->next)
  274. if (p->myid == driverid)
  275. break;
  276. spin_unlock_irqrestore(&global_lock, flags);
  277. return p;
  278. }
  279. static capidrv_contr *findcontrbynumber(u32 contr)
  280. {
  281. unsigned long flags;
  282. capidrv_contr *p = global.contr_list;
  283. spin_lock_irqsave(&global_lock, flags);
  284. for (p = global.contr_list; p; p = p->next)
  285. if (p->contrnr == contr)
  286. break;
  287. spin_unlock_irqrestore(&global_lock, flags);
  288. return p;
  289. }
  290. /* -------- plci management ------------------------------------------ */
  291. static capidrv_plci *new_plci(capidrv_contr * card, int chan)
  292. {
  293. capidrv_plci *plcip;
  294. plcip = kzalloc(sizeof(capidrv_plci), GFP_ATOMIC);
  295. if (plcip == NULL)
  296. return NULL;
  297. plcip->state = ST_PLCI_NONE;
  298. plcip->plci = 0;
  299. plcip->msgid = 0;
  300. plcip->chan = chan;
  301. plcip->next = card->plci_list;
  302. card->plci_list = plcip;
  303. card->bchans[chan].plcip = plcip;
  304. return plcip;
  305. }
  306. static capidrv_plci *find_plci_by_plci(capidrv_contr * card, u32 plci)
  307. {
  308. capidrv_plci *p;
  309. for (p = card->plci_list; p; p = p->next)
  310. if (p->plci == plci)
  311. return p;
  312. return NULL;
  313. }
  314. static capidrv_plci *find_plci_by_msgid(capidrv_contr * card, u16 msgid)
  315. {
  316. capidrv_plci *p;
  317. for (p = card->plci_list; p; p = p->next)
  318. if (p->msgid == msgid)
  319. return p;
  320. return NULL;
  321. }
  322. static capidrv_plci *find_plci_by_ncci(capidrv_contr * card, u32 ncci)
  323. {
  324. capidrv_plci *p;
  325. for (p = card->plci_list; p; p = p->next)
  326. if (p->plci == (ncci & 0xffff))
  327. return p;
  328. return NULL;
  329. }
  330. static void free_plci(capidrv_contr * card, capidrv_plci * plcip)
  331. {
  332. capidrv_plci **pp;
  333. for (pp = &card->plci_list; *pp; pp = &(*pp)->next) {
  334. if (*pp == plcip) {
  335. *pp = (*pp)->next;
  336. card->bchans[plcip->chan].plcip = NULL;
  337. card->bchans[plcip->chan].disconnecting = 0;
  338. card->bchans[plcip->chan].incoming = 0;
  339. kfree(plcip);
  340. return;
  341. }
  342. }
  343. printk(KERN_ERR "capidrv-%d: free_plci %p (0x%x) not found, Huh?\n",
  344. card->contrnr, plcip, plcip->plci);
  345. }
  346. /* -------- ncci management ------------------------------------------ */
  347. static inline capidrv_ncci *new_ncci(capidrv_contr * card,
  348. capidrv_plci * plcip,
  349. u32 ncci)
  350. {
  351. capidrv_ncci *nccip;
  352. nccip = kzalloc(sizeof(capidrv_ncci), GFP_ATOMIC);
  353. if (nccip == NULL)
  354. return NULL;
  355. nccip->ncci = ncci;
  356. nccip->state = ST_NCCI_NONE;
  357. nccip->plcip = plcip;
  358. nccip->chan = plcip->chan;
  359. nccip->datahandle = 0;
  360. nccip->next = plcip->ncci_list;
  361. plcip->ncci_list = nccip;
  362. card->bchans[plcip->chan].nccip = nccip;
  363. return nccip;
  364. }
  365. static inline capidrv_ncci *find_ncci(capidrv_contr * card, u32 ncci)
  366. {
  367. capidrv_plci *plcip;
  368. capidrv_ncci *p;
  369. if ((plcip = find_plci_by_ncci(card, ncci)) == NULL)
  370. return NULL;
  371. for (p = plcip->ncci_list; p; p = p->next)
  372. if (p->ncci == ncci)
  373. return p;
  374. return NULL;
  375. }
  376. static inline capidrv_ncci *find_ncci_by_msgid(capidrv_contr * card,
  377. u32 ncci, u16 msgid)
  378. {
  379. capidrv_plci *plcip;
  380. capidrv_ncci *p;
  381. if ((plcip = find_plci_by_ncci(card, ncci)) == NULL)
  382. return NULL;
  383. for (p = plcip->ncci_list; p; p = p->next)
  384. if (p->msgid == msgid)
  385. return p;
  386. return NULL;
  387. }
  388. static void free_ncci(capidrv_contr * card, struct capidrv_ncci *nccip)
  389. {
  390. struct capidrv_ncci **pp;
  391. for (pp = &(nccip->plcip->ncci_list); *pp; pp = &(*pp)->next) {
  392. if (*pp == nccip) {
  393. *pp = (*pp)->next;
  394. break;
  395. }
  396. }
  397. card->bchans[nccip->chan].nccip = NULL;
  398. kfree(nccip);
  399. }
  400. static int capidrv_add_ack(struct capidrv_ncci *nccip,
  401. u16 datahandle, int len)
  402. {
  403. struct ncci_datahandle_queue *n, **pp;
  404. n = (struct ncci_datahandle_queue *)
  405. kmalloc(sizeof(struct ncci_datahandle_queue), GFP_ATOMIC);
  406. if (!n) {
  407. printk(KERN_ERR "capidrv: kmalloc ncci_datahandle failed\n");
  408. return -1;
  409. }
  410. n->next = NULL;
  411. n->datahandle = datahandle;
  412. n->len = len;
  413. for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) ;
  414. *pp = n;
  415. return 0;
  416. }
  417. static int capidrv_del_ack(struct capidrv_ncci *nccip, u16 datahandle)
  418. {
  419. struct ncci_datahandle_queue **pp, *p;
  420. int len;
  421. for (pp = &nccip->ackqueue; *pp; pp = &(*pp)->next) {
  422. if ((*pp)->datahandle == datahandle) {
  423. p = *pp;
  424. len = p->len;
  425. *pp = (*pp)->next;
  426. kfree(p);
  427. return len;
  428. }
  429. }
  430. return -1;
  431. }
  432. /* -------- convert and send capi message ---------------------------- */
  433. static void send_message(capidrv_contr * card, _cmsg * cmsg)
  434. {
  435. struct sk_buff *skb;
  436. size_t len;
  437. capi_cmsg2message(cmsg, cmsg->buf);
  438. len = CAPIMSG_LEN(cmsg->buf);
  439. skb = alloc_skb(len, GFP_ATOMIC);
  440. if (!skb) {
  441. printk(KERN_ERR "capidrv::send_message: can't allocate mem\n");
  442. return;
  443. }
  444. memcpy(skb_put(skb, len), cmsg->buf, len);
  445. if (capi20_put_message(&global.ap, skb) != CAPI_NOERROR)
  446. kfree_skb(skb);
  447. }
  448. /* -------- state machine -------------------------------------------- */
  449. struct listenstatechange {
  450. int actstate;
  451. int nextstate;
  452. int event;
  453. };
  454. static struct listenstatechange listentable[] =
  455. {
  456. {ST_LISTEN_NONE, ST_LISTEN_WAIT_CONF, EV_LISTEN_REQ},
  457. {ST_LISTEN_ACTIVE, ST_LISTEN_ACTIVE_WAIT_CONF, EV_LISTEN_REQ},
  458. {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_ERROR},
  459. {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_ERROR},
  460. {ST_LISTEN_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
  461. {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_NONE, EV_LISTEN_CONF_EMPTY},
  462. {ST_LISTEN_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
  463. {ST_LISTEN_ACTIVE_WAIT_CONF, ST_LISTEN_ACTIVE, EV_LISTEN_CONF_OK},
  464. {},
  465. };
  466. static void listen_change_state(capidrv_contr * card, int event)
  467. {
  468. struct listenstatechange *p = listentable;
  469. while (p->event) {
  470. if (card->state == p->actstate && p->event == event) {
  471. if (debugmode)
  472. printk(KERN_DEBUG "capidrv-%d: listen_change_state %d -> %d\n",
  473. card->contrnr, card->state, p->nextstate);
  474. card->state = p->nextstate;
  475. return;
  476. }
  477. p++;
  478. }
  479. printk(KERN_ERR "capidrv-%d: listen_change_state state=%d event=%d ????\n",
  480. card->contrnr, card->state, event);
  481. }
  482. /* ------------------------------------------------------------------ */
  483. static void p0(capidrv_contr * card, capidrv_plci * plci)
  484. {
  485. isdn_ctrl cmd;
  486. card->bchans[plci->chan].contr = NULL;
  487. cmd.command = ISDN_STAT_DHUP;
  488. cmd.driver = card->myid;
  489. cmd.arg = plci->chan;
  490. card->interface.statcallb(&cmd);
  491. free_plci(card, plci);
  492. }
  493. /* ------------------------------------------------------------------ */
  494. struct plcistatechange {
  495. int actstate;
  496. int nextstate;
  497. int event;
  498. void (*changefunc) (capidrv_contr * card, capidrv_plci * plci);
  499. };
  500. static struct plcistatechange plcitable[] =
  501. {
  502. /* P-0 */
  503. {ST_PLCI_NONE, ST_PLCI_OUTGOING, EV_PLCI_CONNECT_REQ, NULL},
  504. {ST_PLCI_NONE, ST_PLCI_ALLOCATED, EV_PLCI_FACILITY_IND_UP, NULL},
  505. {ST_PLCI_NONE, ST_PLCI_INCOMING, EV_PLCI_CONNECT_IND, NULL},
  506. {ST_PLCI_NONE, ST_PLCI_RESUMEING, EV_PLCI_RESUME_REQ, NULL},
  507. /* P-0.1 */
  508. {ST_PLCI_OUTGOING, ST_PLCI_NONE, EV_PLCI_CONNECT_CONF_ERROR, p0},
  509. {ST_PLCI_OUTGOING, ST_PLCI_ALLOCATED, EV_PLCI_CONNECT_CONF_OK, NULL},
  510. /* P-1 */
  511. {ST_PLCI_ALLOCATED, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
  512. {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
  513. {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
  514. {ST_PLCI_ALLOCATED, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
  515. /* P-ACT */
  516. {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
  517. {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
  518. {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
  519. {ST_PLCI_ACTIVE, ST_PLCI_HELD, EV_PLCI_HOLD_IND, NULL},
  520. {ST_PLCI_ACTIVE, ST_PLCI_DISCONNECTING, EV_PLCI_SUSPEND_IND, NULL},
  521. /* P-2 */
  522. {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
  523. {ST_PLCI_INCOMING, ST_PLCI_FACILITY_IND, EV_PLCI_FACILITY_IND_UP, NULL},
  524. {ST_PLCI_INCOMING, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_RESP, NULL},
  525. {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
  526. {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
  527. {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
  528. {ST_PLCI_INCOMING, ST_PLCI_DISCONNECTING, EV_PLCI_CD_IND, NULL},
  529. /* P-3 */
  530. {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_CONNECT_REJECT, NULL},
  531. {ST_PLCI_FACILITY_IND, ST_PLCI_ACCEPTING, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
  532. {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
  533. {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
  534. {ST_PLCI_FACILITY_IND, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
  535. /* P-4 */
  536. {ST_PLCI_ACCEPTING, ST_PLCI_ACTIVE, EV_PLCI_CONNECT_ACTIVE_IND, NULL},
  537. {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_DISCONNECT_REQ, NULL},
  538. {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTING, EV_PLCI_FACILITY_IND_DOWN, NULL},
  539. {ST_PLCI_ACCEPTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
  540. /* P-5 */
  541. {ST_PLCI_DISCONNECTING, ST_PLCI_DISCONNECTED, EV_PLCI_DISCONNECT_IND, NULL},
  542. /* P-6 */
  543. {ST_PLCI_DISCONNECTED, ST_PLCI_NONE, EV_PLCI_DISCONNECT_RESP, p0},
  544. /* P-0.Res */
  545. {ST_PLCI_RESUMEING, ST_PLCI_NONE, EV_PLCI_RESUME_CONF_ERROR, p0},
  546. {ST_PLCI_RESUMEING, ST_PLCI_RESUME, EV_PLCI_RESUME_CONF_OK, NULL},
  547. /* P-RES */
  548. {ST_PLCI_RESUME, ST_PLCI_ACTIVE, EV_PLCI_RESUME_IND, NULL},
  549. /* P-HELD */
  550. {ST_PLCI_HELD, ST_PLCI_ACTIVE, EV_PLCI_RETRIEVE_IND, NULL},
  551. {},
  552. };
  553. static void plci_change_state(capidrv_contr * card, capidrv_plci * plci, int event)
  554. {
  555. struct plcistatechange *p = plcitable;
  556. while (p->event) {
  557. if (plci->state == p->actstate && p->event == event) {
  558. if (debugmode)
  559. printk(KERN_DEBUG "capidrv-%d: plci_change_state:0x%x %d -> %d\n",
  560. card->contrnr, plci->plci, plci->state, p->nextstate);
  561. plci->state = p->nextstate;
  562. if (p->changefunc)
  563. p->changefunc(card, plci);
  564. return;
  565. }
  566. p++;
  567. }
  568. printk(KERN_ERR "capidrv-%d: plci_change_state:0x%x state=%d event=%d ????\n",
  569. card->contrnr, plci->plci, plci->state, event);
  570. }
  571. /* ------------------------------------------------------------------ */
  572. static _cmsg cmsg;
  573. static void n0(capidrv_contr * card, capidrv_ncci * ncci)
  574. {
  575. isdn_ctrl cmd;
  576. capi_fill_DISCONNECT_REQ(&cmsg,
  577. global.ap.applid,
  578. card->msgid++,
  579. ncci->plcip->plci,
  580. NULL, /* BChannelinformation */
  581. NULL, /* Keypadfacility */
  582. NULL, /* Useruserdata */ /* $$$$ */
  583. NULL /* Facilitydataarray */
  584. );
  585. send_message(card, &cmsg);
  586. plci_change_state(card, ncci->plcip, EV_PLCI_DISCONNECT_REQ);
  587. cmd.command = ISDN_STAT_BHUP;
  588. cmd.driver = card->myid;
  589. cmd.arg = ncci->chan;
  590. card->interface.statcallb(&cmd);
  591. free_ncci(card, ncci);
  592. }
  593. /* ------------------------------------------------------------------ */
  594. struct nccistatechange {
  595. int actstate;
  596. int nextstate;
  597. int event;
  598. void (*changefunc) (capidrv_contr * card, capidrv_ncci * ncci);
  599. };
  600. static struct nccistatechange nccitable[] =
  601. {
  602. /* N-0 */
  603. {ST_NCCI_NONE, ST_NCCI_OUTGOING, EV_NCCI_CONNECT_B3_REQ, NULL},
  604. {ST_NCCI_NONE, ST_NCCI_INCOMING, EV_NCCI_CONNECT_B3_IND, NULL},
  605. /* N-0.1 */
  606. {ST_NCCI_OUTGOING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_CONF_OK, NULL},
  607. {ST_NCCI_OUTGOING, ST_NCCI_NONE, EV_NCCI_CONNECT_B3_CONF_ERROR, n0},
  608. /* N-1 */
  609. {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_CONNECT_B3_REJECT, NULL},
  610. {ST_NCCI_INCOMING, ST_NCCI_ALLOCATED, EV_NCCI_CONNECT_B3_RESP, NULL},
  611. {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
  612. {ST_NCCI_INCOMING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
  613. /* N-2 */
  614. {ST_NCCI_ALLOCATED, ST_NCCI_ACTIVE, EV_NCCI_CONNECT_B3_ACTIVE_IND, NULL},
  615. {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
  616. {ST_NCCI_ALLOCATED, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
  617. /* N-ACT */
  618. {ST_NCCI_ACTIVE, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
  619. {ST_NCCI_ACTIVE, ST_NCCI_RESETING, EV_NCCI_RESET_B3_REQ, NULL},
  620. {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
  621. {ST_NCCI_ACTIVE, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
  622. /* N-3 */
  623. {ST_NCCI_RESETING, ST_NCCI_ACTIVE, EV_NCCI_RESET_B3_IND, NULL},
  624. {ST_NCCI_RESETING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
  625. {ST_NCCI_RESETING, ST_NCCI_DISCONNECTING, EV_NCCI_DISCONNECT_B3_REQ, NULL},
  626. /* N-4 */
  627. {ST_NCCI_DISCONNECTING, ST_NCCI_DISCONNECTED, EV_NCCI_DISCONNECT_B3_IND, NULL},
  628. {ST_NCCI_DISCONNECTING, ST_NCCI_PREVIOUS, EV_NCCI_DISCONNECT_B3_CONF_ERROR,NULL},
  629. /* N-5 */
  630. {ST_NCCI_DISCONNECTED, ST_NCCI_NONE, EV_NCCI_DISCONNECT_B3_RESP, n0},
  631. {},
  632. };
  633. static void ncci_change_state(capidrv_contr * card, capidrv_ncci * ncci, int event)
  634. {
  635. struct nccistatechange *p = nccitable;
  636. while (p->event) {
  637. if (ncci->state == p->actstate && p->event == event) {
  638. if (debugmode)
  639. printk(KERN_DEBUG "capidrv-%d: ncci_change_state:0x%x %d -> %d\n",
  640. card->contrnr, ncci->ncci, ncci->state, p->nextstate);
  641. if (p->nextstate == ST_NCCI_PREVIOUS) {
  642. ncci->state = ncci->oldstate;
  643. ncci->oldstate = p->actstate;
  644. } else {
  645. ncci->oldstate = p->actstate;
  646. ncci->state = p->nextstate;
  647. }
  648. if (p->changefunc)
  649. p->changefunc(card, ncci);
  650. return;
  651. }
  652. p++;
  653. }
  654. printk(KERN_ERR "capidrv-%d: ncci_change_state:0x%x state=%d event=%d ????\n",
  655. card->contrnr, ncci->ncci, ncci->state, event);
  656. }
  657. /* ------------------------------------------------------------------- */
  658. static inline int new_bchan(capidrv_contr * card)
  659. {
  660. int i;
  661. for (i = 0; i < card->nbchan; i++) {
  662. if (card->bchans[i].plcip == NULL) {
  663. card->bchans[i].disconnecting = 0;
  664. return i;
  665. }
  666. }
  667. return -1;
  668. }
  669. /* ------------------------------------------------------------------- */
  670. static void handle_controller(_cmsg * cmsg)
  671. {
  672. capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
  673. if (!card) {
  674. printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
  675. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  676. cmsg->adr.adrController & 0x7f);
  677. return;
  678. }
  679. switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
  680. case CAPI_LISTEN_CONF: /* Controller */
  681. if (debugmode)
  682. printk(KERN_DEBUG "capidrv-%d: listenconf Info=0x%4x (%s) cipmask=0x%x\n",
  683. card->contrnr, cmsg->Info, capi_info2str(cmsg->Info), card->cipmask);
  684. if (cmsg->Info) {
  685. listen_change_state(card, EV_LISTEN_CONF_ERROR);
  686. } else if (card->cipmask == 0) {
  687. listen_change_state(card, EV_LISTEN_CONF_EMPTY);
  688. } else {
  689. listen_change_state(card, EV_LISTEN_CONF_OK);
  690. }
  691. break;
  692. case CAPI_MANUFACTURER_IND: /* Controller */
  693. if ( cmsg->ManuID == 0x214D5641
  694. && cmsg->Class == 0
  695. && cmsg->Function == 1) {
  696. u8 *data = cmsg->ManuData+3;
  697. u16 len = cmsg->ManuData[0];
  698. u16 layer;
  699. int direction;
  700. if (len == 255) {
  701. len = (cmsg->ManuData[1] | (cmsg->ManuData[2] << 8));
  702. data += 2;
  703. }
  704. len -= 2;
  705. layer = ((*(data-1)) << 8) | *(data-2);
  706. if (layer & 0x300)
  707. direction = (layer & 0x200) ? 0 : 1;
  708. else direction = (layer & 0x800) ? 0 : 1;
  709. if (layer & 0x0C00) {
  710. if ((layer & 0xff) == 0x80) {
  711. handle_dtrace_data(card, direction, 1, data, len);
  712. break;
  713. }
  714. } else if ((layer & 0xff) < 0x80) {
  715. handle_dtrace_data(card, direction, 0, data, len);
  716. break;
  717. }
  718. printk(KERN_INFO "capidrv-%d: %s from controller 0x%x layer 0x%x, ignored\n",
  719. card->contrnr,
  720. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  721. cmsg->adr.adrController, layer);
  722. break;
  723. }
  724. goto ignored;
  725. case CAPI_MANUFACTURER_CONF: /* Controller */
  726. if (cmsg->ManuID == 0x214D5641) {
  727. char *s = NULL;
  728. switch (cmsg->Class) {
  729. case 0: break;
  730. case 1: s = "unknown class"; break;
  731. case 2: s = "unknown function"; break;
  732. default: s = "unkown error"; break;
  733. }
  734. if (s)
  735. printk(KERN_INFO "capidrv-%d: %s from controller 0x%x function %d: %s\n",
  736. card->contrnr,
  737. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  738. cmsg->adr.adrController,
  739. cmsg->Function, s);
  740. break;
  741. }
  742. goto ignored;
  743. case CAPI_FACILITY_IND: /* Controller/plci/ncci */
  744. goto ignored;
  745. case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
  746. goto ignored;
  747. case CAPI_INFO_IND: /* Controller/plci */
  748. goto ignored;
  749. case CAPI_INFO_CONF: /* Controller/plci */
  750. goto ignored;
  751. default:
  752. printk(KERN_ERR "capidrv-%d: got %s from controller 0x%x ???",
  753. card->contrnr,
  754. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  755. cmsg->adr.adrController);
  756. }
  757. return;
  758. ignored:
  759. printk(KERN_INFO "capidrv-%d: %s from controller 0x%x ignored\n",
  760. card->contrnr,
  761. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  762. cmsg->adr.adrController);
  763. }
  764. static void handle_incoming_call(capidrv_contr * card, _cmsg * cmsg)
  765. {
  766. capidrv_plci *plcip;
  767. capidrv_bchan *bchan;
  768. isdn_ctrl cmd;
  769. int chan;
  770. if ((chan = new_bchan(card)) == -1) {
  771. printk(KERN_ERR "capidrv-%d: incoming call on not existing bchan ?\n", card->contrnr);
  772. return;
  773. }
  774. bchan = &card->bchans[chan];
  775. if ((plcip = new_plci(card, chan)) == NULL) {
  776. printk(KERN_ERR "capidrv-%d: incoming call: no memory, sorry.\n", card->contrnr);
  777. return;
  778. }
  779. bchan->incoming = 1;
  780. plcip->plci = cmsg->adr.adrPLCI;
  781. plci_change_state(card, plcip, EV_PLCI_CONNECT_IND);
  782. cmd.command = ISDN_STAT_ICALL;
  783. cmd.driver = card->myid;
  784. cmd.arg = chan;
  785. memset(&cmd.parm.setup, 0, sizeof(cmd.parm.setup));
  786. strncpy(cmd.parm.setup.phone,
  787. cmsg->CallingPartyNumber + 3,
  788. cmsg->CallingPartyNumber[0] - 2);
  789. strncpy(cmd.parm.setup.eazmsn,
  790. cmsg->CalledPartyNumber + 2,
  791. cmsg->CalledPartyNumber[0] - 1);
  792. cmd.parm.setup.si1 = cip2si1(cmsg->CIPValue);
  793. cmd.parm.setup.si2 = cip2si2(cmsg->CIPValue);
  794. cmd.parm.setup.plan = cmsg->CallingPartyNumber[1];
  795. cmd.parm.setup.screen = cmsg->CallingPartyNumber[2];
  796. printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s\n",
  797. card->contrnr,
  798. cmd.parm.setup.phone,
  799. cmd.parm.setup.si1,
  800. cmd.parm.setup.si2,
  801. cmd.parm.setup.eazmsn);
  802. if (cmd.parm.setup.si1 == 1 && cmd.parm.setup.si2 != 0) {
  803. printk(KERN_INFO "capidrv-%d: patching si2=%d to 0 for VBOX\n",
  804. card->contrnr,
  805. cmd.parm.setup.si2);
  806. cmd.parm.setup.si2 = 0;
  807. }
  808. switch (card->interface.statcallb(&cmd)) {
  809. case 0:
  810. case 3:
  811. /* No device matching this call.
  812. * and isdn_common.c has send a HANGUP command
  813. * which is ignored in state ST_PLCI_INCOMING,
  814. * so we send RESP to ignore the call
  815. */
  816. capi_cmsg_answer(cmsg);
  817. cmsg->Reject = 1; /* ignore */
  818. send_message(card, cmsg);
  819. plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
  820. printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s ignored\n",
  821. card->contrnr,
  822. cmd.parm.setup.phone,
  823. cmd.parm.setup.si1,
  824. cmd.parm.setup.si2,
  825. cmd.parm.setup.eazmsn);
  826. break;
  827. case 1:
  828. /* At least one device matching this call (RING on ttyI)
  829. * HL-driver may send ALERTING on the D-channel in this
  830. * case.
  831. * really means: RING on ttyI or a net interface
  832. * accepted this call already.
  833. *
  834. * If the call was accepted, state has already changed,
  835. * and CONNECT_RESP already sent.
  836. */
  837. if (plcip->state == ST_PLCI_INCOMING) {
  838. printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s tty alerting\n",
  839. card->contrnr,
  840. cmd.parm.setup.phone,
  841. cmd.parm.setup.si1,
  842. cmd.parm.setup.si2,
  843. cmd.parm.setup.eazmsn);
  844. capi_fill_ALERT_REQ(cmsg,
  845. global.ap.applid,
  846. card->msgid++,
  847. plcip->plci, /* adr */
  848. NULL,/* BChannelinformation */
  849. NULL,/* Keypadfacility */
  850. NULL,/* Useruserdata */
  851. NULL /* Facilitydataarray */
  852. );
  853. plcip->msgid = cmsg->Messagenumber;
  854. send_message(card, cmsg);
  855. } else {
  856. printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s on netdev\n",
  857. card->contrnr,
  858. cmd.parm.setup.phone,
  859. cmd.parm.setup.si1,
  860. cmd.parm.setup.si2,
  861. cmd.parm.setup.eazmsn);
  862. }
  863. break;
  864. case 2: /* Call will be rejected. */
  865. capi_cmsg_answer(cmsg);
  866. cmsg->Reject = 2; /* reject call, normal call clearing */
  867. send_message(card, cmsg);
  868. plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
  869. break;
  870. default:
  871. /* An error happened. (Invalid parameters for example.) */
  872. capi_cmsg_answer(cmsg);
  873. cmsg->Reject = 8; /* reject call,
  874. destination out of order */
  875. send_message(card, cmsg);
  876. plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
  877. break;
  878. }
  879. return;
  880. }
  881. static void handle_plci(_cmsg * cmsg)
  882. {
  883. capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
  884. capidrv_plci *plcip;
  885. isdn_ctrl cmd;
  886. _cdebbuf *cdb;
  887. if (!card) {
  888. printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
  889. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  890. cmsg->adr.adrController & 0x7f);
  891. return;
  892. }
  893. switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
  894. case CAPI_DISCONNECT_IND: /* plci */
  895. if (cmsg->Reason) {
  896. printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
  897. card->contrnr,
  898. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  899. cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
  900. }
  901. if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
  902. capi_cmsg_answer(cmsg);
  903. send_message(card, cmsg);
  904. goto notfound;
  905. }
  906. card->bchans[plcip->chan].disconnecting = 1;
  907. plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND);
  908. capi_cmsg_answer(cmsg);
  909. send_message(card, cmsg);
  910. plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP);
  911. break;
  912. case CAPI_DISCONNECT_CONF: /* plci */
  913. if (cmsg->Info) {
  914. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
  915. card->contrnr,
  916. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  917. cmsg->Info, capi_info2str(cmsg->Info),
  918. cmsg->adr.adrPLCI);
  919. }
  920. if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
  921. goto notfound;
  922. card->bchans[plcip->chan].disconnecting = 1;
  923. break;
  924. case CAPI_ALERT_CONF: /* plci */
  925. if (cmsg->Info) {
  926. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
  927. card->contrnr,
  928. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  929. cmsg->Info, capi_info2str(cmsg->Info),
  930. cmsg->adr.adrPLCI);
  931. }
  932. break;
  933. case CAPI_CONNECT_IND: /* plci */
  934. handle_incoming_call(card, cmsg);
  935. break;
  936. case CAPI_CONNECT_CONF: /* plci */
  937. if (cmsg->Info) {
  938. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
  939. card->contrnr,
  940. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  941. cmsg->Info, capi_info2str(cmsg->Info),
  942. cmsg->adr.adrPLCI);
  943. }
  944. if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
  945. goto notfound;
  946. plcip->plci = cmsg->adr.adrPLCI;
  947. if (cmsg->Info) {
  948. plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
  949. } else {
  950. plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
  951. }
  952. break;
  953. case CAPI_CONNECT_ACTIVE_IND: /* plci */
  954. if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
  955. goto notfound;
  956. if (card->bchans[plcip->chan].incoming) {
  957. capi_cmsg_answer(cmsg);
  958. send_message(card, cmsg);
  959. plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
  960. } else {
  961. capidrv_ncci *nccip;
  962. capi_cmsg_answer(cmsg);
  963. send_message(card, cmsg);
  964. nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
  965. if (!nccip) {
  966. printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
  967. break; /* $$$$ */
  968. }
  969. capi_fill_CONNECT_B3_REQ(cmsg,
  970. global.ap.applid,
  971. card->msgid++,
  972. plcip->plci, /* adr */
  973. NULL /* NCPI */
  974. );
  975. nccip->msgid = cmsg->Messagenumber;
  976. send_message(card, cmsg);
  977. cmd.command = ISDN_STAT_DCONN;
  978. cmd.driver = card->myid;
  979. cmd.arg = plcip->chan;
  980. card->interface.statcallb(&cmd);
  981. plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
  982. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ);
  983. }
  984. break;
  985. case CAPI_INFO_IND: /* Controller/plci */
  986. if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
  987. goto notfound;
  988. if (cmsg->InfoNumber == 0x4000) {
  989. if (cmsg->InfoElement[0] == 4) {
  990. cmd.command = ISDN_STAT_CINF;
  991. cmd.driver = card->myid;
  992. cmd.arg = plcip->chan;
  993. sprintf(cmd.parm.num, "%lu",
  994. (unsigned long)
  995. ((u32) cmsg->InfoElement[1]
  996. | ((u32) (cmsg->InfoElement[2]) << 8)
  997. | ((u32) (cmsg->InfoElement[3]) << 16)
  998. | ((u32) (cmsg->InfoElement[4]) << 24)));
  999. card->interface.statcallb(&cmd);
  1000. break;
  1001. }
  1002. }
  1003. cdb = capi_cmsg2str(cmsg);
  1004. if (cdb) {
  1005. printk(KERN_WARNING "capidrv-%d: %s\n",
  1006. card->contrnr, cdb->buf);
  1007. cdebbuf_free(cdb);
  1008. } else
  1009. printk(KERN_WARNING "capidrv-%d: CAPI_INFO_IND InfoNumber %x not handled\n",
  1010. card->contrnr, cmsg->InfoNumber);
  1011. break;
  1012. case CAPI_CONNECT_ACTIVE_CONF: /* plci */
  1013. goto ignored;
  1014. case CAPI_SELECT_B_PROTOCOL_CONF: /* plci */
  1015. goto ignored;
  1016. case CAPI_FACILITY_IND: /* Controller/plci/ncci */
  1017. goto ignored;
  1018. case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
  1019. goto ignored;
  1020. case CAPI_INFO_CONF: /* Controller/plci */
  1021. goto ignored;
  1022. default:
  1023. printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
  1024. card->contrnr,
  1025. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1026. cmsg->adr.adrPLCI);
  1027. }
  1028. return;
  1029. ignored:
  1030. printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
  1031. card->contrnr,
  1032. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1033. cmsg->adr.adrPLCI);
  1034. return;
  1035. notfound:
  1036. printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
  1037. card->contrnr,
  1038. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1039. cmsg->adr.adrPLCI);
  1040. return;
  1041. }
  1042. static void handle_ncci(_cmsg * cmsg)
  1043. {
  1044. capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
  1045. capidrv_plci *plcip;
  1046. capidrv_ncci *nccip;
  1047. isdn_ctrl cmd;
  1048. int len;
  1049. if (!card) {
  1050. printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
  1051. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1052. cmsg->adr.adrController & 0x7f);
  1053. return;
  1054. }
  1055. switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
  1056. case CAPI_CONNECT_B3_ACTIVE_IND: /* ncci */
  1057. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1058. goto notfound;
  1059. capi_cmsg_answer(cmsg);
  1060. send_message(card, cmsg);
  1061. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
  1062. cmd.command = ISDN_STAT_BCONN;
  1063. cmd.driver = card->myid;
  1064. cmd.arg = nccip->chan;
  1065. card->interface.statcallb(&cmd);
  1066. printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
  1067. card->contrnr, nccip->chan, nccip->ncci);
  1068. break;
  1069. case CAPI_CONNECT_B3_ACTIVE_CONF: /* ncci */
  1070. goto ignored;
  1071. case CAPI_CONNECT_B3_IND: /* ncci */
  1072. plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
  1073. if (plcip) {
  1074. nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
  1075. if (nccip) {
  1076. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
  1077. capi_fill_CONNECT_B3_RESP(cmsg,
  1078. global.ap.applid,
  1079. card->msgid++,
  1080. nccip->ncci, /* adr */
  1081. 0, /* Reject */
  1082. NULL /* NCPI */
  1083. );
  1084. send_message(card, cmsg);
  1085. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
  1086. break;
  1087. }
  1088. printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
  1089. } else {
  1090. printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
  1091. card->contrnr,
  1092. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1093. cmsg->adr.adrNCCI);
  1094. }
  1095. capi_fill_CONNECT_B3_RESP(cmsg,
  1096. global.ap.applid,
  1097. card->msgid++,
  1098. cmsg->adr.adrNCCI,
  1099. 2, /* Reject */
  1100. NULL /* NCPI */
  1101. );
  1102. send_message(card, cmsg);
  1103. break;
  1104. case CAPI_CONNECT_B3_CONF: /* ncci */
  1105. if (!(nccip = find_ncci_by_msgid(card,
  1106. cmsg->adr.adrNCCI,
  1107. cmsg->Messagenumber)))
  1108. goto notfound;
  1109. nccip->ncci = cmsg->adr.adrNCCI;
  1110. if (cmsg->Info) {
  1111. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
  1112. card->contrnr,
  1113. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1114. cmsg->Info, capi_info2str(cmsg->Info),
  1115. cmsg->adr.adrNCCI);
  1116. }
  1117. if (cmsg->Info)
  1118. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
  1119. else
  1120. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
  1121. break;
  1122. case CAPI_CONNECT_B3_T90_ACTIVE_IND: /* ncci */
  1123. capi_cmsg_answer(cmsg);
  1124. send_message(card, cmsg);
  1125. break;
  1126. case CAPI_DATA_B3_IND: /* ncci */
  1127. /* handled in handle_data() */
  1128. goto ignored;
  1129. case CAPI_DATA_B3_CONF: /* ncci */
  1130. if (cmsg->Info) {
  1131. printk(KERN_WARNING "CAPI_DATA_B3_CONF: Info %x - %s\n",
  1132. cmsg->Info, capi_info2str(cmsg->Info));
  1133. }
  1134. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1135. goto notfound;
  1136. len = capidrv_del_ack(nccip, cmsg->DataHandle);
  1137. if (len < 0)
  1138. break;
  1139. cmd.command = ISDN_STAT_BSENT;
  1140. cmd.driver = card->myid;
  1141. cmd.arg = nccip->chan;
  1142. cmd.parm.length = len;
  1143. card->interface.statcallb(&cmd);
  1144. break;
  1145. case CAPI_DISCONNECT_B3_IND: /* ncci */
  1146. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1147. goto notfound;
  1148. card->bchans[nccip->chan].disconnecting = 1;
  1149. ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND);
  1150. capi_cmsg_answer(cmsg);
  1151. send_message(card, cmsg);
  1152. ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP);
  1153. break;
  1154. case CAPI_DISCONNECT_B3_CONF: /* ncci */
  1155. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1156. goto notfound;
  1157. if (cmsg->Info) {
  1158. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
  1159. card->contrnr,
  1160. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1161. cmsg->Info, capi_info2str(cmsg->Info),
  1162. cmsg->adr.adrNCCI);
  1163. ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
  1164. }
  1165. break;
  1166. case CAPI_RESET_B3_IND: /* ncci */
  1167. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1168. goto notfound;
  1169. ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
  1170. capi_cmsg_answer(cmsg);
  1171. send_message(card, cmsg);
  1172. break;
  1173. case CAPI_RESET_B3_CONF: /* ncci */
  1174. goto ignored; /* $$$$ */
  1175. case CAPI_FACILITY_IND: /* Controller/plci/ncci */
  1176. goto ignored;
  1177. case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
  1178. goto ignored;
  1179. default:
  1180. printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
  1181. card->contrnr,
  1182. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1183. cmsg->adr.adrNCCI);
  1184. }
  1185. return;
  1186. ignored:
  1187. printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
  1188. card->contrnr,
  1189. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1190. cmsg->adr.adrNCCI);
  1191. return;
  1192. notfound:
  1193. printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
  1194. card->contrnr,
  1195. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1196. cmsg->adr.adrNCCI);
  1197. }
  1198. static void handle_data(_cmsg * cmsg, struct sk_buff *skb)
  1199. {
  1200. capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
  1201. capidrv_ncci *nccip;
  1202. if (!card) {
  1203. printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
  1204. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1205. cmsg->adr.adrController & 0x7f);
  1206. kfree_skb(skb);
  1207. return;
  1208. }
  1209. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
  1210. printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
  1211. card->contrnr,
  1212. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1213. cmsg->adr.adrNCCI);
  1214. kfree_skb(skb);
  1215. return;
  1216. }
  1217. (void) skb_pull(skb, CAPIMSG_LEN(skb->data));
  1218. card->interface.rcvcallb_skb(card->myid, nccip->chan, skb);
  1219. capi_cmsg_answer(cmsg);
  1220. send_message(card, cmsg);
  1221. }
  1222. static _cmsg s_cmsg;
  1223. static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
  1224. {
  1225. capi_message2cmsg(&s_cmsg, skb->data);
  1226. if (debugmode > 3) {
  1227. _cdebbuf *cdb = capi_cmsg2str(&s_cmsg);
  1228. if (cdb) {
  1229. printk(KERN_DEBUG "%s: applid=%d %s\n", __func__,
  1230. ap->applid, cdb->buf);
  1231. cdebbuf_free(cdb);
  1232. } else
  1233. printk(KERN_DEBUG "%s: applid=%d %s not traced\n",
  1234. __func__, ap->applid,
  1235. capi_cmd2str(s_cmsg.Command, s_cmsg.Subcommand));
  1236. }
  1237. if (s_cmsg.Command == CAPI_DATA_B3
  1238. && s_cmsg.Subcommand == CAPI_IND) {
  1239. handle_data(&s_cmsg, skb);
  1240. return;
  1241. }
  1242. if ((s_cmsg.adr.adrController & 0xffffff00) == 0)
  1243. handle_controller(&s_cmsg);
  1244. else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0)
  1245. handle_plci(&s_cmsg);
  1246. else
  1247. handle_ncci(&s_cmsg);
  1248. /*
  1249. * data of skb used in s_cmsg,
  1250. * free data when s_cmsg is not used again
  1251. * thanks to Lars Heete <hel@admin.de>
  1252. */
  1253. kfree_skb(skb);
  1254. }
  1255. /* ------------------------------------------------------------------- */
  1256. #define PUTBYTE_TO_STATUS(card, byte) \
  1257. do { \
  1258. *(card)->q931_write++ = (byte); \
  1259. if ((card)->q931_write > (card)->q931_end) \
  1260. (card)->q931_write = (card)->q931_buf; \
  1261. } while (0)
  1262. static void handle_dtrace_data(capidrv_contr *card,
  1263. int send, int level2, u8 *data, u16 len)
  1264. {
  1265. u8 *p, *end;
  1266. isdn_ctrl cmd;
  1267. if (!len) {
  1268. printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
  1269. card->contrnr, len);
  1270. return;
  1271. }
  1272. if (level2) {
  1273. PUTBYTE_TO_STATUS(card, 'D');
  1274. PUTBYTE_TO_STATUS(card, '2');
  1275. PUTBYTE_TO_STATUS(card, send ? '>' : '<');
  1276. PUTBYTE_TO_STATUS(card, ':');
  1277. } else {
  1278. PUTBYTE_TO_STATUS(card, 'D');
  1279. PUTBYTE_TO_STATUS(card, '3');
  1280. PUTBYTE_TO_STATUS(card, send ? '>' : '<');
  1281. PUTBYTE_TO_STATUS(card, ':');
  1282. }
  1283. for (p = data, end = data+len; p < end; p++) {
  1284. u8 w;
  1285. PUTBYTE_TO_STATUS(card, ' ');
  1286. w = (*p >> 4) & 0xf;
  1287. PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
  1288. w = *p & 0xf;
  1289. PUTBYTE_TO_STATUS(card, (w < 10) ? '0'+w : 'A'-10+w);
  1290. }
  1291. PUTBYTE_TO_STATUS(card, '\n');
  1292. cmd.command = ISDN_STAT_STAVAIL;
  1293. cmd.driver = card->myid;
  1294. cmd.arg = len*3+5;
  1295. card->interface.statcallb(&cmd);
  1296. }
  1297. /* ------------------------------------------------------------------- */
  1298. static _cmsg cmdcmsg;
  1299. static int capidrv_ioctl(isdn_ctrl * c, capidrv_contr * card)
  1300. {
  1301. switch (c->arg) {
  1302. case 1:
  1303. debugmode = (int)(*((unsigned int *)c->parm.num));
  1304. printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
  1305. card->contrnr, debugmode);
  1306. return 0;
  1307. default:
  1308. printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
  1309. card->contrnr, c->arg);
  1310. return -EINVAL;
  1311. }
  1312. return -EINVAL;
  1313. }
  1314. /*
  1315. * Handle leased lines (CAPI-Bundling)
  1316. */
  1317. struct internal_bchannelinfo {
  1318. unsigned short channelalloc;
  1319. unsigned short operation;
  1320. unsigned char cmask[31];
  1321. };
  1322. static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
  1323. {
  1324. unsigned long bmask = 0;
  1325. int active = !0;
  1326. char *s;
  1327. int i;
  1328. if (strncmp(teln, "FV:", 3) != 0)
  1329. return 1;
  1330. s = teln + 3;
  1331. while (*s && *s == ' ') s++;
  1332. if (!*s) return -2;
  1333. if (*s == 'p' || *s == 'P') {
  1334. active = 0;
  1335. s++;
  1336. }
  1337. if (*s == 'a' || *s == 'A') {
  1338. active = !0;
  1339. s++;
  1340. }
  1341. while (*s) {
  1342. int digit1 = 0;
  1343. int digit2 = 0;
  1344. if (!isdigit(*s)) return -3;
  1345. while (isdigit(*s)) { digit1 = digit1*10 + (*s - '0'); s++; }
  1346. if (digit1 <= 0 && digit1 > 30) return -4;
  1347. if (*s == 0 || *s == ',' || *s == ' ') {
  1348. bmask |= (1 << digit1);
  1349. digit1 = 0;
  1350. if (*s) s++;
  1351. continue;
  1352. }
  1353. if (*s != '-') return -5;
  1354. s++;
  1355. if (!isdigit(*s)) return -3;
  1356. while (isdigit(*s)) { digit2 = digit2*10 + (*s - '0'); s++; }
  1357. if (digit2 <= 0 && digit2 > 30) return -4;
  1358. if (*s == 0 || *s == ',' || *s == ' ') {
  1359. if (digit1 > digit2)
  1360. for (i = digit2; i <= digit1 ; i++)
  1361. bmask |= (1 << i);
  1362. else
  1363. for (i = digit1; i <= digit2 ; i++)
  1364. bmask |= (1 << i);
  1365. digit1 = digit2 = 0;
  1366. if (*s) s++;
  1367. continue;
  1368. }
  1369. return -6;
  1370. }
  1371. if (activep) *activep = active;
  1372. if (bmaskp) *bmaskp = bmask;
  1373. return 0;
  1374. }
  1375. static int FVteln2capi20(char *teln, u8 AdditionalInfo[1+2+2+31])
  1376. {
  1377. unsigned long bmask;
  1378. int active;
  1379. int rc, i;
  1380. rc = decodeFVteln(teln, &bmask, &active);
  1381. if (rc) return rc;
  1382. /* Length */
  1383. AdditionalInfo[0] = 2+2+31;
  1384. /* Channel: 3 => use channel allocation */
  1385. AdditionalInfo[1] = 3; AdditionalInfo[2] = 0;
  1386. /* Operation: 0 => DTE mode, 1 => DCE mode */
  1387. if (active) {
  1388. AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
  1389. } else {
  1390. AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
  1391. }
  1392. /* Channel mask array */
  1393. AdditionalInfo[5] = 0; /* no D-Channel */
  1394. for (i=1; i <= 30; i++)
  1395. AdditionalInfo[5+i] = (bmask & (1 << i)) ? 0xff : 0;
  1396. return 0;
  1397. }
  1398. static int capidrv_command(isdn_ctrl * c, capidrv_contr * card)
  1399. {
  1400. isdn_ctrl cmd;
  1401. struct capidrv_bchan *bchan;
  1402. struct capidrv_plci *plcip;
  1403. u8 AdditionalInfo[1+2+2+31];
  1404. int rc, isleasedline = 0;
  1405. if (c->command == ISDN_CMD_IOCTL)
  1406. return capidrv_ioctl(c, card);
  1407. switch (c->command) {
  1408. case ISDN_CMD_DIAL:{
  1409. u8 calling[ISDN_MSNLEN + 3];
  1410. u8 called[ISDN_MSNLEN + 2];
  1411. if (debugmode)
  1412. printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
  1413. card->contrnr,
  1414. c->arg,
  1415. c->parm.setup.phone,
  1416. c->parm.setup.si1,
  1417. c->parm.setup.si2,
  1418. c->parm.setup.eazmsn);
  1419. bchan = &card->bchans[c->arg % card->nbchan];
  1420. if (bchan->plcip) {
  1421. printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
  1422. card->contrnr,
  1423. c->arg,
  1424. c->parm.setup.phone,
  1425. c->parm.setup.si1,
  1426. c->parm.setup.si2,
  1427. c->parm.setup.eazmsn,
  1428. bchan->plcip->plci);
  1429. return 0;
  1430. }
  1431. bchan->si1 = c->parm.setup.si1;
  1432. bchan->si2 = c->parm.setup.si2;
  1433. strncpy(bchan->num, c->parm.setup.phone, sizeof(bchan->num));
  1434. strncpy(bchan->mynum, c->parm.setup.eazmsn, sizeof(bchan->mynum));
  1435. rc = FVteln2capi20(bchan->num, AdditionalInfo);
  1436. isleasedline = (rc == 0);
  1437. if (rc < 0)
  1438. printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
  1439. if (isleasedline) {
  1440. calling[0] = 0;
  1441. called[0] = 0;
  1442. if (debugmode)
  1443. printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
  1444. } else {
  1445. calling[0] = strlen(bchan->mynum) + 2;
  1446. calling[1] = 0;
  1447. calling[2] = 0x80;
  1448. strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
  1449. called[0] = strlen(bchan->num) + 1;
  1450. called[1] = 0x80;
  1451. strncpy(called + 2, bchan->num, ISDN_MSNLEN);
  1452. }
  1453. capi_fill_CONNECT_REQ(&cmdcmsg,
  1454. global.ap.applid,
  1455. card->msgid++,
  1456. card->contrnr, /* adr */
  1457. si2cip(bchan->si1, bchan->si2), /* cipvalue */
  1458. called, /* CalledPartyNumber */
  1459. calling, /* CallingPartyNumber */
  1460. NULL, /* CalledPartySubaddress */
  1461. NULL, /* CallingPartySubaddress */
  1462. b1prot(bchan->l2, bchan->l3), /* B1protocol */
  1463. b2prot(bchan->l2, bchan->l3), /* B2protocol */
  1464. b3prot(bchan->l2, bchan->l3), /* B3protocol */
  1465. b1config(bchan->l2, bchan->l3), /* B1configuration */
  1466. NULL, /* B2configuration */
  1467. NULL, /* B3configuration */
  1468. NULL, /* BC */
  1469. NULL, /* LLC */
  1470. NULL, /* HLC */
  1471. /* BChannelinformation */
  1472. isleasedline ? AdditionalInfo : NULL,
  1473. NULL, /* Keypadfacility */
  1474. NULL, /* Useruserdata */
  1475. NULL /* Facilitydataarray */
  1476. );
  1477. if ((plcip = new_plci(card, (c->arg % card->nbchan))) == NULL) {
  1478. cmd.command = ISDN_STAT_DHUP;
  1479. cmd.driver = card->myid;
  1480. cmd.arg = (c->arg % card->nbchan);
  1481. card->interface.statcallb(&cmd);
  1482. return -1;
  1483. }
  1484. plcip->msgid = cmdcmsg.Messagenumber;
  1485. plcip->leasedline = isleasedline;
  1486. plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
  1487. send_message(card, &cmdcmsg);
  1488. return 0;
  1489. }
  1490. case ISDN_CMD_ACCEPTD:
  1491. bchan = &card->bchans[c->arg % card->nbchan];
  1492. if (debugmode)
  1493. printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
  1494. card->contrnr,
  1495. c->arg, bchan->l2, bchan->l3);
  1496. capi_fill_CONNECT_RESP(&cmdcmsg,
  1497. global.ap.applid,
  1498. card->msgid++,
  1499. bchan->plcip->plci, /* adr */
  1500. 0, /* Reject */
  1501. b1prot(bchan->l2, bchan->l3), /* B1protocol */
  1502. b2prot(bchan->l2, bchan->l3), /* B2protocol */
  1503. b3prot(bchan->l2, bchan->l3), /* B3protocol */
  1504. b1config(bchan->l2, bchan->l3), /* B1configuration */
  1505. NULL, /* B2configuration */
  1506. NULL, /* B3configuration */
  1507. NULL, /* ConnectedNumber */
  1508. NULL, /* ConnectedSubaddress */
  1509. NULL, /* LLC */
  1510. NULL, /* BChannelinformation */
  1511. NULL, /* Keypadfacility */
  1512. NULL, /* Useruserdata */
  1513. NULL /* Facilitydataarray */
  1514. );
  1515. capi_cmsg2message(&cmdcmsg, cmdcmsg.buf);
  1516. plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
  1517. send_message(card, &cmdcmsg);
  1518. return 0;
  1519. case ISDN_CMD_ACCEPTB:
  1520. if (debugmode)
  1521. printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
  1522. card->contrnr,
  1523. c->arg);
  1524. return -ENOSYS;
  1525. case ISDN_CMD_HANGUP:
  1526. if (debugmode)
  1527. printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
  1528. card->contrnr,
  1529. c->arg);
  1530. bchan = &card->bchans[c->arg % card->nbchan];
  1531. if (bchan->disconnecting) {
  1532. if (debugmode)
  1533. printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
  1534. card->contrnr,
  1535. c->arg);
  1536. return 0;
  1537. }
  1538. if (bchan->nccip) {
  1539. bchan->disconnecting = 1;
  1540. capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
  1541. global.ap.applid,
  1542. card->msgid++,
  1543. bchan->nccip->ncci,
  1544. NULL /* NCPI */
  1545. );
  1546. ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
  1547. send_message(card, &cmdcmsg);
  1548. return 0;
  1549. } else if (bchan->plcip) {
  1550. if (bchan->plcip->state == ST_PLCI_INCOMING) {
  1551. /*
  1552. * just ignore, we a called from
  1553. * isdn_status_callback(),
  1554. * which will return 0 or 2, this is handled
  1555. * by the CONNECT_IND handler
  1556. */
  1557. bchan->disconnecting = 1;
  1558. return 0;
  1559. } else if (bchan->plcip->plci) {
  1560. bchan->disconnecting = 1;
  1561. capi_fill_DISCONNECT_REQ(&cmdcmsg,
  1562. global.ap.applid,
  1563. card->msgid++,
  1564. bchan->plcip->plci,
  1565. NULL, /* BChannelinformation */
  1566. NULL, /* Keypadfacility */
  1567. NULL, /* Useruserdata */
  1568. NULL /* Facilitydataarray */
  1569. );
  1570. plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
  1571. send_message(card, &cmdcmsg);
  1572. return 0;
  1573. } else {
  1574. printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
  1575. card->contrnr,
  1576. c->arg);
  1577. return -EINVAL;
  1578. }
  1579. }
  1580. printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
  1581. card->contrnr,
  1582. c->arg);
  1583. return -EINVAL;
  1584. /* ready */
  1585. case ISDN_CMD_SETL2:
  1586. if (debugmode)
  1587. printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
  1588. card->contrnr,
  1589. (c->arg & 0xff), (c->arg >> 8));
  1590. bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
  1591. bchan->l2 = (c->arg >> 8);
  1592. return 0;
  1593. case ISDN_CMD_SETL3:
  1594. if (debugmode)
  1595. printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
  1596. card->contrnr,
  1597. (c->arg & 0xff), (c->arg >> 8));
  1598. bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
  1599. bchan->l3 = (c->arg >> 8);
  1600. return 0;
  1601. case ISDN_CMD_SETEAZ:
  1602. if (debugmode)
  1603. printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
  1604. card->contrnr,
  1605. c->parm.num, c->arg);
  1606. bchan = &card->bchans[c->arg % card->nbchan];
  1607. strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
  1608. return 0;
  1609. case ISDN_CMD_CLREAZ:
  1610. if (debugmode)
  1611. printk(KERN_DEBUG "capidrv-%d: clearing EAZ on chan %ld\n",
  1612. card->contrnr, c->arg);
  1613. bchan = &card->bchans[c->arg % card->nbchan];
  1614. bchan->msn[0] = 0;
  1615. return 0;
  1616. default:
  1617. printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
  1618. card->contrnr, c->command);
  1619. return -EINVAL;
  1620. }
  1621. return 0;
  1622. }
  1623. static int if_command(isdn_ctrl * c)
  1624. {
  1625. capidrv_contr *card = findcontrbydriverid(c->driver);
  1626. if (card)
  1627. return capidrv_command(c, card);
  1628. printk(KERN_ERR
  1629. "capidrv: if_command %d called with invalid driverId %d!\n",
  1630. c->command, c->driver);
  1631. return -ENODEV;
  1632. }
  1633. static _cmsg sendcmsg;
  1634. static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
  1635. {
  1636. capidrv_contr *card = findcontrbydriverid(id);
  1637. capidrv_bchan *bchan;
  1638. capidrv_ncci *nccip;
  1639. int len = skb->len;
  1640. int msglen;
  1641. u16 errcode;
  1642. u16 datahandle;
  1643. u32 data;
  1644. if (!card) {
  1645. printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
  1646. id);
  1647. return 0;
  1648. }
  1649. if (debugmode > 4)
  1650. printk(KERN_DEBUG "capidrv-%d: sendbuf len=%d skb=%p doack=%d\n",
  1651. card->contrnr, len, skb, doack);
  1652. bchan = &card->bchans[channel % card->nbchan];
  1653. nccip = bchan->nccip;
  1654. if (!nccip || nccip->state != ST_NCCI_ACTIVE) {
  1655. printk(KERN_ERR "capidrv-%d: if_sendbuf: %s:%d: chan not up!\n",
  1656. card->contrnr, card->name, channel);
  1657. return 0;
  1658. }
  1659. datahandle = nccip->datahandle;
  1660. /*
  1661. * Here we copy pointer skb->data into the 32-bit 'Data' field.
  1662. * The 'Data' field is not used in practice in linux kernel
  1663. * (neither in 32 or 64 bit), but should have some value,
  1664. * since a CAPI message trace will display it.
  1665. *
  1666. * The correct value in the 32 bit case is the address of the
  1667. * data, in 64 bit it makes no sense, we use 0 there.
  1668. */
  1669. #ifdef CONFIG_64BIT
  1670. data = 0;
  1671. #else
  1672. data = (unsigned long) skb->data;
  1673. #endif
  1674. capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
  1675. nccip->ncci, /* adr */
  1676. data, /* Data */
  1677. skb->len, /* DataLength */
  1678. datahandle, /* DataHandle */
  1679. 0 /* Flags */
  1680. );
  1681. if (capidrv_add_ack(nccip, datahandle, doack ? (int)skb->len : -1) < 0)
  1682. return 0;
  1683. capi_cmsg2message(&sendcmsg, sendcmsg.buf);
  1684. msglen = CAPIMSG_LEN(sendcmsg.buf);
  1685. if (skb_headroom(skb) < msglen) {
  1686. struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
  1687. if (!nskb) {
  1688. printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
  1689. card->contrnr);
  1690. (void)capidrv_del_ack(nccip, datahandle);
  1691. return 0;
  1692. }
  1693. printk(KERN_DEBUG "capidrv-%d: only %d bytes headroom, need %d\n",
  1694. card->contrnr, skb_headroom(skb), msglen);
  1695. memcpy(skb_push(nskb, msglen), sendcmsg.buf, msglen);
  1696. errcode = capi20_put_message(&global.ap, nskb);
  1697. if (errcode == CAPI_NOERROR) {
  1698. dev_kfree_skb(skb);
  1699. nccip->datahandle++;
  1700. return len;
  1701. }
  1702. if (debugmode > 3)
  1703. printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
  1704. card->contrnr, errcode, capi_info2str(errcode));
  1705. (void)capidrv_del_ack(nccip, datahandle);
  1706. dev_kfree_skb(nskb);
  1707. return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
  1708. } else {
  1709. memcpy(skb_push(skb, msglen), sendcmsg.buf, msglen);
  1710. errcode = capi20_put_message(&global.ap, skb);
  1711. if (errcode == CAPI_NOERROR) {
  1712. nccip->datahandle++;
  1713. return len;
  1714. }
  1715. if (debugmode > 3)
  1716. printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
  1717. card->contrnr, errcode, capi_info2str(errcode));
  1718. skb_pull(skb, msglen);
  1719. (void)capidrv_del_ack(nccip, datahandle);
  1720. return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
  1721. }
  1722. }
  1723. static int if_readstat(u8 __user *buf, int len, int id, int channel)
  1724. {
  1725. capidrv_contr *card = findcontrbydriverid(id);
  1726. int count;
  1727. u8 __user *p;
  1728. if (!card) {
  1729. printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
  1730. id);
  1731. return -ENODEV;
  1732. }
  1733. for (p=buf, count=0; count < len; p++, count++) {
  1734. if (put_user(*card->q931_read++, p))
  1735. return -EFAULT;
  1736. if (card->q931_read > card->q931_end)
  1737. card->q931_read = card->q931_buf;
  1738. }
  1739. return count;
  1740. }
  1741. static void enable_dchannel_trace(capidrv_contr *card)
  1742. {
  1743. u8 manufacturer[CAPI_MANUFACTURER_LEN];
  1744. capi_version version;
  1745. u16 contr = card->contrnr;
  1746. u16 errcode;
  1747. u16 avmversion[3];
  1748. errcode = capi20_get_manufacturer(contr, manufacturer);
  1749. if (errcode != CAPI_NOERROR) {
  1750. printk(KERN_ERR "%s: can't get manufacturer (0x%x)\n",
  1751. card->name, errcode);
  1752. return;
  1753. }
  1754. if (strstr(manufacturer, "AVM") == NULL) {
  1755. printk(KERN_ERR "%s: not from AVM, no d-channel trace possible (%s)\n",
  1756. card->name, manufacturer);
  1757. return;
  1758. }
  1759. errcode = capi20_get_version(contr, &version);
  1760. if (errcode != CAPI_NOERROR) {
  1761. printk(KERN_ERR "%s: can't get version (0x%x)\n",
  1762. card->name, errcode);
  1763. return;
  1764. }
  1765. avmversion[0] = (version.majormanuversion >> 4) & 0x0f;
  1766. avmversion[1] = (version.majormanuversion << 4) & 0xf0;
  1767. avmversion[1] |= (version.minormanuversion >> 4) & 0x0f;
  1768. avmversion[2] |= version.minormanuversion & 0x0f;
  1769. if (avmversion[0] > 3 || (avmversion[0] == 3 && avmversion[1] > 5)) {
  1770. printk(KERN_INFO "%s: D2 trace enabled\n", card->name);
  1771. capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
  1772. card->msgid++,
  1773. contr,
  1774. 0x214D5641, /* ManuID */
  1775. 0, /* Class */
  1776. 1, /* Function */
  1777. (_cstruct)"\004\200\014\000\000");
  1778. } else {
  1779. printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
  1780. capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
  1781. card->msgid++,
  1782. contr,
  1783. 0x214D5641, /* ManuID */
  1784. 0, /* Class */
  1785. 1, /* Function */
  1786. (_cstruct)"\004\002\003\000\000");
  1787. }
  1788. send_message(card, &cmdcmsg);
  1789. }
  1790. static void send_listen(capidrv_contr *card)
  1791. {
  1792. capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
  1793. card->msgid++,
  1794. card->contrnr, /* controller */
  1795. 1 << 6, /* Infomask */
  1796. card->cipmask,
  1797. card->cipmask2,
  1798. NULL, NULL);
  1799. send_message(card, &cmdcmsg);
  1800. listen_change_state(card, EV_LISTEN_REQ);
  1801. }
  1802. static void listentimerfunc(unsigned long x)
  1803. {
  1804. capidrv_contr *card = (capidrv_contr *)x;
  1805. if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE)
  1806. printk(KERN_ERR "%s: controller dead ??\n", card->name);
  1807. send_listen(card);
  1808. mod_timer(&card->listentimer, jiffies + 60*HZ);
  1809. }
  1810. static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
  1811. {
  1812. capidrv_contr *card;
  1813. unsigned long flags;
  1814. isdn_ctrl cmd;
  1815. char id[20];
  1816. int i;
  1817. sprintf(id, "capidrv-%d", contr);
  1818. if (!try_module_get(THIS_MODULE)) {
  1819. printk(KERN_WARNING "capidrv: (%s) Could not reserve module\n", id);
  1820. return -1;
  1821. }
  1822. if (!(card = kzalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
  1823. printk(KERN_WARNING
  1824. "capidrv: (%s) Could not allocate contr-struct.\n", id);
  1825. return -1;
  1826. }
  1827. card->owner = THIS_MODULE;
  1828. init_timer(&card->listentimer);
  1829. strcpy(card->name, id);
  1830. card->contrnr = contr;
  1831. card->nbchan = profp->nbchannel;
  1832. card->bchans = kmalloc(sizeof(capidrv_bchan) * card->nbchan, GFP_ATOMIC);
  1833. if (!card->bchans) {
  1834. printk(KERN_WARNING
  1835. "capidrv: (%s) Could not allocate bchan-structs.\n", id);
  1836. module_put(card->owner);
  1837. kfree(card);
  1838. return -1;
  1839. }
  1840. card->interface.channels = profp->nbchannel;
  1841. card->interface.maxbufsize = 2048;
  1842. card->interface.command = if_command;
  1843. card->interface.writebuf_skb = if_sendbuf;
  1844. card->interface.writecmd = NULL;
  1845. card->interface.readstat = if_readstat;
  1846. card->interface.features = ISDN_FEATURE_L2_HDLC |
  1847. ISDN_FEATURE_L2_TRANS |
  1848. ISDN_FEATURE_L3_TRANS |
  1849. ISDN_FEATURE_P_UNKNOWN |
  1850. ISDN_FEATURE_L2_X75I |
  1851. ISDN_FEATURE_L2_X75UI |
  1852. ISDN_FEATURE_L2_X75BUI;
  1853. if (profp->support1 & (1<<2))
  1854. card->interface.features |= ISDN_FEATURE_L2_V11096 |
  1855. ISDN_FEATURE_L2_V11019 |
  1856. ISDN_FEATURE_L2_V11038;
  1857. if (profp->support1 & (1<<8))
  1858. card->interface.features |= ISDN_FEATURE_L2_MODEM;
  1859. card->interface.hl_hdrlen = 22; /* len of DATA_B3_REQ */
  1860. strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
  1861. card->q931_read = card->q931_buf;
  1862. card->q931_write = card->q931_buf;
  1863. card->q931_end = card->q931_buf + sizeof(card->q931_buf) - 1;
  1864. if (!register_isdn(&card->interface)) {
  1865. printk(KERN_ERR "capidrv: Unable to register contr %s\n", id);
  1866. kfree(card->bchans);
  1867. module_put(card->owner);
  1868. kfree(card);
  1869. return -1;
  1870. }
  1871. card->myid = card->interface.channels;
  1872. memset(card->bchans, 0, sizeof(capidrv_bchan) * card->nbchan);
  1873. for (i = 0; i < card->nbchan; i++) {
  1874. card->bchans[i].contr = card;
  1875. }
  1876. spin_lock_irqsave(&global_lock, flags);
  1877. card->next = global.contr_list;
  1878. global.contr_list = card;
  1879. global.ncontr++;
  1880. spin_unlock_irqrestore(&global_lock, flags);
  1881. cmd.command = ISDN_STAT_RUN;
  1882. cmd.driver = card->myid;
  1883. card->interface.statcallb(&cmd);
  1884. card->cipmask = 0x1FFF03FF; /* any */
  1885. card->cipmask2 = 0;
  1886. card->listentimer.data = (unsigned long)card;
  1887. card->listentimer.function = listentimerfunc;
  1888. send_listen(card);
  1889. mod_timer(&card->listentimer, jiffies + 60*HZ);
  1890. printk(KERN_INFO "%s: now up (%d B channels)\n",
  1891. card->name, card->nbchan);
  1892. enable_dchannel_trace(card);
  1893. return 0;
  1894. }
  1895. static int capidrv_delcontr(u16 contr)
  1896. {
  1897. capidrv_contr **pp, *card;
  1898. unsigned long flags;
  1899. isdn_ctrl cmd;
  1900. spin_lock_irqsave(&global_lock, flags);
  1901. for (card = global.contr_list; card; card = card->next) {
  1902. if (card->contrnr == contr)
  1903. break;
  1904. }
  1905. if (!card) {
  1906. spin_unlock_irqrestore(&global_lock, flags);
  1907. printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
  1908. return -1;
  1909. }
  1910. /* FIXME: maybe a race condition the card should be removed
  1911. * here from global list /kkeil
  1912. */
  1913. spin_unlock_irqrestore(&global_lock, flags);
  1914. del_timer(&card->listentimer);
  1915. if (debugmode)
  1916. printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
  1917. card->contrnr, card->myid);
  1918. cmd.command = ISDN_STAT_STOP;
  1919. cmd.driver = card->myid;
  1920. card->interface.statcallb(&cmd);
  1921. while (card->nbchan) {
  1922. cmd.command = ISDN_STAT_DISCH;
  1923. cmd.driver = card->myid;
  1924. cmd.arg = card->nbchan-1;
  1925. cmd.parm.num[0] = 0;
  1926. if (debugmode)
  1927. printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
  1928. card->contrnr, card->myid, cmd.arg);
  1929. card->interface.statcallb(&cmd);
  1930. if (card->bchans[card->nbchan-1].nccip)
  1931. free_ncci(card, card->bchans[card->nbchan-1].nccip);
  1932. if (card->bchans[card->nbchan-1].plcip)
  1933. free_plci(card, card->bchans[card->nbchan-1].plcip);
  1934. if (card->plci_list)
  1935. printk(KERN_ERR "capidrv: bug in free_plci()\n");
  1936. card->nbchan--;
  1937. }
  1938. kfree(card->bchans);
  1939. card->bchans = NULL;
  1940. if (debugmode)
  1941. printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
  1942. card->contrnr, card->myid);
  1943. cmd.command = ISDN_STAT_UNLOAD;
  1944. cmd.driver = card->myid;
  1945. card->interface.statcallb(&cmd);
  1946. if (debugmode)
  1947. printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
  1948. card->contrnr, card->myid);
  1949. spin_lock_irqsave(&global_lock, flags);
  1950. for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
  1951. if (*pp == card) {
  1952. *pp = (*pp)->next;
  1953. card->next = NULL;
  1954. global.ncontr--;
  1955. break;
  1956. }
  1957. }
  1958. spin_unlock_irqrestore(&global_lock, flags);
  1959. module_put(card->owner);
  1960. printk(KERN_INFO "%s: now down.\n", card->name);
  1961. kfree(card);
  1962. return 0;
  1963. }
  1964. static void lower_callback(unsigned int cmd, u32 contr, void *data)
  1965. {
  1966. switch (cmd) {
  1967. case KCI_CONTRUP:
  1968. printk(KERN_INFO "capidrv: controller %hu up\n", contr);
  1969. (void) capidrv_addcontr(contr, (capi_profile *) data);
  1970. break;
  1971. case KCI_CONTRDOWN:
  1972. printk(KERN_INFO "capidrv: controller %hu down\n", contr);
  1973. (void) capidrv_delcontr(contr);
  1974. break;
  1975. }
  1976. }
  1977. /*
  1978. * /proc/capi/capidrv:
  1979. * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
  1980. */
  1981. static int proc_capidrv_read_proc(char *page, char **start, off_t off,
  1982. int count, int *eof, void *data)
  1983. {
  1984. int len = 0;
  1985. len += sprintf(page+len, "%lu %lu %lu %lu\n",
  1986. global.ap.nrecvctlpkt,
  1987. global.ap.nrecvdatapkt,
  1988. global.ap.nsentctlpkt,
  1989. global.ap.nsentdatapkt);
  1990. if (off+count >= len)
  1991. *eof = 1;
  1992. if (len < off)
  1993. return 0;
  1994. *start = page + off;
  1995. return ((count < len-off) ? count : len-off);
  1996. }
  1997. static struct procfsentries {
  1998. char *name;
  1999. mode_t mode;
  2000. int (*read_proc)(char *page, char **start, off_t off,
  2001. int count, int *eof, void *data);
  2002. struct proc_dir_entry *procent;
  2003. } procfsentries[] = {
  2004. /* { "capi", S_IFDIR, 0 }, */
  2005. { "capi/capidrv", 0 , proc_capidrv_read_proc },
  2006. };
  2007. static void __init proc_init(void)
  2008. {
  2009. int nelem = ARRAY_SIZE(procfsentries);
  2010. int i;
  2011. for (i=0; i < nelem; i++) {
  2012. struct procfsentries *p = procfsentries + i;
  2013. p->procent = create_proc_entry(p->name, p->mode, NULL);
  2014. if (p->procent) p->procent->read_proc = p->read_proc;
  2015. }
  2016. }
  2017. static void __exit proc_exit(void)
  2018. {
  2019. int nelem = ARRAY_SIZE(procfsentries);
  2020. int i;
  2021. for (i=nelem-1; i >= 0; i--) {
  2022. struct procfsentries *p = procfsentries + i;
  2023. if (p->procent) {
  2024. remove_proc_entry(p->name, NULL);
  2025. p->procent = NULL;
  2026. }
  2027. }
  2028. }
  2029. static int __init capidrv_init(void)
  2030. {
  2031. capi_profile profile;
  2032. char rev[32];
  2033. char *p;
  2034. u32 ncontr, contr;
  2035. u16 errcode;
  2036. if ((p = strchr(revision, ':')) != NULL && p[1]) {
  2037. strncpy(rev, p + 2, sizeof(rev));
  2038. rev[sizeof(rev)-1] = 0;
  2039. if ((p = strchr(rev, '$')) != NULL && p > rev)
  2040. *(p-1) = 0;
  2041. } else
  2042. strcpy(rev, "1.0");
  2043. global.ap.rparam.level3cnt = -2; /* number of bchannels twice */
  2044. global.ap.rparam.datablkcnt = 16;
  2045. global.ap.rparam.datablklen = 2048;
  2046. global.ap.recv_message = capidrv_recv_message;
  2047. errcode = capi20_register(&global.ap);
  2048. if (errcode) {
  2049. return -EIO;
  2050. }
  2051. capi20_set_callback(&global.ap, lower_callback);
  2052. errcode = capi20_get_profile(0, &profile);
  2053. if (errcode != CAPI_NOERROR) {
  2054. capi20_release(&global.ap);
  2055. return -EIO;
  2056. }
  2057. ncontr = profile.ncontroller;
  2058. for (contr = 1; contr <= ncontr; contr++) {
  2059. errcode = capi20_get_profile(contr, &profile);
  2060. if (errcode != CAPI_NOERROR)
  2061. continue;
  2062. (void) capidrv_addcontr(contr, &profile);
  2063. }
  2064. proc_init();
  2065. printk(KERN_NOTICE "capidrv: Rev %s: loaded\n", rev);
  2066. return 0;
  2067. }
  2068. static void __exit capidrv_exit(void)
  2069. {
  2070. char rev[32];
  2071. char *p;
  2072. if ((p = strchr(revision, ':')) != NULL) {
  2073. strncpy(rev, p + 1, sizeof(rev));
  2074. rev[sizeof(rev)-1] = 0;
  2075. if ((p = strchr(rev, '$')) != NULL)
  2076. *p = 0;
  2077. } else {
  2078. strcpy(rev, " ??? ");
  2079. }
  2080. capi20_release(&global.ap);
  2081. proc_exit();
  2082. printk(KERN_NOTICE "capidrv: Rev%s: unloaded\n", rev);
  2083. }
  2084. module_init(capidrv_init);
  2085. module_exit(capidrv_exit);