capi.c 71 KB

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