source: trunk/kernel/devices/dev_txt.c @ 245

Last change on this file since 245 was 245, checked in by max@…, 7 years ago

Hide soclib_tty.

File size: 6.0 KB
RevLine 
[1]1/*
2 * dev_txt.c - TXT (Text Terminal) generic device API implementation.
[49]3 *
[1]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>
[77]27#include <hal_drivers.h>
[1]28#include <thread.h>
29#include <rpc.h>
30#include <printk.h>
31#include <dev_txt.h>
32
33/////////////////////////////////////////////////////////////////////////////////////////
34// Extern global variables
35/////////////////////////////////////////////////////////////////////////////////////////
36
[3]37extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
[1]38
[188]39//////////////////////////////////
40void dev_txt_init( chdev_t * txt )
[1]41{
[188]42    // For all TXT channels other than the TXT0 (kernel terminal),
43    // the PIC chdev must be initialized before the TXT chdev, because
44    // the TXT chdev initialization requires the routing of an external IRQ
[1]45
[188]46    xptr_t    pic_xp  = chdev_dir.pic;
47    uint32_t  channel = txt->channel;
48    uint32_t  impl    = txt->impl;
[1]49
[188]50    assert( (pic_xp != XPTR_NULL) || (channel == 0) , __FUNCTION__ , 
51            "PIC not initialised before TXT" );
52
[23]53    // set chdev name
[188]54    snprintf( txt->name , 16 , "txt_%d" , channel );
[23]55
[245]56    // call driver init function
57    hal_drivers_txt_init(txt, impl);
[1]58
[188]59    // no server thread and no IRQ routing for TXT0
60    if( channel != 0 )
61    {
62        // select a core to execute the TXT server thread
63        lid_t lid = cluster_select_local_core();
[3]64
[188]65        // bind TXT IRQ to the selected core
66        dev_pic_bind_irq( lid , txt );
[3]67
[188]68        // enable TXT IRQ
[238]69        dev_pic_enable_irq( lid , XPTR( local_cxy , txt ) );
[3]70
[188]71        // create server thread
72        thread_t * new_thread;
73        error_t    error;
[3]74
[188]75        error = thread_kernel_create( &new_thread,
76                                      THREAD_DEV,
77                                      &chdev_sequencial_server,
78                                      txt,
79                                      lid );
[3]80
[188]81        assert( (error == 0) , __FUNCTION__ , "cannot create server thread" );
[1]82
[188]83        // set "server" field in chdev descriptor
84        txt->server = new_thread;
[1]85
[188]86        // start server thread
87        thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
88    }
[49]89}
[1]90
91//////////////////////////////////////////////////////////////////////////////////
[3]92// This static function is called by dev_txt_read(), dev_txt_write() functions.
[1]93////////////////////////////////////i/////////////////////////////////////////////
94static error_t dev_txt_access( uint32_t   type,
95                               uint32_t   channel,
96                               char     * buffer,
97                               uint32_t   count )
98{
99    thread_t * this = CURRENT_THREAD;
100
[49]101    txt_dmsg("\n[INFO] in %s : thread %x in process %x enters\n",
[1]102                 __FUNCTION__ , this->trdid , this->process->pid );
103
104    // check channel argument
[3]105    assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" );
[1]106
[3]107    // get extended pointer on remote TXT chdev descriptor
108    xptr_t  dev_xp = chdev_dir.txt[channel];
[1]109
[3]110    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" );
[1]111
112    // register command in calling thread descriptor
[3]113    this->command.txt.dev_xp  = dev_xp;
114    this->command.txt.type    = type;
115    this->command.txt.buf_xp  = XPTR( local_cxy , buffer );
116    this->command.txt.count   = count;
[1]117
[3]118    // register client thread in waiting queue, activate server thread
119    // block client thread on THREAD_BLOCKED_IO and deschedule.
120    // it is re-activated by the ISR signaling IO operation completion.
121    chdev_register_command( dev_xp , this );
[1]122
[49]123    txt_dmsg("\n[INFO] in %s : thread %x in process %x completes / error = %d\n",
[3]124             __FUNCTION__ , this->trdid , this->process->pid , this->command.txt.error );
[1]125
126    // return I/O operation status from calling thread descriptor
[49]127    return this->command.txt.error;
128}
[1]129
130/////////////////////////////////////////
131error_t dev_txt_write( uint32_t   channel,
132                       char     * buffer,
133                       uint32_t   count )
134{
135    return dev_txt_access( TXT_WRITE , channel , buffer , count );
136}
[49]137
[1]138/////////////////////////////////////////
139error_t dev_txt_read( uint32_t   channel,
140                      char     * buffer )
141{
142    return dev_txt_access( TXT_READ , channel , buffer , 1 );
143}
144
[3]145///////////////////////////////////////////////
146error_t dev_txt_sync_write( uint32_t   channel,
147                            char     * buffer,
148                            uint32_t   count )
[1]149{
[49]150    // get pointer on calling thread
[3]151    thread_t * this = CURRENT_THREAD;
[1]152
[49]153    // get extended pointer on TXT[0] chdev
[3]154    xptr_t  dev_xp = chdev_dir.txt[channel];
[1]155
[23]156    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT0 chdev descriptor" );
157
[49]158    // register command in calling thread
[3]159    this->command.txt.dev_xp  = dev_xp;
160    this->command.txt.type    = TXT_SYNC_WRITE;
161    this->command.txt.buf_xp  = XPTR( local_cxy , buffer );
162    this->command.txt.count   = count;
[1]163
[49]164    // get driver command function
[3]165    cxy_t       dev_cxy = GET_CXY( dev_xp );
166    chdev_t   * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
167    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) );
[1]168
[49]169    // call directly driver command
[3]170    cmd( XPTR( local_cxy , this ) );
[1]171
[3]172    // return I/O operation status from calling thread descriptor
173    return this->command.txt.error;
[49]174}
[1]175
Note: See TracBrowser for help on using the repository browser.