]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/tm6000/tm6000-alsa.c
Staging: brcm80211: fix usage of roundup in structures
[net-next-2.6.git] / drivers / staging / tm6000 / tm6000-alsa.c
1 /*
2  *
3  *  Support for audio capture for tm5600/6000/6010
4  *    (c) 2007-2008 Mauro Carvalho Chehab <mchehab@redhat.com>
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 <asm/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 int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
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 <mchehab@redhat.com>");
60 MODULE_LICENSE("GPL");
61 MODULE_SUPPORTED_DEVICE("{{Trident,tm5600},"
62                         "{{Trident,tm6000},"
63                         "{{Trident,tm6010}");
64 static unsigned int debug;
65 module_param(debug, int, 0644);
66 MODULE_PARM_DESC(debug, "enable debug messages");
67
68 /****************************************************************************
69                         Module specific funtions
70  ****************************************************************************/
71
72 /*
73  * BOARD Specific: Sets audio DMA
74  */
75
76 static int _tm6000_start_audio_dma(struct snd_tm6000_card *chip)
77 {
78         struct tm6000_core *core = chip->core;
79         int val;
80
81         dprintk(1, "Starting audio DMA\n");
82
83         /* Enables audio */
84         val = tm6000_get_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x0);
85         val |= 0x20;
86         tm6000_set_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
87
88         tm6000_set_audio_bitrate(core, 48000);
89
90         tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0x80);
91
92         return 0;
93 }
94
95 /*
96  * BOARD Specific: Resets audio DMA
97  */
98 static int _tm6000_stop_audio_dma(struct snd_tm6000_card *chip)
99 {
100         struct tm6000_core *core = chip->core;
101         int val;
102         dprintk(1, "Stopping audio DMA\n");
103
104         /* Enables audio */
105         val = tm6000_get_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, 0x0);
106         val &= ~0x20;
107         tm6000_set_reg(core, TM6010_REQ07_RCC_ACTIVE_VIDEO_IF, val);
108
109         tm6000_set_reg(core, TM6010_REQ08_R01_A_INIT, 0);
110
111         return 0;
112 }
113
114 static void dsp_buffer_free(struct snd_pcm_substream *substream)
115 {
116         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
117
118         dprintk(2, "Freeing buffer\n");
119
120         vfree(substream->runtime->dma_area);
121         substream->runtime->dma_area = NULL;
122         substream->runtime->dma_bytes = 0;
123 }
124
125 static int dsp_buffer_alloc(struct snd_pcm_substream *substream, int size)
126 {
127         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
128
129         dprintk(2, "Allocating buffer\n");
130
131         if (substream->runtime->dma_area) {
132                 if (substream->runtime->dma_bytes > size)
133                         return 0;
134                 dsp_buffer_free(substream);
135         }
136
137         substream->runtime->dma_area = vmalloc(size);
138         if (!substream->runtime->dma_area)
139                 return -ENOMEM;
140
141         substream->runtime->dma_bytes = size;
142
143         return 0;
144 }
145
146
147 /****************************************************************************
148                                 ALSA PCM Interface
149  ****************************************************************************/
150
151 /*
152  * Digital hardware definition
153  */
154 #define DEFAULT_FIFO_SIZE       4096
155
156 static struct snd_pcm_hardware snd_tm6000_digital_hw = {
157         .info = SNDRV_PCM_INFO_MMAP |
158                 SNDRV_PCM_INFO_INTERLEAVED |
159                 SNDRV_PCM_INFO_BLOCK_TRANSFER |
160                 SNDRV_PCM_INFO_MMAP_VALID,
161         .formats = SNDRV_PCM_FMTBIT_S16_LE,
162
163         .rates =                SNDRV_PCM_RATE_48000,
164         .rate_min =             48000,
165         .rate_max =             48000,
166         .channels_min = 2,
167         .channels_max = 2,
168         .period_bytes_min = 62720,
169         .period_bytes_max = 62720,
170         .periods_min = 1,
171         .periods_max = 1024,
172         .buffer_bytes_max = 62720 * 8,
173 };
174
175 /*
176  * audio pcm capture open callback
177  */
178 static int snd_tm6000_pcm_open(struct snd_pcm_substream *substream)
179 {
180         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
181         struct snd_pcm_runtime *runtime = substream->runtime;
182         int err;
183
184         err = snd_pcm_hw_constraint_pow2(runtime, 0,
185                                          SNDRV_PCM_HW_PARAM_PERIODS);
186         if (err < 0)
187                 goto _error;
188
189         chip->substream = substream;
190
191         runtime->hw = snd_tm6000_digital_hw;
192
193         return 0;
194 _error:
195         dprintk(1, "Error opening PCM!\n");
196         return err;
197 }
198
199 /*
200  * audio close callback
201  */
202 static int snd_tm6000_close(struct snd_pcm_substream *substream)
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
215         if (!size || !substream)
216                 return -EINVAL;
217
218         runtime = substream->runtime;
219         if (!runtime || !runtime->dma_area)
220                 return -EINVAL;
221
222         buf_pos = chip->buf_pos;
223         stride = runtime->frame_bits >> 3;
224
225         dprintk(1, "Copying %d bytes at %p[%d] - buf size=%d x %d\n", size,
226                 runtime->dma_area, buf_pos,
227                 (unsigned int)runtime->buffer_size, stride);
228
229         if (buf_pos + size >= runtime->buffer_size * stride) {
230                 unsigned int cnt = runtime->buffer_size * stride - buf_pos;
231                 memcpy(runtime->dma_area + buf_pos, buf, cnt);
232                 memcpy(runtime->dma_area, buf + cnt, size - cnt);
233         } else
234                 memcpy(runtime->dma_area + buf_pos, buf, size);
235
236         chip->buf_pos += size;
237         if (chip->buf_pos >= runtime->buffer_size * stride)
238                 chip->buf_pos -= runtime->buffer_size * stride;
239
240         chip->period_pos += size;
241         if (chip->period_pos >= runtime->period_size) {
242                 chip->period_pos -= runtime->period_size;
243                 period_elapsed = 1;
244         }
245
246         if (period_elapsed)
247                 snd_pcm_period_elapsed(substream);
248
249         return 0;
250 }
251
252 /*
253  * hw_params callback
254  */
255 static int snd_tm6000_hw_params(struct snd_pcm_substream *substream,
256                               struct snd_pcm_hw_params *hw_params)
257 {
258         int size, rc;
259
260         size = params_period_bytes(hw_params) * params_periods(hw_params);
261
262         rc = dsp_buffer_alloc(substream, size);
263         if (rc < 0)
264                 return rc;
265
266         return 0;
267 }
268
269 /*
270  * hw free callback
271  */
272 static int snd_tm6000_hw_free(struct snd_pcm_substream *substream)
273 {
274         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
275
276         _tm6000_stop_audio_dma(chip);
277
278         return 0;
279 }
280
281 /*
282  * prepare callback
283  */
284 static int snd_tm6000_prepare(struct snd_pcm_substream *substream)
285 {
286         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
287
288         chip->buf_pos = 0;
289         chip->period_pos = 0;
290
291         return 0;
292 }
293
294
295 /*
296  * trigger callback
297  */
298 static int snd_tm6000_card_trigger(struct snd_pcm_substream *substream, int cmd)
299 {
300         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
301         int err;
302
303         spin_lock(&chip->reg_lock);
304
305         switch (cmd) {
306         case SNDRV_PCM_TRIGGER_START:
307                 err = _tm6000_start_audio_dma(chip);
308                 break;
309         case SNDRV_PCM_TRIGGER_STOP:
310                 err = _tm6000_stop_audio_dma(chip);
311                 break;
312         default:
313                 err = -EINVAL;
314                 break;
315         }
316
317         spin_unlock(&chip->reg_lock);
318
319         return err;
320 }
321
322 /*
323  * pointer callback
324  */
325 static snd_pcm_uframes_t snd_tm6000_pointer(struct snd_pcm_substream *substream)
326 {
327         struct snd_tm6000_card *chip = snd_pcm_substream_chip(substream);
328
329         return chip->buf_pos;
330 }
331
332 /*
333  * operators
334  */
335 static struct snd_pcm_ops snd_tm6000_pcm_ops = {
336         .open = snd_tm6000_pcm_open,
337         .close = snd_tm6000_close,
338         .ioctl = snd_pcm_lib_ioctl,
339         .hw_params = snd_tm6000_hw_params,
340         .hw_free = snd_tm6000_hw_free,
341         .prepare = snd_tm6000_prepare,
342         .trigger = snd_tm6000_card_trigger,
343         .pointer = snd_tm6000_pointer,
344 };
345
346 /*
347  * create a PCM device
348  */
349
350 /* FIXME: Control interface - How to control volume/mute? */
351
352 /****************************************************************************
353                         Basic Flow for Sound Devices
354  ****************************************************************************/
355
356 /*
357  * Alsa Constructor - Component probe
358  */
359 int tm6000_audio_init(struct tm6000_core *dev)
360 {
361         struct snd_card         *card;
362         struct snd_tm6000_card  *chip;
363         int                     rc;
364         static int              devnr;
365         char                    component[14];
366         struct snd_pcm          *pcm;
367
368         if (!dev)
369                 return 0;
370
371         if (devnr >= SNDRV_CARDS)
372                 return -ENODEV;
373
374         if (!enable[devnr])
375                 return -ENOENT;
376
377         rc = snd_card_create(index[devnr], "tm6000", THIS_MODULE, 0, &card);
378         if (rc < 0) {
379                 snd_printk(KERN_ERR "cannot create card instance %d\n", devnr);
380                 return rc;
381         }
382         strcpy(card->driver, "tm6000-alsa");
383         strcpy(card->shortname, "TM5600/60x0");
384         sprintf(card->longname, "TM5600/60x0 Audio at bus %d device %d",
385                 dev->udev->bus->busnum, dev->udev->devnum);
386
387         sprintf(component, "USB%04x:%04x",
388                 le16_to_cpu(dev->udev->descriptor.idVendor),
389                 le16_to_cpu(dev->udev->descriptor.idProduct));
390         snd_component_add(card, component);
391         snd_card_set_dev(card, &dev->udev->dev);
392
393         chip = kzalloc(sizeof(struct snd_tm6000_card), GFP_KERNEL);
394         if (!chip) {
395                 rc = -ENOMEM;
396                 goto error;
397         }
398
399         chip->core = dev;
400         chip->card = card;
401         dev->adev = chip;
402         spin_lock_init(&chip->reg_lock);
403
404         rc = snd_pcm_new(card, "TM6000 Audio", 0, 0, 1, &pcm);
405         if (rc < 0)
406                 goto error_chip;
407
408         pcm->info_flags = 0;
409         pcm->private_data = chip;
410         strcpy(pcm->name, "Trident TM5600/60x0");
411
412         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_tm6000_pcm_ops);
413
414         rc = snd_card_register(card);
415         if (rc < 0)
416                 goto error_chip;
417
418         dprintk(1,"Registered audio driver for %s\n", card->longname);
419
420         return 0;
421
422 error_chip:
423         kfree(chip);
424         dev->adev = NULL;
425 error:
426         snd_card_free(card);
427         return rc;
428 }
429
430 static int tm6000_audio_fini(struct tm6000_core *dev)
431 {
432         struct snd_tm6000_card  *chip = dev->adev;
433
434         if (!dev)
435                 return 0;
436
437         if (!chip)
438                 return 0;
439
440         if (!chip->card)
441                 return 0;
442
443         snd_card_free(chip->card);
444         chip->card = NULL;
445         kfree(chip);
446         dev->adev = NULL;
447
448         return 0;
449 }
450
451 struct tm6000_ops audio_ops = {
452         .type   = TM6000_AUDIO,
453         .name   = "TM6000 Audio Extension",
454         .init   = tm6000_audio_init,
455         .fini   = tm6000_audio_fini,
456         .fillbuf = tm6000_fillbuf,
457 };
458
459 static int __init tm6000_alsa_register(void)
460 {
461         return tm6000_register_extension(&audio_ops);
462 }
463
464 static void __exit tm6000_alsa_unregister(void)
465 {
466         tm6000_unregister_extension(&audio_ops);
467 }
468
469 module_init(tm6000_alsa_register);
470 module_exit(tm6000_alsa_unregister);