summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/nds/console/console.c
blob: 7c7ab9d9261496ea1b8221605e232294b64f07ac (plain) (blame)
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
189
/*
 * RTEMS for Nintendo DS console driver.
 *
 * Copyright (c) 2008 by Renaud Voltz <renaud.voltz@gmail.com>
 *                       Matthieu Bucchianeri <mbucchia@gmail.com>
 *
 * 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 <bsp.h>
#include <nds.h>
#include <rtems/libio.h>
#include <nds/arm9/console.h>
#include <sys/iosupport.h>

#include <rtems/mw_uid.h>

/*
 * enables testsuite output to desmume. this is used to pass the rtems
 * testsuite.
 * comment the following line to disable (recommended).
 */

//#define TESTSUITE

/*
 * printk support.
 */

/* from NDS support library */
extern void consolePrintChar(char c);
void
nds_putch (char c)
{
#ifdef TESTSUITE
  __asm__ volatile ("swi $0x1");
#endif
  consolePrintChar (c);
}

static volatile char ch = 0;

void
console_push (char c)
{
  ch = c;
}

int
nds_getch (void)
{
  char c;

  while (ch == 0);
  c = ch;
  ch = 0;
  return c;
}

BSP_output_char_function_type BSP_output_char = nds_putch;
BSP_polling_getchar_function_type BSP_poll_char = nds_getch;

/*
 * console write operation.
 */

static ssize_t
nds_write (int minor, const char *buf, size_t len)
{
  int count;

  for (count = 0; count < len; count++) {
    nds_putch (buf[count]);
  }

  return 0;
}

/*
 * console read operation.
 */

static int
nds_read (int minor)
{
  return nds_getch ();
}

/*
 * from touchscreen/parser.c
 */

void register_kbd_msg_queue (char *q_name);
void unregister_kbd_msg_queue (void);

/*
 * Console driver
 */

rtems_device_driver
console_initialize (rtems_device_major_number major,
                    rtems_device_minor_number minor, void *arg)
{
  rtems_status_code status;

  printk ("[+] console started\n");

  rtems_termios_initialize ();

  status = rtems_io_register_name ("/dev/console", major, 0);
  if (status != RTEMS_SUCCESSFUL) {
    printk ("[!] error registering console\n");
    rtems_fatal_error_occurred (status);
  }

  return (RTEMS_SUCCESSFUL);
}

rtems_device_driver
console_open (rtems_device_major_number major,
              rtems_device_minor_number minor, void *arg)
{
  rtems_status_code status;
  static rtems_termios_callbacks cb = {
    NULL,                       /* firstOpen     */
    NULL,                       /* lastClose     */
    nds_read,                   /* pollRead      */
    nds_write,                  /* write         */
    NULL,                       /* setAttributes */
    NULL,                       /* stopRemoteTx  */
    NULL,                       /* startRemoteTx */
    0                           /* 1 = outputUsesInterrupts */
  };

  status = rtems_termios_open (major, minor, arg, &cb);
  if (status != RTEMS_SUCCESSFUL)
    printk ("[!] error opening console\n");

  return (status);
}

rtems_device_driver
console_close (rtems_device_major_number major,
               rtems_device_minor_number minor, void *arg)
{
  rtems_device_driver res = RTEMS_SUCCESSFUL;

  res = rtems_termios_close (arg);

  return res;
}

rtems_device_driver
console_read (rtems_device_major_number major,
              rtems_device_minor_number minor, void *arg)
{
  return rtems_termios_read (arg);
}

rtems_device_driver
console_write (rtems_device_major_number major,
               rtems_device_minor_number minor, void *arg)
{
  return rtems_termios_write (arg);
}

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

  switch (args->command) {
  case MW_UID_REGISTER_DEVICE:
    register_kbd_msg_queue (args->buffer);
    break;
  case MW_UID_UNREGISTER_DEVICE:
    unregister_kbd_msg_queue ();
    break;
  default:
    return rtems_termios_ioctl (arg);
  }
  args->ioctl_return = 0;

  return RTEMS_SUCCESSFUL;
}