avm_pci.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /* $Id: avm_pci.c,v 1.29.2.4 2004/02/11 13:21:32 keil Exp $
  2. *
  3. * low level stuff for AVM Fritz!PCI and ISA PnP isdn cards
  4. *
  5. * Author Karsten Keil
  6. * Copyright by Karsten Keil <keil@isdn4linux.de>
  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. * Thanks to AVM, Berlin for information
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include "hisax.h"
  16. #include "isac.h"
  17. #include "isdnl1.h"
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/isapnp.h>
  21. #include <linux/interrupt.h>
  22. static const char *avm_pci_rev = "$Revision: 1.29.2.4 $";
  23. #define AVM_FRITZ_PCI 1
  24. #define AVM_FRITZ_PNP 2
  25. #define HDLC_FIFO 0x0
  26. #define HDLC_STATUS 0x4
  27. #define AVM_HDLC_1 0x00
  28. #define AVM_HDLC_2 0x01
  29. #define AVM_ISAC_FIFO 0x02
  30. #define AVM_ISAC_REG_LOW 0x04
  31. #define AVM_ISAC_REG_HIGH 0x06
  32. #define AVM_STATUS0_IRQ_ISAC 0x01
  33. #define AVM_STATUS0_IRQ_HDLC 0x02
  34. #define AVM_STATUS0_IRQ_TIMER 0x04
  35. #define AVM_STATUS0_IRQ_MASK 0x07
  36. #define AVM_STATUS0_RESET 0x01
  37. #define AVM_STATUS0_DIS_TIMER 0x02
  38. #define AVM_STATUS0_RES_TIMER 0x04
  39. #define AVM_STATUS0_ENA_IRQ 0x08
  40. #define AVM_STATUS0_TESTBIT 0x10
  41. #define AVM_STATUS1_INT_SEL 0x0f
  42. #define AVM_STATUS1_ENA_IOM 0x80
  43. #define HDLC_MODE_ITF_FLG 0x01
  44. #define HDLC_MODE_TRANS 0x02
  45. #define HDLC_MODE_CCR_7 0x04
  46. #define HDLC_MODE_CCR_16 0x08
  47. #define HDLC_MODE_TESTLOOP 0x80
  48. #define HDLC_INT_XPR 0x80
  49. #define HDLC_INT_XDU 0x40
  50. #define HDLC_INT_RPR 0x20
  51. #define HDLC_INT_MASK 0xE0
  52. #define HDLC_STAT_RME 0x01
  53. #define HDLC_STAT_RDO 0x10
  54. #define HDLC_STAT_CRCVFRRAB 0x0E
  55. #define HDLC_STAT_CRCVFR 0x06
  56. #define HDLC_STAT_RML_MASK 0x3f00
  57. #define HDLC_CMD_XRS 0x80
  58. #define HDLC_CMD_XME 0x01
  59. #define HDLC_CMD_RRS 0x20
  60. #define HDLC_CMD_XML_MASK 0x3f00
  61. /* Interface functions */
  62. static u_char
  63. ReadISAC(struct IsdnCardState *cs, u_char offset)
  64. {
  65. register u_char idx = (offset > 0x2f) ? AVM_ISAC_REG_HIGH : AVM_ISAC_REG_LOW;
  66. register u_char val;
  67. outb(idx, cs->hw.avm.cfg_reg + 4);
  68. val = inb(cs->hw.avm.isac + (offset & 0xf));
  69. return (val);
  70. }
  71. static void
  72. WriteISAC(struct IsdnCardState *cs, u_char offset, u_char value)
  73. {
  74. register u_char idx = (offset > 0x2f) ? AVM_ISAC_REG_HIGH : AVM_ISAC_REG_LOW;
  75. outb(idx, cs->hw.avm.cfg_reg + 4);
  76. outb(value, cs->hw.avm.isac + (offset & 0xf));
  77. }
  78. static void
  79. ReadISACfifo(struct IsdnCardState *cs, u_char *data, int size)
  80. {
  81. outb(AVM_ISAC_FIFO, cs->hw.avm.cfg_reg + 4);
  82. insb(cs->hw.avm.isac, data, size);
  83. }
  84. static void
  85. WriteISACfifo(struct IsdnCardState *cs, u_char *data, int size)
  86. {
  87. outb(AVM_ISAC_FIFO, cs->hw.avm.cfg_reg + 4);
  88. outsb(cs->hw.avm.isac, data, size);
  89. }
  90. static inline u_int
  91. ReadHDLCPCI(struct IsdnCardState *cs, int chan, u_char offset)
  92. {
  93. register u_int idx = chan ? AVM_HDLC_2 : AVM_HDLC_1;
  94. register u_int val;
  95. outl(idx, cs->hw.avm.cfg_reg + 4);
  96. val = inl(cs->hw.avm.isac + offset);
  97. return (val);
  98. }
  99. static inline void
  100. WriteHDLCPCI(struct IsdnCardState *cs, int chan, u_char offset, u_int value)
  101. {
  102. register u_int idx = chan ? AVM_HDLC_2 : AVM_HDLC_1;
  103. outl(idx, cs->hw.avm.cfg_reg + 4);
  104. outl(value, cs->hw.avm.isac + offset);
  105. }
  106. static inline u_char
  107. ReadHDLCPnP(struct IsdnCardState *cs, int chan, u_char offset)
  108. {
  109. register u_char idx = chan ? AVM_HDLC_2 : AVM_HDLC_1;
  110. register u_char val;
  111. outb(idx, cs->hw.avm.cfg_reg + 4);
  112. val = inb(cs->hw.avm.isac + offset);
  113. return (val);
  114. }
  115. static inline void
  116. WriteHDLCPnP(struct IsdnCardState *cs, int chan, u_char offset, u_char value)
  117. {
  118. register u_char idx = chan ? AVM_HDLC_2 : AVM_HDLC_1;
  119. outb(idx, cs->hw.avm.cfg_reg + 4);
  120. outb(value, cs->hw.avm.isac + offset);
  121. }
  122. static u_char
  123. ReadHDLC_s(struct IsdnCardState *cs, int chan, u_char offset)
  124. {
  125. return (0xff & ReadHDLCPCI(cs, chan, offset));
  126. }
  127. static void
  128. WriteHDLC_s(struct IsdnCardState *cs, int chan, u_char offset, u_char value)
  129. {
  130. WriteHDLCPCI(cs, chan, offset, value);
  131. }
  132. static inline
  133. struct BCState *Sel_BCS(struct IsdnCardState *cs, int channel)
  134. {
  135. if (cs->bcs[0].mode && (cs->bcs[0].channel == channel))
  136. return (&cs->bcs[0]);
  137. else if (cs->bcs[1].mode && (cs->bcs[1].channel == channel))
  138. return (&cs->bcs[1]);
  139. else
  140. return (NULL);
  141. }
  142. static void
  143. write_ctrl(struct BCState *bcs, int which) {
  144. if (bcs->cs->debug & L1_DEB_HSCX)
  145. debugl1(bcs->cs, "hdlc %c wr%x ctrl %x",
  146. 'A' + bcs->channel, which, bcs->hw.hdlc.ctrl.ctrl);
  147. if (bcs->cs->subtyp == AVM_FRITZ_PCI) {
  148. WriteHDLCPCI(bcs->cs, bcs->channel, HDLC_STATUS, bcs->hw.hdlc.ctrl.ctrl);
  149. } else {
  150. if (which & 4)
  151. WriteHDLCPnP(bcs->cs, bcs->channel, HDLC_STATUS + 2,
  152. bcs->hw.hdlc.ctrl.sr.mode);
  153. if (which & 2)
  154. WriteHDLCPnP(bcs->cs, bcs->channel, HDLC_STATUS + 1,
  155. bcs->hw.hdlc.ctrl.sr.xml);
  156. if (which & 1)
  157. WriteHDLCPnP(bcs->cs, bcs->channel, HDLC_STATUS,
  158. bcs->hw.hdlc.ctrl.sr.cmd);
  159. }
  160. }
  161. static void
  162. modehdlc(struct BCState *bcs, int mode, int bc)
  163. {
  164. struct IsdnCardState *cs = bcs->cs;
  165. int hdlc = bcs->channel;
  166. if (cs->debug & L1_DEB_HSCX)
  167. debugl1(cs, "hdlc %c mode %d --> %d ichan %d --> %d",
  168. 'A' + hdlc, bcs->mode, mode, hdlc, bc);
  169. bcs->hw.hdlc.ctrl.ctrl = 0;
  170. switch (mode) {
  171. case (-1): /* used for init */
  172. bcs->mode = 1;
  173. bcs->channel = bc;
  174. bc = 0;
  175. case (L1_MODE_NULL):
  176. if (bcs->mode == L1_MODE_NULL)
  177. return;
  178. bcs->hw.hdlc.ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
  179. bcs->hw.hdlc.ctrl.sr.mode = HDLC_MODE_TRANS;
  180. write_ctrl(bcs, 5);
  181. bcs->mode = L1_MODE_NULL;
  182. bcs->channel = bc;
  183. break;
  184. case (L1_MODE_TRANS):
  185. bcs->mode = mode;
  186. bcs->channel = bc;
  187. bcs->hw.hdlc.ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
  188. bcs->hw.hdlc.ctrl.sr.mode = HDLC_MODE_TRANS;
  189. write_ctrl(bcs, 5);
  190. bcs->hw.hdlc.ctrl.sr.cmd = HDLC_CMD_XRS;
  191. write_ctrl(bcs, 1);
  192. bcs->hw.hdlc.ctrl.sr.cmd = 0;
  193. schedule_event(bcs, B_XMTBUFREADY);
  194. break;
  195. case (L1_MODE_HDLC):
  196. bcs->mode = mode;
  197. bcs->channel = bc;
  198. bcs->hw.hdlc.ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
  199. bcs->hw.hdlc.ctrl.sr.mode = HDLC_MODE_ITF_FLG;
  200. write_ctrl(bcs, 5);
  201. bcs->hw.hdlc.ctrl.sr.cmd = HDLC_CMD_XRS;
  202. write_ctrl(bcs, 1);
  203. bcs->hw.hdlc.ctrl.sr.cmd = 0;
  204. schedule_event(bcs, B_XMTBUFREADY);
  205. break;
  206. }
  207. }
  208. static inline void
  209. hdlc_empty_fifo(struct BCState *bcs, int count)
  210. {
  211. register u_int *ptr;
  212. u_char *p;
  213. u_char idx = bcs->channel ? AVM_HDLC_2 : AVM_HDLC_1;
  214. int cnt = 0;
  215. struct IsdnCardState *cs = bcs->cs;
  216. if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
  217. debugl1(cs, "hdlc_empty_fifo %d", count);
  218. if (bcs->hw.hdlc.rcvidx + count > HSCX_BUFMAX) {
  219. if (cs->debug & L1_DEB_WARN)
  220. debugl1(cs, "hdlc_empty_fifo: incoming packet too large");
  221. return;
  222. }
  223. p = bcs->hw.hdlc.rcvbuf + bcs->hw.hdlc.rcvidx;
  224. ptr = (u_int *)p;
  225. bcs->hw.hdlc.rcvidx += count;
  226. if (cs->subtyp == AVM_FRITZ_PCI) {
  227. outl(idx, cs->hw.avm.cfg_reg + 4);
  228. while (cnt < count) {
  229. #ifdef __powerpc__
  230. *ptr++ = in_be32((unsigned *)(cs->hw.avm.isac + _IO_BASE));
  231. #else
  232. *ptr++ = inl(cs->hw.avm.isac);
  233. #endif /* __powerpc__ */
  234. cnt += 4;
  235. }
  236. } else {
  237. outb(idx, cs->hw.avm.cfg_reg + 4);
  238. while (cnt < count) {
  239. *p++ = inb(cs->hw.avm.isac);
  240. cnt++;
  241. }
  242. }
  243. if (cs->debug & L1_DEB_HSCX_FIFO) {
  244. char *t = bcs->blog;
  245. if (cs->subtyp == AVM_FRITZ_PNP)
  246. p = (u_char *) ptr;
  247. t += sprintf(t, "hdlc_empty_fifo %c cnt %d",
  248. bcs->channel ? 'B' : 'A', count);
  249. QuickHex(t, p, count);
  250. debugl1(cs, "%s", bcs->blog);
  251. }
  252. }
  253. static inline void
  254. hdlc_fill_fifo(struct BCState *bcs)
  255. {
  256. struct IsdnCardState *cs = bcs->cs;
  257. int count, cnt = 0;
  258. int fifo_size = 32;
  259. u_char *p;
  260. u_int *ptr;
  261. if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
  262. debugl1(cs, "hdlc_fill_fifo");
  263. if (!bcs->tx_skb)
  264. return;
  265. if (bcs->tx_skb->len <= 0)
  266. return;
  267. bcs->hw.hdlc.ctrl.sr.cmd &= ~HDLC_CMD_XME;
  268. if (bcs->tx_skb->len > fifo_size) {
  269. count = fifo_size;
  270. } else {
  271. count = bcs->tx_skb->len;
  272. if (bcs->mode != L1_MODE_TRANS)
  273. bcs->hw.hdlc.ctrl.sr.cmd |= HDLC_CMD_XME;
  274. }
  275. if ((cs->debug & L1_DEB_HSCX) && !(cs->debug & L1_DEB_HSCX_FIFO))
  276. debugl1(cs, "hdlc_fill_fifo %d/%u", count, bcs->tx_skb->len);
  277. p = bcs->tx_skb->data;
  278. ptr = (u_int *)p;
  279. skb_pull(bcs->tx_skb, count);
  280. bcs->tx_cnt -= count;
  281. bcs->hw.hdlc.count += count;
  282. bcs->hw.hdlc.ctrl.sr.xml = ((count == fifo_size) ? 0 : count);
  283. write_ctrl(bcs, 3); /* sets the correct index too */
  284. if (cs->subtyp == AVM_FRITZ_PCI) {
  285. while (cnt < count) {
  286. #ifdef __powerpc__
  287. out_be32((unsigned *)(cs->hw.avm.isac + _IO_BASE), *ptr++);
  288. #else
  289. outl(*ptr++, cs->hw.avm.isac);
  290. #endif /* __powerpc__ */
  291. cnt += 4;
  292. }
  293. } else {
  294. while (cnt < count) {
  295. outb(*p++, cs->hw.avm.isac);
  296. cnt++;
  297. }
  298. }
  299. if (cs->debug & L1_DEB_HSCX_FIFO) {
  300. char *t = bcs->blog;
  301. if (cs->subtyp == AVM_FRITZ_PNP)
  302. p = (u_char *) ptr;
  303. t += sprintf(t, "hdlc_fill_fifo %c cnt %d",
  304. bcs->channel ? 'B' : 'A', count);
  305. QuickHex(t, p, count);
  306. debugl1(cs, "%s", bcs->blog);
  307. }
  308. }
  309. static void
  310. HDLC_irq(struct BCState *bcs, u_int stat) {
  311. int len;
  312. struct sk_buff *skb;
  313. if (bcs->cs->debug & L1_DEB_HSCX)
  314. debugl1(bcs->cs, "ch%d stat %#x", bcs->channel, stat);
  315. if (stat & HDLC_INT_RPR) {
  316. if (stat & HDLC_STAT_RDO) {
  317. if (bcs->cs->debug & L1_DEB_HSCX)
  318. debugl1(bcs->cs, "RDO");
  319. else
  320. debugl1(bcs->cs, "ch%d stat %#x", bcs->channel, stat);
  321. bcs->hw.hdlc.ctrl.sr.xml = 0;
  322. bcs->hw.hdlc.ctrl.sr.cmd |= HDLC_CMD_RRS;
  323. write_ctrl(bcs, 1);
  324. bcs->hw.hdlc.ctrl.sr.cmd &= ~HDLC_CMD_RRS;
  325. write_ctrl(bcs, 1);
  326. bcs->hw.hdlc.rcvidx = 0;
  327. } else {
  328. if (!(len = (stat & HDLC_STAT_RML_MASK) >> 8))
  329. len = 32;
  330. hdlc_empty_fifo(bcs, len);
  331. if ((stat & HDLC_STAT_RME) || (bcs->mode == L1_MODE_TRANS)) {
  332. if (((stat & HDLC_STAT_CRCVFRRAB) == HDLC_STAT_CRCVFR) ||
  333. (bcs->mode == L1_MODE_TRANS)) {
  334. if (!(skb = dev_alloc_skb(bcs->hw.hdlc.rcvidx)))
  335. printk(KERN_WARNING "HDLC: receive out of memory\n");
  336. else {
  337. memcpy(skb_put(skb, bcs->hw.hdlc.rcvidx),
  338. bcs->hw.hdlc.rcvbuf, bcs->hw.hdlc.rcvidx);
  339. skb_queue_tail(&bcs->rqueue, skb);
  340. }
  341. bcs->hw.hdlc.rcvidx = 0;
  342. schedule_event(bcs, B_RCVBUFREADY);
  343. } else {
  344. if (bcs->cs->debug & L1_DEB_HSCX)
  345. debugl1(bcs->cs, "invalid frame");
  346. else
  347. debugl1(bcs->cs, "ch%d invalid frame %#x", bcs->channel, stat);
  348. bcs->hw.hdlc.rcvidx = 0;
  349. }
  350. }
  351. }
  352. }
  353. if (stat & HDLC_INT_XDU) {
  354. /* Here we lost an TX interrupt, so
  355. * restart transmitting the whole frame.
  356. */
  357. if (bcs->tx_skb) {
  358. skb_push(bcs->tx_skb, bcs->hw.hdlc.count);
  359. bcs->tx_cnt += bcs->hw.hdlc.count;
  360. bcs->hw.hdlc.count = 0;
  361. if (bcs->cs->debug & L1_DEB_WARN)
  362. debugl1(bcs->cs, "ch%d XDU", bcs->channel);
  363. } else if (bcs->cs->debug & L1_DEB_WARN)
  364. debugl1(bcs->cs, "ch%d XDU without skb", bcs->channel);
  365. bcs->hw.hdlc.ctrl.sr.xml = 0;
  366. bcs->hw.hdlc.ctrl.sr.cmd |= HDLC_CMD_XRS;
  367. write_ctrl(bcs, 1);
  368. bcs->hw.hdlc.ctrl.sr.cmd &= ~HDLC_CMD_XRS;
  369. write_ctrl(bcs, 1);
  370. hdlc_fill_fifo(bcs);
  371. } else if (stat & HDLC_INT_XPR) {
  372. if (bcs->tx_skb) {
  373. if (bcs->tx_skb->len) {
  374. hdlc_fill_fifo(bcs);
  375. return;
  376. } else {
  377. if (test_bit(FLG_LLI_L1WAKEUP, &bcs->st->lli.flag) &&
  378. (PACKET_NOACK != bcs->tx_skb->pkt_type)) {
  379. u_long flags;
  380. spin_lock_irqsave(&bcs->aclock, flags);
  381. bcs->ackcnt += bcs->hw.hdlc.count;
  382. spin_unlock_irqrestore(&bcs->aclock, flags);
  383. schedule_event(bcs, B_ACKPENDING);
  384. }
  385. dev_kfree_skb_irq(bcs->tx_skb);
  386. bcs->hw.hdlc.count = 0;
  387. bcs->tx_skb = NULL;
  388. }
  389. }
  390. if ((bcs->tx_skb = skb_dequeue(&bcs->squeue))) {
  391. bcs->hw.hdlc.count = 0;
  392. test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
  393. hdlc_fill_fifo(bcs);
  394. } else {
  395. test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
  396. schedule_event(bcs, B_XMTBUFREADY);
  397. }
  398. }
  399. }
  400. static inline void
  401. HDLC_irq_main(struct IsdnCardState *cs)
  402. {
  403. u_int stat;
  404. struct BCState *bcs;
  405. if (cs->subtyp == AVM_FRITZ_PCI) {
  406. stat = ReadHDLCPCI(cs, 0, HDLC_STATUS);
  407. } else {
  408. stat = ReadHDLCPnP(cs, 0, HDLC_STATUS);
  409. if (stat & HDLC_INT_RPR)
  410. stat |= (ReadHDLCPnP(cs, 0, HDLC_STATUS + 1)) << 8;
  411. }
  412. if (stat & HDLC_INT_MASK) {
  413. if (!(bcs = Sel_BCS(cs, 0))) {
  414. if (cs->debug)
  415. debugl1(cs, "hdlc spurious channel 0 IRQ");
  416. } else
  417. HDLC_irq(bcs, stat);
  418. }
  419. if (cs->subtyp == AVM_FRITZ_PCI) {
  420. stat = ReadHDLCPCI(cs, 1, HDLC_STATUS);
  421. } else {
  422. stat = ReadHDLCPnP(cs, 1, HDLC_STATUS);
  423. if (stat & HDLC_INT_RPR)
  424. stat |= (ReadHDLCPnP(cs, 1, HDLC_STATUS + 1)) << 8;
  425. }
  426. if (stat & HDLC_INT_MASK) {
  427. if (!(bcs = Sel_BCS(cs, 1))) {
  428. if (cs->debug)
  429. debugl1(cs, "hdlc spurious channel 1 IRQ");
  430. } else
  431. HDLC_irq(bcs, stat);
  432. }
  433. }
  434. static void
  435. hdlc_l2l1(struct PStack *st, int pr, void *arg)
  436. {
  437. struct BCState *bcs = st->l1.bcs;
  438. struct sk_buff *skb = arg;
  439. u_long flags;
  440. switch (pr) {
  441. case (PH_DATA | REQUEST):
  442. spin_lock_irqsave(&bcs->cs->lock, flags);
  443. if (bcs->tx_skb) {
  444. skb_queue_tail(&bcs->squeue, skb);
  445. } else {
  446. bcs->tx_skb = skb;
  447. test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
  448. bcs->hw.hdlc.count = 0;
  449. bcs->cs->BC_Send_Data(bcs);
  450. }
  451. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  452. break;
  453. case (PH_PULL | INDICATION):
  454. spin_lock_irqsave(&bcs->cs->lock, flags);
  455. if (bcs->tx_skb) {
  456. printk(KERN_WARNING "hdlc_l2l1: this shouldn't happen\n");
  457. } else {
  458. test_and_set_bit(BC_FLG_BUSY, &bcs->Flag);
  459. bcs->tx_skb = skb;
  460. bcs->hw.hdlc.count = 0;
  461. bcs->cs->BC_Send_Data(bcs);
  462. }
  463. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  464. break;
  465. case (PH_PULL | REQUEST):
  466. if (!bcs->tx_skb) {
  467. test_and_clear_bit(FLG_L1_PULL_REQ, &st->l1.Flags);
  468. st->l1.l1l2(st, PH_PULL | CONFIRM, NULL);
  469. } else
  470. test_and_set_bit(FLG_L1_PULL_REQ, &st->l1.Flags);
  471. break;
  472. case (PH_ACTIVATE | REQUEST):
  473. spin_lock_irqsave(&bcs->cs->lock, flags);
  474. test_and_set_bit(BC_FLG_ACTIV, &bcs->Flag);
  475. modehdlc(bcs, st->l1.mode, st->l1.bc);
  476. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  477. l1_msg_b(st, pr, arg);
  478. break;
  479. case (PH_DEACTIVATE | REQUEST):
  480. l1_msg_b(st, pr, arg);
  481. break;
  482. case (PH_DEACTIVATE | CONFIRM):
  483. spin_lock_irqsave(&bcs->cs->lock, flags);
  484. test_and_clear_bit(BC_FLG_ACTIV, &bcs->Flag);
  485. test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
  486. modehdlc(bcs, 0, st->l1.bc);
  487. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  488. st->l1.l1l2(st, PH_DEACTIVATE | CONFIRM, NULL);
  489. break;
  490. }
  491. }
  492. static void
  493. close_hdlcstate(struct BCState *bcs)
  494. {
  495. modehdlc(bcs, 0, 0);
  496. if (test_and_clear_bit(BC_FLG_INIT, &bcs->Flag)) {
  497. kfree(bcs->hw.hdlc.rcvbuf);
  498. bcs->hw.hdlc.rcvbuf = NULL;
  499. kfree(bcs->blog);
  500. bcs->blog = NULL;
  501. skb_queue_purge(&bcs->rqueue);
  502. skb_queue_purge(&bcs->squeue);
  503. if (bcs->tx_skb) {
  504. dev_kfree_skb_any(bcs->tx_skb);
  505. bcs->tx_skb = NULL;
  506. test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
  507. }
  508. }
  509. }
  510. static int
  511. open_hdlcstate(struct IsdnCardState *cs, struct BCState *bcs)
  512. {
  513. if (!test_and_set_bit(BC_FLG_INIT, &bcs->Flag)) {
  514. if (!(bcs->hw.hdlc.rcvbuf = kmalloc(HSCX_BUFMAX, GFP_ATOMIC))) {
  515. printk(KERN_WARNING
  516. "HiSax: No memory for hdlc.rcvbuf\n");
  517. return (1);
  518. }
  519. if (!(bcs->blog = kmalloc(MAX_BLOG_SPACE, GFP_ATOMIC))) {
  520. printk(KERN_WARNING
  521. "HiSax: No memory for bcs->blog\n");
  522. test_and_clear_bit(BC_FLG_INIT, &bcs->Flag);
  523. kfree(bcs->hw.hdlc.rcvbuf);
  524. bcs->hw.hdlc.rcvbuf = NULL;
  525. return (2);
  526. }
  527. skb_queue_head_init(&bcs->rqueue);
  528. skb_queue_head_init(&bcs->squeue);
  529. }
  530. bcs->tx_skb = NULL;
  531. test_and_clear_bit(BC_FLG_BUSY, &bcs->Flag);
  532. bcs->event = 0;
  533. bcs->hw.hdlc.rcvidx = 0;
  534. bcs->tx_cnt = 0;
  535. return (0);
  536. }
  537. static int
  538. setstack_hdlc(struct PStack *st, struct BCState *bcs)
  539. {
  540. bcs->channel = st->l1.bc;
  541. if (open_hdlcstate(st->l1.hardware, bcs))
  542. return (-1);
  543. st->l1.bcs = bcs;
  544. st->l2.l2l1 = hdlc_l2l1;
  545. setstack_manager(st);
  546. bcs->st = st;
  547. setstack_l1_B(st);
  548. return (0);
  549. }
  550. #if 0
  551. void __init
  552. clear_pending_hdlc_ints(struct IsdnCardState *cs)
  553. {
  554. u_int val;
  555. if (cs->subtyp == AVM_FRITZ_PCI) {
  556. val = ReadHDLCPCI(cs, 0, HDLC_STATUS);
  557. debugl1(cs, "HDLC 1 STA %x", val);
  558. val = ReadHDLCPCI(cs, 1, HDLC_STATUS);
  559. debugl1(cs, "HDLC 2 STA %x", val);
  560. } else {
  561. val = ReadHDLCPnP(cs, 0, HDLC_STATUS);
  562. debugl1(cs, "HDLC 1 STA %x", val);
  563. val = ReadHDLCPnP(cs, 0, HDLC_STATUS + 1);
  564. debugl1(cs, "HDLC 1 RML %x", val);
  565. val = ReadHDLCPnP(cs, 0, HDLC_STATUS + 2);
  566. debugl1(cs, "HDLC 1 MODE %x", val);
  567. val = ReadHDLCPnP(cs, 0, HDLC_STATUS + 3);
  568. debugl1(cs, "HDLC 1 VIN %x", val);
  569. val = ReadHDLCPnP(cs, 1, HDLC_STATUS);
  570. debugl1(cs, "HDLC 2 STA %x", val);
  571. val = ReadHDLCPnP(cs, 1, HDLC_STATUS + 1);
  572. debugl1(cs, "HDLC 2 RML %x", val);
  573. val = ReadHDLCPnP(cs, 1, HDLC_STATUS + 2);
  574. debugl1(cs, "HDLC 2 MODE %x", val);
  575. val = ReadHDLCPnP(cs, 1, HDLC_STATUS + 3);
  576. debugl1(cs, "HDLC 2 VIN %x", val);
  577. }
  578. }
  579. #endif /* 0 */
  580. static void
  581. inithdlc(struct IsdnCardState *cs)
  582. {
  583. cs->bcs[0].BC_SetStack = setstack_hdlc;
  584. cs->bcs[1].BC_SetStack = setstack_hdlc;
  585. cs->bcs[0].BC_Close = close_hdlcstate;
  586. cs->bcs[1].BC_Close = close_hdlcstate;
  587. modehdlc(cs->bcs, -1, 0);
  588. modehdlc(cs->bcs + 1, -1, 1);
  589. }
  590. static irqreturn_t
  591. avm_pcipnp_interrupt(int intno, void *dev_id)
  592. {
  593. struct IsdnCardState *cs = dev_id;
  594. u_long flags;
  595. u_char val;
  596. u_char sval;
  597. spin_lock_irqsave(&cs->lock, flags);
  598. sval = inb(cs->hw.avm.cfg_reg + 2);
  599. if ((sval & AVM_STATUS0_IRQ_MASK) == AVM_STATUS0_IRQ_MASK) {
  600. /* possible a shared IRQ reqest */
  601. spin_unlock_irqrestore(&cs->lock, flags);
  602. return IRQ_NONE;
  603. }
  604. if (!(sval & AVM_STATUS0_IRQ_ISAC)) {
  605. val = ReadISAC(cs, ISAC_ISTA);
  606. isac_interrupt(cs, val);
  607. }
  608. if (!(sval & AVM_STATUS0_IRQ_HDLC)) {
  609. HDLC_irq_main(cs);
  610. }
  611. WriteISAC(cs, ISAC_MASK, 0xFF);
  612. WriteISAC(cs, ISAC_MASK, 0x0);
  613. spin_unlock_irqrestore(&cs->lock, flags);
  614. return IRQ_HANDLED;
  615. }
  616. static void
  617. reset_avmpcipnp(struct IsdnCardState *cs)
  618. {
  619. printk(KERN_INFO "AVM PCI/PnP: reset\n");
  620. outb(AVM_STATUS0_RESET | AVM_STATUS0_DIS_TIMER, cs->hw.avm.cfg_reg + 2);
  621. mdelay(10);
  622. outb(AVM_STATUS0_DIS_TIMER | AVM_STATUS0_RES_TIMER | AVM_STATUS0_ENA_IRQ, cs->hw.avm.cfg_reg + 2);
  623. outb(AVM_STATUS1_ENA_IOM | cs->irq, cs->hw.avm.cfg_reg + 3);
  624. mdelay(10);
  625. printk(KERN_INFO "AVM PCI/PnP: S1 %x\n", inb(cs->hw.avm.cfg_reg + 3));
  626. }
  627. static int
  628. AVM_card_msg(struct IsdnCardState *cs, int mt, void *arg)
  629. {
  630. u_long flags;
  631. switch (mt) {
  632. case CARD_RESET:
  633. spin_lock_irqsave(&cs->lock, flags);
  634. reset_avmpcipnp(cs);
  635. spin_unlock_irqrestore(&cs->lock, flags);
  636. return (0);
  637. case CARD_RELEASE:
  638. outb(0, cs->hw.avm.cfg_reg + 2);
  639. release_region(cs->hw.avm.cfg_reg, 32);
  640. return (0);
  641. case CARD_INIT:
  642. spin_lock_irqsave(&cs->lock, flags);
  643. reset_avmpcipnp(cs);
  644. clear_pending_isac_ints(cs);
  645. initisac(cs);
  646. inithdlc(cs);
  647. outb(AVM_STATUS0_DIS_TIMER | AVM_STATUS0_RES_TIMER,
  648. cs->hw.avm.cfg_reg + 2);
  649. WriteISAC(cs, ISAC_MASK, 0);
  650. outb(AVM_STATUS0_DIS_TIMER | AVM_STATUS0_RES_TIMER |
  651. AVM_STATUS0_ENA_IRQ, cs->hw.avm.cfg_reg + 2);
  652. /* RESET Receiver and Transmitter */
  653. WriteISAC(cs, ISAC_CMDR, 0x41);
  654. spin_unlock_irqrestore(&cs->lock, flags);
  655. return (0);
  656. case CARD_TEST:
  657. return (0);
  658. }
  659. return (0);
  660. }
  661. static int avm_setup_rest(struct IsdnCardState *cs)
  662. {
  663. u_int val, ver;
  664. cs->hw.avm.isac = cs->hw.avm.cfg_reg + 0x10;
  665. if (!request_region(cs->hw.avm.cfg_reg, 32,
  666. (cs->subtyp == AVM_FRITZ_PCI) ? "avm PCI" : "avm PnP")) {
  667. printk(KERN_WARNING
  668. "HiSax: Fritz!PCI/PNP config port %x-%x already in use\n",
  669. cs->hw.avm.cfg_reg,
  670. cs->hw.avm.cfg_reg + 31);
  671. return (0);
  672. }
  673. switch (cs->subtyp) {
  674. case AVM_FRITZ_PCI:
  675. val = inl(cs->hw.avm.cfg_reg);
  676. printk(KERN_INFO "AVM PCI: stat %#x\n", val);
  677. printk(KERN_INFO "AVM PCI: Class %X Rev %d\n",
  678. val & 0xff, (val >> 8) & 0xff);
  679. cs->BC_Read_Reg = &ReadHDLC_s;
  680. cs->BC_Write_Reg = &WriteHDLC_s;
  681. break;
  682. case AVM_FRITZ_PNP:
  683. val = inb(cs->hw.avm.cfg_reg);
  684. ver = inb(cs->hw.avm.cfg_reg + 1);
  685. printk(KERN_INFO "AVM PnP: Class %X Rev %d\n", val, ver);
  686. cs->BC_Read_Reg = &ReadHDLCPnP;
  687. cs->BC_Write_Reg = &WriteHDLCPnP;
  688. break;
  689. default:
  690. printk(KERN_WARNING "AVM unknown subtype %d\n", cs->subtyp);
  691. return (0);
  692. }
  693. printk(KERN_INFO "HiSax: %s config irq:%d base:0x%X\n",
  694. (cs->subtyp == AVM_FRITZ_PCI) ? "AVM Fritz!PCI" : "AVM Fritz!PnP",
  695. cs->irq, cs->hw.avm.cfg_reg);
  696. setup_isac(cs);
  697. cs->readisac = &ReadISAC;
  698. cs->writeisac = &WriteISAC;
  699. cs->readisacfifo = &ReadISACfifo;
  700. cs->writeisacfifo = &WriteISACfifo;
  701. cs->BC_Send_Data = &hdlc_fill_fifo;
  702. cs->cardmsg = &AVM_card_msg;
  703. cs->irq_func = &avm_pcipnp_interrupt;
  704. cs->writeisac(cs, ISAC_MASK, 0xFF);
  705. ISACVersion(cs, (cs->subtyp == AVM_FRITZ_PCI) ? "AVM PCI:" : "AVM PnP:");
  706. return (1);
  707. }
  708. #ifndef __ISAPNP__
  709. static int avm_pnp_setup(struct IsdnCardState *cs)
  710. {
  711. return (1); /* no-op: success */
  712. }
  713. #else
  714. static struct pnp_card *pnp_avm_c = NULL;
  715. static int avm_pnp_setup(struct IsdnCardState *cs)
  716. {
  717. struct pnp_dev *pnp_avm_d = NULL;
  718. if (!isapnp_present())
  719. return (1); /* no-op: success */
  720. if ((pnp_avm_c = pnp_find_card(
  721. ISAPNP_VENDOR('A', 'V', 'M'),
  722. ISAPNP_FUNCTION(0x0900), pnp_avm_c))) {
  723. if ((pnp_avm_d = pnp_find_dev(pnp_avm_c,
  724. ISAPNP_VENDOR('A', 'V', 'M'),
  725. ISAPNP_FUNCTION(0x0900), pnp_avm_d))) {
  726. int err;
  727. pnp_disable_dev(pnp_avm_d);
  728. err = pnp_activate_dev(pnp_avm_d);
  729. if (err < 0) {
  730. printk(KERN_WARNING "%s: pnp_activate_dev ret(%d)\n",
  731. __func__, err);
  732. return (0);
  733. }
  734. cs->hw.avm.cfg_reg =
  735. pnp_port_start(pnp_avm_d, 0);
  736. cs->irq = pnp_irq(pnp_avm_d, 0);
  737. if (!cs->irq) {
  738. printk(KERN_ERR "FritzPnP:No IRQ\n");
  739. return (0);
  740. }
  741. if (!cs->hw.avm.cfg_reg) {
  742. printk(KERN_ERR "FritzPnP:No IO address\n");
  743. return (0);
  744. }
  745. cs->subtyp = AVM_FRITZ_PNP;
  746. return (2); /* goto 'ready' label */
  747. }
  748. }
  749. return (1);
  750. }
  751. #endif /* __ISAPNP__ */
  752. #ifndef CONFIG_PCI
  753. static int avm_pci_setup(struct IsdnCardState *cs)
  754. {
  755. return (1); /* no-op: success */
  756. }
  757. #else
  758. static struct pci_dev *dev_avm = NULL;
  759. static int avm_pci_setup(struct IsdnCardState *cs)
  760. {
  761. if ((dev_avm = hisax_find_pci_device(PCI_VENDOR_ID_AVM,
  762. PCI_DEVICE_ID_AVM_A1, dev_avm))) {
  763. if (pci_enable_device(dev_avm))
  764. return (0);
  765. cs->irq = dev_avm->irq;
  766. if (!cs->irq) {
  767. printk(KERN_ERR "FritzPCI: No IRQ for PCI card found\n");
  768. return (0);
  769. }
  770. cs->hw.avm.cfg_reg = pci_resource_start(dev_avm, 1);
  771. if (!cs->hw.avm.cfg_reg) {
  772. printk(KERN_ERR "FritzPCI: No IO-Adr for PCI card found\n");
  773. return (0);
  774. }
  775. cs->subtyp = AVM_FRITZ_PCI;
  776. } else {
  777. printk(KERN_WARNING "FritzPCI: No PCI card found\n");
  778. return (0);
  779. }
  780. cs->irq_flags |= IRQF_SHARED;
  781. return (1);
  782. }
  783. #endif /* CONFIG_PCI */
  784. int setup_avm_pcipnp(struct IsdnCard *card)
  785. {
  786. struct IsdnCardState *cs = card->cs;
  787. char tmp[64];
  788. int rc;
  789. strcpy(tmp, avm_pci_rev);
  790. printk(KERN_INFO "HiSax: AVM PCI driver Rev. %s\n", HiSax_getrev(tmp));
  791. if (cs->typ != ISDN_CTYPE_FRITZPCI)
  792. return (0);
  793. if (card->para[1]) {
  794. /* old manual method */
  795. cs->hw.avm.cfg_reg = card->para[1];
  796. cs->irq = card->para[0];
  797. cs->subtyp = AVM_FRITZ_PNP;
  798. goto ready;
  799. }
  800. rc = avm_pnp_setup(cs);
  801. if (rc < 1)
  802. return (0);
  803. if (rc == 2)
  804. goto ready;
  805. rc = avm_pci_setup(cs);
  806. if (rc < 1)
  807. return (0);
  808. ready:
  809. return avm_setup_rest(cs);
  810. }