2 * Empiatech em28x1 audio extension
4 * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com>
6 * Copyright (C) 2007 Mauro Carvalho Chehab <mchehab@infradead.org>
7 * - Port to work with the in-kernel driver
10 * This driver is based on my previous au600 usb pstn audio driver
11 * and inherits all the copyrights
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.
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.
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.
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>
49 module_param(debug
, int, 0644);
50 MODULE_PARM_DESC(debug
, "activates debug info");
52 #define dprintk(fmt, arg...) do { \
54 printk(KERN_INFO "em28xx-audio %s: " fmt, \
58 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
60 static int em28xx_deinit_isoc_audio(struct em28xx
*dev
)
64 dprintk("Stopping isoc\n");
65 for (i
= 0; i
< EM28XX_AUDIO_BUFS
; i
++) {
67 usb_kill_urb(dev
->adev
.urb
[i
]);
69 usb_unlink_urb(dev
->adev
.urb
[i
]);
71 usb_free_urb(dev
->adev
.urb
[i
]);
72 dev
->adev
.urb
[i
] = NULL
;
74 kfree(dev
->adev
.transfer_buffer
[i
]);
75 dev
->adev
.transfer_buffer
[i
] = NULL
;
81 static void em28xx_audio_isocirq(struct urb
*urb
)
83 struct em28xx
*dev
= urb
->context
;
86 int period_elapsed
= 0;
90 struct snd_pcm_substream
*substream
;
91 struct snd_pcm_runtime
*runtime
;
93 switch (urb
->status
) {
95 case -ETIMEDOUT
: /* NAK */
97 case -ECONNRESET
: /* kill */
102 dprintk("urb completition error %d.\n", urb
->status
);
106 if (atomic_read(&dev
->stream_started
) == 0)
109 if (dev
->adev
.capture_pcm_substream
) {
110 substream
= dev
->adev
.capture_pcm_substream
;
111 runtime
= substream
->runtime
;
112 stride
= runtime
->frame_bits
>> 3;
114 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
116 urb
->iso_frame_desc
[i
].actual_length
/ stride
;
117 cp
= (unsigned char *)urb
->transfer_buffer
+
118 urb
->iso_frame_desc
[i
].offset
;
123 oldptr
= dev
->adev
.hwptr_done_capture
;
124 if (oldptr
+ length
>= runtime
->buffer_size
) {
126 runtime
->buffer_size
- oldptr
;
127 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
129 memcpy(runtime
->dma_area
, cp
+ cnt
* stride
,
130 length
* stride
- cnt
* stride
);
132 memcpy(runtime
->dma_area
+ oldptr
* stride
, cp
,
136 snd_pcm_stream_lock(substream
);
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
;
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
;
152 snd_pcm_stream_unlock(substream
);
155 snd_pcm_period_elapsed(substream
);
159 status
= usb_submit_urb(urb
, GFP_ATOMIC
);
161 em28xx_errdev("resubmit of audio urb failed (error=%i)\n",
167 static int em28xx_init_audio_isoc(struct em28xx
*dev
)
170 const int sb_size
= EM28XX_NUM_AUDIO_PACKETS
*
171 EM28XX_AUDIO_MAX_PACKET_SIZE
;
173 dprintk("Starting isoc transfers\n");
175 for (i
= 0; i
< EM28XX_AUDIO_BUFS
; i
++) {
179 dev
->adev
.transfer_buffer
[i
] = kmalloc(sb_size
, GFP_ATOMIC
);
180 if (!dev
->adev
.transfer_buffer
[i
])
183 memset(dev
->adev
.transfer_buffer
[i
], 0x80, sb_size
);
184 urb
= usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS
, GFP_ATOMIC
);
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
]);
194 urb
->dev
= dev
->udev
;
196 urb
->pipe
= usb_rcvisocpipe(dev
->udev
, 0x83);
197 urb
->transfer_flags
= URB_ISO_ASAP
;
198 urb
->transfer_buffer
= dev
->adev
.transfer_buffer
[i
];
200 urb
->complete
= em28xx_audio_isocirq
;
201 urb
->number_of_packets
= EM28XX_NUM_AUDIO_PACKETS
;
202 urb
->transfer_buffer_length
= sb_size
;
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
;
210 dev
->adev
.urb
[i
] = urb
;
213 for (i
= 0; i
< EM28XX_AUDIO_BUFS
; i
++) {
214 errCode
= usb_submit_urb(dev
->adev
.urb
[i
], GFP_ATOMIC
);
216 em28xx_errdev("submit of audio urb failed\n");
217 em28xx_deinit_isoc_audio(dev
);
218 atomic_set(&dev
->stream_started
, 0);
227 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream
*subs
,
230 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
232 dprintk("Allocating vbuffer\n");
233 if (runtime
->dma_area
) {
234 if (runtime
->dma_bytes
> size
)
237 vfree(runtime
->dma_area
);
239 runtime
->dma_area
= vmalloc(size
);
240 if (!runtime
->dma_area
)
243 runtime
->dma_bytes
= size
;
248 static struct snd_pcm_hardware snd_em28xx_hw_capture
= {
249 .info
= SNDRV_PCM_INFO_BLOCK_TRANSFER
|
250 SNDRV_PCM_INFO_MMAP
|
251 SNDRV_PCM_INFO_INTERLEAVED
|
252 SNDRV_PCM_INFO_MMAP_VALID
,
254 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
256 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_KNOT
,
262 .buffer_bytes_max
= 62720 * 8, /* just about the value in usbaudio.c */
263 .period_bytes_min
= 64, /* 12544/2, */
264 .period_bytes_max
= 12544,
266 .periods_max
= 98, /* 12544, */
269 static int snd_em28xx_capture_open(struct snd_pcm_substream
*substream
)
271 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
272 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
275 dprintk("opening device and trying to acquire exclusive lock\n");
278 em28xx_err("BUG: em28xx can't find device struct."
279 " Can't proceed with open\n");
283 /* Sets volume, mute, etc */
286 mutex_lock(&dev
->lock
);
287 ret
= em28xx_audio_analog_set(dev
);
291 runtime
->hw
= snd_em28xx_hw_capture
;
292 if (dev
->alt
== 0 && dev
->adev
.users
== 0) {
294 dprintk("changing alternate number to 7\n");
295 usb_set_interface(dev
->udev
, 0, 7);
299 mutex_unlock(&dev
->lock
);
301 snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
);
302 dev
->adev
.capture_pcm_substream
= substream
;
303 runtime
->private_data
= dev
;
307 mutex_unlock(&dev
->lock
);
309 em28xx_err("Error while configuring em28xx mixer\n");
313 static int snd_em28xx_pcm_close(struct snd_pcm_substream
*substream
)
315 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
317 dprintk("closing device\n");
320 mutex_lock(&dev
->lock
);
322 if (atomic_read(&dev
->stream_started
) > 0) {
323 atomic_set(&dev
->stream_started
, 0);
324 schedule_work(&dev
->wq_trigger
);
327 em28xx_audio_analog_set(dev
);
328 if (substream
->runtime
->dma_area
) {
329 dprintk("freeing\n");
330 vfree(substream
->runtime
->dma_area
);
331 substream
->runtime
->dma_area
= NULL
;
333 mutex_unlock(&dev
->lock
);
338 static int snd_em28xx_hw_capture_params(struct snd_pcm_substream
*substream
,
339 struct snd_pcm_hw_params
*hw_params
)
341 unsigned int channels
, rate
, format
;
344 dprintk("Setting capture parameters\n");
346 ret
= snd_pcm_alloc_vmalloc_buffer(substream
,
347 params_buffer_bytes(hw_params
));
350 format
= params_format(hw_params
);
351 rate
= params_rate(hw_params
);
352 channels
= params_channels(hw_params
);
354 /* TODO: set up em28xx audio chip to deliver the correct audio format,
355 current default is 48000hz multiplexed => 96000hz mono
356 which shouldn't matter since analogue TV only supports mono */
360 static int snd_em28xx_hw_capture_free(struct snd_pcm_substream
*substream
)
362 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
364 dprintk("Stop capture, if needed\n");
366 if (atomic_read(&dev
->stream_started
) > 0) {
367 atomic_set(&dev
->stream_started
, 0);
368 schedule_work(&dev
->wq_trigger
);
374 static int snd_em28xx_prepare(struct snd_pcm_substream
*substream
)
376 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
378 dev
->adev
.hwptr_done_capture
= 0;
379 dev
->adev
.capture_transfer_done
= 0;
384 static void audio_trigger(struct work_struct
*work
)
386 struct em28xx
*dev
= container_of(work
, struct em28xx
, wq_trigger
);
388 if (atomic_read(&dev
->stream_started
)) {
389 dprintk("starting capture");
390 em28xx_init_audio_isoc(dev
);
392 dprintk("stopping capture");
393 em28xx_deinit_isoc_audio(dev
);
397 static int snd_em28xx_capture_trigger(struct snd_pcm_substream
*substream
,
400 struct em28xx
*dev
= snd_pcm_substream_chip(substream
);
404 case SNDRV_PCM_TRIGGER_START
:
405 atomic_set(&dev
->stream_started
, 1);
407 case SNDRV_PCM_TRIGGER_STOP
:
408 atomic_set(&dev
->stream_started
, 1);
413 schedule_work(&dev
->wq_trigger
);
417 static snd_pcm_uframes_t
snd_em28xx_capture_pointer(struct snd_pcm_substream
422 snd_pcm_uframes_t hwptr_done
;
424 dev
= snd_pcm_substream_chip(substream
);
425 spin_lock_irqsave(&dev
->adev
.slock
, flags
);
426 hwptr_done
= dev
->adev
.hwptr_done_capture
;
427 spin_unlock_irqrestore(&dev
->adev
.slock
, flags
);
432 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
433 unsigned long offset
)
435 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
437 return vmalloc_to_page(pageptr
);
441 * AC97 volume control support
443 static int em28xx_vol_info(struct snd_kcontrol
*kcontrol
,
444 struct snd_ctl_elem_info
*info
)
446 info
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
448 info
->value
.integer
.min
= 0;
449 info
->value
.integer
.max
= 0x1f;
454 static int em28xx_vol_put(struct snd_kcontrol
*kcontrol
,
455 struct snd_ctl_elem_value
*value
)
457 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
458 u16 val
= (0x1f - (value
->value
.integer
.value
[0] & 0x1f)) |
459 (0x1f - (value
->value
.integer
.value
[1] & 0x1f)) << 8;
462 mutex_lock(&dev
->lock
);
463 rc
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
467 val
|= rc
& 0x8000; /* Preserve the mute flag */
469 rc
= em28xx_write_ac97(dev
, kcontrol
->private_value
, val
);
473 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
474 (val
& 0x8000) ? "muted " : "",
475 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
476 val
, (int)kcontrol
->private_value
);
479 mutex_unlock(&dev
->lock
);
483 static int em28xx_vol_get(struct snd_kcontrol
*kcontrol
,
484 struct snd_ctl_elem_value
*value
)
486 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
489 mutex_lock(&dev
->lock
);
490 val
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
491 mutex_unlock(&dev
->lock
);
495 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
496 (val
& 0x8000) ? "muted " : "",
497 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
498 val
, (int)kcontrol
->private_value
);
500 value
->value
.integer
.value
[0] = 0x1f - (val
& 0x1f);
501 value
->value
.integer
.value
[1] = 0x1f - ((val
<< 8) & 0x1f);
506 static int em28xx_vol_put_mute(struct snd_kcontrol
*kcontrol
,
507 struct snd_ctl_elem_value
*value
)
509 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
510 u16 val
= value
->value
.integer
.value
[0];
513 mutex_lock(&dev
->lock
);
514 rc
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
523 rc
= em28xx_write_ac97(dev
, kcontrol
->private_value
, rc
);
527 dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n",
528 (val
& 0x8000) ? "muted " : "",
529 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
530 val
, (int)kcontrol
->private_value
);
533 mutex_unlock(&dev
->lock
);
537 static int em28xx_vol_get_mute(struct snd_kcontrol
*kcontrol
,
538 struct snd_ctl_elem_value
*value
)
540 struct em28xx
*dev
= snd_kcontrol_chip(kcontrol
);
543 mutex_lock(&dev
->lock
);
544 val
= em28xx_read_ac97(dev
, kcontrol
->private_value
);
545 mutex_unlock(&dev
->lock
);
550 value
->value
.integer
.value
[0] = 0;
552 value
->value
.integer
.value
[0] = 1;
554 dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n",
555 (val
& 0x8000) ? "muted " : "",
556 0x1f - ((val
>> 8) & 0x1f), 0x1f - (val
& 0x1f),
557 val
, (int)kcontrol
->private_value
);
562 static const DECLARE_TLV_DB_SCALE(em28xx_db_scale
, -3450, 150, 0);
564 static int em28xx_cvol_new(struct snd_card
*card
, struct em28xx
*dev
,
569 struct snd_kcontrol
*kctl
;
570 struct snd_kcontrol_new tmp
;
572 memset (&tmp
, 0, sizeof(tmp
));
573 tmp
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
574 tmp
.private_value
= id
,
577 /* Add Mute Control */
578 sprintf(ctl_name
, "%s Switch", name
);
579 tmp
.get
= em28xx_vol_get_mute
;
580 tmp
.put
= em28xx_vol_put_mute
;
581 tmp
.info
= snd_ctl_boolean_mono_info
;
582 kctl
= snd_ctl_new1(&tmp
, dev
);
583 err
= snd_ctl_add(card
, kctl
);
586 dprintk("Added control %s for ac97 volume control 0x%04x\n",
589 memset (&tmp
, 0, sizeof(tmp
));
590 tmp
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
591 tmp
.private_value
= id
,
594 /* Add Volume Control */
595 sprintf(ctl_name
, "%s Volume", name
);
596 tmp
.get
= em28xx_vol_get
;
597 tmp
.put
= em28xx_vol_put
;
598 tmp
.info
= em28xx_vol_info
;
599 tmp
.tlv
.p
= em28xx_db_scale
,
600 kctl
= snd_ctl_new1(&tmp
, dev
);
601 err
= snd_ctl_add(card
, kctl
);
604 dprintk("Added control %s for ac97 volume control 0x%04x\n",
611 * register/unregister code and data
613 static struct snd_pcm_ops snd_em28xx_pcm_capture
= {
614 .open
= snd_em28xx_capture_open
,
615 .close
= snd_em28xx_pcm_close
,
616 .ioctl
= snd_pcm_lib_ioctl
,
617 .hw_params
= snd_em28xx_hw_capture_params
,
618 .hw_free
= snd_em28xx_hw_capture_free
,
619 .prepare
= snd_em28xx_prepare
,
620 .trigger
= snd_em28xx_capture_trigger
,
621 .pointer
= snd_em28xx_capture_pointer
,
622 .page
= snd_pcm_get_vmalloc_page
,
625 static int em28xx_audio_init(struct em28xx
*dev
)
627 struct em28xx_audio
*adev
= &dev
->adev
;
629 struct snd_card
*card
;
633 if (dev
->has_alsa_audio
!= 1) {
634 /* This device does not support the extension (in this case
635 the device is expecting the snd-usb-audio module or
636 doesn't have analog audio support at all) */
640 printk(KERN_INFO
"em28xx-audio.c: probing for em28x1 "
641 "non standard usbaudio\n");
642 printk(KERN_INFO
"em28xx-audio.c: Copyright (C) 2006 Markus "
645 err
= snd_card_create(index
[devnr
], "Em28xx Audio", THIS_MODULE
, 0,
650 spin_lock_init(&adev
->slock
);
651 err
= snd_pcm_new(card
, "Em28xx Audio", 0, 0, 1, &pcm
);
657 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_em28xx_pcm_capture
);
659 pcm
->private_data
= dev
;
660 strcpy(pcm
->name
, "Empia 28xx Capture");
662 snd_card_set_dev(card
, &dev
->udev
->dev
);
663 strcpy(card
->driver
, "Em28xx-Audio");
664 strcpy(card
->shortname
, "Em28xx Audio");
665 strcpy(card
->longname
, "Empia Em28xx Audio");
667 INIT_WORK(&dev
->wq_trigger
, audio_trigger
);
669 if (dev
->audio_mode
.ac97
!= EM28XX_NO_AC97
) {
670 em28xx_cvol_new(card
, dev
, "Video", AC97_VIDEO_VOL
);
671 em28xx_cvol_new(card
, dev
, "Line In", AC97_LINEIN_VOL
);
672 em28xx_cvol_new(card
, dev
, "Phone", AC97_PHONE_VOL
);
673 em28xx_cvol_new(card
, dev
, "Microphone", AC97_PHONE_VOL
);
674 em28xx_cvol_new(card
, dev
, "CD", AC97_CD_VOL
);
675 em28xx_cvol_new(card
, dev
, "AUX", AC97_AUX_VOL
);
676 em28xx_cvol_new(card
, dev
, "PCM", AC97_PCM_OUT_VOL
);
678 em28xx_cvol_new(card
, dev
, "Master", AC97_MASTER_VOL
);
679 em28xx_cvol_new(card
, dev
, "Line", AC97_LINE_LEVEL_VOL
);
680 em28xx_cvol_new(card
, dev
, "Mono", AC97_MASTER_MONO_VOL
);
681 em28xx_cvol_new(card
, dev
, "LFE", AC97_LFE_MASTER_VOL
);
682 em28xx_cvol_new(card
, dev
, "Surround", AC97_SURR_MASTER_VOL
);
685 err
= snd_card_register(card
);
690 adev
->sndcard
= card
;
691 adev
->udev
= dev
->udev
;
696 static int em28xx_audio_fini(struct em28xx
*dev
)
701 if (dev
->has_alsa_audio
!= 1) {
702 /* This device does not support the extension (in this case
703 the device is expecting the snd-usb-audio module or
704 doesn't have analog audio support at all) */
708 if (dev
->adev
.sndcard
) {
709 snd_card_free(dev
->adev
.sndcard
);
710 dev
->adev
.sndcard
= NULL
;
716 static struct em28xx_ops audio_ops
= {
718 .name
= "Em28xx Audio Extension",
719 .init
= em28xx_audio_init
,
720 .fini
= em28xx_audio_fini
,
723 static int __init
em28xx_alsa_register(void)
725 return em28xx_register_extension(&audio_ops
);
728 static void __exit
em28xx_alsa_unregister(void)
730 em28xx_unregister_extension(&audio_ops
);
733 MODULE_LICENSE("GPL");
734 MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>");
735 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
736 MODULE_DESCRIPTION("Em28xx Audio driver");
738 module_init(em28xx_alsa_register
);
739 module_exit(em28xx_alsa_unregister
);