Strip off version numbers from dir name
[dockapps.git] / wmpower / src / power_management / toshiba / toshiba_lib.c
blob5705b6006ee631b7250c93ac357a254a08c0e028
1 /***************************************************************************
2 toshiba_lib.c - description
3 -------------------
4 begin : Feb 10 2003
5 copyright : (C) 2003 by Noberasco Michele
6 e-mail : 2001s098@educ.disi.unige.it
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 * *
26 ***************************************************************************/
28 /***************************************************************************
29 Many thanks to Jonathan A. Buzzard for his Toshiba(tm) Linux Utilities
30 I could never have done this otherwise
31 ***************************************************************************/
33 #define FAN_OFF 1
34 #define FAN_ON 0
36 #define UP_ONE_STEP -5
37 #define DOWN_ONE_STEP -6
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <unistd.h>
43 #include <pwd.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <dirent.h>
48 #ifdef __GLIBC__
49 #ifdef X86
50 #include <sys/perm.h>
51 #endif
52 #endif
54 #include "hci.h"
55 #include "sci.h"
56 #include "toshiba_lib.h"
57 #include "lib_utils.h"
58 #include "power_management.h"
61 void UglyHack(void);
62 int Get_Fan_Status(void);
66 int toshiba_get_fan_status(int use_toshiba_hardware)
68 if (use_toshiba_hardware != 2)
70 FILE *fp = fopen("/proc/acpi/toshiba/fan", "r");
71 if (fp)
73 int result;
74 if (fscanf(fp, "%*s%d", &result) == 1)
76 fclose(fp);
77 return result;
79 fclose(fp);
81 if (!use_toshiba_hardware) return PM_Error;
84 if (Get_Fan_Status()<0x00)
86 fprintf(stderr, "direct access to toshiba hardware failed:\n");
87 fprintf(stderr, "cannot get fan status!\n");
88 return PM_Error;
91 if (Get_Fan_Status()==0x00) return 0;
92 else return 1;
96 void toshiba_set_fan_status(int status)
98 SMMRegisters reg;
99 static int enable_spinoff=1;
100 static time_t timer;
102 if (status == 1)
104 if (toshiba_get_fan_status(2) == 1) return;
105 reg.eax = HCI_SET;
106 reg.ebx = HCI_FAN;
107 reg.ecx = HCI_ENABLE;
108 HciFunction(&reg);
109 usleep(100000);
110 if( toshiba_get_fan_status(2) == 1)
111 fprintf(stderr, "toshiba_lib: cooling fan turned on\n");
112 else
113 fprintf(stderr, "toshiba_lib: unable to turn on cooling fan\n");
114 return;
117 if (status == 0)
119 if (toshiba_get_fan_status(2) == 0) return;
120 if (!enable_spinoff)
122 if ( (time(NULL)-timer) >= 15 ) enable_spinoff=1;
123 else return;
125 enable_spinoff=0;
126 timer = time(NULL);
127 UglyHack();
128 if( toshiba_get_fan_status(2) == 0)
129 fprintf(stderr, "toshiba_lib: cooling fan turned off\n");
130 else
131 fprintf(stderr, "toshiba_lib: unable to turn off cooling fan\n");
132 return;
135 fprintf(stderr, "toshiba_lib: selected invalid fan status\n");
139 int acpi_set_lcd_brightness(int brightness)
141 FILE *fp;
142 static int current_brightness=-1;
143 int min_brightness = 0;
144 static int max_brightness;
146 if (current_brightness == -1)
148 fp= fopen("/proc/acpi/toshiba/lcd", "r");
149 if (!fp)
151 fprintf(stderr, "toshiba_lib: unable to set LCD brightness with ACPI.\n");
152 return 0;
154 fscanf(fp, "%*s%d%*s%d", &current_brightness, &max_brightness);
155 fclose(fp);
156 fprintf(stderr, "toshiba_lib: min brightness is %d\n", min_brightness);
157 fprintf(stderr, "toshiba_lib: max brightness is %d\n", --max_brightness);
158 fprintf(stderr, "toshiba_lib: current brightness is %d\n", current_brightness);
161 if (brightness == TOSHIBA_LCD_MAX) brightness = max_brightness;
162 if (brightness == TOSHIBA_LCD_MIN) brightness = 0;
163 if (brightness == TOSHIBA_LCD_MED) brightness = max_brightness/2;
165 if (brightness == UP_ONE_STEP)
167 if (current_brightness < max_brightness) brightness = current_brightness + 1;
168 else return 1;
170 if (brightness == DOWN_ONE_STEP)
172 if (current_brightness > min_brightness) brightness = current_brightness - 1;
173 else return 1;
176 if (maxBrightness != -1 && brightness > maxBrightness) brightness = maxBrightness;
177 if (minBrightness != -1 && brightness < minBrightness) brightness = minBrightness;
179 if (brightness != current_brightness)
181 fp = fopen("/proc/acpi/toshiba/lcd", "w");
182 if (!fp)
184 fprintf(stderr, "toshiba_lib: unable to set LCD brightness with ACPI.\n");
185 return 0;
187 fprintf(fp, "brightness:%d\n", brightness);
188 fclose(fp);
189 fp = fopen("/proc/acpi/toshiba/lcd", "r");
190 fscanf(fp, "%*s%d", &current_brightness);
191 fclose(fp);
192 if (brightness == current_brightness)
193 fprintf(stderr, "toshiba_lib: set LCD brightness to %d\n", brightness);
194 else
196 fprintf(stderr, "toshiba_lib: unable to set LCD brightness with ACPI.\n");
197 return 0;
201 usleep (125000); /* You must let some time pass between changes */
202 return 1;
206 void hardware_set_lcd_brightness(int brightness)
208 static int current_brightness=-1;
209 unsigned short lcd;
210 static unsigned short lcdtype=999;
211 SMMRegisters reg;
213 lcd = brightness;
215 if (lcdtype==999)
217 SciOpenInterface();
218 reg.ebx = SCI_LCD_BRIGHTNESS;
219 if (SciGet(&reg)==SCI_SUCCESS) lcdtype = SCI_LCD_BRIGHTNESS;
220 else lcdtype = SCI_LCD_BACKLIGHT;
221 reg.ebx = SCI_LCD_MAXBRIGHT;
222 if (SciGet(&reg)==SCI_SUCCESS)
224 lcdtype = SCI_LCD_MAXBRIGHT;
225 if (lcd>1) lcd = 1;
227 fprintf(stderr, "toshiba_lib: min brightness is %d\n", TOSHIBA_LCD_MIN);
228 fprintf(stderr, "toshiba_lib: max brightness is %d\n", TOSHIBA_LCD_MAX);
230 SciCloseInterface();
233 if (brightness == UP_ONE_STEP)
234 {/* Right now this is broken... */
235 /*if (current_brightness < TOSHIBA_LCD_MAX) lcd = current_brightness + 1;
236 else*/ return;
238 if (brightness == DOWN_ONE_STEP)
239 {/* Right now this is broken... */
240 /*if (current_brightness > TOSHIBA_LCD_MIN) lcd = current_brightness - 1;
241 else */return;
244 if (maxBrightness != -1 && lcd > maxBrightness) lcd = maxBrightness;
245 if (minBrightness != -1 && lcd < minBrightness) lcd = minBrightness;
247 if (lcd != current_brightness)
249 SciOpenInterface();
250 reg.ebx = lcdtype;
251 reg.ecx = lcd;
252 if (SciSet(&reg)==SCI_FAILURE)
253 fprintf(stderr, "toshiba_lib: unable to set LCD brightness\n");
254 else
256 fprintf(stderr, "toshiba_lib: changed LCD brightness to %d\n", lcd);
257 current_brightness=lcd;
259 SciCloseInterface();
264 void toshiba_set_lcd_brightness(int brightness, int use_hardware_call)
266 static int acpi_ok = 1;
268 if (!use_hardware_call)
270 acpi_set_lcd_brightness(brightness);
271 return;
273 if (!acpi_ok)
275 hardware_set_lcd_brightness(brightness);
276 return;
278 acpi_ok = acpi_set_lcd_brightness(brightness);
279 if (!acpi_ok)
281 fprintf(stderr, "Trying to control directly Toshiba lcd hardware\n");
282 hardware_set_lcd_brightness(brightness);
286 void Toshiba_lcdBrightness_UpOneStep(int use_hardware)
288 toshiba_set_lcd_brightness(UP_ONE_STEP, use_hardware);
291 void Toshiba_lcdBrightness_DownOneStep(int use_hardware)
293 toshiba_set_lcd_brightness(DOWN_ONE_STEP, use_hardware);
296 /* Check if we are running on a Toshiba laptop */
297 int machine_is_toshiba(int *use_toshiba_hardware)
299 int version,bios,id;
300 int result = 0;
301 DIR *toshiba_proc = opendir("/proc/acpi/toshiba");
303 strcpy(toshiba_model, "cannot get model without direct hardware access");
304 if (toshiba_proc)
306 closedir(toshiba_proc);
307 result = 1;
309 if (!use_toshiba_hardware || !(*use_toshiba_hardware)) return result;
311 /* do some quick checks on the laptop */
312 if (SciSupportCheck(&version)==1)
314 *use_toshiba_hardware = 0;
315 return result;
317 bios = HciGetBiosVersion();
318 if (bios==0)
320 *use_toshiba_hardware = 0;
321 return result;
323 if (HciGetMachineID(&id)==HCI_FAILURE)
325 *use_toshiba_hardware = 0;
326 return result;
329 strcpy(toshiba_model, "unknown model");
330 switch (id)
332 case 0xfc00: strcpy(toshiba_model, "Satellite 2140CDS/2180CDT/2675DVD"); break;
333 case 0xfc01: strcpy(toshiba_model, "Satellite 2710xDVD"); break;
334 case 0xfc02: strcpy(toshiba_model, "Satellite Pro 4270CDT//4280CDT/4300CDT/4340CDT"); break;
335 case 0xfc04: strcpy(toshiba_model, "Portege 3410CT, 3440CT"); break;
336 case 0xfc08: strcpy(toshiba_model, "Satellite 2100CDS/CDT 1550CDS"); break;
337 case 0xfc09: strcpy(toshiba_model, "Satellite 2610CDT, 2650XDVD"); break;
338 case 0xfc0a: strcpy(toshiba_model, "Portage 7140"); break;
339 case 0xfc0b: strcpy(toshiba_model, "Satellite Pro 4200"); break;
340 case 0xfc0c: strcpy(toshiba_model, "Tecra 8100x"); break;
341 case 0xfc0f: strcpy(toshiba_model, "Satellite 2060CDS/CDT"); break;
342 case 0xfc10: strcpy(toshiba_model, "Satellite 2550/2590"); break;
343 case 0xfc11: strcpy(toshiba_model, "Portage 3110CT"); break;
344 case 0xfc12: strcpy(toshiba_model, "Portage 3300CT"); break;
345 case 0xfc13: strcpy(toshiba_model, "Portage 7020CT"); break;
346 case 0xfc15: strcpy(toshiba_model, "Satellite 4030/4030X/4050/4060/4070/4080/4090/4100X CDS/CDT"); break;
347 case 0xfc17: strcpy(toshiba_model, "Satellite 2520/2540 CDS/CDT"); break;
348 case 0xfc18: strcpy(toshiba_model, "Satellite 4000/4010 XCDT"); break;
349 case 0xfc19: strcpy(toshiba_model, "Satellite 4000/4010/4020 CDS/CDT"); break;
350 case 0xfc1a: strcpy(toshiba_model, "Tecra 8000x"); break;
351 case 0xfc1c: strcpy(toshiba_model, "Satellite 2510CDS/CDT"); break;
352 case 0xfc1d: strcpy(toshiba_model, "Portage 3020x"); break;
353 case 0xfc1f: strcpy(toshiba_model, "Portage 7000CT/7010CT"); break;
354 case 0xfc39: strcpy(toshiba_model, "T2200SX"); break;
355 case 0xfc40: strcpy(toshiba_model, "T4500C"); break;
356 case 0xfc41: strcpy(toshiba_model, "T4500"); break;
357 case 0xfc45: strcpy(toshiba_model, "T4400SX/SXC"); break;
358 case 0xfc51: strcpy(toshiba_model, "Satellite 2210CDT, 2770XDVD"); break;
359 case 0xfc52: strcpy(toshiba_model, "Satellite 2775DVD, Dynabook Satellite DB60P/4DA"); break;
360 case 0xfc53: strcpy(toshiba_model, "Portage 7200CT/7220CT, Satellite 4000CDT"); break;
361 case 0xfc54: strcpy(toshiba_model, "Satellite 2800DVD"); break;
362 case 0xfc56: strcpy(toshiba_model, "Portage 3480CT"); break;
363 case 0xfc57: strcpy(toshiba_model, "Satellite 2250CDT"); break;
364 case 0xfc5a: strcpy(toshiba_model, "Satellite Pro 4600"); break;
365 case 0xfc5d: strcpy(toshiba_model, "Satellite 2805"); break;
366 case 0xfc5f: strcpy(toshiba_model, "T3300SL"); break;
367 case 0xfc61: strcpy(toshiba_model, "Tecra 8200"); break;
368 case 0xfc64: strcpy(toshiba_model, "Satellite 1800"); break;
369 case 0xfc69: strcpy(toshiba_model, "T1900C"); break;
370 case 0xfc70: strcpy(toshiba_model, "Libretto L2"); break;
371 case 0xfc6a: strcpy(toshiba_model, "T1900"); break;
372 case 0xfc6c: strcpy(toshiba_model, "Satellite 5005 S504"); break;
373 case 0xfc6d: strcpy(toshiba_model, "T1850C"); break;
374 case 0xfc6e: strcpy(toshiba_model, "T1850"); break;
375 case 0xfc6f: strcpy(toshiba_model, "T1800"); break;
376 case 0xfc71: strcpy(toshiba_model, "Satellite Pro 6000"); break;
377 case 0xfc72: strcpy(toshiba_model, "Satellite 1800"); break;
378 case 0xfc7d: strcpy(toshiba_model, "Satellite Pro 6100"); break;
379 case 0xfc7e: strcpy(toshiba_model, "T4600C"); break;
380 case 0xfc7f: strcpy(toshiba_model, "T4600"); break;
381 case 0xfc8a: strcpy(toshiba_model, "T6600C"); break;
382 case 0xfc91: strcpy(toshiba_model, "T2400CT"); break;
383 case 0xfc97: strcpy(toshiba_model, "T4800CT"); break;
384 case 0xfc99: strcpy(toshiba_model, "T4700CS"); break;
385 case 0xfc9b: strcpy(toshiba_model, "T4700CT"); break;
386 case 0xfc9d: strcpy(toshiba_model, "T1950"); break;
387 case 0xfc9e: strcpy(toshiba_model, "T3400/T3400CT"); break;
388 case 0xfca6: strcpy(toshiba_model, "Portege 2010"); break;
389 case 0xfca9: strcpy(toshiba_model, "Satellite 2410-303"); break;
390 case 0xfcb2: strcpy(toshiba_model, "Libretto 30CT"); break;
391 case 0xfcba: strcpy(toshiba_model, "T2150"); break;
392 case 0xfcbe: strcpy(toshiba_model, "T4850CT"); break;
393 case 0xfcc0: strcpy(toshiba_model, "Satellite Pro 420x"); break;
394 case 0xfcc1: strcpy(toshiba_model, "Satellite 100x"); break;
395 case 0xfcc3: strcpy(toshiba_model, "Tecra 710x/720x"); break;
396 case 0xfcc6: strcpy(toshiba_model, "Satellite Pro 410x"); break;
397 case 0xfcca: strcpy(toshiba_model, "Satellite Pro 400x"); break;
398 case 0xfccb: strcpy(toshiba_model, "Portage 610CT"); break;
399 case 0xfccc: strcpy(toshiba_model, "Tecra 700x"); break;
400 case 0xfccf: strcpy(toshiba_model, "T4900CT"); break;
401 case 0xfcd0: strcpy(toshiba_model, "Satellite 300x"); break;
402 case 0xfcd1: strcpy(toshiba_model, "Tecra 750CDT"); break;
403 case 0xfcd2: strcpy(toshiba_model, "Vision Connect -- what is this???"); break;
404 case 0xfcd3: strcpy(toshiba_model, "Tecra 730XCDT"); break;
405 case 0xfcd4: strcpy(toshiba_model, "Tecra 510x"); break;
406 case 0xfcd5: strcpy(toshiba_model, "Satellite 200x"); break;
407 case 0xfcd7: strcpy(toshiba_model, "Satellite Pro 430x"); break;
408 case 0xfcd8: strcpy(toshiba_model, "Tecra 740x"); break;
409 case 0xfcd9: strcpy(toshiba_model, "Portage 660CDT"); break;
410 case 0xfcda: strcpy(toshiba_model, "Tecra 730CDT"); break;
411 case 0xfcdb: strcpy(toshiba_model, "Portage 620CT"); break;
412 case 0xfcdc: strcpy(toshiba_model, "Portage 650CT"); break;
413 case 0xfcdd: strcpy(toshiba_model, "Satellite 110x"); break;
414 case 0xfcdf: strcpy(toshiba_model, "Tecra 500x"); break;
415 case 0xfce0: strcpy(toshiba_model, "Tecra 780DVD"); break;
416 case 0xfce2: strcpy(toshiba_model, "Satellite 300x"); break;
417 case 0xfce3: strcpy(toshiba_model, "Satellite 310x"); break;
418 case 0xfce4: strcpy(toshiba_model, "Satellite Pro 490x"); break;
419 case 0xfce5: strcpy(toshiba_model, "Libretto 100CT"); break;
420 case 0xfce6: strcpy(toshiba_model, "Libretto 70CT"); break;
421 case 0xfce7: strcpy(toshiba_model, "Tecra 540x/550x"); break;
422 case 0xfce8: strcpy(toshiba_model, "Satellite Pro 470x/480x"); break;
423 case 0xfce9: strcpy(toshiba_model, "Tecra 750DVD"); break;
424 case 0xfcea: strcpy(toshiba_model, "Libretto 60"); break;
425 case 0xfceb: strcpy(toshiba_model, "Libretto 50CT"); break;
426 case 0xfcec: strcpy(toshiba_model, "Satellite 320x/330x, Satellite 2500CDS"); break;
427 case 0xfced: strcpy(toshiba_model, "Tecra 520x/530x"); break;
428 case 0xfcef: strcpy(toshiba_model, "Satellite 220x, Satellite Pro 440x/460x"); break;
429 default:
430 fprintf(stderr, "toshiba_lib: unrecognized machine identification:\n");
431 fprintf(stderr, " machine id : 0x%04x BIOS version : %d.%d SCI version: %d.%d\n",
432 id, (bios & 0xff00)>>8, bios & 0xff,
433 (version & 0xff00)>>8, version & 0xff);
434 fprintf(stderr, "please report this info, along with your toshiba model,\n");
435 fprintf(stderr, "to noberasco.gnu@disi.unige.it\n");
438 return 1;
446 /* INTERNAL FUNCTIONS */
449 int Get_Fan_Status(void)
451 SMMRegisters reg;
453 reg.eax = HCI_GET;
454 reg.ebx = HCI_FAN;
455 HciFunction(&reg);
457 if ((reg.eax & 0xff00)!=HCI_SUCCESS)
458 return -1;
459 else
460 return (int) (reg.ecx & 0xff);
463 void UglyHack(void)
465 unsigned short save=0,display=512,sleep=0,speed=1,cooling=1;
466 SMMRegisters reg;
467 int user;
469 SciOpenInterface();
470 reg.ebx = SCI_BATTERY_SAVE;
471 reg.ecx = save;
472 if (SciSet(&reg)==SCI_SUCCESS) user = 0;
473 else user = 1;
474 if ((save==SCI_USER_SETTINGS) || (user==1))
476 reg.ebx = SCI_DISPLAY_AUTO;
477 reg.ecx = display;
478 if (SciSet(&reg)==SCI_FAILURE) {}
479 reg.ebx = SCI_SLEEP_MODE;
480 reg.ecx = sleep;
481 if (SciSet(&reg)==SCI_FAILURE) {}
482 reg.ebx = SCI_PROCESSING;
483 reg.ecx = speed;
484 if (SciSet(&reg)==SCI_FAILURE) {}
485 reg.ebx = SCI_COOLING_METHOD;
486 reg.ecx = cooling;
487 if (SciSet(&reg)==SCI_FAILURE) {}
488 if (user==0)
490 reg.ebx = SCI_BATTERY_SAVE;
491 reg.ecx = 1;
492 if (SciSet(&reg)==SCI_FAILURE) {}
493 reg.ebx = SCI_BATTERY_SAVE;
494 reg.ecx = save;
495 if (SciSet(&reg)==SCI_FAILURE) {}
498 SciCloseInterface();
499 reg.eax = HCI_SET;
500 reg.ebx = HCI_FAN;
501 reg.ecx = HCI_DISABLE;
502 HciFunction(&reg);
503 usleep(100000);
504 SciOpenInterface();
505 reg.ebx = SCI_COOLING_METHOD;
506 reg.ecx = FAN_OFF;
507 SciCloseInterface();
508 usleep(100000);
510 return;