1 /*
2 * $Id: joydev.c,v 1.13 2000/08/14 21:05:26 vojtech Exp $
3 *
4 * Copyright (c) 1999-2000 Vojtech Pavlik
5 * Copyright (c) 1999 Colin Van Dyke
6 *
7 * Joystick device driver for the input driver suite.
8 *
9 * Sponsored by SuSE and Intel
10 */
11
12 /*
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.
17 *
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.
22 *
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
26 *
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
30 */
31
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>
48
49 #define JOYDEV_MINOR_BASE 0
50 #define JOYDEV_MINORS 32
51 #define JOYDEV_BUFFER_SIZE 64
52
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];
71 };
72
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;
81 };
82
83 static struct joydev *joydev_table[JOYDEV_MINORS];
84
85 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
86 MODULE_DESCRIPTION("Joystick device driver");
87 MODULE_SUPPORTED_DEVICE("input/js");
88
89 static int joydev_correct(int value, struct js_corr *corr)
90 {
91 switch (corr->type) {
92 case JS_CORR_NONE:
93 break;
94 case JS_CORR_BROKEN:
95 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
96 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
97 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
98 break;
99 default:
100 return 0;
101 }
102
103 if (value < -32767) return -32767;
104 if (value > 32767) return 32767;
105
106 return value;
107 }
108
109 static void joydev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
110 {
111 struct joydev *joydev = handle->private;
112 struct joydev_list *list = joydev->list;
113 struct js_event event;
114
115 switch (type) {
116
117 case EV_KEY:
118 if (code < BTN_MISC || value == 2) return;
119 event.type = JS_EVENT_BUTTON;
120 event.number = joydev->keymap[code - BTN_MISC];
121 event.value = value;
122 break;
123
124 case EV_ABS:
125 event.type = JS_EVENT_AXIS;
126 event.number = joydev->absmap[code];
127 event.value = joydev_correct(value, joydev->corr + event.number);
128 if (event.value == joydev->abs[event.number]) return;
129 joydev->abs[event.number] = event.value;
130 break;
131
132 default:
133 return;
134 }
135
136 event.time = jiffies * (1000 / HZ);
137
138 while (list) {
139
140 memcpy(list->buffer + list->head, &event, sizeof(struct js_event));
141
142 if (list->startup == joydev->nabs + joydev->nkey)
143 if (list->tail == (list->head = (list->head + 1) & (JOYDEV_BUFFER_SIZE - 1)))
144 list->startup = 0;
145
146 kill_fasync(&list->fasync, SIGIO, POLL_IN);
147
148 list = list->next;
149 }
150
151 wake_up_interruptible(&joydev->wait);
152 }
153
154 static int joydev_fasync(int fd, struct file *file, int on)
155 {
156 int retval;
157 struct joydev_list *list = file->private_data;
158 retval = fasync_helper(fd, file, on, &list->fasync);
159 return retval < 0 ? retval : 0;
160 }
161
162 static int joydev_release(struct inode * inode, struct file * file)
163 {
164 struct joydev_list *list = file->private_data;
165 struct joydev_list **listptr;
166
167 lock_kernel();
168 listptr = &list->joydev->list;
169 joydev_fasync(-1, file, 0);
170
171 while (*listptr && (*listptr != list))
172 listptr = &((*listptr)->next);
173 *listptr = (*listptr)->next;
174
175 if (!--list->joydev->open) {
176 if (list->joydev->exist) {
177 input_close_device(&list->joydev->handle);
178 } else {
179 input_unregister_minor(list->joydev->devfs);
180 joydev_table[list->joydev->minor] = NULL;
181 kfree(list->joydev);
182 }
183 }
184
185 kfree(list);
186 unlock_kernel();
187
188 return 0;
189 }
190
191 static int joydev_open(struct inode *inode, struct file *file)
192 {
193 struct joydev_list *list;
194 int i = MINOR(inode->i_rdev) - JOYDEV_MINOR_BASE;
195
196 if (i >= JOYDEV_MINORS || !joydev_table[i])
197 return -ENODEV;
198
199 if (!(list = kmalloc(sizeof(struct joydev_list), GFP_KERNEL)))
200 return -ENOMEM;
201 memset(list, 0, sizeof(struct joydev_list));
202
203 list->joydev = joydev_table[i];
204 list->next = joydev_table[i]->list;
205 joydev_table[i]->list = list;
206
207 file->private_data = list;
208
209 if (!list->joydev->open++)
210 if (list->joydev->exist)
211 input_open_device(&list->joydev->handle);
212
213 return 0;
214 }
215
216 static ssize_t joydev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
217 {
218 return -EINVAL;
219 }
220
221 static ssize_t joydev_read(struct file *file, char *buf, size_t count, loff_t *ppos)
222 {
223 DECLARE_WAITQUEUE(wait, current);
224 struct joydev_list *list = file->private_data;
225 struct joydev *joydev = list->joydev;
226 struct input_dev *input = joydev->handle.dev;
227 int retval = 0;
228
229 if (count < sizeof(struct js_event))
230 return -EINVAL;
231
232 if (count == sizeof(struct JS_DATA_TYPE)) {
233
234 struct JS_DATA_TYPE data;
235
236 data.buttons = ((joydev->nkey > 0 && test_bit(joydev->keypam[0], input->key)) ? 1 : 0) |
237 ((joydev->nkey > 1 && test_bit(joydev->keypam[1], input->key)) ? 2 : 0);
238 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
239 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
240
241 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
242 return -EFAULT;
243
244 list->startup = 0;
245 list->tail = list->head;
246
247 return sizeof(struct JS_DATA_TYPE);
248 }
249
250 if (list->head == list->tail && list->startup == joydev->nabs + joydev->nkey) {
251
252 add_wait_queue(&list->joydev->wait, &wait);
253 current->state = TASK_INTERRUPTIBLE;
254
255 while (list->head == list->tail) {
256
257 if (file->f_flags & O_NONBLOCK) {
258 retval = -EAGAIN;
259 break;
260 }
261 if (signal_pending(current)) {
262 retval = -ERESTARTSYS;
263 break;
264 }
265
266 schedule();
267 }
268
269 current->state = TASK_RUNNING;
270 remove_wait_queue(&list->joydev->wait, &wait);
271 }
272
273 if (retval)
274 return retval;
275
276 while (list->startup < joydev->nabs + joydev->nkey && retval + sizeof(struct js_event) <= count) {
277
278 struct js_event event;
279
280 event.time = jiffies * (1000/HZ);
281
282 if (list->startup < joydev->nkey) {
283 event.type = JS_EVENT_BUTTON | JS_EVENT_INIT;
284 event.number = list->startup;
285 event.value = !!test_bit(joydev->keypam[event.number], input->key);
286 } else {
287 event.type = JS_EVENT_AXIS | JS_EVENT_INIT;
288 event.number = list->startup - joydev->nkey;
289 event.value = joydev->abs[event.number];
290 }
291
292 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
293 return -EFAULT;
294
295 list->startup++;
296 retval += sizeof(struct js_event);
297 }
298
299 while (list->head != list->tail && retval + sizeof(struct js_event) <= count) {
300
301 if (copy_to_user(buf + retval, list->buffer + list->tail, sizeof(struct js_event)))
302 return -EFAULT;
303
304 list->tail = (list->tail + 1) & (JOYDEV_BUFFER_SIZE - 1);
305 retval += sizeof(struct js_event);
306 }
307
308 return retval;
309 }
310
311 /* No kernel lock - fine */
312 static unsigned int joydev_poll(struct file *file, poll_table *wait)
313 {
314 struct joydev_list *list = file->private_data;
315 poll_wait(file, &list->joydev->wait, wait);
316 if (list->head != list->tail || list->startup < list->joydev->nabs + list->joydev->nkey)
317 return POLLIN | POLLRDNORM;
318 return 0;
319 }
320
321 static int joydev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
322 {
323 struct joydev_list *list = file->private_data;
324 struct joydev *joydev = list->joydev;
325 struct input_dev *dev = joydev->handle.dev;
326
327
328 switch (cmd) {
329
330 case JS_SET_CAL:
331 return copy_from_user(&joydev->glue.JS_CORR, (struct JS_DATA_TYPE *) arg,
332 sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
333 case JS_GET_CAL:
334 return copy_to_user((struct JS_DATA_TYPE *) arg, &joydev->glue.JS_CORR,
335 sizeof(struct JS_DATA_TYPE)) ? -EFAULT : 0;
336 case JS_SET_TIMEOUT:
337 return get_user(joydev->glue.JS_TIMEOUT, (int *) arg);
338 case JS_GET_TIMEOUT:
339 return put_user(joydev->glue.JS_TIMEOUT, (int *) arg);
340 case JS_SET_TIMELIMIT:
341 return get_user(joydev->glue.JS_TIMELIMIT, (long *) arg);
342 case JS_GET_TIMELIMIT:
343 return put_user(joydev->glue.JS_TIMELIMIT, (long *) arg);
344 case JS_SET_ALL:
345 return copy_from_user(&joydev->glue, (struct JS_DATA_SAVE_TYPE *) arg,
346 sizeof(struct JS_DATA_SAVE_TYPE)) ? -EFAULT : 0;
347 case JS_GET_ALL:
348 return copy_to_user((struct JS_DATA_SAVE_TYPE *) arg, &joydev->glue,
349 sizeof(struct JS_DATA_SAVE_TYPE)) ? -EFAULT : 0;
350
351 case JSIOCGVERSION:
352 return put_user(JS_VERSION, (__u32 *) arg);
353 case JSIOCGAXES:
354 return put_user(joydev->nabs, (__u8 *) arg);
355 case JSIOCGBUTTONS:
356 return put_user(joydev->nkey, (__u8 *) arg);
357 case JSIOCSCORR:
358 return copy_from_user(joydev->corr, (struct js_corr *) arg,
359 sizeof(struct js_corr) * joydev->nabs) ? -EFAULT : 0;
360 case JSIOCGCORR:
361 return copy_to_user((struct js_corr *) arg, joydev->corr,
362 sizeof(struct js_corr) * joydev->nabs) ? -EFAULT : 0;
363 default:
364 if ((cmd & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) == JSIOCGNAME(0)) {
365 int len;
366 if (!dev->name) return 0;
367 len = strlen(dev->name) + 1;
368 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
369 if (copy_to_user((char *) arg, dev->name, len)) return -EFAULT;
370 return len;
371 }
372 }
373 return -EINVAL;
374 }
375
376 static struct file_operations joydev_fops = {
377 owner: THIS_MODULE,
378 read: joydev_read,
379 write: joydev_write,
380 poll: joydev_poll,
381 open: joydev_open,
382 release: joydev_release,
383 ioctl: joydev_ioctl,
384 fasync: joydev_fasync,
385 };
386
387 static struct input_handle *joydev_connect(struct input_handler *handler, struct input_dev *dev)
388 {
389 struct joydev *joydev;
390 int i, j, minor;
391
392 if (!(test_bit(EV_KEY, dev->evbit) && test_bit(EV_ABS, dev->evbit) &&
393 (test_bit(ABS_X, dev->absbit) || test_bit(ABS_Y, dev->absbit)) &&
394 (test_bit(BTN_TRIGGER, dev->keybit) || test_bit(BTN_A, dev->keybit)
395 || test_bit(BTN_1, dev->keybit)))) return NULL;
396
397 for (minor = 0; minor < JOYDEV_MINORS && joydev_table[minor]; minor++);
398 if (minor == JOYDEV_MINORS) {
399 printk(KERN_ERR "joydev: no more free joydev devices\n");
400 return NULL;
401 }
402
403 if (!(joydev = kmalloc(sizeof(struct joydev), GFP_KERNEL)))
404 return NULL;
405 memset(joydev, 0, sizeof(struct joydev));
406
407 init_waitqueue_head(&joydev->wait);
408
409 joydev->minor = minor;
410 joydev_table[minor] = joydev;
411
412 joydev->handle.dev = dev;
413 joydev->handle.handler = handler;
414 joydev->handle.private = joydev;
415
416 joydev->exist = 1;
417
418 for (i = 0; i < ABS_MAX; i++)
419 if (test_bit(i, dev->absbit)) {
420 joydev->absmap[i] = joydev->nabs;
421 joydev->abspam[joydev->nabs] = i;
422 joydev->nabs++;
423 }
424
425 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC; i++)
426 if (test_bit(i + BTN_MISC, dev->keybit)) {
427 joydev->keymap[i] = joydev->nkey;
428 joydev->keypam[joydev->nkey] = i + BTN_MISC;
429 joydev->nkey++;
430 }
431
432 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
433 if (test_bit(i + BTN_MISC, dev->keybit)) {
434 joydev->keymap[i] = joydev->nkey;
435 joydev->keypam[joydev->nkey] = i + BTN_MISC;
436 joydev->nkey++;
437 }
438
439 for (i = 0; i < joydev->nabs; i++) {
440 j = joydev->abspam[i];
441 if (dev->absmax[j] == dev->absmin[j]) {
442 joydev->corr[i].type = JS_CORR_NONE;
443 continue;
444 }
445 joydev->corr[i].type = JS_CORR_BROKEN;
446 joydev->corr[i].prec = dev->absfuzz[j];
447 joydev->corr[i].coef[0] = (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
448 joydev->corr[i].coef[1] = (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
449 joydev->corr[i].coef[2] = (1 << 29) / ((dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j]);
450 joydev->corr[i].coef[3] = (1 << 29) / ((dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j]);
451
452 joydev->abs[i] = joydev_correct(dev->abs[j], joydev->corr + i);
453 }
454
455 joydev->devfs = input_register_minor("js%d", minor, JOYDEV_MINOR_BASE);
456
457 printk(KERN_INFO "js%d: Joystick device for input%d\n", minor, dev->number);
458
459 return &joydev->handle;
460 }
461
462 static void joydev_disconnect(struct input_handle *handle)
463 {
464 struct joydev *joydev = handle->private;
465
466 joydev->exist = 0;
467
468 if (joydev->open) {
469 input_close_device(handle);
470 } else {
471 input_unregister_minor(joydev->devfs);
472 joydev_table[joydev->minor] = NULL;
473 kfree(joydev);
474 }
475 }
476
477 static struct input_handler joydev_handler = {
478 event: joydev_event,
479 connect: joydev_connect,
480 disconnect: joydev_disconnect,
481 fops: &joydev_fops,
482 minor: JOYDEV_MINOR_BASE,
483 };
484
485 static int __init joydev_init(void)
486 {
487 input_register_handler(&joydev_handler);
488 return 0;
489 }
490
491 static void __exit joydev_exit(void)
492 {
493 input_unregister_handler(&joydev_handler);
494 }
495
496 module_init(joydev_init);
497 module_exit(joydev_exit);
498
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.