ev-layer.c 47 KB

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