Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Empiatech em28x1 audio extension |
| 3 | * |
| 4 | * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com> |
| 5 | * |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 6 | * Copyright (C) 2007-2014 Mauro Carvalho Chehab |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 7 | * - Port to work with the in-kernel driver |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame] | 8 | * - Cleanups, fixes, alsa-controls, etc. |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 9 | * |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 10 | * This driver is based on my previous au600 usb pstn audio driver |
| 11 | * and inherits all the copyrights |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 26 | */ |
| 27 | |
| 28 | #include <linux/kernel.h> |
| 29 | #include <linux/usb.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/sound.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/soundcard.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/vmalloc.h> |
| 36 | #include <linux/proc_fs.h> |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 37 | #include <linux/module.h> |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 38 | #include <sound/core.h> |
| 39 | #include <sound/pcm.h> |
| 40 | #include <sound/pcm_params.h> |
| 41 | #include <sound/info.h> |
| 42 | #include <sound/initval.h> |
| 43 | #include <sound/control.h> |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 44 | #include <sound/tlv.h> |
Ezequiel GarcĂa | 67b8d03 | 2012-06-11 15:17:23 -0300 | [diff] [blame] | 45 | #include <sound/ac97_codec.h> |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 46 | #include <media/v4l2-common.h> |
| 47 | #include "em28xx.h" |
| 48 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 49 | static int debug; |
| 50 | module_param(debug, int, 0644); |
| 51 | MODULE_PARM_DESC(debug, "activates debug info"); |
| 52 | |
| 53 | #define dprintk(fmt, arg...) do { \ |
| 54 | if (debug) \ |
| 55 | printk(KERN_INFO "em28xx-audio %s: " fmt, \ |
Harvey Harrison | d80e134 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 56 | __func__, ##arg); \ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 57 | } while (0) |
| 58 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 59 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 60 | |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 61 | static int em28xx_deinit_isoc_audio(struct em28xx *dev) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 62 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 63 | int i; |
| 64 | |
| 65 | dprintk("Stopping isoc\n"); |
Douglas Schilling Landgraf | 3e099ba | 2009-02-08 10:45:34 -0300 | [diff] [blame] | 66 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
Mauro Carvalho Chehab | 46c704d | 2013-12-28 11:47:16 -0300 | [diff] [blame] | 67 | struct urb *urb = dev->adev.urb[i]; |
| 68 | |
Robert Krakora | 9c06210 | 2009-01-25 13:08:07 -0300 | [diff] [blame] | 69 | if (!irqs_disabled()) |
Mauro Carvalho Chehab | 46c704d | 2013-12-28 11:47:16 -0300 | [diff] [blame] | 70 | usb_kill_urb(urb); |
Robert Krakora | 9c06210 | 2009-01-25 13:08:07 -0300 | [diff] [blame] | 71 | else |
Mauro Carvalho Chehab | 46c704d | 2013-12-28 11:47:16 -0300 | [diff] [blame] | 72 | usb_unlink_urb(urb); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 73 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 74 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 78 | static void em28xx_audio_isocirq(struct urb *urb) |
| 79 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 80 | struct em28xx *dev = urb->context; |
| 81 | int i; |
| 82 | unsigned int oldptr; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 83 | int period_elapsed = 0; |
| 84 | int status; |
| 85 | unsigned char *cp; |
| 86 | unsigned int stride; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 87 | struct snd_pcm_substream *substream; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 88 | struct snd_pcm_runtime *runtime; |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 89 | |
| 90 | switch (urb->status) { |
| 91 | case 0: /* success */ |
| 92 | case -ETIMEDOUT: /* NAK */ |
| 93 | break; |
| 94 | case -ECONNRESET: /* kill */ |
| 95 | case -ENOENT: |
| 96 | case -ESHUTDOWN: |
| 97 | return; |
| 98 | default: /* error */ |
| 99 | dprintk("urb completition error %d.\n", urb->status); |
| 100 | break; |
| 101 | } |
| 102 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 103 | if (atomic_read(&dev->stream_started) == 0) |
| 104 | return; |
| 105 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 106 | if (dev->adev.capture_pcm_substream) { |
| 107 | substream = dev->adev.capture_pcm_substream; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 108 | runtime = substream->runtime; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 109 | stride = runtime->frame_bits >> 3; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 110 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 111 | for (i = 0; i < urb->number_of_packets; i++) { |
| 112 | int length = |
| 113 | urb->iso_frame_desc[i].actual_length / stride; |
| 114 | cp = (unsigned char *)urb->transfer_buffer + |
| 115 | urb->iso_frame_desc[i].offset; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 116 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 117 | if (!length) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 118 | continue; |
| 119 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 120 | oldptr = dev->adev.hwptr_done_capture; |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 121 | if (oldptr + length >= runtime->buffer_size) { |
| 122 | unsigned int cnt = |
| 123 | runtime->buffer_size - oldptr; |
| 124 | memcpy(runtime->dma_area + oldptr * stride, cp, |
| 125 | cnt * stride); |
| 126 | memcpy(runtime->dma_area, cp + cnt * stride, |
| 127 | length * stride - cnt * stride); |
| 128 | } else { |
| 129 | memcpy(runtime->dma_area + oldptr * stride, cp, |
| 130 | length * stride); |
| 131 | } |
| 132 | |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 133 | snd_pcm_stream_lock(substream); |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 134 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 135 | dev->adev.hwptr_done_capture += length; |
| 136 | if (dev->adev.hwptr_done_capture >= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 137 | runtime->buffer_size) |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 138 | dev->adev.hwptr_done_capture -= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 139 | runtime->buffer_size; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 140 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 141 | dev->adev.capture_transfer_done += length; |
| 142 | if (dev->adev.capture_transfer_done >= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 143 | runtime->period_size) { |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 144 | dev->adev.capture_transfer_done -= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 145 | runtime->period_size; |
| 146 | period_elapsed = 1; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 147 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 148 | |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 149 | snd_pcm_stream_unlock(substream); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 150 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 151 | if (period_elapsed) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 152 | snd_pcm_period_elapsed(substream); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 153 | } |
| 154 | urb->status = 0; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 155 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 156 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 157 | if (status < 0) { |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 158 | em28xx_errdev("resubmit of audio urb failed (error=%i)\n", |
| 159 | status); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 160 | } |
| 161 | return; |
| 162 | } |
| 163 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 164 | static int em28xx_init_audio_isoc(struct em28xx *dev) |
| 165 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 166 | int i, errCode; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 167 | |
| 168 | dprintk("Starting isoc transfers\n"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 169 | |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 170 | /* Start streaming */ |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 171 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 172 | memset(dev->adev.transfer_buffer[i], 0x80, |
| 173 | dev->adev.urb[i]->transfer_buffer_length); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 174 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 175 | errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 176 | if (errCode) { |
Mauro Carvalho Chehab | debb724 | 2011-06-19 10:15:35 -0300 | [diff] [blame] | 177 | em28xx_errdev("submit of audio urb failed\n"); |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 178 | em28xx_deinit_isoc_audio(dev); |
Mauro Carvalho Chehab | debb724 | 2011-06-19 10:15:35 -0300 | [diff] [blame] | 179 | atomic_set(&dev->stream_started, 0); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 180 | return errCode; |
| 181 | } |
Mauro Carvalho Chehab | debb724 | 2011-06-19 10:15:35 -0300 | [diff] [blame] | 182 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 183 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 184 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 188 | static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, |
| 189 | size_t size) |
| 190 | { |
| 191 | struct snd_pcm_runtime *runtime = subs->runtime; |
| 192 | |
Nicola Soranzo | a1a6ee7 | 2009-02-10 23:28:24 -0300 | [diff] [blame] | 193 | dprintk("Allocating vbuffer\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 194 | if (runtime->dma_area) { |
| 195 | if (runtime->dma_bytes > size) |
| 196 | return 0; |
| 197 | |
| 198 | vfree(runtime->dma_area); |
| 199 | } |
| 200 | runtime->dma_area = vmalloc(size); |
| 201 | if (!runtime->dma_area) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | runtime->dma_bytes = size; |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static struct snd_pcm_hardware snd_em28xx_hw_capture = { |
| 210 | .info = SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 211 | SNDRV_PCM_INFO_MMAP | |
| 212 | SNDRV_PCM_INFO_INTERLEAVED | |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 213 | SNDRV_PCM_INFO_BATCH | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 214 | SNDRV_PCM_INFO_MMAP_VALID, |
| 215 | |
| 216 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 217 | |
| 218 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT, |
| 219 | |
| 220 | .rate_min = 48000, |
| 221 | .rate_max = 48000, |
| 222 | .channels_min = 2, |
| 223 | .channels_max = 2, |
| 224 | .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */ |
| 225 | .period_bytes_min = 64, /* 12544/2, */ |
| 226 | .period_bytes_max = 12544, |
| 227 | .periods_min = 2, |
| 228 | .periods_max = 98, /* 12544, */ |
| 229 | }; |
| 230 | |
| 231 | static int snd_em28xx_capture_open(struct snd_pcm_substream *substream) |
| 232 | { |
| 233 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 234 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 235 | int ret = 0; |
| 236 | |
| 237 | dprintk("opening device and trying to acquire exclusive lock\n"); |
| 238 | |
Mauro Carvalho Chehab | 83ee87a | 2008-06-14 09:41:18 -0300 | [diff] [blame] | 239 | if (!dev) { |
Filipe Rosset | 0bc2308 | 2009-11-04 15:31:25 -0300 | [diff] [blame] | 240 | em28xx_err("BUG: em28xx can't find device struct." |
Mauro Carvalho Chehab | 83ee87a | 2008-06-14 09:41:18 -0300 | [diff] [blame] | 241 | " Can't proceed with open\n"); |
| 242 | return -ENODEV; |
| 243 | } |
| 244 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 245 | runtime->hw = snd_em28xx_hw_capture; |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame] | 246 | if ((dev->alt == 0 || dev->audio_ifnum) && dev->adev.users == 0) { |
| 247 | if (dev->audio_ifnum) |
| 248 | dev->alt = 1; |
| 249 | else |
| 250 | dev->alt = 7; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 251 | |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame] | 252 | dprintk("changing alternate number on interface %d to %d\n", |
| 253 | dev->audio_ifnum, dev->alt); |
| 254 | usb_set_interface(dev->udev, dev->audio_ifnum, dev->alt); |
| 255 | |
| 256 | /* Sets volume, mute, etc */ |
| 257 | dev->mute = 0; |
| 258 | mutex_lock(&dev->lock); |
| 259 | ret = em28xx_audio_analog_set(dev); |
| 260 | if (ret < 0) |
| 261 | goto err; |
| 262 | |
| 263 | dev->adev.users++; |
| 264 | mutex_unlock(&dev->lock); |
| 265 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 266 | |
| 267 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 268 | dev->adev.capture_pcm_substream = substream; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | err: |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 272 | mutex_unlock(&dev->lock); |
| 273 | |
Filipe Rosset | 0bc2308 | 2009-11-04 15:31:25 -0300 | [diff] [blame] | 274 | em28xx_err("Error while configuring em28xx mixer\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream) |
| 279 | { |
| 280 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 281 | |
| 282 | dprintk("closing device\n"); |
| 283 | |
| 284 | dev->mute = 1; |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 285 | mutex_lock(&dev->lock); |
Douglas Schilling Landgraf | bf510ac3 | 2009-02-08 01:11:13 -0300 | [diff] [blame] | 286 | dev->adev.users--; |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 287 | if (atomic_read(&dev->stream_started) > 0) { |
| 288 | atomic_set(&dev->stream_started, 0); |
| 289 | schedule_work(&dev->wq_trigger); |
| 290 | } |
| 291 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 292 | em28xx_audio_analog_set(dev); |
Robert Krakora | 75c74d1 | 2009-05-28 11:16:19 -0300 | [diff] [blame] | 293 | if (substream->runtime->dma_area) { |
| 294 | dprintk("freeing\n"); |
| 295 | vfree(substream->runtime->dma_area); |
| 296 | substream->runtime->dma_area = NULL; |
| 297 | } |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 298 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 299 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream, |
| 304 | struct snd_pcm_hw_params *hw_params) |
| 305 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 306 | int ret; |
| 307 | |
| 308 | dprintk("Setting capture parameters\n"); |
| 309 | |
| 310 | ret = snd_pcm_alloc_vmalloc_buffer(substream, |
| 311 | params_buffer_bytes(hw_params)); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 312 | if (ret < 0) |
| 313 | return ret; |
Hans Verkuil | e92ba28 | 2012-05-14 10:17:35 -0300 | [diff] [blame] | 314 | #if 0 |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 315 | /* TODO: set up em28xx audio chip to deliver the correct audio format, |
| 316 | current default is 48000hz multiplexed => 96000hz mono |
| 317 | which shouldn't matter since analogue TV only supports mono */ |
Hans Verkuil | e92ba28 | 2012-05-14 10:17:35 -0300 | [diff] [blame] | 318 | unsigned int channels, rate, format; |
| 319 | |
| 320 | format = params_format(hw_params); |
| 321 | rate = params_rate(hw_params); |
| 322 | channels = params_channels(hw_params); |
| 323 | #endif |
| 324 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream) |
| 329 | { |
| 330 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 331 | |
| 332 | dprintk("Stop capture, if needed\n"); |
| 333 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 334 | if (atomic_read(&dev->stream_started) > 0) { |
| 335 | atomic_set(&dev->stream_started, 0); |
| 336 | schedule_work(&dev->wq_trigger); |
| 337 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int snd_em28xx_prepare(struct snd_pcm_substream *substream) |
| 343 | { |
Devin Heitmueller | 96fbf77 | 2009-10-15 01:14:34 -0300 | [diff] [blame] | 344 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 345 | |
| 346 | dev->adev.hwptr_done_capture = 0; |
| 347 | dev->adev.capture_transfer_done = 0; |
| 348 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 352 | static void audio_trigger(struct work_struct *work) |
| 353 | { |
| 354 | struct em28xx *dev = container_of(work, struct em28xx, wq_trigger); |
| 355 | |
| 356 | if (atomic_read(&dev->stream_started)) { |
| 357 | dprintk("starting capture"); |
| 358 | em28xx_init_audio_isoc(dev); |
| 359 | } else { |
| 360 | dprintk("stopping capture"); |
| 361 | em28xx_deinit_isoc_audio(dev); |
| 362 | } |
| 363 | } |
| 364 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 365 | static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream, |
| 366 | int cmd) |
| 367 | { |
| 368 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 369 | int retval = 0; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 370 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 371 | switch (cmd) { |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 372 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */ |
| 373 | case SNDRV_PCM_TRIGGER_RESUME: /* fall through */ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 374 | case SNDRV_PCM_TRIGGER_START: |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 375 | atomic_set(&dev->stream_started, 1); |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 376 | break; |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 377 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */ |
| 378 | case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 379 | case SNDRV_PCM_TRIGGER_STOP: |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 380 | atomic_set(&dev->stream_started, 0); |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 381 | break; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 382 | default: |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 383 | retval = -EINVAL; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 384 | } |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 385 | schedule_work(&dev->wq_trigger); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 386 | return retval; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 387 | } |
| 388 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 389 | static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream |
| 390 | *substream) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 391 | { |
Nicola Soranzo | a1a6ee7 | 2009-02-10 23:28:24 -0300 | [diff] [blame] | 392 | unsigned long flags; |
Robert Krakora | 53d12e5 | 2009-01-16 11:23:25 -0300 | [diff] [blame] | 393 | struct em28xx *dev; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 394 | snd_pcm_uframes_t hwptr_done; |
Robert Krakora | 53d12e5 | 2009-01-16 11:23:25 -0300 | [diff] [blame] | 395 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 396 | dev = snd_pcm_substream_chip(substream); |
Douglas Schilling Landgraf | 00bc064 | 2009-01-25 15:12:29 -0300 | [diff] [blame] | 397 | spin_lock_irqsave(&dev->adev.slock, flags); |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 398 | hwptr_done = dev->adev.hwptr_done_capture; |
Douglas Schilling Landgraf | 00bc064 | 2009-01-25 15:12:29 -0300 | [diff] [blame] | 399 | spin_unlock_irqrestore(&dev->adev.slock, flags); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 400 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 401 | return hwptr_done; |
| 402 | } |
| 403 | |
| 404 | static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs, |
| 405 | unsigned long offset) |
| 406 | { |
| 407 | void *pageptr = subs->runtime->dma_area + offset; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 408 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 409 | return vmalloc_to_page(pageptr); |
| 410 | } |
| 411 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 412 | /* |
| 413 | * AC97 volume control support |
| 414 | */ |
| 415 | static int em28xx_vol_info(struct snd_kcontrol *kcontrol, |
| 416 | struct snd_ctl_elem_info *info) |
| 417 | { |
| 418 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 419 | info->count = 2; |
| 420 | info->value.integer.min = 0; |
| 421 | info->value.integer.max = 0x1f; |
| 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 426 | static int em28xx_vol_put(struct snd_kcontrol *kcontrol, |
| 427 | struct snd_ctl_elem_value *value) |
| 428 | { |
| 429 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 430 | u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) | |
| 431 | (0x1f - (value->value.integer.value[1] & 0x1f)) << 8; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 432 | int rc; |
| 433 | |
| 434 | mutex_lock(&dev->lock); |
| 435 | rc = em28xx_read_ac97(dev, kcontrol->private_value); |
| 436 | if (rc < 0) |
| 437 | goto err; |
| 438 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 439 | val |= rc & 0x8000; /* Preserve the mute flag */ |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 440 | |
| 441 | rc = em28xx_write_ac97(dev, kcontrol->private_value, val); |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 442 | if (rc < 0) |
| 443 | goto err; |
| 444 | |
| 445 | dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n", |
| 446 | (val & 0x8000) ? "muted " : "", |
| 447 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 448 | val, (int)kcontrol->private_value); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 449 | |
| 450 | err: |
| 451 | mutex_unlock(&dev->lock); |
| 452 | return rc; |
| 453 | } |
| 454 | |
| 455 | static int em28xx_vol_get(struct snd_kcontrol *kcontrol, |
| 456 | struct snd_ctl_elem_value *value) |
| 457 | { |
| 458 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
| 459 | int val; |
| 460 | |
| 461 | mutex_lock(&dev->lock); |
| 462 | val = em28xx_read_ac97(dev, kcontrol->private_value); |
| 463 | mutex_unlock(&dev->lock); |
| 464 | if (val < 0) |
| 465 | return val; |
| 466 | |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 467 | dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n", |
| 468 | (val & 0x8000) ? "muted " : "", |
| 469 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 470 | val, (int)kcontrol->private_value); |
| 471 | |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 472 | value->value.integer.value[0] = 0x1f - (val & 0x1f); |
| 473 | value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 478 | static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol, |
| 479 | struct snd_ctl_elem_value *value) |
| 480 | { |
| 481 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
| 482 | u16 val = value->value.integer.value[0]; |
| 483 | int rc; |
| 484 | |
| 485 | mutex_lock(&dev->lock); |
| 486 | rc = em28xx_read_ac97(dev, kcontrol->private_value); |
| 487 | if (rc < 0) |
| 488 | goto err; |
| 489 | |
| 490 | if (val) |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 491 | rc &= 0x1f1f; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 492 | else |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 493 | rc |= 0x8000; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 494 | |
| 495 | rc = em28xx_write_ac97(dev, kcontrol->private_value, rc); |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 496 | if (rc < 0) |
| 497 | goto err; |
| 498 | |
| 499 | dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n", |
| 500 | (val & 0x8000) ? "muted " : "", |
| 501 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 502 | val, (int)kcontrol->private_value); |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 503 | |
| 504 | err: |
| 505 | mutex_unlock(&dev->lock); |
| 506 | return rc; |
| 507 | } |
| 508 | |
| 509 | static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol, |
| 510 | struct snd_ctl_elem_value *value) |
| 511 | { |
| 512 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
| 513 | int val; |
| 514 | |
| 515 | mutex_lock(&dev->lock); |
| 516 | val = em28xx_read_ac97(dev, kcontrol->private_value); |
| 517 | mutex_unlock(&dev->lock); |
| 518 | if (val < 0) |
| 519 | return val; |
| 520 | |
| 521 | if (val & 0x8000) |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 522 | value->value.integer.value[0] = 0; |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 523 | else |
| 524 | value->value.integer.value[0] = 1; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 525 | |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 526 | dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n", |
| 527 | (val & 0x8000) ? "muted " : "", |
| 528 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 529 | val, (int)kcontrol->private_value); |
| 530 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 534 | static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0); |
| 535 | |
| 536 | static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev, |
| 537 | char *name, int id) |
| 538 | { |
| 539 | int err; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 540 | char ctl_name[44]; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 541 | struct snd_kcontrol *kctl; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 542 | struct snd_kcontrol_new tmp; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 543 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 544 | memset (&tmp, 0, sizeof(tmp)); |
| 545 | tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 546 | tmp.private_value = id, |
| 547 | tmp.name = ctl_name, |
| 548 | |
| 549 | /* Add Mute Control */ |
| 550 | sprintf(ctl_name, "%s Switch", name); |
| 551 | tmp.get = em28xx_vol_get_mute; |
| 552 | tmp.put = em28xx_vol_put_mute; |
| 553 | tmp.info = snd_ctl_boolean_mono_info; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 554 | kctl = snd_ctl_new1(&tmp, dev); |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 555 | err = snd_ctl_add(card, kctl); |
| 556 | if (err < 0) |
| 557 | return err; |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 558 | dprintk("Added control %s for ac97 volume control 0x%04x\n", |
| 559 | ctl_name, id); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 560 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 561 | memset (&tmp, 0, sizeof(tmp)); |
| 562 | tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 563 | tmp.private_value = id, |
| 564 | tmp.name = ctl_name, |
| 565 | |
| 566 | /* Add Volume Control */ |
| 567 | sprintf(ctl_name, "%s Volume", name); |
| 568 | tmp.get = em28xx_vol_get; |
| 569 | tmp.put = em28xx_vol_put; |
| 570 | tmp.info = em28xx_vol_info; |
| 571 | tmp.tlv.p = em28xx_db_scale, |
| 572 | kctl = snd_ctl_new1(&tmp, dev); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 573 | err = snd_ctl_add(card, kctl); |
| 574 | if (err < 0) |
| 575 | return err; |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 576 | dprintk("Added control %s for ac97 volume control 0x%04x\n", |
| 577 | ctl_name, id); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 582 | /* |
| 583 | * register/unregister code and data |
| 584 | */ |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 585 | static struct snd_pcm_ops snd_em28xx_pcm_capture = { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 586 | .open = snd_em28xx_capture_open, |
| 587 | .close = snd_em28xx_pcm_close, |
| 588 | .ioctl = snd_pcm_lib_ioctl, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 589 | .hw_params = snd_em28xx_hw_capture_params, |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 590 | .hw_free = snd_em28xx_hw_capture_free, |
| 591 | .prepare = snd_em28xx_prepare, |
| 592 | .trigger = snd_em28xx_capture_trigger, |
| 593 | .pointer = snd_em28xx_capture_pointer, |
| 594 | .page = snd_pcm_get_vmalloc_page, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 595 | }; |
| 596 | |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 597 | static void em28xx_audio_free_urb(struct em28xx *dev) |
| 598 | { |
| 599 | int i; |
| 600 | |
| 601 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
| 602 | struct urb *urb = dev->adev.urb[i]; |
| 603 | |
| 604 | if (!dev->adev.urb[i]) |
| 605 | continue; |
| 606 | |
| 607 | usb_free_coherent(dev->udev, |
| 608 | urb->transfer_buffer_length, |
| 609 | dev->adev.transfer_buffer[i], |
| 610 | urb->transfer_dma); |
| 611 | |
| 612 | usb_free_urb(urb); |
| 613 | dev->adev.urb[i] = NULL; |
| 614 | dev->adev.transfer_buffer[i] = NULL; |
| 615 | } |
| 616 | } |
| 617 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 618 | static int em28xx_audio_init(struct em28xx *dev) |
| 619 | { |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 620 | struct em28xx_audio *adev = &dev->adev; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 621 | struct snd_pcm *pcm; |
| 622 | struct snd_card *card; |
Mauro Carvalho Chehab | d2849fa | 2014-01-10 05:43:09 -0300 | [diff] [blame^] | 623 | struct usb_interface *intf; |
| 624 | struct usb_endpoint_descriptor *e, *ep = NULL; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 625 | static int devnr; |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 626 | int err, i; |
| 627 | const int sb_size = EM28XX_NUM_AUDIO_PACKETS * |
| 628 | EM28XX_AUDIO_MAX_PACKET_SIZE; |
Mauro Carvalho Chehab | d2849fa | 2014-01-10 05:43:09 -0300 | [diff] [blame^] | 629 | u8 alt; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 630 | |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame] | 631 | if (!dev->has_alsa_audio || dev->audio_ifnum < 0) { |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 632 | /* This device does not support the extension (in this case |
Devin Heitmueller | 24a613e | 2008-11-12 02:05:19 -0300 | [diff] [blame] | 633 | the device is expecting the snd-usb-audio module or |
| 634 | doesn't have analog audio support at all) */ |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 635 | return 0; |
| 636 | } |
| 637 | |
Mauro Carvalho Chehab | 9634614 | 2013-12-26 12:41:03 -0300 | [diff] [blame] | 638 | em28xx_info("Binding audio extension\n"); |
| 639 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 640 | printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus " |
| 641 | "Rechberger\n"); |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 642 | printk(KERN_INFO |
| 643 | "em28xx-audio.c: Copyright (C) 2007-2014 Mauro Carvalho Chehab\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 644 | |
Takashi Iwai | 758021b | 2009-01-12 15:17:09 +0100 | [diff] [blame] | 645 | err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0, |
| 646 | &card); |
| 647 | if (err < 0) |
| 648 | return err; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 649 | |
| 650 | spin_lock_init(&adev->slock); |
Mauro Carvalho Chehab | 1f6340b | 2008-11-07 14:24:18 -0300 | [diff] [blame] | 651 | err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm); |
| 652 | if (err < 0) { |
| 653 | snd_card_free(card); |
| 654 | return err; |
| 655 | } |
| 656 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 657 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture); |
| 658 | pcm->info_flags = 0; |
| 659 | pcm->private_data = dev; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 660 | strcpy(pcm->name, "Empia 28xx Capture"); |
Nicola Soranzo | 7662b00 | 2009-02-19 13:41:56 -0300 | [diff] [blame] | 661 | |
| 662 | snd_card_set_dev(card, &dev->udev->dev); |
Dan Carpenter | 4a3eaee | 2010-03-23 08:40:43 -0300 | [diff] [blame] | 663 | strcpy(card->driver, "Em28xx-Audio"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 664 | strcpy(card->shortname, "Em28xx Audio"); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 665 | strcpy(card->longname, "Empia Em28xx Audio"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 666 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 667 | INIT_WORK(&dev->wq_trigger, audio_trigger); |
| 668 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 669 | if (dev->audio_mode.ac97 != EM28XX_NO_AC97) { |
Ezequiel GarcĂa | 67b8d03 | 2012-06-11 15:17:23 -0300 | [diff] [blame] | 670 | em28xx_cvol_new(card, dev, "Video", AC97_VIDEO); |
| 671 | em28xx_cvol_new(card, dev, "Line In", AC97_LINE); |
| 672 | em28xx_cvol_new(card, dev, "Phone", AC97_PHONE); |
| 673 | em28xx_cvol_new(card, dev, "Microphone", AC97_MIC); |
| 674 | em28xx_cvol_new(card, dev, "CD", AC97_CD); |
| 675 | em28xx_cvol_new(card, dev, "AUX", AC97_AUX); |
| 676 | em28xx_cvol_new(card, dev, "PCM", AC97_PCM); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 677 | |
Ezequiel GarcĂa | 67b8d03 | 2012-06-11 15:17:23 -0300 | [diff] [blame] | 678 | em28xx_cvol_new(card, dev, "Master", AC97_MASTER); |
| 679 | em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE); |
| 680 | em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO); |
| 681 | em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER); |
| 682 | em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 683 | } |
| 684 | |
Mauro Carvalho Chehab | d2849fa | 2014-01-10 05:43:09 -0300 | [diff] [blame^] | 685 | if (dev->audio_ifnum) |
| 686 | alt = 1; |
| 687 | else |
| 688 | alt = 7; |
| 689 | |
| 690 | intf = usb_ifnum_to_if(dev->udev, dev->audio_ifnum); |
| 691 | |
| 692 | if (intf->num_altsetting <= alt) { |
| 693 | em28xx_errdev("alt %d doesn't exist on interface %d\n", |
| 694 | dev->audio_ifnum, alt); |
| 695 | return -ENODEV; |
| 696 | } |
| 697 | |
| 698 | for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) { |
| 699 | e = &intf->altsetting[alt].endpoint[i].desc; |
| 700 | if (!usb_endpoint_dir_in(e)) |
| 701 | continue; |
| 702 | if (e->bEndpointAddress == EM28XX_EP_AUDIO) { |
| 703 | ep = e; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | if (!ep) { |
| 709 | em28xx_errdev("Couldn't find an audio endpoint"); |
| 710 | return -ENODEV; |
| 711 | } |
| 712 | |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 713 | /* Alloc URB and transfer buffers */ |
| 714 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
| 715 | struct urb *urb; |
| 716 | int j, k; |
| 717 | void *buf; |
| 718 | |
| 719 | urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC); |
| 720 | if (!urb) { |
| 721 | em28xx_errdev("usb_alloc_urb failed!\n"); |
| 722 | em28xx_audio_free_urb(dev); |
| 723 | return -ENOMEM; |
| 724 | } |
| 725 | dev->adev.urb[i] = urb; |
| 726 | |
| 727 | buf = usb_alloc_coherent(dev->udev, sb_size, GFP_ATOMIC, |
| 728 | &urb->transfer_dma); |
| 729 | if (!buf) { |
| 730 | em28xx_errdev("usb_alloc_coherent failed!\n"); |
| 731 | em28xx_audio_free_urb(dev); |
| 732 | return -ENOMEM; |
| 733 | } |
| 734 | dev->adev.transfer_buffer[i] = buf; |
| 735 | |
| 736 | urb->dev = dev->udev; |
| 737 | urb->context = dev; |
| 738 | urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO); |
| 739 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
| 740 | urb->transfer_buffer = dev->adev.transfer_buffer[i]; |
Mauro Carvalho Chehab | d2849fa | 2014-01-10 05:43:09 -0300 | [diff] [blame^] | 741 | urb->interval = 1 << (ep->bInterval - 1); |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 742 | urb->complete = em28xx_audio_isocirq; |
| 743 | urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS; |
| 744 | urb->transfer_buffer_length = sb_size; |
| 745 | |
Mauro Carvalho Chehab | d2849fa | 2014-01-10 05:43:09 -0300 | [diff] [blame^] | 746 | if (!i) |
| 747 | dprintk("Will use ep 0x%02x on intf %d alt %d interval = %d (rcv isoc pipe: 0x%08x)\n", |
| 748 | EM28XX_EP_AUDIO, dev->audio_ifnum, alt, |
| 749 | urb->interval, |
| 750 | urb->pipe); |
| 751 | |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 752 | for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS; |
| 753 | j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) { |
| 754 | urb->iso_frame_desc[j].offset = k; |
| 755 | urb->iso_frame_desc[j].length = |
| 756 | EM28XX_AUDIO_MAX_PACKET_SIZE; |
| 757 | } |
| 758 | } |
| 759 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 760 | err = snd_card_register(card); |
| 761 | if (err < 0) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 762 | snd_card_free(card); |
Mauro Carvalho Chehab | 1f6340b | 2008-11-07 14:24:18 -0300 | [diff] [blame] | 763 | return err; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 764 | } |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 765 | adev->sndcard = card; |
| 766 | adev->udev = dev->udev; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 767 | |
Mauro Carvalho Chehab | 9634614 | 2013-12-26 12:41:03 -0300 | [diff] [blame] | 768 | em28xx_info("Audio extension successfully initialized\n"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | static int em28xx_audio_fini(struct em28xx *dev) |
| 773 | { |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 774 | if (dev == NULL) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 775 | return 0; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 776 | |
Devin Heitmueller | 24a613e | 2008-11-12 02:05:19 -0300 | [diff] [blame] | 777 | if (dev->has_alsa_audio != 1) { |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 778 | /* This device does not support the extension (in this case |
Devin Heitmueller | 24a613e | 2008-11-12 02:05:19 -0300 | [diff] [blame] | 779 | the device is expecting the snd-usb-audio module or |
| 780 | doesn't have analog audio support at all) */ |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 784 | em28xx_audio_free_urb(dev); |
| 785 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 786 | if (dev->adev.sndcard) { |
| 787 | snd_card_free(dev->adev.sndcard); |
| 788 | dev->adev.sndcard = NULL; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 789 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 790 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static struct em28xx_ops audio_ops = { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 795 | .id = EM28XX_AUDIO, |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 796 | .name = "Em28xx Audio Extension", |
| 797 | .init = em28xx_audio_init, |
| 798 | .fini = em28xx_audio_fini, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 799 | }; |
| 800 | |
| 801 | static int __init em28xx_alsa_register(void) |
| 802 | { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 803 | return em28xx_register_extension(&audio_ops); |
| 804 | } |
| 805 | |
| 806 | static void __exit em28xx_alsa_unregister(void) |
| 807 | { |
| 808 | em28xx_unregister_extension(&audio_ops); |
| 809 | } |
| 810 | |
| 811 | MODULE_LICENSE("GPL"); |
| 812 | MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>"); |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame] | 813 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
Mauro Carvalho Chehab | d8992b0 | 2013-12-27 11:14:59 -0300 | [diff] [blame] | 814 | MODULE_DESCRIPTION(DRIVER_DESC " - audio interface"); |
| 815 | MODULE_VERSION(EM28XX_VERSION); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 816 | |
| 817 | module_init(em28xx_alsa_register); |
| 818 | module_exit(em28xx_alsa_unregister); |