[media] em28xx-alsa: add mixer support for AC97 volume controls
[pandora-kernel.git] / 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 <sound/tlv.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                                   __func__, ##arg);             \
56         } while (0)
57
58 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
59
60 static int em28xx_deinit_isoc_audio(struct em28xx *dev)
61 {
62         int i;
63
64         dprintk("Stopping isoc\n");
65         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
66                 if (!irqs_disabled())
67                         usb_kill_urb(dev->adev.urb[i]);
68                 else
69                         usb_unlink_urb(dev->adev.urb[i]);
70
71                 usb_free_urb(dev->adev.urb[i]);
72                 dev->adev.urb[i] = NULL;
73
74                 kfree(dev->adev.transfer_buffer[i]);
75                 dev->adev.transfer_buffer[i] = NULL;
76         }
77
78         return 0;
79 }
80
81 static void em28xx_audio_isocirq(struct urb *urb)
82 {
83         struct em28xx            *dev = urb->context;
84         int                      i;
85         unsigned int             oldptr;
86         int                      period_elapsed = 0;
87         int                      status;
88         unsigned char            *cp;
89         unsigned int             stride;
90         struct snd_pcm_substream *substream;
91         struct snd_pcm_runtime   *runtime;
92
93         switch (urb->status) {
94         case 0:             /* success */
95         case -ETIMEDOUT:    /* NAK */
96                 break;
97         case -ECONNRESET:   /* kill */
98         case -ENOENT:
99         case -ESHUTDOWN:
100                 return;
101         default:            /* error */
102                 dprintk("urb completition error %d.\n", urb->status);
103                 break;
104         }
105
106         if (atomic_read(&dev->stream_started) == 0)
107                 return;
108
109         if (dev->adev.capture_pcm_substream) {
110                 substream = dev->adev.capture_pcm_substream;
111                 runtime = substream->runtime;
112                 stride = runtime->frame_bits >> 3;
113
114                 for (i = 0; i < urb->number_of_packets; i++) {
115                         int length =
116                             urb->iso_frame_desc[i].actual_length / stride;
117                         cp = (unsigned char *)urb->transfer_buffer +
118                             urb->iso_frame_desc[i].offset;
119
120                         if (!length)
121                                 continue;
122
123                         oldptr = dev->adev.hwptr_done_capture;
124                         if (oldptr + length >= runtime->buffer_size) {
125                                 unsigned int cnt =
126                                     runtime->buffer_size - oldptr;
127                                 memcpy(runtime->dma_area + oldptr * stride, cp,
128                                        cnt * stride);
129                                 memcpy(runtime->dma_area, cp + cnt * stride,
130                                        length * stride - cnt * stride);
131                         } else {
132                                 memcpy(runtime->dma_area + oldptr * stride, cp,
133                                        length * stride);
134                         }
135
136                         snd_pcm_stream_lock(substream);
137
138                         dev->adev.hwptr_done_capture += length;
139                         if (dev->adev.hwptr_done_capture >=
140                             runtime->buffer_size)
141                                 dev->adev.hwptr_done_capture -=
142                                     runtime->buffer_size;
143
144                         dev->adev.capture_transfer_done += length;
145                         if (dev->adev.capture_transfer_done >=
146                             runtime->period_size) {
147                                 dev->adev.capture_transfer_done -=
148                                     runtime->period_size;
149                                 period_elapsed = 1;
150                         }
151
152                         snd_pcm_stream_unlock(substream);
153                 }
154                 if (period_elapsed)
155                         snd_pcm_period_elapsed(substream);
156         }
157         urb->status = 0;
158
159         status = usb_submit_urb(urb, GFP_ATOMIC);
160         if (status < 0) {
161                 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
162                               status);
163         }
164         return;
165 }
166
167 static int em28xx_init_audio_isoc(struct em28xx *dev)
168 {
169         int       i, errCode;
170         const int sb_size = EM28XX_NUM_AUDIO_PACKETS *
171                             EM28XX_AUDIO_MAX_PACKET_SIZE;
172
173         dprintk("Starting isoc transfers\n");
174
175         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
176                 struct urb *urb;
177                 int j, k;
178
179                 dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC);
180                 if (!dev->adev.transfer_buffer[i])
181                         return -ENOMEM;
182
183                 memset(dev->adev.transfer_buffer[i], 0x80, sb_size);
184                 urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC);
185                 if (!urb) {
186                         em28xx_errdev("usb_alloc_urb failed!\n");
187                         for (j = 0; j < i; j++) {
188                                 usb_free_urb(dev->adev.urb[j]);
189                                 kfree(dev->adev.transfer_buffer[j]);
190                         }
191                         return -ENOMEM;
192                 }
193
194                 urb->dev = dev->udev;
195                 urb->context = dev;
196                 urb->pipe = usb_rcvisocpipe(dev->udev, 0x83);
197                 urb->transfer_flags = URB_ISO_ASAP;
198                 urb->transfer_buffer = dev->adev.transfer_buffer[i];
199                 urb->interval = 1;
200                 urb->complete = em28xx_audio_isocirq;
201                 urb->number_of_packets = EM28XX_NUM_AUDIO_PACKETS;
202                 urb->transfer_buffer_length = sb_size;
203
204                 for (j = k = 0; j < EM28XX_NUM_AUDIO_PACKETS;
205                              j++, k += EM28XX_AUDIO_MAX_PACKET_SIZE) {
206                         urb->iso_frame_desc[j].offset = k;
207                         urb->iso_frame_desc[j].length =
208                             EM28XX_AUDIO_MAX_PACKET_SIZE;
209                 }
210                 dev->adev.urb[i] = urb;
211         }
212
213         for (i = 0; i < EM28XX_AUDIO_BUFS; i++) {
214                 errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC);
215                 if (errCode) {
216                         em28xx_deinit_isoc_audio(dev);
217                         return errCode;
218                 }
219         }
220
221         return 0;
222 }
223
224 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs,
225                                         size_t size)
226 {
227         struct snd_pcm_runtime *runtime = subs->runtime;
228
229         dprintk("Allocating vbuffer\n");
230         if (runtime->dma_area) {
231                 if (runtime->dma_bytes > size)
232                         return 0;
233
234                 vfree(runtime->dma_area);
235         }
236         runtime->dma_area = vmalloc(size);
237         if (!runtime->dma_area)
238                 return -ENOMEM;
239
240         runtime->dma_bytes = size;
241
242         return 0;
243 }
244
245 static struct snd_pcm_hardware snd_em28xx_hw_capture = {
246         .info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
247                 SNDRV_PCM_INFO_MMAP           |
248                 SNDRV_PCM_INFO_INTERLEAVED    |
249                 SNDRV_PCM_INFO_MMAP_VALID,
250
251         .formats = SNDRV_PCM_FMTBIT_S16_LE,
252
253         .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT,
254
255         .rate_min = 48000,
256         .rate_max = 48000,
257         .channels_min = 2,
258         .channels_max = 2,
259         .buffer_bytes_max = 62720 * 8,  /* just about the value in usbaudio.c */
260         .period_bytes_min = 64,         /* 12544/2, */
261         .period_bytes_max = 12544,
262         .periods_min = 2,
263         .periods_max = 98,              /* 12544, */
264 };
265
266 static int snd_em28xx_capture_open(struct snd_pcm_substream *substream)
267 {
268         struct em28xx *dev = snd_pcm_substream_chip(substream);
269         struct snd_pcm_runtime *runtime = substream->runtime;
270         int ret = 0;
271
272         dprintk("opening device and trying to acquire exclusive lock\n");
273
274         if (!dev) {
275                 em28xx_err("BUG: em28xx can't find device struct."
276                                 " Can't proceed with open\n");
277                 return -ENODEV;
278         }
279
280         /* Sets volume, mute, etc */
281
282         dev->mute = 0;
283         mutex_lock(&dev->lock);
284         ret = em28xx_audio_analog_set(dev);
285         if (ret < 0)
286                 goto err;
287
288         runtime->hw = snd_em28xx_hw_capture;
289         if (dev->alt == 0 && dev->adev.users == 0) {
290                 dev->alt = 7;
291                 dprintk("changing alternate number to 7\n");
292                 usb_set_interface(dev->udev, 0, 7);
293         }
294
295         dev->adev.users++;
296         mutex_unlock(&dev->lock);
297
298         snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
299         dev->adev.capture_pcm_substream = substream;
300         runtime->private_data = dev;
301
302         return 0;
303 err:
304         mutex_unlock(&dev->lock);
305
306         em28xx_err("Error while configuring em28xx mixer\n");
307         return ret;
308 }
309
310 static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream)
311 {
312         struct em28xx *dev = snd_pcm_substream_chip(substream);
313
314         dprintk("closing device\n");
315
316         dev->mute = 1;
317         mutex_lock(&dev->lock);
318         dev->adev.users--;
319         if (atomic_read(&dev->stream_started) > 0) {
320                 atomic_set(&dev->stream_started, 0);
321                 schedule_work(&dev->wq_trigger);
322         }
323
324         em28xx_audio_analog_set(dev);
325         if (substream->runtime->dma_area) {
326                 dprintk("freeing\n");
327                 vfree(substream->runtime->dma_area);
328                 substream->runtime->dma_area = NULL;
329         }
330         mutex_unlock(&dev->lock);
331
332         return 0;
333 }
334
335 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream,
336                                         struct snd_pcm_hw_params *hw_params)
337 {
338         unsigned int channels, rate, format;
339         int ret;
340
341         dprintk("Setting capture parameters\n");
342
343         ret = snd_pcm_alloc_vmalloc_buffer(substream,
344                                 params_buffer_bytes(hw_params));
345         if (ret < 0)
346                 return ret;
347         format = params_format(hw_params);
348         rate = params_rate(hw_params);
349         channels = params_channels(hw_params);
350
351         /* TODO: set up em28xx audio chip to deliver the correct audio format,
352            current default is 48000hz multiplexed => 96000hz mono
353            which shouldn't matter since analogue TV only supports mono */
354         return 0;
355 }
356
357 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream)
358 {
359         struct em28xx *dev = snd_pcm_substream_chip(substream);
360
361         dprintk("Stop capture, if needed\n");
362
363         if (atomic_read(&dev->stream_started) > 0) {
364                 atomic_set(&dev->stream_started, 0);
365                 schedule_work(&dev->wq_trigger);
366         }
367
368         return 0;
369 }
370
371 static int snd_em28xx_prepare(struct snd_pcm_substream *substream)
372 {
373         struct em28xx *dev = snd_pcm_substream_chip(substream);
374
375         dev->adev.hwptr_done_capture = 0;
376         dev->adev.capture_transfer_done = 0;
377
378         return 0;
379 }
380
381 static void audio_trigger(struct work_struct *work)
382 {
383         struct em28xx *dev = container_of(work, struct em28xx, wq_trigger);
384
385         if (atomic_read(&dev->stream_started)) {
386                 dprintk("starting capture");
387                 em28xx_init_audio_isoc(dev);
388         } else {
389                 dprintk("stopping capture");
390                 em28xx_deinit_isoc_audio(dev);
391         }
392 }
393
394 static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream,
395                                       int cmd)
396 {
397         struct em28xx *dev = snd_pcm_substream_chip(substream);
398         int retval = 0;
399
400         switch (cmd) {
401         case SNDRV_PCM_TRIGGER_START:
402                 atomic_set(&dev->stream_started, 1);
403                 break;
404         case SNDRV_PCM_TRIGGER_STOP:
405                 atomic_set(&dev->stream_started, 1);
406                 break;
407         default:
408                 retval = -EINVAL;
409         }
410         schedule_work(&dev->wq_trigger);
411         return retval;
412 }
413
414 static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream
415                                                     *substream)
416 {
417         unsigned long flags;
418         struct em28xx *dev;
419         snd_pcm_uframes_t hwptr_done;
420
421         dev = snd_pcm_substream_chip(substream);
422         spin_lock_irqsave(&dev->adev.slock, flags);
423         hwptr_done = dev->adev.hwptr_done_capture;
424         spin_unlock_irqrestore(&dev->adev.slock, flags);
425
426         return hwptr_done;
427 }
428
429 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
430                                              unsigned long offset)
431 {
432         void *pageptr = subs->runtime->dma_area + offset;
433
434         return vmalloc_to_page(pageptr);
435 }
436
437 /*
438  * AC97 volume control support
439  */
440 static int em28xx_vol_info(struct snd_kcontrol *kcontrol,
441                                 struct snd_ctl_elem_info *info)
442 {
443         info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
444         info->count = 2;
445         info->value.integer.min = 0;
446         info->value.integer.max = 0x1f;
447
448         return 0;
449 }
450
451 /* FIXME: should also add mute controls for each */
452
453 static int em28xx_vol_put(struct snd_kcontrol *kcontrol,
454                                struct snd_ctl_elem_value *value)
455 {
456         struct em28xx *dev = snd_kcontrol_chip(kcontrol);
457         u16 val = (value->value.integer.value[0] & 0x1f) |
458                   (value->value.integer.value[1] & 0x1f) << 8;
459         int rc;
460
461         mutex_lock(&dev->lock);
462         rc = em28xx_read_ac97(dev, kcontrol->private_value);
463         if (rc < 0)
464                 goto err;
465
466         val |= rc & 0x8080;     /* Preserve the mute flags */
467
468         rc = em28xx_write_ac97(dev, kcontrol->private_value, val);
469
470 err:
471         mutex_unlock(&dev->lock);
472         return rc;
473 }
474
475 static int em28xx_vol_get(struct snd_kcontrol *kcontrol,
476                                struct snd_ctl_elem_value *value)
477 {
478         struct em28xx *dev = snd_kcontrol_chip(kcontrol);
479         int val;
480
481         mutex_lock(&dev->lock);
482         val = em28xx_read_ac97(dev, kcontrol->private_value);
483         mutex_unlock(&dev->lock);
484         if (val < 0)
485                 return val;
486
487         value->value.integer.value[0] = val & 0x1f;
488         value->value.integer.value[1] = (val << 8) & 0x1f;
489
490         return 0;
491 }
492
493 static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0);
494
495 static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev,
496                            char *name, int id)
497 {
498         int err;
499         struct snd_kcontrol *kctl;
500         struct snd_kcontrol_new tmp = {
501                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
502                 .name  = name,
503                 .info  = em28xx_vol_info,
504                 .get   = em28xx_vol_get,
505                 .put   = em28xx_vol_put,
506                 .private_value = id,
507                 .tlv.p = em28xx_db_scale,
508         };
509
510         kctl = snd_ctl_new1(&tmp, dev);
511
512         err = snd_ctl_add(card, kctl);
513         if (err < 0)
514                 return err;
515
516         return 0;
517 }
518
519
520 /*
521  * register/unregister code and data
522  */
523 static struct snd_pcm_ops snd_em28xx_pcm_capture = {
524         .open      = snd_em28xx_capture_open,
525         .close     = snd_em28xx_pcm_close,
526         .ioctl     = snd_pcm_lib_ioctl,
527         .hw_params = snd_em28xx_hw_capture_params,
528         .hw_free   = snd_em28xx_hw_capture_free,
529         .prepare   = snd_em28xx_prepare,
530         .trigger   = snd_em28xx_capture_trigger,
531         .pointer   = snd_em28xx_capture_pointer,
532         .page      = snd_pcm_get_vmalloc_page,
533 };
534
535 static int em28xx_audio_init(struct em28xx *dev)
536 {
537         struct em28xx_audio *adev = &dev->adev;
538         struct snd_pcm      *pcm;
539         struct snd_card     *card;
540         static int          devnr;
541         int                 err;
542
543         if (dev->has_alsa_audio != 1) {
544                 /* This device does not support the extension (in this case
545                    the device is expecting the snd-usb-audio module or
546                    doesn't have analog audio support at all) */
547                 return 0;
548         }
549
550         printk(KERN_INFO "em28xx-audio.c: probing for em28x1 "
551                          "non standard usbaudio\n");
552         printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus "
553                          "Rechberger\n");
554
555         err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0,
556                               &card);
557         if (err < 0)
558                 return err;
559
560         spin_lock_init(&adev->slock);
561         err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm);
562         if (err < 0) {
563                 snd_card_free(card);
564                 return err;
565         }
566
567         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture);
568         pcm->info_flags = 0;
569         pcm->private_data = dev;
570         strcpy(pcm->name, "Empia 28xx Capture");
571
572         snd_card_set_dev(card, &dev->udev->dev);
573         strcpy(card->driver, "Em28xx-Audio");
574         strcpy(card->shortname, "Em28xx Audio");
575         strcpy(card->longname, "Empia Em28xx Audio");
576
577         INIT_WORK(&dev->wq_trigger, audio_trigger);
578
579         if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
580                 em28xx_cvol_new(card, dev, "Video", AC97_VIDEO_VOL);
581                 em28xx_cvol_new(card, dev, "Line In", AC97_LINEIN_VOL);
582                 em28xx_cvol_new(card, dev, "Phone", AC97_PHONE_VOL);
583                 em28xx_cvol_new(card, dev, "Microphone", AC97_PHONE_VOL);
584                 em28xx_cvol_new(card, dev, "CD", AC97_CD_VOL);
585                 em28xx_cvol_new(card, dev, "AUX", AC97_AUX_VOL);
586                 em28xx_cvol_new(card, dev, "PCM", AC97_PCM_OUT_VOL);
587
588                 em28xx_cvol_new(card, dev, "Master", AC97_MASTER_VOL);
589                 em28xx_cvol_new(card, dev, "Line", AC97_LINE_LEVEL_VOL);
590                 em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO_VOL);
591                 em28xx_cvol_new(card, dev, "LFE", AC97_LFE_MASTER_VOL);
592                 em28xx_cvol_new(card, dev, "Surround", AC97_SURR_MASTER_VOL);
593         }
594
595         err = snd_card_register(card);
596         if (err < 0) {
597                 snd_card_free(card);
598                 return err;
599         }
600         adev->sndcard = card;
601         adev->udev = dev->udev;
602
603         return 0;
604 }
605
606 static int em28xx_audio_fini(struct em28xx *dev)
607 {
608         if (dev == NULL)
609                 return 0;
610
611         if (dev->has_alsa_audio != 1) {
612                 /* This device does not support the extension (in this case
613                    the device is expecting the snd-usb-audio module or
614                    doesn't have analog audio support at all) */
615                 return 0;
616         }
617
618         if (dev->adev.sndcard) {
619                 snd_card_free(dev->adev.sndcard);
620                 dev->adev.sndcard = NULL;
621         }
622
623         return 0;
624 }
625
626 static struct em28xx_ops audio_ops = {
627         .id   = EM28XX_AUDIO,
628         .name = "Em28xx Audio Extension",
629         .init = em28xx_audio_init,
630         .fini = em28xx_audio_fini,
631 };
632
633 static int __init em28xx_alsa_register(void)
634 {
635         return em28xx_register_extension(&audio_ops);
636 }
637
638 static void __exit em28xx_alsa_unregister(void)
639 {
640         em28xx_unregister_extension(&audio_ops);
641 }
642
643 MODULE_LICENSE("GPL");
644 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
645 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
646 MODULE_DESCRIPTION("Em28xx Audio driver");
647
648 module_init(em28xx_alsa_register);
649 module_exit(em28xx_alsa_unregister);