capidrv.c 63 KB

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