# HG changeset patch
# User Paulo Matias <matias@dotbsd.org>
# Date 1231003406 7200
# Node ID faac71eb818d49271e6b9c67392e80deaba44542
# Parent  f054e739c2bbbc19a93bc5158cb01e3f5fbd15cd
ossphone latency improvements

diff -r f054e739c2bb -r faac71eb818d cmd/ossphone/ossphone.c
--- a/cmd/ossphone/ossphone.c	Wed Dec 31 00:07:02 2008 +0200
+++ b/cmd/ossphone/ossphone.c	Sat Jan 03 15:23:26 2009 -0200
@@ -28,6 +28,8 @@
 int snd_fd = -1;
 
 char *snd_dev = "/dev/dsp";
+char *in_dev;
+char *out_dev;
 int srate = 8000;
 double digit_duration = 0.2;
 double silence_duration = 0.1;
@@ -109,7 +111,8 @@
   ioctl (out_fd, SNDCTL_DSP_MODEM_OFFHOOK, &offhook);
 }
 
-static int wait_dialtone()
+static int
+wait_dialtone()
 {
   double dc_level = 0.0;
   const double min_dc_level = 0.2;
@@ -190,7 +193,7 @@
 }
 
 static void
-exit_handler(void)
+close_devices()
 {
   if (in_fd >= 0)
     {
@@ -199,9 +202,6 @@
     }
   if (out_fd >= 0)
     {
-      int offhook = 0;
-      printf("On-hook\n");
-      ioctl (out_fd, SNDCTL_DSP_MODEM_OFFHOOK, &offhook);
       close (out_fd);
       out_fd = -1;
     }
@@ -213,22 +213,94 @@
 }
 
 static void
+exit_handler(void)
+{
+  if (out_fd >= 0)
+    {
+      int offhook = 0;
+      printf("On-hook\n");
+      ioctl (out_fd, SNDCTL_DSP_MODEM_OFFHOOK, &offhook);
+    }
+  close_devices ();
+}
+
+static void
 sigint_handler(int sig)
 {
   exit (0);
 }
 
+static void
+open_devices(int snd_trig)
+{
+  int channels = 1;
+  int format = AFMT_S16_LE;
+  int tmp;
+
+  snd_fd = open (snd_dev, O_RDWR);
+  if (snd_fd < 0)
+    {
+      perror (snd_dev);
+      exit (-1);
+    }
+
+  in_fd = open (in_dev, O_RDWR);
+  if (in_fd < 0)
+    {
+      perror (in_dev);
+      exit (-1);
+    }
+  tmp=0;
+  ioctl(in_fd, SNDCTL_DSP_COOKEDMODE, &tmp); /* No error checking with this call */
+
+  out_fd = open(out_dev, O_RDWR);
+  if (out_fd < 0)
+    {
+      perror (out_dev);
+      exit (-1);
+    }
+  tmp=0;
+  ioctl(out_fd, SNDCTL_DSP_COOKEDMODE, &tmp); /* No error checking with this call */
+
+  tmp = (48<<16)|4;
+  assert ( ioctl (in_fd,  SNDCTL_DSP_SETFRAGMENT, &tmp) >= 0 );
+  assert ( ioctl (out_fd, SNDCTL_DSP_SETFRAGMENT, &tmp) >= 0 );
+  assert ( ioctl (snd_fd, SNDCTL_DSP_SETFRAGMENT, &tmp) >= 0 );
+
+  assert ( ioctl (in_fd,  SNDCTL_DSP_CHANNELS, &channels) >= 0 );
+  assert ( ioctl (out_fd, SNDCTL_DSP_CHANNELS, &channels) >= 0 );
+  assert ( ioctl (snd_fd, SNDCTL_DSP_CHANNELS, &channels) >= 0 );
+
+  assert ( ioctl (in_fd,  SNDCTL_DSP_SETFMT, &format) >= 0 );
+  assert ( ioctl (out_fd, SNDCTL_DSP_SETFMT, &format) >= 0 );
+  assert ( ioctl (snd_fd, SNDCTL_DSP_SETFMT, &format) >= 0 );
+
+  assert ( ioctl (in_fd,  SNDCTL_DSP_SPEED, &srate) >= 0 );
+  assert ( ioctl (out_fd, SNDCTL_DSP_SPEED, &srate) >= 0 );
+  assert ( ioctl (snd_fd, SNDCTL_DSP_SPEED, &srate) >= 0 );
+
+  tmp = 0;
+  assert ( ioctl (in_fd,  SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
+  tmp = PCM_ENABLE_INPUT;
+  assert ( ioctl (in_fd,  SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
+  tmp = 0;
+  assert ( ioctl (out_fd, SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
+  tmp = PCM_ENABLE_OUTPUT;
+  assert ( ioctl (out_fd, SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
+  tmp = 0;
+  assert ( ioctl (snd_fd, SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
+  tmp = snd_trig;
+  assert ( ioctl (snd_fd, SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
+}
+
 int
 main(int argc, char **argv)
 {
   char *phone_number = "";
 
-  int channels = 1;
-  int format = AFMT_S16_LE;
-
   extern int optind;
   extern char *optarg;
-  int c, tmp;
+  int c;
 
   cmdname=argv[0];
 
@@ -265,68 +337,17 @@
 
   atexit(exit_handler);
 
-  snd_fd = open (snd_dev, O_RDWR);
-  if (snd_fd < 0)
-    {
-      perror (snd_dev);
-      exit (-1);
-    }
-
-  in_fd = open (argv[optind], O_RDWR);
-  if (in_fd < 0)
-    {
-      perror (argv[optind]);
-      exit (-1);
-    }
-  tmp=0;
-  ioctl(in_fd, SNDCTL_DSP_COOKEDMODE, &tmp); // No error checking with this call
-  optind++;
-
-  out_fd = open(argv[optind], O_RDWR);
-  if (out_fd < 0)
-    {
-      perror (argv[optind]);
-      exit (-1);
-    }
-  tmp=0;
-  ioctl(out_fd, SNDCTL_DSP_COOKEDMODE, &tmp); // No error checking with this call
-  optind++;
+  in_dev = argv[optind++];
+  out_dev = argv[optind++];
 
   if (argc > optind)
       phone_number = argv[optind];
 
-  assert ( ioctl (in_fd,  SNDCTL_DSP_CHANNELS, &channels) >= 0 );
-  assert ( ioctl (out_fd, SNDCTL_DSP_CHANNELS, &channels) >= 0 );
-  assert ( ioctl (snd_fd, SNDCTL_DSP_CHANNELS, &channels) >= 0 );
-
-  assert ( ioctl (in_fd,  SNDCTL_DSP_SETFMT, &format) >= 0 );
-  assert ( ioctl (out_fd, SNDCTL_DSP_SETFMT, &format) >= 0 );
-  assert ( ioctl (snd_fd, SNDCTL_DSP_SETFMT, &format) >= 0 );
-
-  assert ( ioctl (in_fd,  SNDCTL_DSP_SPEED, &srate) >= 0 );
-  assert ( ioctl (out_fd, SNDCTL_DSP_SPEED, &srate) >= 0 );
-  assert ( ioctl (snd_fd, SNDCTL_DSP_SPEED, &srate) >= 0 );
-
-  {
-    int tmp;
-    tmp = 0;
-    assert ( ioctl (in_fd,  SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
-    tmp = PCM_ENABLE_INPUT;
-    assert ( ioctl (in_fd,  SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
-    tmp = 0;
-    assert ( ioctl (out_fd, SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
-    tmp = PCM_ENABLE_OUTPUT;
-    assert ( ioctl (out_fd, SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
-    tmp = 0;
-    assert ( ioctl (snd_fd, SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
-    tmp = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
-    assert ( ioctl (snd_fd, SNDCTL_DSP_SETTRIGGER, &tmp) >= 0 );
-  }
-
+  open_devices (PCM_ENABLE_OUTPUT);
   go_offhook ();
 
   if (phone_number[0] != '\0')
-  {
+    {
       if (wait_dialtone () < 0)
         {
           printf("No dial tone.\n");
@@ -338,8 +359,11 @@
   printf("Call in progress...\n");
   printf("Press Ctrl-C to quit.\n");
 
+  close_devices ();
+  open_devices (PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT);
+
   {
-    uint16_t buf[128];
+    uint16_t buf[32];
     fd_set rfds;
     int retval;
 
@@ -362,13 +386,13 @@
              {
                if (FD_ISSET(in_fd, &rfds))
                  {
-                   read(in_fd, buf, sizeof(buf));
-                   write(snd_fd, buf, sizeof(buf));
+                   read (in_fd, buf, sizeof(buf));
+                   write (snd_fd, buf, sizeof(buf));
                  }
                if (FD_ISSET(snd_fd, &rfds))
                  {
-                   read(snd_fd, buf, sizeof(buf));
-                   write(out_fd, buf, sizeof(buf));
+                   read (snd_fd, buf, sizeof(buf));
+                   write (out_fd, buf, sizeof(buf));
                  }
              }
          }
diff -r f054e739c2bb -r faac71eb818d kernel/drv/oss_hdaudio/hdaudio_si3055.c
--- a/kernel/drv/oss_hdaudio/hdaudio_si3055.c	Wed Dec 31 00:07:02 2008 +0200
+++ b/kernel/drv/oss_hdaudio/hdaudio_si3055.c	Sat Jan 03 15:23:26 2009 -0200
@@ -30,7 +30,7 @@
  * Si3055 register IDs.
  */
 #define SI3055_EXT_MODEM_STATUS 2
-#define SI3055_LINE_RATE  3
+#define SI3055_LINE_RATE        3
 #define SI3055_HDA_STREAMS      4
 #define SI3055_GPIO_CONFIG      5
 #define SI3055_GPIO_PIN_STATUS  10
