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