Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/djbw/async_tx
[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/core.h>
39 #include <sound/pcm.h>
40 #include <sound/pcm_params.h>
41 #include <sound/info.h>
42 #include <sound/initval.h>
43 #include <sound/control.h>
44 #include <media/v4l2-common.h>
45 #include "em28xx.h"
46
47 static int debug;
48 module_param(debug, int, 0644);
49 MODULE_PARM_DESC(debug, "activates debug info");
50
51 #define dprintk(fmt, arg...) do {                                       \
52             if (debug)                                                  \
53                 printk(KERN_INFO "em28xx-audio %s: " fmt,               \
54                                   __func__, ##arg);             \
55         } while (0)
56
57 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
58
59 static int em28xx_deinit_isoc_audio(struct em28xx *dev)
60 {
61         int i;
62
63         dprintk("Stopping isoc\n");
64         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
65                 if (!irqs_disabled())
66                         usb_kill_urb(dev->adev.urb[i]);
67                 else
68                         usb_unlink_urb(dev->adev.urb[i]);
69
70                 usb_free_urb(dev->adev.urb[i]);
71                 dev->adev.urb[i] = NULL;
72
73                 kfree(dev->adev.transfer_buffer[i]);
74                 dev->adev.transfer_buffer[i] = NULL;
75         }
76
77         return 0;
78 }
79
80 static void em28xx_audio_isocirq(struct urb *urb)
81 {
82         struct em28xx            *dev = urb->context;
83         int                      i;
84         unsigned int             oldptr;
85         int                      period_elapsed = 0;
86         int                      status;
87         unsigned char            *cp;
88         unsigned int             stride;
89         struct snd_pcm_substream *substream;
90         struct snd_pcm_runtime   *runtime;
91
92         switch (urb->status) {
93         case 0:             /* success */
94         case -ETIMEDOUT:    /* NAK */
95                 break;
96         case -ECONNRESET:   /* kill */
97         case -ENOENT:
98         case -ESHUTDOWN:
99                 return;
100         default:            /* error */
101                 dprintk("urb completition error %d.\n", urb->status);
102                 break;
103         }
104
105         if (dev->adev.capture_pcm_substream) {
106                 substream = dev->adev.capture_pcm_substream;
107                 runtime = substream->runtime;
108                 stride = runtime->frame_bits >> 3;
109
110                 for (i = 0; i < urb->number_of_packets; i++) {
111                         int length =
112                             urb->iso_frame_desc[i].actual_length / stride;
113                         cp = (unsigned char *)urb->transfer_buffer +
114                             urb->iso_frame_desc[i].offset;
115
116                         if (!length)
117                                 continue;
118
119                         oldptr = dev->adev.hwptr_done_capture;
120                         if (oldptr + length >= runtime->buffer_size) {
121                                 unsigned int cnt =
122                                     runtime->buffer_size - oldptr;
123                                 memcpy(runtime->dma_area + oldptr * stride, cp,
124                                        cnt * stride);
125                                 memcpy(runtime->dma_area, cp + cnt * stride,
126                                        length * stride - cnt * stride);
127                         } else {
128                                 memcpy(runtime->dma_area + oldptr * stride, cp,
129                                        length * stride);
130                         }
131
132                         snd_pcm_stream_lock(substream);
133
134                         dev->adev.hwptr_done_capture += length;
135                         if (dev->adev.hwptr_done_capture >=
136                             runtime->buffer_size)
137                                 dev->adev.hwptr_done_capture -=
138                                     runtime->buffer_size;
139
140                         dev->adev.capture_transfer_done += length;
141                         if (dev->adev.capture_transfer_done >=
142                             runtime->period_size) {
143                                 dev->adev.capture_transfer_done -=
144                                     runtime->period_size;
145                                 period_elapsed = 1;
146                         }
147
148                         snd_pcm_stream_unlock(substream);
149                 }
150                 if (period_elapsed)
151                         snd_pcm_period_elapsed(substream);
152         }
153         urb->status = 0;
154
155         status = usb_submit_urb(urb, GFP_ATOMIC);
156         if (status < 0) {
157                 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
158                               status);
159         }
160         return;
161 }
162
163 static int em28xx_init_audio_isoc(struct em28xx *dev)
164 {
165         int       i, errCode;
166         const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
167                             EM28XX_AUDIO_MAX_PACKET_SIZE;
168
169         dprintk("Starting isoc transfers\n");
170
171         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
172                 struct urb *urb;
173                 int j, k;
174
175                 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
176                 if (!dev->adev.transfer_buffer[i])
177                         return -ENOMEM;
178
179                 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
180                 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
181                 if (!urb) {
182                         em28xx_errdev("usb_alloc_urb failed!\n");
183                         for (j = 0; j < i; j++) {
184                                 usb_free_urb(dev->adev.urb[j]);
185                                 kfree(dev->adev.transfer_buffer[j]);
186                         }
187                         return -ENOMEM;
188                 }
189
190                 urb->dev = dev->udev;
191                 urb->context = dev;
192                 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
193                 urb->transfer_flags = URB_ISO_ASAP;
194                 urb->transfer_buffer = dev->adev.transfer_buffer[i];
195                 urb->interval = 1;
196                 urb->complete = em28xx_audio_isocirq;
197                 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
198                 urb->transfer_buffer_length = sb_size;
199
200                 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
201                              j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
202                         urb->iso_frame_desc[j].offset = k;
203                         urb->iso_frame_desc[j].length =
204                             EM28XX_AUDIO_MAX_PACKET_SIZE;
205                 }
206                 dev->adev.urb[i] = urb;
207         }
208
209         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
210                 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
211                 if (errCode) {
212                         em28xx_deinit_isoc_audio(dev);
213                         return errCode;
214                 }
215         }
216
217         return 0;
218 }
219
220 static int em28xx_cmd(struct em28xx *dev, int cmd, int arg)
221 {
222         dprintk("%s transfer\n", (dev->adev.capture_stream == STREAM_ON) ?
223                                  "stop" : "start");
224
225         switch (cmd) {
226         case EM28XX_CAPTURE_STREAM_EN:
227                 if (dev->adev.capture_stream == STREAM_OFF &&
228                     arg == EM28XX_START_AUDIO) {
229                         dev->adev.capture_stream = STREAM_ON;
230                         em28xx_init_audio_isoc(dev);
231                 } else if (dev->adev.capture_stream == STREAM_ON &&
232                            arg == EM28XX_STOP_AUDIO) {
233                         dev->adev.capture_stream = STREAM_OFF;
234                         em28xx_deinit_isoc_audio(dev);
235                 } else {
236                         em28xx_errdev("An underrun very likely occurred. "
237                                         "Ignoring it.\n");
238                 }
239                 return 0;
240         default:
241                 return -EINVAL;
242         }
243 }
244
245 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
246                                         size_t size)
247 {
248         struct snd_pcm_runtime *runtime = subs->runtime;
249
250         dprintk("Allocating vbuffer\n");
251         if (runtime->dma_area) {
252                 if (runtime->dma_bytes > size)
253                         return 0;
254
255                 vfree(runtime->dma_area);
256         }
257         runtime->dma_area = vmalloc(size);
258         if (!runtime->dma_area)
259                 return -ENOMEM;
260
261         runtime->dma_bytes = size;
262
263         return 0;
264 }
265
266 static struct snd_pcm_hardware snd_em28xx_hw_capture = {
267         .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
268                 SNDRV_PCM_INFO_MMAP           |
269                 SNDRV_PCM_INFO_INTERLEAVED    |
270                 SNDRV_PCM_INFO_MMAP_VALID,
271
272         .formats = SNDRV_PCM_FMTBIT_S16_LE,
273
274         .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
275
276         .rate_min = 48000,
277         .rate_max = 48000,
278         .channels_min = 2,
279         .channels_max = 2,
280         .buffer_bytes_max = 62720 * 8,  /* just about the value in usbaudio.c */
281         .period_bytes_min = 64,         /* 12544/2, */
282         .period_bytes_max = 12544,
283         .periods_min = 2,
284         .periods_max = 98,              /* 12544, */
285 };
286
287 static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
288 {
289         struct em28xx *dev = snd_pcm_substream_chip(substream);
290         struct snd_pcm_runtime *runtime = substream->runtime;
291         int ret = 0;
292
293         dprintk("opening device and trying to acquire exclusive lock\n");
294
295         if (!dev) {
296                 printk(KERN_ERR "BUG: em28xx can't find device struct."
297                                 " Can't proceed with open\n");
298                 return -ENODEV;
299         }
300
301         /* Sets volume, mute, etc */
302
303         dev->mute = 0;
304         mutex_lock(&dev->lock);
305         ret = em28xx_audio_analog_set(dev);
306         mutex_unlock(&dev->lock);
307         if (ret < 0)
308                 goto err;
309
310         runtime->hw = snd_em28xx_hw_capture;
311         if (dev->alt == 0 && dev->adev.users == 0) {
312                 int errCode;
313                 dev->alt = 7;
314                 errCode = usb_set_interface(dev->udev, 0, 7);
315                 dprintk("changing alternate number to 7\n");
316         }
317
318         mutex_lock(&dev->lock);
319         dev->adev.users++;
320         mutex_unlock(&dev->lock);
321
322         snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
323         dev->adev.capture_pcm_substream = substream;
324         runtime->private_data = dev;
325
326         return 0;
327 err:
328         printk(KERN_ERR "Error while configuring em28xx mixer\n");
329         return ret;
330 }
331
332 static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
333 {
334         struct em28xx *dev = snd_pcm_substream_chip(substream);
335
336         dprintk("closing device\n");
337
338         dev->mute = 1;
339         mutex_lock(&dev->lock);
340         dev->adev.users--;
341         em28xx_audio_analog_set(dev);
342         mutex_unlock(&dev->lock);
343
344         return 0;
345 }
346
347 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
348                                         struct snd_pcm_hw_params *hw_params)
349 {
350         unsigned int channels, rate, format;
351         int ret;
352
353         dprintk("Setting capture parameters\n");
354
355         ret = snd_pcm_alloc_vmalloc_buffer(substream,
356                                 params_buffer_bytes(hw_params));
357         format = params_format(hw_params);
358         rate = params_rate(hw_params);
359         channels = params_channels(hw_params);
360
361         /* TODO: set up em28xx audio chip to deliver the correct audio format,
362            current default is 48000hz multiplexed => 96000hz mono
363            which shouldn't matter since analogue TV only supports mono */
364         return 0;
365 }
366
367 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
368 {
369         struct em28xx *dev = snd_pcm_substream_chip(substream);
370
371         dprintk("Stop capture, if needed\n");
372
373         if (dev->adev.capture_stream == STREAM_ON)
374                 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_STOP_AUDIO);
375
376         return 0;
377 }
378
379 static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
380 {
381         return 0;
382 }
383
384 static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
385                                       int cmd)
386 {
387         struct em28xx *dev = snd_pcm_substream_chip(substream);
388         int retval;
389
390         dprintk("Should %s capture\n", (cmd == SNDRV_PCM_TRIGGER_START) ?
391                                        "start" : "stop");
392
393         spin_lock(&dev->adev.slock);
394         switch (cmd) {
395         case SNDRV_PCM_TRIGGER_START:
396                 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_START_AUDIO);
397                 retval = 0;
398                 break;
399         case SNDRV_PCM_TRIGGER_STOP:
400                 em28xx_cmd(dev, EM28XX_CAPTURE_STREAM_EN, EM28XX_STOP_AUDIO);
401                 retval = 0;
402                 break;
403         default:
404                 retval = -EINVAL;
405         }
406
407         spin_unlock(&dev->adev.slock);
408         return retval;
409 }
410
411 static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
412                                                     *substream)
413 {
414         unsigned long flags;
415         struct em28xx *dev;
416         snd_pcm_uframes_t hwptr_done;
417
418         dev = snd_pcm_substream_chip(substream);
419         spin_lock_irqsave(&dev->adev.slock, flags);
420         hwptr_done = dev->adev.hwptr_done_capture;
421         spin_unlock_irqrestore(&dev->adev.slock, flags);
422
423         return hwptr_done;
424 }
425
426 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
427                                              unsigned long offset)
428 {
429         void *pageptr = subs->runtime->dma_area + offset;
430
431         return vmalloc_to_page(pageptr);
432 }
433
434 static struct snd_pcm_ops snd_em28xx_pcm_capture = {
435         .open      = snd_em28xx_capture_open,
436         .close     = snd_em28xx_pcm_close,
437         .ioctl     = snd_pcm_lib_ioctl,
438         .hw_params = snd_em28xx_hw_capture_params,
439         .hw_free   = snd_em28xx_hw_capture_free,
440         .prepare   = snd_em28xx_prepare,
441         .trigger   = snd_em28xx_capture_trigger,
442         .pointer   = snd_em28xx_capture_pointer,
443         .page      = snd_pcm_get_vmalloc_page,
444 };
445
446 static int em28xx_audio_init(struct em28xx *dev)
447 {
448         struct em28xx_audio *adev = &dev->adev;
449         struct snd_pcm      *pcm;
450         struct snd_card     *card;
451         static int          devnr;
452         int                 err;
453
454         if (dev->has_alsa_audio != 1) {
455                 /* This device does not support the extension (in this case
456                    the device is expecting the snd-usb-audio module or
457                    doesn't have analog audio support at all) */
458                 return 0;
459         }
460
461         printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
462                          "non standard usbaudio\n");
463         printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
464                          "Rechberger\n");
465
466         err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
467                               &card);
468         if (err < 0)
469                 return err;
470
471         spin_lock_init(&adev->slock);
472         err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
473         if (err < 0) {
474                 snd_card_free(card);
475                 return err;
476         }
477
478         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
479         pcm->info_flags = 0;
480         pcm->private_data = dev;
481         strcpy(pcm->name, "Empia 28xx Capture");
482
483         snd_card_set_dev(card, &dev->udev->dev);
484         strcpy(card->driver, "Empia Em28xx Audio");
485         strcpy(card->shortname, "Em28xx Audio");
486         strcpy(card->longname, "Empia Em28xx Audio");
487
488         err = snd_card_register(card);
489         if (err < 0) {
490                 snd_card_free(card);
491                 return err;
492         }
493         adev->sndcard = card;
494         adev->udev = dev->udev;
495
496         return 0;
497 }
498
499 static int em28xx_audio_fini(struct em28xx *dev)
500 {
501         if (dev == NULL)
502                 return 0;
503
504         if (dev->has_alsa_audio != 1) {
505                 /* This device does not support the extension (in this case
506                    the device is expecting the snd-usb-audio module or
507                    doesn't have analog audio support at all) */
508                 return 0;
509         }
510
511         if (dev->adev.sndcard) {
512                 snd_card_free(dev->adev.sndcard);
513                 dev->adev.sndcard = NULL;
514         }
515
516         return 0;
517 }
518
519 static struct em28xx_ops audio_ops = {
520         .id   = EM28XX_AUDIO,
521         .name = "Em28xx Audio Extension",
522         .init = em28xx_audio_init,
523         .fini = em28xx_audio_fini,
524 };
525
526 static int __init em28xx_alsa_register(void)
527 {
528         return em28xx_register_extension(&audio_ops);
529 }
530
531 static void __exit em28xx_alsa_unregister(void)
532 {
533         em28xx_unregister_extension(&audio_ops);
534 }
535
536 MODULE_LICENSE("GPL");
537 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
538 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
539 MODULE_DESCRIPTION("Em28xx Audio driver");
540
541 module_init(em28xx_alsa_register);
542 module_exit(em28xx_alsa_unregister);