capi.c 63 KB

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