[media, edac] Change my email address
[deliverable/linux.git] / drivers / media / usb / tm6000 / tm6000-alsa.c
CommitLineData
d72d5067
MCC
1/*
2 *
e28f49b0 3 * Support for audio capture for tm5600/6000/6010
37e59f87 4 * (c) 2007-2008 Mauro Carvalho Chehab
d72d5067 5 *
7ca30c52 6 * Based on cx88-alsa.c
d72d5067
MCC
7 *
8 * This program is free software; you can redistribute it and/or modify
7ca30c52
MCC
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
d72d5067
MCC
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/interrupt.h>
6ad6e88a 17#include <linux/usb.h>
4ef09889 18#include <linux/slab.h>
54b78608 19#include <linux/vmalloc.h>
d72d5067 20
d684aee3 21#include <linux/delay.h>
d72d5067
MCC
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/control.h>
26#include <sound/initval.h>
d72d5067 27
d72d5067 28
7ca30c52
MCC
29#include "tm6000.h"
30#include "tm6000-regs.h"
31
32#undef dprintk
d72d5067 33
7ca30c52
MCC
34#define dprintk(level, fmt, arg...) do { \
35 if (debug >= level) \
36 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
37 } while (0)
d72d5067 38
d72d5067
MCC
39/****************************************************************************
40 Module global static vars
41 ****************************************************************************/
42
43static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
0e57ff8d 44
90ab5ee9 45static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
d72d5067
MCC
46
47module_param_array(enable, bool, NULL, 0444);
7ca30c52 48MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
d72d5067
MCC
49
50module_param_array(index, int, NULL, 0444);
7ca30c52 51MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
d72d5067
MCC
52
53
54/****************************************************************************
55 Module macros
56 ****************************************************************************/
57
e28f49b0 58MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
37e59f87 59MODULE_AUTHOR("Mauro Carvalho Chehab");
d72d5067 60MODULE_LICENSE("GPL");
7ca30c52 61MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},"
e28f49b0
DB
62 "{{Trident,tm6000},"
63 "{{Trident,tm6010}");
d72d5067 64static unsigned int debug;
7ca30c52
MCC
65module_param(debug, int, 0644);
66MODULE_PARM_DESC(debug, "enable debug messages");
d72d5067
MCC
67
68/****************************************************************************
69 Module specific funtions
70 ****************************************************************************/
71
72/*
73 * BOARD Specific: Sets audio DMA
74 */
75
7ca30c52 76static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
d72d5067 77{
7ca30c52 78 struct tm6000_core *core = chip->core;
d72d5067 79
3f23a81a
MCC
80 dprintk(1, "Starting audio DMA\n");
81
7ca30c52 82 /* Enables audio */
4129e560 83 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x40, 0x40);
d72d5067 84
a59bff37
MCC
85 tm6000_set_audio_bitrate(core, 48000);
86
d72d5067
MCC
87 return 0;
88}
89
90/*
91 * BOARD Specific: Resets audio DMA
92 */
7ca30c52 93static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
d72d5067 94{
7ca30c52 95 struct tm6000_core *core = chip->core;
8aff8ba9 96
d72d5067
MCC
97 dprintk(1, "Stopping audio DMA\n");
98
8aff8ba9 99 /* Disables audio */
4129e560 100 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x00, 0x40);
d72d5067 101
d72d5067
MCC
102 return 0;
103}
104
54b78608 105static void dsp_buffer_free(struct snd_pcm_substream *substream)
d72d5067 106{
54b78608 107 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
d72d5067 108
7ca30c52 109 dprintk(2, "Freeing buffer\n");
d72d5067 110
54b78608
MCC
111 vfree(substream->runtime->dma_area);
112 substream->runtime->dma_area = NULL;
113 substream->runtime->dma_bytes = 0;
114}
115
116static int dsp_buffer_alloc(struct snd_pcm_substream *substream, int size)
117{
118 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
119
120 dprintk(2, "Allocating buffer\n");
d72d5067 121
54b78608
MCC
122 if (substream->runtime->dma_area) {
123 if (substream->runtime->dma_bytes > size)
124 return 0;
37f9619d 125
54b78608
MCC
126 dsp_buffer_free(substream);
127 }
128
129 substream->runtime->dma_area = vmalloc(size);
130 if (!substream->runtime->dma_area)
131 return -ENOMEM;
d72d5067 132
54b78608
MCC
133 substream->runtime->dma_bytes = size;
134
135 return 0;
d72d5067
MCC
136}
137
54b78608 138
d72d5067
MCC
139/****************************************************************************
140 ALSA PCM Interface
141 ****************************************************************************/
142
143/*
144 * Digital hardware definition
145 */
146#define DEFAULT_FIFO_SIZE 4096
7ca30c52
MCC
147
148static struct snd_pcm_hardware snd_tm6000_digital_hw = {
75f064f2
MCC
149 .info = SNDRV_PCM_INFO_BATCH |
150 SNDRV_PCM_INFO_MMAP |
d72d5067
MCC
151 SNDRV_PCM_INFO_INTERLEAVED |
152 SNDRV_PCM_INFO_BLOCK_TRANSFER |
153 SNDRV_PCM_INFO_MMAP_VALID,
154 .formats = SNDRV_PCM_FMTBIT_S16_LE,
155
75f064f2 156 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
37f9619d
AD
157 .rate_min = 48000,
158 .rate_max = 48000,
d72d5067
MCC
159 .channels_min = 2,
160 .channels_max = 2,
73f4d265
DB
161 .period_bytes_min = 64,
162 .period_bytes_max = 12544,
75f064f2 163 .periods_min = 2,
73f4d265 164 .periods_max = 98,
e8cbf035 165 .buffer_bytes_max = 62720 * 8,
d72d5067
MCC
166};
167
168/*
169 * audio pcm capture open callback
170 */
7ca30c52 171static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
d72d5067 172{
7ca30c52 173 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
d72d5067
MCC
174 struct snd_pcm_runtime *runtime = substream->runtime;
175 int err;
176
7ca30c52
MCC
177 err = snd_pcm_hw_constraint_pow2(runtime, 0,
178 SNDRV_PCM_HW_PARAM_PERIODS);
d72d5067
MCC
179 if (err < 0)
180 goto _error;
181
182 chip->substream = substream;
183
7ca30c52 184 runtime->hw = snd_tm6000_digital_hw;
75f064f2 185 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
d72d5067
MCC
186
187 return 0;
188_error:
7ca30c52 189 dprintk(1, "Error opening PCM!\n");
d72d5067
MCC
190 return err;
191}
192
193/*
194 * audio close callback
195 */
7ca30c52 196static int snd_tm6000_close(struct snd_pcm_substream *substream)
d72d5067 197{
06f08e3a
MCC
198 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
199 struct tm6000_core *core = chip->core;
200
201 if (atomic_read(&core->stream_started) > 0) {
202 atomic_set(&core->stream_started, 0);
203 schedule_work(&core->wq_trigger);
204 }
205
d72d5067
MCC
206 return 0;
207}
208
b17b8699
MCC
209static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
210{
e8cbf035
MCC
211 struct snd_tm6000_card *chip = core->adev;
212 struct snd_pcm_substream *substream = chip->substream;
213 struct snd_pcm_runtime *runtime;
214 int period_elapsed = 0;
215 unsigned int stride, buf_pos;
73f4d265 216 int length;
e8cbf035 217
06f08e3a
MCC
218 if (atomic_read(&core->stream_started) == 0)
219 return 0;
220
73f4d265
DB
221 if (!size || !substream) {
222 dprintk(1, "substream was NULL\n");
e8cbf035 223 return -EINVAL;
73f4d265 224 }
e8cbf035
MCC
225
226 runtime = substream->runtime;
73f4d265
DB
227 if (!runtime || !runtime->dma_area) {
228 dprintk(1, "runtime was NULL\n");
e8cbf035 229 return -EINVAL;
73f4d265 230 }
e8cbf035
MCC
231
232 buf_pos = chip->buf_pos;
233 stride = runtime->frame_bits >> 3;
234
73f4d265
DB
235 if (stride == 0) {
236 dprintk(1, "stride is zero\n");
237 return -EINVAL;
238 }
239
240 length = size / stride;
241 if (length == 0) {
242 dprintk(1, "%s: length was zero\n", __func__);
243 return -EINVAL;
244 }
245
e8cbf035
MCC
246 dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
247 runtime->dma_area, buf_pos,
248 (unsigned int)runtime->buffer_size, stride);
249
73f4d265
DB
250 if (buf_pos + length >= runtime->buffer_size) {
251 unsigned int cnt = runtime->buffer_size - buf_pos;
252 memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
253 memcpy(runtime->dma_area, buf + cnt * stride,
254 length * stride - cnt * stride);
e8cbf035 255 } else
73f4d265
DB
256 memcpy(runtime->dma_area + buf_pos * stride, buf,
257 length * stride);
e8cbf035 258
d684aee3 259 snd_pcm_stream_lock(substream);
e8cbf035 260
73f4d265
DB
261 chip->buf_pos += length;
262 if (chip->buf_pos >= runtime->buffer_size)
263 chip->buf_pos -= runtime->buffer_size;
264
265 chip->period_pos += length;
e8cbf035
MCC
266 if (chip->period_pos >= runtime->period_size) {
267 chip->period_pos -= runtime->period_size;
268 period_elapsed = 1;
269 }
b17b8699 270
d684aee3 271 snd_pcm_stream_unlock(substream);
73f4d265 272
e8cbf035
MCC
273 if (period_elapsed)
274 snd_pcm_period_elapsed(substream);
b17b8699
MCC
275
276 return 0;
277}
278
d72d5067
MCC
279/*
280 * hw_params callback
281 */
7ca30c52
MCC
282static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
283 struct snd_pcm_hw_params *hw_params)
d72d5067 284{
54b78608 285 int size, rc;
7ca30c52 286
54b78608 287 size = params_period_bytes(hw_params) * params_periods(hw_params);
7ca30c52 288
54b78608
MCC
289 rc = dsp_buffer_alloc(substream, size);
290 if (rc < 0)
291 return rc;
7ca30c52
MCC
292
293 return 0;
d72d5067
MCC
294}
295
296/*
297 * hw free callback
298 */
7ca30c52 299static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
d72d5067 300{
3f23a81a 301 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
06f08e3a 302 struct tm6000_core *core = chip->core;
3f23a81a 303
06f08e3a
MCC
304 if (atomic_read(&core->stream_started) > 0) {
305 atomic_set(&core->stream_started, 0);
306 schedule_work(&core->wq_trigger);
307 }
d72d5067 308
8d90e1e3 309 dsp_buffer_free(substream);
d72d5067
MCC
310 return 0;
311}
312
313/*
314 * prepare callback
315 */
7ca30c52 316static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
d72d5067 317{
3f23a81a
MCC
318 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
319
320 chip->buf_pos = 0;
321 chip->period_pos = 0;
322
d72d5067
MCC
323 return 0;
324}
325
7ca30c52 326
d72d5067
MCC
327/*
328 * trigger callback
329 */
06f08e3a
MCC
330static void audio_trigger(struct work_struct *work)
331{
332 struct tm6000_core *core = container_of(work, struct tm6000_core,
333 wq_trigger);
334 struct snd_tm6000_card *chip = core->adev;
335
336 if (atomic_read(&core->stream_started)) {
337 dprintk(1, "starting capture");
338 _tm6000_start_audio_dma(chip);
339 } else {
340 dprintk(1, "stopping capture");
341 _tm6000_stop_audio_dma(chip);
342 }
343}
344
7ca30c52 345static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
d72d5067 346{
7ca30c52 347 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
06f08e3a
MCC
348 struct tm6000_core *core = chip->core;
349 int err = 0;
d72d5067
MCC
350
351 switch (cmd) {
75f064f2
MCC
352 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
353 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
d72d5067 354 case SNDRV_PCM_TRIGGER_START:
06f08e3a 355 atomic_set(&core->stream_started, 1);
d72d5067 356 break;
75f064f2
MCC
357 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
358 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
d72d5067 359 case SNDRV_PCM_TRIGGER_STOP:
06f08e3a 360 atomic_set(&core->stream_started, 0);
d72d5067
MCC
361 break;
362 default:
7ca30c52 363 err = -EINVAL;
d72d5067
MCC
364 break;
365 }
06f08e3a 366 schedule_work(&core->wq_trigger);
d72d5067
MCC
367
368 return err;
369}
d72d5067
MCC
370/*
371 * pointer callback
372 */
7ca30c52 373static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
d72d5067 374{
7ca30c52 375 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
d72d5067 376
3f23a81a 377 return chip->buf_pos;
d72d5067
MCC
378}
379
75f064f2
MCC
380static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
381 unsigned long offset)
382{
383 void *pageptr = subs->runtime->dma_area + offset;
384
385 return vmalloc_to_page(pageptr);
386}
387
d72d5067
MCC
388/*
389 * operators
390 */
7ca30c52
MCC
391static struct snd_pcm_ops snd_tm6000_pcm_ops = {
392 .open = snd_tm6000_pcm_open,
393 .close = snd_tm6000_close,
d72d5067 394 .ioctl = snd_pcm_lib_ioctl,
7ca30c52
MCC
395 .hw_params = snd_tm6000_hw_params,
396 .hw_free = snd_tm6000_hw_free,
397 .prepare = snd_tm6000_prepare,
398 .trigger = snd_tm6000_card_trigger,
399 .pointer = snd_tm6000_pointer,
75f064f2 400 .page = snd_pcm_get_vmalloc_page,
d72d5067
MCC
401};
402
403/*
404 * create a PCM device
405 */
d72d5067 406
7ca30c52 407/* FIXME: Control interface - How to control volume/mute? */
d72d5067
MCC
408
409/****************************************************************************
410 Basic Flow for Sound Devices
411 ****************************************************************************/
412
d72d5067
MCC
413/*
414 * Alsa Constructor - Component probe
415 */
3d1a51db 416static int tm6000_audio_init(struct tm6000_core *dev)
d72d5067 417{
d77057f2
SR
418 struct snd_card *card;
419 struct snd_tm6000_card *chip;
420 int rc;
421 static int devnr;
422 char component[14];
423 struct snd_pcm *pcm;
424
425 if (!dev)
426 return 0;
6ad6e88a 427
d77057f2 428 if (devnr >= SNDRV_CARDS)
6ad6e88a
MCC
429 return -ENODEV;
430
d77057f2 431 if (!enable[devnr])
6ad6e88a
MCC
432 return -ENOENT;
433
0e57ff8d 434 rc = snd_card_create(index[devnr], "tm6000", THIS_MODULE, 0, &card);
427f7fac 435 if (rc < 0) {
d77057f2 436 snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
427f7fac 437 return rc;
6ad6e88a 438 }
3f23a81a
MCC
439 strcpy(card->driver, "tm6000-alsa");
440 strcpy(card->shortname, "TM5600/60x0");
441 sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
442 dev->udev->bus->busnum, dev->udev->devnum);
443
444 sprintf(component, "USB%04x:%04x",
445 le16_to_cpu(dev->udev->descriptor.idVendor),
446 le16_to_cpu(dev->udev->descriptor.idProduct));
447 snd_component_add(card, component);
448 snd_card_set_dev(card, &dev->udev->dev);
6ad6e88a 449
d77057f2 450 chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
6ad6e88a
MCC
451 if (!chip) {
452 rc = -ENOMEM;
453 goto error;
454 }
455
3f23a81a
MCC
456 chip->core = dev;
457 chip->card = card;
458 dev->adev = chip;
d77057f2 459 spin_lock_init(&chip->reg_lock);
3f23a81a 460
d77057f2 461 rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
6ad6e88a 462 if (rc < 0)
cd6541c0 463 goto error_chip;
6ad6e88a 464
d77057f2 465 pcm->info_flags = 0;
3f23a81a 466 pcm->private_data = chip;
d77057f2 467 strcpy(pcm->name, "Trident TM5600/60x0");
d77057f2 468
3f23a81a 469 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
d77057f2 470
06f08e3a 471 INIT_WORK(&dev->wq_trigger, audio_trigger);
6ad6e88a
MCC
472 rc = snd_card_register(card);
473 if (rc < 0)
cd6541c0 474 goto error_chip;
6ad6e88a 475
d684aee3 476 dprintk(1, "Registered audio driver for %s\n", card->longname);
d72d5067 477
d72d5067 478 return 0;
d72d5067 479
cd6541c0
KV
480error_chip:
481 kfree(chip);
482 dev->adev = NULL;
6ad6e88a
MCC
483error:
484 snd_card_free(card);
485 return rc;
d72d5067
MCC
486}
487
0439db75
SR
488static int tm6000_audio_fini(struct tm6000_core *dev)
489{
e839776f 490 struct snd_tm6000_card *chip;
d77057f2
SR
491
492 if (!dev)
493 return 0;
e839776f 494 chip = dev->adev;
d77057f2
SR
495
496 if (!chip)
497 return 0;
498
499 if (!chip->card)
500 return 0;
501
502 snd_card_free(chip->card);
503 chip->card = NULL;
504 kfree(chip);
505 dev->adev = NULL;
506
0439db75
SR
507 return 0;
508}
509
3d1a51db 510static struct tm6000_ops audio_ops = {
39e1256b 511 .type = TM6000_AUDIO,
0439db75
SR
512 .name = "TM6000 Audio Extension",
513 .init = tm6000_audio_init,
514 .fini = tm6000_audio_fini,
b17b8699 515 .fillbuf = tm6000_fillbuf,
0439db75
SR
516};
517
518static int __init tm6000_alsa_register(void)
519{
520 return tm6000_register_extension(&audio_ops);
521}
522
523static void __exit tm6000_alsa_unregister(void)
524{
525 tm6000_unregister_extension(&audio_ops);
526}
527
528module_init(tm6000_alsa_register);
529module_exit(tm6000_alsa_unregister);
This page took 0.349981 seconds and 5 git commands to generate.