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