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 (477 lines) | stat: -rw-r--r-- 12,244 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477

/*********************************************************************
 *                
 * 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 <jdthood@mail.com>
 * Created:       19 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/module.h>
#include <linux/kmod.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#ifdef USE_PROC_FS
#include <linux/proc_fs.h>
#endif
#include <asm/uaccess.h>
#include <linux/wrapper.h>
#include "thinkpad_common.h"
#include "thinkpad.h"
#include "smapi.h"
#include "superio.h"
#include "rtcmosram.h"

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

#define SZ_THINKPAD_VERSION "1.0"
/* must be no longer than LEN_VERSION_MAX */

#define SZ_THINKPAD_NAME "thinkpad"

#define MINOR_THINKPAD 170

/****** forward declarations ******/
#ifdef USE_PROC_FS
#ifndef NEW_PROC
static int thinkpad_read_proc(
	char *pchBuf,
	char **ppchStart,
	off_t offThe,
	int intLen,
	int intUnused
);
#endif
#endif
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
);
void cleanup_module( void );
int init_module( void );

typedef enum _mod_t {
	MOD_SMAPI,
	MOD_SUPERIO,
	MOD_RTCMOSRAM
} mod_t;
typedef int (* pxdo_t)( unsigned long, flag_t );
typedef void (* pxregs_t)( void );

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

/*** module paramters ***/
int enable_smapi = 1;
int enable_superio = 1;
int enable_rtcmosram = 1;
MODULE_PARM( enable_smapi, "i" );
MODULE_PARM( enable_superio, "i" );
MODULE_PARM( enable_rtcmosram, "i" );
MODULE_AUTHOR( "Thomas Hood" );
MODULE_DESCRIPTION( "A driver for IBM ThinkPad configuration" );

/*** local ***/
static int cOpen  = 0;  /* count of opens */
static char _szThinkpadName[] = SZ_THINKPAD_NAME;
static char _szThinkpadVersion[] = SZ_THINKPAD_VERSION;
static char _szSmapiName[] = "smapi";
static char _szSuperioName[] = "superio";
static char _szRtcmosramName[] = "rtcmosram";
static flag_t _fThinkpadReady = 0;

static struct file_operations fileopsThinkpad = {
	NULL,  /* seek */
	NULL, /* read */
	NULL, /* write */
	NULL, /* readdir */
	NULL, /* select */
	thinkpad_ioctl,
	NULL, /* mmap */
	thinkpad_open,
	NULL, /* flush */
	thinkpad_release
};

#ifdef USE_PROC_FS
#ifndef NEW_PROC
/*
 * Used by all the modules:
 */
struct proc_dir_entry _proc_dir_entryThinkpadDir = {
	0,                  /* low_ino: the inode--dynamic */
	8, "thinkpad",      /* len of name, name */
	S_IFDIR | S_IRUGO | S_IXUGO,  /* mode */
	1, 0, 0,            /* nlinks, owner, group */
	0,                  /* size--unused */
	NULL,               /* point to operations--use default */
	NULL
};
static flag_t _fCreatedProcThinkpadDir, _fCreatedProcThinkpad;
static struct proc_dir_entry _proc_dir_entryThinkpad = {
	0,                  /* low_ino: the inode--dynamic */
	8, "thinkpad",      /* len of name, name */
	S_IFREG | S_IRUGO,  /* mode */
	1, 0, 0,            /* nlinks, owner, group */
	0,                  /* size--unused */
	NULL,               /* point to operations--use default */
	&thinkpad_read_proc
};
#endif
#endif

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

/*
 * Set up pointers to the service module ioctl handlers that are loaded
 * and also, if necessary and if enabled, load them.
 *
 * I believe we have to do this for each ioctl since we never know
 * when a module will get unloaded.  I presume, though, that we can
 * count on a module not being unloaded between locate_do_func() and
 * the function call!
 */
static pxdo_t locate_do_func( mod_t modWhich )
{
	pxdo_t pxdoThe;

	if ( modWhich == MOD_SMAPI ) {
		if ( enable_smapi ) {
			pxdoThe = (pxdo_t)get_module_symbol( _szSmapiName, "smapi_do" );
			if ( pxdoThe != NULL ) return pxdoThe;
			request_module( _szSmapiName );
			pxdoThe = (pxdo_t)get_module_symbol( _szSmapiName, "smapi_do" );
			return pxdoThe;
		} else return (pxdo_t)NULL;
	} else if ( modWhich == MOD_SUPERIO ) {
		if ( enable_superio ) {
			pxdoThe = (pxdo_t)get_module_symbol( _szSuperioName, "superio_do" );
			if ( pxdoThe != NULL ) return pxdoThe;
			request_module( _szSuperioName );
			pxdoThe = (pxdo_t)get_module_symbol( _szSuperioName, "superio_do" );
			return pxdoThe;
		} else return (pxdo_t)NULL;
	} else if ( modWhich == MOD_RTCMOSRAM ) {
		if ( enable_rtcmosram ) {
			pxdoThe = (pxdo_t)get_module_symbol( _szRtcmosramName, "rtcmosram_do" );
			if ( pxdoThe != NULL ) return pxdoThe;
			request_module( _szRtcmosramName );
			pxdoThe = (pxdo_t)get_module_symbol( _szRtcmosramName, "rtcmosram_do" );
			return pxdoThe;
		} else return (pxdo_t)NULL;
	}

	return (pxdo_t) NULL;
}

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

	if ( !_fThinkpadReady ) {
		return sprintf(
			pchBuf,
			"%s version %s not ready\n",
			_szThinkpadName, _szThinkpadVersion
		);
	}

	return sprintf(
		pchBuf,
		"%s version %s enabled to access modules: %s %s %s.\n",
		_szThinkpadName, _szThinkpadVersion,
		enable_smapi ? _szSmapiName : "",
		enable_superio ? _szSuperioName : "",
		enable_rtcmosram ? _szRtcmosramName : ""
	);
}
#endif

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

	if ( cOpen ) return -EBUSY;

	cOpen++;
	MOD_INC_USE_COUNT;

	return 0;
}


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

	cOpen--;
	MOD_DEC_USE_COUNT;

	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;

	if ( ! _fThinkpadReady )
		return -ETHINKPAD_EXECUTION;

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

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

		case IOCTL_THINKPAD_GETVER: 
			ulRtnCopy = copy_to_user(
				(byte *)ulongIoctlArg,
				_szThinkpadVersion,
				strlen( _szThinkpadVersion ) + 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", _szThinkpadName, 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 return -EINVAL;
		}

		case IOCTL_SMAPI_REQUEST: {
			pxdo_t pxdoSmapi;
			if ( ! enable_smapi ) return -ETHINKPAD_MODULE_DISABLED;
			pxdoSmapi = locate_do_func( MOD_SMAPI );
			if ( pxdoSmapi == NULL ) return -ETHINKPAD_MODULE_NOT_FOUND;
			return (* pxdoSmapi)(
				ulongIoctlArg, 
				caller_has_w( pfileThe )
			);
		}

		case IOCTL_SUPERIO_REQUEST:  {
			pxdo_t pxdoSuperio;
			if ( ! enable_superio ) return -ETHINKPAD_MODULE_DISABLED;
			pxdoSuperio = locate_do_func( MOD_SUPERIO );
			if ( pxdoSuperio == NULL ) return -ETHINKPAD_MODULE_NOT_FOUND;
			return (* pxdoSuperio)(
				ulongIoctlArg, 
				caller_has_w( pfileThe )
			);
		}

		case IOCTL_RTCMOSRAM_REQUEST: {
			pxdo_t pxdoRtcmosram;
			if ( ! enable_rtcmosram ) return -ETHINKPAD_MODULE_DISABLED;
			pxdoRtcmosram = locate_do_func( MOD_RTCMOSRAM );
			if ( pxdoRtcmosram == NULL ) return -ETHINKPAD_MODULE_NOT_FOUND;
			return (* pxdoRtcmosram)(
				ulongIoctlArg, 
				caller_has_w( pfileThe )
			);
		}

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

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


static struct miscdevice structmiscdeviceThinkpad = {
	MINOR_THINKPAD,
	SZ_THINKPAD_NAME,
	&fileopsThinkpad
};


int __init init_module( void )
{
	int intRtnMiscRegister;

	/*** register ***/
	intRtnMiscRegister = misc_register( &structmiscdeviceThinkpad );
	if ( intRtnMiscRegister ) {
		if ( intRtnMiscRegister == -EBUSY ) {
			printk(
				KERN_ERR
				"%s: error %d was returned by misc_register(): device is busy\n",
				_szThinkpadName, intRtnMiscRegister
			);
		} else {
			printk(
				KERN_ERR
				"%s: error %d was returned by misc_register()\n",
				_szThinkpadName, intRtnMiscRegister
			);
		}
		return intRtnMiscRegister;
	}

	/* this module has been registered successfully */

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

	_fThinkpadReady = 1;

#ifdef USE_PROC_FS
	/*** Add /proc entries ***/
#ifdef NEW_PROC
 	if ( !proc_mkdir( "thinkpad", 0 ) ) {
		printk( KERN_ERR "%s: could not proc_mkdir() /proc/thinkpad/\n", _szThinkpadName );
	} else { /* /proc/thinkpad was created. */
		if ( !create_proc_read_entry( "thinkpad/thinkpad", S_IFREG | S_IRUGO, NULL, thinkpad_read_proc, NULL ) ) {
			printk( KERN_ERR "%s: could not create_proc_read_entry() /proc/thinkpad/thinkpad\n", _szThinkpadName );
		}
	}
#else
	if ( proc_register( &proc_root, &_proc_dir_entryThinkpadDir ) ) {
		printk( KERN_ERR "%s: could not register /proc/thinkpad/\n", _szThinkpadName );
	} else {
		_fCreatedProcThinkpadDir = 1;
		if ( proc_register( &_proc_dir_entryThinkpadDir, &_proc_dir_entryThinkpad ) ) {
			printk( KERN_ERR "%s: could not register /proc/thinkpad/thinkpad\n", _szThinkpadName );
		} else {
			_fCreatedProcThinkpad = 1;
		}
	}
#endif
#endif

	return 0;
}
		

void cleanup_module( void )
{
	int intRtn;

	intRtn = misc_deregister( &structmiscdeviceThinkpad );

	if ( intRtn != 0 ) printk( KERN_ERR "%s: error reported by misc_deregister: %d\n", _szThinkpadName, intRtn );

	_fThinkpadReady = 0;

#ifdef USE_PROC_FS
	/*** Remove /proc entries ***/
#ifdef NEW_PROC
 	remove_proc_entry("thinkpad/thinkpad", NULL);
 	remove_proc_entry("thinkpad", NULL);
#else
	if ( _fCreatedProcThinkpad ) {
		_fCreatedProcThinkpad = 0;
		intRtn = proc_unregister( &_proc_dir_entryThinkpadDir, _proc_dir_entryThinkpad.low_ino );
		if ( intRtn ) {
			printk( KERN_ERR "%s: error returned by proc_unregister()\n", _szThinkpadName );
		} else {
			if ( _fCreatedProcThinkpadDir ) {
				_fCreatedProcThinkpadDir = 0;
				intRtn = proc_unregister( &proc_root, _proc_dir_entryThinkpadDir.low_ino );
				if ( intRtn ) printk( KERN_ERR "%s: error returned by proc_unregister()\n", _szThinkpadName );
			}
		}
	}
#endif
#endif

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

	return;
}