blob: 2ac738fa6a07e0cd66fac559fd3c9688060dff39 [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>
Markus Rechbergera52932b2008-01-05 09:55:47 -030044#include <media/v4l2-common.h>
45#include "em28xx.h"
46
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030047static int debug;
48module_param(debug, int, 0644);
49MODULE_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 Harrisond80e1342008-04-08 23:20:00 -030054 __func__, ##arg); \
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030055 } while (0)
56
Markus Rechbergera52932b2008-01-05 09:55:47 -030057static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
Markus Rechbergera52932b2008-01-05 09:55:47 -030058
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030059static int em28xx_isoc_audio_deinit(struct em28xx *dev)
Markus Rechbergera52932b2008-01-05 09:55:47 -030060{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030061 int i;
62
63 dprintk("Stopping isoc\n");
64 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Robert Krakora9c062102009-01-25 13:08:07 -030065 if (!irqs_disabled())
66 usb_kill_urb(dev->adev.urb[i]);
67 else
68 usb_unlink_urb(dev->adev.urb[i]);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -030069 usb_free_urb(dev->adev.urb[i]);
70 dev->adev.urb[i] = NULL;
Robert Krakora53d12e52009-01-16 11:23:25 -030071
Robert Krakora9c062102009-01-25 13:08:07 -030072 kfree(dev->adev.transfer_buffer[i]);
73 dev->adev.transfer_buffer[i] = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -030074 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030075
Markus Rechbergera52932b2008-01-05 09:55:47 -030076 return 0;
77}
78
Markus Rechbergera52932b2008-01-05 09:55:47 -030079static void em28xx_audio_isocirq(struct urb *urb)
80{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030081 struct em28xx *dev = urb->context;
82 int i;
83 unsigned int oldptr;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030084 int period_elapsed = 0;
85 int status;
86 unsigned char *cp;
87 unsigned int stride;
Markus Rechbergera52932b2008-01-05 09:55:47 -030088 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030089 struct snd_pcm_runtime *runtime;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -030090 if (dev->adev.capture_pcm_substream) {
91 substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -030092 runtime = substream->runtime;
Markus Rechbergera52932b2008-01-05 09:55:47 -030093 stride = runtime->frame_bits >> 3;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030094
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -030095 for (i = 0; i < urb->number_of_packets; i++) {
96 int length =
97 urb->iso_frame_desc[i].actual_length / stride;
98 cp = (unsigned char *)urb->transfer_buffer +
99 urb->iso_frame_desc[i].offset;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300100
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300101 if (!length)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300102 continue;
103
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300104 oldptr = dev->adev.hwptr_done_capture;
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300105 if (oldptr + length >= runtime->buffer_size) {
106 unsigned int cnt =
107 runtime->buffer_size - oldptr;
108 memcpy(runtime->dma_area + oldptr * stride, cp,
109 cnt * stride);
110 memcpy(runtime->dma_area, cp + cnt * stride,
111 length * stride - cnt * stride);
112 } else {
113 memcpy(runtime->dma_area + oldptr * stride, cp,
114 length * stride);
115 }
116
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300117 snd_pcm_stream_lock(substream);
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300118
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300119 dev->adev.hwptr_done_capture += length;
120 if (dev->adev.hwptr_done_capture >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300121 runtime->buffer_size)
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300122 dev->adev.hwptr_done_capture -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300123 runtime->buffer_size;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300124
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300125 dev->adev.capture_transfer_done += length;
126 if (dev->adev.capture_transfer_done >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300127 runtime->period_size) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300128 dev->adev.capture_transfer_done -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300129 runtime->period_size;
130 period_elapsed = 1;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300131 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300132
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300133 snd_pcm_stream_unlock(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300134 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300135 if (period_elapsed)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300136 snd_pcm_period_elapsed(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300137 }
138 urb->status = 0;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300139
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300140 if (dev->adev.shutdown)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300141 return;
142
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300143 status = usb_submit_urb(urb, GFP_ATOMIC);
144 if (status < 0) {
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300145 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
146 status);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300147 }
148 return;
149}
150
Markus Rechbergera52932b2008-01-05 09:55:47 -0300151static int em28xx_init_audio_isoc(struct em28xx *dev)
152{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300153 int i, errCode;
154 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
155 EM28XX_AUDIO_MAX_PACKET_SIZE;
156
157 dprintk("Starting isoc transfers\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300158
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300159 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300160 struct urb *urb;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300161 int j, k;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300162
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300163 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
164 if (!dev->adev.transfer_buffer[i])
Markus Rechbergera52932b2008-01-05 09:55:47 -0300165 return -ENOMEM;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300166
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300167 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300168 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300169 if (!urb) {
170 em28xx_errdev("usb_alloc_urb failed!\n");
171 for (j = 0; j < i; j++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300172 usb_free_urb(dev->adev.urb[j]);
173 kfree(dev->adev.transfer_buffer[j]);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300174 }
Markus Rechbergera52932b2008-01-05 09:55:47 -0300175 return -ENOMEM;
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300176 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300177
178 urb->dev = dev->udev;
179 urb->context = dev;
180 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
181 urb->transfer_flags = URB_ISO_ASAP;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300182 urb->transfer_buffer = dev->adev.transfer_buffer[i];
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300183 urb->interval = 1;
184 urb->complete = em28xx_audio_isocirq;
185 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
186 urb->transfer_buffer_length = sb_size;
187
188 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
189 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
190 urb->iso_frame_desc[j].offset = k;
191 urb->iso_frame_desc[j].length =
192 EM28XX_AUDIO_MAX_PACKET_SIZE;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300193 }
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300194 dev->adev.urb[i] = urb;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300195 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300196
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300197 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300198 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300199 if (errCode) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300200 em28xx_isoc_audio_deinit(dev);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300201
Markus Rechbergera52932b2008-01-05 09:55:47 -0300202 return errCode;
203 }
204 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300205
Markus Rechbergera52932b2008-01-05 09:55:47 -0300206 return 0;
207}
208
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300209static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300210{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300211 dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300212 "stop" : "start");
213
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300214 switch (cmd) {
215 case EM28XX_CAPTURE_STREAM_EN:
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300216 if (dev->adev.capture_stream == STREAM_OFF && arg == 1) {
217 dev->adev.capture_stream = STREAM_ON;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300218 em28xx_init_audio_isoc(dev);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300219 } else if (dev->adev.capture_stream == STREAM_ON && arg == 0) {
220 dev->adev.capture_stream = STREAM_OFF;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300221 em28xx_isoc_audio_deinit(dev);
222 } else {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300223 printk(KERN_ERR "An underrun very likely occurred. "
224 "Ignoring it.\n");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300225 }
226 return 0;
227 default:
228 return -EINVAL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300229 }
230}
231
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300232static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
233 size_t size)
234{
235 struct snd_pcm_runtime *runtime = subs->runtime;
236
237 dprintk("Alocating vbuffer\n");
238 if (runtime->dma_area) {
239 if (runtime->dma_bytes > size)
240 return 0;
241
242 vfree(runtime->dma_area);
243 }
244 runtime->dma_area = vmalloc(size);
245 if (!runtime->dma_area)
246 return -ENOMEM;
247
248 runtime->dma_bytes = size;
249
250 return 0;
251}
252
253static struct snd_pcm_hardware snd_em28xx_hw_capture = {
254 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
255 SNDRV_PCM_INFO_MMAP |
256 SNDRV_PCM_INFO_INTERLEAVED |
257 SNDRV_PCM_INFO_MMAP_VALID,
258
259 .formats = SNDRV_PCM_FMTBIT_S16_LE,
260
261 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
262
263 .rate_min = 48000,
264 .rate_max = 48000,
265 .channels_min = 2,
266 .channels_max = 2,
267 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
268 .period_bytes_min = 64, /* 12544/2, */
269 .period_bytes_max = 12544,
270 .periods_min = 2,
271 .periods_max = 98, /* 12544, */
272};
273
274static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
275{
276 struct em28xx *dev = snd_pcm_substream_chip(substream);
277 struct snd_pcm_runtime *runtime = substream->runtime;
278 int ret = 0;
279
280 dprintk("opening device and trying to acquire exclusive lock\n");
281
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300282 if (!dev) {
283 printk(KERN_ERR "BUG: em28xx can't find device struct."
284 " Can't proceed with open\n");
285 return -ENODEV;
286 }
287
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300288 /* Sets volume, mute, etc */
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300289
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300290 dev->mute = 0;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300291 mutex_lock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300292 ret = em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300293 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300294 if (ret < 0)
295 goto err;
296
297 runtime->hw = snd_em28xx_hw_capture;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300298 if (dev->alt == 0 && dev->adev.users == 0) {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300299 int errCode;
300 dev->alt = 7;
301 errCode = usb_set_interface(dev->udev, 0, 7);
302 dprintk("changing alternate number to 7\n");
303 }
304
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300305 dev->adev.users++;
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 runtime->private_data = dev;
310
311 return 0;
312err:
313 printk(KERN_ERR "Error while configuring em28xx mixer\n");
314 return ret;
315}
316
317static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
318{
319 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300320 dev->adev.users--;
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);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300326 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300327 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300328
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300329 if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
330 dprintk("audio users: %d\n", dev->adev.users);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300331 dprintk("disabling audio stream!\n");
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300332 dev->adev.shutdown = 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300333 dprintk("released lock\n");
334 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
335 }
336 return 0;
337}
338
339static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
340 struct snd_pcm_hw_params *hw_params)
341{
342 unsigned int channels, rate, format;
343 int ret;
344
345 dprintk("Setting capture parameters\n");
346
347 ret = snd_pcm_alloc_vmalloc_buffer(substream,
348 params_buffer_bytes(hw_params));
349 format = params_format(hw_params);
350 rate = params_rate(hw_params);
351 channels = params_channels(hw_params);
352
353 /* TODO: set up em28xx audio chip to deliver the correct audio format,
354 current default is 48000hz multiplexed => 96000hz mono
355 which shouldn't matter since analogue TV only supports mono */
356 return 0;
357}
358
359static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
360{
361 struct em28xx *dev = snd_pcm_substream_chip(substream);
362
363 dprintk("Stop capture, if needed\n");
364
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300365 if (dev->adev.capture_stream == STREAM_ON)
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300366 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
367
368 return 0;
369}
370
371static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
372{
373 return 0;
374}
375
376static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
377 int cmd)
378{
379 struct em28xx *dev = snd_pcm_substream_chip(substream);
380
381 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
382 "start": "stop");
383 switch (cmd) {
384 case SNDRV_PCM_TRIGGER_START:
385 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
386 return 0;
387 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300388 dev->adev.shutdown = 1;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300389 return 0;
390 default:
391 return -EINVAL;
392 }
393}
394
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300395static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
396 *substream)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300397{
Robert Krakora53d12e52009-01-16 11:23:25 -0300398 unsigned long flags;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300399
Robert Krakora53d12e52009-01-16 11:23:25 -0300400 struct em28xx *dev;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300401 snd_pcm_uframes_t hwptr_done;
Robert Krakora53d12e52009-01-16 11:23:25 -0300402
Markus Rechbergera52932b2008-01-05 09:55:47 -0300403 dev = snd_pcm_substream_chip(substream);
Robert Krakora53d12e52009-01-16 11:23:25 -0300404 spin_lock_irqsave(&dev->adev.slock, flags);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300405 hwptr_done = dev->adev.hwptr_done_capture;
Robert Krakora53d12e52009-01-16 11:23:25 -0300406 spin_unlock_irqrestore(&dev->adev.slock, flags);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300407
Markus Rechbergera52932b2008-01-05 09:55:47 -0300408 return hwptr_done;
409}
410
411static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
412 unsigned long offset)
413{
414 void *pageptr = subs->runtime->dma_area + offset;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300415
Markus Rechbergera52932b2008-01-05 09:55:47 -0300416 return vmalloc_to_page(pageptr);
417}
418
419static struct snd_pcm_ops snd_em28xx_pcm_capture = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300420 .open = snd_em28xx_capture_open,
421 .close = snd_em28xx_pcm_close,
422 .ioctl = snd_pcm_lib_ioctl,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300423 .hw_params = snd_em28xx_hw_capture_params,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300424 .hw_free = snd_em28xx_hw_capture_free,
425 .prepare = snd_em28xx_prepare,
426 .trigger = snd_em28xx_capture_trigger,
427 .pointer = snd_em28xx_capture_pointer,
428 .page = snd_pcm_get_vmalloc_page,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300429};
430
Markus Rechbergera52932b2008-01-05 09:55:47 -0300431static int em28xx_audio_init(struct em28xx *dev)
432{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300433 struct em28xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300434 struct snd_pcm *pcm;
435 struct snd_card *card;
436 static int devnr;
Devin Heitmueller65c9fd42008-11-12 02:04:53 -0300437 int err;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300438
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300439 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300440 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300441 the device is expecting the snd-usb-audio module or
442 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300443 return 0;
444 }
445
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300446 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
447 "non standard usbaudio\n");
448 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
449 "Rechberger\n");
450
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300451 card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300452 if (card == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300453 return -ENOMEM;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300454
455 spin_lock_init(&adev->slock);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300456 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
457 if (err < 0) {
458 snd_card_free(card);
459 return err;
460 }
461
Markus Rechbergera52932b2008-01-05 09:55:47 -0300462 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
463 pcm->info_flags = 0;
464 pcm->private_data = dev;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300465 strcpy(pcm->name, "Empia 28xx Capture");
Nicola Soranzo7662b002009-02-19 13:41:56 -0300466
467 snd_card_set_dev(card, &dev->udev->dev);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300468 strcpy(card->driver, "Empia Em28xx Audio");
469 strcpy(card->shortname, "Em28xx Audio");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300470 strcpy(card->longname, "Empia Em28xx Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300471
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300472 err = snd_card_register(card);
473 if (err < 0) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300474 snd_card_free(card);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300475 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300476 }
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300477 adev->sndcard = card;
478 adev->udev = dev->udev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300479
Markus Rechbergera52932b2008-01-05 09:55:47 -0300480 return 0;
481}
482
483static int em28xx_audio_fini(struct em28xx *dev)
484{
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300485 if (dev == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300486 return 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300487
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300488 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300489 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300490 the device is expecting the snd-usb-audio module or
491 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300492 return 0;
493 }
494
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300495 if (dev->adev.sndcard) {
496 snd_card_free(dev->adev.sndcard);
497 dev->adev.sndcard = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300498 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300499
Markus Rechbergera52932b2008-01-05 09:55:47 -0300500 return 0;
501}
502
503static struct em28xx_ops audio_ops = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300504 .id = EM28XX_AUDIO,
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300505 .name = "Em28xx Audio Extension",
506 .init = em28xx_audio_init,
507 .fini = em28xx_audio_fini,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300508};
509
510static int __init em28xx_alsa_register(void)
511{
Markus Rechbergera52932b2008-01-05 09:55:47 -0300512 return em28xx_register_extension(&audio_ops);
513}
514
515static void __exit em28xx_alsa_unregister(void)
516{
517 em28xx_unregister_extension(&audio_ops);
518}
519
520MODULE_LICENSE("GPL");
521MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300522MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300523MODULE_DESCRIPTION("Em28xx Audio driver");
524
525module_init(em28xx_alsa_register);
526module_exit(em28xx_alsa_unregister);