| 1 | /* | 
|---|
| 2 |  * dev_txt.c - TXT (Text Terminal) generic device API implementation. | 
|---|
| 3 |  * | 
|---|
| 4 |  * Author  Alain Greiner    (2016) | 
|---|
| 5 |  * | 
|---|
| 6 |  * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 7 |  * | 
|---|
| 8 |  * This file is part of ALMOS-MK | 
|---|
| 9 |  * | 
|---|
| 10 |  * ALMOS-MKH.is free software; you can redistribute it and/or modify it | 
|---|
| 11 |  * under the terms of the GNU General Public License as published by | 
|---|
| 12 |  * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 13 |  * | 
|---|
| 14 |  * ALMOS-MKH.is distributed in the hope that it will be useful, but | 
|---|
| 15 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 |  * General Public License for more details. | 
|---|
| 18 |  * | 
|---|
| 19 |  * You should have received a copy of the GNU General Public License | 
|---|
| 20 |  * along with ALMOS-kernel; if not, write to the Free Software Foundation, | 
|---|
| 21 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 |  */ | 
|---|
| 23 |  | 
|---|
| 24 | #include <hal_types.h> | 
|---|
| 25 | #include <hal_special.h> | 
|---|
| 26 | #include <hal_remote.h> | 
|---|
| 27 | #include <hal_drivers.h> | 
|---|
| 28 | #include <thread.h> | 
|---|
| 29 | #include <rpc.h> | 
|---|
| 30 | #include <printk.h> | 
|---|
| 31 | #include <dev_txt.h> | 
|---|
| 32 | #include <soclib_tty.h> | 
|---|
| 33 |  | 
|---|
| 34 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 35 | // Extern global variables | 
|---|
| 36 | ///////////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 37 |  | 
|---|
| 38 | extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c | 
|---|
| 39 |  | 
|---|
| 40 | ////////////////////////////////// | 
|---|
| 41 | void dev_txt_init( chdev_t * txt ) | 
|---|
| 42 | { | 
|---|
| 43 |     // For all TXT channels other than the TXT0 (kernel terminal),  | 
|---|
| 44 |     // the PIC chdev must be initialized before the TXT chdev, because | 
|---|
| 45 |     // the TXT chdev initialization requires the routing of an external IRQ | 
|---|
| 46 |  | 
|---|
| 47 |     xptr_t    pic_xp  = chdev_dir.pic; | 
|---|
| 48 |     uint32_t  channel = txt->channel; | 
|---|
| 49 |     uint32_t  impl    = txt->impl; | 
|---|
| 50 |  | 
|---|
| 51 |     assert( (pic_xp != XPTR_NULL) || (channel == 0) , __FUNCTION__ ,  | 
|---|
| 52 |             "PIC not initialised before TXT" ); | 
|---|
| 53 |  | 
|---|
| 54 |     // set chdev name | 
|---|
| 55 |     snprintf( txt->name , 16 , "txt_%d" , channel ); | 
|---|
| 56 |  | 
|---|
| 57 |     // set fields "cmd", "isr", and call driver init function | 
|---|
| 58 |     if( impl == IMPL_TXT_TTY ) | 
|---|
| 59 |     { | 
|---|
| 60 |         txt->cmd = &soclib_tty_cmd; | 
|---|
| 61 |         txt->isr = &soclib_tty_isr; | 
|---|
| 62 |         soclib_tty_init( txt ); | 
|---|
| 63 |     } | 
|---|
| 64 |     else | 
|---|
| 65 |     { | 
|---|
| 66 |         assert( false , __FUNCTION__ , "undefined TXT device implementation" ); | 
|---|
| 67 |     } | 
|---|
| 68 |  | 
|---|
| 69 |     // no server thread and no IRQ routing for TXT0 | 
|---|
| 70 |     if( channel != 0 ) | 
|---|
| 71 |     { | 
|---|
| 72 |         // select a core to execute the TXT server thread | 
|---|
| 73 |         lid_t lid = cluster_select_local_core(); | 
|---|
| 74 |  | 
|---|
| 75 |         // bind TXT IRQ to the selected core | 
|---|
| 76 |         dev_pic_bind_irq( lid , txt ); | 
|---|
| 77 |  | 
|---|
| 78 |         // enable TXT IRQ | 
|---|
| 79 |         dev_pic_enable_irq( lid , txt ); | 
|---|
| 80 |  | 
|---|
| 81 |         // create server thread | 
|---|
| 82 |         thread_t * new_thread; | 
|---|
| 83 |         error_t    error; | 
|---|
| 84 |  | 
|---|
| 85 |         error = thread_kernel_create( &new_thread, | 
|---|
| 86 |                                       THREAD_DEV, | 
|---|
| 87 |                                       &chdev_sequencial_server, | 
|---|
| 88 |                                       txt, | 
|---|
| 89 |                                       lid ); | 
|---|
| 90 |  | 
|---|
| 91 |         assert( (error == 0) , __FUNCTION__ , "cannot create server thread" ); | 
|---|
| 92 |  | 
|---|
| 93 |         // set "server" field in chdev descriptor | 
|---|
| 94 |         txt->server = new_thread; | 
|---|
| 95 |  | 
|---|
| 96 |         // start server thread | 
|---|
| 97 |         thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL ); | 
|---|
| 98 |     } | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | ////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 102 | // This static function is called by dev_txt_read(), dev_txt_write() functions. | 
|---|
| 103 | ////////////////////////////////////i///////////////////////////////////////////// | 
|---|
| 104 | static error_t dev_txt_access( uint32_t   type, | 
|---|
| 105 |                                uint32_t   channel, | 
|---|
| 106 |                                char     * buffer, | 
|---|
| 107 |                                uint32_t   count ) | 
|---|
| 108 | { | 
|---|
| 109 |     thread_t * this = CURRENT_THREAD; | 
|---|
| 110 |  | 
|---|
| 111 |     txt_dmsg("\n[INFO] in %s : thread %x in process %x enters\n", | 
|---|
| 112 |                  __FUNCTION__ , this->trdid , this->process->pid ); | 
|---|
| 113 |  | 
|---|
| 114 |     // check channel argument | 
|---|
| 115 |     assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" ); | 
|---|
| 116 |  | 
|---|
| 117 |     // get extended pointer on remote TXT chdev descriptor | 
|---|
| 118 |     xptr_t  dev_xp = chdev_dir.txt[channel]; | 
|---|
| 119 |  | 
|---|
| 120 |     assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" ); | 
|---|
| 121 |  | 
|---|
| 122 |     // register command in calling thread descriptor | 
|---|
| 123 |     this->command.txt.dev_xp  = dev_xp; | 
|---|
| 124 |     this->command.txt.type    = type; | 
|---|
| 125 |     this->command.txt.buf_xp  = XPTR( local_cxy , buffer ); | 
|---|
| 126 |     this->command.txt.count   = count; | 
|---|
| 127 |  | 
|---|
| 128 |     // register client thread in waiting queue, activate server thread | 
|---|
| 129 |     // block client thread on THREAD_BLOCKED_IO and deschedule. | 
|---|
| 130 |     // it is re-activated by the ISR signaling IO operation completion. | 
|---|
| 131 |     chdev_register_command( dev_xp , this ); | 
|---|
| 132 |  | 
|---|
| 133 |     txt_dmsg("\n[INFO] in %s : thread %x in process %x completes / error = %d\n", | 
|---|
| 134 |              __FUNCTION__ , this->trdid , this->process->pid , this->command.txt.error ); | 
|---|
| 135 |  | 
|---|
| 136 |     // return I/O operation status from calling thread descriptor | 
|---|
| 137 |     return this->command.txt.error; | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | ///////////////////////////////////////// | 
|---|
| 141 | error_t dev_txt_write( uint32_t   channel, | 
|---|
| 142 |                        char     * buffer, | 
|---|
| 143 |                        uint32_t   count ) | 
|---|
| 144 | { | 
|---|
| 145 |     return dev_txt_access( TXT_WRITE , channel , buffer , count ); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | ///////////////////////////////////////// | 
|---|
| 149 | error_t dev_txt_read( uint32_t   channel, | 
|---|
| 150 |                       char     * buffer ) | 
|---|
| 151 | { | 
|---|
| 152 |     return dev_txt_access( TXT_READ , channel , buffer , 1 ); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | /////////////////////////////////////////////// | 
|---|
| 156 | error_t dev_txt_sync_write( uint32_t   channel, | 
|---|
| 157 |                             char     * buffer, | 
|---|
| 158 |                             uint32_t   count ) | 
|---|
| 159 | { | 
|---|
| 160 |     // get pointer on calling thread | 
|---|
| 161 |     thread_t * this = CURRENT_THREAD; | 
|---|
| 162 |  | 
|---|
| 163 |     // get extended pointer on TXT[0] chdev | 
|---|
| 164 |     xptr_t  dev_xp = chdev_dir.txt[channel]; | 
|---|
| 165 |  | 
|---|
| 166 |     assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT0 chdev descriptor" ); | 
|---|
| 167 |  | 
|---|
| 168 |     // register command in calling thread | 
|---|
| 169 |     this->command.txt.dev_xp  = dev_xp; | 
|---|
| 170 |     this->command.txt.type    = TXT_SYNC_WRITE; | 
|---|
| 171 |     this->command.txt.buf_xp  = XPTR( local_cxy , buffer ); | 
|---|
| 172 |     this->command.txt.count   = count; | 
|---|
| 173 |  | 
|---|
| 174 |     // get driver command function | 
|---|
| 175 |     cxy_t       dev_cxy = GET_CXY( dev_xp ); | 
|---|
| 176 |     chdev_t   * dev_ptr = (chdev_t *)GET_PTR( dev_xp ); | 
|---|
| 177 |     dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) ); | 
|---|
| 178 |  | 
|---|
| 179 |     // call directly driver command | 
|---|
| 180 |     cmd( XPTR( local_cxy , this ) ); | 
|---|
| 181 |  | 
|---|
| 182 |     // return I/O operation status from calling thread descriptor | 
|---|
| 183 |     return this->command.txt.error; | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|