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