capi.c 68 KB

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