V4L/DVB (6951): Integrates em28xx-audio.c into em28xx kernel module
[linux-2.6] / drivers / media / video / em28xx / em28xx-audio.c
1 /*
2  *  Empiatech em28x1 audio extension
3  *
4  *  Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
5  *
6  *  Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
7  *      - Port to work with the in-kernel driver
8  *      - Several cleanups
9  *
10  *  This driver is based on my previous au600 usb pstn audio driver
11  *  and inherits all the copyrights
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 2 of the License, or
16  *  (at your option) any later version.
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software
25  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/usb.h>
30 #include <linux/init.h>
31 #include <linux/sound.h>
32 #include <linux/spinlock.h>
33 #include <linux/soundcard.h>
34 #include <linux/slab.h>
35 #include <linux/vmalloc.h>
36 #include <linux/proc_fs.h>
37 #include <linux/module.h>
38 #include <sound/driver.h>
39 #include <sound/core.h>
40 #include <sound/pcm.h>
41 #include <sound/pcm_params.h>
42 #include <sound/info.h>
43 #include <sound/initval.h>
44 #include <sound/control.h>
45 #include <media/v4l2-common.h>
46 #include "em28xx.h"
47
48 static int debug;
49 module_param(debug, int, 0644);
50 MODULE_PARM_DESC(debug, "activates debug info");
51
52 #define dprintk(fmt, arg...) do {                                       \
53             if (debug)                                                  \
54                 printk(KERN_INFO "em28xx-audio %s: " fmt,               \
55                                   __FUNCTION__, ##arg);                 \
56         } while (0)
57
58 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
59
60 static int em28xx_isoc_audio_deinit(struct em28xx *dev)
61 {
62         int i;
63
64         dprintk("Stopping isoc\n");
65         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
66                 usb_kill_urb(dev->adev->urb[i]);
67                 usb_free_urb(dev->adev->urb[i]);
68                 dev->adev->urb[i] = NULL;
69         }
70
71         return 0;
72 }
73
74 static void em28xx_audio_isocirq(struct urb *urb)
75 {
76         struct em28xx            *dev = urb->context;
77         int                      i;
78         unsigned int             oldptr;
79         unsigned long            flags;
80         int                      period_elapsed = 0;
81         int                      status;
82         unsigned char            *cp;
83         unsigned int             stride;
84         struct snd_pcm_substream *substream;
85         struct snd_pcm_runtime   *runtime;
86         if (dev->adev->capture_pcm_substream) {
87                 substream = dev->adev->capture_pcm_substream;
88                 runtime = substream->runtime;
89                 stride = runtime->frame_bits >> 3;
90
91                 for (i = 0; i < urb->number_of_packets; i++) {
92                         int length =
93                             urb->iso_frame_desc[i].actual_length / stride;
94                         cp = (unsigned char *)urb->transfer_buffer +
95                             urb->iso_frame_desc[i].offset;
96
97                         if (!length)
98                                 continue;
99
100                         spin_lock_irqsave(&dev->adev->slock, flags);
101
102                         oldptr = dev->adev->hwptr_done_capture;
103                         dev->adev->hwptr_done_capture += length;
104                         if (dev->adev->hwptr_done_capture >=
105                             runtime->buffer_size)
106                                 dev->adev->hwptr_done_capture -=
107                                     runtime->buffer_size;
108
109                         dev->adev->capture_transfer_done += length;
110                         if (dev->adev->capture_transfer_done >=
111                             runtime->period_size) {
112                                 dev->adev->capture_transfer_done -=
113                                     runtime->period_size;
114                                 period_elapsed = 1;
115                         }
116
117                         spin_unlock_irqrestore(&dev->adev->slock, flags);
118
119                         if (oldptr + length >= runtime->buffer_size) {
120                                 unsigned int cnt =
121                                     runtime->buffer_size - oldptr - 1;
122                                 memcpy(runtime->dma_area + oldptr * stride, cp,
123                                        cnt * stride);
124                                 memcpy(runtime->dma_area, cp + cnt,
125                                        length * stride - cnt * stride);
126                         } else {
127                                 memcpy(runtime->dma_area + oldptr * stride, cp,
128                                        length * stride);
129                         }
130                 }
131                 if (period_elapsed)
132                         snd_pcm_period_elapsed(substream);
133         }
134         urb->status = 0;
135
136         if (dev->adev->shutdown)
137                 return;
138
139         status = usb_submit_urb(urb, GFP_ATOMIC);
140         if (status < 0) {
141                 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
142                               status);
143         }
144         return;
145 }
146
147 static int em28xx_init_audio_isoc(struct em28xx *dev)
148 {
149         int       i, errCode;
150         const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
151                             EM28XX_AUDIO_MAX_PACKET_SIZE;
152
153         dprintk("Starting isoc transfers\n");
154
155         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
156                 struct urb *urb;
157                 int j, k;
158
159                 dev->adev->transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
160                 if (!dev->adev->transfer_buffer[i])
161                         return -ENOMEM;
162
163                 memset(dev->adev->transfer_buffer[i], 0x80, sb_size);
164                 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
165                 if (!urb)
166                         return -ENOMEM;
167
168                 urb->dev = dev->udev;
169                 urb->context = dev;
170                 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
171                 urb->transfer_flags = URB_ISO_ASAP;
172                 urb->transfer_buffer = dev->adev->transfer_buffer[i];
173                 urb->interval = 1;
174                 urb->complete = em28xx_audio_isocirq;
175                 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
176                 urb->transfer_buffer_length = sb_size;
177
178                 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
179                              j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
180                         urb->iso_frame_desc[j].offset = k;
181                         urb->iso_frame_desc[j].length =
182                             EM28XX_AUDIO_MAX_PACKET_SIZE;
183                 }
184                 dev->adev->urb[i] = urb;
185         }
186
187         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
188                 errCode = usb_submit_urb(dev->adev->urb[i], GFP_ATOMIC);
189                 if (errCode) {
190                         em28xx_isoc_audio_deinit(dev);
191
192                         return errCode;
193                 }
194         }
195
196         return 0;
197 }
198
199 static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
200 {
201         dprintk("%s transfer\n", (dev->adev->capture_stream == STREAM_ON)?
202                                  "stop" : "start");
203
204         switch (cmd) {
205         case EM28XX_CAPTURE_STREAM_EN:
206                 if (dev->adev->capture_stream == STREAM_OFF && arg == 1) {
207                         dev->adev->capture_stream = STREAM_ON;
208                         em28xx_init_audio_isoc(dev);
209                 } else if (dev->adev->capture_stream == STREAM_ON && arg == 0) {
210                         dev->adev->capture_stream = STREAM_OFF;
211                         em28xx_isoc_audio_deinit(dev);
212                 } else {
213                         printk(KERN_ERR "An underrun very likely occurred. "
214                                         "Ignoring it.\n");
215                 }
216                 return 0;
217         default:
218                 return -EINVAL;
219         }
220 }
221
222 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
223                                         size_t size)
224 {
225         struct snd_pcm_runtime *runtime = subs->runtime;
226
227         dprintk("Alocating vbuffer\n");
228         if (runtime->dma_area) {
229                 if (runtime->dma_bytes > size)
230                         return 0;
231
232                 vfree(runtime->dma_area);
233         }
234         runtime->dma_area = vmalloc(size);
235         if (!runtime->dma_area)
236                 return -ENOMEM;
237
238         runtime->dma_bytes = size;
239
240         return 0;
241 }
242
243 static struct snd_pcm_hardware snd_em28xx_hw_capture = {
244         .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
245                 SNDRV_PCM_INFO_MMAP           |
246                 SNDRV_PCM_INFO_INTERLEAVED    |
247                 SNDRV_PCM_INFO_MMAP_VALID,
248
249         .formats = SNDRV_PCM_FMTBIT_S16_LE,
250
251         .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
252
253         .rate_min = 48000,
254         .rate_max = 48000,
255         .channels_min = 2,
256         .channels_max = 2,
257         .buffer_bytes_max = 62720 * 8,  /* just about the value in usbaudio.c */
258         .period_bytes_min = 64,         /* 12544/2, */
259         .period_bytes_max = 12544,
260         .periods_min = 2,
261         .periods_max = 98,              /* 12544, */
262 };
263
264 static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
265 {
266         struct em28xx *dev = snd_pcm_substream_chip(substream);
267         struct snd_pcm_runtime *runtime = substream->runtime;
268         int ret = 0;
269
270         dprintk("opening device and trying to acquire exclusive lock\n");
271
272         /* Sets volume, mute, etc */
273         dev->mute = 0;
274         ret = em28xx_audio_analog_set(dev);
275         if (ret < 0)
276                 goto err;
277
278         runtime->hw = snd_em28xx_hw_capture;
279         if (dev->alt == 0 && dev->adev->users == 0) {
280                 int errCode;
281                 dev->alt = 7;
282                 errCode = usb_set_interface(dev->udev, 0, 7);
283                 dprintk("changing alternate number to 7\n");
284         }
285
286         dev->adev->users++;
287
288         snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
289         dev->adev->capture_pcm_substream = substream;
290         runtime->private_data = dev;
291
292         return 0;
293 err:
294         printk(KERN_ERR "Error while configuring em28xx mixer\n");
295         return ret;
296 }
297
298 static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
299 {
300         struct em28xx *dev = snd_pcm_substream_chip(substream);
301         dev->adev->users--;
302
303         dprintk("closing device\n");
304
305         dev->mute = 1;
306         em28xx_audio_analog_set(dev);
307
308         if (dev->adev->users == 0 && dev->adev->shutdown == 1) {
309                 dprintk("audio users: %d\n", dev->adev->users);
310                 dprintk("disabling audio stream!\n");
311                 dev->adev->shutdown = 0;
312                 dprintk("released lock\n");
313                 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
314         }
315         return 0;
316 }
317
318 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
319                                         struct snd_pcm_hw_params *hw_params)
320 {
321         unsigned int channels, rate, format;
322         int ret;
323
324         dprintk("Setting capture parameters\n");
325
326         ret = snd_pcm_alloc_vmalloc_buffer(substream,
327                                 params_buffer_bytes(hw_params));
328         format = params_format(hw_params);
329         rate = params_rate(hw_params);
330         channels = params_channels(hw_params);
331
332         /* TODO: set up em28xx audio chip to deliver the correct audio format,
333            current default is 48000hz multiplexed => 96000hz mono
334            which shouldn't matter since analogue TV only supports mono */
335         return 0;
336 }
337
338 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
339 {
340         struct em28xx *dev = snd_pcm_substream_chip(substream);
341
342         dprintk("Stop capture, if needed\n");
343
344         if (dev->adev->capture_stream == STREAM_ON)
345                 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 0);
346
347         return 0;
348 }
349
350 static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
351 {
352         return 0;
353 }
354
355 static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
356                                       int cmd)
357 {
358         struct em28xx *dev = snd_pcm_substream_chip(substream);
359
360         dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START)?
361                                        "start": "stop");
362         switch (cmd) {
363         case SNDRV_PCM_TRIGGER_START:
364                 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, 1);
365                 return 0;
366         case SNDRV_PCM_TRIGGER_STOP:
367                 dev->adev->shutdown = 1;
368                 return 0;
369         default:
370                 return -EINVAL;
371         }
372 }
373
374 static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
375                                                     *substream)
376 {
377         struct em28xx *dev;
378
379         snd_pcm_uframes_t hwptr_done;
380         dev = snd_pcm_substream_chip(substream);
381         hwptr_done = dev->adev->hwptr_done_capture;
382
383         return hwptr_done;
384 }
385
386 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
387                                              unsigned long offset)
388 {
389         void *pageptr = subs->runtime->dma_area + offset;
390
391         return vmalloc_to_page(pageptr);
392 }
393
394 static struct snd_pcm_ops snd_em28xx_pcm_capture = {
395         .open      = snd_em28xx_capture_open,
396         .close     = snd_em28xx_pcm_close,
397         .ioctl     = snd_pcm_lib_ioctl,
398         .hw_params = snd_em28xx_hw_capture_params,
399         .hw_free   = snd_em28xx_hw_capture_free,
400         .prepare   = snd_em28xx_prepare,
401         .trigger   = snd_em28xx_capture_trigger,
402         .pointer   = snd_em28xx_capture_pointer,
403         .page      = snd_pcm_get_vmalloc_page,
404 };
405
406 static int em28xx_audio_init(struct em28xx *dev)
407 {
408         struct em28xx_audio *adev;
409         struct snd_pcm      *pcm;
410         struct snd_card     *card;
411         static int          devnr;
412         int                 ret, err;
413
414         printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
415                          "non standard usbaudio\n");
416         printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
417                          "Rechberger\n");
418
419         adev = kzalloc(sizeof(*adev), GFP_KERNEL);
420         if (!adev) {
421                 printk(KERN_ERR "em28xx-audio.c: out of memory\n");
422                 return -1;
423         }
424         card = snd_card_new(index[devnr], "Em28xx Audio", THIS_MODULE, 0);
425         if (card == NULL) {
426                 kfree(adev);
427                 return -ENOMEM;
428         }
429
430         spin_lock_init(&adev->slock);
431         ret = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
432         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
433         pcm->info_flags = 0;
434         pcm->private_data = dev;
435         strcpy(pcm->name, "Empia 28xx Capture");
436         strcpy(card->driver, "Empia Em28xx Audio");
437         strcpy(card->shortname, "Em28xx Audio");
438         strcpy(card->longname, "Empia Em28xx Audio");
439
440         err = snd_card_register(card);
441         if (err < 0) {
442                 snd_card_free(card);
443                 return -ENOMEM;
444         }
445         adev->sndcard = card;
446         adev->udev = dev->udev;
447         dev->adev = adev;
448
449         return 0;
450 }
451
452 static int em28xx_audio_fini(struct em28xx *dev)
453 {
454         if (dev == NULL)
455                 return 0;
456
457         if (dev->adev) {
458                 snd_card_free(dev->adev->sndcard);
459                 kfree(dev->adev);
460                 dev->adev = NULL;
461         }
462
463         return 0;
464 }
465
466 static struct em28xx_ops audio_ops = {
467         .id   = EM28XX_AUDIO,
468         .name = "Em28xx Audio Extension",
469         .init = em28xx_audio_init,
470         .fini = em28xx_audio_fini,
471 };
472
473 static int __init em28xx_alsa_register(void)
474 {
475         request_module("em28xx");
476         return em28xx_register_extension(&audio_ops);
477 }
478
479 static void __exit em28xx_alsa_unregister(void)
480 {
481         em28xx_unregister_extension(&audio_ops);
482 }
483
484 MODULE_LICENSE("GPL");
485 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
486 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
487 MODULE_DESCRIPTION("Em28xx Audio driver");
488
489 module_init(em28xx_alsa_register);
490 module_exit(em28xx_alsa_unregister);