ev-layer.c 48 KB

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