18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * w6692.c     mISDN driver for Winbond w6692 based cards
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author      Karsten Keil <kkeil@suse.de>
68c2ecf20Sopenharmony_ci *             based on the w6692 I4L driver from Petr Novak <petr.novak@i.cz>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright 2009  by Karsten Keil <keil@isdn4linux.de>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/pci.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/mISDNhw.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include "w6692.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define W6692_REV	"2.0"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define DBUSY_TIMER_VALUE	80
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cienum {
248c2ecf20Sopenharmony_ci	W6692_ASUS,
258c2ecf20Sopenharmony_ci	W6692_WINBOND,
268c2ecf20Sopenharmony_ci	W6692_USR
278c2ecf20Sopenharmony_ci};
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* private data in the PCI devices list */
308c2ecf20Sopenharmony_cistruct w6692map {
318c2ecf20Sopenharmony_ci	u_int	subtype;
328c2ecf20Sopenharmony_ci	char	*name;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic const struct w6692map  w6692_map[] =
368c2ecf20Sopenharmony_ci{
378c2ecf20Sopenharmony_ci	{W6692_ASUS, "Dynalink/AsusCom IS64PH"},
388c2ecf20Sopenharmony_ci	{W6692_WINBOND, "Winbond W6692"},
398c2ecf20Sopenharmony_ci	{W6692_USR, "USR W6692"}
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define PCI_DEVICE_ID_USR_6692	0x3409
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistruct w6692_ch {
458c2ecf20Sopenharmony_ci	struct bchannel		bch;
468c2ecf20Sopenharmony_ci	u32			addr;
478c2ecf20Sopenharmony_ci	struct timer_list	timer;
488c2ecf20Sopenharmony_ci	u8			b_mode;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistruct w6692_hw {
528c2ecf20Sopenharmony_ci	struct list_head	list;
538c2ecf20Sopenharmony_ci	struct pci_dev		*pdev;
548c2ecf20Sopenharmony_ci	char			name[MISDN_MAX_IDLEN];
558c2ecf20Sopenharmony_ci	u32			irq;
568c2ecf20Sopenharmony_ci	u32			irqcnt;
578c2ecf20Sopenharmony_ci	u32			addr;
588c2ecf20Sopenharmony_ci	u32			fmask;	/* feature mask - bit set per card nr */
598c2ecf20Sopenharmony_ci	int			subtype;
608c2ecf20Sopenharmony_ci	spinlock_t		lock;	/* hw lock */
618c2ecf20Sopenharmony_ci	u8			imask;
628c2ecf20Sopenharmony_ci	u8			pctl;
638c2ecf20Sopenharmony_ci	u8			xaddr;
648c2ecf20Sopenharmony_ci	u8			xdata;
658c2ecf20Sopenharmony_ci	u8			state;
668c2ecf20Sopenharmony_ci	struct w6692_ch		bc[2];
678c2ecf20Sopenharmony_ci	struct dchannel		dch;
688c2ecf20Sopenharmony_ci	char			log[64];
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic LIST_HEAD(Cards);
728c2ecf20Sopenharmony_cistatic DEFINE_RWLOCK(card_lock); /* protect Cards */
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int w6692_cnt;
758c2ecf20Sopenharmony_cistatic int debug;
768c2ecf20Sopenharmony_cistatic u32 led;
778c2ecf20Sopenharmony_cistatic u32 pots;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic void
808c2ecf20Sopenharmony_ci_set_debug(struct w6692_hw *card)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	card->dch.debug = debug;
838c2ecf20Sopenharmony_ci	card->bc[0].bch.debug = debug;
848c2ecf20Sopenharmony_ci	card->bc[1].bch.debug = debug;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int
888c2ecf20Sopenharmony_ciset_debug(const char *val, const struct kernel_param *kp)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	int ret;
918c2ecf20Sopenharmony_ci	struct w6692_hw *card;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	ret = param_set_uint(val, kp);
948c2ecf20Sopenharmony_ci	if (!ret) {
958c2ecf20Sopenharmony_ci		read_lock(&card_lock);
968c2ecf20Sopenharmony_ci		list_for_each_entry(card, &Cards, list)
978c2ecf20Sopenharmony_ci			_set_debug(card);
988c2ecf20Sopenharmony_ci		read_unlock(&card_lock);
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci	return ret;
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Karsten Keil");
1048c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
1058c2ecf20Sopenharmony_ciMODULE_VERSION(W6692_REV);
1068c2ecf20Sopenharmony_cimodule_param_call(debug, set_debug, param_get_uint, &debug, S_IRUGO | S_IWUSR);
1078c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "W6692 debug mask");
1088c2ecf20Sopenharmony_cimodule_param(led, uint, S_IRUGO | S_IWUSR);
1098c2ecf20Sopenharmony_ciMODULE_PARM_DESC(led, "W6692 LED support bitmask (one bit per card)");
1108c2ecf20Sopenharmony_cimodule_param(pots, uint, S_IRUGO | S_IWUSR);
1118c2ecf20Sopenharmony_ciMODULE_PARM_DESC(pots, "W6692 POTS support bitmask (one bit per card)");
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic inline u8
1148c2ecf20Sopenharmony_ciReadW6692(struct w6692_hw *card, u8 offset)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	return inb(card->addr + offset);
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic inline void
1208c2ecf20Sopenharmony_ciWriteW6692(struct w6692_hw *card, u8 offset, u8 value)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	outb(value, card->addr + offset);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic inline u8
1268c2ecf20Sopenharmony_ciReadW6692B(struct w6692_ch *bc, u8 offset)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	return inb(bc->addr + offset);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline void
1328c2ecf20Sopenharmony_ciWriteW6692B(struct w6692_ch *bc, u8 offset, u8 value)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	outb(value, bc->addr + offset);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic void
1388c2ecf20Sopenharmony_cienable_hwirq(struct w6692_hw *card)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	WriteW6692(card, W_IMASK, card->imask);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic void
1448c2ecf20Sopenharmony_cidisable_hwirq(struct w6692_hw *card)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	WriteW6692(card, W_IMASK, 0xff);
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic const char *W6692Ver[] = {"V00", "V01", "V10", "V11"};
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic void
1528c2ecf20Sopenharmony_ciW6692Version(struct w6692_hw *card)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	int val;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	val = ReadW6692(card, W_D_RBCH);
1578c2ecf20Sopenharmony_ci	pr_notice("%s: Winbond W6692 version: %s\n", card->name,
1588c2ecf20Sopenharmony_ci		  W6692Ver[(val >> 6) & 3]);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic void
1628c2ecf20Sopenharmony_ciw6692_led_handler(struct w6692_hw *card, int on)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	if ((!(card->fmask & led)) || card->subtype == W6692_USR)
1658c2ecf20Sopenharmony_ci		return;
1668c2ecf20Sopenharmony_ci	if (on) {
1678c2ecf20Sopenharmony_ci		card->xdata &= 0xfb;	/*  LED ON */
1688c2ecf20Sopenharmony_ci		WriteW6692(card, W_XDATA, card->xdata);
1698c2ecf20Sopenharmony_ci	} else {
1708c2ecf20Sopenharmony_ci		card->xdata |= 0x04;	/*  LED OFF */
1718c2ecf20Sopenharmony_ci		WriteW6692(card, W_XDATA, card->xdata);
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic void
1768c2ecf20Sopenharmony_ciph_command(struct w6692_hw *card, u8 cmd)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	pr_debug("%s: ph_command %x\n", card->name, cmd);
1798c2ecf20Sopenharmony_ci	WriteW6692(card, W_CIX, cmd);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic void
1838c2ecf20Sopenharmony_ciW6692_new_ph(struct w6692_hw *card)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	if (card->state == W_L1CMD_RST)
1868c2ecf20Sopenharmony_ci		ph_command(card, W_L1CMD_DRC);
1878c2ecf20Sopenharmony_ci	schedule_event(&card->dch, FLG_PHCHANGE);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void
1918c2ecf20Sopenharmony_ciW6692_ph_bh(struct dchannel *dch)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct w6692_hw *card = dch->hw;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	switch (card->state) {
1968c2ecf20Sopenharmony_ci	case W_L1CMD_RST:
1978c2ecf20Sopenharmony_ci		dch->state = 0;
1988c2ecf20Sopenharmony_ci		l1_event(dch->l1, HW_RESET_IND);
1998c2ecf20Sopenharmony_ci		break;
2008c2ecf20Sopenharmony_ci	case W_L1IND_CD:
2018c2ecf20Sopenharmony_ci		dch->state = 3;
2028c2ecf20Sopenharmony_ci		l1_event(dch->l1, HW_DEACT_CNF);
2038c2ecf20Sopenharmony_ci		break;
2048c2ecf20Sopenharmony_ci	case W_L1IND_DRD:
2058c2ecf20Sopenharmony_ci		dch->state = 3;
2068c2ecf20Sopenharmony_ci		l1_event(dch->l1, HW_DEACT_IND);
2078c2ecf20Sopenharmony_ci		break;
2088c2ecf20Sopenharmony_ci	case W_L1IND_CE:
2098c2ecf20Sopenharmony_ci		dch->state = 4;
2108c2ecf20Sopenharmony_ci		l1_event(dch->l1, HW_POWERUP_IND);
2118c2ecf20Sopenharmony_ci		break;
2128c2ecf20Sopenharmony_ci	case W_L1IND_LD:
2138c2ecf20Sopenharmony_ci		if (dch->state <= 5) {
2148c2ecf20Sopenharmony_ci			dch->state = 5;
2158c2ecf20Sopenharmony_ci			l1_event(dch->l1, ANYSIGNAL);
2168c2ecf20Sopenharmony_ci		} else {
2178c2ecf20Sopenharmony_ci			dch->state = 8;
2188c2ecf20Sopenharmony_ci			l1_event(dch->l1, LOSTFRAMING);
2198c2ecf20Sopenharmony_ci		}
2208c2ecf20Sopenharmony_ci		break;
2218c2ecf20Sopenharmony_ci	case W_L1IND_ARD:
2228c2ecf20Sopenharmony_ci		dch->state = 6;
2238c2ecf20Sopenharmony_ci		l1_event(dch->l1, INFO2);
2248c2ecf20Sopenharmony_ci		break;
2258c2ecf20Sopenharmony_ci	case W_L1IND_AI8:
2268c2ecf20Sopenharmony_ci		dch->state = 7;
2278c2ecf20Sopenharmony_ci		l1_event(dch->l1, INFO4_P8);
2288c2ecf20Sopenharmony_ci		break;
2298c2ecf20Sopenharmony_ci	case W_L1IND_AI10:
2308c2ecf20Sopenharmony_ci		dch->state = 7;
2318c2ecf20Sopenharmony_ci		l1_event(dch->l1, INFO4_P10);
2328c2ecf20Sopenharmony_ci		break;
2338c2ecf20Sopenharmony_ci	default:
2348c2ecf20Sopenharmony_ci		pr_debug("%s: TE unknown state %02x dch state %02x\n",
2358c2ecf20Sopenharmony_ci			 card->name, card->state, dch->state);
2368c2ecf20Sopenharmony_ci		break;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci	pr_debug("%s: TE newstate %02x\n", card->name, dch->state);
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic void
2428c2ecf20Sopenharmony_ciW6692_empty_Dfifo(struct w6692_hw *card, int count)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	struct dchannel *dch = &card->dch;
2458c2ecf20Sopenharmony_ci	u8 *ptr;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	pr_debug("%s: empty_Dfifo %d\n", card->name, count);
2488c2ecf20Sopenharmony_ci	if (!dch->rx_skb) {
2498c2ecf20Sopenharmony_ci		dch->rx_skb = mI_alloc_skb(card->dch.maxlen, GFP_ATOMIC);
2508c2ecf20Sopenharmony_ci		if (!dch->rx_skb) {
2518c2ecf20Sopenharmony_ci			pr_info("%s: D receive out of memory\n", card->name);
2528c2ecf20Sopenharmony_ci			WriteW6692(card, W_D_CMDR, W_D_CMDR_RACK);
2538c2ecf20Sopenharmony_ci			return;
2548c2ecf20Sopenharmony_ci		}
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci	if ((dch->rx_skb->len + count) >= dch->maxlen) {
2578c2ecf20Sopenharmony_ci		pr_debug("%s: empty_Dfifo overrun %d\n", card->name,
2588c2ecf20Sopenharmony_ci			 dch->rx_skb->len + count);
2598c2ecf20Sopenharmony_ci		WriteW6692(card, W_D_CMDR, W_D_CMDR_RACK);
2608c2ecf20Sopenharmony_ci		return;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci	ptr = skb_put(dch->rx_skb, count);
2638c2ecf20Sopenharmony_ci	insb(card->addr + W_D_RFIFO, ptr, count);
2648c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_CMDR, W_D_CMDR_RACK);
2658c2ecf20Sopenharmony_ci	if (debug & DEBUG_HW_DFIFO) {
2668c2ecf20Sopenharmony_ci		snprintf(card->log, 63, "D-recv %s %d ",
2678c2ecf20Sopenharmony_ci			 card->name, count);
2688c2ecf20Sopenharmony_ci		print_hex_dump_bytes(card->log, DUMP_PREFIX_OFFSET, ptr, count);
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic void
2738c2ecf20Sopenharmony_ciW6692_fill_Dfifo(struct w6692_hw *card)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	struct dchannel *dch = &card->dch;
2768c2ecf20Sopenharmony_ci	int count;
2778c2ecf20Sopenharmony_ci	u8 *ptr;
2788c2ecf20Sopenharmony_ci	u8 cmd = W_D_CMDR_XMS;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	pr_debug("%s: fill_Dfifo\n", card->name);
2818c2ecf20Sopenharmony_ci	if (!dch->tx_skb)
2828c2ecf20Sopenharmony_ci		return;
2838c2ecf20Sopenharmony_ci	count = dch->tx_skb->len - dch->tx_idx;
2848c2ecf20Sopenharmony_ci	if (count <= 0)
2858c2ecf20Sopenharmony_ci		return;
2868c2ecf20Sopenharmony_ci	if (count > W_D_FIFO_THRESH)
2878c2ecf20Sopenharmony_ci		count = W_D_FIFO_THRESH;
2888c2ecf20Sopenharmony_ci	else
2898c2ecf20Sopenharmony_ci		cmd |= W_D_CMDR_XME;
2908c2ecf20Sopenharmony_ci	ptr = dch->tx_skb->data + dch->tx_idx;
2918c2ecf20Sopenharmony_ci	dch->tx_idx += count;
2928c2ecf20Sopenharmony_ci	outsb(card->addr + W_D_XFIFO, ptr, count);
2938c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_CMDR, cmd);
2948c2ecf20Sopenharmony_ci	if (test_and_set_bit(FLG_BUSY_TIMER, &dch->Flags)) {
2958c2ecf20Sopenharmony_ci		pr_debug("%s: fill_Dfifo dbusytimer running\n", card->name);
2968c2ecf20Sopenharmony_ci		del_timer(&dch->timer);
2978c2ecf20Sopenharmony_ci	}
2988c2ecf20Sopenharmony_ci	dch->timer.expires = jiffies + ((DBUSY_TIMER_VALUE * HZ) / 1000);
2998c2ecf20Sopenharmony_ci	add_timer(&dch->timer);
3008c2ecf20Sopenharmony_ci	if (debug & DEBUG_HW_DFIFO) {
3018c2ecf20Sopenharmony_ci		snprintf(card->log, 63, "D-send %s %d ",
3028c2ecf20Sopenharmony_ci			 card->name, count);
3038c2ecf20Sopenharmony_ci		print_hex_dump_bytes(card->log, DUMP_PREFIX_OFFSET, ptr, count);
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic void
3088c2ecf20Sopenharmony_cid_retransmit(struct w6692_hw *card)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	struct dchannel *dch = &card->dch;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
3138c2ecf20Sopenharmony_ci		del_timer(&dch->timer);
3148c2ecf20Sopenharmony_ci#ifdef FIXME
3158c2ecf20Sopenharmony_ci	if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
3168c2ecf20Sopenharmony_ci		dchannel_sched_event(dch, D_CLEARBUSY);
3178c2ecf20Sopenharmony_ci#endif
3188c2ecf20Sopenharmony_ci	if (test_bit(FLG_TX_BUSY, &dch->Flags)) {
3198c2ecf20Sopenharmony_ci		/* Restart frame */
3208c2ecf20Sopenharmony_ci		dch->tx_idx = 0;
3218c2ecf20Sopenharmony_ci		W6692_fill_Dfifo(card);
3228c2ecf20Sopenharmony_ci	} else if (dch->tx_skb) { /* should not happen */
3238c2ecf20Sopenharmony_ci		pr_info("%s: %s without TX_BUSY\n", card->name, __func__);
3248c2ecf20Sopenharmony_ci		test_and_set_bit(FLG_TX_BUSY, &dch->Flags);
3258c2ecf20Sopenharmony_ci		dch->tx_idx = 0;
3268c2ecf20Sopenharmony_ci		W6692_fill_Dfifo(card);
3278c2ecf20Sopenharmony_ci	} else {
3288c2ecf20Sopenharmony_ci		pr_info("%s: XDU no TX_BUSY\n", card->name);
3298c2ecf20Sopenharmony_ci		if (get_next_dframe(dch))
3308c2ecf20Sopenharmony_ci			W6692_fill_Dfifo(card);
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic void
3358c2ecf20Sopenharmony_cihandle_rxD(struct w6692_hw *card) {
3368c2ecf20Sopenharmony_ci	u8	stat;
3378c2ecf20Sopenharmony_ci	int	count;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	stat = ReadW6692(card, W_D_RSTA);
3408c2ecf20Sopenharmony_ci	if (stat & (W_D_RSTA_RDOV | W_D_RSTA_CRCE | W_D_RSTA_RMB)) {
3418c2ecf20Sopenharmony_ci		if (stat & W_D_RSTA_RDOV) {
3428c2ecf20Sopenharmony_ci			pr_debug("%s: D-channel RDOV\n", card->name);
3438c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
3448c2ecf20Sopenharmony_ci			card->dch.err_rx++;
3458c2ecf20Sopenharmony_ci#endif
3468c2ecf20Sopenharmony_ci		}
3478c2ecf20Sopenharmony_ci		if (stat & W_D_RSTA_CRCE) {
3488c2ecf20Sopenharmony_ci			pr_debug("%s: D-channel CRC error\n", card->name);
3498c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
3508c2ecf20Sopenharmony_ci			card->dch.err_crc++;
3518c2ecf20Sopenharmony_ci#endif
3528c2ecf20Sopenharmony_ci		}
3538c2ecf20Sopenharmony_ci		if (stat & W_D_RSTA_RMB) {
3548c2ecf20Sopenharmony_ci			pr_debug("%s: D-channel ABORT\n", card->name);
3558c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
3568c2ecf20Sopenharmony_ci			card->dch.err_rx++;
3578c2ecf20Sopenharmony_ci#endif
3588c2ecf20Sopenharmony_ci		}
3598c2ecf20Sopenharmony_ci		dev_kfree_skb(card->dch.rx_skb);
3608c2ecf20Sopenharmony_ci		card->dch.rx_skb = NULL;
3618c2ecf20Sopenharmony_ci		WriteW6692(card, W_D_CMDR, W_D_CMDR_RACK | W_D_CMDR_RRST);
3628c2ecf20Sopenharmony_ci	} else {
3638c2ecf20Sopenharmony_ci		count = ReadW6692(card, W_D_RBCL) & (W_D_FIFO_THRESH - 1);
3648c2ecf20Sopenharmony_ci		if (count == 0)
3658c2ecf20Sopenharmony_ci			count = W_D_FIFO_THRESH;
3668c2ecf20Sopenharmony_ci		W6692_empty_Dfifo(card, count);
3678c2ecf20Sopenharmony_ci		recv_Dchannel(&card->dch);
3688c2ecf20Sopenharmony_ci	}
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic void
3728c2ecf20Sopenharmony_cihandle_txD(struct w6692_hw *card) {
3738c2ecf20Sopenharmony_ci	if (test_and_clear_bit(FLG_BUSY_TIMER, &card->dch.Flags))
3748c2ecf20Sopenharmony_ci		del_timer(&card->dch.timer);
3758c2ecf20Sopenharmony_ci	if (card->dch.tx_skb && card->dch.tx_idx < card->dch.tx_skb->len) {
3768c2ecf20Sopenharmony_ci		W6692_fill_Dfifo(card);
3778c2ecf20Sopenharmony_ci	} else {
3788c2ecf20Sopenharmony_ci		dev_kfree_skb(card->dch.tx_skb);
3798c2ecf20Sopenharmony_ci		if (get_next_dframe(&card->dch))
3808c2ecf20Sopenharmony_ci			W6692_fill_Dfifo(card);
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic void
3858c2ecf20Sopenharmony_cihandle_statusD(struct w6692_hw *card)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	struct dchannel *dch = &card->dch;
3888c2ecf20Sopenharmony_ci	u8 exval, v1, cir;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	exval = ReadW6692(card, W_D_EXIR);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	pr_debug("%s: D_EXIR %02x\n", card->name, exval);
3938c2ecf20Sopenharmony_ci	if (exval & (W_D_EXI_XDUN | W_D_EXI_XCOL)) {
3948c2ecf20Sopenharmony_ci		/* Transmit underrun/collision */
3958c2ecf20Sopenharmony_ci		pr_debug("%s: D-channel underrun/collision\n", card->name);
3968c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
3978c2ecf20Sopenharmony_ci		dch->err_tx++;
3988c2ecf20Sopenharmony_ci#endif
3998c2ecf20Sopenharmony_ci		d_retransmit(card);
4008c2ecf20Sopenharmony_ci	}
4018c2ecf20Sopenharmony_ci	if (exval & W_D_EXI_RDOV) {	/* RDOV */
4028c2ecf20Sopenharmony_ci		pr_debug("%s: D-channel RDOV\n", card->name);
4038c2ecf20Sopenharmony_ci		WriteW6692(card, W_D_CMDR, W_D_CMDR_RRST);
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	if (exval & W_D_EXI_TIN2)	/* TIN2 - never */
4068c2ecf20Sopenharmony_ci		pr_debug("%s: spurious TIN2 interrupt\n", card->name);
4078c2ecf20Sopenharmony_ci	if (exval & W_D_EXI_MOC) {	/* MOC - not supported */
4088c2ecf20Sopenharmony_ci		v1 = ReadW6692(card, W_MOSR);
4098c2ecf20Sopenharmony_ci		pr_debug("%s: spurious MOC interrupt MOSR %02x\n",
4108c2ecf20Sopenharmony_ci			 card->name, v1);
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci	if (exval & W_D_EXI_ISC) {	/* ISC - Level1 change */
4138c2ecf20Sopenharmony_ci		cir = ReadW6692(card, W_CIR);
4148c2ecf20Sopenharmony_ci		pr_debug("%s: ISC CIR %02X\n", card->name, cir);
4158c2ecf20Sopenharmony_ci		if (cir & W_CIR_ICC) {
4168c2ecf20Sopenharmony_ci			v1 = cir & W_CIR_COD_MASK;
4178c2ecf20Sopenharmony_ci			pr_debug("%s: ph_state_change %x -> %x\n", card->name,
4188c2ecf20Sopenharmony_ci				 dch->state, v1);
4198c2ecf20Sopenharmony_ci			card->state = v1;
4208c2ecf20Sopenharmony_ci			if (card->fmask & led) {
4218c2ecf20Sopenharmony_ci				switch (v1) {
4228c2ecf20Sopenharmony_ci				case W_L1IND_AI8:
4238c2ecf20Sopenharmony_ci				case W_L1IND_AI10:
4248c2ecf20Sopenharmony_ci					w6692_led_handler(card, 1);
4258c2ecf20Sopenharmony_ci					break;
4268c2ecf20Sopenharmony_ci				default:
4278c2ecf20Sopenharmony_ci					w6692_led_handler(card, 0);
4288c2ecf20Sopenharmony_ci					break;
4298c2ecf20Sopenharmony_ci				}
4308c2ecf20Sopenharmony_ci			}
4318c2ecf20Sopenharmony_ci			W6692_new_ph(card);
4328c2ecf20Sopenharmony_ci		}
4338c2ecf20Sopenharmony_ci		if (cir & W_CIR_SCC) {
4348c2ecf20Sopenharmony_ci			v1 = ReadW6692(card, W_SQR);
4358c2ecf20Sopenharmony_ci			pr_debug("%s: SCC SQR %02X\n", card->name, v1);
4368c2ecf20Sopenharmony_ci		}
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci	if (exval & W_D_EXI_WEXP)
4398c2ecf20Sopenharmony_ci		pr_debug("%s: spurious WEXP interrupt!\n", card->name);
4408c2ecf20Sopenharmony_ci	if (exval & W_D_EXI_TEXP)
4418c2ecf20Sopenharmony_ci		pr_debug("%s: spurious TEXP interrupt!\n", card->name);
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_cistatic void
4458c2ecf20Sopenharmony_ciW6692_empty_Bfifo(struct w6692_ch *wch, int count)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct w6692_hw *card = wch->bch.hw;
4488c2ecf20Sopenharmony_ci	u8 *ptr;
4498c2ecf20Sopenharmony_ci	int maxlen;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	pr_debug("%s: empty_Bfifo %d\n", card->name, count);
4528c2ecf20Sopenharmony_ci	if (unlikely(wch->bch.state == ISDN_P_NONE)) {
4538c2ecf20Sopenharmony_ci		pr_debug("%s: empty_Bfifo ISDN_P_NONE\n", card->name);
4548c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK | W_B_CMDR_RACT);
4558c2ecf20Sopenharmony_ci		if (wch->bch.rx_skb)
4568c2ecf20Sopenharmony_ci			skb_trim(wch->bch.rx_skb, 0);
4578c2ecf20Sopenharmony_ci		return;
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci	if (test_bit(FLG_RX_OFF, &wch->bch.Flags)) {
4608c2ecf20Sopenharmony_ci		wch->bch.dropcnt += count;
4618c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK | W_B_CMDR_RACT);
4628c2ecf20Sopenharmony_ci		return;
4638c2ecf20Sopenharmony_ci	}
4648c2ecf20Sopenharmony_ci	maxlen = bchannel_get_rxbuf(&wch->bch, count);
4658c2ecf20Sopenharmony_ci	if (maxlen < 0) {
4668c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK | W_B_CMDR_RACT);
4678c2ecf20Sopenharmony_ci		if (wch->bch.rx_skb)
4688c2ecf20Sopenharmony_ci			skb_trim(wch->bch.rx_skb, 0);
4698c2ecf20Sopenharmony_ci		pr_warn("%s.B%d: No bufferspace for %d bytes\n",
4708c2ecf20Sopenharmony_ci			card->name, wch->bch.nr, count);
4718c2ecf20Sopenharmony_ci		return;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci	ptr = skb_put(wch->bch.rx_skb, count);
4748c2ecf20Sopenharmony_ci	insb(wch->addr + W_B_RFIFO, ptr, count);
4758c2ecf20Sopenharmony_ci	WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK | W_B_CMDR_RACT);
4768c2ecf20Sopenharmony_ci	if (debug & DEBUG_HW_DFIFO) {
4778c2ecf20Sopenharmony_ci		snprintf(card->log, 63, "B%1d-recv %s %d ",
4788c2ecf20Sopenharmony_ci			 wch->bch.nr, card->name, count);
4798c2ecf20Sopenharmony_ci		print_hex_dump_bytes(card->log, DUMP_PREFIX_OFFSET, ptr, count);
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cistatic void
4848c2ecf20Sopenharmony_ciW6692_fill_Bfifo(struct w6692_ch *wch)
4858c2ecf20Sopenharmony_ci{
4868c2ecf20Sopenharmony_ci	struct w6692_hw *card = wch->bch.hw;
4878c2ecf20Sopenharmony_ci	int count, fillempty = 0;
4888c2ecf20Sopenharmony_ci	u8 *ptr, cmd = W_B_CMDR_RACT | W_B_CMDR_XMS;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	pr_debug("%s: fill Bfifo\n", card->name);
4918c2ecf20Sopenharmony_ci	if (!wch->bch.tx_skb) {
4928c2ecf20Sopenharmony_ci		if (!test_bit(FLG_TX_EMPTY, &wch->bch.Flags))
4938c2ecf20Sopenharmony_ci			return;
4948c2ecf20Sopenharmony_ci		ptr = wch->bch.fill;
4958c2ecf20Sopenharmony_ci		count = W_B_FIFO_THRESH;
4968c2ecf20Sopenharmony_ci		fillempty = 1;
4978c2ecf20Sopenharmony_ci	} else {
4988c2ecf20Sopenharmony_ci		count = wch->bch.tx_skb->len - wch->bch.tx_idx;
4998c2ecf20Sopenharmony_ci		if (count <= 0)
5008c2ecf20Sopenharmony_ci			return;
5018c2ecf20Sopenharmony_ci		ptr = wch->bch.tx_skb->data + wch->bch.tx_idx;
5028c2ecf20Sopenharmony_ci	}
5038c2ecf20Sopenharmony_ci	if (count > W_B_FIFO_THRESH)
5048c2ecf20Sopenharmony_ci		count = W_B_FIFO_THRESH;
5058c2ecf20Sopenharmony_ci	else if (test_bit(FLG_HDLC, &wch->bch.Flags))
5068c2ecf20Sopenharmony_ci		cmd |= W_B_CMDR_XME;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	pr_debug("%s: fill Bfifo%d/%d\n", card->name,
5098c2ecf20Sopenharmony_ci		 count, wch->bch.tx_idx);
5108c2ecf20Sopenharmony_ci	wch->bch.tx_idx += count;
5118c2ecf20Sopenharmony_ci	if (fillempty) {
5128c2ecf20Sopenharmony_ci		while (count > 0) {
5138c2ecf20Sopenharmony_ci			outsb(wch->addr + W_B_XFIFO, ptr, MISDN_BCH_FILL_SIZE);
5148c2ecf20Sopenharmony_ci			count -= MISDN_BCH_FILL_SIZE;
5158c2ecf20Sopenharmony_ci		}
5168c2ecf20Sopenharmony_ci	} else {
5178c2ecf20Sopenharmony_ci		outsb(wch->addr + W_B_XFIFO, ptr, count);
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci	WriteW6692B(wch, W_B_CMDR, cmd);
5208c2ecf20Sopenharmony_ci	if ((debug & DEBUG_HW_BFIFO) && !fillempty) {
5218c2ecf20Sopenharmony_ci		snprintf(card->log, 63, "B%1d-send %s %d ",
5228c2ecf20Sopenharmony_ci			 wch->bch.nr, card->name, count);
5238c2ecf20Sopenharmony_ci		print_hex_dump_bytes(card->log, DUMP_PREFIX_OFFSET, ptr, count);
5248c2ecf20Sopenharmony_ci	}
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci#if 0
5288c2ecf20Sopenharmony_cistatic int
5298c2ecf20Sopenharmony_cisetvolume(struct w6692_ch *wch, int mic, struct sk_buff *skb)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	struct w6692_hw *card = wch->bch.hw;
5328c2ecf20Sopenharmony_ci	u16 *vol = (u16 *)skb->data;
5338c2ecf20Sopenharmony_ci	u8 val;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	if ((!(card->fmask & pots)) ||
5368c2ecf20Sopenharmony_ci	    !test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
5378c2ecf20Sopenharmony_ci		return -ENODEV;
5388c2ecf20Sopenharmony_ci	if (skb->len < 2)
5398c2ecf20Sopenharmony_ci		return -EINVAL;
5408c2ecf20Sopenharmony_ci	if (*vol > 7)
5418c2ecf20Sopenharmony_ci		return -EINVAL;
5428c2ecf20Sopenharmony_ci	val = *vol & 7;
5438c2ecf20Sopenharmony_ci	val = 7 - val;
5448c2ecf20Sopenharmony_ci	if (mic) {
5458c2ecf20Sopenharmony_ci		val <<= 3;
5468c2ecf20Sopenharmony_ci		card->xaddr &= 0xc7;
5478c2ecf20Sopenharmony_ci	} else {
5488c2ecf20Sopenharmony_ci		card->xaddr &= 0xf8;
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci	card->xaddr |= val;
5518c2ecf20Sopenharmony_ci	WriteW6692(card, W_XADDR, card->xaddr);
5528c2ecf20Sopenharmony_ci	return 0;
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic int
5568c2ecf20Sopenharmony_cienable_pots(struct w6692_ch *wch)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	struct w6692_hw *card = wch->bch.hw;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	if ((!(card->fmask & pots)) ||
5618c2ecf20Sopenharmony_ci	    !test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
5628c2ecf20Sopenharmony_ci		return -ENODEV;
5638c2ecf20Sopenharmony_ci	wch->b_mode |= W_B_MODE_EPCM | W_B_MODE_BSW0;
5648c2ecf20Sopenharmony_ci	WriteW6692B(wch, W_B_MODE, wch->b_mode);
5658c2ecf20Sopenharmony_ci	WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_XRST);
5668c2ecf20Sopenharmony_ci	card->pctl |= ((wch->bch.nr & 2) ? W_PCTL_PCX : 0);
5678c2ecf20Sopenharmony_ci	WriteW6692(card, W_PCTL, card->pctl);
5688c2ecf20Sopenharmony_ci	return 0;
5698c2ecf20Sopenharmony_ci}
5708c2ecf20Sopenharmony_ci#endif
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic int
5738c2ecf20Sopenharmony_cidisable_pots(struct w6692_ch *wch)
5748c2ecf20Sopenharmony_ci{
5758c2ecf20Sopenharmony_ci	struct w6692_hw *card = wch->bch.hw;
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	if (!(card->fmask & pots))
5788c2ecf20Sopenharmony_ci		return -ENODEV;
5798c2ecf20Sopenharmony_ci	wch->b_mode &= ~(W_B_MODE_EPCM | W_B_MODE_BSW0);
5808c2ecf20Sopenharmony_ci	WriteW6692B(wch, W_B_MODE, wch->b_mode);
5818c2ecf20Sopenharmony_ci	WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_RACT |
5828c2ecf20Sopenharmony_ci		    W_B_CMDR_XRST);
5838c2ecf20Sopenharmony_ci	return 0;
5848c2ecf20Sopenharmony_ci}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_cistatic int
5878c2ecf20Sopenharmony_ciw6692_mode(struct w6692_ch *wch, u32 pr)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	struct w6692_hw	*card;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	card = wch->bch.hw;
5928c2ecf20Sopenharmony_ci	pr_debug("%s: B%d protocol %x-->%x\n", card->name,
5938c2ecf20Sopenharmony_ci		 wch->bch.nr, wch->bch.state, pr);
5948c2ecf20Sopenharmony_ci	switch (pr) {
5958c2ecf20Sopenharmony_ci	case ISDN_P_NONE:
5968c2ecf20Sopenharmony_ci		if ((card->fmask & pots) && (wch->b_mode & W_B_MODE_EPCM))
5978c2ecf20Sopenharmony_ci			disable_pots(wch);
5988c2ecf20Sopenharmony_ci		wch->b_mode = 0;
5998c2ecf20Sopenharmony_ci		mISDN_clear_bchannel(&wch->bch);
6008c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_MODE, wch->b_mode);
6018c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_XRST);
6028c2ecf20Sopenharmony_ci		test_and_clear_bit(FLG_HDLC, &wch->bch.Flags);
6038c2ecf20Sopenharmony_ci		test_and_clear_bit(FLG_TRANSPARENT, &wch->bch.Flags);
6048c2ecf20Sopenharmony_ci		break;
6058c2ecf20Sopenharmony_ci	case ISDN_P_B_RAW:
6068c2ecf20Sopenharmony_ci		wch->b_mode = W_B_MODE_MMS;
6078c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_MODE, wch->b_mode);
6088c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_EXIM, 0);
6098c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_RACT |
6108c2ecf20Sopenharmony_ci			    W_B_CMDR_XRST);
6118c2ecf20Sopenharmony_ci		test_and_set_bit(FLG_TRANSPARENT, &wch->bch.Flags);
6128c2ecf20Sopenharmony_ci		break;
6138c2ecf20Sopenharmony_ci	case ISDN_P_B_HDLC:
6148c2ecf20Sopenharmony_ci		wch->b_mode = W_B_MODE_ITF;
6158c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_MODE, wch->b_mode);
6168c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_ADM1, 0xff);
6178c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_ADM2, 0xff);
6188c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_EXIM, 0);
6198c2ecf20Sopenharmony_ci		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_RACT |
6208c2ecf20Sopenharmony_ci			    W_B_CMDR_XRST);
6218c2ecf20Sopenharmony_ci		test_and_set_bit(FLG_HDLC, &wch->bch.Flags);
6228c2ecf20Sopenharmony_ci		break;
6238c2ecf20Sopenharmony_ci	default:
6248c2ecf20Sopenharmony_ci		pr_info("%s: protocol %x not known\n", card->name, pr);
6258c2ecf20Sopenharmony_ci		return -ENOPROTOOPT;
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci	wch->bch.state = pr;
6288c2ecf20Sopenharmony_ci	return 0;
6298c2ecf20Sopenharmony_ci}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_cistatic void
6328c2ecf20Sopenharmony_cisend_next(struct w6692_ch *wch)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	if (wch->bch.tx_skb && wch->bch.tx_idx < wch->bch.tx_skb->len) {
6358c2ecf20Sopenharmony_ci		W6692_fill_Bfifo(wch);
6368c2ecf20Sopenharmony_ci	} else {
6378c2ecf20Sopenharmony_ci		dev_kfree_skb(wch->bch.tx_skb);
6388c2ecf20Sopenharmony_ci		if (get_next_bframe(&wch->bch)) {
6398c2ecf20Sopenharmony_ci			W6692_fill_Bfifo(wch);
6408c2ecf20Sopenharmony_ci			test_and_clear_bit(FLG_TX_EMPTY, &wch->bch.Flags);
6418c2ecf20Sopenharmony_ci		} else if (test_bit(FLG_TX_EMPTY, &wch->bch.Flags)) {
6428c2ecf20Sopenharmony_ci			W6692_fill_Bfifo(wch);
6438c2ecf20Sopenharmony_ci		}
6448c2ecf20Sopenharmony_ci	}
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic void
6488c2ecf20Sopenharmony_ciW6692B_interrupt(struct w6692_hw *card, int ch)
6498c2ecf20Sopenharmony_ci{
6508c2ecf20Sopenharmony_ci	struct w6692_ch	*wch = &card->bc[ch];
6518c2ecf20Sopenharmony_ci	int		count;
6528c2ecf20Sopenharmony_ci	u8		stat, star = 0;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	stat = ReadW6692B(wch, W_B_EXIR);
6558c2ecf20Sopenharmony_ci	pr_debug("%s: B%d EXIR %02x\n", card->name, wch->bch.nr, stat);
6568c2ecf20Sopenharmony_ci	if (stat & W_B_EXI_RME) {
6578c2ecf20Sopenharmony_ci		star = ReadW6692B(wch, W_B_STAR);
6588c2ecf20Sopenharmony_ci		if (star & (W_B_STAR_RDOV | W_B_STAR_CRCE | W_B_STAR_RMB)) {
6598c2ecf20Sopenharmony_ci			if ((star & W_B_STAR_RDOV) &&
6608c2ecf20Sopenharmony_ci			    test_bit(FLG_ACTIVE, &wch->bch.Flags)) {
6618c2ecf20Sopenharmony_ci				pr_debug("%s: B%d RDOV proto=%x\n", card->name,
6628c2ecf20Sopenharmony_ci					 wch->bch.nr, wch->bch.state);
6638c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
6648c2ecf20Sopenharmony_ci				wch->bch.err_rdo++;
6658c2ecf20Sopenharmony_ci#endif
6668c2ecf20Sopenharmony_ci			}
6678c2ecf20Sopenharmony_ci			if (test_bit(FLG_HDLC, &wch->bch.Flags)) {
6688c2ecf20Sopenharmony_ci				if (star & W_B_STAR_CRCE) {
6698c2ecf20Sopenharmony_ci					pr_debug("%s: B%d CRC error\n",
6708c2ecf20Sopenharmony_ci						 card->name, wch->bch.nr);
6718c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
6728c2ecf20Sopenharmony_ci					wch->bch.err_crc++;
6738c2ecf20Sopenharmony_ci#endif
6748c2ecf20Sopenharmony_ci				}
6758c2ecf20Sopenharmony_ci				if (star & W_B_STAR_RMB) {
6768c2ecf20Sopenharmony_ci					pr_debug("%s: B%d message abort\n",
6778c2ecf20Sopenharmony_ci						 card->name, wch->bch.nr);
6788c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
6798c2ecf20Sopenharmony_ci					wch->bch.err_inv++;
6808c2ecf20Sopenharmony_ci#endif
6818c2ecf20Sopenharmony_ci				}
6828c2ecf20Sopenharmony_ci			}
6838c2ecf20Sopenharmony_ci			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK |
6848c2ecf20Sopenharmony_ci				    W_B_CMDR_RRST | W_B_CMDR_RACT);
6858c2ecf20Sopenharmony_ci			if (wch->bch.rx_skb)
6868c2ecf20Sopenharmony_ci				skb_trim(wch->bch.rx_skb, 0);
6878c2ecf20Sopenharmony_ci		} else {
6888c2ecf20Sopenharmony_ci			count = ReadW6692B(wch, W_B_RBCL) &
6898c2ecf20Sopenharmony_ci				(W_B_FIFO_THRESH - 1);
6908c2ecf20Sopenharmony_ci			if (count == 0)
6918c2ecf20Sopenharmony_ci				count = W_B_FIFO_THRESH;
6928c2ecf20Sopenharmony_ci			W6692_empty_Bfifo(wch, count);
6938c2ecf20Sopenharmony_ci			recv_Bchannel(&wch->bch, 0, false);
6948c2ecf20Sopenharmony_ci		}
6958c2ecf20Sopenharmony_ci	}
6968c2ecf20Sopenharmony_ci	if (stat & W_B_EXI_RMR) {
6978c2ecf20Sopenharmony_ci		if (!(stat & W_B_EXI_RME))
6988c2ecf20Sopenharmony_ci			star = ReadW6692B(wch, W_B_STAR);
6998c2ecf20Sopenharmony_ci		if (star & W_B_STAR_RDOV) {
7008c2ecf20Sopenharmony_ci			pr_debug("%s: B%d RDOV proto=%x\n", card->name,
7018c2ecf20Sopenharmony_ci				 wch->bch.nr, wch->bch.state);
7028c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
7038c2ecf20Sopenharmony_ci			wch->bch.err_rdo++;
7048c2ecf20Sopenharmony_ci#endif
7058c2ecf20Sopenharmony_ci			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK |
7068c2ecf20Sopenharmony_ci				    W_B_CMDR_RRST | W_B_CMDR_RACT);
7078c2ecf20Sopenharmony_ci		} else {
7088c2ecf20Sopenharmony_ci			W6692_empty_Bfifo(wch, W_B_FIFO_THRESH);
7098c2ecf20Sopenharmony_ci			if (test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
7108c2ecf20Sopenharmony_ci				recv_Bchannel(&wch->bch, 0, false);
7118c2ecf20Sopenharmony_ci		}
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci	if (stat & W_B_EXI_RDOV) {
7148c2ecf20Sopenharmony_ci		/* only if it is not handled yet */
7158c2ecf20Sopenharmony_ci		if (!(star & W_B_STAR_RDOV)) {
7168c2ecf20Sopenharmony_ci			pr_debug("%s: B%d RDOV IRQ proto=%x\n", card->name,
7178c2ecf20Sopenharmony_ci				 wch->bch.nr, wch->bch.state);
7188c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
7198c2ecf20Sopenharmony_ci			wch->bch.err_rdo++;
7208c2ecf20Sopenharmony_ci#endif
7218c2ecf20Sopenharmony_ci			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK |
7228c2ecf20Sopenharmony_ci				    W_B_CMDR_RRST | W_B_CMDR_RACT);
7238c2ecf20Sopenharmony_ci		}
7248c2ecf20Sopenharmony_ci	}
7258c2ecf20Sopenharmony_ci	if (stat & W_B_EXI_XFR) {
7268c2ecf20Sopenharmony_ci		if (!(stat & (W_B_EXI_RME | W_B_EXI_RMR))) {
7278c2ecf20Sopenharmony_ci			star = ReadW6692B(wch, W_B_STAR);
7288c2ecf20Sopenharmony_ci			pr_debug("%s: B%d star %02x\n", card->name,
7298c2ecf20Sopenharmony_ci				 wch->bch.nr, star);
7308c2ecf20Sopenharmony_ci		}
7318c2ecf20Sopenharmony_ci		if (star & W_B_STAR_XDOW) {
7328c2ecf20Sopenharmony_ci			pr_warn("%s: B%d XDOW proto=%x\n", card->name,
7338c2ecf20Sopenharmony_ci				wch->bch.nr, wch->bch.state);
7348c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
7358c2ecf20Sopenharmony_ci			wch->bch.err_xdu++;
7368c2ecf20Sopenharmony_ci#endif
7378c2ecf20Sopenharmony_ci			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_XRST |
7388c2ecf20Sopenharmony_ci				    W_B_CMDR_RACT);
7398c2ecf20Sopenharmony_ci			/* resend */
7408c2ecf20Sopenharmony_ci			if (wch->bch.tx_skb) {
7418c2ecf20Sopenharmony_ci				if (!test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
7428c2ecf20Sopenharmony_ci					wch->bch.tx_idx = 0;
7438c2ecf20Sopenharmony_ci			}
7448c2ecf20Sopenharmony_ci		}
7458c2ecf20Sopenharmony_ci		send_next(wch);
7468c2ecf20Sopenharmony_ci		if (star & W_B_STAR_XDOW)
7478c2ecf20Sopenharmony_ci			return; /* handle XDOW only once */
7488c2ecf20Sopenharmony_ci	}
7498c2ecf20Sopenharmony_ci	if (stat & W_B_EXI_XDUN) {
7508c2ecf20Sopenharmony_ci		pr_warn("%s: B%d XDUN proto=%x\n", card->name,
7518c2ecf20Sopenharmony_ci			wch->bch.nr, wch->bch.state);
7528c2ecf20Sopenharmony_ci#ifdef ERROR_STATISTIC
7538c2ecf20Sopenharmony_ci		wch->bch.err_xdu++;
7548c2ecf20Sopenharmony_ci#endif
7558c2ecf20Sopenharmony_ci		/* resend - no XRST needed */
7568c2ecf20Sopenharmony_ci		if (wch->bch.tx_skb) {
7578c2ecf20Sopenharmony_ci			if (!test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
7588c2ecf20Sopenharmony_ci				wch->bch.tx_idx = 0;
7598c2ecf20Sopenharmony_ci		} else if (test_bit(FLG_FILLEMPTY, &wch->bch.Flags)) {
7608c2ecf20Sopenharmony_ci			test_and_set_bit(FLG_TX_EMPTY, &wch->bch.Flags);
7618c2ecf20Sopenharmony_ci		}
7628c2ecf20Sopenharmony_ci		send_next(wch);
7638c2ecf20Sopenharmony_ci	}
7648c2ecf20Sopenharmony_ci}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_cistatic irqreturn_t
7678c2ecf20Sopenharmony_ciw6692_irq(int intno, void *dev_id)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	struct w6692_hw	*card = dev_id;
7708c2ecf20Sopenharmony_ci	u8		ista;
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	spin_lock(&card->lock);
7738c2ecf20Sopenharmony_ci	ista = ReadW6692(card, W_ISTA);
7748c2ecf20Sopenharmony_ci	if ((ista | card->imask) == card->imask) {
7758c2ecf20Sopenharmony_ci		/* possible a shared  IRQ reqest */
7768c2ecf20Sopenharmony_ci		spin_unlock(&card->lock);
7778c2ecf20Sopenharmony_ci		return IRQ_NONE;
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci	card->irqcnt++;
7808c2ecf20Sopenharmony_ci	pr_debug("%s: ista %02x\n", card->name, ista);
7818c2ecf20Sopenharmony_ci	ista &= ~card->imask;
7828c2ecf20Sopenharmony_ci	if (ista & W_INT_B1_EXI)
7838c2ecf20Sopenharmony_ci		W6692B_interrupt(card, 0);
7848c2ecf20Sopenharmony_ci	if (ista & W_INT_B2_EXI)
7858c2ecf20Sopenharmony_ci		W6692B_interrupt(card, 1);
7868c2ecf20Sopenharmony_ci	if (ista & W_INT_D_RME)
7878c2ecf20Sopenharmony_ci		handle_rxD(card);
7888c2ecf20Sopenharmony_ci	if (ista & W_INT_D_RMR)
7898c2ecf20Sopenharmony_ci		W6692_empty_Dfifo(card, W_D_FIFO_THRESH);
7908c2ecf20Sopenharmony_ci	if (ista & W_INT_D_XFR)
7918c2ecf20Sopenharmony_ci		handle_txD(card);
7928c2ecf20Sopenharmony_ci	if (ista & W_INT_D_EXI)
7938c2ecf20Sopenharmony_ci		handle_statusD(card);
7948c2ecf20Sopenharmony_ci	if (ista & (W_INT_XINT0 | W_INT_XINT1)) /* XINT0/1 - never */
7958c2ecf20Sopenharmony_ci		pr_debug("%s: W6692 spurious XINT!\n", card->name);
7968c2ecf20Sopenharmony_ci/* End IRQ Handler */
7978c2ecf20Sopenharmony_ci	spin_unlock(&card->lock);
7988c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cistatic void
8028c2ecf20Sopenharmony_cidbusy_timer_handler(struct timer_list *t)
8038c2ecf20Sopenharmony_ci{
8048c2ecf20Sopenharmony_ci	struct dchannel *dch = from_timer(dch, t, timer);
8058c2ecf20Sopenharmony_ci	struct w6692_hw	*card = dch->hw;
8068c2ecf20Sopenharmony_ci	int		rbch, star;
8078c2ecf20Sopenharmony_ci	u_long		flags;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	if (test_bit(FLG_BUSY_TIMER, &dch->Flags)) {
8108c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
8118c2ecf20Sopenharmony_ci		rbch = ReadW6692(card, W_D_RBCH);
8128c2ecf20Sopenharmony_ci		star = ReadW6692(card, W_D_STAR);
8138c2ecf20Sopenharmony_ci		pr_debug("%s: D-Channel Busy RBCH %02x STAR %02x\n",
8148c2ecf20Sopenharmony_ci			 card->name, rbch, star);
8158c2ecf20Sopenharmony_ci		if (star & W_D_STAR_XBZ)	/* D-Channel Busy */
8168c2ecf20Sopenharmony_ci			test_and_set_bit(FLG_L1_BUSY, &dch->Flags);
8178c2ecf20Sopenharmony_ci		else {
8188c2ecf20Sopenharmony_ci			/* discard frame; reset transceiver */
8198c2ecf20Sopenharmony_ci			test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags);
8208c2ecf20Sopenharmony_ci			if (dch->tx_idx)
8218c2ecf20Sopenharmony_ci				dch->tx_idx = 0;
8228c2ecf20Sopenharmony_ci			else
8238c2ecf20Sopenharmony_ci				pr_info("%s: W6692 D-Channel Busy no tx_idx\n",
8248c2ecf20Sopenharmony_ci					card->name);
8258c2ecf20Sopenharmony_ci			/* Transmitter reset */
8268c2ecf20Sopenharmony_ci			WriteW6692(card, W_D_CMDR, W_D_CMDR_XRST);
8278c2ecf20Sopenharmony_ci		}
8288c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
8298c2ecf20Sopenharmony_ci	}
8308c2ecf20Sopenharmony_ci}
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_cistatic void initW6692(struct w6692_hw *card)
8338c2ecf20Sopenharmony_ci{
8348c2ecf20Sopenharmony_ci	u8	val;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	timer_setup(&card->dch.timer, dbusy_timer_handler, 0);
8378c2ecf20Sopenharmony_ci	w6692_mode(&card->bc[0], ISDN_P_NONE);
8388c2ecf20Sopenharmony_ci	w6692_mode(&card->bc[1], ISDN_P_NONE);
8398c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_CTL, 0x00);
8408c2ecf20Sopenharmony_ci	disable_hwirq(card);
8418c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_SAM, 0xff);
8428c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_TAM, 0xff);
8438c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_MODE, W_D_MODE_RACT);
8448c2ecf20Sopenharmony_ci	card->state = W_L1CMD_RST;
8458c2ecf20Sopenharmony_ci	ph_command(card, W_L1CMD_RST);
8468c2ecf20Sopenharmony_ci	ph_command(card, W_L1CMD_ECK);
8478c2ecf20Sopenharmony_ci	/* enable all IRQ but extern */
8488c2ecf20Sopenharmony_ci	card->imask = 0x18;
8498c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_EXIM, 0x00);
8508c2ecf20Sopenharmony_ci	WriteW6692B(&card->bc[0], W_B_EXIM, 0);
8518c2ecf20Sopenharmony_ci	WriteW6692B(&card->bc[1], W_B_EXIM, 0);
8528c2ecf20Sopenharmony_ci	/* Reset D-chan receiver and transmitter */
8538c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_CMDR, W_D_CMDR_RRST | W_D_CMDR_XRST);
8548c2ecf20Sopenharmony_ci	/* Reset B-chan receiver and transmitter */
8558c2ecf20Sopenharmony_ci	WriteW6692B(&card->bc[0], W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_XRST);
8568c2ecf20Sopenharmony_ci	WriteW6692B(&card->bc[1], W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_XRST);
8578c2ecf20Sopenharmony_ci	/* enable peripheral */
8588c2ecf20Sopenharmony_ci	if (card->subtype == W6692_USR) {
8598c2ecf20Sopenharmony_ci		/* seems that USR implemented some power control features
8608c2ecf20Sopenharmony_ci		 * Pin 79 is connected to the oscilator circuit so we
8618c2ecf20Sopenharmony_ci		 * have to handle it here
8628c2ecf20Sopenharmony_ci		 */
8638c2ecf20Sopenharmony_ci		card->pctl = 0x80;
8648c2ecf20Sopenharmony_ci		card->xdata = 0;
8658c2ecf20Sopenharmony_ci		WriteW6692(card, W_PCTL, card->pctl);
8668c2ecf20Sopenharmony_ci		WriteW6692(card, W_XDATA, card->xdata);
8678c2ecf20Sopenharmony_ci	} else {
8688c2ecf20Sopenharmony_ci		card->pctl = W_PCTL_OE5 | W_PCTL_OE4 | W_PCTL_OE2 |
8698c2ecf20Sopenharmony_ci			W_PCTL_OE1 | W_PCTL_OE0;
8708c2ecf20Sopenharmony_ci		card->xaddr = 0x00;/* all sw off */
8718c2ecf20Sopenharmony_ci		if (card->fmask & pots)
8728c2ecf20Sopenharmony_ci			card->xdata |= 0x06;	/*  POWER UP/ LED OFF / ALAW */
8738c2ecf20Sopenharmony_ci		if (card->fmask & led)
8748c2ecf20Sopenharmony_ci			card->xdata |= 0x04;	/* LED OFF */
8758c2ecf20Sopenharmony_ci		if ((card->fmask & pots) || (card->fmask & led)) {
8768c2ecf20Sopenharmony_ci			WriteW6692(card, W_PCTL, card->pctl);
8778c2ecf20Sopenharmony_ci			WriteW6692(card, W_XADDR, card->xaddr);
8788c2ecf20Sopenharmony_ci			WriteW6692(card, W_XDATA, card->xdata);
8798c2ecf20Sopenharmony_ci			val = ReadW6692(card, W_XADDR);
8808c2ecf20Sopenharmony_ci			if (debug & DEBUG_HW)
8818c2ecf20Sopenharmony_ci				pr_notice("%s: W_XADDR=%02x\n",
8828c2ecf20Sopenharmony_ci					  card->name, val);
8838c2ecf20Sopenharmony_ci		}
8848c2ecf20Sopenharmony_ci	}
8858c2ecf20Sopenharmony_ci}
8868c2ecf20Sopenharmony_ci
8878c2ecf20Sopenharmony_cistatic void
8888c2ecf20Sopenharmony_cireset_w6692(struct w6692_hw *card)
8898c2ecf20Sopenharmony_ci{
8908c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_CTL, W_D_CTL_SRST);
8918c2ecf20Sopenharmony_ci	mdelay(10);
8928c2ecf20Sopenharmony_ci	WriteW6692(card, W_D_CTL, 0);
8938c2ecf20Sopenharmony_ci}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_cistatic int
8968c2ecf20Sopenharmony_ciinit_card(struct w6692_hw *card)
8978c2ecf20Sopenharmony_ci{
8988c2ecf20Sopenharmony_ci	int	cnt = 3;
8998c2ecf20Sopenharmony_ci	u_long	flags;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&card->lock, flags);
9028c2ecf20Sopenharmony_ci	disable_hwirq(card);
9038c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&card->lock, flags);
9048c2ecf20Sopenharmony_ci	if (request_irq(card->irq, w6692_irq, IRQF_SHARED, card->name, card)) {
9058c2ecf20Sopenharmony_ci		pr_info("%s: couldn't get interrupt %d\n", card->name,
9068c2ecf20Sopenharmony_ci			card->irq);
9078c2ecf20Sopenharmony_ci		return -EIO;
9088c2ecf20Sopenharmony_ci	}
9098c2ecf20Sopenharmony_ci	while (cnt--) {
9108c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
9118c2ecf20Sopenharmony_ci		initW6692(card);
9128c2ecf20Sopenharmony_ci		enable_hwirq(card);
9138c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
9148c2ecf20Sopenharmony_ci		/* Timeout 10ms */
9158c2ecf20Sopenharmony_ci		msleep_interruptible(10);
9168c2ecf20Sopenharmony_ci		if (debug & DEBUG_HW)
9178c2ecf20Sopenharmony_ci			pr_notice("%s: IRQ %d count %d\n", card->name,
9188c2ecf20Sopenharmony_ci				  card->irq, card->irqcnt);
9198c2ecf20Sopenharmony_ci		if (!card->irqcnt) {
9208c2ecf20Sopenharmony_ci			pr_info("%s: IRQ(%d) getting no IRQs during init %d\n",
9218c2ecf20Sopenharmony_ci				card->name, card->irq, 3 - cnt);
9228c2ecf20Sopenharmony_ci			reset_w6692(card);
9238c2ecf20Sopenharmony_ci		} else
9248c2ecf20Sopenharmony_ci			return 0;
9258c2ecf20Sopenharmony_ci	}
9268c2ecf20Sopenharmony_ci	free_irq(card->irq, card);
9278c2ecf20Sopenharmony_ci	return -EIO;
9288c2ecf20Sopenharmony_ci}
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_cistatic int
9318c2ecf20Sopenharmony_ciw6692_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	struct bchannel *bch = container_of(ch, struct bchannel, ch);
9348c2ecf20Sopenharmony_ci	struct w6692_ch	*bc = container_of(bch, struct w6692_ch, bch);
9358c2ecf20Sopenharmony_ci	struct w6692_hw *card = bch->hw;
9368c2ecf20Sopenharmony_ci	int ret = -EINVAL;
9378c2ecf20Sopenharmony_ci	struct mISDNhead *hh = mISDN_HEAD_P(skb);
9388c2ecf20Sopenharmony_ci	unsigned long flags;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	switch (hh->prim) {
9418c2ecf20Sopenharmony_ci	case PH_DATA_REQ:
9428c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
9438c2ecf20Sopenharmony_ci		ret = bchannel_senddata(bch, skb);
9448c2ecf20Sopenharmony_ci		if (ret > 0) { /* direct TX */
9458c2ecf20Sopenharmony_ci			ret = 0;
9468c2ecf20Sopenharmony_ci			W6692_fill_Bfifo(bc);
9478c2ecf20Sopenharmony_ci		}
9488c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
9498c2ecf20Sopenharmony_ci		return ret;
9508c2ecf20Sopenharmony_ci	case PH_ACTIVATE_REQ:
9518c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
9528c2ecf20Sopenharmony_ci		if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
9538c2ecf20Sopenharmony_ci			ret = w6692_mode(bc, ch->protocol);
9548c2ecf20Sopenharmony_ci		else
9558c2ecf20Sopenharmony_ci			ret = 0;
9568c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
9578c2ecf20Sopenharmony_ci		if (!ret)
9588c2ecf20Sopenharmony_ci			_queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
9598c2ecf20Sopenharmony_ci				    NULL, GFP_KERNEL);
9608c2ecf20Sopenharmony_ci		break;
9618c2ecf20Sopenharmony_ci	case PH_DEACTIVATE_REQ:
9628c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
9638c2ecf20Sopenharmony_ci		mISDN_clear_bchannel(bch);
9648c2ecf20Sopenharmony_ci		w6692_mode(bc, ISDN_P_NONE);
9658c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
9668c2ecf20Sopenharmony_ci		_queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
9678c2ecf20Sopenharmony_ci			    NULL, GFP_KERNEL);
9688c2ecf20Sopenharmony_ci		ret = 0;
9698c2ecf20Sopenharmony_ci		break;
9708c2ecf20Sopenharmony_ci	default:
9718c2ecf20Sopenharmony_ci		pr_info("%s: %s unknown prim(%x,%x)\n",
9728c2ecf20Sopenharmony_ci			card->name, __func__, hh->prim, hh->id);
9738c2ecf20Sopenharmony_ci		ret = -EINVAL;
9748c2ecf20Sopenharmony_ci	}
9758c2ecf20Sopenharmony_ci	if (!ret)
9768c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
9778c2ecf20Sopenharmony_ci	return ret;
9788c2ecf20Sopenharmony_ci}
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_cistatic int
9818c2ecf20Sopenharmony_cichannel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	return mISDN_ctrl_bchannel(bch, cq);
9848c2ecf20Sopenharmony_ci}
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_cistatic int
9878c2ecf20Sopenharmony_ciopen_bchannel(struct w6692_hw *card, struct channel_req *rq)
9888c2ecf20Sopenharmony_ci{
9898c2ecf20Sopenharmony_ci	struct bchannel *bch;
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	if (rq->adr.channel == 0 || rq->adr.channel > 2)
9928c2ecf20Sopenharmony_ci		return -EINVAL;
9938c2ecf20Sopenharmony_ci	if (rq->protocol == ISDN_P_NONE)
9948c2ecf20Sopenharmony_ci		return -EINVAL;
9958c2ecf20Sopenharmony_ci	bch = &card->bc[rq->adr.channel - 1].bch;
9968c2ecf20Sopenharmony_ci	if (test_and_set_bit(FLG_OPEN, &bch->Flags))
9978c2ecf20Sopenharmony_ci		return -EBUSY; /* b-channel can be only open once */
9988c2ecf20Sopenharmony_ci	bch->ch.protocol = rq->protocol;
9998c2ecf20Sopenharmony_ci	rq->ch = &bch->ch;
10008c2ecf20Sopenharmony_ci	return 0;
10018c2ecf20Sopenharmony_ci}
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_cistatic int
10048c2ecf20Sopenharmony_cichannel_ctrl(struct w6692_hw *card, struct mISDN_ctrl_req *cq)
10058c2ecf20Sopenharmony_ci{
10068c2ecf20Sopenharmony_ci	int	ret = 0;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	switch (cq->op) {
10098c2ecf20Sopenharmony_ci	case MISDN_CTRL_GETOP:
10108c2ecf20Sopenharmony_ci		cq->op = MISDN_CTRL_L1_TIMER3;
10118c2ecf20Sopenharmony_ci		break;
10128c2ecf20Sopenharmony_ci	case MISDN_CTRL_L1_TIMER3:
10138c2ecf20Sopenharmony_ci		ret = l1_event(card->dch.l1, HW_TIMER3_VALUE | (cq->p1 & 0xff));
10148c2ecf20Sopenharmony_ci		break;
10158c2ecf20Sopenharmony_ci	default:
10168c2ecf20Sopenharmony_ci		pr_info("%s: unknown CTRL OP %x\n", card->name, cq->op);
10178c2ecf20Sopenharmony_ci		ret = -EINVAL;
10188c2ecf20Sopenharmony_ci		break;
10198c2ecf20Sopenharmony_ci	}
10208c2ecf20Sopenharmony_ci	return ret;
10218c2ecf20Sopenharmony_ci}
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_cistatic int
10248c2ecf20Sopenharmony_ciw6692_bctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
10258c2ecf20Sopenharmony_ci{
10268c2ecf20Sopenharmony_ci	struct bchannel *bch = container_of(ch, struct bchannel, ch);
10278c2ecf20Sopenharmony_ci	struct w6692_ch *bc = container_of(bch, struct w6692_ch, bch);
10288c2ecf20Sopenharmony_ci	struct w6692_hw *card = bch->hw;
10298c2ecf20Sopenharmony_ci	int ret = -EINVAL;
10308c2ecf20Sopenharmony_ci	u_long flags;
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	pr_debug("%s: %s cmd:%x %p\n", card->name, __func__, cmd, arg);
10338c2ecf20Sopenharmony_ci	switch (cmd) {
10348c2ecf20Sopenharmony_ci	case CLOSE_CHANNEL:
10358c2ecf20Sopenharmony_ci		test_and_clear_bit(FLG_OPEN, &bch->Flags);
10368c2ecf20Sopenharmony_ci		cancel_work_sync(&bch->workq);
10378c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
10388c2ecf20Sopenharmony_ci		mISDN_clear_bchannel(bch);
10398c2ecf20Sopenharmony_ci		w6692_mode(bc, ISDN_P_NONE);
10408c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
10418c2ecf20Sopenharmony_ci		ch->protocol = ISDN_P_NONE;
10428c2ecf20Sopenharmony_ci		ch->peer = NULL;
10438c2ecf20Sopenharmony_ci		module_put(THIS_MODULE);
10448c2ecf20Sopenharmony_ci		ret = 0;
10458c2ecf20Sopenharmony_ci		break;
10468c2ecf20Sopenharmony_ci	case CONTROL_CHANNEL:
10478c2ecf20Sopenharmony_ci		ret = channel_bctrl(bch, arg);
10488c2ecf20Sopenharmony_ci		break;
10498c2ecf20Sopenharmony_ci	default:
10508c2ecf20Sopenharmony_ci		pr_info("%s: %s unknown prim(%x)\n",
10518c2ecf20Sopenharmony_ci			card->name, __func__, cmd);
10528c2ecf20Sopenharmony_ci	}
10538c2ecf20Sopenharmony_ci	return ret;
10548c2ecf20Sopenharmony_ci}
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_cistatic int
10578c2ecf20Sopenharmony_ciw6692_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
10588c2ecf20Sopenharmony_ci{
10598c2ecf20Sopenharmony_ci	struct mISDNdevice	*dev = container_of(ch, struct mISDNdevice, D);
10608c2ecf20Sopenharmony_ci	struct dchannel		*dch = container_of(dev, struct dchannel, dev);
10618c2ecf20Sopenharmony_ci	struct w6692_hw		*card = container_of(dch, struct w6692_hw, dch);
10628c2ecf20Sopenharmony_ci	int			ret = -EINVAL;
10638c2ecf20Sopenharmony_ci	struct mISDNhead	*hh = mISDN_HEAD_P(skb);
10648c2ecf20Sopenharmony_ci	u32			id;
10658c2ecf20Sopenharmony_ci	u_long			flags;
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	switch (hh->prim) {
10688c2ecf20Sopenharmony_ci	case PH_DATA_REQ:
10698c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
10708c2ecf20Sopenharmony_ci		ret = dchannel_senddata(dch, skb);
10718c2ecf20Sopenharmony_ci		if (ret > 0) { /* direct TX */
10728c2ecf20Sopenharmony_ci			id = hh->id; /* skb can be freed */
10738c2ecf20Sopenharmony_ci			W6692_fill_Dfifo(card);
10748c2ecf20Sopenharmony_ci			ret = 0;
10758c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&card->lock, flags);
10768c2ecf20Sopenharmony_ci			queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
10778c2ecf20Sopenharmony_ci		} else
10788c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&card->lock, flags);
10798c2ecf20Sopenharmony_ci		return ret;
10808c2ecf20Sopenharmony_ci	case PH_ACTIVATE_REQ:
10818c2ecf20Sopenharmony_ci		ret = l1_event(dch->l1, hh->prim);
10828c2ecf20Sopenharmony_ci		break;
10838c2ecf20Sopenharmony_ci	case PH_DEACTIVATE_REQ:
10848c2ecf20Sopenharmony_ci		test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
10858c2ecf20Sopenharmony_ci		ret = l1_event(dch->l1, hh->prim);
10868c2ecf20Sopenharmony_ci		break;
10878c2ecf20Sopenharmony_ci	}
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	if (!ret)
10908c2ecf20Sopenharmony_ci		dev_kfree_skb(skb);
10918c2ecf20Sopenharmony_ci	return ret;
10928c2ecf20Sopenharmony_ci}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_cistatic int
10958c2ecf20Sopenharmony_ciw6692_l1callback(struct dchannel *dch, u32 cmd)
10968c2ecf20Sopenharmony_ci{
10978c2ecf20Sopenharmony_ci	struct w6692_hw *card = container_of(dch, struct w6692_hw, dch);
10988c2ecf20Sopenharmony_ci	u_long flags;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	pr_debug("%s: cmd(%x) state(%02x)\n", card->name, cmd, card->state);
11018c2ecf20Sopenharmony_ci	switch (cmd) {
11028c2ecf20Sopenharmony_ci	case INFO3_P8:
11038c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
11048c2ecf20Sopenharmony_ci		ph_command(card, W_L1CMD_AR8);
11058c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
11068c2ecf20Sopenharmony_ci		break;
11078c2ecf20Sopenharmony_ci	case INFO3_P10:
11088c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
11098c2ecf20Sopenharmony_ci		ph_command(card, W_L1CMD_AR10);
11108c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
11118c2ecf20Sopenharmony_ci		break;
11128c2ecf20Sopenharmony_ci	case HW_RESET_REQ:
11138c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
11148c2ecf20Sopenharmony_ci		if (card->state != W_L1IND_DRD)
11158c2ecf20Sopenharmony_ci			ph_command(card, W_L1CMD_RST);
11168c2ecf20Sopenharmony_ci		ph_command(card, W_L1CMD_ECK);
11178c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
11188c2ecf20Sopenharmony_ci		break;
11198c2ecf20Sopenharmony_ci	case HW_DEACT_REQ:
11208c2ecf20Sopenharmony_ci		skb_queue_purge(&dch->squeue);
11218c2ecf20Sopenharmony_ci		if (dch->tx_skb) {
11228c2ecf20Sopenharmony_ci			dev_kfree_skb(dch->tx_skb);
11238c2ecf20Sopenharmony_ci			dch->tx_skb = NULL;
11248c2ecf20Sopenharmony_ci		}
11258c2ecf20Sopenharmony_ci		dch->tx_idx = 0;
11268c2ecf20Sopenharmony_ci		if (dch->rx_skb) {
11278c2ecf20Sopenharmony_ci			dev_kfree_skb(dch->rx_skb);
11288c2ecf20Sopenharmony_ci			dch->rx_skb = NULL;
11298c2ecf20Sopenharmony_ci		}
11308c2ecf20Sopenharmony_ci		test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
11318c2ecf20Sopenharmony_ci		if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
11328c2ecf20Sopenharmony_ci			del_timer(&dch->timer);
11338c2ecf20Sopenharmony_ci		break;
11348c2ecf20Sopenharmony_ci	case HW_POWERUP_REQ:
11358c2ecf20Sopenharmony_ci		spin_lock_irqsave(&card->lock, flags);
11368c2ecf20Sopenharmony_ci		ph_command(card, W_L1CMD_ECK);
11378c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&card->lock, flags);
11388c2ecf20Sopenharmony_ci		break;
11398c2ecf20Sopenharmony_ci	case PH_ACTIVATE_IND:
11408c2ecf20Sopenharmony_ci		test_and_set_bit(FLG_ACTIVE, &dch->Flags);
11418c2ecf20Sopenharmony_ci		_queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
11428c2ecf20Sopenharmony_ci			    GFP_ATOMIC);
11438c2ecf20Sopenharmony_ci		break;
11448c2ecf20Sopenharmony_ci	case PH_DEACTIVATE_IND:
11458c2ecf20Sopenharmony_ci		test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
11468c2ecf20Sopenharmony_ci		_queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
11478c2ecf20Sopenharmony_ci			    GFP_ATOMIC);
11488c2ecf20Sopenharmony_ci		break;
11498c2ecf20Sopenharmony_ci	default:
11508c2ecf20Sopenharmony_ci		pr_debug("%s: %s unknown command %x\n", card->name,
11518c2ecf20Sopenharmony_ci			 __func__, cmd);
11528c2ecf20Sopenharmony_ci		return -1;
11538c2ecf20Sopenharmony_ci	}
11548c2ecf20Sopenharmony_ci	return 0;
11558c2ecf20Sopenharmony_ci}
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_cistatic int
11588c2ecf20Sopenharmony_ciopen_dchannel(struct w6692_hw *card, struct channel_req *rq, void *caller)
11598c2ecf20Sopenharmony_ci{
11608c2ecf20Sopenharmony_ci	pr_debug("%s: %s dev(%d) open from %p\n", card->name, __func__,
11618c2ecf20Sopenharmony_ci		 card->dch.dev.id, caller);
11628c2ecf20Sopenharmony_ci	if (rq->protocol != ISDN_P_TE_S0)
11638c2ecf20Sopenharmony_ci		return -EINVAL;
11648c2ecf20Sopenharmony_ci	if (rq->adr.channel == 1)
11658c2ecf20Sopenharmony_ci		/* E-Channel not supported */
11668c2ecf20Sopenharmony_ci		return -EINVAL;
11678c2ecf20Sopenharmony_ci	rq->ch = &card->dch.dev.D;
11688c2ecf20Sopenharmony_ci	rq->ch->protocol = rq->protocol;
11698c2ecf20Sopenharmony_ci	if (card->dch.state == 7)
11708c2ecf20Sopenharmony_ci		_queue_data(rq->ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
11718c2ecf20Sopenharmony_ci			    0, NULL, GFP_KERNEL);
11728c2ecf20Sopenharmony_ci	return 0;
11738c2ecf20Sopenharmony_ci}
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_cistatic int
11768c2ecf20Sopenharmony_ciw6692_dctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
11778c2ecf20Sopenharmony_ci{
11788c2ecf20Sopenharmony_ci	struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
11798c2ecf20Sopenharmony_ci	struct dchannel *dch = container_of(dev, struct dchannel, dev);
11808c2ecf20Sopenharmony_ci	struct w6692_hw *card = container_of(dch, struct w6692_hw, dch);
11818c2ecf20Sopenharmony_ci	struct channel_req *rq;
11828c2ecf20Sopenharmony_ci	int err = 0;
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	pr_debug("%s: DCTRL: %x %p\n", card->name, cmd, arg);
11858c2ecf20Sopenharmony_ci	switch (cmd) {
11868c2ecf20Sopenharmony_ci	case OPEN_CHANNEL:
11878c2ecf20Sopenharmony_ci		rq = arg;
11888c2ecf20Sopenharmony_ci		if (rq->protocol == ISDN_P_TE_S0)
11898c2ecf20Sopenharmony_ci			err = open_dchannel(card, rq, __builtin_return_address(0));
11908c2ecf20Sopenharmony_ci		else
11918c2ecf20Sopenharmony_ci			err = open_bchannel(card, rq);
11928c2ecf20Sopenharmony_ci		if (err)
11938c2ecf20Sopenharmony_ci			break;
11948c2ecf20Sopenharmony_ci		if (!try_module_get(THIS_MODULE))
11958c2ecf20Sopenharmony_ci			pr_info("%s: cannot get module\n", card->name);
11968c2ecf20Sopenharmony_ci		break;
11978c2ecf20Sopenharmony_ci	case CLOSE_CHANNEL:
11988c2ecf20Sopenharmony_ci		pr_debug("%s: dev(%d) close from %p\n", card->name,
11998c2ecf20Sopenharmony_ci			 dch->dev.id, __builtin_return_address(0));
12008c2ecf20Sopenharmony_ci		module_put(THIS_MODULE);
12018c2ecf20Sopenharmony_ci		break;
12028c2ecf20Sopenharmony_ci	case CONTROL_CHANNEL:
12038c2ecf20Sopenharmony_ci		err = channel_ctrl(card, arg);
12048c2ecf20Sopenharmony_ci		break;
12058c2ecf20Sopenharmony_ci	default:
12068c2ecf20Sopenharmony_ci		pr_debug("%s: unknown DCTRL command %x\n", card->name, cmd);
12078c2ecf20Sopenharmony_ci		return -EINVAL;
12088c2ecf20Sopenharmony_ci	}
12098c2ecf20Sopenharmony_ci	return err;
12108c2ecf20Sopenharmony_ci}
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_cistatic int
12138c2ecf20Sopenharmony_cisetup_w6692(struct w6692_hw *card)
12148c2ecf20Sopenharmony_ci{
12158c2ecf20Sopenharmony_ci	u32	val;
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	if (!request_region(card->addr, 256, card->name)) {
12188c2ecf20Sopenharmony_ci		pr_info("%s: config port %x-%x already in use\n", card->name,
12198c2ecf20Sopenharmony_ci			card->addr, card->addr + 255);
12208c2ecf20Sopenharmony_ci		return -EIO;
12218c2ecf20Sopenharmony_ci	}
12228c2ecf20Sopenharmony_ci	W6692Version(card);
12238c2ecf20Sopenharmony_ci	card->bc[0].addr = card->addr;
12248c2ecf20Sopenharmony_ci	card->bc[1].addr = card->addr + 0x40;
12258c2ecf20Sopenharmony_ci	val = ReadW6692(card, W_ISTA);
12268c2ecf20Sopenharmony_ci	if (debug & DEBUG_HW)
12278c2ecf20Sopenharmony_ci		pr_notice("%s ISTA=%02x\n", card->name, val);
12288c2ecf20Sopenharmony_ci	val = ReadW6692(card, W_IMASK);
12298c2ecf20Sopenharmony_ci	if (debug & DEBUG_HW)
12308c2ecf20Sopenharmony_ci		pr_notice("%s IMASK=%02x\n", card->name, val);
12318c2ecf20Sopenharmony_ci	val = ReadW6692(card, W_D_EXIR);
12328c2ecf20Sopenharmony_ci	if (debug & DEBUG_HW)
12338c2ecf20Sopenharmony_ci		pr_notice("%s D_EXIR=%02x\n", card->name, val);
12348c2ecf20Sopenharmony_ci	val = ReadW6692(card, W_D_EXIM);
12358c2ecf20Sopenharmony_ci	if (debug & DEBUG_HW)
12368c2ecf20Sopenharmony_ci		pr_notice("%s D_EXIM=%02x\n", card->name, val);
12378c2ecf20Sopenharmony_ci	val = ReadW6692(card, W_D_RSTA);
12388c2ecf20Sopenharmony_ci	if (debug & DEBUG_HW)
12398c2ecf20Sopenharmony_ci		pr_notice("%s D_RSTA=%02x\n", card->name, val);
12408c2ecf20Sopenharmony_ci	return 0;
12418c2ecf20Sopenharmony_ci}
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_cistatic void
12448c2ecf20Sopenharmony_cirelease_card(struct w6692_hw *card)
12458c2ecf20Sopenharmony_ci{
12468c2ecf20Sopenharmony_ci	u_long	flags;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	spin_lock_irqsave(&card->lock, flags);
12498c2ecf20Sopenharmony_ci	disable_hwirq(card);
12508c2ecf20Sopenharmony_ci	w6692_mode(&card->bc[0], ISDN_P_NONE);
12518c2ecf20Sopenharmony_ci	w6692_mode(&card->bc[1], ISDN_P_NONE);
12528c2ecf20Sopenharmony_ci	if ((card->fmask & led) || card->subtype == W6692_USR) {
12538c2ecf20Sopenharmony_ci		card->xdata |= 0x04;	/*  LED OFF */
12548c2ecf20Sopenharmony_ci		WriteW6692(card, W_XDATA, card->xdata);
12558c2ecf20Sopenharmony_ci	}
12568c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&card->lock, flags);
12578c2ecf20Sopenharmony_ci	free_irq(card->irq, card);
12588c2ecf20Sopenharmony_ci	l1_event(card->dch.l1, CLOSE_CHANNEL);
12598c2ecf20Sopenharmony_ci	mISDN_unregister_device(&card->dch.dev);
12608c2ecf20Sopenharmony_ci	release_region(card->addr, 256);
12618c2ecf20Sopenharmony_ci	mISDN_freebchannel(&card->bc[1].bch);
12628c2ecf20Sopenharmony_ci	mISDN_freebchannel(&card->bc[0].bch);
12638c2ecf20Sopenharmony_ci	mISDN_freedchannel(&card->dch);
12648c2ecf20Sopenharmony_ci	write_lock_irqsave(&card_lock, flags);
12658c2ecf20Sopenharmony_ci	list_del(&card->list);
12668c2ecf20Sopenharmony_ci	write_unlock_irqrestore(&card_lock, flags);
12678c2ecf20Sopenharmony_ci	pci_disable_device(card->pdev);
12688c2ecf20Sopenharmony_ci	pci_set_drvdata(card->pdev, NULL);
12698c2ecf20Sopenharmony_ci	kfree(card);
12708c2ecf20Sopenharmony_ci}
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_cistatic int
12738c2ecf20Sopenharmony_cisetup_instance(struct w6692_hw *card)
12748c2ecf20Sopenharmony_ci{
12758c2ecf20Sopenharmony_ci	int		i, err;
12768c2ecf20Sopenharmony_ci	u_long		flags;
12778c2ecf20Sopenharmony_ci
12788c2ecf20Sopenharmony_ci	snprintf(card->name, MISDN_MAX_IDLEN - 1, "w6692.%d", w6692_cnt + 1);
12798c2ecf20Sopenharmony_ci	write_lock_irqsave(&card_lock, flags);
12808c2ecf20Sopenharmony_ci	list_add_tail(&card->list, &Cards);
12818c2ecf20Sopenharmony_ci	write_unlock_irqrestore(&card_lock, flags);
12828c2ecf20Sopenharmony_ci	card->fmask = (1 << w6692_cnt);
12838c2ecf20Sopenharmony_ci	_set_debug(card);
12848c2ecf20Sopenharmony_ci	spin_lock_init(&card->lock);
12858c2ecf20Sopenharmony_ci	mISDN_initdchannel(&card->dch, MAX_DFRAME_LEN_L1, W6692_ph_bh);
12868c2ecf20Sopenharmony_ci	card->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0);
12878c2ecf20Sopenharmony_ci	card->dch.dev.D.send = w6692_l2l1D;
12888c2ecf20Sopenharmony_ci	card->dch.dev.D.ctrl = w6692_dctrl;
12898c2ecf20Sopenharmony_ci	card->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
12908c2ecf20Sopenharmony_ci		(1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
12918c2ecf20Sopenharmony_ci	card->dch.hw = card;
12928c2ecf20Sopenharmony_ci	card->dch.dev.nrbchan = 2;
12938c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
12948c2ecf20Sopenharmony_ci		mISDN_initbchannel(&card->bc[i].bch, MAX_DATA_MEM,
12958c2ecf20Sopenharmony_ci				   W_B_FIFO_THRESH);
12968c2ecf20Sopenharmony_ci		card->bc[i].bch.hw = card;
12978c2ecf20Sopenharmony_ci		card->bc[i].bch.nr = i + 1;
12988c2ecf20Sopenharmony_ci		card->bc[i].bch.ch.nr = i + 1;
12998c2ecf20Sopenharmony_ci		card->bc[i].bch.ch.send = w6692_l2l1B;
13008c2ecf20Sopenharmony_ci		card->bc[i].bch.ch.ctrl = w6692_bctrl;
13018c2ecf20Sopenharmony_ci		set_channelmap(i + 1, card->dch.dev.channelmap);
13028c2ecf20Sopenharmony_ci		list_add(&card->bc[i].bch.ch.list, &card->dch.dev.bchannels);
13038c2ecf20Sopenharmony_ci	}
13048c2ecf20Sopenharmony_ci	err = setup_w6692(card);
13058c2ecf20Sopenharmony_ci	if (err)
13068c2ecf20Sopenharmony_ci		goto error_setup;
13078c2ecf20Sopenharmony_ci	err = mISDN_register_device(&card->dch.dev, &card->pdev->dev,
13088c2ecf20Sopenharmony_ci				    card->name);
13098c2ecf20Sopenharmony_ci	if (err)
13108c2ecf20Sopenharmony_ci		goto error_reg;
13118c2ecf20Sopenharmony_ci	err = init_card(card);
13128c2ecf20Sopenharmony_ci	if (err)
13138c2ecf20Sopenharmony_ci		goto error_init;
13148c2ecf20Sopenharmony_ci	err = create_l1(&card->dch, w6692_l1callback);
13158c2ecf20Sopenharmony_ci	if (!err) {
13168c2ecf20Sopenharmony_ci		w6692_cnt++;
13178c2ecf20Sopenharmony_ci		pr_notice("W6692 %d cards installed\n", w6692_cnt);
13188c2ecf20Sopenharmony_ci		return 0;
13198c2ecf20Sopenharmony_ci	}
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	free_irq(card->irq, card);
13228c2ecf20Sopenharmony_cierror_init:
13238c2ecf20Sopenharmony_ci	mISDN_unregister_device(&card->dch.dev);
13248c2ecf20Sopenharmony_cierror_reg:
13258c2ecf20Sopenharmony_ci	release_region(card->addr, 256);
13268c2ecf20Sopenharmony_cierror_setup:
13278c2ecf20Sopenharmony_ci	mISDN_freebchannel(&card->bc[1].bch);
13288c2ecf20Sopenharmony_ci	mISDN_freebchannel(&card->bc[0].bch);
13298c2ecf20Sopenharmony_ci	mISDN_freedchannel(&card->dch);
13308c2ecf20Sopenharmony_ci	write_lock_irqsave(&card_lock, flags);
13318c2ecf20Sopenharmony_ci	list_del(&card->list);
13328c2ecf20Sopenharmony_ci	write_unlock_irqrestore(&card_lock, flags);
13338c2ecf20Sopenharmony_ci	kfree(card);
13348c2ecf20Sopenharmony_ci	return err;
13358c2ecf20Sopenharmony_ci}
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_cistatic int
13388c2ecf20Sopenharmony_ciw6692_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
13398c2ecf20Sopenharmony_ci{
13408c2ecf20Sopenharmony_ci	int		err = -ENOMEM;
13418c2ecf20Sopenharmony_ci	struct w6692_hw	*card;
13428c2ecf20Sopenharmony_ci	struct w6692map	*m = (struct w6692map *)ent->driver_data;
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	card = kzalloc(sizeof(struct w6692_hw), GFP_KERNEL);
13458c2ecf20Sopenharmony_ci	if (!card) {
13468c2ecf20Sopenharmony_ci		pr_info("No kmem for w6692 card\n");
13478c2ecf20Sopenharmony_ci		return err;
13488c2ecf20Sopenharmony_ci	}
13498c2ecf20Sopenharmony_ci	card->pdev = pdev;
13508c2ecf20Sopenharmony_ci	card->subtype = m->subtype;
13518c2ecf20Sopenharmony_ci	err = pci_enable_device(pdev);
13528c2ecf20Sopenharmony_ci	if (err) {
13538c2ecf20Sopenharmony_ci		kfree(card);
13548c2ecf20Sopenharmony_ci		return err;
13558c2ecf20Sopenharmony_ci	}
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_ci	printk(KERN_INFO "mISDN_w6692: found adapter %s at %s\n",
13588c2ecf20Sopenharmony_ci	       m->name, pci_name(pdev));
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci	card->addr = pci_resource_start(pdev, 1);
13618c2ecf20Sopenharmony_ci	card->irq = pdev->irq;
13628c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, card);
13638c2ecf20Sopenharmony_ci	err = setup_instance(card);
13648c2ecf20Sopenharmony_ci	if (err)
13658c2ecf20Sopenharmony_ci		pci_set_drvdata(pdev, NULL);
13668c2ecf20Sopenharmony_ci	return err;
13678c2ecf20Sopenharmony_ci}
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_cistatic void
13708c2ecf20Sopenharmony_ciw6692_remove_pci(struct pci_dev *pdev)
13718c2ecf20Sopenharmony_ci{
13728c2ecf20Sopenharmony_ci	struct w6692_hw	*card = pci_get_drvdata(pdev);
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	if (card)
13758c2ecf20Sopenharmony_ci		release_card(card);
13768c2ecf20Sopenharmony_ci	else
13778c2ecf20Sopenharmony_ci		if (debug)
13788c2ecf20Sopenharmony_ci			pr_notice("%s: drvdata already removed\n", __func__);
13798c2ecf20Sopenharmony_ci}
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_cistatic const struct pci_device_id w6692_ids[] = {
13828c2ecf20Sopenharmony_ci	{ PCI_VENDOR_ID_DYNALINK, PCI_DEVICE_ID_DYNALINK_IS64PH,
13838c2ecf20Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, (ulong)&w6692_map[0]},
13848c2ecf20Sopenharmony_ci	{ PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_6692,
13858c2ecf20Sopenharmony_ci	  PCI_VENDOR_ID_USR, PCI_DEVICE_ID_USR_6692, 0, 0,
13868c2ecf20Sopenharmony_ci	  (ulong)&w6692_map[2]},
13878c2ecf20Sopenharmony_ci	{ PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_6692,
13888c2ecf20Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, (ulong)&w6692_map[1]},
13898c2ecf20Sopenharmony_ci	{ }
13908c2ecf20Sopenharmony_ci};
13918c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, w6692_ids);
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_cistatic struct pci_driver w6692_driver = {
13948c2ecf20Sopenharmony_ci	.name =  "w6692",
13958c2ecf20Sopenharmony_ci	.probe = w6692_probe,
13968c2ecf20Sopenharmony_ci	.remove = w6692_remove_pci,
13978c2ecf20Sopenharmony_ci	.id_table = w6692_ids,
13988c2ecf20Sopenharmony_ci};
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_cistatic int __init w6692_init(void)
14018c2ecf20Sopenharmony_ci{
14028c2ecf20Sopenharmony_ci	int err;
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	pr_notice("Winbond W6692 PCI driver Rev. %s\n", W6692_REV);
14058c2ecf20Sopenharmony_ci
14068c2ecf20Sopenharmony_ci	err = pci_register_driver(&w6692_driver);
14078c2ecf20Sopenharmony_ci	return err;
14088c2ecf20Sopenharmony_ci}
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_cistatic void __exit w6692_cleanup(void)
14118c2ecf20Sopenharmony_ci{
14128c2ecf20Sopenharmony_ci	pci_unregister_driver(&w6692_driver);
14138c2ecf20Sopenharmony_ci}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_cimodule_init(w6692_init);
14168c2ecf20Sopenharmony_cimodule_exit(w6692_cleanup);
1417