title: Trouble generating beep in C code in Ubuntu
body: <p>I am learning about device drivers and want to write a kernel module which can generate a sound from pcspkr. I have used the following code in my module:</p>
  
  <pre><code>static void play_sound(uint32_t nFrequence)
  {
      uint32_t Div;
      uint8_t tmp;
  
      //Set the PIT to the desired frequency
      Div = 1193180 / nFrequence;
      outb(0x43, 0xb6);
      outb(0x42, (uint8_t)(Div));
      outb(0x42, (uint8_t)(Div &gt;&gt; 8));
  
      //And play the sound using the PC speaker
      tmp = inb(0x61);
      if (tmp != (tmp | 3))
      {
          outb(0x61, tmp | 3);
      }
  }
  
  //make it shutup
  static void nosound(void)
  {
      uint8_t tmp = inb(0x61) &amp; 0xFC;
      outb(0x61, tmp);
  }
  
  //Make a beep
  void beep(void)
  {
      static int ev = 1;
      if(ev)
      {
          play_sound(1000);
          ev = 0;
      }
      else
      {
          nosound();
          ev = 1;
      }
      //set_PIT_2(old_frequency);
  }
  </code></pre>
  
  <p>This generates no sound at all. I have tried generating sound through a normal c program using <code>ioctl(console_fd, KIOCSOUND, 1193180 / freq)</code> but even this doesn't work.</p>
  
  <p>After further investigation, I installed <code>beep</code>, ran <code>sudo modprobe pcspkr</code>, checked volume levels in <code>alsamixer</code>.</p>
  
  <p><code>printf '\a'</code> works and produces a short sound.</p>
  
  <p><code>beep</code> also produces the same sound before <code>sudo modprobe pcspkr</code> but does not produce any sound after it.</p>
  
  <p>I ran <code>speaker-test</code> and it works as expected and produces a beep at a desired frequency.</p>
  
  <p>Am I missing something? Please help me figure this out. Thanks in advance.</p>