From huangant@Evilant.dorm7.nctu.edu.tw  Sat Apr 14 00:20:15 2001
Return-Path: <huangant@Evilant.dorm7.nctu.edu.tw>
Received: from Evilant.dorm7.nctu.edu.tw (EvilAnt.Dorm7.NCTU.edu.tw [140.113.89.168])
	by hub.freebsd.org (Postfix) with ESMTP id C584C37B50E
	for <FreeBSD-gnats-submit@freebsd.org>; Sat, 14 Apr 2001 00:20:14 -0700 (PDT)
	(envelope-from huangant@Evilant.dorm7.nctu.edu.tw)
Received: (from huangant@localhost)
	by Evilant.dorm7.nctu.edu.tw (8.11.3/8.11.3) id f3E7KCb04683;
	Sat, 14 Apr 2001 15:20:12 +0800 (CST)
	(envelope-from huangant)
Message-Id: <200104140720.f3E7KCb04683@Evilant.dorm7.nctu.edu.tw>
Date: Sat, 14 Apr 2001 15:20:12 +0800 (CST)
From: huangant@cis.nctu.edu.tw
Reply-To: huangant@cis.nctu.edu.tw
To: FreeBSD-gnats-submit@freebsd.org
Cc: huangant@cis.nctu.edu.tw
Subject: [patch] fix of ioctl(SNDCTL_DSP_SPEED) return value
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         26563
>Category:       kern
>Synopsis:       ioctl(SNDCTL_DSP_SPEED) returns -1 when fall back to orignal sample rate
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    sound
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Apr 14 00:30:01 PDT 2001
>Closed-Date:    Wed Feb 26 06:47:42 PST 2003
>Last-Modified:  Wed Feb 26 06:47:42 PST 2003
>Originator:     Chun-Ying Huang
>Release:        FreeBSD 4.3-BETA2 i386
>Organization:
NCTU Dept. of Computer Information Science, Taiwan
>Environment:
System: FreeBSD Evilant.dorm7.nctu.edu.tw 4.3-BETA2 FreeBSD 4.3-BETA2 #6: Fri Apr 13 12:04:14 CST 2001 huangant@Evilant.dorm7.nctu.edu.tw:/usr/src/sys/compile/EVILANT i386


>Description:

	When I am trying to set output sample rate that my sound card
	not support (my sound card is fixed at 48khz), the
	ioctl(SNDCTL_DSP_SPEED) return -1 and the value passed into 
	ioctl() keep unchanged.
	
	According to OSS programming guide, the ioctl() should ONLY
	return -1 when critical/serious fault happens. When fails on
	setting sample rates, it should return 0 (no error) and
	change the value passed into ioctl() to the sample rate
	previous used.
	
	This problem will happen on some fixed sample rate sound chips,
	for example, VIA 82C686 AC'97 Codec, and cause some media player
	(such as 'xmms') caused an incorrect sound output.

	This problem exists in 4.3-BETA2, RC, and 20010412-CURRENT.
	
>How-To-Repeat:

	Run following piece of the code:
=== cut here ===
#include <stdio.h>
#include <fcntl.h>
#include <sys/soundcard.h>

int main() {
	int fd, freq;
	
	if((fd = open("/dev/dsp", O_WRONLY))==-1) {
		printf("open %s failed.\n", "/dev/dsp");
		return(-1);
	}

	freq = 44100;
	printf("trying setting 44100hz...");
	if(ioctl(fd, SNDCTL_DSP_SPEED, &freq)==-1)
		freq = -1;
	printf("[%d]\n", freq);

	close(fd);
	return(0);
}
=== cut here ===

	When setting 44.1khz as sample rate, ioctl() will return -1 and
	keep freq==44100 on my VIA82C686 AC'97 Codec.

	Before applying any patches, result of this code shows:
	
	trying setting 44100hz...[-1]

>Fix:

	Here is a very sample patch to solve this problem.
	The patch is just return correct value when sample rate
	falling back to previous rate caused by setting unsupported
	sample rate.

	PS. This patch is generate based on 4.3-BETA2, but it has
	been tested on 4.3-RC(OK) and
	20010412-CURRENT(with some offset).

=== cut here ===
--- sys/dev/sound/pcm/channel.c.old	Fri Apr 13 11:55:53 2001
+++ sys/dev/sound/pcm/channel.c	Fri Apr 13 11:56:14 2001
@@ -1178,7 +1178,7 @@
 	r = chn_tryspeed(c, speed);
 	if (r) {
 		DEB(printf("Failed to set speed %d falling back to %d\n", speed, oldspeed));
-		chn_tryspeed(c, oldspeed);
+		r = chn_tryspeed(c, oldspeed);
 	}
 	return r;
 }
=== cut here ===

	After applying this patch, result of above test code shows:
	
	trying setting 44100hz...[48000]
	
	on my VIA 82C686 AC'97 Codec.:)

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->sound 
Responsible-Changed-By: iedowse 
Responsible-Changed-When: Sat Nov 30 19:29:21 PST 2002 
Responsible-Changed-Why:  

Sound-related PR. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=26563 
State-Changed-From-To: open->closed 
State-Changed-By: orion 
State-Changed-When: Wed Feb 26 06:43:40 PST 2003 
State-Changed-Why:  
Applied, albeit very late after submission! 

With the feeder code these days do automatic rate and format 
conversion, this isn't a particular problem now. 

Thanks. 


http://www.freebsd.org/cgi/query-pr.cgi?pr=26563 
>Unformatted:
