capidrv.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522
  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 char *capi_info2str(u16 reason)
  670. {
  671. #ifndef CONFIG_ISDN_CAPI_CAPIDRV_VERBOSE
  672. return "..";
  673. #else
  674. switch (reason) {
  675. /*-- informative values (corresponding message was processed) -----*/
  676. case 0x0001:
  677. return "NCPI not supported by current protocol, NCPI ignored";
  678. case 0x0002:
  679. return "Flags not supported by current protocol, flags ignored";
  680. case 0x0003:
  681. return "Alert already sent by another application";
  682. /*-- error information concerning CAPI_REGISTER -----*/
  683. case 0x1001:
  684. return "Too many applications";
  685. case 0x1002:
  686. return "Logical block size too small, must be at least 128 Bytes";
  687. case 0x1003:
  688. return "Buffer exceeds 64 kByte";
  689. case 0x1004:
  690. return "Message buffer size too small, must be at least 1024 Bytes";
  691. case 0x1005:
  692. return "Max. number of logical connections not supported";
  693. case 0x1006:
  694. return "Reserved";
  695. case 0x1007:
  696. return "The message could not be accepted because of an internal busy condition";
  697. case 0x1008:
  698. return "OS resource error (no memory ?)";
  699. case 0x1009:
  700. return "CAPI not installed";
  701. case 0x100A:
  702. return "Controller does not support external equipment";
  703. case 0x100B:
  704. return "Controller does only support external equipment";
  705. /*-- error information concerning message exchange functions -----*/
  706. case 0x1101:
  707. return "Illegal application number";
  708. case 0x1102:
  709. return "Illegal command or subcommand or message length less than 12 bytes";
  710. case 0x1103:
  711. return "The message could not be accepted because of a queue full condition !! The error code does not imply that CAPI cannot receive messages directed to another controller, PLCI or NCCI";
  712. case 0x1104:
  713. return "Queue is empty";
  714. case 0x1105:
  715. return "Queue overflow, a message was lost !! This indicates a configuration error. The only recovery from this error is to perform a CAPI_RELEASE";
  716. case 0x1106:
  717. return "Unknown notification parameter";
  718. case 0x1107:
  719. return "The Message could not be accepted because of an internal busy condition";
  720. case 0x1108:
  721. return "OS Resource error (no memory ?)";
  722. case 0x1109:
  723. return "CAPI not installed";
  724. case 0x110A:
  725. return "Controller does not support external equipment";
  726. case 0x110B:
  727. return "Controller does only support external equipment";
  728. /*-- error information concerning resource / coding problems -----*/
  729. case 0x2001:
  730. return "Message not supported in current state";
  731. case 0x2002:
  732. return "Illegal Controller / PLCI / NCCI";
  733. case 0x2003:
  734. return "Out of PLCI";
  735. case 0x2004:
  736. return "Out of NCCI";
  737. case 0x2005:
  738. return "Out of LISTEN";
  739. case 0x2006:
  740. return "Out of FAX resources (protocol T.30)";
  741. case 0x2007:
  742. return "Illegal message parameter coding";
  743. /*-- error information concerning requested services -----*/
  744. case 0x3001:
  745. return "B1 protocol not supported";
  746. case 0x3002:
  747. return "B2 protocol not supported";
  748. case 0x3003:
  749. return "B3 protocol not supported";
  750. case 0x3004:
  751. return "B1 protocol parameter not supported";
  752. case 0x3005:
  753. return "B2 protocol parameter not supported";
  754. case 0x3006:
  755. return "B3 protocol parameter not supported";
  756. case 0x3007:
  757. return "B protocol combination not supported";
  758. case 0x3008:
  759. return "NCPI not supported";
  760. case 0x3009:
  761. return "CIP Value unknown";
  762. case 0x300A:
  763. return "Flags not supported (reserved bits)";
  764. case 0x300B:
  765. return "Facility not supported";
  766. case 0x300C:
  767. return "Data length not supported by current protocol";
  768. case 0x300D:
  769. return "Reset procedure not supported by current protocol";
  770. /*-- informations about the clearing of a physical connection -----*/
  771. case 0x3301:
  772. return "Protocol error layer 1 (broken line or B-channel removed by signalling protocol)";
  773. case 0x3302:
  774. return "Protocol error layer 2";
  775. case 0x3303:
  776. return "Protocol error layer 3";
  777. case 0x3304:
  778. return "Another application got that call";
  779. /*-- T.30 specific reasons -----*/
  780. case 0x3311:
  781. return "Connecting not successful (remote station is no FAX G3 machine)";
  782. case 0x3312:
  783. return "Connecting not successful (training error)";
  784. case 0x3313:
  785. return "Disconnected before transfer (remote station does not support transfer mode, e.g. resolution)";
  786. case 0x3314:
  787. return "Disconnected during transfer (remote abort)";
  788. case 0x3315:
  789. return "Disconnected during transfer (remote procedure error, e.g. unsuccessful repetition of T.30 commands)";
  790. case 0x3316:
  791. return "Disconnected during transfer (local tx data underrun)";
  792. case 0x3317:
  793. return "Disconnected during transfer (local rx data overflow)";
  794. case 0x3318:
  795. return "Disconnected during transfer (local abort)";
  796. case 0x3319:
  797. return "Illegal parameter coding (e.g. SFF coding error)";
  798. /*-- disconnect causes from the network according to ETS 300 102-1/Q.931 -----*/
  799. case 0x3481: return "Unallocated (unassigned) number";
  800. case 0x3482: return "No route to specified transit network";
  801. case 0x3483: return "No route to destination";
  802. case 0x3486: return "Channel unacceptable";
  803. case 0x3487:
  804. return "Call awarded and being delivered in an established channel";
  805. case 0x3490: return "Normal call clearing";
  806. case 0x3491: return "User busy";
  807. case 0x3492: return "No user responding";
  808. case 0x3493: return "No answer from user (user alerted)";
  809. case 0x3495: return "Call rejected";
  810. case 0x3496: return "Number changed";
  811. case 0x349A: return "Non-selected user clearing";
  812. case 0x349B: return "Destination out of order";
  813. case 0x349C: return "Invalid number format";
  814. case 0x349D: return "Facility rejected";
  815. case 0x349E: return "Response to STATUS ENQUIRY";
  816. case 0x349F: return "Normal, unspecified";
  817. case 0x34A2: return "No circuit / channel available";
  818. case 0x34A6: return "Network out of order";
  819. case 0x34A9: return "Temporary failure";
  820. case 0x34AA: return "Switching equipment congestion";
  821. case 0x34AB: return "Access information discarded";
  822. case 0x34AC: return "Requested circuit / channel not available";
  823. case 0x34AF: return "Resources unavailable, unspecified";
  824. case 0x34B1: return "Quality of service unavailable";
  825. case 0x34B2: return "Requested facility not subscribed";
  826. case 0x34B9: return "Bearer capability not authorized";
  827. case 0x34BA: return "Bearer capability not presently available";
  828. case 0x34BF: return "Service or option not available, unspecified";
  829. case 0x34C1: return "Bearer capability not implemented";
  830. case 0x34C2: return "Channel type not implemented";
  831. case 0x34C5: return "Requested facility not implemented";
  832. case 0x34C6: return "Only restricted digital information bearer capability is available";
  833. case 0x34CF: return "Service or option not implemented, unspecified";
  834. case 0x34D1: return "Invalid call reference value";
  835. case 0x34D2: return "Identified channel does not exist";
  836. case 0x34D3: return "A suspended call exists, but this call identity does not";
  837. case 0x34D4: return "Call identity in use";
  838. case 0x34D5: return "No call suspended";
  839. case 0x34D6: return "Call having the requested call identity has been cleared";
  840. case 0x34D8: return "Incompatible destination";
  841. case 0x34DB: return "Invalid transit network selection";
  842. case 0x34DF: return "Invalid message, unspecified";
  843. case 0x34E0: return "Mandatory information element is missing";
  844. case 0x34E1: return "Message type non-existent or not implemented";
  845. case 0x34E2: return "Message not compatible with call state or message type non-existent or not implemented";
  846. case 0x34E3: return "Information element non-existent or not implemented";
  847. case 0x34E4: return "Invalid information element contents";
  848. case 0x34E5: return "Message not compatible with call state";
  849. case 0x34E6: return "Recovery on timer expiry";
  850. case 0x34EF: return "Protocol error, unspecified";
  851. case 0x34FF: return "Interworking, unspecified";
  852. default: return "No additional information";
  853. }
  854. #endif
  855. }
  856. static void handle_controller(_cmsg *cmsg)
  857. {
  858. capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
  859. if (!card) {
  860. printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
  861. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  862. cmsg->adr.adrController & 0x7f);
  863. return;
  864. }
  865. switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
  866. case CAPI_LISTEN_CONF: /* Controller */
  867. if (debugmode)
  868. printk(KERN_DEBUG "capidrv-%d: listenconf Info=0x%4x (%s) cipmask=0x%x\n",
  869. card->contrnr, cmsg->Info, capi_info2str(cmsg->Info), card->cipmask);
  870. if (cmsg->Info) {
  871. listen_change_state(card, EV_LISTEN_CONF_ERROR);
  872. } else if (card->cipmask == 0) {
  873. listen_change_state(card, EV_LISTEN_CONF_EMPTY);
  874. } else {
  875. listen_change_state(card, EV_LISTEN_CONF_OK);
  876. }
  877. break;
  878. case CAPI_MANUFACTURER_IND: /* Controller */
  879. if (cmsg->ManuID == 0x214D5641
  880. && cmsg->Class == 0
  881. && cmsg->Function == 1) {
  882. u8 *data = cmsg->ManuData + 3;
  883. u16 len = cmsg->ManuData[0];
  884. u16 layer;
  885. int direction;
  886. if (len == 255) {
  887. len = (cmsg->ManuData[1] | (cmsg->ManuData[2] << 8));
  888. data += 2;
  889. }
  890. len -= 2;
  891. layer = ((*(data - 1)) << 8) | *(data - 2);
  892. if (layer & 0x300)
  893. direction = (layer & 0x200) ? 0 : 1;
  894. else direction = (layer & 0x800) ? 0 : 1;
  895. if (layer & 0x0C00) {
  896. if ((layer & 0xff) == 0x80) {
  897. handle_dtrace_data(card, direction, 1, data, len);
  898. break;
  899. }
  900. } else if ((layer & 0xff) < 0x80) {
  901. handle_dtrace_data(card, direction, 0, data, len);
  902. break;
  903. }
  904. printk(KERN_INFO "capidrv-%d: %s from controller 0x%x layer 0x%x, ignored\n",
  905. card->contrnr,
  906. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  907. cmsg->adr.adrController, layer);
  908. break;
  909. }
  910. goto ignored;
  911. case CAPI_MANUFACTURER_CONF: /* Controller */
  912. if (cmsg->ManuID == 0x214D5641) {
  913. char *s = NULL;
  914. switch (cmsg->Class) {
  915. case 0: break;
  916. case 1: s = "unknown class"; break;
  917. case 2: s = "unknown function"; break;
  918. default: s = "unknown error"; break;
  919. }
  920. if (s)
  921. printk(KERN_INFO "capidrv-%d: %s from controller 0x%x function %d: %s\n",
  922. card->contrnr,
  923. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  924. cmsg->adr.adrController,
  925. cmsg->Function, s);
  926. break;
  927. }
  928. goto ignored;
  929. case CAPI_FACILITY_IND: /* Controller/plci/ncci */
  930. goto ignored;
  931. case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
  932. goto ignored;
  933. case CAPI_INFO_IND: /* Controller/plci */
  934. goto ignored;
  935. case CAPI_INFO_CONF: /* Controller/plci */
  936. goto ignored;
  937. default:
  938. printk(KERN_ERR "capidrv-%d: got %s from controller 0x%x ???",
  939. card->contrnr,
  940. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  941. cmsg->adr.adrController);
  942. }
  943. return;
  944. ignored:
  945. printk(KERN_INFO "capidrv-%d: %s from controller 0x%x ignored\n",
  946. card->contrnr,
  947. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  948. cmsg->adr.adrController);
  949. }
  950. static void handle_incoming_call(capidrv_contr *card, _cmsg *cmsg)
  951. {
  952. capidrv_plci *plcip;
  953. capidrv_bchan *bchan;
  954. isdn_ctrl cmd;
  955. int chan;
  956. if ((chan = new_bchan(card)) == -1) {
  957. printk(KERN_ERR "capidrv-%d: incoming call on not existing bchan ?\n", card->contrnr);
  958. return;
  959. }
  960. bchan = &card->bchans[chan];
  961. if ((plcip = new_plci(card, chan)) == NULL) {
  962. printk(KERN_ERR "capidrv-%d: incoming call: no memory, sorry.\n", card->contrnr);
  963. return;
  964. }
  965. bchan->incoming = 1;
  966. plcip->plci = cmsg->adr.adrPLCI;
  967. plci_change_state(card, plcip, EV_PLCI_CONNECT_IND);
  968. cmd.command = ISDN_STAT_ICALL;
  969. cmd.driver = card->myid;
  970. cmd.arg = chan;
  971. memset(&cmd.parm.setup, 0, sizeof(cmd.parm.setup));
  972. strncpy(cmd.parm.setup.phone,
  973. cmsg->CallingPartyNumber + 3,
  974. cmsg->CallingPartyNumber[0] - 2);
  975. strncpy(cmd.parm.setup.eazmsn,
  976. cmsg->CalledPartyNumber + 2,
  977. cmsg->CalledPartyNumber[0] - 1);
  978. cmd.parm.setup.si1 = cip2si1(cmsg->CIPValue);
  979. cmd.parm.setup.si2 = cip2si2(cmsg->CIPValue);
  980. cmd.parm.setup.plan = cmsg->CallingPartyNumber[1];
  981. cmd.parm.setup.screen = cmsg->CallingPartyNumber[2];
  982. printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s\n",
  983. card->contrnr,
  984. cmd.parm.setup.phone,
  985. cmd.parm.setup.si1,
  986. cmd.parm.setup.si2,
  987. cmd.parm.setup.eazmsn);
  988. if (cmd.parm.setup.si1 == 1 && cmd.parm.setup.si2 != 0) {
  989. printk(KERN_INFO "capidrv-%d: patching si2=%d to 0 for VBOX\n",
  990. card->contrnr,
  991. cmd.parm.setup.si2);
  992. cmd.parm.setup.si2 = 0;
  993. }
  994. switch (card->interface.statcallb(&cmd)) {
  995. case 0:
  996. case 3:
  997. /* No device matching this call.
  998. * and isdn_common.c has send a HANGUP command
  999. * which is ignored in state ST_PLCI_INCOMING,
  1000. * so we send RESP to ignore the call
  1001. */
  1002. capi_cmsg_answer(cmsg);
  1003. cmsg->Reject = 1; /* ignore */
  1004. plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
  1005. send_message(card, cmsg);
  1006. printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s ignored\n",
  1007. card->contrnr,
  1008. cmd.parm.setup.phone,
  1009. cmd.parm.setup.si1,
  1010. cmd.parm.setup.si2,
  1011. cmd.parm.setup.eazmsn);
  1012. break;
  1013. case 1:
  1014. /* At least one device matching this call (RING on ttyI)
  1015. * HL-driver may send ALERTING on the D-channel in this
  1016. * case.
  1017. * really means: RING on ttyI or a net interface
  1018. * accepted this call already.
  1019. *
  1020. * If the call was accepted, state has already changed,
  1021. * and CONNECT_RESP already sent.
  1022. */
  1023. if (plcip->state == ST_PLCI_INCOMING) {
  1024. printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s tty alerting\n",
  1025. card->contrnr,
  1026. cmd.parm.setup.phone,
  1027. cmd.parm.setup.si1,
  1028. cmd.parm.setup.si2,
  1029. cmd.parm.setup.eazmsn);
  1030. capi_fill_ALERT_REQ(cmsg,
  1031. global.ap.applid,
  1032. card->msgid++,
  1033. plcip->plci, /* adr */
  1034. NULL,/* BChannelinformation */
  1035. NULL,/* Keypadfacility */
  1036. NULL,/* Useruserdata */
  1037. NULL /* Facilitydataarray */
  1038. );
  1039. plcip->msgid = cmsg->Messagenumber;
  1040. send_message(card, cmsg);
  1041. } else {
  1042. printk(KERN_INFO "capidrv-%d: incoming call %s,%d,%d,%s on netdev\n",
  1043. card->contrnr,
  1044. cmd.parm.setup.phone,
  1045. cmd.parm.setup.si1,
  1046. cmd.parm.setup.si2,
  1047. cmd.parm.setup.eazmsn);
  1048. }
  1049. break;
  1050. case 2: /* Call will be rejected. */
  1051. capi_cmsg_answer(cmsg);
  1052. cmsg->Reject = 2; /* reject call, normal call clearing */
  1053. plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
  1054. send_message(card, cmsg);
  1055. break;
  1056. default:
  1057. /* An error happened. (Invalid parameters for example.) */
  1058. capi_cmsg_answer(cmsg);
  1059. cmsg->Reject = 8; /* reject call,
  1060. destination out of order */
  1061. plci_change_state(card, plcip, EV_PLCI_CONNECT_REJECT);
  1062. send_message(card, cmsg);
  1063. break;
  1064. }
  1065. return;
  1066. }
  1067. static void handle_plci(_cmsg *cmsg)
  1068. {
  1069. capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
  1070. capidrv_plci *plcip;
  1071. isdn_ctrl cmd;
  1072. _cdebbuf *cdb;
  1073. if (!card) {
  1074. printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
  1075. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1076. cmsg->adr.adrController & 0x7f);
  1077. return;
  1078. }
  1079. switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
  1080. case CAPI_DISCONNECT_IND: /* plci */
  1081. if (cmsg->Reason) {
  1082. printk(KERN_INFO "capidrv-%d: %s reason 0x%x (%s) for plci 0x%x\n",
  1083. card->contrnr,
  1084. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1085. cmsg->Reason, capi_info2str(cmsg->Reason), cmsg->adr.adrPLCI);
  1086. }
  1087. if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI))) {
  1088. capi_cmsg_answer(cmsg);
  1089. send_message(card, cmsg);
  1090. goto notfound;
  1091. }
  1092. card->bchans[plcip->chan].disconnecting = 1;
  1093. plci_change_state(card, plcip, EV_PLCI_DISCONNECT_IND);
  1094. capi_cmsg_answer(cmsg);
  1095. plci_change_state(card, plcip, EV_PLCI_DISCONNECT_RESP);
  1096. send_message(card, cmsg);
  1097. break;
  1098. case CAPI_DISCONNECT_CONF: /* plci */
  1099. if (cmsg->Info) {
  1100. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
  1101. card->contrnr,
  1102. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1103. cmsg->Info, capi_info2str(cmsg->Info),
  1104. cmsg->adr.adrPLCI);
  1105. }
  1106. if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
  1107. goto notfound;
  1108. card->bchans[plcip->chan].disconnecting = 1;
  1109. break;
  1110. case CAPI_ALERT_CONF: /* plci */
  1111. if (cmsg->Info) {
  1112. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
  1113. card->contrnr,
  1114. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1115. cmsg->Info, capi_info2str(cmsg->Info),
  1116. cmsg->adr.adrPLCI);
  1117. }
  1118. break;
  1119. case CAPI_CONNECT_IND: /* plci */
  1120. handle_incoming_call(card, cmsg);
  1121. break;
  1122. case CAPI_CONNECT_CONF: /* plci */
  1123. if (cmsg->Info) {
  1124. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for plci 0x%x\n",
  1125. card->contrnr,
  1126. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1127. cmsg->Info, capi_info2str(cmsg->Info),
  1128. cmsg->adr.adrPLCI);
  1129. }
  1130. if (!(plcip = find_plci_by_msgid(card, cmsg->Messagenumber)))
  1131. goto notfound;
  1132. plcip->plci = cmsg->adr.adrPLCI;
  1133. if (cmsg->Info) {
  1134. plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_ERROR);
  1135. } else {
  1136. plci_change_state(card, plcip, EV_PLCI_CONNECT_CONF_OK);
  1137. }
  1138. break;
  1139. case CAPI_CONNECT_ACTIVE_IND: /* plci */
  1140. if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
  1141. goto notfound;
  1142. if (card->bchans[plcip->chan].incoming) {
  1143. capi_cmsg_answer(cmsg);
  1144. plci_change_state(card, plcip, EV_PLCI_CONNECT_ACTIVE_IND);
  1145. send_message(card, cmsg);
  1146. } else {
  1147. capidrv_ncci *nccip;
  1148. capi_cmsg_answer(cmsg);
  1149. send_message(card, cmsg);
  1150. nccip = new_ncci(card, plcip, cmsg->adr.adrPLCI);
  1151. if (!nccip) {
  1152. printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
  1153. break; /* $$$$ */
  1154. }
  1155. capi_fill_CONNECT_B3_REQ(cmsg,
  1156. global.ap.applid,
  1157. card->msgid++,
  1158. plcip->plci, /* adr */
  1159. NULL /* NCPI */
  1160. );
  1161. nccip->msgid = cmsg->Messagenumber;
  1162. plci_change_state(card, plcip,
  1163. EV_PLCI_CONNECT_ACTIVE_IND);
  1164. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_REQ);
  1165. send_message(card, cmsg);
  1166. cmd.command = ISDN_STAT_DCONN;
  1167. cmd.driver = card->myid;
  1168. cmd.arg = plcip->chan;
  1169. card->interface.statcallb(&cmd);
  1170. }
  1171. break;
  1172. case CAPI_INFO_IND: /* Controller/plci */
  1173. if (!(plcip = find_plci_by_plci(card, cmsg->adr.adrPLCI)))
  1174. goto notfound;
  1175. if (cmsg->InfoNumber == 0x4000) {
  1176. if (cmsg->InfoElement[0] == 4) {
  1177. cmd.command = ISDN_STAT_CINF;
  1178. cmd.driver = card->myid;
  1179. cmd.arg = plcip->chan;
  1180. sprintf(cmd.parm.num, "%lu",
  1181. (unsigned long)
  1182. ((u32) cmsg->InfoElement[1]
  1183. | ((u32) (cmsg->InfoElement[2]) << 8)
  1184. | ((u32) (cmsg->InfoElement[3]) << 16)
  1185. | ((u32) (cmsg->InfoElement[4]) << 24)));
  1186. card->interface.statcallb(&cmd);
  1187. break;
  1188. }
  1189. }
  1190. cdb = capi_cmsg2str(cmsg);
  1191. if (cdb) {
  1192. printk(KERN_WARNING "capidrv-%d: %s\n",
  1193. card->contrnr, cdb->buf);
  1194. cdebbuf_free(cdb);
  1195. } else
  1196. printk(KERN_WARNING "capidrv-%d: CAPI_INFO_IND InfoNumber %x not handled\n",
  1197. card->contrnr, cmsg->InfoNumber);
  1198. break;
  1199. case CAPI_CONNECT_ACTIVE_CONF: /* plci */
  1200. goto ignored;
  1201. case CAPI_SELECT_B_PROTOCOL_CONF: /* plci */
  1202. goto ignored;
  1203. case CAPI_FACILITY_IND: /* Controller/plci/ncci */
  1204. goto ignored;
  1205. case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
  1206. goto ignored;
  1207. case CAPI_INFO_CONF: /* Controller/plci */
  1208. goto ignored;
  1209. default:
  1210. printk(KERN_ERR "capidrv-%d: got %s for plci 0x%x ???",
  1211. card->contrnr,
  1212. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1213. cmsg->adr.adrPLCI);
  1214. }
  1215. return;
  1216. ignored:
  1217. printk(KERN_INFO "capidrv-%d: %s for plci 0x%x ignored\n",
  1218. card->contrnr,
  1219. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1220. cmsg->adr.adrPLCI);
  1221. return;
  1222. notfound:
  1223. printk(KERN_ERR "capidrv-%d: %s: plci 0x%x not found\n",
  1224. card->contrnr,
  1225. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1226. cmsg->adr.adrPLCI);
  1227. return;
  1228. }
  1229. static void handle_ncci(_cmsg *cmsg)
  1230. {
  1231. capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
  1232. capidrv_plci *plcip;
  1233. capidrv_ncci *nccip;
  1234. isdn_ctrl cmd;
  1235. int len;
  1236. if (!card) {
  1237. printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
  1238. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1239. cmsg->adr.adrController & 0x7f);
  1240. return;
  1241. }
  1242. switch (CAPICMD(cmsg->Command, cmsg->Subcommand)) {
  1243. case CAPI_CONNECT_B3_ACTIVE_IND: /* ncci */
  1244. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1245. goto notfound;
  1246. capi_cmsg_answer(cmsg);
  1247. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_ACTIVE_IND);
  1248. send_message(card, cmsg);
  1249. cmd.command = ISDN_STAT_BCONN;
  1250. cmd.driver = card->myid;
  1251. cmd.arg = nccip->chan;
  1252. card->interface.statcallb(&cmd);
  1253. printk(KERN_INFO "capidrv-%d: chan %d up with ncci 0x%x\n",
  1254. card->contrnr, nccip->chan, nccip->ncci);
  1255. break;
  1256. case CAPI_CONNECT_B3_ACTIVE_CONF: /* ncci */
  1257. goto ignored;
  1258. case CAPI_CONNECT_B3_IND: /* ncci */
  1259. plcip = find_plci_by_ncci(card, cmsg->adr.adrNCCI);
  1260. if (plcip) {
  1261. nccip = new_ncci(card, plcip, cmsg->adr.adrNCCI);
  1262. if (nccip) {
  1263. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_IND);
  1264. capi_fill_CONNECT_B3_RESP(cmsg,
  1265. global.ap.applid,
  1266. card->msgid++,
  1267. nccip->ncci, /* adr */
  1268. 0, /* Reject */
  1269. NULL /* NCPI */
  1270. );
  1271. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_RESP);
  1272. send_message(card, cmsg);
  1273. break;
  1274. }
  1275. printk(KERN_ERR "capidrv-%d: no mem for ncci, sorry\n", card->contrnr);
  1276. } else {
  1277. printk(KERN_ERR "capidrv-%d: %s: plci for ncci 0x%x not found\n",
  1278. card->contrnr,
  1279. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1280. cmsg->adr.adrNCCI);
  1281. }
  1282. capi_fill_CONNECT_B3_RESP(cmsg,
  1283. global.ap.applid,
  1284. card->msgid++,
  1285. cmsg->adr.adrNCCI,
  1286. 2, /* Reject */
  1287. NULL /* NCPI */
  1288. );
  1289. send_message(card, cmsg);
  1290. break;
  1291. case CAPI_CONNECT_B3_CONF: /* ncci */
  1292. if (!(nccip = find_ncci_by_msgid(card,
  1293. cmsg->adr.adrNCCI,
  1294. cmsg->Messagenumber)))
  1295. goto notfound;
  1296. nccip->ncci = cmsg->adr.adrNCCI;
  1297. if (cmsg->Info) {
  1298. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
  1299. card->contrnr,
  1300. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1301. cmsg->Info, capi_info2str(cmsg->Info),
  1302. cmsg->adr.adrNCCI);
  1303. }
  1304. if (cmsg->Info)
  1305. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_ERROR);
  1306. else
  1307. ncci_change_state(card, nccip, EV_NCCI_CONNECT_B3_CONF_OK);
  1308. break;
  1309. case CAPI_CONNECT_B3_T90_ACTIVE_IND: /* ncci */
  1310. capi_cmsg_answer(cmsg);
  1311. send_message(card, cmsg);
  1312. break;
  1313. case CAPI_DATA_B3_IND: /* ncci */
  1314. /* handled in handle_data() */
  1315. goto ignored;
  1316. case CAPI_DATA_B3_CONF: /* ncci */
  1317. if (cmsg->Info) {
  1318. printk(KERN_WARNING "CAPI_DATA_B3_CONF: Info %x - %s\n",
  1319. cmsg->Info, capi_info2str(cmsg->Info));
  1320. }
  1321. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1322. goto notfound;
  1323. len = capidrv_del_ack(nccip, cmsg->DataHandle);
  1324. if (len < 0)
  1325. break;
  1326. cmd.command = ISDN_STAT_BSENT;
  1327. cmd.driver = card->myid;
  1328. cmd.arg = nccip->chan;
  1329. cmd.parm.length = len;
  1330. card->interface.statcallb(&cmd);
  1331. break;
  1332. case CAPI_DISCONNECT_B3_IND: /* ncci */
  1333. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1334. goto notfound;
  1335. card->bchans[nccip->chan].disconnecting = 1;
  1336. ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_IND);
  1337. capi_cmsg_answer(cmsg);
  1338. ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_RESP);
  1339. send_message(card, cmsg);
  1340. break;
  1341. case CAPI_DISCONNECT_B3_CONF: /* ncci */
  1342. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1343. goto notfound;
  1344. if (cmsg->Info) {
  1345. printk(KERN_INFO "capidrv-%d: %s info 0x%x (%s) for ncci 0x%x\n",
  1346. card->contrnr,
  1347. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1348. cmsg->Info, capi_info2str(cmsg->Info),
  1349. cmsg->adr.adrNCCI);
  1350. ncci_change_state(card, nccip, EV_NCCI_DISCONNECT_B3_CONF_ERROR);
  1351. }
  1352. break;
  1353. case CAPI_RESET_B3_IND: /* ncci */
  1354. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI)))
  1355. goto notfound;
  1356. ncci_change_state(card, nccip, EV_NCCI_RESET_B3_IND);
  1357. capi_cmsg_answer(cmsg);
  1358. send_message(card, cmsg);
  1359. break;
  1360. case CAPI_RESET_B3_CONF: /* ncci */
  1361. goto ignored; /* $$$$ */
  1362. case CAPI_FACILITY_IND: /* Controller/plci/ncci */
  1363. goto ignored;
  1364. case CAPI_FACILITY_CONF: /* Controller/plci/ncci */
  1365. goto ignored;
  1366. default:
  1367. printk(KERN_ERR "capidrv-%d: got %s for ncci 0x%x ???",
  1368. card->contrnr,
  1369. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1370. cmsg->adr.adrNCCI);
  1371. }
  1372. return;
  1373. ignored:
  1374. printk(KERN_INFO "capidrv-%d: %s for ncci 0x%x ignored\n",
  1375. card->contrnr,
  1376. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1377. cmsg->adr.adrNCCI);
  1378. return;
  1379. notfound:
  1380. printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
  1381. card->contrnr,
  1382. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1383. cmsg->adr.adrNCCI);
  1384. }
  1385. static void handle_data(_cmsg *cmsg, struct sk_buff *skb)
  1386. {
  1387. capidrv_contr *card = findcontrbynumber(cmsg->adr.adrController & 0x7f);
  1388. capidrv_ncci *nccip;
  1389. if (!card) {
  1390. printk(KERN_ERR "capidrv: %s from unknown controller 0x%x\n",
  1391. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1392. cmsg->adr.adrController & 0x7f);
  1393. kfree_skb(skb);
  1394. return;
  1395. }
  1396. if (!(nccip = find_ncci(card, cmsg->adr.adrNCCI))) {
  1397. printk(KERN_ERR "capidrv-%d: %s: ncci 0x%x not found\n",
  1398. card->contrnr,
  1399. capi_cmd2str(cmsg->Command, cmsg->Subcommand),
  1400. cmsg->adr.adrNCCI);
  1401. kfree_skb(skb);
  1402. return;
  1403. }
  1404. (void) skb_pull(skb, CAPIMSG_LEN(skb->data));
  1405. card->interface.rcvcallb_skb(card->myid, nccip->chan, skb);
  1406. capi_cmsg_answer(cmsg);
  1407. send_message(card, cmsg);
  1408. }
  1409. static _cmsg s_cmsg;
  1410. static void capidrv_recv_message(struct capi20_appl *ap, struct sk_buff *skb)
  1411. {
  1412. capi_message2cmsg(&s_cmsg, skb->data);
  1413. if (debugmode > 3) {
  1414. _cdebbuf *cdb = capi_cmsg2str(&s_cmsg);
  1415. if (cdb) {
  1416. printk(KERN_DEBUG "%s: applid=%d %s\n", __func__,
  1417. ap->applid, cdb->buf);
  1418. cdebbuf_free(cdb);
  1419. } else
  1420. printk(KERN_DEBUG "%s: applid=%d %s not traced\n",
  1421. __func__, ap->applid,
  1422. capi_cmd2str(s_cmsg.Command, s_cmsg.Subcommand));
  1423. }
  1424. if (s_cmsg.Command == CAPI_DATA_B3
  1425. && s_cmsg.Subcommand == CAPI_IND) {
  1426. handle_data(&s_cmsg, skb);
  1427. return;
  1428. }
  1429. if ((s_cmsg.adr.adrController & 0xffffff00) == 0)
  1430. handle_controller(&s_cmsg);
  1431. else if ((s_cmsg.adr.adrPLCI & 0xffff0000) == 0)
  1432. handle_plci(&s_cmsg);
  1433. else
  1434. handle_ncci(&s_cmsg);
  1435. /*
  1436. * data of skb used in s_cmsg,
  1437. * free data when s_cmsg is not used again
  1438. * thanks to Lars Heete <hel@admin.de>
  1439. */
  1440. kfree_skb(skb);
  1441. }
  1442. /* ------------------------------------------------------------------- */
  1443. #define PUTBYTE_TO_STATUS(card, byte) \
  1444. do { \
  1445. *(card)->q931_write++ = (byte); \
  1446. if ((card)->q931_write > (card)->q931_end) \
  1447. (card)->q931_write = (card)->q931_buf; \
  1448. } while (0)
  1449. static void handle_dtrace_data(capidrv_contr *card,
  1450. int send, int level2, u8 *data, u16 len)
  1451. {
  1452. u8 *p, *end;
  1453. isdn_ctrl cmd;
  1454. if (!len) {
  1455. printk(KERN_DEBUG "capidrv-%d: avmb1_q931_data: len == %d\n",
  1456. card->contrnr, len);
  1457. return;
  1458. }
  1459. if (level2) {
  1460. PUTBYTE_TO_STATUS(card, 'D');
  1461. PUTBYTE_TO_STATUS(card, '2');
  1462. PUTBYTE_TO_STATUS(card, send ? '>' : '<');
  1463. PUTBYTE_TO_STATUS(card, ':');
  1464. } else {
  1465. PUTBYTE_TO_STATUS(card, 'D');
  1466. PUTBYTE_TO_STATUS(card, '3');
  1467. PUTBYTE_TO_STATUS(card, send ? '>' : '<');
  1468. PUTBYTE_TO_STATUS(card, ':');
  1469. }
  1470. for (p = data, end = data + len; p < end; p++) {
  1471. PUTBYTE_TO_STATUS(card, ' ');
  1472. PUTBYTE_TO_STATUS(card, hex_asc_hi(*p));
  1473. PUTBYTE_TO_STATUS(card, hex_asc_lo(*p));
  1474. }
  1475. PUTBYTE_TO_STATUS(card, '\n');
  1476. cmd.command = ISDN_STAT_STAVAIL;
  1477. cmd.driver = card->myid;
  1478. cmd.arg = len * 3 + 5;
  1479. card->interface.statcallb(&cmd);
  1480. }
  1481. /* ------------------------------------------------------------------- */
  1482. static _cmsg cmdcmsg;
  1483. static int capidrv_ioctl(isdn_ctrl *c, capidrv_contr *card)
  1484. {
  1485. switch (c->arg) {
  1486. case 1:
  1487. debugmode = (int)(*((unsigned int *)c->parm.num));
  1488. printk(KERN_DEBUG "capidrv-%d: debugmode=%d\n",
  1489. card->contrnr, debugmode);
  1490. return 0;
  1491. default:
  1492. printk(KERN_DEBUG "capidrv-%d: capidrv_ioctl(%ld) called ??\n",
  1493. card->contrnr, c->arg);
  1494. return -EINVAL;
  1495. }
  1496. return -EINVAL;
  1497. }
  1498. /*
  1499. * Handle leased lines (CAPI-Bundling)
  1500. */
  1501. struct internal_bchannelinfo {
  1502. unsigned short channelalloc;
  1503. unsigned short operation;
  1504. unsigned char cmask[31];
  1505. };
  1506. static int decodeFVteln(char *teln, unsigned long *bmaskp, int *activep)
  1507. {
  1508. unsigned long bmask = 0;
  1509. int active = !0;
  1510. char *s;
  1511. int i;
  1512. if (strncmp(teln, "FV:", 3) != 0)
  1513. return 1;
  1514. s = teln + 3;
  1515. while (*s && *s == ' ') s++;
  1516. if (!*s) return -2;
  1517. if (*s == 'p' || *s == 'P') {
  1518. active = 0;
  1519. s++;
  1520. }
  1521. if (*s == 'a' || *s == 'A') {
  1522. active = !0;
  1523. s++;
  1524. }
  1525. while (*s) {
  1526. int digit1 = 0;
  1527. int digit2 = 0;
  1528. char *endp;
  1529. digit1 = simple_strtoul(s, &endp, 10);
  1530. if (s == endp)
  1531. return -3;
  1532. s = endp;
  1533. if (digit1 <= 0 || digit1 > 30) return -4;
  1534. if (*s == 0 || *s == ',' || *s == ' ') {
  1535. bmask |= (1 << digit1);
  1536. digit1 = 0;
  1537. if (*s) s++;
  1538. continue;
  1539. }
  1540. if (*s != '-') return -5;
  1541. s++;
  1542. digit2 = simple_strtoul(s, &endp, 10);
  1543. if (s == endp)
  1544. return -3;
  1545. s = endp;
  1546. if (digit2 <= 0 || digit2 > 30) return -4;
  1547. if (*s == 0 || *s == ',' || *s == ' ') {
  1548. if (digit1 > digit2)
  1549. for (i = digit2; i <= digit1; i++)
  1550. bmask |= (1 << i);
  1551. else
  1552. for (i = digit1; i <= digit2; i++)
  1553. bmask |= (1 << i);
  1554. digit1 = digit2 = 0;
  1555. if (*s) s++;
  1556. continue;
  1557. }
  1558. return -6;
  1559. }
  1560. if (activep) *activep = active;
  1561. if (bmaskp) *bmaskp = bmask;
  1562. return 0;
  1563. }
  1564. static int FVteln2capi20(char *teln, u8 AdditionalInfo[1 + 2 + 2 + 31])
  1565. {
  1566. unsigned long bmask;
  1567. int active;
  1568. int rc, i;
  1569. rc = decodeFVteln(teln, &bmask, &active);
  1570. if (rc) return rc;
  1571. /* Length */
  1572. AdditionalInfo[0] = 2 + 2 + 31;
  1573. /* Channel: 3 => use channel allocation */
  1574. AdditionalInfo[1] = 3; AdditionalInfo[2] = 0;
  1575. /* Operation: 0 => DTE mode, 1 => DCE mode */
  1576. if (active) {
  1577. AdditionalInfo[3] = 0; AdditionalInfo[4] = 0;
  1578. } else {
  1579. AdditionalInfo[3] = 1; AdditionalInfo[4] = 0;
  1580. }
  1581. /* Channel mask array */
  1582. AdditionalInfo[5] = 0; /* no D-Channel */
  1583. for (i = 1; i <= 30; i++)
  1584. AdditionalInfo[5 + i] = (bmask & (1 << i)) ? 0xff : 0;
  1585. return 0;
  1586. }
  1587. static int capidrv_command(isdn_ctrl *c, capidrv_contr *card)
  1588. {
  1589. isdn_ctrl cmd;
  1590. struct capidrv_bchan *bchan;
  1591. struct capidrv_plci *plcip;
  1592. u8 AdditionalInfo[1 + 2 + 2 + 31];
  1593. int rc, isleasedline = 0;
  1594. if (c->command == ISDN_CMD_IOCTL)
  1595. return capidrv_ioctl(c, card);
  1596. switch (c->command) {
  1597. case ISDN_CMD_DIAL: {
  1598. u8 calling[ISDN_MSNLEN + 3];
  1599. u8 called[ISDN_MSNLEN + 2];
  1600. if (debugmode)
  1601. printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_DIAL(ch=%ld,\"%s,%d,%d,%s\")\n",
  1602. card->contrnr,
  1603. c->arg,
  1604. c->parm.setup.phone,
  1605. c->parm.setup.si1,
  1606. c->parm.setup.si2,
  1607. c->parm.setup.eazmsn);
  1608. bchan = &card->bchans[c->arg % card->nbchan];
  1609. if (bchan->plcip) {
  1610. printk(KERN_ERR "capidrv-%d: dail ch=%ld,\"%s,%d,%d,%s\" in use (plci=0x%x)\n",
  1611. card->contrnr,
  1612. c->arg,
  1613. c->parm.setup.phone,
  1614. c->parm.setup.si1,
  1615. c->parm.setup.si2,
  1616. c->parm.setup.eazmsn,
  1617. bchan->plcip->plci);
  1618. return 0;
  1619. }
  1620. bchan->si1 = c->parm.setup.si1;
  1621. bchan->si2 = c->parm.setup.si2;
  1622. strncpy(bchan->num, c->parm.setup.phone, sizeof(bchan->num));
  1623. strncpy(bchan->mynum, c->parm.setup.eazmsn, sizeof(bchan->mynum));
  1624. rc = FVteln2capi20(bchan->num, AdditionalInfo);
  1625. isleasedline = (rc == 0);
  1626. if (rc < 0)
  1627. printk(KERN_ERR "capidrv-%d: WARNING: invalid leased linedefinition \"%s\"\n", card->contrnr, bchan->num);
  1628. if (isleasedline) {
  1629. calling[0] = 0;
  1630. called[0] = 0;
  1631. if (debugmode)
  1632. printk(KERN_DEBUG "capidrv-%d: connecting leased line\n", card->contrnr);
  1633. } else {
  1634. calling[0] = strlen(bchan->mynum) + 2;
  1635. calling[1] = 0;
  1636. calling[2] = 0x80;
  1637. strncpy(calling + 3, bchan->mynum, ISDN_MSNLEN);
  1638. called[0] = strlen(bchan->num) + 1;
  1639. called[1] = 0x80;
  1640. strncpy(called + 2, bchan->num, ISDN_MSNLEN);
  1641. }
  1642. capi_fill_CONNECT_REQ(&cmdcmsg,
  1643. global.ap.applid,
  1644. card->msgid++,
  1645. card->contrnr, /* adr */
  1646. si2cip(bchan->si1, bchan->si2), /* cipvalue */
  1647. called, /* CalledPartyNumber */
  1648. calling, /* CallingPartyNumber */
  1649. NULL, /* CalledPartySubaddress */
  1650. NULL, /* CallingPartySubaddress */
  1651. b1prot(bchan->l2, bchan->l3), /* B1protocol */
  1652. b2prot(bchan->l2, bchan->l3), /* B2protocol */
  1653. b3prot(bchan->l2, bchan->l3), /* B3protocol */
  1654. b1config(bchan->l2, bchan->l3), /* B1configuration */
  1655. NULL, /* B2configuration */
  1656. NULL, /* B3configuration */
  1657. NULL, /* BC */
  1658. NULL, /* LLC */
  1659. NULL, /* HLC */
  1660. /* BChannelinformation */
  1661. isleasedline ? AdditionalInfo : NULL,
  1662. NULL, /* Keypadfacility */
  1663. NULL, /* Useruserdata */
  1664. NULL /* Facilitydataarray */
  1665. );
  1666. if ((plcip = new_plci(card, (c->arg % card->nbchan))) == NULL) {
  1667. cmd.command = ISDN_STAT_DHUP;
  1668. cmd.driver = card->myid;
  1669. cmd.arg = (c->arg % card->nbchan);
  1670. card->interface.statcallb(&cmd);
  1671. return -1;
  1672. }
  1673. plcip->msgid = cmdcmsg.Messagenumber;
  1674. plcip->leasedline = isleasedline;
  1675. plci_change_state(card, plcip, EV_PLCI_CONNECT_REQ);
  1676. send_message(card, &cmdcmsg);
  1677. return 0;
  1678. }
  1679. case ISDN_CMD_ACCEPTD:
  1680. bchan = &card->bchans[c->arg % card->nbchan];
  1681. if (debugmode)
  1682. printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTD(ch=%ld) l2=%d l3=%d\n",
  1683. card->contrnr,
  1684. c->arg, bchan->l2, bchan->l3);
  1685. capi_fill_CONNECT_RESP(&cmdcmsg,
  1686. global.ap.applid,
  1687. card->msgid++,
  1688. bchan->plcip->plci, /* adr */
  1689. 0, /* Reject */
  1690. b1prot(bchan->l2, bchan->l3), /* B1protocol */
  1691. b2prot(bchan->l2, bchan->l3), /* B2protocol */
  1692. b3prot(bchan->l2, bchan->l3), /* B3protocol */
  1693. b1config(bchan->l2, bchan->l3), /* B1configuration */
  1694. NULL, /* B2configuration */
  1695. NULL, /* B3configuration */
  1696. NULL, /* ConnectedNumber */
  1697. NULL, /* ConnectedSubaddress */
  1698. NULL, /* LLC */
  1699. NULL, /* BChannelinformation */
  1700. NULL, /* Keypadfacility */
  1701. NULL, /* Useruserdata */
  1702. NULL /* Facilitydataarray */
  1703. );
  1704. capi_cmsg2message(&cmdcmsg, cmdcmsg.buf);
  1705. plci_change_state(card, bchan->plcip, EV_PLCI_CONNECT_RESP);
  1706. send_message(card, &cmdcmsg);
  1707. return 0;
  1708. case ISDN_CMD_ACCEPTB:
  1709. if (debugmode)
  1710. printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_ACCEPTB(ch=%ld)\n",
  1711. card->contrnr,
  1712. c->arg);
  1713. return -ENOSYS;
  1714. case ISDN_CMD_HANGUP:
  1715. if (debugmode)
  1716. printk(KERN_DEBUG "capidrv-%d: ISDN_CMD_HANGUP(ch=%ld)\n",
  1717. card->contrnr,
  1718. c->arg);
  1719. bchan = &card->bchans[c->arg % card->nbchan];
  1720. if (bchan->disconnecting) {
  1721. if (debugmode)
  1722. printk(KERN_DEBUG "capidrv-%d: chan %ld already disconnecting ...\n",
  1723. card->contrnr,
  1724. c->arg);
  1725. return 0;
  1726. }
  1727. if (bchan->nccip) {
  1728. bchan->disconnecting = 1;
  1729. capi_fill_DISCONNECT_B3_REQ(&cmdcmsg,
  1730. global.ap.applid,
  1731. card->msgid++,
  1732. bchan->nccip->ncci,
  1733. NULL /* NCPI */
  1734. );
  1735. ncci_change_state(card, bchan->nccip, EV_NCCI_DISCONNECT_B3_REQ);
  1736. send_message(card, &cmdcmsg);
  1737. return 0;
  1738. } else if (bchan->plcip) {
  1739. if (bchan->plcip->state == ST_PLCI_INCOMING) {
  1740. /*
  1741. * just ignore, we a called from
  1742. * isdn_status_callback(),
  1743. * which will return 0 or 2, this is handled
  1744. * by the CONNECT_IND handler
  1745. */
  1746. bchan->disconnecting = 1;
  1747. return 0;
  1748. } else if (bchan->plcip->plci) {
  1749. bchan->disconnecting = 1;
  1750. capi_fill_DISCONNECT_REQ(&cmdcmsg,
  1751. global.ap.applid,
  1752. card->msgid++,
  1753. bchan->plcip->plci,
  1754. NULL, /* BChannelinformation */
  1755. NULL, /* Keypadfacility */
  1756. NULL, /* Useruserdata */
  1757. NULL /* Facilitydataarray */
  1758. );
  1759. plci_change_state(card, bchan->plcip, EV_PLCI_DISCONNECT_REQ);
  1760. send_message(card, &cmdcmsg);
  1761. return 0;
  1762. } else {
  1763. printk(KERN_ERR "capidrv-%d: chan %ld disconnect request while waiting for CONNECT_CONF\n",
  1764. card->contrnr,
  1765. c->arg);
  1766. return -EINVAL;
  1767. }
  1768. }
  1769. printk(KERN_ERR "capidrv-%d: chan %ld disconnect request on free channel\n",
  1770. card->contrnr,
  1771. c->arg);
  1772. return -EINVAL;
  1773. /* ready */
  1774. case ISDN_CMD_SETL2:
  1775. if (debugmode)
  1776. printk(KERN_DEBUG "capidrv-%d: set L2 on chan %ld to %ld\n",
  1777. card->contrnr,
  1778. (c->arg & 0xff), (c->arg >> 8));
  1779. bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
  1780. bchan->l2 = (c->arg >> 8);
  1781. return 0;
  1782. case ISDN_CMD_SETL3:
  1783. if (debugmode)
  1784. printk(KERN_DEBUG "capidrv-%d: set L3 on chan %ld to %ld\n",
  1785. card->contrnr,
  1786. (c->arg & 0xff), (c->arg >> 8));
  1787. bchan = &card->bchans[(c->arg & 0xff) % card->nbchan];
  1788. bchan->l3 = (c->arg >> 8);
  1789. return 0;
  1790. case ISDN_CMD_SETEAZ:
  1791. if (debugmode)
  1792. printk(KERN_DEBUG "capidrv-%d: set EAZ \"%s\" on chan %ld\n",
  1793. card->contrnr,
  1794. c->parm.num, c->arg);
  1795. bchan = &card->bchans[c->arg % card->nbchan];
  1796. strncpy(bchan->msn, c->parm.num, ISDN_MSNLEN);
  1797. return 0;
  1798. case ISDN_CMD_CLREAZ:
  1799. if (debugmode)
  1800. printk(KERN_DEBUG "capidrv-%d: clearing EAZ on chan %ld\n",
  1801. card->contrnr, c->arg);
  1802. bchan = &card->bchans[c->arg % card->nbchan];
  1803. bchan->msn[0] = 0;
  1804. return 0;
  1805. default:
  1806. printk(KERN_ERR "capidrv-%d: ISDN_CMD_%d, Huh?\n",
  1807. card->contrnr, c->command);
  1808. return -EINVAL;
  1809. }
  1810. return 0;
  1811. }
  1812. static int if_command(isdn_ctrl *c)
  1813. {
  1814. capidrv_contr *card = findcontrbydriverid(c->driver);
  1815. if (card)
  1816. return capidrv_command(c, card);
  1817. printk(KERN_ERR
  1818. "capidrv: if_command %d called with invalid driverId %d!\n",
  1819. c->command, c->driver);
  1820. return -ENODEV;
  1821. }
  1822. static _cmsg sendcmsg;
  1823. static int if_sendbuf(int id, int channel, int doack, struct sk_buff *skb)
  1824. {
  1825. capidrv_contr *card = findcontrbydriverid(id);
  1826. capidrv_bchan *bchan;
  1827. capidrv_ncci *nccip;
  1828. int len = skb->len;
  1829. int msglen;
  1830. u16 errcode;
  1831. u16 datahandle;
  1832. u32 data;
  1833. if (!card) {
  1834. printk(KERN_ERR "capidrv: if_sendbuf called with invalid driverId %d!\n",
  1835. id);
  1836. return 0;
  1837. }
  1838. if (debugmode > 4)
  1839. printk(KERN_DEBUG "capidrv-%d: sendbuf len=%d skb=%p doack=%d\n",
  1840. card->contrnr, len, skb, doack);
  1841. bchan = &card->bchans[channel % card->nbchan];
  1842. nccip = bchan->nccip;
  1843. if (!nccip || nccip->state != ST_NCCI_ACTIVE) {
  1844. printk(KERN_ERR "capidrv-%d: if_sendbuf: %s:%d: chan not up!\n",
  1845. card->contrnr, card->name, channel);
  1846. return 0;
  1847. }
  1848. datahandle = nccip->datahandle;
  1849. /*
  1850. * Here we copy pointer skb->data into the 32-bit 'Data' field.
  1851. * The 'Data' field is not used in practice in linux kernel
  1852. * (neither in 32 or 64 bit), but should have some value,
  1853. * since a CAPI message trace will display it.
  1854. *
  1855. * The correct value in the 32 bit case is the address of the
  1856. * data, in 64 bit it makes no sense, we use 0 there.
  1857. */
  1858. #ifdef CONFIG_64BIT
  1859. data = 0;
  1860. #else
  1861. data = (unsigned long) skb->data;
  1862. #endif
  1863. capi_fill_DATA_B3_REQ(&sendcmsg, global.ap.applid, card->msgid++,
  1864. nccip->ncci, /* adr */
  1865. data, /* Data */
  1866. skb->len, /* DataLength */
  1867. datahandle, /* DataHandle */
  1868. 0 /* Flags */
  1869. );
  1870. if (capidrv_add_ack(nccip, datahandle, doack ? (int)skb->len : -1) < 0)
  1871. return 0;
  1872. capi_cmsg2message(&sendcmsg, sendcmsg.buf);
  1873. msglen = CAPIMSG_LEN(sendcmsg.buf);
  1874. if (skb_headroom(skb) < msglen) {
  1875. struct sk_buff *nskb = skb_realloc_headroom(skb, msglen);
  1876. if (!nskb) {
  1877. printk(KERN_ERR "capidrv-%d: if_sendbuf: no memory\n",
  1878. card->contrnr);
  1879. (void)capidrv_del_ack(nccip, datahandle);
  1880. return 0;
  1881. }
  1882. printk(KERN_DEBUG "capidrv-%d: only %d bytes headroom, need %d\n",
  1883. card->contrnr, skb_headroom(skb), msglen);
  1884. memcpy(skb_push(nskb, msglen), sendcmsg.buf, msglen);
  1885. errcode = capi20_put_message(&global.ap, nskb);
  1886. if (errcode == CAPI_NOERROR) {
  1887. dev_kfree_skb(skb);
  1888. nccip->datahandle++;
  1889. return len;
  1890. }
  1891. if (debugmode > 3)
  1892. printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
  1893. card->contrnr, errcode, capi_info2str(errcode));
  1894. (void)capidrv_del_ack(nccip, datahandle);
  1895. dev_kfree_skb(nskb);
  1896. return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
  1897. } else {
  1898. memcpy(skb_push(skb, msglen), sendcmsg.buf, msglen);
  1899. errcode = capi20_put_message(&global.ap, skb);
  1900. if (errcode == CAPI_NOERROR) {
  1901. nccip->datahandle++;
  1902. return len;
  1903. }
  1904. if (debugmode > 3)
  1905. printk(KERN_DEBUG "capidrv-%d: sendbuf putmsg ret(%x) - %s\n",
  1906. card->contrnr, errcode, capi_info2str(errcode));
  1907. skb_pull(skb, msglen);
  1908. (void)capidrv_del_ack(nccip, datahandle);
  1909. return errcode == CAPI_SENDQUEUEFULL ? 0 : -1;
  1910. }
  1911. }
  1912. static int if_readstat(u8 __user *buf, int len, int id, int channel)
  1913. {
  1914. capidrv_contr *card = findcontrbydriverid(id);
  1915. int count;
  1916. u8 __user *p;
  1917. if (!card) {
  1918. printk(KERN_ERR "capidrv: if_readstat called with invalid driverId %d!\n",
  1919. id);
  1920. return -ENODEV;
  1921. }
  1922. for (p = buf, count = 0; count < len; p++, count++) {
  1923. if (put_user(*card->q931_read++, p))
  1924. return -EFAULT;
  1925. if (card->q931_read > card->q931_end)
  1926. card->q931_read = card->q931_buf;
  1927. }
  1928. return count;
  1929. }
  1930. static void enable_dchannel_trace(capidrv_contr *card)
  1931. {
  1932. u8 manufacturer[CAPI_MANUFACTURER_LEN];
  1933. capi_version version;
  1934. u16 contr = card->contrnr;
  1935. u16 errcode;
  1936. u16 avmversion[3];
  1937. errcode = capi20_get_manufacturer(contr, manufacturer);
  1938. if (errcode != CAPI_NOERROR) {
  1939. printk(KERN_ERR "%s: can't get manufacturer (0x%x)\n",
  1940. card->name, errcode);
  1941. return;
  1942. }
  1943. if (strstr(manufacturer, "AVM") == NULL) {
  1944. printk(KERN_ERR "%s: not from AVM, no d-channel trace possible (%s)\n",
  1945. card->name, manufacturer);
  1946. return;
  1947. }
  1948. errcode = capi20_get_version(contr, &version);
  1949. if (errcode != CAPI_NOERROR) {
  1950. printk(KERN_ERR "%s: can't get version (0x%x)\n",
  1951. card->name, errcode);
  1952. return;
  1953. }
  1954. avmversion[0] = (version.majormanuversion >> 4) & 0x0f;
  1955. avmversion[1] = (version.majormanuversion << 4) & 0xf0;
  1956. avmversion[1] |= (version.minormanuversion >> 4) & 0x0f;
  1957. avmversion[2] |= version.minormanuversion & 0x0f;
  1958. if (avmversion[0] > 3 || (avmversion[0] == 3 && avmversion[1] > 5)) {
  1959. printk(KERN_INFO "%s: D2 trace enabled\n", card->name);
  1960. capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
  1961. card->msgid++,
  1962. contr,
  1963. 0x214D5641, /* ManuID */
  1964. 0, /* Class */
  1965. 1, /* Function */
  1966. (_cstruct)"\004\200\014\000\000");
  1967. } else {
  1968. printk(KERN_INFO "%s: D3 trace enabled\n", card->name);
  1969. capi_fill_MANUFACTURER_REQ(&cmdcmsg, global.ap.applid,
  1970. card->msgid++,
  1971. contr,
  1972. 0x214D5641, /* ManuID */
  1973. 0, /* Class */
  1974. 1, /* Function */
  1975. (_cstruct)"\004\002\003\000\000");
  1976. }
  1977. send_message(card, &cmdcmsg);
  1978. }
  1979. static void send_listen(capidrv_contr *card)
  1980. {
  1981. capi_fill_LISTEN_REQ(&cmdcmsg, global.ap.applid,
  1982. card->msgid++,
  1983. card->contrnr, /* controller */
  1984. 1 << 6, /* Infomask */
  1985. card->cipmask,
  1986. card->cipmask2,
  1987. NULL, NULL);
  1988. listen_change_state(card, EV_LISTEN_REQ);
  1989. send_message(card, &cmdcmsg);
  1990. }
  1991. static void listentimerfunc(unsigned long x)
  1992. {
  1993. capidrv_contr *card = (capidrv_contr *)x;
  1994. if (card->state != ST_LISTEN_NONE && card->state != ST_LISTEN_ACTIVE)
  1995. printk(KERN_ERR "%s: controller dead ??\n", card->name);
  1996. send_listen(card);
  1997. mod_timer(&card->listentimer, jiffies + 60 * HZ);
  1998. }
  1999. static int capidrv_addcontr(u16 contr, struct capi_profile *profp)
  2000. {
  2001. capidrv_contr *card;
  2002. unsigned long flags;
  2003. isdn_ctrl cmd;
  2004. char id[20];
  2005. int i;
  2006. sprintf(id, "capidrv-%d", contr);
  2007. if (!try_module_get(THIS_MODULE)) {
  2008. printk(KERN_WARNING "capidrv: (%s) Could not reserve module\n", id);
  2009. return -1;
  2010. }
  2011. if (!(card = kzalloc(sizeof(capidrv_contr), GFP_ATOMIC))) {
  2012. printk(KERN_WARNING
  2013. "capidrv: (%s) Could not allocate contr-struct.\n", id);
  2014. return -1;
  2015. }
  2016. card->owner = THIS_MODULE;
  2017. init_timer(&card->listentimer);
  2018. strcpy(card->name, id);
  2019. card->contrnr = contr;
  2020. card->nbchan = profp->nbchannel;
  2021. card->bchans = kmalloc(sizeof(capidrv_bchan) * card->nbchan, GFP_ATOMIC);
  2022. if (!card->bchans) {
  2023. printk(KERN_WARNING
  2024. "capidrv: (%s) Could not allocate bchan-structs.\n", id);
  2025. module_put(card->owner);
  2026. kfree(card);
  2027. return -1;
  2028. }
  2029. card->interface.channels = profp->nbchannel;
  2030. card->interface.maxbufsize = 2048;
  2031. card->interface.command = if_command;
  2032. card->interface.writebuf_skb = if_sendbuf;
  2033. card->interface.writecmd = NULL;
  2034. card->interface.readstat = if_readstat;
  2035. card->interface.features =
  2036. ISDN_FEATURE_L2_HDLC |
  2037. ISDN_FEATURE_L2_TRANS |
  2038. ISDN_FEATURE_L3_TRANS |
  2039. ISDN_FEATURE_P_UNKNOWN |
  2040. ISDN_FEATURE_L2_X75I |
  2041. ISDN_FEATURE_L2_X75UI |
  2042. ISDN_FEATURE_L2_X75BUI;
  2043. if (profp->support1 & (1 << 2))
  2044. card->interface.features |=
  2045. ISDN_FEATURE_L2_V11096 |
  2046. ISDN_FEATURE_L2_V11019 |
  2047. ISDN_FEATURE_L2_V11038;
  2048. if (profp->support1 & (1 << 8))
  2049. card->interface.features |= ISDN_FEATURE_L2_MODEM;
  2050. card->interface.hl_hdrlen = 22; /* len of DATA_B3_REQ */
  2051. strncpy(card->interface.id, id, sizeof(card->interface.id) - 1);
  2052. card->q931_read = card->q931_buf;
  2053. card->q931_write = card->q931_buf;
  2054. card->q931_end = card->q931_buf + sizeof(card->q931_buf) - 1;
  2055. if (!register_isdn(&card->interface)) {
  2056. printk(KERN_ERR "capidrv: Unable to register contr %s\n", id);
  2057. kfree(card->bchans);
  2058. module_put(card->owner);
  2059. kfree(card);
  2060. return -1;
  2061. }
  2062. card->myid = card->interface.channels;
  2063. memset(card->bchans, 0, sizeof(capidrv_bchan) * card->nbchan);
  2064. for (i = 0; i < card->nbchan; i++) {
  2065. card->bchans[i].contr = card;
  2066. }
  2067. spin_lock_irqsave(&global_lock, flags);
  2068. card->next = global.contr_list;
  2069. global.contr_list = card;
  2070. global.ncontr++;
  2071. spin_unlock_irqrestore(&global_lock, flags);
  2072. cmd.command = ISDN_STAT_RUN;
  2073. cmd.driver = card->myid;
  2074. card->interface.statcallb(&cmd);
  2075. card->cipmask = 0x1FFF03FF; /* any */
  2076. card->cipmask2 = 0;
  2077. card->listentimer.data = (unsigned long)card;
  2078. card->listentimer.function = listentimerfunc;
  2079. send_listen(card);
  2080. mod_timer(&card->listentimer, jiffies + 60 * HZ);
  2081. printk(KERN_INFO "%s: now up (%d B channels)\n",
  2082. card->name, card->nbchan);
  2083. enable_dchannel_trace(card);
  2084. return 0;
  2085. }
  2086. static int capidrv_delcontr(u16 contr)
  2087. {
  2088. capidrv_contr **pp, *card;
  2089. unsigned long flags;
  2090. isdn_ctrl cmd;
  2091. spin_lock_irqsave(&global_lock, flags);
  2092. for (card = global.contr_list; card; card = card->next) {
  2093. if (card->contrnr == contr)
  2094. break;
  2095. }
  2096. if (!card) {
  2097. spin_unlock_irqrestore(&global_lock, flags);
  2098. printk(KERN_ERR "capidrv: delcontr: no contr %u\n", contr);
  2099. return -1;
  2100. }
  2101. /* FIXME: maybe a race condition the card should be removed
  2102. * here from global list /kkeil
  2103. */
  2104. spin_unlock_irqrestore(&global_lock, flags);
  2105. del_timer(&card->listentimer);
  2106. if (debugmode)
  2107. printk(KERN_DEBUG "capidrv-%d: id=%d unloading\n",
  2108. card->contrnr, card->myid);
  2109. cmd.command = ISDN_STAT_STOP;
  2110. cmd.driver = card->myid;
  2111. card->interface.statcallb(&cmd);
  2112. while (card->nbchan) {
  2113. cmd.command = ISDN_STAT_DISCH;
  2114. cmd.driver = card->myid;
  2115. cmd.arg = card->nbchan - 1;
  2116. cmd.parm.num[0] = 0;
  2117. if (debugmode)
  2118. printk(KERN_DEBUG "capidrv-%d: id=%d disable chan=%ld\n",
  2119. card->contrnr, card->myid, cmd.arg);
  2120. card->interface.statcallb(&cmd);
  2121. if (card->bchans[card->nbchan - 1].nccip)
  2122. free_ncci(card, card->bchans[card->nbchan - 1].nccip);
  2123. if (card->bchans[card->nbchan - 1].plcip)
  2124. free_plci(card, card->bchans[card->nbchan - 1].plcip);
  2125. if (card->plci_list)
  2126. printk(KERN_ERR "capidrv: bug in free_plci()\n");
  2127. card->nbchan--;
  2128. }
  2129. kfree(card->bchans);
  2130. card->bchans = NULL;
  2131. if (debugmode)
  2132. printk(KERN_DEBUG "capidrv-%d: id=%d isdn unload\n",
  2133. card->contrnr, card->myid);
  2134. cmd.command = ISDN_STAT_UNLOAD;
  2135. cmd.driver = card->myid;
  2136. card->interface.statcallb(&cmd);
  2137. if (debugmode)
  2138. printk(KERN_DEBUG "capidrv-%d: id=%d remove contr from list\n",
  2139. card->contrnr, card->myid);
  2140. spin_lock_irqsave(&global_lock, flags);
  2141. for (pp = &global.contr_list; *pp; pp = &(*pp)->next) {
  2142. if (*pp == card) {
  2143. *pp = (*pp)->next;
  2144. card->next = NULL;
  2145. global.ncontr--;
  2146. break;
  2147. }
  2148. }
  2149. spin_unlock_irqrestore(&global_lock, flags);
  2150. module_put(card->owner);
  2151. printk(KERN_INFO "%s: now down.\n", card->name);
  2152. kfree(card);
  2153. return 0;
  2154. }
  2155. static int
  2156. lower_callback(struct notifier_block *nb, unsigned long val, void *v)
  2157. {
  2158. capi_profile profile;
  2159. u32 contr = (long)v;
  2160. switch (val) {
  2161. case CAPICTR_UP:
  2162. printk(KERN_INFO "capidrv: controller %hu up\n", contr);
  2163. if (capi20_get_profile(contr, &profile) == CAPI_NOERROR)
  2164. (void) capidrv_addcontr(contr, &profile);
  2165. break;
  2166. case CAPICTR_DOWN:
  2167. printk(KERN_INFO "capidrv: controller %hu down\n", contr);
  2168. (void) capidrv_delcontr(contr);
  2169. break;
  2170. }
  2171. return NOTIFY_OK;
  2172. }
  2173. /*
  2174. * /proc/capi/capidrv:
  2175. * nrecvctlpkt nrecvdatapkt nsendctlpkt nsenddatapkt
  2176. */
  2177. static int capidrv_proc_show(struct seq_file *m, void *v)
  2178. {
  2179. seq_printf(m, "%lu %lu %lu %lu\n",
  2180. global.ap.nrecvctlpkt,
  2181. global.ap.nrecvdatapkt,
  2182. global.ap.nsentctlpkt,
  2183. global.ap.nsentdatapkt);
  2184. return 0;
  2185. }
  2186. static int capidrv_proc_open(struct inode *inode, struct file *file)
  2187. {
  2188. return single_open(file, capidrv_proc_show, NULL);
  2189. }
  2190. static const struct file_operations capidrv_proc_fops = {
  2191. .owner = THIS_MODULE,
  2192. .open = capidrv_proc_open,
  2193. .read = seq_read,
  2194. .llseek = seq_lseek,
  2195. .release = single_release,
  2196. };
  2197. static void __init proc_init(void)
  2198. {
  2199. proc_create("capi/capidrv", 0, NULL, &capidrv_proc_fops);
  2200. }
  2201. static void __exit proc_exit(void)
  2202. {
  2203. remove_proc_entry("capi/capidrv", NULL);
  2204. }
  2205. static struct notifier_block capictr_nb = {
  2206. .notifier_call = lower_callback,
  2207. };
  2208. static int __init capidrv_init(void)
  2209. {
  2210. capi_profile profile;
  2211. u32 ncontr, contr;
  2212. u16 errcode;
  2213. global.ap.rparam.level3cnt = -2; /* number of bchannels twice */
  2214. global.ap.rparam.datablkcnt = 16;
  2215. global.ap.rparam.datablklen = 2048;
  2216. global.ap.recv_message = capidrv_recv_message;
  2217. errcode = capi20_register(&global.ap);
  2218. if (errcode) {
  2219. return -EIO;
  2220. }
  2221. register_capictr_notifier(&capictr_nb);
  2222. errcode = capi20_get_profile(0, &profile);
  2223. if (errcode != CAPI_NOERROR) {
  2224. unregister_capictr_notifier(&capictr_nb);
  2225. capi20_release(&global.ap);
  2226. return -EIO;
  2227. }
  2228. ncontr = profile.ncontroller;
  2229. for (contr = 1; contr <= ncontr; contr++) {
  2230. errcode = capi20_get_profile(contr, &profile);
  2231. if (errcode != CAPI_NOERROR)
  2232. continue;
  2233. (void) capidrv_addcontr(contr, &profile);
  2234. }
  2235. proc_init();
  2236. return 0;
  2237. }
  2238. static void __exit capidrv_exit(void)
  2239. {
  2240. unregister_capictr_notifier(&capictr_nb);
  2241. capi20_release(&global.ap);
  2242. proc_exit();
  2243. }
  2244. module_init(capidrv_init);
  2245. module_exit(capidrv_exit);