source: rtems/c/src/lib/libbsp/m68k/mcf5206elite/console/console.c @ cfaa366

4.115
Last change on this file since cfaa366 was cfaa366, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 17:55:58

General - Remove extraneous blank line in license message

Many files had an extra blank line in the license text
found in the file header. This patch removes that line.

The script that did this also turned off execute permission
when it was turned on incorrectly.

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Console driver for Motorola MCF5206E UART modules
3 *
4 * Copyright (C) 2000 OKTET Ltd., St.-Petersburg, Russia
5 * Author: Victor V. Vengerov <vvv@oktet.ru>
6 *
7 *  COPYRIGHT (c) 1989-1998.
8 *  On-Line Applications Research Corporation (OAR).
9 *
10 *  The license and distribution terms for this file may be
11 *  found in the file LICENSE in this distribution or at
12 *  http://www.rtems.com/license/LICENSE.
13 *
14 *  $Id$
15 */
16
17#include <termios.h>
18#include <bsp.h>
19#include <rtems/libio.h>
20#include "mcf5206/mcf5206e.h"
21#include "mcf5206/mcfuart.h"
22
23/* Descriptor structures for two on-chip UART channels */
24static mcfuart uart[2];
25
26/* Console operations mode:
27 *     0 - raw (non-termios) polled input/output
28 *     1 - termios-based polled input/output
29 *     2 - termios-based interrupt-driven input/output
30 */
31int console_mode = 2;
32#define CONSOLE_MODE_RAW  (0)
33#define CONSOLE_MODE_POLL (1)
34#define CONSOLE_MODE_INT  (2)
35
36/* Wrapper functions for MCF UART generic driver */
37
38/* console_poll_read --
39 *     wrapper for poll read function
40 *
41 * PARAMETERS:
42 *     minor - minor device number
43 *
44 * RETURNS:
45 *     character code readed from UART, or -1 if there is no characters
46 *     available
47 */
48static int
49console_poll_read(int minor)
50{
51    return mcfuart_poll_read(&uart[minor]);
52}
53
54/* console_interrupt_write --
55 *     wrapper for interrupt write function
56 *
57 * PARAMETERS:
58 *     minor - minor device number
59 *     buf - output buffer
60 *     len - output buffer length
61 *
62 * RETURNS:
63 *     result code
64 */
65static ssize_t
66console_interrupt_write(int minor, const char *buf, size_t len)
67{
68    return mcfuart_interrupt_write(&uart[minor], buf, len);
69}
70
71/* console_poll_write --
72 *     wrapper for polling mode write function
73 *
74 * PARAMETERS:
75 *     minor - minor device number
76 *     buf - output buffer
77 *     len - output buffer length
78 *
79 * RETURNS:
80 *     result code
81 */
82static ssize_t
83console_poll_write(int minor, const char *buf, size_t len)
84{
85    return mcfuart_poll_write(&uart[minor], buf, len);
86}
87
88/* console_set_attributes --
89 *     wrapper for hardware-dependent termios attributes setting
90 *
91 * PARAMETERS:
92 *     minor - minor device number
93 *     t - pointer to the termios structure
94 *
95 * RETURNS:
96 *     result code
97 */
98static int
99console_set_attributes(int minor, const struct termios *t)
100{
101    return mcfuart_set_attributes(&uart[minor], t);
102}
103
104/* console_stop_remote_tx --
105 *     wrapper for stopping data flow from remote party.
106 *
107 * PARAMETERS:
108 *     minor - minor device number
109 *
110 * RETURNS:
111 *     result code
112 */
113static int
114console_stop_remote_tx(int minor)
115{
116    if (minor < sizeof(uart)/sizeof(uart[0]))
117        return mcfuart_stop_remote_tx(&uart[minor]);
118    else
119        return RTEMS_INVALID_NUMBER;
120}
121
122/* console_start_remote_tx --
123 *     wrapper for resuming data flow from remote party.
124 *
125 * PARAMETERS:
126 *     minor - minor device number
127 *
128 */
129static int
130console_start_remote_tx(int minor)
131{
132    if (minor < sizeof(uart)/sizeof(uart[0]))
133        return mcfuart_start_remote_tx(&uart[minor]);
134    else
135        return RTEMS_INVALID_NUMBER;
136}
137
138/* console_first_open --
139 *     wrapper for UART controller initialization functions
140 *
141 * PARAMETERS:
142 *     major - major device number
143 *     minor - minor device number
144 *     arg - libio device open argument
145 *
146 * RETURNS:
147 *     error code
148 */
149static int
150console_first_open(int major, int minor, void *arg)
151{
152    rtems_libio_open_close_args_t *args = arg;
153    rtems_status_code sc;
154    uint8_t         intvec;
155
156    switch (minor)
157    {
158        case 0: intvec = BSP_INTVEC_UART1; break;
159        case 1: intvec = BSP_INTVEC_UART2; break;
160        default:
161            return RTEMS_INVALID_NUMBER;
162    }
163
164    if (console_mode != CONSOLE_MODE_INT)
165    {
166        intvec = 0;
167    }
168
169    sc = mcfuart_init(&uart[minor],          /* uart */
170                      args->iop->data1,      /* tty */
171                      intvec,                /* interrupt vector number */
172                      minor+1);
173
174    if (sc == RTEMS_SUCCESSFUL)
175        sc = mcfuart_reset(&uart[minor]);
176
177    return sc;
178}
179
180/* console_last_close --
181 *     wrapper for UART controller close function
182 *
183 * PARAMETERS:
184 *     major - major device number
185 *     minor - minor device number
186 *     arg - libio device close argument
187 *
188 * RETURNS:
189 *     error code
190 */
191static int
192console_last_close(int major, int minor, void *arg)
193{
194    return mcfuart_disable(&uart[minor]);
195}
196
197/* console_initialize --
198 *     This routine initializes the console IO drivers and register devices
199 *     in RTEMS I/O system.
200 *
201 * PARAMETERS:
202 *     major - major console device number
203 *     minor - minor console device number (not used)
204 *     arg - device initialize argument
205 *
206 * RETURNS:
207 *     RTEMS error code (RTEMS_SUCCESSFUL if device initialized successfuly)
208 */
209rtems_device_driver
210console_initialize(rtems_device_major_number major,
211                   rtems_device_minor_number minor,
212                   void *arg)
213{
214    rtems_status_code status;
215
216    /*
217     * Set up TERMIOS
218     */
219    if (console_mode != CONSOLE_MODE_RAW)
220        rtems_termios_initialize ();
221
222    /*
223     * Register the devices
224     */
225    status = rtems_io_register_name ("/dev/console", major, 0);
226    if (status != RTEMS_SUCCESSFUL)
227        rtems_fatal_error_occurred (status);
228    status = rtems_io_register_name ("/dev/aux", major, 1);
229    if (status != RTEMS_SUCCESSFUL)
230        rtems_fatal_error_occurred (status);
231
232    if (console_mode == CONSOLE_MODE_RAW)
233    {
234        rtems_status_code sc;
235        sc = mcfuart_init(&uart[0],              /* uart */
236                          NULL,                  /* tty */
237                          0,                     /* interrupt vector number */
238                          1);                    /* UART channel number */
239
240        if (sc == RTEMS_SUCCESSFUL)
241            sc = mcfuart_reset(&uart[0]);
242
243        sc = mcfuart_init(&uart[1],              /* uart */
244                          NULL,                  /* tty */
245                          0,                     /* interrupt vector number */
246                          2);                    /* UART channel number */
247
248        if (sc == RTEMS_SUCCESSFUL)
249            sc = mcfuart_reset(&uart[1]);
250        return sc;
251    }
252
253    return RTEMS_SUCCESSFUL;
254}
255
256/* console_open --
257 *     Open console device driver. Pass appropriate termios callback
258 *     functions to termios library.
259 *
260 * PARAMETERS:
261 *     major - major device number for console devices
262 *     minor - minor device number for console
263 *     arg - device opening argument
264 *
265 * RETURNS:
266 *     RTEMS error code
267 */
268rtems_device_driver
269console_open(rtems_device_major_number major,
270             rtems_device_minor_number minor,
271             void *arg)
272{
273    static const rtems_termios_callbacks intr_callbacks = {
274        console_first_open,        /* firstOpen */
275        console_last_close,        /* lastClose */
276        NULL,                      /* pollRead */
277        console_interrupt_write,   /* write */
278        console_set_attributes,    /* setAttributes */
279        console_stop_remote_tx,    /* stopRemoteTx */
280        console_start_remote_tx,   /* startRemoteTx */
281        1                          /* outputUsesInterrupts */
282    };
283    static const rtems_termios_callbacks poll_callbacks = {
284        console_first_open,        /* firstOpen */
285        console_last_close,        /* lastClose */
286        console_poll_read,         /* pollRead */
287        console_poll_write,        /* write */
288        console_set_attributes,    /* setAttributes */
289        console_stop_remote_tx,    /* stopRemoteTx */
290        console_start_remote_tx,   /* startRemoteTx */
291        0                          /* outputUsesInterrupts */
292    };
293
294    switch (console_mode)
295    {
296        case CONSOLE_MODE_RAW:
297            return RTEMS_SUCCESSFUL;
298
299        case CONSOLE_MODE_INT:
300            return rtems_termios_open(major, minor, arg, &intr_callbacks);
301
302        case CONSOLE_MODE_POLL:
303            return rtems_termios_open(major, minor, arg, &poll_callbacks);
304
305        default:
306            rtems_fatal_error_occurred(0xC07A1310);
307    }
308    return RTEMS_INTERNAL_ERROR;
309}
310
311/* console_close --
312 *     Close console device.
313 *
314 * PARAMETERS:
315 *     major - major device number for console devices
316 *     minor - minor device number for console
317 *     arg - device close argument
318 *
319 * RETURNS:
320 *     RTEMS error code
321 */
322rtems_device_driver
323console_close(rtems_device_major_number major,
324              rtems_device_minor_number minor,
325              void *arg)
326{
327    if (console_mode != CONSOLE_MODE_RAW)
328        return rtems_termios_close (arg);
329    else
330        return RTEMS_SUCCESSFUL;
331}
332
333/* console_read --
334 *     Read from the console device
335 *
336 * PARAMETERS:
337 *     major - major device number for console devices
338 *     minor - minor device number for console
339 *     arg - device read argument
340 *
341 * RETURNS:
342 *     RTEMS error code
343 */
344rtems_device_driver
345console_read(rtems_device_major_number major,
346             rtems_device_minor_number minor,
347             void *arg)
348{
349    if (console_mode != CONSOLE_MODE_RAW)
350    {
351        return rtems_termios_read (arg);
352    }
353    else
354    {
355        rtems_libio_rw_args_t *argp = arg;
356        char *buf = argp->buffer;
357        int count = argp->count;
358        int n = 0;
359        int c;
360        while (n < count)
361        {
362            do {
363                c = mcfuart_poll_read(&uart[minor]);
364            } while (c == -1);
365            if (c == '\r')
366                c = '\n';
367            *(buf++) = c;
368            n++;
369            if (c == '\n')
370                break;
371        }
372        argp->bytes_moved = n;
373        return RTEMS_SUCCESSFUL;
374    }
375}
376
377/* console_write --
378 *     Write to the console device
379 *
380 * PARAMETERS:
381 *     major - major device number for console devices
382 *     minor - minor device number for console
383 *     arg - device write argument
384 *
385 * RETURNS:
386 *     RTEMS error code
387 */
388rtems_device_driver
389console_write(rtems_device_major_number major,
390              rtems_device_minor_number minor,
391              void *arg
392)
393{
394    if (console_mode != CONSOLE_MODE_RAW)
395    {
396        return rtems_termios_write (arg);
397    }
398    else
399    {
400        rtems_libio_rw_args_t *argp = arg;
401        char cr = '\r';
402        char *buf = argp->buffer;
403        int count = argp->count;
404        int i;
405        for (i = 0; i < count; i++)
406        {
407            if (*buf == '\n')
408                mcfuart_poll_write(&uart[minor], &cr, 1);
409            mcfuart_poll_write(&uart[minor], buf, 1);
410            buf++;
411        }
412        argp->bytes_moved = count;
413        return RTEMS_SUCCESSFUL;
414    }
415}
416
417/* console_control --
418 *     Handle console device I/O control (IOCTL)
419 *
420 * PARAMETERS:
421 *     major - major device number for console devices
422 *     minor - minor device number for console
423 *     arg - device ioctl argument
424 *
425 * RETURNS:
426 *     RTEMS error code
427 */
428rtems_device_driver
429console_control(rtems_device_major_number major,
430                rtems_device_minor_number minor,
431                void *arg)
432{
433    if (console_mode != CONSOLE_MODE_RAW)
434    {
435        return rtems_termios_ioctl (arg);
436    }
437    else
438    {
439        return RTEMS_SUCCESSFUL;
440    }
441}
Note: See TracBrowser for help on using the repository browser.