blob: 7cd3a963ed2e70a6595e5672e52bbd111fe66842 [file] [log] [blame]
Karsten Keil6115d2f2009-07-22 19:52:24 +02001/*
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 */
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000023#include <linux/interrupt.h>
Karsten Keil6115d2f2009-07-22 19:52:24 +020024#include <linux/module.h>
25#include <linux/pci.h>
26#include <linux/delay.h>
27#include <linux/mISDNhw.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Karsten Keil6115d2f2009-07-22 19:52:24 +020029#include <asm/unaligned.h>
30#include "ipac.h"
31
32
Karsten Keil09e79a772012-05-15 23:51:03 +000033#define AVMFRITZ_REV "2.2"
Karsten Keil6115d2f2009-07-22 19:52:24 +020034
35static int AVM_cnt;
36static int debug;
37
38enum {
39 AVM_FRITZ_PCI,
40 AVM_FRITZ_PCIV2,
41};
42
43#define HDLC_FIFO 0x0
44#define HDLC_STATUS 0x4
45#define CHIP_WINDOW 0x10
46
47#define CHIP_INDEX 0x4
48#define AVM_HDLC_1 0x00
49#define AVM_HDLC_2 0x01
50#define AVM_ISAC_FIFO 0x02
51#define AVM_ISAC_REG_LOW 0x04
52#define AVM_ISAC_REG_HIGH 0x06
53
54#define AVM_STATUS0_IRQ_ISAC 0x01
55#define AVM_STATUS0_IRQ_HDLC 0x02
56#define AVM_STATUS0_IRQ_TIMER 0x04
57#define AVM_STATUS0_IRQ_MASK 0x07
58
59#define AVM_STATUS0_RESET 0x01
60#define AVM_STATUS0_DIS_TIMER 0x02
61#define AVM_STATUS0_RES_TIMER 0x04
62#define AVM_STATUS0_ENA_IRQ 0x08
63#define AVM_STATUS0_TESTBIT 0x10
64
65#define AVM_STATUS1_INT_SEL 0x0f
66#define AVM_STATUS1_ENA_IOM 0x80
67
68#define HDLC_MODE_ITF_FLG 0x01
69#define HDLC_MODE_TRANS 0x02
70#define HDLC_MODE_CCR_7 0x04
71#define HDLC_MODE_CCR_16 0x08
Karsten Keil09e79a772012-05-15 23:51:03 +000072#define HDLC_FIFO_SIZE_128 0x20
Karsten Keil6115d2f2009-07-22 19:52:24 +020073#define HDLC_MODE_TESTLOOP 0x80
74
75#define HDLC_INT_XPR 0x80
76#define HDLC_INT_XDU 0x40
77#define HDLC_INT_RPR 0x20
78#define HDLC_INT_MASK 0xE0
79
80#define HDLC_STAT_RME 0x01
81#define HDLC_STAT_RDO 0x10
82#define HDLC_STAT_CRCVFRRAB 0x0E
83#define HDLC_STAT_CRCVFR 0x06
Karsten Keil09e79a772012-05-15 23:51:03 +000084#define HDLC_STAT_RML_MASK_V1 0x3f00
85#define HDLC_STAT_RML_MASK_V2 0x7f00
Karsten Keil6115d2f2009-07-22 19:52:24 +020086
87#define HDLC_CMD_XRS 0x80
88#define HDLC_CMD_XME 0x01
89#define HDLC_CMD_RRS 0x20
90#define HDLC_CMD_XML_MASK 0x3f00
Karsten Keil09e79a772012-05-15 23:51:03 +000091
92#define HDLC_FIFO_SIZE_V1 32
93#define HDLC_FIFO_SIZE_V2 128
Karsten Keil6115d2f2009-07-22 19:52:24 +020094
95/* Fritz PCI v2.0 */
96
97#define AVM_HDLC_FIFO_1 0x10
98#define AVM_HDLC_FIFO_2 0x18
99
100#define AVM_HDLC_STATUS_1 0x14
101#define AVM_HDLC_STATUS_2 0x1c
102
103#define AVM_ISACX_INDEX 0x04
104#define AVM_ISACX_DATA 0x08
105
106/* data struct */
107#define LOG_SIZE 63
108
109struct hdlc_stat_reg {
110#ifdef __BIG_ENDIAN
111 u8 fill;
112 u8 mode;
113 u8 xml;
114 u8 cmd;
115#else
116 u8 cmd;
117 u8 xml;
118 u8 mode;
119 u8 fill;
120#endif
121} __attribute__((packed));
122
123struct hdlc_hw {
124 union {
125 u32 ctrl;
126 struct hdlc_stat_reg sr;
127 } ctrl;
128 u32 stat;
129};
130
131struct fritzcard {
132 struct list_head list;
133 struct pci_dev *pdev;
134 char name[MISDN_MAX_IDLEN];
135 u8 type;
136 u8 ctrlreg;
137 u16 irq;
138 u32 irqcnt;
139 u32 addr;
140 spinlock_t lock; /* hw lock */
141 struct isac_hw isac;
142 struct hdlc_hw hdlc[2];
143 struct bchannel bch[2];
144 char log[LOG_SIZE + 1];
145};
146
147static LIST_HEAD(Cards);
148static DEFINE_RWLOCK(card_lock); /* protect Cards */
149
150static void
151_set_debug(struct fritzcard *card)
152{
153 card->isac.dch.debug = debug;
154 card->bch[0].debug = debug;
155 card->bch[1].debug = debug;
156}
157
158static int
159set_debug(const char *val, struct kernel_param *kp)
160{
161 int ret;
162 struct fritzcard *card;
163
164 ret = param_set_uint(val, kp);
165 if (!ret) {
166 read_lock(&card_lock);
167 list_for_each_entry(card, &Cards, list)
168 _set_debug(card);
169 read_unlock(&card_lock);
170 }
171 return ret;
172}
173
174MODULE_AUTHOR("Karsten Keil");
175MODULE_LICENSE("GPL v2");
176MODULE_VERSION(AVMFRITZ_REV);
177module_param_call(debug, set_debug, param_get_uint, &debug, S_IRUGO | S_IWUSR);
178MODULE_PARM_DESC(debug, "avmfritz debug mask");
179
180/* Interface functions */
181
182static u8
183ReadISAC_V1(void *p, u8 offset)
184{
185 struct fritzcard *fc = p;
186 u8 idx = (offset > 0x2f) ? AVM_ISAC_REG_HIGH : AVM_ISAC_REG_LOW;
187
188 outb(idx, fc->addr + CHIP_INDEX);
189 return inb(fc->addr + CHIP_WINDOW + (offset & 0xf));
190}
191
192static void
193WriteISAC_V1(void *p, u8 offset, u8 value)
194{
195 struct fritzcard *fc = p;
196 u8 idx = (offset > 0x2f) ? AVM_ISAC_REG_HIGH : AVM_ISAC_REG_LOW;
197
198 outb(idx, fc->addr + CHIP_INDEX);
199 outb(value, fc->addr + CHIP_WINDOW + (offset & 0xf));
200}
201
202static void
203ReadFiFoISAC_V1(void *p, u8 off, u8 *data, int size)
204{
205 struct fritzcard *fc = p;
206
207 outb(AVM_ISAC_FIFO, fc->addr + CHIP_INDEX);
208 insb(fc->addr + CHIP_WINDOW, data, size);
209}
210
211static void
212WriteFiFoISAC_V1(void *p, u8 off, u8 *data, int size)
213{
214 struct fritzcard *fc = p;
215
216 outb(AVM_ISAC_FIFO, fc->addr + CHIP_INDEX);
217 outsb(fc->addr + CHIP_WINDOW, data, size);
218}
219
220static u8
221ReadISAC_V2(void *p, u8 offset)
222{
223 struct fritzcard *fc = p;
224
225 outl(offset, fc->addr + AVM_ISACX_INDEX);
226 return 0xff & inl(fc->addr + AVM_ISACX_DATA);
227}
228
229static void
230WriteISAC_V2(void *p, u8 offset, u8 value)
231{
232 struct fritzcard *fc = p;
233
234 outl(offset, fc->addr + AVM_ISACX_INDEX);
235 outl(value, fc->addr + AVM_ISACX_DATA);
236}
237
238static void
239ReadFiFoISAC_V2(void *p, u8 off, u8 *data, int size)
240{
241 struct fritzcard *fc = p;
242 int i;
243
244 outl(off, fc->addr + AVM_ISACX_INDEX);
245 for (i = 0; i < size; i++)
246 data[i] = 0xff & inl(fc->addr + AVM_ISACX_DATA);
247}
248
249static void
250WriteFiFoISAC_V2(void *p, u8 off, u8 *data, int size)
251{
252 struct fritzcard *fc = p;
253 int i;
254
255 outl(off, fc->addr + AVM_ISACX_INDEX);
256 for (i = 0; i < size; i++)
257 outl(data[i], fc->addr + AVM_ISACX_DATA);
258}
259
260static struct bchannel *
261Sel_BCS(struct fritzcard *fc, u32 channel)
262{
263 if (test_bit(FLG_ACTIVE, &fc->bch[0].Flags) &&
Joe Perches475be4d2012-02-19 19:52:38 -0800264 (fc->bch[0].nr & channel))
Karsten Keil6115d2f2009-07-22 19:52:24 +0200265 return &fc->bch[0];
266 else if (test_bit(FLG_ACTIVE, &fc->bch[1].Flags) &&
Joe Perches475be4d2012-02-19 19:52:38 -0800267 (fc->bch[1].nr & channel))
Karsten Keil6115d2f2009-07-22 19:52:24 +0200268 return &fc->bch[1];
269 else
270 return NULL;
271}
272
273static inline void
274__write_ctrl_pci(struct fritzcard *fc, struct hdlc_hw *hdlc, u32 channel) {
275 u32 idx = channel == 2 ? AVM_HDLC_2 : AVM_HDLC_1;
276
277 outl(idx, fc->addr + CHIP_INDEX);
278 outl(hdlc->ctrl.ctrl, fc->addr + CHIP_WINDOW + HDLC_STATUS);
279}
280
281static inline void
282__write_ctrl_pciv2(struct fritzcard *fc, struct hdlc_hw *hdlc, u32 channel) {
283 outl(hdlc->ctrl.ctrl, fc->addr + (channel == 2 ? AVM_HDLC_STATUS_2 :
Joe Perches475be4d2012-02-19 19:52:38 -0800284 AVM_HDLC_STATUS_1));
Karsten Keil6115d2f2009-07-22 19:52:24 +0200285}
286
287void
288write_ctrl(struct bchannel *bch, int which) {
289 struct fritzcard *fc = bch->hw;
290 struct hdlc_hw *hdlc;
291
292 hdlc = &fc->hdlc[(bch->nr - 1) & 1];
293 pr_debug("%s: hdlc %c wr%x ctrl %x\n", fc->name, '@' + bch->nr,
Joe Perches475be4d2012-02-19 19:52:38 -0800294 which, hdlc->ctrl.ctrl);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200295 switch (fc->type) {
296 case AVM_FRITZ_PCIV2:
297 __write_ctrl_pciv2(fc, hdlc, bch->nr);
298 break;
299 case AVM_FRITZ_PCI:
300 __write_ctrl_pci(fc, hdlc, bch->nr);
301 break;
302 }
303}
304
305
306static inline u32
307__read_status_pci(u_long addr, u32 channel)
308{
309 outl(channel == 2 ? AVM_HDLC_2 : AVM_HDLC_1, addr + CHIP_INDEX);
310 return inl(addr + CHIP_WINDOW + HDLC_STATUS);
311}
312
313static inline u32
314__read_status_pciv2(u_long addr, u32 channel)
315{
316 return inl(addr + (channel == 2 ? AVM_HDLC_STATUS_2 :
Joe Perches475be4d2012-02-19 19:52:38 -0800317 AVM_HDLC_STATUS_1));
Karsten Keil6115d2f2009-07-22 19:52:24 +0200318}
319
320
321static u32
322read_status(struct fritzcard *fc, u32 channel)
323{
324 switch (fc->type) {
325 case AVM_FRITZ_PCIV2:
326 return __read_status_pciv2(fc->addr, channel);
327 case AVM_FRITZ_PCI:
328 return __read_status_pci(fc->addr, channel);
329 }
330 /* dummy */
331 return 0;
332}
333
334static void
335enable_hwirq(struct fritzcard *fc)
336{
337 fc->ctrlreg |= AVM_STATUS0_ENA_IRQ;
338 outb(fc->ctrlreg, fc->addr + 2);
339}
340
341static void
342disable_hwirq(struct fritzcard *fc)
343{
344 fc->ctrlreg &= ~AVM_STATUS0_ENA_IRQ;
345 outb(fc->ctrlreg, fc->addr + 2);
346}
347
348static int
349modehdlc(struct bchannel *bch, int protocol)
350{
351 struct fritzcard *fc = bch->hw;
352 struct hdlc_hw *hdlc;
Karsten Keil09e79a772012-05-15 23:51:03 +0000353 u8 mode;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200354
355 hdlc = &fc->hdlc[(bch->nr - 1) & 1];
356 pr_debug("%s: hdlc %c protocol %x-->%x ch %d\n", fc->name,
Joe Perches475be4d2012-02-19 19:52:38 -0800357 '@' + bch->nr, bch->state, protocol, bch->nr);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200358 hdlc->ctrl.ctrl = 0;
Karsten Keil09e79a772012-05-15 23:51:03 +0000359 mode = (fc->type == AVM_FRITZ_PCIV2) ? HDLC_FIFO_SIZE_128 : 0;
360
Karsten Keil6115d2f2009-07-22 19:52:24 +0200361 switch (protocol) {
362 case -1: /* used for init */
363 bch->state = -1;
364 case ISDN_P_NONE:
365 if (bch->state == ISDN_P_NONE)
366 break;
367 hdlc->ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
Karsten Keil09e79a772012-05-15 23:51:03 +0000368 hdlc->ctrl.sr.mode = mode | HDLC_MODE_TRANS;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200369 write_ctrl(bch, 5);
370 bch->state = ISDN_P_NONE;
371 test_and_clear_bit(FLG_HDLC, &bch->Flags);
372 test_and_clear_bit(FLG_TRANSPARENT, &bch->Flags);
373 break;
374 case ISDN_P_B_RAW:
375 bch->state = protocol;
376 hdlc->ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
Karsten Keil09e79a772012-05-15 23:51:03 +0000377 hdlc->ctrl.sr.mode = mode | HDLC_MODE_TRANS;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200378 write_ctrl(bch, 5);
379 hdlc->ctrl.sr.cmd = HDLC_CMD_XRS;
380 write_ctrl(bch, 1);
381 hdlc->ctrl.sr.cmd = 0;
382 test_and_set_bit(FLG_TRANSPARENT, &bch->Flags);
383 break;
384 case ISDN_P_B_HDLC:
385 bch->state = protocol;
386 hdlc->ctrl.sr.cmd = HDLC_CMD_XRS | HDLC_CMD_RRS;
Karsten Keil09e79a772012-05-15 23:51:03 +0000387 hdlc->ctrl.sr.mode = mode | HDLC_MODE_ITF_FLG;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200388 write_ctrl(bch, 5);
389 hdlc->ctrl.sr.cmd = HDLC_CMD_XRS;
390 write_ctrl(bch, 1);
391 hdlc->ctrl.sr.cmd = 0;
392 test_and_set_bit(FLG_HDLC, &bch->Flags);
393 break;
394 default:
395 pr_info("%s: protocol not known %x\n", fc->name, protocol);
396 return -ENOPROTOOPT;
397 }
398 return 0;
399}
400
401static void
402hdlc_empty_fifo(struct bchannel *bch, int count)
403{
404 u32 *ptr;
405 u8 *p;
406 u32 val, addr;
Karsten Keil7206e652012-05-15 23:51:05 +0000407 int cnt;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200408 struct fritzcard *fc = bch->hw;
409
410 pr_debug("%s: %s %d\n", fc->name, __func__, count);
Karsten Keil7206e652012-05-15 23:51:05 +0000411 cnt = bchannel_get_rxbuf(bch, count);
412 if (cnt < 0) {
413 pr_warning("%s.B%d: No bufferspace for %d bytes\n",
414 fc->name, bch->nr, count);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200415 return;
416 }
417 p = skb_put(bch->rx_skb, count);
418 ptr = (u32 *)p;
Karsten Keil09e79a772012-05-15 23:51:03 +0000419 if (fc->type == AVM_FRITZ_PCIV2)
Karsten Keil6115d2f2009-07-22 19:52:24 +0200420 addr = fc->addr + (bch->nr == 2 ?
Joe Perches475be4d2012-02-19 19:52:38 -0800421 AVM_HDLC_FIFO_2 : AVM_HDLC_FIFO_1);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200422 else {
423 addr = fc->addr + CHIP_WINDOW;
424 outl(bch->nr == 2 ? AVM_HDLC_2 : AVM_HDLC_1, fc->addr);
425 }
Karsten Keil7206e652012-05-15 23:51:05 +0000426 cnt = 0;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200427 while (cnt < count) {
428 val = le32_to_cpu(inl(addr));
429 put_unaligned(val, ptr);
430 ptr++;
431 cnt += 4;
432 }
433 if (debug & DEBUG_HW_BFIFO) {
434 snprintf(fc->log, LOG_SIZE, "B%1d-recv %s %d ",
Joe Perches475be4d2012-02-19 19:52:38 -0800435 bch->nr, fc->name, count);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200436 print_hex_dump_bytes(fc->log, DUMP_PREFIX_OFFSET, p, count);
437 }
438}
439
440static void
441hdlc_fill_fifo(struct bchannel *bch)
442{
443 struct fritzcard *fc = bch->hw;
444 struct hdlc_hw *hdlc;
Karsten Keil09e79a772012-05-15 23:51:03 +0000445 int count, fs, cnt = 0;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200446 u8 *p;
447 u32 *ptr, val, addr;
448
449 hdlc = &fc->hdlc[(bch->nr - 1) & 1];
450 if (!bch->tx_skb)
451 return;
452 count = bch->tx_skb->len - bch->tx_idx;
453 if (count <= 0)
454 return;
Karsten Keil09e79a772012-05-15 23:51:03 +0000455 fs = (fc->type == AVM_FRITZ_PCIV2) ?
456 HDLC_FIFO_SIZE_V2 : HDLC_FIFO_SIZE_V1;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200457 p = bch->tx_skb->data + bch->tx_idx;
458 hdlc->ctrl.sr.cmd &= ~HDLC_CMD_XME;
Karsten Keil09e79a772012-05-15 23:51:03 +0000459 if (count > fs) {
460 count = fs;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200461 } else {
462 if (test_bit(FLG_HDLC, &bch->Flags))
463 hdlc->ctrl.sr.cmd |= HDLC_CMD_XME;
464 }
465 pr_debug("%s: %s %d/%d/%d", fc->name, __func__, count,
Joe Perches475be4d2012-02-19 19:52:38 -0800466 bch->tx_idx, bch->tx_skb->len);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200467 ptr = (u32 *)p;
468 bch->tx_idx += count;
Karsten Keil09e79a772012-05-15 23:51:03 +0000469 hdlc->ctrl.sr.xml = ((count == fs) ? 0 : count);
470 if (fc->type == AVM_FRITZ_PCIV2) {
Karsten Keil6115d2f2009-07-22 19:52:24 +0200471 __write_ctrl_pciv2(fc, hdlc, bch->nr);
472 addr = fc->addr + (bch->nr == 2 ?
Joe Perches475be4d2012-02-19 19:52:38 -0800473 AVM_HDLC_FIFO_2 : AVM_HDLC_FIFO_1);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200474 } else {
475 __write_ctrl_pci(fc, hdlc, bch->nr);
476 addr = fc->addr + CHIP_WINDOW;
477 }
478 while (cnt < count) {
479 val = get_unaligned(ptr);
480 outl(cpu_to_le32(val), addr);
481 ptr++;
482 cnt += 4;
483 }
484 if (debug & DEBUG_HW_BFIFO) {
485 snprintf(fc->log, LOG_SIZE, "B%1d-send %s %d ",
Joe Perches475be4d2012-02-19 19:52:38 -0800486 bch->nr, fc->name, count);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200487 print_hex_dump_bytes(fc->log, DUMP_PREFIX_OFFSET, p, count);
488 }
489}
490
491static void
492HDLC_irq_xpr(struct bchannel *bch)
493{
Karsten Keil8bfddfb2012-05-15 23:51:02 +0000494 if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len) {
Karsten Keil6115d2f2009-07-22 19:52:24 +0200495 hdlc_fill_fifo(bch);
Karsten Keil8bfddfb2012-05-15 23:51:02 +0000496 } else {
497 if (bch->tx_skb)
Karsten Keil6115d2f2009-07-22 19:52:24 +0200498 dev_kfree_skb(bch->tx_skb);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200499 if (get_next_bframe(bch))
500 hdlc_fill_fifo(bch);
501 }
502}
503
504static void
505HDLC_irq(struct bchannel *bch, u32 stat)
506{
507 struct fritzcard *fc = bch->hw;
Karsten Keil09e79a772012-05-15 23:51:03 +0000508 int len, fs;
509 u32 rmlMask;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200510 struct hdlc_hw *hdlc;
511
512 hdlc = &fc->hdlc[(bch->nr - 1) & 1];
513 pr_debug("%s: ch%d stat %#x\n", fc->name, bch->nr, stat);
Karsten Keil09e79a772012-05-15 23:51:03 +0000514 if (fc->type == AVM_FRITZ_PCIV2) {
515 rmlMask = HDLC_STAT_RML_MASK_V2;
516 fs = HDLC_FIFO_SIZE_V2;
517 } else {
518 rmlMask = HDLC_STAT_RML_MASK_V1;
519 fs = HDLC_FIFO_SIZE_V1;
520 }
Karsten Keil6115d2f2009-07-22 19:52:24 +0200521 if (stat & HDLC_INT_RPR) {
522 if (stat & HDLC_STAT_RDO) {
Karsten Keil09e79a772012-05-15 23:51:03 +0000523 pr_warning("%s: ch%d stat %x RDO\n",
524 fc->name, bch->nr, stat);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200525 hdlc->ctrl.sr.xml = 0;
526 hdlc->ctrl.sr.cmd |= HDLC_CMD_RRS;
527 write_ctrl(bch, 1);
528 hdlc->ctrl.sr.cmd &= ~HDLC_CMD_RRS;
529 write_ctrl(bch, 1);
530 if (bch->rx_skb)
531 skb_trim(bch->rx_skb, 0);
532 } else {
Karsten Keil09e79a772012-05-15 23:51:03 +0000533 len = (stat & rmlMask) >> 8;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200534 if (!len)
Karsten Keil09e79a772012-05-15 23:51:03 +0000535 len = fs;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200536 hdlc_empty_fifo(bch, len);
537 if (!bch->rx_skb)
538 goto handle_tx;
Karsten Keil034005a2012-05-15 23:51:06 +0000539 if (test_bit(FLG_TRANSPARENT, &bch->Flags)) {
540 recv_Bchannel(bch, 0, false);
541 } else if (stat & HDLC_STAT_RME) {
542 if ((stat & HDLC_STAT_CRCVFRRAB) ==
543 HDLC_STAT_CRCVFR) {
544 recv_Bchannel(bch, 0, false);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200545 } else {
Karsten Keil09e79a772012-05-15 23:51:03 +0000546 pr_warning("%s: got invalid frame\n",
547 fc->name);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200548 skb_trim(bch->rx_skb, 0);
549 }
550 }
551 }
552 }
553handle_tx:
554 if (stat & HDLC_INT_XDU) {
555 /* Here we lost an TX interrupt, so
556 * restart transmitting the whole frame on HDLC
557 * in transparent mode we send the next data
558 */
Karsten Keil09e79a772012-05-15 23:51:03 +0000559 pr_warning("%s: ch%d stat %x XDU %s\n", fc->name, bch->nr,
560 stat, bch->tx_skb ? "tx_skb" : "no tx_skb");
Karsten Keil6115d2f2009-07-22 19:52:24 +0200561 if (bch->tx_skb && bch->tx_skb->len) {
562 if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
563 bch->tx_idx = 0;
564 }
565 hdlc->ctrl.sr.xml = 0;
566 hdlc->ctrl.sr.cmd |= HDLC_CMD_XRS;
567 write_ctrl(bch, 1);
568 hdlc->ctrl.sr.cmd &= ~HDLC_CMD_XRS;
569 HDLC_irq_xpr(bch);
570 return;
571 } else if (stat & HDLC_INT_XPR)
572 HDLC_irq_xpr(bch);
573}
574
575static inline void
576HDLC_irq_main(struct fritzcard *fc)
577{
578 u32 stat;
579 struct bchannel *bch;
580
581 stat = read_status(fc, 1);
582 if (stat & HDLC_INT_MASK) {
583 bch = Sel_BCS(fc, 1);
584 if (bch)
585 HDLC_irq(bch, stat);
586 else
587 pr_debug("%s: spurious ch1 IRQ\n", fc->name);
588 }
589 stat = read_status(fc, 2);
590 if (stat & HDLC_INT_MASK) {
591 bch = Sel_BCS(fc, 2);
592 if (bch)
593 HDLC_irq(bch, stat);
594 else
595 pr_debug("%s: spurious ch2 IRQ\n", fc->name);
596 }
597}
598
599static irqreturn_t
600avm_fritz_interrupt(int intno, void *dev_id)
601{
602 struct fritzcard *fc = dev_id;
603 u8 val;
604 u8 sval;
605
606 spin_lock(&fc->lock);
607 sval = inb(fc->addr + 2);
608 pr_debug("%s: irq stat0 %x\n", fc->name, sval);
609 if ((sval & AVM_STATUS0_IRQ_MASK) == AVM_STATUS0_IRQ_MASK) {
610 /* shared IRQ from other HW */
611 spin_unlock(&fc->lock);
612 return IRQ_NONE;
613 }
614 fc->irqcnt++;
615
616 if (!(sval & AVM_STATUS0_IRQ_ISAC)) {
617 val = ReadISAC_V1(fc, ISAC_ISTA);
618 mISDNisac_irq(&fc->isac, val);
619 }
620 if (!(sval & AVM_STATUS0_IRQ_HDLC))
621 HDLC_irq_main(fc);
622 spin_unlock(&fc->lock);
623 return IRQ_HANDLED;
624}
625
626static irqreturn_t
627avm_fritzv2_interrupt(int intno, void *dev_id)
628{
629 struct fritzcard *fc = dev_id;
630 u8 val;
631 u8 sval;
632
633 spin_lock(&fc->lock);
634 sval = inb(fc->addr + 2);
635 pr_debug("%s: irq stat0 %x\n", fc->name, sval);
636 if (!(sval & AVM_STATUS0_IRQ_MASK)) {
637 /* shared IRQ from other HW */
638 spin_unlock(&fc->lock);
639 return IRQ_NONE;
640 }
641 fc->irqcnt++;
642
643 if (sval & AVM_STATUS0_IRQ_HDLC)
644 HDLC_irq_main(fc);
645 if (sval & AVM_STATUS0_IRQ_ISAC) {
646 val = ReadISAC_V2(fc, ISACX_ISTA);
647 mISDNisac_irq(&fc->isac, val);
648 }
649 if (sval & AVM_STATUS0_IRQ_TIMER) {
650 pr_debug("%s: timer irq\n", fc->name);
651 outb(fc->ctrlreg | AVM_STATUS0_RES_TIMER, fc->addr + 2);
652 udelay(1);
653 outb(fc->ctrlreg, fc->addr + 2);
654 }
655 spin_unlock(&fc->lock);
656 return IRQ_HANDLED;
657}
658
659static int
660avm_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
661{
662 struct bchannel *bch = container_of(ch, struct bchannel, ch);
663 struct fritzcard *fc = bch->hw;
664 int ret = -EINVAL;
665 struct mISDNhead *hh = mISDN_HEAD_P(skb);
Karsten Keil8bfddfb2012-05-15 23:51:02 +0000666 unsigned long flags;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200667
668 switch (hh->prim) {
669 case PH_DATA_REQ:
670 spin_lock_irqsave(&fc->lock, flags);
671 ret = bchannel_senddata(bch, skb);
672 if (ret > 0) { /* direct TX */
Karsten Keil6115d2f2009-07-22 19:52:24 +0200673 hdlc_fill_fifo(bch);
674 ret = 0;
Karsten Keil8bfddfb2012-05-15 23:51:02 +0000675 }
676 spin_unlock_irqrestore(&fc->lock, flags);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200677 return ret;
678 case PH_ACTIVATE_REQ:
679 spin_lock_irqsave(&fc->lock, flags);
680 if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
681 ret = modehdlc(bch, ch->protocol);
682 else
683 ret = 0;
684 spin_unlock_irqrestore(&fc->lock, flags);
685 if (!ret)
686 _queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -0800687 NULL, GFP_KERNEL);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200688 break;
689 case PH_DEACTIVATE_REQ:
690 spin_lock_irqsave(&fc->lock, flags);
691 mISDN_clear_bchannel(bch);
692 modehdlc(bch, ISDN_P_NONE);
693 spin_unlock_irqrestore(&fc->lock, flags);
694 _queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
Joe Perches475be4d2012-02-19 19:52:38 -0800695 NULL, GFP_KERNEL);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200696 ret = 0;
697 break;
698 }
699 if (!ret)
700 dev_kfree_skb(skb);
701 return ret;
702}
703
704static void
705inithdlc(struct fritzcard *fc)
706{
707 modehdlc(&fc->bch[0], -1);
708 modehdlc(&fc->bch[1], -1);
709}
710
711void
712clear_pending_hdlc_ints(struct fritzcard *fc)
713{
714 u32 val;
715
716 val = read_status(fc, 1);
717 pr_debug("%s: HDLC 1 STA %x\n", fc->name, val);
718 val = read_status(fc, 2);
719 pr_debug("%s: HDLC 2 STA %x\n", fc->name, val);
720}
721
722static void
723reset_avm(struct fritzcard *fc)
724{
725 switch (fc->type) {
726 case AVM_FRITZ_PCI:
727 fc->ctrlreg = AVM_STATUS0_RESET | AVM_STATUS0_DIS_TIMER;
728 break;
729 case AVM_FRITZ_PCIV2:
730 fc->ctrlreg = AVM_STATUS0_RESET;
731 break;
732 }
733 if (debug & DEBUG_HW)
734 pr_notice("%s: reset\n", fc->name);
735 disable_hwirq(fc);
736 mdelay(5);
737 switch (fc->type) {
738 case AVM_FRITZ_PCI:
739 fc->ctrlreg = AVM_STATUS0_DIS_TIMER | AVM_STATUS0_RES_TIMER;
740 disable_hwirq(fc);
741 outb(AVM_STATUS1_ENA_IOM, fc->addr + 3);
742 break;
743 case AVM_FRITZ_PCIV2:
744 fc->ctrlreg = 0;
745 disable_hwirq(fc);
746 break;
747 }
748 mdelay(1);
749 if (debug & DEBUG_HW)
750 pr_notice("%s: S0/S1 %x/%x\n", fc->name,
Joe Perches475be4d2012-02-19 19:52:38 -0800751 inb(fc->addr + 2), inb(fc->addr + 3));
Karsten Keil6115d2f2009-07-22 19:52:24 +0200752}
753
754static int
755init_card(struct fritzcard *fc)
756{
757 int ret, cnt = 3;
758 u_long flags;
759
760 reset_avm(fc); /* disable IRQ */
761 if (fc->type == AVM_FRITZ_PCIV2)
762 ret = request_irq(fc->irq, avm_fritzv2_interrupt,
Joe Perches475be4d2012-02-19 19:52:38 -0800763 IRQF_SHARED, fc->name, fc);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200764 else
765 ret = request_irq(fc->irq, avm_fritz_interrupt,
Joe Perches475be4d2012-02-19 19:52:38 -0800766 IRQF_SHARED, fc->name, fc);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200767 if (ret) {
768 pr_info("%s: couldn't get interrupt %d\n",
769 fc->name, fc->irq);
770 return ret;
771 }
772 while (cnt--) {
773 spin_lock_irqsave(&fc->lock, flags);
774 ret = fc->isac.init(&fc->isac);
775 if (ret) {
776 spin_unlock_irqrestore(&fc->lock, flags);
777 pr_info("%s: ISAC init failed with %d\n",
778 fc->name, ret);
779 break;
780 }
781 clear_pending_hdlc_ints(fc);
782 inithdlc(fc);
783 enable_hwirq(fc);
784 /* RESET Receiver and Transmitter */
Karsten Keil09e79a772012-05-15 23:51:03 +0000785 if (fc->type == AVM_FRITZ_PCIV2) {
Karsten Keil6115d2f2009-07-22 19:52:24 +0200786 WriteISAC_V2(fc, ISACX_MASK, 0);
787 WriteISAC_V2(fc, ISACX_CMDRD, 0x41);
788 } else {
789 WriteISAC_V1(fc, ISAC_MASK, 0);
790 WriteISAC_V1(fc, ISAC_CMDR, 0x41);
791 }
792 spin_unlock_irqrestore(&fc->lock, flags);
793 /* Timeout 10ms */
794 msleep_interruptible(10);
795 if (debug & DEBUG_HW)
796 pr_notice("%s: IRQ %d count %d\n", fc->name,
Joe Perches475be4d2012-02-19 19:52:38 -0800797 fc->irq, fc->irqcnt);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200798 if (!fc->irqcnt) {
799 pr_info("%s: IRQ(%d) getting no IRQs during init %d\n",
800 fc->name, fc->irq, 3 - cnt);
801 reset_avm(fc);
802 } else
803 return 0;
804 }
805 free_irq(fc->irq, fc);
806 return -EIO;
807}
808
809static int
810channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
811{
Karsten Keil034005a2012-05-15 23:51:06 +0000812 return mISDN_ctrl_bchannel(bch, cq);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200813}
814
815static int
816avm_bctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
817{
818 struct bchannel *bch = container_of(ch, struct bchannel, ch);
819 struct fritzcard *fc = bch->hw;
820 int ret = -EINVAL;
821 u_long flags;
822
823 pr_debug("%s: %s cmd:%x %p\n", fc->name, __func__, cmd, arg);
824 switch (cmd) {
825 case CLOSE_CHANNEL:
826 test_and_clear_bit(FLG_OPEN, &bch->Flags);
Karsten Keil13681122012-05-15 23:51:01 +0000827 spin_lock_irqsave(&fc->lock, flags);
828 mISDN_freebchannel(bch);
829 modehdlc(bch, ISDN_P_NONE);
830 spin_unlock_irqrestore(&fc->lock, flags);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200831 ch->protocol = ISDN_P_NONE;
832 ch->peer = NULL;
833 module_put(THIS_MODULE);
834 ret = 0;
835 break;
836 case CONTROL_CHANNEL:
837 ret = channel_bctrl(bch, arg);
838 break;
839 default:
840 pr_info("%s: %s unknown prim(%x)\n", fc->name, __func__, cmd);
841 }
842 return ret;
843}
844
845static int
846channel_ctrl(struct fritzcard *fc, struct mISDN_ctrl_req *cq)
847{
848 int ret = 0;
849
850 switch (cq->op) {
851 case MISDN_CTRL_GETOP:
Karsten Keilc626c122012-05-04 04:15:33 +0000852 cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_L1_TIMER3;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200853 break;
854 case MISDN_CTRL_LOOP:
855 /* cq->channel: 0 disable, 1 B1 loop 2 B2 loop, 3 both */
856 if (cq->channel < 0 || cq->channel > 3) {
857 ret = -EINVAL;
858 break;
859 }
860 ret = fc->isac.ctrl(&fc->isac, HW_TESTLOOP, cq->channel);
861 break;
Karsten Keilc626c122012-05-04 04:15:33 +0000862 case MISDN_CTRL_L1_TIMER3:
863 ret = fc->isac.ctrl(&fc->isac, HW_TIMER3_VALUE, cq->p1);
864 break;
Karsten Keil6115d2f2009-07-22 19:52:24 +0200865 default:
866 pr_info("%s: %s unknown Op %x\n", fc->name, __func__, cq->op);
867 ret = -EINVAL;
868 break;
869 }
870 return ret;
871}
872
873static int
874open_bchannel(struct fritzcard *fc, struct channel_req *rq)
875{
876 struct bchannel *bch;
877
Dan Carpenter819a1002012-03-26 21:20:48 +0000878 if (rq->adr.channel == 0 || rq->adr.channel > 2)
Karsten Keil6115d2f2009-07-22 19:52:24 +0200879 return -EINVAL;
880 if (rq->protocol == ISDN_P_NONE)
881 return -EINVAL;
882 bch = &fc->bch[rq->adr.channel - 1];
883 if (test_and_set_bit(FLG_OPEN, &bch->Flags))
884 return -EBUSY; /* b-channel can be only open once */
885 test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
886 bch->ch.protocol = rq->protocol;
887 rq->ch = &bch->ch;
888 return 0;
889}
890
891/*
892 * device control function
893 */
894static int
895avm_dctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
896{
897 struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
898 struct dchannel *dch = container_of(dev, struct dchannel, dev);
899 struct fritzcard *fc = dch->hw;
900 struct channel_req *rq;
901 int err = 0;
902
903 pr_debug("%s: %s cmd:%x %p\n", fc->name, __func__, cmd, arg);
904 switch (cmd) {
905 case OPEN_CHANNEL:
906 rq = arg;
907 if (rq->protocol == ISDN_P_TE_S0)
908 err = fc->isac.open(&fc->isac, rq);
909 else
910 err = open_bchannel(fc, rq);
911 if (err)
912 break;
913 if (!try_module_get(THIS_MODULE))
914 pr_info("%s: cannot get module\n", fc->name);
915 break;
916 case CLOSE_CHANNEL:
917 pr_debug("%s: dev(%d) close from %p\n", fc->name, dch->dev.id,
Joe Perches475be4d2012-02-19 19:52:38 -0800918 __builtin_return_address(0));
Karsten Keil6115d2f2009-07-22 19:52:24 +0200919 module_put(THIS_MODULE);
920 break;
921 case CONTROL_CHANNEL:
922 err = channel_ctrl(fc, arg);
923 break;
924 default:
925 pr_debug("%s: %s unknown command %x\n",
Joe Perches475be4d2012-02-19 19:52:38 -0800926 fc->name, __func__, cmd);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200927 return -EINVAL;
928 }
929 return err;
930}
931
932int
933setup_fritz(struct fritzcard *fc)
934{
935 u32 val, ver;
936
937 if (!request_region(fc->addr, 32, fc->name)) {
938 pr_info("%s: AVM config port %x-%x already in use\n",
939 fc->name, fc->addr, fc->addr + 31);
940 return -EIO;
941 }
942 switch (fc->type) {
943 case AVM_FRITZ_PCI:
944 val = inl(fc->addr);
945 outl(AVM_HDLC_1, fc->addr + CHIP_INDEX);
946 ver = inl(fc->addr + CHIP_WINDOW + HDLC_STATUS) >> 24;
947 if (debug & DEBUG_HW) {
948 pr_notice("%s: PCI stat %#x\n", fc->name, val);
949 pr_notice("%s: PCI Class %X Rev %d\n", fc->name,
Joe Perches475be4d2012-02-19 19:52:38 -0800950 val & 0xff, (val >> 8) & 0xff);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200951 pr_notice("%s: HDLC version %x\n", fc->name, ver & 0xf);
952 }
953 ASSIGN_FUNC(V1, ISAC, fc->isac);
954 fc->isac.type = IPAC_TYPE_ISAC;
955 break;
956 case AVM_FRITZ_PCIV2:
957 val = inl(fc->addr);
958 ver = inl(fc->addr + AVM_HDLC_STATUS_1) >> 24;
959 if (debug & DEBUG_HW) {
960 pr_notice("%s: PCI V2 stat %#x\n", fc->name, val);
961 pr_notice("%s: PCI V2 Class %X Rev %d\n", fc->name,
Joe Perches475be4d2012-02-19 19:52:38 -0800962 val & 0xff, (val >> 8) & 0xff);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200963 pr_notice("%s: HDLC version %x\n", fc->name, ver & 0xf);
964 }
965 ASSIGN_FUNC(V2, ISAC, fc->isac);
966 fc->isac.type = IPAC_TYPE_ISACX;
967 break;
968 default:
969 release_region(fc->addr, 32);
970 pr_info("%s: AVM unknown type %d\n", fc->name, fc->type);
971 return -ENODEV;
972 }
973 pr_notice("%s: %s config irq:%d base:0x%X\n", fc->name,
Joe Perches475be4d2012-02-19 19:52:38 -0800974 (fc->type == AVM_FRITZ_PCI) ? "AVM Fritz!CARD PCI" :
975 "AVM Fritz!CARD PCIv2", fc->irq, fc->addr);
Karsten Keil6115d2f2009-07-22 19:52:24 +0200976 return 0;
977}
978
979static void
980release_card(struct fritzcard *card)
981{
982 u_long flags;
983
984 disable_hwirq(card);
985 spin_lock_irqsave(&card->lock, flags);
986 modehdlc(&card->bch[0], ISDN_P_NONE);
987 modehdlc(&card->bch[1], ISDN_P_NONE);
988 spin_unlock_irqrestore(&card->lock, flags);
989 card->isac.release(&card->isac);
990 free_irq(card->irq, card);
991 mISDN_freebchannel(&card->bch[1]);
992 mISDN_freebchannel(&card->bch[0]);
993 mISDN_unregister_device(&card->isac.dch.dev);
994 release_region(card->addr, 32);
995 pci_disable_device(card->pdev);
996 pci_set_drvdata(card->pdev, NULL);
997 write_lock_irqsave(&card_lock, flags);
998 list_del(&card->list);
999 write_unlock_irqrestore(&card_lock, flags);
1000 kfree(card);
1001 AVM_cnt--;
1002}
1003
1004static int __devinit
1005setup_instance(struct fritzcard *card)
1006{
1007 int i, err;
Karsten Keil034005a2012-05-15 23:51:06 +00001008 unsigned short minsize;
Karsten Keil6115d2f2009-07-22 19:52:24 +02001009 u_long flags;
1010
1011 snprintf(card->name, MISDN_MAX_IDLEN - 1, "AVM.%d", AVM_cnt + 1);
1012 write_lock_irqsave(&card_lock, flags);
1013 list_add_tail(&card->list, &Cards);
1014 write_unlock_irqrestore(&card_lock, flags);
1015
1016 _set_debug(card);
1017 card->isac.name = card->name;
1018 spin_lock_init(&card->lock);
1019 card->isac.hwlock = &card->lock;
1020 mISDNisac_init(&card->isac, card);
1021
1022 card->isac.dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
Joe Perches475be4d2012-02-19 19:52:38 -08001023 (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
Karsten Keil6115d2f2009-07-22 19:52:24 +02001024 card->isac.dch.dev.D.ctrl = avm_dctrl;
1025 for (i = 0; i < 2; i++) {
1026 card->bch[i].nr = i + 1;
1027 set_channelmap(i + 1, card->isac.dch.dev.channelmap);
Karsten Keil034005a2012-05-15 23:51:06 +00001028 if (AVM_FRITZ_PCIV2 == card->type)
1029 minsize = HDLC_FIFO_SIZE_V2;
1030 else
1031 minsize = HDLC_FIFO_SIZE_V1;
1032 mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM, minsize);
Karsten Keil6115d2f2009-07-22 19:52:24 +02001033 card->bch[i].hw = card;
1034 card->bch[i].ch.send = avm_l2l1B;
1035 card->bch[i].ch.ctrl = avm_bctrl;
1036 card->bch[i].ch.nr = i + 1;
1037 list_add(&card->bch[i].ch.list, &card->isac.dch.dev.bchannels);
1038 }
1039 err = setup_fritz(card);
1040 if (err)
1041 goto error;
1042 err = mISDN_register_device(&card->isac.dch.dev, &card->pdev->dev,
Joe Perches475be4d2012-02-19 19:52:38 -08001043 card->name);
Karsten Keil6115d2f2009-07-22 19:52:24 +02001044 if (err)
1045 goto error_reg;
1046 err = init_card(card);
1047 if (!err) {
1048 AVM_cnt++;
1049 pr_notice("AVM %d cards installed DEBUG\n", AVM_cnt);
1050 return 0;
1051 }
1052 mISDN_unregister_device(&card->isac.dch.dev);
1053error_reg:
1054 release_region(card->addr, 32);
1055error:
1056 card->isac.release(&card->isac);
1057 mISDN_freebchannel(&card->bch[1]);
1058 mISDN_freebchannel(&card->bch[0]);
1059 write_lock_irqsave(&card_lock, flags);
1060 list_del(&card->list);
1061 write_unlock_irqrestore(&card_lock, flags);
1062 kfree(card);
1063 return err;
1064}
1065
1066static int __devinit
1067fritzpci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1068{
1069 int err = -ENOMEM;
1070 struct fritzcard *card;
1071
1072 card = kzalloc(sizeof(struct fritzcard), GFP_KERNEL);
1073 if (!card) {
1074 pr_info("No kmem for fritzcard\n");
1075 return err;
1076 }
1077 if (pdev->device == PCI_DEVICE_ID_AVM_A1_V2)
1078 card->type = AVM_FRITZ_PCIV2;
1079 else
1080 card->type = AVM_FRITZ_PCI;
1081 card->pdev = pdev;
1082 err = pci_enable_device(pdev);
1083 if (err) {
1084 kfree(card);
1085 return err;
1086 }
1087
1088 pr_notice("mISDN: found adapter %s at %s\n",
Joe Perches475be4d2012-02-19 19:52:38 -08001089 (char *) ent->driver_data, pci_name(pdev));
Karsten Keil6115d2f2009-07-22 19:52:24 +02001090
1091 card->addr = pci_resource_start(pdev, 1);
1092 card->irq = pdev->irq;
1093 pci_set_drvdata(pdev, card);
1094 err = setup_instance(card);
1095 if (err)
1096 pci_set_drvdata(pdev, NULL);
1097 return err;
1098}
1099
1100static void __devexit
1101fritz_remove_pci(struct pci_dev *pdev)
1102{
1103 struct fritzcard *card = pci_get_drvdata(pdev);
1104
1105 if (card)
1106 release_card(card);
1107 else
1108 if (debug)
Uwe Kleine-König698f9312010-07-02 20:41:51 +02001109 pr_info("%s: drvdata already removed\n", __func__);
Karsten Keil6115d2f2009-07-22 19:52:24 +02001110}
1111
1112static struct pci_device_id fcpci_ids[] __devinitdata = {
1113 { PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_A1, PCI_ANY_ID, PCI_ANY_ID,
1114 0, 0, (unsigned long) "Fritz!Card PCI"},
1115 { PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_A1_V2, PCI_ANY_ID, PCI_ANY_ID,
1116 0, 0, (unsigned long) "Fritz!Card PCI v2" },
1117 { }
1118};
1119MODULE_DEVICE_TABLE(pci, fcpci_ids);
1120
1121static struct pci_driver fcpci_driver = {
1122 .name = "fcpci",
1123 .probe = fritzpci_probe,
1124 .remove = __devexit_p(fritz_remove_pci),
1125 .id_table = fcpci_ids,
1126};
1127
1128static int __init AVM_init(void)
1129{
1130 int err;
1131
1132 pr_notice("AVM Fritz PCI driver Rev. %s\n", AVMFRITZ_REV);
1133 err = pci_register_driver(&fcpci_driver);
1134 return err;
1135}
1136
1137static void __exit AVM_cleanup(void)
1138{
1139 pci_unregister_driver(&fcpci_driver);
1140}
1141
1142module_init(AVM_init);
1143module_exit(AVM_cleanup);