linux/drivers/media/video/cx231xx/cx231xx-audio.c
<<
>>
Prefs
   1/*
   2 *  Conexant Cx231xx audio extension
   3 *
   4 *  Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
   5 *       Based on em28xx driver
   6 *
   7 *
   8 *  This program is free software; you can redistribute it and/or modify
   9 *  it under the terms of the GNU General Public License as published by
  10 *  the Free Software Foundation; either version 2 of the License, or
  11 *  (at your option) any later version.
  12 *
  13 *  This program is distributed in the hope that it will be useful,
  14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 *  GNU General Public License for more details.
  17 *
  18 *  You should have received a copy of the GNU General Public License
  19 *  along with this program; if not, write to the Free Software
  20 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21 */
  22
  23#include <linux/kernel.h>
  24#include <linux/usb.h>
  25#include <linux/init.h>
  26#include <linux/sound.h>
  27#include <linux/spinlock.h>
  28#include <linux/soundcard.h>
  29#include <linux/slab.h>
  30#include <linux/vmalloc.h>
  31#include <linux/proc_fs.h>
  32#include <linux/module.h>
  33#include <sound/core.h>
  34#include <sound/pcm.h>
  35#include <sound/pcm_params.h>
  36#include <sound/info.h>
  37#include <sound/initval.h>
  38#include <sound/control.h>
  39#include <media/v4l2-common.h>
  40#include "cx231xx.h"
  41
  42static int debug;
  43module_param(debug, int, 0644);
  44MODULE_PARM_DESC(debug, "activates debug info");
  45
  46#define dprintk(fmt, arg...) do {                                       \
  47                if (debug)                                              \
  48                        printk(KERN_INFO "cx231xx-audio %s: " fmt,      \
  49                                __func__, ##arg);                       \
  50        } while (0)
  51
  52static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  53
  54static int cx231xx_isoc_audio_deinit(struct cx231xx *dev)
  55{
  56        int i;
  57
  58        dprintk("Stopping isoc\n");
  59
  60        for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
  61                if (dev->adev.urb[i]) {
  62                        if (!irqs_disabled())
  63                                usb_kill_urb(dev->adev.urb[i]);
  64                        else
  65                                usb_unlink_urb(dev->adev.urb[i]);
  66
  67                        usb_free_urb(dev->adev.urb[i]);
  68                        dev->adev.urb[i] = NULL;
  69
  70                        kfree(dev->adev.transfer_buffer[i]);
  71                        dev->adev.transfer_buffer[i] = NULL;
  72                }
  73        }
  74
  75        return 0;
  76}
  77
  78static int cx231xx_bulk_audio_deinit(struct cx231xx *dev)
  79{
  80        int i;
  81
  82        dprintk("Stopping bulk\n");
  83
  84        for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
  85                if (dev->adev.urb[i]) {
  86                        if (!irqs_disabled())
  87                                usb_kill_urb(dev->adev.urb[i]);
  88                        else
  89                                usb_unlink_urb(dev->adev.urb[i]);
  90
  91                        usb_free_urb(dev->adev.urb[i]);
  92                        dev->adev.urb[i] = NULL;
  93
  94                        kfree(dev->adev.transfer_buffer[i]);
  95                        dev->adev.transfer_buffer[i] = NULL;
  96                }
  97        }
  98
  99        return 0;
 100}
 101
 102static void cx231xx_audio_isocirq(struct urb *urb)
 103{
 104        struct cx231xx *dev = urb->context;
 105        int i;
 106        unsigned int oldptr;
 107        int period_elapsed = 0;
 108        int status;
 109        unsigned char *cp;
 110        unsigned int stride;
 111        struct snd_pcm_substream *substream;
 112        struct snd_pcm_runtime *runtime;
 113
 114        if (dev->state & DEV_DISCONNECTED)
 115                return;
 116
 117        switch (urb->status) {
 118        case 0:         /* success */
 119        case -ETIMEDOUT:        /* NAK */
 120                break;
 121        case -ECONNRESET:       /* kill */
 122        case -ENOENT:
 123        case -ESHUTDOWN:
 124                return;
 125        default:                /* error */
 126                dprintk("urb completition error %d.\n", urb->status);
 127                break;
 128        }
 129
 130        if (atomic_read(&dev->stream_started) == 0)
 131                return;
 132
 133        if (dev->adev.capture_pcm_substream) {
 134                substream = dev->adev.capture_pcm_substream;
 135                runtime = substream->runtime;
 136                stride = runtime->frame_bits >> 3;
 137
 138                for (i = 0; i < urb->number_of_packets; i++) {
 139                        int length = urb->iso_frame_desc[i].actual_length /
 140                                     stride;
 141                        cp = (unsigned char *)urb->transfer_buffer +
 142                                              urb->iso_frame_desc[i].offset;
 143
 144                        if (!length)
 145                                continue;
 146
 147                        oldptr = dev->adev.hwptr_done_capture;
 148                        if (oldptr + length >= runtime->buffer_size) {
 149                                unsigned int cnt;
 150
 151                                cnt = runtime->buffer_size - oldptr;
 152                                memcpy(runtime->dma_area + oldptr * stride, cp,
 153                                       cnt * stride);
 154                                memcpy(runtime->dma_area, cp + cnt * stride,
 155                                       length * stride - cnt * stride);
 156                        } else {
 157                                memcpy(runtime->dma_area + oldptr * stride, cp,
 158                                       length * stride);
 159                        }
 160
 161                        snd_pcm_stream_lock(substream);
 162
 163                        dev->adev.hwptr_done_capture += length;
 164                        if (dev->adev.hwptr_done_capture >=
 165                                                runtime->buffer_size)
 166                                dev->adev.hwptr_done_capture -=
 167                                                runtime->buffer_size;
 168
 169                        dev->adev.capture_transfer_done += length;
 170                        if (dev->adev.capture_transfer_done >=
 171                                runtime->period_size) {
 172                                dev->adev.capture_transfer_done -=
 173                                                runtime->period_size;
 174                                period_elapsed = 1;
 175                        }
 176                        snd_pcm_stream_unlock(substream);
 177                }
 178                if (period_elapsed)
 179                        snd_pcm_period_elapsed(substream);
 180        }
 181        urb->status = 0;
 182
 183        status = usb_submit_urb(urb, GFP_ATOMIC);
 184        if (status < 0) {
 185                cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
 186                               status);
 187        }
 188        return;
 189}
 190
 191static void cx231xx_audio_bulkirq(struct urb *urb)
 192{
 193        struct cx231xx *dev = urb->context;
 194        unsigned int oldptr;
 195        int period_elapsed = 0;
 196        int status;
 197        unsigned char *cp;
 198        unsigned int stride;
 199        struct snd_pcm_substream *substream;
 200        struct snd_pcm_runtime *runtime;
 201
 202        if (dev->state & DEV_DISCONNECTED)
 203                return;
 204
 205        switch (urb->status) {
 206        case 0:         /* success */
 207        case -ETIMEDOUT:        /* NAK */
 208                break;
 209        case -ECONNRESET:       /* kill */
 210        case -ENOENT:
 211        case -ESHUTDOWN:
 212                return;
 213        default:                /* error */
 214                dprintk("urb completition error %d.\n", urb->status);
 215                break;
 216        }
 217
 218        if (atomic_read(&dev->stream_started) == 0)
 219                return;
 220
 221        if (dev->adev.capture_pcm_substream) {
 222                substream = dev->adev.capture_pcm_substream;
 223                runtime = substream->runtime;
 224                stride = runtime->frame_bits >> 3;
 225
 226                if (1) {
 227                        int length = urb->actual_length /
 228                                     stride;
 229                        cp = (unsigned char *)urb->transfer_buffer;
 230
 231                        oldptr = dev->adev.hwptr_done_capture;
 232                        if (oldptr + length >= runtime->buffer_size) {
 233                                unsigned int cnt;
 234
 235                                cnt = runtime->buffer_size - oldptr;
 236                                memcpy(runtime->dma_area + oldptr * stride, cp,
 237                                       cnt * stride);
 238                                memcpy(runtime->dma_area, cp + cnt * stride,
 239                                       length * stride - cnt * stride);
 240                        } else {
 241                                memcpy(runtime->dma_area + oldptr * stride, cp,
 242                                       length * stride);
 243                        }
 244
 245                        snd_pcm_stream_lock(substream);
 246
 247                        dev->adev.hwptr_done_capture += length;
 248                        if (dev->adev.hwptr_done_capture >=
 249                                                runtime->buffer_size)
 250                                dev->adev.hwptr_done_capture -=
 251                                                runtime->buffer_size;
 252
 253                        dev->adev.capture_transfer_done += length;
 254                        if (dev->adev.capture_transfer_done >=
 255                                runtime->period_size) {
 256                                dev->adev.capture_transfer_done -=
 257                                                runtime->period_size;
 258                                period_elapsed = 1;
 259                        }
 260                        snd_pcm_stream_unlock(substream);
 261                }
 262                if (period_elapsed)
 263                        snd_pcm_period_elapsed(substream);
 264        }
 265        urb->status = 0;
 266
 267        status = usb_submit_urb(urb, GFP_ATOMIC);
 268        if (status < 0) {
 269                cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
 270                               status);
 271        }
 272        return;
 273}
 274
 275static int cx231xx_init_audio_isoc(struct cx231xx *dev)
 276{
 277        int i, errCode;
 278        int sb_size;
 279
 280        cx231xx_info("%s: Starting ISO AUDIO transfers\n", __func__);
 281
 282        if (dev->state & DEV_DISCONNECTED)
 283                return -ENODEV;
 284
 285        sb_size = CX231XX_ISO_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
 286
 287        for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
 288                struct urb *urb;
 289                int j, k;
 290
 291                dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
 292                if (!dev->adev.transfer_buffer[i])
 293                        return -ENOMEM;
 294
 295                memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
 296                urb = usb_alloc_urb(CX231XX_ISO_NUM_AUDIO_PACKETS, GFP_ATOMIC);
 297                if (!urb) {
 298                        cx231xx_errdev("usb_alloc_urb failed!\n");
 299                        for (j = 0; j < i; j++) {
 300                                usb_free_urb(dev->adev.urb[j]);
 301                                kfree(dev->adev.transfer_buffer[j]);
 302                        }
 303                        return -ENOMEM;
 304                }
 305
 306                urb->dev = dev->udev;
 307                urb->context = dev;
 308                urb->pipe = usb_rcvisocpipe(dev->udev,
 309                                                dev->adev.end_point_addr);
 310                urb->transfer_flags = URB_ISO_ASAP;
 311                urb->transfer_buffer = dev->adev.transfer_buffer[i];
 312                urb->interval = 1;
 313                urb->complete = cx231xx_audio_isocirq;
 314                urb->number_of_packets = CX231XX_ISO_NUM_AUDIO_PACKETS;
 315                urb->transfer_buffer_length = sb_size;
 316
 317                for (j = k = 0; j < CX231XX_ISO_NUM_AUDIO_PACKETS;
 318                        j++, k += dev->adev.max_pkt_size) {
 319                        urb->iso_frame_desc[j].offset = k;
 320                        urb->iso_frame_desc[j].length = dev->adev.max_pkt_size;
 321                }
 322                dev->adev.urb[i] = urb;
 323        }
 324
 325        for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
 326                errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
 327                if (errCode < 0) {
 328                        cx231xx_isoc_audio_deinit(dev);
 329                        return errCode;
 330                }
 331        }
 332
 333        return errCode;
 334}
 335
 336static int cx231xx_init_audio_bulk(struct cx231xx *dev)
 337{
 338        int i, errCode;
 339        int sb_size;
 340
 341        cx231xx_info("%s: Starting BULK AUDIO transfers\n", __func__);
 342
 343        if (dev->state & DEV_DISCONNECTED)
 344                return -ENODEV;
 345
 346        sb_size = CX231XX_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
 347
 348        for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
 349                struct urb *urb;
 350                int j;
 351
 352                dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
 353                if (!dev->adev.transfer_buffer[i])
 354                        return -ENOMEM;
 355
 356                memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
 357                urb = usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
 358                if (!urb) {
 359                        cx231xx_errdev("usb_alloc_urb failed!\n");
 360                        for (j = 0; j < i; j++) {
 361                                usb_free_urb(dev->adev.urb[j]);
 362                                kfree(dev->adev.transfer_buffer[j]);
 363                        }
 364                        return -ENOMEM;
 365                }
 366
 367                urb->dev = dev->udev;
 368                urb->context = dev;
 369                urb->pipe = usb_rcvbulkpipe(dev->udev,
 370                                                dev->adev.end_point_addr);
 371                urb->transfer_flags = 0;
 372                urb->transfer_buffer = dev->adev.transfer_buffer[i];
 373                urb->complete = cx231xx_audio_bulkirq;
 374                urb->transfer_buffer_length = sb_size;
 375
 376                dev->adev.urb[i] = urb;
 377
 378        }
 379
 380        for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
 381                errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
 382                if (errCode < 0) {
 383                        cx231xx_bulk_audio_deinit(dev);
 384                        return errCode;
 385                }
 386        }
 387
 388        return errCode;
 389}
 390
 391static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
 392                                        size_t size)
 393{
 394        struct snd_pcm_runtime *runtime = subs->runtime;
 395
 396        dprintk("Allocating vbuffer\n");
 397        if (runtime->dma_area) {
 398                if (runtime->dma_bytes > size)
 399                        return 0;
 400
 401                vfree(runtime->dma_area);
 402        }
 403        runtime->dma_area = vmalloc(size);
 404        if (!runtime->dma_area)
 405                return -ENOMEM;
 406
 407        runtime->dma_bytes = size;
 408
 409        return 0;
 410}
 411
 412static struct snd_pcm_hardware snd_cx231xx_hw_capture = {
 413        .info = SNDRV_PCM_INFO_BLOCK_TRANSFER   |
 414            SNDRV_PCM_INFO_MMAP                 |
 415            SNDRV_PCM_INFO_INTERLEAVED          |
 416            SNDRV_PCM_INFO_MMAP_VALID,
 417
 418        .formats = SNDRV_PCM_FMTBIT_S16_LE,
 419
 420        .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
 421
 422        .rate_min = 48000,
 423        .rate_max = 48000,
 424        .channels_min = 2,
 425        .channels_max = 2,
 426        .buffer_bytes_max = 62720 * 8,  /* just about the value in usbaudio.c */
 427        .period_bytes_min = 64,         /* 12544/2, */
 428        .period_bytes_max = 12544,
 429        .periods_min = 2,
 430        .periods_max = 98,              /* 12544, */
 431};
 432
 433static int snd_cx231xx_capture_open(struct snd_pcm_substream *substream)
 434{
 435        struct cx231xx *dev = snd_pcm_substream_chip(substream);
 436        struct snd_pcm_runtime *runtime = substream->runtime;
 437        int ret = 0;
 438
 439        dprintk("opening device and trying to acquire exclusive lock\n");
 440
 441        if (!dev) {
 442                cx231xx_errdev("BUG: cx231xx can't find device struct."
 443                               " Can't proceed with open\n");
 444                return -ENODEV;
 445        }
 446
 447        if (dev->state & DEV_DISCONNECTED) {
 448                cx231xx_errdev("Can't open. the device was removed.\n");
 449                return -ENODEV;
 450        }
 451
 452        /* Sets volume, mute, etc */
 453        dev->mute = 0;
 454
 455        /* set alternate setting for audio interface */
 456        /* 1 - 48000 samples per sec */
 457        mutex_lock(&dev->lock);
 458        if (dev->USE_ISO)
 459                ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 1);
 460        else
 461                ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0);
 462        mutex_unlock(&dev->lock);
 463        if (ret < 0) {
 464                cx231xx_errdev("failed to set alternate setting !\n");
 465
 466                return ret;
 467        }
 468
 469        runtime->hw = snd_cx231xx_hw_capture;
 470
 471        mutex_lock(&dev->lock);
 472        /* inform hardware to start streaming */
 473        ret = cx231xx_capture_start(dev, 1, Audio);
 474
 475        dev->adev.users++;
 476        mutex_unlock(&dev->lock);
 477
 478        snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
 479        dev->adev.capture_pcm_substream = substream;
 480        runtime->private_data = dev;
 481
 482        return 0;
 483}
 484
 485static int snd_cx231xx_pcm_close(struct snd_pcm_substream *substream)
 486{
 487        int ret;
 488        struct cx231xx *dev = snd_pcm_substream_chip(substream);
 489
 490        dprintk("closing device\n");
 491
 492        /* inform hardware to stop streaming */
 493        mutex_lock(&dev->lock);
 494        ret = cx231xx_capture_start(dev, 0, Audio);
 495
 496        /* set alternate setting for audio interface */
 497        /* 1 - 48000 samples per sec */
 498        ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0);
 499        if (ret < 0) {
 500                cx231xx_errdev("failed to set alternate setting !\n");
 501
 502                mutex_unlock(&dev->lock);
 503                return ret;
 504        }
 505
 506        dev->mute = 1;
 507        dev->adev.users--;
 508        mutex_unlock(&dev->lock);
 509
 510        if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
 511                dprintk("audio users: %d\n", dev->adev.users);
 512                dprintk("disabling audio stream!\n");
 513                dev->adev.shutdown = 0;
 514                dprintk("released lock\n");
 515                if (atomic_read(&dev->stream_started) > 0) {
 516                        atomic_set(&dev->stream_started, 0);
 517                        schedule_work(&dev->wq_trigger);
 518                }
 519        }
 520        return 0;
 521}
 522
 523static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream *substream,
 524                                         struct snd_pcm_hw_params *hw_params)
 525{
 526        int ret;
 527
 528        dprintk("Setting capture parameters\n");
 529
 530        ret = snd_pcm_alloc_vmalloc_buffer(substream,
 531                                           params_buffer_bytes(hw_params));
 532#if 0
 533        /* TODO: set up cx231xx audio chip to deliver the correct audio format,
 534           current default is 48000hz multiplexed => 96000hz mono
 535           which shouldn't matter since analogue TV only supports mono */
 536        unsigned int channels, rate, format;
 537
 538        format = params_format(hw_params);
 539        rate = params_rate(hw_params);
 540        channels = params_channels(hw_params);
 541#endif
 542
 543        return ret;
 544}
 545
 546static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream *substream)
 547{
 548        struct cx231xx *dev = snd_pcm_substream_chip(substream);
 549
 550        dprintk("Stop capture, if needed\n");
 551
 552        if (atomic_read(&dev->stream_started) > 0) {
 553                atomic_set(&dev->stream_started, 0);
 554                schedule_work(&dev->wq_trigger);
 555        }
 556
 557        return 0;
 558}
 559
 560static int snd_cx231xx_prepare(struct snd_pcm_substream *substream)
 561{
 562        struct cx231xx *dev = snd_pcm_substream_chip(substream);
 563
 564        dev->adev.hwptr_done_capture = 0;
 565        dev->adev.capture_transfer_done = 0;
 566
 567        return 0;
 568}
 569
 570static void audio_trigger(struct work_struct *work)
 571{
 572        struct cx231xx *dev = container_of(work, struct cx231xx, wq_trigger);
 573
 574        if (atomic_read(&dev->stream_started)) {
 575                dprintk("starting capture");
 576                if (is_fw_load(dev) == 0)
 577                        cx25840_call(dev, core, load_fw);
 578                if (dev->USE_ISO)
 579                        cx231xx_init_audio_isoc(dev);
 580                else
 581                        cx231xx_init_audio_bulk(dev);
 582        } else {
 583                dprintk("stopping capture");
 584                cx231xx_isoc_audio_deinit(dev);
 585        }
 586}
 587
 588static int snd_cx231xx_capture_trigger(struct snd_pcm_substream *substream,
 589                                       int cmd)
 590{
 591        struct cx231xx *dev = snd_pcm_substream_chip(substream);
 592        int retval = 0;
 593
 594        if (dev->state & DEV_DISCONNECTED)
 595                return -ENODEV;
 596
 597        spin_lock(&dev->adev.slock);
 598        switch (cmd) {
 599        case SNDRV_PCM_TRIGGER_START:
 600                atomic_set(&dev->stream_started, 1);
 601                break;
 602        case SNDRV_PCM_TRIGGER_STOP:
 603                atomic_set(&dev->stream_started, 0);
 604                break;
 605        default:
 606                retval = -EINVAL;
 607                break;
 608        }
 609        spin_unlock(&dev->adev.slock);
 610
 611        schedule_work(&dev->wq_trigger);
 612
 613        return retval;
 614}
 615
 616static snd_pcm_uframes_t snd_cx231xx_capture_pointer(struct snd_pcm_substream
 617                                                     *substream)
 618{
 619        struct cx231xx *dev;
 620        unsigned long flags;
 621        snd_pcm_uframes_t hwptr_done;
 622
 623        dev = snd_pcm_substream_chip(substream);
 624
 625        spin_lock_irqsave(&dev->adev.slock, flags);
 626        hwptr_done = dev->adev.hwptr_done_capture;
 627        spin_unlock_irqrestore(&dev->adev.slock, flags);
 628
 629        return hwptr_done;
 630}
 631
 632static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
 633                                             unsigned long offset)
 634{
 635        void *pageptr = subs->runtime->dma_area + offset;
 636
 637        return vmalloc_to_page(pageptr);
 638}
 639
 640static struct snd_pcm_ops snd_cx231xx_pcm_capture = {
 641        .open = snd_cx231xx_capture_open,
 642        .close = snd_cx231xx_pcm_close,
 643        .ioctl = snd_pcm_lib_ioctl,
 644        .hw_params = snd_cx231xx_hw_capture_params,
 645        .hw_free = snd_cx231xx_hw_capture_free,
 646        .prepare = snd_cx231xx_prepare,
 647        .trigger = snd_cx231xx_capture_trigger,
 648        .pointer = snd_cx231xx_capture_pointer,
 649        .page = snd_pcm_get_vmalloc_page,
 650};
 651
 652static int cx231xx_audio_init(struct cx231xx *dev)
 653{
 654        struct cx231xx_audio *adev = &dev->adev;
 655        struct snd_pcm *pcm;
 656        struct snd_card *card;
 657        static int devnr;
 658        int err;
 659        struct usb_interface *uif;
 660        int i, isoc_pipe = 0;
 661
 662        if (dev->has_alsa_audio != 1) {
 663                /* This device does not support the extension (in this case
 664                   the device is expecting the snd-usb-audio module or
 665                   doesn't have analog audio support at all) */
 666                return 0;
 667        }
 668
 669        cx231xx_info("cx231xx-audio.c: probing for cx231xx "
 670                     "non standard usbaudio\n");
 671
 672        err = snd_card_create(index[devnr], "Cx231xx Audio", THIS_MODULE,
 673                              0, &card);
 674        if (err < 0)
 675                return err;
 676
 677        spin_lock_init(&adev->slock);
 678        err = snd_pcm_new(card, "Cx231xx Audio", 0, 0, 1, &pcm);
 679        if (err < 0) {
 680                snd_card_free(card);
 681                return err;
 682        }
 683
 684        snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
 685                        &snd_cx231xx_pcm_capture);
 686        pcm->info_flags = 0;
 687        pcm->private_data = dev;
 688        strcpy(pcm->name, "Conexant cx231xx Capture");
 689        snd_card_set_dev(card, &dev->udev->dev);
 690        strcpy(card->driver, "Cx231xx-Audio");
 691        strcpy(card->shortname, "Cx231xx Audio");
 692        strcpy(card->longname, "Conexant cx231xx Audio");
 693
 694        INIT_WORK(&dev->wq_trigger, audio_trigger);
 695
 696        err = snd_card_register(card);
 697        if (err < 0) {
 698                snd_card_free(card);
 699                return err;
 700        }
 701        adev->sndcard = card;
 702        adev->udev = dev->udev;
 703
 704        /* compute alternate max packet sizes for Audio */
 705        uif =
 706            dev->udev->actconfig->interface[dev->current_pcb_config.
 707                                            hs_config_info[0].interface_info.
 708                                            audio_index + 1];
 709
 710        adev->end_point_addr =
 711            le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
 712                        bEndpointAddress);
 713
 714        adev->num_alt = uif->num_altsetting;
 715        cx231xx_info("EndPoint Addr 0x%x, Alternate settings: %i\n",
 716                     adev->end_point_addr, adev->num_alt);
 717        adev->alt_max_pkt_size = kmalloc(32 * adev->num_alt, GFP_KERNEL);
 718
 719        if (adev->alt_max_pkt_size == NULL) {
 720                cx231xx_errdev("out of memory!\n");
 721                return -ENOMEM;
 722        }
 723
 724        for (i = 0; i < adev->num_alt; i++) {
 725                u16 tmp =
 726                    le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
 727                                wMaxPacketSize);
 728                adev->alt_max_pkt_size[i] =
 729                    (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
 730                cx231xx_info("Alternate setting %i, max size= %i\n", i,
 731                             adev->alt_max_pkt_size[i]);
 732        }
 733
 734        return 0;
 735}
 736
 737static int cx231xx_audio_fini(struct cx231xx *dev)
 738{
 739        if (dev == NULL)
 740                return 0;
 741
 742        if (dev->has_alsa_audio != 1) {
 743                /* This device does not support the extension (in this case
 744                   the device is expecting the snd-usb-audio module or
 745                   doesn't have analog audio support at all) */
 746                return 0;
 747        }
 748
 749        if (dev->adev.sndcard) {
 750                snd_card_free(dev->adev.sndcard);
 751                kfree(dev->adev.alt_max_pkt_size);
 752                dev->adev.sndcard = NULL;
 753        }
 754
 755        return 0;
 756}
 757
 758static struct cx231xx_ops audio_ops = {
 759        .id = CX231XX_AUDIO,
 760        .name = "Cx231xx Audio Extension",
 761        .init = cx231xx_audio_init,
 762        .fini = cx231xx_audio_fini,
 763};
 764
 765static int __init cx231xx_alsa_register(void)
 766{
 767        return cx231xx_register_extension(&audio_ops);
 768}
 769
 770static void __exit cx231xx_alsa_unregister(void)
 771{
 772        cx231xx_unregister_extension(&audio_ops);
 773}
 774
 775MODULE_LICENSE("GPL");
 776MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
 777MODULE_DESCRIPTION("Cx231xx Audio driver");
 778
 779module_init(cx231xx_alsa_register);
 780module_exit(cx231xx_alsa_unregister);
 781