R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * * |
| 3 | * easycap_sound.c * |
| 4 | * * |
| 5 | * Audio driver for EasyCAP USB2.0 Video Capture Device DC60 * |
| 6 | * * |
| 7 | * * |
| 8 | ******************************************************************************/ |
| 9 | /* |
| 10 | * |
| 11 | * Copyright (C) 2010 R.M. Thomas <rmthomas@sciolus.org> |
| 12 | * |
| 13 | * |
| 14 | * This is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * The software is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this software; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | * |
| 28 | */ |
| 29 | /*****************************************************************************/ |
| 30 | |
| 31 | #include "easycap.h" |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 32 | |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 33 | /*--------------------------------------------------------------------------*/ |
| 34 | /* |
| 35 | * PARAMETERS USED WHEN REGISTERING THE AUDIO INTERFACE |
| 36 | */ |
| 37 | /*--------------------------------------------------------------------------*/ |
| 38 | static const struct snd_pcm_hardware alsa_hardware = { |
| 39 | .info = SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 40 | SNDRV_PCM_INFO_MMAP | |
| 41 | SNDRV_PCM_INFO_INTERLEAVED | |
| 42 | SNDRV_PCM_INFO_MMAP_VALID, |
| 43 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 44 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000, |
| 45 | .rate_min = 32000, |
| 46 | .rate_max = 48000, |
| 47 | .channels_min = 2, |
| 48 | .channels_max = 2, |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 49 | .buffer_bytes_max = PAGE_SIZE * |
| 50 | PAGES_PER_AUDIO_FRAGMENT * |
| 51 | AUDIO_FRAGMENT_MANY, |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 52 | .period_bytes_min = PAGE_SIZE * PAGES_PER_AUDIO_FRAGMENT, |
| 53 | .period_bytes_max = PAGE_SIZE * PAGES_PER_AUDIO_FRAGMENT * 2, |
| 54 | .periods_min = AUDIO_FRAGMENT_MANY, |
| 55 | .periods_max = AUDIO_FRAGMENT_MANY * 2, |
| 56 | }; |
| 57 | |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 58 | |
| 59 | /*****************************************************************************/ |
| 60 | /*---------------------------------------------------------------------------*/ |
| 61 | /* |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 62 | * ON COMPLETION OF AN AUDIO URB ITS DATA IS COPIED TO THE DAM BUFFER |
| 63 | * PROVIDED peasycap->audio_idle IS ZERO. REGARDLESS OF THIS BEING TRUE, |
| 64 | * IT IS RESUBMITTED PROVIDED peasycap->audio_isoc_streaming IS NOT ZERO. |
| 65 | */ |
| 66 | /*---------------------------------------------------------------------------*/ |
| 67 | void |
| 68 | easycap_alsa_complete(struct urb *purb) |
| 69 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 70 | struct easycap *peasycap; |
| 71 | struct snd_pcm_substream *pss; |
| 72 | struct snd_pcm_runtime *prt; |
| 73 | int dma_bytes, fragment_bytes; |
| 74 | int isfragment; |
| 75 | u8 *p1, *p2; |
| 76 | s16 tmp; |
| 77 | int i, j, more, much, rc; |
Tomas Winkler | 3fc0dae | 2011-02-03 13:42:40 +0200 | [diff] [blame] | 78 | #ifdef UPSAMPLE |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 79 | int k; |
| 80 | s16 oldaudio, newaudio, delta; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 81 | #endif /*UPSAMPLE*/ |
| 82 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 83 | JOT(16, "\n"); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 84 | |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 85 | if (!purb) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 86 | SAY("ERROR: purb is NULL\n"); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 87 | return; |
| 88 | } |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 89 | peasycap = purb->context; |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 90 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 91 | SAY("ERROR: peasycap is NULL\n"); |
| 92 | return; |
| 93 | } |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 94 | much = 0; |
| 95 | if (peasycap->audio_idle) { |
| 96 | JOM(16, "%i=audio_idle %i=audio_isoc_streaming\n", |
| 97 | peasycap->audio_idle, peasycap->audio_isoc_streaming); |
| 98 | if (peasycap->audio_isoc_streaming) |
| 99 | goto resubmit; |
| 100 | } |
| 101 | /*---------------------------------------------------------------------------*/ |
| 102 | pss = peasycap->psubstream; |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 103 | if (!pss) |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 104 | goto resubmit; |
| 105 | prt = pss->runtime; |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 106 | if (!prt) |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 107 | goto resubmit; |
| 108 | dma_bytes = (int)prt->dma_bytes; |
| 109 | if (0 == dma_bytes) |
| 110 | goto resubmit; |
| 111 | fragment_bytes = 4 * ((int)prt->period_size); |
| 112 | if (0 == fragment_bytes) |
| 113 | goto resubmit; |
| 114 | /* -------------------------------------------------------------------------*/ |
| 115 | if (purb->status) { |
| 116 | if ((-ESHUTDOWN == purb->status) || (-ENOENT == purb->status)) { |
| 117 | JOM(16, "urb status -ESHUTDOWN or -ENOENT\n"); |
| 118 | return; |
| 119 | } |
| 120 | SAM("ERROR: non-zero urb status: -%s: %d\n", |
| 121 | strerror(purb->status), purb->status); |
| 122 | goto resubmit; |
| 123 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 124 | /*---------------------------------------------------------------------------*/ |
| 125 | /* |
| 126 | * PROCEED HERE WHEN NO ERROR |
| 127 | */ |
| 128 | /*---------------------------------------------------------------------------*/ |
| 129 | |
Tomas Winkler | 3fc0dae | 2011-02-03 13:42:40 +0200 | [diff] [blame] | 130 | #ifdef UPSAMPLE |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 131 | oldaudio = peasycap->oldaudio; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 132 | #endif /*UPSAMPLE*/ |
| 133 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 134 | for (i = 0; i < purb->number_of_packets; i++) { |
| 135 | if (purb->iso_frame_desc[i].status < 0) { |
| 136 | SAM("-%s: %d\n", |
| 137 | strerror(purb->iso_frame_desc[i].status), |
| 138 | purb->iso_frame_desc[i].status); |
| 139 | } |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 140 | if (purb->iso_frame_desc[i].status) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 141 | JOM(12, "discarding audio samples because " |
| 142 | "%i=purb->iso_frame_desc[i].status\n", |
| 143 | purb->iso_frame_desc[i].status); |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 144 | continue; |
| 145 | } |
| 146 | more = purb->iso_frame_desc[i].actual_length; |
| 147 | if (more == 0) { |
| 148 | peasycap->audio_mt++; |
| 149 | continue; |
| 150 | } |
| 151 | if (0 > more) { |
| 152 | SAM("MISTAKE: more is negative\n"); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if (peasycap->audio_mt) { |
| 157 | JOM(12, "%4i empty audio urb frames\n", |
| 158 | peasycap->audio_mt); |
| 159 | peasycap->audio_mt = 0; |
| 160 | } |
| 161 | |
| 162 | p1 = (u8 *)(purb->transfer_buffer + |
| 163 | purb->iso_frame_desc[i].offset); |
| 164 | |
| 165 | /* |
| 166 | * COPY more BYTES FROM ISOC BUFFER |
| 167 | * TO THE DMA BUFFER, CONVERTING |
| 168 | * 8-BIT MONO TO 16-BIT SIGNED |
| 169 | * LITTLE-ENDIAN SAMPLES IF NECESSARY |
| 170 | */ |
| 171 | while (more) { |
| 172 | much = dma_bytes - peasycap->dma_fill; |
| 173 | if (0 > much) { |
| 174 | SAM("MISTAKE: much is negative\n"); |
| 175 | return; |
| 176 | } |
| 177 | if (0 == much) { |
| 178 | peasycap->dma_fill = 0; |
| 179 | peasycap->dma_next = fragment_bytes; |
| 180 | JOM(8, "wrapped dma buffer\n"); |
| 181 | } |
| 182 | if (!peasycap->microphone) { |
| 183 | if (much > more) |
| 184 | much = more; |
| 185 | memcpy(prt->dma_area + peasycap->dma_fill, |
| 186 | p1, much); |
| 187 | p1 += much; |
| 188 | more -= much; |
| 189 | } else { |
| 190 | #ifdef UPSAMPLE |
| 191 | if (much % 16) |
| 192 | JOM(8, "MISTAKE? much" |
| 193 | " is not divisible by 16\n"); |
| 194 | if (much > (16 * more)) |
| 195 | much = 16 * more; |
| 196 | p2 = (u8 *)(prt->dma_area + peasycap->dma_fill); |
| 197 | |
| 198 | for (j = 0; j < (much / 16); j++) { |
| 199 | newaudio = ((int) *p1) - 128; |
| 200 | newaudio = 128 * newaudio; |
| 201 | |
| 202 | delta = (newaudio - oldaudio) / 4; |
| 203 | tmp = oldaudio + delta; |
| 204 | |
| 205 | for (k = 0; k < 4; k++) { |
| 206 | *p2 = (0x00FF & tmp); |
| 207 | *(p2 + 1) = (0xFF00 & tmp) >> 8; |
| 208 | p2 += 2; |
| 209 | *p2 = (0x00FF & tmp); |
| 210 | *(p2 + 1) = (0xFF00 & tmp) >> 8; |
| 211 | p2 += 2; |
| 212 | tmp += delta; |
| 213 | } |
| 214 | p1++; |
| 215 | more--; |
| 216 | oldaudio = tmp; |
| 217 | } |
| 218 | #else /*!UPSAMPLE*/ |
| 219 | if (much > (2 * more)) |
| 220 | much = 2 * more; |
| 221 | p2 = (u8 *)(prt->dma_area + peasycap->dma_fill); |
| 222 | |
| 223 | for (j = 0; j < (much / 2); j++) { |
| 224 | tmp = ((int) *p1) - 128; |
| 225 | tmp = 128 * tmp; |
| 226 | *p2 = (0x00FF & tmp); |
| 227 | *(p2 + 1) = (0xFF00 & tmp) >> 8; |
| 228 | p1++; |
| 229 | p2 += 2; |
| 230 | more--; |
| 231 | } |
| 232 | #endif /*UPSAMPLE*/ |
| 233 | } |
| 234 | peasycap->dma_fill += much; |
| 235 | if (peasycap->dma_fill >= peasycap->dma_next) { |
| 236 | isfragment = peasycap->dma_fill / fragment_bytes; |
| 237 | if (0 > isfragment) { |
| 238 | SAM("MISTAKE: isfragment is negative\n"); |
| 239 | return; |
| 240 | } |
| 241 | peasycap->dma_read = (isfragment - 1) * fragment_bytes; |
| 242 | peasycap->dma_next = (isfragment + 1) * fragment_bytes; |
| 243 | if (dma_bytes < peasycap->dma_next) |
| 244 | peasycap->dma_next = fragment_bytes; |
| 245 | |
| 246 | if (0 <= peasycap->dma_read) { |
| 247 | JOM(8, "snd_pcm_period_elapsed(), %i=" |
| 248 | "isfragment\n", isfragment); |
| 249 | snd_pcm_period_elapsed(pss); |
| 250 | } |
| 251 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 252 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 253 | |
Tomas Winkler | 3fc0dae | 2011-02-03 13:42:40 +0200 | [diff] [blame] | 254 | #ifdef UPSAMPLE |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 255 | peasycap->oldaudio = oldaudio; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 256 | #endif /*UPSAMPLE*/ |
| 257 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 258 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 259 | /*---------------------------------------------------------------------------*/ |
| 260 | /* |
| 261 | * RESUBMIT THIS URB |
| 262 | */ |
| 263 | /*---------------------------------------------------------------------------*/ |
| 264 | resubmit: |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 265 | if (peasycap->audio_isoc_streaming == 0) |
| 266 | return; |
| 267 | |
| 268 | rc = usb_submit_urb(purb, GFP_ATOMIC); |
| 269 | if (rc) { |
| 270 | if ((-ENODEV != rc) && (-ENOENT != rc)) { |
| 271 | SAM("ERROR: while %i=audio_idle, usb_submit_urb failed " |
| 272 | "with rc: -%s :%d\n", |
| 273 | peasycap->audio_idle, strerror(rc), rc); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 274 | } |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 275 | if (0 < peasycap->audio_isoc_streaming) |
| 276 | peasycap->audio_isoc_streaming--; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 277 | } |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 278 | return; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 279 | } |
| 280 | /*****************************************************************************/ |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 281 | static int easycap_alsa_open(struct snd_pcm_substream *pss) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 282 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 283 | struct snd_pcm *psnd_pcm; |
| 284 | struct snd_card *psnd_card; |
| 285 | struct easycap *peasycap; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 286 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 287 | JOT(4, "\n"); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 288 | if (!pss) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 289 | SAY("ERROR: pss is NULL\n"); |
| 290 | return -EFAULT; |
| 291 | } |
| 292 | psnd_pcm = pss->pcm; |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 293 | if (!psnd_pcm) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 294 | SAY("ERROR: psnd_pcm is NULL\n"); |
| 295 | return -EFAULT; |
| 296 | } |
| 297 | psnd_card = psnd_pcm->card; |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 298 | if (!psnd_card) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 299 | SAY("ERROR: psnd_card is NULL\n"); |
| 300 | return -EFAULT; |
| 301 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 302 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 303 | peasycap = psnd_card->private_data; |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 304 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 305 | SAY("ERROR: peasycap is NULL\n"); |
| 306 | return -EFAULT; |
| 307 | } |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 308 | if (peasycap->psnd_card != psnd_card) { |
| 309 | SAM("ERROR: bad peasycap->psnd_card\n"); |
| 310 | return -EFAULT; |
| 311 | } |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 312 | if (peasycap->psubstream) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 313 | SAM("ERROR: bad peasycap->psubstream\n"); |
| 314 | return -EFAULT; |
| 315 | } |
| 316 | pss->private_data = peasycap; |
| 317 | peasycap->psubstream = pss; |
| 318 | pss->runtime->hw = peasycap->alsa_hardware; |
| 319 | pss->runtime->private_data = peasycap; |
| 320 | pss->private_data = peasycap; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 321 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 322 | if (0 != easycap_sound_setup(peasycap)) { |
| 323 | JOM(4, "ending unsuccessfully\n"); |
| 324 | return -EFAULT; |
| 325 | } |
| 326 | JOM(4, "ending successfully\n"); |
| 327 | return 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 328 | } |
| 329 | /*****************************************************************************/ |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 330 | static int easycap_alsa_close(struct snd_pcm_substream *pss) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 331 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 332 | struct easycap *peasycap; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 333 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 334 | JOT(4, "\n"); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 335 | if (!pss) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 336 | SAY("ERROR: pss is NULL\n"); |
| 337 | return -EFAULT; |
| 338 | } |
| 339 | peasycap = snd_pcm_substream_chip(pss); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 340 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 341 | SAY("ERROR: peasycap is NULL\n"); |
| 342 | return -EFAULT; |
| 343 | } |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 344 | pss->private_data = NULL; |
| 345 | peasycap->psubstream = NULL; |
| 346 | JOT(4, "ending successfully\n"); |
| 347 | return 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 348 | } |
| 349 | /*****************************************************************************/ |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 350 | static int easycap_alsa_vmalloc(struct snd_pcm_substream *pss, size_t sz) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 351 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 352 | struct snd_pcm_runtime *prt; |
| 353 | JOT(4, "\n"); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 354 | |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 355 | if (!pss) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 356 | SAY("ERROR: pss is NULL\n"); |
| 357 | return -EFAULT; |
| 358 | } |
| 359 | prt = pss->runtime; |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 360 | if (!prt) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 361 | SAY("ERROR: substream.runtime is NULL\n"); |
| 362 | return -EFAULT; |
| 363 | } |
| 364 | if (prt->dma_area) { |
| 365 | if (prt->dma_bytes > sz) |
| 366 | return 0; |
| 367 | vfree(prt->dma_area); |
| 368 | } |
| 369 | prt->dma_area = vmalloc(sz); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 370 | if (!prt->dma_area) |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 371 | return -ENOMEM; |
| 372 | prt->dma_bytes = sz; |
| 373 | return 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 374 | } |
| 375 | /*****************************************************************************/ |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 376 | static int easycap_alsa_hw_params(struct snd_pcm_substream *pss, |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 377 | struct snd_pcm_hw_params *phw) |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 378 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 379 | int rc; |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 380 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 381 | JOT(4, "%i\n", (params_buffer_bytes(phw))); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 382 | if (!pss) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 383 | SAY("ERROR: pss is NULL\n"); |
| 384 | return -EFAULT; |
| 385 | } |
| 386 | rc = easycap_alsa_vmalloc(pss, params_buffer_bytes(phw)); |
| 387 | if (rc) |
| 388 | return rc; |
| 389 | return 0; |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 390 | } |
| 391 | /*****************************************************************************/ |
| 392 | static int easycap_alsa_hw_free(struct snd_pcm_substream *pss) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 393 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 394 | struct snd_pcm_runtime *prt; |
| 395 | JOT(4, "\n"); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 396 | |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 397 | if (!pss) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 398 | SAY("ERROR: pss is NULL\n"); |
| 399 | return -EFAULT; |
| 400 | } |
| 401 | prt = pss->runtime; |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 402 | if (!prt) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 403 | SAY("ERROR: substream.runtime is NULL\n"); |
| 404 | return -EFAULT; |
| 405 | } |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 406 | if (prt->dma_area) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 407 | JOT(8, "prt->dma_area = %p\n", prt->dma_area); |
| 408 | vfree(prt->dma_area); |
| 409 | prt->dma_area = NULL; |
| 410 | } else |
| 411 | JOT(8, "dma_area already freed\n"); |
| 412 | return 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 413 | } |
| 414 | /*****************************************************************************/ |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 415 | static int easycap_alsa_prepare(struct snd_pcm_substream *pss) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 416 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 417 | struct easycap *peasycap; |
| 418 | struct snd_pcm_runtime *prt; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 419 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 420 | JOT(4, "\n"); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 421 | if (!pss) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 422 | SAY("ERROR: pss is NULL\n"); |
| 423 | return -EFAULT; |
| 424 | } |
| 425 | prt = pss->runtime; |
| 426 | peasycap = snd_pcm_substream_chip(pss); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 427 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 428 | SAY("ERROR: peasycap is NULL\n"); |
| 429 | return -EFAULT; |
| 430 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 431 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 432 | JOM(16, "ALSA decides %8i Hz=rate\n", pss->runtime->rate); |
| 433 | JOM(16, "ALSA decides %8ld =period_size\n", pss->runtime->period_size); |
| 434 | JOM(16, "ALSA decides %8i =periods\n", pss->runtime->periods); |
| 435 | JOM(16, "ALSA decides %8ld =buffer_size\n", pss->runtime->buffer_size); |
| 436 | JOM(16, "ALSA decides %8zd =dma_bytes\n", pss->runtime->dma_bytes); |
| 437 | JOM(16, "ALSA decides %8ld =boundary\n", pss->runtime->boundary); |
| 438 | JOM(16, "ALSA decides %8i =period_step\n", pss->runtime->period_step); |
| 439 | JOM(16, "ALSA decides %8i =sample_bits\n", pss->runtime->sample_bits); |
| 440 | JOM(16, "ALSA decides %8i =frame_bits\n", pss->runtime->frame_bits); |
| 441 | JOM(16, "ALSA decides %8ld =min_align\n", pss->runtime->min_align); |
| 442 | JOM(12, "ALSA decides %8ld =hw_ptr_base\n", pss->runtime->hw_ptr_base); |
| 443 | JOM(12, "ALSA decides %8ld =hw_ptr_interrupt\n", |
| 444 | pss->runtime->hw_ptr_interrupt); |
| 445 | |
| 446 | if (prt->dma_bytes != 4 * ((int)prt->period_size) * ((int)prt->periods)) { |
| 447 | SAY("MISTAKE: unexpected ALSA parameters\n"); |
| 448 | return -ENOENT; |
| 449 | } |
| 450 | return 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 451 | } |
| 452 | /*****************************************************************************/ |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 453 | static int easycap_alsa_ack(struct snd_pcm_substream *pss) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 454 | { |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 455 | return 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 456 | } |
| 457 | /*****************************************************************************/ |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 458 | static int easycap_alsa_trigger(struct snd_pcm_substream *pss, int cmd) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 459 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 460 | struct easycap *peasycap; |
| 461 | int retval; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 462 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 463 | JOT(4, "%i=cmd cf %i=START %i=STOP\n", cmd, SNDRV_PCM_TRIGGER_START, |
| 464 | SNDRV_PCM_TRIGGER_STOP); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 465 | if (!pss) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 466 | SAY("ERROR: pss is NULL\n"); |
| 467 | return -EFAULT; |
| 468 | } |
| 469 | peasycap = snd_pcm_substream_chip(pss); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 470 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 471 | SAY("ERROR: peasycap is NULL\n"); |
| 472 | return -EFAULT; |
| 473 | } |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 474 | switch (cmd) { |
| 475 | case SNDRV_PCM_TRIGGER_START: { |
| 476 | peasycap->audio_idle = 0; |
| 477 | break; |
| 478 | } |
| 479 | case SNDRV_PCM_TRIGGER_STOP: { |
| 480 | peasycap->audio_idle = 1; |
| 481 | break; |
| 482 | } |
| 483 | default: |
| 484 | retval = -EINVAL; |
| 485 | } |
| 486 | return 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 487 | } |
| 488 | /*****************************************************************************/ |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 489 | static snd_pcm_uframes_t easycap_alsa_pointer(struct snd_pcm_substream *pss) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 490 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 491 | struct easycap *peasycap; |
| 492 | snd_pcm_uframes_t offset; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 493 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 494 | JOT(16, "\n"); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 495 | if (!pss) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 496 | SAY("ERROR: pss is NULL\n"); |
| 497 | return -EFAULT; |
| 498 | } |
| 499 | peasycap = snd_pcm_substream_chip(pss); |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 500 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 501 | SAY("ERROR: peasycap is NULL\n"); |
| 502 | return -EFAULT; |
| 503 | } |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 504 | if ((0 != peasycap->audio_eof) || (0 != peasycap->audio_idle)) { |
| 505 | JOM(8, "returning -EIO because " |
| 506 | "%i=audio_idle %i=audio_eof\n", |
| 507 | peasycap->audio_idle, peasycap->audio_eof); |
| 508 | return -EIO; |
| 509 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 510 | /*---------------------------------------------------------------------------*/ |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 511 | if (0 > peasycap->dma_read) { |
| 512 | JOM(8, "returning -EBUSY\n"); |
| 513 | return -EBUSY; |
| 514 | } |
| 515 | offset = ((snd_pcm_uframes_t)peasycap->dma_read)/4; |
| 516 | JOM(8, "ALSA decides %8i =hw_ptr_base\n", (int)pss->runtime->hw_ptr_base); |
| 517 | JOM(8, "ALSA decides %8i =hw_ptr_interrupt\n", |
| 518 | (int)pss->runtime->hw_ptr_interrupt); |
| 519 | JOM(8, "%7i=offset %7i=dma_read %7i=dma_next\n", |
| 520 | (int)offset, peasycap->dma_read, peasycap->dma_next); |
| 521 | return offset; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 522 | } |
| 523 | /*****************************************************************************/ |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 524 | static struct page * |
| 525 | easycap_alsa_page(struct snd_pcm_substream *pss, unsigned long offset) |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 526 | { |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 527 | return vmalloc_to_page(pss->runtime->dma_area + offset); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 528 | } |
| 529 | /*****************************************************************************/ |
| 530 | |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 531 | static struct snd_pcm_ops easycap_alsa_pcm_ops = { |
| 532 | .open = easycap_alsa_open, |
| 533 | .close = easycap_alsa_close, |
| 534 | .ioctl = snd_pcm_lib_ioctl, |
| 535 | .hw_params = easycap_alsa_hw_params, |
| 536 | .hw_free = easycap_alsa_hw_free, |
| 537 | .prepare = easycap_alsa_prepare, |
| 538 | .ack = easycap_alsa_ack, |
| 539 | .trigger = easycap_alsa_trigger, |
| 540 | .pointer = easycap_alsa_pointer, |
| 541 | .page = easycap_alsa_page, |
| 542 | }; |
| 543 | |
| 544 | /*****************************************************************************/ |
| 545 | /*---------------------------------------------------------------------------*/ |
| 546 | /* |
| 547 | * THE FUNCTION snd_card_create() HAS THIS_MODULE AS AN ARGUMENT. THIS |
| 548 | * MEANS MODULE easycap. BEWARE. |
| 549 | */ |
| 550 | /*---------------------------------------------------------------------------*/ |
| 551 | int easycap_alsa_probe(struct easycap *peasycap) |
| 552 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 553 | int rc; |
| 554 | struct snd_card *psnd_card; |
| 555 | struct snd_pcm *psnd_pcm; |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 556 | |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 557 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 558 | SAY("ERROR: peasycap is NULL\n"); |
| 559 | return -ENODEV; |
| 560 | } |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 561 | if (0 > peasycap->minor) { |
| 562 | SAY("ERROR: no minor\n"); |
| 563 | return -ENODEV; |
| 564 | } |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 565 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 566 | peasycap->alsa_hardware = alsa_hardware; |
Tomas Winkler | 27d683a | 2011-03-03 00:10:49 +0200 | [diff] [blame] | 567 | if (peasycap->microphone) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 568 | peasycap->alsa_hardware.rates = SNDRV_PCM_RATE_32000; |
| 569 | peasycap->alsa_hardware.rate_min = 32000; |
| 570 | peasycap->alsa_hardware.rate_max = 32000; |
| 571 | } else { |
| 572 | peasycap->alsa_hardware.rates = SNDRV_PCM_RATE_48000; |
| 573 | peasycap->alsa_hardware.rate_min = 48000; |
| 574 | peasycap->alsa_hardware.rate_max = 48000; |
| 575 | } |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 576 | |
| 577 | if (0 != snd_card_create(SNDRV_DEFAULT_IDX1, "easycap_alsa", |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 578 | THIS_MODULE, 0, &psnd_card)) { |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 579 | SAY("ERROR: Cannot do ALSA snd_card_create()\n"); |
| 580 | return -EFAULT; |
| 581 | } |
| 582 | |
| 583 | sprintf(&psnd_card->id[0], "EasyALSA%i", peasycap->minor); |
| 584 | strcpy(&psnd_card->driver[0], EASYCAP_DRIVER_DESCRIPTION); |
| 585 | strcpy(&psnd_card->shortname[0], "easycap_alsa"); |
| 586 | sprintf(&psnd_card->longname[0], "%s", &psnd_card->shortname[0]); |
| 587 | |
| 588 | psnd_card->dev = &peasycap->pusb_device->dev; |
| 589 | psnd_card->private_data = peasycap; |
| 590 | peasycap->psnd_card = psnd_card; |
| 591 | |
| 592 | rc = snd_pcm_new(psnd_card, "easycap_pcm", 0, 0, 1, &psnd_pcm); |
Tomas Winkler | 6911e7e | 2011-02-03 13:42:42 +0200 | [diff] [blame] | 593 | if (rc) { |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 594 | SAM("ERROR: Cannot do ALSA snd_pcm_new()\n"); |
| 595 | snd_card_free(psnd_card); |
| 596 | return -EFAULT; |
| 597 | } |
| 598 | |
| 599 | snd_pcm_set_ops(psnd_pcm, SNDRV_PCM_STREAM_CAPTURE, |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 600 | &easycap_alsa_pcm_ops); |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 601 | psnd_pcm->info_flags = 0; |
| 602 | strcpy(&psnd_pcm->name[0], &psnd_card->id[0]); |
| 603 | psnd_pcm->private_data = peasycap; |
| 604 | peasycap->psnd_pcm = psnd_pcm; |
Tomas Winkler | 3c1fb66 | 2011-02-03 13:42:37 +0200 | [diff] [blame] | 605 | peasycap->psubstream = NULL; |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 606 | |
| 607 | rc = snd_card_register(psnd_card); |
Tomas Winkler | 6911e7e | 2011-02-03 13:42:42 +0200 | [diff] [blame] | 608 | if (rc) { |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 609 | SAM("ERROR: Cannot do ALSA snd_card_register()\n"); |
| 610 | snd_card_free(psnd_card); |
| 611 | return -EFAULT; |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 612 | } |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 613 | |
| 614 | SAM("registered %s\n", &psnd_card->id[0]); |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 615 | return 0; |
Tomas Winkler | 68e4cca | 2011-01-24 17:08:22 +0200 | [diff] [blame] | 616 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 617 | |
| 618 | /*****************************************************************************/ |
| 619 | /*****************************************************************************/ |
| 620 | /*****************************************************************************/ |
| 621 | /*****************************************************************************/ |
| 622 | /*****************************************************************************/ |
| 623 | /*****************************************************************************/ |
| 624 | /*---------------------------------------------------------------------------*/ |
| 625 | /* |
| 626 | * COMMON AUDIO INITIALIZATION |
| 627 | */ |
| 628 | /*---------------------------------------------------------------------------*/ |
| 629 | int |
| 630 | easycap_sound_setup(struct easycap *peasycap) |
| 631 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 632 | int rc; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 633 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 634 | JOM(4, "starting initialization\n"); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 635 | |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 636 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 637 | SAY("ERROR: peasycap is NULL.\n"); |
| 638 | return -EFAULT; |
| 639 | } |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 640 | if (!peasycap->pusb_device) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 641 | SAM("ERROR: peasycap->pusb_device is NULL\n"); |
| 642 | return -ENODEV; |
| 643 | } |
| 644 | JOM(16, "0x%08lX=peasycap->pusb_device\n", (long int)peasycap->pusb_device); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 645 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 646 | rc = audio_setup(peasycap); |
| 647 | JOM(8, "audio_setup() returned %i\n", rc); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 648 | |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 649 | if (!peasycap->pusb_device) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 650 | SAM("ERROR: peasycap->pusb_device has become NULL\n"); |
| 651 | return -ENODEV; |
| 652 | } |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 653 | /*---------------------------------------------------------------------------*/ |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 654 | if (!peasycap->pusb_device) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 655 | SAM("ERROR: peasycap->pusb_device has become NULL\n"); |
| 656 | return -ENODEV; |
| 657 | } |
| 658 | rc = usb_set_interface(peasycap->pusb_device, peasycap->audio_interface, |
| 659 | peasycap->audio_altsetting_on); |
| 660 | JOM(8, "usb_set_interface(.,%i,%i) returned %i\n", peasycap->audio_interface, |
| 661 | peasycap->audio_altsetting_on, rc); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 662 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 663 | rc = wakeup_device(peasycap->pusb_device); |
| 664 | JOM(8, "wakeup_device() returned %i\n", rc); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 665 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 666 | peasycap->audio_eof = 0; |
| 667 | peasycap->audio_idle = 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 668 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 669 | peasycap->timeval1.tv_sec = 0; |
| 670 | peasycap->timeval1.tv_usec = 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 671 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 672 | submit_audio_urbs(peasycap); |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 673 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 674 | JOM(4, "finished initialization\n"); |
| 675 | return 0; |
Mike Thomas | a985591 | 2011-01-10 18:41:11 +0000 | [diff] [blame] | 676 | } |
| 677 | /*****************************************************************************/ |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 678 | /*---------------------------------------------------------------------------*/ |
| 679 | /* |
| 680 | * SUBMIT ALL AUDIO URBS. |
| 681 | */ |
| 682 | /*---------------------------------------------------------------------------*/ |
| 683 | int |
| 684 | submit_audio_urbs(struct easycap *peasycap) |
| 685 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 686 | struct data_urb *pdata_urb; |
| 687 | struct urb *purb; |
| 688 | struct list_head *plist_head; |
| 689 | int j, isbad, nospc, m, rc; |
| 690 | int isbuf; |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 691 | |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 692 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 693 | SAY("ERROR: peasycap is NULL\n"); |
| 694 | return -EFAULT; |
| 695 | } |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 696 | if (!peasycap->purb_audio_head) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 697 | SAM("ERROR: peasycap->urb_audio_head uninitialized\n"); |
| 698 | return -EFAULT; |
| 699 | } |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 700 | if (!peasycap->pusb_device) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 701 | SAM("ERROR: peasycap->pusb_device is NULL\n"); |
| 702 | return -EFAULT; |
| 703 | } |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 704 | |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 705 | if (peasycap->audio_isoc_streaming) { |
| 706 | JOM(4, "already streaming audio urbs\n"); |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | JOM(4, "initial submission of all audio urbs\n"); |
| 711 | rc = usb_set_interface(peasycap->pusb_device, |
| 712 | peasycap->audio_interface, |
| 713 | peasycap->audio_altsetting_on); |
| 714 | JOM(8, "usb_set_interface(.,%i,%i) returned %i\n", |
| 715 | peasycap->audio_interface, |
| 716 | peasycap->audio_altsetting_on, rc); |
| 717 | |
| 718 | isbad = 0; |
| 719 | nospc = 0; |
| 720 | m = 0; |
| 721 | list_for_each(plist_head, peasycap->purb_audio_head) { |
| 722 | pdata_urb = list_entry(plist_head, struct data_urb, list_head); |
| 723 | if (pdata_urb && pdata_urb->purb) { |
| 724 | purb = pdata_urb->purb; |
| 725 | isbuf = pdata_urb->isbuf; |
| 726 | |
| 727 | purb->interval = 1; |
| 728 | purb->dev = peasycap->pusb_device; |
| 729 | purb->pipe = usb_rcvisocpipe(peasycap->pusb_device, |
| 730 | peasycap->audio_endpointnumber); |
| 731 | purb->transfer_flags = URB_ISO_ASAP; |
| 732 | purb->transfer_buffer = peasycap->audio_isoc_buffer[isbuf].pgo; |
| 733 | purb->transfer_buffer_length = peasycap->audio_isoc_buffer_size; |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 734 | purb->complete = easycap_alsa_complete; |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 735 | purb->context = peasycap; |
| 736 | purb->start_frame = 0; |
| 737 | purb->number_of_packets = peasycap->audio_isoc_framesperdesc; |
| 738 | for (j = 0; j < peasycap->audio_isoc_framesperdesc; j++) { |
| 739 | purb->iso_frame_desc[j].offset = j * peasycap->audio_isoc_maxframesize; |
| 740 | purb->iso_frame_desc[j].length = peasycap->audio_isoc_maxframesize; |
| 741 | } |
| 742 | |
| 743 | rc = usb_submit_urb(purb, GFP_KERNEL); |
| 744 | if (rc) { |
| 745 | isbad++; |
| 746 | SAM("ERROR: usb_submit_urb() failed" |
| 747 | " for urb with rc: -%s: %d\n", |
| 748 | strerror(rc), rc); |
| 749 | } else { |
| 750 | m++; |
| 751 | } |
| 752 | } else { |
| 753 | isbad++; |
| 754 | } |
| 755 | } |
| 756 | if (nospc) { |
| 757 | SAM("-ENOSPC=usb_submit_urb() for %i urbs\n", nospc); |
| 758 | SAM("..... possibly inadequate USB bandwidth\n"); |
| 759 | peasycap->audio_eof = 1; |
| 760 | } |
| 761 | if (isbad) { |
| 762 | JOM(4, "attempting cleanup instead of submitting\n"); |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 763 | list_for_each(plist_head, (peasycap->purb_audio_head)) { |
| 764 | pdata_urb = list_entry(plist_head, struct data_urb, list_head); |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 765 | if (pdata_urb && pdata_urb->purb) |
| 766 | usb_kill_urb(pdata_urb->purb); |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 767 | } |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 768 | peasycap->audio_isoc_streaming = 0; |
| 769 | } else { |
| 770 | peasycap->audio_isoc_streaming = m; |
| 771 | JOM(4, "submitted %i audio urbs\n", m); |
| 772 | } |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 773 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 774 | return 0; |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 775 | } |
| 776 | /*****************************************************************************/ |
| 777 | /*---------------------------------------------------------------------------*/ |
| 778 | /* |
| 779 | * KILL ALL AUDIO URBS. |
| 780 | */ |
| 781 | /*---------------------------------------------------------------------------*/ |
| 782 | int |
| 783 | kill_audio_urbs(struct easycap *peasycap) |
| 784 | { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 785 | int m; |
| 786 | struct list_head *plist_head; |
| 787 | struct data_urb *pdata_urb; |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 788 | |
Tomas Winkler | 6888393 | 2011-03-03 00:10:51 +0200 | [diff] [blame] | 789 | if (!peasycap) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 790 | SAY("ERROR: peasycap is NULL\n"); |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 791 | return -EFAULT; |
| 792 | } |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 793 | |
| 794 | if (!peasycap->audio_isoc_streaming) { |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 795 | JOM(8, "%i=audio_isoc_streaming, no audio urbs killed\n", |
| 796 | peasycap->audio_isoc_streaming); |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 797 | return 0; |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 798 | } |
Tomas Winkler | 5917def | 2011-03-06 10:59:03 +0200 | [diff] [blame] | 799 | |
| 800 | if (!peasycap->purb_audio_head) { |
| 801 | SAM("ERROR: peasycap->purb_audio_head is NULL\n"); |
| 802 | return -EFAULT; |
| 803 | } |
| 804 | |
| 805 | peasycap->audio_isoc_streaming = 0; |
| 806 | JOM(4, "killing audio urbs\n"); |
| 807 | m = 0; |
| 808 | list_for_each(plist_head, (peasycap->purb_audio_head)) { |
| 809 | pdata_urb = list_entry(plist_head, struct data_urb, list_head); |
| 810 | if (pdata_urb && pdata_urb->purb) { |
| 811 | usb_kill_urb(pdata_urb->purb); |
| 812 | m++; |
| 813 | } |
| 814 | } |
| 815 | JOM(4, "%i audio urbs killed\n", m); |
| 816 | |
Tomas Winkler | a75af07 | 2011-02-28 19:27:07 +0200 | [diff] [blame] | 817 | return 0; |
R.M. Thomas | 702422b | 2010-06-18 12:29:49 -0700 | [diff] [blame] | 818 | } |
| 819 | /*****************************************************************************/ |