capi.c 68 KB

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