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