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