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