Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 //
0003 // Empiatech em28x1 audio extension
0004 //
0005 // Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
0006 //
0007 // Copyright (C) 2007-2016 Mauro Carvalho Chehab
0008 //  - Port to work with the in-kernel driver
0009 //  - Cleanups, fixes, alsa-controls, etc.
0010 //
0011 // This driver is based on my previous au600 usb pstn audio driver
0012 // and inherits all the copyrights
0013 
0014 #include "em28xx.h"
0015 
0016 #include <linux/kernel.h>
0017 #include <linux/usb.h>
0018 #include <linux/init.h>
0019 #include <linux/sound.h>
0020 #include <linux/spinlock.h>
0021 #include <linux/soundcard.h>
0022 #include <linux/slab.h>
0023 #include <linux/module.h>
0024 #include <sound/core.h>
0025 #include <sound/pcm.h>
0026 #include <sound/pcm_params.h>
0027 #include <sound/info.h>
0028 #include <sound/initval.h>
0029 #include <sound/control.h>
0030 #include <sound/tlv.h>
0031 #include <sound/ac97_codec.h>
0032 #include <media/v4l2-common.h>
0033 
0034 static int debug;
0035 module_param(debug, int, 0644);
0036 MODULE_PARM_DESC(debug, "activates debug info");
0037 
0038 #define EM28XX_MAX_AUDIO_BUFS       5
0039 #define EM28XX_MIN_AUDIO_PACKETS    64
0040 
0041 #define dprintk(fmt, arg...) do {                   \
0042     if (debug)                      \
0043         dev_printk(KERN_DEBUG, &dev->intf->dev,         \
0044                "video: %s: " fmt, __func__, ## arg);    \
0045 } while (0)
0046 
0047 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
0048 
0049 static int em28xx_deinit_isoc_audio(struct em28xx *dev)
0050 {
0051     int i;
0052 
0053     dprintk("Stopping isoc\n");
0054     for (i = 0; i < dev->adev.num_urb; i++) {
0055         struct urb *urb = dev->adev.urb[i];
0056 
0057         if (!irqs_disabled())
0058             usb_kill_urb(urb);
0059         else
0060             usb_unlink_urb(urb);
0061     }
0062 
0063     return 0;
0064 }
0065 
0066 static void em28xx_audio_isocirq(struct urb *urb)
0067 {
0068     struct em28xx            *dev = urb->context;
0069     int                      i;
0070     unsigned int             oldptr;
0071     int                      period_elapsed = 0;
0072     int                      status;
0073     unsigned char            *cp;
0074     unsigned int             stride;
0075     struct snd_pcm_substream *substream;
0076     struct snd_pcm_runtime   *runtime;
0077 
0078     if (dev->disconnected) {
0079         dprintk("device disconnected while streaming. URB status=%d.\n",
0080             urb->status);
0081         atomic_set(&dev->adev.stream_started, 0);
0082         return;
0083     }
0084 
0085     switch (urb->status) {
0086     case 0:             /* success */
0087     case -ETIMEDOUT:    /* NAK */
0088         break;
0089     case -ECONNRESET:   /* kill */
0090     case -ENOENT:
0091     case -ESHUTDOWN:
0092         return;
0093     default:            /* error */
0094         dprintk("urb completion error %d.\n", urb->status);
0095         break;
0096     }
0097 
0098     if (atomic_read(&dev->adev.stream_started) == 0)
0099         return;
0100 
0101     if (dev->adev.capture_pcm_substream) {
0102         substream = dev->adev.capture_pcm_substream;
0103         runtime = substream->runtime;
0104         stride = runtime->frame_bits >> 3;
0105 
0106         for (i = 0; i < urb->number_of_packets; i++) {
0107             unsigned long flags;
0108             int length =
0109                 urb->iso_frame_desc[i].actual_length / stride;
0110             cp = (unsigned char *)urb->transfer_buffer +
0111                 urb->iso_frame_desc[i].offset;
0112 
0113             if (!length)
0114                 continue;
0115 
0116             oldptr = dev->adev.hwptr_done_capture;
0117             if (oldptr + length >= runtime->buffer_size) {
0118                 unsigned int cnt =
0119                     runtime->buffer_size - oldptr;
0120                 memcpy(runtime->dma_area + oldptr * stride, cp,
0121                        cnt * stride);
0122                 memcpy(runtime->dma_area, cp + cnt * stride,
0123                        length * stride - cnt * stride);
0124             } else {
0125                 memcpy(runtime->dma_area + oldptr * stride, cp,
0126                        length * stride);
0127             }
0128 
0129             snd_pcm_stream_lock_irqsave(substream, flags);
0130 
0131             dev->adev.hwptr_done_capture += length;
0132             if (dev->adev.hwptr_done_capture >=
0133                 runtime->buffer_size)
0134                 dev->adev.hwptr_done_capture -=
0135                     runtime->buffer_size;
0136 
0137             dev->adev.capture_transfer_done += length;
0138             if (dev->adev.capture_transfer_done >=
0139                 runtime->period_size) {
0140                 dev->adev.capture_transfer_done -=
0141                     runtime->period_size;
0142                 period_elapsed = 1;
0143             }
0144 
0145             snd_pcm_stream_unlock_irqrestore(substream, flags);
0146         }
0147         if (period_elapsed)
0148             snd_pcm_period_elapsed(substream);
0149     }
0150     urb->status = 0;
0151 
0152     status = usb_submit_urb(urb, GFP_ATOMIC);
0153     if (status < 0)
0154         dev_err(&dev->intf->dev,
0155             "resubmit of audio urb failed (error=%i)\n",
0156             status);
0157 }
0158 
0159 static int em28xx_init_audio_isoc(struct em28xx *dev)
0160 {
0161     int       i, err;
0162 
0163     dprintk("Starting isoc transfers\n");
0164 
0165     /* Start streaming */
0166     for (i = 0; i < dev->adev.num_urb; i++) {
0167         memset(dev->adev.transfer_buffer[i], 0x80,
0168                dev->adev.urb[i]->transfer_buffer_length);
0169 
0170         err = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
0171         if (err) {
0172             dev_err(&dev->intf->dev,
0173                 "submit of audio urb failed (error=%i)\n",
0174                 err);
0175             em28xx_deinit_isoc_audio(dev);
0176             atomic_set(&dev->adev.stream_started, 0);
0177             return err;
0178         }
0179     }
0180 
0181     return 0;
0182 }
0183 
0184 static const struct snd_pcm_hardware snd_em28xx_hw_capture = {
0185     .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
0186         SNDRV_PCM_INFO_MMAP           |
0187         SNDRV_PCM_INFO_INTERLEAVED    |
0188         SNDRV_PCM_INFO_BATCH          |
0189         SNDRV_PCM_INFO_MMAP_VALID,
0190 
0191     .formats = SNDRV_PCM_FMTBIT_S16_LE,
0192 
0193     .rates = SNDRV_PCM_RATE_48000,
0194 
0195     .rate_min = 48000,
0196     .rate_max = 48000,
0197     .channels_min = 2,
0198     .channels_max = 2,
0199     .buffer_bytes_max = 62720 * 8,  /* just about the value in usbaudio.c */
0200 
0201     /*
0202      * The period is 12.288 bytes. Allow a 10% of variation along its
0203      * value, in order to avoid overruns/underruns due to some clock
0204      * drift.
0205      *
0206      * FIXME: This period assumes 64 packets, and a 48000 PCM rate.
0207      * Calculate it dynamically.
0208      */
0209     .period_bytes_min = 11059,
0210     .period_bytes_max = 13516,
0211 
0212     .periods_min = 2,
0213     .periods_max = 98,      /* 12544, */
0214 };
0215 
0216 static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
0217 {
0218     struct em28xx *dev = snd_pcm_substream_chip(substream);
0219     struct snd_pcm_runtime *runtime = substream->runtime;
0220     int nonblock, ret = 0;
0221 
0222     if (!dev) {
0223         pr_err("em28xx-audio: BUG: em28xx can't find device struct. Can't proceed with open\n");
0224         return -ENODEV;
0225     }
0226 
0227     if (dev->disconnected)
0228         return -ENODEV;
0229 
0230     dprintk("opening device and trying to acquire exclusive lock\n");
0231 
0232     nonblock = !!(substream->f_flags & O_NONBLOCK);
0233     if (nonblock) {
0234         if (!mutex_trylock(&dev->lock))
0235             return -EAGAIN;
0236     } else {
0237         mutex_lock(&dev->lock);
0238     }
0239 
0240     runtime->hw = snd_em28xx_hw_capture;
0241 
0242     if (dev->adev.users == 0) {
0243         if (!dev->alt || dev->is_audio_only) {
0244             struct usb_device *udev;
0245 
0246             udev = interface_to_usbdev(dev->intf);
0247 
0248             if (dev->is_audio_only)
0249                 /* audio is on a separate interface */
0250                 dev->alt = 1;
0251             else
0252                 /* audio is on the same interface as video */
0253                 dev->alt = 7;
0254                 /*
0255                  * FIXME: The intention seems to be to select
0256                  * the alt setting with the largest
0257                  * wMaxPacketSize for the video endpoint.
0258                  * At least dev->alt should be used instead, but
0259                  * we should probably not touch it at all if it
0260                  * is already >0, because wMaxPacketSize of the
0261                  * audio endpoints seems to be the same for all.
0262                  */
0263             dprintk("changing alternate number on interface %d to %d\n",
0264                 dev->ifnum, dev->alt);
0265             usb_set_interface(udev, dev->ifnum, dev->alt);
0266         }
0267 
0268         /* Sets volume, mute, etc */
0269         dev->mute = 0;
0270         ret = em28xx_audio_analog_set(dev);
0271         if (ret < 0)
0272             goto err;
0273     }
0274 
0275     kref_get(&dev->ref);
0276     dev->adev.users++;
0277     mutex_unlock(&dev->lock);
0278 
0279     /* Dynamically adjust the period size */
0280     snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
0281     snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
0282                      dev->adev.period * 95 / 100,
0283                      dev->adev.period * 105 / 100);
0284 
0285     dev->adev.capture_pcm_substream = substream;
0286 
0287     return 0;
0288 err:
0289     mutex_unlock(&dev->lock);
0290 
0291     dev_err(&dev->intf->dev,
0292         "Error while configuring em28xx mixer\n");
0293     return ret;
0294 }
0295 
0296 static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
0297 {
0298     struct em28xx *dev = snd_pcm_substream_chip(substream);
0299 
0300     dprintk("closing device\n");
0301 
0302     dev->mute = 1;
0303     mutex_lock(&dev->lock);
0304     dev->adev.users--;
0305     if (atomic_read(&dev->adev.stream_started) > 0) {
0306         atomic_set(&dev->adev.stream_started, 0);
0307         schedule_work(&dev->adev.wq_trigger);
0308     }
0309 
0310     em28xx_audio_analog_set(dev);
0311     mutex_unlock(&dev->lock);
0312     kref_put(&dev->ref, em28xx_free_device);
0313 
0314     return 0;
0315 }
0316 
0317 static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
0318 {
0319     struct em28xx *dev = snd_pcm_substream_chip(substream);
0320 
0321     if (dev->disconnected)
0322         return -ENODEV;
0323 
0324     dev->adev.hwptr_done_capture = 0;
0325     dev->adev.capture_transfer_done = 0;
0326 
0327     return 0;
0328 }
0329 
0330 static void audio_trigger(struct work_struct *work)
0331 {
0332     struct em28xx_audio *adev =
0333                 container_of(work, struct em28xx_audio, wq_trigger);
0334     struct em28xx *dev = container_of(adev, struct em28xx, adev);
0335 
0336     if (atomic_read(&adev->stream_started)) {
0337         dprintk("starting capture");
0338         em28xx_init_audio_isoc(dev);
0339     } else {
0340         dprintk("stopping capture");
0341         em28xx_deinit_isoc_audio(dev);
0342     }
0343 }
0344 
0345 static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
0346                       int cmd)
0347 {
0348     struct em28xx *dev = snd_pcm_substream_chip(substream);
0349     int retval = 0;
0350 
0351     if (dev->disconnected)
0352         return -ENODEV;
0353 
0354     switch (cmd) {
0355     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0356     case SNDRV_PCM_TRIGGER_RESUME:
0357     case SNDRV_PCM_TRIGGER_START:
0358         atomic_set(&dev->adev.stream_started, 1);
0359         break;
0360     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0361     case SNDRV_PCM_TRIGGER_SUSPEND:
0362     case SNDRV_PCM_TRIGGER_STOP:
0363         atomic_set(&dev->adev.stream_started, 0);
0364         break;
0365     default:
0366         retval = -EINVAL;
0367     }
0368     schedule_work(&dev->adev.wq_trigger);
0369     return retval;
0370 }
0371 
0372 static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
0373                             *substream)
0374 {
0375     unsigned long flags;
0376     struct em28xx *dev;
0377     snd_pcm_uframes_t hwptr_done;
0378 
0379     dev = snd_pcm_substream_chip(substream);
0380     if (dev->disconnected)
0381         return SNDRV_PCM_POS_XRUN;
0382 
0383     spin_lock_irqsave(&dev->adev.slock, flags);
0384     hwptr_done = dev->adev.hwptr_done_capture;
0385     spin_unlock_irqrestore(&dev->adev.slock, flags);
0386 
0387     return hwptr_done;
0388 }
0389 
0390 /*
0391  * AC97 volume control support
0392  */
0393 static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
0394                struct snd_ctl_elem_info *info)
0395 {
0396     struct em28xx *dev = snd_kcontrol_chip(kcontrol);
0397 
0398     if (dev->disconnected)
0399         return -ENODEV;
0400 
0401     info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0402     info->count = 2;
0403     info->value.integer.min = 0;
0404     info->value.integer.max = 0x1f;
0405 
0406     return 0;
0407 }
0408 
0409 static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
0410               struct snd_ctl_elem_value *value)
0411 {
0412     struct em28xx *dev = snd_kcontrol_chip(kcontrol);
0413     struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
0414     u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
0415           (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
0416     int nonblock = 0;
0417     int rc;
0418 
0419     if (dev->disconnected)
0420         return -ENODEV;
0421 
0422     if (substream)
0423         nonblock = !!(substream->f_flags & O_NONBLOCK);
0424     if (nonblock) {
0425         if (!mutex_trylock(&dev->lock))
0426             return -EAGAIN;
0427     } else {
0428         mutex_lock(&dev->lock);
0429     }
0430     rc = em28xx_read_ac97(dev, kcontrol->private_value);
0431     if (rc < 0)
0432         goto err;
0433 
0434     val |= rc & 0x8000; /* Preserve the mute flag */
0435 
0436     rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
0437     if (rc < 0)
0438         goto err;
0439 
0440     dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
0441         (val & 0x8000) ? "muted " : "",
0442         0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
0443         val, (int)kcontrol->private_value);
0444 
0445 err:
0446     mutex_unlock(&dev->lock);
0447     return rc;
0448 }
0449 
0450 static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
0451               struct snd_ctl_elem_value *value)
0452 {
0453     struct em28xx *dev = snd_kcontrol_chip(kcontrol);
0454     struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
0455     int nonblock = 0;
0456     int val;
0457 
0458     if (dev->disconnected)
0459         return -ENODEV;
0460 
0461     if (substream)
0462         nonblock = !!(substream->f_flags & O_NONBLOCK);
0463     if (nonblock) {
0464         if (!mutex_trylock(&dev->lock))
0465             return -EAGAIN;
0466     } else {
0467         mutex_lock(&dev->lock);
0468     }
0469     val = em28xx_read_ac97(dev, kcontrol->private_value);
0470     mutex_unlock(&dev->lock);
0471     if (val < 0)
0472         return val;
0473 
0474     dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
0475         (val & 0x8000) ? "muted " : "",
0476         0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
0477         val, (int)kcontrol->private_value);
0478 
0479     value->value.integer.value[0] = 0x1f - (val & 0x1f);
0480     value->value.integer.value[1] = 0x1f - ((val >> 8) & 0x1f);
0481 
0482     return 0;
0483 }
0484 
0485 static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
0486                    struct snd_ctl_elem_value *value)
0487 {
0488     struct em28xx *dev = snd_kcontrol_chip(kcontrol);
0489     u16 val = value->value.integer.value[0];
0490     struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
0491     int nonblock = 0;
0492     int rc;
0493 
0494     if (dev->disconnected)
0495         return -ENODEV;
0496 
0497     if (substream)
0498         nonblock = !!(substream->f_flags & O_NONBLOCK);
0499     if (nonblock) {
0500         if (!mutex_trylock(&dev->lock))
0501             return -EAGAIN;
0502     } else {
0503         mutex_lock(&dev->lock);
0504     }
0505     rc = em28xx_read_ac97(dev, kcontrol->private_value);
0506     if (rc < 0)
0507         goto err;
0508 
0509     if (val)
0510         rc &= 0x1f1f;
0511     else
0512         rc |= 0x8000;
0513 
0514     rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
0515     if (rc < 0)
0516         goto err;
0517 
0518     dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
0519         (val & 0x8000) ? "muted " : "",
0520         0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
0521         val, (int)kcontrol->private_value);
0522 
0523 err:
0524     mutex_unlock(&dev->lock);
0525     return rc;
0526 }
0527 
0528 static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
0529                    struct snd_ctl_elem_value *value)
0530 {
0531     struct em28xx *dev = snd_kcontrol_chip(kcontrol);
0532     struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream;
0533     int nonblock = 0;
0534     int val;
0535 
0536     if (dev->disconnected)
0537         return -ENODEV;
0538 
0539     if (substream)
0540         nonblock = !!(substream->f_flags & O_NONBLOCK);
0541     if (nonblock) {
0542         if (!mutex_trylock(&dev->lock))
0543             return -EAGAIN;
0544     } else {
0545         mutex_lock(&dev->lock);
0546     }
0547     val = em28xx_read_ac97(dev, kcontrol->private_value);
0548     mutex_unlock(&dev->lock);
0549     if (val < 0)
0550         return val;
0551 
0552     if (val & 0x8000)
0553         value->value.integer.value[0] = 0;
0554     else
0555         value->value.integer.value[0] = 1;
0556 
0557     dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
0558         (val & 0x8000) ? "muted " : "",
0559         0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
0560         val, (int)kcontrol->private_value);
0561 
0562     return 0;
0563 }
0564 
0565 static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
0566 
0567 static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
0568                char *name, int id)
0569 {
0570     int err;
0571     char ctl_name[44];
0572     struct snd_kcontrol *kctl;
0573     struct snd_kcontrol_new tmp;
0574 
0575     memset(&tmp, 0, sizeof(tmp));
0576     tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
0577     tmp.private_value = id;
0578     tmp.name  = ctl_name;
0579 
0580     /* Add Mute Control */
0581     sprintf(ctl_name, "%s Switch", name);
0582     tmp.get  = em28xx_vol_get_mute;
0583     tmp.put  = em28xx_vol_put_mute;
0584     tmp.info = snd_ctl_boolean_mono_info;
0585     kctl = snd_ctl_new1(&tmp, dev);
0586     err = snd_ctl_add(card, kctl);
0587     if (err < 0)
0588         return err;
0589     dprintk("Added control %s for ac97 volume control 0x%04x\n",
0590         ctl_name, id);
0591 
0592     memset(&tmp, 0, sizeof(tmp));
0593     tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
0594     tmp.private_value = id;
0595     tmp.name  = ctl_name;
0596 
0597     /* Add Volume Control */
0598     sprintf(ctl_name, "%s Volume", name);
0599     tmp.get   = em28xx_vol_get;
0600     tmp.put   = em28xx_vol_put;
0601     tmp.info  = em28xx_vol_info;
0602     tmp.tlv.p = em28xx_db_scale;
0603     kctl = snd_ctl_new1(&tmp, dev);
0604     err = snd_ctl_add(card, kctl);
0605     if (err < 0)
0606         return err;
0607     dprintk("Added control %s for ac97 volume control 0x%04x\n",
0608         ctl_name, id);
0609 
0610     return 0;
0611 }
0612 
0613 /*
0614  * register/unregister code and data
0615  */
0616 static const struct snd_pcm_ops snd_em28xx_pcm_capture = {
0617     .open      = snd_em28xx_capture_open,
0618     .close     = snd_em28xx_pcm_close,
0619     .prepare   = snd_em28xx_prepare,
0620     .trigger   = snd_em28xx_capture_trigger,
0621     .pointer   = snd_em28xx_capture_pointer,
0622 };
0623 
0624 static void em28xx_audio_free_urb(struct em28xx *dev)
0625 {
0626     struct usb_device *udev = interface_to_usbdev(dev->intf);
0627     int i;
0628 
0629     for (i = 0; i < dev->adev.num_urb; i++) {
0630         struct urb *urb = dev->adev.urb[i];
0631 
0632         if (!urb)
0633             continue;
0634 
0635         usb_free_coherent(udev, urb->transfer_buffer_length,
0636                   dev->adev.transfer_buffer[i],
0637                   urb->transfer_dma);
0638 
0639         usb_free_urb(urb);
0640     }
0641     kfree(dev->adev.urb);
0642     kfree(dev->adev.transfer_buffer);
0643     dev->adev.num_urb = 0;
0644 }
0645 
0646 /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */
0647 static int em28xx_audio_ep_packet_size(struct usb_device *udev,
0648                        struct usb_endpoint_descriptor *e)
0649 {
0650     int size = le16_to_cpu(e->wMaxPacketSize);
0651 
0652     if (udev->speed == USB_SPEED_HIGH)
0653         return (size & 0x7ff) *  (1 + (((size) >> 11) & 0x03));
0654 
0655     return size & 0x7ff;
0656 }
0657 
0658 static int em28xx_audio_urb_init(struct em28xx *dev)
0659 {
0660     struct usb_interface *intf;
0661     struct usb_endpoint_descriptor *e, *ep = NULL;
0662     struct usb_device *udev = interface_to_usbdev(dev->intf);
0663     int                 i, ep_size, interval, num_urb, npackets;
0664     int         urb_size, bytes_per_transfer;
0665     u8 alt;
0666 
0667     if (dev->ifnum)
0668         alt = 1;
0669     else
0670         alt = 7;
0671 
0672     intf = usb_ifnum_to_if(udev, dev->ifnum);
0673 
0674     if (intf->num_altsetting <= alt) {
0675         dev_err(&dev->intf->dev, "alt %d doesn't exist on interface %d\n",
0676             dev->ifnum, alt);
0677         return -ENODEV;
0678     }
0679 
0680     for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) {
0681         e = &intf->altsetting[alt].endpoint[i].desc;
0682         if (!usb_endpoint_dir_in(e))
0683             continue;
0684         if (e->bEndpointAddress == EM28XX_EP_AUDIO) {
0685             ep = e;
0686             break;
0687         }
0688     }
0689 
0690     if (!ep) {
0691         dev_err(&dev->intf->dev, "Couldn't find an audio endpoint");
0692         return -ENODEV;
0693     }
0694 
0695     ep_size = em28xx_audio_ep_packet_size(udev, ep);
0696     interval = 1 << (ep->bInterval - 1);
0697 
0698     dev_info(&dev->intf->dev,
0699          "Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n",
0700          EM28XX_EP_AUDIO, usb_speed_string(udev->speed),
0701          dev->ifnum, alt, interval, ep_size);
0702 
0703     /* Calculate the number and size of URBs to better fit the audio samples */
0704 
0705     /*
0706      * Estimate the number of bytes per DMA transfer.
0707      *
0708      * This is given by the bit rate (for now, only 48000 Hz) multiplied
0709      * by 2 channels and 2 bytes/sample divided by the number of microframe
0710      * intervals and by the microframe rate (125 us)
0711      */
0712     bytes_per_transfer = DIV_ROUND_UP(48000 * 2 * 2, 125 * interval);
0713 
0714     /*
0715      * Estimate the number of transfer URBs. Don't let it go past the
0716      * maximum number of URBs that is known to be supported by the device.
0717      */
0718     num_urb = DIV_ROUND_UP(bytes_per_transfer, ep_size);
0719     if (num_urb > EM28XX_MAX_AUDIO_BUFS)
0720         num_urb = EM28XX_MAX_AUDIO_BUFS;
0721 
0722     /*
0723      * Now that we know the number of bytes per transfer and the number of
0724      * URBs, estimate the typical size of an URB, in order to adjust the
0725      * minimal number of packets.
0726      */
0727     urb_size = bytes_per_transfer / num_urb;
0728 
0729     /*
0730      * Now, calculate the amount of audio packets to be filled on each
0731      * URB. In order to preserve the old behaviour, use a minimal
0732      * threshold for this value.
0733      */
0734     npackets = EM28XX_MIN_AUDIO_PACKETS;
0735     if (urb_size > ep_size * npackets)
0736         npackets = DIV_ROUND_UP(urb_size, ep_size);
0737 
0738     dev_info(&dev->intf->dev,
0739          "Number of URBs: %d, with %d packets and %d size\n",
0740          num_urb, npackets, urb_size);
0741 
0742     /* Estimate the bytes per period */
0743     dev->adev.period = urb_size * npackets;
0744 
0745     /* Allocate space to store the number of URBs to be used */
0746 
0747     dev->adev.transfer_buffer = kcalloc(num_urb,
0748                         sizeof(*dev->adev.transfer_buffer),
0749                         GFP_KERNEL);
0750     if (!dev->adev.transfer_buffer)
0751         return -ENOMEM;
0752 
0753     dev->adev.urb = kcalloc(num_urb, sizeof(*dev->adev.urb), GFP_KERNEL);
0754     if (!dev->adev.urb) {
0755         kfree(dev->adev.transfer_buffer);
0756         return -ENOMEM;
0757     }
0758 
0759     /* Alloc memory for each URB and for each transfer buffer */
0760     dev->adev.num_urb = num_urb;
0761     for (i = 0; i < num_urb; i++) {
0762         struct urb *urb;
0763         int j, k;
0764         void *buf;
0765 
0766         urb = usb_alloc_urb(npackets, GFP_KERNEL);
0767         if (!urb) {
0768             em28xx_audio_free_urb(dev);
0769             return -ENOMEM;
0770         }
0771         dev->adev.urb[i] = urb;
0772 
0773         buf = usb_alloc_coherent(udev, npackets * ep_size, GFP_KERNEL,
0774                      &urb->transfer_dma);
0775         if (!buf) {
0776             dev_err(&dev->intf->dev,
0777                 "usb_alloc_coherent failed!\n");
0778             em28xx_audio_free_urb(dev);
0779             return -ENOMEM;
0780         }
0781         dev->adev.transfer_buffer[i] = buf;
0782 
0783         urb->dev = udev;
0784         urb->context = dev;
0785         urb->pipe = usb_rcvisocpipe(udev, EM28XX_EP_AUDIO);
0786         urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
0787         urb->transfer_buffer = buf;
0788         urb->interval = interval;
0789         urb->complete = em28xx_audio_isocirq;
0790         urb->number_of_packets = npackets;
0791         urb->transfer_buffer_length = ep_size * npackets;
0792 
0793         for (j = k = 0; j < npackets; j++, k += ep_size) {
0794             urb->iso_frame_desc[j].offset = k;
0795             urb->iso_frame_desc[j].length = ep_size;
0796         }
0797     }
0798 
0799     return 0;
0800 }
0801 
0802 static int em28xx_audio_init(struct em28xx *dev)
0803 {
0804     struct em28xx_audio *adev = &dev->adev;
0805     struct usb_device *udev = interface_to_usbdev(dev->intf);
0806     struct snd_pcm      *pcm;
0807     struct snd_card     *card;
0808     static int          devnr;
0809     int         err;
0810 
0811     if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
0812         /*
0813          * This device does not support the extension (in this case
0814          * the device is expecting the snd-usb-audio module or
0815          * doesn't have analog audio support at all)
0816          */
0817         return 0;
0818     }
0819 
0820     dev_info(&dev->intf->dev, "Binding audio extension\n");
0821 
0822     kref_get(&dev->ref);
0823 
0824     dev_info(&dev->intf->dev,
0825          "em28xx-audio.c: Copyright (C) 2006 Markus Rechberger\n");
0826     dev_info(&dev->intf->dev,
0827          "em28xx-audio.c: Copyright (C) 2007-2016 Mauro Carvalho Chehab\n");
0828 
0829     err = snd_card_new(&dev->intf->dev, index[devnr], "Em28xx Audio",
0830                THIS_MODULE, 0, &card);
0831     if (err < 0)
0832         return err;
0833 
0834     spin_lock_init(&adev->slock);
0835     adev->sndcard = card;
0836     adev->udev = udev;
0837 
0838     err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
0839     if (err < 0)
0840         goto card_free;
0841 
0842     snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
0843     snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
0844     pcm->info_flags = 0;
0845     pcm->private_data = dev;
0846     strscpy(pcm->name, "Empia 28xx Capture", sizeof(pcm->name));
0847 
0848     strscpy(card->driver, "Em28xx-Audio", sizeof(card->driver));
0849     strscpy(card->shortname, "Em28xx Audio", sizeof(card->shortname));
0850     strscpy(card->longname, "Empia Em28xx Audio", sizeof(card->longname));
0851 
0852     INIT_WORK(&adev->wq_trigger, audio_trigger);
0853 
0854     if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
0855         em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
0856         em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
0857         em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
0858         em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
0859         em28xx_cvol_new(card, dev, "CD", AC97_CD);
0860         em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
0861         em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
0862 
0863         em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
0864         em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
0865         em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
0866         em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
0867         em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
0868     }
0869 
0870     err = em28xx_audio_urb_init(dev);
0871     if (err)
0872         goto card_free;
0873 
0874     err = snd_card_register(card);
0875     if (err < 0)
0876         goto urb_free;
0877 
0878     dev_info(&dev->intf->dev, "Audio extension successfully initialized\n");
0879     return 0;
0880 
0881 urb_free:
0882     em28xx_audio_free_urb(dev);
0883 
0884 card_free:
0885     snd_card_free(card);
0886     adev->sndcard = NULL;
0887 
0888     return err;
0889 }
0890 
0891 static int em28xx_audio_fini(struct em28xx *dev)
0892 {
0893     if (!dev)
0894         return 0;
0895 
0896     if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR) {
0897         /*
0898          * This device does not support the extension (in this case
0899          * the device is expecting the snd-usb-audio module or
0900          * doesn't have analog audio support at all)
0901          */
0902         return 0;
0903     }
0904 
0905     dev_info(&dev->intf->dev, "Closing audio extension\n");
0906 
0907     if (dev->adev.sndcard) {
0908         snd_card_disconnect(dev->adev.sndcard);
0909         flush_work(&dev->adev.wq_trigger);
0910 
0911         em28xx_audio_free_urb(dev);
0912 
0913         snd_card_free(dev->adev.sndcard);
0914         dev->adev.sndcard = NULL;
0915     }
0916 
0917     kref_put(&dev->ref, em28xx_free_device);
0918     return 0;
0919 }
0920 
0921 static int em28xx_audio_suspend(struct em28xx *dev)
0922 {
0923     if (!dev)
0924         return 0;
0925 
0926     if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR)
0927         return 0;
0928 
0929     dev_info(&dev->intf->dev, "Suspending audio extension\n");
0930     em28xx_deinit_isoc_audio(dev);
0931     atomic_set(&dev->adev.stream_started, 0);
0932     return 0;
0933 }
0934 
0935 static int em28xx_audio_resume(struct em28xx *dev)
0936 {
0937     if (!dev)
0938         return 0;
0939 
0940     if (dev->usb_audio_type != EM28XX_USB_AUDIO_VENDOR)
0941         return 0;
0942 
0943     dev_info(&dev->intf->dev, "Resuming audio extension\n");
0944     /* Nothing to do other than schedule_work() ?? */
0945     schedule_work(&dev->adev.wq_trigger);
0946     return 0;
0947 }
0948 
0949 static struct em28xx_ops audio_ops = {
0950     .id   = EM28XX_AUDIO,
0951     .name = "Em28xx Audio Extension",
0952     .init = em28xx_audio_init,
0953     .fini = em28xx_audio_fini,
0954     .suspend = em28xx_audio_suspend,
0955     .resume = em28xx_audio_resume,
0956 };
0957 
0958 static int __init em28xx_alsa_register(void)
0959 {
0960     return em28xx_register_extension(&audio_ops);
0961 }
0962 
0963 static void __exit em28xx_alsa_unregister(void)
0964 {
0965     em28xx_unregister_extension(&audio_ops);
0966 }
0967 
0968 MODULE_LICENSE("GPL v2");
0969 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
0970 MODULE_AUTHOR("Mauro Carvalho Chehab");
0971 MODULE_DESCRIPTION(DRIVER_DESC " - audio interface");
0972 MODULE_VERSION(EM28XX_VERSION);
0973 
0974 module_init(em28xx_alsa_register);
0975 module_exit(em28xx_alsa_unregister);