-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipod.c
More file actions
160 lines (144 loc) · 3.43 KB
/
ipod.c
File metadata and controls
160 lines (144 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* ipod.c - generic iPod library
*
* Copyright (C) 2005 Courtney Cavin, Alastair Stuart, Bernard Leach
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <unistd.h>
#include "ipod.h"
#include "ipodhardware.h"
long ipod_get_hw_version(void)
{
static long generation = -1;
if (generation == -1) {
int i;
char cpuinfo[256];
char *ptr;
FILE *file;
if ((file = fopen(CPUINFOLOC, "r")) != NULL) {
while (fgets(cpuinfo, sizeof(cpuinfo), file) != NULL)
if (strncmp(cpuinfo, "Revision", 8) == 0)
break;
fclose(file);
for (i = 0; !isspace(cpuinfo[i]); i++);
for (; isspace(cpuinfo[i]); i++);
ptr = cpuinfo + i + 2;
generation = strtol(ptr, NULL, 16);
} else {
generation = 0;
}
}
return generation;
}
char ipod_get_fs_type(void)
{
FILE *fp;
char ret = 'U';
char line[256];
char mount[32];
char fstype[32];
if ((fp = fopen(MOUNTSLOC, "r")) != NULL) {
while (fgets(line, 255, fp) != NULL) {
sscanf(line, "%s%*s%s", mount, fstype);
if (strcmp(mount, "/dev/root") == 0)
break;
}
if (strncmp(fstype, "hfs", 3) == 0)
ret = 'H';
else
ret = 'F';
fclose(fp);
}
return ret;
}
void ipod_beep(void)
{
#ifdef IPOD
if (ipod_get_hw_version() >= 0x40000) {
int i, j;
outl(inl(0x70000010) & ~0xc, 0x70000010);
outl(inl(0x6000600c) | 0x20000, 0x6000600c); /* enable device */
for (j = 0; j < 10; j++) {
for (i = 0; i < 0x888; i++ ) {
outl(0x80000000 | 0x800000 | i, 0x7000a000); /* set pitch */
}
}
outl(0x0, 0x7000a000); /* piezo off */
} else {
static int fd = -1;
static char buf;
if (fd == -1 && (fd = open("/dev/ttyS1", O_WRONLY)) == -1
&& (fd = open("/dev/tts/1", O_WRONLY)) == -1) {
return;
}
write(fd, &buf, 1);
}
#else
if (isatty(1)) {
printf("\a");
}
#endif
}
int ipod_read_apm(int *battery, int *charging)
{
#ifdef IPOD
FILE *file;
int ac_line_status = 0xff;
int battery_status = 0xff;
int battery_flag = 0xff;
int percentage = -1;
int time_units = -1;
if ((file = fopen(APMLOC, "r")) != NULL) {
fscanf(file, "%*s %*d.%*d 0x%*02x 0x%02x 0x%02x 0x%02x %d%% %d",
&ac_line_status, &battery_status,
&battery_flag, &percentage, &time_units);
fclose(file);
if (battery) {
*battery = time_units;
}
if (charging) {
*charging = (battery_status != 0xff && battery_status & 0x3) ? 1 : 0;
}
return 0;
}
if (battery) *battery = 512;
if (charging) *charging = 0;
#else
int t = time (0);
int u = t/2, v = u/4;
if (battery) {
if ((u % 4) == 0) {
*battery = 512;
} else {
srand ((unsigned)time (0));
*battery = rand() % 512;
}
}
if (charging) {
if ((v % 2) == 0) {
*charging = 1;
} else {
*charging = 0;
}
}
#endif
return 0;
}