Import 2.4.0-test5pre2
[davej-history.git] / drivers / usb / joydev.c
blob2210ad0d1923351c49363bfe8770b90f498201f7
1 /*
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
32 #include <asm/io.h>
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>
42 #include <linux/mm.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
53 struct joydev {
54 int exist;
55 int open;
56 int minor;
57 struct input_handle handle;
58 wait_queue_head_t wait;
59 devfs_handle_t devfs;
60 struct joydev *next;
61 struct joydev_list *list;
62 struct js_corr corr[ABS_MAX];
63 struct JS_DATA_SAVE_TYPE glue;
64 int nabs;
65 int nkey;
66 __u16 keymap[KEY_MAX - BTN_MISC];
67 __u16 keypam[KEY_MAX - BTN_MISC];
68 __u8 absmap[ABS_MAX];
69 __u8 abspam[ABS_MAX];
70 __s16 abs[ABS_MAX];
73 struct joydev_list {
74 struct js_event buffer[JOYDEV_BUFFER_SIZE];
75 int head;
76 int tail;
77 int startup;
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)
90 switch (corr->type) {
91 case JS_CORR_NONE:
92 break;
93 case JS_CORR_BROKEN:
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);
97 break;
98 default:
99 return 0;
102 if (value < -32767) return -32767;
103 if (value > 32767) return 32767;
105 return value;
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;
114 switch (type) {
116 case EV_KEY:
117 if (code < BTN_MISC || value == 2) return;
118 event.type = JS_EVENT_BUTTON;
119 event.number = joydev->keymap[code - BTN_MISC];
120 event.value = value;
121 break;
123 case EV_ABS:
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;
129 break;
131 default:
132 return;
135 event.time = jiffies * (1000 / HZ);
137 while (list) {
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)))
143 list->startup = 0;
145 kill_fasync(&list->fasync, SIGIO, POLL_IN);
147 list = list->next;
150 wake_up_interruptible(&joydev->wait);
153 static int joydev_fasync(int fd, struct file *file, int on)
155 int retval;
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;
166 lock_kernel();
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);
177 } else {
178 input_unregister_minor(list->joydev->devfs);
179 joydev_table[list->joydev->minor] = NULL;
180 kfree(list->joydev);
184 kfree(list);
185 unlock_kernel();
187 return 0;
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])
196 return -ENODEV;
198 if (!(list = kmalloc(sizeof(struct joydev_list), GFP_KERNEL)))
199 return -ENOMEM;
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);
212 return 0;
215 static ssize_t joydev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
217 return -EINVAL;
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;
226 int retval = 0;
228 if (count < sizeof(struct js_event))
229 return -EINVAL;
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)))
241 return -EFAULT;
243 list->startup = 0;
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) {
257 retval = -EAGAIN;
258 break;
260 if (signal_pending(current)) {
261 retval = -ERESTARTSYS;
262 break;
265 schedule();
268 current->state = TASK_RUNNING;
269 remove_wait_queue(&list->joydev->wait, &wait);
272 if (retval)
273 return retval;
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);
285 } else {
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)))
292 return -EFAULT;
294 list->startup++;
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)))
301 return -EFAULT;
303 list->tail = (list->tail + 1) & (JOYDEV_BUFFER_SIZE - 1);
304 retval += sizeof(struct js_event);
307 return retval;
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;
317 return 0;
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;
327 switch (cmd) {
329 case JS_SET_CAL:
330 return copy_from_user(&joydev->glue.JS_CORR, (struct JS_DATA_TYPE *) arg,
331 sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
332 case JS_GET_CAL:
333 return copy_to_user((struct JS_DATA_TYPE *) arg, &joydev->glue.JS_CORR,
334 sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
335 case JS_SET_TIMEOUT:
336 return get_user(joydev->glue.JS_TIMEOUT, (int *) arg);
337 case JS_GET_TIMEOUT:
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);
343 case JS_SET_ALL:
344 return copy_from_user(&joydev->glue, (struct JS_DATA_SAVE_TYPE *) arg,
345 sizeof(struct JS_DATA_SAVE_TYPE)) ? -EFAULT : 0;
346 case JS_GET_ALL:
347 return copy_to_user((struct JS_DATA_SAVE_TYPE *) arg, &joydev->glue,
348 sizeof(struct JS_DATA_SAVE_TYPE)) ? -EFAULT : 0;
350 case JSIOCGVERSION:
351 return put_user(JS_VERSION, (__u32 *) arg);
352 case JSIOCGAXES:
353 return put_user(joydev->nabs, (__u8 *) arg);
354 case JSIOCGBUTTONS:
355 return put_user(joydev->nkey, (__u8 *) arg);
356 case JSIOCSCORR:
357 return copy_from_user(joydev->corr, (struct js_corr *) arg,
358 sizeof(struct js_corr) * joydev->nabs) ? -EFAULT : 0;
359 case JSIOCGCORR:
360 return copy_to_user((struct js_corr *) arg, joydev->corr,
361 sizeof(struct js_corr) * joydev->nabs) ? -EFAULT : 0;
362 default:
363 if ((cmd & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) == JSIOCGNAME(0)) {
364 int len;
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;
369 return len;
372 return -EINVAL;
375 static struct file_operations joydev_fops = {
376 owner: THIS_MODULE,
377 read: joydev_read,
378 write: joydev_write,
379 poll: joydev_poll,
380 open: joydev_open,
381 release: joydev_release,
382 ioctl: joydev_ioctl,
383 fasync: joydev_fasync,
386 static struct input_handle *joydev_connect(struct input_handler *handler, struct input_dev *dev)
388 struct joydev *joydev;
389 int i, j, minor;
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");
399 return NULL;
402 if (!(joydev = kmalloc(sizeof(struct joydev), GFP_KERNEL)))
403 return NULL;
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;
415 joydev->exist = 1;
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;
421 joydev->nabs++;
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;
428 joydev->nkey++;
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;
435 joydev->nkey++;
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;
442 continue;
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;
465 joydev->exist = 0;
467 if (joydev->open) {
468 input_close_device(handle);
469 } else {
470 input_unregister_minor(joydev->devfs);
471 joydev_table[joydev->minor] = NULL;
472 kfree(joydev);
476 static struct input_handler joydev_handler = {
477 event: joydev_event,
478 connect: joydev_connect,
479 disconnect: joydev_disconnect,
480 fops: &joydev_fops,
481 minor: JOYDEV_MINOR_BASE,
484 static int __init joydev_init(void)
486 input_register_handler(&joydev_handler);
487 return 0;
490 static void __exit joydev_exit(void)
492 input_unregister_handler(&joydev_handler);
495 module_init(joydev_init);
496 module_exit(joydev_exit);