Import 2.3.49pre2
[davej-history.git] / drivers / usb / joydev.c
bloba5dcabdabfe96fe9716d3644cfcffadc26c8dd84
1 /*
2 * joydev.c Version 0.1
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
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>
48 #define JOYDEV_MAJOR 15
49 #define JOYDEV_BUFFER_SIZE 64
51 struct joydev {
52 char name[32];
53 int used;
54 struct input_handle handle;
55 int minor;
56 wait_queue_head_t wait;
57 struct joydev *next;
58 struct joydev_list *list;
59 struct js_corr corr[ABS_MAX];
60 struct JS_DATA_SAVE_TYPE glue;
61 int nabs;
62 int nkey;
63 __u16 keymap[KEY_MAX - BTN_MISC];
64 __u16 keypam[KEY_MAX - BTN_MISC];
65 __u8 absmap[ABS_MAX];
66 __u8 abspam[ABS_MAX];
69 struct joydev_list {
70 struct js_event buffer[JOYDEV_BUFFER_SIZE];
71 int head;
72 int tail;
73 int startup;
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)
87 switch (corr->type) {
88 case JS_CORR_NONE:
89 break;
90 case JS_CORR_BROKEN:
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);
94 break;
95 default:
96 return 0;
99 if (value < -32767) return -32767;
100 if (value > 32767) return 32767;
102 return value;
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;
111 switch (type) {
113 case EV_KEY:
114 if (code < BTN_MISC || value == 2) return;
115 event.type = JS_EVENT_BUTTON;
116 event.number = joydev->keymap[code - BTN_MISC];
117 event.value = value;
118 break;
120 case EV_ABS:
121 event.type = JS_EVENT_AXIS;
122 event.number = joydev->absmap[code];
123 event.value = joydev_correct(value, &joydev->corr[event.number]);
124 break;
126 default:
127 return;
130 event.time = jiffies * (1000 / HZ);
132 while (list) {
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)))
138 list->startup = 0;
140 if (list->fasync)
141 kill_fasync(list->fasync, SIGIO, POLL_IN);
143 list = list->next;
146 wake_up_interruptible(&joydev->wait);
149 static int joydev_fasync(int fd, struct file *file, int on)
151 int retval;
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);
170 kfree(list->joydev);
173 kfree(list);
175 MOD_DEC_USE_COUNT;
176 return 0;
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)
185 return -EINVAL;
187 if (i > BITS_PER_LONG || !test_bit(i, &joydev_minors))
188 return -ENODEV;
190 if (!(list = kmalloc(sizeof(struct joydev_list), GFP_KERNEL)))
191 return -ENOMEM;
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++;
203 MOD_INC_USE_COUNT;
204 return 0;
207 static ssize_t joydev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
209 return -EINVAL;
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;
218 int retval = 0;
220 if (count < sizeof(struct js_event))
221 return -EINVAL;
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)))
233 return -EFAULT;
235 list->startup = 0;
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) {
249 retval = -EAGAIN;
250 break;
252 if (signal_pending(current)) {
253 retval = -ERESTARTSYS;
254 break;
257 schedule();
260 current->state = TASK_RUNNING;
261 remove_wait_queue(&list->joydev->wait, &wait);
264 if (retval)
265 return retval;
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;
277 } else {
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)))
285 return -EFAULT;
287 list->startup++;
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)))
294 return -EFAULT;
296 list->tail = (list->tail + 1) & (JOYDEV_BUFFER_SIZE - 1);
297 retval += sizeof(struct js_event);
300 return retval;
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;
309 return 0;
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;
317 switch (cmd) {
319 case JS_SET_CAL:
320 return copy_from_user(&joydev->glue.JS_CORR, (struct JS_DATA_TYPE *) arg,
321 sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
322 case JS_GET_CAL:
323 return copy_to_user((struct JS_DATA_TYPE *) arg, &joydev->glue.JS_CORR,
324 sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
325 case JS_SET_TIMEOUT:
326 return get_user(joydev->glue.JS_TIMEOUT, (int *) arg);
327 case JS_GET_TIMEOUT:
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);
333 case JS_SET_ALL:
334 return copy_from_user(&joydev->glue, (struct JS_DATA_SAVE_TYPE *) arg,
335 sizeof(struct JS_DATA_SAVE_TYPE)) ? -EFAULT : 0;
336 case JS_GET_ALL:
337 return copy_to_user((struct JS_DATA_SAVE_TYPE *) arg, &joydev->glue,
338 sizeof(struct JS_DATA_SAVE_TYPE)) ? -EFAULT : 0;
340 case JSIOCGVERSION:
341 return put_user(JS_VERSION, (__u32 *) arg);
342 case JSIOCGAXES:
343 return put_user(joydev->nabs, (__u8 *) arg);
344 case JSIOCGBUTTONS:
345 return put_user(joydev->nkey, (__u8 *) arg);
346 case JSIOCSCORR:
347 return copy_from_user(joydev->corr, (struct js_corr *) arg,
348 sizeof(struct js_corr) * joydev->nabs) ? -EFAULT : 0;
349 case JSIOCGCORR:
350 return copy_to_user((struct js_corr *) arg, joydev->corr,
351 sizeof(struct js_corr) * joydev->nabs) ? -EFAULT : 0;
352 default:
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;
357 return len;
360 return -EINVAL;
363 static struct file_operations joydev_fops = {
364 read: joydev_read,
365 write: joydev_write,
366 poll: joydev_poll,
367 open: joydev_open,
368 release: joydev_release,
369 ioctl: joydev_ioctl,
370 fasync: joydev_fasync,
373 static int joydev_connect(struct input_handler *handler, struct input_dev *dev)
375 struct joydev *joydev;
376 int i, j;
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)))
384 return -1;
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");
392 return -1;
395 sprintf(joydev->name, "joydev%d", joydev->minor);
397 joydev->handle.dev = dev;
398 joydev->handle.handler = handler;
399 joydev->handle.private = joydev;
401 joydev->used = 1;
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;
407 joydev->nabs++;
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;
414 joydev->nkey++;
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;
421 joydev->nkey++;
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;
432 continue;
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);
446 return 0;
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);
457 kfree(joydev);
461 static struct input_handler joydev_handler = {
462 event: joydev_event,
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);
471 return -EBUSY;
473 input_register_handler(&joydev_handler);
474 return 0;
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);