Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:54

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @brief Generic console driver implementation.
0007  */
0008 
0009 /*
0010  * Copyright (c) 2011 embedded brains GmbH & Co. KG
0011  *
0012  * Redistribution and use in source and binary forms, with or without
0013  * modification, are permitted provided that the following conditions
0014  * are met:
0015  * 1. Redistributions of source code must retain the above copyright
0016  *    notice, this list of conditions and the following disclaimer.
0017  * 2. Redistributions in binary form must reproduce the above copyright
0018  *    notice, this list of conditions and the following disclaimer in the
0019  *    documentation and/or other materials provided with the distribution.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0031  * POSSIBILITY OF SUCH DAMAGE.
0032  */
0033 
0034 #include <sys/cdefs.h>
0035 
0036 #include <bsp.h>
0037 #include <bsp/console-generic.h>
0038 #include <bsp/fatal.h>
0039 
0040 #include <rtems/bspIo.h>
0041 #include <rtems/console.h>
0042 
0043 static const struct termios console_generic_termios = {
0044   .c_cflag = CS8 | CREAD | CLOCAL | __CONCAT(B, BSP_DEFAULT_BAUD_RATE)
0045 };
0046 
0047 static void console_generic_char_out(char c)
0048 {
0049   int minor = (int) console_generic_minor;
0050   const console_generic_callbacks *cb =
0051     console_generic_info_table [minor].callbacks;
0052 
0053   (*cb->poll_write)(minor, c);
0054 }
0055 
0056 static int console_generic_char_in(void)
0057 {
0058   int minor = (int) console_generic_minor;
0059   const console_generic_callbacks *cb =
0060     console_generic_info_table [minor].callbacks;
0061 
0062   return (*cb->poll_read)(minor);
0063 }
0064 
0065 static void console_generic_char_out_do_init(void)
0066 {
0067   int minor = (int) console_generic_minor;
0068   const console_generic_callbacks *cb =
0069     console_generic_info_table [minor].callbacks;
0070   const struct termios *term = &console_generic_termios;
0071 
0072   BSP_output_char = console_generic_char_out;
0073   (*cb->termios_callbacks.setAttributes)(minor, term);
0074 }
0075 
0076 static void console_generic_char_out_init(char c)
0077 {
0078   console_generic_char_out_do_init();
0079   console_generic_char_out(c);
0080 }
0081 
0082 rtems_device_driver console_initialize(
0083   rtems_device_major_number major,
0084   rtems_device_minor_number minor,
0085   void *arg
0086 )
0087 {
0088   rtems_status_code sc = RTEMS_SUCCESSFUL;
0089   const console_generic_info *info_table = console_generic_info_table;
0090   rtems_device_minor_number count = console_generic_info_count;
0091   rtems_device_minor_number console = console_generic_minor;
0092 
0093   if (count <= 0) {
0094     bsp_fatal(MPC55XX_FATAL_CONSOLE_GENERIC_COUNT);
0095   }
0096 
0097   rtems_termios_initialize();
0098 
0099   for (minor = 0; minor < count; ++minor) {
0100     const console_generic_info *info = info_table + minor;
0101 
0102     sc = rtems_io_register_name(info->device_path, major, minor);
0103     if (sc != RTEMS_SUCCESSFUL) {
0104       bsp_fatal(MPC55XX_FATAL_CONSOLE_GENERIC_REGISTER);
0105     }
0106   }
0107 
0108   sc = rtems_io_register_name(CONSOLE_DEVICE_NAME, major, console);
0109   if (sc != RTEMS_SUCCESSFUL) {
0110     bsp_fatal(MPC55XX_FATAL_CONSOLE_GENERIC_REGISTER_CONSOLE);
0111   }
0112 
0113   console_generic_char_out_do_init();
0114 
0115   return sc;
0116 }
0117 
0118 rtems_device_driver console_open(
0119   rtems_device_major_number major,
0120   rtems_device_minor_number minor,
0121   void *arg
0122 )
0123 {
0124   rtems_status_code sc = RTEMS_SUCCESSFUL;
0125   rtems_device_minor_number count = console_generic_info_count;
0126 
0127   if (minor < count) {
0128     const console_generic_info *info = &console_generic_info_table [minor];
0129 
0130     sc = rtems_termios_open(
0131       major,
0132       minor,
0133       arg,
0134       &info->callbacks->termios_callbacks
0135     );
0136   } else {
0137     sc = RTEMS_INVALID_ID;
0138   }
0139 
0140   return sc;
0141 }
0142 
0143 rtems_device_driver console_close(
0144   rtems_device_major_number major,
0145   rtems_device_minor_number minor,
0146   void *arg
0147 )
0148 {
0149   return rtems_termios_close(arg);
0150 }
0151 
0152 rtems_device_driver console_read(
0153   rtems_device_major_number major,
0154   rtems_device_minor_number minor,
0155   void *arg
0156 )
0157 {
0158   return rtems_termios_read(arg);
0159 }
0160 
0161 rtems_device_driver console_write(
0162   rtems_device_major_number major,
0163   rtems_device_minor_number minor,
0164   void *arg
0165 )
0166 {
0167   return rtems_termios_write(arg);
0168 }
0169 
0170 rtems_device_driver console_control(
0171   rtems_device_major_number major,
0172   rtems_device_minor_number minor,
0173   void *arg
0174 )
0175 {
0176   return rtems_termios_ioctl(arg);
0177 }
0178 
0179 BSP_output_char_function_type BSP_output_char = console_generic_char_out_init;
0180 
0181 BSP_polling_getchar_function_type BSP_poll_char = console_generic_char_in;