V4L/DVB (8026): Avoids an OOPS if dev struct can't be successfully recovered
[linux-block.git] / drivers / media / video / em28xx / em28xx-audio.c
CommitLineData
a52932b4
MR
1/*
2 * Empiatech em28x1 audio extension
3 *
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
5 *
6d79468d
MCC
6 * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
7 * - Port to work with the in-kernel driver
8 * - Several cleanups
9 *
a52932b4
MR
10 * This driver is based on my previous au600 usb pstn audio driver
11 * and inherits all the copyrights
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/kernel.h>
29#include <linux/usb.h>
30#include <linux/init.h>
31#include <linux/sound.h>
32#include <linux/spinlock.h>
33#include <linux/soundcard.h>
34#include <linux/slab.h>
35#include <linux/vmalloc.h>
36#include <linux/proc_fs.h>
6d79468d 37#include <linux/module.h>
a52932b4
MR
38#include <sound/core.h>
39#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/info.h>
42#include <sound/initval.h>
43#include <sound/control.h>
a52932b4
MR
44#include <media/v4l2-common.h>
45#include "em28xx.h"
46
6d79468d
MCC
47static int debug;
48module_param(debug, int, 0644);
49MODULE_PARM_DESC(debug, "activates debug info");
a52932b4 50
6d79468d
MCC
51#define dprintk(fmt, arg...) do { \
52 if (debug) \
53 printk(KERN_INFO "em28xx-audio %s: " fmt, \
d80e134d 54 __func__, ##arg); \
6d79468d 55 } while (0)
a52932b4 56
6d79468d 57static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
a52932b4 58
6d79468d 59static int em28xx_isoc_audio_deinit(struct em28xx *dev)
a52932b4 60{
6d79468d 61 int i;
a52932b4 62
6d79468d
MCC
63 dprintk("Stopping isoc\n");
64 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
65 usb_kill_urb(dev->adev->urb[i]);
66 usb_free_urb(dev->adev->urb[i]);
67 dev->adev->urb[i] = NULL;
a52932b4 68 }
a52932b4 69
a52932b4
MR
70 return 0;
71}
72
a52932b4
MR
73static void em28xx_audio_isocirq(struct urb *urb)
74{
6d79468d
MCC
75 struct em28xx *dev = urb->context;
76 int i;
77 unsigned int oldptr;
78 unsigned long flags;
79 int period_elapsed = 0;
80 int status;
81 unsigned char *cp;
82 unsigned int stride;
a52932b4 83 struct snd_pcm_substream *substream;
6d79468d 84 struct snd_pcm_runtime *runtime;
1a6f11e0
MCC
85 if (dev->adev->capture_pcm_substream) {
86 substream = dev->adev->capture_pcm_substream;
87 runtime = substream->runtime;
a52932b4 88 stride = runtime->frame_bits >> 3;
6d79468d 89
1a6f11e0
MCC
90 for (i = 0; i < urb->number_of_packets; i++) {
91 int length =
92 urb->iso_frame_desc[i].actual_length / stride;
93 cp = (unsigned char *)urb->transfer_buffer +
94 urb->iso_frame_desc[i].offset;
a52932b4 95
1a6f11e0 96 if (!length)
a52932b4
MR
97 continue;
98
99 spin_lock_irqsave(&dev->adev->slock, flags);
6d79468d 100
a52932b4 101 oldptr = dev->adev->hwptr_done_capture;
1a6f11e0
MCC
102 dev->adev->hwptr_done_capture += length;
103 if (dev->adev->hwptr_done_capture >=
104 runtime->buffer_size)
105 dev->adev->hwptr_done_capture -=
106 runtime->buffer_size;
a52932b4
MR
107
108 dev->adev->capture_transfer_done += length;
1a6f11e0
MCC
109 if (dev->adev->capture_transfer_done >=
110 runtime->period_size) {
111 dev->adev->capture_transfer_done -=
112 runtime->period_size;
113 period_elapsed = 1;
a52932b4 114 }
6d79468d 115
a52932b4
MR
116 spin_unlock_irqrestore(&dev->adev->slock, flags);
117
1a6f11e0
MCC
118 if (oldptr + length >= runtime->buffer_size) {
119 unsigned int cnt =
120 runtime->buffer_size - oldptr - 1;
121 memcpy(runtime->dma_area + oldptr * stride, cp,
122 cnt * stride);
123 memcpy(runtime->dma_area, cp + cnt,
124 length * stride - cnt * stride);
a52932b4 125 } else {
1a6f11e0
MCC
126 memcpy(runtime->dma_area + oldptr * stride, cp,
127 length * stride);
a52932b4
MR
128 }
129 }
6d79468d 130 if (period_elapsed)
a52932b4 131 snd_pcm_period_elapsed(substream);
a52932b4
MR
132 }
133 urb->status = 0;
1a6f11e0
MCC
134
135 if (dev->adev->shutdown)
a52932b4
MR
136 return;
137
6d79468d
MCC
138 status = usb_submit_urb(urb, GFP_ATOMIC);
139 if (status < 0) {
1a6f11e0
MCC
140 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
141 status);
a52932b4
MR
142 }
143 return;
144}
145
a52932b4
MR
146static int em28xx_init_audio_isoc(struct em28xx *dev)
147{
6d79468d
MCC
148 int i, errCode;
149 const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
150 EM28XX_AUDIO_MAX_PACKET_SIZE;
151
152 dprintk("Starting isoc transfers\n");
a52932b4 153
1a6f11e0 154 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
a52932b4 155 struct urb *urb;
1a6f11e0 156 int j, k;
6d79468d 157
1a6f11e0 158 dev->adev->transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
6d79468d 159 if (!dev->adev->transfer_buffer[i])
a52932b4 160 return -ENOMEM;
6d79468d 161
1a6f11e0
MCC
162 memset(dev->adev->transfer_buffer[i], 0x80, sb_size);
163 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
6d79468d 164 if (!urb)
a52932b4 165 return -ENOMEM;
6d79468d
MCC
166
167 urb->dev = dev->udev;
168 urb->context = dev;
169 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
170 urb->transfer_flags = URB_ISO_ASAP;
171 urb->transfer_buffer = dev->adev->transfer_buffer[i];
172 urb->interval = 1;
173 urb->complete = em28xx_audio_isocirq;
174 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
175 urb->transfer_buffer_length = sb_size;
176
177 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
178 j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
179 urb->iso_frame_desc[j].offset = k;
180 urb->iso_frame_desc[j].length =
181 EM28XX_AUDIO_MAX_PACKET_SIZE;
a52932b4 182 }
6d79468d 183 dev->adev->urb[i] = urb;
a52932b4 184 }
6d79468d 185
1a6f11e0 186 for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
a52932b4 187 errCode = usb_submit_urb(dev->adev->urb[i], GFP_ATOMIC);
1a6f11e0 188 if (errCode) {
a52932b4 189 em28xx_isoc_audio_deinit(dev);
6d79468d 190
a52932b4
MR
191 return errCode;
192 }
193 }
6d79468d 194
a52932b4
MR
195 return 0;
196}
197
1a6f11e0 198static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
a52932b4 199{
6d79468d
MCC
200 dprintk("%s transfer\n", (dev->adev->capture_stream == STREAM_ON)?
201 "stop" : "start");
202
1a6f11e0
MCC
203 switch (cmd) {
204 case EM28XX_CAPTURE_STREAM_EN:
205 if (dev->adev->capture_stream == STREAM_OFF && arg == 1) {
206 dev->adev->capture_stream = STREAM_ON;
207 em28xx_init_audio_isoc(dev);
208 } else if (dev->adev->capture_stream == STREAM_ON && arg == 0) {
209 dev->adev->capture_stream = STREAM_OFF;
210 em28xx_isoc_audio_deinit(dev);
211 } else {
6d79468d
MCC
212 printk(KERN_ERR "An underrun very likely occurred. "
213 "Ignoring it.\n");
1a6f11e0
MCC
214 }
215 return 0;
216 default:
217 return -EINVAL;
a52932b4
MR
218 }
219}
220
6d79468d
MCC
221static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
222 size_t size)
223{
224 struct snd_pcm_runtime *runtime = subs->runtime;
225
226 dprintk("Alocating vbuffer\n");
227 if (runtime->dma_area) {
228 if (runtime->dma_bytes > size)
229 return 0;
230
231 vfree(runtime->dma_area);
232 }
233 runtime->dma_area = vmalloc(size);
234 if (!runtime->dma_area)
235 return -ENOMEM;
236
237 runtime->dma_bytes = size;
238
239 return 0;
240}
241
242static struct snd_pcm_hardware snd_em28xx_hw_capture = {
243 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
244 SNDRV_PCM_INFO_MMAP |
245 SNDRV_PCM_INFO_INTERLEAVED |
246 SNDRV_PCM_INFO_MMAP_VALID,
247
248 .formats = SNDRV_PCM_FMTBIT_S16_LE,
249
250 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
251
252 .rate_min = 48000,
253 .rate_max = 48000,
254 .channels_min = 2,
255 .channels_max = 2,
256 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
257 .period_bytes_min = 64, /* 12544/2, */
258 .period_bytes_max = 12544,
259 .periods_min = 2,
260 .periods_max = 98, /* 12544, */
261};
262
263static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
264{
265 struct em28xx *dev = snd_pcm_substream_chip(substream);
266 struct snd_pcm_runtime *runtime = substream->runtime;
267 int ret = 0;
268
269 dprintk("opening device and trying to acquire exclusive lock\n");
270
83ee87a3
MCC
271 if (!dev) {
272 printk(KERN_ERR "BUG: em28xx can't find device struct."
273 " Can't proceed with open\n");
274 return -ENODEV;
275 }
276
6d79468d 277 /* Sets volume, mute, etc */
92ea42f4 278
6d79468d 279 dev->mute = 0;
92ea42f4 280 mutex_lock(&dev->lock);
6d79468d 281 ret = em28xx_audio_analog_set(dev);
92ea42f4 282 mutex_unlock(&dev->lock);
6d79468d
MCC
283 if (ret < 0)
284 goto err;
285
286 runtime->hw = snd_em28xx_hw_capture;
287 if (dev->alt == 0 && dev->adev->users == 0) {
288 int errCode;
289 dev->alt = 7;
290 errCode = usb_set_interface(dev->udev, 0, 7);
291 dprintk("changing alternate number to 7\n");
292 }
293
294 dev->adev->users++;
295
296 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
297 dev->adev->capture_pcm_substream = substream;
298 runtime->private_data = dev;
299
300 return 0;
301err:
302 printk(KERN_ERR "Error while configuring em28xx mixer\n");
303 return ret;
304}
305
306static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
307{
308 struct em28xx *dev = snd_pcm_substream_chip(substream);
309 dev->adev->users--;
310
311 dprintk("closing device\n");
312
313 dev->mute = 1;
92ea42f4 314 mutex_lock(&dev->lock);
6d79468d 315 em28xx_audio_analog_set(dev);
92ea42f4 316 mutex_unlock(&dev->lock);
6d79468d
MCC
317
318 if (dev->adev->users == 0 && dev->adev->shutdown == 1) {
319 dprintk("audio users: %d\n", dev->adev->users);
320 dprintk("disabling audio stream!\n");
321 dev->adev->shutdown = 0;
322 dprintk("released lock\n");
323 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
324 }
325 return 0;
326}
327
328static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
329 struct snd_pcm_hw_params *hw_params)
330{
331 unsigned int channels, rate, format;
332 int ret;
333
334 dprintk("Setting capture parameters\n");
335
336 ret = snd_pcm_alloc_vmalloc_buffer(substream,
337 params_buffer_bytes(hw_params));
338 format = params_format(hw_params);
339 rate = params_rate(hw_params);
340 channels = params_channels(hw_params);
341
342 /* TODO: set up em28xx audio chip to deliver the correct audio format,
343 current default is 48000hz multiplexed => 96000hz mono
344 which shouldn't matter since analogue TV only supports mono */
345 return 0;
346}
347
348static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
349{
350 struct em28xx *dev = snd_pcm_substream_chip(substream);
351
352 dprintk("Stop capture, if needed\n");
353
354 if (dev->adev->capture_stream == STREAM_ON)
355 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
356
357 return 0;
358}
359
360static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
361{
362 return 0;
363}
364
365static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
366 int cmd)
367{
368 struct em28xx *dev = snd_pcm_substream_chip(substream);
369
370 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
371 "start": "stop");
372 switch (cmd) {
373 case SNDRV_PCM_TRIGGER_START:
374 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
375 return 0;
376 case SNDRV_PCM_TRIGGER_STOP:
377 dev->adev->shutdown = 1;
378 return 0;
379 default:
380 return -EINVAL;
381 }
382}
383
1a6f11e0
MCC
384static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
385 *substream)
a52932b4
MR
386{
387 struct em28xx *dev;
6d79468d 388
a52932b4
MR
389 snd_pcm_uframes_t hwptr_done;
390 dev = snd_pcm_substream_chip(substream);
391 hwptr_done = dev->adev->hwptr_done_capture;
6d79468d 392
a52932b4
MR
393 return hwptr_done;
394}
395
396static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
397 unsigned long offset)
398{
399 void *pageptr = subs->runtime->dma_area + offset;
6d79468d 400
a52932b4
MR
401 return vmalloc_to_page(pageptr);
402}
403
404static struct snd_pcm_ops snd_em28xx_pcm_capture = {
6d79468d
MCC
405 .open = snd_em28xx_capture_open,
406 .close = snd_em28xx_pcm_close,
407 .ioctl = snd_pcm_lib_ioctl,
a52932b4 408 .hw_params = snd_em28xx_hw_capture_params,
6d79468d
MCC
409 .hw_free = snd_em28xx_hw_capture_free,
410 .prepare = snd_em28xx_prepare,
411 .trigger = snd_em28xx_capture_trigger,
412 .pointer = snd_em28xx_capture_pointer,
413 .page = snd_pcm_get_vmalloc_page,
a52932b4
MR
414};
415
a52932b4
MR
416static int em28xx_audio_init(struct em28xx *dev)
417{
418 struct em28xx_audio *adev;
6d79468d
MCC
419 struct snd_pcm *pcm;
420 struct snd_card *card;
421 static int devnr;
422 int ret, err;
423
df619181
DH
424 if (dev->has_audio_class) {
425 /* This device does not support the extension (in this case
426 the device is expecting the snd-usb-audio module */
427 return 0;
428 }
429
6d79468d
MCC
430 printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
431 "non standard usbaudio\n");
432 printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
433 "Rechberger\n");
434
1a6f11e0
MCC
435 adev = kzalloc(sizeof(*adev), GFP_KERNEL);
436 if (!adev) {
6d79468d 437 printk(KERN_ERR "em28xx-audio.c: out of memory\n");
a52932b4
MR
438 return -1;
439 }
1a6f11e0
MCC
440 card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
441 if (card == NULL) {
a52932b4
MR
442 kfree(adev);
443 return -ENOMEM;
444 }
445
446 spin_lock_init(&adev->slock);
1a6f11e0 447 ret = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
a52932b4
MR
448 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
449 pcm->info_flags = 0;
450 pcm->private_data = dev;
1a6f11e0 451 strcpy(pcm->name, "Empia 28xx Capture");
a52932b4
MR
452 strcpy(card->driver, "Empia Em28xx Audio");
453 strcpy(card->shortname, "Em28xx Audio");
1a6f11e0 454 strcpy(card->longname, "Empia Em28xx Audio");
a52932b4 455
6d79468d
MCC
456 err = snd_card_register(card);
457 if (err < 0) {
a52932b4
MR
458 snd_card_free(card);
459 return -ENOMEM;
460 }
1a6f11e0
MCC
461 adev->sndcard = card;
462 adev->udev = dev->udev;
463 dev->adev = adev;
6d79468d 464
a52932b4
MR
465 return 0;
466}
467
468static int em28xx_audio_fini(struct em28xx *dev)
469{
1a6f11e0 470 if (dev == NULL)
a52932b4 471 return 0;
6d79468d 472
df619181
DH
473 if (dev->has_audio_class) {
474 /* This device does not support the extension (in this case
475 the device is expecting the snd-usb-audio module */
476 return 0;
477 }
478
1a6f11e0 479 if (dev->adev) {
a52932b4
MR
480 snd_card_free(dev->adev->sndcard);
481 kfree(dev->adev);
1a6f11e0 482 dev->adev = NULL;
a52932b4 483 }
6d79468d 484
a52932b4
MR
485 return 0;
486}
487
488static struct em28xx_ops audio_ops = {
6d79468d 489 .id = EM28XX_AUDIO,
1a6f11e0
MCC
490 .name = "Em28xx Audio Extension",
491 .init = em28xx_audio_init,
492 .fini = em28xx_audio_fini,
a52932b4
MR
493};
494
495static int __init em28xx_alsa_register(void)
496{
a52932b4
MR
497 return em28xx_register_extension(&audio_ops);
498}
499
500static void __exit em28xx_alsa_unregister(void)
501{
502 em28xx_unregister_extension(&audio_ops);
503}
504
505MODULE_LICENSE("GPL");
506MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
6d79468d 507MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
a52932b4
MR
508MODULE_DESCRIPTION("Em28xx Audio driver");
509
510module_init(em28xx_alsa_register);
511module_exit(em28xx_alsa_unregister);