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