ev-layer.c 48 KB

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