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