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