capidrv.c 63 KB

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