cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

hfcsusb.c (55453B)


      1// SPDX-License-Identifier: GPL-2.0-or-later
      2/* hfcsusb.c
      3 * mISDN driver for Colognechip HFC-S USB chip
      4 *
      5 * Copyright 2001 by Peter Sprenger (sprenger@moving-bytes.de)
      6 * Copyright 2008 by Martin Bachem (info@bachem-it.com)
      7 *
      8 * module params
      9 *   debug=<n>, default=0, with n=0xHHHHGGGG
     10 *      H - l1 driver flags described in hfcsusb.h
     11 *      G - common mISDN debug flags described at mISDNhw.h
     12 *
     13 *   poll=<n>, default 128
     14 *     n : burst size of PH_DATA_IND at transparent rx data
     15 *
     16 * Revision: 0.3.3 (socket), 2008-11-05
     17 */
     18
     19#include <linux/module.h>
     20#include <linux/delay.h>
     21#include <linux/usb.h>
     22#include <linux/mISDNhw.h>
     23#include <linux/slab.h>
     24#include "hfcsusb.h"
     25
     26static unsigned int debug;
     27static int poll = DEFAULT_TRANSP_BURST_SZ;
     28
     29static LIST_HEAD(HFClist);
     30static DEFINE_RWLOCK(HFClock);
     31
     32
     33MODULE_AUTHOR("Martin Bachem");
     34MODULE_LICENSE("GPL");
     35module_param(debug, uint, S_IRUGO | S_IWUSR);
     36module_param(poll, int, 0);
     37
     38static int hfcsusb_cnt;
     39
     40/* some function prototypes */
     41static void hfcsusb_ph_command(struct hfcsusb *hw, u_char command);
     42static void release_hw(struct hfcsusb *hw);
     43static void reset_hfcsusb(struct hfcsusb *hw);
     44static void setPortMode(struct hfcsusb *hw);
     45static void hfcsusb_start_endpoint(struct hfcsusb *hw, int channel);
     46static void hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel);
     47static int  hfcsusb_setup_bch(struct bchannel *bch, int protocol);
     48static void deactivate_bchannel(struct bchannel *bch);
     49static int  hfcsusb_ph_info(struct hfcsusb *hw);
     50
     51/* start next background transfer for control channel */
     52static void
     53ctrl_start_transfer(struct hfcsusb *hw)
     54{
     55	if (debug & DBG_HFC_CALL_TRACE)
     56		printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
     57
     58	if (hw->ctrl_cnt) {
     59		hw->ctrl_urb->pipe = hw->ctrl_out_pipe;
     60		hw->ctrl_urb->setup_packet = (u_char *)&hw->ctrl_write;
     61		hw->ctrl_urb->transfer_buffer = NULL;
     62		hw->ctrl_urb->transfer_buffer_length = 0;
     63		hw->ctrl_write.wIndex =
     64			cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].hfcs_reg);
     65		hw->ctrl_write.wValue =
     66			cpu_to_le16(hw->ctrl_buff[hw->ctrl_out_idx].reg_val);
     67
     68		usb_submit_urb(hw->ctrl_urb, GFP_ATOMIC);
     69	}
     70}
     71
     72/*
     73 * queue a control transfer request to write HFC-S USB
     74 * chip register using CTRL resuest queue
     75 */
     76static int write_reg(struct hfcsusb *hw, __u8 reg, __u8 val)
     77{
     78	struct ctrl_buf *buf;
     79
     80	if (debug & DBG_HFC_CALL_TRACE)
     81		printk(KERN_DEBUG "%s: %s reg(0x%02x) val(0x%02x)\n",
     82		       hw->name, __func__, reg, val);
     83
     84	spin_lock(&hw->ctrl_lock);
     85	if (hw->ctrl_cnt >= HFC_CTRL_BUFSIZE) {
     86		spin_unlock(&hw->ctrl_lock);
     87		return 1;
     88	}
     89	buf = &hw->ctrl_buff[hw->ctrl_in_idx];
     90	buf->hfcs_reg = reg;
     91	buf->reg_val = val;
     92	if (++hw->ctrl_in_idx >= HFC_CTRL_BUFSIZE)
     93		hw->ctrl_in_idx = 0;
     94	if (++hw->ctrl_cnt == 1)
     95		ctrl_start_transfer(hw);
     96	spin_unlock(&hw->ctrl_lock);
     97
     98	return 0;
     99}
    100
    101/* control completion routine handling background control cmds */
    102static void
    103ctrl_complete(struct urb *urb)
    104{
    105	struct hfcsusb *hw = (struct hfcsusb *) urb->context;
    106
    107	if (debug & DBG_HFC_CALL_TRACE)
    108		printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
    109
    110	urb->dev = hw->dev;
    111	if (hw->ctrl_cnt) {
    112		hw->ctrl_cnt--;	/* decrement actual count */
    113		if (++hw->ctrl_out_idx >= HFC_CTRL_BUFSIZE)
    114			hw->ctrl_out_idx = 0;	/* pointer wrap */
    115
    116		ctrl_start_transfer(hw); /* start next transfer */
    117	}
    118}
    119
    120/* handle LED bits   */
    121static void
    122set_led_bit(struct hfcsusb *hw, signed short led_bits, int set_on)
    123{
    124	if (set_on) {
    125		if (led_bits < 0)
    126			hw->led_state &= ~abs(led_bits);
    127		else
    128			hw->led_state |= led_bits;
    129	} else {
    130		if (led_bits < 0)
    131			hw->led_state |= abs(led_bits);
    132		else
    133			hw->led_state &= ~led_bits;
    134	}
    135}
    136
    137/* handle LED requests  */
    138static void
    139handle_led(struct hfcsusb *hw, int event)
    140{
    141	struct hfcsusb_vdata *driver_info = (struct hfcsusb_vdata *)
    142		hfcsusb_idtab[hw->vend_idx].driver_info;
    143	__u8 tmpled;
    144
    145	if (driver_info->led_scheme == LED_OFF)
    146		return;
    147	tmpled = hw->led_state;
    148
    149	switch (event) {
    150	case LED_POWER_ON:
    151		set_led_bit(hw, driver_info->led_bits[0], 1);
    152		set_led_bit(hw, driver_info->led_bits[1], 0);
    153		set_led_bit(hw, driver_info->led_bits[2], 0);
    154		set_led_bit(hw, driver_info->led_bits[3], 0);
    155		break;
    156	case LED_POWER_OFF:
    157		set_led_bit(hw, driver_info->led_bits[0], 0);
    158		set_led_bit(hw, driver_info->led_bits[1], 0);
    159		set_led_bit(hw, driver_info->led_bits[2], 0);
    160		set_led_bit(hw, driver_info->led_bits[3], 0);
    161		break;
    162	case LED_S0_ON:
    163		set_led_bit(hw, driver_info->led_bits[1], 1);
    164		break;
    165	case LED_S0_OFF:
    166		set_led_bit(hw, driver_info->led_bits[1], 0);
    167		break;
    168	case LED_B1_ON:
    169		set_led_bit(hw, driver_info->led_bits[2], 1);
    170		break;
    171	case LED_B1_OFF:
    172		set_led_bit(hw, driver_info->led_bits[2], 0);
    173		break;
    174	case LED_B2_ON:
    175		set_led_bit(hw, driver_info->led_bits[3], 1);
    176		break;
    177	case LED_B2_OFF:
    178		set_led_bit(hw, driver_info->led_bits[3], 0);
    179		break;
    180	}
    181
    182	if (hw->led_state != tmpled) {
    183		if (debug & DBG_HFC_CALL_TRACE)
    184			printk(KERN_DEBUG "%s: %s reg(0x%02x) val(x%02x)\n",
    185			       hw->name, __func__,
    186			       HFCUSB_P_DATA, hw->led_state);
    187
    188		write_reg(hw, HFCUSB_P_DATA, hw->led_state);
    189	}
    190}
    191
    192/*
    193 * Layer2 -> Layer 1 Bchannel data
    194 */
    195static int
    196hfcusb_l2l1B(struct mISDNchannel *ch, struct sk_buff *skb)
    197{
    198	struct bchannel		*bch = container_of(ch, struct bchannel, ch);
    199	struct hfcsusb		*hw = bch->hw;
    200	int			ret = -EINVAL;
    201	struct mISDNhead	*hh = mISDN_HEAD_P(skb);
    202	u_long			flags;
    203
    204	if (debug & DBG_HFC_CALL_TRACE)
    205		printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
    206
    207	switch (hh->prim) {
    208	case PH_DATA_REQ:
    209		spin_lock_irqsave(&hw->lock, flags);
    210		ret = bchannel_senddata(bch, skb);
    211		spin_unlock_irqrestore(&hw->lock, flags);
    212		if (debug & DBG_HFC_CALL_TRACE)
    213			printk(KERN_DEBUG "%s: %s PH_DATA_REQ ret(%i)\n",
    214			       hw->name, __func__, ret);
    215		if (ret > 0)
    216			ret = 0;
    217		return ret;
    218	case PH_ACTIVATE_REQ:
    219		if (!test_and_set_bit(FLG_ACTIVE, &bch->Flags)) {
    220			hfcsusb_start_endpoint(hw, bch->nr - 1);
    221			ret = hfcsusb_setup_bch(bch, ch->protocol);
    222		} else
    223			ret = 0;
    224		if (!ret)
    225			_queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
    226				    0, NULL, GFP_KERNEL);
    227		break;
    228	case PH_DEACTIVATE_REQ:
    229		deactivate_bchannel(bch);
    230		_queue_data(ch, PH_DEACTIVATE_IND, MISDN_ID_ANY,
    231			    0, NULL, GFP_KERNEL);
    232		ret = 0;
    233		break;
    234	}
    235	if (!ret)
    236		dev_kfree_skb(skb);
    237	return ret;
    238}
    239
    240/*
    241 * send full D/B channel status information
    242 * as MPH_INFORMATION_IND
    243 */
    244static int
    245hfcsusb_ph_info(struct hfcsusb *hw)
    246{
    247	struct ph_info *phi;
    248	struct dchannel *dch = &hw->dch;
    249	int i;
    250
    251	phi = kzalloc(struct_size(phi, bch, dch->dev.nrbchan), GFP_ATOMIC);
    252	if (!phi)
    253		return -ENOMEM;
    254
    255	phi->dch.ch.protocol = hw->protocol;
    256	phi->dch.ch.Flags = dch->Flags;
    257	phi->dch.state = dch->state;
    258	phi->dch.num_bch = dch->dev.nrbchan;
    259	for (i = 0; i < dch->dev.nrbchan; i++) {
    260		phi->bch[i].protocol = hw->bch[i].ch.protocol;
    261		phi->bch[i].Flags = hw->bch[i].Flags;
    262	}
    263	_queue_data(&dch->dev.D, MPH_INFORMATION_IND, MISDN_ID_ANY,
    264		    struct_size(phi, bch, dch->dev.nrbchan), phi, GFP_ATOMIC);
    265	kfree(phi);
    266
    267	return 0;
    268}
    269
    270/*
    271 * Layer2 -> Layer 1 Dchannel data
    272 */
    273static int
    274hfcusb_l2l1D(struct mISDNchannel *ch, struct sk_buff *skb)
    275{
    276	struct mISDNdevice	*dev = container_of(ch, struct mISDNdevice, D);
    277	struct dchannel		*dch = container_of(dev, struct dchannel, dev);
    278	struct mISDNhead	*hh = mISDN_HEAD_P(skb);
    279	struct hfcsusb		*hw = dch->hw;
    280	int			ret = -EINVAL;
    281	u_long			flags;
    282
    283	switch (hh->prim) {
    284	case PH_DATA_REQ:
    285		if (debug & DBG_HFC_CALL_TRACE)
    286			printk(KERN_DEBUG "%s: %s: PH_DATA_REQ\n",
    287			       hw->name, __func__);
    288
    289		spin_lock_irqsave(&hw->lock, flags);
    290		ret = dchannel_senddata(dch, skb);
    291		spin_unlock_irqrestore(&hw->lock, flags);
    292		if (ret > 0) {
    293			ret = 0;
    294			queue_ch_frame(ch, PH_DATA_CNF, hh->id, NULL);
    295		}
    296		break;
    297
    298	case PH_ACTIVATE_REQ:
    299		if (debug & DBG_HFC_CALL_TRACE)
    300			printk(KERN_DEBUG "%s: %s: PH_ACTIVATE_REQ %s\n",
    301			       hw->name, __func__,
    302			       (hw->protocol == ISDN_P_NT_S0) ? "NT" : "TE");
    303
    304		if (hw->protocol == ISDN_P_NT_S0) {
    305			ret = 0;
    306			if (test_bit(FLG_ACTIVE, &dch->Flags)) {
    307				_queue_data(&dch->dev.D,
    308					    PH_ACTIVATE_IND, MISDN_ID_ANY, 0,
    309					    NULL, GFP_ATOMIC);
    310			} else {
    311				hfcsusb_ph_command(hw,
    312						   HFC_L1_ACTIVATE_NT);
    313				test_and_set_bit(FLG_L2_ACTIVATED,
    314						 &dch->Flags);
    315			}
    316		} else {
    317			hfcsusb_ph_command(hw, HFC_L1_ACTIVATE_TE);
    318			ret = l1_event(dch->l1, hh->prim);
    319		}
    320		break;
    321
    322	case PH_DEACTIVATE_REQ:
    323		if (debug & DBG_HFC_CALL_TRACE)
    324			printk(KERN_DEBUG "%s: %s: PH_DEACTIVATE_REQ\n",
    325			       hw->name, __func__);
    326		test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
    327
    328		if (hw->protocol == ISDN_P_NT_S0) {
    329			hfcsusb_ph_command(hw, HFC_L1_DEACTIVATE_NT);
    330			spin_lock_irqsave(&hw->lock, flags);
    331			skb_queue_purge(&dch->squeue);
    332			if (dch->tx_skb) {
    333				dev_kfree_skb(dch->tx_skb);
    334				dch->tx_skb = NULL;
    335			}
    336			dch->tx_idx = 0;
    337			if (dch->rx_skb) {
    338				dev_kfree_skb(dch->rx_skb);
    339				dch->rx_skb = NULL;
    340			}
    341			test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
    342			spin_unlock_irqrestore(&hw->lock, flags);
    343#ifdef FIXME
    344			if (test_and_clear_bit(FLG_L1_BUSY, &dch->Flags))
    345				dchannel_sched_event(&hc->dch, D_CLEARBUSY);
    346#endif
    347			ret = 0;
    348		} else
    349			ret = l1_event(dch->l1, hh->prim);
    350		break;
    351	case MPH_INFORMATION_REQ:
    352		ret = hfcsusb_ph_info(hw);
    353		break;
    354	}
    355
    356	return ret;
    357}
    358
    359/*
    360 * Layer 1 callback function
    361 */
    362static int
    363hfc_l1callback(struct dchannel *dch, u_int cmd)
    364{
    365	struct hfcsusb *hw = dch->hw;
    366
    367	if (debug & DBG_HFC_CALL_TRACE)
    368		printk(KERN_DEBUG "%s: %s cmd 0x%x\n",
    369		       hw->name, __func__, cmd);
    370
    371	switch (cmd) {
    372	case INFO3_P8:
    373	case INFO3_P10:
    374	case HW_RESET_REQ:
    375	case HW_POWERUP_REQ:
    376		break;
    377
    378	case HW_DEACT_REQ:
    379		skb_queue_purge(&dch->squeue);
    380		if (dch->tx_skb) {
    381			dev_kfree_skb(dch->tx_skb);
    382			dch->tx_skb = NULL;
    383		}
    384		dch->tx_idx = 0;
    385		if (dch->rx_skb) {
    386			dev_kfree_skb(dch->rx_skb);
    387			dch->rx_skb = NULL;
    388		}
    389		test_and_clear_bit(FLG_TX_BUSY, &dch->Flags);
    390		break;
    391	case PH_ACTIVATE_IND:
    392		test_and_set_bit(FLG_ACTIVE, &dch->Flags);
    393		_queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
    394			    GFP_ATOMIC);
    395		break;
    396	case PH_DEACTIVATE_IND:
    397		test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
    398		_queue_data(&dch->dev.D, cmd, MISDN_ID_ANY, 0, NULL,
    399			    GFP_ATOMIC);
    400		break;
    401	default:
    402		if (dch->debug & DEBUG_HW)
    403			printk(KERN_DEBUG "%s: %s: unknown cmd %x\n",
    404			       hw->name, __func__, cmd);
    405		return -1;
    406	}
    407	return hfcsusb_ph_info(hw);
    408}
    409
    410static int
    411open_dchannel(struct hfcsusb *hw, struct mISDNchannel *ch,
    412	      struct channel_req *rq)
    413{
    414	int err = 0;
    415
    416	if (debug & DEBUG_HW_OPEN)
    417		printk(KERN_DEBUG "%s: %s: dev(%d) open addr(%i) from %p\n",
    418		       hw->name, __func__, hw->dch.dev.id, rq->adr.channel,
    419		       __builtin_return_address(0));
    420	if (rq->protocol == ISDN_P_NONE)
    421		return -EINVAL;
    422
    423	test_and_clear_bit(FLG_ACTIVE, &hw->dch.Flags);
    424	test_and_clear_bit(FLG_ACTIVE, &hw->ech.Flags);
    425	hfcsusb_start_endpoint(hw, HFC_CHAN_D);
    426
    427	/* E-Channel logging */
    428	if (rq->adr.channel == 1) {
    429		if (hw->fifos[HFCUSB_PCM_RX].pipe) {
    430			hfcsusb_start_endpoint(hw, HFC_CHAN_E);
    431			set_bit(FLG_ACTIVE, &hw->ech.Flags);
    432			_queue_data(&hw->ech.dev.D, PH_ACTIVATE_IND,
    433				    MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
    434		} else
    435			return -EINVAL;
    436	}
    437
    438	if (!hw->initdone) {
    439		hw->protocol = rq->protocol;
    440		if (rq->protocol == ISDN_P_TE_S0) {
    441			err = create_l1(&hw->dch, hfc_l1callback);
    442			if (err)
    443				return err;
    444		}
    445		setPortMode(hw);
    446		ch->protocol = rq->protocol;
    447		hw->initdone = 1;
    448	} else {
    449		if (rq->protocol != ch->protocol)
    450			return -EPROTONOSUPPORT;
    451	}
    452
    453	if (((ch->protocol == ISDN_P_NT_S0) && (hw->dch.state == 3)) ||
    454	    ((ch->protocol == ISDN_P_TE_S0) && (hw->dch.state == 7)))
    455		_queue_data(ch, PH_ACTIVATE_IND, MISDN_ID_ANY,
    456			    0, NULL, GFP_KERNEL);
    457	rq->ch = ch;
    458	if (!try_module_get(THIS_MODULE))
    459		printk(KERN_WARNING "%s: %s: cannot get module\n",
    460		       hw->name, __func__);
    461	return 0;
    462}
    463
    464static int
    465open_bchannel(struct hfcsusb *hw, struct channel_req *rq)
    466{
    467	struct bchannel		*bch;
    468
    469	if (rq->adr.channel == 0 || rq->adr.channel > 2)
    470		return -EINVAL;
    471	if (rq->protocol == ISDN_P_NONE)
    472		return -EINVAL;
    473
    474	if (debug & DBG_HFC_CALL_TRACE)
    475		printk(KERN_DEBUG "%s: %s B%i\n",
    476		       hw->name, __func__, rq->adr.channel);
    477
    478	bch = &hw->bch[rq->adr.channel - 1];
    479	if (test_and_set_bit(FLG_OPEN, &bch->Flags))
    480		return -EBUSY; /* b-channel can be only open once */
    481	bch->ch.protocol = rq->protocol;
    482	rq->ch = &bch->ch;
    483
    484	if (!try_module_get(THIS_MODULE))
    485		printk(KERN_WARNING "%s: %s:cannot get module\n",
    486		       hw->name, __func__);
    487	return 0;
    488}
    489
    490static int
    491channel_ctrl(struct hfcsusb *hw, struct mISDN_ctrl_req *cq)
    492{
    493	int ret = 0;
    494
    495	if (debug & DBG_HFC_CALL_TRACE)
    496		printk(KERN_DEBUG "%s: %s op(0x%x) channel(0x%x)\n",
    497		       hw->name, __func__, (cq->op), (cq->channel));
    498
    499	switch (cq->op) {
    500	case MISDN_CTRL_GETOP:
    501		cq->op = MISDN_CTRL_LOOP | MISDN_CTRL_CONNECT |
    502			MISDN_CTRL_DISCONNECT;
    503		break;
    504	default:
    505		printk(KERN_WARNING "%s: %s: unknown Op %x\n",
    506		       hw->name, __func__, cq->op);
    507		ret = -EINVAL;
    508		break;
    509	}
    510	return ret;
    511}
    512
    513/*
    514 * device control function
    515 */
    516static int
    517hfc_dctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
    518{
    519	struct mISDNdevice	*dev = container_of(ch, struct mISDNdevice, D);
    520	struct dchannel		*dch = container_of(dev, struct dchannel, dev);
    521	struct hfcsusb		*hw = dch->hw;
    522	struct channel_req	*rq;
    523	int			err = 0;
    524
    525	if (dch->debug & DEBUG_HW)
    526		printk(KERN_DEBUG "%s: %s: cmd:%x %p\n",
    527		       hw->name, __func__, cmd, arg);
    528	switch (cmd) {
    529	case OPEN_CHANNEL:
    530		rq = arg;
    531		if ((rq->protocol == ISDN_P_TE_S0) ||
    532		    (rq->protocol == ISDN_P_NT_S0))
    533			err = open_dchannel(hw, ch, rq);
    534		else
    535			err = open_bchannel(hw, rq);
    536		if (!err)
    537			hw->open++;
    538		break;
    539	case CLOSE_CHANNEL:
    540		hw->open--;
    541		if (debug & DEBUG_HW_OPEN)
    542			printk(KERN_DEBUG
    543			       "%s: %s: dev(%d) close from %p (open %d)\n",
    544			       hw->name, __func__, hw->dch.dev.id,
    545			       __builtin_return_address(0), hw->open);
    546		if (!hw->open) {
    547			hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
    548			if (hw->fifos[HFCUSB_PCM_RX].pipe)
    549				hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
    550			handle_led(hw, LED_POWER_ON);
    551		}
    552		module_put(THIS_MODULE);
    553		break;
    554	case CONTROL_CHANNEL:
    555		err = channel_ctrl(hw, arg);
    556		break;
    557	default:
    558		if (dch->debug & DEBUG_HW)
    559			printk(KERN_DEBUG "%s: %s: unknown command %x\n",
    560			       hw->name, __func__, cmd);
    561		return -EINVAL;
    562	}
    563	return err;
    564}
    565
    566/*
    567 * S0 TE state change event handler
    568 */
    569static void
    570ph_state_te(struct dchannel *dch)
    571{
    572	struct hfcsusb *hw = dch->hw;
    573
    574	if (debug & DEBUG_HW) {
    575		if (dch->state <= HFC_MAX_TE_LAYER1_STATE)
    576			printk(KERN_DEBUG "%s: %s: %s\n", hw->name, __func__,
    577			       HFC_TE_LAYER1_STATES[dch->state]);
    578		else
    579			printk(KERN_DEBUG "%s: %s: TE F%d\n",
    580			       hw->name, __func__, dch->state);
    581	}
    582
    583	switch (dch->state) {
    584	case 0:
    585		l1_event(dch->l1, HW_RESET_IND);
    586		break;
    587	case 3:
    588		l1_event(dch->l1, HW_DEACT_IND);
    589		break;
    590	case 5:
    591	case 8:
    592		l1_event(dch->l1, ANYSIGNAL);
    593		break;
    594	case 6:
    595		l1_event(dch->l1, INFO2);
    596		break;
    597	case 7:
    598		l1_event(dch->l1, INFO4_P8);
    599		break;
    600	}
    601	if (dch->state == 7)
    602		handle_led(hw, LED_S0_ON);
    603	else
    604		handle_led(hw, LED_S0_OFF);
    605}
    606
    607/*
    608 * S0 NT state change event handler
    609 */
    610static void
    611ph_state_nt(struct dchannel *dch)
    612{
    613	struct hfcsusb *hw = dch->hw;
    614
    615	if (debug & DEBUG_HW) {
    616		if (dch->state <= HFC_MAX_NT_LAYER1_STATE)
    617			printk(KERN_DEBUG "%s: %s: %s\n",
    618			       hw->name, __func__,
    619			       HFC_NT_LAYER1_STATES[dch->state]);
    620
    621		else
    622			printk(KERN_INFO DRIVER_NAME "%s: %s: NT G%d\n",
    623			       hw->name, __func__, dch->state);
    624	}
    625
    626	switch (dch->state) {
    627	case (1):
    628		test_and_clear_bit(FLG_ACTIVE, &dch->Flags);
    629		test_and_clear_bit(FLG_L2_ACTIVATED, &dch->Flags);
    630		hw->nt_timer = 0;
    631		hw->timers &= ~NT_ACTIVATION_TIMER;
    632		handle_led(hw, LED_S0_OFF);
    633		break;
    634
    635	case (2):
    636		if (hw->nt_timer < 0) {
    637			hw->nt_timer = 0;
    638			hw->timers &= ~NT_ACTIVATION_TIMER;
    639			hfcsusb_ph_command(dch->hw, HFC_L1_DEACTIVATE_NT);
    640		} else {
    641			hw->timers |= NT_ACTIVATION_TIMER;
    642			hw->nt_timer = NT_T1_COUNT;
    643			/* allow G2 -> G3 transition */
    644			write_reg(hw, HFCUSB_STATES, 2 | HFCUSB_NT_G2_G3);
    645		}
    646		break;
    647	case (3):
    648		hw->nt_timer = 0;
    649		hw->timers &= ~NT_ACTIVATION_TIMER;
    650		test_and_set_bit(FLG_ACTIVE, &dch->Flags);
    651		_queue_data(&dch->dev.D, PH_ACTIVATE_IND,
    652			    MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
    653		handle_led(hw, LED_S0_ON);
    654		break;
    655	case (4):
    656		hw->nt_timer = 0;
    657		hw->timers &= ~NT_ACTIVATION_TIMER;
    658		break;
    659	default:
    660		break;
    661	}
    662	hfcsusb_ph_info(hw);
    663}
    664
    665static void
    666ph_state(struct dchannel *dch)
    667{
    668	struct hfcsusb *hw = dch->hw;
    669
    670	if (hw->protocol == ISDN_P_NT_S0)
    671		ph_state_nt(dch);
    672	else if (hw->protocol == ISDN_P_TE_S0)
    673		ph_state_te(dch);
    674}
    675
    676/*
    677 * disable/enable BChannel for desired protocoll
    678 */
    679static int
    680hfcsusb_setup_bch(struct bchannel *bch, int protocol)
    681{
    682	struct hfcsusb *hw = bch->hw;
    683	__u8 conhdlc, sctrl, sctrl_r;
    684
    685	if (debug & DEBUG_HW)
    686		printk(KERN_DEBUG "%s: %s: protocol %x-->%x B%d\n",
    687		       hw->name, __func__, bch->state, protocol,
    688		       bch->nr);
    689
    690	/* setup val for CON_HDLC */
    691	conhdlc = 0;
    692	if (protocol > ISDN_P_NONE)
    693		conhdlc = 8;	/* enable FIFO */
    694
    695	switch (protocol) {
    696	case (-1):	/* used for init */
    697		bch->state = -1;
    698		fallthrough;
    699	case (ISDN_P_NONE):
    700		if (bch->state == ISDN_P_NONE)
    701			return 0; /* already in idle state */
    702		bch->state = ISDN_P_NONE;
    703		clear_bit(FLG_HDLC, &bch->Flags);
    704		clear_bit(FLG_TRANSPARENT, &bch->Flags);
    705		break;
    706	case (ISDN_P_B_RAW):
    707		conhdlc |= 2;
    708		bch->state = protocol;
    709		set_bit(FLG_TRANSPARENT, &bch->Flags);
    710		break;
    711	case (ISDN_P_B_HDLC):
    712		bch->state = protocol;
    713		set_bit(FLG_HDLC, &bch->Flags);
    714		break;
    715	default:
    716		if (debug & DEBUG_HW)
    717			printk(KERN_DEBUG "%s: %s: prot not known %x\n",
    718			       hw->name, __func__, protocol);
    719		return -ENOPROTOOPT;
    720	}
    721
    722	if (protocol >= ISDN_P_NONE) {
    723		write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 0 : 2);
    724		write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
    725		write_reg(hw, HFCUSB_INC_RES_F, 2);
    726		write_reg(hw, HFCUSB_FIFO, (bch->nr == 1) ? 1 : 3);
    727		write_reg(hw, HFCUSB_CON_HDLC, conhdlc);
    728		write_reg(hw, HFCUSB_INC_RES_F, 2);
    729
    730		sctrl = 0x40 + ((hw->protocol == ISDN_P_TE_S0) ? 0x00 : 0x04);
    731		sctrl_r = 0x0;
    732		if (test_bit(FLG_ACTIVE, &hw->bch[0].Flags)) {
    733			sctrl |= 1;
    734			sctrl_r |= 1;
    735		}
    736		if (test_bit(FLG_ACTIVE, &hw->bch[1].Flags)) {
    737			sctrl |= 2;
    738			sctrl_r |= 2;
    739		}
    740		write_reg(hw, HFCUSB_SCTRL, sctrl);
    741		write_reg(hw, HFCUSB_SCTRL_R, sctrl_r);
    742
    743		if (protocol > ISDN_P_NONE)
    744			handle_led(hw, (bch->nr == 1) ? LED_B1_ON : LED_B2_ON);
    745		else
    746			handle_led(hw, (bch->nr == 1) ? LED_B1_OFF :
    747				   LED_B2_OFF);
    748	}
    749	return hfcsusb_ph_info(hw);
    750}
    751
    752static void
    753hfcsusb_ph_command(struct hfcsusb *hw, u_char command)
    754{
    755	if (debug & DEBUG_HW)
    756		printk(KERN_DEBUG "%s: %s: %x\n",
    757		       hw->name, __func__, command);
    758
    759	switch (command) {
    760	case HFC_L1_ACTIVATE_TE:
    761		/* force sending sending INFO1 */
    762		write_reg(hw, HFCUSB_STATES, 0x14);
    763		/* start l1 activation */
    764		write_reg(hw, HFCUSB_STATES, 0x04);
    765		break;
    766
    767	case HFC_L1_FORCE_DEACTIVATE_TE:
    768		write_reg(hw, HFCUSB_STATES, 0x10);
    769		write_reg(hw, HFCUSB_STATES, 0x03);
    770		break;
    771
    772	case HFC_L1_ACTIVATE_NT:
    773		if (hw->dch.state == 3)
    774			_queue_data(&hw->dch.dev.D, PH_ACTIVATE_IND,
    775				    MISDN_ID_ANY, 0, NULL, GFP_ATOMIC);
    776		else
    777			write_reg(hw, HFCUSB_STATES, HFCUSB_ACTIVATE |
    778				  HFCUSB_DO_ACTION | HFCUSB_NT_G2_G3);
    779		break;
    780
    781	case HFC_L1_DEACTIVATE_NT:
    782		write_reg(hw, HFCUSB_STATES,
    783			  HFCUSB_DO_ACTION);
    784		break;
    785	}
    786}
    787
    788/*
    789 * Layer 1 B-channel hardware access
    790 */
    791static int
    792channel_bctrl(struct bchannel *bch, struct mISDN_ctrl_req *cq)
    793{
    794	return mISDN_ctrl_bchannel(bch, cq);
    795}
    796
    797/* collect data from incoming interrupt or isochron USB data */
    798static void
    799hfcsusb_rx_frame(struct usb_fifo *fifo, __u8 *data, unsigned int len,
    800		 int finish)
    801{
    802	struct hfcsusb	*hw = fifo->hw;
    803	struct sk_buff	*rx_skb = NULL;
    804	int		maxlen = 0;
    805	int		fifon = fifo->fifonum;
    806	int		i;
    807	int		hdlc = 0;
    808	unsigned long	flags;
    809
    810	if (debug & DBG_HFC_CALL_TRACE)
    811		printk(KERN_DEBUG "%s: %s: fifo(%i) len(%i) "
    812		       "dch(%p) bch(%p) ech(%p)\n",
    813		       hw->name, __func__, fifon, len,
    814		       fifo->dch, fifo->bch, fifo->ech);
    815
    816	if (!len)
    817		return;
    818
    819	if ((!!fifo->dch + !!fifo->bch + !!fifo->ech) != 1) {
    820		printk(KERN_DEBUG "%s: %s: undefined channel\n",
    821		       hw->name, __func__);
    822		return;
    823	}
    824
    825	spin_lock_irqsave(&hw->lock, flags);
    826	if (fifo->dch) {
    827		rx_skb = fifo->dch->rx_skb;
    828		maxlen = fifo->dch->maxlen;
    829		hdlc = 1;
    830	}
    831	if (fifo->bch) {
    832		if (test_bit(FLG_RX_OFF, &fifo->bch->Flags)) {
    833			fifo->bch->dropcnt += len;
    834			spin_unlock_irqrestore(&hw->lock, flags);
    835			return;
    836		}
    837		maxlen = bchannel_get_rxbuf(fifo->bch, len);
    838		rx_skb = fifo->bch->rx_skb;
    839		if (maxlen < 0) {
    840			if (rx_skb)
    841				skb_trim(rx_skb, 0);
    842			pr_warn("%s.B%d: No bufferspace for %d bytes\n",
    843				hw->name, fifo->bch->nr, len);
    844			spin_unlock_irqrestore(&hw->lock, flags);
    845			return;
    846		}
    847		maxlen = fifo->bch->maxlen;
    848		hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
    849	}
    850	if (fifo->ech) {
    851		rx_skb = fifo->ech->rx_skb;
    852		maxlen = fifo->ech->maxlen;
    853		hdlc = 1;
    854	}
    855
    856	if (fifo->dch || fifo->ech) {
    857		if (!rx_skb) {
    858			rx_skb = mI_alloc_skb(maxlen, GFP_ATOMIC);
    859			if (rx_skb) {
    860				if (fifo->dch)
    861					fifo->dch->rx_skb = rx_skb;
    862				if (fifo->ech)
    863					fifo->ech->rx_skb = rx_skb;
    864				skb_trim(rx_skb, 0);
    865			} else {
    866				printk(KERN_DEBUG "%s: %s: No mem for rx_skb\n",
    867				       hw->name, __func__);
    868				spin_unlock_irqrestore(&hw->lock, flags);
    869				return;
    870			}
    871		}
    872		/* D/E-Channel SKB range check */
    873		if ((rx_skb->len + len) >= MAX_DFRAME_LEN_L1) {
    874			printk(KERN_DEBUG "%s: %s: sbk mem exceeded "
    875			       "for fifo(%d) HFCUSB_D_RX\n",
    876			       hw->name, __func__, fifon);
    877			skb_trim(rx_skb, 0);
    878			spin_unlock_irqrestore(&hw->lock, flags);
    879			return;
    880		}
    881	}
    882
    883	skb_put_data(rx_skb, data, len);
    884
    885	if (hdlc) {
    886		/* we have a complete hdlc packet */
    887		if (finish) {
    888			if ((rx_skb->len > 3) &&
    889			    (!(rx_skb->data[rx_skb->len - 1]))) {
    890				if (debug & DBG_HFC_FIFO_VERBOSE) {
    891					printk(KERN_DEBUG "%s: %s: fifon(%i)"
    892					       " new RX len(%i): ",
    893					       hw->name, __func__, fifon,
    894					       rx_skb->len);
    895					i = 0;
    896					while (i < rx_skb->len)
    897						printk("%02x ",
    898						       rx_skb->data[i++]);
    899					printk("\n");
    900				}
    901
    902				/* remove CRC & status */
    903				skb_trim(rx_skb, rx_skb->len - 3);
    904
    905				if (fifo->dch)
    906					recv_Dchannel(fifo->dch);
    907				if (fifo->bch)
    908					recv_Bchannel(fifo->bch, MISDN_ID_ANY,
    909						      0);
    910				if (fifo->ech)
    911					recv_Echannel(fifo->ech,
    912						      &hw->dch);
    913			} else {
    914				if (debug & DBG_HFC_FIFO_VERBOSE) {
    915					printk(KERN_DEBUG
    916					       "%s: CRC or minlen ERROR fifon(%i) "
    917					       "RX len(%i): ",
    918					       hw->name, fifon, rx_skb->len);
    919					i = 0;
    920					while (i < rx_skb->len)
    921						printk("%02x ",
    922						       rx_skb->data[i++]);
    923					printk("\n");
    924				}
    925				skb_trim(rx_skb, 0);
    926			}
    927		}
    928	} else {
    929		/* deliver transparent data to layer2 */
    930		recv_Bchannel(fifo->bch, MISDN_ID_ANY, false);
    931	}
    932	spin_unlock_irqrestore(&hw->lock, flags);
    933}
    934
    935static void
    936fill_isoc_urb(struct urb *urb, struct usb_device *dev, unsigned int pipe,
    937	      void *buf, int num_packets, int packet_size, int interval,
    938	      usb_complete_t complete, void *context)
    939{
    940	int k;
    941
    942	usb_fill_bulk_urb(urb, dev, pipe, buf, packet_size * num_packets,
    943			  complete, context);
    944
    945	urb->number_of_packets = num_packets;
    946	urb->transfer_flags = URB_ISO_ASAP;
    947	urb->actual_length = 0;
    948	urb->interval = interval;
    949
    950	for (k = 0; k < num_packets; k++) {
    951		urb->iso_frame_desc[k].offset = packet_size * k;
    952		urb->iso_frame_desc[k].length = packet_size;
    953		urb->iso_frame_desc[k].actual_length = 0;
    954	}
    955}
    956
    957/* receive completion routine for all ISO tx fifos   */
    958static void
    959rx_iso_complete(struct urb *urb)
    960{
    961	struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
    962	struct usb_fifo *fifo = context_iso_urb->owner_fifo;
    963	struct hfcsusb *hw = fifo->hw;
    964	int k, len, errcode, offset, num_isoc_packets, fifon, maxlen,
    965		status, iso_status, i;
    966	__u8 *buf;
    967	static __u8 eof[8];
    968	__u8 s0_state;
    969	unsigned long flags;
    970
    971	fifon = fifo->fifonum;
    972	status = urb->status;
    973
    974	spin_lock_irqsave(&hw->lock, flags);
    975	if (fifo->stop_gracefull) {
    976		fifo->stop_gracefull = 0;
    977		fifo->active = 0;
    978		spin_unlock_irqrestore(&hw->lock, flags);
    979		return;
    980	}
    981	spin_unlock_irqrestore(&hw->lock, flags);
    982
    983	/*
    984	 * ISO transfer only partially completed,
    985	 * look at individual frame status for details
    986	 */
    987	if (status == -EXDEV) {
    988		if (debug & DEBUG_HW)
    989			printk(KERN_DEBUG "%s: %s: with -EXDEV "
    990			       "urb->status %d, fifonum %d\n",
    991			       hw->name, __func__,  status, fifon);
    992
    993		/* clear status, so go on with ISO transfers */
    994		status = 0;
    995	}
    996
    997	s0_state = 0;
    998	if (fifo->active && !status) {
    999		num_isoc_packets = iso_packets[fifon];
   1000		maxlen = fifo->usb_packet_maxlen;
   1001
   1002		for (k = 0; k < num_isoc_packets; ++k) {
   1003			len = urb->iso_frame_desc[k].actual_length;
   1004			offset = urb->iso_frame_desc[k].offset;
   1005			buf = context_iso_urb->buffer + offset;
   1006			iso_status = urb->iso_frame_desc[k].status;
   1007
   1008			if (iso_status && (debug & DBG_HFC_FIFO_VERBOSE)) {
   1009				printk(KERN_DEBUG "%s: %s: "
   1010				       "ISO packet %i, status: %i\n",
   1011				       hw->name, __func__, k, iso_status);
   1012			}
   1013
   1014			/* USB data log for every D ISO in */
   1015			if ((fifon == HFCUSB_D_RX) &&
   1016			    (debug & DBG_HFC_USB_VERBOSE)) {
   1017				printk(KERN_DEBUG
   1018				       "%s: %s: %d (%d/%d) len(%d) ",
   1019				       hw->name, __func__, urb->start_frame,
   1020				       k, num_isoc_packets - 1,
   1021				       len);
   1022				for (i = 0; i < len; i++)
   1023					printk("%x ", buf[i]);
   1024				printk("\n");
   1025			}
   1026
   1027			if (!iso_status) {
   1028				if (fifo->last_urblen != maxlen) {
   1029					/*
   1030					 * save fifo fill-level threshold bits
   1031					 * to use them later in TX ISO URB
   1032					 * completions
   1033					 */
   1034					hw->threshold_mask = buf[1];
   1035
   1036					if (fifon == HFCUSB_D_RX)
   1037						s0_state = (buf[0] >> 4);
   1038
   1039					eof[fifon] = buf[0] & 1;
   1040					if (len > 2)
   1041						hfcsusb_rx_frame(fifo, buf + 2,
   1042								 len - 2, (len < maxlen)
   1043								 ? eof[fifon] : 0);
   1044				} else
   1045					hfcsusb_rx_frame(fifo, buf, len,
   1046							 (len < maxlen) ?
   1047							 eof[fifon] : 0);
   1048				fifo->last_urblen = len;
   1049			}
   1050		}
   1051
   1052		/* signal S0 layer1 state change */
   1053		if ((s0_state) && (hw->initdone) &&
   1054		    (s0_state != hw->dch.state)) {
   1055			hw->dch.state = s0_state;
   1056			schedule_event(&hw->dch, FLG_PHCHANGE);
   1057		}
   1058
   1059		fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
   1060			      context_iso_urb->buffer, num_isoc_packets,
   1061			      fifo->usb_packet_maxlen, fifo->intervall,
   1062			      (usb_complete_t)rx_iso_complete, urb->context);
   1063		errcode = usb_submit_urb(urb, GFP_ATOMIC);
   1064		if (errcode < 0) {
   1065			if (debug & DEBUG_HW)
   1066				printk(KERN_DEBUG "%s: %s: error submitting "
   1067				       "ISO URB: %d\n",
   1068				       hw->name, __func__, errcode);
   1069		}
   1070	} else {
   1071		if (status && (debug & DBG_HFC_URB_INFO))
   1072			printk(KERN_DEBUG "%s: %s: rx_iso_complete : "
   1073			       "urb->status %d, fifonum %d\n",
   1074			       hw->name, __func__, status, fifon);
   1075	}
   1076}
   1077
   1078/* receive completion routine for all interrupt rx fifos */
   1079static void
   1080rx_int_complete(struct urb *urb)
   1081{
   1082	int len, status, i;
   1083	__u8 *buf, maxlen, fifon;
   1084	struct usb_fifo *fifo = (struct usb_fifo *) urb->context;
   1085	struct hfcsusb *hw = fifo->hw;
   1086	static __u8 eof[8];
   1087	unsigned long flags;
   1088
   1089	spin_lock_irqsave(&hw->lock, flags);
   1090	if (fifo->stop_gracefull) {
   1091		fifo->stop_gracefull = 0;
   1092		fifo->active = 0;
   1093		spin_unlock_irqrestore(&hw->lock, flags);
   1094		return;
   1095	}
   1096	spin_unlock_irqrestore(&hw->lock, flags);
   1097
   1098	fifon = fifo->fifonum;
   1099	if ((!fifo->active) || (urb->status)) {
   1100		if (debug & DBG_HFC_URB_ERROR)
   1101			printk(KERN_DEBUG
   1102			       "%s: %s: RX-Fifo %i is going down (%i)\n",
   1103			       hw->name, __func__, fifon, urb->status);
   1104
   1105		fifo->urb->interval = 0; /* cancel automatic rescheduling */
   1106		return;
   1107	}
   1108	len = urb->actual_length;
   1109	buf = fifo->buffer;
   1110	maxlen = fifo->usb_packet_maxlen;
   1111
   1112	/* USB data log for every D INT in */
   1113	if ((fifon == HFCUSB_D_RX) && (debug & DBG_HFC_USB_VERBOSE)) {
   1114		printk(KERN_DEBUG "%s: %s: D RX INT len(%d) ",
   1115		       hw->name, __func__, len);
   1116		for (i = 0; i < len; i++)
   1117			printk("%02x ", buf[i]);
   1118		printk("\n");
   1119	}
   1120
   1121	if (fifo->last_urblen != fifo->usb_packet_maxlen) {
   1122		/* the threshold mask is in the 2nd status byte */
   1123		hw->threshold_mask = buf[1];
   1124
   1125		/* signal S0 layer1 state change */
   1126		if (hw->initdone && ((buf[0] >> 4) != hw->dch.state)) {
   1127			hw->dch.state = (buf[0] >> 4);
   1128			schedule_event(&hw->dch, FLG_PHCHANGE);
   1129		}
   1130
   1131		eof[fifon] = buf[0] & 1;
   1132		/* if we have more than the 2 status bytes -> collect data */
   1133		if (len > 2)
   1134			hfcsusb_rx_frame(fifo, buf + 2,
   1135					 urb->actual_length - 2,
   1136					 (len < maxlen) ? eof[fifon] : 0);
   1137	} else {
   1138		hfcsusb_rx_frame(fifo, buf, urb->actual_length,
   1139				 (len < maxlen) ? eof[fifon] : 0);
   1140	}
   1141	fifo->last_urblen = urb->actual_length;
   1142
   1143	status = usb_submit_urb(urb, GFP_ATOMIC);
   1144	if (status) {
   1145		if (debug & DEBUG_HW)
   1146			printk(KERN_DEBUG "%s: %s: error resubmitting USB\n",
   1147			       hw->name, __func__);
   1148	}
   1149}
   1150
   1151/* transmit completion routine for all ISO tx fifos */
   1152static void
   1153tx_iso_complete(struct urb *urb)
   1154{
   1155	struct iso_urb *context_iso_urb = (struct iso_urb *) urb->context;
   1156	struct usb_fifo *fifo = context_iso_urb->owner_fifo;
   1157	struct hfcsusb *hw = fifo->hw;
   1158	struct sk_buff *tx_skb;
   1159	int k, tx_offset, num_isoc_packets, sink, remain, current_len,
   1160		errcode, hdlc, i;
   1161	int *tx_idx;
   1162	int frame_complete, fifon, status, fillempty = 0;
   1163	__u8 threshbit, *p;
   1164	unsigned long flags;
   1165
   1166	spin_lock_irqsave(&hw->lock, flags);
   1167	if (fifo->stop_gracefull) {
   1168		fifo->stop_gracefull = 0;
   1169		fifo->active = 0;
   1170		spin_unlock_irqrestore(&hw->lock, flags);
   1171		return;
   1172	}
   1173
   1174	if (fifo->dch) {
   1175		tx_skb = fifo->dch->tx_skb;
   1176		tx_idx = &fifo->dch->tx_idx;
   1177		hdlc = 1;
   1178	} else if (fifo->bch) {
   1179		tx_skb = fifo->bch->tx_skb;
   1180		tx_idx = &fifo->bch->tx_idx;
   1181		hdlc = test_bit(FLG_HDLC, &fifo->bch->Flags);
   1182		if (!tx_skb && !hdlc &&
   1183		    test_bit(FLG_FILLEMPTY, &fifo->bch->Flags))
   1184			fillempty = 1;
   1185	} else {
   1186		printk(KERN_DEBUG "%s: %s: neither BCH nor DCH\n",
   1187		       hw->name, __func__);
   1188		spin_unlock_irqrestore(&hw->lock, flags);
   1189		return;
   1190	}
   1191
   1192	fifon = fifo->fifonum;
   1193	status = urb->status;
   1194
   1195	tx_offset = 0;
   1196
   1197	/*
   1198	 * ISO transfer only partially completed,
   1199	 * look at individual frame status for details
   1200	 */
   1201	if (status == -EXDEV) {
   1202		if (debug & DBG_HFC_URB_ERROR)
   1203			printk(KERN_DEBUG "%s: %s: "
   1204			       "-EXDEV (%i) fifon (%d)\n",
   1205			       hw->name, __func__, status, fifon);
   1206
   1207		/* clear status, so go on with ISO transfers */
   1208		status = 0;
   1209	}
   1210
   1211	if (fifo->active && !status) {
   1212		/* is FifoFull-threshold set for our channel? */
   1213		threshbit = (hw->threshold_mask & (1 << fifon));
   1214		num_isoc_packets = iso_packets[fifon];
   1215
   1216		/* predict dataflow to avoid fifo overflow */
   1217		if (fifon >= HFCUSB_D_TX)
   1218			sink = (threshbit) ? SINK_DMIN : SINK_DMAX;
   1219		else
   1220			sink = (threshbit) ? SINK_MIN : SINK_MAX;
   1221		fill_isoc_urb(urb, fifo->hw->dev, fifo->pipe,
   1222			      context_iso_urb->buffer, num_isoc_packets,
   1223			      fifo->usb_packet_maxlen, fifo->intervall,
   1224			      (usb_complete_t)tx_iso_complete, urb->context);
   1225		memset(context_iso_urb->buffer, 0,
   1226		       sizeof(context_iso_urb->buffer));
   1227		frame_complete = 0;
   1228
   1229		for (k = 0; k < num_isoc_packets; ++k) {
   1230			/* analyze tx success of previous ISO packets */
   1231			if (debug & DBG_HFC_URB_ERROR) {
   1232				errcode = urb->iso_frame_desc[k].status;
   1233				if (errcode) {
   1234					printk(KERN_DEBUG "%s: %s: "
   1235					       "ISO packet %i, status: %i\n",
   1236					       hw->name, __func__, k, errcode);
   1237				}
   1238			}
   1239
   1240			/* Generate next ISO Packets */
   1241			if (tx_skb)
   1242				remain = tx_skb->len - *tx_idx;
   1243			else if (fillempty)
   1244				remain = 15; /* > not complete */
   1245			else
   1246				remain = 0;
   1247
   1248			if (remain > 0) {
   1249				fifo->bit_line -= sink;
   1250				current_len = (0 - fifo->bit_line) / 8;
   1251				if (current_len > 14)
   1252					current_len = 14;
   1253				if (current_len < 0)
   1254					current_len = 0;
   1255				if (remain < current_len)
   1256					current_len = remain;
   1257
   1258				/* how much bit do we put on the line? */
   1259				fifo->bit_line += current_len * 8;
   1260
   1261				context_iso_urb->buffer[tx_offset] = 0;
   1262				if (current_len == remain) {
   1263					if (hdlc) {
   1264						/* signal frame completion */
   1265						context_iso_urb->
   1266							buffer[tx_offset] = 1;
   1267						/* add 2 byte flags and 16bit
   1268						 * CRC at end of ISDN frame */
   1269						fifo->bit_line += 32;
   1270					}
   1271					frame_complete = 1;
   1272				}
   1273
   1274				/* copy tx data to iso-urb buffer */
   1275				p = context_iso_urb->buffer + tx_offset + 1;
   1276				if (fillempty) {
   1277					memset(p, fifo->bch->fill[0],
   1278					       current_len);
   1279				} else {
   1280					memcpy(p, (tx_skb->data + *tx_idx),
   1281					       current_len);
   1282					*tx_idx += current_len;
   1283				}
   1284				urb->iso_frame_desc[k].offset = tx_offset;
   1285				urb->iso_frame_desc[k].length = current_len + 1;
   1286
   1287				/* USB data log for every D ISO out */
   1288				if ((fifon == HFCUSB_D_RX) && !fillempty &&
   1289				    (debug & DBG_HFC_USB_VERBOSE)) {
   1290					printk(KERN_DEBUG
   1291					       "%s: %s (%d/%d) offs(%d) len(%d) ",
   1292					       hw->name, __func__,
   1293					       k, num_isoc_packets - 1,
   1294					       urb->iso_frame_desc[k].offset,
   1295					       urb->iso_frame_desc[k].length);
   1296
   1297					for (i = urb->iso_frame_desc[k].offset;
   1298					     i < (urb->iso_frame_desc[k].offset
   1299						  + urb->iso_frame_desc[k].length);
   1300					     i++)
   1301						printk("%x ",
   1302						       context_iso_urb->buffer[i]);
   1303
   1304					printk(" skb->len(%i) tx-idx(%d)\n",
   1305					       tx_skb->len, *tx_idx);
   1306				}
   1307
   1308				tx_offset += (current_len + 1);
   1309			} else {
   1310				urb->iso_frame_desc[k].offset = tx_offset++;
   1311				urb->iso_frame_desc[k].length = 1;
   1312				/* we lower data margin every msec */
   1313				fifo->bit_line -= sink;
   1314				if (fifo->bit_line < BITLINE_INF)
   1315					fifo->bit_line = BITLINE_INF;
   1316			}
   1317
   1318			if (frame_complete) {
   1319				frame_complete = 0;
   1320
   1321				if (debug & DBG_HFC_FIFO_VERBOSE) {
   1322					printk(KERN_DEBUG  "%s: %s: "
   1323					       "fifon(%i) new TX len(%i): ",
   1324					       hw->name, __func__,
   1325					       fifon, tx_skb->len);
   1326					i = 0;
   1327					while (i < tx_skb->len)
   1328						printk("%02x ",
   1329						       tx_skb->data[i++]);
   1330					printk("\n");
   1331				}
   1332
   1333				dev_kfree_skb(tx_skb);
   1334				tx_skb = NULL;
   1335				if (fifo->dch && get_next_dframe(fifo->dch))
   1336					tx_skb = fifo->dch->tx_skb;
   1337				else if (fifo->bch &&
   1338					 get_next_bframe(fifo->bch))
   1339					tx_skb = fifo->bch->tx_skb;
   1340			}
   1341		}
   1342		errcode = usb_submit_urb(urb, GFP_ATOMIC);
   1343		if (errcode < 0) {
   1344			if (debug & DEBUG_HW)
   1345				printk(KERN_DEBUG
   1346				       "%s: %s: error submitting ISO URB: %d \n",
   1347				       hw->name, __func__, errcode);
   1348		}
   1349
   1350		/*
   1351		 * abuse DChannel tx iso completion to trigger NT mode state
   1352		 * changes tx_iso_complete is assumed to be called every
   1353		 * fifo->intervall (ms)
   1354		 */
   1355		if ((fifon == HFCUSB_D_TX) && (hw->protocol == ISDN_P_NT_S0)
   1356		    && (hw->timers & NT_ACTIVATION_TIMER)) {
   1357			if ((--hw->nt_timer) < 0)
   1358				schedule_event(&hw->dch, FLG_PHCHANGE);
   1359		}
   1360
   1361	} else {
   1362		if (status && (debug & DBG_HFC_URB_ERROR))
   1363			printk(KERN_DEBUG  "%s: %s: urb->status %s (%i)"
   1364			       "fifonum=%d\n",
   1365			       hw->name, __func__,
   1366			       symbolic(urb_errlist, status), status, fifon);
   1367	}
   1368	spin_unlock_irqrestore(&hw->lock, flags);
   1369}
   1370
   1371/*
   1372 * allocs urbs and start isoc transfer with two pending urbs to avoid
   1373 * gaps in the transfer chain
   1374 */
   1375static int
   1376start_isoc_chain(struct usb_fifo *fifo, int num_packets_per_urb,
   1377		 usb_complete_t complete, int packet_size)
   1378{
   1379	struct hfcsusb *hw = fifo->hw;
   1380	int i, k, errcode;
   1381
   1382	if (debug)
   1383		printk(KERN_DEBUG "%s: %s: fifo %i\n",
   1384		       hw->name, __func__, fifo->fifonum);
   1385
   1386	/* allocate Memory for Iso out Urbs */
   1387	for (i = 0; i < 2; i++) {
   1388		if (!(fifo->iso[i].urb)) {
   1389			fifo->iso[i].urb =
   1390				usb_alloc_urb(num_packets_per_urb, GFP_KERNEL);
   1391			if (!(fifo->iso[i].urb)) {
   1392				printk(KERN_DEBUG
   1393				       "%s: %s: alloc urb for fifo %i failed",
   1394				       hw->name, __func__, fifo->fifonum);
   1395				continue;
   1396			}
   1397			fifo->iso[i].owner_fifo = (struct usb_fifo *) fifo;
   1398			fifo->iso[i].indx = i;
   1399
   1400			/* Init the first iso */
   1401			if (ISO_BUFFER_SIZE >=
   1402			    (fifo->usb_packet_maxlen *
   1403			     num_packets_per_urb)) {
   1404				fill_isoc_urb(fifo->iso[i].urb,
   1405					      fifo->hw->dev, fifo->pipe,
   1406					      fifo->iso[i].buffer,
   1407					      num_packets_per_urb,
   1408					      fifo->usb_packet_maxlen,
   1409					      fifo->intervall, complete,
   1410					      &fifo->iso[i]);
   1411				memset(fifo->iso[i].buffer, 0,
   1412				       sizeof(fifo->iso[i].buffer));
   1413
   1414				for (k = 0; k < num_packets_per_urb; k++) {
   1415					fifo->iso[i].urb->
   1416						iso_frame_desc[k].offset =
   1417						k * packet_size;
   1418					fifo->iso[i].urb->
   1419						iso_frame_desc[k].length =
   1420						packet_size;
   1421				}
   1422			} else {
   1423				printk(KERN_DEBUG
   1424				       "%s: %s: ISO Buffer size to small!\n",
   1425				       hw->name, __func__);
   1426			}
   1427		}
   1428		fifo->bit_line = BITLINE_INF;
   1429
   1430		errcode = usb_submit_urb(fifo->iso[i].urb, GFP_KERNEL);
   1431		fifo->active = (errcode >= 0) ? 1 : 0;
   1432		fifo->stop_gracefull = 0;
   1433		if (errcode < 0) {
   1434			printk(KERN_DEBUG "%s: %s: %s URB nr:%d\n",
   1435			       hw->name, __func__,
   1436			       symbolic(urb_errlist, errcode), i);
   1437		}
   1438	}
   1439	return fifo->active;
   1440}
   1441
   1442static void
   1443stop_iso_gracefull(struct usb_fifo *fifo)
   1444{
   1445	struct hfcsusb *hw = fifo->hw;
   1446	int i, timeout;
   1447	u_long flags;
   1448
   1449	for (i = 0; i < 2; i++) {
   1450		spin_lock_irqsave(&hw->lock, flags);
   1451		if (debug)
   1452			printk(KERN_DEBUG "%s: %s for fifo %i.%i\n",
   1453			       hw->name, __func__, fifo->fifonum, i);
   1454		fifo->stop_gracefull = 1;
   1455		spin_unlock_irqrestore(&hw->lock, flags);
   1456	}
   1457
   1458	for (i = 0; i < 2; i++) {
   1459		timeout = 3;
   1460		while (fifo->stop_gracefull && timeout--)
   1461			schedule_timeout_interruptible((HZ / 1000) * 16);
   1462		if (debug && fifo->stop_gracefull)
   1463			printk(KERN_DEBUG "%s: ERROR %s for fifo %i.%i\n",
   1464			       hw->name, __func__, fifo->fifonum, i);
   1465	}
   1466}
   1467
   1468static void
   1469stop_int_gracefull(struct usb_fifo *fifo)
   1470{
   1471	struct hfcsusb *hw = fifo->hw;
   1472	int timeout;
   1473	u_long flags;
   1474
   1475	spin_lock_irqsave(&hw->lock, flags);
   1476	if (debug)
   1477		printk(KERN_DEBUG "%s: %s for fifo %i\n",
   1478		       hw->name, __func__, fifo->fifonum);
   1479	fifo->stop_gracefull = 1;
   1480	spin_unlock_irqrestore(&hw->lock, flags);
   1481
   1482	timeout = 3;
   1483	while (fifo->stop_gracefull && timeout--)
   1484		schedule_timeout_interruptible((HZ / 1000) * 3);
   1485	if (debug && fifo->stop_gracefull)
   1486		printk(KERN_DEBUG "%s: ERROR %s for fifo %i\n",
   1487		       hw->name, __func__, fifo->fifonum);
   1488}
   1489
   1490/* start the interrupt transfer for the given fifo */
   1491static void
   1492start_int_fifo(struct usb_fifo *fifo)
   1493{
   1494	struct hfcsusb *hw = fifo->hw;
   1495	int errcode;
   1496
   1497	if (debug)
   1498		printk(KERN_DEBUG "%s: %s: INT IN fifo:%d\n",
   1499		       hw->name, __func__, fifo->fifonum);
   1500
   1501	if (!fifo->urb) {
   1502		fifo->urb = usb_alloc_urb(0, GFP_KERNEL);
   1503		if (!fifo->urb)
   1504			return;
   1505	}
   1506	usb_fill_int_urb(fifo->urb, fifo->hw->dev, fifo->pipe,
   1507			 fifo->buffer, fifo->usb_packet_maxlen,
   1508			 (usb_complete_t)rx_int_complete, fifo, fifo->intervall);
   1509	fifo->active = 1;
   1510	fifo->stop_gracefull = 0;
   1511	errcode = usb_submit_urb(fifo->urb, GFP_KERNEL);
   1512	if (errcode) {
   1513		printk(KERN_DEBUG "%s: %s: submit URB: status:%i\n",
   1514		       hw->name, __func__, errcode);
   1515		fifo->active = 0;
   1516	}
   1517}
   1518
   1519static void
   1520setPortMode(struct hfcsusb *hw)
   1521{
   1522	if (debug & DEBUG_HW)
   1523		printk(KERN_DEBUG "%s: %s %s\n", hw->name, __func__,
   1524		       (hw->protocol == ISDN_P_TE_S0) ? "TE" : "NT");
   1525
   1526	if (hw->protocol == ISDN_P_TE_S0) {
   1527		write_reg(hw, HFCUSB_SCTRL, 0x40);
   1528		write_reg(hw, HFCUSB_SCTRL_E, 0x00);
   1529		write_reg(hw, HFCUSB_CLKDEL, CLKDEL_TE);
   1530		write_reg(hw, HFCUSB_STATES, 3 | 0x10);
   1531		write_reg(hw, HFCUSB_STATES, 3);
   1532	} else {
   1533		write_reg(hw, HFCUSB_SCTRL, 0x44);
   1534		write_reg(hw, HFCUSB_SCTRL_E, 0x09);
   1535		write_reg(hw, HFCUSB_CLKDEL, CLKDEL_NT);
   1536		write_reg(hw, HFCUSB_STATES, 1 | 0x10);
   1537		write_reg(hw, HFCUSB_STATES, 1);
   1538	}
   1539}
   1540
   1541static void
   1542reset_hfcsusb(struct hfcsusb *hw)
   1543{
   1544	struct usb_fifo *fifo;
   1545	int i;
   1546
   1547	if (debug & DEBUG_HW)
   1548		printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
   1549
   1550	/* do Chip reset */
   1551	write_reg(hw, HFCUSB_CIRM, 8);
   1552
   1553	/* aux = output, reset off */
   1554	write_reg(hw, HFCUSB_CIRM, 0x10);
   1555
   1556	/* set USB_SIZE to match the wMaxPacketSize for INT or BULK transfers */
   1557	write_reg(hw, HFCUSB_USB_SIZE, (hw->packet_size / 8) |
   1558		  ((hw->packet_size / 8) << 4));
   1559
   1560	/* set USB_SIZE_I to match the the wMaxPacketSize for ISO transfers */
   1561	write_reg(hw, HFCUSB_USB_SIZE_I, hw->iso_packet_size);
   1562
   1563	/* enable PCM/GCI master mode */
   1564	write_reg(hw, HFCUSB_MST_MODE1, 0);	/* set default values */
   1565	write_reg(hw, HFCUSB_MST_MODE0, 1);	/* enable master mode */
   1566
   1567	/* init the fifos */
   1568	write_reg(hw, HFCUSB_F_THRES,
   1569		  (HFCUSB_TX_THRESHOLD / 8) | ((HFCUSB_RX_THRESHOLD / 8) << 4));
   1570
   1571	fifo = hw->fifos;
   1572	for (i = 0; i < HFCUSB_NUM_FIFOS; i++) {
   1573		write_reg(hw, HFCUSB_FIFO, i);	/* select the desired fifo */
   1574		fifo[i].max_size =
   1575			(i <= HFCUSB_B2_RX) ? MAX_BCH_SIZE : MAX_DFRAME_LEN;
   1576		fifo[i].last_urblen = 0;
   1577
   1578		/* set 2 bit for D- & E-channel */
   1579		write_reg(hw, HFCUSB_HDLC_PAR, ((i <= HFCUSB_B2_RX) ? 0 : 2));
   1580
   1581		/* enable all fifos */
   1582		if (i == HFCUSB_D_TX)
   1583			write_reg(hw, HFCUSB_CON_HDLC,
   1584				  (hw->protocol == ISDN_P_NT_S0) ? 0x08 : 0x09);
   1585		else
   1586			write_reg(hw, HFCUSB_CON_HDLC, 0x08);
   1587		write_reg(hw, HFCUSB_INC_RES_F, 2); /* reset the fifo */
   1588	}
   1589
   1590	write_reg(hw, HFCUSB_SCTRL_R, 0); /* disable both B receivers */
   1591	handle_led(hw, LED_POWER_ON);
   1592}
   1593
   1594/* start USB data pipes dependand on device's endpoint configuration */
   1595static void
   1596hfcsusb_start_endpoint(struct hfcsusb *hw, int channel)
   1597{
   1598	/* quick check if endpoint already running */
   1599	if ((channel == HFC_CHAN_D) && (hw->fifos[HFCUSB_D_RX].active))
   1600		return;
   1601	if ((channel == HFC_CHAN_B1) && (hw->fifos[HFCUSB_B1_RX].active))
   1602		return;
   1603	if ((channel == HFC_CHAN_B2) && (hw->fifos[HFCUSB_B2_RX].active))
   1604		return;
   1605	if ((channel == HFC_CHAN_E) && (hw->fifos[HFCUSB_PCM_RX].active))
   1606		return;
   1607
   1608	/* start rx endpoints using USB INT IN method */
   1609	if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
   1610		start_int_fifo(hw->fifos + channel * 2 + 1);
   1611
   1612	/* start rx endpoints using USB ISO IN method */
   1613	if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO) {
   1614		switch (channel) {
   1615		case HFC_CHAN_D:
   1616			start_isoc_chain(hw->fifos + HFCUSB_D_RX,
   1617					 ISOC_PACKETS_D,
   1618					 (usb_complete_t)rx_iso_complete,
   1619					 16);
   1620			break;
   1621		case HFC_CHAN_E:
   1622			start_isoc_chain(hw->fifos + HFCUSB_PCM_RX,
   1623					 ISOC_PACKETS_D,
   1624					 (usb_complete_t)rx_iso_complete,
   1625					 16);
   1626			break;
   1627		case HFC_CHAN_B1:
   1628			start_isoc_chain(hw->fifos + HFCUSB_B1_RX,
   1629					 ISOC_PACKETS_B,
   1630					 (usb_complete_t)rx_iso_complete,
   1631					 16);
   1632			break;
   1633		case HFC_CHAN_B2:
   1634			start_isoc_chain(hw->fifos + HFCUSB_B2_RX,
   1635					 ISOC_PACKETS_B,
   1636					 (usb_complete_t)rx_iso_complete,
   1637					 16);
   1638			break;
   1639		}
   1640	}
   1641
   1642	/* start tx endpoints using USB ISO OUT method */
   1643	switch (channel) {
   1644	case HFC_CHAN_D:
   1645		start_isoc_chain(hw->fifos + HFCUSB_D_TX,
   1646				 ISOC_PACKETS_B,
   1647				 (usb_complete_t)tx_iso_complete, 1);
   1648		break;
   1649	case HFC_CHAN_B1:
   1650		start_isoc_chain(hw->fifos + HFCUSB_B1_TX,
   1651				 ISOC_PACKETS_D,
   1652				 (usb_complete_t)tx_iso_complete, 1);
   1653		break;
   1654	case HFC_CHAN_B2:
   1655		start_isoc_chain(hw->fifos + HFCUSB_B2_TX,
   1656				 ISOC_PACKETS_B,
   1657				 (usb_complete_t)tx_iso_complete, 1);
   1658		break;
   1659	}
   1660}
   1661
   1662/* stop USB data pipes dependand on device's endpoint configuration */
   1663static void
   1664hfcsusb_stop_endpoint(struct hfcsusb *hw, int channel)
   1665{
   1666	/* quick check if endpoint currently running */
   1667	if ((channel == HFC_CHAN_D) && (!hw->fifos[HFCUSB_D_RX].active))
   1668		return;
   1669	if ((channel == HFC_CHAN_B1) && (!hw->fifos[HFCUSB_B1_RX].active))
   1670		return;
   1671	if ((channel == HFC_CHAN_B2) && (!hw->fifos[HFCUSB_B2_RX].active))
   1672		return;
   1673	if ((channel == HFC_CHAN_E) && (!hw->fifos[HFCUSB_PCM_RX].active))
   1674		return;
   1675
   1676	/* rx endpoints using USB INT IN method */
   1677	if (hw->cfg_used == CNF_3INT3ISO || hw->cfg_used == CNF_4INT3ISO)
   1678		stop_int_gracefull(hw->fifos + channel * 2 + 1);
   1679
   1680	/* rx endpoints using USB ISO IN method */
   1681	if (hw->cfg_used == CNF_3ISO3ISO || hw->cfg_used == CNF_4ISO3ISO)
   1682		stop_iso_gracefull(hw->fifos + channel * 2 + 1);
   1683
   1684	/* tx endpoints using USB ISO OUT method */
   1685	if (channel != HFC_CHAN_E)
   1686		stop_iso_gracefull(hw->fifos + channel * 2);
   1687}
   1688
   1689
   1690/* Hardware Initialization */
   1691static int
   1692setup_hfcsusb(struct hfcsusb *hw)
   1693{
   1694	void *dmabuf = kmalloc(sizeof(u_char), GFP_KERNEL);
   1695	u_char b;
   1696	int ret;
   1697
   1698	if (debug & DBG_HFC_CALL_TRACE)
   1699		printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
   1700
   1701	if (!dmabuf)
   1702		return -ENOMEM;
   1703
   1704	ret = read_reg_atomic(hw, HFCUSB_CHIP_ID, dmabuf);
   1705
   1706	memcpy(&b, dmabuf, sizeof(u_char));
   1707	kfree(dmabuf);
   1708
   1709	/* check the chip id */
   1710	if (ret != 1) {
   1711		printk(KERN_DEBUG "%s: %s: cannot read chip id\n",
   1712		       hw->name, __func__);
   1713		return 1;
   1714	}
   1715	if (b != HFCUSB_CHIPID) {
   1716		printk(KERN_DEBUG "%s: %s: Invalid chip id 0x%02x\n",
   1717		       hw->name, __func__, b);
   1718		return 1;
   1719	}
   1720
   1721	/* first set the needed config, interface and alternate */
   1722	(void) usb_set_interface(hw->dev, hw->if_used, hw->alt_used);
   1723
   1724	hw->led_state = 0;
   1725
   1726	/* init the background machinery for control requests */
   1727	hw->ctrl_read.bRequestType = 0xc0;
   1728	hw->ctrl_read.bRequest = 1;
   1729	hw->ctrl_read.wLength = cpu_to_le16(1);
   1730	hw->ctrl_write.bRequestType = 0x40;
   1731	hw->ctrl_write.bRequest = 0;
   1732	hw->ctrl_write.wLength = 0;
   1733	usb_fill_control_urb(hw->ctrl_urb, hw->dev, hw->ctrl_out_pipe,
   1734			     (u_char *)&hw->ctrl_write, NULL, 0,
   1735			     (usb_complete_t)ctrl_complete, hw);
   1736
   1737	reset_hfcsusb(hw);
   1738	return 0;
   1739}
   1740
   1741static void
   1742release_hw(struct hfcsusb *hw)
   1743{
   1744	if (debug & DBG_HFC_CALL_TRACE)
   1745		printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
   1746
   1747	/*
   1748	 * stop all endpoints gracefully
   1749	 * TODO: mISDN_core should generate CLOSE_CHANNEL
   1750	 *       signals after calling mISDN_unregister_device()
   1751	 */
   1752	hfcsusb_stop_endpoint(hw, HFC_CHAN_D);
   1753	hfcsusb_stop_endpoint(hw, HFC_CHAN_B1);
   1754	hfcsusb_stop_endpoint(hw, HFC_CHAN_B2);
   1755	if (hw->fifos[HFCUSB_PCM_RX].pipe)
   1756		hfcsusb_stop_endpoint(hw, HFC_CHAN_E);
   1757	if (hw->protocol == ISDN_P_TE_S0)
   1758		l1_event(hw->dch.l1, CLOSE_CHANNEL);
   1759
   1760	mISDN_unregister_device(&hw->dch.dev);
   1761	mISDN_freebchannel(&hw->bch[1]);
   1762	mISDN_freebchannel(&hw->bch[0]);
   1763	mISDN_freedchannel(&hw->dch);
   1764
   1765	if (hw->ctrl_urb) {
   1766		usb_kill_urb(hw->ctrl_urb);
   1767		usb_free_urb(hw->ctrl_urb);
   1768		hw->ctrl_urb = NULL;
   1769	}
   1770
   1771	if (hw->intf)
   1772		usb_set_intfdata(hw->intf, NULL);
   1773	list_del(&hw->list);
   1774	kfree(hw);
   1775	hw = NULL;
   1776}
   1777
   1778static void
   1779deactivate_bchannel(struct bchannel *bch)
   1780{
   1781	struct hfcsusb *hw = bch->hw;
   1782	u_long flags;
   1783
   1784	if (bch->debug & DEBUG_HW)
   1785		printk(KERN_DEBUG "%s: %s: bch->nr(%i)\n",
   1786		       hw->name, __func__, bch->nr);
   1787
   1788	spin_lock_irqsave(&hw->lock, flags);
   1789	mISDN_clear_bchannel(bch);
   1790	spin_unlock_irqrestore(&hw->lock, flags);
   1791	hfcsusb_setup_bch(bch, ISDN_P_NONE);
   1792	hfcsusb_stop_endpoint(hw, bch->nr - 1);
   1793}
   1794
   1795/*
   1796 * Layer 1 B-channel hardware access
   1797 */
   1798static int
   1799hfc_bctrl(struct mISDNchannel *ch, u_int cmd, void *arg)
   1800{
   1801	struct bchannel	*bch = container_of(ch, struct bchannel, ch);
   1802	int		ret = -EINVAL;
   1803
   1804	if (bch->debug & DEBUG_HW)
   1805		printk(KERN_DEBUG "%s: cmd:%x %p\n", __func__, cmd, arg);
   1806
   1807	switch (cmd) {
   1808	case HW_TESTRX_RAW:
   1809	case HW_TESTRX_HDLC:
   1810	case HW_TESTRX_OFF:
   1811		ret = -EINVAL;
   1812		break;
   1813
   1814	case CLOSE_CHANNEL:
   1815		test_and_clear_bit(FLG_OPEN, &bch->Flags);
   1816		deactivate_bchannel(bch);
   1817		ch->protocol = ISDN_P_NONE;
   1818		ch->peer = NULL;
   1819		module_put(THIS_MODULE);
   1820		ret = 0;
   1821		break;
   1822	case CONTROL_CHANNEL:
   1823		ret = channel_bctrl(bch, arg);
   1824		break;
   1825	default:
   1826		printk(KERN_WARNING "%s: unknown prim(%x)\n",
   1827		       __func__, cmd);
   1828	}
   1829	return ret;
   1830}
   1831
   1832static int
   1833setup_instance(struct hfcsusb *hw, struct device *parent)
   1834{
   1835	u_long	flags;
   1836	int	err, i;
   1837
   1838	if (debug & DBG_HFC_CALL_TRACE)
   1839		printk(KERN_DEBUG "%s: %s\n", hw->name, __func__);
   1840
   1841	spin_lock_init(&hw->ctrl_lock);
   1842	spin_lock_init(&hw->lock);
   1843
   1844	mISDN_initdchannel(&hw->dch, MAX_DFRAME_LEN_L1, ph_state);
   1845	hw->dch.debug = debug & 0xFFFF;
   1846	hw->dch.hw = hw;
   1847	hw->dch.dev.Dprotocols = (1 << ISDN_P_TE_S0) | (1 << ISDN_P_NT_S0);
   1848	hw->dch.dev.D.send = hfcusb_l2l1D;
   1849	hw->dch.dev.D.ctrl = hfc_dctrl;
   1850
   1851	/* enable E-Channel logging */
   1852	if (hw->fifos[HFCUSB_PCM_RX].pipe)
   1853		mISDN_initdchannel(&hw->ech, MAX_DFRAME_LEN_L1, NULL);
   1854
   1855	hw->dch.dev.Bprotocols = (1 << (ISDN_P_B_RAW & ISDN_P_B_MASK)) |
   1856		(1 << (ISDN_P_B_HDLC & ISDN_P_B_MASK));
   1857	hw->dch.dev.nrbchan = 2;
   1858	for (i = 0; i < 2; i++) {
   1859		hw->bch[i].nr = i + 1;
   1860		set_channelmap(i + 1, hw->dch.dev.channelmap);
   1861		hw->bch[i].debug = debug;
   1862		mISDN_initbchannel(&hw->bch[i], MAX_DATA_MEM, poll >> 1);
   1863		hw->bch[i].hw = hw;
   1864		hw->bch[i].ch.send = hfcusb_l2l1B;
   1865		hw->bch[i].ch.ctrl = hfc_bctrl;
   1866		hw->bch[i].ch.nr = i + 1;
   1867		list_add(&hw->bch[i].ch.list, &hw->dch.dev.bchannels);
   1868	}
   1869
   1870	hw->fifos[HFCUSB_B1_TX].bch = &hw->bch[0];
   1871	hw->fifos[HFCUSB_B1_RX].bch = &hw->bch[0];
   1872	hw->fifos[HFCUSB_B2_TX].bch = &hw->bch[1];
   1873	hw->fifos[HFCUSB_B2_RX].bch = &hw->bch[1];
   1874	hw->fifos[HFCUSB_D_TX].dch = &hw->dch;
   1875	hw->fifos[HFCUSB_D_RX].dch = &hw->dch;
   1876	hw->fifos[HFCUSB_PCM_RX].ech = &hw->ech;
   1877	hw->fifos[HFCUSB_PCM_TX].ech = &hw->ech;
   1878
   1879	err = setup_hfcsusb(hw);
   1880	if (err)
   1881		goto out;
   1882
   1883	snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s.%d", DRIVER_NAME,
   1884		 hfcsusb_cnt + 1);
   1885	printk(KERN_INFO "%s: registered as '%s'\n",
   1886	       DRIVER_NAME, hw->name);
   1887
   1888	err = mISDN_register_device(&hw->dch.dev, parent, hw->name);
   1889	if (err)
   1890		goto out;
   1891
   1892	hfcsusb_cnt++;
   1893	write_lock_irqsave(&HFClock, flags);
   1894	list_add_tail(&hw->list, &HFClist);
   1895	write_unlock_irqrestore(&HFClock, flags);
   1896	return 0;
   1897
   1898out:
   1899	mISDN_freebchannel(&hw->bch[1]);
   1900	mISDN_freebchannel(&hw->bch[0]);
   1901	mISDN_freedchannel(&hw->dch);
   1902	kfree(hw);
   1903	return err;
   1904}
   1905
   1906static int
   1907hfcsusb_probe(struct usb_interface *intf, const struct usb_device_id *id)
   1908{
   1909	struct hfcsusb			*hw;
   1910	struct usb_device		*dev = interface_to_usbdev(intf);
   1911	struct usb_host_interface	*iface = intf->cur_altsetting;
   1912	struct usb_host_interface	*iface_used = NULL;
   1913	struct usb_host_endpoint	*ep;
   1914	struct hfcsusb_vdata		*driver_info;
   1915	int ifnum = iface->desc.bInterfaceNumber, i, idx, alt_idx,
   1916		probe_alt_setting, vend_idx, cfg_used, *vcf, attr, cfg_found,
   1917		ep_addr, cmptbl[16], small_match, iso_packet_size, packet_size,
   1918		alt_used = 0;
   1919
   1920	vend_idx = 0xffff;
   1921	for (i = 0; hfcsusb_idtab[i].idVendor; i++) {
   1922		if ((le16_to_cpu(dev->descriptor.idVendor)
   1923		     == hfcsusb_idtab[i].idVendor) &&
   1924		    (le16_to_cpu(dev->descriptor.idProduct)
   1925		     == hfcsusb_idtab[i].idProduct)) {
   1926			vend_idx = i;
   1927			continue;
   1928		}
   1929	}
   1930
   1931	printk(KERN_DEBUG
   1932	       "%s: interface(%d) actalt(%d) minor(%d) vend_idx(%d)\n",
   1933	       __func__, ifnum, iface->desc.bAlternateSetting,
   1934	       intf->minor, vend_idx);
   1935
   1936	if (vend_idx == 0xffff) {
   1937		printk(KERN_WARNING
   1938		       "%s: no valid vendor found in USB descriptor\n",
   1939		       __func__);
   1940		return -EIO;
   1941	}
   1942	/* if vendor and product ID is OK, start probing alternate settings */
   1943	alt_idx = 0;
   1944	small_match = -1;
   1945
   1946	/* default settings */
   1947	iso_packet_size = 16;
   1948	packet_size = 64;
   1949
   1950	while (alt_idx < intf->num_altsetting) {
   1951		iface = intf->altsetting + alt_idx;
   1952		probe_alt_setting = iface->desc.bAlternateSetting;
   1953		cfg_used = 0;
   1954
   1955		while (validconf[cfg_used][0]) {
   1956			cfg_found = 1;
   1957			vcf = validconf[cfg_used];
   1958			ep = iface->endpoint;
   1959			memcpy(cmptbl, vcf, 16 * sizeof(int));
   1960
   1961			/* check for all endpoints in this alternate setting */
   1962			for (i = 0; i < iface->desc.bNumEndpoints; i++) {
   1963				ep_addr = ep->desc.bEndpointAddress;
   1964
   1965				/* get endpoint base */
   1966				idx = ((ep_addr & 0x7f) - 1) * 2;
   1967				if (idx > 15)
   1968					return -EIO;
   1969
   1970				if (ep_addr & 0x80)
   1971					idx++;
   1972				attr = ep->desc.bmAttributes;
   1973
   1974				if (cmptbl[idx] != EP_NOP) {
   1975					if (cmptbl[idx] == EP_NUL)
   1976						cfg_found = 0;
   1977					if (attr == USB_ENDPOINT_XFER_INT
   1978					    && cmptbl[idx] == EP_INT)
   1979						cmptbl[idx] = EP_NUL;
   1980					if (attr == USB_ENDPOINT_XFER_BULK
   1981					    && cmptbl[idx] == EP_BLK)
   1982						cmptbl[idx] = EP_NUL;
   1983					if (attr == USB_ENDPOINT_XFER_ISOC
   1984					    && cmptbl[idx] == EP_ISO)
   1985						cmptbl[idx] = EP_NUL;
   1986
   1987					if (attr == USB_ENDPOINT_XFER_INT &&
   1988					    ep->desc.bInterval < vcf[17]) {
   1989						cfg_found = 0;
   1990					}
   1991				}
   1992				ep++;
   1993			}
   1994
   1995			for (i = 0; i < 16; i++)
   1996				if (cmptbl[i] != EP_NOP && cmptbl[i] != EP_NUL)
   1997					cfg_found = 0;
   1998
   1999			if (cfg_found) {
   2000				if (small_match < cfg_used) {
   2001					small_match = cfg_used;
   2002					alt_used = probe_alt_setting;
   2003					iface_used = iface;
   2004				}
   2005			}
   2006			cfg_used++;
   2007		}
   2008		alt_idx++;
   2009	}	/* (alt_idx < intf->num_altsetting) */
   2010
   2011	/* not found a valid USB Ta Endpoint config */
   2012	if (small_match == -1)
   2013		return -EIO;
   2014
   2015	iface = iface_used;
   2016	hw = kzalloc(sizeof(struct hfcsusb), GFP_KERNEL);
   2017	if (!hw)
   2018		return -ENOMEM;	/* got no mem */
   2019	snprintf(hw->name, MISDN_MAX_IDLEN - 1, "%s", DRIVER_NAME);
   2020
   2021	ep = iface->endpoint;
   2022	vcf = validconf[small_match];
   2023
   2024	for (i = 0; i < iface->desc.bNumEndpoints; i++) {
   2025		struct usb_fifo *f;
   2026
   2027		ep_addr = ep->desc.bEndpointAddress;
   2028		/* get endpoint base */
   2029		idx = ((ep_addr & 0x7f) - 1) * 2;
   2030		if (ep_addr & 0x80)
   2031			idx++;
   2032		f = &hw->fifos[idx & 7];
   2033
   2034		/* init Endpoints */
   2035		if (vcf[idx] == EP_NOP || vcf[idx] == EP_NUL) {
   2036			ep++;
   2037			continue;
   2038		}
   2039		switch (ep->desc.bmAttributes) {
   2040		case USB_ENDPOINT_XFER_INT:
   2041			f->pipe = usb_rcvintpipe(dev,
   2042						 ep->desc.bEndpointAddress);
   2043			f->usb_transfer_mode = USB_INT;
   2044			packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
   2045			break;
   2046		case USB_ENDPOINT_XFER_BULK:
   2047			if (ep_addr & 0x80)
   2048				f->pipe = usb_rcvbulkpipe(dev,
   2049							  ep->desc.bEndpointAddress);
   2050			else
   2051				f->pipe = usb_sndbulkpipe(dev,
   2052							  ep->desc.bEndpointAddress);
   2053			f->usb_transfer_mode = USB_BULK;
   2054			packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
   2055			break;
   2056		case USB_ENDPOINT_XFER_ISOC:
   2057			if (ep_addr & 0x80)
   2058				f->pipe = usb_rcvisocpipe(dev,
   2059							  ep->desc.bEndpointAddress);
   2060			else
   2061				f->pipe = usb_sndisocpipe(dev,
   2062							  ep->desc.bEndpointAddress);
   2063			f->usb_transfer_mode = USB_ISOC;
   2064			iso_packet_size = le16_to_cpu(ep->desc.wMaxPacketSize);
   2065			break;
   2066		default:
   2067			f->pipe = 0;
   2068		}
   2069
   2070		if (f->pipe) {
   2071			f->fifonum = idx & 7;
   2072			f->hw = hw;
   2073			f->usb_packet_maxlen =
   2074				le16_to_cpu(ep->desc.wMaxPacketSize);
   2075			f->intervall = ep->desc.bInterval;
   2076		}
   2077		ep++;
   2078	}
   2079	hw->dev = dev; /* save device */
   2080	hw->if_used = ifnum; /* save used interface */
   2081	hw->alt_used = alt_used; /* and alternate config */
   2082	hw->ctrl_paksize = dev->descriptor.bMaxPacketSize0; /* control size */
   2083	hw->cfg_used = vcf[16];	/* store used config */
   2084	hw->vend_idx = vend_idx; /* store found vendor */
   2085	hw->packet_size = packet_size;
   2086	hw->iso_packet_size = iso_packet_size;
   2087
   2088	/* create the control pipes needed for register access */
   2089	hw->ctrl_in_pipe = usb_rcvctrlpipe(hw->dev, 0);
   2090	hw->ctrl_out_pipe = usb_sndctrlpipe(hw->dev, 0);
   2091
   2092	driver_info = (struct hfcsusb_vdata *)
   2093		      hfcsusb_idtab[vend_idx].driver_info;
   2094
   2095	hw->ctrl_urb = usb_alloc_urb(0, GFP_KERNEL);
   2096	if (!hw->ctrl_urb) {
   2097		pr_warn("%s: No memory for control urb\n",
   2098			driver_info->vend_name);
   2099		kfree(hw);
   2100		return -ENOMEM;
   2101	}
   2102
   2103	pr_info("%s: %s: detected \"%s\" (%s, if=%d alt=%d)\n",
   2104		hw->name, __func__, driver_info->vend_name,
   2105		conf_str[small_match], ifnum, alt_used);
   2106
   2107	if (setup_instance(hw, dev->dev.parent))
   2108		return -EIO;
   2109
   2110	hw->intf = intf;
   2111	usb_set_intfdata(hw->intf, hw);
   2112	return 0;
   2113}
   2114
   2115/* function called when an active device is removed */
   2116static void
   2117hfcsusb_disconnect(struct usb_interface *intf)
   2118{
   2119	struct hfcsusb *hw = usb_get_intfdata(intf);
   2120	struct hfcsusb *next;
   2121	int cnt = 0;
   2122
   2123	printk(KERN_INFO "%s: device disconnected\n", hw->name);
   2124
   2125	handle_led(hw, LED_POWER_OFF);
   2126	release_hw(hw);
   2127
   2128	list_for_each_entry_safe(hw, next, &HFClist, list)
   2129		cnt++;
   2130	if (!cnt)
   2131		hfcsusb_cnt = 0;
   2132
   2133	usb_set_intfdata(intf, NULL);
   2134}
   2135
   2136static struct usb_driver hfcsusb_drv = {
   2137	.name = DRIVER_NAME,
   2138	.id_table = hfcsusb_idtab,
   2139	.probe = hfcsusb_probe,
   2140	.disconnect = hfcsusb_disconnect,
   2141	.disable_hub_initiated_lpm = 1,
   2142};
   2143
   2144module_usb_driver(hfcsusb_drv);