capi.c 68 KB

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