capi.c 63 KB

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