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