wct1xxp.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. /*
  2. * Digium, Inc. Wildcard T100P T1/PRI card Driver
  3. *
  4. * Written by Mark Spencer <markster@digium.com>
  5. * Matthew Fredrickson <creslin@digium.com>
  6. * William Meadows <wmeadows@digium.com>
  7. *
  8. * Copyright (C) 2001-2008, Digium, Inc.
  9. *
  10. * All rights reserved.
  11. *
  12. */
  13. /*
  14. * See http://www.asterisk.org for more information about
  15. * the Asterisk project. Please do not directly contact
  16. * any of the maintainers of this project for assistance;
  17. * the project provides a web site, mailing lists and IRC
  18. * channels for your use.
  19. *
  20. * This program is free software, distributed under the terms of
  21. * the GNU General Public License Version 2 as published by the
  22. * Free Software Foundation. See the LICENSE file included with
  23. * this program for more details.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/errno.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/usb.h>
  30. #include <linux/pci.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/slab.h>
  34. #include <dahdi/kernel.h>
  35. #define WC_MAX_CARDS 32
  36. /*
  37. #define TEST_REGS
  38. */
  39. /* Define to get more attention-grabbing but slightly more I/O using
  40. alarm status */
  41. #define FANCY_ALARM
  42. #define DELAY 0x0 /* 30 = 15 cycles, 10 = 8 cycles, 0 = 3 cycles */
  43. #define WC_CNTL 0x00
  44. #define WC_OPER 0x01
  45. #define WC_AUXC 0x02
  46. #define WC_AUXD 0x03
  47. #define WC_MASK0 0x04
  48. #define WC_MASK1 0x05
  49. #define WC_INTSTAT 0x06
  50. #define WC_DMAWS 0x08
  51. #define WC_DMAWI 0x0c
  52. #define WC_DMAWE 0x10
  53. #define WC_DMARS 0x18
  54. #define WC_DMARI 0x1c
  55. #define WC_DMARE 0x20
  56. #define WC_CURPOS 0x24
  57. #define WC_SERC 0x2d
  58. #define WC_FSCDELAY 0x2f
  59. #define WC_USERREG 0xc0
  60. #define WC_CLOCK 0x0
  61. #define WC_LEDTEST 0x1
  62. #define WC_VERSION 0x2
  63. /* Offset between transmit and receive */
  64. #define WC_OFFSET 4
  65. #define BIT_CS (1 << 7)
  66. #define BIT_ADDR (0xf << 3)
  67. #define BIT_LED0 (1 << 0)
  68. #define BIT_LED1 (1 << 1)
  69. #define BIT_TEST (1 << 2)
  70. static char *chips[] =
  71. {
  72. "DS2152",
  73. "DS21352",
  74. "DS21552",
  75. "Unknown Chip (3)",
  76. "DS2154",
  77. "DS21354",
  78. "DS21554",
  79. "Unknown Chip (7)",
  80. };
  81. static int chanmap_t1[] =
  82. { 2,1,0,
  83. 6,5,4,
  84. 10,9,8,
  85. 14,13,12,
  86. 18,17,16,
  87. 22,21,20,
  88. 26,25,24,
  89. 30,29,28 };
  90. static int chanmap_e1[] =
  91. { 2,1,0,
  92. 7,6,5,4,
  93. 11,10,9,8,
  94. 15,14,13,12,
  95. 19,18,17,16,
  96. 23,22,21,20,
  97. 27,26,25,24,
  98. 31,30,29,28 };
  99. #ifdef FANCY_ALARM
  100. static int altab[] = {
  101. 0, 0, 0, 1, 2, 3, 4, 6, 8, 9, 11, 13, 16, 18, 20, 22, 24, 25, 27, 28, 29, 30, 31, 31, 32, 31, 31, 30, 29, 28, 27, 25, 23, 22, 20, 18, 16, 13, 11, 9, 8, 6, 4, 3, 2, 1, 0, 0,
  102. };
  103. #endif
  104. struct t1xxp {
  105. struct pci_dev *dev;
  106. spinlock_t lock;
  107. int ise1;
  108. int num;
  109. /* Our offset for finding channel 1 */
  110. int offset;
  111. char *variety;
  112. unsigned int intcount;
  113. int usecount;
  114. int clocktimeout;
  115. int sync;
  116. int dead;
  117. int blinktimer;
  118. int alarmtimer;
  119. int loopupcnt;
  120. int loopdowncnt;
  121. int miss;
  122. int misslast;
  123. int *chanmap;
  124. #ifdef FANCY_ALARM
  125. int alarmpos;
  126. #endif
  127. unsigned char ledtestreg;
  128. unsigned char outbyte;
  129. unsigned long ioaddr;
  130. unsigned short canary;
  131. /* T1 signalling */
  132. unsigned char txsiga[3];
  133. unsigned char txsigb[3];
  134. dma_addr_t readdma;
  135. dma_addr_t writedma;
  136. volatile unsigned char *writechunk; /* Double-word aligned write memory */
  137. volatile unsigned char *readchunk; /* Double-word aligned read memory */
  138. unsigned char ec_chunk1[31][DAHDI_CHUNKSIZE];
  139. unsigned char ec_chunk2[31][DAHDI_CHUNKSIZE];
  140. unsigned char tempo[32];
  141. struct dahdi_span span; /* Span */
  142. struct dahdi_device *ddev;
  143. struct dahdi_chan *chans[31]; /* Channels */
  144. };
  145. #define CANARY 0xca1e
  146. static int debug = 0; /* doesnt do anything */
  147. static struct t1xxp *cards[WC_MAX_CARDS];
  148. static inline void start_alarm(struct t1xxp *wc)
  149. {
  150. #ifdef FANCY_ALARM
  151. wc->alarmpos = 0;
  152. #endif
  153. wc->blinktimer = 0;
  154. }
  155. static inline void stop_alarm(struct t1xxp *wc)
  156. {
  157. #ifdef FANCY_ALARM
  158. wc->alarmpos = 0;
  159. #endif
  160. wc->blinktimer = 0;
  161. }
  162. static inline void __select_framer(struct t1xxp *wc, int reg)
  163. {
  164. /* Top four bits of address from AUX 6-3 */
  165. wc->outbyte &= ~BIT_CS;
  166. wc->outbyte &= ~BIT_ADDR;
  167. wc->outbyte |= (reg & 0xf0) >> 1;
  168. outb(wc->outbyte, wc->ioaddr + WC_AUXD);
  169. }
  170. static inline void __select_control(struct t1xxp *wc)
  171. {
  172. if (!(wc->outbyte & BIT_CS)) {
  173. wc->outbyte |= BIT_CS;
  174. outb(wc->outbyte, wc->ioaddr + WC_AUXD);
  175. }
  176. }
  177. static int t1xxp_open(struct dahdi_chan *chan)
  178. {
  179. struct t1xxp *wc = chan->pvt;
  180. if (wc->dead)
  181. return -ENODEV;
  182. wc->usecount++;
  183. return 0;
  184. }
  185. static int __t1_get_reg(struct t1xxp *wc, int reg)
  186. {
  187. unsigned char res;
  188. __select_framer(wc, reg);
  189. /* Get value */
  190. res = inb(wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
  191. return res;
  192. }
  193. static int __t1_set_reg(struct t1xxp *wc, int reg, unsigned char val)
  194. {
  195. __select_framer(wc, reg);
  196. /* Send address */
  197. outb(val, wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
  198. return 0;
  199. }
  200. static int __control_set_reg(struct t1xxp *wc, int reg, unsigned char val)
  201. {
  202. __select_control(wc);
  203. outb(val, wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
  204. return 0;
  205. }
  206. static int control_set_reg(struct t1xxp *wc, int reg, unsigned char val)
  207. {
  208. unsigned long flags;
  209. int res;
  210. spin_lock_irqsave(&wc->lock, flags);
  211. res = __control_set_reg(wc, reg, val);
  212. spin_unlock_irqrestore(&wc->lock, flags);
  213. return res;
  214. }
  215. static int __control_get_reg(struct t1xxp *wc, int reg)
  216. {
  217. unsigned char res;
  218. /* The following makes UTTERLY no sense, but what was happening
  219. was that reads in some cases were not actually happening
  220. on the physical bus. Why, we dunno. But in debugging, we found
  221. that writing before reading (in this case to an unused position)
  222. seems to get rid of the problem */
  223. __control_set_reg(wc,3,0x69); /* do magic here */
  224. /* now get the read byte from the Xilinx part */
  225. res = inb(wc->ioaddr + WC_USERREG + ((reg & 0xf) << 2));
  226. return res;
  227. }
  228. static int control_get_reg(struct t1xxp *wc, int reg)
  229. {
  230. unsigned long flags;
  231. int res;
  232. spin_lock_irqsave(&wc->lock, flags);
  233. res = __control_get_reg(wc, reg);
  234. spin_unlock_irqrestore(&wc->lock, flags);
  235. return res;
  236. }
  237. static void t1xxp_release(struct t1xxp *wc)
  238. {
  239. unsigned int x;
  240. dahdi_unregister_device(wc->ddev);
  241. for (x = 0; x < (wc->ise1 ? 31 : 24); x++) {
  242. kfree(wc->chans[x]);
  243. }
  244. dahdi_free_device(wc->ddev);
  245. kfree(wc);
  246. printk(KERN_INFO "Freed a Wildcard\n");
  247. }
  248. static int t1xxp_close(struct dahdi_chan *chan)
  249. {
  250. struct t1xxp *wc = chan->pvt;
  251. wc->usecount--;
  252. /* If we're dead, release us now */
  253. if (!wc->usecount && wc->dead)
  254. t1xxp_release(wc);
  255. return 0;
  256. }
  257. static void t1xxp_enable_interrupts(struct t1xxp *wc)
  258. {
  259. /* Clear interrupts */
  260. outb(0xff, wc->ioaddr + WC_INTSTAT);
  261. /* Enable interrupts (we care about all of them) */
  262. outb(0x3c /* 0x3f */, wc->ioaddr + WC_MASK0);
  263. /* No external interrupts */
  264. outb(0x00, wc->ioaddr + WC_MASK1);
  265. }
  266. static void t1xxp_start_dma(struct t1xxp *wc)
  267. {
  268. /* Reset Master and TDM */
  269. outb(DELAY | 0x0f, wc->ioaddr + WC_CNTL);
  270. set_current_state(TASK_INTERRUPTIBLE);
  271. schedule_timeout(1);
  272. outb(DELAY | 0x01, wc->ioaddr + WC_CNTL);
  273. outb(0x01, wc->ioaddr + WC_OPER);
  274. if (debug) printk(KERN_DEBUG "Started DMA\n");
  275. }
  276. static void __t1xxp_stop_dma(struct t1xxp *wc)
  277. {
  278. outb(0x00, wc->ioaddr + WC_OPER);
  279. }
  280. static void __t1xxp_disable_interrupts(struct t1xxp *wc)
  281. {
  282. outb(0x00, wc->ioaddr + WC_MASK0);
  283. outb(0x00, wc->ioaddr + WC_MASK1);
  284. }
  285. static void __t1xxp_set_clear(struct t1xxp *wc)
  286. {
  287. /* Setup registers */
  288. int x,y;
  289. unsigned char b;
  290. /* No such thing under E1 */
  291. if (wc->ise1) {
  292. printk(KERN_NOTICE "Can't set clear mode on an E1!\n");
  293. return;
  294. }
  295. for (x=0;x<3;x++) {
  296. b = 0;
  297. for (y=0;y<8;y++)
  298. if (wc->chans[x * 8 + y]->sig & DAHDI_SIG_CLEAR)
  299. b |= (1 << y);
  300. __t1_set_reg(wc, 0x39 + x, b);
  301. }
  302. }
  303. static void t1xxp_t1_framer_start(struct t1xxp *wc)
  304. {
  305. int i;
  306. char *coding, *framing;
  307. unsigned long endjiffies;
  308. int alreadyrunning = wc->span.flags & DAHDI_FLAG_RUNNING;
  309. unsigned long flags;
  310. spin_lock_irqsave(&wc->lock, flags);
  311. /* Build up config */
  312. i = 0x20;
  313. if (wc->span.lineconfig & DAHDI_CONFIG_ESF) {
  314. coding = "ESF";
  315. i = 0x88;
  316. } else {
  317. coding = "SF";
  318. }
  319. if (wc->span.lineconfig & DAHDI_CONFIG_B8ZS) {
  320. framing = "B8ZS";
  321. i |= 0x44;
  322. } else {
  323. framing = "AMI";
  324. }
  325. __t1_set_reg(wc, 0x38, i);
  326. if (!(wc->span.lineconfig & DAHDI_CONFIG_ESF)) {
  327. /* 1c in FDL bit */
  328. __t1_set_reg(wc, 0x7e, 0x1c);
  329. } else {
  330. __t1_set_reg(wc, 0x7e, 0x00);
  331. }
  332. /* Set outgoing LBO */
  333. __t1_set_reg(wc, 0x7c, wc->span.txlevel << 5);
  334. printk(KERN_DEBUG "Using %s/%s coding/framing\n", coding, framing);
  335. if (!alreadyrunning) {
  336. /* Setup the clear channels */
  337. __t1xxp_set_clear(wc);
  338. /* Set LIRST bit to 1 */
  339. __t1_set_reg(wc, 0x0a, 0x80);
  340. spin_unlock_irqrestore(&wc->lock, flags);
  341. /* Wait 100ms to give plenty of time for reset */
  342. endjiffies = jiffies + 10;
  343. while(endjiffies < jiffies);
  344. spin_lock_irqsave(&wc->lock, flags);
  345. /* Reset LIRST bit and reset elastic stores */
  346. __t1_set_reg(wc, 0xa, 0x30);
  347. wc->span.flags |= DAHDI_FLAG_RUNNING;
  348. }
  349. spin_unlock_irqrestore(&wc->lock, flags);
  350. }
  351. static void t1xxp_e1_framer_start(struct t1xxp *wc)
  352. {
  353. int i;
  354. char *coding, *framing;
  355. unsigned long endjiffies;
  356. int alreadyrunning = wc->span.flags & DAHDI_FLAG_RUNNING;
  357. unsigned long flags;
  358. char *crcing = "";
  359. unsigned char ccr1, tcr1, tcr2;
  360. spin_lock_irqsave(&wc->lock, flags);
  361. /* Build up config */
  362. ccr1 = 0;
  363. tcr1 = 8;
  364. tcr2 = 0;
  365. if (wc->span.lineconfig & DAHDI_CONFIG_CCS) {
  366. coding = "CCS"; /* Receive CCS */
  367. ccr1 |= 8;
  368. } else {
  369. tcr1 |= 0x20;
  370. coding = "CAS";
  371. }
  372. if (wc->span.lineconfig & DAHDI_CONFIG_HDB3) {
  373. ccr1 |= 0x44; /* TX/RX HDB3 */
  374. framing = "HDB3";
  375. } else {
  376. framing = "AMI";
  377. }
  378. if (wc->span.lineconfig & DAHDI_CONFIG_CRC4) {
  379. ccr1 |= 0x11;
  380. tcr2 |= 0x02;
  381. crcing = " with CRC4";
  382. }
  383. __t1_set_reg(wc, 0x12, tcr1);
  384. __t1_set_reg(wc, 0x13, tcr2);
  385. __t1_set_reg(wc, 0x14, ccr1);
  386. __t1_set_reg(wc, 0x18, 0x20); /* 120 Ohm */
  387. #if 0 /* XXX Does LBO Matter? XXX */
  388. /* Set outgoing LBO */
  389. __t1_set_reg(wc, 0x7c, wc->span.txlevel << 5);
  390. #endif
  391. printk(KERN_DEBUG "Using %s/%s coding/framing%s 120 Ohms\n", coding, framing,crcing);
  392. if (!alreadyrunning) {
  393. __t1_set_reg(wc,0x1b,0x8a); /* CCR3: LIRST & TSCLKM */
  394. __t1_set_reg(wc,0x20,0x1b); /* TAFR */
  395. __t1_set_reg(wc,0x21,0x5f); /* TNAFR */
  396. __t1_set_reg(wc,0x40,0xb); /* TSR1 */
  397. for(i = 0x41; i <= 0x4f; i++) __t1_set_reg(wc,i,0x55);
  398. for(i = 0x22; i <= 0x25; i++) __t1_set_reg(wc,i,0xff);
  399. spin_unlock_irqrestore(&wc->lock, flags);
  400. /* Wait 100ms to give plenty of time for reset */
  401. endjiffies = jiffies + 10;
  402. while(endjiffies < jiffies);
  403. spin_lock_irqsave(&wc->lock, flags);
  404. __t1_set_reg(wc, 0x1b, 0x9a); /* Set ESR */
  405. __t1_set_reg(wc, 0x1b, 0x82); /* TSCLKM only now */
  406. /* Reset LIRST bit and reset elastic stores */
  407. wc->span.flags |= DAHDI_FLAG_RUNNING;
  408. }
  409. spin_unlock_irqrestore(&wc->lock, flags);
  410. }
  411. static int t1xxp_framer_sanity_check(struct t1xxp *wc)
  412. {
  413. int res;
  414. int chipid;
  415. unsigned long flags;
  416. int x;
  417. /* Sanity check */
  418. spin_lock_irqsave(&wc->lock, flags);
  419. for (x=0x0;x<192;x++)
  420. __t1_set_reg(wc, x, 0);
  421. res = __t1_get_reg(wc, 0x0f);
  422. res = __t1_get_reg(wc, 0x0f);
  423. chipid = ((res & 0x80) >> 5) | ((res & 0x30) >> 4);
  424. wc->ise1 = (res & 0x80) ? (1 << 4) : 0;
  425. spin_unlock_irqrestore(&wc->lock, flags);
  426. printk(KERN_DEBUG "Framer: %s, Revision: %d (%s)\n", chips[chipid], res & 0xf, wc->ise1 ? "E1" : "T1");
  427. return 0;
  428. }
  429. static int t1xxp_framer_hard_reset(struct t1xxp *wc)
  430. {
  431. int x;
  432. unsigned long flags;
  433. spin_lock_irqsave(&wc->lock, flags);
  434. /* Initialize all registers to 0 */
  435. for (x=0x0;x<192;x++)
  436. __t1_set_reg(wc, x, 0);
  437. if (wc->ise1) {
  438. /* Set LOTCMC (switch to RCLCK if TCLK fails) */
  439. __t1_set_reg(wc, 0x1a, 0x04);
  440. /* RSYNC is an input */
  441. __t1_set_reg(wc, 0x10, 0x20);
  442. /* Rx elastic store enabled, 2.048 Mhz (in theory) */
  443. __t1_set_reg(wc, 0x11, 0x06);
  444. /* TSYNC is an input, Tsis mode */
  445. __t1_set_reg(wc, 0x12, 0x08);
  446. /* Tx elastic store enabled, 2.048 Mhz (in theory) */
  447. __t1_set_reg(wc, 0x1b, 0x82);
  448. } else {
  449. /* Full-on sync required for T1 */
  450. __t1_set_reg(wc, 0x2b, 0x08);
  451. /* RSYNC is an input */
  452. __t1_set_reg(wc, 0x2c, 0x08);
  453. /* Enable tx RBS bits */
  454. __t1_set_reg(wc, 0x35, 0x10);
  455. /* TSYNC is output */
  456. __t1_set_reg(wc, 0x36, 0x04);
  457. /* Tx and Rx elastic store enabled, 2.048 Mhz (in theory) */
  458. __t1_set_reg(wc, 0x37, 0x9c);
  459. /* Setup Loopup / Loopdown codes */
  460. __t1_set_reg(wc, 0x12, 0x22);
  461. __t1_set_reg(wc, 0x14, 0x80);
  462. __t1_set_reg(wc, 0x15, 0x80);
  463. }
  464. spin_unlock_irqrestore(&wc->lock, flags);
  465. return 0;
  466. }
  467. static int t1xxp_rbsbits(struct dahdi_chan *chan, int bits)
  468. {
  469. struct t1xxp *wc = chan->pvt;
  470. unsigned long flags;
  471. int b,o;
  472. unsigned char mask;
  473. /* Byte offset */
  474. spin_lock_irqsave(&wc->lock, flags);
  475. if (wc->ise1) {
  476. if (chan->chanpos < 16) {
  477. mask = ((bits << 4) | wc->chans[chan->chanpos - 1 + 16]->txsig);
  478. __t1_set_reg(wc, 0x40 + chan->chanpos, mask);
  479. }
  480. else if (chan->chanpos > 16) {
  481. mask = (bits | (wc->chans[chan->chanpos - 1 - 16]->txsig << 4));
  482. __t1_set_reg(wc, 0x40 + chan->chanpos - 16, mask);
  483. }
  484. wc->chans[chan->chanpos - 1]->txsig = bits;
  485. } else {
  486. b = (chan->chanpos - 1) / 8;
  487. o = (chan->chanpos - 1) % 8;
  488. mask = (1 << o);
  489. if (bits & DAHDI_ABIT) {
  490. /* Set A-bit */
  491. wc->txsiga[b] |= mask;
  492. } else {
  493. /* Clear A-bit */
  494. wc->txsiga[b] &= ~mask;
  495. }
  496. if (bits & DAHDI_BBIT) {
  497. /* Set B-bit */
  498. wc->txsigb[b] |= mask;
  499. } else {
  500. wc->txsigb[b] &= ~mask;
  501. }
  502. /* Output new values */
  503. __t1_set_reg(wc, 0x70 + b, wc->txsiga[b]);
  504. __t1_set_reg(wc, 0x73 + b, wc->txsigb[b]);
  505. __t1_set_reg(wc, 0x76 + b, wc->txsiga[b]);
  506. __t1_set_reg(wc, 0x79 + b, wc->txsigb[b]);
  507. }
  508. spin_unlock_irqrestore(&wc->lock, flags);
  509. return 0;
  510. }
  511. static int t1xxp_ioctl(struct dahdi_chan *chan, unsigned int cmd, unsigned long data)
  512. {
  513. switch(cmd) {
  514. default:
  515. return -ENOTTY;
  516. }
  517. }
  518. static inline struct t1xxp *t1xxp_from_span(struct dahdi_span *span)
  519. {
  520. return container_of(span, struct t1xxp, span);
  521. }
  522. static int t1xxp_startup(struct file *file, struct dahdi_span *span)
  523. {
  524. struct t1xxp *wc = t1xxp_from_span(span);
  525. int i,alreadyrunning = span->flags & DAHDI_FLAG_RUNNING;
  526. /* initialize the start value for the entire chunk of last ec buffer */
  527. for(i = 0; i < span->channels; i++)
  528. {
  529. memset(wc->ec_chunk1[i],
  530. DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
  531. memset(wc->ec_chunk2[i],
  532. DAHDI_LIN2X(0,span->chans[i]),DAHDI_CHUNKSIZE);
  533. }
  534. /* Reset framer with proper parameters and start */
  535. if (wc->ise1)
  536. t1xxp_e1_framer_start(wc);
  537. else
  538. t1xxp_t1_framer_start(wc);
  539. printk(KERN_INFO "Calling startup (flags is %lu)\n", span->flags);
  540. if (!alreadyrunning) {
  541. /* Only if we're not already going */
  542. t1xxp_enable_interrupts(wc);
  543. t1xxp_start_dma(wc);
  544. span->flags |= DAHDI_FLAG_RUNNING;
  545. }
  546. return 0;
  547. }
  548. static int t1xxp_shutdown(struct dahdi_span *span)
  549. {
  550. struct t1xxp *wc = t1xxp_from_span(span);
  551. unsigned long flags;
  552. spin_lock_irqsave(&wc->lock, flags);
  553. __t1xxp_stop_dma(wc);
  554. __t1xxp_disable_interrupts(wc);
  555. span->flags &= ~DAHDI_FLAG_RUNNING;
  556. spin_unlock_irqrestore(&wc->lock, flags);
  557. t1xxp_framer_hard_reset(wc);
  558. return 0;
  559. }
  560. static int t1xxp_maint(struct dahdi_span *span, int cmd)
  561. {
  562. struct t1xxp *wc = t1xxp_from_span(span);
  563. int res = 0;
  564. unsigned long flags;
  565. spin_lock_irqsave(&wc->lock, flags);
  566. if (wc->ise1) {
  567. switch(cmd) {
  568. case DAHDI_MAINT_NONE:
  569. __t1_set_reg(wc,0xa8,0); /* no loops */
  570. break;
  571. case DAHDI_MAINT_LOCALLOOP:
  572. __t1_set_reg(wc,0xa8,0x40); /* local loop */
  573. break;
  574. case DAHDI_MAINT_REMOTELOOP:
  575. __t1_set_reg(wc,0xa8,0x80); /* remote loop */
  576. break;
  577. case DAHDI_MAINT_LOOPUP:
  578. case DAHDI_MAINT_LOOPDOWN:
  579. res = -ENOSYS;
  580. break;
  581. default:
  582. printk(KERN_NOTICE "wct1xxp/E1: Unknown maint command: %d\n", cmd);
  583. res = -EINVAL;
  584. break;
  585. }
  586. } else {
  587. switch(cmd) {
  588. case DAHDI_MAINT_NONE:
  589. __t1_set_reg(wc,0x19,0); /* no local loop */
  590. __t1_set_reg(wc,0x0a,0); /* no remote loop */
  591. __t1_set_reg(wc, 0x30, 0); /* stop sending loopup code*/
  592. break;
  593. case DAHDI_MAINT_LOCALLOOP:
  594. __t1_set_reg(wc,0x19,0x40); /* local loop */
  595. __t1_set_reg(wc,0x0a,0); /* no remote loop */
  596. break;
  597. case DAHDI_MAINT_REMOTELOOP:
  598. __t1_set_reg(wc,0x1e,0); /* no local loop */
  599. __t1_set_reg(wc,0x0a,0x40); /* remote loop */
  600. break;
  601. case DAHDI_MAINT_LOOPUP:
  602. __t1_set_reg(wc,0x30,2); /* send loopup code */
  603. __t1_set_reg(wc,0x12,0x22); /* send loopup code */
  604. __t1_set_reg(wc,0x13,0x80); /* send loopup code */
  605. break;
  606. case DAHDI_MAINT_LOOPDOWN:
  607. __t1_set_reg(wc,0x30,2); /* send loopdown code */
  608. __t1_set_reg(wc,0x12,0x62); /* send loopdown code */
  609. __t1_set_reg(wc,0x13,0x90); /* send loopdown code */
  610. break;
  611. default:
  612. printk(KERN_NOTICE "wct1xxp/T1: Unknown maint command: %d\n", cmd);
  613. res = -EINVAL;
  614. }
  615. }
  616. spin_unlock_irqrestore(&wc->lock, flags);
  617. return res;
  618. }
  619. static int
  620. t1xxp_chanconfig(struct file *file, struct dahdi_chan *chan, int sigtype)
  621. {
  622. struct t1xxp *wc = chan->pvt;
  623. unsigned long flags;
  624. int alreadyrunning = chan->span->flags & DAHDI_FLAG_RUNNING;
  625. spin_lock_irqsave(&wc->lock, flags);
  626. if (alreadyrunning && !wc->ise1)
  627. __t1xxp_set_clear(wc);
  628. spin_unlock_irqrestore(&wc->lock, flags);
  629. return 0;
  630. }
  631. static int
  632. t1xxp_spanconfig(struct file *file, struct dahdi_span *span,
  633. struct dahdi_lineconfig *lc)
  634. {
  635. struct t1xxp *wc = t1xxp_from_span(span);
  636. /* Do we want to SYNC on receive or not */
  637. wc->sync = (lc->sync) ? 1 : 0;
  638. /* If already running, apply changes immediately */
  639. if (span->flags & DAHDI_FLAG_RUNNING)
  640. return t1xxp_startup(file, span);
  641. return 0;
  642. }
  643. static const struct dahdi_span_ops t1xxp_span_ops = {
  644. .owner = THIS_MODULE,
  645. .spanconfig = t1xxp_spanconfig,
  646. .chanconfig = t1xxp_chanconfig,
  647. .startup = t1xxp_startup,
  648. .shutdown = t1xxp_shutdown,
  649. .rbsbits = t1xxp_rbsbits,
  650. .maint = t1xxp_maint,
  651. .open = t1xxp_open,
  652. .close = t1xxp_close,
  653. .ioctl = t1xxp_ioctl,
  654. };
  655. static int t1xxp_software_init(struct t1xxp *wc)
  656. {
  657. int x;
  658. /* Find position */
  659. for (x=0;x<WC_MAX_CARDS;x++) {
  660. if (!cards[x]) {
  661. cards[x] = wc;
  662. break;
  663. }
  664. }
  665. if (x >= WC_MAX_CARDS)
  666. return -1;
  667. wc->ddev = dahdi_create_device();
  668. wc->num = x;
  669. sprintf(wc->span.name, "WCT1/%d", wc->num);
  670. snprintf(wc->span.desc, sizeof(wc->span.desc) - 1, "%s Card %d", wc->variety, wc->num);
  671. wc->ddev->manufacturer = "Digium";
  672. wc->ddev->devicetype = wc->variety;
  673. wc->ddev->location = kasprintf(GFP_KERNEL, "PCI Bus %02d Slot %02d",
  674. wc->dev->bus->number,
  675. PCI_SLOT(wc->dev->devfn) + 1);
  676. if (!wc->ddev->location)
  677. return -ENOMEM;
  678. wc->span.chans = wc->chans;
  679. wc->span.flags = DAHDI_FLAG_RBS;
  680. if (wc->ise1) {
  681. wc->span.channels = 31;
  682. wc->span.deflaw = DAHDI_LAW_ALAW;
  683. wc->span.linecompat = DAHDI_CONFIG_HDB3 | DAHDI_CONFIG_CCS | DAHDI_CONFIG_CRC4;
  684. wc->span.spantype = SPANTYPE_DIGITAL_E1;
  685. } else {
  686. wc->span.channels = 24;
  687. wc->span.deflaw = DAHDI_LAW_MULAW;
  688. wc->span.linecompat = DAHDI_CONFIG_AMI | DAHDI_CONFIG_B8ZS | DAHDI_CONFIG_D4 | DAHDI_CONFIG_ESF;
  689. wc->span.spantype = SPANTYPE_DIGITAL_T1;
  690. }
  691. for (x=0;x<wc->span.channels;x++) {
  692. sprintf(wc->chans[x]->name, "WCT1/%d/%d", wc->num, x + 1);
  693. wc->chans[x]->sigcap = DAHDI_SIG_EM | DAHDI_SIG_CLEAR | DAHDI_SIG_EM_E1 |
  694. DAHDI_SIG_FXSLS | DAHDI_SIG_FXSGS |
  695. DAHDI_SIG_FXSKS | DAHDI_SIG_FXOLS | DAHDI_SIG_DACS_RBS |
  696. DAHDI_SIG_FXOGS | DAHDI_SIG_FXOKS | DAHDI_SIG_CAS | DAHDI_SIG_SF;
  697. wc->chans[x]->pvt = wc;
  698. wc->chans[x]->chanpos = x + 1;
  699. }
  700. wc->span.ops = &t1xxp_span_ops;
  701. list_add_tail(&wc->span.device_node, &wc->ddev->spans);
  702. if (dahdi_register_device(wc->ddev, &wc->dev->dev)) {
  703. printk(KERN_NOTICE "Unable to register span with DAHDI\n");
  704. return -1;
  705. }
  706. return 0;
  707. }
  708. static inline void __handle_leds(struct t1xxp *wc)
  709. {
  710. int oldreg;
  711. wc->blinktimer++;
  712. if (wc->span.alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE)) {
  713. /* Red/Blue alarm */
  714. #ifdef FANCY_ALARM
  715. if (wc->blinktimer == (altab[wc->alarmpos] >> 1)) {
  716. wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
  717. __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
  718. }
  719. if (wc->blinktimer == 0xf) {
  720. wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
  721. __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
  722. wc->blinktimer = -1;
  723. wc->alarmpos++;
  724. if (wc->alarmpos >= (sizeof(altab) / sizeof(altab[0])))
  725. wc->alarmpos = 0;
  726. }
  727. #else
  728. if (wc->blinktimer == 160) {
  729. wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
  730. __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
  731. } else if (wc->blinktimer == 480) {
  732. wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
  733. __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
  734. wc->blinktimer = 0;
  735. }
  736. #endif
  737. } else if (wc->span.alarms & DAHDI_ALARM_YELLOW) {
  738. /* Yellow Alarm */
  739. if (!(wc->blinktimer % 2))
  740. wc->ledtestreg = (wc->ledtestreg | BIT_LED1) & ~BIT_LED0;
  741. else
  742. wc->ledtestreg = (wc->ledtestreg | BIT_LED0) & ~BIT_LED1;
  743. __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
  744. } else {
  745. /* No Alarm */
  746. oldreg = wc->ledtestreg;
  747. if (wc->span.maintstat != DAHDI_MAINT_NONE)
  748. wc->ledtestreg |= BIT_TEST;
  749. else
  750. wc->ledtestreg &= ~BIT_TEST;
  751. if (wc->span.flags & DAHDI_FLAG_RUNNING)
  752. wc->ledtestreg = (wc->ledtestreg | BIT_LED0) & ~BIT_LED1;
  753. else
  754. wc->ledtestreg = wc->ledtestreg & ~(BIT_LED0 | BIT_LED1);
  755. if (oldreg != wc->ledtestreg)
  756. __control_set_reg(wc, WC_LEDTEST, wc->ledtestreg);
  757. }
  758. }
  759. static void t1xxp_transmitprep(struct t1xxp *wc, int ints)
  760. {
  761. volatile unsigned char *txbuf;
  762. int x,y;
  763. int pos;
  764. if (ints & 0x04 /* 0x01 */) {
  765. /* We just finished sending the first buffer, start filling it
  766. now */
  767. txbuf = wc->writechunk;
  768. } else {
  769. /* Just finished sending second buffer, fill it now */
  770. txbuf = wc->writechunk + 32 * DAHDI_CHUNKSIZE;
  771. }
  772. dahdi_transmit(&wc->span);
  773. for (x=0;x<wc->offset;x++)
  774. txbuf[x] = wc->tempo[x];
  775. for (y=0;y<DAHDI_CHUNKSIZE;y++) {
  776. for (x=0;x<wc->span.channels;x++) {
  777. pos = y * 32 + wc->chanmap[x] + wc->offset;
  778. /* Put channel number as outgoing data */
  779. if (pos < 32 * DAHDI_CHUNKSIZE)
  780. txbuf[pos] = wc->chans[x]->writechunk[y];
  781. else
  782. wc->tempo[pos - 32 * DAHDI_CHUNKSIZE] = wc->chans[x]->writechunk[y];
  783. }
  784. }
  785. }
  786. static void t1xxp_receiveprep(struct t1xxp *wc, int ints)
  787. {
  788. volatile unsigned char *rxbuf;
  789. volatile unsigned int *canary;
  790. int x;
  791. int y;
  792. unsigned int oldcan;
  793. if (ints & 0x04) {
  794. /* Just received first buffer */
  795. rxbuf = wc->readchunk;
  796. canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 64 - 4);
  797. } else {
  798. rxbuf = wc->readchunk + DAHDI_CHUNKSIZE * 32;
  799. canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 32 - 4);
  800. }
  801. oldcan = *canary;
  802. if (((oldcan & 0xffff0000) >> 16) != CANARY) {
  803. /* Check top part */
  804. if (debug) printk(KERN_DEBUG "Expecting top %04x, got %04x\n", CANARY, (oldcan & 0xffff0000) >> 16);
  805. wc->ddev->irqmisses++;
  806. } else if ((oldcan & 0xffff) != ((wc->canary - 1) & 0xffff)) {
  807. if (debug) printk(KERN_DEBUG "Expecting bottom %d, got %d\n", wc->canary - 1, oldcan & 0xffff);
  808. wc->ddev->irqmisses++;
  809. }
  810. for (y=0;y<DAHDI_CHUNKSIZE;y++) {
  811. for (x=0;x<wc->span.channels;x++) {
  812. /* XXX Optimize, remove * and + XXX */
  813. /* Must map received channels into appropriate data */
  814. wc->chans[x]->readchunk[y] =
  815. rxbuf[32 * y + ((wc->chanmap[x] + WC_OFFSET + wc->offset) & 0x1f)];
  816. }
  817. if (!wc->ise1) {
  818. for (x=3;x<32;x+=4) {
  819. if (rxbuf[32 * y + ((x + WC_OFFSET) & 0x1f)] == 0x7f) {
  820. if (wc->offset != (x-3)) {
  821. /* Resync */
  822. control_set_reg(wc, WC_CLOCK, 0x02 | wc->sync | wc->ise1);
  823. wc->clocktimeout = 100;
  824. #if 1
  825. if (debug) printk(KERN_DEBUG "T1: Lost our place, resyncing\n");
  826. #endif
  827. }
  828. }
  829. }
  830. } else {
  831. if (!wc->clocktimeout && !wc->span.alarms) {
  832. if ((rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)] & 0x7f) != 0x1b) {
  833. if (wc->miss) {
  834. if (debug) printk(KERN_DEBUG "Double miss (%d, %d)...\n", wc->misslast, rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)]);
  835. control_set_reg(wc, WC_CLOCK, 0x02 | wc->sync | wc->ise1);
  836. wc->clocktimeout = 100;
  837. } else {
  838. wc->miss = 1;
  839. wc->misslast = rxbuf[32 * y + ((3 + WC_OFFSET + wc->offset) & 0x1f)];
  840. }
  841. } else {
  842. wc->miss = 0;
  843. }
  844. } else {
  845. wc->miss = 0;
  846. }
  847. }
  848. }
  849. /* Store the next canary */
  850. canary = (unsigned int *)(rxbuf + DAHDI_CHUNKSIZE * 32 - 4);
  851. *canary = (wc->canary++) | (CANARY << 16);
  852. for (x=0;x<wc->span.channels;x++) {
  853. dahdi_ec_chunk(wc->chans[x], wc->chans[x]->readchunk, wc->ec_chunk2[x]);
  854. memcpy(wc->ec_chunk2[x],wc->ec_chunk1[x],DAHDI_CHUNKSIZE);
  855. memcpy(wc->ec_chunk1[x],wc->chans[x]->writechunk,DAHDI_CHUNKSIZE);
  856. }
  857. dahdi_receive(&wc->span);
  858. }
  859. static void t1xxp_check_sigbits(struct t1xxp *wc, int x)
  860. {
  861. int a,b,i,y,rxs;
  862. unsigned long flags;
  863. spin_lock_irqsave(&wc->lock, flags);
  864. if (wc->ise1) {
  865. /* Read 5 registers at a time, loading 10 channels at a time */
  866. for (i = (x * 5); i < (x * 5) + 5; i++) {
  867. a = __t1_get_reg(wc, 0x31 + i);
  868. /* Get high channel in low bits */
  869. rxs = (a & 0xf);
  870. if (!(wc->chans[i+16]->sig & DAHDI_SIG_CLEAR)) {
  871. if (wc->chans[i+16]->rxsig != rxs) {
  872. spin_unlock_irqrestore(&wc->lock, flags);
  873. dahdi_rbsbits(wc->chans[i+16], rxs);
  874. spin_lock_irqsave(&wc->lock, flags);
  875. }
  876. }
  877. rxs = (a >> 4) & 0xf;
  878. if (!(wc->chans[i]->sig & DAHDI_SIG_CLEAR)) {
  879. if (wc->chans[i]->rxsig != rxs) {
  880. spin_unlock_irqrestore(&wc->lock, flags);
  881. dahdi_rbsbits(wc->chans[i], rxs);
  882. spin_lock_irqsave(&wc->lock, flags);
  883. }
  884. }
  885. }
  886. } else {
  887. a = __t1_get_reg(wc, 0x60 + x);
  888. b = __t1_get_reg(wc, 0x63 + x);
  889. for (y=0;y<8;y++) {
  890. i = x * 8 + y;
  891. rxs = 0;
  892. if (a & (1 << y))
  893. rxs |= DAHDI_ABIT;
  894. if (b & (1 << y))
  895. rxs |= DAHDI_BBIT;
  896. if (!(wc->chans[i]->sig & DAHDI_SIG_CLEAR)) {
  897. if (wc->chans[i]->rxsig != rxs) {
  898. spin_unlock_irqrestore(&wc->lock, flags);
  899. dahdi_rbsbits(wc->chans[i], rxs);
  900. spin_lock_irqsave(&wc->lock, flags);
  901. }
  902. }
  903. }
  904. }
  905. spin_unlock_irqrestore(&wc->lock, flags);
  906. }
  907. static void t1xxp_check_alarms(struct t1xxp *wc)
  908. {
  909. unsigned char c,d;
  910. int alarms;
  911. int x,j;
  912. unsigned long flags;
  913. spin_lock_irqsave(&wc->lock, flags);
  914. if (wc->ise1) {
  915. __t1_set_reg(wc, 0x06, 0xff);
  916. c = __t1_get_reg(wc, 0x6);
  917. } else {
  918. /* Get RIR2 */
  919. c = __t1_get_reg(wc, 0x31);
  920. wc->span.rxlevel = c >> 6;
  921. /* Get status register s*/
  922. __t1_set_reg(wc, 0x20, 0xff);
  923. c = __t1_get_reg(wc, 0x20);
  924. }
  925. /* Assume no alarms */
  926. alarms = 0;
  927. /* And consider only carrier alarms */
  928. wc->span.alarms &= (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_NOTOPEN);
  929. if (wc->ise1) {
  930. /* XXX Implement me XXX */
  931. } else {
  932. /* Detect loopup code if we're not sending one */
  933. if ((!wc->span.mainttimer) && (c & 0x80)) {
  934. /* Loop-up code detected */
  935. if ((wc->loopupcnt++ > 80) && (wc->span.maintstat != DAHDI_MAINT_REMOTELOOP)) {
  936. __t1_set_reg(wc, 0x1e, 0); /* No local loop */
  937. __t1_set_reg(wc, 0x0a, 0x40); /* Remote Loop */
  938. wc->span.maintstat = DAHDI_MAINT_REMOTELOOP;
  939. }
  940. } else {
  941. wc->loopupcnt = 0;
  942. }
  943. /* Same for loopdown code */
  944. if ((!wc->span.mainttimer) && (c & 0x40)) {
  945. /* Loop-down code detected */
  946. if ((wc->loopdowncnt++ > 80) && (wc->span.maintstat == DAHDI_MAINT_REMOTELOOP)) {
  947. __t1_set_reg(wc, 0x1e, 0); /* No local loop */
  948. __t1_set_reg(wc, 0x0a, 0x0); /* No remote Loop */
  949. wc->span.maintstat = DAHDI_MAINT_NONE;
  950. }
  951. } else
  952. wc->loopdowncnt = 0;
  953. }
  954. if (wc->span.lineconfig & DAHDI_CONFIG_NOTOPEN) {
  955. for (x=0,j=0;x < wc->span.channels;x++)
  956. if ((wc->chans[x]->flags & DAHDI_FLAG_OPEN) ||
  957. dahdi_have_netdev(wc->chans[x]))
  958. j++;
  959. if (!j)
  960. alarms |= DAHDI_ALARM_NOTOPEN;
  961. }
  962. if (wc->ise1) {
  963. if (c & 0x9)
  964. alarms |= DAHDI_ALARM_RED;
  965. if (c & 0x2)
  966. alarms |= DAHDI_ALARM_BLUE;
  967. } else {
  968. /* Check actual alarm status */
  969. if (c & 0x3)
  970. alarms |= DAHDI_ALARM_RED;
  971. if (c & 0x8)
  972. alarms |= DAHDI_ALARM_BLUE;
  973. }
  974. /* Keep track of recovering */
  975. if ((!alarms) && wc->span.alarms)
  976. wc->alarmtimer = DAHDI_ALARMSETTLE_TIME;
  977. /* If receiving alarms, go into Yellow alarm state */
  978. if (alarms && (!wc->span.alarms)) {
  979. #if 0
  980. printk(KERN_DEBUG "Going into yellow alarm\n");
  981. #endif
  982. if (wc->ise1)
  983. __t1_set_reg(wc, 0x21, 0x7f);
  984. else
  985. __t1_set_reg(wc, 0x35, 0x11);
  986. }
  987. if (wc->span.alarms != alarms) {
  988. d = __control_get_reg(wc, WC_CLOCK);
  989. start_alarm(wc);
  990. if (!(alarms & (DAHDI_ALARM_RED | DAHDI_ALARM_BLUE | DAHDI_ALARM_LOOPBACK)) &&
  991. wc->sync) {
  992. /* Use the receive signalling */
  993. wc->span.syncsrc = wc->span.spanno;
  994. d |= 1;
  995. } else {
  996. wc->span.syncsrc = 0;
  997. d &= ~1;
  998. }
  999. __control_set_reg(wc, WC_CLOCK, d);
  1000. }
  1001. if (wc->alarmtimer)
  1002. alarms |= DAHDI_ALARM_RECOVER;
  1003. if (c & 0x4)
  1004. alarms |= DAHDI_ALARM_YELLOW;
  1005. wc->span.alarms = alarms;
  1006. spin_unlock_irqrestore(&wc->lock, flags);
  1007. dahdi_alarm_notify(&wc->span);
  1008. }
  1009. static void t1xxp_do_counters(struct t1xxp *wc)
  1010. {
  1011. unsigned long flags;
  1012. spin_lock_irqsave(&wc->lock, flags);
  1013. if (wc->alarmtimer) {
  1014. if (!--wc->alarmtimer) {
  1015. wc->span.alarms &= ~(DAHDI_ALARM_RECOVER);
  1016. /* Clear yellow alarm */
  1017. #if 0
  1018. printk(KERN_DEBUG "Coming out of alarm\n");
  1019. #endif
  1020. if (wc->ise1)
  1021. __t1_set_reg(wc, 0x21, 0x5f);
  1022. else
  1023. __t1_set_reg(wc, 0x35, 0x10);
  1024. spin_unlock_irqrestore(&wc->lock, flags);
  1025. dahdi_alarm_notify(&wc->span);
  1026. spin_lock_irqsave(&wc->lock, flags);
  1027. }
  1028. }
  1029. spin_unlock_irqrestore(&wc->lock, flags);
  1030. }
  1031. DAHDI_IRQ_HANDLER(t1xxp_interrupt)
  1032. {
  1033. struct t1xxp *wc = dev_id;
  1034. unsigned char ints;
  1035. unsigned long flags;
  1036. int x;
  1037. ints = inb(wc->ioaddr + WC_INTSTAT);
  1038. if (!ints)
  1039. return IRQ_NONE;
  1040. local_irq_save(flags);
  1041. outb(ints, wc->ioaddr + WC_INTSTAT);
  1042. if (!wc->intcount) {
  1043. if (debug) printk(KERN_DEBUG "Got interrupt: 0x%04x\n", ints);
  1044. }
  1045. wc->intcount++;
  1046. if (wc->clocktimeout && !--wc->clocktimeout)
  1047. control_set_reg(wc, WC_CLOCK, 0x00 | wc->sync | wc->ise1);
  1048. if (ints & 0x0f) {
  1049. t1xxp_receiveprep(wc, ints);
  1050. t1xxp_transmitprep(wc, ints);
  1051. }
  1052. spin_lock(&wc->lock);
  1053. #if 1
  1054. __handle_leds(wc);
  1055. #endif
  1056. spin_unlock(&wc->lock);
  1057. /* Count down timers */
  1058. t1xxp_do_counters(wc);
  1059. /* Do some things that we don't have to do very often */
  1060. x = wc->intcount & 15 /* 63 */;
  1061. switch(x) {
  1062. case 0:
  1063. case 1:
  1064. case 2:
  1065. t1xxp_check_sigbits(wc, x);
  1066. break;
  1067. case 4:
  1068. /* Check alarms 1/4 as frequently */
  1069. if (!(wc->intcount & 0x30))
  1070. t1xxp_check_alarms(wc);
  1071. break;
  1072. }
  1073. if (ints & 0x10)
  1074. printk(KERN_INFO "PCI Master abort\n");
  1075. if (ints & 0x20)
  1076. printk(KERN_INFO "PCI Target abort\n");
  1077. local_irq_restore(flags);
  1078. return IRQ_RETVAL(1);
  1079. }
  1080. static int t1xxp_hardware_init(struct t1xxp *wc)
  1081. {
  1082. /* Hardware PCI stuff */
  1083. /* Reset chip and registers */
  1084. outb(DELAY | 0x0e, wc->ioaddr + WC_CNTL);
  1085. /* Set all outputs to 0 */
  1086. outb(0x00, wc->ioaddr + WC_AUXD);
  1087. /* Set all to outputs except AUX1 (TDO). */
  1088. outb(0xfd, wc->ioaddr + WC_AUXC);
  1089. /* Configure the serial port: double clock, 20ns width, no inversion,
  1090. MSB first */
  1091. outb(0xc8, wc->ioaddr + WC_SERC);
  1092. /* Internally delay FSC by one */
  1093. outb(0x01, wc->ioaddr + WC_FSCDELAY);
  1094. /* Back to normal, with automatic DMA wrap around */
  1095. outb(DELAY | 0x01, wc->ioaddr + WC_CNTL);
  1096. /* Make sure serial port and DMA are out of reset */
  1097. outb(inb(wc->ioaddr + WC_CNTL) & 0xf9, WC_CNTL);
  1098. /* Setup DMA Addresses */
  1099. /* Start at writedma */
  1100. outl(wc->writedma, wc->ioaddr + WC_DMAWS); /* Write start */
  1101. /* First frame */
  1102. outl(wc->writedma + DAHDI_CHUNKSIZE * 32 - 4, wc->ioaddr + WC_DMAWI); /* Middle (interrupt) */
  1103. /* Second frame */
  1104. outl(wc->writedma + DAHDI_CHUNKSIZE * 32 * 2 - 4, wc->ioaddr + WC_DMAWE); /* End */
  1105. outl(wc->readdma, wc->ioaddr + WC_DMARS); /* Read start */
  1106. /* First frame */
  1107. outl(wc->readdma + DAHDI_CHUNKSIZE * 32 - 4, wc->ioaddr + WC_DMARI); /* Middle (interrupt) */
  1108. /* Second frame */
  1109. outl(wc->readdma + DAHDI_CHUNKSIZE * 32 * 2 - 4, wc->ioaddr + WC_DMARE); /* End */
  1110. if (debug) printk(KERN_DEBUG "Setting up DMA (write/read = %08lx/%08lx)\n", (long)wc->writedma, (long)wc->readdma);
  1111. /* Check out the controller */
  1112. if (debug) printk(KERN_DEBUG "Controller version: %02x\n", control_get_reg(wc, WC_VERSION));
  1113. control_set_reg(wc, WC_LEDTEST, 0x00);
  1114. /* Sanity check also determines e1 or t1 */
  1115. if (t1xxp_framer_sanity_check(wc))
  1116. return -1;
  1117. if (wc->ise1)
  1118. wc->chanmap = chanmap_e1;
  1119. else
  1120. wc->chanmap = chanmap_t1;
  1121. /* Setup clock appropriately */
  1122. control_set_reg(wc, WC_CLOCK, 0x02 | wc->sync | wc->ise1);
  1123. wc->clocktimeout = 100;
  1124. /* Reset the T1 and report */
  1125. t1xxp_framer_hard_reset(wc);
  1126. start_alarm(wc);
  1127. return 0;
  1128. }
  1129. static int __devinit t1xxp_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1130. {
  1131. struct t1xxp *wc;
  1132. unsigned int *canary;
  1133. unsigned int x;
  1134. if (pci_enable_device(pdev)) {
  1135. printk(KERN_ERR "%s: pci_enable_device failed\n", __FUNCTION__);
  1136. return -EIO;
  1137. }
  1138. if (!(wc = kmalloc(sizeof(*wc), GFP_KERNEL))) {
  1139. printk(KERN_ERR "%s: Failed allocation a wc\n", __FUNCTION__);
  1140. return -ENOMEM;
  1141. }
  1142. memset(wc, 0x0, sizeof(*wc));
  1143. spin_lock_init(&wc->lock);
  1144. wc->ioaddr = pci_resource_start(pdev, 0);
  1145. wc->dev = pdev;
  1146. wc->offset = 28; /* And you thought 42 was the answer */
  1147. wc->writechunk =
  1148. /* 32 channels, Double-buffer, Read/Write */
  1149. (unsigned char *)pci_alloc_consistent(pdev, DAHDI_MAX_CHUNKSIZE * 32 * 2 * 2, &wc->writedma);
  1150. if (!wc->writechunk) {
  1151. printk(KERN_NOTICE "wct1xxp: Unable to allocate DMA-able memory\n");
  1152. return -ENOMEM;
  1153. }
  1154. /* Read is after the whole write piece (in bytes) */
  1155. wc->readchunk = wc->writechunk + DAHDI_CHUNKSIZE * 32 * 2;
  1156. /* Same thing... */
  1157. wc->readdma = wc->writedma + DAHDI_CHUNKSIZE * 32 * 2;
  1158. /* Initialize Write/Buffers to all blank data */
  1159. memset((void *)wc->writechunk,0x00,DAHDI_MAX_CHUNKSIZE * 2 * 2 * 32);
  1160. /* Initialize canary */
  1161. canary = (unsigned int *)(wc->readchunk + DAHDI_CHUNKSIZE * 64 - 4);
  1162. *canary = (CANARY << 16) | (0xffff);
  1163. /* Enable bus mastering */
  1164. pci_set_master(pdev);
  1165. /* Keep track of which device we are */
  1166. pci_set_drvdata(pdev, wc);
  1167. if (request_irq(pdev->irq, t1xxp_interrupt, IRQF_SHARED, "t1xxp", wc)) {
  1168. printk(KERN_NOTICE "t1xxp: Unable to request IRQ %d\n", pdev->irq);
  1169. kfree(wc);
  1170. return -EIO;
  1171. }
  1172. /* Initialize hardware */
  1173. t1xxp_hardware_init(wc);
  1174. /* We now know which version of card we have */
  1175. if (wc->ise1) {
  1176. wc->variety = "Digium Wildcard E100P E1/PRA";
  1177. } else {
  1178. wc->variety = "Digium Wildcard T100P T1/PRI";
  1179. }
  1180. for (x = 0; x < (wc->ise1 ? 31 : 24); x++) {
  1181. if (!(wc->chans[x] = kmalloc(sizeof(*wc->chans[x]), GFP_KERNEL))) {
  1182. while (x) {
  1183. kfree(wc->chans[--x]);
  1184. }
  1185. kfree(wc);
  1186. return -ENOMEM;
  1187. }
  1188. memset(wc->chans[x], 0, sizeof(*wc->chans[x]));
  1189. }
  1190. /* Misc. software stuff */
  1191. t1xxp_software_init(wc);
  1192. printk(KERN_INFO "Found a Wildcard: %s\n", wc->variety);
  1193. return 0;
  1194. }
  1195. static void t1xxp_stop_stuff(struct t1xxp *wc)
  1196. {
  1197. /* Kill clock */
  1198. control_set_reg(wc, WC_CLOCK, 0);
  1199. /* Turn off LED's */
  1200. control_set_reg(wc, WC_LEDTEST, 0);
  1201. /* Reset the T1 */
  1202. t1xxp_framer_hard_reset(wc);
  1203. }
  1204. static void __devexit t1xxp_remove_one(struct pci_dev *pdev)
  1205. {
  1206. struct t1xxp *wc = pci_get_drvdata(pdev);
  1207. if (wc) {
  1208. /* Stop any DMA */
  1209. __t1xxp_stop_dma(wc);
  1210. /* In case hardware is still there */
  1211. __t1xxp_disable_interrupts(wc);
  1212. t1xxp_stop_stuff(wc);
  1213. /* Immediately free resources */
  1214. pci_free_consistent(pdev, DAHDI_MAX_CHUNKSIZE * 2 * 2 * 32 * 4, (void *)wc->writechunk, wc->writedma);
  1215. free_irq(pdev->irq, wc);
  1216. /* Reset PCI chip and registers */
  1217. outb(DELAY | 0x0e, wc->ioaddr + WC_CNTL);
  1218. /* Release span, possibly delayed */
  1219. if (!wc->usecount)
  1220. t1xxp_release(wc);
  1221. else
  1222. wc->dead = 1;
  1223. }
  1224. }
  1225. static DEFINE_PCI_DEVICE_TABLE(t1xxp_pci_tbl) = {
  1226. { 0xe159, 0x0001, 0x6159, PCI_ANY_ID, 0, 0, (unsigned long) "Digium Wildcard T100P T1/PRI or E100P E1/PRA Board" },
  1227. { 0 }
  1228. };
  1229. MODULE_DEVICE_TABLE(pci,t1xxp_pci_tbl);
  1230. static struct pci_driver t1xxp_driver = {
  1231. .name = "t1xxp",
  1232. .probe = t1xxp_init_one,
  1233. .remove = __devexit_p(t1xxp_remove_one),
  1234. .suspend = NULL,
  1235. .resume = NULL,
  1236. . id_table = t1xxp_pci_tbl,
  1237. };
  1238. static int __init t1xxp_init(void)
  1239. {
  1240. int res;
  1241. res = dahdi_pci_module(&t1xxp_driver);
  1242. if (res)
  1243. return -ENODEV;
  1244. return 0;
  1245. }
  1246. static void __exit t1xxp_cleanup(void)
  1247. {
  1248. pci_unregister_driver(&t1xxp_driver);
  1249. }
  1250. module_param(debug, int, 0600);
  1251. MODULE_DESCRIPTION("Wildcard T100P/E100P Driver");
  1252. MODULE_AUTHOR("Mark Spencer <markster@digium.com>");
  1253. MODULE_LICENSE("GPL v2");
  1254. module_init(t1xxp_init);
  1255. module_exit(t1xxp_cleanup);