ev-layer.c 55 KB

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