capi.c 67 KB

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