card_pri.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745
  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 "xpd.h"
  29. #include "xproto.h"
  30. #include "xpp_dahdi.h"
  31. #include "card_pri.h"
  32. #include "dahdi_debug.h"
  33. #include "xpd.h"
  34. #include "xbus-core.h"
  35. static const char rcsid[] = "$Id$";
  36. static DEF_PARM(int, debug, 0, 0644, "Print DBG statements"); /* must be before dahdi_debug.h */
  37. static DEF_PARM(uint, poll_interval, 500, 0644, "Poll channel state interval in milliseconds (0 - disable)");
  38. #define PRI_LINES_BITMASK BITMASK(31)
  39. #define PRI_SIGCAP ( \
  40. DAHDI_SIG_EM | \
  41. DAHDI_SIG_CLEAR | \
  42. DAHDI_SIG_FXSLS | \
  43. DAHDI_SIG_FXSGS | \
  44. DAHDI_SIG_FXSKS | \
  45. DAHDI_SIG_HARDHDLC | \
  46. DAHDI_SIG_MTP2 | \
  47. DAHDI_SIG_FXOLS | \
  48. DAHDI_SIG_FXOGS | \
  49. DAHDI_SIG_FXOKS | \
  50. DAHDI_SIG_CAS | \
  51. DAHDI_SIG_EM_E1 | \
  52. DAHDI_SIG_DACS_RBS \
  53. )
  54. static bool is_sigtype_dchan(int sigtype)
  55. {
  56. if((sigtype & DAHDI_SIG_HDLCRAW) == DAHDI_SIG_HDLCRAW)
  57. return 1;
  58. if((sigtype & DAHDI_SIG_HDLCFCS) == DAHDI_SIG_HDLCFCS)
  59. return 1;
  60. if((sigtype & DAHDI_SIG_HARDHDLC) == DAHDI_SIG_HARDHDLC)
  61. return 1;
  62. return 0;
  63. }
  64. #define MAX_SLAVES 4 /* we have MUX of 4 clocks */
  65. #define PRI_PORT(xpd) ((xpd)->addr.subunit)
  66. #define CHAN_PER_REGS(p) (((p)->is_esf) ? 2 : 4)
  67. /*---------------- PRI Protocol Commands ----------------------------------*/
  68. static bool pri_packet_is_valid(xpacket_t *pack);
  69. static void pri_packet_dump(const char *msg, xpacket_t *pack);
  70. #ifdef OLD_PROC
  71. static int proc_pri_info_read(char *page, char **start, off_t off, int count, int *eof, void *data);
  72. static int proc_pri_info_write(struct file *file, const char __user *buffer, unsigned long count, void *data);
  73. #endif
  74. static int pri_startup(struct dahdi_span *span);
  75. static int pri_shutdown(struct dahdi_span *span);
  76. static int pri_rbsbits(struct dahdi_chan *chan, int bits);
  77. static int pri_lineconfig(xpd_t *xpd, int lineconfig);
  78. static void send_idlebits(xpd_t *xpd, bool saveold);
  79. #define PROC_REGISTER_FNAME "slics"
  80. #ifdef OLD_PROC
  81. #define PROC_PRI_INFO_FNAME "pri_info"
  82. #endif
  83. enum pri_protocol {
  84. PRI_PROTO_0 = 0,
  85. PRI_PROTO_E1 = 1,
  86. PRI_PROTO_T1 = 2,
  87. PRI_PROTO_J1 = 3
  88. };
  89. static const char *pri_protocol_name(enum pri_protocol pri_protocol)
  90. {
  91. static const char *protocol_names[] = {
  92. [PRI_PROTO_0] = "??", /* unknown */
  93. [PRI_PROTO_E1] = "E1",
  94. [PRI_PROTO_T1] = "T1",
  95. [PRI_PROTO_J1] = "J1"
  96. };
  97. return protocol_names[pri_protocol];
  98. }
  99. static int pri_num_channels(enum pri_protocol pri_protocol)
  100. {
  101. static int num_channels[] = {
  102. [PRI_PROTO_0] = 0,
  103. [PRI_PROTO_E1] = 31,
  104. [PRI_PROTO_T1] = 24,
  105. [PRI_PROTO_J1] = 0
  106. };
  107. return num_channels[pri_protocol];
  108. }
  109. static const char *type_name(enum pri_protocol pri_protocol)
  110. {
  111. static const char *names[4] = {
  112. [PRI_PROTO_0] = "PRI-Unknown",
  113. [PRI_PROTO_E1] = "E1",
  114. [PRI_PROTO_T1] = "T1",
  115. [PRI_PROTO_J1] = "J1"
  116. };
  117. return names[pri_protocol];
  118. }
  119. static int pri_linecompat(enum pri_protocol pri_protocol)
  120. {
  121. static const int linecompat[] = {
  122. [PRI_PROTO_0] = 0,
  123. [PRI_PROTO_E1] =
  124. /* coding */
  125. DAHDI_CONFIG_CCS |
  126. DAHDI_CONFIG_CRC4 |
  127. /* framing */
  128. DAHDI_CONFIG_AMI | DAHDI_CONFIG_HDB3,
  129. [PRI_PROTO_T1] =
  130. /* coding */
  131. DAHDI_CONFIG_D4 |
  132. DAHDI_CONFIG_ESF |
  133. /* framing */
  134. DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS,
  135. [PRI_PROTO_J1] = 0
  136. };
  137. DBG(GENERAL, "pri_linecompat: pri_protocol=%d\n", pri_protocol);
  138. return linecompat[pri_protocol];
  139. }
  140. #define PRI_DCHAN_IDX(priv) ((priv)->dchan_num - 1)
  141. enum pri_led_state {
  142. PRI_LED_OFF = 0x0,
  143. PRI_LED_ON = 0x1,
  144. /*
  145. * We blink by software from driver, so that
  146. * if the driver malfunction that blink would stop.
  147. */
  148. // PRI_LED_BLINK_SLOW = 0x2, /* 1/2 a second blink cycle */
  149. // PRI_LED_BLINK_FAST = 0x3 /* 1/4 a second blink cycle */
  150. };
  151. enum pri_led_selectors {
  152. BOTTOM_RED_LED = 0,
  153. BOTTOM_GREEN_LED = 1,
  154. TOP_RED_LED = 2,
  155. TOP_GREEN_LED = 3,
  156. };
  157. #define NUM_LEDS 4
  158. struct pri_leds {
  159. byte state:2; /* enum pri_led_state */
  160. byte led_sel:2; /* enum pri_led_selectors */
  161. byte reserved:4;
  162. };
  163. #define REG_CCB1_T 0x2F /* Clear Channel Register 1 */
  164. #define REG_FRS0 0x4C /* Framer Receive Status Register 0 */
  165. #define REG_FRS0_T1_FSR BIT(0) /* T1 - Frame Search Restart Flag */
  166. #define REG_FRS0_LMFA BIT(1) /* Loss of Multiframe Alignment */
  167. #define REG_FRS0_E1_NMF BIT(2) /* E1 - No Multiframe Alignment Found */
  168. #define REG_FRS0_RRA BIT(4) /* Receive Remote Alarm: T1-YELLOW-Alarm */
  169. #define REG_FRS0_LFA BIT(5) /* Loss of Frame Alignment */
  170. #define REG_FRS0_AIS BIT(6) /* Alarm Indication Signal: T1-BLUE-Alarm */
  171. #define REG_FRS0_LOS BIT(7) /* Los Of Signal: T1-RED-Alarm */
  172. #define REG_FRS1 0x4D /* Framer Receive Status Register 1 */
  173. #define REG_LIM0 0x36
  174. #define REG_LIM0_MAS BIT(0) /* Master Mode, DCO-R circuitry is frequency synchronized to the clock supplied by SYNC */
  175. #define REG_LIM0_RTRS BIT(5) /*
  176. * Receive Termination Resistance Selection:
  177. * integrated resistor to create 75 Ohm termination (100 || 300 = 75)
  178. * 0 = 100 Ohm
  179. * 1 = 75 Ohm
  180. */
  181. #define REG_LIM0_LL BIT(1) /* LL (Local Loopback) */
  182. #define REG_FMR0 0x1C
  183. #define REG_FMR0_E_RC0 BIT(4) /* Receive Code - LSB */
  184. #define REG_FMR0_E_RC1 BIT(5) /* Receive Code - MSB */
  185. #define REG_FMR0_E_XC0 BIT(6) /* Transmit Code - LSB */
  186. #define REG_FMR0_E_XC1 BIT(7) /* Transmit Code - MSB */
  187. #define REG_FMR1 0x1D
  188. #define REG_FMR1_XAIS BIT(0) /* Transmit AIS toward transmit end */
  189. #define REG_FMR1_SSD0 BIT(1)
  190. #define REG_FMR1_ECM BIT(2)
  191. #define REG_FMR1_T_CRC BIT(3) /* Enable CRC6 */
  192. #define REG_FMR1_E_XFS BIT(3) /* Transmit Framing Select */
  193. #define REG_FMR1_PMOD BIT(4) /* E1 = 0, T1/J1 = 1 */
  194. #define REG_FMR1_EDL BIT(5)
  195. #define REG_FMR1_AFR BIT(6)
  196. #define REG_FMR2 0x1E
  197. #define REG_FMR2_E_ALMF BIT(0) /* Automatic Loss of Multiframe */
  198. #define REG_FMR2_T_EXZE BIT(0) /* Excessive Zeros Detection Enable */
  199. #define REG_FMR2_E_AXRA BIT(1) /* Automatic Transmit Remote Alarm */
  200. #define REG_FMR2_T_AXRA BIT(1) /* Automatic Transmit Remote Alarm */
  201. #define REG_FMR2_E_PLB BIT(2) /* Payload Loop-Back */
  202. #define REG_FMR2_E_RFS0 BIT(6) /* Receive Framing Select - LSB */
  203. #define REG_FMR2_E_RFS1 BIT(7) /* Receive Framing Select - MSB */
  204. #define REG_FMR2_T_SSP BIT(5) /* Select Synchronization/Resynchronization Procedure */
  205. #define REG_FMR2_T_MCSP BIT(6) /* Multiple Candidates Synchronization Procedure */
  206. #define REG_FMR2_T_AFRS BIT(7) /* Automatic Force Resynchronization */
  207. #define REG_FMR3 0x31
  208. #define REG_FMR3_EXTIW BIT(0) /* Extended CRC4 to Non-CRC4 Interworking */
  209. #define REG_FMR4 0x20
  210. #define REG_FMR4_FM0 BIT(0)
  211. #define REG_FMR4_FM1 BIT(1)
  212. #define REG_FMR4_AUTO BIT(2)
  213. #define REG_FMR4_SSC0 BIT(3)
  214. #define REG_FMR4_SSC1 BIT(4)
  215. #define REG_FMR4_XRA BIT(5) /* Transmit Remote Alarm (Yellow Alarm) */
  216. #define REG_FMR4_TM BIT(6)
  217. #define REG_FMR4_AIS3 BIT(7)
  218. #define REG_XSW_E 0x20
  219. #define REG_XSW_E_XY4 BIT(0)
  220. #define REG_XSW_E_XY3 BIT(1)
  221. #define REG_XSW_E_XY2 BIT(2)
  222. #define REG_XSW_E_XY1 BIT(3)
  223. #define REG_XSW_E_XY0 BIT(4)
  224. #define REG_XSW_E_XRA BIT(5) /* Transmit Remote Alarm (Yellow Alarm) */
  225. #define REG_XSW_E_XTM BIT(6)
  226. #define REG_XSW_E_XSIS BIT(7)
  227. #define REG_XSP_E 0x21
  228. #define REG_FMR5_T 0x21
  229. #define REG_XSP_E_XSIF BIT(2) /* Transmit Spare Bit For International Use (FAS Word) */
  230. #define REG_FMR5_T_XTM BIT(2) /* Transmit Transparent Mode */
  231. #define REG_XSP_E_AXS BIT(3) /* Automatic Transmission of Submultiframe Status */
  232. #define REG_XSP_E_EBP BIT(4) /* E-Bit Polarity, Si-bit position of every outgoing CRC multiframe */
  233. #define REG_XSP_E_CASEN BIT(6) /* CAS: Channel Associated Signaling Enable */
  234. #define REG_FMR5_T_EIBR BIT(6) /* CAS: Enable Internal Bit Robbing Access */
  235. #define REG_XC0_T 0x22 /* Transmit Control 0 */
  236. #define REG_XC0_BRIF BIT(5) /* Bit Robbing Idle Function */
  237. #define REG_CMDR_E 0x02 /* Command Register */
  238. #define REG_CMDR_RRES BIT(6) /* Receiver reset */
  239. #define REG_CMDR_XRES BIT(4) /* Transmitter reset */
  240. #define REG_RC0 0x24
  241. #define REG_RC0_SJR BIT(7) /* T1 = 0, J1 = 1 */
  242. #define REG_CMR1 0x44
  243. #define REG_CMR1_DRSS (BIT(7) | BIT(6))
  244. #define REG_CMR1_RS (BIT(5) | BIT(4))
  245. #define REG_CMR1_STF BIT(2)
  246. #define REG_RS1_E 0x70 /* Receive CAS Register 1 */
  247. #define REG_RS2_E 0x71 /* Receive CAS Register 2 */
  248. #define REG_RS3_E 0x72 /* Receive CAS Register 3 */
  249. #define REG_RS4_E 0x73 /* Receive CAS Register 4 */
  250. #define REG_RS5_E 0x74 /* Receive CAS Register 5 */
  251. #define REG_RS6_E 0x75 /* Receive CAS Register 6 */
  252. #define REG_RS7_E 0x76 /* Receive CAS Register 7 */
  253. #define REG_RS8_E 0x77 /* Receive CAS Register 8 */
  254. #define REG_RS9_E 0x78 /* Receive CAS Register 9 */
  255. #define REG_RS10_E 0x79 /* Receive CAS Register 10 */
  256. #define REG_RS11_E 0x7A /* Receive CAS Register 11 */
  257. #define REG_RS12_E 0x7B /* Receive CAS Register 12 */
  258. #define REG_RS13_E 0x7C /* Receive CAS Register 13 */
  259. #define REG_RS14_E 0x7D /* Receive CAS Register 14 */
  260. #define REG_RS15_E 0x7E /* Receive CAS Register 15 */
  261. #define REG_RS16_E 0x7F /* Receive CAS Register 16 */
  262. #define REG_PC2 0x81 /* Port Configuration 2 */
  263. #define REG_PC3 0x82 /* Port Configuration 3 */
  264. #define REG_PC4 0x83 /* Port Configuration 4 */
  265. #define REG_XPM2 0x28 /* Transmit Pulse Mask 2 */
  266. #define VAL_PC_SYPR 0x00 /* Synchronous Pulse Receive (Input, low active) */
  267. #define VAL_PC_GPI 0x90 /* General purpose input */
  268. #define VAL_PC_GPOH 0x0A /* General Purpose Output, high level */
  269. #define VAL_PC_GPOL 0x0B /* General Purpose Output, low level */
  270. #define NUM_CAS_RS_E (REG_RS16_E - REG_RS2_E + 1)
  271. /* and of those, the ones used in T1: */
  272. #define NUM_CAS_RS_T (REG_RS12_E - REG_RS1_E + 1)
  273. struct PRI_priv_data {
  274. bool clock_source;
  275. #ifdef OLD_PROC
  276. struct proc_dir_entry *pri_info;
  277. #endif
  278. enum pri_protocol pri_protocol;
  279. xpp_line_t rbslines;
  280. int deflaw;
  281. unsigned int dchan_num;
  282. bool initialized;
  283. int is_cas;
  284. unsigned int chanconfig_dchan;
  285. #define NO_DCHAN (0)
  286. #define DCHAN(p) ((p)->chanconfig_dchan)
  287. #define VALID_DCHAN(p) (DCHAN(p) != NO_DCHAN)
  288. #define SET_DCHAN(p,d) do { DCHAN(p) = (d); } while(0);
  289. byte cas_rs_e[NUM_CAS_RS_E];
  290. byte cas_ts_e[NUM_CAS_RS_E];
  291. uint cas_replies;
  292. bool is_esf;
  293. bool local_loopback;
  294. uint poll_noreplies;
  295. uint layer1_replies;
  296. byte reg_frs0;
  297. byte reg_frs1;
  298. bool layer1_up;
  299. int alarms;
  300. byte dchan_tx_sample;
  301. byte dchan_rx_sample;
  302. uint dchan_tx_counter;
  303. uint dchan_rx_counter;
  304. bool dchan_alive;
  305. uint dchan_alive_ticks;
  306. enum pri_led_state ledstate[NUM_LEDS];
  307. };
  308. static xproto_table_t PROTO_TABLE(PRI);
  309. DEF_RPACKET_DATA(PRI, SET_LED, /* Set one of the LED's */
  310. struct pri_leds pri_leds;
  311. );
  312. static /* 0x33 */ DECLARE_CMD(PRI, SET_LED, enum pri_led_selectors led_sel, enum pri_led_state to_led_state);
  313. #define DO_LED(xpd, which, tostate) \
  314. CALL_PROTO(PRI, SET_LED, (xpd)->xbus, (xpd), (which), (tostate))
  315. /*---------------- PRI: Methods -------------------------------------------*/
  316. static int query_subunit(xpd_t *xpd, byte regnum)
  317. {
  318. XPD_DBG(REGS, xpd, "(%d%d): REG=0x%02X\n",
  319. xpd->addr.unit, xpd->addr.subunit,
  320. regnum);
  321. return xpp_register_request(
  322. xpd->xbus, xpd,
  323. PRI_PORT(xpd), /* portno */
  324. 0, /* writing */
  325. regnum,
  326. 0, /* do_subreg */
  327. 0, /* subreg */
  328. 0, /* data_L */
  329. 0, /* do_datah */
  330. 0, /* data_H */
  331. 0 /* should_reply */
  332. );
  333. }
  334. static int write_subunit(xpd_t *xpd, byte regnum, byte val)
  335. {
  336. XPD_DBG(REGS, xpd, "(%d%d): REG=0x%02X dataL=0x%02X\n",
  337. xpd->addr.unit, xpd->addr.subunit,
  338. regnum, val);
  339. return xpp_register_request(
  340. xpd->xbus, xpd,
  341. PRI_PORT(xpd), /* portno */
  342. 1, /* writing */
  343. regnum,
  344. 0, /* do_subreg */
  345. 0, /* subreg */
  346. val, /* data_L */
  347. 0, /* do_datah */
  348. 0, /* data_H */
  349. 0 /* should_reply */
  350. );
  351. }
  352. static int pri_write_reg(xpd_t *xpd, int regnum, byte val)
  353. {
  354. XPD_DBG(REGS, xpd, "(%d%d): REG=0x%02X dataL=0x%02X\n",
  355. xpd->addr.unit, xpd->addr.subunit,
  356. regnum, val);
  357. return xpp_register_request(
  358. xpd->xbus, xpd,
  359. 0, /* portno=0 */
  360. 1, /* writing */
  361. regnum,
  362. 0, /* do_subreg */
  363. 0, /* subreg */
  364. val, /* data_L */
  365. 0, /* do_datah */
  366. 0, /* data_H */
  367. 0 /* should_reply */
  368. );
  369. }
  370. static int cas_regbase(xpd_t *xpd)
  371. {
  372. struct PRI_priv_data *priv;
  373. priv = xpd->priv;
  374. switch (priv->pri_protocol) {
  375. case PRI_PROTO_E1:
  376. return REG_RS2_E;
  377. case PRI_PROTO_T1:
  378. /* fall-through */
  379. case PRI_PROTO_J1:
  380. return REG_RS1_E;
  381. case PRI_PROTO_0:
  382. /* fall-through */
  383. ;
  384. }
  385. BUG();
  386. return 0;
  387. }
  388. static int cas_numregs(xpd_t *xpd)
  389. {
  390. struct PRI_priv_data *priv;
  391. priv = xpd->priv;
  392. switch (priv->pri_protocol) {
  393. case PRI_PROTO_E1:
  394. return NUM_CAS_RS_E;
  395. case PRI_PROTO_T1:
  396. /* fall-through */
  397. case PRI_PROTO_J1:
  398. return NUM_CAS_RS_T;
  399. case PRI_PROTO_0:
  400. /* fall-through */
  401. ;
  402. }
  403. BUG();
  404. return 0;
  405. }
  406. static int write_cas_reg(xpd_t *xpd, int rsnum, byte val)
  407. {
  408. struct PRI_priv_data *priv;
  409. int regbase = cas_regbase(xpd);
  410. int num_cas_rs = cas_numregs(xpd);
  411. int regnum;
  412. bool is_d4 = 0;
  413. BUG_ON(!xpd);
  414. priv = xpd->priv;
  415. if ((priv->pri_protocol == PRI_PROTO_T1) && !priv->is_esf) {
  416. /* same data should be copied to RS7..12 in D4 only */
  417. is_d4 = 1;
  418. }
  419. if (rsnum < 0 || rsnum >= num_cas_rs) {
  420. XPD_ERR(xpd, "RBS(TX): rsnum=%d\n", rsnum);
  421. BUG();
  422. }
  423. regnum = regbase + rsnum;
  424. priv->cas_ts_e[rsnum] = val;
  425. XPD_DBG(SIGNAL, xpd, "RBS(TX): reg=0x%X val=0x%02X\n", regnum, val);
  426. write_subunit(xpd, regbase + rsnum, val);
  427. if (is_d4) {
  428. /* same data should be copied to RS7..12 in D4 only */
  429. regnum = REG_RS7_E + rsnum;
  430. XPD_DBG(SIGNAL, xpd, "RBS(TX): reg=0x%X val=0x%02X\n",
  431. regnum, val);
  432. write_subunit(xpd, regnum, val);
  433. }
  434. return 0;
  435. }
  436. #ifdef OLD_PROC
  437. static void pri_proc_remove(xbus_t *xbus, xpd_t *xpd)
  438. {
  439. struct PRI_priv_data *priv;
  440. BUG_ON(!xpd);
  441. priv = xpd->priv;
  442. XPD_DBG(PROC, xpd, "\n");
  443. #ifdef CONFIG_PROC_FS
  444. if(priv->pri_info) {
  445. XPD_DBG(PROC, xpd, "Removing xpd PRI_INFO file\n");
  446. remove_proc_entry(PROC_PRI_INFO_FNAME, xpd->proc_xpd_dir);
  447. }
  448. #endif
  449. }
  450. #endif
  451. #ifdef OLD_PROC
  452. static int pri_proc_create(xbus_t *xbus, xpd_t *xpd)
  453. {
  454. struct PRI_priv_data *priv;
  455. BUG_ON(!xpd);
  456. priv = xpd->priv;
  457. XPD_DBG(PROC, xpd, "\n");
  458. #ifdef CONFIG_PROC_FS
  459. XPD_DBG(PROC, xpd, "Creating PRI_INFO file\n");
  460. priv->pri_info = create_proc_entry(PROC_PRI_INFO_FNAME, 0644, xpd->proc_xpd_dir);
  461. if(!priv->pri_info) {
  462. XPD_ERR(xpd, "Failed to create proc '%s'\n", PROC_PRI_INFO_FNAME);
  463. goto err;
  464. }
  465. SET_PROC_DIRENTRY_OWNER(priv->pri_info);
  466. priv->pri_info->write_proc = proc_pri_info_write;
  467. priv->pri_info->read_proc = proc_pri_info_read;
  468. priv->pri_info->data = xpd;
  469. #endif
  470. return 0;
  471. err:
  472. pri_proc_remove(xbus, xpd);
  473. return -EINVAL;
  474. }
  475. #endif
  476. static bool valid_pri_modes(const xpd_t *xpd)
  477. {
  478. struct PRI_priv_data *priv;
  479. BUG_ON(!xpd);
  480. priv = xpd->priv;
  481. if(
  482. priv->pri_protocol != PRI_PROTO_E1 &&
  483. priv->pri_protocol != PRI_PROTO_T1 &&
  484. priv->pri_protocol != PRI_PROTO_J1)
  485. return 0;
  486. return 1;
  487. }
  488. static void PRI_card_pcm_recompute(xbus_t *xbus, xpd_t *xpd,
  489. xpp_line_t pcm_mask)
  490. {
  491. struct PRI_priv_data *priv;
  492. int i;
  493. int line_count = 0;
  494. unsigned long flags;
  495. uint pcm_len;
  496. BUG_ON(!xpd);
  497. priv = xpd->priv;
  498. spin_lock_irqsave(&xpd->lock_recompute_pcm, flags);
  499. //XPD_DBG(SIGNAL, xpd, "pcm_mask=0x%X\n", pcm_mask);
  500. /* Add/remove all the trivial cases */
  501. pcm_mask |= xpd->offhook_state;
  502. for_each_line(xpd, i)
  503. if (IS_SET(pcm_mask, i))
  504. line_count++;
  505. if(priv->is_cas) {
  506. if(priv->pri_protocol == PRI_PROTO_E1) {
  507. /* CAS: Don't send PCM to D-Channel */
  508. line_count--;
  509. pcm_mask &= ~BIT(PRI_DCHAN_IDX(priv));
  510. }
  511. }
  512. /*
  513. * FIXME: Workaround a bug in sync code of the Astribank.
  514. * Send dummy PCM for sync.
  515. */
  516. if (xpd->addr.unit == 0 && pcm_mask == 0) {
  517. pcm_mask = BIT(0);
  518. line_count = 1;
  519. }
  520. pcm_len = (line_count)
  521. ? RPACKET_HEADERSIZE + sizeof(xpp_line_t) +
  522. line_count * DAHDI_CHUNKSIZE
  523. : 0L;
  524. update_wanted_pcm_mask(xpd, pcm_mask, pcm_len);
  525. spin_unlock_irqrestore(&xpd->lock_recompute_pcm, flags);
  526. }
  527. /*
  528. * Set E1/T1/J1
  529. * May only be called on unregistered xpd's
  530. * (the span and channel description are set according to this)
  531. */
  532. static int set_pri_proto(xpd_t *xpd, enum pri_protocol set_proto)
  533. {
  534. struct PRI_priv_data *priv;
  535. int deflaw;
  536. unsigned int dchan_num;
  537. int default_lineconfig = 0;
  538. int ret;
  539. BUG_ON(!xpd);
  540. priv = xpd->priv;
  541. if(SPAN_REGISTERED(xpd)) {
  542. XPD_NOTICE(xpd, "Registered as span %d. Cannot do setup pri protocol (%s)\n",
  543. xpd->span.spanno, __FUNCTION__);
  544. return -EBUSY;
  545. }
  546. if(priv->pri_protocol != PRI_PROTO_0) {
  547. if(priv->pri_protocol == set_proto) {
  548. XPD_NOTICE(xpd, "Already in protocol %s. Ignored\n", pri_protocol_name(set_proto));
  549. return 0;
  550. } else {
  551. XPD_INFO(xpd, "Switching from %s to %s\n",
  552. pri_protocol_name(priv->pri_protocol),
  553. pri_protocol_name(set_proto));
  554. }
  555. }
  556. switch(set_proto) {
  557. case PRI_PROTO_E1:
  558. deflaw = DAHDI_LAW_ALAW;
  559. dchan_num = 16;
  560. default_lineconfig = DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4 | DAHDI_CONFIG_HDB3;
  561. break;
  562. case PRI_PROTO_T1:
  563. deflaw = DAHDI_LAW_MULAW;
  564. dchan_num = 24;
  565. default_lineconfig = DAHDI_CONFIG_ESF | DAHDI_CONFIG_B8ZS;
  566. break;
  567. case PRI_PROTO_J1:
  568. /*
  569. * Check all assumptions
  570. */
  571. deflaw = DAHDI_LAW_MULAW;
  572. dchan_num = 24;
  573. default_lineconfig = 0; /* FIXME: J1??? */
  574. XPD_NOTICE(xpd, "J1 is not supported yet\n");
  575. return -ENOSYS;
  576. default:
  577. XPD_ERR(xpd, "%s: Unknown pri protocol = %d\n",
  578. __FUNCTION__, set_proto);
  579. return -EINVAL;
  580. }
  581. priv->pri_protocol = set_proto;
  582. priv->is_cas = -1;
  583. xpd->channels = pri_num_channels(set_proto);
  584. xpd->offhook_state = BITMASK(xpd->channels);
  585. CALL_XMETHOD(card_pcm_recompute, xpd->xbus, xpd, 0);
  586. priv->deflaw = deflaw;
  587. priv->dchan_num = dchan_num;
  588. priv->local_loopback = 0;
  589. xpd->type_name = type_name(priv->pri_protocol);
  590. XPD_DBG(GENERAL, xpd, "%s, channels=%d, dchan_num=%d, deflaw=%d\n",
  591. pri_protocol_name(set_proto),
  592. xpd->channels,
  593. priv->dchan_num,
  594. priv->deflaw
  595. );
  596. /*
  597. * Must set default now, so layer1 polling (Register REG_FRS0) would
  598. * give reliable results.
  599. */
  600. ret = pri_lineconfig(xpd, default_lineconfig);
  601. if(ret) {
  602. XPD_NOTICE(xpd, "Failed setting PRI default line config\n");
  603. return ret;
  604. }
  605. return 0;
  606. }
  607. static void dahdi_update_syncsrc(xpd_t *xpd)
  608. {
  609. struct PRI_priv_data *priv;
  610. xpd_t *subxpd;
  611. int best_spanno = 0;
  612. int i;
  613. if(!SPAN_REGISTERED(xpd))
  614. return;
  615. for(i = 0; i < MAX_SLAVES; i++) {
  616. subxpd = xpd_byaddr(xpd->xbus, xpd->addr.unit, i);
  617. if(!subxpd)
  618. continue;
  619. priv = subxpd->priv;
  620. if(priv->clock_source && priv->alarms == 0) {
  621. if(best_spanno)
  622. XPD_ERR(xpd, "Duplicate XPD's with clock_source=1\n");
  623. best_spanno = subxpd->span.spanno;
  624. }
  625. }
  626. for(i = 0; i < MAX_SLAVES; i++) {
  627. subxpd = xpd_byaddr(xpd->xbus, xpd->addr.unit, i);
  628. if(!subxpd)
  629. continue;
  630. if(subxpd->span.syncsrc == best_spanno)
  631. XPD_DBG(SYNC, xpd, "Setting SyncSource to span %d\n", best_spanno);
  632. else
  633. XPD_DBG(SYNC, xpd, "Slaving to span %d\n", best_spanno);
  634. subxpd->span.syncsrc = best_spanno;
  635. }
  636. }
  637. /*
  638. * Called from:
  639. * - set_master_mode() --
  640. * As a result of dahdi_cfg
  641. * - layer1_state() --
  642. * As a result of an alarm.
  643. */
  644. static void set_clocking(xpd_t *xpd)
  645. {
  646. xbus_t *xbus;
  647. xpd_t *best_xpd = NULL;
  648. int best_subunit = -1; /* invalid */
  649. unsigned int best_subunit_prio = INT_MAX;
  650. int i;
  651. xbus = xpd->xbus;
  652. /* Find subunit with best timing priority */
  653. for(i = 0; i < MAX_SLAVES; i++) {
  654. struct PRI_priv_data *priv;
  655. xpd_t *subxpd;
  656. subxpd = xpd_byaddr(xbus, xpd->addr.unit, i);
  657. if(!subxpd)
  658. continue;
  659. priv = subxpd->priv;
  660. if(priv->alarms != 0)
  661. continue;
  662. if(subxpd->timing_priority > 0 && subxpd->timing_priority < best_subunit_prio) {
  663. best_xpd = subxpd;
  664. best_subunit = i;
  665. best_subunit_prio = subxpd->timing_priority;
  666. }
  667. }
  668. /* Now set it */
  669. if(best_xpd && ((struct PRI_priv_data *)(best_xpd->priv))->clock_source == 0) {
  670. byte reg_pc_init[] = { VAL_PC_GPI, VAL_PC_GPI, VAL_PC_GPI };
  671. for(i = 0; i < ARRAY_SIZE(reg_pc_init); i++) {
  672. byte reg_pc = reg_pc_init[i];
  673. reg_pc |= (best_subunit & (1 << i)) ? VAL_PC_GPOH : VAL_PC_GPOL;
  674. XPD_DBG(SYNC, best_xpd,
  675. "ClockSource Set: PC%d=0x%02X\n", 2+i, reg_pc);
  676. pri_write_reg(xpd, REG_PC2 + i, reg_pc);
  677. }
  678. ((struct PRI_priv_data *)(best_xpd->priv))->clock_source = 1;
  679. }
  680. /* clear old clock sources */
  681. for(i = 0; i < MAX_SLAVES; i++) {
  682. struct PRI_priv_data *priv;
  683. xpd_t *subxpd;
  684. subxpd = xpd_byaddr(xbus, xpd->addr.unit, i);
  685. if(subxpd && subxpd != best_xpd) {
  686. XPD_DBG(SYNC, subxpd, "Clearing clock source\n");
  687. priv = subxpd->priv;
  688. priv->clock_source = 0;
  689. }
  690. }
  691. dahdi_update_syncsrc(xpd);
  692. }
  693. static void set_reg_lim0(const char *msg, xpd_t *xpd)
  694. {
  695. struct PRI_priv_data *priv;
  696. bool is_master_mode;
  697. bool localloop;
  698. byte lim0 = 0;
  699. BUG_ON(!xpd);
  700. priv = xpd->priv;
  701. BUG_ON(!priv);
  702. is_master_mode = xpd->timing_priority == 0;
  703. localloop = priv->local_loopback;
  704. lim0 |= (localloop) ? REG_LIM0_LL : 0;
  705. if(is_master_mode)
  706. lim0 |= REG_LIM0_MAS;
  707. else
  708. lim0 &= ~REG_LIM0_MAS;
  709. XPD_DBG(SIGNAL, xpd, "%s(%s): %s, %s\n", __FUNCTION__, msg,
  710. (is_master_mode) ? "MASTER" : "SLAVE",
  711. (localloop) ? "LOCALLOOP" : "NO_LOCALLOOP");
  712. write_subunit(xpd, REG_LIM0 , lim0);
  713. }
  714. /*
  715. * Normally set by the timing parameter in /etc/dahdi/system.conf
  716. * If this is called by dahdi_cfg, than it's too late to change
  717. * dahdi sync priority (we are already registered)
  718. *
  719. * Also called from set_localloop()
  720. */
  721. static int set_master_mode(const char *msg, xpd_t *xpd)
  722. {
  723. struct PRI_priv_data *priv;
  724. BUG_ON(!xpd);
  725. XPD_DBG(SIGNAL, xpd, "\n");
  726. priv = xpd->priv;
  727. set_reg_lim0(__FUNCTION__, xpd);
  728. set_clocking(xpd);
  729. return 0;
  730. }
  731. static int set_localloop(xpd_t *xpd, bool localloop)
  732. {
  733. struct PRI_priv_data *priv;
  734. BUG_ON(!xpd);
  735. priv = xpd->priv;
  736. if(SPAN_REGISTERED(xpd)) {
  737. XPD_NOTICE(xpd, "Registered as span %d. Cannot do %s\n",
  738. xpd->span.spanno, __FUNCTION__);
  739. return -EBUSY;
  740. }
  741. priv->local_loopback = localloop;
  742. XPD_DBG(SIGNAL, xpd, "%s: %s\n", __FUNCTION__, (localloop) ? "LOCALLOOP" : "NO");
  743. set_master_mode(__FUNCTION__, xpd);
  744. return 0;
  745. }
  746. #define VALID_CONFIG(bit,flg,str) [bit] = { .flags = flg, .name = str }
  747. static const struct {
  748. const char *name;
  749. const int flags;
  750. } valid_spanconfigs[sizeof(unsigned int)*8] = {
  751. /* These apply to T1 */
  752. VALID_CONFIG(4, DAHDI_CONFIG_D4, "D4"),
  753. VALID_CONFIG(5, DAHDI_CONFIG_ESF, "ESF"),
  754. VALID_CONFIG(6, DAHDI_CONFIG_AMI, "AMI"),
  755. VALID_CONFIG(7, DAHDI_CONFIG_B8ZS, "B8ZS"),
  756. /* These apply to E1 */
  757. VALID_CONFIG(8, DAHDI_CONFIG_CCS, "CCS"),
  758. VALID_CONFIG(9, DAHDI_CONFIG_HDB3, "HDB3"),
  759. VALID_CONFIG(10, DAHDI_CONFIG_CRC4, "CRC4"),
  760. };
  761. /*
  762. * Mark the lines as CLEAR or RBS signalling.
  763. * With T1, we need to mark the CLEAR lines on the REG_CCB1_T registers
  764. * Should be called only when we are registered to DAHDI
  765. * The channo parameter:
  766. * channo == 0: set lines for the whole span
  767. * channo != 0: only set modified lines
  768. */
  769. static void set_rbslines(xpd_t *xpd, int channo)
  770. {
  771. struct PRI_priv_data *priv;
  772. xpp_line_t new_rbslines = 0;
  773. xpp_line_t modified_lines;
  774. int i;
  775. priv = xpd->priv;
  776. for_each_line(xpd, i) {
  777. struct dahdi_chan *chan = XPD_CHAN(xpd, i);
  778. if(chan->flags & DAHDI_FLAG_CLEAR)
  779. BIT_CLR(new_rbslines, i);
  780. else
  781. BIT_SET(new_rbslines, i);
  782. }
  783. new_rbslines &= BITMASK(xpd->channels);
  784. modified_lines = priv->rbslines ^ new_rbslines;
  785. XPD_DBG(DEVICES, xpd, "RBSLINES-%d(%s): 0x%X\n",
  786. channo, pri_protocol_name(priv->pri_protocol), new_rbslines);
  787. if((priv->pri_protocol == PRI_PROTO_T1) || (priv->pri_protocol == PRI_PROTO_J1)) {
  788. byte clear_lines = 0; /* Mark clear lines */
  789. bool reg_changed = 0;
  790. for_each_line(xpd, i) {
  791. int bytenum = i / 8;
  792. int bitnum = i % 8;
  793. if(!IS_SET(new_rbslines, i)) {
  794. BIT_SET(clear_lines, (7 - bitnum));
  795. }
  796. if(IS_SET(modified_lines, i))
  797. reg_changed = 1;
  798. if(bitnum == 7) {
  799. if(channo == 0 || reg_changed) {
  800. bytenum += REG_CCB1_T;
  801. XPD_DBG(DEVICES, xpd, "RBS(%s): modified=0x%X rbslines=0x%X reg=0x%X clear_lines=0x%X\n",
  802. pri_protocol_name(priv->pri_protocol),
  803. modified_lines, new_rbslines, bytenum, clear_lines);
  804. write_subunit(xpd, bytenum, clear_lines);
  805. }
  806. clear_lines = 0;
  807. reg_changed = 0;
  808. }
  809. }
  810. }
  811. priv->rbslines = new_rbslines;
  812. }
  813. static int set_mode_cas(xpd_t *xpd, bool want_cas)
  814. {
  815. struct PRI_priv_data *priv;
  816. priv = xpd->priv;
  817. XPD_INFO(xpd, "Setting TDM to %s\n", (want_cas) ? "CAS" : "PRI");
  818. if(want_cas) {
  819. priv->is_cas = 1;
  820. priv->dchan_alive = 0;
  821. } else {
  822. priv->is_cas = 0;
  823. }
  824. return 0;
  825. }
  826. static int pri_lineconfig(xpd_t *xpd, int lineconfig)
  827. {
  828. struct PRI_priv_data *priv;
  829. const char *framingstr = "";
  830. const char *codingstr = "";
  831. const char *crcstr = "";
  832. #ifdef JAPANEZE_SUPPORT
  833. byte rc0 = 0; /* FIXME: PCM offsets */
  834. #endif
  835. byte fmr0 = 0;
  836. byte fmr1 = REG_FMR1_ECM;
  837. byte fmr2 = 0;
  838. byte fmr3 = 0; /* write only for CRC4 */
  839. byte fmr4 = 0;
  840. byte cmdr = REG_CMDR_RRES | REG_CMDR_XRES;
  841. byte xsp = 0;
  842. unsigned int bad_bits;
  843. bool force_cas = 0;
  844. int i;
  845. BUG_ON(!xpd);
  846. priv = xpd->priv;
  847. /*
  848. * validate
  849. */
  850. bad_bits = lineconfig & pri_linecompat(priv->pri_protocol);
  851. bad_bits = bad_bits ^ lineconfig;
  852. for(i = 0; i < ARRAY_SIZE(valid_spanconfigs); i++) {
  853. unsigned int flags = valid_spanconfigs[i].flags;
  854. if(bad_bits & BIT(i)) {
  855. if(flags) {
  856. XPD_ERR(xpd,
  857. "Bad config item '%s' for %s. Ignore\n",
  858. valid_spanconfigs[i].name,
  859. pri_protocol_name(priv->pri_protocol));
  860. } else {
  861. /* we got real garbage */
  862. XPD_ERR(xpd,
  863. "Unknown config item 0x%lX for %s. Ignore\n",
  864. BIT(i),
  865. pri_protocol_name(priv->pri_protocol));
  866. }
  867. }
  868. if(flags && flags != BIT(i)) {
  869. ERR("%s: BUG: i=%d flags=0x%X\n",
  870. __FUNCTION__, i, flags);
  871. // BUG();
  872. }
  873. }
  874. if(bad_bits)
  875. goto bad_lineconfig;
  876. if(priv->pri_protocol == PRI_PROTO_E1) {
  877. fmr1 |= REG_FMR1_AFR;
  878. fmr2 = REG_FMR2_E_AXRA | REG_FMR2_E_ALMF; /* 0x03 */
  879. fmr4 = 0x9F; /* E1.XSW: All spare bits = 1*/
  880. xsp |= REG_XSP_E_EBP | REG_XSP_E_AXS | REG_XSP_E_XSIF;
  881. } else if(priv->pri_protocol == PRI_PROTO_T1) {
  882. fmr1 |= REG_FMR1_PMOD | REG_FMR1_T_CRC;
  883. fmr2 = REG_FMR2_T_SSP | REG_FMR2_T_AXRA; /* 0x22 */
  884. fmr4 = 0x0C;
  885. xsp &= ~REG_FMR5_T_XTM;
  886. force_cas = 1; /* T1 - Chip always in CAS mode */
  887. } else if(priv->pri_protocol == PRI_PROTO_J1) {
  888. fmr1 |= REG_FMR1_PMOD;
  889. fmr4 = 0x1C;
  890. xsp &= ~REG_FMR5_T_XTM;
  891. force_cas = 1; /* T1 - Chip always in CAS mode */
  892. XPD_ERR(xpd, "J1 unsupported yet\n");
  893. return -ENOSYS;
  894. }
  895. if(priv->local_loopback)
  896. fmr2 |= REG_FMR2_E_PLB;
  897. /* framing first */
  898. if (lineconfig & DAHDI_CONFIG_B8ZS) {
  899. framingstr = "B8ZS";
  900. fmr0 = REG_FMR0_E_XC1 | REG_FMR0_E_XC0 | REG_FMR0_E_RC1 | REG_FMR0_E_RC0;
  901. } else if (lineconfig & DAHDI_CONFIG_AMI) {
  902. framingstr = "AMI";
  903. fmr0 = REG_FMR0_E_XC1 | REG_FMR0_E_RC1;
  904. /*
  905. * From Infineon Errata Sheet: PEF 22554, Version 3.1
  906. * Problem: Incorrect CAS Receiption when
  907. * using AMI receive line code
  908. * Workaround: For E1,
  909. * "...The receive line coding HDB3 is
  910. * recommended instead."
  911. * For T1,
  912. * "...in T1 mode it is recommended to
  913. * configure the Rx side to B8ZS coding"
  914. * For both cases this is the same bit in FMR0
  915. */
  916. if(priv->pri_protocol == PRI_PROTO_J1)
  917. XPD_NOTICE(xpd, "J1 is not supported yet\n");
  918. else
  919. fmr0 |= REG_FMR0_E_RC0;
  920. } else if (lineconfig & DAHDI_CONFIG_HDB3) {
  921. framingstr = "HDB3";
  922. fmr0 = REG_FMR0_E_XC1 | REG_FMR0_E_XC0 | REG_FMR0_E_RC1 | REG_FMR0_E_RC0;
  923. } else {
  924. XPD_NOTICE(xpd, "Bad lineconfig. Not (B8ZS|AMI|HDB3). Ignored.\n");
  925. return -EINVAL;
  926. }
  927. /* then coding */
  928. priv->is_esf = 0;
  929. if (lineconfig & DAHDI_CONFIG_ESF) {
  930. codingstr = "ESF";
  931. fmr4 |= REG_FMR4_FM1;
  932. fmr2 |= REG_FMR2_T_AXRA | REG_FMR2_T_MCSP | REG_FMR2_T_SSP;
  933. priv->is_esf = 1;
  934. } else if (lineconfig & DAHDI_CONFIG_D4) {
  935. codingstr = "D4";
  936. } else if (lineconfig & DAHDI_CONFIG_CCS) {
  937. codingstr = "CCS";
  938. set_mode_cas(xpd, 0); /* In E1 we know right from the span statement. */
  939. } else {
  940. codingstr = "CAS"; /* In E1 we know right from the span statement. */
  941. force_cas = 1;
  942. set_mode_cas(xpd, 1);
  943. }
  944. CALL_XMETHOD(card_pcm_recompute, xpd->xbus, xpd, 0);
  945. /*
  946. * E1's can enable CRC checking
  947. * CRC4 is legal only for E1, and it is checked by pri_linecompat()
  948. * in the beginning of the function.
  949. */
  950. if (lineconfig & DAHDI_CONFIG_CRC4) {
  951. crcstr = "CRC4";
  952. fmr1 |= REG_FMR1_E_XFS;
  953. fmr2 |= REG_FMR2_E_RFS1;
  954. fmr3 |= REG_FMR3_EXTIW;
  955. }
  956. XPD_DBG(GENERAL, xpd, "[%s] lineconfig=%s/%s/%s %s (0x%X)\n",
  957. (priv->clock_source)?"MASTER":"SLAVE",
  958. framingstr, codingstr, crcstr,
  959. (lineconfig & DAHDI_CONFIG_NOTOPEN)?"YELLOW":"",
  960. lineconfig);
  961. set_reg_lim0(__FUNCTION__, xpd);
  962. XPD_DBG(GENERAL, xpd, "%s: fmr1(0x%02X) = 0x%02X\n", __FUNCTION__, REG_FMR1, fmr1);
  963. write_subunit(xpd, REG_FMR1, fmr1);
  964. XPD_DBG(GENERAL, xpd, "%s: fmr2(0x%02X) = 0x%02X\n", __FUNCTION__, REG_FMR2, fmr2);
  965. write_subunit(xpd, REG_FMR2, fmr2);
  966. XPD_DBG(GENERAL, xpd, "%s: fmr0(0x%02X) = 0x%02X\n", __FUNCTION__, REG_FMR0, fmr0);
  967. write_subunit(xpd, REG_FMR0, fmr0);
  968. XPD_DBG(GENERAL, xpd, "%s: fmr4(0x%02X) = 0x%02X\n", __FUNCTION__, REG_FMR4, fmr4);
  969. write_subunit(xpd, REG_FMR4, fmr4);
  970. if(fmr3) {
  971. XPD_DBG(GENERAL, xpd, "%s: fmr3(0x%02X) = 0x%02X\n", __FUNCTION__, REG_FMR3, fmr3);
  972. write_subunit(xpd, REG_FMR3, fmr3);
  973. }
  974. XPD_DBG(GENERAL, xpd, "%s: cmdr(0x%02X) = 0x%02X\n", __FUNCTION__, REG_CMDR_E, cmdr);
  975. write_subunit(xpd, REG_CMDR_E, cmdr);
  976. #ifdef JAPANEZE_SUPPORT
  977. if(rc0) {
  978. XPD_DBG(GENERAL, xpd, "%s: rc0(0x%02X) = 0x%02X\n", __FUNCTION__, REG_RC0, rc0);
  979. write_subunit(xpd, REG_RC0, rc0);
  980. }
  981. #endif
  982. if(force_cas) {
  983. xsp |= REG_XSP_E_CASEN; /* Same as REG_FMR5_T_EIBR for T1 */
  984. }
  985. XPD_DBG(GENERAL, xpd, "%s: xsp(0x%02X) = 0x%02X\n", __FUNCTION__, REG_XSP_E, xsp);
  986. write_subunit(xpd, REG_XSP_E, xsp);
  987. return 0;
  988. bad_lineconfig:
  989. XPD_ERR(xpd, "Bad lineconfig. Abort\n");
  990. return -EINVAL;
  991. }
  992. /*
  993. * Called only for 'span' keyword in /etc/dahdi/system.conf
  994. */
  995. static int pri_spanconfig(struct dahdi_span *span, struct dahdi_lineconfig *lc)
  996. {
  997. xpd_t *xpd = container_of(span, struct xpd, span);
  998. struct PRI_priv_data *priv;
  999. int ret;
  1000. BUG_ON(!xpd);
  1001. priv = xpd->priv;
  1002. if(lc->span != xpd->span.spanno) {
  1003. XPD_ERR(xpd, "I am span %d but got spanconfig for span %d\n",
  1004. xpd->span.spanno, lc->span);
  1005. return -EINVAL;
  1006. }
  1007. /*
  1008. * FIXME: lc->name is unused by dahdi_cfg and dahdi...
  1009. * We currently ignore it also.
  1010. */
  1011. XPD_DBG(GENERAL, xpd, "[%s] lbo=%d lineconfig=0x%X sync=%d\n",
  1012. (priv->clock_source)?"MASTER":"SLAVE", lc->lbo, lc->lineconfig, lc->sync);
  1013. ret = pri_lineconfig(xpd, lc->lineconfig);
  1014. if(!ret) {
  1015. span->lineconfig = lc->lineconfig;
  1016. xpd->timing_priority = lc->sync;
  1017. set_master_mode("spanconfig", xpd);
  1018. elect_syncer("PRI-master_mode");
  1019. }
  1020. return ret;
  1021. }
  1022. /*
  1023. * Set signalling type (if appropriate)
  1024. * Called from dahdi with spinlock held on chan. Must not call back
  1025. * dahdi functions.
  1026. */
  1027. static int pri_chanconfig(struct dahdi_chan *chan, int sigtype)
  1028. {
  1029. xpd_t *xpd = container_of(chan->span, struct xpd, span);
  1030. struct PRI_priv_data *priv;
  1031. BUG_ON(!xpd);
  1032. priv = xpd->priv;
  1033. DBG(GENERAL, "channel %d (%s) -> %s\n", chan->channo, chan->name, sig2str(sigtype));
  1034. /*
  1035. * Some bookkeeping to check if we have DChan defined or not
  1036. * FIXME: actually use this to prevent duplicate DChan definitions
  1037. * and prevent DChan definitions with CAS.
  1038. */
  1039. if(is_sigtype_dchan(sigtype)) {
  1040. if(VALID_DCHAN(priv) && DCHAN(priv) != chan->channo) {
  1041. ERR("channel %d (%s) marked DChan but also channel %d.\n",
  1042. chan->channo, chan->name, DCHAN(priv));
  1043. return -EINVAL;
  1044. } else {
  1045. XPD_DBG(GENERAL, xpd, "channel %d (%s) marked as DChan\n", chan->channo, chan->name);
  1046. SET_DCHAN(priv, chan->channo);
  1047. /* In T1, we don't know before-hand */
  1048. if(priv->pri_protocol != PRI_PROTO_E1 && priv->is_cas != 0)
  1049. set_mode_cas(xpd, 0);
  1050. }
  1051. } else {
  1052. if(chan->channo == 1) {
  1053. XPD_DBG(GENERAL, xpd, "channel %d (%s) marked a not DChan\n", chan->channo, chan->name);
  1054. SET_DCHAN(priv, NO_DCHAN);
  1055. }
  1056. /* In T1, we don't know before-hand */
  1057. if(priv->pri_protocol != PRI_PROTO_E1 && priv->is_cas != 1)
  1058. set_mode_cas(xpd, 1);
  1059. }
  1060. if(xpd->span.flags & DAHDI_FLAG_RUNNING) {
  1061. XPD_DBG(DEVICES, xpd, "Span is RUNNING. Updating rbslines.\n");
  1062. set_rbslines(xpd, chan->channo);
  1063. }
  1064. // FIXME: sanity checks:
  1065. // - should be supported (within the sigcap)
  1066. // - should not replace fxs <->fxo ??? (covered by previous?)
  1067. return 0;
  1068. }
  1069. static xpd_t *PRI_card_new(xbus_t *xbus, int unit, int subunit, const xproto_table_t *proto_table,
  1070. byte subtype, int subunits, int subunit_ports, bool to_phone)
  1071. {
  1072. xpd_t *xpd = NULL;
  1073. struct PRI_priv_data *priv;
  1074. int channels = min(31, CHANNELS_PERXPD); /* worst case */
  1075. if(subunit_ports != 1) {
  1076. XBUS_ERR(xbus, "Bad subunit_ports=%d\n", subunit_ports);
  1077. return NULL;
  1078. }
  1079. XBUS_DBG(GENERAL, xbus, "\n");
  1080. xpd = xpd_alloc(xbus, unit, subunit, subtype, subunits, sizeof(struct PRI_priv_data), proto_table, channels);
  1081. if(!xpd)
  1082. return NULL;
  1083. priv = xpd->priv;
  1084. priv->pri_protocol = PRI_PROTO_0; /* Default, changes in set_pri_proto() */
  1085. priv->deflaw = DAHDI_LAW_DEFAULT; /* Default, changes in set_pri_proto() */
  1086. xpd->type_name = type_name(priv->pri_protocol);
  1087. #ifdef OLD_PROC
  1088. if(pri_proc_create(xbus, xpd) < 0) {
  1089. xpd_free(xpd);
  1090. return NULL;
  1091. }
  1092. #endif
  1093. xbus->sync_mode_default = SYNC_MODE_AB;
  1094. return xpd;
  1095. }
  1096. static int PRI_card_init(xbus_t *xbus, xpd_t *xpd)
  1097. {
  1098. struct PRI_priv_data *priv;
  1099. int ret = 0;
  1100. xproto_table_t *proto_table;
  1101. BUG_ON(!xpd);
  1102. XPD_DBG(GENERAL, xpd, "\n");
  1103. xpd->type = XPD_TYPE_PRI;
  1104. proto_table = &PROTO_TABLE(PRI);
  1105. priv = xpd->priv;
  1106. if(priv->pri_protocol == PRI_PROTO_0) {
  1107. /*
  1108. * init_card_* script didn't set pri protocol
  1109. * Let's have a default E1
  1110. */
  1111. ret = set_pri_proto(xpd, PRI_PROTO_E1);
  1112. if(ret < 0)
  1113. goto err;
  1114. }
  1115. SET_DCHAN(priv, NO_DCHAN);
  1116. /*
  1117. * initialization script should have set correct
  1118. * operating modes.
  1119. */
  1120. if(!valid_pri_modes(xpd)) {
  1121. XPD_NOTICE(xpd, "PRI protocol not set\n");
  1122. goto err;
  1123. }
  1124. xpd->type_name = type_name(priv->pri_protocol);
  1125. xpd->direction = TO_PSTN;
  1126. XPD_DBG(DEVICES, xpd, "%s\n", xpd->type_name);
  1127. xpd->timing_priority = 1; /* High priority SLAVE */
  1128. set_master_mode(__FUNCTION__, xpd);
  1129. for(ret = 0; ret < NUM_LEDS; ret++) {
  1130. DO_LED(xpd, ret, PRI_LED_ON);
  1131. msleep(20);
  1132. DO_LED(xpd, ret, PRI_LED_OFF);
  1133. }
  1134. priv->initialized = 1;
  1135. return 0;
  1136. err:
  1137. #ifdef OLD_PROC
  1138. pri_proc_remove(xbus, xpd);
  1139. #endif
  1140. XPD_ERR(xpd, "Failed initializing registers (%d)\n", ret);
  1141. return ret;
  1142. }
  1143. static int PRI_card_remove(xbus_t *xbus, xpd_t *xpd)
  1144. {
  1145. struct PRI_priv_data *priv;
  1146. BUG_ON(!xpd);
  1147. priv = xpd->priv;
  1148. XPD_DBG(GENERAL, xpd, "\n");
  1149. #ifdef OLD_PROC
  1150. pri_proc_remove(xbus, xpd);
  1151. #endif
  1152. return 0;
  1153. }
  1154. #ifdef DAHDI_AUDIO_NOTIFY
  1155. static int pri_audio_notify(struct dahdi_chan *chan, int on)
  1156. {
  1157. xpd_t *xpd = chan->pvt;
  1158. int pos = chan->chanpos - 1;
  1159. BUG_ON(!xpd);
  1160. LINE_DBG(SIGNAL, xpd, pos, "PRI-AUDIO: %s\n", (on) ? "on" : "off");
  1161. mark_offhook(xpd, pos, on);
  1162. return 0;
  1163. }
  1164. #endif
  1165. static const struct dahdi_span_ops PRI_span_ops = {
  1166. .owner = THIS_MODULE,
  1167. .spanconfig = pri_spanconfig,
  1168. .chanconfig = pri_chanconfig,
  1169. .startup = pri_startup,
  1170. .shutdown = pri_shutdown,
  1171. .rbsbits = pri_rbsbits,
  1172. .open = xpp_open,
  1173. .close = xpp_close,
  1174. .ioctl = xpp_ioctl,
  1175. .maint = xpp_maint,
  1176. #ifdef DAHDI_SYNC_TICK
  1177. .sync_tick = dahdi_sync_tick,
  1178. #endif
  1179. #ifdef CONFIG_DAHDI_WATCHDOG
  1180. .watchdog = xpp_watchdog,
  1181. #endif
  1182. #ifdef DAHDI_AUDIO_NOTIFY
  1183. .audio_notify = pri_audio_notify,
  1184. #endif
  1185. };
  1186. static int PRI_card_dahdi_preregistration(xpd_t *xpd, bool on)
  1187. {
  1188. xbus_t *xbus;
  1189. struct PRI_priv_data *priv;
  1190. int i;
  1191. BUG_ON(!xpd);
  1192. xbus = xpd->xbus;
  1193. priv = xpd->priv;
  1194. BUG_ON(!xbus);
  1195. XPD_DBG(GENERAL, xpd, "%s (proto=%s, channels=%d, deflaw=%d)\n",
  1196. (on)?"on":"off",
  1197. pri_protocol_name(priv->pri_protocol),
  1198. xpd->channels,
  1199. priv->deflaw);
  1200. if(!on) {
  1201. /* Nothing to do yet */
  1202. return 0;
  1203. }
  1204. xpd->span.spantype = pri_protocol_name(priv->pri_protocol);
  1205. xpd->span.linecompat = pri_linecompat(priv->pri_protocol);
  1206. xpd->span.deflaw = priv->deflaw;
  1207. for_each_line(xpd, i) {
  1208. struct dahdi_chan *cur_chan = XPD_CHAN(xpd, i);
  1209. bool is_dchan = i == PRI_DCHAN_IDX(priv);
  1210. XPD_DBG(GENERAL, xpd, "setting PRI channel %d (%s)\n", i,
  1211. (is_dchan)?"DCHAN":"CLEAR");
  1212. snprintf(cur_chan->name, MAX_CHANNAME, "XPP_%s/%02d/%1d%1d/%d",
  1213. xpd->type_name, xbus->num, xpd->addr.unit, xpd->addr.subunit, i);
  1214. cur_chan->chanpos = i + 1;
  1215. cur_chan->pvt = xpd;
  1216. cur_chan->sigcap = PRI_SIGCAP;
  1217. if(is_dchan && !priv->is_cas) { /* D-CHAN */
  1218. //FIXME: cur_chan->flags |= DAHDI_FLAG_PRIDCHAN;
  1219. cur_chan->flags &= ~DAHDI_FLAG_HDLC;
  1220. }
  1221. }
  1222. xpd->offhook_state = xpd->wanted_pcm_mask;
  1223. xpd->span.ops = &PRI_span_ops;
  1224. return 0;
  1225. }
  1226. static int PRI_card_dahdi_postregistration(xpd_t *xpd, bool on)
  1227. {
  1228. xbus_t *xbus;
  1229. struct PRI_priv_data *priv;
  1230. BUG_ON(!xpd);
  1231. xbus = xpd->xbus;
  1232. priv = xpd->priv;
  1233. BUG_ON(!xbus);
  1234. XPD_DBG(GENERAL, xpd, "%s\n", (on)?"on":"off");
  1235. dahdi_update_syncsrc(xpd);
  1236. return(0);
  1237. }
  1238. static void dchan_state(xpd_t *xpd, bool up)
  1239. {
  1240. struct PRI_priv_data *priv;
  1241. BUG_ON(!xpd);
  1242. priv = xpd->priv;
  1243. BUG_ON(!priv);
  1244. if(priv->is_cas) {
  1245. return;
  1246. }
  1247. if(priv->dchan_alive == up)
  1248. return;
  1249. if(!priv->layer1_up) /* No layer1, kill dchan */
  1250. up = 0;
  1251. if(up) {
  1252. XPD_DBG(SIGNAL, xpd, "STATE CHANGE: D-Channel RUNNING\n");
  1253. priv->dchan_alive = 1;
  1254. } else {
  1255. int d = PRI_DCHAN_IDX(priv);
  1256. if(SPAN_REGISTERED(xpd) && d >= 0 && d < xpd->channels) {
  1257. byte *pcm;
  1258. pcm = (byte *)XPD_CHAN(xpd, d)->readchunk;
  1259. pcm[0] = 0x00;
  1260. pcm = (byte *)XPD_CHAN(xpd, d)->writechunk;
  1261. pcm[0] = 0x00;
  1262. }
  1263. XPD_DBG(SIGNAL, xpd, "STATE CHANGE: D-Channel STOPPED\n");
  1264. priv->dchan_rx_counter = priv->dchan_tx_counter = 0;
  1265. priv->dchan_alive = 0;
  1266. priv->dchan_alive_ticks = 0;
  1267. priv->dchan_rx_sample = priv->dchan_tx_sample = 0x00;
  1268. }
  1269. }
  1270. /*
  1271. * LED managment is done by the driver now:
  1272. * - Turn constant ON RED/GREEN led to indicate MASTER/SLAVE port
  1273. * - Very fast "Double Blink" to indicate Layer1 alive (without D-Channel)
  1274. * - Constant blink (1/2 sec cycle) to indicate D-Channel alive.
  1275. */
  1276. static void handle_leds(xbus_t *xbus, xpd_t *xpd)
  1277. {
  1278. struct PRI_priv_data *priv;
  1279. unsigned int timer_count;
  1280. int which_led;
  1281. int other_led;
  1282. enum pri_led_state ledstate;
  1283. int mod;
  1284. BUG_ON(!xpd);
  1285. priv = xpd->priv;
  1286. BUG_ON(!priv);
  1287. if(xpd->timing_priority == 0) {
  1288. which_led = TOP_RED_LED;
  1289. other_led = BOTTOM_GREEN_LED;
  1290. } else {
  1291. which_led = BOTTOM_GREEN_LED;
  1292. other_led = TOP_RED_LED;
  1293. }
  1294. ledstate = priv->ledstate[which_led];
  1295. timer_count = xpd->timer_count;
  1296. if(xpd->blink_mode) {
  1297. if((timer_count % DEFAULT_LED_PERIOD) == 0) {
  1298. // led state is toggled
  1299. if(ledstate == PRI_LED_OFF) {
  1300. DO_LED(xpd, which_led, PRI_LED_ON);
  1301. DO_LED(xpd, other_led, PRI_LED_ON);
  1302. } else {
  1303. DO_LED(xpd, which_led, PRI_LED_OFF);
  1304. DO_LED(xpd, other_led, PRI_LED_OFF);
  1305. }
  1306. }
  1307. return;
  1308. }
  1309. if(priv->ledstate[other_led] != PRI_LED_OFF)
  1310. DO_LED(xpd, other_led, PRI_LED_OFF);
  1311. if(priv->dchan_alive) {
  1312. mod = timer_count % 1000;
  1313. switch(mod) {
  1314. case 0:
  1315. DO_LED(xpd, which_led, PRI_LED_ON);
  1316. break;
  1317. case 500:
  1318. DO_LED(xpd, which_led, PRI_LED_OFF);
  1319. break;
  1320. }
  1321. } else if(priv->layer1_up) {
  1322. mod = timer_count % 1000;
  1323. switch(mod) {
  1324. case 0:
  1325. case 100:
  1326. DO_LED(xpd, which_led, PRI_LED_ON);
  1327. break;
  1328. case 50:
  1329. case 150:
  1330. DO_LED(xpd, which_led, PRI_LED_OFF);
  1331. break;
  1332. }
  1333. } else {
  1334. if(priv->ledstate[which_led] != PRI_LED_ON)
  1335. DO_LED(xpd, which_led, PRI_LED_ON);
  1336. }
  1337. }
  1338. static int PRI_card_tick(xbus_t *xbus, xpd_t *xpd)
  1339. {
  1340. struct PRI_priv_data *priv;
  1341. BUG_ON(!xpd);
  1342. priv = xpd->priv;
  1343. BUG_ON(!priv);
  1344. if(!priv->initialized || !xbus->self_ticking)
  1345. return 0;
  1346. /*
  1347. * Poll layer1 status (cascade subunits)
  1348. */
  1349. if(poll_interval != 0 &&
  1350. ((xpd->timer_count % poll_interval) == 0)) {
  1351. priv->poll_noreplies++;
  1352. query_subunit(xpd, REG_FRS0);
  1353. //query_subunit(xpd, REG_FRS1);
  1354. }
  1355. if(priv->dchan_tx_counter >= 1 && priv->dchan_rx_counter > 1) {
  1356. dchan_state(xpd, 1);
  1357. priv->dchan_alive_ticks++;
  1358. }
  1359. handle_leds(xbus, xpd);
  1360. return 0;
  1361. }
  1362. static int PRI_card_ioctl(xpd_t *xpd, int pos, unsigned int cmd, unsigned long arg)
  1363. {
  1364. struct dahdi_chan *chan;
  1365. BUG_ON(!xpd);
  1366. if(!XBUS_IS(xpd->xbus, READY))
  1367. return -ENODEV;
  1368. chan = XPD_CHAN(xpd, pos);
  1369. switch (cmd) {
  1370. case DAHDI_TONEDETECT:
  1371. /*
  1372. * Asterisk call all span types with this (FXS specific)
  1373. * call. Silently ignore it.
  1374. */
  1375. LINE_DBG(SIGNAL, xpd, pos, "PRI: TONEDETECT (%s)\n",
  1376. (chan->flags & DAHDI_FLAG_AUDIO) ?
  1377. "AUDIO" : "SILENCE");
  1378. return -ENOTTY;
  1379. case DAHDI_ONHOOKTRANSFER:
  1380. LINE_DBG(SIGNAL, xpd, pos, "PRI: ONHOOKTRANSFER\n");
  1381. return -ENOTTY;
  1382. case DAHDI_VMWI:
  1383. LINE_DBG(SIGNAL, xpd, pos, "PRI: VMWI\n");
  1384. return -ENOTTY;
  1385. case DAHDI_VMWI_CONFIG:
  1386. LINE_DBG(SIGNAL, xpd, pos, "PRI: VMWI_CONFIG\n");
  1387. return -ENOTTY;
  1388. default:
  1389. report_bad_ioctl(THIS_MODULE->name, xpd, pos, cmd);
  1390. return -ENOTTY;
  1391. }
  1392. return 0;
  1393. }
  1394. static int PRI_card_close(xpd_t *xpd, lineno_t pos)
  1395. {
  1396. //struct dahdi_chan *chan = XPD_CHAN(xpd, pos);
  1397. dchan_state(xpd, 0);
  1398. return 0;
  1399. }
  1400. /*
  1401. * Called only for 'span' keyword in /etc/dahdi/system.conf
  1402. */
  1403. static int pri_startup(struct dahdi_span *span)
  1404. {
  1405. xpd_t *xpd = container_of(span, struct xpd, span);
  1406. struct PRI_priv_data *priv;
  1407. BUG_ON(!xpd);
  1408. priv = xpd->priv;
  1409. BUG_ON(!priv);
  1410. if(!XBUS_IS(xpd->xbus, READY)) {
  1411. XPD_DBG(GENERAL, xpd, "Startup called by dahdi. No Hardware. Ignored\n");
  1412. return -ENODEV;
  1413. }
  1414. XPD_DBG(GENERAL, xpd, "STARTUP\n");
  1415. // Turn on all channels
  1416. CALL_XMETHOD(XPD_STATE, xpd->xbus, xpd, 1);
  1417. set_rbslines(xpd, 0);
  1418. write_subunit(xpd, REG_XPM2, 0x00);
  1419. return 0;
  1420. }
  1421. /*
  1422. * Called only for 'span' keyword in /etc/dahdi/system.conf
  1423. */
  1424. static int pri_shutdown(struct dahdi_span *span)
  1425. {
  1426. xpd_t *xpd = container_of(span, struct xpd, span);
  1427. struct PRI_priv_data *priv;
  1428. BUG_ON(!xpd);
  1429. priv = xpd->priv;
  1430. BUG_ON(!priv);
  1431. if(!XBUS_IS(xpd->xbus, READY)) {
  1432. XPD_DBG(GENERAL, xpd, "Shutdown called by dahdi. No Hardware. Ignored\n");
  1433. return -ENODEV;
  1434. }
  1435. XPD_DBG(GENERAL, xpd, "SHUTDOWN\n");
  1436. // Turn off all channels
  1437. CALL_XMETHOD(XPD_STATE, xpd->xbus, xpd, 0);
  1438. return 0;
  1439. }
  1440. static int encode_rbsbits_e1(xpd_t *xpd, int pos, int bits)
  1441. {
  1442. struct PRI_priv_data *priv;
  1443. byte val;
  1444. int rsnum;
  1445. BUG_ON(!xpd);
  1446. priv = xpd->priv;
  1447. BUG_ON(!priv);
  1448. BUG_ON(priv->pri_protocol != PRI_PROTO_E1);
  1449. if(pos == 15)
  1450. return 0; /* Don't write dchan in CAS */
  1451. if(pos < 0 || pos > 31) {
  1452. XPD_NOTICE(xpd, "%s: pos=%d out of range. Ignore\n", __FUNCTION__, pos);
  1453. return 0;
  1454. }
  1455. if(pos >= 16) {
  1456. /* Low nibble */
  1457. rsnum = pos - 16;
  1458. val = (priv->cas_ts_e[rsnum] & 0xF0) | (bits & 0x0F);
  1459. } else {
  1460. /* High nibble */
  1461. rsnum = pos;
  1462. val = (priv->cas_ts_e[rsnum] & 0x0F) | ((bits << 4) & 0xF0);
  1463. }
  1464. LINE_DBG(SIGNAL, xpd, pos, "RBS: TX: bits=0x%X\n", bits);
  1465. write_cas_reg(xpd, rsnum, val);
  1466. return 0;
  1467. }
  1468. static int encode_rbsbits_t1(xpd_t *xpd, int pos, int bits)
  1469. {
  1470. struct PRI_priv_data *priv;
  1471. int rsnum;
  1472. int chan_per_reg;
  1473. int offset;
  1474. int width;
  1475. uint tx_bits = bits;
  1476. uint mask;
  1477. byte val;
  1478. BUG_ON(!xpd);
  1479. priv = xpd->priv;
  1480. BUG_ON(!priv);
  1481. BUG_ON(priv->pri_protocol != PRI_PROTO_T1);
  1482. if(pos < 0 || pos >= xpd->channels) {
  1483. XPD_ERR(xpd, "%s: Bad pos=%d\n", __FUNCTION__, pos);
  1484. return 0;
  1485. }
  1486. chan_per_reg = CHAN_PER_REGS(priv);
  1487. width = 8 / chan_per_reg;
  1488. rsnum = pos / chan_per_reg;
  1489. offset = pos % chan_per_reg;
  1490. mask = BITMASK(width) << (chan_per_reg - offset - 1) * width;
  1491. if (!priv->is_esf)
  1492. tx_bits >>= 2;
  1493. tx_bits &= BITMASK(width);
  1494. tx_bits <<= (chan_per_reg - offset - 1) * width;
  1495. val = priv->cas_ts_e[rsnum];
  1496. val &= ~mask;
  1497. val |= tx_bits;
  1498. LINE_DBG(SIGNAL, xpd, pos,
  1499. "bits=0x%02X RS%02d(%s) offset=%d tx_bits=0x%02X\n",
  1500. bits, rsnum+1,
  1501. (priv->is_esf) ? "esf" : "d4",
  1502. offset, tx_bits);
  1503. write_cas_reg(xpd, rsnum , val);
  1504. priv->dchan_tx_counter++;
  1505. return 0;
  1506. }
  1507. static void send_idlebits(xpd_t *xpd, bool saveold)
  1508. {
  1509. struct PRI_priv_data *priv;
  1510. byte save_rs[NUM_CAS_RS_E];
  1511. int i;
  1512. if (!SPAN_REGISTERED(xpd))
  1513. return;
  1514. priv = xpd->priv;
  1515. BUG_ON(!priv);
  1516. XPD_DBG(SIGNAL, xpd, "saveold=%d\n", saveold);
  1517. if (saveold)
  1518. memcpy(save_rs, priv->cas_ts_e, sizeof(save_rs));
  1519. for_each_line(xpd, i) {
  1520. struct dahdi_chan *chan = XPD_CHAN(xpd, i);
  1521. pri_rbsbits(chan, chan->idlebits);
  1522. }
  1523. if (saveold)
  1524. memcpy(priv->cas_ts_e, save_rs, sizeof(save_rs));
  1525. }
  1526. static void send_oldbits(xpd_t *xpd)
  1527. {
  1528. struct PRI_priv_data *priv;
  1529. int i;
  1530. BUG_ON(!xpd);
  1531. priv = xpd->priv;
  1532. BUG_ON(!priv);
  1533. XPD_DBG(SIGNAL, xpd, "\n");
  1534. for (i = 0; i < cas_numregs(xpd); i++)
  1535. write_cas_reg(xpd, i , priv->cas_ts_e[i]);
  1536. }
  1537. static int pri_rbsbits(struct dahdi_chan *chan, int bits)
  1538. {
  1539. xpd_t *xpd;
  1540. struct PRI_priv_data *priv;
  1541. int pos;
  1542. xpd = chan->pvt;
  1543. BUG_ON(!xpd);
  1544. pos = chan->chanpos - 1;
  1545. priv = xpd->priv;
  1546. BUG_ON(!priv);
  1547. if(!priv->layer1_up) {
  1548. XPD_DBG(SIGNAL, xpd, "RBS: TX: No layer1 yet. Keep going.\n");
  1549. }
  1550. if(!priv->is_cas) {
  1551. XPD_DBG(SIGNAL, xpd, "RBS: TX: not in CAS mode. Ignore.\n");
  1552. return 0;
  1553. }
  1554. if (chan->sig == DAHDI_SIG_NONE) {
  1555. LINE_DBG(SIGNAL, xpd, pos,
  1556. "RBS: TX: sigtyp=%s. , bits=0x%X. Ignore.\n",
  1557. sig2str(chan->sig), bits);
  1558. return 0;
  1559. }
  1560. if(priv->pri_protocol == PRI_PROTO_E1) {
  1561. if(encode_rbsbits_e1(xpd, pos, bits) < 0)
  1562. return -EINVAL;
  1563. } else if(priv->pri_protocol == PRI_PROTO_T1) {
  1564. if(encode_rbsbits_t1(xpd, pos, bits) < 0)
  1565. return -EINVAL;
  1566. } else {
  1567. XPD_NOTICE(xpd, "%s: protocol %s is not supported yet with CAS\n",
  1568. __FUNCTION__, pri_protocol_name(priv->pri_protocol));
  1569. return -EINVAL;
  1570. }
  1571. return 0;
  1572. }
  1573. /*! Copy PCM chunks from the buffers of the xpd to a new packet
  1574. * \param xbus xbus of source xpd.
  1575. * \param xpd source xpd.
  1576. * \param lines a bitmask of the active channels that need to be copied.
  1577. * \param pack packet to be filled.
  1578. *
  1579. * On PRI this function is should also shift the lines mask one bit, as
  1580. * channel 0 on the wire is an internal chip control channel. We only
  1581. * send 31 channels to the device, but they should be called 1-31 rather
  1582. * than 0-30 .
  1583. */
  1584. static void PRI_card_pcm_fromspan(xbus_t *xbus, xpd_t *xpd, xpacket_t *pack)
  1585. {
  1586. struct PRI_priv_data *priv;
  1587. byte *pcm;
  1588. unsigned long flags;
  1589. int i;
  1590. xpp_line_t wanted_lines;
  1591. int physical_chan;
  1592. int physical_mask = 0;
  1593. BUG_ON(!xbus);
  1594. BUG_ON(!xpd);
  1595. BUG_ON(!pack);
  1596. priv = xpd->priv;
  1597. BUG_ON(!priv);
  1598. pcm = RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, pcm);
  1599. spin_lock_irqsave(&xpd->lock, flags);
  1600. wanted_lines = xpd->wanted_pcm_mask;
  1601. physical_chan = 0;
  1602. for_each_line(xpd, i) {
  1603. struct dahdi_chan *chan = XPD_CHAN(xpd, i);
  1604. if(priv->pri_protocol == PRI_PROTO_E1) {
  1605. /* In E1 - Only 0'th channel is unused */
  1606. if(i == 0) {
  1607. physical_chan++;
  1608. }
  1609. } else if(priv->pri_protocol == PRI_PROTO_T1) {
  1610. /* In T1 - Every 4'th channel is unused */
  1611. if((i % 3) == 0) {
  1612. physical_chan++;
  1613. }
  1614. }
  1615. if(IS_SET(wanted_lines, i)) {
  1616. physical_mask |= BIT(physical_chan);
  1617. if(SPAN_REGISTERED(xpd)) {
  1618. #ifdef DEBUG_PCMTX
  1619. int channo = XPD_CHAN(xpd, i)->channo;
  1620. if(pcmtx >= 0 && pcmtx_chan == channo)
  1621. memset((u_char *)pcm, pcmtx, DAHDI_CHUNKSIZE);
  1622. else
  1623. #endif
  1624. memcpy((u_char *)pcm, chan->writechunk, DAHDI_CHUNKSIZE);
  1625. if(i == PRI_DCHAN_IDX(priv)) {
  1626. if(priv->dchan_tx_sample != chan->writechunk[0]) {
  1627. priv->dchan_tx_sample = chan->writechunk[0];
  1628. priv->dchan_tx_counter++;
  1629. } else if(chan->writechunk[0] == 0xFF)
  1630. dchan_state(xpd, 0);
  1631. else
  1632. chan->writechunk[0] = 0xFF; /* Clobber for next tick */
  1633. }
  1634. } else
  1635. memset((u_char *)pcm, DAHDI_XLAW(0, chan), DAHDI_CHUNKSIZE);
  1636. pcm += DAHDI_CHUNKSIZE;
  1637. }
  1638. physical_chan++;
  1639. }
  1640. RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, lines) = physical_mask;
  1641. XPD_COUNTER(xpd, PCM_WRITE)++;
  1642. spin_unlock_irqrestore(&xpd->lock, flags);
  1643. }
  1644. /*! Copy PCM chunks from the packet we received to the xpd struct.
  1645. * \param xbus xbus of target xpd.
  1646. * \param xpd target xpd.
  1647. * \param pack Source packet.
  1648. *
  1649. * On PRI this function is should also shift the lines back mask one bit, as
  1650. * channel 0 on the wire is an internal chip control channel.
  1651. *
  1652. * \see PRI_card_pcm_fromspan
  1653. */
  1654. static void PRI_card_pcm_tospan(xbus_t *xbus, xpd_t *xpd, xpacket_t *pack)
  1655. {
  1656. struct PRI_priv_data *priv;
  1657. byte *pcm;
  1658. xpp_line_t physical_mask;
  1659. unsigned long flags;
  1660. int i;
  1661. int logical_chan;
  1662. if(!SPAN_REGISTERED(xpd))
  1663. return;
  1664. priv = xpd->priv;
  1665. BUG_ON(!priv);
  1666. pcm = RPACKET_FIELD(pack, GLOBAL, PCM_READ, pcm);
  1667. physical_mask = RPACKET_FIELD(pack, GLOBAL, PCM_WRITE, lines);
  1668. spin_lock_irqsave(&xpd->lock, flags);
  1669. logical_chan = 0;
  1670. for (i = 0; i < CHANNELS_PERXPD; i++) {
  1671. volatile u_char *r;
  1672. if(priv->pri_protocol == PRI_PROTO_E1) {
  1673. /* In E1 - Only 0'th channel is unused */
  1674. if(i == 0)
  1675. continue;
  1676. } else if(priv->pri_protocol == PRI_PROTO_T1) {
  1677. /* In T1 - Every 4'th channel is unused */
  1678. if((i % 4) == 0)
  1679. continue;
  1680. }
  1681. if(logical_chan == PRI_DCHAN_IDX(priv) && !priv->is_cas) {
  1682. if(priv->dchan_rx_sample != pcm[0]) {
  1683. if(debug & DBG_PCM) {
  1684. XPD_INFO(xpd, "RX-D-Chan: prev=0x%X now=0x%X\n",
  1685. priv->dchan_rx_sample, pcm[0]);
  1686. dump_packet("RX-D-Chan", pack, 1);
  1687. }
  1688. priv->dchan_rx_sample = pcm[0];
  1689. priv->dchan_rx_counter++;
  1690. } else if(pcm[0] == 0xFF)
  1691. dchan_state(xpd, 0);
  1692. }
  1693. if(IS_SET(physical_mask, i)) {
  1694. r = XPD_CHAN(xpd, logical_chan)->readchunk;
  1695. // memset((u_char *)r, 0x5A, DAHDI_CHUNKSIZE); // DEBUG
  1696. memcpy((u_char *)r, pcm, DAHDI_CHUNKSIZE);
  1697. pcm += DAHDI_CHUNKSIZE;
  1698. }
  1699. logical_chan++;
  1700. }
  1701. XPD_COUNTER(xpd, PCM_READ)++;
  1702. spin_unlock_irqrestore(&xpd->lock, flags);
  1703. }
  1704. int PRI_timing_priority(xbus_t *xbus, xpd_t *xpd)
  1705. {
  1706. struct PRI_priv_data *priv;
  1707. priv = xpd->priv;
  1708. BUG_ON(!priv);
  1709. if (priv->layer1_up)
  1710. return xpd->timing_priority;
  1711. XPD_DBG(SYNC, xpd, "No timing priority (no layer1)\n");
  1712. return -ENOENT;
  1713. }
  1714. /*---------------- PRI: HOST COMMANDS -------------------------------------*/
  1715. static /* 0x0F */ HOSTCMD(PRI, XPD_STATE, bool on)
  1716. {
  1717. BUG_ON(!xpd);
  1718. XPD_DBG(GENERAL, xpd, "%s\n", (on)?"on":"off");
  1719. return 0;
  1720. }
  1721. static /* 0x33 */ HOSTCMD(PRI, SET_LED, enum pri_led_selectors led_sel, enum pri_led_state to_led_state)
  1722. {
  1723. int ret = 0;
  1724. xframe_t *xframe;
  1725. xpacket_t *pack;
  1726. struct pri_leds *pri_leds;
  1727. struct PRI_priv_data *priv;
  1728. BUG_ON(!xbus);
  1729. BUG_ON(!xpd);
  1730. priv = xpd->priv;
  1731. BUG_ON(!priv);
  1732. XPD_DBG(LEDS, xpd, "led_sel=%d to_state=%d\n", led_sel, to_led_state);
  1733. XFRAME_NEW_CMD(xframe, pack, xbus, PRI, SET_LED, xpd->xbus_idx);
  1734. pri_leds = &RPACKET_FIELD(pack, PRI, SET_LED, pri_leds);
  1735. pri_leds->state = to_led_state;
  1736. pri_leds->led_sel = led_sel;
  1737. pri_leds->reserved = 0;
  1738. XPACKET_LEN(pack) = RPACKET_SIZE(PRI, SET_LED);
  1739. ret = send_cmd_frame(xbus, xframe);
  1740. priv->ledstate[led_sel] = to_led_state;
  1741. return ret;
  1742. }
  1743. /*---------------- PRI: Astribank Reply Handlers --------------------------*/
  1744. static void layer1_state(xpd_t *xpd, byte data_low)
  1745. {
  1746. struct PRI_priv_data *priv;
  1747. int alarms = DAHDI_ALARM_NONE;
  1748. int layer1_up_prev;
  1749. BUG_ON(!xpd);
  1750. priv = xpd->priv;
  1751. BUG_ON(!priv);
  1752. priv->poll_noreplies = 0;
  1753. if(data_low & REG_FRS0_LOS)
  1754. alarms |= DAHDI_ALARM_RED;
  1755. if(data_low & REG_FRS0_AIS)
  1756. alarms |= DAHDI_ALARM_BLUE;
  1757. if(data_low & REG_FRS0_RRA)
  1758. alarms |= DAHDI_ALARM_YELLOW;
  1759. layer1_up_prev = priv->layer1_up;
  1760. priv->layer1_up = alarms == DAHDI_ALARM_NONE;
  1761. #if 0
  1762. /*
  1763. * Some bad bits (e.g: LMFA and NMF have no alarm "colors"
  1764. * associated. However, layer1 is still not working if they are set.
  1765. * FIXME: These behave differently in E1/T1, so ignore them for while.
  1766. */
  1767. if(data_low & (REG_FRS0_LMFA | REG_FRS0_E1_NMF))
  1768. priv->layer1_up = 0;
  1769. #endif
  1770. priv->alarms = alarms;
  1771. if(!priv->layer1_up) {
  1772. dchan_state(xpd, 0);
  1773. } else if (priv->is_cas && !layer1_up_prev) {
  1774. int regbase = cas_regbase(xpd);
  1775. int i;
  1776. XPD_DBG(SIGNAL , xpd,
  1777. "Returning From Alarm Refreshing Rx register data \n");
  1778. for (i = 0; i < cas_numregs(xpd); i++)
  1779. query_subunit(xpd, regbase + i);
  1780. }
  1781. if(SPAN_REGISTERED(xpd) && xpd->span.alarms != alarms) {
  1782. char str1[MAX_PROC_WRITE];
  1783. char str2[MAX_PROC_WRITE];
  1784. alarm2str(xpd->span.alarms, str1, sizeof(str1));
  1785. alarm2str(alarms, str2, sizeof(str2));
  1786. XPD_NOTICE(xpd, "Alarms: 0x%X (%s) => 0x%X (%s)\n",
  1787. xpd->span.alarms, str1,
  1788. alarms, str2);
  1789. if (priv->is_cas) {
  1790. if (alarms == DAHDI_ALARM_NONE)
  1791. send_oldbits(xpd);
  1792. else if (xpd->span.alarms == DAHDI_ALARM_NONE)
  1793. send_idlebits(xpd, 1);
  1794. }
  1795. xpd->span.alarms = alarms;
  1796. elect_syncer("LAYER1");
  1797. dahdi_alarm_notify(&xpd->span);
  1798. set_clocking(xpd);
  1799. }
  1800. priv->reg_frs0 = data_low;
  1801. priv->layer1_replies++;
  1802. XPD_DBG(REGS, xpd, "subunit=%d data_low=0x%02X\n", xpd->addr.subunit, data_low);
  1803. }
  1804. static int decode_cas_e1(xpd_t *xpd, byte regnum, byte data_low)
  1805. {
  1806. struct PRI_priv_data *priv;
  1807. uint pos = regnum - REG_RS2_E;
  1808. int rsnum = pos + 2;
  1809. int chan1 = pos;
  1810. int chan2 = pos + 16;
  1811. int val1 = (data_low >> 4) & 0xF;
  1812. int val2 = data_low & 0xF;
  1813. priv = xpd->priv;
  1814. BUG_ON(!priv->is_cas);
  1815. BUG_ON(priv->pri_protocol != PRI_PROTO_E1);
  1816. XPD_DBG(SIGNAL, xpd, "RBS: RX: data_low=0x%02X\n", data_low);
  1817. if(pos < 0 || pos >= NUM_CAS_RS_E) {
  1818. XPD_ERR(xpd, "%s: got bad pos=%d [0-%d]\n", __FUNCTION__, pos, NUM_CAS_RS_E);
  1819. return -EINVAL;
  1820. }
  1821. if(chan1 < 0 || chan1 > xpd->channels) {
  1822. XPD_NOTICE(xpd, "%s: %s CAS: Bad chan1 number (%d)\n",
  1823. __FUNCTION__,
  1824. pri_protocol_name(priv->pri_protocol),
  1825. chan1);
  1826. return -EINVAL;
  1827. }
  1828. if(chan2 < 0 || chan2 > xpd->channels) {
  1829. XPD_NOTICE(xpd, "%s: %s CAS: Bad chan2 number (%d)\n",
  1830. __FUNCTION__,
  1831. pri_protocol_name(priv->pri_protocol),
  1832. chan2);
  1833. return -EINVAL;
  1834. }
  1835. XPD_DBG(SIGNAL, xpd, "RBS: RX: RS%02d (channel %2d, channel %2d): 0x%02X -> 0x%02X\n",
  1836. rsnum, chan1+1, chan2+1, priv->cas_rs_e[pos], data_low);
  1837. if(SPAN_REGISTERED(xpd)) {
  1838. dahdi_rbsbits(XPD_CHAN(xpd, chan1), val1);
  1839. dahdi_rbsbits(XPD_CHAN(xpd, chan2), val2);
  1840. }
  1841. priv->dchan_rx_counter++;
  1842. priv->cas_rs_e[pos] = data_low;
  1843. return 0;
  1844. }
  1845. static int decode_cas_t1(xpd_t *xpd, byte regnum, byte data_low)
  1846. {
  1847. struct PRI_priv_data *priv;
  1848. uint rsnum;
  1849. uint chan_per_reg;
  1850. uint width;
  1851. int i;
  1852. priv = xpd->priv;
  1853. BUG_ON(!priv->is_cas);
  1854. BUG_ON(priv->pri_protocol != PRI_PROTO_T1);
  1855. rsnum = regnum - REG_RS1_E;
  1856. if(rsnum < 0 || rsnum >= 12) {
  1857. XPD_ERR(xpd, "Bad rsnum=%d\n", rsnum);
  1858. return 0;
  1859. }
  1860. if(!priv->is_esf)
  1861. rsnum = rsnum % 6; /* 2 identical banks of 6 registers */
  1862. chan_per_reg = CHAN_PER_REGS(priv);
  1863. width = 8 / chan_per_reg;
  1864. XPD_DBG(SIGNAL, xpd,
  1865. "RBS: RX(%s,%d): RS%02d data_low=0x%02X\n",
  1866. (priv->is_esf) ? "esf" : "d4",
  1867. chan_per_reg,
  1868. rsnum+1, data_low);
  1869. for(i = 0; i < chan_per_reg; i++) {
  1870. uint rxsig = (data_low >> (i * width)) & BITMASK(width);
  1871. int pos;
  1872. struct dahdi_chan *chan;
  1873. if (!priv->is_esf)
  1874. rxsig <<= 2;
  1875. pos = rsnum * chan_per_reg + chan_per_reg - i - 1;
  1876. if(pos < 0 || pos >= xpd->channels) {
  1877. XPD_ERR(xpd, "%s: Bad pos=%d\n", __FUNCTION__, pos);
  1878. continue;
  1879. }
  1880. chan = XPD_CHAN(xpd, pos);
  1881. if(!chan) {
  1882. XPD_ERR(xpd, "%s: Null channel in pos=%d\n", __FUNCTION__, pos);
  1883. continue;
  1884. }
  1885. if(chan->rxsig != rxsig) {
  1886. LINE_DBG(SIGNAL, xpd, pos, "i=%d rxsig=0x%02X\n", i, rxsig);
  1887. dahdi_rbsbits(chan, rxsig);
  1888. }
  1889. }
  1890. priv->cas_rs_e[rsnum] = data_low;
  1891. return 0;
  1892. }
  1893. static void process_cas_dchan(xpd_t *xpd, byte regnum, byte data_low)
  1894. {
  1895. struct PRI_priv_data *priv;
  1896. priv = xpd->priv;
  1897. if(!priv->is_cas) {
  1898. static int rate_limit;
  1899. if((rate_limit++ % 10003) == 0)
  1900. XPD_NOTICE(xpd, "RBS: RX: not in CAS mode. Ignore.\n");
  1901. return;
  1902. }
  1903. if(!priv->layer1_up) {
  1904. static int rate_limit;
  1905. if((rate_limit++ % 10003) == 0)
  1906. XPD_DBG(SIGNAL, xpd, "RBS: RX: No layer1.\n");
  1907. }
  1908. if(!SPAN_REGISTERED(xpd)) {
  1909. static int rate_limit;
  1910. if((rate_limit++ % 10003) == 0)
  1911. XPD_DBG(SIGNAL, xpd, "RBS: RX: Span not registered. Ignore.\n");
  1912. return;
  1913. }
  1914. if(priv->pri_protocol == PRI_PROTO_E1) {
  1915. if(regnum == REG_RS1_E)
  1916. return; /* Time slot 0: Ignored for E1 */
  1917. if(regnum < REG_RS2_E) {
  1918. /* Should not happen, but harmless. Ignore */
  1919. if (regnum == REG_RS1_E)
  1920. return;
  1921. XPD_NOTICE(xpd,
  1922. "%s: received register 0x%X in protocol %s. Ignore\n",
  1923. __FUNCTION__, regnum, pri_protocol_name(priv->pri_protocol));
  1924. return;
  1925. }
  1926. if(decode_cas_e1(xpd, regnum, data_low) < 0)
  1927. return;
  1928. } else if(priv->pri_protocol == PRI_PROTO_T1) {
  1929. if(regnum > REG_RS12_E) {
  1930. XPD_NOTICE(xpd,
  1931. "%s: received register 0x%X in protocol %s. Ignore\n",
  1932. __FUNCTION__, regnum, pri_protocol_name(priv->pri_protocol));
  1933. return;
  1934. }
  1935. if(decode_cas_t1(xpd, regnum, data_low) < 0)
  1936. return;
  1937. } else {
  1938. XPD_NOTICE(xpd, "%s: protocol %s is not supported yet with CAS\n",
  1939. __FUNCTION__, pri_protocol_name(priv->pri_protocol));
  1940. }
  1941. priv->cas_replies++;
  1942. }
  1943. static int PRI_card_register_reply(xbus_t *xbus, xpd_t *xpd, reg_cmd_t *info)
  1944. {
  1945. unsigned long flags;
  1946. struct PRI_priv_data *priv;
  1947. struct xpd_addr addr;
  1948. xpd_t *orig_xpd;
  1949. byte regnum;
  1950. byte data_low;
  1951. /* Map UNIT + PORTNUM to XPD */
  1952. orig_xpd = xpd;
  1953. addr.unit = orig_xpd->addr.unit;
  1954. addr.subunit = info->portnum;
  1955. regnum = REG_FIELD(info, regnum);
  1956. data_low = REG_FIELD(info, data_low);
  1957. xpd = xpd_byaddr(xbus, addr.unit, addr.subunit);
  1958. if(!xpd) {
  1959. static int rate_limit;
  1960. if((rate_limit++ % 1003) < 5)
  1961. notify_bad_xpd(__FUNCTION__, xbus, addr , orig_xpd->xpdname);
  1962. return -EPROTO;
  1963. }
  1964. spin_lock_irqsave(&xpd->lock, flags);
  1965. priv = xpd->priv;
  1966. BUG_ON(!priv);
  1967. if(info->is_multibyte) {
  1968. XPD_NOTICE(xpd, "Got Multibyte: %d bytes, eoframe: %d\n",
  1969. info->bytes, info->eoframe);
  1970. goto end;
  1971. }
  1972. if(regnum == REG_FRS0 && !REG_FIELD(info, do_subreg))
  1973. layer1_state(xpd, data_low);
  1974. else if(regnum == REG_FRS1 && !REG_FIELD(info, do_subreg))
  1975. priv->reg_frs1 = data_low;
  1976. if(priv->is_cas && !REG_FIELD(info, do_subreg)) {
  1977. if(regnum >= REG_RS1_E && regnum <= REG_RS16_E) {
  1978. process_cas_dchan(xpd, regnum, data_low);
  1979. }
  1980. }
  1981. /* Update /proc info only if reply relate to the last slic read request */
  1982. if(
  1983. REG_FIELD(&xpd->requested_reply, regnum) == REG_FIELD(info, regnum) &&
  1984. REG_FIELD(&xpd->requested_reply, do_subreg) == REG_FIELD(info, do_subreg) &&
  1985. REG_FIELD(&xpd->requested_reply, subreg) == REG_FIELD(info, subreg)) {
  1986. xpd->last_reply = *info;
  1987. }
  1988. end:
  1989. spin_unlock_irqrestore(&xpd->lock, flags);
  1990. return 0;
  1991. }
  1992. static xproto_table_t PROTO_TABLE(PRI) = {
  1993. .owner = THIS_MODULE,
  1994. .entries = {
  1995. /* Table Card Opcode */
  1996. },
  1997. .name = "PRI", /* protocol name */
  1998. .ports_per_subunit = 1,
  1999. .type = XPD_TYPE_PRI,
  2000. .xops = {
  2001. .card_new = PRI_card_new,
  2002. .card_init = PRI_card_init,
  2003. .card_remove = PRI_card_remove,
  2004. .card_dahdi_preregistration = PRI_card_dahdi_preregistration,
  2005. .card_dahdi_postregistration = PRI_card_dahdi_postregistration,
  2006. .card_tick = PRI_card_tick,
  2007. .card_pcm_recompute = PRI_card_pcm_recompute,
  2008. .card_pcm_fromspan = PRI_card_pcm_fromspan,
  2009. .card_pcm_tospan = PRI_card_pcm_tospan,
  2010. .card_timing_priority = PRI_timing_priority,
  2011. .card_ioctl = PRI_card_ioctl,
  2012. .card_close = PRI_card_close,
  2013. .card_register_reply = PRI_card_register_reply,
  2014. .XPD_STATE = XPROTO_CALLER(PRI, XPD_STATE),
  2015. },
  2016. .packet_is_valid = pri_packet_is_valid,
  2017. .packet_dump = pri_packet_dump,
  2018. };
  2019. static bool pri_packet_is_valid(xpacket_t *pack)
  2020. {
  2021. const xproto_entry_t *xe = NULL;
  2022. // DBG(GENERAL, "\n");
  2023. xe = xproto_card_entry(&PROTO_TABLE(PRI), XPACKET_OP(pack));
  2024. return xe != NULL;
  2025. }
  2026. static void pri_packet_dump(const char *msg, xpacket_t *pack)
  2027. {
  2028. DBG(GENERAL, "%s\n", msg);
  2029. }
  2030. /*------------------------- REGISTER Handling --------------------------*/
  2031. #ifdef OLD_PROC
  2032. static int proc_pri_info_write(struct file *file, const char __user *buffer, unsigned long count, void *data)
  2033. {
  2034. xpd_t *xpd = data;
  2035. struct PRI_priv_data *priv;
  2036. char buf[MAX_PROC_WRITE];
  2037. char *p;
  2038. char *tok;
  2039. int ret = 0;
  2040. bool got_localloop = 0;
  2041. bool got_nolocalloop = 0;
  2042. bool got_e1 = 0;
  2043. bool got_t1 = 0;
  2044. bool got_j1 = 0;
  2045. if(!xpd)
  2046. return -ENODEV;
  2047. XPD_NOTICE(xpd, "%s: DEPRECATED: %s[%d] write to /proc interface instead of /sys\n",
  2048. __FUNCTION__, current->comm, current->tgid);
  2049. priv = xpd->priv;
  2050. if(count >= MAX_PROC_WRITE) { /* leave room for null */
  2051. XPD_ERR(xpd, "write too long (%ld)\n", count);
  2052. return -E2BIG;
  2053. }
  2054. if(copy_from_user(buf, buffer, count)) {
  2055. XPD_ERR(xpd, "Failed reading user data\n");
  2056. return -EFAULT;
  2057. }
  2058. buf[count] = '\0';
  2059. XPD_DBG(PROC, xpd, "PRI-SETUP: got %s\n", buf);
  2060. /*
  2061. * First parse. Act only of *everything* is good.
  2062. */
  2063. p = buf;
  2064. while((tok = strsep(&p, " \t\v\n")) != NULL) {
  2065. if(*tok == '\0')
  2066. continue;
  2067. XPD_DBG(PROC, xpd, "Got token='%s'\n", tok);
  2068. if(strnicmp(tok, "LOCALLOOP", 8) == 0)
  2069. got_localloop = 1;
  2070. else if(strnicmp(tok, "NOLOCALLOOP", 8) == 0)
  2071. got_nolocalloop = 1;
  2072. else if(strnicmp(tok, "E1", 2) == 0)
  2073. got_e1 = 1;
  2074. else if(strnicmp(tok, "T1", 2) == 0)
  2075. got_t1 = 1;
  2076. else if(strnicmp(tok, "J1", 2) == 0) {
  2077. got_j1 = 1;
  2078. } else {
  2079. XPD_NOTICE(xpd, "PRI-SETUP: unknown keyword: '%s'\n", tok);
  2080. return -EINVAL;
  2081. }
  2082. }
  2083. if(got_e1)
  2084. ret = set_pri_proto(xpd, PRI_PROTO_E1);
  2085. else if(got_t1)
  2086. ret = set_pri_proto(xpd, PRI_PROTO_T1);
  2087. else if(got_j1)
  2088. ret = set_pri_proto(xpd, PRI_PROTO_J1);
  2089. if(priv->pri_protocol == PRI_PROTO_0) {
  2090. XPD_ERR(xpd,
  2091. "Must set PRI protocol (E1/T1/J1) before setting other parameters\n");
  2092. return -EINVAL;
  2093. }
  2094. if(got_localloop)
  2095. ret = set_localloop(xpd, 1);
  2096. if(got_nolocalloop)
  2097. ret = set_localloop(xpd, 0);
  2098. return (ret) ? ret : count;
  2099. }
  2100. static int proc_pri_info_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  2101. {
  2102. int len = 0;
  2103. unsigned long flags;
  2104. xpd_t *xpd = data;
  2105. struct PRI_priv_data *priv;
  2106. int i;
  2107. DBG(PROC, "\n");
  2108. if(!xpd)
  2109. return -ENODEV;
  2110. XPD_NOTICE(xpd, "%s: DEPRECATED: %s[%d] read from /proc interface instead of /sys\n",
  2111. __FUNCTION__, current->comm, current->tgid);
  2112. spin_lock_irqsave(&xpd->lock, flags);
  2113. priv = xpd->priv;
  2114. BUG_ON(!priv);
  2115. len += sprintf(page + len, "PRI: %s %s%s (deflaw=%d, dchan=%d)\n",
  2116. (priv->clock_source) ? "MASTER" : "SLAVE",
  2117. pri_protocol_name(priv->pri_protocol),
  2118. (priv->local_loopback) ? " LOCALLOOP" : "",
  2119. priv->deflaw, priv->dchan_num);
  2120. len += sprintf(page + len, "%05d Layer1: ", priv->layer1_replies);
  2121. if(priv->poll_noreplies > 1)
  2122. len += sprintf(page + len, "No Replies [%d]\n",
  2123. priv->poll_noreplies);
  2124. else {
  2125. len += sprintf(page + len, "%s\n",
  2126. ((priv->layer1_up) ? "UP" : "DOWN"));
  2127. len += sprintf(page + len,
  2128. "Framer Status: FRS0=0x%02X, FRS1=0x%02X ALARMS:",
  2129. priv->reg_frs0, priv->reg_frs1);
  2130. if(priv->reg_frs0 & REG_FRS0_LOS)
  2131. len += sprintf(page + len, " RED");
  2132. if(priv->reg_frs0 & REG_FRS0_AIS)
  2133. len += sprintf(page + len, " BLUE");
  2134. if(priv->reg_frs0 & REG_FRS0_RRA)
  2135. len += sprintf(page + len, " YELLOW");
  2136. len += sprintf(page + len, "\n");
  2137. }
  2138. if(priv->is_cas) {
  2139. len += sprintf(page + len,
  2140. "CAS: replies=%d\n", priv->cas_replies);
  2141. len += sprintf(page + len, " CAS-TS: ");
  2142. for(i = 0; i < NUM_CAS_RS_E; i++) {
  2143. len += sprintf(page + len, " %02X", priv->cas_ts_e[i]);
  2144. }
  2145. len += sprintf(page + len, "\n");
  2146. len += sprintf(page + len, " CAS-RS: ");
  2147. for(i = 0; i < NUM_CAS_RS_E; i++) {
  2148. len += sprintf(page + len, " %02X", priv->cas_rs_e[i]);
  2149. }
  2150. len += sprintf(page + len, "\n");
  2151. }
  2152. len += sprintf(page + len, "D-Channel: TX=[%5d] (0x%02X) RX=[%5d] (0x%02X) ",
  2153. priv->dchan_tx_counter, priv->dchan_tx_sample,
  2154. priv->dchan_rx_counter, priv->dchan_rx_sample);
  2155. if(priv->dchan_alive) {
  2156. len += sprintf(page + len, "(alive %d K-ticks)\n",
  2157. priv->dchan_alive_ticks/1000);
  2158. } else {
  2159. len += sprintf(page + len, "(dead)\n");
  2160. }
  2161. for(i = 0; i < NUM_LEDS; i++)
  2162. len += sprintf(page + len, "LED #%d: %d\n", i, priv->ledstate[i]);
  2163. spin_unlock_irqrestore(&xpd->lock, flags);
  2164. if (len <= off+count)
  2165. *eof = 1;
  2166. *start = page + off;
  2167. len -= off;
  2168. if (len > count)
  2169. len = count;
  2170. if (len < 0)
  2171. len = 0;
  2172. return len;
  2173. }
  2174. #endif
  2175. /*------------------------- sysfs stuff --------------------------------*/
  2176. static DEVICE_ATTR_READER(pri_protocol_show, dev, buf)
  2177. {
  2178. xpd_t *xpd;
  2179. struct PRI_priv_data *priv;
  2180. unsigned long flags;
  2181. int len = 0;
  2182. BUG_ON(!dev);
  2183. xpd = dev_to_xpd(dev);
  2184. if(!xpd)
  2185. return -ENODEV;
  2186. priv = xpd->priv;
  2187. BUG_ON(!priv);
  2188. spin_lock_irqsave(&xpd->lock, flags);
  2189. len += sprintf(buf, "%s\n", pri_protocol_name(priv->pri_protocol));
  2190. spin_unlock_irqrestore(&xpd->lock, flags);
  2191. return len;
  2192. }
  2193. static DEVICE_ATTR_WRITER(pri_protocol_store, dev, buf, count)
  2194. {
  2195. xpd_t *xpd;
  2196. enum pri_protocol new_protocol = PRI_PROTO_0;
  2197. int i;
  2198. int ret;
  2199. BUG_ON(!dev);
  2200. xpd = dev_to_xpd(dev);
  2201. XPD_DBG(GENERAL, xpd, "%s\n", buf);
  2202. if(!xpd)
  2203. return -ENODEV;
  2204. if((i = strcspn(buf, " \r\n")) != 2) {
  2205. XPD_NOTICE(xpd,
  2206. "Protocol name '%s' has %d characters (should be 2). Ignored.\n",
  2207. buf, i);
  2208. return -EINVAL;
  2209. }
  2210. if(strnicmp(buf, "E1", 2) == 0)
  2211. new_protocol = PRI_PROTO_E1;
  2212. else if(strnicmp(buf, "T1", 2) == 0)
  2213. new_protocol = PRI_PROTO_T1;
  2214. else if(strnicmp(buf, "J1", 2) == 0)
  2215. new_protocol = PRI_PROTO_J1;
  2216. else {
  2217. XPD_NOTICE(xpd,
  2218. "Unknown PRI protocol '%s' (should be E1|T1|J1). Ignored.\n",
  2219. buf);
  2220. return -EINVAL;
  2221. }
  2222. ret = set_pri_proto(xpd, new_protocol);
  2223. return (ret < 0) ? ret : count;
  2224. }
  2225. static DEVICE_ATTR(pri_protocol, S_IRUGO | S_IWUSR, pri_protocol_show, pri_protocol_store);
  2226. static DEVICE_ATTR_READER(pri_localloop_show, dev, buf)
  2227. {
  2228. xpd_t *xpd;
  2229. struct PRI_priv_data *priv;
  2230. unsigned long flags;
  2231. int len = 0;
  2232. BUG_ON(!dev);
  2233. xpd = dev_to_xpd(dev);
  2234. if(!xpd)
  2235. return -ENODEV;
  2236. priv = xpd->priv;
  2237. BUG_ON(!priv);
  2238. spin_lock_irqsave(&xpd->lock, flags);
  2239. len += sprintf(buf, "%c\n",
  2240. (priv->local_loopback) ? 'Y' : 'N');
  2241. spin_unlock_irqrestore(&xpd->lock, flags);
  2242. return len;
  2243. }
  2244. static DEVICE_ATTR_WRITER(pri_localloop_store, dev, buf, count)
  2245. {
  2246. xpd_t *xpd;
  2247. bool ll = 0;
  2248. int i;
  2249. int ret;
  2250. BUG_ON(!dev);
  2251. xpd = dev_to_xpd(dev);
  2252. XPD_DBG(GENERAL, xpd, "%s\n", buf);
  2253. if(!xpd)
  2254. return -ENODEV;
  2255. if((i = strcspn(buf, " \r\n")) != 1) {
  2256. XPD_NOTICE(xpd,
  2257. "Value '%s' has %d characters (should be 1). Ignored.\n",
  2258. buf, i);
  2259. return -EINVAL;
  2260. }
  2261. if(strchr("1Yy", buf[0]) != NULL)
  2262. ll = 1;
  2263. else if(strchr("0Nn", buf[0]) != NULL)
  2264. ll = 0;
  2265. else {
  2266. XPD_NOTICE(xpd,
  2267. "Unknown value '%s' (should be [1Yy]|[0Nn]). Ignored.\n",
  2268. buf);
  2269. return -EINVAL;
  2270. }
  2271. ret = set_localloop(xpd, ll);
  2272. return (ret < 0) ? ret : count;
  2273. }
  2274. static DEVICE_ATTR(pri_localloop, S_IRUGO | S_IWUSR, pri_localloop_show, pri_localloop_store);
  2275. static DEVICE_ATTR_READER(pri_layer1_show, dev, buf)
  2276. {
  2277. xpd_t *xpd;
  2278. struct PRI_priv_data *priv;
  2279. unsigned long flags;
  2280. int len = 0;
  2281. BUG_ON(!dev);
  2282. xpd = dev_to_xpd(dev);
  2283. if(!xpd)
  2284. return -ENODEV;
  2285. priv = xpd->priv;
  2286. BUG_ON(!priv);
  2287. spin_lock_irqsave(&xpd->lock, flags);
  2288. if(priv->poll_noreplies > 1)
  2289. len += sprintf(buf + len, "Unknown[%d]", priv->poll_noreplies);
  2290. else
  2291. len += sprintf(buf + len, "%-10s", ((priv->layer1_up) ? "UP" : "DOWN"));
  2292. len += sprintf(buf + len, "%d\n", priv->layer1_replies);
  2293. spin_unlock_irqrestore(&xpd->lock, flags);
  2294. return len;
  2295. }
  2296. static DEVICE_ATTR(pri_layer1, S_IRUGO, pri_layer1_show, NULL);
  2297. static DEVICE_ATTR_READER(pri_alarms_show, dev, buf)
  2298. {
  2299. xpd_t *xpd;
  2300. struct PRI_priv_data *priv;
  2301. unsigned long flags;
  2302. int len = 0;
  2303. const static struct {
  2304. byte bits;
  2305. const char *name;
  2306. } alarm_types[] = {
  2307. { REG_FRS0_LOS, "RED" },
  2308. { REG_FRS0_AIS, "BLUE" },
  2309. { REG_FRS0_RRA, "YELLOW" },
  2310. };
  2311. BUG_ON(!dev);
  2312. xpd = dev_to_xpd(dev);
  2313. if(!xpd)
  2314. return -ENODEV;
  2315. priv = xpd->priv;
  2316. BUG_ON(!priv);
  2317. spin_lock_irqsave(&xpd->lock, flags);
  2318. if(priv->poll_noreplies > 1)
  2319. len += sprintf(buf + len, "Unknown[%d]", priv->poll_noreplies);
  2320. else {
  2321. int i;
  2322. for(i = 0; i < ARRAY_SIZE(alarm_types); i++) {
  2323. if(priv->reg_frs0 & alarm_types[i].bits)
  2324. len += sprintf(buf + len, "%s ", alarm_types[i].name);
  2325. }
  2326. }
  2327. len += sprintf(buf + len, "\n");
  2328. spin_unlock_irqrestore(&xpd->lock, flags);
  2329. return len;
  2330. }
  2331. static DEVICE_ATTR(pri_alarms, S_IRUGO, pri_alarms_show, NULL);
  2332. static DEVICE_ATTR_READER(pri_cas_show, dev, buf)
  2333. {
  2334. xpd_t *xpd;
  2335. struct PRI_priv_data *priv;
  2336. unsigned long flags;
  2337. int len = 0;
  2338. BUG_ON(!dev);
  2339. xpd = dev_to_xpd(dev);
  2340. if(!xpd)
  2341. return -ENODEV;
  2342. priv = xpd->priv;
  2343. BUG_ON(!priv);
  2344. spin_lock_irqsave(&xpd->lock, flags);
  2345. if(priv->is_cas) {
  2346. int i;
  2347. len += sprintf(buf + len,
  2348. "CAS: replies=%d\n", priv->cas_replies);
  2349. len += sprintf(buf + len, " CAS-TS: ");
  2350. for(i = 0; i < NUM_CAS_RS_E; i++) {
  2351. len += sprintf(buf + len, " %02X", priv->cas_ts_e[i]);
  2352. }
  2353. len += sprintf(buf + len, "\n");
  2354. len += sprintf(buf + len, " CAS-RS: ");
  2355. for(i = 0; i < NUM_CAS_RS_E; i++) {
  2356. len += sprintf(buf + len, " %02X", priv->cas_rs_e[i]);
  2357. }
  2358. len += sprintf(buf + len, "\n");
  2359. }
  2360. spin_unlock_irqrestore(&xpd->lock, flags);
  2361. return len;
  2362. }
  2363. static DEVICE_ATTR(pri_cas, S_IRUGO, pri_cas_show, NULL);
  2364. static DEVICE_ATTR_READER(pri_dchan_show, dev, buf)
  2365. {
  2366. xpd_t *xpd;
  2367. struct PRI_priv_data *priv;
  2368. unsigned long flags;
  2369. int len = 0;
  2370. BUG_ON(!dev);
  2371. xpd = dev_to_xpd(dev);
  2372. if(!xpd)
  2373. return -ENODEV;
  2374. priv = xpd->priv;
  2375. BUG_ON(!priv);
  2376. spin_lock_irqsave(&xpd->lock, flags);
  2377. len += sprintf(buf + len, "D-Channel: TX=[%5d] (0x%02X) RX=[%5d] (0x%02X) ",
  2378. priv->dchan_tx_counter, priv->dchan_tx_sample,
  2379. priv->dchan_rx_counter, priv->dchan_rx_sample);
  2380. if(priv->dchan_alive) {
  2381. len += sprintf(buf + len, "(alive %d K-ticks)\n",
  2382. priv->dchan_alive_ticks/1000);
  2383. } else {
  2384. len += sprintf(buf + len, "(dead)\n");
  2385. }
  2386. spin_unlock_irqrestore(&xpd->lock, flags);
  2387. return len;
  2388. }
  2389. static DEVICE_ATTR(pri_dchan, S_IRUGO, pri_dchan_show, NULL);
  2390. static DEVICE_ATTR_READER(pri_clocking_show, dev, buf)
  2391. {
  2392. xpd_t *xpd;
  2393. struct PRI_priv_data *priv;
  2394. unsigned long flags;
  2395. int len = 0;
  2396. BUG_ON(!dev);
  2397. xpd = dev_to_xpd(dev);
  2398. if(!xpd)
  2399. return -ENODEV;
  2400. priv = xpd->priv;
  2401. BUG_ON(!priv);
  2402. spin_lock_irqsave(&xpd->lock, flags);
  2403. len += sprintf(buf + len, "%s\n", (priv->clock_source) ? "MASTER" : "SLAVE");
  2404. spin_unlock_irqrestore(&xpd->lock, flags);
  2405. return len;
  2406. }
  2407. static DEVICE_ATTR(pri_clocking, S_IRUGO, pri_clocking_show, NULL);
  2408. static int pri_xpd_probe(struct device *dev)
  2409. {
  2410. xpd_t *xpd;
  2411. int ret = 0;
  2412. xpd = dev_to_xpd(dev);
  2413. /* Is it our device? */
  2414. if(xpd->type != XPD_TYPE_PRI) {
  2415. XPD_ERR(xpd, "drop suggestion for %s (%d)\n",
  2416. dev_name(dev), xpd->type);
  2417. return -EINVAL;
  2418. }
  2419. XPD_DBG(DEVICES, xpd, "SYSFS\n");
  2420. ret = device_create_file(dev, &dev_attr_pri_protocol);
  2421. if(ret) {
  2422. XPD_ERR(xpd, "%s: device_create_file(pri_protocol) failed: %d\n", __FUNCTION__, ret);
  2423. goto fail_pri_protocol;
  2424. }
  2425. ret = device_create_file(dev, &dev_attr_pri_localloop);
  2426. if(ret) {
  2427. XPD_ERR(xpd, "%s: device_create_file(pri_localloop) failed: %d\n", __FUNCTION__, ret);
  2428. goto fail_pri_localloop;
  2429. }
  2430. ret = device_create_file(dev, &dev_attr_pri_layer1);
  2431. if(ret) {
  2432. XPD_ERR(xpd, "%s: device_create_file(pri_layer1) failed: %d\n", __FUNCTION__, ret);
  2433. goto fail_pri_layer1;
  2434. }
  2435. ret = device_create_file(dev, &dev_attr_pri_alarms);
  2436. if(ret) {
  2437. XPD_ERR(xpd, "%s: device_create_file(pri_alarms) failed: %d\n", __FUNCTION__, ret);
  2438. goto fail_pri_alarms;
  2439. }
  2440. ret = device_create_file(dev, &dev_attr_pri_cas);
  2441. if(ret) {
  2442. XPD_ERR(xpd, "%s: device_create_file(pri_cas) failed: %d\n", __FUNCTION__, ret);
  2443. goto fail_pri_cas;
  2444. }
  2445. ret = device_create_file(dev, &dev_attr_pri_dchan);
  2446. if(ret) {
  2447. XPD_ERR(xpd, "%s: device_create_file(pri_dchan) failed: %d\n", __FUNCTION__, ret);
  2448. goto fail_pri_dchan;
  2449. }
  2450. ret = device_create_file(dev, &dev_attr_pri_clocking);
  2451. if(ret) {
  2452. XPD_ERR(xpd, "%s: device_create_file(pri_clocking) failed: %d\n", __FUNCTION__, ret);
  2453. goto fail_pri_clocking;
  2454. }
  2455. return 0;
  2456. fail_pri_clocking:
  2457. device_remove_file(dev, &dev_attr_pri_dchan);
  2458. fail_pri_dchan:
  2459. device_remove_file(dev, &dev_attr_pri_cas);
  2460. fail_pri_cas:
  2461. device_remove_file(dev, &dev_attr_pri_alarms);
  2462. fail_pri_alarms:
  2463. device_remove_file(dev, &dev_attr_pri_layer1);
  2464. fail_pri_layer1:
  2465. device_remove_file(dev, &dev_attr_pri_localloop);
  2466. fail_pri_localloop:
  2467. device_remove_file(dev, &dev_attr_pri_protocol);
  2468. fail_pri_protocol:
  2469. return ret;
  2470. }
  2471. static int pri_xpd_remove(struct device *dev)
  2472. {
  2473. xpd_t *xpd;
  2474. xpd = dev_to_xpd(dev);
  2475. XPD_DBG(DEVICES, xpd, "SYSFS\n");
  2476. device_remove_file(dev, &dev_attr_pri_clocking);
  2477. device_remove_file(dev, &dev_attr_pri_dchan);
  2478. device_remove_file(dev, &dev_attr_pri_cas);
  2479. device_remove_file(dev, &dev_attr_pri_alarms);
  2480. device_remove_file(dev, &dev_attr_pri_layer1);
  2481. device_remove_file(dev, &dev_attr_pri_localloop);
  2482. device_remove_file(dev, &dev_attr_pri_protocol);
  2483. return 0;
  2484. }
  2485. static struct xpd_driver pri_driver = {
  2486. .type = XPD_TYPE_PRI,
  2487. .driver = {
  2488. .name = "pri",
  2489. #ifndef OLD_HOTPLUG_SUPPORT
  2490. .owner = THIS_MODULE,
  2491. #endif
  2492. .probe = pri_xpd_probe,
  2493. .remove = pri_xpd_remove
  2494. }
  2495. };
  2496. static int __init card_pri_startup(void)
  2497. {
  2498. int ret;
  2499. if((ret = xpd_driver_register(&pri_driver.driver)) < 0)
  2500. return ret;
  2501. INFO("revision %s\n", XPP_VERSION);
  2502. #ifdef DAHDI_AUDIO_NOTIFY
  2503. INFO("FEATURE: WITH DAHDI_AUDIO_NOTIFY\n");
  2504. #else
  2505. INFO("FEATURE: WITHOUT DAHDI_AUDIO_NOTIFY\n");
  2506. #endif
  2507. xproto_register(&PROTO_TABLE(PRI));
  2508. return 0;
  2509. }
  2510. static void __exit card_pri_cleanup(void)
  2511. {
  2512. DBG(GENERAL, "\n");
  2513. xproto_unregister(&PROTO_TABLE(PRI));
  2514. xpd_driver_unregister(&pri_driver.driver);
  2515. }
  2516. MODULE_DESCRIPTION("XPP PRI Card Driver");
  2517. MODULE_AUTHOR("Oron Peled <oron@actcom.co.il>");
  2518. MODULE_LICENSE("GPL");
  2519. MODULE_VERSION(XPP_VERSION);
  2520. MODULE_ALIAS_XPD(XPD_TYPE_PRI);
  2521. module_init(card_pri_startup);
  2522. module_exit(card_pri_cleanup);