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