2 * $Id: mousedev.c,v 1.10 2000/06/23 09:23:00 vojtech Exp $ 4 * Copyright (c) 1999-2000 Vojtech Pavlik 6 * Input driver to PS/2 or ImPS/2 device driver module. 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 * Should you need to contact me, the author, you can do so either by 27 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail: 28 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic 31 #define MOUSEDEV_MINOR_BASE 32 32 #define MOUSEDEV_MINORS 32 33 #define MOUSEDEV_MIX 31 35 #include <linux/malloc.h> 36 #include <linux/poll.h> 37 #include <linux/module.h> 38 #include <linux/init.h> 39 #include <linux/input.h> 40 #include <linux/config.h> 42 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X 43 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024 45 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y 46 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768 53 wait_queue_head_t wait
; 54 struct mousedev_list
*list
; 55 struct input_handle handle
; 59 struct mousedev_list
{ 60 struct fasync_struct
*fasync
; 61 struct mousedev
*mousedev
; 62 struct mousedev_list
*next
; 63 int dx
, dy
, dz
, oldx
, oldy
; 65 unsigned long buttons
; 66 unsigned char ready
, buffer
, bufsiz
; 67 unsigned char mode
, genseq
, impseq
; 70 #define MOUSEDEV_GENIUS_LEN 5 71 #define MOUSEDEV_IMPS_LEN 6 73 static unsigned char mousedev_genius_seq
[] = {0xe8,3,0xe6,0xe6,0xe6}; 74 static unsigned char mousedev_imps_seq
[] = {0xf3,200,0xf3,100,0xf3,80}; 76 static struct input_handler mousedev_handler
; 78 static struct mousedev
*mousedev_table
[MOUSEDEV_MINORS
]; 79 static struct mousedev mousedev_mix
; 81 static voidmousedev_event(struct input_handle
*handle
,unsigned int type
,unsigned int code
,int value
) 83 struct mousedev
*mousedevs
[3] = { handle
->private, &mousedev_mix
, NULL
}; 84 struct mousedev
**mousedev
= mousedevs
; 85 struct mousedev_list
*list
; 89 list
= (*mousedev
)->list
; 95 size
= handle
->dev
->absmax
[ABS_X
] - handle
->dev
->absmin
[ABS_X
]; 96 list
->dx
+= (value
* CONFIG_INPUT_MOUSEDEV_SCREEN_X
- list
->oldx
) / size
; 97 list
->oldx
+= list
->dx
* size
; 100 size
= handle
->dev
->absmax
[ABS_Y
] - handle
->dev
->absmin
[ABS_Y
]; 101 list
->dy
-= (value
* CONFIG_INPUT_MOUSEDEV_SCREEN_Y
- list
->oldy
) / size
; 102 list
->oldy
-= list
->dy
* size
; 108 case REL_X
: list
->dx
+= value
;break; 109 case REL_Y
: list
->dy
-= value
;break; 110 case REL_WHEEL
:if(list
->mode
) list
->dz
-= value
;break; 118 case BTN_LEFT
: index
=0;break; 120 case BTN_EXTRA
:if(list
->mode
>1) { index
=4;break; } 123 case BTN_RIGHT
: index
=1;break; 125 case BTN_SIDE
:if(list
->mode
>1) { index
=3;break; } 128 case BTN_MIDDLE
: index
=2;break; 132 case0:clear_bit(index
, &list
->buttons
);break; 133 case1:set_bit(index
, &list
->buttons
);break; 141 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
); 146 wake_up_interruptible(&((*mousedev
)->wait
)); 151 static intmousedev_fasync(int fd
,struct file
*file
,int on
) 154 struct mousedev_list
*list
= file
->private_data
; 155 retval
=fasync_helper(fd
, file
, on
, &list
->fasync
); 156 return retval
<0? retval
:0; 159 static intmousedev_release(struct inode
* inode
,struct file
* file
) 161 struct mousedev_list
*list
= file
->private_data
; 162 struct mousedev_list
**listptr
= &list
->mousedev
->list
; 164 mousedev_fasync(-1, file
,0); 166 while(*listptr
&& (*listptr
!= list
)) 167 listptr
= &((*listptr
)->next
); 168 *listptr
= (*listptr
)->next
; 170 if(!--list
->mousedev
->open
) { 171 if(list
->mousedev
->minor
== MOUSEDEV_MIX
) { 172 struct input_handle
*handle
= mousedev_handler
.handle
; 174 struct mousedev
*mousedev
= handle
->private; 175 if(!mousedev
->open
) { 176 if(mousedev
->exist
) { 177 input_close_device(&mousedev
->handle
); 179 input_unregister_minor(mousedev
->devfs
); 180 mousedev_table
[mousedev
->minor
] = NULL
; 184 handle
= handle
->hnext
; 187 if(!mousedev_mix
.open
) { 188 if(list
->mousedev
->exist
) { 189 input_close_device(&list
->mousedev
->handle
); 191 input_unregister_minor(list
->mousedev
->devfs
); 192 mousedev_table
[list
->mousedev
->minor
] = NULL
; 193 kfree(list
->mousedev
); 204 static intmousedev_open(struct inode
* inode
,struct file
* file
) 206 struct mousedev_list
*list
; 207 int i
=MINOR(inode
->i_rdev
) - MOUSEDEV_MINOR_BASE
; 209 if(i
> MOUSEDEV_MINORS
|| !mousedev_table
[i
]) 212 if(!(list
=kmalloc(sizeof(struct mousedev_list
), GFP_KERNEL
))) 214 memset(list
,0,sizeof(struct mousedev_list
)); 216 list
->mousedev
= mousedev_table
[i
]; 217 list
->next
= mousedev_table
[i
]->list
; 218 mousedev_table
[i
]->list
= list
; 219 file
->private_data
= list
; 221 if(!list
->mousedev
->open
++) { 222 if(list
->mousedev
->minor
== MOUSEDEV_MIX
) { 223 struct input_handle
*handle
= mousedev_handler
.handle
; 225 struct mousedev
*mousedev
= handle
->private; 228 input_open_device(handle
); 229 handle
= handle
->hnext
; 232 if(!mousedev_mix
.open
) 233 if(list
->mousedev
->exist
) 234 input_open_device(&list
->mousedev
->handle
); 241 static voidmousedev_packet(struct mousedev_list
*list
,unsigned char off
) 243 list
->ps2
[off
] =0x08| ((list
->dx
<0) <<4) | ((list
->dy
<0) <<5) | (list
->buttons
&0x07); 244 list
->ps2
[off
+1] = (list
->dx
>127?127: (list
->dx
< -127? -127: list
->dx
)); 245 list
->ps2
[off
+2] = (list
->dy
>127?127: (list
->dy
< -127? -127: list
->dy
)); 246 list
->dx
-= list
->ps2
[off
+1]; 247 list
->dy
-= list
->ps2
[off
+2]; 248 list
->bufsiz
= off
+3; 251 list
->ps2
[off
] |= ((list
->buttons
&0x30) <<2); 254 list
->ps2
[off
+3] = (list
->dz
>127?127: (list
->dz
< -127? -127: list
->dz
)); 256 list
->dz
-= list
->ps2
[off
+3]; 258 if(!list
->dx
&& !list
->dy
&& (!list
->mode
|| !list
->dz
)) list
->ready
=0; 259 list
->buffer
= list
->bufsiz
; 263 static ssize_t
mousedev_write(struct file
* file
,const char* buffer
,size_t count
, loff_t
*ppos
) 265 struct mousedev_list
*list
= file
->private_data
; 269 for(i
=0; i
< count
; i
++) { 273 if(c
== mousedev_genius_seq
[list
->genseq
]) { 274 if(++list
->genseq
== MOUSEDEV_GENIUS_LEN
) { 279 }else list
->genseq
=0; 281 if(c
== mousedev_imps_seq
[list
->impseq
]) { 282 if(++list
->impseq
== MOUSEDEV_IMPS_LEN
) { 287 }else list
->impseq
=0; 295 mousedev_packet(list
,1); 298 case0xf2:/* Get ID */ 299 list
->ps2
[1] = (list
->mode
==1) ?3:0; 303 case0xe9:/* Get info */ 305 list
->ps2
[1] =0x00; list
->ps2
[2] =0x33; list
->ps2
[3] =0x55; 307 list
->ps2
[1] =0x60; list
->ps2
[2] =3; list
->ps2
[3] =200; 313 list
->buffer
= list
->bufsiz
; 316 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
); 318 wake_up_interruptible(&list
->mousedev
->wait
); 323 static ssize_t
mousedev_read(struct file
* file
,char* buffer
,size_t count
, loff_t
*ppos
) 325 DECLARE_WAITQUEUE(wait
, current
); 326 struct mousedev_list
*list
= file
->private_data
; 329 if(!list
->ready
&& !list
->buffer
) { 331 add_wait_queue(&list
->mousedev
->wait
, &wait
); 332 current
->state
= TASK_INTERRUPTIBLE
; 334 while(!list
->ready
) { 336 if(file
->f_flags
& O_NONBLOCK
) { 340 if(signal_pending(current
)) { 341 retval
= -ERESTARTSYS
; 348 current
->state
= TASK_RUNNING
; 349 remove_wait_queue(&list
->mousedev
->wait
, &wait
); 356 mousedev_packet(list
,0); 358 if(count
> list
->buffer
) 359 count
= list
->buffer
; 361 if(copy_to_user(buffer
, list
->ps2
+ list
->bufsiz
- list
->buffer
, count
)) 364 list
->buffer
-= count
; 369 /* No kernel lock - fine */ 370 static unsigned intmousedev_poll(struct file
*file
, poll_table
*wait
) 372 struct mousedev_list
*list
= file
->private_data
; 373 poll_wait(file
, &list
->mousedev
->wait
, wait
); 374 if(list
->ready
|| list
->buffer
) 375 return POLLIN
| POLLRDNORM
; 379 struct file_operations mousedev_fops
= { 382 write
: mousedev_write
, 385 release
: mousedev_release
, 386 fasync
: mousedev_fasync
, 389 static struct input_handle
*mousedev_connect(struct input_handler
*handler
,struct input_dev
*dev
) 391 struct mousedev
*mousedev
; 394 if(!test_bit(EV_KEY
, dev
->evbit
) || 395 (!test_bit(BTN_LEFT
, dev
->keybit
) && !test_bit(BTN_TOUCH
, dev
->keybit
))) 398 if((!test_bit(EV_REL
, dev
->evbit
) || !test_bit(REL_X
, dev
->relbit
)) && 399 (!test_bit(EV_ABS
, dev
->evbit
) || !test_bit(ABS_X
, dev
->absbit
))) 402 for(minor
=0; minor
< MOUSEDEV_MINORS
&& mousedev_table
[minor
]; minor
++); 403 if(mousedev_table
[minor
]) { 404 printk(KERN_ERR
"mousedev: no more free mousedev devices\n"); 408 if(!(mousedev
=kmalloc(sizeof(struct mousedev
), GFP_KERNEL
))) 410 memset(mousedev
,0,sizeof(struct mousedev
)); 411 init_waitqueue_head(&mousedev
->wait
); 414 mousedev
->minor
= minor
; 415 mousedev_table
[minor
] = mousedev
; 417 mousedev
->handle
.dev
= dev
; 418 mousedev
->handle
.handler
= handler
; 419 mousedev
->handle
.private= mousedev
; 421 mousedev
->devfs
=input_register_minor("mouse%d", minor
, MOUSEDEV_MINOR_BASE
); 423 if(mousedev_mix
.open
) 424 input_open_device(&mousedev
->handle
); 426 printk(KERN_INFO
"mouse%d: PS/2 mouse device for input%d\n", minor
, dev
->number
); 428 return&mousedev
->handle
; 431 static voidmousedev_disconnect(struct input_handle
*handle
) 433 struct mousedev
*mousedev
= handle
->private; 438 input_close_device(handle
); 440 if(mousedev_mix
.open
) 441 input_close_device(handle
); 442 input_unregister_minor(mousedev
->devfs
); 443 mousedev_table
[mousedev
->minor
] = NULL
; 448 static struct input_handler mousedev_handler
= { 449 event
: mousedev_event
, 450 connect
: mousedev_connect
, 451 disconnect
: mousedev_disconnect
, 452 fops
: &mousedev_fops
, 453 minor
: MOUSEDEV_MINOR_BASE
, 456 static int __init
mousedev_init(void) 458 input_register_handler(&mousedev_handler
); 460 memset(&mousedev_mix
,0,sizeof(struct mousedev
)); 461 init_waitqueue_head(&mousedev_mix
.wait
); 462 mousedev_table
[MOUSEDEV_MIX
] = &mousedev_mix
; 463 mousedev_mix
.exist
=1; 464 mousedev_mix
.minor
= MOUSEDEV_MIX
; 465 mousedev_mix
.devfs
=input_register_minor("mice", MOUSEDEV_MIX
, MOUSEDEV_MINOR_BASE
); 467 printk(KERN_INFO
"mice: PS/2 mouse device common for all mice\n"); 472 static void __exit
mousedev_exit(void) 474 input_unregister_minor(mousedev_mix
.devfs
); 475 input_unregister_handler(&mousedev_handler
); 478 module_init(mousedev_init
); 479 module_exit(mousedev_exit
);