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