perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / media / usb / tm6000 / tm6000-alsa.c
blobf18cffae4c85a15bbaf38a170de44fa0e6c637a3
1 // SPDX-License-Identifier: GPL-2.0
2 // Support for audio capture for tm5600/6000/6010
3 // Copyright (c) 2007-2008 Mauro Carvalho Chehab <mchehab@kernel.org>
4 //
5 // Based on cx88-alsa.c
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/device.h>
10 #include <linux/interrupt.h>
11 #include <linux/usb.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
15 #include <linux/delay.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/control.h>
20 #include <sound/initval.h>
23 #include "tm6000.h"
24 #include "tm6000-regs.h"
26 #undef dprintk
28 #define dprintk(level, fmt, arg...) do { \
29 if (debug >= level) \
30 printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg); \
31 } while (0)
33 /****************************************************************************
34 Module global static vars
35 ****************************************************************************/
37 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
39 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
41 module_param_array(enable, bool, NULL, 0444);
42 MODULE_PARM_DESC(enable, "Enable tm6000x soundcard. default enabled.");
44 module_param_array(index, int, NULL, 0444);
45 MODULE_PARM_DESC(index, "Index value for tm6000x capture interface(s).");
48 /****************************************************************************
49 Module macros
50 ****************************************************************************/
52 MODULE_DESCRIPTION("ALSA driver module for tm5600/tm6000/tm6010 based TV cards");
53 MODULE_AUTHOR("Mauro Carvalho Chehab");
54 MODULE_LICENSE("GPL v2");
55 MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},{{Trident,tm6000},{{Trident,tm6010}");
56 static unsigned int debug;
57 module_param(debug, int, 0644);
58 MODULE_PARM_DESC(debug, "enable debug messages");
60 /****************************************************************************
61 Module specific funtions
62 ****************************************************************************/
65 * BOARD Specific: Sets audio DMA
68 static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
70 struct tm6000_core *core = chip->core;
72 dprintk(1, "Starting audio DMA\n");
74 /* Enables audio */
75 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x40, 0x40);
77 tm6000_set_audio_bitrate(core, 48000);
79 return 0;
83 * BOARD Specific: Resets audio DMA
85 static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
87 struct tm6000_core *core = chip->core;
89 dprintk(1, "Stopping audio DMA\n");
91 /* Disables audio */
92 tm6000_set_reg_mask(core, TM6010_REQ07_RCC_ACTIVE_IF, 0x00, 0x40);
94 return 0;
97 static void dsp_buffer_free(struct snd_pcm_substream *substream)
99 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
101 dprintk(2, "Freeing buffer\n");
103 vfree(substream->runtime->dma_area);
104 substream->runtime->dma_area = NULL;
105 substream->runtime->dma_bytes = 0;
108 static int dsp_buffer_alloc(struct snd_pcm_substream *substream, int size)
110 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
112 dprintk(2, "Allocating buffer\n");
114 if (substream->runtime->dma_area) {
115 if (substream->runtime->dma_bytes > size)
116 return 0;
118 dsp_buffer_free(substream);
121 substream->runtime->dma_area = vmalloc(size);
122 if (!substream->runtime->dma_area)
123 return -ENOMEM;
125 substream->runtime->dma_bytes = size;
127 return 0;
131 /****************************************************************************
132 ALSA PCM Interface
133 ****************************************************************************/
136 * Digital hardware definition
138 #define DEFAULT_FIFO_SIZE 4096
140 static const struct snd_pcm_hardware snd_tm6000_digital_hw = {
141 .info = SNDRV_PCM_INFO_BATCH |
142 SNDRV_PCM_INFO_MMAP |
143 SNDRV_PCM_INFO_INTERLEAVED |
144 SNDRV_PCM_INFO_BLOCK_TRANSFER |
145 SNDRV_PCM_INFO_MMAP_VALID,
146 .formats = SNDRV_PCM_FMTBIT_S16_LE,
148 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
149 .rate_min = 48000,
150 .rate_max = 48000,
151 .channels_min = 2,
152 .channels_max = 2,
153 .period_bytes_min = 64,
154 .period_bytes_max = 12544,
155 .periods_min = 2,
156 .periods_max = 98,
157 .buffer_bytes_max = 62720 * 8,
161 * audio pcm capture open callback
163 static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
165 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
166 struct snd_pcm_runtime *runtime = substream->runtime;
167 int err;
169 err = snd_pcm_hw_constraint_pow2(runtime, 0,
170 SNDRV_PCM_HW_PARAM_PERIODS);
171 if (err < 0)
172 goto _error;
174 chip->substream = substream;
176 runtime->hw = snd_tm6000_digital_hw;
177 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
179 return 0;
180 _error:
181 dprintk(1, "Error opening PCM!\n");
182 return err;
186 * audio close callback
188 static int snd_tm6000_close(struct snd_pcm_substream *substream)
190 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
191 struct tm6000_core *core = chip->core;
193 if (atomic_read(&core->stream_started) > 0) {
194 atomic_set(&core->stream_started, 0);
195 schedule_work(&core->wq_trigger);
198 return 0;
201 static int tm6000_fillbuf(struct tm6000_core *core, char *buf, int size)
203 struct snd_tm6000_card *chip = core->adev;
204 struct snd_pcm_substream *substream = chip->substream;
205 struct snd_pcm_runtime *runtime;
206 int period_elapsed = 0;
207 unsigned int stride, buf_pos;
208 int length;
210 if (atomic_read(&core->stream_started) == 0)
211 return 0;
213 if (!size || !substream) {
214 dprintk(1, "substream was NULL\n");
215 return -EINVAL;
218 runtime = substream->runtime;
219 if (!runtime || !runtime->dma_area) {
220 dprintk(1, "runtime was NULL\n");
221 return -EINVAL;
224 buf_pos = chip->buf_pos;
225 stride = runtime->frame_bits >> 3;
227 if (stride == 0) {
228 dprintk(1, "stride is zero\n");
229 return -EINVAL;
232 length = size / stride;
233 if (length == 0) {
234 dprintk(1, "%s: length was zero\n", __func__);
235 return -EINVAL;
238 dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
239 runtime->dma_area, buf_pos,
240 (unsigned int)runtime->buffer_size, stride);
242 if (buf_pos + length >= runtime->buffer_size) {
243 unsigned int cnt = runtime->buffer_size - buf_pos;
244 memcpy(runtime->dma_area + buf_pos * stride, buf, cnt * stride);
245 memcpy(runtime->dma_area, buf + cnt * stride,
246 length * stride - cnt * stride);
247 } else
248 memcpy(runtime->dma_area + buf_pos * stride, buf,
249 length * stride);
251 snd_pcm_stream_lock(substream);
253 chip->buf_pos += length;
254 if (chip->buf_pos >= runtime->buffer_size)
255 chip->buf_pos -= runtime->buffer_size;
257 chip->period_pos += length;
258 if (chip->period_pos >= runtime->period_size) {
259 chip->period_pos -= runtime->period_size;
260 period_elapsed = 1;
263 snd_pcm_stream_unlock(substream);
265 if (period_elapsed)
266 snd_pcm_period_elapsed(substream);
268 return 0;
272 * hw_params callback
274 static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
275 struct snd_pcm_hw_params *hw_params)
277 int size, rc;
279 size = params_period_bytes(hw_params) * params_periods(hw_params);
281 rc = dsp_buffer_alloc(substream, size);
282 if (rc < 0)
283 return rc;
285 return 0;
289 * hw free callback
291 static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
293 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
294 struct tm6000_core *core = chip->core;
296 if (atomic_read(&core->stream_started) > 0) {
297 atomic_set(&core->stream_started, 0);
298 schedule_work(&core->wq_trigger);
301 dsp_buffer_free(substream);
302 return 0;
306 * prepare callback
308 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
310 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
312 chip->buf_pos = 0;
313 chip->period_pos = 0;
315 return 0;
320 * trigger callback
322 static void audio_trigger(struct work_struct *work)
324 struct tm6000_core *core = container_of(work, struct tm6000_core,
325 wq_trigger);
326 struct snd_tm6000_card *chip = core->adev;
328 if (atomic_read(&core->stream_started)) {
329 dprintk(1, "starting capture");
330 _tm6000_start_audio_dma(chip);
331 } else {
332 dprintk(1, "stopping capture");
333 _tm6000_stop_audio_dma(chip);
337 static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
339 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
340 struct tm6000_core *core = chip->core;
341 int err = 0;
343 switch (cmd) {
344 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */
345 case SNDRV_PCM_TRIGGER_RESUME: /* fall through */
346 case SNDRV_PCM_TRIGGER_START:
347 atomic_set(&core->stream_started, 1);
348 break;
349 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */
350 case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */
351 case SNDRV_PCM_TRIGGER_STOP:
352 atomic_set(&core->stream_started, 0);
353 break;
354 default:
355 err = -EINVAL;
356 break;
358 schedule_work(&core->wq_trigger);
360 return err;
363 * pointer callback
365 static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
367 struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
369 return chip->buf_pos;
372 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
373 unsigned long offset)
375 void *pageptr = subs->runtime->dma_area + offset;
377 return vmalloc_to_page(pageptr);
381 * operators
383 static const struct snd_pcm_ops snd_tm6000_pcm_ops = {
384 .open = snd_tm6000_pcm_open,
385 .close = snd_tm6000_close,
386 .ioctl = snd_pcm_lib_ioctl,
387 .hw_params = snd_tm6000_hw_params,
388 .hw_free = snd_tm6000_hw_free,
389 .prepare = snd_tm6000_prepare,
390 .trigger = snd_tm6000_card_trigger,
391 .pointer = snd_tm6000_pointer,
392 .page = snd_pcm_get_vmalloc_page,
396 * create a PCM device
399 /* FIXME: Control interface - How to control volume/mute? */
401 /****************************************************************************
402 Basic Flow for Sound Devices
403 ****************************************************************************/
406 * Alsa Constructor - Component probe
408 static int tm6000_audio_init(struct tm6000_core *dev)
410 struct snd_card *card;
411 struct snd_tm6000_card *chip;
412 int rc;
413 static int devnr;
414 char component[14];
415 struct snd_pcm *pcm;
417 if (!dev)
418 return 0;
420 if (devnr >= SNDRV_CARDS)
421 return -ENODEV;
423 if (!enable[devnr])
424 return -ENOENT;
426 rc = snd_card_new(&dev->udev->dev, index[devnr], "tm6000",
427 THIS_MODULE, 0, &card);
428 if (rc < 0) {
429 snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
430 return rc;
432 strcpy(card->driver, "tm6000-alsa");
433 strcpy(card->shortname, "TM5600/60x0");
434 sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
435 dev->udev->bus->busnum, dev->udev->devnum);
437 sprintf(component, "USB%04x:%04x",
438 le16_to_cpu(dev->udev->descriptor.idVendor),
439 le16_to_cpu(dev->udev->descriptor.idProduct));
440 snd_component_add(card, component);
442 chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
443 if (!chip) {
444 rc = -ENOMEM;
445 goto error;
448 chip->core = dev;
449 chip->card = card;
450 dev->adev = chip;
451 spin_lock_init(&chip->reg_lock);
453 rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
454 if (rc < 0)
455 goto error_chip;
457 pcm->info_flags = 0;
458 pcm->private_data = chip;
459 strcpy(pcm->name, "Trident TM5600/60x0");
461 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
463 INIT_WORK(&dev->wq_trigger, audio_trigger);
464 rc = snd_card_register(card);
465 if (rc < 0)
466 goto error_chip;
468 dprintk(1, "Registered audio driver for %s\n", card->longname);
470 return 0;
472 error_chip:
473 kfree(chip);
474 dev->adev = NULL;
475 error:
476 snd_card_free(card);
477 return rc;
480 static int tm6000_audio_fini(struct tm6000_core *dev)
482 struct snd_tm6000_card *chip;
484 if (!dev)
485 return 0;
486 chip = dev->adev;
488 if (!chip)
489 return 0;
491 if (!chip->card)
492 return 0;
494 snd_card_free(chip->card);
495 chip->card = NULL;
496 kfree(chip);
497 dev->adev = NULL;
499 return 0;
502 static struct tm6000_ops audio_ops = {
503 .type = TM6000_AUDIO,
504 .name = "TM6000 Audio Extension",
505 .init = tm6000_audio_init,
506 .fini = tm6000_audio_fini,
507 .fillbuf = tm6000_fillbuf,
510 static int __init tm6000_alsa_register(void)
512 return tm6000_register_extension(&audio_ops);
515 static void __exit tm6000_alsa_unregister(void)
517 tm6000_unregister_extension(&audio_ops);
520 module_init(tm6000_alsa_register);
521 module_exit(tm6000_alsa_unregister);