ev-layer.c 47 KB

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