| 1 |
/* |
| 2 |
* Mach Operating System |
| 3 |
* Copyright (c) 1991,1990,1989 Carnegie Mellon University |
| 4 |
* All Rights Reserved. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify and distribute this software and its |
| 7 |
* documentation is hereby granted, provided that both the copyright |
| 8 |
* notice and this permission notice appear in all copies of the |
| 9 |
* software, derivative works or modified versions, and any portions |
| 10 |
* thereof, and that both notices appear in supporting documentation. |
| 11 |
* |
| 12 |
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" |
| 13 |
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR |
| 14 |
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. |
| 15 |
* |
| 16 |
* Carnegie Mellon requests users of this software to return to |
| 17 |
* |
| 18 |
* Software Distribution Coordinator or [email protected] |
| 19 |
* School of Computer Science |
| 20 |
* Carnegie Mellon University |
| 21 |
* Pittsburgh PA 15213-3890 |
| 22 |
* |
| 23 |
* any improvements or extensions that they make and grant Carnegie Mellon |
| 24 |
* the rights to redistribute these changes. |
| 25 |
*/ |
| 26 |
/* ********************************************************************** |
| 27 |
File: kd_mouse.c |
| 28 |
Description: mouse driver as part of keyboard/display driver |
| 29 |
|
| 30 |
$ Header: $ |
| 31 |
|
| 32 |
Copyright Ing. C. Olivetti & C. S.p.A. 1989. |
| 33 |
All rights reserved. |
| 34 |
********************************************************************** */ |
| 35 |
/* |
| 36 |
Copyright 1988, 1989 by Olivetti Advanced Technology Center, Inc., |
| 37 |
Cupertino, California. |
| 38 |
|
| 39 |
All Rights Reserved |
| 40 |
|
| 41 |
Permission to use, copy, modify, and distribute this software and |
| 42 |
its documentation for any purpose and without fee is hereby |
| 43 |
granted, provided that the above copyright notice appears in all |
| 44 |
copies and that both the copyright notice and this permission notice |
| 45 |
appear in supporting documentation, and that the name of Olivetti |
| 46 |
not be used in advertising or publicity pertaining to distribution |
| 47 |
of the software without specific, written prior permission. |
| 48 |
|
| 49 |
OLIVETTI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE |
| 50 |
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, |
| 51 |
IN NO EVENT SHALL OLIVETTI BE LIABLE FOR ANY SPECIAL, INDIRECT, OR |
| 52 |
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 53 |
LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, |
| 54 |
NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUR OF OR IN CONNECTION |
| 55 |
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 56 |
*/ |
| 57 |
|
| 58 |
/* |
| 59 |
* Hacked up support for serial mouse connected to COM1, using Mouse |
| 60 |
* Systems 5-byte protocol at 1200 baud. This should work for |
| 61 |
* Mouse Systems, SummaMouse, and Logitek C7 mice. |
| 62 |
* |
| 63 |
* The interface provided by /dev/mouse is a series of events as |
| 64 |
* described in i386at/kd.h. |
| 65 |
*/ |
| 66 |
|
| 67 |
#include <mach/boolean.h> |
| 68 |
#include <sys/types.h> |
| 69 |
#include <kern/printf.h> |
| 70 |
#include <device/ds_routines.h> |
| 71 |
#include <device/device_types.h> |
| 72 |
#include <device/io_req.h> |
| 73 |
#include <device/subrs.h> |
| 74 |
#include <i386/ipl.h> |
| 75 |
#include <i386/irq.h> |
| 76 |
#include <i386/pio.h> |
| 77 |
#include <chips/busses.h> |
| 78 |
#include <i386at/com.h> |
| 79 |
#include <i386at/kd.h> |
| 80 |
#include <i386at/kd_queue.h> |
| 81 |
#include <i386at/i8250.h> |
| 82 |
|
| 83 |
#include "kd_mouse.h" |
| 84 |
|
| 85 |
static interrupt_handler_fn oldvect; /* old interrupt vector */ |
| 86 |
static int oldunit; |
| 87 |
extern struct bus_device *cominfo[]; |
| 88 |
|
| 89 |
kd_event_queue mouse_queue; /* queue of mouse events */ |
| 90 |
boolean_t mouse_in_use = FALSE; |
| 91 |
queue_head_t mouse_read_queue = { &mouse_read_queue, &mouse_read_queue }; |
| 92 |
|
| 93 |
|
| 94 |
/* |
| 95 |
* The state of the 3 buttons is encoded in the low-order 3 bits (both |
| 96 |
* here and in other variables in the driver). |
| 97 |
*/ |
| 98 |
u_char lastbuttons; /* previous state of mouse buttons */ |
| 99 |
#define MOUSE_UP 1 |
| 100 |
#define MOUSE_DOWN 0 |
| 101 |
#define MOUSE_ALL_UP 0x7 |
| 102 |
|
| 103 |
int mouse_baud = BCNT1200; |
| 104 |
|
| 105 |
boolean_t mouse_char_cmd = FALSE; /* mouse response is to cmd */ |
| 106 |
boolean_t mouse_char_wanted = FALSE; /* want mouse response */ |
| 107 |
int mouse_char_index; /* mouse response */ |
| 108 |
|
| 109 |
#define IBM_MOUSE_IRQ 12 |
| 110 |
|
| 111 |
/* |
| 112 |
* init_mouse_hw - initialize the serial port. |
| 113 |
*/ |
| 114 |
static void |
| 115 |
init_mouse_hw(dev_t unit, int mode) |
| 116 |
{ |
| 117 |
unsigned short base_addr = cominfo[unit]->address; |
| 118 |
|
| 119 |
outb(base_addr + RIE, 0); |
| 120 |
outb(base_addr + RLC, LCDLAB); |
| 121 |
outb(base_addr + RDLSB, mouse_baud & 0xff); |
| 122 |
outb(base_addr + RDMSB, (mouse_baud >> 8) & 0xff); |
| 123 |
outb(base_addr + RLC, mode); |
| 124 |
outb(base_addr + RMC, MCDTR | MCRTS | MCOUT2); |
| 125 |
outb(base_addr + RIE, IERD | IELS); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
/* |
| 130 |
* mouseopen - Verify that the request is read-only, initialize, |
| 131 |
* and remember process group leader. |
| 132 |
*/ |
| 133 |
/* |
| 134 |
* Low 3 bits of minor are the com port #. |
| 135 |
* The high 5 bits of minor are the mouse type |
| 136 |
*/ |
| 137 |
#define MOUSE_SYSTEM_MOUSE 0 |
| 138 |
#define MICROSOFT_MOUSE 1 |
| 139 |
#define IBM_MOUSE 2 |
| 140 |
#define NO_MOUSE 3 |
| 141 |
#define LOGITECH_TRACKMAN 4 |
| 142 |
#define MICROSOFT_MOUSE7 5 |
| 143 |
static int mouse_type; |
| 144 |
static int mousebufsize; |
| 145 |
static int mousebufindex = 0; |
| 146 |
int track_man[10]; |
| 147 |
|
| 148 |
/*ARGSUSED*/ |
| 149 |
int |
| 150 |
mouseopen(dev_t dev, int flags, io_req_t ior) |
| 151 |
{ |
| 152 |
if (mouse_in_use) |
| 153 |
return (D_ALREADY_OPEN); |
| 154 |
mouse_in_use = TRUE; /* locking? */ |
| 155 |
kdq_reset(&mouse_queue); |
| 156 |
lastbuttons = MOUSE_ALL_UP; |
| 157 |
|
| 158 |
switch (mouse_type = ((minor(dev) & 0xf8) >> 3)) { |
| 159 |
case MICROSOFT_MOUSE7: |
| 160 |
mousebufsize = 3; |
| 161 |
serial_mouse_open(dev); |
| 162 |
init_mouse_hw(dev&7, LC7); |
| 163 |
break; |
| 164 |
case MICROSOFT_MOUSE: |
| 165 |
mousebufsize = 3; |
| 166 |
serial_mouse_open(dev); |
| 167 |
init_mouse_hw(dev&7, LC8); |
| 168 |
break; |
| 169 |
case MOUSE_SYSTEM_MOUSE: |
| 170 |
mousebufsize = 5; |
| 171 |
serial_mouse_open(dev); |
| 172 |
init_mouse_hw(dev&7, LC8); |
| 173 |
break; |
| 174 |
case LOGITECH_TRACKMAN: |
| 175 |
mousebufsize = 3; |
| 176 |
serial_mouse_open(dev); |
| 177 |
init_mouse_hw(dev&7, LC7); |
| 178 |
track_man[0] = comgetc(dev&7); |
| 179 |
track_man[1] = comgetc(dev&7); |
| 180 |
if (track_man[0] != 0x4d && |
| 181 |
track_man[1] != 0x33) { |
| 182 |
printf("LOGITECH_TRACKMAN: NOT M3"); |
| 183 |
} |
| 184 |
break; |
| 185 |
case IBM_MOUSE: |
| 186 |
mousebufsize = 3; |
| 187 |
kd_mouse_open(dev, IBM_MOUSE_IRQ); |
| 188 |
ibm_ps2_mouse_open(dev); |
| 189 |
break; |
| 190 |
case NO_MOUSE: |
| 191 |
break; |
| 192 |
} |
| 193 |
mousebufindex = 0; |
| 194 |
return(0); |
| 195 |
} |
| 196 |
|
| 197 |
void |
| 198 |
serial_mouse_open(dev_t dev) |
| 199 |
{ |
| 200 |
int unit = minor(dev) & 0x7; |
| 201 |
int mouse_pic = cominfo[unit]->sysdep1; |
| 202 |
|
| 203 |
spl_t s = splhi(); /* disable interrupts */ |
| 204 |
|
| 205 |
oldvect = ivect[mouse_pic]; |
| 206 |
ivect[mouse_pic] = mouseintr; |
| 207 |
|
| 208 |
oldunit = iunit[mouse_pic]; |
| 209 |
iunit[mouse_pic] = unit; |
| 210 |
|
| 211 |
/* XXX other arrays to init? */ |
| 212 |
splx(s); /* XXX - should come after init? */ |
| 213 |
} |
| 214 |
|
| 215 |
int mouse_packets = 0; |
| 216 |
|
| 217 |
void |
| 218 |
kd_mouse_open( |
| 219 |
dev_t dev, |
| 220 |
int mouse_pic) |
| 221 |
{ |
| 222 |
spl_t s = splhi(); /* disable interrupts */ |
| 223 |
|
| 224 |
oldvect = ivect[mouse_pic]; |
| 225 |
ivect[mouse_pic] = kdintr; |
| 226 |
unmask_irq(mouse_pic); |
| 227 |
splx(s); |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
* mouseclose - Disable interrupts on the serial port, reset driver flags, |
| 232 |
* and restore the serial port interrupt vector. |
| 233 |
*/ |
| 234 |
void |
| 235 |
mouseclose( |
| 236 |
dev_t dev, |
| 237 |
int flags) |
| 238 |
{ |
| 239 |
switch (mouse_type) { |
| 240 |
case MICROSOFT_MOUSE: |
| 241 |
case MICROSOFT_MOUSE7: |
| 242 |
case MOUSE_SYSTEM_MOUSE: |
| 243 |
case LOGITECH_TRACKMAN: |
| 244 |
serial_mouse_close(dev, flags); |
| 245 |
break; |
| 246 |
case IBM_MOUSE: |
| 247 |
ibm_ps2_mouse_close(dev); |
| 248 |
kd_mouse_close(dev, IBM_MOUSE_IRQ); |
| 249 |
{int i = 20000; for (;i--;); } |
| 250 |
kd_mouse_drain(); |
| 251 |
break; |
| 252 |
case NO_MOUSE: |
| 253 |
break; |
| 254 |
} |
| 255 |
|
| 256 |
kdq_reset(&mouse_queue); /* paranoia */ |
| 257 |
mouse_in_use = FALSE; |
| 258 |
} |
| 259 |
|
| 260 |
/*ARGSUSED*/ |
| 261 |
void |
| 262 |
serial_mouse_close( |
| 263 |
dev_t dev, |
| 264 |
int flags) |
| 265 |
{ |
| 266 |
spl_t o_pri = splhi(); /* mutex with open() */ |
| 267 |
int unit = minor(dev) & 0x7; |
| 268 |
int mouse_pic = cominfo[unit]->sysdep1; |
| 269 |
unsigned short base_addr = cominfo[unit]->address; |
| 270 |
|
| 271 |
assert(ivect[mouse_pic] == mouseintr); |
| 272 |
outb(base_addr + RIE, 0); /* disable serial port */ |
| 273 |
outb(base_addr + RMC, 0); /* no rts */ |
| 274 |
ivect[mouse_pic] = oldvect; |
| 275 |
iunit[mouse_pic] = oldunit; |
| 276 |
|
| 277 |
(void)splx(o_pri); |
| 278 |
} |
| 279 |
|
| 280 |
void |
| 281 |
kd_mouse_close( |
| 282 |
dev_t dev, |
| 283 |
int mouse_pic) |
| 284 |
{ |
| 285 |
spl_t s = splhi(); |
| 286 |
|
| 287 |
mask_irq(mouse_pic); |
| 288 |
ivect[mouse_pic] = oldvect; |
| 289 |
splx(s); |
| 290 |
} |
| 291 |
|
| 292 |
io_return_t mousegetstat( |
| 293 |
dev_t dev, |
| 294 |
dev_flavor_t flavor, |
| 295 |
dev_status_t data, /* pointer to OUT array */ |
| 296 |
mach_msg_type_number_t *count) /* OUT */ |
| 297 |
{ |
| 298 |
switch (flavor) { |
| 299 |
case DEV_GET_SIZE: |
| 300 |
data[DEV_GET_SIZE_DEVICE_SIZE] = 0; |
| 301 |
data[DEV_GET_SIZE_RECORD_SIZE] = sizeof(kd_event); |
| 302 |
*count = DEV_GET_SIZE_COUNT; |
| 303 |
break; |
| 304 |
default: |
| 305 |
return D_INVALID_OPERATION; |
| 306 |
} |
| 307 |
return D_SUCCESS; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
/* |
| 312 |
* mouseread - dequeue and return any queued events. |
| 313 |
*/ |
| 314 |
int |
| 315 |
mouseread( |
| 316 |
dev_t dev, |
| 317 |
io_req_t ior) |
| 318 |
{ |
| 319 |
int err, count; |
| 320 |
spl_t s; |
| 321 |
|
| 322 |
/* Check if IO_COUNT is a multiple of the record size. */ |
| 323 |
if (ior->io_count % sizeof(kd_event) != 0) |
| 324 |
return D_INVALID_SIZE; |
| 325 |
|
| 326 |
err = device_read_alloc(ior, (vm_size_t)ior->io_count); |
| 327 |
if (err != KERN_SUCCESS) |
| 328 |
return (err); |
| 329 |
|
| 330 |
s = SPLKD(); |
| 331 |
if (kdq_empty(&mouse_queue)) { |
| 332 |
if (ior->io_mode & D_NOWAIT) { |
| 333 |
splx(s); |
| 334 |
return (D_WOULD_BLOCK); |
| 335 |
} |
| 336 |
ior->io_done = mouse_read_done; |
| 337 |
enqueue_tail(&mouse_read_queue, (queue_entry_t)ior); |
| 338 |
splx(s); |
| 339 |
return (D_IO_QUEUED); |
| 340 |
} |
| 341 |
count = 0; |
| 342 |
while (!kdq_empty(&mouse_queue) && count < ior->io_count) { |
| 343 |
kd_event *ev; |
| 344 |
|
| 345 |
ev = kdq_get(&mouse_queue); |
| 346 |
*(kd_event *)(&ior->io_data[count]) = *ev; |
| 347 |
count += sizeof(kd_event); |
| 348 |
} |
| 349 |
splx(s); |
| 350 |
ior->io_residual = ior->io_count - count; |
| 351 |
return (D_SUCCESS); |
| 352 |
} |
| 353 |
|
| 354 |
boolean_t mouse_read_done(io_req_t ior) |
| 355 |
{ |
| 356 |
int count; |
| 357 |
spl_t s; |
| 358 |
|
| 359 |
s = SPLKD(); |
| 360 |
if (kdq_empty(&mouse_queue)) { |
| 361 |
ior->io_done = mouse_read_done; |
| 362 |
enqueue_tail(&mouse_read_queue, (queue_entry_t)ior); |
| 363 |
splx(s); |
| 364 |
return (FALSE); |
| 365 |
} |
| 366 |
|
| 367 |
count = 0; |
| 368 |
while (!kdq_empty(&mouse_queue) && count < ior->io_count) { |
| 369 |
kd_event *ev; |
| 370 |
|
| 371 |
ev = kdq_get(&mouse_queue); |
| 372 |
*(kd_event *)(&ior->io_data[count]) = *ev; |
| 373 |
count += sizeof(kd_event); |
| 374 |
} |
| 375 |
splx(s); |
| 376 |
|
| 377 |
ior->io_residual = ior->io_count - count; |
| 378 |
ds_read_done(ior); |
| 379 |
|
| 380 |
return (TRUE); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
/* |
| 386 |
* mouseintr - Get a byte and pass it up for handling. Called at SPLKD. |
| 387 |
*/ |
| 388 |
void |
| 389 |
mouseintr(int unit) |
| 390 |
{ |
| 391 |
unsigned short base_addr = cominfo[unit]->address; |
| 392 |
unsigned char id, ls; |
| 393 |
|
| 394 |
/* get reason for interrupt and line status */ |
| 395 |
id = inb(base_addr + RID); |
| 396 |
ls = inb(base_addr + RLS); |
| 397 |
|
| 398 |
/* handle status changes */ |
| 399 |
if (id == IDLS) { |
| 400 |
if (ls & LSDR) { |
| 401 |
inb(base_addr + RDAT); /* flush bad character */ |
| 402 |
} |
| 403 |
return; /* ignore status change */ |
| 404 |
} |
| 405 |
|
| 406 |
if (id & IDRD) { |
| 407 |
mouse_handle_byte((u_char)(inb(base_addr + RDAT) & 0xff)); |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
/* |
| 413 |
* handle_byte - Accumulate bytes until we have an entire packet. |
| 414 |
* If the mouse has moved or any of the buttons have changed state (up |
| 415 |
* or down), enqueue the corresponding events. |
| 416 |
* Called at SPLKD. |
| 417 |
* XXX - magic numbers. |
| 418 |
*/ |
| 419 |
int show_mouse_byte = 0; |
| 420 |
/* |
| 421 |
X down; middle down; middle up; X up 50 0 0; 50 0 0 22; 50 0 0 02; 40 0 0 |
| 422 |
X down; middle down; X up; middle up 50 0 0; 50 0 0 22; 40 0 0 22; 40 0 0 2 |
| 423 |
* |
| 424 |
* The trick here is that all the while the middle button is down you get 4 byte |
| 425 |
* packets with the last byte 0x22. When the middle button goes up you get a |
| 426 |
* last packet with 0x02. |
| 427 |
*/ |
| 428 |
int lastgitech = 0x40; /* figure whether the first 3 bytes imply */ |
| 429 |
/* its time to expect a fourth */ |
| 430 |
int fourthgitech = 0; /* look for the 4th byte; we must process it */ |
| 431 |
int middlegitech = 0; /* what should the middle button be */ |
| 432 |
|
| 433 |
static u_char mousebuf[MOUSEBUFSIZE]; /* 5-byte packet from mouse */ |
| 434 |
|
| 435 |
void |
| 436 |
mouse_handle_byte(u_char ch) |
| 437 |
{ |
| 438 |
if (show_mouse_byte) { |
| 439 |
printf("%x(%c) ", ch, ch); |
| 440 |
} |
| 441 |
|
| 442 |
if (mouse_char_cmd) { |
| 443 |
/* |
| 444 |
* Mouse character is response to command |
| 445 |
*/ |
| 446 |
if (mousebufindex < mousebufsize) |
| 447 |
mousebuf[mousebufindex++] = ch; |
| 448 |
if (mouse_char_wanted) { |
| 449 |
mouse_char_wanted = FALSE; |
| 450 |
wakeup((vm_offset_t)&mousebuf); |
| 451 |
} |
| 452 |
return; |
| 453 |
} |
| 454 |
|
| 455 |
if (mousebufindex == 0) { |
| 456 |
switch (mouse_type) { |
| 457 |
case MICROSOFT_MOUSE7: |
| 458 |
if ((ch & 0x40) != 0x40) |
| 459 |
return; |
| 460 |
break; |
| 461 |
case MICROSOFT_MOUSE: |
| 462 |
if ((ch & 0xc0) != 0xc0) |
| 463 |
return; |
| 464 |
break; |
| 465 |
case MOUSE_SYSTEM_MOUSE: |
| 466 |
if ((ch & 0xf8) != 0x80) |
| 467 |
return; |
| 468 |
break; |
| 469 |
case LOGITECH_TRACKMAN: |
| 470 |
if (fourthgitech == 1) { |
| 471 |
fourthgitech = 0; |
| 472 |
if (ch & 0xf0) |
| 473 |
middlegitech = 0x4; |
| 474 |
else |
| 475 |
middlegitech = 0x0; |
| 476 |
mouse_packet_microsoft_mouse(mousebuf); |
| 477 |
return; |
| 478 |
} else if ((ch & 0xc0) != 0x40) |
| 479 |
return; |
| 480 |
break; |
| 481 |
case IBM_MOUSE: |
| 482 |
break; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
mousebuf[mousebufindex++] = ch; |
| 487 |
if (mousebufindex < mousebufsize) |
| 488 |
return; |
| 489 |
|
| 490 |
/* got a packet */ |
| 491 |
mousebufindex = 0; |
| 492 |
|
| 493 |
switch (mouse_type) { |
| 494 |
case MICROSOFT_MOUSE7: |
| 495 |
case MICROSOFT_MOUSE: |
| 496 |
mouse_packet_microsoft_mouse(mousebuf); |
| 497 |
break; |
| 498 |
case MOUSE_SYSTEM_MOUSE: |
| 499 |
mouse_packet_mouse_system_mouse(mousebuf); |
| 500 |
break; |
| 501 |
case LOGITECH_TRACKMAN: |
| 502 |
if ( mousebuf[1] || mousebuf[2] || |
| 503 |
mousebuf[0] != lastgitech) { |
| 504 |
mouse_packet_microsoft_mouse(mousebuf); |
| 505 |
lastgitech = mousebuf[0] & 0xf0; |
| 506 |
} else { |
| 507 |
fourthgitech = 1; |
| 508 |
} |
| 509 |
break; |
| 510 |
case IBM_MOUSE: |
| 511 |
mouse_packet_ibm_ps2_mouse(mousebuf); |
| 512 |
break; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
void |
| 517 |
mouse_packet_mouse_system_mouse(u_char mousebuf[MOUSEBUFSIZE]) |
| 518 |
{ |
| 519 |
u_char buttons, buttonchanges; |
| 520 |
struct mouse_motion moved; |
| 521 |
|
| 522 |
buttons = mousebuf[0] & 0x7; /* get current state of buttons */ |
| 523 |
buttonchanges = buttons ^ lastbuttons; |
| 524 |
moved.mm_deltaX = (char)mousebuf[1] + (char)mousebuf[3]; |
| 525 |
moved.mm_deltaY = (char)mousebuf[2] + (char)mousebuf[4]; |
| 526 |
|
| 527 |
if (moved.mm_deltaX != 0 || moved.mm_deltaY != 0) |
| 528 |
mouse_moved(moved); |
| 529 |
|
| 530 |
if (buttonchanges != 0) { |
| 531 |
lastbuttons = buttons; |
| 532 |
if (buttonchanges & 1) |
| 533 |
mouse_button(MOUSE_RIGHT, buttons & 1); |
| 534 |
if (buttonchanges & 2) |
| 535 |
mouse_button(MOUSE_MIDDLE, (buttons & 2) >> 1); |
| 536 |
if (buttonchanges & 4) |
| 537 |
mouse_button(MOUSE_LEFT, (buttons & 4) >> 2); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
/* same as above for microsoft mouse */ |
| 542 |
/* |
| 543 |
* 3 byte microsoft format used |
| 544 |
* |
| 545 |
* 7 6 5 4 3 2 1 0 |
| 546 |
* 1 1 L R Y7 Y6 X7 X6 |
| 547 |
* 1 0 X5 X4 X3 X3 X1 X0 |
| 548 |
* 1 0 Y5 Y4 Y3 Y2 Y1 Y0 |
| 549 |
* |
| 550 |
*/ |
| 551 |
void |
| 552 |
mouse_packet_microsoft_mouse(u_char mousebuf[MOUSEBUFSIZE]) |
| 553 |
{ |
| 554 |
u_char buttons, buttonchanges; |
| 555 |
struct mouse_motion moved; |
| 556 |
|
| 557 |
buttons = ((mousebuf[0] & 0x30) >> 4); |
| 558 |
buttons |= middlegitech; |
| 559 |
/* get current state of buttons */ |
| 560 |
#ifdef gross_hack |
| 561 |
if (buttons == 0x03) /* both buttons down */ |
| 562 |
buttons = 0x04; |
| 563 |
#endif /* gross_hack */ |
| 564 |
buttons = (~buttons) & 0x07; /* convert to not pressed */ |
| 565 |
|
| 566 |
buttonchanges = buttons ^ lastbuttons; |
| 567 |
moved.mm_deltaX = ((mousebuf[0] & 0x03) << 6) | (mousebuf[1] & 0x3F); |
| 568 |
moved.mm_deltaY = ((mousebuf[0] & 0x0c) << 4) | (mousebuf[2] & 0x3F); |
| 569 |
if (moved.mm_deltaX & 0x80) /* negative, in fact */ |
| 570 |
moved.mm_deltaX = moved.mm_deltaX - 0x100; |
| 571 |
if (moved.mm_deltaY & 0x80) /* negative, in fact */ |
| 572 |
moved.mm_deltaY = moved.mm_deltaY - 0x100; |
| 573 |
/* and finally the Y orientation is different for the microsoft mouse */ |
| 574 |
moved.mm_deltaY = -moved.mm_deltaY; |
| 575 |
|
| 576 |
if (moved.mm_deltaX != 0 || moved.mm_deltaY != 0) |
| 577 |
mouse_moved(moved); |
| 578 |
|
| 579 |
if (buttonchanges != 0) { |
| 580 |
lastbuttons = buttons; |
| 581 |
if (buttonchanges & 1) |
| 582 |
mouse_button(MOUSE_RIGHT, (buttons & 1) ? |
| 583 |
MOUSE_UP : MOUSE_DOWN); |
| 584 |
if (buttonchanges & 2) |
| 585 |
mouse_button(MOUSE_LEFT, (buttons & 2) ? |
| 586 |
MOUSE_UP : MOUSE_DOWN); |
| 587 |
if (buttonchanges & 4) |
| 588 |
mouse_button(MOUSE_MIDDLE, (buttons & 4) ? |
| 589 |
MOUSE_UP : MOUSE_DOWN); |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
/* |
| 594 |
* AUX device (PS2) open/close |
| 595 |
*/ |
| 596 |
|
| 597 |
/* |
| 598 |
* Write character to mouse. Called at spltty. |
| 599 |
*/ |
| 600 |
static void kd_mouse_write( |
| 601 |
unsigned char ch) |
| 602 |
{ |
| 603 |
while (inb(K_STATUS) & K_IBUF_FUL) |
| 604 |
continue; /* wait for 'input' port empty */ |
| 605 |
outb(K_CMD, 0xd4); /* send next character to mouse */ |
| 606 |
|
| 607 |
while (inb(K_STATUS) & K_IBUF_FUL) |
| 608 |
continue; /* wait for 'input' port empty */ |
| 609 |
outb(K_RDWR, ch); /* send command to mouse */ |
| 610 |
} |
| 611 |
|
| 612 |
/* |
| 613 |
* Read next character from mouse, waiting for interrupt |
| 614 |
* to deliver it. Called at spltty. |
| 615 |
*/ |
| 616 |
static int kd_mouse_read(void) |
| 617 |
{ |
| 618 |
int ch; |
| 619 |
|
| 620 |
if (mouse_char_index >= mousebufsize) |
| 621 |
return -1; |
| 622 |
|
| 623 |
while (mousebufindex <= mouse_char_index) { |
| 624 |
mouse_char_wanted = TRUE; |
| 625 |
assert_wait((event_t) &mousebuf, FALSE); |
| 626 |
/* We are at tty SPL level, interrupts can not happen between |
| 627 |
* assert_wait and thread_block. */ |
| 628 |
thread_block((void (*)()) 0); |
| 629 |
} |
| 630 |
|
| 631 |
ch = mousebuf[mouse_char_index++]; |
| 632 |
|
| 633 |
return ch; |
| 634 |
} |
| 635 |
|
| 636 |
/* |
| 637 |
* Prepare buffer for receiving next packet from mouse. |
| 638 |
*/ |
| 639 |
static void kd_mouse_read_reset(void) |
| 640 |
{ |
| 641 |
mousebufindex = 0; |
| 642 |
mouse_char_index = 0; |
| 643 |
} |
| 644 |
|
| 645 |
void |
| 646 |
ibm_ps2_mouse_open(dev_t dev) |
| 647 |
{ |
| 648 |
spl_t s = spltty(); |
| 649 |
|
| 650 |
lastbuttons = 0; |
| 651 |
mouse_char_cmd = TRUE; /* responses are to commands */ |
| 652 |
|
| 653 |
kd_sendcmd(0xa8); /* enable mouse in kbd */ |
| 654 |
|
| 655 |
kd_cmdreg_write(0x47); /* allow mouse interrupts */ |
| 656 |
/* magic number for ibm? */ |
| 657 |
|
| 658 |
kd_mouse_read_reset(); |
| 659 |
kd_mouse_write(0xff); /* reset mouse */ |
| 660 |
if (kd_mouse_read() != 0xfa) { |
| 661 |
splx(s); |
| 662 |
return; /* need ACK */ |
| 663 |
} |
| 664 |
|
| 665 |
(void) kd_mouse_read(); /* discard 2-character mouse ID */ |
| 666 |
(void) kd_mouse_read(); |
| 667 |
|
| 668 |
kd_mouse_read_reset(); |
| 669 |
kd_mouse_write(0xea); /* set stream mode */ |
| 670 |
if (kd_mouse_read() != 0xfa) { |
| 671 |
splx(s); |
| 672 |
return; /* need ACK */ |
| 673 |
} |
| 674 |
|
| 675 |
kd_mouse_read_reset(); |
| 676 |
kd_mouse_write(0xf4); /* enable */ |
| 677 |
if (kd_mouse_read() != 0xfa) { |
| 678 |
splx(s); |
| 679 |
return; /* need ACK */ |
| 680 |
} |
| 681 |
|
| 682 |
kd_mouse_read_reset(); |
| 683 |
mouse_char_cmd = FALSE; /* now we get mouse packets */ |
| 684 |
|
| 685 |
splx(s); |
| 686 |
} |
| 687 |
|
| 688 |
void |
| 689 |
ibm_ps2_mouse_close(dev_t dev) |
| 690 |
{ |
| 691 |
spl_t s = spltty(); |
| 692 |
|
| 693 |
mouse_char_cmd = TRUE; /* responses are to commands */ |
| 694 |
|
| 695 |
kd_mouse_read_reset(); |
| 696 |
kd_mouse_write(0xff); /* reset mouse */ |
| 697 |
if (kd_mouse_read() == 0xfa) { |
| 698 |
/* got ACK: discard 2-char mouse ID */ |
| 699 |
(void) kd_mouse_read(); |
| 700 |
(void) kd_mouse_read(); |
| 701 |
} |
| 702 |
|
| 703 |
kd_sendcmd(0xa7); /* disable mouse in kbd */ |
| 704 |
kd_cmdreg_write(0x65); /* disallow mouse interrupts */ |
| 705 |
/* magic number for ibm? */ |
| 706 |
|
| 707 |
splx(s); |
| 708 |
} |
| 709 |
|
| 710 |
/* |
| 711 |
* 3 byte ibm ps2 format used |
| 712 |
* |
| 713 |
* 7 6 5 4 3 2 1 0 |
| 714 |
* YO XO YS XS 1 M R L |
| 715 |
* X7 X6 X5 X4 X3 X3 X1 X0 |
| 716 |
* Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0 |
| 717 |
* |
| 718 |
*/ |
| 719 |
void |
| 720 |
mouse_packet_ibm_ps2_mouse(u_char mousebuf[MOUSEBUFSIZE]) |
| 721 |
{ |
| 722 |
u_char buttons, buttonchanges; |
| 723 |
struct mouse_motion moved; |
| 724 |
|
| 725 |
buttons = mousebuf[0] & 0x7; /* get current state of buttons */ |
| 726 |
buttonchanges = buttons ^ lastbuttons; |
| 727 |
moved.mm_deltaX = ((mousebuf[0]&0x10) ? 0xffffff00 : 0 ) | (u_char)mousebuf[1]; |
| 728 |
moved.mm_deltaY = ((mousebuf[0]&0x20) ? 0xffffff00 : 0 ) | (u_char)mousebuf[2]; |
| 729 |
if (mouse_packets) { |
| 730 |
printf("(%x:%x:%x)", mousebuf[0], mousebuf[1], mousebuf[2]); |
| 731 |
return; |
| 732 |
} |
| 733 |
|
| 734 |
if (moved.mm_deltaX != 0 || moved.mm_deltaY != 0) |
| 735 |
mouse_moved(moved); |
| 736 |
|
| 737 |
if (buttonchanges != 0) { |
| 738 |
lastbuttons = buttons; |
| 739 |
if (buttonchanges & 1) |
| 740 |
mouse_button(MOUSE_LEFT, !(buttons & 1)); |
| 741 |
if (buttonchanges & 2) |
| 742 |
mouse_button(MOUSE_RIGHT, !((buttons & 2) >> 1)); |
| 743 |
if (buttonchanges & 4) |
| 744 |
mouse_button(MOUSE_MIDDLE, !((buttons & 4) >> 2)); |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
/* |
| 749 |
* Enqueue a mouse-motion event. Called at SPLKD. |
| 750 |
*/ |
| 751 |
void |
| 752 |
mouse_moved(struct mouse_motion where) |
| 753 |
{ |
| 754 |
kd_event ev; |
| 755 |
|
| 756 |
ev.type = MOUSE_MOTION; |
| 757 |
/* Not used but we set it to avoid garbage */ |
| 758 |
ev.unused_time.seconds = 0; |
| 759 |
ev.unused_time.microseconds = 0; |
| 760 |
ev.value.mmotion = where; |
| 761 |
mouse_enqueue(&ev); |
| 762 |
} |
| 763 |
|
| 764 |
/* |
| 765 |
* Enqueue an event for mouse button press or release. Called at SPLKD. |
| 766 |
*/ |
| 767 |
void |
| 768 |
mouse_button( |
| 769 |
kev_type which, |
| 770 |
u_char direction) |
| 771 |
{ |
| 772 |
kd_event ev; |
| 773 |
|
| 774 |
ev.type = which; |
| 775 |
ev.value.up = (direction == MOUSE_UP) ? TRUE : FALSE; |
| 776 |
/* Not used but we set it to avoid garbage */ |
| 777 |
ev.unused_time.seconds = 0; |
| 778 |
ev.unused_time.microseconds = 0; |
| 779 |
mouse_enqueue(&ev); |
| 780 |
} |
| 781 |
|
| 782 |
/* |
| 783 |
* mouse_enqueue - enqueue an event and wake up selecting processes, if |
| 784 |
* any. Called at SPLKD. |
| 785 |
*/ |
| 786 |
|
| 787 |
void |
| 788 |
mouse_enqueue(kd_event *ev) |
| 789 |
{ |
| 790 |
if (kdq_full(&mouse_queue)) |
| 791 |
printf_once("mouse: queue full\n"); |
| 792 |
else |
| 793 |
kdq_put(&mouse_queue, ev); |
| 794 |
|
| 795 |
{ |
| 796 |
io_req_t ior; |
| 797 |
while ((ior = (io_req_t)dequeue_head(&mouse_read_queue)) != 0) |
| 798 |
iodone(ior); |
| 799 |
} |
| 800 |
} |