capidrv.c 62 KB

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