4 * Copyright (c) 1999 Vojtech Pavlik
5 * Copyright (c) 1999 Colin Van Dyke
7 * Joystick device driver for the input driver suite.
9 * Sponsored by SuSE and Intel
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
29 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
33 #include <asm/system.h>
34 #include <asm/segment.h>
35 #include <linux/delay.h>
36 #include <linux/errno.h>
37 #include <linux/joystick.h>
38 #include <linux/input.h>
39 #include <linux/kernel.h>
40 #include <linux/major.h>
41 #include <linux/malloc.h>
43 #include <linux/miscdevice.h>
44 #include <linux/module.h>
45 #include <linux/poll.h>
46 #include <linux/init.h>
48 #define JOYDEV_MAJOR 15
49 #define JOYDEV_BUFFER_SIZE 64
54 struct input_handle handle
;
56 wait_queue_head_t wait
;
58 struct joydev_list
*list
;
59 struct js_corr corr
[ABS_MAX
];
60 struct JS_DATA_SAVE_TYPE glue
;
63 __u16 keymap
[KEY_MAX
- BTN_MISC
];
64 __u16 keypam
[KEY_MAX
- BTN_MISC
];
70 struct js_event buffer
[JOYDEV_BUFFER_SIZE
];
74 struct fasync_struct
*fasync
;
75 struct joydev
*joydev
;
76 struct joydev_list
*next
;
79 static unsigned long joydev_minors
= 0;
80 static struct joydev
*joydev_base
[BITS_PER_LONG
];
82 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
83 MODULE_SUPPORTED_DEVICE("js");
85 static int joydev_correct(int value
, struct js_corr
*corr
)
91 value
= value
> corr
->coef
[0] ? (value
< corr
->coef
[1] ? 0 :
92 ((corr
->coef
[3] * (value
- corr
->coef
[1])) >> 14)) :
93 ((corr
->coef
[2] * (value
- corr
->coef
[0])) >> 14);
99 if (value
< -32767) return -32767;
100 if (value
> 32767) return 32767;
105 static void joydev_event(struct input_handle
*handle
, unsigned int type
, unsigned int code
, int value
)
107 struct joydev
*joydev
= handle
->private;
108 struct joydev_list
*list
= joydev
->list
;
109 struct js_event event
;
114 if (code
< BTN_MISC
|| value
== 2) return;
115 event
.type
= JS_EVENT_BUTTON
;
116 event
.number
= joydev
->keymap
[code
- BTN_MISC
];
121 event
.type
= JS_EVENT_AXIS
;
122 event
.number
= joydev
->absmap
[code
];
123 event
.value
= joydev_correct(value
, &joydev
->corr
[event
.number
]);
130 event
.time
= jiffies
* (1000 / HZ
);
134 memcpy(list
->buffer
+ list
->head
, &event
, sizeof(struct js_event
));
136 if (list
->startup
== joydev
->nabs
+ joydev
->nkey
)
137 if (list
->tail
== (list
->head
= (list
->head
+ 1) & (JOYDEV_BUFFER_SIZE
- 1)))
141 kill_fasync(list
->fasync
, SIGIO
, POLL_IN
);
146 wake_up_interruptible(&joydev
->wait
);
149 static int joydev_fasync(int fd
, struct file
*file
, int on
)
152 struct joydev_list
*list
= file
->private_data
;
153 retval
= fasync_helper(fd
, file
, on
, &list
->fasync
);
154 return retval
< 0 ? retval
: 0;
157 static int joydev_release(struct inode
* inode
, struct file
* file
)
159 struct joydev_list
*list
= file
->private_data
;
160 struct joydev_list
**listptr
= &list
->joydev
->list
;
162 joydev_fasync(-1, file
, 0);
164 while (*listptr
&& (*listptr
!= list
))
165 listptr
= &((*listptr
)->next
);
166 *listptr
= (*listptr
)->next
;
168 if (!--list
->joydev
->used
) {
169 clear_bit(list
->joydev
->minor
, &joydev_minors
);
179 static int joydev_open(struct inode
*inode
, struct file
*file
)
181 struct joydev_list
*list
;
182 int i
= MINOR(inode
->i_rdev
);
184 if (MAJOR(inode
->i_rdev
) != JOYSTICK_MAJOR
)
187 if (i
> BITS_PER_LONG
|| !test_bit(i
, &joydev_minors
))
190 if (!(list
= kmalloc(sizeof(struct joydev_list
), GFP_KERNEL
)))
193 memset(list
, 0, sizeof(struct joydev_list
));
195 list
->joydev
= joydev_base
[i
];
196 list
->next
= joydev_base
[i
]->list
;
197 joydev_base
[i
]->list
= list
;
199 file
->private_data
= list
;
201 list
->joydev
->used
++;
207 static ssize_t
joydev_write(struct file
* file
, const char * buffer
, size_t count
, loff_t
*ppos
)
212 static ssize_t
joydev_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
214 DECLARE_WAITQUEUE(wait
, current
);
215 struct joydev_list
*list
= file
->private_data
;
216 struct joydev
*joydev
= list
->joydev
;
217 struct input_dev
*input
= joydev
->handle
.dev
;
220 if (count
< sizeof(struct js_event
))
223 if (count
== sizeof(struct JS_DATA_TYPE
)) {
225 struct JS_DATA_TYPE data
;
227 data
.buttons
= (joydev
->nkey
> 0 && test_bit(joydev
->keypam
[0], input
->key
)) ? 1 : 0 |
228 (joydev
->nkey
> 1 && test_bit(joydev
->keypam
[1], input
->key
)) ? 2 : 0;
229 data
.x
= ((joydev_correct(input
->abs
[ABS_X
], &joydev
->corr
[0]) / 256) + 128) >> joydev
->glue
.JS_CORR
.x
;
230 data
.y
= ((joydev_correct(input
->abs
[ABS_Y
], &joydev
->corr
[1]) / 256) + 128) >> joydev
->glue
.JS_CORR
.y
;
232 if (copy_to_user(buf
, &data
, sizeof(struct JS_DATA_TYPE
)))
236 list
->tail
= list
->head
;
238 return sizeof(struct JS_DATA_TYPE
);
241 if (list
->head
== list
->tail
&& list
->startup
== joydev
->nabs
+ joydev
->nkey
) {
243 add_wait_queue(&list
->joydev
->wait
, &wait
);
244 current
->state
= TASK_INTERRUPTIBLE
;
246 while (list
->head
== list
->tail
) {
248 if (file
->f_flags
& O_NONBLOCK
) {
252 if (signal_pending(current
)) {
253 retval
= -ERESTARTSYS
;
260 current
->state
= TASK_RUNNING
;
261 remove_wait_queue(&list
->joydev
->wait
, &wait
);
267 while (list
->startup
< joydev
->nabs
+ joydev
->nkey
&& retval
+ sizeof(struct js_event
) <= count
) {
269 struct js_event event
;
271 event
.time
= jiffies
* (1000/HZ
);
273 if (list
->startup
< joydev
->nkey
) {
274 event
.type
= JS_EVENT_BUTTON
| JS_EVENT_INIT
;
275 event
.value
= !!test_bit(joydev
->keypam
[list
->startup
], input
->key
);
276 event
.number
= list
->startup
;
278 event
.type
= JS_EVENT_AXIS
| JS_EVENT_INIT
;
279 event
.value
= joydev_correct(input
->abs
[joydev
->abspam
[list
->startup
- joydev
->nkey
]],
280 &joydev
->corr
[list
->startup
- joydev
->nkey
]);
281 event
.number
= list
->startup
- joydev
->nkey
;
284 if (copy_to_user(buf
+ retval
, &event
, sizeof(struct js_event
)))
288 retval
+= sizeof(struct js_event
);
291 while (list
->head
!= list
->tail
&& retval
+ sizeof(struct js_event
) <= count
) {
293 if (copy_to_user(buf
+ retval
, list
->buffer
+ list
->tail
, sizeof(struct js_event
)))
296 list
->tail
= (list
->tail
+ 1) & (JOYDEV_BUFFER_SIZE
- 1);
297 retval
+= sizeof(struct js_event
);
303 static unsigned int joydev_poll(struct file
*file
, poll_table
*wait
)
305 struct joydev_list
*list
= file
->private_data
;
306 poll_wait(file
, &list
->joydev
->wait
, wait
);
307 if (list
->head
!= list
->tail
|| list
->startup
< list
->joydev
->nabs
+ list
->joydev
->nkey
)
308 return POLLIN
| POLLRDNORM
;
312 static int joydev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
314 struct joydev_list
*list
= file
->private_data
;
315 struct joydev
*joydev
= list
->joydev
;
320 return copy_from_user(&joydev
->glue
.JS_CORR
, (struct JS_DATA_TYPE
*) arg
,
321 sizeof(struct JS_DATA_TYPE
)) ? -EFAULT
: 0;
323 return copy_to_user((struct JS_DATA_TYPE
*) arg
, &joydev
->glue
.JS_CORR
,
324 sizeof(struct JS_DATA_TYPE
)) ? -EFAULT
: 0;
326 return get_user(joydev
->glue
.JS_TIMEOUT
, (int *) arg
);
328 return put_user(joydev
->glue
.JS_TIMEOUT
, (int *) arg
);
329 case JS_SET_TIMELIMIT
:
330 return get_user(joydev
->glue
.JS_TIMELIMIT
, (long *) arg
);
331 case JS_GET_TIMELIMIT
:
332 return put_user(joydev
->glue
.JS_TIMELIMIT
, (long *) arg
);
334 return copy_from_user(&joydev
->glue
, (struct JS_DATA_SAVE_TYPE
*) arg
,
335 sizeof(struct JS_DATA_SAVE_TYPE
)) ? -EFAULT
: 0;
337 return copy_to_user((struct JS_DATA_SAVE_TYPE
*) arg
, &joydev
->glue
,
338 sizeof(struct JS_DATA_SAVE_TYPE
)) ? -EFAULT
: 0;
341 return put_user(JS_VERSION
, (__u32
*) arg
);
343 return put_user(joydev
->nabs
, (__u8
*) arg
);
345 return put_user(joydev
->nkey
, (__u8
*) arg
);
347 return copy_from_user(joydev
->corr
, (struct js_corr
*) arg
,
348 sizeof(struct js_corr
) * joydev
->nabs
) ? -EFAULT
: 0;
350 return copy_to_user((struct js_corr
*) arg
, joydev
->corr
,
351 sizeof(struct js_corr
) * joydev
->nabs
) ? -EFAULT
: 0;
353 if ((cmd
& ~(_IOC_SIZEMASK
<< _IOC_SIZESHIFT
)) == JSIOCGNAME(0)) {
354 int len
= strlen(joydev
->name
) + 1;
355 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
356 if (copy_to_user((char *) arg
, joydev
->name
, len
)) return -EFAULT
;
363 static struct file_operations joydev_fops
= {
368 release
: joydev_release
,
370 fasync
: joydev_fasync
,
373 static int joydev_connect(struct input_handler
*handler
, struct input_dev
*dev
)
375 struct joydev
*joydev
;
378 if (!(test_bit(EV_KEY
, dev
->evbit
) && test_bit(EV_ABS
, dev
->evbit
) &&
379 test_bit(ABS_X
, dev
->absbit
) && test_bit(ABS_Y
, dev
->absbit
) &&
380 (test_bit(BTN_TRIGGER
, dev
->keybit
) || test_bit(BTN_A
, dev
->keybit
)
381 || test_bit(BTN_1
, dev
->keybit
)))) return -1;
383 if (!(joydev
= kmalloc(sizeof(struct joydev
), GFP_KERNEL
)))
386 memset(joydev
, 0, sizeof(struct joydev
));
388 init_waitqueue_head(&joydev
->wait
);
390 if (joydev_minors
== -1) {
391 printk("Can't register new joystick - 32 devices already taken.\n");
395 sprintf(joydev
->name
, "joydev%d", joydev
->minor
);
397 joydev
->handle
.dev
= dev
;
398 joydev
->handle
.handler
= handler
;
399 joydev
->handle
.private = joydev
;
403 for (i
= 0; i
< ABS_MAX
; i
++)
404 if (test_bit(i
, dev
->absbit
)) {
405 joydev
->absmap
[i
] = joydev
->nabs
;
406 joydev
->abspam
[joydev
->nabs
] = i
;
410 for (i
= BTN_JOYSTICK
- BTN_MISC
; i
< KEY_MAX
- BTN_MISC
; i
++)
411 if (test_bit(i
+ BTN_MISC
, dev
->keybit
)) {
412 joydev
->keymap
[i
] = joydev
->nkey
;
413 joydev
->keypam
[joydev
->nkey
] = i
+ BTN_MISC
;
417 for (i
= 0; i
< BTN_JOYSTICK
- BTN_MISC
; i
++)
418 if (test_bit(i
+ BTN_MISC
, dev
->keybit
)) {
419 joydev
->keymap
[i
] = joydev
->nkey
;
420 joydev
->keypam
[joydev
->nkey
] = i
+ BTN_MISC
;
424 joydev
->minor
= ffz(joydev_minors
);
425 set_bit(joydev
->minor
, &joydev_minors
);
426 joydev_base
[joydev
->minor
] = joydev
;
428 for (i
= 0; i
< joydev
->nabs
; i
++) {
429 j
= joydev
->abspam
[i
];
430 if (dev
->absmax
[j
] == dev
->absmin
[j
]) {
431 joydev
->corr
[i
].type
= JS_CORR_NONE
;
434 joydev
->corr
[i
].type
= JS_CORR_BROKEN
;
435 joydev
->corr
[i
].prec
= dev
->absfuzz
[j
];
436 joydev
->corr
[i
].coef
[0] = (dev
->absmax
[j
] + dev
->absmin
[j
]) / 2 - dev
->absflat
[j
];
437 joydev
->corr
[i
].coef
[1] = (dev
->absmax
[j
] + dev
->absmin
[j
]) / 2 + dev
->absflat
[j
];
438 joydev
->corr
[i
].coef
[2] = (1 << 29) / ((dev
->absmax
[j
] - dev
->absmin
[j
]) / 2 - 2 * dev
->absflat
[j
]);
439 joydev
->corr
[i
].coef
[3] = (1 << 29) / ((dev
->absmax
[j
] - dev
->absmin
[j
]) / 2 - 2 * dev
->absflat
[j
]);
442 input_open_device(&joydev
->handle
);
444 printk("%s: Joystick device for input%d on /dev/js%d\n", joydev
->name
, dev
->number
, joydev
->minor
);
449 static void joydev_disconnect(struct input_handle
*handle
)
451 struct joydev
*joydev
= handle
->private;
453 input_close_device(handle
);
455 if (!--joydev
->used
) {
456 clear_bit(joydev
->minor
, &joydev_minors
);
461 static struct input_handler joydev_handler
= {
463 connect
: joydev_connect
,
464 disconnect
: joydev_disconnect
,
467 static int __init
joydev_init(void)
469 if (register_chrdev(JOYDEV_MAJOR
, "js", &joydev_fops
)) {
470 printk(KERN_ERR
"joydev: unable to get major %d for joystick\n", JOYDEV_MAJOR
);
473 input_register_handler(&joydev_handler
);
477 static void __exit
joydev_exit(void)
479 input_unregister_handler(&joydev_handler
);
480 if (unregister_chrdev(JOYSTICK_MAJOR
, "js"))
481 printk(KERN_ERR
"js: can't unregister device\n");
484 module_init(joydev_init
);
485 module_exit(joydev_exit
);