ev-layer.c 48 KB

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