root/drivers/staging/isdn/gigaset/bas-gigaset.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. get_usb_rcmsg
  2. get_usb_statmsg
  3. usb_pipetype_str
  4. dump_urb
  5. gigaset_set_modem_ctrl
  6. gigaset_baud_rate
  7. gigaset_set_line_ctrl
  8. update_basstate
  9. error_hangup
  10. error_reset
  11. check_pending
  12. cmd_in_timeout
  13. read_ctrl_callback
  14. atread_submit
  15. int_in_work
  16. int_in_resubmit
  17. read_int_callback
  18. read_iso_callback
  19. write_iso_callback
  20. starturbs
  21. stopurbs
  22. submit_iso_write_urb
  23. write_iso_tasklet
  24. read_iso_tasklet
  25. req_timeout
  26. write_ctrl_callback
  27. req_submit
  28. gigaset_init_bchannel
  29. gigaset_close_bchannel
  30. complete_cb
  31. write_command_callback
  32. atrdy_timeout
  33. atwrite_submit
  34. start_cbsend
  35. gigaset_write_cmd
  36. gigaset_write_room
  37. gigaset_chars_in_buffer
  38. gigaset_brkchars
  39. gigaset_freebcshw
  40. gigaset_initbcshw
  41. gigaset_reinitbcshw
  42. gigaset_freecshw
  43. gigaset_initcshw
  44. freeurbs
  45. gigaset_probe
  46. gigaset_disconnect
  47. gigaset_suspend
  48. gigaset_resume
  49. gigaset_pre_reset
  50. gigaset_post_reset
  51. bas_gigaset_init
  52. bas_gigaset_exit

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * USB driver for Gigaset 307x base via direct USB connection.
   4  *
   5  * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
   6  *                       Tilman Schmidt <tilman@imap.cc>,
   7  *                       Stefan Eilers.
   8  *
   9  * =====================================================================
  10  * =====================================================================
  11  */
  12 
  13 #include "gigaset.h"
  14 #include <linux/usb.h>
  15 #include <linux/module.h>
  16 #include <linux/moduleparam.h>
  17 
  18 /* Version Information */
  19 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
  20 #define DRIVER_DESC "USB Driver for Gigaset 307x"
  21 
  22 
  23 /* Module parameters */
  24 
  25 static int startmode = SM_ISDN;
  26 static int cidmode = 1;
  27 
  28 module_param(startmode, int, S_IRUGO);
  29 module_param(cidmode, int, S_IRUGO);
  30 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
  31 MODULE_PARM_DESC(cidmode, "Call-ID mode");
  32 
  33 #define GIGASET_MINORS     1
  34 #define GIGASET_MINOR      16
  35 #define GIGASET_MODULENAME "bas_gigaset"
  36 #define GIGASET_DEVNAME    "ttyGB"
  37 
  38 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
  39 #define IF_WRITEBUF 264
  40 
  41 /* interrupt pipe message size according to ibid. ch. 2.2 */
  42 #define IP_MSGSIZE 3
  43 
  44 /* Values for the Gigaset 307x */
  45 #define USB_GIGA_VENDOR_ID      0x0681
  46 #define USB_3070_PRODUCT_ID     0x0001
  47 #define USB_3075_PRODUCT_ID     0x0002
  48 #define USB_SX303_PRODUCT_ID    0x0021
  49 #define USB_SX353_PRODUCT_ID    0x0022
  50 
  51 /* table of devices that work with this driver */
  52 static const struct usb_device_id gigaset_table[] = {
  53         { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
  54         { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
  55         { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
  56         { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
  57         { } /* Terminating entry */
  58 };
  59 
  60 MODULE_DEVICE_TABLE(usb, gigaset_table);
  61 
  62 /*======================= local function prototypes ==========================*/
  63 
  64 /* function called if a new device belonging to this driver is connected */
  65 static int gigaset_probe(struct usb_interface *interface,
  66                          const struct usb_device_id *id);
  67 
  68 /* Function will be called if the device is unplugged */
  69 static void gigaset_disconnect(struct usb_interface *interface);
  70 
  71 /* functions called before/after suspend */
  72 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
  73 static int gigaset_resume(struct usb_interface *intf);
  74 
  75 /* functions called before/after device reset */
  76 static int gigaset_pre_reset(struct usb_interface *intf);
  77 static int gigaset_post_reset(struct usb_interface *intf);
  78 
  79 static int atread_submit(struct cardstate *, int);
  80 static void stopurbs(struct bas_bc_state *);
  81 static int req_submit(struct bc_state *, int, int, int);
  82 static int atwrite_submit(struct cardstate *, unsigned char *, int);
  83 static int start_cbsend(struct cardstate *);
  84 
  85 /*============================================================================*/
  86 
  87 struct bas_cardstate {
  88         struct usb_device       *udev;          /* USB device pointer */
  89         struct cardstate        *cs;
  90         struct usb_interface    *interface;     /* interface for this device */
  91         unsigned char           minor;          /* starting minor number */
  92 
  93         struct urb              *urb_ctrl;      /* control pipe default URB */
  94         struct usb_ctrlrequest  dr_ctrl;
  95         struct timer_list       timer_ctrl;     /* control request timeout */
  96         int                     retry_ctrl;
  97 
  98         struct timer_list       timer_atrdy;    /* AT command ready timeout */
  99         struct urb              *urb_cmd_out;   /* for sending AT commands */
 100         struct usb_ctrlrequest  dr_cmd_out;
 101         int                     retry_cmd_out;
 102 
 103         struct urb              *urb_cmd_in;    /* for receiving AT replies */
 104         struct usb_ctrlrequest  dr_cmd_in;
 105         struct timer_list       timer_cmd_in;   /* receive request timeout */
 106         unsigned char           *rcvbuf;        /* AT reply receive buffer */
 107 
 108         struct urb              *urb_int_in;    /* URB for interrupt pipe */
 109         unsigned char           *int_in_buf;
 110         struct work_struct      int_in_wq;      /* for usb_clear_halt() */
 111         struct timer_list       timer_int_in;   /* int read retry delay */
 112         int                     retry_int_in;
 113 
 114         spinlock_t              lock;           /* locks all following */
 115         int                     basstate;       /* bitmap (BS_*) */
 116         int                     pending;        /* uncompleted base request */
 117         wait_queue_head_t       waitqueue;
 118         int                     rcvbuf_size;    /* size of AT receive buffer */
 119                                                 /* 0: no receive in progress */
 120         int                     retry_cmd_in;   /* receive req retry count */
 121 };
 122 
 123 /* status of direct USB connection to 307x base (bits in basstate) */
 124 #define BS_ATOPEN       0x001   /* AT channel open */
 125 #define BS_B1OPEN       0x002   /* B channel 1 open */
 126 #define BS_B2OPEN       0x004   /* B channel 2 open */
 127 #define BS_ATREADY      0x008   /* base ready for AT command */
 128 #define BS_INIT         0x010   /* base has signalled INIT_OK */
 129 #define BS_ATTIMER      0x020   /* waiting for HD_READY_SEND_ATDATA */
 130 #define BS_ATRDPEND     0x040   /* urb_cmd_in in use */
 131 #define BS_ATWRPEND     0x080   /* urb_cmd_out in use */
 132 #define BS_SUSPEND      0x100   /* USB port suspended */
 133 #define BS_RESETTING    0x200   /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
 134 
 135 
 136 static struct gigaset_driver *driver;
 137 
 138 /* usb specific object needed to register this driver with the usb subsystem */
 139 static struct usb_driver gigaset_usb_driver = {
 140         .name =         GIGASET_MODULENAME,
 141         .probe =        gigaset_probe,
 142         .disconnect =   gigaset_disconnect,
 143         .id_table =     gigaset_table,
 144         .suspend =      gigaset_suspend,
 145         .resume =       gigaset_resume,
 146         .reset_resume = gigaset_post_reset,
 147         .pre_reset =    gigaset_pre_reset,
 148         .post_reset =   gigaset_post_reset,
 149         .disable_hub_initiated_lpm = 1,
 150 };
 151 
 152 /* get message text for usb_submit_urb return code
 153  */
 154 static char *get_usb_rcmsg(int rc)
 155 {
 156         static char unkmsg[28];
 157 
 158         switch (rc) {
 159         case 0:
 160                 return "success";
 161         case -ENOMEM:
 162                 return "out of memory";
 163         case -ENODEV:
 164                 return "device not present";
 165         case -ENOENT:
 166                 return "endpoint not present";
 167         case -ENXIO:
 168                 return "URB type not supported";
 169         case -EINVAL:
 170                 return "invalid argument";
 171         case -EAGAIN:
 172                 return "start frame too early or too much scheduled";
 173         case -EFBIG:
 174                 return "too many isoc frames requested";
 175         case -EPIPE:
 176                 return "endpoint stalled";
 177         case -EMSGSIZE:
 178                 return "invalid packet size";
 179         case -ENOSPC:
 180                 return "would overcommit USB bandwidth";
 181         case -ESHUTDOWN:
 182                 return "device shut down";
 183         case -EPERM:
 184                 return "reject flag set";
 185         case -EHOSTUNREACH:
 186                 return "device suspended";
 187         default:
 188                 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
 189                 return unkmsg;
 190         }
 191 }
 192 
 193 /* get message text for USB status code
 194  */
 195 static char *get_usb_statmsg(int status)
 196 {
 197         static char unkmsg[28];
 198 
 199         switch (status) {
 200         case 0:
 201                 return "success";
 202         case -ENOENT:
 203                 return "unlinked (sync)";
 204         case -EINPROGRESS:
 205                 return "URB still pending";
 206         case -EPROTO:
 207                 return "bitstuff error, timeout, or unknown USB error";
 208         case -EILSEQ:
 209                 return "CRC mismatch, timeout, or unknown USB error";
 210         case -ETIME:
 211                 return "USB response timeout";
 212         case -EPIPE:
 213                 return "endpoint stalled";
 214         case -ECOMM:
 215                 return "IN buffer overrun";
 216         case -ENOSR:
 217                 return "OUT buffer underrun";
 218         case -EOVERFLOW:
 219                 return "endpoint babble";
 220         case -EREMOTEIO:
 221                 return "short packet";
 222         case -ENODEV:
 223                 return "device removed";
 224         case -EXDEV:
 225                 return "partial isoc transfer";
 226         case -EINVAL:
 227                 return "ISO madness";
 228         case -ECONNRESET:
 229                 return "unlinked (async)";
 230         case -ESHUTDOWN:
 231                 return "device shut down";
 232         default:
 233                 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
 234                 return unkmsg;
 235         }
 236 }
 237 
 238 /* usb_pipetype_str
 239  * retrieve string representation of USB pipe type
 240  */
 241 static inline char *usb_pipetype_str(int pipe)
 242 {
 243         if (usb_pipeisoc(pipe))
 244                 return "Isoc";
 245         if (usb_pipeint(pipe))
 246                 return "Int";
 247         if (usb_pipecontrol(pipe))
 248                 return "Ctrl";
 249         if (usb_pipebulk(pipe))
 250                 return "Bulk";
 251         return "?";
 252 }
 253 
 254 /* dump_urb
 255  * write content of URB to syslog for debugging
 256  */
 257 static inline void dump_urb(enum debuglevel level, const char *tag,
 258                             struct urb *urb)
 259 {
 260 #ifdef CONFIG_GIGASET_DEBUG
 261         int i;
 262         gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
 263         if (urb) {
 264                 gig_dbg(level,
 265                         "  dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
 266                         "hcpriv=0x%08lx, transfer_flags=0x%x,",
 267                         (unsigned long) urb->dev,
 268                         usb_pipetype_str(urb->pipe),
 269                         usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
 270                         usb_pipein(urb->pipe) ? "in" : "out",
 271                         (unsigned long) urb->hcpriv,
 272                         urb->transfer_flags);
 273                 gig_dbg(level,
 274                         "  transfer_buffer=0x%08lx[%d], actual_length=%d, "
 275                         "setup_packet=0x%08lx,",
 276                         (unsigned long) urb->transfer_buffer,
 277                         urb->transfer_buffer_length, urb->actual_length,
 278                         (unsigned long) urb->setup_packet);
 279                 gig_dbg(level,
 280                         "  start_frame=%d, number_of_packets=%d, interval=%d, "
 281                         "error_count=%d,",
 282                         urb->start_frame, urb->number_of_packets, urb->interval,
 283                         urb->error_count);
 284                 gig_dbg(level,
 285                         "  context=0x%08lx, complete=0x%08lx, "
 286                         "iso_frame_desc[]={",
 287                         (unsigned long) urb->context,
 288                         (unsigned long) urb->complete);
 289                 for (i = 0; i < urb->number_of_packets; i++) {
 290                         struct usb_iso_packet_descriptor *pifd
 291                                 = &urb->iso_frame_desc[i];
 292                         gig_dbg(level,
 293                                 "    {offset=%u, length=%u, actual_length=%u, "
 294                                 "status=%u}",
 295                                 pifd->offset, pifd->length, pifd->actual_length,
 296                                 pifd->status);
 297                 }
 298         }
 299         gig_dbg(level, "}}");
 300 #endif
 301 }
 302 
 303 /* read/set modem control bits etc. (m10x only) */
 304 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
 305                                   unsigned new_state)
 306 {
 307         return -EINVAL;
 308 }
 309 
 310 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
 311 {
 312         return -EINVAL;
 313 }
 314 
 315 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
 316 {
 317         return -EINVAL;
 318 }
 319 
 320 /* set/clear bits in base connection state, return previous state
 321  */
 322 static inline int update_basstate(struct bas_cardstate *ucs,
 323                                   int set, int clear)
 324 {
 325         unsigned long flags;
 326         int state;
 327 
 328         spin_lock_irqsave(&ucs->lock, flags);
 329         state = ucs->basstate;
 330         ucs->basstate = (state & ~clear) | set;
 331         spin_unlock_irqrestore(&ucs->lock, flags);
 332         return state;
 333 }
 334 
 335 /* error_hangup
 336  * hang up any existing connection because of an unrecoverable error
 337  * This function may be called from any context and takes care of scheduling
 338  * the necessary actions for execution outside of interrupt context.
 339  * cs->lock must not be held.
 340  * argument:
 341  *      B channel control structure
 342  */
 343 static inline void error_hangup(struct bc_state *bcs)
 344 {
 345         struct cardstate *cs = bcs->cs;
 346 
 347         gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
 348         gigaset_schedule_event(cs);
 349 }
 350 
 351 /* error_reset
 352  * reset Gigaset device because of an unrecoverable error
 353  * This function may be called from any context, and takes care of
 354  * scheduling the necessary actions for execution outside of interrupt context.
 355  * cs->hw.bas->lock must not be held.
 356  * argument:
 357  *      controller state structure
 358  */
 359 static inline void error_reset(struct cardstate *cs)
 360 {
 361         /* reset interrupt pipe to recover (ignore errors) */
 362         update_basstate(cs->hw.bas, BS_RESETTING, 0);
 363         if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))
 364                 /* submission failed, escalate to USB port reset */
 365                 usb_queue_reset_device(cs->hw.bas->interface);
 366 }
 367 
 368 /* check_pending
 369  * check for completion of pending control request
 370  * parameter:
 371  *      ucs     hardware specific controller state structure
 372  */
 373 static void check_pending(struct bas_cardstate *ucs)
 374 {
 375         unsigned long flags;
 376 
 377         spin_lock_irqsave(&ucs->lock, flags);
 378         switch (ucs->pending) {
 379         case 0:
 380                 break;
 381         case HD_OPEN_ATCHANNEL:
 382                 if (ucs->basstate & BS_ATOPEN)
 383                         ucs->pending = 0;
 384                 break;
 385         case HD_OPEN_B1CHANNEL:
 386                 if (ucs->basstate & BS_B1OPEN)
 387                         ucs->pending = 0;
 388                 break;
 389         case HD_OPEN_B2CHANNEL:
 390                 if (ucs->basstate & BS_B2OPEN)
 391                         ucs->pending = 0;
 392                 break;
 393         case HD_CLOSE_ATCHANNEL:
 394                 if (!(ucs->basstate & BS_ATOPEN))
 395                         ucs->pending = 0;
 396                 break;
 397         case HD_CLOSE_B1CHANNEL:
 398                 if (!(ucs->basstate & BS_B1OPEN))
 399                         ucs->pending = 0;
 400                 break;
 401         case HD_CLOSE_B2CHANNEL:
 402                 if (!(ucs->basstate & BS_B2OPEN))
 403                         ucs->pending = 0;
 404                 break;
 405         case HD_DEVICE_INIT_ACK:                /* no reply expected */
 406                 ucs->pending = 0;
 407                 break;
 408         case HD_RESET_INTERRUPT_PIPE:
 409                 if (!(ucs->basstate & BS_RESETTING))
 410                         ucs->pending = 0;
 411                 break;
 412         /*
 413          * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
 414          * and should never end up here
 415          */
 416         default:
 417                 dev_warn(&ucs->interface->dev,
 418                          "unknown pending request 0x%02x cleared\n",
 419                          ucs->pending);
 420                 ucs->pending = 0;
 421         }
 422 
 423         if (!ucs->pending)
 424                 del_timer(&ucs->timer_ctrl);
 425 
 426         spin_unlock_irqrestore(&ucs->lock, flags);
 427 }
 428 
 429 /* cmd_in_timeout
 430  * timeout routine for command input request
 431  * argument:
 432  *      controller state structure
 433  */
 434 static void cmd_in_timeout(struct timer_list *t)
 435 {
 436         struct bas_cardstate *ucs = from_timer(ucs, t, timer_cmd_in);
 437         struct cardstate *cs = ucs->cs;
 438         int rc;
 439 
 440         if (!ucs->rcvbuf_size) {
 441                 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
 442                 return;
 443         }
 444 
 445         if (ucs->retry_cmd_in++ >= BAS_RETRY) {
 446                 dev_err(cs->dev,
 447                         "control read: timeout, giving up after %d tries\n",
 448                         ucs->retry_cmd_in);
 449                 kfree(ucs->rcvbuf);
 450                 ucs->rcvbuf = NULL;
 451                 ucs->rcvbuf_size = 0;
 452                 error_reset(cs);
 453                 return;
 454         }
 455 
 456         gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
 457                 __func__, ucs->retry_cmd_in);
 458         rc = atread_submit(cs, BAS_TIMEOUT);
 459         if (rc < 0) {
 460                 kfree(ucs->rcvbuf);
 461                 ucs->rcvbuf = NULL;
 462                 ucs->rcvbuf_size = 0;
 463                 if (rc != -ENODEV)
 464                         error_reset(cs);
 465         }
 466 }
 467 
 468 /* read_ctrl_callback
 469  * USB completion handler for control pipe input
 470  * called by the USB subsystem in interrupt context
 471  * parameter:
 472  *      urb     USB request block
 473  *              urb->context = inbuf structure for controller state
 474  */
 475 static void read_ctrl_callback(struct urb *urb)
 476 {
 477         struct inbuf_t *inbuf = urb->context;
 478         struct cardstate *cs = inbuf->cs;
 479         struct bas_cardstate *ucs = cs->hw.bas;
 480         int status = urb->status;
 481         unsigned numbytes;
 482         int rc;
 483 
 484         update_basstate(ucs, 0, BS_ATRDPEND);
 485         wake_up(&ucs->waitqueue);
 486         del_timer(&ucs->timer_cmd_in);
 487 
 488         switch (status) {
 489         case 0:                         /* normal completion */
 490                 numbytes = urb->actual_length;
 491                 if (unlikely(numbytes != ucs->rcvbuf_size)) {
 492                         dev_warn(cs->dev,
 493                                  "control read: received %d chars, expected %d\n",
 494                                  numbytes, ucs->rcvbuf_size);
 495                         if (numbytes > ucs->rcvbuf_size)
 496                                 numbytes = ucs->rcvbuf_size;
 497                 }
 498 
 499                 /* copy received bytes to inbuf, notify event layer */
 500                 if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
 501                         gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
 502                         gigaset_schedule_event(cs);
 503                 }
 504                 break;
 505 
 506         case -ENOENT:                   /* cancelled */
 507         case -ECONNRESET:               /* cancelled (async) */
 508         case -EINPROGRESS:              /* pending */
 509         case -ENODEV:                   /* device removed */
 510         case -ESHUTDOWN:                /* device shut down */
 511                 /* no further action necessary */
 512                 gig_dbg(DEBUG_USBREQ, "%s: %s",
 513                         __func__, get_usb_statmsg(status));
 514                 break;
 515 
 516         default:                        /* other errors: retry */
 517                 if (ucs->retry_cmd_in++ < BAS_RETRY) {
 518                         gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
 519                                 get_usb_statmsg(status), ucs->retry_cmd_in);
 520                         rc = atread_submit(cs, BAS_TIMEOUT);
 521                         if (rc >= 0)
 522                                 /* successfully resubmitted, skip freeing */
 523                                 return;
 524                         if (rc == -ENODEV)
 525                                 /* disconnect, no further action necessary */
 526                                 break;
 527                 }
 528                 dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
 529                         get_usb_statmsg(status), ucs->retry_cmd_in);
 530                 error_reset(cs);
 531         }
 532 
 533         /* read finished, free buffer */
 534         kfree(ucs->rcvbuf);
 535         ucs->rcvbuf = NULL;
 536         ucs->rcvbuf_size = 0;
 537 }
 538 
 539 /* atread_submit
 540  * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
 541  * parameters:
 542  *      cs      controller state structure
 543  *      timeout timeout in 1/10 sec., 0: none
 544  * return value:
 545  *      0 on success
 546  *      -EBUSY if another request is pending
 547  *      any URB submission error code
 548  */
 549 static int atread_submit(struct cardstate *cs, int timeout)
 550 {
 551         struct bas_cardstate *ucs = cs->hw.bas;
 552         int basstate;
 553         int ret;
 554 
 555         gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
 556                 ucs->rcvbuf_size);
 557 
 558         basstate = update_basstate(ucs, BS_ATRDPEND, 0);
 559         if (basstate & BS_ATRDPEND) {
 560                 dev_err(cs->dev,
 561                         "could not submit HD_READ_ATMESSAGE: URB busy\n");
 562                 return -EBUSY;
 563         }
 564 
 565         if (basstate & BS_SUSPEND) {
 566                 dev_notice(cs->dev,
 567                            "HD_READ_ATMESSAGE not submitted, "
 568                            "suspend in progress\n");
 569                 update_basstate(ucs, 0, BS_ATRDPEND);
 570                 /* treat like disconnect */
 571                 return -ENODEV;
 572         }
 573 
 574         ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
 575         ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
 576         ucs->dr_cmd_in.wValue = 0;
 577         ucs->dr_cmd_in.wIndex = 0;
 578         ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
 579         usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
 580                              usb_rcvctrlpipe(ucs->udev, 0),
 581                              (unsigned char *) &ucs->dr_cmd_in,
 582                              ucs->rcvbuf, ucs->rcvbuf_size,
 583                              read_ctrl_callback, cs->inbuf);
 584 
 585         ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
 586         if (ret != 0) {
 587                 update_basstate(ucs, 0, BS_ATRDPEND);
 588                 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
 589                         get_usb_rcmsg(ret));
 590                 return ret;
 591         }
 592 
 593         if (timeout > 0) {
 594                 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
 595                 mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
 596         }
 597         return 0;
 598 }
 599 
 600 /* int_in_work
 601  * workqueue routine to clear halt on interrupt in endpoint
 602  */
 603 
 604 static void int_in_work(struct work_struct *work)
 605 {
 606         struct bas_cardstate *ucs =
 607                 container_of(work, struct bas_cardstate, int_in_wq);
 608         struct urb *urb = ucs->urb_int_in;
 609         struct cardstate *cs = urb->context;
 610         int rc;
 611 
 612         /* clear halt condition */
 613         rc = usb_clear_halt(ucs->udev, urb->pipe);
 614         gig_dbg(DEBUG_USBREQ, "clear_halt: %s", get_usb_rcmsg(rc));
 615         if (rc == 0)
 616                 /* success, resubmit interrupt read URB */
 617                 rc = usb_submit_urb(urb, GFP_ATOMIC);
 618 
 619         switch (rc) {
 620         case 0:         /* success */
 621         case -ENODEV:   /* device gone */
 622         case -EINVAL:   /* URB already resubmitted, or terminal badness */
 623                 break;
 624         default:        /* failure: try to recover by resetting the device */
 625                 dev_err(cs->dev, "clear halt failed: %s\n", get_usb_rcmsg(rc));
 626                 rc = usb_lock_device_for_reset(ucs->udev, ucs->interface);
 627                 if (rc == 0) {
 628                         rc = usb_reset_device(ucs->udev);
 629                         usb_unlock_device(ucs->udev);
 630                 }
 631         }
 632         ucs->retry_int_in = 0;
 633 }
 634 
 635 /* int_in_resubmit
 636  * timer routine for interrupt read delayed resubmit
 637  * argument:
 638  *      controller state structure
 639  */
 640 static void int_in_resubmit(struct timer_list *t)
 641 {
 642         struct bas_cardstate *ucs = from_timer(ucs, t, timer_int_in);
 643         struct cardstate *cs = ucs->cs;
 644         int rc;
 645 
 646         if (ucs->retry_int_in++ >= BAS_RETRY) {
 647                 dev_err(cs->dev, "interrupt read: giving up after %d tries\n",
 648                         ucs->retry_int_in);
 649                 usb_queue_reset_device(ucs->interface);
 650                 return;
 651         }
 652 
 653         gig_dbg(DEBUG_USBREQ, "%s: retry %d", __func__, ucs->retry_int_in);
 654         rc = usb_submit_urb(ucs->urb_int_in, GFP_ATOMIC);
 655         if (rc != 0 && rc != -ENODEV) {
 656                 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
 657                         get_usb_rcmsg(rc));
 658                 usb_queue_reset_device(ucs->interface);
 659         }
 660 }
 661 
 662 /* read_int_callback
 663  * USB completion handler for interrupt pipe input
 664  * called by the USB subsystem in interrupt context
 665  * parameter:
 666  *      urb     USB request block
 667  *              urb->context = controller state structure
 668  */
 669 static void read_int_callback(struct urb *urb)
 670 {
 671         struct cardstate *cs = urb->context;
 672         struct bas_cardstate *ucs = cs->hw.bas;
 673         struct bc_state *bcs;
 674         int status = urb->status;
 675         unsigned long flags;
 676         int rc;
 677         unsigned l;
 678         int channel;
 679 
 680         switch (status) {
 681         case 0:                 /* success */
 682                 ucs->retry_int_in = 0;
 683                 break;
 684         case -EPIPE:                    /* endpoint stalled */
 685                 schedule_work(&ucs->int_in_wq);
 686                 /* fall through */
 687         case -ENOENT:                   /* cancelled */
 688         case -ECONNRESET:               /* cancelled (async) */
 689         case -EINPROGRESS:              /* pending */
 690         case -ENODEV:                   /* device removed */
 691         case -ESHUTDOWN:                /* device shut down */
 692                 /* no further action necessary */
 693                 gig_dbg(DEBUG_USBREQ, "%s: %s",
 694                         __func__, get_usb_statmsg(status));
 695                 return;
 696         case -EPROTO:                   /* protocol error or unplug */
 697         case -EILSEQ:
 698         case -ETIME:
 699                 /* resubmit after delay */
 700                 gig_dbg(DEBUG_USBREQ, "%s: %s",
 701                         __func__, get_usb_statmsg(status));
 702                 mod_timer(&ucs->timer_int_in, jiffies + HZ / 10);
 703                 return;
 704         default:                /* other errors: just resubmit */
 705                 dev_warn(cs->dev, "interrupt read: %s\n",
 706                          get_usb_statmsg(status));
 707                 goto resubmit;
 708         }
 709 
 710         /* drop incomplete packets even if the missing bytes wouldn't matter */
 711         if (unlikely(urb->actual_length < IP_MSGSIZE)) {
 712                 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
 713                          urb->actual_length);
 714                 goto resubmit;
 715         }
 716 
 717         l = (unsigned) ucs->int_in_buf[1] +
 718                 (((unsigned) ucs->int_in_buf[2]) << 8);
 719 
 720         gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
 721                 urb->actual_length, (int)ucs->int_in_buf[0], l,
 722                 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
 723 
 724         channel = 0;
 725 
 726         switch (ucs->int_in_buf[0]) {
 727         case HD_DEVICE_INIT_OK:
 728                 update_basstate(ucs, BS_INIT, 0);
 729                 break;
 730 
 731         case HD_READY_SEND_ATDATA:
 732                 del_timer(&ucs->timer_atrdy);
 733                 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
 734                 start_cbsend(cs);
 735                 break;
 736 
 737         case HD_OPEN_B2CHANNEL_ACK:
 738                 ++channel;
 739                 /* fall through */
 740         case HD_OPEN_B1CHANNEL_ACK:
 741                 bcs = cs->bcs + channel;
 742                 update_basstate(ucs, BS_B1OPEN << channel, 0);
 743                 gigaset_bchannel_up(bcs);
 744                 break;
 745 
 746         case HD_OPEN_ATCHANNEL_ACK:
 747                 update_basstate(ucs, BS_ATOPEN, 0);
 748                 start_cbsend(cs);
 749                 break;
 750 
 751         case HD_CLOSE_B2CHANNEL_ACK:
 752                 ++channel;
 753                 /* fall through */
 754         case HD_CLOSE_B1CHANNEL_ACK:
 755                 bcs = cs->bcs + channel;
 756                 update_basstate(ucs, 0, BS_B1OPEN << channel);
 757                 stopurbs(bcs->hw.bas);
 758                 gigaset_bchannel_down(bcs);
 759                 break;
 760 
 761         case HD_CLOSE_ATCHANNEL_ACK:
 762                 update_basstate(ucs, 0, BS_ATOPEN);
 763                 break;
 764 
 765         case HD_B2_FLOW_CONTROL:
 766                 ++channel;
 767                 /* fall through */
 768         case HD_B1_FLOW_CONTROL:
 769                 bcs = cs->bcs + channel;
 770                 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
 771                            &bcs->hw.bas->corrbytes);
 772                 gig_dbg(DEBUG_ISO,
 773                         "Flow control (channel %d, sub %d): 0x%02x => %d",
 774                         channel, bcs->hw.bas->numsub, l,
 775                         atomic_read(&bcs->hw.bas->corrbytes));
 776                 break;
 777 
 778         case HD_RECEIVEATDATA_ACK:      /* AT response ready to be received */
 779                 if (!l) {
 780                         dev_warn(cs->dev,
 781                                  "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
 782                         break;
 783                 }
 784                 spin_lock_irqsave(&cs->lock, flags);
 785                 if (ucs->basstate & BS_ATRDPEND) {
 786                         spin_unlock_irqrestore(&cs->lock, flags);
 787                         dev_warn(cs->dev,
 788                                  "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
 789                                  l, ucs->rcvbuf_size);
 790                         break;
 791                 }
 792                 if (ucs->rcvbuf_size) {
 793                         /* throw away previous buffer - we have no queue */
 794                         dev_err(cs->dev,
 795                                 "receive AT data overrun, %d bytes lost\n",
 796                                 ucs->rcvbuf_size);
 797                         kfree(ucs->rcvbuf);
 798                         ucs->rcvbuf_size = 0;
 799                 }
 800                 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
 801                 if (ucs->rcvbuf == NULL) {
 802                         spin_unlock_irqrestore(&cs->lock, flags);
 803                         dev_err(cs->dev, "out of memory receiving AT data\n");
 804                         break;
 805                 }
 806                 ucs->rcvbuf_size = l;
 807                 ucs->retry_cmd_in = 0;
 808                 rc = atread_submit(cs, BAS_TIMEOUT);
 809                 if (rc < 0) {
 810                         kfree(ucs->rcvbuf);
 811                         ucs->rcvbuf = NULL;
 812                         ucs->rcvbuf_size = 0;
 813                 }
 814                 spin_unlock_irqrestore(&cs->lock, flags);
 815                 if (rc < 0 && rc != -ENODEV)
 816                         error_reset(cs);
 817                 break;
 818 
 819         case HD_RESET_INTERRUPT_PIPE_ACK:
 820                 update_basstate(ucs, 0, BS_RESETTING);
 821                 dev_notice(cs->dev, "interrupt pipe reset\n");
 822                 break;
 823 
 824         case HD_SUSPEND_END:
 825                 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
 826                 break;
 827 
 828         default:
 829                 dev_warn(cs->dev,
 830                          "unknown Gigaset signal 0x%02x (%u) ignored\n",
 831                          (int) ucs->int_in_buf[0], l);
 832         }
 833 
 834         check_pending(ucs);
 835         wake_up(&ucs->waitqueue);
 836 
 837 resubmit:
 838         rc = usb_submit_urb(urb, GFP_ATOMIC);
 839         if (unlikely(rc != 0 && rc != -ENODEV)) {
 840                 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
 841                         get_usb_rcmsg(rc));
 842                 error_reset(cs);
 843         }
 844 }
 845 
 846 /* read_iso_callback
 847  * USB completion handler for B channel isochronous input
 848  * called by the USB subsystem in interrupt context
 849  * parameter:
 850  *      urb     USB request block of completed request
 851  *              urb->context = bc_state structure
 852  */
 853 static void read_iso_callback(struct urb *urb)
 854 {
 855         struct bc_state *bcs;
 856         struct bas_bc_state *ubc;
 857         int status = urb->status;
 858         unsigned long flags;
 859         int i, rc;
 860 
 861         /* status codes not worth bothering the tasklet with */
 862         if (unlikely(status == -ENOENT ||
 863                      status == -ECONNRESET ||
 864                      status == -EINPROGRESS ||
 865                      status == -ENODEV ||
 866                      status == -ESHUTDOWN)) {
 867                 gig_dbg(DEBUG_ISO, "%s: %s",
 868                         __func__, get_usb_statmsg(status));
 869                 return;
 870         }
 871 
 872         bcs = urb->context;
 873         ubc = bcs->hw.bas;
 874 
 875         spin_lock_irqsave(&ubc->isoinlock, flags);
 876         if (likely(ubc->isoindone == NULL)) {
 877                 /* pass URB to tasklet */
 878                 ubc->isoindone = urb;
 879                 ubc->isoinstatus = status;
 880                 tasklet_hi_schedule(&ubc->rcvd_tasklet);
 881         } else {
 882                 /* tasklet still busy, drop data and resubmit URB */
 883                 gig_dbg(DEBUG_ISO, "%s: overrun", __func__);
 884                 ubc->loststatus = status;
 885                 for (i = 0; i < BAS_NUMFRAMES; i++) {
 886                         ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
 887                         if (unlikely(urb->iso_frame_desc[i].status != 0 &&
 888                                      urb->iso_frame_desc[i].status != -EINPROGRESS))
 889                                 ubc->loststatus = urb->iso_frame_desc[i].status;
 890                         urb->iso_frame_desc[i].status = 0;
 891                         urb->iso_frame_desc[i].actual_length = 0;
 892                 }
 893                 if (likely(ubc->running)) {
 894                         /* urb->dev is clobbered by USB subsystem */
 895                         urb->dev = bcs->cs->hw.bas->udev;
 896                         urb->transfer_flags = URB_ISO_ASAP;
 897                         urb->number_of_packets = BAS_NUMFRAMES;
 898                         rc = usb_submit_urb(urb, GFP_ATOMIC);
 899                         if (unlikely(rc != 0 && rc != -ENODEV)) {
 900                                 dev_err(bcs->cs->dev,
 901                                         "could not resubmit isoc read URB: %s\n",
 902                                         get_usb_rcmsg(rc));
 903                                 dump_urb(DEBUG_ISO, "isoc read", urb);
 904                                 error_hangup(bcs);
 905                         }
 906                 }
 907         }
 908         spin_unlock_irqrestore(&ubc->isoinlock, flags);
 909 }
 910 
 911 /* write_iso_callback
 912  * USB completion handler for B channel isochronous output
 913  * called by the USB subsystem in interrupt context
 914  * parameter:
 915  *      urb     USB request block of completed request
 916  *              urb->context = isow_urbctx_t structure
 917  */
 918 static void write_iso_callback(struct urb *urb)
 919 {
 920         struct isow_urbctx_t *ucx;
 921         struct bas_bc_state *ubc;
 922         int status = urb->status;
 923         unsigned long flags;
 924 
 925         /* status codes not worth bothering the tasklet with */
 926         if (unlikely(status == -ENOENT ||
 927                      status == -ECONNRESET ||
 928                      status == -EINPROGRESS ||
 929                      status == -ENODEV ||
 930                      status == -ESHUTDOWN)) {
 931                 gig_dbg(DEBUG_ISO, "%s: %s",
 932                         __func__, get_usb_statmsg(status));
 933                 return;
 934         }
 935 
 936         /* pass URB context to tasklet */
 937         ucx = urb->context;
 938         ubc = ucx->bcs->hw.bas;
 939         ucx->status = status;
 940 
 941         spin_lock_irqsave(&ubc->isooutlock, flags);
 942         ubc->isooutovfl = ubc->isooutdone;
 943         ubc->isooutdone = ucx;
 944         spin_unlock_irqrestore(&ubc->isooutlock, flags);
 945         tasklet_hi_schedule(&ubc->sent_tasklet);
 946 }
 947 
 948 /* starturbs
 949  * prepare and submit USB request blocks for isochronous input and output
 950  * argument:
 951  *      B channel control structure
 952  * return value:
 953  *      0 on success
 954  *      < 0 on error (no URBs submitted)
 955  */
 956 static int starturbs(struct bc_state *bcs)
 957 {
 958         struct usb_device *udev = bcs->cs->hw.bas->udev;
 959         struct bas_bc_state *ubc = bcs->hw.bas;
 960         struct urb *urb;
 961         int j, k;
 962         int rc;
 963 
 964         /* initialize L2 reception */
 965         if (bcs->proto2 == L2_HDLC)
 966                 bcs->inputstate |= INS_flag_hunt;
 967 
 968         /* submit all isochronous input URBs */
 969         ubc->running = 1;
 970         for (k = 0; k < BAS_INURBS; k++) {
 971                 urb = ubc->isoinurbs[k];
 972                 if (!urb) {
 973                         rc = -EFAULT;
 974                         goto error;
 975                 }
 976                 usb_fill_int_urb(urb, udev,
 977                                  usb_rcvisocpipe(udev, 3 + 2 * bcs->channel),
 978                                  ubc->isoinbuf + k * BAS_INBUFSIZE,
 979                                  BAS_INBUFSIZE, read_iso_callback, bcs,
 980                                  BAS_FRAMETIME);
 981 
 982                 urb->transfer_flags = URB_ISO_ASAP;
 983                 urb->number_of_packets = BAS_NUMFRAMES;
 984                 for (j = 0; j < BAS_NUMFRAMES; j++) {
 985                         urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
 986                         urb->iso_frame_desc[j].length = BAS_MAXFRAME;
 987                         urb->iso_frame_desc[j].status = 0;
 988                         urb->iso_frame_desc[j].actual_length = 0;
 989                 }
 990 
 991                 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
 992                 rc = usb_submit_urb(urb, GFP_ATOMIC);
 993                 if (rc != 0)
 994                         goto error;
 995         }
 996 
 997         /* initialize L2 transmission */
 998         gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
 999 
1000         /* set up isochronous output URBs for flag idling */
1001         for (k = 0; k < BAS_OUTURBS; ++k) {
1002                 urb = ubc->isoouturbs[k].urb;
1003                 if (!urb) {
1004                         rc = -EFAULT;
1005                         goto error;
1006                 }
1007                 usb_fill_int_urb(urb, udev,
1008                                  usb_sndisocpipe(udev, 4 + 2 * bcs->channel),
1009                                  ubc->isooutbuf->data,
1010                                  sizeof(ubc->isooutbuf->data),
1011                                  write_iso_callback, &ubc->isoouturbs[k],
1012                                  BAS_FRAMETIME);
1013 
1014                 urb->transfer_flags = URB_ISO_ASAP;
1015                 urb->number_of_packets = BAS_NUMFRAMES;
1016                 for (j = 0; j < BAS_NUMFRAMES; ++j) {
1017                         urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
1018                         urb->iso_frame_desc[j].length = BAS_NORMFRAME;
1019                         urb->iso_frame_desc[j].status = 0;
1020                         urb->iso_frame_desc[j].actual_length = 0;
1021                 }
1022                 ubc->isoouturbs[k].limit = -1;
1023         }
1024 
1025         /* keep one URB free, submit the others */
1026         for (k = 0; k < BAS_OUTURBS - 1; ++k) {
1027                 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
1028                 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
1029                 if (rc != 0)
1030                         goto error;
1031         }
1032         dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
1033         ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS - 1];
1034         ubc->isooutdone = ubc->isooutovfl = NULL;
1035         return 0;
1036 error:
1037         stopurbs(ubc);
1038         return rc;
1039 }
1040 
1041 /* stopurbs
1042  * cancel the USB request blocks for isochronous input and output
1043  * errors are silently ignored
1044  * argument:
1045  *      B channel control structure
1046  */
1047 static void stopurbs(struct bas_bc_state *ubc)
1048 {
1049         int k, rc;
1050 
1051         ubc->running = 0;
1052 
1053         for (k = 0; k < BAS_INURBS; ++k) {
1054                 rc = usb_unlink_urb(ubc->isoinurbs[k]);
1055                 gig_dbg(DEBUG_ISO,
1056                         "%s: isoc input URB %d unlinked, result = %s",
1057                         __func__, k, get_usb_rcmsg(rc));
1058         }
1059 
1060         for (k = 0; k < BAS_OUTURBS; ++k) {
1061                 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
1062                 gig_dbg(DEBUG_ISO,
1063                         "%s: isoc output URB %d unlinked, result = %s",
1064                         __func__, k, get_usb_rcmsg(rc));
1065         }
1066 }
1067 
1068 /* Isochronous Write - Bottom Half */
1069 /* =============================== */
1070 
1071 /* submit_iso_write_urb
1072  * fill and submit the next isochronous write URB
1073  * parameters:
1074  *      ucx     context structure containing URB
1075  * return value:
1076  *      number of frames submitted in URB
1077  *      0 if URB not submitted because no data available (isooutbuf busy)
1078  *      error code < 0 on error
1079  */
1080 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1081 {
1082         struct urb *urb = ucx->urb;
1083         struct bas_bc_state *ubc = ucx->bcs->hw.bas;
1084         struct usb_iso_packet_descriptor *ifd;
1085         int corrbytes, nframe, rc;
1086 
1087         /* urb->dev is clobbered by USB subsystem */
1088         urb->dev = ucx->bcs->cs->hw.bas->udev;
1089         urb->transfer_flags = URB_ISO_ASAP;
1090         urb->transfer_buffer = ubc->isooutbuf->data;
1091         urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1092 
1093         for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1094                 ifd = &urb->iso_frame_desc[nframe];
1095 
1096                 /* compute frame length according to flow control */
1097                 ifd->length = BAS_NORMFRAME;
1098                 corrbytes = atomic_read(&ubc->corrbytes);
1099                 if (corrbytes != 0) {
1100                         gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1101                                 __func__, corrbytes);
1102                         if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1103                                 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1104                         else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1105                                 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1106                         ifd->length += corrbytes;
1107                         atomic_add(-corrbytes, &ubc->corrbytes);
1108                 }
1109 
1110                 /* retrieve block of data to send */
1111                 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1112                 if (rc < 0) {
1113                         if (rc == -EBUSY) {
1114                                 gig_dbg(DEBUG_ISO,
1115                                         "%s: buffer busy at frame %d",
1116                                         __func__, nframe);
1117                                 /* tasklet will be restarted from
1118                                    gigaset_isoc_send_skb() */
1119                         } else {
1120                                 dev_err(ucx->bcs->cs->dev,
1121                                         "%s: buffer error %d at frame %d\n",
1122                                         __func__, rc, nframe);
1123                                 return rc;
1124                         }
1125                         break;
1126                 }
1127                 ifd->offset = rc;
1128                 ucx->limit = ubc->isooutbuf->nextread;
1129                 ifd->status = 0;
1130                 ifd->actual_length = 0;
1131         }
1132         if (unlikely(nframe == 0))
1133                 return 0;       /* no data to send */
1134         urb->number_of_packets = nframe;
1135 
1136         rc = usb_submit_urb(urb, GFP_ATOMIC);
1137         if (unlikely(rc)) {
1138                 if (rc == -ENODEV)
1139                         /* device removed - give up silently */
1140                         gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1141                 else
1142                         dev_err(ucx->bcs->cs->dev,
1143                                 "could not submit isoc write URB: %s\n",
1144                                 get_usb_rcmsg(rc));
1145                 return rc;
1146         }
1147         ++ubc->numsub;
1148         return nframe;
1149 }
1150 
1151 /* write_iso_tasklet
1152  * tasklet scheduled when an isochronous output URB from the Gigaset device
1153  * has completed
1154  * parameter:
1155  *      data    B channel state structure
1156  */
1157 static void write_iso_tasklet(unsigned long data)
1158 {
1159         struct bc_state *bcs = (struct bc_state *) data;
1160         struct bas_bc_state *ubc = bcs->hw.bas;
1161         struct cardstate *cs = bcs->cs;
1162         struct isow_urbctx_t *done, *next, *ovfl;
1163         struct urb *urb;
1164         int status;
1165         struct usb_iso_packet_descriptor *ifd;
1166         unsigned long flags;
1167         int i;
1168         struct sk_buff *skb;
1169         int len;
1170         int rc;
1171 
1172         /* loop while completed URBs arrive in time */
1173         for (;;) {
1174                 if (unlikely(!(ubc->running))) {
1175                         gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1176                         return;
1177                 }
1178 
1179                 /* retrieve completed URBs */
1180                 spin_lock_irqsave(&ubc->isooutlock, flags);
1181                 done = ubc->isooutdone;
1182                 ubc->isooutdone = NULL;
1183                 ovfl = ubc->isooutovfl;
1184                 ubc->isooutovfl = NULL;
1185                 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1186                 if (ovfl) {
1187                         dev_err(cs->dev, "isoc write underrun\n");
1188                         error_hangup(bcs);
1189                         break;
1190                 }
1191                 if (!done)
1192                         break;
1193 
1194                 /* submit free URB if available */
1195                 spin_lock_irqsave(&ubc->isooutlock, flags);
1196                 next = ubc->isooutfree;
1197                 ubc->isooutfree = NULL;
1198                 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1199                 if (next) {
1200                         rc = submit_iso_write_urb(next);
1201                         if (unlikely(rc <= 0 && rc != -ENODEV)) {
1202                                 /* could not submit URB, put it back */
1203                                 spin_lock_irqsave(&ubc->isooutlock, flags);
1204                                 if (ubc->isooutfree == NULL) {
1205                                         ubc->isooutfree = next;
1206                                         next = NULL;
1207                                 }
1208                                 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1209                                 if (next) {
1210                                         /* couldn't put it back */
1211                                         dev_err(cs->dev,
1212                                                 "losing isoc write URB\n");
1213                                         error_hangup(bcs);
1214                                 }
1215                         }
1216                 }
1217 
1218                 /* process completed URB */
1219                 urb = done->urb;
1220                 status = done->status;
1221                 switch (status) {
1222                 case -EXDEV:                    /* partial completion */
1223                         gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1224                                 __func__);
1225                         /* fall through - what's the difference anyway? */
1226                 case 0:                         /* normal completion */
1227                         /* inspect individual frames
1228                          * assumptions (for lack of documentation):
1229                          * - actual_length bytes of first frame in error are
1230                          *   successfully sent
1231                          * - all following frames are not sent at all
1232                          */
1233                         for (i = 0; i < BAS_NUMFRAMES; i++) {
1234                                 ifd = &urb->iso_frame_desc[i];
1235                                 if (ifd->status ||
1236                                     ifd->actual_length != ifd->length) {
1237                                         dev_warn(cs->dev,
1238                                                  "isoc write: frame %d[%d/%d]: %s\n",
1239                                                  i, ifd->actual_length,
1240                                                  ifd->length,
1241                                                  get_usb_statmsg(ifd->status));
1242                                         break;
1243                                 }
1244                         }
1245                         break;
1246                 case -EPIPE:                    /* stall - probably underrun */
1247                         dev_err(cs->dev, "isoc write: stalled\n");
1248                         error_hangup(bcs);
1249                         break;
1250                 default:                        /* other errors */
1251                         dev_warn(cs->dev, "isoc write: %s\n",
1252                                  get_usb_statmsg(status));
1253                 }
1254 
1255                 /* mark the write buffer area covered by this URB as free */
1256                 if (done->limit >= 0)
1257                         ubc->isooutbuf->read = done->limit;
1258 
1259                 /* mark URB as free */
1260                 spin_lock_irqsave(&ubc->isooutlock, flags);
1261                 next = ubc->isooutfree;
1262                 ubc->isooutfree = done;
1263                 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1264                 if (next) {
1265                         /* only one URB still active - resubmit one */
1266                         rc = submit_iso_write_urb(next);
1267                         if (unlikely(rc <= 0 && rc != -ENODEV)) {
1268                                 /* couldn't submit */
1269                                 error_hangup(bcs);
1270                         }
1271                 }
1272         }
1273 
1274         /* process queued SKBs */
1275         while ((skb = skb_dequeue(&bcs->squeue))) {
1276                 /* copy to output buffer, doing L2 encapsulation */
1277                 len = skb->len;
1278                 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1279                         /* insufficient buffer space, push back onto queue */
1280                         skb_queue_head(&bcs->squeue, skb);
1281                         gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1282                                 __func__, skb_queue_len(&bcs->squeue));
1283                         break;
1284                 }
1285                 skb_pull(skb, len);
1286                 gigaset_skb_sent(bcs, skb);
1287                 dev_kfree_skb_any(skb);
1288         }
1289 }
1290 
1291 /* Isochronous Read - Bottom Half */
1292 /* ============================== */
1293 
1294 /* read_iso_tasklet
1295  * tasklet scheduled when an isochronous input URB from the Gigaset device
1296  * has completed
1297  * parameter:
1298  *      data    B channel state structure
1299  */
1300 static void read_iso_tasklet(unsigned long data)
1301 {
1302         struct bc_state *bcs = (struct bc_state *) data;
1303         struct bas_bc_state *ubc = bcs->hw.bas;
1304         struct cardstate *cs = bcs->cs;
1305         struct urb *urb;
1306         int status;
1307         struct usb_iso_packet_descriptor *ifd;
1308         char *rcvbuf;
1309         unsigned long flags;
1310         int totleft, numbytes, offset, frame, rc;
1311 
1312         /* loop while more completed URBs arrive in the meantime */
1313         for (;;) {
1314                 /* retrieve URB */
1315                 spin_lock_irqsave(&ubc->isoinlock, flags);
1316                 urb = ubc->isoindone;
1317                 if (!urb) {
1318                         spin_unlock_irqrestore(&ubc->isoinlock, flags);
1319                         return;
1320                 }
1321                 status = ubc->isoinstatus;
1322                 ubc->isoindone = NULL;
1323                 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1324                         dev_warn(cs->dev,
1325                                  "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
1326                                  get_usb_statmsg(ubc->loststatus),
1327                                  ubc->isoinlost);
1328                         ubc->loststatus = -EINPROGRESS;
1329                 }
1330                 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1331 
1332                 if (unlikely(!(ubc->running))) {
1333                         gig_dbg(DEBUG_ISO,
1334                                 "%s: channel not running, "
1335                                 "dropped URB with status: %s",
1336                                 __func__, get_usb_statmsg(status));
1337                         return;
1338                 }
1339 
1340                 switch (status) {
1341                 case 0:                         /* normal completion */
1342                         break;
1343                 case -EXDEV:                    /* inspect individual frames
1344                                                    (we do that anyway) */
1345                         gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1346                                 __func__);
1347                         break;
1348                 case -ENOENT:
1349                 case -ECONNRESET:
1350                 case -EINPROGRESS:
1351                         gig_dbg(DEBUG_ISO, "%s: %s",
1352                                 __func__, get_usb_statmsg(status));
1353                         continue;               /* -> skip */
1354                 case -EPIPE:
1355                         dev_err(cs->dev, "isoc read: stalled\n");
1356                         error_hangup(bcs);
1357                         continue;               /* -> skip */
1358                 default:                        /* other error */
1359                         dev_warn(cs->dev, "isoc read: %s\n",
1360                                  get_usb_statmsg(status));
1361                         goto error;
1362                 }
1363 
1364                 rcvbuf = urb->transfer_buffer;
1365                 totleft = urb->actual_length;
1366                 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1367                         ifd = &urb->iso_frame_desc[frame];
1368                         numbytes = ifd->actual_length;
1369                         switch (ifd->status) {
1370                         case 0:                 /* success */
1371                                 break;
1372                         case -EPROTO:           /* protocol error or unplug */
1373                         case -EILSEQ:
1374                         case -ETIME:
1375                                 /* probably just disconnected, ignore */
1376                                 gig_dbg(DEBUG_ISO,
1377                                         "isoc read: frame %d[%d]: %s\n",
1378                                         frame, numbytes,
1379                                         get_usb_statmsg(ifd->status));
1380                                 break;
1381                         default:                /* other error */
1382                                 /* report, assume transferred bytes are ok */
1383                                 dev_warn(cs->dev,
1384                                          "isoc read: frame %d[%d]: %s\n",
1385                                          frame, numbytes,
1386                                          get_usb_statmsg(ifd->status));
1387                         }
1388                         if (unlikely(numbytes > BAS_MAXFRAME))
1389                                 dev_warn(cs->dev,
1390                                          "isoc read: frame %d[%d]: %s\n",
1391                                          frame, numbytes,
1392                                          "exceeds max frame size");
1393                         if (unlikely(numbytes > totleft)) {
1394                                 dev_warn(cs->dev,
1395                                          "isoc read: frame %d[%d]: %s\n",
1396                                          frame, numbytes,
1397                                          "exceeds total transfer length");
1398                                 numbytes = totleft;
1399                         }
1400                         offset = ifd->offset;
1401                         if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1402                                 dev_warn(cs->dev,
1403                                          "isoc read: frame %d[%d]: %s\n",
1404                                          frame, numbytes,
1405                                          "exceeds end of buffer");
1406                                 numbytes = BAS_INBUFSIZE - offset;
1407                         }
1408                         gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1409                         totleft -= numbytes;
1410                 }
1411                 if (unlikely(totleft > 0))
1412                         dev_warn(cs->dev, "isoc read: %d data bytes missing\n",
1413                                  totleft);
1414 
1415 error:
1416                 /* URB processed, resubmit */
1417                 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1418                         urb->iso_frame_desc[frame].status = 0;
1419                         urb->iso_frame_desc[frame].actual_length = 0;
1420                 }
1421                 /* urb->dev is clobbered by USB subsystem */
1422                 urb->dev = bcs->cs->hw.bas->udev;
1423                 urb->transfer_flags = URB_ISO_ASAP;
1424                 urb->number_of_packets = BAS_NUMFRAMES;
1425                 rc = usb_submit_urb(urb, GFP_ATOMIC);
1426                 if (unlikely(rc != 0 && rc != -ENODEV)) {
1427                         dev_err(cs->dev,
1428                                 "could not resubmit isoc read URB: %s\n",
1429                                 get_usb_rcmsg(rc));
1430                         dump_urb(DEBUG_ISO, "resubmit isoc read", urb);
1431                         error_hangup(bcs);
1432                 }
1433         }
1434 }
1435 
1436 /* Channel Operations */
1437 /* ================== */
1438 
1439 /* req_timeout
1440  * timeout routine for control output request
1441  * argument:
1442  *      controller state structure
1443  */
1444 static void req_timeout(struct timer_list *t)
1445 {
1446         struct bas_cardstate *ucs = from_timer(ucs, t, timer_ctrl);
1447         struct cardstate *cs = ucs->cs;
1448         int pending;
1449         unsigned long flags;
1450 
1451         check_pending(ucs);
1452 
1453         spin_lock_irqsave(&ucs->lock, flags);
1454         pending = ucs->pending;
1455         ucs->pending = 0;
1456         spin_unlock_irqrestore(&ucs->lock, flags);
1457 
1458         switch (pending) {
1459         case 0:                                 /* no pending request */
1460                 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1461                 break;
1462 
1463         case HD_OPEN_ATCHANNEL:
1464                 dev_err(cs->dev, "timeout opening AT channel\n");
1465                 error_reset(cs);
1466                 break;
1467 
1468         case HD_OPEN_B1CHANNEL:
1469                 dev_err(cs->dev, "timeout opening channel 1\n");
1470                 error_hangup(&cs->bcs[0]);
1471                 break;
1472 
1473         case HD_OPEN_B2CHANNEL:
1474                 dev_err(cs->dev, "timeout opening channel 2\n");
1475                 error_hangup(&cs->bcs[1]);
1476                 break;
1477 
1478         case HD_CLOSE_ATCHANNEL:
1479                 dev_err(cs->dev, "timeout closing AT channel\n");
1480                 error_reset(cs);
1481                 break;
1482 
1483         case HD_CLOSE_B1CHANNEL:
1484                 dev_err(cs->dev, "timeout closing channel 1\n");
1485                 error_reset(cs);
1486                 break;
1487 
1488         case HD_CLOSE_B2CHANNEL:
1489                 dev_err(cs->dev, "timeout closing channel 2\n");
1490                 error_reset(cs);
1491                 break;
1492 
1493         case HD_RESET_INTERRUPT_PIPE:
1494                 /* error recovery escalation */
1495                 dev_err(cs->dev,
1496                         "reset interrupt pipe timeout, attempting USB reset\n");
1497                 usb_queue_reset_device(ucs->interface);
1498                 break;
1499 
1500         default:
1501                 dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
1502                          pending);
1503         }
1504 
1505         wake_up(&ucs->waitqueue);
1506 }
1507 
1508 /* write_ctrl_callback
1509  * USB completion handler for control pipe output
1510  * called by the USB subsystem in interrupt context
1511  * parameter:
1512  *      urb     USB request block of completed request
1513  *              urb->context = hardware specific controller state structure
1514  */
1515 static void write_ctrl_callback(struct urb *urb)
1516 {
1517         struct bas_cardstate *ucs = urb->context;
1518         int status = urb->status;
1519         int rc;
1520         unsigned long flags;
1521 
1522         /* check status */
1523         switch (status) {
1524         case 0:                                 /* normal completion */
1525                 spin_lock_irqsave(&ucs->lock, flags);
1526                 switch (ucs->pending) {
1527                 case HD_DEVICE_INIT_ACK:        /* no reply expected */
1528                         del_timer(&ucs->timer_ctrl);
1529                         ucs->pending = 0;
1530                         break;
1531                 }
1532                 spin_unlock_irqrestore(&ucs->lock, flags);
1533                 return;
1534 
1535         case -ENOENT:                   /* cancelled */
1536         case -ECONNRESET:               /* cancelled (async) */
1537         case -EINPROGRESS:              /* pending */
1538         case -ENODEV:                   /* device removed */
1539         case -ESHUTDOWN:                /* device shut down */
1540                 /* ignore silently */
1541                 gig_dbg(DEBUG_USBREQ, "%s: %s",
1542                         __func__, get_usb_statmsg(status));
1543                 break;
1544 
1545         default:                                /* any failure */
1546                 /* don't retry if suspend requested */
1547                 if (++ucs->retry_ctrl > BAS_RETRY ||
1548                     (ucs->basstate & BS_SUSPEND)) {
1549                         dev_err(&ucs->interface->dev,
1550                                 "control request 0x%02x failed: %s\n",
1551                                 ucs->dr_ctrl.bRequest,
1552                                 get_usb_statmsg(status));
1553                         break;          /* give up */
1554                 }
1555                 dev_notice(&ucs->interface->dev,
1556                            "control request 0x%02x: %s, retry %d\n",
1557                            ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
1558                            ucs->retry_ctrl);
1559                 /* urb->dev is clobbered by USB subsystem */
1560                 urb->dev = ucs->udev;
1561                 rc = usb_submit_urb(urb, GFP_ATOMIC);
1562                 if (unlikely(rc)) {
1563                         dev_err(&ucs->interface->dev,
1564                                 "could not resubmit request 0x%02x: %s\n",
1565                                 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1566                         break;
1567                 }
1568                 /* resubmitted */
1569                 return;
1570         }
1571 
1572         /* failed, clear pending request */
1573         spin_lock_irqsave(&ucs->lock, flags);
1574         del_timer(&ucs->timer_ctrl);
1575         ucs->pending = 0;
1576         spin_unlock_irqrestore(&ucs->lock, flags);
1577         wake_up(&ucs->waitqueue);
1578 }
1579 
1580 /* req_submit
1581  * submit a control output request without message buffer to the Gigaset base
1582  * and optionally start a timeout
1583  * parameters:
1584  *      bcs     B channel control structure
1585  *      req     control request code (HD_*)
1586  *      val     control request parameter value (set to 0 if unused)
1587  *      timeout timeout in seconds (0: no timeout)
1588  * return value:
1589  *      0 on success
1590  *      -EBUSY if another request is pending
1591  *      any URB submission error code
1592  */
1593 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1594 {
1595         struct bas_cardstate *ucs = bcs->cs->hw.bas;
1596         int ret;
1597         unsigned long flags;
1598 
1599         gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1600 
1601         spin_lock_irqsave(&ucs->lock, flags);
1602         if (ucs->pending) {
1603                 spin_unlock_irqrestore(&ucs->lock, flags);
1604                 dev_err(bcs->cs->dev,
1605                         "submission of request 0x%02x failed: "
1606                         "request 0x%02x still pending\n",
1607                         req, ucs->pending);
1608                 return -EBUSY;
1609         }
1610 
1611         ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1612         ucs->dr_ctrl.bRequest = req;
1613         ucs->dr_ctrl.wValue = cpu_to_le16(val);
1614         ucs->dr_ctrl.wIndex = 0;
1615         ucs->dr_ctrl.wLength = 0;
1616         usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1617                              usb_sndctrlpipe(ucs->udev, 0),
1618                              (unsigned char *) &ucs->dr_ctrl, NULL, 0,
1619                              write_ctrl_callback, ucs);
1620         ucs->retry_ctrl = 0;
1621         ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
1622         if (unlikely(ret)) {
1623                 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1624                         req, get_usb_rcmsg(ret));
1625                 spin_unlock_irqrestore(&ucs->lock, flags);
1626                 return ret;
1627         }
1628         ucs->pending = req;
1629 
1630         if (timeout > 0) {
1631                 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1632                 mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
1633         }
1634 
1635         spin_unlock_irqrestore(&ucs->lock, flags);
1636         return 0;
1637 }
1638 
1639 /* gigaset_init_bchannel
1640  * called by common.c to connect a B channel
1641  * initialize isochronous I/O and tell the Gigaset base to open the channel
1642  * argument:
1643  *      B channel control structure
1644  * return value:
1645  *      0 on success, error code < 0 on error
1646  */
1647 static int gigaset_init_bchannel(struct bc_state *bcs)
1648 {
1649         struct cardstate *cs = bcs->cs;
1650         int req, ret;
1651         unsigned long flags;
1652 
1653         spin_lock_irqsave(&cs->lock, flags);
1654         if (unlikely(!cs->connected)) {
1655                 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1656                 spin_unlock_irqrestore(&cs->lock, flags);
1657                 return -ENODEV;
1658         }
1659 
1660         if (cs->hw.bas->basstate & BS_SUSPEND) {
1661                 dev_notice(cs->dev,
1662                            "not starting isoc I/O, suspend in progress\n");
1663                 spin_unlock_irqrestore(&cs->lock, flags);
1664                 return -EHOSTUNREACH;
1665         }
1666 
1667         ret = starturbs(bcs);
1668         if (ret < 0) {
1669                 spin_unlock_irqrestore(&cs->lock, flags);
1670                 dev_err(cs->dev,
1671                         "could not start isoc I/O for channel B%d: %s\n",
1672                         bcs->channel + 1,
1673                         ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1674                 if (ret != -ENODEV)
1675                         error_hangup(bcs);
1676                 return ret;
1677         }
1678 
1679         req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1680         ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1681         if (ret < 0) {
1682                 dev_err(cs->dev, "could not open channel B%d\n",
1683                         bcs->channel + 1);
1684                 stopurbs(bcs->hw.bas);
1685         }
1686 
1687         spin_unlock_irqrestore(&cs->lock, flags);
1688         if (ret < 0 && ret != -ENODEV)
1689                 error_hangup(bcs);
1690         return ret;
1691 }
1692 
1693 /* gigaset_close_bchannel
1694  * called by common.c to disconnect a B channel
1695  * tell the Gigaset base to close the channel
1696  * stopping isochronous I/O and LL notification will be done when the
1697  * acknowledgement for the close arrives
1698  * argument:
1699  *      B channel control structure
1700  * return value:
1701  *      0 on success, error code < 0 on error
1702  */
1703 static int gigaset_close_bchannel(struct bc_state *bcs)
1704 {
1705         struct cardstate *cs = bcs->cs;
1706         int req, ret;
1707         unsigned long flags;
1708 
1709         spin_lock_irqsave(&cs->lock, flags);
1710         if (unlikely(!cs->connected)) {
1711                 spin_unlock_irqrestore(&cs->lock, flags);
1712                 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1713                 return -ENODEV;
1714         }
1715 
1716         if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1717                 /* channel not running: just signal common.c */
1718                 spin_unlock_irqrestore(&cs->lock, flags);
1719                 gigaset_bchannel_down(bcs);
1720                 return 0;
1721         }
1722 
1723         /* channel running: tell device to close it */
1724         req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1725         ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1726         if (ret < 0)
1727                 dev_err(cs->dev, "closing channel B%d failed\n",
1728                         bcs->channel + 1);
1729 
1730         spin_unlock_irqrestore(&cs->lock, flags);
1731         return ret;
1732 }
1733 
1734 /* Device Operations */
1735 /* ================= */
1736 
1737 /* complete_cb
1738  * unqueue first command buffer from queue, waking any sleepers
1739  * must be called with cs->cmdlock held
1740  * parameter:
1741  *      cs      controller state structure
1742  */
1743 static void complete_cb(struct cardstate *cs)
1744 {
1745         struct cmdbuf_t *cb = cs->cmdbuf;
1746 
1747         /* unqueue completed buffer */
1748         cs->cmdbytes -= cs->curlen;
1749         gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
1750                 cs->curlen, cs->cmdbytes);
1751         if (cb->next != NULL) {
1752                 cs->cmdbuf = cb->next;
1753                 cs->cmdbuf->prev = NULL;
1754                 cs->curlen = cs->cmdbuf->len;
1755         } else {
1756                 cs->cmdbuf = NULL;
1757                 cs->lastcmdbuf = NULL;
1758                 cs->curlen = 0;
1759         }
1760 
1761         if (cb->wake_tasklet)
1762                 tasklet_schedule(cb->wake_tasklet);
1763 
1764         kfree(cb);
1765 }
1766 
1767 /* write_command_callback
1768  * USB completion handler for AT command transmission
1769  * called by the USB subsystem in interrupt context
1770  * parameter:
1771  *      urb     USB request block of completed request
1772  *              urb->context = controller state structure
1773  */
1774 static void write_command_callback(struct urb *urb)
1775 {
1776         struct cardstate *cs = urb->context;
1777         struct bas_cardstate *ucs = cs->hw.bas;
1778         int status = urb->status;
1779         unsigned long flags;
1780 
1781         update_basstate(ucs, 0, BS_ATWRPEND);
1782         wake_up(&ucs->waitqueue);
1783 
1784         /* check status */
1785         switch (status) {
1786         case 0:                                 /* normal completion */
1787                 break;
1788         case -ENOENT:                   /* cancelled */
1789         case -ECONNRESET:               /* cancelled (async) */
1790         case -EINPROGRESS:              /* pending */
1791         case -ENODEV:                   /* device removed */
1792         case -ESHUTDOWN:                /* device shut down */
1793                 /* ignore silently */
1794                 gig_dbg(DEBUG_USBREQ, "%s: %s",
1795                         __func__, get_usb_statmsg(status));
1796                 return;
1797         default:                                /* any failure */
1798                 if (++ucs->retry_cmd_out > BAS_RETRY) {
1799                         dev_warn(cs->dev,
1800                                  "command write: %s, "
1801                                  "giving up after %d retries\n",
1802                                  get_usb_statmsg(status),
1803                                  ucs->retry_cmd_out);
1804                         break;
1805                 }
1806                 if (ucs->basstate & BS_SUSPEND) {
1807                         dev_warn(cs->dev,
1808                                  "command write: %s, "
1809                                  "won't retry - suspend requested\n",
1810                                  get_usb_statmsg(status));
1811                         break;
1812                 }
1813                 if (cs->cmdbuf == NULL) {
1814                         dev_warn(cs->dev,
1815                                  "command write: %s, "
1816                                  "cannot retry - cmdbuf gone\n",
1817                                  get_usb_statmsg(status));
1818                         break;
1819                 }
1820                 dev_notice(cs->dev, "command write: %s, retry %d\n",
1821                            get_usb_statmsg(status), ucs->retry_cmd_out);
1822                 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1823                         /* resubmitted - bypass regular exit block */
1824                         return;
1825                 /* command send failed, assume base still waiting */
1826                 update_basstate(ucs, BS_ATREADY, 0);
1827         }
1828 
1829         spin_lock_irqsave(&cs->cmdlock, flags);
1830         if (cs->cmdbuf != NULL)
1831                 complete_cb(cs);
1832         spin_unlock_irqrestore(&cs->cmdlock, flags);
1833 }
1834 
1835 /* atrdy_timeout
1836  * timeout routine for AT command transmission
1837  * argument:
1838  *      controller state structure
1839  */
1840 static void atrdy_timeout(struct timer_list *t)
1841 {
1842         struct bas_cardstate *ucs = from_timer(ucs, t, timer_atrdy);
1843         struct cardstate *cs = ucs->cs;
1844 
1845         dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1846 
1847         /* fake the missing signal - what else can I do? */
1848         update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1849         start_cbsend(cs);
1850 }
1851 
1852 /* atwrite_submit
1853  * submit an HD_WRITE_ATMESSAGE command URB
1854  * parameters:
1855  *      cs      controller state structure
1856  *      buf     buffer containing command to send
1857  *      len     length of command to send
1858  * return value:
1859  *      0 on success
1860  *      -EBUSY if another request is pending
1861  *      any URB submission error code
1862  */
1863 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1864 {
1865         struct bas_cardstate *ucs = cs->hw.bas;
1866         int rc;
1867 
1868         gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1869 
1870         if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1871                 dev_err(cs->dev,
1872                         "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1873                 return -EBUSY;
1874         }
1875 
1876         ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1877         ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1878         ucs->dr_cmd_out.wValue = 0;
1879         ucs->dr_cmd_out.wIndex = 0;
1880         ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1881         usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1882                              usb_sndctrlpipe(ucs->udev, 0),
1883                              (unsigned char *) &ucs->dr_cmd_out, buf, len,
1884                              write_command_callback, cs);
1885         rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
1886         if (unlikely(rc)) {
1887                 update_basstate(ucs, 0, BS_ATWRPEND);
1888                 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1889                         get_usb_rcmsg(rc));
1890                 return rc;
1891         }
1892 
1893         /* submitted successfully, start timeout if necessary */
1894         if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1895                 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1896                         ATRDY_TIMEOUT);
1897                 mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
1898         }
1899         return 0;
1900 }
1901 
1902 /* start_cbsend
1903  * start transmission of AT command queue if necessary
1904  * parameter:
1905  *      cs              controller state structure
1906  * return value:
1907  *      0 on success
1908  *      error code < 0 on error
1909  */
1910 static int start_cbsend(struct cardstate *cs)
1911 {
1912         struct cmdbuf_t *cb;
1913         struct bas_cardstate *ucs = cs->hw.bas;
1914         unsigned long flags;
1915         int rc;
1916         int retval = 0;
1917 
1918         /* check if suspend requested */
1919         if (ucs->basstate & BS_SUSPEND) {
1920                 gig_dbg(DEBUG_OUTPUT, "suspending");
1921                 return -EHOSTUNREACH;
1922         }
1923 
1924         /* check if AT channel is open */
1925         if (!(ucs->basstate & BS_ATOPEN)) {
1926                 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
1927                 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1928                 if (rc < 0) {
1929                         /* flush command queue */
1930                         spin_lock_irqsave(&cs->cmdlock, flags);
1931                         while (cs->cmdbuf != NULL)
1932                                 complete_cb(cs);
1933                         spin_unlock_irqrestore(&cs->cmdlock, flags);
1934                 }
1935                 return rc;
1936         }
1937 
1938         /* try to send first command in queue */
1939         spin_lock_irqsave(&cs->cmdlock, flags);
1940 
1941         while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
1942                 ucs->retry_cmd_out = 0;
1943                 rc = atwrite_submit(cs, cb->buf, cb->len);
1944                 if (unlikely(rc)) {
1945                         retval = rc;
1946                         complete_cb(cs);
1947                 }
1948         }
1949 
1950         spin_unlock_irqrestore(&cs->cmdlock, flags);
1951         return retval;
1952 }
1953 
1954 /* gigaset_write_cmd
1955  * This function is called by the device independent part of the driver
1956  * to transmit an AT command string to the Gigaset device.
1957  * It encapsulates the device specific method for transmission over the
1958  * direct USB connection to the base.
1959  * The command string is added to the queue of commands to send, and
1960  * USB transmission is started if necessary.
1961  * parameters:
1962  *      cs              controller state structure
1963  *      cb              command buffer structure
1964  * return value:
1965  *      number of bytes queued on success
1966  *      error code < 0 on error
1967  */
1968 static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
1969 {
1970         unsigned long flags;
1971         int rc;
1972 
1973         gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
1974                            DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1975                            "CMD Transmit", cb->len, cb->buf);
1976 
1977         /* translate "+++" escape sequence sent as a single separate command
1978          * into "close AT channel" command for error recovery
1979          * The next command will reopen the AT channel automatically.
1980          */
1981         if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
1982                 /* If an HD_RECEIVEATDATA_ACK message remains unhandled
1983                  * because of an error, the base never sends another one.
1984                  * The response channel is thus effectively blocked.
1985                  * Closing and reopening the AT channel does *not* clear
1986                  * this condition.
1987                  * As a stopgap measure, submit a zero-length AT read
1988                  * before closing the AT channel. This has the undocumented
1989                  * effect of triggering a new HD_RECEIVEATDATA_ACK message
1990                  * from the base if necessary.
1991                  * The subsequent AT channel close then discards any pending
1992                  * messages.
1993                  */
1994                 spin_lock_irqsave(&cs->lock, flags);
1995                 if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {
1996                         kfree(cs->hw.bas->rcvbuf);
1997                         cs->hw.bas->rcvbuf = NULL;
1998                         cs->hw.bas->rcvbuf_size = 0;
1999                         cs->hw.bas->retry_cmd_in = 0;
2000                         atread_submit(cs, 0);
2001                 }
2002                 spin_unlock_irqrestore(&cs->lock, flags);
2003 
2004                 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
2005                 if (cb->wake_tasklet)
2006                         tasklet_schedule(cb->wake_tasklet);
2007                 if (!rc)
2008                         rc = cb->len;
2009                 kfree(cb);
2010                 return rc;
2011         }
2012 
2013         spin_lock_irqsave(&cs->cmdlock, flags);
2014         cb->prev = cs->lastcmdbuf;
2015         if (cs->lastcmdbuf)
2016                 cs->lastcmdbuf->next = cb;
2017         else {
2018                 cs->cmdbuf = cb;
2019                 cs->curlen = cb->len;
2020         }
2021         cs->cmdbytes += cb->len;
2022         cs->lastcmdbuf = cb;
2023         spin_unlock_irqrestore(&cs->cmdlock, flags);
2024 
2025         spin_lock_irqsave(&cs->lock, flags);
2026         if (unlikely(!cs->connected)) {
2027                 spin_unlock_irqrestore(&cs->lock, flags);
2028                 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
2029                 /* flush command queue */
2030                 spin_lock_irqsave(&cs->cmdlock, flags);
2031                 while (cs->cmdbuf != NULL)
2032                         complete_cb(cs);
2033                 spin_unlock_irqrestore(&cs->cmdlock, flags);
2034                 return -ENODEV;
2035         }
2036         rc = start_cbsend(cs);
2037         spin_unlock_irqrestore(&cs->lock, flags);
2038         return rc < 0 ? rc : cb->len;
2039 }
2040 
2041 /* gigaset_write_room
2042  * tty_driver.write_room interface routine
2043  * return number of characters the driver will accept to be written via
2044  * gigaset_write_cmd
2045  * parameter:
2046  *      controller state structure
2047  * return value:
2048  *      number of characters
2049  */
2050 static int gigaset_write_room(struct cardstate *cs)
2051 {
2052         return IF_WRITEBUF;
2053 }
2054 
2055 /* gigaset_chars_in_buffer
2056  * tty_driver.chars_in_buffer interface routine
2057  * return number of characters waiting to be sent
2058  * parameter:
2059  *      controller state structure
2060  * return value:
2061  *      number of characters
2062  */
2063 static int gigaset_chars_in_buffer(struct cardstate *cs)
2064 {
2065         return cs->cmdbytes;
2066 }
2067 
2068 /* gigaset_brkchars
2069  * implementation of ioctl(GIGASET_BRKCHARS)
2070  * parameter:
2071  *      controller state structure
2072  * return value:
2073  *      -EINVAL (unimplemented function)
2074  */
2075 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2076 {
2077         return -EINVAL;
2078 }
2079 
2080 
2081 /* Device Initialization/Shutdown */
2082 /* ============================== */
2083 
2084 /* Free hardware dependent part of the B channel structure
2085  * parameter:
2086  *      bcs     B channel structure
2087  */
2088 static void gigaset_freebcshw(struct bc_state *bcs)
2089 {
2090         struct bas_bc_state *ubc = bcs->hw.bas;
2091         int i;
2092 
2093         if (!ubc)
2094                 return;
2095 
2096         /* kill URBs and tasklets before freeing - better safe than sorry */
2097         ubc->running = 0;
2098         gig_dbg(DEBUG_INIT, "%s: killing isoc URBs", __func__);
2099         for (i = 0; i < BAS_OUTURBS; ++i) {
2100                 usb_kill_urb(ubc->isoouturbs[i].urb);
2101                 usb_free_urb(ubc->isoouturbs[i].urb);
2102         }
2103         for (i = 0; i < BAS_INURBS; ++i) {
2104                 usb_kill_urb(ubc->isoinurbs[i]);
2105                 usb_free_urb(ubc->isoinurbs[i]);
2106         }
2107         tasklet_kill(&ubc->sent_tasklet);
2108         tasklet_kill(&ubc->rcvd_tasklet);
2109         kfree(ubc->isooutbuf);
2110         kfree(ubc);
2111         bcs->hw.bas = NULL;
2112 }
2113 
2114 /* Initialize hardware dependent part of the B channel structure
2115  * parameter:
2116  *      bcs     B channel structure
2117  * return value:
2118  *      0 on success, error code < 0 on failure
2119  */
2120 static int gigaset_initbcshw(struct bc_state *bcs)
2121 {
2122         int i;
2123         struct bas_bc_state *ubc;
2124 
2125         bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2126         if (!ubc) {
2127                 pr_err("out of memory\n");
2128                 return -ENOMEM;
2129         }
2130 
2131         ubc->running = 0;
2132         atomic_set(&ubc->corrbytes, 0);
2133         spin_lock_init(&ubc->isooutlock);
2134         for (i = 0; i < BAS_OUTURBS; ++i) {
2135                 ubc->isoouturbs[i].urb = NULL;
2136                 ubc->isoouturbs[i].bcs = bcs;
2137         }
2138         ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2139         ubc->numsub = 0;
2140         ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2141         if (!ubc->isooutbuf) {
2142                 pr_err("out of memory\n");
2143                 kfree(ubc);
2144                 bcs->hw.bas = NULL;
2145                 return -ENOMEM;
2146         }
2147         tasklet_init(&ubc->sent_tasklet,
2148                      write_iso_tasklet, (unsigned long) bcs);
2149 
2150         spin_lock_init(&ubc->isoinlock);
2151         for (i = 0; i < BAS_INURBS; ++i)
2152                 ubc->isoinurbs[i] = NULL;
2153         ubc->isoindone = NULL;
2154         ubc->loststatus = -EINPROGRESS;
2155         ubc->isoinlost = 0;
2156         ubc->seqlen = 0;
2157         ubc->inbyte = 0;
2158         ubc->inbits = 0;
2159         ubc->goodbytes = 0;
2160         ubc->alignerrs = 0;
2161         ubc->fcserrs = 0;
2162         ubc->frameerrs = 0;
2163         ubc->giants = 0;
2164         ubc->runts = 0;
2165         ubc->aborts = 0;
2166         ubc->shared0s = 0;
2167         ubc->stolen0s = 0;
2168         tasklet_init(&ubc->rcvd_tasklet,
2169                      read_iso_tasklet, (unsigned long) bcs);
2170         return 0;
2171 }
2172 
2173 static void gigaset_reinitbcshw(struct bc_state *bcs)
2174 {
2175         struct bas_bc_state *ubc = bcs->hw.bas;
2176 
2177         bcs->hw.bas->running = 0;
2178         atomic_set(&bcs->hw.bas->corrbytes, 0);
2179         bcs->hw.bas->numsub = 0;
2180         spin_lock_init(&ubc->isooutlock);
2181         spin_lock_init(&ubc->isoinlock);
2182         ubc->loststatus = -EINPROGRESS;
2183 }
2184 
2185 static void gigaset_freecshw(struct cardstate *cs)
2186 {
2187         /* timers, URBs and rcvbuf are disposed of in disconnect */
2188         kfree(cs->hw.bas->int_in_buf);
2189         kfree(cs->hw.bas);
2190         cs->hw.bas = NULL;
2191 }
2192 
2193 /* Initialize hardware dependent part of the cardstate structure
2194  * parameter:
2195  *      cs      cardstate structure
2196  * return value:
2197  *      0 on success, error code < 0 on failure
2198  */
2199 static int gigaset_initcshw(struct cardstate *cs)
2200 {
2201         struct bas_cardstate *ucs;
2202 
2203         cs->hw.bas = ucs = kzalloc(sizeof(*ucs), GFP_KERNEL);
2204         if (!ucs) {
2205                 pr_err("out of memory\n");
2206                 return -ENOMEM;
2207         }
2208         ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2209         if (!ucs->int_in_buf) {
2210                 kfree(ucs);
2211                 pr_err("out of memory\n");
2212                 return -ENOMEM;
2213         }
2214 
2215         spin_lock_init(&ucs->lock);
2216         ucs->cs = cs;
2217         timer_setup(&ucs->timer_ctrl, req_timeout, 0);
2218         timer_setup(&ucs->timer_atrdy, atrdy_timeout, 0);
2219         timer_setup(&ucs->timer_cmd_in, cmd_in_timeout, 0);
2220         timer_setup(&ucs->timer_int_in, int_in_resubmit, 0);
2221         init_waitqueue_head(&ucs->waitqueue);
2222         INIT_WORK(&ucs->int_in_wq, int_in_work);
2223 
2224         return 0;
2225 }
2226 
2227 /* freeurbs
2228  * unlink and deallocate all URBs unconditionally
2229  * caller must make sure that no commands are still in progress
2230  * parameter:
2231  *      cs      controller state structure
2232  */
2233 static void freeurbs(struct cardstate *cs)
2234 {
2235         struct bas_cardstate *ucs = cs->hw.bas;
2236         struct bas_bc_state *ubc;
2237         int i, j;
2238 
2239         gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
2240         for (j = 0; j < BAS_CHANNELS; ++j) {
2241                 ubc = cs->bcs[j].hw.bas;
2242                 for (i = 0; i < BAS_OUTURBS; ++i) {
2243                         usb_kill_urb(ubc->isoouturbs[i].urb);
2244                         usb_free_urb(ubc->isoouturbs[i].urb);
2245                         ubc->isoouturbs[i].urb = NULL;
2246                 }
2247                 for (i = 0; i < BAS_INURBS; ++i) {
2248                         usb_kill_urb(ubc->isoinurbs[i]);
2249                         usb_free_urb(ubc->isoinurbs[i]);
2250                         ubc->isoinurbs[i] = NULL;
2251                 }
2252         }
2253         usb_kill_urb(ucs->urb_int_in);
2254         usb_free_urb(ucs->urb_int_in);
2255         ucs->urb_int_in = NULL;
2256         usb_kill_urb(ucs->urb_cmd_out);
2257         usb_free_urb(ucs->urb_cmd_out);
2258         ucs->urb_cmd_out = NULL;
2259         usb_kill_urb(ucs->urb_cmd_in);
2260         usb_free_urb(ucs->urb_cmd_in);
2261         ucs->urb_cmd_in = NULL;
2262         usb_kill_urb(ucs->urb_ctrl);
2263         usb_free_urb(ucs->urb_ctrl);
2264         ucs->urb_ctrl = NULL;
2265 }
2266 
2267 /* gigaset_probe
2268  * This function is called when a new USB device is connected.
2269  * It checks whether the new device is handled by this driver.
2270  */
2271 static int gigaset_probe(struct usb_interface *interface,
2272                          const struct usb_device_id *id)
2273 {
2274         struct usb_host_interface *hostif;
2275         struct usb_device *udev = interface_to_usbdev(interface);
2276         struct cardstate *cs = NULL;
2277         struct bas_cardstate *ucs = NULL;
2278         struct bas_bc_state *ubc;
2279         struct usb_endpoint_descriptor *endpoint;
2280         int i, j;
2281         int rc;
2282 
2283         gig_dbg(DEBUG_INIT,
2284                 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2285                 __func__, le16_to_cpu(udev->descriptor.idVendor),
2286                 le16_to_cpu(udev->descriptor.idProduct));
2287 
2288         /* set required alternate setting */
2289         hostif = interface->cur_altsetting;
2290         if (hostif->desc.bAlternateSetting != 3) {
2291                 gig_dbg(DEBUG_INIT,
2292                         "%s: wrong alternate setting %d - trying to switch",
2293                         __func__, hostif->desc.bAlternateSetting);
2294                 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2295                     < 0) {
2296                         dev_warn(&udev->dev, "usb_set_interface failed, "
2297                                  "device %d interface %d altsetting %d\n",
2298                                  udev->devnum, hostif->desc.bInterfaceNumber,
2299                                  hostif->desc.bAlternateSetting);
2300                         return -ENODEV;
2301                 }
2302                 hostif = interface->cur_altsetting;
2303         }
2304 
2305         /* Reject application specific interfaces
2306          */
2307         if (hostif->desc.bInterfaceClass != 255) {
2308                 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2309                          __func__, hostif->desc.bInterfaceClass);
2310                 return -ENODEV;
2311         }
2312 
2313         if (hostif->desc.bNumEndpoints < 1)
2314                 return -ENODEV;
2315 
2316         dev_info(&udev->dev,
2317                  "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2318                  __func__, le16_to_cpu(udev->descriptor.idVendor),
2319                  le16_to_cpu(udev->descriptor.idProduct));
2320 
2321         /* allocate memory for our device state and initialize it */
2322         cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2323                             GIGASET_MODULENAME);
2324         if (!cs)
2325                 return -ENODEV;
2326         ucs = cs->hw.bas;
2327 
2328         /* save off device structure ptrs for later use */
2329         usb_get_dev(udev);
2330         ucs->udev = udev;
2331         ucs->interface = interface;
2332         cs->dev = &interface->dev;
2333 
2334         /* allocate URBs:
2335          * - one for the interrupt pipe
2336          * - three for the different uses of the default control pipe
2337          * - three for each isochronous pipe
2338          */
2339         if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2340             !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2341             !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2342             !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
2343                 goto allocerr;
2344 
2345         for (j = 0; j < BAS_CHANNELS; ++j) {
2346                 ubc = cs->bcs[j].hw.bas;
2347                 for (i = 0; i < BAS_OUTURBS; ++i)
2348                         if (!(ubc->isoouturbs[i].urb =
2349                               usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2350                                 goto allocerr;
2351                 for (i = 0; i < BAS_INURBS; ++i)
2352                         if (!(ubc->isoinurbs[i] =
2353                               usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2354                                 goto allocerr;
2355         }
2356 
2357         ucs->rcvbuf = NULL;
2358         ucs->rcvbuf_size = 0;
2359 
2360         /* Fill the interrupt urb and send it to the core */
2361         endpoint = &hostif->endpoint[0].desc;
2362         usb_fill_int_urb(ucs->urb_int_in, udev,
2363                          usb_rcvintpipe(udev,
2364                                         usb_endpoint_num(endpoint)),
2365                          ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
2366                          endpoint->bInterval);
2367         rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2368         if (rc != 0) {
2369                 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2370                         get_usb_rcmsg(rc));
2371                 goto error;
2372         }
2373         ucs->retry_int_in = 0;
2374 
2375         /* tell the device that the driver is ready */
2376         rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2377         if (rc != 0)
2378                 goto error;
2379 
2380         /* tell common part that the device is ready */
2381         if (startmode == SM_LOCKED)
2382                 cs->mstate = MS_LOCKED;
2383 
2384         /* save address of controller structure */
2385         usb_set_intfdata(interface, cs);
2386 
2387         rc = gigaset_start(cs);
2388         if (rc < 0)
2389                 goto error;
2390 
2391         return 0;
2392 
2393 allocerr:
2394         dev_err(cs->dev, "could not allocate URBs\n");
2395         rc = -ENOMEM;
2396 error:
2397         freeurbs(cs);
2398         usb_set_intfdata(interface, NULL);
2399         usb_put_dev(udev);
2400         gigaset_freecs(cs);
2401         return rc;
2402 }
2403 
2404 /* gigaset_disconnect
2405  * This function is called when the Gigaset base is unplugged.
2406  */
2407 static void gigaset_disconnect(struct usb_interface *interface)
2408 {
2409         struct cardstate *cs;
2410         struct bas_cardstate *ucs;
2411         int j;
2412 
2413         cs = usb_get_intfdata(interface);
2414 
2415         ucs = cs->hw.bas;
2416 
2417         dev_info(cs->dev, "disconnecting Gigaset base\n");
2418 
2419         /* mark base as not ready, all channels disconnected */
2420         ucs->basstate = 0;
2421 
2422         /* tell LL all channels are down */
2423         for (j = 0; j < BAS_CHANNELS; ++j)
2424                 gigaset_bchannel_down(cs->bcs + j);
2425 
2426         /* stop driver (common part) */
2427         gigaset_stop(cs);
2428 
2429         /* stop delayed work and URBs, free ressources */
2430         del_timer_sync(&ucs->timer_ctrl);
2431         del_timer_sync(&ucs->timer_atrdy);
2432         del_timer_sync(&ucs->timer_cmd_in);
2433         del_timer_sync(&ucs->timer_int_in);
2434         cancel_work_sync(&ucs->int_in_wq);
2435         freeurbs(cs);
2436         usb_set_intfdata(interface, NULL);
2437         kfree(ucs->rcvbuf);
2438         ucs->rcvbuf = NULL;
2439         ucs->rcvbuf_size = 0;
2440         usb_put_dev(ucs->udev);
2441         ucs->interface = NULL;
2442         ucs->udev = NULL;
2443         cs->dev = NULL;
2444         gigaset_freecs(cs);
2445 }
2446 
2447 /* gigaset_suspend
2448  * This function is called before the USB connection is suspended
2449  * or before the USB device is reset.
2450  * In the latter case, message == PMSG_ON.
2451  */
2452 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2453 {
2454         struct cardstate *cs = usb_get_intfdata(intf);
2455         struct bas_cardstate *ucs = cs->hw.bas;
2456         int rc;
2457 
2458         /* set suspend flag; this stops AT command/response traffic */
2459         if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2460                 gig_dbg(DEBUG_SUSPEND, "already suspended");
2461                 return 0;
2462         }
2463 
2464         /* wait a bit for blocking conditions to go away */
2465         rc = wait_event_timeout(ucs->waitqueue,
2466                                 !(ucs->basstate &
2467                                   (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)),
2468                                 BAS_TIMEOUT * HZ / 10);
2469         gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2470 
2471         /* check for conditions preventing suspend */
2472         if (ucs->basstate & (BS_B1OPEN | BS_B2OPEN | BS_ATRDPEND | BS_ATWRPEND)) {
2473                 dev_warn(cs->dev, "cannot suspend:\n");
2474                 if (ucs->basstate & BS_B1OPEN)
2475                         dev_warn(cs->dev, " B channel 1 open\n");
2476                 if (ucs->basstate & BS_B2OPEN)
2477                         dev_warn(cs->dev, " B channel 2 open\n");
2478                 if (ucs->basstate & BS_ATRDPEND)
2479                         dev_warn(cs->dev, " receiving AT reply\n");
2480                 if (ucs->basstate & BS_ATWRPEND)
2481                         dev_warn(cs->dev, " sending AT command\n");
2482                 update_basstate(ucs, 0, BS_SUSPEND);
2483                 return -EBUSY;
2484         }
2485 
2486         /* close AT channel if open */
2487         if (ucs->basstate & BS_ATOPEN) {
2488                 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2489                 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2490                 if (rc) {
2491                         update_basstate(ucs, 0, BS_SUSPEND);
2492                         return rc;
2493                 }
2494                 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2495                                    BAS_TIMEOUT * HZ / 10);
2496                 /* in case of timeout, proceed anyway */
2497         }
2498 
2499         /* kill all URBs and delayed work that might still be pending */
2500         usb_kill_urb(ucs->urb_ctrl);
2501         usb_kill_urb(ucs->urb_int_in);
2502         del_timer_sync(&ucs->timer_ctrl);
2503         del_timer_sync(&ucs->timer_atrdy);
2504         del_timer_sync(&ucs->timer_cmd_in);
2505         del_timer_sync(&ucs->timer_int_in);
2506 
2507         /* don't try to cancel int_in_wq from within reset as it
2508          * might be the one requesting the reset
2509          */
2510         if (message.event != PM_EVENT_ON)
2511                 cancel_work_sync(&ucs->int_in_wq);
2512 
2513         gig_dbg(DEBUG_SUSPEND, "suspend complete");
2514         return 0;
2515 }
2516 
2517 /* gigaset_resume
2518  * This function is called after the USB connection has been resumed.
2519  */
2520 static int gigaset_resume(struct usb_interface *intf)
2521 {
2522         struct cardstate *cs = usb_get_intfdata(intf);
2523         struct bas_cardstate *ucs = cs->hw.bas;
2524         int rc;
2525 
2526         /* resubmit interrupt URB for spontaneous messages from base */
2527         rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2528         if (rc) {
2529                 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2530                         get_usb_rcmsg(rc));
2531                 return rc;
2532         }
2533         ucs->retry_int_in = 0;
2534 
2535         /* clear suspend flag to reallow activity */
2536         update_basstate(ucs, 0, BS_SUSPEND);
2537 
2538         gig_dbg(DEBUG_SUSPEND, "resume complete");
2539         return 0;
2540 }
2541 
2542 /* gigaset_pre_reset
2543  * This function is called before the USB connection is reset.
2544  */
2545 static int gigaset_pre_reset(struct usb_interface *intf)
2546 {
2547         /* handle just like suspend */
2548         return gigaset_suspend(intf, PMSG_ON);
2549 }
2550 
2551 /* gigaset_post_reset
2552  * This function is called after the USB connection has been reset.
2553  */
2554 static int gigaset_post_reset(struct usb_interface *intf)
2555 {
2556         /* FIXME: send HD_DEVICE_INIT_ACK? */
2557 
2558         /* resume operations */
2559         return gigaset_resume(intf);
2560 }
2561 
2562 
2563 static const struct gigaset_ops gigops = {
2564         .write_cmd = gigaset_write_cmd,
2565         .write_room = gigaset_write_room,
2566         .chars_in_buffer = gigaset_chars_in_buffer,
2567         .brkchars = gigaset_brkchars,
2568         .init_bchannel = gigaset_init_bchannel,
2569         .close_bchannel = gigaset_close_bchannel,
2570         .initbcshw = gigaset_initbcshw,
2571         .freebcshw = gigaset_freebcshw,
2572         .reinitbcshw = gigaset_reinitbcshw,
2573         .initcshw = gigaset_initcshw,
2574         .freecshw = gigaset_freecshw,
2575         .set_modem_ctrl = gigaset_set_modem_ctrl,
2576         .baud_rate = gigaset_baud_rate,
2577         .set_line_ctrl = gigaset_set_line_ctrl,
2578         .send_skb = gigaset_isoc_send_skb,
2579         .handle_input = gigaset_isoc_input,
2580 };
2581 
2582 /* bas_gigaset_init
2583  * This function is called after the kernel module is loaded.
2584  */
2585 static int __init bas_gigaset_init(void)
2586 {
2587         int result;
2588 
2589         /* allocate memory for our driver state and initialize it */
2590         driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2591                                     GIGASET_MODULENAME, GIGASET_DEVNAME,
2592                                     &gigops, THIS_MODULE);
2593         if (driver == NULL)
2594                 goto error;
2595 
2596         /* register this driver with the USB subsystem */
2597         result = usb_register(&gigaset_usb_driver);
2598         if (result < 0) {
2599                 pr_err("error %d registering USB driver\n", -result);
2600                 goto error;
2601         }
2602 
2603         pr_info(DRIVER_DESC "\n");
2604         return 0;
2605 
2606 error:
2607         if (driver)
2608                 gigaset_freedriver(driver);
2609         driver = NULL;
2610         return -1;
2611 }
2612 
2613 /* bas_gigaset_exit
2614  * This function is called before the kernel module is unloaded.
2615  */
2616 static void __exit bas_gigaset_exit(void)
2617 {
2618         struct bas_cardstate *ucs;
2619         int i;
2620 
2621         gigaset_blockdriver(driver); /* => probe will fail
2622                                       * => no gigaset_start any more
2623                                       */
2624 
2625         /* stop all connected devices */
2626         for (i = 0; i < driver->minors; i++) {
2627                 if (gigaset_shutdown(driver->cs + i) < 0)
2628                         continue;               /* no device */
2629                 /* from now on, no isdn callback should be possible */
2630 
2631                 /* close all still open channels */
2632                 ucs = driver->cs[i].hw.bas;
2633                 if (ucs->basstate & BS_B1OPEN) {
2634                         gig_dbg(DEBUG_INIT, "closing B1 channel");
2635                         usb_control_msg(ucs->udev,
2636                                         usb_sndctrlpipe(ucs->udev, 0),
2637                                         HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2638                                         0, 0, NULL, 0, BAS_TIMEOUT);
2639                 }
2640                 if (ucs->basstate & BS_B2OPEN) {
2641                         gig_dbg(DEBUG_INIT, "closing B2 channel");
2642                         usb_control_msg(ucs->udev,
2643                                         usb_sndctrlpipe(ucs->udev, 0),
2644                                         HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2645                                         0, 0, NULL, 0, BAS_TIMEOUT);
2646                 }
2647                 if (ucs->basstate & BS_ATOPEN) {
2648                         gig_dbg(DEBUG_INIT, "closing AT channel");
2649                         usb_control_msg(ucs->udev,
2650                                         usb_sndctrlpipe(ucs->udev, 0),
2651                                         HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2652                                         0, 0, NULL, 0, BAS_TIMEOUT);
2653                 }
2654                 ucs->basstate = 0;
2655         }
2656 
2657         /* deregister this driver with the USB subsystem */
2658         usb_deregister(&gigaset_usb_driver);
2659         /* this will call the disconnect-callback */
2660         /* from now on, no disconnect/probe callback should be running */
2661 
2662         gigaset_freedriver(driver);
2663         driver = NULL;
2664 }
2665 
2666 
2667 module_init(bas_gigaset_init);
2668 module_exit(bas_gigaset_exit);
2669 
2670 MODULE_AUTHOR(DRIVER_AUTHOR);
2671 MODULE_DESCRIPTION(DRIVER_DESC);
2672 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */