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 (248 lines) | stat: -rw-r--r-- 5,774 bytes parent folder | download
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

/*********************************************************************
 *                
 * Filename:      rtcmosram.c
 * Description:   rtcmosram is a kernel module that serves to interface
 *                with the RT/CMOS RAM in IBM ThinkPads
 * Author:        Thomas Hood
 * 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_driver.h"

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/ioport.h>
#include <linux/proc_fs.h>
#include <linux/mc146818rtc.h>
#include <linux/version.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include "thinkpad_common.h"
#include "rtcmosram.h"


/****** definitions ******/


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

extern struct proc_dir_entry *thinkpad_ppde;

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

static const char _szMyName[] = "rtcmosram";
static const char _szImName[] = "rtcmosram_do";
static const char _szMyVersion[] = "5.0";
static const char _szProcfile[] = "driver/thinkpad/rtcmosram";
static struct resource *_presourceRtcmosram;

#ifdef MODULE
MODULE_AUTHOR( "Thomas Hood" );
MODULE_DESCRIPTION( "Driver for the RT CMOS RAM device on IBM ThinkPads" );
MODULE_LICENSE( "GPL" );
#endif


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

/*
 * We use _p variants just for safety.  The rtc_lock spinlock keeps us
 * from fighting with the rtc driver.
 */
static byte cmos_getb( byte bWhere )
{
	byte bGot;
	unsigned long flags;

	spin_lock_irqsave(&rtc_lock,flags);
	outb_p( bWhere, 0x70 );
	bGot = inb_p( 0x71 );
	outb_p( 0x0f, 0x70 ); /* required, according to Tech Ref */
	inb( 0x71 );
	spin_unlock_irqrestore(&rtc_lock,flags);

	return bGot;
}


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

	spin_lock_irqsave(&rtc_lock,flags);
	outb_p( bWhere, 0x70 );
	outb_p( bWhat, 0x71 );
	outb_p( 0x0F, 0x70 ); /* required, according to Tech Ref */
	inb( 0x71 );
	spin_unlock_irqrestore(&rtc_lock,flags);

	return;
}


static int rtcmosram_read_proc(
	char *pchBuf, 
	char **ppchStart, 
	off_t off,
	int intCount, 
	int *pintEof, 
	void *data
) {
	return snprintf(
		pchBuf, intCount,
		"%s version %s accessing RT CMOS RAM at ioports 0x%x, 0x%x\n",
		_szMyName, _szMyVersion,
		0x70, 0x71
	);
}


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

	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",
				_szMyName, ioparmMy.in.wFunc
			);
			return -EINVAL;
	} /* switch */

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


static int __init rtcmosram_init( void )
{

	_presourceRtcmosram = request_region( 0x70, 2, _szMyName );
	if ( _presourceRtcmosram == NULL ) {
#if 0
		printk(KERN_ERR
			"%s: I/O ports for RT CMOS RAM not available -- aborting.\n",
			_szMyName
		);
		return -EBUSY;
#else
		/*
		 * In some configs this region is already claimed by "rtc"
		 * Don't worry about it, since we take rtc_lock when we
		 * access the CMOS RAM
		 */
		printk(KERN_INFO
			"%s: I/O ports for RT CMOS RAM not available, but ignoring this.\n",
			_szMyName
		);
#endif
	}

	if ( !thinkpad_ppde || !create_proc_read_entry(
		_szProcfile,
		S_IFREG | S_IRUGO,
		NULL,
		rtcmosram_read_proc,
		NULL
	) ) {
		printk(KERN_ERR
			"%s: Could not create_proc_read_entry() /proc/%s\n",
			_szMyName, _szProcfile
		);
	}
	/* proc entry created */

	inter_module_register( _szImName, THIS_MODULE, &rtcmosram_do );

	return 0;
}


static void __exit rtcmosram_exit( void )
{

	inter_module_unregister( _szImName );

 	remove_proc_entry( _szProcfile, NULL );

	if ( _presourceRtcmosram != NULL ) release_resource( _presourceRtcmosram );

	return;
}

module_init(rtcmosram_init);
module_exit(rtcmosram_exit);