lm_sensors2/kernel/chips w83792d.c
Hi:
Quoted 7 lines · * [email protected] <[email protected]> [2005-03-26…
Update of
/home/cvs/lm_sensors2/kernel/chipsIn directory mordac.netroedge.com:/tmp/cvs-serv26712Modified Files:
w83792d.cLog Message:
(mds) fix negative fan values. Patch from Chunhao.
Why is there a huge 'do {...} while(0)' loop in w83792d_fan()? I guess it's some kind of fan divisor auto-configuration. If that's the case, shouldn't it be 'do {...} while(1)'? Also, why not move the w83792d_update_client() call to the bottom of the loop?
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
Hi Chunhao:
Quoted 3 lines · I wrote:
Quoted 3 lines
Why is there a huge 'do {...} while(0)' loop in
w83792d_fan()? I guess it's some kind of fan divisor auto-configuration. If that's the case, shouldn't it be 'do {...} while(1)'?
Quoted 4 lines · Chunhao
We use that huge 'do {...} while(0)' to substitute the "goto" sentence, because the "goto" sentence is NOT welcome in most cases, we want to avoid
it. :-)
If that was your intention, I would prefer if you just used goto. To use 'do {...} while(0)' as a "stealth" goto is even worse.
Quoted 4 lines
Although the idea of changing "while (0)" into "while (1)" is feasible, We can NOT do that, it's dangerous to use such endless loop. We have tested
it, the "while (1)" leads to bad result, while "while (0)" works well.
Well, the purpose of this part of the code was not immediately clear to me. Now I've read it more closely. It seems you are trying to settle on an appropriate fan divisor - one which is appropriate for the measured fan speed. But... the loop obviously isn't a loop at all, so you're only incrementing or decrementing fan_div[nr] by 1. That means the user would have to read the fan multiple times before the fan_div arrives at a useful value. Is that what you wanted?
Also notice, the way it's written now... the value reported in results[0] (the limit) will be wrong if the fan_div is written back to the chip immediately after.
Quoted 5 lines
Quoted 2 lines
Also, why not move the
w83792d_update_client()call to the bottom of the loop?Which case do you mean ? Case 1 or Case 2?
(snip)
I should have noticed in the first place... but actually it doesn't matter at all. It will only really update the values once anyway because of the rate limiting. As it's written now, you may just as well get rid of the second one altogether. Do you see why?
At any rate, I think there are multiple problems here. Could you please explain exactly what you are trying to do between these lines? Then maybe I can help you better.
Quoted 3 lines
1000 else if (operation =
SENSORS_PROC_REAL_READ) { ???
1042 } else if (operation =SENSORS_PROC_REAL_WRITE) {
Regards,
Signature · 2 lines
Mark M. Hoffman [email protected]
Hi Chunhao, hi Mark,
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
We use that huge 'do {...} while(0)' to substitute the "goto" sentence, because the "goto" sentence is NOT welcome in most cases, we want to avoid it. :-)
If that was your intention, I would prefer if you just used goto. To use 'do {...} while(0)' as a "stealth" goto is even worse.
I second Mark on this, using a loop block when you have no itention to actually loop is error-prone.
It looks quite obvious to me that this block should be made a seperate function, as it's quite big and accomplishes a specific task. Breaks will become returns, so you would need neither the loop-which-is-not-a-loop nor gotos. This is exactly how things are supposed to be done in C.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Although the idea of changing "while (0)" into "while (1)" is feasible, We can NOT do that, it's dangerous to use such endless loop. We have tested it, the "while (1)" leads to bad result, while "while (0)" works well.
Of course. It's not supposed to be a loop in the first place, so (I guess) it lacks a proper exit condition.
Quoted 7 lines
Quoted 7 lines
Well, the purpose of this part of the code was not immediately clear to me. Now I've read it more closely. It seems you are trying to settle on an appropriate fan divisor - one which is appropriate for the measured fan speed. But... the loop obviously isn't a loop at all, so you're only incrementing or decrementing fan_div[nr] by 1. That means the user would have to read the fan multiple times before the
fan_divarrives at a useful value. Is that what you wanted?
That's quite logical. I do the same in my pc87360 driver. When the fan clock divider is too low and the register overflows, you have no idea what the "ideal" divider would be, so you have to increase it step by step until you get a valid reading.
A different approach would be to jump to the highest divider, and go down from there until the register value fits in the ideal range. This would be optimum for missing fans and better for very slow fans, but in the common case (fan is present and not too slow), the simple algorithm of increasing the divider step by step seems to work better, as the correct divider will be reached after only one or two tries.
A yet different approach, which I took in the new w83627ehf driver, is to mainly set the divider according to the fan min limit and not to the fan reading. This works because by definition the fan reading should always be more that the limit. That way, users who know which speed they expect will set the limit just below that, which will in turn set the correct divider, which will then never change (unless the fan speed actually drops below that limit). Or course I always make sure that there is a significant room below the limit before a divider change is required. If you think about it, you'll see that it's exactly how voltage channels work. Not all voltage values can be measured, only some range around the expected value, typically a 0-133% range of nominal value. Same I do with fans in the w83627ehf case.
These methods are still at the experimental stage, granted, but I really believe that once we have found the best algorithm and implement it in more drivers, the end user will enjoy the improvement. Just see how many tickets and posts we have about fan_div problems and you'll see what I mean.
Thanks,
--
Jean Delvare
Hi Mark M. Hoffman
Thank you for your suggestion, please refer to my answer below.
Quoted 1 line
Why is there a huge 'do {...} while(0)' loop in
w83792d_fan()? I
guess
Quoted 2 lines
it's some kind of fan divisor auto-configuration. If that's the case, shouldn't it be 'do {...} while(1)'?
We use that huge 'do {...} while(0)' to substitute the "goto" sentence, because the "goto" sentence is NOT welcome in most cases, we want to avoid
it. :-)
Although the idea of changing "while (0)" into "while (1)" is feasible, We can NOT do that, it's dangerous to use such endless loop. We have tested
it, the "while (1)" leads to bad result, while "while (0)" works well.
Quoted 1 line
Also, why not move the
w83792d_update_client()call to the bottom of
the loop?
Which case do you mean ? Case 1 or Case 2?
Original:
/* adjust Fan Divisor, then change RPM */
do {
w83792d_update_client(client);
if ((data->fan[nr]>0x50) &&
(data->fan[nr]<0xff)) {
......
} else {
......
}
} while (0);After modification, Case 1:
/* adjust Fan Divisor, then change RPM */
do {
if ((data->fan[nr]>0x50) &&
(data->fan[nr]<0xff)) {
......
} else {
......
}
w83792d_update_client(client);
} while (0);After modification, Case 2:
/* adjust Fan Divisor, then change RPM */
do {
if ((data->fan[nr]>0x50) &&
(data->fan[nr]<0xff)) {
......
} else {
......
}
} while (0);
w83792d_update_client(client);It seems the "Case 1" is NOT good, because the "w83792d_update_client()"
will NOT be executed when some "break" appears.
To tell the truth, I did not consider such problem before, Would you please give me the sufficient reason of the modification?
Thanks
Best Regards
Chunhao
==============================================The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original author of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such person, please kindly reply the sender indicating accordingly and delete all copies of it from your computer and network server immediately. We thank you for your cooperation. It is advisable that any unauthorized use of confidential information of Winbond is strictly prohibited; and any information in this email that does not relate to the official business of Winbond shall be deemed as neither given nor endorsed by Winbond.==============================================If your computer is unable to decode Chinese font, please ignore the following message. They essentially repea!
t the English statement above.本信件內所含華邦電子的財產性機密性資訊, 僅授權原發信人指定之收信人取閱之用. 假使您並非被指定之收信人或因任何原因在未經授權的情形之下收到本信件, 請您告知原發信人並立即將信件從電腦與網路伺服器中予以消除. 對於您的合作, 我們先此致謝. 特此提醒, 任何未經授權擅自使用華邦電子的機密資訊的行為是被嚴格禁止的. 信件與華邦電子營業無關之內容,不得視為華邦電子之立場或意見.
Hi Mark M. Hoffman
Quoted 1 line
Quoted 1 line
We use that huge 'do {...} while(0)' to substitute the "goto"
sentence,
Quoted 6 lines
Quoted 3 lines
because the "goto" sentence is NOT welcome in most cases, we want to avoid
it. :-)If that was your intention, I would prefer if you just used goto. To use 'do {...} while(0)' as a "stealth" goto is even worse.
Maybe you are right, I copied these codes from our Windows driver, without
too much consideration about it. I don't resist "goto" in my 792 driver, you
can see there are some "goto" sentences in some other places of the driver.
You may help me change the "do...while(0)" into "goto" sentence if you want,
since I do NOT have a lm_sensors CVS account, If I want to modify it, I have to generate a patch and send it to MDS for his help.
After the modification, please just send me a notice so that I can download
from CVS and test it immediately. Or you may send it to me first for my test, then commit it into CVS after the test.
Quoted 1 line
Quoted 1 line
Although the idea of changing "while (0)" into "while (1)" is
feasible,
Quoted 3 lines
Quoted 3 lines
We can NOT do that, it's dangerous to use such endless loop. We have tested
it, the "while (1)" leads to bad result, while "while (0)" works
well.
Quoted 8 lines
Well, the purpose of this part of the code was not immediately clear to me. Now I've read it more closely. It seems you are trying to settle on an appropriate fan divisor - one which is appropriate for the measured fan speed. But... the loop obviously isn't a loop at all, so you're only incrementing or decrementing fan_div[nr] by 1. That means the user would have to read the fan multiple times before the
fan_divarrives at a useful value. Is that what you wanted?
Yes, you are right, that is what I want, users have to run "sensors" for several times before they get an appropriate fan measured value. In our 792 Windows version, there is a GUI which access the 792 chip with an interval such as every 5 seconds. So after several seconds, it will reach the appropriate fan measured value automatically.
Quoted 4 lines
Also notice, the way it's written now... the value reported in results[0] (the limit) will be wrong if the
fan_divis written back to the chip immediately after.
Quoted 12 lines
I should have noticed in the first place... but actually it doesn't matter at all. It will only really update the values once anyway because of the rate limiting. As it's written now, you may just as well get rid of the second one altogether. Do you see why?
At any rate, I think there are multiple problems here. Could you please explain exactly what you are trying to do between these lines? Then maybe I can help you better.
Quoted 3 lines
1000 else if (operation =
SENSORS_PROC_REAL_READ) { ???
1042 } else if (operation =SENSORS_PROC_REAL_WRITE) {
When I wrote these codes, I'm also afraid of leading to some problems, But the test result on our two motherboards seems good, So I kept those codes. I remember that I have ever sent a mail to lm_sensors group for your suggestion about this function w83792d_fan(), but do NOT get any response.:-(
If you can help me to modify these codes better, you may try to modify it, and ask me to test. But please keep my purpose of modifying the fan divisor and measured value automatically, because it the request of our customers. :-)
Thanks
Best Regards
Chunhao
2005-3-29
==============================================The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original author of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such person, please kindly reply the sender indicating accordingly and delete all copies of it from your computer and network server immediately. We thank you for your cooperation. It is advisable that any unauthorized use of confidential information of Winbond is strictly prohibited; and any information in this email that does not relate to the official business of Winbond shall be deemed as neither given nor endorsed by Winbond.==============================================If your computer is unable to decode Chinese font, please ignore the following message. They essentially repea!
t the English statement above.本信件內所含華邦電子的財產性機密性資訊, 僅授權原發信人指定之收信人取閱之用. 假使您並非被指定之收信人或因任何原因在未經授權的情形之下收到本信件, 請您告知原發信人並立即將信件從電腦與網路伺服器中予以消除. 對於您的合作, 我們先此致謝. 特此提醒, 任何未經授權擅自使用華邦電子的機密資訊的行為是被嚴格禁止的. 信件與華邦電子營業無關之內容,不得視為華邦電子之立場或意見.
Dear Jean, Dear Mark,
Quoted 8 lines
I second Mark on this, using a loop block when you have no itention to actually loop is error-prone.
It looks quite obvious to me that this block should be made a seperate function, as it's quite big and accomplishes a specific task. Breaks will become returns, so you would need neither the loop-which-is-not-a-loop nor gotos. This is exactly how things are supposed to be done in C.
Your suggestion is good, I will adopt it, :-)
I will send you a new patch after my modification and test.
Quoted 6 lines
That's quite logical. I do the same in my pc87360 driver. When the fan clock divider is too low and the register overflows, you have no idea what the "ideal" divider would be, so you have to increase it step by step until you get a valid reading.
A different approach would be to jump to the highest divider, and go
down
Quoted 1 line
from there until the register value fits in the ideal range. This
would
Quoted 3 lines
be optimum for missing fans and better for very slow fans, but in the common case (fan is present and not too slow), the simple algorithm of increasing the divider step by step seems to work better, as the
correct
Quoted 3 lines
divider will be reached after only one or two tries.
A yet different approach, which I took in the new w83627ehf driver, is
to
Quoted 1 line
mainly set the divider according to the fan min limit and not to the
fan
Quoted 1 line
reading. This works because by definition the fan reading should
always
Quoted 1 line
be more that the limit. That way, users who know which speed they
expect
Quoted 4 lines
will set the limit just below that, which will in turn set the correct divider, which will then never change (unless the fan speed actually drops below that limit). Or course I always make sure that there is a significant room below the limit before a divider change is required.
If
Quoted 2 lines
you think about it, you'll see that it's exactly how voltage channels work. Not all voltage values can be measured, only some range around
the
Quoted 4 lines
expected value, typically a 0-133% range of nominal value. Same I do with fans in the w83627ehf case.
These methods are still at the experimental stage, granted, but I
really
Quoted 2 lines
believe that once we have found the best algorithm and implement it in more drivers, the end user will enjoy the improvement. Just see how
many
Quoted 2 lines
tickets and posts we have about
fan_divproblems and you'll see what I mean.
Your explanation is as detailed as usual, I can catch it :-) I will keep my original method (increase/decrease step by step) before we
find one better method.
Thanks
Best Regards
Chunhao
==============================================The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original author of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such person, please kindly reply the sender indicating accordingly and delete all copies of it from your computer and network server immediately. We thank you for your cooperation. It is advisable that any unauthorized use of confidential information of Winbond is strictly prohibited; and any information in this email that does not relate to the official business of Winbond shall be deemed as neither given nor endorsed by Winbond.==============================================If your computer is unable to decode Chinese font, please ignore the following message. They essentially repea!
t the English statement above.本信件內所含華邦電子的財產性機密性資訊, 僅授權原發信人指定之收信人取閱之用. 假使您並非被指定之收信人或因任何原因在未經授權的情形之下收到本信件, 請您告知原發信人並立即將信件從電腦與網路伺服器中予以消除. 對於您的合作, 我們先此致謝. 特此提醒, 任何未經授權擅自使用華邦電子的機密資訊的行為是被嚴格禁止的. 信件與華邦電子營業無關之內容,不得視為華邦電子之立場或意見.
Hi Jean, Hi Mark,
Quoted 4 lines
That's quite logical. I do the same in my pc87360 driver. When the fan clock divider is too low and the register overflows, you have no idea what the "ideal" divider would be, so you have to increase it step by step until you get a valid reading.
I have one question here:
When the fan divisor adjust itself automatically step by step, Should the
fan limit keep the original value? For Example, in my present 792 driver,
#sensors
Fan1: 1614 RPM (min = 2500 RPM, div = 4)
If the divisor increase from 4 to 8, the fan1 limit value will become 1250RPM:
Fan1: 807 RPM (min = 1250 RPM, div = 8)
Do you think it's quite correct? Should I modify my 792 driver to keep the original 2500RPM just like this?
Fan1: 807 RPM (min = 2500 RPM, div = 8)
I check your kernel/chips/pc87360.c, there is:
static void pc87360_autodiv(struct pc87360_data *data, int nr)
{
u8 old_min = data->fan_min[nr];
/* Increase clock divider if needed and possible */
if ((data->fan_status[nr] & 0x04) /* overflow flag */
|| (data->fan[nr] >= 224)) { /* next to overflow */
if ((data->fan_status[nr] & 0x60) != 0x60) {
data->fan_status[nr] += 0x20;
data->fan_min[nr] >>= 1;
data->fan[nr] >>= 1;
#ifdef DEBUG
printk(KERN_DEBUG "pc87360.o: Increasing "
"clock divider to %d for fan %d\n",
FAN_DIV_FROM_REG(data->fan_status[nr]),
nr+1);
#endif
}
} else {
/* Decrease clock divider if possible */
while (!(data->fan_min[nr] & 0x80) /* fan min "nails"
divider */
&& data->fan[nr] < 85 /* bad accuracy */
&& (data->fan_status[nr] & 0x60) != 0x00) {
data->fan_status[nr] -= 0x20;
data->fan_min[nr] <<= 1;
data->fan[nr] <<= 1;
#ifdef DEBUG
printk(KERN_DEBUG "pc87360.o: Decreasing "
"clock divider to %d for fan %d\n",
FAN_DIV_FROM_REG(data->fan_status[nr]),
nr+1);
#endif
}
}
/* Write new fan min if it changed */
if (old_min != data->fan_min[nr]) {
pc87360_write_value(data, LD_FAN, NO_BANK,
PC87360_REG_FAN_MIN(nr),
data->fan_min[nr]);
}
}It seems that in your driver, the fan limit value will also change with the fan divisor like mine? Could you tell me the exact purpose of "old_min"?
Thanks
Best Regards
Chunhao
==============================================The privileged confidential information contained in this email is intended for use only by the addressees as indicated by the original author of this email. If you are not the addressee indicated in this email or are not responsible for delivery of the email to such person, please kindly reply the sender indicating accordingly and delete all copies of it from your computer and network server immediately. We thank you for your cooperation. It is advisable that any unauthorized use of confidential information of Winbond is strictly prohibited; and any information in this email that does not relate to the official business of Winbond shall be deemed as neither given nor endorsed by Winbond.==============================================If your computer is unable to decode Chinese font, please ignore the following message. They essentially repea!
t the English statement above.本信件內所含華邦電子的財產性機密性資訊, 僅授權原發信人指定之收信人取閱之用. 假使您並非被指定之收信人或因任何原因在未經授權的情形之下收到本信件, 請您告知原發信人並立即將信件從電腦與網路伺服器中予以消除. 對於您的合作, 我們先此致謝. 特此提醒, 任何未經授權擅自使用華邦電子的機密資訊的行為是被嚴格禁止的. 信件與華邦電子營業無關之內容,不得視為華邦電子之立場或意見.
Hi Chunhao,
Quoted 2 lines
When the fan divisor adjust itself automatically step by step, Should the fan limit keep the original value?
Yes it should. It is already prefered when changing the divider is a manual operation. It becomes a requirement when the divider changes automatically.
Quoted 2 lines
It seems that in your driver, the fan limit value will also change with the fan divisor like mine?
No, the fan min value, expressed in RPM, is preserved.
Quoted 1 line
Could you tell me the exact purpose of "
old_min"?
It's there to prevent writing the new register value if it happens to be the same as before (which will basically happen if the divider was not changed - I guess I could have tested the divider value instead for a similar result.
--
Jean Delvare