linux-old/drivers/usb/auermain.c
<<
>>
Prefs
   1/*****************************************************************************/
   2/*
   3 *      auermain.c  --  Auerswald PBX/System Telephone usb driver.
   4 *
   5 *      Copyright (C) 2002-2004  Wolfgang Mües (wolfgang@iksw-muees.de)
   6 *
   7 *      Very much code of this driver is borrowed from dabusb.c (Deti Fliegl)
   8 *      and from the USB Skeleton driver (Greg Kroah-Hartman). Thank you.
   9 *
  10 *      This program is free software; you can redistribute it and/or modify
  11 *      it under the terms of the GNU General Public License as published by
  12 *      the Free Software Foundation; either version 2 of the License, or
  13 *      (at your option) any later version.
  14 *
  15 *      This program is distributed in the hope that it will be useful,
  16 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18 *      GNU General Public License for more details.
  19 *
  20 *      You should have received a copy of the GNU General Public License
  21 *      along with this program; if not, write to the Free Software
  22 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23 */
  24 /*****************************************************************************/
  25
  26/* Standard Linux module include files */
  27#include <linux/slab.h>
  28#include <linux/module.h>
  29#include <linux/init.h>
  30#include <linux/devfs_fs_kernel.h>
  31#undef DEBUG                    /* include debug macros until it's done */
  32#include <linux/usb.h>
  33#include "auerchain.h"
  34#include "auerbuf.h"
  35#include "auerchar.h"
  36#include "auerserv.h"
  37#include "auermain.h"
  38#include "auerisdn.h"
  39
  40/*-------------------------------------------------------------------*/
  41/* Debug support                                                     */
  42#ifdef DEBUG
  43#define dump( desc, adr, len) \
  44do {                    \
  45        unsigned int u; \
  46        printk (KERN_DEBUG); \
  47        printk (desc); \
  48        for (u = 0; u < len; u++) \
  49                printk (" %02X", adr[u] & 0xFF); \
  50        printk ("\n"); \
  51} while (0)
  52#else
  53#define dump( desc, adr, len)
  54#endif
  55
  56/*-------------------------------------------------------------------*/
  57/* Version Information */
  58#define DRIVER_VERSION "1.2.7"
  59#define DRIVER_AUTHOR  "Wolfgang Mües <wolfgang@iksw-muees.de>"
  60#define DRIVER_DESC    "Auerswald PBX/System Telephone usb driver"
  61
  62/*-------------------------------------------------------------------*/
  63/* Internal data structures                                          */
  64
  65/* the global usb devfs handle */
  66extern devfs_handle_t usb_devfs_handle;
  67
  68/* array of pointers to our devices that are currently connected */
  69struct auerswald *auerdev_table[AUER_MAX_DEVICES];
  70
  71/* lock to protect the auerdev_table structure */
  72struct semaphore auerdev_table_mutex;
  73
  74/*-------------------------------------------------------------------*/
  75/* Forwards */
  76static void auerswald_ctrlread_complete(struct urb *urb);
  77
  78/*-------------------------------------------------------------------*/
  79/* Completion handlers */
  80
  81/* Values of urb->status or results of usb_submit_urb():
  820               Initial, OK
  83-EINPROGRESS    during submission until end
  84-ENOENT         if urb is unlinked
  85-ETIMEDOUT      Transfer timed out, NAK
  86-ENOMEM         Memory Overflow
  87-ENODEV         Specified USB-device or bus doesn't exist
  88-ENXIO          URB already queued
  89-EINVAL         a) Invalid transfer type specified (or not supported)
  90                b) Invalid interrupt interval (0n256)
  91-EAGAIN         a) Specified ISO start frame too early
  92                b) (using ISO-ASAP) Too much scheduled for the future wait some time and try again.
  93-EFBIG          Too much ISO frames requested (currently uhci900)
  94-EPIPE          Specified pipe-handle/Endpoint is already stalled
  95-EMSGSIZE       Endpoint message size is zero, do interface/alternate setting
  96-EPROTO         a) Bitstuff error
  97                b) Unknown USB error
  98-EILSEQ         CRC mismatch
  99-ENOSR          Buffer error
 100-EREMOTEIO      Short packet detected
 101-EXDEV          ISO transfer only partially completed look at individual frame status for details
 102-EINVAL         ISO madness, if this happens: Log off and go home
 103-EOVERFLOW      babble
 104*/
 105
 106/* check if a status code allows a retry */
 107static int auerswald_status_retry(int status)
 108{
 109        switch (status) {
 110        case 0:
 111        case -ETIMEDOUT:
 112        case -EOVERFLOW:
 113        case -EFBIG:
 114        case -EAGAIN:
 115        case -EPROTO:
 116        case -EILSEQ:
 117        case -ENOSR:
 118        case -EREMOTEIO:
 119                return 1;       /* do a retry */
 120        }
 121        return 0;               /* no retry possible */
 122}
 123
 124
 125/* Completion of asynchronous write block */
 126void auerchar_ctrlwrite_complete(struct urb *urb)
 127{
 128        struct auerbuf *bp = (struct auerbuf *) urb->context;
 129        struct auerswald *cp =
 130            ((struct auerswald *) ((char *) (bp->list) -
 131                                   (unsigned
 132                                    long) (&((struct auerswald *) 0)->
 133                                           bufctl)));
 134        dbg("auerchar_ctrlwrite_complete called");
 135
 136        /* reuse the buffer */
 137        auerbuf_releasebuf(bp);
 138        /* Wake up all processes waiting for a buffer */
 139        wake_up(&cp->bufferwait);
 140}
 141
 142/* Completion handler for dummy retry packet */
 143static void auerswald_ctrlread_wretcomplete(struct urb *urb)
 144{
 145        struct auerbuf *bp = (struct auerbuf *) urb->context;
 146        struct auerswald *cp;
 147        int ret;
 148        dbg("auerswald_ctrlread_wretcomplete called");
 149        dbg("complete with status: %d", urb->status);
 150        cp = ((struct auerswald *) ((char *) (bp->list) -
 151                                    (unsigned
 152                                     long) (&((struct auerswald *) 0)->
 153                                            bufctl)));
 154
 155        /* check if it is possible to advance */
 156        if (!auerswald_status_retry(urb->status) || !cp->usbdev) {
 157                /* reuse the buffer */
 158                err("control dummy: transmission error %d, can not retry",
 159                    urb->status);
 160                auerbuf_releasebuf(bp);
 161                /* Wake up all processes waiting for a buffer */
 162                wake_up(&cp->bufferwait);
 163                return;
 164        }
 165
 166        /* fill the control message */
 167        bp->dr->bRequestType = AUT_RREQ;
 168        bp->dr->bRequest = AUV_RBLOCK;
 169        bp->dr->wLength = bp->dr->wValue;       /* temporary stored */
 170        bp->dr->wValue = cpu_to_le16(1);        /* Retry Flag */
 171        /* bp->dr->wIndex    = channel id;          remains */
 172        FILL_CONTROL_URB(bp->urbp, cp->usbdev,
 173                         usb_rcvctrlpipe(cp->usbdev, 0),
 174                         (unsigned char *) bp->dr, bp->bufp,
 175                         le16_to_cpu(bp->dr->wLength),
 176                         (usb_complete_t) auerswald_ctrlread_complete, bp);
 177
 178        /* submit the control msg as next paket */
 179        ret = auerchain_submit_urb_list(&cp->controlchain, bp->urbp, 1);
 180        if (ret) {
 181                dbg("auerswald_ctrlread_complete: nonzero result of auerchain_submit_urb_list %d", ret);
 182                bp->urbp->status = ret;
 183                auerswald_ctrlread_complete(bp->urbp);
 184        }
 185}
 186
 187/* completion handler for receiving of control messages */
 188static void auerswald_ctrlread_complete(struct urb *urb)
 189{
 190        unsigned int serviceid;
 191        struct auerswald *cp;
 192        struct auerscon *scp;
 193        struct auerbuf *bp = (struct auerbuf *) urb->context;
 194        int ret;
 195        dbg("auerswald_ctrlread_complete called");
 196
 197        cp = ((struct auerswald *) ((char *) (bp->list) -
 198                                    (unsigned
 199                                     long) (&((struct auerswald *) 0)->
 200                                            bufctl)));
 201
 202        /* check if there is valid data in this urb */
 203        if (urb->status) {
 204                dbg("complete with non-zero status: %d", urb->status);
 205                /* should we do a retry? */
 206                if (!auerswald_status_retry(urb->status)
 207                    || !cp->usbdev || (cp->version < AUV_RETRY)
 208                    || (bp->retries >= AU_RETRIES)) {
 209                        /* reuse the buffer */
 210                        err("control read: transmission error %d, can not retry", urb->status);
 211                        auerbuf_releasebuf(bp);
 212                        /* Wake up all processes waiting for a buffer */
 213                        wake_up(&cp->bufferwait);
 214                        return;
 215                }
 216                bp->retries++;
 217                dbg("Retry count = %d", bp->retries);
 218                /* send a long dummy control-write-message to allow device firmware to react */
 219                bp->dr->bRequestType = AUT_WREQ;
 220                bp->dr->bRequest = AUV_DUMMY;
 221                bp->dr->wValue = bp->dr->wLength;       /* temporary storage */
 222                // bp->dr->wIndex    channel ID remains
 223                bp->dr->wLength = cpu_to_le16(32);      /* >= 8 bytes */
 224                FILL_CONTROL_URB(bp->urbp, cp->usbdev,
 225                                 usb_sndctrlpipe(cp->usbdev, 0),
 226                                 (unsigned char *) bp->dr, bp->bufp, 32,
 227                                 (usb_complete_t)
 228                                 auerswald_ctrlread_wretcomplete, bp);
 229
 230                /* submit the control msg as next paket */
 231                ret =
 232                    auerchain_submit_urb_list(&cp->controlchain, bp->urbp,
 233                                              1);
 234                if (ret) {
 235                        dbg("auerswald_ctrlread_complete: nonzero result of auerchain_submit_urb_list %d", ret);
 236                        bp->urbp->status = ret;
 237                        auerswald_ctrlread_wretcomplete(bp->urbp);
 238                }
 239                return;
 240        }
 241
 242        /* get the actual bytecount (incl. headerbyte) */
 243        bp->len = urb->actual_length;
 244        serviceid = bp->bufp[0] & AUH_TYPEMASK;
 245        dbg("Paket with serviceid %d and %d bytes received", serviceid,
 246            bp->len);
 247
 248        /* dispatch the paket */
 249        scp = cp->services[serviceid];
 250        if (scp) {
 251                /* look, Ma, a listener! */
 252                scp->dispatch(scp, bp);
 253        }
 254
 255        /* release the paket */
 256        auerbuf_releasebuf(bp);
 257        /* Wake up all processes waiting for a buffer */
 258        wake_up(&cp->bufferwait);
 259}
 260
 261/*-------------------------------------------------------------------*/
 262/* Handling of Interrupt Endpoint                                    */
 263/* This interrupt Endpoint is used to inform the host about waiting
 264   messages from the USB device.
 265*/
 266/* int completion handler. */
 267static void auerswald_int_complete(struct urb *urb)
 268{
 269        unsigned int channelid;
 270        unsigned int bytecount;
 271        int ret;
 272        struct auerbuf *bp = NULL;
 273        struct auerswald *cp = (struct auerswald *) urb->context;
 274
 275        dbg("auerswald_int_complete called");
 276
 277        /* do not respond to an error condition */
 278        if (urb->status != 0) {
 279                dbg("nonzero URB status = %d", urb->status);
 280                return;
 281        }
 282
 283        /* check if all needed data was received */
 284        if (urb->actual_length < AU_IRQMINSIZE) {
 285                dbg("invalid data length received: %d bytes",
 286                    urb->actual_length);
 287                return;
 288        }
 289
 290        /* check the command code */
 291        if (cp->intbufp[0] != AU_IRQCMDID) {
 292                dbg("invalid command received: %d", cp->intbufp[0]);
 293                return;
 294        }
 295
 296        /* check the command type */
 297        if (cp->intbufp[1] != AU_BLOCKRDY) {
 298                dbg("invalid command type received: %d", cp->intbufp[1]);
 299                return;
 300        }
 301
 302        /* now extract the information */
 303        channelid = cp->intbufp[2];
 304        bytecount = le16_to_cpup(&cp->intbufp[3]);
 305
 306        /* check the channel id */
 307        if (channelid >= AUH_TYPESIZE) {
 308                dbg("invalid channel id received: %d", channelid);
 309                return;
 310        }
 311
 312        /* check the byte count */
 313        if (bytecount > (cp->maxControlLength + AUH_SIZE)) {
 314                dbg("invalid byte count received: %d", bytecount);
 315                return;
 316        }
 317        dbg("Service Channel = %d", channelid);
 318        dbg("Byte Count = %d", bytecount);
 319
 320        /* get a buffer for the next data paket */
 321        bp = auerbuf_getbuf(&cp->bufctl);
 322        /* if no buffer available: skip it */
 323        if (!bp) {
 324                dbg("auerswald_int_complete: no data buffer available");
 325                /* can we do something more?
 326                   This is a big problem: if this int packet is ignored, the
 327                   device will wait forever and not signal any more data.
 328                   The only real solution is: having enought buffers!
 329                   Or perhaps temporary disabling the int endpoint?
 330                 */
 331                return;
 332        }
 333
 334        /* fill the control message */
 335        bp->dr->bRequestType = AUT_RREQ;
 336        bp->dr->bRequest = AUV_RBLOCK;
 337        bp->dr->wValue = cpu_to_le16(0);
 338        bp->dr->wIndex = cpu_to_le16(channelid | AUH_DIRECT | AUH_UNSPLIT);
 339        bp->dr->wLength = cpu_to_le16(bytecount);
 340        FILL_CONTROL_URB(bp->urbp, cp->usbdev,
 341                         usb_rcvctrlpipe(cp->usbdev, 0),
 342                         (unsigned char *) bp->dr, bp->bufp, bytecount,
 343                         (usb_complete_t) auerswald_ctrlread_complete, bp);
 344
 345        /* submit the control msg */
 346        ret = auerchain_submit_urb(&cp->controlchain, bp->urbp);
 347        if (ret) {
 348                dbg("auerswald_int_complete: nonzero result of auerchain_submit_urb %d", ret);
 349                bp->urbp->status = ret;
 350                auerswald_ctrlread_complete(bp->urbp);
 351                /* here applies the same problem as above: device locking! */
 352        }
 353}
 354
 355/* int memory deallocation
 356   NOTE: no mutex please!
 357*/
 358static void auerswald_int_free(struct auerswald *cp)
 359{
 360        if (cp->inturbp) {
 361                usb_free_urb(cp->inturbp);
 362                cp->inturbp = NULL;
 363        }
 364        kfree(cp->intbufp);
 365}
 366
 367/* This function is called to activate the interrupt
 368   endpoint. This function returns 0 if successfull or an error code.
 369   NOTE: no mutex please!
 370*/
 371static int auerswald_int_open(struct auerswald *cp)
 372{
 373        int ret;
 374        struct usb_endpoint_descriptor *ep;
 375        int irqsize;
 376        dbg("auerswald_int_open");
 377
 378        ep = usb_epnum_to_ep_desc(cp->usbdev, USB_DIR_IN | AU_IRQENDP);
 379        if (!ep) {
 380                ret = -EFAULT;
 381                goto intoend;
 382        }
 383        irqsize = ep->wMaxPacketSize;
 384        cp->irqsize = irqsize;
 385
 386        /* allocate the urb and data buffer */
 387        if (!cp->inturbp) {
 388                cp->inturbp = usb_alloc_urb(0);
 389                if (!cp->inturbp) {
 390                        ret = -ENOMEM;
 391                        goto intoend;
 392                }
 393        }
 394        if (!cp->intbufp) {
 395                cp->intbufp = (char *) kmalloc(irqsize, GFP_KERNEL);
 396                if (!cp->intbufp) {
 397                        ret = -ENOMEM;
 398                        goto intoend;
 399                }
 400        }
 401        /* setup urb */
 402        FILL_INT_URB(cp->inturbp, cp->usbdev,
 403                     usb_rcvintpipe(cp->usbdev, AU_IRQENDP), cp->intbufp,
 404                     irqsize, auerswald_int_complete, cp, ep->bInterval);
 405        /* start the urb */
 406        cp->inturbp->status = 0;        /* needed! */
 407        ret = usb_submit_urb(cp->inturbp);
 408
 409      intoend:
 410        if (ret < 0) {
 411                /* activation of interrupt endpoint has failed. Now clean up. */
 412                dbg("auerswald_int_open: activation of int endpoint failed");
 413
 414                /* deallocate memory */
 415                auerswald_int_free(cp);
 416        }
 417        return ret;
 418}
 419
 420/* This function is called to deactivate the interrupt
 421   endpoint. This function returns 0 if successfull or an error code.
 422   NOTE: no mutex please!
 423*/
 424static int auerswald_int_release(struct auerswald *cp)
 425{
 426        int ret = 0;
 427        dbg("auerswald_int_release");
 428
 429        /* stop the int endpoint */
 430        if (cp->inturbp) {
 431                ret = usb_unlink_urb(cp->inturbp);
 432                if (ret)
 433                        dbg("nonzero int unlink result received: %d", ret);
 434        }
 435
 436        /* deallocate memory */
 437        auerswald_int_free(cp);
 438
 439        return ret;
 440}
 441
 442/* --------------------------------------------------------------------- */
 443/* Helper functions                                                      */
 444
 445/* Delete an auerswald driver context */
 446void auerswald_delete(struct auerswald *cp)
 447{
 448        dbg("auerswald_delete");
 449        if (cp == NULL)
 450                return;
 451
 452        /* Wake up all processes waiting for a buffer */
 453        wake_up(&cp->bufferwait);
 454
 455        /* Cleaning up */
 456        auerisdn_disconnect(cp);
 457        auerswald_int_release(cp);
 458        auerchain_free(&cp->controlchain);
 459        auerbuf_free_buffers(&cp->bufctl);
 460
 461        /* release the memory */
 462        kfree(cp);
 463}
 464
 465
 466/* add a new service to the device
 467   scp->id must be set!
 468   return: 0 if OK, else error code
 469*/
 470int auerswald_addservice(struct auerswald *cp, struct auerscon *scp)
 471{
 472        int ret;
 473
 474        /* is the device available? */
 475        if (!cp->usbdev) {
 476                dbg("usbdev == NULL");
 477                return -EIO;    /*no: can not add a service, sorry */
 478        }
 479
 480        /* is the service available? */
 481        if (cp->services[scp->id]) {
 482                dbg("service is busy");
 483                return -EBUSY;
 484        }
 485
 486        /* device is available, service is free */
 487        cp->services[scp->id] = scp;
 488
 489        /* register service in device */
 490        ret = auerchain_control_msg(&cp->controlchain,                  /* pointer to control chain */
 491                                    cp->usbdev,                         /* pointer to device */
 492                                    usb_sndctrlpipe(cp->usbdev, 0),     /* pipe to control endpoint */
 493                                    AUV_CHANNELCTL,                     /* USB message request value */
 494                                    AUT_WREQ,                           /* USB message request type value */
 495                                    0x01,       /* open */              /* USB message value */
 496                                    scp->id,                            /* USB message index value */
 497                                    NULL,                               /* pointer to the data to send */
 498                                    0,                                  /* length in bytes of the data to send */
 499                                    HZ * 2);                            /* time to wait for the message to complete before timing out */
 500        if (ret < 0) {
 501                dbg("auerswald_addservice: auerchain_control_msg returned error code %d", ret);
 502                /* undo above actions */
 503                cp->services[scp->id] = NULL;
 504                return ret;
 505        }
 506
 507        dbg("auerswald_addservice: channel open OK");
 508        return 0;
 509}
 510
 511
 512/* remove a service from the the device
 513   scp->id must be set! */
 514void auerswald_removeservice(struct auerswald *cp, struct auerscon *scp)
 515{
 516        dbg("auerswald_removeservice called");
 517
 518        /* check if we have a service allocated */
 519        if (scp->id == AUH_UNASSIGNED)
 520                return;
 521
 522        /* If there is a device: close the channel */
 523        if (cp->usbdev && !cp->disconnecting) {
 524                /* Close the service channel inside the device */
 525                int ret = auerchain_control_msg(&cp->controlchain,              /* pointer to control chain */
 526                                                cp->usbdev,                     /* pointer to device */
 527                                                usb_sndctrlpipe(cp->usbdev, 0), /* pipe to control endpoint */
 528                                                AUV_CHANNELCTL,                 /* USB message request value */
 529                                                AUT_WREQ,                       /* USB message request type value */
 530                                                0x00,   /* close */             /* USB message value */
 531                                                scp->id,                        /* USB message index value */
 532                                                NULL,                           /* pointer to the data to send */
 533                                                0,                              /* length in bytes of the data to send */
 534                                                HZ * 2);                        /* time to wait for the message to complete before timing out */
 535                if (ret < 0) {
 536                        dbg("auerswald_removeservice: auerchain_control_msg returned error code %d", ret);
 537                } else {
 538                        dbg("auerswald_removeservice: channel close OK");
 539                }
 540        }
 541
 542        /* remove the service from the device */
 543        cp->services[scp->id] = NULL;
 544        scp->id = AUH_UNASSIGNED;
 545}
 546
 547
 548/*----------------------------------------------------------------------*/
 549/* File operation structure                                             */
 550static struct file_operations auerswald_fops = {
 551        owner:THIS_MODULE,
 552        llseek:auerchar_llseek,
 553        read:auerchar_read,
 554        write:auerchar_write,
 555        ioctl:auerchar_ioctl,
 556        open:auerchar_open,
 557        release:auerchar_release,
 558};
 559
 560/* --------------------------------------------------------------------- */
 561/* Special USB driver functions                                          */
 562
 563/* Probe if this driver wants to serve an USB device
 564
 565   This entry point is called whenever a new device is attached to the bus.
 566   Then the device driver has to create a new instance of its internal data
 567   structures for the new device.
 568
 569   The  dev argument specifies the device context, which contains pointers
 570   to all USB descriptors. The  interface argument specifies the interface
 571   number. If a USB driver wants to bind itself to a particular device and
 572   interface it has to return a pointer. This pointer normally references
 573   the device driver's context structure.
 574
 575   Probing normally is done by checking the vendor and product identifications
 576   or the class and subclass definitions. If they match the interface number
 577   is compared with the ones supported by the driver. When probing is done
 578   class based it might be necessary to parse some more USB descriptors because
 579   the device properties can differ in a wide range.
 580*/
 581static void *auerswald_probe(struct usb_device *usbdev, unsigned int ifnum,
 582                             const struct usb_device_id *id)
 583{
 584        struct auerswald *cp = NULL;
 585        DECLARE_WAIT_QUEUE_HEAD(wqh);
 586        unsigned int dtindex;
 587        unsigned int u = 0;
 588        char *pbuf;
 589        int ret;
 590
 591        dbg("probe: vendor id 0x%x, device id 0x%x ifnum:%d",
 592            usbdev->descriptor.idVendor, usbdev->descriptor.idProduct,
 593            ifnum);
 594
 595        /* See if the device offered us matches that we can accept */
 596        if (usbdev->descriptor.idVendor != ID_AUERSWALD)
 597                return NULL;
 598
 599        /* we use only the first -and only- interface */
 600        if (ifnum != 0)
 601                return NULL;
 602
 603        /* prevent module unloading while sleeping */
 604        MOD_INC_USE_COUNT;
 605
 606        /* allocate memory for our device and intialize it */
 607        cp = kmalloc(sizeof(struct auerswald), GFP_KERNEL);
 608        if (cp == NULL) {
 609                err("out of memory");
 610                goto pfail;
 611        }
 612
 613        /* Initialize device descriptor */
 614        memset(cp, 0, sizeof(struct auerswald));
 615        init_MUTEX(&cp->mutex);
 616        cp->usbdev = usbdev;
 617        auerchain_init(&cp->controlchain);
 618        auerbuf_init(&cp->bufctl);
 619        init_waitqueue_head(&cp->bufferwait);
 620        auerisdn_init_dev(cp);
 621
 622        /* find a free slot in the device table */
 623        down(&auerdev_table_mutex);
 624        for (dtindex = 0; dtindex < AUER_MAX_DEVICES; ++dtindex) {
 625                if (auerdev_table[dtindex] == NULL)
 626                        break;
 627        }
 628        if (dtindex >= AUER_MAX_DEVICES) {
 629                err("more than %d devices plugged in, can not handle this device", AUER_MAX_DEVICES);
 630                up(&auerdev_table_mutex);
 631                goto pfail;
 632        }
 633
 634        /* Give the device a name */
 635        sprintf(cp->name, AU_PREFIX "%d", dtindex);
 636
 637        /* Store the index */
 638        cp->dtindex = dtindex;
 639        auerdev_table[dtindex] = cp;
 640        up(&auerdev_table_mutex);
 641
 642        /* initialize the devfs node for this device and register it */
 643        cp->devfs = devfs_register(usb_devfs_handle, cp->name,
 644                                   DEVFS_FL_DEFAULT, USB_MAJOR,
 645                                   AUER_MINOR_BASE + dtindex,
 646                                   S_IFCHR | S_IRUGO | S_IWUGO,
 647                                   &auerswald_fops, NULL);
 648
 649        /* Get the usb version of the device */
 650        cp->version = cp->usbdev->descriptor.bcdDevice;
 651        dbg("Version is %X", cp->version);
 652
 653        /* allow some time to settle the device */
 654        sleep_on_timeout(&wqh, HZ / 3);
 655
 656        /* Try to get a suitable textual description of the device */
 657        /* Device name: */
 658        ret =
 659            usb_string(cp->usbdev, AUSI_DEVICE, cp->dev_desc,
 660                       AUSI_DLEN - 1);
 661        if (ret >= 0) {
 662                u += ret;
 663                /* Append Serial Number */
 664                memcpy(&cp->dev_desc[u], ",Ser# ", 6);
 665                u += 6;
 666                ret =
 667                    usb_string(cp->usbdev, AUSI_SERIALNR, &cp->dev_desc[u],
 668                               AUSI_DLEN - u - 1);
 669                if (ret >= 0) {
 670                        u += ret;
 671                        /* Append subscriber number */
 672                        memcpy(&cp->dev_desc[u], ", ", 2);
 673                        u += 2;
 674                        ret =
 675                            usb_string(cp->usbdev, AUSI_MSN,
 676                                       &cp->dev_desc[u],
 677                                       AUSI_DLEN - u - 1);
 678                        if (ret >= 0) {
 679                                u += ret;
 680                        }
 681                }
 682        }
 683        cp->dev_desc[u] = '\0';
 684        info("device is a %s", cp->dev_desc);
 685
 686        /* get the maximum allowed control transfer length */
 687        pbuf = (char *) kmalloc(2, GFP_KERNEL); /* use an allocated buffer because of urb target */
 688        if (!pbuf) {
 689                err("out of memory");
 690                goto pfail;
 691        }
 692        ret = usb_control_msg(cp->usbdev,                       /* pointer to device */
 693                              usb_rcvctrlpipe(cp->usbdev, 0),   /* pipe to control endpoint */
 694                              AUV_GETINFO,                      /* USB message request value */
 695                              AUT_RREQ,                         /* USB message request type value */
 696                              0,                                /* USB message value */
 697                              AUDI_MBCTRANS,                    /* USB message index value */
 698                              pbuf,                             /* pointer to the receive buffer */
 699                              2,                                /* length of the buffer */
 700                              HZ * 2);                          /* time to wait for the message to complete before timing out */
 701        if (ret == 2) {
 702                cp->maxControlLength = le16_to_cpup(pbuf);
 703                kfree(pbuf);
 704                dbg("setup: max. allowed control transfersize is %d bytes",
 705                    cp->maxControlLength);
 706        } else {
 707                kfree(pbuf);
 708                err("setup: getting max. allowed control transfer length failed with error %d", ret);
 709                goto pfail;
 710        }
 711        /* allocate a chain for the control messages */
 712        if (auerchain_setup(&cp->controlchain, AUCH_ELEMENTS)) {
 713                err("out of memory");
 714                goto pfail;
 715        }
 716
 717        /* allocate buffers for control messages */
 718        if (auerbuf_setup
 719            (&cp->bufctl, AU_RBUFFERS * 2,
 720             cp->maxControlLength + AUH_SIZE)) {
 721                err("out of memory");
 722                goto pfail;
 723        }
 724
 725        /* start the interrupt endpoint */
 726        if (auerswald_int_open(cp)) {
 727                err("int endpoint failed");
 728                goto pfail;
 729        }
 730
 731        /* Try to connect to hisax interface */
 732        if (auerisdn_probe(cp)) {
 733                err("hisax connect failed");
 734                goto pfail;
 735        }
 736
 737        /* all OK */
 738        return cp;
 739
 740        /* Error exit: clean up the memory */
 741      pfail:auerswald_delete(cp);
 742        MOD_DEC_USE_COUNT;
 743        return NULL;
 744}
 745
 746
 747/* Disconnect driver from a served device
 748
 749   This function is called whenever a device which was served by this driver
 750   is disconnected.
 751
 752   The argument  dev specifies the device context and the  driver_context
 753   returns a pointer to the previously registered  driver_context of the
 754   probe function. After returning from the disconnect function the USB
 755   framework completly deallocates all data structures associated with
 756   this device. So especially the usb_device structure must not be used
 757   any longer by the usb driver.
 758*/
 759static void auerswald_disconnect(struct usb_device *usbdev,
 760                                 void *driver_context)
 761{
 762        struct auerswald *cp = (struct auerswald *) driver_context;
 763        unsigned int u;
 764
 765        /* all parallel tasks can react on disconnect ASAP */
 766        cp->disconnecting = 1;
 767        down(&cp->mutex);
 768        info("device /dev/usb/%s now disconnecting", cp->name);
 769
 770        /* remove from device table */
 771        /* Nobody can open() this device any more */
 772        down(&auerdev_table_mutex);
 773        auerdev_table[cp->dtindex] = NULL;
 774        up(&auerdev_table_mutex);
 775
 776        /* remove our devfs node */
 777        /* Nobody can see this device any more */
 778        devfs_unregister(cp->devfs);
 779
 780        /* stop the ISDN connection */
 781        auerisdn_disconnect(cp);
 782
 783        /* Stop the interrupt endpoint 0 */
 784        auerswald_int_release(cp);
 785
 786        /* remove the control chain allocated in auerswald_probe
 787           This has the benefit of
 788           a) all pending (a)synchronous urbs are unlinked
 789           b) all buffers dealing with urbs are reclaimed
 790         */
 791        auerchain_free(&cp->controlchain);
 792
 793        if (cp->open_count == 0) {
 794                struct auerscon *scp;
 795                /* nobody is using this device. So we can clean up now */
 796                up(&cp->mutex); /* up() is possible here because no other task
 797                                   can open the device (see above). I don't want
 798                                   to kfree() a locked mutex. */
 799                /* disconnect the D channel */
 800                scp = cp->services[AUH_DCHANNEL];
 801                if (scp)
 802                        scp->disconnect(scp);
 803                auerswald_delete(cp);
 804        } else {
 805                /* device is used. Remove the pointer to the
 806                   usb device (it's not valid any more). The last
 807                   release() will do the clean up */
 808                cp->usbdev = NULL;
 809                up(&cp->mutex);
 810                /* Terminate waiting writers */
 811                wake_up(&cp->bufferwait);
 812                /* Inform all waiting readers */
 813                for (u = 0; u < AUH_TYPESIZE; u++) {
 814                        struct auerscon *scp = cp->services[u];
 815                        if (scp)
 816                                scp->disconnect(scp);
 817                }
 818        }
 819
 820        /* The device releases this module */
 821        MOD_DEC_USE_COUNT;
 822}
 823
 824/* Descriptor for the devices which are served by this driver.
 825   NOTE: this struct is parsed by the usbmanager install scripts.
 826         Don't change without caution!
 827*/
 828static struct usb_device_id auerswald_ids[] = {
 829        {USB_DEVICE(ID_AUERSWALD, 0x00C0)},     /* COMpact 2104 USB/DSL */
 830        {USB_DEVICE(ID_AUERSWALD, 0x00DB)},     /* COMpact 4410/2206 USB */
 831        {USB_DEVICE(ID_AUERSWALD, 0x00DC)},     /* COMpact 4406 DSL */
 832        {USB_DEVICE(ID_AUERSWALD, 0x00DD)},     /* COMpact 2204 USB */
 833        {USB_DEVICE(ID_AUERSWALD, 0x00F1)},     /* COMfort 2000 System Telephone */
 834        {USB_DEVICE(ID_AUERSWALD, 0x00F2)},     /* COMfort 1200 System Telephone */
 835        {}                                      /* Terminating entry */
 836};
 837
 838/* Standard module device table */
 839MODULE_DEVICE_TABLE(usb, auerswald_ids);
 840
 841/* Standard usb driver struct */
 842static struct usb_driver auerswald_driver = {
 843        name:"auerswald",
 844        probe:auerswald_probe,
 845        disconnect:auerswald_disconnect,
 846        fops:&auerswald_fops,
 847        minor:AUER_MINOR_BASE,
 848        id_table:auerswald_ids,
 849};
 850
 851
 852/* --------------------------------------------------------------------- */
 853/* Module loading/unloading                                              */
 854
 855/* Driver initialisation. Called after module loading.
 856   NOTE: there is no concurrency at _init
 857*/
 858static int __init auerswald_init(void)
 859{
 860        int result;
 861        dbg("init");
 862
 863        /* initialize the device table */
 864        memset(&auerdev_table, 0, sizeof(auerdev_table));
 865        init_MUTEX(&auerdev_table_mutex);
 866        auerisdn_init();
 867
 868        /* register driver at the USB subsystem */
 869        /* NOTE: usb_register() may call probe()! */
 870        result = usb_register(&auerswald_driver);
 871        if (result < 0) {
 872                err("driver could not be registered");
 873                return -1;
 874        }
 875        return 0;
 876}
 877
 878/* Driver deinit. Called before module removal.
 879   NOTE: there is no concurrency at _cleanup
 880*/
 881static void __exit auerswald_cleanup(void)
 882{
 883        dbg("cleanup");
 884        auerisdn_cleanup();
 885        usb_deregister(&auerswald_driver);
 886}
 887
 888/* --------------------------------------------------------------------- */
 889/* Linux device driver module description                                */
 890
 891MODULE_AUTHOR(DRIVER_AUTHOR);
 892MODULE_DESCRIPTION(DRIVER_DESC);
 893MODULE_LICENSE("GPL");
 894
 895module_init(auerswald_init);
 896module_exit(auerswald_cleanup);
 897
 898/* --------------------------------------------------------------------- */
 899
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.