Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Empiatech em28x1 audio extension |
| 3 | * |
| 4 | * Copyright (C) 2006 Markus Rechberger <mrechberger@gmail.com> |
| 5 | * |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame^] | 6 | * Copyright (C) 2007-2011 Mauro Carvalho Chehab <mchehab@redhat.com> |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 7 | * - Port to work with the in-kernel driver |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame^] | 8 | * - Cleanups, fixes, alsa-controls, etc. |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 9 | * |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 37 | #include <linux/module.h> |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 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> |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 44 | #include <sound/tlv.h> |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 45 | #include <media/v4l2-common.h> |
| 46 | #include "em28xx.h" |
| 47 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 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, \ |
Harvey Harrison | d80e134 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 55 | __func__, ##arg); \ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 56 | } while (0) |
| 57 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 58 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 59 | |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 60 | static int em28xx_deinit_isoc_audio(struct em28xx *dev) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 61 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 62 | int i; |
| 63 | |
| 64 | dprintk("Stopping isoc\n"); |
Douglas Schilling Landgraf | 3e099ba | 2009-02-08 10:45:34 -0300 | [diff] [blame] | 65 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
Robert Krakora | 9c06210 | 2009-01-25 13:08:07 -0300 | [diff] [blame] | 66 | if (!irqs_disabled()) |
| 67 | usb_kill_urb(dev->adev.urb[i]); |
| 68 | else |
| 69 | usb_unlink_urb(dev->adev.urb[i]); |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 70 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 71 | usb_free_urb(dev->adev.urb[i]); |
| 72 | dev->adev.urb[i] = NULL; |
Robert Krakora | 53d12e5 | 2009-01-16 11:23:25 -0300 | [diff] [blame] | 73 | |
Robert Krakora | 9c06210 | 2009-01-25 13:08:07 -0300 | [diff] [blame] | 74 | kfree(dev->adev.transfer_buffer[i]); |
| 75 | dev->adev.transfer_buffer[i] = NULL; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 76 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 77 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 81 | static void em28xx_audio_isocirq(struct urb *urb) |
| 82 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 83 | struct em28xx *dev = urb->context; |
| 84 | int i; |
| 85 | unsigned int oldptr; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 86 | int period_elapsed = 0; |
| 87 | int status; |
| 88 | unsigned char *cp; |
| 89 | unsigned int stride; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 90 | struct snd_pcm_substream *substream; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 91 | struct snd_pcm_runtime *runtime; |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 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 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 106 | if (atomic_read(&dev->stream_started) == 0) |
| 107 | return; |
| 108 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 109 | if (dev->adev.capture_pcm_substream) { |
| 110 | substream = dev->adev.capture_pcm_substream; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 111 | runtime = substream->runtime; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 112 | stride = runtime->frame_bits >> 3; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 113 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 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; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 119 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 120 | if (!length) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 121 | continue; |
| 122 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 123 | oldptr = dev->adev.hwptr_done_capture; |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 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 | |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 136 | snd_pcm_stream_lock(substream); |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 137 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 138 | dev->adev.hwptr_done_capture += length; |
| 139 | if (dev->adev.hwptr_done_capture >= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 140 | runtime->buffer_size) |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 141 | dev->adev.hwptr_done_capture -= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 142 | runtime->buffer_size; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 143 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 144 | dev->adev.capture_transfer_done += length; |
| 145 | if (dev->adev.capture_transfer_done >= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 146 | runtime->period_size) { |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 147 | dev->adev.capture_transfer_done -= |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 148 | runtime->period_size; |
| 149 | period_elapsed = 1; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 150 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 151 | |
Mauro Carvalho Chehab | 50f3beb | 2008-11-24 08:45:57 -0300 | [diff] [blame] | 152 | snd_pcm_stream_unlock(substream); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 153 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 154 | if (period_elapsed) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 155 | snd_pcm_period_elapsed(substream); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 156 | } |
| 157 | urb->status = 0; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 158 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 159 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 160 | if (status < 0) { |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 161 | em28xx_errdev("resubmit of audio urb failed (error=%i)\n", |
| 162 | status); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 163 | } |
| 164 | return; |
| 165 | } |
| 166 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 167 | static int em28xx_init_audio_isoc(struct em28xx *dev) |
| 168 | { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 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"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 174 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 175 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 176 | struct urb *urb; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 177 | int j, k; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 178 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 179 | dev->adev.transfer_buffer[i] = kmalloc(sb_size, GFP_ATOMIC); |
| 180 | if (!dev->adev.transfer_buffer[i]) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 181 | return -ENOMEM; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 182 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 183 | memset(dev->adev.transfer_buffer[i], 0x80, sb_size); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 184 | urb = usb_alloc_urb(EM28XX_NUM_AUDIO_PACKETS, GFP_ATOMIC); |
Douglas Schilling Landgraf | ff9b3e4 | 2008-09-04 11:20:20 -0300 | [diff] [blame] | 185 | if (!urb) { |
| 186 | em28xx_errdev("usb_alloc_urb failed!\n"); |
| 187 | for (j = 0; j < i; j++) { |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 188 | usb_free_urb(dev->adev.urb[j]); |
| 189 | kfree(dev->adev.transfer_buffer[j]); |
Douglas Schilling Landgraf | ff9b3e4 | 2008-09-04 11:20:20 -0300 | [diff] [blame] | 190 | } |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 191 | return -ENOMEM; |
Douglas Schilling Landgraf | ff9b3e4 | 2008-09-04 11:20:20 -0300 | [diff] [blame] | 192 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 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; |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 198 | urb->transfer_buffer = dev->adev.transfer_buffer[i]; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 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; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 209 | } |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 210 | dev->adev.urb[i] = urb; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 211 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 212 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 213 | for (i = 0; i < EM28XX_AUDIO_BUFS; i++) { |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 214 | errCode = usb_submit_urb(dev->adev.urb[i], GFP_ATOMIC); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 215 | if (errCode) { |
Mauro Carvalho Chehab | debb724 | 2011-06-19 10:15:35 -0300 | [diff] [blame] | 216 | em28xx_errdev("submit of audio urb failed\n"); |
Robert Krakora | aa5a182 | 2009-02-08 13:09:11 -0300 | [diff] [blame] | 217 | em28xx_deinit_isoc_audio(dev); |
Mauro Carvalho Chehab | debb724 | 2011-06-19 10:15:35 -0300 | [diff] [blame] | 218 | atomic_set(&dev->stream_started, 0); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 219 | return errCode; |
| 220 | } |
Mauro Carvalho Chehab | debb724 | 2011-06-19 10:15:35 -0300 | [diff] [blame] | 221 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 222 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 223 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 227 | static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, |
| 228 | size_t size) |
| 229 | { |
| 230 | struct snd_pcm_runtime *runtime = subs->runtime; |
| 231 | |
Nicola Soranzo | a1a6ee7 | 2009-02-10 23:28:24 -0300 | [diff] [blame] | 232 | dprintk("Allocating vbuffer\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 233 | if (runtime->dma_area) { |
| 234 | if (runtime->dma_bytes > size) |
| 235 | return 0; |
| 236 | |
| 237 | vfree(runtime->dma_area); |
| 238 | } |
| 239 | runtime->dma_area = vmalloc(size); |
| 240 | if (!runtime->dma_area) |
| 241 | return -ENOMEM; |
| 242 | |
| 243 | runtime->dma_bytes = size; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 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 | |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 252 | SNDRV_PCM_INFO_BATCH | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 253 | SNDRV_PCM_INFO_MMAP_VALID, |
| 254 | |
| 255 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 256 | |
| 257 | .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_KNOT, |
| 258 | |
| 259 | .rate_min = 48000, |
| 260 | .rate_max = 48000, |
| 261 | .channels_min = 2, |
| 262 | .channels_max = 2, |
| 263 | .buffer_bytes_max = 62720 * 8, /* just about the value in usbaudio.c */ |
| 264 | .period_bytes_min = 64, /* 12544/2, */ |
| 265 | .period_bytes_max = 12544, |
| 266 | .periods_min = 2, |
| 267 | .periods_max = 98, /* 12544, */ |
| 268 | }; |
| 269 | |
| 270 | static int snd_em28xx_capture_open(struct snd_pcm_substream *substream) |
| 271 | { |
| 272 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 273 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 274 | int ret = 0; |
| 275 | |
| 276 | dprintk("opening device and trying to acquire exclusive lock\n"); |
| 277 | |
Mauro Carvalho Chehab | 83ee87a | 2008-06-14 09:41:18 -0300 | [diff] [blame] | 278 | if (!dev) { |
Filipe Rosset | 0bc2308 | 2009-11-04 15:31:25 -0300 | [diff] [blame] | 279 | em28xx_err("BUG: em28xx can't find device struct." |
Mauro Carvalho Chehab | 83ee87a | 2008-06-14 09:41:18 -0300 | [diff] [blame] | 280 | " Can't proceed with open\n"); |
| 281 | return -ENODEV; |
| 282 | } |
| 283 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 284 | runtime->hw = snd_em28xx_hw_capture; |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame^] | 285 | if ((dev->alt == 0 || dev->audio_ifnum) && dev->adev.users == 0) { |
| 286 | if (dev->audio_ifnum) |
| 287 | dev->alt = 1; |
| 288 | else |
| 289 | dev->alt = 7; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 290 | |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame^] | 291 | dprintk("changing alternate number on interface %d to %d\n", |
| 292 | dev->audio_ifnum, dev->alt); |
| 293 | usb_set_interface(dev->udev, dev->audio_ifnum, dev->alt); |
| 294 | |
| 295 | /* Sets volume, mute, etc */ |
| 296 | dev->mute = 0; |
| 297 | mutex_lock(&dev->lock); |
| 298 | ret = em28xx_audio_analog_set(dev); |
| 299 | if (ret < 0) |
| 300 | goto err; |
| 301 | |
| 302 | dev->adev.users++; |
| 303 | mutex_unlock(&dev->lock); |
| 304 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 305 | |
| 306 | snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 307 | dev->adev.capture_pcm_substream = substream; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 308 | runtime->private_data = dev; |
| 309 | |
| 310 | return 0; |
| 311 | err: |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 312 | mutex_unlock(&dev->lock); |
| 313 | |
Filipe Rosset | 0bc2308 | 2009-11-04 15:31:25 -0300 | [diff] [blame] | 314 | em28xx_err("Error while configuring em28xx mixer\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | static int snd_em28xx_pcm_close(struct snd_pcm_substream *substream) |
| 319 | { |
| 320 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 321 | |
| 322 | dprintk("closing device\n"); |
| 323 | |
| 324 | dev->mute = 1; |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 325 | mutex_lock(&dev->lock); |
Douglas Schilling Landgraf | bf510ac | 2009-02-08 01:11:13 -0300 | [diff] [blame] | 326 | dev->adev.users--; |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 327 | if (atomic_read(&dev->stream_started) > 0) { |
| 328 | atomic_set(&dev->stream_started, 0); |
| 329 | schedule_work(&dev->wq_trigger); |
| 330 | } |
| 331 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 332 | em28xx_audio_analog_set(dev); |
Robert Krakora | 75c74d1 | 2009-05-28 11:16:19 -0300 | [diff] [blame] | 333 | if (substream->runtime->dma_area) { |
| 334 | dprintk("freeing\n"); |
| 335 | vfree(substream->runtime->dma_area); |
| 336 | substream->runtime->dma_area = NULL; |
| 337 | } |
Mauro Carvalho Chehab | 92ea42f | 2008-02-06 18:52:15 -0300 | [diff] [blame] | 338 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 339 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int snd_em28xx_hw_capture_params(struct snd_pcm_substream *substream, |
| 344 | struct snd_pcm_hw_params *hw_params) |
| 345 | { |
| 346 | unsigned int channels, rate, format; |
| 347 | int ret; |
| 348 | |
| 349 | dprintk("Setting capture parameters\n"); |
| 350 | |
| 351 | ret = snd_pcm_alloc_vmalloc_buffer(substream, |
| 352 | params_buffer_bytes(hw_params)); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 353 | if (ret < 0) |
| 354 | return ret; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 355 | format = params_format(hw_params); |
| 356 | rate = params_rate(hw_params); |
| 357 | channels = params_channels(hw_params); |
| 358 | |
| 359 | /* TODO: set up em28xx audio chip to deliver the correct audio format, |
| 360 | current default is 48000hz multiplexed => 96000hz mono |
| 361 | which shouldn't matter since analogue TV only supports mono */ |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int snd_em28xx_hw_capture_free(struct snd_pcm_substream *substream) |
| 366 | { |
| 367 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 368 | |
| 369 | dprintk("Stop capture, if needed\n"); |
| 370 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 371 | if (atomic_read(&dev->stream_started) > 0) { |
| 372 | atomic_set(&dev->stream_started, 0); |
| 373 | schedule_work(&dev->wq_trigger); |
| 374 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int snd_em28xx_prepare(struct snd_pcm_substream *substream) |
| 380 | { |
Devin Heitmueller | 96fbf77 | 2009-10-15 01:14:34 -0300 | [diff] [blame] | 381 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
| 382 | |
| 383 | dev->adev.hwptr_done_capture = 0; |
| 384 | dev->adev.capture_transfer_done = 0; |
| 385 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 389 | static void audio_trigger(struct work_struct *work) |
| 390 | { |
| 391 | struct em28xx *dev = container_of(work, struct em28xx, wq_trigger); |
| 392 | |
| 393 | if (atomic_read(&dev->stream_started)) { |
| 394 | dprintk("starting capture"); |
| 395 | em28xx_init_audio_isoc(dev); |
| 396 | } else { |
| 397 | dprintk("stopping capture"); |
| 398 | em28xx_deinit_isoc_audio(dev); |
| 399 | } |
| 400 | } |
| 401 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 402 | static int snd_em28xx_capture_trigger(struct snd_pcm_substream *substream, |
| 403 | int cmd) |
| 404 | { |
| 405 | struct em28xx *dev = snd_pcm_substream_chip(substream); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 406 | int retval = 0; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 407 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 408 | switch (cmd) { |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 409 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: /* fall through */ |
| 410 | case SNDRV_PCM_TRIGGER_RESUME: /* fall through */ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 411 | case SNDRV_PCM_TRIGGER_START: |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 412 | atomic_set(&dev->stream_started, 1); |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 413 | break; |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 414 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: /* fall through */ |
| 415 | case SNDRV_PCM_TRIGGER_SUSPEND: /* fall through */ |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 416 | case SNDRV_PCM_TRIGGER_STOP: |
Mauro Carvalho Chehab | dff0f8c | 2011-06-19 13:39:31 -0300 | [diff] [blame] | 417 | atomic_set(&dev->stream_started, 0); |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 418 | break; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 419 | default: |
Douglas Schilling Landgraf | df39ca6 | 2009-02-08 14:17:15 -0300 | [diff] [blame] | 420 | retval = -EINVAL; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 421 | } |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 422 | schedule_work(&dev->wq_trigger); |
Mauro Carvalho Chehab | 00d2e7a | 2011-06-17 20:28:51 -0300 | [diff] [blame] | 423 | return retval; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 424 | } |
| 425 | |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 426 | static snd_pcm_uframes_t snd_em28xx_capture_pointer(struct snd_pcm_substream |
| 427 | *substream) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 428 | { |
Nicola Soranzo | a1a6ee7 | 2009-02-10 23:28:24 -0300 | [diff] [blame] | 429 | unsigned long flags; |
Robert Krakora | 53d12e5 | 2009-01-16 11:23:25 -0300 | [diff] [blame] | 430 | struct em28xx *dev; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 431 | snd_pcm_uframes_t hwptr_done; |
Robert Krakora | 53d12e5 | 2009-01-16 11:23:25 -0300 | [diff] [blame] | 432 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 433 | dev = snd_pcm_substream_chip(substream); |
Douglas Schilling Landgraf | 00bc064 | 2009-01-25 15:12:29 -0300 | [diff] [blame] | 434 | spin_lock_irqsave(&dev->adev.slock, flags); |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 435 | hwptr_done = dev->adev.hwptr_done_capture; |
Douglas Schilling Landgraf | 00bc064 | 2009-01-25 15:12:29 -0300 | [diff] [blame] | 436 | spin_unlock_irqrestore(&dev->adev.slock, flags); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 437 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 438 | return hwptr_done; |
| 439 | } |
| 440 | |
| 441 | static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs, |
| 442 | unsigned long offset) |
| 443 | { |
| 444 | void *pageptr = subs->runtime->dma_area + offset; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 445 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 446 | return vmalloc_to_page(pageptr); |
| 447 | } |
| 448 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 449 | /* |
| 450 | * AC97 volume control support |
| 451 | */ |
| 452 | static int em28xx_vol_info(struct snd_kcontrol *kcontrol, |
| 453 | struct snd_ctl_elem_info *info) |
| 454 | { |
| 455 | info->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 456 | info->count = 2; |
| 457 | info->value.integer.min = 0; |
| 458 | info->value.integer.max = 0x1f; |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 463 | static int em28xx_vol_put(struct snd_kcontrol *kcontrol, |
| 464 | struct snd_ctl_elem_value *value) |
| 465 | { |
| 466 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 467 | u16 val = (0x1f - (value->value.integer.value[0] & 0x1f)) | |
| 468 | (0x1f - (value->value.integer.value[1] & 0x1f)) << 8; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 469 | int rc; |
| 470 | |
| 471 | mutex_lock(&dev->lock); |
| 472 | rc = em28xx_read_ac97(dev, kcontrol->private_value); |
| 473 | if (rc < 0) |
| 474 | goto err; |
| 475 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 476 | val |= rc & 0x8000; /* Preserve the mute flag */ |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 477 | |
| 478 | rc = em28xx_write_ac97(dev, kcontrol->private_value, val); |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 479 | if (rc < 0) |
| 480 | goto err; |
| 481 | |
| 482 | dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n", |
| 483 | (val & 0x8000) ? "muted " : "", |
| 484 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 485 | val, (int)kcontrol->private_value); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 486 | |
| 487 | err: |
| 488 | mutex_unlock(&dev->lock); |
| 489 | return rc; |
| 490 | } |
| 491 | |
| 492 | static int em28xx_vol_get(struct snd_kcontrol *kcontrol, |
| 493 | struct snd_ctl_elem_value *value) |
| 494 | { |
| 495 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
| 496 | int val; |
| 497 | |
| 498 | mutex_lock(&dev->lock); |
| 499 | val = em28xx_read_ac97(dev, kcontrol->private_value); |
| 500 | mutex_unlock(&dev->lock); |
| 501 | if (val < 0) |
| 502 | return val; |
| 503 | |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 504 | dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n", |
| 505 | (val & 0x8000) ? "muted " : "", |
| 506 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 507 | val, (int)kcontrol->private_value); |
| 508 | |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 509 | value->value.integer.value[0] = 0x1f - (val & 0x1f); |
| 510 | value->value.integer.value[1] = 0x1f - ((val << 8) & 0x1f); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 515 | static int em28xx_vol_put_mute(struct snd_kcontrol *kcontrol, |
| 516 | struct snd_ctl_elem_value *value) |
| 517 | { |
| 518 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
| 519 | u16 val = value->value.integer.value[0]; |
| 520 | int rc; |
| 521 | |
| 522 | mutex_lock(&dev->lock); |
| 523 | rc = em28xx_read_ac97(dev, kcontrol->private_value); |
| 524 | if (rc < 0) |
| 525 | goto err; |
| 526 | |
| 527 | if (val) |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 528 | rc &= 0x1f1f; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 529 | else |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 530 | rc |= 0x8000; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 531 | |
| 532 | rc = em28xx_write_ac97(dev, kcontrol->private_value, rc); |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 533 | if (rc < 0) |
| 534 | goto err; |
| 535 | |
| 536 | dprintk("%sleft vol %d, right vol %d (0x%04x) to ac97 volume control 0x%04x\n", |
| 537 | (val & 0x8000) ? "muted " : "", |
| 538 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 539 | val, (int)kcontrol->private_value); |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 540 | |
| 541 | err: |
| 542 | mutex_unlock(&dev->lock); |
| 543 | return rc; |
| 544 | } |
| 545 | |
| 546 | static int em28xx_vol_get_mute(struct snd_kcontrol *kcontrol, |
| 547 | struct snd_ctl_elem_value *value) |
| 548 | { |
| 549 | struct em28xx *dev = snd_kcontrol_chip(kcontrol); |
| 550 | int val; |
| 551 | |
| 552 | mutex_lock(&dev->lock); |
| 553 | val = em28xx_read_ac97(dev, kcontrol->private_value); |
| 554 | mutex_unlock(&dev->lock); |
| 555 | if (val < 0) |
| 556 | return val; |
| 557 | |
| 558 | if (val & 0x8000) |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 559 | value->value.integer.value[0] = 0; |
Mauro Carvalho Chehab | 71d7d83 | 2011-06-19 13:14:07 -0300 | [diff] [blame] | 560 | else |
| 561 | value->value.integer.value[0] = 1; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 562 | |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 563 | dprintk("%sleft vol %d, right vol %d (0x%04x) from ac97 volume control 0x%04x\n", |
| 564 | (val & 0x8000) ? "muted " : "", |
| 565 | 0x1f - ((val >> 8) & 0x1f), 0x1f - (val & 0x1f), |
| 566 | val, (int)kcontrol->private_value); |
| 567 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 568 | return 0; |
| 569 | } |
| 570 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 571 | static const DECLARE_TLV_DB_SCALE(em28xx_db_scale, -3450, 150, 0); |
| 572 | |
| 573 | static int em28xx_cvol_new(struct snd_card *card, struct em28xx *dev, |
| 574 | char *name, int id) |
| 575 | { |
| 576 | int err; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 577 | char ctl_name[44]; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 578 | struct snd_kcontrol *kctl; |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 579 | struct snd_kcontrol_new tmp; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 580 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 581 | memset (&tmp, 0, sizeof(tmp)); |
| 582 | tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 583 | tmp.private_value = id, |
| 584 | tmp.name = ctl_name, |
| 585 | |
| 586 | /* Add Mute Control */ |
| 587 | sprintf(ctl_name, "%s Switch", name); |
| 588 | tmp.get = em28xx_vol_get_mute; |
| 589 | tmp.put = em28xx_vol_put_mute; |
| 590 | tmp.info = snd_ctl_boolean_mono_info; |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 591 | kctl = snd_ctl_new1(&tmp, dev); |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 592 | err = snd_ctl_add(card, kctl); |
| 593 | if (err < 0) |
| 594 | return err; |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 595 | dprintk("Added control %s for ac97 volume control 0x%04x\n", |
| 596 | ctl_name, id); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 597 | |
Mauro Carvalho Chehab | 43131a2 | 2011-06-19 13:08:46 -0300 | [diff] [blame] | 598 | memset (&tmp, 0, sizeof(tmp)); |
| 599 | tmp.iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 600 | tmp.private_value = id, |
| 601 | tmp.name = ctl_name, |
| 602 | |
| 603 | /* Add Volume Control */ |
| 604 | sprintf(ctl_name, "%s Volume", name); |
| 605 | tmp.get = em28xx_vol_get; |
| 606 | tmp.put = em28xx_vol_put; |
| 607 | tmp.info = em28xx_vol_info; |
| 608 | tmp.tlv.p = em28xx_db_scale, |
| 609 | kctl = snd_ctl_new1(&tmp, dev); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 610 | err = snd_ctl_add(card, kctl); |
| 611 | if (err < 0) |
| 612 | return err; |
Mauro Carvalho Chehab | 66cb695 | 2011-06-18 11:00:20 -0300 | [diff] [blame] | 613 | dprintk("Added control %s for ac97 volume control 0x%04x\n", |
| 614 | ctl_name, id); |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 619 | /* |
| 620 | * register/unregister code and data |
| 621 | */ |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 622 | static struct snd_pcm_ops snd_em28xx_pcm_capture = { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 623 | .open = snd_em28xx_capture_open, |
| 624 | .close = snd_em28xx_pcm_close, |
| 625 | .ioctl = snd_pcm_lib_ioctl, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 626 | .hw_params = snd_em28xx_hw_capture_params, |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 627 | .hw_free = snd_em28xx_hw_capture_free, |
| 628 | .prepare = snd_em28xx_prepare, |
| 629 | .trigger = snd_em28xx_capture_trigger, |
| 630 | .pointer = snd_em28xx_capture_pointer, |
| 631 | .page = snd_pcm_get_vmalloc_page, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 632 | }; |
| 633 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 634 | static int em28xx_audio_init(struct em28xx *dev) |
| 635 | { |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 636 | struct em28xx_audio *adev = &dev->adev; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 637 | struct snd_pcm *pcm; |
| 638 | struct snd_card *card; |
| 639 | static int devnr; |
Devin Heitmueller | 65c9fd4 | 2008-11-12 02:04:53 -0300 | [diff] [blame] | 640 | int err; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 641 | |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame^] | 642 | if (!dev->has_alsa_audio || dev->audio_ifnum < 0) { |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 643 | /* This device does not support the extension (in this case |
Devin Heitmueller | 24a613e | 2008-11-12 02:05:19 -0300 | [diff] [blame] | 644 | the device is expecting the snd-usb-audio module or |
| 645 | doesn't have analog audio support at all) */ |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame^] | 649 | printk(KERN_INFO "em28xx-audio.c: probing for em28xx Audio Vendor Class\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 650 | printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2006 Markus " |
| 651 | "Rechberger\n"); |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame^] | 652 | printk(KERN_INFO "em28xx-audio.c: Copyright (C) 2007-2011 Mauro Carvalho Chehab\n"); |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 653 | |
Takashi Iwai | 758021b | 2009-01-12 15:17:09 +0100 | [diff] [blame] | 654 | err = snd_card_create(index[devnr], "Em28xx Audio", THIS_MODULE, 0, |
| 655 | &card); |
| 656 | if (err < 0) |
| 657 | return err; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 658 | |
| 659 | spin_lock_init(&adev->slock); |
Mauro Carvalho Chehab | 1f6340b | 2008-11-07 14:24:18 -0300 | [diff] [blame] | 660 | err = snd_pcm_new(card, "Em28xx Audio", 0, 0, 1, &pcm); |
| 661 | if (err < 0) { |
| 662 | snd_card_free(card); |
| 663 | return err; |
| 664 | } |
| 665 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 666 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_em28xx_pcm_capture); |
| 667 | pcm->info_flags = 0; |
| 668 | pcm->private_data = dev; |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 669 | strcpy(pcm->name, "Empia 28xx Capture"); |
Nicola Soranzo | 7662b00 | 2009-02-19 13:41:56 -0300 | [diff] [blame] | 670 | |
| 671 | snd_card_set_dev(card, &dev->udev->dev); |
Dan Carpenter | 4a3eaee | 2010-03-23 08:40:43 -0300 | [diff] [blame] | 672 | strcpy(card->driver, "Em28xx-Audio"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 673 | strcpy(card->shortname, "Em28xx Audio"); |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 674 | strcpy(card->longname, "Empia Em28xx Audio"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 675 | |
Mauro Carvalho Chehab | 39a96b4 | 2010-10-09 15:53:58 -0300 | [diff] [blame] | 676 | INIT_WORK(&dev->wq_trigger, audio_trigger); |
| 677 | |
Mauro Carvalho Chehab | 850d24a | 2011-06-19 13:06:40 -0300 | [diff] [blame] | 678 | if (dev->audio_mode.ac97 != EM28XX_NO_AC97) { |
| 679 | em28xx_cvol_new(card, dev, "Video", AC97_VIDEO_VOL); |
| 680 | em28xx_cvol_new(card, dev, "Line In", AC97_LINEIN_VOL); |
| 681 | em28xx_cvol_new(card, dev, "Phone", AC97_PHONE_VOL); |
| 682 | em28xx_cvol_new(card, dev, "Microphone", AC97_PHONE_VOL); |
| 683 | em28xx_cvol_new(card, dev, "CD", AC97_CD_VOL); |
| 684 | em28xx_cvol_new(card, dev, "AUX", AC97_AUX_VOL); |
| 685 | em28xx_cvol_new(card, dev, "PCM", AC97_PCM_OUT_VOL); |
| 686 | |
| 687 | em28xx_cvol_new(card, dev, "Master", AC97_MASTER_VOL); |
| 688 | em28xx_cvol_new(card, dev, "Line", AC97_LINE_LEVEL_VOL); |
| 689 | em28xx_cvol_new(card, dev, "Mono", AC97_MASTER_MONO_VOL); |
| 690 | em28xx_cvol_new(card, dev, "LFE", AC97_LFE_MASTER_VOL); |
| 691 | em28xx_cvol_new(card, dev, "Surround", AC97_SURR_MASTER_VOL); |
| 692 | } |
| 693 | |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 694 | err = snd_card_register(card); |
| 695 | if (err < 0) { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 696 | snd_card_free(card); |
Mauro Carvalho Chehab | 1f6340b | 2008-11-07 14:24:18 -0300 | [diff] [blame] | 697 | return err; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 698 | } |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 699 | adev->sndcard = card; |
| 700 | adev->udev = dev->udev; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 701 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int em28xx_audio_fini(struct em28xx *dev) |
| 706 | { |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 707 | if (dev == NULL) |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 708 | return 0; |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 709 | |
Devin Heitmueller | 24a613e | 2008-11-12 02:05:19 -0300 | [diff] [blame] | 710 | if (dev->has_alsa_audio != 1) { |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 711 | /* This device does not support the extension (in this case |
Devin Heitmueller | 24a613e | 2008-11-12 02:05:19 -0300 | [diff] [blame] | 712 | the device is expecting the snd-usb-audio module or |
| 713 | doesn't have analog audio support at all) */ |
Devin Heitmueller | df61918 | 2008-06-10 12:34:35 -0300 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
Mauro Carvalho Chehab | 9baed99 | 2008-12-31 09:37:33 -0300 | [diff] [blame] | 717 | if (dev->adev.sndcard) { |
| 718 | snd_card_free(dev->adev.sndcard); |
| 719 | dev->adev.sndcard = NULL; |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 720 | } |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 721 | |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | static struct em28xx_ops audio_ops = { |
Mauro Carvalho Chehab | 6d79468 | 2008-01-05 09:57:31 -0300 | [diff] [blame] | 726 | .id = EM28XX_AUDIO, |
Mauro Carvalho Chehab | 1a6f11e | 2008-01-05 09:56:24 -0300 | [diff] [blame] | 727 | .name = "Em28xx Audio Extension", |
| 728 | .init = em28xx_audio_init, |
| 729 | .fini = em28xx_audio_fini, |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 730 | }; |
| 731 | |
| 732 | static int __init em28xx_alsa_register(void) |
| 733 | { |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 734 | return em28xx_register_extension(&audio_ops); |
| 735 | } |
| 736 | |
| 737 | static void __exit em28xx_alsa_unregister(void) |
| 738 | { |
| 739 | em28xx_unregister_extension(&audio_ops); |
| 740 | } |
| 741 | |
| 742 | MODULE_LICENSE("GPL"); |
| 743 | MODULE_AUTHOR("Markus Rechberger <mrechberger@gmail.com>"); |
Mauro Carvalho Chehab | 4f83e7b | 2011-06-17 15:15:12 -0300 | [diff] [blame^] | 744 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
Markus Rechberger | a52932b | 2008-01-05 09:55:47 -0300 | [diff] [blame] | 745 | MODULE_DESCRIPTION("Em28xx Audio driver"); |
| 746 | |
| 747 | module_init(em28xx_alsa_register); |
| 748 | module_exit(em28xx_alsa_unregister); |