File: thinkpad.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 (374 lines) | stat: -rw-r--r-- 10,210 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374

/*********************************************************************
 *                
 * Filename:      thinkpad.c
 * Description:   loadable kernel module that serves as a router of ioctl
 *                requests to other modules that control various hardware
 *                features of IBM ThinkPads
 * Author:        Thomas Hood
 * Created:       19 July 1999 
 *
 * Notes:  This driver exists so that several thinkpad devices can be
 *         driven with only a single permanent misc device minor number
 *         allocated.
 *
 * 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"

#define EXPORT_SYMTAB

#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/version.h>
#include <asm/uaccess.h>
/* #include <linux/wrapper.h> */
#include "thinkpad_common.h"
#include "thinkpad.h"
#include "smapi.h"
#include "superio.h"
#include "rtcmosram.h"
#include "thinkpadpm.h"

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

#define MINOR_THINKPAD 170

/****** forward declarations ******/
static int thinkpad_open(
	struct inode * pinodeThe,
	struct file * pfileThe
);
static int thinkpad_release(
	struct inode * pinodeThe,
	struct file * pfileThe 
);
static int thinkpad_ioctl(
	struct inode * pinodeThe,
	struct file * pfileThe,
	unsigned int uintIoctlNum,
	unsigned long ulongIoctlArg
);

typedef enum _mod_t {
	MOD_SMAPI,
	MOD_SUPERIO,
	MOD_RTCMOSRAM,
	MOD_THINKPADPM
} mod_t;

/* pointer to an executable "do" function returning an integer */
typedef int (* pxint_do_t)( unsigned long, flag_t );

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

/*** global ***/
struct proc_dir_entry *thinkpad_ppde;

/*** module parameters ***/
static int enable_smapi = 1;
static int enable_superio = 1;
static int enable_rtcmosram = 1;
static int enable_thinkpadpm = 1;
#ifdef MODULE
MODULE_PARM( enable_smapi, "i" );
MODULE_PARM_DESC( enable_smapi, "Enable/disable (1/0) use of the smapi module" );
MODULE_PARM( enable_superio, "i" );
MODULE_PARM_DESC( enable_superio, "Enable/disable (1/0) use of the superio module" );
MODULE_PARM( enable_rtcmosram, "i" );
MODULE_PARM_DESC( enable_rtcmosram, "Enable/disable (1/0) use of the rtcmosram module" );
MODULE_PARM( enable_thinkpadpm, "i" );
MODULE_PARM_DESC( enable_thinkpadpm, "Enable/disable (1/0) use of the thinkpadpm module" );
MODULE_AUTHOR( "Thomas Hood" );
MODULE_DESCRIPTION( "Metadriver for IBM ThinkPad hardware drivers" );
MODULE_LICENSE( "GPL" );
#endif

/*** local ***/
static const char _szMyName[] = "thinkpad";
static const char _szMyVersion[] = "5.5";
static const char _szSmapiName[] = "smapi";
static const char _szSuperioName[] = "superio";
static const char _szRtcmosramName[] = "rtcmosram";
static const char _szThinkpadpmName[] = "thinkpadpm";
static flag_t _fInUse = 1;

static struct file_operations _fileopsThinkpad = {
	.ioctl   = thinkpad_ioctl,
	.open    = thinkpad_open,
	.release = thinkpad_release,
	.owner   = THIS_MODULE
};


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

static int thinkpad_read_proc(
	char *pchBuf, 
	char **ppchStart, 
	off_t off,
	int intCount, 
	int *pintEof, 
	void *pdata
) {
	return snprintf(
		pchBuf, intCount,
		"%s version %s enabled to access modules: %s %s %s %s.\n",
		_szMyName, _szMyVersion,
		enable_smapi ? _szSmapiName : "",
		enable_superio ? _szSuperioName : "",
		enable_rtcmosram ? _szRtcmosramName : "",
		enable_thinkpadpm ? _szThinkpadpmName : ""
	);
}

static int thinkpad_open(
	struct inode * pinodeThe,
	struct file * pfileThe
) {

	if ( _fInUse ) return -EBUSY;

	_fInUse = 1;

	return 0;
}


static int thinkpad_release(
	struct inode * pinodeThe,
	struct file * pfileThe
) {

	_fInUse = 0;

	return 0;
}


static flag_t caller_has_w( struct file * pfileThe )
{
	return ((pfileThe->f_mode) & FMODE_WRITE) ? 1 : 0;
}


static int thinkpad_ioctl(
	struct inode * pinodeThe,
	struct file * pfileThe,
	unsigned int uintIoctlNum,
	unsigned long ulongIoctlArg
) {
	unsigned long ulRtnCopy;

#ifdef DEBUG_VERBOSE
	printk( "%s: Doing ioctl number 0x%x\n", _szMyName, uintIoctlNum );
#endif

	switch ( uintIoctlNum ) {
		/* should  return  at the end of each case block */

		case IOCTL_THINKPAD_GETVER: 
			ulRtnCopy = copy_to_user(
				(byte *)ulongIoctlArg,
				_szMyVersion,
				strlen( _szMyVersion ) + 1
			);
			if ( ulRtnCopy ) return -EFAULT;
			return 0;

		case IOCTL_THINKPAD_ENABLE:
		case IOCTL_THINKPAD_DISABLE: {
			long lRes;
			char szBuf[LEN_NAME_MAX+1];
			flag_t fEnable;

			fEnable = ( uintIoctlNum == IOCTL_THINKPAD_ENABLE ) ? 1 : 0;

			if ( !caller_has_w( pfileThe ) ) return -EACCES;

			lRes = strncpy_from_user(
				szBuf,
				(char *)ulongIoctlArg,
				LEN_NAME_MAX
			);
			szBuf[LEN_NAME_MAX] = '\0'; /* ensure termination */

			printk(KERN_INFO
				"%s: %sabling %s\n", _szMyName, fEnable?"En":"Dis", szBuf
			);

			if ( strnicmp( szBuf, _szSmapiName, LEN_NAME_MAX ) == 0 ) {
				enable_smapi = fEnable; return 0;
			} else if ( strnicmp( szBuf, _szSuperioName, LEN_NAME_MAX ) == 0 ) {
				enable_superio = fEnable; return 0;
			} else if ( strnicmp( szBuf, _szRtcmosramName, LEN_NAME_MAX ) == 0 ) {
				enable_rtcmosram = fEnable; return 0; 
			} else if ( strnicmp( szBuf, _szThinkpadpmName, LEN_NAME_MAX ) == 0 ) {
				enable_thinkpadpm = fEnable; return 0; 
			}
			return -EINVAL;
		}

		case IOCTL_SMAPI_REQUEST: {
			pxint_do_t pxint_doSmapi;
			int intRet;
			if ( ! enable_smapi ) return -ETHINKPAD_MODULE_DISABLED;
			pxint_doSmapi = (pxint_do_t)inter_module_get_request( "smapi_do" , _szSmapiName );
			if ( pxint_doSmapi == NULL ) return -ETHINKPAD_MODULE_NOT_FOUND;
			intRet = (*pxint_doSmapi)(
				ulongIoctlArg, 
				caller_has_w( pfileThe )
			);
			inter_module_put("smapi_do");
			if ( intRet > 0 ) intRet = -ETHINKPAD_PROGRAMMING;
			return intRet;
		}

		case IOCTL_SUPERIO_REQUEST:  {
			pxint_do_t pxint_doSuperio;
			int intRet;
			if ( ! enable_superio ) return -ETHINKPAD_MODULE_DISABLED;
			pxint_doSuperio = (pxint_do_t)inter_module_get_request( "superio_do" , _szSuperioName );
			if ( pxint_doSuperio == NULL ) return -ETHINKPAD_MODULE_NOT_FOUND;
			intRet = (*pxint_doSuperio)(
				ulongIoctlArg, 
				caller_has_w( pfileThe )
			);
			inter_module_put("superio_do");
			if ( intRet > 0 ) intRet = -ETHINKPAD_PROGRAMMING;
			return intRet;
		}

		case IOCTL_RTCMOSRAM_REQUEST: {
			pxint_do_t pxint_doRtcmosram;
			int intRet;
			if ( ! enable_rtcmosram ) return -ETHINKPAD_MODULE_DISABLED;
			pxint_doRtcmosram = (pxint_do_t)inter_module_get_request( "rtcmosram_do" , _szRtcmosramName );
			if ( pxint_doRtcmosram == NULL ) return -ETHINKPAD_MODULE_NOT_FOUND;
			intRet = (*pxint_doRtcmosram)(
				ulongIoctlArg, 
				caller_has_w( pfileThe )
			);
			inter_module_put("rtcmosram_do");
			if ( intRet > 0 ) intRet = -ETHINKPAD_PROGRAMMING;
			return intRet;
		}

		case IOCTL_THINKPADPM_REQUEST: {
			pxint_do_t pxint_doThinkpadpm;
			int intRet;
			if ( ! enable_thinkpadpm ) return -ETHINKPAD_MODULE_DISABLED;
			pxint_doThinkpadpm = (pxint_do_t)inter_module_get_request( "thinkpadpm_do" , _szThinkpadpmName );
			if ( pxint_doThinkpadpm == NULL ) return -ETHINKPAD_MODULE_NOT_FOUND;
			intRet = (*pxint_doThinkpadpm)(
				ulongIoctlArg, 
				caller_has_w( pfileThe )
			);
			inter_module_put("thinkpadpm_do");
			if ( intRet > 0 ) intRet = -ETHINKPAD_PROGRAMMING;
			return intRet;
		}

		default:
			/* ioctl number not recognized -- do nothing */
			return -ENOTTY;

	} /* switch */
			
	return -ETHINKPAD_PROGRAMMING; /* We should never arrive here */
}


static struct miscdevice _structmiscdeviceThinkpad = {
	.minor = MINOR_THINKPAD,
	.name  = "thinkpad",
	.fops  = &_fileopsThinkpad
};

static int __init thinkpad_init( void )
{
	int intRtnMiscRegister;

	intRtnMiscRegister = misc_register( &_structmiscdeviceThinkpad );
	if ( intRtnMiscRegister ) {
		if ( intRtnMiscRegister == -EBUSY ) {
			printk(KERN_ERR
				"%s: Error %d was returned by misc_register(): device is busy\n",
				_szMyName, intRtnMiscRegister
			);
		} else {
			printk(KERN_ERR
				"%s: Error %d was returned by misc_register()\n",
				_szMyName, intRtnMiscRegister
			);
		}
		return intRtnMiscRegister;
	}

	/* this module has been registered successfully */

	printk(KERN_INFO
		"%s: I have registered to handle major: %d minor: %d.\n",
		_szMyName, 10, MINOR_THINKPAD
	);

 	if ( (thinkpad_ppde = proc_mkdir( "driver/thinkpad", NULL )) == NULL ) {
		printk(KERN_ERR "%s: Could not proc_mkdir() /proc/driver/thinkpad/\n", _szMyName );
	} else { /* /proc/thinkpad was created. */
		if ( !create_proc_read_entry( "driver/thinkpad/thinkpad", S_IFREG | S_IRUGO, NULL, thinkpad_read_proc, NULL )) {
			printk(KERN_ERR "%s: Could not create_proc_read_entry() /proc/driver/thinkpad/thinkpad\n", _szMyName );
		}
	}
	/* proc entries created */

	_fInUse = 0;

	return 0;
}
		

static void __exit thinkpad_exit( void )
{
	int intRtn;

 	remove_proc_entry( "driver/thinkpad/thinkpad", NULL );
 	remove_proc_entry( "driver/thinkpad", NULL );

	intRtn = misc_deregister( &_structmiscdeviceThinkpad );
	if ( intRtn != 0 ) printk(KERN_ERR "%s: Error reported by misc_deregister: %d\n", _szMyName, intRtn );

	printk(KERN_INFO
		"%s: I have cleaned up.\n",
		_szMyName
	);

	return;
}

EXPORT_SYMBOL(thinkpad_ppde);
module_init(thinkpad_init);
module_exit(thinkpad_exit);