ev-layer.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /*
  2. * Stuff used by all variants of the driver
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers <Eilers.Stefan@epost.de>,
  5. * Hansjoerg Lipp <hjlipp@web.de>,
  6. * Tilman Schmidt <tilman@imap.cc>.
  7. *
  8. * =====================================================================
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. * =====================================================================
  14. * ToDo: ...
  15. * =====================================================================
  16. * Version: $Id: ev-layer.c,v 1.4.2.18 2006/02/04 18:28:16 hjlipp Exp $
  17. * =====================================================================
  18. */
  19. #include "gigaset.h"
  20. /* ========================================================== */
  21. /* bit masks for pending commands */
  22. #define PC_INIT 0x004
  23. #define PC_DLE0 0x008
  24. #define PC_DLE1 0x010
  25. #define PC_CID 0x080
  26. #define PC_NOCID 0x100
  27. #define PC_HUP 0x002
  28. #define PC_DIAL 0x001
  29. #define PC_ACCEPT 0x040
  30. #define PC_SHUTDOWN 0x020
  31. #define PC_CIDMODE 0x200
  32. #define PC_UMMODE 0x400
  33. /* types of modem responses */
  34. #define RT_NOTHING 0
  35. #define RT_ZSAU 1
  36. #define RT_RING 2
  37. #define RT_NUMBER 3
  38. #define RT_STRING 4
  39. #define RT_HEX 5
  40. #define RT_ZCAU 6
  41. /* Possible ASCII responses */
  42. #define RSP_OK 0
  43. //#define RSP_BUSY 1
  44. //#define RSP_CONNECT 2
  45. #define RSP_ZGCI 3
  46. #define RSP_RING 4
  47. #define RSP_ZAOC 5
  48. #define RSP_ZCSTR 6
  49. #define RSP_ZCFGT 7
  50. #define RSP_ZCFG 8
  51. #define RSP_ZCCR 9
  52. #define RSP_EMPTY 10
  53. #define RSP_ZLOG 11
  54. #define RSP_ZCAU 12
  55. #define RSP_ZMWI 13
  56. #define RSP_ZABINFO 14
  57. #define RSP_ZSMLSTCHG 15
  58. #define RSP_VAR 100
  59. #define RSP_ZSAU (RSP_VAR + VAR_ZSAU)
  60. #define RSP_ZDLE (RSP_VAR + VAR_ZDLE)
  61. #define RSP_ZVLS (RSP_VAR + VAR_ZVLS)
  62. #define RSP_ZCTP (RSP_VAR + VAR_ZCTP)
  63. #define RSP_STR (RSP_VAR + VAR_NUM)
  64. #define RSP_NMBR (RSP_STR + STR_NMBR)
  65. #define RSP_ZCPN (RSP_STR + STR_ZCPN)
  66. #define RSP_ZCON (RSP_STR + STR_ZCON)
  67. #define RSP_ZBC (RSP_STR + STR_ZBC)
  68. #define RSP_ZHLC (RSP_STR + STR_ZHLC)
  69. #define RSP_ERROR -1 /* ERROR */
  70. #define RSP_WRONG_CID -2 /* unknown cid in cmd */
  71. //#define RSP_EMPTY -3
  72. #define RSP_UNKNOWN -4 /* unknown response */
  73. #define RSP_FAIL -5 /* internal error */
  74. #define RSP_INVAL -6 /* invalid response */
  75. #define RSP_NONE -19
  76. #define RSP_STRING -20
  77. #define RSP_NULL -21
  78. //#define RSP_RETRYFAIL -22
  79. //#define RSP_RETRY -23
  80. //#define RSP_SKIP -24
  81. #define RSP_INIT -27
  82. #define RSP_ANY -26
  83. #define RSP_LAST -28
  84. #define RSP_NODEV -9
  85. /* actions for process_response */
  86. #define ACT_NOTHING 0
  87. #define ACT_SETDLE1 1
  88. #define ACT_SETDLE0 2
  89. #define ACT_FAILINIT 3
  90. #define ACT_HUPMODEM 4
  91. #define ACT_CONFIGMODE 5
  92. #define ACT_INIT 6
  93. #define ACT_DLE0 7
  94. #define ACT_DLE1 8
  95. #define ACT_FAILDLE0 9
  96. #define ACT_FAILDLE1 10
  97. #define ACT_RING 11
  98. #define ACT_CID 12
  99. #define ACT_FAILCID 13
  100. #define ACT_SDOWN 14
  101. #define ACT_FAILSDOWN 15
  102. #define ACT_DEBUG 16
  103. #define ACT_WARN 17
  104. #define ACT_DIALING 18
  105. #define ACT_ABORTDIAL 19
  106. #define ACT_DISCONNECT 20
  107. #define ACT_CONNECT 21
  108. #define ACT_REMOTEREJECT 22
  109. #define ACT_CONNTIMEOUT 23
  110. #define ACT_REMOTEHUP 24
  111. #define ACT_ABORTHUP 25
  112. #define ACT_ICALL 26
  113. #define ACT_ACCEPTED 27
  114. #define ACT_ABORTACCEPT 28
  115. #define ACT_TIMEOUT 29
  116. #define ACT_GETSTRING 30
  117. #define ACT_SETVER 31
  118. #define ACT_FAILVER 32
  119. #define ACT_GOTVER 33
  120. #define ACT_TEST 34
  121. #define ACT_ERROR 35
  122. #define ACT_ABORTCID 36
  123. #define ACT_ZCAU 37
  124. #define ACT_NOTIFY_BC_DOWN 38
  125. #define ACT_NOTIFY_BC_UP 39
  126. #define ACT_DIAL 40
  127. #define ACT_ACCEPT 41
  128. #define ACT_PROTO_L2 42
  129. #define ACT_HUP 43
  130. #define ACT_IF_LOCK 44
  131. #define ACT_START 45
  132. #define ACT_STOP 46
  133. #define ACT_FAKEDLE0 47
  134. #define ACT_FAKEHUP 48
  135. #define ACT_FAKESDOWN 49
  136. #define ACT_SHUTDOWN 50
  137. #define ACT_PROC_CIDMODE 51
  138. #define ACT_UMODESET 52
  139. #define ACT_FAILUMODE 53
  140. #define ACT_CMODESET 54
  141. #define ACT_FAILCMODE 55
  142. #define ACT_IF_VER 56
  143. #define ACT_CMD 100
  144. /* at command sequences */
  145. #define SEQ_NONE 0
  146. #define SEQ_INIT 100
  147. #define SEQ_DLE0 200
  148. #define SEQ_DLE1 250
  149. #define SEQ_CID 300
  150. #define SEQ_NOCID 350
  151. #define SEQ_HUP 400
  152. #define SEQ_DIAL 600
  153. #define SEQ_ACCEPT 720
  154. #define SEQ_SHUTDOWN 500
  155. #define SEQ_CIDMODE 10
  156. #define SEQ_UMMODE 11
  157. // 100: init, 200: dle0, 250:dle1, 300: get cid (dial), 350: "hup" (no cid), 400: hup, 500: reset, 600: dial, 700: ring
  158. struct reply_t gigaset_tab_nocid_m10x[]= /* with dle mode */
  159. {
  160. /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
  161. /* initialize device, set cid mode if possible */
  162. //{RSP_INIT, -1, -1,100, 900, 0, {ACT_TEST}},
  163. //{RSP_ERROR, 900,900, -1, 0, 0, {ACT_FAILINIT}},
  164. //{RSP_OK, 900,900, -1, 100, INIT_TIMEOUT,
  165. // {ACT_TIMEOUT}},
  166. {RSP_INIT, -1, -1,SEQ_INIT, 100, INIT_TIMEOUT,
  167. {ACT_TIMEOUT}}, /* wait until device is ready */
  168. {EV_TIMEOUT, 100,100, -1, 101, 3, {0}, "Z\r"}, /* device in transparent mode? try to initialize it. */
  169. {RSP_OK, 101,103, -1, 120, 5, {ACT_GETSTRING}, "+GMR\r"}, /* get version */
  170. {EV_TIMEOUT, 101,101, -1, 102, 5, {0}, "Z\r"}, /* timeout => try once again. */
  171. {RSP_ERROR, 101,101, -1, 102, 5, {0}, "Z\r"}, /* error => try once again. */
  172. {EV_TIMEOUT, 102,102, -1, 108, 5, {ACT_SETDLE1}, "^SDLE=0\r"}, /* timeout => try again in DLE mode. */
  173. {RSP_OK, 108,108, -1, 104,-1},
  174. {RSP_ZDLE, 104,104, 0, 103, 5, {0}, "Z\r"},
  175. {EV_TIMEOUT, 104,104, -1, 0, 0, {ACT_FAILINIT}},
  176. {RSP_ERROR, 108,108, -1, 0, 0, {ACT_FAILINIT}},
  177. {EV_TIMEOUT, 108,108, -1, 105, 2, {ACT_SETDLE0,
  178. ACT_HUPMODEM,
  179. ACT_TIMEOUT}}, /* still timeout => connection in unimodem mode? */
  180. {EV_TIMEOUT, 105,105, -1, 103, 5, {0}, "Z\r"},
  181. {RSP_ERROR, 102,102, -1, 107, 5, {0}, "^GETPRE\r"}, /* ERROR on ATZ => maybe in config mode? */
  182. {RSP_OK, 107,107, -1, 0, 0, {ACT_CONFIGMODE}},
  183. {RSP_ERROR, 107,107, -1, 0, 0, {ACT_FAILINIT}},
  184. {EV_TIMEOUT, 107,107, -1, 0, 0, {ACT_FAILINIT}},
  185. {RSP_ERROR, 103,103, -1, 0, 0, {ACT_FAILINIT}},
  186. {EV_TIMEOUT, 103,103, -1, 0, 0, {ACT_FAILINIT}},
  187. {RSP_STRING, 120,120, -1, 121,-1, {ACT_SETVER}},
  188. {EV_TIMEOUT, 120,121, -1, 0, 0, {ACT_FAILVER, ACT_INIT}},
  189. {RSP_ERROR, 120,121, -1, 0, 0, {ACT_FAILVER, ACT_INIT}},
  190. {RSP_OK, 121,121, -1, 0, 0, {ACT_GOTVER, ACT_INIT}},
  191. #if 0
  192. {EV_TIMEOUT, 120,121, -1, 130, 5, {ACT_FAILVER}, "^SGCI=1\r"},
  193. {RSP_ERROR, 120,121, -1, 130, 5, {ACT_FAILVER}, "^SGCI=1\r"},
  194. {RSP_OK, 121,121, -1, 130, 5, {ACT_GOTVER}, "^SGCI=1\r"},
  195. {RSP_OK, 130,130, -1, 0, 0, {ACT_INIT}},
  196. {RSP_ERROR, 130,130, -1, 0, 0, {ACT_FAILINIT}},
  197. {EV_TIMEOUT, 130,130, -1, 0, 0, {ACT_FAILINIT}},
  198. #endif
  199. /* leave dle mode */
  200. {RSP_INIT, 0, 0,SEQ_DLE0, 201, 5, {0}, "^SDLE=0\r"},
  201. {RSP_OK, 201,201, -1, 202,-1},
  202. //{RSP_ZDLE, 202,202, 0, 202, 0, {ACT_ERROR}},//DELETE
  203. {RSP_ZDLE, 202,202, 0, 0, 0, {ACT_DLE0}},
  204. {RSP_NODEV, 200,249, -1, 0, 0, {ACT_FAKEDLE0}},
  205. {RSP_ERROR, 200,249, -1, 0, 0, {ACT_FAILDLE0}},
  206. {EV_TIMEOUT, 200,249, -1, 0, 0, {ACT_FAILDLE0}},
  207. /* enter dle mode */
  208. {RSP_INIT, 0, 0,SEQ_DLE1, 251, 5, {0}, "^SDLE=1\r"},
  209. {RSP_OK, 251,251, -1, 252,-1},
  210. {RSP_ZDLE, 252,252, 1, 0, 0, {ACT_DLE1}},
  211. {RSP_ERROR, 250,299, -1, 0, 0, {ACT_FAILDLE1}},
  212. {EV_TIMEOUT, 250,299, -1, 0, 0, {ACT_FAILDLE1}},
  213. /* incoming call */
  214. {RSP_RING, -1, -1, -1, -1,-1, {ACT_RING}},
  215. /* get cid */
  216. //{RSP_INIT, 0, 0,300, 901, 0, {ACT_TEST}},
  217. //{RSP_ERROR, 901,901, -1, 0, 0, {ACT_FAILCID}},
  218. //{RSP_OK, 901,901, -1, 301, 5, {0}, "^SGCI?\r"},
  219. {RSP_INIT, 0, 0,SEQ_CID, 301, 5, {0}, "^SGCI?\r"},
  220. {RSP_OK, 301,301, -1, 302,-1},
  221. {RSP_ZGCI, 302,302, -1, 0, 0, {ACT_CID}},
  222. {RSP_ERROR, 301,349, -1, 0, 0, {ACT_FAILCID}},
  223. {EV_TIMEOUT, 301,349, -1, 0, 0, {ACT_FAILCID}},
  224. /* enter cid mode */
  225. {RSP_INIT, 0, 0,SEQ_CIDMODE, 150, 5, {0}, "^SGCI=1\r"},
  226. {RSP_OK, 150,150, -1, 0, 0, {ACT_CMODESET}},
  227. {RSP_ERROR, 150,150, -1, 0, 0, {ACT_FAILCMODE}},
  228. {EV_TIMEOUT, 150,150, -1, 0, 0, {ACT_FAILCMODE}},
  229. /* leave cid mode */
  230. //{RSP_INIT, 0, 0,SEQ_UMMODE, 160, 5, {0}, "^SGCI=0\r"},
  231. {RSP_INIT, 0, 0,SEQ_UMMODE, 160, 5, {0}, "Z\r"},
  232. {RSP_OK, 160,160, -1, 0, 0, {ACT_UMODESET}},
  233. {RSP_ERROR, 160,160, -1, 0, 0, {ACT_FAILUMODE}},
  234. {EV_TIMEOUT, 160,160, -1, 0, 0, {ACT_FAILUMODE}},
  235. /* abort getting cid */
  236. {RSP_INIT, 0, 0,SEQ_NOCID, 0, 0, {ACT_ABORTCID}},
  237. /* reset */
  238. #if 0
  239. {RSP_INIT, 0, 0,SEQ_SHUTDOWN, 503, 5, {0}, "^SGCI=0\r"},
  240. {RSP_OK, 503,503, -1, 504, 5, {0}, "Z\r"},
  241. #endif
  242. {RSP_INIT, 0, 0,SEQ_SHUTDOWN, 504, 5, {0}, "Z\r"},
  243. {RSP_OK, 504,504, -1, 0, 0, {ACT_SDOWN}},
  244. {RSP_ERROR, 501,599, -1, 0, 0, {ACT_FAILSDOWN}},
  245. {EV_TIMEOUT, 501,599, -1, 0, 0, {ACT_FAILSDOWN}},
  246. {RSP_NODEV, 501,599, -1, 0, 0, {ACT_FAKESDOWN}},
  247. {EV_PROC_CIDMODE,-1, -1, -1, -1,-1, {ACT_PROC_CIDMODE}}, //FIXME
  248. {EV_IF_LOCK, -1, -1, -1, -1,-1, {ACT_IF_LOCK}}, //FIXME
  249. {EV_IF_VER, -1, -1, -1, -1,-1, {ACT_IF_VER}}, //FIXME
  250. {EV_START, -1, -1, -1, -1,-1, {ACT_START}}, //FIXME
  251. {EV_STOP, -1, -1, -1, -1,-1, {ACT_STOP}}, //FIXME
  252. {EV_SHUTDOWN, -1, -1, -1, -1,-1, {ACT_SHUTDOWN}}, //FIXME
  253. /* misc. */
  254. {RSP_EMPTY, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  255. {RSP_ZCFGT, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  256. {RSP_ZCFG, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  257. {RSP_ZLOG, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  258. {RSP_ZMWI, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  259. {RSP_ZABINFO, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  260. {RSP_ZSMLSTCHG,-1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  261. {RSP_ZCAU, -1, -1, -1, -1,-1, {ACT_ZCAU}},
  262. {RSP_NONE, -1, -1, -1, -1,-1, {ACT_DEBUG}},
  263. {RSP_ANY, -1, -1, -1, -1,-1, {ACT_WARN}},
  264. {RSP_LAST}
  265. };
  266. // 600: start dialing, 650: dial in progress, 800: connection is up, 700: ring, 400: hup, 750: accepted icall
  267. struct reply_t gigaset_tab_cid_m10x[] = /* for M10x */
  268. {
  269. /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
  270. /* dial */
  271. {EV_DIAL, -1, -1, -1, -1,-1, {ACT_DIAL}}, //FIXME
  272. {RSP_INIT, 0, 0,SEQ_DIAL, 601, 5, {ACT_CMD+AT_BC}},
  273. {RSP_OK, 601,601, -1, 602, 5, {ACT_CMD+AT_HLC}},
  274. {RSP_NULL, 602,602, -1, 603, 5, {ACT_CMD+AT_PROTO}},
  275. {RSP_OK, 602,602, -1, 603, 5, {ACT_CMD+AT_PROTO}},
  276. {RSP_OK, 603,603, -1, 604, 5, {ACT_CMD+AT_TYPE}},
  277. {RSP_OK, 604,604, -1, 605, 5, {ACT_CMD+AT_MSN}},
  278. {RSP_OK, 605,605, -1, 606, 5, {ACT_CMD+AT_ISO}},
  279. {RSP_NULL, 605,605, -1, 606, 5, {ACT_CMD+AT_ISO}},
  280. {RSP_OK, 606,606, -1, 607, 5, {0}, "+VLS=17\r"}, /* set "Endgeraetemodus" */
  281. {RSP_OK, 607,607, -1, 608,-1},
  282. //{RSP_ZSAU, 608,608,ZSAU_PROCEEDING, 608, 0, {ACT_ERROR}},//DELETE
  283. {RSP_ZSAU, 608,608,ZSAU_PROCEEDING, 609, 5, {ACT_CMD+AT_DIAL}},
  284. {RSP_OK, 609,609, -1, 650, 0, {ACT_DIALING}},
  285. {RSP_ZVLS, 608,608, 17, -1,-1, {ACT_DEBUG}},
  286. {RSP_ZCTP, 609,609, -1, -1,-1, {ACT_DEBUG}},
  287. {RSP_ZCPN, 609,609, -1, -1,-1, {ACT_DEBUG}},
  288. {RSP_ERROR, 601,609, -1, 0, 0, {ACT_ABORTDIAL}},
  289. {EV_TIMEOUT, 601,609, -1, 0, 0, {ACT_ABORTDIAL}},
  290. /* dialing */
  291. {RSP_ZCTP, 650,650, -1, -1,-1, {ACT_DEBUG}},
  292. {RSP_ZCPN, 650,650, -1, -1,-1, {ACT_DEBUG}},
  293. {RSP_ZSAU, 650,650,ZSAU_CALL_DELIVERED, -1,-1, {ACT_DEBUG}}, /* some devices don't send this */
  294. /* connection established */
  295. {RSP_ZSAU, 650,650,ZSAU_ACTIVE, 800,-1, {ACT_CONNECT}}, //FIXME -> DLE1
  296. {RSP_ZSAU, 750,750,ZSAU_ACTIVE, 800,-1, {ACT_CONNECT}}, //FIXME -> DLE1
  297. {EV_BC_OPEN, 800,800, -1, 800,-1, {ACT_NOTIFY_BC_UP}}, //FIXME new constate + timeout
  298. /* remote hangup */
  299. {RSP_ZSAU, 650,650,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEREJECT}},
  300. {RSP_ZSAU, 750,750,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP}},
  301. {RSP_ZSAU, 800,800,ZSAU_DISCONNECT_IND, 0, 0, {ACT_REMOTEHUP}},
  302. /* hangup */
  303. {EV_HUP, -1, -1, -1, -1,-1, {ACT_HUP}}, //FIXME
  304. {RSP_INIT, -1, -1,SEQ_HUP, 401, 5, {0}, "+VLS=0\r"}, /* hang up */ //-1,-1?
  305. {RSP_OK, 401,401, -1, 402, 5},
  306. {RSP_ZVLS, 402,402, 0, 403, 5},
  307. {RSP_ZSAU, 403,403,ZSAU_DISCONNECT_REQ, -1,-1, {ACT_DEBUG}}, /* if not remote hup */
  308. //{RSP_ZSAU, 403,403,ZSAU_NULL, 401, 0, {ACT_ERROR}}, //DELETE//FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
  309. {RSP_ZSAU, 403,403,ZSAU_NULL, 0, 0, {ACT_DISCONNECT}}, //FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
  310. {RSP_NODEV, 401,403, -1, 0, 0, {ACT_FAKEHUP}}, //FIXME -> DLE0 // should we do this _before_ hanging up for base driver?
  311. {RSP_ERROR, 401,401, -1, 0, 0, {ACT_ABORTHUP}},
  312. {EV_TIMEOUT, 401,403, -1, 0, 0, {ACT_ABORTHUP}},
  313. {EV_BC_CLOSED, 0, 0, -1, 0,-1, {ACT_NOTIFY_BC_DOWN}}, //FIXME new constate + timeout
  314. /* ring */
  315. {RSP_ZBC, 700,700, -1, -1,-1, {0}},
  316. {RSP_ZHLC, 700,700, -1, -1,-1, {0}},
  317. {RSP_NMBR, 700,700, -1, -1,-1, {0}},
  318. {RSP_ZCPN, 700,700, -1, -1,-1, {0}},
  319. {RSP_ZCTP, 700,700, -1, -1,-1, {0}},
  320. {EV_TIMEOUT, 700,700, -1, 720,720, {ACT_ICALL}},
  321. {EV_BC_CLOSED,720,720, -1, 0,-1, {ACT_NOTIFY_BC_DOWN}},
  322. /*accept icall*/
  323. {EV_ACCEPT, -1, -1, -1, -1,-1, {ACT_ACCEPT}}, //FIXME
  324. {RSP_INIT, 720,720,SEQ_ACCEPT, 721, 5, {ACT_CMD+AT_PROTO}},
  325. {RSP_OK, 721,721, -1, 722, 5, {ACT_CMD+AT_ISO}},
  326. {RSP_OK, 722,722, -1, 723, 5, {0}, "+VLS=17\r"}, /* set "Endgeraetemodus" */
  327. {RSP_OK, 723,723, -1, 724, 5, {0}},
  328. {RSP_ZVLS, 724,724, 17, 750,50, {ACT_ACCEPTED}},
  329. {RSP_ERROR, 721,729, -1, 0, 0, {ACT_ABORTACCEPT}},
  330. {EV_TIMEOUT, 721,729, -1, 0, 0, {ACT_ABORTACCEPT}},
  331. {RSP_ZSAU, 700,729,ZSAU_NULL, 0, 0, {ACT_ABORTACCEPT}},
  332. {RSP_ZSAU, 700,729,ZSAU_ACTIVE, 0, 0, {ACT_ABORTACCEPT}},
  333. {RSP_ZSAU, 700,729,ZSAU_DISCONNECT_IND, 0, 0, {ACT_ABORTACCEPT}},
  334. {EV_TIMEOUT, 750,750, -1, 0, 0, {ACT_CONNTIMEOUT}},
  335. /* misc. */
  336. {EV_PROTO_L2, -1, -1, -1, -1,-1, {ACT_PROTO_L2}}, //FIXME
  337. {RSP_ZCON, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  338. {RSP_ZCCR, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  339. {RSP_ZAOC, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  340. {RSP_ZCSTR, -1, -1, -1, -1,-1, {ACT_DEBUG}}, //FIXME
  341. {RSP_ZCAU, -1, -1, -1, -1,-1, {ACT_ZCAU}},
  342. {RSP_NONE, -1, -1, -1, -1,-1, {ACT_DEBUG}},
  343. {RSP_ANY, -1, -1, -1, -1,-1, {ACT_WARN}},
  344. {RSP_LAST}
  345. };
  346. #if 0
  347. static struct reply_t tab_nocid[]= /* no dle mode */ //FIXME aenderungen uebernehmen
  348. {
  349. /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
  350. {RSP_ANY, -1, -1, -1, -1,-1, ACT_WARN, NULL},
  351. {RSP_LAST,0,0,0,0,0,0}
  352. };
  353. static struct reply_t tab_cid[] = /* no dle mode */ //FIXME aenderungen uebernehmen
  354. {
  355. /* resp_code, min_ConState, max_ConState, parameter, new_ConState, timeout, action, command */
  356. {RSP_ANY, -1, -1, -1, -1,-1, ACT_WARN, NULL},
  357. {RSP_LAST,0,0,0,0,0,0}
  358. };
  359. #endif
  360. static struct resp_type_t resp_type[]=
  361. {
  362. /*{"", RSP_EMPTY, RT_NOTHING},*/
  363. {"OK", RSP_OK, RT_NOTHING},
  364. {"ERROR", RSP_ERROR, RT_NOTHING},
  365. {"ZSAU", RSP_ZSAU, RT_ZSAU},
  366. {"ZCAU", RSP_ZCAU, RT_ZCAU},
  367. {"RING", RSP_RING, RT_RING},
  368. {"ZGCI", RSP_ZGCI, RT_NUMBER},
  369. {"ZVLS", RSP_ZVLS, RT_NUMBER},
  370. {"ZCTP", RSP_ZCTP, RT_NUMBER},
  371. {"ZDLE", RSP_ZDLE, RT_NUMBER},
  372. {"ZCFGT", RSP_ZCFGT, RT_NUMBER},
  373. {"ZCCR", RSP_ZCCR, RT_NUMBER},
  374. {"ZMWI", RSP_ZMWI, RT_NUMBER},
  375. {"ZHLC", RSP_ZHLC, RT_STRING},
  376. {"ZBC", RSP_ZBC, RT_STRING},
  377. {"NMBR", RSP_NMBR, RT_STRING},
  378. {"ZCPN", RSP_ZCPN, RT_STRING},
  379. {"ZCON", RSP_ZCON, RT_STRING},
  380. {"ZAOC", RSP_ZAOC, RT_STRING},
  381. {"ZCSTR", RSP_ZCSTR, RT_STRING},
  382. {"ZCFG", RSP_ZCFG, RT_HEX},
  383. {"ZLOG", RSP_ZLOG, RT_NOTHING},
  384. {"ZABINFO", RSP_ZABINFO, RT_NOTHING},
  385. {"ZSMLSTCHG", RSP_ZSMLSTCHG, RT_NOTHING},
  386. {NULL,0,0}
  387. };
  388. /*
  389. * Get integer from char-pointer
  390. */
  391. static int isdn_getnum(char *p)
  392. {
  393. int v = -1;
  394. IFNULLRETVAL(p, -1);
  395. dbg(DEBUG_TRANSCMD, "string: %s", p);
  396. while (*p >= '0' && *p <= '9')
  397. v = ((v < 0) ? 0 : (v * 10)) + (int) ((*p++) - '0');
  398. if (*p)
  399. v = -1; /* invalid Character */
  400. return v;
  401. }
  402. /*
  403. * Get integer from char-pointer
  404. */
  405. static int isdn_gethex(char *p)
  406. {
  407. int v = 0;
  408. int c;
  409. IFNULLRETVAL(p, -1);
  410. dbg(DEBUG_TRANSCMD, "string: %s", p);
  411. if (!*p)
  412. return -1;
  413. do {
  414. if (v > (INT_MAX - 15) / 16)
  415. return -1;
  416. c = *p;
  417. if (c >= '0' && c <= '9')
  418. c -= '0';
  419. else if (c >= 'a' && c <= 'f')
  420. c -= 'a' - 10;
  421. else if (c >= 'A' && c <= 'F')
  422. c -= 'A' - 10;
  423. else
  424. return -1;
  425. v = v * 16 + c;
  426. } while (*++p);
  427. return v;
  428. }
  429. static inline void new_index(atomic_t *index, int max)
  430. {
  431. if (atomic_read(index) == max) //FIXME race?
  432. atomic_set(index, 0);
  433. else
  434. atomic_inc(index);
  435. }
  436. /* retrieve CID from parsed response
  437. * returns 0 if no CID, -1 if invalid CID, or CID value 1..65535
  438. */
  439. static int cid_of_response(char *s)
  440. {
  441. int cid;
  442. if (s[-1] != ';')
  443. return 0; /* no CID separator */
  444. cid = isdn_getnum(s);
  445. if (cid < 0)
  446. return 0; /* CID not numeric */
  447. if (cid < 1 || cid > 65535)
  448. return -1; /* CID out of range */
  449. return cid;
  450. //FIXME is ;<digit>+ at end of non-CID response really impossible?
  451. }
  452. /* This function will be called via task queue from the callback handler.
  453. * We received a modem response and have to handle it..
  454. */
  455. void gigaset_handle_modem_response(struct cardstate *cs)
  456. {
  457. unsigned char *argv[MAX_REC_PARAMS + 1];
  458. int params;
  459. int i, j;
  460. struct resp_type_t *rt;
  461. int curarg;
  462. unsigned long flags;
  463. unsigned next, tail, head;
  464. struct event_t *event;
  465. int resp_code;
  466. int param_type;
  467. int abort;
  468. size_t len;
  469. int cid;
  470. int rawstring;
  471. IFNULLRET(cs);
  472. len = cs->cbytes;
  473. if (!len) {
  474. /* ignore additional LFs/CRs (M10x config mode or cx100) */
  475. dbg(DEBUG_MCMD, "skipped EOL [%02X]", cs->respdata[len]);
  476. return;
  477. }
  478. cs->respdata[len] = 0;
  479. dbg(DEBUG_TRANSCMD, "raw string: '%s'", cs->respdata);
  480. argv[0] = cs->respdata;
  481. params = 1;
  482. if (cs->at_state.getstring) {
  483. /* getstring only allowed without cid at the moment */
  484. cs->at_state.getstring = 0;
  485. rawstring = 1;
  486. cid = 0;
  487. } else {
  488. /* parse line */
  489. for (i = 0; i < len; i++)
  490. switch (cs->respdata[i]) {
  491. case ';':
  492. case ',':
  493. case '=':
  494. if (params > MAX_REC_PARAMS) {
  495. warn("too many parameters in response");
  496. /* need last parameter (might be CID) */
  497. params--;
  498. }
  499. argv[params++] = cs->respdata + i + 1;
  500. }
  501. rawstring = 0;
  502. cid = params > 1 ? cid_of_response(argv[params-1]) : 0;
  503. if (cid < 0) {
  504. gigaset_add_event(cs, &cs->at_state, RSP_INVAL,
  505. NULL, 0, NULL);
  506. return;
  507. }
  508. for (j = 1; j < params; ++j)
  509. argv[j][-1] = 0;
  510. dbg(DEBUG_TRANSCMD, "CMD received: %s", argv[0]);
  511. if (cid) {
  512. --params;
  513. dbg(DEBUG_TRANSCMD, "CID: %s", argv[params]);
  514. }
  515. dbg(DEBUG_TRANSCMD, "available params: %d", params - 1);
  516. for (j = 1; j < params; j++)
  517. dbg(DEBUG_TRANSCMD, "param %d: %s", j, argv[j]);
  518. }
  519. spin_lock_irqsave(&cs->ev_lock, flags);
  520. head = atomic_read(&cs->ev_head);
  521. tail = atomic_read(&cs->ev_tail);
  522. abort = 1;
  523. curarg = 0;
  524. while (curarg < params) {
  525. next = (tail + 1) % MAX_EVENTS;
  526. if (unlikely(next == head)) {
  527. err("event queue full");
  528. break;
  529. }
  530. event = cs->events + tail;
  531. event->at_state = NULL;
  532. event->cid = cid;
  533. event->ptr = NULL;
  534. event->arg = NULL;
  535. tail = next;
  536. if (rawstring) {
  537. resp_code = RSP_STRING;
  538. param_type = RT_STRING;
  539. } else {
  540. for (rt = resp_type; rt->response; ++rt)
  541. if (!strcmp(argv[curarg], rt->response))
  542. break;
  543. if (!rt->response) {
  544. event->type = RSP_UNKNOWN;
  545. warn("unknown modem response: %s",
  546. argv[curarg]);
  547. break;
  548. }
  549. resp_code = rt->resp_code;
  550. param_type = rt->type;
  551. ++curarg;
  552. }
  553. event->type = resp_code;
  554. switch (param_type) {
  555. case RT_NOTHING:
  556. break;
  557. case RT_RING:
  558. if (!cid) {
  559. err("received RING without CID!");
  560. event->type = RSP_INVAL;
  561. abort = 1;
  562. } else {
  563. event->cid = 0;
  564. event->parameter = cid;
  565. abort = 0;
  566. }
  567. break;
  568. case RT_ZSAU:
  569. if (curarg >= params) {
  570. event->parameter = ZSAU_NONE;
  571. break;
  572. }
  573. if (!strcmp(argv[curarg], "OUTGOING_CALL_PROCEEDING"))
  574. event->parameter = ZSAU_OUTGOING_CALL_PROCEEDING;
  575. else if (!strcmp(argv[curarg], "CALL_DELIVERED"))
  576. event->parameter = ZSAU_CALL_DELIVERED;
  577. else if (!strcmp(argv[curarg], "ACTIVE"))
  578. event->parameter = ZSAU_ACTIVE;
  579. else if (!strcmp(argv[curarg], "DISCONNECT_IND"))
  580. event->parameter = ZSAU_DISCONNECT_IND;
  581. else if (!strcmp(argv[curarg], "NULL"))
  582. event->parameter = ZSAU_NULL;
  583. else if (!strcmp(argv[curarg], "DISCONNECT_REQ"))
  584. event->parameter = ZSAU_DISCONNECT_REQ;
  585. else {
  586. event->parameter = ZSAU_UNKNOWN;
  587. warn("%s: unknown parameter %s after ZSAU",
  588. __func__, argv[curarg]);
  589. }
  590. ++curarg;
  591. break;
  592. case RT_STRING:
  593. if (curarg < params) {
  594. len = strlen(argv[curarg]) + 1;
  595. event->ptr = kmalloc(len, GFP_ATOMIC);
  596. if (event->ptr)
  597. memcpy(event->ptr, argv[curarg], len);
  598. else
  599. err("no memory for string!");
  600. ++curarg;
  601. }
  602. #ifdef CONFIG_GIGASET_DEBUG
  603. if (!event->ptr)
  604. dbg(DEBUG_CMD, "string==NULL");
  605. else
  606. dbg(DEBUG_CMD,
  607. "string==%s", (char *) event->ptr);
  608. #endif
  609. break;
  610. case RT_ZCAU:
  611. event->parameter = -1;
  612. if (curarg + 1 < params) {
  613. i = isdn_gethex(argv[curarg]);
  614. j = isdn_gethex(argv[curarg + 1]);
  615. if (i >= 0 && i < 256 && j >= 0 && j < 256)
  616. event->parameter = (unsigned) i << 8
  617. | j;
  618. curarg += 2;
  619. } else
  620. curarg = params - 1;
  621. break;
  622. case RT_NUMBER:
  623. case RT_HEX:
  624. if (curarg < params) {
  625. if (param_type == RT_HEX)
  626. event->parameter =
  627. isdn_gethex(argv[curarg]);
  628. else
  629. event->parameter =
  630. isdn_getnum(argv[curarg]);
  631. ++curarg;
  632. } else
  633. event->parameter = -1;
  634. #ifdef CONFIG_GIGASET_DEBUG
  635. dbg(DEBUG_CMD, "parameter==%d", event->parameter);
  636. #endif
  637. break;
  638. }
  639. if (resp_code == RSP_ZDLE)
  640. cs->dle = event->parameter;
  641. if (abort)
  642. break;
  643. }
  644. atomic_set(&cs->ev_tail, tail);
  645. spin_unlock_irqrestore(&cs->ev_lock, flags);
  646. if (curarg != params)
  647. dbg(DEBUG_ANY, "invalid number of processed parameters: %d/%d",
  648. curarg, params);
  649. }
  650. EXPORT_SYMBOL_GPL(gigaset_handle_modem_response);
  651. /* disconnect
  652. * process closing of connection associated with given AT state structure
  653. */
  654. static void disconnect(struct at_state_t **at_state_p)
  655. {
  656. unsigned long flags;
  657. struct bc_state *bcs;
  658. struct cardstate *cs;
  659. IFNULLRET(at_state_p);
  660. IFNULLRET(*at_state_p);
  661. bcs = (*at_state_p)->bcs;
  662. cs = (*at_state_p)->cs;
  663. IFNULLRET(cs);
  664. new_index(&(*at_state_p)->seq_index, MAX_SEQ_INDEX);
  665. /* revert to selected idle mode */
  666. if (!atomic_read(&cs->cidmode)) {
  667. cs->at_state.pending_commands |= PC_UMMODE;
  668. atomic_set(&cs->commands_pending, 1); //FIXME
  669. dbg(DEBUG_CMD, "Scheduling PC_UMMODE");
  670. }
  671. if (bcs) {
  672. /* B channel assigned: invoke hardware specific handler */
  673. cs->ops->close_bchannel(bcs);
  674. } else {
  675. /* no B channel assigned: just deallocate */
  676. spin_lock_irqsave(&cs->lock, flags);
  677. list_del(&(*at_state_p)->list);
  678. kfree(*at_state_p);
  679. *at_state_p = NULL;
  680. spin_unlock_irqrestore(&cs->lock, flags);
  681. }
  682. }
  683. /* get_free_channel
  684. * get a free AT state structure: either one of those associated with the
  685. * B channels of the Gigaset device, or if none of those is available,
  686. * a newly allocated one with bcs=NULL
  687. * The structure should be freed by calling disconnect() after use.
  688. */
  689. static inline struct at_state_t *get_free_channel(struct cardstate *cs,
  690. int cid)
  691. /* cids: >0: siemens-cid
  692. 0: without cid
  693. -1: no cid assigned yet
  694. */
  695. {
  696. unsigned long flags;
  697. int i;
  698. struct at_state_t *ret;
  699. for (i = 0; i < cs->channels; ++i)
  700. if (gigaset_get_channel(cs->bcs + i)) {
  701. ret = &cs->bcs[i].at_state;
  702. ret->cid = cid;
  703. return ret;
  704. }
  705. spin_lock_irqsave(&cs->lock, flags);
  706. ret = kmalloc(sizeof(struct at_state_t), GFP_ATOMIC);
  707. if (ret) {
  708. gigaset_at_init(ret, NULL, cs, cid);
  709. list_add(&ret->list, &cs->temp_at_states);
  710. }
  711. spin_unlock_irqrestore(&cs->lock, flags);
  712. return ret;
  713. }
  714. static void init_failed(struct cardstate *cs, int mode)
  715. {
  716. int i;
  717. struct at_state_t *at_state;
  718. cs->at_state.pending_commands &= ~PC_INIT;
  719. atomic_set(&cs->mode, mode);
  720. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  721. gigaset_free_channels(cs);
  722. for (i = 0; i < cs->channels; ++i) {
  723. at_state = &cs->bcs[i].at_state;
  724. if (at_state->pending_commands & PC_CID) {
  725. at_state->pending_commands &= ~PC_CID;
  726. at_state->pending_commands |= PC_NOCID;
  727. atomic_set(&cs->commands_pending, 1);
  728. }
  729. }
  730. }
  731. static void schedule_init(struct cardstate *cs, int state)
  732. {
  733. if (cs->at_state.pending_commands & PC_INIT) {
  734. dbg(DEBUG_CMD, "not scheduling PC_INIT again");
  735. return;
  736. }
  737. atomic_set(&cs->mstate, state);
  738. atomic_set(&cs->mode, M_UNKNOWN);
  739. gigaset_block_channels(cs);
  740. cs->at_state.pending_commands |= PC_INIT;
  741. atomic_set(&cs->commands_pending, 1);
  742. dbg(DEBUG_CMD, "Scheduling PC_INIT");
  743. }
  744. /* Add "AT" to a command, add the cid, dle encode it, send the result to the hardware. */
  745. static void send_command(struct cardstate *cs, const char *cmd, int cid,
  746. int dle, gfp_t kmallocflags)
  747. {
  748. size_t cmdlen, buflen;
  749. char *cmdpos, *cmdbuf, *cmdtail;
  750. cmdlen = strlen(cmd);
  751. buflen = 11 + cmdlen;
  752. if (likely(buflen > cmdlen)) {
  753. cmdbuf = kmalloc(buflen, kmallocflags);
  754. if (likely(cmdbuf != NULL)) {
  755. cmdpos = cmdbuf + 9;
  756. cmdtail = cmdpos + cmdlen;
  757. memcpy(cmdpos, cmd, cmdlen);
  758. if (cid > 0 && cid <= 65535) {
  759. do {
  760. *--cmdpos = '0' + cid % 10;
  761. cid /= 10;
  762. ++cmdlen;
  763. } while (cid);
  764. }
  765. cmdlen += 2;
  766. *--cmdpos = 'T';
  767. *--cmdpos = 'A';
  768. if (dle) {
  769. cmdlen += 4;
  770. *--cmdpos = '(';
  771. *--cmdpos = 0x10;
  772. *cmdtail++ = 0x10;
  773. *cmdtail++ = ')';
  774. }
  775. cs->ops->write_cmd(cs, cmdpos, cmdlen, NULL);
  776. kfree(cmdbuf);
  777. } else
  778. err("no memory for command buffer");
  779. } else
  780. err("overflow in buflen");
  781. }
  782. static struct at_state_t *at_state_from_cid(struct cardstate *cs, int cid)
  783. {
  784. struct at_state_t *at_state;
  785. int i;
  786. unsigned long flags;
  787. if (cid == 0)
  788. return &cs->at_state;
  789. for (i = 0; i < cs->channels; ++i)
  790. if (cid == cs->bcs[i].at_state.cid)
  791. return &cs->bcs[i].at_state;
  792. spin_lock_irqsave(&cs->lock, flags);
  793. list_for_each_entry(at_state, &cs->temp_at_states, list)
  794. if (cid == at_state->cid) {
  795. spin_unlock_irqrestore(&cs->lock, flags);
  796. return at_state;
  797. }
  798. spin_unlock_irqrestore(&cs->lock, flags);
  799. return NULL;
  800. }
  801. static void bchannel_down(struct bc_state *bcs)
  802. {
  803. IFNULLRET(bcs);
  804. IFNULLRET(bcs->cs);
  805. if (bcs->chstate & CHS_B_UP) {
  806. bcs->chstate &= ~CHS_B_UP;
  807. gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BHUP);
  808. }
  809. if (bcs->chstate & (CHS_D_UP | CHS_NOTIFY_LL)) {
  810. bcs->chstate &= ~(CHS_D_UP | CHS_NOTIFY_LL);
  811. gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DHUP);
  812. }
  813. gigaset_free_channel(bcs);
  814. gigaset_bcs_reinit(bcs);
  815. }
  816. static void bchannel_up(struct bc_state *bcs)
  817. {
  818. IFNULLRET(bcs);
  819. if (!(bcs->chstate & CHS_D_UP)) {
  820. notice("%s: D channel not up", __func__);
  821. bcs->chstate |= CHS_D_UP;
  822. gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
  823. }
  824. if (bcs->chstate & CHS_B_UP) {
  825. notice("%s: B channel already up", __func__);
  826. return;
  827. }
  828. bcs->chstate |= CHS_B_UP;
  829. gigaset_i4l_channel_cmd(bcs, ISDN_STAT_BCONN);
  830. }
  831. static void start_dial(struct at_state_t *at_state, void *data, int seq_index)
  832. {
  833. struct bc_state *bcs = at_state->bcs;
  834. struct cardstate *cs = at_state->cs;
  835. int retval;
  836. bcs->chstate |= CHS_NOTIFY_LL;
  837. //atomic_set(&bcs->status, BCS_INIT);
  838. if (atomic_read(&at_state->seq_index) != seq_index)
  839. goto error;
  840. retval = gigaset_isdn_setup_dial(at_state, data);
  841. if (retval != 0)
  842. goto error;
  843. at_state->pending_commands |= PC_CID;
  844. dbg(DEBUG_CMD, "Scheduling PC_CID");
  845. //#ifdef GIG_MAYINITONDIAL
  846. // if (atomic_read(&cs->MState) == MS_UNKNOWN) {
  847. // cs->at_state.pending_commands |= PC_INIT;
  848. // dbg(DEBUG_CMD, "Scheduling PC_INIT");
  849. // }
  850. //#endif
  851. atomic_set(&cs->commands_pending, 1); //FIXME
  852. return;
  853. error:
  854. at_state->pending_commands |= PC_NOCID;
  855. dbg(DEBUG_CMD, "Scheduling PC_NOCID");
  856. atomic_set(&cs->commands_pending, 1); //FIXME
  857. return;
  858. }
  859. static void start_accept(struct at_state_t *at_state)
  860. {
  861. struct cardstate *cs = at_state->cs;
  862. int retval;
  863. retval = gigaset_isdn_setup_accept(at_state);
  864. if (retval == 0) {
  865. at_state->pending_commands |= PC_ACCEPT;
  866. dbg(DEBUG_CMD, "Scheduling PC_ACCEPT");
  867. atomic_set(&cs->commands_pending, 1); //FIXME
  868. } else {
  869. //FIXME
  870. at_state->pending_commands |= PC_HUP;
  871. dbg(DEBUG_CMD, "Scheduling PC_HUP");
  872. atomic_set(&cs->commands_pending, 1); //FIXME
  873. }
  874. }
  875. static void do_start(struct cardstate *cs)
  876. {
  877. gigaset_free_channels(cs);
  878. if (atomic_read(&cs->mstate) != MS_LOCKED)
  879. schedule_init(cs, MS_INIT);
  880. gigaset_i4l_cmd(cs, ISDN_STAT_RUN);
  881. // FIXME: not in locked mode
  882. // FIXME 2: only after init sequence
  883. cs->waiting = 0;
  884. wake_up(&cs->waitqueue);
  885. }
  886. static void finish_shutdown(struct cardstate *cs)
  887. {
  888. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  889. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  890. atomic_set(&cs->mode, M_UNKNOWN);
  891. }
  892. /* The rest is done by cleanup_cs () in user mode. */
  893. cs->cmd_result = -ENODEV;
  894. cs->waiting = 0;
  895. wake_up_interruptible(&cs->waitqueue);
  896. }
  897. static void do_shutdown(struct cardstate *cs)
  898. {
  899. gigaset_block_channels(cs);
  900. if (atomic_read(&cs->mstate) == MS_READY) {
  901. atomic_set(&cs->mstate, MS_SHUTDOWN);
  902. cs->at_state.pending_commands |= PC_SHUTDOWN;
  903. atomic_set(&cs->commands_pending, 1); //FIXME
  904. dbg(DEBUG_CMD, "Scheduling PC_SHUTDOWN"); //FIXME
  905. //gigaset_schedule_event(cs); //FIXME
  906. } else
  907. finish_shutdown(cs);
  908. }
  909. static void do_stop(struct cardstate *cs)
  910. {
  911. do_shutdown(cs);
  912. }
  913. /* Entering cid mode or getting a cid failed:
  914. * try to initialize the device and try again.
  915. *
  916. * channel >= 0: getting cid for the channel failed
  917. * channel < 0: entering cid mode failed
  918. *
  919. * returns 0 on failure
  920. */
  921. static int reinit_and_retry(struct cardstate *cs, int channel)
  922. {
  923. int i;
  924. if (--cs->retry_count <= 0)
  925. return 0;
  926. for (i = 0; i < cs->channels; ++i)
  927. if (cs->bcs[i].at_state.cid > 0)
  928. return 0;
  929. if (channel < 0)
  930. warn("Could not enter cid mode. Reinit device and try again.");
  931. else {
  932. warn("Could not get a call id. Reinit device and try again.");
  933. cs->bcs[channel].at_state.pending_commands |= PC_CID;
  934. }
  935. schedule_init(cs, MS_INIT);
  936. return 1;
  937. }
  938. static int at_state_invalid(struct cardstate *cs,
  939. struct at_state_t *test_ptr)
  940. {
  941. unsigned long flags;
  942. unsigned channel;
  943. struct at_state_t *at_state;
  944. int retval = 0;
  945. spin_lock_irqsave(&cs->lock, flags);
  946. if (test_ptr == &cs->at_state)
  947. goto exit;
  948. list_for_each_entry(at_state, &cs->temp_at_states, list)
  949. if (at_state == test_ptr)
  950. goto exit;
  951. for (channel = 0; channel < cs->channels; ++channel)
  952. if (&cs->bcs[channel].at_state == test_ptr)
  953. goto exit;
  954. retval = 1;
  955. exit:
  956. spin_unlock_irqrestore(&cs->lock, flags);
  957. return retval;
  958. }
  959. static void handle_icall(struct cardstate *cs, struct bc_state *bcs,
  960. struct at_state_t **p_at_state)
  961. {
  962. int retval;
  963. struct at_state_t *at_state = *p_at_state;
  964. retval = gigaset_isdn_icall(at_state);
  965. switch (retval) {
  966. case ICALL_ACCEPT:
  967. break;
  968. default:
  969. err("internal error: disposition=%d", retval);
  970. /* --v-- fall through --v-- */
  971. case ICALL_IGNORE:
  972. case ICALL_REJECT:
  973. /* hang up actively
  974. * Device doc says that would reject the call.
  975. * In fact it doesn't.
  976. */
  977. at_state->pending_commands |= PC_HUP;
  978. atomic_set(&cs->commands_pending, 1);
  979. break;
  980. }
  981. }
  982. static int do_lock(struct cardstate *cs)
  983. {
  984. int mode;
  985. int i;
  986. switch (atomic_read(&cs->mstate)) {
  987. case MS_UNINITIALIZED:
  988. case MS_READY:
  989. if (cs->cur_at_seq || !list_empty(&cs->temp_at_states) ||
  990. cs->at_state.pending_commands)
  991. return -EBUSY;
  992. for (i = 0; i < cs->channels; ++i)
  993. if (cs->bcs[i].at_state.pending_commands)
  994. return -EBUSY;
  995. if (!gigaset_get_channels(cs))
  996. return -EBUSY;
  997. break;
  998. case MS_LOCKED:
  999. //retval = -EACCES;
  1000. break;
  1001. default:
  1002. return -EBUSY;
  1003. }
  1004. mode = atomic_read(&cs->mode);
  1005. atomic_set(&cs->mstate, MS_LOCKED);
  1006. atomic_set(&cs->mode, M_UNKNOWN);
  1007. //FIXME reset card state / at states / bcs states
  1008. return mode;
  1009. }
  1010. static int do_unlock(struct cardstate *cs)
  1011. {
  1012. if (atomic_read(&cs->mstate) != MS_LOCKED)
  1013. return -EINVAL;
  1014. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  1015. atomic_set(&cs->mode, M_UNKNOWN);
  1016. gigaset_free_channels(cs);
  1017. //FIXME reset card state / at states / bcs states
  1018. if (atomic_read(&cs->connected))
  1019. schedule_init(cs, MS_INIT);
  1020. return 0;
  1021. }
  1022. static void do_action(int action, struct cardstate *cs,
  1023. struct bc_state *bcs,
  1024. struct at_state_t **p_at_state, char **pp_command,
  1025. int *p_genresp, int *p_resp_code,
  1026. struct event_t *ev)
  1027. {
  1028. struct at_state_t *at_state = *p_at_state;
  1029. struct at_state_t *at_state2;
  1030. unsigned long flags;
  1031. int channel;
  1032. unsigned char *s, *e;
  1033. int i;
  1034. unsigned long val;
  1035. switch (action) {
  1036. case ACT_NOTHING:
  1037. break;
  1038. case ACT_TIMEOUT:
  1039. at_state->waiting = 1;
  1040. break;
  1041. case ACT_INIT:
  1042. //FIXME setup everything
  1043. cs->at_state.pending_commands &= ~PC_INIT;
  1044. cs->cur_at_seq = SEQ_NONE;
  1045. atomic_set(&cs->mode, M_UNIMODEM);
  1046. if (!atomic_read(&cs->cidmode)) {
  1047. gigaset_free_channels(cs);
  1048. atomic_set(&cs->mstate, MS_READY);
  1049. break;
  1050. }
  1051. cs->at_state.pending_commands |= PC_CIDMODE;
  1052. atomic_set(&cs->commands_pending, 1); //FIXME
  1053. dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
  1054. break;
  1055. case ACT_FAILINIT:
  1056. warn("Could not initialize the device.");
  1057. cs->dle = 0;
  1058. init_failed(cs, M_UNKNOWN);
  1059. cs->cur_at_seq = SEQ_NONE;
  1060. break;
  1061. case ACT_CONFIGMODE:
  1062. init_failed(cs, M_CONFIG);
  1063. cs->cur_at_seq = SEQ_NONE;
  1064. break;
  1065. case ACT_SETDLE1:
  1066. cs->dle = 1;
  1067. /* cs->inbuf[0].inputstate |= INS_command | INS_DLE_command; */
  1068. cs->inbuf[0].inputstate &=
  1069. ~(INS_command | INS_DLE_command);
  1070. break;
  1071. case ACT_SETDLE0:
  1072. cs->dle = 0;
  1073. cs->inbuf[0].inputstate =
  1074. (cs->inbuf[0].inputstate & ~INS_DLE_command)
  1075. | INS_command;
  1076. break;
  1077. case ACT_CMODESET:
  1078. if (atomic_read(&cs->mstate) == MS_INIT ||
  1079. atomic_read(&cs->mstate) == MS_RECOVER) {
  1080. gigaset_free_channels(cs);
  1081. atomic_set(&cs->mstate, MS_READY);
  1082. }
  1083. atomic_set(&cs->mode, M_CID);
  1084. cs->cur_at_seq = SEQ_NONE;
  1085. break;
  1086. case ACT_UMODESET:
  1087. atomic_set(&cs->mode, M_UNIMODEM);
  1088. cs->cur_at_seq = SEQ_NONE;
  1089. break;
  1090. case ACT_FAILCMODE:
  1091. cs->cur_at_seq = SEQ_NONE;
  1092. if (atomic_read(&cs->mstate) == MS_INIT ||
  1093. atomic_read(&cs->mstate) == MS_RECOVER) {
  1094. init_failed(cs, M_UNKNOWN);
  1095. break;
  1096. }
  1097. if (!reinit_and_retry(cs, -1))
  1098. schedule_init(cs, MS_RECOVER);
  1099. break;
  1100. case ACT_FAILUMODE:
  1101. cs->cur_at_seq = SEQ_NONE;
  1102. schedule_init(cs, MS_RECOVER);
  1103. break;
  1104. case ACT_HUPMODEM:
  1105. /* send "+++" (hangup in unimodem mode) */
  1106. cs->ops->write_cmd(cs, "+++", 3, NULL);
  1107. break;
  1108. case ACT_RING:
  1109. /* get fresh AT state structure for new CID */
  1110. at_state2 = get_free_channel(cs, ev->parameter);
  1111. if (!at_state2) {
  1112. warn("RING ignored: "
  1113. "could not allocate channel structure");
  1114. break;
  1115. }
  1116. /* initialize AT state structure
  1117. * note that bcs may be NULL if no B channel is free
  1118. */
  1119. at_state2->ConState = 700;
  1120. kfree(at_state2->str_var[STR_NMBR]);
  1121. at_state2->str_var[STR_NMBR] = NULL;
  1122. kfree(at_state2->str_var[STR_ZCPN]);
  1123. at_state2->str_var[STR_ZCPN] = NULL;
  1124. kfree(at_state2->str_var[STR_ZBC]);
  1125. at_state2->str_var[STR_ZBC] = NULL;
  1126. kfree(at_state2->str_var[STR_ZHLC]);
  1127. at_state2->str_var[STR_ZHLC] = NULL;
  1128. at_state2->int_var[VAR_ZCTP] = -1;
  1129. spin_lock_irqsave(&cs->lock, flags);
  1130. at_state2->timer_expires = RING_TIMEOUT;
  1131. at_state2->timer_active = 1;
  1132. spin_unlock_irqrestore(&cs->lock, flags);
  1133. break;
  1134. case ACT_ICALL:
  1135. handle_icall(cs, bcs, p_at_state);
  1136. at_state = *p_at_state;
  1137. break;
  1138. case ACT_FAILSDOWN:
  1139. warn("Could not shut down the device.");
  1140. /* fall through */
  1141. case ACT_FAKESDOWN:
  1142. case ACT_SDOWN:
  1143. cs->cur_at_seq = SEQ_NONE;
  1144. finish_shutdown(cs);
  1145. break;
  1146. case ACT_CONNECT:
  1147. if (cs->onechannel) {
  1148. at_state->pending_commands |= PC_DLE1;
  1149. atomic_set(&cs->commands_pending, 1);
  1150. break;
  1151. }
  1152. bcs->chstate |= CHS_D_UP;
  1153. gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
  1154. cs->ops->init_bchannel(bcs);
  1155. break;
  1156. case ACT_DLE1:
  1157. cs->cur_at_seq = SEQ_NONE;
  1158. bcs = cs->bcs + cs->curchannel;
  1159. bcs->chstate |= CHS_D_UP;
  1160. gigaset_i4l_channel_cmd(bcs, ISDN_STAT_DCONN);
  1161. cs->ops->init_bchannel(bcs);
  1162. break;
  1163. case ACT_FAKEHUP:
  1164. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  1165. /* fall through */
  1166. case ACT_DISCONNECT:
  1167. cs->cur_at_seq = SEQ_NONE;
  1168. at_state->cid = -1;
  1169. if (bcs && cs->onechannel && cs->dle) {
  1170. /* Check for other open channels not needed:
  1171. * DLE only used for M10x with one B channel.
  1172. */
  1173. at_state->pending_commands |= PC_DLE0;
  1174. atomic_set(&cs->commands_pending, 1);
  1175. } else {
  1176. disconnect(p_at_state);
  1177. at_state = *p_at_state;
  1178. }
  1179. break;
  1180. case ACT_FAKEDLE0:
  1181. at_state->int_var[VAR_ZDLE] = 0;
  1182. cs->dle = 0;
  1183. /* fall through */
  1184. case ACT_DLE0:
  1185. cs->cur_at_seq = SEQ_NONE;
  1186. at_state2 = &cs->bcs[cs->curchannel].at_state;
  1187. disconnect(&at_state2);
  1188. break;
  1189. case ACT_ABORTHUP:
  1190. cs->cur_at_seq = SEQ_NONE;
  1191. warn("Could not hang up.");
  1192. at_state->cid = -1;
  1193. if (bcs && cs->onechannel)
  1194. at_state->pending_commands |= PC_DLE0;
  1195. else {
  1196. disconnect(p_at_state);
  1197. at_state = *p_at_state;
  1198. }
  1199. schedule_init(cs, MS_RECOVER);
  1200. break;
  1201. case ACT_FAILDLE0:
  1202. cs->cur_at_seq = SEQ_NONE;
  1203. warn("Could not leave DLE mode.");
  1204. at_state2 = &cs->bcs[cs->curchannel].at_state;
  1205. disconnect(&at_state2);
  1206. schedule_init(cs, MS_RECOVER);
  1207. break;
  1208. case ACT_FAILDLE1:
  1209. cs->cur_at_seq = SEQ_NONE;
  1210. warn("Could not enter DLE mode. Try to hang up.");
  1211. channel = cs->curchannel;
  1212. cs->bcs[channel].at_state.pending_commands |= PC_HUP;
  1213. atomic_set(&cs->commands_pending, 1);
  1214. break;
  1215. case ACT_CID: /* got cid; start dialing */
  1216. cs->cur_at_seq = SEQ_NONE;
  1217. channel = cs->curchannel;
  1218. if (ev->parameter > 0 && ev->parameter <= 65535) {
  1219. cs->bcs[channel].at_state.cid = ev->parameter;
  1220. cs->bcs[channel].at_state.pending_commands |=
  1221. PC_DIAL;
  1222. atomic_set(&cs->commands_pending, 1);
  1223. break;
  1224. }
  1225. /* fall through */
  1226. case ACT_FAILCID:
  1227. cs->cur_at_seq = SEQ_NONE;
  1228. channel = cs->curchannel;
  1229. if (!reinit_and_retry(cs, channel)) {
  1230. warn("Could not get a call id. Dialing not possible");
  1231. at_state2 = &cs->bcs[channel].at_state;
  1232. disconnect(&at_state2);
  1233. }
  1234. break;
  1235. case ACT_ABORTCID:
  1236. cs->cur_at_seq = SEQ_NONE;
  1237. at_state2 = &cs->bcs[cs->curchannel].at_state;
  1238. disconnect(&at_state2);
  1239. break;
  1240. case ACT_DIALING:
  1241. case ACT_ACCEPTED:
  1242. cs->cur_at_seq = SEQ_NONE;
  1243. break;
  1244. case ACT_ABORTACCEPT: /* hangup/error/timeout during ICALL processing */
  1245. disconnect(p_at_state);
  1246. at_state = *p_at_state;
  1247. break;
  1248. case ACT_ABORTDIAL: /* error/timeout during dial preparation */
  1249. cs->cur_at_seq = SEQ_NONE;
  1250. at_state->pending_commands |= PC_HUP;
  1251. atomic_set(&cs->commands_pending, 1);
  1252. break;
  1253. case ACT_REMOTEREJECT: /* DISCONNECT_IND after dialling */
  1254. case ACT_CONNTIMEOUT: /* timeout waiting for ZSAU=ACTIVE */
  1255. case ACT_REMOTEHUP: /* DISCONNECT_IND with established connection */
  1256. at_state->pending_commands |= PC_HUP;
  1257. atomic_set(&cs->commands_pending, 1);
  1258. break;
  1259. case ACT_GETSTRING: /* warning: RING, ZDLE, ... are not handled properly any more */
  1260. at_state->getstring = 1;
  1261. break;
  1262. case ACT_SETVER:
  1263. if (!ev->ptr) {
  1264. *p_genresp = 1;
  1265. *p_resp_code = RSP_ERROR;
  1266. break;
  1267. }
  1268. s = ev->ptr;
  1269. if (!strcmp(s, "OK")) {
  1270. *p_genresp = 1;
  1271. *p_resp_code = RSP_ERROR;
  1272. break;
  1273. }
  1274. for (i = 0; i < 4; ++i) {
  1275. val = simple_strtoul(s, (char **) &e, 10);
  1276. if (val > INT_MAX || e == s)
  1277. break;
  1278. if (i == 3) {
  1279. if (*e)
  1280. break;
  1281. } else if (*e != '.')
  1282. break;
  1283. else
  1284. s = e + 1;
  1285. cs->fwver[i] = val;
  1286. }
  1287. if (i != 4) {
  1288. *p_genresp = 1;
  1289. *p_resp_code = RSP_ERROR;
  1290. break;
  1291. }
  1292. /*at_state->getstring = 1;*/
  1293. cs->gotfwver = 0;
  1294. break;
  1295. case ACT_GOTVER:
  1296. if (cs->gotfwver == 0) {
  1297. cs->gotfwver = 1;
  1298. dbg(DEBUG_ANY,
  1299. "firmware version %02d.%03d.%02d.%02d",
  1300. cs->fwver[0], cs->fwver[1],
  1301. cs->fwver[2], cs->fwver[3]);
  1302. break;
  1303. }
  1304. /* fall through */
  1305. case ACT_FAILVER:
  1306. cs->gotfwver = -1;
  1307. err("could not read firmware version.");
  1308. break;
  1309. #ifdef CONFIG_GIGASET_DEBUG
  1310. case ACT_ERROR:
  1311. *p_genresp = 1;
  1312. *p_resp_code = RSP_ERROR;
  1313. break;
  1314. case ACT_TEST:
  1315. {
  1316. static int count = 3; //2; //1;
  1317. *p_genresp = 1;
  1318. *p_resp_code = count ? RSP_ERROR : RSP_OK;
  1319. if (count > 0)
  1320. --count;
  1321. }
  1322. break;
  1323. #endif
  1324. case ACT_DEBUG:
  1325. dbg(DEBUG_ANY, "%s: resp_code %d in ConState %d",
  1326. __func__, ev->type, at_state->ConState);
  1327. break;
  1328. case ACT_WARN:
  1329. warn("%s: resp_code %d in ConState %d!",
  1330. __func__, ev->type, at_state->ConState);
  1331. break;
  1332. case ACT_ZCAU:
  1333. warn("cause code %04x in connection state %d.",
  1334. ev->parameter, at_state->ConState);
  1335. break;
  1336. /* events from the LL */
  1337. case ACT_DIAL:
  1338. start_dial(at_state, ev->ptr, ev->parameter);
  1339. break;
  1340. case ACT_ACCEPT:
  1341. start_accept(at_state);
  1342. break;
  1343. case ACT_PROTO_L2:
  1344. dbg(DEBUG_CMD,
  1345. "set protocol to %u", (unsigned) ev->parameter);
  1346. at_state->bcs->proto2 = ev->parameter;
  1347. break;
  1348. case ACT_HUP:
  1349. at_state->pending_commands |= PC_HUP;
  1350. atomic_set(&cs->commands_pending, 1); //FIXME
  1351. dbg(DEBUG_CMD, "Scheduling PC_HUP");
  1352. break;
  1353. /* hotplug events */
  1354. case ACT_STOP:
  1355. do_stop(cs);
  1356. break;
  1357. case ACT_START:
  1358. do_start(cs);
  1359. break;
  1360. /* events from the interface */ // FIXME without ACT_xxxx?
  1361. case ACT_IF_LOCK:
  1362. cs->cmd_result = ev->parameter ? do_lock(cs) : do_unlock(cs);
  1363. cs->waiting = 0;
  1364. wake_up(&cs->waitqueue);
  1365. break;
  1366. case ACT_IF_VER:
  1367. if (ev->parameter != 0)
  1368. cs->cmd_result = -EINVAL;
  1369. else if (cs->gotfwver != 1) {
  1370. cs->cmd_result = -ENOENT;
  1371. } else {
  1372. memcpy(ev->arg, cs->fwver, sizeof cs->fwver);
  1373. cs->cmd_result = 0;
  1374. }
  1375. cs->waiting = 0;
  1376. wake_up(&cs->waitqueue);
  1377. break;
  1378. /* events from the proc file system */ // FIXME without ACT_xxxx?
  1379. case ACT_PROC_CIDMODE:
  1380. if (ev->parameter != atomic_read(&cs->cidmode)) {
  1381. atomic_set(&cs->cidmode, ev->parameter);
  1382. if (ev->parameter) {
  1383. cs->at_state.pending_commands |= PC_CIDMODE;
  1384. dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
  1385. } else {
  1386. cs->at_state.pending_commands |= PC_UMMODE;
  1387. dbg(DEBUG_CMD, "Scheduling PC_UMMODE");
  1388. }
  1389. atomic_set(&cs->commands_pending, 1);
  1390. }
  1391. cs->waiting = 0;
  1392. wake_up(&cs->waitqueue);
  1393. break;
  1394. /* events from the hardware drivers */
  1395. case ACT_NOTIFY_BC_DOWN:
  1396. bchannel_down(bcs);
  1397. break;
  1398. case ACT_NOTIFY_BC_UP:
  1399. bchannel_up(bcs);
  1400. break;
  1401. case ACT_SHUTDOWN:
  1402. do_shutdown(cs);
  1403. break;
  1404. default:
  1405. if (action >= ACT_CMD && action < ACT_CMD + AT_NUM) {
  1406. *pp_command = at_state->bcs->commands[action - ACT_CMD];
  1407. if (!*pp_command) {
  1408. *p_genresp = 1;
  1409. *p_resp_code = RSP_NULL;
  1410. }
  1411. } else
  1412. err("%s: action==%d!", __func__, action);
  1413. }
  1414. }
  1415. /* State machine to do the calling and hangup procedure */
  1416. static void process_event(struct cardstate *cs, struct event_t *ev)
  1417. {
  1418. struct bc_state *bcs;
  1419. char *p_command = NULL;
  1420. struct reply_t *rep;
  1421. int rcode;
  1422. int genresp = 0;
  1423. int resp_code = RSP_ERROR;
  1424. int sendcid;
  1425. struct at_state_t *at_state;
  1426. int index;
  1427. int curact;
  1428. unsigned long flags;
  1429. IFNULLRET(cs);
  1430. IFNULLRET(ev);
  1431. if (ev->cid >= 0) {
  1432. at_state = at_state_from_cid(cs, ev->cid);
  1433. if (!at_state) {
  1434. gigaset_add_event(cs, &cs->at_state, RSP_WRONG_CID,
  1435. NULL, 0, NULL);
  1436. return;
  1437. }
  1438. } else {
  1439. at_state = ev->at_state;
  1440. if (at_state_invalid(cs, at_state)) {
  1441. dbg(DEBUG_ANY,
  1442. "event for invalid at_state %p", at_state);
  1443. return;
  1444. }
  1445. }
  1446. dbg(DEBUG_CMD,
  1447. "connection state %d, event %d", at_state->ConState, ev->type);
  1448. bcs = at_state->bcs;
  1449. sendcid = at_state->cid;
  1450. /* Setting the pointer to the dial array */
  1451. rep = at_state->replystruct;
  1452. IFNULLRET(rep);
  1453. if (ev->type == EV_TIMEOUT) {
  1454. if (ev->parameter != atomic_read(&at_state->timer_index)
  1455. || !at_state->timer_active) {
  1456. ev->type = RSP_NONE; /* old timeout */
  1457. dbg(DEBUG_ANY, "old timeout");
  1458. } else if (!at_state->waiting)
  1459. dbg(DEBUG_ANY, "timeout occured");
  1460. else
  1461. dbg(DEBUG_ANY, "stopped waiting");
  1462. }
  1463. /* if the response belongs to a variable in at_state->int_var[VAR_XXXX] or at_state->str_var[STR_XXXX], set it */
  1464. if (ev->type >= RSP_VAR && ev->type < RSP_VAR + VAR_NUM) {
  1465. index = ev->type - RSP_VAR;
  1466. at_state->int_var[index] = ev->parameter;
  1467. } else if (ev->type >= RSP_STR && ev->type < RSP_STR + STR_NUM) {
  1468. index = ev->type - RSP_STR;
  1469. kfree(at_state->str_var[index]);
  1470. at_state->str_var[index] = ev->ptr;
  1471. ev->ptr = NULL; /* prevent process_events() from deallocating ptr */
  1472. }
  1473. if (ev->type == EV_TIMEOUT || ev->type == RSP_STRING)
  1474. at_state->getstring = 0;
  1475. /* Search row in dial array which matches modem response and current constate */
  1476. for (;; rep++) {
  1477. rcode = rep->resp_code;
  1478. /* dbg (DEBUG_ANY, "rcode %d", rcode); */
  1479. if (rcode == RSP_LAST) {
  1480. /* found nothing...*/
  1481. warn("%s: rcode=RSP_LAST: resp_code %d in ConState %d!",
  1482. __func__, ev->type, at_state->ConState);
  1483. return;
  1484. }
  1485. if ((rcode == RSP_ANY || rcode == ev->type)
  1486. && ((int) at_state->ConState >= rep->min_ConState)
  1487. && (rep->max_ConState < 0
  1488. || (int) at_state->ConState <= rep->max_ConState)
  1489. && (rep->parameter < 0 || rep->parameter == ev->parameter))
  1490. break;
  1491. }
  1492. p_command = rep->command;
  1493. at_state->waiting = 0;
  1494. for (curact = 0; curact < MAXACT; ++curact) {
  1495. /* The row tells us what we should do ..
  1496. */
  1497. do_action(rep->action[curact], cs, bcs, &at_state, &p_command, &genresp, &resp_code, ev);
  1498. if (!at_state)
  1499. break; /* may be freed after disconnect */
  1500. }
  1501. if (at_state) {
  1502. /* Jump to the next con-state regarding the array */
  1503. if (rep->new_ConState >= 0)
  1504. at_state->ConState = rep->new_ConState;
  1505. if (genresp) {
  1506. spin_lock_irqsave(&cs->lock, flags);
  1507. at_state->timer_expires = 0; //FIXME
  1508. at_state->timer_active = 0; //FIXME
  1509. spin_unlock_irqrestore(&cs->lock, flags);
  1510. gigaset_add_event(cs, at_state, resp_code, NULL, 0, NULL);
  1511. } else {
  1512. /* Send command to modem if not NULL... */
  1513. if (p_command/*rep->command*/) {
  1514. if (atomic_read(&cs->connected))
  1515. send_command(cs, p_command,
  1516. sendcid, cs->dle,
  1517. GFP_ATOMIC);
  1518. else
  1519. gigaset_add_event(cs, at_state,
  1520. RSP_NODEV,
  1521. NULL, 0, NULL);
  1522. }
  1523. spin_lock_irqsave(&cs->lock, flags);
  1524. if (!rep->timeout) {
  1525. at_state->timer_expires = 0;
  1526. at_state->timer_active = 0;
  1527. } else if (rep->timeout > 0) { /* new timeout */
  1528. at_state->timer_expires = rep->timeout * 10;
  1529. at_state->timer_active = 1;
  1530. new_index(&at_state->timer_index,
  1531. MAX_TIMER_INDEX);
  1532. }
  1533. spin_unlock_irqrestore(&cs->lock, flags);
  1534. }
  1535. }
  1536. }
  1537. static void schedule_sequence(struct cardstate *cs,
  1538. struct at_state_t *at_state, int sequence)
  1539. {
  1540. cs->cur_at_seq = sequence;
  1541. gigaset_add_event(cs, at_state, RSP_INIT, NULL, sequence, NULL);
  1542. }
  1543. static void process_command_flags(struct cardstate *cs)
  1544. {
  1545. struct at_state_t *at_state = NULL;
  1546. struct bc_state *bcs;
  1547. int i;
  1548. int sequence;
  1549. IFNULLRET(cs);
  1550. atomic_set(&cs->commands_pending, 0);
  1551. if (cs->cur_at_seq) {
  1552. dbg(DEBUG_CMD, "not searching scheduled commands: busy");
  1553. return;
  1554. }
  1555. dbg(DEBUG_CMD, "searching scheduled commands");
  1556. sequence = SEQ_NONE;
  1557. /* clear pending_commands and hangup channels on shutdown */
  1558. if (cs->at_state.pending_commands & PC_SHUTDOWN) {
  1559. cs->at_state.pending_commands &= ~PC_CIDMODE;
  1560. for (i = 0; i < cs->channels; ++i) {
  1561. bcs = cs->bcs + i;
  1562. at_state = &bcs->at_state;
  1563. at_state->pending_commands &=
  1564. ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
  1565. if (at_state->cid > 0)
  1566. at_state->pending_commands |= PC_HUP;
  1567. if (at_state->pending_commands & PC_CID) {
  1568. at_state->pending_commands |= PC_NOCID;
  1569. at_state->pending_commands &= ~PC_CID;
  1570. }
  1571. }
  1572. }
  1573. /* clear pending_commands and hangup channels on reset */
  1574. if (cs->at_state.pending_commands & PC_INIT) {
  1575. cs->at_state.pending_commands &= ~PC_CIDMODE;
  1576. for (i = 0; i < cs->channels; ++i) {
  1577. bcs = cs->bcs + i;
  1578. at_state = &bcs->at_state;
  1579. at_state->pending_commands &=
  1580. ~(PC_DLE1 | PC_ACCEPT | PC_DIAL);
  1581. if (at_state->cid > 0)
  1582. at_state->pending_commands |= PC_HUP;
  1583. if (atomic_read(&cs->mstate) == MS_RECOVER) {
  1584. if (at_state->pending_commands & PC_CID) {
  1585. at_state->pending_commands |= PC_NOCID;
  1586. at_state->pending_commands &= ~PC_CID;
  1587. }
  1588. }
  1589. }
  1590. }
  1591. /* only switch back to unimodem mode, if no commands are pending and no channels are up */
  1592. if (cs->at_state.pending_commands == PC_UMMODE
  1593. && !atomic_read(&cs->cidmode)
  1594. && list_empty(&cs->temp_at_states)
  1595. && atomic_read(&cs->mode) == M_CID) {
  1596. sequence = SEQ_UMMODE;
  1597. at_state = &cs->at_state;
  1598. for (i = 0; i < cs->channels; ++i) {
  1599. bcs = cs->bcs + i;
  1600. if (bcs->at_state.pending_commands ||
  1601. bcs->at_state.cid > 0) {
  1602. sequence = SEQ_NONE;
  1603. break;
  1604. }
  1605. }
  1606. }
  1607. cs->at_state.pending_commands &= ~PC_UMMODE;
  1608. if (sequence != SEQ_NONE) {
  1609. schedule_sequence(cs, at_state, sequence);
  1610. return;
  1611. }
  1612. for (i = 0; i < cs->channels; ++i) {
  1613. bcs = cs->bcs + i;
  1614. if (bcs->at_state.pending_commands & PC_HUP) {
  1615. bcs->at_state.pending_commands &= ~PC_HUP;
  1616. if (bcs->at_state.pending_commands & PC_CID) {
  1617. /* not yet dialing: PC_NOCID is sufficient */
  1618. bcs->at_state.pending_commands |= PC_NOCID;
  1619. bcs->at_state.pending_commands &= ~PC_CID;
  1620. } else {
  1621. schedule_sequence(cs, &bcs->at_state, SEQ_HUP);
  1622. return;
  1623. }
  1624. }
  1625. if (bcs->at_state.pending_commands & PC_NOCID) {
  1626. bcs->at_state.pending_commands &= ~PC_NOCID;
  1627. cs->curchannel = bcs->channel;
  1628. schedule_sequence(cs, &cs->at_state, SEQ_NOCID);
  1629. return;
  1630. } else if (bcs->at_state.pending_commands & PC_DLE0) {
  1631. bcs->at_state.pending_commands &= ~PC_DLE0;
  1632. cs->curchannel = bcs->channel;
  1633. schedule_sequence(cs, &cs->at_state, SEQ_DLE0);
  1634. return;
  1635. }
  1636. }
  1637. list_for_each_entry(at_state, &cs->temp_at_states, list)
  1638. if (at_state->pending_commands & PC_HUP) {
  1639. at_state->pending_commands &= ~PC_HUP;
  1640. schedule_sequence(cs, at_state, SEQ_HUP);
  1641. return;
  1642. }
  1643. if (cs->at_state.pending_commands & PC_INIT) {
  1644. cs->at_state.pending_commands &= ~PC_INIT;
  1645. cs->dle = 0; //FIXME
  1646. cs->inbuf->inputstate = INS_command;
  1647. //FIXME reset card state (or -> LOCK0)?
  1648. schedule_sequence(cs, &cs->at_state, SEQ_INIT);
  1649. return;
  1650. }
  1651. if (cs->at_state.pending_commands & PC_SHUTDOWN) {
  1652. cs->at_state.pending_commands &= ~PC_SHUTDOWN;
  1653. schedule_sequence(cs, &cs->at_state, SEQ_SHUTDOWN);
  1654. return;
  1655. }
  1656. if (cs->at_state.pending_commands & PC_CIDMODE) {
  1657. cs->at_state.pending_commands &= ~PC_CIDMODE;
  1658. if (atomic_read(&cs->mode) == M_UNIMODEM) {
  1659. #if 0
  1660. cs->retry_count = 2;
  1661. #else
  1662. cs->retry_count = 1;
  1663. #endif
  1664. schedule_sequence(cs, &cs->at_state, SEQ_CIDMODE);
  1665. return;
  1666. }
  1667. }
  1668. for (i = 0; i < cs->channels; ++i) {
  1669. bcs = cs->bcs + i;
  1670. if (bcs->at_state.pending_commands & PC_DLE1) {
  1671. bcs->at_state.pending_commands &= ~PC_DLE1;
  1672. cs->curchannel = bcs->channel;
  1673. schedule_sequence(cs, &cs->at_state, SEQ_DLE1);
  1674. return;
  1675. }
  1676. if (bcs->at_state.pending_commands & PC_ACCEPT) {
  1677. bcs->at_state.pending_commands &= ~PC_ACCEPT;
  1678. schedule_sequence(cs, &bcs->at_state, SEQ_ACCEPT);
  1679. return;
  1680. }
  1681. if (bcs->at_state.pending_commands & PC_DIAL) {
  1682. bcs->at_state.pending_commands &= ~PC_DIAL;
  1683. schedule_sequence(cs, &bcs->at_state, SEQ_DIAL);
  1684. return;
  1685. }
  1686. if (bcs->at_state.pending_commands & PC_CID) {
  1687. switch (atomic_read(&cs->mode)) {
  1688. case M_UNIMODEM:
  1689. cs->at_state.pending_commands |= PC_CIDMODE;
  1690. dbg(DEBUG_CMD, "Scheduling PC_CIDMODE");
  1691. atomic_set(&cs->commands_pending, 1);
  1692. return;
  1693. #ifdef GIG_MAYINITONDIAL
  1694. case M_UNKNOWN:
  1695. schedule_init(cs, MS_INIT);
  1696. return;
  1697. #endif
  1698. }
  1699. bcs->at_state.pending_commands &= ~PC_CID;
  1700. cs->curchannel = bcs->channel;
  1701. #ifdef GIG_RETRYCID
  1702. cs->retry_count = 2;
  1703. #else
  1704. cs->retry_count = 1;
  1705. #endif
  1706. schedule_sequence(cs, &cs->at_state, SEQ_CID);
  1707. return;
  1708. }
  1709. }
  1710. }
  1711. static void process_events(struct cardstate *cs)
  1712. {
  1713. struct event_t *ev;
  1714. unsigned head, tail;
  1715. int i;
  1716. int check_flags = 0;
  1717. int was_busy;
  1718. /* no locking needed (only one reader) */
  1719. head = atomic_read(&cs->ev_head);
  1720. for (i = 0; i < 2 * MAX_EVENTS; ++i) {
  1721. tail = atomic_read(&cs->ev_tail);
  1722. if (tail == head) {
  1723. if (!check_flags && !atomic_read(&cs->commands_pending))
  1724. break;
  1725. check_flags = 0;
  1726. process_command_flags(cs);
  1727. tail = atomic_read(&cs->ev_tail);
  1728. if (tail == head) {
  1729. if (!atomic_read(&cs->commands_pending))
  1730. break;
  1731. continue;
  1732. }
  1733. }
  1734. ev = cs->events + head;
  1735. was_busy = cs->cur_at_seq != SEQ_NONE;
  1736. process_event(cs, ev);
  1737. kfree(ev->ptr);
  1738. ev->ptr = NULL;
  1739. if (was_busy && cs->cur_at_seq == SEQ_NONE)
  1740. check_flags = 1;
  1741. head = (head + 1) % MAX_EVENTS;
  1742. atomic_set(&cs->ev_head, head);
  1743. }
  1744. if (i == 2 * MAX_EVENTS) {
  1745. err("infinite loop in process_events; aborting.");
  1746. }
  1747. }
  1748. /* tasklet scheduled on any event received from the Gigaset device
  1749. * parameter:
  1750. * data ISDN controller state structure
  1751. */
  1752. void gigaset_handle_event(unsigned long data)
  1753. {
  1754. struct cardstate *cs = (struct cardstate *) data;
  1755. IFNULLRET(cs);
  1756. IFNULLRET(cs->inbuf);
  1757. /* handle incoming data on control/common channel */
  1758. if (atomic_read(&cs->inbuf->head) != atomic_read(&cs->inbuf->tail)) {
  1759. dbg(DEBUG_INTR, "processing new data");
  1760. cs->ops->handle_input(cs->inbuf);
  1761. }
  1762. process_events(cs);
  1763. }