ev-layer.c 49 KB

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