V4L/DVB (10957a): cx231xx: Fix compilation breakage
[deliverable/linux.git] / drivers / media / video / cx231xx / cx231xx-audio.c
CommitLineData
e0d3bafd
SD
1/*
2 * Conexant Cx231xx audio extension
3 *
4 * Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5 * Based on em28xx driver
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/kernel.h>
24#include <linux/usb.h>
25#include <linux/init.h>
26#include <linux/sound.h>
27#include <linux/spinlock.h>
28#include <linux/soundcard.h>
29#include <linux/slab.h>
30#include <linux/vmalloc.h>
31#include <linux/proc_fs.h>
32#include <linux/module.h>
33#include <sound/core.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/info.h>
37#include <sound/initval.h>
38#include <sound/control.h>
39#include <media/v4l2-common.h>
40#include "cx231xx.h"
41#include "cx231xx-pcb-config.h"
42
43static int debug;
44module_param(debug, int, 0644);
45MODULE_PARM_DESC(debug, "activates debug info");
46
47#define dprintk(fmt, arg...) do { \
48 if (debug) \
49 printk(KERN_INFO "cx231xx-audio %s: " fmt, \
50 __func__, ##arg); \
51 } while (0)
52
53static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
54
55static int cx231xx_isoc_audio_deinit(struct cx231xx *dev)
56{
57 int i;
58
59 dprintk("Stopping isoc\n");
60
e0d3bafd 61 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
84b5dbf3
MCC
62 if (dev->adev.urb[i]) {
63 if (!irqs_disabled())
64 usb_kill_urb(dev->adev.urb[i]);
65 else
66 usb_unlink_urb(dev->adev.urb[i]);
e0d3bafd 67
84b5dbf3
MCC
68 usb_free_urb(dev->adev.urb[i]);
69 dev->adev.urb[i] = NULL;
e0d3bafd 70
84b5dbf3
MCC
71 kfree(dev->adev.transfer_buffer[i]);
72 dev->adev.transfer_buffer[i] = NULL;
84b5dbf3 73 }
e0d3bafd
SD
74 }
75
76 return 0;
77}
78
79static void cx231xx_audio_isocirq(struct urb *urb)
80{
84b5dbf3
MCC
81 struct cx231xx *dev = urb->context;
82 int i;
83 unsigned int oldptr;
84 int period_elapsed = 0;
85 int status;
86 unsigned char *cp;
87 unsigned int stride;
e0d3bafd 88 struct snd_pcm_substream *substream;
84b5dbf3
MCC
89 struct snd_pcm_runtime *runtime;
90
91 switch (urb->status) {
92 case 0: /* success */
93 case -ETIMEDOUT: /* NAK */
94 break;
95 case -ECONNRESET: /* kill */
96 case -ENOENT:
97 case -ESHUTDOWN:
98 return;
99 default: /* error */
100 dprintk("urb completition error %d.\n", urb->status);
101 break;
e0d3bafd
SD
102 }
103
104 if (dev->adev.capture_pcm_substream) {
105 substream = dev->adev.capture_pcm_substream;
106 runtime = substream->runtime;
107 stride = runtime->frame_bits >> 3;
108
109 for (i = 0; i < urb->number_of_packets; i++) {
cde4362f
MCC
110 int length = urb->iso_frame_desc[i].actual_length /
111 stride;
e0d3bafd 112 cp = (unsigned char *)urb->transfer_buffer +
cde4362f 113 urb->iso_frame_desc[i].offset;
e0d3bafd
SD
114
115 if (!length)
116 continue;
117
118 oldptr = dev->adev.hwptr_done_capture;
119 if (oldptr + length >= runtime->buffer_size) {
cde4362f
MCC
120 unsigned int cnt;
121
122 cnt = runtime->buffer_size - oldptr;
e0d3bafd
SD
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
132 snd_pcm_stream_lock(substream);
133
134 dev->adev.hwptr_done_capture += length;
cde4362f
MCC
135 if (dev->adev.hwptr_done_capture >= runtime->buffer_size)
136 dev->adev.hwptr_done_capture -= runtime->buffer_size;
e0d3bafd
SD
137
138 dev->adev.capture_transfer_done += length;
cde4362f
MCC
139 if (dev->adev.capture_transfer_done >= runtime->period_size) {
140 dev->adev.capture_transfer_done -= runtime->period_size;
e0d3bafd
SD
141 period_elapsed = 1;
142 }
e0d3bafd
SD
143 snd_pcm_stream_unlock(substream);
144 }
145 if (period_elapsed)
146 snd_pcm_period_elapsed(substream);
147 }
148 urb->status = 0;
149
150 status = usb_submit_urb(urb, GFP_ATOMIC);
151 if (status < 0) {
152 cx231xx_errdev("resubmit of audio urb failed (error=%i)\n",
84b5dbf3 153 status);
e0d3bafd
SD
154 }
155 return;
156}
157
158static int cx231xx_init_audio_isoc(struct cx231xx *dev)
159{
84b5dbf3
MCC
160 int i, errCode;
161 int sb_size;
e0d3bafd 162
84b5dbf3 163 cx231xx_info("%s: Starting AUDIO transfers\n", __func__);
e0d3bafd 164
84b5dbf3 165 sb_size = CX231XX_NUM_AUDIO_PACKETS * dev->adev.max_pkt_size;
e0d3bafd
SD
166
167 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
168 struct urb *urb;
169 int j, k;
170
171 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
172 if (!dev->adev.transfer_buffer[i])
173 return -ENOMEM;
174
175 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
176 urb = usb_alloc_urb(CX231XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
177 if (!urb) {
178 cx231xx_errdev("usb_alloc_urb failed!\n");
179 for (j = 0; j < i; j++) {
180 usb_free_urb(dev->adev.urb[j]);
181 kfree(dev->adev.transfer_buffer[j]);
182 }
183 return -ENOMEM;
184 }
185
186 urb->dev = dev->udev;
187 urb->context = dev;
cde4362f 188 urb->pipe = usb_rcvisocpipe(dev->udev, dev->adev.end_point_addr);
e0d3bafd
SD
189 urb->transfer_flags = URB_ISO_ASAP;
190 urb->transfer_buffer = dev->adev.transfer_buffer[i];
191 urb->interval = 1;
192 urb->complete = cx231xx_audio_isocirq;
193 urb->number_of_packets = CX231XX_NUM_AUDIO_PACKETS;
194 urb->transfer_buffer_length = sb_size;
195
cde4362f 196 for (j = k = 0; j < CX231XX_NUM_AUDIO_PACKETS; j++, k += dev->adev.max_pkt_size) {
e0d3bafd 197 urb->iso_frame_desc[j].offset = k;
84b5dbf3 198 urb->iso_frame_desc[j].length = dev->adev.max_pkt_size;
e0d3bafd
SD
199 }
200 dev->adev.urb[i] = urb;
201 }
202
203 for (i = 0; i < CX231XX_AUDIO_BUFS; i++) {
204 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
205 if (errCode < 0) {
206 cx231xx_isoc_audio_deinit(dev);
207 return errCode;
208 }
209 }
210
211 return errCode;
212}
213
214static int cx231xx_cmd(struct cx231xx *dev, int cmd, int arg)
215{
84b5dbf3
MCC
216 dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
217 "stop" : "start");
e0d3bafd
SD
218
219 switch (cmd) {
84b5dbf3 220 case CX231XX_CAPTURE_STREAM_EN:
e0d3bafd
SD
221 if (dev->adev.capture_stream == STREAM_OFF && arg == 1) {
222 dev->adev.capture_stream = STREAM_ON;
223 cx231xx_init_audio_isoc(dev);
224 } else if (dev->adev.capture_stream == STREAM_ON && arg == 0) {
225 dev->adev.capture_stream = STREAM_OFF;
226 cx231xx_isoc_audio_deinit(dev);
227 } else {
84b5dbf3
MCC
228 cx231xx_errdev("An underrun very likely occurred. "
229 "Ignoring it.\n");
e0d3bafd
SD
230 }
231 return 0;
232 default:
233 return -EINVAL;
234 }
235}
236
237static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
238 size_t size)
239{
240 struct snd_pcm_runtime *runtime = subs->runtime;
241
242 dprintk("Allocating vbuffer\n");
243 if (runtime->dma_area) {
244 if (runtime->dma_bytes > size)
245 return 0;
246
247 vfree(runtime->dma_area);
248 }
249 runtime->dma_area = vmalloc(size);
250 if (!runtime->dma_area)
251 return -ENOMEM;
252
253 runtime->dma_bytes = size;
254
255 return 0;
256}
257
258static struct snd_pcm_hardware snd_cx231xx_hw_capture = {
259 .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
84b5dbf3
MCC
260 SNDRV_PCM_INFO_MMAP |
261 SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_MMAP_VALID,
e0d3bafd
SD
262
263 .formats = SNDRV_PCM_FMTBIT_S16_LE,
264
265 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
266
267 .rate_min = 48000,
268 .rate_max = 48000,
269 .channels_min = 2,
270 .channels_max = 2,
271 .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */
cde4362f 272 .period_bytes_min = 64, /* 12544/2, */
e0d3bafd
SD
273 .period_bytes_max = 12544,
274 .periods_min = 2,
cde4362f 275 .periods_max = 98, /* 12544, */
e0d3bafd
SD
276};
277
278static int snd_cx231xx_capture_open(struct snd_pcm_substream *substream)
279{
280 struct cx231xx *dev = snd_pcm_substream_chip(substream);
281 struct snd_pcm_runtime *runtime = substream->runtime;
282 int ret = 0;
283
284 dprintk("opening device and trying to acquire exclusive lock\n");
285
286 if (!dev) {
287 cx231xx_errdev("BUG: cx231xx can't find device struct."
84b5dbf3 288 " Can't proceed with open\n");
e0d3bafd
SD
289 return -ENODEV;
290 }
291
292 /* Sets volume, mute, etc */
293 dev->mute = 0;
294
84b5dbf3
MCC
295 /* set alternate setting for audio interface */
296 ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 1); /* 1 - 48000 samples per sec */
297 if (ret < 0) {
298 cx231xx_errdev("failed to set alternate setting !\n");
e0d3bafd 299
84b5dbf3
MCC
300 return ret;
301 }
e0d3bafd 302
84b5dbf3
MCC
303 /* inform hardware to start streaming */
304 ret = cx231xx_capture_start(dev, 1, Audio);
e0d3bafd
SD
305
306 runtime->hw = snd_cx231xx_hw_capture;
307
84b5dbf3 308 mutex_lock(&dev->lock);
e0d3bafd 309 dev->adev.users++;
84b5dbf3 310 mutex_unlock(&dev->lock);
e0d3bafd
SD
311
312 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
313 dev->adev.capture_pcm_substream = substream;
314 runtime->private_data = dev;
315
316 return 0;
317}
318
319static int snd_cx231xx_pcm_close(struct snd_pcm_substream *substream)
320{
84b5dbf3 321 int ret;
e0d3bafd
SD
322 struct cx231xx *dev = snd_pcm_substream_chip(substream);
323
e0d3bafd
SD
324 dprintk("closing device\n");
325
84b5dbf3
MCC
326 /* set alternate setting for audio interface */
327 ret = cx231xx_set_alt_setting(dev, INDEX_AUDIO, 0); /* 1 - 48000 samples per sec */
328 if (ret < 0) {
329 cx231xx_errdev("failed to set alternate setting !\n");
e0d3bafd 330
84b5dbf3
MCC
331 return ret;
332 }
e0d3bafd 333
84b5dbf3
MCC
334 /* inform hardware to start streaming */
335 ret = cx231xx_capture_start(dev, 0, Audio);
e0d3bafd
SD
336
337 dev->mute = 1;
338 mutex_lock(&dev->lock);
84b5dbf3 339 dev->adev.users--;
e0d3bafd
SD
340 mutex_unlock(&dev->lock);
341
342 if (dev->adev.users == 0 && dev->adev.shutdown == 1) {
343 dprintk("audio users: %d\n", dev->adev.users);
344 dprintk("disabling audio stream!\n");
345 dev->adev.shutdown = 0;
346 dprintk("released lock\n");
347 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, 0);
348 }
349 return 0;
350}
351
352static int snd_cx231xx_hw_capture_params(struct snd_pcm_substream *substream,
84b5dbf3 353 struct snd_pcm_hw_params *hw_params)
e0d3bafd
SD
354{
355 unsigned int channels, rate, format;
356 int ret;
357
358 dprintk("Setting capture parameters\n");
359
360 ret = snd_pcm_alloc_vmalloc_buffer(substream,
84b5dbf3 361 params_buffer_bytes(hw_params));
e0d3bafd
SD
362 format = params_format(hw_params);
363 rate = params_rate(hw_params);
364 channels = params_channels(hw_params);
365
366 /* TODO: set up cx231xx audio chip to deliver the correct audio format,
367 current default is 48000hz multiplexed => 96000hz mono
368 which shouldn't matter since analogue TV only supports mono */
369 return 0;
370}
371
372static int snd_cx231xx_hw_capture_free(struct snd_pcm_substream *substream)
373{
374 struct cx231xx *dev = snd_pcm_substream_chip(substream);
375
376 dprintk("Stop capture, if needed\n");
377
378 if (dev->adev.capture_stream == STREAM_ON)
379 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, CX231XX_STOP_AUDIO);
380
381 return 0;
382}
383
384static int snd_cx231xx_prepare(struct snd_pcm_substream *substream)
385{
386 return 0;
387}
388
389static int snd_cx231xx_capture_trigger(struct snd_pcm_substream *substream,
84b5dbf3 390 int cmd)
e0d3bafd
SD
391{
392 struct cx231xx *dev = snd_pcm_substream_chip(substream);
84b5dbf3 393 int retval;
e0d3bafd 394
84b5dbf3
MCC
395 dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START) ?
396 "start" : "stop");
e0d3bafd 397
84b5dbf3 398 spin_lock(&dev->adev.slock);
e0d3bafd
SD
399 switch (cmd) {
400 case SNDRV_PCM_TRIGGER_START:
84b5dbf3
MCC
401 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN,
402 CX231XX_START_AUDIO);
e0d3bafd
SD
403 retval = 0;
404 break;
405 case SNDRV_PCM_TRIGGER_STOP:
84b5dbf3 406 cx231xx_cmd(dev, CX231XX_CAPTURE_STREAM_EN, CX231XX_STOP_AUDIO);
e0d3bafd 407 retval = 0;
84b5dbf3 408 break;
e0d3bafd
SD
409 default:
410 retval = -EINVAL;
411 }
412
84b5dbf3 413 spin_unlock(&dev->adev.slock);
e0d3bafd
SD
414 return retval;
415}
416
417static snd_pcm_uframes_t snd_cx231xx_capture_pointer(struct snd_pcm_substream
84b5dbf3 418 *substream)
e0d3bafd
SD
419{
420 struct cx231xx *dev;
84b5dbf3 421 unsigned long flags;
e0d3bafd
SD
422 snd_pcm_uframes_t hwptr_done;
423
424 dev = snd_pcm_substream_chip(substream);
425
84b5dbf3 426 spin_lock_irqsave(&dev->adev.slock, flags);
e0d3bafd 427 hwptr_done = dev->adev.hwptr_done_capture;
84b5dbf3 428 spin_unlock_irqrestore(&dev->adev.slock, flags);
e0d3bafd
SD
429
430 return hwptr_done;
431}
432
433static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
434 unsigned long offset)
435{
436 void *pageptr = subs->runtime->dma_area + offset;
437
438 return vmalloc_to_page(pageptr);
439}
440
441static struct snd_pcm_ops snd_cx231xx_pcm_capture = {
84b5dbf3
MCC
442 .open = snd_cx231xx_capture_open,
443 .close = snd_cx231xx_pcm_close,
444 .ioctl = snd_pcm_lib_ioctl,
e0d3bafd 445 .hw_params = snd_cx231xx_hw_capture_params,
84b5dbf3
MCC
446 .hw_free = snd_cx231xx_hw_capture_free,
447 .prepare = snd_cx231xx_prepare,
448 .trigger = snd_cx231xx_capture_trigger,
449 .pointer = snd_cx231xx_capture_pointer,
450 .page = snd_pcm_get_vmalloc_page,
e0d3bafd
SD
451};
452
453static int cx231xx_audio_init(struct cx231xx *dev)
454{
455 struct cx231xx_audio *adev = &dev->adev;
84b5dbf3
MCC
456 struct snd_pcm *pcm;
457 struct snd_card *card;
458 static int devnr;
459 int err;
460 struct usb_interface *uif;
461 int i, isoc_pipe = 0;
e0d3bafd
SD
462
463 if (dev->has_alsa_audio != 1) {
464 /* This device does not support the extension (in this case
465 the device is expecting the snd-usb-audio module or
466 doesn't have analog audio support at all) */
467 return 0;
468 }
469
470 cx231xx_info("cx231xx-audio.c: probing for cx231xx "
84b5dbf3 471 "non standard usbaudio\n");
e0d3bafd
SD
472
473 card = snd_card_new(index[devnr], "Cx231xx Audio", THIS_MODULE, 0);
cde4362f 474 if (card == NULL)
e0d3bafd 475 return -ENOMEM;
e0d3bafd
SD
476
477 spin_lock_init(&adev->slock);
478 err = snd_pcm_new(card, "Cx231xx Audio", 0, 0, 1, &pcm);
479 if (err < 0) {
480 snd_card_free(card);
481 return err;
482 }
483
84b5dbf3
MCC
484 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
485 &snd_cx231xx_pcm_capture);
e0d3bafd
SD
486 pcm->info_flags = 0;
487 pcm->private_data = dev;
488 strcpy(pcm->name, "Conexant cx231xx Capture");
489 strcpy(card->driver, "Conexant cx231xx Audio");
490 strcpy(card->shortname, "Cx231xx Audio");
491 strcpy(card->longname, "Conexant cx231xx Audio");
492
493 err = snd_card_register(card);
494 if (err < 0) {
495 snd_card_free(card);
496 return err;
497 }
498 adev->sndcard = card;
499 adev->udev = dev->udev;
500
84b5dbf3
MCC
501 /* compute alternate max packet sizes for Audio */
502 uif =
503 dev->udev->actconfig->interface[dev->current_pcb_config.
504 hs_config_info[0].interface_info.
505 audio_index + 1];
e0d3bafd 506
84b5dbf3
MCC
507 adev->end_point_addr =
508 le16_to_cpu(uif->altsetting[0].endpoint[isoc_pipe].desc.
509 bEndpointAddress);
e0d3bafd 510
84b5dbf3
MCC
511 adev->num_alt = uif->num_altsetting;
512 cx231xx_info(": EndPoint Addr 0x%x, Alternate settings: %i\n",
513 adev->end_point_addr, adev->num_alt);
514 adev->alt_max_pkt_size = kmalloc(32 * adev->num_alt, GFP_KERNEL);
e0d3bafd 515
84b5dbf3
MCC
516 if (adev->alt_max_pkt_size == NULL) {
517 cx231xx_errdev("out of memory!\n");
518 return -ENOMEM;
519 }
e0d3bafd 520
84b5dbf3
MCC
521 for (i = 0; i < adev->num_alt; i++) {
522 u16 tmp =
523 le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
524 wMaxPacketSize);
525 adev->alt_max_pkt_size[i] =
526 (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
527 cx231xx_info("Alternate setting %i, max size= %i\n", i,
528 adev->alt_max_pkt_size[i]);
529 }
e0d3bafd
SD
530
531 return 0;
532}
533
534static int cx231xx_audio_fini(struct cx231xx *dev)
535{
536 if (dev == NULL)
537 return 0;
538
539 if (dev->has_alsa_audio != 1) {
540 /* This device does not support the extension (in this case
541 the device is expecting the snd-usb-audio module or
542 doesn't have analog audio support at all) */
543 return 0;
544 }
545
546 if (dev->adev.sndcard) {
547 snd_card_free(dev->adev.sndcard);
84b5dbf3 548 kfree(dev->adev.alt_max_pkt_size);
e0d3bafd
SD
549 dev->adev.sndcard = NULL;
550 }
551
552 return 0;
553}
554
555static struct cx231xx_ops audio_ops = {
84b5dbf3 556 .id = CX231XX_AUDIO,
e0d3bafd
SD
557 .name = "Cx231xx Audio Extension",
558 .init = cx231xx_audio_init,
559 .fini = cx231xx_audio_fini,
560};
561
562static int __init cx231xx_alsa_register(void)
563{
564 return cx231xx_register_extension(&audio_ops);
565}
566
567static void __exit cx231xx_alsa_unregister(void)
568{
569 cx231xx_unregister_extension(&audio_ops);
570}
571
572MODULE_LICENSE("GPL");
573MODULE_AUTHOR("Srinivasa Deevi <srinivasa.deevi@conexant.com>");
574MODULE_DESCRIPTION("Cx231xx Audio driver");
575
576module_init(cx231xx_alsa_register);
577module_exit(cx231xx_alsa_unregister);
This page took 0.052997 seconds and 5 git commands to generate.