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

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

Start hiding the architecture-specific drivers behind the
hal_drivers_xx interface.

File size: 6.4 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
[3]39extern chdev_pic_input_t  chdev_pic_input;   // allocated in kernel_init.c
[1]40
[3]41////////////////////////////////////
42void dev_txt_init( chdev_t * chdev )
[1]43{
[3]44    // the local ICU chdev must be initialized before the TXT chdev, because
[49]45    // the TXT chdev initialization requires allocation of a WTI from local ICU
[3]46    xptr_t  icu_xp  = chdev_dir.icu[local_cxy];
47    assert( (icu_xp != XPTR_NULL) , __FUNCTION__ , "ICU not initialised before TXT" );
[1]48
[3]49    // get implementation and channel index
50    uint32_t  impl    = chdev->impl;
51    uint32_t  channel = chdev->channel;
[1]52
[23]53    // set chdev name
54    snprintf( chdev->name , 16 , "txt_%d" , chdev->channel );
55
[3]56    // set fields "cmd", "isr", and call driver init function
[1]57    if( impl == IMPL_TXT_TTY )
58    {
[77]59        hal_drivers_txt_init( chdev );
[1]60    }
61    else
62    {
[3]63        assert( false , __FUNCTION__ , "undefined TXT device implementation" );
[1]64    }
65
[3]66    // get a WTI mailbox from local ICU
67    uint32_t wti_id = dev_icu_wti_alloc();
68
69    assert( (wti_id != -1) , __FUNCTION__ , "cannot allocate WTI mailbox" );
70
71    // select a core
72    lid_t lid = cluster_select_local_core();
73
74    // enable WTI IRQ and update WTI interrupt vector
75    dev_icu_enable_irq( lid , WTI_TYPE , wti_id , chdev );
76
[49]77    // link IOC IRQ to WTI mailbox in PIC component
[3]78    uint32_t irq_id = chdev_pic_input.txt[channel];
79    dev_pic_bind_irq( irq_id , local_cxy , wti_id );
80
[1]81    // create server thread
[3]82    thread_t * new_thread;
[1]83    error_t    error;
84
[3]85    error = thread_kernel_create( &new_thread,
86                                  THREAD_DEV,
87                                  &chdev_sequencial_server,
88                                  chdev,
[49]89                                  lid );
[3]90    assert( (error == 0) , __FUNCTION__ , "cannot create server thread" );
[1]91
[3]92    // set "server" field in chdev descriptor
93    chdev->server = new_thread;
[49]94
[1]95    // start server thread
[3]96    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
[49]97}
[1]98
99//////////////////////////////////////////////////////////////////////////////////
[3]100// This static function is called by dev_txt_read(), dev_txt_write() functions.
[1]101////////////////////////////////////i/////////////////////////////////////////////
102static error_t dev_txt_access( uint32_t   type,
103                               uint32_t   channel,
104                               char     * buffer,
105                               uint32_t   count )
106{
107    thread_t * this = CURRENT_THREAD;
108
[49]109    txt_dmsg("\n[INFO] in %s : thread %x in process %x enters\n",
[1]110                 __FUNCTION__ , this->trdid , this->process->pid );
111
112    // check channel argument
[3]113    assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" );
[1]114
[3]115    // get extended pointer on remote TXT chdev descriptor
116    xptr_t  dev_xp = chdev_dir.txt[channel];
[1]117
[3]118    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" );
[1]119
120    // register command in calling thread descriptor
[3]121    this->command.txt.dev_xp  = dev_xp;
122    this->command.txt.type    = type;
123    this->command.txt.buf_xp  = XPTR( local_cxy , buffer );
124    this->command.txt.count   = count;
[1]125
[3]126    // register client thread in waiting queue, activate server thread
127    // block client thread on THREAD_BLOCKED_IO and deschedule.
128    // it is re-activated by the ISR signaling IO operation completion.
129    chdev_register_command( dev_xp , this );
[1]130
[49]131    txt_dmsg("\n[INFO] in %s : thread %x in process %x completes / error = %d\n",
[3]132             __FUNCTION__ , this->trdid , this->process->pid , this->command.txt.error );
[1]133
134    // return I/O operation status from calling thread descriptor
[49]135    return this->command.txt.error;
136}
[1]137
138/////////////////////////////////////////
139error_t dev_txt_write( uint32_t   channel,
140                       char     * buffer,
141                       uint32_t   count )
142{
143    return dev_txt_access( TXT_WRITE , channel , buffer , count );
144}
[49]145
[1]146/////////////////////////////////////////
147error_t dev_txt_read( uint32_t   channel,
148                      char     * buffer )
149{
150    return dev_txt_access( TXT_READ , channel , buffer , 1 );
151}
152
[3]153///////////////////////////////////////////////
154error_t dev_txt_sync_write( uint32_t   channel,
155                            char     * buffer,
156                            uint32_t   count )
[1]157{
[49]158    // get pointer on calling thread
[3]159    thread_t * this = CURRENT_THREAD;
[1]160
[49]161    // get extended pointer on TXT[0] chdev
[3]162    xptr_t  dev_xp = chdev_dir.txt[channel];
[1]163
[23]164    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT0 chdev descriptor" );
165
[49]166    // register command in calling thread
[3]167    this->command.txt.dev_xp  = dev_xp;
168    this->command.txt.type    = TXT_SYNC_WRITE;
169    this->command.txt.buf_xp  = XPTR( local_cxy , buffer );
170    this->command.txt.count   = count;
[1]171
[49]172    // get driver command function
[3]173    cxy_t       dev_cxy = GET_CXY( dev_xp );
174    chdev_t   * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
175    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) );
[1]176
[49]177    // call directly driver command
[3]178    cmd( XPTR( local_cxy , this ) );
[1]179
[3]180    // return I/O operation status from calling thread descriptor
181    return this->command.txt.error;
[49]182}
[1]183
Note: See TracBrowser for help on using the repository browser.