card_bri.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. /*
  2. * Written by Oron Peled <oron@actcom.co.il>
  3. * Copyright (C) 2004-2006, Xorcom
  4. *
  5. * Parts derived from Cologne demo driver for the chip.
  6. *
  7. * All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/fs.h>
  27. #include <linux/delay.h>
  28. #include <linux/seq_file.h>
  29. #include "xpd.h"
  30. #include "xproto.h"
  31. #include "xpp_dahdi.h"
  32. #include "card_bri.h"
  33. #include "dahdi_debug.h"
  34. #include "xbus-core.h"
  35. static const char rcsid[] = "$Id$";
  36. #ifndef DAHDI_SIG_HARDHDLC
  37. #error Cannot build BRI without HARDHDLC supprt
  38. #endif
  39. /* must be before dahdi_debug.h */
  40. static DEF_PARM(int, debug, 0, 0644, "Print DBG statements");
  41. static DEF_PARM(uint, poll_interval, 500, 0644,
  42. "Poll channel state interval in milliseconds (0 - disable)");
  43. static DEF_PARM_BOOL(nt_keepalive, 1, 0644,
  44. "Force BRI_NT to keep trying connection");
  45. enum xhfc_states {
  46. ST_RESET = 0, /* G/F0 */
  47. /* TE */
  48. ST_TE_SENSING = 2, /* F2 */
  49. ST_TE_DEACTIVATED = 3, /* F3 */
  50. ST_TE_SIGWAIT = 4, /* F4 */
  51. ST_TE_IDENT = 5, /* F5 */
  52. ST_TE_SYNCED = 6, /* F6 */
  53. ST_TE_ACTIVATED = 7, /* F7 */
  54. ST_TE_LOST_FRAMING = 8, /* F8 */
  55. /* NT */
  56. ST_NT_DEACTIVATED = 1, /* G1 */
  57. ST_NT_ACTIVATING = 2, /* G2 */
  58. ST_NT_ACTIVATED = 3, /* G3 */
  59. ST_NT_DEACTIVTING = 4, /* G4 */
  60. };
  61. #ifdef CONFIG_PROC_FS
  62. static const char *xhfc_state_name(bool is_nt, enum xhfc_states state)
  63. {
  64. const char *p;
  65. #define _E(x) [ST_ ## x] = #x
  66. static const char *te_names[] = {
  67. _E(RESET),
  68. _E(TE_SENSING),
  69. _E(TE_DEACTIVATED),
  70. _E(TE_SIGWAIT),
  71. _E(TE_IDENT),
  72. _E(TE_SYNCED),
  73. _E(TE_ACTIVATED),
  74. _E(TE_LOST_FRAMING),
  75. };
  76. static const char *nt_names[] = {
  77. _E(RESET),
  78. _E(NT_DEACTIVATED),
  79. _E(NT_ACTIVATING),
  80. _E(NT_ACTIVATED),
  81. _E(NT_DEACTIVTING),
  82. };
  83. #undef _E
  84. if (is_nt) {
  85. if (state > ST_NT_DEACTIVTING)
  86. p = "NT ???";
  87. else
  88. p = nt_names[state];
  89. } else {
  90. if (state > ST_TE_LOST_FRAMING)
  91. p = "TE ???";
  92. else
  93. p = te_names[state];
  94. }
  95. return p;
  96. }
  97. #endif
  98. /* xhfc Layer1 physical commands */
  99. #define HFC_L1_ACTIVATE_TE 0x01
  100. #define HFC_L1_FORCE_DEACTIVATE_TE 0x02
  101. #define HFC_L1_ACTIVATE_NT 0x03
  102. #define HFC_L1_DEACTIVATE_NT 0x04
  103. #define HFC_L1_ACTIVATING 1
  104. #define HFC_L1_ACTIVATED 2
  105. #define HFC_TIMER_T1 2500
  106. #define HFC_TIMER_T3 8000 /* 8s activation timer T3 */
  107. #define HFC_TIMER_OFF -1 /* timer disabled */
  108. #define A_SU_WR_STA 0x30 /* ST/Up state machine register */
  109. #define V_SU_LD_STA 0x10
  110. #define V_SU_ACT 0x60 /* start activation/deactivation */
  111. #define STA_DEACTIVATE 0x40 /* start deactivation in A_SU_WR_STA */
  112. #define STA_ACTIVATE 0x60 /* start activation in A_SU_WR_STA */
  113. #define V_SU_SET_G2_G3 0x80
  114. #define A_SU_RD_STA 0x30
  115. typedef union {
  116. struct {
  117. __u8 v_su_sta:4;
  118. __u8 v_su_fr_sync:1;
  119. __u8 v_su_t2_exp:1;
  120. __u8 v_su_info0:1;
  121. __u8 v_g2_g3:1;
  122. } bits;
  123. __u8 reg;
  124. } su_rd_sta_t;
  125. #define REG30_LOST 3 /* in polls */
  126. #define DCHAN_LOST 15000 /* in ticks */
  127. #define BRI_DCHAN_SIGCAP DAHDI_SIG_HARDHDLC
  128. #define BRI_BCHAN_SIGCAP (DAHDI_SIG_CLEAR | DAHDI_SIG_DACS)
  129. #define IS_NT(xpd) (PHONEDEV(xpd).direction == TO_PHONE)
  130. #define BRI_PORT(xpd) ((xpd)->addr.subunit)
  131. /* shift in PCM highway */
  132. #define SUBUNIT_PCM_SHIFT 4
  133. #define PCM_SHIFT(mask, sunit) ((mask) << (SUBUNIT_PCM_SHIFT * (sunit)))
  134. /*---------------- BRI Protocol Commands ----------------------------------*/
  135. static int write_state_register(xpd_t *xpd, __u8 value);
  136. static bool bri_packet_is_valid(xpacket_t *pack);
  137. static void bri_packet_dump(const char *msg, xpacket_t *pack);
  138. #ifdef CONFIG_PROC_FS
  139. static const struct file_operations proc_bri_info_ops;
  140. #endif
  141. static int bri_spanconfig(struct file *file, struct dahdi_span *span,
  142. struct dahdi_lineconfig *lc);
  143. static int bri_chanconfig(struct file *file, struct dahdi_chan *chan,
  144. int sigtype);
  145. static int bri_startup(struct file *file, struct dahdi_span *span);
  146. static int bri_shutdown(struct dahdi_span *span);
  147. #define PROC_REGISTER_FNAME "slics"
  148. #define PROC_BRI_INFO_FNAME "bri_info"
  149. enum led_state {
  150. BRI_LED_OFF = 0x0,
  151. BRI_LED_ON = 0x1,
  152. /*
  153. * We blink by software from driver, so that
  154. * if the driver malfunction that blink would stop.
  155. */
  156. // BRI_LED_BLINK_SLOW = 0x2, /* 1/2 a second blink cycle */
  157. // BRI_LED_BLINK_FAST = 0x3 /* 1/4 a second blink cycle */
  158. };
  159. enum bri_led_names {
  160. GREEN_LED = 0,
  161. RED_LED = 1
  162. };
  163. #define NUM_LEDS 2
  164. #define LED_TICKS 100
  165. struct bri_leds {
  166. __u8 state:2;
  167. __u8 led_sel:1; /* 0 - GREEN, 1 - RED */
  168. __u8 reserved:5;
  169. };
  170. #ifndef MAX_DFRAME_LEN_L1
  171. #define MAX_DFRAME_LEN_L1 300
  172. #endif
  173. #define DCHAN_BUFSIZE MAX_DFRAME_LEN_L1
  174. struct BRI_priv_data {
  175. struct proc_dir_entry *bri_info;
  176. su_rd_sta_t state_register;
  177. bool initialized;
  178. bool dchan_is_open;
  179. int t1; /* timer 1 for NT deactivation */
  180. int t3; /* timer 3 for TE activation */
  181. ulong l1_flags;
  182. bool reg30_good;
  183. uint reg30_ticks;
  184. bool layer1_up;
  185. /*
  186. * D-Chan: buffers + extra state info.
  187. */
  188. atomic_t hdlc_pending;
  189. bool txframe_begin;
  190. uint tick_counter;
  191. uint poll_counter;
  192. uint dchan_tx_counter;
  193. uint dchan_rx_counter;
  194. uint dchan_rx_drops;
  195. bool dchan_alive;
  196. uint dchan_alive_ticks;
  197. uint dchan_notx_ticks;
  198. uint dchan_norx_ticks;
  199. enum led_state ledstate[NUM_LEDS];
  200. };
  201. static xproto_table_t PROTO_TABLE(BRI);
  202. DEF_RPACKET_DATA(BRI, SET_LED, /* Set one of the LED's */
  203. struct bri_leds bri_leds;);
  204. static /* 0x33 */ DECLARE_CMD(BRI, SET_LED, enum bri_led_names which_led,
  205. enum led_state to_led_state);
  206. #define DO_LED(xpd, which, tostate) \
  207. CALL_PROTO(BRI, SET_LED, (xpd)->xbus, (xpd), (which), (tostate))
  208. #define DEBUG_BUF_SIZE (100)
  209. static void dump_hex_buf(xpd_t *xpd, char *msg, __u8 *buf, size_t len)
  210. {
  211. char debug_buf[DEBUG_BUF_SIZE + 1];
  212. int i;
  213. int n = 0;
  214. debug_buf[0] = '\0';
  215. for (i = 0; i < len && n < DEBUG_BUF_SIZE; i++)
  216. n += snprintf(&debug_buf[n], DEBUG_BUF_SIZE - n, "%02X ",
  217. buf[i]);
  218. XPD_NOTICE(xpd, "%s[0..%zd]: %s%s\n", msg, len - 1, debug_buf,
  219. (n >= DEBUG_BUF_SIZE) ? "..." : "");
  220. }
  221. static void dump_dchan_packet(xpd_t *xpd, bool transmit, __u8 *buf, int len)
  222. {
  223. struct BRI_priv_data *priv;
  224. char msgbuf[MAX_PROC_WRITE];
  225. char ftype = '?';
  226. char *direction;
  227. int frame_begin;
  228. priv = xpd->priv;
  229. BUG_ON(!priv);
  230. if (transmit) {
  231. direction = "TX";
  232. frame_begin = priv->txframe_begin;
  233. } else {
  234. direction = "RX";
  235. frame_begin = 1;
  236. }
  237. if (frame_begin) { /* Packet start */
  238. if (!IS_SET(buf[0], 7))
  239. ftype = 'I'; /* Information */
  240. else if (IS_SET(buf[0], 7) && !IS_SET(buf[0], 6))
  241. ftype = 'S'; /* Supervisory */
  242. else if (IS_SET(buf[0], 7) && IS_SET(buf[0], 6))
  243. ftype = 'U'; /* Unnumbered */
  244. else
  245. XPD_NOTICE(xpd, "Unknown frame type 0x%X\n", buf[0]);
  246. snprintf(msgbuf, MAX_PROC_WRITE, "D-Chan %s = (%c) ", direction,
  247. ftype);
  248. } else {
  249. snprintf(msgbuf, MAX_PROC_WRITE, "D-Chan %s = ", direction);
  250. }
  251. dump_hex_buf(xpd, msgbuf, buf, len);
  252. }
  253. static void set_bri_timer(xpd_t *xpd, const char *name, int *bri_timer,
  254. int value)
  255. {
  256. if (value == HFC_TIMER_OFF)
  257. XPD_DBG(SIGNAL, xpd, "Timer %s DISABLE\n", name);
  258. else
  259. XPD_DBG(SIGNAL, xpd, "Timer %s: set to %d\n", name, value);
  260. *bri_timer = value;
  261. }
  262. static void dchan_state(xpd_t *xpd, bool up)
  263. {
  264. struct BRI_priv_data *priv;
  265. BUG_ON(!xpd);
  266. priv = xpd->priv;
  267. BUG_ON(!priv);
  268. if (priv->dchan_alive == up)
  269. return;
  270. if (up) {
  271. XPD_DBG(SIGNAL, xpd, "STATE CHANGE: D-Channel RUNNING\n");
  272. priv->dchan_alive = 1;
  273. } else {
  274. XPD_DBG(SIGNAL, xpd, "STATE CHANGE: D-Channel STOPPED\n");
  275. priv->dchan_rx_counter = priv->dchan_tx_counter =
  276. priv->dchan_rx_drops = 0;
  277. priv->dchan_alive = 0;
  278. priv->dchan_alive_ticks = 0;
  279. }
  280. }
  281. static void layer1_state(xpd_t *xpd, bool up)
  282. {
  283. struct BRI_priv_data *priv;
  284. BUG_ON(!xpd);
  285. priv = xpd->priv;
  286. BUG_ON(!priv);
  287. if (priv->layer1_up == up)
  288. return;
  289. priv->layer1_up = up;
  290. XPD_DBG(SIGNAL, xpd, "STATE CHANGE: Layer1 %s\n", (up) ? "UP" : "DOWN");
  291. if (!up)
  292. dchan_state(xpd, 0);
  293. }
  294. static void te_activation(xpd_t *xpd, bool on)
  295. {
  296. struct BRI_priv_data *priv;
  297. __u8 curr_state;
  298. BUG_ON(!xpd);
  299. priv = xpd->priv;
  300. BUG_ON(!priv);
  301. curr_state = priv->state_register.bits.v_su_sta;
  302. XPD_DBG(SIGNAL, xpd, "%s\n", (on) ? "ON" : "OFF");
  303. if (on) {
  304. if (curr_state == ST_TE_DEACTIVATED) {
  305. XPD_DBG(SIGNAL, xpd, "HFC_L1_ACTIVATE_TE\n");
  306. set_bit(HFC_L1_ACTIVATING, &priv->l1_flags);
  307. write_state_register(xpd, STA_ACTIVATE);
  308. set_bri_timer(xpd, "T3", &priv->t3, HFC_TIMER_T3);
  309. } else {
  310. XPD_DBG(SIGNAL, xpd,
  311. "HFC_L1_ACTIVATE_TE (state %d, ignored)\n",
  312. curr_state);
  313. }
  314. } else { /* happen only because of T3 expiry */
  315. switch (curr_state) {
  316. case ST_TE_DEACTIVATED: /* F3 */
  317. case ST_TE_SYNCED: /* F6 */
  318. case ST_TE_ACTIVATED: /* F7 */
  319. XPD_DBG(SIGNAL, xpd,
  320. "HFC_L1_FORCE_DEACTIVATE_TE "
  321. "(state %d, ignored)\n",
  322. curr_state);
  323. break;
  324. case ST_TE_SIGWAIT: /* F4 */
  325. case ST_TE_IDENT: /* F5 */
  326. case ST_TE_LOST_FRAMING: /* F8 */
  327. XPD_DBG(SIGNAL, xpd, "HFC_L1_FORCE_DEACTIVATE_TE\n");
  328. write_state_register(xpd, STA_DEACTIVATE);
  329. break;
  330. default:
  331. XPD_NOTICE(xpd, "Bad TE state: %d\n", curr_state);
  332. break;
  333. }
  334. }
  335. }
  336. static void nt_activation(xpd_t *xpd, bool on)
  337. {
  338. struct BRI_priv_data *priv;
  339. __u8 curr_state;
  340. BUG_ON(!xpd);
  341. priv = xpd->priv;
  342. BUG_ON(!priv);
  343. curr_state = priv->state_register.bits.v_su_sta;
  344. XPD_DBG(SIGNAL, xpd, "%s\n", (on) ? "ON" : "OFF");
  345. if (on) {
  346. switch (curr_state) {
  347. case ST_RESET: /* F/G 0 */
  348. case ST_NT_DEACTIVATED: /* G1 */
  349. case ST_NT_DEACTIVTING: /* G4 */
  350. XPD_DBG(SIGNAL, xpd, "HFC_L1_ACTIVATE_NT\n");
  351. set_bri_timer(xpd, "T1", &priv->t1, HFC_TIMER_T1);
  352. set_bit(HFC_L1_ACTIVATING, &priv->l1_flags);
  353. write_state_register(xpd, STA_ACTIVATE);
  354. break;
  355. case ST_NT_ACTIVATING: /* G2 */
  356. case ST_NT_ACTIVATED: /* G3 */
  357. XPD_DBG(SIGNAL, xpd,
  358. "HFC_L1_ACTIVATE_NT (in state %d, ignored)\n",
  359. curr_state);
  360. break;
  361. }
  362. } else {
  363. switch (curr_state) {
  364. case ST_RESET: /* F/G 0 */
  365. case ST_NT_DEACTIVATED: /* G1 */
  366. case ST_NT_DEACTIVTING: /* G4 */
  367. XPD_DBG(SIGNAL, xpd,
  368. "HFC_L1_DEACTIVATE_NT (in state %d, ignored)\n",
  369. curr_state);
  370. break;
  371. case ST_NT_ACTIVATING: /* G2 */
  372. case ST_NT_ACTIVATED: /* G3 */
  373. XPD_DBG(SIGNAL, xpd, "HFC_L1_DEACTIVATE_NT\n");
  374. write_state_register(xpd, STA_DEACTIVATE);
  375. break;
  376. default:
  377. XPD_NOTICE(xpd, "Bad NT state: %d\n", curr_state);
  378. break;
  379. }
  380. }
  381. }
  382. /*
  383. * D-Chan receive
  384. */
  385. static int bri_check_stat(xpd_t *xpd, struct dahdi_chan *dchan, __u8 *buf,
  386. int len)
  387. {
  388. struct BRI_priv_data *priv;
  389. __u8 status;
  390. priv = xpd->priv;
  391. BUG_ON(!priv);
  392. if (len <= 0) {
  393. XPD_NOTICE(xpd, "D-Chan RX DROP: short frame (len=%d)\n", len);
  394. dahdi_hdlc_abort(dchan, DAHDI_EVENT_ABORT);
  395. return -EPROTO;
  396. }
  397. status = buf[len - 1];
  398. if (status) {
  399. int event = DAHDI_EVENT_ABORT;
  400. if (status == 0xFF) {
  401. XPD_NOTICE(xpd, "D-Chan RX DROP: ABORT: %d\n", status);
  402. } else {
  403. XPD_NOTICE(xpd, "D-Chan RX DROP: BADFCS: %d\n", status);
  404. event = DAHDI_EVENT_BADFCS;
  405. }
  406. dump_hex_buf(xpd, "D-Chan RX: current packet", buf, len);
  407. dahdi_hdlc_abort(dchan, event);
  408. return -EPROTO;
  409. }
  410. return 0;
  411. }
  412. static int rx_dchan(xpd_t *xpd, reg_cmd_t *regcmd)
  413. {
  414. struct BRI_priv_data *priv;
  415. __u8 *src;
  416. struct dahdi_chan *dchan;
  417. uint len;
  418. bool eoframe;
  419. int ret = 0;
  420. src = REG_XDATA(regcmd);
  421. len = regcmd->bytes;
  422. eoframe = regcmd->eoframe;
  423. if (len <= 0)
  424. return 0;
  425. if (!SPAN_REGISTERED(xpd)) /* Nowhere to copy data */
  426. return 0;
  427. BUG_ON(!xpd);
  428. priv = xpd->priv;
  429. BUG_ON(!priv);
  430. dchan = XPD_CHAN(xpd, 2);
  431. if (!IS_OFFHOOK(xpd, 2)) { /* D-chan is used? */
  432. static int rate_limit;
  433. if ((rate_limit++ % 1000) == 0)
  434. XPD_DBG(SIGNAL, xpd, "D-Chan unused\n");
  435. goto out;
  436. }
  437. XPD_DBG(GENERAL, xpd, "D-Chan RX: eoframe=%d len=%d\n", eoframe, len);
  438. dahdi_hdlc_putbuf(dchan, src, (eoframe) ? len - 1 : len);
  439. if (!eoframe)
  440. goto out;
  441. if ((ret = bri_check_stat(xpd, dchan, src, len)) < 0)
  442. goto out;
  443. /*
  444. * Tell Dahdi that we received len-1 bytes.
  445. * They include the data and a 2-byte checksum.
  446. * The last byte (that we don't pass on) is 0 if the
  447. * checksum is correct. If it were wrong, we would drop the
  448. * packet in the "if (src[len-1])" above.
  449. */
  450. dahdi_hdlc_finish(dchan);
  451. priv->dchan_rx_counter++;
  452. priv->dchan_norx_ticks = 0;
  453. out:
  454. return ret;
  455. }
  456. /*
  457. * D-Chan transmit
  458. */
  459. /*
  460. * DAHDI calls this when it has data it wants to send to
  461. * the HDLC controller
  462. */
  463. static void bri_hdlc_hard_xmit(struct dahdi_chan *chan)
  464. {
  465. xpd_t *xpd = chan->pvt;
  466. struct dahdi_chan *dchan;
  467. struct BRI_priv_data *priv;
  468. priv = xpd->priv;
  469. BUG_ON(!priv);
  470. dchan = XPD_CHAN(xpd, 2);
  471. if (dchan == chan)
  472. atomic_inc(&priv->hdlc_pending);
  473. }
  474. static int send_dchan_frame(xpd_t *xpd, xframe_t *xframe, bool is_eof)
  475. {
  476. struct BRI_priv_data *priv;
  477. int ret;
  478. XPD_DBG(COMMANDS, xpd, "eoframe=%d\n", is_eof);
  479. priv = xpd->priv;
  480. if (!test_bit(HFC_L1_ACTIVATED, &priv->l1_flags)
  481. && !test_bit(HFC_L1_ACTIVATING, &priv->l1_flags)) {
  482. XPD_DBG(SIGNAL, xpd,
  483. "Want to transmit: Kick D-Channel transmiter\n");
  484. if (!IS_NT(xpd))
  485. te_activation(xpd, 1);
  486. else
  487. nt_activation(xpd, 1);
  488. }
  489. dump_xframe("send_dchan_frame", xpd->xbus, xframe, debug);
  490. ret = send_cmd_frame(xpd->xbus, xframe);
  491. if (ret < 0)
  492. XPD_ERR(xpd, "%s: failed sending xframe\n", __func__);
  493. if (is_eof) {
  494. atomic_dec(&priv->hdlc_pending);
  495. priv->dchan_tx_counter++;
  496. priv->txframe_begin = 1;
  497. } else
  498. priv->txframe_begin = 0;
  499. priv->dchan_notx_ticks = 0;
  500. return ret;
  501. }
  502. /*
  503. * Fill a single multibyte REGISTER_REQUEST
  504. */
  505. static void fill_multibyte(xpd_t *xpd, xpacket_t *pack,
  506. bool eoframe, char *buf, int len)
  507. {
  508. reg_cmd_t *reg_cmd;
  509. char *p;
  510. XPACKET_INIT(pack, GLOBAL, REGISTER_REQUEST, xpd->xbus_idx, 0, 0);
  511. XPACKET_LEN(pack) = RPACKET_SIZE(GLOBAL, REGISTER_REQUEST);
  512. reg_cmd = &RPACKET_FIELD(pack, GLOBAL, REGISTER_REQUEST, reg_cmd);
  513. reg_cmd->bytes = len;
  514. reg_cmd->is_multibyte = 1;
  515. reg_cmd->portnum = xpd->addr.subunit;
  516. reg_cmd->eoframe = eoframe;
  517. p = REG_XDATA(reg_cmd);
  518. memcpy(p, buf, len);
  519. if (debug)
  520. dump_dchan_packet(xpd, 1, p, len);
  521. }
  522. /*
  523. * Transmit available D-Channel frames
  524. *
  525. * - FPGA firmware expect to get this as a sequence of REGISTER_REQUEST
  526. * multibyte commands.
  527. * - The payload of each command is limited to MULTIBYTE_MAX_LEN bytes.
  528. * - We batch several REGISTER_REQUEST packets into a single xframe.
  529. * - The xframe is terminated when we get a bri "end of frame"
  530. * or when the xframe is full (should not happen).
  531. */
  532. static int tx_dchan(xpd_t *xpd)
  533. {
  534. struct BRI_priv_data *priv;
  535. xframe_t *xframe;
  536. xpacket_t *pack;
  537. int packet_count;
  538. int eoframe;
  539. int ret;
  540. priv = xpd->priv;
  541. BUG_ON(!priv);
  542. if (atomic_read(&priv->hdlc_pending) == 0)
  543. return 0;
  544. if (!SPAN_REGISTERED(xpd)
  545. || !(PHONEDEV(xpd).span.flags & DAHDI_FLAG_RUNNING))
  546. return 0;
  547. /* Allocate frame */
  548. xframe = ALLOC_SEND_XFRAME(xpd->xbus);
  549. if (!xframe) {
  550. XPD_NOTICE(xpd, "%s: failed to allocate new xframe\n",
  551. __func__);
  552. return -ENOMEM;
  553. }
  554. for (packet_count = 0, eoframe = 0; !eoframe; packet_count++) {
  555. int packet_len = RPACKET_SIZE(GLOBAL, REGISTER_REQUEST);
  556. char buf[MULTIBYTE_MAX_LEN];
  557. int len = MULTIBYTE_MAX_LEN;
  558. /* Reserve packet */
  559. pack = xframe_next_packet(xframe, packet_len);
  560. if (!pack) {
  561. BUG_ON(!packet_count);
  562. /*
  563. * A split. Send what we currently have.
  564. */
  565. XPD_NOTICE(xpd, "%s: xframe is full (%d packets)\n",
  566. __func__, packet_count);
  567. break;
  568. }
  569. /* Get data from DAHDI */
  570. eoframe = dahdi_hdlc_getbuf(XPD_CHAN(xpd, 2), buf, &len);
  571. if (len <= 0) {
  572. /*
  573. * Already checked priv->hdlc_pending,
  574. * should never get here.
  575. */
  576. if (printk_ratelimit())
  577. XPD_ERR(xpd,
  578. "%s: hdlc_pending, but nothing "
  579. "to transmit?\n", __func__);
  580. FREE_SEND_XFRAME(xpd->xbus, xframe);
  581. return -EINVAL;
  582. }
  583. BUG_ON(len > MULTIBYTE_MAX_LEN);
  584. fill_multibyte(xpd, pack, eoframe != 0, buf, len);
  585. }
  586. return send_dchan_frame(xpd, xframe, eoframe != 0);
  587. return ret;
  588. }
  589. /*---------------- BRI: Methods -------------------------------------------*/
  590. static void bri_proc_remove(xbus_t *xbus, xpd_t *xpd)
  591. {
  592. struct BRI_priv_data *priv;
  593. BUG_ON(!xpd);
  594. priv = xpd->priv;
  595. XPD_DBG(PROC, xpd, "\n");
  596. #ifdef CONFIG_PROC_FS
  597. if (priv->bri_info) {
  598. XPD_DBG(PROC, xpd, "Removing '%s'\n", PROC_BRI_INFO_FNAME);
  599. remove_proc_entry(PROC_BRI_INFO_FNAME, xpd->proc_xpd_dir);
  600. }
  601. #endif
  602. }
  603. static int bri_proc_create(xbus_t *xbus, xpd_t *xpd)
  604. {
  605. struct BRI_priv_data *priv;
  606. BUG_ON(!xpd);
  607. priv = xpd->priv;
  608. XPD_DBG(PROC, xpd, "\n");
  609. #ifdef CONFIG_PROC_FS
  610. XPD_DBG(PROC, xpd, "Creating '%s'\n", PROC_BRI_INFO_FNAME);
  611. priv->bri_info = proc_create_data(PROC_BRI_INFO_FNAME, 0444,
  612. xpd->proc_xpd_dir, &proc_bri_info_ops, xpd);
  613. if (!priv->bri_info) {
  614. XPD_ERR(xpd, "Failed to create proc file '%s'\n",
  615. PROC_BRI_INFO_FNAME);
  616. bri_proc_remove(xbus, xpd);
  617. return -EINVAL;
  618. }
  619. SET_PROC_DIRENTRY_OWNER(priv->bri_info);
  620. #endif
  621. return 0;
  622. }
  623. static xpd_t *BRI_card_new(xbus_t *xbus, int unit, int subunit,
  624. const xproto_table_t *proto_table, __u8 subtype,
  625. int subunits, int subunit_ports, bool to_phone)
  626. {
  627. xpd_t *xpd = NULL;
  628. int channels = min(3, CHANNELS_PERXPD);
  629. if (subunit_ports != 1) {
  630. XBUS_ERR(xbus, "Bad subunit_ports=%d\n", subunit_ports);
  631. return NULL;
  632. }
  633. XBUS_DBG(GENERAL, xbus, "\n");
  634. xpd =
  635. xpd_alloc(xbus, unit, subunit, subtype, subunits,
  636. sizeof(struct BRI_priv_data), proto_table, channels);
  637. if (!xpd)
  638. return NULL;
  639. PHONEDEV(xpd).direction = (to_phone) ? TO_PHONE : TO_PSTN;
  640. xpd->type_name = (to_phone) ? "BRI_NT" : "BRI_TE";
  641. xbus->sync_mode_default = SYNC_MODE_AB;
  642. if (bri_proc_create(xbus, xpd) < 0)
  643. goto err;
  644. return xpd;
  645. err:
  646. xpd_free(xpd);
  647. return NULL;
  648. }
  649. static int BRI_card_init(xbus_t *xbus, xpd_t *xpd)
  650. {
  651. struct BRI_priv_data *priv;
  652. BUG_ON(!xpd);
  653. XPD_DBG(GENERAL, xpd, "\n");
  654. priv = xpd->priv;
  655. DO_LED(xpd, GREEN_LED, BRI_LED_OFF);
  656. DO_LED(xpd, RED_LED, BRI_LED_OFF);
  657. set_bri_timer(xpd, "T1", &priv->t1, HFC_TIMER_OFF);
  658. write_state_register(xpd, 0); /* Enable L1 state machine */
  659. priv->initialized = 1;
  660. return 0;
  661. }
  662. static int BRI_card_remove(xbus_t *xbus, xpd_t *xpd)
  663. {
  664. BUG_ON(!xpd);
  665. XPD_DBG(GENERAL, xpd, "\n");
  666. bri_proc_remove(xbus, xpd);
  667. return 0;
  668. }
  669. #ifdef DAHDI_AUDIO_NOTIFY
  670. static int bri_audio_notify(struct dahdi_chan *chan, int on)
  671. {
  672. xpd_t *xpd = chan->pvt;
  673. int pos = chan->chanpos - 1;
  674. BUG_ON(!xpd);
  675. LINE_DBG(SIGNAL, xpd, pos, "BRI-AUDIO: %s\n", (on) ? "on" : "off");
  676. mark_offhook(xpd, pos, on);
  677. return 0;
  678. }
  679. #endif
  680. static const struct dahdi_span_ops BRI_span_ops = {
  681. .owner = THIS_MODULE,
  682. .spanconfig = bri_spanconfig,
  683. .chanconfig = bri_chanconfig,
  684. .startup = bri_startup,
  685. .shutdown = bri_shutdown,
  686. .hdlc_hard_xmit = bri_hdlc_hard_xmit,
  687. .open = xpp_open,
  688. .close = xpp_close,
  689. .hooksig = xpp_hooksig, /* Only with RBS bits */
  690. .ioctl = xpp_ioctl,
  691. .maint = xpp_maint,
  692. .echocan_create = xpp_echocan_create,
  693. .echocan_name = xpp_echocan_name,
  694. .assigned = xpp_span_assigned,
  695. #ifdef DAHDI_SYNC_TICK
  696. .sync_tick = dahdi_sync_tick,
  697. #endif
  698. #ifdef CONFIG_DAHDI_WATCHDOG
  699. .watchdog = xpp_watchdog,
  700. #endif
  701. #ifdef DAHDI_AUDIO_NOTIFY
  702. .audio_notify = bri_audio_notify,
  703. #endif
  704. };
  705. static int BRI_card_dahdi_preregistration(xpd_t *xpd, bool on)
  706. {
  707. xbus_t *xbus;
  708. struct BRI_priv_data *priv;
  709. int i;
  710. BUG_ON(!xpd);
  711. xbus = xpd->xbus;
  712. priv = xpd->priv;
  713. BUG_ON(!xbus);
  714. XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
  715. if (!on) {
  716. /* Nothing to do yet */
  717. return 0;
  718. }
  719. PHONEDEV(xpd).span.spantype =
  720. (PHONEDEV(xpd).direction == TO_PHONE)
  721. ? SPANTYPE_DIGITAL_BRI_NT
  722. : SPANTYPE_DIGITAL_BRI_TE;
  723. PHONEDEV(xpd).span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_CCS;
  724. PHONEDEV(xpd).span.deflaw = DAHDI_LAW_ALAW;
  725. BIT_SET(PHONEDEV(xpd).digital_signalling, 2); /* D-Channel */
  726. for_each_line(xpd, i) {
  727. struct dahdi_chan *cur_chan = XPD_CHAN(xpd, i);
  728. XPD_DBG(GENERAL, xpd, "setting BRI channel %d\n", i);
  729. snprintf(cur_chan->name, MAX_CHANNAME, "XPP_%s/%02d/%1d%1d/%d",
  730. xpd->type_name, xbus->num, xpd->addr.unit,
  731. xpd->addr.subunit, i);
  732. cur_chan->chanpos = i + 1;
  733. cur_chan->pvt = xpd;
  734. if (i == 2) { /* D-CHAN */
  735. cur_chan->sigcap = BRI_DCHAN_SIGCAP;
  736. clear_bit(DAHDI_FLAGBIT_HDLC, &cur_chan->flags);
  737. priv->txframe_begin = 1;
  738. atomic_set(&priv->hdlc_pending, 0);
  739. } else {
  740. cur_chan->sigcap = BRI_BCHAN_SIGCAP;
  741. }
  742. }
  743. CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
  744. PHONEDEV(xpd).span.ops = &BRI_span_ops;
  745. return 0;
  746. }
  747. static int BRI_card_dahdi_postregistration(xpd_t *xpd, bool on)
  748. {
  749. xbus_t *xbus;
  750. BUG_ON(!xpd);
  751. xbus = xpd->xbus;
  752. BUG_ON(!xbus);
  753. XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "on" : "off");
  754. return (0);
  755. }
  756. static int BRI_card_hooksig(xpd_t *xpd, int pos, enum dahdi_txsig txsig)
  757. {
  758. LINE_DBG(SIGNAL, xpd, pos, "%s\n", txsig2str(txsig));
  759. return 0;
  760. }
  761. /*
  762. * LED managment is done by the driver now:
  763. * - Turn constant ON RED/GREEN led to indicate NT/TE port
  764. * - Very fast "Double Blink" to indicate Layer1 alive (without D-Channel)
  765. * - Constant blink (1/2 sec cycle) to indicate D-Channel alive.
  766. */
  767. static void handle_leds(xbus_t *xbus, xpd_t *xpd)
  768. {
  769. struct BRI_priv_data *priv;
  770. unsigned int timer_count;
  771. int which_led;
  772. int other_led;
  773. int mod;
  774. BUG_ON(!xpd);
  775. if (IS_NT(xpd)) {
  776. which_led = RED_LED;
  777. other_led = GREEN_LED;
  778. } else {
  779. which_led = GREEN_LED;
  780. other_led = RED_LED;
  781. }
  782. priv = xpd->priv;
  783. BUG_ON(!priv);
  784. timer_count = xpd->timer_count;
  785. if (xpd->blink_mode) {
  786. if ((timer_count % DEFAULT_LED_PERIOD) == 0) {
  787. // led state is toggled
  788. if (priv->ledstate[which_led] == BRI_LED_OFF) {
  789. DO_LED(xpd, which_led, BRI_LED_ON);
  790. DO_LED(xpd, other_led, BRI_LED_ON);
  791. } else {
  792. DO_LED(xpd, which_led, BRI_LED_OFF);
  793. DO_LED(xpd, other_led, BRI_LED_OFF);
  794. }
  795. }
  796. return;
  797. }
  798. if (priv->ledstate[other_led] != BRI_LED_OFF)
  799. DO_LED(xpd, other_led, BRI_LED_OFF);
  800. if (priv->dchan_alive) {
  801. mod = timer_count % 1000;
  802. switch (mod) {
  803. case 0:
  804. DO_LED(xpd, which_led, BRI_LED_ON);
  805. break;
  806. case 500:
  807. DO_LED(xpd, which_led, BRI_LED_OFF);
  808. break;
  809. }
  810. } else if (priv->layer1_up) {
  811. mod = timer_count % 1000;
  812. switch (mod) {
  813. case 0:
  814. case 100:
  815. DO_LED(xpd, which_led, BRI_LED_ON);
  816. break;
  817. case 50:
  818. case 150:
  819. DO_LED(xpd, which_led, BRI_LED_OFF);
  820. break;
  821. }
  822. } else {
  823. if (priv->ledstate[which_led] != BRI_LED_ON)
  824. DO_LED(xpd, which_led, BRI_LED_ON);
  825. }
  826. }
  827. static void handle_bri_timers(xpd_t *xpd)
  828. {
  829. struct BRI_priv_data *priv;
  830. BUG_ON(!xpd);
  831. priv = xpd->priv;
  832. BUG_ON(!priv);
  833. if (IS_NT(xpd)) {
  834. if (priv->t1 > HFC_TIMER_OFF) {
  835. if (--priv->t1 == 0) {
  836. set_bri_timer(xpd, "T1", &priv->t1,
  837. HFC_TIMER_OFF);
  838. if (!nt_keepalive) {
  839. /* G2 */
  840. if (priv->state_register.bits.v_su_sta == ST_NT_ACTIVATING) {
  841. XPD_DBG(SIGNAL, xpd,
  842. "T1 Expired. "
  843. "Deactivate NT\n");
  844. clear_bit(HFC_L1_ACTIVATING,
  845. &priv->l1_flags);
  846. /* Deactivate NT */
  847. nt_activation(xpd, 0);
  848. } else
  849. XPD_DBG(SIGNAL, xpd,
  850. "T1 Expired. "
  851. "(state %d, ignored)\n",
  852. priv->state_register.
  853. bits.v_su_sta);
  854. }
  855. }
  856. }
  857. } else {
  858. if (priv->t3 > HFC_TIMER_OFF) {
  859. /* timer expired ? */
  860. if (--priv->t3 == 0) {
  861. XPD_DBG(SIGNAL, xpd,
  862. "T3 expired. Deactivate TE\n");
  863. set_bri_timer(xpd, "T3", &priv->t3,
  864. HFC_TIMER_OFF);
  865. clear_bit(HFC_L1_ACTIVATING, &priv->l1_flags);
  866. te_activation(xpd, 0); /* Deactivate TE */
  867. }
  868. }
  869. }
  870. }
  871. /* Poll the register ST/Up-State-machine Register, to see if the cable
  872. * if a cable is connected to the port.
  873. */
  874. static int BRI_card_tick(xbus_t *xbus, xpd_t *xpd)
  875. {
  876. struct BRI_priv_data *priv;
  877. BUG_ON(!xpd);
  878. priv = xpd->priv;
  879. BUG_ON(!priv);
  880. if (!priv->initialized || !xbus->self_ticking)
  881. return 0;
  882. if (poll_interval != 0 && (priv->tick_counter % poll_interval) == 0) {
  883. // XPD_DBG(GENERAL, xpd, "%d\n", priv->tick_counter);
  884. priv->poll_counter++;
  885. xpp_register_request(xbus, xpd,
  886. BRI_PORT(xpd), /* portno */
  887. 0, /* writing */
  888. A_SU_RD_STA, /* regnum */
  889. 0, /* do_subreg */
  890. 0, /* subreg */
  891. 0, /* data_low */
  892. 0, /* do_datah */
  893. 0, /* data_high */
  894. 0 /* should_reply */
  895. );
  896. if (IS_NT(xpd) && nt_keepalive
  897. && !test_bit(HFC_L1_ACTIVATED, &priv->l1_flags)
  898. && !test_bit(HFC_L1_ACTIVATING, &priv->l1_flags)) {
  899. XPD_DBG(SIGNAL, xpd, "Kick NT D-Channel\n");
  900. nt_activation(xpd, 1);
  901. }
  902. }
  903. /* Detect D-Channel disconnect heuristic */
  904. priv->dchan_notx_ticks++;
  905. priv->dchan_norx_ticks++;
  906. priv->dchan_alive_ticks++;
  907. if (priv->dchan_alive
  908. && (priv->dchan_notx_ticks > DCHAN_LOST
  909. || priv->dchan_norx_ticks > DCHAN_LOST)) {
  910. /*
  911. * No tx_dchan() or rx_dchan() for many ticks
  912. * This D-Channel is probabelly dead.
  913. */
  914. dchan_state(xpd, 0);
  915. } else if (priv->dchan_rx_counter > 1 && priv->dchan_tx_counter > 1) {
  916. if (!priv->dchan_alive)
  917. dchan_state(xpd, 1);
  918. }
  919. /* Detect Layer1 disconnect */
  920. if (priv->reg30_good && priv->reg30_ticks > poll_interval * REG30_LOST) {
  921. /* No reply for 1/2 a second */
  922. XPD_ERR(xpd, "Lost state tracking for %d ticks\n",
  923. priv->reg30_ticks);
  924. priv->reg30_good = 0;
  925. layer1_state(xpd, 0);
  926. }
  927. handle_leds(xbus, xpd);
  928. handle_bri_timers(xpd);
  929. tx_dchan(xpd);
  930. priv->tick_counter++;
  931. priv->reg30_ticks++;
  932. return 0;
  933. }
  934. static int BRI_card_ioctl(xpd_t *xpd, int pos, unsigned int cmd,
  935. unsigned long arg)
  936. {
  937. BUG_ON(!xpd);
  938. if (!XBUS_IS(xpd->xbus, READY))
  939. return -ENODEV;
  940. switch (cmd) {
  941. case DAHDI_TONEDETECT:
  942. /*
  943. * Asterisk call all span types with this (FXS specific)
  944. * call. Silently ignore it.
  945. */
  946. LINE_DBG(SIGNAL, xpd, pos, "BRI: Starting a call\n");
  947. return -ENOTTY;
  948. default:
  949. report_bad_ioctl(THIS_MODULE->name, xpd, pos, cmd);
  950. return -ENOTTY;
  951. }
  952. return 0;
  953. }
  954. static int BRI_card_open(xpd_t *xpd, lineno_t pos)
  955. {
  956. struct BRI_priv_data *priv;
  957. BUG_ON(!xpd);
  958. priv = xpd->priv;
  959. if (pos == 2) {
  960. priv->dchan_is_open = 1;
  961. LINE_DBG(SIGNAL, xpd, pos, "OFFHOOK the whole span\n");
  962. BIT_SET(PHONEDEV(xpd).offhook_state, 0);
  963. BIT_SET(PHONEDEV(xpd).offhook_state, 1);
  964. BIT_SET(PHONEDEV(xpd).offhook_state, 2);
  965. CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
  966. }
  967. return 0;
  968. }
  969. static int BRI_card_close(xpd_t *xpd, lineno_t pos)
  970. {
  971. struct BRI_priv_data *priv;
  972. priv = xpd->priv;
  973. /* Clear D-Channel pending data */
  974. if (pos == 2) {
  975. LINE_DBG(SIGNAL, xpd, pos, "ONHOOK the whole span\n");
  976. BIT_CLR(PHONEDEV(xpd).offhook_state, 0);
  977. BIT_CLR(PHONEDEV(xpd).offhook_state, 1);
  978. BIT_CLR(PHONEDEV(xpd).offhook_state, 2);
  979. CALL_PHONE_METHOD(card_pcm_recompute, xpd, 0);
  980. priv->dchan_is_open = 0;
  981. } else if (!priv->dchan_is_open)
  982. mark_offhook(xpd, pos, 0); /* e.g: patgen/pattest */
  983. return 0;
  984. }
  985. /*
  986. * Called only for 'span' keyword in /etc/dahdi/system.conf
  987. */
  988. static int bri_spanconfig(struct file *file, struct dahdi_span *span,
  989. struct dahdi_lineconfig *lc)
  990. {
  991. struct phonedev *phonedev = container_of(span, struct phonedev, span);
  992. xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
  993. const char *framingstr = "";
  994. const char *codingstr = "";
  995. const char *crcstr = "";
  996. /* framing first */
  997. if (lc->lineconfig & DAHDI_CONFIG_B8ZS)
  998. framingstr = "B8ZS";
  999. else if (lc->lineconfig & DAHDI_CONFIG_AMI)
  1000. framingstr = "AMI";
  1001. else if (lc->lineconfig & DAHDI_CONFIG_HDB3)
  1002. framingstr = "HDB3";
  1003. /* then coding */
  1004. if (lc->lineconfig & DAHDI_CONFIG_ESF)
  1005. codingstr = "ESF";
  1006. else if (lc->lineconfig & DAHDI_CONFIG_D4)
  1007. codingstr = "D4";
  1008. else if (lc->lineconfig & DAHDI_CONFIG_CCS)
  1009. codingstr = "CCS";
  1010. /* E1's can enable CRC checking */
  1011. if (lc->lineconfig & DAHDI_CONFIG_CRC4)
  1012. crcstr = "CRC4";
  1013. XPD_DBG(GENERAL, xpd,
  1014. "[%s]: span=%d (%s) lbo=%d lineconfig=%s/%s/%s (0x%X) sync=%d\n",
  1015. IS_NT(xpd) ? "NT" : "TE", lc->span, lc->name, lc->lbo,
  1016. framingstr, codingstr, crcstr, lc->lineconfig, lc->sync);
  1017. PHONEDEV(xpd).timing_priority = lc->sync;
  1018. elect_syncer("BRI-spanconfig");
  1019. /*
  1020. * FIXME: validate
  1021. */
  1022. span->lineconfig = lc->lineconfig;
  1023. return 0;
  1024. }
  1025. /*
  1026. * Set signalling type (if appropriate)
  1027. * Called from dahdi with spinlock held on chan. Must not call back
  1028. * dahdi functions.
  1029. */
  1030. static int bri_chanconfig(struct file *file, struct dahdi_chan *chan,
  1031. int sigtype)
  1032. {
  1033. DBG(GENERAL, "channel %d (%s) -> %s\n", chan->channo, chan->name,
  1034. sig2str(sigtype));
  1035. // FIXME: sanity checks:
  1036. // - should be supported (within the sigcap)
  1037. // - should not replace fxs <->fxo ??? (covered by previous?)
  1038. return 0;
  1039. }
  1040. /*
  1041. * Called only for 'span' keyword in /etc/dahdi/system.conf
  1042. */
  1043. static int bri_startup(struct file *file, struct dahdi_span *span)
  1044. {
  1045. struct phonedev *phonedev = container_of(span, struct phonedev, span);
  1046. xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
  1047. struct BRI_priv_data *priv;
  1048. struct dahdi_chan *dchan;
  1049. BUG_ON(!xpd);
  1050. priv = xpd->priv;
  1051. BUG_ON(!priv);
  1052. if (!XBUS_IS(xpd->xbus, READY)) {
  1053. XPD_DBG(GENERAL, xpd,
  1054. "Startup called by dahdi. No Hardware. Ignored\n");
  1055. return -ENODEV;
  1056. }
  1057. XPD_DBG(GENERAL, xpd, "STARTUP\n");
  1058. // Turn on all channels
  1059. CALL_PHONE_METHOD(card_state, xpd, 1);
  1060. if (SPAN_REGISTERED(xpd)) {
  1061. dchan = XPD_CHAN(xpd, 2);
  1062. span->flags |= DAHDI_FLAG_RUNNING;
  1063. /*
  1064. * Dahdi (wrongly) assume that D-Channel need HDLC decoding
  1065. * and during dahdi registration override our flags.
  1066. *
  1067. * Don't Get Mad, Get Even: Now we override dahdi :-)
  1068. */
  1069. clear_bit(DAHDI_FLAGBIT_HDLC, &dchan->flags);
  1070. }
  1071. return 0;
  1072. }
  1073. /*
  1074. * Called only for 'span' keyword in /etc/dahdi/system.conf
  1075. */
  1076. static int bri_shutdown(struct dahdi_span *span)
  1077. {
  1078. struct phonedev *phonedev = container_of(span, struct phonedev, span);
  1079. xpd_t *xpd = container_of(phonedev, struct xpd, phonedev);
  1080. struct BRI_priv_data *priv;
  1081. BUG_ON(!xpd);
  1082. priv = xpd->priv;
  1083. BUG_ON(!priv);
  1084. if (!XBUS_IS(xpd->xbus, READY)) {
  1085. XPD_DBG(GENERAL, xpd,
  1086. "Shutdown called by dahdi. No Hardware. Ignored\n");
  1087. return -ENODEV;
  1088. }
  1089. XPD_DBG(GENERAL, xpd, "SHUTDOWN\n");
  1090. // Turn off all channels
  1091. CALL_PHONE_METHOD(card_state, xpd, 0);
  1092. return 0;
  1093. }
  1094. static void BRI_card_pcm_recompute(xpd_t *xpd, xpp_line_t dont_care)
  1095. {
  1096. int i;
  1097. int line_count;
  1098. xpp_line_t pcm_mask;
  1099. uint pcm_len;
  1100. xpd_t *main_xpd;
  1101. unsigned long flags;
  1102. BUG_ON(!xpd);
  1103. main_xpd = xpd_byaddr(xpd->xbus, xpd->addr.unit, 0);
  1104. if (!main_xpd) {
  1105. XPD_DBG(DEVICES, xpd,
  1106. "Unit 0 is already gone. Ignore request\n");
  1107. return;
  1108. }
  1109. /*
  1110. * We calculate all subunits, so use the main lock
  1111. * as a mutex for the whole operation.
  1112. */
  1113. spin_lock_irqsave(&PHONEDEV(main_xpd).lock_recompute_pcm, flags);
  1114. line_count = 0;
  1115. pcm_mask = 0;
  1116. for (i = 0; i < MAX_SUBUNIT; i++) {
  1117. xpd_t *sub_xpd = xpd_byaddr(xpd->xbus, main_xpd->addr.unit, i);
  1118. if (sub_xpd) {
  1119. xpp_line_t lines =
  1120. PHONEDEV(sub_xpd).
  1121. offhook_state & ~(PHONEDEV(sub_xpd).
  1122. digital_signalling);
  1123. if (lines) {
  1124. pcm_mask |= PCM_SHIFT(lines, i);
  1125. line_count += 2;
  1126. }
  1127. /* subunits have fake pcm_len and wanted_pcm_mask */
  1128. if (i > 0)
  1129. update_wanted_pcm_mask(sub_xpd, lines, 0);
  1130. }
  1131. }
  1132. /*
  1133. * FIXME: Workaround a bug in sync code of the Astribank.
  1134. * Send dummy PCM for sync.
  1135. */
  1136. if (main_xpd->addr.unit == 0 && line_count == 0) {
  1137. pcm_mask = BIT(0);
  1138. line_count = 1;
  1139. }
  1140. /*
  1141. * The main unit account for all subunits (pcm_len and wanted_pcm_mask).
  1142. */
  1143. pcm_len = (line_count)
  1144. ? RPACKET_HEADERSIZE + sizeof(xpp_line_t) +
  1145. line_count * DAHDI_CHUNKSIZE : 0L;
  1146. update_wanted_pcm_mask(main_xpd, pcm_mask, pcm_len);
  1147. spin_unlock_irqrestore(&PHONEDEV(main_xpd).lock_recompute_pcm, flags);
  1148. }
  1149. static void BRI_card_pcm_fromspan(xpd_t *xpd, xpacket_t *pack)
  1150. {
  1151. __u8 *pcm;
  1152. unsigned long flags;
  1153. int i;
  1154. int subunit;
  1155. xpp_line_t pcm_mask = 0;
  1156. xpp_line_t wanted_lines;
  1157. BUG_ON(!xpd);
  1158. BUG_ON(!pack);
  1159. pcm = RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, pcm);
  1160. for (subunit = 0; subunit < MAX_SUBUNIT; subunit++) {
  1161. xpd_t *tmp_xpd;
  1162. tmp_xpd = xpd_byaddr(xpd->xbus, xpd->addr.unit, subunit);
  1163. if (!tmp_xpd || !tmp_xpd->card_present)
  1164. continue;
  1165. spin_lock_irqsave(&tmp_xpd->lock, flags);
  1166. wanted_lines = PHONEDEV(tmp_xpd).wanted_pcm_mask;
  1167. for_each_line(tmp_xpd, i) {
  1168. struct dahdi_chan *chan = XPD_CHAN(tmp_xpd, i);
  1169. if (IS_SET(wanted_lines, i)) {
  1170. if (SPAN_REGISTERED(tmp_xpd)) {
  1171. #ifdef DEBUG_PCMTX
  1172. int channo = chan->channo;
  1173. if (pcmtx >= 0 && pcmtx_chan == channo)
  1174. memset((u_char *)pcm, pcmtx,
  1175. DAHDI_CHUNKSIZE);
  1176. else
  1177. #endif
  1178. memcpy((u_char *)pcm,
  1179. chan->writechunk,
  1180. DAHDI_CHUNKSIZE);
  1181. } else
  1182. memset((u_char *)pcm, 0x7F,
  1183. DAHDI_CHUNKSIZE);
  1184. pcm += DAHDI_CHUNKSIZE;
  1185. }
  1186. }
  1187. pcm_mask |= PCM_SHIFT(wanted_lines, subunit);
  1188. XPD_COUNTER(tmp_xpd, PCM_WRITE)++;
  1189. spin_unlock_irqrestore(&tmp_xpd->lock, flags);
  1190. }
  1191. RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, lines) = pcm_mask;
  1192. }
  1193. static void BRI_card_pcm_tospan(xpd_t *xpd, xpacket_t *pack)
  1194. {
  1195. __u8 *pcm;
  1196. xpp_line_t pcm_mask;
  1197. unsigned long flags;
  1198. int subunit;
  1199. int i;
  1200. /*
  1201. * Subunit 0 handle all other subunits
  1202. */
  1203. if (xpd->addr.subunit != 0)
  1204. return;
  1205. if (!SPAN_REGISTERED(xpd))
  1206. return;
  1207. pcm = RPACKET_FIELD(pack, GLOBAL, PCM_READ, pcm);
  1208. pcm_mask = RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, lines);
  1209. for (subunit = 0; subunit < MAX_SUBUNIT;
  1210. subunit++, pcm_mask >>= SUBUNIT_PCM_SHIFT) {
  1211. xpd_t *tmp_xpd;
  1212. if (!pcm_mask)
  1213. break; /* optimize */
  1214. tmp_xpd = xpd_byaddr(xpd->xbus, xpd->addr.unit, subunit);
  1215. if (!tmp_xpd || !tmp_xpd->card_present
  1216. || !SPAN_REGISTERED(tmp_xpd))
  1217. continue;
  1218. spin_lock_irqsave(&tmp_xpd->lock, flags);
  1219. for (i = 0; i < 2; i++) {
  1220. xpp_line_t tmp_mask = pcm_mask & (BIT(0) | BIT(1));
  1221. volatile u_char *r;
  1222. if (IS_SET(tmp_mask, i)) {
  1223. r = XPD_CHAN(tmp_xpd, i)->readchunk;
  1224. #if 0
  1225. /* DEBUG */
  1226. memset((u_char *)r, 0x5A, DAHDI_CHUNKSIZE);
  1227. #endif
  1228. memcpy((u_char *)r, pcm, DAHDI_CHUNKSIZE);
  1229. pcm += DAHDI_CHUNKSIZE;
  1230. }
  1231. }
  1232. XPD_COUNTER(tmp_xpd, PCM_READ)++;
  1233. spin_unlock_irqrestore(&tmp_xpd->lock, flags);
  1234. }
  1235. }
  1236. static int BRI_timing_priority(xpd_t *xpd)
  1237. {
  1238. struct BRI_priv_data *priv;
  1239. priv = xpd->priv;
  1240. BUG_ON(!priv);
  1241. if (priv->layer1_up)
  1242. return PHONEDEV(xpd).timing_priority;
  1243. XPD_DBG(SYNC, xpd, "No timing priority (no layer1)\n");
  1244. return -ENOENT;
  1245. }
  1246. static int BRI_echocancel_timeslot(xpd_t *xpd, int pos)
  1247. {
  1248. return xpd->addr.subunit * 4 + pos;
  1249. }
  1250. static int BRI_echocancel_setmask(xpd_t *xpd, xpp_line_t ec_mask)
  1251. {
  1252. struct BRI_priv_data *priv;
  1253. int i;
  1254. BUG_ON(!xpd);
  1255. priv = xpd->priv;
  1256. BUG_ON(!priv);
  1257. XPD_DBG(GENERAL, xpd, "0x%8X\n", ec_mask);
  1258. if (!ECHOOPS(xpd->xbus)) {
  1259. XPD_DBG(GENERAL, xpd,
  1260. "No echo canceller in XBUS: Doing nothing.\n");
  1261. return -EINVAL;
  1262. }
  1263. for (i = 0; i < PHONEDEV(xpd).channels - 1; i++) {
  1264. int on = BIT(i) & ec_mask;
  1265. CALL_EC_METHOD(ec_set, xpd->xbus, xpd, i, on);
  1266. }
  1267. CALL_EC_METHOD(ec_update, xpd->xbus, xpd->xbus);
  1268. return 0;
  1269. }
  1270. /*---------------- BRI: HOST COMMANDS -------------------------------------*/
  1271. static /* 0x33 */ HOSTCMD(BRI, SET_LED, enum bri_led_names which_led,
  1272. enum led_state to_led_state)
  1273. {
  1274. int ret = 0;
  1275. xframe_t *xframe;
  1276. xpacket_t *pack;
  1277. struct bri_leds *bri_leds;
  1278. struct BRI_priv_data *priv;
  1279. BUG_ON(!xbus);
  1280. priv = xpd->priv;
  1281. BUG_ON(!priv);
  1282. XPD_DBG(LEDS, xpd, "%s -> %d\n", (which_led) ? "RED" : "GREEN",
  1283. to_led_state);
  1284. XFRAME_NEW_CMD(xframe, pack, xbus, BRI, SET_LED, xpd->xbus_idx);
  1285. bri_leds = &RPACKET_FIELD(pack, BRI, SET_LED, bri_leds);
  1286. bri_leds->state = to_led_state;
  1287. bri_leds->led_sel = which_led;
  1288. XPACKET_LEN(pack) = RPACKET_SIZE(BRI, SET_LED);
  1289. ret = send_cmd_frame(xbus, xframe);
  1290. priv->ledstate[which_led] = to_led_state;
  1291. return ret;
  1292. }
  1293. static int write_state_register(xpd_t *xpd, __u8 value)
  1294. {
  1295. int ret;
  1296. XPD_DBG(REGS, xpd, "value = 0x%02X\n", value);
  1297. ret = xpp_register_request(xpd->xbus, xpd,
  1298. BRI_PORT(xpd), /* portno */
  1299. 1, /* writing */
  1300. A_SU_WR_STA, /* regnum */
  1301. 0, /* do_subreg */
  1302. 0, /* subreg */
  1303. value, /* data_low */
  1304. 0, /* do_datah */
  1305. 0, /* data_high */
  1306. 0 /* should_reply */
  1307. );
  1308. return ret;
  1309. }
  1310. /*---------------- BRI: Astribank Reply Handlers --------------------------*/
  1311. static void su_new_state(xpd_t *xpd, __u8 reg_x30)
  1312. {
  1313. struct BRI_priv_data *priv;
  1314. su_rd_sta_t new_state;
  1315. BUG_ON(!xpd);
  1316. priv = xpd->priv;
  1317. BUG_ON(!priv);
  1318. if (!priv->initialized) {
  1319. XPD_ERR(xpd, "%s called on uninitialized AB\n", __func__);
  1320. return;
  1321. }
  1322. new_state.reg = reg_x30;
  1323. if (new_state.bits.v_su_t2_exp)
  1324. XPD_NOTICE(xpd, "T2 Expired\n");
  1325. priv->reg30_ticks = 0;
  1326. priv->reg30_good = 1;
  1327. if (priv->state_register.bits.v_su_sta == new_state.bits.v_su_sta)
  1328. return; /* same same */
  1329. XPD_DBG(SIGNAL, xpd, "%02X ---> %02X (info0=%d) (%s%i)\n",
  1330. priv->state_register.reg, reg_x30, new_state.bits.v_su_info0,
  1331. IS_NT(xpd) ? "G" : "F", new_state.bits.v_su_sta);
  1332. if (!IS_NT(xpd)) {
  1333. switch (new_state.bits.v_su_sta) {
  1334. case ST_TE_DEACTIVATED: /* F3 */
  1335. XPD_DBG(SIGNAL, xpd, "State ST_TE_DEACTIVATED (F3)\n");
  1336. clear_bit(HFC_L1_ACTIVATED, &priv->l1_flags);
  1337. layer1_state(xpd, 0);
  1338. break;
  1339. case ST_TE_SIGWAIT: /* F4 */
  1340. XPD_DBG(SIGNAL, xpd, "State ST_TE_SIGWAIT (F4)\n");
  1341. layer1_state(xpd, 0);
  1342. break;
  1343. case ST_TE_IDENT: /* F5 */
  1344. XPD_DBG(SIGNAL, xpd, "State ST_TE_IDENT (F5)\n");
  1345. layer1_state(xpd, 0);
  1346. break;
  1347. case ST_TE_SYNCED: /* F6 */
  1348. XPD_DBG(SIGNAL, xpd, "State ST_TE_SYNCED (F6)\n");
  1349. layer1_state(xpd, 0);
  1350. break;
  1351. case ST_TE_ACTIVATED: /* F7 */
  1352. XPD_DBG(SIGNAL, xpd, "State ST_TE_ACTIVATED (F7)\n");
  1353. set_bri_timer(xpd, "T3", &priv->t3, HFC_TIMER_OFF);
  1354. clear_bit(HFC_L1_ACTIVATING, &priv->l1_flags);
  1355. set_bit(HFC_L1_ACTIVATED, &priv->l1_flags);
  1356. layer1_state(xpd, 1);
  1357. update_xpd_status(xpd, DAHDI_ALARM_NONE);
  1358. break;
  1359. case ST_TE_LOST_FRAMING: /* F8 */
  1360. XPD_DBG(SIGNAL, xpd, "State ST_TE_LOST_FRAMING (F8)\n");
  1361. layer1_state(xpd, 0);
  1362. break;
  1363. default:
  1364. XPD_NOTICE(xpd, "Bad TE state: %d\n",
  1365. new_state.bits.v_su_sta);
  1366. break;
  1367. }
  1368. } else {
  1369. switch (new_state.bits.v_su_sta) {
  1370. case ST_NT_DEACTIVATED: /* G1 */
  1371. XPD_DBG(SIGNAL, xpd, "State ST_NT_DEACTIVATED (G1)\n");
  1372. clear_bit(HFC_L1_ACTIVATED, &priv->l1_flags);
  1373. set_bri_timer(xpd, "T1", &priv->t1, HFC_TIMER_OFF);
  1374. layer1_state(xpd, 0);
  1375. break;
  1376. case ST_NT_ACTIVATING: /* G2 */
  1377. XPD_DBG(SIGNAL, xpd, "State ST_NT_ACTIVATING (G2)\n");
  1378. layer1_state(xpd, 0);
  1379. if (!test_bit(HFC_L1_ACTIVATED, &priv->l1_flags))
  1380. nt_activation(xpd, 1);
  1381. break;
  1382. case ST_NT_ACTIVATED: /* G3 */
  1383. XPD_DBG(SIGNAL, xpd, "State ST_NT_ACTIVATED (G3)\n");
  1384. clear_bit(HFC_L1_ACTIVATING, &priv->l1_flags);
  1385. set_bit(HFC_L1_ACTIVATED, &priv->l1_flags);
  1386. set_bri_timer(xpd, "T1", &priv->t1, HFC_TIMER_OFF);
  1387. layer1_state(xpd, 1);
  1388. update_xpd_status(xpd, DAHDI_ALARM_NONE);
  1389. break;
  1390. case ST_NT_DEACTIVTING: /* G4 */
  1391. XPD_DBG(SIGNAL, xpd, "State ST_NT_DEACTIVTING (G4)\n");
  1392. set_bri_timer(xpd, "T1", &priv->t1, HFC_TIMER_OFF);
  1393. layer1_state(xpd, 0);
  1394. break;
  1395. default:
  1396. XPD_NOTICE(xpd, "Bad NT state: %d\n",
  1397. new_state.bits.v_su_sta);
  1398. break;
  1399. }
  1400. }
  1401. priv->state_register.reg = new_state.reg;
  1402. }
  1403. static int BRI_card_register_reply(xbus_t *xbus, xpd_t *xpd, reg_cmd_t *info)
  1404. {
  1405. unsigned long flags;
  1406. struct BRI_priv_data *priv;
  1407. struct xpd_addr addr;
  1408. xpd_t *orig_xpd;
  1409. int ret;
  1410. /* Map UNIT + PORTNUM to XPD */
  1411. orig_xpd = xpd;
  1412. addr.unit = orig_xpd->addr.unit;
  1413. addr.subunit = info->portnum;
  1414. xpd = xpd_byaddr(xbus, addr.unit, addr.subunit);
  1415. if (!xpd) {
  1416. static int rate_limit;
  1417. if ((rate_limit++ % 1003) < 5)
  1418. notify_bad_xpd(__func__, xbus, addr, orig_xpd->xpdname);
  1419. return -EPROTO;
  1420. }
  1421. spin_lock_irqsave(&xpd->lock, flags);
  1422. priv = xpd->priv;
  1423. BUG_ON(!priv);
  1424. if (REG_FIELD(info, do_subreg)) {
  1425. XPD_DBG(REGS, xpd, "RI %02X %02X %02X\n",
  1426. REG_FIELD(info, regnum), REG_FIELD(info, subreg),
  1427. REG_FIELD(info, data_low));
  1428. } else {
  1429. if (REG_FIELD(info, regnum) != A_SU_RD_STA)
  1430. XPD_DBG(REGS, xpd, "RD %02X %02X\n",
  1431. REG_FIELD(info, regnum), REG_FIELD(info,
  1432. data_low));
  1433. else
  1434. XPD_DBG(REGS, xpd, "Got SU_RD_STA=%02X\n",
  1435. REG_FIELD(info, data_low));
  1436. }
  1437. if (info->is_multibyte) {
  1438. XPD_DBG(REGS, xpd, "Got Multibyte: %d bytes, eoframe: %d\n",
  1439. info->bytes, info->eoframe);
  1440. ret = rx_dchan(xpd, info);
  1441. if (ret < 0) {
  1442. priv->dchan_rx_drops++;
  1443. if (atomic_read(&PHONEDEV(xpd).open_counter) > 0)
  1444. XPD_NOTICE(xpd, "Multibyte Drop: errno=%d\n",
  1445. ret);
  1446. }
  1447. goto end;
  1448. }
  1449. if (REG_FIELD(info, regnum) == A_SU_RD_STA)
  1450. su_new_state(xpd, REG_FIELD(info, data_low));
  1451. /* Update /proc info only if reply relate to the last slic read request */
  1452. if (REG_FIELD(&xpd->requested_reply, regnum) ==
  1453. REG_FIELD(info, regnum)
  1454. && REG_FIELD(&xpd->requested_reply, do_subreg) ==
  1455. REG_FIELD(info, do_subreg)
  1456. && REG_FIELD(&xpd->requested_reply, subreg) ==
  1457. REG_FIELD(info, subreg)) {
  1458. xpd->last_reply = *info;
  1459. }
  1460. end:
  1461. spin_unlock_irqrestore(&xpd->lock, flags);
  1462. return 0;
  1463. }
  1464. static int BRI_card_state(xpd_t *xpd, bool on)
  1465. {
  1466. struct BRI_priv_data *priv;
  1467. BUG_ON(!xpd);
  1468. priv = xpd->priv;
  1469. XPD_DBG(GENERAL, xpd, "%s\n", (on) ? "ON" : "OFF");
  1470. if (on) {
  1471. if (!test_bit(HFC_L1_ACTIVATED, &priv->l1_flags)) {
  1472. if (!IS_NT(xpd))
  1473. te_activation(xpd, 1);
  1474. else
  1475. nt_activation(xpd, 1);
  1476. }
  1477. } else if (IS_NT(xpd))
  1478. nt_activation(xpd, 0);
  1479. return 0;
  1480. }
  1481. static const struct xops bri_xops = {
  1482. .card_new = BRI_card_new,
  1483. .card_init = BRI_card_init,
  1484. .card_remove = BRI_card_remove,
  1485. .card_tick = BRI_card_tick,
  1486. .card_register_reply = BRI_card_register_reply,
  1487. };
  1488. static const struct phoneops bri_phoneops = {
  1489. .card_dahdi_preregistration = BRI_card_dahdi_preregistration,
  1490. .card_dahdi_postregistration = BRI_card_dahdi_postregistration,
  1491. .card_hooksig = BRI_card_hooksig,
  1492. .card_pcm_recompute = BRI_card_pcm_recompute,
  1493. .card_pcm_fromspan = BRI_card_pcm_fromspan,
  1494. .card_pcm_tospan = BRI_card_pcm_tospan,
  1495. .card_timing_priority = BRI_timing_priority,
  1496. .echocancel_timeslot = BRI_echocancel_timeslot,
  1497. .echocancel_setmask = BRI_echocancel_setmask,
  1498. .card_ioctl = BRI_card_ioctl,
  1499. .card_open = BRI_card_open,
  1500. .card_close = BRI_card_close,
  1501. .card_state = BRI_card_state,
  1502. };
  1503. static xproto_table_t PROTO_TABLE(BRI) = {
  1504. .owner = THIS_MODULE,
  1505. .entries = {
  1506. /* Table Card Opcode */
  1507. },
  1508. .name = "BRI", /* protocol name */
  1509. .ports_per_subunit = 1,
  1510. .type = XPD_TYPE_BRI,
  1511. .xops = &bri_xops,
  1512. .phoneops = &bri_phoneops,
  1513. .packet_is_valid = bri_packet_is_valid,
  1514. .packet_dump = bri_packet_dump,
  1515. };
  1516. static bool bri_packet_is_valid(xpacket_t *pack)
  1517. {
  1518. const xproto_entry_t *xe = NULL;
  1519. // DBG(GENERAL, "\n");
  1520. xe = xproto_card_entry(&PROTO_TABLE(BRI), XPACKET_OP(pack));
  1521. return xe != NULL;
  1522. }
  1523. static void bri_packet_dump(const char *msg, xpacket_t *pack)
  1524. {
  1525. DBG(GENERAL, "%s\n", msg);
  1526. }
  1527. /*------------------------- REGISTER Handling --------------------------*/
  1528. #ifdef CONFIG_PROC_FS
  1529. static int proc_bri_info_show(struct seq_file *sfile, void *not_used)
  1530. {
  1531. unsigned long flags;
  1532. xpd_t *xpd = sfile->private;
  1533. struct BRI_priv_data *priv;
  1534. DBG(PROC, "\n");
  1535. if (!xpd)
  1536. return -ENODEV;
  1537. spin_lock_irqsave(&xpd->lock, flags);
  1538. priv = xpd->priv;
  1539. BUG_ON(!priv);
  1540. seq_printf(sfile, "%05d Layer 1: ", priv->poll_counter);
  1541. if (priv->reg30_good) {
  1542. seq_printf(sfile, "%-5s ", (priv->layer1_up) ? "UP" : "DOWN");
  1543. seq_printf(sfile,
  1544. "%c%d %-15s -- fr_sync=%d t2_exp=%d info0=%d g2_g3=%d\n",
  1545. IS_NT(xpd) ? 'G' : 'F',
  1546. priv->state_register.bits.v_su_sta,
  1547. xhfc_state_name(IS_NT(xpd),
  1548. priv->state_register.bits.v_su_sta),
  1549. priv->state_register.bits.v_su_fr_sync,
  1550. priv->state_register.bits.v_su_t2_exp,
  1551. priv->state_register.bits.v_su_info0,
  1552. priv->state_register.bits.v_g2_g3);
  1553. } else {
  1554. seq_printf(sfile, "Unknown\n");
  1555. }
  1556. if (IS_NT(xpd))
  1557. seq_printf(sfile, "T1 Timer: %d\n", priv->t1);
  1558. else
  1559. seq_printf(sfile, "T3 Timer: %d\n", priv->t3);
  1560. seq_printf(sfile, "Tick Counter: %d\n", priv->tick_counter);
  1561. seq_printf(sfile, "Last Poll Reply: %d ticks ago\n",
  1562. priv->reg30_ticks);
  1563. seq_printf(sfile, "reg30_good=%d\n", priv->reg30_good);
  1564. seq_printf(sfile, "D-Channel: TX=[%5d] RX=[%5d] BAD=[%5d] ",
  1565. priv->dchan_tx_counter, priv->dchan_rx_counter,
  1566. priv->dchan_rx_drops);
  1567. if (priv->dchan_alive) {
  1568. seq_printf(sfile, "(alive %d K-ticks)\n",
  1569. priv->dchan_alive_ticks / 1000);
  1570. } else {
  1571. seq_printf(sfile, "(dead)\n");
  1572. }
  1573. seq_printf(sfile, "dchan_notx_ticks: %d\n",
  1574. priv->dchan_notx_ticks);
  1575. seq_printf(sfile, "dchan_norx_ticks: %d\n",
  1576. priv->dchan_norx_ticks);
  1577. seq_printf(sfile, "LED: %-10s = %d\n", "GREEN",
  1578. priv->ledstate[GREEN_LED]);
  1579. seq_printf(sfile, "LED: %-10s = %d\n", "RED",
  1580. priv->ledstate[RED_LED]);
  1581. seq_printf(sfile, "\nDCHAN:\n");
  1582. seq_printf(sfile, "\n");
  1583. spin_unlock_irqrestore(&xpd->lock, flags);
  1584. return 0;
  1585. }
  1586. static int proc_bri_info_open(struct inode *inode, struct file *file)
  1587. {
  1588. return single_open(file, proc_bri_info_show, PDE_DATA(inode));
  1589. }
  1590. static const struct file_operations proc_bri_info_ops = {
  1591. .owner = THIS_MODULE,
  1592. .open = proc_bri_info_open,
  1593. .read = seq_read,
  1594. .llseek = seq_lseek,
  1595. .release = single_release,
  1596. };
  1597. #endif
  1598. static int bri_xpd_probe(struct device *dev)
  1599. {
  1600. xpd_t *xpd;
  1601. xpd = dev_to_xpd(dev);
  1602. /* Is it our device? */
  1603. if (xpd->type != XPD_TYPE_BRI) {
  1604. XPD_ERR(xpd, "drop suggestion for %s (%d)\n", dev_name(dev),
  1605. xpd->type);
  1606. return -EINVAL;
  1607. }
  1608. XPD_DBG(DEVICES, xpd, "SYSFS\n");
  1609. return 0;
  1610. }
  1611. static int bri_xpd_remove(struct device *dev)
  1612. {
  1613. xpd_t *xpd;
  1614. xpd = dev_to_xpd(dev);
  1615. XPD_DBG(DEVICES, xpd, "SYSFS\n");
  1616. return 0;
  1617. }
  1618. static struct xpd_driver bri_driver = {
  1619. .type = XPD_TYPE_BRI,
  1620. .driver = {
  1621. .name = "bri",
  1622. .owner = THIS_MODULE,
  1623. .probe = bri_xpd_probe,
  1624. .remove = bri_xpd_remove}
  1625. };
  1626. static int __init card_bri_startup(void)
  1627. {
  1628. int ret;
  1629. if ((ret = xpd_driver_register(&bri_driver.driver)) < 0)
  1630. return ret;
  1631. INFO("revision %s\n", XPP_VERSION);
  1632. xproto_register(&PROTO_TABLE(BRI));
  1633. return 0;
  1634. }
  1635. static void __exit card_bri_cleanup(void)
  1636. {
  1637. DBG(GENERAL, "\n");
  1638. xproto_unregister(&PROTO_TABLE(BRI));
  1639. xpd_driver_unregister(&bri_driver.driver);
  1640. }
  1641. MODULE_DESCRIPTION("XPP BRI Card Driver");
  1642. MODULE_AUTHOR("Oron Peled <oron@actcom.co.il>");
  1643. MODULE_LICENSE("GPL");
  1644. MODULE_VERSION(XPP_VERSION);
  1645. MODULE_ALIAS_XPD(XPD_TYPE_BRI);
  1646. module_init(card_bri_startup);
  1647. module_exit(card_bri_cleanup);