capi.c 68 KB

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