2 * $Id: joydev.c,v 1.11 2000/06/23 09:23:00 vojtech Exp $
4 * Copyright (c) 1999-2000 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>
47 #include <linux/smp_lock.h>
49 #define JOYDEV_MINOR_BASE 0
50 #define JOYDEV_MINORS 32
51 #define JOYDEV_BUFFER_SIZE 64
57 struct input_handle handle
;
58 wait_queue_head_t wait
;
61 struct joydev_list
*list
;
62 struct js_corr corr
[ABS_MAX
];
63 struct JS_DATA_SAVE_TYPE glue
;
66 __u16 keymap
[KEY_MAX
- BTN_MISC
];
67 __u16 keypam
[KEY_MAX
- BTN_MISC
];
74 struct js_event buffer
[JOYDEV_BUFFER_SIZE
];
78 struct fasync_struct
*fasync
;
79 struct joydev
*joydev
;
80 struct joydev_list
*next
;
83 static struct joydev
*joydev_table
[JOYDEV_MINORS
];
85 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
86 MODULE_SUPPORTED_DEVICE("input/js");
88 static int joydev_correct(int value
, struct js_corr
*corr
)
94 value
= value
> corr
->coef
[0] ? (value
< corr
->coef
[1] ? 0 :
95 ((corr
->coef
[3] * (value
- corr
->coef
[1])) >> 14)) :
96 ((corr
->coef
[2] * (value
- corr
->coef
[0])) >> 14);
102 if (value
< -32767) return -32767;
103 if (value
> 32767) return 32767;
108 static void joydev_event(struct input_handle
*handle
, unsigned int type
, unsigned int code
, int value
)
110 struct joydev
*joydev
= handle
->private;
111 struct joydev_list
*list
= joydev
->list
;
112 struct js_event event
;
117 if (code
< BTN_MISC
|| value
== 2) return;
118 event
.type
= JS_EVENT_BUTTON
;
119 event
.number
= joydev
->keymap
[code
- BTN_MISC
];
124 event
.type
= JS_EVENT_AXIS
;
125 event
.number
= joydev
->absmap
[code
];
126 event
.value
= joydev_correct(value
, joydev
->corr
+ event
.number
);
127 if (event
.value
== joydev
->abs
[event
.number
]) return;
128 joydev
->abs
[event
.number
] = event
.value
;
135 event
.time
= jiffies
* (1000 / HZ
);
139 memcpy(list
->buffer
+ list
->head
, &event
, sizeof(struct js_event
));
141 if (list
->startup
== joydev
->nabs
+ joydev
->nkey
)
142 if (list
->tail
== (list
->head
= (list
->head
+ 1) & (JOYDEV_BUFFER_SIZE
- 1)))
145 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
150 wake_up_interruptible(&joydev
->wait
);
153 static int joydev_fasync(int fd
, struct file
*file
, int on
)
156 struct joydev_list
*list
= file
->private_data
;
157 retval
= fasync_helper(fd
, file
, on
, &list
->fasync
);
158 return retval
< 0 ? retval
: 0;
161 static int joydev_release(struct inode
* inode
, struct file
* file
)
163 struct joydev_list
*list
= file
->private_data
;
164 struct joydev_list
**listptr
;
167 listptr
= &list
->joydev
->list
;
168 joydev_fasync(-1, file
, 0);
170 while (*listptr
&& (*listptr
!= list
))
171 listptr
= &((*listptr
)->next
);
172 *listptr
= (*listptr
)->next
;
174 if (!--list
->joydev
->open
) {
175 if (list
->joydev
->exist
) {
176 input_close_device(&list
->joydev
->handle
);
178 input_unregister_minor(list
->joydev
->devfs
);
179 joydev_table
[list
->joydev
->minor
] = NULL
;
190 static int joydev_open(struct inode
*inode
, struct file
*file
)
192 struct joydev_list
*list
;
193 int i
= MINOR(inode
->i_rdev
) - JOYDEV_MINOR_BASE
;
195 if (i
> JOYDEV_MINORS
|| !joydev_table
[i
])
198 if (!(list
= kmalloc(sizeof(struct joydev_list
), GFP_KERNEL
)))
200 memset(list
, 0, sizeof(struct joydev_list
));
202 list
->joydev
= joydev_table
[i
];
203 list
->next
= joydev_table
[i
]->list
;
204 joydev_table
[i
]->list
= list
;
206 file
->private_data
= list
;
208 if (!list
->joydev
->open
++)
209 if (list
->joydev
->exist
)
210 input_open_device(&list
->joydev
->handle
);
215 static ssize_t
joydev_write(struct file
* file
, const char * buffer
, size_t count
, loff_t
*ppos
)
220 static ssize_t
joydev_read(struct file
*file
, char *buf
, size_t count
, loff_t
*ppos
)
222 DECLARE_WAITQUEUE(wait
, current
);
223 struct joydev_list
*list
= file
->private_data
;
224 struct joydev
*joydev
= list
->joydev
;
225 struct input_dev
*input
= joydev
->handle
.dev
;
228 if (count
< sizeof(struct js_event
))
231 if (count
== sizeof(struct JS_DATA_TYPE
)) {
233 struct JS_DATA_TYPE data
;
235 data
.buttons
= ((joydev
->nkey
> 0 && test_bit(joydev
->keypam
[0], input
->key
)) ? 1 : 0) |
236 ((joydev
->nkey
> 1 && test_bit(joydev
->keypam
[1], input
->key
)) ? 2 : 0);
237 data
.x
= (joydev
->abs
[0] / 256 + 128) >> joydev
->glue
.JS_CORR
.x
;
238 data
.y
= (joydev
->abs
[1] / 256 + 128) >> joydev
->glue
.JS_CORR
.y
;
240 if (copy_to_user(buf
, &data
, sizeof(struct JS_DATA_TYPE
)))
244 list
->tail
= list
->head
;
246 return sizeof(struct JS_DATA_TYPE
);
249 if (list
->head
== list
->tail
&& list
->startup
== joydev
->nabs
+ joydev
->nkey
) {
251 add_wait_queue(&list
->joydev
->wait
, &wait
);
252 current
->state
= TASK_INTERRUPTIBLE
;
254 while (list
->head
== list
->tail
) {
256 if (file
->f_flags
& O_NONBLOCK
) {
260 if (signal_pending(current
)) {
261 retval
= -ERESTARTSYS
;
268 current
->state
= TASK_RUNNING
;
269 remove_wait_queue(&list
->joydev
->wait
, &wait
);
275 while (list
->startup
< joydev
->nabs
+ joydev
->nkey
&& retval
+ sizeof(struct js_event
) <= count
) {
277 struct js_event event
;
279 event
.time
= jiffies
* (1000/HZ
);
281 if (list
->startup
< joydev
->nkey
) {
282 event
.type
= JS_EVENT_BUTTON
| JS_EVENT_INIT
;
283 event
.number
= list
->startup
;
284 event
.value
= !!test_bit(joydev
->keypam
[event
.number
], input
->key
);
286 event
.type
= JS_EVENT_AXIS
| JS_EVENT_INIT
;
287 event
.number
= list
->startup
- joydev
->nkey
;
288 event
.value
= joydev
->abs
[event
.number
];
291 if (copy_to_user(buf
+ retval
, &event
, sizeof(struct js_event
)))
295 retval
+= sizeof(struct js_event
);
298 while (list
->head
!= list
->tail
&& retval
+ sizeof(struct js_event
) <= count
) {
300 if (copy_to_user(buf
+ retval
, list
->buffer
+ list
->tail
, sizeof(struct js_event
)))
303 list
->tail
= (list
->tail
+ 1) & (JOYDEV_BUFFER_SIZE
- 1);
304 retval
+= sizeof(struct js_event
);
310 /* No kernel lock - fine */
311 static unsigned int joydev_poll(struct file
*file
, poll_table
*wait
)
313 struct joydev_list
*list
= file
->private_data
;
314 poll_wait(file
, &list
->joydev
->wait
, wait
);
315 if (list
->head
!= list
->tail
|| list
->startup
< list
->joydev
->nabs
+ list
->joydev
->nkey
)
316 return POLLIN
| POLLRDNORM
;
320 static int joydev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
322 struct joydev_list
*list
= file
->private_data
;
323 struct joydev
*joydev
= list
->joydev
;
324 struct input_dev
*dev
= joydev
->handle
.dev
;
330 return copy_from_user(&joydev
->glue
.JS_CORR
, (struct JS_DATA_TYPE
*) arg
,
331 sizeof(struct JS_DATA_TYPE
)) ? -EFAULT
: 0;
333 return copy_to_user((struct JS_DATA_TYPE
*) arg
, &joydev
->glue
.JS_CORR
,
334 sizeof(struct JS_DATA_TYPE
)) ? -EFAULT
: 0;
336 return get_user(joydev
->glue
.JS_TIMEOUT
, (int *) arg
);
338 return put_user(joydev
->glue
.JS_TIMEOUT
, (int *) arg
);
339 case JS_SET_TIMELIMIT
:
340 return get_user(joydev
->glue
.JS_TIMELIMIT
, (long *) arg
);
341 case JS_GET_TIMELIMIT
:
342 return put_user(joydev
->glue
.JS_TIMELIMIT
, (long *) arg
);
344 return copy_from_user(&joydev
->glue
, (struct JS_DATA_SAVE_TYPE
*) arg
,
345 sizeof(struct JS_DATA_SAVE_TYPE
)) ? -EFAULT
: 0;
347 return copy_to_user((struct JS_DATA_SAVE_TYPE
*) arg
, &joydev
->glue
,
348 sizeof(struct JS_DATA_SAVE_TYPE
)) ? -EFAULT
: 0;
351 return put_user(JS_VERSION
, (__u32
*) arg
);
353 return put_user(joydev
->nabs
, (__u8
*) arg
);
355 return put_user(joydev
->nkey
, (__u8
*) arg
);
357 return copy_from_user(joydev
->corr
, (struct js_corr
*) arg
,
358 sizeof(struct js_corr
) * joydev
->nabs
) ? -EFAULT
: 0;
360 return copy_to_user((struct js_corr
*) arg
, joydev
->corr
,
361 sizeof(struct js_corr
) * joydev
->nabs
) ? -EFAULT
: 0;
363 if ((cmd
& ~(_IOC_SIZEMASK
<< _IOC_SIZESHIFT
)) == JSIOCGNAME(0)) {
365 if (!dev
->name
) return 0;
366 len
= strlen(dev
->name
) + 1;
367 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
368 if (copy_to_user((char *) arg
, dev
->name
, len
)) return -EFAULT
;
375 static struct file_operations joydev_fops
= {
381 release
: joydev_release
,
383 fasync
: joydev_fasync
,
386 static struct input_handle
*joydev_connect(struct input_handler
*handler
, struct input_dev
*dev
)
388 struct joydev
*joydev
;
391 if (!(test_bit(EV_KEY
, dev
->evbit
) && test_bit(EV_ABS
, dev
->evbit
) &&
392 test_bit(ABS_X
, dev
->absbit
) && test_bit(ABS_Y
, dev
->absbit
) &&
393 (test_bit(BTN_TRIGGER
, dev
->keybit
) || test_bit(BTN_A
, dev
->keybit
)
394 || test_bit(BTN_1
, dev
->keybit
)))) return NULL
;
396 for (minor
= 0; minor
< JOYDEV_MINORS
&& joydev_table
[minor
]; minor
++);
397 if (joydev_table
[minor
]) {
398 printk(KERN_ERR
"joydev: no more free joydev devices\n");
402 if (!(joydev
= kmalloc(sizeof(struct joydev
), GFP_KERNEL
)))
404 memset(joydev
, 0, sizeof(struct joydev
));
406 init_waitqueue_head(&joydev
->wait
);
408 joydev
->minor
= minor
;
409 joydev_table
[minor
] = joydev
;
411 joydev
->handle
.dev
= dev
;
412 joydev
->handle
.handler
= handler
;
413 joydev
->handle
.private = joydev
;
417 for (i
= 0; i
< ABS_MAX
; i
++)
418 if (test_bit(i
, dev
->absbit
)) {
419 joydev
->absmap
[i
] = joydev
->nabs
;
420 joydev
->abspam
[joydev
->nabs
] = i
;
424 for (i
= BTN_JOYSTICK
- BTN_MISC
; i
< KEY_MAX
- BTN_MISC
; i
++)
425 if (test_bit(i
+ BTN_MISC
, dev
->keybit
)) {
426 joydev
->keymap
[i
] = joydev
->nkey
;
427 joydev
->keypam
[joydev
->nkey
] = i
+ BTN_MISC
;
431 for (i
= 0; i
< BTN_JOYSTICK
- BTN_MISC
; i
++)
432 if (test_bit(i
+ BTN_MISC
, dev
->keybit
)) {
433 joydev
->keymap
[i
] = joydev
->nkey
;
434 joydev
->keypam
[joydev
->nkey
] = i
+ BTN_MISC
;
438 for (i
= 0; i
< joydev
->nabs
; i
++) {
439 j
= joydev
->abspam
[i
];
440 if (dev
->absmax
[j
] == dev
->absmin
[j
]) {
441 joydev
->corr
[i
].type
= JS_CORR_NONE
;
444 joydev
->corr
[i
].type
= JS_CORR_BROKEN
;
445 joydev
->corr
[i
].prec
= dev
->absfuzz
[j
];
446 joydev
->corr
[i
].coef
[0] = (dev
->absmax
[j
] + dev
->absmin
[j
]) / 2 - dev
->absflat
[j
];
447 joydev
->corr
[i
].coef
[1] = (dev
->absmax
[j
] + dev
->absmin
[j
]) / 2 + dev
->absflat
[j
];
448 joydev
->corr
[i
].coef
[2] = (1 << 29) / ((dev
->absmax
[j
] - dev
->absmin
[j
]) / 2 - 2 * dev
->absflat
[j
]);
449 joydev
->corr
[i
].coef
[3] = (1 << 29) / ((dev
->absmax
[j
] - dev
->absmin
[j
]) / 2 - 2 * dev
->absflat
[j
]);
451 joydev
->abs
[i
] = joydev_correct(dev
->abs
[j
], joydev
->corr
+ i
);
454 joydev
->devfs
= input_register_minor("js%d", minor
, JOYDEV_MINOR_BASE
);
456 printk(KERN_INFO
"js%d: Joystick device for input%d\n", minor
, dev
->number
);
458 return &joydev
->handle
;
461 static void joydev_disconnect(struct input_handle
*handle
)
463 struct joydev
*joydev
= handle
->private;
468 input_close_device(handle
);
470 input_unregister_minor(joydev
->devfs
);
471 joydev_table
[joydev
->minor
] = NULL
;
476 static struct input_handler joydev_handler
= {
478 connect
: joydev_connect
,
479 disconnect
: joydev_disconnect
,
481 minor
: JOYDEV_MINOR_BASE
,
484 static int __init
joydev_init(void)
486 input_register_handler(&joydev_handler
);
490 static void __exit
joydev_exit(void)
492 input_unregister_handler(&joydev_handler
);
495 module_init(joydev_init
);
496 module_exit(joydev_exit
);