capidrv.c 62 KB

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