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 | |
Mauro Carvalho Chehab | 1b3fd2d | 2014-01-10 05:53:24 -0300 | [diff] [blame] | 53 | #define EM28XX_MAX_AUDIO_BUFS 5 |
| 54 | #define EM28XX_MIN_AUDIO_PACKETS 64 |
Mauro Carvalho Chehab | 49677ae | 2014-01-10 13:29:16 -0300 | [diff] [blame] | 55 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 56 | #define dprintk(fmt, arg...) do { \ |
| 57 | if (debug) \ |
| 58 | printk(KERN_INFO "em28xx-audio %s: " fmt, \ |
Harvey Harrison | d80e134 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 59 | __func__, ##arg); \ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 60 | } while (0) |
| 61 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 62 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 63 | |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 64 | static int em28xx_deinit_isoc_audio(struct em28xx *dev) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 65 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 66 | int i; |
| 67 | |
| 68 | dprintk("Stopping isoc\n"); |
Mauro Carvalho Chehab | 1b3fd2d | 2014-01-10 05:53:24 -0300 | [diff] [blame] | 69 | for (i = 0; i < dev->adev.num_urb; i++) { |
Mauro Carvalho Chehab | 46c704d | 2013-12-28 11:47:16 -0300 | [diff] [blame] | 70 | struct urb *urb = dev->adev.urb[i]; |
| 71 | |
Robert Krakora | 9c06210 | 2009-01-25 13:08:07 -0300 | [diff] [blame] | 72 | if (!irqs_disabled()) |
Mauro Carvalho Chehab | 46c704d | 2013-12-28 11:47:16 -0300 | [diff] [blame] | 73 | usb_kill_urb(urb); |
Robert Krakora | 9c06210 | 2009-01-25 13:08:07 -0300 | [diff] [blame] | 74 | else |
Mauro Carvalho Chehab | 46c704d | 2013-12-28 11:47:16 -0300 | [diff] [blame] | 75 | usb_unlink_urb(urb); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 76 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 77 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 81 | static void em28xx_audio_isocirq(struct urb *urb) |
| 82 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 83 | struct em28xx *dev = urb->context; |
| 84 | int i; |
| 85 | unsigned int oldptr; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 86 | int period_elapsed = 0; |
| 87 | int status; |
| 88 | unsigned char *cp; |
| 89 | unsigned int stride; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 90 | struct snd_pcm_substream *substream; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 91 | struct snd_pcm_runtime *runtime; |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 92 | |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 93 | if (dev->disconnected) { |
| 94 | dprintk("device disconnected while streaming. URB status=%d.\n", urb->status); |
| 95 | atomic_set(&dev->stream_started, 0); |
| 96 | return; |
| 97 | } |
| 98 | |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 99 | switch (urb->status) { |
| 100 | case 0: /* success */ |
| 101 | case -ETIMEDOUT: /* NAK */ |
| 102 | break; |
| 103 | case -ECONNRESET: /* kill */ |
| 104 | case -ENOENT: |
| 105 | case -ESHUTDOWN: |
| 106 | return; |
| 107 | default: /* error */ |
| 108 | dprintk("urb completition error %d.\n", urb->status); |
| 109 | break; |
| 110 | } |
| 111 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 112 | if (atomic_read(&dev->stream_started) == 0) |
| 113 | return; |
| 114 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 115 | if (dev->adev.capture_pcm_substream) { |
| 116 | substream = dev->adev.capture_pcm_substream; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 117 | runtime = substream->runtime; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 118 | stride = runtime->frame_bits >> 3; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 119 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 120 | for (i = 0; i < urb->number_of_packets; i++) { |
| 121 | int length = |
| 122 | urb->iso_frame_desc[i].actual_length / stride; |
| 123 | cp = (unsigned char *)urb->transfer_buffer + |
| 124 | urb->iso_frame_desc[i].offset; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 125 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 126 | if (!length) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 127 | continue; |
| 128 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 129 | oldptr = dev->adev.hwptr_done_capture; |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 130 | if (oldptr + length >= runtime->buffer_size) { |
| 131 | unsigned int cnt = |
| 132 | runtime->buffer_size - oldptr; |
| 133 | memcpy(runtime->dma_area + oldptr * stride, cp, |
| 134 | cnt * stride); |
| 135 | memcpy(runtime->dma_area, cp + cnt * stride, |
| 136 | length * stride - cnt * stride); |
| 137 | } else { |
| 138 | memcpy(runtime->dma_area + oldptr * stride, cp, |
| 139 | length * stride); |
| 140 | } |
| 141 | |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 142 | snd_pcm_stream_lock(substream); |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 143 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 144 | dev->adev.hwptr_done_capture += length; |
| 145 | if (dev->adev.hwptr_done_capture >= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 146 | runtime->buffer_size) |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 147 | dev->adev.hwptr_done_capture -= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 148 | runtime->buffer_size; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 149 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 150 | dev->adev.capture_transfer_done += length; |
| 151 | if (dev->adev.capture_transfer_done >= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 152 | runtime->period_size) { |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 153 | dev->adev.capture_transfer_done -= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 154 | runtime->period_size; |
| 155 | period_elapsed = 1; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 156 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 157 | |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 158 | snd_pcm_stream_unlock(substream); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 159 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 160 | if (period_elapsed) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 161 | snd_pcm_period_elapsed(substream); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 162 | } |
| 163 | urb->status = 0; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 164 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 165 | status = usb_submit_urb(urb, GFP_ATOMIC); |
Mauro Carvalho Chehab | bf6e8aa | 2014-01-14 14:34:13 -0300 | [diff] [blame] | 166 | if (status < 0) |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 167 | em28xx_errdev("resubmit of audio urb failed (error=%i)\n", |
| 168 | status); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 172 | static int em28xx_init_audio_isoc(struct em28xx *dev) |
| 173 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 174 | int i, errCode; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 175 | |
| 176 | dprintk("Starting isoc transfers\n"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 177 | |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 178 | /* Start streaming */ |
Mauro Carvalho Chehab | 1b3fd2d | 2014-01-10 05:53:24 -0300 | [diff] [blame] | 179 | for (i = 0; i < dev->adev.num_urb; i++) { |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 180 | memset(dev->adev.transfer_buffer[i], 0x80, |
| 181 | dev->adev.urb[i]->transfer_buffer_length); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 182 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 183 | errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 184 | if (errCode) { |
Mauro Carvalho Chehab | bf6e8aa | 2014-01-14 14:34:13 -0300 | [diff] [blame] | 185 | em28xx_errdev("submit of audio urb failed (error=%i)\n", |
| 186 | errCode); |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 187 | em28xx_deinit_isoc_audio(dev); |
Mauro Carvalho Chehab | debb724 | 2011-06-19 10:15:35 -0300 | [diff] [blame] | 188 | atomic_set(&dev->stream_started, 0); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 189 | return errCode; |
| 190 | } |
Mauro Carvalho Chehab | debb724 | 2011-06-19 10:15:35 -0300 | [diff] [blame] | 191 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 192 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 193 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 197 | static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, |
| 198 | size_t size) |
| 199 | { |
| 200 | struct snd_pcm_runtime *runtime = subs->runtime; |
| 201 | |
Nicola Soranzo | a1a6ee7 | 2009-02-10 23:28:24 -0300 | [diff] [blame] | 202 | dprintk("Allocating vbuffer\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 203 | if (runtime->dma_area) { |
| 204 | if (runtime->dma_bytes > size) |
| 205 | return 0; |
| 206 | |
| 207 | vfree(runtime->dma_area); |
| 208 | } |
| 209 | runtime->dma_area = vmalloc(size); |
| 210 | if (!runtime->dma_area) |
| 211 | return -ENOMEM; |
| 212 | |
| 213 | runtime->dma_bytes = size; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static struct snd_pcm_hardware snd_em28xx_hw_capture = { |
| 219 | .info = SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 220 | SNDRV_PCM_INFO_MMAP | |
| 221 | SNDRV_PCM_INFO_INTERLEAVED | |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 222 | SNDRV_PCM_INFO_BATCH | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 223 | SNDRV_PCM_INFO_MMAP_VALID, |
| 224 | |
| 225 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 226 | |
Mauro Carvalho Chehab | 49677ae | 2014-01-10 13:29:16 -0300 | [diff] [blame] | 227 | .rates = SNDRV_PCM_RATE_48000, |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 228 | |
| 229 | .rate_min = 48000, |
| 230 | .rate_max = 48000, |
| 231 | .channels_min = 2, |
| 232 | .channels_max = 2, |
| 233 | .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */ |
Mauro Carvalho Chehab | 49677ae | 2014-01-10 13:29:16 -0300 | [diff] [blame] | 234 | |
| 235 | |
| 236 | /* |
| 237 | * The period is 12.288 bytes. Allow a 10% of variation along its |
| 238 | * value, in order to avoid overruns/underruns due to some clock |
| 239 | * drift. |
| 240 | * |
| 241 | * FIXME: This period assumes 64 packets, and a 48000 PCM rate. |
| 242 | * Calculate it dynamically. |
| 243 | */ |
| 244 | .period_bytes_min = 11059, |
| 245 | .period_bytes_max = 13516, |
| 246 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 247 | .periods_min = 2, |
| 248 | .periods_max = 98, /* 12544, */ |
| 249 | }; |
| 250 | |
| 251 | static int snd_em28xx_capture_open(struct snd_pcm_substream *substream) |
| 252 | { |
| 253 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 254 | struct snd_pcm_runtime *runtime = substream->runtime; |
Frank Schaefer | 01ae3b5 | 2014-01-17 14:18:42 -0300 | [diff] [blame] | 255 | int nonblock, ret = 0; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 256 | |
Mauro Carvalho Chehab | 83ee87a | 2008-06-14 09:41:18 -0300 | [diff] [blame] | 257 | if (!dev) { |
Filipe Rosset | 0bc2308 | 2009-11-04 15:31:25 -0300 | [diff] [blame] | 258 | em28xx_err("BUG: em28xx can't find device struct." |
Mauro Carvalho Chehab | 83ee87a | 2008-06-14 09:41:18 -0300 | [diff] [blame] | 259 | " Can't proceed with open\n"); |
| 260 | return -ENODEV; |
| 261 | } |
| 262 | |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 263 | if (dev->disconnected) |
| 264 | return -ENODEV; |
| 265 | |
| 266 | dprintk("opening device and trying to acquire exclusive lock\n"); |
| 267 | |
Frank Schaefer | 01ae3b5 | 2014-01-17 14:18:42 -0300 | [diff] [blame] | 268 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
| 269 | if (nonblock) { |
| 270 | if (!mutex_trylock(&dev->lock)) |
| 271 | return -EAGAIN; |
| 272 | } else |
| 273 | mutex_lock(&dev->lock); |
| 274 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 275 | runtime->hw = snd_em28xx_hw_capture; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 276 | |
Frank Schaefer | ba35ca0 | 2014-01-17 14:18:43 -0300 | [diff] [blame^] | 277 | if (dev->adev.users == 0) { |
| 278 | if (dev->alt == 0 || dev->is_audio_only) { |
| 279 | if (dev->is_audio_only) |
| 280 | /* audio is on a separate interface */ |
| 281 | dev->alt = 1; |
| 282 | else |
| 283 | /* audio is on the same interface as video */ |
| 284 | dev->alt = 7; |
| 285 | /* |
| 286 | * FIXME: The intention seems to be to select |
| 287 | * the alt setting with the largest |
| 288 | * wMaxPacketSize for the video endpoint. |
| 289 | * At least dev->alt should be used instead, but |
| 290 | * we should probably not touch it at all if it |
| 291 | * is already >0, because wMaxPacketSize of the |
| 292 | * audio endpoints seems to be the same for all. |
| 293 | */ |
| 294 | dprintk("changing alternate number on interface %d to %d\n", |
| 295 | dev->ifnum, dev->alt); |
| 296 | usb_set_interface(dev->udev, dev->ifnum, dev->alt); |
| 297 | } |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame] | 298 | |
| 299 | /* Sets volume, mute, etc */ |
| 300 | dev->mute = 0; |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame] | 301 | ret = em28xx_audio_analog_set(dev); |
| 302 | if (ret < 0) |
| 303 | goto err; |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame] | 304 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 305 | |
Mauro Carvalho Chehab | 47677e5 | 2014-03-05 11:21:07 -0300 | [diff] [blame] | 306 | kref_get(&dev->ref); |
Frank Schaefer | 01ae3b5 | 2014-01-17 14:18:42 -0300 | [diff] [blame] | 307 | dev->adev.users++; |
| 308 | mutex_unlock(&dev->lock); |
| 309 | |
Mauro Carvalho Chehab | a02b9c2 | 2014-01-13 03:34:23 -0300 | [diff] [blame] | 310 | /* Dynamically adjust the period size */ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 311 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
Mauro Carvalho Chehab | a02b9c2 | 2014-01-13 03:34:23 -0300 | [diff] [blame] | 312 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, |
| 313 | dev->adev.period * 95 / 100, |
| 314 | dev->adev.period * 105 / 100); |
| 315 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 316 | dev->adev.capture_pcm_substream = substream; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 317 | |
| 318 | return 0; |
| 319 | err: |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 320 | mutex_unlock(&dev->lock); |
| 321 | |
Filipe Rosset | 0bc2308 | 2009-11-04 15:31:25 -0300 | [diff] [blame] | 322 | em28xx_err("Error while configuring em28xx mixer\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream) |
| 327 | { |
| 328 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 329 | |
| 330 | dprintk("closing device\n"); |
| 331 | |
| 332 | dev->mute = 1; |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 333 | mutex_lock(&dev->lock); |
Douglas Schilling Landgraf | bf510ac3 | 2009-02-08 01:11:13 -0300 | [diff] [blame] | 334 | dev->adev.users--; |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 335 | if (atomic_read(&dev->stream_started) > 0) { |
| 336 | atomic_set(&dev->stream_started, 0); |
| 337 | schedule_work(&dev->wq_trigger); |
| 338 | } |
| 339 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 340 | em28xx_audio_analog_set(dev); |
Robert Krakora | 75c74d1 | 2009-05-28 11:16:19 -0300 | [diff] [blame] | 341 | if (substream->runtime->dma_area) { |
| 342 | dprintk("freeing\n"); |
| 343 | vfree(substream->runtime->dma_area); |
| 344 | substream->runtime->dma_area = NULL; |
| 345 | } |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 346 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 47677e5 | 2014-03-05 11:21:07 -0300 | [diff] [blame] | 347 | kref_put(&dev->ref, em28xx_free_device); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 348 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream, |
| 353 | struct snd_pcm_hw_params *hw_params) |
| 354 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 355 | int ret; |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 356 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 357 | |
| 358 | if (dev->disconnected) |
| 359 | return -ENODEV; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 360 | |
| 361 | dprintk("Setting capture parameters\n"); |
| 362 | |
| 363 | ret = snd_pcm_alloc_vmalloc_buffer(substream, |
| 364 | params_buffer_bytes(hw_params)); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 365 | if (ret < 0) |
| 366 | return ret; |
Hans Verkuil | e92ba28 | 2012-05-14 10:17:35 -0300 | [diff] [blame] | 367 | #if 0 |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 368 | /* TODO: set up em28xx audio chip to deliver the correct audio format, |
| 369 | current default is 48000hz multiplexed => 96000hz mono |
| 370 | which shouldn't matter since analogue TV only supports mono */ |
Hans Verkuil | e92ba28 | 2012-05-14 10:17:35 -0300 | [diff] [blame] | 371 | unsigned int channels, rate, format; |
| 372 | |
| 373 | format = params_format(hw_params); |
| 374 | rate = params_rate(hw_params); |
| 375 | channels = params_channels(hw_params); |
| 376 | #endif |
| 377 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream) |
| 382 | { |
| 383 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 384 | |
| 385 | dprintk("Stop capture, if needed\n"); |
| 386 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 387 | if (atomic_read(&dev->stream_started) > 0) { |
| 388 | atomic_set(&dev->stream_started, 0); |
| 389 | schedule_work(&dev->wq_trigger); |
| 390 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static int snd_em28xx_prepare(struct snd_pcm_substream *substream) |
| 396 | { |
Devin Heitmueller | 96fbf77 | 2009-10-15 01:14:34 -0300 | [diff] [blame] | 397 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 398 | |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 399 | if (dev->disconnected) |
| 400 | return -ENODEV; |
| 401 | |
Devin Heitmueller | 96fbf77 | 2009-10-15 01:14:34 -0300 | [diff] [blame] | 402 | dev->adev.hwptr_done_capture = 0; |
| 403 | dev->adev.capture_transfer_done = 0; |
| 404 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 405 | return 0; |
| 406 | } |
| 407 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 408 | static void audio_trigger(struct work_struct *work) |
| 409 | { |
| 410 | struct em28xx *dev = container_of(work, struct em28xx, wq_trigger); |
| 411 | |
| 412 | if (atomic_read(&dev->stream_started)) { |
| 413 | dprintk("starting capture"); |
| 414 | em28xx_init_audio_isoc(dev); |
| 415 | } else { |
| 416 | dprintk("stopping capture"); |
| 417 | em28xx_deinit_isoc_audio(dev); |
| 418 | } |
| 419 | } |
| 420 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 421 | static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream, |
| 422 | int cmd) |
| 423 | { |
| 424 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 425 | int retval = 0; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 426 | |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 427 | if (dev->disconnected) |
| 428 | return -ENODEV; |
| 429 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 430 | switch (cmd) { |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 431 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */ |
| 432 | case SNDRV_PCM_TRIGGER_RESUME: /* fall through */ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 433 | case SNDRV_PCM_TRIGGER_START: |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 434 | atomic_set(&dev->stream_started, 1); |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 435 | break; |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 436 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */ |
| 437 | case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 438 | case SNDRV_PCM_TRIGGER_STOP: |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 439 | atomic_set(&dev->stream_started, 0); |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 440 | break; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 441 | default: |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 442 | retval = -EINVAL; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 443 | } |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 444 | schedule_work(&dev->wq_trigger); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 445 | return retval; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 446 | } |
| 447 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 448 | static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream |
| 449 | *substream) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 450 | { |
Nicola Soranzo | a1a6ee7 | 2009-02-10 23:28:24 -0300 | [diff] [blame] | 451 | unsigned long flags; |
Robert Krakora | 53d12e5 | 2009-01-16 11:23:25 -0300 | [diff] [blame] | 452 | struct em28xx *dev; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 453 | snd_pcm_uframes_t hwptr_done; |
Robert Krakora | 53d12e5 | 2009-01-16 11:23:25 -0300 | [diff] [blame] | 454 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 455 | dev = snd_pcm_substream_chip(substream); |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 456 | if (dev->disconnected) |
Mauro Carvalho Chehab | 229295f | 2014-01-12 12:57:08 -0300 | [diff] [blame] | 457 | return SNDRV_PCM_POS_XRUN; |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 458 | |
Douglas Schilling Landgraf | 00bc064 | 2009-01-25 15:12:29 -0300 | [diff] [blame] | 459 | spin_lock_irqsave(&dev->adev.slock, flags); |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 460 | hwptr_done = dev->adev.hwptr_done_capture; |
Douglas Schilling Landgraf | 00bc064 | 2009-01-25 15:12:29 -0300 | [diff] [blame] | 461 | spin_unlock_irqrestore(&dev->adev.slock, flags); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 462 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 463 | return hwptr_done; |
| 464 | } |
| 465 | |
| 466 | static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs, |
| 467 | unsigned long offset) |
| 468 | { |
| 469 | void *pageptr = subs->runtime->dma_area + offset; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 470 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 471 | return vmalloc_to_page(pageptr); |
| 472 | } |
| 473 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 474 | /* |
| 475 | * AC97 volume control support |
| 476 | */ |
| 477 | static int em28xx_vol_info(struct snd_kcontrol *kcontrol, |
| 478 | struct snd_ctl_elem_info *info) |
| 479 | { |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 480 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
| 481 | |
| 482 | if (dev->disconnected) |
| 483 | return -ENODEV; |
| 484 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 485 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 486 | info->count = 2; |
| 487 | info->value.integer.min = 0; |
| 488 | info->value.integer.max = 0x1f; |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 493 | static int em28xx_vol_put(struct snd_kcontrol *kcontrol, |
| 494 | struct snd_ctl_elem_value *value) |
| 495 | { |
| 496 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 497 | struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream; |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 498 | u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) | |
| 499 | (0x1f - (value->value.integer.value[1] & 0x1f)) << 8; |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 500 | int nonblock = 0; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 501 | int rc; |
| 502 | |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 503 | if (dev->disconnected) |
| 504 | return -ENODEV; |
| 505 | |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 506 | if (substream) |
| 507 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
| 508 | if (nonblock) { |
| 509 | if (!mutex_trylock(&dev->lock)) |
| 510 | return -EAGAIN; |
| 511 | } else |
| 512 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 513 | rc = em28xx_read_ac97(dev, kcontrol->private_value); |
| 514 | if (rc < 0) |
| 515 | goto err; |
| 516 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 517 | val |= rc & 0x8000; /* Preserve the mute flag */ |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 518 | |
| 519 | rc = em28xx_write_ac97(dev, kcontrol->private_value, val); |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 520 | if (rc < 0) |
| 521 | goto err; |
| 522 | |
| 523 | dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n", |
| 524 | (val & 0x8000) ? "muted " : "", |
| 525 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 526 | val, (int)kcontrol->private_value); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 527 | |
| 528 | err: |
| 529 | mutex_unlock(&dev->lock); |
| 530 | return rc; |
| 531 | } |
| 532 | |
| 533 | static int em28xx_vol_get(struct snd_kcontrol *kcontrol, |
| 534 | struct snd_ctl_elem_value *value) |
| 535 | { |
| 536 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 537 | struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream; |
| 538 | int nonblock = 0; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 539 | int val; |
| 540 | |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 541 | if (dev->disconnected) |
| 542 | return -ENODEV; |
| 543 | |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 544 | if (substream) |
| 545 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
| 546 | if (nonblock) { |
| 547 | if (!mutex_trylock(&dev->lock)) |
| 548 | return -EAGAIN; |
| 549 | } else |
| 550 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 551 | val = em28xx_read_ac97(dev, kcontrol->private_value); |
| 552 | mutex_unlock(&dev->lock); |
| 553 | if (val < 0) |
| 554 | return val; |
| 555 | |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 556 | dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n", |
| 557 | (val & 0x8000) ? "muted " : "", |
| 558 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 559 | val, (int)kcontrol->private_value); |
| 560 | |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 561 | value->value.integer.value[0] = 0x1f - (val & 0x1f); |
| 562 | value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 563 | |
| 564 | return 0; |
| 565 | } |
| 566 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 567 | static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol, |
| 568 | struct snd_ctl_elem_value *value) |
| 569 | { |
| 570 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
| 571 | u16 val = value->value.integer.value[0]; |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 572 | struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream; |
| 573 | int nonblock = 0; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 574 | int rc; |
| 575 | |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 576 | if (dev->disconnected) |
| 577 | return -ENODEV; |
| 578 | |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 579 | if (substream) |
| 580 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
| 581 | if (nonblock) { |
| 582 | if (!mutex_trylock(&dev->lock)) |
| 583 | return -EAGAIN; |
| 584 | } else |
| 585 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 586 | rc = em28xx_read_ac97(dev, kcontrol->private_value); |
| 587 | if (rc < 0) |
| 588 | goto err; |
| 589 | |
| 590 | if (val) |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 591 | rc &= 0x1f1f; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 592 | else |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 593 | rc |= 0x8000; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 594 | |
| 595 | rc = em28xx_write_ac97(dev, kcontrol->private_value, rc); |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 596 | if (rc < 0) |
| 597 | goto err; |
| 598 | |
| 599 | dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n", |
| 600 | (val & 0x8000) ? "muted " : "", |
| 601 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 602 | val, (int)kcontrol->private_value); |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 603 | |
| 604 | err: |
| 605 | mutex_unlock(&dev->lock); |
| 606 | return rc; |
| 607 | } |
| 608 | |
| 609 | static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol, |
| 610 | struct snd_ctl_elem_value *value) |
| 611 | { |
| 612 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 613 | struct snd_pcm_substream *substream = dev->adev.capture_pcm_substream; |
| 614 | int nonblock = 0; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 615 | int val; |
| 616 | |
Mauro Carvalho Chehab | 8b1fa57 | 2014-01-12 10:21:42 -0300 | [diff] [blame] | 617 | if (dev->disconnected) |
| 618 | return -ENODEV; |
| 619 | |
Mauro Carvalho Chehab | 3490663 | 2014-01-12 09:22:29 -0300 | [diff] [blame] | 620 | if (substream) |
| 621 | nonblock = !!(substream->f_flags & O_NONBLOCK); |
| 622 | if (nonblock) { |
| 623 | if (!mutex_trylock(&dev->lock)) |
| 624 | return -EAGAIN; |
| 625 | } else |
| 626 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 627 | val = em28xx_read_ac97(dev, kcontrol->private_value); |
| 628 | mutex_unlock(&dev->lock); |
| 629 | if (val < 0) |
| 630 | return val; |
| 631 | |
| 632 | if (val & 0x8000) |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 633 | value->value.integer.value[0] = 0; |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 634 | else |
| 635 | value->value.integer.value[0] = 1; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 636 | |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 637 | dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n", |
| 638 | (val & 0x8000) ? "muted " : "", |
| 639 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 640 | val, (int)kcontrol->private_value); |
| 641 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 642 | return 0; |
| 643 | } |
| 644 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 645 | static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0); |
| 646 | |
| 647 | static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev, |
| 648 | char *name, int id) |
| 649 | { |
| 650 | int err; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 651 | char ctl_name[44]; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 652 | struct snd_kcontrol *kctl; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 653 | struct snd_kcontrol_new tmp; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 654 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 655 | memset (&tmp, 0, sizeof(tmp)); |
| 656 | tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 657 | tmp.private_value = id, |
| 658 | tmp.name = ctl_name, |
| 659 | |
| 660 | /* Add Mute Control */ |
| 661 | sprintf(ctl_name, "%s Switch", name); |
| 662 | tmp.get = em28xx_vol_get_mute; |
| 663 | tmp.put = em28xx_vol_put_mute; |
| 664 | tmp.info = snd_ctl_boolean_mono_info; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 665 | kctl = snd_ctl_new1(&tmp, dev); |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 666 | err = snd_ctl_add(card, kctl); |
| 667 | if (err < 0) |
| 668 | return err; |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 669 | dprintk("Added control %s for ac97 volume control 0x%04x\n", |
| 670 | ctl_name, id); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 671 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 672 | memset (&tmp, 0, sizeof(tmp)); |
| 673 | tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 674 | tmp.private_value = id, |
| 675 | tmp.name = ctl_name, |
| 676 | |
| 677 | /* Add Volume Control */ |
| 678 | sprintf(ctl_name, "%s Volume", name); |
| 679 | tmp.get = em28xx_vol_get; |
| 680 | tmp.put = em28xx_vol_put; |
| 681 | tmp.info = em28xx_vol_info; |
| 682 | tmp.tlv.p = em28xx_db_scale, |
| 683 | kctl = snd_ctl_new1(&tmp, dev); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 684 | err = snd_ctl_add(card, kctl); |
| 685 | if (err < 0) |
| 686 | return err; |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 687 | dprintk("Added control %s for ac97 volume control 0x%04x\n", |
| 688 | ctl_name, id); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 693 | /* |
| 694 | * register/unregister code and data |
| 695 | */ |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 696 | static struct snd_pcm_ops snd_em28xx_pcm_capture = { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 697 | .open = snd_em28xx_capture_open, |
| 698 | .close = snd_em28xx_pcm_close, |
| 699 | .ioctl = snd_pcm_lib_ioctl, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 700 | .hw_params = snd_em28xx_hw_capture_params, |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 701 | .hw_free = snd_em28xx_hw_capture_free, |
| 702 | .prepare = snd_em28xx_prepare, |
| 703 | .trigger = snd_em28xx_capture_trigger, |
| 704 | .pointer = snd_em28xx_capture_pointer, |
| 705 | .page = snd_pcm_get_vmalloc_page, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 706 | }; |
| 707 | |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 708 | static void em28xx_audio_free_urb(struct em28xx *dev) |
| 709 | { |
| 710 | int i; |
| 711 | |
Mauro Carvalho Chehab | 1b3fd2d | 2014-01-10 05:53:24 -0300 | [diff] [blame] | 712 | for (i = 0; i < dev->adev.num_urb; i++) { |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 713 | struct urb *urb = dev->adev.urb[i]; |
| 714 | |
Mauro Carvalho Chehab | 1b3fd2d | 2014-01-10 05:53:24 -0300 | [diff] [blame] | 715 | if (!urb) |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 716 | continue; |
| 717 | |
Wei Yongjun | ff0284c | 2014-01-15 02:52:12 -0300 | [diff] [blame] | 718 | usb_free_coherent(dev->udev, urb->transfer_buffer_length, |
| 719 | dev->adev.transfer_buffer[i], |
| 720 | urb->transfer_dma); |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 721 | |
| 722 | usb_free_urb(urb); |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 723 | } |
Mauro Carvalho Chehab | 1b3fd2d | 2014-01-10 05:53:24 -0300 | [diff] [blame] | 724 | kfree(dev->adev.urb); |
| 725 | kfree(dev->adev.transfer_buffer); |
| 726 | dev->adev.num_urb = 0; |
| 727 | } |
| 728 | |
| 729 | /* high bandwidth multiplier, as encoded in highspeed endpoint descriptors */ |
| 730 | static int em28xx_audio_ep_packet_size(struct usb_device *udev, |
| 731 | struct usb_endpoint_descriptor *e) |
| 732 | { |
| 733 | int size = le16_to_cpu(e->wMaxPacketSize); |
| 734 | |
| 735 | if (udev->speed == USB_SPEED_HIGH) |
| 736 | return (size & 0x7ff) * (1 + (((size) >> 11) & 0x03)); |
| 737 | |
| 738 | return size & 0x7ff; |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 739 | } |
| 740 | |
Mauro Carvalho Chehab | 966f416 | 2014-01-12 10:10:34 -0300 | [diff] [blame] | 741 | static int em28xx_audio_urb_init(struct em28xx *dev) |
| 742 | { |
| 743 | struct usb_interface *intf; |
| 744 | struct usb_endpoint_descriptor *e, *ep = NULL; |
| 745 | int i, ep_size, interval, num_urb, npackets; |
| 746 | int urb_size, bytes_per_transfer; |
| 747 | u8 alt; |
| 748 | |
Frank Schaefer | 961717b | 2014-01-13 19:02:06 -0300 | [diff] [blame] | 749 | if (dev->ifnum) |
Mauro Carvalho Chehab | 966f416 | 2014-01-12 10:10:34 -0300 | [diff] [blame] | 750 | alt = 1; |
| 751 | else |
| 752 | alt = 7; |
| 753 | |
Frank Schaefer | 961717b | 2014-01-13 19:02:06 -0300 | [diff] [blame] | 754 | intf = usb_ifnum_to_if(dev->udev, dev->ifnum); |
Mauro Carvalho Chehab | 966f416 | 2014-01-12 10:10:34 -0300 | [diff] [blame] | 755 | |
| 756 | if (intf->num_altsetting <= alt) { |
| 757 | em28xx_errdev("alt %d doesn't exist on interface %d\n", |
Frank Schaefer | 961717b | 2014-01-13 19:02:06 -0300 | [diff] [blame] | 758 | dev->ifnum, alt); |
Mauro Carvalho Chehab | 966f416 | 2014-01-12 10:10:34 -0300 | [diff] [blame] | 759 | return -ENODEV; |
| 760 | } |
| 761 | |
| 762 | for (i = 0; i < intf->altsetting[alt].desc.bNumEndpoints; i++) { |
| 763 | e = &intf->altsetting[alt].endpoint[i].desc; |
| 764 | if (!usb_endpoint_dir_in(e)) |
| 765 | continue; |
| 766 | if (e->bEndpointAddress == EM28XX_EP_AUDIO) { |
| 767 | ep = e; |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | if (!ep) { |
| 773 | em28xx_errdev("Couldn't find an audio endpoint"); |
| 774 | return -ENODEV; |
| 775 | } |
| 776 | |
| 777 | ep_size = em28xx_audio_ep_packet_size(dev->udev, ep); |
| 778 | interval = 1 << (ep->bInterval - 1); |
| 779 | |
| 780 | em28xx_info("Endpoint 0x%02x %s on intf %d alt %d interval = %d, size %d\n", |
| 781 | EM28XX_EP_AUDIO, usb_speed_string(dev->udev->speed), |
Frank Schaefer | 961717b | 2014-01-13 19:02:06 -0300 | [diff] [blame] | 782 | dev->ifnum, alt, |
Mauro Carvalho Chehab | 966f416 | 2014-01-12 10:10:34 -0300 | [diff] [blame] | 783 | interval, |
| 784 | ep_size); |
| 785 | |
| 786 | /* Calculate the number and size of URBs to better fit the audio samples */ |
| 787 | |
| 788 | /* |
| 789 | * Estimate the number of bytes per DMA transfer. |
| 790 | * |
| 791 | * This is given by the bit rate (for now, only 48000 Hz) multiplied |
| 792 | * by 2 channels and 2 bytes/sample divided by the number of microframe |
| 793 | * intervals and by the microframe rate (125 us) |
| 794 | */ |
| 795 | bytes_per_transfer = DIV_ROUND_UP(48000 * 2 * 2, 125 * interval); |
| 796 | |
| 797 | /* |
| 798 | * Estimate the number of transfer URBs. Don't let it go past the |
| 799 | * maximum number of URBs that is known to be supported by the device. |
| 800 | */ |
| 801 | num_urb = DIV_ROUND_UP(bytes_per_transfer, ep_size); |
| 802 | if (num_urb > EM28XX_MAX_AUDIO_BUFS) |
| 803 | num_urb = EM28XX_MAX_AUDIO_BUFS; |
| 804 | |
| 805 | /* |
| 806 | * Now that we know the number of bytes per transfer and the number of |
| 807 | * URBs, estimate the typical size of an URB, in order to adjust the |
| 808 | * minimal number of packets. |
| 809 | */ |
| 810 | urb_size = bytes_per_transfer / num_urb; |
| 811 | |
| 812 | /* |
| 813 | * Now, calculate the amount of audio packets to be filled on each |
| 814 | * URB. In order to preserve the old behaviour, use a minimal |
| 815 | * threshold for this value. |
| 816 | */ |
| 817 | npackets = EM28XX_MIN_AUDIO_PACKETS; |
| 818 | if (urb_size > ep_size * npackets) |
| 819 | npackets = DIV_ROUND_UP(urb_size, ep_size); |
| 820 | |
| 821 | em28xx_info("Number of URBs: %d, with %d packets and %d size", |
| 822 | num_urb, npackets, urb_size); |
| 823 | |
Mauro Carvalho Chehab | a02b9c2 | 2014-01-13 03:34:23 -0300 | [diff] [blame] | 824 | /* Estimate the bytes per period */ |
| 825 | dev->adev.period = urb_size * npackets; |
| 826 | |
Mauro Carvalho Chehab | 966f416 | 2014-01-12 10:10:34 -0300 | [diff] [blame] | 827 | /* Allocate space to store the number of URBs to be used */ |
| 828 | |
| 829 | dev->adev.transfer_buffer = kcalloc(num_urb, |
| 830 | sizeof(*dev->adev.transfer_buffer), |
| 831 | GFP_ATOMIC); |
| 832 | if (!dev->adev.transfer_buffer) { |
| 833 | return -ENOMEM; |
| 834 | } |
| 835 | |
| 836 | dev->adev.urb = kcalloc(num_urb, sizeof(*dev->adev.urb), GFP_ATOMIC); |
| 837 | if (!dev->adev.urb) { |
| 838 | kfree(dev->adev.transfer_buffer); |
| 839 | return -ENOMEM; |
| 840 | } |
| 841 | |
| 842 | /* Alloc memory for each URB and for each transfer buffer */ |
| 843 | dev->adev.num_urb = num_urb; |
| 844 | for (i = 0; i < num_urb; i++) { |
| 845 | struct urb *urb; |
| 846 | int j, k; |
| 847 | void *buf; |
| 848 | |
| 849 | urb = usb_alloc_urb(npackets, GFP_ATOMIC); |
| 850 | if (!urb) { |
| 851 | em28xx_errdev("usb_alloc_urb failed!\n"); |
| 852 | em28xx_audio_free_urb(dev); |
| 853 | return -ENOMEM; |
| 854 | } |
| 855 | dev->adev.urb[i] = urb; |
| 856 | |
| 857 | buf = usb_alloc_coherent(dev->udev, npackets * ep_size, GFP_ATOMIC, |
| 858 | &urb->transfer_dma); |
| 859 | if (!buf) { |
| 860 | em28xx_errdev("usb_alloc_coherent failed!\n"); |
| 861 | em28xx_audio_free_urb(dev); |
| 862 | return -ENOMEM; |
| 863 | } |
| 864 | dev->adev.transfer_buffer[i] = buf; |
| 865 | |
| 866 | urb->dev = dev->udev; |
| 867 | urb->context = dev; |
| 868 | urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO); |
| 869 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
| 870 | urb->transfer_buffer = buf; |
| 871 | urb->interval = interval; |
| 872 | urb->complete = em28xx_audio_isocirq; |
| 873 | urb->number_of_packets = npackets; |
| 874 | urb->transfer_buffer_length = ep_size * npackets; |
| 875 | |
| 876 | for (j = k = 0; j < npackets; j++, k += ep_size) { |
| 877 | urb->iso_frame_desc[j].offset = k; |
| 878 | urb->iso_frame_desc[j].length = ep_size; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 885 | static int em28xx_audio_init(struct em28xx *dev) |
| 886 | { |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 887 | struct em28xx_audio *adev = &dev->adev; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 888 | struct snd_pcm *pcm; |
| 889 | struct snd_card *card; |
| 890 | static int devnr; |
Mauro Carvalho Chehab | 966f416 | 2014-01-12 10:10:34 -0300 | [diff] [blame] | 891 | int err; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 892 | |
Frank Schaefer | 961717b | 2014-01-13 19:02:06 -0300 | [diff] [blame] | 893 | if (!dev->has_alsa_audio) { |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 894 | /* This device does not support the extension (in this case |
Devin Heitmueller | 24a613e | 2008-11-12 02:05:19 -0300 | [diff] [blame] | 895 | the device is expecting the snd-usb-audio module or |
| 896 | doesn't have analog audio support at all) */ |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 897 | return 0; |
| 898 | } |
| 899 | |
Mauro Carvalho Chehab | 9634614 | 2013-12-26 12:41:03 -0300 | [diff] [blame] | 900 | em28xx_info("Binding audio extension\n"); |
| 901 | |
Mauro Carvalho Chehab | 47677e5 | 2014-03-05 11:21:07 -0300 | [diff] [blame] | 902 | kref_get(&dev->ref); |
| 903 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 904 | printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus " |
| 905 | "Rechberger\n"); |
Mauro Carvalho Chehab | f5f894d | 2013-12-28 21:16:26 -0300 | [diff] [blame] | 906 | printk(KERN_INFO |
| 907 | "em28xx-audio.c: Copyright (C) 2007-2014 Mauro Carvalho Chehab\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 908 | |
Takashi Iwai | 758021b | 2009-01-12 15:17:09 +0100 | [diff] [blame] | 909 | err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0, |
| 910 | &card); |
| 911 | if (err < 0) |
| 912 | return err; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 913 | |
| 914 | spin_lock_init(&adev->slock); |
Mauro Carvalho Chehab | 1b3fd2d | 2014-01-10 05:53:24 -0300 | [diff] [blame] | 915 | adev->sndcard = card; |
| 916 | adev->udev = dev->udev; |
| 917 | |
Mauro Carvalho Chehab | 1f6340b | 2008-11-07 14:24:18 -0300 | [diff] [blame] | 918 | err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm); |
Mauro Carvalho Chehab | 0cd03a0 | 2014-01-12 17:20:31 -0300 | [diff] [blame] | 919 | if (err < 0) |
| 920 | goto card_free; |
Mauro Carvalho Chehab | 1f6340b | 2008-11-07 14:24:18 -0300 | [diff] [blame] | 921 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 922 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture); |
| 923 | pcm->info_flags = 0; |
| 924 | pcm->private_data = dev; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 925 | strcpy(pcm->name, "Empia 28xx Capture"); |
Nicola Soranzo | 7662b00 | 2009-02-19 13:41:56 -0300 | [diff] [blame] | 926 | |
| 927 | snd_card_set_dev(card, &dev->udev->dev); |
Dan Carpenter | 4a3eaee | 2010-03-23 08:40:43 -0300 | [diff] [blame] | 928 | strcpy(card->driver, "Em28xx-Audio"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 929 | strcpy(card->shortname, "Em28xx Audio"); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 930 | strcpy(card->longname, "Empia Em28xx Audio"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 931 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 932 | INIT_WORK(&dev->wq_trigger, audio_trigger); |
| 933 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 934 | if (dev->audio_mode.ac97 != EM28XX_NO_AC97) { |
Ezequiel García | 67b8d03 | 2012-06-11 15:17:23 -0300 | [diff] [blame] | 935 | em28xx_cvol_new(card, dev, "Video", AC97_VIDEO); |
| 936 | em28xx_cvol_new(card, dev, "Line In", AC97_LINE); |
| 937 | em28xx_cvol_new(card, dev, "Phone", AC97_PHONE); |
| 938 | em28xx_cvol_new(card, dev, "Microphone", AC97_MIC); |
| 939 | em28xx_cvol_new(card, dev, "CD", AC97_CD); |
| 940 | em28xx_cvol_new(card, dev, "AUX", AC97_AUX); |
| 941 | em28xx_cvol_new(card, dev, "PCM", AC97_PCM); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 942 | |
Ezequiel García | 67b8d03 | 2012-06-11 15:17:23 -0300 | [diff] [blame] | 943 | em28xx_cvol_new(card, dev, "Master", AC97_MASTER); |
| 944 | em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE); |
| 945 | em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO); |
| 946 | em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER); |
| 947 | em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 948 | } |
| 949 | |
Mauro Carvalho Chehab | 966f416 | 2014-01-12 10:10:34 -0300 | [diff] [blame] | 950 | err = em28xx_audio_urb_init(dev); |
Mauro Carvalho Chehab | 0cd03a0 | 2014-01-12 17:20:31 -0300 | [diff] [blame] | 951 | if (err) |
| 952 | goto card_free; |
Mauro Carvalho Chehab | d2849fa | 2014-01-10 05:43:09 -0300 | [diff] [blame] | 953 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 954 | err = snd_card_register(card); |
Mauro Carvalho Chehab | 0cd03a0 | 2014-01-12 17:20:31 -0300 | [diff] [blame] | 955 | if (err < 0) |
| 956 | goto urb_free; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 957 | |
Mauro Carvalho Chehab | 9634614 | 2013-12-26 12:41:03 -0300 | [diff] [blame] | 958 | em28xx_info("Audio extension successfully initialized\n"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 959 | return 0; |
Mauro Carvalho Chehab | 0cd03a0 | 2014-01-12 17:20:31 -0300 | [diff] [blame] | 960 | |
| 961 | urb_free: |
| 962 | em28xx_audio_free_urb(dev); |
| 963 | |
| 964 | card_free: |
| 965 | snd_card_free(card); |
Mauro Carvalho Chehab | 452f236 | 2014-01-14 14:49:04 -0200 | [diff] [blame] | 966 | adev->sndcard = NULL; |
Mauro Carvalho Chehab | 0cd03a0 | 2014-01-12 17:20:31 -0300 | [diff] [blame] | 967 | |
| 968 | return err; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static int em28xx_audio_fini(struct em28xx *dev) |
| 972 | { |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 973 | if (dev == NULL) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 974 | return 0; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 975 | |
Mauro Carvalho Chehab | 47677e5 | 2014-03-05 11:21:07 -0300 | [diff] [blame] | 976 | if (!dev->has_alsa_audio) { |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 977 | /* This device does not support the extension (in this case |
Devin Heitmueller | 24a613e | 2008-11-12 02:05:19 -0300 | [diff] [blame] | 978 | the device is expecting the snd-usb-audio module or |
| 979 | doesn't have analog audio support at all) */ |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 980 | return 0; |
| 981 | } |
| 982 | |
Mauro Carvalho Chehab | aa929ad | 2014-01-12 19:22:07 -0300 | [diff] [blame] | 983 | em28xx_info("Closing audio extension"); |
| 984 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 985 | if (dev->adev.sndcard) { |
Mauro Carvalho Chehab | 452f236 | 2014-01-14 14:49:04 -0200 | [diff] [blame] | 986 | snd_card_disconnect(dev->adev.sndcard); |
| 987 | flush_work(&dev->wq_trigger); |
| 988 | |
| 989 | em28xx_audio_free_urb(dev); |
| 990 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 991 | snd_card_free(dev->adev.sndcard); |
| 992 | dev->adev.sndcard = NULL; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 993 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 994 | |
Mauro Carvalho Chehab | 47677e5 | 2014-03-05 11:21:07 -0300 | [diff] [blame] | 995 | kref_put(&dev->ref, em28xx_free_device); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 996 | return 0; |
| 997 | } |
| 998 | |
Shuah Khan | 6d746f9 | 2014-02-21 21:50:14 -0300 | [diff] [blame] | 999 | static int em28xx_audio_suspend(struct em28xx *dev) |
| 1000 | { |
| 1001 | if (dev == NULL) |
| 1002 | return 0; |
| 1003 | |
| 1004 | if (!dev->has_alsa_audio) |
| 1005 | return 0; |
| 1006 | |
| 1007 | em28xx_info("Suspending audio extension"); |
| 1008 | em28xx_deinit_isoc_audio(dev); |
| 1009 | atomic_set(&dev->stream_started, 0); |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | static int em28xx_audio_resume(struct em28xx *dev) |
| 1014 | { |
| 1015 | if (dev == NULL) |
| 1016 | return 0; |
| 1017 | |
| 1018 | if (!dev->has_alsa_audio) |
| 1019 | return 0; |
| 1020 | |
| 1021 | em28xx_info("Resuming audio extension"); |
| 1022 | /* Nothing to do other than schedule_work() ?? */ |
| 1023 | schedule_work(&dev->wq_trigger); |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 1027 | static struct em28xx_ops audio_ops = { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 1028 | .id = EM28XX_AUDIO, |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 1029 | .name = "Em28xx Audio Extension", |
| 1030 | .init = em28xx_audio_init, |
| 1031 | .fini = em28xx_audio_fini, |
Shuah Khan | 6d746f9 | 2014-02-21 21:50:14 -0300 | [diff] [blame] | 1032 | .suspend = em28xx_audio_suspend, |
| 1033 | .resume = em28xx_audio_resume, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 1034 | }; |
| 1035 | |
| 1036 | static int __init em28xx_alsa_register(void) |
| 1037 | { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 1038 | return em28xx_register_extension(&audio_ops); |
| 1039 | } |
| 1040 | |
| 1041 | static void __exit em28xx_alsa_unregister(void) |
| 1042 | { |
| 1043 | em28xx_unregister_extension(&audio_ops); |
| 1044 | } |
| 1045 | |
| 1046 | MODULE_LICENSE("GPL"); |
| 1047 | MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>"); |
Mauro Carvalho Chehab | 37e59f8 | 2014-02-07 08:03:07 -0200 | [diff] [blame] | 1048 | MODULE_AUTHOR("Mauro Carvalho Chehab"); |
Mauro Carvalho Chehab | d8992b0 | 2013-12-27 11:14:59 -0300 | [diff] [blame] | 1049 | MODULE_DESCRIPTION(DRIVER_DESC " - audio interface"); |
| 1050 | MODULE_VERSION(EM28XX_VERSION); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 1051 | |
| 1052 | module_init(em28xx_alsa_register); |
| 1053 | module_exit(em28xx_alsa_unregister); |