summaryrefslogtreecommitdiff
path: root/cpukit/libmisc/mouse/serial_mouse.c
blob: 874903a607a07f2ec651e8210aff01e9c6aa1d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * @file
 *
 * @brief Serial Mouse Driver
 * @ingroup libmisc_mouse Serial Mouse Driver
 */

/*
 *  COPYRIGHT (c) 1989-2011.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <rtems/bspIo.h>
#include <rtems/libio.h>
#include <termios.h>
#include <rtems/termiostypes.h>
#include <rtems/mouse_parser.h>
#include <rtems/serial_mouse.h>

int         serial_mouse_fd = -1;
const char *serial_mouse_device;
const char *serial_mouse_type;

static int serial_mouse_l_rint(int c, struct rtems_termios_tty *tp)
{
  unsigned char buf = c;

  /* call mouse_parser( void *ptr, char *buffer, int size ) */
  mouse_parser_enqueue( &buf, 1 );
  return 0;
}

static struct rtems_termios_linesw serial_mouse_linesw = {
  .l_open = NULL,
  .l_close = NULL,
  .l_read  = NULL,
  .l_write = NULL,
  .l_rint  = serial_mouse_l_rint,
  .l_start = NULL,
  .l_ioctl = NULL,
  .l_modem = NULL
};


/*
 *  Serial Mouse - device driver INITIALIZE entry point.
 */
rtems_device_driver serial_mouse_initialize(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *arg
)
{
  bsp_get_serial_mouse_device(
    &serial_mouse_device,
    &serial_mouse_type
  );

  (void) rtems_io_register_name( "/dev/mouse", major, 0 );

  rtems_termios_linesw[ 6 ] = serial_mouse_linesw;

  return RTEMS_SUCCESSFUL;
}

/*
 * serial_mouse - device driver OPEN entry point
 */
rtems_device_driver serial_mouse_open(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *args
)
{
  struct termios  termios_attr;
  int             status;
  int             disc = 6;

  /* XXX open(2) the configured /dev/comX */
  /* XXX save the file descriptor */
  serial_mouse_fd = open( serial_mouse_device, O_RDONLY );
  if ( serial_mouse_fd == -1 ) {
   printk(
     "Error opening serial_mouse device on %s\n",
     serial_mouse_device
   );
   return RTEMS_IO_ERROR;
  }

  /* 1200-8-N-1, without hardware flow control */
  /* BSP_uart_init( BSP_UART_PORT, 1200, CHR_8_BITS, 0, 0, 0 ); */
  status = tcgetattr(serial_mouse_fd, &termios_attr );
  if (status != 0) {
    printk("Error getting mouse attributes\n");
    return RTEMS_IO_ERROR;
  }
  termios_attr.c_lflag &= ~(ICANON|ECHO|ECHONL|ECHOK|ECHOE|ECHOPRT|ECHOCTL);
  termios_attr.c_iflag &= ~(IXON|IXANY|IXOFF);
  /*
  termios_attr.c_cc[VMIN] = itask_VMIN;
  termios_attr.c_cc[VTIME] = itask_VTIME;
  */
  termios_attr.c_cflag |= B1200;
  termios_attr.c_cflag |= CS8;
  status = tcsetattr( serial_mouse_fd, TCSANOW, &termios_attr );
  if (status != 0) {
    printk("Error setting mouse attributes\n");
    return RTEMS_IO_ERROR;
  }

  status = ioctl(serial_mouse_fd, TIOCSETD, &disc);
  if (status != 0) {
    printk("Error setting mouse attributes\n");
    return RTEMS_IO_ERROR;
  }

  sleep(5);
  return RTEMS_SUCCESSFUL;
}

rtems_device_driver serial_mouse_close(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *arg
)
{
  close( serial_mouse_fd );

  return RTEMS_SUCCESSFUL;
}

rtems_device_driver serial_mouse_read(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *arg
)
{
  return RTEMS_SUCCESSFUL;
}


rtems_device_driver serial_mouse_write(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *arg
)
{
  return RTEMS_SUCCESSFUL;
}


rtems_device_driver serial_mouse_control(
  rtems_device_major_number  major,
  rtems_device_minor_number  minor,
  void                      *arg
)
{
  rtems_libio_ioctl_args_t *args = (rtems_libio_ioctl_args_t *)arg;

  switch( args->command ) {

    case MW_UID_REGISTER_DEVICE:
      printk( "SerialMouse: reg=%s\n", (const char*) args->buffer );
      mouse_parser_initialize( serial_mouse_type );
      break;

    case MW_UID_UNREGISTER_DEVICE:
      break;

    default:
      args->ioctl_return = ioctl(serial_mouse_fd, args->command, args->buffer );
      if ( !args->ioctl_return )
        return RTEMS_SUCCESSFUL;
      return RTEMS_IO_ERROR;
  }
  args->ioctl_return = 0;
  return RTEMS_SUCCESSFUL;
}