xref: /openbmc/linux/drivers/isdn/hardware/mISDN/w6692.c (revision 3e5981774755f2f967cd1d3f3898576cff20e565)
1707b2ce6SKarsten Keil /*
2707b2ce6SKarsten Keil  * w6692.c     mISDN driver for Winbond w6692 based cards
3707b2ce6SKarsten Keil  *
4707b2ce6SKarsten Keil  * Author      Karsten Keil <kkeil@suse.de>
5707b2ce6SKarsten Keil  *             based on the w6692 I4L driver from Petr Novak <petr.novak@i.cz>
6707b2ce6SKarsten Keil  *
7707b2ce6SKarsten Keil  * Copyright 2009  by Karsten Keil <keil@isdn4linux.de>
8707b2ce6SKarsten Keil  *
9707b2ce6SKarsten Keil  * This program is free software; you can redistribute it and/or modify
10707b2ce6SKarsten Keil  * it under the terms of the GNU General Public License version 2 as
11707b2ce6SKarsten Keil  * published by the Free Software Foundation.
12707b2ce6SKarsten Keil  *
13707b2ce6SKarsten Keil  * This program is distributed in the hope that it will be useful,
14707b2ce6SKarsten Keil  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15707b2ce6SKarsten Keil  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16707b2ce6SKarsten Keil  * GNU General Public License for more details.
17707b2ce6SKarsten Keil  *
18707b2ce6SKarsten Keil  * You should have received a copy of the GNU General Public License
19707b2ce6SKarsten Keil  * along with this program; if not, write to the Free Software
20707b2ce6SKarsten Keil  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21707b2ce6SKarsten Keil  *
22707b2ce6SKarsten Keil  */
23707b2ce6SKarsten Keil 
24707b2ce6SKarsten Keil #include <linux/module.h>
25707b2ce6SKarsten Keil #include <linux/pci.h>
26707b2ce6SKarsten Keil #include <linux/delay.h>
27707b2ce6SKarsten Keil #include <linux/mISDNhw.h>
28707b2ce6SKarsten Keil #include "w6692.h"
29707b2ce6SKarsten Keil 
30707b2ce6SKarsten Keil #define W6692_REV	"2.0"
31707b2ce6SKarsten Keil 
32707b2ce6SKarsten Keil #define DBUSY_TIMER_VALUE	80
33707b2ce6SKarsten Keil 
34707b2ce6SKarsten Keil enum {
35707b2ce6SKarsten Keil 	W6692_ASUS,
36707b2ce6SKarsten Keil 	W6692_WINBOND,
37707b2ce6SKarsten Keil 	W6692_USR
38707b2ce6SKarsten Keil };
39707b2ce6SKarsten Keil 
40707b2ce6SKarsten Keil /* private data in the PCI devices list */
41707b2ce6SKarsten Keil struct w6692map {
42707b2ce6SKarsten Keil 	u_int	subtype;
43707b2ce6SKarsten Keil 	char	*name;
44707b2ce6SKarsten Keil };
45707b2ce6SKarsten Keil 
46707b2ce6SKarsten Keil static const struct w6692map  w6692_map[] =
47707b2ce6SKarsten Keil {
48707b2ce6SKarsten Keil 	{W6692_ASUS, "Dynalink/AsusCom IS64PH"},
49707b2ce6SKarsten Keil 	{W6692_WINBOND, "Winbond W6692"},
50707b2ce6SKarsten Keil 	{W6692_USR, "USR W6692"}
51707b2ce6SKarsten Keil };
52707b2ce6SKarsten Keil 
53707b2ce6SKarsten Keil #ifndef PCI_VENDOR_ID_USR
54707b2ce6SKarsten Keil #define PCI_VENDOR_ID_USR	0x16ec
55707b2ce6SKarsten Keil #define PCI_DEVICE_ID_USR_6692	0x3409
56707b2ce6SKarsten Keil #endif
57707b2ce6SKarsten Keil 
58707b2ce6SKarsten Keil struct w6692_ch {
59707b2ce6SKarsten Keil 	struct bchannel		bch;
60707b2ce6SKarsten Keil 	u32			addr;
61707b2ce6SKarsten Keil 	struct timer_list	timer;
62707b2ce6SKarsten Keil 	u8			b_mode;
63707b2ce6SKarsten Keil };
64707b2ce6SKarsten Keil 
65707b2ce6SKarsten Keil struct w6692_hw {
66707b2ce6SKarsten Keil 	struct list_head	list;
67707b2ce6SKarsten Keil 	struct pci_dev		*pdev;
68707b2ce6SKarsten Keil 	char			name[MISDN_MAX_IDLEN];
69707b2ce6SKarsten Keil 	u32			irq;
70707b2ce6SKarsten Keil 	u32			irqcnt;
71707b2ce6SKarsten Keil 	u32			addr;
72707b2ce6SKarsten Keil 	u32			fmask;	/* feature mask - bit set per card nr */
73707b2ce6SKarsten Keil 	int			subtype;
74707b2ce6SKarsten Keil 	spinlock_t		lock;	/* hw lock */
75707b2ce6SKarsten Keil 	u8			imask;
76707b2ce6SKarsten Keil 	u8			pctl;
77707b2ce6SKarsten Keil 	u8			xaddr;
78707b2ce6SKarsten Keil 	u8			xdata;
79707b2ce6SKarsten Keil 	u8			state;
80707b2ce6SKarsten Keil 	struct w6692_ch		bc[2];
81707b2ce6SKarsten Keil 	struct dchannel		dch;
82707b2ce6SKarsten Keil 	char			log[64];
83707b2ce6SKarsten Keil };
84707b2ce6SKarsten Keil 
85707b2ce6SKarsten Keil static LIST_HEAD(Cards);
86707b2ce6SKarsten Keil static DEFINE_RWLOCK(card_lock); /* protect Cards */
87707b2ce6SKarsten Keil 
88707b2ce6SKarsten Keil static int w6692_cnt;
89707b2ce6SKarsten Keil static int debug;
90707b2ce6SKarsten Keil static u32 led;
91707b2ce6SKarsten Keil static u32 pots;
92707b2ce6SKarsten Keil 
93707b2ce6SKarsten Keil static void
94707b2ce6SKarsten Keil _set_debug(struct w6692_hw *card)
95707b2ce6SKarsten Keil {
96707b2ce6SKarsten Keil 	card->dch.debug = debug;
97707b2ce6SKarsten Keil 	card->bc[0].bch.debug = debug;
98707b2ce6SKarsten Keil 	card->bc[1].bch.debug = debug;
99707b2ce6SKarsten Keil }
100707b2ce6SKarsten Keil 
101707b2ce6SKarsten Keil static int
102707b2ce6SKarsten Keil set_debug(const char *val, struct kernel_param *kp)
103707b2ce6SKarsten Keil {
104707b2ce6SKarsten Keil 	int ret;
105707b2ce6SKarsten Keil 	struct w6692_hw *card;
106707b2ce6SKarsten Keil 
107707b2ce6SKarsten Keil 	ret = param_set_uint(val, kp);
108707b2ce6SKarsten Keil 	if (!ret) {
109707b2ce6SKarsten Keil 		read_lock(&card_lock);
110707b2ce6SKarsten Keil 		list_for_each_entry(card, &Cards, list)
111707b2ce6SKarsten Keil 			_set_debug(card);
112707b2ce6SKarsten Keil 		read_unlock(&card_lock);
113707b2ce6SKarsten Keil 	}
114707b2ce6SKarsten Keil 	return ret;
115707b2ce6SKarsten Keil }
116707b2ce6SKarsten Keil 
117707b2ce6SKarsten Keil MODULE_AUTHOR("Karsten Keil");
118707b2ce6SKarsten Keil MODULE_LICENSE("GPL v2");
119707b2ce6SKarsten Keil MODULE_VERSION(W6692_REV);
120707b2ce6SKarsten Keil module_param_call(debug, set_debug, param_get_uint, &debug, S_IRUGO | S_IWUSR);
121707b2ce6SKarsten Keil MODULE_PARM_DESC(debug, "W6692 debug mask");
122707b2ce6SKarsten Keil module_param(led, uint, S_IRUGO | S_IWUSR);
123707b2ce6SKarsten Keil MODULE_PARM_DESC(led, "W6692 LED support bitmask (one bit per card)");
124707b2ce6SKarsten Keil module_param(pots, uint, S_IRUGO | S_IWUSR);
125707b2ce6SKarsten Keil MODULE_PARM_DESC(pots, "W6692 POTS support bitmask (one bit per card)");
126707b2ce6SKarsten Keil 
127707b2ce6SKarsten Keil static inline u8
128707b2ce6SKarsten Keil ReadW6692(struct w6692_hw *card, u8 offset)
129707b2ce6SKarsten Keil {
130707b2ce6SKarsten Keil 	return inb(card->addr + offset);
131707b2ce6SKarsten Keil }
132707b2ce6SKarsten Keil 
133707b2ce6SKarsten Keil static inline void
134707b2ce6SKarsten Keil WriteW6692(struct w6692_hw *card, u8 offset, u8 value)
135707b2ce6SKarsten Keil {
136707b2ce6SKarsten Keil 	outb(value, card->addr + offset);
137707b2ce6SKarsten Keil }
138707b2ce6SKarsten Keil 
139707b2ce6SKarsten Keil static inline u8
140707b2ce6SKarsten Keil ReadW6692B(struct w6692_ch *bc, u8 offset)
141707b2ce6SKarsten Keil {
142707b2ce6SKarsten Keil 	return inb(bc->addr + offset);
143707b2ce6SKarsten Keil }
144707b2ce6SKarsten Keil 
145707b2ce6SKarsten Keil static inline void
146707b2ce6SKarsten Keil WriteW6692B(struct w6692_ch *bc, u8 offset, u8 value)
147707b2ce6SKarsten Keil {
148707b2ce6SKarsten Keil 	outb(value, bc->addr + offset);
149707b2ce6SKarsten Keil }
150707b2ce6SKarsten Keil 
151707b2ce6SKarsten Keil static void
152707b2ce6SKarsten Keil enable_hwirq(struct w6692_hw *card)
153707b2ce6SKarsten Keil {
154707b2ce6SKarsten Keil 	WriteW6692(card, W_IMASK, card->imask);
155707b2ce6SKarsten Keil }
156707b2ce6SKarsten Keil 
157707b2ce6SKarsten Keil static void
158707b2ce6SKarsten Keil disable_hwirq(struct w6692_hw *card)
159707b2ce6SKarsten Keil {
160707b2ce6SKarsten Keil 	WriteW6692(card, W_IMASK, 0xff);
161707b2ce6SKarsten Keil }
162707b2ce6SKarsten Keil 
163707b2ce6SKarsten Keil static const char *W6692Ver[] = {"V00", "V01", "V10", "V11"};
164707b2ce6SKarsten Keil 
165707b2ce6SKarsten Keil static void
166707b2ce6SKarsten Keil W6692Version(struct w6692_hw *card)
167707b2ce6SKarsten Keil {
168707b2ce6SKarsten Keil 	int val;
169707b2ce6SKarsten Keil 
170707b2ce6SKarsten Keil 	val = ReadW6692(card, W_D_RBCH);
171707b2ce6SKarsten Keil 	pr_notice("%s: Winbond W6692 version: %s\n", card->name,
172707b2ce6SKarsten Keil 		W6692Ver[(val >> 6) & 3]);
173707b2ce6SKarsten Keil }
174707b2ce6SKarsten Keil 
175707b2ce6SKarsten Keil static void
176707b2ce6SKarsten Keil w6692_led_handler(struct w6692_hw *card, int on)
177707b2ce6SKarsten Keil {
178707b2ce6SKarsten Keil 	if ((!(card->fmask & led)) || card->subtype == W6692_USR)
179707b2ce6SKarsten Keil 		return;
180707b2ce6SKarsten Keil 	if (on) {
181707b2ce6SKarsten Keil 		card->xdata &= 0xfb;	/*  LED ON */
182707b2ce6SKarsten Keil 		WriteW6692(card, W_XDATA, card->xdata);
183707b2ce6SKarsten Keil 	} else {
184707b2ce6SKarsten Keil 		card->xdata |= 0x04;	/*  LED OFF */
185707b2ce6SKarsten Keil 		WriteW6692(card, W_XDATA, card->xdata);
186707b2ce6SKarsten Keil 	}
187707b2ce6SKarsten Keil }
188707b2ce6SKarsten Keil 
189707b2ce6SKarsten Keil static void
190707b2ce6SKarsten Keil ph_command(struct w6692_hw *card, u8 cmd)
191707b2ce6SKarsten Keil {
192707b2ce6SKarsten Keil 	pr_debug("%s: ph_command %x\n", card->name, cmd);
193707b2ce6SKarsten Keil 	WriteW6692(card, W_CIX, cmd);
194707b2ce6SKarsten Keil }
195707b2ce6SKarsten Keil 
196707b2ce6SKarsten Keil static void
197707b2ce6SKarsten Keil W6692_new_ph(struct w6692_hw *card)
198707b2ce6SKarsten Keil {
199707b2ce6SKarsten Keil 	if (card->state == W_L1CMD_RST)
200707b2ce6SKarsten Keil 		ph_command(card, W_L1CMD_DRC);
201707b2ce6SKarsten Keil 	schedule_event(&card->dch, FLG_PHCHANGE);
202707b2ce6SKarsten Keil }
203707b2ce6SKarsten Keil 
204707b2ce6SKarsten Keil static void
205707b2ce6SKarsten Keil W6692_ph_bh(struct dchannel *dch)
206707b2ce6SKarsten Keil {
207707b2ce6SKarsten Keil 	struct w6692_hw *card = dch->hw;
208707b2ce6SKarsten Keil 
209707b2ce6SKarsten Keil 	switch (card->state) {
210707b2ce6SKarsten Keil 	case W_L1CMD_RST:
211707b2ce6SKarsten Keil 		dch->state = 0;
212707b2ce6SKarsten Keil 		l1_event(dch->l1, HW_RESET_IND);
213707b2ce6SKarsten Keil 		break;
214707b2ce6SKarsten Keil 	case W_L1IND_CD:
215707b2ce6SKarsten Keil 		dch->state = 3;
216707b2ce6SKarsten Keil 		l1_event(dch->l1, HW_DEACT_CNF);
217707b2ce6SKarsten Keil 		break;
218707b2ce6SKarsten Keil 	case W_L1IND_DRD:
219707b2ce6SKarsten Keil 		dch->state = 3;
220707b2ce6SKarsten Keil 		l1_event(dch->l1, HW_DEACT_IND);
221707b2ce6SKarsten Keil 		break;
222707b2ce6SKarsten Keil 	case W_L1IND_CE:
223707b2ce6SKarsten Keil 		dch->state = 4;
224707b2ce6SKarsten Keil 		l1_event(dch->l1, HW_POWERUP_IND);
225707b2ce6SKarsten Keil 		break;
226707b2ce6SKarsten Keil 	case W_L1IND_LD:
227707b2ce6SKarsten Keil 		if (dch->state <= 5) {
228707b2ce6SKarsten Keil 			dch->state = 5;
229707b2ce6SKarsten Keil 			l1_event(dch->l1, ANYSIGNAL);
230707b2ce6SKarsten Keil 		} else {
231707b2ce6SKarsten Keil 			dch->state = 8;
232707b2ce6SKarsten Keil 			l1_event(dch->l1, LOSTFRAMING);
233707b2ce6SKarsten Keil 		}
234707b2ce6SKarsten Keil 		break;
235707b2ce6SKarsten Keil 	case W_L1IND_ARD:
236707b2ce6SKarsten Keil 		dch->state = 6;
237707b2ce6SKarsten Keil 		l1_event(dch->l1, INFO2);
238707b2ce6SKarsten Keil 		break;
239707b2ce6SKarsten Keil 	case W_L1IND_AI8:
240707b2ce6SKarsten Keil 		dch->state = 7;
241707b2ce6SKarsten Keil 		l1_event(dch->l1, INFO4_P8);
242707b2ce6SKarsten Keil 		break;
243707b2ce6SKarsten Keil 	case W_L1IND_AI10:
244707b2ce6SKarsten Keil 		dch->state = 7;
245707b2ce6SKarsten Keil 		l1_event(dch->l1, INFO4_P10);
246707b2ce6SKarsten Keil 		break;
247707b2ce6SKarsten Keil 	default:
248707b2ce6SKarsten Keil 		pr_debug("%s: TE unknown state %02x dch state %02x\n",
249707b2ce6SKarsten Keil 			card->name, card->state, dch->state);
250707b2ce6SKarsten Keil 		break;
251707b2ce6SKarsten Keil 	}
252707b2ce6SKarsten Keil 	pr_debug("%s: TE newstate %02x\n", card->name, dch->state);
253707b2ce6SKarsten Keil }
254707b2ce6SKarsten Keil 
255707b2ce6SKarsten Keil static void
256707b2ce6SKarsten Keil W6692_empty_Dfifo(struct w6692_hw *card, int count)
257707b2ce6SKarsten Keil {
258707b2ce6SKarsten Keil 	struct dchannel *dch = &card->dch;
259707b2ce6SKarsten Keil 	u8 *ptr;
260707b2ce6SKarsten Keil 
261707b2ce6SKarsten Keil 	pr_debug("%s: empty_Dfifo %d\n", card->name, count);
262707b2ce6SKarsten Keil 	if (!dch->rx_skb) {
263707b2ce6SKarsten Keil 		dch->rx_skb = mI_alloc_skb(card->dch.maxlen, GFP_ATOMIC);
264707b2ce6SKarsten Keil 		if (!dch->rx_skb) {
265707b2ce6SKarsten Keil 			pr_info("%s: D receive out of memory\n", card->name);
266707b2ce6SKarsten Keil 			WriteW6692(card, W_D_CMDR, W_D_CMDR_RACK);
267707b2ce6SKarsten Keil 			return;
268707b2ce6SKarsten Keil 		}
269707b2ce6SKarsten Keil 	}
270707b2ce6SKarsten Keil 	if ((dch->rx_skb->len + count) >= dch->maxlen) {
271707b2ce6SKarsten Keil 		pr_debug("%s: empty_Dfifo overrun %d\n", card->name,
272707b2ce6SKarsten Keil 			dch->rx_skb->len + count);
273707b2ce6SKarsten Keil 		WriteW6692(card, W_D_CMDR, W_D_CMDR_RACK);
274707b2ce6SKarsten Keil 		return;
275707b2ce6SKarsten Keil 	}
276707b2ce6SKarsten Keil 	ptr = skb_put(dch->rx_skb, count);
277707b2ce6SKarsten Keil 	insb(card->addr + W_D_RFIFO, ptr, count);
278707b2ce6SKarsten Keil 	WriteW6692(card, W_D_CMDR, W_D_CMDR_RACK);
279707b2ce6SKarsten Keil 	if (debug & DEBUG_HW_DFIFO) {
280707b2ce6SKarsten Keil 		snprintf(card->log, 63, "D-recv %s %d ",
281707b2ce6SKarsten Keil 			card->name, count);
282707b2ce6SKarsten Keil 		print_hex_dump_bytes(card->log, DUMP_PREFIX_OFFSET, ptr, count);
283707b2ce6SKarsten Keil 	}
284707b2ce6SKarsten Keil }
285707b2ce6SKarsten Keil 
286707b2ce6SKarsten Keil static void
287707b2ce6SKarsten Keil W6692_fill_Dfifo(struct w6692_hw *card)
288707b2ce6SKarsten Keil {
289707b2ce6SKarsten Keil 	struct dchannel *dch = &card->dch;
290707b2ce6SKarsten Keil 	int count;
291707b2ce6SKarsten Keil 	u8 *ptr;
292707b2ce6SKarsten Keil 	u8 cmd = W_D_CMDR_XMS;
293707b2ce6SKarsten Keil 
294707b2ce6SKarsten Keil 	pr_debug("%s: fill_Dfifo\n", card->name);
295707b2ce6SKarsten Keil 	if (!dch->tx_skb)
296707b2ce6SKarsten Keil 		return;
297707b2ce6SKarsten Keil 	count = dch->tx_skb->len - dch->tx_idx;
298707b2ce6SKarsten Keil 	if (count <= 0)
299707b2ce6SKarsten Keil 		return;
300707b2ce6SKarsten Keil 	if (count > W_D_FIFO_THRESH)
301707b2ce6SKarsten Keil 		count = W_D_FIFO_THRESH;
302707b2ce6SKarsten Keil 	else
303707b2ce6SKarsten Keil 		cmd |= W_D_CMDR_XME;
304707b2ce6SKarsten Keil 	ptr = dch->tx_skb->data + dch->tx_idx;
305707b2ce6SKarsten Keil 	dch->tx_idx += count;
306707b2ce6SKarsten Keil 	outsb(card->addr + W_D_XFIFO, ptr, count);
307707b2ce6SKarsten Keil 	WriteW6692(card, W_D_CMDR, cmd);
308707b2ce6SKarsten Keil 	if (test_and_set_bit(FLG_BUSY_TIMER, &dch->Flags)) {
309707b2ce6SKarsten Keil 		pr_debug("%s: fill_Dfifo dbusytimer running\n", card->name);
310707b2ce6SKarsten Keil 		del_timer(&dch->timer);
311707b2ce6SKarsten Keil 	}
312707b2ce6SKarsten Keil 	init_timer(&dch->timer);
313707b2ce6SKarsten Keil 	dch->timer.expires = jiffies + ((DBUSY_TIMER_VALUE * HZ)/1000);
314707b2ce6SKarsten Keil 	add_timer(&dch->timer);
315707b2ce6SKarsten Keil 	if (debug & DEBUG_HW_DFIFO) {
316707b2ce6SKarsten Keil 		snprintf(card->log, 63, "D-send %s %d ",
317707b2ce6SKarsten Keil 			card->name, count);
318707b2ce6SKarsten Keil 		print_hex_dump_bytes(card->log, DUMP_PREFIX_OFFSET, ptr, count);
319707b2ce6SKarsten Keil 	}
320707b2ce6SKarsten Keil }
321707b2ce6SKarsten Keil 
322707b2ce6SKarsten Keil static void
323707b2ce6SKarsten Keil d_retransmit(struct w6692_hw *card)
324707b2ce6SKarsten Keil {
325707b2ce6SKarsten Keil 	struct dchannel *dch = &card->dch;
326707b2ce6SKarsten Keil 
327707b2ce6SKarsten Keil 	if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
328707b2ce6SKarsten Keil 		del_timer(&dch->timer);
329707b2ce6SKarsten Keil #ifdef FIXME
330707b2ce6SKarsten Keil 	if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
331707b2ce6SKarsten Keil 		dchannel_sched_event(dch, D_CLEARBUSY);
332707b2ce6SKarsten Keil #endif
333707b2ce6SKarsten Keil 	if (test_bit(FLG_TX_BUSY, &dch->Flags)) {
334707b2ce6SKarsten Keil 		/* Restart frame */
335707b2ce6SKarsten Keil 		dch->tx_idx = 0;
336707b2ce6SKarsten Keil 		W6692_fill_Dfifo(card);
337707b2ce6SKarsten Keil 	} else if (dch->tx_skb) { /* should not happen */
338707b2ce6SKarsten Keil 		pr_info("%s: %s without TX_BUSY\n", card->name, __func__);
339707b2ce6SKarsten Keil 		test_and_set_bit(FLG_TX_BUSY, &dch->Flags);
340707b2ce6SKarsten Keil 		dch->tx_idx = 0;
341707b2ce6SKarsten Keil 		W6692_fill_Dfifo(card);
342707b2ce6SKarsten Keil 	} else {
343707b2ce6SKarsten Keil 		pr_info("%s: XDU no TX_BUSY\n", card->name);
344707b2ce6SKarsten Keil 		if (get_next_dframe(dch))
345707b2ce6SKarsten Keil 			W6692_fill_Dfifo(card);
346707b2ce6SKarsten Keil 	}
347707b2ce6SKarsten Keil }
348707b2ce6SKarsten Keil 
349707b2ce6SKarsten Keil static void
350707b2ce6SKarsten Keil handle_rxD(struct w6692_hw *card) {
351707b2ce6SKarsten Keil 	u8	stat;
352707b2ce6SKarsten Keil 	int	count;
353707b2ce6SKarsten Keil 
354707b2ce6SKarsten Keil 	stat = ReadW6692(card, W_D_RSTA);
355707b2ce6SKarsten Keil 	if (stat & (W_D_RSTA_RDOV | W_D_RSTA_CRCE | W_D_RSTA_RMB)) {
356707b2ce6SKarsten Keil 		if (stat & W_D_RSTA_RDOV) {
357707b2ce6SKarsten Keil 			pr_debug("%s: D-channel RDOV\n", card->name);
358707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
359707b2ce6SKarsten Keil 			card->dch.err_rx++;
360707b2ce6SKarsten Keil #endif
361707b2ce6SKarsten Keil 		}
362707b2ce6SKarsten Keil 		if (stat & W_D_RSTA_CRCE) {
363707b2ce6SKarsten Keil 			pr_debug("%s: D-channel CRC error\n", card->name);
364707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
365707b2ce6SKarsten Keil 			card->dch.err_crc++;
366707b2ce6SKarsten Keil #endif
367707b2ce6SKarsten Keil 		}
368707b2ce6SKarsten Keil 		if (stat & W_D_RSTA_RMB) {
369707b2ce6SKarsten Keil 			pr_debug("%s: D-channel ABORT\n", card->name);
370707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
371707b2ce6SKarsten Keil 			card->dch.err_rx++;
372707b2ce6SKarsten Keil #endif
373707b2ce6SKarsten Keil 		}
374707b2ce6SKarsten Keil 		if (card->dch.rx_skb)
375707b2ce6SKarsten Keil 			dev_kfree_skb(card->dch.rx_skb);
376707b2ce6SKarsten Keil 		card->dch.rx_skb = NULL;
377707b2ce6SKarsten Keil 		WriteW6692(card, W_D_CMDR, W_D_CMDR_RACK | W_D_CMDR_RRST);
378707b2ce6SKarsten Keil 	} else {
379707b2ce6SKarsten Keil 		count = ReadW6692(card, W_D_RBCL) & (W_D_FIFO_THRESH - 1);
380707b2ce6SKarsten Keil 		if (count == 0)
381707b2ce6SKarsten Keil 			count = W_D_FIFO_THRESH;
382707b2ce6SKarsten Keil 		W6692_empty_Dfifo(card, count);
383707b2ce6SKarsten Keil 		recv_Dchannel(&card->dch);
384707b2ce6SKarsten Keil 	}
385707b2ce6SKarsten Keil }
386707b2ce6SKarsten Keil 
387707b2ce6SKarsten Keil static void
388707b2ce6SKarsten Keil handle_txD(struct w6692_hw *card) {
389707b2ce6SKarsten Keil 	if (test_and_clear_bit(FLG_BUSY_TIMER, &card->dch.Flags))
390707b2ce6SKarsten Keil 		del_timer(&card->dch.timer);
391707b2ce6SKarsten Keil 	if (card->dch.tx_skb && card->dch.tx_idx < card->dch.tx_skb->len) {
392707b2ce6SKarsten Keil 		W6692_fill_Dfifo(card);
393707b2ce6SKarsten Keil 	} else {
394707b2ce6SKarsten Keil 		if (card->dch.tx_skb)
395707b2ce6SKarsten Keil 			dev_kfree_skb(card->dch.tx_skb);
396707b2ce6SKarsten Keil 		if (get_next_dframe(&card->dch))
397707b2ce6SKarsten Keil 			W6692_fill_Dfifo(card);
398707b2ce6SKarsten Keil 	}
399707b2ce6SKarsten Keil }
400707b2ce6SKarsten Keil 
401707b2ce6SKarsten Keil static void
402707b2ce6SKarsten Keil handle_statusD(struct w6692_hw *card)
403707b2ce6SKarsten Keil {
404707b2ce6SKarsten Keil 	struct dchannel *dch = &card->dch;
405707b2ce6SKarsten Keil 	u8 exval, v1, cir;
406707b2ce6SKarsten Keil 
407707b2ce6SKarsten Keil 	exval = ReadW6692(card, W_D_EXIR);
408707b2ce6SKarsten Keil 
409707b2ce6SKarsten Keil 	pr_debug("%s: D_EXIR %02x\n", card->name, exval);
410707b2ce6SKarsten Keil 	if (exval & (W_D_EXI_XDUN | W_D_EXI_XCOL)) {
411707b2ce6SKarsten Keil 		/* Transmit underrun/collision */
412707b2ce6SKarsten Keil 		pr_debug("%s: D-channel underrun/collision\n", card->name);
413707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
414707b2ce6SKarsten Keil 		dch->err_tx++;
415707b2ce6SKarsten Keil #endif
416707b2ce6SKarsten Keil 		d_retransmit(card);
417707b2ce6SKarsten Keil 	}
418707b2ce6SKarsten Keil 	if (exval & W_D_EXI_RDOV) {	/* RDOV */
419707b2ce6SKarsten Keil 		pr_debug("%s: D-channel RDOV\n", card->name);
420707b2ce6SKarsten Keil 		WriteW6692(card, W_D_CMDR, W_D_CMDR_RRST);
421707b2ce6SKarsten Keil 	}
422707b2ce6SKarsten Keil 	if (exval & W_D_EXI_TIN2)	/* TIN2 - never */
423707b2ce6SKarsten Keil 		pr_debug("%s: spurious TIN2 interrupt\n", card->name);
424707b2ce6SKarsten Keil 	if (exval & W_D_EXI_MOC) {	/* MOC - not supported */
425707b2ce6SKarsten Keil 		v1 = ReadW6692(card, W_MOSR);
426707b2ce6SKarsten Keil 		pr_debug("%s: spurious MOC interrupt MOSR %02x\n",
427707b2ce6SKarsten Keil 			card->name, v1);
428707b2ce6SKarsten Keil 	}
429707b2ce6SKarsten Keil 	if (exval & W_D_EXI_ISC) {	/* ISC - Level1 change */
430707b2ce6SKarsten Keil 		cir = ReadW6692(card, W_CIR);
431707b2ce6SKarsten Keil 		pr_debug("%s: ISC CIR %02X\n", card->name, cir);
432707b2ce6SKarsten Keil 		if (cir & W_CIR_ICC) {
433707b2ce6SKarsten Keil 			v1 = cir & W_CIR_COD_MASK;
434707b2ce6SKarsten Keil 			pr_debug("%s: ph_state_change %x -> %x\n", card->name,
435707b2ce6SKarsten Keil 				dch->state, v1);
436707b2ce6SKarsten Keil 			card->state = v1;
437707b2ce6SKarsten Keil 			if (card->fmask & led) {
438707b2ce6SKarsten Keil 				switch (v1) {
439707b2ce6SKarsten Keil 				case W_L1IND_AI8:
440707b2ce6SKarsten Keil 				case W_L1IND_AI10:
441707b2ce6SKarsten Keil 					w6692_led_handler(card, 1);
442707b2ce6SKarsten Keil 					break;
443707b2ce6SKarsten Keil 				default:
444707b2ce6SKarsten Keil 					w6692_led_handler(card, 0);
445707b2ce6SKarsten Keil 					break;
446707b2ce6SKarsten Keil 				}
447707b2ce6SKarsten Keil 			}
448707b2ce6SKarsten Keil 			W6692_new_ph(card);
449707b2ce6SKarsten Keil 		}
450707b2ce6SKarsten Keil 		if (cir & W_CIR_SCC) {
451707b2ce6SKarsten Keil 			v1 = ReadW6692(card, W_SQR);
452707b2ce6SKarsten Keil 			pr_debug("%s: SCC SQR %02X\n", card->name, v1);
453707b2ce6SKarsten Keil 		}
454707b2ce6SKarsten Keil 	}
455707b2ce6SKarsten Keil 	if (exval & W_D_EXI_WEXP)
456707b2ce6SKarsten Keil 		pr_debug("%s: spurious WEXP interrupt!\n", card->name);
457707b2ce6SKarsten Keil 	if (exval & W_D_EXI_TEXP)
458707b2ce6SKarsten Keil 		pr_debug("%s: spurious TEXP interrupt!\n", card->name);
459707b2ce6SKarsten Keil }
460707b2ce6SKarsten Keil 
461707b2ce6SKarsten Keil static void
462707b2ce6SKarsten Keil W6692_empty_Bfifo(struct w6692_ch *wch, int count)
463707b2ce6SKarsten Keil {
464707b2ce6SKarsten Keil 	struct w6692_hw *card = wch->bch.hw;
465707b2ce6SKarsten Keil 	u8 *ptr;
466707b2ce6SKarsten Keil 
467707b2ce6SKarsten Keil 	pr_debug("%s: empty_Bfifo %d\n", card->name, count);
468707b2ce6SKarsten Keil 	if (unlikely(wch->bch.state == ISDN_P_NONE)) {
469707b2ce6SKarsten Keil 		pr_debug("%s: empty_Bfifo ISDN_P_NONE\n", card->name);
470707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK | W_B_CMDR_RACT);
471707b2ce6SKarsten Keil 		if (wch->bch.rx_skb)
472707b2ce6SKarsten Keil 			skb_trim(wch->bch.rx_skb, 0);
473707b2ce6SKarsten Keil 		return;
474707b2ce6SKarsten Keil 	}
475707b2ce6SKarsten Keil 	if (!wch->bch.rx_skb) {
476707b2ce6SKarsten Keil 		wch->bch.rx_skb = mI_alloc_skb(wch->bch.maxlen, GFP_ATOMIC);
477707b2ce6SKarsten Keil 		if (unlikely(!wch->bch.rx_skb)) {
478707b2ce6SKarsten Keil 			pr_info("%s: B receive out of memory\n", card->name);
479707b2ce6SKarsten Keil 			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK |
480707b2ce6SKarsten Keil 				W_B_CMDR_RACT);
481707b2ce6SKarsten Keil 			return;
482707b2ce6SKarsten Keil 		}
483707b2ce6SKarsten Keil 	}
484707b2ce6SKarsten Keil 	if (wch->bch.rx_skb->len + count > wch->bch.maxlen) {
485707b2ce6SKarsten Keil 		pr_debug("%s: empty_Bfifo incoming packet too large\n",
486707b2ce6SKarsten Keil 			card->name);
487707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK | W_B_CMDR_RACT);
488707b2ce6SKarsten Keil 		skb_trim(wch->bch.rx_skb, 0);
489707b2ce6SKarsten Keil 		return;
490707b2ce6SKarsten Keil 	}
491707b2ce6SKarsten Keil 	ptr = skb_put(wch->bch.rx_skb, count);
492707b2ce6SKarsten Keil 	insb(wch->addr + W_B_RFIFO, ptr, count);
493707b2ce6SKarsten Keil 	WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK | W_B_CMDR_RACT);
494707b2ce6SKarsten Keil 	if (debug & DEBUG_HW_DFIFO) {
495707b2ce6SKarsten Keil 		snprintf(card->log, 63, "B%1d-recv %s %d ",
496707b2ce6SKarsten Keil 			wch->bch.nr, card->name, count);
497707b2ce6SKarsten Keil 		print_hex_dump_bytes(card->log, DUMP_PREFIX_OFFSET, ptr, count);
498707b2ce6SKarsten Keil 	}
499707b2ce6SKarsten Keil }
500707b2ce6SKarsten Keil 
501707b2ce6SKarsten Keil static void
502707b2ce6SKarsten Keil W6692_fill_Bfifo(struct w6692_ch *wch)
503707b2ce6SKarsten Keil {
504707b2ce6SKarsten Keil 	struct w6692_hw *card = wch->bch.hw;
505707b2ce6SKarsten Keil 	int count;
506707b2ce6SKarsten Keil 	u8 *ptr, cmd = W_B_CMDR_RACT | W_B_CMDR_XMS;
507707b2ce6SKarsten Keil 
508707b2ce6SKarsten Keil 	pr_debug("%s: fill Bfifo\n", card->name);
509707b2ce6SKarsten Keil 	if (!wch->bch.tx_skb)
510707b2ce6SKarsten Keil 		return;
511707b2ce6SKarsten Keil 	count = wch->bch.tx_skb->len - wch->bch.tx_idx;
512707b2ce6SKarsten Keil 	if (count <= 0)
513707b2ce6SKarsten Keil 		return;
514707b2ce6SKarsten Keil 	ptr = wch->bch.tx_skb->data + wch->bch.tx_idx;
515707b2ce6SKarsten Keil 	if (count > W_B_FIFO_THRESH)
516707b2ce6SKarsten Keil 		count = W_B_FIFO_THRESH;
517707b2ce6SKarsten Keil 	else if (test_bit(FLG_HDLC, &wch->bch.Flags))
518707b2ce6SKarsten Keil 		cmd |= W_B_CMDR_XME;
519707b2ce6SKarsten Keil 
520707b2ce6SKarsten Keil 	pr_debug("%s: fill Bfifo%d/%d\n", card->name,
521707b2ce6SKarsten Keil 			count, wch->bch.tx_idx);
522707b2ce6SKarsten Keil 	wch->bch.tx_idx += count;
523707b2ce6SKarsten Keil 	outsb(wch->addr + W_B_XFIFO, ptr, count);
524707b2ce6SKarsten Keil 	WriteW6692B(wch, W_B_CMDR, cmd);
525707b2ce6SKarsten Keil 	if (debug & DEBUG_HW_DFIFO) {
526707b2ce6SKarsten Keil 		snprintf(card->log, 63, "B%1d-send %s %d ",
527707b2ce6SKarsten Keil 			wch->bch.nr, card->name, count);
528707b2ce6SKarsten Keil 		print_hex_dump_bytes(card->log, DUMP_PREFIX_OFFSET, ptr, count);
529707b2ce6SKarsten Keil 	}
530707b2ce6SKarsten Keil }
531707b2ce6SKarsten Keil 
532*3e598177SJiri Slaby #if 0
533707b2ce6SKarsten Keil static int
534707b2ce6SKarsten Keil setvolume(struct w6692_ch *wch, int mic, struct sk_buff *skb)
535707b2ce6SKarsten Keil {
536707b2ce6SKarsten Keil 	struct w6692_hw *card = wch->bch.hw;
537707b2ce6SKarsten Keil 	u16 *vol = (u16 *)skb->data;
538707b2ce6SKarsten Keil 	u8 val;
539707b2ce6SKarsten Keil 
540707b2ce6SKarsten Keil 	if ((!(card->fmask & pots)) ||
541707b2ce6SKarsten Keil 	    !test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
542707b2ce6SKarsten Keil 		return -ENODEV;
543707b2ce6SKarsten Keil 	if (skb->len < 2)
544707b2ce6SKarsten Keil 		return -EINVAL;
545707b2ce6SKarsten Keil 	if (*vol > 7)
546707b2ce6SKarsten Keil 		return -EINVAL;
547707b2ce6SKarsten Keil 	val = *vol & 7;
548707b2ce6SKarsten Keil 	val = 7 - val;
549707b2ce6SKarsten Keil 	if (mic) {
550707b2ce6SKarsten Keil 		val <<= 3;
551707b2ce6SKarsten Keil 		card->xaddr &= 0xc7;
552707b2ce6SKarsten Keil 	} else {
553707b2ce6SKarsten Keil 		card->xaddr &= 0xf8;
554707b2ce6SKarsten Keil 	}
555707b2ce6SKarsten Keil 	card->xaddr |= val;
556707b2ce6SKarsten Keil 	WriteW6692(card, W_XADDR, card->xaddr);
557707b2ce6SKarsten Keil 	return 0;
558707b2ce6SKarsten Keil }
559707b2ce6SKarsten Keil 
560707b2ce6SKarsten Keil static int
561707b2ce6SKarsten Keil enable_pots(struct w6692_ch *wch)
562707b2ce6SKarsten Keil {
563707b2ce6SKarsten Keil 	struct w6692_hw *card = wch->bch.hw;
564707b2ce6SKarsten Keil 
565707b2ce6SKarsten Keil 	if ((!(card->fmask & pots)) ||
566707b2ce6SKarsten Keil 	    !test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
567707b2ce6SKarsten Keil 		return -ENODEV;
568707b2ce6SKarsten Keil 	wch->b_mode |= W_B_MODE_EPCM | W_B_MODE_BSW0;
569707b2ce6SKarsten Keil 	WriteW6692B(wch, W_B_MODE, wch->b_mode);
570707b2ce6SKarsten Keil 	WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_XRST);
571707b2ce6SKarsten Keil 	card->pctl |= ((wch->bch.nr & 2) ? W_PCTL_PCX : 0);
572707b2ce6SKarsten Keil 	WriteW6692(card, W_PCTL, card->pctl);
573707b2ce6SKarsten Keil 	return 0;
574707b2ce6SKarsten Keil }
575*3e598177SJiri Slaby #endif
576707b2ce6SKarsten Keil 
577707b2ce6SKarsten Keil static int
578707b2ce6SKarsten Keil disable_pots(struct w6692_ch *wch)
579707b2ce6SKarsten Keil {
580707b2ce6SKarsten Keil 	struct w6692_hw *card = wch->bch.hw;
581707b2ce6SKarsten Keil 
582707b2ce6SKarsten Keil 	if (!(card->fmask & pots))
583707b2ce6SKarsten Keil 		return -ENODEV;
584707b2ce6SKarsten Keil 	wch->b_mode &= ~(W_B_MODE_EPCM | W_B_MODE_BSW0);
585707b2ce6SKarsten Keil 	WriteW6692B(wch, W_B_MODE, wch->b_mode);
586707b2ce6SKarsten Keil 	WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_RACT |
587707b2ce6SKarsten Keil 		W_B_CMDR_XRST);
588707b2ce6SKarsten Keil 	return 0;
589707b2ce6SKarsten Keil }
590707b2ce6SKarsten Keil 
591707b2ce6SKarsten Keil static int
592707b2ce6SKarsten Keil w6692_mode(struct w6692_ch *wch, u32 pr)
593707b2ce6SKarsten Keil {
594707b2ce6SKarsten Keil 	struct w6692_hw	*card;
595707b2ce6SKarsten Keil 
596707b2ce6SKarsten Keil 	card = wch->bch.hw;
597707b2ce6SKarsten Keil 	pr_debug("%s: B%d protocol %x-->%x\n", card->name,
598707b2ce6SKarsten Keil 		wch->bch.nr, wch->bch.state, pr);
599707b2ce6SKarsten Keil 	switch (pr) {
600707b2ce6SKarsten Keil 	case ISDN_P_NONE:
601707b2ce6SKarsten Keil 		if ((card->fmask & pots) && (wch->b_mode & W_B_MODE_EPCM))
602707b2ce6SKarsten Keil 			disable_pots(wch);
603707b2ce6SKarsten Keil 		wch->b_mode = 0;
604707b2ce6SKarsten Keil 		mISDN_clear_bchannel(&wch->bch);
605707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_MODE, wch->b_mode);
606707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_XRST);
607707b2ce6SKarsten Keil 		test_and_clear_bit(FLG_HDLC, &wch->bch.Flags);
608707b2ce6SKarsten Keil 		test_and_clear_bit(FLG_TRANSPARENT, &wch->bch.Flags);
609707b2ce6SKarsten Keil 		break;
610707b2ce6SKarsten Keil 	case ISDN_P_B_RAW:
611707b2ce6SKarsten Keil 		wch->b_mode = W_B_MODE_MMS;
612707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_MODE, wch->b_mode);
613707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_EXIM, 0);
614707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_RACT |
615707b2ce6SKarsten Keil 			W_B_CMDR_XRST);
616707b2ce6SKarsten Keil 		test_and_set_bit(FLG_TRANSPARENT, &wch->bch.Flags);
617707b2ce6SKarsten Keil 		break;
618707b2ce6SKarsten Keil 	case ISDN_P_B_HDLC:
619707b2ce6SKarsten Keil 		wch->b_mode = W_B_MODE_ITF;
620707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_MODE, wch->b_mode);
621707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_ADM1, 0xff);
622707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_ADM2, 0xff);
623707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_EXIM, 0);
624707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_RACT |
625707b2ce6SKarsten Keil 			W_B_CMDR_XRST);
626707b2ce6SKarsten Keil 		test_and_set_bit(FLG_HDLC, &wch->bch.Flags);
627707b2ce6SKarsten Keil 		break;
628707b2ce6SKarsten Keil 	default:
629707b2ce6SKarsten Keil 		pr_info("%s: protocol %x not known\n", card->name, pr);
630707b2ce6SKarsten Keil 		return -ENOPROTOOPT;
631707b2ce6SKarsten Keil 	}
632707b2ce6SKarsten Keil 	wch->bch.state = pr;
633707b2ce6SKarsten Keil 	return 0;
634707b2ce6SKarsten Keil }
635707b2ce6SKarsten Keil 
636707b2ce6SKarsten Keil static void
637707b2ce6SKarsten Keil send_next(struct w6692_ch *wch)
638707b2ce6SKarsten Keil {
639707b2ce6SKarsten Keil 	if (wch->bch.tx_skb && wch->bch.tx_idx < wch->bch.tx_skb->len)
640707b2ce6SKarsten Keil 		W6692_fill_Bfifo(wch);
641707b2ce6SKarsten Keil 	else {
642707b2ce6SKarsten Keil 		if (wch->bch.tx_skb) {
643707b2ce6SKarsten Keil 			/* send confirm, on trans, free on hdlc. */
644707b2ce6SKarsten Keil 			if (test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
645707b2ce6SKarsten Keil 				confirm_Bsend(&wch->bch);
646707b2ce6SKarsten Keil 			dev_kfree_skb(wch->bch.tx_skb);
647707b2ce6SKarsten Keil 		}
648707b2ce6SKarsten Keil 		if (get_next_bframe(&wch->bch))
649707b2ce6SKarsten Keil 			W6692_fill_Bfifo(wch);
650707b2ce6SKarsten Keil 	}
651707b2ce6SKarsten Keil }
652707b2ce6SKarsten Keil 
653707b2ce6SKarsten Keil static void
654707b2ce6SKarsten Keil W6692B_interrupt(struct w6692_hw *card, int ch)
655707b2ce6SKarsten Keil {
656707b2ce6SKarsten Keil 	struct w6692_ch	*wch = &card->bc[ch];
657707b2ce6SKarsten Keil 	int		count;
658707b2ce6SKarsten Keil 	u8		stat, star = 0;
659707b2ce6SKarsten Keil 
660707b2ce6SKarsten Keil 	stat = ReadW6692B(wch, W_B_EXIR);
661707b2ce6SKarsten Keil 	pr_debug("%s: B%d EXIR %02x\n", card->name, wch->bch.nr, stat);
662707b2ce6SKarsten Keil 	if (stat & W_B_EXI_RME) {
663707b2ce6SKarsten Keil 		star = ReadW6692B(wch, W_B_STAR);
664707b2ce6SKarsten Keil 		if (star & (W_B_STAR_RDOV | W_B_STAR_CRCE | W_B_STAR_RMB)) {
665707b2ce6SKarsten Keil 			if ((star & W_B_STAR_RDOV) &&
666707b2ce6SKarsten Keil 			    test_bit(FLG_ACTIVE, &wch->bch.Flags)) {
667707b2ce6SKarsten Keil 				pr_debug("%s: B%d RDOV proto=%x\n", card->name,
668707b2ce6SKarsten Keil 					wch->bch.nr, wch->bch.state);
669707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
670707b2ce6SKarsten Keil 				wch->bch.err_rdo++;
671707b2ce6SKarsten Keil #endif
672707b2ce6SKarsten Keil 			}
673707b2ce6SKarsten Keil 			if (test_bit(FLG_HDLC, &wch->bch.Flags)) {
674707b2ce6SKarsten Keil 				if (star & W_B_STAR_CRCE) {
675707b2ce6SKarsten Keil 					pr_debug("%s: B%d CRC error\n",
676707b2ce6SKarsten Keil 						card->name, wch->bch.nr);
677707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
678707b2ce6SKarsten Keil 					wch->bch.err_crc++;
679707b2ce6SKarsten Keil #endif
680707b2ce6SKarsten Keil 				}
681707b2ce6SKarsten Keil 				if (star & W_B_STAR_RMB) {
682707b2ce6SKarsten Keil 					pr_debug("%s: B%d message abort\n",
683707b2ce6SKarsten Keil 						card->name, wch->bch.nr);
684707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
685707b2ce6SKarsten Keil 					wch->bch.err_inv++;
686707b2ce6SKarsten Keil #endif
687707b2ce6SKarsten Keil 				}
688707b2ce6SKarsten Keil 			}
689707b2ce6SKarsten Keil 			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK |
690707b2ce6SKarsten Keil 				W_B_CMDR_RRST | W_B_CMDR_RACT);
691707b2ce6SKarsten Keil 			if (wch->bch.rx_skb)
692707b2ce6SKarsten Keil 				skb_trim(wch->bch.rx_skb, 0);
693707b2ce6SKarsten Keil 		} else {
694707b2ce6SKarsten Keil 			count = ReadW6692B(wch, W_B_RBCL) &
695707b2ce6SKarsten Keil 				(W_B_FIFO_THRESH - 1);
696707b2ce6SKarsten Keil 			if (count == 0)
697707b2ce6SKarsten Keil 				count = W_B_FIFO_THRESH;
698707b2ce6SKarsten Keil 			W6692_empty_Bfifo(wch, count);
699707b2ce6SKarsten Keil 			recv_Bchannel(&wch->bch, 0);
700707b2ce6SKarsten Keil 		}
701707b2ce6SKarsten Keil 	}
702707b2ce6SKarsten Keil 	if (stat & W_B_EXI_RMR) {
703707b2ce6SKarsten Keil 		if (!(stat & W_B_EXI_RME))
704707b2ce6SKarsten Keil 			star = ReadW6692B(wch, W_B_STAR);
705707b2ce6SKarsten Keil 		if (star & W_B_STAR_RDOV) {
706707b2ce6SKarsten Keil 			pr_debug("%s: B%d RDOV proto=%x\n", card->name,
707707b2ce6SKarsten Keil 				wch->bch.nr, wch->bch.state);
708707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
709707b2ce6SKarsten Keil 			wch->bch.err_rdo++;
710707b2ce6SKarsten Keil #endif
711707b2ce6SKarsten Keil 			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK |
712707b2ce6SKarsten Keil 				W_B_CMDR_RRST | W_B_CMDR_RACT);
713707b2ce6SKarsten Keil 		} else {
714707b2ce6SKarsten Keil 			W6692_empty_Bfifo(wch, W_B_FIFO_THRESH);
715707b2ce6SKarsten Keil 			if (test_bit(FLG_TRANSPARENT, &wch->bch.Flags) &&
716707b2ce6SKarsten Keil 			    wch->bch.rx_skb && (wch->bch.rx_skb->len > 0))
717707b2ce6SKarsten Keil 				recv_Bchannel(&wch->bch, 0);
718707b2ce6SKarsten Keil 		}
719707b2ce6SKarsten Keil 	}
720707b2ce6SKarsten Keil 	if (stat & W_B_EXI_RDOV) {
721707b2ce6SKarsten Keil 		/* only if it is not handled yet */
722707b2ce6SKarsten Keil 		if (!(star & W_B_STAR_RDOV)) {
723707b2ce6SKarsten Keil 			pr_debug("%s: B%d RDOV IRQ proto=%x\n", card->name,
724707b2ce6SKarsten Keil 				wch->bch.nr, wch->bch.state);
725707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
726707b2ce6SKarsten Keil 			wch->bch.err_rdo++;
727707b2ce6SKarsten Keil #endif
728707b2ce6SKarsten Keil 			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_RACK |
729707b2ce6SKarsten Keil 				W_B_CMDR_RRST | W_B_CMDR_RACT);
730707b2ce6SKarsten Keil 		}
731707b2ce6SKarsten Keil 	}
732707b2ce6SKarsten Keil 	if (stat & W_B_EXI_XFR) {
733707b2ce6SKarsten Keil 		if (!(stat & (W_B_EXI_RME | W_B_EXI_RMR))) {
734707b2ce6SKarsten Keil 			star = ReadW6692B(wch, W_B_STAR);
735707b2ce6SKarsten Keil 			pr_debug("%s: B%d star %02x\n", card->name,
736707b2ce6SKarsten Keil 				wch->bch.nr, star);
737707b2ce6SKarsten Keil 		}
738707b2ce6SKarsten Keil 		if (star & W_B_STAR_XDOW) {
739707b2ce6SKarsten Keil 			pr_debug("%s: B%d XDOW proto=%x\n", card->name,
740707b2ce6SKarsten Keil 				wch->bch.nr, wch->bch.state);
741707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
742707b2ce6SKarsten Keil 			wch->bch.err_xdu++;
743707b2ce6SKarsten Keil #endif
744707b2ce6SKarsten Keil 			WriteW6692B(wch, W_B_CMDR, W_B_CMDR_XRST |
745707b2ce6SKarsten Keil 				W_B_CMDR_RACT);
746707b2ce6SKarsten Keil 			/* resend */
747707b2ce6SKarsten Keil 			if (wch->bch.tx_skb) {
748707b2ce6SKarsten Keil 				if (!test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
749707b2ce6SKarsten Keil 					wch->bch.tx_idx = 0;
750707b2ce6SKarsten Keil 			}
751707b2ce6SKarsten Keil 		}
752707b2ce6SKarsten Keil 		send_next(wch);
753707b2ce6SKarsten Keil 		if (stat & W_B_EXI_XDUN)
754707b2ce6SKarsten Keil 			return; /* handle XDOW only once */
755707b2ce6SKarsten Keil 	}
756707b2ce6SKarsten Keil 	if (stat & W_B_EXI_XDUN) {
757707b2ce6SKarsten Keil 		pr_debug("%s: B%d XDUN proto=%x\n", card->name,
758707b2ce6SKarsten Keil 			wch->bch.nr, wch->bch.state);
759707b2ce6SKarsten Keil #ifdef ERROR_STATISTIC
760707b2ce6SKarsten Keil 		wch->bch.err_xdu++;
761707b2ce6SKarsten Keil #endif
762707b2ce6SKarsten Keil 		WriteW6692B(wch, W_B_CMDR, W_B_CMDR_XRST | W_B_CMDR_RACT);
763707b2ce6SKarsten Keil 		/* resend */
764707b2ce6SKarsten Keil 		if (wch->bch.tx_skb) {
765707b2ce6SKarsten Keil 			if (!test_bit(FLG_TRANSPARENT, &wch->bch.Flags))
766707b2ce6SKarsten Keil 				wch->bch.tx_idx = 0;
767707b2ce6SKarsten Keil 		}
768707b2ce6SKarsten Keil 		send_next(wch);
769707b2ce6SKarsten Keil 	}
770707b2ce6SKarsten Keil }
771707b2ce6SKarsten Keil 
772707b2ce6SKarsten Keil static irqreturn_t
773707b2ce6SKarsten Keil w6692_irq(int intno, void *dev_id)
774707b2ce6SKarsten Keil {
775707b2ce6SKarsten Keil 	struct w6692_hw	*card = dev_id;
776707b2ce6SKarsten Keil 	u8		ista;
777707b2ce6SKarsten Keil 
778707b2ce6SKarsten Keil 	spin_lock(&card->lock);
779707b2ce6SKarsten Keil 	ista = ReadW6692(card, W_ISTA);
780707b2ce6SKarsten Keil 	if ((ista | card->imask) == card->imask) {
781707b2ce6SKarsten Keil 		/* possible a shared  IRQ reqest */
782707b2ce6SKarsten Keil 		spin_unlock(&card->lock);
783707b2ce6SKarsten Keil 		return IRQ_NONE;
784707b2ce6SKarsten Keil 	}
785707b2ce6SKarsten Keil 	card->irqcnt++;
786707b2ce6SKarsten Keil 	pr_debug("%s: ista %02x\n", card->name, ista);
787707b2ce6SKarsten Keil 	ista &= ~card->imask;
788707b2ce6SKarsten Keil 	if (ista & W_INT_B1_EXI)
789707b2ce6SKarsten Keil 		W6692B_interrupt(card, 0);
790707b2ce6SKarsten Keil 	if (ista & W_INT_B2_EXI)
791707b2ce6SKarsten Keil 		W6692B_interrupt(card, 1);
792707b2ce6SKarsten Keil 	if (ista & W_INT_D_RME)
793707b2ce6SKarsten Keil 		handle_rxD(card);
794707b2ce6SKarsten Keil 	if (ista & W_INT_D_RMR)
795707b2ce6SKarsten Keil 		W6692_empty_Dfifo(card, W_D_FIFO_THRESH);
796707b2ce6SKarsten Keil 	if (ista & W_INT_D_XFR)
797707b2ce6SKarsten Keil 		handle_txD(card);
798707b2ce6SKarsten Keil 	if (ista & W_INT_D_EXI)
799707b2ce6SKarsten Keil 		handle_statusD(card);
800707b2ce6SKarsten Keil 	if (ista & (W_INT_XINT0 | W_INT_XINT1)) /* XINT0/1 - never */
801707b2ce6SKarsten Keil 		pr_debug("%s: W6692 spurious XINT!\n", card->name);
802707b2ce6SKarsten Keil /* End IRQ Handler */
803707b2ce6SKarsten Keil 	spin_unlock(&card->lock);
804707b2ce6SKarsten Keil 	return IRQ_HANDLED;
805707b2ce6SKarsten Keil }
806707b2ce6SKarsten Keil 
807707b2ce6SKarsten Keil static void
808707b2ce6SKarsten Keil dbusy_timer_handler(struct dchannel *dch)
809707b2ce6SKarsten Keil {
810707b2ce6SKarsten Keil 	struct w6692_hw	*card = dch->hw;
811707b2ce6SKarsten Keil 	int		rbch, star;
812707b2ce6SKarsten Keil 	u_long		flags;
813707b2ce6SKarsten Keil 
814707b2ce6SKarsten Keil 	if (test_bit(FLG_BUSY_TIMER, &dch->Flags)) {
815707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
816707b2ce6SKarsten Keil 		rbch = ReadW6692(card, W_D_RBCH);
817707b2ce6SKarsten Keil 		star = ReadW6692(card, W_D_STAR);
818707b2ce6SKarsten Keil 		pr_debug("%s: D-Channel Busy RBCH %02x STAR %02x\n",
819707b2ce6SKarsten Keil 			card->name, rbch, star);
820707b2ce6SKarsten Keil 		if (star & W_D_STAR_XBZ)	/* D-Channel Busy */
821707b2ce6SKarsten Keil 			test_and_set_bit(FLG_L1_BUSY, &dch->Flags);
822707b2ce6SKarsten Keil 		else {
823707b2ce6SKarsten Keil 			/* discard frame; reset transceiver */
824707b2ce6SKarsten Keil 			test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags);
825707b2ce6SKarsten Keil 			if (dch->tx_idx)
826707b2ce6SKarsten Keil 				dch->tx_idx = 0;
827707b2ce6SKarsten Keil 			else
828707b2ce6SKarsten Keil 				pr_info("%s: W6692 D-Channel Busy no tx_idx\n",
829707b2ce6SKarsten Keil 					card->name);
830707b2ce6SKarsten Keil 			/* Transmitter reset */
831707b2ce6SKarsten Keil 			WriteW6692(card, W_D_CMDR, W_D_CMDR_XRST);
832707b2ce6SKarsten Keil 		}
833707b2ce6SKarsten Keil 		spin_unlock_irqrestore(&card->lock, flags);
834707b2ce6SKarsten Keil 	}
835707b2ce6SKarsten Keil }
836707b2ce6SKarsten Keil 
837707b2ce6SKarsten Keil void initW6692(struct w6692_hw *card)
838707b2ce6SKarsten Keil {
839707b2ce6SKarsten Keil 	u8	val;
840707b2ce6SKarsten Keil 
841707b2ce6SKarsten Keil 	card->dch.timer.function = (void *)dbusy_timer_handler;
842707b2ce6SKarsten Keil 	card->dch.timer.data = (u_long)&card->dch;
843707b2ce6SKarsten Keil 	init_timer(&card->dch.timer);
844707b2ce6SKarsten Keil 	w6692_mode(&card->bc[0], ISDN_P_NONE);
845707b2ce6SKarsten Keil 	w6692_mode(&card->bc[1], ISDN_P_NONE);
846707b2ce6SKarsten Keil 	WriteW6692(card, W_D_CTL, 0x00);
847707b2ce6SKarsten Keil 	disable_hwirq(card);
848707b2ce6SKarsten Keil 	WriteW6692(card, W_D_SAM, 0xff);
849707b2ce6SKarsten Keil 	WriteW6692(card, W_D_TAM, 0xff);
850707b2ce6SKarsten Keil 	WriteW6692(card, W_D_MODE, W_D_MODE_RACT);
851707b2ce6SKarsten Keil 	card->state = W_L1CMD_RST;
852707b2ce6SKarsten Keil 	ph_command(card, W_L1CMD_RST);
853707b2ce6SKarsten Keil 	ph_command(card, W_L1CMD_ECK);
854707b2ce6SKarsten Keil 	/* enable all IRQ but extern */
855707b2ce6SKarsten Keil 	card->imask = 0x18;
856707b2ce6SKarsten Keil 	WriteW6692(card, W_D_EXIM, 0x00);
857707b2ce6SKarsten Keil 	WriteW6692B(&card->bc[0], W_B_EXIM, 0);
858707b2ce6SKarsten Keil 	WriteW6692B(&card->bc[1], W_B_EXIM, 0);
859707b2ce6SKarsten Keil 	/* Reset D-chan receiver and transmitter */
860707b2ce6SKarsten Keil 	WriteW6692(card, W_D_CMDR, W_D_CMDR_RRST | W_D_CMDR_XRST);
861707b2ce6SKarsten Keil 	/* Reset B-chan receiver and transmitter */
862707b2ce6SKarsten Keil 	WriteW6692B(&card->bc[0], W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_XRST);
863707b2ce6SKarsten Keil 	WriteW6692B(&card->bc[1], W_B_CMDR, W_B_CMDR_RRST | W_B_CMDR_XRST);
864707b2ce6SKarsten Keil 	/* enable peripheral */
865707b2ce6SKarsten Keil 	if (card->subtype == W6692_USR) {
866707b2ce6SKarsten Keil 		/* seems that USR implemented some power control features
867707b2ce6SKarsten Keil 		 * Pin 79 is connected to the oscilator circuit so we
868707b2ce6SKarsten Keil 		 * have to handle it here
869707b2ce6SKarsten Keil 		 */
870707b2ce6SKarsten Keil 		card->pctl = 0x80;
871707b2ce6SKarsten Keil 		card->xdata = 0;
872707b2ce6SKarsten Keil 		WriteW6692(card, W_PCTL, card->pctl);
873707b2ce6SKarsten Keil 		WriteW6692(card, W_XDATA, card->xdata);
874707b2ce6SKarsten Keil 	} else {
875707b2ce6SKarsten Keil 		card->pctl = W_PCTL_OE5 | W_PCTL_OE4 | W_PCTL_OE2 |
876707b2ce6SKarsten Keil 			W_PCTL_OE1 | W_PCTL_OE0;
877707b2ce6SKarsten Keil 		card->xaddr = 0x00;/* all sw off */
878707b2ce6SKarsten Keil 		if (card->fmask & pots)
879707b2ce6SKarsten Keil 			card->xdata |= 0x06;	/*  POWER UP/ LED OFF / ALAW */
880707b2ce6SKarsten Keil 		if (card->fmask & led)
881707b2ce6SKarsten Keil 			card->xdata |= 0x04;	/* LED OFF */
882707b2ce6SKarsten Keil 		if ((card->fmask & pots) || (card->fmask & led)) {
883707b2ce6SKarsten Keil 			WriteW6692(card, W_PCTL, card->pctl);
884707b2ce6SKarsten Keil 			WriteW6692(card, W_XADDR, card->xaddr);
885707b2ce6SKarsten Keil 			WriteW6692(card, W_XDATA, card->xdata);
886707b2ce6SKarsten Keil 			val = ReadW6692(card, W_XADDR);
887707b2ce6SKarsten Keil 			if (debug & DEBUG_HW)
888707b2ce6SKarsten Keil 				pr_notice("%s: W_XADDR=%02x\n",
889707b2ce6SKarsten Keil 					card->name, val);
890707b2ce6SKarsten Keil 		}
891707b2ce6SKarsten Keil 	}
892707b2ce6SKarsten Keil }
893707b2ce6SKarsten Keil 
894707b2ce6SKarsten Keil static void
895707b2ce6SKarsten Keil reset_w6692(struct w6692_hw *card)
896707b2ce6SKarsten Keil {
897707b2ce6SKarsten Keil 	WriteW6692(card, W_D_CTL, W_D_CTL_SRST);
898707b2ce6SKarsten Keil 	mdelay(10);
899707b2ce6SKarsten Keil 	WriteW6692(card, W_D_CTL, 0);
900707b2ce6SKarsten Keil }
901707b2ce6SKarsten Keil 
902707b2ce6SKarsten Keil static int
903707b2ce6SKarsten Keil init_card(struct w6692_hw *card)
904707b2ce6SKarsten Keil {
905707b2ce6SKarsten Keil 	int	cnt = 3;
906707b2ce6SKarsten Keil 	u_long	flags;
907707b2ce6SKarsten Keil 
908707b2ce6SKarsten Keil 	spin_lock_irqsave(&card->lock, flags);
909707b2ce6SKarsten Keil 	disable_hwirq(card);
910707b2ce6SKarsten Keil 	spin_unlock_irqrestore(&card->lock, flags);
911707b2ce6SKarsten Keil 	if (request_irq(card->irq, w6692_irq, IRQF_SHARED, card->name, card)) {
912707b2ce6SKarsten Keil 		pr_info("%s: couldn't get interrupt %d\n", card->name,
913707b2ce6SKarsten Keil 			card->irq);
914707b2ce6SKarsten Keil 		return -EIO;
915707b2ce6SKarsten Keil 	}
916707b2ce6SKarsten Keil 	while (cnt--) {
917707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
918707b2ce6SKarsten Keil 		initW6692(card);
919707b2ce6SKarsten Keil 		enable_hwirq(card);
920707b2ce6SKarsten Keil 		spin_unlock_irqrestore(&card->lock, flags);
921707b2ce6SKarsten Keil 		/* Timeout 10ms */
922707b2ce6SKarsten Keil 		msleep_interruptible(10);
923707b2ce6SKarsten Keil 		if (debug & DEBUG_HW)
924707b2ce6SKarsten Keil 			pr_notice("%s: IRQ %d count %d\n", card->name,
925707b2ce6SKarsten Keil 				card->irq, card->irqcnt);
926707b2ce6SKarsten Keil 		if (!card->irqcnt) {
927707b2ce6SKarsten Keil 			pr_info("%s: IRQ(%d) getting no IRQs during init %d\n",
928707b2ce6SKarsten Keil 				card->name, card->irq, 3 - cnt);
929707b2ce6SKarsten Keil 			reset_w6692(card);
930707b2ce6SKarsten Keil 		} else
931707b2ce6SKarsten Keil 			return 0;
932707b2ce6SKarsten Keil 	}
933707b2ce6SKarsten Keil 	free_irq(card->irq, card);
934707b2ce6SKarsten Keil 	return -EIO;
935707b2ce6SKarsten Keil }
936707b2ce6SKarsten Keil 
937707b2ce6SKarsten Keil static int
938707b2ce6SKarsten Keil w6692_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
939707b2ce6SKarsten Keil {
940707b2ce6SKarsten Keil 	struct bchannel *bch = container_of(ch, struct bchannel, ch);
941707b2ce6SKarsten Keil 	struct w6692_ch	*bc = container_of(bch, struct w6692_ch, bch);
942707b2ce6SKarsten Keil 	struct w6692_hw *card = bch->hw;
943707b2ce6SKarsten Keil 	int ret = -EINVAL;
944707b2ce6SKarsten Keil 	struct mISDNhead *hh = mISDN_HEAD_P(skb);
945707b2ce6SKarsten Keil 	u32 id;
946707b2ce6SKarsten Keil 	u_long flags;
947707b2ce6SKarsten Keil 
948707b2ce6SKarsten Keil 	switch (hh->prim) {
949707b2ce6SKarsten Keil 	case PH_DATA_REQ:
950707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
951707b2ce6SKarsten Keil 		ret = bchannel_senddata(bch, skb);
952707b2ce6SKarsten Keil 		if (ret > 0) { /* direct TX */
953707b2ce6SKarsten Keil 			id = hh->id; /* skb can be freed */
954707b2ce6SKarsten Keil 			ret = 0;
955707b2ce6SKarsten Keil 			W6692_fill_Bfifo(bc);
956707b2ce6SKarsten Keil 			spin_unlock_irqrestore(&card->lock, flags);
957707b2ce6SKarsten Keil 			if (!test_bit(FLG_TRANSPARENT, &bch->Flags))
958707b2ce6SKarsten Keil 				queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
959707b2ce6SKarsten Keil 		} else
960707b2ce6SKarsten Keil 			spin_unlock_irqrestore(&card->lock, flags);
961707b2ce6SKarsten Keil 		return ret;
962707b2ce6SKarsten Keil 	case PH_ACTIVATE_REQ:
963707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
964707b2ce6SKarsten Keil 		if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags))
965707b2ce6SKarsten Keil 			ret = w6692_mode(bc, ch->protocol);
966707b2ce6SKarsten Keil 		else
967707b2ce6SKarsten Keil 			ret = 0;
968707b2ce6SKarsten Keil 		spin_unlock_irqrestore(&card->lock, flags);
969707b2ce6SKarsten Keil 		if (!ret)
970707b2ce6SKarsten Keil 			_queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
971707b2ce6SKarsten Keil 				NULL, GFP_KERNEL);
972707b2ce6SKarsten Keil 		break;
973707b2ce6SKarsten Keil 	case PH_DEACTIVATE_REQ:
974707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
975707b2ce6SKarsten Keil 		mISDN_clear_bchannel(bch);
976707b2ce6SKarsten Keil 		w6692_mode(bc, ISDN_P_NONE);
977707b2ce6SKarsten Keil 		spin_unlock_irqrestore(&card->lock, flags);
978707b2ce6SKarsten Keil 		_queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY, 0,
979707b2ce6SKarsten Keil 			NULL, GFP_KERNEL);
980707b2ce6SKarsten Keil 		ret = 0;
981707b2ce6SKarsten Keil 		break;
982707b2ce6SKarsten Keil 	default:
983707b2ce6SKarsten Keil 		pr_info("%s: %s unknown prim(%x,%x)\n",
984707b2ce6SKarsten Keil 			card->name, __func__, hh->prim, hh->id);
985707b2ce6SKarsten Keil 		ret = -EINVAL;
986707b2ce6SKarsten Keil 	}
987707b2ce6SKarsten Keil 	if (!ret)
988707b2ce6SKarsten Keil 		dev_kfree_skb(skb);
989707b2ce6SKarsten Keil 	return ret;
990707b2ce6SKarsten Keil }
991707b2ce6SKarsten Keil 
992707b2ce6SKarsten Keil static int
993707b2ce6SKarsten Keil channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
994707b2ce6SKarsten Keil {
995707b2ce6SKarsten Keil 	int	ret = 0;
996707b2ce6SKarsten Keil 
997707b2ce6SKarsten Keil 	switch (cq->op) {
998707b2ce6SKarsten Keil 	case MISDN_CTRL_GETOP:
999707b2ce6SKarsten Keil 		cq->op = 0;
1000707b2ce6SKarsten Keil 		break;
1001707b2ce6SKarsten Keil 	/* Nothing implemented yet */
1002707b2ce6SKarsten Keil 	case MISDN_CTRL_FILL_EMPTY:
1003707b2ce6SKarsten Keil 	default:
1004707b2ce6SKarsten Keil 		pr_info("%s: unknown Op %x\n", __func__, cq->op);
1005707b2ce6SKarsten Keil 		ret = -EINVAL;
1006707b2ce6SKarsten Keil 		break;
1007707b2ce6SKarsten Keil 	}
1008707b2ce6SKarsten Keil 	return ret;
1009707b2ce6SKarsten Keil }
1010707b2ce6SKarsten Keil 
1011707b2ce6SKarsten Keil static int
1012707b2ce6SKarsten Keil open_bchannel(struct w6692_hw *card, struct channel_req *rq)
1013707b2ce6SKarsten Keil {
1014707b2ce6SKarsten Keil 	struct bchannel *bch;
1015707b2ce6SKarsten Keil 
1016707b2ce6SKarsten Keil 	if (rq->adr.channel > 2)
1017707b2ce6SKarsten Keil 		return -EINVAL;
1018707b2ce6SKarsten Keil 	if (rq->protocol == ISDN_P_NONE)
1019707b2ce6SKarsten Keil 		return -EINVAL;
1020707b2ce6SKarsten Keil 	bch = &card->bc[rq->adr.channel - 1].bch;
1021707b2ce6SKarsten Keil 	if (test_and_set_bit(FLG_OPEN, &bch->Flags))
1022707b2ce6SKarsten Keil 		return -EBUSY; /* b-channel can be only open once */
1023707b2ce6SKarsten Keil 	test_and_clear_bit(FLG_FILLEMPTY, &bch->Flags);
1024707b2ce6SKarsten Keil 	bch->ch.protocol = rq->protocol;
1025707b2ce6SKarsten Keil 	rq->ch = &bch->ch;
1026707b2ce6SKarsten Keil 	return 0;
1027707b2ce6SKarsten Keil }
1028707b2ce6SKarsten Keil 
1029707b2ce6SKarsten Keil static int
1030707b2ce6SKarsten Keil channel_ctrl(struct w6692_hw *card, struct mISDN_ctrl_req *cq)
1031707b2ce6SKarsten Keil {
1032707b2ce6SKarsten Keil 	int	ret = 0;
1033707b2ce6SKarsten Keil 
1034707b2ce6SKarsten Keil 	switch (cq->op) {
1035707b2ce6SKarsten Keil 	case MISDN_CTRL_GETOP:
1036707b2ce6SKarsten Keil 		cq->op = 0;
1037707b2ce6SKarsten Keil 		break;
1038707b2ce6SKarsten Keil 	default:
1039707b2ce6SKarsten Keil 		pr_info("%s: unknown CTRL OP %x\n", card->name, cq->op);
1040707b2ce6SKarsten Keil 		ret = -EINVAL;
1041707b2ce6SKarsten Keil 		break;
1042707b2ce6SKarsten Keil 	}
1043707b2ce6SKarsten Keil 	return ret;
1044707b2ce6SKarsten Keil }
1045707b2ce6SKarsten Keil 
1046707b2ce6SKarsten Keil static int
1047707b2ce6SKarsten Keil w6692_bctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
1048707b2ce6SKarsten Keil {
1049707b2ce6SKarsten Keil 	struct bchannel *bch = container_of(ch, struct bchannel, ch);
1050707b2ce6SKarsten Keil 	struct w6692_ch *bc = container_of(bch, struct w6692_ch, bch);
1051707b2ce6SKarsten Keil 	struct w6692_hw *card = bch->hw;
1052707b2ce6SKarsten Keil 	int ret = -EINVAL;
1053707b2ce6SKarsten Keil 	u_long flags;
1054707b2ce6SKarsten Keil 
1055707b2ce6SKarsten Keil 	pr_debug("%s: %s cmd:%x %p\n", card->name, __func__, cmd, arg);
1056707b2ce6SKarsten Keil 	switch (cmd) {
1057707b2ce6SKarsten Keil 	case CLOSE_CHANNEL:
1058707b2ce6SKarsten Keil 		test_and_clear_bit(FLG_OPEN, &bch->Flags);
1059707b2ce6SKarsten Keil 		if (test_bit(FLG_ACTIVE, &bch->Flags)) {
1060707b2ce6SKarsten Keil 			spin_lock_irqsave(&card->lock, flags);
1061707b2ce6SKarsten Keil 			mISDN_freebchannel(bch);
1062707b2ce6SKarsten Keil 			w6692_mode(bc, ISDN_P_NONE);
1063707b2ce6SKarsten Keil 			spin_unlock_irqrestore(&card->lock, flags);
1064707b2ce6SKarsten Keil 		} else {
1065707b2ce6SKarsten Keil 			skb_queue_purge(&bch->rqueue);
1066707b2ce6SKarsten Keil 			bch->rcount = 0;
1067707b2ce6SKarsten Keil 		}
1068707b2ce6SKarsten Keil 		ch->protocol = ISDN_P_NONE;
1069707b2ce6SKarsten Keil 		ch->peer = NULL;
1070707b2ce6SKarsten Keil 		module_put(THIS_MODULE);
1071707b2ce6SKarsten Keil 		ret = 0;
1072707b2ce6SKarsten Keil 		break;
1073707b2ce6SKarsten Keil 	case CONTROL_CHANNEL:
1074707b2ce6SKarsten Keil 		ret = channel_bctrl(bch, arg);
1075707b2ce6SKarsten Keil 		break;
1076707b2ce6SKarsten Keil 	default:
1077707b2ce6SKarsten Keil 		pr_info("%s: %s unknown prim(%x)\n",
1078707b2ce6SKarsten Keil 			card->name, __func__, cmd);
1079707b2ce6SKarsten Keil 	}
1080707b2ce6SKarsten Keil 	return ret;
1081707b2ce6SKarsten Keil }
1082707b2ce6SKarsten Keil 
1083707b2ce6SKarsten Keil static int
1084707b2ce6SKarsten Keil w6692_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
1085707b2ce6SKarsten Keil {
1086707b2ce6SKarsten Keil 	struct mISDNdevice	*dev = container_of(ch, struct mISDNdevice, D);
1087707b2ce6SKarsten Keil 	struct dchannel		*dch = container_of(dev, struct dchannel, dev);
1088707b2ce6SKarsten Keil 	struct w6692_hw		*card = container_of(dch, struct w6692_hw, dch);
1089707b2ce6SKarsten Keil 	int			ret = -EINVAL;
1090707b2ce6SKarsten Keil 	struct mISDNhead	*hh = mISDN_HEAD_P(skb);
1091707b2ce6SKarsten Keil 	u32			id;
1092707b2ce6SKarsten Keil 	u_long			flags;
1093707b2ce6SKarsten Keil 
1094707b2ce6SKarsten Keil 	switch (hh->prim) {
1095707b2ce6SKarsten Keil 	case PH_DATA_REQ:
1096707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
1097707b2ce6SKarsten Keil 		ret = dchannel_senddata(dch, skb);
1098707b2ce6SKarsten Keil 		if (ret > 0) { /* direct TX */
1099707b2ce6SKarsten Keil 			id = hh->id; /* skb can be freed */
1100707b2ce6SKarsten Keil 			W6692_fill_Dfifo(card);
1101707b2ce6SKarsten Keil 			ret = 0;
1102707b2ce6SKarsten Keil 			spin_unlock_irqrestore(&card->lock, flags);
1103707b2ce6SKarsten Keil 			queue_ch_frame(ch, PH_DATA_CNF, id, NULL);
1104707b2ce6SKarsten Keil 		} else
1105707b2ce6SKarsten Keil 			spin_unlock_irqrestore(&card->lock, flags);
1106707b2ce6SKarsten Keil 		return ret;
1107707b2ce6SKarsten Keil 	case PH_ACTIVATE_REQ:
1108707b2ce6SKarsten Keil 		ret = l1_event(dch->l1, hh->prim);
1109707b2ce6SKarsten Keil 		break;
1110707b2ce6SKarsten Keil 	case PH_DEACTIVATE_REQ:
1111707b2ce6SKarsten Keil 		test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
1112707b2ce6SKarsten Keil 		ret = l1_event(dch->l1, hh->prim);
1113707b2ce6SKarsten Keil 		break;
1114707b2ce6SKarsten Keil 	}
1115707b2ce6SKarsten Keil 
1116707b2ce6SKarsten Keil 	if (!ret)
1117707b2ce6SKarsten Keil 		dev_kfree_skb(skb);
1118707b2ce6SKarsten Keil 	return ret;
1119707b2ce6SKarsten Keil }
1120707b2ce6SKarsten Keil 
1121707b2ce6SKarsten Keil static int
1122707b2ce6SKarsten Keil w6692_l1callback(struct dchannel *dch, u32 cmd)
1123707b2ce6SKarsten Keil {
1124707b2ce6SKarsten Keil 	struct w6692_hw *card = container_of(dch, struct w6692_hw, dch);
1125707b2ce6SKarsten Keil 	u_long flags;
1126707b2ce6SKarsten Keil 
1127707b2ce6SKarsten Keil 	pr_debug("%s: cmd(%x) state(%02x)\n", card->name, cmd, card->state);
1128707b2ce6SKarsten Keil 	switch (cmd) {
1129707b2ce6SKarsten Keil 	case INFO3_P8:
1130707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
1131707b2ce6SKarsten Keil 		ph_command(card, W_L1CMD_AR8);
1132707b2ce6SKarsten Keil 		spin_unlock_irqrestore(&card->lock, flags);
1133707b2ce6SKarsten Keil 		break;
1134707b2ce6SKarsten Keil 	case INFO3_P10:
1135707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
1136707b2ce6SKarsten Keil 		ph_command(card, W_L1CMD_AR10);
1137707b2ce6SKarsten Keil 		spin_unlock_irqrestore(&card->lock, flags);
1138707b2ce6SKarsten Keil 		break;
1139707b2ce6SKarsten Keil 	case HW_RESET_REQ:
1140707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
1141707b2ce6SKarsten Keil 		if (card->state != W_L1IND_DRD)
1142707b2ce6SKarsten Keil 			ph_command(card, W_L1CMD_RST);
1143707b2ce6SKarsten Keil 		ph_command(card, W_L1CMD_ECK);
1144707b2ce6SKarsten Keil 		spin_unlock_irqrestore(&card->lock, flags);
1145707b2ce6SKarsten Keil 		break;
1146707b2ce6SKarsten Keil 	case HW_DEACT_REQ:
1147707b2ce6SKarsten Keil 		skb_queue_purge(&dch->squeue);
1148707b2ce6SKarsten Keil 		if (dch->tx_skb) {
1149707b2ce6SKarsten Keil 			dev_kfree_skb(dch->tx_skb);
1150707b2ce6SKarsten Keil 			dch->tx_skb = NULL;
1151707b2ce6SKarsten Keil 		}
1152707b2ce6SKarsten Keil 		dch->tx_idx = 0;
1153707b2ce6SKarsten Keil 		if (dch->rx_skb) {
1154707b2ce6SKarsten Keil 			dev_kfree_skb(dch->rx_skb);
1155707b2ce6SKarsten Keil 			dch->rx_skb = NULL;
1156707b2ce6SKarsten Keil 		}
1157707b2ce6SKarsten Keil 		test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
1158707b2ce6SKarsten Keil 		if (test_and_clear_bit(FLG_BUSY_TIMER, &dch->Flags))
1159707b2ce6SKarsten Keil 			del_timer(&dch->timer);
1160707b2ce6SKarsten Keil 		break;
1161707b2ce6SKarsten Keil 	case HW_POWERUP_REQ:
1162707b2ce6SKarsten Keil 		spin_lock_irqsave(&card->lock, flags);
1163707b2ce6SKarsten Keil 		ph_command(card, W_L1CMD_ECK);
1164707b2ce6SKarsten Keil 		spin_unlock_irqrestore(&card->lock, flags);
1165707b2ce6SKarsten Keil 		break;
1166707b2ce6SKarsten Keil 	case PH_ACTIVATE_IND:
1167707b2ce6SKarsten Keil 		test_and_set_bit(FLG_ACTIVE, &dch->Flags);
1168707b2ce6SKarsten Keil 		_queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
1169707b2ce6SKarsten Keil 			GFP_ATOMIC);
1170707b2ce6SKarsten Keil 		break;
1171707b2ce6SKarsten Keil 	case PH_DEACTIVATE_IND:
1172707b2ce6SKarsten Keil 		test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
1173707b2ce6SKarsten Keil 		_queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
1174707b2ce6SKarsten Keil 			GFP_ATOMIC);
1175707b2ce6SKarsten Keil 		break;
1176707b2ce6SKarsten Keil 	default:
1177707b2ce6SKarsten Keil 		pr_debug("%s: %s unknown command %x\n", card->name,
1178707b2ce6SKarsten Keil 			__func__, cmd);
1179707b2ce6SKarsten Keil 		return -1;
1180707b2ce6SKarsten Keil 	}
1181707b2ce6SKarsten Keil 	return 0;
1182707b2ce6SKarsten Keil }
1183707b2ce6SKarsten Keil 
1184707b2ce6SKarsten Keil static int
1185707b2ce6SKarsten Keil open_dchannel(struct w6692_hw *card, struct channel_req *rq)
1186707b2ce6SKarsten Keil {
1187707b2ce6SKarsten Keil 	pr_debug("%s: %s dev(%d) open from %p\n", card->name, __func__,
1188707b2ce6SKarsten Keil 		card->dch.dev.id, __builtin_return_address(1));
1189707b2ce6SKarsten Keil 	if (rq->protocol != ISDN_P_TE_S0)
1190707b2ce6SKarsten Keil 		return -EINVAL;
1191707b2ce6SKarsten Keil 	if (rq->adr.channel == 1)
1192707b2ce6SKarsten Keil 		/* E-Channel not supported */
1193707b2ce6SKarsten Keil 		return -EINVAL;
1194707b2ce6SKarsten Keil 	rq->ch = &card->dch.dev.D;
1195707b2ce6SKarsten Keil 	rq->ch->protocol = rq->protocol;
1196707b2ce6SKarsten Keil 	if (card->dch.state == 7)
1197707b2ce6SKarsten Keil 		_queue_data(rq->ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
1198707b2ce6SKarsten Keil 		    0, NULL, GFP_KERNEL);
1199707b2ce6SKarsten Keil 	return 0;
1200707b2ce6SKarsten Keil }
1201707b2ce6SKarsten Keil 
1202707b2ce6SKarsten Keil static int
1203707b2ce6SKarsten Keil w6692_dctrl(struct mISDNchannel *ch, u32 cmd, void *arg)
1204707b2ce6SKarsten Keil {
1205707b2ce6SKarsten Keil 	struct mISDNdevice *dev = container_of(ch, struct mISDNdevice, D);
1206707b2ce6SKarsten Keil 	struct dchannel *dch = container_of(dev, struct dchannel, dev);
1207707b2ce6SKarsten Keil 	struct w6692_hw *card = container_of(dch, struct w6692_hw, dch);
1208707b2ce6SKarsten Keil 	struct channel_req *rq;
1209707b2ce6SKarsten Keil 	int err = 0;
1210707b2ce6SKarsten Keil 
1211707b2ce6SKarsten Keil 	pr_debug("%s: DCTRL: %x %p\n", card->name, cmd, arg);
1212707b2ce6SKarsten Keil 	switch (cmd) {
1213707b2ce6SKarsten Keil 	case OPEN_CHANNEL:
1214707b2ce6SKarsten Keil 		rq = arg;
1215707b2ce6SKarsten Keil 		if (rq->protocol == ISDN_P_TE_S0)
1216707b2ce6SKarsten Keil 			err = open_dchannel(card, rq);
1217707b2ce6SKarsten Keil 		else
1218707b2ce6SKarsten Keil 			err = open_bchannel(card, rq);
1219707b2ce6SKarsten Keil 		if (err)
1220707b2ce6SKarsten Keil 			break;
1221707b2ce6SKarsten Keil 		if (!try_module_get(THIS_MODULE))
1222707b2ce6SKarsten Keil 			pr_info("%s: cannot get module\n", card->name);
1223707b2ce6SKarsten Keil 		break;
1224707b2ce6SKarsten Keil 	case CLOSE_CHANNEL:
1225707b2ce6SKarsten Keil 		pr_debug("%s: dev(%d) close from %p\n", card->name,
1226707b2ce6SKarsten Keil 			dch->dev.id, __builtin_return_address(0));
1227707b2ce6SKarsten Keil 		module_put(THIS_MODULE);
1228707b2ce6SKarsten Keil 		break;
1229707b2ce6SKarsten Keil 	case CONTROL_CHANNEL:
1230707b2ce6SKarsten Keil 		err = channel_ctrl(card, arg);
1231707b2ce6SKarsten Keil 		break;
1232707b2ce6SKarsten Keil 	default:
1233707b2ce6SKarsten Keil 		pr_debug("%s: unknown DCTRL command %x\n", card->name, cmd);
1234707b2ce6SKarsten Keil 		return -EINVAL;
1235707b2ce6SKarsten Keil 	}
1236707b2ce6SKarsten Keil 	return err;
1237707b2ce6SKarsten Keil }
1238707b2ce6SKarsten Keil 
123970034918SStephen Rothwell static int
1240707b2ce6SKarsten Keil setup_w6692(struct w6692_hw *card)
1241707b2ce6SKarsten Keil {
1242707b2ce6SKarsten Keil 	u32	val;
1243707b2ce6SKarsten Keil 
1244707b2ce6SKarsten Keil 	if (!request_region(card->addr, 256, card->name)) {
1245707b2ce6SKarsten Keil 		pr_info("%s: config port %x-%x already in use\n", card->name,
1246707b2ce6SKarsten Keil 		       card->addr, card->addr + 255);
1247707b2ce6SKarsten Keil 		return -EIO;
1248707b2ce6SKarsten Keil 	}
1249707b2ce6SKarsten Keil 	W6692Version(card);
1250707b2ce6SKarsten Keil 	card->bc[0].addr = card->addr;
1251707b2ce6SKarsten Keil 	card->bc[1].addr = card->addr + 0x40;
1252707b2ce6SKarsten Keil 	val = ReadW6692(card, W_ISTA);
1253707b2ce6SKarsten Keil 	if (debug & DEBUG_HW)
1254707b2ce6SKarsten Keil 		pr_notice("%s ISTA=%02x\n", card->name, val);
1255707b2ce6SKarsten Keil 	val = ReadW6692(card, W_IMASK);
1256707b2ce6SKarsten Keil 	if (debug & DEBUG_HW)
1257707b2ce6SKarsten Keil 		pr_notice("%s IMASK=%02x\n", card->name, val);
1258707b2ce6SKarsten Keil 	val = ReadW6692(card, W_D_EXIR);
1259707b2ce6SKarsten Keil 	if (debug & DEBUG_HW)
1260707b2ce6SKarsten Keil 		pr_notice("%s D_EXIR=%02x\n", card->name, val);
1261707b2ce6SKarsten Keil 	val = ReadW6692(card, W_D_EXIM);
1262707b2ce6SKarsten Keil 	if (debug & DEBUG_HW)
1263707b2ce6SKarsten Keil 		pr_notice("%s D_EXIM=%02x\n", card->name, val);
1264707b2ce6SKarsten Keil 	val = ReadW6692(card, W_D_RSTA);
1265707b2ce6SKarsten Keil 	if (debug & DEBUG_HW)
1266707b2ce6SKarsten Keil 		pr_notice("%s D_RSTA=%02x\n", card->name, val);
1267707b2ce6SKarsten Keil 	return 0;
1268707b2ce6SKarsten Keil }
1269707b2ce6SKarsten Keil 
1270707b2ce6SKarsten Keil static void
1271707b2ce6SKarsten Keil release_card(struct w6692_hw *card)
1272707b2ce6SKarsten Keil {
1273707b2ce6SKarsten Keil 	u_long	flags;
1274707b2ce6SKarsten Keil 
1275707b2ce6SKarsten Keil 	spin_lock_irqsave(&card->lock, flags);
1276707b2ce6SKarsten Keil 	disable_hwirq(card);
1277707b2ce6SKarsten Keil 	w6692_mode(&card->bc[0], ISDN_P_NONE);
1278707b2ce6SKarsten Keil 	w6692_mode(&card->bc[1], ISDN_P_NONE);
1279707b2ce6SKarsten Keil 	if ((card->fmask & led) || card->subtype == W6692_USR) {
1280707b2ce6SKarsten Keil 		card->xdata |= 0x04;	/*  LED OFF */
1281707b2ce6SKarsten Keil 		WriteW6692(card, W_XDATA, card->xdata);
1282707b2ce6SKarsten Keil 	}
1283707b2ce6SKarsten Keil 	spin_unlock_irqrestore(&card->lock, flags);
1284707b2ce6SKarsten Keil 	free_irq(card->irq, card);
1285707b2ce6SKarsten Keil 	l1_event(card->dch.l1, CLOSE_CHANNEL);
1286707b2ce6SKarsten Keil 	mISDN_unregister_device(&card->dch.dev);
1287707b2ce6SKarsten Keil 	release_region(card->addr, 256);
1288707b2ce6SKarsten Keil 	mISDN_freebchannel(&card->bc[1].bch);
1289707b2ce6SKarsten Keil 	mISDN_freebchannel(&card->bc[0].bch);
1290707b2ce6SKarsten Keil 	mISDN_freedchannel(&card->dch);
1291707b2ce6SKarsten Keil 	write_lock_irqsave(&card_lock, flags);
1292707b2ce6SKarsten Keil 	list_del(&card->list);
1293707b2ce6SKarsten Keil 	write_unlock_irqrestore(&card_lock, flags);
1294707b2ce6SKarsten Keil 	pci_disable_device(card->pdev);
1295707b2ce6SKarsten Keil 	pci_set_drvdata(card->pdev, NULL);
1296707b2ce6SKarsten Keil 	kfree(card);
1297707b2ce6SKarsten Keil }
1298707b2ce6SKarsten Keil 
1299707b2ce6SKarsten Keil static int
1300707b2ce6SKarsten Keil setup_instance(struct w6692_hw *card)
1301707b2ce6SKarsten Keil {
1302707b2ce6SKarsten Keil 	int		i, err;
1303707b2ce6SKarsten Keil 	u_long		flags;
1304707b2ce6SKarsten Keil 
1305707b2ce6SKarsten Keil 	snprintf(card->name, MISDN_MAX_IDLEN - 1, "w6692.%d", w6692_cnt + 1);
1306707b2ce6SKarsten Keil 	write_lock_irqsave(&card_lock, flags);
1307707b2ce6SKarsten Keil 	list_add_tail(&card->list, &Cards);
1308707b2ce6SKarsten Keil 	write_unlock_irqrestore(&card_lock, flags);
1309707b2ce6SKarsten Keil 	card->fmask = (1 << w6692_cnt);
1310707b2ce6SKarsten Keil 	_set_debug(card);
1311707b2ce6SKarsten Keil 	spin_lock_init(&card->lock);
1312707b2ce6SKarsten Keil 	mISDN_initdchannel(&card->dch, MAX_DFRAME_LEN_L1, W6692_ph_bh);
1313707b2ce6SKarsten Keil 	card->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0);
1314707b2ce6SKarsten Keil 	card->dch.dev.D.send = w6692_l2l1D;
1315707b2ce6SKarsten Keil 	card->dch.dev.D.ctrl = w6692_dctrl;
1316707b2ce6SKarsten Keil 	card->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
1317707b2ce6SKarsten Keil 		(1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
1318707b2ce6SKarsten Keil 	card->dch.hw = card;
1319707b2ce6SKarsten Keil 	card->dch.dev.nrbchan = 2;
1320707b2ce6SKarsten Keil 	for (i = 0; i < 2; i++) {
1321707b2ce6SKarsten Keil 		mISDN_initbchannel(&card->bc[i].bch, MAX_DATA_MEM);
1322707b2ce6SKarsten Keil 		card->bc[i].bch.hw = card;
1323707b2ce6SKarsten Keil 		card->bc[i].bch.nr = i + 1;
1324707b2ce6SKarsten Keil 		card->bc[i].bch.ch.nr = i + 1;
1325707b2ce6SKarsten Keil 		card->bc[i].bch.ch.send = w6692_l2l1B;
1326707b2ce6SKarsten Keil 		card->bc[i].bch.ch.ctrl = w6692_bctrl;
1327707b2ce6SKarsten Keil 		set_channelmap(i + 1, card->dch.dev.channelmap);
1328707b2ce6SKarsten Keil 		list_add(&card->bc[i].bch.ch.list, &card->dch.dev.bchannels);
1329707b2ce6SKarsten Keil 	}
1330707b2ce6SKarsten Keil 	err = setup_w6692(card);
1331707b2ce6SKarsten Keil 	if (err)
1332707b2ce6SKarsten Keil 		goto error_setup;
1333707b2ce6SKarsten Keil 	err = mISDN_register_device(&card->dch.dev, &card->pdev->dev,
1334707b2ce6SKarsten Keil 		card->name);
1335707b2ce6SKarsten Keil 	if (err)
1336707b2ce6SKarsten Keil 		goto error_reg;
1337707b2ce6SKarsten Keil 	err = init_card(card);
1338707b2ce6SKarsten Keil 	if (err)
1339707b2ce6SKarsten Keil 		goto error_init;
1340707b2ce6SKarsten Keil 	err = create_l1(&card->dch, w6692_l1callback);
1341707b2ce6SKarsten Keil 	if (!err) {
1342707b2ce6SKarsten Keil 		w6692_cnt++;
1343707b2ce6SKarsten Keil 		pr_notice("W6692 %d cards installed\n", w6692_cnt);
1344707b2ce6SKarsten Keil 		return 0;
1345707b2ce6SKarsten Keil 	}
1346707b2ce6SKarsten Keil 
1347707b2ce6SKarsten Keil 	free_irq(card->irq, card);
1348707b2ce6SKarsten Keil error_init:
1349707b2ce6SKarsten Keil 	mISDN_unregister_device(&card->dch.dev);
1350707b2ce6SKarsten Keil error_reg:
1351707b2ce6SKarsten Keil 	release_region(card->addr, 256);
1352707b2ce6SKarsten Keil error_setup:
1353707b2ce6SKarsten Keil 	mISDN_freebchannel(&card->bc[1].bch);
1354707b2ce6SKarsten Keil 	mISDN_freebchannel(&card->bc[0].bch);
1355707b2ce6SKarsten Keil 	mISDN_freedchannel(&card->dch);
1356707b2ce6SKarsten Keil 	write_lock_irqsave(&card_lock, flags);
1357707b2ce6SKarsten Keil 	list_del(&card->list);
1358707b2ce6SKarsten Keil 	write_unlock_irqrestore(&card_lock, flags);
1359707b2ce6SKarsten Keil 	kfree(card);
1360707b2ce6SKarsten Keil 	return err;
1361707b2ce6SKarsten Keil }
1362707b2ce6SKarsten Keil 
1363707b2ce6SKarsten Keil static int __devinit
1364707b2ce6SKarsten Keil w6692_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1365707b2ce6SKarsten Keil {
1366707b2ce6SKarsten Keil 	int		err = -ENOMEM;
1367707b2ce6SKarsten Keil 	struct w6692_hw	*card;
1368707b2ce6SKarsten Keil 	struct w6692map	*m = (struct w6692map *)ent->driver_data;
1369707b2ce6SKarsten Keil 
1370707b2ce6SKarsten Keil 	card = kzalloc(sizeof(struct w6692_hw), GFP_KERNEL);
1371707b2ce6SKarsten Keil 	if (!card) {
1372707b2ce6SKarsten Keil 		pr_info("No kmem for w6692 card\n");
1373707b2ce6SKarsten Keil 		return err;
1374707b2ce6SKarsten Keil 	}
1375707b2ce6SKarsten Keil 	card->pdev = pdev;
1376707b2ce6SKarsten Keil 	card->subtype = m->subtype;
1377707b2ce6SKarsten Keil 	err = pci_enable_device(pdev);
1378707b2ce6SKarsten Keil 	if (err) {
1379707b2ce6SKarsten Keil 		kfree(card);
1380707b2ce6SKarsten Keil 		return err;
1381707b2ce6SKarsten Keil 	}
1382707b2ce6SKarsten Keil 
1383707b2ce6SKarsten Keil 	printk(KERN_INFO "mISDN_w6692: found adapter %s at %s\n",
1384707b2ce6SKarsten Keil 	       m->name, pci_name(pdev));
1385707b2ce6SKarsten Keil 
1386707b2ce6SKarsten Keil 	card->addr = pci_resource_start(pdev, 1);
1387707b2ce6SKarsten Keil 	card->irq = pdev->irq;
1388707b2ce6SKarsten Keil 	pci_set_drvdata(pdev, card);
1389707b2ce6SKarsten Keil 	err = setup_instance(card);
1390707b2ce6SKarsten Keil 	if (err)
1391707b2ce6SKarsten Keil 		pci_set_drvdata(pdev, NULL);
1392707b2ce6SKarsten Keil 	return err;
1393707b2ce6SKarsten Keil }
1394707b2ce6SKarsten Keil 
1395707b2ce6SKarsten Keil static void __devexit
1396707b2ce6SKarsten Keil w6692_remove_pci(struct pci_dev *pdev)
1397707b2ce6SKarsten Keil {
1398707b2ce6SKarsten Keil 	struct w6692_hw	*card = pci_get_drvdata(pdev);
1399707b2ce6SKarsten Keil 
1400707b2ce6SKarsten Keil 	if (card)
1401707b2ce6SKarsten Keil 		release_card(card);
1402707b2ce6SKarsten Keil 	else
1403707b2ce6SKarsten Keil 		if (debug)
1404707b2ce6SKarsten Keil 			pr_notice("%s: drvdata allready removed\n", __func__);
1405707b2ce6SKarsten Keil }
1406707b2ce6SKarsten Keil 
1407707b2ce6SKarsten Keil static struct pci_device_id w6692_ids[] = {
1408707b2ce6SKarsten Keil 	{ PCI_VENDOR_ID_DYNALINK, PCI_DEVICE_ID_DYNALINK_IS64PH,
1409707b2ce6SKarsten Keil 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, (ulong)&w6692_map[0]},
1410707b2ce6SKarsten Keil 	{ PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_6692,
1411707b2ce6SKarsten Keil 	  PCI_VENDOR_ID_USR, PCI_DEVICE_ID_USR_6692, 0, 0,
1412707b2ce6SKarsten Keil 	  (ulong)&w6692_map[2]},
1413707b2ce6SKarsten Keil 	{ PCI_VENDOR_ID_WINBOND2, PCI_DEVICE_ID_WINBOND2_6692,
1414707b2ce6SKarsten Keil 	  PCI_ANY_ID, PCI_ANY_ID, 0, 0, (ulong)&w6692_map[1]},
1415707b2ce6SKarsten Keil 	{ }
1416707b2ce6SKarsten Keil };
1417707b2ce6SKarsten Keil MODULE_DEVICE_TABLE(pci, w6692_ids);
1418707b2ce6SKarsten Keil 
1419707b2ce6SKarsten Keil static struct pci_driver w6692_driver = {
1420707b2ce6SKarsten Keil 	.name =  "w6692",
1421707b2ce6SKarsten Keil 	.probe = w6692_probe,
1422707b2ce6SKarsten Keil 	.remove = __devexit_p(w6692_remove_pci),
1423707b2ce6SKarsten Keil 	.id_table = w6692_ids,
1424707b2ce6SKarsten Keil };
1425707b2ce6SKarsten Keil 
1426707b2ce6SKarsten Keil static int __init w6692_init(void)
1427707b2ce6SKarsten Keil {
1428707b2ce6SKarsten Keil 	int err;
1429707b2ce6SKarsten Keil 
1430707b2ce6SKarsten Keil 	pr_notice("Winbond W6692 PCI driver Rev. %s\n", W6692_REV);
1431707b2ce6SKarsten Keil 
1432707b2ce6SKarsten Keil 	err = pci_register_driver(&w6692_driver);
1433707b2ce6SKarsten Keil 	return err;
1434707b2ce6SKarsten Keil }
1435707b2ce6SKarsten Keil 
1436707b2ce6SKarsten Keil static void __exit w6692_cleanup(void)
1437707b2ce6SKarsten Keil {
1438707b2ce6SKarsten Keil 	pci_unregister_driver(&w6692_driver);
1439707b2ce6SKarsten Keil }
1440707b2ce6SKarsten Keil 
1441707b2ce6SKarsten Keil module_init(w6692_init);
1442707b2ce6SKarsten Keil module_exit(w6692_cleanup);
1443