capi.c 68 KB

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