capi.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408
  1. /*
  2. * Kernel CAPI interface for the Gigaset driver
  3. *
  4. * Copyright (c) 2009 by Tilman Schmidt <tilman@imap.cc>.
  5. *
  6. * =====================================================================
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. * =====================================================================
  12. */
  13. #include "gigaset.h"
  14. #include <linux/proc_fs.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/isdn/capilli.h>
  17. #include <linux/isdn/capicmd.h>
  18. #include <linux/isdn/capiutil.h>
  19. /* missing from kernelcapi.h */
  20. #define CapiNcpiNotSupportedByProtocol 0x0001
  21. #define CapiFlagsNotSupportedByProtocol 0x0002
  22. #define CapiAlertAlreadySent 0x0003
  23. #define CapiFacilitySpecificFunctionNotSupported 0x3011
  24. /* missing from capicmd.h */
  25. #define CAPI_CONNECT_IND_BASELEN (CAPI_MSG_BASELEN+4+2+8*1)
  26. #define CAPI_CONNECT_ACTIVE_IND_BASELEN (CAPI_MSG_BASELEN+4+3*1)
  27. #define CAPI_CONNECT_B3_IND_BASELEN (CAPI_MSG_BASELEN+4+1)
  28. #define CAPI_CONNECT_B3_ACTIVE_IND_BASELEN (CAPI_MSG_BASELEN+4+1)
  29. #define CAPI_DATA_B3_REQ_LEN64 (CAPI_MSG_BASELEN+4+4+2+2+2+8)
  30. #define CAPI_DATA_B3_CONF_LEN (CAPI_MSG_BASELEN+4+2+2)
  31. #define CAPI_DISCONNECT_IND_LEN (CAPI_MSG_BASELEN+4+2)
  32. #define CAPI_DISCONNECT_B3_IND_BASELEN (CAPI_MSG_BASELEN+4+2+1)
  33. #define CAPI_FACILITY_CONF_BASELEN (CAPI_MSG_BASELEN+4+2+2+1)
  34. /* most _CONF messages contain only Controller/PLCI/NCCI and Info parameters */
  35. #define CAPI_STDCONF_LEN (CAPI_MSG_BASELEN+4+2)
  36. #define CAPI_FACILITY_HANDSET 0x0000
  37. #define CAPI_FACILITY_DTMF 0x0001
  38. #define CAPI_FACILITY_V42BIS 0x0002
  39. #define CAPI_FACILITY_SUPPSVC 0x0003
  40. #define CAPI_FACILITY_WAKEUP 0x0004
  41. #define CAPI_FACILITY_LI 0x0005
  42. #define CAPI_SUPPSVC_GETSUPPORTED 0x0000
  43. /* missing from capiutil.h */
  44. #define CAPIMSG_PLCI_PART(m) CAPIMSG_U8(m, 9)
  45. #define CAPIMSG_NCCI_PART(m) CAPIMSG_U16(m, 10)
  46. #define CAPIMSG_HANDLE_REQ(m) CAPIMSG_U16(m, 18) /* DATA_B3_REQ/_IND only! */
  47. #define CAPIMSG_FLAGS(m) CAPIMSG_U16(m, 20)
  48. #define CAPIMSG_SETCONTROLLER(m, contr) capimsg_setu8(m, 8, contr)
  49. #define CAPIMSG_SETPLCI_PART(m, plci) capimsg_setu8(m, 9, plci)
  50. #define CAPIMSG_SETNCCI_PART(m, ncci) capimsg_setu16(m, 10, ncci)
  51. #define CAPIMSG_SETFLAGS(m, flags) capimsg_setu16(m, 20, flags)
  52. /* parameters with differing location in DATA_B3_CONF/_RESP: */
  53. #define CAPIMSG_SETHANDLE_CONF(m, handle) capimsg_setu16(m, 12, handle)
  54. #define CAPIMSG_SETINFO_CONF(m, info) capimsg_setu16(m, 14, info)
  55. /* Flags (DATA_B3_REQ/_IND) */
  56. #define CAPI_FLAGS_DELIVERY_CONFIRMATION 0x04
  57. #define CAPI_FLAGS_RESERVED (~0x1f)
  58. /* buffer sizes */
  59. #define MAX_BC_OCTETS 11
  60. #define MAX_HLC_OCTETS 3
  61. #define MAX_NUMBER_DIGITS 20
  62. #define MAX_FMT_IE_LEN 20
  63. /* values for bcs->apconnstate */
  64. #define APCONN_NONE 0 /* inactive/listening */
  65. #define APCONN_SETUP 1 /* connecting */
  66. #define APCONN_ACTIVE 2 /* B channel up */
  67. /* registered application data structure */
  68. struct gigaset_capi_appl {
  69. struct list_head ctrlist;
  70. struct gigaset_capi_appl *bcnext;
  71. u16 id;
  72. struct capi_register_params rp;
  73. u16 nextMessageNumber;
  74. u32 listenInfoMask;
  75. u32 listenCIPmask;
  76. };
  77. /* CAPI specific controller data structure */
  78. struct gigaset_capi_ctr {
  79. struct capi_ctr ctr;
  80. struct list_head appls;
  81. struct sk_buff_head sendqueue;
  82. atomic_t sendqlen;
  83. /* two _cmsg structures possibly used concurrently: */
  84. _cmsg hcmsg; /* for message composition triggered from hardware */
  85. _cmsg acmsg; /* for dissection of messages sent from application */
  86. u8 bc_buf[MAX_BC_OCTETS+1];
  87. u8 hlc_buf[MAX_HLC_OCTETS+1];
  88. u8 cgpty_buf[MAX_NUMBER_DIGITS+3];
  89. u8 cdpty_buf[MAX_NUMBER_DIGITS+2];
  90. };
  91. /* CIP Value table (from CAPI 2.0 standard, ch. 6.1) */
  92. static struct {
  93. u8 *bc;
  94. u8 *hlc;
  95. } cip2bchlc[] = {
  96. [1] = { "8090A3", NULL },
  97. /* Speech (A-law) */
  98. [2] = { "8890", NULL },
  99. /* Unrestricted digital information */
  100. [3] = { "8990", NULL },
  101. /* Restricted digital information */
  102. [4] = { "9090A3", NULL },
  103. /* 3,1 kHz audio (A-law) */
  104. [5] = { "9190", NULL },
  105. /* 7 kHz audio */
  106. [6] = { "9890", NULL },
  107. /* Video */
  108. [7] = { "88C0C6E6", NULL },
  109. /* Packet mode */
  110. [8] = { "8890218F", NULL },
  111. /* 56 kbit/s rate adaptation */
  112. [9] = { "9190A5", NULL },
  113. /* Unrestricted digital information with tones/announcements */
  114. [16] = { "8090A3", "9181" },
  115. /* Telephony */
  116. [17] = { "9090A3", "9184" },
  117. /* Group 2/3 facsimile */
  118. [18] = { "8890", "91A1" },
  119. /* Group 4 facsimile Class 1 */
  120. [19] = { "8890", "91A4" },
  121. /* Teletex service basic and mixed mode
  122. and Group 4 facsimile service Classes II and III */
  123. [20] = { "8890", "91A8" },
  124. /* Teletex service basic and processable mode */
  125. [21] = { "8890", "91B1" },
  126. /* Teletex service basic mode */
  127. [22] = { "8890", "91B2" },
  128. /* International interworking for Videotex */
  129. [23] = { "8890", "91B5" },
  130. /* Telex */
  131. [24] = { "8890", "91B8" },
  132. /* Message Handling Systems in accordance with X.400 */
  133. [25] = { "8890", "91C1" },
  134. /* OSI application in accordance with X.200 */
  135. [26] = { "9190A5", "9181" },
  136. /* 7 kHz telephony */
  137. [27] = { "9190A5", "916001" },
  138. /* Video telephony, first connection */
  139. [28] = { "8890", "916002" },
  140. /* Video telephony, second connection */
  141. };
  142. /*
  143. * helper functions
  144. * ================
  145. */
  146. /*
  147. * emit unsupported parameter warning
  148. */
  149. static inline void ignore_cstruct_param(struct cardstate *cs, _cstruct param,
  150. char *msgname, char *paramname)
  151. {
  152. if (param && *param)
  153. dev_warn(cs->dev, "%s: ignoring unsupported parameter: %s\n",
  154. msgname, paramname);
  155. }
  156. /*
  157. * convert an IE from Gigaset hex string to ETSI binary representation
  158. * including length byte
  159. * return value: result length, -1 on error
  160. */
  161. static int encode_ie(char *in, u8 *out, int maxlen)
  162. {
  163. int l = 0;
  164. while (*in) {
  165. if (!isxdigit(in[0]) || !isxdigit(in[1]) || l >= maxlen)
  166. return -1;
  167. out[++l] = (hex_to_bin(in[0]) << 4) + hex_to_bin(in[1]);
  168. in += 2;
  169. }
  170. out[0] = l;
  171. return l;
  172. }
  173. /*
  174. * convert an IE from ETSI binary representation including length byte
  175. * to Gigaset hex string
  176. */
  177. static void decode_ie(u8 *in, char *out)
  178. {
  179. int i = *in;
  180. while (i-- > 0) {
  181. /* ToDo: conversion to upper case necessary? */
  182. *out++ = toupper(hex_asc_hi(*++in));
  183. *out++ = toupper(hex_asc_lo(*in));
  184. }
  185. }
  186. /*
  187. * retrieve application data structure for an application ID
  188. */
  189. static inline struct gigaset_capi_appl *
  190. get_appl(struct gigaset_capi_ctr *iif, u16 appl)
  191. {
  192. struct gigaset_capi_appl *ap;
  193. list_for_each_entry(ap, &iif->appls, ctrlist)
  194. if (ap->id == appl)
  195. return ap;
  196. return NULL;
  197. }
  198. /*
  199. * dump CAPI message to kernel messages for debugging
  200. */
  201. static inline void dump_cmsg(enum debuglevel level, const char *tag, _cmsg *p)
  202. {
  203. #ifdef CONFIG_GIGASET_DEBUG
  204. _cdebbuf *cdb;
  205. if (!(gigaset_debuglevel & level))
  206. return;
  207. cdb = capi_cmsg2str(p);
  208. if (cdb) {
  209. gig_dbg(level, "%s: [%d] %s", tag, p->ApplId, cdb->buf);
  210. cdebbuf_free(cdb);
  211. } else {
  212. gig_dbg(level, "%s: [%d] %s", tag, p->ApplId,
  213. capi_cmd2str(p->Command, p->Subcommand));
  214. }
  215. #endif
  216. }
  217. static inline void dump_rawmsg(enum debuglevel level, const char *tag,
  218. unsigned char *data)
  219. {
  220. #ifdef CONFIG_GIGASET_DEBUG
  221. char *dbgline;
  222. int i, l;
  223. if (!(gigaset_debuglevel & level))
  224. return;
  225. l = CAPIMSG_LEN(data);
  226. if (l < 12) {
  227. gig_dbg(level, "%s: ??? LEN=%04d", tag, l);
  228. return;
  229. }
  230. gig_dbg(level, "%s: 0x%02x:0x%02x: ID=%03d #0x%04x LEN=%04d NCCI=0x%x",
  231. tag, CAPIMSG_COMMAND(data), CAPIMSG_SUBCOMMAND(data),
  232. CAPIMSG_APPID(data), CAPIMSG_MSGID(data), l,
  233. CAPIMSG_CONTROL(data));
  234. l -= 12;
  235. dbgline = kmalloc(3*l, GFP_ATOMIC);
  236. if (!dbgline)
  237. return;
  238. for (i = 0; i < l; i++) {
  239. dbgline[3*i] = hex_asc_hi(data[12+i]);
  240. dbgline[3*i+1] = hex_asc_lo(data[12+i]);
  241. dbgline[3*i+2] = ' ';
  242. }
  243. dbgline[3*l-1] = '\0';
  244. gig_dbg(level, " %s", dbgline);
  245. kfree(dbgline);
  246. if (CAPIMSG_COMMAND(data) == CAPI_DATA_B3 &&
  247. (CAPIMSG_SUBCOMMAND(data) == CAPI_REQ ||
  248. CAPIMSG_SUBCOMMAND(data) == CAPI_IND)) {
  249. l = CAPIMSG_DATALEN(data);
  250. gig_dbg(level, " DataLength=%d", l);
  251. if (l <= 0 || !(gigaset_debuglevel & DEBUG_LLDATA))
  252. return;
  253. if (l > 64)
  254. l = 64; /* arbitrary limit */
  255. dbgline = kmalloc(3*l, GFP_ATOMIC);
  256. if (!dbgline)
  257. return;
  258. data += CAPIMSG_LEN(data);
  259. for (i = 0; i < l; i++) {
  260. dbgline[3*i] = hex_asc_hi(data[i]);
  261. dbgline[3*i+1] = hex_asc_lo(data[i]);
  262. dbgline[3*i+2] = ' ';
  263. }
  264. dbgline[3*l-1] = '\0';
  265. gig_dbg(level, " %s", dbgline);
  266. kfree(dbgline);
  267. }
  268. #endif
  269. }
  270. /*
  271. * format CAPI IE as string
  272. */
  273. static const char *format_ie(const char *ie)
  274. {
  275. static char result[3*MAX_FMT_IE_LEN];
  276. int len, count;
  277. char *pout = result;
  278. if (!ie)
  279. return "NULL";
  280. count = len = ie[0];
  281. if (count > MAX_FMT_IE_LEN)
  282. count = MAX_FMT_IE_LEN-1;
  283. while (count--) {
  284. *pout++ = hex_asc_hi(*++ie);
  285. *pout++ = hex_asc_lo(*ie);
  286. *pout++ = ' ';
  287. }
  288. if (len > MAX_FMT_IE_LEN) {
  289. *pout++ = '.';
  290. *pout++ = '.';
  291. *pout++ = '.';
  292. }
  293. *--pout = 0;
  294. return result;
  295. }
  296. /*
  297. * emit DATA_B3_CONF message
  298. */
  299. static void send_data_b3_conf(struct cardstate *cs, struct capi_ctr *ctr,
  300. u16 appl, u16 msgid, int channel,
  301. u16 handle, u16 info)
  302. {
  303. struct sk_buff *cskb;
  304. u8 *msg;
  305. cskb = alloc_skb(CAPI_DATA_B3_CONF_LEN, GFP_ATOMIC);
  306. if (!cskb) {
  307. dev_err(cs->dev, "%s: out of memory\n", __func__);
  308. return;
  309. }
  310. /* frequent message, avoid _cmsg overhead */
  311. msg = __skb_put(cskb, CAPI_DATA_B3_CONF_LEN);
  312. CAPIMSG_SETLEN(msg, CAPI_DATA_B3_CONF_LEN);
  313. CAPIMSG_SETAPPID(msg, appl);
  314. CAPIMSG_SETCOMMAND(msg, CAPI_DATA_B3);
  315. CAPIMSG_SETSUBCOMMAND(msg, CAPI_CONF);
  316. CAPIMSG_SETMSGID(msg, msgid);
  317. CAPIMSG_SETCONTROLLER(msg, ctr->cnr);
  318. CAPIMSG_SETPLCI_PART(msg, channel);
  319. CAPIMSG_SETNCCI_PART(msg, 1);
  320. CAPIMSG_SETHANDLE_CONF(msg, handle);
  321. CAPIMSG_SETINFO_CONF(msg, info);
  322. /* emit message */
  323. dump_rawmsg(DEBUG_MCMD, __func__, msg);
  324. capi_ctr_handle_message(ctr, appl, cskb);
  325. }
  326. /*
  327. * driver interface functions
  328. * ==========================
  329. */
  330. /**
  331. * gigaset_skb_sent() - acknowledge transmission of outgoing skb
  332. * @bcs: B channel descriptor structure.
  333. * @skb: sent data.
  334. *
  335. * Called by hardware module {bas,ser,usb}_gigaset when the data in a
  336. * skb has been successfully sent, for signalling completion to the LL.
  337. */
  338. void gigaset_skb_sent(struct bc_state *bcs, struct sk_buff *dskb)
  339. {
  340. struct cardstate *cs = bcs->cs;
  341. struct gigaset_capi_ctr *iif = cs->iif;
  342. struct gigaset_capi_appl *ap = bcs->ap;
  343. unsigned char *req = skb_mac_header(dskb);
  344. u16 flags;
  345. /* update statistics */
  346. ++bcs->trans_up;
  347. if (!ap) {
  348. dev_err(cs->dev, "%s: no application\n", __func__);
  349. return;
  350. }
  351. /* don't send further B3 messages if disconnected */
  352. if (bcs->apconnstate < APCONN_ACTIVE) {
  353. gig_dbg(DEBUG_MCMD, "disconnected, discarding ack");
  354. return;
  355. }
  356. /*
  357. * send DATA_B3_CONF if "delivery confirmation" bit was set in request;
  358. * otherwise it has already been sent by do_data_b3_req()
  359. */
  360. flags = CAPIMSG_FLAGS(req);
  361. if (flags & CAPI_FLAGS_DELIVERY_CONFIRMATION)
  362. send_data_b3_conf(cs, &iif->ctr, ap->id, CAPIMSG_MSGID(req),
  363. bcs->channel + 1, CAPIMSG_HANDLE_REQ(req),
  364. (flags & ~CAPI_FLAGS_DELIVERY_CONFIRMATION) ?
  365. CapiFlagsNotSupportedByProtocol :
  366. CAPI_NOERROR);
  367. }
  368. EXPORT_SYMBOL_GPL(gigaset_skb_sent);
  369. /**
  370. * gigaset_skb_rcvd() - pass received skb to LL
  371. * @bcs: B channel descriptor structure.
  372. * @skb: received data.
  373. *
  374. * Called by hardware module {bas,ser,usb}_gigaset when user data has
  375. * been successfully received, for passing to the LL.
  376. * Warning: skb must not be accessed anymore!
  377. */
  378. void gigaset_skb_rcvd(struct bc_state *bcs, struct sk_buff *skb)
  379. {
  380. struct cardstate *cs = bcs->cs;
  381. struct gigaset_capi_ctr *iif = cs->iif;
  382. struct gigaset_capi_appl *ap = bcs->ap;
  383. int len = skb->len;
  384. /* update statistics */
  385. bcs->trans_down++;
  386. if (!ap) {
  387. dev_err(cs->dev, "%s: no application\n", __func__);
  388. return;
  389. }
  390. /* don't send further B3 messages if disconnected */
  391. if (bcs->apconnstate < APCONN_ACTIVE) {
  392. gig_dbg(DEBUG_MCMD, "disconnected, discarding data");
  393. dev_kfree_skb_any(skb);
  394. return;
  395. }
  396. /*
  397. * prepend DATA_B3_IND message to payload
  398. * Parameters: NCCI = 1, all others 0/unused
  399. * frequent message, avoid _cmsg overhead
  400. */
  401. skb_push(skb, CAPI_DATA_B3_REQ_LEN);
  402. CAPIMSG_SETLEN(skb->data, CAPI_DATA_B3_REQ_LEN);
  403. CAPIMSG_SETAPPID(skb->data, ap->id);
  404. CAPIMSG_SETCOMMAND(skb->data, CAPI_DATA_B3);
  405. CAPIMSG_SETSUBCOMMAND(skb->data, CAPI_IND);
  406. CAPIMSG_SETMSGID(skb->data, ap->nextMessageNumber++);
  407. CAPIMSG_SETCONTROLLER(skb->data, iif->ctr.cnr);
  408. CAPIMSG_SETPLCI_PART(skb->data, bcs->channel + 1);
  409. CAPIMSG_SETNCCI_PART(skb->data, 1);
  410. /* Data parameter not used */
  411. CAPIMSG_SETDATALEN(skb->data, len);
  412. /* Data handle parameter not used */
  413. CAPIMSG_SETFLAGS(skb->data, 0);
  414. /* Data64 parameter not present */
  415. /* emit message */
  416. dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
  417. capi_ctr_handle_message(&iif->ctr, ap->id, skb);
  418. }
  419. EXPORT_SYMBOL_GPL(gigaset_skb_rcvd);
  420. /**
  421. * gigaset_isdn_rcv_err() - signal receive error
  422. * @bcs: B channel descriptor structure.
  423. *
  424. * Called by hardware module {bas,ser,usb}_gigaset when a receive error
  425. * has occurred, for signalling to the LL.
  426. */
  427. void gigaset_isdn_rcv_err(struct bc_state *bcs)
  428. {
  429. /* if currently ignoring packets, just count down */
  430. if (bcs->ignore) {
  431. bcs->ignore--;
  432. return;
  433. }
  434. /* update statistics */
  435. bcs->corrupted++;
  436. /* ToDo: signal error -> LL */
  437. }
  438. EXPORT_SYMBOL_GPL(gigaset_isdn_rcv_err);
  439. /**
  440. * gigaset_isdn_icall() - signal incoming call
  441. * @at_state: connection state structure.
  442. *
  443. * Called by main module at tasklet level to notify the LL that an incoming
  444. * call has been received. @at_state contains the parameters of the call.
  445. *
  446. * Return value: call disposition (ICALL_*)
  447. */
  448. int gigaset_isdn_icall(struct at_state_t *at_state)
  449. {
  450. struct cardstate *cs = at_state->cs;
  451. struct bc_state *bcs = at_state->bcs;
  452. struct gigaset_capi_ctr *iif = cs->iif;
  453. struct gigaset_capi_appl *ap;
  454. u32 actCIPmask;
  455. struct sk_buff *skb;
  456. unsigned int msgsize;
  457. unsigned long flags;
  458. int i;
  459. /*
  460. * ToDo: signal calls without a free B channel, too
  461. * (requires a u8 handle for the at_state structure that can
  462. * be stored in the PLCI and used in the CONNECT_RESP message
  463. * handler to retrieve it)
  464. */
  465. if (!bcs)
  466. return ICALL_IGNORE;
  467. /* prepare CONNECT_IND message, using B channel number as PLCI */
  468. capi_cmsg_header(&iif->hcmsg, 0, CAPI_CONNECT, CAPI_IND, 0,
  469. iif->ctr.cnr | ((bcs->channel + 1) << 8));
  470. /* minimum size, all structs empty */
  471. msgsize = CAPI_CONNECT_IND_BASELEN;
  472. /* Bearer Capability (mandatory) */
  473. if (at_state->str_var[STR_ZBC]) {
  474. /* pass on BC from Gigaset */
  475. if (encode_ie(at_state->str_var[STR_ZBC], iif->bc_buf,
  476. MAX_BC_OCTETS) < 0) {
  477. dev_warn(cs->dev, "RING ignored - bad BC %s\n",
  478. at_state->str_var[STR_ZBC]);
  479. return ICALL_IGNORE;
  480. }
  481. /* look up corresponding CIP value */
  482. iif->hcmsg.CIPValue = 0; /* default if nothing found */
  483. for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
  484. if (cip2bchlc[i].bc != NULL &&
  485. cip2bchlc[i].hlc == NULL &&
  486. !strcmp(cip2bchlc[i].bc,
  487. at_state->str_var[STR_ZBC])) {
  488. iif->hcmsg.CIPValue = i;
  489. break;
  490. }
  491. } else {
  492. /* no BC (internal call): assume CIP 1 (speech, A-law) */
  493. iif->hcmsg.CIPValue = 1;
  494. encode_ie(cip2bchlc[1].bc, iif->bc_buf, MAX_BC_OCTETS);
  495. }
  496. iif->hcmsg.BC = iif->bc_buf;
  497. msgsize += iif->hcmsg.BC[0];
  498. /* High Layer Compatibility (optional) */
  499. if (at_state->str_var[STR_ZHLC]) {
  500. /* pass on HLC from Gigaset */
  501. if (encode_ie(at_state->str_var[STR_ZHLC], iif->hlc_buf,
  502. MAX_HLC_OCTETS) < 0) {
  503. dev_warn(cs->dev, "RING ignored - bad HLC %s\n",
  504. at_state->str_var[STR_ZHLC]);
  505. return ICALL_IGNORE;
  506. }
  507. iif->hcmsg.HLC = iif->hlc_buf;
  508. msgsize += iif->hcmsg.HLC[0];
  509. /* look up corresponding CIP value */
  510. /* keep BC based CIP value if none found */
  511. if (at_state->str_var[STR_ZBC])
  512. for (i = 0; i < ARRAY_SIZE(cip2bchlc); i++)
  513. if (cip2bchlc[i].hlc != NULL &&
  514. !strcmp(cip2bchlc[i].hlc,
  515. at_state->str_var[STR_ZHLC]) &&
  516. !strcmp(cip2bchlc[i].bc,
  517. at_state->str_var[STR_ZBC])) {
  518. iif->hcmsg.CIPValue = i;
  519. break;
  520. }
  521. }
  522. /* Called Party Number (optional) */
  523. if (at_state->str_var[STR_ZCPN]) {
  524. i = strlen(at_state->str_var[STR_ZCPN]);
  525. if (i > MAX_NUMBER_DIGITS) {
  526. dev_warn(cs->dev, "RING ignored - bad number %s\n",
  527. at_state->str_var[STR_ZBC]);
  528. return ICALL_IGNORE;
  529. }
  530. iif->cdpty_buf[0] = i + 1;
  531. iif->cdpty_buf[1] = 0x80; /* type / numbering plan unknown */
  532. memcpy(iif->cdpty_buf+2, at_state->str_var[STR_ZCPN], i);
  533. iif->hcmsg.CalledPartyNumber = iif->cdpty_buf;
  534. msgsize += iif->hcmsg.CalledPartyNumber[0];
  535. }
  536. /* Calling Party Number (optional) */
  537. if (at_state->str_var[STR_NMBR]) {
  538. i = strlen(at_state->str_var[STR_NMBR]);
  539. if (i > MAX_NUMBER_DIGITS) {
  540. dev_warn(cs->dev, "RING ignored - bad number %s\n",
  541. at_state->str_var[STR_ZBC]);
  542. return ICALL_IGNORE;
  543. }
  544. iif->cgpty_buf[0] = i + 2;
  545. iif->cgpty_buf[1] = 0x00; /* type / numbering plan unknown */
  546. iif->cgpty_buf[2] = 0x80; /* pres. allowed, not screened */
  547. memcpy(iif->cgpty_buf+3, at_state->str_var[STR_NMBR], i);
  548. iif->hcmsg.CallingPartyNumber = iif->cgpty_buf;
  549. msgsize += iif->hcmsg.CallingPartyNumber[0];
  550. }
  551. /* remaining parameters (not supported, always left NULL):
  552. * - CalledPartySubaddress
  553. * - CallingPartySubaddress
  554. * - AdditionalInfo
  555. * - BChannelinformation
  556. * - Keypadfacility
  557. * - Useruserdata
  558. * - Facilitydataarray
  559. */
  560. gig_dbg(DEBUG_CMD, "icall: PLCI %x CIP %d BC %s",
  561. iif->hcmsg.adr.adrPLCI, iif->hcmsg.CIPValue,
  562. format_ie(iif->hcmsg.BC));
  563. gig_dbg(DEBUG_CMD, "icall: HLC %s",
  564. format_ie(iif->hcmsg.HLC));
  565. gig_dbg(DEBUG_CMD, "icall: CgPty %s",
  566. format_ie(iif->hcmsg.CallingPartyNumber));
  567. gig_dbg(DEBUG_CMD, "icall: CdPty %s",
  568. format_ie(iif->hcmsg.CalledPartyNumber));
  569. /* scan application list for matching listeners */
  570. spin_lock_irqsave(&bcs->aplock, flags);
  571. if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE) {
  572. dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
  573. __func__, bcs->ap, bcs->apconnstate);
  574. bcs->ap = NULL;
  575. bcs->apconnstate = APCONN_NONE;
  576. }
  577. spin_unlock_irqrestore(&bcs->aplock, flags);
  578. actCIPmask = 1 | (1 << iif->hcmsg.CIPValue);
  579. list_for_each_entry(ap, &iif->appls, ctrlist)
  580. if (actCIPmask & ap->listenCIPmask) {
  581. /* build CONNECT_IND message for this application */
  582. iif->hcmsg.ApplId = ap->id;
  583. iif->hcmsg.Messagenumber = ap->nextMessageNumber++;
  584. skb = alloc_skb(msgsize, GFP_ATOMIC);
  585. if (!skb) {
  586. dev_err(cs->dev, "%s: out of memory\n",
  587. __func__);
  588. break;
  589. }
  590. capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
  591. dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
  592. /* add to listeners on this B channel, update state */
  593. spin_lock_irqsave(&bcs->aplock, flags);
  594. ap->bcnext = bcs->ap;
  595. bcs->ap = ap;
  596. bcs->chstate |= CHS_NOTIFY_LL;
  597. bcs->apconnstate = APCONN_SETUP;
  598. spin_unlock_irqrestore(&bcs->aplock, flags);
  599. /* emit message */
  600. capi_ctr_handle_message(&iif->ctr, ap->id, skb);
  601. }
  602. /*
  603. * Return "accept" if any listeners.
  604. * Gigaset will send ALERTING.
  605. * There doesn't seem to be a way to avoid this.
  606. */
  607. return bcs->ap ? ICALL_ACCEPT : ICALL_IGNORE;
  608. }
  609. /*
  610. * send a DISCONNECT_IND message to an application
  611. * does not sleep, clobbers the controller's hcmsg structure
  612. */
  613. static void send_disconnect_ind(struct bc_state *bcs,
  614. struct gigaset_capi_appl *ap, u16 reason)
  615. {
  616. struct cardstate *cs = bcs->cs;
  617. struct gigaset_capi_ctr *iif = cs->iif;
  618. struct sk_buff *skb;
  619. if (bcs->apconnstate == APCONN_NONE)
  620. return;
  621. capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT, CAPI_IND,
  622. ap->nextMessageNumber++,
  623. iif->ctr.cnr | ((bcs->channel + 1) << 8));
  624. iif->hcmsg.Reason = reason;
  625. skb = alloc_skb(CAPI_DISCONNECT_IND_LEN, GFP_ATOMIC);
  626. if (!skb) {
  627. dev_err(cs->dev, "%s: out of memory\n", __func__);
  628. return;
  629. }
  630. capi_cmsg2message(&iif->hcmsg, __skb_put(skb, CAPI_DISCONNECT_IND_LEN));
  631. dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
  632. capi_ctr_handle_message(&iif->ctr, ap->id, skb);
  633. }
  634. /*
  635. * send a DISCONNECT_B3_IND message to an application
  636. * Parameters: NCCI = 1, NCPI empty, Reason_B3 = 0
  637. * does not sleep, clobbers the controller's hcmsg structure
  638. */
  639. static void send_disconnect_b3_ind(struct bc_state *bcs,
  640. struct gigaset_capi_appl *ap)
  641. {
  642. struct cardstate *cs = bcs->cs;
  643. struct gigaset_capi_ctr *iif = cs->iif;
  644. struct sk_buff *skb;
  645. /* nothing to do if no logical connection active */
  646. if (bcs->apconnstate < APCONN_ACTIVE)
  647. return;
  648. bcs->apconnstate = APCONN_SETUP;
  649. capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
  650. ap->nextMessageNumber++,
  651. iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
  652. skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_ATOMIC);
  653. if (!skb) {
  654. dev_err(cs->dev, "%s: out of memory\n", __func__);
  655. return;
  656. }
  657. capi_cmsg2message(&iif->hcmsg,
  658. __skb_put(skb, CAPI_DISCONNECT_B3_IND_BASELEN));
  659. dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
  660. capi_ctr_handle_message(&iif->ctr, ap->id, skb);
  661. }
  662. /**
  663. * gigaset_isdn_connD() - signal D channel connect
  664. * @bcs: B channel descriptor structure.
  665. *
  666. * Called by main module at tasklet level to notify the LL that the D channel
  667. * connection has been established.
  668. */
  669. void gigaset_isdn_connD(struct bc_state *bcs)
  670. {
  671. struct cardstate *cs = bcs->cs;
  672. struct gigaset_capi_ctr *iif = cs->iif;
  673. struct gigaset_capi_appl *ap;
  674. struct sk_buff *skb;
  675. unsigned int msgsize;
  676. unsigned long flags;
  677. spin_lock_irqsave(&bcs->aplock, flags);
  678. ap = bcs->ap;
  679. if (!ap) {
  680. spin_unlock_irqrestore(&bcs->aplock, flags);
  681. dev_err(cs->dev, "%s: no application\n", __func__);
  682. return;
  683. }
  684. if (bcs->apconnstate == APCONN_NONE) {
  685. spin_unlock_irqrestore(&bcs->aplock, flags);
  686. dev_warn(cs->dev, "%s: application %u not connected\n",
  687. __func__, ap->id);
  688. return;
  689. }
  690. spin_unlock_irqrestore(&bcs->aplock, flags);
  691. while (ap->bcnext) {
  692. /* this should never happen */
  693. dev_warn(cs->dev, "%s: dropping extra application %u\n",
  694. __func__, ap->bcnext->id);
  695. send_disconnect_ind(bcs, ap->bcnext,
  696. CapiCallGivenToOtherApplication);
  697. ap->bcnext = ap->bcnext->bcnext;
  698. }
  699. /* prepare CONNECT_ACTIVE_IND message
  700. * Note: LLC not supported by device
  701. */
  702. capi_cmsg_header(&iif->hcmsg, ap->id, CAPI_CONNECT_ACTIVE, CAPI_IND,
  703. ap->nextMessageNumber++,
  704. iif->ctr.cnr | ((bcs->channel + 1) << 8));
  705. /* minimum size, all structs empty */
  706. msgsize = CAPI_CONNECT_ACTIVE_IND_BASELEN;
  707. /* ToDo: set parameter: Connected number
  708. * (requires ev-layer state machine extension to collect
  709. * ZCON device reply)
  710. */
  711. /* build and emit CONNECT_ACTIVE_IND message */
  712. skb = alloc_skb(msgsize, GFP_ATOMIC);
  713. if (!skb) {
  714. dev_err(cs->dev, "%s: out of memory\n", __func__);
  715. return;
  716. }
  717. capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
  718. dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
  719. capi_ctr_handle_message(&iif->ctr, ap->id, skb);
  720. }
  721. /**
  722. * gigaset_isdn_hupD() - signal D channel hangup
  723. * @bcs: B channel descriptor structure.
  724. *
  725. * Called by main module at tasklet level to notify the LL that the D channel
  726. * connection has been shut down.
  727. */
  728. void gigaset_isdn_hupD(struct bc_state *bcs)
  729. {
  730. struct gigaset_capi_appl *ap;
  731. unsigned long flags;
  732. /*
  733. * ToDo: pass on reason code reported by device
  734. * (requires ev-layer state machine extension to collect
  735. * ZCAU device reply)
  736. */
  737. spin_lock_irqsave(&bcs->aplock, flags);
  738. while (bcs->ap != NULL) {
  739. ap = bcs->ap;
  740. bcs->ap = ap->bcnext;
  741. spin_unlock_irqrestore(&bcs->aplock, flags);
  742. send_disconnect_b3_ind(bcs, ap);
  743. send_disconnect_ind(bcs, ap, 0);
  744. spin_lock_irqsave(&bcs->aplock, flags);
  745. }
  746. bcs->apconnstate = APCONN_NONE;
  747. spin_unlock_irqrestore(&bcs->aplock, flags);
  748. }
  749. /**
  750. * gigaset_isdn_connB() - signal B channel connect
  751. * @bcs: B channel descriptor structure.
  752. *
  753. * Called by main module at tasklet level to notify the LL that the B channel
  754. * connection has been established.
  755. */
  756. void gigaset_isdn_connB(struct bc_state *bcs)
  757. {
  758. struct cardstate *cs = bcs->cs;
  759. struct gigaset_capi_ctr *iif = cs->iif;
  760. struct gigaset_capi_appl *ap;
  761. struct sk_buff *skb;
  762. unsigned long flags;
  763. unsigned int msgsize;
  764. u8 command;
  765. spin_lock_irqsave(&bcs->aplock, flags);
  766. ap = bcs->ap;
  767. if (!ap) {
  768. spin_unlock_irqrestore(&bcs->aplock, flags);
  769. dev_err(cs->dev, "%s: no application\n", __func__);
  770. return;
  771. }
  772. if (!bcs->apconnstate) {
  773. spin_unlock_irqrestore(&bcs->aplock, flags);
  774. dev_warn(cs->dev, "%s: application %u not connected\n",
  775. __func__, ap->id);
  776. return;
  777. }
  778. /*
  779. * emit CONNECT_B3_ACTIVE_IND if we already got CONNECT_B3_REQ;
  780. * otherwise we have to emit CONNECT_B3_IND first, and follow up with
  781. * CONNECT_B3_ACTIVE_IND in reply to CONNECT_B3_RESP
  782. * Parameters in both cases always: NCCI = 1, NCPI empty
  783. */
  784. if (bcs->apconnstate >= APCONN_ACTIVE) {
  785. command = CAPI_CONNECT_B3_ACTIVE;
  786. msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
  787. } else {
  788. command = CAPI_CONNECT_B3;
  789. msgsize = CAPI_CONNECT_B3_IND_BASELEN;
  790. }
  791. bcs->apconnstate = APCONN_ACTIVE;
  792. spin_unlock_irqrestore(&bcs->aplock, flags);
  793. while (ap->bcnext) {
  794. /* this should never happen */
  795. dev_warn(cs->dev, "%s: dropping extra application %u\n",
  796. __func__, ap->bcnext->id);
  797. send_disconnect_ind(bcs, ap->bcnext,
  798. CapiCallGivenToOtherApplication);
  799. ap->bcnext = ap->bcnext->bcnext;
  800. }
  801. capi_cmsg_header(&iif->hcmsg, ap->id, command, CAPI_IND,
  802. ap->nextMessageNumber++,
  803. iif->ctr.cnr | ((bcs->channel + 1) << 8) | (1 << 16));
  804. skb = alloc_skb(msgsize, GFP_ATOMIC);
  805. if (!skb) {
  806. dev_err(cs->dev, "%s: out of memory\n", __func__);
  807. return;
  808. }
  809. capi_cmsg2message(&iif->hcmsg, __skb_put(skb, msgsize));
  810. dump_cmsg(DEBUG_CMD, __func__, &iif->hcmsg);
  811. capi_ctr_handle_message(&iif->ctr, ap->id, skb);
  812. }
  813. /**
  814. * gigaset_isdn_hupB() - signal B channel hangup
  815. * @bcs: B channel descriptor structure.
  816. *
  817. * Called by main module to notify the LL that the B channel connection has
  818. * been shut down.
  819. */
  820. void gigaset_isdn_hupB(struct bc_state *bcs)
  821. {
  822. struct cardstate *cs = bcs->cs;
  823. struct gigaset_capi_appl *ap = bcs->ap;
  824. /* ToDo: assure order of DISCONNECT_B3_IND and DISCONNECT_IND ? */
  825. if (!ap) {
  826. dev_err(cs->dev, "%s: no application\n", __func__);
  827. return;
  828. }
  829. send_disconnect_b3_ind(bcs, ap);
  830. }
  831. /**
  832. * gigaset_isdn_start() - signal device availability
  833. * @cs: device descriptor structure.
  834. *
  835. * Called by main module to notify the LL that the device is available for
  836. * use.
  837. */
  838. void gigaset_isdn_start(struct cardstate *cs)
  839. {
  840. struct gigaset_capi_ctr *iif = cs->iif;
  841. /* fill profile data: manufacturer name */
  842. strcpy(iif->ctr.manu, "Siemens");
  843. /* CAPI and device version */
  844. iif->ctr.version.majorversion = 2; /* CAPI 2.0 */
  845. iif->ctr.version.minorversion = 0;
  846. /* ToDo: check/assert cs->gotfwver? */
  847. iif->ctr.version.majormanuversion = cs->fwver[0];
  848. iif->ctr.version.minormanuversion = cs->fwver[1];
  849. /* number of B channels supported */
  850. iif->ctr.profile.nbchannel = cs->channels;
  851. /* global options: internal controller, supplementary services */
  852. iif->ctr.profile.goptions = 0x11;
  853. /* B1 protocols: 64 kbit/s HDLC or transparent */
  854. iif->ctr.profile.support1 = 0x03;
  855. /* B2 protocols: transparent only */
  856. /* ToDo: X.75 SLP ? */
  857. iif->ctr.profile.support2 = 0x02;
  858. /* B3 protocols: transparent only */
  859. iif->ctr.profile.support3 = 0x01;
  860. /* no serial number */
  861. strcpy(iif->ctr.serial, "0");
  862. capi_ctr_ready(&iif->ctr);
  863. }
  864. /**
  865. * gigaset_isdn_stop() - signal device unavailability
  866. * @cs: device descriptor structure.
  867. *
  868. * Called by main module to notify the LL that the device is no longer
  869. * available for use.
  870. */
  871. void gigaset_isdn_stop(struct cardstate *cs)
  872. {
  873. struct gigaset_capi_ctr *iif = cs->iif;
  874. capi_ctr_down(&iif->ctr);
  875. }
  876. /*
  877. * kernel CAPI callback methods
  878. * ============================
  879. */
  880. /*
  881. * register CAPI application
  882. */
  883. static void gigaset_register_appl(struct capi_ctr *ctr, u16 appl,
  884. capi_register_params *rp)
  885. {
  886. struct gigaset_capi_ctr *iif
  887. = container_of(ctr, struct gigaset_capi_ctr, ctr);
  888. struct cardstate *cs = ctr->driverdata;
  889. struct gigaset_capi_appl *ap;
  890. gig_dbg(DEBUG_CMD, "%s [%u] l3cnt=%u blkcnt=%u blklen=%u",
  891. __func__, appl, rp->level3cnt, rp->datablkcnt, rp->datablklen);
  892. list_for_each_entry(ap, &iif->appls, ctrlist)
  893. if (ap->id == appl) {
  894. dev_notice(cs->dev,
  895. "application %u already registered\n", appl);
  896. return;
  897. }
  898. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  899. if (!ap) {
  900. dev_err(cs->dev, "%s: out of memory\n", __func__);
  901. return;
  902. }
  903. ap->id = appl;
  904. ap->rp = *rp;
  905. list_add(&ap->ctrlist, &iif->appls);
  906. dev_info(cs->dev, "application %u registered\n", ap->id);
  907. }
  908. /*
  909. * remove CAPI application from channel
  910. * helper function to keep indentation levels down and stay in 80 columns
  911. */
  912. static inline void remove_appl_from_channel(struct bc_state *bcs,
  913. struct gigaset_capi_appl *ap)
  914. {
  915. struct cardstate *cs = bcs->cs;
  916. struct gigaset_capi_appl *bcap;
  917. unsigned long flags;
  918. int prevconnstate;
  919. spin_lock_irqsave(&bcs->aplock, flags);
  920. bcap = bcs->ap;
  921. if (bcap == NULL) {
  922. spin_unlock_irqrestore(&bcs->aplock, flags);
  923. return;
  924. }
  925. /* check first application on channel */
  926. if (bcap == ap) {
  927. bcs->ap = ap->bcnext;
  928. if (bcs->ap != NULL) {
  929. spin_unlock_irqrestore(&bcs->aplock, flags);
  930. return;
  931. }
  932. /* none left, clear channel state */
  933. prevconnstate = bcs->apconnstate;
  934. bcs->apconnstate = APCONN_NONE;
  935. spin_unlock_irqrestore(&bcs->aplock, flags);
  936. if (prevconnstate == APCONN_ACTIVE) {
  937. dev_notice(cs->dev, "%s: hanging up channel %u\n",
  938. __func__, bcs->channel);
  939. gigaset_add_event(cs, &bcs->at_state,
  940. EV_HUP, NULL, 0, NULL);
  941. gigaset_schedule_event(cs);
  942. }
  943. return;
  944. }
  945. /* check remaining list */
  946. do {
  947. if (bcap->bcnext == ap) {
  948. bcap->bcnext = bcap->bcnext->bcnext;
  949. return;
  950. }
  951. bcap = bcap->bcnext;
  952. } while (bcap != NULL);
  953. spin_unlock_irqrestore(&bcs->aplock, flags);
  954. }
  955. /*
  956. * release CAPI application
  957. */
  958. static void gigaset_release_appl(struct capi_ctr *ctr, u16 appl)
  959. {
  960. struct gigaset_capi_ctr *iif
  961. = container_of(ctr, struct gigaset_capi_ctr, ctr);
  962. struct cardstate *cs = iif->ctr.driverdata;
  963. struct gigaset_capi_appl *ap, *tmp;
  964. unsigned ch;
  965. gig_dbg(DEBUG_CMD, "%s [%u]", __func__, appl);
  966. list_for_each_entry_safe(ap, tmp, &iif->appls, ctrlist)
  967. if (ap->id == appl) {
  968. /* remove from any channels */
  969. for (ch = 0; ch < cs->channels; ch++)
  970. remove_appl_from_channel(&cs->bcs[ch], ap);
  971. /* remove from registration list */
  972. list_del(&ap->ctrlist);
  973. kfree(ap);
  974. dev_info(cs->dev, "application %u released\n", appl);
  975. }
  976. }
  977. /*
  978. * =====================================================================
  979. * outgoing CAPI message handler
  980. * =====================================================================
  981. */
  982. /*
  983. * helper function: emit reply message with given Info value
  984. */
  985. static void send_conf(struct gigaset_capi_ctr *iif,
  986. struct gigaset_capi_appl *ap,
  987. struct sk_buff *skb,
  988. u16 info)
  989. {
  990. /*
  991. * _CONF replies always only have NCCI and Info parameters
  992. * so they'll fit into the _REQ message skb
  993. */
  994. capi_cmsg_answer(&iif->acmsg);
  995. iif->acmsg.Info = info;
  996. capi_cmsg2message(&iif->acmsg, skb->data);
  997. __skb_trim(skb, CAPI_STDCONF_LEN);
  998. dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
  999. capi_ctr_handle_message(&iif->ctr, ap->id, skb);
  1000. }
  1001. /*
  1002. * process FACILITY_REQ message
  1003. */
  1004. static void do_facility_req(struct gigaset_capi_ctr *iif,
  1005. struct gigaset_capi_appl *ap,
  1006. struct sk_buff *skb)
  1007. {
  1008. struct cardstate *cs = iif->ctr.driverdata;
  1009. _cmsg *cmsg = &iif->acmsg;
  1010. struct sk_buff *cskb;
  1011. u8 *pparam;
  1012. unsigned int msgsize = CAPI_FACILITY_CONF_BASELEN;
  1013. u16 function, info;
  1014. static u8 confparam[10]; /* max. 9 octets + length byte */
  1015. /* decode message */
  1016. capi_message2cmsg(cmsg, skb->data);
  1017. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1018. /*
  1019. * Facility Request Parameter is not decoded by capi_message2cmsg()
  1020. * encoding depends on Facility Selector
  1021. */
  1022. switch (cmsg->FacilitySelector) {
  1023. case CAPI_FACILITY_DTMF: /* ToDo */
  1024. info = CapiFacilityNotSupported;
  1025. confparam[0] = 2; /* length */
  1026. /* DTMF information: Unknown DTMF request */
  1027. capimsg_setu16(confparam, 1, 2);
  1028. break;
  1029. case CAPI_FACILITY_V42BIS: /* not supported */
  1030. info = CapiFacilityNotSupported;
  1031. confparam[0] = 2; /* length */
  1032. /* V.42 bis information: not available */
  1033. capimsg_setu16(confparam, 1, 1);
  1034. break;
  1035. case CAPI_FACILITY_SUPPSVC:
  1036. /* decode Function parameter */
  1037. pparam = cmsg->FacilityRequestParameter;
  1038. if (pparam == NULL || *pparam < 2) {
  1039. dev_notice(cs->dev, "%s: %s missing\n", "FACILITY_REQ",
  1040. "Facility Request Parameter");
  1041. send_conf(iif, ap, skb, CapiIllMessageParmCoding);
  1042. return;
  1043. }
  1044. function = CAPIMSG_U16(pparam, 1);
  1045. switch (function) {
  1046. case CAPI_SUPPSVC_GETSUPPORTED:
  1047. info = CapiSuccess;
  1048. /* Supplementary Service specific parameter */
  1049. confparam[3] = 6; /* length */
  1050. /* Supplementary services info: Success */
  1051. capimsg_setu16(confparam, 4, CapiSuccess);
  1052. /* Supported Services: none */
  1053. capimsg_setu32(confparam, 6, 0);
  1054. break;
  1055. /* ToDo: add supported services */
  1056. default:
  1057. info = CapiFacilitySpecificFunctionNotSupported;
  1058. /* Supplementary Service specific parameter */
  1059. confparam[3] = 2; /* length */
  1060. /* Supplementary services info: not supported */
  1061. capimsg_setu16(confparam, 4,
  1062. CapiSupplementaryServiceNotSupported);
  1063. }
  1064. /* Facility confirmation parameter */
  1065. confparam[0] = confparam[3] + 3; /* total length */
  1066. /* Function: copy from _REQ message */
  1067. capimsg_setu16(confparam, 1, function);
  1068. /* Supplementary Service specific parameter already set above */
  1069. break;
  1070. case CAPI_FACILITY_WAKEUP: /* ToDo */
  1071. info = CapiFacilityNotSupported;
  1072. confparam[0] = 2; /* length */
  1073. /* Number of accepted awake request parameters: 0 */
  1074. capimsg_setu16(confparam, 1, 0);
  1075. break;
  1076. default:
  1077. info = CapiFacilityNotSupported;
  1078. confparam[0] = 0; /* empty struct */
  1079. }
  1080. /* send FACILITY_CONF with given Info and confirmation parameter */
  1081. capi_cmsg_answer(cmsg);
  1082. cmsg->Info = info;
  1083. cmsg->FacilityConfirmationParameter = confparam;
  1084. msgsize += confparam[0]; /* length */
  1085. cskb = alloc_skb(msgsize, GFP_ATOMIC);
  1086. if (!cskb) {
  1087. dev_err(cs->dev, "%s: out of memory\n", __func__);
  1088. return;
  1089. }
  1090. capi_cmsg2message(cmsg, __skb_put(cskb, msgsize));
  1091. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1092. capi_ctr_handle_message(&iif->ctr, ap->id, cskb);
  1093. }
  1094. /*
  1095. * process LISTEN_REQ message
  1096. * just store the masks in the application data structure
  1097. */
  1098. static void do_listen_req(struct gigaset_capi_ctr *iif,
  1099. struct gigaset_capi_appl *ap,
  1100. struct sk_buff *skb)
  1101. {
  1102. /* decode message */
  1103. capi_message2cmsg(&iif->acmsg, skb->data);
  1104. dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
  1105. /* store listening parameters */
  1106. ap->listenInfoMask = iif->acmsg.InfoMask;
  1107. ap->listenCIPmask = iif->acmsg.CIPmask;
  1108. send_conf(iif, ap, skb, CapiSuccess);
  1109. }
  1110. /*
  1111. * process ALERT_REQ message
  1112. * nothing to do, Gigaset always alerts anyway
  1113. */
  1114. static void do_alert_req(struct gigaset_capi_ctr *iif,
  1115. struct gigaset_capi_appl *ap,
  1116. struct sk_buff *skb)
  1117. {
  1118. /* decode message */
  1119. capi_message2cmsg(&iif->acmsg, skb->data);
  1120. dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
  1121. send_conf(iif, ap, skb, CapiAlertAlreadySent);
  1122. }
  1123. /*
  1124. * process CONNECT_REQ message
  1125. * allocate a B channel, prepare dial commands, queue a DIAL event,
  1126. * emit CONNECT_CONF reply
  1127. */
  1128. static void do_connect_req(struct gigaset_capi_ctr *iif,
  1129. struct gigaset_capi_appl *ap,
  1130. struct sk_buff *skb)
  1131. {
  1132. struct cardstate *cs = iif->ctr.driverdata;
  1133. _cmsg *cmsg = &iif->acmsg;
  1134. struct bc_state *bcs;
  1135. char **commands;
  1136. char *s;
  1137. u8 *pp;
  1138. unsigned long flags;
  1139. int i, l, lbc, lhlc;
  1140. u16 info;
  1141. /* decode message */
  1142. capi_message2cmsg(cmsg, skb->data);
  1143. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1144. /* get free B channel & construct PLCI */
  1145. bcs = gigaset_get_free_channel(cs);
  1146. if (!bcs) {
  1147. dev_notice(cs->dev, "%s: no B channel available\n",
  1148. "CONNECT_REQ");
  1149. send_conf(iif, ap, skb, CapiNoPlciAvailable);
  1150. return;
  1151. }
  1152. spin_lock_irqsave(&bcs->aplock, flags);
  1153. if (bcs->ap != NULL || bcs->apconnstate != APCONN_NONE)
  1154. dev_warn(cs->dev, "%s: channel not properly cleared (%p/%d)\n",
  1155. __func__, bcs->ap, bcs->apconnstate);
  1156. ap->bcnext = NULL;
  1157. bcs->ap = ap;
  1158. bcs->apconnstate = APCONN_SETUP;
  1159. spin_unlock_irqrestore(&bcs->aplock, flags);
  1160. bcs->rx_bufsize = ap->rp.datablklen;
  1161. dev_kfree_skb(bcs->rx_skb);
  1162. gigaset_new_rx_skb(bcs);
  1163. cmsg->adr.adrPLCI |= (bcs->channel + 1) << 8;
  1164. /* build command table */
  1165. commands = kzalloc(AT_NUM*(sizeof *commands), GFP_KERNEL);
  1166. if (!commands)
  1167. goto oom;
  1168. /* encode parameter: Called party number */
  1169. pp = cmsg->CalledPartyNumber;
  1170. if (pp == NULL || *pp == 0) {
  1171. dev_notice(cs->dev, "%s: %s missing\n",
  1172. "CONNECT_REQ", "Called party number");
  1173. info = CapiIllMessageParmCoding;
  1174. goto error;
  1175. }
  1176. l = *pp++;
  1177. /* check type of number/numbering plan byte */
  1178. switch (*pp) {
  1179. case 0x80: /* unknown type / unknown numbering plan */
  1180. case 0x81: /* unknown type / ISDN/Telephony numbering plan */
  1181. break;
  1182. default: /* others: warn about potential misinterpretation */
  1183. dev_notice(cs->dev, "%s: %s type/plan 0x%02x unsupported\n",
  1184. "CONNECT_REQ", "Called party number", *pp);
  1185. }
  1186. pp++;
  1187. l--;
  1188. /* translate "**" internal call prefix to CTP value */
  1189. if (l >= 2 && pp[0] == '*' && pp[1] == '*') {
  1190. s = "^SCTP=0\r";
  1191. pp += 2;
  1192. l -= 2;
  1193. } else {
  1194. s = "^SCTP=1\r";
  1195. }
  1196. commands[AT_TYPE] = kstrdup(s, GFP_KERNEL);
  1197. if (!commands[AT_TYPE])
  1198. goto oom;
  1199. commands[AT_DIAL] = kmalloc(l+3, GFP_KERNEL);
  1200. if (!commands[AT_DIAL])
  1201. goto oom;
  1202. snprintf(commands[AT_DIAL], l+3, "D%.*s\r", l, pp);
  1203. /* encode parameter: Calling party number */
  1204. pp = cmsg->CallingPartyNumber;
  1205. if (pp != NULL && *pp > 0) {
  1206. l = *pp++;
  1207. /* check type of number/numbering plan byte */
  1208. /* ToDo: allow for/handle Ext=1? */
  1209. switch (*pp) {
  1210. case 0x00: /* unknown type / unknown numbering plan */
  1211. case 0x01: /* unknown type / ISDN/Telephony num. plan */
  1212. break;
  1213. default:
  1214. dev_notice(cs->dev,
  1215. "%s: %s type/plan 0x%02x unsupported\n",
  1216. "CONNECT_REQ", "Calling party number", *pp);
  1217. }
  1218. pp++;
  1219. l--;
  1220. /* check presentation indicator */
  1221. if (!l) {
  1222. dev_notice(cs->dev, "%s: %s IE truncated\n",
  1223. "CONNECT_REQ", "Calling party number");
  1224. info = CapiIllMessageParmCoding;
  1225. goto error;
  1226. }
  1227. switch (*pp & 0xfc) { /* ignore Screening indicator */
  1228. case 0x80: /* Presentation allowed */
  1229. s = "^SCLIP=1\r";
  1230. break;
  1231. case 0xa0: /* Presentation restricted */
  1232. s = "^SCLIP=0\r";
  1233. break;
  1234. default:
  1235. dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
  1236. "CONNECT_REQ",
  1237. "Presentation/Screening indicator",
  1238. *pp);
  1239. s = "^SCLIP=1\r";
  1240. }
  1241. commands[AT_CLIP] = kstrdup(s, GFP_KERNEL);
  1242. if (!commands[AT_CLIP])
  1243. goto oom;
  1244. pp++;
  1245. l--;
  1246. if (l) {
  1247. /* number */
  1248. commands[AT_MSN] = kmalloc(l+8, GFP_KERNEL);
  1249. if (!commands[AT_MSN])
  1250. goto oom;
  1251. snprintf(commands[AT_MSN], l+8, "^SMSN=%*s\r", l, pp);
  1252. }
  1253. }
  1254. /* check parameter: CIP Value */
  1255. if (cmsg->CIPValue >= ARRAY_SIZE(cip2bchlc) ||
  1256. (cmsg->CIPValue > 0 && cip2bchlc[cmsg->CIPValue].bc == NULL)) {
  1257. dev_notice(cs->dev, "%s: unknown CIP value %d\n",
  1258. "CONNECT_REQ", cmsg->CIPValue);
  1259. info = CapiCipValueUnknown;
  1260. goto error;
  1261. }
  1262. /*
  1263. * check/encode parameters: BC & HLC
  1264. * must be encoded together as device doesn't accept HLC separately
  1265. * explicit parameters override values derived from CIP
  1266. */
  1267. /* determine lengths */
  1268. if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
  1269. lbc = 2*cmsg->BC[0];
  1270. else if (cip2bchlc[cmsg->CIPValue].bc) /* BC derived from CIP */
  1271. lbc = strlen(cip2bchlc[cmsg->CIPValue].bc);
  1272. else /* no BC */
  1273. lbc = 0;
  1274. if (cmsg->HLC && cmsg->HLC[0]) /* HLC specified explicitly */
  1275. lhlc = 2*cmsg->HLC[0];
  1276. else if (cip2bchlc[cmsg->CIPValue].hlc) /* HLC derived from CIP */
  1277. lhlc = strlen(cip2bchlc[cmsg->CIPValue].hlc);
  1278. else /* no HLC */
  1279. lhlc = 0;
  1280. if (lbc) {
  1281. /* have BC: allocate and assemble command string */
  1282. l = lbc + 7; /* "^SBC=" + value + "\r" + null byte */
  1283. if (lhlc)
  1284. l += lhlc + 7; /* ";^SHLC=" + value */
  1285. commands[AT_BC] = kmalloc(l, GFP_KERNEL);
  1286. if (!commands[AT_BC])
  1287. goto oom;
  1288. strcpy(commands[AT_BC], "^SBC=");
  1289. if (cmsg->BC && cmsg->BC[0]) /* BC specified explicitly */
  1290. decode_ie(cmsg->BC, commands[AT_BC] + 5);
  1291. else /* BC derived from CIP */
  1292. strcpy(commands[AT_BC] + 5,
  1293. cip2bchlc[cmsg->CIPValue].bc);
  1294. if (lhlc) {
  1295. strcpy(commands[AT_BC] + lbc + 5, ";^SHLC=");
  1296. if (cmsg->HLC && cmsg->HLC[0])
  1297. /* HLC specified explicitly */
  1298. decode_ie(cmsg->HLC,
  1299. commands[AT_BC] + lbc + 12);
  1300. else /* HLC derived from CIP */
  1301. strcpy(commands[AT_BC] + lbc + 12,
  1302. cip2bchlc[cmsg->CIPValue].hlc);
  1303. }
  1304. strcpy(commands[AT_BC] + l - 2, "\r");
  1305. } else {
  1306. /* no BC */
  1307. if (lhlc) {
  1308. dev_notice(cs->dev, "%s: cannot set HLC without BC\n",
  1309. "CONNECT_REQ");
  1310. info = CapiIllMessageParmCoding; /* ? */
  1311. goto error;
  1312. }
  1313. }
  1314. /* check/encode parameter: B Protocol */
  1315. if (cmsg->BProtocol == CAPI_DEFAULT) {
  1316. bcs->proto2 = L2_HDLC;
  1317. dev_warn(cs->dev,
  1318. "B2 Protocol X.75 SLP unsupported, using Transparent\n");
  1319. } else {
  1320. switch (cmsg->B1protocol) {
  1321. case 0:
  1322. bcs->proto2 = L2_HDLC;
  1323. break;
  1324. case 1:
  1325. bcs->proto2 = L2_VOICE;
  1326. break;
  1327. default:
  1328. dev_warn(cs->dev,
  1329. "B1 Protocol %u unsupported, using Transparent\n",
  1330. cmsg->B1protocol);
  1331. bcs->proto2 = L2_VOICE;
  1332. }
  1333. if (cmsg->B2protocol != 1)
  1334. dev_warn(cs->dev,
  1335. "B2 Protocol %u unsupported, using Transparent\n",
  1336. cmsg->B2protocol);
  1337. if (cmsg->B3protocol != 0)
  1338. dev_warn(cs->dev,
  1339. "B3 Protocol %u unsupported, using Transparent\n",
  1340. cmsg->B3protocol);
  1341. ignore_cstruct_param(cs, cmsg->B1configuration,
  1342. "CONNECT_REQ", "B1 Configuration");
  1343. ignore_cstruct_param(cs, cmsg->B2configuration,
  1344. "CONNECT_REQ", "B2 Configuration");
  1345. ignore_cstruct_param(cs, cmsg->B3configuration,
  1346. "CONNECT_REQ", "B3 Configuration");
  1347. }
  1348. commands[AT_PROTO] = kmalloc(9, GFP_KERNEL);
  1349. if (!commands[AT_PROTO])
  1350. goto oom;
  1351. snprintf(commands[AT_PROTO], 9, "^SBPR=%u\r", bcs->proto2);
  1352. /* ToDo: check/encode remaining parameters */
  1353. ignore_cstruct_param(cs, cmsg->CalledPartySubaddress,
  1354. "CONNECT_REQ", "Called pty subaddr");
  1355. ignore_cstruct_param(cs, cmsg->CallingPartySubaddress,
  1356. "CONNECT_REQ", "Calling pty subaddr");
  1357. ignore_cstruct_param(cs, cmsg->LLC,
  1358. "CONNECT_REQ", "LLC");
  1359. if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
  1360. ignore_cstruct_param(cs, cmsg->BChannelinformation,
  1361. "CONNECT_REQ", "B Channel Information");
  1362. ignore_cstruct_param(cs, cmsg->Keypadfacility,
  1363. "CONNECT_REQ", "Keypad Facility");
  1364. ignore_cstruct_param(cs, cmsg->Useruserdata,
  1365. "CONNECT_REQ", "User-User Data");
  1366. ignore_cstruct_param(cs, cmsg->Facilitydataarray,
  1367. "CONNECT_REQ", "Facility Data Array");
  1368. }
  1369. /* encode parameter: B channel to use */
  1370. commands[AT_ISO] = kmalloc(9, GFP_KERNEL);
  1371. if (!commands[AT_ISO])
  1372. goto oom;
  1373. snprintf(commands[AT_ISO], 9, "^SISO=%u\r",
  1374. (unsigned) bcs->channel + 1);
  1375. /* queue & schedule EV_DIAL event */
  1376. if (!gigaset_add_event(cs, &bcs->at_state, EV_DIAL, commands,
  1377. bcs->at_state.seq_index, NULL)) {
  1378. info = CAPI_MSGOSRESOURCEERR;
  1379. goto error;
  1380. }
  1381. gigaset_schedule_event(cs);
  1382. send_conf(iif, ap, skb, CapiSuccess);
  1383. return;
  1384. oom:
  1385. dev_err(cs->dev, "%s: out of memory\n", __func__);
  1386. info = CAPI_MSGOSRESOURCEERR;
  1387. error:
  1388. if (commands)
  1389. for (i = 0; i < AT_NUM; i++)
  1390. kfree(commands[i]);
  1391. kfree(commands);
  1392. gigaset_free_channel(bcs);
  1393. send_conf(iif, ap, skb, info);
  1394. }
  1395. /*
  1396. * process CONNECT_RESP message
  1397. * checks protocol parameters and queues an ACCEPT or HUP event
  1398. */
  1399. static void do_connect_resp(struct gigaset_capi_ctr *iif,
  1400. struct gigaset_capi_appl *ap,
  1401. struct sk_buff *skb)
  1402. {
  1403. struct cardstate *cs = iif->ctr.driverdata;
  1404. _cmsg *cmsg = &iif->acmsg;
  1405. struct bc_state *bcs;
  1406. struct gigaset_capi_appl *oap;
  1407. unsigned long flags;
  1408. int channel;
  1409. /* decode message */
  1410. capi_message2cmsg(cmsg, skb->data);
  1411. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1412. dev_kfree_skb_any(skb);
  1413. /* extract and check channel number from PLCI */
  1414. channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
  1415. if (!channel || channel > cs->channels) {
  1416. dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
  1417. "CONNECT_RESP", "PLCI", cmsg->adr.adrPLCI);
  1418. return;
  1419. }
  1420. bcs = cs->bcs + channel - 1;
  1421. switch (cmsg->Reject) {
  1422. case 0: /* Accept */
  1423. /* drop all competing applications, keep only this one */
  1424. spin_lock_irqsave(&bcs->aplock, flags);
  1425. while (bcs->ap != NULL) {
  1426. oap = bcs->ap;
  1427. bcs->ap = oap->bcnext;
  1428. if (oap != ap) {
  1429. spin_unlock_irqrestore(&bcs->aplock, flags);
  1430. send_disconnect_ind(bcs, oap,
  1431. CapiCallGivenToOtherApplication);
  1432. spin_lock_irqsave(&bcs->aplock, flags);
  1433. }
  1434. }
  1435. ap->bcnext = NULL;
  1436. bcs->ap = ap;
  1437. spin_unlock_irqrestore(&bcs->aplock, flags);
  1438. bcs->rx_bufsize = ap->rp.datablklen;
  1439. dev_kfree_skb(bcs->rx_skb);
  1440. gigaset_new_rx_skb(bcs);
  1441. bcs->chstate |= CHS_NOTIFY_LL;
  1442. /* check/encode B channel protocol */
  1443. if (cmsg->BProtocol == CAPI_DEFAULT) {
  1444. bcs->proto2 = L2_HDLC;
  1445. dev_warn(cs->dev,
  1446. "B2 Protocol X.75 SLP unsupported, using Transparent\n");
  1447. } else {
  1448. switch (cmsg->B1protocol) {
  1449. case 0:
  1450. bcs->proto2 = L2_HDLC;
  1451. break;
  1452. case 1:
  1453. bcs->proto2 = L2_VOICE;
  1454. break;
  1455. default:
  1456. dev_warn(cs->dev,
  1457. "B1 Protocol %u unsupported, using Transparent\n",
  1458. cmsg->B1protocol);
  1459. bcs->proto2 = L2_VOICE;
  1460. }
  1461. if (cmsg->B2protocol != 1)
  1462. dev_warn(cs->dev,
  1463. "B2 Protocol %u unsupported, using Transparent\n",
  1464. cmsg->B2protocol);
  1465. if (cmsg->B3protocol != 0)
  1466. dev_warn(cs->dev,
  1467. "B3 Protocol %u unsupported, using Transparent\n",
  1468. cmsg->B3protocol);
  1469. ignore_cstruct_param(cs, cmsg->B1configuration,
  1470. "CONNECT_RESP", "B1 Configuration");
  1471. ignore_cstruct_param(cs, cmsg->B2configuration,
  1472. "CONNECT_RESP", "B2 Configuration");
  1473. ignore_cstruct_param(cs, cmsg->B3configuration,
  1474. "CONNECT_RESP", "B3 Configuration");
  1475. }
  1476. /* ToDo: check/encode remaining parameters */
  1477. ignore_cstruct_param(cs, cmsg->ConnectedNumber,
  1478. "CONNECT_RESP", "Connected Number");
  1479. ignore_cstruct_param(cs, cmsg->ConnectedSubaddress,
  1480. "CONNECT_RESP", "Connected Subaddress");
  1481. ignore_cstruct_param(cs, cmsg->LLC,
  1482. "CONNECT_RESP", "LLC");
  1483. if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
  1484. ignore_cstruct_param(cs, cmsg->BChannelinformation,
  1485. "CONNECT_RESP", "BChannel Information");
  1486. ignore_cstruct_param(cs, cmsg->Keypadfacility,
  1487. "CONNECT_RESP", "Keypad Facility");
  1488. ignore_cstruct_param(cs, cmsg->Useruserdata,
  1489. "CONNECT_RESP", "User-User Data");
  1490. ignore_cstruct_param(cs, cmsg->Facilitydataarray,
  1491. "CONNECT_RESP", "Facility Data Array");
  1492. }
  1493. /* Accept call */
  1494. if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
  1495. EV_ACCEPT, NULL, 0, NULL))
  1496. return;
  1497. gigaset_schedule_event(cs);
  1498. return;
  1499. case 1: /* Ignore */
  1500. /* send DISCONNECT_IND to this application */
  1501. send_disconnect_ind(bcs, ap, 0);
  1502. /* remove it from the list of listening apps */
  1503. spin_lock_irqsave(&bcs->aplock, flags);
  1504. if (bcs->ap == ap) {
  1505. bcs->ap = ap->bcnext;
  1506. if (bcs->ap == NULL) {
  1507. /* last one: stop ev-layer hupD notifications */
  1508. bcs->apconnstate = APCONN_NONE;
  1509. bcs->chstate &= ~CHS_NOTIFY_LL;
  1510. }
  1511. spin_unlock_irqrestore(&bcs->aplock, flags);
  1512. return;
  1513. }
  1514. for (oap = bcs->ap; oap != NULL; oap = oap->bcnext) {
  1515. if (oap->bcnext == ap) {
  1516. oap->bcnext = oap->bcnext->bcnext;
  1517. spin_unlock_irqrestore(&bcs->aplock, flags);
  1518. return;
  1519. }
  1520. }
  1521. spin_unlock_irqrestore(&bcs->aplock, flags);
  1522. dev_err(cs->dev, "%s: application %u not found\n",
  1523. __func__, ap->id);
  1524. return;
  1525. default: /* Reject */
  1526. /* drop all competing applications, keep only this one */
  1527. spin_lock_irqsave(&bcs->aplock, flags);
  1528. while (bcs->ap != NULL) {
  1529. oap = bcs->ap;
  1530. bcs->ap = oap->bcnext;
  1531. if (oap != ap) {
  1532. spin_unlock_irqrestore(&bcs->aplock, flags);
  1533. send_disconnect_ind(bcs, oap,
  1534. CapiCallGivenToOtherApplication);
  1535. spin_lock_irqsave(&bcs->aplock, flags);
  1536. }
  1537. }
  1538. ap->bcnext = NULL;
  1539. bcs->ap = ap;
  1540. spin_unlock_irqrestore(&bcs->aplock, flags);
  1541. /* reject call - will trigger DISCONNECT_IND for this app */
  1542. dev_info(cs->dev, "%s: Reject=%x\n",
  1543. "CONNECT_RESP", cmsg->Reject);
  1544. if (!gigaset_add_event(cs, &cs->bcs[channel-1].at_state,
  1545. EV_HUP, NULL, 0, NULL))
  1546. return;
  1547. gigaset_schedule_event(cs);
  1548. return;
  1549. }
  1550. }
  1551. /*
  1552. * process CONNECT_B3_REQ message
  1553. * build NCCI and emit CONNECT_B3_CONF reply
  1554. */
  1555. static void do_connect_b3_req(struct gigaset_capi_ctr *iif,
  1556. struct gigaset_capi_appl *ap,
  1557. struct sk_buff *skb)
  1558. {
  1559. struct cardstate *cs = iif->ctr.driverdata;
  1560. _cmsg *cmsg = &iif->acmsg;
  1561. struct bc_state *bcs;
  1562. int channel;
  1563. /* decode message */
  1564. capi_message2cmsg(cmsg, skb->data);
  1565. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1566. /* extract and check channel number from PLCI */
  1567. channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
  1568. if (!channel || channel > cs->channels) {
  1569. dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
  1570. "CONNECT_B3_REQ", "PLCI", cmsg->adr.adrPLCI);
  1571. send_conf(iif, ap, skb, CapiIllContrPlciNcci);
  1572. return;
  1573. }
  1574. bcs = &cs->bcs[channel-1];
  1575. /* mark logical connection active */
  1576. bcs->apconnstate = APCONN_ACTIVE;
  1577. /* build NCCI: always 1 (one B3 connection only) */
  1578. cmsg->adr.adrNCCI |= 1 << 16;
  1579. /* NCPI parameter: not applicable for B3 Transparent */
  1580. ignore_cstruct_param(cs, cmsg->NCPI, "CONNECT_B3_REQ", "NCPI");
  1581. send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
  1582. CapiNcpiNotSupportedByProtocol : CapiSuccess);
  1583. }
  1584. /*
  1585. * process CONNECT_B3_RESP message
  1586. * Depending on the Reject parameter, either emit CONNECT_B3_ACTIVE_IND
  1587. * or queue EV_HUP and emit DISCONNECT_B3_IND.
  1588. * The emitted message is always shorter than the received one,
  1589. * allowing to reuse the skb.
  1590. */
  1591. static void do_connect_b3_resp(struct gigaset_capi_ctr *iif,
  1592. struct gigaset_capi_appl *ap,
  1593. struct sk_buff *skb)
  1594. {
  1595. struct cardstate *cs = iif->ctr.driverdata;
  1596. _cmsg *cmsg = &iif->acmsg;
  1597. struct bc_state *bcs;
  1598. int channel;
  1599. unsigned int msgsize;
  1600. u8 command;
  1601. /* decode message */
  1602. capi_message2cmsg(cmsg, skb->data);
  1603. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1604. /* extract and check channel number and NCCI */
  1605. channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
  1606. if (!channel || channel > cs->channels ||
  1607. ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
  1608. dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
  1609. "CONNECT_B3_RESP", "NCCI", cmsg->adr.adrNCCI);
  1610. dev_kfree_skb_any(skb);
  1611. return;
  1612. }
  1613. bcs = &cs->bcs[channel-1];
  1614. if (cmsg->Reject) {
  1615. /* Reject: clear B3 connect received flag */
  1616. bcs->apconnstate = APCONN_SETUP;
  1617. /* trigger hangup, causing eventual DISCONNECT_IND */
  1618. if (!gigaset_add_event(cs, &bcs->at_state,
  1619. EV_HUP, NULL, 0, NULL)) {
  1620. dev_kfree_skb_any(skb);
  1621. return;
  1622. }
  1623. gigaset_schedule_event(cs);
  1624. /* emit DISCONNECT_B3_IND */
  1625. command = CAPI_DISCONNECT_B3;
  1626. msgsize = CAPI_DISCONNECT_B3_IND_BASELEN;
  1627. } else {
  1628. /*
  1629. * Accept: emit CONNECT_B3_ACTIVE_IND immediately, as
  1630. * we only send CONNECT_B3_IND if the B channel is up
  1631. */
  1632. command = CAPI_CONNECT_B3_ACTIVE;
  1633. msgsize = CAPI_CONNECT_B3_ACTIVE_IND_BASELEN;
  1634. }
  1635. capi_cmsg_header(cmsg, ap->id, command, CAPI_IND,
  1636. ap->nextMessageNumber++, cmsg->adr.adrNCCI);
  1637. __skb_trim(skb, msgsize);
  1638. capi_cmsg2message(cmsg, skb->data);
  1639. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1640. capi_ctr_handle_message(&iif->ctr, ap->id, skb);
  1641. }
  1642. /*
  1643. * process DISCONNECT_REQ message
  1644. * schedule EV_HUP and emit DISCONNECT_B3_IND if necessary,
  1645. * emit DISCONNECT_CONF reply
  1646. */
  1647. static void do_disconnect_req(struct gigaset_capi_ctr *iif,
  1648. struct gigaset_capi_appl *ap,
  1649. struct sk_buff *skb)
  1650. {
  1651. struct cardstate *cs = iif->ctr.driverdata;
  1652. _cmsg *cmsg = &iif->acmsg;
  1653. struct bc_state *bcs;
  1654. _cmsg *b3cmsg;
  1655. struct sk_buff *b3skb;
  1656. int channel;
  1657. /* decode message */
  1658. capi_message2cmsg(cmsg, skb->data);
  1659. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1660. /* extract and check channel number from PLCI */
  1661. channel = (cmsg->adr.adrPLCI >> 8) & 0xff;
  1662. if (!channel || channel > cs->channels) {
  1663. dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
  1664. "DISCONNECT_REQ", "PLCI", cmsg->adr.adrPLCI);
  1665. send_conf(iif, ap, skb, CapiIllContrPlciNcci);
  1666. return;
  1667. }
  1668. bcs = cs->bcs + channel - 1;
  1669. /* ToDo: process parameter: Additional info */
  1670. if (cmsg->AdditionalInfo != CAPI_DEFAULT) {
  1671. ignore_cstruct_param(cs, cmsg->BChannelinformation,
  1672. "DISCONNECT_REQ", "B Channel Information");
  1673. ignore_cstruct_param(cs, cmsg->Keypadfacility,
  1674. "DISCONNECT_REQ", "Keypad Facility");
  1675. ignore_cstruct_param(cs, cmsg->Useruserdata,
  1676. "DISCONNECT_REQ", "User-User Data");
  1677. ignore_cstruct_param(cs, cmsg->Facilitydataarray,
  1678. "DISCONNECT_REQ", "Facility Data Array");
  1679. }
  1680. /* skip if DISCONNECT_IND already sent */
  1681. if (!bcs->apconnstate)
  1682. return;
  1683. /* check for active logical connection */
  1684. if (bcs->apconnstate >= APCONN_ACTIVE) {
  1685. /*
  1686. * emit DISCONNECT_B3_IND with cause 0x3301
  1687. * use separate cmsg structure, as the content of iif->acmsg
  1688. * is still needed for creating the _CONF message
  1689. */
  1690. b3cmsg = kmalloc(sizeof(*b3cmsg), GFP_KERNEL);
  1691. if (!b3cmsg) {
  1692. dev_err(cs->dev, "%s: out of memory\n", __func__);
  1693. send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
  1694. return;
  1695. }
  1696. capi_cmsg_header(b3cmsg, ap->id, CAPI_DISCONNECT_B3, CAPI_IND,
  1697. ap->nextMessageNumber++,
  1698. cmsg->adr.adrPLCI | (1 << 16));
  1699. b3cmsg->Reason_B3 = CapiProtocolErrorLayer1;
  1700. b3skb = alloc_skb(CAPI_DISCONNECT_B3_IND_BASELEN, GFP_KERNEL);
  1701. if (b3skb == NULL) {
  1702. dev_err(cs->dev, "%s: out of memory\n", __func__);
  1703. send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
  1704. return;
  1705. }
  1706. capi_cmsg2message(b3cmsg,
  1707. __skb_put(b3skb, CAPI_DISCONNECT_B3_IND_BASELEN));
  1708. kfree(b3cmsg);
  1709. capi_ctr_handle_message(&iif->ctr, ap->id, b3skb);
  1710. }
  1711. /* trigger hangup, causing eventual DISCONNECT_IND */
  1712. if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
  1713. send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
  1714. return;
  1715. }
  1716. gigaset_schedule_event(cs);
  1717. /* emit reply */
  1718. send_conf(iif, ap, skb, CapiSuccess);
  1719. }
  1720. /*
  1721. * process DISCONNECT_B3_REQ message
  1722. * schedule EV_HUP and emit DISCONNECT_B3_CONF reply
  1723. */
  1724. static void do_disconnect_b3_req(struct gigaset_capi_ctr *iif,
  1725. struct gigaset_capi_appl *ap,
  1726. struct sk_buff *skb)
  1727. {
  1728. struct cardstate *cs = iif->ctr.driverdata;
  1729. _cmsg *cmsg = &iif->acmsg;
  1730. struct bc_state *bcs;
  1731. int channel;
  1732. /* decode message */
  1733. capi_message2cmsg(cmsg, skb->data);
  1734. dump_cmsg(DEBUG_CMD, __func__, cmsg);
  1735. /* extract and check channel number and NCCI */
  1736. channel = (cmsg->adr.adrNCCI >> 8) & 0xff;
  1737. if (!channel || channel > cs->channels ||
  1738. ((cmsg->adr.adrNCCI >> 16) & 0xffff) != 1) {
  1739. dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
  1740. "DISCONNECT_B3_REQ", "NCCI", cmsg->adr.adrNCCI);
  1741. send_conf(iif, ap, skb, CapiIllContrPlciNcci);
  1742. return;
  1743. }
  1744. bcs = &cs->bcs[channel-1];
  1745. /* reject if logical connection not active */
  1746. if (bcs->apconnstate < APCONN_ACTIVE) {
  1747. send_conf(iif, ap, skb,
  1748. CapiMessageNotSupportedInCurrentState);
  1749. return;
  1750. }
  1751. /* trigger hangup, causing eventual DISCONNECT_B3_IND */
  1752. if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
  1753. send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
  1754. return;
  1755. }
  1756. gigaset_schedule_event(cs);
  1757. /* NCPI parameter: not applicable for B3 Transparent */
  1758. ignore_cstruct_param(cs, cmsg->NCPI,
  1759. "DISCONNECT_B3_REQ", "NCPI");
  1760. send_conf(iif, ap, skb, (cmsg->NCPI && cmsg->NCPI[0]) ?
  1761. CapiNcpiNotSupportedByProtocol : CapiSuccess);
  1762. }
  1763. /*
  1764. * process DATA_B3_REQ message
  1765. */
  1766. static void do_data_b3_req(struct gigaset_capi_ctr *iif,
  1767. struct gigaset_capi_appl *ap,
  1768. struct sk_buff *skb)
  1769. {
  1770. struct cardstate *cs = iif->ctr.driverdata;
  1771. struct bc_state *bcs;
  1772. int channel = CAPIMSG_PLCI_PART(skb->data);
  1773. u16 ncci = CAPIMSG_NCCI_PART(skb->data);
  1774. u16 msglen = CAPIMSG_LEN(skb->data);
  1775. u16 datalen = CAPIMSG_DATALEN(skb->data);
  1776. u16 flags = CAPIMSG_FLAGS(skb->data);
  1777. u16 msgid = CAPIMSG_MSGID(skb->data);
  1778. u16 handle = CAPIMSG_HANDLE_REQ(skb->data);
  1779. /* frequent message, avoid _cmsg overhead */
  1780. dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
  1781. /* check parameters */
  1782. if (channel == 0 || channel > cs->channels || ncci != 1) {
  1783. dev_notice(cs->dev, "%s: invalid %s 0x%02x\n",
  1784. "DATA_B3_REQ", "NCCI", CAPIMSG_NCCI(skb->data));
  1785. send_conf(iif, ap, skb, CapiIllContrPlciNcci);
  1786. return;
  1787. }
  1788. bcs = &cs->bcs[channel-1];
  1789. if (msglen != CAPI_DATA_B3_REQ_LEN && msglen != CAPI_DATA_B3_REQ_LEN64)
  1790. dev_notice(cs->dev, "%s: unexpected length %d\n",
  1791. "DATA_B3_REQ", msglen);
  1792. if (msglen + datalen != skb->len)
  1793. dev_notice(cs->dev, "%s: length mismatch (%d+%d!=%d)\n",
  1794. "DATA_B3_REQ", msglen, datalen, skb->len);
  1795. if (msglen + datalen > skb->len) {
  1796. /* message too short for announced data length */
  1797. send_conf(iif, ap, skb, CapiIllMessageParmCoding); /* ? */
  1798. return;
  1799. }
  1800. if (flags & CAPI_FLAGS_RESERVED) {
  1801. dev_notice(cs->dev, "%s: reserved flags set (%x)\n",
  1802. "DATA_B3_REQ", flags);
  1803. send_conf(iif, ap, skb, CapiIllMessageParmCoding);
  1804. return;
  1805. }
  1806. /* reject if logical connection not active */
  1807. if (bcs->apconnstate < APCONN_ACTIVE) {
  1808. send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
  1809. return;
  1810. }
  1811. /* pull CAPI message into link layer header */
  1812. skb_reset_mac_header(skb);
  1813. skb->mac_len = msglen;
  1814. skb_pull(skb, msglen);
  1815. /* pass to device-specific module */
  1816. if (cs->ops->send_skb(bcs, skb) < 0) {
  1817. send_conf(iif, ap, skb, CAPI_MSGOSRESOURCEERR);
  1818. return;
  1819. }
  1820. /*
  1821. * DATA_B3_CONF will be sent by gigaset_skb_sent() only if "delivery
  1822. * confirmation" bit is set; otherwise we have to send it now
  1823. */
  1824. if (!(flags & CAPI_FLAGS_DELIVERY_CONFIRMATION))
  1825. send_data_b3_conf(cs, &iif->ctr, ap->id, msgid, channel, handle,
  1826. flags ? CapiFlagsNotSupportedByProtocol
  1827. : CAPI_NOERROR);
  1828. }
  1829. /*
  1830. * process RESET_B3_REQ message
  1831. * just always reply "not supported by current protocol"
  1832. */
  1833. static void do_reset_b3_req(struct gigaset_capi_ctr *iif,
  1834. struct gigaset_capi_appl *ap,
  1835. struct sk_buff *skb)
  1836. {
  1837. /* decode message */
  1838. capi_message2cmsg(&iif->acmsg, skb->data);
  1839. dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
  1840. send_conf(iif, ap, skb,
  1841. CapiResetProcedureNotSupportedByCurrentProtocol);
  1842. }
  1843. /*
  1844. * dump unsupported/ignored messages at most twice per minute,
  1845. * some apps send those very frequently
  1846. */
  1847. static unsigned long ignored_msg_dump_time;
  1848. /*
  1849. * unsupported CAPI message handler
  1850. */
  1851. static void do_unsupported(struct gigaset_capi_ctr *iif,
  1852. struct gigaset_capi_appl *ap,
  1853. struct sk_buff *skb)
  1854. {
  1855. /* decode message */
  1856. capi_message2cmsg(&iif->acmsg, skb->data);
  1857. if (printk_timed_ratelimit(&ignored_msg_dump_time, 30 * 1000))
  1858. dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
  1859. send_conf(iif, ap, skb, CapiMessageNotSupportedInCurrentState);
  1860. }
  1861. /*
  1862. * CAPI message handler: no-op
  1863. */
  1864. static void do_nothing(struct gigaset_capi_ctr *iif,
  1865. struct gigaset_capi_appl *ap,
  1866. struct sk_buff *skb)
  1867. {
  1868. if (printk_timed_ratelimit(&ignored_msg_dump_time, 30 * 1000)) {
  1869. /* decode message */
  1870. capi_message2cmsg(&iif->acmsg, skb->data);
  1871. dump_cmsg(DEBUG_CMD, __func__, &iif->acmsg);
  1872. }
  1873. dev_kfree_skb_any(skb);
  1874. }
  1875. static void do_data_b3_resp(struct gigaset_capi_ctr *iif,
  1876. struct gigaset_capi_appl *ap,
  1877. struct sk_buff *skb)
  1878. {
  1879. dump_rawmsg(DEBUG_MCMD, __func__, skb->data);
  1880. dev_kfree_skb_any(skb);
  1881. }
  1882. /* table of outgoing CAPI message handlers with lookup function */
  1883. typedef void (*capi_send_handler_t)(struct gigaset_capi_ctr *,
  1884. struct gigaset_capi_appl *,
  1885. struct sk_buff *);
  1886. static struct {
  1887. u16 cmd;
  1888. capi_send_handler_t handler;
  1889. } capi_send_handler_table[] = {
  1890. /* most frequent messages first for faster lookup */
  1891. { CAPI_DATA_B3_REQ, do_data_b3_req },
  1892. { CAPI_DATA_B3_RESP, do_data_b3_resp },
  1893. { CAPI_ALERT_REQ, do_alert_req },
  1894. { CAPI_CONNECT_ACTIVE_RESP, do_nothing },
  1895. { CAPI_CONNECT_B3_ACTIVE_RESP, do_nothing },
  1896. { CAPI_CONNECT_B3_REQ, do_connect_b3_req },
  1897. { CAPI_CONNECT_B3_RESP, do_connect_b3_resp },
  1898. { CAPI_CONNECT_B3_T90_ACTIVE_RESP, do_nothing },
  1899. { CAPI_CONNECT_REQ, do_connect_req },
  1900. { CAPI_CONNECT_RESP, do_connect_resp },
  1901. { CAPI_DISCONNECT_B3_REQ, do_disconnect_b3_req },
  1902. { CAPI_DISCONNECT_B3_RESP, do_nothing },
  1903. { CAPI_DISCONNECT_REQ, do_disconnect_req },
  1904. { CAPI_DISCONNECT_RESP, do_nothing },
  1905. { CAPI_FACILITY_REQ, do_facility_req },
  1906. { CAPI_FACILITY_RESP, do_nothing },
  1907. { CAPI_LISTEN_REQ, do_listen_req },
  1908. { CAPI_SELECT_B_PROTOCOL_REQ, do_unsupported },
  1909. { CAPI_RESET_B3_REQ, do_reset_b3_req },
  1910. { CAPI_RESET_B3_RESP, do_nothing },
  1911. /*
  1912. * ToDo: support overlap sending (requires ev-layer state
  1913. * machine extension to generate additional ATD commands)
  1914. */
  1915. { CAPI_INFO_REQ, do_unsupported },
  1916. { CAPI_INFO_RESP, do_nothing },
  1917. /*
  1918. * ToDo: what's the proper response for these?
  1919. */
  1920. { CAPI_MANUFACTURER_REQ, do_nothing },
  1921. { CAPI_MANUFACTURER_RESP, do_nothing },
  1922. };
  1923. /* look up handler */
  1924. static inline capi_send_handler_t lookup_capi_send_handler(const u16 cmd)
  1925. {
  1926. size_t i;
  1927. for (i = 0; i < ARRAY_SIZE(capi_send_handler_table); i++)
  1928. if (capi_send_handler_table[i].cmd == cmd)
  1929. return capi_send_handler_table[i].handler;
  1930. return NULL;
  1931. }
  1932. /**
  1933. * gigaset_send_message() - accept a CAPI message from an application
  1934. * @ctr: controller descriptor structure.
  1935. * @skb: CAPI message.
  1936. *
  1937. * Return value: CAPI error code
  1938. * Note: capidrv (and probably others, too) only uses the return value to
  1939. * decide whether it has to free the skb (only if result != CAPI_NOERROR (0))
  1940. */
  1941. static u16 gigaset_send_message(struct capi_ctr *ctr, struct sk_buff *skb)
  1942. {
  1943. struct gigaset_capi_ctr *iif
  1944. = container_of(ctr, struct gigaset_capi_ctr, ctr);
  1945. struct cardstate *cs = ctr->driverdata;
  1946. struct gigaset_capi_appl *ap;
  1947. capi_send_handler_t handler;
  1948. /* can only handle linear sk_buffs */
  1949. if (skb_linearize(skb) < 0) {
  1950. dev_warn(cs->dev, "%s: skb_linearize failed\n", __func__);
  1951. return CAPI_MSGOSRESOURCEERR;
  1952. }
  1953. /* retrieve application data structure */
  1954. ap = get_appl(iif, CAPIMSG_APPID(skb->data));
  1955. if (!ap) {
  1956. dev_notice(cs->dev, "%s: application %u not registered\n",
  1957. __func__, CAPIMSG_APPID(skb->data));
  1958. return CAPI_ILLAPPNR;
  1959. }
  1960. /* look up command */
  1961. handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
  1962. if (!handler) {
  1963. /* unknown/unsupported message type */
  1964. if (printk_ratelimit())
  1965. dev_notice(cs->dev, "%s: unsupported message %u\n",
  1966. __func__, CAPIMSG_CMD(skb->data));
  1967. return CAPI_ILLCMDORSUBCMDORMSGTOSMALL;
  1968. }
  1969. /* serialize */
  1970. if (atomic_add_return(1, &iif->sendqlen) > 1) {
  1971. /* queue behind other messages */
  1972. skb_queue_tail(&iif->sendqueue, skb);
  1973. return CAPI_NOERROR;
  1974. }
  1975. /* process message */
  1976. handler(iif, ap, skb);
  1977. /* process other messages arrived in the meantime */
  1978. while (atomic_sub_return(1, &iif->sendqlen) > 0) {
  1979. skb = skb_dequeue(&iif->sendqueue);
  1980. if (!skb) {
  1981. /* should never happen */
  1982. dev_err(cs->dev, "%s: send queue empty\n", __func__);
  1983. continue;
  1984. }
  1985. ap = get_appl(iif, CAPIMSG_APPID(skb->data));
  1986. if (!ap) {
  1987. /* could that happen? */
  1988. dev_warn(cs->dev, "%s: application %u vanished\n",
  1989. __func__, CAPIMSG_APPID(skb->data));
  1990. continue;
  1991. }
  1992. handler = lookup_capi_send_handler(CAPIMSG_CMD(skb->data));
  1993. if (!handler) {
  1994. /* should never happen */
  1995. dev_err(cs->dev, "%s: handler %x vanished\n",
  1996. __func__, CAPIMSG_CMD(skb->data));
  1997. continue;
  1998. }
  1999. handler(iif, ap, skb);
  2000. }
  2001. return CAPI_NOERROR;
  2002. }
  2003. /**
  2004. * gigaset_procinfo() - build single line description for controller
  2005. * @ctr: controller descriptor structure.
  2006. *
  2007. * Return value: pointer to generated string (null terminated)
  2008. */
  2009. static char *gigaset_procinfo(struct capi_ctr *ctr)
  2010. {
  2011. return ctr->name; /* ToDo: more? */
  2012. }
  2013. static int gigaset_proc_show(struct seq_file *m, void *v)
  2014. {
  2015. struct capi_ctr *ctr = m->private;
  2016. struct cardstate *cs = ctr->driverdata;
  2017. char *s;
  2018. int i;
  2019. seq_printf(m, "%-16s %s\n", "name", ctr->name);
  2020. seq_printf(m, "%-16s %s %s\n", "dev",
  2021. dev_driver_string(cs->dev), dev_name(cs->dev));
  2022. seq_printf(m, "%-16s %d\n", "id", cs->myid);
  2023. if (cs->gotfwver)
  2024. seq_printf(m, "%-16s %d.%d.%d.%d\n", "firmware",
  2025. cs->fwver[0], cs->fwver[1], cs->fwver[2], cs->fwver[3]);
  2026. seq_printf(m, "%-16s %d\n", "channels", cs->channels);
  2027. seq_printf(m, "%-16s %s\n", "onechannel", cs->onechannel ? "yes" : "no");
  2028. switch (cs->mode) {
  2029. case M_UNKNOWN:
  2030. s = "unknown";
  2031. break;
  2032. case M_CONFIG:
  2033. s = "config";
  2034. break;
  2035. case M_UNIMODEM:
  2036. s = "Unimodem";
  2037. break;
  2038. case M_CID:
  2039. s = "CID";
  2040. break;
  2041. default:
  2042. s = "??";
  2043. }
  2044. seq_printf(m, "%-16s %s\n", "mode", s);
  2045. switch (cs->mstate) {
  2046. case MS_UNINITIALIZED:
  2047. s = "uninitialized";
  2048. break;
  2049. case MS_INIT:
  2050. s = "init";
  2051. break;
  2052. case MS_LOCKED:
  2053. s = "locked";
  2054. break;
  2055. case MS_SHUTDOWN:
  2056. s = "shutdown";
  2057. break;
  2058. case MS_RECOVER:
  2059. s = "recover";
  2060. break;
  2061. case MS_READY:
  2062. s = "ready";
  2063. break;
  2064. default:
  2065. s = "??";
  2066. }
  2067. seq_printf(m, "%-16s %s\n", "mstate", s);
  2068. seq_printf(m, "%-16s %s\n", "running", cs->running ? "yes" : "no");
  2069. seq_printf(m, "%-16s %s\n", "connected", cs->connected ? "yes" : "no");
  2070. seq_printf(m, "%-16s %s\n", "isdn_up", cs->isdn_up ? "yes" : "no");
  2071. seq_printf(m, "%-16s %s\n", "cidmode", cs->cidmode ? "yes" : "no");
  2072. for (i = 0; i < cs->channels; i++) {
  2073. seq_printf(m, "[%d]%-13s %d\n", i, "corrupted",
  2074. cs->bcs[i].corrupted);
  2075. seq_printf(m, "[%d]%-13s %d\n", i, "trans_down",
  2076. cs->bcs[i].trans_down);
  2077. seq_printf(m, "[%d]%-13s %d\n", i, "trans_up",
  2078. cs->bcs[i].trans_up);
  2079. seq_printf(m, "[%d]%-13s %d\n", i, "chstate",
  2080. cs->bcs[i].chstate);
  2081. switch (cs->bcs[i].proto2) {
  2082. case L2_BITSYNC:
  2083. s = "bitsync";
  2084. break;
  2085. case L2_HDLC:
  2086. s = "HDLC";
  2087. break;
  2088. case L2_VOICE:
  2089. s = "voice";
  2090. break;
  2091. default:
  2092. s = "??";
  2093. }
  2094. seq_printf(m, "[%d]%-13s %s\n", i, "proto2", s);
  2095. }
  2096. return 0;
  2097. }
  2098. static int gigaset_proc_open(struct inode *inode, struct file *file)
  2099. {
  2100. return single_open(file, gigaset_proc_show, PDE(inode)->data);
  2101. }
  2102. static const struct file_operations gigaset_proc_fops = {
  2103. .owner = THIS_MODULE,
  2104. .open = gigaset_proc_open,
  2105. .read = seq_read,
  2106. .llseek = seq_lseek,
  2107. .release = single_release,
  2108. };
  2109. /**
  2110. * gigaset_isdn_regdev() - register device to LL
  2111. * @cs: device descriptor structure.
  2112. * @isdnid: device name.
  2113. *
  2114. * Return value: 1 for success, 0 for failure
  2115. */
  2116. int gigaset_isdn_regdev(struct cardstate *cs, const char *isdnid)
  2117. {
  2118. struct gigaset_capi_ctr *iif;
  2119. int rc;
  2120. iif = kmalloc(sizeof(*iif), GFP_KERNEL);
  2121. if (!iif) {
  2122. pr_err("%s: out of memory\n", __func__);
  2123. return 0;
  2124. }
  2125. /* prepare controller structure */
  2126. iif->ctr.owner = THIS_MODULE;
  2127. iif->ctr.driverdata = cs;
  2128. strncpy(iif->ctr.name, isdnid, sizeof(iif->ctr.name));
  2129. iif->ctr.driver_name = "gigaset";
  2130. iif->ctr.load_firmware = NULL;
  2131. iif->ctr.reset_ctr = NULL;
  2132. iif->ctr.register_appl = gigaset_register_appl;
  2133. iif->ctr.release_appl = gigaset_release_appl;
  2134. iif->ctr.send_message = gigaset_send_message;
  2135. iif->ctr.procinfo = gigaset_procinfo;
  2136. iif->ctr.proc_fops = &gigaset_proc_fops;
  2137. INIT_LIST_HEAD(&iif->appls);
  2138. skb_queue_head_init(&iif->sendqueue);
  2139. atomic_set(&iif->sendqlen, 0);
  2140. /* register controller with CAPI */
  2141. rc = attach_capi_ctr(&iif->ctr);
  2142. if (rc) {
  2143. pr_err("attach_capi_ctr failed (%d)\n", rc);
  2144. kfree(iif);
  2145. return 0;
  2146. }
  2147. cs->iif = iif;
  2148. cs->hw_hdr_len = CAPI_DATA_B3_REQ_LEN;
  2149. return 1;
  2150. }
  2151. /**
  2152. * gigaset_isdn_unregdev() - unregister device from LL
  2153. * @cs: device descriptor structure.
  2154. */
  2155. void gigaset_isdn_unregdev(struct cardstate *cs)
  2156. {
  2157. struct gigaset_capi_ctr *iif = cs->iif;
  2158. detach_capi_ctr(&iif->ctr);
  2159. kfree(iif);
  2160. cs->iif = NULL;
  2161. }
  2162. static struct capi_driver capi_driver_gigaset = {
  2163. .name = "gigaset",
  2164. .revision = "1.0",
  2165. };
  2166. /**
  2167. * gigaset_isdn_regdrv() - register driver to LL
  2168. */
  2169. void gigaset_isdn_regdrv(void)
  2170. {
  2171. pr_info("Kernel CAPI interface\n");
  2172. register_capi_driver(&capi_driver_gigaset);
  2173. }
  2174. /**
  2175. * gigaset_isdn_unregdrv() - unregister driver from LL
  2176. */
  2177. void gigaset_isdn_unregdrv(void)
  2178. {
  2179. unregister_capi_driver(&capi_driver_gigaset);
  2180. }