card_fxo.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. /*
  2. * Written by Oron Peled <oron@actcom.co.il>
  3. * Copyright (C) 2004-2006, Xorcom
  4. *
  5. * All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/fs.h>
  25. #include <linux/delay.h>
  26. #include "xpd.h"
  27. #include "xproto.h"
  28. #include "xpp_dahdi.h"
  29. #include "card_fxo.h"
  30. #include "dahdi_debug.h"
  31. #include "xbus-core.h"
  32. static const char rcsid[] = "$Id$";
  33. static DEF_PARM(int, debug, 0, 0644, "Print DBG statements");
  34. static DEF_PARM(uint, poll_battery_interval, 500, 0644, "Poll battery interval in milliseconds (0 - disable)");
  35. #ifdef WITH_METERING
  36. static DEF_PARM(uint, poll_metering_interval, 500, 0644, "Poll metering interval in milliseconds (0 - disable)");
  37. #endif
  38. static DEF_PARM(int, ring_debounce, 50, 0644, "Number of ticks to debounce a false RING indication");
  39. static DEF_PARM(int, caller_id_style, 0, 0444, "Caller-Id detection style: 0 - [BELL], 1 - [ETSI_FSK], 2 - [ETSI_DTMF]");
  40. static DEF_PARM(int, power_denial_safezone, 650, 0644, "msec after offhook to ignore power-denial ( (0 - disable power-denial)");
  41. static DEF_PARM(int, power_denial_minlen, 80, 0644, "Minimal detected power-denial length (msec) (0 - disable power-denial)");
  42. static DEF_PARM(uint, battery_threshold, 3, 0644, "Minimum voltage that shows there is battery");
  43. static DEF_PARM(uint, battery_debounce, 1000, 0644, "Minimum interval (msec) for detection of battery off");
  44. enum cid_style {
  45. CID_STYLE_BELL = 0, /* E.g: US (Bellcore) */
  46. CID_STYLE_ETSI_FSK = 1, /* E.g: UK (British Telecom) */
  47. CID_STYLE_ETSI_DTMF = 2, /* E.g: DK, Russia */
  48. };
  49. /* Signaling is opposite (fxs signalling for fxo card) */
  50. #if 1
  51. #define FXO_DEFAULT_SIGCAP (DAHDI_SIG_FXSKS | DAHDI_SIG_FXSLS)
  52. #else
  53. #define FXO_DEFAULT_SIGCAP (DAHDI_SIG_SF)
  54. #endif
  55. enum fxo_leds {
  56. LED_GREEN,
  57. LED_RED,
  58. };
  59. #define NUM_LEDS 2
  60. #define DELAY_UNTIL_DIALTONE 3000
  61. /*
  62. * Minimum duration for polarity reversal detection (in ticks)
  63. * Should be longer than the time to detect a ring, so voltage
  64. * fluctuation during ring won't trigger false detection.
  65. */
  66. #define POLREV_THRESHOLD 200
  67. #define POWER_DENIAL_CURRENT 3
  68. #define POWER_DENIAL_DELAY 2500 /* ticks */
  69. /* Shortcuts */
  70. #define DAA_WRITE 1
  71. #define DAA_READ 0
  72. #define DAA_DIRECT_REQUEST(xbus,xpd,port,writing,reg,dL) \
  73. xpp_register_request((xbus), (xpd), (port), (writing), (reg), 0, 0, (dL), 0, 0, 0)
  74. /*---------------- FXO Protocol Commands ----------------------------------*/
  75. static /* 0x0F */ DECLARE_CMD(FXO, XPD_STATE, bool on);
  76. static bool fxo_packet_is_valid(xpacket_t *pack);
  77. static void fxo_packet_dump(const char *msg, xpacket_t *pack);
  78. #ifdef CONFIG_PROC_FS
  79. static int proc_fxo_info_read(char *page, char **start, off_t off, int count, int *eof, void *data);
  80. #ifdef WITH_METERING
  81. static int proc_xpd_metering_read(char *page, char **start, off_t off, int count, int *eof, void *data);
  82. #endif
  83. #endif
  84. static void dahdi_report_battery(xpd_t *xpd, lineno_t chan);
  85. #define PROC_REGISTER_FNAME "slics"
  86. #define PROC_FXO_INFO_FNAME "fxo_info"
  87. #ifdef WITH_METERING
  88. #define PROC_METERING_FNAME "metering_read"
  89. #endif
  90. #define REG_DAA_CONTROL1 0x05 /* 5 - DAA Control 1 */
  91. #define REG_DAA_CONTROL1_OH BIT(0) /* Off-Hook. */
  92. #define REG_DAA_CONTROL1_ONHM BIT(3) /* On-Hook Line Monitor */
  93. #define DAA_REG_METERING 0x11 /* 17 */
  94. #define DAA_REG_CURRENT 0x1C /* 28 */
  95. #define DAA_REG_VBAT 0x1D /* 29 */
  96. enum battery_state {
  97. BATTERY_UNKNOWN = 0,
  98. BATTERY_ON = 1,
  99. BATTERY_OFF = -1
  100. };
  101. enum polarity_state {
  102. POL_UNKNOWN = 0,
  103. POL_POSITIVE = 1,
  104. POL_NEGATIVE = -1
  105. };
  106. enum power_state {
  107. POWER_UNKNOWN = 0,
  108. POWER_ON = 1,
  109. POWER_OFF = -1
  110. };
  111. struct FXO_priv_data {
  112. #ifdef WITH_METERING
  113. struct proc_dir_entry *meteringfile;
  114. #endif
  115. struct proc_dir_entry *fxo_info;
  116. uint poll_counter;
  117. signed char battery_voltage[CHANNELS_PERXPD];
  118. signed char battery_current[CHANNELS_PERXPD];
  119. enum battery_state battery[CHANNELS_PERXPD];
  120. ushort nobattery_debounce[CHANNELS_PERXPD];
  121. enum polarity_state polarity[CHANNELS_PERXPD];
  122. ushort polarity_debounce[CHANNELS_PERXPD];
  123. enum power_state power[CHANNELS_PERXPD];
  124. ushort power_denial_delay[CHANNELS_PERXPD];
  125. ushort power_denial_length[CHANNELS_PERXPD];
  126. ushort power_denial_safezone[CHANNELS_PERXPD];
  127. xpp_line_t cidfound; /* 0 - OFF, 1 - ON */
  128. unsigned int cidtimer[CHANNELS_PERXPD];
  129. xpp_line_t ledstate[NUM_LEDS]; /* 0 - OFF, 1 - ON */
  130. xpp_line_t ledcontrol[NUM_LEDS]; /* 0 - OFF, 1 - ON */
  131. int led_counter[NUM_LEDS][CHANNELS_PERXPD];
  132. atomic_t ring_debounce[CHANNELS_PERXPD];
  133. #ifdef WITH_METERING
  134. uint metering_count[CHANNELS_PERXPD];
  135. xpp_line_t metering_tone_state;
  136. #endif
  137. };
  138. /*
  139. * LED counter values:
  140. * n>1 : BLINK every n'th tick
  141. */
  142. #define LED_COUNTER(priv,pos,color) ((priv)->led_counter[color][pos])
  143. #define IS_BLINKING(priv,pos,color) (LED_COUNTER(priv,pos,color) > 0)
  144. #define MARK_BLINK(priv,pos,color,t) ((priv)->led_counter[color][pos] = (t))
  145. #define MARK_OFF(priv,pos,color) do { BIT_CLR((priv)->ledcontrol[color],(pos)); MARK_BLINK((priv),(pos),(color),0); } while(0)
  146. #define MARK_ON(priv,pos,color) do { BIT_SET((priv)->ledcontrol[color],(pos)); MARK_BLINK((priv),(pos),(color),0); } while(0)
  147. #define LED_BLINK_RING (1000/8) /* in ticks */
  148. /*---------------- FXO: Static functions ----------------------------------*/
  149. static const char *power2str(enum power_state pw)
  150. {
  151. switch(pw) {
  152. case POWER_UNKNOWN: return "UNKNOWN";
  153. case POWER_OFF: return "OFF";
  154. case POWER_ON: return "ON";
  155. }
  156. return NULL;
  157. }
  158. static void power_change(xpd_t *xpd, int portno, enum power_state pw)
  159. {
  160. struct FXO_priv_data *priv;
  161. priv = xpd->priv;
  162. LINE_DBG(SIGNAL, xpd, portno, "power: %s -> %s\n",
  163. power2str(priv->power[portno]),
  164. power2str(pw));
  165. priv->power[portno] = pw;
  166. }
  167. static void reset_battery_readings(xpd_t *xpd, lineno_t pos)
  168. {
  169. struct FXO_priv_data *priv = xpd->priv;
  170. priv->nobattery_debounce[pos] = 0;
  171. priv->power_denial_delay[pos] = 0;
  172. power_change(xpd, pos, POWER_UNKNOWN);
  173. }
  174. static const int led_register_mask[] = { BIT(7), BIT(6), BIT(5) };
  175. /*
  176. * LED control is done via DAA register 0x20
  177. */
  178. static int do_led(xpd_t *xpd, lineno_t chan, byte which, bool on)
  179. {
  180. int ret = 0;
  181. struct FXO_priv_data *priv;
  182. xbus_t *xbus;
  183. byte value;
  184. BUG_ON(!xpd);
  185. xbus = xpd->xbus;
  186. priv = xpd->priv;
  187. which = which % NUM_LEDS;
  188. if(IS_SET(xpd->digital_outputs, chan) || IS_SET(xpd->digital_inputs, chan))
  189. goto out;
  190. if(chan == PORT_BROADCAST) {
  191. priv->ledstate[which] = (on) ? ~0 : 0;
  192. } else {
  193. if(on) {
  194. BIT_SET(priv->ledstate[which], chan);
  195. } else {
  196. BIT_CLR(priv->ledstate[which], chan);
  197. }
  198. }
  199. value = 0;
  200. value |= ((BIT(5) | BIT(6) | BIT(7)) & ~led_register_mask[which]);
  201. value |= (on) ? BIT(0) : 0;
  202. value |= (on) ? BIT(1) : 0;
  203. LINE_DBG(LEDS, xpd, chan, "LED: which=%d -- %s\n", which, (on) ? "on" : "off");
  204. ret = DAA_DIRECT_REQUEST(xbus, xpd, chan, DAA_WRITE, 0x20, value);
  205. out:
  206. return ret;
  207. }
  208. static void handle_fxo_leds(xpd_t *xpd)
  209. {
  210. int i;
  211. unsigned long flags;
  212. const enum fxo_leds colors[] = { LED_GREEN, LED_RED };
  213. enum fxo_leds color;
  214. unsigned int timer_count;
  215. struct FXO_priv_data *priv;
  216. BUG_ON(!xpd);
  217. spin_lock_irqsave(&xpd->lock, flags);
  218. priv = xpd->priv;
  219. timer_count = xpd->timer_count;
  220. for(color = 0; color < ARRAY_SIZE(colors); color++) {
  221. for_each_line(xpd, i) {
  222. if(IS_SET(xpd->digital_outputs, i) || IS_SET(xpd->digital_inputs, i))
  223. continue;
  224. if((xpd->blink_mode & BIT(i)) || IS_BLINKING(priv, i, color)) { // Blinking
  225. int mod_value = LED_COUNTER(priv, i, color);
  226. if(!mod_value)
  227. mod_value = DEFAULT_LED_PERIOD; /* safety value */
  228. // led state is toggled
  229. if((timer_count % mod_value) == 0) {
  230. LINE_DBG(LEDS, xpd, i, "ledstate=%s\n", (IS_SET(priv->ledstate[color], i))?"ON":"OFF");
  231. if(!IS_SET(priv->ledstate[color], i)) {
  232. do_led(xpd, i, color, 1);
  233. } else {
  234. do_led(xpd, i, color, 0);
  235. }
  236. }
  237. } else if(IS_SET(priv->ledcontrol[color], i) && !IS_SET(priv->ledstate[color], i)) {
  238. do_led(xpd, i, color, 1);
  239. } else if(!IS_SET(priv->ledcontrol[color], i) && IS_SET(priv->ledstate[color], i)) {
  240. do_led(xpd, i, color, 0);
  241. }
  242. }
  243. }
  244. spin_unlock_irqrestore(&xpd->lock, flags);
  245. }
  246. static void update_dahdi_ring(xpd_t *xpd, int pos, bool on)
  247. {
  248. BUG_ON(!xpd);
  249. if(caller_id_style == CID_STYLE_BELL)
  250. oht_pcm(xpd, pos, !on);
  251. /*
  252. * We should not spinlock before calling dahdi_hooksig() as
  253. * it may call back into our xpp_hooksig() and cause
  254. * a nested spinlock scenario
  255. */
  256. notify_rxsig(xpd, pos, (on) ? DAHDI_RXSIG_RING : DAHDI_RXSIG_OFFHOOK);
  257. }
  258. static void mark_ring(xpd_t *xpd, lineno_t pos, bool on, bool update_dahdi)
  259. {
  260. struct FXO_priv_data *priv;
  261. priv = xpd->priv;
  262. BUG_ON(!priv);
  263. atomic_set(&priv->ring_debounce[pos], 0); /* Stop debouncing */
  264. /*
  265. * We don't want to check battery during ringing
  266. * due to voltage fluctuations.
  267. */
  268. reset_battery_readings(xpd, pos);
  269. if(on && !xpd->ringing[pos]) {
  270. LINE_DBG(SIGNAL, xpd, pos, "START\n");
  271. xpd->ringing[pos] = 1;
  272. priv->cidtimer[pos] = xpd->timer_count;
  273. MARK_BLINK(priv, pos, LED_GREEN, LED_BLINK_RING);
  274. if(update_dahdi)
  275. update_dahdi_ring(xpd, pos, on);
  276. } else if(!on && xpd->ringing[pos]) {
  277. LINE_DBG(SIGNAL, xpd, pos, "STOP\n");
  278. xpd->ringing[pos] = 0;
  279. priv->cidtimer[pos] = xpd->timer_count;
  280. if(IS_BLINKING(priv, pos, LED_GREEN))
  281. MARK_BLINK(priv, pos, LED_GREEN, 0);
  282. if(update_dahdi)
  283. update_dahdi_ring(xpd, pos, on);
  284. }
  285. }
  286. static int do_sethook(xpd_t *xpd, int pos, bool to_offhook)
  287. {
  288. unsigned long flags;
  289. xbus_t *xbus;
  290. struct FXO_priv_data *priv;
  291. int ret = 0;
  292. byte value;
  293. BUG_ON(!xpd);
  294. BUG_ON(xpd->direction == TO_PHONE); // We can SETHOOK state only on PSTN
  295. xbus = xpd->xbus;
  296. priv = xpd->priv;
  297. BUG_ON(!priv);
  298. if(priv->battery[pos] != BATTERY_ON && to_offhook) {
  299. LINE_NOTICE(xpd, pos, "Cannot take offhook while battery is off!\n");
  300. return -EINVAL;
  301. }
  302. spin_lock_irqsave(&xpd->lock, flags);
  303. mark_ring(xpd, pos, 0, 0); // No more rings
  304. value = REG_DAA_CONTROL1_ONHM; /* Bit 3 is for CID */
  305. if(to_offhook)
  306. value |= REG_DAA_CONTROL1_OH;
  307. LINE_DBG(SIGNAL, xpd, pos, "SETHOOK: value=0x%02X %s\n", value, (to_offhook)?"OFFHOOK":"ONHOOK");
  308. if(to_offhook)
  309. MARK_ON(priv, pos, LED_GREEN);
  310. else
  311. MARK_OFF(priv, pos, LED_GREEN);
  312. ret = DAA_DIRECT_REQUEST(xbus, xpd, pos, DAA_WRITE, REG_DAA_CONTROL1, value);
  313. mark_offhook(xpd, pos, to_offhook);
  314. if(caller_id_style != CID_STYLE_ETSI_DTMF)
  315. oht_pcm(xpd, pos, 0);
  316. #ifdef WITH_METERING
  317. priv->metering_count[pos] = 0;
  318. priv->metering_tone_state = 0L;
  319. DAA_DIRECT_REQUEST(xbus, xpd, pos, DAA_WRITE, DAA_REG_METERING, 0x2D);
  320. #endif
  321. reset_battery_readings(xpd, pos); /* unstable during hook changes */
  322. if(to_offhook) {
  323. priv->power_denial_safezone[pos] = power_denial_safezone;
  324. } else {
  325. priv->power_denial_length[pos] = 0;
  326. priv->power_denial_safezone[pos] = 0;
  327. }
  328. priv->cidtimer[pos] = xpd->timer_count;
  329. spin_unlock_irqrestore(&xpd->lock, flags);
  330. return ret;
  331. }
  332. /*---------------- FXO: Methods -------------------------------------------*/
  333. static void fxo_proc_remove(xbus_t *xbus, xpd_t *xpd)
  334. {
  335. struct FXO_priv_data *priv;
  336. BUG_ON(!xpd);
  337. priv = xpd->priv;
  338. XPD_DBG(PROC, xpd, "\n");
  339. #ifdef CONFIG_PROC_FS
  340. #ifdef WITH_METERING
  341. if(priv->meteringfile) {
  342. XPD_DBG(PROC, xpd, "Removing xpd metering tone file\n");
  343. priv->meteringfile->data = NULL;
  344. remove_proc_entry(PROC_METERING_FNAME, xpd->proc_xpd_dir);
  345. priv->meteringfile = NULL;
  346. }
  347. #endif
  348. if(priv->fxo_info) {
  349. XPD_DBG(PROC, xpd, "Removing xpd FXO_INFO file\n");
  350. remove_proc_entry(PROC_FXO_INFO_FNAME, xpd->proc_xpd_dir);
  351. priv->fxo_info = NULL;
  352. }
  353. #endif
  354. }
  355. static int fxo_proc_create(xbus_t *xbus, xpd_t *xpd)
  356. {
  357. struct FXO_priv_data *priv;
  358. BUG_ON(!xpd);
  359. priv = xpd->priv;
  360. #ifdef CONFIG_PROC_FS
  361. XPD_DBG(PROC, xpd, "Creating FXO_INFO file\n");
  362. priv->fxo_info = create_proc_read_entry(PROC_FXO_INFO_FNAME, 0444, xpd->proc_xpd_dir, proc_fxo_info_read, xpd);
  363. if(!priv->fxo_info) {
  364. XPD_ERR(xpd, "Failed to create proc file '%s'\n", PROC_FXO_INFO_FNAME);
  365. fxo_proc_remove(xbus, xpd);
  366. return -EINVAL;
  367. }
  368. SET_PROC_DIRENTRY_OWNER(priv->fxo_info);
  369. #ifdef WITH_METERING
  370. XPD_DBG(PROC, xpd, "Creating Metering tone file\n");
  371. priv->meteringfile = create_proc_read_entry(PROC_METERING_FNAME, 0444, xpd->proc_xpd_dir,
  372. proc_xpd_metering_read, xpd);
  373. if(!priv->meteringfile) {
  374. XPD_ERR(xpd, "Failed to create proc file '%s'\n", PROC_METERING_FNAME);
  375. fxo_proc_remove(xbus, xpd);
  376. return -EINVAL;
  377. }
  378. SET_PROC_DIRENTRY_OWNER(priv->meteringfile);
  379. #endif
  380. #endif
  381. return 0;
  382. }
  383. static xpd_t *FXO_card_new(xbus_t *xbus, int unit, int subunit, const xproto_table_t *proto_table,
  384. byte subtype, int subunits, int subunit_ports, bool to_phone)
  385. {
  386. xpd_t *xpd = NULL;
  387. int channels;
  388. if(to_phone) {
  389. XBUS_NOTICE(xbus,
  390. "XPD=%d%d: try to instanciate FXO with reverse direction\n",
  391. unit, subunit);
  392. return NULL;
  393. }
  394. if(subtype == 2)
  395. channels = min(2, subunit_ports);
  396. else
  397. channels = min(8, subunit_ports);
  398. xpd = xpd_alloc(xbus, unit, subunit, subtype, subunits, sizeof(struct FXO_priv_data), proto_table, channels);
  399. if(!xpd)
  400. return NULL;
  401. xpd->direction = TO_PSTN;
  402. xpd->type_name = "FXO";
  403. if(fxo_proc_create(xbus, xpd) < 0)
  404. goto err;
  405. return xpd;
  406. err:
  407. xpd_free(xpd);
  408. return NULL;
  409. }
  410. static int FXO_card_init(xbus_t *xbus, xpd_t *xpd)
  411. {
  412. struct FXO_priv_data *priv;
  413. int i;
  414. BUG_ON(!xpd);
  415. priv = xpd->priv;
  416. // Hanghup all lines
  417. for_each_line(xpd, i) {
  418. do_sethook(xpd, i, 0);
  419. priv->polarity[i] = POL_UNKNOWN; /* will be updated on next battery sample */
  420. priv->polarity_debounce[i] = 0;
  421. priv->battery[i] = BATTERY_UNKNOWN; /* will be updated on next battery sample */
  422. priv->power[i] = POWER_UNKNOWN; /* will be updated on next battery sample */
  423. if(caller_id_style == CID_STYLE_ETSI_DTMF)
  424. oht_pcm(xpd, i, 1);
  425. }
  426. XPD_DBG(GENERAL, xpd, "done\n");
  427. for_each_line(xpd, i) {
  428. do_led(xpd, i, LED_GREEN, 0);
  429. }
  430. for_each_line(xpd, i) {
  431. do_led(xpd, i, LED_GREEN, 1);
  432. msleep(50);
  433. }
  434. for_each_line(xpd, i) {
  435. do_led(xpd, i, LED_GREEN, 0);
  436. msleep(50);
  437. }
  438. CALL_XMETHOD(card_pcm_recompute, xbus, xpd, 0);
  439. return 0;
  440. }
  441. static int FXO_card_remove(xbus_t *xbus, xpd_t *xpd)
  442. {
  443. struct FXO_priv_data *priv;
  444. BUG_ON(!xpd);
  445. priv = xpd->priv;
  446. XPD_DBG(GENERAL, xpd, "\n");
  447. fxo_proc_remove(xbus, xpd);
  448. return 0;
  449. }
  450. static int FXO_card_dahdi_preregistration(xpd_t *xpd, bool on)
  451. {
  452. xbus_t *xbus;
  453. struct FXO_priv_data *priv;
  454. int i;
  455. unsigned int timer_count;
  456. BUG_ON(!xpd);
  457. xbus = xpd->xbus;
  458. BUG_ON(!xbus);
  459. priv = xpd->priv;
  460. BUG_ON(!priv);
  461. timer_count = xpd->timer_count;
  462. XPD_DBG(GENERAL, xpd, "%s\n", (on)?"ON":"OFF");
  463. for_each_line(xpd, i) {
  464. struct dahdi_chan *cur_chan = XPD_CHAN(xpd, i);
  465. XPD_DBG(GENERAL, xpd, "setting FXO channel %d\n", i);
  466. snprintf(cur_chan->name, MAX_CHANNAME, "XPP_FXO/%02d/%1d%1d/%d",
  467. xbus->num, xpd->addr.unit, xpd->addr.subunit, i);
  468. cur_chan->chanpos = i + 1;
  469. cur_chan->pvt = xpd;
  470. cur_chan->sigcap = FXO_DEFAULT_SIGCAP;
  471. }
  472. for_each_line(xpd, i) {
  473. MARK_ON(priv, i, LED_GREEN);
  474. msleep(4);
  475. MARK_ON(priv, i, LED_RED);
  476. }
  477. for_each_line(xpd, i) {
  478. priv->cidtimer[i] = timer_count;
  479. }
  480. return 0;
  481. }
  482. static int FXO_card_dahdi_postregistration(xpd_t *xpd, bool on)
  483. {
  484. xbus_t *xbus;
  485. struct FXO_priv_data *priv;
  486. int i;
  487. BUG_ON(!xpd);
  488. xbus = xpd->xbus;
  489. BUG_ON(!xbus);
  490. priv = xpd->priv;
  491. BUG_ON(!priv);
  492. XPD_DBG(GENERAL, xpd, "%s\n", (on)?"ON":"OFF");
  493. for_each_line(xpd, i) {
  494. dahdi_report_battery(xpd, i);
  495. MARK_OFF(priv, i, LED_GREEN);
  496. msleep(2);
  497. MARK_OFF(priv, i, LED_RED);
  498. msleep(2);
  499. }
  500. return 0;
  501. }
  502. static int FXO_card_hooksig(xbus_t *xbus, xpd_t *xpd, int pos, enum dahdi_txsig txsig)
  503. {
  504. struct FXO_priv_data *priv;
  505. int ret = 0;
  506. priv = xpd->priv;
  507. BUG_ON(!priv);
  508. LINE_DBG(SIGNAL, xpd, pos, "%s\n", txsig2str(txsig));
  509. BUG_ON(xpd->direction != TO_PSTN);
  510. /* XXX Enable hooksig for FXO XXX */
  511. switch(txsig) {
  512. case DAHDI_TXSIG_START:
  513. case DAHDI_TXSIG_OFFHOOK:
  514. ret = do_sethook(xpd, pos, 1);
  515. break;
  516. case DAHDI_TXSIG_ONHOOK:
  517. ret = do_sethook(xpd, pos, 0);
  518. break;
  519. default:
  520. XPD_NOTICE(xpd, "Can't set tx state to %s (%d)\n",
  521. txsig2str(txsig), txsig);
  522. return -EINVAL;
  523. }
  524. return ret;
  525. }
  526. static void dahdi_report_battery(xpd_t *xpd, lineno_t chan)
  527. {
  528. struct FXO_priv_data *priv;
  529. BUG_ON(!xpd);
  530. priv = xpd->priv;
  531. if(SPAN_REGISTERED(xpd)) {
  532. switch(priv->battery[chan]) {
  533. case BATTERY_UNKNOWN:
  534. /* no-op */
  535. break;
  536. case BATTERY_OFF:
  537. LINE_DBG(SIGNAL, xpd, chan, "Send DAHDI_ALARM_RED\n");
  538. dahdi_alarm_channel(XPD_CHAN(xpd, chan), DAHDI_ALARM_RED);
  539. break;
  540. case BATTERY_ON:
  541. LINE_DBG(SIGNAL, xpd, chan, "Send DAHDI_ALARM_NONE\n");
  542. dahdi_alarm_channel(XPD_CHAN(xpd, chan), DAHDI_ALARM_NONE);
  543. break;
  544. }
  545. }
  546. }
  547. static int FXO_card_open(xpd_t *xpd, lineno_t chan)
  548. {
  549. struct FXO_priv_data *priv;
  550. BUG_ON(!xpd);
  551. priv = xpd->priv;
  552. return 0;
  553. }
  554. static void poll_battery(xbus_t *xbus, xpd_t *xpd)
  555. {
  556. int i;
  557. for_each_line(xpd, i) {
  558. DAA_DIRECT_REQUEST(xbus, xpd, i, DAA_READ, DAA_REG_VBAT, 0);
  559. }
  560. }
  561. #ifdef WITH_METERING
  562. static void poll_metering(xbus_t *xbus, xpd_t *xpd)
  563. {
  564. int i;
  565. for_each_line(xpd, i) {
  566. if (IS_OFFHOOK(xpd, i))
  567. DAA_DIRECT_REQUEST(xbus, xpd, i, DAA_READ, DAA_REG_METERING, 0);
  568. }
  569. }
  570. #endif
  571. static void handle_fxo_ring(xpd_t *xpd)
  572. {
  573. struct FXO_priv_data *priv;
  574. int i;
  575. priv = xpd->priv;
  576. for_each_line(xpd, i) {
  577. if(atomic_read(&priv->ring_debounce[i]) > 0) {
  578. /* Maybe start ring */
  579. if(atomic_dec_and_test(&priv->ring_debounce[i]))
  580. mark_ring(xpd, i, 1, 1);
  581. } else if (atomic_read(&priv->ring_debounce[i]) < 0) {
  582. /* Maybe stop ring */
  583. if(atomic_inc_and_test(&priv->ring_debounce[i]))
  584. mark_ring(xpd, i, 0, 1);
  585. }
  586. }
  587. }
  588. static void handle_fxo_power_denial(xpd_t *xpd)
  589. {
  590. struct FXO_priv_data *priv;
  591. int i;
  592. if(!power_denial_safezone)
  593. return; /* Ignore power denials */
  594. priv = xpd->priv;
  595. for_each_line(xpd, i) {
  596. if(xpd->ringing[i] || !IS_OFFHOOK(xpd, i)) {
  597. priv->power_denial_delay[i] = 0;
  598. continue;
  599. }
  600. if(priv->power_denial_safezone[i] > 0) {
  601. if(--priv->power_denial_safezone[i] == 0) {
  602. /*
  603. * Poll current, previous answers are meaningless
  604. */
  605. DAA_DIRECT_REQUEST(xpd->xbus, xpd, i, DAA_READ, DAA_REG_CURRENT, 0);
  606. }
  607. continue;
  608. }
  609. if(priv->power_denial_length[i] > 0) {
  610. priv->power_denial_length[i]--;
  611. if(priv->power_denial_length[i] <= 0) {
  612. /*
  613. * But maybe the FXS started to ring (and the firmware haven't
  614. * detected it yet). This would cause false power denials.
  615. * So we just flag it and schedule more ticks to wait.
  616. */
  617. LINE_DBG(SIGNAL, xpd, i, "Possible Power Denial Hangup\n");
  618. priv->power_denial_delay[i] = POWER_DENIAL_DELAY;
  619. }
  620. continue;
  621. }
  622. if (priv->power_denial_delay[i] > 0) {
  623. /*
  624. * Ring detection by the firmware takes some time.
  625. * Therefore we delay our decision until we are
  626. * sure that no ring has started during this time.
  627. */
  628. priv->power_denial_delay[i]--;
  629. if (priv->power_denial_delay[i] <= 0) {
  630. LINE_DBG(SIGNAL, xpd, i, "Power Denial Hangup\n");
  631. priv->power_denial_delay[i] = 0;
  632. /*
  633. * Let Asterisk decide what to do
  634. */
  635. notify_rxsig(xpd, i, DAHDI_RXSIG_ONHOOK);
  636. }
  637. }
  638. }
  639. }
  640. /*
  641. * For caller-id CID_STYLE_ETSI_DTMF:
  642. * - No indication is passed before the CID
  643. * - We try to detect it and send "fake" polarity reversal.
  644. * - The chan_dahdi.conf should have cidstart=polarity
  645. * - Based on an idea in http://bugs.digium.com/view.php?id=9096
  646. */
  647. static void check_etsi_dtmf(xpd_t *xpd)
  648. {
  649. struct FXO_priv_data *priv;
  650. int portno;
  651. unsigned int timer_count;
  652. if(!SPAN_REGISTERED(xpd))
  653. return;
  654. priv = xpd->priv;
  655. BUG_ON(!priv);
  656. timer_count = xpd->timer_count;
  657. for_each_line(xpd, portno) {
  658. /* Skip offhook and ringing ports */
  659. if(IS_OFFHOOK(xpd, portno) || xpd->ringing[portno])
  660. continue;
  661. if(IS_SET(priv->cidfound, portno)) {
  662. if(timer_count > priv->cidtimer[portno] + 4000) {
  663. /* reset flags if it's been a while */
  664. priv->cidtimer[portno] = timer_count;
  665. BIT_CLR(priv->cidfound, portno);
  666. LINE_DBG(SIGNAL, xpd, portno, "Reset CID flag\n");
  667. }
  668. continue;
  669. }
  670. if(timer_count > priv->cidtimer[portno] + 400) {
  671. struct dahdi_chan *chan = XPD_CHAN(xpd, portno);
  672. int sample;
  673. int i;
  674. for(i = 0; i < DAHDI_CHUNKSIZE; i++) {
  675. sample = DAHDI_XLAW(chan->readchunk[i], chan);
  676. if(sample > 16000 || sample < -16000) {
  677. priv->cidtimer[portno] = timer_count;
  678. BIT_SET(priv->cidfound, portno);
  679. LINE_DBG(SIGNAL, xpd, portno, "Found DTMF CLIP (%d)\n", i);
  680. dahdi_qevent_lock(chan, DAHDI_EVENT_POLARITY);
  681. break;
  682. }
  683. }
  684. }
  685. }
  686. }
  687. static int FXO_card_tick(xbus_t *xbus, xpd_t *xpd)
  688. {
  689. struct FXO_priv_data *priv;
  690. BUG_ON(!xpd);
  691. priv = xpd->priv;
  692. BUG_ON(!priv);
  693. if(poll_battery_interval != 0 && (priv->poll_counter % poll_battery_interval) == 0)
  694. poll_battery(xbus, xpd);
  695. #ifdef WITH_METERING
  696. if(poll_metering_interval != 0 && (priv->poll_counter % poll_metering_interval) == 0)
  697. poll_metering(xbus, xpd);
  698. #endif
  699. handle_fxo_leds(xpd);
  700. handle_fxo_ring(xpd);
  701. handle_fxo_power_denial(xpd);
  702. if(caller_id_style == CID_STYLE_ETSI_DTMF && likely(xpd->card_present))
  703. check_etsi_dtmf(xpd);
  704. priv->poll_counter++;
  705. return 0;
  706. }
  707. #include <dahdi/wctdm_user.h>
  708. /*
  709. * The first register is the ACIM, the other are coefficient registers.
  710. * We define the array size explicitly to track possible inconsistencies
  711. * if the struct is modified.
  712. */
  713. static const char echotune_regs[sizeof(struct wctdm_echo_coefs)] = {30, 45, 46, 47, 48, 49, 50, 51, 52};
  714. static int FXO_card_ioctl(xpd_t *xpd, int pos, unsigned int cmd, unsigned long arg)
  715. {
  716. int i,ret;
  717. unsigned char echotune_data[ARRAY_SIZE(echotune_regs)];
  718. BUG_ON(!xpd);
  719. if(!XBUS_IS(xpd->xbus, READY))
  720. return -ENODEV;
  721. switch (cmd) {
  722. case WCTDM_SET_ECHOTUNE:
  723. XPD_DBG(GENERAL, xpd, "-- Setting echo registers: \n");
  724. /* first off: check if this span is fxs. If not: -EINVALID */
  725. if (copy_from_user(&echotune_data, (void __user *)arg, sizeof(echotune_data)))
  726. return -EFAULT;
  727. for (i = 0; i < ARRAY_SIZE(echotune_regs); i++) {
  728. XPD_DBG(REGS, xpd, "Reg=0x%02X, data=0x%02X\n", echotune_regs[i], echotune_data[i]);
  729. ret = DAA_DIRECT_REQUEST(xpd->xbus, xpd, pos, DAA_WRITE, echotune_regs[i], echotune_data[i]);
  730. if (ret < 0) {
  731. LINE_NOTICE(xpd, pos, "Couldn't write %0x02X to register %0x02X\n",
  732. echotune_data[i], echotune_regs[i]);
  733. return ret;
  734. }
  735. msleep(1);
  736. }
  737. XPD_DBG(GENERAL, xpd, "-- Set echo registers successfully\n");
  738. break;
  739. case DAHDI_TONEDETECT:
  740. /*
  741. * Asterisk call all span types with this (FXS specific)
  742. * call. Silently ignore it.
  743. */
  744. LINE_DBG(GENERAL, xpd, pos,
  745. "DAHDI_TONEDETECT (FXO: NOTIMPLEMENTED)\n");
  746. return -ENOTTY;
  747. default:
  748. report_bad_ioctl(THIS_MODULE->name, xpd, pos, cmd);
  749. return -ENOTTY;
  750. }
  751. return 0;
  752. }
  753. /*---------------- FXO: HOST COMMANDS -------------------------------------*/
  754. static /* 0x0F */ HOSTCMD(FXO, XPD_STATE, bool on)
  755. {
  756. int ret = 0;
  757. struct FXO_priv_data *priv;
  758. BUG_ON(!xbus);
  759. BUG_ON(!xpd);
  760. priv = xpd->priv;
  761. BUG_ON(!priv);
  762. XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
  763. return ret;
  764. }
  765. /*---------------- FXO: Astribank Reply Handlers --------------------------*/
  766. HANDLER_DEF(FXO, SIG_CHANGED)
  767. {
  768. xpp_line_t sig_status = RPACKET_FIELD(pack, FXO, SIG_CHANGED, sig_status);
  769. xpp_line_t sig_toggles = RPACKET_FIELD(pack, FXO, SIG_CHANGED, sig_toggles);
  770. unsigned long flags;
  771. int i;
  772. struct FXO_priv_data *priv;
  773. if(!xpd) {
  774. notify_bad_xpd(__FUNCTION__, xbus, XPACKET_ADDR(pack), cmd->name);
  775. return -EPROTO;
  776. }
  777. priv = xpd->priv;
  778. BUG_ON(!priv);
  779. XPD_DBG(SIGNAL, xpd, "(PSTN) sig_toggles=0x%04X sig_status=0x%04X\n", sig_toggles, sig_status);
  780. spin_lock_irqsave(&xpd->lock, flags);
  781. for_each_line(xpd, i) {
  782. int debounce;
  783. if(IS_SET(sig_toggles, i)) {
  784. if(priv->battery[i] == BATTERY_OFF) {
  785. /*
  786. * With poll_battery_interval==0 we cannot have BATTERY_OFF
  787. * so we won't get here
  788. */
  789. LINE_NOTICE(xpd, i, "SIG_CHANGED while battery is off. Ignored.\n");
  790. continue;
  791. }
  792. /* First report false ring alarms */
  793. debounce = atomic_read(&priv->ring_debounce[i]);
  794. if(debounce)
  795. LINE_NOTICE(xpd, i, "debounced false ring (only %d ticks)\n", debounce);
  796. /*
  797. * Now set a new ring alarm.
  798. * It will be checked in handle_fxo_ring()
  799. */
  800. debounce = (IS_SET(sig_status, i)) ? ring_debounce : -ring_debounce;
  801. atomic_set(&priv->ring_debounce[i], debounce);
  802. }
  803. }
  804. spin_unlock_irqrestore(&xpd->lock, flags);
  805. return 0;
  806. }
  807. static void update_battery_voltage(xpd_t *xpd, byte data_low, xportno_t portno)
  808. {
  809. struct FXO_priv_data *priv;
  810. enum polarity_state pol;
  811. int msec;
  812. signed char volts = (signed char)data_low;
  813. priv = xpd->priv;
  814. BUG_ON(!priv);
  815. priv->battery_voltage[portno] = volts;
  816. if(xpd->ringing[portno])
  817. goto ignore_reading; /* ring voltage create false alarms */
  818. if(abs(volts) < battery_threshold) {
  819. /*
  820. * Check for battery voltage fluctuations
  821. */
  822. if(priv->battery[portno] != BATTERY_OFF) {
  823. int milliseconds;
  824. milliseconds = priv->nobattery_debounce[portno]++ *
  825. poll_battery_interval;
  826. if(milliseconds > battery_debounce) {
  827. LINE_DBG(SIGNAL, xpd, portno, "BATTERY OFF voltage=%d\n", volts);
  828. priv->battery[portno] = BATTERY_OFF;
  829. dahdi_report_battery(xpd, portno);
  830. /* What's the polarity ? */
  831. priv->polarity[portno] = POL_UNKNOWN;
  832. priv->polarity_debounce[portno] = 0;
  833. /* What's the current ? */
  834. power_change(xpd, portno, POWER_UNKNOWN);
  835. /*
  836. * Stop further processing for now
  837. */
  838. goto ignore_reading;
  839. }
  840. }
  841. } else {
  842. priv->nobattery_debounce[portno] = 0;
  843. if(priv->battery[portno] != BATTERY_ON) {
  844. LINE_DBG(SIGNAL, xpd, portno, "BATTERY ON voltage=%d\n", volts);
  845. priv->battery[portno] = BATTERY_ON;
  846. dahdi_report_battery(xpd, portno);
  847. }
  848. }
  849. #if 0
  850. /*
  851. * Mark FXO ports without battery!
  852. */
  853. if(priv->battery[portno] != BATTERY_ON)
  854. MARK_ON(priv, portno, LED_RED);
  855. else
  856. MARK_OFF(priv, portno, LED_RED);
  857. #endif
  858. if(priv->battery[portno] != BATTERY_ON) {
  859. priv->polarity[portno] = POL_UNKNOWN; /* What's the polarity ? */
  860. return;
  861. }
  862. /*
  863. * Handle reverse polarity
  864. */
  865. if(volts == 0)
  866. pol = POL_UNKNOWN;
  867. else if(volts < 0)
  868. pol = POL_NEGATIVE;
  869. else
  870. pol = POL_POSITIVE;
  871. if(priv->polarity[portno] == pol) {
  872. /*
  873. * Same polarity, reset debounce counter
  874. */
  875. priv->polarity_debounce[portno] = 0;
  876. return;
  877. }
  878. /*
  879. * Track polarity reversals and debounce spikes.
  880. * Only reversals with long duration count.
  881. */
  882. msec = priv->polarity_debounce[portno]++ * poll_battery_interval;
  883. if (msec >= POLREV_THRESHOLD) {
  884. priv->polarity_debounce[portno] = 0;
  885. if(pol != POL_UNKNOWN && priv->polarity[portno] != POL_UNKNOWN) {
  886. char *polname = NULL;
  887. if(pol == POL_POSITIVE)
  888. polname = "Positive";
  889. else if(pol == POL_NEGATIVE)
  890. polname = "Negative";
  891. else
  892. BUG();
  893. LINE_DBG(SIGNAL, xpd, portno,
  894. "Polarity changed to %s\n", polname);
  895. /*
  896. * Inform dahdi/Asterisk:
  897. * 1. Maybe used for hangup detection during offhook
  898. * 2. In some countries used to report caller-id during onhook
  899. * but before first ring.
  900. */
  901. if(caller_id_style == CID_STYLE_ETSI_FSK)
  902. oht_pcm(xpd, portno, 1); /* will be cleared on ring/offhook */
  903. if(SPAN_REGISTERED(xpd)) {
  904. LINE_DBG(SIGNAL, xpd, portno,
  905. "Send DAHDI_EVENT_POLARITY: %s\n", polname);
  906. dahdi_qevent_lock(XPD_CHAN(xpd, portno), DAHDI_EVENT_POLARITY);
  907. }
  908. }
  909. priv->polarity[portno] = pol;
  910. }
  911. return;
  912. ignore_reading:
  913. /*
  914. * Reset debounce counters to prevent false alarms
  915. */
  916. reset_battery_readings(xpd, portno); /* unstable during hook changes */
  917. }
  918. static void update_battery_current(xpd_t *xpd, byte data_low, xportno_t portno)
  919. {
  920. struct FXO_priv_data *priv;
  921. priv = xpd->priv;
  922. BUG_ON(!priv);
  923. priv->battery_current[portno] = data_low;
  924. /*
  925. * During ringing, current is not stable.
  926. * During onhook there should not be current anyway.
  927. */
  928. if(xpd->ringing[portno] || !IS_OFFHOOK(xpd, portno))
  929. goto ignore_it;
  930. /*
  931. * Power denial with no battery voltage is meaningless
  932. */
  933. if(priv->battery[portno] != BATTERY_ON)
  934. goto ignore_it;
  935. /* Safe zone after offhook */
  936. if(priv->power_denial_safezone[portno] > 0)
  937. goto ignore_it;
  938. if(data_low < POWER_DENIAL_CURRENT) {
  939. if(priv->power[portno] == POWER_ON) {
  940. power_change(xpd, portno, POWER_OFF);
  941. priv->power_denial_length[portno] = power_denial_minlen;
  942. }
  943. } else {
  944. if(priv->power[portno] != POWER_ON) {
  945. power_change(xpd, portno, POWER_ON);
  946. priv->power_denial_length[portno] = 0;
  947. /* We are now OFFHOOK */
  948. hookstate_changed(xpd, portno, 1);
  949. }
  950. }
  951. return;
  952. ignore_it:
  953. priv->power_denial_delay[portno] = 0;
  954. }
  955. #ifdef WITH_METERING
  956. #define BTD_BIT BIT(0)
  957. static void update_metering_state(xpd_t *xpd, byte data_low, lineno_t portno)
  958. {
  959. struct FXO_priv_data *priv;
  960. bool metering_tone = data_low & BTD_BIT;
  961. bool old_metering_tone;
  962. priv = xpd->priv;
  963. BUG_ON(!priv);
  964. old_metering_tone = IS_SET(priv->metering_tone_state, portno);
  965. LINE_DBG(SIGNAL, xpd, portno, "METERING: %s [dL=0x%X] (%d)\n",
  966. (metering_tone) ? "ON" : "OFF",
  967. data_low, priv->metering_count[portno]);
  968. if(metering_tone && !old_metering_tone) {
  969. /* Rising edge */
  970. priv->metering_count[portno]++;
  971. BIT_SET(priv->metering_tone_state, portno);
  972. } else if(!metering_tone && old_metering_tone)
  973. BIT_CLR(priv->metering_tone_state, portno);
  974. if(metering_tone) {
  975. /* Clear the BTD bit */
  976. data_low &= ~BTD_BIT;
  977. DAA_DIRECT_REQUEST(xpd->xbus, xpd, portno, DAA_WRITE, DAA_REG_METERING, data_low);
  978. }
  979. }
  980. #endif
  981. static int FXO_card_register_reply(xbus_t *xbus, xpd_t *xpd, reg_cmd_t *info)
  982. {
  983. struct FXO_priv_data *priv;
  984. lineno_t portno;
  985. priv = xpd->priv;
  986. BUG_ON(!priv);
  987. portno = info->portnum;
  988. switch(REG_FIELD(info, regnum)) {
  989. case DAA_REG_VBAT:
  990. update_battery_voltage(xpd, REG_FIELD(info, data_low), portno);
  991. break;
  992. case DAA_REG_CURRENT:
  993. update_battery_current(xpd, REG_FIELD(info, data_low), portno);
  994. break;
  995. #ifdef WITH_METERING
  996. case DAA_REG_METERING:
  997. update_metering_state(xpd, REG_FIELD(info, data_low), portno);
  998. break;
  999. #endif
  1000. }
  1001. LINE_DBG(REGS, xpd, portno, "%c reg_num=0x%X, dataL=0x%X dataH=0x%X\n",
  1002. ((info->bytes == 3)?'I':'D'),
  1003. REG_FIELD(info, regnum),
  1004. REG_FIELD(info, data_low),
  1005. REG_FIELD(info, data_high));
  1006. /* Update /proc info only if reply relate to the last slic read request */
  1007. if(
  1008. REG_FIELD(&xpd->requested_reply, regnum) == REG_FIELD(info, regnum) &&
  1009. REG_FIELD(&xpd->requested_reply, do_subreg) == REG_FIELD(info, do_subreg) &&
  1010. REG_FIELD(&xpd->requested_reply, subreg) == REG_FIELD(info, subreg)) {
  1011. xpd->last_reply = *info;
  1012. }
  1013. return 0;
  1014. }
  1015. static xproto_table_t PROTO_TABLE(FXO) = {
  1016. .owner = THIS_MODULE,
  1017. .entries = {
  1018. /* Prototable Card Opcode */
  1019. XENTRY( FXO, FXO, SIG_CHANGED ),
  1020. },
  1021. .name = "FXO", /* protocol name */
  1022. .ports_per_subunit = 8,
  1023. .type = XPD_TYPE_FXO,
  1024. .xops = {
  1025. .card_new = FXO_card_new,
  1026. .card_init = FXO_card_init,
  1027. .card_remove = FXO_card_remove,
  1028. .card_dahdi_preregistration = FXO_card_dahdi_preregistration,
  1029. .card_dahdi_postregistration = FXO_card_dahdi_postregistration,
  1030. .card_hooksig = FXO_card_hooksig,
  1031. .card_tick = FXO_card_tick,
  1032. .card_pcm_recompute = generic_card_pcm_recompute,
  1033. .card_pcm_fromspan = generic_card_pcm_fromspan,
  1034. .card_pcm_tospan = generic_card_pcm_tospan,
  1035. .card_timing_priority = generic_timing_priority,
  1036. .card_ioctl = FXO_card_ioctl,
  1037. .card_open = FXO_card_open,
  1038. .card_register_reply = FXO_card_register_reply,
  1039. .XPD_STATE = XPROTO_CALLER(FXO, XPD_STATE),
  1040. },
  1041. .packet_is_valid = fxo_packet_is_valid,
  1042. .packet_dump = fxo_packet_dump,
  1043. };
  1044. static bool fxo_packet_is_valid(xpacket_t *pack)
  1045. {
  1046. const xproto_entry_t *xe;
  1047. //DBG(GENERAL, "\n");
  1048. xe = xproto_card_entry(&PROTO_TABLE(FXO), XPACKET_OP(pack));
  1049. return xe != NULL;
  1050. }
  1051. static void fxo_packet_dump(const char *msg, xpacket_t *pack)
  1052. {
  1053. DBG(GENERAL, "%s\n", msg);
  1054. }
  1055. /*------------------------- DAA Handling --------------------------*/
  1056. #ifdef CONFIG_PROC_FS
  1057. static int proc_fxo_info_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  1058. {
  1059. int len = 0;
  1060. unsigned long flags;
  1061. xpd_t *xpd = data;
  1062. struct FXO_priv_data *priv;
  1063. int i;
  1064. if(!xpd)
  1065. return -ENODEV;
  1066. spin_lock_irqsave(&xpd->lock, flags);
  1067. priv = xpd->priv;
  1068. BUG_ON(!priv);
  1069. len += sprintf(page + len, "\t%-17s: ", "Channel");
  1070. for_each_line(xpd, i) {
  1071. if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
  1072. len += sprintf(page + len, "%4d ", i % 10);
  1073. }
  1074. len += sprintf(page + len, "\nLeds:");
  1075. len += sprintf(page + len, "\n\t%-17s: ", "state");
  1076. for_each_line(xpd, i) {
  1077. if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
  1078. len += sprintf(page + len, " %d%d ",
  1079. IS_SET(priv->ledstate[LED_GREEN], i),
  1080. IS_SET(priv->ledstate[LED_RED], i));
  1081. }
  1082. len += sprintf(page + len, "\n\t%-17s: ", "blinking");
  1083. for_each_line(xpd, i) {
  1084. if(!IS_SET(xpd->digital_outputs, i) && !IS_SET(xpd->digital_inputs, i))
  1085. len += sprintf(page + len, " %d%d ",
  1086. IS_BLINKING(priv,i,LED_GREEN),
  1087. IS_BLINKING(priv,i,LED_RED));
  1088. }
  1089. len += sprintf(page + len, "\nBattery-Data:");
  1090. len += sprintf(page + len, "\n\t%-17s: ", "voltage");
  1091. for_each_line(xpd, i) {
  1092. len += sprintf(page + len, "%4d ", priv->battery_voltage[i]);
  1093. }
  1094. len += sprintf(page + len, "\n\t%-17s: ", "current");
  1095. for_each_line(xpd, i) {
  1096. len += sprintf(page + len, "%4d ", priv->battery_current[i]);
  1097. }
  1098. len += sprintf(page + len, "\nBattery:");
  1099. len += sprintf(page + len, "\n\t%-17s: ", "on");
  1100. for_each_line(xpd, i) {
  1101. char *bat;
  1102. if(priv->battery[i] == BATTERY_ON)
  1103. bat = "+";
  1104. else if(priv->battery[i] == BATTERY_OFF)
  1105. bat = "-";
  1106. else
  1107. bat = ".";
  1108. len += sprintf(page + len, "%4s ", bat);
  1109. }
  1110. len += sprintf(page + len, "\n\t%-17s: ", "debounce");
  1111. for_each_line(xpd, i) {
  1112. len += sprintf(page + len, "%4d ", priv->nobattery_debounce[i]);
  1113. }
  1114. len += sprintf(page + len, "\nPolarity-Reverse:");
  1115. len += sprintf(page + len, "\n\t%-17s: ", "polarity");
  1116. for_each_line(xpd, i) {
  1117. char *polname;
  1118. if(priv->polarity[i] == POL_POSITIVE)
  1119. polname = "+";
  1120. else if(priv->polarity[i] == POL_NEGATIVE)
  1121. polname = "-";
  1122. else
  1123. polname = ".";
  1124. len += sprintf(page + len, "%4s ", polname);
  1125. }
  1126. len += sprintf(page + len, "\n\t%-17s: ", "debounce");
  1127. for_each_line(xpd, i) {
  1128. len += sprintf(page + len, "%4d ", priv->polarity_debounce[i]);
  1129. }
  1130. len += sprintf(page + len, "\nPower-Denial:");
  1131. len += sprintf(page + len, "\n\t%-17s: ", "power");
  1132. for_each_line(xpd, i) {
  1133. char *curr;
  1134. if(priv->power[i] == POWER_ON)
  1135. curr = "+";
  1136. else if(priv->power[i] == POWER_OFF)
  1137. curr = "-";
  1138. else
  1139. curr = ".";
  1140. len += sprintf(page + len, "%4s ", curr);
  1141. }
  1142. len += sprintf(page + len, "\n\t%-17s: ", "safezone");
  1143. for_each_line(xpd, i) {
  1144. len += sprintf(page + len, "%4d ", priv->power_denial_safezone[i]);
  1145. }
  1146. len += sprintf(page + len, "\n\t%-17s: ", "delay");
  1147. for_each_line(xpd, i) {
  1148. len += sprintf(page + len, "%4d ", priv->power_denial_delay[i]);
  1149. }
  1150. #ifdef WITH_METERING
  1151. len += sprintf(page + len, "\nMetering:");
  1152. len += sprintf(page + len, "\n\t%-17s: ", "count");
  1153. for_each_line(xpd, i) {
  1154. len += sprintf(page + len, "%4d ", priv->metering_count[i]);
  1155. }
  1156. #endif
  1157. len += sprintf(page + len, "\n");
  1158. spin_unlock_irqrestore(&xpd->lock, flags);
  1159. if (len <= off+count)
  1160. *eof = 1;
  1161. *start = page + off;
  1162. len -= off;
  1163. if (len > count)
  1164. len = count;
  1165. if (len < 0)
  1166. len = 0;
  1167. return len;
  1168. }
  1169. #endif
  1170. #ifdef WITH_METERING
  1171. static int proc_xpd_metering_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  1172. {
  1173. int len = 0;
  1174. unsigned long flags;
  1175. xpd_t *xpd = data;
  1176. struct FXO_priv_data *priv;
  1177. int i;
  1178. if(!xpd)
  1179. return -ENODEV;
  1180. priv = xpd->priv;
  1181. BUG_ON(!priv);
  1182. spin_lock_irqsave(&xpd->lock, flags);
  1183. len += sprintf(page + len, "# Chan\tMeter (since last read)\n");
  1184. for_each_line(xpd, i) {
  1185. len += sprintf(page + len, "%d\t%d\n",
  1186. i, priv->metering_count[i]);
  1187. }
  1188. spin_unlock_irqrestore(&xpd->lock, flags);
  1189. if (len <= off+count)
  1190. *eof = 1;
  1191. *start = page + off;
  1192. len -= off;
  1193. if (len > count)
  1194. len = count;
  1195. if (len < 0)
  1196. len = 0;
  1197. /* Zero meters */
  1198. for_each_line(xpd, i)
  1199. priv->metering_count[i] = 0;
  1200. return len;
  1201. }
  1202. #endif
  1203. static DEVICE_ATTR_READER(fxo_battery_show, dev, buf)
  1204. {
  1205. xpd_t *xpd;
  1206. struct FXO_priv_data *priv;
  1207. unsigned long flags;
  1208. int len = 0;
  1209. int i;
  1210. BUG_ON(!dev);
  1211. xpd = dev_to_xpd(dev);
  1212. if(!xpd)
  1213. return -ENODEV;
  1214. priv = xpd->priv;
  1215. BUG_ON(!priv);
  1216. spin_lock_irqsave(&xpd->lock, flags);
  1217. for_each_line(xpd, i) {
  1218. char bat;
  1219. if(priv->battery[i] == BATTERY_ON)
  1220. bat = '+';
  1221. else if(priv->battery[i] == BATTERY_OFF)
  1222. bat = '-';
  1223. else
  1224. bat = '.';
  1225. len += sprintf(buf + len, "%c ", bat);
  1226. }
  1227. len += sprintf(buf + len, "\n");
  1228. spin_unlock_irqrestore(&xpd->lock, flags);
  1229. return len;
  1230. }
  1231. static DEVICE_ATTR(fxo_battery, S_IRUGO, fxo_battery_show, NULL);
  1232. static int fxo_xpd_probe(struct device *dev)
  1233. {
  1234. xpd_t *xpd;
  1235. int ret;
  1236. xpd = dev_to_xpd(dev);
  1237. /* Is it our device? */
  1238. if(xpd->type != XPD_TYPE_FXO) {
  1239. XPD_ERR(xpd, "drop suggestion for %s (%d)\n",
  1240. dev_name(dev), xpd->type);
  1241. return -EINVAL;
  1242. }
  1243. XPD_DBG(DEVICES, xpd, "SYSFS\n");
  1244. ret = device_create_file(dev, &dev_attr_fxo_battery);
  1245. if(ret) {
  1246. XPD_ERR(xpd, "%s: device_create_file(fxo_battery) failed: %d\n", __FUNCTION__, ret);
  1247. goto fail_fxo_battery;
  1248. }
  1249. return 0;
  1250. fail_fxo_battery:
  1251. return ret;
  1252. }
  1253. static int fxo_xpd_remove(struct device *dev)
  1254. {
  1255. xpd_t *xpd;
  1256. xpd = dev_to_xpd(dev);
  1257. XPD_DBG(DEVICES, xpd, "SYSFS\n");
  1258. device_remove_file(dev, &dev_attr_fxo_battery);
  1259. return 0;
  1260. }
  1261. static struct xpd_driver fxo_driver = {
  1262. .type = XPD_TYPE_FXO,
  1263. .driver = {
  1264. .name = "fxo",
  1265. #ifndef OLD_HOTPLUG_SUPPORT
  1266. .owner = THIS_MODULE,
  1267. #endif
  1268. .probe = fxo_xpd_probe,
  1269. .remove = fxo_xpd_remove
  1270. }
  1271. };
  1272. static int __init card_fxo_startup(void)
  1273. {
  1274. int ret;
  1275. if(ring_debounce <= 0) {
  1276. ERR("ring_debounce=%d. Must be positive number of ticks\n", ring_debounce);
  1277. return -EINVAL;
  1278. }
  1279. if((ret = xpd_driver_register(&fxo_driver.driver)) < 0)
  1280. return ret;
  1281. INFO("revision %s\n", XPP_VERSION);
  1282. #ifdef WITH_METERING
  1283. INFO("FEATURE: WITH METERING Detection\n");
  1284. #else
  1285. INFO("FEATURE: NO METERING Detection\n");
  1286. #endif
  1287. xproto_register(&PROTO_TABLE(FXO));
  1288. return 0;
  1289. }
  1290. static void __exit card_fxo_cleanup(void)
  1291. {
  1292. xproto_unregister(&PROTO_TABLE(FXO));
  1293. xpd_driver_unregister(&fxo_driver.driver);
  1294. }
  1295. MODULE_DESCRIPTION("XPP FXO Card Driver");
  1296. MODULE_AUTHOR("Oron Peled <oron@actcom.co.il>");
  1297. MODULE_LICENSE("GPL");
  1298. MODULE_VERSION(XPP_VERSION);
  1299. MODULE_ALIAS_XPD(XPD_TYPE_FXO);
  1300. module_init(card_fxo_startup);
  1301. module_exit(card_fxo_cleanup);