ev-layer.c 47 KB

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