source: rtems/c/src/lib/libbsp/i386/pc386/console/ps2_mouse.c @ 68f4e5f

4.104.114.84.95
Last change on this file since 68f4e5f was 68f4e5f, checked in by Jennifer Averett <Jennifer.Averett@…>, on 04/18/05 at 17:21:58

2005-04-18 Eric Valette <eric.valette@…>

  • clock/ckinit.c, console/console.c, console/ps2_mouse.c, console/serial_mouse.c, timer/timer.c: Added parameter to irq handler
  • Property mode set to 100644
File size: 14.9 KB
Line 
1/*
2 * linux/drivers/char/pc_keyb.c
3 * Separation of the PC low-level part by Geert Uytterhoeven, May 1997
4 * See keyboard.c for the whole history.
5 * Major cleanup by Martin Mares, May 1997
6 * Combined the keyboard and PS/2 mouse handling into one file,
7 * because they share the same hardware.
8 * Johan Myreen <jem@iki.fi> 1998-10-08.
9 * Code fixes to handle mouse ACKs properly.
10 * C. Scott Ananian <cananian@alumni.princeton.edu> 1999-01-29.
11 *
12 * RTEMS port: by Rosimildo da Silva.
13 * This module was ported from Linux.
14 *
15 */
16
17#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include <errno.h>
21#include <sys/types.h>
22
23#include <bsp.h>
24#include <irq.h>
25#include <rtems/libio.h>
26#include <termios.h>
27#include <i386_io.h>
28#include <rtems/mw_uid.h>
29
30#define  INITIALIZE_MOUSE
31/* Some configuration switches are present in the include file... */
32#include "ps2_mouse.h"
33#include "mouse_parser.h"
34
35static void kbd_write_command_w(int data);
36#if 0
37static void kbd_write_output_w(int data);
38#endif
39
40static unsigned char handle_kbd_event(void);
41
42/* used only by send_data - set by keyboard_interrupt */
43static volatile unsigned char reply_expected = 0;
44static volatile unsigned char acknowledge = 0;
45static volatile unsigned char resend = 0;
46
47/*
48 *      PS/2 Auxiliary Device
49 */
50static int psaux_init(void);
51
52static struct aux_queue *queue; /* Mouse data buffer. */
53static int aux_count = 0;
54/* used when we send commands to the mouse that expect an ACK. */
55static unsigned char mouse_reply_expected = 0;
56
57#define AUX_INTS_OFF (KBD_MODE_KCC | KBD_MODE_DISABLE_MOUSE | KBD_MODE_SYS | KBD_MODE_KBD_INT)
58#define AUX_INTS_ON  (KBD_MODE_KCC | KBD_MODE_SYS | KBD_MODE_MOUSE_INT | KBD_MODE_KBD_INT)
59#define MAX_RETRIES     60              /* some aux operations take long time*/
60
61static void ps2_mouse_interrupt();
62
63static void ( *driver_input_handler_ps2 )( void *,  char *, int ) = 0;
64
65/*
66 * This routine sets the handler to handle the characters received
67 * from the serial port.
68 */
69void ps2_set_driver_handler( int port, void ( *handler )( void *,  char *, int ) )
70{
71   driver_input_handler_ps2 = handler;
72}
73
74static void mdelay( unsigned long t )
75{
76  Wait_X_ms( t );
77}
78
79static void*    termios_ttyp_paux = NULL;
80
81static void
82isr_on(const rtems_irq_connect_data *unused)
83{
84  return;
85}
86
87static void
88isr_off(const rtems_irq_connect_data *unused)
89{
90  return;
91}
92
93static int isr_is_on(const rtems_irq_connect_data *irq)
94{
95  return BSP_irq_enabled_at_i8259s( irq->name );
96}
97
98static rtems_irq_connect_data ps2_isr_data = { AUX_IRQ,
99                                               ps2_mouse_interrupt,
100                                               0,
101                                               isr_on, 
102                                               isr_off,
103                                               isr_is_on };
104
105/*
106 * Wait for keyboard controller input buffer to drain.
107 *
108 * Don't use 'jiffies' so that we don't depend on
109 * interrupts..
110 *
111 * Quote from PS/2 System Reference Manual:
112 *
113 * "Address hex 0060 and address hex 0064 should be written only when
114 * the input-buffer-full bit and output-buffer-full bit in the
115 * Controller Status register are set 0."
116 */
117
118static void kb_wait(void)
119{
120        unsigned long timeout = KBC_TIMEOUT;
121
122        do {
123                /*
124                 * "handle_kbd_event()" will handle any incoming events
125                 * while we wait - keypresses or mouse movement.
126                 */
127                unsigned char status = handle_kbd_event();
128
129                if (! (status & KBD_STAT_IBF))
130                        return;
131
132                mdelay(1);
133
134                timeout--;
135        } while (timeout);
136#ifdef KBD_REPORT_TIMEOUTS
137        printk( "Keyboard timed out[1]\n");
138#endif
139}
140
141static int do_acknowledge(unsigned char scancode)
142{
143        if (reply_expected) {
144          /* Unfortunately, we must recognise these codes only if we know they
145           * are known to be valid (i.e., after sending a command), because there
146           * are some brain-damaged keyboards (yes, FOCUS 9000 again) which have
147           * keys with such codes :(
148           */
149                if (scancode == KBD_REPLY_ACK) {
150                        acknowledge = 1;
151                        reply_expected = 0;
152                        return 0;
153                } else if (scancode == KBD_REPLY_RESEND) {
154                        resend = 1;
155                        reply_expected = 0;
156                        return 0;
157                }
158                /* Should not happen... */
159#if 0
160                printk( "keyboard reply expected - got %02x\n",
161                       scancode);
162#endif
163        }
164        return 1;
165}
166
167static inline void handle_mouse_event(unsigned char scancode)
168{
169        if (mouse_reply_expected) {
170                if (scancode == AUX_ACK) {
171                        mouse_reply_expected--;
172                        return;
173                }
174                mouse_reply_expected = 0;
175        }
176
177        if (aux_count) {
178                int head = queue->head;
179
180                queue->buf[head] = scancode;
181                head = (head + 1) & (AUX_BUF_SIZE-1);
182                if (head != queue->tail) {
183                        queue->head = head;
184                }
185      /* if the input queue is active, add to it */
186      if( driver_input_handler_ps2 )
187      {
188          driver_input_handler_ps2( NULL,  &scancode, 1 );
189      }
190      else
191      {
192         /* post this byte to termios */
193              rtems_termios_enqueue_raw_characters( termios_ttyp_paux, &scancode, 1 );
194      }
195        }
196}
197
198/*
199 * This reads the keyboard status port, and does the
200 * appropriate action.
201 *
202 * It requires that we hold the keyboard controller
203 * spinlock.
204 */
205static unsigned char handle_kbd_event(void)
206{
207        unsigned char status = kbd_read_status();
208        unsigned int work = 10000;
209
210        while (status & KBD_STAT_OBF) {
211                unsigned char scancode;
212
213                scancode = kbd_read_input();
214
215                if (status & KBD_STAT_MOUSE_OBF) {
216                        handle_mouse_event(scancode);
217                } else {
218                        do_acknowledge(scancode);
219         printk("pc_keyb: %X ", scancode );
220                }
221                status = kbd_read_status();
222                if(!work--)
223                {
224                        printk("pc_keyb: controller jammed (0x%02X).\n",
225                                status);
226                        break;
227                }
228        }
229
230        return status;
231}
232
233static void ps2_mouse_interrupt()
234{
235        handle_kbd_event();
236}
237
238/*
239 * send_data sends a character to the keyboard and waits
240 * for an acknowledge, possibly retrying if asked to. Returns
241 * the success status.
242 *
243 * Don't use 'jiffies', so that we don't depend on interrupts
244 */
245#if 0
246static int send_data(unsigned char data)
247{
248        int retries = 3;
249
250        do {
251                unsigned long timeout = KBD_TIMEOUT;
252
253                acknowledge = 0; /* Set by interrupt routine on receipt of ACK. */
254                resend = 0;
255                reply_expected = 1;
256                kbd_write_output_w(data);
257                for (;;) {
258                        if (acknowledge)
259                                return 1;
260                        if (resend)
261                                break;
262                        mdelay(1);
263                        if (!--timeout) {
264#ifdef KBD_REPORT_TIMEOUTS
265                                printk( "Keyboard timeout[2]\n");
266#endif
267                                return 0;
268                        }
269                }
270        } while (retries-- > 0);
271#ifdef KBD_REPORT_TIMEOUTS
272        printk( "keyboard: Too many NACKs -- noisy kbd cable?\n");
273#endif
274        return 0;
275}
276#endif
277
278#if 0
279#define KBD_NO_DATA     (-1)    /* No data */
280#define KBD_BAD_DATA    (-2)    /* Parity or other error */
281
282static int kbd_read_data(void)
283{
284        int retval = KBD_NO_DATA;
285        unsigned char status;
286
287        status = kbd_read_status();
288        if (status & KBD_STAT_OBF) {
289                unsigned char data = kbd_read_input();
290
291                retval = data;
292                if (status & (KBD_STAT_GTO | KBD_STAT_PERR))
293                        retval = KBD_BAD_DATA;
294        }
295        return retval;
296}
297
298static void kbd_clear_input(void)
299{
300        int maxread = 100;      /* Random number */
301
302        do {
303                if (kbd_read_data() == KBD_NO_DATA)
304                        break;
305        } while (--maxread);
306}
307
308static int kbd_wait_for_input(void)
309{
310        long timeout = KBD_INIT_TIMEOUT;
311
312        do {
313                int retval = kbd_read_data();
314                if (retval >= 0)
315                        return retval;
316                mdelay(1);
317        } while (--timeout);
318        return -1;
319}
320#endif
321
322static void kbd_write_command_w(int data)
323{
324        kb_wait();
325        kbd_write_command(data);
326}
327
328#if 0
329static void kbd_write_output_w(int data)
330{
331        kb_wait();
332        kbd_write_output(data);
333}
334#endif
335
336static void kbd_write_cmd(int cmd)
337{
338        kb_wait();
339        kbd_write_command(KBD_CCMD_WRITE_MODE);
340        kb_wait();
341        kbd_write_output(cmd);
342}
343
344/*
345 * Check if this is a dual port controller.
346 */
347static int detect_auxiliary_port(void)
348{
349        int loops = 10;
350        int retval = 0;
351
352        /* Put the value 0x5A in the output buffer using the "Write
353         * Auxiliary Device Output Buffer" command (0xD3). Poll the
354         * Status Register for a while to see if the value really
355         * turns up in the Data Register. If the KBD_STAT_MOUSE_OBF
356         * bit is also set to 1 in the Status Register, we assume this
357         * controller has an Auxiliary Port (a.k.a. Mouse Port).
358         */
359        kb_wait();
360        kbd_write_command(KBD_CCMD_WRITE_AUX_OBUF);
361
362        kb_wait();
363        kbd_write_output(0x5a); /* 0x5a is a random dummy value. */
364
365        do {
366                unsigned char status = kbd_read_status();
367
368                if (status & KBD_STAT_OBF) {
369                        (void) kbd_read_input();
370                        if (status & KBD_STAT_MOUSE_OBF) {
371                                printk( "Detected PS/2 Mouse Port.\n");
372                                retval = 1;
373                        }
374                        break;
375                }
376                mdelay(1);
377        } while (--loops);
378        return retval;
379}
380
381/*
382 * Send a byte to the mouse.
383 */
384static void aux_write_dev(int val)
385{
386        kb_wait();
387        kbd_write_command(KBD_CCMD_WRITE_MOUSE);
388        kb_wait();
389        kbd_write_output(val);
390}
391
392/*
393 * Send a byte to the mouse & handle returned ack
394 */
395static void aux_write_ack(int val)
396{
397        kb_wait();
398        kbd_write_command(KBD_CCMD_WRITE_MOUSE);
399        kb_wait();
400        kbd_write_output(val);
401        /* we expect an ACK in response. */
402        mouse_reply_expected++;
403        kb_wait();
404}
405
406static unsigned char get_from_queue(void)
407{
408        unsigned char result;
409        result = queue->buf[queue->tail];
410        queue->tail = (queue->tail + 1) & (AUX_BUF_SIZE-1);
411        return result;
412}
413
414static int queue_empty(void)
415{
416        return queue->head == queue->tail;
417}
418
419/*
420 * Random magic cookie for the aux device
421 */
422#define AUX_DEV ((void *)queue)
423
424static int release_aux()
425{
426        if (--aux_count)
427                return 0;
428        kbd_write_cmd(AUX_INTS_OFF);                        /* Disable controller ints */
429        kbd_write_command_w(KBD_CCMD_MOUSE_DISABLE);
430
431   BSP_remove_rtems_irq_handler( &ps2_isr_data );
432        return 0;
433}
434/*
435 * Install interrupt handler.
436 * Enable auxiliary device.
437 */
438
439static int open_aux()
440{
441  rtems_status_code status;
442
443        if (aux_count++) {
444                return 0;
445        }
446        queue->head = queue->tail = 0;          /* Flush input queue */
447
448   status = BSP_install_rtems_irq_handler( &ps2_isr_data );
449   if( !status )
450        {
451          printk("Error installing ps2-mouse interrupt handler!\n" );
452          rtems_fatal_error_occurred( status );
453        }
454
455        kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE);     /* Enable the
456                                                           auxiliary port on
457                                                           controller. */
458        aux_write_ack(AUX_ENABLE_DEV); /* Enable aux device */
459        kbd_write_cmd(AUX_INTS_ON); /* Enable controller ints */
460        return 0;
461}
462
463/*
464 * Put bytes from input queue to buffer.
465 */
466size_t read_aux(char * buffer, size_t count )
467{
468        size_t i = count;
469        unsigned char c;
470
471        if (queue_empty())
472        {
473                return 0;
474   }
475        while (i > 0 && !queue_empty())
476        {
477                c = get_from_queue();
478                *buffer++ = c;
479                i--;
480        }
481        return count-i;
482}
483
484/*
485 * Write to the aux device.
486 */
487
488static int write_aux( int minor, const char * buffer, int count )
489{
490        int retval = 0;
491
492        if (count) {
493                int written = 0;
494
495                if (count > 32)
496                        count = 32; /* Limit to 32 bytes. */
497                do {
498                        char c;
499                        c = *buffer++;
500                        aux_write_dev(c);
501                        written++;
502                } while (--count);
503                retval = -EIO;
504                if (written) {
505                        retval = written;
506                }
507        }
508        return retval;
509}
510
511#if 0
512static unsigned int aux_poll()
513{
514        if( !queue_empty() )
515                return 1;
516        return 0;
517}
518#endif
519
520static int psaux_init( void )
521{
522        if( !detect_auxiliary_port() )
523   {
524        printk( "PS/2 - mouse not found.\n" );
525                return -EIO;
526   }
527
528        queue = (struct aux_queue *)malloc( sizeof(*queue) );
529   memset(queue, 0, sizeof(*queue));
530        queue->head = queue->tail = 0;
531        queue->proc_list = NULL;
532
533#ifdef INITIALIZE_MOUSE
534        kbd_write_command_w(KBD_CCMD_MOUSE_ENABLE); /* Enable Aux. */
535        aux_write_ack(AUX_SET_SAMPLE);
536        aux_write_ack(100);                           /* 100 samples/sec */
537        aux_write_ack(AUX_SET_RES);
538        aux_write_ack(3);                                /* 8 counts per mm */
539        aux_write_ack(AUX_SET_SCALE21); /* 2:1 scaling */
540#endif /* INITIALIZE_MOUSE */
541        kbd_write_command(KBD_CCMD_MOUSE_DISABLE); /* Disable aux device. */
542        kbd_write_cmd(AUX_INTS_OFF); /* Disable controller ints. */
543        return 0;
544}
545
546void paux_reserve_resources(rtems_configuration_table *conf)
547{
548  rtems_termios_reserve_resources(conf, 1);
549  return;
550}
551
552/*
553 * paux device driver INITIALIZE entry point.
554 */
555rtems_device_driver
556paux_initialize(  rtems_device_major_number major,
557                   rtems_device_minor_number minor,
558                   void                      *arg)
559{
560   rtems_status_code status;
561
562  /*
563   * Set up TERMIOS
564   */
565   rtems_termios_initialize();
566
567        printk( "PS/2 mouse probe.\n" );
568   if( psaux_init() < 0 )
569   {
570      printk("Error detecting PS/2 mouse --\n");
571
572      /* we might want to finish the application here !!! */
573   }
574   open_aux();
575
576  /*
577   * Register the device
578   */
579  status = rtems_io_register_name ("/dev/mouse", major, 0);
580  if (status != RTEMS_SUCCESSFUL)
581  {
582      printk("Error registering paux device!\n");
583      rtems_fatal_error_occurred (status);
584  }
585  return RTEMS_SUCCESSFUL;
586} /* tty_initialize */
587
588static int paux_last_close(int major, int minor, void *arg)
589{
590  release_aux();
591  return 0;
592}
593
594/*
595 * Write to the aux device. This routine is invoked by the
596 * termios framework whenever the "ECHO" feature is on.
597 * It does nothing write now.
598 */
599static int write_aux_echo( int minor, const char * buffer, int count )
600{
601   return 0;
602}
603
604/*
605 * paux device driver OPEN entry point
606 */
607rtems_device_driver
608paux_open(rtems_device_major_number major,
609                rtems_device_minor_number minor,
610                void                      *arg)
611{
612  rtems_status_code              status;
613  static rtems_termios_callbacks cb =
614  {
615    NULL,                 /* firstOpen */
616    paux_last_close,      /* lastClose */
617    NULL,                 /* poll read  */
618    write_aux_echo,       /* write */
619    NULL,                 /* setAttributes */
620    NULL,                 /* stopRemoteTx */
621    NULL,                 /* startRemoteTx */
622    0                     /* outputUsesInterrupts */
623  };
624
625  status = rtems_termios_open (major, minor, arg, &cb );
626  termios_ttyp_paux = ( (rtems_libio_open_close_args_t *)arg)->iop->data1;
627  return status;
628}
629
630/*
631 * paux device driver CLOSE entry point
632 */
633rtems_device_driver
634paux_close(rtems_device_major_number major,
635           rtems_device_minor_number minor,
636           void                      *arg)
637{
638  return (rtems_termios_close (arg));
639}
640
641/*
642 * paux device driver READ entry point.
643 * Read characters from the PS/2 mouse.
644 */
645rtems_device_driver
646paux_read(rtems_device_major_number major,
647          rtems_device_minor_number minor,
648         void                      *arg)
649{
650  return rtems_termios_read (arg);
651} /* tty_read */
652
653/*
654 * paux device driver WRITE entry point.
655 * Write characters to the PS/2 mouse.
656 */
657rtems_device_driver
658paux_write(rtems_device_major_number major,
659          rtems_device_minor_number minor,
660          void                    * arg)
661{
662  rtems_libio_rw_args_t *rw_args = (rtems_libio_rw_args_t *)arg;
663  char                  *buffer  = rw_args->buffer;
664  int                    maximum  = rw_args->count;
665  rw_args->bytes_moved = write_aux( minor, buffer, maximum );
666  return RTEMS_SUCCESSFUL;
667} /* tty_write */
668
669/*
670 * Handle ioctl request.
671 */
672rtems_device_driver
673paux_control(rtems_device_major_number major,
674             rtems_device_minor_number minor,
675             void                      * arg
676)
677{
678        rtems_libio_ioctl_args_t *args = arg;
679        switch( args->command )
680        {
681           default:
682      return rtems_termios_ioctl (arg);
683                break;
684
685      case MW_UID_REGISTER_DEVICE:
686      printk( "PS2 Mouse: reg=%s\n", args->buffer );
687      register_mou_msg_queue( args->buffer, -1 );
688                break;
689
690      case MW_UID_UNREGISTER_DEVICE:
691      unregister_mou_msg_queue( -1 );
692                break;
693   }
694        args->ioctl_return = 0;
695   return RTEMS_SUCCESSFUL;
696}
Note: See TracBrowser for help on using the repository browser.