ipacx.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /*
  2. *
  3. * IPACX specific routines
  4. *
  5. * Author Joerg Petersohn
  6. * Derived from hisax_isac.c, isac.c, hscx.c and others
  7. *
  8. * This software may be used and distributed according to the terms
  9. * of the GNU General Public License, incorporated herein by reference.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include "hisax_if.h"
  16. #include "hisax.h"
  17. #include "isdnl1.h"
  18. #include "ipacx.h"
  19. #define DBUSY_TIMER_VALUE 80
  20. #define TIMER3_VALUE 7000
  21. #define MAX_DFRAME_LEN_L1 300
  22. #define B_FIFO_SIZE 64
  23. #define D_FIFO_SIZE 32
  24. // ipacx interrupt mask values
  25. #define _MASK_IMASK 0x2E // global mask
  26. #define _MASKB_IMASK 0x0B
  27. #define _MASKD_IMASK 0x03 // all on
  28. //----------------------------------------------------------
  29. // local function declarations
  30. //----------------------------------------------------------
  31. static void ph_command(struct IsdnCardState *cs, unsigned int command);
  32. static inline void cic_int(struct IsdnCardState *cs);
  33. static void dch_l2l1(struct PStack *st, int pr, void *arg);
  34. static void dbusy_timer_handler(struct IsdnCardState *cs);
  35. static void dch_empty_fifo(struct IsdnCardState *cs, int count);
  36. static void dch_fill_fifo(struct IsdnCardState *cs);
  37. static inline void dch_int(struct IsdnCardState *cs);
  38. static void dch_setstack(struct PStack *st, struct IsdnCardState *cs);
  39. static void dch_init(struct IsdnCardState *cs);
  40. static void bch_l2l1(struct PStack *st, int pr, void *arg);
  41. static void bch_empty_fifo(struct BCState *bcs, int count);
  42. static void bch_fill_fifo(struct BCState *bcs);
  43. static void bch_int(struct IsdnCardState *cs, u_char hscx);
  44. static void bch_mode(struct BCState *bcs, int mode, int bc);
  45. static void bch_close_state(struct BCState *bcs);
  46. static int bch_open_state(struct IsdnCardState *cs, struct BCState *bcs);
  47. static int bch_setstack(struct PStack *st, struct BCState *bcs);
  48. static void bch_init(struct IsdnCardState *cs, int hscx);
  49. static void clear_pending_ints(struct IsdnCardState *cs);
  50. //----------------------------------------------------------
  51. // Issue Layer 1 command to chip
  52. //----------------------------------------------------------
  53. static void
  54. ph_command(struct IsdnCardState *cs, unsigned int command)
  55. {
  56. if (cs->debug & L1_DEB_ISAC)
  57. debugl1(cs, "ph_command (%#x) in (%#x)", command,
  58. cs->dc.isac.ph_state);
  59. //###################################
  60. // printk(KERN_INFO "ph_command (%#x)\n", command);
  61. //###################################
  62. cs->writeisac(cs, IPACX_CIX0, (command << 4) | 0x0E);
  63. }
  64. //----------------------------------------------------------
  65. // Transceiver interrupt handler
  66. //----------------------------------------------------------
  67. static inline void
  68. cic_int(struct IsdnCardState *cs)
  69. {
  70. u_char event;
  71. event = cs->readisac(cs, IPACX_CIR0) >> 4;
  72. if (cs->debug & L1_DEB_ISAC) debugl1(cs, "cic_int(event=%#x)", event);
  73. //#########################################
  74. // printk(KERN_INFO "cic_int(%x)\n", event);
  75. //#########################################
  76. cs->dc.isac.ph_state = event;
  77. schedule_event(cs, D_L1STATECHANGE);
  78. }
  79. //==========================================================
  80. // D channel functions
  81. //==========================================================
  82. //----------------------------------------------------------
  83. // Command entry point
  84. //----------------------------------------------------------
  85. static void
  86. dch_l2l1(struct PStack *st, int pr, void *arg)
  87. {
  88. struct IsdnCardState *cs = (struct IsdnCardState *) st->l1.hardware;
  89. struct sk_buff *skb = arg;
  90. u_char cda1_cr;
  91. switch (pr) {
  92. case (PH_DATA | REQUEST):
  93. if (cs->debug & DEB_DLOG_HEX) LogFrame(cs, skb->data, skb->len);
  94. if (cs->debug & DEB_DLOG_VERBOSE) dlogframe(cs, skb, 0);
  95. if (cs->tx_skb) {
  96. skb_queue_tail(&cs->sq, skb);
  97. #ifdef L2FRAME_DEBUG
  98. if (cs->debug & L1_DEB_LAPD) Logl2Frame(cs, skb, "PH_DATA Queued", 0);
  99. #endif
  100. } else {
  101. cs->tx_skb = skb;
  102. cs->tx_cnt = 0;
  103. #ifdef L2FRAME_DEBUG
  104. if (cs->debug & L1_DEB_LAPD) Logl2Frame(cs, skb, "PH_DATA", 0);
  105. #endif
  106. dch_fill_fifo(cs);
  107. }
  108. break;
  109. case (PH_PULL | INDICATION):
  110. if (cs->tx_skb) {
  111. if (cs->debug & L1_DEB_WARN)
  112. debugl1(cs, " l2l1 tx_skb exist this shouldn't happen");
  113. skb_queue_tail(&cs->sq, skb);
  114. break;
  115. }
  116. if (cs->debug & DEB_DLOG_HEX) LogFrame(cs, skb->data, skb->len);
  117. if (cs->debug & DEB_DLOG_VERBOSE) dlogframe(cs, skb, 0);
  118. cs->tx_skb = skb;
  119. cs->tx_cnt = 0;
  120. #ifdef L2FRAME_DEBUG
  121. if (cs->debug & L1_DEB_LAPD) Logl2Frame(cs, skb, "PH_DATA_PULLED", 0);
  122. #endif
  123. dch_fill_fifo(cs);
  124. break;
  125. case (PH_PULL | REQUEST):
  126. #ifdef L2FRAME_DEBUG
  127. if (cs->debug & L1_DEB_LAPD) debugl1(cs, "-> PH_REQUEST_PULL");
  128. #endif
  129. if (!cs->tx_skb) {
  130. clear_bit(FLG_L1_PULL_REQ, &st->l1.Flags);
  131. st->l1.l1l2(st, PH_PULL | CONFIRM, NULL);
  132. } else
  133. set_bit(FLG_L1_PULL_REQ, &st->l1.Flags);
  134. break;
  135. case (HW_RESET | REQUEST):
  136. case (HW_ENABLE | REQUEST):
  137. if ((cs->dc.isac.ph_state == IPACX_IND_RES) ||
  138. (cs->dc.isac.ph_state == IPACX_IND_DR) ||
  139. (cs->dc.isac.ph_state == IPACX_IND_DC))
  140. ph_command(cs, IPACX_CMD_TIM);
  141. else
  142. ph_command(cs, IPACX_CMD_RES);
  143. break;
  144. case (HW_INFO3 | REQUEST):
  145. ph_command(cs, IPACX_CMD_AR8);
  146. break;
  147. case (HW_TESTLOOP | REQUEST):
  148. cs->writeisac(cs, IPACX_CDA_TSDP10, 0x80); // Timeslot 0 is B1
  149. cs->writeisac(cs, IPACX_CDA_TSDP11, 0x81); // Timeslot 0 is B1
  150. cda1_cr = cs->readisac(cs, IPACX_CDA1_CR);
  151. (void) cs->readisac(cs, IPACX_CDA2_CR);
  152. if ((long)arg & 1) { // loop B1
  153. cs->writeisac(cs, IPACX_CDA1_CR, cda1_cr | 0x0a);
  154. }
  155. else { // B1 off
  156. cs->writeisac(cs, IPACX_CDA1_CR, cda1_cr & ~0x0a);
  157. }
  158. if ((long)arg & 2) { // loop B2
  159. cs->writeisac(cs, IPACX_CDA1_CR, cda1_cr | 0x14);
  160. }
  161. else { // B2 off
  162. cs->writeisac(cs, IPACX_CDA1_CR, cda1_cr & ~0x14);
  163. }
  164. break;
  165. case (HW_DEACTIVATE | RESPONSE):
  166. skb_queue_purge(&cs->rq);
  167. skb_queue_purge(&cs->sq);
  168. if (cs->tx_skb) {
  169. dev_kfree_skb_any(cs->tx_skb);
  170. cs->tx_skb = NULL;
  171. }
  172. if (test_and_clear_bit(FLG_DBUSY_TIMER, &cs->HW_Flags))
  173. del_timer(&cs->dbusytimer);
  174. break;
  175. default:
  176. if (cs->debug & L1_DEB_WARN) debugl1(cs, "dch_l2l1 unknown %04x", pr);
  177. break;
  178. }
  179. }
  180. //----------------------------------------------------------
  181. //----------------------------------------------------------
  182. static void
  183. dbusy_timer_handler(struct IsdnCardState *cs)
  184. {
  185. struct PStack *st;
  186. int rbchd, stard;
  187. if (test_bit(FLG_DBUSY_TIMER, &cs->HW_Flags)) {
  188. rbchd = cs->readisac(cs, IPACX_RBCHD);
  189. stard = cs->readisac(cs, IPACX_STARD);
  190. if (cs->debug)
  191. debugl1(cs, "D-Channel Busy RBCHD %02x STARD %02x", rbchd, stard);
  192. if (!(stard & 0x40)) { // D-Channel Busy
  193. set_bit(FLG_L1_DBUSY, &cs->HW_Flags);
  194. for (st = cs->stlist; st; st = st->next) {
  195. st->l1.l1l2(st, PH_PAUSE | INDICATION, NULL); // flow control on
  196. }
  197. } else {
  198. // seems we lost an interrupt; reset transceiver */
  199. clear_bit(FLG_DBUSY_TIMER, &cs->HW_Flags);
  200. if (cs->tx_skb) {
  201. dev_kfree_skb_any(cs->tx_skb);
  202. cs->tx_cnt = 0;
  203. cs->tx_skb = NULL;
  204. } else {
  205. printk(KERN_WARNING "HiSax: ISAC D-Channel Busy no skb\n");
  206. debugl1(cs, "D-Channel Busy no skb");
  207. }
  208. cs->writeisac(cs, IPACX_CMDRD, 0x01); // Tx reset, generates XPR
  209. }
  210. }
  211. }
  212. //----------------------------------------------------------
  213. // Fill buffer from receive FIFO
  214. //----------------------------------------------------------
  215. static void
  216. dch_empty_fifo(struct IsdnCardState *cs, int count)
  217. {
  218. u_char *ptr;
  219. if ((cs->debug & L1_DEB_ISAC) && !(cs->debug & L1_DEB_ISAC_FIFO))
  220. debugl1(cs, "dch_empty_fifo()");
  221. // message too large, remove
  222. if ((cs->rcvidx + count) >= MAX_DFRAME_LEN_L1) {
  223. if (cs->debug & L1_DEB_WARN)
  224. debugl1(cs, "dch_empty_fifo() incoming message too large");
  225. cs->writeisac(cs, IPACX_CMDRD, 0x80); // RMC
  226. cs->rcvidx = 0;
  227. return;
  228. }
  229. ptr = cs->rcvbuf + cs->rcvidx;
  230. cs->rcvidx += count;
  231. cs->readisacfifo(cs, ptr, count);
  232. cs->writeisac(cs, IPACX_CMDRD, 0x80); // RMC
  233. if (cs->debug & L1_DEB_ISAC_FIFO) {
  234. char *t = cs->dlog;
  235. t += sprintf(t, "dch_empty_fifo() cnt %d", count);
  236. QuickHex(t, ptr, count);
  237. debugl1(cs, "%s", cs->dlog);
  238. }
  239. }
  240. //----------------------------------------------------------
  241. // Fill transmit FIFO
  242. //----------------------------------------------------------
  243. static void
  244. dch_fill_fifo(struct IsdnCardState *cs)
  245. {
  246. int count;
  247. u_char cmd, *ptr;
  248. if ((cs->debug & L1_DEB_ISAC) && !(cs->debug & L1_DEB_ISAC_FIFO))
  249. debugl1(cs, "dch_fill_fifo()");
  250. if (!cs->tx_skb) return;
  251. count = cs->tx_skb->len;
  252. if (count <= 0) return;
  253. if (count > D_FIFO_SIZE) {
  254. count = D_FIFO_SIZE;
  255. cmd = 0x08; // XTF
  256. } else {
  257. cmd = 0x0A; // XTF | XME
  258. }
  259. ptr = cs->tx_skb->data;
  260. skb_pull(cs->tx_skb, count);
  261. cs->tx_cnt += count;
  262. cs->writeisacfifo(cs, ptr, count);
  263. cs->writeisac(cs, IPACX_CMDRD, cmd);
  264. // set timeout for transmission contol
  265. if (test_and_set_bit(FLG_DBUSY_TIMER, &cs->HW_Flags)) {
  266. debugl1(cs, "dch_fill_fifo dbusytimer running");
  267. del_timer(&cs->dbusytimer);
  268. }
  269. init_timer(&cs->dbusytimer);
  270. cs->dbusytimer.expires = jiffies + ((DBUSY_TIMER_VALUE * HZ)/1000);
  271. add_timer(&cs->dbusytimer);
  272. if (cs->debug & L1_DEB_ISAC_FIFO) {
  273. char *t = cs->dlog;
  274. t += sprintf(t, "dch_fill_fifo() cnt %d", count);
  275. QuickHex(t, ptr, count);
  276. debugl1(cs, "%s", cs->dlog);
  277. }
  278. }
  279. //----------------------------------------------------------
  280. // D channel interrupt handler
  281. //----------------------------------------------------------
  282. static inline void
  283. dch_int(struct IsdnCardState *cs)
  284. {
  285. struct sk_buff *skb;
  286. u_char istad, rstad;
  287. int count;
  288. istad = cs->readisac(cs, IPACX_ISTAD);
  289. //##############################################
  290. // printk(KERN_WARNING "dch_int(istad=%02x)\n", istad);
  291. //##############################################
  292. if (istad & 0x80) { // RME
  293. rstad = cs->readisac(cs, IPACX_RSTAD);
  294. if ((rstad & 0xf0) != 0xa0) { // !(VFR && !RDO && CRC && !RAB)
  295. if (!(rstad & 0x80))
  296. if (cs->debug & L1_DEB_WARN)
  297. debugl1(cs, "dch_int(): invalid frame");
  298. if ((rstad & 0x40))
  299. if (cs->debug & L1_DEB_WARN)
  300. debugl1(cs, "dch_int(): RDO");
  301. if (!(rstad & 0x20))
  302. if (cs->debug & L1_DEB_WARN)
  303. debugl1(cs, "dch_int(): CRC error");
  304. cs->writeisac(cs, IPACX_CMDRD, 0x80); // RMC
  305. } else { // received frame ok
  306. count = cs->readisac(cs, IPACX_RBCLD);
  307. if (count) count--; // RSTAB is last byte
  308. count &= D_FIFO_SIZE - 1;
  309. if (count == 0) count = D_FIFO_SIZE;
  310. dch_empty_fifo(cs, count);
  311. if ((count = cs->rcvidx) > 0) {
  312. cs->rcvidx = 0;
  313. if (!(skb = dev_alloc_skb(count)))
  314. printk(KERN_WARNING "HiSax dch_int(): receive out of memory\n");
  315. else {
  316. memcpy(skb_put(skb, count), cs->rcvbuf, count);
  317. skb_queue_tail(&cs->rq, skb);
  318. }
  319. }
  320. }
  321. cs->rcvidx = 0;
  322. schedule_event(cs, D_RCVBUFREADY);
  323. }
  324. if (istad & 0x40) { // RPF
  325. dch_empty_fifo(cs, D_FIFO_SIZE);
  326. }
  327. if (istad & 0x20) { // RFO
  328. if (cs->debug & L1_DEB_WARN) debugl1(cs, "dch_int(): RFO");
  329. cs->writeisac(cs, IPACX_CMDRD, 0x40); //RRES
  330. }
  331. if (istad & 0x10) { // XPR
  332. if (test_and_clear_bit(FLG_DBUSY_TIMER, &cs->HW_Flags))
  333. del_timer(&cs->dbusytimer);
  334. if (test_and_clear_bit(FLG_L1_DBUSY, &cs->HW_Flags))
  335. schedule_event(cs, D_CLEARBUSY);
  336. if (cs->tx_skb) {
  337. if (cs->tx_skb->len) {
  338. dch_fill_fifo(cs);
  339. goto afterXPR;
  340. }
  341. else {
  342. dev_kfree_skb_irq(cs->tx_skb);
  343. cs->tx_skb = NULL;
  344. cs->tx_cnt = 0;
  345. }
  346. }
  347. if ((cs->tx_skb = skb_dequeue(&cs->sq))) {
  348. cs->tx_cnt = 0;
  349. dch_fill_fifo(cs);
  350. }
  351. else {
  352. schedule_event(cs, D_XMTBUFREADY);
  353. }
  354. }
  355. afterXPR:
  356. if (istad & 0x0C) { // XDU or XMR
  357. if (cs->debug & L1_DEB_WARN) debugl1(cs, "dch_int(): XDU");
  358. if (cs->tx_skb) {
  359. skb_push(cs->tx_skb, cs->tx_cnt); // retransmit
  360. cs->tx_cnt = 0;
  361. dch_fill_fifo(cs);
  362. } else {
  363. printk(KERN_WARNING "HiSax: ISAC XDU no skb\n");
  364. debugl1(cs, "ISAC XDU no skb");
  365. }
  366. }
  367. }
  368. //----------------------------------------------------------
  369. //----------------------------------------------------------
  370. static void
  371. dch_setstack(struct PStack *st, struct IsdnCardState *cs)
  372. {
  373. st->l1.l1hw = dch_l2l1;
  374. }
  375. //----------------------------------------------------------
  376. //----------------------------------------------------------
  377. static void
  378. dch_init(struct IsdnCardState *cs)
  379. {
  380. printk(KERN_INFO "HiSax: IPACX ISDN driver v0.1.0\n");
  381. cs->setstack_d = dch_setstack;
  382. cs->dbusytimer.function = (void *) dbusy_timer_handler;
  383. cs->dbusytimer.data = (long) cs;
  384. init_timer(&cs->dbusytimer);
  385. cs->writeisac(cs, IPACX_TR_CONF0, 0x00); // clear LDD
  386. cs->writeisac(cs, IPACX_TR_CONF2, 0x00); // enable transmitter
  387. cs->writeisac(cs, IPACX_MODED, 0xC9); // transparent mode 0, RAC, stop/go
  388. cs->writeisac(cs, IPACX_MON_CR, 0x00); // disable monitor channel
  389. }
  390. //==========================================================
  391. // B channel functions
  392. //==========================================================
  393. //----------------------------------------------------------
  394. // Entry point for commands
  395. //----------------------------------------------------------
  396. static void
  397. bch_l2l1(struct PStack *st, int pr, void *arg)
  398. {
  399. struct BCState *bcs = st->l1.bcs;
  400. struct sk_buff *skb = arg;
  401. u_long flags;
  402. switch (pr) {
  403. case (PH_DATA | REQUEST):
  404. spin_lock_irqsave(&bcs->cs->lock, flags);
  405. if (bcs->tx_skb) {
  406. skb_queue_tail(&bcs->squeue, skb);
  407. } else {
  408. bcs->tx_skb = skb;
  409. set_bit(BC_FLG_BUSY, &bcs->Flag);
  410. bcs->hw.hscx.count = 0;
  411. bch_fill_fifo(bcs);
  412. }
  413. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  414. break;
  415. case (PH_PULL | INDICATION):
  416. spin_lock_irqsave(&bcs->cs->lock, flags);
  417. if (bcs->tx_skb) {
  418. printk(KERN_WARNING "HiSax bch_l2l1(): this shouldn't happen\n");
  419. } else {
  420. set_bit(BC_FLG_BUSY, &bcs->Flag);
  421. bcs->tx_skb = skb;
  422. bcs->hw.hscx.count = 0;
  423. bch_fill_fifo(bcs);
  424. }
  425. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  426. break;
  427. case (PH_PULL | REQUEST):
  428. if (!bcs->tx_skb) {
  429. clear_bit(FLG_L1_PULL_REQ, &st->l1.Flags);
  430. st->l1.l1l2(st, PH_PULL | CONFIRM, NULL);
  431. } else
  432. set_bit(FLG_L1_PULL_REQ, &st->l1.Flags);
  433. break;
  434. case (PH_ACTIVATE | REQUEST):
  435. spin_lock_irqsave(&bcs->cs->lock, flags);
  436. set_bit(BC_FLG_ACTIV, &bcs->Flag);
  437. bch_mode(bcs, st->l1.mode, st->l1.bc);
  438. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  439. l1_msg_b(st, pr, arg);
  440. break;
  441. case (PH_DEACTIVATE | REQUEST):
  442. l1_msg_b(st, pr, arg);
  443. break;
  444. case (PH_DEACTIVATE | CONFIRM):
  445. spin_lock_irqsave(&bcs->cs->lock, flags);
  446. clear_bit(BC_FLG_ACTIV, &bcs->Flag);
  447. clear_bit(BC_FLG_BUSY, &bcs->Flag);
  448. bch_mode(bcs, 0, st->l1.bc);
  449. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  450. st->l1.l1l2(st, PH_DEACTIVATE | CONFIRM, NULL);
  451. break;
  452. }
  453. }
  454. //----------------------------------------------------------
  455. // Read B channel fifo to receive buffer
  456. //----------------------------------------------------------
  457. static void
  458. bch_empty_fifo(struct BCState *bcs, int count)
  459. {
  460. u_char *ptr, hscx;
  461. struct IsdnCardState *cs;
  462. int cnt;
  463. cs = bcs->cs;
  464. hscx = bcs->hw.hscx.hscx;
  465. if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
  466. debugl1(cs, "bch_empty_fifo()");
  467. // message too large, remove
  468. if (bcs->hw.hscx.rcvidx + count > HSCX_BUFMAX) {
  469. if (cs->debug & L1_DEB_WARN)
  470. debugl1(cs, "bch_empty_fifo() incoming packet too large");
  471. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, 0x80); // RMC
  472. bcs->hw.hscx.rcvidx = 0;
  473. return;
  474. }
  475. ptr = bcs->hw.hscx.rcvbuf + bcs->hw.hscx.rcvidx;
  476. cnt = count;
  477. while (cnt--) *ptr++ = cs->BC_Read_Reg(cs, hscx, IPACX_RFIFOB);
  478. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, 0x80); // RMC
  479. ptr = bcs->hw.hscx.rcvbuf + bcs->hw.hscx.rcvidx;
  480. bcs->hw.hscx.rcvidx += count;
  481. if (cs->debug & L1_DEB_HSCX_FIFO) {
  482. char *t = bcs->blog;
  483. t += sprintf(t, "bch_empty_fifo() B-%d cnt %d", hscx, count);
  484. QuickHex(t, ptr, count);
  485. debugl1(cs, "%s", bcs->blog);
  486. }
  487. }
  488. //----------------------------------------------------------
  489. // Fill buffer to transmit FIFO
  490. //----------------------------------------------------------
  491. static void
  492. bch_fill_fifo(struct BCState *bcs)
  493. {
  494. struct IsdnCardState *cs;
  495. int more, count, cnt;
  496. u_char *ptr, *p, hscx;
  497. cs = bcs->cs;
  498. if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
  499. debugl1(cs, "bch_fill_fifo()");
  500. if (!bcs->tx_skb) return;
  501. if (bcs->tx_skb->len <= 0) return;
  502. hscx = bcs->hw.hscx.hscx;
  503. more = (bcs->mode == L1_MODE_TRANS) ? 1 : 0;
  504. if (bcs->tx_skb->len > B_FIFO_SIZE) {
  505. more = 1;
  506. count = B_FIFO_SIZE;
  507. } else {
  508. count = bcs->tx_skb->len;
  509. }
  510. cnt = count;
  511. p = ptr = bcs->tx_skb->data;
  512. skb_pull(bcs->tx_skb, count);
  513. bcs->tx_cnt -= count;
  514. bcs->hw.hscx.count += count;
  515. while (cnt--) cs->BC_Write_Reg(cs, hscx, IPACX_XFIFOB, *p++);
  516. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, (more ? 0x08 : 0x0a));
  517. if (cs->debug & L1_DEB_HSCX_FIFO) {
  518. char *t = bcs->blog;
  519. t += sprintf(t, "%s() B-%d cnt %d", __func__, hscx, count);
  520. QuickHex(t, ptr, count);
  521. debugl1(cs, "%s", bcs->blog);
  522. }
  523. }
  524. //----------------------------------------------------------
  525. // B channel interrupt handler
  526. //----------------------------------------------------------
  527. static void
  528. bch_int(struct IsdnCardState *cs, u_char hscx)
  529. {
  530. u_char istab;
  531. struct BCState *bcs;
  532. struct sk_buff *skb;
  533. int count;
  534. u_char rstab;
  535. bcs = cs->bcs + hscx;
  536. istab = cs->BC_Read_Reg(cs, hscx, IPACX_ISTAB);
  537. //##############################################
  538. // printk(KERN_WARNING "bch_int(istab=%02x)\n", istab);
  539. //##############################################
  540. if (!test_bit(BC_FLG_INIT, &bcs->Flag)) return;
  541. if (istab & 0x80) { // RME
  542. rstab = cs->BC_Read_Reg(cs, hscx, IPACX_RSTAB);
  543. if ((rstab & 0xf0) != 0xa0) { // !(VFR && !RDO && CRC && !RAB)
  544. if (!(rstab & 0x80))
  545. if (cs->debug & L1_DEB_WARN)
  546. debugl1(cs, "bch_int() B-%d: invalid frame", hscx);
  547. if ((rstab & 0x40) && (bcs->mode != L1_MODE_NULL))
  548. if (cs->debug & L1_DEB_WARN)
  549. debugl1(cs, "bch_int() B-%d: RDO mode=%d", hscx, bcs->mode);
  550. if (!(rstab & 0x20))
  551. if (cs->debug & L1_DEB_WARN)
  552. debugl1(cs, "bch_int() B-%d: CRC error", hscx);
  553. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, 0x80); // RMC
  554. }
  555. else { // received frame ok
  556. count = cs->BC_Read_Reg(cs, hscx, IPACX_RBCLB) & (B_FIFO_SIZE - 1);
  557. if (count == 0) count = B_FIFO_SIZE;
  558. bch_empty_fifo(bcs, count);
  559. if ((count = bcs->hw.hscx.rcvidx - 1) > 0) {
  560. if (cs->debug & L1_DEB_HSCX_FIFO)
  561. debugl1(cs, "bch_int Frame %d", count);
  562. if (!(skb = dev_alloc_skb(count)))
  563. printk(KERN_WARNING "HiSax bch_int(): receive frame out of memory\n");
  564. else {
  565. memcpy(skb_put(skb, count), bcs->hw.hscx.rcvbuf, count);
  566. skb_queue_tail(&bcs->rqueue, skb);
  567. }
  568. }
  569. }
  570. bcs->hw.hscx.rcvidx = 0;
  571. schedule_event(bcs, B_RCVBUFREADY);
  572. }
  573. if (istab & 0x40) { // RPF
  574. bch_empty_fifo(bcs, B_FIFO_SIZE);
  575. if (bcs->mode == L1_MODE_TRANS) { // queue every chunk
  576. // receive transparent audio data
  577. if (!(skb = dev_alloc_skb(B_FIFO_SIZE)))
  578. printk(KERN_WARNING "HiSax bch_int(): receive transparent out of memory\n");
  579. else {
  580. memcpy(skb_put(skb, B_FIFO_SIZE), bcs->hw.hscx.rcvbuf, B_FIFO_SIZE);
  581. skb_queue_tail(&bcs->rqueue, skb);
  582. }
  583. bcs->hw.hscx.rcvidx = 0;
  584. schedule_event(bcs, B_RCVBUFREADY);
  585. }
  586. }
  587. if (istab & 0x20) { // RFO
  588. if (cs->debug & L1_DEB_WARN)
  589. debugl1(cs, "bch_int() B-%d: RFO error", hscx);
  590. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, 0x40); // RRES
  591. }
  592. if (istab & 0x10) { // XPR
  593. if (bcs->tx_skb) {
  594. if (bcs->tx_skb->len) {
  595. bch_fill_fifo(bcs);
  596. goto afterXPR;
  597. } else {
  598. if (test_bit(FLG_LLI_L1WAKEUP, &bcs->st->lli.flag) &&
  599. (PACKET_NOACK != bcs->tx_skb->pkt_type)) {
  600. u_long flags;
  601. spin_lock_irqsave(&bcs->aclock, flags);
  602. bcs->ackcnt += bcs->hw.hscx.count;
  603. spin_unlock_irqrestore(&bcs->aclock, flags);
  604. schedule_event(bcs, B_ACKPENDING);
  605. }
  606. }
  607. dev_kfree_skb_irq(bcs->tx_skb);
  608. bcs->hw.hscx.count = 0;
  609. bcs->tx_skb = NULL;
  610. }
  611. if ((bcs->tx_skb = skb_dequeue(&bcs->squeue))) {
  612. bcs->hw.hscx.count = 0;
  613. set_bit(BC_FLG_BUSY, &bcs->Flag);
  614. bch_fill_fifo(bcs);
  615. } else {
  616. clear_bit(BC_FLG_BUSY, &bcs->Flag);
  617. schedule_event(bcs, B_XMTBUFREADY);
  618. }
  619. }
  620. afterXPR:
  621. if (istab & 0x04) { // XDU
  622. if (bcs->mode == L1_MODE_TRANS) {
  623. bch_fill_fifo(bcs);
  624. }
  625. else {
  626. if (bcs->tx_skb) { // restart transmitting the whole frame
  627. skb_push(bcs->tx_skb, bcs->hw.hscx.count);
  628. bcs->tx_cnt += bcs->hw.hscx.count;
  629. bcs->hw.hscx.count = 0;
  630. }
  631. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, 0x01); // XRES
  632. if (cs->debug & L1_DEB_WARN)
  633. debugl1(cs, "bch_int() B-%d XDU error", hscx);
  634. }
  635. }
  636. }
  637. //----------------------------------------------------------
  638. //----------------------------------------------------------
  639. static void
  640. bch_mode(struct BCState *bcs, int mode, int bc)
  641. {
  642. struct IsdnCardState *cs = bcs->cs;
  643. int hscx = bcs->hw.hscx.hscx;
  644. bc = bc ? 1 : 0; // in case bc is greater than 1
  645. if (cs->debug & L1_DEB_HSCX)
  646. debugl1(cs, "mode_bch() switch B-%d mode %d chan %d", hscx, mode, bc);
  647. bcs->mode = mode;
  648. bcs->channel = bc;
  649. // map controller to according timeslot
  650. if (!hscx)
  651. {
  652. cs->writeisac(cs, IPACX_BCHA_TSDP_BC1, 0x80 | bc);
  653. cs->writeisac(cs, IPACX_BCHA_CR, 0x88);
  654. }
  655. else
  656. {
  657. cs->writeisac(cs, IPACX_BCHB_TSDP_BC1, 0x80 | bc);
  658. cs->writeisac(cs, IPACX_BCHB_CR, 0x88);
  659. }
  660. switch (mode) {
  661. case (L1_MODE_NULL):
  662. cs->BC_Write_Reg(cs, hscx, IPACX_MODEB, 0xC0); // rec off
  663. cs->BC_Write_Reg(cs, hscx, IPACX_EXMB, 0x30); // std adj.
  664. cs->BC_Write_Reg(cs, hscx, IPACX_MASKB, 0xFF); // ints off
  665. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, 0x41); // validate adjustments
  666. break;
  667. case (L1_MODE_TRANS):
  668. cs->BC_Write_Reg(cs, hscx, IPACX_MODEB, 0x88); // ext transp mode
  669. cs->BC_Write_Reg(cs, hscx, IPACX_EXMB, 0x00); // xxx00000
  670. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, 0x41); // validate adjustments
  671. cs->BC_Write_Reg(cs, hscx, IPACX_MASKB, _MASKB_IMASK);
  672. break;
  673. case (L1_MODE_HDLC):
  674. cs->BC_Write_Reg(cs, hscx, IPACX_MODEB, 0xC8); // transp mode 0
  675. cs->BC_Write_Reg(cs, hscx, IPACX_EXMB, 0x01); // idle=hdlc flags crc enabled
  676. cs->BC_Write_Reg(cs, hscx, IPACX_CMDRB, 0x41); // validate adjustments
  677. cs->BC_Write_Reg(cs, hscx, IPACX_MASKB, _MASKB_IMASK);
  678. break;
  679. }
  680. }
  681. //----------------------------------------------------------
  682. //----------------------------------------------------------
  683. static void
  684. bch_close_state(struct BCState *bcs)
  685. {
  686. bch_mode(bcs, 0, bcs->channel);
  687. if (test_and_clear_bit(BC_FLG_INIT, &bcs->Flag)) {
  688. kfree(bcs->hw.hscx.rcvbuf);
  689. bcs->hw.hscx.rcvbuf = NULL;
  690. kfree(bcs->blog);
  691. bcs->blog = NULL;
  692. skb_queue_purge(&bcs->rqueue);
  693. skb_queue_purge(&bcs->squeue);
  694. if (bcs->tx_skb) {
  695. dev_kfree_skb_any(bcs->tx_skb);
  696. bcs->tx_skb = NULL;
  697. clear_bit(BC_FLG_BUSY, &bcs->Flag);
  698. }
  699. }
  700. }
  701. //----------------------------------------------------------
  702. //----------------------------------------------------------
  703. static int
  704. bch_open_state(struct IsdnCardState *cs, struct BCState *bcs)
  705. {
  706. if (!test_and_set_bit(BC_FLG_INIT, &bcs->Flag)) {
  707. if (!(bcs->hw.hscx.rcvbuf = kmalloc(HSCX_BUFMAX, GFP_ATOMIC))) {
  708. printk(KERN_WARNING
  709. "HiSax open_bchstate(): No memory for hscx.rcvbuf\n");
  710. clear_bit(BC_FLG_INIT, &bcs->Flag);
  711. return (1);
  712. }
  713. if (!(bcs->blog = kmalloc(MAX_BLOG_SPACE, GFP_ATOMIC))) {
  714. printk(KERN_WARNING
  715. "HiSax open_bchstate: No memory for bcs->blog\n");
  716. clear_bit(BC_FLG_INIT, &bcs->Flag);
  717. kfree(bcs->hw.hscx.rcvbuf);
  718. bcs->hw.hscx.rcvbuf = NULL;
  719. return (2);
  720. }
  721. skb_queue_head_init(&bcs->rqueue);
  722. skb_queue_head_init(&bcs->squeue);
  723. }
  724. bcs->tx_skb = NULL;
  725. clear_bit(BC_FLG_BUSY, &bcs->Flag);
  726. bcs->event = 0;
  727. bcs->hw.hscx.rcvidx = 0;
  728. bcs->tx_cnt = 0;
  729. return (0);
  730. }
  731. //----------------------------------------------------------
  732. //----------------------------------------------------------
  733. static int
  734. bch_setstack(struct PStack *st, struct BCState *bcs)
  735. {
  736. bcs->channel = st->l1.bc;
  737. if (bch_open_state(st->l1.hardware, bcs)) return (-1);
  738. st->l1.bcs = bcs;
  739. st->l2.l2l1 = bch_l2l1;
  740. setstack_manager(st);
  741. bcs->st = st;
  742. setstack_l1_B(st);
  743. return (0);
  744. }
  745. //----------------------------------------------------------
  746. //----------------------------------------------------------
  747. static void
  748. bch_init(struct IsdnCardState *cs, int hscx)
  749. {
  750. cs->bcs[hscx].BC_SetStack = bch_setstack;
  751. cs->bcs[hscx].BC_Close = bch_close_state;
  752. cs->bcs[hscx].hw.hscx.hscx = hscx;
  753. cs->bcs[hscx].cs = cs;
  754. bch_mode(cs->bcs + hscx, 0, hscx);
  755. }
  756. //==========================================================
  757. // Shared functions
  758. //==========================================================
  759. //----------------------------------------------------------
  760. // Main interrupt handler
  761. //----------------------------------------------------------
  762. void
  763. interrupt_ipacx(struct IsdnCardState *cs)
  764. {
  765. u_char ista;
  766. while ((ista = cs->readisac(cs, IPACX_ISTA))) {
  767. //#################################################
  768. // printk(KERN_WARNING "interrupt_ipacx(ista=%02x)\n", ista);
  769. //#################################################
  770. if (ista & 0x80) bch_int(cs, 0); // B channel interrupts
  771. if (ista & 0x40) bch_int(cs, 1);
  772. if (ista & 0x01) dch_int(cs); // D channel
  773. if (ista & 0x10) cic_int(cs); // Layer 1 state
  774. }
  775. }
  776. //----------------------------------------------------------
  777. // Clears chip interrupt status
  778. //----------------------------------------------------------
  779. static void
  780. clear_pending_ints(struct IsdnCardState *cs)
  781. {
  782. int ista;
  783. // all interrupts off
  784. cs->writeisac(cs, IPACX_MASK, 0xff);
  785. cs->writeisac(cs, IPACX_MASKD, 0xff);
  786. cs->BC_Write_Reg(cs, 0, IPACX_MASKB, 0xff);
  787. cs->BC_Write_Reg(cs, 1, IPACX_MASKB, 0xff);
  788. ista = cs->readisac(cs, IPACX_ISTA);
  789. if (ista & 0x80) cs->BC_Read_Reg(cs, 0, IPACX_ISTAB);
  790. if (ista & 0x40) cs->BC_Read_Reg(cs, 1, IPACX_ISTAB);
  791. if (ista & 0x10) cs->readisac(cs, IPACX_CIR0);
  792. if (ista & 0x01) cs->readisac(cs, IPACX_ISTAD);
  793. }
  794. //----------------------------------------------------------
  795. // Does chip configuration work
  796. // Work to do depends on bit mask in part
  797. //----------------------------------------------------------
  798. void
  799. init_ipacx(struct IsdnCardState *cs, int part)
  800. {
  801. if (part & 1) { // initialise chip
  802. //##################################################
  803. // printk(KERN_INFO "init_ipacx(%x)\n", part);
  804. //##################################################
  805. clear_pending_ints(cs);
  806. bch_init(cs, 0);
  807. bch_init(cs, 1);
  808. dch_init(cs);
  809. }
  810. if (part & 2) { // reenable all interrupts and start chip
  811. cs->BC_Write_Reg(cs, 0, IPACX_MASKB, _MASKB_IMASK);
  812. cs->BC_Write_Reg(cs, 1, IPACX_MASKB, _MASKB_IMASK);
  813. cs->writeisac(cs, IPACX_MASKD, _MASKD_IMASK);
  814. cs->writeisac(cs, IPACX_MASK, _MASK_IMASK); // global mask register
  815. // reset HDLC Transmitters/receivers
  816. cs->writeisac(cs, IPACX_CMDRD, 0x41);
  817. cs->BC_Write_Reg(cs, 0, IPACX_CMDRB, 0x41);
  818. cs->BC_Write_Reg(cs, 1, IPACX_CMDRB, 0x41);
  819. ph_command(cs, IPACX_CMD_RES);
  820. }
  821. }
  822. //----------------- end of file -----------------------