capidrv.c 62 KB

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