blob: 2fdb66ee44ab7532b7e0d6a9bf3e043eddec8a87 [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 Chehab4f83e7b2011-06-17 15:15:12 -03006 * Copyright (C) 2007-2011 Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03007 * - Port to work with the in-kernel driver
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -03008 * - Cleanups, fixes, alsa-controls, etc.
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -03009 *
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>
Ezequiel García67b8d032012-06-11 15:17:23 -030045#include <sound/ac97_codec.h>
Markus Rechbergera52932b2008-01-05 09:55:47 -030046#include <media/v4l2-common.h>
47#include "em28xx.h"
48
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030049static int debug;
50module_param(debug, int, 0644);
51MODULE_PARM_DESC(debug, "activates debug info");
52
53#define dprintk(fmt, arg...) do { \
54 if (debug) \
55 printk(KERN_INFO "em28xx-audio %s: " fmt, \
Harvey Harrisond80e1342008-04-08 23:20:00 -030056 __func__, ##arg); \
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030057 } while (0)
58
Markus Rechbergera52932b2008-01-05 09:55:47 -030059static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
Markus Rechbergera52932b2008-01-05 09:55:47 -030060
Robert Krakoraaa5a1822009-02-08 13:09:11 -030061static int em28xx_deinit_isoc_audio(struct em28xx *dev)
Markus Rechbergera52932b2008-01-05 09:55:47 -030062{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030063 int i;
64
65 dprintk("Stopping isoc\n");
Douglas Schilling Landgraf3e099ba2009-02-08 10:45:34 -030066 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Robert Krakora9c062102009-01-25 13:08:07 -030067 if (!irqs_disabled())
68 usb_kill_urb(dev->adev.urb[i]);
69 else
70 usb_unlink_urb(dev->adev.urb[i]);
Robert Krakoraaa5a1822009-02-08 13:09:11 -030071
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -030072 usb_free_urb(dev->adev.urb[i]);
73 dev->adev.urb[i] = NULL;
Robert Krakora53d12e52009-01-16 11:23:25 -030074
Robert Krakora9c062102009-01-25 13:08:07 -030075 kfree(dev->adev.transfer_buffer[i]);
76 dev->adev.transfer_buffer[i] = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -030077 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030078
Markus Rechbergera52932b2008-01-05 09:55:47 -030079 return 0;
80}
81
Markus Rechbergera52932b2008-01-05 09:55:47 -030082static void em28xx_audio_isocirq(struct urb *urb)
83{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030084 struct em28xx *dev = urb->context;
85 int i;
86 unsigned int oldptr;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030087 int period_elapsed = 0;
88 int status;
89 unsigned char *cp;
90 unsigned int stride;
Markus Rechbergera52932b2008-01-05 09:55:47 -030091 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030092 struct snd_pcm_runtime *runtime;
Robert Krakoraaa5a1822009-02-08 13:09:11 -030093
94 switch (urb->status) {
95 case 0: /* success */
96 case -ETIMEDOUT: /* NAK */
97 break;
98 case -ECONNRESET: /* kill */
99 case -ENOENT:
100 case -ESHUTDOWN:
101 return;
102 default: /* error */
103 dprintk("urb completition error %d.\n", urb->status);
104 break;
105 }
106
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300107 if (atomic_read(&dev->stream_started) == 0)
108 return;
109
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300110 if (dev->adev.capture_pcm_substream) {
111 substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300112 runtime = substream->runtime;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300113 stride = runtime->frame_bits >> 3;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300114
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300115 for (i = 0; i < urb->number_of_packets; i++) {
116 int length =
117 urb->iso_frame_desc[i].actual_length / stride;
118 cp = (unsigned char *)urb->transfer_buffer +
119 urb->iso_frame_desc[i].offset;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300120
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300121 if (!length)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300122 continue;
123
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300124 oldptr = dev->adev.hwptr_done_capture;
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300125 if (oldptr + length >= runtime->buffer_size) {
126 unsigned int cnt =
127 runtime->buffer_size - oldptr;
128 memcpy(runtime->dma_area + oldptr * stride, cp,
129 cnt * stride);
130 memcpy(runtime->dma_area, cp + cnt * stride,
131 length * stride - cnt * stride);
132 } else {
133 memcpy(runtime->dma_area + oldptr * stride, cp,
134 length * stride);
135 }
136
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300137 snd_pcm_stream_lock(substream);
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300138
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300139 dev->adev.hwptr_done_capture += length;
140 if (dev->adev.hwptr_done_capture >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300141 runtime->buffer_size)
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300142 dev->adev.hwptr_done_capture -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300143 runtime->buffer_size;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300144
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300145 dev->adev.capture_transfer_done += length;
146 if (dev->adev.capture_transfer_done >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300147 runtime->period_size) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300148 dev->adev.capture_transfer_done -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300149 runtime->period_size;
150 period_elapsed = 1;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300151 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300152
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300153 snd_pcm_stream_unlock(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300154 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300155 if (period_elapsed)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300156 snd_pcm_period_elapsed(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300157 }
158 urb->status = 0;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300159
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300160 status = usb_submit_urb(urb, GFP_ATOMIC);
161 if (status < 0) {
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300162 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
163 status);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300164 }
165 return;
166}
167
Markus Rechbergera52932b2008-01-05 09:55:47 -0300168static int em28xx_init_audio_isoc(struct em28xx *dev)
169{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300170 int i, errCode;
171 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
172 EM28XX_AUDIO_MAX_PACKET_SIZE;
173
174 dprintk("Starting isoc transfers\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300175
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300176 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300177 struct urb *urb;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300178 int j, k;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300179
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300180 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
181 if (!dev->adev.transfer_buffer[i])
Markus Rechbergera52932b2008-01-05 09:55:47 -0300182 return -ENOMEM;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300183
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300184 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300185 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300186 if (!urb) {
187 em28xx_errdev("usb_alloc_urb failed!\n");
188 for (j = 0; j < i; j++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300189 usb_free_urb(dev->adev.urb[j]);
190 kfree(dev->adev.transfer_buffer[j]);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300191 }
Markus Rechbergera52932b2008-01-05 09:55:47 -0300192 return -ENOMEM;
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300193 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300194
195 urb->dev = dev->udev;
196 urb->context = dev;
Holger Nelson8ab33622011-12-28 18:55:41 -0300197 urb->pipe = usb_rcvisocpipe(dev->udev, EM28XX_EP_AUDIO);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300198 urb->transfer_flags = URB_ISO_ASAP;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300199 urb->transfer_buffer = dev->adev.transfer_buffer[i];
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300200 urb->interval = 1;
201 urb->complete = em28xx_audio_isocirq;
202 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
203 urb->transfer_buffer_length = sb_size;
204
205 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
206 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
207 urb->iso_frame_desc[j].offset = k;
208 urb->iso_frame_desc[j].length =
209 EM28XX_AUDIO_MAX_PACKET_SIZE;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300210 }
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300211 dev->adev.urb[i] = urb;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300212 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300213
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300214 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300215 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300216 if (errCode) {
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300217 em28xx_errdev("submit of audio urb failed\n");
Robert Krakoraaa5a1822009-02-08 13:09:11 -0300218 em28xx_deinit_isoc_audio(dev);
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300219 atomic_set(&dev->stream_started, 0);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300220 return errCode;
221 }
Mauro Carvalho Chehabdebb7242011-06-19 10:15:35 -0300222
Markus Rechbergera52932b2008-01-05 09:55:47 -0300223 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300224
Markus Rechbergera52932b2008-01-05 09:55:47 -0300225 return 0;
226}
227
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300228static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
229 size_t size)
230{
231 struct snd_pcm_runtime *runtime = subs->runtime;
232
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300233 dprintk("Allocating vbuffer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300234 if (runtime->dma_area) {
235 if (runtime->dma_bytes > size)
236 return 0;
237
238 vfree(runtime->dma_area);
239 }
240 runtime->dma_area = vmalloc(size);
241 if (!runtime->dma_area)
242 return -ENOMEM;
243
244 runtime->dma_bytes = size;
245
246 return 0;
247}
248
249static struct snd_pcm_hardware snd_em28xx_hw_capture = {
250 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
251 SNDRV_PCM_INFO_MMAP |
252 SNDRV_PCM_INFO_INTERLEAVED |
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300253 SNDRV_PCM_INFO_BATCH |
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300254 SNDRV_PCM_INFO_MMAP_VALID,
255
256 .formats = SNDRV_PCM_FMTBIT_S16_LE,
257
258 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
259
260 .rate_min = 48000,
261 .rate_max = 48000,
262 .channels_min = 2,
263 .channels_max = 2,
264 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
265 .period_bytes_min = 64, /* 12544/2, */
266 .period_bytes_max = 12544,
267 .periods_min = 2,
268 .periods_max = 98, /* 12544, */
269};
270
271static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
272{
273 struct em28xx *dev = snd_pcm_substream_chip(substream);
274 struct snd_pcm_runtime *runtime = substream->runtime;
275 int ret = 0;
276
277 dprintk("opening device and trying to acquire exclusive lock\n");
278
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300279 if (!dev) {
Filipe Rosset0bc23082009-11-04 15:31:25 -0300280 em28xx_err("BUG: em28xx can't find device struct."
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300281 " Can't proceed with open\n");
282 return -ENODEV;
283 }
284
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300285 runtime->hw = snd_em28xx_hw_capture;
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300286 if ((dev->alt == 0 || dev->audio_ifnum) && dev->adev.users == 0) {
287 if (dev->audio_ifnum)
288 dev->alt = 1;
289 else
290 dev->alt = 7;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300291
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300292 dprintk("changing alternate number on interface %d to %d\n",
293 dev->audio_ifnum, dev->alt);
294 usb_set_interface(dev->udev, dev->audio_ifnum, dev->alt);
295
296 /* Sets volume, mute, etc */
297 dev->mute = 0;
298 mutex_lock(&dev->lock);
299 ret = em28xx_audio_analog_set(dev);
300 if (ret < 0)
301 goto err;
302
303 dev->adev.users++;
304 mutex_unlock(&dev->lock);
305 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300306
307 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300308 dev->adev.capture_pcm_substream = substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300309
310 return 0;
311err:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300312 mutex_unlock(&dev->lock);
313
Filipe Rosset0bc23082009-11-04 15:31:25 -0300314 em28xx_err("Error while configuring em28xx mixer\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300315 return ret;
316}
317
318static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
319{
320 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300321
322 dprintk("closing device\n");
323
324 dev->mute = 1;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300325 mutex_lock(&dev->lock);
Douglas Schilling Landgrafbf510ac2009-02-08 01:11:13 -0300326 dev->adev.users--;
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300327 if (atomic_read(&dev->stream_started) > 0) {
328 atomic_set(&dev->stream_started, 0);
329 schedule_work(&dev->wq_trigger);
330 }
331
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300332 em28xx_audio_analog_set(dev);
Robert Krakora75c74d12009-05-28 11:16:19 -0300333 if (substream->runtime->dma_area) {
334 dprintk("freeing\n");
335 vfree(substream->runtime->dma_area);
336 substream->runtime->dma_area = NULL;
337 }
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300338 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300339
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300340 return 0;
341}
342
343static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
344 struct snd_pcm_hw_params *hw_params)
345{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300346 int ret;
347
348 dprintk("Setting capture parameters\n");
349
350 ret = snd_pcm_alloc_vmalloc_buffer(substream,
351 params_buffer_bytes(hw_params));
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300352 if (ret < 0)
353 return ret;
Hans Verkuile92ba282012-05-14 10:17:35 -0300354#if 0
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300355 /* TODO: set up em28xx audio chip to deliver the correct audio format,
356 current default is 48000hz multiplexed => 96000hz mono
357 which shouldn't matter since analogue TV only supports mono */
Hans Verkuile92ba282012-05-14 10:17:35 -0300358 unsigned int channels, rate, format;
359
360 format = params_format(hw_params);
361 rate = params_rate(hw_params);
362 channels = params_channels(hw_params);
363#endif
364
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300365 return 0;
366}
367
368static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
369{
370 struct em28xx *dev = snd_pcm_substream_chip(substream);
371
372 dprintk("Stop capture, if needed\n");
373
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300374 if (atomic_read(&dev->stream_started) > 0) {
375 atomic_set(&dev->stream_started, 0);
376 schedule_work(&dev->wq_trigger);
377 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300378
379 return 0;
380}
381
382static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
383{
Devin Heitmueller96fbf772009-10-15 01:14:34 -0300384 struct em28xx *dev = snd_pcm_substream_chip(substream);
385
386 dev->adev.hwptr_done_capture = 0;
387 dev->adev.capture_transfer_done = 0;
388
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300389 return 0;
390}
391
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300392static void audio_trigger(struct work_struct *work)
393{
394 struct em28xx *dev = container_of(work, struct em28xx, wq_trigger);
395
396 if (atomic_read(&dev->stream_started)) {
397 dprintk("starting capture");
398 em28xx_init_audio_isoc(dev);
399 } else {
400 dprintk("stopping capture");
401 em28xx_deinit_isoc_audio(dev);
402 }
403}
404
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300405static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
406 int cmd)
407{
408 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300409 int retval = 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300410
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300411 switch (cmd) {
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300412 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
413 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300414 case SNDRV_PCM_TRIGGER_START:
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300415 atomic_set(&dev->stream_started, 1);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300416 break;
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300417 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
418 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300419 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehabdff0f8c2011-06-19 13:39:31 -0300420 atomic_set(&dev->stream_started, 0);
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300421 break;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300422 default:
Douglas Schilling Landgrafdf39ca62009-02-08 14:17:15 -0300423 retval = -EINVAL;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300424 }
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300425 schedule_work(&dev->wq_trigger);
Mauro Carvalho Chehab00d2e7a2011-06-17 20:28:51 -0300426 return retval;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300427}
428
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300429static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
430 *substream)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300431{
Nicola Soranzoa1a6ee72009-02-10 23:28:24 -0300432 unsigned long flags;
Robert Krakora53d12e52009-01-16 11:23:25 -0300433 struct em28xx *dev;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300434 snd_pcm_uframes_t hwptr_done;
Robert Krakora53d12e52009-01-16 11:23:25 -0300435
Markus Rechbergera52932b2008-01-05 09:55:47 -0300436 dev = snd_pcm_substream_chip(substream);
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300437 spin_lock_irqsave(&dev->adev.slock, flags);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300438 hwptr_done = dev->adev.hwptr_done_capture;
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300439 spin_unlock_irqrestore(&dev->adev.slock, flags);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300440
Markus Rechbergera52932b2008-01-05 09:55:47 -0300441 return hwptr_done;
442}
443
444static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
445 unsigned long offset)
446{
447 void *pageptr = subs->runtime->dma_area + offset;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300448
Markus Rechbergera52932b2008-01-05 09:55:47 -0300449 return vmalloc_to_page(pageptr);
450}
451
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300452/*
453 * AC97 volume control support
454 */
455static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
456 struct snd_ctl_elem_info *info)
457{
458 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
459 info->count = 2;
460 info->value.integer.min = 0;
461 info->value.integer.max = 0x1f;
462
463 return 0;
464}
465
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300466static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
467 struct snd_ctl_elem_value *value)
468{
469 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300470 u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) |
471 (0x1f - (value->value.integer.value[1] & 0x1f)) << 8;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300472 int rc;
473
474 mutex_lock(&dev->lock);
475 rc = em28xx_read_ac97(dev, kcontrol->private_value);
476 if (rc < 0)
477 goto err;
478
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300479 val |= rc & 0x8000; /* Preserve the mute flag */
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300480
481 rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300482 if (rc < 0)
483 goto err;
484
485 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
486 (val & 0x8000) ? "muted " : "",
487 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
488 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300489
490err:
491 mutex_unlock(&dev->lock);
492 return rc;
493}
494
495static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
496 struct snd_ctl_elem_value *value)
497{
498 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
499 int val;
500
501 mutex_lock(&dev->lock);
502 val = em28xx_read_ac97(dev, kcontrol->private_value);
503 mutex_unlock(&dev->lock);
504 if (val < 0)
505 return val;
506
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300507 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
508 (val & 0x8000) ? "muted " : "",
509 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
510 val, (int)kcontrol->private_value);
511
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300512 value->value.integer.value[0] = 0x1f - (val & 0x1f);
513 value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300514
515 return 0;
516}
517
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300518static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol,
519 struct snd_ctl_elem_value *value)
520{
521 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
522 u16 val = value->value.integer.value[0];
523 int rc;
524
525 mutex_lock(&dev->lock);
526 rc = em28xx_read_ac97(dev, kcontrol->private_value);
527 if (rc < 0)
528 goto err;
529
530 if (val)
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300531 rc &= 0x1f1f;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300532 else
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300533 rc |= 0x8000;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300534
535 rc = em28xx_write_ac97(dev, kcontrol->private_value, rc);
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300536 if (rc < 0)
537 goto err;
538
539 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
540 (val & 0x8000) ? "muted " : "",
541 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
542 val, (int)kcontrol->private_value);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300543
544err:
545 mutex_unlock(&dev->lock);
546 return rc;
547}
548
549static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol,
550 struct snd_ctl_elem_value *value)
551{
552 struct em28xx *dev = snd_kcontrol_chip(kcontrol);
553 int val;
554
555 mutex_lock(&dev->lock);
556 val = em28xx_read_ac97(dev, kcontrol->private_value);
557 mutex_unlock(&dev->lock);
558 if (val < 0)
559 return val;
560
561 if (val & 0x8000)
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300562 value->value.integer.value[0] = 0;
Mauro Carvalho Chehab71d7d832011-06-19 13:14:07 -0300563 else
564 value->value.integer.value[0] = 1;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300565
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300566 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
567 (val & 0x8000) ? "muted " : "",
568 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f),
569 val, (int)kcontrol->private_value);
570
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300571 return 0;
572}
573
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300574static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
575
576static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
577 char *name, int id)
578{
579 int err;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300580 char ctl_name[44];
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300581 struct snd_kcontrol *kctl;
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300582 struct snd_kcontrol_new tmp;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300583
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300584 memset (&tmp, 0, sizeof(tmp));
585 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
586 tmp.private_value = id,
587 tmp.name = ctl_name,
588
589 /* Add Mute Control */
590 sprintf(ctl_name, "%s Switch", name);
591 tmp.get = em28xx_vol_get_mute;
592 tmp.put = em28xx_vol_put_mute;
593 tmp.info = snd_ctl_boolean_mono_info;
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300594 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300595 err = snd_ctl_add(card, kctl);
596 if (err < 0)
597 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300598 dprintk("Added control %s for ac97 volume control 0x%04x\n",
599 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300600
Mauro Carvalho Chehab43131a22011-06-19 13:08:46 -0300601 memset (&tmp, 0, sizeof(tmp));
602 tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
603 tmp.private_value = id,
604 tmp.name = ctl_name,
605
606 /* Add Volume Control */
607 sprintf(ctl_name, "%s Volume", name);
608 tmp.get = em28xx_vol_get;
609 tmp.put = em28xx_vol_put;
610 tmp.info = em28xx_vol_info;
611 tmp.tlv.p = em28xx_db_scale,
612 kctl = snd_ctl_new1(&tmp, dev);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300613 err = snd_ctl_add(card, kctl);
614 if (err < 0)
615 return err;
Mauro Carvalho Chehab66cb6952011-06-18 11:00:20 -0300616 dprintk("Added control %s for ac97 volume control 0x%04x\n",
617 ctl_name, id);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300618
619 return 0;
620}
621
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300622/*
623 * register/unregister code and data
624 */
Markus Rechbergera52932b2008-01-05 09:55:47 -0300625static struct snd_pcm_ops snd_em28xx_pcm_capture = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300626 .open = snd_em28xx_capture_open,
627 .close = snd_em28xx_pcm_close,
628 .ioctl = snd_pcm_lib_ioctl,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300629 .hw_params = snd_em28xx_hw_capture_params,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300630 .hw_free = snd_em28xx_hw_capture_free,
631 .prepare = snd_em28xx_prepare,
632 .trigger = snd_em28xx_capture_trigger,
633 .pointer = snd_em28xx_capture_pointer,
634 .page = snd_pcm_get_vmalloc_page,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300635};
636
Markus Rechbergera52932b2008-01-05 09:55:47 -0300637static int em28xx_audio_init(struct em28xx *dev)
638{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300639 struct em28xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300640 struct snd_pcm *pcm;
641 struct snd_card *card;
642 static int devnr;
Devin Heitmueller65c9fd42008-11-12 02:04:53 -0300643 int err;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300644
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300645 if (!dev->has_alsa_audio || dev->audio_ifnum < 0) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300646 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300647 the device is expecting the snd-usb-audio module or
648 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300649 return 0;
650 }
651
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300652 printk(KERN_INFO "em28xx-audio.c: probing for em28xx Audio Vendor Class\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300653 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
654 "Rechberger\n");
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300655 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2007-2011 Mauro Carvalho Chehab\n");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300656
Takashi Iwai758021b2009-01-12 15:17:09 +0100657 err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
658 &card);
659 if (err < 0)
660 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300661
662 spin_lock_init(&adev->slock);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300663 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
664 if (err < 0) {
665 snd_card_free(card);
666 return err;
667 }
668
Markus Rechbergera52932b2008-01-05 09:55:47 -0300669 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
670 pcm->info_flags = 0;
671 pcm->private_data = dev;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300672 strcpy(pcm->name, "Empia 28xx Capture");
Nicola Soranzo7662b002009-02-19 13:41:56 -0300673
674 snd_card_set_dev(card, &dev->udev->dev);
Dan Carpenter4a3eaee2010-03-23 08:40:43 -0300675 strcpy(card->driver, "Em28xx-Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300676 strcpy(card->shortname, "Em28xx Audio");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300677 strcpy(card->longname, "Empia Em28xx Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300678
Mauro Carvalho Chehab39a96b42010-10-09 15:53:58 -0300679 INIT_WORK(&dev->wq_trigger, audio_trigger);
680
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300681 if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
Ezequiel García67b8d032012-06-11 15:17:23 -0300682 em28xx_cvol_new(card, dev, "Video", AC97_VIDEO);
683 em28xx_cvol_new(card, dev, "Line In", AC97_LINE);
684 em28xx_cvol_new(card, dev, "Phone", AC97_PHONE);
685 em28xx_cvol_new(card, dev, "Microphone", AC97_MIC);
686 em28xx_cvol_new(card, dev, "CD", AC97_CD);
687 em28xx_cvol_new(card, dev, "AUX", AC97_AUX);
688 em28xx_cvol_new(card, dev, "PCM", AC97_PCM);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300689
Ezequiel García67b8d032012-06-11 15:17:23 -0300690 em28xx_cvol_new(card, dev, "Master", AC97_MASTER);
691 em28xx_cvol_new(card, dev, "Line", AC97_HEADPHONE);
692 em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO);
693 em28xx_cvol_new(card, dev, "LFE", AC97_CENTER_LFE_MASTER);
694 em28xx_cvol_new(card, dev, "Surround", AC97_SURROUND_MASTER);
Mauro Carvalho Chehab850d24a2011-06-19 13:06:40 -0300695 }
696
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300697 err = snd_card_register(card);
698 if (err < 0) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300699 snd_card_free(card);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300700 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300701 }
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300702 adev->sndcard = card;
703 adev->udev = dev->udev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300704
Markus Rechbergera52932b2008-01-05 09:55:47 -0300705 return 0;
706}
707
708static int em28xx_audio_fini(struct em28xx *dev)
709{
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300710 if (dev == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300711 return 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300712
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300713 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300714 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300715 the device is expecting the snd-usb-audio module or
716 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300717 return 0;
718 }
719
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300720 if (dev->adev.sndcard) {
721 snd_card_free(dev->adev.sndcard);
722 dev->adev.sndcard = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300723 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300724
Markus Rechbergera52932b2008-01-05 09:55:47 -0300725 return 0;
726}
727
728static struct em28xx_ops audio_ops = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300729 .id = EM28XX_AUDIO,
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300730 .name = "Em28xx Audio Extension",
731 .init = em28xx_audio_init,
732 .fini = em28xx_audio_fini,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300733};
734
735static int __init em28xx_alsa_register(void)
736{
Markus Rechbergera52932b2008-01-05 09:55:47 -0300737 return em28xx_register_extension(&audio_ops);
738}
739
740static void __exit em28xx_alsa_unregister(void)
741{
742 em28xx_unregister_extension(&audio_ops);
743}
744
745MODULE_LICENSE("GPL");
746MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
Mauro Carvalho Chehab4f83e7b2011-06-17 15:15:12 -0300747MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300748MODULE_DESCRIPTION("Em28xx Audio driver");
749
750module_init(em28xx_alsa_register);
751module_exit(em28xx_alsa_unregister);