summaryrefslogtreecommitdiffstats
path: root/drivers/char/joystick/joystick.c
blob: aff1acd173c35bc2dca5167196844b09cac5d26b (plain)
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
/*
 * joystick.c  Version 1.2
 *
 * Copyright (c) 1996-1998 Vojtech Pavlik
 */

/*
 * This is the main joystick driver for Linux. It doesn't support any
 * devices directly, rather is lets you use sub-modules to do that job. See
 * Documentation/joystick.txt for more info.
 */

/*
 * 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
 * 
 * Should you need to contact me, the author, you can do so either by
 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
 */

#include <asm/io.h>
#include <asm/system.h>
#include <asm/segment.h>
#include <linux/config.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/joystick.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/malloc.h>
#include <linux/mm.h>
#include <linux/module.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
#include <asm/spinlock.h>
#include <linux/poll.h>
#endif

/*
 * Configurable parameters.
 */

#define JS_REFRESH_TIME		HZ/50	/* Time between two reads of joysticks (20ms) */

/*
 * Buffer macros.
 */

#define ROT(A,B,C)	((((A)<(C))&&(((B)>(A))&&((B)<(C))))||(((A)>(C))&&(((B)>(A))||((B)<(C)))))
#define GOF(X)		(((X)==JS_BUFF_SIZE-1)?0:(X)+1)
#define GOB(X)		((X)?(X)-1:JS_BUFF_SIZE-1)
#define DIFF(X,Y)	((X)>(Y)?(X)-(Y):(Y)-(X))

/*
 * Global variables.
 */

static struct JS_DATA_SAVE_TYPE js_comp_glue;
static struct js_port  *js_port  = NULL;
static struct js_dev   *js_dev   = NULL;
static struct timer_list js_timer;
spinlock_t js_lock = SPIN_LOCK_UNLOCKED;
static int js_use_count = 0;

/*
 * Exported variables.
 */

unsigned int js_time_speed = 0;
js_time_func js_get_time;
js_delta_func js_delta;

unsigned int js_time_speed_a = 0;
js_time_func js_get_time_a;
js_delta_func js_delta_a;

/*
 * Module info.
 */

MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_SUPPORTED_DEVICE("js");

/*
 * js_get_time_*() are different functions to get current time.
 * js_delta_*() are functions to compute time difference.
 */

#ifdef __i386__

static unsigned int js_get_time_rdtsc(void)
{
	unsigned int x;
	__asm__ __volatile__ ( "rdtsc" : "=A" (x) );
	return x;
}

static unsigned int js_get_time_pit(void)
{
	unsigned long flags;
	unsigned int x;

	__save_flags(flags);
	__cli();
	outb(0, 0x43);
	x = inb(0x40);
	x |= inb(0x40) << 8;
	__restore_flags(flags);

	return x;
}

static int js_delta_pit(unsigned int x, unsigned int y)
{
	return y - x + ( y < x ? 1193180L / HZ : 0 );
}

static unsigned int js_get_time_counter(void)
{
	static int time_counter = 0;
	return time_counter++;
}

#else
#ifdef __alpha__

static unsigned int js_get_time_rpcc(void)
{
	unsigned int x;
	__asm__ __volatile__ ( "rpcc %0" : "=r" (x) );
	return x;
}

#else

#ifndef MODULE
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
static unsigned int js_get_time_system(void)
{
	static struct timeval js_tv;
	get_fast_time(&js_tv);
	return js_tv.tv_sec * 1000000L + js_tv.tv_usec;
}
#endif
#endif

#endif
#endif

static int js_delta_normal(unsigned int x, unsigned int y)
{
	return x - y;
}

/*
 * js_calibrate_time() calibrates a given timer.
 */

static int __init js_calibrate_time(js_time_func get_time, js_delta_func delta)
{
	unsigned int t1, t2, t3;
	unsigned long flags;

	__save_flags(flags);
	__cli();
	t1 = get_time();
	udelay(1000);
	t2 = get_time();
	t3 = get_time();
	__restore_flags(flags);

	return delta(t2, t1) - delta(t3, t2);
}

/*
 * js_calibrate_time_counter() calibrates the counter timer, which can't
 * be calibrated using the above function.
 */

#ifdef __i386__

static int __init js_calibrate_time_counter(void)
{
	unsigned int i, j, t1, t2, t3;

	j = jiffies; do { inb(0x201); t1 = js_get_time_counter(); } while (j == jiffies);
	j = jiffies; do { inb(0x201); t2 = js_get_time_counter(); } while (j == jiffies);

	j = (t2 - t1) * HZ / 1000;

	t1 = js_get_time_pit();
	for (i = 0; i < 1000; i++) {
		inb(0x201);
		js_get_time_counter();
	}
	t2 = js_get_time_pit();
	t3 = js_get_time_pit();

	i = 1193180L / (js_delta_pit(t2, t1) - js_delta_pit(t3, t2));

	if (DIFF(i,j) > 5)
		printk(KERN_WARNING "js: Counter timer calibration unsure,"
			" pass1 (0.%d MHz) and pass2 (0.%d MHz) differ.\n", j, i);

	return (i + j) >> 1;
}

#endif

/*
 * js_setup_time chooses the best available timers
 * on the system and calibrates them.
 */

static int __init js_setup_time(void)
{
	int t;
	char *name, *name_a;

	name = "";
	name_a = "";
	js_time_speed = 0;
	js_time_speed_a = 0;

#ifdef __i386__

	t = js_calibrate_time(js_get_time_pit, js_delta_pit);

	if (DIFF(t, 1193) > 5)
		printk(KERN_WARNING "js: Measured PIT speed is %d.%03d MHz, but should be 1.193 MHz.\n"
		       KERN_WARNING "js: This is probably caused by wrong BogoMIPS value. It is: %ld, should be: %ld.\n",
			t / 1000, t % 1000, loops_per_sec / 500000, loops_per_sec / (t * 500000 / 1193));

	if (JS_HAS_RDTSC && (t = js_calibrate_time(js_get_time_rdtsc, js_delta_normal)) > 0) {

		js_time_speed_a = t;
		js_get_time_a = js_get_time_rdtsc;
		js_delta_a = js_delta_normal;
		js_time_speed = t;
		js_get_time = js_get_time_rdtsc;
		js_delta = js_delta_normal;
		name = "RDTSC";

	} else {

		js_time_speed_a = t;
		js_get_time_a = js_get_time_pit;
		js_delta_a = js_delta_pit;
		name_a = "PIT";

		t = js_calibrate_time_counter();

		js_time_speed = t;
		js_get_time = js_get_time_counter;
		js_delta = js_delta_normal;
		name = "counter";

	}

#else
#ifdef __alpha__

	t = js_calibrate_time(js_get_time_rpcc, js_delta_normal);

	js_time_speed_a = t;
	js_get_time_a = js_get_time_rpcc;
	js_delta_a = js_delta_normal;
	js_time_speed = t;
	js_get_time = js_get_time_rpcc;
	js_delta = js_delta_normal;
	name = "RPCC";

#else

#ifndef MODULE
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
	t = js_calibrate_time(js_get_time_system, js_delta_normal);

	js_time_speed_a = t;
	js_get_time_a = js_get_time_system;
	js_delta_a = js_delta_normal;
	js_time_speed = t;
	js_get_time = js_get_time_system;
	js_delta = js_delta_normal;
	name = "system";
#endif
#endif

#endif
#endif

	printk(KERN_INFO "js: Version %d.%d.%d ",
		JS_VERSION >> 16 & 0xff, JS_VERSION >> 8 & 0xff, JS_VERSION & 0xff);

	if (js_time_speed_a <= 0 || js_time_speed <= 0) {
		printk("\n");
		return -1;
	}

	printk("using ");

	if (js_time_speed > 10000) {
		t = js_time_speed / 1000 + (js_time_speed % 1000 >= 500);
		printk("%d MHz ", t);
	} else {
		t = js_time_speed / 10 + (js_time_speed % 10 >= 5);
		printk("%d.%02d MHz ", t / 100, t % 100);
	}

	if (js_get_time_a != js_get_time) {
		t = js_time_speed_a / 10 + (js_time_speed_a % 10 >= 5);
		printk("%s timer and %d.%02d MHz %s timer.\n",
			name, t / 100, t % 100, name_a);
	} else {
		printk("%s timer.\n", name);
	}

	return 0;
}


/*
 * js_correct() performs correction of raw joystick data.
 */

static int js_correct(int value, struct js_corr *corr)
{
	switch (corr->type) {
		case JS_CORR_NONE:
			break;
		case JS_CORR_BROKEN:
			value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
				((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
				((corr->coef[2] * (value - corr->coef[0])) >> 14);
			break;

		default:
			return 0;
	}

	if (value < -32767) return -32767;
	if (value >  32767) return  32767;

	return value;
}

/*
 * js_button() returns value of button number i.
 */

static inline int js_button(int *buttons, int i)
{
	return (buttons[i >> 5] >> (i & 0x1f)) & 1;
}


/*
 * js_add_event() adds an event to the buffer. This requires additional
 * queue post-processing done by js_sync_buff.
 */

static void js_add_event(struct js_dev *jd, __u32 time, __u8 type, __u8 number, __s16 value)
{
	jd->buff[jd->ahead].time = time;
	jd->buff[jd->ahead].type = type;
	jd->buff[jd->ahead].number = number;
	jd->buff[jd->ahead].value = value;
	if (++jd->ahead == JS_BUFF_SIZE) jd->ahead = 0;
}

/*
 * js_flush_data() does the same as js_process_data, except for that it doesn't
 * generate any events - it just copies the data from new to cur.
 */

static void js_flush_data(struct js_dev *jd)
{
	int i;

	for (i = 0; i < ((jd->num_buttons - 1) >> 5) + 1; i++)
		jd->cur.buttons[i] = jd->new.buttons[i];
	for (i = 0; i < jd->num_axes; i++)
		jd->cur.axes[i] = jd->new.axes[i];
}

/*
 * js_process_data() finds changes in button states and axis positions and adds
 * them as events to the buffer.
 */

static void js_process_data(struct js_dev *jd)
{
	int i, t;

	for (i = 0; i < jd->num_buttons; i++)
	if ((t = js_button(jd->new.buttons, i)) != js_button(jd->cur.buttons, i)) {
		js_add_event(jd, jiffies, JS_EVENT_BUTTON, i, t);
		jd->cur.buttons[i >> 5] ^= (1 << (i & 0x1f));
	}

	for (i = 0; i < jd->num_axes; i++) {
		t = js_correct(jd->new.axes[i], &jd->corr[i]);
		if (((jd->corr[i].prec == -1) && t) ||
			((DIFF(jd->new.axes[i], jd->cur.axes[i]) > jd->corr[i].prec) &&
			(t != js_correct(jd->cur.axes[i], &jd->corr[i])))) {
			js_add_event(jd, jiffies, JS_EVENT_AXIS, i, t);
			jd->cur.axes[i] = jd->new.axes[i];
		}
	}
}

/*
 * js_sync_buff() checks for all overflows caused by recent additions to the buffer.
 * These happen only if some process is reading the data too slowly. It
 * wakes up any process waiting for data.
 */

static void js_sync_buff(struct js_dev *jd)
{
	struct js_list *curl = jd->list;

	if (jd->bhead != jd->ahead) {
		if(ROT(jd->bhead, jd->tail, jd->ahead) || (jd->tail == jd->bhead)) {
			while (curl) {
				if (ROT(jd->bhead, curl->tail, jd->ahead) || (curl->tail == jd->bhead)) {
					curl->tail = jd->ahead;
					curl->startup = 0;
				}
				curl = curl->next;
			}
			jd->tail = jd->ahead;
		}
		jd->bhead = jd->ahead;
		wake_up_interruptible(&jd->wait);
	}
}

/*
 * js_do_timer() acts as an interrupt replacement. It reads the data
 * from all ports and then generates events for all devices.
 */

static void js_do_timer(unsigned long data)
{
	struct js_port *curp = js_port;
	struct js_dev *curd = js_dev;
	unsigned long flags;

	while (curp != NULL) {
		curp->read(curp->info, curp->axes, curp->buttons);
		curp = curp->next;
	}

	spin_lock_irqsave(&js_lock, flags);

	while (curd != NULL) {
		if (data) {
			js_process_data(curd);
			js_sync_buff(curd);
		} else {
			js_flush_data(curd);
		}
		curd = curd->next;
	}

	spin_unlock_irqrestore(&js_lock, flags);

	js_timer.expires = jiffies + JS_REFRESH_TIME;
	add_timer(&js_timer);
}

/*
 * js_read() copies one or more entries from jsd[].buff to user
 * space.
 */

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
static ssize_t js_read(struct file *file, char *buf, size_t count, loff_t *ppos)
#else
static int js_read(struct inode *inode, struct file *file, char *buf, int count)
#endif
{
	struct wait_queue wait = { current, NULL };
	struct js_event *buff = (void *) buf;
	struct js_list *curl;
	struct js_dev *jd;
	unsigned long blocks = count / sizeof(struct js_event);
	int written = 0;
	int new_tail, orig_tail;
	int retval = 0;
	unsigned long flags;

	curl = file->private_data;
	jd = curl->dev;
	orig_tail = curl->tail;

/*
 * Check user data.
 */

	if (!blocks)
		return -EINVAL;

/*
 * Lock it.
 */

	spin_lock_irqsave(&js_lock, flags);

/*
 * Handle (non)blocking i/o.
 */
	if (count != sizeof(struct JS_DATA_TYPE)) {

		if (GOF(curl->tail) == jd->bhead && curl->startup == jd->num_axes + jd->num_buttons) {

			add_wait_queue(&jd->wait, &wait);
			current->state = TASK_INTERRUPTIBLE;

			while (GOF(curl->tail) == jd->bhead) {

				if (file->f_flags & O_NONBLOCK) {
					retval = -EAGAIN;
					break;
				}
				if (signal_pending(current)) {
					retval = -ERESTARTSYS;
					break;
				}

				spin_unlock_irqrestore(&js_lock, flags);
				schedule();
				spin_lock_irqsave(&js_lock, flags);

			}

			current->state = TASK_RUNNING;
			remove_wait_queue(&jd->wait, &wait);
		}

		if (retval) {
			spin_unlock_irqrestore(&js_lock, flags);
			return retval;
		}

/*
 * Initial state.
 */

		while (curl->startup < jd->num_axes + jd->num_buttons && written < blocks && !retval) {

			struct js_event tmpevent;

			if (curl->startup < jd->num_buttons) {
				tmpevent.type = JS_EVENT_BUTTON | JS_EVENT_INIT;
				tmpevent.value = js_button(jd->cur.buttons, curl->startup);
				tmpevent.number = curl->startup;
			} else {
				tmpevent.type = JS_EVENT_AXIS | JS_EVENT_INIT;
				tmpevent.value = js_correct(jd->cur.axes[curl->startup - jd->num_buttons],
								&jd->corr[curl->startup - jd->num_buttons]);
				tmpevent.number = curl->startup - jd->num_buttons;
			}

			tmpevent.time = jiffies * (1000/HZ);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
			if (copy_to_user(&buff[written], &tmpevent, sizeof(struct js_event)))
				retval = -EFAULT;
#else
			if (!(retval = verify_area(VERIFY_WRITE, &buff[written], sizeof(struct js_event))))
				memcpy_tofs(&buff[written], &tmpevent, sizeof(struct js_event));
#endif

			curl->startup++;
			written++;
		}

/*
 * Buffer data.
 */

		while ((jd->bhead != (new_tail = GOF(curl->tail))) && (written < blocks) && !retval) {

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
			if (copy_to_user(&buff[written], &jd->buff[new_tail], sizeof(struct js_event)))
				retval = -EFAULT;
			if (put_user((__u32)(jd->buff[new_tail].time * (1000/HZ)), &buff[written].time))
				retval = -EFAULT;
#else
			if (!(retval = verify_area(VERIFY_WRITE, &buff[written], sizeof(struct js_event)))) {
				memcpy_tofs(&buff[written], &jd->buff[new_tail], sizeof(struct js_event));
				put_user((__u32)(jd->buff[new_tail].time * (1000/HZ)), &buff[written].time);
			}
#endif
			curl->tail = new_tail;
			written++;
		}
	}

	else

/*
 * Handle version 0.x compatibility.
 */

	{
		struct JS_DATA_TYPE data;

		data.buttons = jd->new.buttons[0];
		data.x = jd->num_axes < 1 ? 0 :
			((js_correct(jd->new.axes[0], &jd->corr[0]) / 256) + 128) >> js_comp_glue.JS_CORR.x;
		data.y = jd->num_axes < 2 ? 0 :
			((js_correct(jd->new.axes[1], &jd->corr[1]) / 256) + 128) >> js_comp_glue.JS_CORR.y;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
		retval = copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
#else
		if (!(retval = verify_area(VERIFY_WRITE, buf, sizeof(struct JS_DATA_TYPE)))) {
			memcpy_tofs(buf, &data, sizeof(struct JS_DATA_TYPE));
		}
#endif

		curl->startup = 0;
		curl->tail = GOB(jd->bhead);
		if (!retval) retval = sizeof(struct JS_DATA_TYPE);
	}

/*
 * Check main tail and move it.
 */

	if (orig_tail == jd->tail) {
		new_tail = curl->tail;
		curl = jd->list;
		while (curl != NULL && curl->tail != jd->tail) {
			if (ROT(jd->bhead, new_tail, curl->tail) ||
				(jd->bhead == curl->tail)) new_tail = curl->tail;
			curl = curl->next;
		}
		if (curl == NULL) jd->tail = new_tail;
	}

	spin_unlock_irqrestore(&js_lock, flags);

	return retval ? retval : written * sizeof(struct js_event);
}

/*
 * js_poll() does select() support.
 */

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)

static unsigned int js_poll(struct file *file, poll_table *wait)
{
	struct js_list *curl = file->private_data;
	unsigned long flags;
	int retval = 0;
	poll_wait(file, &curl->dev->wait, wait);
	spin_lock_irqsave(&js_lock, flags);	
	if (GOF(curl->tail) != curl->dev->bhead ||
		curl->startup < curl->dev->num_axes + curl->dev->num_buttons) retval = POLLIN | POLLRDNORM;
	spin_unlock_irqrestore(&js_lock, flags);
	return retval;
}

#else

static int js_select(struct inode *inode, struct file *file, int sel_type, select_table *wait)
{
	struct js_list *curl = file->private_data;
	if (sel_type == SEL_IN) {
		if (GOF(curl->tail) != curl->dev->bhead) return 1;
		select_wait(&curl->dev->wait, wait);
	}
	return 0;
}

#endif

/*
 * js_ioctl handles misc ioctl calls.
 */

static int js_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	struct js_list *curl;
	struct js_dev *jd;
	int len;

	curl = file->private_data;
	jd = curl->dev;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)

	switch (cmd) {

/*
 * 0.x compatibility
 */

		case JS_SET_CAL:
			return copy_from_user(&js_comp_glue.JS_CORR, (struct JS_DATA_TYPE *) arg,
				sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
		case JS_GET_CAL:
			return copy_to_user((struct JS_DATA_TYPE *) arg, &js_comp_glue.JS_CORR,
				sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
		case JS_SET_TIMEOUT:
			return get_user(js_comp_glue.JS_TIMEOUT, (int *) arg);
		case JS_GET_TIMEOUT:
			return put_user(js_comp_glue.JS_TIMEOUT, (int *) arg);
		case JS_SET_TIMELIMIT:
			return get_user(js_comp_glue.JS_TIMELIMIT, (long *) arg);
		case JS_GET_TIMELIMIT:
			return put_user(js_comp_glue.JS_TIMELIMIT, (long *) arg);
		case JS_SET_ALL:
			return copy_from_user(&js_comp_glue, (struct JS_DATA_SAVE_TYPE *) arg,
						sizeof(struct JS_DATA_SAVE_TYPE)) ? -EFAULT : 0;
		case JS_GET_ALL:
			return copy_to_user((struct JS_DATA_SAVE_TYPE *) arg, &js_comp_glue,
						sizeof(struct JS_DATA_SAVE_TYPE)) ? -EFAULT : 0;

/*
 * 1.x ioctl calls
 */

		case JSIOCGVERSION:
			return put_user(JS_VERSION, (__u32 *) arg);
		case JSIOCGAXES:
			return put_user(jd->num_axes, (__u8 *) arg);
		case JSIOCGBUTTONS:
			return put_user(jd->num_buttons, (__u8 *) arg);
		case JSIOCSCORR:
			return copy_from_user(jd->corr, (struct js_corr *) arg,
						sizeof(struct js_corr) * jd->num_axes) ? -EFAULT : 0;
		case JSIOCGCORR:
			return copy_to_user((struct js_corr *) arg, jd->corr,
						sizeof(struct js_corr) * jd->num_axes) ? -EFAULT : 0;
		default:
			if ((cmd & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) == JSIOCGNAME(0)) {
				len = strlen(jd->name) + 1;
				if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
				if (copy_to_user((char *) arg, jd->name, len)) return -EFAULT;
				return len;
			}
	}

#else

	switch (cmd) {

/*
 * 0.x compatibility
 */

		case JS_SET_CAL:
			if (verify_area(VERIFY_READ, (struct JS_DATA_TYPE *) arg,
				sizeof(struct JS_DATA_TYPE))) return -EFAULT;
			memcpy_fromfs(&js_comp_glue.JS_CORR, (struct JS_DATA_SAVE_TYPE *) arg,
				sizeof(struct JS_DATA_TYPE));
			return 0;
		case JS_GET_CAL:
			if (verify_area(VERIFY_WRITE, (struct JS_DATA_TYPE *) arg,
				sizeof(struct JS_DATA_TYPE))) return -EFAULT;
			memcpy_tofs((struct JS_DATA_SAVE_TYPE *) arg, &js_comp_glue.JS_CORR,
				sizeof(struct JS_DATA_TYPE));
			return 0;
		case JS_SET_TIMEOUT:
			if (verify_area(VERIFY_READ, (int *) arg, sizeof(int))) return -EFAULT;
			js_comp_glue.JS_TIMEOUT = get_user((int *) arg);
			return 0;
		case JS_GET_TIMEOUT:
			if (verify_area(VERIFY_WRITE, (int *) arg, sizeof(int))) return -EFAULT;
			put_user(js_comp_glue.JS_TIMEOUT, (int *) arg);
			return 0;
		case JS_SET_TIMELIMIT:
			if (verify_area(VERIFY_READ, (long *) arg, sizeof(long))) return -EFAULT;
			js_comp_glue.JS_TIMELIMIT = get_user((long *) arg);
			return 0;
		case JS_GET_TIMELIMIT:
			if (verify_area(VERIFY_WRITE, (long *) arg, sizeof(long))) return -EFAULT;
			put_user(js_comp_glue.JS_TIMELIMIT, (long *) arg);
			return 0;
		case JS_SET_ALL:
			if (verify_area(VERIFY_READ, (struct JS_DATA_SAVE_TYPE *) arg,
				sizeof(struct JS_DATA_SAVE_TYPE))) return -EFAULT;
			memcpy_fromfs(&js_comp_glue, (struct JS_DATA_SAVE_TYPE *) arg,
				sizeof(struct JS_DATA_SAVE_TYPE));
			return 0;
		case JS_GET_ALL:
			if (verify_area(VERIFY_WRITE, (struct JS_DATA_SAVE_TYPE *) arg,
				sizeof(struct JS_DATA_SAVE_TYPE))) return -EFAULT;
			memcpy_tofs((struct JS_DATA_SAVE_TYPE *) arg, &js_comp_glue,
				sizeof(struct JS_DATA_SAVE_TYPE));
			return 0;

/*
 * 1.x ioctl calls
 */

		case JSIOCGVERSION:
			if (verify_area(VERIFY_WRITE, (__u32 *) arg, sizeof(__u32))) return -EFAULT;
			put_user(JS_VERSION, (__u32 *) arg);
			return 0;
		case JSIOCGAXES:
			if (verify_area(VERIFY_WRITE, (__u8 *) arg, sizeof(__u8))) return -EFAULT;
			put_user(jd->num_axes, (__u8 *) arg);
			return 0;
		case JSIOCGBUTTONS:
			if (verify_area(VERIFY_WRITE, (__u8 *) arg, sizeof(__u8))) return -EFAULT;
			put_user(jd->num_buttons, (__u8 *) arg);
			return 0;
		case JSIOCSCORR:
			if (verify_area(VERIFY_READ, (struct js_corr *) arg,
				sizeof(struct js_corr) * jd->num_axes)) return -EFAULT;
			memcpy_fromfs(jd->corr, (struct js_corr *) arg,
				sizeof(struct js_corr) * jd->num_axes);
			return 0;
		case JSIOCGCORR:
			if (verify_area(VERIFY_WRITE, (struct js_corr *) arg,
				sizeof(struct js_corr) * jd->num_axes)) return -EFAULT;
			memcpy_tofs((struct js_corr *) arg,
				jd->corr, sizeof(struct js_corr) * jd->num_axes);
			return 0;
		default:
			if ((cmd & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) == JSIOCGNAME(0)) {
				len = strlen(jd->name) + 1;
				if (verify_area(VERIFY_WRITE, (char *) arg, len)) return -EFAULT;
				if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
				memcpy_tofs((char *) arg, jd->name, len);
				return len;
			}
	}

#endif

	return -EINVAL;
}

/*
 * js_open() performs necessary initialization and adds
 * an entry to the linked list.
 */

static int js_open(struct inode *inode, struct file *file)
{
	struct js_list *curl, *new;
	struct js_dev *jd = js_dev;
	int i = MINOR(inode->i_rdev);
	unsigned long flags;
	int result; 

	if (MAJOR(inode->i_rdev) != JOYSTICK_MAJOR)
		return -EINVAL;

	spin_lock_irqsave(&js_lock, flags);

	while (i > 0 && jd != NULL) {
		jd = jd->next;
		i--;
	}

	spin_unlock_irqrestore(&js_lock, flags);

	if (jd == NULL) return -ENODEV;

	if ((result = jd->open(jd))) return result;

	MOD_INC_USE_COUNT;
	if (!js_use_count++) js_do_timer(0);

	if ((new = kmalloc(sizeof(struct js_list), GFP_KERNEL)) != NULL) {

		spin_lock_irqsave(&js_lock, flags);

		curl = jd->list;

		jd->list = new;
		jd->list->next = curl;
		jd->list->dev = jd;
		jd->list->startup = 0;
		jd->list->tail = GOB(jd->bhead);
		file->private_data = jd->list;

		spin_unlock_irqrestore(&js_lock, flags);

	} else {
		result = -ENOMEM;
	}

	return result;
}

/*
 * js_release() removes an entry from list and deallocates memory
 * used by it.
 */

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
static int js_release(struct inode *inode, struct file *file)
#else
static void js_release(struct inode *inode, struct file *file)
#endif
{
	struct js_list *curl = file->private_data;
	struct js_dev *jd = curl->dev;
	struct js_list **curp = &jd->list;
	int new_tail;
	unsigned long flags;

	spin_lock_irqsave(&js_lock, flags);

	while (*curp && (*curp != curl)) curp = &((*curp)->next);
	*curp = (*curp)->next;

	if (jd->list != NULL)
	if (curl->tail == jd->tail) {
		curl = jd->list;
		new_tail = curl->tail;
		while (curl != NULL && curl->tail != jd->tail) {
			if (ROT(jd->bhead, new_tail, curl->tail) ||
			       (jd->bhead == curl->tail)) new_tail = curl->tail;
			curl = curl->next;
		}
		if (!curl) jd->tail = new_tail;
	}

	spin_unlock_irqrestore(&js_lock, flags);

	kfree(file->private_data);

	if (!--js_use_count) del_timer(&js_timer);
	MOD_DEC_USE_COUNT;

	jd->close(jd);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
	return 0;
#endif
}

/*
 * js_dump_mem() dumps all data structures in memory.
 * It's used for debugging only.
 */

#if 0
static void js_dump_mem(void)
{

	struct js_port *curp = js_port;
	struct js_dev *curd = js_dev;
	int i;

	printk(",--- Dumping Devices:\n");
	printk("| js_dev = %x\n", (int) js_dev);

	while (curd != NULL) {
		printk("|  %s-device %x, next %x axes %d, buttons %d, port %x - %#x\n",
			curd->next ? "|":"`",
			(int) curd, (int) curd->next, curd->num_axes, curd->num_buttons, (int) curd->port, curd->port->io);
		curd = curd->next;
	}

	printk(">--- Dumping ports:\n");
	printk("| js_port = %x\n", (int) js_port);

	while (curp != NULL) {
		printk("|  %s-port %x, next %x, io %#x, devices %d\n",
			curp->next ? "|":"`",
			(int) curp, (int) curp->next, curp->io, curp->ndevs);
		for (i = 0; i < curp->ndevs; i++) {
			curd = curp->devs[i];
			if (curd)
			printk("|  %s %s-device %x, next %x axes %d, buttons %d, port %x\n",
				curp->next ? "|":" ", (i < curp->ndevs-1) ? "|":"`",
				(int) curd, (int) curd->next, curd->num_axes, curd->num_buttons, (int) curd->port);
			else
			printk("|  %s %s-device %x, not there\n",
				curp->next ? "|":" ", (i < curp->ndevs-1) ? "|":"`", (int) curd);

		}
		curp = curp->next;
	}

	printk("`--- Done\n");
}
#endif


struct js_port *js_register_port(struct js_port *port,
				void *info, int devs, int infos, js_read_func read)
{
	struct js_port **ptrp = &js_port;
	struct js_port *curp;
	void *all;
	int i;
	unsigned long flags;

	if ((all = kmalloc(sizeof(struct js_port) + 4 * devs * sizeof(void*) + infos, GFP_KERNEL)) == NULL)
		return NULL;

	curp = all;

	curp->next = NULL;
	curp->prev = port;
	curp->read = read;
	curp->ndevs = devs;

	curp->devs = all += sizeof(struct js_port);
	for (i = 0; i < devs; i++) curp->devs[i] = NULL;

	curp->axes = all += devs * sizeof(void*);
	curp->buttons = (void*) all += devs * sizeof(void*);
	curp->corr = all += devs * sizeof(void*);

	if (infos) {
		curp->info = all += devs * sizeof(void*); 
		memcpy(curp->info, info, infos);
	} else {
		curp->info = NULL;
	}

	spin_lock_irqsave(&js_lock, flags);

	while (*ptrp != NULL) ptrp=&((*ptrp)->next);
	*ptrp = curp;

	spin_unlock_irqrestore(&js_lock, flags);

	return curp;
}

struct js_port *js_unregister_port(struct js_port *port)
{
	struct js_port **curp = &js_port;
	struct js_port *prev;
	unsigned long flags;

	spin_lock_irqsave(&js_lock, flags);

	while (*curp != NULL && (*curp != port)) curp = &((*curp)->next);
	*curp = (*curp)->next;

	spin_unlock_irqrestore(&js_lock, flags);

	prev = port->prev;
	kfree(port);

	return prev;
}

int js_register_device(struct js_port *port, int number, int axes, int buttons, char *name,
					js_ops_func open, js_ops_func close)
{
	struct js_dev **ptrd = &js_dev;
	struct js_dev *curd;
	void *all;
	int i = 0;
	unsigned long flags;

	if ((all = kmalloc(sizeof(struct js_dev) + 2 * axes * sizeof(int) +
			2 * (((buttons - 1) >> 5) + 1) * sizeof(int) +
			axes * sizeof(struct js_corr) + strlen(name) + 1, GFP_KERNEL)) == NULL)
		return -1;

	curd = all;

	curd->next = NULL;
	curd->list = NULL;
	curd->port = port;
	curd->wait = NULL;
	curd->open = open;
	curd->close = close;

	curd->ahead = 0;
	curd->bhead = 0;
	curd->tail = JS_BUFF_SIZE - 1;
	curd->num_axes = axes;
	curd->num_buttons = buttons;

	curd->cur.axes = all += sizeof(struct js_dev);
	curd->cur.buttons = all += axes * sizeof(int);
	curd->new.axes = all += (((buttons - 1) >> 5) + 1) * sizeof(int);
	curd->new.buttons = all += axes * sizeof(int);
	curd->corr = all += (((buttons -1 ) >> 5) + 1) * sizeof(int);

	curd->name = all += axes * sizeof(struct js_corr);
	strcpy(curd->name, name);

	port->devs[number] = curd;
	port->axes[number] = curd->new.axes;
	port->buttons[number] = curd->new.buttons;
	port->corr[number] = curd->corr;

	spin_lock_irqsave(&js_lock, flags);

	while (*ptrd != NULL) { ptrd=&(*ptrd)->next; i++; }
	*ptrd = curd;

	spin_unlock_irqrestore(&js_lock, flags);	

	return i;
}

void js_unregister_device(struct js_dev *dev)
{
	struct js_dev **curd = &js_dev;
	unsigned long flags;

	spin_lock_irqsave(&js_lock, flags);

	while (*curd != NULL && (*curd != dev)) curd = &((*curd)->next);
	*curd = (*curd)->next;

	spin_unlock_irqrestore(&js_lock, flags);	

	kfree(dev);
}

/*
 * The operations structure.
 */

static struct file_operations js_fops =
{
	read:		js_read,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,0)
	poll:		js_poll,
#else
	select:		js_select,
#endif
	ioctl:		js_ioctl,
	open:		js_open,
	release:	js_release,
};

/*
 * js_init() registers the driver and calls the probe function.
 * also initializes some crucial variables.
 */

#ifdef MODULE
int init_module(void)
#else
int __init js_init(void)
#endif
{
	int result;

	js_setup_time();

	if (register_chrdev(JOYSTICK_MAJOR, "js", &js_fops)) {
		printk(KERN_ERR "js: unable to get major %d for joystick\n", JOYSTICK_MAJOR);
		return -EBUSY;
	}

	spin_lock_init(&js_lock);

	init_timer(&js_timer);
	js_timer.function = js_do_timer;
	js_timer.data = 1;

	memset(&js_comp_glue, 0, sizeof(struct JS_DATA_SAVE_TYPE));
	js_comp_glue.JS_TIMEOUT = JS_DEF_TIMEOUT;
	js_comp_glue.JS_TIMELIMIT = JS_DEF_TIMELIMIT;

#ifdef MODULE
	result = 0;
#else
	result = -ENODEV;
#ifdef CONFIG_JOY_LIGHTNING
	if (!js_l4_init()) result = 0;
#endif
#ifdef CONFIG_JOY_SIDEWINDER
	if (!js_sw_init()) result = 0;
#endif
#ifdef CONFIG_JOY_ASSASIN
	if (!js_as_init()) result = 0;
#endif
#ifdef CONFIG_JOY_LOGITECH
	if (!js_lt_init()) result = 0;
#endif
#ifdef CONFIG_JOY_THRUSTMASTER
	if (!js_tm_init()) result = 0;
#endif
#ifdef CONFIG_JOY_GRAVIS
	if (!js_gr_init()) result = 0;
#endif
#ifdef CONFIG_JOY_ANALOG
	if (!js_an_init()) result = 0;
#endif
#ifdef CONFIG_JOY_CONSOLE
	if (!js_console_init()) result = 0;
#endif
#ifdef CONFIG_JOY_DB9
	if (!js_db9_init()) result = 0;
#endif
#ifdef CONFIG_JOY_AMIGA
	if (!js_am_init()) result = 0;
#endif
	if (result) printk(KERN_ERR "js: no joysticks found\n");
#endif

	return result;
}

/*
 * cleanup_module() handles module removal.
 */

#ifdef MODULE
void cleanup_module(void)
{
	del_timer(&js_timer);
	if (unregister_chrdev(JOYSTICK_MAJOR, "js"))
		printk(KERN_ERR "js: can't unregister device\n");
}
#endif