Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 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 | */ |
Alexey Dobriyan | a6b7a40 | 2011-06-06 10:43:46 +0000 | [diff] [blame] | 23 | #include <linux/interrupt.h> |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/pci.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/mISDNhw.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 29 | #include <asm/unaligned.h> |
| 30 | #include "ipac.h" |
| 31 | |
| 32 | |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 33 | #define AVMFRITZ_REV "2.2" |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 34 | |
| 35 | static int AVM_cnt; |
| 36 | static int debug; |
| 37 | |
| 38 | enum { |
| 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 72 | #define HDLC_FIFO_SIZE_128 0x20 |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 73 | #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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 84 | #define HDLC_STAT_RML_MASK_V1 0x3f00 |
| 85 | #define HDLC_STAT_RML_MASK_V2 0x7f00 |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 86 | |
| 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 91 | |
| 92 | #define HDLC_FIFO_SIZE_V1 32 |
| 93 | #define HDLC_FIFO_SIZE_V2 128 |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 94 | |
| 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 | |
| 109 | struct 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 | |
| 123 | struct hdlc_hw { |
| 124 | union { |
| 125 | u32 ctrl; |
| 126 | struct hdlc_stat_reg sr; |
| 127 | } ctrl; |
| 128 | u32 stat; |
| 129 | }; |
| 130 | |
| 131 | struct 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 | |
| 147 | static LIST_HEAD(Cards); |
| 148 | static DEFINE_RWLOCK(card_lock); /* protect Cards */ |
| 149 | |
| 150 | static 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 | |
| 158 | static int |
| 159 | set_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 | |
| 174 | MODULE_AUTHOR("Karsten Keil"); |
| 175 | MODULE_LICENSE("GPL v2"); |
| 176 | MODULE_VERSION(AVMFRITZ_REV); |
| 177 | module_param_call(debug, set_debug, param_get_uint, &debug, S_IRUGO | S_IWUSR); |
| 178 | MODULE_PARM_DESC(debug, "avmfritz debug mask"); |
| 179 | |
| 180 | /* Interface functions */ |
| 181 | |
| 182 | static u8 |
| 183 | ReadISAC_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 | |
| 192 | static void |
| 193 | WriteISAC_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 | |
| 202 | static void |
| 203 | ReadFiFoISAC_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 | |
| 211 | static void |
| 212 | WriteFiFoISAC_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 | |
| 220 | static u8 |
| 221 | ReadISAC_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 | |
| 229 | static void |
| 230 | WriteISAC_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 | |
| 238 | static void |
| 239 | ReadFiFoISAC_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 | |
| 249 | static void |
| 250 | WriteFiFoISAC_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 | |
| 260 | static struct bchannel * |
| 261 | Sel_BCS(struct fritzcard *fc, u32 channel) |
| 262 | { |
| 263 | if (test_bit(FLG_ACTIVE, &fc->bch[0].Flags) && |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 264 | (fc->bch[0].nr & channel)) |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 265 | return &fc->bch[0]; |
| 266 | else if (test_bit(FLG_ACTIVE, &fc->bch[1].Flags) && |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 267 | (fc->bch[1].nr & channel)) |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 268 | return &fc->bch[1]; |
| 269 | else |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | static 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 | |
| 281 | static 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 284 | AVM_HDLC_STATUS_1)); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void |
| 288 | write_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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 294 | which, hdlc->ctrl.ctrl); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 295 | 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 | |
| 306 | static 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 | |
| 313 | static inline u32 |
| 314 | __read_status_pciv2(u_long addr, u32 channel) |
| 315 | { |
| 316 | return inl(addr + (channel == 2 ? AVM_HDLC_STATUS_2 : |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 317 | AVM_HDLC_STATUS_1)); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
| 321 | static u32 |
| 322 | read_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 | |
| 334 | static void |
| 335 | enable_hwirq(struct fritzcard *fc) |
| 336 | { |
| 337 | fc->ctrlreg |= AVM_STATUS0_ENA_IRQ; |
| 338 | outb(fc->ctrlreg, fc->addr + 2); |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | disable_hwirq(struct fritzcard *fc) |
| 343 | { |
| 344 | fc->ctrlreg &= ~AVM_STATUS0_ENA_IRQ; |
| 345 | outb(fc->ctrlreg, fc->addr + 2); |
| 346 | } |
| 347 | |
| 348 | static int |
| 349 | modehdlc(struct bchannel *bch, int protocol) |
| 350 | { |
| 351 | struct fritzcard *fc = bch->hw; |
| 352 | struct hdlc_hw *hdlc; |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 353 | u8 mode; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 354 | |
| 355 | hdlc = &fc->hdlc[(bch->nr - 1) & 1]; |
| 356 | pr_debug("%s: hdlc %c protocol %x-->%x ch %d\n", fc->name, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 357 | '@' + bch->nr, bch->state, protocol, bch->nr); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 358 | hdlc->ctrl.ctrl = 0; |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 359 | mode = (fc->type == AVM_FRITZ_PCIV2) ? HDLC_FIFO_SIZE_128 : 0; |
| 360 | |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 361 | 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 368 | hdlc->ctrl.sr.mode = mode | HDLC_MODE_TRANS; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 369 | 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 377 | hdlc->ctrl.sr.mode = mode | HDLC_MODE_TRANS; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 378 | 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 387 | hdlc->ctrl.sr.mode = mode | HDLC_MODE_ITF_FLG; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 388 | 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 | |
| 401 | static void |
| 402 | hdlc_empty_fifo(struct bchannel *bch, int count) |
| 403 | { |
| 404 | u32 *ptr; |
| 405 | u8 *p; |
| 406 | u32 val, addr; |
Karsten Keil | 7206e65 | 2012-05-15 23:51:05 +0000 | [diff] [blame^] | 407 | int cnt; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 408 | struct fritzcard *fc = bch->hw; |
| 409 | |
| 410 | pr_debug("%s: %s %d\n", fc->name, __func__, count); |
Karsten Keil | 7206e65 | 2012-05-15 23:51:05 +0000 | [diff] [blame^] | 411 | 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 Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | p = skb_put(bch->rx_skb, count); |
| 418 | ptr = (u32 *)p; |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 419 | if (fc->type == AVM_FRITZ_PCIV2) |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 420 | addr = fc->addr + (bch->nr == 2 ? |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 421 | AVM_HDLC_FIFO_2 : AVM_HDLC_FIFO_1); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 422 | else { |
| 423 | addr = fc->addr + CHIP_WINDOW; |
| 424 | outl(bch->nr == 2 ? AVM_HDLC_2 : AVM_HDLC_1, fc->addr); |
| 425 | } |
Karsten Keil | 7206e65 | 2012-05-15 23:51:05 +0000 | [diff] [blame^] | 426 | cnt = 0; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 427 | 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 435 | bch->nr, fc->name, count); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 436 | print_hex_dump_bytes(fc->log, DUMP_PREFIX_OFFSET, p, count); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static void |
| 441 | hdlc_fill_fifo(struct bchannel *bch) |
| 442 | { |
| 443 | struct fritzcard *fc = bch->hw; |
| 444 | struct hdlc_hw *hdlc; |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 445 | int count, fs, cnt = 0; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 446 | 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 455 | fs = (fc->type == AVM_FRITZ_PCIV2) ? |
| 456 | HDLC_FIFO_SIZE_V2 : HDLC_FIFO_SIZE_V1; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 457 | p = bch->tx_skb->data + bch->tx_idx; |
| 458 | hdlc->ctrl.sr.cmd &= ~HDLC_CMD_XME; |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 459 | if (count > fs) { |
| 460 | count = fs; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 461 | } 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 466 | bch->tx_idx, bch->tx_skb->len); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 467 | ptr = (u32 *)p; |
| 468 | bch->tx_idx += count; |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 469 | hdlc->ctrl.sr.xml = ((count == fs) ? 0 : count); |
| 470 | if (fc->type == AVM_FRITZ_PCIV2) { |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 471 | __write_ctrl_pciv2(fc, hdlc, bch->nr); |
| 472 | addr = fc->addr + (bch->nr == 2 ? |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 473 | AVM_HDLC_FIFO_2 : AVM_HDLC_FIFO_1); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 474 | } 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 486 | bch->nr, fc->name, count); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 487 | print_hex_dump_bytes(fc->log, DUMP_PREFIX_OFFSET, p, count); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static void |
| 492 | HDLC_irq_xpr(struct bchannel *bch) |
| 493 | { |
Karsten Keil | 8bfddfb | 2012-05-15 23:51:02 +0000 | [diff] [blame] | 494 | if (bch->tx_skb && bch->tx_idx < bch->tx_skb->len) { |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 495 | hdlc_fill_fifo(bch); |
Karsten Keil | 8bfddfb | 2012-05-15 23:51:02 +0000 | [diff] [blame] | 496 | } else { |
| 497 | if (bch->tx_skb) |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 498 | dev_kfree_skb(bch->tx_skb); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 499 | if (get_next_bframe(bch)) |
| 500 | hdlc_fill_fifo(bch); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static void |
| 505 | HDLC_irq(struct bchannel *bch, u32 stat) |
| 506 | { |
| 507 | struct fritzcard *fc = bch->hw; |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 508 | int len, fs; |
| 509 | u32 rmlMask; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 510 | 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 514 | 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 Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 521 | if (stat & HDLC_INT_RPR) { |
| 522 | if (stat & HDLC_STAT_RDO) { |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 523 | pr_warning("%s: ch%d stat %x RDO\n", |
| 524 | fc->name, bch->nr, stat); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 525 | 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 533 | len = (stat & rmlMask) >> 8; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 534 | if (!len) |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 535 | len = fs; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 536 | hdlc_empty_fifo(bch, len); |
| 537 | if (!bch->rx_skb) |
| 538 | goto handle_tx; |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 539 | if (test_bit(FLG_TRANSPARENT, &bch->Flags) || |
| 540 | (stat & HDLC_STAT_RME)) { |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 541 | if (((stat & HDLC_STAT_CRCVFRRAB) == |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 542 | HDLC_STAT_CRCVFR) || |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 543 | test_bit(FLG_TRANSPARENT, &bch->Flags)) { |
| 544 | recv_Bchannel(bch, 0); |
| 545 | } else { |
Karsten Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 546 | pr_warning("%s: got invalid frame\n", |
| 547 | fc->name); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 548 | skb_trim(bch->rx_skb, 0); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | handle_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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 559 | 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 Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 561 | 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 | |
| 575 | static inline void |
| 576 | HDLC_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 | |
| 599 | static irqreturn_t |
| 600 | avm_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 | |
| 626 | static irqreturn_t |
| 627 | avm_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 | |
| 659 | static int |
| 660 | avm_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 Keil | 8bfddfb | 2012-05-15 23:51:02 +0000 | [diff] [blame] | 666 | unsigned long flags; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 667 | |
| 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 Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 673 | hdlc_fill_fifo(bch); |
| 674 | ret = 0; |
Karsten Keil | 8bfddfb | 2012-05-15 23:51:02 +0000 | [diff] [blame] | 675 | } |
| 676 | spin_unlock_irqrestore(&fc->lock, flags); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 677 | 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 687 | NULL, GFP_KERNEL); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 688 | 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 695 | NULL, GFP_KERNEL); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 696 | ret = 0; |
| 697 | break; |
| 698 | } |
| 699 | if (!ret) |
| 700 | dev_kfree_skb(skb); |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | static void |
| 705 | inithdlc(struct fritzcard *fc) |
| 706 | { |
| 707 | modehdlc(&fc->bch[0], -1); |
| 708 | modehdlc(&fc->bch[1], -1); |
| 709 | } |
| 710 | |
| 711 | void |
| 712 | clear_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 | |
| 722 | static void |
| 723 | reset_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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 751 | inb(fc->addr + 2), inb(fc->addr + 3)); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static int |
| 755 | init_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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 763 | IRQF_SHARED, fc->name, fc); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 764 | else |
| 765 | ret = request_irq(fc->irq, avm_fritz_interrupt, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 766 | IRQF_SHARED, fc->name, fc); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 767 | 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 Keil | 09e79a77 | 2012-05-15 23:51:03 +0000 | [diff] [blame] | 785 | if (fc->type == AVM_FRITZ_PCIV2) { |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 786 | 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 Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 797 | fc->irq, fc->irqcnt); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 798 | 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 | |
| 809 | static int |
| 810 | channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq) |
| 811 | { |
| 812 | int ret = 0; |
| 813 | struct fritzcard *fc = bch->hw; |
| 814 | |
| 815 | switch (cq->op) { |
| 816 | case MISDN_CTRL_GETOP: |
| 817 | cq->op = 0; |
| 818 | break; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 819 | /* Nothing implemented yet */ |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 820 | case MISDN_CTRL_FILL_EMPTY: |
| 821 | default: |
| 822 | pr_info("%s: %s unknown Op %x\n", fc->name, __func__, cq->op); |
| 823 | ret = -EINVAL; |
| 824 | break; |
| 825 | } |
| 826 | return ret; |
| 827 | } |
| 828 | |
| 829 | static int |
| 830 | avm_bctrl(struct mISDNchannel *ch, u32 cmd, void *arg) |
| 831 | { |
| 832 | struct bchannel *bch = container_of(ch, struct bchannel, ch); |
| 833 | struct fritzcard *fc = bch->hw; |
| 834 | int ret = -EINVAL; |
| 835 | u_long flags; |
| 836 | |
| 837 | pr_debug("%s: %s cmd:%x %p\n", fc->name, __func__, cmd, arg); |
| 838 | switch (cmd) { |
| 839 | case CLOSE_CHANNEL: |
| 840 | test_and_clear_bit(FLG_OPEN, &bch->Flags); |
Karsten Keil | 1368112 | 2012-05-15 23:51:01 +0000 | [diff] [blame] | 841 | spin_lock_irqsave(&fc->lock, flags); |
| 842 | mISDN_freebchannel(bch); |
| 843 | modehdlc(bch, ISDN_P_NONE); |
| 844 | spin_unlock_irqrestore(&fc->lock, flags); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 845 | ch->protocol = ISDN_P_NONE; |
| 846 | ch->peer = NULL; |
| 847 | module_put(THIS_MODULE); |
| 848 | ret = 0; |
| 849 | break; |
| 850 | case CONTROL_CHANNEL: |
| 851 | ret = channel_bctrl(bch, arg); |
| 852 | break; |
| 853 | default: |
| 854 | pr_info("%s: %s unknown prim(%x)\n", fc->name, __func__, cmd); |
| 855 | } |
| 856 | return ret; |
| 857 | } |
| 858 | |
| 859 | static int |
| 860 | channel_ctrl(struct fritzcard *fc, struct mISDN_ctrl_req *cq) |
| 861 | { |
| 862 | int ret = 0; |
| 863 | |
| 864 | switch (cq->op) { |
| 865 | case MISDN_CTRL_GETOP: |
Karsten Keil | c626c12 | 2012-05-04 04:15:33 +0000 | [diff] [blame] | 866 | cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_L1_TIMER3; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 867 | break; |
| 868 | case MISDN_CTRL_LOOP: |
| 869 | /* cq->channel: 0 disable, 1 B1 loop 2 B2 loop, 3 both */ |
| 870 | if (cq->channel < 0 || cq->channel > 3) { |
| 871 | ret = -EINVAL; |
| 872 | break; |
| 873 | } |
| 874 | ret = fc->isac.ctrl(&fc->isac, HW_TESTLOOP, cq->channel); |
| 875 | break; |
Karsten Keil | c626c12 | 2012-05-04 04:15:33 +0000 | [diff] [blame] | 876 | case MISDN_CTRL_L1_TIMER3: |
| 877 | ret = fc->isac.ctrl(&fc->isac, HW_TIMER3_VALUE, cq->p1); |
| 878 | break; |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 879 | default: |
| 880 | pr_info("%s: %s unknown Op %x\n", fc->name, __func__, cq->op); |
| 881 | ret = -EINVAL; |
| 882 | break; |
| 883 | } |
| 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | static int |
| 888 | open_bchannel(struct fritzcard *fc, struct channel_req *rq) |
| 889 | { |
| 890 | struct bchannel *bch; |
| 891 | |
Dan Carpenter | 819a100 | 2012-03-26 21:20:48 +0000 | [diff] [blame] | 892 | if (rq->adr.channel == 0 || rq->adr.channel > 2) |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 893 | return -EINVAL; |
| 894 | if (rq->protocol == ISDN_P_NONE) |
| 895 | return -EINVAL; |
| 896 | bch = &fc->bch[rq->adr.channel - 1]; |
| 897 | if (test_and_set_bit(FLG_OPEN, &bch->Flags)) |
| 898 | return -EBUSY; /* b-channel can be only open once */ |
| 899 | test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags); |
| 900 | bch->ch.protocol = rq->protocol; |
| 901 | rq->ch = &bch->ch; |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * device control function |
| 907 | */ |
| 908 | static int |
| 909 | avm_dctrl(struct mISDNchannel *ch, u32 cmd, void *arg) |
| 910 | { |
| 911 | struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D); |
| 912 | struct dchannel *dch = container_of(dev, struct dchannel, dev); |
| 913 | struct fritzcard *fc = dch->hw; |
| 914 | struct channel_req *rq; |
| 915 | int err = 0; |
| 916 | |
| 917 | pr_debug("%s: %s cmd:%x %p\n", fc->name, __func__, cmd, arg); |
| 918 | switch (cmd) { |
| 919 | case OPEN_CHANNEL: |
| 920 | rq = arg; |
| 921 | if (rq->protocol == ISDN_P_TE_S0) |
| 922 | err = fc->isac.open(&fc->isac, rq); |
| 923 | else |
| 924 | err = open_bchannel(fc, rq); |
| 925 | if (err) |
| 926 | break; |
| 927 | if (!try_module_get(THIS_MODULE)) |
| 928 | pr_info("%s: cannot get module\n", fc->name); |
| 929 | break; |
| 930 | case CLOSE_CHANNEL: |
| 931 | pr_debug("%s: dev(%d) close from %p\n", fc->name, dch->dev.id, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 932 | __builtin_return_address(0)); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 933 | module_put(THIS_MODULE); |
| 934 | break; |
| 935 | case CONTROL_CHANNEL: |
| 936 | err = channel_ctrl(fc, arg); |
| 937 | break; |
| 938 | default: |
| 939 | pr_debug("%s: %s unknown command %x\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 940 | fc->name, __func__, cmd); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 941 | return -EINVAL; |
| 942 | } |
| 943 | return err; |
| 944 | } |
| 945 | |
| 946 | int |
| 947 | setup_fritz(struct fritzcard *fc) |
| 948 | { |
| 949 | u32 val, ver; |
| 950 | |
| 951 | if (!request_region(fc->addr, 32, fc->name)) { |
| 952 | pr_info("%s: AVM config port %x-%x already in use\n", |
| 953 | fc->name, fc->addr, fc->addr + 31); |
| 954 | return -EIO; |
| 955 | } |
| 956 | switch (fc->type) { |
| 957 | case AVM_FRITZ_PCI: |
| 958 | val = inl(fc->addr); |
| 959 | outl(AVM_HDLC_1, fc->addr + CHIP_INDEX); |
| 960 | ver = inl(fc->addr + CHIP_WINDOW + HDLC_STATUS) >> 24; |
| 961 | if (debug & DEBUG_HW) { |
| 962 | pr_notice("%s: PCI stat %#x\n", fc->name, val); |
| 963 | pr_notice("%s: PCI Class %X Rev %d\n", fc->name, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 964 | val & 0xff, (val >> 8) & 0xff); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 965 | pr_notice("%s: HDLC version %x\n", fc->name, ver & 0xf); |
| 966 | } |
| 967 | ASSIGN_FUNC(V1, ISAC, fc->isac); |
| 968 | fc->isac.type = IPAC_TYPE_ISAC; |
| 969 | break; |
| 970 | case AVM_FRITZ_PCIV2: |
| 971 | val = inl(fc->addr); |
| 972 | ver = inl(fc->addr + AVM_HDLC_STATUS_1) >> 24; |
| 973 | if (debug & DEBUG_HW) { |
| 974 | pr_notice("%s: PCI V2 stat %#x\n", fc->name, val); |
| 975 | pr_notice("%s: PCI V2 Class %X Rev %d\n", fc->name, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 976 | val & 0xff, (val >> 8) & 0xff); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 977 | pr_notice("%s: HDLC version %x\n", fc->name, ver & 0xf); |
| 978 | } |
| 979 | ASSIGN_FUNC(V2, ISAC, fc->isac); |
| 980 | fc->isac.type = IPAC_TYPE_ISACX; |
| 981 | break; |
| 982 | default: |
| 983 | release_region(fc->addr, 32); |
| 984 | pr_info("%s: AVM unknown type %d\n", fc->name, fc->type); |
| 985 | return -ENODEV; |
| 986 | } |
| 987 | pr_notice("%s: %s config irq:%d base:0x%X\n", fc->name, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 988 | (fc->type == AVM_FRITZ_PCI) ? "AVM Fritz!CARD PCI" : |
| 989 | "AVM Fritz!CARD PCIv2", fc->irq, fc->addr); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | release_card(struct fritzcard *card) |
| 995 | { |
| 996 | u_long flags; |
| 997 | |
| 998 | disable_hwirq(card); |
| 999 | spin_lock_irqsave(&card->lock, flags); |
| 1000 | modehdlc(&card->bch[0], ISDN_P_NONE); |
| 1001 | modehdlc(&card->bch[1], ISDN_P_NONE); |
| 1002 | spin_unlock_irqrestore(&card->lock, flags); |
| 1003 | card->isac.release(&card->isac); |
| 1004 | free_irq(card->irq, card); |
| 1005 | mISDN_freebchannel(&card->bch[1]); |
| 1006 | mISDN_freebchannel(&card->bch[0]); |
| 1007 | mISDN_unregister_device(&card->isac.dch.dev); |
| 1008 | release_region(card->addr, 32); |
| 1009 | pci_disable_device(card->pdev); |
| 1010 | pci_set_drvdata(card->pdev, NULL); |
| 1011 | write_lock_irqsave(&card_lock, flags); |
| 1012 | list_del(&card->list); |
| 1013 | write_unlock_irqrestore(&card_lock, flags); |
| 1014 | kfree(card); |
| 1015 | AVM_cnt--; |
| 1016 | } |
| 1017 | |
| 1018 | static int __devinit |
| 1019 | setup_instance(struct fritzcard *card) |
| 1020 | { |
| 1021 | int i, err; |
| 1022 | u_long flags; |
| 1023 | |
| 1024 | snprintf(card->name, MISDN_MAX_IDLEN - 1, "AVM.%d", AVM_cnt + 1); |
| 1025 | write_lock_irqsave(&card_lock, flags); |
| 1026 | list_add_tail(&card->list, &Cards); |
| 1027 | write_unlock_irqrestore(&card_lock, flags); |
| 1028 | |
| 1029 | _set_debug(card); |
| 1030 | card->isac.name = card->name; |
| 1031 | spin_lock_init(&card->lock); |
| 1032 | card->isac.hwlock = &card->lock; |
| 1033 | mISDNisac_init(&card->isac, card); |
| 1034 | |
| 1035 | card->isac.dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 1036 | (1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK)); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 1037 | card->isac.dch.dev.D.ctrl = avm_dctrl; |
| 1038 | for (i = 0; i < 2; i++) { |
| 1039 | card->bch[i].nr = i + 1; |
| 1040 | set_channelmap(i + 1, card->isac.dch.dev.channelmap); |
| 1041 | mISDN_initbchannel(&card->bch[i], MAX_DATA_MEM); |
| 1042 | card->bch[i].hw = card; |
| 1043 | card->bch[i].ch.send = avm_l2l1B; |
| 1044 | card->bch[i].ch.ctrl = avm_bctrl; |
| 1045 | card->bch[i].ch.nr = i + 1; |
| 1046 | list_add(&card->bch[i].ch.list, &card->isac.dch.dev.bchannels); |
| 1047 | } |
| 1048 | err = setup_fritz(card); |
| 1049 | if (err) |
| 1050 | goto error; |
| 1051 | err = mISDN_register_device(&card->isac.dch.dev, &card->pdev->dev, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 1052 | card->name); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 1053 | if (err) |
| 1054 | goto error_reg; |
| 1055 | err = init_card(card); |
| 1056 | if (!err) { |
| 1057 | AVM_cnt++; |
| 1058 | pr_notice("AVM %d cards installed DEBUG\n", AVM_cnt); |
| 1059 | return 0; |
| 1060 | } |
| 1061 | mISDN_unregister_device(&card->isac.dch.dev); |
| 1062 | error_reg: |
| 1063 | release_region(card->addr, 32); |
| 1064 | error: |
| 1065 | card->isac.release(&card->isac); |
| 1066 | mISDN_freebchannel(&card->bch[1]); |
| 1067 | mISDN_freebchannel(&card->bch[0]); |
| 1068 | write_lock_irqsave(&card_lock, flags); |
| 1069 | list_del(&card->list); |
| 1070 | write_unlock_irqrestore(&card_lock, flags); |
| 1071 | kfree(card); |
| 1072 | return err; |
| 1073 | } |
| 1074 | |
| 1075 | static int __devinit |
| 1076 | fritzpci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 1077 | { |
| 1078 | int err = -ENOMEM; |
| 1079 | struct fritzcard *card; |
| 1080 | |
| 1081 | card = kzalloc(sizeof(struct fritzcard), GFP_KERNEL); |
| 1082 | if (!card) { |
| 1083 | pr_info("No kmem for fritzcard\n"); |
| 1084 | return err; |
| 1085 | } |
| 1086 | if (pdev->device == PCI_DEVICE_ID_AVM_A1_V2) |
| 1087 | card->type = AVM_FRITZ_PCIV2; |
| 1088 | else |
| 1089 | card->type = AVM_FRITZ_PCI; |
| 1090 | card->pdev = pdev; |
| 1091 | err = pci_enable_device(pdev); |
| 1092 | if (err) { |
| 1093 | kfree(card); |
| 1094 | return err; |
| 1095 | } |
| 1096 | |
| 1097 | pr_notice("mISDN: found adapter %s at %s\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 1098 | (char *) ent->driver_data, pci_name(pdev)); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 1099 | |
| 1100 | card->addr = pci_resource_start(pdev, 1); |
| 1101 | card->irq = pdev->irq; |
| 1102 | pci_set_drvdata(pdev, card); |
| 1103 | err = setup_instance(card); |
| 1104 | if (err) |
| 1105 | pci_set_drvdata(pdev, NULL); |
| 1106 | return err; |
| 1107 | } |
| 1108 | |
| 1109 | static void __devexit |
| 1110 | fritz_remove_pci(struct pci_dev *pdev) |
| 1111 | { |
| 1112 | struct fritzcard *card = pci_get_drvdata(pdev); |
| 1113 | |
| 1114 | if (card) |
| 1115 | release_card(card); |
| 1116 | else |
| 1117 | if (debug) |
Uwe Kleine-König | 698f931 | 2010-07-02 20:41:51 +0200 | [diff] [blame] | 1118 | pr_info("%s: drvdata already removed\n", __func__); |
Karsten Keil | 6115d2f | 2009-07-22 19:52:24 +0200 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | static struct pci_device_id fcpci_ids[] __devinitdata = { |
| 1122 | { PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_A1, PCI_ANY_ID, PCI_ANY_ID, |
| 1123 | 0, 0, (unsigned long) "Fritz!Card PCI"}, |
| 1124 | { PCI_VENDOR_ID_AVM, PCI_DEVICE_ID_AVM_A1_V2, PCI_ANY_ID, PCI_ANY_ID, |
| 1125 | 0, 0, (unsigned long) "Fritz!Card PCI v2" }, |
| 1126 | { } |
| 1127 | }; |
| 1128 | MODULE_DEVICE_TABLE(pci, fcpci_ids); |
| 1129 | |
| 1130 | static struct pci_driver fcpci_driver = { |
| 1131 | .name = "fcpci", |
| 1132 | .probe = fritzpci_probe, |
| 1133 | .remove = __devexit_p(fritz_remove_pci), |
| 1134 | .id_table = fcpci_ids, |
| 1135 | }; |
| 1136 | |
| 1137 | static int __init AVM_init(void) |
| 1138 | { |
| 1139 | int err; |
| 1140 | |
| 1141 | pr_notice("AVM Fritz PCI driver Rev. %s\n", AVMFRITZ_REV); |
| 1142 | err = pci_register_driver(&fcpci_driver); |
| 1143 | return err; |
| 1144 | } |
| 1145 | |
| 1146 | static void __exit AVM_cleanup(void) |
| 1147 | { |
| 1148 | pci_unregister_driver(&fcpci_driver); |
| 1149 | } |
| 1150 | |
| 1151 | module_init(AVM_init); |
| 1152 | module_exit(AVM_cleanup); |