capi.c 68 KB

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