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