capi.c 68 KB

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