ev-layer.c 49 KB

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