1/* 2 * USB driver for Gigaset 307x directly or using M105 Data. 3 * 4 * Copyright (c) 2001 by Stefan Eilers 5 * and Hansjoerg Lipp <hjlipp@web.de>. 6 * 7 * This driver was derived from the USB skeleton driver by 8 * Greg Kroah-Hartman <greg@kroah.com> 9 * 10 * ===================================================================== 11 * This program is free software; you can redistribute it and/or 12 * modify it under the terms of the GNU General Public License as 13 * published by the Free Software Foundation; either version 2 of 14 * the License, or (at your option) any later version. 15 * ===================================================================== 16 */ 17 18#include "gigaset.h" 19#include <linux/usb.h> 20#include <linux/module.h> 21#include <linux/moduleparam.h> 22 23/* Version Information */ 24#define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers" 25#define DRIVER_DESC "USB Driver for Gigaset 307x using M105" 26 27/* Module parameters */ 28 29static int startmode = SM_ISDN; 30static int cidmode = 1; 31 32module_param(startmode, int, S_IRUGO); 33module_param(cidmode, int, S_IRUGO); 34MODULE_PARM_DESC(startmode, "start in isdn4linux mode"); 35MODULE_PARM_DESC(cidmode, "Call-ID mode"); 36 37#define GIGASET_MINORS 1 38#define GIGASET_MINOR 8 39#define GIGASET_MODULENAME "usb_gigaset" 40#define GIGASET_DEVNAME "ttyGU" 41 42/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */ 43#define IF_WRITEBUF 264 44 45/* Values for the Gigaset M105 Data */ 46#define USB_M105_VENDOR_ID 0x0681 47#define USB_M105_PRODUCT_ID 0x0009 48 49/* table of devices that work with this driver */ 50static const struct usb_device_id gigaset_table[] = { 51 { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) }, 52 { } /* Terminating entry */ 53}; 54 55MODULE_DEVICE_TABLE(usb, gigaset_table); 56 57 58/* functions called if a device of this driver is connected/disconnected */ 59static int gigaset_probe(struct usb_interface *interface, 60 const struct usb_device_id *id); 61static void gigaset_disconnect(struct usb_interface *interface); 62 63/* functions called before/after suspend */ 64static int gigaset_suspend(struct usb_interface *intf, pm_message_t message); 65static int gigaset_resume(struct usb_interface *intf); 66static int gigaset_pre_reset(struct usb_interface *intf); 67 68static struct gigaset_driver *driver; 69 70/* usb specific object needed to register this driver with the usb subsystem */ 71static struct usb_driver gigaset_usb_driver = { 72 .name = GIGASET_MODULENAME, 73 .probe = gigaset_probe, 74 .disconnect = gigaset_disconnect, 75 .id_table = gigaset_table, 76 .suspend = gigaset_suspend, 77 .resume = gigaset_resume, 78 .reset_resume = gigaset_resume, 79 .pre_reset = gigaset_pre_reset, 80 .post_reset = gigaset_resume, 81}; 82 83struct usb_cardstate { 84 struct usb_device *udev; /* usb device pointer */ 85 struct usb_interface *interface; /* interface for this device */ 86 int busy; /* bulk output in progress */ 87 88 /* Output buffer */ 89 unsigned char *bulk_out_buffer; 90 int bulk_out_size; 91 __u8 bulk_out_endpointAddr; 92 struct urb *bulk_out_urb; 93 94 /* Input buffer */ 95 unsigned char *rcvbuf; 96 int rcvbuf_size; 97 struct urb *read_urb; 98 __u8 int_in_endpointAddr; 99 100 char bchars[6]; /* for request 0x19 */ 101}; 102 103static inline unsigned tiocm_to_gigaset(unsigned state) 104{ 105 return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0); 106} 107 108static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state, 109 unsigned new_state) 110{ 111 struct usb_device *udev = cs->hw.usb->udev; 112 unsigned mask, val; 113 int r; 114 115 mask = tiocm_to_gigaset(old_state ^ new_state); 116 val = tiocm_to_gigaset(new_state); 117 118 gig_dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask); 119 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 7, 0x41, 120 (val & 0xff) | ((mask & 0xff) << 8), 0, 121 NULL, 0, 2000 /* timeout? */); 122 if (r < 0) 123 return r; 124 return 0; 125} 126 127/* 128 * Set M105 configuration value 129 * using undocumented device commands reverse engineered from USB traces 130 * of the Siemens Windows driver 131 */ 132static int set_value(struct cardstate *cs, u8 req, u16 val) 133{ 134 struct usb_device *udev = cs->hw.usb->udev; 135 int r, r2; 136 137 gig_dbg(DEBUG_USBREQ, "request %02x (%04x)", 138 (unsigned)req, (unsigned)val); 139 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x12, 0x41, 140 0xf /*?*/, 0, NULL, 0, 2000 /*?*/); 141 /* no idea what this does */ 142 if (r < 0) { 143 dev_err(&udev->dev, "error %d on request 0x12\n", -r); 144 return r; 145 } 146 147 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), req, 0x41, 148 val, 0, NULL, 0, 2000 /*?*/); 149 if (r < 0) 150 dev_err(&udev->dev, "error %d on request 0x%02x\n", 151 -r, (unsigned)req); 152 153 r2 = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41, 154 0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/); 155 if (r2 < 0) 156 dev_err(&udev->dev, "error %d on request 0x19\n", -r2); 157 158 return r < 0 ? r : (r2 < 0 ? r2 : 0); 159} 160 161/* 162 * set the baud rate on the internal serial adapter 163 * using the undocumented parameter setting command 164 */ 165static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag) 166{ 167 u16 val; 168 u32 rate; 169 170 cflag &= CBAUD; 171 172 switch (cflag) { 173 case B300: rate = 300; break; 174 case B600: rate = 600; break; 175 case B1200: rate = 1200; break; 176 case B2400: rate = 2400; break; 177 case B4800: rate = 4800; break; 178 case B9600: rate = 9600; break; 179 case B19200: rate = 19200; break; 180 case B38400: rate = 38400; break; 181 case B57600: rate = 57600; break; 182 case B115200: rate = 115200; break; 183 default: 184 rate = 9600; 185 dev_err(cs->dev, "unsupported baudrate request 0x%x," 186 " using default of B9600\n", cflag); 187 } 188 189 val = 0x383fff / rate + 1; 190 191 return set_value(cs, 1, val); 192} 193 194/* 195 * set the line format on the internal serial adapter 196 * using the undocumented parameter setting command 197 */ 198static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag) 199{ 200 u16 val = 0; 201 202 /* set the parity */ 203 if (cflag & PARENB) 204 val |= (cflag & PARODD) ? 0x10 : 0x20; 205 206 /* set the number of data bits */ 207 switch (cflag & CSIZE) { 208 case CS5: 209 val |= 5 << 8; break; 210 case CS6: 211 val |= 6 << 8; break; 212 case CS7: 213 val |= 7 << 8; break; 214 case CS8: 215 val |= 8 << 8; break; 216 default: 217 dev_err(cs->dev, "CSIZE was not CS5-CS8, using default of 8\n"); 218 val |= 8 << 8; 219 break; 220 } 221 222 /* set the number of stop bits */ 223 if (cflag & CSTOPB) { 224 if ((cflag & CSIZE) == CS5) 225 val |= 1; /* 1.5 stop bits */ 226 else 227 val |= 2; /* 2 stop bits */ 228 } 229 230 return set_value(cs, 3, val); 231} 232 233 234/*============================================================================*/ 235static int gigaset_init_bchannel(struct bc_state *bcs) 236{ 237 /* nothing to do for M10x */ 238 gigaset_bchannel_up(bcs); 239 return 0; 240} 241 242static int gigaset_close_bchannel(struct bc_state *bcs) 243{ 244 /* nothing to do for M10x */ 245 gigaset_bchannel_down(bcs); 246 return 0; 247} 248 249static int write_modem(struct cardstate *cs); 250static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb); 251 252 253/* Write tasklet handler: Continue sending current skb, or send command, or 254 * start sending an skb from the send queue. 255 */ 256static void gigaset_modem_fill(unsigned long data) 257{ 258 struct cardstate *cs = (struct cardstate *) data; 259 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */ 260 struct cmdbuf_t *cb; 261 int again; 262 263 gig_dbg(DEBUG_OUTPUT, "modem_fill"); 264 265 if (cs->hw.usb->busy) { 266 gig_dbg(DEBUG_OUTPUT, "modem_fill: busy"); 267 return; 268 } 269 270 do { 271 again = 0; 272 if (!bcs->tx_skb) { /* no skb is being sent */ 273 cb = cs->cmdbuf; 274 if (cb) { /* commands to send? */ 275 gig_dbg(DEBUG_OUTPUT, "modem_fill: cb"); 276 if (send_cb(cs, cb) < 0) { 277 gig_dbg(DEBUG_OUTPUT, 278 "modem_fill: send_cb failed"); 279 again = 1; /* no callback will be 280 called! */ 281 } 282 } else { /* skbs to send? */ 283 bcs->tx_skb = skb_dequeue(&bcs->squeue); 284 if (bcs->tx_skb) 285 gig_dbg(DEBUG_INTR, 286 "Dequeued skb (Adr: %lx)!", 287 (unsigned long) bcs->tx_skb); 288 } 289 } 290 291 if (bcs->tx_skb) { 292 gig_dbg(DEBUG_OUTPUT, "modem_fill: tx_skb"); 293 if (write_modem(cs) < 0) { 294 gig_dbg(DEBUG_OUTPUT, 295 "modem_fill: write_modem failed"); 296 again = 1; /* no callback will be called! */ 297 } 298 } 299 } while (again); 300} 301 302/* 303 * Interrupt Input URB completion routine 304 */ 305static void gigaset_read_int_callback(struct urb *urb) 306{ 307 struct cardstate *cs = urb->context; 308 struct inbuf_t *inbuf = cs->inbuf; 309 int status = urb->status; 310 int r; 311 unsigned numbytes; 312 unsigned char *src; 313 unsigned long flags; 314 315 if (!status) { 316 numbytes = urb->actual_length; 317 318 if (numbytes) { 319 src = cs->hw.usb->rcvbuf; 320 if (unlikely(*src)) 321 dev_warn(cs->dev, 322 "%s: There was no leading 0, but 0x%02x!\n", 323 __func__, (unsigned) *src); 324 ++src; /* skip leading 0x00 */ 325 --numbytes; 326 if (gigaset_fill_inbuf(inbuf, src, numbytes)) { 327 gig_dbg(DEBUG_INTR, "%s-->BH", __func__); 328 gigaset_schedule_event(inbuf->cs); 329 } 330 } else 331 gig_dbg(DEBUG_INTR, "Received zero block length"); 332 } else { 333 /* The urb might have been killed. */ 334 gig_dbg(DEBUG_ANY, "%s - nonzero status received: %d", 335 __func__, status); 336 if (status == -ENOENT || status == -ESHUTDOWN) 337 /* killed or endpoint shutdown: don't resubmit */ 338 return; 339 } 340 341 /* resubmit URB */ 342 spin_lock_irqsave(&cs->lock, flags); 343 if (!cs->connected) { 344 spin_unlock_irqrestore(&cs->lock, flags); 345 pr_err("%s: disconnected\n", __func__); 346 return; 347 } 348 r = usb_submit_urb(urb, GFP_ATOMIC); 349 spin_unlock_irqrestore(&cs->lock, flags); 350 if (r) 351 dev_err(cs->dev, "error %d resubmitting URB\n", -r); 352} 353 354 355/* This callback routine is called when data was transmitted to the device. */ 356static void gigaset_write_bulk_callback(struct urb *urb) 357{ 358 struct cardstate *cs = urb->context; 359 int status = urb->status; 360 unsigned long flags; 361 362 switch (status) { 363 case 0: /* normal completion */ 364 break; 365 case -ENOENT: /* killed */ 366 gig_dbg(DEBUG_ANY, "%s: killed", __func__); 367 cs->hw.usb->busy = 0; 368 return; 369 default: 370 dev_err(cs->dev, "bulk transfer failed (status %d)\n", 371 -status); 372 /* That's all we can do. Communication problems 373 are handled by timeouts or network protocols. */ 374 } 375 376 spin_lock_irqsave(&cs->lock, flags); 377 if (!cs->connected) { 378 pr_err("%s: disconnected\n", __func__); 379 } else { 380 cs->hw.usb->busy = 0; 381 tasklet_schedule(&cs->write_tasklet); 382 } 383 spin_unlock_irqrestore(&cs->lock, flags); 384} 385 386static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb) 387{ 388 struct cmdbuf_t *tcb; 389 unsigned long flags; 390 int count; 391 int status = -ENOENT; 392 struct usb_cardstate *ucs = cs->hw.usb; 393 394 do { 395 if (!cb->len) { 396 tcb = cb; 397 398 spin_lock_irqsave(&cs->cmdlock, flags); 399 cs->cmdbytes -= cs->curlen; 400 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left", 401 cs->curlen, cs->cmdbytes); 402 cs->cmdbuf = cb = cb->next; 403 if (cb) { 404 cb->prev = NULL; 405 cs->curlen = cb->len; 406 } else { 407 cs->lastcmdbuf = NULL; 408 cs->curlen = 0; 409 } 410 spin_unlock_irqrestore(&cs->cmdlock, flags); 411 412 if (tcb->wake_tasklet) 413 tasklet_schedule(tcb->wake_tasklet); 414 kfree(tcb); 415 } 416 if (cb) { 417 count = min(cb->len, ucs->bulk_out_size); 418 gig_dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count); 419 420 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev, 421 usb_sndbulkpipe(ucs->udev, 422 ucs->bulk_out_endpointAddr & 0x0f), 423 cb->buf + cb->offset, count, 424 gigaset_write_bulk_callback, cs); 425 426 cb->offset += count; 427 cb->len -= count; 428 ucs->busy = 1; 429 430 spin_lock_irqsave(&cs->lock, flags); 431 status = cs->connected ? 432 usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC) : 433 -ENODEV; 434 spin_unlock_irqrestore(&cs->lock, flags); 435 436 if (status) { 437 ucs->busy = 0; 438 dev_err(cs->dev, 439 "could not submit urb (error %d)\n", 440 -status); 441 cb->len = 0; /* skip urb => remove cb+wakeup 442 in next loop cycle */ 443 } 444 } 445 } while (cb && status); /* next command on error */ 446 447 return status; 448} 449 450/* Send command to device. */ 451static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb) 452{ 453 unsigned long flags; 454 455 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ? 456 DEBUG_TRANSCMD : DEBUG_LOCKCMD, 457 "CMD Transmit", cb->len, cb->buf); 458 459 spin_lock_irqsave(&cs->cmdlock, flags); 460 cb->prev = cs->lastcmdbuf; 461 if (cs->lastcmdbuf) 462 cs->lastcmdbuf->next = cb; 463 else { 464 cs->cmdbuf = cb; 465 cs->curlen = cb->len; 466 } 467 cs->cmdbytes += cb->len; 468 cs->lastcmdbuf = cb; 469 spin_unlock_irqrestore(&cs->cmdlock, flags); 470 471 spin_lock_irqsave(&cs->lock, flags); 472 if (cs->connected) 473 tasklet_schedule(&cs->write_tasklet); 474 spin_unlock_irqrestore(&cs->lock, flags); 475 return cb->len; 476} 477 478static int gigaset_write_room(struct cardstate *cs) 479{ 480 unsigned bytes; 481 482 bytes = cs->cmdbytes; 483 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0; 484} 485 486static int gigaset_chars_in_buffer(struct cardstate *cs) 487{ 488 return cs->cmdbytes; 489} 490 491/* 492 * set the break characters on the internal serial adapter 493 * using undocumented device commands reverse engineered from USB traces 494 * of the Siemens Windows driver 495 */ 496static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6]) 497{ 498 struct usb_device *udev = cs->hw.usb->udev; 499 500 gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf); 501 memcpy(cs->hw.usb->bchars, buf, 6); 502 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41, 503 0, 0, &buf, 6, 2000); 504} 505 506static int gigaset_freebcshw(struct bc_state *bcs) 507{ 508 /* unused */ 509 return 1; 510} 511 512/* Initialize the b-channel structure */ 513static int gigaset_initbcshw(struct bc_state *bcs) 514{ 515 /* unused */ 516 bcs->hw.usb = NULL; 517 return 1; 518} 519 520static void gigaset_reinitbcshw(struct bc_state *bcs) 521{ 522 /* nothing to do for M10x */ 523} 524 525static void gigaset_freecshw(struct cardstate *cs) 526{ 527 tasklet_kill(&cs->write_tasklet); 528 kfree(cs->hw.usb); 529} 530 531static int gigaset_initcshw(struct cardstate *cs) 532{ 533 struct usb_cardstate *ucs; 534 535 cs->hw.usb = ucs = 536 kmalloc(sizeof(struct usb_cardstate), GFP_KERNEL); 537 if (!ucs) { 538 pr_err("out of memory\n"); 539 return 0; 540 } 541 542 ucs->bchars[0] = 0; 543 ucs->bchars[1] = 0; 544 ucs->bchars[2] = 0; 545 ucs->bchars[3] = 0; 546 ucs->bchars[4] = 0x11; 547 ucs->bchars[5] = 0x13; 548 ucs->bulk_out_buffer = NULL; 549 ucs->bulk_out_urb = NULL; 550 ucs->read_urb = NULL; 551 tasklet_init(&cs->write_tasklet, 552 gigaset_modem_fill, (unsigned long) cs); 553 554 return 1; 555} 556 557/* Send data from current skb to the device. */ 558static int write_modem(struct cardstate *cs) 559{ 560 int ret = 0; 561 int count; 562 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */ 563 struct usb_cardstate *ucs = cs->hw.usb; 564 unsigned long flags; 565 566 gig_dbg(DEBUG_OUTPUT, "len: %d...", bcs->tx_skb->len); 567 568 if (!bcs->tx_skb->len) { 569 dev_kfree_skb_any(bcs->tx_skb); 570 bcs->tx_skb = NULL; 571 return -EINVAL; 572 } 573 574 /* Copy data to bulk out buffer and transmit data */ 575 count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size); 576 skb_copy_from_linear_data(bcs->tx_skb, ucs->bulk_out_buffer, count); 577 skb_pull(bcs->tx_skb, count); 578 ucs->busy = 1; 579 gig_dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count); 580 581 spin_lock_irqsave(&cs->lock, flags); 582 if (cs->connected) { 583 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev, 584 usb_sndbulkpipe(ucs->udev, 585 ucs->bulk_out_endpointAddr & 586 0x0f), 587 ucs->bulk_out_buffer, count, 588 gigaset_write_bulk_callback, cs); 589 ret = usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC); 590 } else { 591 ret = -ENODEV; 592 } 593 spin_unlock_irqrestore(&cs->lock, flags); 594 595 if (ret) { 596 dev_err(cs->dev, "could not submit urb (error %d)\n", -ret); 597 ucs->busy = 0; 598 } 599 600 if (!bcs->tx_skb->len) { 601 /* skb sent completely */ 602 gigaset_skb_sent(bcs, bcs->tx_skb); 603 604 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!", 605 (unsigned long) bcs->tx_skb); 606 dev_kfree_skb_any(bcs->tx_skb); 607 bcs->tx_skb = NULL; 608 } 609 610 return ret; 611} 612 613static int gigaset_probe(struct usb_interface *interface, 614 const struct usb_device_id *id) 615{ 616 int retval; 617 struct usb_device *udev = interface_to_usbdev(interface); 618 struct usb_host_interface *hostif = interface->cur_altsetting; 619 struct cardstate *cs = NULL; 620 struct usb_cardstate *ucs = NULL; 621 struct usb_endpoint_descriptor *endpoint; 622 int buffer_size; 623 624 gig_dbg(DEBUG_ANY, "%s: Check if device matches ...", __func__); 625 626 /* See if the device offered us matches what we can accept */ 627 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_M105_VENDOR_ID) || 628 (le16_to_cpu(udev->descriptor.idProduct) != USB_M105_PRODUCT_ID)) { 629 gig_dbg(DEBUG_ANY, "device ID (0x%x, 0x%x) not for me - skip", 630 le16_to_cpu(udev->descriptor.idVendor), 631 le16_to_cpu(udev->descriptor.idProduct)); 632 return -ENODEV; 633 } 634 if (hostif->desc.bInterfaceNumber != 0) { 635 gig_dbg(DEBUG_ANY, "interface %d not for me - skip", 636 hostif->desc.bInterfaceNumber); 637 return -ENODEV; 638 } 639 if (hostif->desc.bAlternateSetting != 0) { 640 dev_notice(&udev->dev, "unsupported altsetting %d - skip", 641 hostif->desc.bAlternateSetting); 642 return -ENODEV; 643 } 644 if (hostif->desc.bInterfaceClass != 255) { 645 dev_notice(&udev->dev, "unsupported interface class %d - skip", 646 hostif->desc.bInterfaceClass); 647 return -ENODEV; 648 } 649 650 dev_info(&udev->dev, "%s: Device matched ... !\n", __func__); 651 652 /* allocate memory for our device state and intialize it */ 653 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME); 654 if (!cs) 655 return -ENODEV; 656 ucs = cs->hw.usb; 657 658 /* save off device structure ptrs for later use */ 659 usb_get_dev(udev); 660 ucs->udev = udev; 661 ucs->interface = interface; 662 cs->dev = &interface->dev; 663 664 /* save address of controller structure */ 665 usb_set_intfdata(interface, cs); 666 667 endpoint = &hostif->endpoint[0].desc; 668 669 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 670 ucs->bulk_out_size = buffer_size; 671 ucs->bulk_out_endpointAddr = endpoint->bEndpointAddress; 672 ucs->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL); 673 if (!ucs->bulk_out_buffer) { 674 dev_err(cs->dev, "Couldn't allocate bulk_out_buffer\n"); 675 retval = -ENOMEM; 676 goto error; 677 } 678 679 ucs->bulk_out_urb = usb_alloc_urb(0, GFP_KERNEL); 680 if (!ucs->bulk_out_urb) { 681 dev_err(cs->dev, "Couldn't allocate bulk_out_urb\n"); 682 retval = -ENOMEM; 683 goto error; 684 } 685 686 endpoint = &hostif->endpoint[1].desc; 687 688 ucs->busy = 0; 689 690 ucs->read_urb = usb_alloc_urb(0, GFP_KERNEL); 691 if (!ucs->read_urb) { 692 dev_err(cs->dev, "No free urbs available\n"); 693 retval = -ENOMEM; 694 goto error; 695 } 696 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize); 697 ucs->rcvbuf_size = buffer_size; 698 ucs->int_in_endpointAddr = endpoint->bEndpointAddress; 699 ucs->rcvbuf = kmalloc(buffer_size, GFP_KERNEL); 700 if (!ucs->rcvbuf) { 701 dev_err(cs->dev, "Couldn't allocate rcvbuf\n"); 702 retval = -ENOMEM; 703 goto error; 704 } 705 /* Fill the interrupt urb and send it to the core */ 706 usb_fill_int_urb(ucs->read_urb, udev, 707 usb_rcvintpipe(udev, 708 endpoint->bEndpointAddress & 0x0f), 709 ucs->rcvbuf, buffer_size, 710 gigaset_read_int_callback, 711 cs, endpoint->bInterval); 712 713 retval = usb_submit_urb(ucs->read_urb, GFP_KERNEL); 714 if (retval) { 715 dev_err(cs->dev, "Could not submit URB (error %d)\n", -retval); 716 goto error; 717 } 718 719 /* tell common part that the device is ready */ 720 if (startmode == SM_LOCKED) 721 cs->mstate = MS_LOCKED; 722 723 if (!gigaset_start(cs)) { 724 tasklet_kill(&cs->write_tasklet); 725 retval = -ENODEV; 726 goto error; 727 } 728 return 0; 729 730error: 731 usb_kill_urb(ucs->read_urb); 732 kfree(ucs->bulk_out_buffer); 733 usb_free_urb(ucs->bulk_out_urb); 734 kfree(ucs->rcvbuf); 735 usb_free_urb(ucs->read_urb); 736 usb_set_intfdata(interface, NULL); 737 ucs->read_urb = ucs->bulk_out_urb = NULL; 738 ucs->rcvbuf = ucs->bulk_out_buffer = NULL; 739 usb_put_dev(ucs->udev); 740 ucs->udev = NULL; 741 ucs->interface = NULL; 742 gigaset_freecs(cs); 743 return retval; 744} 745 746static void gigaset_disconnect(struct usb_interface *interface) 747{ 748 struct cardstate *cs; 749 struct usb_cardstate *ucs; 750 751 cs = usb_get_intfdata(interface); 752 ucs = cs->hw.usb; 753 754 dev_info(cs->dev, "disconnecting Gigaset USB adapter\n"); 755 756 usb_kill_urb(ucs->read_urb); 757 758 gigaset_stop(cs); 759 760 usb_set_intfdata(interface, NULL); 761 tasklet_kill(&cs->write_tasklet); 762 763 usb_kill_urb(ucs->bulk_out_urb); 764 765 kfree(ucs->bulk_out_buffer); 766 usb_free_urb(ucs->bulk_out_urb); 767 kfree(ucs->rcvbuf); 768 usb_free_urb(ucs->read_urb); 769 ucs->read_urb = ucs->bulk_out_urb = NULL; 770 ucs->rcvbuf = ucs->bulk_out_buffer = NULL; 771 772 usb_put_dev(ucs->udev); 773 ucs->interface = NULL; 774 ucs->udev = NULL; 775 cs->dev = NULL; 776 gigaset_freecs(cs); 777} 778 779/* gigaset_suspend 780 * This function is called before the USB connection is suspended or reset. 781 */ 782static int gigaset_suspend(struct usb_interface *intf, pm_message_t message) 783{ 784 struct cardstate *cs = usb_get_intfdata(intf); 785 786 /* stop activity */ 787 cs->connected = 0; /* prevent rescheduling */ 788 usb_kill_urb(cs->hw.usb->read_urb); 789 tasklet_kill(&cs->write_tasklet); 790 usb_kill_urb(cs->hw.usb->bulk_out_urb); 791 792 gig_dbg(DEBUG_SUSPEND, "suspend complete"); 793 return 0; 794} 795 796/* gigaset_resume 797 * This function is called after the USB connection has been resumed or reset. 798 */ 799static int gigaset_resume(struct usb_interface *intf) 800{ 801 struct cardstate *cs = usb_get_intfdata(intf); 802 int rc; 803 804 /* resubmit interrupt URB */ 805 cs->connected = 1; 806 rc = usb_submit_urb(cs->hw.usb->read_urb, GFP_KERNEL); 807 if (rc) { 808 dev_err(cs->dev, "Could not submit read URB (error %d)\n", -rc); 809 return rc; 810 } 811 812 gig_dbg(DEBUG_SUSPEND, "resume complete"); 813 return 0; 814} 815 816/* gigaset_pre_reset 817 * This function is called before the USB connection is reset. 818 */ 819static int gigaset_pre_reset(struct usb_interface *intf) 820{ 821 /* same as suspend */ 822 return gigaset_suspend(intf, PMSG_ON); 823} 824 825static const struct gigaset_ops ops = { 826 gigaset_write_cmd, 827 gigaset_write_room, 828 gigaset_chars_in_buffer, 829 gigaset_brkchars, 830 gigaset_init_bchannel, 831 gigaset_close_bchannel, 832 gigaset_initbcshw, 833 gigaset_freebcshw, 834 gigaset_reinitbcshw, 835 gigaset_initcshw, 836 gigaset_freecshw, 837 gigaset_set_modem_ctrl, 838 gigaset_baud_rate, 839 gigaset_set_line_ctrl, 840 gigaset_m10x_send_skb, 841 gigaset_m10x_input, 842}; 843 844/* 845 * This function is called while kernel-module is loaded 846 */ 847static int __init usb_gigaset_init(void) 848{ 849 int result; 850 851 /* allocate memory for our driver state and intialize it */ 852 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS, 853 GIGASET_MODULENAME, GIGASET_DEVNAME, 854 &ops, THIS_MODULE); 855 if (driver == NULL) 856 goto error; 857 858 /* register this driver with the USB subsystem */ 859 result = usb_register(&gigaset_usb_driver); 860 if (result < 0) { 861 pr_err("error %d registering USB driver\n", -result); 862 goto error; 863 } 864 865 pr_info(DRIVER_DESC "\n"); 866 return 0; 867 868error: 869 if (driver) 870 gigaset_freedriver(driver); 871 driver = NULL; 872 return -1; 873} 874 875/* 876 * This function is called while unloading the kernel-module 877 */ 878static void __exit usb_gigaset_exit(void) 879{ 880 int i; 881 882 gigaset_blockdriver(driver); /* => probe will fail 883 * => no gigaset_start any more 884 */ 885 886 /* stop all connected devices */ 887 for (i = 0; i < driver->minors; i++) 888 gigaset_shutdown(driver->cs + i); 889 890 /* from now on, no isdn callback should be possible */ 891 892 /* deregister this driver with the USB subsystem */ 893 usb_deregister(&gigaset_usb_driver); 894 /* this will call the disconnect-callback */ 895 /* from now on, no disconnect/probe callback should be running */ 896 897 gigaset_freedriver(driver); 898 driver = NULL; 899} 900 901 902module_init(usb_gigaset_init); 903module_exit(usb_gigaset_exit); 904 905MODULE_AUTHOR(DRIVER_AUTHOR); 906MODULE_DESCRIPTION(DRIVER_DESC); 907 908MODULE_LICENSE("GPL"); 909