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