capidrv.c 70 KB

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