1 /******************************************************************************
5 * Audio driver for EasyCAP USB2.0 Video Capture Device DC60 *
8 ******************************************************************************/
11 * Copyright (C) 2010 R.M. Thomas <rmthomas@sciolus.org>
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.
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.
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
29 /*****************************************************************************/
32 #include "easycap_debug.h"
33 #include "easycap_sound.h"
35 /*****************************************************************************/
36 /*---------------------------------------------------------------------------*/
38 * ON COMPLETION OF AN AUDIO URB ITS DATA IS COPIED TO THE AUDIO BUFFERS
39 * PROVIDED peasycap->audio_idle IS ZERO. REGARDLESS OF THIS BEING TRUE,
40 * IT IS RESUBMITTED PROVIDED peasycap->audio_isoc_streaming IS NOT ZERO.
42 /*---------------------------------------------------------------------------*/
44 easysnd_complete(struct urb *purb)
46 struct easycap *peasycap;
47 struct data_buffer *paudio_buffer;
50 int i, j, more, much, leap, rc;
53 __s16 oldaudio, newaudio, delta;
59 SAY("ERROR: purb is NULL\n");
62 peasycap = purb->context;
63 if (NULL == peasycap) {
64 SAY("ERROR: peasycap is NULL\n");
67 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
68 SAY("ERROR: bad peasycap\n");
74 if (peasycap->audio_idle) {
75 JOM(16, "%i=audio_idle %i=audio_isoc_streaming\n", \
76 peasycap->audio_idle, peasycap->audio_isoc_streaming);
77 if (peasycap->audio_isoc_streaming) {
78 rc = usb_submit_urb(purb, GFP_ATOMIC);
81 SAM("ERROR: while %i=audio_idle, " \
82 "usb_submit_urb() failed with rc:\n", \
83 peasycap->audio_idle);
121 SAM("unknown error: 0x%08X\n", rc);
129 /*---------------------------------------------------------------------------*/
131 if ((-ESHUTDOWN == purb->status) || (-ENOENT == purb->status)) {
132 JOM(16, "urb status -ESHUTDOWN or -ENOENT\n");
135 SAM("ERROR: non-zero urb status:\n");
136 switch (purb->status) {
138 SAM("-EINPROGRESS\n");
170 SAM("-EOPNOTSUPP\n");
173 case -EPFNOSUPPORT: {
174 SAM("-EPFNOSUPPORT\n");
177 case -EAFNOSUPPORT: {
178 SAM("-EAFNOSUPPORT\n");
182 SAM("-EADDRINUSE\n");
185 case -EADDRNOTAVAIL: {
186 SAM("-EADDRNOTAVAIL\n");
210 SAM("-ECONNRESET\n");
218 SAM("unknown error code 0x%08X\n", purb->status);
222 /*---------------------------------------------------------------------------*/
224 * RESUBMIT THIS URB AFTER AN ERROR
226 * (THIS IS DUPLICATE CODE TO REDUCE INDENTATION OF THE NO-ERROR PATH)
228 /*---------------------------------------------------------------------------*/
229 if (peasycap->audio_isoc_streaming) {
230 rc = usb_submit_urb(purb, GFP_ATOMIC);
232 SAM("ERROR: while %i=audio_idle, usb_submit_urb() "
233 "failed with rc:\n", peasycap->audio_idle);
268 SAM("0x%08X\n", rc); break;
275 /*---------------------------------------------------------------------------*/
277 * PROCEED HERE WHEN NO ERROR
279 /*---------------------------------------------------------------------------*/
280 #if defined(UPSAMPLE)
281 oldaudio = peasycap->oldaudio;
284 for (i = 0; i < purb->number_of_packets; i++) {
285 switch (purb->iso_frame_desc[i].status) {
294 SAM("-EINPROGRESS\n");
346 SAM("-ECONNRESET\n");
358 SAM("unknown error:0x%08X\n", purb->iso_frame_desc[i].status);
362 if (!purb->iso_frame_desc[i].status) {
363 more = purb->iso_frame_desc[i].actual_length;
365 #if defined(TESTTONE)
367 more = purb->iso_frame_desc[i].length;
371 peasycap->audio_mt++;
373 if (peasycap->audio_mt) {
374 JOM(16, "%4i empty audio urb frames\n", \
376 peasycap->audio_mt = 0;
379 p1 = (__u8 *)(purb->transfer_buffer + \
380 purb->iso_frame_desc[i].offset);
385 /*---------------------------------------------------------------------------*/
387 * COPY more BYTES FROM ISOC BUFFER TO AUDIO BUFFER,
388 * CONVERTING 8-BIT MONO TO 16-BIT SIGNED LITTLE-ENDIAN SAMPLES IF NECESSARY
390 /*---------------------------------------------------------------------------*/
393 SAM("easysnd_complete: MISTAKE: " \
394 "more is negative\n");
397 if (peasycap->audio_buffer_page_many <= \
398 peasycap->audio_fill) {
400 "peasycap->audio_fill\n");
404 paudio_buffer = &peasycap->audio_buffer\
405 [peasycap->audio_fill];
406 if (PAGE_SIZE < (paudio_buffer->pto - \
407 paudio_buffer->pgo)) {
408 SAM("ERROR: bad paudio_buffer->pto\n");
411 if (PAGE_SIZE == (paudio_buffer->pto - \
412 paudio_buffer->pgo)) {
414 #if defined(TESTTONE)
415 easysnd_testtone(peasycap, \
416 peasycap->audio_fill);
419 paudio_buffer->pto = \
421 (peasycap->audio_fill)++;
423 audio_buffer_page_many <= \
424 peasycap->audio_fill)
425 peasycap->audio_fill = 0;
427 JOM(12, "bumped peasycap->" \
428 "audio_fill to %i\n", \
429 peasycap->audio_fill);
431 paudio_buffer = &peasycap->\
433 [peasycap->audio_fill];
434 paudio_buffer->pto = \
437 if (!(peasycap->audio_fill % \
439 audio_pages_per_fragment)) {
440 JOM(12, "wakeup call on wq_" \
441 "audio, %i=frag reading %i" \
442 "=fragment fill\n", \
443 (peasycap->audio_read / \
445 audio_pages_per_fragment), \
446 (peasycap->audio_fill / \
448 audio_pages_per_fragment));
449 wake_up_interruptible\
450 (&(peasycap->wq_audio));
454 much = PAGE_SIZE - (int)(paudio_buffer->pto -\
457 if (false == peasycap->microphone) {
461 memcpy(paudio_buffer->pto, p1, much);
465 #if defined(UPSAMPLE)
467 JOM(8, "MISTAKE? much" \
468 " is not divisible by 16\n");
473 p2 = (__u8 *)paudio_buffer->pto;
475 for (j = 0; j < (much/16); j++) {
476 newaudio = ((int) *p1) - 128;
480 delta = (newaudio - oldaudio) \
482 s16 = oldaudio + delta;
484 for (k = 0; k < 4; k++) {
485 *p2 = (0x00FF & s16);
486 *(p2 + 1) = (0xFF00 & \
489 *p2 = (0x00FF & s16);
490 *(p2 + 1) = (0xFF00 & \
501 if (much > (2 * more))
503 p2 = (__u8 *)paudio_buffer->pto;
505 for (j = 0; j < (much / 2); j++) {
506 s16 = ((int) *p1) - 128;
509 *p2 = (0x00FF & s16);
510 *(p2 + 1) = (0xFF00 & s16) >> \
517 (paudio_buffer->pto) += much;
521 JOM(12, "discarding audio samples because " \
522 "%i=purb->iso_frame_desc[i].status\n", \
523 purb->iso_frame_desc[i].status);
526 #if defined(UPSAMPLE)
527 peasycap->oldaudio = oldaudio;
531 /*---------------------------------------------------------------------------*/
533 * RESUBMIT THIS URB AFTER NO ERROR
535 /*---------------------------------------------------------------------------*/
536 if (peasycap->audio_isoc_streaming) {
537 rc = usb_submit_urb(purb, GFP_ATOMIC);
540 SAM("ERROR: while %i=audio_idle, " \
541 "usb_submit_urb() failed " \
542 "with rc:\n", peasycap->audio_idle);
581 SAM("unknown error: 0x%08X\n", rc);
589 /*****************************************************************************/
590 /*---------------------------------------------------------------------------*/
592 * THE AUDIO URBS ARE SUBMITTED AT THIS EARLY STAGE SO THAT IT IS POSSIBLE TO
593 * STREAM FROM /dev/easysnd1 WITH SIMPLE PROGRAMS SUCH AS cat WHICH DO NOT
594 * HAVE AN IOCTL INTERFACE.
596 /*---------------------------------------------------------------------------*/
598 easysnd_open(struct inode *inode, struct file *file)
600 struct usb_interface *pusb_interface;
601 struct easycap *peasycap;
603 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
604 #if defined(EASYCAP_IS_VIDEODEV_CLIENT)
605 #if defined(EASYCAP_NEEDS_V4L2_DEVICE_H)
606 struct v4l2_device *pv4l2_device;
607 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
608 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
609 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
613 subminor = iminor(inode);
615 pusb_interface = usb_find_interface(&easycap_usb_driver, subminor);
616 if (NULL == pusb_interface) {
617 SAY("ERROR: pusb_interface is NULL\n");
618 SAY("ending unsuccessfully\n");
621 peasycap = usb_get_intfdata(pusb_interface);
622 if (NULL == peasycap) {
623 SAY("ERROR: peasycap is NULL\n");
624 SAY("ending unsuccessfully\n");
627 /*---------------------------------------------------------------------------*/
628 #if (!defined(EASYCAP_IS_VIDEODEV_CLIENT))
630 /*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
632 #if defined(EASYCAP_NEEDS_V4L2_DEVICE_H)
633 /*---------------------------------------------------------------------------*/
635 * SOME VERSIONS OF THE videodev MODULE OVERWRITE THE DATA WHICH HAS
636 * BEEN WRITTEN BY THE CALL TO usb_set_intfdata() IN easycap_usb_probe(),
637 * REPLACING IT WITH A POINTER TO THE EMBEDDED v4l2_device STRUCTURE.
638 * TO DETECT THIS, THE STRING IN THE easycap.telltale[] BUFFER IS CHECKED.
640 /*---------------------------------------------------------------------------*/
641 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
642 pv4l2_device = usb_get_intfdata(pusb_interface);
643 if ((struct v4l2_device *)NULL == pv4l2_device) {
644 SAY("ERROR: pv4l2_device is NULL\n");
647 peasycap = (struct easycap *) \
648 container_of(pv4l2_device, struct easycap, v4l2_device);
650 #endif /*EASYCAP_NEEDS_V4L2_DEVICE_H*/
652 #endif /*EASYCAP_IS_VIDEODEV_CLIENT*/
653 /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
654 /*---------------------------------------------------------------------------*/
655 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
656 SAY("ERROR: bad peasycap: 0x%08lX\n", (unsigned long int) peasycap);
659 /*---------------------------------------------------------------------------*/
661 file->private_data = peasycap;
663 /*---------------------------------------------------------------------------*/
667 /*---------------------------------------------------------------------------*/
668 JOM(4, "starting initialization\n");
670 if ((struct usb_device *)NULL == peasycap->pusb_device) {
671 SAM("ERROR: peasycap->pusb_device is NULL\n");
674 JOM(16, "0x%08lX=peasycap->pusb_device\n", (long int)peasycap->pusb_device);
676 rc = audio_setup(peasycap);
678 JOM(8, "audio_setup() returned %i\n", rc);
680 JOM(8, "easysnd open(): ERROR: audio_setup() returned %i\n", rc);
682 if ((struct usb_device *)NULL == peasycap->pusb_device) {
683 SAM("ERROR: peasycap->pusb_device has become NULL\n");
686 /*---------------------------------------------------------------------------*/
687 if ((struct usb_device *)NULL == peasycap->pusb_device) {
688 SAM("ERROR: peasycap->pusb_device has become NULL\n");
691 rc = usb_set_interface(peasycap->pusb_device, peasycap->audio_interface, \
692 peasycap->audio_altsetting_on);
693 JOM(8, "usb_set_interface(.,%i,%i) returned %i\n", peasycap->audio_interface, \
694 peasycap->audio_altsetting_on, rc);
696 rc = wakeup_device(peasycap->pusb_device);
698 JOM(8, "wakeup_device() returned %i\n", rc);
700 JOM(8, "ERROR: wakeup_device() returned %i\n", rc);
702 peasycap->audio_eof = 0;
703 peasycap->audio_idle = 0;
705 peasycap->timeval1.tv_sec = 0;
706 peasycap->timeval1.tv_usec = 0;
708 submit_audio_urbs(peasycap);
710 JOM(4, "finished initialization\n");
713 /*****************************************************************************/
715 easysnd_release(struct inode *inode, struct file *file)
717 struct easycap *peasycap;
721 peasycap = file->private_data;
722 if (NULL == peasycap) {
723 SAY("ERROR: peasycap is NULL.\n");
726 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
727 SAY("ERROR: bad peasycap: 0x%08lX\n", (unsigned long int) peasycap);
730 if (0 != kill_audio_urbs(peasycap)) {
731 SAM("ERROR: kill_audio_urbs() failed\n");
734 JOM(4, "ending successfully\n");
737 /*****************************************************************************/
739 easysnd_read(struct file *file, char __user *puserspacebuffer, \
740 size_t kount, loff_t *poff)
742 struct timeval timeval;
743 long long int above, below, mean;
744 struct signed_div_result sdr;
746 long int kount1, more, rc, l0, lm;
748 struct easycap *peasycap;
749 struct data_buffer *pdata_buffer;
752 /*---------------------------------------------------------------------------*/
754 * DO A BLOCKING READ TO TRANSFER DATA TO USER SPACE.
756 ******************************************************************************
757 ***** N.B. IF THIS FUNCTION RETURNS 0, NOTHING IS SEEN IN USER SPACE. ******
758 ***** THIS CONDITION SIGNIFIES END-OF-FILE. ******
759 ******************************************************************************
761 /*---------------------------------------------------------------------------*/
763 JOT(8, "===== easysnd_read(): kount=%i, *poff=%i\n", (int)kount, (int)(*poff));
766 SAY("ERROR: file is NULL\n");
769 peasycap = file->private_data;
770 if (NULL == peasycap) {
771 SAY("ERROR in easysnd_read(): peasycap is NULL\n");
774 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
775 SAY("ERROR: bad peasycap: 0x%08lX\n", (unsigned long int) peasycap);
778 if (NULL == peasycap->pusb_device) {
779 SAY("ERROR in easysnd_read(): peasycap->pusb_device is NULL\n");
782 kd = isdongle(peasycap);
783 if (0 <= kd && DONGLE_MANY > kd) {
784 if (mutex_lock_interruptible(&(easycap_dongle[kd].mutex_audio))) {
785 SAY("ERROR: cannot lock easycap_dongle[%i].mutex_audio\n", kd);
788 JOM(4, "locked easycap_dongle[%i].mutex_audio\n", kd);
789 /*---------------------------------------------------------------------------*/
791 * MEANWHILE, easycap_usb_disconnect() MAY HAVE FREED POINTER peasycap,
792 * IN WHICH CASE A REPEAT CALL TO isdongle() WILL FAIL.
793 * IF NECESSARY, BAIL OUT.
795 /*---------------------------------------------------------------------------*/
796 if (kd != isdongle(peasycap))
799 SAY("ERROR: file is NULL\n");
800 mutex_unlock(&easycap_dongle[kd].mutex_audio);
803 peasycap = file->private_data;
804 if (NULL == peasycap) {
805 SAY("ERROR: peasycap is NULL\n");
806 mutex_unlock(&easycap_dongle[kd].mutex_audio);
809 if (memcmp(&peasycap->telltale[0], TELLTALE, strlen(TELLTALE))) {
810 SAY("ERROR: bad peasycap: 0x%08lX\n", \
811 (unsigned long int) peasycap);
812 mutex_unlock(&easycap_dongle[kd].mutex_audio);
815 if (NULL == peasycap->pusb_device) {
816 SAM("ERROR: peasycap->pusb_device is NULL\n");
817 mutex_unlock(&easycap_dongle[kd].mutex_audio);
821 /*---------------------------------------------------------------------------*/
823 * IF easycap_usb_disconnect() HAS ALREADY FREED POINTER peasycap BEFORE THE
824 * ATTEMPT TO ACQUIRE THE SEMAPHORE, isdongle() WILL HAVE FAILED. BAIL OUT.
826 /*---------------------------------------------------------------------------*/
829 /*---------------------------------------------------------------------------*/
830 if (file->f_flags & O_NONBLOCK)
831 JOT(16, "NONBLOCK kount=%i, *poff=%i\n", (int)kount, (int)(*poff));
833 JOT(8, "BLOCKING kount=%i, *poff=%i\n", (int)kount, (int)(*poff));
835 if ((0 > peasycap->audio_read) || \
836 (peasycap->audio_buffer_page_many <= peasycap->audio_read)) {
837 SAM("ERROR: peasycap->audio_read out of range\n");
838 mutex_unlock(&easycap_dongle[kd].mutex_audio);
841 pdata_buffer = &peasycap->audio_buffer[peasycap->audio_read];
842 if ((struct data_buffer *)NULL == pdata_buffer) {
843 SAM("ERROR: pdata_buffer is NULL\n");
844 mutex_unlock(&easycap_dongle[kd].mutex_audio);
847 JOM(12, "before wait, %i=frag read %i=frag fill\n", \
848 (peasycap->audio_read / peasycap->audio_pages_per_fragment), \
849 (peasycap->audio_fill / peasycap->audio_pages_per_fragment));
850 fragment = (peasycap->audio_read / peasycap->audio_pages_per_fragment);
851 while ((fragment == (peasycap->audio_fill / \
852 peasycap->audio_pages_per_fragment)) || \
853 (0 == (PAGE_SIZE - (pdata_buffer->pto - pdata_buffer->pgo)))) {
854 if (file->f_flags & O_NONBLOCK) {
855 JOM(16, "returning -EAGAIN as instructed\n");
856 mutex_unlock(&easycap_dongle[kd].mutex_audio);
859 rc = wait_event_interruptible(peasycap->wq_audio, \
860 (peasycap->audio_idle || peasycap->audio_eof || \
861 ((fragment != (peasycap->audio_fill / \
862 peasycap->audio_pages_per_fragment)) && \
863 (0 < (PAGE_SIZE - (pdata_buffer->pto - pdata_buffer->pgo))))));
865 SAM("aborted by signal\n");
866 mutex_unlock(&easycap_dongle[kd].mutex_audio);
869 if (peasycap->audio_eof) {
870 JOM(8, "returning 0 because %i=audio_eof\n", \
871 peasycap->audio_eof);
872 kill_audio_urbs(peasycap);
873 mutex_unlock(&easycap_dongle[kd].mutex_audio);
876 if (peasycap->audio_idle) {
877 JOM(16, "returning 0 because %i=audio_idle\n", \
878 peasycap->audio_idle);
879 mutex_unlock(&easycap_dongle[kd].mutex_audio);
882 if (!peasycap->audio_isoc_streaming) {
883 JOM(16, "returning 0 because audio urbs not streaming\n");
884 mutex_unlock(&easycap_dongle[kd].mutex_audio);
888 JOM(12, "after wait, %i=frag read %i=frag fill\n", \
889 (peasycap->audio_read / peasycap->audio_pages_per_fragment), \
890 (peasycap->audio_fill / peasycap->audio_pages_per_fragment));
892 while (fragment == (peasycap->audio_read / \
893 peasycap->audio_pages_per_fragment)) {
894 if (NULL == pdata_buffer->pgo) {
895 SAM("ERROR: pdata_buffer->pgo is NULL\n");
896 mutex_unlock(&easycap_dongle[kd].mutex_audio);
899 if (NULL == pdata_buffer->pto) {
900 SAM("ERROR: pdata_buffer->pto is NULL\n");
901 mutex_unlock(&easycap_dongle[kd].mutex_audio);
904 kount1 = PAGE_SIZE - (pdata_buffer->pto - pdata_buffer->pgo);
906 SAM("easysnd_read: MISTAKE: kount1 is negative\n");
907 mutex_unlock(&easycap_dongle[kd].mutex_audio);
911 (peasycap->audio_read)++;
912 if (peasycap->audio_buffer_page_many <= peasycap->audio_read)
913 peasycap->audio_read = 0;
914 JOM(12, "bumped peasycap->audio_read to %i\n", \
915 peasycap->audio_read);
917 if (fragment != (peasycap->audio_read / \
918 peasycap->audio_pages_per_fragment))
921 if ((0 > peasycap->audio_read) || \
922 (peasycap->audio_buffer_page_many <= \
923 peasycap->audio_read)) {
924 SAM("ERROR: peasycap->audio_read out of range\n");
925 mutex_unlock(&easycap_dongle[kd].mutex_audio);
928 pdata_buffer = &peasycap->audio_buffer[peasycap->audio_read];
929 if ((struct data_buffer *)NULL == pdata_buffer) {
930 SAM("ERROR: pdata_buffer is NULL\n");
931 mutex_unlock(&easycap_dongle[kd].mutex_audio);
934 if (NULL == pdata_buffer->pgo) {
935 SAM("ERROR: pdata_buffer->pgo is NULL\n");
936 mutex_unlock(&easycap_dongle[kd].mutex_audio);
939 if (NULL == pdata_buffer->pto) {
940 SAM("ERROR: pdata_buffer->pto is NULL\n");
941 mutex_unlock(&easycap_dongle[kd].mutex_audio);
944 kount1 = PAGE_SIZE - (pdata_buffer->pto - pdata_buffer->pgo);
946 JOM(12, "ready to send %li bytes\n", (long int) kount1);
947 JOM(12, "still to send %li bytes\n", (long int) kount);
951 JOM(12, "agreed to send %li bytes from page %i\n", \
952 more, peasycap->audio_read);
956 /*---------------------------------------------------------------------------*/
958 * ACCUMULATE DYNAMIC-RANGE INFORMATION
960 /*---------------------------------------------------------------------------*/
961 p0 = (unsigned char *)pdata_buffer->pgo; l0 = 0; lm = more/2;
963 SUMMER(p0, &peasycap->audio_sample, &peasycap->audio_niveau, \
964 &peasycap->audio_square); l0++; p0 += 2;
966 /*---------------------------------------------------------------------------*/
967 rc = copy_to_user(puserspacebuffer, pdata_buffer->pto, more);
969 SAM("ERROR: copy_to_user() returned %li\n", rc);
970 mutex_unlock(&easycap_dongle[kd].mutex_audio);
973 *poff += (loff_t)more;
974 szret += (size_t)more;
975 pdata_buffer->pto += more;
976 puserspacebuffer += more;
977 kount -= (size_t)more;
979 JOM(12, "after read, %i=frag read %i=frag fill\n", \
980 (peasycap->audio_read / peasycap->audio_pages_per_fragment), \
981 (peasycap->audio_fill / peasycap->audio_pages_per_fragment));
983 SAM("MISTAKE: %li=kount %li=szret\n", \
984 (long int)kount, (long int)szret);
986 /*---------------------------------------------------------------------------*/
988 * CALCULATE DYNAMIC RANGE FOR (VAPOURWARE) AUTOMATIC VOLUME CONTROL
990 /*---------------------------------------------------------------------------*/
991 if (peasycap->audio_sample) {
992 below = peasycap->audio_sample;
993 above = peasycap->audio_square;
994 sdr = signed_div(above, below);
995 above = sdr.quotient;
996 mean = peasycap->audio_niveau;
997 sdr = signed_div(mean, peasycap->audio_sample);
999 JOM(8, "%8lli=mean %8lli=meansquare after %lli samples, =>\n", \
1000 sdr.quotient, above, peasycap->audio_sample);
1002 sdr = signed_div(above, 32768);
1003 JOM(8, "audio dynamic range is roughly %lli\n", sdr.quotient);
1005 /*---------------------------------------------------------------------------*/
1007 * UPDATE THE AUDIO CLOCK
1009 /*---------------------------------------------------------------------------*/
1010 do_gettimeofday(&timeval);
1011 if (!peasycap->timeval1.tv_sec) {
1012 peasycap->audio_bytes = 0;
1013 peasycap->timeval3 = timeval;
1014 peasycap->timeval1 = peasycap->timeval3;
1015 sdr.quotient = 192000;
1017 peasycap->audio_bytes += (long long int) szret;
1018 below = ((long long int)(1000000)) * \
1019 ((long long int)(timeval.tv_sec - \
1020 peasycap->timeval3.tv_sec)) + \
1021 (long long int)(timeval.tv_usec - peasycap->timeval3.tv_usec);
1022 above = 1000000 * ((long long int) peasycap->audio_bytes);
1025 sdr = signed_div(above, below);
1027 sdr.quotient = 192000;
1029 JOM(8, "audio streaming at %lli bytes/second\n", sdr.quotient);
1030 peasycap->dnbydt = sdr.quotient;
1032 JOM(8, "returning %li\n", (long int)szret);
1033 mutex_unlock(&easycap_dongle[kd].mutex_audio);
1036 /*****************************************************************************/
1037 /*---------------------------------------------------------------------------*/
1039 * SUBMIT ALL AUDIO URBS.
1041 /*---------------------------------------------------------------------------*/
1043 submit_audio_urbs(struct easycap *peasycap)
1045 struct data_urb *pdata_urb;
1047 struct list_head *plist_head;
1048 int j, isbad, nospc, m, rc;
1051 if (NULL == peasycap) {
1052 SAY("ERROR: peasycap is NULL\n");
1055 if ((struct list_head *)NULL == peasycap->purb_audio_head) {
1056 SAM("ERROR: peasycap->urb_audio_head uninitialized\n");
1059 if ((struct usb_device *)NULL == peasycap->pusb_device) {
1060 SAM("ERROR: peasycap->pusb_device is NULL\n");
1063 if (!peasycap->audio_isoc_streaming) {
1064 JOM(4, "initial submission of all audio urbs\n");
1065 rc = usb_set_interface(peasycap->pusb_device,
1066 peasycap->audio_interface, \
1067 peasycap->audio_altsetting_on);
1068 JOM(8, "usb_set_interface(.,%i,%i) returned %i\n", \
1069 peasycap->audio_interface, \
1070 peasycap->audio_altsetting_on, rc);
1072 isbad = 0; nospc = 0; m = 0;
1073 list_for_each(plist_head, (peasycap->purb_audio_head)) {
1074 pdata_urb = list_entry(plist_head, struct data_urb, list_head);
1075 if (NULL != pdata_urb) {
1076 purb = pdata_urb->purb;
1078 isbuf = pdata_urb->isbuf;
1081 purb->dev = peasycap->pusb_device;
1083 usb_rcvisocpipe(peasycap->pusb_device,\
1084 peasycap->audio_endpointnumber);
1085 purb->transfer_flags = URB_ISO_ASAP;
1086 purb->transfer_buffer = \
1087 peasycap->audio_isoc_buffer[isbuf].pgo;
1088 purb->transfer_buffer_length = \
1089 peasycap->audio_isoc_buffer_size;
1090 purb->complete = easysnd_complete;
1091 purb->context = peasycap;
1092 purb->start_frame = 0;
1093 purb->number_of_packets = \
1094 peasycap->audio_isoc_framesperdesc;
1095 for (j = 0; j < peasycap->\
1096 audio_isoc_framesperdesc; \
1098 purb->iso_frame_desc[j].offset = j * \
1100 audio_isoc_maxframesize;
1101 purb->iso_frame_desc[j].length = \
1103 audio_isoc_maxframesize;
1106 rc = usb_submit_urb(purb, GFP_KERNEL);
1109 SAM("ERROR: usb_submit_urb() failed" \
1110 " for urb with rc:\n");
1149 SAM("unknown error code %i\n",\
1165 SAM("-ENOSPC=usb_submit_urb() for %i urbs\n", nospc);
1166 SAM("..... possibly inadequate USB bandwidth\n");
1167 peasycap->audio_eof = 1;
1170 JOM(4, "attempting cleanup instead of submitting\n");
1171 list_for_each(plist_head, (peasycap->purb_audio_head)) {
1172 pdata_urb = list_entry(plist_head, struct data_urb, \
1174 if (NULL != pdata_urb) {
1175 purb = pdata_urb->purb;
1180 peasycap->audio_isoc_streaming = 0;
1182 peasycap->audio_isoc_streaming = 1;
1183 JOM(4, "submitted %i audio urbs\n", m);
1186 JOM(4, "already streaming audio urbs\n");
1190 /*****************************************************************************/
1191 /*---------------------------------------------------------------------------*/
1193 * KILL ALL AUDIO URBS.
1195 /*---------------------------------------------------------------------------*/
1197 kill_audio_urbs(struct easycap *peasycap)
1200 struct list_head *plist_head;
1201 struct data_urb *pdata_urb;
1203 if (NULL == peasycap) {
1204 SAY("ERROR: peasycap is NULL\n");
1207 if (peasycap->audio_isoc_streaming) {
1208 if ((struct list_head *)NULL != peasycap->purb_audio_head) {
1209 peasycap->audio_isoc_streaming = 0;
1210 JOM(4, "killing audio urbs\n");
1212 list_for_each(plist_head, (peasycap->purb_audio_head)) {
1213 pdata_urb = list_entry(plist_head, struct data_urb,
1215 if ((struct data_urb *)NULL != pdata_urb) {
1216 if ((struct urb *)NULL != pdata_urb->purb) {
1217 usb_kill_urb(pdata_urb->purb);
1222 JOM(4, "%i audio urbs killed\n", m);
1224 SAM("ERROR: peasycap->purb_audio_head is NULL\n");
1228 JOM(8, "%i=audio_isoc_streaming, no audio urbs killed\n", \
1229 peasycap->audio_isoc_streaming);
1233 /*****************************************************************************/