File: rtcmosram.c

package info (click to toggle)
thinkpad 5.8-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 604 kB
  • ctags: 1,459
  • sloc: ansic: 5,985; makefile: 226; sh: 179; asm: 44; sed: 22
file content (304 lines) | stat: -rw-r--r-- 6,691 bytes parent folder | download | duplicates (2)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

/*********************************************************************
 *                
 * Filename:      rtcmosram.c
 * Description:   rtcmosram is a kernel module that serves to interface
 *                with the RT/CMOS RAM in IBM ThinkPads
 * Author:        Thomas Hood <jdthood@mail.com>
 * Created:       24 July 1999 
 *
 * Please report bugs to the author ASAP.
 * 
 *     Copyright (c) 1999 J.D. Thomas Hood, All rights reserved
 *     
 *     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.
 * 
 *     To receive a copy of the GNU General Public License, please write
 *     to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *     Boston, MA 02111-1307 USA
 *     
 ********************************************************************/

#include "thinkpad_mod_defines.h"

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/ioport.h>
#ifdef USE_PROC_FS
#include <linux/proc_fs.h>
#endif
#include <asm/io.h>
#include <asm/uaccess.h>
#include "thinkpad_common.h"
#include "rtcmosram.h"


/****** defines ******/

#define SZ_RTCMOSRAM_MODULE_VERSION "1.0"
#define SZ_RTCMOSRAM_MODULE_NAME "rtcmosram"


/****** declarations ******/

#ifdef USE_PROC_FS
#ifndef NEW_PROC
extern struct proc_dir_entry _proc_dir_entryThinkpadDir;
static int rtcmosram_read_proc(
	char *pchBuf,
	char **ppchStart,
	off_t offThe,
	int intLen,
	int intUnused
);
#endif
#endif


/****** variables ******/

static char _szRtcmosramName[] = SZ_RTCMOSRAM_MODULE_NAME;
static char _szRtcmosramVersion[] = SZ_RTCMOSRAM_MODULE_VERSION;
static flag_t _fRtcmosramReady = 0;

#ifdef USE_PROC_FS
#ifndef NEW_PROC
static flag_t _fCreatedProcRtcmosram = 0;
static struct proc_dir_entry _proc_dir_entryRtcmosram = {
	0,                  /* low_ino: the inode--dynamic */
	9, "rtcmosram",     /* len of name, name */
	S_IFREG | S_IRUGO,  /* mode */
	1, 0, 0,            /* nlinks, owner, group */
	0,                  /* size--unused */
	NULL,               /* point to operations--use default */
	&rtcmosram_read_proc
};
#endif
#endif


/****** functions *******/

/*
 * We use _p variants just for safety.
 */
static byte cmos_getb( byte bWhere )
{
	byte bGot;
	unsigned long flags;

	save_flags( flags ); cli();
	outb_p( bWhere, 0x70 );
	bGot = inb_p( 0x71 );
	outb_p( 0x0f, 0x70 ); /* required, according to Tech Ref */
	inb( 0x71 );
	restore_flags( flags );

	return bGot;
}


static void cmos_putb( byte bWhat, byte bWhere )
{
	unsigned long flags;

	save_flags( flags ); cli();
	outb_p( bWhere, 0x70 );
	outb_p( bWhat, 0x71 );
	outb_p( 0x0F, 0x70 ); /* required, according to Tech Ref */
	inb( 0x71 );
	restore_flags( flags );

	return;
}


#ifdef USE_PROC_FS
static int rtcmosram_read_proc(
	char *pchBuf, 
	char **ppchStart, 
	off_t offThe,
	int intLen, 
#ifdef NEW_PROC
	int *eof, 
	void *data
#else
	int intUnused
#endif
) {

	if ( !_fRtcmosramReady ) {
		return sprintf(
			pchBuf,
			"%s version %s not ready\n",
			_szRtcmosramName, _szRtcmosramVersion
		);
	}

	return sprintf(
		pchBuf,
		"%s version %s accessing RT CMOS RAM at ioports 0x%x, 0x%x\n",
		_szRtcmosramName, _szRtcmosramVersion,
		0x70, 0x71
	);
}
#endif


int rtcmosram_do( 
	unsigned long ulongIoctlArg,
	flag_t fCallerHasWritePerm
) {
	rtcmosram_ioparm_t ioparmMy;
	unsigned long ulRtnCopy;

	if ( !_fRtcmosramReady ) return -ETHINKPAD_EXECUTION;

	ulRtnCopy = copy_from_user(
		(byte *)&ioparmMy,
		(byte *)(rtcmosram_ioparm_t *)ulongIoctlArg,
		sizeof( ioparmMy )
	);
	if ( ulRtnCopy ) return -EFAULT;

	switch ( ioparmMy.in.wFunc ) {

		case RTCMOSRAM_FUNC_GETDATA: {
			rtcmosram_data_t dataThe;
			int i;

			for ( i=0; i<sizeof(dataThe); i++ )
				*(((byte *)&dataThe)+i) = cmos_getb( (byte)i );

			ulRtnCopy =  copy_to_user(
				(byte *)&(((rtcmosram_ioparm_t *)ulongIoctlArg)->data),
				(byte *)&dataThe,
				sizeof(dataThe)
			);
			if ( ulRtnCopy ) return -EFAULT;

			return 0;
		}

		case RTCMOSRAM_FUNC_DAYLIGHTSAVINGTIME_ABLIFY:
			if ( ! fCallerHasWritePerm )
				return -EACCES;

			if ( (flag_t)ioparmMy.in.dwParm1 ) {
				cmos_putb( cmos_getb(0xB) | 1, 0xB );
			} else {
				cmos_putb( cmos_getb(0xB) & ~1, 0xB );
			}
			return 0;

		case RTCMOSRAM_FUNC_DAYLIGHTSAVINGTIME_GET: {
			byte bGot;
			bGot = cmos_getb( 0xB );
			ioparmMy.out.wRtn = 0;
			ioparmMy.out.dwParm1 = (bGot & 1) ? (dword)1 : (dword)0;
			ulRtnCopy = copy_to_user(
				(byte *)(rtcmosram_ioparm_t *)ulongIoctlArg,
				(byte *)&ioparmMy,
				sizeof(ioparmMy)
			);
			if ( ulRtnCopy ) return -EFAULT;
			return 0;
		}

		default:
			printk(
				KERN_ERR
				"%s: function %d not recognized\n",
				_szRtcmosramName, ioparmMy.in.wFunc
			);
			return -EINVAL;
	} /* switch */

	return -ETHINKPAD_PROGRAMMING; /* we should not reach here */
}


int init_module( void )
{
	int intRtn;

	intRtn = check_region( 0x70, 2 );
	if ( intRtn < 0 ) {
#if 0
/*
 * In some configs this region
 * is already claimed by "rtc"
 */
		printk(
			KERN_ERR
			"%s: io ports not available\n",
			_szRtcmosramName
		);
		return -EBUSY;
#endif
	} else {
		request_region( 0x70, 2, _szRtcmosramName );
	}

	_fRtcmosramReady = 1;

#ifdef USE_PROC_FS
	/*** Add /proc entry ***/
#ifdef NEW_PROC
	if ( !create_proc_read_entry(
		"thinkpad/rtcmosram",
		S_IFREG | S_IRUGO,
		NULL,
		rtcmosram_read_proc,
		NULL
	) ) {
		printk( KERN_ERR "%s: Could not create_proc_read_entry() /proc/thinkpad/rtcmosram\n", _szRtcmosramName );
	}
#else
	_fCreatedProcRtcmosram = 0;
	intRtn = proc_register( &_proc_dir_entryThinkpadDir, &_proc_dir_entryRtcmosram );
	if ( intRtn ) {
		printk( KERN_ERR "%s: error returned by proc_register()\n", _szRtcmosramName );
	} else {
		_fCreatedProcRtcmosram = 1;
	}
#endif
#endif

	return 0;
}


void cleanup_module( void )
{

	if ( !_fRtcmosramReady ) return;

	_fRtcmosramReady = 0;

	release_region( 0x70, 2 );

#ifdef USE_PROC_FS
	/*** Remove /proc entry ***/
#ifdef NEW_PROC
 	remove_proc_entry( "thinkpad/rtcmosram", NULL );
#else
	if ( _fCreatedProcRtcmosram ) {
		int intRtn;
		_fCreatedProcRtcmosram = 0;
		intRtn = proc_unregister( &_proc_dir_entryThinkpadDir, _proc_dir_entryRtcmosram.low_ino );
		if ( intRtn ) printk( KERN_ERR "%s: error returned by proc_unregister()\n", _szRtcmosramName );
	}
#endif
#endif

	return;
}