blob: 5381f6d7427dae2af1f862d9ef633f68fad9e544 [file] [log] [blame]
Markus Rechbergera52932b2008-01-05 09:55:47 -03001/*
2 * Empiatech em28x1 audio extension
3 *
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
5 *
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03006 * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
7 * - Port to work with the in-kernel driver
8 * - Several cleanups
9 *
Markus Rechbergera52932b2008-01-05 09:55:47 -030010 * 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 Chehab6d794682008-01-05 09:57:31 -030037#include <linux/module.h>
Markus Rechbergera52932b2008-01-05 09:55:47 -030038#include <sound/core.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/info.h>
42#include <sound/initval.h>
43#include <sound/control.h>
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -030044#include <sound/tlv.h>
Markus Rechbergera52932b2008-01-05 09:55:47 -030045#include <media/v4l2-common.h>
46#include "em28xx.h"
47
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030048static int debug;
49module_param(debug, int, 0644);
50MODULE_PARM_DESC(debug, "activates debug info");
51
52#define dprintk(fmt, arg...) do { \
53 if (debug) \
54 printk(KERN_INFO "em28xx-audio %s: " fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030055 __func__, ##arg); \
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030056 } while (0)
57
Markus Rechbergera52932b2008-01-05 09:55:47 -030058static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
Markus Rechbergera52932b2008-01-05 09:55:47 -030059
Robert Krakoraaa5a1822009-02-08 13:09:11 -030060static int em28xx_deinit_isoc_audio(struct em28xx *dev)
Markus Rechbergera52932b2008-01-05 09:55:47 -030061{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030062 int i;
63
64 dprintk("Stopping isoc\n");
Douglas Schilling Landgraf3e099ba2009-02-08 10:45:34 -030065 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Robert Krakora9c062102009-01-25 13:08:07 -030066 if (!irqs_disabled())
67 usb_kill_urb(dev->adev.urb[i]);
68 else
69 usb_unlink_urb(dev->adev.urb[i]);
Robert Krakoraaa5a1822009-02-08 13:09:11 -030070
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -030071 usb_free_urb(dev->adev.urb[i]);
72 dev->adev.urb[i] = NULL;
Robert Krakora53d12e52009-01-16 11:23:25 -030073
Robert Krakora9c062102009-01-25 13:08:07 -030074 kfree(dev->adev.transfer_buffer[i]);
75 dev->adev.transfer_buffer[i] = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -030076 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030077
Markus Rechbergera52932b2008-01-05 09:55:47 -030078 return 0;
79}
80
Markus Rechbergera52932b2008-01-05 09:55:47 -030081static void em28xx_audio_isocirq(struct urb *urb)
82{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030083 struct em28xx *dev = urb->context;
84 int i;
85 unsigned int oldptr;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030086 int period_elapsed = 0;
87 int status;
88 unsigned char *cp;
89 unsigned int stride;
Markus Rechbergera52932b2008-01-05 09:55:47 -030090 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030091 struct snd_pcm_runtime *runtime;
Robert Krakoraaa5a1822009-02-08 13:09:11 -030092
93 switch (urb->status) {
94 case 0: /* success */
95 case -ETIMEDOUT: /* NAK */
96 break;
97 case -ECONNRESET: /* kill */
98 case -ENOENT:
99 case -ESHUTDOWN:
100 return;
101 default: /* error */
102 dprintk("urb completition error %d.\n", urb->status);
103 break;
104 }
105
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300106 if (atomic_read(&dev->stream_started) == 0)
107 return;
108
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300109 if (dev->adev.capture_pcm_substream) {
110 substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300111 runtime = substream->runtime;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300112 stride = runtime->frame_bits >> 3;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300113
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300114 for (i = 0; i < urb->number_of_packets; i++) {
115 int length =
116 urb->iso_frame_desc[i].actual_length / stride;
117 cp = (unsigned char *)urb->transfer_buffer +
118 urb->iso_frame_desc[i].offset;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300119
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300120 if (!length)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300121 continue;
122
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300123 oldptr = dev->adev.hwptr_done_capture;
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300124 if (oldptr + length >= runtime->buffer_size) {
125 unsigned int cnt =
126 runtime->buffer_size - oldptr;
127 memcpy(runtime->dma_area + oldptr * stride, cp,
128 cnt * stride);
129 memcpy(runtime->dma_area, cp + cnt * stride,
130 length * stride - cnt * stride);
131 } else {
132 memcpy(runtime->dma_area + oldptr * stride, cp,
133 length * stride);
134 }
135
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300136 snd_pcm_stream_lock(substream);
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300137
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300138 dev->adev.hwptr_done_capture += length;
139 if (dev->adev.hwptr_done_capture >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300140 runtime->buffer_size)
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300141 dev->adev.hwptr_done_capture -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300142 runtime->buffer_size;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300143
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300144 dev->adev.capture_transfer_done += length;
145 if (dev->adev.capture_transfer_done >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300146 runtime->period_size) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300147 dev->adev.capture_transfer_done -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300148 runtime->period_size;
149 period_elapsed = 1;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300150 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300151
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300152 snd_pcm_stream_unlock(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300153 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300154 if (period_elapsed)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300155 snd_pcm_period_elapsed(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300156 }
157 urb->status = 0;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300158
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300159 status = usb_submit_urb(urb, GFP_ATOMIC);
160 if (status < 0) {
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300161 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
162 status);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300163 }
164 return;
165}
166
Markus Rechbergera52932b2008-01-05 09:55:47 -0300167static int em28xx_init_audio_isoc(struct em28xx *dev)
168{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300169 int i, errCode;
170 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
171 EM28XX_AUDIO_MAX_PACKET_SIZE;
172
173 dprintk("Starting isoc transfers\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300174
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300175 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300176 struct urb *urb;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300177 int j, k;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300178
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300179 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
180 if (!dev->adev.transfer_buffer[i])
Markus Rechbergera52932b2008-01-05 09:55:47 -0300181 return -ENOMEM;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300182
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300183 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300184 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300185 if (!urb) {
186 em28xx_errdev("usb_alloc_urb failed!\n");
187 for (j = 0; j < i; j++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300188 usb_free_urb(dev->adev.urb[j]);
189 kfree(dev->adev.transfer_buffer[j]);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300190 }
Markus Rechbergera52932b2008-01-05 09:55:47 -0300191 return -ENOMEM;
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300192 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300193
194 urb->dev = dev->udev;
195 urb->context = dev;
196 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
197 urb->transfer_flags = URB_ISO_ASAP;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300198 urb->transfer_buffer = dev->adev.transfer_buffer[i];
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300199 urb->interval = 1;
200 urb->complete = em28xx_audio_isocirq;
201 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
202 urb->transfer_buffer_length = sb_size;
203
204 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
205 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
206 urb->iso_frame_desc[j].offset = k;
207 urb->iso_frame_desc[j].length =
208 EM28XX_AUDIO_MAX_PACKET_SIZE;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300209 }
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300210 dev->adev.urb[i] = urb;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300211 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300212
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300213 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300214 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300215 if (errCode) {
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300216 em28xx_errdev("submit of audio urb failed\n");
Robert Krakoraaa5a1822009-02-08 13:09:11 -0300217 em28xx_deinit_isoc_audio(dev);
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300218 atomic_set(&dev->stream_started, 0);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300219 return errCode;
220 }
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300221
Markus Rechbergera52932b2008-01-05 09:55:47 -0300222 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300223
Markus Rechbergera52932b2008-01-05 09:55:47 -0300224 return 0;
225}
226
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300227static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
228 size_t size)
229{
230 struct snd_pcm_runtime *runtime = subs->runtime;
231
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300232 dprintk("Allocating vbuffer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300233 if (runtime->dma_area) {
234 if (runtime->dma_bytes > size)
235 return 0;
236
237 vfree(runtime->dma_area);
238 }
239 runtime->dma_area = vmalloc(size);
240 if (!runtime->dma_area)
241 return -ENOMEM;
242
243 runtime->dma_bytes = size;
244
245 return 0;
246}
247
248static struct snd_pcm_hardware snd_em28xx_hw_capture = {
249 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
250 SNDRV_PCM_INFO_MMAP |
251 SNDRV_PCM_INFO_INTERLEAVED |
252 SNDRV_PCM_INFO_MMAP_VALID,
253
254 .formats = SNDRV_PCM_FMTBIT_S16_LE,
255
256 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
257
258 .rate_min = 48000,
259 .rate_max = 48000,
260 .channels_min = 2,
261 .channels_max = 2,
262 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
263 .period_bytes_min = 64, /* 12544/2, */
264 .period_bytes_max = 12544,
265 .periods_min = 2,
266 .periods_max = 98, /* 12544, */
267};
268
269static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
270{
271 struct em28xx *dev = snd_pcm_substream_chip(substream);
272 struct snd_pcm_runtime *runtime = substream->runtime;
273 int ret = 0;
274
275 dprintk("opening device and trying to acquire exclusive lock\n");
276
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300277 if (!dev) {
Filipe Rosset0bc23082009-11-04 15:31:25 -0300278 em28xx_err("BUG: em28xx can't find device struct."
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300279 " Can't proceed with open\n");
280 return -ENODEV;
281 }
282
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300283 /* Sets volume, mute, etc */
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300284
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300285 dev->mute = 0;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300286 mutex_lock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300287 ret = em28xx_audio_analog_set(dev);
288 if (ret < 0)
289 goto err;
290
291 runtime->hw = snd_em28xx_hw_capture;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300292 if (dev->alt == 0 && dev->adev.users == 0) {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300293 dev->alt = 7;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300294 dprintk("changing alternate number to 7\n");
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300295 usb_set_interface(dev->udev, 0, 7);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300296 }
297
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300298 dev->adev.users++;
Douglas Schilling Landgrafbf510ac2009-02-08 01:11:13 -0300299 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300300
301 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300302 dev->adev.capture_pcm_substream = substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300303 runtime->private_data = dev;
304
305 return 0;
306err:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300307 mutex_unlock(&dev->lock);
308
Filipe Rosset0bc23082009-11-04 15:31:25 -0300309 em28xx_err("Error while configuring em28xx mixer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300310 return ret;
311}
312
313static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
314{
315 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300316
317 dprintk("closing device\n");
318
319 dev->mute = 1;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300320 mutex_lock(&dev->lock);
Douglas Schilling Landgrafbf510ac2009-02-08 01:11:13 -0300321 dev->adev.users--;
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300322 if (atomic_read(&dev->stream_started) > 0) {
323 atomic_set(&dev->stream_started, 0);
324 schedule_work(&dev->wq_trigger);
325 }
326
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300327 em28xx_audio_analog_set(dev);
Robert Krakora75c74d12009-05-28 11:16:19 -0300328 if (substream->runtime->dma_area) {
329 dprintk("freeing\n");
330 vfree(substream->runtime->dma_area);
331 substream->runtime->dma_area = NULL;
332 }
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300333 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300334
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300335 return 0;
336}
337
338static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
339 struct snd_pcm_hw_params *hw_params)
340{
341 unsigned int channels, rate, format;
342 int ret;
343
344 dprintk("Setting capture parameters\n");
345
346 ret = snd_pcm_alloc_vmalloc_buffer(substream,
347 params_buffer_bytes(hw_params));
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300348 if (ret < 0)
349 return ret;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300350 format = params_format(hw_params);
351 rate = params_rate(hw_params);
352 channels = params_channels(hw_params);
353
354 /* TODO: set up em28xx audio chip to deliver the correct audio format,
355 current default is 48000hz multiplexed => 96000hz mono
356 which shouldn't matter since analogue TV only supports mono */
357 return 0;
358}
359
360static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
361{
362 struct em28xx *dev = snd_pcm_substream_chip(substream);
363
364 dprintk("Stop capture, if needed\n");
365
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300366 if (atomic_read(&dev->stream_started) > 0) {
367 atomic_set(&dev->stream_started, 0);
368 schedule_work(&dev->wq_trigger);
369 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300370
371 return 0;
372}
373
374static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
375{
Devin Heitmueller96fbf772009-10-15 01:14:34 -0300376 struct em28xx *dev = snd_pcm_substream_chip(substream);
377
378 dev->adev.hwptr_done_capture = 0;
379 dev->adev.capture_transfer_done = 0;
380
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300381 return 0;
382}
383
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300384static void audio_trigger(struct work_struct *work)
385{
386 struct em28xx *dev = container_of(work, struct em28xx, wq_trigger);
387
388 if (atomic_read(&dev->stream_started)) {
389 dprintk("starting capture");
390 em28xx_init_audio_isoc(dev);
391 } else {
392 dprintk("stopping capture");
393 em28xx_deinit_isoc_audio(dev);
394 }
395}
396
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300397static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
398 int cmd)
399{
400 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300401 int retval = 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300402
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300403 switch (cmd) {
404 case SNDRV_PCM_TRIGGER_START:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300405 atomic_set(&dev->stream_started, 1);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300406 break;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300407 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300408 atomic_set(&dev->stream_started, 1);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300409 break;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300410 default:
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300411 retval = -EINVAL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300412 }
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300413 schedule_work(&dev->wq_trigger);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300414 return retval;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300415}
416
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300417static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
418 *substream)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300419{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300420 unsigned long flags;
Robert Krakora53d12e52009-01-16 11:23:25 -0300421 struct em28xx *dev;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300422 snd_pcm_uframes_t hwptr_done;
Robert Krakora53d12e52009-01-16 11:23:25 -0300423
Markus Rechbergera52932b2008-01-05 09:55:47 -0300424 dev = snd_pcm_substream_chip(substream);
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300425 spin_lock_irqsave(&dev->adev.slock, flags);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300426 hwptr_done = dev->adev.hwptr_done_capture;
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300427 spin_unlock_irqrestore(&dev->adev.slock, flags);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300428
Markus Rechbergera52932b2008-01-05 09:55:47 -0300429 return hwptr_done;
430}
431
432static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
433 unsigned long offset)
434{
435 void *pageptr = subs->runtime->dma_area + offset;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300436
Markus Rechbergera52932b2008-01-05 09:55:47 -0300437 return vmalloc_to_page(pageptr);
438}
439
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300440/*
441 * AC97 volume control support
442 */
443static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
444 struct snd_ctl_elem_info *info)
445{
446 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
447 info->count = 2;
448 info->value.integer.min = 0;
449 info->value.integer.max = 0x1f;
450
451 return 0;
452}
453
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300454static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
455 struct snd_ctl_elem_value *value)
456{
457 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300458 u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
459 (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300460 int rc;
461
462 mutex_lock(&dev->lock);
463 rc = em28xx_read_ac97(dev, kcontrol->private_value);
464 if (rc < 0)
465 goto err;
466
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300467 val |= rc & 0x8000; /* Preserve the mute flag */
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300468
469 rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300470 if (rc < 0)
471 goto err;
472
473 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
474 (val & 0x8000) ? "muted " : "",
475 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
476 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300477
478err:
479 mutex_unlock(&dev->lock);
480 return rc;
481}
482
483static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
484 struct snd_ctl_elem_value *value)
485{
486 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
487 int val;
488
489 mutex_lock(&dev->lock);
490 val = em28xx_read_ac97(dev, kcontrol->private_value);
491 mutex_unlock(&dev->lock);
492 if (val < 0)
493 return val;
494
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300495 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
496 (val & 0x8000) ? "muted " : "",
497 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
498 val, (int)kcontrol->private_value);
499
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300500 value->value.integer.value[0] = 0x1f - (val & 0x1f);
501 value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300502
503 return 0;
504}
505
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300506static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
507 struct snd_ctl_elem_value *value)
508{
509 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
510 u16 val = value->value.integer.value[0];
511 int rc;
512
513 mutex_lock(&dev->lock);
514 rc = em28xx_read_ac97(dev, kcontrol->private_value);
515 if (rc < 0)
516 goto err;
517
518 if (val)
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300519 rc &= 0x1f1f;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300520 else
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300521 rc |= 0x8000;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300522
523 rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300524 if (rc < 0)
525 goto err;
526
527 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
528 (val & 0x8000) ? "muted " : "",
529 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
530 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300531
532err:
533 mutex_unlock(&dev->lock);
534 return rc;
535}
536
537static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
538 struct snd_ctl_elem_value *value)
539{
540 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
541 int val;
542
543 mutex_lock(&dev->lock);
544 val = em28xx_read_ac97(dev, kcontrol->private_value);
545 mutex_unlock(&dev->lock);
546 if (val < 0)
547 return val;
548
549 if (val & 0x8000)
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300550 value->value.integer.value[0] = 0;
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300551 else
552 value->value.integer.value[0] = 1;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300553
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300554 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
555 (val & 0x8000) ? "muted " : "",
556 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
557 val, (int)kcontrol->private_value);
558
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300559 return 0;
560}
561
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300562static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
563
564static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
565 char *name, int id)
566{
567 int err;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300568 char ctl_name[44];
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300569 struct snd_kcontrol *kctl;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300570 struct snd_kcontrol_new tmp;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300571
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300572 memset (&tmp, 0, sizeof(tmp));
573 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
574 tmp.private_value = id,
575 tmp.name = ctl_name,
576
577 /* Add Mute Control */
578 sprintf(ctl_name, "%s Switch", name);
579 tmp.get = em28xx_vol_get_mute;
580 tmp.put = em28xx_vol_put_mute;
581 tmp.info = snd_ctl_boolean_mono_info;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300582 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300583 err = snd_ctl_add(card, kctl);
584 if (err < 0)
585 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300586 dprintk("Added control %s for ac97 volume control 0x%04x\n",
587 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300588
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300589 memset (&tmp, 0, sizeof(tmp));
590 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
591 tmp.private_value = id,
592 tmp.name = ctl_name,
593
594 /* Add Volume Control */
595 sprintf(ctl_name, "%s Volume", name);
596 tmp.get = em28xx_vol_get;
597 tmp.put = em28xx_vol_put;
598 tmp.info = em28xx_vol_info;
599 tmp.tlv.p = em28xx_db_scale,
600 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300601 err = snd_ctl_add(card, kctl);
602 if (err < 0)
603 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300604 dprintk("Added control %s for ac97 volume control 0x%04x\n",
605 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300606
607 return 0;
608}
609
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300610/*
611 * register/unregister code and data
612 */
Markus Rechbergera52932b2008-01-05 09:55:47 -0300613static struct snd_pcm_ops snd_em28xx_pcm_capture = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300614 .open = snd_em28xx_capture_open,
615 .close = snd_em28xx_pcm_close,
616 .ioctl = snd_pcm_lib_ioctl,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300617 .hw_params = snd_em28xx_hw_capture_params,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300618 .hw_free = snd_em28xx_hw_capture_free,
619 .prepare = snd_em28xx_prepare,
620 .trigger = snd_em28xx_capture_trigger,
621 .pointer = snd_em28xx_capture_pointer,
622 .page = snd_pcm_get_vmalloc_page,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300623};
624
Markus Rechbergera52932b2008-01-05 09:55:47 -0300625static int em28xx_audio_init(struct em28xx *dev)
626{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300627 struct em28xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300628 struct snd_pcm *pcm;
629 struct snd_card *card;
630 static int devnr;
Devin Heitmueller65c9fd42008-11-12 02:04:53 -0300631 int err;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300632
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300633 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300634 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300635 the device is expecting the snd-usb-audio module or
636 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300637 return 0;
638 }
639
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300640 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
641 "non standard usbaudio\n");
642 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
643 "Rechberger\n");
644
Takashi Iwai758021b2009-01-12 15:17:09 +0100645 err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
646 &card);
647 if (err < 0)
648 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300649
650 spin_lock_init(&adev->slock);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300651 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
652 if (err < 0) {
653 snd_card_free(card);
654 return err;
655 }
656
Markus Rechbergera52932b2008-01-05 09:55:47 -0300657 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
658 pcm->info_flags = 0;
659 pcm->private_data = dev;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300660 strcpy(pcm->name, "Empia 28xx Capture");
Nicola Soranzo7662b002009-02-19 13:41:56 -0300661
662 snd_card_set_dev(card, &dev->udev->dev);
Dan Carpenter4a3eaee2010-03-23 08:40:43 -0300663 strcpy(card->driver, "Em28xx-Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300664 strcpy(card->shortname, "Em28xx Audio");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300665 strcpy(card->longname, "Empia Em28xx Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300666
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300667 INIT_WORK(&dev->wq_trigger, audio_trigger);
668
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300669 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
670 em28xx_cvol_new(card, dev, "Video", AC97_VIDEO_VOL);
671 em28xx_cvol_new(card, dev, "Line In", AC97_LINEIN_VOL);
672 em28xx_cvol_new(card, dev, "Phone", AC97_PHONE_VOL);
673 em28xx_cvol_new(card, dev, "Microphone", AC97_PHONE_VOL);
674 em28xx_cvol_new(card, dev, "CD", AC97_CD_VOL);
675 em28xx_cvol_new(card, dev, "AUX", AC97_AUX_VOL);
676 em28xx_cvol_new(card, dev, "PCM", AC97_PCM_OUT_VOL);
677
678 em28xx_cvol_new(card, dev, "Master", AC97_MASTER_VOL);
679 em28xx_cvol_new(card, dev, "Line", AC97_LINE_LEVEL_VOL);
680 em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO_VOL);
681 em28xx_cvol_new(card, dev, "LFE", AC97_LFE_MASTER_VOL);
682 em28xx_cvol_new(card, dev, "Surround", AC97_SURR_MASTER_VOL);
683 }
684
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300685 err = snd_card_register(card);
686 if (err < 0) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300687 snd_card_free(card);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300688 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300689 }
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300690 adev->sndcard = card;
691 adev->udev = dev->udev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300692
Markus Rechbergera52932b2008-01-05 09:55:47 -0300693 return 0;
694}
695
696static int em28xx_audio_fini(struct em28xx *dev)
697{
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300698 if (dev == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300699 return 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300700
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300701 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300702 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300703 the device is expecting the snd-usb-audio module or
704 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300705 return 0;
706 }
707
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300708 if (dev->adev.sndcard) {
709 snd_card_free(dev->adev.sndcard);
710 dev->adev.sndcard = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300711 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300712
Markus Rechbergera52932b2008-01-05 09:55:47 -0300713 return 0;
714}
715
716static struct em28xx_ops audio_ops = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300717 .id = EM28XX_AUDIO,
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300718 .name = "Em28xx Audio Extension",
719 .init = em28xx_audio_init,
720 .fini = em28xx_audio_fini,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300721};
722
723static int __init em28xx_alsa_register(void)
724{
Markus Rechbergera52932b2008-01-05 09:55:47 -0300725 return em28xx_register_extension(&audio_ops);
726}
727
728static void __exit em28xx_alsa_unregister(void)
729{
730 em28xx_unregister_extension(&audio_ops);
731}
732
733MODULE_LICENSE("GPL");
734MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300735MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300736MODULE_DESCRIPTION("Em28xx Audio driver");
737
738module_init(em28xx_alsa_register);
739module_exit(em28xx_alsa_unregister);