blob: 213d0400b3ee518bbd064937b4bb1d3f03e69257 [file] [log] [blame]
R.M. Thomas702422b2010-06-18 12:29:49 -07001/******************************************************************************
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. Thomas702422b2010-06-18 12:29:49 -070032
Mike Thomasa9855912011-01-10 18:41:11 +000033/*--------------------------------------------------------------------------*/
34/*
35 * PARAMETERS USED WHEN REGISTERING THE AUDIO INTERFACE
36 */
37/*--------------------------------------------------------------------------*/
38static 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 Winklera75af072011-02-28 19:27:07 +020049 .buffer_bytes_max = PAGE_SIZE *
50 PAGES_PER_AUDIO_FRAGMENT *
51 AUDIO_FRAGMENT_MANY,
Mike Thomasa9855912011-01-10 18:41:11 +000052 .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 Thomasa9855912011-01-10 18:41:11 +000058
59/*****************************************************************************/
60/*---------------------------------------------------------------------------*/
61/*
Mike Thomasa9855912011-01-10 18:41:11 +000062 * 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/*---------------------------------------------------------------------------*/
67void
68easycap_alsa_complete(struct urb *purb)
69{
Tomas Winklera75af072011-02-28 19:27:07 +020070 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 Winkler3fc0dae2011-02-03 13:42:40 +020078#ifdef UPSAMPLE
Tomas Winklera75af072011-02-28 19:27:07 +020079 int k;
80 s16 oldaudio, newaudio, delta;
Mike Thomasa9855912011-01-10 18:41:11 +000081#endif /*UPSAMPLE*/
82
Tomas Winklera75af072011-02-28 19:27:07 +020083 JOT(16, "\n");
Mike Thomasa9855912011-01-10 18:41:11 +000084
Tomas Winkler68883932011-03-03 00:10:51 +020085 if (!purb) {
Tomas Winklera75af072011-02-28 19:27:07 +020086 SAY("ERROR: purb is NULL\n");
Mike Thomasa9855912011-01-10 18:41:11 +000087 return;
88 }
Tomas Winklera75af072011-02-28 19:27:07 +020089 peasycap = purb->context;
Tomas Winkler68883932011-03-03 00:10:51 +020090 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +020091 SAY("ERROR: peasycap is NULL\n");
92 return;
93 }
Tomas Winklera75af072011-02-28 19:27:07 +020094 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 Winkler68883932011-03-03 00:10:51 +0200103 if (!pss)
Tomas Winklera75af072011-02-28 19:27:07 +0200104 goto resubmit;
105 prt = pss->runtime;
Tomas Winkler68883932011-03-03 00:10:51 +0200106 if (!prt)
Tomas Winklera75af072011-02-28 19:27:07 +0200107 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 Thomasa9855912011-01-10 18:41:11 +0000124/*---------------------------------------------------------------------------*/
125/*
126 * PROCEED HERE WHEN NO ERROR
127 */
128/*---------------------------------------------------------------------------*/
129
Tomas Winkler3fc0dae2011-02-03 13:42:40 +0200130#ifdef UPSAMPLE
Tomas Winklera75af072011-02-28 19:27:07 +0200131 oldaudio = peasycap->oldaudio;
Mike Thomasa9855912011-01-10 18:41:11 +0000132#endif /*UPSAMPLE*/
133
Tomas Winklera75af072011-02-28 19:27:07 +0200134 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 Winkler5917def2011-03-06 10:59:03 +0200140 if (purb->iso_frame_desc[i].status) {
Tomas Winklera75af072011-02-28 19:27:07 +0200141 JOM(12, "discarding audio samples because "
142 "%i=purb->iso_frame_desc[i].status\n",
143 purb->iso_frame_desc[i].status);
Tomas Winkler5917def2011-03-06 10:59:03 +0200144 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 Thomasa9855912011-01-10 18:41:11 +0000252 }
Mike Thomasa9855912011-01-10 18:41:11 +0000253
Tomas Winkler3fc0dae2011-02-03 13:42:40 +0200254#ifdef UPSAMPLE
Tomas Winklera75af072011-02-28 19:27:07 +0200255 peasycap->oldaudio = oldaudio;
Mike Thomasa9855912011-01-10 18:41:11 +0000256#endif /*UPSAMPLE*/
257
Tomas Winklera75af072011-02-28 19:27:07 +0200258 }
Mike Thomasa9855912011-01-10 18:41:11 +0000259/*---------------------------------------------------------------------------*/
260/*
261 * RESUBMIT THIS URB
262 */
263/*---------------------------------------------------------------------------*/
264resubmit:
Tomas Winkler5917def2011-03-06 10:59:03 +0200265 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 Thomasa9855912011-01-10 18:41:11 +0000274 }
Tomas Winkler5917def2011-03-06 10:59:03 +0200275 if (0 < peasycap->audio_isoc_streaming)
276 peasycap->audio_isoc_streaming--;
Mike Thomasa9855912011-01-10 18:41:11 +0000277 }
Tomas Winklera75af072011-02-28 19:27:07 +0200278 return;
Mike Thomasa9855912011-01-10 18:41:11 +0000279}
280/*****************************************************************************/
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200281static int easycap_alsa_open(struct snd_pcm_substream *pss)
Mike Thomasa9855912011-01-10 18:41:11 +0000282{
Tomas Winklera75af072011-02-28 19:27:07 +0200283 struct snd_pcm *psnd_pcm;
284 struct snd_card *psnd_card;
285 struct easycap *peasycap;
Mike Thomasa9855912011-01-10 18:41:11 +0000286
Tomas Winklera75af072011-02-28 19:27:07 +0200287 JOT(4, "\n");
Tomas Winkler68883932011-03-03 00:10:51 +0200288 if (!pss) {
Tomas Winklera75af072011-02-28 19:27:07 +0200289 SAY("ERROR: pss is NULL\n");
290 return -EFAULT;
291 }
292 psnd_pcm = pss->pcm;
Tomas Winkler68883932011-03-03 00:10:51 +0200293 if (!psnd_pcm) {
Tomas Winklera75af072011-02-28 19:27:07 +0200294 SAY("ERROR: psnd_pcm is NULL\n");
295 return -EFAULT;
296 }
297 psnd_card = psnd_pcm->card;
Tomas Winkler68883932011-03-03 00:10:51 +0200298 if (!psnd_card) {
Tomas Winklera75af072011-02-28 19:27:07 +0200299 SAY("ERROR: psnd_card is NULL\n");
300 return -EFAULT;
301 }
Mike Thomasa9855912011-01-10 18:41:11 +0000302
Tomas Winklera75af072011-02-28 19:27:07 +0200303 peasycap = psnd_card->private_data;
Tomas Winkler68883932011-03-03 00:10:51 +0200304 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200305 SAY("ERROR: peasycap is NULL\n");
306 return -EFAULT;
307 }
Tomas Winklera75af072011-02-28 19:27:07 +0200308 if (peasycap->psnd_card != psnd_card) {
309 SAM("ERROR: bad peasycap->psnd_card\n");
310 return -EFAULT;
311 }
Tomas Winkler68883932011-03-03 00:10:51 +0200312 if (peasycap->psubstream) {
Tomas Winklera75af072011-02-28 19:27:07 +0200313 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 Thomasa9855912011-01-10 18:41:11 +0000321
Tomas Winklera75af072011-02-28 19:27:07 +0200322 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 Thomasa9855912011-01-10 18:41:11 +0000328}
329/*****************************************************************************/
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200330static int easycap_alsa_close(struct snd_pcm_substream *pss)
Mike Thomasa9855912011-01-10 18:41:11 +0000331{
Tomas Winklera75af072011-02-28 19:27:07 +0200332 struct easycap *peasycap;
Mike Thomasa9855912011-01-10 18:41:11 +0000333
Tomas Winklera75af072011-02-28 19:27:07 +0200334 JOT(4, "\n");
Tomas Winkler68883932011-03-03 00:10:51 +0200335 if (!pss) {
Tomas Winklera75af072011-02-28 19:27:07 +0200336 SAY("ERROR: pss is NULL\n");
337 return -EFAULT;
338 }
339 peasycap = snd_pcm_substream_chip(pss);
Tomas Winkler68883932011-03-03 00:10:51 +0200340 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200341 SAY("ERROR: peasycap is NULL\n");
342 return -EFAULT;
343 }
Tomas Winklera75af072011-02-28 19:27:07 +0200344 pss->private_data = NULL;
345 peasycap->psubstream = NULL;
346 JOT(4, "ending successfully\n");
347 return 0;
Mike Thomasa9855912011-01-10 18:41:11 +0000348}
349/*****************************************************************************/
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200350static int easycap_alsa_vmalloc(struct snd_pcm_substream *pss, size_t sz)
Mike Thomasa9855912011-01-10 18:41:11 +0000351{
Tomas Winklera75af072011-02-28 19:27:07 +0200352 struct snd_pcm_runtime *prt;
353 JOT(4, "\n");
Mike Thomasa9855912011-01-10 18:41:11 +0000354
Tomas Winkler68883932011-03-03 00:10:51 +0200355 if (!pss) {
Tomas Winklera75af072011-02-28 19:27:07 +0200356 SAY("ERROR: pss is NULL\n");
357 return -EFAULT;
358 }
359 prt = pss->runtime;
Tomas Winkler68883932011-03-03 00:10:51 +0200360 if (!prt) {
Tomas Winklera75af072011-02-28 19:27:07 +0200361 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 Winkler68883932011-03-03 00:10:51 +0200370 if (!prt->dma_area)
Tomas Winklera75af072011-02-28 19:27:07 +0200371 return -ENOMEM;
372 prt->dma_bytes = sz;
373 return 0;
Mike Thomasa9855912011-01-10 18:41:11 +0000374}
375/*****************************************************************************/
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200376static int easycap_alsa_hw_params(struct snd_pcm_substream *pss,
Tomas Winklera75af072011-02-28 19:27:07 +0200377 struct snd_pcm_hw_params *phw)
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200378{
Tomas Winklera75af072011-02-28 19:27:07 +0200379 int rc;
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200380
Tomas Winklera75af072011-02-28 19:27:07 +0200381 JOT(4, "%i\n", (params_buffer_bytes(phw)));
Tomas Winkler68883932011-03-03 00:10:51 +0200382 if (!pss) {
Tomas Winklera75af072011-02-28 19:27:07 +0200383 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 Winkler68e4cca2011-01-24 17:08:22 +0200390}
391/*****************************************************************************/
392static int easycap_alsa_hw_free(struct snd_pcm_substream *pss)
Mike Thomasa9855912011-01-10 18:41:11 +0000393{
Tomas Winklera75af072011-02-28 19:27:07 +0200394 struct snd_pcm_runtime *prt;
395 JOT(4, "\n");
Mike Thomasa9855912011-01-10 18:41:11 +0000396
Tomas Winkler68883932011-03-03 00:10:51 +0200397 if (!pss) {
Tomas Winklera75af072011-02-28 19:27:07 +0200398 SAY("ERROR: pss is NULL\n");
399 return -EFAULT;
400 }
401 prt = pss->runtime;
Tomas Winkler68883932011-03-03 00:10:51 +0200402 if (!prt) {
Tomas Winklera75af072011-02-28 19:27:07 +0200403 SAY("ERROR: substream.runtime is NULL\n");
404 return -EFAULT;
405 }
Tomas Winkler68883932011-03-03 00:10:51 +0200406 if (prt->dma_area) {
Tomas Winklera75af072011-02-28 19:27:07 +0200407 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 Thomasa9855912011-01-10 18:41:11 +0000413}
414/*****************************************************************************/
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200415static int easycap_alsa_prepare(struct snd_pcm_substream *pss)
Mike Thomasa9855912011-01-10 18:41:11 +0000416{
Tomas Winklera75af072011-02-28 19:27:07 +0200417 struct easycap *peasycap;
418 struct snd_pcm_runtime *prt;
Mike Thomasa9855912011-01-10 18:41:11 +0000419
Tomas Winklera75af072011-02-28 19:27:07 +0200420 JOT(4, "\n");
Tomas Winkler68883932011-03-03 00:10:51 +0200421 if (!pss) {
Tomas Winklera75af072011-02-28 19:27:07 +0200422 SAY("ERROR: pss is NULL\n");
423 return -EFAULT;
424 }
425 prt = pss->runtime;
426 peasycap = snd_pcm_substream_chip(pss);
Tomas Winkler68883932011-03-03 00:10:51 +0200427 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200428 SAY("ERROR: peasycap is NULL\n");
429 return -EFAULT;
430 }
Mike Thomasa9855912011-01-10 18:41:11 +0000431
Tomas Winklera75af072011-02-28 19:27:07 +0200432 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 Thomasa9855912011-01-10 18:41:11 +0000451}
452/*****************************************************************************/
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200453static int easycap_alsa_ack(struct snd_pcm_substream *pss)
Mike Thomasa9855912011-01-10 18:41:11 +0000454{
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200455 return 0;
Mike Thomasa9855912011-01-10 18:41:11 +0000456}
457/*****************************************************************************/
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200458static int easycap_alsa_trigger(struct snd_pcm_substream *pss, int cmd)
Mike Thomasa9855912011-01-10 18:41:11 +0000459{
Tomas Winklera75af072011-02-28 19:27:07 +0200460 struct easycap *peasycap;
461 int retval;
Mike Thomasa9855912011-01-10 18:41:11 +0000462
Tomas Winklera75af072011-02-28 19:27:07 +0200463 JOT(4, "%i=cmd cf %i=START %i=STOP\n", cmd, SNDRV_PCM_TRIGGER_START,
464 SNDRV_PCM_TRIGGER_STOP);
Tomas Winkler68883932011-03-03 00:10:51 +0200465 if (!pss) {
Tomas Winklera75af072011-02-28 19:27:07 +0200466 SAY("ERROR: pss is NULL\n");
467 return -EFAULT;
468 }
469 peasycap = snd_pcm_substream_chip(pss);
Tomas Winkler68883932011-03-03 00:10:51 +0200470 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200471 SAY("ERROR: peasycap is NULL\n");
472 return -EFAULT;
473 }
Tomas Winklera75af072011-02-28 19:27:07 +0200474 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 Thomasa9855912011-01-10 18:41:11 +0000487}
488/*****************************************************************************/
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200489static snd_pcm_uframes_t easycap_alsa_pointer(struct snd_pcm_substream *pss)
Mike Thomasa9855912011-01-10 18:41:11 +0000490{
Tomas Winklera75af072011-02-28 19:27:07 +0200491 struct easycap *peasycap;
492 snd_pcm_uframes_t offset;
Mike Thomasa9855912011-01-10 18:41:11 +0000493
Tomas Winklera75af072011-02-28 19:27:07 +0200494 JOT(16, "\n");
Tomas Winkler68883932011-03-03 00:10:51 +0200495 if (!pss) {
Tomas Winklera75af072011-02-28 19:27:07 +0200496 SAY("ERROR: pss is NULL\n");
497 return -EFAULT;
498 }
499 peasycap = snd_pcm_substream_chip(pss);
Tomas Winkler68883932011-03-03 00:10:51 +0200500 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200501 SAY("ERROR: peasycap is NULL\n");
502 return -EFAULT;
503 }
Tomas Winklera75af072011-02-28 19:27:07 +0200504 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 Thomasa9855912011-01-10 18:41:11 +0000510/*---------------------------------------------------------------------------*/
Tomas Winklera75af072011-02-28 19:27:07 +0200511 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 Thomasa9855912011-01-10 18:41:11 +0000522}
523/*****************************************************************************/
Tomas Winklera75af072011-02-28 19:27:07 +0200524static struct page *
525easycap_alsa_page(struct snd_pcm_substream *pss, unsigned long offset)
Mike Thomasa9855912011-01-10 18:41:11 +0000526{
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200527 return vmalloc_to_page(pss->runtime->dma_area + offset);
Mike Thomasa9855912011-01-10 18:41:11 +0000528}
529/*****************************************************************************/
530
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200531static 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/*---------------------------------------------------------------------------*/
551int easycap_alsa_probe(struct easycap *peasycap)
552{
Tomas Winklera75af072011-02-28 19:27:07 +0200553 int rc;
554 struct snd_card *psnd_card;
555 struct snd_pcm *psnd_pcm;
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200556
Tomas Winkler68883932011-03-03 00:10:51 +0200557 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200558 SAY("ERROR: peasycap is NULL\n");
559 return -ENODEV;
560 }
Tomas Winklera75af072011-02-28 19:27:07 +0200561 if (0 > peasycap->minor) {
562 SAY("ERROR: no minor\n");
563 return -ENODEV;
564 }
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200565
Tomas Winklera75af072011-02-28 19:27:07 +0200566 peasycap->alsa_hardware = alsa_hardware;
Tomas Winkler27d683a2011-03-03 00:10:49 +0200567 if (peasycap->microphone) {
Tomas Winklera75af072011-02-28 19:27:07 +0200568 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 Winkler68e4cca2011-01-24 17:08:22 +0200576
577 if (0 != snd_card_create(SNDRV_DEFAULT_IDX1, "easycap_alsa",
Tomas Winklera75af072011-02-28 19:27:07 +0200578 THIS_MODULE, 0, &psnd_card)) {
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200579 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 Winkler6911e7e2011-02-03 13:42:42 +0200593 if (rc) {
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200594 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 Winklera75af072011-02-28 19:27:07 +0200600 &easycap_alsa_pcm_ops);
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200601 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 Winkler3c1fb662011-02-03 13:42:37 +0200605 peasycap->psubstream = NULL;
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200606
607 rc = snd_card_register(psnd_card);
Tomas Winkler6911e7e2011-02-03 13:42:42 +0200608 if (rc) {
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200609 SAM("ERROR: Cannot do ALSA snd_card_register()\n");
610 snd_card_free(psnd_card);
611 return -EFAULT;
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200612 }
Tomas Winkler5917def2011-03-06 10:59:03 +0200613
614 SAM("registered %s\n", &psnd_card->id[0]);
Tomas Winklera75af072011-02-28 19:27:07 +0200615 return 0;
Tomas Winkler68e4cca2011-01-24 17:08:22 +0200616}
Mike Thomasa9855912011-01-10 18:41:11 +0000617
618/*****************************************************************************/
619/*****************************************************************************/
620/*****************************************************************************/
621/*****************************************************************************/
622/*****************************************************************************/
623/*****************************************************************************/
624/*---------------------------------------------------------------------------*/
625/*
626 * COMMON AUDIO INITIALIZATION
627 */
628/*---------------------------------------------------------------------------*/
629int
630easycap_sound_setup(struct easycap *peasycap)
631{
Tomas Winklera75af072011-02-28 19:27:07 +0200632 int rc;
Mike Thomasa9855912011-01-10 18:41:11 +0000633
Tomas Winklera75af072011-02-28 19:27:07 +0200634 JOM(4, "starting initialization\n");
Mike Thomasa9855912011-01-10 18:41:11 +0000635
Tomas Winkler68883932011-03-03 00:10:51 +0200636 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200637 SAY("ERROR: peasycap is NULL.\n");
638 return -EFAULT;
639 }
Tomas Winkler68883932011-03-03 00:10:51 +0200640 if (!peasycap->pusb_device) {
Tomas Winklera75af072011-02-28 19:27:07 +0200641 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 Thomasa9855912011-01-10 18:41:11 +0000645
Tomas Winklera75af072011-02-28 19:27:07 +0200646 rc = audio_setup(peasycap);
647 JOM(8, "audio_setup() returned %i\n", rc);
Mike Thomasa9855912011-01-10 18:41:11 +0000648
Tomas Winkler68883932011-03-03 00:10:51 +0200649 if (!peasycap->pusb_device) {
Tomas Winklera75af072011-02-28 19:27:07 +0200650 SAM("ERROR: peasycap->pusb_device has become NULL\n");
651 return -ENODEV;
652 }
Mike Thomasa9855912011-01-10 18:41:11 +0000653/*---------------------------------------------------------------------------*/
Tomas Winkler68883932011-03-03 00:10:51 +0200654 if (!peasycap->pusb_device) {
Tomas Winklera75af072011-02-28 19:27:07 +0200655 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 Thomasa9855912011-01-10 18:41:11 +0000662
Tomas Winklera75af072011-02-28 19:27:07 +0200663 rc = wakeup_device(peasycap->pusb_device);
664 JOM(8, "wakeup_device() returned %i\n", rc);
Mike Thomasa9855912011-01-10 18:41:11 +0000665
Tomas Winklera75af072011-02-28 19:27:07 +0200666 peasycap->audio_eof = 0;
667 peasycap->audio_idle = 0;
Mike Thomasa9855912011-01-10 18:41:11 +0000668
Tomas Winklera75af072011-02-28 19:27:07 +0200669 peasycap->timeval1.tv_sec = 0;
670 peasycap->timeval1.tv_usec = 0;
Mike Thomasa9855912011-01-10 18:41:11 +0000671
Tomas Winklera75af072011-02-28 19:27:07 +0200672 submit_audio_urbs(peasycap);
Mike Thomasa9855912011-01-10 18:41:11 +0000673
Tomas Winklera75af072011-02-28 19:27:07 +0200674 JOM(4, "finished initialization\n");
675 return 0;
Mike Thomasa9855912011-01-10 18:41:11 +0000676}
677/*****************************************************************************/
R.M. Thomas702422b2010-06-18 12:29:49 -0700678/*---------------------------------------------------------------------------*/
679/*
680 * SUBMIT ALL AUDIO URBS.
681 */
682/*---------------------------------------------------------------------------*/
683int
684submit_audio_urbs(struct easycap *peasycap)
685{
Tomas Winklera75af072011-02-28 19:27:07 +0200686 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. Thomas702422b2010-06-18 12:29:49 -0700691
Tomas Winkler68883932011-03-03 00:10:51 +0200692 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200693 SAY("ERROR: peasycap is NULL\n");
694 return -EFAULT;
695 }
Tomas Winkler68883932011-03-03 00:10:51 +0200696 if (!peasycap->purb_audio_head) {
Tomas Winklera75af072011-02-28 19:27:07 +0200697 SAM("ERROR: peasycap->urb_audio_head uninitialized\n");
698 return -EFAULT;
699 }
Tomas Winkler68883932011-03-03 00:10:51 +0200700 if (!peasycap->pusb_device) {
Tomas Winklera75af072011-02-28 19:27:07 +0200701 SAM("ERROR: peasycap->pusb_device is NULL\n");
702 return -EFAULT;
703 }
R.M. Thomas702422b2010-06-18 12:29:49 -0700704
Tomas Winkler5917def2011-03-06 10:59:03 +0200705 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 Winkler5917def2011-03-06 10:59:03 +0200734 purb->complete = easycap_alsa_complete;
Tomas Winkler5917def2011-03-06 10:59:03 +0200735 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 Winklera75af072011-02-28 19:27:07 +0200763 list_for_each(plist_head, (peasycap->purb_audio_head)) {
764 pdata_urb = list_entry(plist_head, struct data_urb, list_head);
Tomas Winkler5917def2011-03-06 10:59:03 +0200765 if (pdata_urb && pdata_urb->purb)
766 usb_kill_urb(pdata_urb->purb);
R.M. Thomas702422b2010-06-18 12:29:49 -0700767 }
Tomas Winkler5917def2011-03-06 10:59:03 +0200768 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. Thomas702422b2010-06-18 12:29:49 -0700773
Tomas Winklera75af072011-02-28 19:27:07 +0200774 return 0;
R.M. Thomas702422b2010-06-18 12:29:49 -0700775}
776/*****************************************************************************/
777/*---------------------------------------------------------------------------*/
778/*
779 * KILL ALL AUDIO URBS.
780 */
781/*---------------------------------------------------------------------------*/
782int
783kill_audio_urbs(struct easycap *peasycap)
784{
Tomas Winklera75af072011-02-28 19:27:07 +0200785 int m;
786 struct list_head *plist_head;
787 struct data_urb *pdata_urb;
R.M. Thomas702422b2010-06-18 12:29:49 -0700788
Tomas Winkler68883932011-03-03 00:10:51 +0200789 if (!peasycap) {
Tomas Winklera75af072011-02-28 19:27:07 +0200790 SAY("ERROR: peasycap is NULL\n");
R.M. Thomas702422b2010-06-18 12:29:49 -0700791 return -EFAULT;
792 }
Tomas Winkler5917def2011-03-06 10:59:03 +0200793
794 if (!peasycap->audio_isoc_streaming) {
Tomas Winklera75af072011-02-28 19:27:07 +0200795 JOM(8, "%i=audio_isoc_streaming, no audio urbs killed\n",
796 peasycap->audio_isoc_streaming);
Tomas Winkler5917def2011-03-06 10:59:03 +0200797 return 0;
Tomas Winklera75af072011-02-28 19:27:07 +0200798 }
Tomas Winkler5917def2011-03-06 10:59:03 +0200799
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 Winklera75af072011-02-28 19:27:07 +0200817 return 0;
R.M. Thomas702422b2010-06-18 12:29:49 -0700818}
819/*****************************************************************************/