avmfritz.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * avm_fritz.c low level stuff for AVM FRITZ!CARD PCI ISDN cards
  3. * Thanks to AVM, Berlin for informations
  4. *
  5. * Author Karsten Keil <keil@isdn4linux.de>
  6. *
  7. * Copyright 2009 by Karsten Keil <keil@isdn4linux.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/pci.h>
  25. #include <linux/delay.h>
  26. #include <linux/mISDNhw.h>
  27. #include <linux/slab.h>
  28. #include <asm/unaligned.h>
  29. #include "ipac.h"
  30. #define AVMFRITZ_REV "2.1"
  31. static int AVM_cnt;
  32. static int debug;
  33. enum {
  34. AVM_FRITZ_PCI,
  35. AVM_FRITZ_PCIV2,
  36. };
  37. #define HDLC_FIFO 0x0
  38. #define HDLC_STATUS 0x4
  39. #define CHIP_WINDOW 0x10
  40. #define CHIP_INDEX 0x4
  41. #define AVM_HDLC_1 0x00
  42. #define AVM_HDLC_2 0x01
  43. #define AVM_ISAC_FIFO 0x02
  44. #define AVM_ISAC_REG_LOW 0x04
  45. #define AVM_ISAC_REG_HIGH 0x06
  46. #define AVM_STATUS0_IRQ_ISAC 0x01
  47. #define AVM_STATUS0_IRQ_HDLC 0x02
  48. #define AVM_STATUS0_IRQ_TIMER 0x04
  49. #define AVM_STATUS0_IRQ_MASK 0x07
  50. #define AVM_STATUS0_RESET 0x01
  51. #define AVM_STATUS0_DIS_TIMER 0x02
  52. #define AVM_STATUS0_RES_TIMER 0x04
  53. #define AVM_STATUS0_ENA_IRQ 0x08
  54. #define AVM_STATUS0_TESTBIT 0x10
  55. #define AVM_STATUS1_INT_SEL 0x0f
  56. #define AVM_STATUS1_ENA_IOM 0x80
  57. #define HDLC_MODE_ITF_FLG 0x01
  58. #define HDLC_MODE_TRANS 0x02
  59. #define HDLC_MODE_CCR_7 0x04
  60. #define HDLC_MODE_CCR_16 0x08
  61. #define HDLC_MODE_TESTLOOP 0x80
  62. #define HDLC_INT_XPR 0x80
  63. #define HDLC_INT_XDU 0x40
  64. #define HDLC_INT_RPR 0x20
  65. #define HDLC_INT_MASK 0xE0
  66. #define HDLC_STAT_RME 0x01
  67. #define HDLC_STAT_RDO 0x10
  68. #define HDLC_STAT_CRCVFRRAB 0x0E
  69. #define HDLC_STAT_CRCVFR 0x06
  70. #define HDLC_STAT_RML_MASK 0x3f00
  71. #define HDLC_CMD_XRS 0x80
  72. #define HDLC_CMD_XME 0x01
  73. #define HDLC_CMD_RRS 0x20
  74. #define HDLC_CMD_XML_MASK 0x3f00
  75. #define HDLC_FIFO_SIZE 32
  76. /* Fritz PCI v2.0 */
  77. #define AVM_HDLC_FIFO_1 0x10
  78. #define AVM_HDLC_FIFO_2 0x18
  79. #define AVM_HDLC_STATUS_1 0x14
  80. #define AVM_HDLC_STATUS_2 0x1c
  81. #define AVM_ISACX_INDEX 0x04
  82. #define AVM_ISACX_DATA 0x08
  83. /* data struct */
  84. #define LOG_SIZE 63
  85. struct hdlc_stat_reg {
  86. #ifdef __BIG_ENDIAN
  87. u8 fill;
  88. u8 mode;
  89. u8 xml;
  90. u8 cmd;
  91. #else
  92. u8 cmd;
  93. u8 xml;
  94. u8 mode;
  95. u8 fill;
  96. #endif
  97. } __attribute__((packed));
  98. struct hdlc_hw {
  99. union {
  100. u32 ctrl;
  101. struct hdlc_stat_reg sr;
  102. } ctrl;
  103. u32 stat;
  104. };
  105. struct fritzcard {
  106. struct list_head list;
  107. struct pci_dev *pdev;
  108. char name[MISDN_MAX_IDLEN];
  109. u8 type;
  110. u8 ctrlreg;
  111. u16 irq;
  112. u32 irqcnt;
  113. u32 addr;
  114. spinlock_t lock; /* hw lock */
  115. struct isac_hw isac;
  116. struct hdlc_hw hdlc[2];
  117. struct bchannel bch[2];
  118. char log[LOG_SIZE + 1];
  119. };
  120. static LIST_HEAD(Cards);
  121. static DEFINE_RWLOCK(card_lock); /* protect Cards */
  122. static void
  123. _set_debug(struct fritzcard *card)
  124. {
  125. card->isac.dch.debug = debug;
  126. card->bch[0].debug = debug;
  127. card->bch[1].debug = debug;
  128. }
  129. static int
  130. set_debug(const char *val, struct kernel_param *kp)
  131. {
  132. int ret;
  133. struct fritzcard *card;
  134. ret = param_set_uint(val, kp);
  135. if (!ret) {
  136. read_lock(&card_lock);
  137. list_for_each_entry(card, &Cards, list)
  138. _set_debug(card);
  139. read_unlock(&card_lock);
  140. }
  141. return ret;
  142. }
  143. MODULE_AUTHOR("Karsten Keil");
  144. MODULE_LICENSE("GPL v2");
  145. MODULE_VERSION(AVMFRITZ_REV);
  146. module_param_call(debug, set_debug, param_get_uint, &debug, S_IRUGO | S_IWUSR);
  147. MODULE_PARM_DESC(debug, "avmfritz debug mask");
  148. /* Interface functions */
  149. static u8
  150. ReadISAC_V1(void *p, u8 offset)
  151. {
  152. struct fritzcard *fc = p;
  153. u8 idx = (offset > 0x2f) ? AVM_ISAC_REG_HIGH : AVM_ISAC_REG_LOW;
  154. outb(idx, fc->addr + CHIP_INDEX);
  155. return inb(fc->addr + CHIP_WINDOW + (offset & 0xf));
  156. }
  157. static void
  158. WriteISAC_V1(void *p, u8 offset, u8 value)
  159. {
  160. struct fritzcard *fc = p;
  161. u8 idx = (offset > 0x2f) ? AVM_ISAC_REG_HIGH : AVM_ISAC_REG_LOW;
  162. outb(idx, fc->addr + CHIP_INDEX);
  163. outb(value, fc->addr + CHIP_WINDOW + (offset & 0xf));
  164. }
  165. static void
  166. ReadFiFoISAC_V1(void *p, u8 off, u8 *data, int size)
  167. {
  168. struct fritzcard *fc = p;
  169. outb(AVM_ISAC_FIFO, fc->addr + CHIP_INDEX);
  170. insb(fc->addr + CHIP_WINDOW, data, size);
  171. }
  172. static void
  173. WriteFiFoISAC_V1(void *p, u8 off, u8 *data, int size)
  174. {
  175. struct fritzcard *fc = p;
  176. outb(AVM_ISAC_FIFO, fc->addr + CHIP_INDEX);
  177. outsb(fc->addr + CHIP_WINDOW, data, size);
  178. }
  179. static u8
  180. ReadISAC_V2(void *p, u8 offset)
  181. {
  182. struct fritzcard *fc = p;
  183. outl(offset, fc->addr + AVM_ISACX_INDEX);
  184. return 0xff & inl(fc->addr + AVM_ISACX_DATA);
  185. }
  186. static void
  187. WriteISAC_V2(void *p, u8 offset, u8 value)
  188. {
  189. struct fritzcard *fc = p;
  190. outl(offset, fc->addr + AVM_ISACX_INDEX);
  191. outl(value, fc->addr + AVM_ISACX_DATA);
  192. }
  193. static void
  194. ReadFiFoISAC_V2(void *p, u8 off, u8 *data, int size)
  195. {
  196. struct fritzcard *fc = p;
  197. int i;
  198. outl(off, fc->addr + AVM_ISACX_INDEX);
  199. for (i = 0; i < size; i++)
  200. data[i] = 0xff & inl(fc->addr + AVM_ISACX_DATA);
  201. }
  202. static void
  203. WriteFiFoISAC_V2(void *p, u8 off, u8 *data, int size)
  204. {
  205. struct fritzcard *fc = p;
  206. int i;
  207. outl(off, fc->addr + AVM_ISACX_INDEX);
  208. for (i = 0; i < size; i++)
  209. outl(data[i], fc->addr + AVM_ISACX_DATA);
  210. }
  211. static struct bchannel *
  212. Sel_BCS(struct fritzcard *fc, u32 channel)
  213. {
  214. if (test_bit(FLG_ACTIVE, &fc->bch[0].Flags) &&
  215. (fc->bch[0].nr & channel))
  216. return &fc->bch[0];
  217. else if (test_bit(FLG_ACTIVE, &fc->bch[1].Flags) &&
  218. (fc->bch[1].nr & channel))
  219. return &fc->bch[1];
  220. else
  221. return NULL;
  222. }
  223. static inline void
  224. __write_ctrl_pci(struct fritzcard *fc, struct hdlc_hw *hdlc, u32 channel) {
  225. u32 idx = channel == 2 ? AVM_HDLC_2 : AVM_HDLC_1;
  226. outl(idx, fc->addr + CHIP_INDEX);
  227. outl(hdlc->ctrl.ctrl, fc->addr + CHIP_WINDOW + HDLC_STATUS);
  228. }
  229. static inline void
  230. __write_ctrl_pciv2(struct fritzcard *fc, struct hdlc_hw *hdlc, u32 channel) {
  231. outl(hdlc->ctrl.ctrl, fc->addr + (channel == 2 ? AVM_HDLC_STATUS_2 :
  232. AVM_HDLC_STATUS_1));
  233. }
  234. void
  235. write_ctrl(struct bchannel *bch, int which) {
  236. struct fritzcard *fc = bch->hw;
  237. struct hdlc_hw *hdlc;
  238. hdlc = &fc->hdlc[(bch->nr - 1) & 1];
  239. pr_debug("%s: hdlc %c wr%x ctrl %x\n", fc->name, '@' + bch->nr,
  240. which, hdlc->ctrl.ctrl);
  241. switch (fc->type) {
  242. case AVM_FRITZ_PCIV2:
  243. __write_ctrl_pciv2(fc, hdlc, bch->nr);
  244. break;
  245. case AVM_FRITZ_PCI:
  246. __write_ctrl_pci(fc, hdlc, bch->nr);
  247. break;
  248. }
  249. }
  250. static inline u32
  251. __read_status_pci(u_long addr, u32 channel)
  252. {
  253. outl(channel == 2 ? AVM_HDLC_2 : AVM_HDLC_1, addr + CHIP_INDEX);
  254. return inl(addr + CHIP_WINDOW + HDLC_STATUS);
  255. }
  256. static inline u32
  257. __read_status_pciv2(u_long addr, u32 channel)
  258. {
  259. return inl(addr + (channel == 2 ? AVM_HDLC_STATUS_2 :
  260. AVM_HDLC_STATUS_1));
  261. }
  262. static u32
  263. read_status(struct fritzcard *fc, u32 channel)
  264. {
  265. switch (fc->type) {
  266. case AVM_FRITZ_PCIV2:
  267. return __read_status_pciv2(fc->addr, channel);
  268. case AVM_FRITZ_PCI:
  269. return __read_status_pci(fc->addr, channel);
  270. }
  271. /* dummy */
  272. return 0;
  273. }
  274. static void
  275. enable_hwirq(struct fritzcard *fc)
  276. {
  277. fc->ctrlreg |= AVM_STATUS0_ENA_IRQ;
  278. outb(fc->ctrlreg, fc->addr + 2);
  279. }
  280. static void
  281. disable_hwirq(struct fritzcard *fc)
  282. {
  283. fc->ctrlreg &= ~AVM_STATUS0_ENA_IRQ;
  284. outb(fc->ctrlreg, fc->addr + 2);
  285. }
  286. static int
  287. modehdlc(struct bchannel *bch, int protocol)
  288. {
  289. struct fritzcard *fc = bch->hw;
  290. struct hdlc_hw *hdlc;
  291. hdlc = &fc->hdlc[(bch->nr - 1) & 1];
  292. pr_debug("%s: hdlc %c protocol %x-->%x ch %d\n", fc->name,
  293. '@' + bch->nr, bch->state, protocol, bch->nr);
  294. hdlc->ctrl.ctrl = 0;
  295. switch (protocol) {
  296. case -1: /* used for init */
  297. bch->state = -1;
  298. case ISDN_P_NONE:
  299. if (bch->state == ISDN_P_NONE)
  300. break;
  301. hdlc->ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
  302. hdlc->ctrl.sr.mode = HDLC_MODE_TRANS;
  303. write_ctrl(bch, 5);
  304. bch->state = ISDN_P_NONE;
  305. test_and_clear_bit(FLG_HDLC, &bch->Flags);
  306. test_and_clear_bit(FLG_TRANSPARENT, &bch->Flags);
  307. break;
  308. case ISDN_P_B_RAW:
  309. bch->state = protocol;
  310. hdlc->ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
  311. hdlc->ctrl.sr.mode = HDLC_MODE_TRANS;
  312. write_ctrl(bch, 5);
  313. hdlc->ctrl.sr.cmd = HDLC_CMD_XRS;
  314. write_ctrl(bch, 1);
  315. hdlc->ctrl.sr.cmd = 0;
  316. test_and_set_bit(FLG_TRANSPARENT, &bch->Flags);
  317. break;
  318. case ISDN_P_B_HDLC:
  319. bch->state = protocol;
  320. hdlc->ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
  321. hdlc->ctrl.sr.mode = HDLC_MODE_ITF_FLG;
  322. write_ctrl(bch, 5);
  323. hdlc->ctrl.sr.cmd = HDLC_CMD_XRS;
  324. write_ctrl(bch, 1);
  325. hdlc->ctrl.sr.cmd = 0;
  326. test_and_set_bit(FLG_HDLC, &bch->Flags);
  327. break;
  328. default:
  329. pr_info("%s: protocol not known %x\n", fc->name, protocol);
  330. return -ENOPROTOOPT;
  331. }
  332. return 0;
  333. }
  334. static void
  335. hdlc_empty_fifo(struct bchannel *bch, int count)
  336. {
  337. u32 *ptr;
  338. u8 *p;
  339. u32 val, addr;
  340. int cnt = 0;
  341. struct fritzcard *fc = bch->hw;
  342. pr_debug("%s: %s %d\n", fc->name, __func__, count);
  343. if (!bch->rx_skb) {
  344. bch->rx_skb = mI_alloc_skb(bch->maxlen, GFP_ATOMIC);
  345. if (!bch->rx_skb) {
  346. pr_info("%s: B receive out of memory\n",
  347. fc->name);
  348. return;
  349. }
  350. }
  351. if ((bch->rx_skb->len + count) > bch->maxlen) {
  352. pr_debug("%s: overrun %d\n", fc->name,
  353. bch->rx_skb->len + count);
  354. return;
  355. }
  356. p = skb_put(bch->rx_skb, count);
  357. ptr = (u32 *)p;
  358. if (AVM_FRITZ_PCIV2 == fc->type)
  359. addr = fc->addr + (bch->nr == 2 ?
  360. AVM_HDLC_FIFO_2 : AVM_HDLC_FIFO_1);
  361. else {
  362. addr = fc->addr + CHIP_WINDOW;
  363. outl(bch->nr == 2 ? AVM_HDLC_2 : AVM_HDLC_1, fc->addr);
  364. }
  365. while (cnt < count) {
  366. val = le32_to_cpu(inl(addr));
  367. put_unaligned(val, ptr);
  368. ptr++;
  369. cnt += 4;
  370. }
  371. if (debug & DEBUG_HW_BFIFO) {
  372. snprintf(fc->log, LOG_SIZE, "B%1d-recv %s %d ",
  373. bch->nr, fc->name, count);
  374. print_hex_dump_bytes(fc->log, DUMP_PREFIX_OFFSET, p, count);
  375. }
  376. }
  377. static void
  378. hdlc_fill_fifo(struct bchannel *bch)
  379. {
  380. struct fritzcard *fc = bch->hw;
  381. struct hdlc_hw *hdlc;
  382. int count, cnt = 0;
  383. u8 *p;
  384. u32 *ptr, val, addr;
  385. hdlc = &fc->hdlc[(bch->nr - 1) & 1];
  386. if (!bch->tx_skb)
  387. return;
  388. count = bch->tx_skb->len - bch->tx_idx;
  389. if (count <= 0)
  390. return;
  391. p = bch->tx_skb->data + bch->tx_idx;
  392. hdlc->ctrl.sr.cmd &= ~HDLC_CMD_XME;
  393. if (count > HDLC_FIFO_SIZE) {
  394. count = HDLC_FIFO_SIZE;
  395. } else {
  396. if (test_bit(FLG_HDLC, &bch->Flags))
  397. hdlc->ctrl.sr.cmd |= HDLC_CMD_XME;
  398. }
  399. pr_debug("%s: %s %d/%d/%d", fc->name, __func__, count,
  400. bch->tx_idx, bch->tx_skb->len);
  401. ptr = (u32 *)p;
  402. bch->tx_idx += count;
  403. hdlc->ctrl.sr.xml = ((count == HDLC_FIFO_SIZE) ? 0 : count);
  404. if (AVM_FRITZ_PCIV2 == fc->type) {
  405. __write_ctrl_pciv2(fc, hdlc, bch->nr);
  406. addr = fc->addr + (bch->nr == 2 ?
  407. AVM_HDLC_FIFO_2 : AVM_HDLC_FIFO_1);
  408. } else {
  409. __write_ctrl_pci(fc, hdlc, bch->nr);
  410. addr = fc->addr + CHIP_WINDOW;
  411. }
  412. while (cnt < count) {
  413. val = get_unaligned(ptr);
  414. outl(cpu_to_le32(val), addr);
  415. ptr++;
  416. cnt += 4;
  417. }
  418. if (debug & DEBUG_HW_BFIFO) {
  419. snprintf(fc->log, LOG_SIZE, "B%1d-send %s %d ",
  420. bch->nr, fc->name, count);
  421. print_hex_dump_bytes(fc->log, DUMP_PREFIX_OFFSET, p, count);
  422. }
  423. }
  424. static void
  425. HDLC_irq_xpr(struct bchannel *bch)
  426. {
  427. if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len)
  428. hdlc_fill_fifo(bch);
  429. else {
  430. if (bch->tx_skb) {
  431. /* send confirm, on trans, free on hdlc. */
  432. if (test_bit(FLG_TRANSPARENT, &bch->Flags))
  433. confirm_Bsend(bch);
  434. dev_kfree_skb(bch->tx_skb);
  435. }
  436. if (get_next_bframe(bch))
  437. hdlc_fill_fifo(bch);
  438. }
  439. }
  440. static void
  441. HDLC_irq(struct bchannel *bch, u32 stat)
  442. {
  443. struct fritzcard *fc = bch->hw;
  444. int len;
  445. struct hdlc_hw *hdlc;
  446. hdlc = &fc->hdlc[(bch->nr - 1) & 1];
  447. pr_debug("%s: ch%d stat %#x\n", fc->name, bch->nr, stat);
  448. if (stat & HDLC_INT_RPR) {
  449. if (stat & HDLC_STAT_RDO) {
  450. hdlc->ctrl.sr.xml = 0;
  451. hdlc->ctrl.sr.cmd |= HDLC_CMD_RRS;
  452. write_ctrl(bch, 1);
  453. hdlc->ctrl.sr.cmd &= ~HDLC_CMD_RRS;
  454. write_ctrl(bch, 1);
  455. if (bch->rx_skb)
  456. skb_trim(bch->rx_skb, 0);
  457. } else {
  458. len = (stat & HDLC_STAT_RML_MASK) >> 8;
  459. if (!len)
  460. len = 32;
  461. hdlc_empty_fifo(bch, len);
  462. if (!bch->rx_skb)
  463. goto handle_tx;
  464. if ((stat & HDLC_STAT_RME) || test_bit(FLG_TRANSPARENT,
  465. &bch->Flags)) {
  466. if (((stat & HDLC_STAT_CRCVFRRAB) ==
  467. HDLC_STAT_CRCVFR) ||
  468. test_bit(FLG_TRANSPARENT, &bch->Flags)) {
  469. recv_Bchannel(bch, 0);
  470. } else {
  471. pr_debug("%s: got invalid frame\n",
  472. fc->name);
  473. skb_trim(bch->rx_skb, 0);
  474. }
  475. }
  476. }
  477. }
  478. handle_tx:
  479. if (stat & HDLC_INT_XDU) {
  480. /* Here we lost an TX interrupt, so
  481. * restart transmitting the whole frame on HDLC
  482. * in transparent mode we send the next data
  483. */
  484. if (bch->tx_skb)
  485. pr_debug("%s: ch%d XDU len(%d) idx(%d) Flags(%lx)\n",
  486. fc->name, bch->nr, bch->tx_skb->len,
  487. bch->tx_idx, bch->Flags);
  488. else
  489. pr_debug("%s: ch%d XDU no tx_skb Flags(%lx)\n",
  490. fc->name, bch->nr, bch->Flags);
  491. if (bch->tx_skb && bch->tx_skb->len) {
  492. if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
  493. bch->tx_idx = 0;
  494. }
  495. hdlc->ctrl.sr.xml = 0;
  496. hdlc->ctrl.sr.cmd |= HDLC_CMD_XRS;
  497. write_ctrl(bch, 1);
  498. hdlc->ctrl.sr.cmd &= ~HDLC_CMD_XRS;
  499. HDLC_irq_xpr(bch);
  500. return;
  501. } else if (stat & HDLC_INT_XPR)
  502. HDLC_irq_xpr(bch);
  503. }
  504. static inline void
  505. HDLC_irq_main(struct fritzcard *fc)
  506. {
  507. u32 stat;
  508. struct bchannel *bch;
  509. stat = read_status(fc, 1);
  510. if (stat & HDLC_INT_MASK) {
  511. bch = Sel_BCS(fc, 1);
  512. if (bch)
  513. HDLC_irq(bch, stat);
  514. else
  515. pr_debug("%s: spurious ch1 IRQ\n", fc->name);
  516. }
  517. stat = read_status(fc, 2);
  518. if (stat & HDLC_INT_MASK) {
  519. bch = Sel_BCS(fc, 2);
  520. if (bch)
  521. HDLC_irq(bch, stat);
  522. else
  523. pr_debug("%s: spurious ch2 IRQ\n", fc->name);
  524. }
  525. }
  526. static irqreturn_t
  527. avm_fritz_interrupt(int intno, void *dev_id)
  528. {
  529. struct fritzcard *fc = dev_id;
  530. u8 val;
  531. u8 sval;
  532. spin_lock(&fc->lock);
  533. sval = inb(fc->addr + 2);
  534. pr_debug("%s: irq stat0 %x\n", fc->name, sval);
  535. if ((sval & AVM_STATUS0_IRQ_MASK) == AVM_STATUS0_IRQ_MASK) {
  536. /* shared IRQ from other HW */
  537. spin_unlock(&fc->lock);
  538. return IRQ_NONE;
  539. }
  540. fc->irqcnt++;
  541. if (!(sval & AVM_STATUS0_IRQ_ISAC)) {
  542. val = ReadISAC_V1(fc, ISAC_ISTA);
  543. mISDNisac_irq(&fc->isac, val);
  544. }
  545. if (!(sval & AVM_STATUS0_IRQ_HDLC))
  546. HDLC_irq_main(fc);
  547. spin_unlock(&fc->lock);
  548. return IRQ_HANDLED;
  549. }
  550. static irqreturn_t
  551. avm_fritzv2_interrupt(int intno, void *dev_id)
  552. {
  553. struct fritzcard *fc = dev_id;
  554. u8 val;
  555. u8 sval;
  556. spin_lock(&fc->lock);
  557. sval = inb(fc->addr + 2);
  558. pr_debug("%s: irq stat0 %x\n", fc->name, sval);
  559. if (!(sval & AVM_STATUS0_IRQ_MASK)) {
  560. /* shared IRQ from other HW */
  561. spin_unlock(&fc->lock);
  562. return IRQ_NONE;
  563. }
  564. fc->irqcnt++;
  565. if (sval & AVM_STATUS0_IRQ_HDLC)
  566. HDLC_irq_main(fc);
  567. if (sval & AVM_STATUS0_IRQ_ISAC) {
  568. val = ReadISAC_V2(fc, ISACX_ISTA);
  569. mISDNisac_irq(&fc->isac, val);
  570. }
  571. if (sval & AVM_STATUS0_IRQ_TIMER) {
  572. pr_debug("%s: timer irq\n", fc->name);
  573. outb(fc->ctrlreg | AVM_STATUS0_RES_TIMER, fc->addr + 2);
  574. udelay(1);
  575. outb(fc->ctrlreg, fc->addr + 2);
  576. }
  577. spin_unlock(&fc->lock);
  578. return IRQ_HANDLED;
  579. }
  580. static int
  581. avm_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
  582. {
  583. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  584. struct fritzcard *fc = bch->hw;
  585. int ret = -EINVAL;
  586. struct mISDNhead *hh = mISDN_HEAD_P(skb);
  587. u32 id;
  588. u_long flags;
  589. switch (hh->prim) {
  590. case PH_DATA_REQ:
  591. spin_lock_irqsave(&fc->lock, flags);
  592. ret = bchannel_senddata(bch, skb);
  593. if (ret > 0) { /* direct TX */
  594. id = hh->id; /* skb can be freed */
  595. hdlc_fill_fifo(bch);
  596. ret = 0;
  597. spin_unlock_irqrestore(&fc->lock, flags);
  598. if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
  599. queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
  600. } else
  601. spin_unlock_irqrestore(&fc->lock, flags);
  602. return ret;
  603. case PH_ACTIVATE_REQ:
  604. spin_lock_irqsave(&fc->lock, flags);
  605. if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
  606. ret = modehdlc(bch, ch->protocol);
  607. else
  608. ret = 0;
  609. spin_unlock_irqrestore(&fc->lock, flags);
  610. if (!ret)
  611. _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
  612. NULL, GFP_KERNEL);
  613. break;
  614. case PH_DEACTIVATE_REQ:
  615. spin_lock_irqsave(&fc->lock, flags);
  616. mISDN_clear_bchannel(bch);
  617. modehdlc(bch, ISDN_P_NONE);
  618. spin_unlock_irqrestore(&fc->lock, flags);
  619. _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
  620. NULL, GFP_KERNEL);
  621. ret = 0;
  622. break;
  623. }
  624. if (!ret)
  625. dev_kfree_skb(skb);
  626. return ret;
  627. }
  628. static void
  629. inithdlc(struct fritzcard *fc)
  630. {
  631. modehdlc(&fc->bch[0], -1);
  632. modehdlc(&fc->bch[1], -1);
  633. }
  634. void
  635. clear_pending_hdlc_ints(struct fritzcard *fc)
  636. {
  637. u32 val;
  638. val = read_status(fc, 1);
  639. pr_debug("%s: HDLC 1 STA %x\n", fc->name, val);
  640. val = read_status(fc, 2);
  641. pr_debug("%s: HDLC 2 STA %x\n", fc->name, val);
  642. }
  643. static void
  644. reset_avm(struct fritzcard *fc)
  645. {
  646. switch (fc->type) {
  647. case AVM_FRITZ_PCI:
  648. fc->ctrlreg = AVM_STATUS0_RESET | AVM_STATUS0_DIS_TIMER;
  649. break;
  650. case AVM_FRITZ_PCIV2:
  651. fc->ctrlreg = AVM_STATUS0_RESET;
  652. break;
  653. }
  654. if (debug & DEBUG_HW)
  655. pr_notice("%s: reset\n", fc->name);
  656. disable_hwirq(fc);
  657. mdelay(5);
  658. switch (fc->type) {
  659. case AVM_FRITZ_PCI:
  660. fc->ctrlreg = AVM_STATUS0_DIS_TIMER | AVM_STATUS0_RES_TIMER;
  661. disable_hwirq(fc);
  662. outb(AVM_STATUS1_ENA_IOM, fc->addr + 3);
  663. break;
  664. case AVM_FRITZ_PCIV2:
  665. fc->ctrlreg = 0;
  666. disable_hwirq(fc);
  667. break;
  668. }
  669. mdelay(1);
  670. if (debug & DEBUG_HW)
  671. pr_notice("%s: S0/S1 %x/%x\n", fc->name,
  672. inb(fc->addr + 2), inb(fc->addr + 3));
  673. }
  674. static int
  675. init_card(struct fritzcard *fc)
  676. {
  677. int ret, cnt = 3;
  678. u_long flags;
  679. reset_avm(fc); /* disable IRQ */
  680. if (fc->type == AVM_FRITZ_PCIV2)
  681. ret = request_irq(fc->irq, avm_fritzv2_interrupt,
  682. IRQF_SHARED, fc->name, fc);
  683. else
  684. ret = request_irq(fc->irq, avm_fritz_interrupt,
  685. IRQF_SHARED, fc->name, fc);
  686. if (ret) {
  687. pr_info("%s: couldn't get interrupt %d\n",
  688. fc->name, fc->irq);
  689. return ret;
  690. }
  691. while (cnt--) {
  692. spin_lock_irqsave(&fc->lock, flags);
  693. ret = fc->isac.init(&fc->isac);
  694. if (ret) {
  695. spin_unlock_irqrestore(&fc->lock, flags);
  696. pr_info("%s: ISAC init failed with %d\n",
  697. fc->name, ret);
  698. break;
  699. }
  700. clear_pending_hdlc_ints(fc);
  701. inithdlc(fc);
  702. enable_hwirq(fc);
  703. /* RESET Receiver and Transmitter */
  704. if (AVM_FRITZ_PCIV2 == fc->type) {
  705. WriteISAC_V2(fc, ISACX_MASK, 0);
  706. WriteISAC_V2(fc, ISACX_CMDRD, 0x41);
  707. } else {
  708. WriteISAC_V1(fc, ISAC_MASK, 0);
  709. WriteISAC_V1(fc, ISAC_CMDR, 0x41);
  710. }
  711. spin_unlock_irqrestore(&fc->lock, flags);
  712. /* Timeout 10ms */
  713. msleep_interruptible(10);
  714. if (debug & DEBUG_HW)
  715. pr_notice("%s: IRQ %d count %d\n", fc->name,
  716. fc->irq, fc->irqcnt);
  717. if (!fc->irqcnt) {
  718. pr_info("%s: IRQ(%d) getting no IRQs during init %d\n",
  719. fc->name, fc->irq, 3 - cnt);
  720. reset_avm(fc);
  721. } else
  722. return 0;
  723. }
  724. free_irq(fc->irq, fc);
  725. return -EIO;
  726. }
  727. static int
  728. channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
  729. {
  730. int ret = 0;
  731. struct fritzcard *fc = bch->hw;
  732. switch (cq->op) {
  733. case MISDN_CTRL_GETOP:
  734. cq->op = 0;
  735. break;
  736. /* Nothing implemented yet */
  737. case MISDN_CTRL_FILL_EMPTY:
  738. default:
  739. pr_info("%s: %s unknown Op %x\n", fc->name, __func__, cq->op);
  740. ret = -EINVAL;
  741. break;
  742. }
  743. return ret;
  744. }
  745. static int
  746. avm_bctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
  747. {
  748. struct bchannel *bch = container_of(ch, struct bchannel, ch);
  749. struct fritzcard *fc = bch->hw;
  750. int ret = -EINVAL;
  751. u_long flags;
  752. pr_debug("%s: %s cmd:%x %p\n", fc->name, __func__, cmd, arg);
  753. switch (cmd) {
  754. case CLOSE_CHANNEL:
  755. test_and_clear_bit(FLG_OPEN, &bch->Flags);
  756. if (test_bit(FLG_ACTIVE, &bch->Flags)) {
  757. spin_lock_irqsave(&fc->lock, flags);
  758. mISDN_freebchannel(bch);
  759. test_and_clear_bit(FLG_TX_BUSY, &bch->Flags);
  760. test_and_clear_bit(FLG_ACTIVE, &bch->Flags);
  761. modehdlc(bch, ISDN_P_NONE);
  762. spin_unlock_irqrestore(&fc->lock, flags);
  763. }
  764. ch->protocol = ISDN_P_NONE;
  765. ch->peer = NULL;
  766. module_put(THIS_MODULE);
  767. ret = 0;
  768. break;
  769. case CONTROL_CHANNEL:
  770. ret = channel_bctrl(bch, arg);
  771. break;
  772. default:
  773. pr_info("%s: %s unknown prim(%x)\n", fc->name, __func__, cmd);
  774. }
  775. return ret;
  776. }
  777. static int
  778. channel_ctrl(struct fritzcard *fc, struct mISDN_ctrl_req *cq)
  779. {
  780. int ret = 0;
  781. switch (cq->op) {
  782. case MISDN_CTRL_GETOP:
  783. cq->op = MISDN_CTRL_LOOP;
  784. break;
  785. case MISDN_CTRL_LOOP:
  786. /* cq->channel: 0 disable, 1 B1 loop 2 B2 loop, 3 both */
  787. if (cq->channel < 0 || cq->channel > 3) {
  788. ret = -EINVAL;
  789. break;
  790. }
  791. ret = fc->isac.ctrl(&fc->isac, HW_TESTLOOP, cq->channel);
  792. break;
  793. default:
  794. pr_info("%s: %s unknown Op %x\n", fc->name, __func__, cq->op);
  795. ret = -EINVAL;
  796. break;
  797. }
  798. return ret;
  799. }
  800. static int
  801. open_bchannel(struct fritzcard *fc, struct channel_req *rq)
  802. {
  803. struct bchannel *bch;
  804. if (rq->adr.channel > 2)
  805. return -EINVAL;
  806. if (rq->protocol == ISDN_P_NONE)
  807. return -EINVAL;
  808. bch = &fc->bch[rq->adr.channel - 1];
  809. if (test_and_set_bit(FLG_OPEN, &bch->Flags))
  810. return -EBUSY; /* b-channel can be only open once */
  811. test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
  812. bch->ch.protocol = rq->protocol;
  813. rq->ch = &bch->ch;
  814. return 0;
  815. }
  816. /*
  817. * device control function
  818. */
  819. static int
  820. avm_dctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
  821. {
  822. struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
  823. struct dchannel *dch = container_of(dev, struct dchannel, dev);
  824. struct fritzcard *fc = dch->hw;
  825. struct channel_req *rq;
  826. int err = 0;
  827. pr_debug("%s: %s cmd:%x %p\n", fc->name, __func__, cmd, arg);
  828. switch (cmd) {
  829. case OPEN_CHANNEL:
  830. rq = arg;
  831. if (rq->protocol == ISDN_P_TE_S0)
  832. err = fc->isac.open(&fc->isac, rq);
  833. else
  834. err = open_bchannel(fc, rq);
  835. if (err)
  836. break;
  837. if (!try_module_get(THIS_MODULE))
  838. pr_info("%s: cannot get module\n", fc->name);
  839. break;
  840. case CLOSE_CHANNEL:
  841. pr_debug("%s: dev(%d) close from %p\n", fc->name, dch->dev.id,
  842. __builtin_return_address(0));
  843. module_put(THIS_MODULE);
  844. break;
  845. case CONTROL_CHANNEL:
  846. err = channel_ctrl(fc, arg);
  847. break;
  848. default:
  849. pr_debug("%s: %s unknown command %x\n",
  850. fc->name, __func__, cmd);
  851. return -EINVAL;
  852. }
  853. return err;
  854. }
  855. int
  856. setup_fritz(struct fritzcard *fc)
  857. {
  858. u32 val, ver;
  859. if (!request_region(fc->addr, 32, fc->name)) {
  860. pr_info("%s: AVM config port %x-%x already in use\n",
  861. fc->name, fc->addr, fc->addr + 31);
  862. return -EIO;
  863. }
  864. switch (fc->type) {
  865. case AVM_FRITZ_PCI:
  866. val = inl(fc->addr);
  867. outl(AVM_HDLC_1, fc->addr + CHIP_INDEX);
  868. ver = inl(fc->addr + CHIP_WINDOW + HDLC_STATUS) >> 24;
  869. if (debug & DEBUG_HW) {
  870. pr_notice("%s: PCI stat %#x\n", fc->name, val);
  871. pr_notice("%s: PCI Class %X Rev %d\n", fc->name,
  872. val & 0xff, (val >> 8) & 0xff);
  873. pr_notice("%s: HDLC version %x\n", fc->name, ver & 0xf);
  874. }
  875. ASSIGN_FUNC(V1, ISAC, fc->isac);
  876. fc->isac.type = IPAC_TYPE_ISAC;
  877. break;
  878. case AVM_FRITZ_PCIV2:
  879. val = inl(fc->addr);
  880. ver = inl(fc->addr + AVM_HDLC_STATUS_1) >> 24;
  881. if (debug & DEBUG_HW) {
  882. pr_notice("%s: PCI V2 stat %#x\n", fc->name, val);
  883. pr_notice("%s: PCI V2 Class %X Rev %d\n", fc->name,
  884. val & 0xff, (val>>8) & 0xff);
  885. pr_notice("%s: HDLC version %x\n", fc->name, ver & 0xf);
  886. }
  887. ASSIGN_FUNC(V2, ISAC, fc->isac);
  888. fc->isac.type = IPAC_TYPE_ISACX;
  889. break;
  890. default:
  891. release_region(fc->addr, 32);
  892. pr_info("%s: AVM unknown type %d\n", fc->name, fc->type);
  893. return -ENODEV;
  894. }
  895. pr_notice("%s: %s config irq:%d base:0x%X\n", fc->name,
  896. (fc->type == AVM_FRITZ_PCI) ? "AVM Fritz!CARD PCI" :
  897. "AVM Fritz!CARD PCIv2", fc->irq, fc->addr);
  898. return 0;
  899. }
  900. static void
  901. release_card(struct fritzcard *card)
  902. {
  903. u_long flags;
  904. disable_hwirq(card);
  905. spin_lock_irqsave(&card->lock, flags);
  906. modehdlc(&card->bch[0], ISDN_P_NONE);
  907. modehdlc(&card->bch[1], ISDN_P_NONE);
  908. spin_unlock_irqrestore(&card->lock, flags);
  909. card->isac.release(&card->isac);
  910. free_irq(card->irq, card);
  911. mISDN_freebchannel(&card->bch[1]);
  912. mISDN_freebchannel(&card->bch[0]);
  913. mISDN_unregister_device(&card->isac.dch.dev);
  914. release_region(card->addr, 32);
  915. pci_disable_device(card->pdev);
  916. pci_set_drvdata(card->pdev, NULL);
  917. write_lock_irqsave(&card_lock, flags);
  918. list_del(&card->list);
  919. write_unlock_irqrestore(&card_lock, flags);
  920. kfree(card);
  921. AVM_cnt--;
  922. }
  923. static int __devinit
  924. setup_instance(struct fritzcard *card)
  925. {
  926. int i, err;
  927. u_long flags;
  928. snprintf(card->name, MISDN_MAX_IDLEN - 1, "AVM.%d", AVM_cnt + 1);
  929. write_lock_irqsave(&card_lock, flags);
  930. list_add_tail(&card->list, &Cards);
  931. write_unlock_irqrestore(&card_lock, flags);
  932. _set_debug(card);
  933. card->isac.name = card->name;
  934. spin_lock_init(&card->lock);
  935. card->isac.hwlock = &card->lock;
  936. mISDNisac_init(&card->isac, card);
  937. card->isac.dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
  938. (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
  939. card->isac.dch.dev.D.ctrl = avm_dctrl;
  940. for (i = 0; i < 2; i++) {
  941. card->bch[i].nr = i + 1;
  942. set_channelmap(i + 1, card->isac.dch.dev.channelmap);
  943. mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM);
  944. card->bch[i].hw = card;
  945. card->bch[i].ch.send = avm_l2l1B;
  946. card->bch[i].ch.ctrl = avm_bctrl;
  947. card->bch[i].ch.nr = i + 1;
  948. list_add(&card->bch[i].ch.list, &card->isac.dch.dev.bchannels);
  949. }
  950. err = setup_fritz(card);
  951. if (err)
  952. goto error;
  953. err = mISDN_register_device(&card->isac.dch.dev, &card->pdev->dev,
  954. card->name);
  955. if (err)
  956. goto error_reg;
  957. err = init_card(card);
  958. if (!err) {
  959. AVM_cnt++;
  960. pr_notice("AVM %d cards installed DEBUG\n", AVM_cnt);
  961. return 0;
  962. }
  963. mISDN_unregister_device(&card->isac.dch.dev);
  964. error_reg:
  965. release_region(card->addr, 32);
  966. error:
  967. card->isac.release(&card->isac);
  968. mISDN_freebchannel(&card->bch[1]);
  969. mISDN_freebchannel(&card->bch[0]);
  970. write_lock_irqsave(&card_lock, flags);
  971. list_del(&card->list);
  972. write_unlock_irqrestore(&card_lock, flags);
  973. kfree(card);
  974. return err;
  975. }
  976. static int __devinit
  977. fritzpci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  978. {
  979. int err = -ENOMEM;
  980. struct fritzcard *card;
  981. card = kzalloc(sizeof(struct fritzcard), GFP_KERNEL);
  982. if (!card) {
  983. pr_info("No kmem for fritzcard\n");
  984. return err;
  985. }
  986. if (pdev->device == PCI_DEVICE_ID_AVM_A1_V2)
  987. card->type = AVM_FRITZ_PCIV2;
  988. else
  989. card->type = AVM_FRITZ_PCI;
  990. card->pdev = pdev;
  991. err = pci_enable_device(pdev);
  992. if (err) {
  993. kfree(card);
  994. return err;
  995. }
  996. pr_notice("mISDN: found adapter %s at %s\n",
  997. (char *) ent->driver_data, pci_name(pdev));
  998. card->addr = pci_resource_start(pdev, 1);
  999. card->irq = pdev->irq;
  1000. pci_set_drvdata(pdev, card);
  1001. err = setup_instance(card);
  1002. if (err)
  1003. pci_set_drvdata(pdev, NULL);
  1004. return err;
  1005. }
  1006. static void __devexit
  1007. fritz_remove_pci(struct pci_dev *pdev)
  1008. {
  1009. struct fritzcard *card = pci_get_drvdata(pdev);
  1010. if (card)
  1011. release_card(card);
  1012. else
  1013. if (debug)
  1014. pr_info("%s: drvdata already removed\n", __func__);
  1015. }
  1016. static struct pci_device_id fcpci_ids[] __devinitdata = {
  1017. { PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_A1, PCI_ANY_ID, PCI_ANY_ID,
  1018. 0, 0, (unsigned long) "Fritz!Card PCI"},
  1019. { PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_A1_V2, PCI_ANY_ID, PCI_ANY_ID,
  1020. 0, 0, (unsigned long) "Fritz!Card PCI v2" },
  1021. { }
  1022. };
  1023. MODULE_DEVICE_TABLE(pci, fcpci_ids);
  1024. static struct pci_driver fcpci_driver = {
  1025. .name = "fcpci",
  1026. .probe = fritzpci_probe,
  1027. .remove = __devexit_p(fritz_remove_pci),
  1028. .id_table = fcpci_ids,
  1029. };
  1030. static int __init AVM_init(void)
  1031. {
  1032. int err;
  1033. pr_notice("AVM Fritz PCI driver Rev. %s\n", AVMFRITZ_REV);
  1034. err = pci_register_driver(&fcpci_driver);
  1035. return err;
  1036. }
  1037. static void __exit AVM_cleanup(void)
  1038. {
  1039. pci_unregister_driver(&fcpci_driver);
  1040. }
  1041. module_init(AVM_init);
  1042. module_exit(AVM_cleanup);