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 | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 6 | * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org> |
| 7 | * - Port to work with the in-kernel driver |
| 8 | * - Several cleanups |
| 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> |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 44 | #include <media/v4l2-common.h> |
| 45 | #include "em28xx.h" |
| 46 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 47 | static int debug; |
| 48 | module_param(debug, int, 0644); |
| 49 | MODULE_PARM_DESC(debug, "activates debug info"); |
| 50 | |
| 51 | #define dprintk(fmt, arg...) do { \ |
| 52 | if (debug) \ |
| 53 | printk(KERN_INFO "em28xx-audio %s: " fmt, \ |
Harvey Harrison | d80e134 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 54 | __func__, ##arg); \ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 55 | } while (0) |
| 56 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 57 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 58 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 59 | static int em28xx_isoc_audio_deinit(struct em28xx *dev) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 60 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 61 | int i; |
| 62 | |
| 63 | dprintk("Stopping isoc\n"); |
| 64 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
| 65 | usb_kill_urb(dev->adev->urb[i]); |
| 66 | usb_free_urb(dev->adev->urb[i]); |
| 67 | dev->adev->urb[i] = NULL; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 68 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 69 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 73 | static void em28xx_audio_isocirq(struct urb *urb) |
| 74 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 75 | struct em28xx *dev = urb->context; |
| 76 | int i; |
| 77 | unsigned int oldptr; |
| 78 | unsigned long flags; |
| 79 | int period_elapsed = 0; |
| 80 | int status; |
| 81 | unsigned char *cp; |
| 82 | unsigned int stride; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 83 | struct snd_pcm_substream *substream; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 84 | struct snd_pcm_runtime *runtime; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 85 | if (dev->adev->capture_pcm_substream) { |
| 86 | substream = dev->adev->capture_pcm_substream; |
| 87 | runtime = substream->runtime; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 88 | stride = runtime->frame_bits >> 3; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 89 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 90 | for (i = 0; i < urb->number_of_packets; i++) { |
| 91 | int length = |
| 92 | urb->iso_frame_desc[i].actual_length / stride; |
| 93 | cp = (unsigned char *)urb->transfer_buffer + |
| 94 | urb->iso_frame_desc[i].offset; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 95 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 96 | if (!length) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 97 | continue; |
| 98 | |
| 99 | spin_lock_irqsave(&dev->adev->slock, flags); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 100 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 101 | oldptr = dev->adev->hwptr_done_capture; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 102 | dev->adev->hwptr_done_capture += length; |
| 103 | if (dev->adev->hwptr_done_capture >= |
| 104 | runtime->buffer_size) |
| 105 | dev->adev->hwptr_done_capture -= |
| 106 | runtime->buffer_size; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 107 | |
| 108 | dev->adev->capture_transfer_done += length; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 109 | if (dev->adev->capture_transfer_done >= |
| 110 | runtime->period_size) { |
| 111 | dev->adev->capture_transfer_done -= |
| 112 | runtime->period_size; |
| 113 | period_elapsed = 1; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 114 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 115 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 116 | spin_unlock_irqrestore(&dev->adev->slock, flags); |
| 117 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 118 | if (oldptr + length >= runtime->buffer_size) { |
| 119 | unsigned int cnt = |
| 120 | runtime->buffer_size - oldptr - 1; |
| 121 | memcpy(runtime->dma_area + oldptr * stride, cp, |
| 122 | cnt * stride); |
| 123 | memcpy(runtime->dma_area, cp + cnt, |
| 124 | length * stride - cnt * stride); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 125 | } else { |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 126 | memcpy(runtime->dma_area + oldptr * stride, cp, |
| 127 | length * stride); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 128 | } |
| 129 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 130 | if (period_elapsed) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 131 | snd_pcm_period_elapsed(substream); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 132 | } |
| 133 | urb->status = 0; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 134 | |
| 135 | if (dev->adev->shutdown) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 136 | return; |
| 137 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 138 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 139 | if (status < 0) { |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 140 | em28xx_errdev("resubmit of audio urb failed (error=%i)\n", |
| 141 | status); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 142 | } |
| 143 | return; |
| 144 | } |
| 145 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 146 | static int em28xx_init_audio_isoc(struct em28xx *dev) |
| 147 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 148 | int i, errCode; |
| 149 | const int sb_size = EM28XX_NUM_AUDIO_PACKETS * |
| 150 | EM28XX_AUDIO_MAX_PACKET_SIZE; |
| 151 | |
| 152 | dprintk("Starting isoc transfers\n"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 153 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 154 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 155 | struct urb *urb; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 156 | int j, k; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 157 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 158 | dev->adev->transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 159 | if (!dev->adev->transfer_buffer[i]) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 160 | return -ENOMEM; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 161 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 162 | memset(dev->adev->transfer_buffer[i], 0x80, sb_size); |
| 163 | urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC); |
Douglas Schilling Landgraf | ff9b3e4 | 2008-09-04 11:20:20 -0300 | [diff] [blame^] | 164 | if (!urb) { |
| 165 | em28xx_errdev("usb_alloc_urb failed!\n"); |
| 166 | for (j = 0; j < i; j++) { |
| 167 | usb_free_urb(dev->adev->urb[j]); |
| 168 | kfree(dev->adev->transfer_buffer[j]); |
| 169 | } |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 170 | return -ENOMEM; |
Douglas Schilling Landgraf | ff9b3e4 | 2008-09-04 11:20:20 -0300 | [diff] [blame^] | 171 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 172 | |
| 173 | urb->dev = dev->udev; |
| 174 | urb->context = dev; |
| 175 | urb->pipe = usb_rcvisocpipe(dev->udev, 0x83); |
| 176 | urb->transfer_flags = URB_ISO_ASAP; |
| 177 | urb->transfer_buffer = dev->adev->transfer_buffer[i]; |
| 178 | urb->interval = 1; |
| 179 | urb->complete = em28xx_audio_isocirq; |
| 180 | urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS; |
| 181 | urb->transfer_buffer_length = sb_size; |
| 182 | |
| 183 | for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS; |
| 184 | j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) { |
| 185 | urb->iso_frame_desc[j].offset = k; |
| 186 | urb->iso_frame_desc[j].length = |
| 187 | EM28XX_AUDIO_MAX_PACKET_SIZE; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 188 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 189 | dev->adev->urb[i] = urb; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 190 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 191 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 192 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 193 | errCode = usb_submit_urb(dev->adev->urb[i], GFP_ATOMIC); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 194 | if (errCode) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 195 | em28xx_isoc_audio_deinit(dev); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 196 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 197 | return errCode; |
| 198 | } |
| 199 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 200 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 204 | static int em28xx_cmd(struct em28xx *dev, int cmd, int arg) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 205 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 206 | dprintk("%s transfer\n", (dev->adev->capture_stream == STREAM_ON)? |
| 207 | "stop" : "start"); |
| 208 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 209 | switch (cmd) { |
| 210 | case EM28XX_CAPTURE_STREAM_EN: |
| 211 | if (dev->adev->capture_stream == STREAM_OFF && arg == 1) { |
| 212 | dev->adev->capture_stream = STREAM_ON; |
| 213 | em28xx_init_audio_isoc(dev); |
| 214 | } else if (dev->adev->capture_stream == STREAM_ON && arg == 0) { |
| 215 | dev->adev->capture_stream = STREAM_OFF; |
| 216 | em28xx_isoc_audio_deinit(dev); |
| 217 | } else { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 218 | printk(KERN_ERR "An underrun very likely occurred. " |
| 219 | "Ignoring it.\n"); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 220 | } |
| 221 | return 0; |
| 222 | default: |
| 223 | return -EINVAL; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 227 | static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, |
| 228 | size_t size) |
| 229 | { |
| 230 | struct snd_pcm_runtime *runtime = subs->runtime; |
| 231 | |
| 232 | dprintk("Alocating vbuffer\n"); |
| 233 | if (runtime->dma_area) { |
| 234 | if (runtime->dma_bytes > size) |
| 235 | return 0; |
| 236 | |
| 237 | vfree(runtime->dma_area); |
| 238 | } |
| 239 | runtime->dma_area = vmalloc(size); |
| 240 | if (!runtime->dma_area) |
| 241 | return -ENOMEM; |
| 242 | |
| 243 | runtime->dma_bytes = size; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static struct snd_pcm_hardware snd_em28xx_hw_capture = { |
| 249 | .info = SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 250 | SNDRV_PCM_INFO_MMAP | |
| 251 | SNDRV_PCM_INFO_INTERLEAVED | |
| 252 | SNDRV_PCM_INFO_MMAP_VALID, |
| 253 | |
| 254 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 255 | |
| 256 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT, |
| 257 | |
| 258 | .rate_min = 48000, |
| 259 | .rate_max = 48000, |
| 260 | .channels_min = 2, |
| 261 | .channels_max = 2, |
| 262 | .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */ |
| 263 | .period_bytes_min = 64, /* 12544/2, */ |
| 264 | .period_bytes_max = 12544, |
| 265 | .periods_min = 2, |
| 266 | .periods_max = 98, /* 12544, */ |
| 267 | }; |
| 268 | |
| 269 | static int snd_em28xx_capture_open(struct snd_pcm_substream *substream) |
| 270 | { |
| 271 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 272 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 273 | int ret = 0; |
| 274 | |
| 275 | dprintk("opening device and trying to acquire exclusive lock\n"); |
| 276 | |
Mauro Carvalho Chehab | 83ee87a | 2008-06-14 09:41:18 -0300 | [diff] [blame] | 277 | if (!dev) { |
| 278 | printk(KERN_ERR "BUG: em28xx can't find device struct." |
| 279 | " Can't proceed with open\n"); |
| 280 | return -ENODEV; |
| 281 | } |
| 282 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 283 | /* Sets volume, mute, etc */ |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 284 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 285 | dev->mute = 0; |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 286 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 287 | ret = em28xx_audio_analog_set(dev); |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 288 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 289 | if (ret < 0) |
| 290 | goto err; |
| 291 | |
| 292 | runtime->hw = snd_em28xx_hw_capture; |
| 293 | if (dev->alt == 0 && dev->adev->users == 0) { |
| 294 | int errCode; |
| 295 | dev->alt = 7; |
| 296 | errCode = usb_set_interface(dev->udev, 0, 7); |
| 297 | dprintk("changing alternate number to 7\n"); |
| 298 | } |
| 299 | |
| 300 | dev->adev->users++; |
| 301 | |
| 302 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
| 303 | dev->adev->capture_pcm_substream = substream; |
| 304 | runtime->private_data = dev; |
| 305 | |
| 306 | return 0; |
| 307 | err: |
| 308 | printk(KERN_ERR "Error while configuring em28xx mixer\n"); |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream) |
| 313 | { |
| 314 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 315 | dev->adev->users--; |
| 316 | |
| 317 | dprintk("closing device\n"); |
| 318 | |
| 319 | dev->mute = 1; |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 320 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 321 | em28xx_audio_analog_set(dev); |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 322 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 323 | |
| 324 | if (dev->adev->users == 0 && dev->adev->shutdown == 1) { |
| 325 | dprintk("audio users: %d\n", dev->adev->users); |
| 326 | dprintk("disabling audio stream!\n"); |
| 327 | dev->adev->shutdown = 0; |
| 328 | dprintk("released lock\n"); |
| 329 | em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0); |
| 330 | } |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream, |
| 335 | struct snd_pcm_hw_params *hw_params) |
| 336 | { |
| 337 | unsigned int channels, rate, format; |
| 338 | int ret; |
| 339 | |
| 340 | dprintk("Setting capture parameters\n"); |
| 341 | |
| 342 | ret = snd_pcm_alloc_vmalloc_buffer(substream, |
| 343 | params_buffer_bytes(hw_params)); |
| 344 | format = params_format(hw_params); |
| 345 | rate = params_rate(hw_params); |
| 346 | channels = params_channels(hw_params); |
| 347 | |
| 348 | /* TODO: set up em28xx audio chip to deliver the correct audio format, |
| 349 | current default is 48000hz multiplexed => 96000hz mono |
| 350 | which shouldn't matter since analogue TV only supports mono */ |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream) |
| 355 | { |
| 356 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 357 | |
| 358 | dprintk("Stop capture, if needed\n"); |
| 359 | |
| 360 | if (dev->adev->capture_stream == STREAM_ON) |
| 361 | em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0); |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int snd_em28xx_prepare(struct snd_pcm_substream *substream) |
| 367 | { |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream, |
| 372 | int cmd) |
| 373 | { |
| 374 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 375 | |
| 376 | dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)? |
| 377 | "start": "stop"); |
| 378 | switch (cmd) { |
| 379 | case SNDRV_PCM_TRIGGER_START: |
| 380 | em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1); |
| 381 | return 0; |
| 382 | case SNDRV_PCM_TRIGGER_STOP: |
| 383 | dev->adev->shutdown = 1; |
| 384 | return 0; |
| 385 | default: |
| 386 | return -EINVAL; |
| 387 | } |
| 388 | } |
| 389 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 390 | static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream |
| 391 | *substream) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 392 | { |
| 393 | struct em28xx *dev; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 394 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 395 | snd_pcm_uframes_t hwptr_done; |
| 396 | dev = snd_pcm_substream_chip(substream); |
| 397 | hwptr_done = dev->adev->hwptr_done_capture; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 398 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 399 | return hwptr_done; |
| 400 | } |
| 401 | |
| 402 | static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs, |
| 403 | unsigned long offset) |
| 404 | { |
| 405 | void *pageptr = subs->runtime->dma_area + offset; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 406 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 407 | return vmalloc_to_page(pageptr); |
| 408 | } |
| 409 | |
| 410 | static struct snd_pcm_ops snd_em28xx_pcm_capture = { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 411 | .open = snd_em28xx_capture_open, |
| 412 | .close = snd_em28xx_pcm_close, |
| 413 | .ioctl = snd_pcm_lib_ioctl, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 414 | .hw_params = snd_em28xx_hw_capture_params, |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 415 | .hw_free = snd_em28xx_hw_capture_free, |
| 416 | .prepare = snd_em28xx_prepare, |
| 417 | .trigger = snd_em28xx_capture_trigger, |
| 418 | .pointer = snd_em28xx_capture_pointer, |
| 419 | .page = snd_pcm_get_vmalloc_page, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 420 | }; |
| 421 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 422 | static int em28xx_audio_init(struct em28xx *dev) |
| 423 | { |
| 424 | struct em28xx_audio *adev; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 425 | struct snd_pcm *pcm; |
| 426 | struct snd_card *card; |
| 427 | static int devnr; |
| 428 | int ret, err; |
| 429 | |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 430 | if (dev->has_audio_class) { |
| 431 | /* This device does not support the extension (in this case |
| 432 | the device is expecting the snd-usb-audio module */ |
| 433 | return 0; |
| 434 | } |
| 435 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 436 | printk(KERN_INFO "em28xx-audio.c: probing for em28x1 " |
| 437 | "non standard usbaudio\n"); |
| 438 | printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus " |
| 439 | "Rechberger\n"); |
| 440 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 441 | adev = kzalloc(sizeof(*adev), GFP_KERNEL); |
| 442 | if (!adev) { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 443 | printk(KERN_ERR "em28xx-audio.c: out of memory\n"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 444 | return -1; |
| 445 | } |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 446 | card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0); |
| 447 | if (card == NULL) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 448 | kfree(adev); |
| 449 | return -ENOMEM; |
| 450 | } |
| 451 | |
| 452 | spin_lock_init(&adev->slock); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 453 | ret = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 454 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture); |
| 455 | pcm->info_flags = 0; |
| 456 | pcm->private_data = dev; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 457 | strcpy(pcm->name, "Empia 28xx Capture"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 458 | strcpy(card->driver, "Empia Em28xx Audio"); |
| 459 | strcpy(card->shortname, "Em28xx Audio"); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 460 | strcpy(card->longname, "Empia Em28xx Audio"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 461 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 462 | err = snd_card_register(card); |
| 463 | if (err < 0) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 464 | snd_card_free(card); |
| 465 | return -ENOMEM; |
| 466 | } |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 467 | adev->sndcard = card; |
| 468 | adev->udev = dev->udev; |
| 469 | dev->adev = adev; |
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 0; |
| 472 | } |
| 473 | |
| 474 | static int em28xx_audio_fini(struct em28xx *dev) |
| 475 | { |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 476 | if (dev == NULL) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 477 | return 0; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 478 | |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 479 | if (dev->has_audio_class) { |
| 480 | /* This device does not support the extension (in this case |
| 481 | the device is expecting the snd-usb-audio module */ |
| 482 | return 0; |
| 483 | } |
| 484 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 485 | if (dev->adev) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 486 | snd_card_free(dev->adev->sndcard); |
| 487 | kfree(dev->adev); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 488 | dev->adev = NULL; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 489 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 490 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | static struct em28xx_ops audio_ops = { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 495 | .id = EM28XX_AUDIO, |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 496 | .name = "Em28xx Audio Extension", |
| 497 | .init = em28xx_audio_init, |
| 498 | .fini = em28xx_audio_fini, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 499 | }; |
| 500 | |
| 501 | static int __init em28xx_alsa_register(void) |
| 502 | { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 503 | return em28xx_register_extension(&audio_ops); |
| 504 | } |
| 505 | |
| 506 | static void __exit em28xx_alsa_unregister(void) |
| 507 | { |
| 508 | em28xx_unregister_extension(&audio_ops); |
| 509 | } |
| 510 | |
| 511 | MODULE_LICENSE("GPL"); |
| 512 | MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 513 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 514 | MODULE_DESCRIPTION("Em28xx Audio driver"); |
| 515 | |
| 516 | module_init(em28xx_alsa_register); |
| 517 | module_exit(em28xx_alsa_unregister); |