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