capidrv.c 69 KB

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