blob: c698d3c9690fc13942e5360074d118ef4e5a439a [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
Robert Krakoraaa5a1822009-02-08 13:09:11 -030059static int em28xx_deinit_isoc_audio(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");
Douglas Schilling Landgraf3e099ba2009-02-08 10:45:34 -030064 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]);
Robert Krakoraaa5a1822009-02-08 13:09:11 -030069
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -030070 usb_free_urb(dev->adev.urb[i]);
71 dev->adev.urb[i] = NULL;
Robert Krakora53d12e52009-01-16 11:23:25 -030072
Robert Krakora9c062102009-01-25 13:08:07 -030073 kfree(dev->adev.transfer_buffer[i]);
74 dev->adev.transfer_buffer[i] = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -030075 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030076
Markus Rechbergera52932b2008-01-05 09:55:47 -030077 return 0;
78}
79
Markus Rechbergera52932b2008-01-05 09:55:47 -030080static void em28xx_audio_isocirq(struct urb *urb)
81{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030082 struct em28xx *dev = urb->context;
83 int i;
84 unsigned int oldptr;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030085 int period_elapsed = 0;
86 int status;
87 unsigned char *cp;
88 unsigned int stride;
Markus Rechbergera52932b2008-01-05 09:55:47 -030089 struct snd_pcm_substream *substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -030090 struct snd_pcm_runtime *runtime;
Robert Krakoraaa5a1822009-02-08 13:09:11 -030091
92 switch (urb->status) {
93 case 0: /* success */
94 case -ETIMEDOUT: /* NAK */
95 break;
96 case -ECONNRESET: /* kill */
97 case -ENOENT:
98 case -ESHUTDOWN:
99 return;
100 default: /* error */
101 dprintk("urb completition error %d.\n", urb->status);
102 break;
103 }
104
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300105 if (dev->adev.capture_pcm_substream) {
106 substream = dev->adev.capture_pcm_substream;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300107 runtime = substream->runtime;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300108 stride = runtime->frame_bits >> 3;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300109
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300110 for (i = 0; i < urb->number_of_packets; i++) {
111 int length =
112 urb->iso_frame_desc[i].actual_length / stride;
113 cp = (unsigned char *)urb->transfer_buffer +
114 urb->iso_frame_desc[i].offset;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300115
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300116 if (!length)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300117 continue;
118
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300119 oldptr = dev->adev.hwptr_done_capture;
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300120 if (oldptr + length >= runtime->buffer_size) {
121 unsigned int cnt =
122 runtime->buffer_size - oldptr;
123 memcpy(runtime->dma_area + oldptr * stride, cp,
124 cnt * stride);
125 memcpy(runtime->dma_area, cp + cnt * stride,
126 length * stride - cnt * stride);
127 } else {
128 memcpy(runtime->dma_area + oldptr * stride, cp,
129 length * stride);
130 }
131
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300132 snd_pcm_stream_lock(substream);
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300133
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300134 dev->adev.hwptr_done_capture += length;
135 if (dev->adev.hwptr_done_capture >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300136 runtime->buffer_size)
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300137 dev->adev.hwptr_done_capture -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300138 runtime->buffer_size;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300139
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300140 dev->adev.capture_transfer_done += length;
141 if (dev->adev.capture_transfer_done >=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300142 runtime->period_size) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300143 dev->adev.capture_transfer_done -=
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300144 runtime->period_size;
145 period_elapsed = 1;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300146 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300147
Mauro Carvalho Chehab50f3beb2008-11-24 08:45:57 -0300148 snd_pcm_stream_unlock(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300149 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300150 if (period_elapsed)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300151 snd_pcm_period_elapsed(substream);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300152 }
153 urb->status = 0;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300154
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300155 if (dev->adev.shutdown)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300156 return;
157
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300158 status = usb_submit_urb(urb, GFP_ATOMIC);
159 if (status < 0) {
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300160 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
161 status);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300162 }
163 return;
164}
165
Markus Rechbergera52932b2008-01-05 09:55:47 -0300166static int em28xx_init_audio_isoc(struct em28xx *dev)
167{
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300168 int i, errCode;
169 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
170 EM28XX_AUDIO_MAX_PACKET_SIZE;
171
172 dprintk("Starting isoc transfers\n");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300173
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300174 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300175 struct urb *urb;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300176 int j, k;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300177
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300178 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
179 if (!dev->adev.transfer_buffer[i])
Markus Rechbergera52932b2008-01-05 09:55:47 -0300180 return -ENOMEM;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300181
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300182 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300183 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300184 if (!urb) {
185 em28xx_errdev("usb_alloc_urb failed!\n");
186 for (j = 0; j < i; j++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300187 usb_free_urb(dev->adev.urb[j]);
188 kfree(dev->adev.transfer_buffer[j]);
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300189 }
Markus Rechbergera52932b2008-01-05 09:55:47 -0300190 return -ENOMEM;
Douglas Schilling Landgrafff9b3e42008-09-04 11:20:20 -0300191 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300192
193 urb->dev = dev->udev;
194 urb->context = dev;
195 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
196 urb->transfer_flags = URB_ISO_ASAP;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300197 urb->transfer_buffer = dev->adev.transfer_buffer[i];
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300198 urb->interval = 1;
199 urb->complete = em28xx_audio_isocirq;
200 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
201 urb->transfer_buffer_length = sb_size;
202
203 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
204 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
205 urb->iso_frame_desc[j].offset = k;
206 urb->iso_frame_desc[j].length =
207 EM28XX_AUDIO_MAX_PACKET_SIZE;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300208 }
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300209 dev->adev.urb[i] = urb;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300210 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300211
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300212 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300213 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300214 if (errCode) {
Robert Krakoraaa5a1822009-02-08 13:09:11 -0300215 em28xx_deinit_isoc_audio(dev);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300216 return errCode;
217 }
218 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300219
Markus Rechbergera52932b2008-01-05 09:55:47 -0300220 return 0;
221}
222
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300223static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300224{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300225 dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300226 "stop" : "start");
227
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300228 switch (cmd) {
229 case EM28XX_CAPTURE_STREAM_EN:
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300230 if (dev->adev.capture_stream == STREAM_OFF && arg == 1) {
231 dev->adev.capture_stream = STREAM_ON;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300232 em28xx_init_audio_isoc(dev);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300233 } else if (dev->adev.capture_stream == STREAM_ON && arg == 0) {
234 dev->adev.capture_stream = STREAM_OFF;
Robert Krakoraaa5a1822009-02-08 13:09:11 -0300235 em28xx_deinit_isoc_audio(dev);
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300236 } else {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300237 printk(KERN_ERR "An underrun very likely occurred. "
238 "Ignoring it.\n");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300239 }
240 return 0;
241 default:
242 return -EINVAL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300243 }
244}
245
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300246static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
247 size_t size)
248{
249 struct snd_pcm_runtime *runtime = subs->runtime;
250
251 dprintk("Alocating vbuffer\n");
252 if (runtime->dma_area) {
253 if (runtime->dma_bytes > size)
254 return 0;
255
256 vfree(runtime->dma_area);
257 }
258 runtime->dma_area = vmalloc(size);
259 if (!runtime->dma_area)
260 return -ENOMEM;
261
262 runtime->dma_bytes = size;
263
264 return 0;
265}
266
267static struct snd_pcm_hardware snd_em28xx_hw_capture = {
268 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
269 SNDRV_PCM_INFO_MMAP |
270 SNDRV_PCM_INFO_INTERLEAVED |
271 SNDRV_PCM_INFO_MMAP_VALID,
272
273 .formats = SNDRV_PCM_FMTBIT_S16_LE,
274
275 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
276
277 .rate_min = 48000,
278 .rate_max = 48000,
279 .channels_min = 2,
280 .channels_max = 2,
281 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
282 .period_bytes_min = 64, /* 12544/2, */
283 .period_bytes_max = 12544,
284 .periods_min = 2,
285 .periods_max = 98, /* 12544, */
286};
287
288static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
289{
290 struct em28xx *dev = snd_pcm_substream_chip(substream);
291 struct snd_pcm_runtime *runtime = substream->runtime;
292 int ret = 0;
293
294 dprintk("opening device and trying to acquire exclusive lock\n");
295
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300296 if (!dev) {
297 printk(KERN_ERR "BUG: em28xx can't find device struct."
298 " Can't proceed with open\n");
299 return -ENODEV;
300 }
301
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300302 /* Sets volume, mute, etc */
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300303
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300304 dev->mute = 0;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300305 mutex_lock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300306 ret = em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300307 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300308 if (ret < 0)
309 goto err;
310
311 runtime->hw = snd_em28xx_hw_capture;
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300312 if (dev->alt == 0 && dev->adev.users == 0) {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300313 int errCode;
314 dev->alt = 7;
315 errCode = usb_set_interface(dev->udev, 0, 7);
316 dprintk("changing alternate number to 7\n");
317 }
318
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300319 dev->adev.users++;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300320
321 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300322 dev->adev.capture_pcm_substream = substream;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300323 runtime->private_data = dev;
324
325 return 0;
326err:
327 printk(KERN_ERR "Error while configuring em28xx mixer\n");
328 return ret;
329}
330
331static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
332{
333 struct em28xx *dev = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300334 dev->adev.users--;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300335
336 dprintk("closing device\n");
337
338 dev->mute = 1;
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300339 mutex_lock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300340 em28xx_audio_analog_set(dev);
Mauro Carvalho Chehab92ea42f2008-02-06 18:52:15 -0300341 mutex_unlock(&dev->lock);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300342
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300343 if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
344 dprintk("audio users: %d\n", dev->adev.users);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300345 dprintk("disabling audio stream!\n");
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300346 dev->adev.shutdown = 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300347 dprintk("released lock\n");
348 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
349 }
350 return 0;
351}
352
353static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
354 struct snd_pcm_hw_params *hw_params)
355{
356 unsigned int channels, rate, format;
357 int ret;
358
359 dprintk("Setting capture parameters\n");
360
361 ret = snd_pcm_alloc_vmalloc_buffer(substream,
362 params_buffer_bytes(hw_params));
363 format = params_format(hw_params);
364 rate = params_rate(hw_params);
365 channels = params_channels(hw_params);
366
367 /* TODO: set up em28xx audio chip to deliver the correct audio format,
368 current default is 48000hz multiplexed => 96000hz mono
369 which shouldn't matter since analogue TV only supports mono */
370 return 0;
371}
372
373static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
374{
375 struct em28xx *dev = snd_pcm_substream_chip(substream);
376
377 dprintk("Stop capture, if needed\n");
378
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300379 if (dev->adev.capture_stream == STREAM_ON)
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300380 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
381
382 return 0;
383}
384
385static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
386{
387 return 0;
388}
389
390static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
391 int cmd)
392{
393 struct em28xx *dev = snd_pcm_substream_chip(substream);
394
395 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
396 "start": "stop");
397 switch (cmd) {
398 case SNDRV_PCM_TRIGGER_START:
399 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
400 return 0;
401 case SNDRV_PCM_TRIGGER_STOP:
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300402 dev->adev.shutdown = 1;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300403 return 0;
404 default:
405 return -EINVAL;
406 }
407}
408
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300409static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
410 *substream)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300411{
Robert Krakora53d12e52009-01-16 11:23:25 -0300412 unsigned long flags;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300413
Robert Krakora53d12e52009-01-16 11:23:25 -0300414 struct em28xx *dev;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300415 snd_pcm_uframes_t hwptr_done;
Robert Krakora53d12e52009-01-16 11:23:25 -0300416
Markus Rechbergera52932b2008-01-05 09:55:47 -0300417 dev = snd_pcm_substream_chip(substream);
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300418 spin_lock_irqsave(&dev->adev.slock, flags);
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300419 hwptr_done = dev->adev.hwptr_done_capture;
Douglas Schilling Landgraf00bc0642009-01-25 15:12:29 -0300420 spin_unlock_irqrestore(&dev->adev.slock, flags);
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300421
Markus Rechbergera52932b2008-01-05 09:55:47 -0300422 return hwptr_done;
423}
424
425static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
426 unsigned long offset)
427{
428 void *pageptr = subs->runtime->dma_area + offset;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300429
Markus Rechbergera52932b2008-01-05 09:55:47 -0300430 return vmalloc_to_page(pageptr);
431}
432
433static struct snd_pcm_ops snd_em28xx_pcm_capture = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300434 .open = snd_em28xx_capture_open,
435 .close = snd_em28xx_pcm_close,
436 .ioctl = snd_pcm_lib_ioctl,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300437 .hw_params = snd_em28xx_hw_capture_params,
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300438 .hw_free = snd_em28xx_hw_capture_free,
439 .prepare = snd_em28xx_prepare,
440 .trigger = snd_em28xx_capture_trigger,
441 .pointer = snd_em28xx_capture_pointer,
442 .page = snd_pcm_get_vmalloc_page,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300443};
444
Markus Rechbergera52932b2008-01-05 09:55:47 -0300445static int em28xx_audio_init(struct em28xx *dev)
446{
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300447 struct em28xx_audio *adev = &dev->adev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300448 struct snd_pcm *pcm;
449 struct snd_card *card;
450 static int devnr;
Devin Heitmueller65c9fd42008-11-12 02:04:53 -0300451 int err;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300452
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300453 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300454 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300455 the device is expecting the snd-usb-audio module or
456 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300457 return 0;
458 }
459
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300460 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
461 "non standard usbaudio\n");
462 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
463 "Rechberger\n");
464
Takashi Iwai758021b2009-01-12 15:17:09 +0100465 err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
466 &card);
467 if (err < 0)
468 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300469
470 spin_lock_init(&adev->slock);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300471 err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
472 if (err < 0) {
473 snd_card_free(card);
474 return err;
475 }
476
Markus Rechbergera52932b2008-01-05 09:55:47 -0300477 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
478 pcm->info_flags = 0;
479 pcm->private_data = dev;
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300480 strcpy(pcm->name, "Empia 28xx Capture");
Nicola Soranzo7662b002009-02-19 13:41:56 -0300481
482 snd_card_set_dev(card, &dev->udev->dev);
Markus Rechbergera52932b2008-01-05 09:55:47 -0300483 strcpy(card->driver, "Empia Em28xx Audio");
484 strcpy(card->shortname, "Em28xx Audio");
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300485 strcpy(card->longname, "Empia Em28xx Audio");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300486
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300487 err = snd_card_register(card);
488 if (err < 0) {
Markus Rechbergera52932b2008-01-05 09:55:47 -0300489 snd_card_free(card);
Mauro Carvalho Chehab1f6340b2008-11-07 14:24:18 -0300490 return err;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300491 }
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300492 adev->sndcard = card;
493 adev->udev = dev->udev;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300494
Markus Rechbergera52932b2008-01-05 09:55:47 -0300495 return 0;
496}
497
498static int em28xx_audio_fini(struct em28xx *dev)
499{
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300500 if (dev == NULL)
Markus Rechbergera52932b2008-01-05 09:55:47 -0300501 return 0;
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300502
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300503 if (dev->has_alsa_audio != 1) {
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300504 /* This device does not support the extension (in this case
Devin Heitmueller24a613e2008-11-12 02:05:19 -0300505 the device is expecting the snd-usb-audio module or
506 doesn't have analog audio support at all) */
Devin Heitmuellerdf619182008-06-10 12:34:35 -0300507 return 0;
508 }
509
Mauro Carvalho Chehab9baed992008-12-31 09:37:33 -0300510 if (dev->adev.sndcard) {
511 snd_card_free(dev->adev.sndcard);
512 dev->adev.sndcard = NULL;
Markus Rechbergera52932b2008-01-05 09:55:47 -0300513 }
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300514
Markus Rechbergera52932b2008-01-05 09:55:47 -0300515 return 0;
516}
517
518static struct em28xx_ops audio_ops = {
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300519 .id = EM28XX_AUDIO,
Mauro Carvalho Chehab1a6f11e2008-01-05 09:56:24 -0300520 .name = "Em28xx Audio Extension",
521 .init = em28xx_audio_init,
522 .fini = em28xx_audio_fini,
Markus Rechbergera52932b2008-01-05 09:55:47 -0300523};
524
525static int __init em28xx_alsa_register(void)
526{
Markus Rechbergera52932b2008-01-05 09:55:47 -0300527 return em28xx_register_extension(&audio_ops);
528}
529
530static void __exit em28xx_alsa_unregister(void)
531{
532 em28xx_unregister_extension(&audio_ops);
533}
534
535MODULE_LICENSE("GPL");
536MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
Mauro Carvalho Chehab6d794682008-01-05 09:57:31 -0300537MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
Markus Rechbergera52932b2008-01-05 09:55:47 -0300538MODULE_DESCRIPTION("Em28xx Audio driver");
539
540module_init(em28xx_alsa_register);
541module_exit(em28xx_alsa_unregister);