ev-layer.c 55 KB

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