ev-layer.c 52 KB

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